aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorPaul Moore <pmoore@redhat.com>2014-01-28 14:44:16 -0500
committerPaul Moore <pmoore@redhat.com>2014-02-05 10:39:48 -0500
commit825e587af2e90e9b953849f3347a01d8f383d577 (patch)
treee48942a05882da47544e179c6a0c920e00137a6a /net
parent8ed814602876bec9bad2649ca17f34b499357a1c (diff)
parentd8ec26d7f8287f5788a494f56e8814210f0e64be (diff)
Merge tag 'v3.13' into stable-3.14
Linux 3.13 Conflicts: security/selinux/hooks.c Trivial merge issue in selinux_inet_conn_request() likely due to me including patches that I sent to the stable folks in my next tree resulting in the patch hitting twice (I think). Thankfully it was an easy fix this time, but regardless, lesson learned, I will not do that again.
Diffstat (limited to 'net')
-rw-r--r--net/8021q/vlan.c18
-rw-r--r--net/8021q/vlan.h105
-rw-r--r--net/8021q/vlan_dev.c51
-rw-r--r--net/9p/trans_fd.c4
-rw-r--r--net/9p/trans_virtio.c9
-rw-r--r--net/Kconfig5
-rw-r--r--net/Makefile1
-rw-r--r--net/appletalk/ddp.c16
-rw-r--r--net/atm/common.c2
-rw-r--r--net/ax25/af_ax25.c6
-rw-r--r--net/batman-adv/Makefile3
-rw-r--r--net/batman-adv/bat_iv_ogm.c546
-rw-r--r--net/batman-adv/bridge_loop_avoidance.c64
-rw-r--r--net/batman-adv/bridge_loop_avoidance.h10
-rw-r--r--net/batman-adv/debugfs.c9
-rw-r--r--net/batman-adv/distributed-arp-table.c237
-rw-r--r--net/batman-adv/distributed-arp-table.h5
-rw-r--r--net/batman-adv/fragmentation.c491
-rw-r--r--net/batman-adv/fragmentation.h50
-rw-r--r--net/batman-adv/gateway_client.c247
-rw-r--r--net/batman-adv/gateway_client.h2
-rw-r--r--net/batman-adv/gateway_common.c230
-rw-r--r--net/batman-adv/gateway_common.h14
-rw-r--r--net/batman-adv/hard-interface.c110
-rw-r--r--net/batman-adv/hard-interface.h2
-rw-r--r--net/batman-adv/icmp_socket.c128
-rw-r--r--net/batman-adv/icmp_socket.h2
-rw-r--r--net/batman-adv/main.c695
-rw-r--r--net/batman-adv/main.h68
-rw-r--r--net/batman-adv/network-coding.c93
-rw-r--r--net/batman-adv/network-coding.h5
-rw-r--r--net/batman-adv/originator.c364
-rw-r--r--net/batman-adv/originator.h13
-rw-r--r--net/batman-adv/packet.h442
-rw-r--r--net/batman-adv/routing.c557
-rw-r--r--net/batman-adv/routing.h13
-rw-r--r--net/batman-adv/send.c241
-rw-r--r--net/batman-adv/send.h53
-rw-r--r--net/batman-adv/soft-interface.c283
-rw-r--r--net/batman-adv/soft-interface.h4
-rw-r--r--net/batman-adv/sysfs.c274
-rw-r--r--net/batman-adv/sysfs.h10
-rw-r--r--net/batman-adv/translation-table.c2088
-rw-r--r--net/batman-adv/translation-table.h46
-rw-r--r--net/batman-adv/types.h436
-rw-r--r--net/batman-adv/unicast.c491
-rw-r--r--net/batman-adv/unicast.h92
-rw-r--r--net/batman-adv/vis.c938
-rw-r--r--net/batman-adv/vis.h36
-rw-r--r--net/bluetooth/Makefile2
-rw-r--r--net/bluetooth/a2mp.c72
-rw-r--r--net/bluetooth/a2mp.h150
-rw-r--r--net/bluetooth/af_bluetooth.c70
-rw-r--r--net/bluetooth/amp.c10
-rw-r--r--net/bluetooth/amp.h54
-rw-r--r--net/bluetooth/bnep/core.c18
-rw-r--r--net/bluetooth/cmtp/core.c6
-rw-r--r--net/bluetooth/hci_conn.c194
-rw-r--r--net/bluetooth/hci_core.c1128
-rw-r--r--net/bluetooth/hci_event.c175
-rw-r--r--net/bluetooth/hci_sock.c218
-rw-r--r--net/bluetooth/hci_sysfs.c373
-rw-r--r--net/bluetooth/hidp/core.c18
-rw-r--r--net/bluetooth/hidp/hidp.h4
-rw-r--r--net/bluetooth/l2cap_core.c541
-rw-r--r--net/bluetooth/l2cap_sock.c222
-rw-r--r--net/bluetooth/mgmt.c1609
-rw-r--r--net/bluetooth/rfcomm/core.c36
-rw-r--r--net/bluetooth/rfcomm/sock.c56
-rw-r--r--net/bluetooth/sco.c58
-rw-r--r--net/bluetooth/smp.c101
-rw-r--r--net/bluetooth/smp.h146
-rw-r--r--net/bridge/br_device.c9
-rw-r--r--net/bridge/br_if.c3
-rw-r--r--net/bridge/br_input.c2
-rw-r--r--net/bridge/br_multicast.c52
-rw-r--r--net/bridge/br_netfilter.c24
-rw-r--r--net/bridge/br_private.h312
-rw-r--r--net/bridge/br_private_stp.h24
-rw-r--r--net/bridge/br_stp_bpdu.c2
-rw-r--r--net/bridge/br_vlan.c24
-rw-r--r--net/bridge/netfilter/Kconfig4
-rw-r--r--net/bridge/netfilter/Makefile2
-rw-r--r--net/bridge/netfilter/ebt_among.c2
-rw-r--r--net/bridge/netfilter/ebt_ip6.c8
-rw-r--r--net/bridge/netfilter/ebt_ulog.c9
-rw-r--r--net/bridge/netfilter/ebtable_filter.c16
-rw-r--r--net/bridge/netfilter/ebtable_nat.c16
-rw-r--r--net/bridge/netfilter/nf_tables_bridge.c102
-rw-r--r--net/caif/caif_socket.c4
-rw-r--r--net/caif/cfpkt_skbuff.c12
-rw-r--r--net/can/af_can.c2
-rw-r--r--net/can/af_can.h6
-rw-r--r--net/ceph/auth_none.h2
-rw-r--r--net/ceph/auth_x.h2
-rw-r--r--net/ceph/crypto.h48
-rw-r--r--net/compat.c5
-rw-r--r--net/core/datagram.c2
-rw-r--r--net/core/dev.c573
-rw-r--r--net/core/dev_addr_lists.c4
-rw-r--r--net/core/drop_monitor.c16
-rw-r--r--net/core/ethtool.c3
-rw-r--r--net/core/fib_rules.c3
-rw-r--r--net/core/filter.c30
-rw-r--r--net/core/flow_dissector.c91
-rw-r--r--net/core/iovec.c5
-rw-r--r--net/core/neighbour.c5
-rw-r--r--net/core/net-sysfs.c16
-rw-r--r--net/core/netpoll.c44
-rw-r--r--net/core/netprio_cgroup.c3
-rw-r--r--net/core/pktgen.c7
-rw-r--r--net/core/rtnetlink.c12
-rw-r--r--net/core/secure_seq.c16
-rw-r--r--net/core/skbuff.c220
-rw-r--r--net/core/sock.c47
-rw-r--r--net/core/utils.c49
-rw-r--r--net/dccp/ackvec.h21
-rw-r--r--net/dccp/ccid.h18
-rw-r--r--net/dccp/ccids/lib/loss_interval.h8
-rw-r--r--net/dccp/ccids/lib/packet_history.h25
-rw-r--r--net/dccp/ccids/lib/tfrc.h22
-rw-r--r--net/dccp/dccp.h186
-rw-r--r--net/dccp/feat.h26
-rw-r--r--net/dccp/ipv4.c19
-rw-r--r--net/dccp/ipv6.c84
-rw-r--r--net/dccp/ipv6.h2
-rw-r--r--net/dccp/minisocks.c15
-rw-r--r--net/dccp/output.c4
-rw-r--r--net/dccp/probe.c19
-rw-r--r--net/dccp/proto.c4
-rw-r--r--net/decnet/netfilter/dn_rtmsg.c2
-rw-r--r--net/ethernet/eth.c30
-rw-r--r--net/hsr/Kconfig27
-rw-r--r--net/hsr/Makefile7
-rw-r--r--net/hsr/hsr_device.c596
-rw-r--r--net/hsr/hsr_device.h29
-rw-r--r--net/hsr/hsr_framereg.c504
-rw-r--r--net/hsr/hsr_framereg.h53
-rw-r--r--net/hsr/hsr_main.c469
-rw-r--r--net/hsr/hsr_main.h166
-rw-r--r--net/hsr/hsr_netlink.c465
-rw-r--r--net/hsr/hsr_netlink.h30
-rw-r--r--net/ieee802154/6lowpan.c56
-rw-r--r--net/ieee802154/dgram.c3
-rw-r--r--net/ieee802154/ieee802154.h21
-rw-r--r--net/ieee802154/netlink.c45
-rw-r--r--net/ieee802154/nl-mac.c79
-rw-r--r--net/ieee802154/nl-phy.c43
-rw-r--r--net/ipv4/af_inet.c108
-rw-r--r--net/ipv4/datagram.c2
-rw-r--r--net/ipv4/esp4.c49
-rw-r--r--net/ipv4/fib_frontend.c2
-rw-r--r--net/ipv4/fib_lookup.h26
-rw-r--r--net/ipv4/fib_rules.c5
-rw-r--r--net/ipv4/fib_semantics.c2
-rw-r--r--net/ipv4/fib_trie.c28
-rw-r--r--net/ipv4/gre_demux.c29
-rw-r--r--net/ipv4/gre_offload.c14
-rw-r--r--net/ipv4/icmp.c5
-rw-r--r--net/ipv4/inet_connection_sock.c54
-rw-r--r--net/ipv4/inet_diag.c141
-rw-r--r--net/ipv4/inet_fragment.c3
-rw-r--r--net/ipv4/inet_hashtables.c110
-rw-r--r--net/ipv4/inet_timewait_sock.c59
-rw-r--r--net/ipv4/ip_fragment.c1
-rw-r--r--net/ipv4/ip_gre.c1
-rw-r--r--net/ipv4/ip_output.c38
-rw-r--r--net/ipv4/ip_sockglue.c30
-rw-r--r--net/ipv4/ip_tunnel.c12
-rw-r--r--net/ipv4/ip_tunnel_core.c33
-rw-r--r--net/ipv4/ip_vti.c68
-rw-r--r--net/ipv4/ipip.c11
-rw-r--r--net/ipv4/ipmr.c7
-rw-r--r--net/ipv4/netfilter/Kconfig21
-rw-r--r--net/ipv4/netfilter/Makefile6
-rw-r--r--net/ipv4/netfilter/arp_tables.c5
-rw-r--r--net/ipv4/netfilter/arptable_filter.c5
-rw-r--r--net/ipv4/netfilter/ip_tables.c5
-rw-r--r--net/ipv4/netfilter/ipt_CLUSTERIP.c112
-rw-r--r--net/ipv4/netfilter/ipt_SYNPROXY.c4
-rw-r--r--net/ipv4/netfilter/ipt_ULOG.c7
-rw-r--r--net/ipv4/netfilter/iptable_filter.c7
-rw-r--r--net/ipv4/netfilter/iptable_mangle.c10
-rw-r--r--net/ipv4/netfilter/iptable_nat.c26
-rw-r--r--net/ipv4/netfilter/iptable_raw.c6
-rw-r--r--net/ipv4/netfilter/iptable_security.c7
-rw-r--r--net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c12
-rw-r--r--net/ipv4/netfilter/nf_defrag_ipv4.c6
-rw-r--r--net/ipv4/netfilter/nf_tables_arp.c102
-rw-r--r--net/ipv4/netfilter/nf_tables_ipv4.c127
-rw-r--r--net/ipv4/netfilter/nft_chain_nat_ipv4.c205
-rw-r--r--net/ipv4/netfilter/nft_chain_route_ipv4.c90
-rw-r--r--net/ipv4/netfilter/nft_reject_ipv4.c123
-rw-r--r--net/ipv4/ping.c100
-rw-r--r--net/ipv4/protocol.c8
-rw-r--r--net/ipv4/raw.c12
-rw-r--r--net/ipv4/route.c28
-rw-r--r--net/ipv4/syncookies.c80
-rw-r--r--net/ipv4/sysctl_net_ipv4.c137
-rw-r--r--net/ipv4/tcp.c27
-rw-r--r--net/ipv4/tcp_bic.c5
-rw-r--r--net/ipv4/tcp_cong.c47
-rw-r--r--net/ipv4/tcp_cubic.c5
-rw-r--r--net/ipv4/tcp_fastopen.c29
-rw-r--r--net/ipv4/tcp_highspeed.c4
-rw-r--r--net/ipv4/tcp_htcp.c4
-rw-r--r--net/ipv4/tcp_hybla.c5
-rw-r--r--net/ipv4/tcp_illinois.c5
-rw-r--r--net/ipv4/tcp_input.c171
-rw-r--r--net/ipv4/tcp_ipv4.c156
-rw-r--r--net/ipv4/tcp_lp.c5
-rw-r--r--net/ipv4/tcp_memcontrol.c95
-rw-r--r--net/ipv4/tcp_metrics.c88
-rw-r--r--net/ipv4/tcp_minisocks.c7
-rw-r--r--net/ipv4/tcp_offload.c52
-rw-r--r--net/ipv4/tcp_output.c40
-rw-r--r--net/ipv4/tcp_probe.c29
-rw-r--r--net/ipv4/tcp_scalable.c5
-rw-r--r--net/ipv4/tcp_timer.c9
-rw-r--r--net/ipv4/tcp_vegas.c11
-rw-r--r--net/ipv4/tcp_vegas.h10
-rw-r--r--net/ipv4/tcp_veno.c9
-rw-r--r--net/ipv4/tcp_yeah.c5
-rw-r--r--net/ipv4/udp.c288
-rw-r--r--net/ipv4/udp_impl.h36
-rw-r--r--net/ipv4/udp_offload.c38
-rw-r--r--net/ipv4/xfrm4_mode_tunnel.c16
-rw-r--r--net/ipv4/xfrm4_policy.c8
-rw-r--r--net/ipv6/Kconfig29
-rw-r--r--net/ipv6/Makefile1
-rw-r--r--net/ipv6/addrconf.c156
-rw-r--r--net/ipv6/af_inet6.c83
-rw-r--r--net/ipv6/datagram.c34
-rw-r--r--net/ipv6/esp6.c48
-rw-r--r--net/ipv6/fib6_rules.c6
-rw-r--r--net/ipv6/inet6_connection_sock.c33
-rw-r--r--net/ipv6/inet6_hashtables.c122
-rw-r--r--net/ipv6/ip6_fib.c205
-rw-r--r--net/ipv6/ip6_flowlabel.c67
-rw-r--r--net/ipv6/ip6_gre.c15
-rw-r--r--net/ipv6/ip6_offload.c40
-rw-r--r--net/ipv6/ip6_output.c45
-rw-r--r--net/ipv6/ip6_tunnel.c46
-rw-r--r--net/ipv6/ip6_vti.c1044
-rw-r--r--net/ipv6/ip6mr.c7
-rw-r--r--net/ipv6/ipv6_sockglue.c35
-rw-r--r--net/ipv6/ndisc.c5
-rw-r--r--net/ipv6/netfilter/Kconfig13
-rw-r--r--net/ipv6/netfilter/Makefile5
-rw-r--r--net/ipv6/netfilter/ip6_tables.c5
-rw-r--r--net/ipv6/netfilter/ip6t_REJECT.c7
-rw-r--r--net/ipv6/netfilter/ip6t_SYNPROXY.c4
-rw-r--r--net/ipv6/netfilter/ip6table_filter.c5
-rw-r--r--net/ipv6/netfilter/ip6table_mangle.c10
-rw-r--r--net/ipv6/netfilter/ip6table_nat.c27
-rw-r--r--net/ipv6/netfilter/ip6table_raw.c5
-rw-r--r--net/ipv6/netfilter/ip6table_security.c5
-rw-r--r--net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c66
-rw-r--r--net/ipv6/netfilter/nf_conntrack_reasm.c35
-rw-r--r--net/ipv6/netfilter/nf_defrag_ipv6_hooks.c11
-rw-r--r--net/ipv6/netfilter/nf_tables_ipv6.c127
-rw-r--r--net/ipv6/netfilter/nft_chain_nat_ipv6.c211
-rw-r--r--net/ipv6/netfilter/nft_chain_route_ipv6.c88
-rw-r--r--net/ipv6/ping.c5
-rw-r--r--net/ipv6/protocol.c4
-rw-r--r--net/ipv6/raw.c26
-rw-r--r--net/ipv6/reassembly.c12
-rw-r--r--net/ipv6/route.c109
-rw-r--r--net/ipv6/sit.c113
-rw-r--r--net/ipv6/syncookies.c75
-rw-r--r--net/ipv6/tcp_ipv6.c114
-rw-r--r--net/ipv6/tcpv6_offload.c34
-rw-r--r--net/ipv6/udp.c98
-rw-r--r--net/ipv6/udp_impl.h41
-rw-r--r--net/ipv6/udp_offload.c4
-rw-r--r--net/ipv6/xfrm6_mode_tunnel.c69
-rw-r--r--net/ipv6/xfrm6_policy.c8
-rw-r--r--net/ipx/af_ipx.c3
-rw-r--r--net/irda/af_irda.c9
-rw-r--r--net/irda/irnet/irnet.h15
-rw-r--r--net/irda/irnetlink.c5
-rw-r--r--net/iucv/af_iucv.c2
-rw-r--r--net/key/af_key.c1
-rw-r--r--net/l2tp/l2tp_core.c10
-rw-r--r--net/l2tp/l2tp_core.h57
-rw-r--r--net/l2tp/l2tp_debugfs.c5
-rw-r--r--net/l2tp/l2tp_ip.c4
-rw-r--r--net/l2tp/l2tp_ip6.c19
-rw-r--r--net/l2tp/l2tp_netlink.c13
-rw-r--r--net/l2tp/l2tp_ppp.c14
-rw-r--r--net/llc/af_llc.c7
-rw-r--r--net/mac80211/Kconfig12
-rw-r--r--net/mac80211/aes_ccm.c169
-rw-r--r--net/mac80211/aes_ccm.h14
-rw-r--r--net/mac80211/cfg.c247
-rw-r--r--net/mac80211/chan.c5
-rw-r--r--net/mac80211/debug.h10
-rw-r--r--net/mac80211/debugfs.c55
-rw-r--r--net/mac80211/debugfs_netdev.c25
-rw-r--r--net/mac80211/driver-ops.h27
-rw-r--r--net/mac80211/ht.c41
-rw-r--r--net/mac80211/ibss.c598
-rw-r--r--net/mac80211/ieee80211_i.h72
-rw-r--r--net/mac80211/iface.c14
-rw-r--r--net/mac80211/key.c2
-rw-r--r--net/mac80211/key.h2
-rw-r--r--net/mac80211/main.c3
-rw-r--r--net/mac80211/mesh.c306
-rw-r--r--net/mac80211/mesh_plink.c3
-rw-r--r--net/mac80211/mesh_ps.c33
-rw-r--r--net/mac80211/mlme.c453
-rw-r--r--net/mac80211/rate.c15
-rw-r--r--net/mac80211/rate.h12
-rw-r--r--net/mac80211/rc80211_minstrel.c14
-rw-r--r--net/mac80211/rc80211_minstrel_ht.c30
-rw-r--r--net/mac80211/rc80211_pid_debugfs.c26
-rw-r--r--net/mac80211/rx.c45
-rw-r--r--net/mac80211/scan.c5
-rw-r--r--net/mac80211/spectmgmt.c171
-rw-r--r--net/mac80211/sta_info.c72
-rw-r--r--net/mac80211/sta_info.h5
-rw-r--r--net/mac80211/status.c31
-rw-r--r--net/mac80211/trace.h39
-rw-r--r--net/mac80211/tx.c107
-rw-r--r--net/mac80211/util.c287
-rw-r--r--net/mac80211/vht.c4
-rw-r--r--net/mac80211/wpa.c44
-rw-r--r--net/mac802154/ieee802154_dev.c6
-rw-r--r--net/mac802154/wpan.c2
-rw-r--r--net/mpls/mpls_gso.c1
-rw-r--r--net/netfilter/Kconfig54
-rw-r--r--net/netfilter/Makefile18
-rw-r--r--net/netfilter/core.c2
-rw-r--r--net/netfilter/ipset/Kconfig20
-rw-r--r--net/netfilter/ipset/Makefile2
-rw-r--r--net/netfilter/ipset/ip_set_bitmap_gen.h174
-rw-r--r--net/netfilter/ipset/ip_set_bitmap_ip.c125
-rw-r--r--net/netfilter/ipset/ip_set_bitmap_ipmac.c156
-rw-r--r--net/netfilter/ipset/ip_set_bitmap_port.c114
-rw-r--r--net/netfilter/ipset/ip_set_core.c409
-rw-r--r--net/netfilter/ipset/ip_set_getport.c18
-rw-r--r--net/netfilter/ipset/ip_set_hash_gen.h545
-rw-r--r--net/netfilter/ipset/ip_set_hash_ip.c58
-rw-r--r--net/netfilter/ipset/ip_set_hash_ipport.c80
-rw-r--r--net/netfilter/ipset/ip_set_hash_ipportip.c86
-rw-r--r--net/netfilter/ipset/ip_set_hash_ipportnet.c108
-rw-r--r--net/netfilter/ipset/ip_set_hash_net.c85
-rw-r--r--net/netfilter/ipset/ip_set_hash_netiface.c98
-rw-r--r--net/netfilter/ipset/ip_set_hash_netnet.c481
-rw-r--r--net/netfilter/ipset/ip_set_hash_netport.c92
-rw-r--r--net/netfilter/ipset/ip_set_hash_netportnet.c586
-rw-r--r--net/netfilter/ipset/ip_set_list_set.c274
-rw-r--r--net/netfilter/ipvs/ip_vs_core.c95
-rw-r--r--net/netfilter/ipvs/ip_vs_ctl.c35
-rw-r--r--net/netfilter/ipvs/ip_vs_lblc.c2
-rw-r--r--net/netfilter/ipvs/ip_vs_lblcr.c2
-rw-r--r--net/netfilter/ipvs/ip_vs_nfct.c6
-rw-r--r--net/netfilter/ipvs/ip_vs_pe_sip.c8
-rw-r--r--net/netfilter/ipvs/ip_vs_proto_sctp.c48
-rw-r--r--net/netfilter/ipvs/ip_vs_sh.c39
-rw-r--r--net/netfilter/ipvs/ip_vs_sync.c7
-rw-r--r--net/netfilter/nf_conntrack_acct.c12
-rw-r--r--net/netfilter/nf_conntrack_core.c19
-rw-r--r--net/netfilter/nf_conntrack_netlink.c51
-rw-r--r--net/netfilter/nf_conntrack_seqadj.c9
-rw-r--r--net/netfilter/nf_conntrack_sip.c133
-rw-r--r--net/netfilter/nf_conntrack_timestamp.c1
-rw-r--r--net/netfilter/nf_internals.h28
-rw-r--r--net/netfilter/nf_nat_core.c20
-rw-r--r--net/netfilter/nf_nat_irc.c32
-rw-r--r--net/netfilter/nf_nat_sip.c35
-rw-r--r--net/netfilter/nf_synproxy_core.c7
-rw-r--r--net/netfilter/nf_tables_api.c3309
-rw-r--r--net/netfilter/nf_tables_core.c270
-rw-r--r--net/netfilter/nfnetlink.c177
-rw-r--r--net/netfilter/nfnetlink_cttimeout.c161
-rw-r--r--net/netfilter/nfnetlink_log.c12
-rw-r--r--net/netfilter/nfnetlink_queue_core.c6
-rw-r--r--net/netfilter/nft_bitwise.c146
-rw-r--r--net/netfilter/nft_byteorder.c173
-rw-r--r--net/netfilter/nft_cmp.c223
-rw-r--r--net/netfilter/nft_compat.c775
-rw-r--r--net/netfilter/nft_counter.c113
-rw-r--r--net/netfilter/nft_ct.c258
-rw-r--r--net/netfilter/nft_expr_template.c94
-rw-r--r--net/netfilter/nft_exthdr.c133
-rw-r--r--net/netfilter/nft_hash.c231
-rw-r--r--net/netfilter/nft_immediate.c132
-rw-r--r--net/netfilter/nft_limit.c119
-rw-r--r--net/netfilter/nft_log.c146
-rw-r--r--net/netfilter/nft_lookup.c141
-rw-r--r--net/netfilter/nft_meta.c228
-rw-r--r--net/netfilter/nft_meta_target.c117
-rw-r--r--net/netfilter/nft_nat.c224
-rw-r--r--net/netfilter/nft_payload.c160
-rw-r--r--net/netfilter/nft_rbtree.c247
-rw-r--r--net/netfilter/x_tables.c7
-rw-r--r--net/netfilter/xt_NFQUEUE.c7
-rw-r--r--net/netfilter/xt_TCPMSS.c72
-rw-r--r--net/netfilter/xt_TPROXY.c2
-rw-r--r--net/netfilter/xt_connbytes.c6
-rw-r--r--net/netfilter/xt_hashlimit.c25
-rw-r--r--net/netfilter/xt_set.c224
-rw-r--r--net/netfilter/xt_socket.c15
-rw-r--r--net/netlabel/netlabel_cipso_v4.c4
-rw-r--r--net/netlabel/netlabel_kapi.c2
-rw-r--r--net/netlabel/netlabel_mgmt.c4
-rw-r--r--net/netlabel/netlabel_unlabeled.c4
-rw-r--r--net/netlink/af_netlink.c4
-rw-r--r--net/netlink/genetlink.c531
-rw-r--r--net/netrom/af_netrom.c3
-rw-r--r--net/nfc/Kconfig14
-rw-r--r--net/nfc/Makefile2
-rw-r--r--net/nfc/core.c22
-rw-r--r--net/nfc/digital.h170
-rw-r--r--net/nfc/digital_core.c737
-rw-r--r--net/nfc/digital_dep.c729
-rw-r--r--net/nfc/digital_technology.c770
-rw-r--r--net/nfc/llcp_sock.c2
-rw-r--r--net/nfc/nci/spi.c239
-rw-r--r--net/nfc/netlink.c130
-rw-r--r--net/nfc/rawsock.c9
-rw-r--r--net/openvswitch/Makefile2
-rw-r--r--net/openvswitch/datapath.c733
-rw-r--r--net/openvswitch/datapath.h10
-rw-r--r--net/openvswitch/dp_notify.c18
-rw-r--r--net/openvswitch/flow.c1605
-rw-r--r--net/openvswitch/flow.h132
-rw-r--r--net/openvswitch/flow_netlink.c1630
-rw-r--r--net/openvswitch/flow_netlink.h60
-rw-r--r--net/openvswitch/flow_table.c592
-rw-r--r--net/openvswitch/flow_table.h81
-rw-r--r--net/openvswitch/vport-gre.c2
-rw-r--r--net/openvswitch/vport-internal_dev.c2
-rw-r--r--net/openvswitch/vport-netdev.c16
-rw-r--r--net/openvswitch/vport-netdev.h1
-rw-r--r--net/openvswitch/vport-vxlan.c3
-rw-r--r--net/openvswitch/vport.c8
-rw-r--r--net/packet/af_packet.c108
-rw-r--r--net/packet/internal.h1
-rw-r--r--net/phonet/datagram.c9
-rw-r--r--net/phonet/socket.c24
-rw-r--r--net/rds/connection.c12
-rw-r--r--net/rds/ib.c3
-rw-r--r--net/rds/ib_recv.c7
-rw-r--r--net/rds/ib_send.c5
-rw-r--r--net/rds/rds.h2
-rw-r--r--net/rds/recv.c2
-rw-r--r--net/rfkill/Kconfig2
-rw-r--r--net/rfkill/rfkill-gpio.c211
-rw-r--r--net/rose/af_rose.c24
-rw-r--r--net/rxrpc/ar-internal.h150
-rw-r--r--net/rxrpc/ar-recvmsg.c9
-rw-r--r--net/sched/Kconfig10
-rw-r--r--net/sched/Makefile1
-rw-r--r--net/sched/act_api.c26
-rw-r--r--net/sched/act_csum.c12
-rw-r--r--net/sched/act_gact.c9
-rw-r--r--net/sched/act_ipt.c12
-rw-r--r--net/sched/act_mirred.c2
-rw-r--r--net/sched/act_nat.c12
-rw-r--r--net/sched/act_pedit.c10
-rw-r--r--net/sched/act_police.c9
-rw-r--r--net/sched/act_simple.c10
-rw-r--r--net/sched/act_skbedit.c8
-rw-r--r--net/sched/cls_basic.c2
-rw-r--r--net/sched/cls_bpf.c385
-rw-r--r--net/sched/cls_cgroup.c4
-rw-r--r--net/sched/em_ipset.c7
-rw-r--r--net/sched/em_meta.c4
-rw-r--r--net/sched/sch_api.c3
-rw-r--r--net/sched/sch_fq.c41
-rw-r--r--net/sched/sch_generic.c11
-rw-r--r--net/sched/sch_htb.c25
-rw-r--r--net/sched/sch_netem.c8
-rw-r--r--net/sched/sch_tbf.c149
-rw-r--r--net/sctp/associola.c15
-rw-r--r--net/sctp/auth.c14
-rw-r--r--net/sctp/chunk.c2
-rw-r--r--net/sctp/ipv6.c26
-rw-r--r--net/sctp/objcnt.c9
-rw-r--r--net/sctp/output.c15
-rw-r--r--net/sctp/outqueue.c38
-rw-r--r--net/sctp/probe.c17
-rw-r--r--net/sctp/sm_make_chunk.c29
-rw-r--r--net/sctp/sm_sideeffect.c1
-rw-r--r--net/sctp/sm_statefuns.c12
-rw-r--r--net/sctp/socket.c38
-rw-r--r--net/sctp/sysctl.c76
-rw-r--r--net/sctp/transport.c2
-rw-r--r--net/socket.c24
-rw-r--r--net/sunrpc/auth_gss/auth_gss.c59
-rw-r--r--net/sunrpc/auth_gss/gss_krb5_unseal.c8
-rw-r--r--net/sunrpc/auth_gss/gss_krb5_wrap.c10
-rw-r--r--net/sunrpc/auth_gss/gss_rpc_upcall.c3
-rw-r--r--net/sunrpc/auth_gss/gss_rpc_xdr.c29
-rw-r--r--net/sunrpc/auth_gss/svcauth_gss.c4
-rw-r--r--net/sunrpc/clnt.c177
-rw-r--r--net/sunrpc/rpc_pipe.c23
-rw-r--r--net/sunrpc/svc.c2
-rw-r--r--net/sunrpc/svcsock.c4
-rw-r--r--net/sunrpc/xprt.c63
-rw-r--r--net/sunrpc/xprtsock.c75
-rw-r--r--net/tipc/bcast.c16
-rw-r--r--net/tipc/bearer.c18
-rw-r--r--net/tipc/bearer.h10
-rw-r--r--net/tipc/core.c7
-rw-r--r--net/tipc/core.h28
-rw-r--r--net/tipc/eth_media.c68
-rw-r--r--net/tipc/handler.c11
-rw-r--r--net/tipc/ib_media.c58
-rw-r--r--net/tipc/link.c494
-rw-r--r--net/tipc/link.h24
-rw-r--r--net/tipc/msg.c27
-rw-r--r--net/tipc/msg.h15
-rw-r--r--net/tipc/netlink.c11
-rw-r--r--net/tipc/node.c7
-rw-r--r--net/tipc/node.h6
-rw-r--r--net/tipc/port.c111
-rw-r--r--net/tipc/port.h22
-rw-r--r--net/tipc/socket.c64
-rw-r--r--net/unix/af_unix.c21
-rw-r--r--net/vmw_vsock/Kconfig2
-rw-r--r--net/vmw_vsock/af_vsock.c2
-rw-r--r--net/vmw_vsock/vmci_transport.c2
-rw-r--r--net/wimax/op-msg.c27
-rw-r--r--net/wimax/op-reset.c17
-rw-r--r--net/wimax/op-rfkill.c21
-rw-r--r--net/wimax/op-state-get.c17
-rw-r--r--net/wimax/stack.c96
-rw-r--r--net/wimax/wimax-internal.h26
-rw-r--r--net/wireless/chan.c4
-rw-r--r--net/wireless/core.c9
-rw-r--r--net/wireless/core.h15
-rw-r--r--net/wireless/debugfs.c24
-rw-r--r--net/wireless/genregdb.awk6
-rw-r--r--net/wireless/ibss.c42
-rw-r--r--net/wireless/mlme.c6
-rw-r--r--net/wireless/nl80211.c368
-rw-r--r--net/wireless/radiotap.c4
-rw-r--r--net/wireless/reg.c53
-rw-r--r--net/wireless/reg.h4
-rw-r--r--net/wireless/scan.c4
-rw-r--r--net/wireless/sme.c26
-rw-r--r--net/wireless/sysfs.h4
-rw-r--r--net/wireless/util.c23
-rw-r--r--net/x25/Kconfig4
-rw-r--r--net/x25/af_x25.c3
-rw-r--r--net/xfrm/xfrm_algo.c13
-rw-r--r--net/xfrm/xfrm_hash.h4
-rw-r--r--net/xfrm/xfrm_ipcomp.c18
-rw-r--r--net/xfrm/xfrm_policy.c7
-rw-r--r--net/xfrm/xfrm_state.c6
553 files changed, 38076 insertions, 15913 deletions
diff --git a/net/8021q/vlan.c b/net/8021q/vlan.c
index 61fc573f1142..b3d17d1c49c3 100644
--- a/net/8021q/vlan.c
+++ b/net/8021q/vlan.c
@@ -98,14 +98,14 @@ void unregister_vlan_dev(struct net_device *dev, struct list_head *head)
98 vlan_gvrp_request_leave(dev); 98 vlan_gvrp_request_leave(dev);
99 99
100 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL); 100 vlan_group_set_device(grp, vlan->vlan_proto, vlan_id, NULL);
101
102 netdev_upper_dev_unlink(real_dev, dev);
101 /* Because unregister_netdevice_queue() makes sure at least one rcu 103 /* Because unregister_netdevice_queue() makes sure at least one rcu
102 * grace period is respected before device freeing, 104 * grace period is respected before device freeing,
103 * we dont need to call synchronize_net() here. 105 * we dont need to call synchronize_net() here.
104 */ 106 */
105 unregister_netdevice_queue(dev, head); 107 unregister_netdevice_queue(dev, head);
106 108
107 netdev_upper_dev_unlink(real_dev, dev);
108
109 if (grp->nr_vlan_devs == 0) { 109 if (grp->nr_vlan_devs == 0) {
110 vlan_mvrp_uninit_applicant(real_dev); 110 vlan_mvrp_uninit_applicant(real_dev);
111 vlan_gvrp_uninit_applicant(real_dev); 111 vlan_gvrp_uninit_applicant(real_dev);
@@ -169,13 +169,13 @@ int register_vlan_dev(struct net_device *dev)
169 if (err < 0) 169 if (err < 0)
170 goto out_uninit_mvrp; 170 goto out_uninit_mvrp;
171 171
172 err = netdev_upper_dev_link(real_dev, dev);
173 if (err)
174 goto out_uninit_mvrp;
175
176 err = register_netdevice(dev); 172 err = register_netdevice(dev);
177 if (err < 0) 173 if (err < 0)
178 goto out_upper_dev_unlink; 174 goto out_uninit_mvrp;
175
176 err = netdev_upper_dev_link(real_dev, dev);
177 if (err)
178 goto out_unregister_netdev;
179 179
180 /* Account for reference in struct vlan_dev_priv */ 180 /* Account for reference in struct vlan_dev_priv */
181 dev_hold(real_dev); 181 dev_hold(real_dev);
@@ -191,8 +191,8 @@ int register_vlan_dev(struct net_device *dev)
191 191
192 return 0; 192 return 0;
193 193
194out_upper_dev_unlink: 194out_unregister_netdev:
195 netdev_upper_dev_unlink(real_dev, dev); 195 unregister_netdevice(dev);
196out_uninit_mvrp: 196out_uninit_mvrp:
197 if (grp->nr_vlan_devs == 0) 197 if (grp->nr_vlan_devs == 0)
198 vlan_mvrp_uninit_applicant(real_dev); 198 vlan_mvrp_uninit_applicant(real_dev);
diff --git a/net/8021q/vlan.h b/net/8021q/vlan.h
index ba5983f34c42..5704ed9c3a23 100644
--- a/net/8021q/vlan.h
+++ b/net/8021q/vlan.h
@@ -5,83 +5,6 @@
5#include <linux/u64_stats_sync.h> 5#include <linux/u64_stats_sync.h>
6#include <linux/list.h> 6#include <linux/list.h>
7 7
8
9/**
10 * struct vlan_priority_tci_mapping - vlan egress priority mappings
11 * @priority: skb priority
12 * @vlan_qos: vlan priority: (skb->priority << 13) & 0xE000
13 * @next: pointer to next struct
14 */
15struct vlan_priority_tci_mapping {
16 u32 priority;
17 u16 vlan_qos;
18 struct vlan_priority_tci_mapping *next;
19};
20
21
22/**
23 * struct vlan_pcpu_stats - VLAN percpu rx/tx stats
24 * @rx_packets: number of received packets
25 * @rx_bytes: number of received bytes
26 * @rx_multicast: number of received multicast packets
27 * @tx_packets: number of transmitted packets
28 * @tx_bytes: number of transmitted bytes
29 * @syncp: synchronization point for 64bit counters
30 * @rx_errors: number of rx errors
31 * @tx_dropped: number of tx drops
32 */
33struct vlan_pcpu_stats {
34 u64 rx_packets;
35 u64 rx_bytes;
36 u64 rx_multicast;
37 u64 tx_packets;
38 u64 tx_bytes;
39 struct u64_stats_sync syncp;
40 u32 rx_errors;
41 u32 tx_dropped;
42};
43
44struct netpoll;
45
46/**
47 * struct vlan_dev_priv - VLAN private device data
48 * @nr_ingress_mappings: number of ingress priority mappings
49 * @ingress_priority_map: ingress priority mappings
50 * @nr_egress_mappings: number of egress priority mappings
51 * @egress_priority_map: hash of egress priority mappings
52 * @vlan_proto: VLAN encapsulation protocol
53 * @vlan_id: VLAN identifier
54 * @flags: device flags
55 * @real_dev: underlying netdevice
56 * @real_dev_addr: address of underlying netdevice
57 * @dent: proc dir entry
58 * @vlan_pcpu_stats: ptr to percpu rx stats
59 */
60struct vlan_dev_priv {
61 unsigned int nr_ingress_mappings;
62 u32 ingress_priority_map[8];
63 unsigned int nr_egress_mappings;
64 struct vlan_priority_tci_mapping *egress_priority_map[16];
65
66 __be16 vlan_proto;
67 u16 vlan_id;
68 u16 flags;
69
70 struct net_device *real_dev;
71 unsigned char real_dev_addr[ETH_ALEN];
72
73 struct proc_dir_entry *dent;
74 struct vlan_pcpu_stats __percpu *vlan_pcpu_stats;
75#ifdef CONFIG_NET_POLL_CONTROLLER
76 struct netpoll *netpoll;
77#endif
78};
79
80static inline struct vlan_dev_priv *vlan_dev_priv(const struct net_device *dev)
81{
82 return netdev_priv(dev);
83}
84
85/* if this changes, algorithm will have to be reworked because this 8/* if this changes, algorithm will have to be reworked because this
86 * depends on completely exhausting the VLAN identifier space. Thus 9 * depends on completely exhausting the VLAN identifier space. Thus
87 * it gives constant time look-up, but in many cases it wastes memory. 10 * it gives constant time look-up, but in many cases it wastes memory.
@@ -196,12 +119,12 @@ static inline u32 vlan_get_ingress_priority(struct net_device *dev,
196} 119}
197 120
198#ifdef CONFIG_VLAN_8021Q_GVRP 121#ifdef CONFIG_VLAN_8021Q_GVRP
199extern int vlan_gvrp_request_join(const struct net_device *dev); 122int vlan_gvrp_request_join(const struct net_device *dev);
200extern void vlan_gvrp_request_leave(const struct net_device *dev); 123void vlan_gvrp_request_leave(const struct net_device *dev);
201extern int vlan_gvrp_init_applicant(struct net_device *dev); 124int vlan_gvrp_init_applicant(struct net_device *dev);
202extern void vlan_gvrp_uninit_applicant(struct net_device *dev); 125void vlan_gvrp_uninit_applicant(struct net_device *dev);
203extern int vlan_gvrp_init(void); 126int vlan_gvrp_init(void);
204extern void vlan_gvrp_uninit(void); 127void vlan_gvrp_uninit(void);
205#else 128#else
206static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; } 129static inline int vlan_gvrp_request_join(const struct net_device *dev) { return 0; }
207static inline void vlan_gvrp_request_leave(const struct net_device *dev) {} 130static inline void vlan_gvrp_request_leave(const struct net_device *dev) {}
@@ -212,12 +135,12 @@ static inline void vlan_gvrp_uninit(void) {}
212#endif 135#endif
213 136
214#ifdef CONFIG_VLAN_8021Q_MVRP 137#ifdef CONFIG_VLAN_8021Q_MVRP
215extern int vlan_mvrp_request_join(const struct net_device *dev); 138int vlan_mvrp_request_join(const struct net_device *dev);
216extern void vlan_mvrp_request_leave(const struct net_device *dev); 139void vlan_mvrp_request_leave(const struct net_device *dev);
217extern int vlan_mvrp_init_applicant(struct net_device *dev); 140int vlan_mvrp_init_applicant(struct net_device *dev);
218extern void vlan_mvrp_uninit_applicant(struct net_device *dev); 141void vlan_mvrp_uninit_applicant(struct net_device *dev);
219extern int vlan_mvrp_init(void); 142int vlan_mvrp_init(void);
220extern void vlan_mvrp_uninit(void); 143void vlan_mvrp_uninit(void);
221#else 144#else
222static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; } 145static inline int vlan_mvrp_request_join(const struct net_device *dev) { return 0; }
223static inline void vlan_mvrp_request_leave(const struct net_device *dev) {} 146static inline void vlan_mvrp_request_leave(const struct net_device *dev) {}
@@ -229,8 +152,8 @@ static inline void vlan_mvrp_uninit(void) {}
229 152
230extern const char vlan_fullname[]; 153extern const char vlan_fullname[];
231extern const char vlan_version[]; 154extern const char vlan_version[];
232extern int vlan_netlink_init(void); 155int vlan_netlink_init(void);
233extern void vlan_netlink_fini(void); 156void vlan_netlink_fini(void);
234 157
235extern struct rtnl_link_ops vlan_link_ops; 158extern struct rtnl_link_ops vlan_link_ops;
236 159
diff --git a/net/8021q/vlan_dev.c b/net/8021q/vlan_dev.c
index 09bf1c38805b..47c908f1f626 100644
--- a/net/8021q/vlan_dev.c
+++ b/net/8021q/vlan_dev.c
@@ -68,25 +68,6 @@ static int vlan_dev_rebuild_header(struct sk_buff *skb)
68 return 0; 68 return 0;
69} 69}
70 70
71static inline u16
72vlan_dev_get_egress_qos_mask(struct net_device *dev, struct sk_buff *skb)
73{
74 struct vlan_priority_tci_mapping *mp;
75
76 smp_rmb(); /* coupled with smp_wmb() in vlan_dev_set_egress_priority() */
77
78 mp = vlan_dev_priv(dev)->egress_priority_map[(skb->priority & 0xF)];
79 while (mp) {
80 if (mp->priority == skb->priority) {
81 return mp->vlan_qos; /* This should already be shifted
82 * to mask correctly with the
83 * VLAN's TCI */
84 }
85 mp = mp->next;
86 }
87 return 0;
88}
89
90/* 71/*
91 * Create the VLAN header for an arbitrary protocol layer 72 * Create the VLAN header for an arbitrary protocol layer
92 * 73 *
@@ -111,7 +92,7 @@ static int vlan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
111 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN); 92 vhdr = (struct vlan_hdr *) skb_push(skb, VLAN_HLEN);
112 93
113 vlan_tci = vlan->vlan_id; 94 vlan_tci = vlan->vlan_id;
114 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb); 95 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
115 vhdr->h_vlan_TCI = htons(vlan_tci); 96 vhdr->h_vlan_TCI = htons(vlan_tci);
116 97
117 /* 98 /*
@@ -168,7 +149,7 @@ static netdev_tx_t vlan_dev_hard_start_xmit(struct sk_buff *skb,
168 vlan->flags & VLAN_FLAG_REORDER_HDR) { 149 vlan->flags & VLAN_FLAG_REORDER_HDR) {
169 u16 vlan_tci; 150 u16 vlan_tci;
170 vlan_tci = vlan->vlan_id; 151 vlan_tci = vlan->vlan_id;
171 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb); 152 vlan_tci |= vlan_dev_get_egress_qos_mask(dev, skb->priority);
172 skb = __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci); 153 skb = __vlan_hwaccel_put_tag(skb, vlan->vlan_proto, vlan_tci);
173 } 154 }
174 155
@@ -549,6 +530,23 @@ static const struct header_ops vlan_header_ops = {
549 .parse = eth_header_parse, 530 .parse = eth_header_parse,
550}; 531};
551 532
533static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev,
534 unsigned short type,
535 const void *daddr, const void *saddr,
536 unsigned int len)
537{
538 struct vlan_dev_priv *vlan = vlan_dev_priv(dev);
539 struct net_device *real_dev = vlan->real_dev;
540
541 return dev_hard_header(skb, real_dev, type, daddr, saddr, len);
542}
543
544static const struct header_ops vlan_passthru_header_ops = {
545 .create = vlan_passthru_hard_header,
546 .rebuild = dev_rebuild_header,
547 .parse = eth_header_parse,
548};
549
552static struct device_type vlan_type = { 550static struct device_type vlan_type = {
553 .name = "vlan", 551 .name = "vlan",
554}; 552};
@@ -558,7 +556,7 @@ static const struct net_device_ops vlan_netdev_ops;
558static int vlan_dev_init(struct net_device *dev) 556static int vlan_dev_init(struct net_device *dev)
559{ 557{
560 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev; 558 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
561 int subclass = 0; 559 int subclass = 0, i;
562 560
563 netif_carrier_off(dev); 561 netif_carrier_off(dev);
564 562
@@ -592,7 +590,7 @@ static int vlan_dev_init(struct net_device *dev)
592 590
593 dev->needed_headroom = real_dev->needed_headroom; 591 dev->needed_headroom = real_dev->needed_headroom;
594 if (real_dev->features & NETIF_F_HW_VLAN_CTAG_TX) { 592 if (real_dev->features & NETIF_F_HW_VLAN_CTAG_TX) {
595 dev->header_ops = real_dev->header_ops; 593 dev->header_ops = &vlan_passthru_header_ops;
596 dev->hard_header_len = real_dev->hard_header_len; 594 dev->hard_header_len = real_dev->hard_header_len;
597 } else { 595 } else {
598 dev->header_ops = &vlan_header_ops; 596 dev->header_ops = &vlan_header_ops;
@@ -612,6 +610,13 @@ static int vlan_dev_init(struct net_device *dev)
612 if (!vlan_dev_priv(dev)->vlan_pcpu_stats) 610 if (!vlan_dev_priv(dev)->vlan_pcpu_stats)
613 return -ENOMEM; 611 return -ENOMEM;
614 612
613 for_each_possible_cpu(i) {
614 struct vlan_pcpu_stats *vlan_stat;
615 vlan_stat = per_cpu_ptr(vlan_dev_priv(dev)->vlan_pcpu_stats, i);
616 u64_stats_init(&vlan_stat->syncp);
617 }
618
619
615 return 0; 620 return 0;
616} 621}
617 622
diff --git a/net/9p/trans_fd.c b/net/9p/trans_fd.c
index 3ffda1b3799b..9321a7763067 100644
--- a/net/9p/trans_fd.c
+++ b/net/9p/trans_fd.c
@@ -244,10 +244,10 @@ p9_fd_poll(struct p9_client *client, struct poll_table_struct *pt)
244 if (!ts) 244 if (!ts)
245 return -EREMOTEIO; 245 return -EREMOTEIO;
246 246
247 if (!ts->rd->f_op || !ts->rd->f_op->poll) 247 if (!ts->rd->f_op->poll)
248 return -EIO; 248 return -EIO;
249 249
250 if (!ts->wr->f_op || !ts->wr->f_op->poll) 250 if (!ts->wr->f_op->poll)
251 return -EIO; 251 return -EIO;
252 252
253 ret = ts->rd->f_op->poll(ts->rd, pt); 253 ret = ts->rd->f_op->poll(ts->rd, pt);
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 990afab2be1b..9c5a1aa34d12 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -544,9 +544,7 @@ static int p9_virtio_probe(struct virtio_device *vdev)
544 544
545 chan->inuse = false; 545 chan->inuse = false;
546 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) { 546 if (virtio_has_feature(vdev, VIRTIO_9P_MOUNT_TAG)) {
547 vdev->config->get(vdev, 547 virtio_cread(vdev, struct virtio_9p_config, tag_len, &tag_len);
548 offsetof(struct virtio_9p_config, tag_len),
549 &tag_len, sizeof(tag_len));
550 } else { 548 } else {
551 err = -EINVAL; 549 err = -EINVAL;
552 goto out_free_vq; 550 goto out_free_vq;
@@ -556,8 +554,9 @@ static int p9_virtio_probe(struct virtio_device *vdev)
556 err = -ENOMEM; 554 err = -ENOMEM;
557 goto out_free_vq; 555 goto out_free_vq;
558 } 556 }
559 vdev->config->get(vdev, offsetof(struct virtio_9p_config, tag), 557
560 tag, tag_len); 558 virtio_cread_bytes(vdev, offsetof(struct virtio_9p_config, tag),
559 tag, tag_len);
561 chan->tag = tag; 560 chan->tag = tag;
562 chan->tag_len = tag_len; 561 chan->tag_len = tag_len;
563 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr); 562 err = sysfs_create_file(&(vdev->dev.kobj), &dev_attr_mount_tag.attr);
diff --git a/net/Kconfig b/net/Kconfig
index b50dacc072f0..d334678c0bd8 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -220,10 +220,11 @@ source "net/openvswitch/Kconfig"
220source "net/vmw_vsock/Kconfig" 220source "net/vmw_vsock/Kconfig"
221source "net/netlink/Kconfig" 221source "net/netlink/Kconfig"
222source "net/mpls/Kconfig" 222source "net/mpls/Kconfig"
223source "net/hsr/Kconfig"
223 224
224config RPS 225config RPS
225 boolean 226 boolean
226 depends on SMP && SYSFS && USE_GENERIC_SMP_HELPERS 227 depends on SMP && SYSFS
227 default y 228 default y
228 229
229config RFS_ACCEL 230config RFS_ACCEL
@@ -234,7 +235,7 @@ config RFS_ACCEL
234 235
235config XPS 236config XPS
236 boolean 237 boolean
237 depends on SMP && USE_GENERIC_SMP_HELPERS 238 depends on SMP
238 default y 239 default y
239 240
240config NETPRIO_CGROUP 241config NETPRIO_CGROUP
diff --git a/net/Makefile b/net/Makefile
index 9492e8cb64e9..8fa2f91517f1 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -71,3 +71,4 @@ obj-$(CONFIG_NFC) += nfc/
71obj-$(CONFIG_OPENVSWITCH) += openvswitch/ 71obj-$(CONFIG_OPENVSWITCH) += openvswitch/
72obj-$(CONFIG_VSOCKETS) += vmw_vsock/ 72obj-$(CONFIG_VSOCKETS) += vmw_vsock/
73obj-$(CONFIG_NET_MPLS_GSO) += mpls/ 73obj-$(CONFIG_NET_MPLS_GSO) += mpls/
74obj-$(CONFIG_HSR) += hsr/
diff --git a/net/appletalk/ddp.c b/net/appletalk/ddp.c
index 7fee50d637f9..7d424ac6e760 100644
--- a/net/appletalk/ddp.c
+++ b/net/appletalk/ddp.c
@@ -1735,7 +1735,6 @@ static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr
1735 size_t size, int flags) 1735 size_t size, int flags)
1736{ 1736{
1737 struct sock *sk = sock->sk; 1737 struct sock *sk = sock->sk;
1738 struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
1739 struct ddpehdr *ddp; 1738 struct ddpehdr *ddp;
1740 int copied = 0; 1739 int copied = 0;
1741 int offset = 0; 1740 int offset = 0;
@@ -1764,14 +1763,13 @@ static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr
1764 } 1763 }
1765 err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied); 1764 err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
1766 1765
1767 if (!err) { 1766 if (!err && msg->msg_name) {
1768 if (sat) { 1767 struct sockaddr_at *sat = msg->msg_name;
1769 sat->sat_family = AF_APPLETALK; 1768 sat->sat_family = AF_APPLETALK;
1770 sat->sat_port = ddp->deh_sport; 1769 sat->sat_port = ddp->deh_sport;
1771 sat->sat_addr.s_node = ddp->deh_snode; 1770 sat->sat_addr.s_node = ddp->deh_snode;
1772 sat->sat_addr.s_net = ddp->deh_snet; 1771 sat->sat_addr.s_net = ddp->deh_snet;
1773 } 1772 msg->msg_namelen = sizeof(*sat);
1774 msg->msg_namelen = sizeof(*sat);
1775 } 1773 }
1776 1774
1777 skb_free_datagram(sk, skb); /* Free the datagram. */ 1775 skb_free_datagram(sk, skb); /* Free the datagram. */
diff --git a/net/atm/common.c b/net/atm/common.c
index 737bef59ce89..7b491006eaf4 100644
--- a/net/atm/common.c
+++ b/net/atm/common.c
@@ -531,8 +531,6 @@ int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
531 struct sk_buff *skb; 531 struct sk_buff *skb;
532 int copied, error = -EINVAL; 532 int copied, error = -EINVAL;
533 533
534 msg->msg_namelen = 0;
535
536 if (sock->state != SS_CONNECTED) 534 if (sock->state != SS_CONNECTED)
537 return -ENOTCONN; 535 return -ENOTCONN;
538 536
diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
index 4b4d2b779ec1..7bb1605bdfd9 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -1636,11 +1636,11 @@ static int ax25_recvmsg(struct kiocb *iocb, struct socket *sock,
1636 1636
1637 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 1637 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1638 1638
1639 if (msg->msg_namelen != 0) { 1639 if (msg->msg_name) {
1640 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1641 ax25_digi digi; 1640 ax25_digi digi;
1642 ax25_address src; 1641 ax25_address src;
1643 const unsigned char *mac = skb_mac_header(skb); 1642 const unsigned char *mac = skb_mac_header(skb);
1643 struct sockaddr_ax25 *sax = msg->msg_name;
1644 1644
1645 memset(sax, 0, sizeof(struct full_sockaddr_ax25)); 1645 memset(sax, 0, sizeof(struct full_sockaddr_ax25));
1646 ax25_addr_parse(mac + 1, skb->data - mac - 1, &src, NULL, 1646 ax25_addr_parse(mac + 1, skb->data - mac - 1, &src, NULL,
@@ -1735,7 +1735,7 @@ static int ax25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1735 res = -EFAULT; 1735 res = -EFAULT;
1736 break; 1736 break;
1737 } 1737 }
1738 if (amount > AX25_NOUID_BLOCK) { 1738 if (amount < 0 || amount > AX25_NOUID_BLOCK) {
1739 res = -EINVAL; 1739 res = -EINVAL;
1740 break; 1740 break;
1741 } 1741 }
diff --git a/net/batman-adv/Makefile b/net/batman-adv/Makefile
index 489bb36f1b94..4f4aabbd8eab 100644
--- a/net/batman-adv/Makefile
+++ b/net/batman-adv/Makefile
@@ -24,6 +24,7 @@ batman-adv-y += bitarray.o
24batman-adv-$(CONFIG_BATMAN_ADV_BLA) += bridge_loop_avoidance.o 24batman-adv-$(CONFIG_BATMAN_ADV_BLA) += bridge_loop_avoidance.o
25batman-adv-y += debugfs.o 25batman-adv-y += debugfs.o
26batman-adv-$(CONFIG_BATMAN_ADV_DAT) += distributed-arp-table.o 26batman-adv-$(CONFIG_BATMAN_ADV_DAT) += distributed-arp-table.o
27batman-adv-y += fragmentation.o
27batman-adv-y += gateway_client.o 28batman-adv-y += gateway_client.o
28batman-adv-y += gateway_common.o 29batman-adv-y += gateway_common.o
29batman-adv-y += hard-interface.o 30batman-adv-y += hard-interface.o
@@ -37,5 +38,3 @@ batman-adv-y += send.o
37batman-adv-y += soft-interface.o 38batman-adv-y += soft-interface.o
38batman-adv-y += sysfs.o 39batman-adv-y += sysfs.o
39batman-adv-y += translation-table.o 40batman-adv-y += translation-table.o
40batman-adv-y += unicast.o
41batman-adv-y += vis.o
diff --git a/net/batman-adv/bat_iv_ogm.c b/net/batman-adv/bat_iv_ogm.c
index 0a8a80cd4bf1..b9c8a6eedf45 100644
--- a/net/batman-adv/bat_iv_ogm.c
+++ b/net/batman-adv/bat_iv_ogm.c
@@ -87,22 +87,198 @@ static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
87 return (uint8_t)(sum / count); 87 return (uint8_t)(sum / count);
88} 88}
89 89
90/**
91 * batadv_iv_ogm_orig_free - free the private resources allocated for this
92 * orig_node
93 * @orig_node: the orig_node for which the resources have to be free'd
94 */
95static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
96{
97 kfree(orig_node->bat_iv.bcast_own);
98 kfree(orig_node->bat_iv.bcast_own_sum);
99}
100
101/**
102 * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
103 * include the new hard-interface
104 * @orig_node: the orig_node that has to be changed
105 * @max_if_num: the current amount of interfaces
106 *
107 * Returns 0 on success, a negative error code otherwise.
108 */
109static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
110 int max_if_num)
111{
112 void *data_ptr;
113 size_t data_size, old_size;
114 int ret = -ENOMEM;
115
116 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
117
118 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
119 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
120 data_ptr = kmalloc(data_size, GFP_ATOMIC);
121 if (!data_ptr)
122 goto unlock;
123
124 memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
125 kfree(orig_node->bat_iv.bcast_own);
126 orig_node->bat_iv.bcast_own = data_ptr;
127
128 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
129 if (!data_ptr) {
130 kfree(orig_node->bat_iv.bcast_own);
131 goto unlock;
132 }
133
134 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
135 (max_if_num - 1) * sizeof(uint8_t));
136 kfree(orig_node->bat_iv.bcast_own_sum);
137 orig_node->bat_iv.bcast_own_sum = data_ptr;
138
139 ret = 0;
140
141unlock:
142 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
143
144 return ret;
145}
146
147/**
148 * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
149 * exclude the removed interface
150 * @orig_node: the orig_node that has to be changed
151 * @max_if_num: the current amount of interfaces
152 * @del_if_num: the index of the interface being removed
153 *
154 * Returns 0 on success, a negative error code otherwise.
155 */
156static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
157 int max_if_num, int del_if_num)
158{
159 int chunk_size, ret = -ENOMEM, if_offset;
160 void *data_ptr = NULL;
161
162 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
163
164 /* last interface was removed */
165 if (max_if_num == 0)
166 goto free_bcast_own;
167
168 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
169 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
170 if (!data_ptr)
171 goto unlock;
172
173 /* copy first part */
174 memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
175
176 /* copy second part */
177 memcpy((char *)data_ptr + del_if_num * chunk_size,
178 orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
179 (max_if_num - del_if_num) * chunk_size);
180
181free_bcast_own:
182 kfree(orig_node->bat_iv.bcast_own);
183 orig_node->bat_iv.bcast_own = data_ptr;
184
185 if (max_if_num == 0)
186 goto free_own_sum;
187
188 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
189 if (!data_ptr) {
190 kfree(orig_node->bat_iv.bcast_own);
191 goto unlock;
192 }
193
194 memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
195 del_if_num * sizeof(uint8_t));
196
197 if_offset = (del_if_num + 1) * sizeof(uint8_t);
198 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
199 orig_node->bat_iv.bcast_own_sum + if_offset,
200 (max_if_num - del_if_num) * sizeof(uint8_t));
201
202free_own_sum:
203 kfree(orig_node->bat_iv.bcast_own_sum);
204 orig_node->bat_iv.bcast_own_sum = data_ptr;
205
206 ret = 0;
207unlock:
208 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
209
210 return ret;
211}
212
213/**
214 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
215 * @bat_priv: the bat priv with all the soft interface information
216 * @addr: mac address of the originator
217 *
218 * Returns the originator object corresponding to the passed mac address or NULL
219 * on failure.
220 * If the object does not exists it is created an initialised.
221 */
222static struct batadv_orig_node *
223batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
224{
225 struct batadv_orig_node *orig_node;
226 int size, hash_added;
227
228 orig_node = batadv_orig_hash_find(bat_priv, addr);
229 if (orig_node)
230 return orig_node;
231
232 orig_node = batadv_orig_node_new(bat_priv, addr);
233 if (!orig_node)
234 return NULL;
235
236 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
237
238 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
239 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
240 if (!orig_node->bat_iv.bcast_own)
241 goto free_orig_node;
242
243 size = bat_priv->num_ifaces * sizeof(uint8_t);
244 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
245 if (!orig_node->bat_iv.bcast_own_sum)
246 goto free_bcast_own;
247
248 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
249 batadv_choose_orig, orig_node,
250 &orig_node->hash_entry);
251 if (hash_added != 0)
252 goto free_bcast_own;
253
254 return orig_node;
255
256free_bcast_own:
257 kfree(orig_node->bat_iv.bcast_own);
258free_orig_node:
259 batadv_orig_node_free_ref(orig_node);
260
261 return NULL;
262}
263
90static struct batadv_neigh_node * 264static struct batadv_neigh_node *
91batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface, 265batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
92 const uint8_t *neigh_addr, 266 const uint8_t *neigh_addr,
93 struct batadv_orig_node *orig_node, 267 struct batadv_orig_node *orig_node,
94 struct batadv_orig_node *orig_neigh) 268 struct batadv_orig_node *orig_neigh)
95{ 269{
270 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
96 struct batadv_neigh_node *neigh_node; 271 struct batadv_neigh_node *neigh_node;
97 272
98 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr); 273 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
99 if (!neigh_node) 274 if (!neigh_node)
100 goto out; 275 goto out;
101 276
102 INIT_LIST_HEAD(&neigh_node->bonding_list); 277 spin_lock_init(&neigh_node->bat_iv.lq_update_lock);
103 278
104 neigh_node->orig_node = orig_neigh; 279 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
105 neigh_node->if_incoming = hard_iface; 280 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
281 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
106 282
107 spin_lock_bh(&orig_node->neigh_list_lock); 283 spin_lock_bh(&orig_node->neigh_list_lock);
108 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); 284 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
@@ -131,13 +307,12 @@ static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
131 hard_iface->bat_iv.ogm_buff = ogm_buff; 307 hard_iface->bat_iv.ogm_buff = ogm_buff;
132 308
133 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; 309 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
134 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM; 310 batadv_ogm_packet->packet_type = BATADV_IV_OGM;
135 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION; 311 batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
136 batadv_ogm_packet->header.ttl = 2; 312 batadv_ogm_packet->ttl = 2;
137 batadv_ogm_packet->flags = BATADV_NO_FLAGS; 313 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
314 batadv_ogm_packet->reserved = 0;
138 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE; 315 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
139 batadv_ogm_packet->tt_num_changes = 0;
140 batadv_ogm_packet->ttvn = 0;
141 316
142 res = 0; 317 res = 0;
143 318
@@ -171,7 +346,7 @@ batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
171 346
172 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; 347 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
173 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP; 348 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
174 batadv_ogm_packet->header.ttl = BATADV_TTL; 349 batadv_ogm_packet->ttl = BATADV_TTL;
175} 350}
176 351
177/* when do we schedule our own ogm to be sent */ 352/* when do we schedule our own ogm to be sent */
@@ -207,12 +382,12 @@ static uint8_t batadv_hop_penalty(uint8_t tq,
207 382
208/* is there another aggregated packet here? */ 383/* is there another aggregated packet here? */
209static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, 384static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
210 int tt_num_changes) 385 __be16 tvlv_len)
211{ 386{
212 int next_buff_pos = 0; 387 int next_buff_pos = 0;
213 388
214 next_buff_pos += buff_pos + BATADV_OGM_HLEN; 389 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
215 next_buff_pos += batadv_tt_len(tt_num_changes); 390 next_buff_pos += ntohs(tvlv_len);
216 391
217 return (next_buff_pos <= packet_len) && 392 return (next_buff_pos <= packet_len) &&
218 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES); 393 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
@@ -240,7 +415,7 @@ static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
240 415
241 /* adjust all flags and log packets */ 416 /* adjust all flags and log packets */
242 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, 417 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
243 batadv_ogm_packet->tt_num_changes)) { 418 batadv_ogm_packet->tvlv_len)) {
244 /* we might have aggregated direct link packets with an 419 /* we might have aggregated direct link packets with an
245 * ordinary base packet 420 * ordinary base packet
246 */ 421 */
@@ -256,18 +431,18 @@ static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
256 fwd_str = "Sending own"; 431 fwd_str = "Sending own";
257 432
258 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 433 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
259 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n", 434 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
260 fwd_str, (packet_num > 0 ? "aggregated " : ""), 435 fwd_str, (packet_num > 0 ? "aggregated " : ""),
261 batadv_ogm_packet->orig, 436 batadv_ogm_packet->orig,
262 ntohl(batadv_ogm_packet->seqno), 437 ntohl(batadv_ogm_packet->seqno),
263 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl, 438 batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
264 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 439 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
265 "on" : "off"), 440 "on" : "off"),
266 batadv_ogm_packet->ttvn, hard_iface->net_dev->name, 441 hard_iface->net_dev->name,
267 hard_iface->net_dev->dev_addr); 442 hard_iface->net_dev->dev_addr);
268 443
269 buff_pos += BATADV_OGM_HLEN; 444 buff_pos += BATADV_OGM_HLEN;
270 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes); 445 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
271 packet_num++; 446 packet_num++;
272 packet_pos = forw_packet->skb->data + buff_pos; 447 packet_pos = forw_packet->skb->data + buff_pos;
273 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; 448 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
@@ -316,7 +491,7 @@ static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
316 /* multihomed peer assumed 491 /* multihomed peer assumed
317 * non-primary OGMs are only broadcasted on their interface 492 * non-primary OGMs are only broadcasted on their interface
318 */ 493 */
319 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) || 494 if ((directlink && (batadv_ogm_packet->ttl == 1)) ||
320 (forw_packet->own && (forw_packet->if_incoming != primary_if))) { 495 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
321 /* FIXME: what about aggregated packets ? */ 496 /* FIXME: what about aggregated packets ? */
322 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 497 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
@@ -324,7 +499,7 @@ static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
324 (forw_packet->own ? "Sending own" : "Forwarding"), 499 (forw_packet->own ? "Sending own" : "Forwarding"),
325 batadv_ogm_packet->orig, 500 batadv_ogm_packet->orig,
326 ntohl(batadv_ogm_packet->seqno), 501 ntohl(batadv_ogm_packet->seqno),
327 batadv_ogm_packet->header.ttl, 502 batadv_ogm_packet->ttl,
328 forw_packet->if_incoming->net_dev->name, 503 forw_packet->if_incoming->net_dev->name,
329 forw_packet->if_incoming->net_dev->dev_addr); 504 forw_packet->if_incoming->net_dev->dev_addr);
330 505
@@ -397,7 +572,7 @@ batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
397 */ 572 */
398 if ((!directlink) && 573 if ((!directlink) &&
399 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) && 574 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
400 (batadv_ogm_packet->header.ttl != 1) && 575 (batadv_ogm_packet->ttl != 1) &&
401 576
402 /* own packets originating non-primary 577 /* own packets originating non-primary
403 * interfaces leave only that interface 578 * interfaces leave only that interface
@@ -412,7 +587,7 @@ batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
412 * interface only - we still can aggregate 587 * interface only - we still can aggregate
413 */ 588 */
414 if ((directlink) && 589 if ((directlink) &&
415 (new_bat_ogm_packet->header.ttl == 1) && 590 (new_bat_ogm_packet->ttl == 1) &&
416 (forw_packet->if_incoming == if_incoming) && 591 (forw_packet->if_incoming == if_incoming) &&
417 592
418 /* packets from direct neighbors or 593 /* packets from direct neighbors or
@@ -601,9 +776,9 @@ static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
601 struct batadv_hard_iface *if_incoming) 776 struct batadv_hard_iface *if_incoming)
602{ 777{
603 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 778 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
604 uint8_t tt_num_changes; 779 uint16_t tvlv_len;
605 780
606 if (batadv_ogm_packet->header.ttl <= 1) { 781 if (batadv_ogm_packet->ttl <= 1) {
607 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); 782 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
608 return; 783 return;
609 } 784 }
@@ -621,9 +796,9 @@ static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
621 return; 796 return;
622 } 797 }
623 798
624 tt_num_changes = batadv_ogm_packet->tt_num_changes; 799 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
625 800
626 batadv_ogm_packet->header.ttl--; 801 batadv_ogm_packet->ttl--;
627 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN); 802 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
628 803
629 /* apply hop penalty */ 804 /* apply hop penalty */
@@ -632,7 +807,7 @@ static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
632 807
633 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 808 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
634 "Forwarding packet: tq: %i, ttl: %i\n", 809 "Forwarding packet: tq: %i, ttl: %i\n",
635 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl); 810 batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
636 811
637 /* switch of primaries first hop flag when forwarding */ 812 /* switch of primaries first hop flag when forwarding */
638 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP; 813 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
@@ -642,7 +817,7 @@ static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
642 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; 817 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
643 818
644 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet, 819 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
645 BATADV_OGM_HLEN + batadv_tt_len(tt_num_changes), 820 BATADV_OGM_HLEN + tvlv_len,
646 if_incoming, 0, batadv_iv_ogm_fwd_send_time()); 821 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
647} 822}
648 823
@@ -662,20 +837,22 @@ batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
662 uint32_t i; 837 uint32_t i;
663 size_t word_index; 838 size_t word_index;
664 uint8_t *w; 839 uint8_t *w;
840 int if_num;
665 841
666 for (i = 0; i < hash->size; i++) { 842 for (i = 0; i < hash->size; i++) {
667 head = &hash->table[i]; 843 head = &hash->table[i];
668 844
669 rcu_read_lock(); 845 rcu_read_lock();
670 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 846 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
671 spin_lock_bh(&orig_node->ogm_cnt_lock); 847 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
672 word_index = hard_iface->if_num * BATADV_NUM_WORDS; 848 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
673 word = &(orig_node->bcast_own[word_index]); 849 word = &(orig_node->bat_iv.bcast_own[word_index]);
674 850
675 batadv_bit_get_packet(bat_priv, word, 1, 0); 851 batadv_bit_get_packet(bat_priv, word, 1, 0);
676 w = &orig_node->bcast_own_sum[hard_iface->if_num]; 852 if_num = hard_iface->if_num;
853 w = &orig_node->bat_iv.bcast_own_sum[if_num];
677 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); 854 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
678 spin_unlock_bh(&orig_node->ogm_cnt_lock); 855 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
679 } 856 }
680 rcu_read_unlock(); 857 rcu_read_unlock();
681 } 858 }
@@ -688,43 +865,29 @@ static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
688 struct batadv_ogm_packet *batadv_ogm_packet; 865 struct batadv_ogm_packet *batadv_ogm_packet;
689 struct batadv_hard_iface *primary_if; 866 struct batadv_hard_iface *primary_if;
690 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len; 867 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
691 int vis_server, tt_num_changes = 0;
692 uint32_t seqno; 868 uint32_t seqno;
693 uint8_t bandwidth; 869 uint16_t tvlv_len = 0;
694 870
695 vis_server = atomic_read(&bat_priv->vis_mode);
696 primary_if = batadv_primary_if_get_selected(bat_priv); 871 primary_if = batadv_primary_if_get_selected(bat_priv);
697 872
698 if (hard_iface == primary_if) 873 if (hard_iface == primary_if) {
699 tt_num_changes = batadv_tt_append_diff(bat_priv, ogm_buff, 874 /* tt changes have to be committed before the tvlv data is
700 ogm_buff_len, 875 * appended as it may alter the tt tvlv container
701 BATADV_OGM_HLEN); 876 */
877 batadv_tt_local_commit_changes(bat_priv);
878 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
879 ogm_buff_len,
880 BATADV_OGM_HLEN);
881 }
702 882
703 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff); 883 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
884 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
704 885
705 /* change sequence number to network order */ 886 /* change sequence number to network order */
706 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno); 887 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
707 batadv_ogm_packet->seqno = htonl(seqno); 888 batadv_ogm_packet->seqno = htonl(seqno);
708 atomic_inc(&hard_iface->bat_iv.ogm_seqno); 889 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
709 890
710 batadv_ogm_packet->ttvn = atomic_read(&bat_priv->tt.vn);
711 batadv_ogm_packet->tt_crc = htons(bat_priv->tt.local_crc);
712 if (tt_num_changes >= 0)
713 batadv_ogm_packet->tt_num_changes = tt_num_changes;
714
715 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC)
716 batadv_ogm_packet->flags |= BATADV_VIS_SERVER;
717 else
718 batadv_ogm_packet->flags &= ~BATADV_VIS_SERVER;
719
720 if (hard_iface == primary_if &&
721 atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_SERVER) {
722 bandwidth = (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
723 batadv_ogm_packet->gw_flags = bandwidth;
724 } else {
725 batadv_ogm_packet->gw_flags = BATADV_NO_FLAGS;
726 }
727
728 batadv_iv_ogm_slide_own_bcast_window(hard_iface); 891 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
729 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff, 892 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
730 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1, 893 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
@@ -770,18 +933,18 @@ batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
770 if (dup_status != BATADV_NO_DUP) 933 if (dup_status != BATADV_NO_DUP)
771 continue; 934 continue;
772 935
773 spin_lock_bh(&tmp_neigh_node->lq_update_lock); 936 spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
774 batadv_ring_buffer_set(tmp_neigh_node->tq_recv, 937 batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv,
775 &tmp_neigh_node->tq_index, 0); 938 &tmp_neigh_node->bat_iv.tq_index, 0);
776 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv); 939 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv);
777 tmp_neigh_node->tq_avg = tq_avg; 940 tmp_neigh_node->bat_iv.tq_avg = tq_avg;
778 spin_unlock_bh(&tmp_neigh_node->lq_update_lock); 941 spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
779 } 942 }
780 943
781 if (!neigh_node) { 944 if (!neigh_node) {
782 struct batadv_orig_node *orig_tmp; 945 struct batadv_orig_node *orig_tmp;
783 946
784 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source); 947 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
785 if (!orig_tmp) 948 if (!orig_tmp)
786 goto unlock; 949 goto unlock;
787 950
@@ -798,80 +961,55 @@ batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
798 961
799 rcu_read_unlock(); 962 rcu_read_unlock();
800 963
801 orig_node->flags = batadv_ogm_packet->flags;
802 neigh_node->last_seen = jiffies; 964 neigh_node->last_seen = jiffies;
803 965
804 spin_lock_bh(&neigh_node->lq_update_lock); 966 spin_lock_bh(&neigh_node->bat_iv.lq_update_lock);
805 batadv_ring_buffer_set(neigh_node->tq_recv, 967 batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv,
806 &neigh_node->tq_index, 968 &neigh_node->bat_iv.tq_index,
807 batadv_ogm_packet->tq); 969 batadv_ogm_packet->tq);
808 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv); 970 tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv);
809 spin_unlock_bh(&neigh_node->lq_update_lock); 971 neigh_node->bat_iv.tq_avg = tq_avg;
972 spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock);
810 973
811 if (dup_status == BATADV_NO_DUP) { 974 if (dup_status == BATADV_NO_DUP) {
812 orig_node->last_ttl = batadv_ogm_packet->header.ttl; 975 orig_node->last_ttl = batadv_ogm_packet->ttl;
813 neigh_node->last_ttl = batadv_ogm_packet->header.ttl; 976 neigh_node->last_ttl = batadv_ogm_packet->ttl;
814 } 977 }
815 978
816 batadv_bonding_candidate_add(orig_node, neigh_node); 979 batadv_bonding_candidate_add(bat_priv, orig_node, neigh_node);
817 980
818 /* if this neighbor already is our next hop there is nothing 981 /* if this neighbor already is our next hop there is nothing
819 * to change 982 * to change
820 */ 983 */
821 router = batadv_orig_node_get_router(orig_node); 984 router = batadv_orig_node_get_router(orig_node);
822 if (router == neigh_node) 985 if (router == neigh_node)
823 goto update_tt; 986 goto out;
824 987
825 /* if this neighbor does not offer a better TQ we won't consider it */ 988 /* if this neighbor does not offer a better TQ we won't consider it */
826 if (router && (router->tq_avg > neigh_node->tq_avg)) 989 if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg))
827 goto update_tt; 990 goto out;
828 991
829 /* if the TQ is the same and the link not more symmetric we 992 /* if the TQ is the same and the link not more symmetric we
830 * won't consider it either 993 * won't consider it either
831 */ 994 */
832 if (router && (neigh_node->tq_avg == router->tq_avg)) { 995 if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) {
833 orig_node_tmp = router->orig_node; 996 orig_node_tmp = router->orig_node;
834 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); 997 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
835 if_num = router->if_incoming->if_num; 998 if_num = router->if_incoming->if_num;
836 sum_orig = orig_node_tmp->bcast_own_sum[if_num]; 999 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
837 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); 1000 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
838 1001
839 orig_node_tmp = neigh_node->orig_node; 1002 orig_node_tmp = neigh_node->orig_node;
840 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock); 1003 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
841 if_num = neigh_node->if_incoming->if_num; 1004 if_num = neigh_node->if_incoming->if_num;
842 sum_neigh = orig_node_tmp->bcast_own_sum[if_num]; 1005 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
843 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock); 1006 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
844 1007
845 if (sum_orig >= sum_neigh) 1008 if (sum_orig >= sum_neigh)
846 goto update_tt; 1009 goto out;
847 } 1010 }
848 1011
849 batadv_update_route(bat_priv, orig_node, neigh_node); 1012 batadv_update_route(bat_priv, orig_node, neigh_node);
850
851update_tt:
852 /* I have to check for transtable changes only if the OGM has been
853 * sent through a primary interface
854 */
855 if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
856 (batadv_ogm_packet->header.ttl > 2)) ||
857 (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
858 batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
859 batadv_ogm_packet->tt_num_changes,
860 batadv_ogm_packet->ttvn,
861 ntohs(batadv_ogm_packet->tt_crc));
862
863 if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
864 batadv_gw_node_update(bat_priv, orig_node,
865 batadv_ogm_packet->gw_flags);
866
867 orig_node->gw_flags = batadv_ogm_packet->gw_flags;
868
869 /* restart gateway selection if fast or late switching was enabled */
870 if ((orig_node->gw_flags) &&
871 (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
872 (atomic_read(&bat_priv->gw_sel_class) > 2))
873 batadv_gw_check_election(bat_priv, orig_node);
874
875 goto out; 1013 goto out;
876 1014
877unlock: 1015unlock:
@@ -893,7 +1031,7 @@ static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
893 uint8_t total_count; 1031 uint8_t total_count;
894 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own; 1032 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
895 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; 1033 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
896 int tq_asym_penalty, inv_asym_penalty, ret = 0; 1034 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
897 unsigned int combined_tq; 1035 unsigned int combined_tq;
898 1036
899 /* find corresponding one hop neighbor */ 1037 /* find corresponding one hop neighbor */
@@ -931,10 +1069,11 @@ static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
931 orig_node->last_seen = jiffies; 1069 orig_node->last_seen = jiffies;
932 1070
933 /* find packet count of corresponding one hop neighbor */ 1071 /* find packet count of corresponding one hop neighbor */
934 spin_lock_bh(&orig_node->ogm_cnt_lock); 1072 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
935 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num]; 1073 if_num = if_incoming->if_num;
936 neigh_rq_count = neigh_node->real_packet_count; 1074 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
937 spin_unlock_bh(&orig_node->ogm_cnt_lock); 1075 neigh_rq_count = neigh_node->bat_iv.real_packet_count;
1076 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
938 1077
939 /* pay attention to not get a value bigger than 100 % */ 1078 /* pay attention to not get a value bigger than 100 % */
940 if (orig_eq_count > neigh_rq_count) 1079 if (orig_eq_count > neigh_rq_count)
@@ -1016,12 +1155,13 @@ batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1016 uint32_t seqno = ntohl(batadv_ogm_packet->seqno); 1155 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
1017 uint8_t *neigh_addr; 1156 uint8_t *neigh_addr;
1018 uint8_t packet_count; 1157 uint8_t packet_count;
1158 unsigned long *bitmap;
1019 1159
1020 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig); 1160 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1021 if (!orig_node) 1161 if (!orig_node)
1022 return BATADV_NO_DUP; 1162 return BATADV_NO_DUP;
1023 1163
1024 spin_lock_bh(&orig_node->ogm_cnt_lock); 1164 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1025 seq_diff = seqno - orig_node->last_real_seqno; 1165 seq_diff = seqno - orig_node->last_real_seqno;
1026 1166
1027 /* signalize caller that the packet is to be dropped. */ 1167 /* signalize caller that the packet is to be dropped. */
@@ -1036,7 +1176,7 @@ batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1036 hlist_for_each_entry_rcu(tmp_neigh_node, 1176 hlist_for_each_entry_rcu(tmp_neigh_node,
1037 &orig_node->neigh_list, list) { 1177 &orig_node->neigh_list, list) {
1038 neigh_addr = tmp_neigh_node->addr; 1178 neigh_addr = tmp_neigh_node->addr;
1039 is_dup = batadv_test_bit(tmp_neigh_node->real_bits, 1179 is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits,
1040 orig_node->last_real_seqno, 1180 orig_node->last_real_seqno,
1041 seqno); 1181 seqno);
1042 1182
@@ -1052,13 +1192,13 @@ batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1052 } 1192 }
1053 1193
1054 /* if the window moved, set the update flag. */ 1194 /* if the window moved, set the update flag. */
1055 need_update |= batadv_bit_get_packet(bat_priv, 1195 bitmap = tmp_neigh_node->bat_iv.real_bits;
1056 tmp_neigh_node->real_bits, 1196 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1057 seq_diff, set_mark); 1197 seq_diff, set_mark);
1058 1198
1059 packet_count = bitmap_weight(tmp_neigh_node->real_bits, 1199 packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits,
1060 BATADV_TQ_LOCAL_WINDOW_SIZE); 1200 BATADV_TQ_LOCAL_WINDOW_SIZE);
1061 tmp_neigh_node->real_packet_count = packet_count; 1201 tmp_neigh_node->bat_iv.real_packet_count = packet_count;
1062 } 1202 }
1063 rcu_read_unlock(); 1203 rcu_read_unlock();
1064 1204
@@ -1070,7 +1210,7 @@ batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1070 } 1210 }
1071 1211
1072out: 1212out:
1073 spin_unlock_bh(&orig_node->ogm_cnt_lock); 1213 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1074 batadv_orig_node_free_ref(orig_node); 1214 batadv_orig_node_free_ref(orig_node);
1075 return ret; 1215 return ret;
1076} 1216}
@@ -1082,7 +1222,7 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1082{ 1222{
1083 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); 1223 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1084 struct batadv_hard_iface *hard_iface; 1224 struct batadv_hard_iface *hard_iface;
1085 struct batadv_orig_node *orig_neigh_node, *orig_node; 1225 struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp;
1086 struct batadv_neigh_node *router = NULL, *router_router = NULL; 1226 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1087 struct batadv_neigh_node *orig_neigh_router = NULL; 1227 struct batadv_neigh_node *orig_neigh_router = NULL;
1088 int has_directlink_flag; 1228 int has_directlink_flag;
@@ -1107,7 +1247,7 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1107 * packet in an aggregation. Here we expect that the padding 1247 * packet in an aggregation. Here we expect that the padding
1108 * is always zero (or not 0x01) 1248 * is always zero (or not 0x01)
1109 */ 1249 */
1110 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM) 1250 if (batadv_ogm_packet->packet_type != BATADV_IV_OGM)
1111 return; 1251 return;
1112 1252
1113 /* could be changed by schedule_own_packet() */ 1253 /* could be changed by schedule_own_packet() */
@@ -1122,15 +1262,13 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1122 is_single_hop_neigh = true; 1262 is_single_hop_neigh = true;
1123 1263
1124 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1125 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %#.4x, changes %u, tq %d, TTL %d, V %d, IDF %d)\n", 1265 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1126 ethhdr->h_source, if_incoming->net_dev->name, 1266 ethhdr->h_source, if_incoming->net_dev->name,
1127 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig, 1267 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1128 batadv_ogm_packet->prev_sender, 1268 batadv_ogm_packet->prev_sender,
1129 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn, 1269 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
1130 ntohs(batadv_ogm_packet->tt_crc), 1270 batadv_ogm_packet->ttl,
1131 batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq, 1271 batadv_ogm_packet->version, has_directlink_flag);
1132 batadv_ogm_packet->header.ttl,
1133 batadv_ogm_packet->header.version, has_directlink_flag);
1134 1272
1135 rcu_read_lock(); 1273 rcu_read_lock();
1136 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 1274 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
@@ -1168,8 +1306,8 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1168 int16_t if_num; 1306 int16_t if_num;
1169 uint8_t *weight; 1307 uint8_t *weight;
1170 1308
1171 orig_neigh_node = batadv_get_orig_node(bat_priv, 1309 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1172 ethhdr->h_source); 1310 ethhdr->h_source);
1173 if (!orig_neigh_node) 1311 if (!orig_neigh_node)
1174 return; 1312 return;
1175 1313
@@ -1183,15 +1321,15 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1183 if_num = if_incoming->if_num; 1321 if_num = if_incoming->if_num;
1184 offset = if_num * BATADV_NUM_WORDS; 1322 offset = if_num * BATADV_NUM_WORDS;
1185 1323
1186 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock); 1324 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1187 word = &(orig_neigh_node->bcast_own[offset]); 1325 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
1188 bit_pos = if_incoming_seqno - 2; 1326 bit_pos = if_incoming_seqno - 2;
1189 bit_pos -= ntohl(batadv_ogm_packet->seqno); 1327 bit_pos -= ntohl(batadv_ogm_packet->seqno);
1190 batadv_set_bit(word, bit_pos); 1328 batadv_set_bit(word, bit_pos);
1191 weight = &orig_neigh_node->bcast_own_sum[if_num]; 1329 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1192 *weight = bitmap_weight(word, 1330 *weight = bitmap_weight(word,
1193 BATADV_TQ_LOCAL_WINDOW_SIZE); 1331 BATADV_TQ_LOCAL_WINDOW_SIZE);
1194 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock); 1332 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1195 } 1333 }
1196 1334
1197 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 1335 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
@@ -1214,7 +1352,7 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1214 return; 1352 return;
1215 } 1353 }
1216 1354
1217 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig); 1355 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1218 if (!orig_node) 1356 if (!orig_node)
1219 return; 1357 return;
1220 1358
@@ -1235,10 +1373,12 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1235 } 1373 }
1236 1374
1237 router = batadv_orig_node_get_router(orig_node); 1375 router = batadv_orig_node_get_router(orig_node);
1238 if (router) 1376 if (router) {
1239 router_router = batadv_orig_node_get_router(router->orig_node); 1377 orig_node_tmp = router->orig_node;
1378 router_router = batadv_orig_node_get_router(orig_node_tmp);
1379 }
1240 1380
1241 if ((router && router->tq_avg != 0) && 1381 if ((router && router->bat_iv.tq_avg != 0) &&
1242 (batadv_compare_eth(router->addr, ethhdr->h_source))) 1382 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1243 is_from_best_next_hop = true; 1383 is_from_best_next_hop = true;
1244 1384
@@ -1254,14 +1394,16 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1254 goto out; 1394 goto out;
1255 } 1395 }
1256 1396
1397 batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1398
1257 /* if sender is a direct neighbor the sender mac equals 1399 /* if sender is a direct neighbor the sender mac equals
1258 * originator mac 1400 * originator mac
1259 */ 1401 */
1260 if (is_single_hop_neigh) 1402 if (is_single_hop_neigh)
1261 orig_neigh_node = orig_node; 1403 orig_neigh_node = orig_node;
1262 else 1404 else
1263 orig_neigh_node = batadv_get_orig_node(bat_priv, 1405 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1264 ethhdr->h_source); 1406 ethhdr->h_source);
1265 1407
1266 if (!orig_neigh_node) 1408 if (!orig_neigh_node)
1267 goto out; 1409 goto out;
@@ -1291,7 +1433,7 @@ static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1291 * seqno and similar ttl as the non-duplicate 1433 * seqno and similar ttl as the non-duplicate
1292 */ 1434 */
1293 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno); 1435 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
1294 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl; 1436 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->ttl;
1295 if (is_bidirect && ((dup_status == BATADV_NO_DUP) || 1437 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1296 (sameseq && similar_ttl))) 1438 (sameseq && similar_ttl)))
1297 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr, 1439 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
@@ -1350,9 +1492,9 @@ static int batadv_iv_ogm_receive(struct sk_buff *skb,
1350 struct batadv_ogm_packet *batadv_ogm_packet; 1492 struct batadv_ogm_packet *batadv_ogm_packet;
1351 struct ethhdr *ethhdr; 1493 struct ethhdr *ethhdr;
1352 int buff_pos = 0, packet_len; 1494 int buff_pos = 0, packet_len;
1353 unsigned char *tt_buff, *packet_buff; 1495 unsigned char *tvlv_buff, *packet_buff;
1354 bool ret;
1355 uint8_t *packet_pos; 1496 uint8_t *packet_pos;
1497 bool ret;
1356 1498
1357 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); 1499 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1358 if (!ret) 1500 if (!ret)
@@ -1375,14 +1517,14 @@ static int batadv_iv_ogm_receive(struct sk_buff *skb,
1375 1517
1376 /* unpack the aggregated packets and process them one by one */ 1518 /* unpack the aggregated packets and process them one by one */
1377 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len, 1519 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1378 batadv_ogm_packet->tt_num_changes)) { 1520 batadv_ogm_packet->tvlv_len)) {
1379 tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN; 1521 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
1380 1522
1381 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff, 1523 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1382 if_incoming); 1524 tvlv_buff, if_incoming);
1383 1525
1384 buff_pos += BATADV_OGM_HLEN; 1526 buff_pos += BATADV_OGM_HLEN;
1385 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes); 1527 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
1386 1528
1387 packet_pos = packet_buff + buff_pos; 1529 packet_pos = packet_buff + buff_pos;
1388 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; 1530 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
@@ -1392,6 +1534,106 @@ static int batadv_iv_ogm_receive(struct sk_buff *skb,
1392 return NET_RX_SUCCESS; 1534 return NET_RX_SUCCESS;
1393} 1535}
1394 1536
1537/**
1538 * batadv_iv_ogm_orig_print - print the originator table
1539 * @bat_priv: the bat priv with all the soft interface information
1540 * @seq: debugfs table seq_file struct
1541 */
1542static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
1543 struct seq_file *seq)
1544{
1545 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
1546 struct batadv_hashtable *hash = bat_priv->orig_hash;
1547 int last_seen_msecs, last_seen_secs;
1548 struct batadv_orig_node *orig_node;
1549 unsigned long last_seen_jiffies;
1550 struct hlist_head *head;
1551 int batman_count = 0;
1552 uint32_t i;
1553
1554 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
1555 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
1556 "Nexthop", "outgoingIF", "Potential nexthops");
1557
1558 for (i = 0; i < hash->size; i++) {
1559 head = &hash->table[i];
1560
1561 rcu_read_lock();
1562 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1563 neigh_node = batadv_orig_node_get_router(orig_node);
1564 if (!neigh_node)
1565 continue;
1566
1567 if (neigh_node->bat_iv.tq_avg == 0)
1568 goto next;
1569
1570 last_seen_jiffies = jiffies - orig_node->last_seen;
1571 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1572 last_seen_secs = last_seen_msecs / 1000;
1573 last_seen_msecs = last_seen_msecs % 1000;
1574
1575 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
1576 orig_node->orig, last_seen_secs,
1577 last_seen_msecs, neigh_node->bat_iv.tq_avg,
1578 neigh_node->addr,
1579 neigh_node->if_incoming->net_dev->name);
1580
1581 hlist_for_each_entry_rcu(neigh_node_tmp,
1582 &orig_node->neigh_list, list) {
1583 seq_printf(seq, " %pM (%3i)",
1584 neigh_node_tmp->addr,
1585 neigh_node_tmp->bat_iv.tq_avg);
1586 }
1587
1588 seq_puts(seq, "\n");
1589 batman_count++;
1590
1591next:
1592 batadv_neigh_node_free_ref(neigh_node);
1593 }
1594 rcu_read_unlock();
1595 }
1596
1597 if (batman_count == 0)
1598 seq_puts(seq, "No batman nodes in range ...\n");
1599}
1600
1601/**
1602 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
1603 * @neigh1: the first neighbor object of the comparison
1604 * @neigh2: the second neighbor object of the comparison
1605 *
1606 * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
1607 * lower, the same as or higher than the metric via neigh2
1608 */
1609static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
1610 struct batadv_neigh_node *neigh2)
1611{
1612 uint8_t tq1, tq2;
1613
1614 tq1 = neigh1->bat_iv.tq_avg;
1615 tq2 = neigh2->bat_iv.tq_avg;
1616
1617 return tq1 - tq2;
1618}
1619
1620/**
1621 * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
1622 * neigh2 from the metric prospective
1623 * @neigh1: the first neighbor object of the comparison
1624 * @neigh2: the second neighbor object of the comparison
1625 *
1626 * Returns true if the metric via neigh1 is equally good or better than the
1627 * metric via neigh2, false otherwise.
1628 */
1629static bool batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
1630 struct batadv_neigh_node *neigh2)
1631{
1632 int diff = batadv_iv_ogm_neigh_cmp(neigh1, neigh2);
1633
1634 return diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
1635}
1636
1395static struct batadv_algo_ops batadv_batman_iv __read_mostly = { 1637static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1396 .name = "BATMAN_IV", 1638 .name = "BATMAN_IV",
1397 .bat_iface_enable = batadv_iv_ogm_iface_enable, 1639 .bat_iface_enable = batadv_iv_ogm_iface_enable,
@@ -1400,6 +1642,12 @@ static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1400 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set, 1642 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1401 .bat_ogm_schedule = batadv_iv_ogm_schedule, 1643 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1402 .bat_ogm_emit = batadv_iv_ogm_emit, 1644 .bat_ogm_emit = batadv_iv_ogm_emit,
1645 .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
1646 .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
1647 .bat_orig_print = batadv_iv_ogm_orig_print,
1648 .bat_orig_free = batadv_iv_ogm_orig_free,
1649 .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
1650 .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
1403}; 1651};
1404 1652
1405int __init batadv_iv_init(void) 1653int __init batadv_iv_init(void)
diff --git a/net/batman-adv/bridge_loop_avoidance.c b/net/batman-adv/bridge_loop_avoidance.c
index 264de88db320..28eb5e6d0a02 100644
--- a/net/batman-adv/bridge_loop_avoidance.c
+++ b/net/batman-adv/bridge_loop_avoidance.c
@@ -411,10 +411,10 @@ batadv_bla_get_backbone_gw(struct batadv_priv *bat_priv, uint8_t *orig,
411 return NULL; 411 return NULL;
412 } 412 }
413 413
414 /* this is a gateway now, remove any tt entries */ 414 /* this is a gateway now, remove any TT entry on this VLAN */
415 orig_node = batadv_orig_hash_find(bat_priv, orig); 415 orig_node = batadv_orig_hash_find(bat_priv, orig);
416 if (orig_node) { 416 if (orig_node) {
417 batadv_tt_global_del_orig(bat_priv, orig_node, 417 batadv_tt_global_del_orig(bat_priv, orig_node, vid,
418 "became a backbone gateway"); 418 "became a backbone gateway");
419 batadv_orig_node_free_ref(orig_node); 419 batadv_orig_node_free_ref(orig_node);
420 } 420 }
@@ -858,30 +858,28 @@ static int batadv_bla_process_claim(struct batadv_priv *bat_priv,
858 struct batadv_hard_iface *primary_if, 858 struct batadv_hard_iface *primary_if,
859 struct sk_buff *skb) 859 struct sk_buff *skb)
860{ 860{
861 struct ethhdr *ethhdr; 861 struct batadv_bla_claim_dst *bla_dst;
862 uint8_t *hw_src, *hw_dst;
862 struct vlan_ethhdr *vhdr; 863 struct vlan_ethhdr *vhdr;
864 struct ethhdr *ethhdr;
863 struct arphdr *arphdr; 865 struct arphdr *arphdr;
864 uint8_t *hw_src, *hw_dst; 866 unsigned short vid;
865 struct batadv_bla_claim_dst *bla_dst; 867 __be16 proto;
866 uint16_t proto;
867 int headlen; 868 int headlen;
868 unsigned short vid = BATADV_NO_FLAGS;
869 int ret; 869 int ret;
870 870
871 vid = batadv_get_vid(skb, 0);
871 ethhdr = eth_hdr(skb); 872 ethhdr = eth_hdr(skb);
872 873
873 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) { 874 proto = ethhdr->h_proto;
875 headlen = ETH_HLEN;
876 if (vid & BATADV_VLAN_HAS_TAG) {
874 vhdr = (struct vlan_ethhdr *)ethhdr; 877 vhdr = (struct vlan_ethhdr *)ethhdr;
875 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK; 878 proto = vhdr->h_vlan_encapsulated_proto;
876 vid |= BATADV_VLAN_HAS_TAG; 879 headlen += VLAN_HLEN;
877 proto = ntohs(vhdr->h_vlan_encapsulated_proto);
878 headlen = sizeof(*vhdr);
879 } else {
880 proto = ntohs(ethhdr->h_proto);
881 headlen = ETH_HLEN;
882 } 880 }
883 881
884 if (proto != ETH_P_ARP) 882 if (proto != htons(ETH_P_ARP))
885 return 0; /* not a claim frame */ 883 return 0; /* not a claim frame */
886 884
887 /* this must be a ARP frame. check if it is a claim. */ 885 /* this must be a ARP frame. check if it is a claim. */
@@ -1317,12 +1315,14 @@ out:
1317 1315
1318/* @bat_priv: the bat priv with all the soft interface information 1316/* @bat_priv: the bat priv with all the soft interface information
1319 * @orig: originator mac address 1317 * @orig: originator mac address
1318 * @vid: VLAN identifier
1320 * 1319 *
1321 * check if the originator is a gateway for any VLAN ID. 1320 * Check if the originator is a gateway for the VLAN identified by vid.
1322 * 1321 *
1323 * returns 1 if it is found, 0 otherwise 1322 * Returns true if orig is a backbone for this vid, false otherwise.
1324 */ 1323 */
1325int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig) 1324bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
1325 unsigned short vid)
1326{ 1326{
1327 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash; 1327 struct batadv_hashtable *hash = bat_priv->bla.backbone_hash;
1328 struct hlist_head *head; 1328 struct hlist_head *head;
@@ -1330,25 +1330,26 @@ int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
1330 int i; 1330 int i;
1331 1331
1332 if (!atomic_read(&bat_priv->bridge_loop_avoidance)) 1332 if (!atomic_read(&bat_priv->bridge_loop_avoidance))
1333 return 0; 1333 return false;
1334 1334
1335 if (!hash) 1335 if (!hash)
1336 return 0; 1336 return false;
1337 1337
1338 for (i = 0; i < hash->size; i++) { 1338 for (i = 0; i < hash->size; i++) {
1339 head = &hash->table[i]; 1339 head = &hash->table[i];
1340 1340
1341 rcu_read_lock(); 1341 rcu_read_lock();
1342 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) { 1342 hlist_for_each_entry_rcu(backbone_gw, head, hash_entry) {
1343 if (batadv_compare_eth(backbone_gw->orig, orig)) { 1343 if (batadv_compare_eth(backbone_gw->orig, orig) &&
1344 backbone_gw->vid == vid) {
1344 rcu_read_unlock(); 1345 rcu_read_unlock();
1345 return 1; 1346 return true;
1346 } 1347 }
1347 } 1348 }
1348 rcu_read_unlock(); 1349 rcu_read_unlock();
1349 } 1350 }
1350 1351
1351 return 0; 1352 return false;
1352} 1353}
1353 1354
1354 1355
@@ -1365,10 +1366,8 @@ int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig)
1365int batadv_bla_is_backbone_gw(struct sk_buff *skb, 1366int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1366 struct batadv_orig_node *orig_node, int hdr_size) 1367 struct batadv_orig_node *orig_node, int hdr_size)
1367{ 1368{
1368 struct ethhdr *ethhdr;
1369 struct vlan_ethhdr *vhdr;
1370 struct batadv_bla_backbone_gw *backbone_gw; 1369 struct batadv_bla_backbone_gw *backbone_gw;
1371 unsigned short vid = BATADV_NO_FLAGS; 1370 unsigned short vid;
1372 1371
1373 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance)) 1372 if (!atomic_read(&orig_node->bat_priv->bridge_loop_avoidance))
1374 return 0; 1373 return 0;
@@ -1377,16 +1376,7 @@ int batadv_bla_is_backbone_gw(struct sk_buff *skb,
1377 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN)) 1376 if (!pskb_may_pull(skb, hdr_size + ETH_HLEN))
1378 return 0; 1377 return 0;
1379 1378
1380 ethhdr = (struct ethhdr *)(((uint8_t *)skb->data) + hdr_size); 1379 vid = batadv_get_vid(skb, hdr_size);
1381
1382 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
1383 if (!pskb_may_pull(skb, hdr_size + sizeof(struct vlan_ethhdr)))
1384 return 0;
1385
1386 vhdr = (struct vlan_ethhdr *)(skb->data + hdr_size);
1387 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1388 vid |= BATADV_VLAN_HAS_TAG;
1389 }
1390 1380
1391 /* see if this originator is a backbone gw for this VLAN */ 1381 /* see if this originator is a backbone gw for this VLAN */
1392 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv, 1382 backbone_gw = batadv_backbone_hash_find(orig_node->bat_priv,
diff --git a/net/batman-adv/bridge_loop_avoidance.h b/net/batman-adv/bridge_loop_avoidance.h
index 4b102e71e5bd..da173e760e77 100644
--- a/net/batman-adv/bridge_loop_avoidance.h
+++ b/net/batman-adv/bridge_loop_avoidance.h
@@ -30,7 +30,8 @@ int batadv_bla_is_backbone_gw(struct sk_buff *skb,
30int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset); 30int batadv_bla_claim_table_seq_print_text(struct seq_file *seq, void *offset);
31int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq, 31int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq,
32 void *offset); 32 void *offset);
33int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig); 33bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, uint8_t *orig,
34 unsigned short vid);
34int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv, 35int batadv_bla_check_bcast_duplist(struct batadv_priv *bat_priv,
35 struct sk_buff *skb); 36 struct sk_buff *skb);
36void batadv_bla_update_orig_address(struct batadv_priv *bat_priv, 37void batadv_bla_update_orig_address(struct batadv_priv *bat_priv,
@@ -74,10 +75,11 @@ static inline int batadv_bla_backbone_table_seq_print_text(struct seq_file *seq,
74 return 0; 75 return 0;
75} 76}
76 77
77static inline int batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv, 78static inline bool batadv_bla_is_backbone_gw_orig(struct batadv_priv *bat_priv,
78 uint8_t *orig) 79 uint8_t *orig,
80 unsigned short vid)
79{ 81{
80 return 0; 82 return false;
81} 83}
82 84
83static inline int 85static inline int
diff --git a/net/batman-adv/debugfs.c b/net/batman-adv/debugfs.c
index f186a55b23c3..049a7a2ac5b6 100644
--- a/net/batman-adv/debugfs.c
+++ b/net/batman-adv/debugfs.c
@@ -28,7 +28,6 @@
28#include "gateway_common.h" 28#include "gateway_common.h"
29#include "gateway_client.h" 29#include "gateway_client.h"
30#include "soft-interface.h" 30#include "soft-interface.h"
31#include "vis.h"
32#include "icmp_socket.h" 31#include "icmp_socket.h"
33#include "bridge_loop_avoidance.h" 32#include "bridge_loop_avoidance.h"
34#include "distributed-arp-table.h" 33#include "distributed-arp-table.h"
@@ -300,12 +299,6 @@ static int batadv_transtable_local_open(struct inode *inode, struct file *file)
300 return single_open(file, batadv_tt_local_seq_print_text, net_dev); 299 return single_open(file, batadv_tt_local_seq_print_text, net_dev);
301} 300}
302 301
303static int batadv_vis_data_open(struct inode *inode, struct file *file)
304{
305 struct net_device *net_dev = (struct net_device *)inode->i_private;
306 return single_open(file, batadv_vis_seq_print_text, net_dev);
307}
308
309struct batadv_debuginfo { 302struct batadv_debuginfo {
310 struct attribute attr; 303 struct attribute attr;
311 const struct file_operations fops; 304 const struct file_operations fops;
@@ -356,7 +349,6 @@ static BATADV_DEBUGINFO(dat_cache, S_IRUGO, batadv_dat_cache_open);
356#endif 349#endif
357static BATADV_DEBUGINFO(transtable_local, S_IRUGO, 350static BATADV_DEBUGINFO(transtable_local, S_IRUGO,
358 batadv_transtable_local_open); 351 batadv_transtable_local_open);
359static BATADV_DEBUGINFO(vis_data, S_IRUGO, batadv_vis_data_open);
360#ifdef CONFIG_BATMAN_ADV_NC 352#ifdef CONFIG_BATMAN_ADV_NC
361static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open); 353static BATADV_DEBUGINFO(nc_nodes, S_IRUGO, batadv_nc_nodes_open);
362#endif 354#endif
@@ -373,7 +365,6 @@ static struct batadv_debuginfo *batadv_mesh_debuginfos[] = {
373 &batadv_debuginfo_dat_cache, 365 &batadv_debuginfo_dat_cache,
374#endif 366#endif
375 &batadv_debuginfo_transtable_local, 367 &batadv_debuginfo_transtable_local,
376 &batadv_debuginfo_vis_data,
377#ifdef CONFIG_BATMAN_ADV_NC 368#ifdef CONFIG_BATMAN_ADV_NC
378 &batadv_debuginfo_nc_nodes, 369 &batadv_debuginfo_nc_nodes,
379#endif 370#endif
diff --git a/net/batman-adv/distributed-arp-table.c b/net/batman-adv/distributed-arp-table.c
index 06345d401588..b316a4cb6f14 100644
--- a/net/batman-adv/distributed-arp-table.c
+++ b/net/batman-adv/distributed-arp-table.c
@@ -19,6 +19,7 @@
19 19
20#include <linux/if_ether.h> 20#include <linux/if_ether.h>
21#include <linux/if_arp.h> 21#include <linux/if_arp.h>
22#include <linux/if_vlan.h>
22#include <net/arp.h> 23#include <net/arp.h>
23 24
24#include "main.h" 25#include "main.h"
@@ -29,7 +30,6 @@
29#include "send.h" 30#include "send.h"
30#include "types.h" 31#include "types.h"
31#include "translation-table.h" 32#include "translation-table.h"
32#include "unicast.h"
33 33
34static void batadv_dat_purge(struct work_struct *work); 34static void batadv_dat_purge(struct work_struct *work);
35 35
@@ -206,15 +206,11 @@ static __be32 batadv_arp_ip_dst(struct sk_buff *skb, int hdr_size)
206 */ 206 */
207static uint32_t batadv_hash_dat(const void *data, uint32_t size) 207static uint32_t batadv_hash_dat(const void *data, uint32_t size)
208{ 208{
209 const unsigned char *key = data;
210 uint32_t hash = 0; 209 uint32_t hash = 0;
211 size_t i; 210 const struct batadv_dat_entry *dat = data;
212 211
213 for (i = 0; i < 4; i++) { 212 hash = batadv_hash_bytes(hash, &dat->ip, sizeof(dat->ip));
214 hash += key[i]; 213 hash = batadv_hash_bytes(hash, &dat->vid, sizeof(dat->vid));
215 hash += (hash << 10);
216 hash ^= (hash >> 6);
217 }
218 214
219 hash += (hash << 3); 215 hash += (hash << 3);
220 hash ^= (hash >> 11); 216 hash ^= (hash >> 11);
@@ -228,21 +224,26 @@ static uint32_t batadv_hash_dat(const void *data, uint32_t size)
228 * table 224 * table
229 * @bat_priv: the bat priv with all the soft interface information 225 * @bat_priv: the bat priv with all the soft interface information
230 * @ip: search key 226 * @ip: search key
227 * @vid: VLAN identifier
231 * 228 *
232 * Returns the dat_entry if found, NULL otherwise. 229 * Returns the dat_entry if found, NULL otherwise.
233 */ 230 */
234static struct batadv_dat_entry * 231static struct batadv_dat_entry *
235batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip) 232batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip,
233 unsigned short vid)
236{ 234{
237 struct hlist_head *head; 235 struct hlist_head *head;
238 struct batadv_dat_entry *dat_entry, *dat_entry_tmp = NULL; 236 struct batadv_dat_entry to_find, *dat_entry, *dat_entry_tmp = NULL;
239 struct batadv_hashtable *hash = bat_priv->dat.hash; 237 struct batadv_hashtable *hash = bat_priv->dat.hash;
240 uint32_t index; 238 uint32_t index;
241 239
242 if (!hash) 240 if (!hash)
243 return NULL; 241 return NULL;
244 242
245 index = batadv_hash_dat(&ip, hash->size); 243 to_find.ip = ip;
244 to_find.vid = vid;
245
246 index = batadv_hash_dat(&to_find, hash->size);
246 head = &hash->table[index]; 247 head = &hash->table[index];
247 248
248 rcu_read_lock(); 249 rcu_read_lock();
@@ -266,22 +267,24 @@ batadv_dat_entry_hash_find(struct batadv_priv *bat_priv, __be32 ip)
266 * @bat_priv: the bat priv with all the soft interface information 267 * @bat_priv: the bat priv with all the soft interface information
267 * @ip: ipv4 to add/edit 268 * @ip: ipv4 to add/edit
268 * @mac_addr: mac address to assign to the given ipv4 269 * @mac_addr: mac address to assign to the given ipv4
270 * @vid: VLAN identifier
269 */ 271 */
270static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip, 272static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
271 uint8_t *mac_addr) 273 uint8_t *mac_addr, unsigned short vid)
272{ 274{
273 struct batadv_dat_entry *dat_entry; 275 struct batadv_dat_entry *dat_entry;
274 int hash_added; 276 int hash_added;
275 277
276 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip); 278 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip, vid);
277 /* if this entry is already known, just update it */ 279 /* if this entry is already known, just update it */
278 if (dat_entry) { 280 if (dat_entry) {
279 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr)) 281 if (!batadv_compare_eth(dat_entry->mac_addr, mac_addr))
280 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN); 282 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
281 dat_entry->last_update = jiffies; 283 dat_entry->last_update = jiffies;
282 batadv_dbg(BATADV_DBG_DAT, bat_priv, 284 batadv_dbg(BATADV_DBG_DAT, bat_priv,
283 "Entry updated: %pI4 %pM\n", &dat_entry->ip, 285 "Entry updated: %pI4 %pM (vid: %d)\n",
284 dat_entry->mac_addr); 286 &dat_entry->ip, dat_entry->mac_addr,
287 BATADV_PRINT_VID(vid));
285 goto out; 288 goto out;
286 } 289 }
287 290
@@ -290,12 +293,13 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
290 goto out; 293 goto out;
291 294
292 dat_entry->ip = ip; 295 dat_entry->ip = ip;
296 dat_entry->vid = vid;
293 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN); 297 memcpy(dat_entry->mac_addr, mac_addr, ETH_ALEN);
294 dat_entry->last_update = jiffies; 298 dat_entry->last_update = jiffies;
295 atomic_set(&dat_entry->refcount, 2); 299 atomic_set(&dat_entry->refcount, 2);
296 300
297 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat, 301 hash_added = batadv_hash_add(bat_priv->dat.hash, batadv_compare_dat,
298 batadv_hash_dat, &dat_entry->ip, 302 batadv_hash_dat, dat_entry,
299 &dat_entry->hash_entry); 303 &dat_entry->hash_entry);
300 304
301 if (unlikely(hash_added != 0)) { 305 if (unlikely(hash_added != 0)) {
@@ -304,8 +308,8 @@ static void batadv_dat_entry_add(struct batadv_priv *bat_priv, __be32 ip,
304 goto out; 308 goto out;
305 } 309 }
306 310
307 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM\n", 311 batadv_dbg(BATADV_DBG_DAT, bat_priv, "New entry added: %pI4 %pM (vid: %d)\n",
308 &dat_entry->ip, dat_entry->mac_addr); 312 &dat_entry->ip, dat_entry->mac_addr, BATADV_PRINT_VID(vid));
309 313
310out: 314out:
311 if (dat_entry) 315 if (dat_entry)
@@ -345,7 +349,7 @@ static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
345 349
346 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; 350 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
347 351
348 switch (unicast_4addr_packet->u.header.packet_type) { 352 switch (unicast_4addr_packet->u.packet_type) {
349 case BATADV_UNICAST: 353 case BATADV_UNICAST:
350 batadv_dbg(BATADV_DBG_DAT, bat_priv, 354 batadv_dbg(BATADV_DBG_DAT, bat_priv,
351 "* encapsulated within a UNICAST packet\n"); 355 "* encapsulated within a UNICAST packet\n");
@@ -370,7 +374,7 @@ static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
370 break; 374 break;
371 default: 375 default:
372 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n", 376 batadv_dbg(BATADV_DBG_DAT, bat_priv, "* type: Unknown (%u)!\n",
373 unicast_4addr_packet->u.header.packet_type); 377 unicast_4addr_packet->u.packet_type);
374 } 378 }
375 break; 379 break;
376 case BATADV_BCAST: 380 case BATADV_BCAST:
@@ -383,7 +387,7 @@ static void batadv_dbg_arp(struct batadv_priv *bat_priv, struct sk_buff *skb,
383 default: 387 default:
384 batadv_dbg(BATADV_DBG_DAT, bat_priv, 388 batadv_dbg(BATADV_DBG_DAT, bat_priv,
385 "* encapsulated within an unknown packet type (0x%x)\n", 389 "* encapsulated within an unknown packet type (0x%x)\n",
386 unicast_4addr_packet->u.header.packet_type); 390 unicast_4addr_packet->u.packet_type);
387 } 391 }
388} 392}
389 393
@@ -419,6 +423,10 @@ static bool batadv_is_orig_node_eligible(struct batadv_dat_candidate *res,
419 bool ret = false; 423 bool ret = false;
420 int j; 424 int j;
421 425
426 /* check if orig node candidate is running DAT */
427 if (!(candidate->capabilities & BATADV_ORIG_CAPA_HAS_DAT))
428 goto out;
429
422 /* Check if this node has already been selected... */ 430 /* Check if this node has already been selected... */
423 for (j = 0; j < select; j++) 431 for (j = 0; j < select; j++)
424 if (res[j].orig_node == candidate) 432 if (res[j].orig_node == candidate)
@@ -588,9 +596,9 @@ static bool batadv_dat_send_data(struct batadv_priv *bat_priv,
588 goto free_orig; 596 goto free_orig;
589 597
590 tmp_skb = pskb_copy(skb, GFP_ATOMIC); 598 tmp_skb = pskb_copy(skb, GFP_ATOMIC);
591 if (!batadv_unicast_4addr_prepare_skb(bat_priv, tmp_skb, 599 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, tmp_skb,
592 cand[i].orig_node, 600 cand[i].orig_node,
593 packet_subtype)) { 601 packet_subtype)) {
594 kfree_skb(tmp_skb); 602 kfree_skb(tmp_skb);
595 goto free_neigh; 603 goto free_neigh;
596 } 604 }
@@ -626,6 +634,59 @@ out:
626} 634}
627 635
628/** 636/**
637 * batadv_dat_tvlv_container_update - update the dat tvlv container after dat
638 * setting change
639 * @bat_priv: the bat priv with all the soft interface information
640 */
641static void batadv_dat_tvlv_container_update(struct batadv_priv *bat_priv)
642{
643 char dat_mode;
644
645 dat_mode = atomic_read(&bat_priv->distributed_arp_table);
646
647 switch (dat_mode) {
648 case 0:
649 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
650 break;
651 case 1:
652 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_DAT, 1,
653 NULL, 0);
654 break;
655 }
656}
657
658/**
659 * batadv_dat_status_update - update the dat tvlv container after dat
660 * setting change
661 * @net_dev: the soft interface net device
662 */
663void batadv_dat_status_update(struct net_device *net_dev)
664{
665 struct batadv_priv *bat_priv = netdev_priv(net_dev);
666 batadv_dat_tvlv_container_update(bat_priv);
667}
668
669/**
670 * batadv_gw_tvlv_ogm_handler_v1 - process incoming dat tvlv container
671 * @bat_priv: the bat priv with all the soft interface information
672 * @orig: the orig_node of the ogm
673 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
674 * @tvlv_value: tvlv buffer containing the gateway data
675 * @tvlv_value_len: tvlv buffer length
676 */
677static void batadv_dat_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
678 struct batadv_orig_node *orig,
679 uint8_t flags,
680 void *tvlv_value,
681 uint16_t tvlv_value_len)
682{
683 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
684 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_DAT;
685 else
686 orig->capabilities |= BATADV_ORIG_CAPA_HAS_DAT;
687}
688
689/**
629 * batadv_dat_hash_free - free the local DAT hash table 690 * batadv_dat_hash_free - free the local DAT hash table
630 * @bat_priv: the bat priv with all the soft interface information 691 * @bat_priv: the bat priv with all the soft interface information
631 */ 692 */
@@ -657,6 +718,10 @@ int batadv_dat_init(struct batadv_priv *bat_priv)
657 718
658 batadv_dat_start_timer(bat_priv); 719 batadv_dat_start_timer(bat_priv);
659 720
721 batadv_tvlv_handler_register(bat_priv, batadv_dat_tvlv_ogm_handler_v1,
722 NULL, BATADV_TVLV_DAT, 1,
723 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
724 batadv_dat_tvlv_container_update(bat_priv);
660 return 0; 725 return 0;
661} 726}
662 727
@@ -666,6 +731,9 @@ int batadv_dat_init(struct batadv_priv *bat_priv)
666 */ 731 */
667void batadv_dat_free(struct batadv_priv *bat_priv) 732void batadv_dat_free(struct batadv_priv *bat_priv)
668{ 733{
734 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_DAT, 1);
735 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_DAT, 1);
736
669 cancel_delayed_work_sync(&bat_priv->dat.work); 737 cancel_delayed_work_sync(&bat_priv->dat.work);
670 738
671 batadv_dat_hash_free(bat_priv); 739 batadv_dat_hash_free(bat_priv);
@@ -693,8 +761,8 @@ int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
693 goto out; 761 goto out;
694 762
695 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name); 763 seq_printf(seq, "Distributed ARP Table (%s):\n", net_dev->name);
696 seq_printf(seq, " %-7s %-13s %5s\n", "IPv4", "MAC", 764 seq_printf(seq, " %-7s %-9s %4s %11s\n", "IPv4",
697 "last-seen"); 765 "MAC", "VID", "last-seen");
698 766
699 for (i = 0; i < hash->size; i++) { 767 for (i = 0; i < hash->size; i++) {
700 head = &hash->table[i]; 768 head = &hash->table[i];
@@ -707,8 +775,9 @@ int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
707 last_seen_msecs = last_seen_msecs % 60000; 775 last_seen_msecs = last_seen_msecs % 60000;
708 last_seen_secs = last_seen_msecs / 1000; 776 last_seen_secs = last_seen_msecs / 1000;
709 777
710 seq_printf(seq, " * %15pI4 %14pM %6i:%02i\n", 778 seq_printf(seq, " * %15pI4 %14pM %4i %6i:%02i\n",
711 &dat_entry->ip, dat_entry->mac_addr, 779 &dat_entry->ip, dat_entry->mac_addr,
780 BATADV_PRINT_VID(dat_entry->vid),
712 last_seen_mins, last_seen_secs); 781 last_seen_mins, last_seen_secs);
713 } 782 }
714 rcu_read_unlock(); 783 rcu_read_unlock();
@@ -795,6 +864,31 @@ out:
795} 864}
796 865
797/** 866/**
867 * batadv_dat_get_vid - extract the VLAN identifier from skb if any
868 * @skb: the buffer containing the packet to extract the VID from
869 * @hdr_size: the size of the batman-adv header encapsulating the packet
870 *
871 * If the packet embedded in the skb is vlan tagged this function returns the
872 * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
873 */
874static unsigned short batadv_dat_get_vid(struct sk_buff *skb, int *hdr_size)
875{
876 unsigned short vid;
877
878 vid = batadv_get_vid(skb, *hdr_size);
879
880 /* ARP parsing functions jump forward of hdr_size + ETH_HLEN.
881 * If the header contained in the packet is a VLAN one (which is longer)
882 * hdr_size is updated so that the functions will still skip the
883 * correct amount of bytes.
884 */
885 if (vid & BATADV_VLAN_HAS_TAG)
886 *hdr_size += VLAN_HLEN;
887
888 return vid;
889}
890
891/**
798 * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to 892 * batadv_dat_snoop_outgoing_arp_request - snoop the ARP request and try to
799 * answer using DAT 893 * answer using DAT
800 * @bat_priv: the bat priv with all the soft interface information 894 * @bat_priv: the bat priv with all the soft interface information
@@ -813,26 +907,31 @@ bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
813 bool ret = false; 907 bool ret = false;
814 struct batadv_dat_entry *dat_entry = NULL; 908 struct batadv_dat_entry *dat_entry = NULL;
815 struct sk_buff *skb_new; 909 struct sk_buff *skb_new;
910 int hdr_size = 0;
911 unsigned short vid;
816 912
817 if (!atomic_read(&bat_priv->distributed_arp_table)) 913 if (!atomic_read(&bat_priv->distributed_arp_table))
818 goto out; 914 goto out;
819 915
820 type = batadv_arp_get_type(bat_priv, skb, 0); 916 vid = batadv_dat_get_vid(skb, &hdr_size);
917
918 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
821 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast 919 /* If the node gets an ARP_REQUEST it has to send a DHT_GET unicast
822 * message to the selected DHT candidates 920 * message to the selected DHT candidates
823 */ 921 */
824 if (type != ARPOP_REQUEST) 922 if (type != ARPOP_REQUEST)
825 goto out; 923 goto out;
826 924
827 batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REQUEST"); 925 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
926 "Parsing outgoing ARP REQUEST");
828 927
829 ip_src = batadv_arp_ip_src(skb, 0); 928 ip_src = batadv_arp_ip_src(skb, hdr_size);
830 hw_src = batadv_arp_hw_src(skb, 0); 929 hw_src = batadv_arp_hw_src(skb, hdr_size);
831 ip_dst = batadv_arp_ip_dst(skb, 0); 930 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
832 931
833 batadv_dat_entry_add(bat_priv, ip_src, hw_src); 932 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
834 933
835 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst); 934 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
836 if (dat_entry) { 935 if (dat_entry) {
837 /* If the ARP request is destined for a local client the local 936 /* If the ARP request is destined for a local client the local
838 * client will answer itself. DAT would only generate a 937 * client will answer itself. DAT would only generate a
@@ -842,7 +941,8 @@ bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
842 * additional DAT answer may trigger kernel warnings about 941 * additional DAT answer may trigger kernel warnings about
843 * a packet coming from the wrong port. 942 * a packet coming from the wrong port.
844 */ 943 */
845 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr)) { 944 if (batadv_is_my_client(bat_priv, dat_entry->mac_addr,
945 BATADV_NO_FLAGS)) {
846 ret = true; 946 ret = true;
847 goto out; 947 goto out;
848 } 948 }
@@ -853,11 +953,15 @@ bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
853 if (!skb_new) 953 if (!skb_new)
854 goto out; 954 goto out;
855 955
956 if (vid & BATADV_VLAN_HAS_TAG)
957 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
958 vid & VLAN_VID_MASK);
959
856 skb_reset_mac_header(skb_new); 960 skb_reset_mac_header(skb_new);
857 skb_new->protocol = eth_type_trans(skb_new, 961 skb_new->protocol = eth_type_trans(skb_new,
858 bat_priv->soft_iface); 962 bat_priv->soft_iface);
859 bat_priv->stats.rx_packets++; 963 bat_priv->stats.rx_packets++;
860 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN; 964 bat_priv->stats.rx_bytes += skb->len + ETH_HLEN + hdr_size;
861 bat_priv->soft_iface->last_rx = jiffies; 965 bat_priv->soft_iface->last_rx = jiffies;
862 966
863 netif_rx(skb_new); 967 netif_rx(skb_new);
@@ -892,11 +996,14 @@ bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
892 struct sk_buff *skb_new; 996 struct sk_buff *skb_new;
893 struct batadv_dat_entry *dat_entry = NULL; 997 struct batadv_dat_entry *dat_entry = NULL;
894 bool ret = false; 998 bool ret = false;
999 unsigned short vid;
895 int err; 1000 int err;
896 1001
897 if (!atomic_read(&bat_priv->distributed_arp_table)) 1002 if (!atomic_read(&bat_priv->distributed_arp_table))
898 goto out; 1003 goto out;
899 1004
1005 vid = batadv_dat_get_vid(skb, &hdr_size);
1006
900 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 1007 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
901 if (type != ARPOP_REQUEST) 1008 if (type != ARPOP_REQUEST)
902 goto out; 1009 goto out;
@@ -908,9 +1015,9 @@ bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
908 batadv_dbg_arp(bat_priv, skb, type, hdr_size, 1015 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
909 "Parsing incoming ARP REQUEST"); 1016 "Parsing incoming ARP REQUEST");
910 1017
911 batadv_dat_entry_add(bat_priv, ip_src, hw_src); 1018 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
912 1019
913 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst); 1020 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
914 if (!dat_entry) 1021 if (!dat_entry)
915 goto out; 1022 goto out;
916 1023
@@ -921,17 +1028,22 @@ bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
921 if (!skb_new) 1028 if (!skb_new)
922 goto out; 1029 goto out;
923 1030
1031 if (vid & BATADV_VLAN_HAS_TAG)
1032 skb_new = vlan_insert_tag(skb_new, htons(ETH_P_8021Q),
1033 vid & VLAN_VID_MASK);
1034
924 /* To preserve backwards compatibility, the node has choose the outgoing 1035 /* To preserve backwards compatibility, the node has choose the outgoing
925 * format based on the incoming request packet type. The assumption is 1036 * format based on the incoming request packet type. The assumption is
926 * that a node not using the 4addr packet format doesn't support it. 1037 * that a node not using the 4addr packet format doesn't support it.
927 */ 1038 */
928 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet)) 1039 if (hdr_size == sizeof(struct batadv_unicast_4addr_packet))
929 err = batadv_unicast_4addr_send_skb(bat_priv, skb_new, 1040 err = batadv_send_skb_via_tt_4addr(bat_priv, skb_new,
930 BATADV_P_DAT_CACHE_REPLY); 1041 BATADV_P_DAT_CACHE_REPLY,
1042 vid);
931 else 1043 else
932 err = batadv_unicast_send_skb(bat_priv, skb_new); 1044 err = batadv_send_skb_via_tt(bat_priv, skb_new, vid);
933 1045
934 if (!err) { 1046 if (err != NET_XMIT_DROP) {
935 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX); 1047 batadv_inc_counter(bat_priv, BATADV_CNT_DAT_CACHED_REPLY_TX);
936 ret = true; 1048 ret = true;
937 } 1049 }
@@ -954,23 +1066,28 @@ void batadv_dat_snoop_outgoing_arp_reply(struct batadv_priv *bat_priv,
954 uint16_t type; 1066 uint16_t type;
955 __be32 ip_src, ip_dst; 1067 __be32 ip_src, ip_dst;
956 uint8_t *hw_src, *hw_dst; 1068 uint8_t *hw_src, *hw_dst;
1069 int hdr_size = 0;
1070 unsigned short vid;
957 1071
958 if (!atomic_read(&bat_priv->distributed_arp_table)) 1072 if (!atomic_read(&bat_priv->distributed_arp_table))
959 return; 1073 return;
960 1074
961 type = batadv_arp_get_type(bat_priv, skb, 0); 1075 vid = batadv_dat_get_vid(skb, &hdr_size);
1076
1077 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
962 if (type != ARPOP_REPLY) 1078 if (type != ARPOP_REPLY)
963 return; 1079 return;
964 1080
965 batadv_dbg_arp(bat_priv, skb, type, 0, "Parsing outgoing ARP REPLY"); 1081 batadv_dbg_arp(bat_priv, skb, type, hdr_size,
1082 "Parsing outgoing ARP REPLY");
966 1083
967 hw_src = batadv_arp_hw_src(skb, 0); 1084 hw_src = batadv_arp_hw_src(skb, hdr_size);
968 ip_src = batadv_arp_ip_src(skb, 0); 1085 ip_src = batadv_arp_ip_src(skb, hdr_size);
969 hw_dst = batadv_arp_hw_dst(skb, 0); 1086 hw_dst = batadv_arp_hw_dst(skb, hdr_size);
970 ip_dst = batadv_arp_ip_dst(skb, 0); 1087 ip_dst = batadv_arp_ip_dst(skb, hdr_size);
971 1088
972 batadv_dat_entry_add(bat_priv, ip_src, hw_src); 1089 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
973 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst); 1090 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
974 1091
975 /* Send the ARP reply to the candidates for both the IP addresses that 1092 /* Send the ARP reply to the candidates for both the IP addresses that
976 * the node obtained from the ARP reply 1093 * the node obtained from the ARP reply
@@ -992,10 +1109,13 @@ bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
992 __be32 ip_src, ip_dst; 1109 __be32 ip_src, ip_dst;
993 uint8_t *hw_src, *hw_dst; 1110 uint8_t *hw_src, *hw_dst;
994 bool ret = false; 1111 bool ret = false;
1112 unsigned short vid;
995 1113
996 if (!atomic_read(&bat_priv->distributed_arp_table)) 1114 if (!atomic_read(&bat_priv->distributed_arp_table))
997 goto out; 1115 goto out;
998 1116
1117 vid = batadv_dat_get_vid(skb, &hdr_size);
1118
999 type = batadv_arp_get_type(bat_priv, skb, hdr_size); 1119 type = batadv_arp_get_type(bat_priv, skb, hdr_size);
1000 if (type != ARPOP_REPLY) 1120 if (type != ARPOP_REPLY)
1001 goto out; 1121 goto out;
@@ -1011,13 +1131,13 @@ bool batadv_dat_snoop_incoming_arp_reply(struct batadv_priv *bat_priv,
1011 /* Update our internal cache with both the IP addresses the node got 1131 /* Update our internal cache with both the IP addresses the node got
1012 * within the ARP reply 1132 * within the ARP reply
1013 */ 1133 */
1014 batadv_dat_entry_add(bat_priv, ip_src, hw_src); 1134 batadv_dat_entry_add(bat_priv, ip_src, hw_src, vid);
1015 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst); 1135 batadv_dat_entry_add(bat_priv, ip_dst, hw_dst, vid);
1016 1136
1017 /* if this REPLY is directed to a client of mine, let's deliver the 1137 /* if this REPLY is directed to a client of mine, let's deliver the
1018 * packet to the interface 1138 * packet to the interface
1019 */ 1139 */
1020 ret = !batadv_is_my_client(bat_priv, hw_dst); 1140 ret = !batadv_is_my_client(bat_priv, hw_dst, vid);
1021out: 1141out:
1022 if (ret) 1142 if (ret)
1023 kfree_skb(skb); 1143 kfree_skb(skb);
@@ -1040,7 +1160,8 @@ bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1040 __be32 ip_dst; 1160 __be32 ip_dst;
1041 struct batadv_dat_entry *dat_entry = NULL; 1161 struct batadv_dat_entry *dat_entry = NULL;
1042 bool ret = false; 1162 bool ret = false;
1043 const size_t bcast_len = sizeof(struct batadv_bcast_packet); 1163 int hdr_size = sizeof(struct batadv_bcast_packet);
1164 unsigned short vid;
1044 1165
1045 if (!atomic_read(&bat_priv->distributed_arp_table)) 1166 if (!atomic_read(&bat_priv->distributed_arp_table))
1046 goto out; 1167 goto out;
@@ -1051,12 +1172,14 @@ bool batadv_dat_drop_broadcast_packet(struct batadv_priv *bat_priv,
1051 if (forw_packet->num_packets) 1172 if (forw_packet->num_packets)
1052 goto out; 1173 goto out;
1053 1174
1054 type = batadv_arp_get_type(bat_priv, forw_packet->skb, bcast_len); 1175 vid = batadv_dat_get_vid(forw_packet->skb, &hdr_size);
1176
1177 type = batadv_arp_get_type(bat_priv, forw_packet->skb, hdr_size);
1055 if (type != ARPOP_REQUEST) 1178 if (type != ARPOP_REQUEST)
1056 goto out; 1179 goto out;
1057 1180
1058 ip_dst = batadv_arp_ip_dst(forw_packet->skb, bcast_len); 1181 ip_dst = batadv_arp_ip_dst(forw_packet->skb, hdr_size);
1059 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst); 1182 dat_entry = batadv_dat_entry_hash_find(bat_priv, ip_dst, vid);
1060 /* check if the node already got this entry */ 1183 /* check if the node already got this entry */
1061 if (!dat_entry) { 1184 if (!dat_entry) {
1062 batadv_dbg(BATADV_DBG_DAT, bat_priv, 1185 batadv_dbg(BATADV_DBG_DAT, bat_priv,
diff --git a/net/batman-adv/distributed-arp-table.h b/net/batman-adv/distributed-arp-table.h
index 125c8c6fcfad..60d853beb8d8 100644
--- a/net/batman-adv/distributed-arp-table.h
+++ b/net/batman-adv/distributed-arp-table.h
@@ -29,6 +29,7 @@
29 29
30#define BATADV_DAT_ADDR_MAX ((batadv_dat_addr_t)~(batadv_dat_addr_t)0) 30#define BATADV_DAT_ADDR_MAX ((batadv_dat_addr_t)~(batadv_dat_addr_t)0)
31 31
32void batadv_dat_status_update(struct net_device *net_dev);
32bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv, 33bool batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
33 struct sk_buff *skb); 34 struct sk_buff *skb);
34bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv, 35bool batadv_dat_snoop_incoming_arp_request(struct batadv_priv *bat_priv,
@@ -98,6 +99,10 @@ static inline void batadv_dat_inc_counter(struct batadv_priv *bat_priv,
98 99
99#else 100#else
100 101
102static inline void batadv_dat_status_update(struct net_device *net_dev)
103{
104}
105
101static inline bool 106static inline bool
102batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv, 107batadv_dat_snoop_outgoing_arp_request(struct batadv_priv *bat_priv,
103 struct sk_buff *skb) 108 struct sk_buff *skb)
diff --git a/net/batman-adv/fragmentation.c b/net/batman-adv/fragmentation.c
new file mode 100644
index 000000000000..6ddb6145ffb5
--- /dev/null
+++ b/net/batman-adv/fragmentation.c
@@ -0,0 +1,491 @@
1/* Copyright (C) 2013 B.A.T.M.A.N. contributors:
2 *
3 * Martin Hundebøll <martin@hundeboll.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#include "main.h"
21#include "fragmentation.h"
22#include "send.h"
23#include "originator.h"
24#include "routing.h"
25#include "hard-interface.h"
26#include "soft-interface.h"
27
28
29/**
30 * batadv_frag_clear_chain - delete entries in the fragment buffer chain
31 * @head: head of chain with entries.
32 *
33 * Free fragments in the passed hlist. Should be called with appropriate lock.
34 */
35static void batadv_frag_clear_chain(struct hlist_head *head)
36{
37 struct batadv_frag_list_entry *entry;
38 struct hlist_node *node;
39
40 hlist_for_each_entry_safe(entry, node, head, list) {
41 hlist_del(&entry->list);
42 kfree_skb(entry->skb);
43 kfree(entry);
44 }
45}
46
47/**
48 * batadv_frag_purge_orig - free fragments associated to an orig
49 * @orig_node: originator to free fragments from
50 * @check_cb: optional function to tell if an entry should be purged
51 */
52void batadv_frag_purge_orig(struct batadv_orig_node *orig_node,
53 bool (*check_cb)(struct batadv_frag_table_entry *))
54{
55 struct batadv_frag_table_entry *chain;
56 uint8_t i;
57
58 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
59 chain = &orig_node->fragments[i];
60 spin_lock_bh(&orig_node->fragments[i].lock);
61
62 if (!check_cb || check_cb(chain)) {
63 batadv_frag_clear_chain(&orig_node->fragments[i].head);
64 orig_node->fragments[i].size = 0;
65 }
66
67 spin_unlock_bh(&orig_node->fragments[i].lock);
68 }
69}
70
71/**
72 * batadv_frag_size_limit - maximum possible size of packet to be fragmented
73 *
74 * Returns the maximum size of payload that can be fragmented.
75 */
76static int batadv_frag_size_limit(void)
77{
78 int limit = BATADV_FRAG_MAX_FRAG_SIZE;
79
80 limit -= sizeof(struct batadv_frag_packet);
81 limit *= BATADV_FRAG_MAX_FRAGMENTS;
82
83 return limit;
84}
85
86/**
87 * batadv_frag_init_chain - check and prepare fragment chain for new fragment
88 * @chain: chain in fragments table to init
89 * @seqno: sequence number of the received fragment
90 *
91 * Make chain ready for a fragment with sequence number "seqno". Delete existing
92 * entries if they have an "old" sequence number.
93 *
94 * Caller must hold chain->lock.
95 *
96 * Returns true if chain is empty and caller can just insert the new fragment
97 * without searching for the right position.
98 */
99static bool batadv_frag_init_chain(struct batadv_frag_table_entry *chain,
100 uint16_t seqno)
101{
102 if (chain->seqno == seqno)
103 return false;
104
105 if (!hlist_empty(&chain->head))
106 batadv_frag_clear_chain(&chain->head);
107
108 chain->size = 0;
109 chain->seqno = seqno;
110
111 return true;
112}
113
114/**
115 * batadv_frag_insert_packet - insert a fragment into a fragment chain
116 * @orig_node: originator that the fragment was received from
117 * @skb: skb to insert
118 * @chain_out: list head to attach complete chains of fragments to
119 *
120 * Insert a new fragment into the reverse ordered chain in the right table
121 * entry. The hash table entry is cleared if "old" fragments exist in it.
122 *
123 * Returns true if skb is buffered, false on error. If the chain has all the
124 * fragments needed to merge the packet, the chain is moved to the passed head
125 * to avoid locking the chain in the table.
126 */
127static bool batadv_frag_insert_packet(struct batadv_orig_node *orig_node,
128 struct sk_buff *skb,
129 struct hlist_head *chain_out)
130{
131 struct batadv_frag_table_entry *chain;
132 struct batadv_frag_list_entry *frag_entry_new = NULL, *frag_entry_curr;
133 struct batadv_frag_packet *frag_packet;
134 uint8_t bucket;
135 uint16_t seqno, hdr_size = sizeof(struct batadv_frag_packet);
136 bool ret = false;
137
138 /* Linearize packet to avoid linearizing 16 packets in a row when doing
139 * the later merge. Non-linear merge should be added to remove this
140 * linearization.
141 */
142 if (skb_linearize(skb) < 0)
143 goto err;
144
145 frag_packet = (struct batadv_frag_packet *)skb->data;
146 seqno = ntohs(frag_packet->seqno);
147 bucket = seqno % BATADV_FRAG_BUFFER_COUNT;
148
149 frag_entry_new = kmalloc(sizeof(*frag_entry_new), GFP_ATOMIC);
150 if (!frag_entry_new)
151 goto err;
152
153 frag_entry_new->skb = skb;
154 frag_entry_new->no = frag_packet->no;
155
156 /* Select entry in the "chain table" and delete any prior fragments
157 * with another sequence number. batadv_frag_init_chain() returns true,
158 * if the list is empty at return.
159 */
160 chain = &orig_node->fragments[bucket];
161 spin_lock_bh(&chain->lock);
162 if (batadv_frag_init_chain(chain, seqno)) {
163 hlist_add_head(&frag_entry_new->list, &chain->head);
164 chain->size = skb->len - hdr_size;
165 chain->timestamp = jiffies;
166 ret = true;
167 goto out;
168 }
169
170 /* Find the position for the new fragment. */
171 hlist_for_each_entry(frag_entry_curr, &chain->head, list) {
172 /* Drop packet if fragment already exists. */
173 if (frag_entry_curr->no == frag_entry_new->no)
174 goto err_unlock;
175
176 /* Order fragments from highest to lowest. */
177 if (frag_entry_curr->no < frag_entry_new->no) {
178 hlist_add_before(&frag_entry_new->list,
179 &frag_entry_curr->list);
180 chain->size += skb->len - hdr_size;
181 chain->timestamp = jiffies;
182 ret = true;
183 goto out;
184 }
185 }
186
187 /* Reached the end of the list, so insert after 'frag_entry_curr'. */
188 if (likely(frag_entry_curr)) {
189 hlist_add_after(&frag_entry_curr->list, &frag_entry_new->list);
190 chain->size += skb->len - hdr_size;
191 chain->timestamp = jiffies;
192 ret = true;
193 }
194
195out:
196 if (chain->size > batadv_frag_size_limit() ||
197 ntohs(frag_packet->total_size) > batadv_frag_size_limit()) {
198 /* Clear chain if total size of either the list or the packet
199 * exceeds the maximum size of one merged packet.
200 */
201 batadv_frag_clear_chain(&chain->head);
202 chain->size = 0;
203 } else if (ntohs(frag_packet->total_size) == chain->size) {
204 /* All fragments received. Hand over chain to caller. */
205 hlist_move_list(&chain->head, chain_out);
206 chain->size = 0;
207 }
208
209err_unlock:
210 spin_unlock_bh(&chain->lock);
211
212err:
213 if (!ret)
214 kfree(frag_entry_new);
215
216 return ret;
217}
218
219/**
220 * batadv_frag_merge_packets - merge a chain of fragments
221 * @chain: head of chain with fragments
222 * @skb: packet with total size of skb after merging
223 *
224 * Expand the first skb in the chain and copy the content of the remaining
225 * skb's into the expanded one. After doing so, clear the chain.
226 *
227 * Returns the merged skb or NULL on error.
228 */
229static struct sk_buff *
230batadv_frag_merge_packets(struct hlist_head *chain, struct sk_buff *skb)
231{
232 struct batadv_frag_packet *packet;
233 struct batadv_frag_list_entry *entry;
234 struct sk_buff *skb_out = NULL;
235 int size, hdr_size = sizeof(struct batadv_frag_packet);
236
237 /* Make sure incoming skb has non-bogus data. */
238 packet = (struct batadv_frag_packet *)skb->data;
239 size = ntohs(packet->total_size);
240 if (size > batadv_frag_size_limit())
241 goto free;
242
243 /* Remove first entry, as this is the destination for the rest of the
244 * fragments.
245 */
246 entry = hlist_entry(chain->first, struct batadv_frag_list_entry, list);
247 hlist_del(&entry->list);
248 skb_out = entry->skb;
249 kfree(entry);
250
251 /* Make room for the rest of the fragments. */
252 if (pskb_expand_head(skb_out, 0, size - skb->len, GFP_ATOMIC) < 0) {
253 kfree_skb(skb_out);
254 skb_out = NULL;
255 goto free;
256 }
257
258 /* Move the existing MAC header to just before the payload. (Override
259 * the fragment header.)
260 */
261 skb_pull_rcsum(skb_out, hdr_size);
262 memmove(skb_out->data - ETH_HLEN, skb_mac_header(skb_out), ETH_HLEN);
263 skb_set_mac_header(skb_out, -ETH_HLEN);
264 skb_reset_network_header(skb_out);
265 skb_reset_transport_header(skb_out);
266
267 /* Copy the payload of the each fragment into the last skb */
268 hlist_for_each_entry(entry, chain, list) {
269 size = entry->skb->len - hdr_size;
270 memcpy(skb_put(skb_out, size), entry->skb->data + hdr_size,
271 size);
272 }
273
274free:
275 /* Locking is not needed, because 'chain' is not part of any orig. */
276 batadv_frag_clear_chain(chain);
277 return skb_out;
278}
279
280/**
281 * batadv_frag_skb_buffer - buffer fragment for later merge
282 * @skb: skb to buffer
283 * @orig_node_src: originator that the skb is received from
284 *
285 * Add fragment to buffer and merge fragments if possible.
286 *
287 * There are three possible outcomes: 1) Packet is merged: Return true and
288 * set *skb to merged packet; 2) Packet is buffered: Return true and set *skb
289 * to NULL; 3) Error: Return false and leave skb as is.
290 */
291bool batadv_frag_skb_buffer(struct sk_buff **skb,
292 struct batadv_orig_node *orig_node_src)
293{
294 struct sk_buff *skb_out = NULL;
295 struct hlist_head head = HLIST_HEAD_INIT;
296 bool ret = false;
297
298 /* Add packet to buffer and table entry if merge is possible. */
299 if (!batadv_frag_insert_packet(orig_node_src, *skb, &head))
300 goto out_err;
301
302 /* Leave if more fragments are needed to merge. */
303 if (hlist_empty(&head))
304 goto out;
305
306 skb_out = batadv_frag_merge_packets(&head, *skb);
307 if (!skb_out)
308 goto out_err;
309
310out:
311 *skb = skb_out;
312 ret = true;
313out_err:
314 return ret;
315}
316
317/**
318 * batadv_frag_skb_fwd - forward fragments that would exceed MTU when merged
319 * @skb: skb to forward
320 * @recv_if: interface that the skb is received on
321 * @orig_node_src: originator that the skb is received from
322 *
323 * Look up the next-hop of the fragments payload and check if the merged packet
324 * will exceed the MTU towards the next-hop. If so, the fragment is forwarded
325 * without merging it.
326 *
327 * Returns true if the fragment is consumed/forwarded, false otherwise.
328 */
329bool batadv_frag_skb_fwd(struct sk_buff *skb,
330 struct batadv_hard_iface *recv_if,
331 struct batadv_orig_node *orig_node_src)
332{
333 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
334 struct batadv_orig_node *orig_node_dst = NULL;
335 struct batadv_neigh_node *neigh_node = NULL;
336 struct batadv_frag_packet *packet;
337 uint16_t total_size;
338 bool ret = false;
339
340 packet = (struct batadv_frag_packet *)skb->data;
341 orig_node_dst = batadv_orig_hash_find(bat_priv, packet->dest);
342 if (!orig_node_dst)
343 goto out;
344
345 neigh_node = batadv_find_router(bat_priv, orig_node_dst, recv_if);
346 if (!neigh_node)
347 goto out;
348
349 /* Forward the fragment, if the merged packet would be too big to
350 * be assembled.
351 */
352 total_size = ntohs(packet->total_size);
353 if (total_size > neigh_node->if_incoming->net_dev->mtu) {
354 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_FWD);
355 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_FWD_BYTES,
356 skb->len + ETH_HLEN);
357
358 packet->ttl--;
359 batadv_send_skb_packet(skb, neigh_node->if_incoming,
360 neigh_node->addr);
361 ret = true;
362 }
363
364out:
365 if (orig_node_dst)
366 batadv_orig_node_free_ref(orig_node_dst);
367 if (neigh_node)
368 batadv_neigh_node_free_ref(neigh_node);
369 return ret;
370}
371
372/**
373 * batadv_frag_create - create a fragment from skb
374 * @skb: skb to create fragment from
375 * @frag_head: header to use in new fragment
376 * @mtu: size of new fragment
377 *
378 * Split the passed skb into two fragments: A new one with size matching the
379 * passed mtu and the old one with the rest. The new skb contains data from the
380 * tail of the old skb.
381 *
382 * Returns the new fragment, NULL on error.
383 */
384static struct sk_buff *batadv_frag_create(struct sk_buff *skb,
385 struct batadv_frag_packet *frag_head,
386 unsigned int mtu)
387{
388 struct sk_buff *skb_fragment;
389 unsigned header_size = sizeof(*frag_head);
390 unsigned fragment_size = mtu - header_size;
391
392 skb_fragment = netdev_alloc_skb(NULL, mtu + ETH_HLEN);
393 if (!skb_fragment)
394 goto err;
395
396 skb->priority = TC_PRIO_CONTROL;
397
398 /* Eat the last mtu-bytes of the skb */
399 skb_reserve(skb_fragment, header_size + ETH_HLEN);
400 skb_split(skb, skb_fragment, skb->len - fragment_size);
401
402 /* Add the header */
403 skb_push(skb_fragment, header_size);
404 memcpy(skb_fragment->data, frag_head, header_size);
405
406err:
407 return skb_fragment;
408}
409
410/**
411 * batadv_frag_send_packet - create up to 16 fragments from the passed skb
412 * @skb: skb to create fragments from
413 * @orig_node: final destination of the created fragments
414 * @neigh_node: next-hop of the created fragments
415 *
416 * Returns true on success, false otherwise.
417 */
418bool batadv_frag_send_packet(struct sk_buff *skb,
419 struct batadv_orig_node *orig_node,
420 struct batadv_neigh_node *neigh_node)
421{
422 struct batadv_priv *bat_priv;
423 struct batadv_hard_iface *primary_if;
424 struct batadv_frag_packet frag_header;
425 struct sk_buff *skb_fragment;
426 unsigned mtu = neigh_node->if_incoming->net_dev->mtu;
427 unsigned header_size = sizeof(frag_header);
428 unsigned max_fragment_size, max_packet_size;
429
430 /* To avoid merge and refragmentation at next-hops we never send
431 * fragments larger than BATADV_FRAG_MAX_FRAG_SIZE
432 */
433 mtu = min_t(unsigned, mtu, BATADV_FRAG_MAX_FRAG_SIZE);
434 max_fragment_size = (mtu - header_size - ETH_HLEN);
435 max_packet_size = max_fragment_size * BATADV_FRAG_MAX_FRAGMENTS;
436
437 /* Don't even try to fragment, if we need more than 16 fragments */
438 if (skb->len > max_packet_size)
439 goto out_err;
440
441 bat_priv = orig_node->bat_priv;
442 primary_if = batadv_primary_if_get_selected(bat_priv);
443 if (!primary_if)
444 goto out_err;
445
446 /* Create one header to be copied to all fragments */
447 frag_header.packet_type = BATADV_UNICAST_FRAG;
448 frag_header.version = BATADV_COMPAT_VERSION;
449 frag_header.ttl = BATADV_TTL;
450 frag_header.seqno = htons(atomic_inc_return(&bat_priv->frag_seqno));
451 frag_header.reserved = 0;
452 frag_header.no = 0;
453 frag_header.total_size = htons(skb->len);
454 memcpy(frag_header.orig, primary_if->net_dev->dev_addr, ETH_ALEN);
455 memcpy(frag_header.dest, orig_node->orig, ETH_ALEN);
456
457 /* Eat and send fragments from the tail of skb */
458 while (skb->len > max_fragment_size) {
459 skb_fragment = batadv_frag_create(skb, &frag_header, mtu);
460 if (!skb_fragment)
461 goto out_err;
462
463 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
464 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
465 skb_fragment->len + ETH_HLEN);
466 batadv_send_skb_packet(skb_fragment, neigh_node->if_incoming,
467 neigh_node->addr);
468 frag_header.no++;
469
470 /* The initial check in this function should cover this case */
471 if (frag_header.no == BATADV_FRAG_MAX_FRAGMENTS - 1)
472 goto out_err;
473 }
474
475 /* Make room for the fragment header. */
476 if (batadv_skb_head_push(skb, header_size) < 0 ||
477 pskb_expand_head(skb, header_size + ETH_HLEN, 0, GFP_ATOMIC) < 0)
478 goto out_err;
479
480 memcpy(skb->data, &frag_header, header_size);
481
482 /* Send the last fragment */
483 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_TX);
484 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_TX_BYTES,
485 skb->len + ETH_HLEN);
486 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
487
488 return true;
489out_err:
490 return false;
491}
diff --git a/net/batman-adv/fragmentation.h b/net/batman-adv/fragmentation.h
new file mode 100644
index 000000000000..ca029e2676e7
--- /dev/null
+++ b/net/batman-adv/fragmentation.h
@@ -0,0 +1,50 @@
1/* Copyright (C) 2013 B.A.T.M.A.N. contributors:
2 *
3 * Martin Hundebøll <martin@hundeboll.net>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#ifndef _NET_BATMAN_ADV_FRAGMENTATION_H_
21#define _NET_BATMAN_ADV_FRAGMENTATION_H_
22
23void batadv_frag_purge_orig(struct batadv_orig_node *orig,
24 bool (*check_cb)(struct batadv_frag_table_entry *));
25bool batadv_frag_skb_fwd(struct sk_buff *skb,
26 struct batadv_hard_iface *recv_if,
27 struct batadv_orig_node *orig_node_src);
28bool batadv_frag_skb_buffer(struct sk_buff **skb,
29 struct batadv_orig_node *orig_node);
30bool batadv_frag_send_packet(struct sk_buff *skb,
31 struct batadv_orig_node *orig_node,
32 struct batadv_neigh_node *neigh_node);
33
34/**
35 * batadv_frag_check_entry - check if a list of fragments has timed out
36 * @frags_entry: table entry to check
37 *
38 * Returns true if the frags entry has timed out, false otherwise.
39 */
40static inline bool
41batadv_frag_check_entry(struct batadv_frag_table_entry *frags_entry)
42{
43 if (!hlist_empty(&frags_entry->head) &&
44 batadv_has_timed_out(frags_entry->timestamp, BATADV_FRAG_TIMEOUT))
45 return true;
46 else
47 return false;
48}
49
50#endif /* _NET_BATMAN_ADV_FRAGMENTATION_H_ */
diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index 1ce4b8763ef2..2449afaa7638 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -118,7 +118,6 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
118 uint32_t max_gw_factor = 0, tmp_gw_factor = 0; 118 uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
119 uint32_t gw_divisor; 119 uint32_t gw_divisor;
120 uint8_t max_tq = 0; 120 uint8_t max_tq = 0;
121 int down, up;
122 uint8_t tq_avg; 121 uint8_t tq_avg;
123 struct batadv_orig_node *orig_node; 122 struct batadv_orig_node *orig_node;
124 123
@@ -138,14 +137,13 @@ batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
138 if (!atomic_inc_not_zero(&gw_node->refcount)) 137 if (!atomic_inc_not_zero(&gw_node->refcount))
139 goto next; 138 goto next;
140 139
141 tq_avg = router->tq_avg; 140 tq_avg = router->bat_iv.tq_avg;
142 141
143 switch (atomic_read(&bat_priv->gw_sel_class)) { 142 switch (atomic_read(&bat_priv->gw_sel_class)) {
144 case 1: /* fast connection */ 143 case 1: /* fast connection */
145 batadv_gw_bandwidth_to_kbit(orig_node->gw_flags, 144 tmp_gw_factor = tq_avg * tq_avg;
146 &down, &up); 145 tmp_gw_factor *= gw_node->bandwidth_down;
147 146 tmp_gw_factor *= 100 * 100;
148 tmp_gw_factor = tq_avg * tq_avg * down * 100 * 100;
149 tmp_gw_factor /= gw_divisor; 147 tmp_gw_factor /= gw_divisor;
150 148
151 if ((tmp_gw_factor > max_gw_factor) || 149 if ((tmp_gw_factor > max_gw_factor) ||
@@ -223,11 +221,6 @@ void batadv_gw_election(struct batadv_priv *bat_priv)
223 struct batadv_neigh_node *router = NULL; 221 struct batadv_neigh_node *router = NULL;
224 char gw_addr[18] = { '\0' }; 222 char gw_addr[18] = { '\0' };
225 223
226 /* The batman daemon checks here if we already passed a full originator
227 * cycle in order to make sure we don't choose the first gateway we
228 * hear about. This check is based on the daemon's uptime which we
229 * don't have.
230 */
231 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT) 224 if (atomic_read(&bat_priv->gw_mode) != BATADV_GW_MODE_CLIENT)
232 goto out; 225 goto out;
233 226
@@ -258,16 +251,22 @@ void batadv_gw_election(struct batadv_priv *bat_priv)
258 NULL); 251 NULL);
259 } else if ((!curr_gw) && (next_gw)) { 252 } else if ((!curr_gw) && (next_gw)) {
260 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 253 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
261 "Adding route to gateway %pM (gw_flags: %i, tq: %i)\n", 254 "Adding route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
262 next_gw->orig_node->orig, 255 next_gw->orig_node->orig,
263 next_gw->orig_node->gw_flags, router->tq_avg); 256 next_gw->bandwidth_down / 10,
257 next_gw->bandwidth_down % 10,
258 next_gw->bandwidth_up / 10,
259 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
264 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD, 260 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_ADD,
265 gw_addr); 261 gw_addr);
266 } else { 262 } else {
267 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 263 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
268 "Changing route to gateway %pM (gw_flags: %i, tq: %i)\n", 264 "Changing route to gateway %pM (bandwidth: %u.%u/%u.%u MBit, tq: %i)\n",
269 next_gw->orig_node->orig, 265 next_gw->orig_node->orig,
270 next_gw->orig_node->gw_flags, router->tq_avg); 266 next_gw->bandwidth_down / 10,
267 next_gw->bandwidth_down % 10,
268 next_gw->bandwidth_up / 10,
269 next_gw->bandwidth_up % 10, router->bat_iv.tq_avg);
271 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE, 270 batadv_throw_uevent(bat_priv, BATADV_UEV_GW, BATADV_UEV_CHANGE,
272 gw_addr); 271 gw_addr);
273 } 272 }
@@ -306,8 +305,8 @@ void batadv_gw_check_election(struct batadv_priv *bat_priv,
306 if (!router_orig) 305 if (!router_orig)
307 goto out; 306 goto out;
308 307
309 gw_tq_avg = router_gw->tq_avg; 308 gw_tq_avg = router_gw->bat_iv.tq_avg;
310 orig_tq_avg = router_orig->tq_avg; 309 orig_tq_avg = router_orig->bat_iv.tq_avg;
311 310
312 /* the TQ value has to be better */ 311 /* the TQ value has to be better */
313 if (orig_tq_avg < gw_tq_avg) 312 if (orig_tq_avg < gw_tq_avg)
@@ -337,12 +336,20 @@ out:
337 return; 336 return;
338} 337}
339 338
339/**
340 * batadv_gw_node_add - add gateway node to list of available gateways
341 * @bat_priv: the bat priv with all the soft interface information
342 * @orig_node: originator announcing gateway capabilities
343 * @gateway: announced bandwidth information
344 */
340static void batadv_gw_node_add(struct batadv_priv *bat_priv, 345static void batadv_gw_node_add(struct batadv_priv *bat_priv,
341 struct batadv_orig_node *orig_node, 346 struct batadv_orig_node *orig_node,
342 uint8_t new_gwflags) 347 struct batadv_tvlv_gateway_data *gateway)
343{ 348{
344 struct batadv_gw_node *gw_node; 349 struct batadv_gw_node *gw_node;
345 int down, up; 350
351 if (gateway->bandwidth_down == 0)
352 return;
346 353
347 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC); 354 gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
348 if (!gw_node) 355 if (!gw_node)
@@ -356,73 +363,116 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
356 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list); 363 hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
357 spin_unlock_bh(&bat_priv->gw.list_lock); 364 spin_unlock_bh(&bat_priv->gw.list_lock);
358 365
359 batadv_gw_bandwidth_to_kbit(new_gwflags, &down, &up);
360 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 366 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
361 "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n", 367 "Found new gateway %pM -> gw bandwidth: %u.%u/%u.%u MBit\n",
362 orig_node->orig, new_gwflags, 368 orig_node->orig,
363 (down > 2048 ? down / 1024 : down), 369 ntohl(gateway->bandwidth_down) / 10,
364 (down > 2048 ? "MBit" : "KBit"), 370 ntohl(gateway->bandwidth_down) % 10,
365 (up > 2048 ? up / 1024 : up), 371 ntohl(gateway->bandwidth_up) / 10,
366 (up > 2048 ? "MBit" : "KBit")); 372 ntohl(gateway->bandwidth_up) % 10);
367} 373}
368 374
369void batadv_gw_node_update(struct batadv_priv *bat_priv, 375/**
370 struct batadv_orig_node *orig_node, 376 * batadv_gw_node_get - retrieve gateway node from list of available gateways
371 uint8_t new_gwflags) 377 * @bat_priv: the bat priv with all the soft interface information
378 * @orig_node: originator announcing gateway capabilities
379 *
380 * Returns gateway node if found or NULL otherwise.
381 */
382static struct batadv_gw_node *
383batadv_gw_node_get(struct batadv_priv *bat_priv,
384 struct batadv_orig_node *orig_node)
372{ 385{
373 struct batadv_gw_node *gw_node, *curr_gw; 386 struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
374
375 /* Note: We don't need a NULL check here, since curr_gw never gets
376 * dereferenced. If curr_gw is NULL we also should not exit as we may
377 * have this gateway in our list (duplication check!) even though we
378 * have no currently selected gateway.
379 */
380 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
381 387
382 rcu_read_lock(); 388 rcu_read_lock();
383 hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) { 389 hlist_for_each_entry_rcu(gw_node_tmp, &bat_priv->gw.list, list) {
384 if (gw_node->orig_node != orig_node) 390 if (gw_node_tmp->orig_node != orig_node)
385 continue; 391 continue;
386 392
387 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 393 if (gw_node_tmp->deleted)
388 "Gateway class of originator %pM changed from %i to %i\n", 394 continue;
389 orig_node->orig, gw_node->orig_node->gw_flags,
390 new_gwflags);
391 395
392 gw_node->deleted = 0; 396 if (!atomic_inc_not_zero(&gw_node_tmp->refcount))
397 continue;
393 398
394 if (new_gwflags == BATADV_NO_FLAGS) { 399 gw_node = gw_node_tmp;
395 gw_node->deleted = jiffies; 400 break;
396 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 401 }
397 "Gateway %pM removed from gateway list\n", 402 rcu_read_unlock();
398 orig_node->orig);
399 403
400 if (gw_node == curr_gw) 404 return gw_node;
401 goto deselect; 405}
402 }
403 406
404 goto unlock; 407/**
408 * batadv_gw_node_update - update list of available gateways with changed
409 * bandwidth information
410 * @bat_priv: the bat priv with all the soft interface information
411 * @orig_node: originator announcing gateway capabilities
412 * @gateway: announced bandwidth information
413 */
414void batadv_gw_node_update(struct batadv_priv *bat_priv,
415 struct batadv_orig_node *orig_node,
416 struct batadv_tvlv_gateway_data *gateway)
417{
418 struct batadv_gw_node *gw_node, *curr_gw = NULL;
419
420 gw_node = batadv_gw_node_get(bat_priv, orig_node);
421 if (!gw_node) {
422 batadv_gw_node_add(bat_priv, orig_node, gateway);
423 goto out;
405 } 424 }
406 425
407 if (new_gwflags == BATADV_NO_FLAGS) 426 if ((gw_node->bandwidth_down == ntohl(gateway->bandwidth_down)) &&
408 goto unlock; 427 (gw_node->bandwidth_up == ntohl(gateway->bandwidth_up)))
428 goto out;
409 429
410 batadv_gw_node_add(bat_priv, orig_node, new_gwflags); 430 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
411 goto unlock; 431 "Gateway bandwidth of originator %pM changed from %u.%u/%u.%u MBit to %u.%u/%u.%u MBit\n",
432 orig_node->orig,
433 gw_node->bandwidth_down / 10,
434 gw_node->bandwidth_down % 10,
435 gw_node->bandwidth_up / 10,
436 gw_node->bandwidth_up % 10,
437 ntohl(gateway->bandwidth_down) / 10,
438 ntohl(gateway->bandwidth_down) % 10,
439 ntohl(gateway->bandwidth_up) / 10,
440 ntohl(gateway->bandwidth_up) % 10);
441
442 gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
443 gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
444
445 gw_node->deleted = 0;
446 if (ntohl(gateway->bandwidth_down) == 0) {
447 gw_node->deleted = jiffies;
448 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
449 "Gateway %pM removed from gateway list\n",
450 orig_node->orig);
412 451
413deselect: 452 /* Note: We don't need a NULL check here, since curr_gw never
414 batadv_gw_deselect(bat_priv); 453 * gets dereferenced.
415unlock: 454 */
416 rcu_read_unlock(); 455 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
456 if (gw_node == curr_gw)
457 batadv_gw_deselect(bat_priv);
458 }
417 459
460out:
418 if (curr_gw) 461 if (curr_gw)
419 batadv_gw_node_free_ref(curr_gw); 462 batadv_gw_node_free_ref(curr_gw);
463 if (gw_node)
464 batadv_gw_node_free_ref(gw_node);
420} 465}
421 466
422void batadv_gw_node_delete(struct batadv_priv *bat_priv, 467void batadv_gw_node_delete(struct batadv_priv *bat_priv,
423 struct batadv_orig_node *orig_node) 468 struct batadv_orig_node *orig_node)
424{ 469{
425 batadv_gw_node_update(bat_priv, orig_node, 0); 470 struct batadv_tvlv_gateway_data gateway;
471
472 gateway.bandwidth_down = 0;
473 gateway.bandwidth_up = 0;
474
475 batadv_gw_node_update(bat_priv, orig_node, &gateway);
426} 476}
427 477
428void batadv_gw_node_purge(struct batadv_priv *bat_priv) 478void batadv_gw_node_purge(struct batadv_priv *bat_priv)
@@ -467,9 +517,7 @@ static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
467{ 517{
468 struct batadv_gw_node *curr_gw; 518 struct batadv_gw_node *curr_gw;
469 struct batadv_neigh_node *router; 519 struct batadv_neigh_node *router;
470 int down, up, ret = -1; 520 int ret = -1;
471
472 batadv_gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
473 521
474 router = batadv_orig_node_get_router(gw_node->orig_node); 522 router = batadv_orig_node_get_router(gw_node->orig_node);
475 if (!router) 523 if (!router)
@@ -477,16 +525,15 @@ static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
477 525
478 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 526 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
479 527
480 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n", 528 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
481 (curr_gw == gw_node ? "=>" : " "), 529 (curr_gw == gw_node ? "=>" : " "),
482 gw_node->orig_node->orig, 530 gw_node->orig_node->orig,
483 router->tq_avg, router->addr, 531 router->bat_iv.tq_avg, router->addr,
484 router->if_incoming->net_dev->name, 532 router->if_incoming->net_dev->name,
485 gw_node->orig_node->gw_flags, 533 gw_node->bandwidth_down / 10,
486 (down > 2048 ? down / 1024 : down), 534 gw_node->bandwidth_down % 10,
487 (down > 2048 ? "MBit" : "KBit"), 535 gw_node->bandwidth_up / 10,
488 (up > 2048 ? up / 1024 : up), 536 gw_node->bandwidth_up % 10);
489 (up > 2048 ? "MBit" : "KBit"));
490 537
491 batadv_neigh_node_free_ref(router); 538 batadv_neigh_node_free_ref(router);
492 if (curr_gw) 539 if (curr_gw)
@@ -508,7 +555,7 @@ int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
508 goto out; 555 goto out;
509 556
510 seq_printf(seq, 557 seq_printf(seq,
511 " %-12s (%s/%i) %17s [%10s]: gw_class ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 558 " %-12s (%s/%i) %17s [%10s]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
512 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF", 559 "Gateway", "#", BATADV_TQ_MAX_VALUE, "Nexthop", "outgoingIF",
513 BATADV_SOURCE_VERSION, primary_if->net_dev->name, 560 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
514 primary_if->net_dev->dev_addr, net_dev->name); 561 primary_if->net_dev->dev_addr, net_dev->name);
@@ -603,24 +650,29 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
603 struct iphdr *iphdr; 650 struct iphdr *iphdr;
604 struct ipv6hdr *ipv6hdr; 651 struct ipv6hdr *ipv6hdr;
605 struct udphdr *udphdr; 652 struct udphdr *udphdr;
653 struct vlan_ethhdr *vhdr;
654 __be16 proto;
606 655
607 /* check for ethernet header */ 656 /* check for ethernet header */
608 if (!pskb_may_pull(skb, *header_len + ETH_HLEN)) 657 if (!pskb_may_pull(skb, *header_len + ETH_HLEN))
609 return false; 658 return false;
610 ethhdr = (struct ethhdr *)skb->data; 659 ethhdr = (struct ethhdr *)skb->data;
660 proto = ethhdr->h_proto;
611 *header_len += ETH_HLEN; 661 *header_len += ETH_HLEN;
612 662
613 /* check for initial vlan header */ 663 /* check for initial vlan header */
614 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) { 664 if (proto == htons(ETH_P_8021Q)) {
615 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN)) 665 if (!pskb_may_pull(skb, *header_len + VLAN_HLEN))
616 return false; 666 return false;
617 ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN); 667
668 vhdr = (struct vlan_ethhdr *)skb->data;
669 proto = vhdr->h_vlan_encapsulated_proto;
618 *header_len += VLAN_HLEN; 670 *header_len += VLAN_HLEN;
619 } 671 }
620 672
621 /* check for ip header */ 673 /* check for ip header */
622 switch (ntohs(ethhdr->h_proto)) { 674 switch (proto) {
623 case ETH_P_IP: 675 case htons(ETH_P_IP):
624 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr))) 676 if (!pskb_may_pull(skb, *header_len + sizeof(*iphdr)))
625 return false; 677 return false;
626 iphdr = (struct iphdr *)(skb->data + *header_len); 678 iphdr = (struct iphdr *)(skb->data + *header_len);
@@ -631,7 +683,7 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
631 return false; 683 return false;
632 684
633 break; 685 break;
634 case ETH_P_IPV6: 686 case htons(ETH_P_IPV6):
635 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr))) 687 if (!pskb_may_pull(skb, *header_len + sizeof(*ipv6hdr)))
636 return false; 688 return false;
637 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len); 689 ipv6hdr = (struct ipv6hdr *)(skb->data + *header_len);
@@ -658,28 +710,44 @@ bool batadv_gw_is_dhcp_target(struct sk_buff *skb, unsigned int *header_len)
658 *header_len += sizeof(*udphdr); 710 *header_len += sizeof(*udphdr);
659 711
660 /* check for bootp port */ 712 /* check for bootp port */
661 if ((ntohs(ethhdr->h_proto) == ETH_P_IP) && 713 if ((proto == htons(ETH_P_IP)) &&
662 (ntohs(udphdr->dest) != 67)) 714 (udphdr->dest != htons(67)))
663 return false; 715 return false;
664 716
665 if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) && 717 if ((proto == htons(ETH_P_IPV6)) &&
666 (ntohs(udphdr->dest) != 547)) 718 (udphdr->dest != htons(547)))
667 return false; 719 return false;
668 720
669 return true; 721 return true;
670} 722}
671 723
672/* this call might reallocate skb data */ 724/**
725 * batadv_gw_out_of_range - check if the dhcp request destination is the best gw
726 * @bat_priv: the bat priv with all the soft interface information
727 * @skb: the outgoing packet
728 *
729 * Check if the skb is a DHCP request and if it is sent to the current best GW
730 * server. Due to topology changes it may be the case that the GW server
731 * previously selected is not the best one anymore.
732 *
733 * Returns true if the packet destination is unicast and it is not the best gw,
734 * false otherwise.
735 *
736 * This call might reallocate skb data.
737 */
673bool batadv_gw_out_of_range(struct batadv_priv *bat_priv, 738bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
674 struct sk_buff *skb) 739 struct sk_buff *skb)
675{ 740{
676 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL; 741 struct batadv_neigh_node *neigh_curr = NULL, *neigh_old = NULL;
677 struct batadv_orig_node *orig_dst_node = NULL; 742 struct batadv_orig_node *orig_dst_node = NULL;
678 struct batadv_gw_node *curr_gw = NULL; 743 struct batadv_gw_node *gw_node = NULL, *curr_gw = NULL;
679 struct ethhdr *ethhdr; 744 struct ethhdr *ethhdr;
680 bool ret, out_of_range = false; 745 bool ret, out_of_range = false;
681 unsigned int header_len = 0; 746 unsigned int header_len = 0;
682 uint8_t curr_tq_avg; 747 uint8_t curr_tq_avg;
748 unsigned short vid;
749
750 vid = batadv_get_vid(skb, 0);
683 751
684 ret = batadv_gw_is_dhcp_target(skb, &header_len); 752 ret = batadv_gw_is_dhcp_target(skb, &header_len);
685 if (!ret) 753 if (!ret)
@@ -687,11 +755,12 @@ bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
687 755
688 ethhdr = (struct ethhdr *)skb->data; 756 ethhdr = (struct ethhdr *)skb->data;
689 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source, 757 orig_dst_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
690 ethhdr->h_dest); 758 ethhdr->h_dest, vid);
691 if (!orig_dst_node) 759 if (!orig_dst_node)
692 goto out; 760 goto out;
693 761
694 if (!orig_dst_node->gw_flags) 762 gw_node = batadv_gw_node_get(bat_priv, orig_dst_node);
763 if (!gw_node->bandwidth_down == 0)
695 goto out; 764 goto out;
696 765
697 ret = batadv_is_type_dhcprequest(skb, header_len); 766 ret = batadv_is_type_dhcprequest(skb, header_len);
@@ -723,7 +792,7 @@ bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
723 if (!neigh_curr) 792 if (!neigh_curr)
724 goto out; 793 goto out;
725 794
726 curr_tq_avg = neigh_curr->tq_avg; 795 curr_tq_avg = neigh_curr->bat_iv.tq_avg;
727 break; 796 break;
728 case BATADV_GW_MODE_OFF: 797 case BATADV_GW_MODE_OFF:
729 default: 798 default:
@@ -734,7 +803,7 @@ bool batadv_gw_out_of_range(struct batadv_priv *bat_priv,
734 if (!neigh_old) 803 if (!neigh_old)
735 goto out; 804 goto out;
736 805
737 if (curr_tq_avg - neigh_old->tq_avg > BATADV_GW_THRESHOLD) 806 if (curr_tq_avg - neigh_old->bat_iv.tq_avg > BATADV_GW_THRESHOLD)
738 out_of_range = true; 807 out_of_range = true;
739 808
740out: 809out:
@@ -742,6 +811,8 @@ out:
742 batadv_orig_node_free_ref(orig_dst_node); 811 batadv_orig_node_free_ref(orig_dst_node);
743 if (curr_gw) 812 if (curr_gw)
744 batadv_gw_node_free_ref(curr_gw); 813 batadv_gw_node_free_ref(curr_gw);
814 if (gw_node)
815 batadv_gw_node_free_ref(gw_node);
745 if (neigh_old) 816 if (neigh_old)
746 batadv_neigh_node_free_ref(neigh_old); 817 batadv_neigh_node_free_ref(neigh_old);
747 if (neigh_curr) 818 if (neigh_curr)
diff --git a/net/batman-adv/gateway_client.h b/net/batman-adv/gateway_client.h
index ceef4ebe8bcd..d95c2d23195e 100644
--- a/net/batman-adv/gateway_client.h
+++ b/net/batman-adv/gateway_client.h
@@ -29,7 +29,7 @@ void batadv_gw_check_election(struct batadv_priv *bat_priv,
29 struct batadv_orig_node *orig_node); 29 struct batadv_orig_node *orig_node);
30void batadv_gw_node_update(struct batadv_priv *bat_priv, 30void batadv_gw_node_update(struct batadv_priv *bat_priv,
31 struct batadv_orig_node *orig_node, 31 struct batadv_orig_node *orig_node,
32 uint8_t new_gwflags); 32 struct batadv_tvlv_gateway_data *gateway);
33void batadv_gw_node_delete(struct batadv_priv *bat_priv, 33void batadv_gw_node_delete(struct batadv_priv *bat_priv,
34 struct batadv_orig_node *orig_node); 34 struct batadv_orig_node *orig_node);
35void batadv_gw_node_purge(struct batadv_priv *bat_priv); 35void batadv_gw_node_purge(struct batadv_priv *bat_priv);
diff --git a/net/batman-adv/gateway_common.c b/net/batman-adv/gateway_common.c
index 84bb2b18d711..b211b0f9cb78 100644
--- a/net/batman-adv/gateway_common.c
+++ b/net/batman-adv/gateway_common.c
@@ -21,64 +21,23 @@
21#include "gateway_common.h" 21#include "gateway_common.h"
22#include "gateway_client.h" 22#include "gateway_client.h"
23 23
24/* calculates the gateway class from kbit */ 24/**
25static void batadv_kbit_to_gw_bandwidth(int down, int up, long *gw_srv_class) 25 * batadv_parse_gw_bandwidth - parse supplied string buffer to extract download
26{ 26 * and upload bandwidth information
27 int mdown = 0, tdown, tup, difference; 27 * @net_dev: the soft interface net device
28 uint8_t sbit, part; 28 * @buff: string buffer to parse
29 29 * @down: pointer holding the returned download bandwidth information
30 *gw_srv_class = 0; 30 * @up: pointer holding the returned upload bandwidth information
31 difference = 0x0FFFFFFF; 31 *
32 32 * Returns false on parse error and true otherwise.
33 /* test all downspeeds */ 33 */
34 for (sbit = 0; sbit < 2; sbit++) {
35 for (part = 0; part < 16; part++) {
36 tdown = 32 * (sbit + 2) * (1 << part);
37
38 if (abs(tdown - down) < difference) {
39 *gw_srv_class = (sbit << 7) + (part << 3);
40 difference = abs(tdown - down);
41 mdown = tdown;
42 }
43 }
44 }
45
46 /* test all upspeeds */
47 difference = 0x0FFFFFFF;
48
49 for (part = 0; part < 8; part++) {
50 tup = ((part + 1) * (mdown)) / 8;
51
52 if (abs(tup - up) < difference) {
53 *gw_srv_class = (*gw_srv_class & 0xF8) | part;
54 difference = abs(tup - up);
55 }
56 }
57}
58
59/* returns the up and downspeeds in kbit, calculated from the class */
60void batadv_gw_bandwidth_to_kbit(uint8_t gw_srv_class, int *down, int *up)
61{
62 int sbit = (gw_srv_class & 0x80) >> 7;
63 int dpart = (gw_srv_class & 0x78) >> 3;
64 int upart = (gw_srv_class & 0x07);
65
66 if (!gw_srv_class) {
67 *down = 0;
68 *up = 0;
69 return;
70 }
71
72 *down = 32 * (sbit + 2) * (1 << dpart);
73 *up = ((upart + 1) * (*down)) / 8;
74}
75
76static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff, 34static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
77 int *up, int *down) 35 uint32_t *down, uint32_t *up)
78{ 36{
79 int ret, multi = 1; 37 enum batadv_bandwidth_units bw_unit_type = BATADV_BW_UNIT_KBIT;
80 char *slash_ptr, *tmp_ptr; 38 char *slash_ptr, *tmp_ptr;
81 long ldown, lup; 39 long ldown, lup;
40 int ret;
82 41
83 slash_ptr = strchr(buff, '/'); 42 slash_ptr = strchr(buff, '/');
84 if (slash_ptr) 43 if (slash_ptr)
@@ -88,10 +47,10 @@ static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
88 tmp_ptr = buff + strlen(buff) - 4; 47 tmp_ptr = buff + strlen(buff) - 4;
89 48
90 if (strnicmp(tmp_ptr, "mbit", 4) == 0) 49 if (strnicmp(tmp_ptr, "mbit", 4) == 0)
91 multi = 1024; 50 bw_unit_type = BATADV_BW_UNIT_MBIT;
92 51
93 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || 52 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
94 (multi > 1)) 53 (bw_unit_type == BATADV_BW_UNIT_MBIT))
95 *tmp_ptr = '\0'; 54 *tmp_ptr = '\0';
96 } 55 }
97 56
@@ -103,20 +62,28 @@ static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
103 return false; 62 return false;
104 } 63 }
105 64
106 *down = ldown * multi; 65 switch (bw_unit_type) {
66 case BATADV_BW_UNIT_MBIT:
67 *down = ldown * 10;
68 break;
69 case BATADV_BW_UNIT_KBIT:
70 default:
71 *down = ldown / 100;
72 break;
73 }
107 74
108 /* we also got some upload info */ 75 /* we also got some upload info */
109 if (slash_ptr) { 76 if (slash_ptr) {
110 multi = 1; 77 bw_unit_type = BATADV_BW_UNIT_KBIT;
111 78
112 if (strlen(slash_ptr + 1) > 4) { 79 if (strlen(slash_ptr + 1) > 4) {
113 tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1); 80 tmp_ptr = slash_ptr + 1 - 4 + strlen(slash_ptr + 1);
114 81
115 if (strnicmp(tmp_ptr, "mbit", 4) == 0) 82 if (strnicmp(tmp_ptr, "mbit", 4) == 0)
116 multi = 1024; 83 bw_unit_type = BATADV_BW_UNIT_MBIT;
117 84
118 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || 85 if ((strnicmp(tmp_ptr, "kbit", 4) == 0) ||
119 (multi > 1)) 86 (bw_unit_type == BATADV_BW_UNIT_MBIT))
120 *tmp_ptr = '\0'; 87 *tmp_ptr = '\0';
121 } 88 }
122 89
@@ -128,52 +95,149 @@ static bool batadv_parse_gw_bandwidth(struct net_device *net_dev, char *buff,
128 return false; 95 return false;
129 } 96 }
130 97
131 *up = lup * multi; 98 switch (bw_unit_type) {
99 case BATADV_BW_UNIT_MBIT:
100 *up = lup * 10;
101 break;
102 case BATADV_BW_UNIT_KBIT:
103 default:
104 *up = lup / 100;
105 break;
106 }
132 } 107 }
133 108
134 return true; 109 return true;
135} 110}
136 111
112/**
113 * batadv_gw_tvlv_container_update - update the gw tvlv container after gateway
114 * setting change
115 * @bat_priv: the bat priv with all the soft interface information
116 */
117void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv)
118{
119 struct batadv_tvlv_gateway_data gw;
120 uint32_t down, up;
121 char gw_mode;
122
123 gw_mode = atomic_read(&bat_priv->gw_mode);
124
125 switch (gw_mode) {
126 case BATADV_GW_MODE_OFF:
127 case BATADV_GW_MODE_CLIENT:
128 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
129 break;
130 case BATADV_GW_MODE_SERVER:
131 down = atomic_read(&bat_priv->gw.bandwidth_down);
132 up = atomic_read(&bat_priv->gw.bandwidth_up);
133 gw.bandwidth_down = htonl(down);
134 gw.bandwidth_up = htonl(up);
135 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_GW, 1,
136 &gw, sizeof(gw));
137 break;
138 }
139}
140
137ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, 141ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
138 size_t count) 142 size_t count)
139{ 143{
140 struct batadv_priv *bat_priv = netdev_priv(net_dev); 144 struct batadv_priv *bat_priv = netdev_priv(net_dev);
141 long gw_bandwidth_tmp = 0; 145 uint32_t down_curr, up_curr, down_new = 0, up_new = 0;
142 int up = 0, down = 0;
143 bool ret; 146 bool ret;
144 147
145 ret = batadv_parse_gw_bandwidth(net_dev, buff, &up, &down); 148 down_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_down);
149 up_curr = (unsigned int)atomic_read(&bat_priv->gw.bandwidth_up);
150
151 ret = batadv_parse_gw_bandwidth(net_dev, buff, &down_new, &up_new);
146 if (!ret) 152 if (!ret)
147 goto end; 153 goto end;
148 154
149 if ((!down) || (down < 256)) 155 if (!down_new)
150 down = 2000; 156 down_new = 1;
151
152 if (!up)
153 up = down / 5;
154 157
155 batadv_kbit_to_gw_bandwidth(down, up, &gw_bandwidth_tmp); 158 if (!up_new)
159 up_new = down_new / 5;
156 160
157 /* the gw bandwidth we guessed above might not match the given 161 if (!up_new)
158 * speeds, hence we need to calculate it back to show the number 162 up_new = 1;
159 * that is going to be propagated
160 */
161 batadv_gw_bandwidth_to_kbit((uint8_t)gw_bandwidth_tmp, &down, &up);
162 163
163 if (atomic_read(&bat_priv->gw_bandwidth) == gw_bandwidth_tmp) 164 if ((down_curr == down_new) && (up_curr == up_new))
164 return count; 165 return count;
165 166
166 batadv_gw_deselect(bat_priv); 167 batadv_gw_deselect(bat_priv);
167 batadv_info(net_dev, 168 batadv_info(net_dev,
168 "Changing gateway bandwidth from: '%i' to: '%ld' (propagating: %d%s/%d%s)\n", 169 "Changing gateway bandwidth from: '%u.%u/%u.%u MBit' to: '%u.%u/%u.%u MBit'\n",
169 atomic_read(&bat_priv->gw_bandwidth), gw_bandwidth_tmp, 170 down_curr / 10, down_curr % 10, up_curr / 10, up_curr % 10,
170 (down > 2048 ? down / 1024 : down), 171 down_new / 10, down_new % 10, up_new / 10, up_new % 10);
171 (down > 2048 ? "MBit" : "KBit"),
172 (up > 2048 ? up / 1024 : up),
173 (up > 2048 ? "MBit" : "KBit"));
174 172
175 atomic_set(&bat_priv->gw_bandwidth, gw_bandwidth_tmp); 173 atomic_set(&bat_priv->gw.bandwidth_down, down_new);
174 atomic_set(&bat_priv->gw.bandwidth_up, up_new);
175 batadv_gw_tvlv_container_update(bat_priv);
176 176
177end: 177end:
178 return count; 178 return count;
179} 179}
180
181/**
182 * batadv_gw_tvlv_ogm_handler_v1 - process incoming gateway tvlv container
183 * @bat_priv: the bat priv with all the soft interface information
184 * @orig: the orig_node of the ogm
185 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
186 * @tvlv_value: tvlv buffer containing the gateway data
187 * @tvlv_value_len: tvlv buffer length
188 */
189static void batadv_gw_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
190 struct batadv_orig_node *orig,
191 uint8_t flags,
192 void *tvlv_value,
193 uint16_t tvlv_value_len)
194{
195 struct batadv_tvlv_gateway_data gateway, *gateway_ptr;
196
197 /* only fetch the tvlv value if the handler wasn't called via the
198 * CIFNOTFND flag and if there is data to fetch
199 */
200 if ((flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) ||
201 (tvlv_value_len < sizeof(gateway))) {
202 gateway.bandwidth_down = 0;
203 gateway.bandwidth_up = 0;
204 } else {
205 gateway_ptr = tvlv_value;
206 gateway.bandwidth_down = gateway_ptr->bandwidth_down;
207 gateway.bandwidth_up = gateway_ptr->bandwidth_up;
208 if ((gateway.bandwidth_down == 0) ||
209 (gateway.bandwidth_up == 0)) {
210 gateway.bandwidth_down = 0;
211 gateway.bandwidth_up = 0;
212 }
213 }
214
215 batadv_gw_node_update(bat_priv, orig, &gateway);
216
217 /* restart gateway selection if fast or late switching was enabled */
218 if ((gateway.bandwidth_down != 0) &&
219 (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
220 (atomic_read(&bat_priv->gw_sel_class) > 2))
221 batadv_gw_check_election(bat_priv, orig);
222}
223
224/**
225 * batadv_gw_init - initialise the gateway handling internals
226 * @bat_priv: the bat priv with all the soft interface information
227 */
228void batadv_gw_init(struct batadv_priv *bat_priv)
229{
230 batadv_tvlv_handler_register(bat_priv, batadv_gw_tvlv_ogm_handler_v1,
231 NULL, BATADV_TVLV_GW, 1,
232 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
233}
234
235/**
236 * batadv_gw_free - free the gateway handling internals
237 * @bat_priv: the bat priv with all the soft interface information
238 */
239void batadv_gw_free(struct batadv_priv *bat_priv)
240{
241 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_GW, 1);
242 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_GW, 1);
243}
diff --git a/net/batman-adv/gateway_common.h b/net/batman-adv/gateway_common.h
index 509b2bf8c2f4..56384a4cd18c 100644
--- a/net/batman-adv/gateway_common.h
+++ b/net/batman-adv/gateway_common.h
@@ -26,12 +26,24 @@ enum batadv_gw_modes {
26 BATADV_GW_MODE_SERVER, 26 BATADV_GW_MODE_SERVER,
27}; 27};
28 28
29/**
30 * enum batadv_bandwidth_units - bandwidth unit types
31 * @BATADV_BW_UNIT_KBIT: unit type kbit
32 * @BATADV_BW_UNIT_MBIT: unit type mbit
33 */
34enum batadv_bandwidth_units {
35 BATADV_BW_UNIT_KBIT,
36 BATADV_BW_UNIT_MBIT,
37};
38
29#define BATADV_GW_MODE_OFF_NAME "off" 39#define BATADV_GW_MODE_OFF_NAME "off"
30#define BATADV_GW_MODE_CLIENT_NAME "client" 40#define BATADV_GW_MODE_CLIENT_NAME "client"
31#define BATADV_GW_MODE_SERVER_NAME "server" 41#define BATADV_GW_MODE_SERVER_NAME "server"
32 42
33void batadv_gw_bandwidth_to_kbit(uint8_t gw_class, int *down, int *up);
34ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff, 43ssize_t batadv_gw_bandwidth_set(struct net_device *net_dev, char *buff,
35 size_t count); 44 size_t count);
45void batadv_gw_tvlv_container_update(struct batadv_priv *bat_priv);
46void batadv_gw_init(struct batadv_priv *bat_priv);
47void batadv_gw_free(struct batadv_priv *bat_priv);
36 48
37#endif /* _NET_BATMAN_ADV_GATEWAY_COMMON_H_ */ 49#endif /* _NET_BATMAN_ADV_GATEWAY_COMMON_H_ */
diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c
index c478e6bcf89b..57c2a19dcb5c 100644
--- a/net/batman-adv/hard-interface.c
+++ b/net/batman-adv/hard-interface.c
@@ -28,6 +28,7 @@
28#include "originator.h" 28#include "originator.h"
29#include "hash.h" 29#include "hash.h"
30#include "bridge_loop_avoidance.h" 30#include "bridge_loop_avoidance.h"
31#include "gateway_client.h"
31 32
32#include <linux/if_arp.h> 33#include <linux/if_arp.h>
33#include <linux/if_ether.h> 34#include <linux/if_ether.h>
@@ -124,8 +125,11 @@ static int batadv_is_valid_iface(const struct net_device *net_dev)
124 * 125 *
125 * Returns true if the net device is a 802.11 wireless device, false otherwise. 126 * Returns true if the net device is a 802.11 wireless device, false otherwise.
126 */ 127 */
127static bool batadv_is_wifi_netdev(struct net_device *net_device) 128bool batadv_is_wifi_netdev(struct net_device *net_device)
128{ 129{
130 if (!net_device)
131 return false;
132
129#ifdef CONFIG_WIRELESS_EXT 133#ifdef CONFIG_WIRELESS_EXT
130 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to 134 /* pre-cfg80211 drivers have to implement WEXT, so it is possible to
131 * check for wireless_handlers != NULL 135 * check for wireless_handlers != NULL
@@ -141,34 +145,6 @@ static bool batadv_is_wifi_netdev(struct net_device *net_device)
141 return false; 145 return false;
142} 146}
143 147
144/**
145 * batadv_is_wifi_iface - check if the given interface represented by ifindex
146 * is a wifi interface
147 * @ifindex: interface index to check
148 *
149 * Returns true if the interface represented by ifindex is a 802.11 wireless
150 * device, false otherwise.
151 */
152bool batadv_is_wifi_iface(int ifindex)
153{
154 struct net_device *net_device = NULL;
155 bool ret = false;
156
157 if (ifindex == BATADV_NULL_IFINDEX)
158 goto out;
159
160 net_device = dev_get_by_index(&init_net, ifindex);
161 if (!net_device)
162 goto out;
163
164 ret = batadv_is_wifi_netdev(net_device);
165
166out:
167 if (net_device)
168 dev_put(net_device);
169 return ret;
170}
171
172static struct batadv_hard_iface * 148static struct batadv_hard_iface *
173batadv_hardif_get_active(const struct net_device *soft_iface) 149batadv_hardif_get_active(const struct net_device *soft_iface)
174{ 150{
@@ -194,22 +170,13 @@ out:
194static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv, 170static void batadv_primary_if_update_addr(struct batadv_priv *bat_priv,
195 struct batadv_hard_iface *oldif) 171 struct batadv_hard_iface *oldif)
196{ 172{
197 struct batadv_vis_packet *vis_packet;
198 struct batadv_hard_iface *primary_if; 173 struct batadv_hard_iface *primary_if;
199 struct sk_buff *skb;
200 174
201 primary_if = batadv_primary_if_get_selected(bat_priv); 175 primary_if = batadv_primary_if_get_selected(bat_priv);
202 if (!primary_if) 176 if (!primary_if)
203 goto out; 177 goto out;
204 178
205 batadv_dat_init_own_addr(bat_priv, primary_if); 179 batadv_dat_init_own_addr(bat_priv, primary_if);
206
207 skb = bat_priv->vis.my_info->skb_packet;
208 vis_packet = (struct batadv_vis_packet *)skb->data;
209 memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
210 memcpy(vis_packet->sender_orig,
211 primary_if->net_dev->dev_addr, ETH_ALEN);
212
213 batadv_bla_update_orig_address(bat_priv, primary_if, oldif); 180 batadv_bla_update_orig_address(bat_priv, primary_if, oldif);
214out: 181out:
215 if (primary_if) 182 if (primary_if)
@@ -275,16 +242,10 @@ static void batadv_check_known_mac_addr(const struct net_device *net_dev)
275 242
276int batadv_hardif_min_mtu(struct net_device *soft_iface) 243int batadv_hardif_min_mtu(struct net_device *soft_iface)
277{ 244{
278 const struct batadv_priv *bat_priv = netdev_priv(soft_iface); 245 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
279 const struct batadv_hard_iface *hard_iface; 246 const struct batadv_hard_iface *hard_iface;
280 /* allow big frames if all devices are capable to do so
281 * (have MTU > 1500 + BAT_HEADER_LEN)
282 */
283 int min_mtu = ETH_DATA_LEN; 247 int min_mtu = ETH_DATA_LEN;
284 248
285 if (atomic_read(&bat_priv->fragmentation))
286 goto out;
287
288 rcu_read_lock(); 249 rcu_read_lock();
289 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { 250 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
290 if ((hard_iface->if_status != BATADV_IF_ACTIVE) && 251 if ((hard_iface->if_status != BATADV_IF_ACTIVE) &&
@@ -294,23 +255,40 @@ int batadv_hardif_min_mtu(struct net_device *soft_iface)
294 if (hard_iface->soft_iface != soft_iface) 255 if (hard_iface->soft_iface != soft_iface)
295 continue; 256 continue;
296 257
297 min_mtu = min_t(int, 258 min_mtu = min_t(int, hard_iface->net_dev->mtu, min_mtu);
298 hard_iface->net_dev->mtu - BATADV_HEADER_LEN,
299 min_mtu);
300 } 259 }
301 rcu_read_unlock(); 260 rcu_read_unlock();
261
262 atomic_set(&bat_priv->packet_size_max, min_mtu);
263
264 if (atomic_read(&bat_priv->fragmentation) == 0)
265 goto out;
266
267 /* with fragmentation enabled the maximum size of internally generated
268 * packets such as translation table exchanges or tvlv containers, etc
269 * has to be calculated
270 */
271 min_mtu = min_t(int, min_mtu, BATADV_FRAG_MAX_FRAG_SIZE);
272 min_mtu -= sizeof(struct batadv_frag_packet);
273 min_mtu *= BATADV_FRAG_MAX_FRAGMENTS;
274 atomic_set(&bat_priv->packet_size_max, min_mtu);
275
276 /* with fragmentation enabled we can fragment external packets easily */
277 min_mtu = min_t(int, min_mtu, ETH_DATA_LEN);
278
302out: 279out:
303 return min_mtu; 280 return min_mtu - batadv_max_header_len();
304} 281}
305 282
306/* adjusts the MTU if a new interface with a smaller MTU appeared. */ 283/* adjusts the MTU if a new interface with a smaller MTU appeared. */
307void batadv_update_min_mtu(struct net_device *soft_iface) 284void batadv_update_min_mtu(struct net_device *soft_iface)
308{ 285{
309 int min_mtu; 286 soft_iface->mtu = batadv_hardif_min_mtu(soft_iface);
310 287
311 min_mtu = batadv_hardif_min_mtu(soft_iface); 288 /* Check if the local translate table should be cleaned up to match a
312 if (soft_iface->mtu != min_mtu) 289 * new (and smaller) MTU.
313 soft_iface->mtu = min_mtu; 290 */
291 batadv_tt_local_resize_to_mtu(soft_iface);
314} 292}
315 293
316static void 294static void
@@ -388,7 +366,8 @@ int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
388{ 366{
389 struct batadv_priv *bat_priv; 367 struct batadv_priv *bat_priv;
390 struct net_device *soft_iface, *master; 368 struct net_device *soft_iface, *master;
391 __be16 ethertype = __constant_htons(ETH_P_BATMAN); 369 __be16 ethertype = htons(ETH_P_BATMAN);
370 int max_header_len = batadv_max_header_len();
392 int ret; 371 int ret;
393 372
394 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE) 373 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
@@ -453,23 +432,22 @@ int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
453 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev; 432 hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
454 dev_add_pack(&hard_iface->batman_adv_ptype); 433 dev_add_pack(&hard_iface->batman_adv_ptype);
455 434
456 atomic_set(&hard_iface->frag_seqno, 1);
457 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n", 435 batadv_info(hard_iface->soft_iface, "Adding interface: %s\n",
458 hard_iface->net_dev->name); 436 hard_iface->net_dev->name);
459 437
460 if (atomic_read(&bat_priv->fragmentation) && 438 if (atomic_read(&bat_priv->fragmentation) &&
461 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) 439 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
462 batadv_info(hard_iface->soft_iface, 440 batadv_info(hard_iface->soft_iface,
463 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %zi would solve the problem.\n", 441 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. Packets going over this interface will be fragmented on layer2 which could impact the performance. Setting the MTU to %i would solve the problem.\n",
464 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 442 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
465 ETH_DATA_LEN + BATADV_HEADER_LEN); 443 ETH_DATA_LEN + max_header_len);
466 444
467 if (!atomic_read(&bat_priv->fragmentation) && 445 if (!atomic_read(&bat_priv->fragmentation) &&
468 hard_iface->net_dev->mtu < ETH_DATA_LEN + BATADV_HEADER_LEN) 446 hard_iface->net_dev->mtu < ETH_DATA_LEN + max_header_len)
469 batadv_info(hard_iface->soft_iface, 447 batadv_info(hard_iface->soft_iface,
470 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %zi.\n", 448 "The MTU of interface %s is too small (%i) to handle the transport of batman-adv packets. If you experience problems getting traffic through try increasing the MTU to %i.\n",
471 hard_iface->net_dev->name, hard_iface->net_dev->mtu, 449 hard_iface->net_dev->name, hard_iface->net_dev->mtu,
472 ETH_DATA_LEN + BATADV_HEADER_LEN); 450 ETH_DATA_LEN + max_header_len);
473 451
474 if (batadv_hardif_is_iface_up(hard_iface)) 452 if (batadv_hardif_is_iface_up(hard_iface))
475 batadv_hardif_activate_interface(hard_iface); 453 batadv_hardif_activate_interface(hard_iface);
@@ -533,8 +511,12 @@ void batadv_hardif_disable_interface(struct batadv_hard_iface *hard_iface,
533 dev_put(hard_iface->soft_iface); 511 dev_put(hard_iface->soft_iface);
534 512
535 /* nobody uses this interface anymore */ 513 /* nobody uses this interface anymore */
536 if (!bat_priv->num_ifaces && autodel == BATADV_IF_CLEANUP_AUTO) 514 if (!bat_priv->num_ifaces) {
537 batadv_softif_destroy_sysfs(hard_iface->soft_iface); 515 batadv_gw_check_client_stop(bat_priv);
516
517 if (autodel == BATADV_IF_CLEANUP_AUTO)
518 batadv_softif_destroy_sysfs(hard_iface->soft_iface);
519 }
538 520
539 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface); 521 netdev_upper_dev_unlink(hard_iface->net_dev, hard_iface->soft_iface);
540 hard_iface->soft_iface = NULL; 522 hard_iface->soft_iface = NULL;
@@ -652,6 +634,8 @@ static int batadv_hard_if_event(struct notifier_block *this,
652 634
653 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) { 635 if (batadv_softif_is_valid(net_dev) && event == NETDEV_REGISTER) {
654 batadv_sysfs_add_meshif(net_dev); 636 batadv_sysfs_add_meshif(net_dev);
637 bat_priv = netdev_priv(net_dev);
638 batadv_softif_create_vlan(bat_priv, BATADV_NO_FLAGS);
655 return NOTIFY_DONE; 639 return NOTIFY_DONE;
656 } 640 }
657 641
diff --git a/net/batman-adv/hard-interface.h b/net/batman-adv/hard-interface.h
index 49892881a7c5..df4c8bd45c40 100644
--- a/net/batman-adv/hard-interface.h
+++ b/net/batman-adv/hard-interface.h
@@ -41,6 +41,7 @@ enum batadv_hard_if_cleanup {
41 41
42extern struct notifier_block batadv_hard_if_notifier; 42extern struct notifier_block batadv_hard_if_notifier;
43 43
44bool batadv_is_wifi_netdev(struct net_device *net_device);
44struct batadv_hard_iface* 45struct batadv_hard_iface*
45batadv_hardif_get_by_netdev(const struct net_device *net_dev); 46batadv_hardif_get_by_netdev(const struct net_device *net_dev);
46int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface, 47int batadv_hardif_enable_interface(struct batadv_hard_iface *hard_iface,
@@ -51,7 +52,6 @@ void batadv_hardif_remove_interfaces(void);
51int batadv_hardif_min_mtu(struct net_device *soft_iface); 52int batadv_hardif_min_mtu(struct net_device *soft_iface);
52void batadv_update_min_mtu(struct net_device *soft_iface); 53void batadv_update_min_mtu(struct net_device *soft_iface);
53void batadv_hardif_free_rcu(struct rcu_head *rcu); 54void batadv_hardif_free_rcu(struct rcu_head *rcu);
54bool batadv_is_wifi_iface(int ifindex);
55 55
56static inline void 56static inline void
57batadv_hardif_free_ref(struct batadv_hard_iface *hard_iface) 57batadv_hardif_free_ref(struct batadv_hard_iface *hard_iface)
diff --git a/net/batman-adv/icmp_socket.c b/net/batman-adv/icmp_socket.c
index 5a99bb4b6b82..130cc3217e2b 100644
--- a/net/batman-adv/icmp_socket.c
+++ b/net/batman-adv/icmp_socket.c
@@ -29,7 +29,7 @@
29static struct batadv_socket_client *batadv_socket_client_hash[256]; 29static struct batadv_socket_client *batadv_socket_client_hash[256];
30 30
31static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, 31static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
32 struct batadv_icmp_packet_rr *icmp_packet, 32 struct batadv_icmp_header *icmph,
33 size_t icmp_len); 33 size_t icmp_len);
34 34
35void batadv_socket_init(void) 35void batadv_socket_init(void)
@@ -155,13 +155,13 @@ static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
155 struct batadv_priv *bat_priv = socket_client->bat_priv; 155 struct batadv_priv *bat_priv = socket_client->bat_priv;
156 struct batadv_hard_iface *primary_if = NULL; 156 struct batadv_hard_iface *primary_if = NULL;
157 struct sk_buff *skb; 157 struct sk_buff *skb;
158 struct batadv_icmp_packet_rr *icmp_packet; 158 struct batadv_icmp_packet_rr *icmp_packet_rr;
159 159 struct batadv_icmp_header *icmp_header;
160 struct batadv_orig_node *orig_node = NULL; 160 struct batadv_orig_node *orig_node = NULL;
161 struct batadv_neigh_node *neigh_node = NULL; 161 struct batadv_neigh_node *neigh_node = NULL;
162 size_t packet_len = sizeof(struct batadv_icmp_packet); 162 size_t packet_len = sizeof(struct batadv_icmp_packet);
163 163
164 if (len < sizeof(struct batadv_icmp_packet)) { 164 if (len < sizeof(struct batadv_icmp_header)) {
165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
166 "Error - can't send packet from char device: invalid packet size\n"); 166 "Error - can't send packet from char device: invalid packet size\n");
167 return -EINVAL; 167 return -EINVAL;
@@ -174,8 +174,10 @@ static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
174 goto out; 174 goto out;
175 } 175 }
176 176
177 if (len >= sizeof(struct batadv_icmp_packet_rr)) 177 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
178 packet_len = sizeof(struct batadv_icmp_packet_rr); 178 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
179 else
180 packet_len = len;
179 181
180 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN); 182 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
181 if (!skb) { 183 if (!skb) {
@@ -185,67 +187,78 @@ static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
185 187
186 skb->priority = TC_PRIO_CONTROL; 188 skb->priority = TC_PRIO_CONTROL;
187 skb_reserve(skb, ETH_HLEN); 189 skb_reserve(skb, ETH_HLEN);
188 icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len); 190 icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
189 191
190 if (copy_from_user(icmp_packet, buff, packet_len)) { 192 if (copy_from_user(icmp_header, buff, packet_len)) {
191 len = -EFAULT; 193 len = -EFAULT;
192 goto free_skb; 194 goto free_skb;
193 } 195 }
194 196
195 if (icmp_packet->header.packet_type != BATADV_ICMP) { 197 if (icmp_header->packet_type != BATADV_ICMP) {
196 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 198 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
197 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n"); 199 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
198 len = -EINVAL; 200 len = -EINVAL;
199 goto free_skb; 201 goto free_skb;
200 } 202 }
201 203
202 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { 204 switch (icmp_header->msg_type) {
205 case BATADV_ECHO_REQUEST:
206 if (len < sizeof(struct batadv_icmp_packet)) {
207 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
208 "Error - can't send packet from char device: invalid packet size\n");
209 len = -EINVAL;
210 goto free_skb;
211 }
212
213 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
214 goto dst_unreach;
215
216 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
217 if (!orig_node)
218 goto dst_unreach;
219
220 neigh_node = batadv_orig_node_get_router(orig_node);
221 if (!neigh_node)
222 goto dst_unreach;
223
224 if (!neigh_node->if_incoming)
225 goto dst_unreach;
226
227 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
228 goto dst_unreach;
229
230 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
231 if (packet_len == sizeof(*icmp_packet_rr))
232 memcpy(icmp_packet_rr->rr,
233 neigh_node->if_incoming->net_dev->dev_addr,
234 ETH_ALEN);
235
236 break;
237 default:
203 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 238 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
204 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n"); 239 "Error - can't send packet from char device: got unknown message type\n");
205 len = -EINVAL; 240 len = -EINVAL;
206 goto free_skb; 241 goto free_skb;
207 } 242 }
208 243
209 icmp_packet->uid = socket_client->index; 244 icmp_header->uid = socket_client->index;
210 245
211 if (icmp_packet->header.version != BATADV_COMPAT_VERSION) { 246 if (icmp_header->version != BATADV_COMPAT_VERSION) {
212 icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM; 247 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
213 icmp_packet->header.version = BATADV_COMPAT_VERSION; 248 icmp_header->version = BATADV_COMPAT_VERSION;
214 batadv_socket_add_packet(socket_client, icmp_packet, 249 batadv_socket_add_packet(socket_client, icmp_header,
215 packet_len); 250 packet_len);
216 goto free_skb; 251 goto free_skb;
217 } 252 }
218 253
219 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 254 memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
220 goto dst_unreach;
221
222 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
223 if (!orig_node)
224 goto dst_unreach;
225
226 neigh_node = batadv_orig_node_get_router(orig_node);
227 if (!neigh_node)
228 goto dst_unreach;
229
230 if (!neigh_node->if_incoming)
231 goto dst_unreach;
232
233 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
234 goto dst_unreach;
235
236 memcpy(icmp_packet->orig,
237 primary_if->net_dev->dev_addr, ETH_ALEN);
238
239 if (packet_len == sizeof(struct batadv_icmp_packet_rr))
240 memcpy(icmp_packet->rr,
241 neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
242 255
243 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); 256 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
244 goto out; 257 goto out;
245 258
246dst_unreach: 259dst_unreach:
247 icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE; 260 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
248 batadv_socket_add_packet(socket_client, icmp_packet, packet_len); 261 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
249free_skb: 262free_skb:
250 kfree_skb(skb); 263 kfree_skb(skb);
251out: 264out:
@@ -298,27 +311,40 @@ err:
298 return -ENOMEM; 311 return -ENOMEM;
299} 312}
300 313
314/**
315 * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
316 * on an icmp socket.
317 * @socket_client: the socket this packet belongs to
318 * @icmph: pointer to the header of the icmp packet
319 * @icmp_len: total length of the icmp packet
320 */
301static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, 321static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
302 struct batadv_icmp_packet_rr *icmp_packet, 322 struct batadv_icmp_header *icmph,
303 size_t icmp_len) 323 size_t icmp_len)
304{ 324{
305 struct batadv_socket_packet *socket_packet; 325 struct batadv_socket_packet *socket_packet;
326 size_t len;
306 327
307 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); 328 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
308 329
309 if (!socket_packet) 330 if (!socket_packet)
310 return; 331 return;
311 332
333 len = icmp_len;
334 /* check the maximum length before filling the buffer */
335 if (len > sizeof(socket_packet->icmp_packet))
336 len = sizeof(socket_packet->icmp_packet);
337
312 INIT_LIST_HEAD(&socket_packet->list); 338 INIT_LIST_HEAD(&socket_packet->list);
313 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); 339 memcpy(&socket_packet->icmp_packet, icmph, len);
314 socket_packet->icmp_len = icmp_len; 340 socket_packet->icmp_len = len;
315 341
316 spin_lock_bh(&socket_client->lock); 342 spin_lock_bh(&socket_client->lock);
317 343
318 /* while waiting for the lock the socket_client could have been 344 /* while waiting for the lock the socket_client could have been
319 * deleted 345 * deleted
320 */ 346 */
321 if (!batadv_socket_client_hash[icmp_packet->uid]) { 347 if (!batadv_socket_client_hash[icmph->uid]) {
322 spin_unlock_bh(&socket_client->lock); 348 spin_unlock_bh(&socket_client->lock);
323 kfree(socket_packet); 349 kfree(socket_packet);
324 return; 350 return;
@@ -342,12 +368,18 @@ static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
342 wake_up(&socket_client->queue_wait); 368 wake_up(&socket_client->queue_wait);
343} 369}
344 370
345void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet, 371/**
372 * batadv_socket_receive_packet - schedule an icmp packet to be received
373 * locally and sent to userspace.
374 * @icmph: pointer to the header of the icmp packet
375 * @icmp_len: total length of the icmp packet
376 */
377void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
346 size_t icmp_len) 378 size_t icmp_len)
347{ 379{
348 struct batadv_socket_client *hash; 380 struct batadv_socket_client *hash;
349 381
350 hash = batadv_socket_client_hash[icmp_packet->uid]; 382 hash = batadv_socket_client_hash[icmph->uid];
351 if (hash) 383 if (hash)
352 batadv_socket_add_packet(hash, icmp_packet, icmp_len); 384 batadv_socket_add_packet(hash, icmph, icmp_len);
353} 385}
diff --git a/net/batman-adv/icmp_socket.h b/net/batman-adv/icmp_socket.h
index 1fcca37b6223..6665080dff79 100644
--- a/net/batman-adv/icmp_socket.h
+++ b/net/batman-adv/icmp_socket.h
@@ -24,7 +24,7 @@
24 24
25void batadv_socket_init(void); 25void batadv_socket_init(void);
26int batadv_socket_setup(struct batadv_priv *bat_priv); 26int batadv_socket_setup(struct batadv_priv *bat_priv);
27void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet, 27void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
28 size_t icmp_len); 28 size_t icmp_len);
29 29
30#endif /* _NET_BATMAN_ADV_ICMP_SOCKET_H_ */ 30#endif /* _NET_BATMAN_ADV_ICMP_SOCKET_H_ */
diff --git a/net/batman-adv/main.c b/net/batman-adv/main.c
index 1356af660b5b..faba0f61ad53 100644
--- a/net/batman-adv/main.c
+++ b/net/batman-adv/main.c
@@ -36,10 +36,11 @@
36#include "gateway_client.h" 36#include "gateway_client.h"
37#include "bridge_loop_avoidance.h" 37#include "bridge_loop_avoidance.h"
38#include "distributed-arp-table.h" 38#include "distributed-arp-table.h"
39#include "vis.h" 39#include "gateway_common.h"
40#include "hash.h" 40#include "hash.h"
41#include "bat_algo.h" 41#include "bat_algo.h"
42#include "network-coding.h" 42#include "network-coding.h"
43#include "fragmentation.h"
43 44
44 45
45/* List manipulations on hardif_list have to be rtnl_lock()'ed, 46/* List manipulations on hardif_list have to be rtnl_lock()'ed,
@@ -109,9 +110,11 @@ int batadv_mesh_init(struct net_device *soft_iface)
109 spin_lock_init(&bat_priv->tt.req_list_lock); 110 spin_lock_init(&bat_priv->tt.req_list_lock);
110 spin_lock_init(&bat_priv->tt.roam_list_lock); 111 spin_lock_init(&bat_priv->tt.roam_list_lock);
111 spin_lock_init(&bat_priv->tt.last_changeset_lock); 112 spin_lock_init(&bat_priv->tt.last_changeset_lock);
113 spin_lock_init(&bat_priv->tt.commit_lock);
112 spin_lock_init(&bat_priv->gw.list_lock); 114 spin_lock_init(&bat_priv->gw.list_lock);
113 spin_lock_init(&bat_priv->vis.hash_lock); 115 spin_lock_init(&bat_priv->tvlv.container_list_lock);
114 spin_lock_init(&bat_priv->vis.list_lock); 116 spin_lock_init(&bat_priv->tvlv.handler_list_lock);
117 spin_lock_init(&bat_priv->softif_vlan_list_lock);
115 118
116 INIT_HLIST_HEAD(&bat_priv->forw_bat_list); 119 INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
117 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list); 120 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
@@ -119,6 +122,9 @@ int batadv_mesh_init(struct net_device *soft_iface)
119 INIT_LIST_HEAD(&bat_priv->tt.changes_list); 122 INIT_LIST_HEAD(&bat_priv->tt.changes_list);
120 INIT_LIST_HEAD(&bat_priv->tt.req_list); 123 INIT_LIST_HEAD(&bat_priv->tt.req_list);
121 INIT_LIST_HEAD(&bat_priv->tt.roam_list); 124 INIT_LIST_HEAD(&bat_priv->tt.roam_list);
125 INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
126 INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
127 INIT_HLIST_HEAD(&bat_priv->softif_vlan_list);
122 128
123 ret = batadv_originator_init(bat_priv); 129 ret = batadv_originator_init(bat_priv);
124 if (ret < 0) 130 if (ret < 0)
@@ -128,13 +134,6 @@ int batadv_mesh_init(struct net_device *soft_iface)
128 if (ret < 0) 134 if (ret < 0)
129 goto err; 135 goto err;
130 136
131 batadv_tt_local_add(soft_iface, soft_iface->dev_addr,
132 BATADV_NULL_IFINDEX);
133
134 ret = batadv_vis_init(bat_priv);
135 if (ret < 0)
136 goto err;
137
138 ret = batadv_bla_init(bat_priv); 137 ret = batadv_bla_init(bat_priv);
139 if (ret < 0) 138 if (ret < 0)
140 goto err; 139 goto err;
@@ -147,6 +146,8 @@ int batadv_mesh_init(struct net_device *soft_iface)
147 if (ret < 0) 146 if (ret < 0)
148 goto err; 147 goto err;
149 148
149 batadv_gw_init(bat_priv);
150
150 atomic_set(&bat_priv->gw.reselect, 0); 151 atomic_set(&bat_priv->gw.reselect, 0);
151 atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE); 152 atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
152 153
@@ -165,8 +166,6 @@ void batadv_mesh_free(struct net_device *soft_iface)
165 166
166 batadv_purge_outstanding_packets(bat_priv, NULL); 167 batadv_purge_outstanding_packets(bat_priv, NULL);
167 168
168 batadv_vis_quit(bat_priv);
169
170 batadv_gw_node_purge(bat_priv); 169 batadv_gw_node_purge(bat_priv);
171 batadv_nc_mesh_free(bat_priv); 170 batadv_nc_mesh_free(bat_priv);
172 batadv_dat_free(bat_priv); 171 batadv_dat_free(bat_priv);
@@ -185,6 +184,8 @@ void batadv_mesh_free(struct net_device *soft_iface)
185 */ 184 */
186 batadv_originator_free(bat_priv); 185 batadv_originator_free(bat_priv);
187 186
187 batadv_gw_free(bat_priv);
188
188 free_percpu(bat_priv->bat_counters); 189 free_percpu(bat_priv->bat_counters);
189 bat_priv->bat_counters = NULL; 190 bat_priv->bat_counters = NULL;
190 191
@@ -255,6 +256,31 @@ out:
255} 256}
256 257
257/** 258/**
259 * batadv_max_header_len - calculate maximum encapsulation overhead for a
260 * payload packet
261 *
262 * Return the maximum encapsulation overhead in bytes.
263 */
264int batadv_max_header_len(void)
265{
266 int header_len = 0;
267
268 header_len = max_t(int, header_len,
269 sizeof(struct batadv_unicast_packet));
270 header_len = max_t(int, header_len,
271 sizeof(struct batadv_unicast_4addr_packet));
272 header_len = max_t(int, header_len,
273 sizeof(struct batadv_bcast_packet));
274
275#ifdef CONFIG_BATMAN_ADV_NC
276 header_len = max_t(int, header_len,
277 sizeof(struct batadv_coded_packet));
278#endif
279
280 return header_len + ETH_HLEN;
281}
282
283/**
258 * batadv_skb_set_priority - sets skb priority according to packet content 284 * batadv_skb_set_priority - sets skb priority according to packet content
259 * @skb: the packet to be sent 285 * @skb: the packet to be sent
260 * @offset: offset to the packet content 286 * @offset: offset to the packet content
@@ -357,17 +383,17 @@ int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
357 383
358 batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data; 384 batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
359 385
360 if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) { 386 if (batadv_ogm_packet->version != BATADV_COMPAT_VERSION) {
361 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 387 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
362 "Drop packet: incompatible batman version (%i)\n", 388 "Drop packet: incompatible batman version (%i)\n",
363 batadv_ogm_packet->header.version); 389 batadv_ogm_packet->version);
364 goto err_free; 390 goto err_free;
365 } 391 }
366 392
367 /* all receive handlers return whether they received or reused 393 /* all receive handlers return whether they received or reused
368 * the supplied skb. if not, we have to free the skb. 394 * the supplied skb. if not, we have to free the skb.
369 */ 395 */
370 idx = batadv_ogm_packet->header.packet_type; 396 idx = batadv_ogm_packet->packet_type;
371 ret = (*batadv_rx_handler[idx])(skb, hard_iface); 397 ret = (*batadv_rx_handler[idx])(skb, hard_iface);
372 398
373 if (ret == NET_RX_DROP) 399 if (ret == NET_RX_DROP)
@@ -392,22 +418,31 @@ static void batadv_recv_handler_init(void)
392 for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++) 418 for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
393 batadv_rx_handler[i] = batadv_recv_unhandled_packet; 419 batadv_rx_handler[i] = batadv_recv_unhandled_packet;
394 420
395 /* batman icmp packet */ 421 for (i = BATADV_UNICAST_MIN; i <= BATADV_UNICAST_MAX; i++)
396 batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet; 422 batadv_rx_handler[i] = batadv_recv_unhandled_unicast_packet;
423
424 /* compile time checks for struct member offsets */
425 BUILD_BUG_ON(offsetof(struct batadv_unicast_4addr_packet, src) != 10);
426 BUILD_BUG_ON(offsetof(struct batadv_unicast_packet, dest) != 4);
427 BUILD_BUG_ON(offsetof(struct batadv_unicast_tvlv_packet, dst) != 4);
428 BUILD_BUG_ON(offsetof(struct batadv_frag_packet, dest) != 4);
429 BUILD_BUG_ON(offsetof(struct batadv_icmp_packet, dst) != 4);
430 BUILD_BUG_ON(offsetof(struct batadv_icmp_packet_rr, dst) != 4);
431
432 /* broadcast packet */
433 batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
434
435 /* unicast packets ... */
397 /* unicast with 4 addresses packet */ 436 /* unicast with 4 addresses packet */
398 batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet; 437 batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
399 /* unicast packet */ 438 /* unicast packet */
400 batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet; 439 batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
401 /* fragmented unicast packet */ 440 /* unicast tvlv packet */
402 batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet; 441 batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
403 /* broadcast packet */ 442 /* batman icmp packet */
404 batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet; 443 batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
405 /* vis packet */ 444 /* Fragmented packets */
406 batadv_rx_handler[BATADV_VIS] = batadv_recv_vis_packet; 445 batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_frag_packet;
407 /* Translation table query (request or response) */
408 batadv_rx_handler[BATADV_TT_QUERY] = batadv_recv_tt_query;
409 /* Roaming advertisement */
410 batadv_rx_handler[BATADV_ROAM_ADV] = batadv_recv_roam_adv;
411} 446}
412 447
413int 448int
@@ -415,7 +450,12 @@ batadv_recv_handler_register(uint8_t packet_type,
415 int (*recv_handler)(struct sk_buff *, 450 int (*recv_handler)(struct sk_buff *,
416 struct batadv_hard_iface *)) 451 struct batadv_hard_iface *))
417{ 452{
418 if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet) 453 int (*curr)(struct sk_buff *,
454 struct batadv_hard_iface *);
455 curr = batadv_rx_handler[packet_type];
456
457 if ((curr != batadv_recv_unhandled_packet) &&
458 (curr != batadv_recv_unhandled_unicast_packet))
419 return -EBUSY; 459 return -EBUSY;
420 460
421 batadv_rx_handler[packet_type] = recv_handler; 461 batadv_rx_handler[packet_type] = recv_handler;
@@ -461,7 +501,9 @@ int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
461 !bat_algo_ops->bat_iface_update_mac || 501 !bat_algo_ops->bat_iface_update_mac ||
462 !bat_algo_ops->bat_primary_iface_set || 502 !bat_algo_ops->bat_primary_iface_set ||
463 !bat_algo_ops->bat_ogm_schedule || 503 !bat_algo_ops->bat_ogm_schedule ||
464 !bat_algo_ops->bat_ogm_emit) { 504 !bat_algo_ops->bat_ogm_emit ||
505 !bat_algo_ops->bat_neigh_cmp ||
506 !bat_algo_ops->bat_neigh_is_equiv_or_better) {
465 pr_info("Routing algo '%s' does not implement required ops\n", 507 pr_info("Routing algo '%s' does not implement required ops\n",
466 bat_algo_ops->name); 508 bat_algo_ops->name);
467 ret = -EINVAL; 509 ret = -EINVAL;
@@ -536,6 +578,601 @@ __be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
536 return htonl(crc); 578 return htonl(crc);
537} 579}
538 580
581/**
582 * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and
583 * possibly free it
584 * @tvlv_handler: the tvlv handler to free
585 */
586static void
587batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler)
588{
589 if (atomic_dec_and_test(&tvlv_handler->refcount))
590 kfree_rcu(tvlv_handler, rcu);
591}
592
593/**
594 * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
595 * based on the provided type and version (both need to match)
596 * @bat_priv: the bat priv with all the soft interface information
597 * @type: tvlv handler type to look for
598 * @version: tvlv handler version to look for
599 *
600 * Returns tvlv handler if found or NULL otherwise.
601 */
602static struct batadv_tvlv_handler
603*batadv_tvlv_handler_get(struct batadv_priv *bat_priv,
604 uint8_t type, uint8_t version)
605{
606 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
607
608 rcu_read_lock();
609 hlist_for_each_entry_rcu(tvlv_handler_tmp,
610 &bat_priv->tvlv.handler_list, list) {
611 if (tvlv_handler_tmp->type != type)
612 continue;
613
614 if (tvlv_handler_tmp->version != version)
615 continue;
616
617 if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount))
618 continue;
619
620 tvlv_handler = tvlv_handler_tmp;
621 break;
622 }
623 rcu_read_unlock();
624
625 return tvlv_handler;
626}
627
628/**
629 * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
630 * possibly free it
631 * @tvlv_handler: the tvlv container to free
632 */
633static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
634{
635 if (atomic_dec_and_test(&tvlv->refcount))
636 kfree(tvlv);
637}
638
639/**
640 * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
641 * list based on the provided type and version (both need to match)
642 * @bat_priv: the bat priv with all the soft interface information
643 * @type: tvlv container type to look for
644 * @version: tvlv container version to look for
645 *
646 * Has to be called with the appropriate locks being acquired
647 * (tvlv.container_list_lock).
648 *
649 * Returns tvlv container if found or NULL otherwise.
650 */
651static struct batadv_tvlv_container
652*batadv_tvlv_container_get(struct batadv_priv *bat_priv,
653 uint8_t type, uint8_t version)
654{
655 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
656
657 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
658 if (tvlv_tmp->tvlv_hdr.type != type)
659 continue;
660
661 if (tvlv_tmp->tvlv_hdr.version != version)
662 continue;
663
664 if (!atomic_inc_not_zero(&tvlv_tmp->refcount))
665 continue;
666
667 tvlv = tvlv_tmp;
668 break;
669 }
670
671 return tvlv;
672}
673
674/**
675 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
676 * list entries
677 * @bat_priv: the bat priv with all the soft interface information
678 *
679 * Has to be called with the appropriate locks being acquired
680 * (tvlv.container_list_lock).
681 *
682 * Returns size of all currently registered tvlv containers in bytes.
683 */
684static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
685{
686 struct batadv_tvlv_container *tvlv;
687 uint16_t tvlv_len = 0;
688
689 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
690 tvlv_len += sizeof(struct batadv_tvlv_hdr);
691 tvlv_len += ntohs(tvlv->tvlv_hdr.len);
692 }
693
694 return tvlv_len;
695}
696
697/**
698 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
699 * list
700 * @tvlv: the to be removed tvlv container
701 *
702 * Has to be called with the appropriate locks being acquired
703 * (tvlv.container_list_lock).
704 */
705static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv)
706{
707 if (!tvlv)
708 return;
709
710 hlist_del(&tvlv->list);
711
712 /* first call to decrement the counter, second call to free */
713 batadv_tvlv_container_free_ref(tvlv);
714 batadv_tvlv_container_free_ref(tvlv);
715}
716
717/**
718 * batadv_tvlv_container_unregister - unregister tvlv container based on the
719 * provided type and version (both need to match)
720 * @bat_priv: the bat priv with all the soft interface information
721 * @type: tvlv container type to unregister
722 * @version: tvlv container type to unregister
723 */
724void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
725 uint8_t type, uint8_t version)
726{
727 struct batadv_tvlv_container *tvlv;
728
729 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
730 tvlv = batadv_tvlv_container_get(bat_priv, type, version);
731 batadv_tvlv_container_remove(tvlv);
732 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
733}
734
735/**
736 * batadv_tvlv_container_register - register tvlv type, version and content
737 * to be propagated with each (primary interface) OGM
738 * @bat_priv: the bat priv with all the soft interface information
739 * @type: tvlv container type
740 * @version: tvlv container version
741 * @tvlv_value: tvlv container content
742 * @tvlv_value_len: tvlv container content length
743 *
744 * If a container of the same type and version was already registered the new
745 * content is going to replace the old one.
746 */
747void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
748 uint8_t type, uint8_t version,
749 void *tvlv_value, uint16_t tvlv_value_len)
750{
751 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
752
753 if (!tvlv_value)
754 tvlv_value_len = 0;
755
756 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
757 if (!tvlv_new)
758 return;
759
760 tvlv_new->tvlv_hdr.version = version;
761 tvlv_new->tvlv_hdr.type = type;
762 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
763
764 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
765 INIT_HLIST_NODE(&tvlv_new->list);
766 atomic_set(&tvlv_new->refcount, 1);
767
768 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
769 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
770 batadv_tvlv_container_remove(tvlv_old);
771 hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
772 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
773}
774
775/**
776 * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate
777 * requested packet size
778 * @packet_buff: packet buffer
779 * @packet_buff_len: packet buffer size
780 * @packet_min_len: requested packet minimum size
781 * @additional_packet_len: requested additional packet size on top of minimum
782 * size
783 *
784 * Returns true of the packet buffer could be changed to the requested size,
785 * false otherwise.
786 */
787static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
788 int *packet_buff_len,
789 int min_packet_len,
790 int additional_packet_len)
791{
792 unsigned char *new_buff;
793
794 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
795
796 /* keep old buffer if kmalloc should fail */
797 if (new_buff) {
798 memcpy(new_buff, *packet_buff, min_packet_len);
799 kfree(*packet_buff);
800 *packet_buff = new_buff;
801 *packet_buff_len = min_packet_len + additional_packet_len;
802 return true;
803 }
804
805 return false;
806}
807
808/**
809 * batadv_tvlv_container_ogm_append - append tvlv container content to given
810 * OGM packet buffer
811 * @bat_priv: the bat priv with all the soft interface information
812 * @packet_buff: ogm packet buffer
813 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
814 * content
815 * @packet_min_len: ogm header size to be preserved for the OGM itself
816 *
817 * The ogm packet might be enlarged or shrunk depending on the current size
818 * and the size of the to-be-appended tvlv containers.
819 *
820 * Returns size of all appended tvlv containers in bytes.
821 */
822uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
823 unsigned char **packet_buff,
824 int *packet_buff_len,
825 int packet_min_len)
826{
827 struct batadv_tvlv_container *tvlv;
828 struct batadv_tvlv_hdr *tvlv_hdr;
829 uint16_t tvlv_value_len;
830 void *tvlv_value;
831 bool ret;
832
833 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
834 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
835
836 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
837 packet_min_len, tvlv_value_len);
838
839 if (!ret)
840 goto end;
841
842 if (!tvlv_value_len)
843 goto end;
844
845 tvlv_value = (*packet_buff) + packet_min_len;
846
847 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
848 tvlv_hdr = tvlv_value;
849 tvlv_hdr->type = tvlv->tvlv_hdr.type;
850 tvlv_hdr->version = tvlv->tvlv_hdr.version;
851 tvlv_hdr->len = tvlv->tvlv_hdr.len;
852 tvlv_value = tvlv_hdr + 1;
853 memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
854 tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
855 }
856
857end:
858 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
859 return tvlv_value_len;
860}
861
862/**
863 * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
864 * appropriate handlers
865 * @bat_priv: the bat priv with all the soft interface information
866 * @tvlv_handler: tvlv callback function handling the tvlv content
867 * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
868 * @orig_node: orig node emitting the ogm packet
869 * @src: source mac address of the unicast packet
870 * @dst: destination mac address of the unicast packet
871 * @tvlv_value: tvlv content
872 * @tvlv_value_len: tvlv content length
873 *
874 * Returns success if handler was not found or the return value of the handler
875 * callback.
876 */
877static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
878 struct batadv_tvlv_handler *tvlv_handler,
879 bool ogm_source,
880 struct batadv_orig_node *orig_node,
881 uint8_t *src, uint8_t *dst,
882 void *tvlv_value, uint16_t tvlv_value_len)
883{
884 if (!tvlv_handler)
885 return NET_RX_SUCCESS;
886
887 if (ogm_source) {
888 if (!tvlv_handler->ogm_handler)
889 return NET_RX_SUCCESS;
890
891 if (!orig_node)
892 return NET_RX_SUCCESS;
893
894 tvlv_handler->ogm_handler(bat_priv, orig_node,
895 BATADV_NO_FLAGS,
896 tvlv_value, tvlv_value_len);
897 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
898 } else {
899 if (!src)
900 return NET_RX_SUCCESS;
901
902 if (!dst)
903 return NET_RX_SUCCESS;
904
905 if (!tvlv_handler->unicast_handler)
906 return NET_RX_SUCCESS;
907
908 return tvlv_handler->unicast_handler(bat_priv, src,
909 dst, tvlv_value,
910 tvlv_value_len);
911 }
912
913 return NET_RX_SUCCESS;
914}
915
916/**
917 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
918 * appropriate handlers
919 * @bat_priv: the bat priv with all the soft interface information
920 * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
921 * @orig_node: orig node emitting the ogm packet
922 * @src: source mac address of the unicast packet
923 * @dst: destination mac address of the unicast packet
924 * @tvlv_value: tvlv content
925 * @tvlv_value_len: tvlv content length
926 *
927 * Returns success when processing an OGM or the return value of all called
928 * handler callbacks.
929 */
930int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
931 bool ogm_source,
932 struct batadv_orig_node *orig_node,
933 uint8_t *src, uint8_t *dst,
934 void *tvlv_value, uint16_t tvlv_value_len)
935{
936 struct batadv_tvlv_handler *tvlv_handler;
937 struct batadv_tvlv_hdr *tvlv_hdr;
938 uint16_t tvlv_value_cont_len;
939 uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
940 int ret = NET_RX_SUCCESS;
941
942 while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
943 tvlv_hdr = tvlv_value;
944 tvlv_value_cont_len = ntohs(tvlv_hdr->len);
945 tvlv_value = tvlv_hdr + 1;
946 tvlv_value_len -= sizeof(*tvlv_hdr);
947
948 if (tvlv_value_cont_len > tvlv_value_len)
949 break;
950
951 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
952 tvlv_hdr->type,
953 tvlv_hdr->version);
954
955 ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
956 ogm_source, orig_node,
957 src, dst, tvlv_value,
958 tvlv_value_cont_len);
959 if (tvlv_handler)
960 batadv_tvlv_handler_free_ref(tvlv_handler);
961 tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len;
962 tvlv_value_len -= tvlv_value_cont_len;
963 }
964
965 if (!ogm_source)
966 return ret;
967
968 rcu_read_lock();
969 hlist_for_each_entry_rcu(tvlv_handler,
970 &bat_priv->tvlv.handler_list, list) {
971 if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
972 !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
973 tvlv_handler->ogm_handler(bat_priv, orig_node,
974 cifnotfound, NULL, 0);
975
976 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
977 }
978 rcu_read_unlock();
979
980 return NET_RX_SUCCESS;
981}
982
983/**
984 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
985 * handlers
986 * @bat_priv: the bat priv with all the soft interface information
987 * @batadv_ogm_packet: ogm packet containing the tvlv containers
988 * @orig_node: orig node emitting the ogm packet
989 */
990void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
991 struct batadv_ogm_packet *batadv_ogm_packet,
992 struct batadv_orig_node *orig_node)
993{
994 void *tvlv_value;
995 uint16_t tvlv_value_len;
996
997 if (!batadv_ogm_packet)
998 return;
999
1000 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
1001 if (!tvlv_value_len)
1002 return;
1003
1004 tvlv_value = batadv_ogm_packet + 1;
1005
1006 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
1007 tvlv_value, tvlv_value_len);
1008}
1009
1010/**
1011 * batadv_tvlv_handler_register - register tvlv handler based on the provided
1012 * type and version (both need to match) for ogm tvlv payload and/or unicast
1013 * payload
1014 * @bat_priv: the bat priv with all the soft interface information
1015 * @optr: ogm tvlv handler callback function. This function receives the orig
1016 * node, flags and the tvlv content as argument to process.
1017 * @uptr: unicast tvlv handler callback function. This function receives the
1018 * source & destination of the unicast packet as well as the tvlv content
1019 * to process.
1020 * @type: tvlv handler type to be registered
1021 * @version: tvlv handler version to be registered
1022 * @flags: flags to enable or disable TVLV API behavior
1023 */
1024void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
1025 void (*optr)(struct batadv_priv *bat_priv,
1026 struct batadv_orig_node *orig,
1027 uint8_t flags,
1028 void *tvlv_value,
1029 uint16_t tvlv_value_len),
1030 int (*uptr)(struct batadv_priv *bat_priv,
1031 uint8_t *src, uint8_t *dst,
1032 void *tvlv_value,
1033 uint16_t tvlv_value_len),
1034 uint8_t type, uint8_t version, uint8_t flags)
1035{
1036 struct batadv_tvlv_handler *tvlv_handler;
1037
1038 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
1039 if (tvlv_handler) {
1040 batadv_tvlv_handler_free_ref(tvlv_handler);
1041 return;
1042 }
1043
1044 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
1045 if (!tvlv_handler)
1046 return;
1047
1048 tvlv_handler->ogm_handler = optr;
1049 tvlv_handler->unicast_handler = uptr;
1050 tvlv_handler->type = type;
1051 tvlv_handler->version = version;
1052 tvlv_handler->flags = flags;
1053 atomic_set(&tvlv_handler->refcount, 1);
1054 INIT_HLIST_NODE(&tvlv_handler->list);
1055
1056 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1057 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
1058 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1059}
1060
1061/**
1062 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
1063 * provided type and version (both need to match)
1064 * @bat_priv: the bat priv with all the soft interface information
1065 * @type: tvlv handler type to be unregistered
1066 * @version: tvlv handler version to be unregistered
1067 */
1068void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
1069 uint8_t type, uint8_t version)
1070{
1071 struct batadv_tvlv_handler *tvlv_handler;
1072
1073 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
1074 if (!tvlv_handler)
1075 return;
1076
1077 batadv_tvlv_handler_free_ref(tvlv_handler);
1078 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1079 hlist_del_rcu(&tvlv_handler->list);
1080 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1081 batadv_tvlv_handler_free_ref(tvlv_handler);
1082}
1083
1084/**
1085 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
1086 * specified host
1087 * @bat_priv: the bat priv with all the soft interface information
1088 * @src: source mac address of the unicast packet
1089 * @dst: destination mac address of the unicast packet
1090 * @type: tvlv type
1091 * @version: tvlv version
1092 * @tvlv_value: tvlv content
1093 * @tvlv_value_len: tvlv content length
1094 */
1095void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src,
1096 uint8_t *dst, uint8_t type, uint8_t version,
1097 void *tvlv_value, uint16_t tvlv_value_len)
1098{
1099 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1100 struct batadv_tvlv_hdr *tvlv_hdr;
1101 struct batadv_orig_node *orig_node;
1102 struct sk_buff *skb = NULL;
1103 unsigned char *tvlv_buff;
1104 unsigned int tvlv_len;
1105 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
1106 bool ret = false;
1107
1108 orig_node = batadv_orig_hash_find(bat_priv, dst);
1109 if (!orig_node)
1110 goto out;
1111
1112 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
1113
1114 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
1115 if (!skb)
1116 goto out;
1117
1118 skb->priority = TC_PRIO_CONTROL;
1119 skb_reserve(skb, ETH_HLEN);
1120 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
1121 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
1122 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
1123 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
1124 unicast_tvlv_packet->ttl = BATADV_TTL;
1125 unicast_tvlv_packet->reserved = 0;
1126 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
1127 unicast_tvlv_packet->align = 0;
1128 memcpy(unicast_tvlv_packet->src, src, ETH_ALEN);
1129 memcpy(unicast_tvlv_packet->dst, dst, ETH_ALEN);
1130
1131 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
1132 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
1133 tvlv_hdr->version = version;
1134 tvlv_hdr->type = type;
1135 tvlv_hdr->len = htons(tvlv_value_len);
1136 tvlv_buff += sizeof(*tvlv_hdr);
1137 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
1138
1139 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
1140 ret = true;
1141
1142out:
1143 if (skb && !ret)
1144 kfree_skb(skb);
1145 if (orig_node)
1146 batadv_orig_node_free_ref(orig_node);
1147}
1148
1149/**
1150 * batadv_get_vid - extract the VLAN identifier from skb if any
1151 * @skb: the buffer containing the packet
1152 * @header_len: length of the batman header preceding the ethernet header
1153 *
1154 * If the packet embedded in the skb is vlan tagged this function returns the
1155 * VID with the BATADV_VLAN_HAS_TAG flag. Otherwise BATADV_NO_FLAGS is returned.
1156 */
1157unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len)
1158{
1159 struct ethhdr *ethhdr = (struct ethhdr *)(skb->data + header_len);
1160 struct vlan_ethhdr *vhdr;
1161 unsigned short vid;
1162
1163 if (ethhdr->h_proto != htons(ETH_P_8021Q))
1164 return BATADV_NO_FLAGS;
1165
1166 if (!pskb_may_pull(skb, header_len + VLAN_ETH_HLEN))
1167 return BATADV_NO_FLAGS;
1168
1169 vhdr = (struct vlan_ethhdr *)(skb->data + header_len);
1170 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
1171 vid |= BATADV_VLAN_HAS_TAG;
1172
1173 return vid;
1174}
1175
539static int batadv_param_set_ra(const char *val, const struct kernel_param *kp) 1176static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
540{ 1177{
541 struct batadv_algo_ops *bat_algo_ops; 1178 struct batadv_algo_ops *bat_algo_ops;
diff --git a/net/batman-adv/main.h b/net/batman-adv/main.h
index 24675523930f..f94f287b8670 100644
--- a/net/batman-adv/main.h
+++ b/net/batman-adv/main.h
@@ -20,13 +20,13 @@
20#ifndef _NET_BATMAN_ADV_MAIN_H_ 20#ifndef _NET_BATMAN_ADV_MAIN_H_
21#define _NET_BATMAN_ADV_MAIN_H_ 21#define _NET_BATMAN_ADV_MAIN_H_
22 22
23#define BATADV_DRIVER_AUTHOR "Marek Lindner <lindner_marek@yahoo.de>, " \ 23#define BATADV_DRIVER_AUTHOR "Marek Lindner <mareklindner@neomailbox.ch>, " \
24 "Simon Wunderlich <siwu@hrz.tu-chemnitz.de>" 24 "Simon Wunderlich <sw@simonwunderlich.de>"
25#define BATADV_DRIVER_DESC "B.A.T.M.A.N. advanced" 25#define BATADV_DRIVER_DESC "B.A.T.M.A.N. advanced"
26#define BATADV_DRIVER_DEVICE "batman-adv" 26#define BATADV_DRIVER_DEVICE "batman-adv"
27 27
28#ifndef BATADV_SOURCE_VERSION 28#ifndef BATADV_SOURCE_VERSION
29#define BATADV_SOURCE_VERSION "2013.4.0" 29#define BATADV_SOURCE_VERSION "2013.5.0"
30#endif 30#endif
31 31
32/* B.A.T.M.A.N. parameters */ 32/* B.A.T.M.A.N. parameters */
@@ -86,7 +86,11 @@
86/* numbers of originator to contact for any PUT/GET DHT operation */ 86/* numbers of originator to contact for any PUT/GET DHT operation */
87#define BATADV_DAT_CANDIDATES_NUM 3 87#define BATADV_DAT_CANDIDATES_NUM 3
88 88
89#define BATADV_VIS_INTERVAL 5000 /* 5 seconds */ 89/**
90 * BATADV_TQ_SIMILARITY_THRESHOLD - TQ points that a secondary metric can differ
91 * at most from the primary one in order to be still considered acceptable
92 */
93#define BATADV_TQ_SIMILARITY_THRESHOLD 50
90 94
91/* how much worse secondary interfaces may be to be considered as bonding 95/* how much worse secondary interfaces may be to be considered as bonding
92 * candidates 96 * candidates
@@ -133,6 +137,15 @@ enum batadv_uev_type {
133 137
134#define BATADV_GW_THRESHOLD 50 138#define BATADV_GW_THRESHOLD 50
135 139
140/* Number of fragment chains for each orig_node */
141#define BATADV_FRAG_BUFFER_COUNT 8
142/* Maximum number of fragments for one packet */
143#define BATADV_FRAG_MAX_FRAGMENTS 16
144/* Maxumim size of each fragment */
145#define BATADV_FRAG_MAX_FRAG_SIZE 1400
146/* Time to keep fragments while waiting for rest of the fragments */
147#define BATADV_FRAG_TIMEOUT 10000
148
136#define BATADV_DAT_CANDIDATE_NOT_FOUND 0 149#define BATADV_DAT_CANDIDATE_NOT_FOUND 0
137#define BATADV_DAT_CANDIDATE_ORIG 1 150#define BATADV_DAT_CANDIDATE_ORIG 1
138 151
@@ -160,15 +173,9 @@ enum batadv_uev_type {
160#include <net/rtnetlink.h> 173#include <net/rtnetlink.h>
161#include <linux/jiffies.h> 174#include <linux/jiffies.h>
162#include <linux/seq_file.h> 175#include <linux/seq_file.h>
163#include "types.h" 176#include <linux/if_vlan.h>
164 177
165/** 178#include "types.h"
166 * batadv_vlan_flags - flags for the four MSB of any vlan ID field
167 * @BATADV_VLAN_HAS_TAG: whether the field contains a valid vlan tag or not
168 */
169enum batadv_vlan_flags {
170 BATADV_VLAN_HAS_TAG = BIT(15),
171};
172 179
173#define BATADV_PRINT_VID(vid) (vid & BATADV_VLAN_HAS_TAG ? \ 180#define BATADV_PRINT_VID(vid) (vid & BATADV_VLAN_HAS_TAG ? \
174 (int)(vid & VLAN_VID_MASK) : -1) 181 (int)(vid & VLAN_VID_MASK) : -1)
@@ -184,6 +191,7 @@ void batadv_mesh_free(struct net_device *soft_iface);
184int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr); 191int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr);
185struct batadv_hard_iface * 192struct batadv_hard_iface *
186batadv_seq_print_text_primary_if_get(struct seq_file *seq); 193batadv_seq_print_text_primary_if_get(struct seq_file *seq);
194int batadv_max_header_len(void);
187void batadv_skb_set_priority(struct sk_buff *skb, int offset); 195void batadv_skb_set_priority(struct sk_buff *skb, int offset);
188int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev, 196int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
189 struct packet_type *ptype, 197 struct packet_type *ptype,
@@ -326,4 +334,40 @@ static inline uint64_t batadv_sum_counter(struct batadv_priv *bat_priv,
326 */ 334 */
327#define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0])) 335#define BATADV_SKB_CB(__skb) ((struct batadv_skb_cb *)&((__skb)->cb[0]))
328 336
337void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
338 uint8_t type, uint8_t version,
339 void *tvlv_value, uint16_t tvlv_value_len);
340uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
341 unsigned char **packet_buff,
342 int *packet_buff_len,
343 int packet_min_len);
344void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
345 struct batadv_ogm_packet *batadv_ogm_packet,
346 struct batadv_orig_node *orig_node);
347void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
348 uint8_t type, uint8_t version);
349
350void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
351 void (*optr)(struct batadv_priv *bat_priv,
352 struct batadv_orig_node *orig,
353 uint8_t flags,
354 void *tvlv_value,
355 uint16_t tvlv_value_len),
356 int (*uptr)(struct batadv_priv *bat_priv,
357 uint8_t *src, uint8_t *dst,
358 void *tvlv_value,
359 uint16_t tvlv_value_len),
360 uint8_t type, uint8_t version, uint8_t flags);
361void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
362 uint8_t type, uint8_t version);
363int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
364 bool ogm_source,
365 struct batadv_orig_node *orig_node,
366 uint8_t *src, uint8_t *dst,
367 void *tvlv_buff, uint16_t tvlv_buff_len);
368void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src,
369 uint8_t *dst, uint8_t type, uint8_t version,
370 void *tvlv_value, uint16_t tvlv_value_len);
371unsigned short batadv_get_vid(struct sk_buff *skb, size_t header_len);
372
329#endif /* _NET_BATMAN_ADV_MAIN_H_ */ 373#endif /* _NET_BATMAN_ADV_MAIN_H_ */
diff --git a/net/batman-adv/network-coding.c b/net/batman-adv/network-coding.c
index 4ecc0b6bf8ab..511d7e1eea38 100644
--- a/net/batman-adv/network-coding.c
+++ b/net/batman-adv/network-coding.c
@@ -59,6 +59,59 @@ static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
59} 59}
60 60
61/** 61/**
62 * batadv_nc_tvlv_container_update - update the network coding tvlv container
63 * after network coding setting change
64 * @bat_priv: the bat priv with all the soft interface information
65 */
66static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
67{
68 char nc_mode;
69
70 nc_mode = atomic_read(&bat_priv->network_coding);
71
72 switch (nc_mode) {
73 case 0:
74 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
75 break;
76 case 1:
77 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
78 NULL, 0);
79 break;
80 }
81}
82
83/**
84 * batadv_nc_status_update - update the network coding tvlv container after
85 * network coding setting change
86 * @net_dev: the soft interface net device
87 */
88void batadv_nc_status_update(struct net_device *net_dev)
89{
90 struct batadv_priv *bat_priv = netdev_priv(net_dev);
91 batadv_nc_tvlv_container_update(bat_priv);
92}
93
94/**
95 * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
96 * @bat_priv: the bat priv with all the soft interface information
97 * @orig: the orig_node of the ogm
98 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
99 * @tvlv_value: tvlv buffer containing the gateway data
100 * @tvlv_value_len: tvlv buffer length
101 */
102static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
103 struct batadv_orig_node *orig,
104 uint8_t flags,
105 void *tvlv_value,
106 uint16_t tvlv_value_len)
107{
108 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
109 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_NC;
110 else
111 orig->capabilities |= BATADV_ORIG_CAPA_HAS_NC;
112}
113
114/**
62 * batadv_nc_mesh_init - initialise coding hash table and start house keeping 115 * batadv_nc_mesh_init - initialise coding hash table and start house keeping
63 * @bat_priv: the bat priv with all the soft interface information 116 * @bat_priv: the bat priv with all the soft interface information
64 */ 117 */
@@ -87,6 +140,10 @@ int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
87 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker); 140 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
88 batadv_nc_start_timer(bat_priv); 141 batadv_nc_start_timer(bat_priv);
89 142
143 batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
144 NULL, BATADV_TVLV_NC, 1,
145 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
146 batadv_nc_tvlv_container_update(bat_priv);
90 return 0; 147 return 0;
91 148
92err: 149err:
@@ -665,7 +722,7 @@ static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
665{ 722{
666 if (orig_node->last_real_seqno != ntohl(ogm_packet->seqno)) 723 if (orig_node->last_real_seqno != ntohl(ogm_packet->seqno))
667 return false; 724 return false;
668 if (orig_node->last_ttl != ogm_packet->header.ttl + 1) 725 if (orig_node->last_ttl != ogm_packet->ttl + 1)
669 return false; 726 return false;
670 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender)) 727 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
671 return false; 728 return false;
@@ -802,6 +859,10 @@ void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
802 if (!atomic_read(&bat_priv->network_coding)) 859 if (!atomic_read(&bat_priv->network_coding))
803 goto out; 860 goto out;
804 861
862 /* check if orig node is network coding enabled */
863 if (!(orig_node->capabilities & BATADV_ORIG_CAPA_HAS_NC))
864 goto out;
865
805 /* accept ogms from 'good' neighbors and single hop neighbors */ 866 /* accept ogms from 'good' neighbors and single hop neighbors */
806 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) && 867 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
807 !is_single_hop_neigh) 868 !is_single_hop_neigh)
@@ -942,7 +1003,7 @@ static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
942 struct batadv_nc_packet *nc_packet, 1003 struct batadv_nc_packet *nc_packet,
943 struct batadv_neigh_node *neigh_node) 1004 struct batadv_neigh_node *neigh_node)
944{ 1005{
945 uint8_t tq_weighted_neigh, tq_weighted_coding; 1006 uint8_t tq_weighted_neigh, tq_weighted_coding, tq_tmp;
946 struct sk_buff *skb_dest, *skb_src; 1007 struct sk_buff *skb_dest, *skb_src;
947 struct batadv_unicast_packet *packet1; 1008 struct batadv_unicast_packet *packet1;
948 struct batadv_unicast_packet *packet2; 1009 struct batadv_unicast_packet *packet2;
@@ -967,8 +1028,10 @@ static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
967 if (!router_coding) 1028 if (!router_coding)
968 goto out; 1029 goto out;
969 1030
970 tq_weighted_neigh = batadv_nc_random_weight_tq(router_neigh->tq_avg); 1031 tq_tmp = batadv_nc_random_weight_tq(router_neigh->bat_iv.tq_avg);
971 tq_weighted_coding = batadv_nc_random_weight_tq(router_coding->tq_avg); 1032 tq_weighted_neigh = tq_tmp;
1033 tq_tmp = batadv_nc_random_weight_tq(router_coding->bat_iv.tq_avg);
1034 tq_weighted_coding = tq_tmp;
972 1035
973 /* Select one destination for the MAC-header dst-field based on 1036 /* Select one destination for the MAC-header dst-field based on
974 * weighted TQ-values. 1037 * weighted TQ-values.
@@ -1019,9 +1082,9 @@ static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
1019 coded_packet = (struct batadv_coded_packet *)skb_dest->data; 1082 coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1020 skb_reset_mac_header(skb_dest); 1083 skb_reset_mac_header(skb_dest);
1021 1084
1022 coded_packet->header.packet_type = BATADV_CODED; 1085 coded_packet->packet_type = BATADV_CODED;
1023 coded_packet->header.version = BATADV_COMPAT_VERSION; 1086 coded_packet->version = BATADV_COMPAT_VERSION;
1024 coded_packet->header.ttl = packet1->header.ttl; 1087 coded_packet->ttl = packet1->ttl;
1025 1088
1026 /* Info about first unicast packet */ 1089 /* Info about first unicast packet */
1027 memcpy(coded_packet->first_source, first_source, ETH_ALEN); 1090 memcpy(coded_packet->first_source, first_source, ETH_ALEN);
@@ -1034,7 +1097,7 @@ static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
1034 memcpy(coded_packet->second_source, second_source, ETH_ALEN); 1097 memcpy(coded_packet->second_source, second_source, ETH_ALEN);
1035 memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN); 1098 memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
1036 coded_packet->second_crc = packet_id2; 1099 coded_packet->second_crc = packet_id2;
1037 coded_packet->second_ttl = packet2->header.ttl; 1100 coded_packet->second_ttl = packet2->ttl;
1038 coded_packet->second_ttvn = packet2->ttvn; 1101 coded_packet->second_ttvn = packet2->ttvn;
1039 coded_packet->coded_len = htons(coding_len); 1102 coded_packet->coded_len = htons(coding_len);
1040 1103
@@ -1389,7 +1452,7 @@ bool batadv_nc_skb_forward(struct sk_buff *skb,
1389 /* We only handle unicast packets */ 1452 /* We only handle unicast packets */
1390 payload = skb_network_header(skb); 1453 payload = skb_network_header(skb);
1391 packet = (struct batadv_unicast_packet *)payload; 1454 packet = (struct batadv_unicast_packet *)payload;
1392 if (packet->header.packet_type != BATADV_UNICAST) 1455 if (packet->packet_type != BATADV_UNICAST)
1393 goto out; 1456 goto out;
1394 1457
1395 /* Try to find a coding opportunity and send the skb if one is found */ 1458 /* Try to find a coding opportunity and send the skb if one is found */
@@ -1442,7 +1505,7 @@ void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1442 /* Check for supported packet type */ 1505 /* Check for supported packet type */
1443 payload = skb_network_header(skb); 1506 payload = skb_network_header(skb);
1444 packet = (struct batadv_unicast_packet *)payload; 1507 packet = (struct batadv_unicast_packet *)payload;
1445 if (packet->header.packet_type != BATADV_UNICAST) 1508 if (packet->packet_type != BATADV_UNICAST)
1446 goto out; 1509 goto out;
1447 1510
1448 /* Find existing nc_path or create a new */ 1511 /* Find existing nc_path or create a new */
@@ -1560,7 +1623,7 @@ batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
1560 ttvn = coded_packet_tmp.second_ttvn; 1623 ttvn = coded_packet_tmp.second_ttvn;
1561 } else { 1624 } else {
1562 orig_dest = coded_packet_tmp.first_orig_dest; 1625 orig_dest = coded_packet_tmp.first_orig_dest;
1563 ttl = coded_packet_tmp.header.ttl; 1626 ttl = coded_packet_tmp.ttl;
1564 ttvn = coded_packet_tmp.first_ttvn; 1627 ttvn = coded_packet_tmp.first_ttvn;
1565 } 1628 }
1566 1629
@@ -1585,9 +1648,9 @@ batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
1585 1648
1586 /* Create decoded unicast packet */ 1649 /* Create decoded unicast packet */
1587 unicast_packet = (struct batadv_unicast_packet *)skb->data; 1650 unicast_packet = (struct batadv_unicast_packet *)skb->data;
1588 unicast_packet->header.packet_type = BATADV_UNICAST; 1651 unicast_packet->packet_type = BATADV_UNICAST;
1589 unicast_packet->header.version = BATADV_COMPAT_VERSION; 1652 unicast_packet->version = BATADV_COMPAT_VERSION;
1590 unicast_packet->header.ttl = ttl; 1653 unicast_packet->ttl = ttl;
1591 memcpy(unicast_packet->dest, orig_dest, ETH_ALEN); 1654 memcpy(unicast_packet->dest, orig_dest, ETH_ALEN);
1592 unicast_packet->ttvn = ttvn; 1655 unicast_packet->ttvn = ttvn;
1593 1656
@@ -1735,6 +1798,8 @@ free_nc_packet:
1735 */ 1798 */
1736void batadv_nc_mesh_free(struct batadv_priv *bat_priv) 1799void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
1737{ 1800{
1801 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
1802 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
1738 cancel_delayed_work_sync(&bat_priv->nc.work); 1803 cancel_delayed_work_sync(&bat_priv->nc.work);
1739 1804
1740 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL); 1805 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
diff --git a/net/batman-adv/network-coding.h b/net/batman-adv/network-coding.h
index ddfa618e80bf..d4fd315b5261 100644
--- a/net/batman-adv/network-coding.h
+++ b/net/batman-adv/network-coding.h
@@ -22,6 +22,7 @@
22 22
23#ifdef CONFIG_BATMAN_ADV_NC 23#ifdef CONFIG_BATMAN_ADV_NC
24 24
25void batadv_nc_status_update(struct net_device *net_dev);
25int batadv_nc_init(void); 26int batadv_nc_init(void);
26int batadv_nc_mesh_init(struct batadv_priv *bat_priv); 27int batadv_nc_mesh_init(struct batadv_priv *bat_priv);
27void batadv_nc_mesh_free(struct batadv_priv *bat_priv); 28void batadv_nc_mesh_free(struct batadv_priv *bat_priv);
@@ -47,6 +48,10 @@ int batadv_nc_init_debugfs(struct batadv_priv *bat_priv);
47 48
48#else /* ifdef CONFIG_BATMAN_ADV_NC */ 49#else /* ifdef CONFIG_BATMAN_ADV_NC */
49 50
51static inline void batadv_nc_status_update(struct net_device *net_dev)
52{
53}
54
50static inline int batadv_nc_init(void) 55static inline int batadv_nc_init(void)
51{ 56{
52 return 0; 57 return 0;
diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c
index f50553a7de62..8ab14340d10f 100644
--- a/net/batman-adv/originator.c
+++ b/net/batman-adv/originator.c
@@ -25,10 +25,10 @@
25#include "routing.h" 25#include "routing.h"
26#include "gateway_client.h" 26#include "gateway_client.h"
27#include "hard-interface.h" 27#include "hard-interface.h"
28#include "unicast.h"
29#include "soft-interface.h" 28#include "soft-interface.h"
30#include "bridge_loop_avoidance.h" 29#include "bridge_loop_avoidance.h"
31#include "network-coding.h" 30#include "network-coding.h"
31#include "fragmentation.h"
32 32
33/* hash class keys */ 33/* hash class keys */
34static struct lock_class_key batadv_orig_hash_lock_class_key; 34static struct lock_class_key batadv_orig_hash_lock_class_key;
@@ -36,7 +36,7 @@ static struct lock_class_key batadv_orig_hash_lock_class_key;
36static void batadv_purge_orig(struct work_struct *work); 36static void batadv_purge_orig(struct work_struct *work);
37 37
38/* returns 1 if they are the same originator */ 38/* returns 1 if they are the same originator */
39static int batadv_compare_orig(const struct hlist_node *node, const void *data2) 39int batadv_compare_orig(const struct hlist_node *node, const void *data2)
40{ 40{
41 const void *data1 = container_of(node, struct batadv_orig_node, 41 const void *data1 = container_of(node, struct batadv_orig_node,
42 hash_entry); 42 hash_entry);
@@ -44,6 +44,88 @@ static int batadv_compare_orig(const struct hlist_node *node, const void *data2)
44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); 44 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45} 45}
46 46
47/**
48 * batadv_orig_node_vlan_get - get an orig_node_vlan object
49 * @orig_node: the originator serving the VLAN
50 * @vid: the VLAN identifier
51 *
52 * Returns the vlan object identified by vid and belonging to orig_node or NULL
53 * if it does not exist.
54 */
55struct batadv_orig_node_vlan *
56batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
57 unsigned short vid)
58{
59 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
60
61 rcu_read_lock();
62 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
63 if (tmp->vid != vid)
64 continue;
65
66 if (!atomic_inc_not_zero(&tmp->refcount))
67 continue;
68
69 vlan = tmp;
70
71 break;
72 }
73 rcu_read_unlock();
74
75 return vlan;
76}
77
78/**
79 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
80 * object
81 * @orig_node: the originator serving the VLAN
82 * @vid: the VLAN identifier
83 *
84 * Returns NULL in case of failure or the vlan object identified by vid and
85 * belonging to orig_node otherwise. The object is created and added to the list
86 * if it does not exist.
87 *
88 * The object is returned with refcounter increased by 1.
89 */
90struct batadv_orig_node_vlan *
91batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
92 unsigned short vid)
93{
94 struct batadv_orig_node_vlan *vlan;
95
96 spin_lock_bh(&orig_node->vlan_list_lock);
97
98 /* first look if an object for this vid already exists */
99 vlan = batadv_orig_node_vlan_get(orig_node, vid);
100 if (vlan)
101 goto out;
102
103 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
104 if (!vlan)
105 goto out;
106
107 atomic_set(&vlan->refcount, 2);
108 vlan->vid = vid;
109
110 list_add_rcu(&vlan->list, &orig_node->vlan_list);
111
112out:
113 spin_unlock_bh(&orig_node->vlan_list_lock);
114
115 return vlan;
116}
117
118/**
119 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
120 * the originator-vlan object
121 * @orig_vlan: the originator-vlan object to release
122 */
123void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
124{
125 if (atomic_dec_and_test(&orig_vlan->refcount))
126 kfree_rcu(orig_vlan, rcu);
127}
128
47int batadv_originator_init(struct batadv_priv *bat_priv) 129int batadv_originator_init(struct batadv_priv *bat_priv)
48{ 130{
49 if (bat_priv->orig_hash) 131 if (bat_priv->orig_hash)
@@ -90,11 +172,20 @@ batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
90 return router; 172 return router;
91} 173}
92 174
175/**
176 * batadv_neigh_node_new - create and init a new neigh_node object
177 * @hard_iface: the interface where the neighbour is connected to
178 * @neigh_addr: the mac address of the neighbour interface
179 * @orig_node: originator object representing the neighbour
180 *
181 * Allocates a new neigh_node object and initialises all the generic fields.
182 * Returns the new object or NULL on failure.
183 */
93struct batadv_neigh_node * 184struct batadv_neigh_node *
94batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, 185batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
95 const uint8_t *neigh_addr) 186 const uint8_t *neigh_addr,
187 struct batadv_orig_node *orig_node)
96{ 188{
97 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
98 struct batadv_neigh_node *neigh_node; 189 struct batadv_neigh_node *neigh_node;
99 190
100 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC); 191 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
@@ -104,15 +195,14 @@ batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
104 INIT_HLIST_NODE(&neigh_node->list); 195 INIT_HLIST_NODE(&neigh_node->list);
105 196
106 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN); 197 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
107 spin_lock_init(&neigh_node->lq_update_lock); 198 neigh_node->if_incoming = hard_iface;
199 neigh_node->orig_node = orig_node;
200
201 INIT_LIST_HEAD(&neigh_node->bonding_list);
108 202
109 /* extra reference for return */ 203 /* extra reference for return */
110 atomic_set(&neigh_node->refcount, 2); 204 atomic_set(&neigh_node->refcount, 2);
111 205
112 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
113 "Creating new neighbor %pM on interface %s\n", neigh_addr,
114 hard_iface->net_dev->name);
115
116out: 206out:
117 return neigh_node; 207 return neigh_node;
118} 208}
@@ -146,13 +236,15 @@ static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
146 /* Free nc_nodes */ 236 /* Free nc_nodes */
147 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL); 237 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
148 238
149 batadv_frag_list_free(&orig_node->frag_list); 239 batadv_frag_purge_orig(orig_node, NULL);
150 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, 240
241 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
151 "originator timed out"); 242 "originator timed out");
152 243
244 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
245 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
246
153 kfree(orig_node->tt_buff); 247 kfree(orig_node->tt_buff);
154 kfree(orig_node->bcast_own);
155 kfree(orig_node->bcast_own_sum);
156 kfree(orig_node); 248 kfree(orig_node);
157} 249}
158 250
@@ -210,20 +302,22 @@ void batadv_originator_free(struct batadv_priv *bat_priv)
210 batadv_hash_destroy(hash); 302 batadv_hash_destroy(hash);
211} 303}
212 304
213/* this function finds or creates an originator entry for the given 305/**
214 * address if it does not exits 306 * batadv_orig_node_new - creates a new orig_node
307 * @bat_priv: the bat priv with all the soft interface information
308 * @addr: the mac address of the originator
309 *
310 * Creates a new originator object and initialise all the generic fields.
311 * The new object is not added to the originator list.
312 * Returns the newly created object or NULL on failure.
215 */ 313 */
216struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv, 314struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
217 const uint8_t *addr) 315 const uint8_t *addr)
218{ 316{
219 struct batadv_orig_node *orig_node; 317 struct batadv_orig_node *orig_node;
220 int size; 318 struct batadv_orig_node_vlan *vlan;
221 int hash_added;
222 unsigned long reset_time; 319 unsigned long reset_time;
223 320 int i;
224 orig_node = batadv_orig_hash_find(bat_priv, addr);
225 if (orig_node)
226 return orig_node;
227 321
228 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, 322 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
229 "Creating new originator: %pM\n", addr); 323 "Creating new originator: %pM\n", addr);
@@ -234,10 +328,12 @@ struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
234 328
235 INIT_HLIST_HEAD(&orig_node->neigh_list); 329 INIT_HLIST_HEAD(&orig_node->neigh_list);
236 INIT_LIST_HEAD(&orig_node->bond_list); 330 INIT_LIST_HEAD(&orig_node->bond_list);
237 spin_lock_init(&orig_node->ogm_cnt_lock); 331 INIT_LIST_HEAD(&orig_node->vlan_list);
238 spin_lock_init(&orig_node->bcast_seqno_lock); 332 spin_lock_init(&orig_node->bcast_seqno_lock);
239 spin_lock_init(&orig_node->neigh_list_lock); 333 spin_lock_init(&orig_node->neigh_list_lock);
240 spin_lock_init(&orig_node->tt_buff_lock); 334 spin_lock_init(&orig_node->tt_buff_lock);
335 spin_lock_init(&orig_node->tt_lock);
336 spin_lock_init(&orig_node->vlan_list_lock);
241 337
242 batadv_nc_init_orig(orig_node); 338 batadv_nc_init_orig(orig_node);
243 339
@@ -249,43 +345,32 @@ struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv,
249 memcpy(orig_node->orig, addr, ETH_ALEN); 345 memcpy(orig_node->orig, addr, ETH_ALEN);
250 batadv_dat_init_orig_node_addr(orig_node); 346 batadv_dat_init_orig_node_addr(orig_node);
251 orig_node->router = NULL; 347 orig_node->router = NULL;
252 orig_node->tt_crc = 0;
253 atomic_set(&orig_node->last_ttvn, 0); 348 atomic_set(&orig_node->last_ttvn, 0);
254 orig_node->tt_buff = NULL; 349 orig_node->tt_buff = NULL;
255 orig_node->tt_buff_len = 0; 350 orig_node->tt_buff_len = 0;
256 atomic_set(&orig_node->tt_size, 0);
257 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS); 351 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
258 orig_node->bcast_seqno_reset = reset_time; 352 orig_node->bcast_seqno_reset = reset_time;
259 orig_node->batman_seqno_reset = reset_time; 353 orig_node->batman_seqno_reset = reset_time;
260 354
261 atomic_set(&orig_node->bond_candidates, 0); 355 atomic_set(&orig_node->bond_candidates, 0);
262 356
263 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; 357 /* create a vlan object for the "untagged" LAN */
264 358 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
265 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC); 359 if (!vlan)
266 if (!orig_node->bcast_own)
267 goto free_orig_node; 360 goto free_orig_node;
361 /* batadv_orig_node_vlan_new() increases the refcounter.
362 * Immediately release vlan since it is not needed anymore in this
363 * context
364 */
365 batadv_orig_node_vlan_free_ref(vlan);
268 366
269 size = bat_priv->num_ifaces * sizeof(uint8_t); 367 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
270 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC); 368 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
271 369 spin_lock_init(&orig_node->fragments[i].lock);
272 INIT_LIST_HEAD(&orig_node->frag_list); 370 orig_node->fragments[i].size = 0;
273 orig_node->last_frag_packet = 0; 371 }
274
275 if (!orig_node->bcast_own_sum)
276 goto free_bcast_own;
277
278 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
279 batadv_choose_orig, orig_node,
280 &orig_node->hash_entry);
281 if (hash_added != 0)
282 goto free_bcast_own_sum;
283 372
284 return orig_node; 373 return orig_node;
285free_bcast_own_sum:
286 kfree(orig_node->bcast_own_sum);
287free_bcast_own:
288 kfree(orig_node->bcast_own);
289free_orig_node: 374free_orig_node:
290 kfree(orig_node); 375 kfree(orig_node);
291 return NULL; 376 return NULL;
@@ -294,15 +379,16 @@ free_orig_node:
294static bool 379static bool
295batadv_purge_orig_neighbors(struct batadv_priv *bat_priv, 380batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
296 struct batadv_orig_node *orig_node, 381 struct batadv_orig_node *orig_node,
297 struct batadv_neigh_node **best_neigh_node) 382 struct batadv_neigh_node **best_neigh)
298{ 383{
384 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
299 struct hlist_node *node_tmp; 385 struct hlist_node *node_tmp;
300 struct batadv_neigh_node *neigh_node; 386 struct batadv_neigh_node *neigh_node;
301 bool neigh_purged = false; 387 bool neigh_purged = false;
302 unsigned long last_seen; 388 unsigned long last_seen;
303 struct batadv_hard_iface *if_incoming; 389 struct batadv_hard_iface *if_incoming;
304 390
305 *best_neigh_node = NULL; 391 *best_neigh = NULL;
306 392
307 spin_lock_bh(&orig_node->neigh_list_lock); 393 spin_lock_bh(&orig_node->neigh_list_lock);
308 394
@@ -335,9 +421,12 @@ batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
335 batadv_bonding_candidate_del(orig_node, neigh_node); 421 batadv_bonding_candidate_del(orig_node, neigh_node);
336 batadv_neigh_node_free_ref(neigh_node); 422 batadv_neigh_node_free_ref(neigh_node);
337 } else { 423 } else {
338 if ((!*best_neigh_node) || 424 /* store the best_neighbour if this is the first
339 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg)) 425 * iteration or if a better neighbor has been found
340 *best_neigh_node = neigh_node; 426 */
427 if (!*best_neigh ||
428 bao->bat_neigh_cmp(neigh_node, *best_neigh) > 0)
429 *best_neigh = neigh_node;
341 } 430 }
342 } 431 }
343 432
@@ -388,17 +477,14 @@ static void _batadv_purge_orig(struct batadv_priv *bat_priv)
388 hlist_for_each_entry_safe(orig_node, node_tmp, 477 hlist_for_each_entry_safe(orig_node, node_tmp,
389 head, hash_entry) { 478 head, hash_entry) {
390 if (batadv_purge_orig_node(bat_priv, orig_node)) { 479 if (batadv_purge_orig_node(bat_priv, orig_node)) {
391 if (orig_node->gw_flags) 480 batadv_gw_node_delete(bat_priv, orig_node);
392 batadv_gw_node_delete(bat_priv,
393 orig_node);
394 hlist_del_rcu(&orig_node->hash_entry); 481 hlist_del_rcu(&orig_node->hash_entry);
395 batadv_orig_node_free_ref(orig_node); 482 batadv_orig_node_free_ref(orig_node);
396 continue; 483 continue;
397 } 484 }
398 485
399 if (batadv_has_timed_out(orig_node->last_frag_packet, 486 batadv_frag_purge_orig(orig_node,
400 BATADV_FRAG_TIMEOUT)) 487 batadv_frag_check_entry);
401 batadv_frag_list_free(&orig_node->frag_list);
402 } 488 }
403 spin_unlock_bh(list_lock); 489 spin_unlock_bh(list_lock);
404 } 490 }
@@ -429,100 +515,26 @@ int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
429{ 515{
430 struct net_device *net_dev = (struct net_device *)seq->private; 516 struct net_device *net_dev = (struct net_device *)seq->private;
431 struct batadv_priv *bat_priv = netdev_priv(net_dev); 517 struct batadv_priv *bat_priv = netdev_priv(net_dev);
432 struct batadv_hashtable *hash = bat_priv->orig_hash;
433 struct hlist_head *head;
434 struct batadv_hard_iface *primary_if; 518 struct batadv_hard_iface *primary_if;
435 struct batadv_orig_node *orig_node;
436 struct batadv_neigh_node *neigh_node, *neigh_node_tmp;
437 int batman_count = 0;
438 int last_seen_secs;
439 int last_seen_msecs;
440 unsigned long last_seen_jiffies;
441 uint32_t i;
442 519
443 primary_if = batadv_seq_print_text_primary_if_get(seq); 520 primary_if = batadv_seq_print_text_primary_if_get(seq);
444 if (!primary_if) 521 if (!primary_if)
445 goto out; 522 return 0;
446 523
447 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n", 524 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
448 BATADV_SOURCE_VERSION, primary_if->net_dev->name, 525 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
449 primary_if->net_dev->dev_addr, net_dev->name); 526 primary_if->net_dev->dev_addr, net_dev->name,
450 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", 527 bat_priv->bat_algo_ops->name);
451 "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
452 "Nexthop", "outgoingIF", "Potential nexthops");
453
454 for (i = 0; i < hash->size; i++) {
455 head = &hash->table[i];
456
457 rcu_read_lock();
458 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
459 neigh_node = batadv_orig_node_get_router(orig_node);
460 if (!neigh_node)
461 continue;
462
463 if (neigh_node->tq_avg == 0)
464 goto next;
465
466 last_seen_jiffies = jiffies - orig_node->last_seen;
467 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
468 last_seen_secs = last_seen_msecs / 1000;
469 last_seen_msecs = last_seen_msecs % 1000;
470
471 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
472 orig_node->orig, last_seen_secs,
473 last_seen_msecs, neigh_node->tq_avg,
474 neigh_node->addr,
475 neigh_node->if_incoming->net_dev->name);
476
477 hlist_for_each_entry_rcu(neigh_node_tmp,
478 &orig_node->neigh_list, list) {
479 seq_printf(seq, " %pM (%3i)",
480 neigh_node_tmp->addr,
481 neigh_node_tmp->tq_avg);
482 }
483 528
484 seq_puts(seq, "\n"); 529 batadv_hardif_free_ref(primary_if);
485 batman_count++;
486 530
487next: 531 if (!bat_priv->bat_algo_ops->bat_orig_print) {
488 batadv_neigh_node_free_ref(neigh_node); 532 seq_puts(seq,
489 } 533 "No printing function for this routing protocol\n");
490 rcu_read_unlock(); 534 return 0;
491 } 535 }
492 536
493 if (batman_count == 0) 537 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq);
494 seq_puts(seq, "No batman nodes in range ...\n");
495
496out:
497 if (primary_if)
498 batadv_hardif_free_ref(primary_if);
499 return 0;
500}
501
502static int batadv_orig_node_add_if(struct batadv_orig_node *orig_node,
503 int max_if_num)
504{
505 void *data_ptr;
506 size_t data_size, old_size;
507
508 data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS;
509 old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
510 data_ptr = kmalloc(data_size, GFP_ATOMIC);
511 if (!data_ptr)
512 return -ENOMEM;
513
514 memcpy(data_ptr, orig_node->bcast_own, old_size);
515 kfree(orig_node->bcast_own);
516 orig_node->bcast_own = data_ptr;
517
518 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
519 if (!data_ptr)
520 return -ENOMEM;
521
522 memcpy(data_ptr, orig_node->bcast_own_sum,
523 (max_if_num - 1) * sizeof(uint8_t));
524 kfree(orig_node->bcast_own_sum);
525 orig_node->bcast_own_sum = data_ptr;
526 538
527 return 0; 539 return 0;
528} 540}
@@ -531,6 +543,7 @@ int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
531 int max_if_num) 543 int max_if_num)
532{ 544{
533 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 545 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
546 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
534 struct batadv_hashtable *hash = bat_priv->orig_hash; 547 struct batadv_hashtable *hash = bat_priv->orig_hash;
535 struct hlist_head *head; 548 struct hlist_head *head;
536 struct batadv_orig_node *orig_node; 549 struct batadv_orig_node *orig_node;
@@ -545,10 +558,10 @@ int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
545 558
546 rcu_read_lock(); 559 rcu_read_lock();
547 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 560 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
548 spin_lock_bh(&orig_node->ogm_cnt_lock); 561 ret = 0;
549 ret = batadv_orig_node_add_if(orig_node, max_if_num); 562 if (bao->bat_orig_add_if)
550 spin_unlock_bh(&orig_node->ogm_cnt_lock); 563 ret = bao->bat_orig_add_if(orig_node,
551 564 max_if_num);
552 if (ret == -ENOMEM) 565 if (ret == -ENOMEM)
553 goto err; 566 goto err;
554 } 567 }
@@ -562,54 +575,6 @@ err:
562 return -ENOMEM; 575 return -ENOMEM;
563} 576}
564 577
565static int batadv_orig_node_del_if(struct batadv_orig_node *orig_node,
566 int max_if_num, int del_if_num)
567{
568 void *data_ptr = NULL;
569 int chunk_size;
570
571 /* last interface was removed */
572 if (max_if_num == 0)
573 goto free_bcast_own;
574
575 chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
576 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
577 if (!data_ptr)
578 return -ENOMEM;
579
580 /* copy first part */
581 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
582
583 /* copy second part */
584 memcpy((char *)data_ptr + del_if_num * chunk_size,
585 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
586 (max_if_num - del_if_num) * chunk_size);
587
588free_bcast_own:
589 kfree(orig_node->bcast_own);
590 orig_node->bcast_own = data_ptr;
591
592 if (max_if_num == 0)
593 goto free_own_sum;
594
595 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
596 if (!data_ptr)
597 return -ENOMEM;
598
599 memcpy(data_ptr, orig_node->bcast_own_sum,
600 del_if_num * sizeof(uint8_t));
601
602 memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
603 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
604 (max_if_num - del_if_num) * sizeof(uint8_t));
605
606free_own_sum:
607 kfree(orig_node->bcast_own_sum);
608 orig_node->bcast_own_sum = data_ptr;
609
610 return 0;
611}
612
613int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, 578int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
614 int max_if_num) 579 int max_if_num)
615{ 580{
@@ -618,6 +583,7 @@ int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
618 struct hlist_head *head; 583 struct hlist_head *head;
619 struct batadv_hard_iface *hard_iface_tmp; 584 struct batadv_hard_iface *hard_iface_tmp;
620 struct batadv_orig_node *orig_node; 585 struct batadv_orig_node *orig_node;
586 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
621 uint32_t i; 587 uint32_t i;
622 int ret; 588 int ret;
623 589
@@ -629,11 +595,11 @@ int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
629 595
630 rcu_read_lock(); 596 rcu_read_lock();
631 hlist_for_each_entry_rcu(orig_node, head, hash_entry) { 597 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
632 spin_lock_bh(&orig_node->ogm_cnt_lock); 598 ret = 0;
633 ret = batadv_orig_node_del_if(orig_node, max_if_num, 599 if (bao->bat_orig_del_if)
634 hard_iface->if_num); 600 ret = bao->bat_orig_del_if(orig_node,
635 spin_unlock_bh(&orig_node->ogm_cnt_lock); 601 max_if_num,
636 602 hard_iface->if_num);
637 if (ret == -ENOMEM) 603 if (ret == -ENOMEM)
638 goto err; 604 goto err;
639 } 605 }
diff --git a/net/batman-adv/originator.h b/net/batman-adv/originator.h
index 7887b84a9af4..6f77d808a916 100644
--- a/net/batman-adv/originator.h
+++ b/net/batman-adv/originator.h
@@ -22,16 +22,18 @@
22 22
23#include "hash.h" 23#include "hash.h"
24 24
25int batadv_compare_orig(const struct hlist_node *node, const void *data2);
25int batadv_originator_init(struct batadv_priv *bat_priv); 26int batadv_originator_init(struct batadv_priv *bat_priv);
26void batadv_originator_free(struct batadv_priv *bat_priv); 27void batadv_originator_free(struct batadv_priv *bat_priv);
27void batadv_purge_orig_ref(struct batadv_priv *bat_priv); 28void batadv_purge_orig_ref(struct batadv_priv *bat_priv);
28void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node); 29void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node);
29void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node); 30void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node);
30struct batadv_orig_node *batadv_get_orig_node(struct batadv_priv *bat_priv, 31struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
31 const uint8_t *addr); 32 const uint8_t *addr);
32struct batadv_neigh_node * 33struct batadv_neigh_node *
33batadv_neigh_node_new(struct batadv_hard_iface *hard_iface, 34batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
34 const uint8_t *neigh_addr); 35 const uint8_t *neigh_addr,
36 struct batadv_orig_node *orig_node);
35void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node); 37void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node);
36struct batadv_neigh_node * 38struct batadv_neigh_node *
37batadv_orig_node_get_router(struct batadv_orig_node *orig_node); 39batadv_orig_node_get_router(struct batadv_orig_node *orig_node);
@@ -40,6 +42,13 @@ int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
40 int max_if_num); 42 int max_if_num);
41int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface, 43int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
42 int max_if_num); 44 int max_if_num);
45struct batadv_orig_node_vlan *
46batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
47 unsigned short vid);
48struct batadv_orig_node_vlan *
49batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
50 unsigned short vid);
51void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan);
43 52
44 53
45/* hashfunction to choose an entry in a hash table of given size 54/* hashfunction to choose an entry in a hash table of given size
diff --git a/net/batman-adv/packet.h b/net/batman-adv/packet.h
index a51ccfc39da4..2dd8f2422550 100644
--- a/net/batman-adv/packet.h
+++ b/net/batman-adv/packet.h
@@ -20,17 +20,34 @@
20#ifndef _NET_BATMAN_ADV_PACKET_H_ 20#ifndef _NET_BATMAN_ADV_PACKET_H_
21#define _NET_BATMAN_ADV_PACKET_H_ 21#define _NET_BATMAN_ADV_PACKET_H_
22 22
23/**
24 * enum batadv_packettype - types for batman-adv encapsulated packets
25 * @BATADV_IV_OGM: originator messages for B.A.T.M.A.N. IV
26 * @BATADV_BCAST: broadcast packets carrying broadcast payload
27 * @BATADV_CODED: network coded packets
28 *
29 * @BATADV_UNICAST: unicast packets carrying unicast payload traffic
30 * @BATADV_UNICAST_FRAG: unicast packets carrying a fragment of the original
31 * payload packet
32 * @BATADV_UNICAST_4ADDR: unicast packet including the originator address of
33 * the sender
34 * @BATADV_ICMP: unicast packet like IP ICMP used for ping or traceroute
35 * @BATADV_UNICAST_TVLV: unicast packet carrying TVLV containers
36 */
23enum batadv_packettype { 37enum batadv_packettype {
24 BATADV_IV_OGM = 0x01, 38 /* 0x00 - 0x3f: local packets or special rules for handling */
25 BATADV_ICMP = 0x02, 39 BATADV_IV_OGM = 0x00,
26 BATADV_UNICAST = 0x03, 40 BATADV_BCAST = 0x01,
27 BATADV_BCAST = 0x04, 41 BATADV_CODED = 0x02,
28 BATADV_VIS = 0x05, 42 /* 0x40 - 0x7f: unicast */
29 BATADV_UNICAST_FRAG = 0x06, 43#define BATADV_UNICAST_MIN 0x40
30 BATADV_TT_QUERY = 0x07, 44 BATADV_UNICAST = 0x40,
31 BATADV_ROAM_ADV = 0x08, 45 BATADV_UNICAST_FRAG = 0x41,
32 BATADV_UNICAST_4ADDR = 0x09, 46 BATADV_UNICAST_4ADDR = 0x42,
33 BATADV_CODED = 0x0a, 47 BATADV_ICMP = 0x43,
48 BATADV_UNICAST_TVLV = 0x44,
49#define BATADV_UNICAST_MAX 0x7f
50 /* 0x80 - 0xff: reserved */
34}; 51};
35 52
36/** 53/**
@@ -48,13 +65,21 @@ enum batadv_subtype {
48}; 65};
49 66
50/* this file is included by batctl which needs these defines */ 67/* this file is included by batctl which needs these defines */
51#define BATADV_COMPAT_VERSION 14 68#define BATADV_COMPAT_VERSION 15
52 69
70/**
71 * enum batadv_iv_flags - flags used in B.A.T.M.A.N. IV OGM packets
72 * @BATADV_NOT_BEST_NEXT_HOP: flag is set when ogm packet is forwarded and was
73 * previously received from someone else than the best neighbor.
74 * @BATADV_PRIMARIES_FIRST_HOP: flag is set when the primary interface address
75 * is used, and the packet travels its first hop.
76 * @BATADV_DIRECTLINK: flag is for the first hop or if rebroadcasted from a
77 * one hop neighbor on the interface where it was originally received.
78 */
53enum batadv_iv_flags { 79enum batadv_iv_flags {
54 BATADV_NOT_BEST_NEXT_HOP = BIT(3), 80 BATADV_NOT_BEST_NEXT_HOP = BIT(0),
55 BATADV_PRIMARIES_FIRST_HOP = BIT(4), 81 BATADV_PRIMARIES_FIRST_HOP = BIT(1),
56 BATADV_VIS_SERVER = BIT(5), 82 BATADV_DIRECTLINK = BIT(2),
57 BATADV_DIRECTLINK = BIT(6),
58}; 83};
59 84
60/* ICMP message types */ 85/* ICMP message types */
@@ -66,43 +91,44 @@ enum batadv_icmp_packettype {
66 BATADV_PARAMETER_PROBLEM = 12, 91 BATADV_PARAMETER_PROBLEM = 12,
67}; 92};
68 93
69/* vis defines */ 94/* tt data subtypes */
70enum batadv_vis_packettype { 95#define BATADV_TT_DATA_TYPE_MASK 0x0F
71 BATADV_VIS_TYPE_SERVER_SYNC = 0,
72 BATADV_VIS_TYPE_CLIENT_UPDATE = 1,
73};
74
75/* fragmentation defines */
76enum batadv_unicast_frag_flags {
77 BATADV_UNI_FRAG_HEAD = BIT(0),
78 BATADV_UNI_FRAG_LARGETAIL = BIT(1),
79};
80
81/* TT_QUERY subtypes */
82#define BATADV_TT_QUERY_TYPE_MASK 0x3
83 96
84enum batadv_tt_query_packettype { 97/**
85 BATADV_TT_REQUEST = 0, 98 * enum batadv_tt_data_flags - flags for tt data tvlv
86 BATADV_TT_RESPONSE = 1, 99 * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
87}; 100 * @BATADV_TT_REQUEST: TT request message
88 101 * @BATADV_TT_RESPONSE: TT response message
89/* TT_QUERY flags */ 102 * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
90enum batadv_tt_query_flags { 103 */
91 BATADV_TT_FULL_TABLE = BIT(2), 104enum batadv_tt_data_flags {
105 BATADV_TT_OGM_DIFF = BIT(0),
106 BATADV_TT_REQUEST = BIT(1),
107 BATADV_TT_RESPONSE = BIT(2),
108 BATADV_TT_FULL_TABLE = BIT(4),
92}; 109};
93 110
94/* BATADV_TT_CLIENT flags. 111/* BATADV_TT_CLIENT flags.
95 * Flags from BIT(0) to BIT(7) are sent on the wire, while flags from BIT(8) to 112 * Flags from BIT(0) to BIT(7) are sent on the wire, while flags from BIT(8) to
96 * BIT(15) are used for local computation only 113 * BIT(15) are used for local computation only.
114 * Flags from BIT(4) to BIT(7) are kept in sync with the rest of the network.
97 */ 115 */
98enum batadv_tt_client_flags { 116enum batadv_tt_client_flags {
99 BATADV_TT_CLIENT_DEL = BIT(0), 117 BATADV_TT_CLIENT_DEL = BIT(0),
100 BATADV_TT_CLIENT_ROAM = BIT(1), 118 BATADV_TT_CLIENT_ROAM = BIT(1),
101 BATADV_TT_CLIENT_WIFI = BIT(2), 119 BATADV_TT_CLIENT_WIFI = BIT(4),
102 BATADV_TT_CLIENT_TEMP = BIT(3),
103 BATADV_TT_CLIENT_NOPURGE = BIT(8), 120 BATADV_TT_CLIENT_NOPURGE = BIT(8),
104 BATADV_TT_CLIENT_NEW = BIT(9), 121 BATADV_TT_CLIENT_NEW = BIT(9),
105 BATADV_TT_CLIENT_PENDING = BIT(10), 122 BATADV_TT_CLIENT_PENDING = BIT(10),
123 BATADV_TT_CLIENT_TEMP = BIT(11),
124};
125
126/**
127 * batadv_vlan_flags - flags for the four MSB of any vlan ID field
128 * @BATADV_VLAN_HAS_TAG: whether the field contains a valid vlan tag or not
129 */
130enum batadv_vlan_flags {
131 BATADV_VLAN_HAS_TAG = BIT(15),
106}; 132};
107 133
108/* claim frame types for the bridge loop avoidance */ 134/* claim frame types for the bridge loop avoidance */
@@ -113,6 +139,23 @@ enum batadv_bla_claimframe {
113 BATADV_CLAIM_TYPE_REQUEST = 0x03, 139 BATADV_CLAIM_TYPE_REQUEST = 0x03,
114}; 140};
115 141
142/**
143 * enum batadv_tvlv_type - tvlv type definitions
144 * @BATADV_TVLV_GW: gateway tvlv
145 * @BATADV_TVLV_DAT: distributed arp table tvlv
146 * @BATADV_TVLV_NC: network coding tvlv
147 * @BATADV_TVLV_TT: translation table tvlv
148 * @BATADV_TVLV_ROAM: roaming advertisement tvlv
149 */
150enum batadv_tvlv_type {
151 BATADV_TVLV_GW = 0x01,
152 BATADV_TVLV_DAT = 0x02,
153 BATADV_TVLV_NC = 0x03,
154 BATADV_TVLV_TT = 0x04,
155 BATADV_TVLV_ROAM = 0x05,
156};
157
158#pragma pack(2)
116/* the destination hardware field in the ARP frame is used to 159/* the destination hardware field in the ARP frame is used to
117 * transport the claim type and the group id 160 * transport the claim type and the group id
118 */ 161 */
@@ -121,57 +164,114 @@ struct batadv_bla_claim_dst {
121 uint8_t type; /* bla_claimframe */ 164 uint8_t type; /* bla_claimframe */
122 __be16 group; /* group id */ 165 __be16 group; /* group id */
123}; 166};
167#pragma pack()
124 168
125struct batadv_header { 169/**
170 * struct batadv_ogm_packet - ogm (routing protocol) packet
171 * @packet_type: batman-adv packet type, part of the general header
172 * @version: batman-adv protocol version, part of the genereal header
173 * @ttl: time to live for this packet, part of the genereal header
174 * @flags: contains routing relevant flags - see enum batadv_iv_flags
175 * @tvlv_len: length of tvlv data following the ogm header
176 */
177struct batadv_ogm_packet {
126 uint8_t packet_type; 178 uint8_t packet_type;
127 uint8_t version; /* batman version field */ 179 uint8_t version;
128 uint8_t ttl; 180 uint8_t ttl;
129 /* the parent struct has to add a byte after the header to make 181 uint8_t flags;
130 * everything 4 bytes aligned again
131 */
132};
133
134struct batadv_ogm_packet {
135 struct batadv_header header;
136 uint8_t flags; /* 0x40: DIRECTLINK flag, 0x20 VIS_SERVER flag... */
137 __be32 seqno; 182 __be32 seqno;
138 uint8_t orig[ETH_ALEN]; 183 uint8_t orig[ETH_ALEN];
139 uint8_t prev_sender[ETH_ALEN]; 184 uint8_t prev_sender[ETH_ALEN];
140 uint8_t gw_flags; /* flags related to gateway class */ 185 uint8_t reserved;
141 uint8_t tq; 186 uint8_t tq;
142 uint8_t tt_num_changes; 187 __be16 tvlv_len;
143 uint8_t ttvn; /* translation table version number */ 188 /* __packed is not needed as the struct size is divisible by 4,
144 __be16 tt_crc; 189 * and the largest data type in this struct has a size of 4.
145} __packed; 190 */
191};
146 192
147#define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet) 193#define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
148 194
195/**
196 * batadv_icmp_header - common members among all the ICMP packets
197 * @packet_type: batman-adv packet type, part of the general header
198 * @version: batman-adv protocol version, part of the genereal header
199 * @ttl: time to live for this packet, part of the genereal header
200 * @msg_type: ICMP packet type
201 * @dst: address of the destination node
202 * @orig: address of the source node
203 * @uid: local ICMP socket identifier
204 * @align: not used - useful for alignment purposes only
205 *
206 * This structure is used for ICMP packets parsing only and it is never sent
207 * over the wire. The alignment field at the end is there to ensure that
208 * members are padded the same way as they are in real packets.
209 */
210struct batadv_icmp_header {
211 uint8_t packet_type;
212 uint8_t version;
213 uint8_t ttl;
214 uint8_t msg_type; /* see ICMP message types above */
215 uint8_t dst[ETH_ALEN];
216 uint8_t orig[ETH_ALEN];
217 uint8_t uid;
218 uint8_t align[3];
219};
220
221/**
222 * batadv_icmp_packet - ICMP packet
223 * @packet_type: batman-adv packet type, part of the general header
224 * @version: batman-adv protocol version, part of the genereal header
225 * @ttl: time to live for this packet, part of the genereal header
226 * @msg_type: ICMP packet type
227 * @dst: address of the destination node
228 * @orig: address of the source node
229 * @uid: local ICMP socket identifier
230 * @reserved: not used - useful for alignment
231 * @seqno: ICMP sequence number
232 */
149struct batadv_icmp_packet { 233struct batadv_icmp_packet {
150 struct batadv_header header; 234 uint8_t packet_type;
235 uint8_t version;
236 uint8_t ttl;
151 uint8_t msg_type; /* see ICMP message types above */ 237 uint8_t msg_type; /* see ICMP message types above */
152 uint8_t dst[ETH_ALEN]; 238 uint8_t dst[ETH_ALEN];
153 uint8_t orig[ETH_ALEN]; 239 uint8_t orig[ETH_ALEN];
154 __be16 seqno;
155 uint8_t uid; 240 uint8_t uid;
156 uint8_t reserved; 241 uint8_t reserved;
242 __be16 seqno;
157}; 243};
158 244
159#define BATADV_RR_LEN 16 245#define BATADV_RR_LEN 16
160 246
161/* icmp_packet_rr must start with all fields from imcp_packet 247/**
162 * as this is assumed by code that handles ICMP packets 248 * batadv_icmp_packet_rr - ICMP RouteRecord packet
249 * @packet_type: batman-adv packet type, part of the general header
250 * @version: batman-adv protocol version, part of the genereal header
251 * @ttl: time to live for this packet, part of the genereal header
252 * @msg_type: ICMP packet type
253 * @dst: address of the destination node
254 * @orig: address of the source node
255 * @uid: local ICMP socket identifier
256 * @rr_cur: number of entries the rr array
257 * @seqno: ICMP sequence number
258 * @rr: route record array
163 */ 259 */
164struct batadv_icmp_packet_rr { 260struct batadv_icmp_packet_rr {
165 struct batadv_header header; 261 uint8_t packet_type;
262 uint8_t version;
263 uint8_t ttl;
166 uint8_t msg_type; /* see ICMP message types above */ 264 uint8_t msg_type; /* see ICMP message types above */
167 uint8_t dst[ETH_ALEN]; 265 uint8_t dst[ETH_ALEN];
168 uint8_t orig[ETH_ALEN]; 266 uint8_t orig[ETH_ALEN];
169 __be16 seqno;
170 uint8_t uid; 267 uint8_t uid;
171 uint8_t rr_cur; 268 uint8_t rr_cur;
269 __be16 seqno;
172 uint8_t rr[BATADV_RR_LEN][ETH_ALEN]; 270 uint8_t rr[BATADV_RR_LEN][ETH_ALEN];
173}; 271};
174 272
273#define BATADV_ICMP_MAX_PACKET_SIZE sizeof(struct batadv_icmp_packet_rr)
274
175/* All packet headers in front of an ethernet header have to be completely 275/* All packet headers in front of an ethernet header have to be completely
176 * divisible by 2 but not by 4 to make the payload after the ethernet 276 * divisible by 2 but not by 4 to make the payload after the ethernet
177 * header again 4 bytes boundary aligned. 277 * header again 4 bytes boundary aligned.
@@ -184,8 +284,18 @@ struct batadv_icmp_packet_rr {
184 */ 284 */
185#pragma pack(2) 285#pragma pack(2)
186 286
287/**
288 * struct batadv_unicast_packet - unicast packet for network payload
289 * @packet_type: batman-adv packet type, part of the general header
290 * @version: batman-adv protocol version, part of the genereal header
291 * @ttl: time to live for this packet, part of the genereal header
292 * @ttvn: translation table version number
293 * @dest: originator destination of the unicast packet
294 */
187struct batadv_unicast_packet { 295struct batadv_unicast_packet {
188 struct batadv_header header; 296 uint8_t packet_type;
297 uint8_t version;
298 uint8_t ttl;
189 uint8_t ttvn; /* destination translation table version number */ 299 uint8_t ttvn; /* destination translation table version number */
190 uint8_t dest[ETH_ALEN]; 300 uint8_t dest[ETH_ALEN];
191 /* "4 bytes boundary + 2 bytes" long to make the payload after the 301 /* "4 bytes boundary + 2 bytes" long to make the payload after the
@@ -209,18 +319,50 @@ struct batadv_unicast_4addr_packet {
209 */ 319 */
210}; 320};
211 321
212struct batadv_unicast_frag_packet { 322/**
213 struct batadv_header header; 323 * struct batadv_frag_packet - fragmented packet
214 uint8_t ttvn; /* destination translation table version number */ 324 * @packet_type: batman-adv packet type, part of the general header
215 uint8_t dest[ETH_ALEN]; 325 * @version: batman-adv protocol version, part of the genereal header
216 uint8_t flags; 326 * @ttl: time to live for this packet, part of the genereal header
217 uint8_t align; 327 * @dest: final destination used when routing fragments
218 uint8_t orig[ETH_ALEN]; 328 * @orig: originator of the fragment used when merging the packet
219 __be16 seqno; 329 * @no: fragment number within this sequence
220} __packed; 330 * @reserved: reserved byte for alignment
331 * @seqno: sequence identification
332 * @total_size: size of the merged packet
333 */
334struct batadv_frag_packet {
335 uint8_t packet_type;
336 uint8_t version; /* batman version field */
337 uint8_t ttl;
338#if defined(__BIG_ENDIAN_BITFIELD)
339 uint8_t no:4;
340 uint8_t reserved:4;
341#elif defined(__LITTLE_ENDIAN_BITFIELD)
342 uint8_t reserved:4;
343 uint8_t no:4;
344#else
345#error "unknown bitfield endianess"
346#endif
347 uint8_t dest[ETH_ALEN];
348 uint8_t orig[ETH_ALEN];
349 __be16 seqno;
350 __be16 total_size;
351};
221 352
353/**
354 * struct batadv_bcast_packet - broadcast packet for network payload
355 * @packet_type: batman-adv packet type, part of the general header
356 * @version: batman-adv protocol version, part of the genereal header
357 * @ttl: time to live for this packet, part of the genereal header
358 * @reserved: reserved byte for alignment
359 * @seqno: sequence identification
360 * @orig: originator of the broadcast packet
361 */
222struct batadv_bcast_packet { 362struct batadv_bcast_packet {
223 struct batadv_header header; 363 uint8_t packet_type;
364 uint8_t version; /* batman version field */
365 uint8_t ttl;
224 uint8_t reserved; 366 uint8_t reserved;
225 __be32 seqno; 367 __be32 seqno;
226 uint8_t orig[ETH_ALEN]; 368 uint8_t orig[ETH_ALEN];
@@ -229,59 +371,11 @@ struct batadv_bcast_packet {
229 */ 371 */
230}; 372};
231 373
232#pragma pack()
233
234struct batadv_vis_packet {
235 struct batadv_header header;
236 uint8_t vis_type; /* which type of vis-participant sent this? */
237 __be32 seqno; /* sequence number */
238 uint8_t entries; /* number of entries behind this struct */
239 uint8_t reserved;
240 uint8_t vis_orig[ETH_ALEN]; /* originator reporting its neighbors */
241 uint8_t target_orig[ETH_ALEN]; /* who should receive this packet */
242 uint8_t sender_orig[ETH_ALEN]; /* who sent or forwarded this packet */
243};
244
245struct batadv_tt_query_packet {
246 struct batadv_header header;
247 /* the flag field is a combination of:
248 * - TT_REQUEST or TT_RESPONSE
249 * - TT_FULL_TABLE
250 */
251 uint8_t flags;
252 uint8_t dst[ETH_ALEN];
253 uint8_t src[ETH_ALEN];
254 /* the ttvn field is:
255 * if TT_REQUEST: ttvn that triggered the
256 * request
257 * if TT_RESPONSE: new ttvn for the src
258 * orig_node
259 */
260 uint8_t ttvn;
261 /* tt_data field is:
262 * if TT_REQUEST: crc associated with the
263 * ttvn
264 * if TT_RESPONSE: table_size
265 */
266 __be16 tt_data;
267} __packed;
268
269struct batadv_roam_adv_packet {
270 struct batadv_header header;
271 uint8_t reserved;
272 uint8_t dst[ETH_ALEN];
273 uint8_t src[ETH_ALEN];
274 uint8_t client[ETH_ALEN];
275} __packed;
276
277struct batadv_tt_change {
278 uint8_t flags;
279 uint8_t addr[ETH_ALEN];
280} __packed;
281
282/** 374/**
283 * struct batadv_coded_packet - network coded packet 375 * struct batadv_coded_packet - network coded packet
284 * @header: common batman packet header and ttl of first included packet 376 * @packet_type: batman-adv packet type, part of the general header
377 * @version: batman-adv protocol version, part of the genereal header
378 * @ttl: time to live for this packet, part of the genereal header
285 * @reserved: Align following fields to 2-byte boundaries 379 * @reserved: Align following fields to 2-byte boundaries
286 * @first_source: original source of first included packet 380 * @first_source: original source of first included packet
287 * @first_orig_dest: original destinal of first included packet 381 * @first_orig_dest: original destinal of first included packet
@@ -296,7 +390,9 @@ struct batadv_tt_change {
296 * @coded_len: length of network coded part of the payload 390 * @coded_len: length of network coded part of the payload
297 */ 391 */
298struct batadv_coded_packet { 392struct batadv_coded_packet {
299 struct batadv_header header; 393 uint8_t packet_type;
394 uint8_t version; /* batman version field */
395 uint8_t ttl;
300 uint8_t first_ttvn; 396 uint8_t first_ttvn;
301 /* uint8_t first_dest[ETH_ALEN]; - saved in mac header destination */ 397 /* uint8_t first_dest[ETH_ALEN]; - saved in mac header destination */
302 uint8_t first_source[ETH_ALEN]; 398 uint8_t first_source[ETH_ALEN];
@@ -311,4 +407,102 @@ struct batadv_coded_packet {
311 __be16 coded_len; 407 __be16 coded_len;
312}; 408};
313 409
410#pragma pack()
411
412/**
413 * struct batadv_unicast_tvlv - generic unicast packet with tvlv payload
414 * @packet_type: batman-adv packet type, part of the general header
415 * @version: batman-adv protocol version, part of the genereal header
416 * @ttl: time to live for this packet, part of the genereal header
417 * @reserved: reserved field (for packet alignment)
418 * @src: address of the source
419 * @dst: address of the destination
420 * @tvlv_len: length of tvlv data following the unicast tvlv header
421 * @align: 2 bytes to align the header to a 4 byte boundry
422 */
423struct batadv_unicast_tvlv_packet {
424 uint8_t packet_type;
425 uint8_t version; /* batman version field */
426 uint8_t ttl;
427 uint8_t reserved;
428 uint8_t dst[ETH_ALEN];
429 uint8_t src[ETH_ALEN];
430 __be16 tvlv_len;
431 uint16_t align;
432};
433
434/**
435 * struct batadv_tvlv_hdr - base tvlv header struct
436 * @type: tvlv container type (see batadv_tvlv_type)
437 * @version: tvlv container version
438 * @len: tvlv container length
439 */
440struct batadv_tvlv_hdr {
441 uint8_t type;
442 uint8_t version;
443 __be16 len;
444};
445
446/**
447 * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
448 * container
449 * @bandwidth_down: advertised uplink download bandwidth
450 * @bandwidth_up: advertised uplink upload bandwidth
451 */
452struct batadv_tvlv_gateway_data {
453 __be32 bandwidth_down;
454 __be32 bandwidth_up;
455};
456
457/**
458 * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
459 * @flags: translation table flags (see batadv_tt_data_flags)
460 * @ttvn: translation table version number
461 * @vlan_num: number of announced VLANs. In the TVLV this struct is followed by
462 * one batadv_tvlv_tt_vlan_data object per announced vlan
463 */
464struct batadv_tvlv_tt_data {
465 uint8_t flags;
466 uint8_t ttvn;
467 __be16 num_vlan;
468};
469
470/**
471 * struct batadv_tvlv_tt_vlan_data - vlan specific tt data propagated through
472 * the tt tvlv container
473 * @crc: crc32 checksum of the entries belonging to this vlan
474 * @vid: vlan identifier
475 * @reserved: unused, useful for alignment purposes
476 */
477struct batadv_tvlv_tt_vlan_data {
478 __be32 crc;
479 __be16 vid;
480 uint16_t reserved;
481};
482
483/**
484 * struct batadv_tvlv_tt_change - translation table diff data
485 * @flags: status indicators concerning the non-mesh client (see
486 * batadv_tt_client_flags)
487 * @reserved: reserved field - useful for alignment purposes only
488 * @addr: mac address of non-mesh client that triggered this tt change
489 * @vid: VLAN identifier
490 */
491struct batadv_tvlv_tt_change {
492 uint8_t flags;
493 uint8_t reserved[3];
494 uint8_t addr[ETH_ALEN];
495 __be16 vid;
496};
497
498/**
499 * struct batadv_tvlv_roam_adv - roaming advertisement
500 * @client: mac address of roaming client
501 * @vid: VLAN identifier
502 */
503struct batadv_tvlv_roam_adv {
504 uint8_t client[ETH_ALEN];
505 __be16 vid;
506};
507
314#endif /* _NET_BATMAN_ADV_PACKET_H_ */ 508#endif /* _NET_BATMAN_ADV_PACKET_H_ */
diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
index 0439395d7ba5..46278bfb8fdb 100644
--- a/net/batman-adv/routing.c
+++ b/net/batman-adv/routing.c
@@ -25,11 +25,12 @@
25#include "icmp_socket.h" 25#include "icmp_socket.h"
26#include "translation-table.h" 26#include "translation-table.h"
27#include "originator.h" 27#include "originator.h"
28#include "vis.h"
29#include "unicast.h"
30#include "bridge_loop_avoidance.h" 28#include "bridge_loop_avoidance.h"
31#include "distributed-arp-table.h" 29#include "distributed-arp-table.h"
32#include "network-coding.h" 30#include "network-coding.h"
31#include "fragmentation.h"
32
33#include <linux/if_vlan.h>
33 34
34static int batadv_route_unicast_packet(struct sk_buff *skb, 35static int batadv_route_unicast_packet(struct sk_buff *skb,
35 struct batadv_hard_iface *recv_if); 36 struct batadv_hard_iface *recv_if);
@@ -46,7 +47,7 @@ static void _batadv_update_route(struct batadv_priv *bat_priv,
46 if ((curr_router) && (!neigh_node)) { 47 if ((curr_router) && (!neigh_node)) {
47 batadv_dbg(BATADV_DBG_ROUTES, bat_priv, 48 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
48 "Deleting route towards: %pM\n", orig_node->orig); 49 "Deleting route towards: %pM\n", orig_node->orig);
49 batadv_tt_global_del_orig(bat_priv, orig_node, 50 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
50 "Deleted route towards originator"); 51 "Deleted route towards originator");
51 52
52 /* route added */ 53 /* route added */
@@ -114,9 +115,19 @@ out:
114 return; 115 return;
115} 116}
116 117
117void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node, 118/**
119 * batadv_bonding_candidate_add - consider a new link for bonding mode towards
120 * the given originator
121 * @bat_priv: the bat priv with all the soft interface information
122 * @orig_node: the target node
123 * @neigh_node: the neighbor representing the new link to consider for bonding
124 * mode
125 */
126void batadv_bonding_candidate_add(struct batadv_priv *bat_priv,
127 struct batadv_orig_node *orig_node,
118 struct batadv_neigh_node *neigh_node) 128 struct batadv_neigh_node *neigh_node)
119{ 129{
130 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
120 struct batadv_neigh_node *tmp_neigh_node, *router = NULL; 131 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
121 uint8_t interference_candidate = 0; 132 uint8_t interference_candidate = 0;
122 133
@@ -131,8 +142,9 @@ void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
131 if (!router) 142 if (!router)
132 goto candidate_del; 143 goto candidate_del;
133 144
145
134 /* ... and is good enough to be considered */ 146 /* ... and is good enough to be considered */
135 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD) 147 if (bao->bat_neigh_is_equiv_or_better(neigh_node, router))
136 goto candidate_del; 148 goto candidate_del;
137 149
138 /* check if we have another candidate with the same mac address or 150 /* check if we have another candidate with the same mac address or
@@ -248,46 +260,65 @@ bool batadv_check_management_packet(struct sk_buff *skb,
248 return true; 260 return true;
249} 261}
250 262
263/**
264 * batadv_recv_my_icmp_packet - receive an icmp packet locally
265 * @bat_priv: the bat priv with all the soft interface information
266 * @skb: icmp packet to process
267 *
268 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
269 * otherwise.
270 */
251static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv, 271static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
252 struct sk_buff *skb, size_t icmp_len) 272 struct sk_buff *skb)
253{ 273{
254 struct batadv_hard_iface *primary_if = NULL; 274 struct batadv_hard_iface *primary_if = NULL;
255 struct batadv_orig_node *orig_node = NULL; 275 struct batadv_orig_node *orig_node = NULL;
256 struct batadv_icmp_packet_rr *icmp_packet; 276 struct batadv_icmp_header *icmph;
257 int ret = NET_RX_DROP; 277 int res, ret = NET_RX_DROP;
258 278
259 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 279 icmph = (struct batadv_icmp_header *)skb->data;
260 280
261 /* add data to device queue */ 281 switch (icmph->msg_type) {
262 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { 282 case BATADV_ECHO_REPLY:
263 batadv_socket_receive_packet(icmp_packet, icmp_len); 283 case BATADV_DESTINATION_UNREACHABLE:
264 goto out; 284 case BATADV_TTL_EXCEEDED:
265 } 285 /* receive the packet */
286 if (skb_linearize(skb) < 0)
287 break;
266 288
267 primary_if = batadv_primary_if_get_selected(bat_priv); 289 batadv_socket_receive_packet(icmph, skb->len);
268 if (!primary_if) 290 break;
269 goto out; 291 case BATADV_ECHO_REQUEST:
292 /* answer echo request (ping) */
293 primary_if = batadv_primary_if_get_selected(bat_priv);
294 if (!primary_if)
295 goto out;
270 296
271 /* answer echo request (ping) */ 297 /* get routing information */
272 /* get routing information */ 298 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
273 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); 299 if (!orig_node)
274 if (!orig_node) 300 goto out;
275 goto out;
276 301
277 /* create a copy of the skb, if needed, to modify it. */ 302 /* create a copy of the skb, if needed, to modify it. */
278 if (skb_cow(skb, ETH_HLEN) < 0) 303 if (skb_cow(skb, ETH_HLEN) < 0)
279 goto out; 304 goto out;
280 305
281 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 306 icmph = (struct batadv_icmp_header *)skb->data;
282 307
283 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); 308 memcpy(icmph->dst, icmph->orig, ETH_ALEN);
284 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); 309 memcpy(icmph->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
285 icmp_packet->msg_type = BATADV_ECHO_REPLY; 310 icmph->msg_type = BATADV_ECHO_REPLY;
286 icmp_packet->header.ttl = BATADV_TTL; 311 icmph->ttl = BATADV_TTL;
287 312
288 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 313 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
289 ret = NET_RX_SUCCESS; 314 if (res != NET_XMIT_DROP)
315 ret = NET_RX_SUCCESS;
290 316
317 break;
318 default:
319 /* drop unknown type */
320 goto out;
321 }
291out: 322out:
292 if (primary_if) 323 if (primary_if)
293 batadv_hardif_free_ref(primary_if); 324 batadv_hardif_free_ref(primary_if);
@@ -329,9 +360,10 @@ static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
329 icmp_packet = (struct batadv_icmp_packet *)skb->data; 360 icmp_packet = (struct batadv_icmp_packet *)skb->data;
330 361
331 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); 362 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
332 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); 363 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr,
364 ETH_ALEN);
333 icmp_packet->msg_type = BATADV_TTL_EXCEEDED; 365 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
334 icmp_packet->header.ttl = BATADV_TTL; 366 icmp_packet->ttl = BATADV_TTL;
335 367
336 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 368 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
337 ret = NET_RX_SUCCESS; 369 ret = NET_RX_SUCCESS;
@@ -349,16 +381,13 @@ int batadv_recv_icmp_packet(struct sk_buff *skb,
349 struct batadv_hard_iface *recv_if) 381 struct batadv_hard_iface *recv_if)
350{ 382{
351 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 383 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
352 struct batadv_icmp_packet_rr *icmp_packet; 384 struct batadv_icmp_header *icmph;
385 struct batadv_icmp_packet_rr *icmp_packet_rr;
353 struct ethhdr *ethhdr; 386 struct ethhdr *ethhdr;
354 struct batadv_orig_node *orig_node = NULL; 387 struct batadv_orig_node *orig_node = NULL;
355 int hdr_size = sizeof(struct batadv_icmp_packet); 388 int hdr_size = sizeof(struct batadv_icmp_header);
356 int ret = NET_RX_DROP; 389 int ret = NET_RX_DROP;
357 390
358 /* we truncate all incoming icmp packets if they don't match our size */
359 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
360 hdr_size = sizeof(struct batadv_icmp_packet_rr);
361
362 /* drop packet if it has not necessary minimum size */ 391 /* drop packet if it has not necessary minimum size */
363 if (unlikely(!pskb_may_pull(skb, hdr_size))) 392 if (unlikely(!pskb_may_pull(skb, hdr_size)))
364 goto out; 393 goto out;
@@ -377,26 +406,39 @@ int batadv_recv_icmp_packet(struct sk_buff *skb,
377 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest)) 406 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
378 goto out; 407 goto out;
379 408
380 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 409 icmph = (struct batadv_icmp_header *)skb->data;
381 410
382 /* add record route information if not full */ 411 /* add record route information if not full */
383 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) && 412 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
384 (icmp_packet->rr_cur < BATADV_RR_LEN)) { 413 icmph->msg_type == BATADV_ECHO_REQUEST) &&
385 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), 414 (skb->len >= sizeof(struct batadv_icmp_packet_rr))) {
415 if (skb_linearize(skb) < 0)
416 goto out;
417
418 /* create a copy of the skb, if needed, to modify it. */
419 if (skb_cow(skb, ETH_HLEN) < 0)
420 goto out;
421
422 icmph = (struct batadv_icmp_header *)skb->data;
423 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
424 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
425 goto out;
426
427 memcpy(&(icmp_packet_rr->rr[icmp_packet_rr->rr_cur]),
386 ethhdr->h_dest, ETH_ALEN); 428 ethhdr->h_dest, ETH_ALEN);
387 icmp_packet->rr_cur++; 429 icmp_packet_rr->rr_cur++;
388 } 430 }
389 431
390 /* packet for me */ 432 /* packet for me */
391 if (batadv_is_my_mac(bat_priv, icmp_packet->dst)) 433 if (batadv_is_my_mac(bat_priv, icmph->dst))
392 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size); 434 return batadv_recv_my_icmp_packet(bat_priv, skb);
393 435
394 /* TTL exceeded */ 436 /* TTL exceeded */
395 if (icmp_packet->header.ttl < 2) 437 if (icmph->ttl < 2)
396 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb); 438 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
397 439
398 /* get routing information */ 440 /* get routing information */
399 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst); 441 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
400 if (!orig_node) 442 if (!orig_node)
401 goto out; 443 goto out;
402 444
@@ -404,10 +446,10 @@ int batadv_recv_icmp_packet(struct sk_buff *skb,
404 if (skb_cow(skb, ETH_HLEN) < 0) 446 if (skb_cow(skb, ETH_HLEN) < 0)
405 goto out; 447 goto out;
406 448
407 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data; 449 icmph = (struct batadv_icmp_header *)skb->data;
408 450
409 /* decrement ttl */ 451 /* decrement ttl */
410 icmp_packet->header.ttl--; 452 icmph->ttl--;
411 453
412 /* route it */ 454 /* route it */
413 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP) 455 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
@@ -474,18 +516,25 @@ out:
474 return router; 516 return router;
475} 517}
476 518
477/* Interface Alternating: Use the best of the 519/**
478 * remaining candidates which are not using 520 * batadv_find_ifalter_router - find the best of the remaining candidates which
479 * this interface. 521 * are not using this interface
522 * @bat_priv: the bat priv with all the soft interface information
523 * @primary_orig: the destination
524 * @recv_if: the interface that the router returned by this function has to not
525 * use
480 * 526 *
481 * Increases the returned router's refcount 527 * Returns the best candidate towards primary_orig that is not using recv_if.
528 * Increases the returned neighbor's refcount
482 */ 529 */
483static struct batadv_neigh_node * 530static struct batadv_neigh_node *
484batadv_find_ifalter_router(struct batadv_orig_node *primary_orig, 531batadv_find_ifalter_router(struct batadv_priv *bat_priv,
532 struct batadv_orig_node *primary_orig,
485 const struct batadv_hard_iface *recv_if) 533 const struct batadv_hard_iface *recv_if)
486{ 534{
487 struct batadv_neigh_node *tmp_neigh_node;
488 struct batadv_neigh_node *router = NULL, *first_candidate = NULL; 535 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
536 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
537 struct batadv_neigh_node *tmp_neigh_node;
489 538
490 rcu_read_lock(); 539 rcu_read_lock();
491 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, 540 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
@@ -497,7 +546,7 @@ batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
497 if (tmp_neigh_node->if_incoming == recv_if) 546 if (tmp_neigh_node->if_incoming == recv_if)
498 continue; 547 continue;
499 548
500 if (router && tmp_neigh_node->tq_avg <= router->tq_avg) 549 if (router && bao->bat_neigh_cmp(tmp_neigh_node, router))
501 continue; 550 continue;
502 551
503 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) 552 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
@@ -557,126 +606,6 @@ static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
557 return 0; 606 return 0;
558} 607}
559 608
560int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
561{
562 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
563 struct batadv_tt_query_packet *tt_query;
564 uint16_t tt_size;
565 int hdr_size = sizeof(*tt_query);
566 char tt_flag;
567 size_t packet_size;
568
569 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
570 return NET_RX_DROP;
571
572 /* I could need to modify it */
573 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
574 goto out;
575
576 tt_query = (struct batadv_tt_query_packet *)skb->data;
577
578 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
579 case BATADV_TT_REQUEST:
580 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
581
582 /* If we cannot provide an answer the tt_request is
583 * forwarded
584 */
585 if (!batadv_send_tt_response(bat_priv, tt_query)) {
586 if (tt_query->flags & BATADV_TT_FULL_TABLE)
587 tt_flag = 'F';
588 else
589 tt_flag = '.';
590
591 batadv_dbg(BATADV_DBG_TT, bat_priv,
592 "Routing TT_REQUEST to %pM [%c]\n",
593 tt_query->dst,
594 tt_flag);
595 return batadv_route_unicast_packet(skb, recv_if);
596 }
597 break;
598 case BATADV_TT_RESPONSE:
599 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
600
601 if (batadv_is_my_mac(bat_priv, tt_query->dst)) {
602 /* packet needs to be linearized to access the TT
603 * changes
604 */
605 if (skb_linearize(skb) < 0)
606 goto out;
607 /* skb_linearize() possibly changed skb->data */
608 tt_query = (struct batadv_tt_query_packet *)skb->data;
609
610 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
611
612 /* Ensure we have all the claimed data */
613 packet_size = sizeof(struct batadv_tt_query_packet);
614 packet_size += tt_size;
615 if (unlikely(skb_headlen(skb) < packet_size))
616 goto out;
617
618 batadv_handle_tt_response(bat_priv, tt_query);
619 } else {
620 if (tt_query->flags & BATADV_TT_FULL_TABLE)
621 tt_flag = 'F';
622 else
623 tt_flag = '.';
624 batadv_dbg(BATADV_DBG_TT, bat_priv,
625 "Routing TT_RESPONSE to %pM [%c]\n",
626 tt_query->dst,
627 tt_flag);
628 return batadv_route_unicast_packet(skb, recv_if);
629 }
630 break;
631 }
632
633out:
634 /* returning NET_RX_DROP will make the caller function kfree the skb */
635 return NET_RX_DROP;
636}
637
638int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
639{
640 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
641 struct batadv_roam_adv_packet *roam_adv_packet;
642 struct batadv_orig_node *orig_node;
643
644 if (batadv_check_unicast_packet(bat_priv, skb,
645 sizeof(*roam_adv_packet)) < 0)
646 goto out;
647
648 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
649
650 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
651
652 if (!batadv_is_my_mac(bat_priv, roam_adv_packet->dst))
653 return batadv_route_unicast_packet(skb, recv_if);
654
655 /* check if it is a backbone gateway. we don't accept
656 * roaming advertisement from it, as it has the same
657 * entries as we have.
658 */
659 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
660 goto out;
661
662 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
663 if (!orig_node)
664 goto out;
665
666 batadv_dbg(BATADV_DBG_TT, bat_priv,
667 "Received ROAMING_ADV from %pM (client %pM)\n",
668 roam_adv_packet->src, roam_adv_packet->client);
669
670 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
671 BATADV_TT_CLIENT_ROAM,
672 atomic_read(&orig_node->last_ttvn) + 1);
673
674 batadv_orig_node_free_ref(orig_node);
675out:
676 /* returning NET_RX_DROP will make the caller function kfree the skb */
677 return NET_RX_DROP;
678}
679
680/* find a suitable router for this originator, and use 609/* find a suitable router for this originator, and use
681 * bonding if possible. increases the found neighbors 610 * bonding if possible. increases the found neighbors
682 * refcount. 611 * refcount.
@@ -751,7 +680,8 @@ batadv_find_router(struct batadv_priv *bat_priv,
751 if (bonding_enabled) 680 if (bonding_enabled)
752 router = batadv_find_bond_router(primary_orig_node, recv_if); 681 router = batadv_find_bond_router(primary_orig_node, recv_if);
753 else 682 else
754 router = batadv_find_ifalter_router(primary_orig_node, recv_if); 683 router = batadv_find_ifalter_router(bat_priv, primary_orig_node,
684 recv_if);
755 685
756return_router: 686return_router:
757 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE) 687 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
@@ -772,16 +702,14 @@ static int batadv_route_unicast_packet(struct sk_buff *skb,
772{ 702{
773 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 703 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
774 struct batadv_orig_node *orig_node = NULL; 704 struct batadv_orig_node *orig_node = NULL;
775 struct batadv_neigh_node *neigh_node = NULL;
776 struct batadv_unicast_packet *unicast_packet; 705 struct batadv_unicast_packet *unicast_packet;
777 struct ethhdr *ethhdr = eth_hdr(skb); 706 struct ethhdr *ethhdr = eth_hdr(skb);
778 int res, hdr_len, ret = NET_RX_DROP; 707 int res, hdr_len, ret = NET_RX_DROP;
779 struct sk_buff *new_skb;
780 708
781 unicast_packet = (struct batadv_unicast_packet *)skb->data; 709 unicast_packet = (struct batadv_unicast_packet *)skb->data;
782 710
783 /* TTL exceeded */ 711 /* TTL exceeded */
784 if (unicast_packet->header.ttl < 2) { 712 if (unicast_packet->ttl < 2) {
785 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n", 713 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
786 ethhdr->h_source, unicast_packet->dest); 714 ethhdr->h_source, unicast_packet->dest);
787 goto out; 715 goto out;
@@ -793,49 +721,15 @@ static int batadv_route_unicast_packet(struct sk_buff *skb,
793 if (!orig_node) 721 if (!orig_node)
794 goto out; 722 goto out;
795 723
796 /* find_router() increases neigh_nodes refcount if found. */
797 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
798
799 if (!neigh_node)
800 goto out;
801
802 /* create a copy of the skb, if needed, to modify it. */ 724 /* create a copy of the skb, if needed, to modify it. */
803 if (skb_cow(skb, ETH_HLEN) < 0) 725 if (skb_cow(skb, ETH_HLEN) < 0)
804 goto out; 726 goto out;
805 727
806 unicast_packet = (struct batadv_unicast_packet *)skb->data;
807
808 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
809 atomic_read(&bat_priv->fragmentation) &&
810 skb->len > neigh_node->if_incoming->net_dev->mtu) {
811 ret = batadv_frag_send_skb(skb, bat_priv,
812 neigh_node->if_incoming,
813 neigh_node->addr);
814 goto out;
815 }
816
817 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
818 batadv_frag_can_reassemble(skb,
819 neigh_node->if_incoming->net_dev->mtu)) {
820 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
821
822 if (ret == NET_RX_DROP)
823 goto out;
824
825 /* packet was buffered for late merge */
826 if (!new_skb) {
827 ret = NET_RX_SUCCESS;
828 goto out;
829 }
830
831 skb = new_skb;
832 unicast_packet = (struct batadv_unicast_packet *)skb->data;
833 }
834
835 /* decrement ttl */ 728 /* decrement ttl */
836 unicast_packet->header.ttl--; 729 unicast_packet = (struct batadv_unicast_packet *)skb->data;
730 unicast_packet->ttl--;
837 731
838 switch (unicast_packet->header.packet_type) { 732 switch (unicast_packet->packet_type) {
839 case BATADV_UNICAST_4ADDR: 733 case BATADV_UNICAST_4ADDR:
840 hdr_len = sizeof(struct batadv_unicast_4addr_packet); 734 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
841 break; 735 break;
@@ -867,8 +761,6 @@ static int batadv_route_unicast_packet(struct sk_buff *skb,
867 } 761 }
868 762
869out: 763out:
870 if (neigh_node)
871 batadv_neigh_node_free_ref(neigh_node);
872 if (orig_node) 764 if (orig_node)
873 batadv_orig_node_free_ref(orig_node); 765 batadv_orig_node_free_ref(orig_node);
874 return ret; 766 return ret;
@@ -879,6 +771,7 @@ out:
879 * @bat_priv: the bat priv with all the soft interface information 771 * @bat_priv: the bat priv with all the soft interface information
880 * @unicast_packet: the unicast header to be updated 772 * @unicast_packet: the unicast header to be updated
881 * @dst_addr: the payload destination 773 * @dst_addr: the payload destination
774 * @vid: VLAN identifier
882 * 775 *
883 * Search the translation table for dst_addr and update the unicast header with 776 * Search the translation table for dst_addr and update the unicast header with
884 * the new corresponding information (originator address where the destination 777 * the new corresponding information (originator address where the destination
@@ -889,21 +782,22 @@ out:
889static bool 782static bool
890batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, 783batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
891 struct batadv_unicast_packet *unicast_packet, 784 struct batadv_unicast_packet *unicast_packet,
892 uint8_t *dst_addr) 785 uint8_t *dst_addr, unsigned short vid)
893{ 786{
894 struct batadv_orig_node *orig_node = NULL; 787 struct batadv_orig_node *orig_node = NULL;
895 struct batadv_hard_iface *primary_if = NULL; 788 struct batadv_hard_iface *primary_if = NULL;
896 bool ret = false; 789 bool ret = false;
897 uint8_t *orig_addr, orig_ttvn; 790 uint8_t *orig_addr, orig_ttvn;
898 791
899 if (batadv_is_my_client(bat_priv, dst_addr)) { 792 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
900 primary_if = batadv_primary_if_get_selected(bat_priv); 793 primary_if = batadv_primary_if_get_selected(bat_priv);
901 if (!primary_if) 794 if (!primary_if)
902 goto out; 795 goto out;
903 orig_addr = primary_if->net_dev->dev_addr; 796 orig_addr = primary_if->net_dev->dev_addr;
904 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 797 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
905 } else { 798 } else {
906 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr); 799 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
800 vid);
907 if (!orig_node) 801 if (!orig_node)
908 goto out; 802 goto out;
909 803
@@ -930,11 +824,12 @@ out:
930 824
931static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv, 825static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
932 struct sk_buff *skb, int hdr_len) { 826 struct sk_buff *skb, int hdr_len) {
933 uint8_t curr_ttvn, old_ttvn; 827 struct batadv_unicast_packet *unicast_packet;
828 struct batadv_hard_iface *primary_if;
934 struct batadv_orig_node *orig_node; 829 struct batadv_orig_node *orig_node;
830 uint8_t curr_ttvn, old_ttvn;
935 struct ethhdr *ethhdr; 831 struct ethhdr *ethhdr;
936 struct batadv_hard_iface *primary_if; 832 unsigned short vid;
937 struct batadv_unicast_packet *unicast_packet;
938 int is_old_ttvn; 833 int is_old_ttvn;
939 834
940 /* check if there is enough data before accessing it */ 835 /* check if there is enough data before accessing it */
@@ -946,6 +841,7 @@ static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
946 return 0; 841 return 0;
947 842
948 unicast_packet = (struct batadv_unicast_packet *)skb->data; 843 unicast_packet = (struct batadv_unicast_packet *)skb->data;
844 vid = batadv_get_vid(skb, hdr_len);
949 ethhdr = (struct ethhdr *)(skb->data + hdr_len); 845 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
950 846
951 /* check if the destination client was served by this node and it is now 847 /* check if the destination client was served by this node and it is now
@@ -953,9 +849,9 @@ static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
953 * message and that it knows the new destination in the mesh to re-route 849 * message and that it knows the new destination in the mesh to re-route
954 * the packet to 850 * the packet to
955 */ 851 */
956 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) { 852 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
957 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet, 853 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
958 ethhdr->h_dest)) 854 ethhdr->h_dest, vid))
959 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, 855 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
960 bat_priv, 856 bat_priv,
961 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n", 857 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
@@ -1001,7 +897,7 @@ static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
1001 * target host 897 * target host
1002 */ 898 */
1003 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet, 899 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
1004 ethhdr->h_dest)) { 900 ethhdr->h_dest, vid)) {
1005 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv, 901 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
1006 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n", 902 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1007 unicast_packet->dest, ethhdr->h_dest, 903 unicast_packet->dest, ethhdr->h_dest,
@@ -1013,7 +909,7 @@ static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
1013 * currently served by this node or there is no destination at all and 909 * currently served by this node or there is no destination at all and
1014 * it is possible to drop the packet 910 * it is possible to drop the packet
1015 */ 911 */
1016 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest)) 912 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
1017 return 0; 913 return 0;
1018 914
1019 /* update the header in order to let the packet be delivered to this 915 /* update the header in order to let the packet be delivered to this
@@ -1032,6 +928,34 @@ static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
1032 return 1; 928 return 1;
1033} 929}
1034 930
931/**
932 * batadv_recv_unhandled_unicast_packet - receive and process packets which
933 * are in the unicast number space but not yet known to the implementation
934 * @skb: unicast tvlv packet to process
935 * @recv_if: pointer to interface this packet was received on
936 *
937 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
938 * otherwise.
939 */
940int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
941 struct batadv_hard_iface *recv_if)
942{
943 struct batadv_unicast_packet *unicast_packet;
944 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
945 int check, hdr_size = sizeof(*unicast_packet);
946
947 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
948 if (check < 0)
949 return NET_RX_DROP;
950
951 /* we don't know about this type, drop it. */
952 unicast_packet = (struct batadv_unicast_packet *)skb->data;
953 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
954 return NET_RX_DROP;
955
956 return batadv_route_unicast_packet(skb, recv_if);
957}
958
1035int batadv_recv_unicast_packet(struct sk_buff *skb, 959int batadv_recv_unicast_packet(struct sk_buff *skb,
1036 struct batadv_hard_iface *recv_if) 960 struct batadv_hard_iface *recv_if)
1037{ 961{
@@ -1046,7 +970,7 @@ int batadv_recv_unicast_packet(struct sk_buff *skb,
1046 unicast_packet = (struct batadv_unicast_packet *)skb->data; 970 unicast_packet = (struct batadv_unicast_packet *)skb->data;
1047 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data; 971 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
1048 972
1049 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR; 973 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
1050 /* the caller function should have already pulled 2 bytes */ 974 /* the caller function should have already pulled 2 bytes */
1051 if (is4addr) 975 if (is4addr)
1052 hdr_size = sizeof(*unicast_4addr_packet); 976 hdr_size = sizeof(*unicast_4addr_packet);
@@ -1094,51 +1018,112 @@ rx_success:
1094 return batadv_route_unicast_packet(skb, recv_if); 1018 return batadv_route_unicast_packet(skb, recv_if);
1095} 1019}
1096 1020
1097int batadv_recv_ucast_frag_packet(struct sk_buff *skb, 1021/**
1098 struct batadv_hard_iface *recv_if) 1022 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
1023 * @skb: unicast tvlv packet to process
1024 * @recv_if: pointer to interface this packet was received on
1025 * @dst_addr: the payload destination
1026 *
1027 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
1028 * otherwise.
1029 */
1030int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1031 struct batadv_hard_iface *recv_if)
1099{ 1032{
1100 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface); 1033 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1101 struct batadv_unicast_frag_packet *unicast_packet; 1034 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1102 int hdr_size = sizeof(*unicast_packet); 1035 unsigned char *tvlv_buff;
1103 struct sk_buff *new_skb = NULL; 1036 uint16_t tvlv_buff_len;
1104 int ret; 1037 int hdr_size = sizeof(*unicast_tvlv_packet);
1038 int ret = NET_RX_DROP;
1105 1039
1106 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0) 1040 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1107 return NET_RX_DROP; 1041 return NET_RX_DROP;
1108 1042
1109 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size)) 1043 /* the header is likely to be modified while forwarding */
1044 if (skb_cow(skb, hdr_size) < 0)
1110 return NET_RX_DROP; 1045 return NET_RX_DROP;
1111 1046
1112 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data; 1047 /* packet needs to be linearized to access the tvlv content */
1048 if (skb_linearize(skb) < 0)
1049 return NET_RX_DROP;
1113 1050
1114 /* packet for me */ 1051 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1115 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
1116 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1117 1052
1118 if (ret == NET_RX_DROP) 1053 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1119 return NET_RX_DROP; 1054 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1120 1055
1121 /* packet was buffered for late merge */ 1056 if (tvlv_buff_len > skb->len - hdr_size)
1122 if (!new_skb) 1057 return NET_RX_DROP;
1123 return NET_RX_SUCCESS;
1124 1058
1125 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb, 1059 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1126 hdr_size)) 1060 unicast_tvlv_packet->src,
1127 goto rx_success; 1061 unicast_tvlv_packet->dst,
1128 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb, 1062 tvlv_buff, tvlv_buff_len);
1129 hdr_size))
1130 goto rx_success;
1131 1063
1132 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if, 1064 if (ret != NET_RX_SUCCESS)
1133 sizeof(struct batadv_unicast_packet), NULL); 1065 ret = batadv_route_unicast_packet(skb, recv_if);
1134 1066
1135rx_success: 1067 return ret;
1136 return NET_RX_SUCCESS; 1068}
1069
1070/**
1071 * batadv_recv_frag_packet - process received fragment
1072 * @skb: the received fragment
1073 * @recv_if: interface that the skb is received on
1074 *
1075 * This function does one of the three following things: 1) Forward fragment, if
1076 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
1077 * lack further fragments; 3) Merge fragments, if we have all needed parts.
1078 *
1079 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1080 */
1081int batadv_recv_frag_packet(struct sk_buff *skb,
1082 struct batadv_hard_iface *recv_if)
1083{
1084 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1085 struct batadv_orig_node *orig_node_src = NULL;
1086 struct batadv_frag_packet *frag_packet;
1087 int ret = NET_RX_DROP;
1088
1089 if (batadv_check_unicast_packet(bat_priv, skb,
1090 sizeof(*frag_packet)) < 0)
1091 goto out;
1092
1093 frag_packet = (struct batadv_frag_packet *)skb->data;
1094 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1095 if (!orig_node_src)
1096 goto out;
1097
1098 /* Route the fragment if it is not for us and too big to be merged. */
1099 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1100 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1101 ret = NET_RX_SUCCESS;
1102 goto out;
1137 } 1103 }
1138 1104
1139 return batadv_route_unicast_packet(skb, recv_if); 1105 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1140} 1106 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1141 1107
1108 /* Add fragment to buffer and merge if possible. */
1109 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1110 goto out;
1111
1112 /* Deliver merged packet to the appropriate handler, if it was
1113 * merged
1114 */
1115 if (skb)
1116 batadv_batman_skb_recv(skb, recv_if->net_dev,
1117 &recv_if->batman_adv_ptype, NULL);
1118
1119 ret = NET_RX_SUCCESS;
1120
1121out:
1122 if (orig_node_src)
1123 batadv_orig_node_free_ref(orig_node_src);
1124
1125 return ret;
1126}
1142 1127
1143int batadv_recv_bcast_packet(struct sk_buff *skb, 1128int batadv_recv_bcast_packet(struct sk_buff *skb,
1144 struct batadv_hard_iface *recv_if) 1129 struct batadv_hard_iface *recv_if)
@@ -1175,7 +1160,7 @@ int batadv_recv_bcast_packet(struct sk_buff *skb,
1175 if (batadv_is_my_mac(bat_priv, bcast_packet->orig)) 1160 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1176 goto out; 1161 goto out;
1177 1162
1178 if (bcast_packet->header.ttl < 2) 1163 if (bcast_packet->ttl < 2)
1179 goto out; 1164 goto out;
1180 1165
1181 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig); 1166 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
@@ -1240,53 +1225,3 @@ out:
1240 batadv_orig_node_free_ref(orig_node); 1225 batadv_orig_node_free_ref(orig_node);
1241 return ret; 1226 return ret;
1242} 1227}
1243
1244int batadv_recv_vis_packet(struct sk_buff *skb,
1245 struct batadv_hard_iface *recv_if)
1246{
1247 struct batadv_vis_packet *vis_packet;
1248 struct ethhdr *ethhdr;
1249 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1250 int hdr_size = sizeof(*vis_packet);
1251
1252 /* keep skb linear */
1253 if (skb_linearize(skb) < 0)
1254 return NET_RX_DROP;
1255
1256 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1257 return NET_RX_DROP;
1258
1259 vis_packet = (struct batadv_vis_packet *)skb->data;
1260 ethhdr = eth_hdr(skb);
1261
1262 /* not for me */
1263 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
1264 return NET_RX_DROP;
1265
1266 /* ignore own packets */
1267 if (batadv_is_my_mac(bat_priv, vis_packet->vis_orig))
1268 return NET_RX_DROP;
1269
1270 if (batadv_is_my_mac(bat_priv, vis_packet->sender_orig))
1271 return NET_RX_DROP;
1272
1273 switch (vis_packet->vis_type) {
1274 case BATADV_VIS_TYPE_SERVER_SYNC:
1275 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1276 skb_headlen(skb));
1277 break;
1278
1279 case BATADV_VIS_TYPE_CLIENT_UPDATE:
1280 batadv_receive_client_update_packet(bat_priv, vis_packet,
1281 skb_headlen(skb));
1282 break;
1283
1284 default: /* ignore unknown packet */
1285 break;
1286 }
1287
1288 /* We take a copy of the data in the packet, so we should
1289 * always free the skbuf.
1290 */
1291 return NET_RX_DROP;
1292}
diff --git a/net/batman-adv/routing.h b/net/batman-adv/routing.h
index 72a29bde2010..19544ddb81b5 100644
--- a/net/batman-adv/routing.h
+++ b/net/batman-adv/routing.h
@@ -30,23 +30,26 @@ int batadv_recv_icmp_packet(struct sk_buff *skb,
30 struct batadv_hard_iface *recv_if); 30 struct batadv_hard_iface *recv_if);
31int batadv_recv_unicast_packet(struct sk_buff *skb, 31int batadv_recv_unicast_packet(struct sk_buff *skb,
32 struct batadv_hard_iface *recv_if); 32 struct batadv_hard_iface *recv_if);
33int batadv_recv_ucast_frag_packet(struct sk_buff *skb, 33int batadv_recv_frag_packet(struct sk_buff *skb,
34 struct batadv_hard_iface *recv_if); 34 struct batadv_hard_iface *iface);
35int batadv_recv_bcast_packet(struct sk_buff *skb, 35int batadv_recv_bcast_packet(struct sk_buff *skb,
36 struct batadv_hard_iface *recv_if); 36 struct batadv_hard_iface *recv_if);
37int batadv_recv_vis_packet(struct sk_buff *skb,
38 struct batadv_hard_iface *recv_if);
39int batadv_recv_tt_query(struct sk_buff *skb, 37int batadv_recv_tt_query(struct sk_buff *skb,
40 struct batadv_hard_iface *recv_if); 38 struct batadv_hard_iface *recv_if);
41int batadv_recv_roam_adv(struct sk_buff *skb, 39int batadv_recv_roam_adv(struct sk_buff *skb,
42 struct batadv_hard_iface *recv_if); 40 struct batadv_hard_iface *recv_if);
41int batadv_recv_unicast_tvlv(struct sk_buff *skb,
42 struct batadv_hard_iface *recv_if);
43int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
44 struct batadv_hard_iface *recv_if);
43struct batadv_neigh_node * 45struct batadv_neigh_node *
44batadv_find_router(struct batadv_priv *bat_priv, 46batadv_find_router(struct batadv_priv *bat_priv,
45 struct batadv_orig_node *orig_node, 47 struct batadv_orig_node *orig_node,
46 const struct batadv_hard_iface *recv_if); 48 const struct batadv_hard_iface *recv_if);
47void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node, 49void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
48 struct batadv_neigh_node *neigh_node); 50 struct batadv_neigh_node *neigh_node);
49void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node, 51void batadv_bonding_candidate_add(struct batadv_priv *bat_priv,
52 struct batadv_orig_node *orig_node,
50 struct batadv_neigh_node *neigh_node); 53 struct batadv_neigh_node *neigh_node);
51void batadv_bonding_save_primary(const struct batadv_orig_node *orig_node, 54void batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
52 struct batadv_orig_node *orig_neigh_node, 55 struct batadv_orig_node *orig_neigh_node,
diff --git a/net/batman-adv/send.c b/net/batman-adv/send.c
index 0266edd0fa7f..fba4dcfcfac2 100644
--- a/net/batman-adv/send.c
+++ b/net/batman-adv/send.c
@@ -24,12 +24,11 @@
24#include "translation-table.h" 24#include "translation-table.h"
25#include "soft-interface.h" 25#include "soft-interface.h"
26#include "hard-interface.h" 26#include "hard-interface.h"
27#include "vis.h"
28#include "gateway_common.h" 27#include "gateway_common.h"
28#include "gateway_client.h"
29#include "originator.h" 29#include "originator.h"
30#include "network-coding.h" 30#include "network-coding.h"
31 31#include "fragmentation.h"
32#include <linux/if_ether.h>
33 32
34static void batadv_send_outstanding_bcast_packet(struct work_struct *work); 33static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
35 34
@@ -64,10 +63,10 @@ int batadv_send_skb_packet(struct sk_buff *skb,
64 ethhdr = eth_hdr(skb); 63 ethhdr = eth_hdr(skb);
65 memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN); 64 memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
66 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN); 65 memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
67 ethhdr->h_proto = __constant_htons(ETH_P_BATMAN); 66 ethhdr->h_proto = htons(ETH_P_BATMAN);
68 67
69 skb_set_network_header(skb, ETH_HLEN); 68 skb_set_network_header(skb, ETH_HLEN);
70 skb->protocol = __constant_htons(ETH_P_BATMAN); 69 skb->protocol = htons(ETH_P_BATMAN);
71 70
72 skb->dev = hard_iface->net_dev; 71 skb->dev = hard_iface->net_dev;
73 72
@@ -109,7 +108,19 @@ int batadv_send_skb_to_orig(struct sk_buff *skb,
109 /* batadv_find_router() increases neigh_nodes refcount if found. */ 108 /* batadv_find_router() increases neigh_nodes refcount if found. */
110 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if); 109 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
111 if (!neigh_node) 110 if (!neigh_node)
112 return ret; 111 goto out;
112
113 /* Check if the skb is too large to send in one piece and fragment
114 * it if needed.
115 */
116 if (atomic_read(&bat_priv->fragmentation) &&
117 skb->len > neigh_node->if_incoming->net_dev->mtu) {
118 /* Fragment and send packet. */
119 if (batadv_frag_send_packet(skb, orig_node, neigh_node))
120 ret = NET_XMIT_SUCCESS;
121
122 goto out;
123 }
113 124
114 /* try to network code the packet, if it is received on an interface 125 /* try to network code the packet, if it is received on an interface
115 * (i.e. being forwarded). If the packet originates from this node or if 126 * (i.e. being forwarded). If the packet originates from this node or if
@@ -123,11 +134,225 @@ int batadv_send_skb_to_orig(struct sk_buff *skb,
123 ret = NET_XMIT_SUCCESS; 134 ret = NET_XMIT_SUCCESS;
124 } 135 }
125 136
126 batadv_neigh_node_free_ref(neigh_node); 137out:
138 if (neigh_node)
139 batadv_neigh_node_free_ref(neigh_node);
140
141 return ret;
142}
143
144/**
145 * batadv_send_skb_push_fill_unicast - extend the buffer and initialize the
146 * common fields for unicast packets
147 * @skb: the skb carrying the unicast header to initialize
148 * @hdr_size: amount of bytes to push at the beginning of the skb
149 * @orig_node: the destination node
150 *
151 * Returns false if the buffer extension was not possible or true otherwise.
152 */
153static bool
154batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,
155 struct batadv_orig_node *orig_node)
156{
157 struct batadv_unicast_packet *unicast_packet;
158 uint8_t ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
159
160 if (batadv_skb_head_push(skb, hdr_size) < 0)
161 return false;
162
163 unicast_packet = (struct batadv_unicast_packet *)skb->data;
164 unicast_packet->version = BATADV_COMPAT_VERSION;
165 /* batman packet type: unicast */
166 unicast_packet->packet_type = BATADV_UNICAST;
167 /* set unicast ttl */
168 unicast_packet->ttl = BATADV_TTL;
169 /* copy the destination for faster routing */
170 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
171 /* set the destination tt version number */
172 unicast_packet->ttvn = ttvn;
173
174 return true;
175}
176
177/**
178 * batadv_send_skb_prepare_unicast - encapsulate an skb with a unicast header
179 * @skb: the skb containing the payload to encapsulate
180 * @orig_node: the destination node
181 *
182 * Returns false if the payload could not be encapsulated or true otherwise.
183 */
184static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,
185 struct batadv_orig_node *orig_node)
186{
187 size_t uni_size = sizeof(struct batadv_unicast_packet);
188
189 return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);
190}
191
192/**
193 * batadv_send_skb_prepare_unicast_4addr - encapsulate an skb with a
194 * unicast 4addr header
195 * @bat_priv: the bat priv with all the soft interface information
196 * @skb: the skb containing the payload to encapsulate
197 * @orig_node: the destination node
198 * @packet_subtype: the unicast 4addr packet subtype to use
199 *
200 * Returns false if the payload could not be encapsulated or true otherwise.
201 */
202bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
203 struct sk_buff *skb,
204 struct batadv_orig_node *orig,
205 int packet_subtype)
206{
207 struct batadv_hard_iface *primary_if;
208 struct batadv_unicast_4addr_packet *uc_4addr_packet;
209 bool ret = false;
210
211 primary_if = batadv_primary_if_get_selected(bat_priv);
212 if (!primary_if)
213 goto out;
214
215 /* Pull the header space and fill the unicast_packet substructure.
216 * We can do that because the first member of the uc_4addr_packet
217 * is of type struct unicast_packet
218 */
219 if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),
220 orig))
221 goto out;
222
223 uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
224 uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;
225 memcpy(uc_4addr_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
226 uc_4addr_packet->subtype = packet_subtype;
227 uc_4addr_packet->reserved = 0;
228
229 ret = true;
230out:
231 if (primary_if)
232 batadv_hardif_free_ref(primary_if);
233 return ret;
234}
235
236/**
237 * batadv_send_skb_unicast - encapsulate and send an skb via unicast
238 * @bat_priv: the bat priv with all the soft interface information
239 * @skb: payload to send
240 * @packet_type: the batman unicast packet type to use
241 * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
242 * 4addr packets)
243 * @orig_node: the originator to send the packet to
244 * @vid: the vid to be used to search the translation table
245 *
246 * Wrap the given skb into a batman-adv unicast or unicast-4addr header
247 * depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied
248 * as packet_type. Then send this frame to the given orig_node and release a
249 * reference to this orig_node.
250 *
251 * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
252 */
253static int batadv_send_skb_unicast(struct batadv_priv *bat_priv,
254 struct sk_buff *skb, int packet_type,
255 int packet_subtype,
256 struct batadv_orig_node *orig_node,
257 unsigned short vid)
258{
259 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
260 struct batadv_unicast_packet *unicast_packet;
261 int ret = NET_XMIT_DROP;
262
263 if (!orig_node)
264 goto out;
265
266 switch (packet_type) {
267 case BATADV_UNICAST:
268 if (!batadv_send_skb_prepare_unicast(skb, orig_node))
269 goto out;
270 break;
271 case BATADV_UNICAST_4ADDR:
272 if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,
273 orig_node,
274 packet_subtype))
275 goto out;
276 break;
277 default:
278 /* this function supports UNICAST and UNICAST_4ADDR only. It
279 * should never be invoked with any other packet type
280 */
281 goto out;
282 }
283
284 unicast_packet = (struct batadv_unicast_packet *)skb->data;
285
286 /* inform the destination node that we are still missing a correct route
287 * for this client. The destination will receive this packet and will
288 * try to reroute it because the ttvn contained in the header is less
289 * than the current one
290 */
291 if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))
292 unicast_packet->ttvn = unicast_packet->ttvn - 1;
127 293
294 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
295 ret = NET_XMIT_SUCCESS;
296
297out:
298 if (orig_node)
299 batadv_orig_node_free_ref(orig_node);
300 if (ret == NET_XMIT_DROP)
301 kfree_skb(skb);
128 return ret; 302 return ret;
129} 303}
130 304
305/**
306 * batadv_send_skb_via_tt_generic - send an skb via TT lookup
307 * @bat_priv: the bat priv with all the soft interface information
308 * @skb: payload to send
309 * @packet_type: the batman unicast packet type to use
310 * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
311 * 4addr packets)
312 * @vid: the vid to be used to search the translation table
313 *
314 * Look up the recipient node for the destination address in the ethernet
315 * header via the translation table. Wrap the given skb into a batman-adv
316 * unicast or unicast-4addr header depending on whether BATADV_UNICAST or
317 * BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame
318 * to the according destination node.
319 *
320 * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
321 */
322int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,
323 struct sk_buff *skb, int packet_type,
324 int packet_subtype, unsigned short vid)
325{
326 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
327 struct batadv_orig_node *orig_node;
328
329 orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
330 ethhdr->h_dest, vid);
331 return batadv_send_skb_unicast(bat_priv, skb, packet_type,
332 packet_subtype, orig_node, vid);
333}
334
335/**
336 * batadv_send_skb_via_gw - send an skb via gateway lookup
337 * @bat_priv: the bat priv with all the soft interface information
338 * @skb: payload to send
339 * @vid: the vid to be used to search the translation table
340 *
341 * Look up the currently selected gateway. Wrap the given skb into a batman-adv
342 * unicast header and send this frame to this gateway node.
343 *
344 * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
345 */
346int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,
347 unsigned short vid)
348{
349 struct batadv_orig_node *orig_node;
350
351 orig_node = batadv_gw_get_selected_orig(bat_priv);
352 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
353 orig_node, vid);
354}
355
131void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface) 356void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
132{ 357{
133 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); 358 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
@@ -211,7 +436,7 @@ int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
211 436
212 /* as we have a copy now, it is safe to decrease the TTL */ 437 /* as we have a copy now, it is safe to decrease the TTL */
213 bcast_packet = (struct batadv_bcast_packet *)newskb->data; 438 bcast_packet = (struct batadv_bcast_packet *)newskb->data;
214 bcast_packet->header.ttl--; 439 bcast_packet->ttl--;
215 440
216 skb_reset_mac_header(newskb); 441 skb_reset_mac_header(newskb);
217 442
diff --git a/net/batman-adv/send.h b/net/batman-adv/send.h
index e7b17880fca4..aa2e2537a739 100644
--- a/net/batman-adv/send.h
+++ b/net/batman-adv/send.h
@@ -34,5 +34,58 @@ void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work);
34void 34void
35batadv_purge_outstanding_packets(struct batadv_priv *bat_priv, 35batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
36 const struct batadv_hard_iface *hard_iface); 36 const struct batadv_hard_iface *hard_iface);
37bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
38 struct sk_buff *skb,
39 struct batadv_orig_node *orig_node,
40 int packet_subtype);
41int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,
42 struct sk_buff *skb, int packet_type,
43 int packet_subtype, unsigned short vid);
44int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,
45 unsigned short vid);
46
47/**
48 * batadv_send_skb_via_tt - send an skb via TT lookup
49 * @bat_priv: the bat priv with all the soft interface information
50 * @skb: the payload to send
51 * @vid: the vid to be used to search the translation table
52 *
53 * Look up the recipient node for the destination address in the ethernet
54 * header via the translation table. Wrap the given skb into a batman-adv
55 * unicast header. Then send this frame to the according destination node.
56 *
57 * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
58 */
59static inline int batadv_send_skb_via_tt(struct batadv_priv *bat_priv,
60 struct sk_buff *skb,
61 unsigned short vid)
62{
63 return batadv_send_skb_via_tt_generic(bat_priv, skb, BATADV_UNICAST, 0,
64 vid);
65}
66
67/**
68 * batadv_send_skb_via_tt_4addr - send an skb via TT lookup
69 * @bat_priv: the bat priv with all the soft interface information
70 * @skb: the payload to send
71 * @packet_subtype: the unicast 4addr packet subtype to use
72 * @vid: the vid to be used to search the translation table
73 *
74 * Look up the recipient node for the destination address in the ethernet
75 * header via the translation table. Wrap the given skb into a batman-adv
76 * unicast-4addr header. Then send this frame to the according destination
77 * node.
78 *
79 * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
80 */
81static inline int batadv_send_skb_via_tt_4addr(struct batadv_priv *bat_priv,
82 struct sk_buff *skb,
83 int packet_subtype,
84 unsigned short vid)
85{
86 return batadv_send_skb_via_tt_generic(bat_priv, skb,
87 BATADV_UNICAST_4ADDR,
88 packet_subtype, vid);
89}
37 90
38#endif /* _NET_BATMAN_ADV_SEND_H_ */ 91#endif /* _NET_BATMAN_ADV_SEND_H_ */
diff --git a/net/batman-adv/soft-interface.c b/net/batman-adv/soft-interface.c
index 813db4e64602..a8f99d1486c0 100644
--- a/net/batman-adv/soft-interface.c
+++ b/net/batman-adv/soft-interface.c
@@ -34,8 +34,6 @@
34#include <linux/ethtool.h> 34#include <linux/ethtool.h>
35#include <linux/etherdevice.h> 35#include <linux/etherdevice.h>
36#include <linux/if_vlan.h> 36#include <linux/if_vlan.h>
37#include <linux/if_ether.h>
38#include "unicast.h"
39#include "bridge_loop_avoidance.h" 37#include "bridge_loop_avoidance.h"
40#include "network-coding.h" 38#include "network-coding.h"
41 39
@@ -120,9 +118,10 @@ static int batadv_interface_set_mac_addr(struct net_device *dev, void *p)
120 118
121 /* only modify transtable if it has been initialized before */ 119 /* only modify transtable if it has been initialized before */
122 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) { 120 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE) {
123 batadv_tt_local_remove(bat_priv, old_addr, 121 batadv_tt_local_remove(bat_priv, old_addr, BATADV_NO_FLAGS,
124 "mac address changed", false); 122 "mac address changed", false);
125 batadv_tt_local_add(dev, addr->sa_data, BATADV_NULL_IFINDEX); 123 batadv_tt_local_add(dev, addr->sa_data, BATADV_NO_FLAGS,
124 BATADV_NULL_IFINDEX);
126 } 125 }
127 126
128 return 0; 127 return 0;
@@ -139,36 +138,48 @@ static int batadv_interface_change_mtu(struct net_device *dev, int new_mtu)
139 return 0; 138 return 0;
140} 139}
141 140
141/**
142 * batadv_interface_set_rx_mode - set the rx mode of a device
143 * @dev: registered network device to modify
144 *
145 * We do not actually need to set any rx filters for the virtual batman
146 * soft interface. However a dummy handler enables a user to set static
147 * multicast listeners for instance.
148 */
149static void batadv_interface_set_rx_mode(struct net_device *dev)
150{
151}
152
142static int batadv_interface_tx(struct sk_buff *skb, 153static int batadv_interface_tx(struct sk_buff *skb,
143 struct net_device *soft_iface) 154 struct net_device *soft_iface)
144{ 155{
145 struct ethhdr *ethhdr = (struct ethhdr *)skb->data; 156 struct ethhdr *ethhdr;
146 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 157 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
147 struct batadv_hard_iface *primary_if = NULL; 158 struct batadv_hard_iface *primary_if = NULL;
148 struct batadv_bcast_packet *bcast_packet; 159 struct batadv_bcast_packet *bcast_packet;
149 struct vlan_ethhdr *vhdr; 160 __be16 ethertype = htons(ETH_P_BATMAN);
150 __be16 ethertype = __constant_htons(ETH_P_BATMAN);
151 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00, 161 static const uint8_t stp_addr[ETH_ALEN] = {0x01, 0x80, 0xC2, 0x00,
152 0x00, 0x00}; 162 0x00, 0x00};
153 static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00, 163 static const uint8_t ectp_addr[ETH_ALEN] = {0xCF, 0x00, 0x00, 0x00,
154 0x00, 0x00}; 164 0x00, 0x00};
165 struct vlan_ethhdr *vhdr;
155 unsigned int header_len = 0; 166 unsigned int header_len = 0;
156 int data_len = skb->len, ret; 167 int data_len = skb->len, ret;
157 unsigned short vid __maybe_unused = BATADV_NO_FLAGS;
158 bool do_bcast = false;
159 uint32_t seqno;
160 unsigned long brd_delay = 1; 168 unsigned long brd_delay = 1;
169 bool do_bcast = false, client_added;
170 unsigned short vid;
171 uint32_t seqno;
161 172
162 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) 173 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
163 goto dropped; 174 goto dropped;
164 175
165 soft_iface->trans_start = jiffies; 176 soft_iface->trans_start = jiffies;
177 vid = batadv_get_vid(skb, 0);
178 ethhdr = (struct ethhdr *)skb->data;
166 179
167 switch (ntohs(ethhdr->h_proto)) { 180 switch (ntohs(ethhdr->h_proto)) {
168 case ETH_P_8021Q: 181 case ETH_P_8021Q:
169 vhdr = (struct vlan_ethhdr *)skb->data; 182 vhdr = (struct vlan_ethhdr *)skb->data;
170 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
171 vid |= BATADV_VLAN_HAS_TAG;
172 183
173 if (vhdr->h_vlan_encapsulated_proto != ethertype) 184 if (vhdr->h_vlan_encapsulated_proto != ethertype)
174 break; 185 break;
@@ -185,8 +196,12 @@ static int batadv_interface_tx(struct sk_buff *skb,
185 ethhdr = (struct ethhdr *)skb->data; 196 ethhdr = (struct ethhdr *)skb->data;
186 197
187 /* Register the client MAC in the transtable */ 198 /* Register the client MAC in the transtable */
188 if (!is_multicast_ether_addr(ethhdr->h_source)) 199 if (!is_multicast_ether_addr(ethhdr->h_source)) {
189 batadv_tt_local_add(soft_iface, ethhdr->h_source, skb->skb_iif); 200 client_added = batadv_tt_local_add(soft_iface, ethhdr->h_source,
201 vid, skb->skb_iif);
202 if (!client_added)
203 goto dropped;
204 }
190 205
191 /* don't accept stp packets. STP does not help in meshes. 206 /* don't accept stp packets. STP does not help in meshes.
192 * better use the bridge loop avoidance ... 207 * better use the bridge loop avoidance ...
@@ -249,11 +264,11 @@ static int batadv_interface_tx(struct sk_buff *skb,
249 goto dropped; 264 goto dropped;
250 265
251 bcast_packet = (struct batadv_bcast_packet *)skb->data; 266 bcast_packet = (struct batadv_bcast_packet *)skb->data;
252 bcast_packet->header.version = BATADV_COMPAT_VERSION; 267 bcast_packet->version = BATADV_COMPAT_VERSION;
253 bcast_packet->header.ttl = BATADV_TTL; 268 bcast_packet->ttl = BATADV_TTL;
254 269
255 /* batman packet type: broadcast */ 270 /* batman packet type: broadcast */
256 bcast_packet->header.packet_type = BATADV_BCAST; 271 bcast_packet->packet_type = BATADV_BCAST;
257 bcast_packet->reserved = 0; 272 bcast_packet->reserved = 0;
258 273
259 /* hw address of first interface is the orig mac because only 274 /* hw address of first interface is the orig mac because only
@@ -286,8 +301,12 @@ static int batadv_interface_tx(struct sk_buff *skb,
286 301
287 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb); 302 batadv_dat_snoop_outgoing_arp_reply(bat_priv, skb);
288 303
289 ret = batadv_unicast_send_skb(bat_priv, skb); 304 if (is_multicast_ether_addr(ethhdr->h_dest))
290 if (ret != 0) 305 ret = batadv_send_skb_via_gw(bat_priv, skb, vid);
306 else
307 ret = batadv_send_skb_via_tt(bat_priv, skb, vid);
308
309 if (ret == NET_XMIT_DROP)
291 goto dropped_freed; 310 goto dropped_freed;
292 } 311 }
293 312
@@ -309,15 +328,16 @@ void batadv_interface_rx(struct net_device *soft_iface,
309 struct sk_buff *skb, struct batadv_hard_iface *recv_if, 328 struct sk_buff *skb, struct batadv_hard_iface *recv_if,
310 int hdr_size, struct batadv_orig_node *orig_node) 329 int hdr_size, struct batadv_orig_node *orig_node)
311{ 330{
331 struct batadv_bcast_packet *batadv_bcast_packet;
312 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 332 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
313 struct ethhdr *ethhdr; 333 __be16 ethertype = htons(ETH_P_BATMAN);
314 struct vlan_ethhdr *vhdr; 334 struct vlan_ethhdr *vhdr;
315 struct batadv_header *batadv_header = (struct batadv_header *)skb->data; 335 struct ethhdr *ethhdr;
316 unsigned short vid __maybe_unused = BATADV_NO_FLAGS; 336 unsigned short vid;
317 __be16 ethertype = __constant_htons(ETH_P_BATMAN);
318 bool is_bcast; 337 bool is_bcast;
319 338
320 is_bcast = (batadv_header->packet_type == BATADV_BCAST); 339 batadv_bcast_packet = (struct batadv_bcast_packet *)skb->data;
340 is_bcast = (batadv_bcast_packet->packet_type == BATADV_BCAST);
321 341
322 /* check if enough space is available for pulling, and pull */ 342 /* check if enough space is available for pulling, and pull */
323 if (!pskb_may_pull(skb, hdr_size)) 343 if (!pskb_may_pull(skb, hdr_size))
@@ -326,13 +346,17 @@ void batadv_interface_rx(struct net_device *soft_iface,
326 skb_pull_rcsum(skb, hdr_size); 346 skb_pull_rcsum(skb, hdr_size);
327 skb_reset_mac_header(skb); 347 skb_reset_mac_header(skb);
328 348
349 /* clean the netfilter state now that the batman-adv header has been
350 * removed
351 */
352 nf_reset(skb);
353
354 vid = batadv_get_vid(skb, 0);
329 ethhdr = eth_hdr(skb); 355 ethhdr = eth_hdr(skb);
330 356
331 switch (ntohs(ethhdr->h_proto)) { 357 switch (ntohs(ethhdr->h_proto)) {
332 case ETH_P_8021Q: 358 case ETH_P_8021Q:
333 vhdr = (struct vlan_ethhdr *)skb->data; 359 vhdr = (struct vlan_ethhdr *)skb->data;
334 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
335 vid |= BATADV_VLAN_HAS_TAG;
336 360
337 if (vhdr->h_vlan_encapsulated_proto != ethertype) 361 if (vhdr->h_vlan_encapsulated_proto != ethertype)
338 break; 362 break;
@@ -368,9 +392,10 @@ void batadv_interface_rx(struct net_device *soft_iface,
368 392
369 if (orig_node) 393 if (orig_node)
370 batadv_tt_add_temporary_global_entry(bat_priv, orig_node, 394 batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
371 ethhdr->h_source); 395 ethhdr->h_source, vid);
372 396
373 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest)) 397 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest,
398 vid))
374 goto dropped; 399 goto dropped;
375 400
376 netif_rx(skb); 401 netif_rx(skb);
@@ -382,6 +407,177 @@ out:
382 return; 407 return;
383} 408}
384 409
410/**
411 * batadv_softif_vlan_free_ref - decrease the vlan object refcounter and
412 * possibly free it
413 * @softif_vlan: the vlan object to release
414 */
415void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *softif_vlan)
416{
417 if (atomic_dec_and_test(&softif_vlan->refcount))
418 kfree_rcu(softif_vlan, rcu);
419}
420
421/**
422 * batadv_softif_vlan_get - get the vlan object for a specific vid
423 * @bat_priv: the bat priv with all the soft interface information
424 * @vid: the identifier of the vlan object to retrieve
425 *
426 * Returns the private data of the vlan matching the vid passed as argument or
427 * NULL otherwise. The refcounter of the returned object is incremented by 1.
428 */
429struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
430 unsigned short vid)
431{
432 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
433
434 rcu_read_lock();
435 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
436 if (vlan_tmp->vid != vid)
437 continue;
438
439 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
440 continue;
441
442 vlan = vlan_tmp;
443 break;
444 }
445 rcu_read_unlock();
446
447 return vlan;
448}
449
450/**
451 * batadv_create_vlan - allocate the needed resources for a new vlan
452 * @bat_priv: the bat priv with all the soft interface information
453 * @vid: the VLAN identifier
454 *
455 * Returns 0 on success, a negative error otherwise.
456 */
457int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
458{
459 struct batadv_softif_vlan *vlan;
460 int err;
461
462 vlan = batadv_softif_vlan_get(bat_priv, vid);
463 if (vlan) {
464 batadv_softif_vlan_free_ref(vlan);
465 return -EEXIST;
466 }
467
468 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
469 if (!vlan)
470 return -ENOMEM;
471
472 vlan->vid = vid;
473 atomic_set(&vlan->refcount, 1);
474
475 atomic_set(&vlan->ap_isolation, 0);
476
477 err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
478 if (err) {
479 kfree(vlan);
480 return err;
481 }
482
483 /* add a new TT local entry. This one will be marked with the NOPURGE
484 * flag
485 */
486 batadv_tt_local_add(bat_priv->soft_iface,
487 bat_priv->soft_iface->dev_addr, vid,
488 BATADV_NULL_IFINDEX);
489
490 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
491 hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
492 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
493
494 return 0;
495}
496
497/**
498 * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
499 * @bat_priv: the bat priv with all the soft interface information
500 * @vlan: the object to remove
501 */
502static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
503 struct batadv_softif_vlan *vlan)
504{
505 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
506 hlist_del_rcu(&vlan->list);
507 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
508
509 batadv_sysfs_del_vlan(bat_priv, vlan);
510
511 /* explicitly remove the associated TT local entry because it is marked
512 * with the NOPURGE flag
513 */
514 batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
515 vlan->vid, "vlan interface destroyed", false);
516
517 batadv_softif_vlan_free_ref(vlan);
518}
519
520/**
521 * batadv_interface_add_vid - ndo_add_vid API implementation
522 * @dev: the netdev of the mesh interface
523 * @vid: identifier of the new vlan
524 *
525 * Set up all the internal structures for handling the new vlan on top of the
526 * mesh interface
527 *
528 * Returns 0 on success or a negative error code in case of failure.
529 */
530static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
531 unsigned short vid)
532{
533 struct batadv_priv *bat_priv = netdev_priv(dev);
534
535 /* only 802.1Q vlans are supported.
536 * batman-adv does not know how to handle other types
537 */
538 if (proto != htons(ETH_P_8021Q))
539 return -EINVAL;
540
541 vid |= BATADV_VLAN_HAS_TAG;
542
543 return batadv_softif_create_vlan(bat_priv, vid);
544}
545
546/**
547 * batadv_interface_kill_vid - ndo_kill_vid API implementation
548 * @dev: the netdev of the mesh interface
549 * @vid: identifier of the deleted vlan
550 *
551 * Destroy all the internal structures used to handle the vlan identified by vid
552 * on top of the mesh interface
553 *
554 * Returns 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
555 * or -ENOENT if the specified vlan id wasn't registered.
556 */
557static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
558 unsigned short vid)
559{
560 struct batadv_priv *bat_priv = netdev_priv(dev);
561 struct batadv_softif_vlan *vlan;
562
563 /* only 802.1Q vlans are supported. batman-adv does not know how to
564 * handle other types
565 */
566 if (proto != htons(ETH_P_8021Q))
567 return -EINVAL;
568
569 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
570 if (!vlan)
571 return -ENOENT;
572
573 batadv_softif_destroy_vlan(bat_priv, vlan);
574
575 /* finally free the vlan object */
576 batadv_softif_vlan_free_ref(vlan);
577
578 return 0;
579}
580
385/* batman-adv network devices have devices nesting below it and are a special 581/* batman-adv network devices have devices nesting below it and are a special
386 * "super class" of normal network devices; split their locks off into a 582 * "super class" of normal network devices; split their locks off into a
387 * separate class since they always nest. 583 * separate class since they always nest.
@@ -421,6 +617,7 @@ static void batadv_set_lockdep_class(struct net_device *dev)
421 */ 617 */
422static void batadv_softif_destroy_finish(struct work_struct *work) 618static void batadv_softif_destroy_finish(struct work_struct *work)
423{ 619{
620 struct batadv_softif_vlan *vlan;
424 struct batadv_priv *bat_priv; 621 struct batadv_priv *bat_priv;
425 struct net_device *soft_iface; 622 struct net_device *soft_iface;
426 623
@@ -428,6 +625,13 @@ static void batadv_softif_destroy_finish(struct work_struct *work)
428 cleanup_work); 625 cleanup_work);
429 soft_iface = bat_priv->soft_iface; 626 soft_iface = bat_priv->soft_iface;
430 627
628 /* destroy the "untagged" VLAN */
629 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
630 if (vlan) {
631 batadv_softif_destroy_vlan(bat_priv, vlan);
632 batadv_softif_vlan_free_ref(vlan);
633 }
634
431 batadv_sysfs_del_meshif(soft_iface); 635 batadv_sysfs_del_meshif(soft_iface);
432 636
433 rtnl_lock(); 637 rtnl_lock();
@@ -444,6 +648,7 @@ static void batadv_softif_destroy_finish(struct work_struct *work)
444static int batadv_softif_init_late(struct net_device *dev) 648static int batadv_softif_init_late(struct net_device *dev)
445{ 649{
446 struct batadv_priv *bat_priv; 650 struct batadv_priv *bat_priv;
651 uint32_t random_seqno;
447 int ret; 652 int ret;
448 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM; 653 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
449 654
@@ -468,17 +673,17 @@ static int batadv_softif_init_late(struct net_device *dev)
468#ifdef CONFIG_BATMAN_ADV_DAT 673#ifdef CONFIG_BATMAN_ADV_DAT
469 atomic_set(&bat_priv->distributed_arp_table, 1); 674 atomic_set(&bat_priv->distributed_arp_table, 1);
470#endif 675#endif
471 atomic_set(&bat_priv->ap_isolation, 0);
472 atomic_set(&bat_priv->vis_mode, BATADV_VIS_TYPE_CLIENT_UPDATE);
473 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF); 676 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
474 atomic_set(&bat_priv->gw_sel_class, 20); 677 atomic_set(&bat_priv->gw_sel_class, 20);
475 atomic_set(&bat_priv->gw_bandwidth, 41); 678 atomic_set(&bat_priv->gw.bandwidth_down, 100);
679 atomic_set(&bat_priv->gw.bandwidth_up, 20);
476 atomic_set(&bat_priv->orig_interval, 1000); 680 atomic_set(&bat_priv->orig_interval, 1000);
477 atomic_set(&bat_priv->hop_penalty, 30); 681 atomic_set(&bat_priv->hop_penalty, 30);
478#ifdef CONFIG_BATMAN_ADV_DEBUG 682#ifdef CONFIG_BATMAN_ADV_DEBUG
479 atomic_set(&bat_priv->log_level, 0); 683 atomic_set(&bat_priv->log_level, 0);
480#endif 684#endif
481 atomic_set(&bat_priv->fragmentation, 1); 685 atomic_set(&bat_priv->fragmentation, 1);
686 atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
482 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN); 687 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
483 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN); 688 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
484 689
@@ -493,6 +698,10 @@ static int batadv_softif_init_late(struct net_device *dev)
493 bat_priv->tt.last_changeset = NULL; 698 bat_priv->tt.last_changeset = NULL;
494 bat_priv->tt.last_changeset_len = 0; 699 bat_priv->tt.last_changeset_len = 0;
495 700
701 /* randomize initial seqno to avoid collision */
702 get_random_bytes(&random_seqno, sizeof(random_seqno));
703 atomic_set(&bat_priv->frag_seqno, random_seqno);
704
496 bat_priv->primary_if = NULL; 705 bat_priv->primary_if = NULL;
497 bat_priv->num_ifaces = 0; 706 bat_priv->num_ifaces = 0;
498 707
@@ -578,8 +787,11 @@ static const struct net_device_ops batadv_netdev_ops = {
578 .ndo_open = batadv_interface_open, 787 .ndo_open = batadv_interface_open,
579 .ndo_stop = batadv_interface_release, 788 .ndo_stop = batadv_interface_release,
580 .ndo_get_stats = batadv_interface_stats, 789 .ndo_get_stats = batadv_interface_stats,
790 .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
791 .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
581 .ndo_set_mac_address = batadv_interface_set_mac_addr, 792 .ndo_set_mac_address = batadv_interface_set_mac_addr,
582 .ndo_change_mtu = batadv_interface_change_mtu, 793 .ndo_change_mtu = batadv_interface_change_mtu,
794 .ndo_set_rx_mode = batadv_interface_set_rx_mode,
583 .ndo_start_xmit = batadv_interface_tx, 795 .ndo_start_xmit = batadv_interface_tx,
584 .ndo_validate_addr = eth_validate_addr, 796 .ndo_validate_addr = eth_validate_addr,
585 .ndo_add_slave = batadv_softif_slave_add, 797 .ndo_add_slave = batadv_softif_slave_add,
@@ -616,6 +828,7 @@ static void batadv_softif_init_early(struct net_device *dev)
616 828
617 dev->netdev_ops = &batadv_netdev_ops; 829 dev->netdev_ops = &batadv_netdev_ops;
618 dev->destructor = batadv_softif_free; 830 dev->destructor = batadv_softif_free;
831 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
619 dev->tx_queue_len = 0; 832 dev->tx_queue_len = 0;
620 833
621 /* can't call min_mtu, because the needed variables 834 /* can't call min_mtu, because the needed variables
@@ -623,7 +836,7 @@ static void batadv_softif_init_early(struct net_device *dev)
623 */ 836 */
624 dev->mtu = ETH_DATA_LEN; 837 dev->mtu = ETH_DATA_LEN;
625 /* reserve more space in the skbuff for our header */ 838 /* reserve more space in the skbuff for our header */
626 dev->hard_header_len = BATADV_HEADER_LEN; 839 dev->hard_header_len = batadv_max_header_len();
627 840
628 /* generate random address */ 841 /* generate random address */
629 eth_hw_addr_random(dev); 842 eth_hw_addr_random(dev);
@@ -760,6 +973,12 @@ static const struct {
760 { "mgmt_tx_bytes" }, 973 { "mgmt_tx_bytes" },
761 { "mgmt_rx" }, 974 { "mgmt_rx" },
762 { "mgmt_rx_bytes" }, 975 { "mgmt_rx_bytes" },
976 { "frag_tx" },
977 { "frag_tx_bytes" },
978 { "frag_rx" },
979 { "frag_rx_bytes" },
980 { "frag_fwd" },
981 { "frag_fwd_bytes" },
763 { "tt_request_tx" }, 982 { "tt_request_tx" },
764 { "tt_request_rx" }, 983 { "tt_request_rx" },
765 { "tt_response_tx" }, 984 { "tt_response_tx" },
diff --git a/net/batman-adv/soft-interface.h b/net/batman-adv/soft-interface.h
index 2f2472c2ea0d..06fc91ff5a02 100644
--- a/net/batman-adv/soft-interface.h
+++ b/net/batman-adv/soft-interface.h
@@ -28,5 +28,9 @@ struct net_device *batadv_softif_create(const char *name);
28void batadv_softif_destroy_sysfs(struct net_device *soft_iface); 28void batadv_softif_destroy_sysfs(struct net_device *soft_iface);
29int batadv_softif_is_valid(const struct net_device *net_dev); 29int batadv_softif_is_valid(const struct net_device *net_dev);
30extern struct rtnl_link_ops batadv_link_ops; 30extern struct rtnl_link_ops batadv_link_ops;
31int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid);
32void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *softif_vlan);
33struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
34 unsigned short vid);
31 35
32#endif /* _NET_BATMAN_ADV_SOFT_INTERFACE_H_ */ 36#endif /* _NET_BATMAN_ADV_SOFT_INTERFACE_H_ */
diff --git a/net/batman-adv/sysfs.c b/net/batman-adv/sysfs.c
index 4114b961bc2c..6335433310af 100644
--- a/net/batman-adv/sysfs.c
+++ b/net/batman-adv/sysfs.c
@@ -21,11 +21,12 @@
21#include "sysfs.h" 21#include "sysfs.h"
22#include "translation-table.h" 22#include "translation-table.h"
23#include "distributed-arp-table.h" 23#include "distributed-arp-table.h"
24#include "network-coding.h"
24#include "originator.h" 25#include "originator.h"
25#include "hard-interface.h" 26#include "hard-interface.h"
27#include "soft-interface.h"
26#include "gateway_common.h" 28#include "gateway_common.h"
27#include "gateway_client.h" 29#include "gateway_client.h"
28#include "vis.h"
29 30
30static struct net_device *batadv_kobj_to_netdev(struct kobject *obj) 31static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
31{ 32{
@@ -39,6 +40,53 @@ static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
39 return netdev_priv(net_dev); 40 return netdev_priv(net_dev);
40} 41}
41 42
43/**
44 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
45 * @obj: kobject to covert
46 *
47 * Returns the associated batadv_priv struct.
48 */
49static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
50{
51 /* VLAN specific attributes are located in the root sysfs folder if they
52 * refer to the untagged VLAN..
53 */
54 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
55 return batadv_kobj_to_batpriv(obj);
56
57 /* ..while the attributes for the tagged vlans are located in
58 * the in the corresponding "vlan%VID" subfolder
59 */
60 return batadv_kobj_to_batpriv(obj->parent);
61}
62
63/**
64 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
65 * @obj: kobject to covert
66 *
67 * Returns the associated softif_vlan struct if found, NULL otherwise.
68 */
69static struct batadv_softif_vlan *
70batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
71{
72 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
73
74 rcu_read_lock();
75 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
76 if (vlan_tmp->kobj != obj)
77 continue;
78
79 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
80 continue;
81
82 vlan = vlan_tmp;
83 break;
84 }
85 rcu_read_unlock();
86
87 return vlan;
88}
89
42#define BATADV_UEV_TYPE_VAR "BATTYPE=" 90#define BATADV_UEV_TYPE_VAR "BATTYPE="
43#define BATADV_UEV_ACTION_VAR "BATACTION=" 91#define BATADV_UEV_ACTION_VAR "BATACTION="
44#define BATADV_UEV_DATA_VAR "BATDATA=" 92#define BATADV_UEV_DATA_VAR "BATDATA="
@@ -53,6 +101,15 @@ static char *batadv_uev_type_str[] = {
53 "gw" 101 "gw"
54}; 102};
55 103
104/* Use this, if you have customized show and store functions for vlan attrs */
105#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
106struct batadv_attribute batadv_attr_vlan_##_name = { \
107 .attr = {.name = __stringify(_name), \
108 .mode = _mode }, \
109 .show = _show, \
110 .store = _store, \
111};
112
56/* Use this, if you have customized show and store functions */ 113/* Use this, if you have customized show and store functions */
57#define BATADV_ATTR(_name, _mode, _show, _store) \ 114#define BATADV_ATTR(_name, _mode, _show, _store) \
58struct batadv_attribute batadv_attr_##_name = { \ 115struct batadv_attribute batadv_attr_##_name = { \
@@ -122,6 +179,41 @@ ssize_t batadv_show_##_name(struct kobject *kobj, \
122 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \ 179 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
123 batadv_store_##_name) 180 batadv_store_##_name)
124 181
182#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
183ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
184 struct attribute *attr, char *buff, \
185 size_t count) \
186{ \
187 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
188 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
189 kobj); \
190 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
191 attr, &vlan->_name, \
192 bat_priv->soft_iface); \
193 batadv_softif_vlan_free_ref(vlan); \
194 return res; \
195}
196
197#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
198ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
199 struct attribute *attr, char *buff) \
200{ \
201 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
202 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
203 kobj); \
204 size_t res = sprintf(buff, "%s\n", \
205 atomic_read(&vlan->_name) == 0 ? \
206 "disabled" : "enabled"); \
207 batadv_softif_vlan_free_ref(vlan); \
208 return res; \
209}
210
211/* Use this, if you are going to turn a [name] in the vlan struct on or off */
212#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
213 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
214 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
215 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
216 batadv_store_vlan_##_name)
125 217
126static int batadv_store_bool_attr(char *buff, size_t count, 218static int batadv_store_bool_attr(char *buff, size_t count,
127 struct net_device *net_dev, 219 struct net_device *net_dev,
@@ -230,74 +322,6 @@ __batadv_store_uint_attr(const char *buff, size_t count,
230 return ret; 322 return ret;
231} 323}
232 324
233static ssize_t batadv_show_vis_mode(struct kobject *kobj,
234 struct attribute *attr, char *buff)
235{
236 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
237 int vis_mode = atomic_read(&bat_priv->vis_mode);
238 const char *mode;
239
240 if (vis_mode == BATADV_VIS_TYPE_CLIENT_UPDATE)
241 mode = "client";
242 else
243 mode = "server";
244
245 return sprintf(buff, "%s\n", mode);
246}
247
248static ssize_t batadv_store_vis_mode(struct kobject *kobj,
249 struct attribute *attr, char *buff,
250 size_t count)
251{
252 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
253 struct batadv_priv *bat_priv = netdev_priv(net_dev);
254 unsigned long val;
255 int ret, vis_mode_tmp = -1;
256 const char *old_mode, *new_mode;
257
258 ret = kstrtoul(buff, 10, &val);
259
260 if (((count == 2) && (!ret) &&
261 (val == BATADV_VIS_TYPE_CLIENT_UPDATE)) ||
262 (strncmp(buff, "client", 6) == 0) ||
263 (strncmp(buff, "off", 3) == 0))
264 vis_mode_tmp = BATADV_VIS_TYPE_CLIENT_UPDATE;
265
266 if (((count == 2) && (!ret) &&
267 (val == BATADV_VIS_TYPE_SERVER_SYNC)) ||
268 (strncmp(buff, "server", 6) == 0))
269 vis_mode_tmp = BATADV_VIS_TYPE_SERVER_SYNC;
270
271 if (vis_mode_tmp < 0) {
272 if (buff[count - 1] == '\n')
273 buff[count - 1] = '\0';
274
275 batadv_info(net_dev,
276 "Invalid parameter for 'vis mode' setting received: %s\n",
277 buff);
278 return -EINVAL;
279 }
280
281 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
282 return count;
283
284 if (atomic_read(&bat_priv->vis_mode) == BATADV_VIS_TYPE_CLIENT_UPDATE)
285 old_mode = "client";
286 else
287 old_mode = "server";
288
289 if (vis_mode_tmp == BATADV_VIS_TYPE_CLIENT_UPDATE)
290 new_mode = "client";
291 else
292 new_mode = "server";
293
294 batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
295 new_mode);
296
297 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
298 return count;
299}
300
301static ssize_t batadv_show_bat_algo(struct kobject *kobj, 325static ssize_t batadv_show_bat_algo(struct kobject *kobj,
302 struct attribute *attr, char *buff) 326 struct attribute *attr, char *buff)
303{ 327{
@@ -390,6 +414,7 @@ static ssize_t batadv_store_gw_mode(struct kobject *kobj,
390 */ 414 */
391 batadv_gw_check_client_stop(bat_priv); 415 batadv_gw_check_client_stop(bat_priv);
392 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp); 416 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
417 batadv_gw_tvlv_container_update(bat_priv);
393 return count; 418 return count;
394} 419}
395 420
@@ -397,15 +422,13 @@ static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
397 struct attribute *attr, char *buff) 422 struct attribute *attr, char *buff)
398{ 423{
399 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); 424 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
400 int down, up; 425 uint32_t down, up;
401 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth); 426
402 427 down = atomic_read(&bat_priv->gw.bandwidth_down);
403 batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up); 428 up = atomic_read(&bat_priv->gw.bandwidth_up);
404 return sprintf(buff, "%i%s/%i%s\n", 429
405 (down > 2048 ? down / 1024 : down), 430 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
406 (down > 2048 ? "MBit" : "KBit"), 431 down % 10, up / 10, up % 10);
407 (up > 2048 ? up / 1024 : up),
408 (up > 2048 ? "MBit" : "KBit"));
409} 432}
410 433
411static ssize_t batadv_store_gw_bwidth(struct kobject *kobj, 434static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
@@ -426,12 +449,10 @@ BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
426BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL); 449BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
427#endif 450#endif
428#ifdef CONFIG_BATMAN_ADV_DAT 451#ifdef CONFIG_BATMAN_ADV_DAT
429BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR, NULL); 452BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
453 batadv_dat_status_update);
430#endif 454#endif
431BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu); 455BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
432BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
433static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
434 batadv_store_vis_mode);
435static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL); 456static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
436static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode, 457static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
437 batadv_store_gw_mode); 458 batadv_store_gw_mode);
@@ -447,7 +468,8 @@ static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
447BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL); 468BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
448#endif 469#endif
449#ifdef CONFIG_BATMAN_ADV_NC 470#ifdef CONFIG_BATMAN_ADV_NC
450BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR, NULL); 471BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
472 batadv_nc_status_update);
451#endif 473#endif
452 474
453static struct batadv_attribute *batadv_mesh_attrs[] = { 475static struct batadv_attribute *batadv_mesh_attrs[] = {
@@ -460,8 +482,6 @@ static struct batadv_attribute *batadv_mesh_attrs[] = {
460 &batadv_attr_distributed_arp_table, 482 &batadv_attr_distributed_arp_table,
461#endif 483#endif
462 &batadv_attr_fragmentation, 484 &batadv_attr_fragmentation,
463 &batadv_attr_ap_isolation,
464 &batadv_attr_vis_mode,
465 &batadv_attr_routing_algo, 485 &batadv_attr_routing_algo,
466 &batadv_attr_gw_mode, 486 &batadv_attr_gw_mode,
467 &batadv_attr_orig_interval, 487 &batadv_attr_orig_interval,
@@ -477,6 +497,16 @@ static struct batadv_attribute *batadv_mesh_attrs[] = {
477 NULL, 497 NULL,
478}; 498};
479 499
500BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
501
502/**
503 * batadv_vlan_attrs - array of vlan specific sysfs attributes
504 */
505static struct batadv_attribute *batadv_vlan_attrs[] = {
506 &batadv_attr_vlan_ap_isolation,
507 NULL,
508};
509
480int batadv_sysfs_add_meshif(struct net_device *dev) 510int batadv_sysfs_add_meshif(struct net_device *dev)
481{ 511{
482 struct kobject *batif_kobject = &dev->dev.kobj; 512 struct kobject *batif_kobject = &dev->dev.kobj;
@@ -527,6 +557,80 @@ void batadv_sysfs_del_meshif(struct net_device *dev)
527 bat_priv->mesh_obj = NULL; 557 bat_priv->mesh_obj = NULL;
528} 558}
529 559
560/**
561 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
562 * @dev: netdev of the mesh interface
563 * @vlan: private data of the newly added VLAN interface
564 *
565 * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
566 */
567int batadv_sysfs_add_vlan(struct net_device *dev,
568 struct batadv_softif_vlan *vlan)
569{
570 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
571 struct batadv_priv *bat_priv = netdev_priv(dev);
572 struct batadv_attribute **bat_attr;
573 int err;
574
575 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
576 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
577 vlan->vid & VLAN_VID_MASK);
578
579 vlan->kobj = kobject_create_and_add(vlan_subdir,
580 bat_priv->mesh_obj);
581 if (!vlan->kobj) {
582 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
583 dev->name, vlan_subdir);
584 goto out;
585 }
586 } else {
587 /* the untagged LAN uses the root folder to store its "VLAN
588 * specific attributes"
589 */
590 vlan->kobj = bat_priv->mesh_obj;
591 kobject_get(bat_priv->mesh_obj);
592 }
593
594 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
595 err = sysfs_create_file(vlan->kobj,
596 &((*bat_attr)->attr));
597 if (err) {
598 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
599 dev->name, vlan_subdir,
600 ((*bat_attr)->attr).name);
601 goto rem_attr;
602 }
603 }
604
605 return 0;
606
607rem_attr:
608 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
609 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
610
611 kobject_put(vlan->kobj);
612 vlan->kobj = NULL;
613out:
614 return -ENOMEM;
615}
616
617/**
618 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
619 * @bat_priv: the bat priv with all the soft interface information
620 * @vlan: the private data of the VLAN to destroy
621 */
622void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
623 struct batadv_softif_vlan *vlan)
624{
625 struct batadv_attribute **bat_attr;
626
627 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
628 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
629
630 kobject_put(vlan->kobj);
631 vlan->kobj = NULL;
632}
633
530static ssize_t batadv_show_mesh_iface(struct kobject *kobj, 634static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
531 struct attribute *attr, char *buff) 635 struct attribute *attr, char *buff)
532{ 636{
diff --git a/net/batman-adv/sysfs.h b/net/batman-adv/sysfs.h
index 479acf4c16f4..c7d725de50ad 100644
--- a/net/batman-adv/sysfs.h
+++ b/net/batman-adv/sysfs.h
@@ -22,6 +22,12 @@
22 22
23#define BATADV_SYSFS_IF_MESH_SUBDIR "mesh" 23#define BATADV_SYSFS_IF_MESH_SUBDIR "mesh"
24#define BATADV_SYSFS_IF_BAT_SUBDIR "batman_adv" 24#define BATADV_SYSFS_IF_BAT_SUBDIR "batman_adv"
25/**
26 * BATADV_SYSFS_VLAN_SUBDIR_PREFIX - prefix of the subfolder that will be
27 * created in the sysfs hierarchy for each VLAN interface. The subfolder will
28 * be named "BATADV_SYSFS_VLAN_SUBDIR_PREFIX%vid".
29 */
30#define BATADV_SYSFS_VLAN_SUBDIR_PREFIX "vlan"
25 31
26struct batadv_attribute { 32struct batadv_attribute {
27 struct attribute attr; 33 struct attribute attr;
@@ -36,6 +42,10 @@ void batadv_sysfs_del_meshif(struct net_device *dev);
36int batadv_sysfs_add_hardif(struct kobject **hardif_obj, 42int batadv_sysfs_add_hardif(struct kobject **hardif_obj,
37 struct net_device *dev); 43 struct net_device *dev);
38void batadv_sysfs_del_hardif(struct kobject **hardif_obj); 44void batadv_sysfs_del_hardif(struct kobject **hardif_obj);
45int batadv_sysfs_add_vlan(struct net_device *dev,
46 struct batadv_softif_vlan *vlan);
47void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
48 struct batadv_softif_vlan *vlan);
39int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type, 49int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
40 enum batadv_uev_action action, const char *data); 50 enum batadv_uev_action action, const char *data);
41 51
diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index 34510f38708f..ff625fedbc5e 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -27,13 +27,14 @@
27#include "routing.h" 27#include "routing.h"
28#include "bridge_loop_avoidance.h" 28#include "bridge_loop_avoidance.h"
29 29
30#include <linux/crc16.h> 30#include <linux/crc32c.h>
31 31
32/* hash class keys */ 32/* hash class keys */
33static struct lock_class_key batadv_tt_local_hash_lock_class_key; 33static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34static struct lock_class_key batadv_tt_global_hash_lock_class_key; 34static struct lock_class_key batadv_tt_global_hash_lock_class_key;
35 35
36static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client, 36static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
37 unsigned short vid,
37 struct batadv_orig_node *orig_node); 38 struct batadv_orig_node *orig_node);
38static void batadv_tt_purge(struct work_struct *work); 39static void batadv_tt_purge(struct work_struct *work);
39static void 40static void
@@ -41,7 +42,8 @@ batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
41static void batadv_tt_global_del(struct batadv_priv *bat_priv, 42static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42 struct batadv_orig_node *orig_node, 43 struct batadv_orig_node *orig_node,
43 const unsigned char *addr, 44 const unsigned char *addr,
44 const char *message, bool roaming); 45 unsigned short vid, const char *message,
46 bool roaming);
45 47
46/* returns 1 if they are the same mac addr */ 48/* returns 1 if they are the same mac addr */
47static int batadv_compare_tt(const struct hlist_node *node, const void *data2) 49static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
@@ -52,43 +54,93 @@ static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
52 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0); 54 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
53} 55}
54 56
57/**
58 * batadv_choose_tt - return the index of the tt entry in the hash table
59 * @data: pointer to the tt_common_entry object to map
60 * @size: the size of the hash table
61 *
62 * Returns the hash index where the object represented by 'data' should be
63 * stored at.
64 */
65static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
66{
67 struct batadv_tt_common_entry *tt;
68 uint32_t hash = 0;
69
70 tt = (struct batadv_tt_common_entry *)data;
71 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
72 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
73
74 hash += (hash << 3);
75 hash ^= (hash >> 11);
76 hash += (hash << 15);
77
78 return hash % size;
79}
80
81/**
82 * batadv_tt_hash_find - look for a client in the given hash table
83 * @hash: the hash table to search
84 * @addr: the mac address of the client to look for
85 * @vid: VLAN identifier
86 *
87 * Returns a pointer to the tt_common struct belonging to the searched client if
88 * found, NULL otherwise.
89 */
55static struct batadv_tt_common_entry * 90static struct batadv_tt_common_entry *
56batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data) 91batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
92 unsigned short vid)
57{ 93{
58 struct hlist_head *head; 94 struct hlist_head *head;
59 struct batadv_tt_common_entry *tt_common_entry; 95 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
60 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
61 uint32_t index; 96 uint32_t index;
62 97
63 if (!hash) 98 if (!hash)
64 return NULL; 99 return NULL;
65 100
66 index = batadv_choose_orig(data, hash->size); 101 memcpy(to_search.addr, addr, ETH_ALEN);
102 to_search.vid = vid;
103
104 index = batadv_choose_tt(&to_search, hash->size);
67 head = &hash->table[index]; 105 head = &hash->table[index];
68 106
69 rcu_read_lock(); 107 rcu_read_lock();
70 hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) { 108 hlist_for_each_entry_rcu(tt, head, hash_entry) {
71 if (!batadv_compare_eth(tt_common_entry, data)) 109 if (!batadv_compare_eth(tt, addr))
72 continue; 110 continue;
73 111
74 if (!atomic_inc_not_zero(&tt_common_entry->refcount)) 112 if (tt->vid != vid)
75 continue; 113 continue;
76 114
77 tt_common_entry_tmp = tt_common_entry; 115 if (!atomic_inc_not_zero(&tt->refcount))
116 continue;
117
118 tt_tmp = tt;
78 break; 119 break;
79 } 120 }
80 rcu_read_unlock(); 121 rcu_read_unlock();
81 122
82 return tt_common_entry_tmp; 123 return tt_tmp;
83} 124}
84 125
126/**
127 * batadv_tt_local_hash_find - search the local table for a given client
128 * @bat_priv: the bat priv with all the soft interface information
129 * @addr: the mac address of the client to look for
130 * @vid: VLAN identifier
131 *
132 * Returns a pointer to the corresponding tt_local_entry struct if the client is
133 * found, NULL otherwise.
134 */
85static struct batadv_tt_local_entry * 135static struct batadv_tt_local_entry *
86batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data) 136batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
137 unsigned short vid)
87{ 138{
88 struct batadv_tt_common_entry *tt_common_entry; 139 struct batadv_tt_common_entry *tt_common_entry;
89 struct batadv_tt_local_entry *tt_local_entry = NULL; 140 struct batadv_tt_local_entry *tt_local_entry = NULL;
90 141
91 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data); 142 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
143 vid);
92 if (tt_common_entry) 144 if (tt_common_entry)
93 tt_local_entry = container_of(tt_common_entry, 145 tt_local_entry = container_of(tt_common_entry,
94 struct batadv_tt_local_entry, 146 struct batadv_tt_local_entry,
@@ -96,13 +148,24 @@ batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
96 return tt_local_entry; 148 return tt_local_entry;
97} 149}
98 150
151/**
152 * batadv_tt_global_hash_find - search the global table for a given client
153 * @bat_priv: the bat priv with all the soft interface information
154 * @addr: the mac address of the client to look for
155 * @vid: VLAN identifier
156 *
157 * Returns a pointer to the corresponding tt_global_entry struct if the client
158 * is found, NULL otherwise.
159 */
99static struct batadv_tt_global_entry * 160static struct batadv_tt_global_entry *
100batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data) 161batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
162 unsigned short vid)
101{ 163{
102 struct batadv_tt_common_entry *tt_common_entry; 164 struct batadv_tt_common_entry *tt_common_entry;
103 struct batadv_tt_global_entry *tt_global_entry = NULL; 165 struct batadv_tt_global_entry *tt_global_entry = NULL;
104 166
105 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data); 167 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
168 vid);
106 if (tt_common_entry) 169 if (tt_common_entry)
107 tt_global_entry = container_of(tt_common_entry, 170 tt_global_entry = container_of(tt_common_entry,
108 struct batadv_tt_global_entry, 171 struct batadv_tt_global_entry,
@@ -117,25 +180,17 @@ batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
117 kfree_rcu(tt_local_entry, common.rcu); 180 kfree_rcu(tt_local_entry, common.rcu);
118} 181}
119 182
120static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu) 183/**
121{ 184 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
122 struct batadv_tt_common_entry *tt_common_entry; 185 * tt_global_entry and possibly free it
123 struct batadv_tt_global_entry *tt_global_entry; 186 * @tt_global_entry: the object to free
124 187 */
125 tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
126 tt_global_entry = container_of(tt_common_entry,
127 struct batadv_tt_global_entry, common);
128
129 kfree(tt_global_entry);
130}
131
132static void 188static void
133batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry) 189batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
134{ 190{
135 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) { 191 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
136 batadv_tt_global_del_orig_list(tt_global_entry); 192 batadv_tt_global_del_orig_list(tt_global_entry);
137 call_rcu(&tt_global_entry->common.rcu, 193 kfree_rcu(tt_global_entry, common.rcu);
138 batadv_tt_global_entry_free_rcu);
139 } 194 }
140} 195}
141 196
@@ -153,13 +208,107 @@ static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
153 kfree(orig_entry); 208 kfree(orig_entry);
154} 209}
155 210
211/**
212 * batadv_tt_local_size_mod - change the size by v of the local table identified
213 * by vid
214 * @bat_priv: the bat priv with all the soft interface information
215 * @vid: the VLAN identifier of the sub-table to change
216 * @v: the amount to sum to the local table size
217 */
218static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
219 unsigned short vid, int v)
220{
221 struct batadv_softif_vlan *vlan;
222
223 vlan = batadv_softif_vlan_get(bat_priv, vid);
224 if (!vlan)
225 return;
226
227 atomic_add(v, &vlan->tt.num_entries);
228
229 batadv_softif_vlan_free_ref(vlan);
230}
231
232/**
233 * batadv_tt_local_size_inc - increase by one the local table size for the given
234 * vid
235 * @bat_priv: the bat priv with all the soft interface information
236 * @vid: the VLAN identifier
237 */
238static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
239 unsigned short vid)
240{
241 batadv_tt_local_size_mod(bat_priv, vid, 1);
242}
243
244/**
245 * batadv_tt_local_size_dec - decrease by one the local table size for the given
246 * vid
247 * @bat_priv: the bat priv with all the soft interface information
248 * @vid: the VLAN identifier
249 */
250static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
251 unsigned short vid)
252{
253 batadv_tt_local_size_mod(bat_priv, vid, -1);
254}
255
256/**
257 * batadv_tt_global_size_mod - change the size by v of the local table
258 * identified by vid
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 * @v: the amount to sum to the global table size
262 */
263static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
264 unsigned short vid, int v)
265{
266 struct batadv_orig_node_vlan *vlan;
267
268 vlan = batadv_orig_node_vlan_new(orig_node, vid);
269 if (!vlan)
270 return;
271
272 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
273 spin_lock_bh(&orig_node->vlan_list_lock);
274 list_del_rcu(&vlan->list);
275 spin_unlock_bh(&orig_node->vlan_list_lock);
276 batadv_orig_node_vlan_free_ref(vlan);
277 }
278
279 batadv_orig_node_vlan_free_ref(vlan);
280}
281
282/**
283 * batadv_tt_global_size_inc - increase by one the global table size for the
284 * given vid
285 * @orig_node: the originator which global table size has to be decreased
286 * @vid: the vlan identifier
287 */
288static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
289 unsigned short vid)
290{
291 batadv_tt_global_size_mod(orig_node, vid, 1);
292}
293
294/**
295 * batadv_tt_global_size_dec - decrease by one the global table size for the
296 * given vid
297 * @orig_node: the originator which global table size has to be decreased
298 * @vid: the vlan identifier
299 */
300static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
301 unsigned short vid)
302{
303 batadv_tt_global_size_mod(orig_node, vid, -1);
304}
305
156static void 306static void
157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry) 307batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
158{ 308{
159 if (!atomic_dec_and_test(&orig_entry->refcount)) 309 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return; 310 return;
161 /* to avoid race conditions, immediately decrease the tt counter */ 311
162 atomic_dec(&orig_entry->orig_node->tt_size);
163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu); 312 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
164} 313}
165 314
@@ -180,12 +329,14 @@ static void batadv_tt_local_event(struct batadv_priv *bat_priv,
180 bool del_op_requested, del_op_entry; 329 bool del_op_requested, del_op_entry;
181 330
182 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC); 331 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
183
184 if (!tt_change_node) 332 if (!tt_change_node)
185 return; 333 return;
186 334
187 tt_change_node->change.flags = flags; 335 tt_change_node->change.flags = flags;
336 memset(tt_change_node->change.reserved, 0,
337 sizeof(tt_change_node->change.reserved));
188 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN); 338 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
339 tt_change_node->change.vid = htons(common->vid);
189 340
190 del_op_requested = flags & BATADV_TT_CLIENT_DEL; 341 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
191 342
@@ -208,6 +359,13 @@ static void batadv_tt_local_event(struct batadv_priv *bat_priv,
208 goto del; 359 goto del;
209 if (del_op_requested && !del_op_entry) 360 if (del_op_requested && !del_op_entry)
210 goto del; 361 goto del;
362
363 /* this is a second add in the same originator interval. It
364 * means that flags have been changed: update them!
365 */
366 if (!del_op_requested && !del_op_entry)
367 entry->change.flags = flags;
368
211 continue; 369 continue;
212del: 370del:
213 list_del(&entry->list); 371 list_del(&entry->list);
@@ -229,9 +387,55 @@ unlock:
229 atomic_inc(&bat_priv->tt.local_changes); 387 atomic_inc(&bat_priv->tt.local_changes);
230} 388}
231 389
232int batadv_tt_len(int changes_num) 390/**
391 * batadv_tt_len - compute length in bytes of given number of tt changes
392 * @changes_num: number of tt changes
393 *
394 * Returns computed length in bytes.
395 */
396static int batadv_tt_len(int changes_num)
233{ 397{
234 return changes_num * sizeof(struct batadv_tt_change); 398 return changes_num * sizeof(struct batadv_tvlv_tt_change);
399}
400
401/**
402 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
403 * @tt_len: available space
404 *
405 * Returns the number of entries.
406 */
407static uint16_t batadv_tt_entries(uint16_t tt_len)
408{
409 return tt_len / batadv_tt_len(1);
410}
411
412/**
413 * batadv_tt_local_table_transmit_size - calculates the local translation table
414 * size when transmitted over the air
415 * @bat_priv: the bat priv with all the soft interface information
416 *
417 * Returns local translation table size in bytes.
418 */
419static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
420{
421 uint16_t num_vlan = 0, tt_local_entries = 0;
422 struct batadv_softif_vlan *vlan;
423 int hdr_size;
424
425 rcu_read_lock();
426 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
427 num_vlan++;
428 tt_local_entries += atomic_read(&vlan->tt.num_entries);
429 }
430 rcu_read_unlock();
431
432 /* header size of tvlv encapsulated tt response payload */
433 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
434 hdr_size += sizeof(struct batadv_tvlv_hdr);
435 hdr_size += sizeof(struct batadv_tvlv_tt_data);
436 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
437
438 return hdr_size + batadv_tt_len(tt_local_entries);
235} 439}
236 440
237static int batadv_tt_local_init(struct batadv_priv *bat_priv) 441static int batadv_tt_local_init(struct batadv_priv *bat_priv)
@@ -255,33 +459,51 @@ static void batadv_tt_global_free(struct batadv_priv *bat_priv,
255 const char *message) 459 const char *message)
256{ 460{
257 batadv_dbg(BATADV_DBG_TT, bat_priv, 461 batadv_dbg(BATADV_DBG_TT, bat_priv,
258 "Deleting global tt entry %pM: %s\n", 462 "Deleting global tt entry %pM (vid: %d): %s\n",
259 tt_global->common.addr, message); 463 tt_global->common.addr,
464 BATADV_PRINT_VID(tt_global->common.vid), message);
260 465
261 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt, 466 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
262 batadv_choose_orig, tt_global->common.addr); 467 batadv_choose_tt, &tt_global->common);
263 batadv_tt_global_entry_free_ref(tt_global); 468 batadv_tt_global_entry_free_ref(tt_global);
264} 469}
265 470
266void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr, 471/**
267 int ifindex) 472 * batadv_tt_local_add - add a new client to the local table or update an
473 * existing client
474 * @soft_iface: netdev struct of the mesh interface
475 * @addr: the mac address of the client to add
476 * @vid: VLAN identifier
477 * @ifindex: index of the interface where the client is connected to (useful to
478 * identify wireless clients)
479 *
480 * Returns true if the client was successfully added, false otherwise.
481 */
482bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
483 unsigned short vid, int ifindex)
268{ 484{
269 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 485 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
270 struct batadv_tt_local_entry *tt_local; 486 struct batadv_tt_local_entry *tt_local;
271 struct batadv_tt_global_entry *tt_global; 487 struct batadv_tt_global_entry *tt_global;
488 struct net_device *in_dev = NULL;
272 struct hlist_head *head; 489 struct hlist_head *head;
273 struct batadv_tt_orig_list_entry *orig_entry; 490 struct batadv_tt_orig_list_entry *orig_entry;
274 int hash_added; 491 int hash_added, table_size, packet_size_max;
275 bool roamed_back = false; 492 bool ret = false, roamed_back = false;
493 uint8_t remote_flags;
494
495 if (ifindex != BATADV_NULL_IFINDEX)
496 in_dev = dev_get_by_index(&init_net, ifindex);
276 497
277 tt_local = batadv_tt_local_hash_find(bat_priv, addr); 498 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
278 tt_global = batadv_tt_global_hash_find(bat_priv, addr); 499 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
279 500
280 if (tt_local) { 501 if (tt_local) {
281 tt_local->last_seen = jiffies; 502 tt_local->last_seen = jiffies;
282 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) { 503 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
283 batadv_dbg(BATADV_DBG_TT, bat_priv, 504 batadv_dbg(BATADV_DBG_TT, bat_priv,
284 "Re-adding pending client %pM\n", addr); 505 "Re-adding pending client %pM (vid: %d)\n",
506 addr, BATADV_PRINT_VID(vid));
285 /* whatever the reason why the PENDING flag was set, 507 /* whatever the reason why the PENDING flag was set,
286 * this is a client which was enqueued to be removed in 508 * this is a client which was enqueued to be removed in
287 * this orig_interval. Since it popped up again, the 509 * this orig_interval. Since it popped up again, the
@@ -293,8 +515,8 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
293 515
294 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) { 516 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
295 batadv_dbg(BATADV_DBG_TT, bat_priv, 517 batadv_dbg(BATADV_DBG_TT, bat_priv,
296 "Roaming client %pM came back to its original location\n", 518 "Roaming client %pM (vid: %d) came back to its original location\n",
297 addr); 519 addr, BATADV_PRINT_VID(vid));
298 /* the ROAM flag is set because this client roamed away 520 /* the ROAM flag is set because this client roamed away
299 * and the node got a roaming_advertisement message. Now 521 * and the node got a roaming_advertisement message. Now
300 * that the client popped up again at its original 522 * that the client popped up again at its original
@@ -306,12 +528,24 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
306 goto check_roaming; 528 goto check_roaming;
307 } 529 }
308 530
531 /* Ignore the client if we cannot send it in a full table response. */
532 table_size = batadv_tt_local_table_transmit_size(bat_priv);
533 table_size += batadv_tt_len(1);
534 packet_size_max = atomic_read(&bat_priv->packet_size_max);
535 if (table_size > packet_size_max) {
536 net_ratelimited_function(batadv_info, soft_iface,
537 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
538 table_size, packet_size_max, addr);
539 goto out;
540 }
541
309 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC); 542 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
310 if (!tt_local) 543 if (!tt_local)
311 goto out; 544 goto out;
312 545
313 batadv_dbg(BATADV_DBG_TT, bat_priv, 546 batadv_dbg(BATADV_DBG_TT, bat_priv,
314 "Creating new local tt entry: %pM (ttvn: %d)\n", addr, 547 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
548 addr, BATADV_PRINT_VID(vid),
315 (uint8_t)atomic_read(&bat_priv->tt.vn)); 549 (uint8_t)atomic_read(&bat_priv->tt.vn));
316 550
317 memcpy(tt_local->common.addr, addr, ETH_ALEN); 551 memcpy(tt_local->common.addr, addr, ETH_ALEN);
@@ -320,7 +554,8 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
320 * (consistency check) 554 * (consistency check)
321 */ 555 */
322 tt_local->common.flags = BATADV_TT_CLIENT_NEW; 556 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
323 if (batadv_is_wifi_iface(ifindex)) 557 tt_local->common.vid = vid;
558 if (batadv_is_wifi_netdev(in_dev))
324 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI; 559 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
325 atomic_set(&tt_local->common.refcount, 2); 560 atomic_set(&tt_local->common.refcount, 2);
326 tt_local->last_seen = jiffies; 561 tt_local->last_seen = jiffies;
@@ -331,7 +566,7 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
331 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE; 566 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
332 567
333 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt, 568 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
334 batadv_choose_orig, &tt_local->common, 569 batadv_choose_tt, &tt_local->common,
335 &tt_local->common.hash_entry); 570 &tt_local->common.hash_entry);
336 571
337 if (unlikely(hash_added != 0)) { 572 if (unlikely(hash_added != 0)) {
@@ -353,6 +588,7 @@ check_roaming:
353 rcu_read_lock(); 588 rcu_read_lock();
354 hlist_for_each_entry_rcu(orig_entry, head, list) { 589 hlist_for_each_entry_rcu(orig_entry, head, list) {
355 batadv_send_roam_adv(bat_priv, tt_global->common.addr, 590 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
591 tt_global->common.vid,
356 orig_entry->orig_node); 592 orig_entry->orig_node);
357 } 593 }
358 rcu_read_unlock(); 594 rcu_read_unlock();
@@ -369,78 +605,219 @@ check_roaming:
369 } 605 }
370 } 606 }
371 607
608 /* store the current remote flags before altering them. This helps
609 * understanding is flags are changing or not
610 */
611 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
612
613 if (batadv_is_wifi_netdev(in_dev))
614 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
615 else
616 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
617
618 /* if any "dynamic" flag has been modified, resend an ADD event for this
619 * entry so that all the nodes can get the new flags
620 */
621 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
622 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
623
624 ret = true;
372out: 625out:
626 if (in_dev)
627 dev_put(in_dev);
373 if (tt_local) 628 if (tt_local)
374 batadv_tt_local_entry_free_ref(tt_local); 629 batadv_tt_local_entry_free_ref(tt_local);
375 if (tt_global) 630 if (tt_global)
376 batadv_tt_global_entry_free_ref(tt_global); 631 batadv_tt_global_entry_free_ref(tt_global);
632 return ret;
377} 633}
378 634
379static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff, 635/**
380 int *packet_buff_len, 636 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
381 int min_packet_len, 637 * within a TT Response directed to another node
382 int new_packet_len) 638 * @orig_node: originator for which the TT data has to be prepared
639 * @tt_data: uninitialised pointer to the address of the TVLV buffer
640 * @tt_change: uninitialised pointer to the address of the area where the TT
641 * changed can be stored
642 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
643 * function reserves the amount of space needed to send the entire global TT
644 * table. In case of success the value is updated with the real amount of
645 * reserved bytes
646
647 * Allocate the needed amount of memory for the entire TT TVLV and write its
648 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
649 * objects, one per active VLAN served by the originator node.
650 *
651 * Return the size of the allocated buffer or 0 in case of failure.
652 */
653static uint16_t
654batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
655 struct batadv_tvlv_tt_data **tt_data,
656 struct batadv_tvlv_tt_change **tt_change,
657 int32_t *tt_len)
383{ 658{
384 unsigned char *new_buff; 659 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
660 struct batadv_tvlv_tt_vlan_data *tt_vlan;
661 struct batadv_orig_node_vlan *vlan;
662 uint8_t *tt_change_ptr;
385 663
386 new_buff = kmalloc(new_packet_len, GFP_ATOMIC); 664 rcu_read_lock();
665 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
666 num_vlan++;
667 num_entries += atomic_read(&vlan->tt.num_entries);
668 }
387 669
388 /* keep old buffer if kmalloc should fail */ 670 change_offset = sizeof(**tt_data);
389 if (new_buff) { 671 change_offset += num_vlan * sizeof(*tt_vlan);
390 memcpy(new_buff, *packet_buff, min_packet_len); 672
391 kfree(*packet_buff); 673 /* if tt_len is negative, allocate the space needed by the full table */
392 *packet_buff = new_buff; 674 if (*tt_len < 0)
393 *packet_buff_len = new_packet_len; 675 *tt_len = batadv_tt_len(num_entries);
676
677 tvlv_len = *tt_len;
678 tvlv_len += change_offset;
679
680 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
681 if (!*tt_data) {
682 *tt_len = 0;
683 goto out;
394 } 684 }
685
686 (*tt_data)->flags = BATADV_NO_FLAGS;
687 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
688 (*tt_data)->num_vlan = htons(num_vlan);
689
690 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
691 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
692 tt_vlan->vid = htons(vlan->vid);
693 tt_vlan->crc = htonl(vlan->tt.crc);
694
695 tt_vlan++;
696 }
697
698 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
699 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
700
701out:
702 rcu_read_unlock();
703 return tvlv_len;
395} 704}
396 705
397static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv, 706/**
398 unsigned char **packet_buff, 707 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
399 int *packet_buff_len, 708 * node
400 int min_packet_len) 709 * @bat_priv: the bat priv with all the soft interface information
401{ 710 * @tt_data: uninitialised pointer to the address of the TVLV buffer
402 int req_len; 711 * @tt_change: uninitialised pointer to the address of the area where the TT
712 * changes can be stored
713 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
714 * function reserves the amount of space needed to send the entire local TT
715 * table. In case of success the value is updated with the real amount of
716 * reserved bytes
717 *
718 * Allocate the needed amount of memory for the entire TT TVLV and write its
719 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
720 * objects, one per active VLAN.
721 *
722 * Return the size of the allocated buffer or 0 in case of failure.
723 */
724static uint16_t
725batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
726 struct batadv_tvlv_tt_data **tt_data,
727 struct batadv_tvlv_tt_change **tt_change,
728 int32_t *tt_len)
729{
730 struct batadv_tvlv_tt_vlan_data *tt_vlan;
731 struct batadv_softif_vlan *vlan;
732 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
733 uint8_t *tt_change_ptr;
734 int change_offset;
403 735
404 req_len = min_packet_len; 736 rcu_read_lock();
405 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes)); 737 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
738 num_vlan++;
739 num_entries += atomic_read(&vlan->tt.num_entries);
740 }
406 741
407 /* if we have too many changes for one packet don't send any 742 change_offset = sizeof(**tt_data);
408 * and wait for the tt table request which will be fragmented 743 change_offset += num_vlan * sizeof(*tt_vlan);
409 */ 744
410 if (req_len > bat_priv->soft_iface->mtu) 745 /* if tt_len is negative, allocate the space needed by the full table */
411 req_len = min_packet_len; 746 if (*tt_len < 0)
747 *tt_len = batadv_tt_len(num_entries);
748
749 tvlv_len = *tt_len;
750 tvlv_len += change_offset;
751
752 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
753 if (!*tt_data) {
754 tvlv_len = 0;
755 goto out;
756 }
757
758 (*tt_data)->flags = BATADV_NO_FLAGS;
759 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
760 (*tt_data)->num_vlan = htons(num_vlan);
761
762 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
763 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
764 tt_vlan->vid = htons(vlan->vid);
765 tt_vlan->crc = htonl(vlan->tt.crc);
412 766
413 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len, 767 tt_vlan++;
414 min_packet_len, req_len); 768 }
769
770 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
771 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
772
773out:
774 rcu_read_unlock();
775 return tvlv_len;
415} 776}
416 777
417static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv, 778/**
418 unsigned char **packet_buff, 779 * batadv_tt_tvlv_container_update - update the translation table tvlv container
419 int *packet_buff_len, 780 * after local tt changes have been committed
420 int min_packet_len) 781 * @bat_priv: the bat priv with all the soft interface information
782 */
783static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
421{ 784{
422 struct batadv_tt_change_node *entry, *safe; 785 struct batadv_tt_change_node *entry, *safe;
423 int count = 0, tot_changes = 0, new_len; 786 struct batadv_tvlv_tt_data *tt_data;
424 unsigned char *tt_buff; 787 struct batadv_tvlv_tt_change *tt_change;
788 int tt_diff_len, tt_change_len = 0;
789 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
790 uint16_t tvlv_len;
425 791
426 batadv_tt_prepare_packet_buff(bat_priv, packet_buff, 792 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
427 packet_buff_len, min_packet_len); 793 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
428 794
429 new_len = *packet_buff_len - min_packet_len; 795 /* if we have too many changes for one packet don't send any
430 tt_buff = *packet_buff + min_packet_len; 796 * and wait for the tt table request which will be fragmented
797 */
798 if (tt_diff_len > bat_priv->soft_iface->mtu)
799 tt_diff_len = 0;
800
801 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
802 &tt_change, &tt_diff_len);
803 if (!tvlv_len)
804 return;
431 805
432 if (new_len > 0) 806 tt_data->flags = BATADV_TT_OGM_DIFF;
433 tot_changes = new_len / batadv_tt_len(1); 807
808 if (tt_diff_len == 0)
809 goto container_register;
434 810
435 spin_lock_bh(&bat_priv->tt.changes_list_lock); 811 spin_lock_bh(&bat_priv->tt.changes_list_lock);
436 atomic_set(&bat_priv->tt.local_changes, 0); 812 atomic_set(&bat_priv->tt.local_changes, 0);
437 813
438 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list, 814 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
439 list) { 815 list) {
440 if (count < tot_changes) { 816 if (tt_diff_entries_count < tt_diff_entries_num) {
441 memcpy(tt_buff + batadv_tt_len(count), 817 memcpy(tt_change + tt_diff_entries_count,
442 &entry->change, sizeof(struct batadv_tt_change)); 818 &entry->change,
443 count++; 819 sizeof(struct batadv_tvlv_tt_change));
820 tt_diff_entries_count++;
444 } 821 }
445 list_del(&entry->list); 822 list_del(&entry->list);
446 kfree(entry); 823 kfree(entry);
@@ -452,20 +829,25 @@ static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
452 kfree(bat_priv->tt.last_changeset); 829 kfree(bat_priv->tt.last_changeset);
453 bat_priv->tt.last_changeset_len = 0; 830 bat_priv->tt.last_changeset_len = 0;
454 bat_priv->tt.last_changeset = NULL; 831 bat_priv->tt.last_changeset = NULL;
832 tt_change_len = batadv_tt_len(tt_diff_entries_count);
455 /* check whether this new OGM has no changes due to size problems */ 833 /* check whether this new OGM has no changes due to size problems */
456 if (new_len > 0) { 834 if (tt_diff_entries_count > 0) {
457 /* if kmalloc() fails we will reply with the full table 835 /* if kmalloc() fails we will reply with the full table
458 * instead of providing the diff 836 * instead of providing the diff
459 */ 837 */
460 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC); 838 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
461 if (bat_priv->tt.last_changeset) { 839 if (bat_priv->tt.last_changeset) {
462 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len); 840 memcpy(bat_priv->tt.last_changeset,
463 bat_priv->tt.last_changeset_len = new_len; 841 tt_change, tt_change_len);
842 bat_priv->tt.last_changeset_len = tt_diff_len;
464 } 843 }
465 } 844 }
466 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 845 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
467 846
468 return count; 847container_register:
848 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
849 tvlv_len);
850 kfree(tt_data);
469} 851}
470 852
471int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset) 853int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
@@ -476,7 +858,9 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
476 struct batadv_tt_common_entry *tt_common_entry; 858 struct batadv_tt_common_entry *tt_common_entry;
477 struct batadv_tt_local_entry *tt_local; 859 struct batadv_tt_local_entry *tt_local;
478 struct batadv_hard_iface *primary_if; 860 struct batadv_hard_iface *primary_if;
861 struct batadv_softif_vlan *vlan;
479 struct hlist_head *head; 862 struct hlist_head *head;
863 unsigned short vid;
480 uint32_t i; 864 uint32_t i;
481 int last_seen_secs; 865 int last_seen_secs;
482 int last_seen_msecs; 866 int last_seen_msecs;
@@ -489,11 +873,10 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
489 goto out; 873 goto out;
490 874
491 seq_printf(seq, 875 seq_printf(seq,
492 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n", 876 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
493 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn), 877 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
494 bat_priv->tt.local_crc); 878 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
495 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags", 879 "Flags", "Last seen", "CRC");
496 "Last seen");
497 880
498 for (i = 0; i < hash->size; i++) { 881 for (i = 0; i < hash->size; i++) {
499 head = &hash->table[i]; 882 head = &hash->table[i];
@@ -504,6 +887,7 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
504 tt_local = container_of(tt_common_entry, 887 tt_local = container_of(tt_common_entry,
505 struct batadv_tt_local_entry, 888 struct batadv_tt_local_entry,
506 common); 889 common);
890 vid = tt_common_entry->vid;
507 last_seen_jiffies = jiffies - tt_local->last_seen; 891 last_seen_jiffies = jiffies - tt_local->last_seen;
508 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 892 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
509 last_seen_secs = last_seen_msecs / 1000; 893 last_seen_secs = last_seen_msecs / 1000;
@@ -511,8 +895,17 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
511 895
512 no_purge = tt_common_entry->flags & np_flag; 896 no_purge = tt_common_entry->flags & np_flag;
513 897
514 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n", 898 vlan = batadv_softif_vlan_get(bat_priv, vid);
899 if (!vlan) {
900 seq_printf(seq, "Cannot retrieve VLAN %d\n",
901 BATADV_PRINT_VID(vid));
902 continue;
903 }
904
905 seq_printf(seq,
906 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
515 tt_common_entry->addr, 907 tt_common_entry->addr,
908 BATADV_PRINT_VID(tt_common_entry->vid),
516 (tt_common_entry->flags & 909 (tt_common_entry->flags &
517 BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 910 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
518 no_purge ? 'P' : '.', 911 no_purge ? 'P' : '.',
@@ -523,7 +916,10 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
523 (tt_common_entry->flags & 916 (tt_common_entry->flags &
524 BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 917 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
525 no_purge ? 0 : last_seen_secs, 918 no_purge ? 0 : last_seen_secs,
526 no_purge ? 0 : last_seen_msecs); 919 no_purge ? 0 : last_seen_msecs,
920 vlan->tt.crc);
921
922 batadv_softif_vlan_free_ref(vlan);
527 } 923 }
528 rcu_read_unlock(); 924 rcu_read_unlock();
529 } 925 }
@@ -547,27 +943,29 @@ batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
547 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING; 943 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
548 944
549 batadv_dbg(BATADV_DBG_TT, bat_priv, 945 batadv_dbg(BATADV_DBG_TT, bat_priv,
550 "Local tt entry (%pM) pending to be removed: %s\n", 946 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
551 tt_local_entry->common.addr, message); 947 tt_local_entry->common.addr,
948 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
552} 949}
553 950
554/** 951/**
555 * batadv_tt_local_remove - logically remove an entry from the local table 952 * batadv_tt_local_remove - logically remove an entry from the local table
556 * @bat_priv: the bat priv with all the soft interface information 953 * @bat_priv: the bat priv with all the soft interface information
557 * @addr: the MAC address of the client to remove 954 * @addr: the MAC address of the client to remove
955 * @vid: VLAN identifier
558 * @message: message to append to the log on deletion 956 * @message: message to append to the log on deletion
559 * @roaming: true if the deletion is due to a roaming event 957 * @roaming: true if the deletion is due to a roaming event
560 * 958 *
561 * Returns the flags assigned to the local entry before being deleted 959 * Returns the flags assigned to the local entry before being deleted
562 */ 960 */
563uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv, 961uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
564 const uint8_t *addr, const char *message, 962 const uint8_t *addr, unsigned short vid,
565 bool roaming) 963 const char *message, bool roaming)
566{ 964{
567 struct batadv_tt_local_entry *tt_local_entry; 965 struct batadv_tt_local_entry *tt_local_entry;
568 uint16_t flags, curr_flags = BATADV_NO_FLAGS; 966 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
569 967
570 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 968 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
571 if (!tt_local_entry) 969 if (!tt_local_entry)
572 goto out; 970 goto out;
573 971
@@ -603,8 +1001,16 @@ out:
603 return curr_flags; 1001 return curr_flags;
604} 1002}
605 1003
1004/**
1005 * batadv_tt_local_purge_list - purge inactive tt local entries
1006 * @bat_priv: the bat priv with all the soft interface information
1007 * @head: pointer to the list containing the local tt entries
1008 * @timeout: parameter deciding whether a given tt local entry is considered
1009 * inactive or not
1010 */
606static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv, 1011static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
607 struct hlist_head *head) 1012 struct hlist_head *head,
1013 int timeout)
608{ 1014{
609 struct batadv_tt_local_entry *tt_local_entry; 1015 struct batadv_tt_local_entry *tt_local_entry;
610 struct batadv_tt_common_entry *tt_common_entry; 1016 struct batadv_tt_common_entry *tt_common_entry;
@@ -622,8 +1028,7 @@ static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
622 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) 1028 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
623 continue; 1029 continue;
624 1030
625 if (!batadv_has_timed_out(tt_local_entry->last_seen, 1031 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
626 BATADV_TT_LOCAL_TIMEOUT))
627 continue; 1032 continue;
628 1033
629 batadv_tt_local_set_pending(bat_priv, tt_local_entry, 1034 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
@@ -631,7 +1036,14 @@ static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
631 } 1036 }
632} 1037}
633 1038
634static void batadv_tt_local_purge(struct batadv_priv *bat_priv) 1039/**
1040 * batadv_tt_local_purge - purge inactive tt local entries
1041 * @bat_priv: the bat priv with all the soft interface information
1042 * @timeout: parameter deciding whether a given tt local entry is considered
1043 * inactive or not
1044 */
1045static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1046 int timeout)
635{ 1047{
636 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 1048 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
637 struct hlist_head *head; 1049 struct hlist_head *head;
@@ -643,7 +1055,7 @@ static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
643 list_lock = &hash->list_locks[i]; 1055 list_lock = &hash->list_locks[i];
644 1056
645 spin_lock_bh(list_lock); 1057 spin_lock_bh(list_lock);
646 batadv_tt_local_purge_list(bat_priv, head); 1058 batadv_tt_local_purge_list(bat_priv, head, timeout);
647 spin_unlock_bh(list_lock); 1059 spin_unlock_bh(list_lock);
648 } 1060 }
649} 1061}
@@ -784,7 +1196,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
784 1196
785 INIT_HLIST_NODE(&orig_entry->list); 1197 INIT_HLIST_NODE(&orig_entry->list);
786 atomic_inc(&orig_node->refcount); 1198 atomic_inc(&orig_node->refcount);
787 atomic_inc(&orig_node->tt_size); 1199 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
788 orig_entry->orig_node = orig_node; 1200 orig_entry->orig_node = orig_node;
789 orig_entry->ttvn = ttvn; 1201 orig_entry->ttvn = ttvn;
790 atomic_set(&orig_entry->refcount, 2); 1202 atomic_set(&orig_entry->refcount, 2);
@@ -803,6 +1215,7 @@ out:
803 * @bat_priv: the bat priv with all the soft interface information 1215 * @bat_priv: the bat priv with all the soft interface information
804 * @orig_node: the originator announcing the client 1216 * @orig_node: the originator announcing the client
805 * @tt_addr: the mac address of the non-mesh client 1217 * @tt_addr: the mac address of the non-mesh client
1218 * @vid: VLAN identifier
806 * @flags: TT flags that have to be set for this non-mesh client 1219 * @flags: TT flags that have to be set for this non-mesh client
807 * @ttvn: the tt version number ever announcing this non-mesh client 1220 * @ttvn: the tt version number ever announcing this non-mesh client
808 * 1221 *
@@ -813,21 +1226,28 @@ out:
813 * If a TT local entry exists for this non-mesh client remove it. 1226 * If a TT local entry exists for this non-mesh client remove it.
814 * 1227 *
815 * The caller must hold orig_node refcount. 1228 * The caller must hold orig_node refcount.
1229 *
1230 * Return true if the new entry has been added, false otherwise
816 */ 1231 */
817int batadv_tt_global_add(struct batadv_priv *bat_priv, 1232static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
818 struct batadv_orig_node *orig_node, 1233 struct batadv_orig_node *orig_node,
819 const unsigned char *tt_addr, uint16_t flags, 1234 const unsigned char *tt_addr,
820 uint8_t ttvn) 1235 unsigned short vid, uint16_t flags,
1236 uint8_t ttvn)
821{ 1237{
822 struct batadv_tt_global_entry *tt_global_entry; 1238 struct batadv_tt_global_entry *tt_global_entry;
823 struct batadv_tt_local_entry *tt_local_entry; 1239 struct batadv_tt_local_entry *tt_local_entry;
824 int ret = 0; 1240 bool ret = false;
825 int hash_added; 1241 int hash_added;
826 struct batadv_tt_common_entry *common; 1242 struct batadv_tt_common_entry *common;
827 uint16_t local_flags; 1243 uint16_t local_flags;
828 1244
829 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr); 1245 /* ignore global entries from backbone nodes */
830 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr); 1246 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1247 return true;
1248
1249 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1250 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
831 1251
832 /* if the node already has a local client for this entry, it has to wait 1252 /* if the node already has a local client for this entry, it has to wait
833 * for a roaming advertisement instead of manually messing up the global 1253 * for a roaming advertisement instead of manually messing up the global
@@ -844,6 +1264,7 @@ int batadv_tt_global_add(struct batadv_priv *bat_priv,
844 1264
845 common = &tt_global_entry->common; 1265 common = &tt_global_entry->common;
846 memcpy(common->addr, tt_addr, ETH_ALEN); 1266 memcpy(common->addr, tt_addr, ETH_ALEN);
1267 common->vid = vid;
847 1268
848 common->flags = flags; 1269 common->flags = flags;
849 tt_global_entry->roam_at = 0; 1270 tt_global_entry->roam_at = 0;
@@ -861,7 +1282,7 @@ int batadv_tt_global_add(struct batadv_priv *bat_priv,
861 1282
862 hash_added = batadv_hash_add(bat_priv->tt.global_hash, 1283 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
863 batadv_compare_tt, 1284 batadv_compare_tt,
864 batadv_choose_orig, common, 1285 batadv_choose_tt, common,
865 &common->hash_entry); 1286 &common->hash_entry);
866 1287
867 if (unlikely(hash_added != 0)) { 1288 if (unlikely(hash_added != 0)) {
@@ -920,14 +1341,15 @@ add_orig_entry:
920 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn); 1341 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
921 1342
922 batadv_dbg(BATADV_DBG_TT, bat_priv, 1343 batadv_dbg(BATADV_DBG_TT, bat_priv,
923 "Creating new global tt entry: %pM (via %pM)\n", 1344 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
924 common->addr, orig_node->orig); 1345 common->addr, BATADV_PRINT_VID(common->vid),
925 ret = 1; 1346 orig_node->orig);
1347 ret = true;
926 1348
927out_remove: 1349out_remove:
928 1350
929 /* remove address from local hash if present */ 1351 /* remove address from local hash if present */
930 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, 1352 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
931 "global tt received", 1353 "global tt received",
932 flags & BATADV_TT_CLIENT_ROAM); 1354 flags & BATADV_TT_CLIENT_ROAM);
933 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI; 1355 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
@@ -947,18 +1369,20 @@ out:
947} 1369}
948 1370
949/* batadv_transtable_best_orig - Get best originator list entry from tt entry 1371/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1372 * @bat_priv: the bat priv with all the soft interface information
950 * @tt_global_entry: global translation table entry to be analyzed 1373 * @tt_global_entry: global translation table entry to be analyzed
951 * 1374 *
952 * This functon assumes the caller holds rcu_read_lock(). 1375 * This functon assumes the caller holds rcu_read_lock().
953 * Returns best originator list entry or NULL on errors. 1376 * Returns best originator list entry or NULL on errors.
954 */ 1377 */
955static struct batadv_tt_orig_list_entry * 1378static struct batadv_tt_orig_list_entry *
956batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry) 1379batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1380 struct batadv_tt_global_entry *tt_global_entry)
957{ 1381{
958 struct batadv_neigh_node *router = NULL; 1382 struct batadv_neigh_node *router, *best_router = NULL;
1383 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
959 struct hlist_head *head; 1384 struct hlist_head *head;
960 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL; 1385 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
961 int best_tq = 0;
962 1386
963 head = &tt_global_entry->orig_list; 1387 head = &tt_global_entry->orig_list;
964 hlist_for_each_entry_rcu(orig_entry, head, list) { 1388 hlist_for_each_entry_rcu(orig_entry, head, list) {
@@ -966,64 +1390,104 @@ batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
966 if (!router) 1390 if (!router)
967 continue; 1391 continue;
968 1392
969 if (router->tq_avg > best_tq) { 1393 if (best_router &&
970 best_entry = orig_entry; 1394 bao->bat_neigh_cmp(router, best_router) <= 0) {
971 best_tq = router->tq_avg; 1395 batadv_neigh_node_free_ref(router);
1396 continue;
972 } 1397 }
973 1398
974 batadv_neigh_node_free_ref(router); 1399 /* release the refcount for the "old" best */
1400 if (best_router)
1401 batadv_neigh_node_free_ref(best_router);
1402
1403 best_entry = orig_entry;
1404 best_router = router;
975 } 1405 }
976 1406
1407 if (best_router)
1408 batadv_neigh_node_free_ref(best_router);
1409
977 return best_entry; 1410 return best_entry;
978} 1411}
979 1412
980/* batadv_tt_global_print_entry - print all orig nodes who announce the address 1413/* batadv_tt_global_print_entry - print all orig nodes who announce the address
981 * for this global entry 1414 * for this global entry
1415 * @bat_priv: the bat priv with all the soft interface information
982 * @tt_global_entry: global translation table entry to be printed 1416 * @tt_global_entry: global translation table entry to be printed
983 * @seq: debugfs table seq_file struct 1417 * @seq: debugfs table seq_file struct
984 * 1418 *
985 * This functon assumes the caller holds rcu_read_lock(). 1419 * This functon assumes the caller holds rcu_read_lock().
986 */ 1420 */
987static void 1421static void
988batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry, 1422batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1423 struct batadv_tt_global_entry *tt_global_entry,
989 struct seq_file *seq) 1424 struct seq_file *seq)
990{ 1425{
991 struct hlist_head *head;
992 struct batadv_tt_orig_list_entry *orig_entry, *best_entry; 1426 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
993 struct batadv_tt_common_entry *tt_common_entry; 1427 struct batadv_tt_common_entry *tt_common_entry;
994 uint16_t flags; 1428 struct batadv_orig_node_vlan *vlan;
1429 struct hlist_head *head;
995 uint8_t last_ttvn; 1430 uint8_t last_ttvn;
1431 uint16_t flags;
996 1432
997 tt_common_entry = &tt_global_entry->common; 1433 tt_common_entry = &tt_global_entry->common;
998 flags = tt_common_entry->flags; 1434 flags = tt_common_entry->flags;
999 1435
1000 best_entry = batadv_transtable_best_orig(tt_global_entry); 1436 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1001 if (best_entry) { 1437 if (best_entry) {
1438 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1439 tt_common_entry->vid);
1440 if (!vlan) {
1441 seq_printf(seq,
1442 " * Cannot retrieve VLAN %d for originator %pM\n",
1443 BATADV_PRINT_VID(tt_common_entry->vid),
1444 best_entry->orig_node->orig);
1445 goto print_list;
1446 }
1447
1002 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn); 1448 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1003 seq_printf(seq, 1449 seq_printf(seq,
1004 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n", 1450 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1005 '*', tt_global_entry->common.addr, 1451 '*', tt_global_entry->common.addr,
1452 BATADV_PRINT_VID(tt_global_entry->common.vid),
1006 best_entry->ttvn, best_entry->orig_node->orig, 1453 best_entry->ttvn, best_entry->orig_node->orig,
1007 last_ttvn, best_entry->orig_node->tt_crc, 1454 last_ttvn, vlan->tt.crc,
1008 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 1455 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1009 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 1456 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1010 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.')); 1457 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1458
1459 batadv_orig_node_vlan_free_ref(vlan);
1011 } 1460 }
1012 1461
1462print_list:
1013 head = &tt_global_entry->orig_list; 1463 head = &tt_global_entry->orig_list;
1014 1464
1015 hlist_for_each_entry_rcu(orig_entry, head, list) { 1465 hlist_for_each_entry_rcu(orig_entry, head, list) {
1016 if (best_entry == orig_entry) 1466 if (best_entry == orig_entry)
1017 continue; 1467 continue;
1018 1468
1469 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1470 tt_common_entry->vid);
1471 if (!vlan) {
1472 seq_printf(seq,
1473 " + Cannot retrieve VLAN %d for originator %pM\n",
1474 BATADV_PRINT_VID(tt_common_entry->vid),
1475 orig_entry->orig_node->orig);
1476 continue;
1477 }
1478
1019 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn); 1479 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1020 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n", 1480 seq_printf(seq,
1481 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1021 '+', tt_global_entry->common.addr, 1482 '+', tt_global_entry->common.addr,
1483 BATADV_PRINT_VID(tt_global_entry->common.vid),
1022 orig_entry->ttvn, orig_entry->orig_node->orig, 1484 orig_entry->ttvn, orig_entry->orig_node->orig,
1023 last_ttvn, 1485 last_ttvn, vlan->tt.crc,
1024 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 1486 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1025 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 1487 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1026 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.')); 1488 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1489
1490 batadv_orig_node_vlan_free_ref(vlan);
1027 } 1491 }
1028} 1492}
1029 1493
@@ -1045,9 +1509,9 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1045 seq_printf(seq, 1509 seq_printf(seq,
1046 "Globally announced TT entries received via the mesh %s\n", 1510 "Globally announced TT entries received via the mesh %s\n",
1047 net_dev->name); 1511 net_dev->name);
1048 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n", 1512 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1049 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC", 1513 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1050 "Flags"); 1514 "CRC", "Flags");
1051 1515
1052 for (i = 0; i < hash->size; i++) { 1516 for (i = 0; i < hash->size; i++) {
1053 head = &hash->table[i]; 1517 head = &hash->table[i];
@@ -1058,7 +1522,7 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1058 tt_global = container_of(tt_common_entry, 1522 tt_global = container_of(tt_common_entry,
1059 struct batadv_tt_global_entry, 1523 struct batadv_tt_global_entry,
1060 common); 1524 common);
1061 batadv_tt_global_print_entry(tt_global, seq); 1525 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1062 } 1526 }
1063 rcu_read_unlock(); 1527 rcu_read_unlock();
1064 } 1528 }
@@ -1080,6 +1544,8 @@ batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1080 head = &tt_global_entry->orig_list; 1544 head = &tt_global_entry->orig_list;
1081 hlist_for_each_entry_safe(orig_entry, safe, head, list) { 1545 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1082 hlist_del_rcu(&orig_entry->list); 1546 hlist_del_rcu(&orig_entry->list);
1547 batadv_tt_global_size_dec(orig_entry->orig_node,
1548 tt_global_entry->common.vid);
1083 batadv_tt_orig_list_entry_free_ref(orig_entry); 1549 batadv_tt_orig_list_entry_free_ref(orig_entry);
1084 } 1550 }
1085 spin_unlock_bh(&tt_global_entry->list_lock); 1551 spin_unlock_bh(&tt_global_entry->list_lock);
@@ -1094,16 +1560,21 @@ batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1094 struct hlist_head *head; 1560 struct hlist_head *head;
1095 struct hlist_node *safe; 1561 struct hlist_node *safe;
1096 struct batadv_tt_orig_list_entry *orig_entry; 1562 struct batadv_tt_orig_list_entry *orig_entry;
1563 unsigned short vid;
1097 1564
1098 spin_lock_bh(&tt_global_entry->list_lock); 1565 spin_lock_bh(&tt_global_entry->list_lock);
1099 head = &tt_global_entry->orig_list; 1566 head = &tt_global_entry->orig_list;
1100 hlist_for_each_entry_safe(orig_entry, safe, head, list) { 1567 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1101 if (orig_entry->orig_node == orig_node) { 1568 if (orig_entry->orig_node == orig_node) {
1569 vid = tt_global_entry->common.vid;
1102 batadv_dbg(BATADV_DBG_TT, bat_priv, 1570 batadv_dbg(BATADV_DBG_TT, bat_priv,
1103 "Deleting %pM from global tt entry %pM: %s\n", 1571 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1104 orig_node->orig, 1572 orig_node->orig,
1105 tt_global_entry->common.addr, message); 1573 tt_global_entry->common.addr,
1574 BATADV_PRINT_VID(vid), message);
1106 hlist_del_rcu(&orig_entry->list); 1575 hlist_del_rcu(&orig_entry->list);
1576 batadv_tt_global_size_dec(orig_node,
1577 tt_global_entry->common.vid);
1107 batadv_tt_orig_list_entry_free_ref(orig_entry); 1578 batadv_tt_orig_list_entry_free_ref(orig_entry);
1108 } 1579 }
1109 } 1580 }
@@ -1150,17 +1621,25 @@ batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1150 orig_node, message); 1621 orig_node, message);
1151} 1622}
1152 1623
1153 1624/**
1154 1625 * batadv_tt_global_del - remove a client from the global table
1626 * @bat_priv: the bat priv with all the soft interface information
1627 * @orig_node: an originator serving this client
1628 * @addr: the mac address of the client
1629 * @vid: VLAN identifier
1630 * @message: a message explaining the reason for deleting the client to print
1631 * for debugging purpose
1632 * @roaming: true if the deletion has been triggered by a roaming event
1633 */
1155static void batadv_tt_global_del(struct batadv_priv *bat_priv, 1634static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1156 struct batadv_orig_node *orig_node, 1635 struct batadv_orig_node *orig_node,
1157 const unsigned char *addr, 1636 const unsigned char *addr, unsigned short vid,
1158 const char *message, bool roaming) 1637 const char *message, bool roaming)
1159{ 1638{
1160 struct batadv_tt_global_entry *tt_global_entry; 1639 struct batadv_tt_global_entry *tt_global_entry;
1161 struct batadv_tt_local_entry *local_entry = NULL; 1640 struct batadv_tt_local_entry *local_entry = NULL;
1162 1641
1163 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 1642 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1164 if (!tt_global_entry) 1643 if (!tt_global_entry)
1165 goto out; 1644 goto out;
1166 1645
@@ -1189,7 +1668,8 @@ static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1189 * the global entry, since it is useless now. 1668 * the global entry, since it is useless now.
1190 */ 1669 */
1191 local_entry = batadv_tt_local_hash_find(bat_priv, 1670 local_entry = batadv_tt_local_hash_find(bat_priv,
1192 tt_global_entry->common.addr); 1671 tt_global_entry->common.addr,
1672 vid);
1193 if (local_entry) { 1673 if (local_entry) {
1194 /* local entry exists, case 2: client roamed to us. */ 1674 /* local entry exists, case 2: client roamed to us. */
1195 batadv_tt_global_del_orig_list(tt_global_entry); 1675 batadv_tt_global_del_orig_list(tt_global_entry);
@@ -1207,8 +1687,18 @@ out:
1207 batadv_tt_local_entry_free_ref(local_entry); 1687 batadv_tt_local_entry_free_ref(local_entry);
1208} 1688}
1209 1689
1690/**
1691 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1692 * given originator matching the provided vid
1693 * @bat_priv: the bat priv with all the soft interface information
1694 * @orig_node: the originator owning the entries to remove
1695 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1696 * removed
1697 * @message: debug message to print as "reason"
1698 */
1210void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, 1699void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1211 struct batadv_orig_node *orig_node, 1700 struct batadv_orig_node *orig_node,
1701 int32_t match_vid,
1212 const char *message) 1702 const char *message)
1213{ 1703{
1214 struct batadv_tt_global_entry *tt_global; 1704 struct batadv_tt_global_entry *tt_global;
@@ -1218,6 +1708,7 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1218 struct hlist_node *safe; 1708 struct hlist_node *safe;
1219 struct hlist_head *head; 1709 struct hlist_head *head;
1220 spinlock_t *list_lock; /* protects write access to the hash lists */ 1710 spinlock_t *list_lock; /* protects write access to the hash lists */
1711 unsigned short vid;
1221 1712
1222 if (!hash) 1713 if (!hash)
1223 return; 1714 return;
@@ -1229,6 +1720,10 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1229 spin_lock_bh(list_lock); 1720 spin_lock_bh(list_lock);
1230 hlist_for_each_entry_safe(tt_common_entry, safe, 1721 hlist_for_each_entry_safe(tt_common_entry, safe,
1231 head, hash_entry) { 1722 head, hash_entry) {
1723 /* remove only matching entries */
1724 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1725 continue;
1726
1232 tt_global = container_of(tt_common_entry, 1727 tt_global = container_of(tt_common_entry,
1233 struct batadv_tt_global_entry, 1728 struct batadv_tt_global_entry,
1234 common); 1729 common);
@@ -1237,9 +1732,11 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1237 orig_node, message); 1732 orig_node, message);
1238 1733
1239 if (hlist_empty(&tt_global->orig_list)) { 1734 if (hlist_empty(&tt_global->orig_list)) {
1735 vid = tt_global->common.vid;
1240 batadv_dbg(BATADV_DBG_TT, bat_priv, 1736 batadv_dbg(BATADV_DBG_TT, bat_priv,
1241 "Deleting global tt entry %pM: %s\n", 1737 "Deleting global tt entry %pM (vid: %d): %s\n",
1242 tt_global->common.addr, message); 1738 tt_global->common.addr,
1739 BATADV_PRINT_VID(vid), message);
1243 hlist_del_rcu(&tt_common_entry->hash_entry); 1740 hlist_del_rcu(&tt_common_entry->hash_entry);
1244 batadv_tt_global_entry_free_ref(tt_global); 1741 batadv_tt_global_entry_free_ref(tt_global);
1245 } 1742 }
@@ -1297,8 +1794,10 @@ static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1297 continue; 1794 continue;
1298 1795
1299 batadv_dbg(BATADV_DBG_TT, bat_priv, 1796 batadv_dbg(BATADV_DBG_TT, bat_priv,
1300 "Deleting global tt entry (%pM): %s\n", 1797 "Deleting global tt entry %pM (vid: %d): %s\n",
1301 tt_global->common.addr, msg); 1798 tt_global->common.addr,
1799 BATADV_PRINT_VID(tt_global->common.vid),
1800 msg);
1302 1801
1303 hlist_del_rcu(&tt_common->hash_entry); 1802 hlist_del_rcu(&tt_common->hash_entry);
1304 1803
@@ -1357,23 +1856,49 @@ _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1357 return ret; 1856 return ret;
1358} 1857}
1359 1858
1859/**
1860 * batadv_transtable_search - get the mesh destination for a given client
1861 * @bat_priv: the bat priv with all the soft interface information
1862 * @src: mac address of the source client
1863 * @addr: mac address of the destination client
1864 * @vid: VLAN identifier
1865 *
1866 * Returns a pointer to the originator that was selected as destination in the
1867 * mesh for contacting the client 'addr', NULL otherwise.
1868 * In case of multiple originators serving the same client, the function returns
1869 * the best one (best in terms of metric towards the destination node).
1870 *
1871 * If the two clients are AP isolated the function returns NULL.
1872 */
1360struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, 1873struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1361 const uint8_t *src, 1874 const uint8_t *src,
1362 const uint8_t *addr) 1875 const uint8_t *addr,
1876 unsigned short vid)
1363{ 1877{
1364 struct batadv_tt_local_entry *tt_local_entry = NULL; 1878 struct batadv_tt_local_entry *tt_local_entry = NULL;
1365 struct batadv_tt_global_entry *tt_global_entry = NULL; 1879 struct batadv_tt_global_entry *tt_global_entry = NULL;
1366 struct batadv_orig_node *orig_node = NULL; 1880 struct batadv_orig_node *orig_node = NULL;
1367 struct batadv_tt_orig_list_entry *best_entry; 1881 struct batadv_tt_orig_list_entry *best_entry;
1882 bool ap_isolation_enabled = false;
1883 struct batadv_softif_vlan *vlan;
1368 1884
1369 if (src && atomic_read(&bat_priv->ap_isolation)) { 1885 /* if the AP isolation is requested on a VLAN, then check for its
1370 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src); 1886 * setting in the proper VLAN private data structure
1887 */
1888 vlan = batadv_softif_vlan_get(bat_priv, vid);
1889 if (vlan) {
1890 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1891 batadv_softif_vlan_free_ref(vlan);
1892 }
1893
1894 if (src && ap_isolation_enabled) {
1895 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
1371 if (!tt_local_entry || 1896 if (!tt_local_entry ||
1372 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)) 1897 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1373 goto out; 1898 goto out;
1374 } 1899 }
1375 1900
1376 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 1901 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1377 if (!tt_global_entry) 1902 if (!tt_global_entry)
1378 goto out; 1903 goto out;
1379 1904
@@ -1385,7 +1910,7 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1385 goto out; 1910 goto out;
1386 1911
1387 rcu_read_lock(); 1912 rcu_read_lock();
1388 best_entry = batadv_transtable_best_orig(tt_global_entry); 1913 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1389 /* found anything? */ 1914 /* found anything? */
1390 if (best_entry) 1915 if (best_entry)
1391 orig_node = best_entry->orig_node; 1916 orig_node = best_entry->orig_node;
@@ -1402,17 +1927,40 @@ out:
1402 return orig_node; 1927 return orig_node;
1403} 1928}
1404 1929
1405/* Calculates the checksum of the local table of a given orig_node */ 1930/**
1406static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv, 1931 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1407 struct batadv_orig_node *orig_node) 1932 * to the given orig_node
1933 * @bat_priv: the bat priv with all the soft interface information
1934 * @orig_node: originator for which the CRC should be computed
1935 * @vid: VLAN identifier for which the CRC32 has to be computed
1936 *
1937 * This function computes the checksum for the global table corresponding to a
1938 * specific originator. In particular, the checksum is computed as follows: For
1939 * each client connected to the originator the CRC32C of the MAC address and the
1940 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1941 * together.
1942 *
1943 * The idea behind is that CRC32C should be used as much as possible in order to
1944 * produce a unique hash of the table, but since the order which is used to feed
1945 * the CRC32C function affects the result and since every node in the network
1946 * probably sorts the clients differently, the hash function cannot be directly
1947 * computed over the entire table. Hence the CRC32C is used only on
1948 * the single client entry, while all the results are then xor'ed together
1949 * because the XOR operation can combine them all while trying to reduce the
1950 * noise as much as possible.
1951 *
1952 * Returns the checksum of the global table of a given originator.
1953 */
1954static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1955 struct batadv_orig_node *orig_node,
1956 unsigned short vid)
1408{ 1957{
1409 uint16_t total = 0, total_one;
1410 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 1958 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1411 struct batadv_tt_common_entry *tt_common; 1959 struct batadv_tt_common_entry *tt_common;
1412 struct batadv_tt_global_entry *tt_global; 1960 struct batadv_tt_global_entry *tt_global;
1413 struct hlist_head *head; 1961 struct hlist_head *head;
1414 uint32_t i; 1962 uint32_t i, crc_tmp, crc = 0;
1415 int j; 1963 uint8_t flags;
1416 1964
1417 for (i = 0; i < hash->size; i++) { 1965 for (i = 0; i < hash->size; i++) {
1418 head = &hash->table[i]; 1966 head = &hash->table[i];
@@ -1422,6 +1970,12 @@ static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1422 tt_global = container_of(tt_common, 1970 tt_global = container_of(tt_common,
1423 struct batadv_tt_global_entry, 1971 struct batadv_tt_global_entry,
1424 common); 1972 common);
1973 /* compute the CRC only for entries belonging to the
1974 * VLAN identified by the vid passed as parameter
1975 */
1976 if (tt_common->vid != vid)
1977 continue;
1978
1425 /* Roaming clients are in the global table for 1979 /* Roaming clients are in the global table for
1426 * consistency only. They don't have to be 1980 * consistency only. They don't have to be
1427 * taken into account while computing the 1981 * taken into account while computing the
@@ -1443,48 +1997,74 @@ static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1443 orig_node)) 1997 orig_node))
1444 continue; 1998 continue;
1445 1999
1446 total_one = 0; 2000 crc_tmp = crc32c(0, &tt_common->vid,
1447 for (j = 0; j < ETH_ALEN; j++) 2001 sizeof(tt_common->vid));
1448 total_one = crc16_byte(total_one, 2002
1449 tt_common->addr[j]); 2003 /* compute the CRC on flags that have to be kept in sync
1450 total ^= total_one; 2004 * among nodes
2005 */
2006 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2007 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2008
2009 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
1451 } 2010 }
1452 rcu_read_unlock(); 2011 rcu_read_unlock();
1453 } 2012 }
1454 2013
1455 return total; 2014 return crc;
1456} 2015}
1457 2016
1458/* Calculates the checksum of the local table */ 2017/**
1459static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv) 2018 * batadv_tt_local_crc - calculates the checksum of the local table
2019 * @bat_priv: the bat priv with all the soft interface information
2020 * @vid: VLAN identifier for which the CRC32 has to be computed
2021 *
2022 * For details about the computation, please refer to the documentation for
2023 * batadv_tt_global_crc().
2024 *
2025 * Returns the checksum of the local table
2026 */
2027static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2028 unsigned short vid)
1460{ 2029{
1461 uint16_t total = 0, total_one;
1462 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 2030 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1463 struct batadv_tt_common_entry *tt_common; 2031 struct batadv_tt_common_entry *tt_common;
1464 struct hlist_head *head; 2032 struct hlist_head *head;
1465 uint32_t i; 2033 uint32_t i, crc_tmp, crc = 0;
1466 int j; 2034 uint8_t flags;
1467 2035
1468 for (i = 0; i < hash->size; i++) { 2036 for (i = 0; i < hash->size; i++) {
1469 head = &hash->table[i]; 2037 head = &hash->table[i];
1470 2038
1471 rcu_read_lock(); 2039 rcu_read_lock();
1472 hlist_for_each_entry_rcu(tt_common, head, hash_entry) { 2040 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2041 /* compute the CRC only for entries belonging to the
2042 * VLAN identified by vid
2043 */
2044 if (tt_common->vid != vid)
2045 continue;
2046
1473 /* not yet committed clients have not to be taken into 2047 /* not yet committed clients have not to be taken into
1474 * account while computing the CRC 2048 * account while computing the CRC
1475 */ 2049 */
1476 if (tt_common->flags & BATADV_TT_CLIENT_NEW) 2050 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1477 continue; 2051 continue;
1478 total_one = 0; 2052
1479 for (j = 0; j < ETH_ALEN; j++) 2053 crc_tmp = crc32c(0, &tt_common->vid,
1480 total_one = crc16_byte(total_one, 2054 sizeof(tt_common->vid));
1481 tt_common->addr[j]); 2055
1482 total ^= total_one; 2056 /* compute the CRC on flags that have to be kept in sync
2057 * among nodes
2058 */
2059 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2060 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2061
2062 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
1483 } 2063 }
1484 rcu_read_unlock(); 2064 rcu_read_unlock();
1485 } 2065 }
1486 2066
1487 return total; 2067 return crc;
1488} 2068}
1489 2069
1490static void batadv_tt_req_list_free(struct batadv_priv *bat_priv) 2070static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
@@ -1503,11 +2083,9 @@ static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1503 2083
1504static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv, 2084static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1505 struct batadv_orig_node *orig_node, 2085 struct batadv_orig_node *orig_node,
1506 const unsigned char *tt_buff, 2086 const void *tt_buff,
1507 uint8_t tt_num_changes) 2087 uint16_t tt_buff_len)
1508{ 2088{
1509 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1510
1511 /* Replace the old buffer only if I received something in the 2089 /* Replace the old buffer only if I received something in the
1512 * last OGM (the OGM could carry no changes) 2090 * last OGM (the OGM could carry no changes)
1513 */ 2091 */
@@ -1569,9 +2147,14 @@ unlock:
1569 return tt_req_node; 2147 return tt_req_node;
1570} 2148}
1571 2149
1572/* data_ptr is useless here, but has to be kept to respect the prototype */ 2150/**
1573static int batadv_tt_local_valid_entry(const void *entry_ptr, 2151 * batadv_tt_local_valid - verify that given tt entry is a valid one
1574 const void *data_ptr) 2152 * @entry_ptr: to be checked local tt entry
2153 * @data_ptr: not used but definition required to satisfy the callback prototype
2154 *
2155 * Returns 1 if the entry is a valid, 0 otherwise.
2156 */
2157static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
1575{ 2158{
1576 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; 2159 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1577 2160
@@ -1598,41 +2181,30 @@ static int batadv_tt_global_valid(const void *entry_ptr,
1598 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node); 2181 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1599} 2182}
1600 2183
1601static struct sk_buff * 2184/**
1602batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, 2185 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
1603 struct batadv_hashtable *hash, 2186 * specified tt hash
1604 struct batadv_priv *bat_priv, 2187 * @bat_priv: the bat priv with all the soft interface information
1605 int (*valid_cb)(const void *, const void *), 2188 * @hash: hash table containing the tt entries
1606 void *cb_data) 2189 * @tt_len: expected tvlv tt data buffer length in number of bytes
2190 * @tvlv_buff: pointer to the buffer to fill with the TT data
2191 * @valid_cb: function to filter tt change entries
2192 * @cb_data: data passed to the filter function as argument
2193 */
2194static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2195 struct batadv_hashtable *hash,
2196 void *tvlv_buff, uint16_t tt_len,
2197 int (*valid_cb)(const void *, const void *),
2198 void *cb_data)
1607{ 2199{
1608 struct batadv_tt_common_entry *tt_common_entry; 2200 struct batadv_tt_common_entry *tt_common_entry;
1609 struct batadv_tt_query_packet *tt_response; 2201 struct batadv_tvlv_tt_change *tt_change;
1610 struct batadv_tt_change *tt_change;
1611 struct hlist_head *head; 2202 struct hlist_head *head;
1612 struct sk_buff *skb = NULL; 2203 uint16_t tt_tot, tt_num_entries = 0;
1613 uint16_t tt_tot, tt_count;
1614 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1615 uint32_t i; 2204 uint32_t i;
1616 size_t len;
1617
1618 if (tt_query_size + tt_len > bat_priv->soft_iface->mtu) {
1619 tt_len = bat_priv->soft_iface->mtu - tt_query_size;
1620 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1621 }
1622 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1623
1624 len = tt_query_size + tt_len;
1625 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
1626 if (!skb)
1627 goto out;
1628
1629 skb->priority = TC_PRIO_CONTROL;
1630 skb_reserve(skb, ETH_HLEN);
1631 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1632 tt_response->ttvn = ttvn;
1633 2205
1634 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size); 2206 tt_tot = batadv_tt_entries(tt_len);
1635 tt_count = 0; 2207 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
1636 2208
1637 rcu_read_lock(); 2209 rcu_read_lock();
1638 for (i = 0; i < hash->size; i++) { 2210 for (i = 0; i < hash->size; i++) {
@@ -1640,7 +2212,7 @@ batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1640 2212
1641 hlist_for_each_entry_rcu(tt_common_entry, 2213 hlist_for_each_entry_rcu(tt_common_entry,
1642 head, hash_entry) { 2214 head, hash_entry) {
1643 if (tt_count == tt_tot) 2215 if (tt_tot == tt_num_entries)
1644 break; 2216 break;
1645 2217
1646 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) 2218 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
@@ -1649,33 +2221,124 @@ batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1649 memcpy(tt_change->addr, tt_common_entry->addr, 2221 memcpy(tt_change->addr, tt_common_entry->addr,
1650 ETH_ALEN); 2222 ETH_ALEN);
1651 tt_change->flags = tt_common_entry->flags; 2223 tt_change->flags = tt_common_entry->flags;
2224 tt_change->vid = htons(tt_common_entry->vid);
2225 memset(tt_change->reserved, 0,
2226 sizeof(tt_change->reserved));
1652 2227
1653 tt_count++; 2228 tt_num_entries++;
1654 tt_change++; 2229 tt_change++;
1655 } 2230 }
1656 } 2231 }
1657 rcu_read_unlock(); 2232 rcu_read_unlock();
2233}
1658 2234
1659 /* store in the message the number of entries we have successfully 2235/**
1660 * copied 2236 * batadv_tt_global_check_crc - check if all the CRCs are correct
1661 */ 2237 * @orig_node: originator for which the CRCs have to be checked
1662 tt_response->tt_data = htons(tt_count); 2238 * @tt_vlan: pointer to the first tvlv VLAN entry
2239 * @num_vlan: number of tvlv VLAN entries
2240 * @create: if true, create VLAN objects if not found
2241 *
2242 * Return true if all the received CRCs match the locally stored ones, false
2243 * otherwise
2244 */
2245static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2246 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2247 uint16_t num_vlan)
2248{
2249 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2250 struct batadv_orig_node_vlan *vlan;
2251 int i;
1663 2252
1664out: 2253 /* check if each received CRC matches the locally stored one */
1665 return skb; 2254 for (i = 0; i < num_vlan; i++) {
2255 tt_vlan_tmp = tt_vlan + i;
2256
2257 /* if orig_node is a backbone node for this VLAN, don't check
2258 * the CRC as we ignore all the global entries over it
2259 */
2260 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2261 orig_node->orig,
2262 ntohs(tt_vlan_tmp->vid)))
2263 continue;
2264
2265 vlan = batadv_orig_node_vlan_get(orig_node,
2266 ntohs(tt_vlan_tmp->vid));
2267 if (!vlan)
2268 return false;
2269
2270 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2271 return false;
2272 }
2273
2274 return true;
2275}
2276
2277/**
2278 * batadv_tt_local_update_crc - update all the local CRCs
2279 * @bat_priv: the bat priv with all the soft interface information
2280 */
2281static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2282{
2283 struct batadv_softif_vlan *vlan;
2284
2285 /* recompute the global CRC for each VLAN */
2286 rcu_read_lock();
2287 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2288 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2289 }
2290 rcu_read_unlock();
1666} 2291}
1667 2292
2293/**
2294 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2295 * @bat_priv: the bat priv with all the soft interface information
2296 * @orig_node: the orig_node for which the CRCs have to be updated
2297 */
2298static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2299 struct batadv_orig_node *orig_node)
2300{
2301 struct batadv_orig_node_vlan *vlan;
2302 uint32_t crc;
2303
2304 /* recompute the global CRC for each VLAN */
2305 rcu_read_lock();
2306 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2307 /* if orig_node is a backbone node for this VLAN, don't compute
2308 * the CRC as we ignore all the global entries over it
2309 */
2310 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2311 vlan->vid))
2312 continue;
2313
2314 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2315 vlan->tt.crc = crc;
2316 }
2317 rcu_read_unlock();
2318}
2319
2320/**
2321 * batadv_send_tt_request - send a TT Request message to a given node
2322 * @bat_priv: the bat priv with all the soft interface information
2323 * @dst_orig_node: the destination of the message
2324 * @ttvn: the version number that the source of the message is looking for
2325 * @tt_vlan: pointer to the first tvlv VLAN object to request
2326 * @num_vlan: number of tvlv VLAN entries
2327 * @full_table: ask for the entire translation table if true, while only for the
2328 * last TT diff otherwise
2329 */
1668static int batadv_send_tt_request(struct batadv_priv *bat_priv, 2330static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1669 struct batadv_orig_node *dst_orig_node, 2331 struct batadv_orig_node *dst_orig_node,
1670 uint8_t ttvn, uint16_t tt_crc, 2332 uint8_t ttvn,
1671 bool full_table) 2333 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2334 uint16_t num_vlan, bool full_table)
1672{ 2335{
1673 struct sk_buff *skb = NULL; 2336 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1674 struct batadv_tt_query_packet *tt_request;
1675 struct batadv_hard_iface *primary_if;
1676 struct batadv_tt_req_node *tt_req_node = NULL; 2337 struct batadv_tt_req_node *tt_req_node = NULL;
1677 int ret = 1; 2338 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
1678 size_t tt_req_len; 2339 struct batadv_hard_iface *primary_if;
2340 bool ret = false;
2341 int i, size;
1679 2342
1680 primary_if = batadv_primary_if_get_selected(bat_priv); 2343 primary_if = batadv_primary_if_get_selected(bat_priv);
1681 if (!primary_if) 2344 if (!primary_if)
@@ -1688,157 +2351,171 @@ static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1688 if (!tt_req_node) 2351 if (!tt_req_node)
1689 goto out; 2352 goto out;
1690 2353
1691 skb = netdev_alloc_skb_ip_align(NULL, sizeof(*tt_request) + ETH_HLEN); 2354 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
1692 if (!skb) 2355 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2356 if (!tvlv_tt_data)
1693 goto out; 2357 goto out;
1694 2358
1695 skb->priority = TC_PRIO_CONTROL; 2359 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1696 skb_reserve(skb, ETH_HLEN); 2360 tvlv_tt_data->ttvn = ttvn;
2361 tvlv_tt_data->num_vlan = htons(num_vlan);
1697 2362
1698 tt_req_len = sizeof(*tt_request); 2363 /* send all the CRCs within the request. This is needed by intermediate
1699 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len); 2364 * nodes to ensure they have the correct table before replying
2365 */
2366 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2367 for (i = 0; i < num_vlan; i++) {
2368 tt_vlan_req->vid = tt_vlan->vid;
2369 tt_vlan_req->crc = tt_vlan->crc;
1700 2370
1701 tt_request->header.packet_type = BATADV_TT_QUERY; 2371 tt_vlan_req++;
1702 tt_request->header.version = BATADV_COMPAT_VERSION; 2372 tt_vlan++;
1703 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); 2373 }
1704 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1705 tt_request->header.ttl = BATADV_TTL;
1706 tt_request->ttvn = ttvn;
1707 tt_request->tt_data = htons(tt_crc);
1708 tt_request->flags = BATADV_TT_REQUEST;
1709 2374
1710 if (full_table) 2375 if (full_table)
1711 tt_request->flags |= BATADV_TT_FULL_TABLE; 2376 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1712 2377
1713 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n", 2378 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1714 dst_orig_node->orig, (full_table ? 'F' : '.')); 2379 dst_orig_node->orig, full_table ? 'F' : '.');
1715 2380
1716 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX); 2381 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1717 2382 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1718 if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL) != NET_XMIT_DROP) 2383 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1719 ret = 0; 2384 tvlv_tt_data, size);
2385 ret = true;
1720 2386
1721out: 2387out:
1722 if (primary_if) 2388 if (primary_if)
1723 batadv_hardif_free_ref(primary_if); 2389 batadv_hardif_free_ref(primary_if);
1724 if (ret)
1725 kfree_skb(skb);
1726 if (ret && tt_req_node) { 2390 if (ret && tt_req_node) {
1727 spin_lock_bh(&bat_priv->tt.req_list_lock); 2391 spin_lock_bh(&bat_priv->tt.req_list_lock);
1728 list_del(&tt_req_node->list); 2392 list_del(&tt_req_node->list);
1729 spin_unlock_bh(&bat_priv->tt.req_list_lock); 2393 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1730 kfree(tt_req_node); 2394 kfree(tt_req_node);
1731 } 2395 }
2396 kfree(tvlv_tt_data);
1732 return ret; 2397 return ret;
1733} 2398}
1734 2399
1735static bool 2400/**
1736batadv_send_other_tt_response(struct batadv_priv *bat_priv, 2401 * batadv_send_other_tt_response - send reply to tt request concerning another
1737 struct batadv_tt_query_packet *tt_request) 2402 * node's translation table
2403 * @bat_priv: the bat priv with all the soft interface information
2404 * @tt_data: tt data containing the tt request information
2405 * @req_src: mac address of tt request sender
2406 * @req_dst: mac address of tt request recipient
2407 *
2408 * Returns true if tt request reply was sent, false otherwise.
2409 */
2410static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2411 struct batadv_tvlv_tt_data *tt_data,
2412 uint8_t *req_src, uint8_t *req_dst)
1738{ 2413{
1739 struct batadv_orig_node *req_dst_orig_node; 2414 struct batadv_orig_node *req_dst_orig_node;
1740 struct batadv_orig_node *res_dst_orig_node = NULL; 2415 struct batadv_orig_node *res_dst_orig_node = NULL;
1741 uint8_t orig_ttvn, req_ttvn, ttvn; 2416 struct batadv_tvlv_tt_change *tt_change;
1742 int res, ret = false; 2417 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1743 unsigned char *tt_buff; 2418 struct batadv_tvlv_tt_vlan_data *tt_vlan;
1744 bool full_table; 2419 bool ret = false, full_table;
1745 uint16_t tt_len, tt_tot; 2420 uint8_t orig_ttvn, req_ttvn;
1746 struct sk_buff *skb = NULL; 2421 uint16_t tvlv_len;
1747 struct batadv_tt_query_packet *tt_response; 2422 int32_t tt_len;
1748 uint8_t *packet_pos;
1749 size_t len;
1750 2423
1751 batadv_dbg(BATADV_DBG_TT, bat_priv, 2424 batadv_dbg(BATADV_DBG_TT, bat_priv,
1752 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n", 2425 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1753 tt_request->src, tt_request->ttvn, tt_request->dst, 2426 req_src, tt_data->ttvn, req_dst,
1754 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 2427 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1755 2428
1756 /* Let's get the orig node of the REAL destination */ 2429 /* Let's get the orig node of the REAL destination */
1757 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst); 2430 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
1758 if (!req_dst_orig_node) 2431 if (!req_dst_orig_node)
1759 goto out; 2432 goto out;
1760 2433
1761 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 2434 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
1762 if (!res_dst_orig_node) 2435 if (!res_dst_orig_node)
1763 goto out; 2436 goto out;
1764 2437
1765 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 2438 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1766 req_ttvn = tt_request->ttvn; 2439 req_ttvn = tt_data->ttvn;
1767 2440
1768 /* I don't have the requested data */ 2441 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2442 /* this node doesn't have the requested data */
1769 if (orig_ttvn != req_ttvn || 2443 if (orig_ttvn != req_ttvn ||
1770 tt_request->tt_data != htons(req_dst_orig_node->tt_crc)) 2444 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2445 ntohs(tt_data->num_vlan)))
1771 goto out; 2446 goto out;
1772 2447
1773 /* If the full table has been explicitly requested */ 2448 /* If the full table has been explicitly requested */
1774 if (tt_request->flags & BATADV_TT_FULL_TABLE || 2449 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
1775 !req_dst_orig_node->tt_buff) 2450 !req_dst_orig_node->tt_buff)
1776 full_table = true; 2451 full_table = true;
1777 else 2452 else
1778 full_table = false; 2453 full_table = false;
1779 2454
1780 /* In this version, fragmentation is not implemented, then 2455 /* TT fragmentation hasn't been implemented yet, so send as many
1781 * I'll send only one packet with as much TT entries as I can 2456 * TT entries fit a single packet as possible only
1782 */ 2457 */
1783 if (!full_table) { 2458 if (!full_table) {
1784 spin_lock_bh(&req_dst_orig_node->tt_buff_lock); 2459 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1785 tt_len = req_dst_orig_node->tt_buff_len; 2460 tt_len = req_dst_orig_node->tt_buff_len;
1786 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1787 2461
1788 len = sizeof(*tt_response) + tt_len; 2462 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
1789 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN); 2463 &tvlv_tt_data,
1790 if (!skb) 2464 &tt_change,
2465 &tt_len);
2466 if (!tt_len)
1791 goto unlock; 2467 goto unlock;
1792 2468
1793 skb->priority = TC_PRIO_CONTROL;
1794 skb_reserve(skb, ETH_HLEN);
1795 packet_pos = skb_put(skb, len);
1796 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1797 tt_response->ttvn = req_ttvn;
1798 tt_response->tt_data = htons(tt_tot);
1799
1800 tt_buff = skb->data + sizeof(*tt_response);
1801 /* Copy the last orig_node's OGM buffer */ 2469 /* Copy the last orig_node's OGM buffer */
1802 memcpy(tt_buff, req_dst_orig_node->tt_buff, 2470 memcpy(tt_change, req_dst_orig_node->tt_buff,
1803 req_dst_orig_node->tt_buff_len); 2471 req_dst_orig_node->tt_buff_len);
1804
1805 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); 2472 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1806 } else { 2473 } else {
1807 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size); 2474 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
1808 tt_len *= sizeof(struct batadv_tt_change); 2475 * in the initial part
1809 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 2476 */
1810 2477 tt_len = -1;
1811 skb = batadv_tt_response_fill_table(tt_len, ttvn, 2478 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
1812 bat_priv->tt.global_hash, 2479 &tvlv_tt_data,
1813 bat_priv, 2480 &tt_change,
1814 batadv_tt_global_valid, 2481 &tt_len);
1815 req_dst_orig_node); 2482 if (!tt_len)
1816 if (!skb)
1817 goto out; 2483 goto out;
1818 2484
1819 tt_response = (struct batadv_tt_query_packet *)skb->data; 2485 /* fill the rest of the tvlv with the real TT entries */
2486 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2487 tt_change, tt_len,
2488 batadv_tt_global_valid,
2489 req_dst_orig_node);
1820 } 2490 }
1821 2491
1822 tt_response->header.packet_type = BATADV_TT_QUERY; 2492 /* Don't send the response, if larger than fragmented packet. */
1823 tt_response->header.version = BATADV_COMPAT_VERSION; 2493 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
1824 tt_response->header.ttl = BATADV_TTL; 2494 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
1825 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN); 2495 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
1826 memcpy(tt_response->dst, tt_request->src, ETH_ALEN); 2496 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
1827 tt_response->flags = BATADV_TT_RESPONSE; 2497 res_dst_orig_node->orig);
2498 goto out;
2499 }
2500
2501 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2502 tvlv_tt_data->ttvn = req_ttvn;
1828 2503
1829 if (full_table) 2504 if (full_table)
1830 tt_response->flags |= BATADV_TT_FULL_TABLE; 2505 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1831 2506
1832 batadv_dbg(BATADV_DBG_TT, bat_priv, 2507 batadv_dbg(BATADV_DBG_TT, bat_priv,
1833 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n", 2508 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1834 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn); 2509 res_dst_orig_node->orig, req_dst_orig_node->orig,
2510 full_table ? 'F' : '.', req_ttvn);
1835 2511
1836 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 2512 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1837 2513
1838 res = batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL); 2514 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1839 if (res != NET_XMIT_DROP) 2515 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
1840 ret = true; 2516 tvlv_len);
1841 2517
2518 ret = true;
1842 goto out; 2519 goto out;
1843 2520
1844unlock: 2521unlock:
@@ -1849,37 +2526,43 @@ out:
1849 batadv_orig_node_free_ref(res_dst_orig_node); 2526 batadv_orig_node_free_ref(res_dst_orig_node);
1850 if (req_dst_orig_node) 2527 if (req_dst_orig_node)
1851 batadv_orig_node_free_ref(req_dst_orig_node); 2528 batadv_orig_node_free_ref(req_dst_orig_node);
1852 if (!ret) 2529 kfree(tvlv_tt_data);
1853 kfree_skb(skb);
1854 return ret; 2530 return ret;
1855} 2531}
1856 2532
1857static bool 2533/**
1858batadv_send_my_tt_response(struct batadv_priv *bat_priv, 2534 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1859 struct batadv_tt_query_packet *tt_request) 2535 * translation table
2536 * @bat_priv: the bat priv with all the soft interface information
2537 * @tt_data: tt data containing the tt request information
2538 * @req_src: mac address of tt request sender
2539 *
2540 * Returns true if tt request reply was sent, false otherwise.
2541 */
2542static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2543 struct batadv_tvlv_tt_data *tt_data,
2544 uint8_t *req_src)
1860{ 2545{
1861 struct batadv_orig_node *orig_node; 2546 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1862 struct batadv_hard_iface *primary_if = NULL; 2547 struct batadv_hard_iface *primary_if = NULL;
1863 uint8_t my_ttvn, req_ttvn, ttvn; 2548 struct batadv_tvlv_tt_change *tt_change;
1864 int ret = false; 2549 struct batadv_orig_node *orig_node;
1865 unsigned char *tt_buff; 2550 uint8_t my_ttvn, req_ttvn;
2551 uint16_t tvlv_len;
1866 bool full_table; 2552 bool full_table;
1867 uint16_t tt_len, tt_tot; 2553 int32_t tt_len;
1868 struct sk_buff *skb = NULL;
1869 struct batadv_tt_query_packet *tt_response;
1870 uint8_t *packet_pos;
1871 size_t len;
1872 2554
1873 batadv_dbg(BATADV_DBG_TT, bat_priv, 2555 batadv_dbg(BATADV_DBG_TT, bat_priv,
1874 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n", 2556 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1875 tt_request->src, tt_request->ttvn, 2557 req_src, tt_data->ttvn,
1876 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 2558 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1877 2559
2560 spin_lock_bh(&bat_priv->tt.commit_lock);
1878 2561
1879 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 2562 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1880 req_ttvn = tt_request->ttvn; 2563 req_ttvn = tt_data->ttvn;
1881 2564
1882 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 2565 orig_node = batadv_orig_hash_find(bat_priv, req_src);
1883 if (!orig_node) 2566 if (!orig_node)
1884 goto out; 2567 goto out;
1885 2568
@@ -1890,103 +2573,104 @@ batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1890 /* If the full table has been explicitly requested or the gap 2573 /* If the full table has been explicitly requested or the gap
1891 * is too big send the whole local translation table 2574 * is too big send the whole local translation table
1892 */ 2575 */
1893 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn || 2576 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1894 !bat_priv->tt.last_changeset) 2577 !bat_priv->tt.last_changeset)
1895 full_table = true; 2578 full_table = true;
1896 else 2579 else
1897 full_table = false; 2580 full_table = false;
1898 2581
1899 /* In this version, fragmentation is not implemented, then 2582 /* TT fragmentation hasn't been implemented yet, so send as many
1900 * I'll send only one packet with as much TT entries as I can 2583 * TT entries fit a single packet as possible only
1901 */ 2584 */
1902 if (!full_table) { 2585 if (!full_table) {
1903 spin_lock_bh(&bat_priv->tt.last_changeset_lock); 2586 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1904 tt_len = bat_priv->tt.last_changeset_len;
1905 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1906 2587
1907 len = sizeof(*tt_response) + tt_len; 2588 tt_len = bat_priv->tt.last_changeset_len;
1908 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN); 2589 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
1909 if (!skb) 2590 &tvlv_tt_data,
2591 &tt_change,
2592 &tt_len);
2593 if (!tt_len)
1910 goto unlock; 2594 goto unlock;
1911 2595
1912 skb->priority = TC_PRIO_CONTROL; 2596 /* Copy the last orig_node's OGM buffer */
1913 skb_reserve(skb, ETH_HLEN); 2597 memcpy(tt_change, bat_priv->tt.last_changeset,
1914 packet_pos = skb_put(skb, len);
1915 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1916 tt_response->ttvn = req_ttvn;
1917 tt_response->tt_data = htons(tt_tot);
1918
1919 tt_buff = skb->data + sizeof(*tt_response);
1920 memcpy(tt_buff, bat_priv->tt.last_changeset,
1921 bat_priv->tt.last_changeset_len); 2598 bat_priv->tt.last_changeset_len);
1922 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 2599 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1923 } else { 2600 } else {
1924 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num); 2601 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1925 tt_len *= sizeof(struct batadv_tt_change); 2602
1926 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 2603 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
1927 2604 * in the initial part
1928 skb = batadv_tt_response_fill_table(tt_len, ttvn, 2605 */
1929 bat_priv->tt.local_hash, 2606 tt_len = -1;
1930 bat_priv, 2607 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
1931 batadv_tt_local_valid_entry, 2608 &tvlv_tt_data,
1932 NULL); 2609 &tt_change,
1933 if (!skb) 2610 &tt_len);
2611 if (!tt_len)
1934 goto out; 2612 goto out;
1935 2613
1936 tt_response = (struct batadv_tt_query_packet *)skb->data; 2614 /* fill the rest of the tvlv with the real TT entries */
2615 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2616 tt_change, tt_len,
2617 batadv_tt_local_valid, NULL);
1937 } 2618 }
1938 2619
1939 tt_response->header.packet_type = BATADV_TT_QUERY; 2620 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1940 tt_response->header.version = BATADV_COMPAT_VERSION; 2621 tvlv_tt_data->ttvn = req_ttvn;
1941 tt_response->header.ttl = BATADV_TTL;
1942 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1943 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1944 tt_response->flags = BATADV_TT_RESPONSE;
1945 2622
1946 if (full_table) 2623 if (full_table)
1947 tt_response->flags |= BATADV_TT_FULL_TABLE; 2624 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1948 2625
1949 batadv_dbg(BATADV_DBG_TT, bat_priv, 2626 batadv_dbg(BATADV_DBG_TT, bat_priv,
1950 "Sending TT_RESPONSE to %pM [%c]\n", 2627 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1951 orig_node->orig, 2628 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
1952 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1953 2629
1954 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 2630 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1955 2631
1956 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 2632 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1957 ret = true; 2633 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2634 tvlv_len);
2635
1958 goto out; 2636 goto out;
1959 2637
1960unlock: 2638unlock:
1961 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 2639 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1962out: 2640out:
2641 spin_unlock_bh(&bat_priv->tt.commit_lock);
1963 if (orig_node) 2642 if (orig_node)
1964 batadv_orig_node_free_ref(orig_node); 2643 batadv_orig_node_free_ref(orig_node);
1965 if (primary_if) 2644 if (primary_if)
1966 batadv_hardif_free_ref(primary_if); 2645 batadv_hardif_free_ref(primary_if);
1967 if (!ret) 2646 kfree(tvlv_tt_data);
1968 kfree_skb(skb); 2647 /* The packet was for this host, so it doesn't need to be re-routed */
1969 /* This packet was for me, so it doesn't need to be re-routed */
1970 return true; 2648 return true;
1971} 2649}
1972 2650
1973bool batadv_send_tt_response(struct batadv_priv *bat_priv, 2651/**
1974 struct batadv_tt_query_packet *tt_request) 2652 * batadv_send_tt_response - send reply to tt request
2653 * @bat_priv: the bat priv with all the soft interface information
2654 * @tt_data: tt data containing the tt request information
2655 * @req_src: mac address of tt request sender
2656 * @req_dst: mac address of tt request recipient
2657 *
2658 * Returns true if tt request reply was sent, false otherwise.
2659 */
2660static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2661 struct batadv_tvlv_tt_data *tt_data,
2662 uint8_t *req_src, uint8_t *req_dst)
1975{ 2663{
1976 if (batadv_is_my_mac(bat_priv, tt_request->dst)) { 2664 if (batadv_is_my_mac(bat_priv, req_dst))
1977 /* don't answer backbone gws! */ 2665 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
1978 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src)) 2666 else
1979 return true; 2667 return batadv_send_other_tt_response(bat_priv, tt_data,
1980 2668 req_src, req_dst);
1981 return batadv_send_my_tt_response(bat_priv, tt_request);
1982 } else {
1983 return batadv_send_other_tt_response(bat_priv, tt_request);
1984 }
1985} 2669}
1986 2670
1987static void _batadv_tt_update_changes(struct batadv_priv *bat_priv, 2671static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1988 struct batadv_orig_node *orig_node, 2672 struct batadv_orig_node *orig_node,
1989 struct batadv_tt_change *tt_change, 2673 struct batadv_tvlv_tt_change *tt_change,
1990 uint16_t tt_num_changes, uint8_t ttvn) 2674 uint16_t tt_num_changes, uint8_t ttvn)
1991{ 2675{
1992 int i; 2676 int i;
@@ -1997,11 +2681,13 @@ static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1997 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM; 2681 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1998 batadv_tt_global_del(bat_priv, orig_node, 2682 batadv_tt_global_del(bat_priv, orig_node,
1999 (tt_change + i)->addr, 2683 (tt_change + i)->addr,
2684 ntohs((tt_change + i)->vid),
2000 "tt removed by changes", 2685 "tt removed by changes",
2001 roams); 2686 roams);
2002 } else { 2687 } else {
2003 if (!batadv_tt_global_add(bat_priv, orig_node, 2688 if (!batadv_tt_global_add(bat_priv, orig_node,
2004 (tt_change + i)->addr, 2689 (tt_change + i)->addr,
2690 ntohs((tt_change + i)->vid),
2005 (tt_change + i)->flags, ttvn)) 2691 (tt_change + i)->flags, ttvn))
2006 /* In case of problem while storing a 2692 /* In case of problem while storing a
2007 * global_entry, we stop the updating 2693 * global_entry, we stop the updating
@@ -2016,21 +2702,22 @@ static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2016} 2702}
2017 2703
2018static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv, 2704static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2019 struct batadv_tt_query_packet *tt_response) 2705 struct batadv_tvlv_tt_change *tt_change,
2706 uint8_t ttvn, uint8_t *resp_src,
2707 uint16_t num_entries)
2020{ 2708{
2021 struct batadv_orig_node *orig_node; 2709 struct batadv_orig_node *orig_node;
2022 2710
2023 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 2711 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2024 if (!orig_node) 2712 if (!orig_node)
2025 goto out; 2713 goto out;
2026 2714
2027 /* Purge the old table first.. */ 2715 /* Purge the old table first.. */
2028 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table"); 2716 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2717 "Received full table");
2029 2718
2030 _batadv_tt_update_changes(bat_priv, orig_node, 2719 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2031 (struct batadv_tt_change *)(tt_response + 1), 2720 ttvn);
2032 ntohs(tt_response->tt_data),
2033 tt_response->ttvn);
2034 2721
2035 spin_lock_bh(&orig_node->tt_buff_lock); 2722 spin_lock_bh(&orig_node->tt_buff_lock);
2036 kfree(orig_node->tt_buff); 2723 kfree(orig_node->tt_buff);
@@ -2038,7 +2725,7 @@ static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2038 orig_node->tt_buff = NULL; 2725 orig_node->tt_buff = NULL;
2039 spin_unlock_bh(&orig_node->tt_buff_lock); 2726 spin_unlock_bh(&orig_node->tt_buff_lock);
2040 2727
2041 atomic_set(&orig_node->last_ttvn, tt_response->ttvn); 2728 atomic_set(&orig_node->last_ttvn, ttvn);
2042 2729
2043out: 2730out:
2044 if (orig_node) 2731 if (orig_node)
@@ -2048,22 +2735,31 @@ out:
2048static void batadv_tt_update_changes(struct batadv_priv *bat_priv, 2735static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2049 struct batadv_orig_node *orig_node, 2736 struct batadv_orig_node *orig_node,
2050 uint16_t tt_num_changes, uint8_t ttvn, 2737 uint16_t tt_num_changes, uint8_t ttvn,
2051 struct batadv_tt_change *tt_change) 2738 struct batadv_tvlv_tt_change *tt_change)
2052{ 2739{
2053 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, 2740 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2054 tt_num_changes, ttvn); 2741 tt_num_changes, ttvn);
2055 2742
2056 batadv_tt_save_orig_buffer(bat_priv, orig_node, 2743 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2057 (unsigned char *)tt_change, tt_num_changes); 2744 batadv_tt_len(tt_num_changes));
2058 atomic_set(&orig_node->last_ttvn, ttvn); 2745 atomic_set(&orig_node->last_ttvn, ttvn);
2059} 2746}
2060 2747
2061bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr) 2748/**
2749 * batadv_is_my_client - check if a client is served by the local node
2750 * @bat_priv: the bat priv with all the soft interface information
2751 * @addr: the mac adress of the client to check
2752 * @vid: VLAN identifier
2753 *
2754 * Returns true if the client is served by this node, false otherwise.
2755 */
2756bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2757 unsigned short vid)
2062{ 2758{
2063 struct batadv_tt_local_entry *tt_local_entry; 2759 struct batadv_tt_local_entry *tt_local_entry;
2064 bool ret = false; 2760 bool ret = false;
2065 2761
2066 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 2762 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2067 if (!tt_local_entry) 2763 if (!tt_local_entry)
2068 goto out; 2764 goto out;
2069 /* Check if the client has been logically deleted (but is kept for 2765 /* Check if the client has been logically deleted (but is kept for
@@ -2079,72 +2775,68 @@ out:
2079 return ret; 2775 return ret;
2080} 2776}
2081 2777
2082void batadv_handle_tt_response(struct batadv_priv *bat_priv, 2778/**
2083 struct batadv_tt_query_packet *tt_response) 2779 * batadv_handle_tt_response - process incoming tt reply
2780 * @bat_priv: the bat priv with all the soft interface information
2781 * @tt_data: tt data containing the tt request information
2782 * @resp_src: mac address of tt reply sender
2783 * @num_entries: number of tt change entries appended to the tt data
2784 */
2785static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2786 struct batadv_tvlv_tt_data *tt_data,
2787 uint8_t *resp_src, uint16_t num_entries)
2084{ 2788{
2085 struct batadv_tt_req_node *node, *safe; 2789 struct batadv_tt_req_node *node, *safe;
2086 struct batadv_orig_node *orig_node = NULL; 2790 struct batadv_orig_node *orig_node = NULL;
2087 struct batadv_tt_change *tt_change; 2791 struct batadv_tvlv_tt_change *tt_change;
2792 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2793 uint16_t change_offset;
2088 2794
2089 batadv_dbg(BATADV_DBG_TT, bat_priv, 2795 batadv_dbg(BATADV_DBG_TT, bat_priv,
2090 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n", 2796 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2091 tt_response->src, tt_response->ttvn, 2797 resp_src, tt_data->ttvn, num_entries,
2092 ntohs(tt_response->tt_data), 2798 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2093 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2094
2095 /* we should have never asked a backbone gw */
2096 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2097 goto out;
2098 2799
2099 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 2800 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2100 if (!orig_node) 2801 if (!orig_node)
2101 goto out; 2802 goto out;
2102 2803
2103 if (tt_response->flags & BATADV_TT_FULL_TABLE) { 2804 spin_lock_bh(&orig_node->tt_lock);
2104 batadv_tt_fill_gtable(bat_priv, tt_response); 2805
2806 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2807 change_offset *= ntohs(tt_data->num_vlan);
2808 change_offset += sizeof(*tt_data);
2809 tvlv_ptr += change_offset;
2810
2811 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2812 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2813 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2814 resp_src, num_entries);
2105 } else { 2815 } else {
2106 tt_change = (struct batadv_tt_change *)(tt_response + 1); 2816 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2107 batadv_tt_update_changes(bat_priv, orig_node, 2817 tt_data->ttvn, tt_change);
2108 ntohs(tt_response->tt_data),
2109 tt_response->ttvn, tt_change);
2110 } 2818 }
2111 2819
2820 /* Recalculate the CRC for this orig_node and store it */
2821 batadv_tt_global_update_crc(bat_priv, orig_node);
2822
2823 spin_unlock_bh(&orig_node->tt_lock);
2824
2112 /* Delete the tt_req_node from pending tt_requests list */ 2825 /* Delete the tt_req_node from pending tt_requests list */
2113 spin_lock_bh(&bat_priv->tt.req_list_lock); 2826 spin_lock_bh(&bat_priv->tt.req_list_lock);
2114 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) { 2827 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2115 if (!batadv_compare_eth(node->addr, tt_response->src)) 2828 if (!batadv_compare_eth(node->addr, resp_src))
2116 continue; 2829 continue;
2117 list_del(&node->list); 2830 list_del(&node->list);
2118 kfree(node); 2831 kfree(node);
2119 } 2832 }
2120 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2121 2833
2122 /* Recalculate the CRC for this orig_node and store it */ 2834 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2123 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2124out: 2835out:
2125 if (orig_node) 2836 if (orig_node)
2126 batadv_orig_node_free_ref(orig_node); 2837 batadv_orig_node_free_ref(orig_node);
2127} 2838}
2128 2839
2129int batadv_tt_init(struct batadv_priv *bat_priv)
2130{
2131 int ret;
2132
2133 ret = batadv_tt_local_init(bat_priv);
2134 if (ret < 0)
2135 return ret;
2136
2137 ret = batadv_tt_global_init(bat_priv);
2138 if (ret < 0)
2139 return ret;
2140
2141 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2142 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2143 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2144
2145 return 1;
2146}
2147
2148static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv) 2840static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2149{ 2841{
2150 struct batadv_tt_roam_node *node, *safe; 2842 struct batadv_tt_roam_node *node, *safe;
@@ -2225,14 +2917,28 @@ unlock:
2225 return ret; 2917 return ret;
2226} 2918}
2227 2919
2920/**
2921 * batadv_send_roam_adv - send a roaming advertisement message
2922 * @bat_priv: the bat priv with all the soft interface information
2923 * @client: mac address of the roaming client
2924 * @vid: VLAN identifier
2925 * @orig_node: message destination
2926 *
2927 * Send a ROAMING_ADV message to the node which was previously serving this
2928 * client. This is done to inform the node that from now on all traffic destined
2929 * for this particular roamed client has to be forwarded to the sender of the
2930 * roaming message.
2931 */
2228static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client, 2932static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2933 unsigned short vid,
2229 struct batadv_orig_node *orig_node) 2934 struct batadv_orig_node *orig_node)
2230{ 2935{
2231 struct sk_buff *skb = NULL;
2232 struct batadv_roam_adv_packet *roam_adv_packet;
2233 int ret = 1;
2234 struct batadv_hard_iface *primary_if; 2936 struct batadv_hard_iface *primary_if;
2235 size_t len = sizeof(*roam_adv_packet); 2937 struct batadv_tvlv_roam_adv tvlv_roam;
2938
2939 primary_if = batadv_primary_if_get_selected(bat_priv);
2940 if (!primary_if)
2941 goto out;
2236 2942
2237 /* before going on we have to check whether the client has 2943 /* before going on we have to check whether the client has
2238 * already roamed to us too many times 2944 * already roamed to us too many times
@@ -2240,40 +2946,22 @@ static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2240 if (!batadv_tt_check_roam_count(bat_priv, client)) 2946 if (!batadv_tt_check_roam_count(bat_priv, client))
2241 goto out; 2947 goto out;
2242 2948
2243 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
2244 if (!skb)
2245 goto out;
2246
2247 skb->priority = TC_PRIO_CONTROL;
2248 skb_reserve(skb, ETH_HLEN);
2249
2250 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2251
2252 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2253 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2254 roam_adv_packet->header.ttl = BATADV_TTL;
2255 roam_adv_packet->reserved = 0;
2256 primary_if = batadv_primary_if_get_selected(bat_priv);
2257 if (!primary_if)
2258 goto out;
2259 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2260 batadv_hardif_free_ref(primary_if);
2261 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2262 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2263
2264 batadv_dbg(BATADV_DBG_TT, bat_priv, 2949 batadv_dbg(BATADV_DBG_TT, bat_priv,
2265 "Sending ROAMING_ADV to %pM (client %pM)\n", 2950 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2266 orig_node->orig, client); 2951 orig_node->orig, client, BATADV_PRINT_VID(vid));
2267 2952
2268 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX); 2953 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2269 2954
2270 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 2955 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2271 ret = 0; 2956 tvlv_roam.vid = htons(vid);
2957
2958 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2959 orig_node->orig, BATADV_TVLV_ROAM, 1,
2960 &tvlv_roam, sizeof(tvlv_roam));
2272 2961
2273out: 2962out:
2274 if (ret && skb) 2963 if (primary_if)
2275 kfree_skb(skb); 2964 batadv_hardif_free_ref(primary_if);
2276 return;
2277} 2965}
2278 2966
2279static void batadv_tt_purge(struct work_struct *work) 2967static void batadv_tt_purge(struct work_struct *work)
@@ -2286,7 +2974,7 @@ static void batadv_tt_purge(struct work_struct *work)
2286 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work); 2974 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2287 bat_priv = container_of(priv_tt, struct batadv_priv, tt); 2975 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2288 2976
2289 batadv_tt_local_purge(bat_priv); 2977 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
2290 batadv_tt_global_purge(bat_priv); 2978 batadv_tt_global_purge(bat_priv);
2291 batadv_tt_req_purge(bat_priv); 2979 batadv_tt_req_purge(bat_priv);
2292 batadv_tt_roam_purge(bat_priv); 2980 batadv_tt_roam_purge(bat_priv);
@@ -2297,6 +2985,9 @@ static void batadv_tt_purge(struct work_struct *work)
2297 2985
2298void batadv_tt_free(struct batadv_priv *bat_priv) 2986void batadv_tt_free(struct batadv_priv *bat_priv)
2299{ 2987{
2988 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2989 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2990
2300 cancel_delayed_work_sync(&bat_priv->tt.work); 2991 cancel_delayed_work_sync(&bat_priv->tt.work);
2301 2992
2302 batadv_tt_local_table_free(bat_priv); 2993 batadv_tt_local_table_free(bat_priv);
@@ -2308,19 +2999,25 @@ void batadv_tt_free(struct batadv_priv *bat_priv)
2308 kfree(bat_priv->tt.last_changeset); 2999 kfree(bat_priv->tt.last_changeset);
2309} 3000}
2310 3001
2311/* This function will enable or disable the specified flags for all the entries 3002/**
2312 * in the given hash table and returns the number of modified entries 3003 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3004 * table and possibly count them in the TT size
3005 * @bat_priv: the bat priv with all the soft interface information
3006 * @flags: the flag to switch
3007 * @enable: whether to set or unset the flag
3008 * @count: whether to increase the TT size by the number of changed entries
2313 */ 3009 */
2314static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash, 3010static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2315 uint16_t flags, bool enable) 3011 uint16_t flags, bool enable, bool count)
2316{ 3012{
2317 uint32_t i; 3013 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3014 struct batadv_tt_common_entry *tt_common_entry;
2318 uint16_t changed_num = 0; 3015 uint16_t changed_num = 0;
2319 struct hlist_head *head; 3016 struct hlist_head *head;
2320 struct batadv_tt_common_entry *tt_common_entry; 3017 uint32_t i;
2321 3018
2322 if (!hash) 3019 if (!hash)
2323 goto out; 3020 return;
2324 3021
2325 for (i = 0; i < hash->size; i++) { 3022 for (i = 0; i < hash->size; i++) {
2326 head = &hash->table[i]; 3023 head = &hash->table[i];
@@ -2338,11 +3035,15 @@ static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2338 tt_common_entry->flags &= ~flags; 3035 tt_common_entry->flags &= ~flags;
2339 } 3036 }
2340 changed_num++; 3037 changed_num++;
3038
3039 if (!count)
3040 continue;
3041
3042 batadv_tt_local_size_inc(bat_priv,
3043 tt_common_entry->vid);
2341 } 3044 }
2342 rcu_read_unlock(); 3045 rcu_read_unlock();
2343 } 3046 }
2344out:
2345 return changed_num;
2346} 3047}
2347 3048
2348/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */ 3049/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
@@ -2370,10 +3071,11 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2370 continue; 3071 continue;
2371 3072
2372 batadv_dbg(BATADV_DBG_TT, bat_priv, 3073 batadv_dbg(BATADV_DBG_TT, bat_priv,
2373 "Deleting local tt entry (%pM): pending\n", 3074 "Deleting local tt entry (%pM, vid: %d): pending\n",
2374 tt_common->addr); 3075 tt_common->addr,
3076 BATADV_PRINT_VID(tt_common->vid));
2375 3077
2376 atomic_dec(&bat_priv->tt.local_entry_num); 3078 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
2377 hlist_del_rcu(&tt_common->hash_entry); 3079 hlist_del_rcu(&tt_common->hash_entry);
2378 tt_local = container_of(tt_common, 3080 tt_local = container_of(tt_common,
2379 struct batadv_tt_local_entry, 3081 struct batadv_tt_local_entry,
@@ -2384,22 +3086,25 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2384 } 3086 }
2385} 3087}
2386 3088
2387static int batadv_tt_commit_changes(struct batadv_priv *bat_priv, 3089/**
2388 unsigned char **packet_buff, 3090 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
2389 int *packet_buff_len, int packet_min_len) 3091 * which have been queued in the time since the last commit
3092 * @bat_priv: the bat priv with all the soft interface information
3093 *
3094 * Caller must hold tt->commit_lock.
3095 */
3096static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
2390{ 3097{
2391 uint16_t changed_num = 0; 3098 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2392 3099 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2393 if (atomic_read(&bat_priv->tt.local_changes) < 1) 3100 batadv_tt_tvlv_container_update(bat_priv);
2394 return -ENOENT; 3101 return;
3102 }
2395 3103
2396 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash, 3104 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
2397 BATADV_TT_CLIENT_NEW, false);
2398 3105
2399 /* all reset entries have to be counted as local entries */
2400 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2401 batadv_tt_local_purge_pending_clients(bat_priv); 3106 batadv_tt_local_purge_pending_clients(bat_priv);
2402 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv); 3107 batadv_tt_local_update_crc(bat_priv);
2403 3108
2404 /* Increment the TTVN only once per OGM interval */ 3109 /* Increment the TTVN only once per OGM interval */
2405 atomic_inc(&bat_priv->tt.vn); 3110 atomic_inc(&bat_priv->tt.vn);
@@ -2409,49 +3114,38 @@ static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2409 3114
2410 /* reset the sending counter */ 3115 /* reset the sending counter */
2411 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX); 3116 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2412 3117 batadv_tt_tvlv_container_update(bat_priv);
2413 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2414 packet_buff_len, packet_min_len);
2415} 3118}
2416 3119
2417/* when calling this function (hard_iface == primary_if) has to be true */ 3120/**
2418int batadv_tt_append_diff(struct batadv_priv *bat_priv, 3121 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2419 unsigned char **packet_buff, int *packet_buff_len, 3122 * have been queued in the time since the last commit
2420 int packet_min_len) 3123 * @bat_priv: the bat priv with all the soft interface information
3124 */
3125void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
2421{ 3126{
2422 int tt_num_changes; 3127 spin_lock_bh(&bat_priv->tt.commit_lock);
2423 3128 batadv_tt_local_commit_changes_nolock(bat_priv);
2424 /* if at least one change happened */ 3129 spin_unlock_bh(&bat_priv->tt.commit_lock);
2425 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2426 packet_buff_len,
2427 packet_min_len);
2428
2429 /* if the changes have been sent often enough */
2430 if ((tt_num_changes < 0) &&
2431 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2432 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2433 packet_min_len, packet_min_len);
2434 tt_num_changes = 0;
2435 }
2436
2437 return tt_num_changes;
2438} 3130}
2439 3131
2440bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src, 3132bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2441 uint8_t *dst) 3133 uint8_t *dst, unsigned short vid)
2442{ 3134{
2443 struct batadv_tt_local_entry *tt_local_entry = NULL; 3135 struct batadv_tt_local_entry *tt_local_entry = NULL;
2444 struct batadv_tt_global_entry *tt_global_entry = NULL; 3136 struct batadv_tt_global_entry *tt_global_entry = NULL;
3137 struct batadv_softif_vlan *vlan;
2445 bool ret = false; 3138 bool ret = false;
2446 3139
2447 if (!atomic_read(&bat_priv->ap_isolation)) 3140 vlan = batadv_softif_vlan_get(bat_priv, vid);
3141 if (!vlan || !atomic_read(&vlan->ap_isolation))
2448 goto out; 3142 goto out;
2449 3143
2450 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst); 3144 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
2451 if (!tt_local_entry) 3145 if (!tt_local_entry)
2452 goto out; 3146 goto out;
2453 3147
2454 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src); 3148 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
2455 if (!tt_global_entry) 3149 if (!tt_global_entry)
2456 goto out; 3150 goto out;
2457 3151
@@ -2461,6 +3155,8 @@ bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2461 ret = true; 3155 ret = true;
2462 3156
2463out: 3157out:
3158 if (vlan)
3159 batadv_softif_vlan_free_ref(vlan);
2464 if (tt_global_entry) 3160 if (tt_global_entry)
2465 batadv_tt_global_entry_free_ref(tt_global_entry); 3161 batadv_tt_global_entry_free_ref(tt_global_entry);
2466 if (tt_local_entry) 3162 if (tt_local_entry)
@@ -2468,19 +3164,29 @@ out:
2468 return ret; 3164 return ret;
2469} 3165}
2470 3166
2471void batadv_tt_update_orig(struct batadv_priv *bat_priv, 3167/**
2472 struct batadv_orig_node *orig_node, 3168 * batadv_tt_update_orig - update global translation table with new tt
2473 const unsigned char *tt_buff, uint8_t tt_num_changes, 3169 * information received via ogms
2474 uint8_t ttvn, uint16_t tt_crc) 3170 * @bat_priv: the bat priv with all the soft interface information
3171 * @orig: the orig_node of the ogm
3172 * @tt_vlan: pointer to the first tvlv VLAN entry
3173 * @tt_num_vlan: number of tvlv VLAN entries
3174 * @tt_change: pointer to the first entry in the TT buffer
3175 * @tt_num_changes: number of tt changes inside the tt buffer
3176 * @ttvn: translation table version number of this changeset
3177 * @tt_crc: crc32 checksum of orig node's translation table
3178 */
3179static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3180 struct batadv_orig_node *orig_node,
3181 const void *tt_buff, uint16_t tt_num_vlan,
3182 struct batadv_tvlv_tt_change *tt_change,
3183 uint16_t tt_num_changes, uint8_t ttvn)
2475{ 3184{
2476 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); 3185 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
3186 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2477 bool full_table = true; 3187 bool full_table = true;
2478 struct batadv_tt_change *tt_change;
2479
2480 /* don't care about a backbone gateways updates. */
2481 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2482 return;
2483 3188
3189 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
2484 /* orig table not initialised AND first diff is in the OGM OR the ttvn 3190 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2485 * increased by one -> we can apply the attached changes 3191 * increased by one -> we can apply the attached changes
2486 */ 3192 */
@@ -2496,7 +3202,9 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2496 goto request_table; 3202 goto request_table;
2497 } 3203 }
2498 3204
2499 tt_change = (struct batadv_tt_change *)tt_buff; 3205 spin_lock_bh(&orig_node->tt_lock);
3206
3207 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
2500 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes, 3208 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2501 ttvn, tt_change); 3209 ttvn, tt_change);
2502 3210
@@ -2504,7 +3212,9 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2504 * prefer to recompute it to spot any possible inconsistency 3212 * prefer to recompute it to spot any possible inconsistency
2505 * in the global table 3213 * in the global table
2506 */ 3214 */
2507 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node); 3215 batadv_tt_global_update_crc(bat_priv, orig_node);
3216
3217 spin_unlock_bh(&orig_node->tt_lock);
2508 3218
2509 /* The ttvn alone is not enough to guarantee consistency 3219 /* The ttvn alone is not enough to guarantee consistency
2510 * because a single value could represent different states 3220 * because a single value could represent different states
@@ -2515,37 +3225,46 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2515 * checking the CRC value is mandatory to detect the 3225 * checking the CRC value is mandatory to detect the
2516 * inconsistency 3226 * inconsistency
2517 */ 3227 */
2518 if (orig_node->tt_crc != tt_crc) 3228 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3229 tt_num_vlan))
2519 goto request_table; 3230 goto request_table;
2520 } else { 3231 } else {
2521 /* if we missed more than one change or our tables are not 3232 /* if we missed more than one change or our tables are not
2522 * in sync anymore -> request fresh tt data 3233 * in sync anymore -> request fresh tt data
2523 */ 3234 */
2524 if (!orig_node->tt_initialised || ttvn != orig_ttvn || 3235 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2525 orig_node->tt_crc != tt_crc) { 3236 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3237 tt_num_vlan)) {
2526request_table: 3238request_table:
2527 batadv_dbg(BATADV_DBG_TT, bat_priv, 3239 batadv_dbg(BATADV_DBG_TT, bat_priv,
2528 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n", 3240 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
2529 orig_node->orig, ttvn, orig_ttvn, tt_crc, 3241 orig_node->orig, ttvn, orig_ttvn,
2530 orig_node->tt_crc, tt_num_changes); 3242 tt_num_changes);
2531 batadv_send_tt_request(bat_priv, orig_node, ttvn, 3243 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2532 tt_crc, full_table); 3244 tt_vlan, tt_num_vlan,
3245 full_table);
2533 return; 3246 return;
2534 } 3247 }
2535 } 3248 }
2536} 3249}
2537 3250
2538/* returns true whether we know that the client has moved from its old 3251/**
2539 * originator to another one. This entry is kept is still kept for consistency 3252 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
2540 * purposes 3253 * @bat_priv: the bat priv with all the soft interface information
3254 * @addr: the mac address of the client to check
3255 * @vid: VLAN identifier
3256 *
3257 * Returns true if we know that the client has moved from its old originator
3258 * to another one. This entry is still kept for consistency purposes and will be
3259 * deleted later by a DEL or because of timeout
2541 */ 3260 */
2542bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv, 3261bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2543 uint8_t *addr) 3262 uint8_t *addr, unsigned short vid)
2544{ 3263{
2545 struct batadv_tt_global_entry *tt_global_entry; 3264 struct batadv_tt_global_entry *tt_global_entry;
2546 bool ret = false; 3265 bool ret = false;
2547 3266
2548 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 3267 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2549 if (!tt_global_entry) 3268 if (!tt_global_entry)
2550 goto out; 3269 goto out;
2551 3270
@@ -2558,19 +3277,20 @@ out:
2558/** 3277/**
2559 * batadv_tt_local_client_is_roaming - tells whether the client is roaming 3278 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2560 * @bat_priv: the bat priv with all the soft interface information 3279 * @bat_priv: the bat priv with all the soft interface information
2561 * @addr: the MAC address of the local client to query 3280 * @addr: the mac address of the local client to query
3281 * @vid: VLAN identifier
2562 * 3282 *
2563 * Returns true if the local client is known to be roaming (it is not served by 3283 * Returns true if the local client is known to be roaming (it is not served by
2564 * this node anymore) or not. If yes, the client is still present in the table 3284 * this node anymore) or not. If yes, the client is still present in the table
2565 * to keep the latter consistent with the node TTVN 3285 * to keep the latter consistent with the node TTVN
2566 */ 3286 */
2567bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv, 3287bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2568 uint8_t *addr) 3288 uint8_t *addr, unsigned short vid)
2569{ 3289{
2570 struct batadv_tt_local_entry *tt_local_entry; 3290 struct batadv_tt_local_entry *tt_local_entry;
2571 bool ret = false; 3291 bool ret = false;
2572 3292
2573 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 3293 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2574 if (!tt_local_entry) 3294 if (!tt_local_entry)
2575 goto out; 3295 goto out;
2576 3296
@@ -2582,26 +3302,268 @@ out:
2582 3302
2583bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv, 3303bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2584 struct batadv_orig_node *orig_node, 3304 struct batadv_orig_node *orig_node,
2585 const unsigned char *addr) 3305 const unsigned char *addr,
3306 unsigned short vid)
2586{ 3307{
2587 bool ret = false; 3308 bool ret = false;
2588 3309
2589 /* if the originator is a backbone node (meaning it belongs to the same 3310 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
2590 * LAN of this node) the temporary client must not be added because to
2591 * reach such destination the node must use the LAN instead of the mesh
2592 */
2593 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2594 goto out;
2595
2596 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2597 BATADV_TT_CLIENT_TEMP, 3311 BATADV_TT_CLIENT_TEMP,
2598 atomic_read(&orig_node->last_ttvn))) 3312 atomic_read(&orig_node->last_ttvn)))
2599 goto out; 3313 goto out;
2600 3314
2601 batadv_dbg(BATADV_DBG_TT, bat_priv, 3315 batadv_dbg(BATADV_DBG_TT, bat_priv,
2602 "Added temporary global client (addr: %pM orig: %pM)\n", 3316 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
2603 addr, orig_node->orig); 3317 addr, BATADV_PRINT_VID(vid), orig_node->orig);
2604 ret = true; 3318 ret = true;
2605out: 3319out:
2606 return ret; 3320 return ret;
2607} 3321}
3322
3323/**
3324 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3325 * maximum packet size that can be transported through the mesh
3326 * @soft_iface: netdev struct of the mesh interface
3327 *
3328 * Remove entries older than 'timeout' and half timeout if more entries need
3329 * to be removed.
3330 */
3331void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3332{
3333 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3334 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3335 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3336 bool reduced = false;
3337
3338 spin_lock_bh(&bat_priv->tt.commit_lock);
3339
3340 while (true) {
3341 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3342 if (packet_size_max >= table_size)
3343 break;
3344
3345 batadv_tt_local_purge(bat_priv, timeout);
3346 batadv_tt_local_purge_pending_clients(bat_priv);
3347
3348 timeout /= 2;
3349 reduced = true;
3350 net_ratelimited_function(batadv_info, soft_iface,
3351 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3352 packet_size_max);
3353 }
3354
3355 /* commit these changes immediately, to avoid synchronization problem
3356 * with the TTVN
3357 */
3358 if (reduced)
3359 batadv_tt_local_commit_changes_nolock(bat_priv);
3360
3361 spin_unlock_bh(&bat_priv->tt.commit_lock);
3362}
3363
3364/**
3365 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3366 * @bat_priv: the bat priv with all the soft interface information
3367 * @orig: the orig_node of the ogm
3368 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3369 * @tvlv_value: tvlv buffer containing the gateway data
3370 * @tvlv_value_len: tvlv buffer length
3371 */
3372static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3373 struct batadv_orig_node *orig,
3374 uint8_t flags, void *tvlv_value,
3375 uint16_t tvlv_value_len)
3376{
3377 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3378 struct batadv_tvlv_tt_change *tt_change;
3379 struct batadv_tvlv_tt_data *tt_data;
3380 uint16_t num_entries, num_vlan;
3381
3382 if (tvlv_value_len < sizeof(*tt_data))
3383 return;
3384
3385 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3386 tvlv_value_len -= sizeof(*tt_data);
3387
3388 num_vlan = ntohs(tt_data->num_vlan);
3389
3390 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3391 return;
3392
3393 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3394 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3395 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3396
3397 num_entries = batadv_tt_entries(tvlv_value_len);
3398
3399 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3400 num_entries, tt_data->ttvn);
3401}
3402
3403/**
3404 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3405 * container
3406 * @bat_priv: the bat priv with all the soft interface information
3407 * @src: mac address of tt tvlv sender
3408 * @dst: mac address of tt tvlv recipient
3409 * @tvlv_value: tvlv buffer containing the tt data
3410 * @tvlv_value_len: tvlv buffer length
3411 *
3412 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3413 * otherwise.
3414 */
3415static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3416 uint8_t *src, uint8_t *dst,
3417 void *tvlv_value,
3418 uint16_t tvlv_value_len)
3419{
3420 struct batadv_tvlv_tt_data *tt_data;
3421 uint16_t tt_vlan_len, tt_num_entries;
3422 char tt_flag;
3423 bool ret;
3424
3425 if (tvlv_value_len < sizeof(*tt_data))
3426 return NET_RX_SUCCESS;
3427
3428 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3429 tvlv_value_len -= sizeof(*tt_data);
3430
3431 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3432 tt_vlan_len *= ntohs(tt_data->num_vlan);
3433
3434 if (tvlv_value_len < tt_vlan_len)
3435 return NET_RX_SUCCESS;
3436
3437 tvlv_value_len -= tt_vlan_len;
3438 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3439
3440 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3441 case BATADV_TT_REQUEST:
3442 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3443
3444 /* If this node cannot provide a TT response the tt_request is
3445 * forwarded
3446 */
3447 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3448 if (!ret) {
3449 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3450 tt_flag = 'F';
3451 else
3452 tt_flag = '.';
3453
3454 batadv_dbg(BATADV_DBG_TT, bat_priv,
3455 "Routing TT_REQUEST to %pM [%c]\n",
3456 dst, tt_flag);
3457 /* tvlv API will re-route the packet */
3458 return NET_RX_DROP;
3459 }
3460 break;
3461 case BATADV_TT_RESPONSE:
3462 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3463
3464 if (batadv_is_my_mac(bat_priv, dst)) {
3465 batadv_handle_tt_response(bat_priv, tt_data,
3466 src, tt_num_entries);
3467 return NET_RX_SUCCESS;
3468 }
3469
3470 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3471 tt_flag = 'F';
3472 else
3473 tt_flag = '.';
3474
3475 batadv_dbg(BATADV_DBG_TT, bat_priv,
3476 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3477
3478 /* tvlv API will re-route the packet */
3479 return NET_RX_DROP;
3480 }
3481
3482 return NET_RX_SUCCESS;
3483}
3484
3485/**
3486 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3487 * @bat_priv: the bat priv with all the soft interface information
3488 * @src: mac address of tt tvlv sender
3489 * @dst: mac address of tt tvlv recipient
3490 * @tvlv_value: tvlv buffer containing the tt data
3491 * @tvlv_value_len: tvlv buffer length
3492 *
3493 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3494 * otherwise.
3495 */
3496static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3497 uint8_t *src, uint8_t *dst,
3498 void *tvlv_value,
3499 uint16_t tvlv_value_len)
3500{
3501 struct batadv_tvlv_roam_adv *roaming_adv;
3502 struct batadv_orig_node *orig_node = NULL;
3503
3504 /* If this node is not the intended recipient of the
3505 * roaming advertisement the packet is forwarded
3506 * (the tvlv API will re-route the packet).
3507 */
3508 if (!batadv_is_my_mac(bat_priv, dst))
3509 return NET_RX_DROP;
3510
3511 if (tvlv_value_len < sizeof(*roaming_adv))
3512 goto out;
3513
3514 orig_node = batadv_orig_hash_find(bat_priv, src);
3515 if (!orig_node)
3516 goto out;
3517
3518 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3519 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3520
3521 batadv_dbg(BATADV_DBG_TT, bat_priv,
3522 "Received ROAMING_ADV from %pM (client %pM)\n",
3523 src, roaming_adv->client);
3524
3525 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3526 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3527 atomic_read(&orig_node->last_ttvn) + 1);
3528
3529out:
3530 if (orig_node)
3531 batadv_orig_node_free_ref(orig_node);
3532 return NET_RX_SUCCESS;
3533}
3534
3535/**
3536 * batadv_tt_init - initialise the translation table internals
3537 * @bat_priv: the bat priv with all the soft interface information
3538 *
3539 * Return 0 on success or negative error number in case of failure.
3540 */
3541int batadv_tt_init(struct batadv_priv *bat_priv)
3542{
3543 int ret;
3544
3545 /* synchronized flags must be remote */
3546 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3547
3548 ret = batadv_tt_local_init(bat_priv);
3549 if (ret < 0)
3550 return ret;
3551
3552 ret = batadv_tt_global_init(bat_priv);
3553 if (ret < 0)
3554 return ret;
3555
3556 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3557 batadv_tt_tvlv_unicast_handler_v1,
3558 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3559
3560 batadv_tvlv_handler_register(bat_priv, NULL,
3561 batadv_roam_tvlv_unicast_handler_v1,
3562 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3563
3564 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3565 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3566 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3567
3568 return 1;
3569}
diff --git a/net/batman-adv/translation-table.h b/net/batman-adv/translation-table.h
index 659a3bb759ce..026b1ffa6746 100644
--- a/net/batman-adv/translation-table.h
+++ b/net/batman-adv/translation-table.h
@@ -20,49 +20,35 @@
20#ifndef _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ 20#ifndef _NET_BATMAN_ADV_TRANSLATION_TABLE_H_
21#define _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ 21#define _NET_BATMAN_ADV_TRANSLATION_TABLE_H_
22 22
23int batadv_tt_len(int changes_num);
24int batadv_tt_init(struct batadv_priv *bat_priv); 23int batadv_tt_init(struct batadv_priv *bat_priv);
25void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr, 24bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
26 int ifindex); 25 unsigned short vid, int ifindex);
27uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv, 26uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
28 const uint8_t *addr, const char *message, 27 const uint8_t *addr, unsigned short vid,
29 bool roaming); 28 const char *message, bool roaming);
30int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset); 29int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset);
31void batadv_tt_global_add_orig(struct batadv_priv *bat_priv,
32 struct batadv_orig_node *orig_node,
33 const unsigned char *tt_buff, int tt_buff_len);
34int batadv_tt_global_add(struct batadv_priv *bat_priv,
35 struct batadv_orig_node *orig_node,
36 const unsigned char *addr, uint16_t flags,
37 uint8_t ttvn);
38int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset); 30int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset);
39void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, 31void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
40 struct batadv_orig_node *orig_node, 32 struct batadv_orig_node *orig_node,
41 const char *message); 33 int32_t match_vid, const char *message);
42struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, 34struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
43 const uint8_t *src, 35 const uint8_t *src,
44 const uint8_t *addr); 36 const uint8_t *addr,
37 unsigned short vid);
45void batadv_tt_free(struct batadv_priv *bat_priv); 38void batadv_tt_free(struct batadv_priv *bat_priv);
46bool batadv_send_tt_response(struct batadv_priv *bat_priv, 39bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
47 struct batadv_tt_query_packet *tt_request); 40 unsigned short vid);
48bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr);
49void batadv_handle_tt_response(struct batadv_priv *bat_priv,
50 struct batadv_tt_query_packet *tt_response);
51bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src, 41bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
52 uint8_t *dst); 42 uint8_t *dst, unsigned short vid);
53void batadv_tt_update_orig(struct batadv_priv *bat_priv, 43void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv);
54 struct batadv_orig_node *orig_node,
55 const unsigned char *tt_buff, uint8_t tt_num_changes,
56 uint8_t ttvn, uint16_t tt_crc);
57int batadv_tt_append_diff(struct batadv_priv *bat_priv,
58 unsigned char **packet_buff, int *packet_buff_len,
59 int packet_min_len);
60bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv, 44bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
61 uint8_t *addr); 45 uint8_t *addr, unsigned short vid);
62bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv, 46bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
63 uint8_t *addr); 47 uint8_t *addr, unsigned short vid);
48void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface);
64bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv, 49bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
65 struct batadv_orig_node *orig_node, 50 struct batadv_orig_node *orig_node,
66 const unsigned char *addr); 51 const unsigned char *addr,
52 unsigned short vid);
67 53
68#endif /* _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ */ 54#endif /* _NET_BATMAN_ADV_TRANSLATION_TABLE_H_ */
diff --git a/net/batman-adv/types.h b/net/batman-adv/types.h
index b2c94e139319..91dd369b0ff2 100644
--- a/net/batman-adv/types.h
+++ b/net/batman-adv/types.h
@@ -24,13 +24,6 @@
24#include "bitarray.h" 24#include "bitarray.h"
25#include <linux/kernel.h> 25#include <linux/kernel.h>
26 26
27/**
28 * Maximum overhead for the encapsulation for a payload packet
29 */
30#define BATADV_HEADER_LEN \
31 (ETH_HLEN + max(sizeof(struct batadv_unicast_packet), \
32 sizeof(struct batadv_bcast_packet)))
33
34#ifdef CONFIG_BATMAN_ADV_DAT 27#ifdef CONFIG_BATMAN_ADV_DAT
35 28
36/* batadv_dat_addr_t is the type used for all DHT addresses. If it is changed, 29/* batadv_dat_addr_t is the type used for all DHT addresses. If it is changed,
@@ -43,6 +36,18 @@
43#endif /* CONFIG_BATMAN_ADV_DAT */ 36#endif /* CONFIG_BATMAN_ADV_DAT */
44 37
45/** 38/**
39 * BATADV_TT_REMOTE_MASK - bitmask selecting the flags that are sent over the
40 * wire only
41 */
42#define BATADV_TT_REMOTE_MASK 0x00FF
43
44/**
45 * BATADV_TT_SYNC_MASK - bitmask of the flags that need to be kept in sync
46 * among the nodes. These flags are used to compute the global/local CRC
47 */
48#define BATADV_TT_SYNC_MASK 0x00F0
49
50/**
46 * struct batadv_hard_iface_bat_iv - per hard interface B.A.T.M.A.N. IV data 51 * struct batadv_hard_iface_bat_iv - per hard interface B.A.T.M.A.N. IV data
47 * @ogm_buff: buffer holding the OGM packet 52 * @ogm_buff: buffer holding the OGM packet
48 * @ogm_buff_len: length of the OGM packet buffer 53 * @ogm_buff_len: length of the OGM packet buffer
@@ -60,7 +65,6 @@ struct batadv_hard_iface_bat_iv {
60 * @if_num: identificator of the interface 65 * @if_num: identificator of the interface
61 * @if_status: status of the interface for batman-adv 66 * @if_status: status of the interface for batman-adv
62 * @net_dev: pointer to the net_device 67 * @net_dev: pointer to the net_device
63 * @frag_seqno: last fragment sequence number sent by this interface
64 * @num_bcasts: number of payload re-broadcasts on this interface (ARQ) 68 * @num_bcasts: number of payload re-broadcasts on this interface (ARQ)
65 * @hardif_obj: kobject of the per interface sysfs "mesh" directory 69 * @hardif_obj: kobject of the per interface sysfs "mesh" directory
66 * @refcount: number of contexts the object is used 70 * @refcount: number of contexts the object is used
@@ -76,7 +80,6 @@ struct batadv_hard_iface {
76 int16_t if_num; 80 int16_t if_num;
77 char if_status; 81 char if_status;
78 struct net_device *net_dev; 82 struct net_device *net_dev;
79 atomic_t frag_seqno;
80 uint8_t num_bcasts; 83 uint8_t num_bcasts;
81 struct kobject *hardif_obj; 84 struct kobject *hardif_obj;
82 atomic_t refcount; 85 atomic_t refcount;
@@ -88,28 +91,97 @@ struct batadv_hard_iface {
88}; 91};
89 92
90/** 93/**
94 * struct batadv_frag_table_entry - head in the fragment buffer table
95 * @head: head of list with fragments
96 * @lock: lock to protect the list of fragments
97 * @timestamp: time (jiffie) of last received fragment
98 * @seqno: sequence number of the fragments in the list
99 * @size: accumulated size of packets in list
100 */
101struct batadv_frag_table_entry {
102 struct hlist_head head;
103 spinlock_t lock; /* protects head */
104 unsigned long timestamp;
105 uint16_t seqno;
106 uint16_t size;
107};
108
109/**
110 * struct batadv_frag_list_entry - entry in a list of fragments
111 * @list: list node information
112 * @skb: fragment
113 * @no: fragment number in the set
114 */
115struct batadv_frag_list_entry {
116 struct hlist_node list;
117 struct sk_buff *skb;
118 uint8_t no;
119};
120
121/**
122 * struct batadv_vlan_tt - VLAN specific TT attributes
123 * @crc: CRC32 checksum of the entries belonging to this vlan
124 * @num_entries: number of TT entries for this VLAN
125 */
126struct batadv_vlan_tt {
127 uint32_t crc;
128 atomic_t num_entries;
129};
130
131/**
132 * batadv_orig_node_vlan - VLAN specific data per orig_node
133 * @vid: the VLAN identifier
134 * @tt: VLAN specific TT attributes
135 * @list: list node for orig_node::vlan_list
136 * @refcount: number of context where this object is currently in use
137 * @rcu: struct used for freeing in a RCU-safe manner
138 */
139struct batadv_orig_node_vlan {
140 unsigned short vid;
141 struct batadv_vlan_tt tt;
142 struct list_head list;
143 atomic_t refcount;
144 struct rcu_head rcu;
145};
146
147/**
148 * struct batadv_orig_bat_iv - B.A.T.M.A.N. IV private orig_node members
149 * @bcast_own: bitfield containing the number of our OGMs this orig_node
150 * rebroadcasted "back" to us (relative to last_real_seqno)
151 * @bcast_own_sum: counted result of bcast_own
152 * @ogm_cnt_lock: lock protecting bcast_own, bcast_own_sum,
153 * neigh_node->bat_iv.real_bits & neigh_node->bat_iv.real_packet_count
154 */
155struct batadv_orig_bat_iv {
156 unsigned long *bcast_own;
157 uint8_t *bcast_own_sum;
158 /* ogm_cnt_lock protects: bcast_own, bcast_own_sum,
159 * neigh_node->bat_iv.real_bits & neigh_node->bat_iv.real_packet_count
160 */
161 spinlock_t ogm_cnt_lock;
162};
163
164/**
91 * struct batadv_orig_node - structure for orig_list maintaining nodes of mesh 165 * struct batadv_orig_node - structure for orig_list maintaining nodes of mesh
92 * @orig: originator ethernet address 166 * @orig: originator ethernet address
93 * @primary_addr: hosts primary interface address 167 * @primary_addr: hosts primary interface address
94 * @router: router that should be used to reach this originator 168 * @router: router that should be used to reach this originator
95 * @batadv_dat_addr_t: address of the orig node in the distributed hash 169 * @batadv_dat_addr_t: address of the orig node in the distributed hash
96 * @bcast_own: bitfield containing the number of our OGMs this orig_node
97 * rebroadcasted "back" to us (relative to last_real_seqno)
98 * @bcast_own_sum: counted result of bcast_own
99 * @last_seen: time when last packet from this node was received 170 * @last_seen: time when last packet from this node was received
100 * @bcast_seqno_reset: time when the broadcast seqno window was reset 171 * @bcast_seqno_reset: time when the broadcast seqno window was reset
101 * @batman_seqno_reset: time when the batman seqno window was reset 172 * @batman_seqno_reset: time when the batman seqno window was reset
102 * @gw_flags: flags related to gateway class 173 * @capabilities: announced capabilities of this originator
103 * @flags: for now only VIS_SERVER flag
104 * @last_ttvn: last seen translation table version number 174 * @last_ttvn: last seen translation table version number
105 * @tt_crc: CRC of the translation table
106 * @tt_buff: last tt changeset this node received from the orig node 175 * @tt_buff: last tt changeset this node received from the orig node
107 * @tt_buff_len: length of the last tt changeset this node received from the 176 * @tt_buff_len: length of the last tt changeset this node received from the
108 * orig node 177 * orig node
109 * @tt_buff_lock: lock that protects tt_buff and tt_buff_len 178 * @tt_buff_lock: lock that protects tt_buff and tt_buff_len
110 * @tt_size: number of global TT entries announced by the orig node
111 * @tt_initialised: bool keeping track of whether or not this node have received 179 * @tt_initialised: bool keeping track of whether or not this node have received
112 * any translation table information from the orig node yet 180 * any translation table information from the orig node yet
181 * @tt_lock: prevents from updating the table while reading it. Table update is
182 * made up by two operations (data structure update and metdata -CRC/TTVN-
183 * recalculation) and they have to be executed atomically in order to avoid
184 * another thread to read the table/metadata between those.
113 * @last_real_seqno: last and best known sequence number 185 * @last_real_seqno: last and best known sequence number
114 * @last_ttl: ttl of last received packet 186 * @last_ttl: ttl of last received packet
115 * @bcast_bits: bitfield containing the info which payload broadcast originated 187 * @bcast_bits: bitfield containing the info which payload broadcast originated
@@ -117,14 +189,9 @@ struct batadv_hard_iface {
117 * last_bcast_seqno) 189 * last_bcast_seqno)
118 * @last_bcast_seqno: last broadcast sequence number received by this host 190 * @last_bcast_seqno: last broadcast sequence number received by this host
119 * @neigh_list: list of potential next hop neighbor towards this orig node 191 * @neigh_list: list of potential next hop neighbor towards this orig node
120 * @frag_list: fragmentation buffer list for fragment re-assembly
121 * @last_frag_packet: time when last fragmented packet from this node was
122 * received
123 * @neigh_list_lock: lock protecting neigh_list, router and bonding_list 192 * @neigh_list_lock: lock protecting neigh_list, router and bonding_list
124 * @hash_entry: hlist node for batadv_priv::orig_hash 193 * @hash_entry: hlist node for batadv_priv::orig_hash
125 * @bat_priv: pointer to soft_iface this orig node belongs to 194 * @bat_priv: pointer to soft_iface this orig node belongs to
126 * @ogm_cnt_lock: lock protecting bcast_own, bcast_own_sum,
127 * neigh_node->real_bits & neigh_node->real_packet_count
128 * @bcast_seqno_lock: lock protecting bcast_bits & last_bcast_seqno 195 * @bcast_seqno_lock: lock protecting bcast_bits & last_bcast_seqno
129 * @bond_candidates: how many candidates are available 196 * @bond_candidates: how many candidates are available
130 * @bond_list: list of bonding candidates 197 * @bond_list: list of bonding candidates
@@ -134,6 +201,11 @@ struct batadv_hard_iface {
134 * @out_coding_list: list of nodes that can hear this orig 201 * @out_coding_list: list of nodes that can hear this orig
135 * @in_coding_list_lock: protects in_coding_list 202 * @in_coding_list_lock: protects in_coding_list
136 * @out_coding_list_lock: protects out_coding_list 203 * @out_coding_list_lock: protects out_coding_list
204 * @fragments: array with heads for fragment chains
205 * @vlan_list: a list of orig_node_vlan structs, one per VLAN served by the
206 * originator represented by this object
207 * @vlan_list_lock: lock protecting vlan_list
208 * @bat_iv: B.A.T.M.A.N. IV private structure
137 */ 209 */
138struct batadv_orig_node { 210struct batadv_orig_node {
139 uint8_t orig[ETH_ALEN]; 211 uint8_t orig[ETH_ALEN];
@@ -142,35 +214,26 @@ struct batadv_orig_node {
142#ifdef CONFIG_BATMAN_ADV_DAT 214#ifdef CONFIG_BATMAN_ADV_DAT
143 batadv_dat_addr_t dat_addr; 215 batadv_dat_addr_t dat_addr;
144#endif 216#endif
145 unsigned long *bcast_own;
146 uint8_t *bcast_own_sum;
147 unsigned long last_seen; 217 unsigned long last_seen;
148 unsigned long bcast_seqno_reset; 218 unsigned long bcast_seqno_reset;
149 unsigned long batman_seqno_reset; 219 unsigned long batman_seqno_reset;
150 uint8_t gw_flags; 220 uint8_t capabilities;
151 uint8_t flags;
152 atomic_t last_ttvn; 221 atomic_t last_ttvn;
153 uint16_t tt_crc;
154 unsigned char *tt_buff; 222 unsigned char *tt_buff;
155 int16_t tt_buff_len; 223 int16_t tt_buff_len;
156 spinlock_t tt_buff_lock; /* protects tt_buff & tt_buff_len */ 224 spinlock_t tt_buff_lock; /* protects tt_buff & tt_buff_len */
157 atomic_t tt_size;
158 bool tt_initialised; 225 bool tt_initialised;
226 /* prevents from changing the table while reading it */
227 spinlock_t tt_lock;
159 uint32_t last_real_seqno; 228 uint32_t last_real_seqno;
160 uint8_t last_ttl; 229 uint8_t last_ttl;
161 DECLARE_BITMAP(bcast_bits, BATADV_TQ_LOCAL_WINDOW_SIZE); 230 DECLARE_BITMAP(bcast_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
162 uint32_t last_bcast_seqno; 231 uint32_t last_bcast_seqno;
163 struct hlist_head neigh_list; 232 struct hlist_head neigh_list;
164 struct list_head frag_list;
165 unsigned long last_frag_packet;
166 /* neigh_list_lock protects: neigh_list, router & bonding_list */ 233 /* neigh_list_lock protects: neigh_list, router & bonding_list */
167 spinlock_t neigh_list_lock; 234 spinlock_t neigh_list_lock;
168 struct hlist_node hash_entry; 235 struct hlist_node hash_entry;
169 struct batadv_priv *bat_priv; 236 struct batadv_priv *bat_priv;
170 /* ogm_cnt_lock protects: bcast_own, bcast_own_sum,
171 * neigh_node->real_bits & neigh_node->real_packet_count
172 */
173 spinlock_t ogm_cnt_lock;
174 /* bcast_seqno_lock protects: bcast_bits & last_bcast_seqno */ 237 /* bcast_seqno_lock protects: bcast_bits & last_bcast_seqno */
175 spinlock_t bcast_seqno_lock; 238 spinlock_t bcast_seqno_lock;
176 atomic_t bond_candidates; 239 atomic_t bond_candidates;
@@ -183,12 +246,28 @@ struct batadv_orig_node {
183 spinlock_t in_coding_list_lock; /* Protects in_coding_list */ 246 spinlock_t in_coding_list_lock; /* Protects in_coding_list */
184 spinlock_t out_coding_list_lock; /* Protects out_coding_list */ 247 spinlock_t out_coding_list_lock; /* Protects out_coding_list */
185#endif 248#endif
249 struct batadv_frag_table_entry fragments[BATADV_FRAG_BUFFER_COUNT];
250 struct list_head vlan_list;
251 spinlock_t vlan_list_lock; /* protects vlan_list */
252 struct batadv_orig_bat_iv bat_iv;
253};
254
255/**
256 * enum batadv_orig_capabilities - orig node capabilities
257 * @BATADV_ORIG_CAPA_HAS_DAT: orig node has distributed arp table enabled
258 * @BATADV_ORIG_CAPA_HAS_NC: orig node has network coding enabled
259 */
260enum batadv_orig_capabilities {
261 BATADV_ORIG_CAPA_HAS_DAT = BIT(0),
262 BATADV_ORIG_CAPA_HAS_NC = BIT(1),
186}; 263};
187 264
188/** 265/**
189 * struct batadv_gw_node - structure for orig nodes announcing gw capabilities 266 * struct batadv_gw_node - structure for orig nodes announcing gw capabilities
190 * @list: list node for batadv_priv_gw::list 267 * @list: list node for batadv_priv_gw::list
191 * @orig_node: pointer to corresponding orig node 268 * @orig_node: pointer to corresponding orig node
269 * @bandwidth_down: advertised uplink download bandwidth
270 * @bandwidth_up: advertised uplink upload bandwidth
192 * @deleted: this struct is scheduled for deletion 271 * @deleted: this struct is scheduled for deletion
193 * @refcount: number of contexts the object is used 272 * @refcount: number of contexts the object is used
194 * @rcu: struct used for freeing in an RCU-safe manner 273 * @rcu: struct used for freeing in an RCU-safe manner
@@ -196,46 +275,57 @@ struct batadv_orig_node {
196struct batadv_gw_node { 275struct batadv_gw_node {
197 struct hlist_node list; 276 struct hlist_node list;
198 struct batadv_orig_node *orig_node; 277 struct batadv_orig_node *orig_node;
278 uint32_t bandwidth_down;
279 uint32_t bandwidth_up;
199 unsigned long deleted; 280 unsigned long deleted;
200 atomic_t refcount; 281 atomic_t refcount;
201 struct rcu_head rcu; 282 struct rcu_head rcu;
202}; 283};
203 284
204/** 285/**
205 * struct batadv_neigh_node - structure for single hop neighbors 286 * struct batadv_neigh_bat_iv - B.A.T.M.A.N. IV specific structure for single
206 * @list: list node for batadv_orig_node::neigh_list 287 * hop neighbors
207 * @addr: mac address of neigh node
208 * @tq_recv: ring buffer of received TQ values from this neigh node 288 * @tq_recv: ring buffer of received TQ values from this neigh node
209 * @tq_index: ring buffer index 289 * @tq_index: ring buffer index
210 * @tq_avg: averaged tq of all tq values in the ring buffer (tq_recv) 290 * @tq_avg: averaged tq of all tq values in the ring buffer (tq_recv)
211 * @last_ttl: last received ttl from this neigh node
212 * @bonding_list: list node for batadv_orig_node::bond_list
213 * @last_seen: when last packet via this neighbor was received
214 * @real_bits: bitfield containing the number of OGMs received from this neigh 291 * @real_bits: bitfield containing the number of OGMs received from this neigh
215 * node (relative to orig_node->last_real_seqno) 292 * node (relative to orig_node->last_real_seqno)
216 * @real_packet_count: counted result of real_bits 293 * @real_packet_count: counted result of real_bits
294 * @lq_update_lock: lock protecting tq_recv & tq_index
295 */
296struct batadv_neigh_bat_iv {
297 uint8_t tq_recv[BATADV_TQ_GLOBAL_WINDOW_SIZE];
298 uint8_t tq_index;
299 uint8_t tq_avg;
300 DECLARE_BITMAP(real_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
301 uint8_t real_packet_count;
302 spinlock_t lq_update_lock; /* protects tq_recv & tq_index */
303};
304
305/**
306 * struct batadv_neigh_node - structure for single hops neighbors
307 * @list: list node for batadv_orig_node::neigh_list
217 * @orig_node: pointer to corresponding orig_node 308 * @orig_node: pointer to corresponding orig_node
309 * @addr: the MAC address of the neighboring interface
218 * @if_incoming: pointer to incoming hard interface 310 * @if_incoming: pointer to incoming hard interface
219 * @lq_update_lock: lock protecting tq_recv & tq_index 311 * @last_seen: when last packet via this neighbor was received
312 * @last_ttl: last received ttl from this neigh node
313 * @bonding_list: list node for batadv_orig_node::bond_list
220 * @refcount: number of contexts the object is used 314 * @refcount: number of contexts the object is used
221 * @rcu: struct used for freeing in an RCU-safe manner 315 * @rcu: struct used for freeing in an RCU-safe manner
316 * @bat_iv: B.A.T.M.A.N. IV private structure
222 */ 317 */
223struct batadv_neigh_node { 318struct batadv_neigh_node {
224 struct hlist_node list; 319 struct hlist_node list;
320 struct batadv_orig_node *orig_node;
225 uint8_t addr[ETH_ALEN]; 321 uint8_t addr[ETH_ALEN];
226 uint8_t tq_recv[BATADV_TQ_GLOBAL_WINDOW_SIZE]; 322 struct batadv_hard_iface *if_incoming;
227 uint8_t tq_index; 323 unsigned long last_seen;
228 uint8_t tq_avg;
229 uint8_t last_ttl; 324 uint8_t last_ttl;
230 struct list_head bonding_list; 325 struct list_head bonding_list;
231 unsigned long last_seen;
232 DECLARE_BITMAP(real_bits, BATADV_TQ_LOCAL_WINDOW_SIZE);
233 uint8_t real_packet_count;
234 struct batadv_orig_node *orig_node;
235 struct batadv_hard_iface *if_incoming;
236 spinlock_t lq_update_lock; /* protects tq_recv & tq_index */
237 atomic_t refcount; 326 atomic_t refcount;
238 struct rcu_head rcu; 327 struct rcu_head rcu;
328 struct batadv_neigh_bat_iv bat_iv;
239}; 329};
240 330
241/** 331/**
@@ -265,6 +355,12 @@ struct batadv_bcast_duplist_entry {
265 * @BATADV_CNT_MGMT_TX_BYTES: transmitted routing protocol traffic bytes counter 355 * @BATADV_CNT_MGMT_TX_BYTES: transmitted routing protocol traffic bytes counter
266 * @BATADV_CNT_MGMT_RX: received routing protocol traffic packet counter 356 * @BATADV_CNT_MGMT_RX: received routing protocol traffic packet counter
267 * @BATADV_CNT_MGMT_RX_BYTES: received routing protocol traffic bytes counter 357 * @BATADV_CNT_MGMT_RX_BYTES: received routing protocol traffic bytes counter
358 * @BATADV_CNT_FRAG_TX: transmitted fragment traffic packet counter
359 * @BATADV_CNT_FRAG_TX_BYTES: transmitted fragment traffic bytes counter
360 * @BATADV_CNT_FRAG_RX: received fragment traffic packet counter
361 * @BATADV_CNT_FRAG_RX_BYTES: received fragment traffic bytes counter
362 * @BATADV_CNT_FRAG_FWD: forwarded fragment traffic packet counter
363 * @BATADV_CNT_FRAG_FWD_BYTES: forwarded fragment traffic bytes counter
268 * @BATADV_CNT_TT_REQUEST_TX: transmitted tt req traffic packet counter 364 * @BATADV_CNT_TT_REQUEST_TX: transmitted tt req traffic packet counter
269 * @BATADV_CNT_TT_REQUEST_RX: received tt req traffic packet counter 365 * @BATADV_CNT_TT_REQUEST_RX: received tt req traffic packet counter
270 * @BATADV_CNT_TT_RESPONSE_TX: transmitted tt resp traffic packet counter 366 * @BATADV_CNT_TT_RESPONSE_TX: transmitted tt resp traffic packet counter
@@ -302,6 +398,12 @@ enum batadv_counters {
302 BATADV_CNT_MGMT_TX_BYTES, 398 BATADV_CNT_MGMT_TX_BYTES,
303 BATADV_CNT_MGMT_RX, 399 BATADV_CNT_MGMT_RX,
304 BATADV_CNT_MGMT_RX_BYTES, 400 BATADV_CNT_MGMT_RX_BYTES,
401 BATADV_CNT_FRAG_TX,
402 BATADV_CNT_FRAG_TX_BYTES,
403 BATADV_CNT_FRAG_RX,
404 BATADV_CNT_FRAG_RX_BYTES,
405 BATADV_CNT_FRAG_FWD,
406 BATADV_CNT_FRAG_FWD_BYTES,
305 BATADV_CNT_TT_REQUEST_TX, 407 BATADV_CNT_TT_REQUEST_TX,
306 BATADV_CNT_TT_REQUEST_RX, 408 BATADV_CNT_TT_REQUEST_RX,
307 BATADV_CNT_TT_RESPONSE_TX, 409 BATADV_CNT_TT_RESPONSE_TX,
@@ -343,11 +445,14 @@ enum batadv_counters {
343 * @changes_list_lock: lock protecting changes_list 445 * @changes_list_lock: lock protecting changes_list
344 * @req_list_lock: lock protecting req_list 446 * @req_list_lock: lock protecting req_list
345 * @roam_list_lock: lock protecting roam_list 447 * @roam_list_lock: lock protecting roam_list
346 * @local_entry_num: number of entries in the local hash table
347 * @local_crc: Checksum of the local table, recomputed before sending a new OGM
348 * @last_changeset: last tt changeset this host has generated 448 * @last_changeset: last tt changeset this host has generated
349 * @last_changeset_len: length of last tt changeset this host has generated 449 * @last_changeset_len: length of last tt changeset this host has generated
350 * @last_changeset_lock: lock protecting last_changeset & last_changeset_len 450 * @last_changeset_lock: lock protecting last_changeset & last_changeset_len
451 * @commit_lock: prevents from executing a local TT commit while reading the
452 * local table. The local TT commit is made up by two operations (data
453 * structure update and metdata -CRC/TTVN- recalculation) and they have to be
454 * executed atomically in order to avoid another thread to read the
455 * table/metadata between those.
351 * @work: work queue callback item for translation table purging 456 * @work: work queue callback item for translation table purging
352 */ 457 */
353struct batadv_priv_tt { 458struct batadv_priv_tt {
@@ -362,12 +467,12 @@ struct batadv_priv_tt {
362 spinlock_t changes_list_lock; /* protects changes */ 467 spinlock_t changes_list_lock; /* protects changes */
363 spinlock_t req_list_lock; /* protects req_list */ 468 spinlock_t req_list_lock; /* protects req_list */
364 spinlock_t roam_list_lock; /* protects roam_list */ 469 spinlock_t roam_list_lock; /* protects roam_list */
365 atomic_t local_entry_num;
366 uint16_t local_crc;
367 unsigned char *last_changeset; 470 unsigned char *last_changeset;
368 int16_t last_changeset_len; 471 int16_t last_changeset_len;
369 /* protects last_changeset & last_changeset_len */ 472 /* protects last_changeset & last_changeset_len */
370 spinlock_t last_changeset_lock; 473 spinlock_t last_changeset_lock;
474 /* prevents from executing a commit while reading the table */
475 spinlock_t commit_lock;
371 struct delayed_work work; 476 struct delayed_work work;
372}; 477};
373 478
@@ -420,31 +525,31 @@ struct batadv_priv_debug_log {
420 * @list: list of available gateway nodes 525 * @list: list of available gateway nodes
421 * @list_lock: lock protecting gw_list & curr_gw 526 * @list_lock: lock protecting gw_list & curr_gw
422 * @curr_gw: pointer to currently selected gateway node 527 * @curr_gw: pointer to currently selected gateway node
528 * @bandwidth_down: advertised uplink download bandwidth (if gw_mode server)
529 * @bandwidth_up: advertised uplink upload bandwidth (if gw_mode server)
423 * @reselect: bool indicating a gateway re-selection is in progress 530 * @reselect: bool indicating a gateway re-selection is in progress
424 */ 531 */
425struct batadv_priv_gw { 532struct batadv_priv_gw {
426 struct hlist_head list; 533 struct hlist_head list;
427 spinlock_t list_lock; /* protects gw_list & curr_gw */ 534 spinlock_t list_lock; /* protects gw_list & curr_gw */
428 struct batadv_gw_node __rcu *curr_gw; /* rcu protected pointer */ 535 struct batadv_gw_node __rcu *curr_gw; /* rcu protected pointer */
536 atomic_t bandwidth_down;
537 atomic_t bandwidth_up;
429 atomic_t reselect; 538 atomic_t reselect;
430}; 539};
431 540
432/** 541/**
433 * struct batadv_priv_vis - per mesh interface vis data 542 * struct batadv_priv_tvlv - per mesh interface tvlv data
434 * @send_list: list of batadv_vis_info packets to sent 543 * @container_list: list of registered tvlv containers to be sent with each OGM
435 * @hash: hash table containing vis data from other nodes in the network 544 * @handler_list: list of the various tvlv content handlers
436 * @hash_lock: lock protecting the hash table 545 * @container_list_lock: protects tvlv container list access
437 * @list_lock: lock protecting my_info::recv_list 546 * @handler_list_lock: protects handler list access
438 * @work: work queue callback item for vis packet sending
439 * @my_info: holds this node's vis data sent on a regular basis
440 */ 547 */
441struct batadv_priv_vis { 548struct batadv_priv_tvlv {
442 struct list_head send_list; 549 struct hlist_head container_list;
443 struct batadv_hashtable *hash; 550 struct hlist_head handler_list;
444 spinlock_t hash_lock; /* protects hash */ 551 spinlock_t container_list_lock; /* protects container_list */
445 spinlock_t list_lock; /* protects my_info::recv_list */ 552 spinlock_t handler_list_lock; /* protects handler_list */
446 struct delayed_work work;
447 struct batadv_vis_info *my_info;
448}; 553};
449 554
450/** 555/**
@@ -491,6 +596,26 @@ struct batadv_priv_nc {
491}; 596};
492 597
493/** 598/**
599 * struct batadv_softif_vlan - per VLAN attributes set
600 * @vid: VLAN identifier
601 * @kobj: kobject for sysfs vlan subdirectory
602 * @ap_isolation: AP isolation state
603 * @tt: TT private attributes (VLAN specific)
604 * @list: list node for bat_priv::softif_vlan_list
605 * @refcount: number of context where this object is currently in use
606 * @rcu: struct used for freeing in a RCU-safe manner
607 */
608struct batadv_softif_vlan {
609 unsigned short vid;
610 struct kobject *kobj;
611 atomic_t ap_isolation; /* boolean */
612 struct batadv_vlan_tt tt;
613 struct hlist_node list;
614 atomic_t refcount;
615 struct rcu_head rcu;
616};
617
618/**
494 * struct batadv_priv - per mesh interface data 619 * struct batadv_priv - per mesh interface data
495 * @mesh_state: current status of the mesh (inactive/active/deactivating) 620 * @mesh_state: current status of the mesh (inactive/active/deactivating)
496 * @soft_iface: net device which holds this struct as private data 621 * @soft_iface: net device which holds this struct as private data
@@ -499,15 +624,15 @@ struct batadv_priv_nc {
499 * @aggregated_ogms: bool indicating whether OGM aggregation is enabled 624 * @aggregated_ogms: bool indicating whether OGM aggregation is enabled
500 * @bonding: bool indicating whether traffic bonding is enabled 625 * @bonding: bool indicating whether traffic bonding is enabled
501 * @fragmentation: bool indicating whether traffic fragmentation is enabled 626 * @fragmentation: bool indicating whether traffic fragmentation is enabled
502 * @ap_isolation: bool indicating whether ap isolation is enabled 627 * @packet_size_max: max packet size that can be transmitted via
628 * multiple fragmented skbs or a single frame if fragmentation is disabled
629 * @frag_seqno: incremental counter to identify chains of egress fragments
503 * @bridge_loop_avoidance: bool indicating whether bridge loop avoidance is 630 * @bridge_loop_avoidance: bool indicating whether bridge loop avoidance is
504 * enabled 631 * enabled
505 * @distributed_arp_table: bool indicating whether distributed ARP table is 632 * @distributed_arp_table: bool indicating whether distributed ARP table is
506 * enabled 633 * enabled
507 * @vis_mode: vis operation: client or server (see batadv_vis_packettype)
508 * @gw_mode: gateway operation: off, client or server (see batadv_gw_modes) 634 * @gw_mode: gateway operation: off, client or server (see batadv_gw_modes)
509 * @gw_sel_class: gateway selection class (applies if gw_mode client) 635 * @gw_sel_class: gateway selection class (applies if gw_mode client)
510 * @gw_bandwidth: gateway announced bandwidth (applies if gw_mode server)
511 * @orig_interval: OGM broadcast interval in milliseconds 636 * @orig_interval: OGM broadcast interval in milliseconds
512 * @hop_penalty: penalty which will be applied to an OGM's tq-field on every hop 637 * @hop_penalty: penalty which will be applied to an OGM's tq-field on every hop
513 * @log_level: configured log level (see batadv_dbg_level) 638 * @log_level: configured log level (see batadv_dbg_level)
@@ -527,11 +652,14 @@ struct batadv_priv_nc {
527 * @primary_if: one of the hard interfaces assigned to this mesh interface 652 * @primary_if: one of the hard interfaces assigned to this mesh interface
528 * becomes the primary interface 653 * becomes the primary interface
529 * @bat_algo_ops: routing algorithm used by this mesh interface 654 * @bat_algo_ops: routing algorithm used by this mesh interface
655 * @softif_vlan_list: a list of softif_vlan structs, one per VLAN created on top
656 * of the mesh interface represented by this object
657 * @softif_vlan_list_lock: lock protecting softif_vlan_list
530 * @bla: bridge loope avoidance data 658 * @bla: bridge loope avoidance data
531 * @debug_log: holding debug logging relevant data 659 * @debug_log: holding debug logging relevant data
532 * @gw: gateway data 660 * @gw: gateway data
533 * @tt: translation table data 661 * @tt: translation table data
534 * @vis: vis data 662 * @tvlv: type-version-length-value data
535 * @dat: distributed arp table data 663 * @dat: distributed arp table data
536 * @network_coding: bool indicating whether network coding is enabled 664 * @network_coding: bool indicating whether network coding is enabled
537 * @batadv_priv_nc: network coding data 665 * @batadv_priv_nc: network coding data
@@ -544,17 +672,16 @@ struct batadv_priv {
544 atomic_t aggregated_ogms; 672 atomic_t aggregated_ogms;
545 atomic_t bonding; 673 atomic_t bonding;
546 atomic_t fragmentation; 674 atomic_t fragmentation;
547 atomic_t ap_isolation; 675 atomic_t packet_size_max;
676 atomic_t frag_seqno;
548#ifdef CONFIG_BATMAN_ADV_BLA 677#ifdef CONFIG_BATMAN_ADV_BLA
549 atomic_t bridge_loop_avoidance; 678 atomic_t bridge_loop_avoidance;
550#endif 679#endif
551#ifdef CONFIG_BATMAN_ADV_DAT 680#ifdef CONFIG_BATMAN_ADV_DAT
552 atomic_t distributed_arp_table; 681 atomic_t distributed_arp_table;
553#endif 682#endif
554 atomic_t vis_mode;
555 atomic_t gw_mode; 683 atomic_t gw_mode;
556 atomic_t gw_sel_class; 684 atomic_t gw_sel_class;
557 atomic_t gw_bandwidth;
558 atomic_t orig_interval; 685 atomic_t orig_interval;
559 atomic_t hop_penalty; 686 atomic_t hop_penalty;
560#ifdef CONFIG_BATMAN_ADV_DEBUG 687#ifdef CONFIG_BATMAN_ADV_DEBUG
@@ -575,6 +702,8 @@ struct batadv_priv {
575 struct work_struct cleanup_work; 702 struct work_struct cleanup_work;
576 struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */ 703 struct batadv_hard_iface __rcu *primary_if; /* rcu protected pointer */
577 struct batadv_algo_ops *bat_algo_ops; 704 struct batadv_algo_ops *bat_algo_ops;
705 struct hlist_head softif_vlan_list;
706 spinlock_t softif_vlan_list_lock; /* protects softif_vlan_list */
578#ifdef CONFIG_BATMAN_ADV_BLA 707#ifdef CONFIG_BATMAN_ADV_BLA
579 struct batadv_priv_bla bla; 708 struct batadv_priv_bla bla;
580#endif 709#endif
@@ -583,7 +712,7 @@ struct batadv_priv {
583#endif 712#endif
584 struct batadv_priv_gw gw; 713 struct batadv_priv_gw gw;
585 struct batadv_priv_tt tt; 714 struct batadv_priv_tt tt;
586 struct batadv_priv_vis vis; 715 struct batadv_priv_tvlv tvlv;
587#ifdef CONFIG_BATMAN_ADV_DAT 716#ifdef CONFIG_BATMAN_ADV_DAT
588 struct batadv_priv_dat dat; 717 struct batadv_priv_dat dat;
589#endif 718#endif
@@ -620,7 +749,7 @@ struct batadv_socket_client {
620struct batadv_socket_packet { 749struct batadv_socket_packet {
621 struct list_head list; 750 struct list_head list;
622 size_t icmp_len; 751 size_t icmp_len;
623 struct batadv_icmp_packet_rr icmp_packet; 752 uint8_t icmp_packet[BATADV_ICMP_MAX_PACKET_SIZE];
624}; 753};
625 754
626/** 755/**
@@ -677,6 +806,7 @@ struct batadv_bla_claim {
677/** 806/**
678 * struct batadv_tt_common_entry - tt local & tt global common data 807 * struct batadv_tt_common_entry - tt local & tt global common data
679 * @addr: mac address of non-mesh client 808 * @addr: mac address of non-mesh client
809 * @vid: VLAN identifier
680 * @hash_entry: hlist node for batadv_priv_tt::local_hash or for 810 * @hash_entry: hlist node for batadv_priv_tt::local_hash or for
681 * batadv_priv_tt::global_hash 811 * batadv_priv_tt::global_hash
682 * @flags: various state handling flags (see batadv_tt_client_flags) 812 * @flags: various state handling flags (see batadv_tt_client_flags)
@@ -686,6 +816,7 @@ struct batadv_bla_claim {
686 */ 816 */
687struct batadv_tt_common_entry { 817struct batadv_tt_common_entry {
688 uint8_t addr[ETH_ALEN]; 818 uint8_t addr[ETH_ALEN];
819 unsigned short vid;
689 struct hlist_node hash_entry; 820 struct hlist_node hash_entry;
690 uint16_t flags; 821 uint16_t flags;
691 unsigned long added_at; 822 unsigned long added_at;
@@ -740,7 +871,7 @@ struct batadv_tt_orig_list_entry {
740 */ 871 */
741struct batadv_tt_change_node { 872struct batadv_tt_change_node {
742 struct list_head list; 873 struct list_head list;
743 struct batadv_tt_change change; 874 struct batadv_tvlv_tt_change change;
744}; 875};
745 876
746/** 877/**
@@ -866,78 +997,6 @@ struct batadv_forw_packet {
866}; 997};
867 998
868/** 999/**
869 * struct batadv_frag_packet_list_entry - storage for fragment packet
870 * @list: list node for orig_node::frag_list
871 * @seqno: sequence number of the fragment
872 * @skb: fragment's skb buffer
873 */
874struct batadv_frag_packet_list_entry {
875 struct list_head list;
876 uint16_t seqno;
877 struct sk_buff *skb;
878};
879
880/**
881 * struct batadv_vis_info - local data for vis information
882 * @first_seen: timestamp used for purging stale vis info entries
883 * @recv_list: List of server-neighbors we have received this packet from. This
884 * packet should not be re-forward to them again. List elements are struct
885 * batadv_vis_recvlist_node
886 * @send_list: list of packets to be forwarded
887 * @refcount: number of contexts the object is used
888 * @hash_entry: hlist node for batadv_priv_vis::hash
889 * @bat_priv: pointer to soft_iface this orig node belongs to
890 * @skb_packet: contains the vis packet
891 */
892struct batadv_vis_info {
893 unsigned long first_seen;
894 struct list_head recv_list;
895 struct list_head send_list;
896 struct kref refcount;
897 struct hlist_node hash_entry;
898 struct batadv_priv *bat_priv;
899 struct sk_buff *skb_packet;
900} __packed;
901
902/**
903 * struct batadv_vis_info_entry - contains link information for vis
904 * @src: source MAC of the link, all zero for local TT entry
905 * @dst: destination MAC of the link, client mac address for local TT entry
906 * @quality: transmission quality of the link, or 0 for local TT entry
907 */
908struct batadv_vis_info_entry {
909 uint8_t src[ETH_ALEN];
910 uint8_t dest[ETH_ALEN];
911 uint8_t quality;
912} __packed;
913
914/**
915 * struct batadv_vis_recvlist_node - list entry for batadv_vis_info::recv_list
916 * @list: list node for batadv_vis_info::recv_list
917 * @mac: MAC address of the originator from where the vis_info was received
918 */
919struct batadv_vis_recvlist_node {
920 struct list_head list;
921 uint8_t mac[ETH_ALEN];
922};
923
924/**
925 * struct batadv_vis_if_list_entry - auxiliary data for vis data generation
926 * @addr: MAC address of the interface
927 * @primary: true if this interface is the primary interface
928 * @list: list node the interface list
929 *
930 * While scanning for vis-entries of a particular vis-originator
931 * this list collects its interfaces to create a subgraph/cluster
932 * out of them later
933 */
934struct batadv_vis_if_list_entry {
935 uint8_t addr[ETH_ALEN];
936 bool primary;
937 struct hlist_node list;
938};
939
940/**
941 * struct batadv_algo_ops - mesh algorithm callbacks 1000 * struct batadv_algo_ops - mesh algorithm callbacks
942 * @list: list node for the batadv_algo_list 1001 * @list: list node for the batadv_algo_list
943 * @name: name of the algorithm 1002 * @name: name of the algorithm
@@ -948,6 +1007,16 @@ struct batadv_vis_if_list_entry {
948 * @bat_primary_iface_set: called when primary interface is selected / changed 1007 * @bat_primary_iface_set: called when primary interface is selected / changed
949 * @bat_ogm_schedule: prepare a new outgoing OGM for the send queue 1008 * @bat_ogm_schedule: prepare a new outgoing OGM for the send queue
950 * @bat_ogm_emit: send scheduled OGM 1009 * @bat_ogm_emit: send scheduled OGM
1010 * @bat_neigh_cmp: compare the metrics of two neighbors
1011 * @bat_neigh_is_equiv_or_better: check if neigh1 is equally good or
1012 * better than neigh2 from the metric prospective
1013 * @bat_orig_print: print the originator table (optional)
1014 * @bat_orig_free: free the resources allocated by the routing algorithm for an
1015 * orig_node object
1016 * @bat_orig_add_if: ask the routing algorithm to apply the needed changes to
1017 * the orig_node due to a new hard-interface being added into the mesh
1018 * @bat_orig_del_if: ask the routing algorithm to apply the needed changes to
1019 * the orig_node due to an hard-interface being removed from the mesh
951 */ 1020 */
952struct batadv_algo_ops { 1021struct batadv_algo_ops {
953 struct hlist_node list; 1022 struct hlist_node list;
@@ -958,6 +1027,17 @@ struct batadv_algo_ops {
958 void (*bat_primary_iface_set)(struct batadv_hard_iface *hard_iface); 1027 void (*bat_primary_iface_set)(struct batadv_hard_iface *hard_iface);
959 void (*bat_ogm_schedule)(struct batadv_hard_iface *hard_iface); 1028 void (*bat_ogm_schedule)(struct batadv_hard_iface *hard_iface);
960 void (*bat_ogm_emit)(struct batadv_forw_packet *forw_packet); 1029 void (*bat_ogm_emit)(struct batadv_forw_packet *forw_packet);
1030 int (*bat_neigh_cmp)(struct batadv_neigh_node *neigh1,
1031 struct batadv_neigh_node *neigh2);
1032 bool (*bat_neigh_is_equiv_or_better)(struct batadv_neigh_node *neigh1,
1033 struct batadv_neigh_node *neigh2);
1034 /* orig_node handling API */
1035 void (*bat_orig_print)(struct batadv_priv *priv, struct seq_file *seq);
1036 void (*bat_orig_free)(struct batadv_orig_node *orig_node);
1037 int (*bat_orig_add_if)(struct batadv_orig_node *orig_node,
1038 int max_if_num);
1039 int (*bat_orig_del_if)(struct batadv_orig_node *orig_node,
1040 int max_if_num, int del_if_num);
961}; 1041};
962 1042
963/** 1043/**
@@ -965,6 +1045,7 @@ struct batadv_algo_ops {
965 * is used to stored ARP entries needed for the global DAT cache 1045 * is used to stored ARP entries needed for the global DAT cache
966 * @ip: the IPv4 corresponding to this DAT/ARP entry 1046 * @ip: the IPv4 corresponding to this DAT/ARP entry
967 * @mac_addr: the MAC address associated to the stored IPv4 1047 * @mac_addr: the MAC address associated to the stored IPv4
1048 * @vid: the vlan ID associated to this entry
968 * @last_update: time in jiffies when this entry was refreshed last time 1049 * @last_update: time in jiffies when this entry was refreshed last time
969 * @hash_entry: hlist node for batadv_priv_dat::hash 1050 * @hash_entry: hlist node for batadv_priv_dat::hash
970 * @refcount: number of contexts the object is used 1051 * @refcount: number of contexts the object is used
@@ -973,6 +1054,7 @@ struct batadv_algo_ops {
973struct batadv_dat_entry { 1054struct batadv_dat_entry {
974 __be32 ip; 1055 __be32 ip;
975 uint8_t mac_addr[ETH_ALEN]; 1056 uint8_t mac_addr[ETH_ALEN];
1057 unsigned short vid;
976 unsigned long last_update; 1058 unsigned long last_update;
977 struct hlist_node hash_entry; 1059 struct hlist_node hash_entry;
978 atomic_t refcount; 1060 atomic_t refcount;
@@ -992,4 +1074,60 @@ struct batadv_dat_candidate {
992 struct batadv_orig_node *orig_node; 1074 struct batadv_orig_node *orig_node;
993}; 1075};
994 1076
1077/**
1078 * struct batadv_tvlv_container - container for tvlv appended to OGMs
1079 * @list: hlist node for batadv_priv_tvlv::container_list
1080 * @tvlv_hdr: tvlv header information needed to construct the tvlv
1081 * @value_len: length of the buffer following this struct which contains
1082 * the actual tvlv payload
1083 * @refcount: number of contexts the object is used
1084 */
1085struct batadv_tvlv_container {
1086 struct hlist_node list;
1087 struct batadv_tvlv_hdr tvlv_hdr;
1088 atomic_t refcount;
1089};
1090
1091/**
1092 * struct batadv_tvlv_handler - handler for specific tvlv type and version
1093 * @list: hlist node for batadv_priv_tvlv::handler_list
1094 * @ogm_handler: handler callback which is given the tvlv payload to process on
1095 * incoming OGM packets
1096 * @unicast_handler: handler callback which is given the tvlv payload to process
1097 * on incoming unicast tvlv packets
1098 * @type: tvlv type this handler feels responsible for
1099 * @version: tvlv version this handler feels responsible for
1100 * @flags: tvlv handler flags
1101 * @refcount: number of contexts the object is used
1102 * @rcu: struct used for freeing in an RCU-safe manner
1103 */
1104struct batadv_tvlv_handler {
1105 struct hlist_node list;
1106 void (*ogm_handler)(struct batadv_priv *bat_priv,
1107 struct batadv_orig_node *orig,
1108 uint8_t flags,
1109 void *tvlv_value, uint16_t tvlv_value_len);
1110 int (*unicast_handler)(struct batadv_priv *bat_priv,
1111 uint8_t *src, uint8_t *dst,
1112 void *tvlv_value, uint16_t tvlv_value_len);
1113 uint8_t type;
1114 uint8_t version;
1115 uint8_t flags;
1116 atomic_t refcount;
1117 struct rcu_head rcu;
1118};
1119
1120/**
1121 * enum batadv_tvlv_handler_flags - tvlv handler flags definitions
1122 * @BATADV_TVLV_HANDLER_OGM_CIFNOTFND: tvlv ogm processing function will call
1123 * this handler even if its type was not found (with no data)
1124 * @BATADV_TVLV_HANDLER_OGM_CALLED: interval tvlv handling flag - the API marks
1125 * a handler as being called, so it won't be called if the
1126 * BATADV_TVLV_HANDLER_OGM_CIFNOTFND flag was set
1127 */
1128enum batadv_tvlv_handler_flags {
1129 BATADV_TVLV_HANDLER_OGM_CIFNOTFND = BIT(1),
1130 BATADV_TVLV_HANDLER_OGM_CALLED = BIT(2),
1131};
1132
995#endif /* _NET_BATMAN_ADV_TYPES_H_ */ 1133#endif /* _NET_BATMAN_ADV_TYPES_H_ */
diff --git a/net/batman-adv/unicast.c b/net/batman-adv/unicast.c
deleted file mode 100644
index 48b31d33ce6b..000000000000
--- a/net/batman-adv/unicast.c
+++ /dev/null
@@ -1,491 +0,0 @@
1/* Copyright (C) 2010-2013 B.A.T.M.A.N. contributors:
2 *
3 * Andreas Langer
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#include "main.h"
21#include "unicast.h"
22#include "send.h"
23#include "soft-interface.h"
24#include "gateway_client.h"
25#include "originator.h"
26#include "hash.h"
27#include "translation-table.h"
28#include "routing.h"
29#include "hard-interface.h"
30
31
32static struct sk_buff *
33batadv_frag_merge_packet(struct list_head *head,
34 struct batadv_frag_packet_list_entry *tfp,
35 struct sk_buff *skb)
36{
37 struct batadv_unicast_frag_packet *up;
38 struct sk_buff *tmp_skb;
39 struct batadv_unicast_packet *unicast_packet;
40 int hdr_len = sizeof(*unicast_packet);
41 int uni_diff = sizeof(*up) - hdr_len;
42 uint8_t *packet_pos;
43
44 up = (struct batadv_unicast_frag_packet *)skb->data;
45 /* set skb to the first part and tmp_skb to the second part */
46 if (up->flags & BATADV_UNI_FRAG_HEAD) {
47 tmp_skb = tfp->skb;
48 } else {
49 tmp_skb = skb;
50 skb = tfp->skb;
51 }
52
53 if (skb_linearize(skb) < 0 || skb_linearize(tmp_skb) < 0)
54 goto err;
55
56 skb_pull(tmp_skb, sizeof(*up));
57 if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0)
58 goto err;
59
60 /* move free entry to end */
61 tfp->skb = NULL;
62 tfp->seqno = 0;
63 list_move_tail(&tfp->list, head);
64
65 memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
66 kfree_skb(tmp_skb);
67
68 memmove(skb->data + uni_diff, skb->data, hdr_len);
69 packet_pos = skb_pull(skb, uni_diff);
70 unicast_packet = (struct batadv_unicast_packet *)packet_pos;
71 unicast_packet->header.packet_type = BATADV_UNICAST;
72
73 return skb;
74
75err:
76 /* free buffered skb, skb will be freed later */
77 kfree_skb(tfp->skb);
78 return NULL;
79}
80
81static void batadv_frag_create_entry(struct list_head *head,
82 struct sk_buff *skb)
83{
84 struct batadv_frag_packet_list_entry *tfp;
85 struct batadv_unicast_frag_packet *up;
86
87 up = (struct batadv_unicast_frag_packet *)skb->data;
88
89 /* free and oldest packets stand at the end */
90 tfp = list_entry((head)->prev, typeof(*tfp), list);
91 kfree_skb(tfp->skb);
92
93 tfp->seqno = ntohs(up->seqno);
94 tfp->skb = skb;
95 list_move(&tfp->list, head);
96 return;
97}
98
99static int batadv_frag_create_buffer(struct list_head *head)
100{
101 int i;
102 struct batadv_frag_packet_list_entry *tfp;
103
104 for (i = 0; i < BATADV_FRAG_BUFFER_SIZE; i++) {
105 tfp = kmalloc(sizeof(*tfp), GFP_ATOMIC);
106 if (!tfp) {
107 batadv_frag_list_free(head);
108 return -ENOMEM;
109 }
110 tfp->skb = NULL;
111 tfp->seqno = 0;
112 INIT_LIST_HEAD(&tfp->list);
113 list_add(&tfp->list, head);
114 }
115
116 return 0;
117}
118
119static struct batadv_frag_packet_list_entry *
120batadv_frag_search_packet(struct list_head *head,
121 const struct batadv_unicast_frag_packet *up)
122{
123 struct batadv_frag_packet_list_entry *tfp;
124 struct batadv_unicast_frag_packet *tmp_up = NULL;
125 bool is_head_tmp, is_head;
126 uint16_t search_seqno;
127
128 if (up->flags & BATADV_UNI_FRAG_HEAD)
129 search_seqno = ntohs(up->seqno)+1;
130 else
131 search_seqno = ntohs(up->seqno)-1;
132
133 is_head = up->flags & BATADV_UNI_FRAG_HEAD;
134
135 list_for_each_entry(tfp, head, list) {
136 if (!tfp->skb)
137 continue;
138
139 if (tfp->seqno == ntohs(up->seqno))
140 goto mov_tail;
141
142 tmp_up = (struct batadv_unicast_frag_packet *)tfp->skb->data;
143
144 if (tfp->seqno == search_seqno) {
145 is_head_tmp = tmp_up->flags & BATADV_UNI_FRAG_HEAD;
146 if (is_head_tmp != is_head)
147 return tfp;
148 else
149 goto mov_tail;
150 }
151 }
152 return NULL;
153
154mov_tail:
155 list_move_tail(&tfp->list, head);
156 return NULL;
157}
158
159void batadv_frag_list_free(struct list_head *head)
160{
161 struct batadv_frag_packet_list_entry *pf, *tmp_pf;
162
163 if (!list_empty(head)) {
164 list_for_each_entry_safe(pf, tmp_pf, head, list) {
165 kfree_skb(pf->skb);
166 list_del(&pf->list);
167 kfree(pf);
168 }
169 }
170 return;
171}
172
173/* frag_reassemble_skb():
174 * returns NET_RX_DROP if the operation failed - skb is left intact
175 * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
176 * or the skb could be reassembled (skb_new will point to the new packet and
177 * skb was freed)
178 */
179int batadv_frag_reassemble_skb(struct sk_buff *skb,
180 struct batadv_priv *bat_priv,
181 struct sk_buff **new_skb)
182{
183 struct batadv_orig_node *orig_node;
184 struct batadv_frag_packet_list_entry *tmp_frag_entry;
185 int ret = NET_RX_DROP;
186 struct batadv_unicast_frag_packet *unicast_packet;
187
188 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
189 *new_skb = NULL;
190
191 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->orig);
192 if (!orig_node)
193 goto out;
194
195 orig_node->last_frag_packet = jiffies;
196
197 if (list_empty(&orig_node->frag_list) &&
198 batadv_frag_create_buffer(&orig_node->frag_list)) {
199 pr_debug("couldn't create frag buffer\n");
200 goto out;
201 }
202
203 tmp_frag_entry = batadv_frag_search_packet(&orig_node->frag_list,
204 unicast_packet);
205
206 if (!tmp_frag_entry) {
207 batadv_frag_create_entry(&orig_node->frag_list, skb);
208 ret = NET_RX_SUCCESS;
209 goto out;
210 }
211
212 *new_skb = batadv_frag_merge_packet(&orig_node->frag_list,
213 tmp_frag_entry, skb);
214 /* if not, merge failed */
215 if (*new_skb)
216 ret = NET_RX_SUCCESS;
217
218out:
219 if (orig_node)
220 batadv_orig_node_free_ref(orig_node);
221 return ret;
222}
223
224int batadv_frag_send_skb(struct sk_buff *skb, struct batadv_priv *bat_priv,
225 struct batadv_hard_iface *hard_iface,
226 const uint8_t dstaddr[])
227{
228 struct batadv_unicast_packet tmp_uc, *unicast_packet;
229 struct batadv_hard_iface *primary_if;
230 struct sk_buff *frag_skb;
231 struct batadv_unicast_frag_packet *frag1, *frag2;
232 int uc_hdr_len = sizeof(*unicast_packet);
233 int ucf_hdr_len = sizeof(*frag1);
234 int data_len = skb->len - uc_hdr_len;
235 int large_tail = 0, ret = NET_RX_DROP;
236 uint16_t seqno;
237
238 primary_if = batadv_primary_if_get_selected(bat_priv);
239 if (!primary_if)
240 goto dropped;
241
242 frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
243 if (!frag_skb)
244 goto dropped;
245
246 skb->priority = TC_PRIO_CONTROL;
247 skb_reserve(frag_skb, ucf_hdr_len);
248
249 unicast_packet = (struct batadv_unicast_packet *)skb->data;
250 memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
251 skb_split(skb, frag_skb, data_len / 2 + uc_hdr_len);
252
253 if (batadv_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
254 batadv_skb_head_push(frag_skb, ucf_hdr_len) < 0)
255 goto drop_frag;
256
257 frag1 = (struct batadv_unicast_frag_packet *)skb->data;
258 frag2 = (struct batadv_unicast_frag_packet *)frag_skb->data;
259
260 memcpy(frag1, &tmp_uc, sizeof(tmp_uc));
261
262 frag1->header.ttl--;
263 frag1->header.version = BATADV_COMPAT_VERSION;
264 frag1->header.packet_type = BATADV_UNICAST_FRAG;
265
266 memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
267 memcpy(frag2, frag1, sizeof(*frag2));
268
269 if (data_len & 1)
270 large_tail = BATADV_UNI_FRAG_LARGETAIL;
271
272 frag1->flags = BATADV_UNI_FRAG_HEAD | large_tail;
273 frag2->flags = large_tail;
274
275 seqno = atomic_add_return(2, &hard_iface->frag_seqno);
276 frag1->seqno = htons(seqno - 1);
277 frag2->seqno = htons(seqno);
278
279 batadv_send_skb_packet(skb, hard_iface, dstaddr);
280 batadv_send_skb_packet(frag_skb, hard_iface, dstaddr);
281 ret = NET_RX_SUCCESS;
282 goto out;
283
284drop_frag:
285 kfree_skb(frag_skb);
286dropped:
287 kfree_skb(skb);
288out:
289 if (primary_if)
290 batadv_hardif_free_ref(primary_if);
291 return ret;
292}
293
294/**
295 * batadv_unicast_push_and_fill_skb - extends the buffer and initializes the
296 * common fields for unicast packets
297 * @skb: packet
298 * @hdr_size: amount of bytes to push at the beginning of the skb
299 * @orig_node: the destination node
300 *
301 * Returns false if the buffer extension was not possible or true otherwise
302 */
303static bool batadv_unicast_push_and_fill_skb(struct sk_buff *skb, int hdr_size,
304 struct batadv_orig_node *orig_node)
305{
306 struct batadv_unicast_packet *unicast_packet;
307 uint8_t ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
308
309 if (batadv_skb_head_push(skb, hdr_size) < 0)
310 return false;
311
312 unicast_packet = (struct batadv_unicast_packet *)skb->data;
313 unicast_packet->header.version = BATADV_COMPAT_VERSION;
314 /* batman packet type: unicast */
315 unicast_packet->header.packet_type = BATADV_UNICAST;
316 /* set unicast ttl */
317 unicast_packet->header.ttl = BATADV_TTL;
318 /* copy the destination for faster routing */
319 memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
320 /* set the destination tt version number */
321 unicast_packet->ttvn = ttvn;
322
323 return true;
324}
325
326/**
327 * batadv_unicast_prepare_skb - encapsulate an skb with a unicast header
328 * @skb: the skb containing the payload to encapsulate
329 * @orig_node: the destination node
330 *
331 * Returns false if the payload could not be encapsulated or true otherwise.
332 *
333 * This call might reallocate skb data.
334 */
335static bool batadv_unicast_prepare_skb(struct sk_buff *skb,
336 struct batadv_orig_node *orig_node)
337{
338 size_t uni_size = sizeof(struct batadv_unicast_packet);
339 return batadv_unicast_push_and_fill_skb(skb, uni_size, orig_node);
340}
341
342/**
343 * batadv_unicast_4addr_prepare_skb - encapsulate an skb with a unicast4addr
344 * header
345 * @bat_priv: the bat priv with all the soft interface information
346 * @skb: the skb containing the payload to encapsulate
347 * @orig_node: the destination node
348 * @packet_subtype: the batman 4addr packet subtype to use
349 *
350 * Returns false if the payload could not be encapsulated or true otherwise.
351 *
352 * This call might reallocate skb data.
353 */
354bool batadv_unicast_4addr_prepare_skb(struct batadv_priv *bat_priv,
355 struct sk_buff *skb,
356 struct batadv_orig_node *orig,
357 int packet_subtype)
358{
359 struct batadv_hard_iface *primary_if;
360 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
361 bool ret = false;
362
363 primary_if = batadv_primary_if_get_selected(bat_priv);
364 if (!primary_if)
365 goto out;
366
367 /* pull the header space and fill the unicast_packet substructure.
368 * We can do that because the first member of the unicast_4addr_packet
369 * is of type struct unicast_packet
370 */
371 if (!batadv_unicast_push_and_fill_skb(skb,
372 sizeof(*unicast_4addr_packet),
373 orig))
374 goto out;
375
376 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
377 unicast_4addr_packet->u.header.packet_type = BATADV_UNICAST_4ADDR;
378 memcpy(unicast_4addr_packet->src, primary_if->net_dev->dev_addr,
379 ETH_ALEN);
380 unicast_4addr_packet->subtype = packet_subtype;
381 unicast_4addr_packet->reserved = 0;
382
383 ret = true;
384out:
385 if (primary_if)
386 batadv_hardif_free_ref(primary_if);
387 return ret;
388}
389
390/**
391 * batadv_unicast_generic_send_skb - send an skb as unicast
392 * @bat_priv: the bat priv with all the soft interface information
393 * @skb: payload to send
394 * @packet_type: the batman unicast packet type to use
395 * @packet_subtype: the batman packet subtype. It is ignored if packet_type is
396 * not BATADV_UNICAT_4ADDR
397 *
398 * Returns 1 in case of error or 0 otherwise
399 */
400int batadv_unicast_generic_send_skb(struct batadv_priv *bat_priv,
401 struct sk_buff *skb, int packet_type,
402 int packet_subtype)
403{
404 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
405 struct batadv_unicast_packet *unicast_packet;
406 struct batadv_orig_node *orig_node;
407 struct batadv_neigh_node *neigh_node;
408 int data_len = skb->len;
409 int ret = NET_RX_DROP;
410 unsigned int dev_mtu, header_len;
411
412 /* get routing information */
413 if (is_multicast_ether_addr(ethhdr->h_dest)) {
414 orig_node = batadv_gw_get_selected_orig(bat_priv);
415 if (orig_node)
416 goto find_router;
417 }
418
419 /* check for tt host - increases orig_node refcount.
420 * returns NULL in case of AP isolation
421 */
422 orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
423 ethhdr->h_dest);
424
425find_router:
426 /* find_router():
427 * - if orig_node is NULL it returns NULL
428 * - increases neigh_nodes refcount if found.
429 */
430 neigh_node = batadv_find_router(bat_priv, orig_node, NULL);
431
432 if (!neigh_node)
433 goto out;
434
435 switch (packet_type) {
436 case BATADV_UNICAST:
437 if (!batadv_unicast_prepare_skb(skb, orig_node))
438 goto out;
439
440 header_len = sizeof(struct batadv_unicast_packet);
441 break;
442 case BATADV_UNICAST_4ADDR:
443 if (!batadv_unicast_4addr_prepare_skb(bat_priv, skb, orig_node,
444 packet_subtype))
445 goto out;
446
447 header_len = sizeof(struct batadv_unicast_4addr_packet);
448 break;
449 default:
450 /* this function supports UNICAST and UNICAST_4ADDR only. It
451 * should never be invoked with any other packet type
452 */
453 goto out;
454 }
455
456 ethhdr = (struct ethhdr *)(skb->data + header_len);
457 unicast_packet = (struct batadv_unicast_packet *)skb->data;
458
459 /* inform the destination node that we are still missing a correct route
460 * for this client. The destination will receive this packet and will
461 * try to reroute it because the ttvn contained in the header is less
462 * than the current one
463 */
464 if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest))
465 unicast_packet->ttvn = unicast_packet->ttvn - 1;
466
467 dev_mtu = neigh_node->if_incoming->net_dev->mtu;
468 /* fragmentation mechanism only works for UNICAST (now) */
469 if (packet_type == BATADV_UNICAST &&
470 atomic_read(&bat_priv->fragmentation) &&
471 data_len + sizeof(*unicast_packet) > dev_mtu) {
472 /* send frag skb decreases ttl */
473 unicast_packet->header.ttl++;
474 ret = batadv_frag_send_skb(skb, bat_priv,
475 neigh_node->if_incoming,
476 neigh_node->addr);
477 goto out;
478 }
479
480 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
481 ret = 0;
482
483out:
484 if (neigh_node)
485 batadv_neigh_node_free_ref(neigh_node);
486 if (orig_node)
487 batadv_orig_node_free_ref(orig_node);
488 if (ret == NET_RX_DROP)
489 kfree_skb(skb);
490 return ret;
491}
diff --git a/net/batman-adv/unicast.h b/net/batman-adv/unicast.h
deleted file mode 100644
index 429cf8a4a31e..000000000000
--- a/net/batman-adv/unicast.h
+++ /dev/null
@@ -1,92 +0,0 @@
1/* Copyright (C) 2010-2013 B.A.T.M.A.N. contributors:
2 *
3 * Andreas Langer
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#ifndef _NET_BATMAN_ADV_UNICAST_H_
21#define _NET_BATMAN_ADV_UNICAST_H_
22
23#include "packet.h"
24
25#define BATADV_FRAG_TIMEOUT 10000 /* purge frag list entries after time in ms */
26#define BATADV_FRAG_BUFFER_SIZE 6 /* number of list elements in buffer */
27
28int batadv_frag_reassemble_skb(struct sk_buff *skb,
29 struct batadv_priv *bat_priv,
30 struct sk_buff **new_skb);
31void batadv_frag_list_free(struct list_head *head);
32int batadv_frag_send_skb(struct sk_buff *skb, struct batadv_priv *bat_priv,
33 struct batadv_hard_iface *hard_iface,
34 const uint8_t dstaddr[]);
35bool batadv_unicast_4addr_prepare_skb(struct batadv_priv *bat_priv,
36 struct sk_buff *skb,
37 struct batadv_orig_node *orig_node,
38 int packet_subtype);
39int batadv_unicast_generic_send_skb(struct batadv_priv *bat_priv,
40 struct sk_buff *skb, int packet_type,
41 int packet_subtype);
42
43
44/**
45 * batadv_unicast_send_skb - send the skb encapsulated in a unicast packet
46 * @bat_priv: the bat priv with all the soft interface information
47 * @skb: the payload to send
48 */
49static inline int batadv_unicast_send_skb(struct batadv_priv *bat_priv,
50 struct sk_buff *skb)
51{
52 return batadv_unicast_generic_send_skb(bat_priv, skb, BATADV_UNICAST,
53 0);
54}
55
56/**
57 * batadv_unicast_send_skb - send the skb encapsulated in a unicast4addr packet
58 * @bat_priv: the bat priv with all the soft interface information
59 * @skb: the payload to send
60 * @packet_subtype: the batman 4addr packet subtype to use
61 */
62static inline int batadv_unicast_4addr_send_skb(struct batadv_priv *bat_priv,
63 struct sk_buff *skb,
64 int packet_subtype)
65{
66 return batadv_unicast_generic_send_skb(bat_priv, skb,
67 BATADV_UNICAST_4ADDR,
68 packet_subtype);
69}
70
71static inline int batadv_frag_can_reassemble(const struct sk_buff *skb, int mtu)
72{
73 const struct batadv_unicast_frag_packet *unicast_packet;
74 int uneven_correction = 0;
75 unsigned int merged_size;
76
77 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
78
79 if (unicast_packet->flags & BATADV_UNI_FRAG_LARGETAIL) {
80 if (unicast_packet->flags & BATADV_UNI_FRAG_HEAD)
81 uneven_correction = 1;
82 else
83 uneven_correction = -1;
84 }
85
86 merged_size = (skb->len - sizeof(*unicast_packet)) * 2;
87 merged_size += sizeof(struct batadv_unicast_packet) + uneven_correction;
88
89 return merged_size <= mtu;
90}
91
92#endif /* _NET_BATMAN_ADV_UNICAST_H_ */
diff --git a/net/batman-adv/vis.c b/net/batman-adv/vis.c
deleted file mode 100644
index d8ea31a58457..000000000000
--- a/net/batman-adv/vis.c
+++ /dev/null
@@ -1,938 +0,0 @@
1/* Copyright (C) 2008-2013 B.A.T.M.A.N. contributors:
2 *
3 * Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#include "main.h"
21#include "send.h"
22#include "translation-table.h"
23#include "vis.h"
24#include "soft-interface.h"
25#include "hard-interface.h"
26#include "hash.h"
27#include "originator.h"
28
29#define BATADV_MAX_VIS_PACKET_SIZE 1000
30
31/* hash class keys */
32static struct lock_class_key batadv_vis_hash_lock_class_key;
33
34/* free the info */
35static void batadv_free_info(struct kref *ref)
36{
37 struct batadv_vis_info *info;
38 struct batadv_priv *bat_priv;
39 struct batadv_vis_recvlist_node *entry, *tmp;
40
41 info = container_of(ref, struct batadv_vis_info, refcount);
42 bat_priv = info->bat_priv;
43
44 list_del_init(&info->send_list);
45 spin_lock_bh(&bat_priv->vis.list_lock);
46 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
47 list_del(&entry->list);
48 kfree(entry);
49 }
50
51 spin_unlock_bh(&bat_priv->vis.list_lock);
52 kfree_skb(info->skb_packet);
53 kfree(info);
54}
55
56/* Compare two vis packets, used by the hashing algorithm */
57static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
58{
59 const struct batadv_vis_info *d1, *d2;
60 const struct batadv_vis_packet *p1, *p2;
61
62 d1 = container_of(node, struct batadv_vis_info, hash_entry);
63 d2 = data2;
64 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
65 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
66 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
67}
68
69/* hash function to choose an entry in a hash table of given size
70 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
71 */
72static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
73{
74 const struct batadv_vis_info *vis_info = data;
75 const struct batadv_vis_packet *packet;
76 const unsigned char *key;
77 uint32_t hash = 0;
78 size_t i;
79
80 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
81 key = packet->vis_orig;
82 for (i = 0; i < ETH_ALEN; i++) {
83 hash += key[i];
84 hash += (hash << 10);
85 hash ^= (hash >> 6);
86 }
87
88 hash += (hash << 3);
89 hash ^= (hash >> 11);
90 hash += (hash << 15);
91
92 return hash % size;
93}
94
95static struct batadv_vis_info *
96batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
97{
98 struct batadv_hashtable *hash = bat_priv->vis.hash;
99 struct hlist_head *head;
100 struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
101 uint32_t index;
102
103 if (!hash)
104 return NULL;
105
106 index = batadv_vis_info_choose(data, hash->size);
107 head = &hash->table[index];
108
109 rcu_read_lock();
110 hlist_for_each_entry_rcu(vis_info, head, hash_entry) {
111 if (!batadv_vis_info_cmp(&vis_info->hash_entry, data))
112 continue;
113
114 vis_info_tmp = vis_info;
115 break;
116 }
117 rcu_read_unlock();
118
119 return vis_info_tmp;
120}
121
122/* insert interface to the list of interfaces of one originator, if it
123 * does not already exist in the list
124 */
125static void batadv_vis_data_insert_interface(const uint8_t *interface,
126 struct hlist_head *if_list,
127 bool primary)
128{
129 struct batadv_vis_if_list_entry *entry;
130
131 hlist_for_each_entry(entry, if_list, list) {
132 if (batadv_compare_eth(entry->addr, interface))
133 return;
134 }
135
136 /* it's a new address, add it to the list */
137 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
138 if (!entry)
139 return;
140 memcpy(entry->addr, interface, ETH_ALEN);
141 entry->primary = primary;
142 hlist_add_head(&entry->list, if_list);
143}
144
145static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
146 const struct hlist_head *if_list)
147{
148 struct batadv_vis_if_list_entry *entry;
149
150 hlist_for_each_entry(entry, if_list, list) {
151 if (entry->primary)
152 seq_puts(seq, "PRIMARY, ");
153 else
154 seq_printf(seq, "SEC %pM, ", entry->addr);
155 }
156}
157
158/* read an entry */
159static ssize_t
160batadv_vis_data_read_entry(struct seq_file *seq,
161 const struct batadv_vis_info_entry *entry,
162 const uint8_t *src, bool primary)
163{
164 if (primary && entry->quality == 0)
165 return seq_printf(seq, "TT %pM, ", entry->dest);
166 else if (batadv_compare_eth(entry->src, src))
167 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
168 entry->quality);
169
170 return 0;
171}
172
173static void
174batadv_vis_data_insert_interfaces(struct hlist_head *list,
175 struct batadv_vis_packet *packet,
176 struct batadv_vis_info_entry *entries)
177{
178 int i;
179
180 for (i = 0; i < packet->entries; i++) {
181 if (entries[i].quality == 0)
182 continue;
183
184 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
185 continue;
186
187 batadv_vis_data_insert_interface(entries[i].src, list, false);
188 }
189}
190
191static void batadv_vis_data_read_entries(struct seq_file *seq,
192 struct hlist_head *list,
193 struct batadv_vis_packet *packet,
194 struct batadv_vis_info_entry *entries)
195{
196 int i;
197 struct batadv_vis_if_list_entry *entry;
198
199 hlist_for_each_entry(entry, list, list) {
200 seq_printf(seq, "%pM,", entry->addr);
201
202 for (i = 0; i < packet->entries; i++)
203 batadv_vis_data_read_entry(seq, &entries[i],
204 entry->addr, entry->primary);
205
206 /* add primary/secondary records */
207 if (batadv_compare_eth(entry->addr, packet->vis_orig))
208 batadv_vis_data_read_prim_sec(seq, list);
209
210 seq_puts(seq, "\n");
211 }
212}
213
214static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
215 const struct hlist_head *head)
216{
217 struct batadv_vis_info *info;
218 struct batadv_vis_packet *packet;
219 uint8_t *entries_pos;
220 struct batadv_vis_info_entry *entries;
221 struct batadv_vis_if_list_entry *entry;
222 struct hlist_node *n;
223
224 HLIST_HEAD(vis_if_list);
225
226 hlist_for_each_entry_rcu(info, head, hash_entry) {
227 packet = (struct batadv_vis_packet *)info->skb_packet->data;
228 entries_pos = (uint8_t *)packet + sizeof(*packet);
229 entries = (struct batadv_vis_info_entry *)entries_pos;
230
231 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
232 true);
233 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
234 entries);
235 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
236 entries);
237
238 hlist_for_each_entry_safe(entry, n, &vis_if_list, list) {
239 hlist_del(&entry->list);
240 kfree(entry);
241 }
242 }
243}
244
245int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
246{
247 struct batadv_hard_iface *primary_if;
248 struct hlist_head *head;
249 struct net_device *net_dev = (struct net_device *)seq->private;
250 struct batadv_priv *bat_priv = netdev_priv(net_dev);
251 struct batadv_hashtable *hash = bat_priv->vis.hash;
252 uint32_t i;
253 int ret = 0;
254 int vis_server = atomic_read(&bat_priv->vis_mode);
255
256 primary_if = batadv_primary_if_get_selected(bat_priv);
257 if (!primary_if)
258 goto out;
259
260 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
261 goto out;
262
263 spin_lock_bh(&bat_priv->vis.hash_lock);
264 for (i = 0; i < hash->size; i++) {
265 head = &hash->table[i];
266 batadv_vis_seq_print_text_bucket(seq, head);
267 }
268 spin_unlock_bh(&bat_priv->vis.hash_lock);
269
270out:
271 if (primary_if)
272 batadv_hardif_free_ref(primary_if);
273 return ret;
274}
275
276/* add the info packet to the send list, if it was not
277 * already linked in.
278 */
279static void batadv_send_list_add(struct batadv_priv *bat_priv,
280 struct batadv_vis_info *info)
281{
282 if (list_empty(&info->send_list)) {
283 kref_get(&info->refcount);
284 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
285 }
286}
287
288/* delete the info packet from the send list, if it was
289 * linked in.
290 */
291static void batadv_send_list_del(struct batadv_vis_info *info)
292{
293 if (!list_empty(&info->send_list)) {
294 list_del_init(&info->send_list);
295 kref_put(&info->refcount, batadv_free_info);
296 }
297}
298
299/* tries to add one entry to the receive list. */
300static void batadv_recv_list_add(struct batadv_priv *bat_priv,
301 struct list_head *recv_list, const char *mac)
302{
303 struct batadv_vis_recvlist_node *entry;
304
305 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
306 if (!entry)
307 return;
308
309 memcpy(entry->mac, mac, ETH_ALEN);
310 spin_lock_bh(&bat_priv->vis.list_lock);
311 list_add_tail(&entry->list, recv_list);
312 spin_unlock_bh(&bat_priv->vis.list_lock);
313}
314
315/* returns 1 if this mac is in the recv_list */
316static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
317 const struct list_head *recv_list,
318 const char *mac)
319{
320 const struct batadv_vis_recvlist_node *entry;
321
322 spin_lock_bh(&bat_priv->vis.list_lock);
323 list_for_each_entry(entry, recv_list, list) {
324 if (batadv_compare_eth(entry->mac, mac)) {
325 spin_unlock_bh(&bat_priv->vis.list_lock);
326 return 1;
327 }
328 }
329 spin_unlock_bh(&bat_priv->vis.list_lock);
330 return 0;
331}
332
333/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
334 * broken.. ). vis hash must be locked outside. is_new is set when the packet
335 * is newer than old entries in the hash.
336 */
337static struct batadv_vis_info *
338batadv_add_packet(struct batadv_priv *bat_priv,
339 struct batadv_vis_packet *vis_packet, int vis_info_len,
340 int *is_new, int make_broadcast)
341{
342 struct batadv_vis_info *info, *old_info;
343 struct batadv_vis_packet *search_packet, *old_packet;
344 struct batadv_vis_info search_elem;
345 struct batadv_vis_packet *packet;
346 struct sk_buff *tmp_skb;
347 int hash_added;
348 size_t len;
349 size_t max_entries;
350
351 *is_new = 0;
352 /* sanity check */
353 if (!bat_priv->vis.hash)
354 return NULL;
355
356 /* see if the packet is already in vis_hash */
357 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
358 if (!search_elem.skb_packet)
359 return NULL;
360 len = sizeof(*search_packet);
361 tmp_skb = search_elem.skb_packet;
362 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
363
364 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
365 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
366 kfree_skb(search_elem.skb_packet);
367
368 if (old_info) {
369 tmp_skb = old_info->skb_packet;
370 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
371 if (!batadv_seq_after(ntohl(vis_packet->seqno),
372 ntohl(old_packet->seqno))) {
373 if (old_packet->seqno == vis_packet->seqno) {
374 batadv_recv_list_add(bat_priv,
375 &old_info->recv_list,
376 vis_packet->sender_orig);
377 return old_info;
378 } else {
379 /* newer packet is already in hash. */
380 return NULL;
381 }
382 }
383 /* remove old entry */
384 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
385 batadv_vis_info_choose, old_info);
386 batadv_send_list_del(old_info);
387 kref_put(&old_info->refcount, batadv_free_info);
388 }
389
390 info = kmalloc(sizeof(*info), GFP_ATOMIC);
391 if (!info)
392 return NULL;
393
394 len = sizeof(*packet) + vis_info_len;
395 info->skb_packet = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
396 if (!info->skb_packet) {
397 kfree(info);
398 return NULL;
399 }
400 info->skb_packet->priority = TC_PRIO_CONTROL;
401 skb_reserve(info->skb_packet, ETH_HLEN);
402 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
403
404 kref_init(&info->refcount);
405 INIT_LIST_HEAD(&info->send_list);
406 INIT_LIST_HEAD(&info->recv_list);
407 info->first_seen = jiffies;
408 info->bat_priv = bat_priv;
409 memcpy(packet, vis_packet, len);
410
411 /* initialize and add new packet. */
412 *is_new = 1;
413
414 /* Make it a broadcast packet, if required */
415 if (make_broadcast)
416 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
417
418 /* repair if entries is longer than packet. */
419 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
420 if (packet->entries > max_entries)
421 packet->entries = max_entries;
422
423 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
424
425 /* try to add it */
426 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
427 batadv_vis_info_choose, info,
428 &info->hash_entry);
429 if (hash_added != 0) {
430 /* did not work (for some reason) */
431 kref_put(&info->refcount, batadv_free_info);
432 info = NULL;
433 }
434
435 return info;
436}
437
438/* handle the server sync packet, forward if needed. */
439void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
440 struct batadv_vis_packet *vis_packet,
441 int vis_info_len)
442{
443 struct batadv_vis_info *info;
444 int is_new, make_broadcast;
445 int vis_server = atomic_read(&bat_priv->vis_mode);
446
447 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
448
449 spin_lock_bh(&bat_priv->vis.hash_lock);
450 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
451 &is_new, make_broadcast);
452 if (!info)
453 goto end;
454
455 /* only if we are server ourselves and packet is newer than the one in
456 * hash.
457 */
458 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
459 batadv_send_list_add(bat_priv, info);
460end:
461 spin_unlock_bh(&bat_priv->vis.hash_lock);
462}
463
464/* handle an incoming client update packet and schedule forward if needed. */
465void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
466 struct batadv_vis_packet *vis_packet,
467 int vis_info_len)
468{
469 struct batadv_vis_info *info;
470 struct batadv_vis_packet *packet;
471 int is_new;
472 int vis_server = atomic_read(&bat_priv->vis_mode);
473 int are_target = 0;
474
475 /* clients shall not broadcast. */
476 if (is_broadcast_ether_addr(vis_packet->target_orig))
477 return;
478
479 /* Are we the target for this VIS packet? */
480 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
481 batadv_is_my_mac(bat_priv, vis_packet->target_orig))
482 are_target = 1;
483
484 spin_lock_bh(&bat_priv->vis.hash_lock);
485 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
486 &is_new, are_target);
487
488 if (!info)
489 goto end;
490 /* note that outdated packets will be dropped at this point. */
491
492 packet = (struct batadv_vis_packet *)info->skb_packet->data;
493
494 /* send only if we're the target server or ... */
495 if (are_target && is_new) {
496 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
497 batadv_send_list_add(bat_priv, info);
498
499 /* ... we're not the recipient (and thus need to forward). */
500 } else if (!batadv_is_my_mac(bat_priv, packet->target_orig)) {
501 batadv_send_list_add(bat_priv, info);
502 }
503
504end:
505 spin_unlock_bh(&bat_priv->vis.hash_lock);
506}
507
508/* Walk the originators and find the VIS server with the best tq. Set the packet
509 * address to its address and return the best_tq.
510 *
511 * Must be called with the originator hash locked
512 */
513static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
514 struct batadv_vis_info *info)
515{
516 struct batadv_hashtable *hash = bat_priv->orig_hash;
517 struct batadv_neigh_node *router;
518 struct hlist_head *head;
519 struct batadv_orig_node *orig_node;
520 struct batadv_vis_packet *packet;
521 int best_tq = -1;
522 uint32_t i;
523
524 packet = (struct batadv_vis_packet *)info->skb_packet->data;
525
526 for (i = 0; i < hash->size; i++) {
527 head = &hash->table[i];
528
529 rcu_read_lock();
530 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
531 router = batadv_orig_node_get_router(orig_node);
532 if (!router)
533 continue;
534
535 if ((orig_node->flags & BATADV_VIS_SERVER) &&
536 (router->tq_avg > best_tq)) {
537 best_tq = router->tq_avg;
538 memcpy(packet->target_orig, orig_node->orig,
539 ETH_ALEN);
540 }
541 batadv_neigh_node_free_ref(router);
542 }
543 rcu_read_unlock();
544 }
545
546 return best_tq;
547}
548
549/* Return true if the vis packet is full. */
550static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
551{
552 const struct batadv_vis_packet *packet;
553 size_t num;
554
555 packet = (struct batadv_vis_packet *)info->skb_packet->data;
556 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
557
558 if (num < packet->entries + 1)
559 return true;
560 return false;
561}
562
563/* generates a packet of own vis data,
564 * returns 0 on success, -1 if no packet could be generated
565 */
566static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
567{
568 struct batadv_hashtable *hash = bat_priv->orig_hash;
569 struct hlist_head *head;
570 struct batadv_orig_node *orig_node;
571 struct batadv_neigh_node *router;
572 struct batadv_vis_info *info = bat_priv->vis.my_info;
573 struct batadv_vis_packet *packet;
574 struct batadv_vis_info_entry *entry;
575 struct batadv_tt_common_entry *tt_common_entry;
576 uint8_t *packet_pos;
577 int best_tq = -1;
578 uint32_t i;
579
580 info->first_seen = jiffies;
581 packet = (struct batadv_vis_packet *)info->skb_packet->data;
582 packet->vis_type = atomic_read(&bat_priv->vis_mode);
583
584 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
585 packet->header.ttl = BATADV_TTL;
586 packet->seqno = htonl(ntohl(packet->seqno) + 1);
587 packet->entries = 0;
588 packet->reserved = 0;
589 skb_trim(info->skb_packet, sizeof(*packet));
590
591 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
592 best_tq = batadv_find_best_vis_server(bat_priv, info);
593
594 if (best_tq < 0)
595 return best_tq;
596 }
597
598 for (i = 0; i < hash->size; i++) {
599 head = &hash->table[i];
600
601 rcu_read_lock();
602 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
603 router = batadv_orig_node_get_router(orig_node);
604 if (!router)
605 continue;
606
607 if (!batadv_compare_eth(router->addr, orig_node->orig))
608 goto next;
609
610 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
611 goto next;
612
613 if (router->tq_avg < 1)
614 goto next;
615
616 /* fill one entry into buffer. */
617 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
618 entry = (struct batadv_vis_info_entry *)packet_pos;
619 memcpy(entry->src,
620 router->if_incoming->net_dev->dev_addr,
621 ETH_ALEN);
622 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
623 entry->quality = router->tq_avg;
624 packet->entries++;
625
626next:
627 batadv_neigh_node_free_ref(router);
628
629 if (batadv_vis_packet_full(info))
630 goto unlock;
631 }
632 rcu_read_unlock();
633 }
634
635 hash = bat_priv->tt.local_hash;
636
637 for (i = 0; i < hash->size; i++) {
638 head = &hash->table[i];
639
640 rcu_read_lock();
641 hlist_for_each_entry_rcu(tt_common_entry, head,
642 hash_entry) {
643 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
644 entry = (struct batadv_vis_info_entry *)packet_pos;
645 memset(entry->src, 0, ETH_ALEN);
646 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
647 entry->quality = 0; /* 0 means TT */
648 packet->entries++;
649
650 if (batadv_vis_packet_full(info))
651 goto unlock;
652 }
653 rcu_read_unlock();
654 }
655
656 return 0;
657
658unlock:
659 rcu_read_unlock();
660 return 0;
661}
662
663/* free old vis packets. Must be called with this vis_hash_lock
664 * held
665 */
666static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
667{
668 uint32_t i;
669 struct batadv_hashtable *hash = bat_priv->vis.hash;
670 struct hlist_node *node_tmp;
671 struct hlist_head *head;
672 struct batadv_vis_info *info;
673
674 for (i = 0; i < hash->size; i++) {
675 head = &hash->table[i];
676
677 hlist_for_each_entry_safe(info, node_tmp,
678 head, hash_entry) {
679 /* never purge own data. */
680 if (info == bat_priv->vis.my_info)
681 continue;
682
683 if (batadv_has_timed_out(info->first_seen,
684 BATADV_VIS_TIMEOUT)) {
685 hlist_del(&info->hash_entry);
686 batadv_send_list_del(info);
687 kref_put(&info->refcount, batadv_free_info);
688 }
689 }
690 }
691}
692
693static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
694 struct batadv_vis_info *info)
695{
696 struct batadv_hashtable *hash = bat_priv->orig_hash;
697 struct hlist_head *head;
698 struct batadv_orig_node *orig_node;
699 struct batadv_vis_packet *packet;
700 struct sk_buff *skb;
701 uint32_t i, res;
702
703
704 packet = (struct batadv_vis_packet *)info->skb_packet->data;
705
706 /* send to all routers in range. */
707 for (i = 0; i < hash->size; i++) {
708 head = &hash->table[i];
709
710 rcu_read_lock();
711 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
712 /* if it's a vis server and reachable, send it. */
713 if (!(orig_node->flags & BATADV_VIS_SERVER))
714 continue;
715
716 /* don't send it if we already received the packet from
717 * this node.
718 */
719 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
720 orig_node->orig))
721 continue;
722
723 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
724 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
725 if (!skb)
726 continue;
727
728 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
729 if (res == NET_XMIT_DROP)
730 kfree_skb(skb);
731 }
732 rcu_read_unlock();
733 }
734}
735
736static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
737 struct batadv_vis_info *info)
738{
739 struct batadv_orig_node *orig_node;
740 struct sk_buff *skb;
741 struct batadv_vis_packet *packet;
742
743 packet = (struct batadv_vis_packet *)info->skb_packet->data;
744
745 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
746 if (!orig_node)
747 goto out;
748
749 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
750 if (!skb)
751 goto out;
752
753 if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
754 kfree_skb(skb);
755
756out:
757 if (orig_node)
758 batadv_orig_node_free_ref(orig_node);
759}
760
761/* only send one vis packet. called from batadv_send_vis_packets() */
762static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
763 struct batadv_vis_info *info)
764{
765 struct batadv_hard_iface *primary_if;
766 struct batadv_vis_packet *packet;
767
768 primary_if = batadv_primary_if_get_selected(bat_priv);
769 if (!primary_if)
770 goto out;
771
772 packet = (struct batadv_vis_packet *)info->skb_packet->data;
773 if (packet->header.ttl < 2) {
774 pr_debug("Error - can't send vis packet: ttl exceeded\n");
775 goto out;
776 }
777
778 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
779 packet->header.ttl--;
780
781 if (is_broadcast_ether_addr(packet->target_orig))
782 batadv_broadcast_vis_packet(bat_priv, info);
783 else
784 batadv_unicast_vis_packet(bat_priv, info);
785 packet->header.ttl++; /* restore TTL */
786
787out:
788 if (primary_if)
789 batadv_hardif_free_ref(primary_if);
790}
791
792/* called from timer; send (and maybe generate) vis packet. */
793static void batadv_send_vis_packets(struct work_struct *work)
794{
795 struct delayed_work *delayed_work;
796 struct batadv_priv *bat_priv;
797 struct batadv_priv_vis *priv_vis;
798 struct batadv_vis_info *info;
799
800 delayed_work = container_of(work, struct delayed_work, work);
801 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
802 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
803 spin_lock_bh(&bat_priv->vis.hash_lock);
804 batadv_purge_vis_packets(bat_priv);
805
806 if (batadv_generate_vis_packet(bat_priv) == 0) {
807 /* schedule if generation was successful */
808 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
809 }
810
811 while (!list_empty(&bat_priv->vis.send_list)) {
812 info = list_first_entry(&bat_priv->vis.send_list,
813 typeof(*info), send_list);
814
815 kref_get(&info->refcount);
816 spin_unlock_bh(&bat_priv->vis.hash_lock);
817
818 batadv_send_vis_packet(bat_priv, info);
819
820 spin_lock_bh(&bat_priv->vis.hash_lock);
821 batadv_send_list_del(info);
822 kref_put(&info->refcount, batadv_free_info);
823 }
824 spin_unlock_bh(&bat_priv->vis.hash_lock);
825
826 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
827 msecs_to_jiffies(BATADV_VIS_INTERVAL));
828}
829
830/* init the vis server. this may only be called when if_list is already
831 * initialized (e.g. bat0 is initialized, interfaces have been added)
832 */
833int batadv_vis_init(struct batadv_priv *bat_priv)
834{
835 struct batadv_vis_packet *packet;
836 int hash_added;
837 unsigned int len;
838 unsigned long first_seen;
839 struct sk_buff *tmp_skb;
840
841 if (bat_priv->vis.hash)
842 return 0;
843
844 spin_lock_bh(&bat_priv->vis.hash_lock);
845
846 bat_priv->vis.hash = batadv_hash_new(256);
847 if (!bat_priv->vis.hash) {
848 pr_err("Can't initialize vis_hash\n");
849 goto err;
850 }
851
852 batadv_hash_set_lock_class(bat_priv->vis.hash,
853 &batadv_vis_hash_lock_class_key);
854
855 bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
856 if (!bat_priv->vis.my_info)
857 goto err;
858
859 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
860 bat_priv->vis.my_info->skb_packet = netdev_alloc_skb_ip_align(NULL,
861 len);
862 if (!bat_priv->vis.my_info->skb_packet)
863 goto free_info;
864
865 bat_priv->vis.my_info->skb_packet->priority = TC_PRIO_CONTROL;
866 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN);
867 tmp_skb = bat_priv->vis.my_info->skb_packet;
868 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
869
870 /* prefill the vis info */
871 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
872 bat_priv->vis.my_info->first_seen = first_seen;
873 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
874 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
875 kref_init(&bat_priv->vis.my_info->refcount);
876 bat_priv->vis.my_info->bat_priv = bat_priv;
877 packet->header.version = BATADV_COMPAT_VERSION;
878 packet->header.packet_type = BATADV_VIS;
879 packet->header.ttl = BATADV_TTL;
880 packet->seqno = 0;
881 packet->reserved = 0;
882 packet->entries = 0;
883
884 INIT_LIST_HEAD(&bat_priv->vis.send_list);
885
886 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
887 batadv_vis_info_choose,
888 bat_priv->vis.my_info,
889 &bat_priv->vis.my_info->hash_entry);
890 if (hash_added != 0) {
891 pr_err("Can't add own vis packet into hash\n");
892 /* not in hash, need to remove it manually. */
893 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
894 goto err;
895 }
896
897 spin_unlock_bh(&bat_priv->vis.hash_lock);
898
899 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
900 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
901 msecs_to_jiffies(BATADV_VIS_INTERVAL));
902
903 return 0;
904
905free_info:
906 kfree(bat_priv->vis.my_info);
907 bat_priv->vis.my_info = NULL;
908err:
909 spin_unlock_bh(&bat_priv->vis.hash_lock);
910 batadv_vis_quit(bat_priv);
911 return -ENOMEM;
912}
913
914/* Decrease the reference count on a hash item info */
915static void batadv_free_info_ref(struct hlist_node *node, void *arg)
916{
917 struct batadv_vis_info *info;
918
919 info = container_of(node, struct batadv_vis_info, hash_entry);
920 batadv_send_list_del(info);
921 kref_put(&info->refcount, batadv_free_info);
922}
923
924/* shutdown vis-server */
925void batadv_vis_quit(struct batadv_priv *bat_priv)
926{
927 if (!bat_priv->vis.hash)
928 return;
929
930 cancel_delayed_work_sync(&bat_priv->vis.work);
931
932 spin_lock_bh(&bat_priv->vis.hash_lock);
933 /* properly remove, kill timers ... */
934 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
935 bat_priv->vis.hash = NULL;
936 bat_priv->vis.my_info = NULL;
937 spin_unlock_bh(&bat_priv->vis.hash_lock);
938}
diff --git a/net/batman-adv/vis.h b/net/batman-adv/vis.h
deleted file mode 100644
index ad92b0e3c230..000000000000
--- a/net/batman-adv/vis.h
+++ /dev/null
@@ -1,36 +0,0 @@
1/* Copyright (C) 2008-2013 B.A.T.M.A.N. contributors:
2 *
3 * Simon Wunderlich, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20#ifndef _NET_BATMAN_ADV_VIS_H_
21#define _NET_BATMAN_ADV_VIS_H_
22
23/* timeout of vis packets in milliseconds */
24#define BATADV_VIS_TIMEOUT 200000
25
26int batadv_vis_seq_print_text(struct seq_file *seq, void *offset);
27void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
28 struct batadv_vis_packet *vis_packet,
29 int vis_info_len);
30void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
31 struct batadv_vis_packet *vis_packet,
32 int vis_info_len);
33int batadv_vis_init(struct batadv_priv *bat_priv);
34void batadv_vis_quit(struct batadv_priv *bat_priv);
35
36#endif /* _NET_BATMAN_ADV_VIS_H_ */
diff --git a/net/bluetooth/Makefile b/net/bluetooth/Makefile
index dea6a287daca..6a791e73e39d 100644
--- a/net/bluetooth/Makefile
+++ b/net/bluetooth/Makefile
@@ -11,3 +11,5 @@ obj-$(CONFIG_BT_HIDP) += hidp/
11bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \ 11bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \
12 hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \ 12 hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \
13 a2mp.o amp.o 13 a2mp.o amp.o
14
15subdir-ccflags-y += -D__CHECK_ENDIAN__
diff --git a/net/bluetooth/a2mp.c b/net/bluetooth/a2mp.c
index 17f33a62f6db..efcd108822c4 100644
--- a/net/bluetooth/a2mp.c
+++ b/net/bluetooth/a2mp.c
@@ -15,8 +15,9 @@
15#include <net/bluetooth/bluetooth.h> 15#include <net/bluetooth/bluetooth.h>
16#include <net/bluetooth/hci_core.h> 16#include <net/bluetooth/hci_core.h>
17#include <net/bluetooth/l2cap.h> 17#include <net/bluetooth/l2cap.h>
18#include <net/bluetooth/a2mp.h> 18
19#include <net/bluetooth/amp.h> 19#include "a2mp.h"
20#include "amp.h"
20 21
21/* Global AMP Manager list */ 22/* Global AMP Manager list */
22LIST_HEAD(amp_mgr_list); 23LIST_HEAD(amp_mgr_list);
@@ -75,33 +76,26 @@ u8 __next_ident(struct amp_mgr *mgr)
75 return mgr->ident; 76 return mgr->ident;
76} 77}
77 78
78static inline void __a2mp_cl_bredr(struct a2mp_cl *cl)
79{
80 cl->id = 0;
81 cl->type = 0;
82 cl->status = 1;
83}
84
85/* hci_dev_list shall be locked */ 79/* hci_dev_list shall be locked */
86static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl, u8 num_ctrl) 80static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
87{ 81{
88 int i = 0;
89 struct hci_dev *hdev; 82 struct hci_dev *hdev;
83 int i = 1;
90 84
91 __a2mp_cl_bredr(cl); 85 cl[0].id = AMP_ID_BREDR;
86 cl[0].type = AMP_TYPE_BREDR;
87 cl[0].status = AMP_STATUS_BLUETOOTH_ONLY;
92 88
93 list_for_each_entry(hdev, &hci_dev_list, list) { 89 list_for_each_entry(hdev, &hci_dev_list, list) {
94 /* Iterate through AMP controllers */ 90 if (hdev->dev_type == HCI_AMP) {
95 if (hdev->id == HCI_BREDR_ID) 91 cl[i].id = hdev->id;
96 continue; 92 cl[i].type = hdev->amp_type;
97 93 if (test_bit(HCI_UP, &hdev->flags))
98 /* Starting from second entry */ 94 cl[i].status = hdev->amp_status;
99 if (++i >= num_ctrl) 95 else
100 return; 96 cl[i].status = AMP_STATUS_POWERED_DOWN;
101 97 i++;
102 cl[i].id = hdev->id; 98 }
103 cl[i].type = hdev->amp_type;
104 cl[i].status = hdev->amp_status;
105 } 99 }
106} 100}
107 101
@@ -129,6 +123,7 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
129 struct a2mp_discov_rsp *rsp; 123 struct a2mp_discov_rsp *rsp;
130 u16 ext_feat; 124 u16 ext_feat;
131 u8 num_ctrl; 125 u8 num_ctrl;
126 struct hci_dev *hdev;
132 127
133 if (len < sizeof(*req)) 128 if (len < sizeof(*req))
134 return -EINVAL; 129 return -EINVAL;
@@ -152,7 +147,14 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
152 147
153 read_lock(&hci_dev_list_lock); 148 read_lock(&hci_dev_list_lock);
154 149
155 num_ctrl = __hci_num_ctrl(); 150 /* at minimum the BR/EDR needs to be listed */
151 num_ctrl = 1;
152
153 list_for_each_entry(hdev, &hci_dev_list, list) {
154 if (hdev->dev_type == HCI_AMP)
155 num_ctrl++;
156 }
157
156 len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp); 158 len = num_ctrl * sizeof(struct a2mp_cl) + sizeof(*rsp);
157 rsp = kmalloc(len, GFP_ATOMIC); 159 rsp = kmalloc(len, GFP_ATOMIC);
158 if (!rsp) { 160 if (!rsp) {
@@ -163,7 +165,7 @@ static int a2mp_discover_req(struct amp_mgr *mgr, struct sk_buff *skb,
163 rsp->mtu = __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU); 165 rsp->mtu = __constant_cpu_to_le16(L2CAP_A2MP_DEFAULT_MTU);
164 rsp->ext_feat = 0; 166 rsp->ext_feat = 0;
165 167
166 __a2mp_add_cl(mgr, rsp->cl, num_ctrl); 168 __a2mp_add_cl(mgr, rsp->cl);
167 169
168 read_unlock(&hci_dev_list_lock); 170 read_unlock(&hci_dev_list_lock);
169 171
@@ -208,7 +210,7 @@ static int a2mp_discover_rsp(struct amp_mgr *mgr, struct sk_buff *skb,
208 BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type, 210 BT_DBG("Remote AMP id %d type %d status %d", cl->id, cl->type,
209 cl->status); 211 cl->status);
210 212
211 if (cl->id != HCI_BREDR_ID && cl->type == HCI_AMP) { 213 if (cl->id != AMP_ID_BREDR && cl->type != AMP_TYPE_BREDR) {
212 struct a2mp_info_req req; 214 struct a2mp_info_req req;
213 215
214 found = true; 216 found = true;
@@ -344,7 +346,7 @@ static int a2mp_getampassoc_req(struct amp_mgr *mgr, struct sk_buff *skb,
344 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC); 346 tmp = amp_mgr_lookup_by_state(READ_LOC_AMP_ASSOC);
345 347
346 hdev = hci_dev_get(req->id); 348 hdev = hci_dev_get(req->id);
347 if (!hdev || hdev->amp_type == HCI_BREDR || tmp) { 349 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR || tmp) {
348 struct a2mp_amp_assoc_rsp rsp; 350 struct a2mp_amp_assoc_rsp rsp;
349 rsp.id = req->id; 351 rsp.id = req->id;
350 352
@@ -451,7 +453,7 @@ static int a2mp_createphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
451 rsp.remote_id = req->local_id; 453 rsp.remote_id = req->local_id;
452 454
453 hdev = hci_dev_get(req->remote_id); 455 hdev = hci_dev_get(req->remote_id);
454 if (!hdev || hdev->amp_type != HCI_AMP) { 456 if (!hdev || hdev->amp_type == AMP_TYPE_BREDR) {
455 rsp.status = A2MP_STATUS_INVALID_CTRL_ID; 457 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
456 goto send_rsp; 458 goto send_rsp;
457 } 459 }
@@ -535,7 +537,8 @@ static int a2mp_discphyslink_req(struct amp_mgr *mgr, struct sk_buff *skb,
535 goto send_rsp; 537 goto send_rsp;
536 } 538 }
537 539
538 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, mgr->l2cap_conn->dst); 540 hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
541 &mgr->l2cap_conn->hcon->dst);
539 if (!hcon) { 542 if (!hcon) {
540 BT_ERR("No phys link exist"); 543 BT_ERR("No phys link exist");
541 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS; 544 rsp.status = A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS;
@@ -669,7 +672,8 @@ static void a2mp_chan_close_cb(struct l2cap_chan *chan)
669 l2cap_chan_put(chan); 672 l2cap_chan_put(chan);
670} 673}
671 674
672static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state) 675static void a2mp_chan_state_change_cb(struct l2cap_chan *chan, int state,
676 int err)
673{ 677{
674 struct amp_mgr *mgr = chan->data; 678 struct amp_mgr *mgr = chan->data;
675 679
@@ -706,6 +710,9 @@ static struct l2cap_ops a2mp_chan_ops = {
706 .teardown = l2cap_chan_no_teardown, 710 .teardown = l2cap_chan_no_teardown,
707 .ready = l2cap_chan_no_ready, 711 .ready = l2cap_chan_no_ready,
708 .defer = l2cap_chan_no_defer, 712 .defer = l2cap_chan_no_defer,
713 .resume = l2cap_chan_no_resume,
714 .set_shutdown = l2cap_chan_no_set_shutdown,
715 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
709}; 716};
710 717
711static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked) 718static struct l2cap_chan *a2mp_chan_open(struct l2cap_conn *conn, bool locked)
@@ -829,6 +836,9 @@ struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
829{ 836{
830 struct amp_mgr *mgr; 837 struct amp_mgr *mgr;
831 838
839 if (conn->hcon->type != ACL_LINK)
840 return NULL;
841
832 mgr = amp_mgr_create(conn, false); 842 mgr = amp_mgr_create(conn, false);
833 if (!mgr) { 843 if (!mgr) {
834 BT_ERR("Could not create AMP manager"); 844 BT_ERR("Could not create AMP manager");
@@ -871,7 +881,7 @@ void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
871 rsp.id = hdev->id; 881 rsp.id = hdev->id;
872 rsp.status = A2MP_STATUS_INVALID_CTRL_ID; 882 rsp.status = A2MP_STATUS_INVALID_CTRL_ID;
873 883
874 if (hdev->amp_type != HCI_BREDR) { 884 if (hdev->amp_type != AMP_TYPE_BREDR) {
875 rsp.status = 0; 885 rsp.status = 0;
876 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw); 886 rsp.total_bw = cpu_to_le32(hdev->amp_total_bw);
877 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw); 887 rsp.max_bw = cpu_to_le32(hdev->amp_max_bw);
diff --git a/net/bluetooth/a2mp.h b/net/bluetooth/a2mp.h
new file mode 100644
index 000000000000..487b54c1308f
--- /dev/null
+++ b/net/bluetooth/a2mp.h
@@ -0,0 +1,150 @@
1/*
2 Copyright (c) 2010,2011 Code Aurora Forum. All rights reserved.
3 Copyright (c) 2011,2012 Intel Corp.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 and
7 only version 2 as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13*/
14
15#ifndef __A2MP_H
16#define __A2MP_H
17
18#include <net/bluetooth/l2cap.h>
19
20#define A2MP_FEAT_EXT 0x8000
21
22enum amp_mgr_state {
23 READ_LOC_AMP_INFO,
24 READ_LOC_AMP_ASSOC,
25 READ_LOC_AMP_ASSOC_FINAL,
26 WRITE_REMOTE_AMP_ASSOC,
27};
28
29struct amp_mgr {
30 struct list_head list;
31 struct l2cap_conn *l2cap_conn;
32 struct l2cap_chan *a2mp_chan;
33 struct l2cap_chan *bredr_chan;
34 struct kref kref;
35 __u8 ident;
36 __u8 handle;
37 unsigned long state;
38 unsigned long flags;
39
40 struct list_head amp_ctrls;
41 struct mutex amp_ctrls_lock;
42};
43
44struct a2mp_cmd {
45 __u8 code;
46 __u8 ident;
47 __le16 len;
48 __u8 data[0];
49} __packed;
50
51/* A2MP command codes */
52#define A2MP_COMMAND_REJ 0x01
53struct a2mp_cmd_rej {
54 __le16 reason;
55 __u8 data[0];
56} __packed;
57
58#define A2MP_DISCOVER_REQ 0x02
59struct a2mp_discov_req {
60 __le16 mtu;
61 __le16 ext_feat;
62} __packed;
63
64struct a2mp_cl {
65 __u8 id;
66 __u8 type;
67 __u8 status;
68} __packed;
69
70#define A2MP_DISCOVER_RSP 0x03
71struct a2mp_discov_rsp {
72 __le16 mtu;
73 __le16 ext_feat;
74 struct a2mp_cl cl[0];
75} __packed;
76
77#define A2MP_CHANGE_NOTIFY 0x04
78#define A2MP_CHANGE_RSP 0x05
79
80#define A2MP_GETINFO_REQ 0x06
81struct a2mp_info_req {
82 __u8 id;
83} __packed;
84
85#define A2MP_GETINFO_RSP 0x07
86struct a2mp_info_rsp {
87 __u8 id;
88 __u8 status;
89 __le32 total_bw;
90 __le32 max_bw;
91 __le32 min_latency;
92 __le16 pal_cap;
93 __le16 assoc_size;
94} __packed;
95
96#define A2MP_GETAMPASSOC_REQ 0x08
97struct a2mp_amp_assoc_req {
98 __u8 id;
99} __packed;
100
101#define A2MP_GETAMPASSOC_RSP 0x09
102struct a2mp_amp_assoc_rsp {
103 __u8 id;
104 __u8 status;
105 __u8 amp_assoc[0];
106} __packed;
107
108#define A2MP_CREATEPHYSLINK_REQ 0x0A
109#define A2MP_DISCONNPHYSLINK_REQ 0x0C
110struct a2mp_physlink_req {
111 __u8 local_id;
112 __u8 remote_id;
113 __u8 amp_assoc[0];
114} __packed;
115
116#define A2MP_CREATEPHYSLINK_RSP 0x0B
117#define A2MP_DISCONNPHYSLINK_RSP 0x0D
118struct a2mp_physlink_rsp {
119 __u8 local_id;
120 __u8 remote_id;
121 __u8 status;
122} __packed;
123
124/* A2MP response status */
125#define A2MP_STATUS_SUCCESS 0x00
126#define A2MP_STATUS_INVALID_CTRL_ID 0x01
127#define A2MP_STATUS_UNABLE_START_LINK_CREATION 0x02
128#define A2MP_STATUS_NO_PHYSICAL_LINK_EXISTS 0x02
129#define A2MP_STATUS_COLLISION_OCCURED 0x03
130#define A2MP_STATUS_DISCONN_REQ_RECVD 0x04
131#define A2MP_STATUS_PHYS_LINK_EXISTS 0x05
132#define A2MP_STATUS_SECURITY_VIOLATION 0x06
133
134extern struct list_head amp_mgr_list;
135extern struct mutex amp_mgr_list_lock;
136
137struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr);
138int amp_mgr_put(struct amp_mgr *mgr);
139u8 __next_ident(struct amp_mgr *mgr);
140struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
141 struct sk_buff *skb);
142struct amp_mgr *amp_mgr_lookup_by_state(u8 state);
143void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data);
144void a2mp_discover_amp(struct l2cap_chan *chan);
145void a2mp_send_getinfo_rsp(struct hci_dev *hdev);
146void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status);
147void a2mp_send_create_phy_link_req(struct hci_dev *hdev, u8 status);
148void a2mp_send_create_phy_link_rsp(struct hci_dev *hdev, u8 status);
149
150#endif /* __A2MP_H */
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 9096137c889c..56ca494621c6 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -25,12 +25,13 @@
25/* Bluetooth address family and sockets. */ 25/* Bluetooth address family and sockets. */
26 26
27#include <linux/module.h> 27#include <linux/module.h>
28#include <linux/debugfs.h>
28#include <asm/ioctls.h> 29#include <asm/ioctls.h>
29 30
30#include <net/bluetooth/bluetooth.h> 31#include <net/bluetooth/bluetooth.h>
31#include <linux/proc_fs.h> 32#include <linux/proc_fs.h>
32 33
33#define VERSION "2.16" 34#define VERSION "2.17"
34 35
35/* Bluetooth sockets */ 36/* Bluetooth sockets */
36#define BT_MAX_PROTO 8 37#define BT_MAX_PROTO 8
@@ -221,12 +222,11 @@ int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
221 if (flags & (MSG_OOB)) 222 if (flags & (MSG_OOB))
222 return -EOPNOTSUPP; 223 return -EOPNOTSUPP;
223 224
224 msg->msg_namelen = 0;
225
226 skb = skb_recv_datagram(sk, flags, noblock, &err); 225 skb = skb_recv_datagram(sk, flags, noblock, &err);
227 if (!skb) { 226 if (!skb) {
228 if (sk->sk_shutdown & RCV_SHUTDOWN) 227 if (sk->sk_shutdown & RCV_SHUTDOWN)
229 return 0; 228 return 0;
229
230 return err; 230 return err;
231 } 231 }
232 232
@@ -238,9 +238,14 @@ int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
238 238
239 skb_reset_transport_header(skb); 239 skb_reset_transport_header(skb);
240 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 240 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
241 if (err == 0) 241 if (err == 0) {
242 sock_recv_ts_and_drops(msg, sk, skb); 242 sock_recv_ts_and_drops(msg, sk, skb);
243 243
244 if (bt_sk(sk)->skb_msg_name)
245 bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
246 &msg->msg_namelen);
247 }
248
244 skb_free_datagram(sk, skb); 249 skb_free_datagram(sk, skb);
245 250
246 return err ? : copied; 251 return err ? : copied;
@@ -287,8 +292,6 @@ int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
287 if (flags & MSG_OOB) 292 if (flags & MSG_OOB)
288 return -EOPNOTSUPP; 293 return -EOPNOTSUPP;
289 294
290 msg->msg_namelen = 0;
291
292 BT_DBG("sk %p size %zu", sk, size); 295 BT_DBG("sk %p size %zu", sk, size);
293 296
294 lock_sock(sk); 297 lock_sock(sk);
@@ -490,6 +493,7 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
490} 493}
491EXPORT_SYMBOL(bt_sock_ioctl); 494EXPORT_SYMBOL(bt_sock_ioctl);
492 495
496/* This function expects the sk lock to be held when called */
493int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) 497int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
494{ 498{
495 DECLARE_WAITQUEUE(wait, current); 499 DECLARE_WAITQUEUE(wait, current);
@@ -525,6 +529,46 @@ int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
525} 529}
526EXPORT_SYMBOL(bt_sock_wait_state); 530EXPORT_SYMBOL(bt_sock_wait_state);
527 531
532/* This function expects the sk lock to be held when called */
533int bt_sock_wait_ready(struct sock *sk, unsigned long flags)
534{
535 DECLARE_WAITQUEUE(wait, current);
536 unsigned long timeo;
537 int err = 0;
538
539 BT_DBG("sk %p", sk);
540
541 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
542
543 add_wait_queue(sk_sleep(sk), &wait);
544 set_current_state(TASK_INTERRUPTIBLE);
545 while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) {
546 if (!timeo) {
547 err = -EAGAIN;
548 break;
549 }
550
551 if (signal_pending(current)) {
552 err = sock_intr_errno(timeo);
553 break;
554 }
555
556 release_sock(sk);
557 timeo = schedule_timeout(timeo);
558 lock_sock(sk);
559 set_current_state(TASK_INTERRUPTIBLE);
560
561 err = sock_error(sk);
562 if (err)
563 break;
564 }
565 __set_current_state(TASK_RUNNING);
566 remove_wait_queue(sk_sleep(sk), &wait);
567
568 return err;
569}
570EXPORT_SYMBOL(bt_sock_wait_ready);
571
528#ifdef CONFIG_PROC_FS 572#ifdef CONFIG_PROC_FS
529struct bt_seq_state { 573struct bt_seq_state {
530 struct bt_sock_list *l; 574 struct bt_sock_list *l;
@@ -563,7 +607,7 @@ static int bt_seq_show(struct seq_file *seq, void *v)
563 struct bt_sock_list *l = s->l; 607 struct bt_sock_list *l = s->l;
564 608
565 if (v == SEQ_START_TOKEN) { 609 if (v == SEQ_START_TOKEN) {
566 seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Src Dst Parent"); 610 seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Parent");
567 611
568 if (l->custom_seq_show) { 612 if (l->custom_seq_show) {
569 seq_putc(seq, ' '); 613 seq_putc(seq, ' ');
@@ -576,15 +620,13 @@ static int bt_seq_show(struct seq_file *seq, void *v)
576 struct bt_sock *bt = bt_sk(sk); 620 struct bt_sock *bt = bt_sk(sk);
577 621
578 seq_printf(seq, 622 seq_printf(seq,
579 "%pK %-6d %-6u %-6u %-6u %-6lu %pMR %pMR %-6lu", 623 "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu",
580 sk, 624 sk,
581 atomic_read(&sk->sk_refcnt), 625 atomic_read(&sk->sk_refcnt),
582 sk_rmem_alloc_get(sk), 626 sk_rmem_alloc_get(sk),
583 sk_wmem_alloc_get(sk), 627 sk_wmem_alloc_get(sk),
584 from_kuid(seq_user_ns(seq), sock_i_uid(sk)), 628 from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
585 sock_i_ino(sk), 629 sock_i_ino(sk),
586 &bt->src,
587 &bt->dst,
588 bt->parent? sock_i_ino(bt->parent): 0LU); 630 bt->parent? sock_i_ino(bt->parent): 0LU);
589 631
590 if (l->custom_seq_show) { 632 if (l->custom_seq_show) {
@@ -662,12 +704,17 @@ static struct net_proto_family bt_sock_family_ops = {
662 .create = bt_sock_create, 704 .create = bt_sock_create,
663}; 705};
664 706
707struct dentry *bt_debugfs;
708EXPORT_SYMBOL_GPL(bt_debugfs);
709
665static int __init bt_init(void) 710static int __init bt_init(void)
666{ 711{
667 int err; 712 int err;
668 713
669 BT_INFO("Core ver %s", VERSION); 714 BT_INFO("Core ver %s", VERSION);
670 715
716 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
717
671 err = bt_sysfs_init(); 718 err = bt_sysfs_init();
672 if (err < 0) 719 if (err < 0)
673 return err; 720 return err;
@@ -708,7 +755,6 @@ error:
708 755
709static void __exit bt_exit(void) 756static void __exit bt_exit(void)
710{ 757{
711
712 sco_exit(); 758 sco_exit();
713 759
714 l2cap_exit(); 760 l2cap_exit();
@@ -718,6 +764,8 @@ static void __exit bt_exit(void)
718 sock_unregister(PF_BLUETOOTH); 764 sock_unregister(PF_BLUETOOTH);
719 765
720 bt_sysfs_cleanup(); 766 bt_sysfs_cleanup();
767
768 debugfs_remove_recursive(bt_debugfs);
721} 769}
722 770
723subsys_initcall(bt_init); 771subsys_initcall(bt_init);
diff --git a/net/bluetooth/amp.c b/net/bluetooth/amp.c
index d459ed43c779..bb39509b3f06 100644
--- a/net/bluetooth/amp.c
+++ b/net/bluetooth/amp.c
@@ -14,10 +14,11 @@
14#include <net/bluetooth/bluetooth.h> 14#include <net/bluetooth/bluetooth.h>
15#include <net/bluetooth/hci.h> 15#include <net/bluetooth/hci.h>
16#include <net/bluetooth/hci_core.h> 16#include <net/bluetooth/hci_core.h>
17#include <net/bluetooth/a2mp.h>
18#include <net/bluetooth/amp.h>
19#include <crypto/hash.h> 17#include <crypto/hash.h>
20 18
19#include "a2mp.h"
20#include "amp.h"
21
21/* Remote AMP Controllers interface */ 22/* Remote AMP Controllers interface */
22void amp_ctrl_get(struct amp_ctrl *ctrl) 23void amp_ctrl_get(struct amp_ctrl *ctrl)
23{ 24{
@@ -110,7 +111,7 @@ static u8 __next_handle(struct amp_mgr *mgr)
110struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr, 111struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
111 u8 remote_id, bool out) 112 u8 remote_id, bool out)
112{ 113{
113 bdaddr_t *dst = mgr->l2cap_conn->dst; 114 bdaddr_t *dst = &mgr->l2cap_conn->hcon->dst;
114 struct hci_conn *hcon; 115 struct hci_conn *hcon;
115 116
116 hcon = hci_conn_add(hdev, AMP_LINK, dst); 117 hcon = hci_conn_add(hdev, AMP_LINK, dst);
@@ -409,7 +410,8 @@ void amp_create_logical_link(struct l2cap_chan *chan)
409 struct hci_cp_create_accept_logical_link cp; 410 struct hci_cp_create_accept_logical_link cp;
410 struct hci_dev *hdev; 411 struct hci_dev *hdev;
411 412
412 BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon, chan->conn->dst); 413 BT_DBG("chan %p hs_hcon %p dst %pMR", chan, hs_hcon,
414 &chan->conn->hcon->dst);
413 415
414 if (!hs_hcon) 416 if (!hs_hcon)
415 return; 417 return;
diff --git a/net/bluetooth/amp.h b/net/bluetooth/amp.h
new file mode 100644
index 000000000000..7ea3db77ba89
--- /dev/null
+++ b/net/bluetooth/amp.h
@@ -0,0 +1,54 @@
1/*
2 Copyright (c) 2011,2012 Intel Corp.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*/
13
14#ifndef __AMP_H
15#define __AMP_H
16
17struct amp_ctrl {
18 struct list_head list;
19 struct kref kref;
20 __u8 id;
21 __u16 assoc_len_so_far;
22 __u16 assoc_rem_len;
23 __u16 assoc_len;
24 __u8 *assoc;
25};
26
27int amp_ctrl_put(struct amp_ctrl *ctrl);
28void amp_ctrl_get(struct amp_ctrl *ctrl);
29struct amp_ctrl *amp_ctrl_add(struct amp_mgr *mgr, u8 id);
30struct amp_ctrl *amp_ctrl_lookup(struct amp_mgr *mgr, u8 id);
31void amp_ctrl_list_flush(struct amp_mgr *mgr);
32
33struct hci_conn *phylink_add(struct hci_dev *hdev, struct amp_mgr *mgr,
34 u8 remote_id, bool out);
35
36int phylink_gen_key(struct hci_conn *hcon, u8 *data, u8 *len, u8 *type);
37
38void amp_read_loc_info(struct hci_dev *hdev, struct amp_mgr *mgr);
39void amp_read_loc_assoc_frag(struct hci_dev *hdev, u8 phy_handle);
40void amp_read_loc_assoc(struct hci_dev *hdev, struct amp_mgr *mgr);
41void amp_read_loc_assoc_final_data(struct hci_dev *hdev,
42 struct hci_conn *hcon);
43void amp_create_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
44 struct hci_conn *hcon);
45void amp_accept_phylink(struct hci_dev *hdev, struct amp_mgr *mgr,
46 struct hci_conn *hcon);
47void amp_write_remote_assoc(struct hci_dev *hdev, u8 handle);
48void amp_write_rem_assoc_continue(struct hci_dev *hdev, u8 handle);
49void amp_physical_cfm(struct hci_conn *bredr_hcon, struct hci_conn *hs_hcon);
50void amp_create_logical_link(struct l2cap_chan *chan);
51void amp_disconnect_logical_link(struct hci_chan *hchan);
52void amp_destroy_logical_link(struct hci_chan *hchan, u8 reason);
53
54#endif /* __AMP_H */
diff --git a/net/bluetooth/bnep/core.c b/net/bluetooth/bnep/core.c
index e430b1abcd2f..a841d3e776c5 100644
--- a/net/bluetooth/bnep/core.c
+++ b/net/bluetooth/bnep/core.c
@@ -32,6 +32,7 @@
32#include <asm/unaligned.h> 32#include <asm/unaligned.h>
33 33
34#include <net/bluetooth/bluetooth.h> 34#include <net/bluetooth/bluetooth.h>
35#include <net/bluetooth/l2cap.h>
35#include <net/bluetooth/hci_core.h> 36#include <net/bluetooth/hci_core.h>
36 37
37#include "bnep.h" 38#include "bnep.h"
@@ -510,20 +511,13 @@ static int bnep_session(void *arg)
510 511
511static struct device *bnep_get_device(struct bnep_session *session) 512static struct device *bnep_get_device(struct bnep_session *session)
512{ 513{
513 bdaddr_t *src = &bt_sk(session->sock->sk)->src;
514 bdaddr_t *dst = &bt_sk(session->sock->sk)->dst;
515 struct hci_dev *hdev;
516 struct hci_conn *conn; 514 struct hci_conn *conn;
517 515
518 hdev = hci_get_route(dst, src); 516 conn = l2cap_pi(session->sock->sk)->chan->conn->hcon;
519 if (!hdev) 517 if (!conn)
520 return NULL; 518 return NULL;
521 519
522 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 520 return &conn->dev;
523
524 hci_dev_put(hdev);
525
526 return conn ? &conn->dev : NULL;
527} 521}
528 522
529static struct device_type bnep_type = { 523static struct device_type bnep_type = {
@@ -539,8 +533,8 @@ int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
539 533
540 BT_DBG(""); 534 BT_DBG("");
541 535
542 baswap((void *) dst, &bt_sk(sock->sk)->dst); 536 baswap((void *) dst, &l2cap_pi(sock->sk)->chan->dst);
543 baswap((void *) src, &bt_sk(sock->sk)->src); 537 baswap((void *) src, &l2cap_pi(sock->sk)->chan->src);
544 538
545 /* session struct allocated as private part of net_device */ 539 /* session struct allocated as private part of net_device */
546 dev = alloc_netdev(sizeof(struct bnep_session), 540 dev = alloc_netdev(sizeof(struct bnep_session),
diff --git a/net/bluetooth/cmtp/core.c b/net/bluetooth/cmtp/core.c
index e0a6ebf2baa6..67fe5e84e68f 100644
--- a/net/bluetooth/cmtp/core.c
+++ b/net/bluetooth/cmtp/core.c
@@ -340,20 +340,20 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
340 340
341 down_write(&cmtp_session_sem); 341 down_write(&cmtp_session_sem);
342 342
343 s = __cmtp_get_session(&bt_sk(sock->sk)->dst); 343 s = __cmtp_get_session(&l2cap_pi(sock->sk)->chan->dst);
344 if (s && s->state == BT_CONNECTED) { 344 if (s && s->state == BT_CONNECTED) {
345 err = -EEXIST; 345 err = -EEXIST;
346 goto failed; 346 goto failed;
347 } 347 }
348 348
349 bacpy(&session->bdaddr, &bt_sk(sock->sk)->dst); 349 bacpy(&session->bdaddr, &l2cap_pi(sock->sk)->chan->dst);
350 350
351 session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu, 351 session->mtu = min_t(uint, l2cap_pi(sock->sk)->chan->omtu,
352 l2cap_pi(sock->sk)->chan->imtu); 352 l2cap_pi(sock->sk)->chan->imtu);
353 353
354 BT_DBG("mtu %d", session->mtu); 354 BT_DBG("mtu %d", session->mtu);
355 355
356 sprintf(session->name, "%pMR", &bt_sk(sock->sk)->dst); 356 sprintf(session->name, "%pMR", &session->bdaddr);
357 357
358 session->sock = sock; 358 session->sock = sock;
359 session->state = BT_CONFIG; 359 session->state = BT_CONFIG;
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index f0817121ec5e..ba5366c320da 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -28,8 +28,9 @@
28 28
29#include <net/bluetooth/bluetooth.h> 29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h> 30#include <net/bluetooth/hci_core.h>
31#include <net/bluetooth/a2mp.h> 31
32#include <net/bluetooth/smp.h> 32#include "smp.h"
33#include "a2mp.h"
33 34
34struct sco_param { 35struct sco_param {
35 u16 pkt_type; 36 u16 pkt_type;
@@ -49,30 +50,6 @@ static const struct sco_param sco_param_wideband[] = {
49 { EDR_ESCO_MASK | ESCO_EV3, 0x0008 }, /* T1 */ 50 { EDR_ESCO_MASK | ESCO_EV3, 0x0008 }, /* T1 */
50}; 51};
51 52
52static void hci_le_create_connection(struct hci_conn *conn)
53{
54 struct hci_dev *hdev = conn->hdev;
55 struct hci_cp_le_create_conn cp;
56
57 conn->state = BT_CONNECT;
58 conn->out = true;
59 conn->link_mode |= HCI_LM_MASTER;
60 conn->sec_level = BT_SECURITY_LOW;
61
62 memset(&cp, 0, sizeof(cp));
63 cp.scan_interval = __constant_cpu_to_le16(0x0060);
64 cp.scan_window = __constant_cpu_to_le16(0x0030);
65 bacpy(&cp.peer_addr, &conn->dst);
66 cp.peer_addr_type = conn->dst_type;
67 cp.conn_interval_min = __constant_cpu_to_le16(0x0028);
68 cp.conn_interval_max = __constant_cpu_to_le16(0x0038);
69 cp.supervision_timeout = __constant_cpu_to_le16(0x002a);
70 cp.min_ce_len = __constant_cpu_to_le16(0x0000);
71 cp.max_ce_len = __constant_cpu_to_le16(0x0000);
72
73 hci_send_cmd(hdev, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
74}
75
76static void hci_le_create_connection_cancel(struct hci_conn *conn) 53static void hci_le_create_connection_cancel(struct hci_conn *conn)
77{ 54{
78 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL); 55 hci_send_cmd(conn->hdev, HCI_OP_LE_CREATE_CONN_CANCEL, 0, NULL);
@@ -340,8 +317,10 @@ static void hci_conn_timeout(struct work_struct *work)
340} 317}
341 318
342/* Enter sniff mode */ 319/* Enter sniff mode */
343static void hci_conn_enter_sniff_mode(struct hci_conn *conn) 320static void hci_conn_idle(struct work_struct *work)
344{ 321{
322 struct hci_conn *conn = container_of(work, struct hci_conn,
323 idle_work.work);
345 struct hci_dev *hdev = conn->hdev; 324 struct hci_dev *hdev = conn->hdev;
346 325
347 BT_DBG("hcon %p mode %d", conn, conn->mode); 326 BT_DBG("hcon %p mode %d", conn, conn->mode);
@@ -375,21 +354,12 @@ static void hci_conn_enter_sniff_mode(struct hci_conn *conn)
375 } 354 }
376} 355}
377 356
378static void hci_conn_idle(unsigned long arg) 357static void hci_conn_auto_accept(struct work_struct *work)
379{
380 struct hci_conn *conn = (void *) arg;
381
382 BT_DBG("hcon %p mode %d", conn, conn->mode);
383
384 hci_conn_enter_sniff_mode(conn);
385}
386
387static void hci_conn_auto_accept(unsigned long arg)
388{ 358{
389 struct hci_conn *conn = (void *) arg; 359 struct hci_conn *conn = container_of(work, struct hci_conn,
390 struct hci_dev *hdev = conn->hdev; 360 auto_accept_work.work);
391 361
392 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst), 362 hci_send_cmd(conn->hdev, HCI_OP_USER_CONFIRM_REPLY, sizeof(conn->dst),
393 &conn->dst); 363 &conn->dst);
394} 364}
395 365
@@ -404,6 +374,7 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
404 return NULL; 374 return NULL;
405 375
406 bacpy(&conn->dst, dst); 376 bacpy(&conn->dst, dst);
377 bacpy(&conn->src, &hdev->bdaddr);
407 conn->hdev = hdev; 378 conn->hdev = hdev;
408 conn->type = type; 379 conn->type = type;
409 conn->mode = HCI_CM_ACTIVE; 380 conn->mode = HCI_CM_ACTIVE;
@@ -437,9 +408,8 @@ struct hci_conn *hci_conn_add(struct hci_dev *hdev, int type, bdaddr_t *dst)
437 INIT_LIST_HEAD(&conn->chan_list); 408 INIT_LIST_HEAD(&conn->chan_list);
438 409
439 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout); 410 INIT_DELAYED_WORK(&conn->disc_work, hci_conn_timeout);
440 setup_timer(&conn->idle_timer, hci_conn_idle, (unsigned long)conn); 411 INIT_DELAYED_WORK(&conn->auto_accept_work, hci_conn_auto_accept);
441 setup_timer(&conn->auto_accept_timer, hci_conn_auto_accept, 412 INIT_DELAYED_WORK(&conn->idle_work, hci_conn_idle);
442 (unsigned long) conn);
443 413
444 atomic_set(&conn->refcnt, 0); 414 atomic_set(&conn->refcnt, 0);
445 415
@@ -460,11 +430,9 @@ int hci_conn_del(struct hci_conn *conn)
460 430
461 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle); 431 BT_DBG("%s hcon %p handle %d", hdev->name, conn, conn->handle);
462 432
463 del_timer(&conn->idle_timer);
464
465 cancel_delayed_work_sync(&conn->disc_work); 433 cancel_delayed_work_sync(&conn->disc_work);
466 434 cancel_delayed_work_sync(&conn->auto_accept_work);
467 del_timer(&conn->auto_accept_timer); 435 cancel_delayed_work_sync(&conn->idle_work);
468 436
469 if (conn->type == ACL_LINK) { 437 if (conn->type == ACL_LINK) {
470 struct hci_conn *sco = conn->link; 438 struct hci_conn *sco = conn->link;
@@ -518,6 +486,7 @@ struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
518 list_for_each_entry(d, &hci_dev_list, list) { 486 list_for_each_entry(d, &hci_dev_list, list) {
519 if (!test_bit(HCI_UP, &d->flags) || 487 if (!test_bit(HCI_UP, &d->flags) ||
520 test_bit(HCI_RAW, &d->flags) || 488 test_bit(HCI_RAW, &d->flags) ||
489 test_bit(HCI_USER_CHANNEL, &d->dev_flags) ||
521 d->dev_type != HCI_BREDR) 490 d->dev_type != HCI_BREDR)
522 continue; 491 continue;
523 492
@@ -545,34 +514,124 @@ struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
545} 514}
546EXPORT_SYMBOL(hci_get_route); 515EXPORT_SYMBOL(hci_get_route);
547 516
517static void create_le_conn_complete(struct hci_dev *hdev, u8 status)
518{
519 struct hci_conn *conn;
520
521 if (status == 0)
522 return;
523
524 BT_ERR("HCI request failed to create LE connection: status 0x%2.2x",
525 status);
526
527 hci_dev_lock(hdev);
528
529 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
530 if (!conn)
531 goto done;
532
533 conn->state = BT_CLOSED;
534
535 mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type,
536 status);
537
538 hci_proto_connect_cfm(conn, status);
539
540 hci_conn_del(conn);
541
542done:
543 hci_dev_unlock(hdev);
544}
545
546static int hci_create_le_conn(struct hci_conn *conn)
547{
548 struct hci_dev *hdev = conn->hdev;
549 struct hci_cp_le_create_conn cp;
550 struct hci_request req;
551 int err;
552
553 hci_req_init(&req, hdev);
554
555 memset(&cp, 0, sizeof(cp));
556 cp.scan_interval = cpu_to_le16(hdev->le_scan_interval);
557 cp.scan_window = cpu_to_le16(hdev->le_scan_window);
558 bacpy(&cp.peer_addr, &conn->dst);
559 cp.peer_addr_type = conn->dst_type;
560 cp.own_address_type = conn->src_type;
561 cp.conn_interval_min = cpu_to_le16(hdev->le_conn_min_interval);
562 cp.conn_interval_max = cpu_to_le16(hdev->le_conn_max_interval);
563 cp.supervision_timeout = __constant_cpu_to_le16(0x002a);
564 cp.min_ce_len = __constant_cpu_to_le16(0x0000);
565 cp.max_ce_len = __constant_cpu_to_le16(0x0000);
566
567 hci_req_add(&req, HCI_OP_LE_CREATE_CONN, sizeof(cp), &cp);
568
569 err = hci_req_run(&req, create_le_conn_complete);
570 if (err) {
571 hci_conn_del(conn);
572 return err;
573 }
574
575 return 0;
576}
577
548static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst, 578static struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
549 u8 dst_type, u8 sec_level, u8 auth_type) 579 u8 dst_type, u8 sec_level, u8 auth_type)
550{ 580{
551 struct hci_conn *le; 581 struct hci_conn *conn;
582 int err;
552 583
553 if (test_bit(HCI_LE_PERIPHERAL, &hdev->flags)) 584 if (test_bit(HCI_ADVERTISING, &hdev->flags))
554 return ERR_PTR(-ENOTSUPP); 585 return ERR_PTR(-ENOTSUPP);
555 586
556 le = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst); 587 /* Some devices send ATT messages as soon as the physical link is
557 if (!le) { 588 * established. To be able to handle these ATT messages, the user-
558 le = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); 589 * space first establishes the connection and then starts the pairing
559 if (le) 590 * process.
560 return ERR_PTR(-EBUSY); 591 *
592 * So if a hci_conn object already exists for the following connection
593 * attempt, we simply update pending_sec_level and auth_type fields
594 * and return the object found.
595 */
596 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, dst);
597 if (conn) {
598 conn->pending_sec_level = sec_level;
599 conn->auth_type = auth_type;
600 goto done;
601 }
561 602
562 le = hci_conn_add(hdev, LE_LINK, dst); 603 /* Since the controller supports only one LE connection attempt at a
563 if (!le) 604 * time, we return -EBUSY if there is any connection attempt running.
564 return ERR_PTR(-ENOMEM); 605 */
606 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
607 if (conn)
608 return ERR_PTR(-EBUSY);
565 609
566 le->dst_type = bdaddr_to_le(dst_type); 610 conn = hci_conn_add(hdev, LE_LINK, dst);
567 hci_le_create_connection(le); 611 if (!conn)
568 } 612 return ERR_PTR(-ENOMEM);
569 613
570 le->pending_sec_level = sec_level; 614 if (dst_type == BDADDR_LE_PUBLIC)
571 le->auth_type = auth_type; 615 conn->dst_type = ADDR_LE_DEV_PUBLIC;
616 else
617 conn->dst_type = ADDR_LE_DEV_RANDOM;
572 618
573 hci_conn_hold(le); 619 conn->src_type = hdev->own_addr_type;
574 620
575 return le; 621 conn->state = BT_CONNECT;
622 conn->out = true;
623 conn->link_mode |= HCI_LM_MASTER;
624 conn->sec_level = BT_SECURITY_LOW;
625 conn->pending_sec_level = sec_level;
626 conn->auth_type = auth_type;
627
628 err = hci_create_le_conn(conn);
629 if (err)
630 return ERR_PTR(err);
631
632done:
633 hci_conn_hold(conn);
634 return conn;
576} 635}
577 636
578static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst, 637static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
@@ -580,6 +639,9 @@ static struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
580{ 639{
581 struct hci_conn *acl; 640 struct hci_conn *acl;
582 641
642 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
643 return ERR_PTR(-ENOTSUPP);
644
583 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 645 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
584 if (!acl) { 646 if (!acl) {
585 acl = hci_conn_add(hdev, ACL_LINK, dst); 647 acl = hci_conn_add(hdev, ACL_LINK, dst);
@@ -846,8 +908,8 @@ void hci_conn_enter_active_mode(struct hci_conn *conn, __u8 force_active)
846 908
847timer: 909timer:
848 if (hdev->idle_timeout > 0) 910 if (hdev->idle_timeout > 0)
849 mod_timer(&conn->idle_timer, 911 queue_delayed_work(hdev->workqueue, &conn->idle_work,
850 jiffies + msecs_to_jiffies(hdev->idle_timeout)); 912 msecs_to_jiffies(hdev->idle_timeout));
851} 913}
852 914
853/* Drop all connection on the device */ 915/* Drop all connection on the device */
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index fb7356fcfe51..6ccc4eb9e55e 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -27,8 +27,9 @@
27 27
28#include <linux/export.h> 28#include <linux/export.h>
29#include <linux/idr.h> 29#include <linux/idr.h>
30
31#include <linux/rfkill.h> 30#include <linux/rfkill.h>
31#include <linux/debugfs.h>
32#include <asm/unaligned.h>
32 33
33#include <net/bluetooth/bluetooth.h> 34#include <net/bluetooth/bluetooth.h>
34#include <net/bluetooth/hci_core.h> 35#include <net/bluetooth/hci_core.h>
@@ -55,6 +56,586 @@ static void hci_notify(struct hci_dev *hdev, int event)
55 hci_sock_dev_event(hdev, event); 56 hci_sock_dev_event(hdev, event);
56} 57}
57 58
59/* ---- HCI debugfs entries ---- */
60
61static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
62 size_t count, loff_t *ppos)
63{
64 struct hci_dev *hdev = file->private_data;
65 char buf[3];
66
67 buf[0] = test_bit(HCI_DUT_MODE, &hdev->dev_flags) ? 'Y': 'N';
68 buf[1] = '\n';
69 buf[2] = '\0';
70 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
71}
72
73static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
74 size_t count, loff_t *ppos)
75{
76 struct hci_dev *hdev = file->private_data;
77 struct sk_buff *skb;
78 char buf[32];
79 size_t buf_size = min(count, (sizeof(buf)-1));
80 bool enable;
81 int err;
82
83 if (!test_bit(HCI_UP, &hdev->flags))
84 return -ENETDOWN;
85
86 if (copy_from_user(buf, user_buf, buf_size))
87 return -EFAULT;
88
89 buf[buf_size] = '\0';
90 if (strtobool(buf, &enable))
91 return -EINVAL;
92
93 if (enable == test_bit(HCI_DUT_MODE, &hdev->dev_flags))
94 return -EALREADY;
95
96 hci_req_lock(hdev);
97 if (enable)
98 skb = __hci_cmd_sync(hdev, HCI_OP_ENABLE_DUT_MODE, 0, NULL,
99 HCI_CMD_TIMEOUT);
100 else
101 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL,
102 HCI_CMD_TIMEOUT);
103 hci_req_unlock(hdev);
104
105 if (IS_ERR(skb))
106 return PTR_ERR(skb);
107
108 err = -bt_to_errno(skb->data[0]);
109 kfree_skb(skb);
110
111 if (err < 0)
112 return err;
113
114 change_bit(HCI_DUT_MODE, &hdev->dev_flags);
115
116 return count;
117}
118
119static const struct file_operations dut_mode_fops = {
120 .open = simple_open,
121 .read = dut_mode_read,
122 .write = dut_mode_write,
123 .llseek = default_llseek,
124};
125
126static int features_show(struct seq_file *f, void *ptr)
127{
128 struct hci_dev *hdev = f->private;
129 u8 p;
130
131 hci_dev_lock(hdev);
132 for (p = 0; p < HCI_MAX_PAGES && p <= hdev->max_page; p++) {
133 seq_printf(f, "%2u: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
134 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n", p,
135 hdev->features[p][0], hdev->features[p][1],
136 hdev->features[p][2], hdev->features[p][3],
137 hdev->features[p][4], hdev->features[p][5],
138 hdev->features[p][6], hdev->features[p][7]);
139 }
140 if (lmp_le_capable(hdev))
141 seq_printf(f, "LE: 0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x "
142 "0x%2.2x 0x%2.2x 0x%2.2x 0x%2.2x\n",
143 hdev->le_features[0], hdev->le_features[1],
144 hdev->le_features[2], hdev->le_features[3],
145 hdev->le_features[4], hdev->le_features[5],
146 hdev->le_features[6], hdev->le_features[7]);
147 hci_dev_unlock(hdev);
148
149 return 0;
150}
151
152static int features_open(struct inode *inode, struct file *file)
153{
154 return single_open(file, features_show, inode->i_private);
155}
156
157static const struct file_operations features_fops = {
158 .open = features_open,
159 .read = seq_read,
160 .llseek = seq_lseek,
161 .release = single_release,
162};
163
164static int blacklist_show(struct seq_file *f, void *p)
165{
166 struct hci_dev *hdev = f->private;
167 struct bdaddr_list *b;
168
169 hci_dev_lock(hdev);
170 list_for_each_entry(b, &hdev->blacklist, list)
171 seq_printf(f, "%pMR (type %u)\n", &b->bdaddr, b->bdaddr_type);
172 hci_dev_unlock(hdev);
173
174 return 0;
175}
176
177static int blacklist_open(struct inode *inode, struct file *file)
178{
179 return single_open(file, blacklist_show, inode->i_private);
180}
181
182static const struct file_operations blacklist_fops = {
183 .open = blacklist_open,
184 .read = seq_read,
185 .llseek = seq_lseek,
186 .release = single_release,
187};
188
189static int uuids_show(struct seq_file *f, void *p)
190{
191 struct hci_dev *hdev = f->private;
192 struct bt_uuid *uuid;
193
194 hci_dev_lock(hdev);
195 list_for_each_entry(uuid, &hdev->uuids, list) {
196 u8 i, val[16];
197
198 /* The Bluetooth UUID values are stored in big endian,
199 * but with reversed byte order. So convert them into
200 * the right order for the %pUb modifier.
201 */
202 for (i = 0; i < 16; i++)
203 val[i] = uuid->uuid[15 - i];
204
205 seq_printf(f, "%pUb\n", val);
206 }
207 hci_dev_unlock(hdev);
208
209 return 0;
210}
211
212static int uuids_open(struct inode *inode, struct file *file)
213{
214 return single_open(file, uuids_show, inode->i_private);
215}
216
217static const struct file_operations uuids_fops = {
218 .open = uuids_open,
219 .read = seq_read,
220 .llseek = seq_lseek,
221 .release = single_release,
222};
223
224static int inquiry_cache_show(struct seq_file *f, void *p)
225{
226 struct hci_dev *hdev = f->private;
227 struct discovery_state *cache = &hdev->discovery;
228 struct inquiry_entry *e;
229
230 hci_dev_lock(hdev);
231
232 list_for_each_entry(e, &cache->all, all) {
233 struct inquiry_data *data = &e->data;
234 seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
235 &data->bdaddr,
236 data->pscan_rep_mode, data->pscan_period_mode,
237 data->pscan_mode, data->dev_class[2],
238 data->dev_class[1], data->dev_class[0],
239 __le16_to_cpu(data->clock_offset),
240 data->rssi, data->ssp_mode, e->timestamp);
241 }
242
243 hci_dev_unlock(hdev);
244
245 return 0;
246}
247
248static int inquiry_cache_open(struct inode *inode, struct file *file)
249{
250 return single_open(file, inquiry_cache_show, inode->i_private);
251}
252
253static const struct file_operations inquiry_cache_fops = {
254 .open = inquiry_cache_open,
255 .read = seq_read,
256 .llseek = seq_lseek,
257 .release = single_release,
258};
259
260static int link_keys_show(struct seq_file *f, void *ptr)
261{
262 struct hci_dev *hdev = f->private;
263 struct list_head *p, *n;
264
265 hci_dev_lock(hdev);
266 list_for_each_safe(p, n, &hdev->link_keys) {
267 struct link_key *key = list_entry(p, struct link_key, list);
268 seq_printf(f, "%pMR %u %*phN %u\n", &key->bdaddr, key->type,
269 HCI_LINK_KEY_SIZE, key->val, key->pin_len);
270 }
271 hci_dev_unlock(hdev);
272
273 return 0;
274}
275
276static int link_keys_open(struct inode *inode, struct file *file)
277{
278 return single_open(file, link_keys_show, inode->i_private);
279}
280
281static const struct file_operations link_keys_fops = {
282 .open = link_keys_open,
283 .read = seq_read,
284 .llseek = seq_lseek,
285 .release = single_release,
286};
287
288static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
289 size_t count, loff_t *ppos)
290{
291 struct hci_dev *hdev = file->private_data;
292 char buf[3];
293
294 buf[0] = test_bit(HCI_DEBUG_KEYS, &hdev->dev_flags) ? 'Y': 'N';
295 buf[1] = '\n';
296 buf[2] = '\0';
297 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
298}
299
300static const struct file_operations use_debug_keys_fops = {
301 .open = simple_open,
302 .read = use_debug_keys_read,
303 .llseek = default_llseek,
304};
305
306static int dev_class_show(struct seq_file *f, void *ptr)
307{
308 struct hci_dev *hdev = f->private;
309
310 hci_dev_lock(hdev);
311 seq_printf(f, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
312 hdev->dev_class[1], hdev->dev_class[0]);
313 hci_dev_unlock(hdev);
314
315 return 0;
316}
317
318static int dev_class_open(struct inode *inode, struct file *file)
319{
320 return single_open(file, dev_class_show, inode->i_private);
321}
322
323static const struct file_operations dev_class_fops = {
324 .open = dev_class_open,
325 .read = seq_read,
326 .llseek = seq_lseek,
327 .release = single_release,
328};
329
330static int voice_setting_get(void *data, u64 *val)
331{
332 struct hci_dev *hdev = data;
333
334 hci_dev_lock(hdev);
335 *val = hdev->voice_setting;
336 hci_dev_unlock(hdev);
337
338 return 0;
339}
340
341DEFINE_SIMPLE_ATTRIBUTE(voice_setting_fops, voice_setting_get,
342 NULL, "0x%4.4llx\n");
343
344static int auto_accept_delay_set(void *data, u64 val)
345{
346 struct hci_dev *hdev = data;
347
348 hci_dev_lock(hdev);
349 hdev->auto_accept_delay = val;
350 hci_dev_unlock(hdev);
351
352 return 0;
353}
354
355static int auto_accept_delay_get(void *data, u64 *val)
356{
357 struct hci_dev *hdev = data;
358
359 hci_dev_lock(hdev);
360 *val = hdev->auto_accept_delay;
361 hci_dev_unlock(hdev);
362
363 return 0;
364}
365
366DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
367 auto_accept_delay_set, "%llu\n");
368
369static int ssp_debug_mode_set(void *data, u64 val)
370{
371 struct hci_dev *hdev = data;
372 struct sk_buff *skb;
373 __u8 mode;
374 int err;
375
376 if (val != 0 && val != 1)
377 return -EINVAL;
378
379 if (!test_bit(HCI_UP, &hdev->flags))
380 return -ENETDOWN;
381
382 hci_req_lock(hdev);
383 mode = val;
384 skb = __hci_cmd_sync(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, sizeof(mode),
385 &mode, HCI_CMD_TIMEOUT);
386 hci_req_unlock(hdev);
387
388 if (IS_ERR(skb))
389 return PTR_ERR(skb);
390
391 err = -bt_to_errno(skb->data[0]);
392 kfree_skb(skb);
393
394 if (err < 0)
395 return err;
396
397 hci_dev_lock(hdev);
398 hdev->ssp_debug_mode = val;
399 hci_dev_unlock(hdev);
400
401 return 0;
402}
403
404static int ssp_debug_mode_get(void *data, u64 *val)
405{
406 struct hci_dev *hdev = data;
407
408 hci_dev_lock(hdev);
409 *val = hdev->ssp_debug_mode;
410 hci_dev_unlock(hdev);
411
412 return 0;
413}
414
415DEFINE_SIMPLE_ATTRIBUTE(ssp_debug_mode_fops, ssp_debug_mode_get,
416 ssp_debug_mode_set, "%llu\n");
417
418static int idle_timeout_set(void *data, u64 val)
419{
420 struct hci_dev *hdev = data;
421
422 if (val != 0 && (val < 500 || val > 3600000))
423 return -EINVAL;
424
425 hci_dev_lock(hdev);
426 hdev->idle_timeout = val;
427 hci_dev_unlock(hdev);
428
429 return 0;
430}
431
432static int idle_timeout_get(void *data, u64 *val)
433{
434 struct hci_dev *hdev = data;
435
436 hci_dev_lock(hdev);
437 *val = hdev->idle_timeout;
438 hci_dev_unlock(hdev);
439
440 return 0;
441}
442
443DEFINE_SIMPLE_ATTRIBUTE(idle_timeout_fops, idle_timeout_get,
444 idle_timeout_set, "%llu\n");
445
446static int sniff_min_interval_set(void *data, u64 val)
447{
448 struct hci_dev *hdev = data;
449
450 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
451 return -EINVAL;
452
453 hci_dev_lock(hdev);
454 hdev->sniff_min_interval = val;
455 hci_dev_unlock(hdev);
456
457 return 0;
458}
459
460static int sniff_min_interval_get(void *data, u64 *val)
461{
462 struct hci_dev *hdev = data;
463
464 hci_dev_lock(hdev);
465 *val = hdev->sniff_min_interval;
466 hci_dev_unlock(hdev);
467
468 return 0;
469}
470
471DEFINE_SIMPLE_ATTRIBUTE(sniff_min_interval_fops, sniff_min_interval_get,
472 sniff_min_interval_set, "%llu\n");
473
474static int sniff_max_interval_set(void *data, u64 val)
475{
476 struct hci_dev *hdev = data;
477
478 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
479 return -EINVAL;
480
481 hci_dev_lock(hdev);
482 hdev->sniff_max_interval = val;
483 hci_dev_unlock(hdev);
484
485 return 0;
486}
487
488static int sniff_max_interval_get(void *data, u64 *val)
489{
490 struct hci_dev *hdev = data;
491
492 hci_dev_lock(hdev);
493 *val = hdev->sniff_max_interval;
494 hci_dev_unlock(hdev);
495
496 return 0;
497}
498
499DEFINE_SIMPLE_ATTRIBUTE(sniff_max_interval_fops, sniff_max_interval_get,
500 sniff_max_interval_set, "%llu\n");
501
502static int static_address_show(struct seq_file *f, void *p)
503{
504 struct hci_dev *hdev = f->private;
505
506 hci_dev_lock(hdev);
507 seq_printf(f, "%pMR\n", &hdev->static_addr);
508 hci_dev_unlock(hdev);
509
510 return 0;
511}
512
513static int static_address_open(struct inode *inode, struct file *file)
514{
515 return single_open(file, static_address_show, inode->i_private);
516}
517
518static const struct file_operations static_address_fops = {
519 .open = static_address_open,
520 .read = seq_read,
521 .llseek = seq_lseek,
522 .release = single_release,
523};
524
525static int own_address_type_set(void *data, u64 val)
526{
527 struct hci_dev *hdev = data;
528
529 if (val != 0 && val != 1)
530 return -EINVAL;
531
532 hci_dev_lock(hdev);
533 hdev->own_addr_type = val;
534 hci_dev_unlock(hdev);
535
536 return 0;
537}
538
539static int own_address_type_get(void *data, u64 *val)
540{
541 struct hci_dev *hdev = data;
542
543 hci_dev_lock(hdev);
544 *val = hdev->own_addr_type;
545 hci_dev_unlock(hdev);
546
547 return 0;
548}
549
550DEFINE_SIMPLE_ATTRIBUTE(own_address_type_fops, own_address_type_get,
551 own_address_type_set, "%llu\n");
552
553static int long_term_keys_show(struct seq_file *f, void *ptr)
554{
555 struct hci_dev *hdev = f->private;
556 struct list_head *p, *n;
557
558 hci_dev_lock(hdev);
559 list_for_each_safe(p, n, &hdev->link_keys) {
560 struct smp_ltk *ltk = list_entry(p, struct smp_ltk, list);
561 seq_printf(f, "%pMR (type %u) %u %u %u %.4x %*phN %*phN\\n",
562 &ltk->bdaddr, ltk->bdaddr_type, ltk->authenticated,
563 ltk->type, ltk->enc_size, __le16_to_cpu(ltk->ediv),
564 8, ltk->rand, 16, ltk->val);
565 }
566 hci_dev_unlock(hdev);
567
568 return 0;
569}
570
571static int long_term_keys_open(struct inode *inode, struct file *file)
572{
573 return single_open(file, long_term_keys_show, inode->i_private);
574}
575
576static const struct file_operations long_term_keys_fops = {
577 .open = long_term_keys_open,
578 .read = seq_read,
579 .llseek = seq_lseek,
580 .release = single_release,
581};
582
583static int conn_min_interval_set(void *data, u64 val)
584{
585 struct hci_dev *hdev = data;
586
587 if (val < 0x0006 || val > 0x0c80 || val > hdev->le_conn_max_interval)
588 return -EINVAL;
589
590 hci_dev_lock(hdev);
591 hdev->le_conn_min_interval = val;
592 hci_dev_unlock(hdev);
593
594 return 0;
595}
596
597static int conn_min_interval_get(void *data, u64 *val)
598{
599 struct hci_dev *hdev = data;
600
601 hci_dev_lock(hdev);
602 *val = hdev->le_conn_min_interval;
603 hci_dev_unlock(hdev);
604
605 return 0;
606}
607
608DEFINE_SIMPLE_ATTRIBUTE(conn_min_interval_fops, conn_min_interval_get,
609 conn_min_interval_set, "%llu\n");
610
611static int conn_max_interval_set(void *data, u64 val)
612{
613 struct hci_dev *hdev = data;
614
615 if (val < 0x0006 || val > 0x0c80 || val < hdev->le_conn_min_interval)
616 return -EINVAL;
617
618 hci_dev_lock(hdev);
619 hdev->le_conn_max_interval = val;
620 hci_dev_unlock(hdev);
621
622 return 0;
623}
624
625static int conn_max_interval_get(void *data, u64 *val)
626{
627 struct hci_dev *hdev = data;
628
629 hci_dev_lock(hdev);
630 *val = hdev->le_conn_max_interval;
631 hci_dev_unlock(hdev);
632
633 return 0;
634}
635
636DEFINE_SIMPLE_ATTRIBUTE(conn_max_interval_fops, conn_max_interval_get,
637 conn_max_interval_set, "%llu\n");
638
58/* ---- HCI requests ---- */ 639/* ---- HCI requests ---- */
59 640
60static void hci_req_sync_complete(struct hci_dev *hdev, u8 result) 641static void hci_req_sync_complete(struct hci_dev *hdev, u8 result)
@@ -307,11 +888,23 @@ static void amp_init(struct hci_request *req)
307 /* Read Local Version */ 888 /* Read Local Version */
308 hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL); 889 hci_req_add(req, HCI_OP_READ_LOCAL_VERSION, 0, NULL);
309 890
891 /* Read Local Supported Commands */
892 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
893
894 /* Read Local Supported Features */
895 hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
896
310 /* Read Local AMP Info */ 897 /* Read Local AMP Info */
311 hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); 898 hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
312 899
313 /* Read Data Blk size */ 900 /* Read Data Blk size */
314 hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL); 901 hci_req_add(req, HCI_OP_READ_DATA_BLOCK_SIZE, 0, NULL);
902
903 /* Read Flow Control Mode */
904 hci_req_add(req, HCI_OP_READ_FLOW_CONTROL_MODE, 0, NULL);
905
906 /* Read Location Data */
907 hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL);
315} 908}
316 909
317static void hci_init1_req(struct hci_request *req, unsigned long opt) 910static void hci_init1_req(struct hci_request *req, unsigned long opt)
@@ -341,6 +934,8 @@ static void hci_init1_req(struct hci_request *req, unsigned long opt)
341 934
342static void bredr_setup(struct hci_request *req) 935static void bredr_setup(struct hci_request *req)
343{ 936{
937 struct hci_dev *hdev = req->hdev;
938
344 __le16 param; 939 __le16 param;
345 __u8 flt_type; 940 __u8 flt_type;
346 941
@@ -356,6 +951,12 @@ static void bredr_setup(struct hci_request *req)
356 /* Read Voice Setting */ 951 /* Read Voice Setting */
357 hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL); 952 hci_req_add(req, HCI_OP_READ_VOICE_SETTING, 0, NULL);
358 953
954 /* Read Number of Supported IAC */
955 hci_req_add(req, HCI_OP_READ_NUM_SUPPORTED_IAC, 0, NULL);
956
957 /* Read Current IAC LAP */
958 hci_req_add(req, HCI_OP_READ_CURRENT_IAC_LAP, 0, NULL);
959
359 /* Clear Event Filters */ 960 /* Clear Event Filters */
360 flt_type = HCI_FLT_CLEAR_ALL; 961 flt_type = HCI_FLT_CLEAR_ALL;
361 hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type); 962 hci_req_add(req, HCI_OP_SET_EVENT_FLT, 1, &flt_type);
@@ -364,8 +965,10 @@ static void bredr_setup(struct hci_request *req)
364 param = __constant_cpu_to_le16(0x7d00); 965 param = __constant_cpu_to_le16(0x7d00);
365 hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, &param); 966 hci_req_add(req, HCI_OP_WRITE_CA_TIMEOUT, 2, &param);
366 967
367 /* Read page scan parameters */ 968 /* AVM Berlin (31), aka "BlueFRITZ!", reports version 1.2,
368 if (req->hdev->hci_ver > BLUETOOTH_VER_1_1) { 969 * but it does not support page scan related HCI commands.
970 */
971 if (hdev->manufacturer != 31 && hdev->hci_ver > BLUETOOTH_VER_1_1) {
369 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL); 972 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_ACTIVITY, 0, NULL);
370 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL); 973 hci_req_add(req, HCI_OP_READ_PAGE_SCAN_TYPE, 0, NULL);
371 } 974 }
@@ -519,6 +1122,8 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
519 1122
520 if (lmp_bredr_capable(hdev)) 1123 if (lmp_bredr_capable(hdev))
521 bredr_setup(req); 1124 bredr_setup(req);
1125 else
1126 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
522 1127
523 if (lmp_le_capable(hdev)) 1128 if (lmp_le_capable(hdev))
524 le_setup(req); 1129 le_setup(req);
@@ -532,6 +1137,14 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
532 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 1137 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
533 1138
534 if (lmp_ssp_capable(hdev)) { 1139 if (lmp_ssp_capable(hdev)) {
1140 /* When SSP is available, then the host features page
1141 * should also be available as well. However some
1142 * controllers list the max_page as 0 as long as SSP
1143 * has not been enabled. To achieve proper debugging
1144 * output, force the minimum max_page to 1 at least.
1145 */
1146 hdev->max_page = 0x01;
1147
535 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 1148 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
536 u8 mode = 0x01; 1149 u8 mode = 0x01;
537 hci_req_add(req, HCI_OP_WRITE_SSP_MODE, 1150 hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
@@ -607,6 +1220,34 @@ static void hci_set_le_support(struct hci_request *req)
607 &cp); 1220 &cp);
608} 1221}
609 1222
1223static void hci_set_event_mask_page_2(struct hci_request *req)
1224{
1225 struct hci_dev *hdev = req->hdev;
1226 u8 events[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
1227
1228 /* If Connectionless Slave Broadcast master role is supported
1229 * enable all necessary events for it.
1230 */
1231 if (hdev->features[2][0] & 0x01) {
1232 events[1] |= 0x40; /* Triggered Clock Capture */
1233 events[1] |= 0x80; /* Synchronization Train Complete */
1234 events[2] |= 0x10; /* Slave Page Response Timeout */
1235 events[2] |= 0x20; /* CSB Channel Map Change */
1236 }
1237
1238 /* If Connectionless Slave Broadcast slave role is supported
1239 * enable all necessary events for it.
1240 */
1241 if (hdev->features[2][0] & 0x02) {
1242 events[2] |= 0x01; /* Synchronization Train Received */
1243 events[2] |= 0x02; /* CSB Receive */
1244 events[2] |= 0x04; /* CSB Timeout */
1245 events[2] |= 0x08; /* Truncated Page Complete */
1246 }
1247
1248 hci_req_add(req, HCI_OP_SET_EVENT_MASK_PAGE_2, sizeof(events), events);
1249}
1250
610static void hci_init3_req(struct hci_request *req, unsigned long opt) 1251static void hci_init3_req(struct hci_request *req, unsigned long opt)
611{ 1252{
612 struct hci_dev *hdev = req->hdev; 1253 struct hci_dev *hdev = req->hdev;
@@ -634,8 +1275,17 @@ static void hci_init3_req(struct hci_request *req, unsigned long opt)
634 hci_setup_link_policy(req); 1275 hci_setup_link_policy(req);
635 1276
636 if (lmp_le_capable(hdev)) { 1277 if (lmp_le_capable(hdev)) {
1278 /* If the controller has a public BD_ADDR, then by
1279 * default use that one. If this is a LE only
1280 * controller without one, default to the random
1281 * address.
1282 */
1283 if (bacmp(&hdev->bdaddr, BDADDR_ANY))
1284 hdev->own_addr_type = ADDR_LE_DEV_PUBLIC;
1285 else
1286 hdev->own_addr_type = ADDR_LE_DEV_RANDOM;
1287
637 hci_set_le_support(req); 1288 hci_set_le_support(req);
638 hci_update_ad(req);
639 } 1289 }
640 1290
641 /* Read features beyond page 1 if available */ 1291 /* Read features beyond page 1 if available */
@@ -648,6 +1298,19 @@ static void hci_init3_req(struct hci_request *req, unsigned long opt)
648 } 1298 }
649} 1299}
650 1300
1301static void hci_init4_req(struct hci_request *req, unsigned long opt)
1302{
1303 struct hci_dev *hdev = req->hdev;
1304
1305 /* Set event mask page 2 if the HCI command for it is supported */
1306 if (hdev->commands[22] & 0x04)
1307 hci_set_event_mask_page_2(req);
1308
1309 /* Check for Synchronization Train support */
1310 if (hdev->features[2][0] & 0x04)
1311 hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
1312}
1313
651static int __hci_init(struct hci_dev *hdev) 1314static int __hci_init(struct hci_dev *hdev)
652{ 1315{
653 int err; 1316 int err;
@@ -656,6 +1319,14 @@ static int __hci_init(struct hci_dev *hdev)
656 if (err < 0) 1319 if (err < 0)
657 return err; 1320 return err;
658 1321
1322 /* The Device Under Test (DUT) mode is special and available for
1323 * all controller types. So just create it early on.
1324 */
1325 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
1326 debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
1327 &dut_mode_fops);
1328 }
1329
659 /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode 1330 /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode
660 * BR/EDR/LE type controllers. AMP controllers only need the 1331 * BR/EDR/LE type controllers. AMP controllers only need the
661 * first stage init. 1332 * first stage init.
@@ -667,7 +1338,75 @@ static int __hci_init(struct hci_dev *hdev)
667 if (err < 0) 1338 if (err < 0)
668 return err; 1339 return err;
669 1340
670 return __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); 1341 err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT);
1342 if (err < 0)
1343 return err;
1344
1345 err = __hci_req_sync(hdev, hci_init4_req, 0, HCI_INIT_TIMEOUT);
1346 if (err < 0)
1347 return err;
1348
1349 /* Only create debugfs entries during the initial setup
1350 * phase and not every time the controller gets powered on.
1351 */
1352 if (!test_bit(HCI_SETUP, &hdev->dev_flags))
1353 return 0;
1354
1355 debugfs_create_file("features", 0444, hdev->debugfs, hdev,
1356 &features_fops);
1357 debugfs_create_u16("manufacturer", 0444, hdev->debugfs,
1358 &hdev->manufacturer);
1359 debugfs_create_u8("hci_version", 0444, hdev->debugfs, &hdev->hci_ver);
1360 debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
1361 debugfs_create_file("blacklist", 0444, hdev->debugfs, hdev,
1362 &blacklist_fops);
1363 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
1364
1365 if (lmp_bredr_capable(hdev)) {
1366 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
1367 hdev, &inquiry_cache_fops);
1368 debugfs_create_file("link_keys", 0400, hdev->debugfs,
1369 hdev, &link_keys_fops);
1370 debugfs_create_file("use_debug_keys", 0444, hdev->debugfs,
1371 hdev, &use_debug_keys_fops);
1372 debugfs_create_file("dev_class", 0444, hdev->debugfs,
1373 hdev, &dev_class_fops);
1374 debugfs_create_file("voice_setting", 0444, hdev->debugfs,
1375 hdev, &voice_setting_fops);
1376 }
1377
1378 if (lmp_ssp_capable(hdev)) {
1379 debugfs_create_file("auto_accept_delay", 0644, hdev->debugfs,
1380 hdev, &auto_accept_delay_fops);
1381 debugfs_create_file("ssp_debug_mode", 0644, hdev->debugfs,
1382 hdev, &ssp_debug_mode_fops);
1383 }
1384
1385 if (lmp_sniff_capable(hdev)) {
1386 debugfs_create_file("idle_timeout", 0644, hdev->debugfs,
1387 hdev, &idle_timeout_fops);
1388 debugfs_create_file("sniff_min_interval", 0644, hdev->debugfs,
1389 hdev, &sniff_min_interval_fops);
1390 debugfs_create_file("sniff_max_interval", 0644, hdev->debugfs,
1391 hdev, &sniff_max_interval_fops);
1392 }
1393
1394 if (lmp_le_capable(hdev)) {
1395 debugfs_create_u8("white_list_size", 0444, hdev->debugfs,
1396 &hdev->le_white_list_size);
1397 debugfs_create_file("static_address", 0444, hdev->debugfs,
1398 hdev, &static_address_fops);
1399 debugfs_create_file("own_address_type", 0644, hdev->debugfs,
1400 hdev, &own_address_type_fops);
1401 debugfs_create_file("long_term_keys", 0400, hdev->debugfs,
1402 hdev, &long_term_keys_fops);
1403 debugfs_create_file("conn_min_interval", 0644, hdev->debugfs,
1404 hdev, &conn_min_interval_fops);
1405 debugfs_create_file("conn_max_interval", 0644, hdev->debugfs,
1406 hdev, &conn_max_interval_fops);
1407 }
1408
1409 return 0;
671} 1410}
672 1411
673static void hci_scan_req(struct hci_request *req, unsigned long opt) 1412static void hci_scan_req(struct hci_request *req, unsigned long opt)
@@ -984,6 +1723,21 @@ int hci_inquiry(void __user *arg)
984 if (!hdev) 1723 if (!hdev)
985 return -ENODEV; 1724 return -ENODEV;
986 1725
1726 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
1727 err = -EBUSY;
1728 goto done;
1729 }
1730
1731 if (hdev->dev_type != HCI_BREDR) {
1732 err = -EOPNOTSUPP;
1733 goto done;
1734 }
1735
1736 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
1737 err = -EOPNOTSUPP;
1738 goto done;
1739 }
1740
987 hci_dev_lock(hdev); 1741 hci_dev_lock(hdev);
988 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX || 1742 if (inquiry_cache_age(hdev) > INQUIRY_CACHE_AGE_MAX ||
989 inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) { 1743 inquiry_cache_empty(hdev) || ir.flags & IREQ_CACHE_FLUSH) {
@@ -1043,100 +1797,10 @@ done:
1043 return err; 1797 return err;
1044} 1798}
1045 1799
1046static u8 create_ad(struct hci_dev *hdev, u8 *ptr) 1800static int hci_dev_do_open(struct hci_dev *hdev)
1047{ 1801{
1048 u8 ad_len = 0, flags = 0;
1049 size_t name_len;
1050
1051 if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags))
1052 flags |= LE_AD_GENERAL;
1053
1054 if (!lmp_bredr_capable(hdev))
1055 flags |= LE_AD_NO_BREDR;
1056
1057 if (lmp_le_br_capable(hdev))
1058 flags |= LE_AD_SIM_LE_BREDR_CTRL;
1059
1060 if (lmp_host_le_br_capable(hdev))
1061 flags |= LE_AD_SIM_LE_BREDR_HOST;
1062
1063 if (flags) {
1064 BT_DBG("adv flags 0x%02x", flags);
1065
1066 ptr[0] = 2;
1067 ptr[1] = EIR_FLAGS;
1068 ptr[2] = flags;
1069
1070 ad_len += 3;
1071 ptr += 3;
1072 }
1073
1074 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
1075 ptr[0] = 2;
1076 ptr[1] = EIR_TX_POWER;
1077 ptr[2] = (u8) hdev->adv_tx_power;
1078
1079 ad_len += 3;
1080 ptr += 3;
1081 }
1082
1083 name_len = strlen(hdev->dev_name);
1084 if (name_len > 0) {
1085 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
1086
1087 if (name_len > max_len) {
1088 name_len = max_len;
1089 ptr[1] = EIR_NAME_SHORT;
1090 } else
1091 ptr[1] = EIR_NAME_COMPLETE;
1092
1093 ptr[0] = name_len + 1;
1094
1095 memcpy(ptr + 2, hdev->dev_name, name_len);
1096
1097 ad_len += (name_len + 2);
1098 ptr += (name_len + 2);
1099 }
1100
1101 return ad_len;
1102}
1103
1104void hci_update_ad(struct hci_request *req)
1105{
1106 struct hci_dev *hdev = req->hdev;
1107 struct hci_cp_le_set_adv_data cp;
1108 u8 len;
1109
1110 if (!lmp_le_capable(hdev))
1111 return;
1112
1113 memset(&cp, 0, sizeof(cp));
1114
1115 len = create_ad(hdev, cp.data);
1116
1117 if (hdev->adv_data_len == len &&
1118 memcmp(cp.data, hdev->adv_data, len) == 0)
1119 return;
1120
1121 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1122 hdev->adv_data_len = len;
1123
1124 cp.length = len;
1125
1126 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1127}
1128
1129/* ---- HCI ioctl helpers ---- */
1130
1131int hci_dev_open(__u16 dev)
1132{
1133 struct hci_dev *hdev;
1134 int ret = 0; 1802 int ret = 0;
1135 1803
1136 hdev = hci_dev_get(dev);
1137 if (!hdev)
1138 return -ENODEV;
1139
1140 BT_DBG("%s %p", hdev->name, hdev); 1804 BT_DBG("%s %p", hdev->name, hdev);
1141 1805
1142 hci_req_lock(hdev); 1806 hci_req_lock(hdev);
@@ -1146,13 +1810,29 @@ int hci_dev_open(__u16 dev)
1146 goto done; 1810 goto done;
1147 } 1811 }
1148 1812
1149 /* Check for rfkill but allow the HCI setup stage to proceed 1813 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) {
1150 * (which in itself doesn't cause any RF activity). 1814 /* Check for rfkill but allow the HCI setup stage to
1151 */ 1815 * proceed (which in itself doesn't cause any RF activity).
1152 if (test_bit(HCI_RFKILLED, &hdev->dev_flags) && 1816 */
1153 !test_bit(HCI_SETUP, &hdev->dev_flags)) { 1817 if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) {
1154 ret = -ERFKILL; 1818 ret = -ERFKILL;
1155 goto done; 1819 goto done;
1820 }
1821
1822 /* Check for valid public address or a configured static
1823 * random adddress, but let the HCI setup proceed to
1824 * be able to determine if there is a public address
1825 * or not.
1826 *
1827 * This check is only valid for BR/EDR controllers
1828 * since AMP controllers do not have an address.
1829 */
1830 if (hdev->dev_type == HCI_BREDR &&
1831 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
1832 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
1833 ret = -EADDRNOTAVAIL;
1834 goto done;
1835 }
1156 } 1836 }
1157 1837
1158 if (test_bit(HCI_UP, &hdev->flags)) { 1838 if (test_bit(HCI_UP, &hdev->flags)) {
@@ -1172,16 +1852,11 @@ int hci_dev_open(__u16 dev)
1172 ret = hdev->setup(hdev); 1852 ret = hdev->setup(hdev);
1173 1853
1174 if (!ret) { 1854 if (!ret) {
1175 /* Treat all non BR/EDR controllers as raw devices if
1176 * enable_hs is not set.
1177 */
1178 if (hdev->dev_type != HCI_BREDR && !enable_hs)
1179 set_bit(HCI_RAW, &hdev->flags);
1180
1181 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 1855 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
1182 set_bit(HCI_RAW, &hdev->flags); 1856 set_bit(HCI_RAW, &hdev->flags);
1183 1857
1184 if (!test_bit(HCI_RAW, &hdev->flags)) 1858 if (!test_bit(HCI_RAW, &hdev->flags) &&
1859 !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
1185 ret = __hci_init(hdev); 1860 ret = __hci_init(hdev);
1186 } 1861 }
1187 1862
@@ -1192,7 +1867,8 @@ int hci_dev_open(__u16 dev)
1192 set_bit(HCI_UP, &hdev->flags); 1867 set_bit(HCI_UP, &hdev->flags);
1193 hci_notify(hdev, HCI_DEV_UP); 1868 hci_notify(hdev, HCI_DEV_UP);
1194 if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 1869 if (!test_bit(HCI_SETUP, &hdev->dev_flags) &&
1195 mgmt_valid_hdev(hdev)) { 1870 !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) &&
1871 hdev->dev_type == HCI_BREDR) {
1196 hci_dev_lock(hdev); 1872 hci_dev_lock(hdev);
1197 mgmt_powered(hdev, 1); 1873 mgmt_powered(hdev, 1);
1198 hci_dev_unlock(hdev); 1874 hci_dev_unlock(hdev);
@@ -1220,10 +1896,41 @@ int hci_dev_open(__u16 dev)
1220 1896
1221done: 1897done:
1222 hci_req_unlock(hdev); 1898 hci_req_unlock(hdev);
1223 hci_dev_put(hdev);
1224 return ret; 1899 return ret;
1225} 1900}
1226 1901
1902/* ---- HCI ioctl helpers ---- */
1903
1904int hci_dev_open(__u16 dev)
1905{
1906 struct hci_dev *hdev;
1907 int err;
1908
1909 hdev = hci_dev_get(dev);
1910 if (!hdev)
1911 return -ENODEV;
1912
1913 /* We need to ensure that no other power on/off work is pending
1914 * before proceeding to call hci_dev_do_open. This is
1915 * particularly important if the setup procedure has not yet
1916 * completed.
1917 */
1918 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1919 cancel_delayed_work(&hdev->power_off);
1920
1921 /* After this call it is guaranteed that the setup procedure
1922 * has finished. This means that error conditions like RFKILL
1923 * or no valid public or static random address apply.
1924 */
1925 flush_workqueue(hdev->req_workqueue);
1926
1927 err = hci_dev_do_open(hdev);
1928
1929 hci_dev_put(hdev);
1930
1931 return err;
1932}
1933
1227static int hci_dev_do_close(struct hci_dev *hdev) 1934static int hci_dev_do_close(struct hci_dev *hdev)
1228{ 1935{
1229 BT_DBG("%s %p", hdev->name, hdev); 1936 BT_DBG("%s %p", hdev->name, hdev);
@@ -1247,6 +1954,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1247 cancel_delayed_work(&hdev->discov_off); 1954 cancel_delayed_work(&hdev->discov_off);
1248 hdev->discov_timeout = 0; 1955 hdev->discov_timeout = 0;
1249 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 1956 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1957 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1250 } 1958 }
1251 1959
1252 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 1960 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
@@ -1268,6 +1976,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1268 skb_queue_purge(&hdev->cmd_q); 1976 skb_queue_purge(&hdev->cmd_q);
1269 atomic_set(&hdev->cmd_cnt, 1); 1977 atomic_set(&hdev->cmd_cnt, 1);
1270 if (!test_bit(HCI_RAW, &hdev->flags) && 1978 if (!test_bit(HCI_RAW, &hdev->flags) &&
1979 !test_bit(HCI_AUTO_OFF, &hdev->dev_flags) &&
1271 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 1980 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
1272 set_bit(HCI_INIT, &hdev->flags); 1981 set_bit(HCI_INIT, &hdev->flags);
1273 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); 1982 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
@@ -1300,15 +2009,16 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1300 hdev->flags = 0; 2009 hdev->flags = 0;
1301 hdev->dev_flags &= ~HCI_PERSISTENT_MASK; 2010 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
1302 2011
1303 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags) && 2012 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
1304 mgmt_valid_hdev(hdev)) { 2013 if (hdev->dev_type == HCI_BREDR) {
1305 hci_dev_lock(hdev); 2014 hci_dev_lock(hdev);
1306 mgmt_powered(hdev, 0); 2015 mgmt_powered(hdev, 0);
1307 hci_dev_unlock(hdev); 2016 hci_dev_unlock(hdev);
2017 }
1308 } 2018 }
1309 2019
1310 /* Controller radio is available but is currently powered down */ 2020 /* Controller radio is available but is currently powered down */
1311 hdev->amp_status = 0; 2021 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
1312 2022
1313 memset(hdev->eir, 0, sizeof(hdev->eir)); 2023 memset(hdev->eir, 0, sizeof(hdev->eir));
1314 memset(hdev->dev_class, 0, sizeof(hdev->dev_class)); 2024 memset(hdev->dev_class, 0, sizeof(hdev->dev_class));
@@ -1328,11 +2038,17 @@ int hci_dev_close(__u16 dev)
1328 if (!hdev) 2038 if (!hdev)
1329 return -ENODEV; 2039 return -ENODEV;
1330 2040
2041 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
2042 err = -EBUSY;
2043 goto done;
2044 }
2045
1331 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 2046 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags))
1332 cancel_delayed_work(&hdev->power_off); 2047 cancel_delayed_work(&hdev->power_off);
1333 2048
1334 err = hci_dev_do_close(hdev); 2049 err = hci_dev_do_close(hdev);
1335 2050
2051done:
1336 hci_dev_put(hdev); 2052 hci_dev_put(hdev);
1337 return err; 2053 return err;
1338} 2054}
@@ -1348,8 +2064,15 @@ int hci_dev_reset(__u16 dev)
1348 2064
1349 hci_req_lock(hdev); 2065 hci_req_lock(hdev);
1350 2066
1351 if (!test_bit(HCI_UP, &hdev->flags)) 2067 if (!test_bit(HCI_UP, &hdev->flags)) {
2068 ret = -ENETDOWN;
1352 goto done; 2069 goto done;
2070 }
2071
2072 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
2073 ret = -EBUSY;
2074 goto done;
2075 }
1353 2076
1354 /* Drop queues */ 2077 /* Drop queues */
1355 skb_queue_purge(&hdev->rx_q); 2078 skb_queue_purge(&hdev->rx_q);
@@ -1384,10 +2107,15 @@ int hci_dev_reset_stat(__u16 dev)
1384 if (!hdev) 2107 if (!hdev)
1385 return -ENODEV; 2108 return -ENODEV;
1386 2109
2110 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
2111 ret = -EBUSY;
2112 goto done;
2113 }
2114
1387 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats)); 2115 memset(&hdev->stat, 0, sizeof(struct hci_dev_stats));
1388 2116
2117done:
1389 hci_dev_put(hdev); 2118 hci_dev_put(hdev);
1390
1391 return ret; 2119 return ret;
1392} 2120}
1393 2121
@@ -1404,6 +2132,21 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
1404 if (!hdev) 2132 if (!hdev)
1405 return -ENODEV; 2133 return -ENODEV;
1406 2134
2135 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
2136 err = -EBUSY;
2137 goto done;
2138 }
2139
2140 if (hdev->dev_type != HCI_BREDR) {
2141 err = -EOPNOTSUPP;
2142 goto done;
2143 }
2144
2145 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
2146 err = -EOPNOTSUPP;
2147 goto done;
2148 }
2149
1407 switch (cmd) { 2150 switch (cmd) {
1408 case HCISETAUTH: 2151 case HCISETAUTH:
1409 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt, 2152 err = hci_req_sync(hdev, hci_auth_req, dr.dev_opt,
@@ -1462,6 +2205,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
1462 break; 2205 break;
1463 } 2206 }
1464 2207
2208done:
1465 hci_dev_put(hdev); 2209 hci_dev_put(hdev);
1466 return err; 2210 return err;
1467} 2211}
@@ -1534,7 +2278,7 @@ int hci_get_dev_info(void __user *arg)
1534 2278
1535 strcpy(di.name, hdev->name); 2279 strcpy(di.name, hdev->name);
1536 di.bdaddr = hdev->bdaddr; 2280 di.bdaddr = hdev->bdaddr;
1537 di.type = (hdev->bus & 0x0f) | (hdev->dev_type << 4); 2281 di.type = (hdev->bus & 0x0f) | ((hdev->dev_type & 0x03) << 4);
1538 di.flags = hdev->flags; 2282 di.flags = hdev->flags;
1539 di.pkt_type = hdev->pkt_type; 2283 di.pkt_type = hdev->pkt_type;
1540 if (lmp_bredr_capable(hdev)) { 2284 if (lmp_bredr_capable(hdev)) {
@@ -1570,6 +2314,9 @@ static int hci_rfkill_set_block(void *data, bool blocked)
1570 2314
1571 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); 2315 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
1572 2316
2317 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
2318 return -EBUSY;
2319
1573 if (blocked) { 2320 if (blocked) {
1574 set_bit(HCI_RFKILLED, &hdev->dev_flags); 2321 set_bit(HCI_RFKILLED, &hdev->dev_flags);
1575 if (!test_bit(HCI_SETUP, &hdev->dev_flags)) 2322 if (!test_bit(HCI_SETUP, &hdev->dev_flags))
@@ -1592,13 +2339,20 @@ static void hci_power_on(struct work_struct *work)
1592 2339
1593 BT_DBG("%s", hdev->name); 2340 BT_DBG("%s", hdev->name);
1594 2341
1595 err = hci_dev_open(hdev->id); 2342 err = hci_dev_do_open(hdev);
1596 if (err < 0) { 2343 if (err < 0) {
1597 mgmt_set_powered_failed(hdev, err); 2344 mgmt_set_powered_failed(hdev, err);
1598 return; 2345 return;
1599 } 2346 }
1600 2347
1601 if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { 2348 /* During the HCI setup phase, a few error conditions are
2349 * ignored and they need to be checked now. If they are still
2350 * valid, it is important to turn the device back off.
2351 */
2352 if (test_bit(HCI_RFKILLED, &hdev->dev_flags) ||
2353 (hdev->dev_type == HCI_BREDR &&
2354 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2355 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
1602 clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); 2356 clear_bit(HCI_AUTO_OFF, &hdev->dev_flags);
1603 hci_dev_do_close(hdev); 2357 hci_dev_do_close(hdev);
1604 } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 2358 } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
@@ -1623,19 +2377,12 @@ static void hci_power_off(struct work_struct *work)
1623static void hci_discov_off(struct work_struct *work) 2377static void hci_discov_off(struct work_struct *work)
1624{ 2378{
1625 struct hci_dev *hdev; 2379 struct hci_dev *hdev;
1626 u8 scan = SCAN_PAGE;
1627 2380
1628 hdev = container_of(work, struct hci_dev, discov_off.work); 2381 hdev = container_of(work, struct hci_dev, discov_off.work);
1629 2382
1630 BT_DBG("%s", hdev->name); 2383 BT_DBG("%s", hdev->name);
1631 2384
1632 hci_dev_lock(hdev); 2385 mgmt_discoverable_timeout(hdev);
1633
1634 hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
1635
1636 hdev->discov_timeout = 0;
1637
1638 hci_dev_unlock(hdev);
1639} 2386}
1640 2387
1641int hci_uuids_clear(struct hci_dev *hdev) 2388int hci_uuids_clear(struct hci_dev *hdev)
@@ -1958,13 +2705,15 @@ int hci_add_remote_oob_data(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 *hash,
1958 return 0; 2705 return 0;
1959} 2706}
1960 2707
1961struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev, bdaddr_t *bdaddr) 2708struct bdaddr_list *hci_blacklist_lookup(struct hci_dev *hdev,
2709 bdaddr_t *bdaddr, u8 type)
1962{ 2710{
1963 struct bdaddr_list *b; 2711 struct bdaddr_list *b;
1964 2712
1965 list_for_each_entry(b, &hdev->blacklist, list) 2713 list_for_each_entry(b, &hdev->blacklist, list) {
1966 if (bacmp(bdaddr, &b->bdaddr) == 0) 2714 if (!bacmp(&b->bdaddr, bdaddr) && b->bdaddr_type == type)
1967 return b; 2715 return b;
2716 }
1968 2717
1969 return NULL; 2718 return NULL;
1970} 2719}
@@ -1974,9 +2723,7 @@ int hci_blacklist_clear(struct hci_dev *hdev)
1974 struct list_head *p, *n; 2723 struct list_head *p, *n;
1975 2724
1976 list_for_each_safe(p, n, &hdev->blacklist) { 2725 list_for_each_safe(p, n, &hdev->blacklist) {
1977 struct bdaddr_list *b; 2726 struct bdaddr_list *b = list_entry(p, struct bdaddr_list, list);
1978
1979 b = list_entry(p, struct bdaddr_list, list);
1980 2727
1981 list_del(p); 2728 list_del(p);
1982 kfree(b); 2729 kfree(b);
@@ -1989,10 +2736,10 @@ int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
1989{ 2736{
1990 struct bdaddr_list *entry; 2737 struct bdaddr_list *entry;
1991 2738
1992 if (bacmp(bdaddr, BDADDR_ANY) == 0) 2739 if (!bacmp(bdaddr, BDADDR_ANY))
1993 return -EBADF; 2740 return -EBADF;
1994 2741
1995 if (hci_blacklist_lookup(hdev, bdaddr)) 2742 if (hci_blacklist_lookup(hdev, bdaddr, type))
1996 return -EEXIST; 2743 return -EEXIST;
1997 2744
1998 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL); 2745 entry = kzalloc(sizeof(struct bdaddr_list), GFP_KERNEL);
@@ -2000,6 +2747,7 @@ int hci_blacklist_add(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
2000 return -ENOMEM; 2747 return -ENOMEM;
2001 2748
2002 bacpy(&entry->bdaddr, bdaddr); 2749 bacpy(&entry->bdaddr, bdaddr);
2750 entry->bdaddr_type = type;
2003 2751
2004 list_add(&entry->list, &hdev->blacklist); 2752 list_add(&entry->list, &hdev->blacklist);
2005 2753
@@ -2010,10 +2758,10 @@ int hci_blacklist_del(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
2010{ 2758{
2011 struct bdaddr_list *entry; 2759 struct bdaddr_list *entry;
2012 2760
2013 if (bacmp(bdaddr, BDADDR_ANY) == 0) 2761 if (!bacmp(bdaddr, BDADDR_ANY))
2014 return hci_blacklist_clear(hdev); 2762 return hci_blacklist_clear(hdev);
2015 2763
2016 entry = hci_blacklist_lookup(hdev, bdaddr); 2764 entry = hci_blacklist_lookup(hdev, bdaddr, type);
2017 if (!entry) 2765 if (!entry)
2018 return -ENOENT; 2766 return -ENOENT;
2019 2767
@@ -2111,13 +2859,19 @@ struct hci_dev *hci_alloc_dev(void)
2111 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1); 2859 hdev->pkt_type = (HCI_DM1 | HCI_DH1 | HCI_HV1);
2112 hdev->esco_type = (ESCO_HV1); 2860 hdev->esco_type = (ESCO_HV1);
2113 hdev->link_mode = (HCI_LM_ACCEPT); 2861 hdev->link_mode = (HCI_LM_ACCEPT);
2114 hdev->io_capability = 0x03; /* No Input No Output */ 2862 hdev->num_iac = 0x01; /* One IAC support is mandatory */
2863 hdev->io_capability = 0x03; /* No Input No Output */
2115 hdev->inq_tx_power = HCI_TX_POWER_INVALID; 2864 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
2116 hdev->adv_tx_power = HCI_TX_POWER_INVALID; 2865 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
2117 2866
2118 hdev->sniff_max_interval = 800; 2867 hdev->sniff_max_interval = 800;
2119 hdev->sniff_min_interval = 80; 2868 hdev->sniff_min_interval = 80;
2120 2869
2870 hdev->le_scan_interval = 0x0060;
2871 hdev->le_scan_window = 0x0030;
2872 hdev->le_conn_min_interval = 0x0028;
2873 hdev->le_conn_max_interval = 0x0038;
2874
2121 mutex_init(&hdev->lock); 2875 mutex_init(&hdev->lock);
2122 mutex_init(&hdev->req_lock); 2876 mutex_init(&hdev->req_lock);
2123 2877
@@ -2206,7 +2960,12 @@ int hci_register_dev(struct hci_dev *hdev)
2206 goto err; 2960 goto err;
2207 } 2961 }
2208 2962
2209 error = hci_add_sysfs(hdev); 2963 if (!IS_ERR_OR_NULL(bt_debugfs))
2964 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
2965
2966 dev_set_name(&hdev->dev, "%s", hdev->name);
2967
2968 error = device_add(&hdev->dev);
2210 if (error < 0) 2969 if (error < 0)
2211 goto err_wqueue; 2970 goto err_wqueue;
2212 2971
@@ -2224,9 +2983,14 @@ int hci_register_dev(struct hci_dev *hdev)
2224 set_bit(HCI_RFKILLED, &hdev->dev_flags); 2983 set_bit(HCI_RFKILLED, &hdev->dev_flags);
2225 2984
2226 set_bit(HCI_SETUP, &hdev->dev_flags); 2985 set_bit(HCI_SETUP, &hdev->dev_flags);
2986 set_bit(HCI_AUTO_OFF, &hdev->dev_flags);
2227 2987
2228 if (hdev->dev_type != HCI_AMP) 2988 if (hdev->dev_type == HCI_BREDR) {
2229 set_bit(HCI_AUTO_OFF, &hdev->dev_flags); 2989 /* Assume BR/EDR support until proven otherwise (such as
2990 * through reading supported features during init.
2991 */
2992 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
2993 }
2230 2994
2231 write_lock(&hci_dev_list_lock); 2995 write_lock(&hci_dev_list_lock);
2232 list_add(&hdev->list, &hci_dev_list); 2996 list_add(&hdev->list, &hci_dev_list);
@@ -2289,7 +3053,9 @@ void hci_unregister_dev(struct hci_dev *hdev)
2289 rfkill_destroy(hdev->rfkill); 3053 rfkill_destroy(hdev->rfkill);
2290 } 3054 }
2291 3055
2292 hci_del_sysfs(hdev); 3056 device_del(&hdev->dev);
3057
3058 debugfs_remove_recursive(hdev->debugfs);
2293 3059
2294 destroy_workqueue(hdev->workqueue); 3060 destroy_workqueue(hdev->workqueue);
2295 destroy_workqueue(hdev->req_workqueue); 3061 destroy_workqueue(hdev->req_workqueue);
@@ -2325,9 +3091,8 @@ int hci_resume_dev(struct hci_dev *hdev)
2325EXPORT_SYMBOL(hci_resume_dev); 3091EXPORT_SYMBOL(hci_resume_dev);
2326 3092
2327/* Receive frame from HCI drivers */ 3093/* Receive frame from HCI drivers */
2328int hci_recv_frame(struct sk_buff *skb) 3094int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
2329{ 3095{
2330 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2331 if (!hdev || (!test_bit(HCI_UP, &hdev->flags) 3096 if (!hdev || (!test_bit(HCI_UP, &hdev->flags)
2332 && !test_bit(HCI_INIT, &hdev->flags))) { 3097 && !test_bit(HCI_INIT, &hdev->flags))) {
2333 kfree_skb(skb); 3098 kfree_skb(skb);
@@ -2386,7 +3151,6 @@ static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
2386 scb->expect = hlen; 3151 scb->expect = hlen;
2387 scb->pkt_type = type; 3152 scb->pkt_type = type;
2388 3153
2389 skb->dev = (void *) hdev;
2390 hdev->reassembly[index] = skb; 3154 hdev->reassembly[index] = skb;
2391 } 3155 }
2392 3156
@@ -2446,7 +3210,7 @@ static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
2446 /* Complete frame */ 3210 /* Complete frame */
2447 3211
2448 bt_cb(skb)->pkt_type = type; 3212 bt_cb(skb)->pkt_type = type;
2449 hci_recv_frame(skb); 3213 hci_recv_frame(hdev, skb);
2450 3214
2451 hdev->reassembly[index] = NULL; 3215 hdev->reassembly[index] = NULL;
2452 return remain; 3216 return remain;
@@ -2537,15 +3301,8 @@ int hci_unregister_cb(struct hci_cb *cb)
2537} 3301}
2538EXPORT_SYMBOL(hci_unregister_cb); 3302EXPORT_SYMBOL(hci_unregister_cb);
2539 3303
2540static int hci_send_frame(struct sk_buff *skb) 3304static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
2541{ 3305{
2542 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
2543
2544 if (!hdev) {
2545 kfree_skb(skb);
2546 return -ENODEV;
2547 }
2548
2549 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); 3306 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
2550 3307
2551 /* Time stamp */ 3308 /* Time stamp */
@@ -2562,7 +3319,8 @@ static int hci_send_frame(struct sk_buff *skb)
2562 /* Get rid of skb owner, prior to sending to the driver. */ 3319 /* Get rid of skb owner, prior to sending to the driver. */
2563 skb_orphan(skb); 3320 skb_orphan(skb);
2564 3321
2565 return hdev->send(skb); 3322 if (hdev->send(hdev, skb) < 0)
3323 BT_ERR("%s sending frame failed", hdev->name);
2566} 3324}
2567 3325
2568void hci_req_init(struct hci_request *req, struct hci_dev *hdev) 3326void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
@@ -2625,7 +3383,6 @@ static struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode,
2625 BT_DBG("skb len %d", skb->len); 3383 BT_DBG("skb len %d", skb->len);
2626 3384
2627 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT; 3385 bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
2628 skb->dev = (void *) hdev;
2629 3386
2630 return skb; 3387 return skb;
2631} 3388}
@@ -2769,7 +3526,6 @@ static void hci_queue_acl(struct hci_chan *chan, struct sk_buff_head *queue,
2769 do { 3526 do {
2770 skb = list; list = list->next; 3527 skb = list; list = list->next;
2771 3528
2772 skb->dev = (void *) hdev;
2773 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; 3529 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
2774 hci_add_acl_hdr(skb, conn->handle, flags); 3530 hci_add_acl_hdr(skb, conn->handle, flags);
2775 3531
@@ -2788,8 +3544,6 @@ void hci_send_acl(struct hci_chan *chan, struct sk_buff *skb, __u16 flags)
2788 3544
2789 BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags); 3545 BT_DBG("%s chan %p flags 0x%4.4x", hdev->name, chan, flags);
2790 3546
2791 skb->dev = (void *) hdev;
2792
2793 hci_queue_acl(chan, &chan->data_q, skb, flags); 3547 hci_queue_acl(chan, &chan->data_q, skb, flags);
2794 3548
2795 queue_work(hdev->workqueue, &hdev->tx_work); 3549 queue_work(hdev->workqueue, &hdev->tx_work);
@@ -2810,7 +3564,6 @@ void hci_send_sco(struct hci_conn *conn, struct sk_buff *skb)
2810 skb_reset_transport_header(skb); 3564 skb_reset_transport_header(skb);
2811 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE); 3565 memcpy(skb_transport_header(skb), &hdr, HCI_SCO_HDR_SIZE);
2812 3566
2813 skb->dev = (void *) hdev;
2814 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT; 3567 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
2815 3568
2816 skb_queue_tail(&conn->data_q, skb); 3569 skb_queue_tail(&conn->data_q, skb);
@@ -3075,7 +3828,7 @@ static void hci_sched_acl_pkt(struct hci_dev *hdev)
3075 hci_conn_enter_active_mode(chan->conn, 3828 hci_conn_enter_active_mode(chan->conn,
3076 bt_cb(skb)->force_active); 3829 bt_cb(skb)->force_active);
3077 3830
3078 hci_send_frame(skb); 3831 hci_send_frame(hdev, skb);
3079 hdev->acl_last_tx = jiffies; 3832 hdev->acl_last_tx = jiffies;
3080 3833
3081 hdev->acl_cnt--; 3834 hdev->acl_cnt--;
@@ -3127,7 +3880,7 @@ static void hci_sched_acl_blk(struct hci_dev *hdev)
3127 hci_conn_enter_active_mode(chan->conn, 3880 hci_conn_enter_active_mode(chan->conn,
3128 bt_cb(skb)->force_active); 3881 bt_cb(skb)->force_active);
3129 3882
3130 hci_send_frame(skb); 3883 hci_send_frame(hdev, skb);
3131 hdev->acl_last_tx = jiffies; 3884 hdev->acl_last_tx = jiffies;
3132 3885
3133 hdev->block_cnt -= blocks; 3886 hdev->block_cnt -= blocks;
@@ -3180,7 +3933,7 @@ static void hci_sched_sco(struct hci_dev *hdev)
3180 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) { 3933 while (hdev->sco_cnt && (conn = hci_low_sent(hdev, SCO_LINK, &quote))) {
3181 while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 3934 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
3182 BT_DBG("skb %p len %d", skb, skb->len); 3935 BT_DBG("skb %p len %d", skb, skb->len);
3183 hci_send_frame(skb); 3936 hci_send_frame(hdev, skb);
3184 3937
3185 conn->sent++; 3938 conn->sent++;
3186 if (conn->sent == ~0) 3939 if (conn->sent == ~0)
@@ -3204,7 +3957,7 @@ static void hci_sched_esco(struct hci_dev *hdev)
3204 &quote))) { 3957 &quote))) {
3205 while (quote-- && (skb = skb_dequeue(&conn->data_q))) { 3958 while (quote-- && (skb = skb_dequeue(&conn->data_q))) {
3206 BT_DBG("skb %p len %d", skb, skb->len); 3959 BT_DBG("skb %p len %d", skb, skb->len);
3207 hci_send_frame(skb); 3960 hci_send_frame(hdev, skb);
3208 3961
3209 conn->sent++; 3962 conn->sent++;
3210 if (conn->sent == ~0) 3963 if (conn->sent == ~0)
@@ -3246,7 +3999,7 @@ static void hci_sched_le(struct hci_dev *hdev)
3246 3999
3247 skb = skb_dequeue(&chan->data_q); 4000 skb = skb_dequeue(&chan->data_q);
3248 4001
3249 hci_send_frame(skb); 4002 hci_send_frame(hdev, skb);
3250 hdev->le_last_tx = jiffies; 4003 hdev->le_last_tx = jiffies;
3251 4004
3252 cnt--; 4005 cnt--;
@@ -3272,19 +4025,17 @@ static void hci_tx_work(struct work_struct *work)
3272 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 4025 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
3273 hdev->sco_cnt, hdev->le_cnt); 4026 hdev->sco_cnt, hdev->le_cnt);
3274 4027
3275 /* Schedule queues and send stuff to HCI driver */ 4028 if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
3276 4029 /* Schedule queues and send stuff to HCI driver */
3277 hci_sched_acl(hdev); 4030 hci_sched_acl(hdev);
3278 4031 hci_sched_sco(hdev);
3279 hci_sched_sco(hdev); 4032 hci_sched_esco(hdev);
3280 4033 hci_sched_le(hdev);
3281 hci_sched_esco(hdev); 4034 }
3282
3283 hci_sched_le(hdev);
3284 4035
3285 /* Send next queued raw (unknown type) packet */ 4036 /* Send next queued raw (unknown type) packet */
3286 while ((skb = skb_dequeue(&hdev->raw_q))) 4037 while ((skb = skb_dequeue(&hdev->raw_q)))
3287 hci_send_frame(skb); 4038 hci_send_frame(hdev, skb);
3288} 4039}
3289 4040
3290/* ----- HCI RX task (incoming data processing) ----- */ 4041/* ----- HCI RX task (incoming data processing) ----- */
@@ -3471,7 +4222,8 @@ static void hci_rx_work(struct work_struct *work)
3471 hci_send_to_sock(hdev, skb); 4222 hci_send_to_sock(hdev, skb);
3472 } 4223 }
3473 4224
3474 if (test_bit(HCI_RAW, &hdev->flags)) { 4225 if (test_bit(HCI_RAW, &hdev->flags) ||
4226 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
3475 kfree_skb(skb); 4227 kfree_skb(skb);
3476 continue; 4228 continue;
3477 } 4229 }
@@ -3526,10 +4278,10 @@ static void hci_cmd_work(struct work_struct *work)
3526 4278
3527 kfree_skb(hdev->sent_cmd); 4279 kfree_skb(hdev->sent_cmd);
3528 4280
3529 hdev->sent_cmd = skb_clone(skb, GFP_ATOMIC); 4281 hdev->sent_cmd = skb_clone(skb, GFP_KERNEL);
3530 if (hdev->sent_cmd) { 4282 if (hdev->sent_cmd) {
3531 atomic_dec(&hdev->cmd_cnt); 4283 atomic_dec(&hdev->cmd_cnt);
3532 hci_send_frame(skb); 4284 hci_send_frame(hdev, skb);
3533 if (test_bit(HCI_RESET, &hdev->flags)) 4285 if (test_bit(HCI_RESET, &hdev->flags))
3534 del_timer(&hdev->cmd_timer); 4286 del_timer(&hdev->cmd_timer);
3535 else 4287 else
@@ -3541,15 +4293,3 @@ static void hci_cmd_work(struct work_struct *work)
3541 } 4293 }
3542 } 4294 }
3543} 4295}
3544
3545u8 bdaddr_to_le(u8 bdaddr_type)
3546{
3547 switch (bdaddr_type) {
3548 case BDADDR_LE_PUBLIC:
3549 return ADDR_LE_DEV_PUBLIC;
3550
3551 default:
3552 /* Fallback to LE Random address type */
3553 return ADDR_LE_DEV_RANDOM;
3554 }
3555}
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index 8db3e89fae35..5935f748c0f9 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -29,8 +29,9 @@
29#include <net/bluetooth/bluetooth.h> 29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h> 30#include <net/bluetooth/hci_core.h>
31#include <net/bluetooth/mgmt.h> 31#include <net/bluetooth/mgmt.h>
32#include <net/bluetooth/a2mp.h> 32
33#include <net/bluetooth/amp.h> 33#include "a2mp.h"
34#include "amp.h"
34 35
35/* Handle HCI Event packets */ 36/* Handle HCI Event packets */
36 37
@@ -194,6 +195,11 @@ static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
194 195
195 memset(hdev->adv_data, 0, sizeof(hdev->adv_data)); 196 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
196 hdev->adv_data_len = 0; 197 hdev->adv_data_len = 0;
198
199 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
200 hdev->scan_rsp_data_len = 0;
201
202 hdev->ssp_debug_mode = 0;
197} 203}
198 204
199static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb) 205static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
@@ -297,6 +303,11 @@ static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
297 goto done; 303 goto done;
298 } 304 }
299 305
306 /* We need to ensure that we set this back on if someone changed
307 * the scan mode through a raw HCI socket.
308 */
309 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
310
300 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags); 311 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
301 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags); 312 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
302 313
@@ -304,11 +315,6 @@ static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
304 set_bit(HCI_ISCAN, &hdev->flags); 315 set_bit(HCI_ISCAN, &hdev->flags);
305 if (!old_iscan) 316 if (!old_iscan)
306 mgmt_discoverable(hdev, 1); 317 mgmt_discoverable(hdev, 1);
307 if (hdev->discov_timeout > 0) {
308 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
309 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
310 to);
311 }
312 } else if (old_iscan) 318 } else if (old_iscan)
313 mgmt_discoverable(hdev, 0); 319 mgmt_discoverable(hdev, 0);
314 320
@@ -412,6 +418,21 @@ static void hci_cc_write_voice_setting(struct hci_dev *hdev,
412 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING); 418 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
413} 419}
414 420
421static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
422 struct sk_buff *skb)
423{
424 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
425
426 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
427
428 if (rp->status)
429 return;
430
431 hdev->num_iac = rp->num_iac;
432
433 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
434}
435
415static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb) 436static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
416{ 437{
417 __u8 status = *((__u8 *) skb->data); 438 __u8 status = *((__u8 *) skb->data);
@@ -449,14 +470,13 @@ static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
449 if (rp->status) 470 if (rp->status)
450 return; 471 return;
451 472
452 hdev->hci_ver = rp->hci_ver; 473 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
453 hdev->hci_rev = __le16_to_cpu(rp->hci_rev); 474 hdev->hci_ver = rp->hci_ver;
454 hdev->lmp_ver = rp->lmp_ver; 475 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
455 hdev->manufacturer = __le16_to_cpu(rp->manufacturer); 476 hdev->lmp_ver = rp->lmp_ver;
456 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver); 477 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
457 478 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
458 BT_DBG("%s manufacturer 0x%4.4x hci ver %d:%d", hdev->name, 479 }
459 hdev->manufacturer, hdev->hci_ver, hdev->hci_rev);
460} 480}
461 481
462static void hci_cc_read_local_commands(struct hci_dev *hdev, 482static void hci_cc_read_local_commands(struct hci_dev *hdev,
@@ -536,7 +556,8 @@ static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
536 if (rp->status) 556 if (rp->status)
537 return; 557 return;
538 558
539 hdev->max_page = rp->max_page; 559 if (hdev->max_page < rp->max_page)
560 hdev->max_page = rp->max_page;
540 561
541 if (rp->page < HCI_MAX_PAGES) 562 if (rp->page < HCI_MAX_PAGES)
542 memcpy(hdev->features[rp->page], rp->features, 8); 563 memcpy(hdev->features[rp->page], rp->features, 8);
@@ -913,17 +934,9 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
913 934
914 if (!status) { 935 if (!status) {
915 if (*sent) 936 if (*sent)
916 set_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags); 937 set_bit(HCI_ADVERTISING, &hdev->dev_flags);
917 else 938 else
918 clear_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags); 939 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
919 }
920
921 if (!test_bit(HCI_INIT, &hdev->flags)) {
922 struct hci_request req;
923
924 hci_req_init(&req, hdev);
925 hci_update_ad(&req);
926 hci_req_run(&req, NULL);
927 } 940 }
928 941
929 hci_dev_unlock(hdev); 942 hci_dev_unlock(hdev);
@@ -994,20 +1007,20 @@ static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
994 return; 1007 return;
995 1008
996 if (!status) { 1009 if (!status) {
997 if (sent->le) 1010 if (sent->le) {
998 hdev->features[1][0] |= LMP_HOST_LE; 1011 hdev->features[1][0] |= LMP_HOST_LE;
999 else 1012 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1013 } else {
1000 hdev->features[1][0] &= ~LMP_HOST_LE; 1014 hdev->features[1][0] &= ~LMP_HOST_LE;
1015 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1016 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
1017 }
1001 1018
1002 if (sent->simul) 1019 if (sent->simul)
1003 hdev->features[1][0] |= LMP_HOST_LE_BREDR; 1020 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1004 else 1021 else
1005 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR; 1022 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
1006 } 1023 }
1007
1008 if (test_bit(HCI_MGMT, &hdev->dev_flags) &&
1009 !test_bit(HCI_INIT, &hdev->flags))
1010 mgmt_le_enable_complete(hdev, sent->le, status);
1011} 1024}
1012 1025
1013static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev, 1026static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
@@ -1291,9 +1304,11 @@ static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1291 goto unlock; 1304 goto unlock;
1292 1305
1293 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) { 1306 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
1294 struct hci_cp_auth_requested cp; 1307 struct hci_cp_auth_requested auth_cp;
1295 cp.handle = __cpu_to_le16(conn->handle); 1308
1296 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp); 1309 auth_cp.handle = __cpu_to_le16(conn->handle);
1310 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1311 sizeof(auth_cp), &auth_cp);
1297 } 1312 }
1298 1313
1299unlock: 1314unlock:
@@ -1465,33 +1480,6 @@ static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1465 hci_dev_unlock(hdev); 1480 hci_dev_unlock(hdev);
1466} 1481}
1467 1482
1468static void hci_cs_le_create_conn(struct hci_dev *hdev, __u8 status)
1469{
1470 struct hci_conn *conn;
1471
1472 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1473
1474 if (status) {
1475 hci_dev_lock(hdev);
1476
1477 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1478 if (!conn) {
1479 hci_dev_unlock(hdev);
1480 return;
1481 }
1482
1483 BT_DBG("%s bdaddr %pMR conn %p", hdev->name, &conn->dst, conn);
1484
1485 conn->state = BT_CLOSED;
1486 mgmt_connect_failed(hdev, &conn->dst, conn->type,
1487 conn->dst_type, status);
1488 hci_proto_connect_cfm(conn, status);
1489 hci_conn_del(conn);
1490
1491 hci_dev_unlock(hdev);
1492 }
1493}
1494
1495static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status) 1483static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1496{ 1484{
1497 struct hci_cp_create_phy_link *cp; 1485 struct hci_cp_create_phy_link *cp;
@@ -1706,7 +1694,7 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
1706 &flags); 1694 &flags);
1707 1695
1708 if ((mask & HCI_LM_ACCEPT) && 1696 if ((mask & HCI_LM_ACCEPT) &&
1709 !hci_blacklist_lookup(hdev, &ev->bdaddr)) { 1697 !hci_blacklist_lookup(hdev, &ev->bdaddr, BDADDR_BREDR)) {
1710 /* Connection accepted */ 1698 /* Connection accepted */
1711 struct inquiry_entry *ie; 1699 struct inquiry_entry *ie;
1712 struct hci_conn *conn; 1700 struct hci_conn *conn;
@@ -1821,10 +1809,25 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
1821 } 1809 }
1822 1810
1823 if (ev->status == 0) { 1811 if (ev->status == 0) {
1824 if (conn->type == ACL_LINK && conn->flush_key) 1812 u8 type = conn->type;
1813
1814 if (type == ACL_LINK && conn->flush_key)
1825 hci_remove_link_key(hdev, &conn->dst); 1815 hci_remove_link_key(hdev, &conn->dst);
1826 hci_proto_disconn_cfm(conn, ev->reason); 1816 hci_proto_disconn_cfm(conn, ev->reason);
1827 hci_conn_del(conn); 1817 hci_conn_del(conn);
1818
1819 /* Re-enable advertising if necessary, since it might
1820 * have been disabled by the connection. From the
1821 * HCI_LE_Set_Advertise_Enable command description in
1822 * the core specification (v4.0):
1823 * "The Controller shall continue advertising until the Host
1824 * issues an LE_Set_Advertise_Enable command with
1825 * Advertising_Enable set to 0x00 (Advertising is disabled)
1826 * or until a connection is created or until the Advertising
1827 * is timed out due to Directed Advertising."
1828 */
1829 if (type == LE_LINK)
1830 mgmt_reenable_advertising(hdev);
1828 } 1831 }
1829 1832
1830unlock: 1833unlock:
@@ -2139,6 +2142,10 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2139 hci_cc_write_voice_setting(hdev, skb); 2142 hci_cc_write_voice_setting(hdev, skb);
2140 break; 2143 break;
2141 2144
2145 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2146 hci_cc_read_num_supported_iac(hdev, skb);
2147 break;
2148
2142 case HCI_OP_WRITE_SSP_MODE: 2149 case HCI_OP_WRITE_SSP_MODE:
2143 hci_cc_write_ssp_mode(hdev, skb); 2150 hci_cc_write_ssp_mode(hdev, skb);
2144 break; 2151 break;
@@ -2342,10 +2349,6 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
2342 hci_cs_disconnect(hdev, ev->status); 2349 hci_cs_disconnect(hdev, ev->status);
2343 break; 2350 break;
2344 2351
2345 case HCI_OP_LE_CREATE_CONN:
2346 hci_cs_le_create_conn(hdev, ev->status);
2347 break;
2348
2349 case HCI_OP_CREATE_PHY_LINK: 2352 case HCI_OP_CREATE_PHY_LINK:
2350 hci_cs_create_phylink(hdev, ev->status); 2353 hci_cs_create_phylink(hdev, ev->status);
2351 break; 2354 break;
@@ -2548,7 +2551,6 @@ static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2548 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle)); 2551 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2549 if (conn) { 2552 if (conn) {
2550 conn->mode = ev->mode; 2553 conn->mode = ev->mode;
2551 conn->interval = __le16_to_cpu(ev->interval);
2552 2554
2553 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND, 2555 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
2554 &conn->flags)) { 2556 &conn->flags)) {
@@ -2930,6 +2932,23 @@ unlock:
2930 hci_dev_unlock(hdev); 2932 hci_dev_unlock(hdev);
2931} 2933}
2932 2934
2935static inline size_t eir_get_length(u8 *eir, size_t eir_len)
2936{
2937 size_t parsed = 0;
2938
2939 while (parsed < eir_len) {
2940 u8 field_len = eir[0];
2941
2942 if (field_len == 0)
2943 return parsed;
2944
2945 parsed += field_len + 1;
2946 eir += field_len + 1;
2947 }
2948
2949 return eir_len;
2950}
2951
2933static void hci_extended_inquiry_result_evt(struct hci_dev *hdev, 2952static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
2934 struct sk_buff *skb) 2953 struct sk_buff *skb)
2935{ 2954{
@@ -3170,7 +3189,8 @@ static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3170 3189
3171 if (hdev->auto_accept_delay > 0) { 3190 if (hdev->auto_accept_delay > 0) {
3172 int delay = msecs_to_jiffies(hdev->auto_accept_delay); 3191 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
3173 mod_timer(&conn->auto_accept_timer, jiffies + delay); 3192 queue_delayed_work(conn->hdev->workqueue,
3193 &conn->auto_accept_work, delay);
3174 goto unlock; 3194 goto unlock;
3175 } 3195 }
3176 3196
@@ -3485,6 +3505,17 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3485 3505
3486 conn->dst_type = ev->bdaddr_type; 3506 conn->dst_type = ev->bdaddr_type;
3487 3507
3508 /* The advertising parameters for own address type
3509 * define which source address and source address
3510 * type this connections has.
3511 */
3512 if (bacmp(&conn->src, BDADDR_ANY)) {
3513 conn->src_type = ADDR_LE_DEV_PUBLIC;
3514 } else {
3515 bacpy(&conn->src, &hdev->static_addr);
3516 conn->src_type = ADDR_LE_DEV_RANDOM;
3517 }
3518
3488 if (ev->role == LE_CONN_ROLE_MASTER) { 3519 if (ev->role == LE_CONN_ROLE_MASTER) {
3489 conn->out = true; 3520 conn->out = true;
3490 conn->link_mode |= HCI_LM_MASTER; 3521 conn->link_mode |= HCI_LM_MASTER;
@@ -3640,8 +3671,8 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
3640 skb_pull(skb, HCI_EVENT_HDR_SIZE); 3671 skb_pull(skb, HCI_EVENT_HDR_SIZE);
3641 3672
3642 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) { 3673 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
3643 struct hci_command_hdr *hdr = (void *) hdev->sent_cmd->data; 3674 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
3644 u16 opcode = __le16_to_cpu(hdr->opcode); 3675 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
3645 3676
3646 hci_req_cmd_complete(hdev, opcode, 0); 3677 hci_req_cmd_complete(hdev, opcode, 0);
3647 } 3678 }
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 9bd7d959e384..7552f9e3089c 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -66,6 +66,46 @@ static struct bt_sock_list hci_sk_list = {
66 .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock) 66 .lock = __RW_LOCK_UNLOCKED(hci_sk_list.lock)
67}; 67};
68 68
69static bool is_filtered_packet(struct sock *sk, struct sk_buff *skb)
70{
71 struct hci_filter *flt;
72 int flt_type, flt_event;
73
74 /* Apply filter */
75 flt = &hci_pi(sk)->filter;
76
77 if (bt_cb(skb)->pkt_type == HCI_VENDOR_PKT)
78 flt_type = 0;
79 else
80 flt_type = bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS;
81
82 if (!test_bit(flt_type, &flt->type_mask))
83 return true;
84
85 /* Extra filter for event packets only */
86 if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT)
87 return false;
88
89 flt_event = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
90
91 if (!hci_test_bit(flt_event, &flt->event_mask))
92 return true;
93
94 /* Check filter only when opcode is set */
95 if (!flt->opcode)
96 return false;
97
98 if (flt_event == HCI_EV_CMD_COMPLETE &&
99 flt->opcode != get_unaligned((__le16 *)(skb->data + 3)))
100 return true;
101
102 if (flt_event == HCI_EV_CMD_STATUS &&
103 flt->opcode != get_unaligned((__le16 *)(skb->data + 4)))
104 return true;
105
106 return false;
107}
108
69/* Send frame to RAW socket */ 109/* Send frame to RAW socket */
70void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb) 110void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
71{ 111{
@@ -77,7 +117,6 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
77 read_lock(&hci_sk_list.lock); 117 read_lock(&hci_sk_list.lock);
78 118
79 sk_for_each(sk, &hci_sk_list.head) { 119 sk_for_each(sk, &hci_sk_list.head) {
80 struct hci_filter *flt;
81 struct sk_buff *nskb; 120 struct sk_buff *nskb;
82 121
83 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev) 122 if (sk->sk_state != BT_BOUND || hci_pi(sk)->hdev != hdev)
@@ -87,31 +126,19 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
87 if (skb->sk == sk) 126 if (skb->sk == sk)
88 continue; 127 continue;
89 128
90 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) 129 if (hci_pi(sk)->channel == HCI_CHANNEL_RAW) {
91 continue; 130 if (is_filtered_packet(sk, skb))
92
93 /* Apply filter */
94 flt = &hci_pi(sk)->filter;
95
96 if (!test_bit((bt_cb(skb)->pkt_type == HCI_VENDOR_PKT) ?
97 0 : (bt_cb(skb)->pkt_type & HCI_FLT_TYPE_BITS),
98 &flt->type_mask))
99 continue;
100
101 if (bt_cb(skb)->pkt_type == HCI_EVENT_PKT) {
102 int evt = (*(__u8 *)skb->data & HCI_FLT_EVENT_BITS);
103
104 if (!hci_test_bit(evt, &flt->event_mask))
105 continue; 131 continue;
106 132 } else if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
107 if (flt->opcode && 133 if (!bt_cb(skb)->incoming)
108 ((evt == HCI_EV_CMD_COMPLETE && 134 continue;
109 flt->opcode != 135 if (bt_cb(skb)->pkt_type != HCI_EVENT_PKT &&
110 get_unaligned((__le16 *)(skb->data + 3))) || 136 bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT &&
111 (evt == HCI_EV_CMD_STATUS && 137 bt_cb(skb)->pkt_type != HCI_SCODATA_PKT)
112 flt->opcode !=
113 get_unaligned((__le16 *)(skb->data + 4)))))
114 continue; 138 continue;
139 } else {
140 /* Don't send frame to other channel types */
141 continue;
115 } 142 }
116 143
117 if (!skb_copy) { 144 if (!skb_copy) {
@@ -360,7 +387,6 @@ static void hci_si_event(struct hci_dev *hdev, int type, int dlen, void *data)
360 __net_timestamp(skb); 387 __net_timestamp(skb);
361 388
362 bt_cb(skb)->pkt_type = HCI_EVENT_PKT; 389 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
363 skb->dev = (void *) hdev;
364 hci_send_to_sock(hdev, skb); 390 hci_send_to_sock(hdev, skb);
365 kfree_skb(skb); 391 kfree_skb(skb);
366} 392}
@@ -426,6 +452,12 @@ static int hci_sock_release(struct socket *sock)
426 bt_sock_unlink(&hci_sk_list, sk); 452 bt_sock_unlink(&hci_sk_list, sk);
427 453
428 if (hdev) { 454 if (hdev) {
455 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
456 mgmt_index_added(hdev);
457 clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags);
458 hci_dev_close(hdev->id);
459 }
460
429 atomic_dec(&hdev->promisc); 461 atomic_dec(&hdev->promisc);
430 hci_dev_put(hdev); 462 hci_dev_put(hdev);
431 } 463 }
@@ -449,7 +481,7 @@ static int hci_sock_blacklist_add(struct hci_dev *hdev, void __user *arg)
449 481
450 hci_dev_lock(hdev); 482 hci_dev_lock(hdev);
451 483
452 err = hci_blacklist_add(hdev, &bdaddr, 0); 484 err = hci_blacklist_add(hdev, &bdaddr, BDADDR_BREDR);
453 485
454 hci_dev_unlock(hdev); 486 hci_dev_unlock(hdev);
455 487
@@ -466,7 +498,7 @@ static int hci_sock_blacklist_del(struct hci_dev *hdev, void __user *arg)
466 498
467 hci_dev_lock(hdev); 499 hci_dev_lock(hdev);
468 500
469 err = hci_blacklist_del(hdev, &bdaddr, 0); 501 err = hci_blacklist_del(hdev, &bdaddr, BDADDR_BREDR);
470 502
471 hci_dev_unlock(hdev); 503 hci_dev_unlock(hdev);
472 504
@@ -482,6 +514,12 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
482 if (!hdev) 514 if (!hdev)
483 return -EBADFD; 515 return -EBADFD;
484 516
517 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags))
518 return -EBUSY;
519
520 if (hdev->dev_type != HCI_BREDR)
521 return -EOPNOTSUPP;
522
485 switch (cmd) { 523 switch (cmd) {
486 case HCISETRAW: 524 case HCISETRAW:
487 if (!capable(CAP_NET_ADMIN)) 525 if (!capable(CAP_NET_ADMIN))
@@ -512,23 +550,29 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
512 if (!capable(CAP_NET_ADMIN)) 550 if (!capable(CAP_NET_ADMIN))
513 return -EPERM; 551 return -EPERM;
514 return hci_sock_blacklist_del(hdev, (void __user *) arg); 552 return hci_sock_blacklist_del(hdev, (void __user *) arg);
515
516 default:
517 if (hdev->ioctl)
518 return hdev->ioctl(hdev, cmd, arg);
519 return -EINVAL;
520 } 553 }
554
555 return -ENOIOCTLCMD;
521} 556}
522 557
523static int hci_sock_ioctl(struct socket *sock, unsigned int cmd, 558static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
524 unsigned long arg) 559 unsigned long arg)
525{ 560{
526 struct sock *sk = sock->sk;
527 void __user *argp = (void __user *) arg; 561 void __user *argp = (void __user *) arg;
562 struct sock *sk = sock->sk;
528 int err; 563 int err;
529 564
530 BT_DBG("cmd %x arg %lx", cmd, arg); 565 BT_DBG("cmd %x arg %lx", cmd, arg);
531 566
567 lock_sock(sk);
568
569 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
570 err = -EBADFD;
571 goto done;
572 }
573
574 release_sock(sk);
575
532 switch (cmd) { 576 switch (cmd) {
533 case HCIGETDEVLIST: 577 case HCIGETDEVLIST:
534 return hci_get_dev_list(argp); 578 return hci_get_dev_list(argp);
@@ -573,13 +617,15 @@ static int hci_sock_ioctl(struct socket *sock, unsigned int cmd,
573 617
574 case HCIINQUIRY: 618 case HCIINQUIRY:
575 return hci_inquiry(argp); 619 return hci_inquiry(argp);
576
577 default:
578 lock_sock(sk);
579 err = hci_sock_bound_ioctl(sk, cmd, arg);
580 release_sock(sk);
581 return err;
582 } 620 }
621
622 lock_sock(sk);
623
624 err = hci_sock_bound_ioctl(sk, cmd, arg);
625
626done:
627 release_sock(sk);
628 return err;
583} 629}
584 630
585static int hci_sock_bind(struct socket *sock, struct sockaddr *addr, 631static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
@@ -629,6 +675,56 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
629 hci_pi(sk)->hdev = hdev; 675 hci_pi(sk)->hdev = hdev;
630 break; 676 break;
631 677
678 case HCI_CHANNEL_USER:
679 if (hci_pi(sk)->hdev) {
680 err = -EALREADY;
681 goto done;
682 }
683
684 if (haddr.hci_dev == HCI_DEV_NONE) {
685 err = -EINVAL;
686 goto done;
687 }
688
689 if (!capable(CAP_NET_ADMIN)) {
690 err = -EPERM;
691 goto done;
692 }
693
694 hdev = hci_dev_get(haddr.hci_dev);
695 if (!hdev) {
696 err = -ENODEV;
697 goto done;
698 }
699
700 if (test_bit(HCI_UP, &hdev->flags) ||
701 test_bit(HCI_INIT, &hdev->flags) ||
702 test_bit(HCI_SETUP, &hdev->dev_flags)) {
703 err = -EBUSY;
704 hci_dev_put(hdev);
705 goto done;
706 }
707
708 if (test_and_set_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
709 err = -EUSERS;
710 hci_dev_put(hdev);
711 goto done;
712 }
713
714 mgmt_index_removed(hdev);
715
716 err = hci_dev_open(hdev->id);
717 if (err) {
718 clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags);
719 hci_dev_put(hdev);
720 goto done;
721 }
722
723 atomic_inc(&hdev->promisc);
724
725 hci_pi(sk)->hdev = hdev;
726 break;
727
632 case HCI_CHANNEL_CONTROL: 728 case HCI_CHANNEL_CONTROL:
633 if (haddr.hci_dev != HCI_DEV_NONE) { 729 if (haddr.hci_dev != HCI_DEV_NONE) {
634 err = -EINVAL; 730 err = -EINVAL;
@@ -677,22 +773,30 @@ static int hci_sock_getname(struct socket *sock, struct sockaddr *addr,
677{ 773{
678 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr; 774 struct sockaddr_hci *haddr = (struct sockaddr_hci *) addr;
679 struct sock *sk = sock->sk; 775 struct sock *sk = sock->sk;
680 struct hci_dev *hdev = hci_pi(sk)->hdev; 776 struct hci_dev *hdev;
777 int err = 0;
681 778
682 BT_DBG("sock %p sk %p", sock, sk); 779 BT_DBG("sock %p sk %p", sock, sk);
683 780
684 if (!hdev) 781 if (peer)
685 return -EBADFD; 782 return -EOPNOTSUPP;
686 783
687 lock_sock(sk); 784 lock_sock(sk);
688 785
786 hdev = hci_pi(sk)->hdev;
787 if (!hdev) {
788 err = -EBADFD;
789 goto done;
790 }
791
689 *addr_len = sizeof(*haddr); 792 *addr_len = sizeof(*haddr);
690 haddr->hci_family = AF_BLUETOOTH; 793 haddr->hci_family = AF_BLUETOOTH;
691 haddr->hci_dev = hdev->id; 794 haddr->hci_dev = hdev->id;
692 haddr->hci_channel= 0; 795 haddr->hci_channel= hci_pi(sk)->channel;
693 796
797done:
694 release_sock(sk); 798 release_sock(sk);
695 return 0; 799 return err;
696} 800}
697 801
698static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg, 802static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
@@ -752,8 +856,6 @@ static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
752 if (!skb) 856 if (!skb)
753 return err; 857 return err;
754 858
755 msg->msg_namelen = 0;
756
757 copied = skb->len; 859 copied = skb->len;
758 if (len < copied) { 860 if (len < copied) {
759 msg->msg_flags |= MSG_TRUNC; 861 msg->msg_flags |= MSG_TRUNC;
@@ -767,6 +869,7 @@ static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
767 case HCI_CHANNEL_RAW: 869 case HCI_CHANNEL_RAW:
768 hci_sock_cmsg(sk, msg, skb); 870 hci_sock_cmsg(sk, msg, skb);
769 break; 871 break;
872 case HCI_CHANNEL_USER:
770 case HCI_CHANNEL_CONTROL: 873 case HCI_CHANNEL_CONTROL:
771 case HCI_CHANNEL_MONITOR: 874 case HCI_CHANNEL_MONITOR:
772 sock_recv_timestamp(msg, sk, skb); 875 sock_recv_timestamp(msg, sk, skb);
@@ -801,6 +904,7 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
801 904
802 switch (hci_pi(sk)->channel) { 905 switch (hci_pi(sk)->channel) {
803 case HCI_CHANNEL_RAW: 906 case HCI_CHANNEL_RAW:
907 case HCI_CHANNEL_USER:
804 break; 908 break;
805 case HCI_CHANNEL_CONTROL: 909 case HCI_CHANNEL_CONTROL:
806 err = mgmt_control(sk, msg, len); 910 err = mgmt_control(sk, msg, len);
@@ -835,9 +939,23 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
835 939
836 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); 940 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
837 skb_pull(skb, 1); 941 skb_pull(skb, 1);
838 skb->dev = (void *) hdev;
839 942
840 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { 943 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
944 /* No permission check is needed for user channel
945 * since that gets enforced when binding the socket.
946 *
947 * However check that the packet type is valid.
948 */
949 if (bt_cb(skb)->pkt_type != HCI_COMMAND_PKT &&
950 bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT &&
951 bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) {
952 err = -EINVAL;
953 goto drop;
954 }
955
956 skb_queue_tail(&hdev->raw_q, skb);
957 queue_work(hdev->workqueue, &hdev->tx_work);
958 } else if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
841 u16 opcode = get_unaligned_le16(skb->data); 959 u16 opcode = get_unaligned_le16(skb->data);
842 u16 ogf = hci_opcode_ogf(opcode); 960 u16 ogf = hci_opcode_ogf(opcode);
843 u16 ocf = hci_opcode_ocf(opcode); 961 u16 ocf = hci_opcode_ocf(opcode);
@@ -895,7 +1013,7 @@ static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
895 lock_sock(sk); 1013 lock_sock(sk);
896 1014
897 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1015 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
898 err = -EINVAL; 1016 err = -EBADFD;
899 goto done; 1017 goto done;
900 } 1018 }
901 1019
@@ -981,7 +1099,7 @@ static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
981 lock_sock(sk); 1099 lock_sock(sk);
982 1100
983 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1101 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
984 err = -EINVAL; 1102 err = -EBADFD;
985 goto done; 1103 goto done;
986 } 1104 }
987 1105
diff --git a/net/bluetooth/hci_sysfs.c b/net/bluetooth/hci_sysfs.c
index edf623a29043..0b61250cfdf9 100644
--- a/net/bluetooth/hci_sysfs.c
+++ b/net/bluetooth/hci_sysfs.c
@@ -1,17 +1,12 @@
1/* Bluetooth HCI driver model support. */ 1/* Bluetooth HCI driver model support. */
2 2
3#include <linux/debugfs.h>
4#include <linux/module.h> 3#include <linux/module.h>
5#include <asm/unaligned.h>
6 4
7#include <net/bluetooth/bluetooth.h> 5#include <net/bluetooth/bluetooth.h>
8#include <net/bluetooth/hci_core.h> 6#include <net/bluetooth/hci_core.h>
9 7
10static struct class *bt_class; 8static struct class *bt_class;
11 9
12struct dentry *bt_debugfs;
13EXPORT_SYMBOL_GPL(bt_debugfs);
14
15static inline char *link_typetostr(int type) 10static inline char *link_typetostr(int type)
16{ 11{
17 switch (type) { 12 switch (type) {
@@ -42,29 +37,15 @@ static ssize_t show_link_address(struct device *dev,
42 return sprintf(buf, "%pMR\n", &conn->dst); 37 return sprintf(buf, "%pMR\n", &conn->dst);
43} 38}
44 39
45static ssize_t show_link_features(struct device *dev,
46 struct device_attribute *attr, char *buf)
47{
48 struct hci_conn *conn = to_hci_conn(dev);
49
50 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
51 conn->features[0][0], conn->features[0][1],
52 conn->features[0][2], conn->features[0][3],
53 conn->features[0][4], conn->features[0][5],
54 conn->features[0][6], conn->features[0][7]);
55}
56
57#define LINK_ATTR(_name, _mode, _show, _store) \ 40#define LINK_ATTR(_name, _mode, _show, _store) \
58struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store) 41struct device_attribute link_attr_##_name = __ATTR(_name, _mode, _show, _store)
59 42
60static LINK_ATTR(type, S_IRUGO, show_link_type, NULL); 43static LINK_ATTR(type, S_IRUGO, show_link_type, NULL);
61static LINK_ATTR(address, S_IRUGO, show_link_address, NULL); 44static LINK_ATTR(address, S_IRUGO, show_link_address, NULL);
62static LINK_ATTR(features, S_IRUGO, show_link_features, NULL);
63 45
64static struct attribute *bt_link_attrs[] = { 46static struct attribute *bt_link_attrs[] = {
65 &link_attr_type.attr, 47 &link_attr_type.attr,
66 &link_attr_address.attr, 48 &link_attr_address.attr,
67 &link_attr_features.attr,
68 NULL 49 NULL
69}; 50};
70 51
@@ -150,28 +131,6 @@ void hci_conn_del_sysfs(struct hci_conn *conn)
150 hci_dev_put(hdev); 131 hci_dev_put(hdev);
151} 132}
152 133
153static inline char *host_bustostr(int bus)
154{
155 switch (bus) {
156 case HCI_VIRTUAL:
157 return "VIRTUAL";
158 case HCI_USB:
159 return "USB";
160 case HCI_PCCARD:
161 return "PCCARD";
162 case HCI_UART:
163 return "UART";
164 case HCI_RS232:
165 return "RS232";
166 case HCI_PCI:
167 return "PCI";
168 case HCI_SDIO:
169 return "SDIO";
170 default:
171 return "UNKNOWN";
172 }
173}
174
175static inline char *host_typetostr(int type) 134static inline char *host_typetostr(int type)
176{ 135{
177 switch (type) { 136 switch (type) {
@@ -184,13 +143,6 @@ static inline char *host_typetostr(int type)
184 } 143 }
185} 144}
186 145
187static ssize_t show_bus(struct device *dev,
188 struct device_attribute *attr, char *buf)
189{
190 struct hci_dev *hdev = to_hci_dev(dev);
191 return sprintf(buf, "%s\n", host_bustostr(hdev->bus));
192}
193
194static ssize_t show_type(struct device *dev, 146static ssize_t show_type(struct device *dev,
195 struct device_attribute *attr, char *buf) 147 struct device_attribute *attr, char *buf)
196{ 148{
@@ -212,14 +164,6 @@ static ssize_t show_name(struct device *dev,
212 return sprintf(buf, "%s\n", name); 164 return sprintf(buf, "%s\n", name);
213} 165}
214 166
215static ssize_t show_class(struct device *dev,
216 struct device_attribute *attr, char *buf)
217{
218 struct hci_dev *hdev = to_hci_dev(dev);
219 return sprintf(buf, "0x%.2x%.2x%.2x\n", hdev->dev_class[2],
220 hdev->dev_class[1], hdev->dev_class[0]);
221}
222
223static ssize_t show_address(struct device *dev, 167static ssize_t show_address(struct device *dev,
224 struct device_attribute *attr, char *buf) 168 struct device_attribute *attr, char *buf)
225{ 169{
@@ -227,150 +171,14 @@ static ssize_t show_address(struct device *dev,
227 return sprintf(buf, "%pMR\n", &hdev->bdaddr); 171 return sprintf(buf, "%pMR\n", &hdev->bdaddr);
228} 172}
229 173
230static ssize_t show_features(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
233 struct hci_dev *hdev = to_hci_dev(dev);
234
235 return sprintf(buf, "0x%02x%02x%02x%02x%02x%02x%02x%02x\n",
236 hdev->features[0][0], hdev->features[0][1],
237 hdev->features[0][2], hdev->features[0][3],
238 hdev->features[0][4], hdev->features[0][5],
239 hdev->features[0][6], hdev->features[0][7]);
240}
241
242static ssize_t show_manufacturer(struct device *dev,
243 struct device_attribute *attr, char *buf)
244{
245 struct hci_dev *hdev = to_hci_dev(dev);
246 return sprintf(buf, "%d\n", hdev->manufacturer);
247}
248
249static ssize_t show_hci_version(struct device *dev,
250 struct device_attribute *attr, char *buf)
251{
252 struct hci_dev *hdev = to_hci_dev(dev);
253 return sprintf(buf, "%d\n", hdev->hci_ver);
254}
255
256static ssize_t show_hci_revision(struct device *dev,
257 struct device_attribute *attr, char *buf)
258{
259 struct hci_dev *hdev = to_hci_dev(dev);
260 return sprintf(buf, "%d\n", hdev->hci_rev);
261}
262
263static ssize_t show_idle_timeout(struct device *dev,
264 struct device_attribute *attr, char *buf)
265{
266 struct hci_dev *hdev = to_hci_dev(dev);
267 return sprintf(buf, "%d\n", hdev->idle_timeout);
268}
269
270static ssize_t store_idle_timeout(struct device *dev,
271 struct device_attribute *attr,
272 const char *buf, size_t count)
273{
274 struct hci_dev *hdev = to_hci_dev(dev);
275 unsigned int val;
276 int rv;
277
278 rv = kstrtouint(buf, 0, &val);
279 if (rv < 0)
280 return rv;
281
282 if (val != 0 && (val < 500 || val > 3600000))
283 return -EINVAL;
284
285 hdev->idle_timeout = val;
286
287 return count;
288}
289
290static ssize_t show_sniff_max_interval(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct hci_dev *hdev = to_hci_dev(dev);
294 return sprintf(buf, "%d\n", hdev->sniff_max_interval);
295}
296
297static ssize_t store_sniff_max_interval(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
300{
301 struct hci_dev *hdev = to_hci_dev(dev);
302 u16 val;
303 int rv;
304
305 rv = kstrtou16(buf, 0, &val);
306 if (rv < 0)
307 return rv;
308
309 if (val == 0 || val % 2 || val < hdev->sniff_min_interval)
310 return -EINVAL;
311
312 hdev->sniff_max_interval = val;
313
314 return count;
315}
316
317static ssize_t show_sniff_min_interval(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
320 struct hci_dev *hdev = to_hci_dev(dev);
321 return sprintf(buf, "%d\n", hdev->sniff_min_interval);
322}
323
324static ssize_t store_sniff_min_interval(struct device *dev,
325 struct device_attribute *attr,
326 const char *buf, size_t count)
327{
328 struct hci_dev *hdev = to_hci_dev(dev);
329 u16 val;
330 int rv;
331
332 rv = kstrtou16(buf, 0, &val);
333 if (rv < 0)
334 return rv;
335
336 if (val == 0 || val % 2 || val > hdev->sniff_max_interval)
337 return -EINVAL;
338
339 hdev->sniff_min_interval = val;
340
341 return count;
342}
343
344static DEVICE_ATTR(bus, S_IRUGO, show_bus, NULL);
345static DEVICE_ATTR(type, S_IRUGO, show_type, NULL); 174static DEVICE_ATTR(type, S_IRUGO, show_type, NULL);
346static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 175static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
347static DEVICE_ATTR(class, S_IRUGO, show_class, NULL);
348static DEVICE_ATTR(address, S_IRUGO, show_address, NULL); 176static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);
349static DEVICE_ATTR(features, S_IRUGO, show_features, NULL);
350static DEVICE_ATTR(manufacturer, S_IRUGO, show_manufacturer, NULL);
351static DEVICE_ATTR(hci_version, S_IRUGO, show_hci_version, NULL);
352static DEVICE_ATTR(hci_revision, S_IRUGO, show_hci_revision, NULL);
353
354static DEVICE_ATTR(idle_timeout, S_IRUGO | S_IWUSR,
355 show_idle_timeout, store_idle_timeout);
356static DEVICE_ATTR(sniff_max_interval, S_IRUGO | S_IWUSR,
357 show_sniff_max_interval, store_sniff_max_interval);
358static DEVICE_ATTR(sniff_min_interval, S_IRUGO | S_IWUSR,
359 show_sniff_min_interval, store_sniff_min_interval);
360 177
361static struct attribute *bt_host_attrs[] = { 178static struct attribute *bt_host_attrs[] = {
362 &dev_attr_bus.attr,
363 &dev_attr_type.attr, 179 &dev_attr_type.attr,
364 &dev_attr_name.attr, 180 &dev_attr_name.attr,
365 &dev_attr_class.attr,
366 &dev_attr_address.attr, 181 &dev_attr_address.attr,
367 &dev_attr_features.attr,
368 &dev_attr_manufacturer.attr,
369 &dev_attr_hci_version.attr,
370 &dev_attr_hci_revision.attr,
371 &dev_attr_idle_timeout.attr,
372 &dev_attr_sniff_max_interval.attr,
373 &dev_attr_sniff_min_interval.attr,
374 NULL 182 NULL
375}; 183};
376 184
@@ -396,141 +204,6 @@ static struct device_type bt_host = {
396 .release = bt_host_release, 204 .release = bt_host_release,
397}; 205};
398 206
399static int inquiry_cache_show(struct seq_file *f, void *p)
400{
401 struct hci_dev *hdev = f->private;
402 struct discovery_state *cache = &hdev->discovery;
403 struct inquiry_entry *e;
404
405 hci_dev_lock(hdev);
406
407 list_for_each_entry(e, &cache->all, all) {
408 struct inquiry_data *data = &e->data;
409 seq_printf(f, "%pMR %d %d %d 0x%.2x%.2x%.2x 0x%.4x %d %d %u\n",
410 &data->bdaddr,
411 data->pscan_rep_mode, data->pscan_period_mode,
412 data->pscan_mode, data->dev_class[2],
413 data->dev_class[1], data->dev_class[0],
414 __le16_to_cpu(data->clock_offset),
415 data->rssi, data->ssp_mode, e->timestamp);
416 }
417
418 hci_dev_unlock(hdev);
419
420 return 0;
421}
422
423static int inquiry_cache_open(struct inode *inode, struct file *file)
424{
425 return single_open(file, inquiry_cache_show, inode->i_private);
426}
427
428static const struct file_operations inquiry_cache_fops = {
429 .open = inquiry_cache_open,
430 .read = seq_read,
431 .llseek = seq_lseek,
432 .release = single_release,
433};
434
435static int blacklist_show(struct seq_file *f, void *p)
436{
437 struct hci_dev *hdev = f->private;
438 struct bdaddr_list *b;
439
440 hci_dev_lock(hdev);
441
442 list_for_each_entry(b, &hdev->blacklist, list)
443 seq_printf(f, "%pMR\n", &b->bdaddr);
444
445 hci_dev_unlock(hdev);
446
447 return 0;
448}
449
450static int blacklist_open(struct inode *inode, struct file *file)
451{
452 return single_open(file, blacklist_show, inode->i_private);
453}
454
455static const struct file_operations blacklist_fops = {
456 .open = blacklist_open,
457 .read = seq_read,
458 .llseek = seq_lseek,
459 .release = single_release,
460};
461
462static void print_bt_uuid(struct seq_file *f, u8 *uuid)
463{
464 u32 data0, data5;
465 u16 data1, data2, data3, data4;
466
467 data5 = get_unaligned_le32(uuid);
468 data4 = get_unaligned_le16(uuid + 4);
469 data3 = get_unaligned_le16(uuid + 6);
470 data2 = get_unaligned_le16(uuid + 8);
471 data1 = get_unaligned_le16(uuid + 10);
472 data0 = get_unaligned_le32(uuid + 12);
473
474 seq_printf(f, "%.8x-%.4x-%.4x-%.4x-%.4x%.8x\n",
475 data0, data1, data2, data3, data4, data5);
476}
477
478static int uuids_show(struct seq_file *f, void *p)
479{
480 struct hci_dev *hdev = f->private;
481 struct bt_uuid *uuid;
482
483 hci_dev_lock(hdev);
484
485 list_for_each_entry(uuid, &hdev->uuids, list)
486 print_bt_uuid(f, uuid->uuid);
487
488 hci_dev_unlock(hdev);
489
490 return 0;
491}
492
493static int uuids_open(struct inode *inode, struct file *file)
494{
495 return single_open(file, uuids_show, inode->i_private);
496}
497
498static const struct file_operations uuids_fops = {
499 .open = uuids_open,
500 .read = seq_read,
501 .llseek = seq_lseek,
502 .release = single_release,
503};
504
505static int auto_accept_delay_set(void *data, u64 val)
506{
507 struct hci_dev *hdev = data;
508
509 hci_dev_lock(hdev);
510
511 hdev->auto_accept_delay = val;
512
513 hci_dev_unlock(hdev);
514
515 return 0;
516}
517
518static int auto_accept_delay_get(void *data, u64 *val)
519{
520 struct hci_dev *hdev = data;
521
522 hci_dev_lock(hdev);
523
524 *val = hdev->auto_accept_delay;
525
526 hci_dev_unlock(hdev);
527
528 return 0;
529}
530
531DEFINE_SIMPLE_ATTRIBUTE(auto_accept_delay_fops, auto_accept_delay_get,
532 auto_accept_delay_set, "%llu\n");
533
534void hci_init_sysfs(struct hci_dev *hdev) 207void hci_init_sysfs(struct hci_dev *hdev)
535{ 208{
536 struct device *dev = &hdev->dev; 209 struct device *dev = &hdev->dev;
@@ -542,52 +215,8 @@ void hci_init_sysfs(struct hci_dev *hdev)
542 device_initialize(dev); 215 device_initialize(dev);
543} 216}
544 217
545int hci_add_sysfs(struct hci_dev *hdev)
546{
547 struct device *dev = &hdev->dev;
548 int err;
549
550 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
551
552 dev_set_name(dev, "%s", hdev->name);
553
554 err = device_add(dev);
555 if (err < 0)
556 return err;
557
558 if (!bt_debugfs)
559 return 0;
560
561 hdev->debugfs = debugfs_create_dir(hdev->name, bt_debugfs);
562 if (!hdev->debugfs)
563 return 0;
564
565 debugfs_create_file("inquiry_cache", 0444, hdev->debugfs,
566 hdev, &inquiry_cache_fops);
567
568 debugfs_create_file("blacklist", 0444, hdev->debugfs,
569 hdev, &blacklist_fops);
570
571 debugfs_create_file("uuids", 0444, hdev->debugfs, hdev, &uuids_fops);
572
573 debugfs_create_file("auto_accept_delay", 0444, hdev->debugfs, hdev,
574 &auto_accept_delay_fops);
575 return 0;
576}
577
578void hci_del_sysfs(struct hci_dev *hdev)
579{
580 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
581
582 debugfs_remove_recursive(hdev->debugfs);
583
584 device_del(&hdev->dev);
585}
586
587int __init bt_sysfs_init(void) 218int __init bt_sysfs_init(void)
588{ 219{
589 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
590
591 bt_class = class_create(THIS_MODULE, "bluetooth"); 220 bt_class = class_create(THIS_MODULE, "bluetooth");
592 221
593 return PTR_ERR_OR_ZERO(bt_class); 222 return PTR_ERR_OR_ZERO(bt_class);
@@ -596,6 +225,4 @@ int __init bt_sysfs_init(void)
596void bt_sysfs_cleanup(void) 225void bt_sysfs_cleanup(void)
597{ 226{
598 class_destroy(bt_class); 227 class_destroy(bt_class);
599
600 debugfs_remove_recursive(bt_debugfs);
601} 228}
diff --git a/net/bluetooth/hidp/core.c b/net/bluetooth/hidp/core.c
index bdc35a7a7fee..292e619db896 100644
--- a/net/bluetooth/hidp/core.c
+++ b/net/bluetooth/hidp/core.c
@@ -767,10 +767,10 @@ static int hidp_setup_hid(struct hidp_session *session,
767 strncpy(hid->name, req->name, sizeof(req->name) - 1); 767 strncpy(hid->name, req->name, sizeof(req->name) - 1);
768 768
769 snprintf(hid->phys, sizeof(hid->phys), "%pMR", 769 snprintf(hid->phys, sizeof(hid->phys), "%pMR",
770 &bt_sk(session->ctrl_sock->sk)->src); 770 &l2cap_pi(session->ctrl_sock->sk)->chan->src);
771 771
772 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR", 772 snprintf(hid->uniq, sizeof(hid->uniq), "%pMR",
773 &bt_sk(session->ctrl_sock->sk)->dst); 773 &l2cap_pi(session->ctrl_sock->sk)->chan->dst);
774 774
775 hid->dev.parent = &session->conn->hcon->dev; 775 hid->dev.parent = &session->conn->hcon->dev;
776 hid->ll_driver = &hidp_hid_driver; 776 hid->ll_driver = &hidp_hid_driver;
@@ -1283,23 +1283,29 @@ static int hidp_session_thread(void *arg)
1283static int hidp_verify_sockets(struct socket *ctrl_sock, 1283static int hidp_verify_sockets(struct socket *ctrl_sock,
1284 struct socket *intr_sock) 1284 struct socket *intr_sock)
1285{ 1285{
1286 struct l2cap_chan *ctrl_chan, *intr_chan;
1286 struct bt_sock *ctrl, *intr; 1287 struct bt_sock *ctrl, *intr;
1287 struct hidp_session *session; 1288 struct hidp_session *session;
1288 1289
1289 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock)) 1290 if (!l2cap_is_socket(ctrl_sock) || !l2cap_is_socket(intr_sock))
1290 return -EINVAL; 1291 return -EINVAL;
1291 1292
1293 ctrl_chan = l2cap_pi(ctrl_sock->sk)->chan;
1294 intr_chan = l2cap_pi(intr_sock->sk)->chan;
1295
1296 if (bacmp(&ctrl_chan->src, &intr_chan->src) ||
1297 bacmp(&ctrl_chan->dst, &intr_chan->dst))
1298 return -ENOTUNIQ;
1299
1292 ctrl = bt_sk(ctrl_sock->sk); 1300 ctrl = bt_sk(ctrl_sock->sk);
1293 intr = bt_sk(intr_sock->sk); 1301 intr = bt_sk(intr_sock->sk);
1294 1302
1295 if (bacmp(&ctrl->src, &intr->src) || bacmp(&ctrl->dst, &intr->dst))
1296 return -ENOTUNIQ;
1297 if (ctrl->sk.sk_state != BT_CONNECTED || 1303 if (ctrl->sk.sk_state != BT_CONNECTED ||
1298 intr->sk.sk_state != BT_CONNECTED) 1304 intr->sk.sk_state != BT_CONNECTED)
1299 return -EBADFD; 1305 return -EBADFD;
1300 1306
1301 /* early session check, we check again during session registration */ 1307 /* early session check, we check again during session registration */
1302 session = hidp_session_find(&ctrl->dst); 1308 session = hidp_session_find(&ctrl_chan->dst);
1303 if (session) { 1309 if (session) {
1304 hidp_session_put(session); 1310 hidp_session_put(session);
1305 return -EEXIST; 1311 return -EEXIST;
@@ -1332,7 +1338,7 @@ int hidp_connection_add(struct hidp_connadd_req *req,
1332 if (!conn) 1338 if (!conn)
1333 return -EBADFD; 1339 return -EBADFD;
1334 1340
1335 ret = hidp_session_new(&session, &bt_sk(ctrl_sock->sk)->dst, ctrl_sock, 1341 ret = hidp_session_new(&session, &chan->dst, ctrl_sock,
1336 intr_sock, req, conn); 1342 intr_sock, req, conn);
1337 if (ret) 1343 if (ret)
1338 goto out_conn; 1344 goto out_conn;
diff --git a/net/bluetooth/hidp/hidp.h b/net/bluetooth/hidp/hidp.h
index 9e6cc3553105..ab5241400cf7 100644
--- a/net/bluetooth/hidp/hidp.h
+++ b/net/bluetooth/hidp/hidp.h
@@ -182,7 +182,7 @@ struct hidp_session {
182}; 182};
183 183
184/* HIDP init defines */ 184/* HIDP init defines */
185extern int __init hidp_init_sockets(void); 185int __init hidp_init_sockets(void);
186extern void __exit hidp_cleanup_sockets(void); 186void __exit hidp_cleanup_sockets(void);
187 187
188#endif /* __HIDP_H */ 188#endif /* __HIDP_H */
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 63fa11109a1c..4af3821df880 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -36,14 +36,15 @@
36#include <net/bluetooth/bluetooth.h> 36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h> 37#include <net/bluetooth/hci_core.h>
38#include <net/bluetooth/l2cap.h> 38#include <net/bluetooth/l2cap.h>
39#include <net/bluetooth/smp.h> 39
40#include <net/bluetooth/a2mp.h> 40#include "smp.h"
41#include <net/bluetooth/amp.h> 41#include "a2mp.h"
42#include "amp.h"
42 43
43bool disable_ertm; 44bool disable_ertm;
44 45
45static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN; 46static u32 l2cap_feat_mask = L2CAP_FEAT_FIXED_CHAN | L2CAP_FEAT_UCD;
46static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP, }; 47static u8 l2cap_fixed_chan[8] = { L2CAP_FC_L2CAP | L2CAP_FC_CONNLESS, };
47 48
48static LIST_HEAD(chan_list); 49static LIST_HEAD(chan_list);
49static DEFINE_RWLOCK(chan_list_lock); 50static DEFINE_RWLOCK(chan_list_lock);
@@ -58,6 +59,18 @@ static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err);
58static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control, 59static void l2cap_tx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
59 struct sk_buff_head *skbs, u8 event); 60 struct sk_buff_head *skbs, u8 event);
60 61
62static inline __u8 bdaddr_type(struct hci_conn *hcon, __u8 type)
63{
64 if (hcon->type == LE_LINK) {
65 if (type == ADDR_LE_DEV_PUBLIC)
66 return BDADDR_LE_PUBLIC;
67 else
68 return BDADDR_LE_RANDOM;
69 }
70
71 return BDADDR_BREDR;
72}
73
61/* ---- L2CAP channels ---- */ 74/* ---- L2CAP channels ---- */
62 75
63static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn, 76static struct l2cap_chan *__l2cap_get_chan_by_dcid(struct l2cap_conn *conn,
@@ -148,7 +161,7 @@ static struct l2cap_chan *__l2cap_global_chan_by_addr(__le16 psm, bdaddr_t *src)
148 struct l2cap_chan *c; 161 struct l2cap_chan *c;
149 162
150 list_for_each_entry(c, &chan_list, global_l) { 163 list_for_each_entry(c, &chan_list, global_l) {
151 if (c->sport == psm && !bacmp(&bt_sk(c->sk)->src, src)) 164 if (c->sport == psm && !bacmp(&c->src, src))
152 return c; 165 return c;
153 } 166 }
154 return NULL; 167 return NULL;
@@ -210,38 +223,25 @@ static u16 l2cap_alloc_cid(struct l2cap_conn *conn)
210 return 0; 223 return 0;
211} 224}
212 225
213static void __l2cap_state_change(struct l2cap_chan *chan, int state) 226static void l2cap_state_change(struct l2cap_chan *chan, int state)
214{ 227{
215 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state), 228 BT_DBG("chan %p %s -> %s", chan, state_to_string(chan->state),
216 state_to_string(state)); 229 state_to_string(state));
217 230
218 chan->state = state; 231 chan->state = state;
219 chan->ops->state_change(chan, state); 232 chan->ops->state_change(chan, state, 0);
220} 233}
221 234
222static void l2cap_state_change(struct l2cap_chan *chan, int state) 235static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
236 int state, int err)
223{ 237{
224 struct sock *sk = chan->sk; 238 chan->state = state;
225 239 chan->ops->state_change(chan, chan->state, err);
226 lock_sock(sk);
227 __l2cap_state_change(chan, state);
228 release_sock(sk);
229}
230
231static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err)
232{
233 struct sock *sk = chan->sk;
234
235 sk->sk_err = err;
236} 240}
237 241
238static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err) 242static inline void l2cap_chan_set_err(struct l2cap_chan *chan, int err)
239{ 243{
240 struct sock *sk = chan->sk; 244 chan->ops->state_change(chan, chan->state, err);
241
242 lock_sock(sk);
243 __l2cap_chan_set_err(chan, err);
244 release_sock(sk);
245} 245}
246 246
247static void __set_retrans_timer(struct l2cap_chan *chan) 247static void __set_retrans_timer(struct l2cap_chan *chan)
@@ -620,10 +620,8 @@ void l2cap_chan_del(struct l2cap_chan *chan, int err)
620void l2cap_chan_close(struct l2cap_chan *chan, int reason) 620void l2cap_chan_close(struct l2cap_chan *chan, int reason)
621{ 621{
622 struct l2cap_conn *conn = chan->conn; 622 struct l2cap_conn *conn = chan->conn;
623 struct sock *sk = chan->sk;
624 623
625 BT_DBG("chan %p state %s sk %p", chan, state_to_string(chan->state), 624 BT_DBG("chan %p state %s", chan, state_to_string(chan->state));
626 sk);
627 625
628 switch (chan->state) { 626 switch (chan->state) {
629 case BT_LISTEN: 627 case BT_LISTEN:
@@ -634,7 +632,7 @@ void l2cap_chan_close(struct l2cap_chan *chan, int reason)
634 case BT_CONFIG: 632 case BT_CONFIG:
635 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED && 633 if (chan->chan_type == L2CAP_CHAN_CONN_ORIENTED &&
636 conn->hcon->type == ACL_LINK) { 634 conn->hcon->type == ACL_LINK) {
637 __set_chan_timer(chan, sk->sk_sndtimeo); 635 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
638 l2cap_send_disconn_req(chan, reason); 636 l2cap_send_disconn_req(chan, reason);
639 } else 637 } else
640 l2cap_chan_del(chan, reason); 638 l2cap_chan_del(chan, reason);
@@ -646,10 +644,11 @@ void l2cap_chan_close(struct l2cap_chan *chan, int reason)
646 struct l2cap_conn_rsp rsp; 644 struct l2cap_conn_rsp rsp;
647 __u16 result; 645 __u16 result;
648 646
649 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) 647 if (test_bit(FLAG_DEFER_SETUP, &chan->flags))
650 result = L2CAP_CR_SEC_BLOCK; 648 result = L2CAP_CR_SEC_BLOCK;
651 else 649 else
652 result = L2CAP_CR_BAD_PSM; 650 result = L2CAP_CR_BAD_PSM;
651
653 l2cap_state_change(chan, BT_DISCONN); 652 l2cap_state_change(chan, BT_DISCONN);
654 653
655 rsp.scid = cpu_to_le16(chan->dcid); 654 rsp.scid = cpu_to_le16(chan->dcid);
@@ -676,7 +675,8 @@ void l2cap_chan_close(struct l2cap_chan *chan, int reason)
676 675
677static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan) 676static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
678{ 677{
679 if (chan->chan_type == L2CAP_CHAN_RAW) { 678 switch (chan->chan_type) {
679 case L2CAP_CHAN_RAW:
680 switch (chan->sec_level) { 680 switch (chan->sec_level) {
681 case BT_SECURITY_HIGH: 681 case BT_SECURITY_HIGH:
682 return HCI_AT_DEDICATED_BONDING_MITM; 682 return HCI_AT_DEDICATED_BONDING_MITM;
@@ -685,15 +685,29 @@ static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
685 default: 685 default:
686 return HCI_AT_NO_BONDING; 686 return HCI_AT_NO_BONDING;
687 } 687 }
688 } else if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) { 688 break;
689 if (chan->sec_level == BT_SECURITY_LOW) 689 case L2CAP_CHAN_CONN_LESS:
690 chan->sec_level = BT_SECURITY_SDP; 690 if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_3DSP)) {
691 691 if (chan->sec_level == BT_SECURITY_LOW)
692 chan->sec_level = BT_SECURITY_SDP;
693 }
692 if (chan->sec_level == BT_SECURITY_HIGH) 694 if (chan->sec_level == BT_SECURITY_HIGH)
693 return HCI_AT_NO_BONDING_MITM; 695 return HCI_AT_NO_BONDING_MITM;
694 else 696 else
695 return HCI_AT_NO_BONDING; 697 return HCI_AT_NO_BONDING;
696 } else { 698 break;
699 case L2CAP_CHAN_CONN_ORIENTED:
700 if (chan->psm == __constant_cpu_to_le16(L2CAP_PSM_SDP)) {
701 if (chan->sec_level == BT_SECURITY_LOW)
702 chan->sec_level = BT_SECURITY_SDP;
703
704 if (chan->sec_level == BT_SECURITY_HIGH)
705 return HCI_AT_NO_BONDING_MITM;
706 else
707 return HCI_AT_NO_BONDING;
708 }
709 /* fall through */
710 default:
697 switch (chan->sec_level) { 711 switch (chan->sec_level) {
698 case BT_SECURITY_HIGH: 712 case BT_SECURITY_HIGH:
699 return HCI_AT_GENERAL_BONDING_MITM; 713 return HCI_AT_GENERAL_BONDING_MITM;
@@ -702,6 +716,7 @@ static inline u8 l2cap_get_auth_type(struct l2cap_chan *chan)
702 default: 716 default:
703 return HCI_AT_NO_BONDING; 717 return HCI_AT_NO_BONDING;
704 } 718 }
719 break;
705 } 720 }
706} 721}
707 722
@@ -1015,14 +1030,29 @@ static inline int __l2cap_no_conn_pending(struct l2cap_chan *chan)
1015static bool __amp_capable(struct l2cap_chan *chan) 1030static bool __amp_capable(struct l2cap_chan *chan)
1016{ 1031{
1017 struct l2cap_conn *conn = chan->conn; 1032 struct l2cap_conn *conn = chan->conn;
1033 struct hci_dev *hdev;
1034 bool amp_available = false;
1018 1035
1019 if (enable_hs && 1036 if (!conn->hs_enabled)
1020 hci_amp_capable() && 1037 return false;
1021 chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED && 1038
1022 conn->fixed_chan_mask & L2CAP_FC_A2MP) 1039 if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP))
1023 return true;
1024 else
1025 return false; 1040 return false;
1041
1042 read_lock(&hci_dev_list_lock);
1043 list_for_each_entry(hdev, &hci_dev_list, list) {
1044 if (hdev->amp_type != AMP_TYPE_BREDR &&
1045 test_bit(HCI_UP, &hdev->flags)) {
1046 amp_available = true;
1047 break;
1048 }
1049 }
1050 read_unlock(&hci_dev_list_lock);
1051
1052 if (chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED)
1053 return amp_available;
1054
1055 return false;
1026} 1056}
1027 1057
1028static bool l2cap_check_efs(struct l2cap_chan *chan) 1058static bool l2cap_check_efs(struct l2cap_chan *chan)
@@ -1186,7 +1216,6 @@ static inline int l2cap_mode_supported(__u8 mode, __u32 feat_mask)
1186 1216
1187static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err) 1217static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
1188{ 1218{
1189 struct sock *sk = chan->sk;
1190 struct l2cap_conn *conn = chan->conn; 1219 struct l2cap_conn *conn = chan->conn;
1191 struct l2cap_disconn_req req; 1220 struct l2cap_disconn_req req;
1192 1221
@@ -1209,10 +1238,7 @@ static void l2cap_send_disconn_req(struct l2cap_chan *chan, int err)
1209 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ, 1238 l2cap_send_cmd(conn, l2cap_get_ident(conn), L2CAP_DISCONN_REQ,
1210 sizeof(req), &req); 1239 sizeof(req), &req);
1211 1240
1212 lock_sock(sk); 1241 l2cap_state_change_and_error(chan, BT_DISCONN, err);
1213 __l2cap_state_change(chan, BT_DISCONN);
1214 __l2cap_chan_set_err(chan, err);
1215 release_sock(sk);
1216} 1242}
1217 1243
1218/* ---- L2CAP connections ---- */ 1244/* ---- L2CAP connections ---- */
@@ -1225,8 +1251,6 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
1225 mutex_lock(&conn->chan_lock); 1251 mutex_lock(&conn->chan_lock);
1226 1252
1227 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) { 1253 list_for_each_entry_safe(chan, tmp, &conn->chan_l, list) {
1228 struct sock *sk = chan->sk;
1229
1230 l2cap_chan_lock(chan); 1254 l2cap_chan_lock(chan);
1231 1255
1232 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1256 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
@@ -1258,19 +1282,16 @@ static void l2cap_conn_start(struct l2cap_conn *conn)
1258 rsp.dcid = cpu_to_le16(chan->scid); 1282 rsp.dcid = cpu_to_le16(chan->scid);
1259 1283
1260 if (l2cap_chan_check_security(chan)) { 1284 if (l2cap_chan_check_security(chan)) {
1261 lock_sock(sk); 1285 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
1262 if (test_bit(BT_SK_DEFER_SETUP,
1263 &bt_sk(sk)->flags)) {
1264 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); 1286 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND);
1265 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND); 1287 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHOR_PEND);
1266 chan->ops->defer(chan); 1288 chan->ops->defer(chan);
1267 1289
1268 } else { 1290 } else {
1269 __l2cap_state_change(chan, BT_CONFIG); 1291 l2cap_state_change(chan, BT_CONFIG);
1270 rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS); 1292 rsp.result = __constant_cpu_to_le16(L2CAP_CR_SUCCESS);
1271 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO); 1293 rsp.status = __constant_cpu_to_le16(L2CAP_CS_NO_INFO);
1272 } 1294 }
1273 release_sock(sk);
1274 } else { 1295 } else {
1275 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND); 1296 rsp.result = __constant_cpu_to_le16(L2CAP_CR_PEND);
1276 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND); 1297 rsp.status = __constant_cpu_to_le16(L2CAP_CS_AUTHEN_PEND);
@@ -1309,8 +1330,6 @@ static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
1309 read_lock(&chan_list_lock); 1330 read_lock(&chan_list_lock);
1310 1331
1311 list_for_each_entry(c, &chan_list, global_l) { 1332 list_for_each_entry(c, &chan_list, global_l) {
1312 struct sock *sk = c->sk;
1313
1314 if (state && c->state != state) 1333 if (state && c->state != state)
1315 continue; 1334 continue;
1316 1335
@@ -1319,16 +1338,16 @@ static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
1319 int src_any, dst_any; 1338 int src_any, dst_any;
1320 1339
1321 /* Exact match. */ 1340 /* Exact match. */
1322 src_match = !bacmp(&bt_sk(sk)->src, src); 1341 src_match = !bacmp(&c->src, src);
1323 dst_match = !bacmp(&bt_sk(sk)->dst, dst); 1342 dst_match = !bacmp(&c->dst, dst);
1324 if (src_match && dst_match) { 1343 if (src_match && dst_match) {
1325 read_unlock(&chan_list_lock); 1344 read_unlock(&chan_list_lock);
1326 return c; 1345 return c;
1327 } 1346 }
1328 1347
1329 /* Closest match */ 1348 /* Closest match */
1330 src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); 1349 src_any = !bacmp(&c->src, BDADDR_ANY);
1331 dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); 1350 dst_any = !bacmp(&c->dst, BDADDR_ANY);
1332 if ((src_match && dst_any) || (src_any && dst_match) || 1351 if ((src_match && dst_any) || (src_any && dst_match) ||
1333 (src_any && dst_any)) 1352 (src_any && dst_any))
1334 c1 = c; 1353 c1 = c;
@@ -1342,14 +1361,15 @@ static struct l2cap_chan *l2cap_global_chan_by_scid(int state, u16 cid,
1342 1361
1343static void l2cap_le_conn_ready(struct l2cap_conn *conn) 1362static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1344{ 1363{
1345 struct sock *parent; 1364 struct hci_conn *hcon = conn->hcon;
1346 struct l2cap_chan *chan, *pchan; 1365 struct l2cap_chan *chan, *pchan;
1366 u8 dst_type;
1347 1367
1348 BT_DBG(""); 1368 BT_DBG("");
1349 1369
1350 /* Check if we have socket listening on cid */ 1370 /* Check if we have socket listening on cid */
1351 pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT, 1371 pchan = l2cap_global_chan_by_scid(BT_LISTEN, L2CAP_CID_ATT,
1352 conn->src, conn->dst); 1372 &hcon->src, &hcon->dst);
1353 if (!pchan) 1373 if (!pchan)
1354 return; 1374 return;
1355 1375
@@ -1357,9 +1377,13 @@ static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1357 if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT)) 1377 if (__l2cap_get_chan_by_dcid(conn, L2CAP_CID_ATT))
1358 return; 1378 return;
1359 1379
1360 parent = pchan->sk; 1380 dst_type = bdaddr_type(hcon, hcon->dst_type);
1361 1381
1362 lock_sock(parent); 1382 /* If device is blocked, do not create a channel for it */
1383 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, dst_type))
1384 return;
1385
1386 l2cap_chan_lock(pchan);
1363 1387
1364 chan = pchan->ops->new_connection(pchan); 1388 chan = pchan->ops->new_connection(pchan);
1365 if (!chan) 1389 if (!chan)
@@ -1367,13 +1391,15 @@ static void l2cap_le_conn_ready(struct l2cap_conn *conn)
1367 1391
1368 chan->dcid = L2CAP_CID_ATT; 1392 chan->dcid = L2CAP_CID_ATT;
1369 1393
1370 bacpy(&bt_sk(chan->sk)->src, conn->src); 1394 bacpy(&chan->src, &hcon->src);
1371 bacpy(&bt_sk(chan->sk)->dst, conn->dst); 1395 bacpy(&chan->dst, &hcon->dst);
1396 chan->src_type = bdaddr_type(hcon, hcon->src_type);
1397 chan->dst_type = dst_type;
1372 1398
1373 __l2cap_chan_add(conn, chan); 1399 __l2cap_chan_add(conn, chan);
1374 1400
1375clean: 1401clean:
1376 release_sock(parent); 1402 l2cap_chan_unlock(pchan);
1377} 1403}
1378 1404
1379static void l2cap_conn_ready(struct l2cap_conn *conn) 1405static void l2cap_conn_ready(struct l2cap_conn *conn)
@@ -1408,12 +1434,7 @@ static void l2cap_conn_ready(struct l2cap_conn *conn)
1408 l2cap_chan_ready(chan); 1434 l2cap_chan_ready(chan);
1409 1435
1410 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1436 } else if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
1411 struct sock *sk = chan->sk; 1437 l2cap_chan_ready(chan);
1412 __clear_chan_timer(chan);
1413 lock_sock(sk);
1414 __l2cap_state_change(chan, BT_CONNECTED);
1415 sk->sk_state_change(sk);
1416 release_sock(sk);
1417 1438
1418 } else if (chan->state == BT_CONNECT) { 1439 } else if (chan->state == BT_CONNECT) {
1419 l2cap_do_start(chan); 1440 l2cap_do_start(chan);
@@ -1633,11 +1654,12 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
1633 break; 1654 break;
1634 } 1655 }
1635 1656
1636 conn->src = &hcon->hdev->bdaddr;
1637 conn->dst = &hcon->dst;
1638
1639 conn->feat_mask = 0; 1657 conn->feat_mask = 0;
1640 1658
1659 if (hcon->type == ACL_LINK)
1660 conn->hs_enabled = test_bit(HCI_HS_ENABLED,
1661 &hcon->hdev->dev_flags);
1662
1641 spin_lock_init(&conn->lock); 1663 spin_lock_init(&conn->lock);
1642 mutex_init(&conn->chan_lock); 1664 mutex_init(&conn->chan_lock);
1643 1665
@@ -1688,8 +1710,6 @@ static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1688 read_lock(&chan_list_lock); 1710 read_lock(&chan_list_lock);
1689 1711
1690 list_for_each_entry(c, &chan_list, global_l) { 1712 list_for_each_entry(c, &chan_list, global_l) {
1691 struct sock *sk = c->sk;
1692
1693 if (state && c->state != state) 1713 if (state && c->state != state)
1694 continue; 1714 continue;
1695 1715
@@ -1698,16 +1718,16 @@ static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1698 int src_any, dst_any; 1718 int src_any, dst_any;
1699 1719
1700 /* Exact match. */ 1720 /* Exact match. */
1701 src_match = !bacmp(&bt_sk(sk)->src, src); 1721 src_match = !bacmp(&c->src, src);
1702 dst_match = !bacmp(&bt_sk(sk)->dst, dst); 1722 dst_match = !bacmp(&c->dst, dst);
1703 if (src_match && dst_match) { 1723 if (src_match && dst_match) {
1704 read_unlock(&chan_list_lock); 1724 read_unlock(&chan_list_lock);
1705 return c; 1725 return c;
1706 } 1726 }
1707 1727
1708 /* Closest match */ 1728 /* Closest match */
1709 src_any = !bacmp(&bt_sk(sk)->src, BDADDR_ANY); 1729 src_any = !bacmp(&c->src, BDADDR_ANY);
1710 dst_any = !bacmp(&bt_sk(sk)->dst, BDADDR_ANY); 1730 dst_any = !bacmp(&c->dst, BDADDR_ANY);
1711 if ((src_match && dst_any) || (src_any && dst_match) || 1731 if ((src_match && dst_any) || (src_any && dst_match) ||
1712 (src_any && dst_any)) 1732 (src_any && dst_any))
1713 c1 = c; 1733 c1 = c;
@@ -1722,18 +1742,16 @@ static struct l2cap_chan *l2cap_global_chan_by_psm(int state, __le16 psm,
1722int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid, 1742int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
1723 bdaddr_t *dst, u8 dst_type) 1743 bdaddr_t *dst, u8 dst_type)
1724{ 1744{
1725 struct sock *sk = chan->sk;
1726 bdaddr_t *src = &bt_sk(sk)->src;
1727 struct l2cap_conn *conn; 1745 struct l2cap_conn *conn;
1728 struct hci_conn *hcon; 1746 struct hci_conn *hcon;
1729 struct hci_dev *hdev; 1747 struct hci_dev *hdev;
1730 __u8 auth_type; 1748 __u8 auth_type;
1731 int err; 1749 int err;
1732 1750
1733 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", src, dst, 1751 BT_DBG("%pMR -> %pMR (type %u) psm 0x%2.2x", &chan->src, dst,
1734 dst_type, __le16_to_cpu(psm)); 1752 dst_type, __le16_to_cpu(psm));
1735 1753
1736 hdev = hci_get_route(dst, src); 1754 hdev = hci_get_route(dst, &chan->src);
1737 if (!hdev) 1755 if (!hdev)
1738 return -EHOSTUNREACH; 1756 return -EHOSTUNREACH;
1739 1757
@@ -1790,9 +1808,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
1790 } 1808 }
1791 1809
1792 /* Set destination address and psm */ 1810 /* Set destination address and psm */
1793 lock_sock(sk); 1811 bacpy(&chan->dst, dst);
1794 bacpy(&bt_sk(sk)->dst, dst); 1812 chan->dst_type = dst_type;
1795 release_sock(sk);
1796 1813
1797 chan->psm = psm; 1814 chan->psm = psm;
1798 chan->dcid = cid; 1815 chan->dcid = cid;
@@ -1825,7 +1842,8 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
1825 } 1842 }
1826 1843
1827 /* Update source addr of the socket */ 1844 /* Update source addr of the socket */
1828 bacpy(src, conn->src); 1845 bacpy(&chan->src, &hcon->src);
1846 chan->src_type = bdaddr_type(hcon, hcon->src_type);
1829 1847
1830 l2cap_chan_unlock(chan); 1848 l2cap_chan_unlock(chan);
1831 l2cap_chan_add(conn, chan); 1849 l2cap_chan_add(conn, chan);
@@ -1835,7 +1853,7 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
1835 hci_conn_drop(hcon); 1853 hci_conn_drop(hcon);
1836 1854
1837 l2cap_state_change(chan, BT_CONNECT); 1855 l2cap_state_change(chan, BT_CONNECT);
1838 __set_chan_timer(chan, sk->sk_sndtimeo); 1856 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
1839 1857
1840 if (hcon->state == BT_CONNECTED) { 1858 if (hcon->state == BT_CONNECTED) {
1841 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) { 1859 if (chan->chan_type != L2CAP_CHAN_CONN_ORIENTED) {
@@ -1855,38 +1873,6 @@ done:
1855 return err; 1873 return err;
1856} 1874}
1857 1875
1858int __l2cap_wait_ack(struct sock *sk)
1859{
1860 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
1861 DECLARE_WAITQUEUE(wait, current);
1862 int err = 0;
1863 int timeo = HZ/5;
1864
1865 add_wait_queue(sk_sleep(sk), &wait);
1866 set_current_state(TASK_INTERRUPTIBLE);
1867 while (chan->unacked_frames > 0 && chan->conn) {
1868 if (!timeo)
1869 timeo = HZ/5;
1870
1871 if (signal_pending(current)) {
1872 err = sock_intr_errno(timeo);
1873 break;
1874 }
1875
1876 release_sock(sk);
1877 timeo = schedule_timeout(timeo);
1878 lock_sock(sk);
1879 set_current_state(TASK_INTERRUPTIBLE);
1880
1881 err = sock_error(sk);
1882 if (err)
1883 break;
1884 }
1885 set_current_state(TASK_RUNNING);
1886 remove_wait_queue(sk_sleep(sk), &wait);
1887 return err;
1888}
1889
1890static void l2cap_monitor_timeout(struct work_struct *work) 1876static void l2cap_monitor_timeout(struct work_struct *work)
1891{ 1877{
1892 struct l2cap_chan *chan = container_of(work, struct l2cap_chan, 1878 struct l2cap_chan *chan = container_of(work, struct l2cap_chan,
@@ -2263,7 +2249,8 @@ static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
2263 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE; 2249 int err, count, hlen = L2CAP_HDR_SIZE + L2CAP_PSMLEN_SIZE;
2264 struct l2cap_hdr *lh; 2250 struct l2cap_hdr *lh;
2265 2251
2266 BT_DBG("chan %p len %zu priority %u", chan, len, priority); 2252 BT_DBG("chan %p psm 0x%2.2x len %zu priority %u", chan,
2253 __le16_to_cpu(chan->psm), len, priority);
2267 2254
2268 count = min_t(unsigned int, (conn->mtu - hlen), len); 2255 count = min_t(unsigned int, (conn->mtu - hlen), len);
2269 2256
@@ -2278,7 +2265,7 @@ static struct sk_buff *l2cap_create_connless_pdu(struct l2cap_chan *chan,
2278 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE); 2265 lh = (struct l2cap_hdr *) skb_put(skb, L2CAP_HDR_SIZE);
2279 lh->cid = cpu_to_le16(chan->dcid); 2266 lh->cid = cpu_to_le16(chan->dcid);
2280 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE); 2267 lh->len = cpu_to_le16(len + L2CAP_PSMLEN_SIZE);
2281 put_unaligned(chan->psm, skb_put(skb, L2CAP_PSMLEN_SIZE)); 2268 put_unaligned(chan->psm, (__le16 *) skb_put(skb, L2CAP_PSMLEN_SIZE));
2282 2269
2283 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb); 2270 err = l2cap_skbuff_fromiovec(chan, msg, len, count, skb);
2284 if (unlikely(err < 0)) { 2271 if (unlikely(err < 0)) {
@@ -2452,6 +2439,9 @@ int l2cap_chan_send(struct l2cap_chan *chan, struct msghdr *msg, size_t len,
2452 int err; 2439 int err;
2453 struct sk_buff_head seg_queue; 2440 struct sk_buff_head seg_queue;
2454 2441
2442 if (!chan->conn)
2443 return -ENOTCONN;
2444
2455 /* Connectionless channel */ 2445 /* Connectionless channel */
2456 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) { 2446 if (chan->chan_type == L2CAP_CHAN_CONN_LESS) {
2457 skb = l2cap_create_connless_pdu(chan, msg, len, priority); 2447 skb = l2cap_create_connless_pdu(chan, msg, len, priority);
@@ -2826,17 +2816,16 @@ static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2826 mutex_lock(&conn->chan_lock); 2816 mutex_lock(&conn->chan_lock);
2827 2817
2828 list_for_each_entry(chan, &conn->chan_l, list) { 2818 list_for_each_entry(chan, &conn->chan_l, list) {
2829 struct sock *sk = chan->sk;
2830 if (chan->chan_type != L2CAP_CHAN_RAW) 2819 if (chan->chan_type != L2CAP_CHAN_RAW)
2831 continue; 2820 continue;
2832 2821
2833 /* Don't send frame to the socket it came from */ 2822 /* Don't send frame to the channel it came from */
2834 if (skb->sk == sk) 2823 if (bt_cb(skb)->chan == chan)
2835 continue; 2824 continue;
2825
2836 nskb = skb_clone(skb, GFP_KERNEL); 2826 nskb = skb_clone(skb, GFP_KERNEL);
2837 if (!nskb) 2827 if (!nskb)
2838 continue; 2828 continue;
2839
2840 if (chan->ops->recv(chan, nskb)) 2829 if (chan->ops->recv(chan, nskb))
2841 kfree_skb(nskb); 2830 kfree_skb(nskb);
2842 } 2831 }
@@ -3043,8 +3032,8 @@ int l2cap_ertm_init(struct l2cap_chan *chan)
3043 3032
3044 skb_queue_head_init(&chan->tx_q); 3033 skb_queue_head_init(&chan->tx_q);
3045 3034
3046 chan->local_amp_id = 0; 3035 chan->local_amp_id = AMP_ID_BREDR;
3047 chan->move_id = 0; 3036 chan->move_id = AMP_ID_BREDR;
3048 chan->move_state = L2CAP_MOVE_STABLE; 3037 chan->move_state = L2CAP_MOVE_STABLE;
3049 chan->move_role = L2CAP_MOVE_ROLE_NONE; 3038 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3050 3039
@@ -3084,20 +3073,20 @@ static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3084 } 3073 }
3085} 3074}
3086 3075
3087static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 3076static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
3088{ 3077{
3089 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 3078 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
3090} 3079}
3091 3080
3092static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 3081static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
3093{ 3082{
3094 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 3083 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
3095} 3084}
3096 3085
3097static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, 3086static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3098 struct l2cap_conf_rfc *rfc) 3087 struct l2cap_conf_rfc *rfc)
3099{ 3088{
3100 if (chan->local_amp_id && chan->hs_hcon) { 3089 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
3101 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; 3090 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3102 3091
3103 /* Class 1 devices have must have ERTM timeouts 3092 /* Class 1 devices have must have ERTM timeouts
@@ -3135,7 +3124,7 @@ static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3135static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 3124static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3136{ 3125{
3137 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 3126 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
3138 __l2cap_ews_supported(chan)) { 3127 __l2cap_ews_supported(chan->conn)) {
3139 /* use extended control field */ 3128 /* use extended control field */
3140 set_bit(FLAG_EXT_CTRL, &chan->flags); 3129 set_bit(FLAG_EXT_CTRL, &chan->flags);
3141 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 3130 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
@@ -3165,7 +3154,7 @@ static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
3165 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 3154 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
3166 break; 3155 break;
3167 3156
3168 if (__l2cap_efs_supported(chan)) 3157 if (__l2cap_efs_supported(chan->conn))
3169 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3158 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3170 3159
3171 /* fall through */ 3160 /* fall through */
@@ -3317,7 +3306,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
3317 break; 3306 break;
3318 3307
3319 case L2CAP_CONF_EWS: 3308 case L2CAP_CONF_EWS:
3320 if (!enable_hs) 3309 if (!chan->conn->hs_enabled)
3321 return -ECONNREFUSED; 3310 return -ECONNREFUSED;
3322 3311
3323 set_bit(FLAG_EXT_CTRL, &chan->flags); 3312 set_bit(FLAG_EXT_CTRL, &chan->flags);
@@ -3349,7 +3338,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
3349 } 3338 }
3350 3339
3351 if (remote_efs) { 3340 if (remote_efs) {
3352 if (__l2cap_efs_supported(chan)) 3341 if (__l2cap_efs_supported(chan->conn))
3353 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3342 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3354 else 3343 else
3355 return -ECONNREFUSED; 3344 return -ECONNREFUSED;
@@ -3715,7 +3704,6 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3715 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 3704 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3716 struct l2cap_conn_rsp rsp; 3705 struct l2cap_conn_rsp rsp;
3717 struct l2cap_chan *chan = NULL, *pchan; 3706 struct l2cap_chan *chan = NULL, *pchan;
3718 struct sock *parent, *sk = NULL;
3719 int result, status = L2CAP_CS_NO_INFO; 3707 int result, status = L2CAP_CS_NO_INFO;
3720 3708
3721 u16 dcid = 0, scid = __le16_to_cpu(req->scid); 3709 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
@@ -3724,16 +3712,15 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3724 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid); 3712 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
3725 3713
3726 /* Check if we have socket listening on psm */ 3714 /* Check if we have socket listening on psm */
3727 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst); 3715 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
3716 &conn->hcon->dst);
3728 if (!pchan) { 3717 if (!pchan) {
3729 result = L2CAP_CR_BAD_PSM; 3718 result = L2CAP_CR_BAD_PSM;
3730 goto sendresp; 3719 goto sendresp;
3731 } 3720 }
3732 3721
3733 parent = pchan->sk;
3734
3735 mutex_lock(&conn->chan_lock); 3722 mutex_lock(&conn->chan_lock);
3736 lock_sock(parent); 3723 l2cap_chan_lock(pchan);
3737 3724
3738 /* Check if the ACL is secure enough (if not SDP) */ 3725 /* Check if the ACL is secure enough (if not SDP) */
3739 if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && 3726 if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) &&
@@ -3753,8 +3740,6 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3753 if (!chan) 3740 if (!chan)
3754 goto response; 3741 goto response;
3755 3742
3756 sk = chan->sk;
3757
3758 /* For certain devices (ex: HID mouse), support for authentication, 3743 /* For certain devices (ex: HID mouse), support for authentication,
3759 * pairing and bonding is optional. For such devices, inorder to avoid 3744 * pairing and bonding is optional. For such devices, inorder to avoid
3760 * the ACL alive for too long after L2CAP disconnection, reset the ACL 3745 * the ACL alive for too long after L2CAP disconnection, reset the ACL
@@ -3762,8 +3747,10 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3762 */ 3747 */
3763 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; 3748 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3764 3749
3765 bacpy(&bt_sk(sk)->src, conn->src); 3750 bacpy(&chan->src, &conn->hcon->src);
3766 bacpy(&bt_sk(sk)->dst, conn->dst); 3751 bacpy(&chan->dst, &conn->hcon->dst);
3752 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
3753 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
3767 chan->psm = psm; 3754 chan->psm = psm;
3768 chan->dcid = scid; 3755 chan->dcid = scid;
3769 chan->local_amp_id = amp_id; 3756 chan->local_amp_id = amp_id;
@@ -3772,14 +3759,14 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3772 3759
3773 dcid = chan->scid; 3760 dcid = chan->scid;
3774 3761
3775 __set_chan_timer(chan, sk->sk_sndtimeo); 3762 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
3776 3763
3777 chan->ident = cmd->ident; 3764 chan->ident = cmd->ident;
3778 3765
3779 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 3766 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
3780 if (l2cap_chan_check_security(chan)) { 3767 if (l2cap_chan_check_security(chan)) {
3781 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 3768 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
3782 __l2cap_state_change(chan, BT_CONNECT2); 3769 l2cap_state_change(chan, BT_CONNECT2);
3783 result = L2CAP_CR_PEND; 3770 result = L2CAP_CR_PEND;
3784 status = L2CAP_CS_AUTHOR_PEND; 3771 status = L2CAP_CS_AUTHOR_PEND;
3785 chan->ops->defer(chan); 3772 chan->ops->defer(chan);
@@ -3788,28 +3775,28 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3788 * The connection will succeed after the 3775 * The connection will succeed after the
3789 * physical link is up. 3776 * physical link is up.
3790 */ 3777 */
3791 if (amp_id) { 3778 if (amp_id == AMP_ID_BREDR) {
3792 __l2cap_state_change(chan, BT_CONNECT2); 3779 l2cap_state_change(chan, BT_CONFIG);
3793 result = L2CAP_CR_PEND;
3794 } else {
3795 __l2cap_state_change(chan, BT_CONFIG);
3796 result = L2CAP_CR_SUCCESS; 3780 result = L2CAP_CR_SUCCESS;
3781 } else {
3782 l2cap_state_change(chan, BT_CONNECT2);
3783 result = L2CAP_CR_PEND;
3797 } 3784 }
3798 status = L2CAP_CS_NO_INFO; 3785 status = L2CAP_CS_NO_INFO;
3799 } 3786 }
3800 } else { 3787 } else {
3801 __l2cap_state_change(chan, BT_CONNECT2); 3788 l2cap_state_change(chan, BT_CONNECT2);
3802 result = L2CAP_CR_PEND; 3789 result = L2CAP_CR_PEND;
3803 status = L2CAP_CS_AUTHEN_PEND; 3790 status = L2CAP_CS_AUTHEN_PEND;
3804 } 3791 }
3805 } else { 3792 } else {
3806 __l2cap_state_change(chan, BT_CONNECT2); 3793 l2cap_state_change(chan, BT_CONNECT2);
3807 result = L2CAP_CR_PEND; 3794 result = L2CAP_CR_PEND;
3808 status = L2CAP_CS_NO_INFO; 3795 status = L2CAP_CS_NO_INFO;
3809 } 3796 }
3810 3797
3811response: 3798response:
3812 release_sock(parent); 3799 l2cap_chan_unlock(pchan);
3813 mutex_unlock(&conn->chan_lock); 3800 mutex_unlock(&conn->chan_lock);
3814 3801
3815sendresp: 3802sendresp:
@@ -3891,13 +3878,13 @@ static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
3891 if (scid) { 3878 if (scid) {
3892 chan = __l2cap_get_chan_by_scid(conn, scid); 3879 chan = __l2cap_get_chan_by_scid(conn, scid);
3893 if (!chan) { 3880 if (!chan) {
3894 err = -EFAULT; 3881 err = -EBADSLT;
3895 goto unlock; 3882 goto unlock;
3896 } 3883 }
3897 } else { 3884 } else {
3898 chan = __l2cap_get_chan_by_ident(conn, cmd->ident); 3885 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3899 if (!chan) { 3886 if (!chan) {
3900 err = -EFAULT; 3887 err = -EBADSLT;
3901 goto unlock; 3888 goto unlock;
3902 } 3889 }
3903 } 3890 }
@@ -3965,6 +3952,18 @@ static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3965 L2CAP_CONF_SUCCESS, flags), data); 3952 L2CAP_CONF_SUCCESS, flags), data);
3966} 3953}
3967 3954
3955static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
3956 u16 scid, u16 dcid)
3957{
3958 struct l2cap_cmd_rej_cid rej;
3959
3960 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID);
3961 rej.scid = __cpu_to_le16(scid);
3962 rej.dcid = __cpu_to_le16(dcid);
3963
3964 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
3965}
3966
3968static inline int l2cap_config_req(struct l2cap_conn *conn, 3967static inline int l2cap_config_req(struct l2cap_conn *conn,
3969 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3968 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3970 u8 *data) 3969 u8 *data)
@@ -3984,18 +3983,14 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
3984 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 3983 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
3985 3984
3986 chan = l2cap_get_chan_by_scid(conn, dcid); 3985 chan = l2cap_get_chan_by_scid(conn, dcid);
3987 if (!chan) 3986 if (!chan) {
3988 return -ENOENT; 3987 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
3988 return 0;
3989 }
3989 3990
3990 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 3991 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
3991 struct l2cap_cmd_rej_cid rej; 3992 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
3992 3993 chan->dcid);
3993 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID);
3994 rej.scid = cpu_to_le16(chan->scid);
3995 rej.dcid = cpu_to_le16(chan->dcid);
3996
3997 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
3998 sizeof(rej), &rej);
3999 goto unlock; 3994 goto unlock;
4000 } 3995 }
4001 3996
@@ -4198,7 +4193,6 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
4198 struct l2cap_disconn_rsp rsp; 4193 struct l2cap_disconn_rsp rsp;
4199 u16 dcid, scid; 4194 u16 dcid, scid;
4200 struct l2cap_chan *chan; 4195 struct l2cap_chan *chan;
4201 struct sock *sk;
4202 4196
4203 if (cmd_len != sizeof(*req)) 4197 if (cmd_len != sizeof(*req))
4204 return -EPROTO; 4198 return -EPROTO;
@@ -4213,20 +4207,17 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
4213 chan = __l2cap_get_chan_by_scid(conn, dcid); 4207 chan = __l2cap_get_chan_by_scid(conn, dcid);
4214 if (!chan) { 4208 if (!chan) {
4215 mutex_unlock(&conn->chan_lock); 4209 mutex_unlock(&conn->chan_lock);
4210 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4216 return 0; 4211 return 0;
4217 } 4212 }
4218 4213
4219 l2cap_chan_lock(chan); 4214 l2cap_chan_lock(chan);
4220 4215
4221 sk = chan->sk;
4222
4223 rsp.dcid = cpu_to_le16(chan->scid); 4216 rsp.dcid = cpu_to_le16(chan->scid);
4224 rsp.scid = cpu_to_le16(chan->dcid); 4217 rsp.scid = cpu_to_le16(chan->dcid);
4225 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 4218 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4226 4219
4227 lock_sock(sk); 4220 chan->ops->set_shutdown(chan);
4228 sk->sk_shutdown = SHUTDOWN_MASK;
4229 release_sock(sk);
4230 4221
4231 l2cap_chan_hold(chan); 4222 l2cap_chan_hold(chan);
4232 l2cap_chan_del(chan, ECONNRESET); 4223 l2cap_chan_del(chan, ECONNRESET);
@@ -4303,7 +4294,7 @@ static inline int l2cap_information_req(struct l2cap_conn *conn,
4303 if (!disable_ertm) 4294 if (!disable_ertm)
4304 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 4295 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
4305 | L2CAP_FEAT_FCS; 4296 | L2CAP_FEAT_FCS;
4306 if (enable_hs) 4297 if (conn->hs_enabled)
4307 feat_mask |= L2CAP_FEAT_EXT_FLOW 4298 feat_mask |= L2CAP_FEAT_EXT_FLOW
4308 | L2CAP_FEAT_EXT_WINDOW; 4299 | L2CAP_FEAT_EXT_WINDOW;
4309 4300
@@ -4314,7 +4305,7 @@ static inline int l2cap_information_req(struct l2cap_conn *conn,
4314 u8 buf[12]; 4305 u8 buf[12];
4315 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 4306 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
4316 4307
4317 if (enable_hs) 4308 if (conn->hs_enabled)
4318 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 4309 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4319 else 4310 else
4320 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 4311 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
@@ -4411,7 +4402,7 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn,
4411 if (cmd_len != sizeof(*req)) 4402 if (cmd_len != sizeof(*req))
4412 return -EPROTO; 4403 return -EPROTO;
4413 4404
4414 if (!enable_hs) 4405 if (!conn->hs_enabled)
4415 return -EINVAL; 4406 return -EINVAL;
4416 4407
4417 psm = le16_to_cpu(req->psm); 4408 psm = le16_to_cpu(req->psm);
@@ -4420,7 +4411,7 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn,
4420 BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id); 4411 BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id);
4421 4412
4422 /* For controller id 0 make BR/EDR connection */ 4413 /* For controller id 0 make BR/EDR connection */
4423 if (req->amp_id == HCI_BREDR_ID) { 4414 if (req->amp_id == AMP_ID_BREDR) {
4424 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, 4415 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4425 req->amp_id); 4416 req->amp_id);
4426 return 0; 4417 return 0;
@@ -4442,10 +4433,13 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn,
4442 struct amp_mgr *mgr = conn->hcon->amp_mgr; 4433 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4443 struct hci_conn *hs_hcon; 4434 struct hci_conn *hs_hcon;
4444 4435
4445 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, conn->dst); 4436 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4437 &conn->hcon->dst);
4446 if (!hs_hcon) { 4438 if (!hs_hcon) {
4447 hci_dev_put(hdev); 4439 hci_dev_put(hdev);
4448 return -EFAULT; 4440 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4441 chan->dcid);
4442 return 0;
4449 } 4443 }
4450 4444
4451 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); 4445 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
@@ -4469,7 +4463,7 @@ error:
4469 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 4463 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4470 sizeof(rsp), &rsp); 4464 sizeof(rsp), &rsp);
4471 4465
4472 return -EFAULT; 4466 return 0;
4473} 4467}
4474 4468
4475static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) 4469static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
@@ -4655,7 +4649,7 @@ void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4655 4649
4656 if (chan->state != BT_CONNECTED) { 4650 if (chan->state != BT_CONNECTED) {
4657 /* Ignore logical link if channel is on BR/EDR */ 4651 /* Ignore logical link if channel is on BR/EDR */
4658 if (chan->local_amp_id) 4652 if (chan->local_amp_id != AMP_ID_BREDR)
4659 l2cap_logical_finish_create(chan, hchan); 4653 l2cap_logical_finish_create(chan, hchan);
4660 } else { 4654 } else {
4661 l2cap_logical_finish_move(chan, hchan); 4655 l2cap_logical_finish_move(chan, hchan);
@@ -4666,7 +4660,7 @@ void l2cap_move_start(struct l2cap_chan *chan)
4666{ 4660{
4667 BT_DBG("chan %p", chan); 4661 BT_DBG("chan %p", chan);
4668 4662
4669 if (chan->local_amp_id == HCI_BREDR_ID) { 4663 if (chan->local_amp_id == AMP_ID_BREDR) {
4670 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) 4664 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4671 return; 4665 return;
4672 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; 4666 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
@@ -4723,7 +4717,7 @@ static void l2cap_do_create(struct l2cap_chan *chan, int result,
4723 sizeof(rsp), &rsp); 4717 sizeof(rsp), &rsp);
4724 4718
4725 if (result == L2CAP_CR_SUCCESS) { 4719 if (result == L2CAP_CR_SUCCESS) {
4726 __l2cap_state_change(chan, BT_CONFIG); 4720 l2cap_state_change(chan, BT_CONFIG);
4727 set_bit(CONF_REQ_SENT, &chan->conf_state); 4721 set_bit(CONF_REQ_SENT, &chan->conf_state);
4728 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), 4722 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4729 L2CAP_CONF_REQ, 4723 L2CAP_CONF_REQ,
@@ -4838,7 +4832,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4838 4832
4839 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id); 4833 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
4840 4834
4841 if (!enable_hs) 4835 if (!conn->hs_enabled)
4842 return -EINVAL; 4836 return -EINVAL;
4843 4837
4844 chan = l2cap_get_chan_by_dcid(conn, icid); 4838 chan = l2cap_get_chan_by_dcid(conn, icid);
@@ -4865,7 +4859,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4865 goto send_move_response; 4859 goto send_move_response;
4866 } 4860 }
4867 4861
4868 if (req->dest_amp_id) { 4862 if (req->dest_amp_id != AMP_ID_BREDR) {
4869 struct hci_dev *hdev; 4863 struct hci_dev *hdev;
4870 hdev = hci_dev_get(req->dest_amp_id); 4864 hdev = hci_dev_get(req->dest_amp_id);
4871 if (!hdev || hdev->dev_type != HCI_AMP || 4865 if (!hdev || hdev->dev_type != HCI_AMP ||
@@ -4885,7 +4879,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4885 */ 4879 */
4886 if ((__chan_is_moving(chan) || 4880 if ((__chan_is_moving(chan) ||
4887 chan->move_role != L2CAP_MOVE_ROLE_NONE) && 4881 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
4888 bacmp(conn->src, conn->dst) > 0) { 4882 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
4889 result = L2CAP_MR_COLLISION; 4883 result = L2CAP_MR_COLLISION;
4890 goto send_move_response; 4884 goto send_move_response;
4891 } 4885 }
@@ -4895,7 +4889,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4895 chan->move_id = req->dest_amp_id; 4889 chan->move_id = req->dest_amp_id;
4896 icid = chan->dcid; 4890 icid = chan->dcid;
4897 4891
4898 if (!req->dest_amp_id) { 4892 if (req->dest_amp_id == AMP_ID_BREDR) {
4899 /* Moving to BR/EDR */ 4893 /* Moving to BR/EDR */
4900 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 4894 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4901 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; 4895 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
@@ -5087,7 +5081,7 @@ static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5087 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { 5081 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5088 if (result == L2CAP_MC_CONFIRMED) { 5082 if (result == L2CAP_MC_CONFIRMED) {
5089 chan->local_amp_id = chan->move_id; 5083 chan->local_amp_id = chan->move_id;
5090 if (!chan->local_amp_id) 5084 if (chan->local_amp_id == AMP_ID_BREDR)
5091 __release_logical_link(chan); 5085 __release_logical_link(chan);
5092 } else { 5086 } else {
5093 chan->move_id = chan->local_amp_id; 5087 chan->move_id = chan->local_amp_id;
@@ -5127,7 +5121,7 @@ static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
5127 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { 5121 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5128 chan->local_amp_id = chan->move_id; 5122 chan->local_amp_id = chan->move_id;
5129 5123
5130 if (!chan->local_amp_id && chan->hs_hchan) 5124 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
5131 __release_logical_link(chan); 5125 __release_logical_link(chan);
5132 5126
5133 l2cap_move_done(chan); 5127 l2cap_move_done(chan);
@@ -5219,7 +5213,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5219 5213
5220 case L2CAP_CONN_RSP: 5214 case L2CAP_CONN_RSP:
5221 case L2CAP_CREATE_CHAN_RSP: 5215 case L2CAP_CREATE_CHAN_RSP:
5222 err = l2cap_connect_create_rsp(conn, cmd, cmd_len, data); 5216 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
5223 break; 5217 break;
5224 5218
5225 case L2CAP_CONF_REQ: 5219 case L2CAP_CONF_REQ:
@@ -5227,7 +5221,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5227 break; 5221 break;
5228 5222
5229 case L2CAP_CONF_RSP: 5223 case L2CAP_CONF_RSP:
5230 err = l2cap_config_rsp(conn, cmd, cmd_len, data); 5224 l2cap_config_rsp(conn, cmd, cmd_len, data);
5231 break; 5225 break;
5232 5226
5233 case L2CAP_DISCONN_REQ: 5227 case L2CAP_DISCONN_REQ:
@@ -5235,7 +5229,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5235 break; 5229 break;
5236 5230
5237 case L2CAP_DISCONN_RSP: 5231 case L2CAP_DISCONN_RSP:
5238 err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data); 5232 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
5239 break; 5233 break;
5240 5234
5241 case L2CAP_ECHO_REQ: 5235 case L2CAP_ECHO_REQ:
@@ -5250,7 +5244,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5250 break; 5244 break;
5251 5245
5252 case L2CAP_INFO_RSP: 5246 case L2CAP_INFO_RSP:
5253 err = l2cap_information_rsp(conn, cmd, cmd_len, data); 5247 l2cap_information_rsp(conn, cmd, cmd_len, data);
5254 break; 5248 break;
5255 5249
5256 case L2CAP_CREATE_CHAN_REQ: 5250 case L2CAP_CREATE_CHAN_REQ:
@@ -5262,7 +5256,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5262 break; 5256 break;
5263 5257
5264 case L2CAP_MOVE_CHAN_RSP: 5258 case L2CAP_MOVE_CHAN_RSP:
5265 err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 5259 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
5266 break; 5260 break;
5267 5261
5268 case L2CAP_MOVE_CHAN_CFM: 5262 case L2CAP_MOVE_CHAN_CFM:
@@ -5270,7 +5264,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5270 break; 5264 break;
5271 5265
5272 case L2CAP_MOVE_CHAN_CFM_RSP: 5266 case L2CAP_MOVE_CHAN_CFM_RSP:
5273 err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 5267 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
5274 break; 5268 break;
5275 5269
5276 default: 5270 default:
@@ -5304,51 +5298,48 @@ static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
5304static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, 5298static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5305 struct sk_buff *skb) 5299 struct sk_buff *skb)
5306{ 5300{
5307 u8 *data = skb->data; 5301 struct hci_conn *hcon = conn->hcon;
5308 int len = skb->len; 5302 struct l2cap_cmd_hdr *cmd;
5309 struct l2cap_cmd_hdr cmd; 5303 u16 len;
5310 int err; 5304 int err;
5311 5305
5312 l2cap_raw_recv(conn, skb); 5306 if (hcon->type != LE_LINK)
5307 goto drop;
5313 5308
5314 while (len >= L2CAP_CMD_HDR_SIZE) { 5309 if (skb->len < L2CAP_CMD_HDR_SIZE)
5315 u16 cmd_len; 5310 goto drop;
5316 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5317 data += L2CAP_CMD_HDR_SIZE;
5318 len -= L2CAP_CMD_HDR_SIZE;
5319 5311
5320 cmd_len = le16_to_cpu(cmd.len); 5312 cmd = (void *) skb->data;
5313 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
5321 5314
5322 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, 5315 len = le16_to_cpu(cmd->len);
5323 cmd.ident);
5324 5316
5325 if (cmd_len > len || !cmd.ident) { 5317 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd->code, len, cmd->ident);
5326 BT_DBG("corrupted command");
5327 break;
5328 }
5329 5318
5330 err = l2cap_le_sig_cmd(conn, &cmd, data); 5319 if (len != skb->len || !cmd->ident) {
5331 if (err) { 5320 BT_DBG("corrupted command");
5332 struct l2cap_cmd_rej_unk rej; 5321 goto drop;
5322 }
5333 5323
5334 BT_ERR("Wrong link type (%d)", err); 5324 err = l2cap_le_sig_cmd(conn, cmd, skb->data);
5325 if (err) {
5326 struct l2cap_cmd_rej_unk rej;
5335 5327
5336 /* FIXME: Map err to a valid reason */ 5328 BT_ERR("Wrong link type (%d)", err);
5337 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5338 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5339 sizeof(rej), &rej);
5340 }
5341 5329
5342 data += cmd_len; 5330 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5343 len -= cmd_len; 5331 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5332 sizeof(rej), &rej);
5344 } 5333 }
5345 5334
5335drop:
5346 kfree_skb(skb); 5336 kfree_skb(skb);
5347} 5337}
5348 5338
5349static inline void l2cap_sig_channel(struct l2cap_conn *conn, 5339static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5350 struct sk_buff *skb) 5340 struct sk_buff *skb)
5351{ 5341{
5342 struct hci_conn *hcon = conn->hcon;
5352 u8 *data = skb->data; 5343 u8 *data = skb->data;
5353 int len = skb->len; 5344 int len = skb->len;
5354 struct l2cap_cmd_hdr cmd; 5345 struct l2cap_cmd_hdr cmd;
@@ -5356,6 +5347,9 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5356 5347
5357 l2cap_raw_recv(conn, skb); 5348 l2cap_raw_recv(conn, skb);
5358 5349
5350 if (hcon->type != ACL_LINK)
5351 goto drop;
5352
5359 while (len >= L2CAP_CMD_HDR_SIZE) { 5353 while (len >= L2CAP_CMD_HDR_SIZE) {
5360 u16 cmd_len; 5354 u16 cmd_len;
5361 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 5355 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
@@ -5378,7 +5372,6 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5378 5372
5379 BT_ERR("Wrong link type (%d)", err); 5373 BT_ERR("Wrong link type (%d)", err);
5380 5374
5381 /* FIXME: Map err to a valid reason */
5382 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 5375 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5383 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, 5376 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5384 sizeof(rej), &rej); 5377 sizeof(rej), &rej);
@@ -5388,6 +5381,7 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5388 len -= cmd_len; 5381 len -= cmd_len;
5389 } 5382 }
5390 5383
5384drop:
5391 kfree_skb(skb); 5385 kfree_skb(skb);
5392} 5386}
5393 5387
@@ -5784,7 +5778,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5784 struct sk_buff *skb, u8 event) 5778 struct sk_buff *skb, u8 event)
5785{ 5779{
5786 int err = 0; 5780 int err = 0;
5787 bool skb_in_use = 0; 5781 bool skb_in_use = false;
5788 5782
5789 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 5783 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5790 event); 5784 event);
@@ -5805,7 +5799,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5805 control->txseq); 5799 control->txseq);
5806 5800
5807 chan->buffer_seq = chan->expected_tx_seq; 5801 chan->buffer_seq = chan->expected_tx_seq;
5808 skb_in_use = 1; 5802 skb_in_use = true;
5809 5803
5810 err = l2cap_reassemble_sdu(chan, skb, control); 5804 err = l2cap_reassemble_sdu(chan, skb, control);
5811 if (err) 5805 if (err)
@@ -5841,7 +5835,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5841 * current frame is stored for later use. 5835 * current frame is stored for later use.
5842 */ 5836 */
5843 skb_queue_tail(&chan->srej_q, skb); 5837 skb_queue_tail(&chan->srej_q, skb);
5844 skb_in_use = 1; 5838 skb_in_use = true;
5845 BT_DBG("Queued %p (queue len %d)", skb, 5839 BT_DBG("Queued %p (queue len %d)", skb,
5846 skb_queue_len(&chan->srej_q)); 5840 skb_queue_len(&chan->srej_q));
5847 5841
@@ -5919,7 +5913,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5919{ 5913{
5920 int err = 0; 5914 int err = 0;
5921 u16 txseq = control->txseq; 5915 u16 txseq = control->txseq;
5922 bool skb_in_use = 0; 5916 bool skb_in_use = false;
5923 5917
5924 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 5918 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5925 event); 5919 event);
@@ -5931,7 +5925,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5931 /* Keep frame for reassembly later */ 5925 /* Keep frame for reassembly later */
5932 l2cap_pass_to_tx(chan, control); 5926 l2cap_pass_to_tx(chan, control);
5933 skb_queue_tail(&chan->srej_q, skb); 5927 skb_queue_tail(&chan->srej_q, skb);
5934 skb_in_use = 1; 5928 skb_in_use = true;
5935 BT_DBG("Queued %p (queue len %d)", skb, 5929 BT_DBG("Queued %p (queue len %d)", skb,
5936 skb_queue_len(&chan->srej_q)); 5930 skb_queue_len(&chan->srej_q));
5937 5931
@@ -5942,7 +5936,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5942 5936
5943 l2cap_pass_to_tx(chan, control); 5937 l2cap_pass_to_tx(chan, control);
5944 skb_queue_tail(&chan->srej_q, skb); 5938 skb_queue_tail(&chan->srej_q, skb);
5945 skb_in_use = 1; 5939 skb_in_use = true;
5946 BT_DBG("Queued %p (queue len %d)", skb, 5940 BT_DBG("Queued %p (queue len %d)", skb,
5947 skb_queue_len(&chan->srej_q)); 5941 skb_queue_len(&chan->srej_q));
5948 5942
@@ -5957,7 +5951,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5957 * the missing frames. 5951 * the missing frames.
5958 */ 5952 */
5959 skb_queue_tail(&chan->srej_q, skb); 5953 skb_queue_tail(&chan->srej_q, skb);
5960 skb_in_use = 1; 5954 skb_in_use = true;
5961 BT_DBG("Queued %p (queue len %d)", skb, 5955 BT_DBG("Queued %p (queue len %d)", skb,
5962 skb_queue_len(&chan->srej_q)); 5956 skb_queue_len(&chan->srej_q));
5963 5957
@@ -5971,7 +5965,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5971 * SREJ'd frames. 5965 * SREJ'd frames.
5972 */ 5966 */
5973 skb_queue_tail(&chan->srej_q, skb); 5967 skb_queue_tail(&chan->srej_q, skb);
5974 skb_in_use = 1; 5968 skb_in_use = true;
5975 BT_DBG("Queued %p (queue len %d)", skb, 5969 BT_DBG("Queued %p (queue len %d)", skb,
5976 skb_queue_len(&chan->srej_q)); 5970 skb_queue_len(&chan->srej_q));
5977 5971
@@ -6380,9 +6374,13 @@ done:
6380static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, 6374static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6381 struct sk_buff *skb) 6375 struct sk_buff *skb)
6382{ 6376{
6377 struct hci_conn *hcon = conn->hcon;
6383 struct l2cap_chan *chan; 6378 struct l2cap_chan *chan;
6384 6379
6385 chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst); 6380 if (hcon->type != ACL_LINK)
6381 goto drop;
6382
6383 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst);
6386 if (!chan) 6384 if (!chan)
6387 goto drop; 6385 goto drop;
6388 6386
@@ -6394,6 +6392,10 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6394 if (chan->imtu < skb->len) 6392 if (chan->imtu < skb->len)
6395 goto drop; 6393 goto drop;
6396 6394
6395 /* Store remote BD_ADDR and PSM for msg_name */
6396 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
6397 bt_cb(skb)->psm = psm;
6398
6397 if (!chan->ops->recv(chan, skb)) 6399 if (!chan->ops->recv(chan, skb))
6398 return; 6400 return;
6399 6401
@@ -6404,15 +6406,22 @@ drop:
6404static void l2cap_att_channel(struct l2cap_conn *conn, 6406static void l2cap_att_channel(struct l2cap_conn *conn,
6405 struct sk_buff *skb) 6407 struct sk_buff *skb)
6406{ 6408{
6409 struct hci_conn *hcon = conn->hcon;
6407 struct l2cap_chan *chan; 6410 struct l2cap_chan *chan;
6408 6411
6412 if (hcon->type != LE_LINK)
6413 goto drop;
6414
6409 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT, 6415 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT,
6410 conn->src, conn->dst); 6416 &hcon->src, &hcon->dst);
6411 if (!chan) 6417 if (!chan)
6412 goto drop; 6418 goto drop;
6413 6419
6414 BT_DBG("chan %p, len %d", chan, skb->len); 6420 BT_DBG("chan %p, len %d", chan, skb->len);
6415 6421
6422 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, hcon->dst_type))
6423 goto drop;
6424
6416 if (chan->imtu < skb->len) 6425 if (chan->imtu < skb->len)
6417 goto drop; 6426 goto drop;
6418 6427
@@ -6441,9 +6450,6 @@ static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6441 BT_DBG("len %d, cid 0x%4.4x", len, cid); 6450 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6442 6451
6443 switch (cid) { 6452 switch (cid) {
6444 case L2CAP_CID_LE_SIGNALING:
6445 l2cap_le_sig_channel(conn, skb);
6446 break;
6447 case L2CAP_CID_SIGNALING: 6453 case L2CAP_CID_SIGNALING:
6448 l2cap_sig_channel(conn, skb); 6454 l2cap_sig_channel(conn, skb);
6449 break; 6455 break;
@@ -6458,6 +6464,10 @@ static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6458 l2cap_att_channel(conn, skb); 6464 l2cap_att_channel(conn, skb);
6459 break; 6465 break;
6460 6466
6467 case L2CAP_CID_LE_SIGNALING:
6468 l2cap_le_sig_channel(conn, skb);
6469 break;
6470
6461 case L2CAP_CID_SMP: 6471 case L2CAP_CID_SMP:
6462 if (smp_sig_channel(conn, skb)) 6472 if (smp_sig_channel(conn, skb))
6463 l2cap_conn_del(conn->hcon, EACCES); 6473 l2cap_conn_del(conn->hcon, EACCES);
@@ -6481,17 +6491,15 @@ int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
6481 /* Find listening sockets and check their link_mode */ 6491 /* Find listening sockets and check their link_mode */
6482 read_lock(&chan_list_lock); 6492 read_lock(&chan_list_lock);
6483 list_for_each_entry(c, &chan_list, global_l) { 6493 list_for_each_entry(c, &chan_list, global_l) {
6484 struct sock *sk = c->sk;
6485
6486 if (c->state != BT_LISTEN) 6494 if (c->state != BT_LISTEN)
6487 continue; 6495 continue;
6488 6496
6489 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 6497 if (!bacmp(&c->src, &hdev->bdaddr)) {
6490 lm1 |= HCI_LM_ACCEPT; 6498 lm1 |= HCI_LM_ACCEPT;
6491 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 6499 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
6492 lm1 |= HCI_LM_MASTER; 6500 lm1 |= HCI_LM_MASTER;
6493 exact++; 6501 exact++;
6494 } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 6502 } else if (!bacmp(&c->src, BDADDR_ANY)) {
6495 lm2 |= HCI_LM_ACCEPT; 6503 lm2 |= HCI_LM_ACCEPT;
6496 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 6504 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
6497 lm2 |= HCI_LM_MASTER; 6505 lm2 |= HCI_LM_MASTER;
@@ -6597,11 +6605,7 @@ int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
6597 6605
6598 if (!status && (chan->state == BT_CONNECTED || 6606 if (!status && (chan->state == BT_CONNECTED ||
6599 chan->state == BT_CONFIG)) { 6607 chan->state == BT_CONFIG)) {
6600 struct sock *sk = chan->sk; 6608 chan->ops->resume(chan);
6601
6602 clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
6603 sk->sk_state_change(sk);
6604
6605 l2cap_check_encryption(chan, encrypt); 6609 l2cap_check_encryption(chan, encrypt);
6606 l2cap_chan_unlock(chan); 6610 l2cap_chan_unlock(chan);
6607 continue; 6611 continue;
@@ -6614,32 +6618,26 @@ int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
6614 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 6618 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
6615 } 6619 }
6616 } else if (chan->state == BT_CONNECT2) { 6620 } else if (chan->state == BT_CONNECT2) {
6617 struct sock *sk = chan->sk;
6618 struct l2cap_conn_rsp rsp; 6621 struct l2cap_conn_rsp rsp;
6619 __u16 res, stat; 6622 __u16 res, stat;
6620 6623
6621 lock_sock(sk);
6622
6623 if (!status) { 6624 if (!status) {
6624 if (test_bit(BT_SK_DEFER_SETUP, 6625 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
6625 &bt_sk(sk)->flags)) {
6626 res = L2CAP_CR_PEND; 6626 res = L2CAP_CR_PEND;
6627 stat = L2CAP_CS_AUTHOR_PEND; 6627 stat = L2CAP_CS_AUTHOR_PEND;
6628 chan->ops->defer(chan); 6628 chan->ops->defer(chan);
6629 } else { 6629 } else {
6630 __l2cap_state_change(chan, BT_CONFIG); 6630 l2cap_state_change(chan, BT_CONFIG);
6631 res = L2CAP_CR_SUCCESS; 6631 res = L2CAP_CR_SUCCESS;
6632 stat = L2CAP_CS_NO_INFO; 6632 stat = L2CAP_CS_NO_INFO;
6633 } 6633 }
6634 } else { 6634 } else {
6635 __l2cap_state_change(chan, BT_DISCONN); 6635 l2cap_state_change(chan, BT_DISCONN);
6636 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 6636 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
6637 res = L2CAP_CR_SEC_BLOCK; 6637 res = L2CAP_CR_SEC_BLOCK;
6638 stat = L2CAP_CS_NO_INFO; 6638 stat = L2CAP_CS_NO_INFO;
6639 } 6639 }
6640 6640
6641 release_sock(sk);
6642
6643 rsp.scid = cpu_to_le16(chan->dcid); 6641 rsp.scid = cpu_to_le16(chan->dcid);
6644 rsp.dcid = cpu_to_le16(chan->scid); 6642 rsp.dcid = cpu_to_le16(chan->scid);
6645 rsp.result = cpu_to_le16(res); 6643 rsp.result = cpu_to_le16(res);
@@ -6756,9 +6754,13 @@ int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
6756 conn->rx_len -= skb->len; 6754 conn->rx_len -= skb->len;
6757 6755
6758 if (!conn->rx_len) { 6756 if (!conn->rx_len) {
6759 /* Complete frame received */ 6757 /* Complete frame received. l2cap_recv_frame
6760 l2cap_recv_frame(conn, conn->rx_skb); 6758 * takes ownership of the skb so set the global
6759 * rx_skb pointer to NULL first.
6760 */
6761 struct sk_buff *rx_skb = conn->rx_skb;
6761 conn->rx_skb = NULL; 6762 conn->rx_skb = NULL;
6763 l2cap_recv_frame(conn, rx_skb);
6762 } 6764 }
6763 break; 6765 break;
6764 } 6766 }
@@ -6775,10 +6777,8 @@ static int l2cap_debugfs_show(struct seq_file *f, void *p)
6775 read_lock(&chan_list_lock); 6777 read_lock(&chan_list_lock);
6776 6778
6777 list_for_each_entry(c, &chan_list, global_l) { 6779 list_for_each_entry(c, &chan_list, global_l) {
6778 struct sock *sk = c->sk;
6779
6780 seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n", 6780 seq_printf(f, "%pMR %pMR %d %d 0x%4.4x 0x%4.4x %d %d %d %d\n",
6781 &bt_sk(sk)->src, &bt_sk(sk)->dst, 6781 &c->src, &c->dst,
6782 c->state, __le16_to_cpu(c->psm), 6782 c->state, __le16_to_cpu(c->psm),
6783 c->scid, c->dcid, c->imtu, c->omtu, 6783 c->scid, c->dcid, c->imtu, c->omtu,
6784 c->sec_level, c->mode); 6784 c->sec_level, c->mode);
@@ -6811,12 +6811,11 @@ int __init l2cap_init(void)
6811 if (err < 0) 6811 if (err < 0)
6812 return err; 6812 return err;
6813 6813
6814 if (bt_debugfs) { 6814 if (IS_ERR_OR_NULL(bt_debugfs))
6815 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, 6815 return 0;
6816 NULL, &l2cap_debugfs_fops); 6816
6817 if (!l2cap_debugfs) 6817 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
6818 BT_ERR("Failed to create L2CAP debug file"); 6818 NULL, &l2cap_debugfs_fops);
6819 }
6820 6819
6821 return 0; 6820 return 0;
6822} 6821}
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 0098af80b213..7cc24d263caa 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -32,7 +32,8 @@
32#include <net/bluetooth/bluetooth.h> 32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h> 33#include <net/bluetooth/hci_core.h>
34#include <net/bluetooth/l2cap.h> 34#include <net/bluetooth/l2cap.h>
35#include <net/bluetooth/smp.h> 35
36#include "smp.h"
36 37
37static struct bt_sock_list l2cap_sk_list = { 38static struct bt_sock_list l2cap_sk_list = {
38 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock) 39 .lock = __RW_LOCK_UNLOCKED(l2cap_sk_list.lock)
@@ -68,6 +69,18 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
68 if (la.l2_cid && la.l2_psm) 69 if (la.l2_cid && la.l2_psm)
69 return -EINVAL; 70 return -EINVAL;
70 71
72 if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
73 return -EINVAL;
74
75 if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
76 /* Connection oriented channels are not supported on LE */
77 if (la.l2_psm)
78 return -EINVAL;
79 /* We only allow ATT user space socket */
80 if (la.l2_cid != __constant_cpu_to_le16(L2CAP_CID_ATT))
81 return -EINVAL;
82 }
83
71 lock_sock(sk); 84 lock_sock(sk);
72 85
73 if (sk->sk_state != BT_OPEN) { 86 if (sk->sk_state != BT_OPEN) {
@@ -99,11 +112,20 @@ static int l2cap_sock_bind(struct socket *sock, struct sockaddr *addr, int alen)
99 if (err < 0) 112 if (err < 0)
100 goto done; 113 goto done;
101 114
102 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP || 115 switch (chan->chan_type) {
103 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM) 116 case L2CAP_CHAN_CONN_LESS:
104 chan->sec_level = BT_SECURITY_SDP; 117 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_3DSP)
118 chan->sec_level = BT_SECURITY_SDP;
119 break;
120 case L2CAP_CHAN_CONN_ORIENTED:
121 if (__le16_to_cpu(la.l2_psm) == L2CAP_PSM_SDP ||
122 __le16_to_cpu(la.l2_psm) == L2CAP_PSM_RFCOMM)
123 chan->sec_level = BT_SECURITY_SDP;
124 break;
125 }
105 126
106 bacpy(&bt_sk(sk)->src, &la.l2_bdaddr); 127 bacpy(&chan->src, &la.l2_bdaddr);
128 chan->src_type = la.l2_bdaddr_type;
107 129
108 chan->state = BT_BOUND; 130 chan->state = BT_BOUND;
109 sk->sk_state = BT_BOUND; 131 sk->sk_state = BT_BOUND;
@@ -134,6 +156,47 @@ static int l2cap_sock_connect(struct socket *sock, struct sockaddr *addr,
134 if (la.l2_cid && la.l2_psm) 156 if (la.l2_cid && la.l2_psm)
135 return -EINVAL; 157 return -EINVAL;
136 158
159 if (!bdaddr_type_is_valid(la.l2_bdaddr_type))
160 return -EINVAL;
161
162 /* Check that the socket wasn't bound to something that
163 * conflicts with the address given to connect(). If chan->src
164 * is BDADDR_ANY it means bind() was never used, in which case
165 * chan->src_type and la.l2_bdaddr_type do not need to match.
166 */
167 if (chan->src_type == BDADDR_BREDR && bacmp(&chan->src, BDADDR_ANY) &&
168 bdaddr_type_is_le(la.l2_bdaddr_type)) {
169 /* Old user space versions will try to incorrectly bind
170 * the ATT socket using BDADDR_BREDR. We need to accept
171 * this and fix up the source address type only when
172 * both the source CID and destination CID indicate
173 * ATT. Anything else is an invalid combination.
174 */
175 if (chan->scid != L2CAP_CID_ATT ||
176 la.l2_cid != __constant_cpu_to_le16(L2CAP_CID_ATT))
177 return -EINVAL;
178
179 /* We don't have the hdev available here to make a
180 * better decision on random vs public, but since all
181 * user space versions that exhibit this issue anyway do
182 * not support random local addresses assuming public
183 * here is good enough.
184 */
185 chan->src_type = BDADDR_LE_PUBLIC;
186 }
187
188 if (chan->src_type != BDADDR_BREDR && la.l2_bdaddr_type == BDADDR_BREDR)
189 return -EINVAL;
190
191 if (bdaddr_type_is_le(la.l2_bdaddr_type)) {
192 /* Connection oriented channels are not supported on LE */
193 if (la.l2_psm)
194 return -EINVAL;
195 /* We only allow ATT user space socket */
196 if (la.l2_cid != __constant_cpu_to_le16(L2CAP_CID_ATT))
197 return -EINVAL;
198 }
199
137 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid), 200 err = l2cap_chan_connect(chan, la.l2_psm, __le16_to_cpu(la.l2_cid),
138 &la.l2_bdaddr, la.l2_bdaddr_type); 201 &la.l2_bdaddr, la.l2_bdaddr_type);
139 if (err) 202 if (err)
@@ -265,12 +328,14 @@ static int l2cap_sock_getname(struct socket *sock, struct sockaddr *addr,
265 328
266 if (peer) { 329 if (peer) {
267 la->l2_psm = chan->psm; 330 la->l2_psm = chan->psm;
268 bacpy(&la->l2_bdaddr, &bt_sk(sk)->dst); 331 bacpy(&la->l2_bdaddr, &chan->dst);
269 la->l2_cid = cpu_to_le16(chan->dcid); 332 la->l2_cid = cpu_to_le16(chan->dcid);
333 la->l2_bdaddr_type = chan->dst_type;
270 } else { 334 } else {
271 la->l2_psm = chan->sport; 335 la->l2_psm = chan->sport;
272 bacpy(&la->l2_bdaddr, &bt_sk(sk)->src); 336 bacpy(&la->l2_bdaddr, &chan->src);
273 la->l2_cid = cpu_to_le16(chan->scid); 337 la->l2_cid = cpu_to_le16(chan->scid);
338 la->l2_bdaddr_type = chan->src_type;
274 } 339 }
275 340
276 return 0; 341 return 0;
@@ -445,11 +510,6 @@ static int l2cap_sock_getsockopt(struct socket *sock, int level, int optname,
445 break; 510 break;
446 511
447 case BT_CHANNEL_POLICY: 512 case BT_CHANNEL_POLICY:
448 if (!enable_hs) {
449 err = -ENOPROTOOPT;
450 break;
451 }
452
453 if (put_user(chan->chan_policy, (u32 __user *) optval)) 513 if (put_user(chan->chan_policy, (u32 __user *) optval))
454 err = -EFAULT; 514 err = -EFAULT;
455 break; 515 break;
@@ -665,10 +725,13 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
665 break; 725 break;
666 } 726 }
667 727
668 if (opt) 728 if (opt) {
669 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 729 set_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
670 else 730 set_bit(FLAG_DEFER_SETUP, &chan->flags);
731 } else {
671 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags); 732 clear_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags);
733 clear_bit(FLAG_DEFER_SETUP, &chan->flags);
734 }
672 break; 735 break;
673 736
674 case BT_FLUSHABLE: 737 case BT_FLUSHABLE:
@@ -683,7 +746,7 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
683 } 746 }
684 747
685 if (opt == BT_FLUSHABLE_OFF) { 748 if (opt == BT_FLUSHABLE_OFF) {
686 struct l2cap_conn *conn = chan->conn; 749 conn = chan->conn;
687 /* proceed further only when we have l2cap_conn and 750 /* proceed further only when we have l2cap_conn and
688 No Flush support in the LM */ 751 No Flush support in the LM */
689 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) { 752 if (!conn || !lmp_no_flush_capable(conn->hcon->hdev)) {
@@ -720,11 +783,6 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
720 break; 783 break;
721 784
722 case BT_CHANNEL_POLICY: 785 case BT_CHANNEL_POLICY:
723 if (!enable_hs) {
724 err = -ENOPROTOOPT;
725 break;
726 }
727
728 if (get_user(opt, (u32 __user *) optval)) { 786 if (get_user(opt, (u32 __user *) optval)) {
729 err = -EFAULT; 787 err = -EFAULT;
730 break; 788 break;
@@ -777,6 +835,12 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
777 if (sk->sk_state != BT_CONNECTED) 835 if (sk->sk_state != BT_CONNECTED)
778 return -ENOTCONN; 836 return -ENOTCONN;
779 837
838 lock_sock(sk);
839 err = bt_sock_wait_ready(sk, msg->msg_flags);
840 release_sock(sk);
841 if (err)
842 return err;
843
780 l2cap_chan_lock(chan); 844 l2cap_chan_lock(chan);
781 err = l2cap_chan_send(chan, msg, len, sk->sk_priority); 845 err = l2cap_chan_send(chan, msg, len, sk->sk_priority);
782 l2cap_chan_unlock(chan); 846 l2cap_chan_unlock(chan);
@@ -799,8 +863,8 @@ static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
799 pi->chan->state = BT_CONFIG; 863 pi->chan->state = BT_CONFIG;
800 864
801 __l2cap_connect_rsp_defer(pi->chan); 865 __l2cap_connect_rsp_defer(pi->chan);
802 release_sock(sk); 866 err = 0;
803 return 0; 867 goto done;
804 } 868 }
805 869
806 release_sock(sk); 870 release_sock(sk);
@@ -856,6 +920,38 @@ static void l2cap_sock_kill(struct sock *sk)
856 sock_put(sk); 920 sock_put(sk);
857} 921}
858 922
923static int __l2cap_wait_ack(struct sock *sk)
924{
925 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
926 DECLARE_WAITQUEUE(wait, current);
927 int err = 0;
928 int timeo = HZ/5;
929
930 add_wait_queue(sk_sleep(sk), &wait);
931 set_current_state(TASK_INTERRUPTIBLE);
932 while (chan->unacked_frames > 0 && chan->conn) {
933 if (!timeo)
934 timeo = HZ/5;
935
936 if (signal_pending(current)) {
937 err = sock_intr_errno(timeo);
938 break;
939 }
940
941 release_sock(sk);
942 timeo = schedule_timeout(timeo);
943 lock_sock(sk);
944 set_current_state(TASK_INTERRUPTIBLE);
945
946 err = sock_error(sk);
947 if (err)
948 break;
949 }
950 set_current_state(TASK_RUNNING);
951 remove_wait_queue(sk_sleep(sk), &wait);
952 return err;
953}
954
859static int l2cap_sock_shutdown(struct socket *sock, int how) 955static int l2cap_sock_shutdown(struct socket *sock, int how)
860{ 956{
861 struct sock *sk = sock->sk; 957 struct sock *sk = sock->sk;
@@ -946,6 +1042,8 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
946{ 1042{
947 struct sock *sk, *parent = chan->data; 1043 struct sock *sk, *parent = chan->data;
948 1044
1045 lock_sock(parent);
1046
949 /* Check for backlog size */ 1047 /* Check for backlog size */
950 if (sk_acceptq_is_full(parent)) { 1048 if (sk_acceptq_is_full(parent)) {
951 BT_DBG("backlog full %d", parent->sk_ack_backlog); 1049 BT_DBG("backlog full %d", parent->sk_ack_backlog);
@@ -963,18 +1061,19 @@ static struct l2cap_chan *l2cap_sock_new_connection_cb(struct l2cap_chan *chan)
963 1061
964 bt_accept_enqueue(parent, sk); 1062 bt_accept_enqueue(parent, sk);
965 1063
1064 release_sock(parent);
1065
966 return l2cap_pi(sk)->chan; 1066 return l2cap_pi(sk)->chan;
967} 1067}
968 1068
969static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) 1069static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
970{ 1070{
971 int err;
972 struct sock *sk = chan->data; 1071 struct sock *sk = chan->data;
973 struct l2cap_pinfo *pi = l2cap_pi(sk); 1072 int err;
974 1073
975 lock_sock(sk); 1074 lock_sock(sk);
976 1075
977 if (pi->rx_busy_skb) { 1076 if (l2cap_pi(sk)->rx_busy_skb) {
978 err = -ENOMEM; 1077 err = -ENOMEM;
979 goto done; 1078 goto done;
980 } 1079 }
@@ -990,9 +1089,9 @@ static int l2cap_sock_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
990 * acked and reassembled until there is buffer space 1089 * acked and reassembled until there is buffer space
991 * available. 1090 * available.
992 */ 1091 */
993 if (err < 0 && pi->chan->mode == L2CAP_MODE_ERTM) { 1092 if (err < 0 && chan->mode == L2CAP_MODE_ERTM) {
994 pi->rx_busy_skb = skb; 1093 l2cap_pi(sk)->rx_busy_skb = skb;
995 l2cap_chan_busy(pi->chan, 1); 1094 l2cap_chan_busy(chan, 1);
996 err = 0; 1095 err = 0;
997 } 1096 }
998 1097
@@ -1050,26 +1149,33 @@ static void l2cap_sock_teardown_cb(struct l2cap_chan *chan, int err)
1050 release_sock(sk); 1149 release_sock(sk);
1051} 1150}
1052 1151
1053static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state) 1152static void l2cap_sock_state_change_cb(struct l2cap_chan *chan, int state,
1153 int err)
1054{ 1154{
1055 struct sock *sk = chan->data; 1155 struct sock *sk = chan->data;
1056 1156
1057 sk->sk_state = state; 1157 sk->sk_state = state;
1158
1159 if (err)
1160 sk->sk_err = err;
1058} 1161}
1059 1162
1060static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan, 1163static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
1061 unsigned long len, int nb) 1164 unsigned long len, int nb)
1062{ 1165{
1166 struct sock *sk = chan->data;
1063 struct sk_buff *skb; 1167 struct sk_buff *skb;
1064 int err; 1168 int err;
1065 1169
1066 l2cap_chan_unlock(chan); 1170 l2cap_chan_unlock(chan);
1067 skb = bt_skb_send_alloc(chan->sk, len, nb, &err); 1171 skb = bt_skb_send_alloc(sk, len, nb, &err);
1068 l2cap_chan_lock(chan); 1172 l2cap_chan_lock(chan);
1069 1173
1070 if (!skb) 1174 if (!skb)
1071 return ERR_PTR(err); 1175 return ERR_PTR(err);
1072 1176
1177 bt_cb(skb)->chan = chan;
1178
1073 return skb; 1179 return skb;
1074} 1180}
1075 1181
@@ -1095,11 +1201,39 @@ static void l2cap_sock_ready_cb(struct l2cap_chan *chan)
1095 1201
1096static void l2cap_sock_defer_cb(struct l2cap_chan *chan) 1202static void l2cap_sock_defer_cb(struct l2cap_chan *chan)
1097{ 1203{
1098 struct sock *sk = chan->data; 1204 struct sock *parent, *sk = chan->data;
1099 struct sock *parent = bt_sk(sk)->parent; 1205
1206 lock_sock(sk);
1100 1207
1208 parent = bt_sk(sk)->parent;
1101 if (parent) 1209 if (parent)
1102 parent->sk_data_ready(parent, 0); 1210 parent->sk_data_ready(parent, 0);
1211
1212 release_sock(sk);
1213}
1214
1215static void l2cap_sock_resume_cb(struct l2cap_chan *chan)
1216{
1217 struct sock *sk = chan->data;
1218
1219 clear_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags);
1220 sk->sk_state_change(sk);
1221}
1222
1223static void l2cap_sock_set_shutdown_cb(struct l2cap_chan *chan)
1224{
1225 struct sock *sk = chan->data;
1226
1227 lock_sock(sk);
1228 sk->sk_shutdown = SHUTDOWN_MASK;
1229 release_sock(sk);
1230}
1231
1232static long l2cap_sock_get_sndtimeo_cb(struct l2cap_chan *chan)
1233{
1234 struct sock *sk = chan->data;
1235
1236 return sk->sk_sndtimeo;
1103} 1237}
1104 1238
1105static struct l2cap_ops l2cap_chan_ops = { 1239static struct l2cap_ops l2cap_chan_ops = {
@@ -1111,6 +1245,9 @@ static struct l2cap_ops l2cap_chan_ops = {
1111 .state_change = l2cap_sock_state_change_cb, 1245 .state_change = l2cap_sock_state_change_cb,
1112 .ready = l2cap_sock_ready_cb, 1246 .ready = l2cap_sock_ready_cb,
1113 .defer = l2cap_sock_defer_cb, 1247 .defer = l2cap_sock_defer_cb,
1248 .resume = l2cap_sock_resume_cb,
1249 .set_shutdown = l2cap_sock_set_shutdown_cb,
1250 .get_sndtimeo = l2cap_sock_get_sndtimeo_cb,
1114 .alloc_skb = l2cap_sock_alloc_skb_cb, 1251 .alloc_skb = l2cap_sock_alloc_skb_cb,
1115}; 1252};
1116 1253
@@ -1120,6 +1257,7 @@ static void l2cap_sock_destruct(struct sock *sk)
1120 1257
1121 if (l2cap_pi(sk)->chan) 1258 if (l2cap_pi(sk)->chan)
1122 l2cap_chan_put(l2cap_pi(sk)->chan); 1259 l2cap_chan_put(l2cap_pi(sk)->chan);
1260
1123 if (l2cap_pi(sk)->rx_busy_skb) { 1261 if (l2cap_pi(sk)->rx_busy_skb) {
1124 kfree_skb(l2cap_pi(sk)->rx_busy_skb); 1262 kfree_skb(l2cap_pi(sk)->rx_busy_skb);
1125 l2cap_pi(sk)->rx_busy_skb = NULL; 1263 l2cap_pi(sk)->rx_busy_skb = NULL;
@@ -1129,10 +1267,22 @@ static void l2cap_sock_destruct(struct sock *sk)
1129 skb_queue_purge(&sk->sk_write_queue); 1267 skb_queue_purge(&sk->sk_write_queue);
1130} 1268}
1131 1269
1270static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name,
1271 int *msg_namelen)
1272{
1273 struct sockaddr_l2 *la = (struct sockaddr_l2 *) msg_name;
1274
1275 memset(la, 0, sizeof(struct sockaddr_l2));
1276 la->l2_family = AF_BLUETOOTH;
1277 la->l2_psm = bt_cb(skb)->psm;
1278 bacpy(&la->l2_bdaddr, &bt_cb(skb)->bdaddr);
1279
1280 *msg_namelen = sizeof(struct sockaddr_l2);
1281}
1282
1132static void l2cap_sock_init(struct sock *sk, struct sock *parent) 1283static void l2cap_sock_init(struct sock *sk, struct sock *parent)
1133{ 1284{
1134 struct l2cap_pinfo *pi = l2cap_pi(sk); 1285 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
1135 struct l2cap_chan *chan = pi->chan;
1136 1286
1137 BT_DBG("sk %p", sk); 1287 BT_DBG("sk %p", sk);
1138 1288
@@ -1156,13 +1306,13 @@ static void l2cap_sock_init(struct sock *sk, struct sock *parent)
1156 1306
1157 security_sk_clone(parent, sk); 1307 security_sk_clone(parent, sk);
1158 } else { 1308 } else {
1159
1160 switch (sk->sk_type) { 1309 switch (sk->sk_type) {
1161 case SOCK_RAW: 1310 case SOCK_RAW:
1162 chan->chan_type = L2CAP_CHAN_RAW; 1311 chan->chan_type = L2CAP_CHAN_RAW;
1163 break; 1312 break;
1164 case SOCK_DGRAM: 1313 case SOCK_DGRAM:
1165 chan->chan_type = L2CAP_CHAN_CONN_LESS; 1314 chan->chan_type = L2CAP_CHAN_CONN_LESS;
1315 bt_sk(sk)->skb_msg_name = l2cap_skb_msg_name;
1166 break; 1316 break;
1167 case SOCK_SEQPACKET: 1317 case SOCK_SEQPACKET:
1168 case SOCK_STREAM: 1318 case SOCK_STREAM:
@@ -1224,8 +1374,6 @@ static struct sock *l2cap_sock_alloc(struct net *net, struct socket *sock,
1224 1374
1225 l2cap_chan_hold(chan); 1375 l2cap_chan_hold(chan);
1226 1376
1227 chan->sk = sk;
1228
1229 l2cap_pi(sk)->chan = chan; 1377 l2cap_pi(sk)->chan = chan;
1230 1378
1231 return sk; 1379 return sk;
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index fedc5399d465..074d83690a41 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -30,12 +30,11 @@
30#include <net/bluetooth/bluetooth.h> 30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h> 31#include <net/bluetooth/hci_core.h>
32#include <net/bluetooth/mgmt.h> 32#include <net/bluetooth/mgmt.h>
33#include <net/bluetooth/smp.h>
34 33
35bool enable_hs; 34#include "smp.h"
36 35
37#define MGMT_VERSION 1 36#define MGMT_VERSION 1
38#define MGMT_REVISION 3 37#define MGMT_REVISION 4
39 38
40static const u16 mgmt_commands[] = { 39static const u16 mgmt_commands[] = {
41 MGMT_OP_READ_INDEX_LIST, 40 MGMT_OP_READ_INDEX_LIST,
@@ -76,6 +75,10 @@ static const u16 mgmt_commands[] = {
76 MGMT_OP_BLOCK_DEVICE, 75 MGMT_OP_BLOCK_DEVICE,
77 MGMT_OP_UNBLOCK_DEVICE, 76 MGMT_OP_UNBLOCK_DEVICE,
78 MGMT_OP_SET_DEVICE_ID, 77 MGMT_OP_SET_DEVICE_ID,
78 MGMT_OP_SET_ADVERTISING,
79 MGMT_OP_SET_BREDR,
80 MGMT_OP_SET_STATIC_ADDRESS,
81 MGMT_OP_SET_SCAN_PARAMS,
79}; 82};
80 83
81static const u16 mgmt_events[] = { 84static const u16 mgmt_events[] = {
@@ -181,11 +184,6 @@ static u8 mgmt_status_table[] = {
181 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */ 184 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
182}; 185};
183 186
184bool mgmt_valid_hdev(struct hci_dev *hdev)
185{
186 return hdev->dev_type == HCI_BREDR;
187}
188
189static u8 mgmt_status(u8 hci_status) 187static u8 mgmt_status(u8 hci_status)
190{ 188{
191 if (hci_status < ARRAY_SIZE(mgmt_status_table)) 189 if (hci_status < ARRAY_SIZE(mgmt_status_table))
@@ -321,10 +319,8 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
321 319
322 count = 0; 320 count = 0;
323 list_for_each_entry(d, &hci_dev_list, list) { 321 list_for_each_entry(d, &hci_dev_list, list) {
324 if (!mgmt_valid_hdev(d)) 322 if (d->dev_type == HCI_BREDR)
325 continue; 323 count++;
326
327 count++;
328 } 324 }
329 325
330 rp_len = sizeof(*rp) + (2 * count); 326 rp_len = sizeof(*rp) + (2 * count);
@@ -339,11 +335,13 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
339 if (test_bit(HCI_SETUP, &d->dev_flags)) 335 if (test_bit(HCI_SETUP, &d->dev_flags))
340 continue; 336 continue;
341 337
342 if (!mgmt_valid_hdev(d)) 338 if (test_bit(HCI_USER_CHANNEL, &d->dev_flags))
343 continue; 339 continue;
344 340
345 rp->index[count++] = cpu_to_le16(d->id); 341 if (d->dev_type == HCI_BREDR) {
346 BT_DBG("Added hci%u", d->id); 342 rp->index[count++] = cpu_to_le16(d->id);
343 BT_DBG("Added hci%u", d->id);
344 }
347 } 345 }
348 346
349 rp->num_controllers = cpu_to_le16(count); 347 rp->num_controllers = cpu_to_le16(count);
@@ -366,9 +364,6 @@ static u32 get_supported_settings(struct hci_dev *hdev)
366 settings |= MGMT_SETTING_POWERED; 364 settings |= MGMT_SETTING_POWERED;
367 settings |= MGMT_SETTING_PAIRABLE; 365 settings |= MGMT_SETTING_PAIRABLE;
368 366
369 if (lmp_ssp_capable(hdev))
370 settings |= MGMT_SETTING_SSP;
371
372 if (lmp_bredr_capable(hdev)) { 367 if (lmp_bredr_capable(hdev)) {
373 settings |= MGMT_SETTING_CONNECTABLE; 368 settings |= MGMT_SETTING_CONNECTABLE;
374 if (hdev->hci_ver >= BLUETOOTH_VER_1_2) 369 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
@@ -376,13 +371,17 @@ static u32 get_supported_settings(struct hci_dev *hdev)
376 settings |= MGMT_SETTING_DISCOVERABLE; 371 settings |= MGMT_SETTING_DISCOVERABLE;
377 settings |= MGMT_SETTING_BREDR; 372 settings |= MGMT_SETTING_BREDR;
378 settings |= MGMT_SETTING_LINK_SECURITY; 373 settings |= MGMT_SETTING_LINK_SECURITY;
379 }
380 374
381 if (enable_hs) 375 if (lmp_ssp_capable(hdev)) {
382 settings |= MGMT_SETTING_HS; 376 settings |= MGMT_SETTING_SSP;
377 settings |= MGMT_SETTING_HS;
378 }
379 }
383 380
384 if (lmp_le_capable(hdev)) 381 if (lmp_le_capable(hdev)) {
385 settings |= MGMT_SETTING_LE; 382 settings |= MGMT_SETTING_LE;
383 settings |= MGMT_SETTING_ADVERTISING;
384 }
386 385
387 return settings; 386 return settings;
388} 387}
@@ -406,7 +405,7 @@ static u32 get_current_settings(struct hci_dev *hdev)
406 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags)) 405 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags))
407 settings |= MGMT_SETTING_PAIRABLE; 406 settings |= MGMT_SETTING_PAIRABLE;
408 407
409 if (lmp_bredr_capable(hdev)) 408 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
410 settings |= MGMT_SETTING_BREDR; 409 settings |= MGMT_SETTING_BREDR;
411 410
412 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 411 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
@@ -421,6 +420,9 @@ static u32 get_current_settings(struct hci_dev *hdev)
421 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) 420 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags))
422 settings |= MGMT_SETTING_HS; 421 settings |= MGMT_SETTING_HS;
423 422
423 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
424 settings |= MGMT_SETTING_ADVERTISING;
425
424 return settings; 426 return settings;
425} 427}
426 428
@@ -534,6 +536,156 @@ static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
534 return ptr; 536 return ptr;
535} 537}
536 538
539static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
540{
541 struct pending_cmd *cmd;
542
543 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
544 if (cmd->opcode == opcode)
545 return cmd;
546 }
547
548 return NULL;
549}
550
551static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
552{
553 u8 ad_len = 0;
554 size_t name_len;
555
556 name_len = strlen(hdev->dev_name);
557 if (name_len > 0) {
558 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
559
560 if (name_len > max_len) {
561 name_len = max_len;
562 ptr[1] = EIR_NAME_SHORT;
563 } else
564 ptr[1] = EIR_NAME_COMPLETE;
565
566 ptr[0] = name_len + 1;
567
568 memcpy(ptr + 2, hdev->dev_name, name_len);
569
570 ad_len += (name_len + 2);
571 ptr += (name_len + 2);
572 }
573
574 return ad_len;
575}
576
577static void update_scan_rsp_data(struct hci_request *req)
578{
579 struct hci_dev *hdev = req->hdev;
580 struct hci_cp_le_set_scan_rsp_data cp;
581 u8 len;
582
583 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
584 return;
585
586 memset(&cp, 0, sizeof(cp));
587
588 len = create_scan_rsp_data(hdev, cp.data);
589
590 if (hdev->scan_rsp_data_len == len &&
591 memcmp(cp.data, hdev->scan_rsp_data, len) == 0)
592 return;
593
594 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
595 hdev->scan_rsp_data_len = len;
596
597 cp.length = len;
598
599 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
600}
601
602static u8 get_adv_discov_flags(struct hci_dev *hdev)
603{
604 struct pending_cmd *cmd;
605
606 /* If there's a pending mgmt command the flags will not yet have
607 * their final values, so check for this first.
608 */
609 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
610 if (cmd) {
611 struct mgmt_mode *cp = cmd->param;
612 if (cp->val == 0x01)
613 return LE_AD_GENERAL;
614 else if (cp->val == 0x02)
615 return LE_AD_LIMITED;
616 } else {
617 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
618 return LE_AD_LIMITED;
619 else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
620 return LE_AD_GENERAL;
621 }
622
623 return 0;
624}
625
626static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
627{
628 u8 ad_len = 0, flags = 0;
629
630 flags |= get_adv_discov_flags(hdev);
631
632 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
633 if (lmp_le_br_capable(hdev))
634 flags |= LE_AD_SIM_LE_BREDR_CTRL;
635 if (lmp_host_le_br_capable(hdev))
636 flags |= LE_AD_SIM_LE_BREDR_HOST;
637 } else {
638 flags |= LE_AD_NO_BREDR;
639 }
640
641 if (flags) {
642 BT_DBG("adv flags 0x%02x", flags);
643
644 ptr[0] = 2;
645 ptr[1] = EIR_FLAGS;
646 ptr[2] = flags;
647
648 ad_len += 3;
649 ptr += 3;
650 }
651
652 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
653 ptr[0] = 2;
654 ptr[1] = EIR_TX_POWER;
655 ptr[2] = (u8) hdev->adv_tx_power;
656
657 ad_len += 3;
658 ptr += 3;
659 }
660
661 return ad_len;
662}
663
664static void update_adv_data(struct hci_request *req)
665{
666 struct hci_dev *hdev = req->hdev;
667 struct hci_cp_le_set_adv_data cp;
668 u8 len;
669
670 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
671 return;
672
673 memset(&cp, 0, sizeof(cp));
674
675 len = create_adv_data(hdev, cp.data);
676
677 if (hdev->adv_data_len == len &&
678 memcmp(cp.data, hdev->adv_data, len) == 0)
679 return;
680
681 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
682 hdev->adv_data_len = len;
683
684 cp.length = len;
685
686 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
687}
688
537static void create_eir(struct hci_dev *hdev, u8 *data) 689static void create_eir(struct hci_dev *hdev, u8 *data)
538{ 690{
539 u8 *ptr = data; 691 u8 *ptr = data;
@@ -632,6 +784,9 @@ static void update_class(struct hci_request *req)
632 if (!hdev_is_powered(hdev)) 784 if (!hdev_is_powered(hdev))
633 return; 785 return;
634 786
787 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
788 return;
789
635 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 790 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags))
636 return; 791 return;
637 792
@@ -639,6 +794,9 @@ static void update_class(struct hci_request *req)
639 cod[1] = hdev->major_class; 794 cod[1] = hdev->major_class;
640 cod[2] = get_service_classes(hdev); 795 cod[2] = get_service_classes(hdev);
641 796
797 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags))
798 cod[1] |= 0x20;
799
642 if (memcmp(cod, hdev->dev_class, 3) == 0) 800 if (memcmp(cod, hdev->dev_class, 3) == 0)
643 return; 801 return;
644 802
@@ -763,18 +921,6 @@ static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
763 } 921 }
764} 922}
765 923
766static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev)
767{
768 struct pending_cmd *cmd;
769
770 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
771 if (cmd->opcode == opcode)
772 return cmd;
773 }
774
775 return NULL;
776}
777
778static void mgmt_pending_remove(struct pending_cmd *cmd) 924static void mgmt_pending_remove(struct pending_cmd *cmd)
779{ 925{
780 list_del(&cmd->list); 926 list_del(&cmd->list);
@@ -804,6 +950,12 @@ static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
804 950
805 hci_dev_lock(hdev); 951 hci_dev_lock(hdev);
806 952
953 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
954 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
955 MGMT_STATUS_BUSY);
956 goto failed;
957 }
958
807 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 959 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) {
808 cancel_delayed_work(&hdev->power_off); 960 cancel_delayed_work(&hdev->power_off);
809 961
@@ -820,12 +972,6 @@ static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
820 goto failed; 972 goto failed;
821 } 973 }
822 974
823 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) {
824 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
825 MGMT_STATUS_BUSY);
826 goto failed;
827 }
828
829 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len); 975 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
830 if (!cmd) { 976 if (!cmd) {
831 err = -ENOMEM; 977 err = -ENOMEM;
@@ -883,27 +1029,141 @@ static int new_settings(struct hci_dev *hdev, struct sock *skip)
883 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); 1029 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip);
884} 1030}
885 1031
1032struct cmd_lookup {
1033 struct sock *sk;
1034 struct hci_dev *hdev;
1035 u8 mgmt_status;
1036};
1037
1038static void settings_rsp(struct pending_cmd *cmd, void *data)
1039{
1040 struct cmd_lookup *match = data;
1041
1042 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1043
1044 list_del(&cmd->list);
1045
1046 if (match->sk == NULL) {
1047 match->sk = cmd->sk;
1048 sock_hold(match->sk);
1049 }
1050
1051 mgmt_pending_free(cmd);
1052}
1053
1054static void cmd_status_rsp(struct pending_cmd *cmd, void *data)
1055{
1056 u8 *status = data;
1057
1058 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1059 mgmt_pending_remove(cmd);
1060}
1061
1062static u8 mgmt_bredr_support(struct hci_dev *hdev)
1063{
1064 if (!lmp_bredr_capable(hdev))
1065 return MGMT_STATUS_NOT_SUPPORTED;
1066 else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1067 return MGMT_STATUS_REJECTED;
1068 else
1069 return MGMT_STATUS_SUCCESS;
1070}
1071
1072static u8 mgmt_le_support(struct hci_dev *hdev)
1073{
1074 if (!lmp_le_capable(hdev))
1075 return MGMT_STATUS_NOT_SUPPORTED;
1076 else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
1077 return MGMT_STATUS_REJECTED;
1078 else
1079 return MGMT_STATUS_SUCCESS;
1080}
1081
1082static void set_discoverable_complete(struct hci_dev *hdev, u8 status)
1083{
1084 struct pending_cmd *cmd;
1085 struct mgmt_mode *cp;
1086 struct hci_request req;
1087 bool changed;
1088
1089 BT_DBG("status 0x%02x", status);
1090
1091 hci_dev_lock(hdev);
1092
1093 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1094 if (!cmd)
1095 goto unlock;
1096
1097 if (status) {
1098 u8 mgmt_err = mgmt_status(status);
1099 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1100 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1101 goto remove_cmd;
1102 }
1103
1104 cp = cmd->param;
1105 if (cp->val) {
1106 changed = !test_and_set_bit(HCI_DISCOVERABLE,
1107 &hdev->dev_flags);
1108
1109 if (hdev->discov_timeout > 0) {
1110 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1111 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1112 to);
1113 }
1114 } else {
1115 changed = test_and_clear_bit(HCI_DISCOVERABLE,
1116 &hdev->dev_flags);
1117 }
1118
1119 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1120
1121 if (changed)
1122 new_settings(hdev, cmd->sk);
1123
1124 /* When the discoverable mode gets changed, make sure
1125 * that class of device has the limited discoverable
1126 * bit correctly set.
1127 */
1128 hci_req_init(&req, hdev);
1129 update_class(&req);
1130 hci_req_run(&req, NULL);
1131
1132remove_cmd:
1133 mgmt_pending_remove(cmd);
1134
1135unlock:
1136 hci_dev_unlock(hdev);
1137}
1138
886static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data, 1139static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
887 u16 len) 1140 u16 len)
888{ 1141{
889 struct mgmt_cp_set_discoverable *cp = data; 1142 struct mgmt_cp_set_discoverable *cp = data;
890 struct pending_cmd *cmd; 1143 struct pending_cmd *cmd;
1144 struct hci_request req;
891 u16 timeout; 1145 u16 timeout;
892 u8 scan; 1146 u8 scan;
893 int err; 1147 int err;
894 1148
895 BT_DBG("request for %s", hdev->name); 1149 BT_DBG("request for %s", hdev->name);
896 1150
897 if (!lmp_bredr_capable(hdev)) 1151 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1152 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
898 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1153 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
899 MGMT_STATUS_NOT_SUPPORTED); 1154 MGMT_STATUS_REJECTED);
900 1155
901 if (cp->val != 0x00 && cp->val != 0x01) 1156 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
902 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1157 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
903 MGMT_STATUS_INVALID_PARAMS); 1158 MGMT_STATUS_INVALID_PARAMS);
904 1159
905 timeout = __le16_to_cpu(cp->timeout); 1160 timeout = __le16_to_cpu(cp->timeout);
906 if (!cp->val && timeout > 0) 1161
1162 /* Disabling discoverable requires that no timeout is set,
1163 * and enabling limited discoverable requires a timeout.
1164 */
1165 if ((cp->val == 0x00 && timeout > 0) ||
1166 (cp->val == 0x02 && timeout == 0))
907 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1167 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
908 MGMT_STATUS_INVALID_PARAMS); 1168 MGMT_STATUS_INVALID_PARAMS);
909 1169
@@ -931,6 +1191,10 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
931 if (!hdev_is_powered(hdev)) { 1191 if (!hdev_is_powered(hdev)) {
932 bool changed = false; 1192 bool changed = false;
933 1193
1194 /* Setting limited discoverable when powered off is
1195 * not a valid operation since it requires a timeout
1196 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1197 */
934 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { 1198 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) {
935 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 1199 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
936 changed = true; 1200 changed = true;
@@ -946,16 +1210,20 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
946 goto failed; 1210 goto failed;
947 } 1211 }
948 1212
949 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { 1213 /* If the current mode is the same, then just update the timeout
950 if (hdev->discov_timeout > 0) { 1214 * value with the new value. And if only the timeout gets updated,
951 cancel_delayed_work(&hdev->discov_off); 1215 * then no need for any HCI transactions.
952 hdev->discov_timeout = 0; 1216 */
953 } 1217 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) &&
1218 (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE,
1219 &hdev->dev_flags)) {
1220 cancel_delayed_work(&hdev->discov_off);
1221 hdev->discov_timeout = timeout;
954 1222
955 if (cp->val && timeout > 0) { 1223 if (cp->val && hdev->discov_timeout > 0) {
956 hdev->discov_timeout = timeout; 1224 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
957 queue_delayed_work(hdev->workqueue, &hdev->discov_off, 1225 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
958 msecs_to_jiffies(hdev->discov_timeout * 1000)); 1226 to);
959 } 1227 }
960 1228
961 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev); 1229 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
@@ -968,20 +1236,66 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
968 goto failed; 1236 goto failed;
969 } 1237 }
970 1238
1239 /* Cancel any potential discoverable timeout that might be
1240 * still active and store new timeout value. The arming of
1241 * the timeout happens in the complete handler.
1242 */
1243 cancel_delayed_work(&hdev->discov_off);
1244 hdev->discov_timeout = timeout;
1245
1246 /* Limited discoverable mode */
1247 if (cp->val == 0x02)
1248 set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1249 else
1250 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1251
1252 hci_req_init(&req, hdev);
1253
1254 /* The procedure for LE-only controllers is much simpler - just
1255 * update the advertising data.
1256 */
1257 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1258 goto update_ad;
1259
971 scan = SCAN_PAGE; 1260 scan = SCAN_PAGE;
972 1261
973 if (cp->val) 1262 if (cp->val) {
1263 struct hci_cp_write_current_iac_lap hci_cp;
1264
1265 if (cp->val == 0x02) {
1266 /* Limited discoverable mode */
1267 hci_cp.num_iac = 2;
1268 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1269 hci_cp.iac_lap[1] = 0x8b;
1270 hci_cp.iac_lap[2] = 0x9e;
1271 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1272 hci_cp.iac_lap[4] = 0x8b;
1273 hci_cp.iac_lap[5] = 0x9e;
1274 } else {
1275 /* General discoverable mode */
1276 hci_cp.num_iac = 1;
1277 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1278 hci_cp.iac_lap[1] = 0x8b;
1279 hci_cp.iac_lap[2] = 0x9e;
1280 }
1281
1282 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1283 (hci_cp.num_iac * 3) + 1, &hci_cp);
1284
974 scan |= SCAN_INQUIRY; 1285 scan |= SCAN_INQUIRY;
975 else 1286 } else {
976 cancel_delayed_work(&hdev->discov_off); 1287 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1288 }
1289
1290 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
977 1291
978 err = hci_send_cmd(hdev, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 1292update_ad:
1293 update_adv_data(&req);
1294
1295 err = hci_req_run(&req, set_discoverable_complete);
979 if (err < 0) 1296 if (err < 0)
980 mgmt_pending_remove(cmd); 1297 mgmt_pending_remove(cmd);
981 1298
982 if (cp->val)
983 hdev->discov_timeout = timeout;
984
985failed: 1299failed:
986 hci_dev_unlock(hdev); 1300 hci_dev_unlock(hdev);
987 return err; 1301 return err;
@@ -993,6 +1307,9 @@ static void write_fast_connectable(struct hci_request *req, bool enable)
993 struct hci_cp_write_page_scan_activity acp; 1307 struct hci_cp_write_page_scan_activity acp;
994 u8 type; 1308 u8 type;
995 1309
1310 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1311 return;
1312
996 if (hdev->hci_ver < BLUETOOTH_VER_1_2) 1313 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
997 return; 1314 return;
998 1315
@@ -1019,9 +1336,55 @@ static void write_fast_connectable(struct hci_request *req, bool enable)
1019 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type); 1336 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
1020} 1337}
1021 1338
1339static u8 get_adv_type(struct hci_dev *hdev)
1340{
1341 struct pending_cmd *cmd;
1342 bool connectable;
1343
1344 /* If there's a pending mgmt command the flag will not yet have
1345 * it's final value, so check for this first.
1346 */
1347 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1348 if (cmd) {
1349 struct mgmt_mode *cp = cmd->param;
1350 connectable = !!cp->val;
1351 } else {
1352 connectable = test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1353 }
1354
1355 return connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
1356}
1357
1358static void enable_advertising(struct hci_request *req)
1359{
1360 struct hci_dev *hdev = req->hdev;
1361 struct hci_cp_le_set_adv_param cp;
1362 u8 enable = 0x01;
1363
1364 memset(&cp, 0, sizeof(cp));
1365 cp.min_interval = __constant_cpu_to_le16(0x0800);
1366 cp.max_interval = __constant_cpu_to_le16(0x0800);
1367 cp.type = get_adv_type(hdev);
1368 cp.own_address_type = hdev->own_addr_type;
1369 cp.channel_map = 0x07;
1370
1371 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1372
1373 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1374}
1375
1376static void disable_advertising(struct hci_request *req)
1377{
1378 u8 enable = 0x00;
1379
1380 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1381}
1382
1022static void set_connectable_complete(struct hci_dev *hdev, u8 status) 1383static void set_connectable_complete(struct hci_dev *hdev, u8 status)
1023{ 1384{
1024 struct pending_cmd *cmd; 1385 struct pending_cmd *cmd;
1386 struct mgmt_mode *cp;
1387 bool changed;
1025 1388
1026 BT_DBG("status 0x%02x", status); 1389 BT_DBG("status 0x%02x", status);
1027 1390
@@ -1031,14 +1394,56 @@ static void set_connectable_complete(struct hci_dev *hdev, u8 status)
1031 if (!cmd) 1394 if (!cmd)
1032 goto unlock; 1395 goto unlock;
1033 1396
1397 if (status) {
1398 u8 mgmt_err = mgmt_status(status);
1399 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1400 goto remove_cmd;
1401 }
1402
1403 cp = cmd->param;
1404 if (cp->val)
1405 changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1406 else
1407 changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1408
1034 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); 1409 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1035 1410
1411 if (changed)
1412 new_settings(hdev, cmd->sk);
1413
1414remove_cmd:
1036 mgmt_pending_remove(cmd); 1415 mgmt_pending_remove(cmd);
1037 1416
1038unlock: 1417unlock:
1039 hci_dev_unlock(hdev); 1418 hci_dev_unlock(hdev);
1040} 1419}
1041 1420
1421static int set_connectable_update_settings(struct hci_dev *hdev,
1422 struct sock *sk, u8 val)
1423{
1424 bool changed = false;
1425 int err;
1426
1427 if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
1428 changed = true;
1429
1430 if (val) {
1431 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1432 } else {
1433 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1434 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1435 }
1436
1437 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1438 if (err < 0)
1439 return err;
1440
1441 if (changed)
1442 return new_settings(hdev, sk);
1443
1444 return 0;
1445}
1446
1042static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data, 1447static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1043 u16 len) 1448 u16 len)
1044{ 1449{
@@ -1050,9 +1455,10 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1050 1455
1051 BT_DBG("request for %s", hdev->name); 1456 BT_DBG("request for %s", hdev->name);
1052 1457
1053 if (!lmp_bredr_capable(hdev)) 1458 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) &&
1459 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1054 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 1460 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1055 MGMT_STATUS_NOT_SUPPORTED); 1461 MGMT_STATUS_REJECTED);
1056 1462
1057 if (cp->val != 0x00 && cp->val != 0x01) 1463 if (cp->val != 0x00 && cp->val != 0x01)
1058 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 1464 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
@@ -1061,25 +1467,7 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1061 hci_dev_lock(hdev); 1467 hci_dev_lock(hdev);
1062 1468
1063 if (!hdev_is_powered(hdev)) { 1469 if (!hdev_is_powered(hdev)) {
1064 bool changed = false; 1470 err = set_connectable_update_settings(hdev, sk, cp->val);
1065
1066 if (!!cp->val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
1067 changed = true;
1068
1069 if (cp->val) {
1070 set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1071 } else {
1072 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1073 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1074 }
1075
1076 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1077 if (err < 0)
1078 goto failed;
1079
1080 if (changed)
1081 err = new_settings(hdev, sk);
1082
1083 goto failed; 1471 goto failed;
1084 } 1472 }
1085 1473
@@ -1090,30 +1478,37 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1090 goto failed; 1478 goto failed;
1091 } 1479 }
1092 1480
1093 if (!!cp->val == test_bit(HCI_PSCAN, &hdev->flags)) {
1094 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1095 goto failed;
1096 }
1097
1098 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len); 1481 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1099 if (!cmd) { 1482 if (!cmd) {
1100 err = -ENOMEM; 1483 err = -ENOMEM;
1101 goto failed; 1484 goto failed;
1102 } 1485 }
1103 1486
1104 if (cp->val) { 1487 hci_req_init(&req, hdev);
1105 scan = SCAN_PAGE;
1106 } else {
1107 scan = 0;
1108 1488
1109 if (test_bit(HCI_ISCAN, &hdev->flags) && 1489 /* If BR/EDR is not enabled and we disable advertising as a
1110 hdev->discov_timeout > 0) 1490 * by-product of disabling connectable, we need to update the
1111 cancel_delayed_work(&hdev->discov_off); 1491 * advertising flags.
1112 } 1492 */
1493 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
1494 if (!cp->val) {
1495 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
1496 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
1497 }
1498 update_adv_data(&req);
1499 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
1500 if (cp->val) {
1501 scan = SCAN_PAGE;
1502 } else {
1503 scan = 0;
1113 1504
1114 hci_req_init(&req, hdev); 1505 if (test_bit(HCI_ISCAN, &hdev->flags) &&
1506 hdev->discov_timeout > 0)
1507 cancel_delayed_work(&hdev->discov_off);
1508 }
1115 1509
1116 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 1510 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1511 }
1117 1512
1118 /* If we're going from non-connectable to connectable or 1513 /* If we're going from non-connectable to connectable or
1119 * vice-versa when fast connectable is enabled ensure that fast 1514 * vice-versa when fast connectable is enabled ensure that fast
@@ -1124,9 +1519,20 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1124 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) 1519 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
1125 write_fast_connectable(&req, false); 1520 write_fast_connectable(&req, false);
1126 1521
1522 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags) &&
1523 hci_conn_num(hdev, LE_LINK) == 0) {
1524 disable_advertising(&req);
1525 enable_advertising(&req);
1526 }
1527
1127 err = hci_req_run(&req, set_connectable_complete); 1528 err = hci_req_run(&req, set_connectable_complete);
1128 if (err < 0) 1529 if (err < 0) {
1129 mgmt_pending_remove(cmd); 1530 mgmt_pending_remove(cmd);
1531 if (err == -ENODATA)
1532 err = set_connectable_update_settings(hdev, sk,
1533 cp->val);
1534 goto failed;
1535 }
1130 1536
1131failed: 1537failed:
1132 hci_dev_unlock(hdev); 1538 hci_dev_unlock(hdev);
@@ -1137,6 +1543,7 @@ static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data,
1137 u16 len) 1543 u16 len)
1138{ 1544{
1139 struct mgmt_mode *cp = data; 1545 struct mgmt_mode *cp = data;
1546 bool changed;
1140 int err; 1547 int err;
1141 1548
1142 BT_DBG("request for %s", hdev->name); 1549 BT_DBG("request for %s", hdev->name);
@@ -1148,17 +1555,18 @@ static int set_pairable(struct sock *sk, struct hci_dev *hdev, void *data,
1148 hci_dev_lock(hdev); 1555 hci_dev_lock(hdev);
1149 1556
1150 if (cp->val) 1557 if (cp->val)
1151 set_bit(HCI_PAIRABLE, &hdev->dev_flags); 1558 changed = !test_and_set_bit(HCI_PAIRABLE, &hdev->dev_flags);
1152 else 1559 else
1153 clear_bit(HCI_PAIRABLE, &hdev->dev_flags); 1560 changed = test_and_clear_bit(HCI_PAIRABLE, &hdev->dev_flags);
1154 1561
1155 err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev); 1562 err = send_settings_rsp(sk, MGMT_OP_SET_PAIRABLE, hdev);
1156 if (err < 0) 1563 if (err < 0)
1157 goto failed; 1564 goto unlock;
1158 1565
1159 err = new_settings(hdev, sk); 1566 if (changed)
1567 err = new_settings(hdev, sk);
1160 1568
1161failed: 1569unlock:
1162 hci_dev_unlock(hdev); 1570 hci_dev_unlock(hdev);
1163 return err; 1571 return err;
1164} 1572}
@@ -1168,14 +1576,15 @@ static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1168{ 1576{
1169 struct mgmt_mode *cp = data; 1577 struct mgmt_mode *cp = data;
1170 struct pending_cmd *cmd; 1578 struct pending_cmd *cmd;
1171 u8 val; 1579 u8 val, status;
1172 int err; 1580 int err;
1173 1581
1174 BT_DBG("request for %s", hdev->name); 1582 BT_DBG("request for %s", hdev->name);
1175 1583
1176 if (!lmp_bredr_capable(hdev)) 1584 status = mgmt_bredr_support(hdev);
1585 if (status)
1177 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 1586 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1178 MGMT_STATUS_NOT_SUPPORTED); 1587 status);
1179 1588
1180 if (cp->val != 0x00 && cp->val != 0x01) 1589 if (cp->val != 0x00 && cp->val != 0x01)
1181 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 1590 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
@@ -1236,11 +1645,15 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1236{ 1645{
1237 struct mgmt_mode *cp = data; 1646 struct mgmt_mode *cp = data;
1238 struct pending_cmd *cmd; 1647 struct pending_cmd *cmd;
1239 u8 val; 1648 u8 status;
1240 int err; 1649 int err;
1241 1650
1242 BT_DBG("request for %s", hdev->name); 1651 BT_DBG("request for %s", hdev->name);
1243 1652
1653 status = mgmt_bredr_support(hdev);
1654 if (status)
1655 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
1656
1244 if (!lmp_ssp_capable(hdev)) 1657 if (!lmp_ssp_capable(hdev))
1245 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 1658 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1246 MGMT_STATUS_NOT_SUPPORTED); 1659 MGMT_STATUS_NOT_SUPPORTED);
@@ -1251,14 +1664,20 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1251 1664
1252 hci_dev_lock(hdev); 1665 hci_dev_lock(hdev);
1253 1666
1254 val = !!cp->val;
1255
1256 if (!hdev_is_powered(hdev)) { 1667 if (!hdev_is_powered(hdev)) {
1257 bool changed = false; 1668 bool changed;
1258 1669
1259 if (val != test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 1670 if (cp->val) {
1260 change_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 1671 changed = !test_and_set_bit(HCI_SSP_ENABLED,
1261 changed = true; 1672 &hdev->dev_flags);
1673 } else {
1674 changed = test_and_clear_bit(HCI_SSP_ENABLED,
1675 &hdev->dev_flags);
1676 if (!changed)
1677 changed = test_and_clear_bit(HCI_HS_ENABLED,
1678 &hdev->dev_flags);
1679 else
1680 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
1262 } 1681 }
1263 1682
1264 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 1683 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
@@ -1271,13 +1690,14 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1271 goto failed; 1690 goto failed;
1272 } 1691 }
1273 1692
1274 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev)) { 1693 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) ||
1694 mgmt_pending_find(MGMT_OP_SET_HS, hdev)) {
1275 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 1695 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1276 MGMT_STATUS_BUSY); 1696 MGMT_STATUS_BUSY);
1277 goto failed; 1697 goto failed;
1278 } 1698 }
1279 1699
1280 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) == val) { 1700 if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) {
1281 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 1701 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1282 goto failed; 1702 goto failed;
1283 } 1703 }
@@ -1288,7 +1708,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1288 goto failed; 1708 goto failed;
1289 } 1709 }
1290 1710
1291 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, sizeof(val), &val); 1711 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
1292 if (err < 0) { 1712 if (err < 0) {
1293 mgmt_pending_remove(cmd); 1713 mgmt_pending_remove(cmd);
1294 goto failed; 1714 goto failed;
@@ -1302,23 +1722,90 @@ failed:
1302static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 1722static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1303{ 1723{
1304 struct mgmt_mode *cp = data; 1724 struct mgmt_mode *cp = data;
1725 bool changed;
1726 u8 status;
1727 int err;
1305 1728
1306 BT_DBG("request for %s", hdev->name); 1729 BT_DBG("request for %s", hdev->name);
1307 1730
1308 if (!enable_hs) 1731 status = mgmt_bredr_support(hdev);
1732 if (status)
1733 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
1734
1735 if (!lmp_ssp_capable(hdev))
1309 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 1736 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1310 MGMT_STATUS_NOT_SUPPORTED); 1737 MGMT_STATUS_NOT_SUPPORTED);
1311 1738
1739 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags))
1740 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1741 MGMT_STATUS_REJECTED);
1742
1312 if (cp->val != 0x00 && cp->val != 0x01) 1743 if (cp->val != 0x00 && cp->val != 0x01)
1313 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 1744 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1314 MGMT_STATUS_INVALID_PARAMS); 1745 MGMT_STATUS_INVALID_PARAMS);
1315 1746
1316 if (cp->val) 1747 hci_dev_lock(hdev);
1317 set_bit(HCI_HS_ENABLED, &hdev->dev_flags); 1748
1318 else 1749 if (cp->val) {
1319 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 1750 changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags);
1751 } else {
1752 if (hdev_is_powered(hdev)) {
1753 err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
1754 MGMT_STATUS_REJECTED);
1755 goto unlock;
1756 }
1757
1758 changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
1759 }
1320 1760
1321 return send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); 1761 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
1762 if (err < 0)
1763 goto unlock;
1764
1765 if (changed)
1766 err = new_settings(hdev, sk);
1767
1768unlock:
1769 hci_dev_unlock(hdev);
1770 return err;
1771}
1772
1773static void le_enable_complete(struct hci_dev *hdev, u8 status)
1774{
1775 struct cmd_lookup match = { NULL, hdev };
1776
1777 if (status) {
1778 u8 mgmt_err = mgmt_status(status);
1779
1780 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
1781 &mgmt_err);
1782 return;
1783 }
1784
1785 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
1786
1787 new_settings(hdev, match.sk);
1788
1789 if (match.sk)
1790 sock_put(match.sk);
1791
1792 /* Make sure the controller has a good default for
1793 * advertising data. Restrict the update to when LE
1794 * has actually been enabled. During power on, the
1795 * update in powered_update_hci will take care of it.
1796 */
1797 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
1798 struct hci_request req;
1799
1800 hci_dev_lock(hdev);
1801
1802 hci_req_init(&req, hdev);
1803 update_adv_data(&req);
1804 update_scan_rsp_data(&req);
1805 hci_req_run(&req, NULL);
1806
1807 hci_dev_unlock(hdev);
1808 }
1322} 1809}
1323 1810
1324static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 1811static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
@@ -1326,6 +1813,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1326 struct mgmt_mode *cp = data; 1813 struct mgmt_mode *cp = data;
1327 struct hci_cp_write_le_host_supported hci_cp; 1814 struct hci_cp_write_le_host_supported hci_cp;
1328 struct pending_cmd *cmd; 1815 struct pending_cmd *cmd;
1816 struct hci_request req;
1329 int err; 1817 int err;
1330 u8 val, enabled; 1818 u8 val, enabled;
1331 1819
@@ -1340,7 +1828,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1340 MGMT_STATUS_INVALID_PARAMS); 1828 MGMT_STATUS_INVALID_PARAMS);
1341 1829
1342 /* LE-only devices do not allow toggling LE on/off */ 1830 /* LE-only devices do not allow toggling LE on/off */
1343 if (!lmp_bredr_capable(hdev)) 1831 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
1344 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 1832 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1345 MGMT_STATUS_REJECTED); 1833 MGMT_STATUS_REJECTED);
1346 1834
@@ -1357,6 +1845,11 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1357 changed = true; 1845 changed = true;
1358 } 1846 }
1359 1847
1848 if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
1849 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
1850 changed = true;
1851 }
1852
1360 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev); 1853 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
1361 if (err < 0) 1854 if (err < 0)
1362 goto unlock; 1855 goto unlock;
@@ -1367,7 +1860,8 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1367 goto unlock; 1860 goto unlock;
1368 } 1861 }
1369 1862
1370 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { 1863 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) ||
1864 mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
1371 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 1865 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
1372 MGMT_STATUS_BUSY); 1866 MGMT_STATUS_BUSY);
1373 goto unlock; 1867 goto unlock;
@@ -1379,15 +1873,22 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1379 goto unlock; 1873 goto unlock;
1380 } 1874 }
1381 1875
1876 hci_req_init(&req, hdev);
1877
1382 memset(&hci_cp, 0, sizeof(hci_cp)); 1878 memset(&hci_cp, 0, sizeof(hci_cp));
1383 1879
1384 if (val) { 1880 if (val) {
1385 hci_cp.le = val; 1881 hci_cp.le = val;
1386 hci_cp.simul = lmp_le_br_capable(hdev); 1882 hci_cp.simul = lmp_le_br_capable(hdev);
1883 } else {
1884 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
1885 disable_advertising(&req);
1387 } 1886 }
1388 1887
1389 err = hci_send_cmd(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp), 1888 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
1390 &hci_cp); 1889 &hci_cp);
1890
1891 err = hci_req_run(&req, le_enable_complete);
1391 if (err < 0) 1892 if (err < 0)
1392 mgmt_pending_remove(cmd); 1893 mgmt_pending_remove(cmd);
1393 1894
@@ -1706,6 +2207,12 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
1706 u16 key_count, expected_len; 2207 u16 key_count, expected_len;
1707 int i; 2208 int i;
1708 2209
2210 BT_DBG("request for %s", hdev->name);
2211
2212 if (!lmp_bredr_capable(hdev))
2213 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2214 MGMT_STATUS_NOT_SUPPORTED);
2215
1709 key_count = __le16_to_cpu(cp->key_count); 2216 key_count = __le16_to_cpu(cp->key_count);
1710 2217
1711 expected_len = sizeof(*cp) + key_count * 2218 expected_len = sizeof(*cp) + key_count *
@@ -2515,8 +3022,11 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
2515 update_eir(&req); 3022 update_eir(&req);
2516 } 3023 }
2517 3024
3025 /* The name is stored in the scan response data and so
3026 * no need to udpate the advertising data here.
3027 */
2518 if (lmp_le_capable(hdev)) 3028 if (lmp_le_capable(hdev))
2519 hci_update_ad(&req); 3029 update_scan_rsp_data(&req);
2520 3030
2521 err = hci_req_run(&req, set_name_complete); 3031 err = hci_req_run(&req, set_name_complete);
2522 if (err < 0) 3032 if (err < 0)
@@ -2685,6 +3195,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
2685 struct hci_request req; 3195 struct hci_request req;
2686 /* General inquiry access code (GIAC) */ 3196 /* General inquiry access code (GIAC) */
2687 u8 lap[3] = { 0x33, 0x8b, 0x9e }; 3197 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3198 u8 status;
2688 int err; 3199 int err;
2689 3200
2690 BT_DBG("%s", hdev->name); 3201 BT_DBG("%s", hdev->name);
@@ -2721,9 +3232,10 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
2721 3232
2722 switch (hdev->discovery.type) { 3233 switch (hdev->discovery.type) {
2723 case DISCOV_TYPE_BREDR: 3234 case DISCOV_TYPE_BREDR:
2724 if (!lmp_bredr_capable(hdev)) { 3235 status = mgmt_bredr_support(hdev);
3236 if (status) {
2725 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, 3237 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
2726 MGMT_STATUS_NOT_SUPPORTED); 3238 status);
2727 mgmt_pending_remove(cmd); 3239 mgmt_pending_remove(cmd);
2728 goto failed; 3240 goto failed;
2729 } 3241 }
@@ -2745,22 +3257,23 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
2745 3257
2746 case DISCOV_TYPE_LE: 3258 case DISCOV_TYPE_LE:
2747 case DISCOV_TYPE_INTERLEAVED: 3259 case DISCOV_TYPE_INTERLEAVED:
2748 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 3260 status = mgmt_le_support(hdev);
3261 if (status) {
2749 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, 3262 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
2750 MGMT_STATUS_NOT_SUPPORTED); 3263 status);
2751 mgmt_pending_remove(cmd); 3264 mgmt_pending_remove(cmd);
2752 goto failed; 3265 goto failed;
2753 } 3266 }
2754 3267
2755 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && 3268 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED &&
2756 !lmp_bredr_capable(hdev)) { 3269 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
2757 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, 3270 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
2758 MGMT_STATUS_NOT_SUPPORTED); 3271 MGMT_STATUS_NOT_SUPPORTED);
2759 mgmt_pending_remove(cmd); 3272 mgmt_pending_remove(cmd);
2760 goto failed; 3273 goto failed;
2761 } 3274 }
2762 3275
2763 if (test_bit(HCI_LE_PERIPHERAL, &hdev->dev_flags)) { 3276 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
2764 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY, 3277 err = cmd_status(sk, hdev->id, MGMT_OP_START_DISCOVERY,
2765 MGMT_STATUS_REJECTED); 3278 MGMT_STATUS_REJECTED);
2766 mgmt_pending_remove(cmd); 3279 mgmt_pending_remove(cmd);
@@ -2778,6 +3291,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
2778 param_cp.type = LE_SCAN_ACTIVE; 3291 param_cp.type = LE_SCAN_ACTIVE;
2779 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); 3292 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT);
2780 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); 3293 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
3294 param_cp.own_address_type = hdev->own_addr_type;
2781 hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), 3295 hci_req_add(&req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
2782 &param_cp); 3296 &param_cp);
2783 3297
@@ -3065,6 +3579,186 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
3065 return err; 3579 return err;
3066} 3580}
3067 3581
3582static void set_advertising_complete(struct hci_dev *hdev, u8 status)
3583{
3584 struct cmd_lookup match = { NULL, hdev };
3585
3586 if (status) {
3587 u8 mgmt_err = mgmt_status(status);
3588
3589 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
3590 cmd_status_rsp, &mgmt_err);
3591 return;
3592 }
3593
3594 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
3595 &match);
3596
3597 new_settings(hdev, match.sk);
3598
3599 if (match.sk)
3600 sock_put(match.sk);
3601}
3602
3603static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
3604 u16 len)
3605{
3606 struct mgmt_mode *cp = data;
3607 struct pending_cmd *cmd;
3608 struct hci_request req;
3609 u8 val, enabled, status;
3610 int err;
3611
3612 BT_DBG("request for %s", hdev->name);
3613
3614 status = mgmt_le_support(hdev);
3615 if (status)
3616 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3617 status);
3618
3619 if (cp->val != 0x00 && cp->val != 0x01)
3620 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3621 MGMT_STATUS_INVALID_PARAMS);
3622
3623 hci_dev_lock(hdev);
3624
3625 val = !!cp->val;
3626 enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
3627
3628 /* The following conditions are ones which mean that we should
3629 * not do any HCI communication but directly send a mgmt
3630 * response to user space (after toggling the flag if
3631 * necessary).
3632 */
3633 if (!hdev_is_powered(hdev) || val == enabled ||
3634 hci_conn_num(hdev, LE_LINK) > 0) {
3635 bool changed = false;
3636
3637 if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) {
3638 change_bit(HCI_ADVERTISING, &hdev->dev_flags);
3639 changed = true;
3640 }
3641
3642 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
3643 if (err < 0)
3644 goto unlock;
3645
3646 if (changed)
3647 err = new_settings(hdev, sk);
3648
3649 goto unlock;
3650 }
3651
3652 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
3653 mgmt_pending_find(MGMT_OP_SET_LE, hdev)) {
3654 err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
3655 MGMT_STATUS_BUSY);
3656 goto unlock;
3657 }
3658
3659 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
3660 if (!cmd) {
3661 err = -ENOMEM;
3662 goto unlock;
3663 }
3664
3665 hci_req_init(&req, hdev);
3666
3667 if (val)
3668 enable_advertising(&req);
3669 else
3670 disable_advertising(&req);
3671
3672 err = hci_req_run(&req, set_advertising_complete);
3673 if (err < 0)
3674 mgmt_pending_remove(cmd);
3675
3676unlock:
3677 hci_dev_unlock(hdev);
3678 return err;
3679}
3680
3681static int set_static_address(struct sock *sk, struct hci_dev *hdev,
3682 void *data, u16 len)
3683{
3684 struct mgmt_cp_set_static_address *cp = data;
3685 int err;
3686
3687 BT_DBG("%s", hdev->name);
3688
3689 if (!lmp_le_capable(hdev))
3690 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
3691 MGMT_STATUS_NOT_SUPPORTED);
3692
3693 if (hdev_is_powered(hdev))
3694 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
3695 MGMT_STATUS_REJECTED);
3696
3697 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
3698 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
3699 return cmd_status(sk, hdev->id,
3700 MGMT_OP_SET_STATIC_ADDRESS,
3701 MGMT_STATUS_INVALID_PARAMS);
3702
3703 /* Two most significant bits shall be set */
3704 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
3705 return cmd_status(sk, hdev->id,
3706 MGMT_OP_SET_STATIC_ADDRESS,
3707 MGMT_STATUS_INVALID_PARAMS);
3708 }
3709
3710 hci_dev_lock(hdev);
3711
3712 bacpy(&hdev->static_addr, &cp->bdaddr);
3713
3714 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0);
3715
3716 hci_dev_unlock(hdev);
3717
3718 return err;
3719}
3720
3721static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
3722 void *data, u16 len)
3723{
3724 struct mgmt_cp_set_scan_params *cp = data;
3725 __u16 interval, window;
3726 int err;
3727
3728 BT_DBG("%s", hdev->name);
3729
3730 if (!lmp_le_capable(hdev))
3731 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3732 MGMT_STATUS_NOT_SUPPORTED);
3733
3734 interval = __le16_to_cpu(cp->interval);
3735
3736 if (interval < 0x0004 || interval > 0x4000)
3737 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3738 MGMT_STATUS_INVALID_PARAMS);
3739
3740 window = __le16_to_cpu(cp->window);
3741
3742 if (window < 0x0004 || window > 0x4000)
3743 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3744 MGMT_STATUS_INVALID_PARAMS);
3745
3746 if (window > interval)
3747 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
3748 MGMT_STATUS_INVALID_PARAMS);
3749
3750 hci_dev_lock(hdev);
3751
3752 hdev->le_scan_interval = interval;
3753 hdev->le_scan_window = window;
3754
3755 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0);
3756
3757 hci_dev_unlock(hdev);
3758
3759 return err;
3760}
3761
3068static void fast_connectable_complete(struct hci_dev *hdev, u8 status) 3762static void fast_connectable_complete(struct hci_dev *hdev, u8 status)
3069{ 3763{
3070 struct pending_cmd *cmd; 3764 struct pending_cmd *cmd;
@@ -3108,7 +3802,8 @@ static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
3108 3802
3109 BT_DBG("%s", hdev->name); 3803 BT_DBG("%s", hdev->name);
3110 3804
3111 if (!lmp_bredr_capable(hdev) || hdev->hci_ver < BLUETOOTH_VER_1_2) 3805 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) ||
3806 hdev->hci_ver < BLUETOOTH_VER_1_2)
3112 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 3807 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
3113 MGMT_STATUS_NOT_SUPPORTED); 3808 MGMT_STATUS_NOT_SUPPORTED);
3114 3809
@@ -3162,6 +3857,148 @@ unlock:
3162 return err; 3857 return err;
3163} 3858}
3164 3859
3860static void set_bredr_scan(struct hci_request *req)
3861{
3862 struct hci_dev *hdev = req->hdev;
3863 u8 scan = 0;
3864
3865 /* Ensure that fast connectable is disabled. This function will
3866 * not do anything if the page scan parameters are already what
3867 * they should be.
3868 */
3869 write_fast_connectable(req, false);
3870
3871 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
3872 scan |= SCAN_PAGE;
3873 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
3874 scan |= SCAN_INQUIRY;
3875
3876 if (scan)
3877 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
3878}
3879
3880static void set_bredr_complete(struct hci_dev *hdev, u8 status)
3881{
3882 struct pending_cmd *cmd;
3883
3884 BT_DBG("status 0x%02x", status);
3885
3886 hci_dev_lock(hdev);
3887
3888 cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev);
3889 if (!cmd)
3890 goto unlock;
3891
3892 if (status) {
3893 u8 mgmt_err = mgmt_status(status);
3894
3895 /* We need to restore the flag if related HCI commands
3896 * failed.
3897 */
3898 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
3899
3900 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
3901 } else {
3902 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
3903 new_settings(hdev, cmd->sk);
3904 }
3905
3906 mgmt_pending_remove(cmd);
3907
3908unlock:
3909 hci_dev_unlock(hdev);
3910}
3911
3912static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
3913{
3914 struct mgmt_mode *cp = data;
3915 struct pending_cmd *cmd;
3916 struct hci_request req;
3917 int err;
3918
3919 BT_DBG("request for %s", hdev->name);
3920
3921 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
3922 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
3923 MGMT_STATUS_NOT_SUPPORTED);
3924
3925 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags))
3926 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
3927 MGMT_STATUS_REJECTED);
3928
3929 if (cp->val != 0x00 && cp->val != 0x01)
3930 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
3931 MGMT_STATUS_INVALID_PARAMS);
3932
3933 hci_dev_lock(hdev);
3934
3935 if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
3936 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
3937 goto unlock;
3938 }
3939
3940 if (!hdev_is_powered(hdev)) {
3941 if (!cp->val) {
3942 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
3943 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
3944 clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
3945 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags);
3946 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
3947 }
3948
3949 change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
3950
3951 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
3952 if (err < 0)
3953 goto unlock;
3954
3955 err = new_settings(hdev, sk);
3956 goto unlock;
3957 }
3958
3959 /* Reject disabling when powered on */
3960 if (!cp->val) {
3961 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
3962 MGMT_STATUS_REJECTED);
3963 goto unlock;
3964 }
3965
3966 if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) {
3967 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
3968 MGMT_STATUS_BUSY);
3969 goto unlock;
3970 }
3971
3972 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
3973 if (!cmd) {
3974 err = -ENOMEM;
3975 goto unlock;
3976 }
3977
3978 /* We need to flip the bit already here so that update_adv_data
3979 * generates the correct flags.
3980 */
3981 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
3982
3983 hci_req_init(&req, hdev);
3984
3985 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
3986 set_bredr_scan(&req);
3987
3988 /* Since only the advertising data flags will change, there
3989 * is no need to update the scan response data.
3990 */
3991 update_adv_data(&req);
3992
3993 err = hci_req_run(&req, set_bredr_complete);
3994 if (err < 0)
3995 mgmt_pending_remove(cmd);
3996
3997unlock:
3998 hci_dev_unlock(hdev);
3999 return err;
4000}
4001
3165static bool ltk_is_valid(struct mgmt_ltk_info *key) 4002static bool ltk_is_valid(struct mgmt_ltk_info *key)
3166{ 4003{
3167 if (key->authenticated != 0x00 && key->authenticated != 0x01) 4004 if (key->authenticated != 0x00 && key->authenticated != 0x01)
@@ -3180,6 +4017,12 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
3180 u16 key_count, expected_len; 4017 u16 key_count, expected_len;
3181 int i, err; 4018 int i, err;
3182 4019
4020 BT_DBG("request for %s", hdev->name);
4021
4022 if (!lmp_le_capable(hdev))
4023 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
4024 MGMT_STATUS_NOT_SUPPORTED);
4025
3183 key_count = __le16_to_cpu(cp->key_count); 4026 key_count = __le16_to_cpu(cp->key_count);
3184 4027
3185 expected_len = sizeof(*cp) + key_count * 4028 expected_len = sizeof(*cp) + key_count *
@@ -3208,15 +4051,19 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
3208 4051
3209 for (i = 0; i < key_count; i++) { 4052 for (i = 0; i < key_count; i++) {
3210 struct mgmt_ltk_info *key = &cp->keys[i]; 4053 struct mgmt_ltk_info *key = &cp->keys[i];
3211 u8 type; 4054 u8 type, addr_type;
4055
4056 if (key->addr.type == BDADDR_LE_PUBLIC)
4057 addr_type = ADDR_LE_DEV_PUBLIC;
4058 else
4059 addr_type = ADDR_LE_DEV_RANDOM;
3212 4060
3213 if (key->master) 4061 if (key->master)
3214 type = HCI_SMP_LTK; 4062 type = HCI_SMP_LTK;
3215 else 4063 else
3216 type = HCI_SMP_LTK_SLAVE; 4064 type = HCI_SMP_LTK_SLAVE;
3217 4065
3218 hci_add_ltk(hdev, &key->addr.bdaddr, 4066 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type,
3219 bdaddr_to_le(key->addr.type),
3220 type, 0, key->authenticated, key->val, 4067 type, 0, key->authenticated, key->val,
3221 key->enc_size, key->ediv, key->rand); 4068 key->enc_size, key->ediv, key->rand);
3222 } 4069 }
@@ -3276,6 +4123,10 @@ static const struct mgmt_handler {
3276 { block_device, false, MGMT_BLOCK_DEVICE_SIZE }, 4123 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
3277 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE }, 4124 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
3278 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE }, 4125 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
4126 { set_advertising, false, MGMT_SETTING_SIZE },
4127 { set_bredr, false, MGMT_SETTING_SIZE },
4128 { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE },
4129 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },
3279}; 4130};
3280 4131
3281 4132
@@ -3320,6 +4171,13 @@ int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen)
3320 MGMT_STATUS_INVALID_INDEX); 4171 MGMT_STATUS_INVALID_INDEX);
3321 goto done; 4172 goto done;
3322 } 4173 }
4174
4175 if (test_bit(HCI_SETUP, &hdev->dev_flags) ||
4176 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) {
4177 err = cmd_status(sk, index, opcode,
4178 MGMT_STATUS_INVALID_INDEX);
4179 goto done;
4180 }
3323 } 4181 }
3324 4182
3325 if (opcode >= ARRAY_SIZE(mgmt_handlers) || 4183 if (opcode >= ARRAY_SIZE(mgmt_handlers) ||
@@ -3365,74 +4223,24 @@ done:
3365 return err; 4223 return err;
3366} 4224}
3367 4225
3368static void cmd_status_rsp(struct pending_cmd *cmd, void *data) 4226void mgmt_index_added(struct hci_dev *hdev)
3369{
3370 u8 *status = data;
3371
3372 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
3373 mgmt_pending_remove(cmd);
3374}
3375
3376int mgmt_index_added(struct hci_dev *hdev)
3377{ 4227{
3378 if (!mgmt_valid_hdev(hdev)) 4228 if (hdev->dev_type != HCI_BREDR)
3379 return -ENOTSUPP; 4229 return;
3380 4230
3381 return mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); 4231 mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL);
3382} 4232}
3383 4233
3384int mgmt_index_removed(struct hci_dev *hdev) 4234void mgmt_index_removed(struct hci_dev *hdev)
3385{ 4235{
3386 u8 status = MGMT_STATUS_INVALID_INDEX; 4236 u8 status = MGMT_STATUS_INVALID_INDEX;
3387 4237
3388 if (!mgmt_valid_hdev(hdev)) 4238 if (hdev->dev_type != HCI_BREDR)
3389 return -ENOTSUPP; 4239 return;
3390 4240
3391 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status); 4241 mgmt_pending_foreach(0, hdev, cmd_status_rsp, &status);
3392 4242
3393 return mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL); 4243 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
3394}
3395
3396struct cmd_lookup {
3397 struct sock *sk;
3398 struct hci_dev *hdev;
3399 u8 mgmt_status;
3400};
3401
3402static void settings_rsp(struct pending_cmd *cmd, void *data)
3403{
3404 struct cmd_lookup *match = data;
3405
3406 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
3407
3408 list_del(&cmd->list);
3409
3410 if (match->sk == NULL) {
3411 match->sk = cmd->sk;
3412 sock_hold(match->sk);
3413 }
3414
3415 mgmt_pending_free(cmd);
3416}
3417
3418static void set_bredr_scan(struct hci_request *req)
3419{
3420 struct hci_dev *hdev = req->hdev;
3421 u8 scan = 0;
3422
3423 /* Ensure that fast connectable is disabled. This function will
3424 * not do anything if the page scan parameters are already what
3425 * they should be.
3426 */
3427 write_fast_connectable(req, false);
3428
3429 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
3430 scan |= SCAN_PAGE;
3431 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags))
3432 scan |= SCAN_INQUIRY;
3433
3434 if (scan)
3435 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
3436} 4244}
3437 4245
3438static void powered_complete(struct hci_dev *hdev, u8 status) 4246static void powered_complete(struct hci_dev *hdev, u8 status)
@@ -3483,13 +4291,33 @@ static int powered_update_hci(struct hci_dev *hdev)
3483 sizeof(cp), &cp); 4291 sizeof(cp), &cp);
3484 } 4292 }
3485 4293
4294 if (lmp_le_capable(hdev)) {
4295 /* Set random address to static address if configured */
4296 if (bacmp(&hdev->static_addr, BDADDR_ANY))
4297 hci_req_add(&req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
4298 &hdev->static_addr);
4299
4300 /* Make sure the controller has a good default for
4301 * advertising data. This also applies to the case
4302 * where BR/EDR was toggled during the AUTO_OFF phase.
4303 */
4304 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) {
4305 update_adv_data(&req);
4306 update_scan_rsp_data(&req);
4307 }
4308
4309 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags))
4310 enable_advertising(&req);
4311 }
4312
3486 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); 4313 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
3487 if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) 4314 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
3488 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, 4315 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
3489 sizeof(link_sec), &link_sec); 4316 sizeof(link_sec), &link_sec);
3490 4317
3491 if (lmp_bredr_capable(hdev)) { 4318 if (lmp_bredr_capable(hdev)) {
3492 set_bredr_scan(&req); 4319 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags))
4320 set_bredr_scan(&req);
3493 update_class(&req); 4321 update_class(&req);
3494 update_name(&req); 4322 update_name(&req);
3495 update_eir(&req); 4323 update_eir(&req);
@@ -3533,76 +4361,110 @@ new_settings:
3533 return err; 4361 return err;
3534} 4362}
3535 4363
3536int mgmt_set_powered_failed(struct hci_dev *hdev, int err) 4364void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
3537{ 4365{
3538 struct pending_cmd *cmd; 4366 struct pending_cmd *cmd;
3539 u8 status; 4367 u8 status;
3540 4368
3541 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); 4369 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev);
3542 if (!cmd) 4370 if (!cmd)
3543 return -ENOENT; 4371 return;
3544 4372
3545 if (err == -ERFKILL) 4373 if (err == -ERFKILL)
3546 status = MGMT_STATUS_RFKILLED; 4374 status = MGMT_STATUS_RFKILLED;
3547 else 4375 else
3548 status = MGMT_STATUS_FAILED; 4376 status = MGMT_STATUS_FAILED;
3549 4377
3550 err = cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); 4378 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
3551 4379
3552 mgmt_pending_remove(cmd); 4380 mgmt_pending_remove(cmd);
4381}
3553 4382
3554 return err; 4383void mgmt_discoverable_timeout(struct hci_dev *hdev)
4384{
4385 struct hci_request req;
4386
4387 hci_dev_lock(hdev);
4388
4389 /* When discoverable timeout triggers, then just make sure
4390 * the limited discoverable flag is cleared. Even in the case
4391 * of a timeout triggered from general discoverable, it is
4392 * safe to unconditionally clear the flag.
4393 */
4394 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
4395 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
4396
4397 hci_req_init(&req, hdev);
4398 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) {
4399 u8 scan = SCAN_PAGE;
4400 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
4401 sizeof(scan), &scan);
4402 }
4403 update_class(&req);
4404 update_adv_data(&req);
4405 hci_req_run(&req, NULL);
4406
4407 hdev->discov_timeout = 0;
4408
4409 new_settings(hdev, NULL);
4410
4411 hci_dev_unlock(hdev);
3555} 4412}
3556 4413
3557int mgmt_discoverable(struct hci_dev *hdev, u8 discoverable) 4414void mgmt_discoverable(struct hci_dev *hdev, u8 discoverable)
3558{ 4415{
3559 struct cmd_lookup match = { NULL, hdev }; 4416 bool changed;
3560 bool changed = false; 4417
3561 int err = 0; 4418 /* Nothing needed here if there's a pending command since that
4419 * commands request completion callback takes care of everything
4420 * necessary.
4421 */
4422 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev))
4423 return;
3562 4424
3563 if (discoverable) { 4425 if (discoverable) {
3564 if (!test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) 4426 changed = !test_and_set_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
3565 changed = true;
3566 } else { 4427 } else {
3567 if (test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) 4428 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags);
3568 changed = true; 4429 changed = test_and_clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags);
3569 } 4430 }
3570 4431
3571 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, settings_rsp, 4432 if (changed) {
3572 &match); 4433 struct hci_request req;
3573 4434
3574 if (changed) 4435 /* In case this change in discoverable was triggered by
3575 err = new_settings(hdev, match.sk); 4436 * a disabling of connectable there could be a need to
3576 4437 * update the advertising flags.
3577 if (match.sk) 4438 */
3578 sock_put(match.sk); 4439 hci_req_init(&req, hdev);
4440 update_adv_data(&req);
4441 hci_req_run(&req, NULL);
3579 4442
3580 return err; 4443 new_settings(hdev, NULL);
4444 }
3581} 4445}
3582 4446
3583int mgmt_connectable(struct hci_dev *hdev, u8 connectable) 4447void mgmt_connectable(struct hci_dev *hdev, u8 connectable)
3584{ 4448{
3585 struct pending_cmd *cmd; 4449 bool changed;
3586 bool changed = false;
3587 int err = 0;
3588 4450
3589 if (connectable) { 4451 /* Nothing needed here if there's a pending command since that
3590 if (!test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags)) 4452 * commands request completion callback takes care of everything
3591 changed = true; 4453 * necessary.
3592 } else { 4454 */
3593 if (test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags)) 4455 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev))
3594 changed = true; 4456 return;
3595 }
3596 4457
3597 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); 4458 if (connectable)
4459 changed = !test_and_set_bit(HCI_CONNECTABLE, &hdev->dev_flags);
4460 else
4461 changed = test_and_clear_bit(HCI_CONNECTABLE, &hdev->dev_flags);
3598 4462
3599 if (changed) 4463 if (changed)
3600 err = new_settings(hdev, cmd ? cmd->sk : NULL); 4464 new_settings(hdev, NULL);
3601
3602 return err;
3603} 4465}
3604 4466
3605int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status) 4467void mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status)
3606{ 4468{
3607 u8 mgmt_err = mgmt_status(status); 4469 u8 mgmt_err = mgmt_status(status);
3608 4470
@@ -3613,12 +4475,10 @@ int mgmt_write_scan_failed(struct hci_dev *hdev, u8 scan, u8 status)
3613 if (scan & SCAN_INQUIRY) 4475 if (scan & SCAN_INQUIRY)
3614 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev, 4476 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE, hdev,
3615 cmd_status_rsp, &mgmt_err); 4477 cmd_status_rsp, &mgmt_err);
3616
3617 return 0;
3618} 4478}
3619 4479
3620int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key, 4480void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
3621 bool persistent) 4481 bool persistent)
3622{ 4482{
3623 struct mgmt_ev_new_link_key ev; 4483 struct mgmt_ev_new_link_key ev;
3624 4484
@@ -3631,10 +4491,10 @@ int mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
3631 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE); 4491 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
3632 ev.key.pin_len = key->pin_len; 4492 ev.key.pin_len = key->pin_len;
3633 4493
3634 return mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL); 4494 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
3635} 4495}
3636 4496
3637int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent) 4497void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent)
3638{ 4498{
3639 struct mgmt_ev_new_long_term_key ev; 4499 struct mgmt_ev_new_long_term_key ev;
3640 4500
@@ -3653,13 +4513,23 @@ int mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, u8 persistent)
3653 memcpy(ev.key.rand, key->rand, sizeof(key->rand)); 4513 memcpy(ev.key.rand, key->rand, sizeof(key->rand));
3654 memcpy(ev.key.val, key->val, sizeof(key->val)); 4514 memcpy(ev.key.val, key->val, sizeof(key->val));
3655 4515
3656 return mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), 4516 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
3657 NULL); 4517}
4518
4519static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
4520 u8 data_len)
4521{
4522 eir[eir_len++] = sizeof(type) + data_len;
4523 eir[eir_len++] = type;
4524 memcpy(&eir[eir_len], data, data_len);
4525 eir_len += data_len;
4526
4527 return eir_len;
3658} 4528}
3659 4529
3660int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 4530void mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3661 u8 addr_type, u32 flags, u8 *name, u8 name_len, 4531 u8 addr_type, u32 flags, u8 *name, u8 name_len,
3662 u8 *dev_class) 4532 u8 *dev_class)
3663{ 4533{
3664 char buf[512]; 4534 char buf[512];
3665 struct mgmt_ev_device_connected *ev = (void *) buf; 4535 struct mgmt_ev_device_connected *ev = (void *) buf;
@@ -3680,8 +4550,8 @@ int mgmt_device_connected(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3680 4550
3681 ev->eir_len = cpu_to_le16(eir_len); 4551 ev->eir_len = cpu_to_le16(eir_len);
3682 4552
3683 return mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf, 4553 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
3684 sizeof(*ev) + eir_len, NULL); 4554 sizeof(*ev) + eir_len, NULL);
3685} 4555}
3686 4556
3687static void disconnect_rsp(struct pending_cmd *cmd, void *data) 4557static void disconnect_rsp(struct pending_cmd *cmd, void *data)
@@ -3719,12 +4589,11 @@ static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
3719 mgmt_pending_remove(cmd); 4589 mgmt_pending_remove(cmd);
3720} 4590}
3721 4591
3722int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr, 4592void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
3723 u8 link_type, u8 addr_type, u8 reason) 4593 u8 link_type, u8 addr_type, u8 reason)
3724{ 4594{
3725 struct mgmt_ev_device_disconnected ev; 4595 struct mgmt_ev_device_disconnected ev;
3726 struct sock *sk = NULL; 4596 struct sock *sk = NULL;
3727 int err;
3728 4597
3729 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk); 4598 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
3730 4599
@@ -3732,45 +4601,39 @@ int mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
3732 ev.addr.type = link_to_bdaddr(link_type, addr_type); 4601 ev.addr.type = link_to_bdaddr(link_type, addr_type);
3733 ev.reason = reason; 4602 ev.reason = reason;
3734 4603
3735 err = mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), 4604 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
3736 sk);
3737 4605
3738 if (sk) 4606 if (sk)
3739 sock_put(sk); 4607 sock_put(sk);
3740 4608
3741 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, 4609 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
3742 hdev); 4610 hdev);
3743
3744 return err;
3745} 4611}
3746 4612
3747int mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, 4613void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
3748 u8 link_type, u8 addr_type, u8 status) 4614 u8 link_type, u8 addr_type, u8 status)
3749{ 4615{
3750 struct mgmt_rp_disconnect rp; 4616 struct mgmt_rp_disconnect rp;
3751 struct pending_cmd *cmd; 4617 struct pending_cmd *cmd;
3752 int err;
3753 4618
3754 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, 4619 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
3755 hdev); 4620 hdev);
3756 4621
3757 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); 4622 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev);
3758 if (!cmd) 4623 if (!cmd)
3759 return -ENOENT; 4624 return;
3760 4625
3761 bacpy(&rp.addr.bdaddr, bdaddr); 4626 bacpy(&rp.addr.bdaddr, bdaddr);
3762 rp.addr.type = link_to_bdaddr(link_type, addr_type); 4627 rp.addr.type = link_to_bdaddr(link_type, addr_type);
3763 4628
3764 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT, 4629 cmd_complete(cmd->sk, cmd->index, MGMT_OP_DISCONNECT,
3765 mgmt_status(status), &rp, sizeof(rp)); 4630 mgmt_status(status), &rp, sizeof(rp));
3766 4631
3767 mgmt_pending_remove(cmd); 4632 mgmt_pending_remove(cmd);
3768
3769 return err;
3770} 4633}
3771 4634
3772int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 4635void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3773 u8 addr_type, u8 status) 4636 u8 addr_type, u8 status)
3774{ 4637{
3775 struct mgmt_ev_connect_failed ev; 4638 struct mgmt_ev_connect_failed ev;
3776 4639
@@ -3778,10 +4641,10 @@ int mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3778 ev.addr.type = link_to_bdaddr(link_type, addr_type); 4641 ev.addr.type = link_to_bdaddr(link_type, addr_type);
3779 ev.status = mgmt_status(status); 4642 ev.status = mgmt_status(status);
3780 4643
3781 return mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL); 4644 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
3782} 4645}
3783 4646
3784int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure) 4647void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
3785{ 4648{
3786 struct mgmt_ev_pin_code_request ev; 4649 struct mgmt_ev_pin_code_request ev;
3787 4650
@@ -3789,52 +4652,45 @@ int mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
3789 ev.addr.type = BDADDR_BREDR; 4652 ev.addr.type = BDADDR_BREDR;
3790 ev.secure = secure; 4653 ev.secure = secure;
3791 4654
3792 return mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), 4655 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
3793 NULL);
3794} 4656}
3795 4657
3796int mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 4658void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
3797 u8 status) 4659 u8 status)
3798{ 4660{
3799 struct pending_cmd *cmd; 4661 struct pending_cmd *cmd;
3800 struct mgmt_rp_pin_code_reply rp; 4662 struct mgmt_rp_pin_code_reply rp;
3801 int err;
3802 4663
3803 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); 4664 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
3804 if (!cmd) 4665 if (!cmd)
3805 return -ENOENT; 4666 return;
3806 4667
3807 bacpy(&rp.addr.bdaddr, bdaddr); 4668 bacpy(&rp.addr.bdaddr, bdaddr);
3808 rp.addr.type = BDADDR_BREDR; 4669 rp.addr.type = BDADDR_BREDR;
3809 4670
3810 err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 4671 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3811 mgmt_status(status), &rp, sizeof(rp)); 4672 mgmt_status(status), &rp, sizeof(rp));
3812 4673
3813 mgmt_pending_remove(cmd); 4674 mgmt_pending_remove(cmd);
3814
3815 return err;
3816} 4675}
3817 4676
3818int mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 4677void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
3819 u8 status) 4678 u8 status)
3820{ 4679{
3821 struct pending_cmd *cmd; 4680 struct pending_cmd *cmd;
3822 struct mgmt_rp_pin_code_reply rp; 4681 struct mgmt_rp_pin_code_reply rp;
3823 int err;
3824 4682
3825 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); 4683 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
3826 if (!cmd) 4684 if (!cmd)
3827 return -ENOENT; 4685 return;
3828 4686
3829 bacpy(&rp.addr.bdaddr, bdaddr); 4687 bacpy(&rp.addr.bdaddr, bdaddr);
3830 rp.addr.type = BDADDR_BREDR; 4688 rp.addr.type = BDADDR_BREDR;
3831 4689
3832 err = cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY, 4690 cmd_complete(cmd->sk, hdev->id, MGMT_OP_PIN_CODE_NEG_REPLY,
3833 mgmt_status(status), &rp, sizeof(rp)); 4691 mgmt_status(status), &rp, sizeof(rp));
3834 4692
3835 mgmt_pending_remove(cmd); 4693 mgmt_pending_remove(cmd);
3836
3837 return err;
3838} 4694}
3839 4695
3840int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr, 4696int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
@@ -3936,8 +4792,8 @@ int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
3936 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL); 4792 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
3937} 4793}
3938 4794
3939int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 4795void mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3940 u8 addr_type, u8 status) 4796 u8 addr_type, u8 status)
3941{ 4797{
3942 struct mgmt_ev_auth_failed ev; 4798 struct mgmt_ev_auth_failed ev;
3943 4799
@@ -3945,40 +4801,36 @@ int mgmt_auth_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
3945 ev.addr.type = link_to_bdaddr(link_type, addr_type); 4801 ev.addr.type = link_to_bdaddr(link_type, addr_type);
3946 ev.status = mgmt_status(status); 4802 ev.status = mgmt_status(status);
3947 4803
3948 return mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL); 4804 mgmt_event(MGMT_EV_AUTH_FAILED, hdev, &ev, sizeof(ev), NULL);
3949} 4805}
3950 4806
3951int mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status) 4807void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
3952{ 4808{
3953 struct cmd_lookup match = { NULL, hdev }; 4809 struct cmd_lookup match = { NULL, hdev };
3954 bool changed = false; 4810 bool changed;
3955 int err = 0;
3956 4811
3957 if (status) { 4812 if (status) {
3958 u8 mgmt_err = mgmt_status(status); 4813 u8 mgmt_err = mgmt_status(status);
3959 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, 4814 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
3960 cmd_status_rsp, &mgmt_err); 4815 cmd_status_rsp, &mgmt_err);
3961 return 0; 4816 return;
3962 } 4817 }
3963 4818
3964 if (test_bit(HCI_AUTH, &hdev->flags)) { 4819 if (test_bit(HCI_AUTH, &hdev->flags))
3965 if (!test_and_set_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) 4820 changed = !test_and_set_bit(HCI_LINK_SECURITY,
3966 changed = true; 4821 &hdev->dev_flags);
3967 } else { 4822 else
3968 if (test_and_clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) 4823 changed = test_and_clear_bit(HCI_LINK_SECURITY,
3969 changed = true; 4824 &hdev->dev_flags);
3970 }
3971 4825
3972 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, 4826 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
3973 &match); 4827 &match);
3974 4828
3975 if (changed) 4829 if (changed)
3976 err = new_settings(hdev, match.sk); 4830 new_settings(hdev, match.sk);
3977 4831
3978 if (match.sk) 4832 if (match.sk)
3979 sock_put(match.sk); 4833 sock_put(match.sk);
3980
3981 return err;
3982} 4834}
3983 4835
3984static void clear_eir(struct hci_request *req) 4836static void clear_eir(struct hci_request *req)
@@ -3996,38 +4848,41 @@ static void clear_eir(struct hci_request *req)
3996 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp); 4848 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
3997} 4849}
3998 4850
3999int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) 4851void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
4000{ 4852{
4001 struct cmd_lookup match = { NULL, hdev }; 4853 struct cmd_lookup match = { NULL, hdev };
4002 struct hci_request req; 4854 struct hci_request req;
4003 bool changed = false; 4855 bool changed = false;
4004 int err = 0;
4005 4856
4006 if (status) { 4857 if (status) {
4007 u8 mgmt_err = mgmt_status(status); 4858 u8 mgmt_err = mgmt_status(status);
4008 4859
4009 if (enable && test_and_clear_bit(HCI_SSP_ENABLED, 4860 if (enable && test_and_clear_bit(HCI_SSP_ENABLED,
4010 &hdev->dev_flags)) 4861 &hdev->dev_flags)) {
4011 err = new_settings(hdev, NULL); 4862 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
4863 new_settings(hdev, NULL);
4864 }
4012 4865
4013 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp, 4866 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
4014 &mgmt_err); 4867 &mgmt_err);
4015 4868 return;
4016 return err;
4017 } 4869 }
4018 4870
4019 if (enable) { 4871 if (enable) {
4020 if (!test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 4872 changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
4021 changed = true;
4022 } else { 4873 } else {
4023 if (test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 4874 changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
4024 changed = true; 4875 if (!changed)
4876 changed = test_and_clear_bit(HCI_HS_ENABLED,
4877 &hdev->dev_flags);
4878 else
4879 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags);
4025 } 4880 }
4026 4881
4027 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); 4882 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
4028 4883
4029 if (changed) 4884 if (changed)
4030 err = new_settings(hdev, match.sk); 4885 new_settings(hdev, match.sk);
4031 4886
4032 if (match.sk) 4887 if (match.sk)
4033 sock_put(match.sk); 4888 sock_put(match.sk);
@@ -4040,8 +4895,6 @@ int mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
4040 clear_eir(&req); 4895 clear_eir(&req);
4041 4896
4042 hci_req_run(&req, NULL); 4897 hci_req_run(&req, NULL);
4043
4044 return err;
4045} 4898}
4046 4899
4047static void sk_lookup(struct pending_cmd *cmd, void *data) 4900static void sk_lookup(struct pending_cmd *cmd, void *data)
@@ -4054,33 +4907,30 @@ static void sk_lookup(struct pending_cmd *cmd, void *data)
4054 } 4907 }
4055} 4908}
4056 4909
4057int mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class, 4910void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
4058 u8 status) 4911 u8 status)
4059{ 4912{
4060 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) }; 4913 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
4061 int err = 0;
4062 4914
4063 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match); 4915 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
4064 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match); 4916 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
4065 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); 4917 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
4066 4918
4067 if (!status) 4919 if (!status)
4068 err = mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 4920 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3,
4069 3, NULL); 4921 NULL);
4070 4922
4071 if (match.sk) 4923 if (match.sk)
4072 sock_put(match.sk); 4924 sock_put(match.sk);
4073
4074 return err;
4075} 4925}
4076 4926
4077int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) 4927void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
4078{ 4928{
4079 struct mgmt_cp_set_local_name ev; 4929 struct mgmt_cp_set_local_name ev;
4080 struct pending_cmd *cmd; 4930 struct pending_cmd *cmd;
4081 4931
4082 if (status) 4932 if (status)
4083 return 0; 4933 return;
4084 4934
4085 memset(&ev, 0, sizeof(ev)); 4935 memset(&ev, 0, sizeof(ev));
4086 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); 4936 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
@@ -4094,96 +4944,54 @@ int mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
4094 * HCI dev don't send any mgmt signals. 4944 * HCI dev don't send any mgmt signals.
4095 */ 4945 */
4096 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) 4946 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev))
4097 return 0; 4947 return;
4098 } 4948 }
4099 4949
4100 return mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), 4950 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
4101 cmd ? cmd->sk : NULL); 4951 cmd ? cmd->sk : NULL);
4102} 4952}
4103 4953
4104int mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash, 4954void mgmt_read_local_oob_data_reply_complete(struct hci_dev *hdev, u8 *hash,
4105 u8 *randomizer, u8 status) 4955 u8 *randomizer, u8 status)
4106{ 4956{
4107 struct pending_cmd *cmd; 4957 struct pending_cmd *cmd;
4108 int err;
4109 4958
4110 BT_DBG("%s status %u", hdev->name, status); 4959 BT_DBG("%s status %u", hdev->name, status);
4111 4960
4112 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev); 4961 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
4113 if (!cmd) 4962 if (!cmd)
4114 return -ENOENT; 4963 return;
4115 4964
4116 if (status) { 4965 if (status) {
4117 err = cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 4966 cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4118 mgmt_status(status)); 4967 mgmt_status(status));
4119 } else { 4968 } else {
4120 struct mgmt_rp_read_local_oob_data rp; 4969 struct mgmt_rp_read_local_oob_data rp;
4121 4970
4122 memcpy(rp.hash, hash, sizeof(rp.hash)); 4971 memcpy(rp.hash, hash, sizeof(rp.hash));
4123 memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer)); 4972 memcpy(rp.randomizer, randomizer, sizeof(rp.randomizer));
4124 4973
4125 err = cmd_complete(cmd->sk, hdev->id, 4974 cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4126 MGMT_OP_READ_LOCAL_OOB_DATA, 0, &rp, 4975 0, &rp, sizeof(rp));
4127 sizeof(rp));
4128 } 4976 }
4129 4977
4130 mgmt_pending_remove(cmd); 4978 mgmt_pending_remove(cmd);
4131
4132 return err;
4133} 4979}
4134 4980
4135int mgmt_le_enable_complete(struct hci_dev *hdev, u8 enable, u8 status) 4981void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
4136{ 4982 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8
4137 struct cmd_lookup match = { NULL, hdev }; 4983 ssp, u8 *eir, u16 eir_len)
4138 bool changed = false;
4139 int err = 0;
4140
4141 if (status) {
4142 u8 mgmt_err = mgmt_status(status);
4143
4144 if (enable && test_and_clear_bit(HCI_LE_ENABLED,
4145 &hdev->dev_flags))
4146 err = new_settings(hdev, NULL);
4147
4148 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
4149 &mgmt_err);
4150
4151 return err;
4152 }
4153
4154 if (enable) {
4155 if (!test_and_set_bit(HCI_LE_ENABLED, &hdev->dev_flags))
4156 changed = true;
4157 } else {
4158 if (test_and_clear_bit(HCI_LE_ENABLED, &hdev->dev_flags))
4159 changed = true;
4160 }
4161
4162 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
4163
4164 if (changed)
4165 err = new_settings(hdev, match.sk);
4166
4167 if (match.sk)
4168 sock_put(match.sk);
4169
4170 return err;
4171}
4172
4173int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
4174 u8 addr_type, u8 *dev_class, s8 rssi, u8 cfm_name, u8
4175 ssp, u8 *eir, u16 eir_len)
4176{ 4984{
4177 char buf[512]; 4985 char buf[512];
4178 struct mgmt_ev_device_found *ev = (void *) buf; 4986 struct mgmt_ev_device_found *ev = (void *) buf;
4179 size_t ev_size; 4987 size_t ev_size;
4180 4988
4181 if (!hci_discovery_active(hdev)) 4989 if (!hci_discovery_active(hdev))
4182 return -EPERM; 4990 return;
4183 4991
4184 /* Leave 5 bytes for a potential CoD field */ 4992 /* Leave 5 bytes for a potential CoD field */
4185 if (sizeof(*ev) + eir_len + 5 > sizeof(buf)) 4993 if (sizeof(*ev) + eir_len + 5 > sizeof(buf))
4186 return -EINVAL; 4994 return;
4187 4995
4188 memset(buf, 0, sizeof(buf)); 4996 memset(buf, 0, sizeof(buf));
4189 4997
@@ -4205,11 +5013,11 @@ int mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
4205 ev->eir_len = cpu_to_le16(eir_len); 5013 ev->eir_len = cpu_to_le16(eir_len);
4206 ev_size = sizeof(*ev) + eir_len; 5014 ev_size = sizeof(*ev) + eir_len;
4207 5015
4208 return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL); 5016 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
4209} 5017}
4210 5018
4211int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 5019void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
4212 u8 addr_type, s8 rssi, u8 *name, u8 name_len) 5020 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
4213{ 5021{
4214 struct mgmt_ev_device_found *ev; 5022 struct mgmt_ev_device_found *ev;
4215 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2]; 5023 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
@@ -4228,11 +5036,10 @@ int mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
4228 5036
4229 ev->eir_len = cpu_to_le16(eir_len); 5037 ev->eir_len = cpu_to_le16(eir_len);
4230 5038
4231 return mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, 5039 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
4232 sizeof(*ev) + eir_len, NULL);
4233} 5040}
4234 5041
4235int mgmt_discovering(struct hci_dev *hdev, u8 discovering) 5042void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
4236{ 5043{
4237 struct mgmt_ev_discovering ev; 5044 struct mgmt_ev_discovering ev;
4238 struct pending_cmd *cmd; 5045 struct pending_cmd *cmd;
@@ -4256,7 +5063,7 @@ int mgmt_discovering(struct hci_dev *hdev, u8 discovering)
4256 ev.type = hdev->discovery.type; 5063 ev.type = hdev->discovery.type;
4257 ev.discovering = discovering; 5064 ev.discovering = discovering;
4258 5065
4259 return mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL); 5066 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
4260} 5067}
4261 5068
4262int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type) 5069int mgmt_device_blocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
@@ -4287,5 +5094,35 @@ int mgmt_device_unblocked(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
4287 cmd ? cmd->sk : NULL); 5094 cmd ? cmd->sk : NULL);
4288} 5095}
4289 5096
4290module_param(enable_hs, bool, 0644); 5097static void adv_enable_complete(struct hci_dev *hdev, u8 status)
4291MODULE_PARM_DESC(enable_hs, "Enable High Speed support"); 5098{
5099 BT_DBG("%s status %u", hdev->name, status);
5100
5101 /* Clear the advertising mgmt setting if we failed to re-enable it */
5102 if (status) {
5103 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
5104 new_settings(hdev, NULL);
5105 }
5106}
5107
5108void mgmt_reenable_advertising(struct hci_dev *hdev)
5109{
5110 struct hci_request req;
5111
5112 if (hci_conn_num(hdev, LE_LINK) > 0)
5113 return;
5114
5115 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags))
5116 return;
5117
5118 hci_req_init(&req, hdev);
5119 enable_advertising(&req);
5120
5121 /* If this fails we have no option but to let user space know
5122 * that we've disabled advertising.
5123 */
5124 if (hci_req_run(&req, adv_enable_complete) < 0) {
5125 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
5126 new_settings(hdev, NULL);
5127 }
5128}
diff --git a/net/bluetooth/rfcomm/core.c b/net/bluetooth/rfcomm/core.c
index ca957d34b0c8..facd8a79c038 100644
--- a/net/bluetooth/rfcomm/core.c
+++ b/net/bluetooth/rfcomm/core.c
@@ -641,13 +641,13 @@ static struct rfcomm_session *rfcomm_session_get(bdaddr_t *src, bdaddr_t *dst)
641{ 641{
642 struct rfcomm_session *s; 642 struct rfcomm_session *s;
643 struct list_head *p, *n; 643 struct list_head *p, *n;
644 struct bt_sock *sk; 644 struct l2cap_chan *chan;
645 list_for_each_safe(p, n, &session_list) { 645 list_for_each_safe(p, n, &session_list) {
646 s = list_entry(p, struct rfcomm_session, list); 646 s = list_entry(p, struct rfcomm_session, list);
647 sk = bt_sk(s->sock->sk); 647 chan = l2cap_pi(s->sock->sk)->chan;
648 648
649 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&sk->src, src)) && 649 if ((!bacmp(src, BDADDR_ANY) || !bacmp(&chan->src, src)) &&
650 !bacmp(&sk->dst, dst)) 650 !bacmp(&chan->dst, dst))
651 return s; 651 return s;
652 } 652 }
653 return NULL; 653 return NULL;
@@ -694,6 +694,7 @@ static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
694 addr.l2_family = AF_BLUETOOTH; 694 addr.l2_family = AF_BLUETOOTH;
695 addr.l2_psm = 0; 695 addr.l2_psm = 0;
696 addr.l2_cid = 0; 696 addr.l2_cid = 0;
697 addr.l2_bdaddr_type = BDADDR_BREDR;
697 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr)); 698 *err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
698 if (*err < 0) 699 if (*err < 0)
699 goto failed; 700 goto failed;
@@ -719,6 +720,7 @@ static struct rfcomm_session *rfcomm_session_create(bdaddr_t *src,
719 addr.l2_family = AF_BLUETOOTH; 720 addr.l2_family = AF_BLUETOOTH;
720 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM); 721 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM);
721 addr.l2_cid = 0; 722 addr.l2_cid = 0;
723 addr.l2_bdaddr_type = BDADDR_BREDR;
722 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK); 724 *err = kernel_connect(sock, (struct sockaddr *) &addr, sizeof(addr), O_NONBLOCK);
723 if (*err == 0 || *err == -EINPROGRESS) 725 if (*err == 0 || *err == -EINPROGRESS)
724 return s; 726 return s;
@@ -732,11 +734,11 @@ failed:
732 734
733void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst) 735void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
734{ 736{
735 struct sock *sk = s->sock->sk; 737 struct l2cap_chan *chan = l2cap_pi(s->sock->sk)->chan;
736 if (src) 738 if (src)
737 bacpy(src, &bt_sk(sk)->src); 739 bacpy(src, &chan->src);
738 if (dst) 740 if (dst)
739 bacpy(dst, &bt_sk(sk)->dst); 741 bacpy(dst, &chan->dst);
740} 742}
741 743
742/* ---- RFCOMM frame sending ---- */ 744/* ---- RFCOMM frame sending ---- */
@@ -1983,6 +1985,7 @@ static int rfcomm_add_listener(bdaddr_t *ba)
1983 addr.l2_family = AF_BLUETOOTH; 1985 addr.l2_family = AF_BLUETOOTH;
1984 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM); 1986 addr.l2_psm = __constant_cpu_to_le16(RFCOMM_PSM);
1985 addr.l2_cid = 0; 1987 addr.l2_cid = 0;
1988 addr.l2_bdaddr_type = BDADDR_BREDR;
1986 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr)); 1989 err = kernel_bind(sock, (struct sockaddr *) &addr, sizeof(addr));
1987 if (err < 0) { 1990 if (err < 0) {
1988 BT_ERR("Bind failed %d", err); 1991 BT_ERR("Bind failed %d", err);
@@ -2112,12 +2115,11 @@ static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
2112 rfcomm_lock(); 2115 rfcomm_lock();
2113 2116
2114 list_for_each_entry(s, &session_list, list) { 2117 list_for_each_entry(s, &session_list, list) {
2118 struct l2cap_chan *chan = l2cap_pi(s->sock->sk)->chan;
2115 struct rfcomm_dlc *d; 2119 struct rfcomm_dlc *d;
2116 list_for_each_entry(d, &s->dlcs, list) { 2120 list_for_each_entry(d, &s->dlcs, list) {
2117 struct sock *sk = s->sock->sk;
2118
2119 seq_printf(f, "%pMR %pMR %ld %d %d %d %d\n", 2121 seq_printf(f, "%pMR %pMR %ld %d %d %d %d\n",
2120 &bt_sk(sk)->src, &bt_sk(sk)->dst, 2122 &chan->src, &chan->dst,
2121 d->state, d->dlci, d->mtu, 2123 d->state, d->dlci, d->mtu,
2122 d->rx_credits, d->tx_credits); 2124 d->rx_credits, d->tx_credits);
2123 } 2125 }
@@ -2155,13 +2157,6 @@ static int __init rfcomm_init(void)
2155 goto unregister; 2157 goto unregister;
2156 } 2158 }
2157 2159
2158 if (bt_debugfs) {
2159 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2160 bt_debugfs, NULL, &rfcomm_dlc_debugfs_fops);
2161 if (!rfcomm_dlc_debugfs)
2162 BT_ERR("Failed to create RFCOMM debug file");
2163 }
2164
2165 err = rfcomm_init_ttys(); 2160 err = rfcomm_init_ttys();
2166 if (err < 0) 2161 if (err < 0)
2167 goto stop; 2162 goto stop;
@@ -2172,6 +2167,13 @@ static int __init rfcomm_init(void)
2172 2167
2173 BT_INFO("RFCOMM ver %s", VERSION); 2168 BT_INFO("RFCOMM ver %s", VERSION);
2174 2169
2170 if (IS_ERR_OR_NULL(bt_debugfs))
2171 return 0;
2172
2173 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2174 bt_debugfs, NULL,
2175 &rfcomm_dlc_debugfs_fops);
2176
2175 return 0; 2177 return 0;
2176 2178
2177cleanup: 2179cleanup:
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 30b3721dc6d7..3c2d3e4aa2f5 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -87,7 +87,8 @@ static void rfcomm_sk_state_change(struct rfcomm_dlc *d, int err)
87 parent->sk_data_ready(parent, 0); 87 parent->sk_data_ready(parent, 0);
88 } else { 88 } else {
89 if (d->state == BT_CONNECTED) 89 if (d->state == BT_CONNECTED)
90 rfcomm_session_getaddr(d->session, &bt_sk(sk)->src, NULL); 90 rfcomm_session_getaddr(d->session,
91 &rfcomm_pi(sk)->src, NULL);
91 sk->sk_state_change(sk); 92 sk->sk_state_change(sk);
92 } 93 }
93 94
@@ -110,7 +111,7 @@ static struct sock *__rfcomm_get_sock_by_addr(u8 channel, bdaddr_t *src)
110 111
111 sk_for_each(sk, &rfcomm_sk_list.head) { 112 sk_for_each(sk, &rfcomm_sk_list.head) {
112 if (rfcomm_pi(sk)->channel == channel && 113 if (rfcomm_pi(sk)->channel == channel &&
113 !bacmp(&bt_sk(sk)->src, src)) 114 !bacmp(&rfcomm_pi(sk)->src, src))
114 break; 115 break;
115 } 116 }
116 117
@@ -132,11 +133,11 @@ static struct sock *rfcomm_get_sock_by_channel(int state, u8 channel, bdaddr_t *
132 133
133 if (rfcomm_pi(sk)->channel == channel) { 134 if (rfcomm_pi(sk)->channel == channel) {
134 /* Exact match. */ 135 /* Exact match. */
135 if (!bacmp(&bt_sk(sk)->src, src)) 136 if (!bacmp(&rfcomm_pi(sk)->src, src))
136 break; 137 break;
137 138
138 /* Closest match */ 139 /* Closest match */
139 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 140 if (!bacmp(&rfcomm_pi(sk)->src, BDADDR_ANY))
140 sk1 = sk; 141 sk1 = sk;
141 } 142 }
142 } 143 }
@@ -355,7 +356,7 @@ static int rfcomm_sock_bind(struct socket *sock, struct sockaddr *addr, int addr
355 err = -EADDRINUSE; 356 err = -EADDRINUSE;
356 } else { 357 } else {
357 /* Save source address */ 358 /* Save source address */
358 bacpy(&bt_sk(sk)->src, &sa->rc_bdaddr); 359 bacpy(&rfcomm_pi(sk)->src, &sa->rc_bdaddr);
359 rfcomm_pi(sk)->channel = sa->rc_channel; 360 rfcomm_pi(sk)->channel = sa->rc_channel;
360 sk->sk_state = BT_BOUND; 361 sk->sk_state = BT_BOUND;
361 } 362 }
@@ -393,13 +394,14 @@ static int rfcomm_sock_connect(struct socket *sock, struct sockaddr *addr, int a
393 } 394 }
394 395
395 sk->sk_state = BT_CONNECT; 396 sk->sk_state = BT_CONNECT;
396 bacpy(&bt_sk(sk)->dst, &sa->rc_bdaddr); 397 bacpy(&rfcomm_pi(sk)->dst, &sa->rc_bdaddr);
397 rfcomm_pi(sk)->channel = sa->rc_channel; 398 rfcomm_pi(sk)->channel = sa->rc_channel;
398 399
399 d->sec_level = rfcomm_pi(sk)->sec_level; 400 d->sec_level = rfcomm_pi(sk)->sec_level;
400 d->role_switch = rfcomm_pi(sk)->role_switch; 401 d->role_switch = rfcomm_pi(sk)->role_switch;
401 402
402 err = rfcomm_dlc_open(d, &bt_sk(sk)->src, &sa->rc_bdaddr, sa->rc_channel); 403 err = rfcomm_dlc_open(d, &rfcomm_pi(sk)->src, &sa->rc_bdaddr,
404 sa->rc_channel);
403 if (!err) 405 if (!err)
404 err = bt_sock_wait_state(sk, BT_CONNECTED, 406 err = bt_sock_wait_state(sk, BT_CONNECTED,
405 sock_sndtimeo(sk, flags & O_NONBLOCK)); 407 sock_sndtimeo(sk, flags & O_NONBLOCK));
@@ -429,7 +431,7 @@ static int rfcomm_sock_listen(struct socket *sock, int backlog)
429 } 431 }
430 432
431 if (!rfcomm_pi(sk)->channel) { 433 if (!rfcomm_pi(sk)->channel) {
432 bdaddr_t *src = &bt_sk(sk)->src; 434 bdaddr_t *src = &rfcomm_pi(sk)->src;
433 u8 channel; 435 u8 channel;
434 436
435 err = -EINVAL; 437 err = -EINVAL;
@@ -530,9 +532,9 @@ static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *
530 sa->rc_family = AF_BLUETOOTH; 532 sa->rc_family = AF_BLUETOOTH;
531 sa->rc_channel = rfcomm_pi(sk)->channel; 533 sa->rc_channel = rfcomm_pi(sk)->channel;
532 if (peer) 534 if (peer)
533 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->dst); 535 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->dst);
534 else 536 else
535 bacpy(&sa->rc_bdaddr, &bt_sk(sk)->src); 537 bacpy(&sa->rc_bdaddr, &rfcomm_pi(sk)->src);
536 538
537 *len = sizeof(struct sockaddr_rc); 539 *len = sizeof(struct sockaddr_rc);
538 return 0; 540 return 0;
@@ -544,7 +546,7 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
544 struct sock *sk = sock->sk; 546 struct sock *sk = sock->sk;
545 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; 547 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
546 struct sk_buff *skb; 548 struct sk_buff *skb;
547 int sent = 0; 549 int sent;
548 550
549 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags)) 551 if (test_bit(RFCOMM_DEFER_SETUP, &d->flags))
550 return -ENOTCONN; 552 return -ENOTCONN;
@@ -559,6 +561,10 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
559 561
560 lock_sock(sk); 562 lock_sock(sk);
561 563
564 sent = bt_sock_wait_ready(sk, msg->msg_flags);
565 if (sent)
566 goto done;
567
562 while (len) { 568 while (len) {
563 size_t size = min_t(size_t, len, d->mtu); 569 size_t size = min_t(size_t, len, d->mtu);
564 int err; 570 int err;
@@ -594,6 +600,7 @@ static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
594 len -= size; 600 len -= size;
595 } 601 }
596 602
603done:
597 release_sock(sk); 604 release_sock(sk);
598 605
599 return sent; 606 return sent;
@@ -608,7 +615,6 @@ static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
608 615
609 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) { 616 if (test_and_clear_bit(RFCOMM_DEFER_SETUP, &d->flags)) {
610 rfcomm_dlc_accept(d); 617 rfcomm_dlc_accept(d);
611 msg->msg_namelen = 0;
612 return 0; 618 return 0;
613 } 619 }
614 620
@@ -732,8 +738,9 @@ static int rfcomm_sock_setsockopt(struct socket *sock, int level, int optname, c
732static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen) 738static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __user *optval, int __user *optlen)
733{ 739{
734 struct sock *sk = sock->sk; 740 struct sock *sk = sock->sk;
741 struct sock *l2cap_sk;
742 struct l2cap_conn *conn;
735 struct rfcomm_conninfo cinfo; 743 struct rfcomm_conninfo cinfo;
736 struct l2cap_conn *conn = l2cap_pi(sk)->chan->conn;
737 int len, err = 0; 744 int len, err = 0;
738 u32 opt; 745 u32 opt;
739 746
@@ -776,6 +783,9 @@ static int rfcomm_sock_getsockopt_old(struct socket *sock, int optname, char __u
776 break; 783 break;
777 } 784 }
778 785
786 l2cap_sk = rfcomm_pi(sk)->dlc->session->sock->sk;
787 conn = l2cap_pi(l2cap_sk)->chan->conn;
788
779 memset(&cinfo, 0, sizeof(cinfo)); 789 memset(&cinfo, 0, sizeof(cinfo));
780 cinfo.hci_handle = conn->hcon->handle; 790 cinfo.hci_handle = conn->hcon->handle;
781 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3); 791 memcpy(cinfo.dev_class, conn->hcon->dev_class, 3);
@@ -946,8 +956,8 @@ int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc *
946 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM); 956 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
947 957
948 rfcomm_sock_init(sk, parent); 958 rfcomm_sock_init(sk, parent);
949 bacpy(&bt_sk(sk)->src, &src); 959 bacpy(&rfcomm_pi(sk)->src, &src);
950 bacpy(&bt_sk(sk)->dst, &dst); 960 bacpy(&rfcomm_pi(sk)->dst, &dst);
951 rfcomm_pi(sk)->channel = channel; 961 rfcomm_pi(sk)->channel = channel;
952 962
953 sk->sk_state = BT_CONFIG; 963 sk->sk_state = BT_CONFIG;
@@ -974,7 +984,7 @@ static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
974 984
975 sk_for_each(sk, &rfcomm_sk_list.head) { 985 sk_for_each(sk, &rfcomm_sk_list.head) {
976 seq_printf(f, "%pMR %pMR %d %d\n", 986 seq_printf(f, "%pMR %pMR %d %d\n",
977 &bt_sk(sk)->src, &bt_sk(sk)->dst, 987 &rfcomm_pi(sk)->src, &rfcomm_pi(sk)->dst,
978 sk->sk_state, rfcomm_pi(sk)->channel); 988 sk->sk_state, rfcomm_pi(sk)->channel);
979 } 989 }
980 990
@@ -1044,15 +1054,15 @@ int __init rfcomm_init_sockets(void)
1044 goto error; 1054 goto error;
1045 } 1055 }
1046 1056
1047 if (bt_debugfs) {
1048 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1049 bt_debugfs, NULL, &rfcomm_sock_debugfs_fops);
1050 if (!rfcomm_sock_debugfs)
1051 BT_ERR("Failed to create RFCOMM debug file");
1052 }
1053
1054 BT_INFO("RFCOMM socket layer initialized"); 1057 BT_INFO("RFCOMM socket layer initialized");
1055 1058
1059 if (IS_ERR_OR_NULL(bt_debugfs))
1060 return 0;
1061
1062 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1063 bt_debugfs, NULL,
1064 &rfcomm_sock_debugfs_fops);
1065
1056 return 0; 1066 return 0;
1057 1067
1058error: 1068error:
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 96bd388d93a4..24fa3964b3c8 100644
--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -92,9 +92,6 @@ static struct sco_conn *sco_conn_add(struct hci_conn *hcon)
92 hcon->sco_data = conn; 92 hcon->sco_data = conn;
93 conn->hcon = hcon; 93 conn->hcon = hcon;
94 94
95 conn->src = &hdev->bdaddr;
96 conn->dst = &hcon->dst;
97
98 if (hdev->sco_mtu > 0) 95 if (hdev->sco_mtu > 0)
99 conn->mtu = hdev->sco_mtu; 96 conn->mtu = hdev->sco_mtu;
100 else 97 else
@@ -156,16 +153,14 @@ static int sco_chan_add(struct sco_conn *conn, struct sock *sk,
156 153
157static int sco_connect(struct sock *sk) 154static int sco_connect(struct sock *sk)
158{ 155{
159 bdaddr_t *src = &bt_sk(sk)->src;
160 bdaddr_t *dst = &bt_sk(sk)->dst;
161 struct sco_conn *conn; 156 struct sco_conn *conn;
162 struct hci_conn *hcon; 157 struct hci_conn *hcon;
163 struct hci_dev *hdev; 158 struct hci_dev *hdev;
164 int err, type; 159 int err, type;
165 160
166 BT_DBG("%pMR -> %pMR", src, dst); 161 BT_DBG("%pMR -> %pMR", &sco_pi(sk)->src, &sco_pi(sk)->dst);
167 162
168 hdev = hci_get_route(dst, src); 163 hdev = hci_get_route(&sco_pi(sk)->dst, &sco_pi(sk)->src);
169 if (!hdev) 164 if (!hdev)
170 return -EHOSTUNREACH; 165 return -EHOSTUNREACH;
171 166
@@ -182,7 +177,8 @@ static int sco_connect(struct sock *sk)
182 goto done; 177 goto done;
183 } 178 }
184 179
185 hcon = hci_connect_sco(hdev, type, dst, sco_pi(sk)->setting); 180 hcon = hci_connect_sco(hdev, type, &sco_pi(sk)->dst,
181 sco_pi(sk)->setting);
186 if (IS_ERR(hcon)) { 182 if (IS_ERR(hcon)) {
187 err = PTR_ERR(hcon); 183 err = PTR_ERR(hcon);
188 goto done; 184 goto done;
@@ -196,7 +192,7 @@ static int sco_connect(struct sock *sk)
196 } 192 }
197 193
198 /* Update source addr of the socket */ 194 /* Update source addr of the socket */
199 bacpy(src, conn->src); 195 bacpy(&sco_pi(sk)->src, &hcon->src);
200 196
201 err = sco_chan_add(conn, sk, NULL); 197 err = sco_chan_add(conn, sk, NULL);
202 if (err) 198 if (err)
@@ -270,7 +266,7 @@ static struct sock *__sco_get_sock_listen_by_addr(bdaddr_t *ba)
270 if (sk->sk_state != BT_LISTEN) 266 if (sk->sk_state != BT_LISTEN)
271 continue; 267 continue;
272 268
273 if (!bacmp(&bt_sk(sk)->src, ba)) 269 if (!bacmp(&sco_pi(sk)->src, ba))
274 return sk; 270 return sk;
275 } 271 }
276 272
@@ -291,11 +287,11 @@ static struct sock *sco_get_sock_listen(bdaddr_t *src)
291 continue; 287 continue;
292 288
293 /* Exact match. */ 289 /* Exact match. */
294 if (!bacmp(&bt_sk(sk)->src, src)) 290 if (!bacmp(&sco_pi(sk)->src, src))
295 break; 291 break;
296 292
297 /* Closest match */ 293 /* Closest match */
298 if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) 294 if (!bacmp(&sco_pi(sk)->src, BDADDR_ANY))
299 sk1 = sk; 295 sk1 = sk;
300 } 296 }
301 297
@@ -475,7 +471,7 @@ static int sco_sock_bind(struct socket *sock, struct sockaddr *addr, int addr_le
475 goto done; 471 goto done;
476 } 472 }
477 473
478 bacpy(&bt_sk(sk)->src, &sa->sco_bdaddr); 474 bacpy(&sco_pi(sk)->src, &sa->sco_bdaddr);
479 475
480 sk->sk_state = BT_BOUND; 476 sk->sk_state = BT_BOUND;
481 477
@@ -505,7 +501,7 @@ static int sco_sock_connect(struct socket *sock, struct sockaddr *addr, int alen
505 lock_sock(sk); 501 lock_sock(sk);
506 502
507 /* Set destination address and psm */ 503 /* Set destination address and psm */
508 bacpy(&bt_sk(sk)->dst, &sa->sco_bdaddr); 504 bacpy(&sco_pi(sk)->dst, &sa->sco_bdaddr);
509 505
510 err = sco_connect(sk); 506 err = sco_connect(sk);
511 if (err) 507 if (err)
@@ -522,7 +518,7 @@ done:
522static int sco_sock_listen(struct socket *sock, int backlog) 518static int sco_sock_listen(struct socket *sock, int backlog)
523{ 519{
524 struct sock *sk = sock->sk; 520 struct sock *sk = sock->sk;
525 bdaddr_t *src = &bt_sk(sk)->src; 521 bdaddr_t *src = &sco_pi(sk)->src;
526 int err = 0; 522 int err = 0;
527 523
528 BT_DBG("sk %p backlog %d", sk, backlog); 524 BT_DBG("sk %p backlog %d", sk, backlog);
@@ -626,9 +622,9 @@ static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len
626 *len = sizeof(struct sockaddr_sco); 622 *len = sizeof(struct sockaddr_sco);
627 623
628 if (peer) 624 if (peer)
629 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->dst); 625 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->dst);
630 else 626 else
631 bacpy(&sa->sco_bdaddr, &bt_sk(sk)->src); 627 bacpy(&sa->sco_bdaddr, &sco_pi(sk)->src);
632 628
633 return 0; 629 return 0;
634} 630}
@@ -715,7 +711,6 @@ static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
715 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 711 test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) {
716 sco_conn_defer_accept(pi->conn->hcon, pi->setting); 712 sco_conn_defer_accept(pi->conn->hcon, pi->setting);
717 sk->sk_state = BT_CONFIG; 713 sk->sk_state = BT_CONFIG;
718 msg->msg_namelen = 0;
719 714
720 release_sock(sk); 715 release_sock(sk);
721 return 0; 716 return 0;
@@ -999,7 +994,7 @@ static void sco_conn_ready(struct sco_conn *conn)
999 } else { 994 } else {
1000 sco_conn_lock(conn); 995 sco_conn_lock(conn);
1001 996
1002 parent = sco_get_sock_listen(conn->src); 997 parent = sco_get_sock_listen(&conn->hcon->src);
1003 if (!parent) { 998 if (!parent) {
1004 sco_conn_unlock(conn); 999 sco_conn_unlock(conn);
1005 return; 1000 return;
@@ -1017,8 +1012,8 @@ static void sco_conn_ready(struct sco_conn *conn)
1017 1012
1018 sco_sock_init(sk, parent); 1013 sco_sock_init(sk, parent);
1019 1014
1020 bacpy(&bt_sk(sk)->src, conn->src); 1015 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1021 bacpy(&bt_sk(sk)->dst, conn->dst); 1016 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1022 1017
1023 hci_conn_hold(conn->hcon); 1018 hci_conn_hold(conn->hcon);
1024 __sco_chan_add(conn, sk, parent); 1019 __sco_chan_add(conn, sk, parent);
@@ -1051,8 +1046,8 @@ int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1051 if (sk->sk_state != BT_LISTEN) 1046 if (sk->sk_state != BT_LISTEN)
1052 continue; 1047 continue;
1053 1048
1054 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) || 1049 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1055 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 1050 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1056 lm |= HCI_LM_ACCEPT; 1051 lm |= HCI_LM_ACCEPT;
1057 1052
1058 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) 1053 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
@@ -1111,8 +1106,8 @@ static int sco_debugfs_show(struct seq_file *f, void *p)
1111 read_lock(&sco_sk_list.lock); 1106 read_lock(&sco_sk_list.lock);
1112 1107
1113 sk_for_each(sk, &sco_sk_list.head) { 1108 sk_for_each(sk, &sco_sk_list.head) {
1114 seq_printf(f, "%pMR %pMR %d\n", &bt_sk(sk)->src, 1109 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1115 &bt_sk(sk)->dst, sk->sk_state); 1110 &sco_pi(sk)->dst, sk->sk_state);
1116 } 1111 }
1117 1112
1118 read_unlock(&sco_sk_list.lock); 1113 read_unlock(&sco_sk_list.lock);
@@ -1181,15 +1176,14 @@ int __init sco_init(void)
1181 goto error; 1176 goto error;
1182 } 1177 }
1183 1178
1184 if (bt_debugfs) {
1185 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1186 NULL, &sco_debugfs_fops);
1187 if (!sco_debugfs)
1188 BT_ERR("Failed to create SCO debug file");
1189 }
1190
1191 BT_INFO("SCO socket layer initialized"); 1179 BT_INFO("SCO socket layer initialized");
1192 1180
1181 if (IS_ERR_OR_NULL(bt_debugfs))
1182 return 0;
1183
1184 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1185 NULL, &sco_debugfs_fops);
1186
1193 return 0; 1187 return 0;
1194 1188
1195error: 1189error:
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index b5562abdd6e0..4b07acb8293c 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -28,7 +28,8 @@
28#include <net/bluetooth/hci_core.h> 28#include <net/bluetooth/hci_core.h>
29#include <net/bluetooth/l2cap.h> 29#include <net/bluetooth/l2cap.h>
30#include <net/bluetooth/mgmt.h> 30#include <net/bluetooth/mgmt.h>
31#include <net/bluetooth/smp.h> 31
32#include "smp.h"
32 33
33#define SMP_TIMEOUT msecs_to_jiffies(30000) 34#define SMP_TIMEOUT msecs_to_jiffies(30000)
34 35
@@ -85,8 +86,8 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
85} 86}
86 87
87static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16], 88static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
88 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia, 89 u8 preq[7], u8 pres[7], u8 _iat, bdaddr_t *ia,
89 u8 _rat, bdaddr_t *ra, u8 res[16]) 90 u8 _rat, bdaddr_t *ra, u8 res[16])
90{ 91{
91 u8 p1[16], p2[16]; 92 u8 p1[16], p2[16];
92 int err; 93 int err;
@@ -126,8 +127,8 @@ static int smp_c1(struct crypto_blkcipher *tfm, u8 k[16], u8 r[16],
126 return err; 127 return err;
127} 128}
128 129
129static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], 130static int smp_s1(struct crypto_blkcipher *tfm, u8 k[16], u8 r1[16],
130 u8 r1[16], u8 r2[16], u8 _r[16]) 131 u8 r2[16], u8 _r[16])
131{ 132{
132 int err; 133 int err;
133 134
@@ -150,7 +151,7 @@ static int smp_rand(u8 *buf)
150} 151}
151 152
152static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code, 153static struct sk_buff *smp_build_cmd(struct l2cap_conn *conn, u8 code,
153 u16 dlen, void *data) 154 u16 dlen, void *data)
154{ 155{
155 struct sk_buff *skb; 156 struct sk_buff *skb;
156 struct l2cap_hdr *lh; 157 struct l2cap_hdr *lh;
@@ -213,9 +214,8 @@ static __u8 seclevel_to_authreq(__u8 sec_level)
213} 214}
214 215
215static void build_pairing_cmd(struct l2cap_conn *conn, 216static void build_pairing_cmd(struct l2cap_conn *conn,
216 struct smp_cmd_pairing *req, 217 struct smp_cmd_pairing *req,
217 struct smp_cmd_pairing *rsp, 218 struct smp_cmd_pairing *rsp, __u8 authreq)
218 __u8 authreq)
219{ 219{
220 u8 dist_keys = 0; 220 u8 dist_keys = 0;
221 221
@@ -249,7 +249,7 @@ static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
249 struct smp_chan *smp = conn->smp_chan; 249 struct smp_chan *smp = conn->smp_chan;
250 250
251 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) || 251 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
252 (max_key_size < SMP_MIN_ENC_KEY_SIZE)) 252 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
253 return SMP_ENC_KEY_SIZE; 253 return SMP_ENC_KEY_SIZE;
254 254
255 smp->enc_key_size = max_key_size; 255 smp->enc_key_size = max_key_size;
@@ -263,15 +263,15 @@ static void smp_failure(struct l2cap_conn *conn, u8 reason, u8 send)
263 263
264 if (send) 264 if (send)
265 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason), 265 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
266 &reason); 266 &reason);
267 267
268 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->hcon->flags); 268 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
269 mgmt_auth_failed(conn->hcon->hdev, conn->dst, hcon->type, 269 mgmt_auth_failed(hcon->hdev, &hcon->dst, hcon->type, hcon->dst_type,
270 hcon->dst_type, HCI_ERROR_AUTH_FAILURE); 270 HCI_ERROR_AUTH_FAILURE);
271 271
272 cancel_delayed_work_sync(&conn->security_timer); 272 cancel_delayed_work_sync(&conn->security_timer);
273 273
274 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &conn->hcon->flags)) 274 if (test_and_clear_bit(HCI_CONN_LE_SMP_PEND, &hcon->flags))
275 smp_chan_destroy(conn); 275 smp_chan_destroy(conn);
276} 276}
277 277
@@ -309,8 +309,8 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
309 /* If either side has unknown io_caps, use JUST WORKS */ 309 /* If either side has unknown io_caps, use JUST WORKS */
310 /* Otherwise, look up method from the table */ 310 /* Otherwise, look up method from the table */
311 if (!(auth & SMP_AUTH_MITM) || 311 if (!(auth & SMP_AUTH_MITM) ||
312 local_io > SMP_IO_KEYBOARD_DISPLAY || 312 local_io > SMP_IO_KEYBOARD_DISPLAY ||
313 remote_io > SMP_IO_KEYBOARD_DISPLAY) 313 remote_io > SMP_IO_KEYBOARD_DISPLAY)
314 method = JUST_WORKS; 314 method = JUST_WORKS;
315 else 315 else
316 method = gen_method[remote_io][local_io]; 316 method = gen_method[remote_io][local_io];
@@ -354,10 +354,10 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
354 hci_dev_lock(hcon->hdev); 354 hci_dev_lock(hcon->hdev);
355 355
356 if (method == REQ_PASSKEY) 356 if (method == REQ_PASSKEY)
357 ret = mgmt_user_passkey_request(hcon->hdev, conn->dst, 357 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
358 hcon->type, hcon->dst_type); 358 hcon->type, hcon->dst_type);
359 else 359 else
360 ret = mgmt_user_confirm_request(hcon->hdev, conn->dst, 360 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
361 hcon->type, hcon->dst_type, 361 hcon->type, hcon->dst_type,
362 cpu_to_le32(passkey), 0); 362 cpu_to_le32(passkey), 0);
363 363
@@ -386,12 +386,13 @@ static void confirm_work(struct work_struct *work)
386 smp->tfm = tfm; 386 smp->tfm = tfm;
387 387
388 if (conn->hcon->out) 388 if (conn->hcon->out)
389 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 0, 389 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
390 conn->src, conn->hcon->dst_type, conn->dst, res); 390 conn->hcon->src_type, &conn->hcon->src,
391 conn->hcon->dst_type, &conn->hcon->dst, res);
391 else 392 else
392 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp, 393 ret = smp_c1(tfm, smp->tk, smp->prnd, smp->preq, smp->prsp,
393 conn->hcon->dst_type, conn->dst, 0, conn->src, 394 conn->hcon->dst_type, &conn->hcon->dst,
394 res); 395 conn->hcon->src_type, &conn->hcon->src, res);
395 if (ret) { 396 if (ret) {
396 reason = SMP_UNSPECIFIED; 397 reason = SMP_UNSPECIFIED;
397 goto error; 398 goto error;
@@ -425,11 +426,13 @@ static void random_work(struct work_struct *work)
425 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave"); 426 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
426 427
427 if (hcon->out) 428 if (hcon->out)
428 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 0, 429 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
429 conn->src, hcon->dst_type, conn->dst, res); 430 hcon->src_type, &hcon->src,
431 hcon->dst_type, &hcon->dst, res);
430 else 432 else
431 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp, 433 ret = smp_c1(tfm, smp->tk, smp->rrnd, smp->preq, smp->prsp,
432 hcon->dst_type, conn->dst, 0, conn->src, res); 434 hcon->dst_type, &hcon->dst,
435 hcon->src_type, &hcon->src, res);
433 if (ret) { 436 if (ret) {
434 reason = SMP_UNSPECIFIED; 437 reason = SMP_UNSPECIFIED;
435 goto error; 438 goto error;
@@ -477,9 +480,9 @@ static void random_work(struct work_struct *work)
477 swap128(key, stk); 480 swap128(key, stk);
478 481
479 memset(stk + smp->enc_key_size, 0, 482 memset(stk + smp->enc_key_size, 0,
480 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size); 483 SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
481 484
482 hci_add_ltk(hcon->hdev, conn->dst, hcon->dst_type, 485 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
483 HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size, 486 HCI_SMP_STK_SLAVE, 0, 0, stk, smp->enc_key_size,
484 ediv, rand); 487 ediv, rand);
485 } 488 }
@@ -494,7 +497,7 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
494{ 497{
495 struct smp_chan *smp; 498 struct smp_chan *smp;
496 499
497 smp = kzalloc(sizeof(struct smp_chan), GFP_ATOMIC); 500 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
498 if (!smp) 501 if (!smp)
499 return NULL; 502 return NULL;
500 503
@@ -649,7 +652,7 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
649 memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); 652 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
650 653
651 if ((req->auth_req & SMP_AUTH_BONDING) && 654 if ((req->auth_req & SMP_AUTH_BONDING) &&
652 (rsp->auth_req & SMP_AUTH_BONDING)) 655 (rsp->auth_req & SMP_AUTH_BONDING))
653 auth = SMP_AUTH_BONDING; 656 auth = SMP_AUTH_BONDING;
654 657
655 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM; 658 auth |= (req->auth_req | rsp->auth_req) & SMP_AUTH_MITM;
@@ -684,7 +687,7 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
684 687
685 swap128(smp->prnd, random); 688 swap128(smp->prnd, random);
686 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random), 689 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(random),
687 random); 690 random);
688 } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) { 691 } else if (test_bit(SMP_FLAG_TK_VALID, &smp->smp_flags)) {
689 queue_work(hdev->workqueue, &smp->confirm); 692 queue_work(hdev->workqueue, &smp->confirm);
690 } else { 693 } else {
@@ -714,7 +717,7 @@ static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
714 struct smp_ltk *key; 717 struct smp_ltk *key;
715 struct hci_conn *hcon = conn->hcon; 718 struct hci_conn *hcon = conn->hcon;
716 719
717 key = hci_find_ltk_by_addr(hcon->hdev, conn->dst, hcon->dst_type); 720 key = hci_find_ltk_by_addr(hcon->hdev, &hcon->dst, hcon->dst_type);
718 if (!key) 721 if (!key)
719 return 0; 722 return 0;
720 723
@@ -728,8 +731,8 @@ static u8 smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
728 hcon->enc_key_size = key->enc_size; 731 hcon->enc_key_size = key->enc_size;
729 732
730 return 1; 733 return 1;
731
732} 734}
735
733static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb) 736static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
734{ 737{
735 struct smp_cmd_security_req *rp = (void *) skb->data; 738 struct smp_cmd_security_req *rp = (void *) skb->data;
@@ -739,6 +742,9 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
739 742
740 BT_DBG("conn %p", conn); 743 BT_DBG("conn %p", conn);
741 744
745 if (!(conn->hcon->link_mode & HCI_LM_MASTER))
746 return SMP_CMD_NOTSUPP;
747
742 hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req); 748 hcon->pending_sec_level = authreq_to_seclevel(rp->auth_req);
743 749
744 if (smp_ltk_encrypt(conn, hcon->pending_sec_level)) 750 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
@@ -835,9 +841,9 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
835 skb_pull(skb, sizeof(*rp)); 841 skb_pull(skb, sizeof(*rp));
836 842
837 hci_dev_lock(hdev); 843 hci_dev_lock(hdev);
838 authenticated = (conn->hcon->sec_level == BT_SECURITY_HIGH); 844 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
839 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 845 hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, HCI_SMP_LTK, 1,
840 HCI_SMP_LTK, 1, authenticated, smp->tk, smp->enc_key_size, 846 authenticated, smp->tk, smp->enc_key_size,
841 rp->ediv, rp->rand); 847 rp->ediv, rp->rand);
842 smp_distribute_keys(conn, 1); 848 smp_distribute_keys(conn, 1);
843 hci_dev_unlock(hdev); 849 hci_dev_unlock(hdev);
@@ -847,16 +853,27 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
847 853
848int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) 854int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
849{ 855{
850 __u8 code = skb->data[0]; 856 struct hci_conn *hcon = conn->hcon;
851 __u8 reason; 857 __u8 code, reason;
852 int err = 0; 858 int err = 0;
853 859
854 if (!test_bit(HCI_LE_ENABLED, &conn->hcon->hdev->dev_flags)) { 860 if (hcon->type != LE_LINK) {
861 kfree_skb(skb);
862 return 0;
863 }
864
865 if (skb->len < 1) {
866 kfree_skb(skb);
867 return -EILSEQ;
868 }
869
870 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
855 err = -ENOTSUPP; 871 err = -ENOTSUPP;
856 reason = SMP_PAIRING_NOTSUPP; 872 reason = SMP_PAIRING_NOTSUPP;
857 goto done; 873 goto done;
858 } 874 }
859 875
876 code = skb->data[0];
860 skb_pull(skb, sizeof(code)); 877 skb_pull(skb, sizeof(code));
861 878
862 /* 879 /*
@@ -974,7 +991,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
974 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 991 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
975 992
976 authenticated = hcon->sec_level == BT_SECURITY_HIGH; 993 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
977 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 994 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
978 HCI_SMP_LTK_SLAVE, 1, authenticated, 995 HCI_SMP_LTK_SLAVE, 1, authenticated,
979 enc.ltk, smp->enc_key_size, ediv, ident.rand); 996 enc.ltk, smp->enc_key_size, ediv, ident.rand);
980 997
@@ -996,10 +1013,10 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
996 1013
997 /* Just public address */ 1014 /* Just public address */
998 memset(&addrinfo, 0, sizeof(addrinfo)); 1015 memset(&addrinfo, 0, sizeof(addrinfo));
999 bacpy(&addrinfo.bdaddr, conn->src); 1016 bacpy(&addrinfo.bdaddr, &conn->hcon->src);
1000 1017
1001 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), 1018 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1002 &addrinfo); 1019 &addrinfo);
1003 1020
1004 *keydist &= ~SMP_DIST_ID_KEY; 1021 *keydist &= ~SMP_DIST_ID_KEY;
1005 } 1022 }
diff --git a/net/bluetooth/smp.h b/net/bluetooth/smp.h
new file mode 100644
index 000000000000..f8ba07f3e5fa
--- /dev/null
+++ b/net/bluetooth/smp.h
@@ -0,0 +1,146 @@
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
23#ifndef __SMP_H
24#define __SMP_H
25
26struct smp_command_hdr {
27 __u8 code;
28} __packed;
29
30#define SMP_CMD_PAIRING_REQ 0x01
31#define SMP_CMD_PAIRING_RSP 0x02
32struct smp_cmd_pairing {
33 __u8 io_capability;
34 __u8 oob_flag;
35 __u8 auth_req;
36 __u8 max_key_size;
37 __u8 init_key_dist;
38 __u8 resp_key_dist;
39} __packed;
40
41#define SMP_IO_DISPLAY_ONLY 0x00
42#define SMP_IO_DISPLAY_YESNO 0x01
43#define SMP_IO_KEYBOARD_ONLY 0x02
44#define SMP_IO_NO_INPUT_OUTPUT 0x03
45#define SMP_IO_KEYBOARD_DISPLAY 0x04
46
47#define SMP_OOB_NOT_PRESENT 0x00
48#define SMP_OOB_PRESENT 0x01
49
50#define SMP_DIST_ENC_KEY 0x01
51#define SMP_DIST_ID_KEY 0x02
52#define SMP_DIST_SIGN 0x04
53
54#define SMP_AUTH_NONE 0x00
55#define SMP_AUTH_BONDING 0x01
56#define SMP_AUTH_MITM 0x04
57
58#define SMP_CMD_PAIRING_CONFIRM 0x03
59struct smp_cmd_pairing_confirm {
60 __u8 confirm_val[16];
61} __packed;
62
63#define SMP_CMD_PAIRING_RANDOM 0x04
64struct smp_cmd_pairing_random {
65 __u8 rand_val[16];
66} __packed;
67
68#define SMP_CMD_PAIRING_FAIL 0x05
69struct smp_cmd_pairing_fail {
70 __u8 reason;
71} __packed;
72
73#define SMP_CMD_ENCRYPT_INFO 0x06
74struct smp_cmd_encrypt_info {
75 __u8 ltk[16];
76} __packed;
77
78#define SMP_CMD_MASTER_IDENT 0x07
79struct smp_cmd_master_ident {
80 __le16 ediv;
81 __u8 rand[8];
82} __packed;
83
84#define SMP_CMD_IDENT_INFO 0x08
85struct smp_cmd_ident_info {
86 __u8 irk[16];
87} __packed;
88
89#define SMP_CMD_IDENT_ADDR_INFO 0x09
90struct smp_cmd_ident_addr_info {
91 __u8 addr_type;
92 bdaddr_t bdaddr;
93} __packed;
94
95#define SMP_CMD_SIGN_INFO 0x0a
96struct smp_cmd_sign_info {
97 __u8 csrk[16];
98} __packed;
99
100#define SMP_CMD_SECURITY_REQ 0x0b
101struct smp_cmd_security_req {
102 __u8 auth_req;
103} __packed;
104
105#define SMP_PASSKEY_ENTRY_FAILED 0x01
106#define SMP_OOB_NOT_AVAIL 0x02
107#define SMP_AUTH_REQUIREMENTS 0x03
108#define SMP_CONFIRM_FAILED 0x04
109#define SMP_PAIRING_NOTSUPP 0x05
110#define SMP_ENC_KEY_SIZE 0x06
111#define SMP_CMD_NOTSUPP 0x07
112#define SMP_UNSPECIFIED 0x08
113#define SMP_REPEATED_ATTEMPTS 0x09
114
115#define SMP_MIN_ENC_KEY_SIZE 7
116#define SMP_MAX_ENC_KEY_SIZE 16
117
118#define SMP_FLAG_TK_VALID 1
119#define SMP_FLAG_CFM_PENDING 2
120#define SMP_FLAG_MITM_AUTH 3
121
122struct smp_chan {
123 struct l2cap_conn *conn;
124 u8 preq[7]; /* SMP Pairing Request */
125 u8 prsp[7]; /* SMP Pairing Response */
126 u8 prnd[16]; /* SMP Pairing Random (local) */
127 u8 rrnd[16]; /* SMP Pairing Random (remote) */
128 u8 pcnf[16]; /* SMP Pairing Confirm */
129 u8 tk[16]; /* SMP Temporary Key */
130 u8 enc_key_size;
131 unsigned long smp_flags;
132 struct crypto_blkcipher *tfm;
133 struct work_struct confirm;
134 struct work_struct random;
135
136};
137
138/* SMP Commands */
139int smp_conn_security(struct hci_conn *hcon, __u8 sec_level);
140int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb);
141int smp_distribute_keys(struct l2cap_conn *conn, __u8 force);
142int smp_user_confirm_reply(struct hci_conn *conn, u16 mgmt_op, __le32 passkey);
143
144void smp_chan_destroy(struct l2cap_conn *conn);
145
146#endif /* __SMP_H */
diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c
index ca04163635da..f00cfd2a0143 100644
--- a/net/bridge/br_device.c
+++ b/net/bridge/br_device.c
@@ -64,7 +64,7 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
64 br_flood_deliver(br, skb, false); 64 br_flood_deliver(br, skb, false);
65 goto out; 65 goto out;
66 } 66 }
67 if (br_multicast_rcv(br, NULL, skb)) { 67 if (br_multicast_rcv(br, NULL, skb, vid)) {
68 kfree_skb(skb); 68 kfree_skb(skb);
69 goto out; 69 goto out;
70 } 70 }
@@ -88,11 +88,18 @@ out:
88static int br_dev_init(struct net_device *dev) 88static int br_dev_init(struct net_device *dev)
89{ 89{
90 struct net_bridge *br = netdev_priv(dev); 90 struct net_bridge *br = netdev_priv(dev);
91 int i;
91 92
92 br->stats = alloc_percpu(struct br_cpu_netstats); 93 br->stats = alloc_percpu(struct br_cpu_netstats);
93 if (!br->stats) 94 if (!br->stats)
94 return -ENOMEM; 95 return -ENOMEM;
95 96
97 for_each_possible_cpu(i) {
98 struct br_cpu_netstats *br_dev_stats;
99 br_dev_stats = per_cpu_ptr(br->stats, i);
100 u64_stats_init(&br_dev_stats->syncp);
101 }
102
96 return 0; 103 return 0;
97} 104}
98 105
diff --git a/net/bridge/br_if.c b/net/bridge/br_if.c
index c41d5fbb91d0..4bf02adb5dc2 100644
--- a/net/bridge/br_if.c
+++ b/net/bridge/br_if.c
@@ -172,6 +172,9 @@ void br_dev_delete(struct net_device *dev, struct list_head *head)
172 del_nbp(p); 172 del_nbp(p);
173 } 173 }
174 174
175 br_fdb_delete_by_port(br, NULL, 1);
176
177 br_vlan_flush(br);
175 del_timer_sync(&br->gc_timer); 178 del_timer_sync(&br->gc_timer);
176 179
177 br_sysfs_delbr(br->dev); 180 br_sysfs_delbr(br->dev);
diff --git a/net/bridge/br_input.c b/net/bridge/br_input.c
index a2fd37ec35f7..7e73c32e205d 100644
--- a/net/bridge/br_input.c
+++ b/net/bridge/br_input.c
@@ -80,7 +80,7 @@ int br_handle_frame_finish(struct sk_buff *skb)
80 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid); 80 br_fdb_update(br, p, eth_hdr(skb)->h_source, vid);
81 81
82 if (!is_broadcast_ether_addr(dest) && is_multicast_ether_addr(dest) && 82 if (!is_broadcast_ether_addr(dest) && is_multicast_ether_addr(dest) &&
83 br_multicast_rcv(br, p, skb)) 83 br_multicast_rcv(br, p, skb, vid))
84 goto drop; 84 goto drop;
85 85
86 if (p->state == BR_STATE_LEARNING) 86 if (p->state == BR_STATE_LEARNING)
diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index 8b0b610ca2c9..ef66365b7354 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -363,7 +363,7 @@ static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br,
363 skb_reset_mac_header(skb); 363 skb_reset_mac_header(skb);
364 eth = eth_hdr(skb); 364 eth = eth_hdr(skb);
365 365
366 memcpy(eth->h_source, br->dev->dev_addr, 6); 366 memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN);
367 eth->h_dest[0] = 1; 367 eth->h_dest[0] = 1;
368 eth->h_dest[1] = 0; 368 eth->h_dest[1] = 0;
369 eth->h_dest[2] = 0x5e; 369 eth->h_dest[2] = 0x5e;
@@ -433,7 +433,7 @@ static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br,
433 skb_reset_mac_header(skb); 433 skb_reset_mac_header(skb);
434 eth = eth_hdr(skb); 434 eth = eth_hdr(skb);
435 435
436 memcpy(eth->h_source, br->dev->dev_addr, 6); 436 memcpy(eth->h_source, br->dev->dev_addr, ETH_ALEN);
437 eth->h_proto = htons(ETH_P_IPV6); 437 eth->h_proto = htons(ETH_P_IPV6);
438 skb_put(skb, sizeof(*eth)); 438 skb_put(skb, sizeof(*eth));
439 439
@@ -947,7 +947,8 @@ void br_multicast_disable_port(struct net_bridge_port *port)
947 947
948static int br_ip4_multicast_igmp3_report(struct net_bridge *br, 948static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
949 struct net_bridge_port *port, 949 struct net_bridge_port *port,
950 struct sk_buff *skb) 950 struct sk_buff *skb,
951 u16 vid)
951{ 952{
952 struct igmpv3_report *ih; 953 struct igmpv3_report *ih;
953 struct igmpv3_grec *grec; 954 struct igmpv3_grec *grec;
@@ -957,12 +958,10 @@ static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
957 int type; 958 int type;
958 int err = 0; 959 int err = 0;
959 __be32 group; 960 __be32 group;
960 u16 vid = 0;
961 961
962 if (!pskb_may_pull(skb, sizeof(*ih))) 962 if (!pskb_may_pull(skb, sizeof(*ih)))
963 return -EINVAL; 963 return -EINVAL;
964 964
965 br_vlan_get_tag(skb, &vid);
966 ih = igmpv3_report_hdr(skb); 965 ih = igmpv3_report_hdr(skb);
967 num = ntohs(ih->ngrec); 966 num = ntohs(ih->ngrec);
968 len = sizeof(*ih); 967 len = sizeof(*ih);
@@ -1005,7 +1004,8 @@ static int br_ip4_multicast_igmp3_report(struct net_bridge *br,
1005#if IS_ENABLED(CONFIG_IPV6) 1004#if IS_ENABLED(CONFIG_IPV6)
1006static int br_ip6_multicast_mld2_report(struct net_bridge *br, 1005static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1007 struct net_bridge_port *port, 1006 struct net_bridge_port *port,
1008 struct sk_buff *skb) 1007 struct sk_buff *skb,
1008 u16 vid)
1009{ 1009{
1010 struct icmp6hdr *icmp6h; 1010 struct icmp6hdr *icmp6h;
1011 struct mld2_grec *grec; 1011 struct mld2_grec *grec;
@@ -1013,12 +1013,10 @@ static int br_ip6_multicast_mld2_report(struct net_bridge *br,
1013 int len; 1013 int len;
1014 int num; 1014 int num;
1015 int err = 0; 1015 int err = 0;
1016 u16 vid = 0;
1017 1016
1018 if (!pskb_may_pull(skb, sizeof(*icmp6h))) 1017 if (!pskb_may_pull(skb, sizeof(*icmp6h)))
1019 return -EINVAL; 1018 return -EINVAL;
1020 1019
1021 br_vlan_get_tag(skb, &vid);
1022 icmp6h = icmp6_hdr(skb); 1020 icmp6h = icmp6_hdr(skb);
1023 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); 1021 num = ntohs(icmp6h->icmp6_dataun.un_data16[1]);
1024 len = sizeof(*icmp6h); 1022 len = sizeof(*icmp6h);
@@ -1141,7 +1139,8 @@ static void br_multicast_query_received(struct net_bridge *br,
1141 1139
1142static int br_ip4_multicast_query(struct net_bridge *br, 1140static int br_ip4_multicast_query(struct net_bridge *br,
1143 struct net_bridge_port *port, 1141 struct net_bridge_port *port,
1144 struct sk_buff *skb) 1142 struct sk_buff *skb,
1143 u16 vid)
1145{ 1144{
1146 const struct iphdr *iph = ip_hdr(skb); 1145 const struct iphdr *iph = ip_hdr(skb);
1147 struct igmphdr *ih = igmp_hdr(skb); 1146 struct igmphdr *ih = igmp_hdr(skb);
@@ -1153,7 +1152,6 @@ static int br_ip4_multicast_query(struct net_bridge *br,
1153 unsigned long now = jiffies; 1152 unsigned long now = jiffies;
1154 __be32 group; 1153 __be32 group;
1155 int err = 0; 1154 int err = 0;
1156 u16 vid = 0;
1157 1155
1158 spin_lock(&br->multicast_lock); 1156 spin_lock(&br->multicast_lock);
1159 if (!netif_running(br->dev) || 1157 if (!netif_running(br->dev) ||
@@ -1189,7 +1187,6 @@ static int br_ip4_multicast_query(struct net_bridge *br,
1189 if (!group) 1187 if (!group)
1190 goto out; 1188 goto out;
1191 1189
1192 br_vlan_get_tag(skb, &vid);
1193 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid); 1190 mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group, vid);
1194 if (!mp) 1191 if (!mp)
1195 goto out; 1192 goto out;
@@ -1219,7 +1216,8 @@ out:
1219#if IS_ENABLED(CONFIG_IPV6) 1216#if IS_ENABLED(CONFIG_IPV6)
1220static int br_ip6_multicast_query(struct net_bridge *br, 1217static int br_ip6_multicast_query(struct net_bridge *br,
1221 struct net_bridge_port *port, 1218 struct net_bridge_port *port,
1222 struct sk_buff *skb) 1219 struct sk_buff *skb,
1220 u16 vid)
1223{ 1221{
1224 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 1222 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
1225 struct mld_msg *mld; 1223 struct mld_msg *mld;
@@ -1231,7 +1229,6 @@ static int br_ip6_multicast_query(struct net_bridge *br,
1231 unsigned long now = jiffies; 1229 unsigned long now = jiffies;
1232 const struct in6_addr *group = NULL; 1230 const struct in6_addr *group = NULL;
1233 int err = 0; 1231 int err = 0;
1234 u16 vid = 0;
1235 1232
1236 spin_lock(&br->multicast_lock); 1233 spin_lock(&br->multicast_lock);
1237 if (!netif_running(br->dev) || 1234 if (!netif_running(br->dev) ||
@@ -1265,7 +1262,6 @@ static int br_ip6_multicast_query(struct net_bridge *br,
1265 if (!group) 1262 if (!group)
1266 goto out; 1263 goto out;
1267 1264
1268 br_vlan_get_tag(skb, &vid);
1269 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid); 1265 mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group, vid);
1270 if (!mp) 1266 if (!mp)
1271 goto out; 1267 goto out;
@@ -1439,7 +1435,8 @@ static void br_ip6_multicast_leave_group(struct net_bridge *br,
1439 1435
1440static int br_multicast_ipv4_rcv(struct net_bridge *br, 1436static int br_multicast_ipv4_rcv(struct net_bridge *br,
1441 struct net_bridge_port *port, 1437 struct net_bridge_port *port,
1442 struct sk_buff *skb) 1438 struct sk_buff *skb,
1439 u16 vid)
1443{ 1440{
1444 struct sk_buff *skb2 = skb; 1441 struct sk_buff *skb2 = skb;
1445 const struct iphdr *iph; 1442 const struct iphdr *iph;
@@ -1447,7 +1444,6 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
1447 unsigned int len; 1444 unsigned int len;
1448 unsigned int offset; 1445 unsigned int offset;
1449 int err; 1446 int err;
1450 u16 vid = 0;
1451 1447
1452 /* We treat OOM as packet loss for now. */ 1448 /* We treat OOM as packet loss for now. */
1453 if (!pskb_may_pull(skb, sizeof(*iph))) 1449 if (!pskb_may_pull(skb, sizeof(*iph)))
@@ -1508,7 +1504,6 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
1508 1504
1509 err = 0; 1505 err = 0;
1510 1506
1511 br_vlan_get_tag(skb2, &vid);
1512 BR_INPUT_SKB_CB(skb)->igmp = 1; 1507 BR_INPUT_SKB_CB(skb)->igmp = 1;
1513 ih = igmp_hdr(skb2); 1508 ih = igmp_hdr(skb2);
1514 1509
@@ -1519,10 +1514,10 @@ static int br_multicast_ipv4_rcv(struct net_bridge *br,
1519 err = br_ip4_multicast_add_group(br, port, ih->group, vid); 1514 err = br_ip4_multicast_add_group(br, port, ih->group, vid);
1520 break; 1515 break;
1521 case IGMPV3_HOST_MEMBERSHIP_REPORT: 1516 case IGMPV3_HOST_MEMBERSHIP_REPORT:
1522 err = br_ip4_multicast_igmp3_report(br, port, skb2); 1517 err = br_ip4_multicast_igmp3_report(br, port, skb2, vid);
1523 break; 1518 break;
1524 case IGMP_HOST_MEMBERSHIP_QUERY: 1519 case IGMP_HOST_MEMBERSHIP_QUERY:
1525 err = br_ip4_multicast_query(br, port, skb2); 1520 err = br_ip4_multicast_query(br, port, skb2, vid);
1526 break; 1521 break;
1527 case IGMP_HOST_LEAVE_MESSAGE: 1522 case IGMP_HOST_LEAVE_MESSAGE:
1528 br_ip4_multicast_leave_group(br, port, ih->group, vid); 1523 br_ip4_multicast_leave_group(br, port, ih->group, vid);
@@ -1540,7 +1535,8 @@ err_out:
1540#if IS_ENABLED(CONFIG_IPV6) 1535#if IS_ENABLED(CONFIG_IPV6)
1541static int br_multicast_ipv6_rcv(struct net_bridge *br, 1536static int br_multicast_ipv6_rcv(struct net_bridge *br,
1542 struct net_bridge_port *port, 1537 struct net_bridge_port *port,
1543 struct sk_buff *skb) 1538 struct sk_buff *skb,
1539 u16 vid)
1544{ 1540{
1545 struct sk_buff *skb2; 1541 struct sk_buff *skb2;
1546 const struct ipv6hdr *ip6h; 1542 const struct ipv6hdr *ip6h;
@@ -1550,7 +1546,6 @@ static int br_multicast_ipv6_rcv(struct net_bridge *br,
1550 unsigned int len; 1546 unsigned int len;
1551 int offset; 1547 int offset;
1552 int err; 1548 int err;
1553 u16 vid = 0;
1554 1549
1555 if (!pskb_may_pull(skb, sizeof(*ip6h))) 1550 if (!pskb_may_pull(skb, sizeof(*ip6h)))
1556 return -EINVAL; 1551 return -EINVAL;
@@ -1640,7 +1635,6 @@ static int br_multicast_ipv6_rcv(struct net_bridge *br,
1640 1635
1641 err = 0; 1636 err = 0;
1642 1637
1643 br_vlan_get_tag(skb, &vid);
1644 BR_INPUT_SKB_CB(skb)->igmp = 1; 1638 BR_INPUT_SKB_CB(skb)->igmp = 1;
1645 1639
1646 switch (icmp6_type) { 1640 switch (icmp6_type) {
@@ -1657,10 +1651,10 @@ static int br_multicast_ipv6_rcv(struct net_bridge *br,
1657 break; 1651 break;
1658 } 1652 }
1659 case ICMPV6_MLD2_REPORT: 1653 case ICMPV6_MLD2_REPORT:
1660 err = br_ip6_multicast_mld2_report(br, port, skb2); 1654 err = br_ip6_multicast_mld2_report(br, port, skb2, vid);
1661 break; 1655 break;
1662 case ICMPV6_MGM_QUERY: 1656 case ICMPV6_MGM_QUERY:
1663 err = br_ip6_multicast_query(br, port, skb2); 1657 err = br_ip6_multicast_query(br, port, skb2, vid);
1664 break; 1658 break;
1665 case ICMPV6_MGM_REDUCTION: 1659 case ICMPV6_MGM_REDUCTION:
1666 { 1660 {
@@ -1681,7 +1675,7 @@ out:
1681#endif 1675#endif
1682 1676
1683int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, 1677int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1684 struct sk_buff *skb) 1678 struct sk_buff *skb, u16 vid)
1685{ 1679{
1686 BR_INPUT_SKB_CB(skb)->igmp = 0; 1680 BR_INPUT_SKB_CB(skb)->igmp = 0;
1687 BR_INPUT_SKB_CB(skb)->mrouters_only = 0; 1681 BR_INPUT_SKB_CB(skb)->mrouters_only = 0;
@@ -1691,10 +1685,10 @@ int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
1691 1685
1692 switch (skb->protocol) { 1686 switch (skb->protocol) {
1693 case htons(ETH_P_IP): 1687 case htons(ETH_P_IP):
1694 return br_multicast_ipv4_rcv(br, port, skb); 1688 return br_multicast_ipv4_rcv(br, port, skb, vid);
1695#if IS_ENABLED(CONFIG_IPV6) 1689#if IS_ENABLED(CONFIG_IPV6)
1696 case htons(ETH_P_IPV6): 1690 case htons(ETH_P_IPV6):
1697 return br_multicast_ipv6_rcv(br, port, skb); 1691 return br_multicast_ipv6_rcv(br, port, skb, vid);
1698#endif 1692#endif
1699 } 1693 }
1700 1694
@@ -2004,7 +1998,7 @@ int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val)
2004 u32 old; 1998 u32 old;
2005 struct net_bridge_mdb_htable *mdb; 1999 struct net_bridge_mdb_htable *mdb;
2006 2000
2007 spin_lock(&br->multicast_lock); 2001 spin_lock_bh(&br->multicast_lock);
2008 if (!netif_running(br->dev)) 2002 if (!netif_running(br->dev))
2009 goto unlock; 2003 goto unlock;
2010 2004
@@ -2036,7 +2030,7 @@ rollback:
2036 } 2030 }
2037 2031
2038unlock: 2032unlock:
2039 spin_unlock(&br->multicast_lock); 2033 spin_unlock_bh(&br->multicast_lock);
2040 2034
2041 return err; 2035 return err;
2042} 2036}
diff --git a/net/bridge/br_netfilter.c b/net/bridge/br_netfilter.c
index f87736270eaa..80cad2cf02a7 100644
--- a/net/bridge/br_netfilter.c
+++ b/net/bridge/br_netfilter.c
@@ -559,6 +559,8 @@ static struct net_device *setup_pre_routing(struct sk_buff *skb)
559 else if (skb->protocol == htons(ETH_P_PPP_SES)) 559 else if (skb->protocol == htons(ETH_P_PPP_SES))
560 nf_bridge->mask |= BRNF_PPPoE; 560 nf_bridge->mask |= BRNF_PPPoE;
561 561
562 /* Must drop socket now because of tproxy. */
563 skb_orphan(skb);
562 return skb->dev; 564 return skb->dev;
563} 565}
564 566
@@ -619,7 +621,7 @@ bad:
619 621
620/* Replicate the checks that IPv6 does on packet reception and pass the packet 622/* Replicate the checks that IPv6 does on packet reception and pass the packet
621 * to ip6tables, which doesn't support NAT, so things are fairly simple. */ 623 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
622static unsigned int br_nf_pre_routing_ipv6(unsigned int hook, 624static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
623 struct sk_buff *skb, 625 struct sk_buff *skb,
624 const struct net_device *in, 626 const struct net_device *in,
625 const struct net_device *out, 627 const struct net_device *out,
@@ -669,7 +671,8 @@ static unsigned int br_nf_pre_routing_ipv6(unsigned int hook,
669 * receiving device) to make netfilter happy, the REDIRECT 671 * receiving device) to make netfilter happy, the REDIRECT
670 * target in particular. Save the original destination IP 672 * target in particular. Save the original destination IP
671 * address to be able to detect DNAT afterwards. */ 673 * address to be able to detect DNAT afterwards. */
672static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb, 674static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
675 struct sk_buff *skb,
673 const struct net_device *in, 676 const struct net_device *in,
674 const struct net_device *out, 677 const struct net_device *out,
675 int (*okfn)(struct sk_buff *)) 678 int (*okfn)(struct sk_buff *))
@@ -691,7 +694,7 @@ static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
691 return NF_ACCEPT; 694 return NF_ACCEPT;
692 695
693 nf_bridge_pull_encap_header_rcsum(skb); 696 nf_bridge_pull_encap_header_rcsum(skb);
694 return br_nf_pre_routing_ipv6(hook, skb, in, out, okfn); 697 return br_nf_pre_routing_ipv6(ops, skb, in, out, okfn);
695 } 698 }
696 699
697 if (!brnf_call_iptables && !br->nf_call_iptables) 700 if (!brnf_call_iptables && !br->nf_call_iptables)
@@ -727,7 +730,8 @@ static unsigned int br_nf_pre_routing(unsigned int hook, struct sk_buff *skb,
727 * took place when the packet entered the bridge), but we 730 * took place when the packet entered the bridge), but we
728 * register an IPv4 PRE_ROUTING 'sabotage' hook that will 731 * register an IPv4 PRE_ROUTING 'sabotage' hook that will
729 * prevent this from happening. */ 732 * prevent this from happening. */
730static unsigned int br_nf_local_in(unsigned int hook, struct sk_buff *skb, 733static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
734 struct sk_buff *skb,
731 const struct net_device *in, 735 const struct net_device *in,
732 const struct net_device *out, 736 const struct net_device *out,
733 int (*okfn)(struct sk_buff *)) 737 int (*okfn)(struct sk_buff *))
@@ -765,7 +769,8 @@ static int br_nf_forward_finish(struct sk_buff *skb)
765 * but we are still able to filter on the 'real' indev/outdev 769 * but we are still able to filter on the 'real' indev/outdev
766 * because of the physdev module. For ARP, indev and outdev are the 770 * because of the physdev module. For ARP, indev and outdev are the
767 * bridge ports. */ 771 * bridge ports. */
768static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb, 772static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
773 struct sk_buff *skb,
769 const struct net_device *in, 774 const struct net_device *in,
770 const struct net_device *out, 775 const struct net_device *out,
771 int (*okfn)(struct sk_buff *)) 776 int (*okfn)(struct sk_buff *))
@@ -818,7 +823,8 @@ static unsigned int br_nf_forward_ip(unsigned int hook, struct sk_buff *skb,
818 return NF_STOLEN; 823 return NF_STOLEN;
819} 824}
820 825
821static unsigned int br_nf_forward_arp(unsigned int hook, struct sk_buff *skb, 826static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
827 struct sk_buff *skb,
822 const struct net_device *in, 828 const struct net_device *in,
823 const struct net_device *out, 829 const struct net_device *out,
824 int (*okfn)(struct sk_buff *)) 830 int (*okfn)(struct sk_buff *))
@@ -878,7 +884,8 @@ static int br_nf_dev_queue_xmit(struct sk_buff *skb)
878#endif 884#endif
879 885
880/* PF_BRIDGE/POST_ROUTING ********************************************/ 886/* PF_BRIDGE/POST_ROUTING ********************************************/
881static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb, 887static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
888 struct sk_buff *skb,
882 const struct net_device *in, 889 const struct net_device *in,
883 const struct net_device *out, 890 const struct net_device *out,
884 int (*okfn)(struct sk_buff *)) 891 int (*okfn)(struct sk_buff *))
@@ -923,7 +930,8 @@ static unsigned int br_nf_post_routing(unsigned int hook, struct sk_buff *skb,
923/* IP/SABOTAGE *****************************************************/ 930/* IP/SABOTAGE *****************************************************/
924/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING 931/* Don't hand locally destined packets to PF_INET(6)/PRE_ROUTING
925 * for the second time. */ 932 * for the second time. */
926static unsigned int ip_sabotage_in(unsigned int hook, struct sk_buff *skb, 933static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
934 struct sk_buff *skb,
927 const struct net_device *in, 935 const struct net_device *in,
928 const struct net_device *out, 936 const struct net_device *out,
929 int (*okfn)(struct sk_buff *)) 937 int (*okfn)(struct sk_buff *))
diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index e14c33b42f75..045d56eaeca2 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -343,10 +343,9 @@ static inline int br_is_root_bridge(const struct net_bridge *br)
343} 343}
344 344
345/* br_device.c */ 345/* br_device.c */
346extern void br_dev_setup(struct net_device *dev); 346void br_dev_setup(struct net_device *dev);
347extern void br_dev_delete(struct net_device *dev, struct list_head *list); 347void br_dev_delete(struct net_device *dev, struct list_head *list);
348extern netdev_tx_t br_dev_xmit(struct sk_buff *skb, 348netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
349 struct net_device *dev);
350#ifdef CONFIG_NET_POLL_CONTROLLER 349#ifdef CONFIG_NET_POLL_CONTROLLER
351static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 350static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
352 struct sk_buff *skb) 351 struct sk_buff *skb)
@@ -357,8 +356,8 @@ static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
357 netpoll_send_skb(np, skb); 356 netpoll_send_skb(np, skb);
358} 357}
359 358
360extern int br_netpoll_enable(struct net_bridge_port *p, gfp_t gfp); 359int br_netpoll_enable(struct net_bridge_port *p, gfp_t gfp);
361extern void br_netpoll_disable(struct net_bridge_port *p); 360void br_netpoll_disable(struct net_bridge_port *p);
362#else 361#else
363static inline void br_netpoll_send_skb(const struct net_bridge_port *p, 362static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
364 struct sk_buff *skb) 363 struct sk_buff *skb)
@@ -376,116 +375,109 @@ static inline void br_netpoll_disable(struct net_bridge_port *p)
376#endif 375#endif
377 376
378/* br_fdb.c */ 377/* br_fdb.c */
379extern int br_fdb_init(void); 378int br_fdb_init(void);
380extern void br_fdb_fini(void); 379void br_fdb_fini(void);
381extern void br_fdb_flush(struct net_bridge *br); 380void br_fdb_flush(struct net_bridge *br);
382extern void br_fdb_changeaddr(struct net_bridge_port *p, 381void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
383 const unsigned char *newaddr); 382void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
384extern void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr); 383void br_fdb_cleanup(unsigned long arg);
385extern void br_fdb_cleanup(unsigned long arg); 384void br_fdb_delete_by_port(struct net_bridge *br,
386extern void br_fdb_delete_by_port(struct net_bridge *br, 385 const struct net_bridge_port *p, int do_all);
387 const struct net_bridge_port *p, int do_all); 386struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
388extern struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br, 387 const unsigned char *addr, __u16 vid);
389 const unsigned char *addr, 388int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
390 __u16 vid); 389int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
391extern int br_fdb_test_addr(struct net_device *dev, unsigned char *addr); 390 unsigned long off);
392extern int br_fdb_fillbuf(struct net_bridge *br, void *buf, 391int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
393 unsigned long count, unsigned long off); 392 const unsigned char *addr, u16 vid);
394extern int br_fdb_insert(struct net_bridge *br, 393void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
395 struct net_bridge_port *source, 394 const unsigned char *addr, u16 vid);
396 const unsigned char *addr, 395int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vid);
397 u16 vid); 396
398extern void br_fdb_update(struct net_bridge *br, 397int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
399 struct net_bridge_port *source, 398 struct net_device *dev, const unsigned char *addr);
400 const unsigned char *addr, 399int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
401 u16 vid); 400 const unsigned char *addr, u16 nlh_flags);
402extern int fdb_delete_by_addr(struct net_bridge *br, const u8 *addr, u16 vid); 401int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
403 402 struct net_device *dev, int idx);
404extern int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
405 struct net_device *dev,
406 const unsigned char *addr);
407extern int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[],
408 struct net_device *dev,
409 const unsigned char *addr,
410 u16 nlh_flags);
411extern int br_fdb_dump(struct sk_buff *skb,
412 struct netlink_callback *cb,
413 struct net_device *dev,
414 int idx);
415 403
416/* br_forward.c */ 404/* br_forward.c */
417extern void br_deliver(const struct net_bridge_port *to, 405void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb);
418 struct sk_buff *skb); 406int br_dev_queue_push_xmit(struct sk_buff *skb);
419extern int br_dev_queue_push_xmit(struct sk_buff *skb); 407void br_forward(const struct net_bridge_port *to,
420extern void br_forward(const struct net_bridge_port *to,
421 struct sk_buff *skb, struct sk_buff *skb0); 408 struct sk_buff *skb, struct sk_buff *skb0);
422extern int br_forward_finish(struct sk_buff *skb); 409int br_forward_finish(struct sk_buff *skb);
423extern void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, 410void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast);
424 bool unicast); 411void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
425extern void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, 412 struct sk_buff *skb2, bool unicast);
426 struct sk_buff *skb2, bool unicast);
427 413
428/* br_if.c */ 414/* br_if.c */
429extern void br_port_carrier_check(struct net_bridge_port *p); 415void br_port_carrier_check(struct net_bridge_port *p);
430extern int br_add_bridge(struct net *net, const char *name); 416int br_add_bridge(struct net *net, const char *name);
431extern int br_del_bridge(struct net *net, const char *name); 417int br_del_bridge(struct net *net, const char *name);
432extern void br_net_exit(struct net *net); 418void br_net_exit(struct net *net);
433extern int br_add_if(struct net_bridge *br, 419int br_add_if(struct net_bridge *br, struct net_device *dev);
434 struct net_device *dev); 420int br_del_if(struct net_bridge *br, struct net_device *dev);
435extern int br_del_if(struct net_bridge *br, 421int br_min_mtu(const struct net_bridge *br);
436 struct net_device *dev); 422netdev_features_t br_features_recompute(struct net_bridge *br,
437extern int br_min_mtu(const struct net_bridge *br); 423 netdev_features_t features);
438extern netdev_features_t br_features_recompute(struct net_bridge *br,
439 netdev_features_t features);
440 424
441/* br_input.c */ 425/* br_input.c */
442extern int br_handle_frame_finish(struct sk_buff *skb); 426int br_handle_frame_finish(struct sk_buff *skb);
443extern rx_handler_result_t br_handle_frame(struct sk_buff **pskb); 427rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
428
429static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
430{
431 return rcu_dereference(dev->rx_handler) == br_handle_frame;
432}
433
434static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
435{
436 return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
437}
444 438
445/* br_ioctl.c */ 439/* br_ioctl.c */
446extern int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); 440int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
447extern int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *arg); 441int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
442 void __user *arg);
448 443
449/* br_multicast.c */ 444/* br_multicast.c */
450#ifdef CONFIG_BRIDGE_IGMP_SNOOPING 445#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
451extern unsigned int br_mdb_rehash_seq; 446extern unsigned int br_mdb_rehash_seq;
452extern int br_multicast_rcv(struct net_bridge *br, 447int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
453 struct net_bridge_port *port, 448 struct sk_buff *skb, u16 vid);
454 struct sk_buff *skb); 449struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
455extern struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 450 struct sk_buff *skb, u16 vid);
456 struct sk_buff *skb, u16 vid); 451void br_multicast_add_port(struct net_bridge_port *port);
457extern void br_multicast_add_port(struct net_bridge_port *port); 452void br_multicast_del_port(struct net_bridge_port *port);
458extern void br_multicast_del_port(struct net_bridge_port *port); 453void br_multicast_enable_port(struct net_bridge_port *port);
459extern void br_multicast_enable_port(struct net_bridge_port *port); 454void br_multicast_disable_port(struct net_bridge_port *port);
460extern void br_multicast_disable_port(struct net_bridge_port *port); 455void br_multicast_init(struct net_bridge *br);
461extern void br_multicast_init(struct net_bridge *br); 456void br_multicast_open(struct net_bridge *br);
462extern void br_multicast_open(struct net_bridge *br); 457void br_multicast_stop(struct net_bridge *br);
463extern void br_multicast_stop(struct net_bridge *br); 458void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
464extern void br_multicast_deliver(struct net_bridge_mdb_entry *mdst, 459 struct sk_buff *skb);
465 struct sk_buff *skb); 460void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
466extern void br_multicast_forward(struct net_bridge_mdb_entry *mdst, 461 struct sk_buff *skb, struct sk_buff *skb2);
467 struct sk_buff *skb, struct sk_buff *skb2); 462int br_multicast_set_router(struct net_bridge *br, unsigned long val);
468extern int br_multicast_set_router(struct net_bridge *br, unsigned long val); 463int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
469extern int br_multicast_set_port_router(struct net_bridge_port *p, 464int br_multicast_toggle(struct net_bridge *br, unsigned long val);
470 unsigned long val); 465int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
471extern int br_multicast_toggle(struct net_bridge *br, unsigned long val); 466int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
472extern int br_multicast_set_querier(struct net_bridge *br, unsigned long val); 467struct net_bridge_mdb_entry *
473extern int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val); 468br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
474extern struct net_bridge_mdb_entry *br_mdb_ip_get( 469struct net_bridge_mdb_entry *
475 struct net_bridge_mdb_htable *mdb, 470br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
476 struct br_ip *dst); 471 struct br_ip *group);
477extern struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 472void br_multicast_free_pg(struct rcu_head *head);
478 struct net_bridge_port *port, struct br_ip *group); 473struct net_bridge_port_group *
479extern void br_multicast_free_pg(struct rcu_head *head); 474br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
480extern struct net_bridge_port_group *br_multicast_new_port_group( 475 struct net_bridge_port_group __rcu *next,
481 struct net_bridge_port *port, 476 unsigned char state);
482 struct br_ip *group, 477void br_mdb_init(void);
483 struct net_bridge_port_group __rcu *next, 478void br_mdb_uninit(void);
484 unsigned char state); 479void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
485extern void br_mdb_init(void); 480 struct br_ip *group, int type);
486extern void br_mdb_uninit(void);
487extern void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
488 struct br_ip *group, int type);
489 481
490#define mlock_dereference(X, br) \ 482#define mlock_dereference(X, br) \
491 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) 483 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
@@ -522,7 +514,8 @@ static inline bool br_multicast_querier_exists(struct net_bridge *br,
522#else 514#else
523static inline int br_multicast_rcv(struct net_bridge *br, 515static inline int br_multicast_rcv(struct net_bridge *br,
524 struct net_bridge_port *port, 516 struct net_bridge_port *port,
525 struct sk_buff *skb) 517 struct sk_buff *skb,
518 u16 vid)
526{ 519{
527 return 0; 520 return 0;
528} 521}
@@ -590,22 +583,21 @@ static inline void br_mdb_uninit(void)
590 583
591/* br_vlan.c */ 584/* br_vlan.c */
592#ifdef CONFIG_BRIDGE_VLAN_FILTERING 585#ifdef CONFIG_BRIDGE_VLAN_FILTERING
593extern bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v, 586bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
594 struct sk_buff *skb, u16 *vid); 587 struct sk_buff *skb, u16 *vid);
595extern bool br_allowed_egress(struct net_bridge *br, 588bool br_allowed_egress(struct net_bridge *br, const struct net_port_vlans *v,
596 const struct net_port_vlans *v, 589 const struct sk_buff *skb);
597 const struct sk_buff *skb); 590struct sk_buff *br_handle_vlan(struct net_bridge *br,
598extern struct sk_buff *br_handle_vlan(struct net_bridge *br, 591 const struct net_port_vlans *v,
599 const struct net_port_vlans *v, 592 struct sk_buff *skb);
600 struct sk_buff *skb); 593int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags);
601extern int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags); 594int br_vlan_delete(struct net_bridge *br, u16 vid);
602extern int br_vlan_delete(struct net_bridge *br, u16 vid); 595void br_vlan_flush(struct net_bridge *br);
603extern void br_vlan_flush(struct net_bridge *br); 596int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
604extern int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val); 597int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags);
605extern int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags); 598int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
606extern int nbp_vlan_delete(struct net_bridge_port *port, u16 vid); 599void nbp_vlan_flush(struct net_bridge_port *port);
607extern void nbp_vlan_flush(struct net_bridge_port *port); 600bool nbp_vlan_find(struct net_bridge_port *port, u16 vid);
608extern bool nbp_vlan_find(struct net_bridge_port *port, u16 vid);
609 601
610static inline struct net_port_vlans *br_get_vlan_info( 602static inline struct net_port_vlans *br_get_vlan_info(
611 const struct net_bridge *br) 603 const struct net_bridge *br)
@@ -724,9 +716,9 @@ static inline u16 br_get_pvid(const struct net_port_vlans *v)
724 716
725/* br_netfilter.c */ 717/* br_netfilter.c */
726#ifdef CONFIG_BRIDGE_NETFILTER 718#ifdef CONFIG_BRIDGE_NETFILTER
727extern int br_netfilter_init(void); 719int br_netfilter_init(void);
728extern void br_netfilter_fini(void); 720void br_netfilter_fini(void);
729extern void br_netfilter_rtable_init(struct net_bridge *); 721void br_netfilter_rtable_init(struct net_bridge *);
730#else 722#else
731#define br_netfilter_init() (0) 723#define br_netfilter_init() (0)
732#define br_netfilter_fini() do { } while(0) 724#define br_netfilter_fini() do { } while(0)
@@ -734,43 +726,39 @@ extern void br_netfilter_rtable_init(struct net_bridge *);
734#endif 726#endif
735 727
736/* br_stp.c */ 728/* br_stp.c */
737extern void br_log_state(const struct net_bridge_port *p); 729void br_log_state(const struct net_bridge_port *p);
738extern struct net_bridge_port *br_get_port(struct net_bridge *br, 730struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
739 u16 port_no); 731void br_init_port(struct net_bridge_port *p);
740extern void br_init_port(struct net_bridge_port *p); 732void br_become_designated_port(struct net_bridge_port *p);
741extern void br_become_designated_port(struct net_bridge_port *p);
742 733
743extern void __br_set_forward_delay(struct net_bridge *br, unsigned long t); 734void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
744extern int br_set_forward_delay(struct net_bridge *br, unsigned long x); 735int br_set_forward_delay(struct net_bridge *br, unsigned long x);
745extern int br_set_hello_time(struct net_bridge *br, unsigned long x); 736int br_set_hello_time(struct net_bridge *br, unsigned long x);
746extern int br_set_max_age(struct net_bridge *br, unsigned long x); 737int br_set_max_age(struct net_bridge *br, unsigned long x);
747 738
748 739
749/* br_stp_if.c */ 740/* br_stp_if.c */
750extern void br_stp_enable_bridge(struct net_bridge *br); 741void br_stp_enable_bridge(struct net_bridge *br);
751extern void br_stp_disable_bridge(struct net_bridge *br); 742void br_stp_disable_bridge(struct net_bridge *br);
752extern void br_stp_set_enabled(struct net_bridge *br, unsigned long val); 743void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
753extern void br_stp_enable_port(struct net_bridge_port *p); 744void br_stp_enable_port(struct net_bridge_port *p);
754extern void br_stp_disable_port(struct net_bridge_port *p); 745void br_stp_disable_port(struct net_bridge_port *p);
755extern bool br_stp_recalculate_bridge_id(struct net_bridge *br); 746bool br_stp_recalculate_bridge_id(struct net_bridge *br);
756extern void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a); 747void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
757extern void br_stp_set_bridge_priority(struct net_bridge *br, 748void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
758 u16 newprio); 749int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
759extern int br_stp_set_port_priority(struct net_bridge_port *p, 750int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
760 unsigned long newprio); 751ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
761extern int br_stp_set_path_cost(struct net_bridge_port *p,
762 unsigned long path_cost);
763extern ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
764 752
765/* br_stp_bpdu.c */ 753/* br_stp_bpdu.c */
766struct stp_proto; 754struct stp_proto;
767extern void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb, 755void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
768 struct net_device *dev); 756 struct net_device *dev);
769 757
770/* br_stp_timer.c */ 758/* br_stp_timer.c */
771extern void br_stp_timer_init(struct net_bridge *br); 759void br_stp_timer_init(struct net_bridge *br);
772extern void br_stp_port_timer_init(struct net_bridge_port *p); 760void br_stp_port_timer_init(struct net_bridge_port *p);
773extern unsigned long br_timer_value(const struct timer_list *timer); 761unsigned long br_timer_value(const struct timer_list *timer);
774 762
775/* br.c */ 763/* br.c */
776#if IS_ENABLED(CONFIG_ATM_LANE) 764#if IS_ENABLED(CONFIG_ATM_LANE)
@@ -779,23 +767,23 @@ extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr)
779 767
780/* br_netlink.c */ 768/* br_netlink.c */
781extern struct rtnl_link_ops br_link_ops; 769extern struct rtnl_link_ops br_link_ops;
782extern int br_netlink_init(void); 770int br_netlink_init(void);
783extern void br_netlink_fini(void); 771void br_netlink_fini(void);
784extern void br_ifinfo_notify(int event, struct net_bridge_port *port); 772void br_ifinfo_notify(int event, struct net_bridge_port *port);
785extern int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg); 773int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg);
786extern int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg); 774int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg);
787extern int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, 775int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
788 struct net_device *dev, u32 filter_mask); 776 u32 filter_mask);
789 777
790#ifdef CONFIG_SYSFS 778#ifdef CONFIG_SYSFS
791/* br_sysfs_if.c */ 779/* br_sysfs_if.c */
792extern const struct sysfs_ops brport_sysfs_ops; 780extern const struct sysfs_ops brport_sysfs_ops;
793extern int br_sysfs_addif(struct net_bridge_port *p); 781int br_sysfs_addif(struct net_bridge_port *p);
794extern int br_sysfs_renameif(struct net_bridge_port *p); 782int br_sysfs_renameif(struct net_bridge_port *p);
795 783
796/* br_sysfs_br.c */ 784/* br_sysfs_br.c */
797extern int br_sysfs_addbr(struct net_device *dev); 785int br_sysfs_addbr(struct net_device *dev);
798extern void br_sysfs_delbr(struct net_device *dev); 786void br_sysfs_delbr(struct net_device *dev);
799 787
800#else 788#else
801 789
diff --git a/net/bridge/br_private_stp.h b/net/bridge/br_private_stp.h
index 0c0fe36e7aa9..2fe910c4e170 100644
--- a/net/bridge/br_private_stp.h
+++ b/net/bridge/br_private_stp.h
@@ -51,19 +51,19 @@ static inline int br_is_designated_port(const struct net_bridge_port *p)
51 51
52 52
53/* br_stp.c */ 53/* br_stp.c */
54extern void br_become_root_bridge(struct net_bridge *br); 54void br_become_root_bridge(struct net_bridge *br);
55extern void br_config_bpdu_generation(struct net_bridge *); 55void br_config_bpdu_generation(struct net_bridge *);
56extern void br_configuration_update(struct net_bridge *); 56void br_configuration_update(struct net_bridge *);
57extern void br_port_state_selection(struct net_bridge *); 57void br_port_state_selection(struct net_bridge *);
58extern void br_received_config_bpdu(struct net_bridge_port *p, 58void br_received_config_bpdu(struct net_bridge_port *p,
59 const struct br_config_bpdu *bpdu); 59 const struct br_config_bpdu *bpdu);
60extern void br_received_tcn_bpdu(struct net_bridge_port *p); 60void br_received_tcn_bpdu(struct net_bridge_port *p);
61extern void br_transmit_config(struct net_bridge_port *p); 61void br_transmit_config(struct net_bridge_port *p);
62extern void br_transmit_tcn(struct net_bridge *br); 62void br_transmit_tcn(struct net_bridge *br);
63extern void br_topology_change_detection(struct net_bridge *br); 63void br_topology_change_detection(struct net_bridge *br);
64 64
65/* br_stp_bpdu.c */ 65/* br_stp_bpdu.c */
66extern void br_send_config_bpdu(struct net_bridge_port *, struct br_config_bpdu *); 66void br_send_config_bpdu(struct net_bridge_port *, struct br_config_bpdu *);
67extern void br_send_tcn_bpdu(struct net_bridge_port *); 67void br_send_tcn_bpdu(struct net_bridge_port *);
68 68
69#endif 69#endif
diff --git a/net/bridge/br_stp_bpdu.c b/net/bridge/br_stp_bpdu.c
index 8660ea3be705..bdb459d21ad8 100644
--- a/net/bridge/br_stp_bpdu.c
+++ b/net/bridge/br_stp_bpdu.c
@@ -153,7 +153,7 @@ void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
153 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0) 153 if (buf[0] != 0 || buf[1] != 0 || buf[2] != 0)
154 goto err; 154 goto err;
155 155
156 p = br_port_get_rcu(dev); 156 p = br_port_get_check_rcu(dev);
157 if (!p) 157 if (!p)
158 goto err; 158 goto err;
159 159
diff --git a/net/bridge/br_vlan.c b/net/bridge/br_vlan.c
index 53f0990eab58..af5ebd18d705 100644
--- a/net/bridge/br_vlan.c
+++ b/net/bridge/br_vlan.c
@@ -34,7 +34,6 @@ static void __vlan_add_flags(struct net_port_vlans *v, u16 vid, u16 flags)
34 34
35static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags) 35static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
36{ 36{
37 const struct net_device_ops *ops;
38 struct net_bridge_port *p = NULL; 37 struct net_bridge_port *p = NULL;
39 struct net_bridge *br; 38 struct net_bridge *br;
40 struct net_device *dev; 39 struct net_device *dev;
@@ -53,17 +52,15 @@ static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
53 br = v->parent.br; 52 br = v->parent.br;
54 dev = br->dev; 53 dev = br->dev;
55 } 54 }
56 ops = dev->netdev_ops;
57 55
58 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) { 56 if (p) {
59 /* Add VLAN to the device filter if it is supported. 57 /* Add VLAN to the device filter if it is supported.
60 * Stricly speaking, this is not necessary now, since 58 * Stricly speaking, this is not necessary now, since
61 * devices are made promiscuous by the bridge, but if 59 * devices are made promiscuous by the bridge, but if
62 * that ever changes this code will allow tagged 60 * that ever changes this code will allow tagged
63 * traffic to enter the bridge. 61 * traffic to enter the bridge.
64 */ 62 */
65 err = ops->ndo_vlan_rx_add_vid(dev, htons(ETH_P_8021Q), 63 err = vlan_vid_add(dev, htons(ETH_P_8021Q), vid);
66 vid);
67 if (err) 64 if (err)
68 return err; 65 return err;
69 } 66 }
@@ -82,8 +79,8 @@ static int __vlan_add(struct net_port_vlans *v, u16 vid, u16 flags)
82 return 0; 79 return 0;
83 80
84out_filt: 81out_filt:
85 if (p && (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)) 82 if (p)
86 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid); 83 vlan_vid_del(dev, htons(ETH_P_8021Q), vid);
87 return err; 84 return err;
88} 85}
89 86
@@ -95,13 +92,8 @@ static int __vlan_del(struct net_port_vlans *v, u16 vid)
95 __vlan_delete_pvid(v, vid); 92 __vlan_delete_pvid(v, vid);
96 clear_bit(vid, v->untagged_bitmap); 93 clear_bit(vid, v->untagged_bitmap);
97 94
98 if (v->port_idx) { 95 if (v->port_idx)
99 struct net_device *dev = v->parent.port->dev; 96 vlan_vid_del(v->parent.port->dev, htons(ETH_P_8021Q), vid);
100 const struct net_device_ops *ops = dev->netdev_ops;
101
102 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
103 ops->ndo_vlan_rx_kill_vid(dev, htons(ETH_P_8021Q), vid);
104 }
105 97
106 clear_bit(vid, v->vlan_bitmap); 98 clear_bit(vid, v->vlan_bitmap);
107 v->num_vlans--; 99 v->num_vlans--;
@@ -398,6 +390,7 @@ int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
398void nbp_vlan_flush(struct net_bridge_port *port) 390void nbp_vlan_flush(struct net_bridge_port *port)
399{ 391{
400 struct net_port_vlans *pv; 392 struct net_port_vlans *pv;
393 u16 vid;
401 394
402 ASSERT_RTNL(); 395 ASSERT_RTNL();
403 396
@@ -405,6 +398,9 @@ void nbp_vlan_flush(struct net_bridge_port *port)
405 if (!pv) 398 if (!pv)
406 return; 399 return;
407 400
401 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID)
402 vlan_vid_del(port->dev, htons(ETH_P_8021Q), vid);
403
408 __vlan_flush(pv); 404 __vlan_flush(pv);
409} 405}
410 406
diff --git a/net/bridge/netfilter/Kconfig b/net/bridge/netfilter/Kconfig
index a9aff9c7d027..5ca74a0e595f 100644
--- a/net/bridge/netfilter/Kconfig
+++ b/net/bridge/netfilter/Kconfig
@@ -1,6 +1,10 @@
1# 1#
2# Bridge netfilter configuration 2# Bridge netfilter configuration
3# 3#
4#
5config NF_TABLES_BRIDGE
6 depends on NF_TABLES
7 tristate "Ethernet Bridge nf_tables support"
4 8
5menuconfig BRIDGE_NF_EBTABLES 9menuconfig BRIDGE_NF_EBTABLES
6 tristate "Ethernet Bridge tables (ebtables) support" 10 tristate "Ethernet Bridge tables (ebtables) support"
diff --git a/net/bridge/netfilter/Makefile b/net/bridge/netfilter/Makefile
index 0718699540b0..ea7629f58b3d 100644
--- a/net/bridge/netfilter/Makefile
+++ b/net/bridge/netfilter/Makefile
@@ -2,6 +2,8 @@
2# Makefile for the netfilter modules for Link Layer filtering on a bridge. 2# Makefile for the netfilter modules for Link Layer filtering on a bridge.
3# 3#
4 4
5obj-$(CONFIG_NF_TABLES_BRIDGE) += nf_tables_bridge.o
6
5obj-$(CONFIG_BRIDGE_NF_EBTABLES) += ebtables.o 7obj-$(CONFIG_BRIDGE_NF_EBTABLES) += ebtables.o
6 8
7# tables 9# tables
diff --git a/net/bridge/netfilter/ebt_among.c b/net/bridge/netfilter/ebt_among.c
index 8b84c581be30..3fb3c848affe 100644
--- a/net/bridge/netfilter/ebt_among.c
+++ b/net/bridge/netfilter/ebt_among.c
@@ -28,7 +28,7 @@ static bool ebt_mac_wormhash_contains(const struct ebt_mac_wormhash *wh,
28 uint32_t cmp[2] = { 0, 0 }; 28 uint32_t cmp[2] = { 0, 0 };
29 int key = ((const unsigned char *)mac)[5]; 29 int key = ((const unsigned char *)mac)[5];
30 30
31 memcpy(((char *) cmp) + 2, mac, 6); 31 memcpy(((char *) cmp) + 2, mac, ETH_ALEN);
32 start = wh->table[key]; 32 start = wh->table[key];
33 limit = wh->table[key + 1]; 33 limit = wh->table[key + 1];
34 if (ip) { 34 if (ip) {
diff --git a/net/bridge/netfilter/ebt_ip6.c b/net/bridge/netfilter/ebt_ip6.c
index 99c85668f551..17fd5f2cb4b8 100644
--- a/net/bridge/netfilter/ebt_ip6.c
+++ b/net/bridge/netfilter/ebt_ip6.c
@@ -48,10 +48,12 @@ ebt_ip6_mt(const struct sk_buff *skb, struct xt_action_param *par)
48 if (info->bitmask & EBT_IP6_TCLASS && 48 if (info->bitmask & EBT_IP6_TCLASS &&
49 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS)) 49 FWINV(info->tclass != ipv6_get_dsfield(ih6), EBT_IP6_TCLASS))
50 return false; 50 return false;
51 if (FWINV(ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk, 51 if ((info->bitmask & EBT_IP6_SOURCE &&
52 &info->saddr), EBT_IP6_SOURCE) || 52 FWINV(ipv6_masked_addr_cmp(&ih6->saddr, &info->smsk,
53 &info->saddr), EBT_IP6_SOURCE)) ||
54 (info->bitmask & EBT_IP6_DEST &&
53 FWINV(ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk, 55 FWINV(ipv6_masked_addr_cmp(&ih6->daddr, &info->dmsk,
54 &info->daddr), EBT_IP6_DEST)) 56 &info->daddr), EBT_IP6_DEST)))
55 return false; 57 return false;
56 if (info->bitmask & EBT_IP6_PROTO) { 58 if (info->bitmask & EBT_IP6_PROTO) {
57 uint8_t nexthdr = ih6->nexthdr; 59 uint8_t nexthdr = ih6->nexthdr;
diff --git a/net/bridge/netfilter/ebt_ulog.c b/net/bridge/netfilter/ebt_ulog.c
index 518093802d1d..7c470c371e14 100644
--- a/net/bridge/netfilter/ebt_ulog.c
+++ b/net/bridge/netfilter/ebt_ulog.c
@@ -181,6 +181,7 @@ static void ebt_ulog_packet(struct net *net, unsigned int hooknr,
181 ub->qlen++; 181 ub->qlen++;
182 182
183 pm = nlmsg_data(nlh); 183 pm = nlmsg_data(nlh);
184 memset(pm, 0, sizeof(*pm));
184 185
185 /* Fill in the ulog data */ 186 /* Fill in the ulog data */
186 pm->version = EBT_ULOG_VERSION; 187 pm->version = EBT_ULOG_VERSION;
@@ -193,8 +194,6 @@ static void ebt_ulog_packet(struct net *net, unsigned int hooknr,
193 pm->hook = hooknr; 194 pm->hook = hooknr;
194 if (uloginfo->prefix != NULL) 195 if (uloginfo->prefix != NULL)
195 strcpy(pm->prefix, uloginfo->prefix); 196 strcpy(pm->prefix, uloginfo->prefix);
196 else
197 *(pm->prefix) = '\0';
198 197
199 if (in) { 198 if (in) {
200 strcpy(pm->physindev, in->name); 199 strcpy(pm->physindev, in->name);
@@ -204,16 +203,14 @@ static void ebt_ulog_packet(struct net *net, unsigned int hooknr,
204 strcpy(pm->indev, br_port_get_rcu(in)->br->dev->name); 203 strcpy(pm->indev, br_port_get_rcu(in)->br->dev->name);
205 else 204 else
206 strcpy(pm->indev, in->name); 205 strcpy(pm->indev, in->name);
207 } else 206 }
208 pm->indev[0] = pm->physindev[0] = '\0';
209 207
210 if (out) { 208 if (out) {
211 /* If out exists, then out is a bridge port */ 209 /* If out exists, then out is a bridge port */
212 strcpy(pm->physoutdev, out->name); 210 strcpy(pm->physoutdev, out->name);
213 /* rcu_read_lock()ed by nf_hook_slow */ 211 /* rcu_read_lock()ed by nf_hook_slow */
214 strcpy(pm->outdev, br_port_get_rcu(out)->br->dev->name); 212 strcpy(pm->outdev, br_port_get_rcu(out)->br->dev->name);
215 } else 213 }
216 pm->outdev[0] = pm->physoutdev[0] = '\0';
217 214
218 if (skb_copy_bits(skb, -ETH_HLEN, pm->data, copy_len) < 0) 215 if (skb_copy_bits(skb, -ETH_HLEN, pm->data, copy_len) < 0)
219 BUG(); 216 BUG();
diff --git a/net/bridge/netfilter/ebtable_filter.c b/net/bridge/netfilter/ebtable_filter.c
index 94b2b700cff8..bb2da7b706e7 100644
--- a/net/bridge/netfilter/ebtable_filter.c
+++ b/net/bridge/netfilter/ebtable_filter.c
@@ -60,17 +60,21 @@ static const struct ebt_table frame_filter =
60}; 60};
61 61
62static unsigned int 62static unsigned int
63ebt_in_hook(unsigned int hook, struct sk_buff *skb, const struct net_device *in, 63ebt_in_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
64 const struct net_device *out, int (*okfn)(struct sk_buff *)) 64 const struct net_device *in, const struct net_device *out,
65 int (*okfn)(struct sk_buff *))
65{ 66{
66 return ebt_do_table(hook, skb, in, out, dev_net(in)->xt.frame_filter); 67 return ebt_do_table(ops->hooknum, skb, in, out,
68 dev_net(in)->xt.frame_filter);
67} 69}
68 70
69static unsigned int 71static unsigned int
70ebt_out_hook(unsigned int hook, struct sk_buff *skb, const struct net_device *in, 72ebt_out_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
71 const struct net_device *out, int (*okfn)(struct sk_buff *)) 73 const struct net_device *in, const struct net_device *out,
74 int (*okfn)(struct sk_buff *))
72{ 75{
73 return ebt_do_table(hook, skb, in, out, dev_net(out)->xt.frame_filter); 76 return ebt_do_table(ops->hooknum, skb, in, out,
77 dev_net(out)->xt.frame_filter);
74} 78}
75 79
76static struct nf_hook_ops ebt_ops_filter[] __read_mostly = { 80static struct nf_hook_ops ebt_ops_filter[] __read_mostly = {
diff --git a/net/bridge/netfilter/ebtable_nat.c b/net/bridge/netfilter/ebtable_nat.c
index 322555acdd40..bd238f1f105b 100644
--- a/net/bridge/netfilter/ebtable_nat.c
+++ b/net/bridge/netfilter/ebtable_nat.c
@@ -60,17 +60,21 @@ static struct ebt_table frame_nat =
60}; 60};
61 61
62static unsigned int 62static unsigned int
63ebt_nat_in(unsigned int hook, struct sk_buff *skb, const struct net_device *in 63ebt_nat_in(const struct nf_hook_ops *ops, struct sk_buff *skb,
64 , const struct net_device *out, int (*okfn)(struct sk_buff *)) 64 const struct net_device *in, const struct net_device *out,
65 int (*okfn)(struct sk_buff *))
65{ 66{
66 return ebt_do_table(hook, skb, in, out, dev_net(in)->xt.frame_nat); 67 return ebt_do_table(ops->hooknum, skb, in, out,
68 dev_net(in)->xt.frame_nat);
67} 69}
68 70
69static unsigned int 71static unsigned int
70ebt_nat_out(unsigned int hook, struct sk_buff *skb, const struct net_device *in 72ebt_nat_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
71 , const struct net_device *out, int (*okfn)(struct sk_buff *)) 73 const struct net_device *in, const struct net_device *out,
74 int (*okfn)(struct sk_buff *))
72{ 75{
73 return ebt_do_table(hook, skb, in, out, dev_net(out)->xt.frame_nat); 76 return ebt_do_table(ops->hooknum, skb, in, out,
77 dev_net(out)->xt.frame_nat);
74} 78}
75 79
76static struct nf_hook_ops ebt_ops_nat[] __read_mostly = { 80static struct nf_hook_ops ebt_ops_nat[] __read_mostly = {
diff --git a/net/bridge/netfilter/nf_tables_bridge.c b/net/bridge/netfilter/nf_tables_bridge.c
new file mode 100644
index 000000000000..cf54b22818c8
--- /dev/null
+++ b/net/bridge/netfilter/nf_tables_bridge.c
@@ -0,0 +1,102 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netfilter_bridge.h>
15#include <net/netfilter/nf_tables.h>
16
17static struct nft_af_info nft_af_bridge __read_mostly = {
18 .family = NFPROTO_BRIDGE,
19 .nhooks = NF_BR_NUMHOOKS,
20 .owner = THIS_MODULE,
21};
22
23static int nf_tables_bridge_init_net(struct net *net)
24{
25 net->nft.bridge = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
26 if (net->nft.bridge == NULL)
27 return -ENOMEM;
28
29 memcpy(net->nft.bridge, &nft_af_bridge, sizeof(nft_af_bridge));
30
31 if (nft_register_afinfo(net, net->nft.bridge) < 0)
32 goto err;
33
34 return 0;
35err:
36 kfree(net->nft.bridge);
37 return -ENOMEM;
38}
39
40static void nf_tables_bridge_exit_net(struct net *net)
41{
42 nft_unregister_afinfo(net->nft.bridge);
43 kfree(net->nft.bridge);
44}
45
46static struct pernet_operations nf_tables_bridge_net_ops = {
47 .init = nf_tables_bridge_init_net,
48 .exit = nf_tables_bridge_exit_net,
49};
50
51static unsigned int
52nft_do_chain_bridge(const struct nf_hook_ops *ops,
53 struct sk_buff *skb,
54 const struct net_device *in,
55 const struct net_device *out,
56 int (*okfn)(struct sk_buff *))
57{
58 struct nft_pktinfo pkt;
59
60 nft_set_pktinfo(&pkt, ops, skb, in, out);
61
62 return nft_do_chain_pktinfo(&pkt, ops);
63}
64
65static struct nf_chain_type filter_bridge = {
66 .family = NFPROTO_BRIDGE,
67 .name = "filter",
68 .type = NFT_CHAIN_T_DEFAULT,
69 .hook_mask = (1 << NF_BR_LOCAL_IN) |
70 (1 << NF_BR_FORWARD) |
71 (1 << NF_BR_LOCAL_OUT),
72 .fn = {
73 [NF_BR_LOCAL_IN] = nft_do_chain_bridge,
74 [NF_BR_FORWARD] = nft_do_chain_bridge,
75 [NF_BR_LOCAL_OUT] = nft_do_chain_bridge,
76 },
77};
78
79static int __init nf_tables_bridge_init(void)
80{
81 int ret;
82
83 nft_register_chain_type(&filter_bridge);
84 ret = register_pernet_subsys(&nf_tables_bridge_net_ops);
85 if (ret < 0)
86 nft_unregister_chain_type(&filter_bridge);
87
88 return ret;
89}
90
91static void __exit nf_tables_bridge_exit(void)
92{
93 unregister_pernet_subsys(&nf_tables_bridge_net_ops);
94 nft_unregister_chain_type(&filter_bridge);
95}
96
97module_init(nf_tables_bridge_init);
98module_exit(nf_tables_bridge_exit);
99
100MODULE_LICENSE("GPL");
101MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
102MODULE_ALIAS_NFT_FAMILY(AF_BRIDGE);
diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c
index 05a41c7ec304..d6be3edb7a43 100644
--- a/net/caif/caif_socket.c
+++ b/net/caif/caif_socket.c
@@ -286,8 +286,6 @@ static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock,
286 if (m->msg_flags&MSG_OOB) 286 if (m->msg_flags&MSG_OOB)
287 goto read_error; 287 goto read_error;
288 288
289 m->msg_namelen = 0;
290
291 skb = skb_recv_datagram(sk, flags, 0 , &ret); 289 skb = skb_recv_datagram(sk, flags, 0 , &ret);
292 if (!skb) 290 if (!skb)
293 goto read_error; 291 goto read_error;
@@ -361,8 +359,6 @@ static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
361 if (flags&MSG_OOB) 359 if (flags&MSG_OOB)
362 goto out; 360 goto out;
363 361
364 msg->msg_namelen = 0;
365
366 /* 362 /*
367 * Lock the socket to prevent queue disordering 363 * Lock the socket to prevent queue disordering
368 * while sleeps in memcpy_tomsg 364 * while sleeps in memcpy_tomsg
diff --git a/net/caif/cfpkt_skbuff.c b/net/caif/cfpkt_skbuff.c
index 6493351f39c6..1be0b521ac49 100644
--- a/net/caif/cfpkt_skbuff.c
+++ b/net/caif/cfpkt_skbuff.c
@@ -203,20 +203,10 @@ int cfpkt_add_body(struct cfpkt *pkt, const void *data, u16 len)
203 PKT_ERROR(pkt, "cow failed\n"); 203 PKT_ERROR(pkt, "cow failed\n");
204 return -EPROTO; 204 return -EPROTO;
205 } 205 }
206 /*
207 * Is the SKB non-linear after skb_cow_data()? If so, we are
208 * going to add data to the last SKB, so we need to adjust
209 * lengths of the top SKB.
210 */
211 if (lastskb != skb) {
212 pr_warn("Packet is non-linear\n");
213 skb->len += len;
214 skb->data_len += len;
215 }
216 } 206 }
217 207
218 /* All set to put the last SKB and optionally write data there. */ 208 /* All set to put the last SKB and optionally write data there. */
219 to = skb_put(lastskb, len); 209 to = pskb_put(skb, lastskb, len);
220 if (likely(data)) 210 if (likely(data))
221 memcpy(to, data, len); 211 memcpy(to, data, len);
222 return 0; 212 return 0;
diff --git a/net/can/af_can.c b/net/can/af_can.c
index 3ab8dd2e1282..d249874a366d 100644
--- a/net/can/af_can.c
+++ b/net/can/af_can.c
@@ -420,7 +420,7 @@ static struct hlist_head *find_rcv_list(canid_t *can_id, canid_t *mask,
420 * @mask: CAN mask (see description) 420 * @mask: CAN mask (see description)
421 * @func: callback function on filter match 421 * @func: callback function on filter match
422 * @data: returned parameter for callback function 422 * @data: returned parameter for callback function
423 * @ident: string for calling module indentification 423 * @ident: string for calling module identification
424 * 424 *
425 * Description: 425 * Description:
426 * Invokes the callback function with the received sk_buff and the given 426 * Invokes the callback function with the received sk_buff and the given
diff --git a/net/can/af_can.h b/net/can/af_can.h
index 1dccb4c33894..6de58b40535c 100644
--- a/net/can/af_can.h
+++ b/net/can/af_can.h
@@ -108,9 +108,9 @@ struct s_pstats {
108extern struct dev_rcv_lists can_rx_alldev_list; 108extern struct dev_rcv_lists can_rx_alldev_list;
109 109
110/* function prototypes for the CAN networklayer procfs (proc.c) */ 110/* function prototypes for the CAN networklayer procfs (proc.c) */
111extern void can_init_proc(void); 111void can_init_proc(void);
112extern void can_remove_proc(void); 112void can_remove_proc(void);
113extern void can_stat_update(unsigned long data); 113void can_stat_update(unsigned long data);
114 114
115/* structures and variables from af_can.c needed in proc.c for reading */ 115/* structures and variables from af_can.c needed in proc.c for reading */
116extern struct timer_list can_stattimer; /* timer for statistics update */ 116extern struct timer_list can_stattimer; /* timer for statistics update */
diff --git a/net/ceph/auth_none.h b/net/ceph/auth_none.h
index ed7d088b1bc9..059a3ce4b53f 100644
--- a/net/ceph/auth_none.h
+++ b/net/ceph/auth_none.h
@@ -23,7 +23,7 @@ struct ceph_auth_none_info {
23 struct ceph_none_authorizer au; /* we only need one; it's static */ 23 struct ceph_none_authorizer au; /* we only need one; it's static */
24}; 24};
25 25
26extern int ceph_auth_none_init(struct ceph_auth_client *ac); 26int ceph_auth_none_init(struct ceph_auth_client *ac);
27 27
28#endif 28#endif
29 29
diff --git a/net/ceph/auth_x.h b/net/ceph/auth_x.h
index c5a058da7ac8..65ee72082d99 100644
--- a/net/ceph/auth_x.h
+++ b/net/ceph/auth_x.h
@@ -45,7 +45,7 @@ struct ceph_x_info {
45 struct ceph_x_authorizer auth_authorizer; 45 struct ceph_x_authorizer auth_authorizer;
46}; 46};
47 47
48extern int ceph_x_init(struct ceph_auth_client *ac); 48int ceph_x_init(struct ceph_auth_client *ac);
49 49
50#endif 50#endif
51 51
diff --git a/net/ceph/crypto.h b/net/ceph/crypto.h
index 3572dc518bc9..d1498224c49d 100644
--- a/net/ceph/crypto.h
+++ b/net/ceph/crypto.h
@@ -20,34 +20,32 @@ static inline void ceph_crypto_key_destroy(struct ceph_crypto_key *key)
20 kfree(key->key); 20 kfree(key->key);
21} 21}
22 22
23extern int ceph_crypto_key_clone(struct ceph_crypto_key *dst, 23int ceph_crypto_key_clone(struct ceph_crypto_key *dst,
24 const struct ceph_crypto_key *src); 24 const struct ceph_crypto_key *src);
25extern int ceph_crypto_key_encode(struct ceph_crypto_key *key, 25int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end);
26 void **p, void *end); 26int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end);
27extern int ceph_crypto_key_decode(struct ceph_crypto_key *key, 27int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *in);
28 void **p, void *end);
29extern int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *in);
30 28
31/* crypto.c */ 29/* crypto.c */
32extern int ceph_decrypt(struct ceph_crypto_key *secret, 30int ceph_decrypt(struct ceph_crypto_key *secret,
33 void *dst, size_t *dst_len, 31 void *dst, size_t *dst_len,
34 const void *src, size_t src_len); 32 const void *src, size_t src_len);
35extern int ceph_encrypt(struct ceph_crypto_key *secret, 33int ceph_encrypt(struct ceph_crypto_key *secret,
36 void *dst, size_t *dst_len, 34 void *dst, size_t *dst_len,
37 const void *src, size_t src_len); 35 const void *src, size_t src_len);
38extern int ceph_decrypt2(struct ceph_crypto_key *secret, 36int ceph_decrypt2(struct ceph_crypto_key *secret,
39 void *dst1, size_t *dst1_len, 37 void *dst1, size_t *dst1_len,
40 void *dst2, size_t *dst2_len, 38 void *dst2, size_t *dst2_len,
41 const void *src, size_t src_len); 39 const void *src, size_t src_len);
42extern int ceph_encrypt2(struct ceph_crypto_key *secret, 40int ceph_encrypt2(struct ceph_crypto_key *secret,
43 void *dst, size_t *dst_len, 41 void *dst, size_t *dst_len,
44 const void *src1, size_t src1_len, 42 const void *src1, size_t src1_len,
45 const void *src2, size_t src2_len); 43 const void *src2, size_t src2_len);
46extern int ceph_crypto_init(void); 44int ceph_crypto_init(void);
47extern void ceph_crypto_shutdown(void); 45void ceph_crypto_shutdown(void);
48 46
49/* armor.c */ 47/* armor.c */
50extern int ceph_armor(char *dst, const char *src, const char *end); 48int ceph_armor(char *dst, const char *src, const char *end);
51extern int ceph_unarmor(char *dst, const char *src, const char *end); 49int ceph_unarmor(char *dst, const char *src, const char *end);
52 50
53#endif 51#endif
diff --git a/net/compat.c b/net/compat.c
index 89032580bd1d..dd32e34c1e2c 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -72,7 +72,7 @@ int get_compat_msghdr(struct msghdr *kmsg, struct compat_msghdr __user *umsg)
72 __get_user(kmsg->msg_flags, &umsg->msg_flags)) 72 __get_user(kmsg->msg_flags, &umsg->msg_flags))
73 return -EFAULT; 73 return -EFAULT;
74 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 74 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
75 return -EINVAL; 75 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
76 kmsg->msg_name = compat_ptr(tmp1); 76 kmsg->msg_name = compat_ptr(tmp1);
77 kmsg->msg_iov = compat_ptr(tmp2); 77 kmsg->msg_iov = compat_ptr(tmp2);
78 kmsg->msg_control = compat_ptr(tmp3); 78 kmsg->msg_control = compat_ptr(tmp3);
@@ -93,7 +93,8 @@ int verify_compat_iovec(struct msghdr *kern_msg, struct iovec *kern_iov,
93 if (err < 0) 93 if (err < 0)
94 return err; 94 return err;
95 } 95 }
96 kern_msg->msg_name = kern_address; 96 if (kern_msg->msg_name)
97 kern_msg->msg_name = kern_address;
97 } else 98 } else
98 kern_msg->msg_name = NULL; 99 kern_msg->msg_name = NULL;
99 100
diff --git a/net/core/datagram.c b/net/core/datagram.c
index af814e764206..a16ed7bbe376 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -577,7 +577,7 @@ EXPORT_SYMBOL(skb_copy_datagram_from_iovec);
577/** 577/**
578 * zerocopy_sg_from_iovec - Build a zerocopy datagram from an iovec 578 * zerocopy_sg_from_iovec - Build a zerocopy datagram from an iovec
579 * @skb: buffer to copy 579 * @skb: buffer to copy
580 * @from: io vector to copy to 580 * @from: io vector to copy from
581 * @offset: offset in the io vector to start copying from 581 * @offset: offset in the io vector to start copying from
582 * @count: amount of vectors to copy to buffer from 582 * @count: amount of vectors to copy to buffer from
583 * 583 *
diff --git a/net/core/dev.c b/net/core/dev.c
index 3430b1ed12e5..0ce469e5ec80 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -131,6 +131,7 @@
131#include <linux/static_key.h> 131#include <linux/static_key.h>
132#include <linux/hashtable.h> 132#include <linux/hashtable.h>
133#include <linux/vmalloc.h> 133#include <linux/vmalloc.h>
134#include <linux/if_macvlan.h>
134 135
135#include "net-sysfs.h" 136#include "net-sysfs.h"
136 137
@@ -1203,7 +1204,7 @@ void netdev_state_change(struct net_device *dev)
1203{ 1204{
1204 if (dev->flags & IFF_UP) { 1205 if (dev->flags & IFF_UP) {
1205 call_netdevice_notifiers(NETDEV_CHANGE, dev); 1206 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1206 rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 1207 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1207 } 1208 }
1208} 1209}
1209EXPORT_SYMBOL(netdev_state_change); 1210EXPORT_SYMBOL(netdev_state_change);
@@ -1293,7 +1294,7 @@ int dev_open(struct net_device *dev)
1293 if (ret < 0) 1294 if (ret < 0)
1294 return ret; 1295 return ret;
1295 1296
1296 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 1297 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1297 call_netdevice_notifiers(NETDEV_UP, dev); 1298 call_netdevice_notifiers(NETDEV_UP, dev);
1298 1299
1299 return ret; 1300 return ret;
@@ -1307,7 +1308,7 @@ static int __dev_close_many(struct list_head *head)
1307 ASSERT_RTNL(); 1308 ASSERT_RTNL();
1308 might_sleep(); 1309 might_sleep();
1309 1310
1310 list_for_each_entry(dev, head, unreg_list) { 1311 list_for_each_entry(dev, head, close_list) {
1311 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); 1312 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1312 1313
1313 clear_bit(__LINK_STATE_START, &dev->state); 1314 clear_bit(__LINK_STATE_START, &dev->state);
@@ -1323,7 +1324,7 @@ static int __dev_close_many(struct list_head *head)
1323 1324
1324 dev_deactivate_many(head); 1325 dev_deactivate_many(head);
1325 1326
1326 list_for_each_entry(dev, head, unreg_list) { 1327 list_for_each_entry(dev, head, close_list) {
1327 const struct net_device_ops *ops = dev->netdev_ops; 1328 const struct net_device_ops *ops = dev->netdev_ops;
1328 1329
1329 /* 1330 /*
@@ -1351,7 +1352,7 @@ static int __dev_close(struct net_device *dev)
1351 /* Temporarily disable netpoll until the interface is down */ 1352 /* Temporarily disable netpoll until the interface is down */
1352 netpoll_rx_disable(dev); 1353 netpoll_rx_disable(dev);
1353 1354
1354 list_add(&dev->unreg_list, &single); 1355 list_add(&dev->close_list, &single);
1355 retval = __dev_close_many(&single); 1356 retval = __dev_close_many(&single);
1356 list_del(&single); 1357 list_del(&single);
1357 1358
@@ -1362,21 +1363,20 @@ static int __dev_close(struct net_device *dev)
1362static int dev_close_many(struct list_head *head) 1363static int dev_close_many(struct list_head *head)
1363{ 1364{
1364 struct net_device *dev, *tmp; 1365 struct net_device *dev, *tmp;
1365 LIST_HEAD(tmp_list);
1366 1366
1367 list_for_each_entry_safe(dev, tmp, head, unreg_list) 1367 /* Remove the devices that don't need to be closed */
1368 list_for_each_entry_safe(dev, tmp, head, close_list)
1368 if (!(dev->flags & IFF_UP)) 1369 if (!(dev->flags & IFF_UP))
1369 list_move(&dev->unreg_list, &tmp_list); 1370 list_del_init(&dev->close_list);
1370 1371
1371 __dev_close_many(head); 1372 __dev_close_many(head);
1372 1373
1373 list_for_each_entry(dev, head, unreg_list) { 1374 list_for_each_entry_safe(dev, tmp, head, close_list) {
1374 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 1375 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1375 call_netdevice_notifiers(NETDEV_DOWN, dev); 1376 call_netdevice_notifiers(NETDEV_DOWN, dev);
1377 list_del_init(&dev->close_list);
1376 } 1378 }
1377 1379
1378 /* rollback_registered_many needs the complete original list */
1379 list_splice(&tmp_list, head);
1380 return 0; 1380 return 0;
1381} 1381}
1382 1382
@@ -1397,7 +1397,7 @@ int dev_close(struct net_device *dev)
1397 /* Block netpoll rx while the interface is going down */ 1397 /* Block netpoll rx while the interface is going down */
1398 netpoll_rx_disable(dev); 1398 netpoll_rx_disable(dev);
1399 1399
1400 list_add(&dev->unreg_list, &single); 1400 list_add(&dev->close_list, &single);
1401 dev_close_many(&single); 1401 dev_close_many(&single);
1402 list_del(&single); 1402 list_del(&single);
1403 1403
@@ -1425,6 +1425,10 @@ void dev_disable_lro(struct net_device *dev)
1425 if (is_vlan_dev(dev)) 1425 if (is_vlan_dev(dev))
1426 dev = vlan_dev_real_dev(dev); 1426 dev = vlan_dev_real_dev(dev);
1427 1427
1428 /* the same for macvlan devices */
1429 if (netif_is_macvlan(dev))
1430 dev = macvlan_dev_real_dev(dev);
1431
1428 dev->wanted_features &= ~NETIF_F_LRO; 1432 dev->wanted_features &= ~NETIF_F_LRO;
1429 netdev_update_features(dev); 1433 netdev_update_features(dev);
1430 1434
@@ -1691,13 +1695,9 @@ int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1691 kfree_skb(skb); 1695 kfree_skb(skb);
1692 return NET_RX_DROP; 1696 return NET_RX_DROP;
1693 } 1697 }
1694 skb->protocol = eth_type_trans(skb, dev);
1695 1698
1696 /* eth_type_trans() can set pkt_type.
1697 * call skb_scrub_packet() after it to clear pkt_type _after_ calling
1698 * eth_type_trans().
1699 */
1700 skb_scrub_packet(skb, true); 1699 skb_scrub_packet(skb, true);
1700 skb->protocol = eth_type_trans(skb, dev);
1701 1701
1702 return netif_rx(skb); 1702 return netif_rx(skb);
1703} 1703}
@@ -2378,6 +2378,8 @@ struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2378 } 2378 }
2379 2379
2380 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); 2380 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
2381 SKB_GSO_CB(skb)->encap_level = 0;
2382
2381 skb_reset_mac_header(skb); 2383 skb_reset_mac_header(skb);
2382 skb_reset_mac_len(skb); 2384 skb_reset_mac_len(skb);
2383 2385
@@ -2603,7 +2605,8 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2603 dev_queue_xmit_nit(skb, dev); 2605 dev_queue_xmit_nit(skb, dev);
2604 2606
2605 skb_len = skb->len; 2607 skb_len = skb->len;
2606 rc = ops->ndo_start_xmit(skb, dev); 2608 rc = ops->ndo_start_xmit(skb, dev);
2609
2607 trace_net_dev_xmit(skb, rc, dev, skb_len); 2610 trace_net_dev_xmit(skb, rc, dev, skb_len);
2608 if (rc == NETDEV_TX_OK) 2611 if (rc == NETDEV_TX_OK)
2609 txq_trans_update(txq); 2612 txq_trans_update(txq);
@@ -2646,6 +2649,7 @@ out_kfree_skb:
2646out: 2649out:
2647 return rc; 2650 return rc;
2648} 2651}
2652EXPORT_SYMBOL_GPL(dev_hard_start_xmit);
2649 2653
2650static void qdisc_pkt_len_init(struct sk_buff *skb) 2654static void qdisc_pkt_len_init(struct sk_buff *skb)
2651{ 2655{
@@ -2801,7 +2805,7 @@ EXPORT_SYMBOL(dev_loopback_xmit);
2801 * the BH enable code must have IRQs enabled so that it will not deadlock. 2805 * the BH enable code must have IRQs enabled so that it will not deadlock.
2802 * --BLG 2806 * --BLG
2803 */ 2807 */
2804int dev_queue_xmit(struct sk_buff *skb) 2808int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
2805{ 2809{
2806 struct net_device *dev = skb->dev; 2810 struct net_device *dev = skb->dev;
2807 struct netdev_queue *txq; 2811 struct netdev_queue *txq;
@@ -2817,7 +2821,7 @@ int dev_queue_xmit(struct sk_buff *skb)
2817 2821
2818 skb_update_prio(skb); 2822 skb_update_prio(skb);
2819 2823
2820 txq = netdev_pick_tx(dev, skb); 2824 txq = netdev_pick_tx(dev, skb, accel_priv);
2821 q = rcu_dereference_bh(txq->qdisc); 2825 q = rcu_dereference_bh(txq->qdisc);
2822 2826
2823#ifdef CONFIG_NET_CLS_ACT 2827#ifdef CONFIG_NET_CLS_ACT
@@ -2882,8 +2886,19 @@ out:
2882 rcu_read_unlock_bh(); 2886 rcu_read_unlock_bh();
2883 return rc; 2887 return rc;
2884} 2888}
2889
2890int dev_queue_xmit(struct sk_buff *skb)
2891{
2892 return __dev_queue_xmit(skb, NULL);
2893}
2885EXPORT_SYMBOL(dev_queue_xmit); 2894EXPORT_SYMBOL(dev_queue_xmit);
2886 2895
2896int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
2897{
2898 return __dev_queue_xmit(skb, accel_priv);
2899}
2900EXPORT_SYMBOL(dev_queue_xmit_accel);
2901
2887 2902
2888/*======================================================================= 2903/*=======================================================================
2889 Receiver routines 2904 Receiver routines
@@ -4374,42 +4389,40 @@ struct netdev_adjacent {
4374 /* upper master flag, there can only be one master device per list */ 4389 /* upper master flag, there can only be one master device per list */
4375 bool master; 4390 bool master;
4376 4391
4377 /* indicates that this dev is our first-level lower/upper device */
4378 bool neighbour;
4379
4380 /* counter for the number of times this device was added to us */ 4392 /* counter for the number of times this device was added to us */
4381 u16 ref_nr; 4393 u16 ref_nr;
4382 4394
4395 /* private field for the users */
4396 void *private;
4397
4383 struct list_head list; 4398 struct list_head list;
4384 struct rcu_head rcu; 4399 struct rcu_head rcu;
4385}; 4400};
4386 4401
4387static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev, 4402static struct netdev_adjacent *__netdev_find_adj_rcu(struct net_device *dev,
4388 struct net_device *adj_dev, 4403 struct net_device *adj_dev,
4389 bool upper) 4404 struct list_head *adj_list)
4390{ 4405{
4391 struct netdev_adjacent *adj; 4406 struct netdev_adjacent *adj;
4392 struct list_head *dev_list;
4393 4407
4394 dev_list = upper ? &dev->upper_dev_list : &dev->lower_dev_list; 4408 list_for_each_entry_rcu(adj, adj_list, list) {
4395
4396 list_for_each_entry(adj, dev_list, list) {
4397 if (adj->dev == adj_dev) 4409 if (adj->dev == adj_dev)
4398 return adj; 4410 return adj;
4399 } 4411 }
4400 return NULL; 4412 return NULL;
4401} 4413}
4402 4414
4403static inline struct netdev_adjacent *__netdev_find_upper(struct net_device *dev, 4415static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev,
4404 struct net_device *udev) 4416 struct net_device *adj_dev,
4417 struct list_head *adj_list)
4405{ 4418{
4406 return __netdev_find_adj(dev, udev, true); 4419 struct netdev_adjacent *adj;
4407}
4408 4420
4409static inline struct netdev_adjacent *__netdev_find_lower(struct net_device *dev, 4421 list_for_each_entry(adj, adj_list, list) {
4410 struct net_device *ldev) 4422 if (adj->dev == adj_dev)
4411{ 4423 return adj;
4412 return __netdev_find_adj(dev, ldev, false); 4424 }
4425 return NULL;
4413} 4426}
4414 4427
4415/** 4428/**
@@ -4426,7 +4439,7 @@ bool netdev_has_upper_dev(struct net_device *dev,
4426{ 4439{
4427 ASSERT_RTNL(); 4440 ASSERT_RTNL();
4428 4441
4429 return __netdev_find_upper(dev, upper_dev); 4442 return __netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper);
4430} 4443}
4431EXPORT_SYMBOL(netdev_has_upper_dev); 4444EXPORT_SYMBOL(netdev_has_upper_dev);
4432 4445
@@ -4441,7 +4454,7 @@ bool netdev_has_any_upper_dev(struct net_device *dev)
4441{ 4454{
4442 ASSERT_RTNL(); 4455 ASSERT_RTNL();
4443 4456
4444 return !list_empty(&dev->upper_dev_list); 4457 return !list_empty(&dev->all_adj_list.upper);
4445} 4458}
4446EXPORT_SYMBOL(netdev_has_any_upper_dev); 4459EXPORT_SYMBOL(netdev_has_any_upper_dev);
4447 4460
@@ -4458,10 +4471,10 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4458 4471
4459 ASSERT_RTNL(); 4472 ASSERT_RTNL();
4460 4473
4461 if (list_empty(&dev->upper_dev_list)) 4474 if (list_empty(&dev->adj_list.upper))
4462 return NULL; 4475 return NULL;
4463 4476
4464 upper = list_first_entry(&dev->upper_dev_list, 4477 upper = list_first_entry(&dev->adj_list.upper,
4465 struct netdev_adjacent, list); 4478 struct netdev_adjacent, list);
4466 if (likely(upper->master)) 4479 if (likely(upper->master))
4467 return upper->dev; 4480 return upper->dev;
@@ -4469,30 +4482,98 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4469} 4482}
4470EXPORT_SYMBOL(netdev_master_upper_dev_get); 4483EXPORT_SYMBOL(netdev_master_upper_dev_get);
4471 4484
4472/* netdev_upper_get_next_dev_rcu - Get the next dev from upper list 4485void *netdev_adjacent_get_private(struct list_head *adj_list)
4486{
4487 struct netdev_adjacent *adj;
4488
4489 adj = list_entry(adj_list, struct netdev_adjacent, list);
4490
4491 return adj->private;
4492}
4493EXPORT_SYMBOL(netdev_adjacent_get_private);
4494
4495/**
4496 * netdev_all_upper_get_next_dev_rcu - Get the next dev from upper list
4473 * @dev: device 4497 * @dev: device
4474 * @iter: list_head ** of the current position 4498 * @iter: list_head ** of the current position
4475 * 4499 *
4476 * Gets the next device from the dev's upper list, starting from iter 4500 * Gets the next device from the dev's upper list, starting from iter
4477 * position. The caller must hold RCU read lock. 4501 * position. The caller must hold RCU read lock.
4478 */ 4502 */
4479struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, 4503struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
4480 struct list_head **iter) 4504 struct list_head **iter)
4481{ 4505{
4482 struct netdev_adjacent *upper; 4506 struct netdev_adjacent *upper;
4483 4507
4484 WARN_ON_ONCE(!rcu_read_lock_held()); 4508 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
4485 4509
4486 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 4510 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4487 4511
4488 if (&upper->list == &dev->upper_dev_list) 4512 if (&upper->list == &dev->all_adj_list.upper)
4489 return NULL; 4513 return NULL;
4490 4514
4491 *iter = &upper->list; 4515 *iter = &upper->list;
4492 4516
4493 return upper->dev; 4517 return upper->dev;
4494} 4518}
4495EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); 4519EXPORT_SYMBOL(netdev_all_upper_get_next_dev_rcu);
4520
4521/**
4522 * netdev_lower_get_next_private - Get the next ->private from the
4523 * lower neighbour list
4524 * @dev: device
4525 * @iter: list_head ** of the current position
4526 *
4527 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4528 * list, starting from iter position. The caller must hold either hold the
4529 * RTNL lock or its own locking that guarantees that the neighbour lower
4530 * list will remain unchainged.
4531 */
4532void *netdev_lower_get_next_private(struct net_device *dev,
4533 struct list_head **iter)
4534{
4535 struct netdev_adjacent *lower;
4536
4537 lower = list_entry(*iter, struct netdev_adjacent, list);
4538
4539 if (&lower->list == &dev->adj_list.lower)
4540 return NULL;
4541
4542 if (iter)
4543 *iter = lower->list.next;
4544
4545 return lower->private;
4546}
4547EXPORT_SYMBOL(netdev_lower_get_next_private);
4548
4549/**
4550 * netdev_lower_get_next_private_rcu - Get the next ->private from the
4551 * lower neighbour list, RCU
4552 * variant
4553 * @dev: device
4554 * @iter: list_head ** of the current position
4555 *
4556 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4557 * list, starting from iter position. The caller must hold RCU read lock.
4558 */
4559void *netdev_lower_get_next_private_rcu(struct net_device *dev,
4560 struct list_head **iter)
4561{
4562 struct netdev_adjacent *lower;
4563
4564 WARN_ON_ONCE(!rcu_read_lock_held());
4565
4566 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4567
4568 if (&lower->list == &dev->adj_list.lower)
4569 return NULL;
4570
4571 if (iter)
4572 *iter = &lower->list;
4573
4574 return lower->private;
4575}
4576EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
4496 4577
4497/** 4578/**
4498 * netdev_master_upper_dev_get_rcu - Get master upper device 4579 * netdev_master_upper_dev_get_rcu - Get master upper device
@@ -4505,7 +4586,7 @@ struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4505{ 4586{
4506 struct netdev_adjacent *upper; 4587 struct netdev_adjacent *upper;
4507 4588
4508 upper = list_first_or_null_rcu(&dev->upper_dev_list, 4589 upper = list_first_or_null_rcu(&dev->adj_list.upper,
4509 struct netdev_adjacent, list); 4590 struct netdev_adjacent, list);
4510 if (upper && likely(upper->master)) 4591 if (upper && likely(upper->master))
4511 return upper->dev; 4592 return upper->dev;
@@ -4515,15 +4596,16 @@ EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4515 4596
4516static int __netdev_adjacent_dev_insert(struct net_device *dev, 4597static int __netdev_adjacent_dev_insert(struct net_device *dev,
4517 struct net_device *adj_dev, 4598 struct net_device *adj_dev,
4518 bool neighbour, bool master, 4599 struct list_head *dev_list,
4519 bool upper) 4600 void *private, bool master)
4520{ 4601{
4521 struct netdev_adjacent *adj; 4602 struct netdev_adjacent *adj;
4603 char linkname[IFNAMSIZ+7];
4604 int ret;
4522 4605
4523 adj = __netdev_find_adj(dev, adj_dev, upper); 4606 adj = __netdev_find_adj(dev, adj_dev, dev_list);
4524 4607
4525 if (adj) { 4608 if (adj) {
4526 BUG_ON(neighbour);
4527 adj->ref_nr++; 4609 adj->ref_nr++;
4528 return 0; 4610 return 0;
4529 } 4611 }
@@ -4534,124 +4616,179 @@ static int __netdev_adjacent_dev_insert(struct net_device *dev,
4534 4616
4535 adj->dev = adj_dev; 4617 adj->dev = adj_dev;
4536 adj->master = master; 4618 adj->master = master;
4537 adj->neighbour = neighbour;
4538 adj->ref_nr = 1; 4619 adj->ref_nr = 1;
4539 4620 adj->private = private;
4540 dev_hold(adj_dev); 4621 dev_hold(adj_dev);
4541 pr_debug("dev_hold for %s, because of %s link added from %s to %s\n",
4542 adj_dev->name, upper ? "upper" : "lower", dev->name,
4543 adj_dev->name);
4544 4622
4545 if (!upper) { 4623 pr_debug("dev_hold for %s, because of link added from %s to %s\n",
4546 list_add_tail_rcu(&adj->list, &dev->lower_dev_list); 4624 adj_dev->name, dev->name, adj_dev->name);
4547 return 0; 4625
4626 if (dev_list == &dev->adj_list.lower) {
4627 sprintf(linkname, "lower_%s", adj_dev->name);
4628 ret = sysfs_create_link(&(dev->dev.kobj),
4629 &(adj_dev->dev.kobj), linkname);
4630 if (ret)
4631 goto free_adj;
4632 } else if (dev_list == &dev->adj_list.upper) {
4633 sprintf(linkname, "upper_%s", adj_dev->name);
4634 ret = sysfs_create_link(&(dev->dev.kobj),
4635 &(adj_dev->dev.kobj), linkname);
4636 if (ret)
4637 goto free_adj;
4548 } 4638 }
4549 4639
4550 /* Ensure that master upper link is always the first item in list. */ 4640 /* Ensure that master link is always the first item in list. */
4551 if (master) 4641 if (master) {
4552 list_add_rcu(&adj->list, &dev->upper_dev_list); 4642 ret = sysfs_create_link(&(dev->dev.kobj),
4553 else 4643 &(adj_dev->dev.kobj), "master");
4554 list_add_tail_rcu(&adj->list, &dev->upper_dev_list); 4644 if (ret)
4645 goto remove_symlinks;
4646
4647 list_add_rcu(&adj->list, dev_list);
4648 } else {
4649 list_add_tail_rcu(&adj->list, dev_list);
4650 }
4555 4651
4556 return 0; 4652 return 0;
4557}
4558 4653
4559static inline int __netdev_upper_dev_insert(struct net_device *dev, 4654remove_symlinks:
4560 struct net_device *udev, 4655 if (dev_list == &dev->adj_list.lower) {
4561 bool master, bool neighbour) 4656 sprintf(linkname, "lower_%s", adj_dev->name);
4562{ 4657 sysfs_remove_link(&(dev->dev.kobj), linkname);
4563 return __netdev_adjacent_dev_insert(dev, udev, neighbour, master, 4658 } else if (dev_list == &dev->adj_list.upper) {
4564 true); 4659 sprintf(linkname, "upper_%s", adj_dev->name);
4565} 4660 sysfs_remove_link(&(dev->dev.kobj), linkname);
4661 }
4566 4662
4567static inline int __netdev_lower_dev_insert(struct net_device *dev, 4663free_adj:
4568 struct net_device *ldev, 4664 kfree(adj);
4569 bool neighbour) 4665 dev_put(adj_dev);
4570{ 4666
4571 return __netdev_adjacent_dev_insert(dev, ldev, neighbour, false, 4667 return ret;
4572 false);
4573} 4668}
4574 4669
4575void __netdev_adjacent_dev_remove(struct net_device *dev, 4670void __netdev_adjacent_dev_remove(struct net_device *dev,
4576 struct net_device *adj_dev, bool upper) 4671 struct net_device *adj_dev,
4672 struct list_head *dev_list)
4577{ 4673{
4578 struct netdev_adjacent *adj; 4674 struct netdev_adjacent *adj;
4675 char linkname[IFNAMSIZ+7];
4579 4676
4580 if (upper) 4677 adj = __netdev_find_adj(dev, adj_dev, dev_list);
4581 adj = __netdev_find_upper(dev, adj_dev);
4582 else
4583 adj = __netdev_find_lower(dev, adj_dev);
4584 4678
4585 if (!adj) 4679 if (!adj) {
4680 pr_err("tried to remove device %s from %s\n",
4681 dev->name, adj_dev->name);
4586 BUG(); 4682 BUG();
4683 }
4587 4684
4588 if (adj->ref_nr > 1) { 4685 if (adj->ref_nr > 1) {
4686 pr_debug("%s to %s ref_nr-- = %d\n", dev->name, adj_dev->name,
4687 adj->ref_nr-1);
4589 adj->ref_nr--; 4688 adj->ref_nr--;
4590 return; 4689 return;
4591 } 4690 }
4592 4691
4692 if (adj->master)
4693 sysfs_remove_link(&(dev->dev.kobj), "master");
4694
4695 if (dev_list == &dev->adj_list.lower) {
4696 sprintf(linkname, "lower_%s", adj_dev->name);
4697 sysfs_remove_link(&(dev->dev.kobj), linkname);
4698 } else if (dev_list == &dev->adj_list.upper) {
4699 sprintf(linkname, "upper_%s", adj_dev->name);
4700 sysfs_remove_link(&(dev->dev.kobj), linkname);
4701 }
4702
4593 list_del_rcu(&adj->list); 4703 list_del_rcu(&adj->list);
4594 pr_debug("dev_put for %s, because of %s link removed from %s to %s\n", 4704 pr_debug("dev_put for %s, because link removed from %s to %s\n",
4595 adj_dev->name, upper ? "upper" : "lower", dev->name, 4705 adj_dev->name, dev->name, adj_dev->name);
4596 adj_dev->name);
4597 dev_put(adj_dev); 4706 dev_put(adj_dev);
4598 kfree_rcu(adj, rcu); 4707 kfree_rcu(adj, rcu);
4599} 4708}
4600 4709
4601static inline void __netdev_upper_dev_remove(struct net_device *dev, 4710int __netdev_adjacent_dev_link_lists(struct net_device *dev,
4602 struct net_device *udev) 4711 struct net_device *upper_dev,
4603{ 4712 struct list_head *up_list,
4604 return __netdev_adjacent_dev_remove(dev, udev, true); 4713 struct list_head *down_list,
4605} 4714 void *private, bool master)
4606
4607static inline void __netdev_lower_dev_remove(struct net_device *dev,
4608 struct net_device *ldev)
4609{
4610 return __netdev_adjacent_dev_remove(dev, ldev, false);
4611}
4612
4613int __netdev_adjacent_dev_insert_link(struct net_device *dev,
4614 struct net_device *upper_dev,
4615 bool master, bool neighbour)
4616{ 4715{
4617 int ret; 4716 int ret;
4618 4717
4619 ret = __netdev_upper_dev_insert(dev, upper_dev, master, neighbour); 4718 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, private,
4719 master);
4620 if (ret) 4720 if (ret)
4621 return ret; 4721 return ret;
4622 4722
4623 ret = __netdev_lower_dev_insert(upper_dev, dev, neighbour); 4723 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, private,
4724 false);
4624 if (ret) { 4725 if (ret) {
4625 __netdev_upper_dev_remove(dev, upper_dev); 4726 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4626 return ret; 4727 return ret;
4627 } 4728 }
4628 4729
4629 return 0; 4730 return 0;
4630} 4731}
4631 4732
4632static inline int __netdev_adjacent_dev_link(struct net_device *dev, 4733int __netdev_adjacent_dev_link(struct net_device *dev,
4633 struct net_device *udev) 4734 struct net_device *upper_dev)
4634{ 4735{
4635 return __netdev_adjacent_dev_insert_link(dev, udev, false, false); 4736 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
4737 &dev->all_adj_list.upper,
4738 &upper_dev->all_adj_list.lower,
4739 NULL, false);
4636} 4740}
4637 4741
4638static inline int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, 4742void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
4639 struct net_device *udev, 4743 struct net_device *upper_dev,
4640 bool master) 4744 struct list_head *up_list,
4745 struct list_head *down_list)
4641{ 4746{
4642 return __netdev_adjacent_dev_insert_link(dev, udev, master, true); 4747 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4748 __netdev_adjacent_dev_remove(upper_dev, dev, down_list);
4643} 4749}
4644 4750
4645void __netdev_adjacent_dev_unlink(struct net_device *dev, 4751void __netdev_adjacent_dev_unlink(struct net_device *dev,
4646 struct net_device *upper_dev) 4752 struct net_device *upper_dev)
4647{ 4753{
4648 __netdev_upper_dev_remove(dev, upper_dev); 4754 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4649 __netdev_lower_dev_remove(upper_dev, dev); 4755 &dev->all_adj_list.upper,
4756 &upper_dev->all_adj_list.lower);
4757}
4758
4759int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
4760 struct net_device *upper_dev,
4761 void *private, bool master)
4762{
4763 int ret = __netdev_adjacent_dev_link(dev, upper_dev);
4764
4765 if (ret)
4766 return ret;
4767
4768 ret = __netdev_adjacent_dev_link_lists(dev, upper_dev,
4769 &dev->adj_list.upper,
4770 &upper_dev->adj_list.lower,
4771 private, master);
4772 if (ret) {
4773 __netdev_adjacent_dev_unlink(dev, upper_dev);
4774 return ret;
4775 }
4776
4777 return 0;
4650} 4778}
4651 4779
4780void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
4781 struct net_device *upper_dev)
4782{
4783 __netdev_adjacent_dev_unlink(dev, upper_dev);
4784 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4785 &dev->adj_list.upper,
4786 &upper_dev->adj_list.lower);
4787}
4652 4788
4653static int __netdev_upper_dev_link(struct net_device *dev, 4789static int __netdev_upper_dev_link(struct net_device *dev,
4654 struct net_device *upper_dev, bool master) 4790 struct net_device *upper_dev, bool master,
4791 void *private)
4655{ 4792{
4656 struct netdev_adjacent *i, *j, *to_i, *to_j; 4793 struct netdev_adjacent *i, *j, *to_i, *to_j;
4657 int ret = 0; 4794 int ret = 0;
@@ -4662,26 +4799,29 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4662 return -EBUSY; 4799 return -EBUSY;
4663 4800
4664 /* To prevent loops, check if dev is not upper device to upper_dev. */ 4801 /* To prevent loops, check if dev is not upper device to upper_dev. */
4665 if (__netdev_find_upper(upper_dev, dev)) 4802 if (__netdev_find_adj(upper_dev, dev, &upper_dev->all_adj_list.upper))
4666 return -EBUSY; 4803 return -EBUSY;
4667 4804
4668 if (__netdev_find_upper(dev, upper_dev)) 4805 if (__netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper))
4669 return -EEXIST; 4806 return -EEXIST;
4670 4807
4671 if (master && netdev_master_upper_dev_get(dev)) 4808 if (master && netdev_master_upper_dev_get(dev))
4672 return -EBUSY; 4809 return -EBUSY;
4673 4810
4674 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, master); 4811 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, private,
4812 master);
4675 if (ret) 4813 if (ret)
4676 return ret; 4814 return ret;
4677 4815
4678 /* Now that we linked these devs, make all the upper_dev's 4816 /* Now that we linked these devs, make all the upper_dev's
4679 * upper_dev_list visible to every dev's lower_dev_list and vice 4817 * all_adj_list.upper visible to every dev's all_adj_list.lower an
4680 * versa, and don't forget the devices itself. All of these 4818 * versa, and don't forget the devices itself. All of these
4681 * links are non-neighbours. 4819 * links are non-neighbours.
4682 */ 4820 */
4683 list_for_each_entry(i, &dev->lower_dev_list, list) { 4821 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4684 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4822 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4823 pr_debug("Interlinking %s with %s, non-neighbour\n",
4824 i->dev->name, j->dev->name);
4685 ret = __netdev_adjacent_dev_link(i->dev, j->dev); 4825 ret = __netdev_adjacent_dev_link(i->dev, j->dev);
4686 if (ret) 4826 if (ret)
4687 goto rollback_mesh; 4827 goto rollback_mesh;
@@ -4689,14 +4829,18 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4689 } 4829 }
4690 4830
4691 /* add dev to every upper_dev's upper device */ 4831 /* add dev to every upper_dev's upper device */
4692 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4832 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4833 pr_debug("linking %s's upper device %s with %s\n",
4834 upper_dev->name, i->dev->name, dev->name);
4693 ret = __netdev_adjacent_dev_link(dev, i->dev); 4835 ret = __netdev_adjacent_dev_link(dev, i->dev);
4694 if (ret) 4836 if (ret)
4695 goto rollback_upper_mesh; 4837 goto rollback_upper_mesh;
4696 } 4838 }
4697 4839
4698 /* add upper_dev to every dev's lower device */ 4840 /* add upper_dev to every dev's lower device */
4699 list_for_each_entry(i, &dev->lower_dev_list, list) { 4841 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4842 pr_debug("linking %s's lower device %s with %s\n", dev->name,
4843 i->dev->name, upper_dev->name);
4700 ret = __netdev_adjacent_dev_link(i->dev, upper_dev); 4844 ret = __netdev_adjacent_dev_link(i->dev, upper_dev);
4701 if (ret) 4845 if (ret)
4702 goto rollback_lower_mesh; 4846 goto rollback_lower_mesh;
@@ -4707,7 +4851,7 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4707 4851
4708rollback_lower_mesh: 4852rollback_lower_mesh:
4709 to_i = i; 4853 to_i = i;
4710 list_for_each_entry(i, &dev->lower_dev_list, list) { 4854 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4711 if (i == to_i) 4855 if (i == to_i)
4712 break; 4856 break;
4713 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4857 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
@@ -4717,7 +4861,7 @@ rollback_lower_mesh:
4717 4861
4718rollback_upper_mesh: 4862rollback_upper_mesh:
4719 to_i = i; 4863 to_i = i;
4720 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4864 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4721 if (i == to_i) 4865 if (i == to_i)
4722 break; 4866 break;
4723 __netdev_adjacent_dev_unlink(dev, i->dev); 4867 __netdev_adjacent_dev_unlink(dev, i->dev);
@@ -4728,8 +4872,8 @@ rollback_upper_mesh:
4728rollback_mesh: 4872rollback_mesh:
4729 to_i = i; 4873 to_i = i;
4730 to_j = j; 4874 to_j = j;
4731 list_for_each_entry(i, &dev->lower_dev_list, list) { 4875 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4732 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4876 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4733 if (i == to_i && j == to_j) 4877 if (i == to_i && j == to_j)
4734 break; 4878 break;
4735 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4879 __netdev_adjacent_dev_unlink(i->dev, j->dev);
@@ -4738,7 +4882,7 @@ rollback_mesh:
4738 break; 4882 break;
4739 } 4883 }
4740 4884
4741 __netdev_adjacent_dev_unlink(dev, upper_dev); 4885 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4742 4886
4743 return ret; 4887 return ret;
4744} 4888}
@@ -4756,7 +4900,7 @@ rollback_mesh:
4756int netdev_upper_dev_link(struct net_device *dev, 4900int netdev_upper_dev_link(struct net_device *dev,
4757 struct net_device *upper_dev) 4901 struct net_device *upper_dev)
4758{ 4902{
4759 return __netdev_upper_dev_link(dev, upper_dev, false); 4903 return __netdev_upper_dev_link(dev, upper_dev, false, NULL);
4760} 4904}
4761EXPORT_SYMBOL(netdev_upper_dev_link); 4905EXPORT_SYMBOL(netdev_upper_dev_link);
4762 4906
@@ -4774,10 +4918,18 @@ EXPORT_SYMBOL(netdev_upper_dev_link);
4774int netdev_master_upper_dev_link(struct net_device *dev, 4918int netdev_master_upper_dev_link(struct net_device *dev,
4775 struct net_device *upper_dev) 4919 struct net_device *upper_dev)
4776{ 4920{
4777 return __netdev_upper_dev_link(dev, upper_dev, true); 4921 return __netdev_upper_dev_link(dev, upper_dev, true, NULL);
4778} 4922}
4779EXPORT_SYMBOL(netdev_master_upper_dev_link); 4923EXPORT_SYMBOL(netdev_master_upper_dev_link);
4780 4924
4925int netdev_master_upper_dev_link_private(struct net_device *dev,
4926 struct net_device *upper_dev,
4927 void *private)
4928{
4929 return __netdev_upper_dev_link(dev, upper_dev, true, private);
4930}
4931EXPORT_SYMBOL(netdev_master_upper_dev_link_private);
4932
4781/** 4933/**
4782 * netdev_upper_dev_unlink - Removes a link to upper device 4934 * netdev_upper_dev_unlink - Removes a link to upper device
4783 * @dev: device 4935 * @dev: device
@@ -4792,38 +4944,68 @@ void netdev_upper_dev_unlink(struct net_device *dev,
4792 struct netdev_adjacent *i, *j; 4944 struct netdev_adjacent *i, *j;
4793 ASSERT_RTNL(); 4945 ASSERT_RTNL();
4794 4946
4795 __netdev_adjacent_dev_unlink(dev, upper_dev); 4947 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4796 4948
4797 /* Here is the tricky part. We must remove all dev's lower 4949 /* Here is the tricky part. We must remove all dev's lower
4798 * devices from all upper_dev's upper devices and vice 4950 * devices from all upper_dev's upper devices and vice
4799 * versa, to maintain the graph relationship. 4951 * versa, to maintain the graph relationship.
4800 */ 4952 */
4801 list_for_each_entry(i, &dev->lower_dev_list, list) 4953 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4802 list_for_each_entry(j, &upper_dev->upper_dev_list, list) 4954 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list)
4803 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4955 __netdev_adjacent_dev_unlink(i->dev, j->dev);
4804 4956
4805 /* remove also the devices itself from lower/upper device 4957 /* remove also the devices itself from lower/upper device
4806 * list 4958 * list
4807 */ 4959 */
4808 list_for_each_entry(i, &dev->lower_dev_list, list) 4960 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4809 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4961 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
4810 4962
4811 list_for_each_entry(i, &upper_dev->upper_dev_list, list) 4963 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
4812 __netdev_adjacent_dev_unlink(dev, i->dev); 4964 __netdev_adjacent_dev_unlink(dev, i->dev);
4813 4965
4814 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev); 4966 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
4815} 4967}
4816EXPORT_SYMBOL(netdev_upper_dev_unlink); 4968EXPORT_SYMBOL(netdev_upper_dev_unlink);
4817 4969
4970void *netdev_lower_dev_get_private_rcu(struct net_device *dev,
4971 struct net_device *lower_dev)
4972{
4973 struct netdev_adjacent *lower;
4974
4975 if (!lower_dev)
4976 return NULL;
4977 lower = __netdev_find_adj_rcu(dev, lower_dev, &dev->adj_list.lower);
4978 if (!lower)
4979 return NULL;
4980
4981 return lower->private;
4982}
4983EXPORT_SYMBOL(netdev_lower_dev_get_private_rcu);
4984
4985void *netdev_lower_dev_get_private(struct net_device *dev,
4986 struct net_device *lower_dev)
4987{
4988 struct netdev_adjacent *lower;
4989
4990 if (!lower_dev)
4991 return NULL;
4992 lower = __netdev_find_adj(dev, lower_dev, &dev->adj_list.lower);
4993 if (!lower)
4994 return NULL;
4995
4996 return lower->private;
4997}
4998EXPORT_SYMBOL(netdev_lower_dev_get_private);
4999
4818static void dev_change_rx_flags(struct net_device *dev, int flags) 5000static void dev_change_rx_flags(struct net_device *dev, int flags)
4819{ 5001{
4820 const struct net_device_ops *ops = dev->netdev_ops; 5002 const struct net_device_ops *ops = dev->netdev_ops;
4821 5003
4822 if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags) 5004 if (ops->ndo_change_rx_flags)
4823 ops->ndo_change_rx_flags(dev, flags); 5005 ops->ndo_change_rx_flags(dev, flags);
4824} 5006}
4825 5007
4826static int __dev_set_promiscuity(struct net_device *dev, int inc) 5008static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
4827{ 5009{
4828 unsigned int old_flags = dev->flags; 5010 unsigned int old_flags = dev->flags;
4829 kuid_t uid; 5011 kuid_t uid;
@@ -4866,6 +5048,8 @@ static int __dev_set_promiscuity(struct net_device *dev, int inc)
4866 5048
4867 dev_change_rx_flags(dev, IFF_PROMISC); 5049 dev_change_rx_flags(dev, IFF_PROMISC);
4868 } 5050 }
5051 if (notify)
5052 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
4869 return 0; 5053 return 0;
4870} 5054}
4871 5055
@@ -4885,7 +5069,7 @@ int dev_set_promiscuity(struct net_device *dev, int inc)
4885 unsigned int old_flags = dev->flags; 5069 unsigned int old_flags = dev->flags;
4886 int err; 5070 int err;
4887 5071
4888 err = __dev_set_promiscuity(dev, inc); 5072 err = __dev_set_promiscuity(dev, inc, true);
4889 if (err < 0) 5073 if (err < 0)
4890 return err; 5074 return err;
4891 if (dev->flags != old_flags) 5075 if (dev->flags != old_flags)
@@ -4894,22 +5078,9 @@ int dev_set_promiscuity(struct net_device *dev, int inc)
4894} 5078}
4895EXPORT_SYMBOL(dev_set_promiscuity); 5079EXPORT_SYMBOL(dev_set_promiscuity);
4896 5080
4897/** 5081static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
4898 * dev_set_allmulti - update allmulti count on a device
4899 * @dev: device
4900 * @inc: modifier
4901 *
4902 * Add or remove reception of all multicast frames to a device. While the
4903 * count in the device remains above zero the interface remains listening
4904 * to all interfaces. Once it hits zero the device reverts back to normal
4905 * filtering operation. A negative @inc value is used to drop the counter
4906 * when releasing a resource needing all multicasts.
4907 * Return 0 if successful or a negative errno code on error.
4908 */
4909
4910int dev_set_allmulti(struct net_device *dev, int inc)
4911{ 5082{
4912 unsigned int old_flags = dev->flags; 5083 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
4913 5084
4914 ASSERT_RTNL(); 5085 ASSERT_RTNL();
4915 5086
@@ -4932,9 +5103,30 @@ int dev_set_allmulti(struct net_device *dev, int inc)
4932 if (dev->flags ^ old_flags) { 5103 if (dev->flags ^ old_flags) {
4933 dev_change_rx_flags(dev, IFF_ALLMULTI); 5104 dev_change_rx_flags(dev, IFF_ALLMULTI);
4934 dev_set_rx_mode(dev); 5105 dev_set_rx_mode(dev);
5106 if (notify)
5107 __dev_notify_flags(dev, old_flags,
5108 dev->gflags ^ old_gflags);
4935 } 5109 }
4936 return 0; 5110 return 0;
4937} 5111}
5112
5113/**
5114 * dev_set_allmulti - update allmulti count on a device
5115 * @dev: device
5116 * @inc: modifier
5117 *
5118 * Add or remove reception of all multicast frames to a device. While the
5119 * count in the device remains above zero the interface remains listening
5120 * to all interfaces. Once it hits zero the device reverts back to normal
5121 * filtering operation. A negative @inc value is used to drop the counter
5122 * when releasing a resource needing all multicasts.
5123 * Return 0 if successful or a negative errno code on error.
5124 */
5125
5126int dev_set_allmulti(struct net_device *dev, int inc)
5127{
5128 return __dev_set_allmulti(dev, inc, true);
5129}
4938EXPORT_SYMBOL(dev_set_allmulti); 5130EXPORT_SYMBOL(dev_set_allmulti);
4939 5131
4940/* 5132/*
@@ -4959,10 +5151,10 @@ void __dev_set_rx_mode(struct net_device *dev)
4959 * therefore calling __dev_set_promiscuity here is safe. 5151 * therefore calling __dev_set_promiscuity here is safe.
4960 */ 5152 */
4961 if (!netdev_uc_empty(dev) && !dev->uc_promisc) { 5153 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
4962 __dev_set_promiscuity(dev, 1); 5154 __dev_set_promiscuity(dev, 1, false);
4963 dev->uc_promisc = true; 5155 dev->uc_promisc = true;
4964 } else if (netdev_uc_empty(dev) && dev->uc_promisc) { 5156 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
4965 __dev_set_promiscuity(dev, -1); 5157 __dev_set_promiscuity(dev, -1, false);
4966 dev->uc_promisc = false; 5158 dev->uc_promisc = false;
4967 } 5159 }
4968 } 5160 }
@@ -5051,9 +5243,13 @@ int __dev_change_flags(struct net_device *dev, unsigned int flags)
5051 5243
5052 if ((flags ^ dev->gflags) & IFF_PROMISC) { 5244 if ((flags ^ dev->gflags) & IFF_PROMISC) {
5053 int inc = (flags & IFF_PROMISC) ? 1 : -1; 5245 int inc = (flags & IFF_PROMISC) ? 1 : -1;
5246 unsigned int old_flags = dev->flags;
5054 5247
5055 dev->gflags ^= IFF_PROMISC; 5248 dev->gflags ^= IFF_PROMISC;
5056 dev_set_promiscuity(dev, inc); 5249
5250 if (__dev_set_promiscuity(dev, inc, false) >= 0)
5251 if (dev->flags != old_flags)
5252 dev_set_rx_mode(dev);
5057 } 5253 }
5058 5254
5059 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI 5255 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
@@ -5064,16 +5260,20 @@ int __dev_change_flags(struct net_device *dev, unsigned int flags)
5064 int inc = (flags & IFF_ALLMULTI) ? 1 : -1; 5260 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
5065 5261
5066 dev->gflags ^= IFF_ALLMULTI; 5262 dev->gflags ^= IFF_ALLMULTI;
5067 dev_set_allmulti(dev, inc); 5263 __dev_set_allmulti(dev, inc, false);
5068 } 5264 }
5069 5265
5070 return ret; 5266 return ret;
5071} 5267}
5072 5268
5073void __dev_notify_flags(struct net_device *dev, unsigned int old_flags) 5269void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
5270 unsigned int gchanges)
5074{ 5271{
5075 unsigned int changes = dev->flags ^ old_flags; 5272 unsigned int changes = dev->flags ^ old_flags;
5076 5273
5274 if (gchanges)
5275 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
5276
5077 if (changes & IFF_UP) { 5277 if (changes & IFF_UP) {
5078 if (dev->flags & IFF_UP) 5278 if (dev->flags & IFF_UP)
5079 call_netdevice_notifiers(NETDEV_UP, dev); 5279 call_netdevice_notifiers(NETDEV_UP, dev);
@@ -5102,17 +5302,14 @@ void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
5102int dev_change_flags(struct net_device *dev, unsigned int flags) 5302int dev_change_flags(struct net_device *dev, unsigned int flags)
5103{ 5303{
5104 int ret; 5304 int ret;
5105 unsigned int changes, old_flags = dev->flags; 5305 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
5106 5306
5107 ret = __dev_change_flags(dev, flags); 5307 ret = __dev_change_flags(dev, flags);
5108 if (ret < 0) 5308 if (ret < 0)
5109 return ret; 5309 return ret;
5110 5310
5111 changes = old_flags ^ dev->flags; 5311 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
5112 if (changes) 5312 __dev_notify_flags(dev, old_flags, changes);
5113 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
5114
5115 __dev_notify_flags(dev, old_flags);
5116 return ret; 5313 return ret;
5117} 5314}
5118EXPORT_SYMBOL(dev_change_flags); 5315EXPORT_SYMBOL(dev_change_flags);
@@ -5259,6 +5456,7 @@ static void net_set_todo(struct net_device *dev)
5259static void rollback_registered_many(struct list_head *head) 5456static void rollback_registered_many(struct list_head *head)
5260{ 5457{
5261 struct net_device *dev, *tmp; 5458 struct net_device *dev, *tmp;
5459 LIST_HEAD(close_head);
5262 5460
5263 BUG_ON(dev_boot_phase); 5461 BUG_ON(dev_boot_phase);
5264 ASSERT_RTNL(); 5462 ASSERT_RTNL();
@@ -5281,7 +5479,9 @@ static void rollback_registered_many(struct list_head *head)
5281 } 5479 }
5282 5480
5283 /* If device is running, close it first. */ 5481 /* If device is running, close it first. */
5284 dev_close_many(head); 5482 list_for_each_entry(dev, head, unreg_list)
5483 list_add_tail(&dev->close_list, &close_head);
5484 dev_close_many(&close_head);
5285 5485
5286 list_for_each_entry(dev, head, unreg_list) { 5486 list_for_each_entry(dev, head, unreg_list) {
5287 /* And unlink it from device chain. */ 5487 /* And unlink it from device chain. */
@@ -5304,7 +5504,7 @@ static void rollback_registered_many(struct list_head *head)
5304 5504
5305 if (!dev->rtnl_link_ops || 5505 if (!dev->rtnl_link_ops ||
5306 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 5506 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5307 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 5507 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
5308 5508
5309 /* 5509 /*
5310 * Flush the unicast and multicast chains 5510 * Flush the unicast and multicast chains
@@ -5703,7 +5903,7 @@ int register_netdevice(struct net_device *dev)
5703 */ 5903 */
5704 if (!dev->rtnl_link_ops || 5904 if (!dev->rtnl_link_ops ||
5705 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 5905 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5706 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 5906 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
5707 5907
5708out: 5908out:
5709 return ret; 5909 return ret;
@@ -6010,6 +6210,16 @@ void netdev_set_default_ethtool_ops(struct net_device *dev,
6010} 6210}
6011EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); 6211EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
6012 6212
6213void netdev_freemem(struct net_device *dev)
6214{
6215 char *addr = (char *)dev - dev->padded;
6216
6217 if (is_vmalloc_addr(addr))
6218 vfree(addr);
6219 else
6220 kfree(addr);
6221}
6222
6013/** 6223/**
6014 * alloc_netdev_mqs - allocate network device 6224 * alloc_netdev_mqs - allocate network device
6015 * @sizeof_priv: size of private data to allocate space for 6225 * @sizeof_priv: size of private data to allocate space for
@@ -6053,7 +6263,9 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6053 /* ensure 32-byte alignment of whole construct */ 6263 /* ensure 32-byte alignment of whole construct */
6054 alloc_size += NETDEV_ALIGN - 1; 6264 alloc_size += NETDEV_ALIGN - 1;
6055 6265
6056 p = kzalloc(alloc_size, GFP_KERNEL); 6266 p = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
6267 if (!p)
6268 p = vzalloc(alloc_size);
6057 if (!p) 6269 if (!p)
6058 return NULL; 6270 return NULL;
6059 6271
@@ -6062,7 +6274,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6062 6274
6063 dev->pcpu_refcnt = alloc_percpu(int); 6275 dev->pcpu_refcnt = alloc_percpu(int);
6064 if (!dev->pcpu_refcnt) 6276 if (!dev->pcpu_refcnt)
6065 goto free_p; 6277 goto free_dev;
6066 6278
6067 if (dev_addr_init(dev)) 6279 if (dev_addr_init(dev))
6068 goto free_pcpu; 6280 goto free_pcpu;
@@ -6077,9 +6289,12 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6077 6289
6078 INIT_LIST_HEAD(&dev->napi_list); 6290 INIT_LIST_HEAD(&dev->napi_list);
6079 INIT_LIST_HEAD(&dev->unreg_list); 6291 INIT_LIST_HEAD(&dev->unreg_list);
6292 INIT_LIST_HEAD(&dev->close_list);
6080 INIT_LIST_HEAD(&dev->link_watch_list); 6293 INIT_LIST_HEAD(&dev->link_watch_list);
6081 INIT_LIST_HEAD(&dev->upper_dev_list); 6294 INIT_LIST_HEAD(&dev->adj_list.upper);
6082 INIT_LIST_HEAD(&dev->lower_dev_list); 6295 INIT_LIST_HEAD(&dev->adj_list.lower);
6296 INIT_LIST_HEAD(&dev->all_adj_list.upper);
6297 INIT_LIST_HEAD(&dev->all_adj_list.lower);
6083 dev->priv_flags = IFF_XMIT_DST_RELEASE; 6298 dev->priv_flags = IFF_XMIT_DST_RELEASE;
6084 setup(dev); 6299 setup(dev);
6085 6300
@@ -6112,8 +6327,8 @@ free_pcpu:
6112 kfree(dev->_rx); 6327 kfree(dev->_rx);
6113#endif 6328#endif
6114 6329
6115free_p: 6330free_dev:
6116 kfree(p); 6331 netdev_freemem(dev);
6117 return NULL; 6332 return NULL;
6118} 6333}
6119EXPORT_SYMBOL(alloc_netdev_mqs); 6334EXPORT_SYMBOL(alloc_netdev_mqs);
@@ -6150,7 +6365,7 @@ void free_netdev(struct net_device *dev)
6150 6365
6151 /* Compatibility with error handling in drivers */ 6366 /* Compatibility with error handling in drivers */
6152 if (dev->reg_state == NETREG_UNINITIALIZED) { 6367 if (dev->reg_state == NETREG_UNINITIALIZED) {
6153 kfree((char *)dev - dev->padded); 6368 netdev_freemem(dev);
6154 return; 6369 return;
6155 } 6370 }
6156 6371
@@ -6312,7 +6527,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
6312 call_netdevice_notifiers(NETDEV_UNREGISTER, dev); 6527 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6313 rcu_barrier(); 6528 rcu_barrier();
6314 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); 6529 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
6315 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 6530 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
6316 6531
6317 /* 6532 /*
6318 * Flush the unicast and multicast chains 6533 * Flush the unicast and multicast chains
@@ -6351,7 +6566,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
6351 * Prevent userspace races by waiting until the network 6566 * Prevent userspace races by waiting until the network
6352 * device is fully setup before sending notifications. 6567 * device is fully setup before sending notifications.
6353 */ 6568 */
6354 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 6569 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
6355 6570
6356 synchronize_net(); 6571 synchronize_net();
6357 err = 0; 6572 err = 0;
diff --git a/net/core/dev_addr_lists.c b/net/core/dev_addr_lists.c
index 6cda4e2c2132..ec40a849fc42 100644
--- a/net/core/dev_addr_lists.c
+++ b/net/core/dev_addr_lists.c
@@ -752,7 +752,7 @@ int dev_mc_del_global(struct net_device *dev, const unsigned char *addr)
752EXPORT_SYMBOL(dev_mc_del_global); 752EXPORT_SYMBOL(dev_mc_del_global);
753 753
754/** 754/**
755 * dev_mc_sync - Synchronize device's unicast list to another device 755 * dev_mc_sync - Synchronize device's multicast list to another device
756 * @to: destination device 756 * @to: destination device
757 * @from: source device 757 * @from: source device
758 * 758 *
@@ -780,7 +780,7 @@ int dev_mc_sync(struct net_device *to, struct net_device *from)
780EXPORT_SYMBOL(dev_mc_sync); 780EXPORT_SYMBOL(dev_mc_sync);
781 781
782/** 782/**
783 * dev_mc_sync_multiple - Synchronize device's unicast list to another 783 * dev_mc_sync_multiple - Synchronize device's multicast list to another
784 * device, but allow for multiple calls to sync to multiple devices. 784 * device, but allow for multiple calls to sync to multiple devices.
785 * @to: destination device 785 * @to: destination device
786 * @from: source device 786 * @from: source device
diff --git a/net/core/drop_monitor.c b/net/core/drop_monitor.c
index 5e78d44333b9..e70301eb7a4a 100644
--- a/net/core/drop_monitor.c
+++ b/net/core/drop_monitor.c
@@ -64,7 +64,6 @@ static struct genl_family net_drop_monitor_family = {
64 .hdrsize = 0, 64 .hdrsize = 0,
65 .name = "NET_DM", 65 .name = "NET_DM",
66 .version = 2, 66 .version = 2,
67 .maxattr = NET_DM_CMD_MAX,
68}; 67};
69 68
70static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data); 69static DEFINE_PER_CPU(struct per_cpu_dm_data, dm_cpu_data);
@@ -106,6 +105,10 @@ static struct sk_buff *reset_per_cpu_data(struct per_cpu_dm_data *data)
106 return skb; 105 return skb;
107} 106}
108 107
108static struct genl_multicast_group dropmon_mcgrps[] = {
109 { .name = "events", },
110};
111
109static void send_dm_alert(struct work_struct *work) 112static void send_dm_alert(struct work_struct *work)
110{ 113{
111 struct sk_buff *skb; 114 struct sk_buff *skb;
@@ -116,7 +119,8 @@ static void send_dm_alert(struct work_struct *work)
116 skb = reset_per_cpu_data(data); 119 skb = reset_per_cpu_data(data);
117 120
118 if (skb) 121 if (skb)
119 genlmsg_multicast(skb, 0, NET_DM_GRP_ALERT, GFP_KERNEL); 122 genlmsg_multicast(&net_drop_monitor_family, skb, 0,
123 0, GFP_KERNEL);
120} 124}
121 125
122/* 126/*
@@ -333,7 +337,7 @@ out:
333 return NOTIFY_DONE; 337 return NOTIFY_DONE;
334} 338}
335 339
336static struct genl_ops dropmon_ops[] = { 340static const struct genl_ops dropmon_ops[] = {
337 { 341 {
338 .cmd = NET_DM_CMD_CONFIG, 342 .cmd = NET_DM_CMD_CONFIG,
339 .doit = net_dm_cmd_config, 343 .doit = net_dm_cmd_config,
@@ -364,13 +368,13 @@ static int __init init_net_drop_monitor(void)
364 return -ENOSPC; 368 return -ENOSPC;
365 } 369 }
366 370
367 rc = genl_register_family_with_ops(&net_drop_monitor_family, 371 rc = genl_register_family_with_ops_groups(&net_drop_monitor_family,
368 dropmon_ops, 372 dropmon_ops, dropmon_mcgrps);
369 ARRAY_SIZE(dropmon_ops));
370 if (rc) { 373 if (rc) {
371 pr_err("Could not create drop monitor netlink family\n"); 374 pr_err("Could not create drop monitor netlink family\n");
372 return rc; 375 return rc;
373 } 376 }
377 WARN_ON(net_drop_monitor_family.mcgrp_offset != NET_DM_GRP_ALERT);
374 378
375 rc = register_netdevice_notifier(&dropmon_net_notifier); 379 rc = register_netdevice_notifier(&dropmon_net_notifier);
376 if (rc < 0) { 380 if (rc < 0) {
diff --git a/net/core/ethtool.c b/net/core/ethtool.c
index 78e9d9223e40..30071dec287a 100644
--- a/net/core/ethtool.c
+++ b/net/core/ethtool.c
@@ -81,6 +81,8 @@ static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN]
81 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation", 81 [NETIF_F_TSO6_BIT] = "tx-tcp6-segmentation",
82 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation", 82 [NETIF_F_FSO_BIT] = "tx-fcoe-segmentation",
83 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation", 83 [NETIF_F_GSO_GRE_BIT] = "tx-gre-segmentation",
84 [NETIF_F_GSO_IPIP_BIT] = "tx-ipip-segmentation",
85 [NETIF_F_GSO_SIT_BIT] = "tx-sit-segmentation",
84 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation", 86 [NETIF_F_GSO_UDP_TUNNEL_BIT] = "tx-udp_tnl-segmentation",
85 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation", 87 [NETIF_F_GSO_MPLS_BIT] = "tx-mpls-segmentation",
86 88
@@ -94,6 +96,7 @@ static const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN]
94 [NETIF_F_LOOPBACK_BIT] = "loopback", 96 [NETIF_F_LOOPBACK_BIT] = "loopback",
95 [NETIF_F_RXFCS_BIT] = "rx-fcs", 97 [NETIF_F_RXFCS_BIT] = "rx-fcs",
96 [NETIF_F_RXALL_BIT] = "rx-all", 98 [NETIF_F_RXALL_BIT] = "rx-all",
99 [NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
97}; 100};
98 101
99static int ethtool_get_features(struct net_device *dev, void __user *useraddr) 102static int ethtool_get_features(struct net_device *dev, void __user *useraddr)
diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index 2e654138433c..f409e0bd35c0 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -460,7 +460,8 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
460 if (frh->action && (frh->action != rule->action)) 460 if (frh->action && (frh->action != rule->action))
461 continue; 461 continue;
462 462
463 if (frh->table && (frh_get_table(frh, tb) != rule->table)) 463 if (frh_get_table(frh, tb) &&
464 (frh_get_table(frh, tb) != rule->table))
464 continue; 465 continue;
465 466
466 if (tb[FRA_PRIORITY] && 467 if (tb[FRA_PRIORITY] &&
diff --git a/net/core/filter.c b/net/core/filter.c
index 01b780856db2..ad30d626a5bd 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -36,7 +36,6 @@
36#include <asm/uaccess.h> 36#include <asm/uaccess.h>
37#include <asm/unaligned.h> 37#include <asm/unaligned.h>
38#include <linux/filter.h> 38#include <linux/filter.h>
39#include <linux/reciprocal_div.h>
40#include <linux/ratelimit.h> 39#include <linux/ratelimit.h>
41#include <linux/seccomp.h> 40#include <linux/seccomp.h>
42#include <linux/if_vlan.h> 41#include <linux/if_vlan.h>
@@ -166,7 +165,7 @@ unsigned int sk_run_filter(const struct sk_buff *skb,
166 A /= X; 165 A /= X;
167 continue; 166 continue;
168 case BPF_S_ALU_DIV_K: 167 case BPF_S_ALU_DIV_K:
169 A = reciprocal_divide(A, K); 168 A /= K;
170 continue; 169 continue;
171 case BPF_S_ALU_MOD_X: 170 case BPF_S_ALU_MOD_X:
172 if (X == 0) 171 if (X == 0)
@@ -553,11 +552,6 @@ int sk_chk_filter(struct sock_filter *filter, unsigned int flen)
553 /* Some instructions need special checks */ 552 /* Some instructions need special checks */
554 switch (code) { 553 switch (code) {
555 case BPF_S_ALU_DIV_K: 554 case BPF_S_ALU_DIV_K:
556 /* check for division by zero */
557 if (ftest->k == 0)
558 return -EINVAL;
559 ftest->k = reciprocal_value(ftest->k);
560 break;
561 case BPF_S_ALU_MOD_K: 555 case BPF_S_ALU_MOD_K:
562 /* check for division by zero */ 556 /* check for division by zero */
563 if (ftest->k == 0) 557 if (ftest->k == 0)
@@ -853,27 +847,7 @@ void sk_decode_filter(struct sock_filter *filt, struct sock_filter *to)
853 to->code = decodes[code]; 847 to->code = decodes[code];
854 to->jt = filt->jt; 848 to->jt = filt->jt;
855 to->jf = filt->jf; 849 to->jf = filt->jf;
856 850 to->k = filt->k;
857 if (code == BPF_S_ALU_DIV_K) {
858 /*
859 * When loaded this rule user gave us X, which was
860 * translated into R = r(X). Now we calculate the
861 * RR = r(R) and report it back. If next time this
862 * value is loaded and RRR = r(RR) is calculated
863 * then the R == RRR will be true.
864 *
865 * One exception. X == 1 translates into R == 0 and
866 * we can't calculate RR out of it with r().
867 */
868
869 if (filt->k == 0)
870 to->k = 1;
871 else
872 to->k = reciprocal_value(filt->k);
873
874 BUG_ON(reciprocal_value(to->k) != filt->k);
875 } else
876 to->k = filt->k;
877} 851}
878 852
879int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len) 853int sk_get_filter(struct sock *sk, struct sock_filter __user *ubuf, unsigned int len)
diff --git a/net/core/flow_dissector.c b/net/core/flow_dissector.c
index 8d7d0dd72db2..2fc5beaf5783 100644
--- a/net/core/flow_dissector.c
+++ b/net/core/flow_dissector.c
@@ -25,9 +25,35 @@ static void iph_to_flow_copy_addrs(struct flow_keys *flow, const struct iphdr *i
25 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst)); 25 memcpy(&flow->src, &iph->saddr, sizeof(flow->src) + sizeof(flow->dst));
26} 26}
27 27
28/**
29 * skb_flow_get_ports - extract the upper layer ports and return them
30 * @skb: buffer to extract the ports from
31 * @thoff: transport header offset
32 * @ip_proto: protocol for which to get port offset
33 *
34 * The function will try to retrieve the ports at offset thoff + poff where poff
35 * is the protocol port offset returned from proto_ports_offset
36 */
37__be32 skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto)
38{
39 int poff = proto_ports_offset(ip_proto);
40
41 if (poff >= 0) {
42 __be32 *ports, _ports;
43
44 ports = skb_header_pointer(skb, thoff + poff,
45 sizeof(_ports), &_ports);
46 if (ports)
47 return *ports;
48 }
49
50 return 0;
51}
52EXPORT_SYMBOL(skb_flow_get_ports);
53
28bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow) 54bool skb_flow_dissect(const struct sk_buff *skb, struct flow_keys *flow)
29{ 55{
30 int poff, nhoff = skb_network_offset(skb); 56 int nhoff = skb_network_offset(skb);
31 u8 ip_proto; 57 u8 ip_proto;
32 __be16 proto = skb->protocol; 58 __be16 proto = skb->protocol;
33 59
@@ -40,15 +66,15 @@ again:
40 struct iphdr _iph; 66 struct iphdr _iph;
41ip: 67ip:
42 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 68 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
43 if (!iph) 69 if (!iph || iph->ihl < 5)
44 return false; 70 return false;
71 nhoff += iph->ihl * 4;
45 72
73 ip_proto = iph->protocol;
46 if (ip_is_fragment(iph)) 74 if (ip_is_fragment(iph))
47 ip_proto = 0; 75 ip_proto = 0;
48 else 76
49 ip_proto = iph->protocol;
50 iph_to_flow_copy_addrs(flow, iph); 77 iph_to_flow_copy_addrs(flow, iph);
51 nhoff += iph->ihl * 4;
52 break; 78 break;
53 } 79 }
54 case __constant_htons(ETH_P_IPV6): { 80 case __constant_htons(ETH_P_IPV6): {
@@ -150,16 +176,7 @@ ipv6:
150 } 176 }
151 177
152 flow->ip_proto = ip_proto; 178 flow->ip_proto = ip_proto;
153 poff = proto_ports_offset(ip_proto); 179 flow->ports = skb_flow_get_ports(skb, nhoff, ip_proto);
154 if (poff >= 0) {
155 __be32 *ports, _ports;
156
157 ports = skb_header_pointer(skb, nhoff + poff,
158 sizeof(_ports), &_ports);
159 if (ports)
160 flow->ports = *ports;
161 }
162
163 flow->thoff = (u16) nhoff; 180 flow->thoff = (u16) nhoff;
164 181
165 return true; 182 return true;
@@ -167,6 +184,22 @@ ipv6:
167EXPORT_SYMBOL(skb_flow_dissect); 184EXPORT_SYMBOL(skb_flow_dissect);
168 185
169static u32 hashrnd __read_mostly; 186static u32 hashrnd __read_mostly;
187static __always_inline void __flow_hash_secret_init(void)
188{
189 net_get_random_once(&hashrnd, sizeof(hashrnd));
190}
191
192static __always_inline u32 __flow_hash_3words(u32 a, u32 b, u32 c)
193{
194 __flow_hash_secret_init();
195 return jhash_3words(a, b, c, hashrnd);
196}
197
198static __always_inline u32 __flow_hash_1word(u32 a)
199{
200 __flow_hash_secret_init();
201 return jhash_1word(a, hashrnd);
202}
170 203
171/* 204/*
172 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses 205 * __skb_get_rxhash: calculate a flow hash based on src/dst addresses
@@ -193,9 +226,9 @@ void __skb_get_rxhash(struct sk_buff *skb)
193 swap(keys.port16[0], keys.port16[1]); 226 swap(keys.port16[0], keys.port16[1]);
194 } 227 }
195 228
196 hash = jhash_3words((__force u32)keys.dst, 229 hash = __flow_hash_3words((__force u32)keys.dst,
197 (__force u32)keys.src, 230 (__force u32)keys.src,
198 (__force u32)keys.ports, hashrnd); 231 (__force u32)keys.ports);
199 if (!hash) 232 if (!hash)
200 hash = 1; 233 hash = 1;
201 234
@@ -231,7 +264,7 @@ u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb,
231 hash = skb->sk->sk_hash; 264 hash = skb->sk->sk_hash;
232 else 265 else
233 hash = (__force u16) skb->protocol; 266 hash = (__force u16) skb->protocol;
234 hash = jhash_1word(hash, hashrnd); 267 hash = __flow_hash_1word(hash);
235 268
236 return (u16) (((u64) hash * qcount) >> 32) + qoffset; 269 return (u16) (((u64) hash * qcount) >> 32) + qoffset;
237} 270}
@@ -323,7 +356,7 @@ static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
323 else 356 else
324 hash = (__force u16) skb->protocol ^ 357 hash = (__force u16) skb->protocol ^
325 skb->rxhash; 358 skb->rxhash;
326 hash = jhash_1word(hash, hashrnd); 359 hash = __flow_hash_1word(hash);
327 queue_index = map->queues[ 360 queue_index = map->queues[
328 ((u64)hash * map->len) >> 32]; 361 ((u64)hash * map->len) >> 32];
329 } 362 }
@@ -362,27 +395,23 @@ u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
362EXPORT_SYMBOL(__netdev_pick_tx); 395EXPORT_SYMBOL(__netdev_pick_tx);
363 396
364struct netdev_queue *netdev_pick_tx(struct net_device *dev, 397struct netdev_queue *netdev_pick_tx(struct net_device *dev,
365 struct sk_buff *skb) 398 struct sk_buff *skb,
399 void *accel_priv)
366{ 400{
367 int queue_index = 0; 401 int queue_index = 0;
368 402
369 if (dev->real_num_tx_queues != 1) { 403 if (dev->real_num_tx_queues != 1) {
370 const struct net_device_ops *ops = dev->netdev_ops; 404 const struct net_device_ops *ops = dev->netdev_ops;
371 if (ops->ndo_select_queue) 405 if (ops->ndo_select_queue)
372 queue_index = ops->ndo_select_queue(dev, skb); 406 queue_index = ops->ndo_select_queue(dev, skb,
407 accel_priv);
373 else 408 else
374 queue_index = __netdev_pick_tx(dev, skb); 409 queue_index = __netdev_pick_tx(dev, skb);
375 queue_index = dev_cap_txqueue(dev, queue_index); 410
411 if (!accel_priv)
412 queue_index = dev_cap_txqueue(dev, queue_index);
376 } 413 }
377 414
378 skb_set_queue_mapping(skb, queue_index); 415 skb_set_queue_mapping(skb, queue_index);
379 return netdev_get_tx_queue(dev, queue_index); 416 return netdev_get_tx_queue(dev, queue_index);
380} 417}
381
382static int __init initialize_hashrnd(void)
383{
384 get_random_bytes(&hashrnd, sizeof(hashrnd));
385 return 0;
386}
387
388late_initcall_sync(initialize_hashrnd);
diff --git a/net/core/iovec.c b/net/core/iovec.c
index b77eeecc0011..b61869429f4c 100644
--- a/net/core/iovec.c
+++ b/net/core/iovec.c
@@ -48,7 +48,8 @@ int verify_iovec(struct msghdr *m, struct iovec *iov, struct sockaddr_storage *a
48 if (err < 0) 48 if (err < 0)
49 return err; 49 return err;
50 } 50 }
51 m->msg_name = address; 51 if (m->msg_name)
52 m->msg_name = address;
52 } else { 53 } else {
53 m->msg_name = NULL; 54 m->msg_name = NULL;
54 } 55 }
@@ -100,7 +101,7 @@ int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
100EXPORT_SYMBOL(memcpy_toiovecend); 101EXPORT_SYMBOL(memcpy_toiovecend);
101 102
102/* 103/*
103 * Copy iovec from kernel. Returns -EFAULT on error. 104 * Copy iovec to kernel. Returns -EFAULT on error.
104 */ 105 */
105 106
106int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov, 107int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index 6072610a8672..932c6d7cf666 100644
--- a/net/core/neighbour.c
+++ b/net/core/neighbour.c
@@ -867,7 +867,7 @@ static void neigh_invalidate(struct neighbour *neigh)
867static void neigh_probe(struct neighbour *neigh) 867static void neigh_probe(struct neighbour *neigh)
868 __releases(neigh->lock) 868 __releases(neigh->lock)
869{ 869{
870 struct sk_buff *skb = skb_peek(&neigh->arp_queue); 870 struct sk_buff *skb = skb_peek_tail(&neigh->arp_queue);
871 /* keep skb alive even if arp_queue overflows */ 871 /* keep skb alive even if arp_queue overflows */
872 if (skb) 872 if (skb)
873 skb = skb_copy(skb, GFP_ATOMIC); 873 skb = skb_copy(skb, GFP_ATOMIC);
@@ -1161,6 +1161,7 @@ int neigh_update(struct neighbour *neigh, const u8 *lladdr, u8 new,
1161 neigh->parms->reachable_time : 1161 neigh->parms->reachable_time :
1162 0))); 1162 0)));
1163 neigh->nud_state = new; 1163 neigh->nud_state = new;
1164 notify = 1;
1164 } 1165 }
1165 1166
1166 if (lladdr != neigh->ha) { 1167 if (lladdr != neigh->ha) {
@@ -1274,7 +1275,7 @@ int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb)
1274 1275
1275 if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL, 1276 if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1276 skb->len) < 0 && 1277 skb->len) < 0 &&
1277 dev->header_ops->rebuild(skb)) 1278 dev_rebuild_header(skb))
1278 return 0; 1279 return 0;
1279 1280
1280 return dev_queue_xmit(skb); 1281 return dev_queue_xmit(skb);
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index d954b56b4e47..f3edf9635e02 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -1263,7 +1263,7 @@ static void netdev_release(struct device *d)
1263 BUG_ON(dev->reg_state != NETREG_RELEASED); 1263 BUG_ON(dev->reg_state != NETREG_RELEASED);
1264 1264
1265 kfree(dev->ifalias); 1265 kfree(dev->ifalias);
1266 kfree((char *)dev - dev->padded); 1266 netdev_freemem(dev);
1267} 1267}
1268 1268
1269static const void *net_namespace(struct device *d) 1269static const void *net_namespace(struct device *d)
@@ -1344,17 +1344,19 @@ int netdev_register_kobject(struct net_device *net)
1344 return error; 1344 return error;
1345} 1345}
1346 1346
1347int netdev_class_create_file(struct class_attribute *class_attr) 1347int netdev_class_create_file_ns(struct class_attribute *class_attr,
1348 const void *ns)
1348{ 1349{
1349 return class_create_file(&net_class, class_attr); 1350 return class_create_file_ns(&net_class, class_attr, ns);
1350} 1351}
1351EXPORT_SYMBOL(netdev_class_create_file); 1352EXPORT_SYMBOL(netdev_class_create_file_ns);
1352 1353
1353void netdev_class_remove_file(struct class_attribute *class_attr) 1354void netdev_class_remove_file_ns(struct class_attribute *class_attr,
1355 const void *ns)
1354{ 1356{
1355 class_remove_file(&net_class, class_attr); 1357 class_remove_file_ns(&net_class, class_attr, ns);
1356} 1358}
1357EXPORT_SYMBOL(netdev_class_remove_file); 1359EXPORT_SYMBOL(netdev_class_remove_file_ns);
1358 1360
1359int netdev_kobject_init(void) 1361int netdev_kobject_init(void)
1360{ 1362{
diff --git a/net/core/netpoll.c b/net/core/netpoll.c
index fc75c9e461b8..19fe9c717ced 100644
--- a/net/core/netpoll.c
+++ b/net/core/netpoll.c
@@ -375,7 +375,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
375 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) { 375 if (skb_queue_len(&npinfo->txq) == 0 && !netpoll_owner_active(dev)) {
376 struct netdev_queue *txq; 376 struct netdev_queue *txq;
377 377
378 txq = netdev_pick_tx(dev, skb); 378 txq = netdev_pick_tx(dev, skb, NULL);
379 379
380 /* try until next clock tick */ 380 /* try until next clock tick */
381 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; 381 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL;
@@ -386,8 +386,14 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
386 !vlan_hw_offload_capable(netif_skb_features(skb), 386 !vlan_hw_offload_capable(netif_skb_features(skb),
387 skb->vlan_proto)) { 387 skb->vlan_proto)) {
388 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb)); 388 skb = __vlan_put_tag(skb, skb->vlan_proto, vlan_tx_tag_get(skb));
389 if (unlikely(!skb)) 389 if (unlikely(!skb)) {
390 break; 390 /* This is actually a packet drop, but we
391 * don't want the code at the end of this
392 * function to try and re-queue a NULL skb.
393 */
394 status = NETDEV_TX_OK;
395 goto unlock_txq;
396 }
391 skb->vlan_tci = 0; 397 skb->vlan_tci = 0;
392 } 398 }
393 399
@@ -395,6 +401,7 @@ void netpoll_send_skb_on_dev(struct netpoll *np, struct sk_buff *skb,
395 if (status == NETDEV_TX_OK) 401 if (status == NETDEV_TX_OK)
396 txq_trans_update(txq); 402 txq_trans_update(txq);
397 } 403 }
404 unlock_txq:
398 __netif_tx_unlock(txq); 405 __netif_tx_unlock(txq);
399 406
400 if (status == NETDEV_TX_OK) 407 if (status == NETDEV_TX_OK)
@@ -636,8 +643,9 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
636 643
637 netpoll_send_skb(np, send_skb); 644 netpoll_send_skb(np, send_skb);
638 645
639 /* If there are several rx_hooks for the same address, 646 /* If there are several rx_skb_hooks for the same
640 we're fine by sending a single reply */ 647 * address we're fine by sending a single reply
648 */
641 break; 649 break;
642 } 650 }
643 spin_unlock_irqrestore(&npinfo->rx_lock, flags); 651 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
@@ -719,8 +727,9 @@ static void netpoll_neigh_reply(struct sk_buff *skb, struct netpoll_info *npinfo
719 727
720 netpoll_send_skb(np, send_skb); 728 netpoll_send_skb(np, send_skb);
721 729
722 /* If there are several rx_hooks for the same address, 730 /* If there are several rx_skb_hooks for the same
723 we're fine by sending a single reply */ 731 * address, we're fine by sending a single reply
732 */
724 break; 733 break;
725 } 734 }
726 spin_unlock_irqrestore(&npinfo->rx_lock, flags); 735 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
@@ -756,11 +765,12 @@ static bool pkt_is_ns(struct sk_buff *skb)
756 765
757int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo) 766int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
758{ 767{
759 int proto, len, ulen; 768 int proto, len, ulen, data_len;
760 int hits = 0; 769 int hits = 0, offset;
761 const struct iphdr *iph; 770 const struct iphdr *iph;
762 struct udphdr *uh; 771 struct udphdr *uh;
763 struct netpoll *np, *tmp; 772 struct netpoll *np, *tmp;
773 uint16_t source;
764 774
765 if (list_empty(&npinfo->rx_np)) 775 if (list_empty(&npinfo->rx_np))
766 goto out; 776 goto out;
@@ -820,7 +830,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
820 830
821 len -= iph->ihl*4; 831 len -= iph->ihl*4;
822 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4); 832 uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
833 offset = (unsigned char *)(uh + 1) - skb->data;
823 ulen = ntohs(uh->len); 834 ulen = ntohs(uh->len);
835 data_len = skb->len - offset;
836 source = ntohs(uh->source);
824 837
825 if (ulen != len) 838 if (ulen != len)
826 goto out; 839 goto out;
@@ -834,9 +847,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
834 if (np->local_port && np->local_port != ntohs(uh->dest)) 847 if (np->local_port && np->local_port != ntohs(uh->dest))
835 continue; 848 continue;
836 849
837 np->rx_hook(np, ntohs(uh->source), 850 np->rx_skb_hook(np, source, skb, offset, data_len);
838 (char *)(uh+1),
839 ulen - sizeof(struct udphdr));
840 hits++; 851 hits++;
841 } 852 }
842 } else { 853 } else {
@@ -859,7 +870,10 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
859 if (!pskb_may_pull(skb, sizeof(struct udphdr))) 870 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
860 goto out; 871 goto out;
861 uh = udp_hdr(skb); 872 uh = udp_hdr(skb);
873 offset = (unsigned char *)(uh + 1) - skb->data;
862 ulen = ntohs(uh->len); 874 ulen = ntohs(uh->len);
875 data_len = skb->len - offset;
876 source = ntohs(uh->source);
863 if (ulen != skb->len) 877 if (ulen != skb->len)
864 goto out; 878 goto out;
865 if (udp6_csum_init(skb, uh, IPPROTO_UDP)) 879 if (udp6_csum_init(skb, uh, IPPROTO_UDP))
@@ -872,9 +886,7 @@ int __netpoll_rx(struct sk_buff *skb, struct netpoll_info *npinfo)
872 if (np->local_port && np->local_port != ntohs(uh->dest)) 886 if (np->local_port && np->local_port != ntohs(uh->dest))
873 continue; 887 continue;
874 888
875 np->rx_hook(np, ntohs(uh->source), 889 np->rx_skb_hook(np, source, skb, offset, data_len);
876 (char *)(uh+1),
877 ulen - sizeof(struct udphdr));
878 hits++; 890 hits++;
879 } 891 }
880#endif 892#endif
@@ -1062,7 +1074,7 @@ int __netpoll_setup(struct netpoll *np, struct net_device *ndev, gfp_t gfp)
1062 1074
1063 npinfo->netpoll = np; 1075 npinfo->netpoll = np;
1064 1076
1065 if (np->rx_hook) { 1077 if (np->rx_skb_hook) {
1066 spin_lock_irqsave(&npinfo->rx_lock, flags); 1078 spin_lock_irqsave(&npinfo->rx_lock, flags);
1067 npinfo->rx_flags |= NETPOLL_RX_ENABLED; 1079 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
1068 list_add_tail(&np->rx, &npinfo->rx_np); 1080 list_add_tail(&np->rx, &npinfo->rx_np);
diff --git a/net/core/netprio_cgroup.c b/net/core/netprio_cgroup.c
index d9cd627e6a16..9b7cf6c85f82 100644
--- a/net/core/netprio_cgroup.c
+++ b/net/core/netprio_cgroup.c
@@ -222,11 +222,10 @@ static void net_prio_attach(struct cgroup_subsys_state *css,
222 struct cgroup_taskset *tset) 222 struct cgroup_taskset *tset)
223{ 223{
224 struct task_struct *p; 224 struct task_struct *p;
225 void *v; 225 void *v = (void *)(unsigned long)css->cgroup->id;
226 226
227 cgroup_taskset_for_each(p, css, tset) { 227 cgroup_taskset_for_each(p, css, tset) {
228 task_lock(p); 228 task_lock(p);
229 v = (void *)(unsigned long)task_netprioidx(p);
230 iterate_fd(p->files, 0, update_netprio, v); 229 iterate_fd(p->files, 0, update_netprio, v);
231 task_unlock(p); 230 task_unlock(p);
232 } 231 }
diff --git a/net/core/pktgen.c b/net/core/pktgen.c
index 261357a66300..a797fff7f222 100644
--- a/net/core/pktgen.c
+++ b/net/core/pktgen.c
@@ -2527,6 +2527,8 @@ static int process_ipsec(struct pktgen_dev *pkt_dev,
2527 if (x) { 2527 if (x) {
2528 int ret; 2528 int ret;
2529 __u8 *eth; 2529 __u8 *eth;
2530 struct iphdr *iph;
2531
2530 nhead = x->props.header_len - skb_headroom(skb); 2532 nhead = x->props.header_len - skb_headroom(skb);
2531 if (nhead > 0) { 2533 if (nhead > 0) {
2532 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC); 2534 ret = pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
@@ -2548,6 +2550,11 @@ static int process_ipsec(struct pktgen_dev *pkt_dev,
2548 eth = (__u8 *) skb_push(skb, ETH_HLEN); 2550 eth = (__u8 *) skb_push(skb, ETH_HLEN);
2549 memcpy(eth, pkt_dev->hh, 12); 2551 memcpy(eth, pkt_dev->hh, 12);
2550 *(u16 *) &eth[12] = protocol; 2552 *(u16 *) &eth[12] = protocol;
2553
2554 /* Update IPv4 header len as well as checksum value */
2555 iph = ip_hdr(skb);
2556 iph->tot_len = htons(skb->len - ETH_HLEN);
2557 ip_send_check(iph);
2551 } 2558 }
2552 } 2559 }
2553 return 1; 2560 return 1;
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 2a0e21de3060..cf67144d3e3c 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -1647,9 +1647,8 @@ int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1647 } 1647 }
1648 1648
1649 dev->rtnl_link_state = RTNL_LINK_INITIALIZED; 1649 dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1650 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1651 1650
1652 __dev_notify_flags(dev, old_flags); 1651 __dev_notify_flags(dev, old_flags, ~0U);
1653 return 0; 1652 return 0;
1654} 1653}
1655EXPORT_SYMBOL(rtnl_configure_link); 1654EXPORT_SYMBOL(rtnl_configure_link);
@@ -1985,14 +1984,15 @@ static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1985 return skb->len; 1984 return skb->len;
1986} 1985}
1987 1986
1988void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change) 1987void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
1988 gfp_t flags)
1989{ 1989{
1990 struct net *net = dev_net(dev); 1990 struct net *net = dev_net(dev);
1991 struct sk_buff *skb; 1991 struct sk_buff *skb;
1992 int err = -ENOBUFS; 1992 int err = -ENOBUFS;
1993 size_t if_info_size; 1993 size_t if_info_size;
1994 1994
1995 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL); 1995 skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
1996 if (skb == NULL) 1996 if (skb == NULL)
1997 goto errout; 1997 goto errout;
1998 1998
@@ -2003,7 +2003,7 @@ void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change)
2003 kfree_skb(skb); 2003 kfree_skb(skb);
2004 goto errout; 2004 goto errout;
2005 } 2005 }
2006 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL); 2006 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2007 return; 2007 return;
2008errout: 2008errout:
2009 if (err < 0) 2009 if (err < 0)
@@ -2717,7 +2717,7 @@ static int rtnetlink_event(struct notifier_block *this, unsigned long event, voi
2717 case NETDEV_JOIN: 2717 case NETDEV_JOIN:
2718 break; 2718 break;
2719 default: 2719 default:
2720 rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 2720 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
2721 break; 2721 break;
2722 } 2722 }
2723 return NOTIFY_DONE; 2723 return NOTIFY_DONE;
diff --git a/net/core/secure_seq.c b/net/core/secure_seq.c
index 8d9d05edd2eb..897da56f3aff 100644
--- a/net/core/secure_seq.c
+++ b/net/core/secure_seq.c
@@ -7,6 +7,7 @@
7#include <linux/hrtimer.h> 7#include <linux/hrtimer.h>
8#include <linux/ktime.h> 8#include <linux/ktime.h>
9#include <linux/string.h> 9#include <linux/string.h>
10#include <linux/net.h>
10 11
11#include <net/secure_seq.h> 12#include <net/secure_seq.h>
12 13
@@ -15,20 +16,9 @@
15 16
16static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned; 17static u32 net_secret[NET_SECRET_SIZE] ____cacheline_aligned;
17 18
18static void net_secret_init(void) 19static __always_inline void net_secret_init(void)
19{ 20{
20 u32 tmp; 21 net_get_random_once(net_secret, sizeof(net_secret));
21 int i;
22
23 if (likely(net_secret[0]))
24 return;
25
26 for (i = NET_SECRET_SIZE; i > 0;) {
27 do {
28 get_random_bytes(&tmp, sizeof(tmp));
29 } while (!tmp);
30 cmpxchg(&net_secret[--i], 0, tmp);
31 }
32} 22}
33#endif 23#endif
34 24
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index d81cff119f73..06e72d3cdf60 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -476,6 +476,18 @@ void skb_add_rx_frag(struct sk_buff *skb, int i, struct page *page, int off,
476} 476}
477EXPORT_SYMBOL(skb_add_rx_frag); 477EXPORT_SYMBOL(skb_add_rx_frag);
478 478
479void skb_coalesce_rx_frag(struct sk_buff *skb, int i, int size,
480 unsigned int truesize)
481{
482 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
483
484 skb_frag_size_add(frag, size);
485 skb->len += size;
486 skb->data_len += size;
487 skb->truesize += truesize;
488}
489EXPORT_SYMBOL(skb_coalesce_rx_frag);
490
479static void skb_drop_list(struct sk_buff **listp) 491static void skb_drop_list(struct sk_buff **listp)
480{ 492{
481 kfree_skb_list(*listp); 493 kfree_skb_list(*listp);
@@ -580,9 +592,6 @@ static void skb_release_head_state(struct sk_buff *skb)
580#if IS_ENABLED(CONFIG_NF_CONNTRACK) 592#if IS_ENABLED(CONFIG_NF_CONNTRACK)
581 nf_conntrack_put(skb->nfct); 593 nf_conntrack_put(skb->nfct);
582#endif 594#endif
583#ifdef NET_SKBUFF_NF_DEFRAG_NEEDED
584 nf_conntrack_put_reasm(skb->nfct_reasm);
585#endif
586#ifdef CONFIG_BRIDGE_NETFILTER 595#ifdef CONFIG_BRIDGE_NETFILTER
587 nf_bridge_put(skb->nf_bridge); 596 nf_bridge_put(skb->nf_bridge);
588#endif 597#endif
@@ -903,6 +912,9 @@ EXPORT_SYMBOL(skb_clone);
903 912
904static void skb_headers_offset_update(struct sk_buff *skb, int off) 913static void skb_headers_offset_update(struct sk_buff *skb, int off)
905{ 914{
915 /* Only adjust this if it actually is csum_start rather than csum */
916 if (skb->ip_summed == CHECKSUM_PARTIAL)
917 skb->csum_start += off;
906 /* {transport,network,mac}_header and tail are relative to skb->head */ 918 /* {transport,network,mac}_header and tail are relative to skb->head */
907 skb->transport_header += off; 919 skb->transport_header += off;
908 skb->network_header += off; 920 skb->network_header += off;
@@ -1036,8 +1048,8 @@ EXPORT_SYMBOL(__pskb_copy);
1036 * @ntail: room to add at tail 1048 * @ntail: room to add at tail
1037 * @gfp_mask: allocation priority 1049 * @gfp_mask: allocation priority
1038 * 1050 *
1039 * Expands (or creates identical copy, if &nhead and &ntail are zero) 1051 * Expands (or creates identical copy, if @nhead and @ntail are zero)
1040 * header of skb. &sk_buff itself is not changed. &sk_buff MUST have 1052 * header of @skb. &sk_buff itself is not changed. &sk_buff MUST have
1041 * reference count of 1. Returns zero in the case of success or error, 1053 * reference count of 1. Returns zero in the case of success or error,
1042 * if expansion failed. In the last case, &sk_buff is not changed. 1054 * if expansion failed. In the last case, &sk_buff is not changed.
1043 * 1055 *
@@ -1109,9 +1121,6 @@ int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,
1109#endif 1121#endif
1110 skb->tail += off; 1122 skb->tail += off;
1111 skb_headers_offset_update(skb, nhead); 1123 skb_headers_offset_update(skb, nhead);
1112 /* Only adjust this if it actually is csum_start rather than csum */
1113 if (skb->ip_summed == CHECKSUM_PARTIAL)
1114 skb->csum_start += nhead;
1115 skb->cloned = 0; 1124 skb->cloned = 0;
1116 skb->hdr_len = 0; 1125 skb->hdr_len = 0;
1117 skb->nohdr = 0; 1126 skb->nohdr = 0;
@@ -1176,7 +1185,6 @@ struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1176 NUMA_NO_NODE); 1185 NUMA_NO_NODE);
1177 int oldheadroom = skb_headroom(skb); 1186 int oldheadroom = skb_headroom(skb);
1178 int head_copy_len, head_copy_off; 1187 int head_copy_len, head_copy_off;
1179 int off;
1180 1188
1181 if (!n) 1189 if (!n)
1182 return NULL; 1190 return NULL;
@@ -1200,11 +1208,7 @@ struct sk_buff *skb_copy_expand(const struct sk_buff *skb,
1200 1208
1201 copy_skb_header(n, skb); 1209 copy_skb_header(n, skb);
1202 1210
1203 off = newheadroom - oldheadroom; 1211 skb_headers_offset_update(n, newheadroom - oldheadroom);
1204 if (n->ip_summed == CHECKSUM_PARTIAL)
1205 n->csum_start += off;
1206
1207 skb_headers_offset_update(n, off);
1208 1212
1209 return n; 1213 return n;
1210} 1214}
@@ -1257,6 +1261,29 @@ free_skb:
1257EXPORT_SYMBOL(skb_pad); 1261EXPORT_SYMBOL(skb_pad);
1258 1262
1259/** 1263/**
1264 * pskb_put - add data to the tail of a potentially fragmented buffer
1265 * @skb: start of the buffer to use
1266 * @tail: tail fragment of the buffer to use
1267 * @len: amount of data to add
1268 *
1269 * This function extends the used data area of the potentially
1270 * fragmented buffer. @tail must be the last fragment of @skb -- or
1271 * @skb itself. If this would exceed the total buffer size the kernel
1272 * will panic. A pointer to the first byte of the extra data is
1273 * returned.
1274 */
1275
1276unsigned char *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
1277{
1278 if (tail != skb) {
1279 skb->data_len += len;
1280 skb->len += len;
1281 }
1282 return skb_put(tail, len);
1283}
1284EXPORT_SYMBOL_GPL(pskb_put);
1285
1286/**
1260 * skb_put - add data to a buffer 1287 * skb_put - add data to a buffer
1261 * @skb: buffer to use 1288 * @skb: buffer to use
1262 * @len: amount of data to add 1289 * @len: amount of data to add
@@ -1933,9 +1960,8 @@ fault:
1933EXPORT_SYMBOL(skb_store_bits); 1960EXPORT_SYMBOL(skb_store_bits);
1934 1961
1935/* Checksum skb data. */ 1962/* Checksum skb data. */
1936 1963__wsum __skb_checksum(const struct sk_buff *skb, int offset, int len,
1937__wsum skb_checksum(const struct sk_buff *skb, int offset, 1964 __wsum csum, const struct skb_checksum_ops *ops)
1938 int len, __wsum csum)
1939{ 1965{
1940 int start = skb_headlen(skb); 1966 int start = skb_headlen(skb);
1941 int i, copy = start - offset; 1967 int i, copy = start - offset;
@@ -1946,7 +1972,7 @@ __wsum skb_checksum(const struct sk_buff *skb, int offset,
1946 if (copy > 0) { 1972 if (copy > 0) {
1947 if (copy > len) 1973 if (copy > len)
1948 copy = len; 1974 copy = len;
1949 csum = csum_partial(skb->data + offset, copy, csum); 1975 csum = ops->update(skb->data + offset, copy, csum);
1950 if ((len -= copy) == 0) 1976 if ((len -= copy) == 0)
1951 return csum; 1977 return csum;
1952 offset += copy; 1978 offset += copy;
@@ -1967,10 +1993,10 @@ __wsum skb_checksum(const struct sk_buff *skb, int offset,
1967 if (copy > len) 1993 if (copy > len)
1968 copy = len; 1994 copy = len;
1969 vaddr = kmap_atomic(skb_frag_page(frag)); 1995 vaddr = kmap_atomic(skb_frag_page(frag));
1970 csum2 = csum_partial(vaddr + frag->page_offset + 1996 csum2 = ops->update(vaddr + frag->page_offset +
1971 offset - start, copy, 0); 1997 offset - start, copy, 0);
1972 kunmap_atomic(vaddr); 1998 kunmap_atomic(vaddr);
1973 csum = csum_block_add(csum, csum2, pos); 1999 csum = ops->combine(csum, csum2, pos, copy);
1974 if (!(len -= copy)) 2000 if (!(len -= copy))
1975 return csum; 2001 return csum;
1976 offset += copy; 2002 offset += copy;
@@ -1989,9 +2015,9 @@ __wsum skb_checksum(const struct sk_buff *skb, int offset,
1989 __wsum csum2; 2015 __wsum csum2;
1990 if (copy > len) 2016 if (copy > len)
1991 copy = len; 2017 copy = len;
1992 csum2 = skb_checksum(frag_iter, offset - start, 2018 csum2 = __skb_checksum(frag_iter, offset - start,
1993 copy, 0); 2019 copy, 0, ops);
1994 csum = csum_block_add(csum, csum2, pos); 2020 csum = ops->combine(csum, csum2, pos, copy);
1995 if ((len -= copy) == 0) 2021 if ((len -= copy) == 0)
1996 return csum; 2022 return csum;
1997 offset += copy; 2023 offset += copy;
@@ -2003,6 +2029,18 @@ __wsum skb_checksum(const struct sk_buff *skb, int offset,
2003 2029
2004 return csum; 2030 return csum;
2005} 2031}
2032EXPORT_SYMBOL(__skb_checksum);
2033
2034__wsum skb_checksum(const struct sk_buff *skb, int offset,
2035 int len, __wsum csum)
2036{
2037 const struct skb_checksum_ops ops = {
2038 .update = csum_partial_ext,
2039 .combine = csum_block_add_ext,
2040 };
2041
2042 return __skb_checksum(skb, offset, len, csum, &ops);
2043}
2006EXPORT_SYMBOL(skb_checksum); 2044EXPORT_SYMBOL(skb_checksum);
2007 2045
2008/* Both of above in one bottle. */ 2046/* Both of above in one bottle. */
@@ -2522,14 +2560,14 @@ EXPORT_SYMBOL(skb_prepare_seq_read);
2522 * @data: destination pointer for data to be returned 2560 * @data: destination pointer for data to be returned
2523 * @st: state variable 2561 * @st: state variable
2524 * 2562 *
2525 * Reads a block of skb data at &consumed relative to the 2563 * Reads a block of skb data at @consumed relative to the
2526 * lower offset specified to skb_prepare_seq_read(). Assigns 2564 * lower offset specified to skb_prepare_seq_read(). Assigns
2527 * the head of the data block to &data and returns the length 2565 * the head of the data block to @data and returns the length
2528 * of the block or 0 if the end of the skb data or the upper 2566 * of the block or 0 if the end of the skb data or the upper
2529 * offset has been reached. 2567 * offset has been reached.
2530 * 2568 *
2531 * The caller is not required to consume all of the data 2569 * The caller is not required to consume all of the data
2532 * returned, i.e. &consumed is typically set to the number 2570 * returned, i.e. @consumed is typically set to the number
2533 * of bytes already consumed and the next call to 2571 * of bytes already consumed and the next call to
2534 * skb_seq_read() will return the remaining part of the block. 2572 * skb_seq_read() will return the remaining part of the block.
2535 * 2573 *
@@ -2758,6 +2796,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2758 struct sk_buff *segs = NULL; 2796 struct sk_buff *segs = NULL;
2759 struct sk_buff *tail = NULL; 2797 struct sk_buff *tail = NULL;
2760 struct sk_buff *fskb = skb_shinfo(skb)->frag_list; 2798 struct sk_buff *fskb = skb_shinfo(skb)->frag_list;
2799 skb_frag_t *skb_frag = skb_shinfo(skb)->frags;
2761 unsigned int mss = skb_shinfo(skb)->gso_size; 2800 unsigned int mss = skb_shinfo(skb)->gso_size;
2762 unsigned int doffset = skb->data - skb_mac_header(skb); 2801 unsigned int doffset = skb->data - skb_mac_header(skb);
2763 unsigned int offset = doffset; 2802 unsigned int offset = doffset;
@@ -2797,16 +2836,38 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2797 if (hsize > len || !sg) 2836 if (hsize > len || !sg)
2798 hsize = len; 2837 hsize = len;
2799 2838
2800 if (!hsize && i >= nfrags) { 2839 if (!hsize && i >= nfrags && skb_headlen(fskb) &&
2801 BUG_ON(fskb->len != len); 2840 (skb_headlen(fskb) == len || sg)) {
2841 BUG_ON(skb_headlen(fskb) > len);
2842
2843 i = 0;
2844 nfrags = skb_shinfo(fskb)->nr_frags;
2845 skb_frag = skb_shinfo(fskb)->frags;
2846 pos += skb_headlen(fskb);
2847
2848 while (pos < offset + len) {
2849 BUG_ON(i >= nfrags);
2850
2851 size = skb_frag_size(skb_frag);
2852 if (pos + size > offset + len)
2853 break;
2854
2855 i++;
2856 pos += size;
2857 skb_frag++;
2858 }
2802 2859
2803 pos += len;
2804 nskb = skb_clone(fskb, GFP_ATOMIC); 2860 nskb = skb_clone(fskb, GFP_ATOMIC);
2805 fskb = fskb->next; 2861 fskb = fskb->next;
2806 2862
2807 if (unlikely(!nskb)) 2863 if (unlikely(!nskb))
2808 goto err; 2864 goto err;
2809 2865
2866 if (unlikely(pskb_trim(nskb, len))) {
2867 kfree_skb(nskb);
2868 goto err;
2869 }
2870
2810 hsize = skb_end_offset(nskb); 2871 hsize = skb_end_offset(nskb);
2811 if (skb_cow_head(nskb, doffset + headroom)) { 2872 if (skb_cow_head(nskb, doffset + headroom)) {
2812 kfree_skb(nskb); 2873 kfree_skb(nskb);
@@ -2837,20 +2898,13 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2837 __copy_skb_header(nskb, skb); 2898 __copy_skb_header(nskb, skb);
2838 nskb->mac_len = skb->mac_len; 2899 nskb->mac_len = skb->mac_len;
2839 2900
2840 /* nskb and skb might have different headroom */ 2901 skb_headers_offset_update(nskb, skb_headroom(nskb) - headroom);
2841 if (nskb->ip_summed == CHECKSUM_PARTIAL)
2842 nskb->csum_start += skb_headroom(nskb) - headroom;
2843
2844 skb_reset_mac_header(nskb);
2845 skb_set_network_header(nskb, skb->mac_len);
2846 nskb->transport_header = (nskb->network_header +
2847 skb_network_header_len(skb));
2848 2902
2849 skb_copy_from_linear_data_offset(skb, -tnl_hlen, 2903 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
2850 nskb->data - tnl_hlen, 2904 nskb->data - tnl_hlen,
2851 doffset + tnl_hlen); 2905 doffset + tnl_hlen);
2852 2906
2853 if (fskb != skb_shinfo(skb)->frag_list) 2907 if (nskb->len == len + doffset)
2854 goto perform_csum_check; 2908 goto perform_csum_check;
2855 2909
2856 if (!sg) { 2910 if (!sg) {
@@ -2868,8 +2922,28 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2868 2922
2869 skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG; 2923 skb_shinfo(nskb)->tx_flags = skb_shinfo(skb)->tx_flags & SKBTX_SHARED_FRAG;
2870 2924
2871 while (pos < offset + len && i < nfrags) { 2925 while (pos < offset + len) {
2872 *frag = skb_shinfo(skb)->frags[i]; 2926 if (i >= nfrags) {
2927 BUG_ON(skb_headlen(fskb));
2928
2929 i = 0;
2930 nfrags = skb_shinfo(fskb)->nr_frags;
2931 skb_frag = skb_shinfo(fskb)->frags;
2932
2933 BUG_ON(!nfrags);
2934
2935 fskb = fskb->next;
2936 }
2937
2938 if (unlikely(skb_shinfo(nskb)->nr_frags >=
2939 MAX_SKB_FRAGS)) {
2940 net_warn_ratelimited(
2941 "skb_segment: too many frags: %u %u\n",
2942 pos, mss);
2943 goto err;
2944 }
2945
2946 *frag = *skb_frag;
2873 __skb_frag_ref(frag); 2947 __skb_frag_ref(frag);
2874 size = skb_frag_size(frag); 2948 size = skb_frag_size(frag);
2875 2949
@@ -2882,6 +2956,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2882 2956
2883 if (pos + size <= offset + len) { 2957 if (pos + size <= offset + len) {
2884 i++; 2958 i++;
2959 skb_frag++;
2885 pos += size; 2960 pos += size;
2886 } else { 2961 } else {
2887 skb_frag_size_sub(frag, pos + size - (offset + len)); 2962 skb_frag_size_sub(frag, pos + size - (offset + len));
@@ -2891,25 +2966,6 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2891 frag++; 2966 frag++;
2892 } 2967 }
2893 2968
2894 if (pos < offset + len) {
2895 struct sk_buff *fskb2 = fskb;
2896
2897 BUG_ON(pos + fskb->len != offset + len);
2898
2899 pos += fskb->len;
2900 fskb = fskb->next;
2901
2902 if (fskb2->next) {
2903 fskb2 = skb_clone(fskb2, GFP_ATOMIC);
2904 if (!fskb2)
2905 goto err;
2906 } else
2907 skb_get(fskb2);
2908
2909 SKB_FRAG_ASSERT(nskb);
2910 skb_shinfo(nskb)->frag_list = fskb2;
2911 }
2912
2913skip_fraglist: 2969skip_fraglist:
2914 nskb->data_len = len - hsize; 2970 nskb->data_len = len - hsize;
2915 nskb->len += nskb->data_len; 2971 nskb->len += nskb->data_len;
@@ -2936,32 +2992,30 @@ EXPORT_SYMBOL_GPL(skb_segment);
2936 2992
2937int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 2993int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2938{ 2994{
2939 struct sk_buff *p = *head; 2995 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
2940 struct sk_buff *nskb;
2941 struct skb_shared_info *skbinfo = skb_shinfo(skb);
2942 struct skb_shared_info *pinfo = skb_shinfo(p);
2943 unsigned int headroom;
2944 unsigned int len = skb_gro_len(skb);
2945 unsigned int offset = skb_gro_offset(skb); 2996 unsigned int offset = skb_gro_offset(skb);
2946 unsigned int headlen = skb_headlen(skb); 2997 unsigned int headlen = skb_headlen(skb);
2998 struct sk_buff *nskb, *lp, *p = *head;
2999 unsigned int len = skb_gro_len(skb);
2947 unsigned int delta_truesize; 3000 unsigned int delta_truesize;
3001 unsigned int headroom;
2948 3002
2949 if (p->len + len >= 65536) 3003 if (unlikely(p->len + len >= 65536))
2950 return -E2BIG; 3004 return -E2BIG;
2951 3005
2952 if (pinfo->frag_list) 3006 lp = NAPI_GRO_CB(p)->last ?: p;
2953 goto merge; 3007 pinfo = skb_shinfo(lp);
2954 else if (headlen <= offset) { 3008
3009 if (headlen <= offset) {
2955 skb_frag_t *frag; 3010 skb_frag_t *frag;
2956 skb_frag_t *frag2; 3011 skb_frag_t *frag2;
2957 int i = skbinfo->nr_frags; 3012 int i = skbinfo->nr_frags;
2958 int nr_frags = pinfo->nr_frags + i; 3013 int nr_frags = pinfo->nr_frags + i;
2959 3014
2960 offset -= headlen;
2961
2962 if (nr_frags > MAX_SKB_FRAGS) 3015 if (nr_frags > MAX_SKB_FRAGS)
2963 return -E2BIG; 3016 goto merge;
2964 3017
3018 offset -= headlen;
2965 pinfo->nr_frags = nr_frags; 3019 pinfo->nr_frags = nr_frags;
2966 skbinfo->nr_frags = 0; 3020 skbinfo->nr_frags = 0;
2967 3021
@@ -2992,7 +3046,7 @@ int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2992 unsigned int first_offset; 3046 unsigned int first_offset;
2993 3047
2994 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 3048 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2995 return -E2BIG; 3049 goto merge;
2996 3050
2997 first_offset = skb->data - 3051 first_offset = skb->data -
2998 (unsigned char *)page_address(page) + 3052 (unsigned char *)page_address(page) +
@@ -3010,7 +3064,10 @@ int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3010 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff)); 3064 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3011 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3065 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3012 goto done; 3066 goto done;
3013 } else if (skb_gro_len(p) != pinfo->gso_size) 3067 }
3068 if (pinfo->frag_list)
3069 goto merge;
3070 if (skb_gro_len(p) != pinfo->gso_size)
3014 return -E2BIG; 3071 return -E2BIG;
3015 3072
3016 headroom = skb_headroom(p); 3073 headroom = skb_headroom(p);
@@ -3062,16 +3119,24 @@ merge:
3062 3119
3063 __skb_pull(skb, offset); 3120 __skb_pull(skb, offset);
3064 3121
3065 NAPI_GRO_CB(p)->last->next = skb; 3122 if (!NAPI_GRO_CB(p)->last)
3123 skb_shinfo(p)->frag_list = skb;
3124 else
3125 NAPI_GRO_CB(p)->last->next = skb;
3066 NAPI_GRO_CB(p)->last = skb; 3126 NAPI_GRO_CB(p)->last = skb;
3067 skb_header_release(skb); 3127 skb_header_release(skb);
3128 lp = p;
3068 3129
3069done: 3130done:
3070 NAPI_GRO_CB(p)->count++; 3131 NAPI_GRO_CB(p)->count++;
3071 p->data_len += len; 3132 p->data_len += len;
3072 p->truesize += delta_truesize; 3133 p->truesize += delta_truesize;
3073 p->len += len; 3134 p->len += len;
3074 3135 if (lp != p) {
3136 lp->data_len += len;
3137 lp->truesize += delta_truesize;
3138 lp->len += len;
3139 }
3075 NAPI_GRO_CB(skb)->same_flow = 1; 3140 NAPI_GRO_CB(skb)->same_flow = 1;
3076 return 0; 3141 return 0;
3077} 3142}
@@ -3519,6 +3584,7 @@ void skb_scrub_packet(struct sk_buff *skb, bool xnet)
3519 skb->tstamp.tv64 = 0; 3584 skb->tstamp.tv64 = 0;
3520 skb->pkt_type = PACKET_HOST; 3585 skb->pkt_type = PACKET_HOST;
3521 skb->skb_iif = 0; 3586 skb->skb_iif = 0;
3587 skb->local_df = 0;
3522 skb_dst_drop(skb); 3588 skb_dst_drop(skb);
3523 skb->mark = 0; 3589 skb->mark = 0;
3524 secpath_reset(skb); 3590 secpath_reset(skb);
diff --git a/net/core/sock.c b/net/core/sock.c
index 0b39e7ae4383..5393b4b719d7 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -475,12 +475,6 @@ discard_and_relse:
475} 475}
476EXPORT_SYMBOL(sk_receive_skb); 476EXPORT_SYMBOL(sk_receive_skb);
477 477
478void sk_reset_txq(struct sock *sk)
479{
480 sk_tx_queue_clear(sk);
481}
482EXPORT_SYMBOL(sk_reset_txq);
483
484struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie) 478struct dst_entry *__sk_dst_check(struct sock *sk, u32 cookie)
485{ 479{
486 struct dst_entry *dst = __sk_dst_get(sk); 480 struct dst_entry *dst = __sk_dst_get(sk);
@@ -888,7 +882,7 @@ set_rcvbuf:
888 882
889 case SO_PEEK_OFF: 883 case SO_PEEK_OFF:
890 if (sock->ops->set_peek_off) 884 if (sock->ops->set_peek_off)
891 sock->ops->set_peek_off(sk, val); 885 ret = sock->ops->set_peek_off(sk, val);
892 else 886 else
893 ret = -EOPNOTSUPP; 887 ret = -EOPNOTSUPP;
894 break; 888 break;
@@ -914,6 +908,13 @@ set_rcvbuf:
914 } 908 }
915 break; 909 break;
916#endif 910#endif
911
912 case SO_MAX_PACING_RATE:
913 sk->sk_max_pacing_rate = val;
914 sk->sk_pacing_rate = min(sk->sk_pacing_rate,
915 sk->sk_max_pacing_rate);
916 break;
917
917 default: 918 default:
918 ret = -ENOPROTOOPT; 919 ret = -ENOPROTOOPT;
919 break; 920 break;
@@ -1177,6 +1178,10 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
1177 break; 1178 break;
1178#endif 1179#endif
1179 1180
1181 case SO_MAX_PACING_RATE:
1182 v.val = sk->sk_max_pacing_rate;
1183 break;
1184
1180 default: 1185 default:
1181 return -ENOPROTOOPT; 1186 return -ENOPROTOOPT;
1182 } 1187 }
@@ -1836,7 +1841,17 @@ EXPORT_SYMBOL(sock_alloc_send_skb);
1836/* On 32bit arches, an skb frag is limited to 2^15 */ 1841/* On 32bit arches, an skb frag is limited to 2^15 */
1837#define SKB_FRAG_PAGE_ORDER get_order(32768) 1842#define SKB_FRAG_PAGE_ORDER get_order(32768)
1838 1843
1839bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag) 1844/**
1845 * skb_page_frag_refill - check that a page_frag contains enough room
1846 * @sz: minimum size of the fragment we want to get
1847 * @pfrag: pointer to page_frag
1848 * @prio: priority for memory allocation
1849 *
1850 * Note: While this allocator tries to use high order pages, there is
1851 * no guarantee that allocations succeed. Therefore, @sz MUST be
1852 * less or equal than PAGE_SIZE.
1853 */
1854bool skb_page_frag_refill(unsigned int sz, struct page_frag *pfrag, gfp_t prio)
1840{ 1855{
1841 int order; 1856 int order;
1842 1857
@@ -1845,16 +1860,16 @@ bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1845 pfrag->offset = 0; 1860 pfrag->offset = 0;
1846 return true; 1861 return true;
1847 } 1862 }
1848 if (pfrag->offset < pfrag->size) 1863 if (pfrag->offset + sz <= pfrag->size)
1849 return true; 1864 return true;
1850 put_page(pfrag->page); 1865 put_page(pfrag->page);
1851 } 1866 }
1852 1867
1853 /* We restrict high order allocations to users that can afford to wait */ 1868 /* We restrict high order allocations to users that can afford to wait */
1854 order = (sk->sk_allocation & __GFP_WAIT) ? SKB_FRAG_PAGE_ORDER : 0; 1869 order = (prio & __GFP_WAIT) ? SKB_FRAG_PAGE_ORDER : 0;
1855 1870
1856 do { 1871 do {
1857 gfp_t gfp = sk->sk_allocation; 1872 gfp_t gfp = prio;
1858 1873
1859 if (order) 1874 if (order)
1860 gfp |= __GFP_COMP | __GFP_NOWARN; 1875 gfp |= __GFP_COMP | __GFP_NOWARN;
@@ -1866,6 +1881,15 @@ bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1866 } 1881 }
1867 } while (--order >= 0); 1882 } while (--order >= 0);
1868 1883
1884 return false;
1885}
1886EXPORT_SYMBOL(skb_page_frag_refill);
1887
1888bool sk_page_frag_refill(struct sock *sk, struct page_frag *pfrag)
1889{
1890 if (likely(skb_page_frag_refill(32U, pfrag, sk->sk_allocation)))
1891 return true;
1892
1869 sk_enter_memory_pressure(sk); 1893 sk_enter_memory_pressure(sk);
1870 sk_stream_moderate_sndbuf(sk); 1894 sk_stream_moderate_sndbuf(sk);
1871 return false; 1895 return false;
@@ -2319,6 +2343,7 @@ void sock_init_data(struct socket *sock, struct sock *sk)
2319 sk->sk_ll_usec = sysctl_net_busy_read; 2343 sk->sk_ll_usec = sysctl_net_busy_read;
2320#endif 2344#endif
2321 2345
2346 sk->sk_max_pacing_rate = ~0U;
2322 sk->sk_pacing_rate = ~0U; 2347 sk->sk_pacing_rate = ~0U;
2323 /* 2348 /*
2324 * Before updating sk_refcnt, we must commit prior changes to memory 2349 * Before updating sk_refcnt, we must commit prior changes to memory
diff --git a/net/core/utils.c b/net/core/utils.c
index aa88e23fc87a..2f737bf90b3f 100644
--- a/net/core/utils.c
+++ b/net/core/utils.c
@@ -338,3 +338,52 @@ void inet_proto_csum_replace16(__sum16 *sum, struct sk_buff *skb,
338 csum_unfold(*sum))); 338 csum_unfold(*sum)));
339} 339}
340EXPORT_SYMBOL(inet_proto_csum_replace16); 340EXPORT_SYMBOL(inet_proto_csum_replace16);
341
342struct __net_random_once_work {
343 struct work_struct work;
344 struct static_key *key;
345};
346
347static void __net_random_once_deferred(struct work_struct *w)
348{
349 struct __net_random_once_work *work =
350 container_of(w, struct __net_random_once_work, work);
351 if (!static_key_enabled(work->key))
352 static_key_slow_inc(work->key);
353 kfree(work);
354}
355
356static void __net_random_once_disable_jump(struct static_key *key)
357{
358 struct __net_random_once_work *w;
359
360 w = kmalloc(sizeof(*w), GFP_ATOMIC);
361 if (!w)
362 return;
363
364 INIT_WORK(&w->work, __net_random_once_deferred);
365 w->key = key;
366 schedule_work(&w->work);
367}
368
369bool __net_get_random_once(void *buf, int nbytes, bool *done,
370 struct static_key *done_key)
371{
372 static DEFINE_SPINLOCK(lock);
373 unsigned long flags;
374
375 spin_lock_irqsave(&lock, flags);
376 if (*done) {
377 spin_unlock_irqrestore(&lock, flags);
378 return false;
379 }
380
381 get_random_bytes(buf, nbytes);
382 *done = true;
383 spin_unlock_irqrestore(&lock, flags);
384
385 __net_random_once_disable_jump(done_key);
386
387 return true;
388}
389EXPORT_SYMBOL(__net_get_random_once);
diff --git a/net/dccp/ackvec.h b/net/dccp/ackvec.h
index a269aa7f7923..3284bfa988c0 100644
--- a/net/dccp/ackvec.h
+++ b/net/dccp/ackvec.h
@@ -101,16 +101,16 @@ struct dccp_ackvec_record {
101 u8 avr_ack_nonce:1; 101 u8 avr_ack_nonce:1;
102}; 102};
103 103
104extern int dccp_ackvec_init(void); 104int dccp_ackvec_init(void);
105extern void dccp_ackvec_exit(void); 105void dccp_ackvec_exit(void);
106 106
107extern struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority); 107struct dccp_ackvec *dccp_ackvec_alloc(const gfp_t priority);
108extern void dccp_ackvec_free(struct dccp_ackvec *av); 108void dccp_ackvec_free(struct dccp_ackvec *av);
109 109
110extern void dccp_ackvec_input(struct dccp_ackvec *av, struct sk_buff *skb); 110void dccp_ackvec_input(struct dccp_ackvec *av, struct sk_buff *skb);
111extern int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seq, u8 sum); 111int dccp_ackvec_update_records(struct dccp_ackvec *av, u64 seq, u8 sum);
112extern void dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno); 112void dccp_ackvec_clear_state(struct dccp_ackvec *av, const u64 ackno);
113extern u16 dccp_ackvec_buflen(const struct dccp_ackvec *av); 113u16 dccp_ackvec_buflen(const struct dccp_ackvec *av);
114 114
115static inline bool dccp_ackvec_is_empty(const struct dccp_ackvec *av) 115static inline bool dccp_ackvec_is_empty(const struct dccp_ackvec *av)
116{ 116{
@@ -133,7 +133,6 @@ struct dccp_ackvec_parsed {
133 struct list_head node; 133 struct list_head node;
134}; 134};
135 135
136extern int dccp_ackvec_parsed_add(struct list_head *head, 136int dccp_ackvec_parsed_add(struct list_head *head, u8 *vec, u8 len, u8 nonce);
137 u8 *vec, u8 len, u8 nonce); 137void dccp_ackvec_parsed_cleanup(struct list_head *parsed_chunks);
138extern void dccp_ackvec_parsed_cleanup(struct list_head *parsed_chunks);
139#endif /* _ACKVEC_H */ 138#endif /* _ACKVEC_H */
diff --git a/net/dccp/ccid.h b/net/dccp/ccid.h
index fb85d371a8de..6eb837a47b5c 100644
--- a/net/dccp/ccid.h
+++ b/net/dccp/ccid.h
@@ -93,8 +93,8 @@ extern struct ccid_operations ccid2_ops;
93extern struct ccid_operations ccid3_ops; 93extern struct ccid_operations ccid3_ops;
94#endif 94#endif
95 95
96extern int ccid_initialize_builtins(void); 96int ccid_initialize_builtins(void);
97extern void ccid_cleanup_builtins(void); 97void ccid_cleanup_builtins(void);
98 98
99struct ccid { 99struct ccid {
100 struct ccid_operations *ccid_ops; 100 struct ccid_operations *ccid_ops;
@@ -106,12 +106,12 @@ static inline void *ccid_priv(const struct ccid *ccid)
106 return (void *)ccid->ccid_priv; 106 return (void *)ccid->ccid_priv;
107} 107}
108 108
109extern bool ccid_support_check(u8 const *ccid_array, u8 array_len); 109bool ccid_support_check(u8 const *ccid_array, u8 array_len);
110extern int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len); 110int ccid_get_builtin_ccids(u8 **ccid_array, u8 *array_len);
111extern int ccid_getsockopt_builtin_ccids(struct sock *sk, int len, 111int ccid_getsockopt_builtin_ccids(struct sock *sk, int len,
112 char __user *, int __user *); 112 char __user *, int __user *);
113 113
114extern struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx); 114struct ccid *ccid_new(const u8 id, struct sock *sk, bool rx);
115 115
116static inline int ccid_get_current_rx_ccid(struct dccp_sock *dp) 116static inline int ccid_get_current_rx_ccid(struct dccp_sock *dp)
117{ 117{
@@ -131,8 +131,8 @@ static inline int ccid_get_current_tx_ccid(struct dccp_sock *dp)
131 return ccid->ccid_ops->ccid_id; 131 return ccid->ccid_ops->ccid_id;
132} 132}
133 133
134extern void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk); 134void ccid_hc_rx_delete(struct ccid *ccid, struct sock *sk);
135extern void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk); 135void ccid_hc_tx_delete(struct ccid *ccid, struct sock *sk);
136 136
137/* 137/*
138 * Congestion control of queued data packets via CCID decision. 138 * Congestion control of queued data packets via CCID decision.
diff --git a/net/dccp/ccids/lib/loss_interval.h b/net/dccp/ccids/lib/loss_interval.h
index d1d2f5383b7d..57f631a86ccd 100644
--- a/net/dccp/ccids/lib/loss_interval.h
+++ b/net/dccp/ccids/lib/loss_interval.h
@@ -65,9 +65,9 @@ static inline u8 tfrc_lh_length(struct tfrc_loss_hist *lh)
65 65
66struct tfrc_rx_hist; 66struct tfrc_rx_hist;
67 67
68extern int tfrc_lh_interval_add(struct tfrc_loss_hist *, struct tfrc_rx_hist *, 68int tfrc_lh_interval_add(struct tfrc_loss_hist *, struct tfrc_rx_hist *,
69 u32 (*first_li)(struct sock *), struct sock *); 69 u32 (*first_li)(struct sock *), struct sock *);
70extern u8 tfrc_lh_update_i_mean(struct tfrc_loss_hist *lh, struct sk_buff *); 70u8 tfrc_lh_update_i_mean(struct tfrc_loss_hist *lh, struct sk_buff *);
71extern void tfrc_lh_cleanup(struct tfrc_loss_hist *lh); 71void tfrc_lh_cleanup(struct tfrc_loss_hist *lh);
72 72
73#endif /* _DCCP_LI_HIST_ */ 73#endif /* _DCCP_LI_HIST_ */
diff --git a/net/dccp/ccids/lib/packet_history.h b/net/dccp/ccids/lib/packet_history.h
index 7ee4a9d9d335..ee362b0b630d 100644
--- a/net/dccp/ccids/lib/packet_history.h
+++ b/net/dccp/ccids/lib/packet_history.h
@@ -60,8 +60,8 @@ static inline struct tfrc_tx_hist_entry *
60 return head; 60 return head;
61} 61}
62 62
63extern int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno); 63int tfrc_tx_hist_add(struct tfrc_tx_hist_entry **headp, u64 seqno);
64extern void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp); 64void tfrc_tx_hist_purge(struct tfrc_tx_hist_entry **headp);
65 65
66/* Subtraction a-b modulo-16, respects circular wrap-around */ 66/* Subtraction a-b modulo-16, respects circular wrap-around */
67#define SUB16(a, b) (((a) + 16 - (b)) & 0xF) 67#define SUB16(a, b) (((a) + 16 - (b)) & 0xF)
@@ -139,20 +139,17 @@ static inline bool tfrc_rx_hist_loss_pending(const struct tfrc_rx_hist *h)
139 return h->loss_count > 0; 139 return h->loss_count > 0;
140} 140}
141 141
142extern void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h, 142void tfrc_rx_hist_add_packet(struct tfrc_rx_hist *h, const struct sk_buff *skb,
143 const struct sk_buff *skb, const u64 ndp); 143 const u64 ndp);
144 144
145extern int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb); 145int tfrc_rx_hist_duplicate(struct tfrc_rx_hist *h, struct sk_buff *skb);
146 146
147struct tfrc_loss_hist; 147struct tfrc_loss_hist;
148extern int tfrc_rx_handle_loss(struct tfrc_rx_hist *h, 148int tfrc_rx_handle_loss(struct tfrc_rx_hist *h, struct tfrc_loss_hist *lh,
149 struct tfrc_loss_hist *lh, 149 struct sk_buff *skb, const u64 ndp,
150 struct sk_buff *skb, const u64 ndp, 150 u32 (*first_li)(struct sock *sk), struct sock *sk);
151 u32 (*first_li)(struct sock *sk), 151u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, const struct sk_buff *skb);
152 struct sock *sk); 152int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h);
153extern u32 tfrc_rx_hist_sample_rtt(struct tfrc_rx_hist *h, 153void tfrc_rx_hist_purge(struct tfrc_rx_hist *h);
154 const struct sk_buff *skb);
155extern int tfrc_rx_hist_alloc(struct tfrc_rx_hist *h);
156extern void tfrc_rx_hist_purge(struct tfrc_rx_hist *h);
157 154
158#endif /* _DCCP_PKT_HIST_ */ 155#endif /* _DCCP_PKT_HIST_ */
diff --git a/net/dccp/ccids/lib/tfrc.h b/net/dccp/ccids/lib/tfrc.h
index ed698c42a5fb..40ee7d62b652 100644
--- a/net/dccp/ccids/lib/tfrc.h
+++ b/net/dccp/ccids/lib/tfrc.h
@@ -55,21 +55,21 @@ static inline u32 tfrc_ewma(const u32 avg, const u32 newval, const u8 weight)
55 return avg ? (weight * avg + (10 - weight) * newval) / 10 : newval; 55 return avg ? (weight * avg + (10 - weight) * newval) / 10 : newval;
56} 56}
57 57
58extern u32 tfrc_calc_x(u16 s, u32 R, u32 p); 58u32 tfrc_calc_x(u16 s, u32 R, u32 p);
59extern u32 tfrc_calc_x_reverse_lookup(u32 fvalue); 59u32 tfrc_calc_x_reverse_lookup(u32 fvalue);
60extern u32 tfrc_invert_loss_event_rate(u32 loss_event_rate); 60u32 tfrc_invert_loss_event_rate(u32 loss_event_rate);
61 61
62extern int tfrc_tx_packet_history_init(void); 62int tfrc_tx_packet_history_init(void);
63extern void tfrc_tx_packet_history_exit(void); 63void tfrc_tx_packet_history_exit(void);
64extern int tfrc_rx_packet_history_init(void); 64int tfrc_rx_packet_history_init(void);
65extern void tfrc_rx_packet_history_exit(void); 65void tfrc_rx_packet_history_exit(void);
66 66
67extern int tfrc_li_init(void); 67int tfrc_li_init(void);
68extern void tfrc_li_exit(void); 68void tfrc_li_exit(void);
69 69
70#ifdef CONFIG_IP_DCCP_TFRC_LIB 70#ifdef CONFIG_IP_DCCP_TFRC_LIB
71extern int tfrc_lib_init(void); 71int tfrc_lib_init(void);
72extern void tfrc_lib_exit(void); 72void tfrc_lib_exit(void);
73#else 73#else
74#define tfrc_lib_init() (0) 74#define tfrc_lib_init() (0)
75#define tfrc_lib_exit() 75#define tfrc_lib_exit()
diff --git a/net/dccp/dccp.h b/net/dccp/dccp.h
index 708e75bf623d..30948784dd58 100644
--- a/net/dccp/dccp.h
+++ b/net/dccp/dccp.h
@@ -53,7 +53,7 @@ extern struct inet_hashinfo dccp_hashinfo;
53 53
54extern struct percpu_counter dccp_orphan_count; 54extern struct percpu_counter dccp_orphan_count;
55 55
56extern void dccp_time_wait(struct sock *sk, int state, int timeo); 56void dccp_time_wait(struct sock *sk, int state, int timeo);
57 57
58/* 58/*
59 * Set safe upper bounds for header and option length. Since Data Offset is 8 59 * Set safe upper bounds for header and option length. Since Data Offset is 8
@@ -224,114 +224,108 @@ static inline void dccp_csum_outgoing(struct sk_buff *skb)
224 skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0); 224 skb->csum = skb_checksum(skb, 0, (cov > skb->len)? skb->len : cov, 0);
225} 225}
226 226
227extern void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb); 227void dccp_v4_send_check(struct sock *sk, struct sk_buff *skb);
228 228
229extern int dccp_retransmit_skb(struct sock *sk); 229int dccp_retransmit_skb(struct sock *sk);
230 230
231extern void dccp_send_ack(struct sock *sk); 231void dccp_send_ack(struct sock *sk);
232extern void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb, 232void dccp_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
233 struct request_sock *rsk); 233 struct request_sock *rsk);
234 234
235extern void dccp_send_sync(struct sock *sk, const u64 seq, 235void dccp_send_sync(struct sock *sk, const u64 seq,
236 const enum dccp_pkt_type pkt_type); 236 const enum dccp_pkt_type pkt_type);
237 237
238/* 238/*
239 * TX Packet Dequeueing Interface 239 * TX Packet Dequeueing Interface
240 */ 240 */
241extern void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb); 241void dccp_qpolicy_push(struct sock *sk, struct sk_buff *skb);
242extern bool dccp_qpolicy_full(struct sock *sk); 242bool dccp_qpolicy_full(struct sock *sk);
243extern void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb); 243void dccp_qpolicy_drop(struct sock *sk, struct sk_buff *skb);
244extern struct sk_buff *dccp_qpolicy_top(struct sock *sk); 244struct sk_buff *dccp_qpolicy_top(struct sock *sk);
245extern struct sk_buff *dccp_qpolicy_pop(struct sock *sk); 245struct sk_buff *dccp_qpolicy_pop(struct sock *sk);
246extern bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param); 246bool dccp_qpolicy_param_ok(struct sock *sk, __be32 param);
247 247
248/* 248/*
249 * TX Packet Output and TX Timers 249 * TX Packet Output and TX Timers
250 */ 250 */
251extern void dccp_write_xmit(struct sock *sk); 251void dccp_write_xmit(struct sock *sk);
252extern void dccp_write_space(struct sock *sk); 252void dccp_write_space(struct sock *sk);
253extern void dccp_flush_write_queue(struct sock *sk, long *time_budget); 253void dccp_flush_write_queue(struct sock *sk, long *time_budget);
254 254
255extern void dccp_init_xmit_timers(struct sock *sk); 255void dccp_init_xmit_timers(struct sock *sk);
256static inline void dccp_clear_xmit_timers(struct sock *sk) 256static inline void dccp_clear_xmit_timers(struct sock *sk)
257{ 257{
258 inet_csk_clear_xmit_timers(sk); 258 inet_csk_clear_xmit_timers(sk);
259} 259}
260 260
261extern unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu); 261unsigned int dccp_sync_mss(struct sock *sk, u32 pmtu);
262 262
263extern const char *dccp_packet_name(const int type); 263const char *dccp_packet_name(const int type);
264 264
265extern void dccp_set_state(struct sock *sk, const int state); 265void dccp_set_state(struct sock *sk, const int state);
266extern void dccp_done(struct sock *sk); 266void dccp_done(struct sock *sk);
267 267
268extern int dccp_reqsk_init(struct request_sock *rq, struct dccp_sock const *dp, 268int dccp_reqsk_init(struct request_sock *rq, struct dccp_sock const *dp,
269 struct sk_buff const *skb); 269 struct sk_buff const *skb);
270 270
271extern int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb); 271int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb);
272 272
273extern struct sock *dccp_create_openreq_child(struct sock *sk, 273struct sock *dccp_create_openreq_child(struct sock *sk,
274 const struct request_sock *req, 274 const struct request_sock *req,
275 const struct sk_buff *skb); 275 const struct sk_buff *skb);
276 276
277extern int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb); 277int dccp_v4_do_rcv(struct sock *sk, struct sk_buff *skb);
278 278
279extern struct sock *dccp_v4_request_recv_sock(struct sock *sk, 279struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
280 struct sk_buff *skb, 280 struct request_sock *req,
281 struct request_sock *req, 281 struct dst_entry *dst);
282 struct dst_entry *dst); 282struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
283extern struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb, 283 struct request_sock *req,
284 struct request_sock *req, 284 struct request_sock **prev);
285 struct request_sock **prev);
286 285
287extern int dccp_child_process(struct sock *parent, struct sock *child, 286int dccp_child_process(struct sock *parent, struct sock *child,
288 struct sk_buff *skb); 287 struct sk_buff *skb);
289extern int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb, 288int dccp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
290 struct dccp_hdr *dh, unsigned int len); 289 struct dccp_hdr *dh, unsigned int len);
291extern int dccp_rcv_established(struct sock *sk, struct sk_buff *skb, 290int dccp_rcv_established(struct sock *sk, struct sk_buff *skb,
292 const struct dccp_hdr *dh, const unsigned int len); 291 const struct dccp_hdr *dh, const unsigned int len);
293 292
294extern int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized); 293int dccp_init_sock(struct sock *sk, const __u8 ctl_sock_initialized);
295extern void dccp_destroy_sock(struct sock *sk); 294void dccp_destroy_sock(struct sock *sk);
296 295
297extern void dccp_close(struct sock *sk, long timeout); 296void dccp_close(struct sock *sk, long timeout);
298extern struct sk_buff *dccp_make_response(struct sock *sk, 297struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
299 struct dst_entry *dst, 298 struct request_sock *req);
300 struct request_sock *req);
301 299
302extern int dccp_connect(struct sock *sk); 300int dccp_connect(struct sock *sk);
303extern int dccp_disconnect(struct sock *sk, int flags); 301int dccp_disconnect(struct sock *sk, int flags);
304extern int dccp_getsockopt(struct sock *sk, int level, int optname, 302int dccp_getsockopt(struct sock *sk, int level, int optname,
305 char __user *optval, int __user *optlen); 303 char __user *optval, int __user *optlen);
306extern int dccp_setsockopt(struct sock *sk, int level, int optname, 304int dccp_setsockopt(struct sock *sk, int level, int optname,
307 char __user *optval, unsigned int optlen); 305 char __user *optval, unsigned int optlen);
308#ifdef CONFIG_COMPAT 306#ifdef CONFIG_COMPAT
309extern int compat_dccp_getsockopt(struct sock *sk, 307int compat_dccp_getsockopt(struct sock *sk, int level, int optname,
310 int level, int optname, 308 char __user *optval, int __user *optlen);
311 char __user *optval, int __user *optlen); 309int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
312extern int compat_dccp_setsockopt(struct sock *sk, 310 char __user *optval, unsigned int optlen);
313 int level, int optname,
314 char __user *optval, unsigned int optlen);
315#endif 311#endif
316extern int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg); 312int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
317extern int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, 313int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
318 struct msghdr *msg, size_t size); 314 size_t size);
319extern int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, 315int dccp_recvmsg(struct kiocb *iocb, struct sock *sk,
320 struct msghdr *msg, size_t len, int nonblock, 316 struct msghdr *msg, size_t len, int nonblock, int flags,
321 int flags, int *addr_len); 317 int *addr_len);
322extern void dccp_shutdown(struct sock *sk, int how); 318void dccp_shutdown(struct sock *sk, int how);
323extern int inet_dccp_listen(struct socket *sock, int backlog); 319int inet_dccp_listen(struct socket *sock, int backlog);
324extern unsigned int dccp_poll(struct file *file, struct socket *sock, 320unsigned int dccp_poll(struct file *file, struct socket *sock,
325 poll_table *wait); 321 poll_table *wait);
326extern int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, 322int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
327 int addr_len); 323
328 324struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *skb);
329extern struct sk_buff *dccp_ctl_make_reset(struct sock *sk, 325int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
330 struct sk_buff *skb); 326void dccp_send_close(struct sock *sk, const int active);
331extern int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code); 327int dccp_invalid_packet(struct sk_buff *skb);
332extern void dccp_send_close(struct sock *sk, const int active); 328u32 dccp_sample_rtt(struct sock *sk, long delta);
333extern int dccp_invalid_packet(struct sk_buff *skb);
334extern u32 dccp_sample_rtt(struct sock *sk, long delta);
335 329
336static inline int dccp_bad_service_code(const struct sock *sk, 330static inline int dccp_bad_service_code(const struct sock *sk,
337 const __be32 service) 331 const __be32 service)
@@ -475,25 +469,25 @@ static inline int dccp_ack_pending(const struct sock *sk)
475 return dccp_ackvec_pending(sk) || inet_csk_ack_scheduled(sk); 469 return dccp_ackvec_pending(sk) || inet_csk_ack_scheduled(sk);
476} 470}
477 471
478extern int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val); 472int dccp_feat_signal_nn_change(struct sock *sk, u8 feat, u64 nn_val);
479extern int dccp_feat_finalise_settings(struct dccp_sock *dp); 473int dccp_feat_finalise_settings(struct dccp_sock *dp);
480extern int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq); 474int dccp_feat_server_ccid_dependencies(struct dccp_request_sock *dreq);
481extern int dccp_feat_insert_opts(struct dccp_sock*, struct dccp_request_sock*, 475int dccp_feat_insert_opts(struct dccp_sock*, struct dccp_request_sock*,
482 struct sk_buff *skb); 476 struct sk_buff *skb);
483extern int dccp_feat_activate_values(struct sock *sk, struct list_head *fn); 477int dccp_feat_activate_values(struct sock *sk, struct list_head *fn);
484extern void dccp_feat_list_purge(struct list_head *fn_list); 478void dccp_feat_list_purge(struct list_head *fn_list);
485 479
486extern int dccp_insert_options(struct sock *sk, struct sk_buff *skb); 480int dccp_insert_options(struct sock *sk, struct sk_buff *skb);
487extern int dccp_insert_options_rsk(struct dccp_request_sock*, struct sk_buff*); 481int dccp_insert_options_rsk(struct dccp_request_sock *, struct sk_buff *);
488extern int dccp_insert_option_elapsed_time(struct sk_buff *skb, u32 elapsed); 482int dccp_insert_option_elapsed_time(struct sk_buff *skb, u32 elapsed);
489extern u32 dccp_timestamp(void); 483u32 dccp_timestamp(void);
490extern void dccp_timestamping_init(void); 484void dccp_timestamping_init(void);
491extern int dccp_insert_option(struct sk_buff *skb, unsigned char option, 485int dccp_insert_option(struct sk_buff *skb, unsigned char option,
492 const void *value, unsigned char len); 486 const void *value, unsigned char len);
493 487
494#ifdef CONFIG_SYSCTL 488#ifdef CONFIG_SYSCTL
495extern int dccp_sysctl_init(void); 489int dccp_sysctl_init(void);
496extern void dccp_sysctl_exit(void); 490void dccp_sysctl_exit(void);
497#else 491#else
498static inline int dccp_sysctl_init(void) 492static inline int dccp_sysctl_init(void)
499{ 493{
diff --git a/net/dccp/feat.h b/net/dccp/feat.h
index 90b957d34d26..0e75cebb2187 100644
--- a/net/dccp/feat.h
+++ b/net/dccp/feat.h
@@ -107,13 +107,13 @@ extern unsigned long sysctl_dccp_sequence_window;
107extern int sysctl_dccp_rx_ccid; 107extern int sysctl_dccp_rx_ccid;
108extern int sysctl_dccp_tx_ccid; 108extern int sysctl_dccp_tx_ccid;
109 109
110extern int dccp_feat_init(struct sock *sk); 110int dccp_feat_init(struct sock *sk);
111extern void dccp_feat_initialise_sysctls(void); 111void dccp_feat_initialise_sysctls(void);
112extern int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local, 112int dccp_feat_register_sp(struct sock *sk, u8 feat, u8 is_local,
113 u8 const *list, u8 len); 113 u8 const *list, u8 len);
114extern int dccp_feat_parse_options(struct sock *, struct dccp_request_sock *, 114int dccp_feat_parse_options(struct sock *, struct dccp_request_sock *,
115 u8 mand, u8 opt, u8 feat, u8 *val, u8 len); 115 u8 mand, u8 opt, u8 feat, u8 *val, u8 len);
116extern int dccp_feat_clone_list(struct list_head const *, struct list_head *); 116int dccp_feat_clone_list(struct list_head const *, struct list_head *);
117 117
118/* 118/*
119 * Encoding variable-length options and their maximum length. 119 * Encoding variable-length options and their maximum length.
@@ -127,11 +127,11 @@ extern int dccp_feat_clone_list(struct list_head const *, struct list_head *);
127 */ 127 */
128#define DCCP_OPTVAL_MAXLEN 6 128#define DCCP_OPTVAL_MAXLEN 6
129 129
130extern void dccp_encode_value_var(const u64 value, u8 *to, const u8 len); 130void dccp_encode_value_var(const u64 value, u8 *to, const u8 len);
131extern u64 dccp_decode_value_var(const u8 *bf, const u8 len); 131u64 dccp_decode_value_var(const u8 *bf, const u8 len);
132extern u64 dccp_feat_nn_get(struct sock *sk, u8 feat); 132u64 dccp_feat_nn_get(struct sock *sk, u8 feat);
133 133
134extern int dccp_insert_option_mandatory(struct sk_buff *skb); 134int dccp_insert_option_mandatory(struct sk_buff *skb);
135extern int dccp_insert_fn_opt(struct sk_buff *skb, u8 type, u8 feat, 135int dccp_insert_fn_opt(struct sk_buff *skb, u8 type, u8 feat, u8 *val, u8 len,
136 u8 *val, u8 len, bool repeat_first); 136 bool repeat_first);
137#endif /* _DCCP_FEAT_H */ 137#endif /* _DCCP_FEAT_H */
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c
index ebc54fef85a5..d9f65fc66db5 100644
--- a/net/dccp/ipv4.c
+++ b/net/dccp/ipv4.c
@@ -174,6 +174,7 @@ static inline void dccp_do_pmtu_discovery(struct sock *sk,
174 mtu = dst_mtu(dst); 174 mtu = dst_mtu(dst);
175 175
176 if (inet->pmtudisc != IP_PMTUDISC_DONT && 176 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
177 ip_sk_accept_pmtu(sk) &&
177 inet_csk(sk)->icsk_pmtu_cookie > mtu) { 178 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
178 dccp_sync_mss(sk, mtu); 179 dccp_sync_mss(sk, mtu);
179 180
@@ -409,9 +410,9 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
409 410
410 newinet = inet_sk(newsk); 411 newinet = inet_sk(newsk);
411 ireq = inet_rsk(req); 412 ireq = inet_rsk(req);
412 newinet->inet_daddr = ireq->rmt_addr; 413 newinet->inet_daddr = ireq->ir_rmt_addr;
413 newinet->inet_rcv_saddr = ireq->loc_addr; 414 newinet->inet_rcv_saddr = ireq->ir_loc_addr;
414 newinet->inet_saddr = ireq->loc_addr; 415 newinet->inet_saddr = ireq->ir_loc_addr;
415 newinet->inet_opt = ireq->opt; 416 newinet->inet_opt = ireq->opt;
416 ireq->opt = NULL; 417 ireq->opt = NULL;
417 newinet->mc_index = inet_iif(skb); 418 newinet->mc_index = inet_iif(skb);
@@ -516,10 +517,10 @@ static int dccp_v4_send_response(struct sock *sk, struct request_sock *req)
516 const struct inet_request_sock *ireq = inet_rsk(req); 517 const struct inet_request_sock *ireq = inet_rsk(req);
517 struct dccp_hdr *dh = dccp_hdr(skb); 518 struct dccp_hdr *dh = dccp_hdr(skb);
518 519
519 dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->loc_addr, 520 dh->dccph_checksum = dccp_v4_csum_finish(skb, ireq->ir_loc_addr,
520 ireq->rmt_addr); 521 ireq->ir_rmt_addr);
521 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, 522 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
522 ireq->rmt_addr, 523 ireq->ir_rmt_addr,
523 ireq->opt); 524 ireq->opt);
524 err = net_xmit_eval(err); 525 err = net_xmit_eval(err);
525 } 526 }
@@ -641,8 +642,8 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
641 goto drop_and_free; 642 goto drop_and_free;
642 643
643 ireq = inet_rsk(req); 644 ireq = inet_rsk(req);
644 ireq->loc_addr = ip_hdr(skb)->daddr; 645 ireq->ir_loc_addr = ip_hdr(skb)->daddr;
645 ireq->rmt_addr = ip_hdr(skb)->saddr; 646 ireq->ir_rmt_addr = ip_hdr(skb)->saddr;
646 647
647 /* 648 /*
648 * Step 3: Process LISTEN state 649 * Step 3: Process LISTEN state
diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index 6cf9f7782ad4..2b90a786e475 100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -67,7 +67,7 @@ static inline void dccp_v6_send_check(struct sock *sk, struct sk_buff *skb)
67 struct dccp_hdr *dh = dccp_hdr(skb); 67 struct dccp_hdr *dh = dccp_hdr(skb);
68 68
69 dccp_csum_outgoing(skb); 69 dccp_csum_outgoing(skb);
70 dh->dccph_checksum = dccp_v6_csum_finish(skb, &np->saddr, &np->daddr); 70 dh->dccph_checksum = dccp_v6_csum_finish(skb, &np->saddr, &sk->sk_v6_daddr);
71} 71}
72 72
73static inline __u64 dccp_v6_init_sequence(struct sk_buff *skb) 73static inline __u64 dccp_v6_init_sequence(struct sk_buff *skb)
@@ -216,7 +216,7 @@ out:
216 216
217static int dccp_v6_send_response(struct sock *sk, struct request_sock *req) 217static int dccp_v6_send_response(struct sock *sk, struct request_sock *req)
218{ 218{
219 struct inet6_request_sock *ireq6 = inet6_rsk(req); 219 struct inet_request_sock *ireq = inet_rsk(req);
220 struct ipv6_pinfo *np = inet6_sk(sk); 220 struct ipv6_pinfo *np = inet6_sk(sk);
221 struct sk_buff *skb; 221 struct sk_buff *skb;
222 struct in6_addr *final_p, final; 222 struct in6_addr *final_p, final;
@@ -226,12 +226,12 @@ static int dccp_v6_send_response(struct sock *sk, struct request_sock *req)
226 226
227 memset(&fl6, 0, sizeof(fl6)); 227 memset(&fl6, 0, sizeof(fl6));
228 fl6.flowi6_proto = IPPROTO_DCCP; 228 fl6.flowi6_proto = IPPROTO_DCCP;
229 fl6.daddr = ireq6->rmt_addr; 229 fl6.daddr = ireq->ir_v6_rmt_addr;
230 fl6.saddr = ireq6->loc_addr; 230 fl6.saddr = ireq->ir_v6_loc_addr;
231 fl6.flowlabel = 0; 231 fl6.flowlabel = 0;
232 fl6.flowi6_oif = ireq6->iif; 232 fl6.flowi6_oif = ireq->ir_iif;
233 fl6.fl6_dport = inet_rsk(req)->rmt_port; 233 fl6.fl6_dport = ireq->ir_rmt_port;
234 fl6.fl6_sport = inet_rsk(req)->loc_port; 234 fl6.fl6_sport = htons(ireq->ir_num);
235 security_req_classify_flow(req, flowi6_to_flowi(&fl6)); 235 security_req_classify_flow(req, flowi6_to_flowi(&fl6));
236 236
237 237
@@ -249,9 +249,9 @@ static int dccp_v6_send_response(struct sock *sk, struct request_sock *req)
249 struct dccp_hdr *dh = dccp_hdr(skb); 249 struct dccp_hdr *dh = dccp_hdr(skb);
250 250
251 dh->dccph_checksum = dccp_v6_csum_finish(skb, 251 dh->dccph_checksum = dccp_v6_csum_finish(skb,
252 &ireq6->loc_addr, 252 &ireq->ir_v6_loc_addr,
253 &ireq6->rmt_addr); 253 &ireq->ir_v6_rmt_addr);
254 fl6.daddr = ireq6->rmt_addr; 254 fl6.daddr = ireq->ir_v6_rmt_addr;
255 err = ip6_xmit(sk, skb, &fl6, np->opt, np->tclass); 255 err = ip6_xmit(sk, skb, &fl6, np->opt, np->tclass);
256 err = net_xmit_eval(err); 256 err = net_xmit_eval(err);
257 } 257 }
@@ -264,8 +264,7 @@ done:
264static void dccp_v6_reqsk_destructor(struct request_sock *req) 264static void dccp_v6_reqsk_destructor(struct request_sock *req)
265{ 265{
266 dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg); 266 dccp_feat_list_purge(&dccp_rsk(req)->dreq_featneg);
267 if (inet6_rsk(req)->pktopts != NULL) 267 kfree_skb(inet_rsk(req)->pktopts);
268 kfree_skb(inet6_rsk(req)->pktopts);
269} 268}
270 269
271static void dccp_v6_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb) 270static void dccp_v6_ctl_send_reset(struct sock *sk, struct sk_buff *rxskb)
@@ -359,7 +358,7 @@ static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
359{ 358{
360 struct request_sock *req; 359 struct request_sock *req;
361 struct dccp_request_sock *dreq; 360 struct dccp_request_sock *dreq;
362 struct inet6_request_sock *ireq6; 361 struct inet_request_sock *ireq;
363 struct ipv6_pinfo *np = inet6_sk(sk); 362 struct ipv6_pinfo *np = inet6_sk(sk);
364 const __be32 service = dccp_hdr_request(skb)->dccph_req_service; 363 const __be32 service = dccp_hdr_request(skb)->dccph_req_service;
365 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb); 364 struct dccp_skb_cb *dcb = DCCP_SKB_CB(skb);
@@ -398,22 +397,22 @@ static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
398 if (security_inet_conn_request(sk, skb, req)) 397 if (security_inet_conn_request(sk, skb, req))
399 goto drop_and_free; 398 goto drop_and_free;
400 399
401 ireq6 = inet6_rsk(req); 400 ireq = inet_rsk(req);
402 ireq6->rmt_addr = ipv6_hdr(skb)->saddr; 401 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
403 ireq6->loc_addr = ipv6_hdr(skb)->daddr; 402 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
404 403
405 if (ipv6_opt_accepted(sk, skb) || 404 if (ipv6_opt_accepted(sk, skb) ||
406 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || 405 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
407 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) { 406 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
408 atomic_inc(&skb->users); 407 atomic_inc(&skb->users);
409 ireq6->pktopts = skb; 408 ireq->pktopts = skb;
410 } 409 }
411 ireq6->iif = sk->sk_bound_dev_if; 410 ireq->ir_iif = sk->sk_bound_dev_if;
412 411
413 /* So that link locals have meaning */ 412 /* So that link locals have meaning */
414 if (!sk->sk_bound_dev_if && 413 if (!sk->sk_bound_dev_if &&
415 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL) 414 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
416 ireq6->iif = inet6_iif(skb); 415 ireq->ir_iif = inet6_iif(skb);
417 416
418 /* 417 /*
419 * Step 3: Process LISTEN state 418 * Step 3: Process LISTEN state
@@ -446,7 +445,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
446 struct request_sock *req, 445 struct request_sock *req,
447 struct dst_entry *dst) 446 struct dst_entry *dst)
448{ 447{
449 struct inet6_request_sock *ireq6 = inet6_rsk(req); 448 struct inet_request_sock *ireq = inet_rsk(req);
450 struct ipv6_pinfo *newnp, *np = inet6_sk(sk); 449 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
451 struct inet_sock *newinet; 450 struct inet_sock *newinet;
452 struct dccp6_sock *newdp6; 451 struct dccp6_sock *newdp6;
@@ -467,11 +466,11 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
467 466
468 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 467 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
469 468
470 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr); 469 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
471 470
472 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); 471 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
473 472
474 newnp->rcv_saddr = newnp->saddr; 473 newsk->sk_v6_rcv_saddr = newnp->saddr;
475 474
476 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped; 475 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
477 newsk->sk_backlog_rcv = dccp_v4_do_rcv; 476 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
@@ -505,12 +504,12 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
505 504
506 memset(&fl6, 0, sizeof(fl6)); 505 memset(&fl6, 0, sizeof(fl6));
507 fl6.flowi6_proto = IPPROTO_DCCP; 506 fl6.flowi6_proto = IPPROTO_DCCP;
508 fl6.daddr = ireq6->rmt_addr; 507 fl6.daddr = ireq->ir_v6_rmt_addr;
509 final_p = fl6_update_dst(&fl6, np->opt, &final); 508 final_p = fl6_update_dst(&fl6, np->opt, &final);
510 fl6.saddr = ireq6->loc_addr; 509 fl6.saddr = ireq->ir_v6_loc_addr;
511 fl6.flowi6_oif = sk->sk_bound_dev_if; 510 fl6.flowi6_oif = sk->sk_bound_dev_if;
512 fl6.fl6_dport = inet_rsk(req)->rmt_port; 511 fl6.fl6_dport = ireq->ir_rmt_port;
513 fl6.fl6_sport = inet_rsk(req)->loc_port; 512 fl6.fl6_sport = htons(ireq->ir_num);
514 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); 513 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
515 514
516 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false); 515 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
@@ -538,10 +537,10 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
538 537
539 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 538 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
540 539
541 newnp->daddr = ireq6->rmt_addr; 540 newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
542 newnp->saddr = ireq6->loc_addr; 541 newnp->saddr = ireq->ir_v6_loc_addr;
543 newnp->rcv_saddr = ireq6->loc_addr; 542 newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
544 newsk->sk_bound_dev_if = ireq6->iif; 543 newsk->sk_bound_dev_if = ireq->ir_iif;
545 544
546 /* Now IPv6 options... 545 /* Now IPv6 options...
547 546
@@ -554,10 +553,10 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
554 553
555 /* Clone pktoptions received with SYN */ 554 /* Clone pktoptions received with SYN */
556 newnp->pktoptions = NULL; 555 newnp->pktoptions = NULL;
557 if (ireq6->pktopts != NULL) { 556 if (ireq->pktopts != NULL) {
558 newnp->pktoptions = skb_clone(ireq6->pktopts, GFP_ATOMIC); 557 newnp->pktoptions = skb_clone(ireq->pktopts, GFP_ATOMIC);
559 consume_skb(ireq6->pktopts); 558 consume_skb(ireq->pktopts);
560 ireq6->pktopts = NULL; 559 ireq->pktopts = NULL;
561 if (newnp->pktoptions) 560 if (newnp->pktoptions)
562 skb_set_owner_r(newnp->pktoptions, newsk); 561 skb_set_owner_r(newnp->pktoptions, newsk);
563 } 562 }
@@ -852,7 +851,6 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
852 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 851 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
853 if (flowlabel == NULL) 852 if (flowlabel == NULL)
854 return -EINVAL; 853 return -EINVAL;
855 usin->sin6_addr = flowlabel->dst;
856 fl6_sock_release(flowlabel); 854 fl6_sock_release(flowlabel);
857 } 855 }
858 } 856 }
@@ -885,7 +883,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
885 return -EINVAL; 883 return -EINVAL;
886 } 884 }
887 885
888 np->daddr = usin->sin6_addr; 886 sk->sk_v6_daddr = usin->sin6_addr;
889 np->flow_label = fl6.flowlabel; 887 np->flow_label = fl6.flowlabel;
890 888
891 /* 889 /*
@@ -915,16 +913,16 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
915 goto failure; 913 goto failure;
916 } 914 }
917 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 915 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
918 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &np->rcv_saddr); 916 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr);
919 917
920 return err; 918 return err;
921 } 919 }
922 920
923 if (!ipv6_addr_any(&np->rcv_saddr)) 921 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
924 saddr = &np->rcv_saddr; 922 saddr = &sk->sk_v6_rcv_saddr;
925 923
926 fl6.flowi6_proto = IPPROTO_DCCP; 924 fl6.flowi6_proto = IPPROTO_DCCP;
927 fl6.daddr = np->daddr; 925 fl6.daddr = sk->sk_v6_daddr;
928 fl6.saddr = saddr ? *saddr : np->saddr; 926 fl6.saddr = saddr ? *saddr : np->saddr;
929 fl6.flowi6_oif = sk->sk_bound_dev_if; 927 fl6.flowi6_oif = sk->sk_bound_dev_if;
930 fl6.fl6_dport = usin->sin6_port; 928 fl6.fl6_dport = usin->sin6_port;
@@ -941,7 +939,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
941 939
942 if (saddr == NULL) { 940 if (saddr == NULL) {
943 saddr = &fl6.saddr; 941 saddr = &fl6.saddr;
944 np->rcv_saddr = *saddr; 942 sk->sk_v6_rcv_saddr = *saddr;
945 } 943 }
946 944
947 /* set the source address */ 945 /* set the source address */
@@ -963,7 +961,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
963 goto late_failure; 961 goto late_failure;
964 962
965 dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32, 963 dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32,
966 np->daddr.s6_addr32, 964 sk->sk_v6_daddr.s6_addr32,
967 inet->inet_sport, 965 inet->inet_sport,
968 inet->inet_dport); 966 inet->inet_dport);
969 err = dccp_connect(sk); 967 err = dccp_connect(sk);
diff --git a/net/dccp/ipv6.h b/net/dccp/ipv6.h
index 6eef81fdbe56..af259e15e7f0 100644
--- a/net/dccp/ipv6.h
+++ b/net/dccp/ipv6.h
@@ -25,12 +25,10 @@ struct dccp6_sock {
25 25
26struct dccp6_request_sock { 26struct dccp6_request_sock {
27 struct dccp_request_sock dccp; 27 struct dccp_request_sock dccp;
28 struct inet6_request_sock inet6;
29}; 28};
30 29
31struct dccp6_timewait_sock { 30struct dccp6_timewait_sock {
32 struct inet_timewait_sock inet; 31 struct inet_timewait_sock inet;
33 struct inet6_timewait_sock tw6;
34}; 32};
35 33
36#endif /* _DCCP_IPV6_H */ 34#endif /* _DCCP_IPV6_H */
diff --git a/net/dccp/minisocks.c b/net/dccp/minisocks.c
index 662071b249cc..9e2f78bc1553 100644
--- a/net/dccp/minisocks.c
+++ b/net/dccp/minisocks.c
@@ -56,12 +56,9 @@ void dccp_time_wait(struct sock *sk, int state, int timeo)
56#if IS_ENABLED(CONFIG_IPV6) 56#if IS_ENABLED(CONFIG_IPV6)
57 if (tw->tw_family == PF_INET6) { 57 if (tw->tw_family == PF_INET6) {
58 const struct ipv6_pinfo *np = inet6_sk(sk); 58 const struct ipv6_pinfo *np = inet6_sk(sk);
59 struct inet6_timewait_sock *tw6;
60 59
61 tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot); 60 tw->tw_v6_daddr = sk->sk_v6_daddr;
62 tw6 = inet6_twsk((struct sock *)tw); 61 tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
63 tw6->tw_v6_daddr = np->daddr;
64 tw6->tw_v6_rcv_saddr = np->rcv_saddr;
65 tw->tw_ipv6only = np->ipv6only; 62 tw->tw_ipv6only = np->ipv6only;
66 } 63 }
67#endif 64#endif
@@ -269,10 +266,10 @@ int dccp_reqsk_init(struct request_sock *req,
269{ 266{
270 struct dccp_request_sock *dreq = dccp_rsk(req); 267 struct dccp_request_sock *dreq = dccp_rsk(req);
271 268
272 inet_rsk(req)->rmt_port = dccp_hdr(skb)->dccph_sport; 269 inet_rsk(req)->ir_rmt_port = dccp_hdr(skb)->dccph_sport;
273 inet_rsk(req)->loc_port = dccp_hdr(skb)->dccph_dport; 270 inet_rsk(req)->ir_num = ntohs(dccp_hdr(skb)->dccph_dport);
274 inet_rsk(req)->acked = 0; 271 inet_rsk(req)->acked = 0;
275 dreq->dreq_timestamp_echo = 0; 272 dreq->dreq_timestamp_echo = 0;
276 273
277 /* inherit feature negotiation options from listening socket */ 274 /* inherit feature negotiation options from listening socket */
278 return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg); 275 return dccp_feat_clone_list(&dp->dccps_featneg, &dreq->dreq_featneg);
diff --git a/net/dccp/output.c b/net/dccp/output.c
index d17fc90a74b6..8876078859da 100644
--- a/net/dccp/output.c
+++ b/net/dccp/output.c
@@ -424,8 +424,8 @@ struct sk_buff *dccp_make_response(struct sock *sk, struct dst_entry *dst,
424 /* Build and checksum header */ 424 /* Build and checksum header */
425 dh = dccp_zeroed_hdr(skb, dccp_header_size); 425 dh = dccp_zeroed_hdr(skb, dccp_header_size);
426 426
427 dh->dccph_sport = inet_rsk(req)->loc_port; 427 dh->dccph_sport = htons(inet_rsk(req)->ir_num);
428 dh->dccph_dport = inet_rsk(req)->rmt_port; 428 dh->dccph_dport = inet_rsk(req)->ir_rmt_port;
429 dh->dccph_doff = (dccp_header_size + 429 dh->dccph_doff = (dccp_header_size +
430 DCCP_SKB_CB(skb)->dccpd_opt_len) / 4; 430 DCCP_SKB_CB(skb)->dccpd_opt_len) / 4;
431 dh->dccph_type = DCCP_PKT_RESPONSE; 431 dh->dccph_type = DCCP_PKT_RESPONSE;
diff --git a/net/dccp/probe.c b/net/dccp/probe.c
index 4c6bdf97a657..595ddf0459db 100644
--- a/net/dccp/probe.c
+++ b/net/dccp/probe.c
@@ -152,17 +152,6 @@ static const struct file_operations dccpprobe_fops = {
152 .llseek = noop_llseek, 152 .llseek = noop_llseek,
153}; 153};
154 154
155static __init int setup_jprobe(void)
156{
157 int ret = register_jprobe(&dccp_send_probe);
158
159 if (ret) {
160 request_module("dccp");
161 ret = register_jprobe(&dccp_send_probe);
162 }
163 return ret;
164}
165
166static __init int dccpprobe_init(void) 155static __init int dccpprobe_init(void)
167{ 156{
168 int ret = -ENOMEM; 157 int ret = -ENOMEM;
@@ -174,7 +163,13 @@ static __init int dccpprobe_init(void)
174 if (!proc_create(procname, S_IRUSR, init_net.proc_net, &dccpprobe_fops)) 163 if (!proc_create(procname, S_IRUSR, init_net.proc_net, &dccpprobe_fops))
175 goto err0; 164 goto err0;
176 165
177 ret = setup_jprobe(); 166 ret = register_jprobe(&dccp_send_probe);
167 if (ret) {
168 ret = request_module("dccp");
169 if (!ret)
170 ret = register_jprobe(&dccp_send_probe);
171 }
172
178 if (ret) 173 if (ret)
179 goto err1; 174 goto err1;
180 175
diff --git a/net/dccp/proto.c b/net/dccp/proto.c
index ba64750f0387..eb892b4f4814 100644
--- a/net/dccp/proto.c
+++ b/net/dccp/proto.c
@@ -1158,10 +1158,8 @@ static int __init dccp_init(void)
1158 goto out_free_bind_bucket_cachep; 1158 goto out_free_bind_bucket_cachep;
1159 } 1159 }
1160 1160
1161 for (i = 0; i <= dccp_hashinfo.ehash_mask; i++) { 1161 for (i = 0; i <= dccp_hashinfo.ehash_mask; i++)
1162 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i); 1162 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].chain, i);
1163 INIT_HLIST_NULLS_HEAD(&dccp_hashinfo.ehash[i].twchain, i);
1164 }
1165 1163
1166 if (inet_ehash_locks_alloc(&dccp_hashinfo)) 1164 if (inet_ehash_locks_alloc(&dccp_hashinfo))
1167 goto out_free_dccp_ehash; 1165 goto out_free_dccp_ehash;
diff --git a/net/decnet/netfilter/dn_rtmsg.c b/net/decnet/netfilter/dn_rtmsg.c
index 2a7efe388344..e83015cecfa7 100644
--- a/net/decnet/netfilter/dn_rtmsg.c
+++ b/net/decnet/netfilter/dn_rtmsg.c
@@ -87,7 +87,7 @@ static void dnrmg_send_peer(struct sk_buff *skb)
87} 87}
88 88
89 89
90static unsigned int dnrmg_hook(unsigned int hook, 90static unsigned int dnrmg_hook(const struct nf_hook_ops *ops,
91 struct sk_buff *skb, 91 struct sk_buff *skb,
92 const struct net_device *in, 92 const struct net_device *in,
93 const struct net_device *out, 93 const struct net_device *out,
diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
index be1f64d35358..8f032bae60ad 100644
--- a/net/ethernet/eth.c
+++ b/net/ethernet/eth.c
@@ -58,7 +58,7 @@
58#include <net/ipv6.h> 58#include <net/ipv6.h>
59#include <net/ip.h> 59#include <net/ip.h>
60#include <net/dsa.h> 60#include <net/dsa.h>
61#include <asm/uaccess.h> 61#include <linux/uaccess.h>
62 62
63__setup("ether=", netdev_boot_setup); 63__setup("ether=", netdev_boot_setup);
64 64
@@ -133,7 +133,7 @@ int eth_rebuild_header(struct sk_buff *skb)
133 return arp_find(eth->h_dest, skb); 133 return arp_find(eth->h_dest, skb);
134#endif 134#endif
135 default: 135 default:
136 printk(KERN_DEBUG 136 netdev_dbg(dev,
137 "%s: unable to resolve type %X addresses.\n", 137 "%s: unable to resolve type %X addresses.\n",
138 dev->name, ntohs(eth->h_proto)); 138 dev->name, ntohs(eth->h_proto));
139 139
@@ -169,20 +169,9 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
169 else 169 else
170 skb->pkt_type = PACKET_MULTICAST; 170 skb->pkt_type = PACKET_MULTICAST;
171 } 171 }
172 172 else if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
173 /* 173 dev->dev_addr)))
174 * This ALLMULTI check should be redundant by 1.4 174 skb->pkt_type = PACKET_OTHERHOST;
175 * so don't forget to remove it.
176 *
177 * Seems, you forgot to remove it. All silly devices
178 * seems to set IFF_PROMISC.
179 */
180
181 else if (1 /*dev->flags&IFF_PROMISC */ ) {
182 if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
183 dev->dev_addr)))
184 skb->pkt_type = PACKET_OTHERHOST;
185 }
186 175
187 /* 176 /*
188 * Some variants of DSA tagging don't have an ethertype field 177 * Some variants of DSA tagging don't have an ethertype field
@@ -190,12 +179,13 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
190 * variants has been configured on the receiving interface, 179 * variants has been configured on the receiving interface,
191 * and if so, set skb->protocol without looking at the packet. 180 * and if so, set skb->protocol without looking at the packet.
192 */ 181 */
193 if (netdev_uses_dsa_tags(dev)) 182 if (unlikely(netdev_uses_dsa_tags(dev)))
194 return htons(ETH_P_DSA); 183 return htons(ETH_P_DSA);
195 if (netdev_uses_trailer_tags(dev)) 184
185 if (unlikely(netdev_uses_trailer_tags(dev)))
196 return htons(ETH_P_TRAILER); 186 return htons(ETH_P_TRAILER);
197 187
198 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) 188 if (likely(ntohs(eth->h_proto) >= ETH_P_802_3_MIN))
199 return eth->h_proto; 189 return eth->h_proto;
200 190
201 /* 191 /*
@@ -204,7 +194,7 @@ __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
204 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This 194 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
205 * won't work for fault tolerant netware but does for the rest. 195 * won't work for fault tolerant netware but does for the rest.
206 */ 196 */
207 if (skb->len >= 2 && *(unsigned short *)(skb->data) == 0xFFFF) 197 if (unlikely(skb->len >= 2 && *(unsigned short *)(skb->data) == 0xFFFF))
208 return htons(ETH_P_802_3); 198 return htons(ETH_P_802_3);
209 199
210 /* 200 /*
diff --git a/net/hsr/Kconfig b/net/hsr/Kconfig
new file mode 100644
index 000000000000..0d3d709052ca
--- /dev/null
+++ b/net/hsr/Kconfig
@@ -0,0 +1,27 @@
1#
2# IEC 62439-3 High-availability Seamless Redundancy
3#
4
5config HSR
6 tristate "High-availability Seamless Redundancy (HSR)"
7 ---help---
8 If you say Y here, then your Linux box will be able to act as a
9 DANH ("Doubly attached node implementing HSR"). For this to work,
10 your Linux box needs (at least) two physical Ethernet interfaces,
11 and it must be connected as a node in a ring network together with
12 other HSR capable nodes.
13
14 All Ethernet frames sent over the hsr device will be sent in both
15 directions on the ring (over both slave ports), giving a redundant,
16 instant fail-over network. Each HSR node in the ring acts like a
17 bridge for HSR frames, but filters frames that have been forwarded
18 earlier.
19
20 This code is a "best effort" to comply with the HSR standard as
21 described in IEC 62439-3:2010 (HSRv0), but no compliancy tests have
22 been made.
23
24 You need to perform any and all necessary tests yourself before
25 relying on this code in a safety critical system!
26
27 If unsure, say N.
diff --git a/net/hsr/Makefile b/net/hsr/Makefile
new file mode 100644
index 000000000000..b68359f181cc
--- /dev/null
+++ b/net/hsr/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for HSR
3#
4
5obj-$(CONFIG_HSR) += hsr.o
6
7hsr-y := hsr_main.o hsr_framereg.o hsr_device.o hsr_netlink.o
diff --git a/net/hsr/hsr_device.c b/net/hsr/hsr_device.c
new file mode 100644
index 000000000000..cac505f166d5
--- /dev/null
+++ b/net/hsr/hsr_device.c
@@ -0,0 +1,596 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 *
11 * This file contains device methods for creating, using and destroying
12 * virtual HSR devices.
13 */
14
15#include <linux/netdevice.h>
16#include <linux/skbuff.h>
17#include <linux/etherdevice.h>
18#include <linux/if_arp.h>
19#include <linux/rtnetlink.h>
20#include <linux/pkt_sched.h>
21#include "hsr_device.h"
22#include "hsr_framereg.h"
23#include "hsr_main.h"
24
25
26static bool is_admin_up(struct net_device *dev)
27{
28 return dev && (dev->flags & IFF_UP);
29}
30
31static bool is_slave_up(struct net_device *dev)
32{
33 return dev && is_admin_up(dev) && netif_oper_up(dev);
34}
35
36static void __hsr_set_operstate(struct net_device *dev, int transition)
37{
38 write_lock_bh(&dev_base_lock);
39 if (dev->operstate != transition) {
40 dev->operstate = transition;
41 write_unlock_bh(&dev_base_lock);
42 netdev_state_change(dev);
43 } else {
44 write_unlock_bh(&dev_base_lock);
45 }
46}
47
48void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1,
49 struct net_device *slave2)
50{
51 if (!is_admin_up(hsr_dev)) {
52 __hsr_set_operstate(hsr_dev, IF_OPER_DOWN);
53 return;
54 }
55
56 if (is_slave_up(slave1) || is_slave_up(slave2))
57 __hsr_set_operstate(hsr_dev, IF_OPER_UP);
58 else
59 __hsr_set_operstate(hsr_dev, IF_OPER_LOWERLAYERDOWN);
60}
61
62void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1,
63 struct net_device *slave2)
64{
65 if (is_slave_up(slave1) || is_slave_up(slave2))
66 netif_carrier_on(hsr_dev);
67 else
68 netif_carrier_off(hsr_dev);
69}
70
71
72void hsr_check_announce(struct net_device *hsr_dev, int old_operstate)
73{
74 struct hsr_priv *hsr_priv;
75
76 hsr_priv = netdev_priv(hsr_dev);
77
78 if ((hsr_dev->operstate == IF_OPER_UP) && (old_operstate != IF_OPER_UP)) {
79 /* Went up */
80 hsr_priv->announce_count = 0;
81 hsr_priv->announce_timer.expires = jiffies +
82 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
83 add_timer(&hsr_priv->announce_timer);
84 }
85
86 if ((hsr_dev->operstate != IF_OPER_UP) && (old_operstate == IF_OPER_UP))
87 /* Went down */
88 del_timer(&hsr_priv->announce_timer);
89}
90
91
92int hsr_get_max_mtu(struct hsr_priv *hsr_priv)
93{
94 int mtu_max;
95
96 if (hsr_priv->slave[0] && hsr_priv->slave[1])
97 mtu_max = min(hsr_priv->slave[0]->mtu, hsr_priv->slave[1]->mtu);
98 else if (hsr_priv->slave[0])
99 mtu_max = hsr_priv->slave[0]->mtu;
100 else if (hsr_priv->slave[1])
101 mtu_max = hsr_priv->slave[1]->mtu;
102 else
103 mtu_max = HSR_TAGLEN;
104
105 return mtu_max - HSR_TAGLEN;
106}
107
108static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
109{
110 struct hsr_priv *hsr_priv;
111
112 hsr_priv = netdev_priv(dev);
113
114 if (new_mtu > hsr_get_max_mtu(hsr_priv)) {
115 netdev_info(hsr_priv->dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
116 HSR_TAGLEN);
117 return -EINVAL;
118 }
119
120 dev->mtu = new_mtu;
121
122 return 0;
123}
124
125static int hsr_dev_open(struct net_device *dev)
126{
127 struct hsr_priv *hsr_priv;
128 int i;
129 char *slave_name;
130
131 hsr_priv = netdev_priv(dev);
132
133 for (i = 0; i < HSR_MAX_SLAVE; i++) {
134 if (hsr_priv->slave[i])
135 slave_name = hsr_priv->slave[i]->name;
136 else
137 slave_name = "null";
138
139 if (!is_slave_up(hsr_priv->slave[i]))
140 netdev_warn(dev, "Slave %c (%s) is not up; please bring it up to get a working HSR network\n",
141 'A' + i, slave_name);
142 }
143
144 return 0;
145}
146
147static int hsr_dev_close(struct net_device *dev)
148{
149 /* Nothing to do here. We could try to restore the state of the slaves
150 * to what they were before being changed by the hsr master dev's state,
151 * but they might have been changed manually in the mean time too, so
152 * taking them up or down here might be confusing and is probably not a
153 * good idea.
154 */
155 return 0;
156}
157
158
159static void hsr_fill_tag(struct hsr_ethhdr *hsr_ethhdr, struct hsr_priv *hsr_priv)
160{
161 unsigned long irqflags;
162
163 /* IEC 62439-1:2010, p 48, says the 4-bit "path" field can take values
164 * between 0001-1001 ("ring identifier", for regular HSR frames),
165 * or 1111 ("HSR management", supervision frames). Unfortunately, the
166 * spec writers forgot to explain what a "ring identifier" is, or
167 * how it is used. So we just set this to 0001 for regular frames,
168 * and 1111 for supervision frames.
169 */
170 set_hsr_tag_path(&hsr_ethhdr->hsr_tag, 0x1);
171
172 /* IEC 62439-1:2010, p 12: "The link service data unit in an Ethernet
173 * frame is the content of the frame located between the Length/Type
174 * field and the Frame Check Sequence."
175 *
176 * IEC 62439-3, p 48, specifies the "original LPDU" to include the
177 * original "LT" field (what "LT" means is not explained anywhere as
178 * far as I can see - perhaps "Length/Type"?). So LSDU_size might
179 * equal original length + 2.
180 * Also, the fact that this field is not used anywhere (might be used
181 * by a RedBox connecting HSR and PRP nets?) means I cannot test its
182 * correctness. Instead of guessing, I set this to 0 here, to make any
183 * problems immediately apparent. Anyone using this driver with PRP/HSR
184 * RedBoxes might need to fix this...
185 */
186 set_hsr_tag_LSDU_size(&hsr_ethhdr->hsr_tag, 0);
187
188 spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags);
189 hsr_ethhdr->hsr_tag.sequence_nr = htons(hsr_priv->sequence_nr);
190 hsr_priv->sequence_nr++;
191 spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags);
192
193 hsr_ethhdr->hsr_tag.encap_proto = hsr_ethhdr->ethhdr.h_proto;
194
195 hsr_ethhdr->ethhdr.h_proto = htons(ETH_P_PRP);
196}
197
198static int slave_xmit(struct sk_buff *skb, struct hsr_priv *hsr_priv,
199 enum hsr_dev_idx dev_idx)
200{
201 struct hsr_ethhdr *hsr_ethhdr;
202
203 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
204
205 skb->dev = hsr_priv->slave[dev_idx];
206
207 hsr_addr_subst_dest(hsr_priv, &hsr_ethhdr->ethhdr, dev_idx);
208
209 /* Address substitution (IEC62439-3 pp 26, 50): replace mac
210 * address of outgoing frame with that of the outgoing slave's.
211 */
212 memcpy(hsr_ethhdr->ethhdr.h_source, skb->dev->dev_addr, ETH_ALEN);
213
214 return dev_queue_xmit(skb);
215}
216
217
218static int hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
219{
220 struct hsr_priv *hsr_priv;
221 struct hsr_ethhdr *hsr_ethhdr;
222 struct sk_buff *skb2;
223 int res1, res2;
224
225 hsr_priv = netdev_priv(dev);
226 hsr_ethhdr = (struct hsr_ethhdr *) skb->data;
227
228 if ((skb->protocol != htons(ETH_P_PRP)) ||
229 (hsr_ethhdr->ethhdr.h_proto != htons(ETH_P_PRP))) {
230 hsr_fill_tag(hsr_ethhdr, hsr_priv);
231 skb->protocol = htons(ETH_P_PRP);
232 }
233
234 skb2 = pskb_copy(skb, GFP_ATOMIC);
235
236 res1 = NET_XMIT_DROP;
237 if (likely(hsr_priv->slave[HSR_DEV_SLAVE_A]))
238 res1 = slave_xmit(skb, hsr_priv, HSR_DEV_SLAVE_A);
239
240 res2 = NET_XMIT_DROP;
241 if (likely(skb2 && hsr_priv->slave[HSR_DEV_SLAVE_B]))
242 res2 = slave_xmit(skb2, hsr_priv, HSR_DEV_SLAVE_B);
243
244 if (likely(res1 == NET_XMIT_SUCCESS || res1 == NET_XMIT_CN ||
245 res2 == NET_XMIT_SUCCESS || res2 == NET_XMIT_CN)) {
246 hsr_priv->dev->stats.tx_packets++;
247 hsr_priv->dev->stats.tx_bytes += skb->len;
248 } else {
249 hsr_priv->dev->stats.tx_dropped++;
250 }
251
252 return NETDEV_TX_OK;
253}
254
255
256static int hsr_header_create(struct sk_buff *skb, struct net_device *dev,
257 unsigned short type, const void *daddr,
258 const void *saddr, unsigned int len)
259{
260 int res;
261
262 /* Make room for the HSR tag now. We will fill it in later (in
263 * hsr_dev_xmit)
264 */
265 if (skb_headroom(skb) < HSR_TAGLEN + ETH_HLEN)
266 return -ENOBUFS;
267 skb_push(skb, HSR_TAGLEN);
268
269 /* To allow VLAN/HSR combos we should probably use
270 * res = dev_hard_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN);
271 * here instead. It would require other changes too, though - e.g.
272 * separate headers for each slave etc...
273 */
274 res = eth_header(skb, dev, type, daddr, saddr, len + HSR_TAGLEN);
275 if (res <= 0)
276 return res;
277 skb_reset_mac_header(skb);
278
279 return res + HSR_TAGLEN;
280}
281
282
283static const struct header_ops hsr_header_ops = {
284 .create = hsr_header_create,
285 .parse = eth_header_parse,
286};
287
288
289/* HSR:2010 supervision frames should be padded so that the whole frame,
290 * including headers and FCS, is 64 bytes (without VLAN).
291 */
292static int hsr_pad(int size)
293{
294 const int min_size = ETH_ZLEN - HSR_TAGLEN - ETH_HLEN;
295
296 if (size >= min_size)
297 return size;
298 return min_size;
299}
300
301static void send_hsr_supervision_frame(struct net_device *hsr_dev, u8 type)
302{
303 struct hsr_priv *hsr_priv;
304 struct sk_buff *skb;
305 int hlen, tlen;
306 struct hsr_sup_tag *hsr_stag;
307 struct hsr_sup_payload *hsr_sp;
308 unsigned long irqflags;
309
310 hlen = LL_RESERVED_SPACE(hsr_dev);
311 tlen = hsr_dev->needed_tailroom;
312 skb = alloc_skb(hsr_pad(sizeof(struct hsr_sup_payload)) + hlen + tlen,
313 GFP_ATOMIC);
314
315 if (skb == NULL)
316 return;
317
318 hsr_priv = netdev_priv(hsr_dev);
319
320 skb_reserve(skb, hlen);
321
322 skb->dev = hsr_dev;
323 skb->protocol = htons(ETH_P_PRP);
324 skb->priority = TC_PRIO_CONTROL;
325
326 if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
327 hsr_priv->sup_multicast_addr,
328 skb->dev->dev_addr, skb->len) < 0)
329 goto out;
330
331 skb_pull(skb, sizeof(struct ethhdr));
332 hsr_stag = (typeof(hsr_stag)) skb->data;
333
334 set_hsr_stag_path(hsr_stag, 0xf);
335 set_hsr_stag_HSR_Ver(hsr_stag, 0);
336
337 spin_lock_irqsave(&hsr_priv->seqnr_lock, irqflags);
338 hsr_stag->sequence_nr = htons(hsr_priv->sequence_nr);
339 hsr_priv->sequence_nr++;
340 spin_unlock_irqrestore(&hsr_priv->seqnr_lock, irqflags);
341
342 hsr_stag->HSR_TLV_Type = type;
343 hsr_stag->HSR_TLV_Length = 12;
344
345 skb_push(skb, sizeof(struct ethhdr));
346
347 /* Payload: MacAddressA */
348 hsr_sp = (typeof(hsr_sp)) skb_put(skb, sizeof(*hsr_sp));
349 memcpy(hsr_sp->MacAddressA, hsr_dev->dev_addr, ETH_ALEN);
350
351 dev_queue_xmit(skb);
352 return;
353
354out:
355 kfree_skb(skb);
356}
357
358
359/* Announce (supervision frame) timer function
360 */
361static void hsr_announce(unsigned long data)
362{
363 struct hsr_priv *hsr_priv;
364
365 hsr_priv = (struct hsr_priv *) data;
366
367 if (hsr_priv->announce_count < 3) {
368 send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_ANNOUNCE);
369 hsr_priv->announce_count++;
370 } else {
371 send_hsr_supervision_frame(hsr_priv->dev, HSR_TLV_LIFE_CHECK);
372 }
373
374 if (hsr_priv->announce_count < 3)
375 hsr_priv->announce_timer.expires = jiffies +
376 msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
377 else
378 hsr_priv->announce_timer.expires = jiffies +
379 msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
380
381 if (is_admin_up(hsr_priv->dev))
382 add_timer(&hsr_priv->announce_timer);
383}
384
385
386static void restore_slaves(struct net_device *hsr_dev)
387{
388 struct hsr_priv *hsr_priv;
389 int i;
390 int res;
391
392 hsr_priv = netdev_priv(hsr_dev);
393
394 rtnl_lock();
395
396 /* Restore promiscuity */
397 for (i = 0; i < HSR_MAX_SLAVE; i++) {
398 if (!hsr_priv->slave[i])
399 continue;
400 res = dev_set_promiscuity(hsr_priv->slave[i], -1);
401 if (res)
402 netdev_info(hsr_dev,
403 "Cannot restore slave promiscuity (%s, %d)\n",
404 hsr_priv->slave[i]->name, res);
405 }
406
407 rtnl_unlock();
408}
409
410static void reclaim_hsr_dev(struct rcu_head *rh)
411{
412 struct hsr_priv *hsr_priv;
413
414 hsr_priv = container_of(rh, struct hsr_priv, rcu_head);
415 free_netdev(hsr_priv->dev);
416}
417
418
419/* According to comments in the declaration of struct net_device, this function
420 * is "Called from unregister, can be used to call free_netdev". Ok then...
421 */
422static void hsr_dev_destroy(struct net_device *hsr_dev)
423{
424 struct hsr_priv *hsr_priv;
425
426 hsr_priv = netdev_priv(hsr_dev);
427
428 del_timer(&hsr_priv->announce_timer);
429 unregister_hsr_master(hsr_priv); /* calls list_del_rcu on hsr_priv */
430 restore_slaves(hsr_dev);
431 call_rcu(&hsr_priv->rcu_head, reclaim_hsr_dev); /* reclaim hsr_priv */
432}
433
434static const struct net_device_ops hsr_device_ops = {
435 .ndo_change_mtu = hsr_dev_change_mtu,
436 .ndo_open = hsr_dev_open,
437 .ndo_stop = hsr_dev_close,
438 .ndo_start_xmit = hsr_dev_xmit,
439};
440
441
442void hsr_dev_setup(struct net_device *dev)
443{
444 random_ether_addr(dev->dev_addr);
445
446 ether_setup(dev);
447 dev->header_ops = &hsr_header_ops;
448 dev->netdev_ops = &hsr_device_ops;
449 dev->tx_queue_len = 0;
450
451 dev->destructor = hsr_dev_destroy;
452}
453
454
455/* Return true if dev is a HSR master; return false otherwise.
456 */
457bool is_hsr_master(struct net_device *dev)
458{
459 return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
460}
461
462static int check_slave_ok(struct net_device *dev)
463{
464 /* Don't allow HSR on non-ethernet like devices */
465 if ((dev->flags & IFF_LOOPBACK) || (dev->type != ARPHRD_ETHER) ||
466 (dev->addr_len != ETH_ALEN)) {
467 netdev_info(dev, "Cannot use loopback or non-ethernet device as HSR slave.\n");
468 return -EINVAL;
469 }
470
471 /* Don't allow enslaving hsr devices */
472 if (is_hsr_master(dev)) {
473 netdev_info(dev, "Cannot create trees of HSR devices.\n");
474 return -EINVAL;
475 }
476
477 if (is_hsr_slave(dev)) {
478 netdev_info(dev, "This device is already a HSR slave.\n");
479 return -EINVAL;
480 }
481
482 if (dev->priv_flags & IFF_802_1Q_VLAN) {
483 netdev_info(dev, "HSR on top of VLAN is not yet supported in this driver.\n");
484 return -EINVAL;
485 }
486
487 /* HSR over bonded devices has not been tested, but I'm not sure it
488 * won't work...
489 */
490
491 return 0;
492}
493
494
495/* Default multicast address for HSR Supervision frames */
496static const unsigned char def_multicast_addr[ETH_ALEN] = {
497 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
498};
499
500int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
501 unsigned char multicast_spec)
502{
503 struct hsr_priv *hsr_priv;
504 int i;
505 int res;
506
507 hsr_priv = netdev_priv(hsr_dev);
508 hsr_priv->dev = hsr_dev;
509 INIT_LIST_HEAD(&hsr_priv->node_db);
510 INIT_LIST_HEAD(&hsr_priv->self_node_db);
511 for (i = 0; i < HSR_MAX_SLAVE; i++)
512 hsr_priv->slave[i] = slave[i];
513
514 spin_lock_init(&hsr_priv->seqnr_lock);
515 /* Overflow soon to find bugs easier: */
516 hsr_priv->sequence_nr = USHRT_MAX - 1024;
517
518 init_timer(&hsr_priv->announce_timer);
519 hsr_priv->announce_timer.function = hsr_announce;
520 hsr_priv->announce_timer.data = (unsigned long) hsr_priv;
521
522 memcpy(hsr_priv->sup_multicast_addr, def_multicast_addr, ETH_ALEN);
523 hsr_priv->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
524
525/* FIXME: should I modify the value of these?
526 *
527 * - hsr_dev->flags - i.e.
528 * IFF_MASTER/SLAVE?
529 * - hsr_dev->priv_flags - i.e.
530 * IFF_EBRIDGE?
531 * IFF_TX_SKB_SHARING?
532 * IFF_HSR_MASTER/SLAVE?
533 */
534
535 for (i = 0; i < HSR_MAX_SLAVE; i++) {
536 res = check_slave_ok(slave[i]);
537 if (res)
538 return res;
539 }
540
541 hsr_dev->features = slave[0]->features & slave[1]->features;
542 /* Prevent recursive tx locking */
543 hsr_dev->features |= NETIF_F_LLTX;
544 /* VLAN on top of HSR needs testing and probably some work on
545 * hsr_header_create() etc.
546 */
547 hsr_dev->features |= NETIF_F_VLAN_CHALLENGED;
548
549 /* Set hsr_dev's MAC address to that of mac_slave1 */
550 memcpy(hsr_dev->dev_addr, hsr_priv->slave[0]->dev_addr, ETH_ALEN);
551
552 /* Set required header length */
553 for (i = 0; i < HSR_MAX_SLAVE; i++) {
554 if (slave[i]->hard_header_len + HSR_TAGLEN >
555 hsr_dev->hard_header_len)
556 hsr_dev->hard_header_len =
557 slave[i]->hard_header_len + HSR_TAGLEN;
558 }
559
560 /* MTU */
561 for (i = 0; i < HSR_MAX_SLAVE; i++)
562 if (slave[i]->mtu - HSR_TAGLEN < hsr_dev->mtu)
563 hsr_dev->mtu = slave[i]->mtu - HSR_TAGLEN;
564
565 /* Make sure the 1st call to netif_carrier_on() gets through */
566 netif_carrier_off(hsr_dev);
567
568 /* Promiscuity */
569 for (i = 0; i < HSR_MAX_SLAVE; i++) {
570 res = dev_set_promiscuity(slave[i], 1);
571 if (res) {
572 netdev_info(hsr_dev, "Cannot set slave promiscuity (%s, %d)\n",
573 slave[i]->name, res);
574 goto fail;
575 }
576 }
577
578 /* Make sure we recognize frames from ourselves in hsr_rcv() */
579 res = hsr_create_self_node(&hsr_priv->self_node_db,
580 hsr_dev->dev_addr,
581 hsr_priv->slave[1]->dev_addr);
582 if (res < 0)
583 goto fail;
584
585 res = register_netdevice(hsr_dev);
586 if (res)
587 goto fail;
588
589 register_hsr_master(hsr_priv);
590
591 return 0;
592
593fail:
594 restore_slaves(hsr_dev);
595 return res;
596}
diff --git a/net/hsr/hsr_device.h b/net/hsr/hsr_device.h
new file mode 100644
index 000000000000..2c7148e73914
--- /dev/null
+++ b/net/hsr/hsr_device.h
@@ -0,0 +1,29 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 */
11
12#ifndef __HSR_DEVICE_H
13#define __HSR_DEVICE_H
14
15#include <linux/netdevice.h>
16#include "hsr_main.h"
17
18void hsr_dev_setup(struct net_device *dev);
19int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
20 unsigned char multicast_spec);
21void hsr_set_operstate(struct net_device *hsr_dev, struct net_device *slave1,
22 struct net_device *slave2);
23void hsr_set_carrier(struct net_device *hsr_dev, struct net_device *slave1,
24 struct net_device *slave2);
25void hsr_check_announce(struct net_device *hsr_dev, int old_operstate);
26bool is_hsr_master(struct net_device *dev);
27int hsr_get_max_mtu(struct hsr_priv *hsr_priv);
28
29#endif /* __HSR_DEVICE_H */
diff --git a/net/hsr/hsr_framereg.c b/net/hsr/hsr_framereg.c
new file mode 100644
index 000000000000..4bdab1521878
--- /dev/null
+++ b/net/hsr/hsr_framereg.c
@@ -0,0 +1,504 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 *
11 * The HSR spec says never to forward the same frame twice on the same
12 * interface. A frame is identified by its source MAC address and its HSR
13 * sequence number. This code keeps track of senders and their sequence numbers
14 * to allow filtering of duplicate frames, and to detect HSR ring errors.
15 */
16
17#include <linux/if_ether.h>
18#include <linux/etherdevice.h>
19#include <linux/slab.h>
20#include <linux/rculist.h>
21#include "hsr_main.h"
22#include "hsr_framereg.h"
23#include "hsr_netlink.h"
24
25
26struct node_entry {
27 struct list_head mac_list;
28 unsigned char MacAddressA[ETH_ALEN];
29 unsigned char MacAddressB[ETH_ALEN];
30 enum hsr_dev_idx AddrB_if; /* The local slave through which AddrB
31 * frames are received from this node
32 */
33 unsigned long time_in[HSR_MAX_SLAVE];
34 bool time_in_stale[HSR_MAX_SLAVE];
35 u16 seq_out[HSR_MAX_DEV];
36 struct rcu_head rcu_head;
37};
38
39/* TODO: use hash lists for mac addresses (linux/jhash.h)? */
40
41
42
43/* Search for mac entry. Caller must hold rcu read lock.
44 */
45static struct node_entry *find_node_by_AddrA(struct list_head *node_db,
46 const unsigned char addr[ETH_ALEN])
47{
48 struct node_entry *node;
49
50 list_for_each_entry_rcu(node, node_db, mac_list) {
51 if (ether_addr_equal(node->MacAddressA, addr))
52 return node;
53 }
54
55 return NULL;
56}
57
58
59/* Search for mac entry. Caller must hold rcu read lock.
60 */
61static struct node_entry *find_node_by_AddrB(struct list_head *node_db,
62 const unsigned char addr[ETH_ALEN])
63{
64 struct node_entry *node;
65
66 list_for_each_entry_rcu(node, node_db, mac_list) {
67 if (ether_addr_equal(node->MacAddressB, addr))
68 return node;
69 }
70
71 return NULL;
72}
73
74
75/* Search for mac entry. Caller must hold rcu read lock.
76 */
77struct node_entry *hsr_find_node(struct list_head *node_db, struct sk_buff *skb)
78{
79 struct node_entry *node;
80 struct ethhdr *ethhdr;
81
82 if (!skb_mac_header_was_set(skb))
83 return NULL;
84
85 ethhdr = (struct ethhdr *) skb_mac_header(skb);
86
87 list_for_each_entry_rcu(node, node_db, mac_list) {
88 if (ether_addr_equal(node->MacAddressA, ethhdr->h_source))
89 return node;
90 if (ether_addr_equal(node->MacAddressB, ethhdr->h_source))
91 return node;
92 }
93
94 return NULL;
95}
96
97
98/* Helper for device init; the self_node_db is used in hsr_rcv() to recognize
99 * frames from self that's been looped over the HSR ring.
100 */
101int hsr_create_self_node(struct list_head *self_node_db,
102 unsigned char addr_a[ETH_ALEN],
103 unsigned char addr_b[ETH_ALEN])
104{
105 struct node_entry *node, *oldnode;
106
107 node = kmalloc(sizeof(*node), GFP_KERNEL);
108 if (!node)
109 return -ENOMEM;
110
111 memcpy(node->MacAddressA, addr_a, ETH_ALEN);
112 memcpy(node->MacAddressB, addr_b, ETH_ALEN);
113
114 rcu_read_lock();
115 oldnode = list_first_or_null_rcu(self_node_db,
116 struct node_entry, mac_list);
117 if (oldnode) {
118 list_replace_rcu(&oldnode->mac_list, &node->mac_list);
119 rcu_read_unlock();
120 synchronize_rcu();
121 kfree(oldnode);
122 } else {
123 rcu_read_unlock();
124 list_add_tail_rcu(&node->mac_list, self_node_db);
125 }
126
127 return 0;
128}
129
130static void node_entry_reclaim(struct rcu_head *rh)
131{
132 kfree(container_of(rh, struct node_entry, rcu_head));
133}
134
135
136/* Add/merge node to the database of nodes. 'skb' must contain an HSR
137 * supervision frame.
138 * - If the supervision header's MacAddressA field is not yet in the database,
139 * this frame is from an hitherto unknown node - add it to the database.
140 * - If the sender's MAC address is not the same as its MacAddressA address,
141 * the node is using PICS_SUBS (address substitution). Record the sender's
142 * address as the node's MacAddressB.
143 *
144 * This function needs to work even if the sender node has changed one of its
145 * slaves' MAC addresses. In this case, there are four different cases described
146 * by (Addr-changed, received-from) pairs as follows. Note that changing the
147 * SlaveA address is equal to changing the node's own address:
148 *
149 * - (AddrB, SlaveB): The new AddrB will be recorded by PICS_SUBS code since
150 * node == NULL.
151 * - (AddrB, SlaveA): Will work as usual (the AddrB change won't be detected
152 * from this frame).
153 *
154 * - (AddrA, SlaveB): The old node will be found. We need to detect this and
155 * remove the node.
156 * - (AddrA, SlaveA): A new node will be registered (non-PICS_SUBS at first).
157 * The old one will be pruned after HSR_NODE_FORGET_TIME.
158 *
159 * We also need to detect if the sender's SlaveA and SlaveB cables have been
160 * swapped.
161 */
162struct node_entry *hsr_merge_node(struct hsr_priv *hsr_priv,
163 struct node_entry *node,
164 struct sk_buff *skb,
165 enum hsr_dev_idx dev_idx)
166{
167 struct hsr_sup_payload *hsr_sp;
168 struct hsr_ethhdr_sp *hsr_ethsup;
169 int i;
170 unsigned long now;
171
172 hsr_ethsup = (struct hsr_ethhdr_sp *) skb_mac_header(skb);
173 hsr_sp = (struct hsr_sup_payload *) skb->data;
174
175 if (node && !ether_addr_equal(node->MacAddressA, hsr_sp->MacAddressA)) {
176 /* Node has changed its AddrA, frame was received from SlaveB */
177 list_del_rcu(&node->mac_list);
178 call_rcu(&node->rcu_head, node_entry_reclaim);
179 node = NULL;
180 }
181
182 if (node && (dev_idx == node->AddrB_if) &&
183 !ether_addr_equal(node->MacAddressB, hsr_ethsup->ethhdr.h_source)) {
184 /* Cables have been swapped */
185 list_del_rcu(&node->mac_list);
186 call_rcu(&node->rcu_head, node_entry_reclaim);
187 node = NULL;
188 }
189
190 if (node && (dev_idx != node->AddrB_if) &&
191 (node->AddrB_if != HSR_DEV_NONE) &&
192 !ether_addr_equal(node->MacAddressA, hsr_ethsup->ethhdr.h_source)) {
193 /* Cables have been swapped */
194 list_del_rcu(&node->mac_list);
195 call_rcu(&node->rcu_head, node_entry_reclaim);
196 node = NULL;
197 }
198
199 if (node)
200 return node;
201
202 node = find_node_by_AddrA(&hsr_priv->node_db, hsr_sp->MacAddressA);
203 if (node) {
204 /* Node is known, but frame was received from an unknown
205 * address. Node is PICS_SUBS capable; merge its AddrB.
206 */
207 memcpy(node->MacAddressB, hsr_ethsup->ethhdr.h_source, ETH_ALEN);
208 node->AddrB_if = dev_idx;
209 return node;
210 }
211
212 node = kzalloc(sizeof(*node), GFP_ATOMIC);
213 if (!node)
214 return NULL;
215
216 memcpy(node->MacAddressA, hsr_sp->MacAddressA, ETH_ALEN);
217 memcpy(node->MacAddressB, hsr_ethsup->ethhdr.h_source, ETH_ALEN);
218 if (!ether_addr_equal(hsr_sp->MacAddressA, hsr_ethsup->ethhdr.h_source))
219 node->AddrB_if = dev_idx;
220 else
221 node->AddrB_if = HSR_DEV_NONE;
222
223 /* We are only interested in time diffs here, so use current jiffies
224 * as initialization. (0 could trigger an spurious ring error warning).
225 */
226 now = jiffies;
227 for (i = 0; i < HSR_MAX_SLAVE; i++)
228 node->time_in[i] = now;
229 for (i = 0; i < HSR_MAX_DEV; i++)
230 node->seq_out[i] = ntohs(hsr_ethsup->hsr_sup.sequence_nr) - 1;
231
232 list_add_tail_rcu(&node->mac_list, &hsr_priv->node_db);
233
234 return node;
235}
236
237
238/* 'skb' is a frame meant for this host, that is to be passed to upper layers.
239 *
240 * If the frame was sent by a node's B interface, replace the sender
241 * address with that node's "official" address (MacAddressA) so that upper
242 * layers recognize where it came from.
243 */
244void hsr_addr_subst_source(struct hsr_priv *hsr_priv, struct sk_buff *skb)
245{
246 struct ethhdr *ethhdr;
247 struct node_entry *node;
248
249 if (!skb_mac_header_was_set(skb)) {
250 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
251 return;
252 }
253 ethhdr = (struct ethhdr *) skb_mac_header(skb);
254
255 rcu_read_lock();
256 node = find_node_by_AddrB(&hsr_priv->node_db, ethhdr->h_source);
257 if (node)
258 memcpy(ethhdr->h_source, node->MacAddressA, ETH_ALEN);
259 rcu_read_unlock();
260}
261
262
263/* 'skb' is a frame meant for another host.
264 * 'hsr_dev_idx' is the HSR index of the outgoing device
265 *
266 * Substitute the target (dest) MAC address if necessary, so the it matches the
267 * recipient interface MAC address, regardless of whether that is the
268 * recipient's A or B interface.
269 * This is needed to keep the packets flowing through switches that learn on
270 * which "side" the different interfaces are.
271 */
272void hsr_addr_subst_dest(struct hsr_priv *hsr_priv, struct ethhdr *ethhdr,
273 enum hsr_dev_idx dev_idx)
274{
275 struct node_entry *node;
276
277 rcu_read_lock();
278 node = find_node_by_AddrA(&hsr_priv->node_db, ethhdr->h_dest);
279 if (node && (node->AddrB_if == dev_idx))
280 memcpy(ethhdr->h_dest, node->MacAddressB, ETH_ALEN);
281 rcu_read_unlock();
282}
283
284
285/* seq_nr_after(a, b) - return true if a is after (higher in sequence than) b,
286 * false otherwise.
287 */
288static bool seq_nr_after(u16 a, u16 b)
289{
290 /* Remove inconsistency where
291 * seq_nr_after(a, b) == seq_nr_before(a, b)
292 */
293 if ((int) b - a == 32768)
294 return false;
295
296 return (((s16) (b - a)) < 0);
297}
298#define seq_nr_before(a, b) seq_nr_after((b), (a))
299#define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
300#define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
301
302
303void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx)
304{
305 if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
306 WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
307 return;
308 }
309 node->time_in[dev_idx] = jiffies;
310 node->time_in_stale[dev_idx] = false;
311}
312
313
314/* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
315 * ethhdr->h_source address and skb->mac_header set.
316 *
317 * Return:
318 * 1 if frame can be shown to have been sent recently on this interface,
319 * 0 otherwise, or
320 * negative error code on error
321 */
322int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx,
323 struct sk_buff *skb)
324{
325 struct hsr_ethhdr *hsr_ethhdr;
326 u16 sequence_nr;
327
328 if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
329 WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
330 return -EINVAL;
331 }
332 if (!skb_mac_header_was_set(skb)) {
333 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
334 return -EINVAL;
335 }
336 hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
337
338 sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
339 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[dev_idx]))
340 return 1;
341
342 node->seq_out[dev_idx] = sequence_nr;
343 return 0;
344}
345
346
347
348static bool is_late(struct node_entry *node, enum hsr_dev_idx dev_idx)
349{
350 enum hsr_dev_idx other;
351
352 if (node->time_in_stale[dev_idx])
353 return true;
354
355 if (dev_idx == HSR_DEV_SLAVE_A)
356 other = HSR_DEV_SLAVE_B;
357 else
358 other = HSR_DEV_SLAVE_A;
359
360 if (node->time_in_stale[other])
361 return false;
362
363 if (time_after(node->time_in[other], node->time_in[dev_idx] +
364 msecs_to_jiffies(MAX_SLAVE_DIFF)))
365 return true;
366
367 return false;
368}
369
370
371/* Remove stale sequence_nr records. Called by timer every
372 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
373 */
374void hsr_prune_nodes(struct hsr_priv *hsr_priv)
375{
376 struct node_entry *node;
377 unsigned long timestamp;
378 unsigned long time_a, time_b;
379
380 rcu_read_lock();
381 list_for_each_entry_rcu(node, &hsr_priv->node_db, mac_list) {
382 /* Shorthand */
383 time_a = node->time_in[HSR_DEV_SLAVE_A];
384 time_b = node->time_in[HSR_DEV_SLAVE_B];
385
386 /* Check for timestamps old enough to risk wrap-around */
387 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
388 node->time_in_stale[HSR_DEV_SLAVE_A] = true;
389 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
390 node->time_in_stale[HSR_DEV_SLAVE_B] = true;
391
392 /* Get age of newest frame from node.
393 * At least one time_in is OK here; nodes get pruned long
394 * before both time_ins can get stale
395 */
396 timestamp = time_a;
397 if (node->time_in_stale[HSR_DEV_SLAVE_A] ||
398 (!node->time_in_stale[HSR_DEV_SLAVE_B] &&
399 time_after(time_b, time_a)))
400 timestamp = time_b;
401
402 /* Warn of ring error only as long as we get frames at all */
403 if (time_is_after_jiffies(timestamp +
404 msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
405
406 if (is_late(node, HSR_DEV_SLAVE_A))
407 hsr_nl_ringerror(hsr_priv, node->MacAddressA,
408 HSR_DEV_SLAVE_A);
409 else if (is_late(node, HSR_DEV_SLAVE_B))
410 hsr_nl_ringerror(hsr_priv, node->MacAddressA,
411 HSR_DEV_SLAVE_B);
412 }
413
414 /* Prune old entries */
415 if (time_is_before_jiffies(timestamp +
416 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
417 hsr_nl_nodedown(hsr_priv, node->MacAddressA);
418 list_del_rcu(&node->mac_list);
419 /* Note that we need to free this entry later: */
420 call_rcu(&node->rcu_head, node_entry_reclaim);
421 }
422 }
423 rcu_read_unlock();
424}
425
426
427void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos,
428 unsigned char addr[ETH_ALEN])
429{
430 struct node_entry *node;
431
432 if (!_pos) {
433 node = list_first_or_null_rcu(&hsr_priv->node_db,
434 struct node_entry, mac_list);
435 if (node)
436 memcpy(addr, node->MacAddressA, ETH_ALEN);
437 return node;
438 }
439
440 node = _pos;
441 list_for_each_entry_continue_rcu(node, &hsr_priv->node_db, mac_list) {
442 memcpy(addr, node->MacAddressA, ETH_ALEN);
443 return node;
444 }
445
446 return NULL;
447}
448
449
450int hsr_get_node_data(struct hsr_priv *hsr_priv,
451 const unsigned char *addr,
452 unsigned char addr_b[ETH_ALEN],
453 unsigned int *addr_b_ifindex,
454 int *if1_age,
455 u16 *if1_seq,
456 int *if2_age,
457 u16 *if2_seq)
458{
459 struct node_entry *node;
460 unsigned long tdiff;
461
462
463 rcu_read_lock();
464 node = find_node_by_AddrA(&hsr_priv->node_db, addr);
465 if (!node) {
466 rcu_read_unlock();
467 return -ENOENT; /* No such entry */
468 }
469
470 memcpy(addr_b, node->MacAddressB, ETH_ALEN);
471
472 tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_A];
473 if (node->time_in_stale[HSR_DEV_SLAVE_A])
474 *if1_age = INT_MAX;
475#if HZ <= MSEC_PER_SEC
476 else if (tdiff > msecs_to_jiffies(INT_MAX))
477 *if1_age = INT_MAX;
478#endif
479 else
480 *if1_age = jiffies_to_msecs(tdiff);
481
482 tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_B];
483 if (node->time_in_stale[HSR_DEV_SLAVE_B])
484 *if2_age = INT_MAX;
485#if HZ <= MSEC_PER_SEC
486 else if (tdiff > msecs_to_jiffies(INT_MAX))
487 *if2_age = INT_MAX;
488#endif
489 else
490 *if2_age = jiffies_to_msecs(tdiff);
491
492 /* Present sequence numbers as if they were incoming on interface */
493 *if1_seq = node->seq_out[HSR_DEV_SLAVE_B];
494 *if2_seq = node->seq_out[HSR_DEV_SLAVE_A];
495
496 if ((node->AddrB_if != HSR_DEV_NONE) && hsr_priv->slave[node->AddrB_if])
497 *addr_b_ifindex = hsr_priv->slave[node->AddrB_if]->ifindex;
498 else
499 *addr_b_ifindex = -1;
500
501 rcu_read_unlock();
502
503 return 0;
504}
diff --git a/net/hsr/hsr_framereg.h b/net/hsr/hsr_framereg.h
new file mode 100644
index 000000000000..e6c4022030ad
--- /dev/null
+++ b/net/hsr/hsr_framereg.h
@@ -0,0 +1,53 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 */
11
12#ifndef _HSR_FRAMEREG_H
13#define _HSR_FRAMEREG_H
14
15#include "hsr_main.h"
16
17struct node_entry;
18
19struct node_entry *hsr_find_node(struct list_head *node_db, struct sk_buff *skb);
20
21struct node_entry *hsr_merge_node(struct hsr_priv *hsr_priv,
22 struct node_entry *node,
23 struct sk_buff *skb,
24 enum hsr_dev_idx dev_idx);
25
26void hsr_addr_subst_source(struct hsr_priv *hsr_priv, struct sk_buff *skb);
27void hsr_addr_subst_dest(struct hsr_priv *hsr_priv, struct ethhdr *ethhdr,
28 enum hsr_dev_idx dev_idx);
29
30void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx);
31
32int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx,
33 struct sk_buff *skb);
34
35void hsr_prune_nodes(struct hsr_priv *hsr_priv);
36
37int hsr_create_self_node(struct list_head *self_node_db,
38 unsigned char addr_a[ETH_ALEN],
39 unsigned char addr_b[ETH_ALEN]);
40
41void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos,
42 unsigned char addr[ETH_ALEN]);
43
44int hsr_get_node_data(struct hsr_priv *hsr_priv,
45 const unsigned char *addr,
46 unsigned char addr_b[ETH_ALEN],
47 unsigned int *addr_b_ifindex,
48 int *if1_age,
49 u16 *if1_seq,
50 int *if2_age,
51 u16 *if2_seq);
52
53#endif /* _HSR_FRAMEREG_H */
diff --git a/net/hsr/hsr_main.c b/net/hsr/hsr_main.c
new file mode 100644
index 000000000000..af68dd83a4e3
--- /dev/null
+++ b/net/hsr/hsr_main.c
@@ -0,0 +1,469 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 *
11 * In addition to routines for registering and unregistering HSR support, this
12 * file also contains the receive routine that handles all incoming frames with
13 * Ethertype (protocol) ETH_P_PRP (HSRv0), and network device event handling.
14 */
15
16#include <linux/netdevice.h>
17#include <linux/rculist.h>
18#include <linux/timer.h>
19#include <linux/etherdevice.h>
20#include "hsr_main.h"
21#include "hsr_device.h"
22#include "hsr_netlink.h"
23#include "hsr_framereg.h"
24
25
26/* List of all registered virtual HSR devices */
27static LIST_HEAD(hsr_list);
28
29void register_hsr_master(struct hsr_priv *hsr_priv)
30{
31 list_add_tail_rcu(&hsr_priv->hsr_list, &hsr_list);
32}
33
34void unregister_hsr_master(struct hsr_priv *hsr_priv)
35{
36 struct hsr_priv *hsr_priv_it;
37
38 list_for_each_entry(hsr_priv_it, &hsr_list, hsr_list)
39 if (hsr_priv_it == hsr_priv) {
40 list_del_rcu(&hsr_priv_it->hsr_list);
41 return;
42 }
43}
44
45bool is_hsr_slave(struct net_device *dev)
46{
47 struct hsr_priv *hsr_priv_it;
48
49 list_for_each_entry_rcu(hsr_priv_it, &hsr_list, hsr_list) {
50 if (dev == hsr_priv_it->slave[0])
51 return true;
52 if (dev == hsr_priv_it->slave[1])
53 return true;
54 }
55
56 return false;
57}
58
59
60/* If dev is a HSR slave device, return the virtual master device. Return NULL
61 * otherwise.
62 */
63static struct hsr_priv *get_hsr_master(struct net_device *dev)
64{
65 struct hsr_priv *hsr_priv;
66
67 rcu_read_lock();
68 list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list)
69 if ((dev == hsr_priv->slave[0]) ||
70 (dev == hsr_priv->slave[1])) {
71 rcu_read_unlock();
72 return hsr_priv;
73 }
74
75 rcu_read_unlock();
76 return NULL;
77}
78
79
80/* If dev is a HSR slave device, return the other slave device. Return NULL
81 * otherwise.
82 */
83static struct net_device *get_other_slave(struct hsr_priv *hsr_priv,
84 struct net_device *dev)
85{
86 if (dev == hsr_priv->slave[0])
87 return hsr_priv->slave[1];
88 if (dev == hsr_priv->slave[1])
89 return hsr_priv->slave[0];
90
91 return NULL;
92}
93
94
95static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event,
96 void *ptr)
97{
98 struct net_device *slave, *other_slave;
99 struct hsr_priv *hsr_priv;
100 int old_operstate;
101 int mtu_max;
102 int res;
103 struct net_device *dev;
104
105 dev = netdev_notifier_info_to_dev(ptr);
106
107 hsr_priv = get_hsr_master(dev);
108 if (hsr_priv) {
109 /* dev is a slave device */
110 slave = dev;
111 other_slave = get_other_slave(hsr_priv, slave);
112 } else {
113 if (!is_hsr_master(dev))
114 return NOTIFY_DONE;
115 hsr_priv = netdev_priv(dev);
116 slave = hsr_priv->slave[0];
117 other_slave = hsr_priv->slave[1];
118 }
119
120 switch (event) {
121 case NETDEV_UP: /* Administrative state DOWN */
122 case NETDEV_DOWN: /* Administrative state UP */
123 case NETDEV_CHANGE: /* Link (carrier) state changes */
124 old_operstate = hsr_priv->dev->operstate;
125 hsr_set_carrier(hsr_priv->dev, slave, other_slave);
126 /* netif_stacked_transfer_operstate() cannot be used here since
127 * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
128 */
129 hsr_set_operstate(hsr_priv->dev, slave, other_slave);
130 hsr_check_announce(hsr_priv->dev, old_operstate);
131 break;
132 case NETDEV_CHANGEADDR:
133
134 /* This should not happen since there's no ndo_set_mac_address()
135 * for HSR devices - i.e. not supported.
136 */
137 if (dev == hsr_priv->dev)
138 break;
139
140 if (dev == hsr_priv->slave[0])
141 memcpy(hsr_priv->dev->dev_addr,
142 hsr_priv->slave[0]->dev_addr, ETH_ALEN);
143
144 /* Make sure we recognize frames from ourselves in hsr_rcv() */
145 res = hsr_create_self_node(&hsr_priv->self_node_db,
146 hsr_priv->dev->dev_addr,
147 hsr_priv->slave[1] ?
148 hsr_priv->slave[1]->dev_addr :
149 hsr_priv->dev->dev_addr);
150 if (res)
151 netdev_warn(hsr_priv->dev,
152 "Could not update HSR node address.\n");
153
154 if (dev == hsr_priv->slave[0])
155 call_netdevice_notifiers(NETDEV_CHANGEADDR, hsr_priv->dev);
156 break;
157 case NETDEV_CHANGEMTU:
158 if (dev == hsr_priv->dev)
159 break; /* Handled in ndo_change_mtu() */
160 mtu_max = hsr_get_max_mtu(hsr_priv);
161 if (hsr_priv->dev->mtu > mtu_max)
162 dev_set_mtu(hsr_priv->dev, mtu_max);
163 break;
164 case NETDEV_UNREGISTER:
165 if (dev == hsr_priv->slave[0])
166 hsr_priv->slave[0] = NULL;
167 if (dev == hsr_priv->slave[1])
168 hsr_priv->slave[1] = NULL;
169
170 /* There should really be a way to set a new slave device... */
171
172 break;
173 case NETDEV_PRE_TYPE_CHANGE:
174 /* HSR works only on Ethernet devices. Refuse slave to change
175 * its type.
176 */
177 return NOTIFY_BAD;
178 }
179
180 return NOTIFY_DONE;
181}
182
183
184static struct timer_list prune_timer;
185
186static void prune_nodes_all(unsigned long data)
187{
188 struct hsr_priv *hsr_priv;
189
190 rcu_read_lock();
191 list_for_each_entry_rcu(hsr_priv, &hsr_list, hsr_list)
192 hsr_prune_nodes(hsr_priv);
193 rcu_read_unlock();
194
195 prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
196 add_timer(&prune_timer);
197}
198
199
200static struct sk_buff *hsr_pull_tag(struct sk_buff *skb)
201{
202 struct hsr_tag *hsr_tag;
203 struct sk_buff *skb2;
204
205 skb2 = skb_share_check(skb, GFP_ATOMIC);
206 if (unlikely(!skb2))
207 goto err_free;
208 skb = skb2;
209
210 if (unlikely(!pskb_may_pull(skb, HSR_TAGLEN)))
211 goto err_free;
212
213 hsr_tag = (struct hsr_tag *) skb->data;
214 skb->protocol = hsr_tag->encap_proto;
215 skb_pull(skb, HSR_TAGLEN);
216
217 return skb;
218
219err_free:
220 kfree_skb(skb);
221 return NULL;
222}
223
224
225/* The uses I can see for these HSR supervision frames are:
226 * 1) Use the frames that are sent after node initialization ("HSR_TLV.Type =
227 * 22") to reset any sequence_nr counters belonging to that node. Useful if
228 * the other node's counter has been reset for some reason.
229 * --
230 * Or not - resetting the counter and bridging the frame would create a
231 * loop, unfortunately.
232 *
233 * 2) Use the LifeCheck frames to detect ring breaks. I.e. if no LifeCheck
234 * frame is received from a particular node, we know something is wrong.
235 * We just register these (as with normal frames) and throw them away.
236 *
237 * 3) Allow different MAC addresses for the two slave interfaces, using the
238 * MacAddressA field.
239 */
240static bool is_supervision_frame(struct hsr_priv *hsr_priv, struct sk_buff *skb)
241{
242 struct hsr_sup_tag *hsr_stag;
243
244 if (!ether_addr_equal(eth_hdr(skb)->h_dest,
245 hsr_priv->sup_multicast_addr))
246 return false;
247
248 hsr_stag = (struct hsr_sup_tag *) skb->data;
249 if (get_hsr_stag_path(hsr_stag) != 0x0f)
250 return false;
251 if ((hsr_stag->HSR_TLV_Type != HSR_TLV_ANNOUNCE) &&
252 (hsr_stag->HSR_TLV_Type != HSR_TLV_LIFE_CHECK))
253 return false;
254 if (hsr_stag->HSR_TLV_Length != 12)
255 return false;
256
257 return true;
258}
259
260
261/* Implementation somewhat according to IEC-62439-3, p. 43
262 */
263static int hsr_rcv(struct sk_buff *skb, struct net_device *dev,
264 struct packet_type *pt, struct net_device *orig_dev)
265{
266 struct hsr_priv *hsr_priv;
267 struct net_device *other_slave;
268 struct node_entry *node;
269 bool deliver_to_self;
270 struct sk_buff *skb_deliver;
271 enum hsr_dev_idx dev_in_idx, dev_other_idx;
272 bool dup_out;
273 int ret;
274
275 hsr_priv = get_hsr_master(dev);
276
277 if (!hsr_priv) {
278 /* Non-HSR-slave device 'dev' is connected to a HSR network */
279 kfree_skb(skb);
280 dev->stats.rx_errors++;
281 return NET_RX_SUCCESS;
282 }
283
284 if (dev == hsr_priv->slave[0]) {
285 dev_in_idx = HSR_DEV_SLAVE_A;
286 dev_other_idx = HSR_DEV_SLAVE_B;
287 } else {
288 dev_in_idx = HSR_DEV_SLAVE_B;
289 dev_other_idx = HSR_DEV_SLAVE_A;
290 }
291
292 node = hsr_find_node(&hsr_priv->self_node_db, skb);
293 if (node) {
294 /* Always kill frames sent by ourselves */
295 kfree_skb(skb);
296 return NET_RX_SUCCESS;
297 }
298
299 /* Is this frame a candidate for local reception? */
300 deliver_to_self = false;
301 if ((skb->pkt_type == PACKET_HOST) ||
302 (skb->pkt_type == PACKET_MULTICAST) ||
303 (skb->pkt_type == PACKET_BROADCAST))
304 deliver_to_self = true;
305 else if (ether_addr_equal(eth_hdr(skb)->h_dest,
306 hsr_priv->dev->dev_addr)) {
307 skb->pkt_type = PACKET_HOST;
308 deliver_to_self = true;
309 }
310
311
312 rcu_read_lock(); /* node_db */
313 node = hsr_find_node(&hsr_priv->node_db, skb);
314
315 if (is_supervision_frame(hsr_priv, skb)) {
316 skb_pull(skb, sizeof(struct hsr_sup_tag));
317 node = hsr_merge_node(hsr_priv, node, skb, dev_in_idx);
318 if (!node) {
319 rcu_read_unlock(); /* node_db */
320 kfree_skb(skb);
321 hsr_priv->dev->stats.rx_dropped++;
322 return NET_RX_DROP;
323 }
324 skb_push(skb, sizeof(struct hsr_sup_tag));
325 deliver_to_self = false;
326 }
327
328 if (!node) {
329 /* Source node unknown; this might be a HSR frame from
330 * another net (different multicast address). Ignore it.
331 */
332 rcu_read_unlock(); /* node_db */
333 kfree_skb(skb);
334 return NET_RX_SUCCESS;
335 }
336
337 /* Register ALL incoming frames as outgoing through the other interface.
338 * This allows us to register frames as incoming only if they are valid
339 * for the receiving interface, without using a specific counter for
340 * incoming frames.
341 */
342 dup_out = hsr_register_frame_out(node, dev_other_idx, skb);
343 if (!dup_out)
344 hsr_register_frame_in(node, dev_in_idx);
345
346 /* Forward this frame? */
347 if (!dup_out && (skb->pkt_type != PACKET_HOST))
348 other_slave = get_other_slave(hsr_priv, dev);
349 else
350 other_slave = NULL;
351
352 if (hsr_register_frame_out(node, HSR_DEV_MASTER, skb))
353 deliver_to_self = false;
354
355 rcu_read_unlock(); /* node_db */
356
357 if (!deliver_to_self && !other_slave) {
358 kfree_skb(skb);
359 /* Circulated frame; silently remove it. */
360 return NET_RX_SUCCESS;
361 }
362
363 skb_deliver = skb;
364 if (deliver_to_self && other_slave) {
365 /* skb_clone() is not enough since we will strip the hsr tag
366 * and do address substitution below
367 */
368 skb_deliver = pskb_copy(skb, GFP_ATOMIC);
369 if (!skb_deliver) {
370 deliver_to_self = false;
371 hsr_priv->dev->stats.rx_dropped++;
372 }
373 }
374
375 if (deliver_to_self) {
376 bool multicast_frame;
377
378 skb_deliver = hsr_pull_tag(skb_deliver);
379 if (!skb_deliver) {
380 hsr_priv->dev->stats.rx_dropped++;
381 goto forward;
382 }
383#if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
384 /* Move everything in the header that is after the HSR tag,
385 * to work around alignment problems caused by the 6-byte HSR
386 * tag. In practice, this removes/overwrites the HSR tag in
387 * the header and restores a "standard" packet.
388 */
389 memmove(skb_deliver->data - HSR_TAGLEN, skb_deliver->data,
390 skb_headlen(skb_deliver));
391
392 /* Adjust skb members so they correspond with the move above.
393 * This cannot possibly underflow skb->data since hsr_pull_tag()
394 * above succeeded.
395 * At this point in the protocol stack, the transport and
396 * network headers have not been set yet, and we haven't touched
397 * the mac header nor the head. So we only need to adjust data
398 * and tail:
399 */
400 skb_deliver->data -= HSR_TAGLEN;
401 skb_deliver->tail -= HSR_TAGLEN;
402#endif
403 skb_deliver->dev = hsr_priv->dev;
404 hsr_addr_subst_source(hsr_priv, skb_deliver);
405 multicast_frame = (skb_deliver->pkt_type == PACKET_MULTICAST);
406 ret = netif_rx(skb_deliver);
407 if (ret == NET_RX_DROP) {
408 hsr_priv->dev->stats.rx_dropped++;
409 } else {
410 hsr_priv->dev->stats.rx_packets++;
411 hsr_priv->dev->stats.rx_bytes += skb->len;
412 if (multicast_frame)
413 hsr_priv->dev->stats.multicast++;
414 }
415 }
416
417forward:
418 if (other_slave) {
419 skb_push(skb, ETH_HLEN);
420 skb->dev = other_slave;
421 dev_queue_xmit(skb);
422 }
423
424 return NET_RX_SUCCESS;
425}
426
427
428static struct packet_type hsr_pt __read_mostly = {
429 .type = htons(ETH_P_PRP),
430 .func = hsr_rcv,
431};
432
433static struct notifier_block hsr_nb = {
434 .notifier_call = hsr_netdev_notify, /* Slave event notifications */
435};
436
437
438static int __init hsr_init(void)
439{
440 int res;
441
442 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_TAGLEN);
443
444 dev_add_pack(&hsr_pt);
445
446 init_timer(&prune_timer);
447 prune_timer.function = prune_nodes_all;
448 prune_timer.data = 0;
449 prune_timer.expires = jiffies + msecs_to_jiffies(PRUNE_PERIOD);
450 add_timer(&prune_timer);
451
452 register_netdevice_notifier(&hsr_nb);
453
454 res = hsr_netlink_init();
455
456 return res;
457}
458
459static void __exit hsr_exit(void)
460{
461 unregister_netdevice_notifier(&hsr_nb);
462 del_timer(&prune_timer);
463 hsr_netlink_exit();
464 dev_remove_pack(&hsr_pt);
465}
466
467module_init(hsr_init);
468module_exit(hsr_exit);
469MODULE_LICENSE("GPL");
diff --git a/net/hsr/hsr_main.h b/net/hsr/hsr_main.h
new file mode 100644
index 000000000000..56fe060c0ab1
--- /dev/null
+++ b/net/hsr/hsr_main.h
@@ -0,0 +1,166 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 */
11
12#ifndef _HSR_PRIVATE_H
13#define _HSR_PRIVATE_H
14
15#include <linux/netdevice.h>
16#include <linux/list.h>
17
18
19/* Time constants as specified in the HSR specification (IEC-62439-3 2010)
20 * Table 8.
21 * All values in milliseconds.
22 */
23#define HSR_LIFE_CHECK_INTERVAL 2000 /* ms */
24#define HSR_NODE_FORGET_TIME 60000 /* ms */
25#define HSR_ANNOUNCE_INTERVAL 100 /* ms */
26
27
28/* By how much may slave1 and slave2 timestamps of latest received frame from
29 * each node differ before we notify of communication problem?
30 */
31#define MAX_SLAVE_DIFF 3000 /* ms */
32
33
34/* How often shall we check for broken ring and remove node entries older than
35 * HSR_NODE_FORGET_TIME?
36 */
37#define PRUNE_PERIOD 3000 /* ms */
38
39
40#define HSR_TLV_ANNOUNCE 22
41#define HSR_TLV_LIFE_CHECK 23
42
43
44/* HSR Tag.
45 * As defined in IEC-62439-3:2010, the HSR tag is really { ethertype = 0x88FB,
46 * path, LSDU_size, sequence Nr }. But we let eth_header() create { h_dest,
47 * h_source, h_proto = 0x88FB }, and add { path, LSDU_size, sequence Nr,
48 * encapsulated protocol } instead.
49 */
50#define HSR_TAGLEN 6
51
52/* Field names below as defined in the IEC:2010 standard for HSR. */
53struct hsr_tag {
54 __be16 path_and_LSDU_size;
55 __be16 sequence_nr;
56 __be16 encap_proto;
57} __packed;
58
59
60/* The helper functions below assumes that 'path' occupies the 4 most
61 * significant bits of the 16-bit field shared by 'path' and 'LSDU_size' (or
62 * equivalently, the 4 most significant bits of HSR tag byte 14).
63 *
64 * This is unclear in the IEC specification; its definition of MAC addresses
65 * indicates the spec is written with the least significant bit first (to the
66 * left). This, however, would mean that the LSDU field would be split in two
67 * with the path field in-between, which seems strange. I'm guessing the MAC
68 * address definition is in error.
69 */
70static inline u16 get_hsr_tag_path(struct hsr_tag *ht)
71{
72 return ntohs(ht->path_and_LSDU_size) >> 12;
73}
74
75static inline u16 get_hsr_tag_LSDU_size(struct hsr_tag *ht)
76{
77 return ntohs(ht->path_and_LSDU_size) & 0x0FFF;
78}
79
80static inline void set_hsr_tag_path(struct hsr_tag *ht, u16 path)
81{
82 ht->path_and_LSDU_size = htons(
83 (ntohs(ht->path_and_LSDU_size) & 0x0FFF) | (path << 12));
84}
85
86static inline void set_hsr_tag_LSDU_size(struct hsr_tag *ht, u16 LSDU_size)
87{
88 ht->path_and_LSDU_size = htons(
89 (ntohs(ht->path_and_LSDU_size) & 0xF000) |
90 (LSDU_size & 0x0FFF));
91}
92
93struct hsr_ethhdr {
94 struct ethhdr ethhdr;
95 struct hsr_tag hsr_tag;
96} __packed;
97
98
99/* HSR Supervision Frame data types.
100 * Field names as defined in the IEC:2010 standard for HSR.
101 */
102struct hsr_sup_tag {
103 __be16 path_and_HSR_Ver;
104 __be16 sequence_nr;
105 __u8 HSR_TLV_Type;
106 __u8 HSR_TLV_Length;
107} __packed;
108
109struct hsr_sup_payload {
110 unsigned char MacAddressA[ETH_ALEN];
111} __packed;
112
113static inline u16 get_hsr_stag_path(struct hsr_sup_tag *hst)
114{
115 return get_hsr_tag_path((struct hsr_tag *) hst);
116}
117
118static inline u16 get_hsr_stag_HSR_ver(struct hsr_sup_tag *hst)
119{
120 return get_hsr_tag_LSDU_size((struct hsr_tag *) hst);
121}
122
123static inline void set_hsr_stag_path(struct hsr_sup_tag *hst, u16 path)
124{
125 set_hsr_tag_path((struct hsr_tag *) hst, path);
126}
127
128static inline void set_hsr_stag_HSR_Ver(struct hsr_sup_tag *hst, u16 HSR_Ver)
129{
130 set_hsr_tag_LSDU_size((struct hsr_tag *) hst, HSR_Ver);
131}
132
133struct hsr_ethhdr_sp {
134 struct ethhdr ethhdr;
135 struct hsr_sup_tag hsr_sup;
136} __packed;
137
138
139enum hsr_dev_idx {
140 HSR_DEV_NONE = -1,
141 HSR_DEV_SLAVE_A = 0,
142 HSR_DEV_SLAVE_B,
143 HSR_DEV_MASTER,
144};
145#define HSR_MAX_SLAVE (HSR_DEV_SLAVE_B + 1)
146#define HSR_MAX_DEV (HSR_DEV_MASTER + 1)
147
148struct hsr_priv {
149 struct list_head hsr_list; /* List of hsr devices */
150 struct rcu_head rcu_head;
151 struct net_device *dev;
152 struct net_device *slave[HSR_MAX_SLAVE];
153 struct list_head node_db; /* Other HSR nodes */
154 struct list_head self_node_db; /* MACs of slaves */
155 struct timer_list announce_timer; /* Supervision frame dispatch */
156 int announce_count;
157 u16 sequence_nr;
158 spinlock_t seqnr_lock; /* locking for sequence_nr */
159 unsigned char sup_multicast_addr[ETH_ALEN];
160};
161
162void register_hsr_master(struct hsr_priv *hsr_priv);
163void unregister_hsr_master(struct hsr_priv *hsr_priv);
164bool is_hsr_slave(struct net_device *dev);
165
166#endif /* _HSR_PRIVATE_H */
diff --git a/net/hsr/hsr_netlink.c b/net/hsr/hsr_netlink.c
new file mode 100644
index 000000000000..01a5261ac7a5
--- /dev/null
+++ b/net/hsr/hsr_netlink.c
@@ -0,0 +1,465 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 *
11 * Routines for handling Netlink messages for HSR.
12 */
13
14#include "hsr_netlink.h"
15#include <linux/kernel.h>
16#include <net/rtnetlink.h>
17#include <net/genetlink.h>
18#include "hsr_main.h"
19#include "hsr_device.h"
20#include "hsr_framereg.h"
21
22static const struct nla_policy hsr_policy[IFLA_HSR_MAX + 1] = {
23 [IFLA_HSR_SLAVE1] = { .type = NLA_U32 },
24 [IFLA_HSR_SLAVE2] = { .type = NLA_U32 },
25 [IFLA_HSR_MULTICAST_SPEC] = { .type = NLA_U8 },
26 [IFLA_HSR_SUPERVISION_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
27 [IFLA_HSR_SEQ_NR] = { .type = NLA_U16 },
28};
29
30
31/* Here, it seems a netdevice has already been allocated for us, and the
32 * hsr_dev_setup routine has been executed. Nice!
33 */
34static int hsr_newlink(struct net *src_net, struct net_device *dev,
35 struct nlattr *tb[], struct nlattr *data[])
36{
37 struct net_device *link[2];
38 unsigned char multicast_spec;
39
40 if (!data[IFLA_HSR_SLAVE1]) {
41 netdev_info(dev, "IFLA_HSR_SLAVE1 missing!\n");
42 return -EINVAL;
43 }
44 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
45 if (!data[IFLA_HSR_SLAVE2]) {
46 netdev_info(dev, "IFLA_HSR_SLAVE2 missing!\n");
47 return -EINVAL;
48 }
49 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
50
51 if (!link[0] || !link[1])
52 return -ENODEV;
53 if (link[0] == link[1])
54 return -EINVAL;
55
56 if (!data[IFLA_HSR_MULTICAST_SPEC])
57 multicast_spec = 0;
58 else
59 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
60
61 return hsr_dev_finalize(dev, link, multicast_spec);
62}
63
64static int hsr_fill_info(struct sk_buff *skb, const struct net_device *dev)
65{
66 struct hsr_priv *hsr_priv;
67
68 hsr_priv = netdev_priv(dev);
69
70 if (hsr_priv->slave[0])
71 if (nla_put_u32(skb, IFLA_HSR_SLAVE1, hsr_priv->slave[0]->ifindex))
72 goto nla_put_failure;
73
74 if (hsr_priv->slave[1])
75 if (nla_put_u32(skb, IFLA_HSR_SLAVE2, hsr_priv->slave[1]->ifindex))
76 goto nla_put_failure;
77
78 if (nla_put(skb, IFLA_HSR_SUPERVISION_ADDR, ETH_ALEN,
79 hsr_priv->sup_multicast_addr) ||
80 nla_put_u16(skb, IFLA_HSR_SEQ_NR, hsr_priv->sequence_nr))
81 goto nla_put_failure;
82
83 return 0;
84
85nla_put_failure:
86 return -EMSGSIZE;
87}
88
89static struct rtnl_link_ops hsr_link_ops __read_mostly = {
90 .kind = "hsr",
91 .maxtype = IFLA_HSR_MAX,
92 .policy = hsr_policy,
93 .priv_size = sizeof(struct hsr_priv),
94 .setup = hsr_dev_setup,
95 .newlink = hsr_newlink,
96 .fill_info = hsr_fill_info,
97};
98
99
100
101/* attribute policy */
102/* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
103static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
104 [HSR_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
105 [HSR_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
106 [HSR_A_IFINDEX] = { .type = NLA_U32 },
107 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
108 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
109 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
110 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
111};
112
113static struct genl_family hsr_genl_family = {
114 .id = GENL_ID_GENERATE,
115 .hdrsize = 0,
116 .name = "HSR",
117 .version = 1,
118 .maxattr = HSR_A_MAX,
119};
120
121static const struct genl_multicast_group hsr_mcgrps[] = {
122 { .name = "hsr-network", },
123};
124
125
126
127/* This is called if for some node with MAC address addr, we only get frames
128 * over one of the slave interfaces. This would indicate an open network ring
129 * (i.e. a link has failed somewhere).
130 */
131void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN],
132 enum hsr_dev_idx dev_idx)
133{
134 struct sk_buff *skb;
135 void *msg_head;
136 int res;
137 int ifindex;
138
139 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
140 if (!skb)
141 goto fail;
142
143 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
144 if (!msg_head)
145 goto nla_put_failure;
146
147 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
148 if (res < 0)
149 goto nla_put_failure;
150
151 if (hsr_priv->slave[dev_idx])
152 ifindex = hsr_priv->slave[dev_idx]->ifindex;
153 else
154 ifindex = -1;
155 res = nla_put_u32(skb, HSR_A_IFINDEX, ifindex);
156 if (res < 0)
157 goto nla_put_failure;
158
159 genlmsg_end(skb, msg_head);
160 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
161
162 return;
163
164nla_put_failure:
165 kfree_skb(skb);
166
167fail:
168 netdev_warn(hsr_priv->dev, "Could not send HSR ring error message\n");
169}
170
171/* This is called when we haven't heard from the node with MAC address addr for
172 * some time (just before the node is removed from the node table/list).
173 */
174void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN])
175{
176 struct sk_buff *skb;
177 void *msg_head;
178 int res;
179
180 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
181 if (!skb)
182 goto fail;
183
184 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
185 if (!msg_head)
186 goto nla_put_failure;
187
188
189 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
190 if (res < 0)
191 goto nla_put_failure;
192
193 genlmsg_end(skb, msg_head);
194 genlmsg_multicast(&hsr_genl_family, skb, 0, 0, GFP_ATOMIC);
195
196 return;
197
198nla_put_failure:
199 kfree_skb(skb);
200
201fail:
202 netdev_warn(hsr_priv->dev, "Could not send HSR node down\n");
203}
204
205
206/* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
207 * about the status of a specific node in the network, defined by its MAC
208 * address.
209 *
210 * Input: hsr ifindex, node mac address
211 * Output: hsr ifindex, node mac address (copied from request),
212 * age of latest frame from node over slave 1, slave 2 [ms]
213 */
214static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
215{
216 /* For receiving */
217 struct nlattr *na;
218 struct net_device *hsr_dev;
219
220 /* For sending */
221 struct sk_buff *skb_out;
222 void *msg_head;
223 struct hsr_priv *hsr_priv;
224 unsigned char hsr_node_addr_b[ETH_ALEN];
225 int hsr_node_if1_age;
226 u16 hsr_node_if1_seq;
227 int hsr_node_if2_age;
228 u16 hsr_node_if2_seq;
229 int addr_b_ifindex;
230 int res;
231
232 if (!info)
233 goto invalid;
234
235 na = info->attrs[HSR_A_IFINDEX];
236 if (!na)
237 goto invalid;
238 na = info->attrs[HSR_A_NODE_ADDR];
239 if (!na)
240 goto invalid;
241
242 hsr_dev = __dev_get_by_index(genl_info_net(info),
243 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
244 if (!hsr_dev)
245 goto invalid;
246 if (!is_hsr_master(hsr_dev))
247 goto invalid;
248
249
250 /* Send reply */
251
252 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
253 if (!skb_out) {
254 res = -ENOMEM;
255 goto fail;
256 }
257
258 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
259 info->snd_seq, &hsr_genl_family, 0,
260 HSR_C_SET_NODE_STATUS);
261 if (!msg_head) {
262 res = -ENOMEM;
263 goto nla_put_failure;
264 }
265
266 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
267 if (res < 0)
268 goto nla_put_failure;
269
270 hsr_priv = netdev_priv(hsr_dev);
271 res = hsr_get_node_data(hsr_priv,
272 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
273 hsr_node_addr_b,
274 &addr_b_ifindex,
275 &hsr_node_if1_age,
276 &hsr_node_if1_seq,
277 &hsr_node_if2_age,
278 &hsr_node_if2_seq);
279 if (res < 0)
280 goto nla_put_failure;
281
282 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
283 nla_data(info->attrs[HSR_A_NODE_ADDR]));
284 if (res < 0)
285 goto nla_put_failure;
286
287 if (addr_b_ifindex > -1) {
288 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
289 hsr_node_addr_b);
290 if (res < 0)
291 goto nla_put_failure;
292
293 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
294 if (res < 0)
295 goto nla_put_failure;
296 }
297
298 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
299 if (res < 0)
300 goto nla_put_failure;
301 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
302 if (res < 0)
303 goto nla_put_failure;
304 if (hsr_priv->slave[0])
305 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
306 hsr_priv->slave[0]->ifindex);
307 if (res < 0)
308 goto nla_put_failure;
309
310 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
311 if (res < 0)
312 goto nla_put_failure;
313 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
314 if (res < 0)
315 goto nla_put_failure;
316 if (hsr_priv->slave[1])
317 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
318 hsr_priv->slave[1]->ifindex);
319
320 genlmsg_end(skb_out, msg_head);
321 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
322
323 return 0;
324
325invalid:
326 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
327 return 0;
328
329nla_put_failure:
330 kfree_skb(skb_out);
331 /* Fall through */
332
333fail:
334 return res;
335}
336
337/* Get a list of MacAddressA of all nodes known to this node (other than self).
338 */
339static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
340{
341 /* For receiving */
342 struct nlattr *na;
343 struct net_device *hsr_dev;
344
345 /* For sending */
346 struct sk_buff *skb_out;
347 void *msg_head;
348 struct hsr_priv *hsr_priv;
349 void *pos;
350 unsigned char addr[ETH_ALEN];
351 int res;
352
353 if (!info)
354 goto invalid;
355
356 na = info->attrs[HSR_A_IFINDEX];
357 if (!na)
358 goto invalid;
359
360 hsr_dev = __dev_get_by_index(genl_info_net(info),
361 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
362 if (!hsr_dev)
363 goto invalid;
364 if (!is_hsr_master(hsr_dev))
365 goto invalid;
366
367
368 /* Send reply */
369
370 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
371 if (!skb_out) {
372 res = -ENOMEM;
373 goto fail;
374 }
375
376 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
377 info->snd_seq, &hsr_genl_family, 0,
378 HSR_C_SET_NODE_LIST);
379 if (!msg_head) {
380 res = -ENOMEM;
381 goto nla_put_failure;
382 }
383
384 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
385 if (res < 0)
386 goto nla_put_failure;
387
388 hsr_priv = netdev_priv(hsr_dev);
389
390 rcu_read_lock();
391 pos = hsr_get_next_node(hsr_priv, NULL, addr);
392 while (pos) {
393 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
394 if (res < 0) {
395 rcu_read_unlock();
396 goto nla_put_failure;
397 }
398 pos = hsr_get_next_node(hsr_priv, pos, addr);
399 }
400 rcu_read_unlock();
401
402 genlmsg_end(skb_out, msg_head);
403 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
404
405 return 0;
406
407invalid:
408 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
409 return 0;
410
411nla_put_failure:
412 kfree_skb(skb_out);
413 /* Fall through */
414
415fail:
416 return res;
417}
418
419
420static const struct genl_ops hsr_ops[] = {
421 {
422 .cmd = HSR_C_GET_NODE_STATUS,
423 .flags = 0,
424 .policy = hsr_genl_policy,
425 .doit = hsr_get_node_status,
426 .dumpit = NULL,
427 },
428 {
429 .cmd = HSR_C_GET_NODE_LIST,
430 .flags = 0,
431 .policy = hsr_genl_policy,
432 .doit = hsr_get_node_list,
433 .dumpit = NULL,
434 },
435};
436
437int __init hsr_netlink_init(void)
438{
439 int rc;
440
441 rc = rtnl_link_register(&hsr_link_ops);
442 if (rc)
443 goto fail_rtnl_link_register;
444
445 rc = genl_register_family_with_ops_groups(&hsr_genl_family, hsr_ops,
446 hsr_mcgrps);
447 if (rc)
448 goto fail_genl_register_family;
449
450 return 0;
451
452fail_genl_register_family:
453 rtnl_link_unregister(&hsr_link_ops);
454fail_rtnl_link_register:
455
456 return rc;
457}
458
459void __exit hsr_netlink_exit(void)
460{
461 genl_unregister_family(&hsr_genl_family);
462 rtnl_link_unregister(&hsr_link_ops);
463}
464
465MODULE_ALIAS_RTNL_LINK("hsr");
diff --git a/net/hsr/hsr_netlink.h b/net/hsr/hsr_netlink.h
new file mode 100644
index 000000000000..d4579dcc3c7d
--- /dev/null
+++ b/net/hsr/hsr_netlink.h
@@ -0,0 +1,30 @@
1/* Copyright 2011-2013 Autronica Fire and Security AS
2 *
3 * This program is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU General Public License as published by the Free
5 * Software Foundation; either version 2 of the License, or (at your option)
6 * any later version.
7 *
8 * Author(s):
9 * 2011-2013 Arvid Brodin, arvid.brodin@xdin.com
10 */
11
12#ifndef __HSR_NETLINK_H
13#define __HSR_NETLINK_H
14
15#include <linux/if_ether.h>
16#include <linux/module.h>
17#include <uapi/linux/hsr_netlink.h>
18
19struct hsr_priv;
20
21int __init hsr_netlink_init(void);
22void __exit hsr_netlink_exit(void);
23
24void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN],
25 int dev_idx);
26void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN]);
27void hsr_nl_framedrop(int dropcount, int dev_idx);
28void hsr_nl_linkdown(int dev_idx);
29
30#endif /* __HSR_NETLINK_H */
diff --git a/net/ieee802154/6lowpan.c b/net/ieee802154/6lowpan.c
index ff41b4d60d30..a2d2456a557a 100644
--- a/net/ieee802154/6lowpan.c
+++ b/net/ieee802154/6lowpan.c
@@ -440,7 +440,6 @@ lowpan_uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
440 default: 440 default:
441 pr_debug("ERROR: unknown UDP format\n"); 441 pr_debug("ERROR: unknown UDP format\n");
442 goto err; 442 goto err;
443 break;
444 } 443 }
445 444
446 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n", 445 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
@@ -548,7 +547,7 @@ static int lowpan_header_create(struct sk_buff *skb,
548 hc06_ptr += 3; 547 hc06_ptr += 3;
549 } else { 548 } else {
550 /* compress nothing */ 549 /* compress nothing */
551 memcpy(hc06_ptr, &hdr, 4); 550 memcpy(hc06_ptr, hdr, 4);
552 /* replace the top byte with new ECN | DSCP format */ 551 /* replace the top byte with new ECN | DSCP format */
553 *hc06_ptr = tmp; 552 *hc06_ptr = tmp;
554 hc06_ptr += 4; 553 hc06_ptr += 4;
@@ -655,7 +654,9 @@ static int lowpan_header_create(struct sk_buff *skb,
655 head[1] = iphc1; 654 head[1] = iphc1;
656 655
657 skb_pull(skb, sizeof(struct ipv6hdr)); 656 skb_pull(skb, sizeof(struct ipv6hdr));
657 skb_reset_transport_header(skb);
658 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head); 658 memcpy(skb_push(skb, hc06_ptr - head), head, hc06_ptr - head);
659 skb_reset_network_header(skb);
659 660
660 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data, 661 lowpan_raw_dump_table(__func__, "raw skb data dump", skb->data,
661 skb->len); 662 skb->len);
@@ -738,7 +739,6 @@ static int lowpan_skb_deliver(struct sk_buff *skb, struct ipv6hdr *hdr)
738 return -ENOMEM; 739 return -ENOMEM;
739 740
740 skb_push(new, sizeof(struct ipv6hdr)); 741 skb_push(new, sizeof(struct ipv6hdr));
741 skb_reset_network_header(new);
742 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr)); 742 skb_copy_to_linear_data(new, hdr, sizeof(struct ipv6hdr));
743 743
744 new->protocol = htons(ETH_P_IPV6); 744 new->protocol = htons(ETH_P_IPV6);
@@ -785,7 +785,6 @@ lowpan_alloc_new_frame(struct sk_buff *skb, u16 len, u16 tag)
785 goto skb_err; 785 goto skb_err;
786 786
787 frame->skb->priority = skb->priority; 787 frame->skb->priority = skb->priority;
788 frame->skb->dev = skb->dev;
789 788
790 /* reserve headroom for uncompressed ipv6 header */ 789 /* reserve headroom for uncompressed ipv6 header */
791 skb_reserve(frame->skb, sizeof(struct ipv6hdr)); 790 skb_reserve(frame->skb, sizeof(struct ipv6hdr));
@@ -957,7 +956,7 @@ lowpan_process_data(struct sk_buff *skb)
957 * Traffic class carried in-line 956 * Traffic class carried in-line
958 * ECN + DSCP (1 byte), Flow Label is elided 957 * ECN + DSCP (1 byte), Flow Label is elided
959 */ 958 */
960 case 1: /* 10b */ 959 case 2: /* 10b */
961 if (lowpan_fetch_skb_u8(skb, &tmp)) 960 if (lowpan_fetch_skb_u8(skb, &tmp))
962 goto drop; 961 goto drop;
963 962
@@ -968,7 +967,7 @@ lowpan_process_data(struct sk_buff *skb)
968 * Flow Label carried in-line 967 * Flow Label carried in-line
969 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided 968 * ECN + 2-bit Pad + Flow Label (3 bytes), DSCP is elided
970 */ 969 */
971 case 2: /* 01b */ 970 case 1: /* 01b */
972 if (lowpan_fetch_skb_u8(skb, &tmp)) 971 if (lowpan_fetch_skb_u8(skb, &tmp))
973 goto drop; 972 goto drop;
974 973
@@ -1061,7 +1060,6 @@ lowpan_process_data(struct sk_buff *skb)
1061 skb = new; 1060 skb = new;
1062 1061
1063 skb_push(skb, sizeof(struct udphdr)); 1062 skb_push(skb, sizeof(struct udphdr));
1064 skb_reset_transport_header(skb);
1065 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr)); 1063 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
1066 1064
1067 lowpan_raw_dump_table(__func__, "raw UDP header dump", 1065 lowpan_raw_dump_table(__func__, "raw UDP header dump",
@@ -1104,50 +1102,40 @@ static int lowpan_set_address(struct net_device *dev, void *p)
1104 return 0; 1102 return 0;
1105} 1103}
1106 1104
1107static int lowpan_get_mac_header_length(struct sk_buff *skb)
1108{
1109 /*
1110 * Currently long addressing mode is supported only, so the overall
1111 * header size is 21:
1112 * FC SeqNum DPAN DA SA Sec
1113 * 2 + 1 + 2 + 8 + 8 + 0 = 21
1114 */
1115 return 21;
1116}
1117
1118static int 1105static int
1119lowpan_fragment_xmit(struct sk_buff *skb, u8 *head, 1106lowpan_fragment_xmit(struct sk_buff *skb, u8 *head,
1120 int mlen, int plen, int offset, int type) 1107 int mlen, int plen, int offset, int type)
1121{ 1108{
1122 struct sk_buff *frag; 1109 struct sk_buff *frag;
1123 int hlen, ret; 1110 int hlen;
1124 1111
1125 hlen = (type == LOWPAN_DISPATCH_FRAG1) ? 1112 hlen = (type == LOWPAN_DISPATCH_FRAG1) ?
1126 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE; 1113 LOWPAN_FRAG1_HEAD_SIZE : LOWPAN_FRAGN_HEAD_SIZE;
1127 1114
1128 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen); 1115 lowpan_raw_dump_inline(__func__, "6lowpan fragment header", head, hlen);
1129 1116
1130 frag = dev_alloc_skb(hlen + mlen + plen + IEEE802154_MFR_SIZE); 1117 frag = netdev_alloc_skb(skb->dev,
1118 hlen + mlen + plen + IEEE802154_MFR_SIZE);
1131 if (!frag) 1119 if (!frag)
1132 return -ENOMEM; 1120 return -ENOMEM;
1133 1121
1134 frag->priority = skb->priority; 1122 frag->priority = skb->priority;
1135 frag->dev = skb->dev;
1136 1123
1137 /* copy header, MFR and payload */ 1124 /* copy header, MFR and payload */
1138 memcpy(skb_put(frag, mlen), skb->data, mlen); 1125 skb_put(frag, mlen);
1139 memcpy(skb_put(frag, hlen), head, hlen); 1126 skb_copy_to_linear_data(frag, skb_mac_header(skb), mlen);
1127
1128 skb_put(frag, hlen);
1129 skb_copy_to_linear_data_offset(frag, mlen, head, hlen);
1140 1130
1141 if (plen) 1131 skb_put(frag, plen);
1142 skb_copy_from_linear_data_offset(skb, offset + mlen, 1132 skb_copy_to_linear_data_offset(frag, mlen + hlen,
1143 skb_put(frag, plen), plen); 1133 skb_network_header(skb) + offset, plen);
1144 1134
1145 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data, 1135 lowpan_raw_dump_table(__func__, " raw fragment dump", frag->data,
1146 frag->len); 1136 frag->len);
1147 1137
1148 ret = dev_queue_xmit(frag); 1138 return dev_queue_xmit(frag);
1149
1150 return ret;
1151} 1139}
1152 1140
1153static int 1141static int
@@ -1156,7 +1144,7 @@ lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
1156 int err, header_length, payload_length, tag, offset = 0; 1144 int err, header_length, payload_length, tag, offset = 0;
1157 u8 head[5]; 1145 u8 head[5];
1158 1146
1159 header_length = lowpan_get_mac_header_length(skb); 1147 header_length = skb->mac_len;
1160 payload_length = skb->len - header_length; 1148 payload_length = skb->len - header_length;
1161 tag = lowpan_dev_info(dev)->fragment_tag++; 1149 tag = lowpan_dev_info(dev)->fragment_tag++;
1162 1150
@@ -1181,7 +1169,7 @@ lowpan_skb_fragmentation(struct sk_buff *skb, struct net_device *dev)
1181 head[0] &= ~LOWPAN_DISPATCH_FRAG1; 1169 head[0] &= ~LOWPAN_DISPATCH_FRAG1;
1182 head[0] |= LOWPAN_DISPATCH_FRAGN; 1170 head[0] |= LOWPAN_DISPATCH_FRAGN;
1183 1171
1184 while ((payload_length - offset > 0) && (err >= 0)) { 1172 while (payload_length - offset > 0) {
1185 int len = LOWPAN_FRAG_SIZE; 1173 int len = LOWPAN_FRAG_SIZE;
1186 1174
1187 head[4] = offset / 8; 1175 head[4] = offset / 8;
@@ -1327,8 +1315,6 @@ static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
1327 1315
1328 /* Pull off the 1-byte of 6lowpan header. */ 1316 /* Pull off the 1-byte of 6lowpan header. */
1329 skb_pull(local_skb, 1); 1317 skb_pull(local_skb, 1);
1330 skb_reset_network_header(local_skb);
1331 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
1332 1318
1333 lowpan_give_skb_to_devices(local_skb); 1319 lowpan_give_skb_to_devices(local_skb);
1334 1320
@@ -1372,8 +1358,10 @@ static int lowpan_newlink(struct net *src_net, struct net_device *dev,
1372 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 1358 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
1373 if (!real_dev) 1359 if (!real_dev)
1374 return -ENODEV; 1360 return -ENODEV;
1375 if (real_dev->type != ARPHRD_IEEE802154) 1361 if (real_dev->type != ARPHRD_IEEE802154) {
1362 dev_put(real_dev);
1376 return -EINVAL; 1363 return -EINVAL;
1364 }
1377 1365
1378 lowpan_dev_info(dev)->real_dev = real_dev; 1366 lowpan_dev_info(dev)->real_dev = real_dev;
1379 lowpan_dev_info(dev)->fragment_tag = 0; 1367 lowpan_dev_info(dev)->fragment_tag = 0;
diff --git a/net/ieee802154/dgram.c b/net/ieee802154/dgram.c
index 581a59504bd5..1865fdf5a5a5 100644
--- a/net/ieee802154/dgram.c
+++ b/net/ieee802154/dgram.c
@@ -315,9 +315,8 @@ static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk,
315 if (saddr) { 315 if (saddr) {
316 saddr->family = AF_IEEE802154; 316 saddr->family = AF_IEEE802154;
317 saddr->addr = mac_cb(skb)->sa; 317 saddr->addr = mac_cb(skb)->sa;
318 }
319 if (addr_len)
320 *addr_len = sizeof(*saddr); 318 *addr_len = sizeof(*saddr);
319 }
321 320
322 if (flags & MSG_TRUNC) 321 if (flags & MSG_TRUNC)
323 copied = skb->len; 322 copied = skb->len;
diff --git a/net/ieee802154/ieee802154.h b/net/ieee802154/ieee802154.h
index aadec428e6ec..cee4425b9956 100644
--- a/net/ieee802154/ieee802154.h
+++ b/net/ieee802154/ieee802154.h
@@ -47,7 +47,24 @@ struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
47int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info); 47int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info);
48 48
49extern struct genl_family nl802154_family; 49extern struct genl_family nl802154_family;
50int nl802154_mac_register(void); 50
51int nl802154_phy_register(void); 51/* genetlink ops/groups */
52int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info);
53int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb);
54int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info);
55int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info);
56
57enum ieee802154_mcgrp_ids {
58 IEEE802154_COORD_MCGRP,
59 IEEE802154_BEACON_MCGRP,
60};
61
62int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info);
63int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info);
64int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info);
65int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info);
66int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info);
67int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info);
68int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb);
52 69
53#endif 70#endif
diff --git a/net/ieee802154/netlink.c b/net/ieee802154/netlink.c
index 7e49bbcc6967..43f1b2bf469f 100644
--- a/net/ieee802154/netlink.c
+++ b/net/ieee802154/netlink.c
@@ -70,7 +70,7 @@ int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
70 if (genlmsg_end(msg, hdr) < 0) 70 if (genlmsg_end(msg, hdr) < 0)
71 goto out; 71 goto out;
72 72
73 return genlmsg_multicast(msg, 0, group, GFP_ATOMIC); 73 return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
74out: 74out:
75 nlmsg_free(msg); 75 nlmsg_free(msg);
76 return -ENOBUFS; 76 return -ENOBUFS;
@@ -109,31 +109,36 @@ out:
109 return -ENOBUFS; 109 return -ENOBUFS;
110} 110}
111 111
112int __init ieee802154_nl_init(void) 112static const struct genl_ops ieee8021154_ops[] = {
113{ 113 /* see nl-phy.c */
114 int rc; 114 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
115 115 ieee802154_dump_phy),
116 rc = genl_register_family(&nl802154_family); 116 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
117 if (rc) 117 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
118 goto fail; 118 /* see nl-mac.c */
119 119 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
120 rc = nl802154_mac_register(); 120 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
121 if (rc) 121 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
122 goto fail; 122 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
123 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
124 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
125 ieee802154_dump_iface),
126};
123 127
124 rc = nl802154_phy_register(); 128static const struct genl_multicast_group ieee802154_mcgrps[] = {
125 if (rc) 129 [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
126 goto fail; 130 [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
131};
127 132
128 return 0;
129 133
130fail: 134int __init ieee802154_nl_init(void)
131 genl_unregister_family(&nl802154_family); 135{
132 return rc; 136 return genl_register_family_with_ops_groups(&nl802154_family,
137 ieee8021154_ops,
138 ieee802154_mcgrps);
133} 139}
134 140
135void __exit ieee802154_nl_exit(void) 141void __exit ieee802154_nl_exit(void)
136{ 142{
137 genl_unregister_family(&nl802154_family); 143 genl_unregister_family(&nl802154_family);
138} 144}
139
diff --git a/net/ieee802154/nl-mac.c b/net/ieee802154/nl-mac.c
index b0bdd8c51e9c..ba5c1e002f37 100644
--- a/net/ieee802154/nl-mac.c
+++ b/net/ieee802154/nl-mac.c
@@ -39,14 +39,6 @@
39 39
40#include "ieee802154.h" 40#include "ieee802154.h"
41 41
42static struct genl_multicast_group ieee802154_coord_mcgrp = {
43 .name = IEEE802154_MCAST_COORD_NAME,
44};
45
46static struct genl_multicast_group ieee802154_beacon_mcgrp = {
47 .name = IEEE802154_MCAST_BEACON_NAME,
48};
49
50int ieee802154_nl_assoc_indic(struct net_device *dev, 42int ieee802154_nl_assoc_indic(struct net_device *dev,
51 struct ieee802154_addr *addr, u8 cap) 43 struct ieee802154_addr *addr, u8 cap)
52{ 44{
@@ -72,7 +64,7 @@ int ieee802154_nl_assoc_indic(struct net_device *dev,
72 nla_put_u8(msg, IEEE802154_ATTR_CAPABILITY, cap)) 64 nla_put_u8(msg, IEEE802154_ATTR_CAPABILITY, cap))
73 goto nla_put_failure; 65 goto nla_put_failure;
74 66
75 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 67 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
76 68
77nla_put_failure: 69nla_put_failure:
78 nlmsg_free(msg); 70 nlmsg_free(msg);
@@ -98,7 +90,7 @@ int ieee802154_nl_assoc_confirm(struct net_device *dev, u16 short_addr,
98 nla_put_u16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) || 90 nla_put_u16(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) ||
99 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) 91 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
100 goto nla_put_failure; 92 goto nla_put_failure;
101 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 93 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
102 94
103nla_put_failure: 95nla_put_failure:
104 nlmsg_free(msg); 96 nlmsg_free(msg);
@@ -133,7 +125,7 @@ int ieee802154_nl_disassoc_indic(struct net_device *dev,
133 } 125 }
134 if (nla_put_u8(msg, IEEE802154_ATTR_REASON, reason)) 126 if (nla_put_u8(msg, IEEE802154_ATTR_REASON, reason))
135 goto nla_put_failure; 127 goto nla_put_failure;
136 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 128 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
137 129
138nla_put_failure: 130nla_put_failure:
139 nlmsg_free(msg); 131 nlmsg_free(msg);
@@ -157,7 +149,7 @@ int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status)
157 dev->dev_addr) || 149 dev->dev_addr) ||
158 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) 150 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
159 goto nla_put_failure; 151 goto nla_put_failure;
160 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 152 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
161 153
162nla_put_failure: 154nla_put_failure:
163 nlmsg_free(msg); 155 nlmsg_free(msg);
@@ -183,7 +175,7 @@ int ieee802154_nl_beacon_indic(struct net_device *dev,
183 nla_put_u16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr) || 175 nla_put_u16(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, coord_addr) ||
184 nla_put_u16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid)) 176 nla_put_u16(msg, IEEE802154_ATTR_COORD_PAN_ID, panid))
185 goto nla_put_failure; 177 goto nla_put_failure;
186 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 178 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
187 179
188nla_put_failure: 180nla_put_failure:
189 nlmsg_free(msg); 181 nlmsg_free(msg);
@@ -214,7 +206,7 @@ int ieee802154_nl_scan_confirm(struct net_device *dev,
214 (edl && 206 (edl &&
215 nla_put(msg, IEEE802154_ATTR_ED_LIST, 27, edl))) 207 nla_put(msg, IEEE802154_ATTR_ED_LIST, 27, edl)))
216 goto nla_put_failure; 208 goto nla_put_failure;
217 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 209 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
218 210
219nla_put_failure: 211nla_put_failure:
220 nlmsg_free(msg); 212 nlmsg_free(msg);
@@ -238,7 +230,7 @@ int ieee802154_nl_start_confirm(struct net_device *dev, u8 status)
238 dev->dev_addr) || 230 dev->dev_addr) ||
239 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) 231 nla_put_u8(msg, IEEE802154_ATTR_STATUS, status))
240 goto nla_put_failure; 232 goto nla_put_failure;
241 return ieee802154_nl_mcast(msg, ieee802154_coord_mcgrp.id); 233 return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP);
242 234
243nla_put_failure: 235nla_put_failure:
244 nlmsg_free(msg); 236 nlmsg_free(msg);
@@ -309,8 +301,7 @@ static struct net_device *ieee802154_nl_get_dev(struct genl_info *info)
309 return dev; 301 return dev;
310} 302}
311 303
312static int ieee802154_associate_req(struct sk_buff *skb, 304int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info)
313 struct genl_info *info)
314{ 305{
315 struct net_device *dev; 306 struct net_device *dev;
316 struct ieee802154_addr addr; 307 struct ieee802154_addr addr;
@@ -357,8 +348,7 @@ out:
357 return ret; 348 return ret;
358} 349}
359 350
360static int ieee802154_associate_resp(struct sk_buff *skb, 351int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info)
361 struct genl_info *info)
362{ 352{
363 struct net_device *dev; 353 struct net_device *dev;
364 struct ieee802154_addr addr; 354 struct ieee802154_addr addr;
@@ -390,8 +380,7 @@ out:
390 return ret; 380 return ret;
391} 381}
392 382
393static int ieee802154_disassociate_req(struct sk_buff *skb, 383int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info)
394 struct genl_info *info)
395{ 384{
396 struct net_device *dev; 385 struct net_device *dev;
397 struct ieee802154_addr addr; 386 struct ieee802154_addr addr;
@@ -433,7 +422,7 @@ out:
433 * PAN_coordinator, battery_life_extension = 0, 422 * PAN_coordinator, battery_life_extension = 0,
434 * coord_realignment = 0, security_enable = 0 423 * coord_realignment = 0, security_enable = 0
435*/ 424*/
436static int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info) 425int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info)
437{ 426{
438 struct net_device *dev; 427 struct net_device *dev;
439 struct ieee802154_addr addr; 428 struct ieee802154_addr addr;
@@ -492,7 +481,7 @@ out:
492 return ret; 481 return ret;
493} 482}
494 483
495static int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info) 484int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info)
496{ 485{
497 struct net_device *dev; 486 struct net_device *dev;
498 int ret = -EOPNOTSUPP; 487 int ret = -EOPNOTSUPP;
@@ -530,8 +519,7 @@ out:
530 return ret; 519 return ret;
531} 520}
532 521
533static int ieee802154_list_iface(struct sk_buff *skb, 522int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info)
534 struct genl_info *info)
535{ 523{
536 /* Request for interface name, index, type, IEEE address, 524 /* Request for interface name, index, type, IEEE address,
537 PAN Id, short address */ 525 PAN Id, short address */
@@ -565,8 +553,7 @@ out_dev:
565 553
566} 554}
567 555
568static int ieee802154_dump_iface(struct sk_buff *skb, 556int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb)
569 struct netlink_callback *cb)
570{ 557{
571 struct net *net = sock_net(skb->sk); 558 struct net *net = sock_net(skb->sk);
572 struct net_device *dev; 559 struct net_device *dev;
@@ -590,41 +577,3 @@ cont:
590 577
591 return skb->len; 578 return skb->len;
592} 579}
593
594static struct genl_ops ieee802154_coordinator_ops[] = {
595 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
596 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
597 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
598 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
599 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
600 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
601 ieee802154_dump_iface),
602};
603
604/*
605 * No need to unregister as family unregistration will do it.
606 */
607int nl802154_mac_register(void)
608{
609 int i;
610 int rc;
611
612 rc = genl_register_mc_group(&nl802154_family,
613 &ieee802154_coord_mcgrp);
614 if (rc)
615 return rc;
616
617 rc = genl_register_mc_group(&nl802154_family,
618 &ieee802154_beacon_mcgrp);
619 if (rc)
620 return rc;
621
622 for (i = 0; i < ARRAY_SIZE(ieee802154_coordinator_ops); i++) {
623 rc = genl_register_ops(&nl802154_family,
624 &ieee802154_coordinator_ops[i]);
625 if (rc)
626 return rc;
627 }
628
629 return 0;
630}
diff --git a/net/ieee802154/nl-phy.c b/net/ieee802154/nl-phy.c
index 22b1a7058fd3..89b265aea151 100644
--- a/net/ieee802154/nl-phy.c
+++ b/net/ieee802154/nl-phy.c
@@ -77,8 +77,7 @@ out:
77 return -EMSGSIZE; 77 return -EMSGSIZE;
78} 78}
79 79
80static int ieee802154_list_phy(struct sk_buff *skb, 80int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
81 struct genl_info *info)
82{ 81{
83 /* Request for interface name, index, type, IEEE address, 82 /* Request for interface name, index, type, IEEE address,
84 PAN Id, short address */ 83 PAN Id, short address */
@@ -151,8 +150,7 @@ static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
151 return 0; 150 return 0;
152} 151}
153 152
154static int ieee802154_dump_phy(struct sk_buff *skb, 153int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
155 struct netlink_callback *cb)
156{ 154{
157 struct dump_phy_data data = { 155 struct dump_phy_data data = {
158 .cb = cb, 156 .cb = cb,
@@ -170,8 +168,7 @@ static int ieee802154_dump_phy(struct sk_buff *skb,
170 return skb->len; 168 return skb->len;
171} 169}
172 170
173static int ieee802154_add_iface(struct sk_buff *skb, 171int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
174 struct genl_info *info)
175{ 172{
176 struct sk_buff *msg; 173 struct sk_buff *msg;
177 struct wpan_phy *phy; 174 struct wpan_phy *phy;
@@ -224,8 +221,10 @@ static int ieee802154_add_iface(struct sk_buff *skb,
224 221
225 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) { 222 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
226 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]); 223 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
227 if (type >= __IEEE802154_DEV_MAX) 224 if (type >= __IEEE802154_DEV_MAX) {
228 return -EINVAL; 225 rc = -EINVAL;
226 goto nla_put_failure;
227 }
229 } 228 }
230 229
231 dev = phy->add_iface(phy, devname, type); 230 dev = phy->add_iface(phy, devname, type);
@@ -273,8 +272,7 @@ out_dev:
273 return rc; 272 return rc;
274} 273}
275 274
276static int ieee802154_del_iface(struct sk_buff *skb, 275int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
277 struct genl_info *info)
278{ 276{
279 struct sk_buff *msg; 277 struct sk_buff *msg;
280 struct wpan_phy *phy; 278 struct wpan_phy *phy;
@@ -356,28 +354,3 @@ out_dev:
356 354
357 return rc; 355 return rc;
358} 356}
359
360static struct genl_ops ieee802154_phy_ops[] = {
361 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
362 ieee802154_dump_phy),
363 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
364 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
365};
366
367/*
368 * No need to unregister as family unregistration will do it.
369 */
370int nl802154_phy_register(void)
371{
372 int i;
373 int rc;
374
375 for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) {
376 rc = genl_register_ops(&nl802154_family,
377 &ieee802154_phy_ops[i]);
378 if (rc)
379 return rc;
380 }
381
382 return 0;
383}
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index cfeb85cff4f0..70011e029ac1 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -245,29 +245,6 @@ out:
245} 245}
246EXPORT_SYMBOL(inet_listen); 246EXPORT_SYMBOL(inet_listen);
247 247
248u32 inet_ehash_secret __read_mostly;
249EXPORT_SYMBOL(inet_ehash_secret);
250
251u32 ipv6_hash_secret __read_mostly;
252EXPORT_SYMBOL(ipv6_hash_secret);
253
254/*
255 * inet_ehash_secret must be set exactly once, and to a non nul value
256 * ipv6_hash_secret must be set exactly once.
257 */
258void build_ehash_secret(void)
259{
260 u32 rnd;
261
262 do {
263 get_random_bytes(&rnd, sizeof(rnd));
264 } while (rnd == 0);
265
266 if (cmpxchg(&inet_ehash_secret, 0, rnd) == 0)
267 get_random_bytes(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
268}
269EXPORT_SYMBOL(build_ehash_secret);
270
271/* 248/*
272 * Create an inet socket. 249 * Create an inet socket.
273 */ 250 */
@@ -284,10 +261,6 @@ static int inet_create(struct net *net, struct socket *sock, int protocol,
284 int try_loading_module = 0; 261 int try_loading_module = 0;
285 int err; 262 int err;
286 263
287 if (unlikely(!inet_ehash_secret))
288 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
289 build_ehash_secret();
290
291 sock->state = SS_UNCONNECTED; 264 sock->state = SS_UNCONNECTED;
292 265
293 /* Look for the requested type/protocol pair. */ 266 /* Look for the requested type/protocol pair. */
@@ -1254,36 +1227,36 @@ static int inet_gso_send_check(struct sk_buff *skb)
1254 if (ihl < sizeof(*iph)) 1227 if (ihl < sizeof(*iph))
1255 goto out; 1228 goto out;
1256 1229
1230 proto = iph->protocol;
1231
1232 /* Warning: after this point, iph might be no longer valid */
1257 if (unlikely(!pskb_may_pull(skb, ihl))) 1233 if (unlikely(!pskb_may_pull(skb, ihl)))
1258 goto out; 1234 goto out;
1259
1260 __skb_pull(skb, ihl); 1235 __skb_pull(skb, ihl);
1236
1261 skb_reset_transport_header(skb); 1237 skb_reset_transport_header(skb);
1262 iph = ip_hdr(skb);
1263 proto = iph->protocol;
1264 err = -EPROTONOSUPPORT; 1238 err = -EPROTONOSUPPORT;
1265 1239
1266 rcu_read_lock();
1267 ops = rcu_dereference(inet_offloads[proto]); 1240 ops = rcu_dereference(inet_offloads[proto]);
1268 if (likely(ops && ops->callbacks.gso_send_check)) 1241 if (likely(ops && ops->callbacks.gso_send_check))
1269 err = ops->callbacks.gso_send_check(skb); 1242 err = ops->callbacks.gso_send_check(skb);
1270 rcu_read_unlock();
1271 1243
1272out: 1244out:
1273 return err; 1245 return err;
1274} 1246}
1275 1247
1276static struct sk_buff *inet_gso_segment(struct sk_buff *skb, 1248static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1277 netdev_features_t features) 1249 netdev_features_t features)
1278{ 1250{
1279 struct sk_buff *segs = ERR_PTR(-EINVAL); 1251 struct sk_buff *segs = ERR_PTR(-EINVAL);
1280 const struct net_offload *ops; 1252 const struct net_offload *ops;
1253 unsigned int offset = 0;
1254 bool udpfrag, encap;
1281 struct iphdr *iph; 1255 struct iphdr *iph;
1282 int proto; 1256 int proto;
1257 int nhoff;
1283 int ihl; 1258 int ihl;
1284 int id; 1259 int id;
1285 unsigned int offset = 0;
1286 bool tunnel;
1287 1260
1288 if (unlikely(skb_shinfo(skb)->gso_type & 1261 if (unlikely(skb_shinfo(skb)->gso_type &
1289 ~(SKB_GSO_TCPV4 | 1262 ~(SKB_GSO_TCPV4 |
@@ -1291,12 +1264,16 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1291 SKB_GSO_DODGY | 1264 SKB_GSO_DODGY |
1292 SKB_GSO_TCP_ECN | 1265 SKB_GSO_TCP_ECN |
1293 SKB_GSO_GRE | 1266 SKB_GSO_GRE |
1267 SKB_GSO_IPIP |
1268 SKB_GSO_SIT |
1294 SKB_GSO_TCPV6 | 1269 SKB_GSO_TCPV6 |
1295 SKB_GSO_UDP_TUNNEL | 1270 SKB_GSO_UDP_TUNNEL |
1296 SKB_GSO_MPLS | 1271 SKB_GSO_MPLS |
1297 0))) 1272 0)))
1298 goto out; 1273 goto out;
1299 1274
1275 skb_reset_network_header(skb);
1276 nhoff = skb_network_header(skb) - skb_mac_header(skb);
1300 if (unlikely(!pskb_may_pull(skb, sizeof(*iph)))) 1277 if (unlikely(!pskb_may_pull(skb, sizeof(*iph))))
1301 goto out; 1278 goto out;
1302 1279
@@ -1305,42 +1282,50 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1305 if (ihl < sizeof(*iph)) 1282 if (ihl < sizeof(*iph))
1306 goto out; 1283 goto out;
1307 1284
1285 id = ntohs(iph->id);
1286 proto = iph->protocol;
1287
1288 /* Warning: after this point, iph might be no longer valid */
1308 if (unlikely(!pskb_may_pull(skb, ihl))) 1289 if (unlikely(!pskb_may_pull(skb, ihl)))
1309 goto out; 1290 goto out;
1291 __skb_pull(skb, ihl);
1310 1292
1311 tunnel = !!skb->encapsulation; 1293 encap = SKB_GSO_CB(skb)->encap_level > 0;
1294 if (encap)
1295 features = skb->dev->hw_enc_features & netif_skb_features(skb);
1296 SKB_GSO_CB(skb)->encap_level += ihl;
1312 1297
1313 __skb_pull(skb, ihl);
1314 skb_reset_transport_header(skb); 1298 skb_reset_transport_header(skb);
1315 iph = ip_hdr(skb); 1299
1316 id = ntohs(iph->id);
1317 proto = iph->protocol;
1318 segs = ERR_PTR(-EPROTONOSUPPORT); 1300 segs = ERR_PTR(-EPROTONOSUPPORT);
1319 1301
1320 rcu_read_lock(); 1302 /* Note : following gso_segment() might change skb->encapsulation */
1303 udpfrag = !skb->encapsulation && proto == IPPROTO_UDP;
1304
1321 ops = rcu_dereference(inet_offloads[proto]); 1305 ops = rcu_dereference(inet_offloads[proto]);
1322 if (likely(ops && ops->callbacks.gso_segment)) 1306 if (likely(ops && ops->callbacks.gso_segment))
1323 segs = ops->callbacks.gso_segment(skb, features); 1307 segs = ops->callbacks.gso_segment(skb, features);
1324 rcu_read_unlock();
1325 1308
1326 if (IS_ERR_OR_NULL(segs)) 1309 if (IS_ERR_OR_NULL(segs))
1327 goto out; 1310 goto out;
1328 1311
1329 skb = segs; 1312 skb = segs;
1330 do { 1313 do {
1331 iph = ip_hdr(skb); 1314 iph = (struct iphdr *)(skb_mac_header(skb) + nhoff);
1332 if (!tunnel && proto == IPPROTO_UDP) { 1315 if (udpfrag) {
1333 iph->id = htons(id); 1316 iph->id = htons(id);
1334 iph->frag_off = htons(offset >> 3); 1317 iph->frag_off = htons(offset >> 3);
1335 if (skb->next != NULL) 1318 if (skb->next != NULL)
1336 iph->frag_off |= htons(IP_MF); 1319 iph->frag_off |= htons(IP_MF);
1337 offset += (skb->len - skb->mac_len - iph->ihl * 4); 1320 offset += skb->len - nhoff - ihl;
1338 } else { 1321 } else {
1339 iph->id = htons(id++); 1322 iph->id = htons(id++);
1340 } 1323 }
1341 iph->tot_len = htons(skb->len - skb->mac_len); 1324 iph->tot_len = htons(skb->len - nhoff);
1342 iph->check = 0; 1325 ip_send_check(iph);
1343 iph->check = ip_fast_csum(skb_network_header(skb), iph->ihl); 1326 if (encap)
1327 skb_reset_inner_headers(skb);
1328 skb->network_header = (u8 *)iph - skb->head;
1344 } while ((skb = skb->next)); 1329 } while ((skb = skb->next));
1345 1330
1346out: 1331out:
@@ -1518,6 +1503,7 @@ int snmp_mib_init(void __percpu *ptr[2], size_t mibsize, size_t align)
1518 ptr[0] = __alloc_percpu(mibsize, align); 1503 ptr[0] = __alloc_percpu(mibsize, align);
1519 if (!ptr[0]) 1504 if (!ptr[0])
1520 return -ENOMEM; 1505 return -ENOMEM;
1506
1521#if SNMP_ARRAY_SZ == 2 1507#if SNMP_ARRAY_SZ == 2
1522 ptr[1] = __alloc_percpu(mibsize, align); 1508 ptr[1] = __alloc_percpu(mibsize, align);
1523 if (!ptr[1]) { 1509 if (!ptr[1]) {
@@ -1546,6 +1532,7 @@ static const struct net_protocol tcp_protocol = {
1546}; 1532};
1547 1533
1548static const struct net_protocol udp_protocol = { 1534static const struct net_protocol udp_protocol = {
1535 .early_demux = udp_v4_early_demux,
1549 .handler = udp_rcv, 1536 .handler = udp_rcv,
1550 .err_handler = udp_err, 1537 .err_handler = udp_err,
1551 .no_policy = 1, 1538 .no_policy = 1,
@@ -1561,6 +1548,8 @@ static const struct net_protocol icmp_protocol = {
1561 1548
1562static __net_init int ipv4_mib_init_net(struct net *net) 1549static __net_init int ipv4_mib_init_net(struct net *net)
1563{ 1550{
1551 int i;
1552
1564 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics, 1553 if (snmp_mib_init((void __percpu **)net->mib.tcp_statistics,
1565 sizeof(struct tcp_mib), 1554 sizeof(struct tcp_mib),
1566 __alignof__(struct tcp_mib)) < 0) 1555 __alignof__(struct tcp_mib)) < 0)
@@ -1569,6 +1558,17 @@ static __net_init int ipv4_mib_init_net(struct net *net)
1569 sizeof(struct ipstats_mib), 1558 sizeof(struct ipstats_mib),
1570 __alignof__(struct ipstats_mib)) < 0) 1559 __alignof__(struct ipstats_mib)) < 0)
1571 goto err_ip_mib; 1560 goto err_ip_mib;
1561
1562 for_each_possible_cpu(i) {
1563 struct ipstats_mib *af_inet_stats;
1564 af_inet_stats = per_cpu_ptr(net->mib.ip_statistics[0], i);
1565 u64_stats_init(&af_inet_stats->syncp);
1566#if SNMP_ARRAY_SZ == 2
1567 af_inet_stats = per_cpu_ptr(net->mib.ip_statistics[1], i);
1568 u64_stats_init(&af_inet_stats->syncp);
1569#endif
1570 }
1571
1572 if (snmp_mib_init((void __percpu **)net->mib.net_statistics, 1572 if (snmp_mib_init((void __percpu **)net->mib.net_statistics,
1573 sizeof(struct linux_mib), 1573 sizeof(struct linux_mib),
1574 __alignof__(struct linux_mib)) < 0) 1574 __alignof__(struct linux_mib)) < 0)
@@ -1646,6 +1646,13 @@ static struct packet_offload ip_packet_offload __read_mostly = {
1646 }, 1646 },
1647}; 1647};
1648 1648
1649static const struct net_offload ipip_offload = {
1650 .callbacks = {
1651 .gso_send_check = inet_gso_send_check,
1652 .gso_segment = inet_gso_segment,
1653 },
1654};
1655
1649static int __init ipv4_offload_init(void) 1656static int __init ipv4_offload_init(void)
1650{ 1657{
1651 /* 1658 /*
@@ -1657,6 +1664,7 @@ static int __init ipv4_offload_init(void)
1657 pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 1664 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
1658 1665
1659 dev_add_offload(&ip_packet_offload); 1666 dev_add_offload(&ip_packet_offload);
1667 inet_add_offload(&ipip_offload, IPPROTO_IPIP);
1660 return 0; 1668 return 0;
1661} 1669}
1662 1670
@@ -1705,8 +1713,6 @@ static int __init inet_init(void)
1705 ip_static_sysctl_init(); 1713 ip_static_sysctl_init();
1706#endif 1714#endif
1707 1715
1708 tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1709
1710 /* 1716 /*
1711 * Add all the base protocols. 1717 * Add all the base protocols.
1712 */ 1718 */
diff --git a/net/ipv4/datagram.c b/net/ipv4/datagram.c
index b28e863fe0a7..19e36376d2a0 100644
--- a/net/ipv4/datagram.c
+++ b/net/ipv4/datagram.c
@@ -57,7 +57,7 @@ int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
57 if (IS_ERR(rt)) { 57 if (IS_ERR(rt)) {
58 err = PTR_ERR(rt); 58 err = PTR_ERR(rt);
59 if (err == -ENETUNREACH) 59 if (err == -ENETUNREACH)
60 IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); 60 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
61 goto out; 61 goto out;
62 } 62 }
63 63
diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c
index 109ee89f123e..7785b28061ac 100644
--- a/net/ipv4/esp4.c
+++ b/net/ipv4/esp4.c
@@ -121,7 +121,6 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
121 struct aead_givcrypt_request *req; 121 struct aead_givcrypt_request *req;
122 struct scatterlist *sg; 122 struct scatterlist *sg;
123 struct scatterlist *asg; 123 struct scatterlist *asg;
124 struct esp_data *esp;
125 struct sk_buff *trailer; 124 struct sk_buff *trailer;
126 void *tmp; 125 void *tmp;
127 u8 *iv; 126 u8 *iv;
@@ -139,8 +138,7 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
139 138
140 /* skb is pure payload to encrypt */ 139 /* skb is pure payload to encrypt */
141 140
142 esp = x->data; 141 aead = x->data;
143 aead = esp->aead;
144 alen = crypto_aead_authsize(aead); 142 alen = crypto_aead_authsize(aead);
145 143
146 tfclen = 0; 144 tfclen = 0;
@@ -154,8 +152,6 @@ static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
154 } 152 }
155 blksize = ALIGN(crypto_aead_blocksize(aead), 4); 153 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
156 clen = ALIGN(skb->len + 2 + tfclen, blksize); 154 clen = ALIGN(skb->len + 2 + tfclen, blksize);
157 if (esp->padlen)
158 clen = ALIGN(clen, esp->padlen);
159 plen = clen - skb->len - tfclen; 155 plen = clen - skb->len - tfclen;
160 156
161 err = skb_cow_data(skb, tfclen + plen + alen, &trailer); 157 err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
@@ -280,8 +276,7 @@ static int esp_input_done2(struct sk_buff *skb, int err)
280{ 276{
281 const struct iphdr *iph; 277 const struct iphdr *iph;
282 struct xfrm_state *x = xfrm_input_state(skb); 278 struct xfrm_state *x = xfrm_input_state(skb);
283 struct esp_data *esp = x->data; 279 struct crypto_aead *aead = x->data;
284 struct crypto_aead *aead = esp->aead;
285 int alen = crypto_aead_authsize(aead); 280 int alen = crypto_aead_authsize(aead);
286 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); 281 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
287 int elen = skb->len - hlen; 282 int elen = skb->len - hlen;
@@ -376,8 +371,7 @@ static void esp_input_done(struct crypto_async_request *base, int err)
376static int esp_input(struct xfrm_state *x, struct sk_buff *skb) 371static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
377{ 372{
378 struct ip_esp_hdr *esph; 373 struct ip_esp_hdr *esph;
379 struct esp_data *esp = x->data; 374 struct crypto_aead *aead = x->data;
380 struct crypto_aead *aead = esp->aead;
381 struct aead_request *req; 375 struct aead_request *req;
382 struct sk_buff *trailer; 376 struct sk_buff *trailer;
383 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead); 377 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
@@ -459,9 +453,8 @@ out:
459 453
460static u32 esp4_get_mtu(struct xfrm_state *x, int mtu) 454static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
461{ 455{
462 struct esp_data *esp = x->data; 456 struct crypto_aead *aead = x->data;
463 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4); 457 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
464 u32 align = max_t(u32, blksize, esp->padlen);
465 unsigned int net_adj; 458 unsigned int net_adj;
466 459
467 switch (x->props.mode) { 460 switch (x->props.mode) {
@@ -476,8 +469,8 @@ static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
476 BUG(); 469 BUG();
477 } 470 }
478 471
479 return ((mtu - x->props.header_len - crypto_aead_authsize(esp->aead) - 472 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
480 net_adj) & ~(align - 1)) + net_adj - 2; 473 net_adj) & ~(blksize - 1)) + net_adj - 2;
481} 474}
482 475
483static void esp4_err(struct sk_buff *skb, u32 info) 476static void esp4_err(struct sk_buff *skb, u32 info)
@@ -511,18 +504,16 @@ static void esp4_err(struct sk_buff *skb, u32 info)
511 504
512static void esp_destroy(struct xfrm_state *x) 505static void esp_destroy(struct xfrm_state *x)
513{ 506{
514 struct esp_data *esp = x->data; 507 struct crypto_aead *aead = x->data;
515 508
516 if (!esp) 509 if (!aead)
517 return; 510 return;
518 511
519 crypto_free_aead(esp->aead); 512 crypto_free_aead(aead);
520 kfree(esp);
521} 513}
522 514
523static int esp_init_aead(struct xfrm_state *x) 515static int esp_init_aead(struct xfrm_state *x)
524{ 516{
525 struct esp_data *esp = x->data;
526 struct crypto_aead *aead; 517 struct crypto_aead *aead;
527 int err; 518 int err;
528 519
@@ -531,7 +522,7 @@ static int esp_init_aead(struct xfrm_state *x)
531 if (IS_ERR(aead)) 522 if (IS_ERR(aead))
532 goto error; 523 goto error;
533 524
534 esp->aead = aead; 525 x->data = aead;
535 526
536 err = crypto_aead_setkey(aead, x->aead->alg_key, 527 err = crypto_aead_setkey(aead, x->aead->alg_key,
537 (x->aead->alg_key_len + 7) / 8); 528 (x->aead->alg_key_len + 7) / 8);
@@ -548,7 +539,6 @@ error:
548 539
549static int esp_init_authenc(struct xfrm_state *x) 540static int esp_init_authenc(struct xfrm_state *x)
550{ 541{
551 struct esp_data *esp = x->data;
552 struct crypto_aead *aead; 542 struct crypto_aead *aead;
553 struct crypto_authenc_key_param *param; 543 struct crypto_authenc_key_param *param;
554 struct rtattr *rta; 544 struct rtattr *rta;
@@ -583,7 +573,7 @@ static int esp_init_authenc(struct xfrm_state *x)
583 if (IS_ERR(aead)) 573 if (IS_ERR(aead))
584 goto error; 574 goto error;
585 575
586 esp->aead = aead; 576 x->data = aead;
587 577
588 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) + 578 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
589 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param)); 579 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
@@ -638,16 +628,11 @@ error:
638 628
639static int esp_init_state(struct xfrm_state *x) 629static int esp_init_state(struct xfrm_state *x)
640{ 630{
641 struct esp_data *esp;
642 struct crypto_aead *aead; 631 struct crypto_aead *aead;
643 u32 align; 632 u32 align;
644 int err; 633 int err;
645 634
646 esp = kzalloc(sizeof(*esp), GFP_KERNEL); 635 x->data = NULL;
647 if (esp == NULL)
648 return -ENOMEM;
649
650 x->data = esp;
651 636
652 if (x->aead) 637 if (x->aead)
653 err = esp_init_aead(x); 638 err = esp_init_aead(x);
@@ -657,9 +642,7 @@ static int esp_init_state(struct xfrm_state *x)
657 if (err) 642 if (err)
658 goto error; 643 goto error;
659 644
660 aead = esp->aead; 645 aead = x->data;
661
662 esp->padlen = 0;
663 646
664 x->props.header_len = sizeof(struct ip_esp_hdr) + 647 x->props.header_len = sizeof(struct ip_esp_hdr) +
665 crypto_aead_ivsize(aead); 648 crypto_aead_ivsize(aead);
@@ -683,9 +666,7 @@ static int esp_init_state(struct xfrm_state *x)
683 } 666 }
684 667
685 align = ALIGN(crypto_aead_blocksize(aead), 4); 668 align = ALIGN(crypto_aead_blocksize(aead), 4);
686 if (esp->padlen) 669 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
687 align = max_t(u32, align, esp->padlen);
688 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
689 670
690error: 671error:
691 return err; 672 return err;
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index b3f627ac4ed8..d846304b7b89 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -933,7 +933,6 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
933 local_bh_disable(); 933 local_bh_disable();
934 934
935 frn->tb_id = tb->tb_id; 935 frn->tb_id = tb->tb_id;
936 rcu_read_lock();
937 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF); 936 frn->err = fib_table_lookup(tb, &fl4, &res, FIB_LOOKUP_NOREF);
938 937
939 if (!frn->err) { 938 if (!frn->err) {
@@ -942,7 +941,6 @@ static void nl_fib_lookup(struct fib_result_nl *frn, struct fib_table *tb)
942 frn->type = res.type; 941 frn->type = res.type;
943 frn->scope = res.scope; 942 frn->scope = res.scope;
944 } 943 }
945 rcu_read_unlock();
946 local_bh_enable(); 944 local_bh_enable();
947 } 945 }
948} 946}
diff --git a/net/ipv4/fib_lookup.h b/net/ipv4/fib_lookup.h
index af0f14aba169..388d113fd289 100644
--- a/net/ipv4/fib_lookup.h
+++ b/net/ipv4/fib_lookup.h
@@ -24,21 +24,17 @@ static inline void fib_alias_accessed(struct fib_alias *fa)
24} 24}
25 25
26/* Exported by fib_semantics.c */ 26/* Exported by fib_semantics.c */
27extern void fib_release_info(struct fib_info *); 27void fib_release_info(struct fib_info *);
28extern struct fib_info *fib_create_info(struct fib_config *cfg); 28struct fib_info *fib_create_info(struct fib_config *cfg);
29extern int fib_nh_match(struct fib_config *cfg, struct fib_info *fi); 29int fib_nh_match(struct fib_config *cfg, struct fib_info *fi);
30extern int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, 30int fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event, u32 tb_id,
31 u32 tb_id, u8 type, __be32 dst, 31 u8 type, __be32 dst, int dst_len, u8 tos, struct fib_info *fi,
32 int dst_len, u8 tos, struct fib_info *fi, 32 unsigned int);
33 unsigned int); 33void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, int dst_len,
34extern void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, 34 u32 tb_id, const struct nl_info *info, unsigned int nlm_flags);
35 int dst_len, u32 tb_id, struct nl_info *info, 35struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio);
36 unsigned int nlm_flags); 36int fib_detect_death(struct fib_info *fi, int order,
37extern struct fib_alias *fib_find_alias(struct list_head *fah, 37 struct fib_info **last_resort, int *last_idx, int dflt);
38 u8 tos, u32 prio);
39extern int fib_detect_death(struct fib_info *fi, int order,
40 struct fib_info **last_resort,
41 int *last_idx, int dflt);
42 38
43static inline void fib_result_assign(struct fib_result *res, 39static inline void fib_result_assign(struct fib_result *res,
44 struct fib_info *fi) 40 struct fib_info *fi)
diff --git a/net/ipv4/fib_rules.c b/net/ipv4/fib_rules.c
index 523be38e37de..f2e15738534d 100644
--- a/net/ipv4/fib_rules.c
+++ b/net/ipv4/fib_rules.c
@@ -104,7 +104,10 @@ errout:
104static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 104static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
105{ 105{
106 struct fib_result *result = (struct fib_result *) arg->result; 106 struct fib_result *result = (struct fib_result *) arg->result;
107 struct net_device *dev = result->fi->fib_dev; 107 struct net_device *dev = NULL;
108
109 if (result->fi)
110 dev = result->fi->fib_dev;
108 111
109 /* do not accept result if the route does 112 /* do not accept result if the route does
110 * not meet the required prefix length 113 * not meet the required prefix length
diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index d5dbca5ecf62..e63f47a4e651 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -380,7 +380,7 @@ static inline size_t fib_nlmsg_size(struct fib_info *fi)
380} 380}
381 381
382void rtmsg_fib(int event, __be32 key, struct fib_alias *fa, 382void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
383 int dst_len, u32 tb_id, struct nl_info *info, 383 int dst_len, u32 tb_id, const struct nl_info *info,
384 unsigned int nlm_flags) 384 unsigned int nlm_flags)
385{ 385{
386 struct sk_buff *skb; 386 struct sk_buff *skb;
diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 3df6d3edb2a1..5afeb5aa4c7c 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -762,12 +762,9 @@ static struct tnode *inflate(struct trie *t, struct tnode *tn)
762 762
763 if (IS_LEAF(node) || ((struct tnode *) node)->pos > 763 if (IS_LEAF(node) || ((struct tnode *) node)->pos >
764 tn->pos + tn->bits - 1) { 764 tn->pos + tn->bits - 1) {
765 if (tkey_extract_bits(node->key, 765 put_child(tn,
766 oldtnode->pos + oldtnode->bits, 766 tkey_extract_bits(node->key, oldtnode->pos, oldtnode->bits + 1),
767 1) == 0) 767 node);
768 put_child(tn, 2*i, node);
769 else
770 put_child(tn, 2*i+1, node);
771 continue; 768 continue;
772 } 769 }
773 770
@@ -1120,12 +1117,8 @@ static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
1120 * first tnode need some special handling 1117 * first tnode need some special handling
1121 */ 1118 */
1122 1119
1123 if (tp)
1124 pos = tp->pos+tp->bits;
1125 else
1126 pos = 0;
1127
1128 if (n) { 1120 if (n) {
1121 pos = tp ? tp->pos+tp->bits : 0;
1129 newpos = tkey_mismatch(key, pos, n->key); 1122 newpos = tkey_mismatch(key, pos, n->key);
1130 tn = tnode_new(n->key, newpos, 1); 1123 tn = tnode_new(n->key, newpos, 1);
1131 } else { 1124 } else {
@@ -2530,16 +2523,17 @@ static int fib_route_seq_show(struct seq_file *seq, void *v)
2530 list_for_each_entry_rcu(fa, &li->falh, fa_list) { 2523 list_for_each_entry_rcu(fa, &li->falh, fa_list) {
2531 const struct fib_info *fi = fa->fa_info; 2524 const struct fib_info *fi = fa->fa_info;
2532 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi); 2525 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
2533 int len;
2534 2526
2535 if (fa->fa_type == RTN_BROADCAST 2527 if (fa->fa_type == RTN_BROADCAST
2536 || fa->fa_type == RTN_MULTICAST) 2528 || fa->fa_type == RTN_MULTICAST)
2537 continue; 2529 continue;
2538 2530
2531 seq_setwidth(seq, 127);
2532
2539 if (fi) 2533 if (fi)
2540 seq_printf(seq, 2534 seq_printf(seq,
2541 "%s\t%08X\t%08X\t%04X\t%d\t%u\t" 2535 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2542 "%d\t%08X\t%d\t%u\t%u%n", 2536 "%d\t%08X\t%d\t%u\t%u",
2543 fi->fib_dev ? fi->fib_dev->name : "*", 2537 fi->fib_dev ? fi->fib_dev->name : "*",
2544 prefix, 2538 prefix,
2545 fi->fib_nh->nh_gw, flags, 0, 0, 2539 fi->fib_nh->nh_gw, flags, 0, 0,
@@ -2548,15 +2542,15 @@ static int fib_route_seq_show(struct seq_file *seq, void *v)
2548 (fi->fib_advmss ? 2542 (fi->fib_advmss ?
2549 fi->fib_advmss + 40 : 0), 2543 fi->fib_advmss + 40 : 0),
2550 fi->fib_window, 2544 fi->fib_window,
2551 fi->fib_rtt >> 3, &len); 2545 fi->fib_rtt >> 3);
2552 else 2546 else
2553 seq_printf(seq, 2547 seq_printf(seq,
2554 "*\t%08X\t%08X\t%04X\t%d\t%u\t" 2548 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2555 "%d\t%08X\t%d\t%u\t%u%n", 2549 "%d\t%08X\t%d\t%u\t%u",
2556 prefix, 0, flags, 0, 0, 0, 2550 prefix, 0, flags, 0, 0, 0,
2557 mask, 0, 0, 0, &len); 2551 mask, 0, 0, 0);
2558 2552
2559 seq_printf(seq, "%*s\n", 127 - len, ""); 2553 seq_pad(seq, '\n');
2560 } 2554 }
2561 } 2555 }
2562 2556
diff --git a/net/ipv4/gre_demux.c b/net/ipv4/gre_demux.c
index 736c9fc3ef93..5893e99e8299 100644
--- a/net/ipv4/gre_demux.c
+++ b/net/ipv4/gre_demux.c
@@ -93,35 +93,6 @@ void gre_build_header(struct sk_buff *skb, const struct tnl_ptk_info *tpi,
93} 93}
94EXPORT_SYMBOL_GPL(gre_build_header); 94EXPORT_SYMBOL_GPL(gre_build_header);
95 95
96struct sk_buff *gre_handle_offloads(struct sk_buff *skb, bool gre_csum)
97{
98 int err;
99
100 if (likely(!skb->encapsulation)) {
101 skb_reset_inner_headers(skb);
102 skb->encapsulation = 1;
103 }
104
105 if (skb_is_gso(skb)) {
106 err = skb_unclone(skb, GFP_ATOMIC);
107 if (unlikely(err))
108 goto error;
109 skb_shinfo(skb)->gso_type |= SKB_GSO_GRE;
110 return skb;
111 } else if (skb->ip_summed == CHECKSUM_PARTIAL && gre_csum) {
112 err = skb_checksum_help(skb);
113 if (unlikely(err))
114 goto error;
115 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
116 skb->ip_summed = CHECKSUM_NONE;
117
118 return skb;
119error:
120 kfree_skb(skb);
121 return ERR_PTR(err);
122}
123EXPORT_SYMBOL_GPL(gre_handle_offloads);
124
125static __sum16 check_checksum(struct sk_buff *skb) 96static __sum16 check_checksum(struct sk_buff *skb)
126{ 97{
127 __sum16 csum = 0; 98 __sum16 csum = 0;
diff --git a/net/ipv4/gre_offload.c b/net/ipv4/gre_offload.c
index 55e6bfb3a289..2cd02f32f99f 100644
--- a/net/ipv4/gre_offload.c
+++ b/net/ipv4/gre_offload.c
@@ -28,6 +28,7 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
28 netdev_features_t enc_features; 28 netdev_features_t enc_features;
29 int ghl = GRE_HEADER_SECTION; 29 int ghl = GRE_HEADER_SECTION;
30 struct gre_base_hdr *greh; 30 struct gre_base_hdr *greh;
31 u16 mac_offset = skb->mac_header;
31 int mac_len = skb->mac_len; 32 int mac_len = skb->mac_len;
32 __be16 protocol = skb->protocol; 33 __be16 protocol = skb->protocol;
33 int tnl_hlen; 34 int tnl_hlen;
@@ -39,7 +40,8 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
39 SKB_GSO_UDP | 40 SKB_GSO_UDP |
40 SKB_GSO_DODGY | 41 SKB_GSO_DODGY |
41 SKB_GSO_TCP_ECN | 42 SKB_GSO_TCP_ECN |
42 SKB_GSO_GRE))) 43 SKB_GSO_GRE |
44 SKB_GSO_IPIP)))
43 goto out; 45 goto out;
44 46
45 if (unlikely(!pskb_may_pull(skb, sizeof(*greh)))) 47 if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
@@ -57,13 +59,13 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
57 } else 59 } else
58 csum = false; 60 csum = false;
59 61
62 if (unlikely(!pskb_may_pull(skb, ghl)))
63 goto out;
64
60 /* setup inner skb. */ 65 /* setup inner skb. */
61 skb->protocol = greh->protocol; 66 skb->protocol = greh->protocol;
62 skb->encapsulation = 0; 67 skb->encapsulation = 0;
63 68
64 if (unlikely(!pskb_may_pull(skb, ghl)))
65 goto out;
66
67 __skb_pull(skb, ghl); 69 __skb_pull(skb, ghl);
68 skb_reset_mac_header(skb); 70 skb_reset_mac_header(skb);
69 skb_set_network_header(skb, skb_inner_network_offset(skb)); 71 skb_set_network_header(skb, skb_inner_network_offset(skb));
@@ -72,8 +74,10 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
72 /* segment inner packet. */ 74 /* segment inner packet. */
73 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb); 75 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
74 segs = skb_mac_gso_segment(skb, enc_features); 76 segs = skb_mac_gso_segment(skb, enc_features);
75 if (!segs || IS_ERR(segs)) 77 if (!segs || IS_ERR(segs)) {
78 skb_gso_error_unwind(skb, protocol, ghl, mac_offset, mac_len);
76 goto out; 79 goto out;
80 }
77 81
78 skb = segs; 82 skb = segs;
79 tnl_hlen = skb_tnl_header_len(skb); 83 tnl_hlen = skb_tnl_header_len(skb);
diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
index 5f7d11a45871..5c0e8bc6e5ba 100644
--- a/net/ipv4/icmp.c
+++ b/net/ipv4/icmp.c
@@ -353,6 +353,9 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
353 saddr = fib_compute_spec_dst(skb); 353 saddr = fib_compute_spec_dst(skb);
354 ipc.opt = NULL; 354 ipc.opt = NULL;
355 ipc.tx_flags = 0; 355 ipc.tx_flags = 0;
356 ipc.ttl = 0;
357 ipc.tos = -1;
358
356 if (icmp_param->replyopts.opt.opt.optlen) { 359 if (icmp_param->replyopts.opt.opt.optlen) {
357 ipc.opt = &icmp_param->replyopts.opt; 360 ipc.opt = &icmp_param->replyopts.opt;
358 if (ipc.opt->opt.srr) 361 if (ipc.opt->opt.srr)
@@ -608,6 +611,8 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
608 ipc.addr = iph->saddr; 611 ipc.addr = iph->saddr;
609 ipc.opt = &icmp_param->replyopts.opt; 612 ipc.opt = &icmp_param->replyopts.opt;
610 ipc.tx_flags = 0; 613 ipc.tx_flags = 0;
614 ipc.ttl = 0;
615 ipc.tos = -1;
611 616
612 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos, 617 rt = icmp_route_lookup(net, &fl4, skb_in, iph, saddr, tos,
613 type, code, icmp_param); 618 type, code, icmp_param);
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 6acb541c9091..fc0e649cc002 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -29,27 +29,19 @@ const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
29EXPORT_SYMBOL(inet_csk_timer_bug_msg); 29EXPORT_SYMBOL(inet_csk_timer_bug_msg);
30#endif 30#endif
31 31
32/*
33 * This struct holds the first and last local port number.
34 */
35struct local_ports sysctl_local_ports __read_mostly = {
36 .lock = __SEQLOCK_UNLOCKED(sysctl_local_ports.lock),
37 .range = { 32768, 61000 },
38};
39
40unsigned long *sysctl_local_reserved_ports; 32unsigned long *sysctl_local_reserved_ports;
41EXPORT_SYMBOL(sysctl_local_reserved_ports); 33EXPORT_SYMBOL(sysctl_local_reserved_ports);
42 34
43void inet_get_local_port_range(int *low, int *high) 35void inet_get_local_port_range(struct net *net, int *low, int *high)
44{ 36{
45 unsigned int seq; 37 unsigned int seq;
46 38
47 do { 39 do {
48 seq = read_seqbegin(&sysctl_local_ports.lock); 40 seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock);
49 41
50 *low = sysctl_local_ports.range[0]; 42 *low = net->ipv4.sysctl_local_ports.range[0];
51 *high = sysctl_local_ports.range[1]; 43 *high = net->ipv4.sysctl_local_ports.range[1];
52 } while (read_seqretry(&sysctl_local_ports.lock, seq)); 44 } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq));
53} 45}
54EXPORT_SYMBOL(inet_get_local_port_range); 46EXPORT_SYMBOL(inet_get_local_port_range);
55 47
@@ -79,17 +71,16 @@ int inet_csk_bind_conflict(const struct sock *sk,
79 (!reuseport || !sk2->sk_reuseport || 71 (!reuseport || !sk2->sk_reuseport ||
80 (sk2->sk_state != TCP_TIME_WAIT && 72 (sk2->sk_state != TCP_TIME_WAIT &&
81 !uid_eq(uid, sock_i_uid(sk2))))) { 73 !uid_eq(uid, sock_i_uid(sk2))))) {
82 const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2); 74
83 if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) || 75 if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr ||
84 sk2_rcv_saddr == sk_rcv_saddr(sk)) 76 sk2->sk_rcv_saddr == sk->sk_rcv_saddr)
85 break; 77 break;
86 } 78 }
87 if (!relax && reuse && sk2->sk_reuse && 79 if (!relax && reuse && sk2->sk_reuse &&
88 sk2->sk_state != TCP_LISTEN) { 80 sk2->sk_state != TCP_LISTEN) {
89 const __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
90 81
91 if (!sk2_rcv_saddr || !sk_rcv_saddr(sk) || 82 if (!sk2->sk_rcv_saddr || !sk->sk_rcv_saddr ||
92 sk2_rcv_saddr == sk_rcv_saddr(sk)) 83 sk2->sk_rcv_saddr == sk->sk_rcv_saddr)
93 break; 84 break;
94 } 85 }
95 } 86 }
@@ -116,7 +107,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
116 int remaining, rover, low, high; 107 int remaining, rover, low, high;
117 108
118again: 109again:
119 inet_get_local_port_range(&low, &high); 110 inet_get_local_port_range(net, &low, &high);
120 remaining = (high - low) + 1; 111 remaining = (high - low) + 1;
121 smallest_rover = rover = net_random() % remaining + low; 112 smallest_rover = rover = net_random() % remaining + low;
122 113
@@ -421,8 +412,8 @@ struct dst_entry *inet_csk_route_req(struct sock *sk,
421 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 412 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
422 sk->sk_protocol, 413 sk->sk_protocol,
423 flags, 414 flags,
424 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr, 415 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
425 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport); 416 ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport);
426 security_req_classify_flow(req, flowi4_to_flowi(fl4)); 417 security_req_classify_flow(req, flowi4_to_flowi(fl4));
427 rt = ip_route_output_flow(net, fl4, sk); 418 rt = ip_route_output_flow(net, fl4, sk);
428 if (IS_ERR(rt)) 419 if (IS_ERR(rt))
@@ -457,8 +448,8 @@ struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
457 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark, 448 flowi4_init_output(fl4, sk->sk_bound_dev_if, sk->sk_mark,
458 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 449 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
459 sk->sk_protocol, inet_sk_flowi_flags(sk), 450 sk->sk_protocol, inet_sk_flowi_flags(sk),
460 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->rmt_addr, 451 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
461 ireq->loc_addr, ireq->rmt_port, inet_sk(sk)->inet_sport); 452 ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport);
462 security_req_classify_flow(req, flowi4_to_flowi(fl4)); 453 security_req_classify_flow(req, flowi4_to_flowi(fl4));
463 rt = ip_route_output_flow(net, fl4, sk); 454 rt = ip_route_output_flow(net, fl4, sk);
464 if (IS_ERR(rt)) 455 if (IS_ERR(rt))
@@ -504,9 +495,9 @@ struct request_sock *inet_csk_search_req(const struct sock *sk,
504 prev = &req->dl_next) { 495 prev = &req->dl_next) {
505 const struct inet_request_sock *ireq = inet_rsk(req); 496 const struct inet_request_sock *ireq = inet_rsk(req);
506 497
507 if (ireq->rmt_port == rport && 498 if (ireq->ir_rmt_port == rport &&
508 ireq->rmt_addr == raddr && 499 ireq->ir_rmt_addr == raddr &&
509 ireq->loc_addr == laddr && 500 ireq->ir_loc_addr == laddr &&
510 AF_INET_FAMILY(req->rsk_ops->family)) { 501 AF_INET_FAMILY(req->rsk_ops->family)) {
511 WARN_ON(req->sk); 502 WARN_ON(req->sk);
512 *prevp = prev; 503 *prevp = prev;
@@ -523,7 +514,8 @@ void inet_csk_reqsk_queue_hash_add(struct sock *sk, struct request_sock *req,
523{ 514{
524 struct inet_connection_sock *icsk = inet_csk(sk); 515 struct inet_connection_sock *icsk = inet_csk(sk);
525 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; 516 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
526 const u32 h = inet_synq_hash(inet_rsk(req)->rmt_addr, inet_rsk(req)->rmt_port, 517 const u32 h = inet_synq_hash(inet_rsk(req)->ir_rmt_addr,
518 inet_rsk(req)->ir_rmt_port,
527 lopt->hash_rnd, lopt->nr_table_entries); 519 lopt->hash_rnd, lopt->nr_table_entries);
528 520
529 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout); 521 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
@@ -683,9 +675,9 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
683 newsk->sk_state = TCP_SYN_RECV; 675 newsk->sk_state = TCP_SYN_RECV;
684 newicsk->icsk_bind_hash = NULL; 676 newicsk->icsk_bind_hash = NULL;
685 677
686 inet_sk(newsk)->inet_dport = inet_rsk(req)->rmt_port; 678 inet_sk(newsk)->inet_dport = inet_rsk(req)->ir_rmt_port;
687 inet_sk(newsk)->inet_num = ntohs(inet_rsk(req)->loc_port); 679 inet_sk(newsk)->inet_num = inet_rsk(req)->ir_num;
688 inet_sk(newsk)->inet_sport = inet_rsk(req)->loc_port; 680 inet_sk(newsk)->inet_sport = htons(inet_rsk(req)->ir_num);
689 newsk->sk_write_space = sk_stream_write_space; 681 newsk->sk_write_space = sk_stream_write_space;
690 682
691 newicsk->icsk_retransmits = 0; 683 newicsk->icsk_retransmits = 0;
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 5f648751fce2..e34dccbc4d70 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -106,6 +106,10 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
106 106
107 r->id.idiag_sport = inet->inet_sport; 107 r->id.idiag_sport = inet->inet_sport;
108 r->id.idiag_dport = inet->inet_dport; 108 r->id.idiag_dport = inet->inet_dport;
109
110 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
111 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
112
109 r->id.idiag_src[0] = inet->inet_rcv_saddr; 113 r->id.idiag_src[0] = inet->inet_rcv_saddr;
110 r->id.idiag_dst[0] = inet->inet_daddr; 114 r->id.idiag_dst[0] = inet->inet_daddr;
111 115
@@ -121,13 +125,13 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
121 125
122#if IS_ENABLED(CONFIG_IPV6) 126#if IS_ENABLED(CONFIG_IPV6)
123 if (r->idiag_family == AF_INET6) { 127 if (r->idiag_family == AF_INET6) {
124 const struct ipv6_pinfo *np = inet6_sk(sk);
125 128
126 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 129 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
127 *(struct in6_addr *)r->id.idiag_dst = np->daddr; 130 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
128 131
129 if (ext & (1 << (INET_DIAG_TCLASS - 1))) 132 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
130 if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0) 133 if (nla_put_u8(skb, INET_DIAG_TCLASS,
134 inet6_sk(sk)->tclass) < 0)
131 goto errout; 135 goto errout;
132 } 136 }
133#endif 137#endif
@@ -222,7 +226,7 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
222 u32 portid, u32 seq, u16 nlmsg_flags, 226 u32 portid, u32 seq, u16 nlmsg_flags,
223 const struct nlmsghdr *unlh) 227 const struct nlmsghdr *unlh)
224{ 228{
225 long tmo; 229 s32 tmo;
226 struct inet_diag_msg *r; 230 struct inet_diag_msg *r;
227 struct nlmsghdr *nlh; 231 struct nlmsghdr *nlh;
228 232
@@ -234,32 +238,36 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
234 r = nlmsg_data(nlh); 238 r = nlmsg_data(nlh);
235 BUG_ON(tw->tw_state != TCP_TIME_WAIT); 239 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
236 240
237 tmo = tw->tw_ttd - jiffies; 241 tmo = tw->tw_ttd - inet_tw_time_stamp();
238 if (tmo < 0) 242 if (tmo < 0)
239 tmo = 0; 243 tmo = 0;
240 244
241 r->idiag_family = tw->tw_family; 245 r->idiag_family = tw->tw_family;
242 r->idiag_retrans = 0; 246 r->idiag_retrans = 0;
247
243 r->id.idiag_if = tw->tw_bound_dev_if; 248 r->id.idiag_if = tw->tw_bound_dev_if;
244 sock_diag_save_cookie(tw, r->id.idiag_cookie); 249 sock_diag_save_cookie(tw, r->id.idiag_cookie);
250
245 r->id.idiag_sport = tw->tw_sport; 251 r->id.idiag_sport = tw->tw_sport;
246 r->id.idiag_dport = tw->tw_dport; 252 r->id.idiag_dport = tw->tw_dport;
253
254 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
255 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
256
247 r->id.idiag_src[0] = tw->tw_rcv_saddr; 257 r->id.idiag_src[0] = tw->tw_rcv_saddr;
248 r->id.idiag_dst[0] = tw->tw_daddr; 258 r->id.idiag_dst[0] = tw->tw_daddr;
259
249 r->idiag_state = tw->tw_substate; 260 r->idiag_state = tw->tw_substate;
250 r->idiag_timer = 3; 261 r->idiag_timer = 3;
251 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 262 r->idiag_expires = jiffies_to_msecs(tmo);
252 r->idiag_rqueue = 0; 263 r->idiag_rqueue = 0;
253 r->idiag_wqueue = 0; 264 r->idiag_wqueue = 0;
254 r->idiag_uid = 0; 265 r->idiag_uid = 0;
255 r->idiag_inode = 0; 266 r->idiag_inode = 0;
256#if IS_ENABLED(CONFIG_IPV6) 267#if IS_ENABLED(CONFIG_IPV6)
257 if (tw->tw_family == AF_INET6) { 268 if (tw->tw_family == AF_INET6) {
258 const struct inet6_timewait_sock *tw6 = 269 *(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr;
259 inet6_twsk((struct sock *)tw); 270 *(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr;
260
261 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
262 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
263 } 271 }
264#endif 272#endif
265 273
@@ -273,10 +281,11 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
273 const struct nlmsghdr *unlh) 281 const struct nlmsghdr *unlh)
274{ 282{
275 if (sk->sk_state == TCP_TIME_WAIT) 283 if (sk->sk_state == TCP_TIME_WAIT)
276 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 284 return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq,
277 skb, r, portid, seq, nlmsg_flags, 285 nlmsg_flags, unlh);
278 unlh); 286
279 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh); 287 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
288 nlmsg_flags, unlh);
280} 289}
281 290
282int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, 291int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
@@ -338,12 +347,9 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_s
338 err = 0; 347 err = 0;
339 348
340out: 349out:
341 if (sk) { 350 if (sk)
342 if (sk->sk_state == TCP_TIME_WAIT) 351 sock_gen_put(sk);
343 inet_twsk_put((struct inet_timewait_sock *)sk); 352
344 else
345 sock_put(sk);
346 }
347out_nosk: 353out_nosk:
348 return err; 354 return err;
349} 355}
@@ -489,10 +495,9 @@ int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
489 entry.family = sk->sk_family; 495 entry.family = sk->sk_family;
490#if IS_ENABLED(CONFIG_IPV6) 496#if IS_ENABLED(CONFIG_IPV6)
491 if (entry.family == AF_INET6) { 497 if (entry.family == AF_INET6) {
492 struct ipv6_pinfo *np = inet6_sk(sk);
493 498
494 entry.saddr = np->rcv_saddr.s6_addr32; 499 entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
495 entry.daddr = np->daddr.s6_addr32; 500 entry.daddr = sk->sk_v6_daddr.s6_addr32;
496 } else 501 } else
497#endif 502#endif
498 { 503 {
@@ -635,22 +640,22 @@ static int inet_csk_diag_dump(struct sock *sk,
635 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 640 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
636} 641}
637 642
638static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 643static int inet_twsk_diag_dump(struct sock *sk,
639 struct sk_buff *skb, 644 struct sk_buff *skb,
640 struct netlink_callback *cb, 645 struct netlink_callback *cb,
641 struct inet_diag_req_v2 *r, 646 struct inet_diag_req_v2 *r,
642 const struct nlattr *bc) 647 const struct nlattr *bc)
643{ 648{
649 struct inet_timewait_sock *tw = inet_twsk(sk);
650
644 if (bc != NULL) { 651 if (bc != NULL) {
645 struct inet_diag_entry entry; 652 struct inet_diag_entry entry;
646 653
647 entry.family = tw->tw_family; 654 entry.family = tw->tw_family;
648#if IS_ENABLED(CONFIG_IPV6) 655#if IS_ENABLED(CONFIG_IPV6)
649 if (tw->tw_family == AF_INET6) { 656 if (tw->tw_family == AF_INET6) {
650 struct inet6_timewait_sock *tw6 = 657 entry.saddr = tw->tw_v6_rcv_saddr.s6_addr32;
651 inet6_twsk((struct sock *)tw); 658 entry.daddr = tw->tw_v6_daddr.s6_addr32;
652 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
653 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
654 } else 659 } else
655#endif 660#endif
656 { 661 {
@@ -682,12 +687,12 @@ static inline void inet_diag_req_addrs(const struct sock *sk,
682#if IS_ENABLED(CONFIG_IPV6) 687#if IS_ENABLED(CONFIG_IPV6)
683 if (sk->sk_family == AF_INET6) { 688 if (sk->sk_family == AF_INET6) {
684 if (req->rsk_ops->family == AF_INET6) { 689 if (req->rsk_ops->family == AF_INET6) {
685 entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32; 690 entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
686 entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32; 691 entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
687 } else if (req->rsk_ops->family == AF_INET) { 692 } else if (req->rsk_ops->family == AF_INET) {
688 ipv6_addr_set_v4mapped(ireq->loc_addr, 693 ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
689 &entry->saddr_storage); 694 &entry->saddr_storage);
690 ipv6_addr_set_v4mapped(ireq->rmt_addr, 695 ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
691 &entry->daddr_storage); 696 &entry->daddr_storage);
692 entry->saddr = entry->saddr_storage.s6_addr32; 697 entry->saddr = entry->saddr_storage.s6_addr32;
693 entry->daddr = entry->daddr_storage.s6_addr32; 698 entry->daddr = entry->daddr_storage.s6_addr32;
@@ -695,8 +700,8 @@ static inline void inet_diag_req_addrs(const struct sock *sk,
695 } else 700 } else
696#endif 701#endif
697 { 702 {
698 entry->saddr = &ireq->loc_addr; 703 entry->saddr = &ireq->ir_loc_addr;
699 entry->daddr = &ireq->rmt_addr; 704 entry->daddr = &ireq->ir_rmt_addr;
700 } 705 }
701} 706}
702 707
@@ -731,9 +736,14 @@ static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
731 tmo = 0; 736 tmo = 0;
732 737
733 r->id.idiag_sport = inet->inet_sport; 738 r->id.idiag_sport = inet->inet_sport;
734 r->id.idiag_dport = ireq->rmt_port; 739 r->id.idiag_dport = ireq->ir_rmt_port;
735 r->id.idiag_src[0] = ireq->loc_addr; 740
736 r->id.idiag_dst[0] = ireq->rmt_addr; 741 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
742 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
743
744 r->id.idiag_src[0] = ireq->ir_loc_addr;
745 r->id.idiag_dst[0] = ireq->ir_rmt_addr;
746
737 r->idiag_expires = jiffies_to_msecs(tmo); 747 r->idiag_expires = jiffies_to_msecs(tmo);
738 r->idiag_rqueue = 0; 748 r->idiag_rqueue = 0;
739 r->idiag_wqueue = 0; 749 r->idiag_wqueue = 0;
@@ -792,13 +802,13 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
792 802
793 if (reqnum < s_reqnum) 803 if (reqnum < s_reqnum)
794 continue; 804 continue;
795 if (r->id.idiag_dport != ireq->rmt_port && 805 if (r->id.idiag_dport != ireq->ir_rmt_port &&
796 r->id.idiag_dport) 806 r->id.idiag_dport)
797 continue; 807 continue;
798 808
799 if (bc) { 809 if (bc) {
800 inet_diag_req_addrs(sk, req, &entry); 810 inet_diag_req_addrs(sk, req, &entry);
801 entry.dport = ntohs(ireq->rmt_port); 811 entry.dport = ntohs(ireq->ir_rmt_port);
802 812
803 if (!inet_diag_bc_run(bc, &entry)) 813 if (!inet_diag_bc_run(bc, &entry))
804 continue; 814 continue;
@@ -911,8 +921,7 @@ skip_listen_ht:
911 921
912 num = 0; 922 num = 0;
913 923
914 if (hlist_nulls_empty(&head->chain) && 924 if (hlist_nulls_empty(&head->chain))
915 hlist_nulls_empty(&head->twchain))
916 continue; 925 continue;
917 926
918 if (i > s_i) 927 if (i > s_i)
@@ -920,24 +929,31 @@ skip_listen_ht:
920 929
921 spin_lock_bh(lock); 930 spin_lock_bh(lock);
922 sk_nulls_for_each(sk, node, &head->chain) { 931 sk_nulls_for_each(sk, node, &head->chain) {
923 struct inet_sock *inet = inet_sk(sk); 932 int res;
933 int state;
924 934
925 if (!net_eq(sock_net(sk), net)) 935 if (!net_eq(sock_net(sk), net))
926 continue; 936 continue;
927 if (num < s_num) 937 if (num < s_num)
928 goto next_normal; 938 goto next_normal;
929 if (!(r->idiag_states & (1 << sk->sk_state))) 939 state = (sk->sk_state == TCP_TIME_WAIT) ?
940 inet_twsk(sk)->tw_substate : sk->sk_state;
941 if (!(r->idiag_states & (1 << state)))
930 goto next_normal; 942 goto next_normal;
931 if (r->sdiag_family != AF_UNSPEC && 943 if (r->sdiag_family != AF_UNSPEC &&
932 sk->sk_family != r->sdiag_family) 944 sk->sk_family != r->sdiag_family)
933 goto next_normal; 945 goto next_normal;
934 if (r->id.idiag_sport != inet->inet_sport && 946 if (r->id.idiag_sport != htons(sk->sk_num) &&
935 r->id.idiag_sport) 947 r->id.idiag_sport)
936 goto next_normal; 948 goto next_normal;
937 if (r->id.idiag_dport != inet->inet_dport && 949 if (r->id.idiag_dport != sk->sk_dport &&
938 r->id.idiag_dport) 950 r->id.idiag_dport)
939 goto next_normal; 951 goto next_normal;
940 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 952 if (sk->sk_state == TCP_TIME_WAIT)
953 res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
954 else
955 res = inet_csk_diag_dump(sk, skb, cb, r, bc);
956 if (res < 0) {
941 spin_unlock_bh(lock); 957 spin_unlock_bh(lock);
942 goto done; 958 goto done;
943 } 959 }
@@ -945,33 +961,6 @@ next_normal:
945 ++num; 961 ++num;
946 } 962 }
947 963
948 if (r->idiag_states & TCPF_TIME_WAIT) {
949 struct inet_timewait_sock *tw;
950
951 inet_twsk_for_each(tw, node,
952 &head->twchain) {
953 if (!net_eq(twsk_net(tw), net))
954 continue;
955
956 if (num < s_num)
957 goto next_dying;
958 if (r->sdiag_family != AF_UNSPEC &&
959 tw->tw_family != r->sdiag_family)
960 goto next_dying;
961 if (r->id.idiag_sport != tw->tw_sport &&
962 r->id.idiag_sport)
963 goto next_dying;
964 if (r->id.idiag_dport != tw->tw_dport &&
965 r->id.idiag_dport)
966 goto next_dying;
967 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
968 spin_unlock_bh(lock);
969 goto done;
970 }
971next_dying:
972 ++num;
973 }
974 }
975 spin_unlock_bh(lock); 964 spin_unlock_bh(lock);
976 } 965 }
977 966
diff --git a/net/ipv4/inet_fragment.c b/net/ipv4/inet_fragment.c
index c5313a9c019b..bb075fc9a14f 100644
--- a/net/ipv4/inet_fragment.c
+++ b/net/ipv4/inet_fragment.c
@@ -93,9 +93,6 @@ void inet_frags_init(struct inet_frags *f)
93 } 93 }
94 rwlock_init(&f->lock); 94 rwlock_init(&f->lock);
95 95
96 f->rnd = (u32) ((totalram_pages ^ (totalram_pages >> 7)) ^
97 (jiffies ^ (jiffies >> 6)));
98
99 setup_timer(&f->secret_timer, inet_frag_secret_rebuild, 96 setup_timer(&f->secret_timer, inet_frag_secret_rebuild,
100 (unsigned long)f); 97 (unsigned long)f);
101 f->secret_timer.expires = jiffies + f->secret_interval; 98 f->secret_timer.expires = jiffies + f->secret_interval;
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 96da9c77deca..8b9cf279450d 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -24,6 +24,31 @@
24#include <net/secure_seq.h> 24#include <net/secure_seq.h>
25#include <net/ip.h> 25#include <net/ip.h>
26 26
27static unsigned int inet_ehashfn(struct net *net, const __be32 laddr,
28 const __u16 lport, const __be32 faddr,
29 const __be16 fport)
30{
31 static u32 inet_ehash_secret __read_mostly;
32
33 net_get_random_once(&inet_ehash_secret, sizeof(inet_ehash_secret));
34
35 return __inet_ehashfn(laddr, lport, faddr, fport,
36 inet_ehash_secret + net_hash_mix(net));
37}
38
39
40static unsigned int inet_sk_ehashfn(const struct sock *sk)
41{
42 const struct inet_sock *inet = inet_sk(sk);
43 const __be32 laddr = inet->inet_rcv_saddr;
44 const __u16 lport = inet->inet_num;
45 const __be32 faddr = inet->inet_daddr;
46 const __be16 fport = inet->inet_dport;
47 struct net *net = sock_net(sk);
48
49 return inet_ehashfn(net, laddr, lport, faddr, fport);
50}
51
27/* 52/*
28 * Allocate and initialize a new local port bind bucket. 53 * Allocate and initialize a new local port bind bucket.
29 * The bindhash mutex for snum's hash chain must be held here. 54 * The bindhash mutex for snum's hash chain must be held here.
@@ -230,6 +255,19 @@ begin:
230} 255}
231EXPORT_SYMBOL_GPL(__inet_lookup_listener); 256EXPORT_SYMBOL_GPL(__inet_lookup_listener);
232 257
258/* All sockets share common refcount, but have different destructors */
259void sock_gen_put(struct sock *sk)
260{
261 if (!atomic_dec_and_test(&sk->sk_refcnt))
262 return;
263
264 if (sk->sk_state == TCP_TIME_WAIT)
265 inet_twsk_free(inet_twsk(sk));
266 else
267 sk_free(sk);
268}
269EXPORT_SYMBOL_GPL(sock_gen_put);
270
233struct sock *__inet_lookup_established(struct net *net, 271struct sock *__inet_lookup_established(struct net *net,
234 struct inet_hashinfo *hashinfo, 272 struct inet_hashinfo *hashinfo,
235 const __be32 saddr, const __be16 sport, 273 const __be32 saddr, const __be16 sport,
@@ -255,13 +293,13 @@ begin:
255 if (likely(INET_MATCH(sk, net, acookie, 293 if (likely(INET_MATCH(sk, net, acookie,
256 saddr, daddr, ports, dif))) { 294 saddr, daddr, ports, dif))) {
257 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) 295 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
258 goto begintw; 296 goto out;
259 if (unlikely(!INET_MATCH(sk, net, acookie, 297 if (unlikely(!INET_MATCH(sk, net, acookie,
260 saddr, daddr, ports, dif))) { 298 saddr, daddr, ports, dif))) {
261 sock_put(sk); 299 sock_gen_put(sk);
262 goto begin; 300 goto begin;
263 } 301 }
264 goto out; 302 goto found;
265 } 303 }
266 } 304 }
267 /* 305 /*
@@ -271,37 +309,9 @@ begin:
271 */ 309 */
272 if (get_nulls_value(node) != slot) 310 if (get_nulls_value(node) != slot)
273 goto begin; 311 goto begin;
274
275begintw:
276 /* Must check for a TIME_WAIT'er before going to listener hash. */
277 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
278 if (sk->sk_hash != hash)
279 continue;
280 if (likely(INET_TW_MATCH(sk, net, acookie,
281 saddr, daddr, ports,
282 dif))) {
283 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
284 sk = NULL;
285 goto out;
286 }
287 if (unlikely(!INET_TW_MATCH(sk, net, acookie,
288 saddr, daddr, ports,
289 dif))) {
290 inet_twsk_put(inet_twsk(sk));
291 goto begintw;
292 }
293 goto out;
294 }
295 }
296 /*
297 * if the nulls value we got at the end of this lookup is
298 * not the expected one, we must restart lookup.
299 * We probably met an item that was moved to another chain.
300 */
301 if (get_nulls_value(node) != slot)
302 goto begintw;
303 sk = NULL;
304out: 312out:
313 sk = NULL;
314found:
305 rcu_read_unlock(); 315 rcu_read_unlock();
306 return sk; 316 return sk;
307} 317}
@@ -326,39 +336,29 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
326 spinlock_t *lock = inet_ehash_lockp(hinfo, hash); 336 spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
327 struct sock *sk2; 337 struct sock *sk2;
328 const struct hlist_nulls_node *node; 338 const struct hlist_nulls_node *node;
329 struct inet_timewait_sock *tw; 339 struct inet_timewait_sock *tw = NULL;
330 int twrefcnt = 0; 340 int twrefcnt = 0;
331 341
332 spin_lock(lock); 342 spin_lock(lock);
333 343
334 /* Check TIME-WAIT sockets first. */
335 sk_nulls_for_each(sk2, node, &head->twchain) {
336 if (sk2->sk_hash != hash)
337 continue;
338
339 if (likely(INET_TW_MATCH(sk2, net, acookie,
340 saddr, daddr, ports, dif))) {
341 tw = inet_twsk(sk2);
342 if (twsk_unique(sk, sk2, twp))
343 goto unique;
344 else
345 goto not_unique;
346 }
347 }
348 tw = NULL;
349
350 /* And established part... */
351 sk_nulls_for_each(sk2, node, &head->chain) { 344 sk_nulls_for_each(sk2, node, &head->chain) {
352 if (sk2->sk_hash != hash) 345 if (sk2->sk_hash != hash)
353 continue; 346 continue;
347
354 if (likely(INET_MATCH(sk2, net, acookie, 348 if (likely(INET_MATCH(sk2, net, acookie,
355 saddr, daddr, ports, dif))) 349 saddr, daddr, ports, dif))) {
350 if (sk2->sk_state == TCP_TIME_WAIT) {
351 tw = inet_twsk(sk2);
352 if (twsk_unique(sk, sk2, twp))
353 break;
354 }
356 goto not_unique; 355 goto not_unique;
356 }
357 } 357 }
358 358
359unique:
360 /* Must record num and sport now. Otherwise we will see 359 /* Must record num and sport now. Otherwise we will see
361 * in hash table socket with a funny identity. */ 360 * in hash table socket with a funny identity.
361 */
362 inet->inet_num = lport; 362 inet->inet_num = lport;
363 inet->inet_sport = htons(lport); 363 inet->inet_sport = htons(lport);
364 sk->sk_hash = hash; 364 sk->sk_hash = hash;
@@ -494,7 +494,7 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
494 u32 offset = hint + port_offset; 494 u32 offset = hint + port_offset;
495 struct inet_timewait_sock *tw = NULL; 495 struct inet_timewait_sock *tw = NULL;
496 496
497 inet_get_local_port_range(&low, &high); 497 inet_get_local_port_range(net, &low, &high);
498 remaining = (high - low) + 1; 498 remaining = (high - low) + 1;
499 499
500 local_bh_disable(); 500 local_bh_disable();
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c
index 1f27c9f4afd0..6d592f8555fb 100644
--- a/net/ipv4/inet_timewait_sock.c
+++ b/net/ipv4/inet_timewait_sock.c
@@ -87,19 +87,11 @@ static void __inet_twsk_kill(struct inet_timewait_sock *tw,
87 refcnt += inet_twsk_bind_unhash(tw, hashinfo); 87 refcnt += inet_twsk_bind_unhash(tw, hashinfo);
88 spin_unlock(&bhead->lock); 88 spin_unlock(&bhead->lock);
89 89
90#ifdef SOCK_REFCNT_DEBUG 90 BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt));
91 if (atomic_read(&tw->tw_refcnt) != 1) { 91 atomic_sub(refcnt, &tw->tw_refcnt);
92 pr_debug("%s timewait_sock %p refcnt=%d\n",
93 tw->tw_prot->name, tw, atomic_read(&tw->tw_refcnt));
94 }
95#endif
96 while (refcnt) {
97 inet_twsk_put(tw);
98 refcnt--;
99 }
100} 92}
101 93
102static noinline void inet_twsk_free(struct inet_timewait_sock *tw) 94void inet_twsk_free(struct inet_timewait_sock *tw)
103{ 95{
104 struct module *owner = tw->tw_prot->owner; 96 struct module *owner = tw->tw_prot->owner;
105 twsk_destructor((struct sock *)tw); 97 twsk_destructor((struct sock *)tw);
@@ -118,6 +110,18 @@ void inet_twsk_put(struct inet_timewait_sock *tw)
118} 110}
119EXPORT_SYMBOL_GPL(inet_twsk_put); 111EXPORT_SYMBOL_GPL(inet_twsk_put);
120 112
113static void inet_twsk_add_node_rcu(struct inet_timewait_sock *tw,
114 struct hlist_nulls_head *list)
115{
116 hlist_nulls_add_head_rcu(&tw->tw_node, list);
117}
118
119static void inet_twsk_add_bind_node(struct inet_timewait_sock *tw,
120 struct hlist_head *list)
121{
122 hlist_add_head(&tw->tw_bind_node, list);
123}
124
121/* 125/*
122 * Enter the time wait state. This is called with locally disabled BH. 126 * Enter the time wait state. This is called with locally disabled BH.
123 * Essentially we whip up a timewait bucket, copy the relevant info into it 127 * Essentially we whip up a timewait bucket, copy the relevant info into it
@@ -146,26 +150,21 @@ void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
146 spin_lock(lock); 150 spin_lock(lock);
147 151
148 /* 152 /*
149 * Step 2: Hash TW into TIMEWAIT chain. 153 * Step 2: Hash TW into tcp ehash chain.
150 * Should be done before removing sk from established chain 154 * Notes :
151 * because readers are lockless and search established first. 155 * - tw_refcnt is set to 3 because :
156 * - We have one reference from bhash chain.
157 * - We have one reference from ehash chain.
158 * We can use atomic_set() because prior spin_lock()/spin_unlock()
159 * committed into memory all tw fields.
152 */ 160 */
153 inet_twsk_add_node_rcu(tw, &ehead->twchain); 161 atomic_set(&tw->tw_refcnt, 1 + 1 + 1);
162 inet_twsk_add_node_rcu(tw, &ehead->chain);
154 163
155 /* Step 3: Remove SK from established hash. */ 164 /* Step 3: Remove SK from hash chain */
156 if (__sk_nulls_del_node_init_rcu(sk)) 165 if (__sk_nulls_del_node_init_rcu(sk))
157 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1); 166 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
158 167
159 /*
160 * Notes :
161 * - We initially set tw_refcnt to 0 in inet_twsk_alloc()
162 * - We add one reference for the bhash link
163 * - We add one reference for the ehash link
164 * - We want this refcnt update done before allowing other
165 * threads to find this tw in ehash chain.
166 */
167 atomic_add(1 + 1 + 1, &tw->tw_refcnt);
168
169 spin_unlock(lock); 168 spin_unlock(lock);
170} 169}
171EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); 170EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
@@ -387,11 +386,11 @@ void inet_twsk_schedule(struct inet_timewait_sock *tw,
387 if (slot >= INET_TWDR_TWKILL_SLOTS) 386 if (slot >= INET_TWDR_TWKILL_SLOTS)
388 slot = INET_TWDR_TWKILL_SLOTS - 1; 387 slot = INET_TWDR_TWKILL_SLOTS - 1;
389 } 388 }
390 tw->tw_ttd = jiffies + timeo; 389 tw->tw_ttd = inet_tw_time_stamp() + timeo;
391 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1); 390 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
392 list = &twdr->cells[slot]; 391 list = &twdr->cells[slot];
393 } else { 392 } else {
394 tw->tw_ttd = jiffies + (slot << INET_TWDR_RECYCLE_TICK); 393 tw->tw_ttd = inet_tw_time_stamp() + (slot << INET_TWDR_RECYCLE_TICK);
395 394
396 if (twdr->twcal_hand < 0) { 395 if (twdr->twcal_hand < 0) {
397 twdr->twcal_hand = 0; 396 twdr->twcal_hand = 0;
@@ -490,7 +489,9 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo,
490restart_rcu: 489restart_rcu:
491 rcu_read_lock(); 490 rcu_read_lock();
492restart: 491restart:
493 sk_nulls_for_each_rcu(sk, node, &head->twchain) { 492 sk_nulls_for_each_rcu(sk, node, &head->chain) {
493 if (sk->sk_state != TCP_TIME_WAIT)
494 continue;
494 tw = inet_twsk(sk); 495 tw = inet_twsk(sk);
495 if ((tw->tw_family != family) || 496 if ((tw->tw_family != family) ||
496 atomic_read(&twsk_net(tw)->count)) 497 atomic_read(&twsk_net(tw)->count))
diff --git a/net/ipv4/ip_fragment.c b/net/ipv4/ip_fragment.c
index b66910aaef4d..2481993a4970 100644
--- a/net/ipv4/ip_fragment.c
+++ b/net/ipv4/ip_fragment.c
@@ -106,6 +106,7 @@ struct ip4_create_arg {
106 106
107static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot) 107static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
108{ 108{
109 net_get_random_once(&ip4_frags.rnd, sizeof(ip4_frags.rnd));
109 return jhash_3words((__force u32)id << 16 | prot, 110 return jhash_3words((__force u32)id << 16 | prot,
110 (__force u32)saddr, (__force u32)daddr, 111 (__force u32)saddr, (__force u32)daddr,
111 ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1); 112 ip4_frags.rnd) & (INETFRAGS_HASHSZ - 1);
diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
index d7aea4c5b940..e560ef34cf4b 100644
--- a/net/ipv4/ip_gre.c
+++ b/net/ipv4/ip_gre.c
@@ -217,6 +217,7 @@ static int ipgre_rcv(struct sk_buff *skb, const struct tnl_ptk_info *tpi)
217 iph->saddr, iph->daddr, tpi->key); 217 iph->saddr, iph->daddr, tpi->key);
218 218
219 if (tunnel) { 219 if (tunnel) {
220 skb_pop_mac_header(skb);
220 ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error); 221 ip_tunnel_rcv(tunnel, skb, tpi, log_ecn_error);
221 return PACKET_RCVD; 222 return PACKET_RCVD;
222 } 223 }
diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
index 3982eabf61e1..df184616493f 100644
--- a/net/ipv4/ip_output.c
+++ b/net/ipv4/ip_output.c
@@ -810,7 +810,7 @@ static int __ip_append_data(struct sock *sk,
810 int copy; 810 int copy;
811 int err; 811 int err;
812 int offset = 0; 812 int offset = 0;
813 unsigned int maxfraglen, fragheaderlen; 813 unsigned int maxfraglen, fragheaderlen, maxnonfragsize;
814 int csummode = CHECKSUM_NONE; 814 int csummode = CHECKSUM_NONE;
815 struct rtable *rt = (struct rtable *)cork->dst; 815 struct rtable *rt = (struct rtable *)cork->dst;
816 816
@@ -823,10 +823,12 @@ static int __ip_append_data(struct sock *sk,
823 823
824 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 824 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
825 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 825 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
826 maxnonfragsize = (inet->pmtudisc >= IP_PMTUDISC_DO) ?
827 mtu : 0xFFFF;
826 828
827 if (cork->length + length > 0xFFFF - fragheaderlen) { 829 if (cork->length + length > maxnonfragsize - fragheaderlen) {
828 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, 830 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
829 mtu-exthdrlen); 831 mtu - (opt ? opt->optlen : 0));
830 return -EMSGSIZE; 832 return -EMSGSIZE;
831 } 833 }
832 834
@@ -1035,7 +1037,6 @@ error:
1035static int ip_setup_cork(struct sock *sk, struct inet_cork *cork, 1037static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1036 struct ipcm_cookie *ipc, struct rtable **rtp) 1038 struct ipcm_cookie *ipc, struct rtable **rtp)
1037{ 1039{
1038 struct inet_sock *inet = inet_sk(sk);
1039 struct ip_options_rcu *opt; 1040 struct ip_options_rcu *opt;
1040 struct rtable *rt; 1041 struct rtable *rt;
1041 1042
@@ -1061,10 +1062,13 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1061 * We steal reference to this route, caller should not release it 1062 * We steal reference to this route, caller should not release it
1062 */ 1063 */
1063 *rtp = NULL; 1064 *rtp = NULL;
1064 cork->fragsize = inet->pmtudisc == IP_PMTUDISC_PROBE ? 1065 cork->fragsize = ip_sk_use_pmtu(sk) ?
1065 rt->dst.dev->mtu : dst_mtu(&rt->dst); 1066 dst_mtu(&rt->dst) : rt->dst.dev->mtu;
1066 cork->dst = &rt->dst; 1067 cork->dst = &rt->dst;
1067 cork->length = 0; 1068 cork->length = 0;
1069 cork->ttl = ipc->ttl;
1070 cork->tos = ipc->tos;
1071 cork->priority = ipc->priority;
1068 cork->tx_flags = ipc->tx_flags; 1072 cork->tx_flags = ipc->tx_flags;
1069 1073
1070 return 0; 1074 return 0;
@@ -1119,7 +1123,7 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1119 int mtu; 1123 int mtu;
1120 int len; 1124 int len;
1121 int err; 1125 int err;
1122 unsigned int maxfraglen, fragheaderlen, fraggap; 1126 unsigned int maxfraglen, fragheaderlen, fraggap, maxnonfragsize;
1123 1127
1124 if (inet->hdrincl) 1128 if (inet->hdrincl)
1125 return -EPERM; 1129 return -EPERM;
@@ -1143,9 +1147,12 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1143 1147
1144 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0); 1148 fragheaderlen = sizeof(struct iphdr) + (opt ? opt->optlen : 0);
1145 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen; 1149 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen;
1150 maxnonfragsize = (inet->pmtudisc >= IP_PMTUDISC_DO) ?
1151 mtu : 0xFFFF;
1146 1152
1147 if (cork->length + size > 0xFFFF - fragheaderlen) { 1153 if (cork->length + size > maxnonfragsize - fragheaderlen) {
1148 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport, mtu); 1154 ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
1155 mtu - (opt ? opt->optlen : 0));
1149 return -EMSGSIZE; 1156 return -EMSGSIZE;
1150 } 1157 }
1151 1158
@@ -1308,7 +1315,8 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1308 /* DF bit is set when we want to see DF on outgoing frames. 1315 /* DF bit is set when we want to see DF on outgoing frames.
1309 * If local_df is set too, we still allow to fragment this frame 1316 * If local_df is set too, we still allow to fragment this frame
1310 * locally. */ 1317 * locally. */
1311 if (inet->pmtudisc >= IP_PMTUDISC_DO || 1318 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1319 inet->pmtudisc == IP_PMTUDISC_PROBE ||
1312 (skb->len <= dst_mtu(&rt->dst) && 1320 (skb->len <= dst_mtu(&rt->dst) &&
1313 ip_dont_fragment(sk, &rt->dst))) 1321 ip_dont_fragment(sk, &rt->dst)))
1314 df = htons(IP_DF); 1322 df = htons(IP_DF);
@@ -1316,7 +1324,9 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1316 if (cork->flags & IPCORK_OPT) 1324 if (cork->flags & IPCORK_OPT)
1317 opt = cork->opt; 1325 opt = cork->opt;
1318 1326
1319 if (rt->rt_type == RTN_MULTICAST) 1327 if (cork->ttl != 0)
1328 ttl = cork->ttl;
1329 else if (rt->rt_type == RTN_MULTICAST)
1320 ttl = inet->mc_ttl; 1330 ttl = inet->mc_ttl;
1321 else 1331 else
1322 ttl = ip_select_ttl(inet, &rt->dst); 1332 ttl = ip_select_ttl(inet, &rt->dst);
@@ -1324,7 +1334,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1324 iph = ip_hdr(skb); 1334 iph = ip_hdr(skb);
1325 iph->version = 4; 1335 iph->version = 4;
1326 iph->ihl = 5; 1336 iph->ihl = 5;
1327 iph->tos = inet->tos; 1337 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1328 iph->frag_off = df; 1338 iph->frag_off = df;
1329 iph->ttl = ttl; 1339 iph->ttl = ttl;
1330 iph->protocol = sk->sk_protocol; 1340 iph->protocol = sk->sk_protocol;
@@ -1336,7 +1346,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1336 ip_options_build(skb, opt, cork->addr, rt, 0); 1346 ip_options_build(skb, opt, cork->addr, rt, 0);
1337 } 1347 }
1338 1348
1339 skb->priority = sk->sk_priority; 1349 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1340 skb->mark = sk->sk_mark; 1350 skb->mark = sk->sk_mark;
1341 /* 1351 /*
1342 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1352 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
@@ -1486,6 +1496,8 @@ void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr,
1486 ipc.addr = daddr; 1496 ipc.addr = daddr;
1487 ipc.opt = NULL; 1497 ipc.opt = NULL;
1488 ipc.tx_flags = 0; 1498 ipc.tx_flags = 0;
1499 ipc.ttl = 0;
1500 ipc.tos = -1;
1489 1501
1490 if (replyopts.opt.opt.optlen) { 1502 if (replyopts.opt.opt.optlen) {
1491 ipc.opt = &replyopts.opt; 1503 ipc.opt = &replyopts.opt;
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index d9c4f113d709..ddf32a6bc415 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -189,7 +189,7 @@ EXPORT_SYMBOL(ip_cmsg_recv);
189 189
190int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc) 190int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
191{ 191{
192 int err; 192 int err, val;
193 struct cmsghdr *cmsg; 193 struct cmsghdr *cmsg;
194 194
195 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) { 195 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
@@ -215,6 +215,24 @@ int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
215 ipc->addr = info->ipi_spec_dst.s_addr; 215 ipc->addr = info->ipi_spec_dst.s_addr;
216 break; 216 break;
217 } 217 }
218 case IP_TTL:
219 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
220 return -EINVAL;
221 val = *(int *)CMSG_DATA(cmsg);
222 if (val < 1 || val > 255)
223 return -EINVAL;
224 ipc->ttl = val;
225 break;
226 case IP_TOS:
227 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
228 return -EINVAL;
229 val = *(int *)CMSG_DATA(cmsg);
230 if (val < 0 || val > 255)
231 return -EINVAL;
232 ipc->tos = val;
233 ipc->priority = rt_tos2priority(ipc->tos);
234 break;
235
218 default: 236 default:
219 return -EINVAL; 237 return -EINVAL;
220 } 238 }
@@ -368,7 +386,7 @@ void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 inf
368/* 386/*
369 * Handle MSG_ERRQUEUE 387 * Handle MSG_ERRQUEUE
370 */ 388 */
371int ip_recv_error(struct sock *sk, struct msghdr *msg, int len) 389int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
372{ 390{
373 struct sock_exterr_skb *serr; 391 struct sock_exterr_skb *serr;
374 struct sk_buff *skb, *skb2; 392 struct sk_buff *skb, *skb2;
@@ -405,6 +423,7 @@ int ip_recv_error(struct sock *sk, struct msghdr *msg, int len)
405 serr->addr_offset); 423 serr->addr_offset);
406 sin->sin_port = serr->port; 424 sin->sin_port = serr->port;
407 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 425 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
426 *addr_len = sizeof(*sin);
408 } 427 }
409 428
410 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 429 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
@@ -609,7 +628,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
609 inet->nodefrag = val ? 1 : 0; 628 inet->nodefrag = val ? 1 : 0;
610 break; 629 break;
611 case IP_MTU_DISCOVER: 630 case IP_MTU_DISCOVER:
612 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) 631 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_INTERFACE)
613 goto e_inval; 632 goto e_inval;
614 inet->pmtudisc = val; 633 inet->pmtudisc = val;
615 break; 634 break;
@@ -1034,11 +1053,12 @@ e_inval:
1034 * destination in skb->cb[] before dst drop. 1053 * destination in skb->cb[] before dst drop.
1035 * This way, receiver doesnt make cache line misses to read rtable. 1054 * This way, receiver doesnt make cache line misses to read rtable.
1036 */ 1055 */
1037void ipv4_pktinfo_prepare(struct sk_buff *skb) 1056void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1038{ 1057{
1039 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); 1058 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1040 1059
1041 if (skb_rtable(skb)) { 1060 if ((inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) &&
1061 skb_rtable(skb)) {
1042 pktinfo->ipi_ifindex = inet_iif(skb); 1062 pktinfo->ipi_ifindex = inet_iif(skb);
1043 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); 1063 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1044 } else { 1064 } else {
diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c
index 63a6d6d6b875..90ff9570d7d4 100644
--- a/net/ipv4/ip_tunnel.c
+++ b/net/ipv4/ip_tunnel.c
@@ -454,6 +454,8 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
454 tstats->rx_bytes += skb->len; 454 tstats->rx_bytes += skb->len;
455 u64_stats_update_end(&tstats->syncp); 455 u64_stats_update_end(&tstats->syncp);
456 456
457 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
458
457 if (tunnel->dev->type == ARPHRD_ETHER) { 459 if (tunnel->dev->type == ARPHRD_ETHER) {
458 skb->protocol = eth_type_trans(skb, tunnel->dev); 460 skb->protocol = eth_type_trans(skb, tunnel->dev);
459 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 461 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
@@ -461,8 +463,6 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
461 skb->dev = tunnel->dev; 463 skb->dev = tunnel->dev;
462 } 464 }
463 465
464 skb_scrub_packet(skb, !net_eq(tunnel->net, dev_net(tunnel->dev)));
465
466 gro_cells_receive(&tunnel->gro_cells, skb); 466 gro_cells_receive(&tunnel->gro_cells, skb);
467 return 0; 467 return 0;
468 468
@@ -976,13 +976,19 @@ int ip_tunnel_init(struct net_device *dev)
976{ 976{
977 struct ip_tunnel *tunnel = netdev_priv(dev); 977 struct ip_tunnel *tunnel = netdev_priv(dev);
978 struct iphdr *iph = &tunnel->parms.iph; 978 struct iphdr *iph = &tunnel->parms.iph;
979 int err; 979 int i, err;
980 980
981 dev->destructor = ip_tunnel_dev_free; 981 dev->destructor = ip_tunnel_dev_free;
982 dev->tstats = alloc_percpu(struct pcpu_tstats); 982 dev->tstats = alloc_percpu(struct pcpu_tstats);
983 if (!dev->tstats) 983 if (!dev->tstats)
984 return -ENOMEM; 984 return -ENOMEM;
985 985
986 for_each_possible_cpu(i) {
987 struct pcpu_tstats *ipt_stats;
988 ipt_stats = per_cpu_ptr(dev->tstats, i);
989 u64_stats_init(&ipt_stats->syncp);
990 }
991
986 err = gro_cells_init(&tunnel->gro_cells, dev); 992 err = gro_cells_init(&tunnel->gro_cells, dev);
987 if (err) { 993 if (err) {
988 free_percpu(dev->tstats); 994 free_percpu(dev->tstats);
diff --git a/net/ipv4/ip_tunnel_core.c b/net/ipv4/ip_tunnel_core.c
index c31e3ad98ef2..42ffbc8d65c6 100644
--- a/net/ipv4/ip_tunnel_core.c
+++ b/net/ipv4/ip_tunnel_core.c
@@ -116,3 +116,36 @@ int iptunnel_pull_header(struct sk_buff *skb, int hdr_len, __be16 inner_proto)
116 return 0; 116 return 0;
117} 117}
118EXPORT_SYMBOL_GPL(iptunnel_pull_header); 118EXPORT_SYMBOL_GPL(iptunnel_pull_header);
119
120struct sk_buff *iptunnel_handle_offloads(struct sk_buff *skb,
121 bool csum_help,
122 int gso_type_mask)
123{
124 int err;
125
126 if (likely(!skb->encapsulation)) {
127 skb_reset_inner_headers(skb);
128 skb->encapsulation = 1;
129 }
130
131 if (skb_is_gso(skb)) {
132 err = skb_unclone(skb, GFP_ATOMIC);
133 if (unlikely(err))
134 goto error;
135 skb_shinfo(skb)->gso_type |= gso_type_mask;
136 return skb;
137 }
138
139 if (skb->ip_summed == CHECKSUM_PARTIAL && csum_help) {
140 err = skb_checksum_help(skb);
141 if (unlikely(err))
142 goto error;
143 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
144 skb->ip_summed = CHECKSUM_NONE;
145
146 return skb;
147error:
148 kfree_skb(skb);
149 return ERR_PTR(err);
150}
151EXPORT_SYMBOL_GPL(iptunnel_handle_offloads);
diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c
index 6e87f853d033..52b802a0cd8c 100644
--- a/net/ipv4/ip_vti.c
+++ b/net/ipv4/ip_vti.c
@@ -49,70 +49,6 @@ static struct rtnl_link_ops vti_link_ops __read_mostly;
49static int vti_net_id __read_mostly; 49static int vti_net_id __read_mostly;
50static int vti_tunnel_init(struct net_device *dev); 50static int vti_tunnel_init(struct net_device *dev);
51 51
52static int vti_err(struct sk_buff *skb, u32 info)
53{
54
55 /* All the routers (except for Linux) return only
56 * 8 bytes of packet payload. It means, that precise relaying of
57 * ICMP in the real Internet is absolutely infeasible.
58 */
59 struct net *net = dev_net(skb->dev);
60 struct ip_tunnel_net *itn = net_generic(net, vti_net_id);
61 struct iphdr *iph = (struct iphdr *)skb->data;
62 const int type = icmp_hdr(skb)->type;
63 const int code = icmp_hdr(skb)->code;
64 struct ip_tunnel *t;
65 int err;
66
67 switch (type) {
68 default:
69 case ICMP_PARAMETERPROB:
70 return 0;
71
72 case ICMP_DEST_UNREACH:
73 switch (code) {
74 case ICMP_SR_FAILED:
75 case ICMP_PORT_UNREACH:
76 /* Impossible event. */
77 return 0;
78 default:
79 /* All others are translated to HOST_UNREACH. */
80 break;
81 }
82 break;
83 case ICMP_TIME_EXCEEDED:
84 if (code != ICMP_EXC_TTL)
85 return 0;
86 break;
87 }
88
89 err = -ENOENT;
90
91 t = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
92 iph->daddr, iph->saddr, 0);
93 if (t == NULL)
94 goto out;
95
96 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
97 ipv4_update_pmtu(skb, dev_net(skb->dev), info,
98 t->parms.link, 0, IPPROTO_IPIP, 0);
99 err = 0;
100 goto out;
101 }
102
103 err = 0;
104 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
105 goto out;
106
107 if (time_before(jiffies, t->err_time + IPTUNNEL_ERR_TIMEO))
108 t->err_count++;
109 else
110 t->err_count = 1;
111 t->err_time = jiffies;
112out:
113 return err;
114}
115
116/* We dont digest the packet therefore let the packet pass */ 52/* We dont digest the packet therefore let the packet pass */
117static int vti_rcv(struct sk_buff *skb) 53static int vti_rcv(struct sk_buff *skb)
118{ 54{
@@ -190,6 +126,7 @@ static netdev_tx_t vti_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
190 if (!rt->dst.xfrm || 126 if (!rt->dst.xfrm ||
191 rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) { 127 rt->dst.xfrm->props.mode != XFRM_MODE_TUNNEL) {
192 dev->stats.tx_carrier_errors++; 128 dev->stats.tx_carrier_errors++;
129 ip_rt_put(rt);
193 goto tx_error_icmp; 130 goto tx_error_icmp;
194 } 131 }
195 tdev = rt->dst.dev; 132 tdev = rt->dst.dev;
@@ -304,9 +241,8 @@ static void __net_init vti_fb_tunnel_init(struct net_device *dev)
304 iph->ihl = 5; 241 iph->ihl = 5;
305} 242}
306 243
307static struct xfrm_tunnel vti_handler __read_mostly = { 244static struct xfrm_tunnel_notifier vti_handler __read_mostly = {
308 .handler = vti_rcv, 245 .handler = vti_rcv,
309 .err_handler = vti_err,
310 .priority = 1, 246 .priority = 1,
311}; 247};
312 248
diff --git a/net/ipv4/ipip.c b/net/ipv4/ipip.c
index 7f80fb4b82d3..fe3e9f7f1f0b 100644
--- a/net/ipv4/ipip.c
+++ b/net/ipv4/ipip.c
@@ -220,17 +220,17 @@ static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
220 if (unlikely(skb->protocol != htons(ETH_P_IP))) 220 if (unlikely(skb->protocol != htons(ETH_P_IP)))
221 goto tx_error; 221 goto tx_error;
222 222
223 if (likely(!skb->encapsulation)) { 223 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
224 skb_reset_inner_headers(skb); 224 if (IS_ERR(skb))
225 skb->encapsulation = 1; 225 goto out;
226 }
227 226
228 ip_tunnel_xmit(skb, dev, tiph, tiph->protocol); 227 ip_tunnel_xmit(skb, dev, tiph, tiph->protocol);
229 return NETDEV_TX_OK; 228 return NETDEV_TX_OK;
230 229
231tx_error: 230tx_error:
232 dev->stats.tx_errors++;
233 dev_kfree_skb(skb); 231 dev_kfree_skb(skb);
232out:
233 dev->stats.tx_errors++;
234 return NETDEV_TX_OK; 234 return NETDEV_TX_OK;
235} 235}
236 236
@@ -275,6 +275,7 @@ static const struct net_device_ops ipip_netdev_ops = {
275#define IPIP_FEATURES (NETIF_F_SG | \ 275#define IPIP_FEATURES (NETIF_F_SG | \
276 NETIF_F_FRAGLIST | \ 276 NETIF_F_FRAGLIST | \
277 NETIF_F_HIGHDMA | \ 277 NETIF_F_HIGHDMA | \
278 NETIF_F_GSO_SOFTWARE | \
278 NETIF_F_HW_CSUM) 279 NETIF_F_HW_CSUM)
279 280
280static void ipip_tunnel_setup(struct net_device *dev) 281static void ipip_tunnel_setup(struct net_device *dev)
diff --git a/net/ipv4/ipmr.c b/net/ipv4/ipmr.c
index 62212c772a4b..1672409f5ba5 100644
--- a/net/ipv4/ipmr.c
+++ b/net/ipv4/ipmr.c
@@ -157,9 +157,12 @@ static struct mr_table *ipmr_get_table(struct net *net, u32 id)
157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 157static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
158 struct mr_table **mrt) 158 struct mr_table **mrt)
159{ 159{
160 struct ipmr_result res;
161 struct fib_lookup_arg arg = { .result = &res, };
162 int err; 160 int err;
161 struct ipmr_result res;
162 struct fib_lookup_arg arg = {
163 .result = &res,
164 .flags = FIB_LOOKUP_NOREF,
165 };
163 166
164 err = fib_rules_lookup(net->ipv4.mr_rules_ops, 167 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg); 168 flowi4_to_flowi(flp4), 0, &arg);
diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
index 1657e39b291f..40d56073cd19 100644
--- a/net/ipv4/netfilter/Kconfig
+++ b/net/ipv4/netfilter/Kconfig
@@ -36,6 +36,27 @@ config NF_CONNTRACK_PROC_COMPAT
36 36
37 If unsure, say Y. 37 If unsure, say Y.
38 38
39config NF_TABLES_IPV4
40 depends on NF_TABLES
41 tristate "IPv4 nf_tables support"
42
43config NFT_REJECT_IPV4
44 depends on NF_TABLES_IPV4
45 tristate "nf_tables IPv4 reject support"
46
47config NFT_CHAIN_ROUTE_IPV4
48 depends on NF_TABLES_IPV4
49 tristate "IPv4 nf_tables route chain support"
50
51config NFT_CHAIN_NAT_IPV4
52 depends on NF_TABLES_IPV4
53 depends on NF_NAT_IPV4 && NFT_NAT
54 tristate "IPv4 nf_tables nat chain support"
55
56config NF_TABLES_ARP
57 depends on NF_TABLES
58 tristate "ARP nf_tables support"
59
39config IP_NF_IPTABLES 60config IP_NF_IPTABLES
40 tristate "IP tables support (required for filtering/masq/NAT)" 61 tristate "IP tables support (required for filtering/masq/NAT)"
41 default m if NETFILTER_ADVANCED=n 62 default m if NETFILTER_ADVANCED=n
diff --git a/net/ipv4/netfilter/Makefile b/net/ipv4/netfilter/Makefile
index 3622b248b6dd..19df72b7ba88 100644
--- a/net/ipv4/netfilter/Makefile
+++ b/net/ipv4/netfilter/Makefile
@@ -27,6 +27,12 @@ obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
27# NAT protocols (nf_nat) 27# NAT protocols (nf_nat)
28obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o 28obj-$(CONFIG_NF_NAT_PROTO_GRE) += nf_nat_proto_gre.o
29 29
30obj-$(CONFIG_NF_TABLES_IPV4) += nf_tables_ipv4.o
31obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
32obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
33obj-$(CONFIG_NFT_CHAIN_NAT_IPV4) += nft_chain_nat_ipv4.o
34obj-$(CONFIG_NF_TABLES_ARP) += nf_tables_arp.o
35
30# generic IP tables 36# generic IP tables
31obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o 37obj-$(CONFIG_IP_NF_IPTABLES) += ip_tables.o
32 38
diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index 85a4f21aac1a..59da7cde0724 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -271,6 +271,11 @@ unsigned int arpt_do_table(struct sk_buff *skb,
271 local_bh_disable(); 271 local_bh_disable();
272 addend = xt_write_recseq_begin(); 272 addend = xt_write_recseq_begin();
273 private = table->private; 273 private = table->private;
274 /*
275 * Ensure we load private-> members after we've fetched the base
276 * pointer.
277 */
278 smp_read_barrier_depends();
274 table_base = private->entries[smp_processor_id()]; 279 table_base = private->entries[smp_processor_id()];
275 280
276 e = get_entry(table_base, private->hook_entry[hook]); 281 e = get_entry(table_base, private->hook_entry[hook]);
diff --git a/net/ipv4/netfilter/arptable_filter.c b/net/ipv4/netfilter/arptable_filter.c
index a865f6f94013..802ddecb30b8 100644
--- a/net/ipv4/netfilter/arptable_filter.c
+++ b/net/ipv4/netfilter/arptable_filter.c
@@ -27,13 +27,14 @@ static const struct xt_table packet_filter = {
27 27
28/* The work comes in here from netfilter.c */ 28/* The work comes in here from netfilter.c */
29static unsigned int 29static unsigned int
30arptable_filter_hook(unsigned int hook, struct sk_buff *skb, 30arptable_filter_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
31 const struct net_device *in, const struct net_device *out, 31 const struct net_device *in, const struct net_device *out,
32 int (*okfn)(struct sk_buff *)) 32 int (*okfn)(struct sk_buff *))
33{ 33{
34 const struct net *net = dev_net((in != NULL) ? in : out); 34 const struct net *net = dev_net((in != NULL) ? in : out);
35 35
36 return arpt_do_table(skb, hook, in, out, net->ipv4.arptable_filter); 36 return arpt_do_table(skb, ops->hooknum, in, out,
37 net->ipv4.arptable_filter);
37} 38}
38 39
39static struct nf_hook_ops *arpfilter_ops __read_mostly; 40static struct nf_hook_ops *arpfilter_ops __read_mostly;
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index d23118d95ff9..718dfbd30cbe 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -327,6 +327,11 @@ ipt_do_table(struct sk_buff *skb,
327 addend = xt_write_recseq_begin(); 327 addend = xt_write_recseq_begin();
328 private = table->private; 328 private = table->private;
329 cpu = smp_processor_id(); 329 cpu = smp_processor_id();
330 /*
331 * Ensure we load private-> members after we've fetched the base
332 * pointer.
333 */
334 smp_read_barrier_depends();
330 table_base = private->entries[cpu]; 335 table_base = private->entries[cpu];
331 jumpstack = (struct ipt_entry **)private->jumpstack[cpu]; 336 jumpstack = (struct ipt_entry **)private->jumpstack[cpu];
332 stackptr = per_cpu_ptr(private->stackptr, cpu); 337 stackptr = per_cpu_ptr(private->stackptr, cpu);
diff --git a/net/ipv4/netfilter/ipt_CLUSTERIP.c b/net/ipv4/netfilter/ipt_CLUSTERIP.c
index 0b732efd32e2..2510c02c2d21 100644
--- a/net/ipv4/netfilter/ipt_CLUSTERIP.c
+++ b/net/ipv4/netfilter/ipt_CLUSTERIP.c
@@ -28,6 +28,7 @@
28#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h> 28#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
29#include <net/netfilter/nf_conntrack.h> 29#include <net/netfilter/nf_conntrack.h>
30#include <net/net_namespace.h> 30#include <net/net_namespace.h>
31#include <net/netns/generic.h>
31#include <net/checksum.h> 32#include <net/checksum.h>
32#include <net/ip.h> 33#include <net/ip.h>
33 34
@@ -57,15 +58,21 @@ struct clusterip_config {
57 struct rcu_head rcu; 58 struct rcu_head rcu;
58}; 59};
59 60
60static LIST_HEAD(clusterip_configs); 61#ifdef CONFIG_PROC_FS
62static const struct file_operations clusterip_proc_fops;
63#endif
61 64
62/* clusterip_lock protects the clusterip_configs list */ 65static int clusterip_net_id __read_mostly;
63static DEFINE_SPINLOCK(clusterip_lock); 66
67struct clusterip_net {
68 struct list_head configs;
69 /* lock protects the configs list */
70 spinlock_t lock;
64 71
65#ifdef CONFIG_PROC_FS 72#ifdef CONFIG_PROC_FS
66static const struct file_operations clusterip_proc_fops; 73 struct proc_dir_entry *procdir;
67static struct proc_dir_entry *clusterip_procdir;
68#endif 74#endif
75};
69 76
70static inline void 77static inline void
71clusterip_config_get(struct clusterip_config *c) 78clusterip_config_get(struct clusterip_config *c)
@@ -92,10 +99,13 @@ clusterip_config_put(struct clusterip_config *c)
92static inline void 99static inline void
93clusterip_config_entry_put(struct clusterip_config *c) 100clusterip_config_entry_put(struct clusterip_config *c)
94{ 101{
102 struct net *net = dev_net(c->dev);
103 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
104
95 local_bh_disable(); 105 local_bh_disable();
96 if (atomic_dec_and_lock(&c->entries, &clusterip_lock)) { 106 if (atomic_dec_and_lock(&c->entries, &cn->lock)) {
97 list_del_rcu(&c->list); 107 list_del_rcu(&c->list);
98 spin_unlock(&clusterip_lock); 108 spin_unlock(&cn->lock);
99 local_bh_enable(); 109 local_bh_enable();
100 110
101 dev_mc_del(c->dev, c->clustermac); 111 dev_mc_del(c->dev, c->clustermac);
@@ -113,11 +123,12 @@ clusterip_config_entry_put(struct clusterip_config *c)
113} 123}
114 124
115static struct clusterip_config * 125static struct clusterip_config *
116__clusterip_config_find(__be32 clusterip) 126__clusterip_config_find(struct net *net, __be32 clusterip)
117{ 127{
118 struct clusterip_config *c; 128 struct clusterip_config *c;
129 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
119 130
120 list_for_each_entry_rcu(c, &clusterip_configs, list) { 131 list_for_each_entry_rcu(c, &cn->configs, list) {
121 if (c->clusterip == clusterip) 132 if (c->clusterip == clusterip)
122 return c; 133 return c;
123 } 134 }
@@ -126,12 +137,12 @@ __clusterip_config_find(__be32 clusterip)
126} 137}
127 138
128static inline struct clusterip_config * 139static inline struct clusterip_config *
129clusterip_config_find_get(__be32 clusterip, int entry) 140clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
130{ 141{
131 struct clusterip_config *c; 142 struct clusterip_config *c;
132 143
133 rcu_read_lock_bh(); 144 rcu_read_lock_bh();
134 c = __clusterip_config_find(clusterip); 145 c = __clusterip_config_find(net, clusterip);
135 if (c) { 146 if (c) {
136 if (unlikely(!atomic_inc_not_zero(&c->refcount))) 147 if (unlikely(!atomic_inc_not_zero(&c->refcount)))
137 c = NULL; 148 c = NULL;
@@ -158,6 +169,7 @@ clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
158 struct net_device *dev) 169 struct net_device *dev)
159{ 170{
160 struct clusterip_config *c; 171 struct clusterip_config *c;
172 struct clusterip_net *cn = net_generic(dev_net(dev), clusterip_net_id);
161 173
162 c = kzalloc(sizeof(*c), GFP_ATOMIC); 174 c = kzalloc(sizeof(*c), GFP_ATOMIC);
163 if (!c) 175 if (!c)
@@ -180,7 +192,7 @@ clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
180 /* create proc dir entry */ 192 /* create proc dir entry */
181 sprintf(buffer, "%pI4", &ip); 193 sprintf(buffer, "%pI4", &ip);
182 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR, 194 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR,
183 clusterip_procdir, 195 cn->procdir,
184 &clusterip_proc_fops, c); 196 &clusterip_proc_fops, c);
185 if (!c->pde) { 197 if (!c->pde) {
186 kfree(c); 198 kfree(c);
@@ -189,9 +201,9 @@ clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
189 } 201 }
190#endif 202#endif
191 203
192 spin_lock_bh(&clusterip_lock); 204 spin_lock_bh(&cn->lock);
193 list_add_rcu(&c->list, &clusterip_configs); 205 list_add_rcu(&c->list, &cn->configs);
194 spin_unlock_bh(&clusterip_lock); 206 spin_unlock_bh(&cn->lock);
195 207
196 return c; 208 return c;
197} 209}
@@ -370,7 +382,7 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)
370 382
371 /* FIXME: further sanity checks */ 383 /* FIXME: further sanity checks */
372 384
373 config = clusterip_config_find_get(e->ip.dst.s_addr, 1); 385 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
374 if (!config) { 386 if (!config) {
375 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) { 387 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
376 pr_info("no config found for %pI4, need 'new'\n", 388 pr_info("no config found for %pI4, need 'new'\n",
@@ -384,7 +396,7 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)
384 return -EINVAL; 396 return -EINVAL;
385 } 397 }
386 398
387 dev = dev_get_by_name(&init_net, e->ip.iniface); 399 dev = dev_get_by_name(par->net, e->ip.iniface);
388 if (!dev) { 400 if (!dev) {
389 pr_info("no such interface %s\n", 401 pr_info("no such interface %s\n",
390 e->ip.iniface); 402 e->ip.iniface);
@@ -483,7 +495,7 @@ static void arp_print(struct arp_payload *payload)
483#endif 495#endif
484 496
485static unsigned int 497static unsigned int
486arp_mangle(unsigned int hook, 498arp_mangle(const struct nf_hook_ops *ops,
487 struct sk_buff *skb, 499 struct sk_buff *skb,
488 const struct net_device *in, 500 const struct net_device *in,
489 const struct net_device *out, 501 const struct net_device *out,
@@ -492,6 +504,7 @@ arp_mangle(unsigned int hook,
492 struct arphdr *arp = arp_hdr(skb); 504 struct arphdr *arp = arp_hdr(skb);
493 struct arp_payload *payload; 505 struct arp_payload *payload;
494 struct clusterip_config *c; 506 struct clusterip_config *c;
507 struct net *net = dev_net(in ? in : out);
495 508
496 /* we don't care about non-ethernet and non-ipv4 ARP */ 509 /* we don't care about non-ethernet and non-ipv4 ARP */
497 if (arp->ar_hrd != htons(ARPHRD_ETHER) || 510 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
@@ -508,7 +521,7 @@ arp_mangle(unsigned int hook,
508 521
509 /* if there is no clusterip configuration for the arp reply's 522 /* if there is no clusterip configuration for the arp reply's
510 * source ip, we don't want to mangle it */ 523 * source ip, we don't want to mangle it */
511 c = clusterip_config_find_get(payload->src_ip, 0); 524 c = clusterip_config_find_get(net, payload->src_ip, 0);
512 if (!c) 525 if (!c)
513 return NF_ACCEPT; 526 return NF_ACCEPT;
514 527
@@ -698,48 +711,75 @@ static const struct file_operations clusterip_proc_fops = {
698 711
699#endif /* CONFIG_PROC_FS */ 712#endif /* CONFIG_PROC_FS */
700 713
714static int clusterip_net_init(struct net *net)
715{
716 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
717
718 INIT_LIST_HEAD(&cn->configs);
719
720 spin_lock_init(&cn->lock);
721
722#ifdef CONFIG_PROC_FS
723 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
724 if (!cn->procdir) {
725 pr_err("Unable to proc dir entry\n");
726 return -ENOMEM;
727 }
728#endif /* CONFIG_PROC_FS */
729
730 return 0;
731}
732
733static void clusterip_net_exit(struct net *net)
734{
735#ifdef CONFIG_PROC_FS
736 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
737 proc_remove(cn->procdir);
738#endif
739}
740
741static struct pernet_operations clusterip_net_ops = {
742 .init = clusterip_net_init,
743 .exit = clusterip_net_exit,
744 .id = &clusterip_net_id,
745 .size = sizeof(struct clusterip_net),
746};
747
701static int __init clusterip_tg_init(void) 748static int __init clusterip_tg_init(void)
702{ 749{
703 int ret; 750 int ret;
704 751
705 ret = xt_register_target(&clusterip_tg_reg); 752 ret = register_pernet_subsys(&clusterip_net_ops);
706 if (ret < 0) 753 if (ret < 0)
707 return ret; 754 return ret;
708 755
756 ret = xt_register_target(&clusterip_tg_reg);
757 if (ret < 0)
758 goto cleanup_subsys;
759
709 ret = nf_register_hook(&cip_arp_ops); 760 ret = nf_register_hook(&cip_arp_ops);
710 if (ret < 0) 761 if (ret < 0)
711 goto cleanup_target; 762 goto cleanup_target;
712 763
713#ifdef CONFIG_PROC_FS
714 clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", init_net.proc_net);
715 if (!clusterip_procdir) {
716 pr_err("Unable to proc dir entry\n");
717 ret = -ENOMEM;
718 goto cleanup_hook;
719 }
720#endif /* CONFIG_PROC_FS */
721
722 pr_info("ClusterIP Version %s loaded successfully\n", 764 pr_info("ClusterIP Version %s loaded successfully\n",
723 CLUSTERIP_VERSION); 765 CLUSTERIP_VERSION);
766
724 return 0; 767 return 0;
725 768
726#ifdef CONFIG_PROC_FS
727cleanup_hook:
728 nf_unregister_hook(&cip_arp_ops);
729#endif /* CONFIG_PROC_FS */
730cleanup_target: 769cleanup_target:
731 xt_unregister_target(&clusterip_tg_reg); 770 xt_unregister_target(&clusterip_tg_reg);
771cleanup_subsys:
772 unregister_pernet_subsys(&clusterip_net_ops);
732 return ret; 773 return ret;
733} 774}
734 775
735static void __exit clusterip_tg_exit(void) 776static void __exit clusterip_tg_exit(void)
736{ 777{
737 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION); 778 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
738#ifdef CONFIG_PROC_FS 779
739 proc_remove(clusterip_procdir);
740#endif
741 nf_unregister_hook(&cip_arp_ops); 780 nf_unregister_hook(&cip_arp_ops);
742 xt_unregister_target(&clusterip_tg_reg); 781 xt_unregister_target(&clusterip_tg_reg);
782 unregister_pernet_subsys(&clusterip_net_ops);
743 783
744 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */ 784 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
745 rcu_barrier_bh(); 785 rcu_barrier_bh();
diff --git a/net/ipv4/netfilter/ipt_SYNPROXY.c b/net/ipv4/netfilter/ipt_SYNPROXY.c
index b6346bf2fde3..a313c3fbeb46 100644
--- a/net/ipv4/netfilter/ipt_SYNPROXY.c
+++ b/net/ipv4/netfilter/ipt_SYNPROXY.c
@@ -244,6 +244,7 @@ synproxy_recv_client_ack(const struct synproxy_net *snet,
244 244
245 this_cpu_inc(snet->stats->cookie_valid); 245 this_cpu_inc(snet->stats->cookie_valid);
246 opts->mss = mss; 246 opts->mss = mss;
247 opts->options |= XT_SYNPROXY_OPT_MSS;
247 248
248 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP) 249 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
249 synproxy_check_timestamp_cookie(opts); 250 synproxy_check_timestamp_cookie(opts);
@@ -297,7 +298,7 @@ synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
297 return XT_CONTINUE; 298 return XT_CONTINUE;
298} 299}
299 300
300static unsigned int ipv4_synproxy_hook(unsigned int hooknum, 301static unsigned int ipv4_synproxy_hook(const struct nf_hook_ops *ops,
301 struct sk_buff *skb, 302 struct sk_buff *skb,
302 const struct net_device *in, 303 const struct net_device *in,
303 const struct net_device *out, 304 const struct net_device *out,
@@ -422,6 +423,7 @@ static void synproxy_tg4_destroy(const struct xt_tgdtor_param *par)
422static struct xt_target synproxy_tg4_reg __read_mostly = { 423static struct xt_target synproxy_tg4_reg __read_mostly = {
423 .name = "SYNPROXY", 424 .name = "SYNPROXY",
424 .family = NFPROTO_IPV4, 425 .family = NFPROTO_IPV4,
426 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
425 .target = synproxy_tg4, 427 .target = synproxy_tg4,
426 .targetsize = sizeof(struct xt_synproxy_info), 428 .targetsize = sizeof(struct xt_synproxy_info),
427 .checkentry = synproxy_tg4_check, 429 .checkentry = synproxy_tg4_check,
diff --git a/net/ipv4/netfilter/ipt_ULOG.c b/net/ipv4/netfilter/ipt_ULOG.c
index cbc22158af49..9cb993cd224b 100644
--- a/net/ipv4/netfilter/ipt_ULOG.c
+++ b/net/ipv4/netfilter/ipt_ULOG.c
@@ -220,6 +220,7 @@ static void ipt_ulog_packet(struct net *net,
220 ub->qlen++; 220 ub->qlen++;
221 221
222 pm = nlmsg_data(nlh); 222 pm = nlmsg_data(nlh);
223 memset(pm, 0, sizeof(*pm));
223 224
224 /* We might not have a timestamp, get one */ 225 /* We might not have a timestamp, get one */
225 if (skb->tstamp.tv64 == 0) 226 if (skb->tstamp.tv64 == 0)
@@ -238,8 +239,6 @@ static void ipt_ulog_packet(struct net *net,
238 } 239 }
239 else if (loginfo->prefix[0] != '\0') 240 else if (loginfo->prefix[0] != '\0')
240 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix)); 241 strncpy(pm->prefix, loginfo->prefix, sizeof(pm->prefix));
241 else
242 *(pm->prefix) = '\0';
243 242
244 if (in && in->hard_header_len > 0 && 243 if (in && in->hard_header_len > 0 &&
245 skb->mac_header != skb->network_header && 244 skb->mac_header != skb->network_header &&
@@ -251,13 +250,9 @@ static void ipt_ulog_packet(struct net *net,
251 250
252 if (in) 251 if (in)
253 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name)); 252 strncpy(pm->indev_name, in->name, sizeof(pm->indev_name));
254 else
255 pm->indev_name[0] = '\0';
256 253
257 if (out) 254 if (out)
258 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name)); 255 strncpy(pm->outdev_name, out->name, sizeof(pm->outdev_name));
259 else
260 pm->outdev_name[0] = '\0';
261 256
262 /* copy_len <= skb->len, so can't fail. */ 257 /* copy_len <= skb->len, so can't fail. */
263 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0) 258 if (skb_copy_bits(skb, 0, pm->payload, copy_len) < 0)
diff --git a/net/ipv4/netfilter/iptable_filter.c b/net/ipv4/netfilter/iptable_filter.c
index 50af5b45c050..e08a74a243a8 100644
--- a/net/ipv4/netfilter/iptable_filter.c
+++ b/net/ipv4/netfilter/iptable_filter.c
@@ -33,20 +33,21 @@ static const struct xt_table packet_filter = {
33}; 33};
34 34
35static unsigned int 35static unsigned int
36iptable_filter_hook(unsigned int hook, struct sk_buff *skb, 36iptable_filter_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
37 const struct net_device *in, const struct net_device *out, 37 const struct net_device *in, const struct net_device *out,
38 int (*okfn)(struct sk_buff *)) 38 int (*okfn)(struct sk_buff *))
39{ 39{
40 const struct net *net; 40 const struct net *net;
41 41
42 if (hook == NF_INET_LOCAL_OUT && 42 if (ops->hooknum == NF_INET_LOCAL_OUT &&
43 (skb->len < sizeof(struct iphdr) || 43 (skb->len < sizeof(struct iphdr) ||
44 ip_hdrlen(skb) < sizeof(struct iphdr))) 44 ip_hdrlen(skb) < sizeof(struct iphdr)))
45 /* root is playing with raw sockets. */ 45 /* root is playing with raw sockets. */
46 return NF_ACCEPT; 46 return NF_ACCEPT;
47 47
48 net = dev_net((in != NULL) ? in : out); 48 net = dev_net((in != NULL) ? in : out);
49 return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_filter); 49 return ipt_do_table(skb, ops->hooknum, in, out,
50 net->ipv4.iptable_filter);
50} 51}
51 52
52static struct nf_hook_ops *filter_ops __read_mostly; 53static struct nf_hook_ops *filter_ops __read_mostly;
diff --git a/net/ipv4/netfilter/iptable_mangle.c b/net/ipv4/netfilter/iptable_mangle.c
index 0d8cd82e0fad..6a5079c34bb3 100644
--- a/net/ipv4/netfilter/iptable_mangle.c
+++ b/net/ipv4/netfilter/iptable_mangle.c
@@ -79,19 +79,19 @@ ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
79 79
80/* The work comes in here from netfilter.c. */ 80/* The work comes in here from netfilter.c. */
81static unsigned int 81static unsigned int
82iptable_mangle_hook(unsigned int hook, 82iptable_mangle_hook(const struct nf_hook_ops *ops,
83 struct sk_buff *skb, 83 struct sk_buff *skb,
84 const struct net_device *in, 84 const struct net_device *in,
85 const struct net_device *out, 85 const struct net_device *out,
86 int (*okfn)(struct sk_buff *)) 86 int (*okfn)(struct sk_buff *))
87{ 87{
88 if (hook == NF_INET_LOCAL_OUT) 88 if (ops->hooknum == NF_INET_LOCAL_OUT)
89 return ipt_mangle_out(skb, out); 89 return ipt_mangle_out(skb, out);
90 if (hook == NF_INET_POST_ROUTING) 90 if (ops->hooknum == NF_INET_POST_ROUTING)
91 return ipt_do_table(skb, hook, in, out, 91 return ipt_do_table(skb, ops->hooknum, in, out,
92 dev_net(out)->ipv4.iptable_mangle); 92 dev_net(out)->ipv4.iptable_mangle);
93 /* PREROUTING/INPUT/FORWARD: */ 93 /* PREROUTING/INPUT/FORWARD: */
94 return ipt_do_table(skb, hook, in, out, 94 return ipt_do_table(skb, ops->hooknum, in, out,
95 dev_net(in)->ipv4.iptable_mangle); 95 dev_net(in)->ipv4.iptable_mangle);
96} 96}
97 97
diff --git a/net/ipv4/netfilter/iptable_nat.c b/net/ipv4/netfilter/iptable_nat.c
index 683bfaffed65..ee2886126e3d 100644
--- a/net/ipv4/netfilter/iptable_nat.c
+++ b/net/ipv4/netfilter/iptable_nat.c
@@ -61,7 +61,7 @@ static unsigned int nf_nat_rule_find(struct sk_buff *skb, unsigned int hooknum,
61} 61}
62 62
63static unsigned int 63static unsigned int
64nf_nat_ipv4_fn(unsigned int hooknum, 64nf_nat_ipv4_fn(const struct nf_hook_ops *ops,
65 struct sk_buff *skb, 65 struct sk_buff *skb,
66 const struct net_device *in, 66 const struct net_device *in,
67 const struct net_device *out, 67 const struct net_device *out,
@@ -71,7 +71,7 @@ nf_nat_ipv4_fn(unsigned int hooknum,
71 enum ip_conntrack_info ctinfo; 71 enum ip_conntrack_info ctinfo;
72 struct nf_conn_nat *nat; 72 struct nf_conn_nat *nat;
73 /* maniptype == SRC for postrouting. */ 73 /* maniptype == SRC for postrouting. */
74 enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum); 74 enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
75 75
76 /* We never see fragments: conntrack defrags on pre-routing 76 /* We never see fragments: conntrack defrags on pre-routing
77 * and local-out, and nf_nat_out protects post-routing. 77 * and local-out, and nf_nat_out protects post-routing.
@@ -108,7 +108,7 @@ nf_nat_ipv4_fn(unsigned int hooknum,
108 case IP_CT_RELATED_REPLY: 108 case IP_CT_RELATED_REPLY:
109 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) { 109 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
110 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo, 110 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
111 hooknum)) 111 ops->hooknum))
112 return NF_DROP; 112 return NF_DROP;
113 else 113 else
114 return NF_ACCEPT; 114 return NF_ACCEPT;
@@ -121,14 +121,14 @@ nf_nat_ipv4_fn(unsigned int hooknum,
121 if (!nf_nat_initialized(ct, maniptype)) { 121 if (!nf_nat_initialized(ct, maniptype)) {
122 unsigned int ret; 122 unsigned int ret;
123 123
124 ret = nf_nat_rule_find(skb, hooknum, in, out, ct); 124 ret = nf_nat_rule_find(skb, ops->hooknum, in, out, ct);
125 if (ret != NF_ACCEPT) 125 if (ret != NF_ACCEPT)
126 return ret; 126 return ret;
127 } else { 127 } else {
128 pr_debug("Already setup manip %s for ct %p\n", 128 pr_debug("Already setup manip %s for ct %p\n",
129 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST", 129 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
130 ct); 130 ct);
131 if (nf_nat_oif_changed(hooknum, ctinfo, nat, out)) 131 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
132 goto oif_changed; 132 goto oif_changed;
133 } 133 }
134 break; 134 break;
@@ -137,11 +137,11 @@ nf_nat_ipv4_fn(unsigned int hooknum,
137 /* ESTABLISHED */ 137 /* ESTABLISHED */
138 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || 138 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
139 ctinfo == IP_CT_ESTABLISHED_REPLY); 139 ctinfo == IP_CT_ESTABLISHED_REPLY);
140 if (nf_nat_oif_changed(hooknum, ctinfo, nat, out)) 140 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
141 goto oif_changed; 141 goto oif_changed;
142 } 142 }
143 143
144 return nf_nat_packet(ct, ctinfo, hooknum, skb); 144 return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
145 145
146oif_changed: 146oif_changed:
147 nf_ct_kill_acct(ct, ctinfo, skb); 147 nf_ct_kill_acct(ct, ctinfo, skb);
@@ -149,7 +149,7 @@ oif_changed:
149} 149}
150 150
151static unsigned int 151static unsigned int
152nf_nat_ipv4_in(unsigned int hooknum, 152nf_nat_ipv4_in(const struct nf_hook_ops *ops,
153 struct sk_buff *skb, 153 struct sk_buff *skb,
154 const struct net_device *in, 154 const struct net_device *in,
155 const struct net_device *out, 155 const struct net_device *out,
@@ -158,7 +158,7 @@ nf_nat_ipv4_in(unsigned int hooknum,
158 unsigned int ret; 158 unsigned int ret;
159 __be32 daddr = ip_hdr(skb)->daddr; 159 __be32 daddr = ip_hdr(skb)->daddr;
160 160
161 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn); 161 ret = nf_nat_ipv4_fn(ops, skb, in, out, okfn);
162 if (ret != NF_DROP && ret != NF_STOLEN && 162 if (ret != NF_DROP && ret != NF_STOLEN &&
163 daddr != ip_hdr(skb)->daddr) 163 daddr != ip_hdr(skb)->daddr)
164 skb_dst_drop(skb); 164 skb_dst_drop(skb);
@@ -167,7 +167,7 @@ nf_nat_ipv4_in(unsigned int hooknum,
167} 167}
168 168
169static unsigned int 169static unsigned int
170nf_nat_ipv4_out(unsigned int hooknum, 170nf_nat_ipv4_out(const struct nf_hook_ops *ops,
171 struct sk_buff *skb, 171 struct sk_buff *skb,
172 const struct net_device *in, 172 const struct net_device *in,
173 const struct net_device *out, 173 const struct net_device *out,
@@ -185,7 +185,7 @@ nf_nat_ipv4_out(unsigned int hooknum,
185 ip_hdrlen(skb) < sizeof(struct iphdr)) 185 ip_hdrlen(skb) < sizeof(struct iphdr))
186 return NF_ACCEPT; 186 return NF_ACCEPT;
187 187
188 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn); 188 ret = nf_nat_ipv4_fn(ops, skb, in, out, okfn);
189#ifdef CONFIG_XFRM 189#ifdef CONFIG_XFRM
190 if (ret != NF_DROP && ret != NF_STOLEN && 190 if (ret != NF_DROP && ret != NF_STOLEN &&
191 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) && 191 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
@@ -207,7 +207,7 @@ nf_nat_ipv4_out(unsigned int hooknum,
207} 207}
208 208
209static unsigned int 209static unsigned int
210nf_nat_ipv4_local_fn(unsigned int hooknum, 210nf_nat_ipv4_local_fn(const struct nf_hook_ops *ops,
211 struct sk_buff *skb, 211 struct sk_buff *skb,
212 const struct net_device *in, 212 const struct net_device *in,
213 const struct net_device *out, 213 const struct net_device *out,
@@ -223,7 +223,7 @@ nf_nat_ipv4_local_fn(unsigned int hooknum,
223 ip_hdrlen(skb) < sizeof(struct iphdr)) 223 ip_hdrlen(skb) < sizeof(struct iphdr))
224 return NF_ACCEPT; 224 return NF_ACCEPT;
225 225
226 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn); 226 ret = nf_nat_ipv4_fn(ops, skb, in, out, okfn);
227 if (ret != NF_DROP && ret != NF_STOLEN && 227 if (ret != NF_DROP && ret != NF_STOLEN &&
228 (ct = nf_ct_get(skb, &ctinfo)) != NULL) { 228 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
229 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); 229 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
diff --git a/net/ipv4/netfilter/iptable_raw.c b/net/ipv4/netfilter/iptable_raw.c
index 1f82aea11df6..b2f7e8f98316 100644
--- a/net/ipv4/netfilter/iptable_raw.c
+++ b/net/ipv4/netfilter/iptable_raw.c
@@ -20,20 +20,20 @@ static const struct xt_table packet_raw = {
20 20
21/* The work comes in here from netfilter.c. */ 21/* The work comes in here from netfilter.c. */
22static unsigned int 22static unsigned int
23iptable_raw_hook(unsigned int hook, struct sk_buff *skb, 23iptable_raw_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
24 const struct net_device *in, const struct net_device *out, 24 const struct net_device *in, const struct net_device *out,
25 int (*okfn)(struct sk_buff *)) 25 int (*okfn)(struct sk_buff *))
26{ 26{
27 const struct net *net; 27 const struct net *net;
28 28
29 if (hook == NF_INET_LOCAL_OUT && 29 if (ops->hooknum == NF_INET_LOCAL_OUT &&
30 (skb->len < sizeof(struct iphdr) || 30 (skb->len < sizeof(struct iphdr) ||
31 ip_hdrlen(skb) < sizeof(struct iphdr))) 31 ip_hdrlen(skb) < sizeof(struct iphdr)))
32 /* root is playing with raw sockets. */ 32 /* root is playing with raw sockets. */
33 return NF_ACCEPT; 33 return NF_ACCEPT;
34 34
35 net = dev_net((in != NULL) ? in : out); 35 net = dev_net((in != NULL) ? in : out);
36 return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_raw); 36 return ipt_do_table(skb, ops->hooknum, in, out, net->ipv4.iptable_raw);
37} 37}
38 38
39static struct nf_hook_ops *rawtable_ops __read_mostly; 39static struct nf_hook_ops *rawtable_ops __read_mostly;
diff --git a/net/ipv4/netfilter/iptable_security.c b/net/ipv4/netfilter/iptable_security.c
index f867a8d38bf7..c86647ed2078 100644
--- a/net/ipv4/netfilter/iptable_security.c
+++ b/net/ipv4/netfilter/iptable_security.c
@@ -37,21 +37,22 @@ static const struct xt_table security_table = {
37}; 37};
38 38
39static unsigned int 39static unsigned int
40iptable_security_hook(unsigned int hook, struct sk_buff *skb, 40iptable_security_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
41 const struct net_device *in, 41 const struct net_device *in,
42 const struct net_device *out, 42 const struct net_device *out,
43 int (*okfn)(struct sk_buff *)) 43 int (*okfn)(struct sk_buff *))
44{ 44{
45 const struct net *net; 45 const struct net *net;
46 46
47 if (hook == NF_INET_LOCAL_OUT && 47 if (ops->hooknum == NF_INET_LOCAL_OUT &&
48 (skb->len < sizeof(struct iphdr) || 48 (skb->len < sizeof(struct iphdr) ||
49 ip_hdrlen(skb) < sizeof(struct iphdr))) 49 ip_hdrlen(skb) < sizeof(struct iphdr)))
50 /* Somebody is playing with raw sockets. */ 50 /* Somebody is playing with raw sockets. */
51 return NF_ACCEPT; 51 return NF_ACCEPT;
52 52
53 net = dev_net((in != NULL) ? in : out); 53 net = dev_net((in != NULL) ? in : out);
54 return ipt_do_table(skb, hook, in, out, net->ipv4.iptable_security); 54 return ipt_do_table(skb, ops->hooknum, in, out,
55 net->ipv4.iptable_security);
55} 56}
56 57
57static struct nf_hook_ops *sectbl_ops __read_mostly; 58static struct nf_hook_ops *sectbl_ops __read_mostly;
diff --git a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
index 86f5b34a4ed1..ecd8bec411c9 100644
--- a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
+++ b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
@@ -92,7 +92,7 @@ static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
92 return NF_ACCEPT; 92 return NF_ACCEPT;
93} 93}
94 94
95static unsigned int ipv4_helper(unsigned int hooknum, 95static unsigned int ipv4_helper(const struct nf_hook_ops *ops,
96 struct sk_buff *skb, 96 struct sk_buff *skb,
97 const struct net_device *in, 97 const struct net_device *in,
98 const struct net_device *out, 98 const struct net_device *out,
@@ -121,7 +121,7 @@ static unsigned int ipv4_helper(unsigned int hooknum,
121 ct, ctinfo); 121 ct, ctinfo);
122} 122}
123 123
124static unsigned int ipv4_confirm(unsigned int hooknum, 124static unsigned int ipv4_confirm(const struct nf_hook_ops *ops,
125 struct sk_buff *skb, 125 struct sk_buff *skb,
126 const struct net_device *in, 126 const struct net_device *in,
127 const struct net_device *out, 127 const struct net_device *out,
@@ -147,16 +147,16 @@ out:
147 return nf_conntrack_confirm(skb); 147 return nf_conntrack_confirm(skb);
148} 148}
149 149
150static unsigned int ipv4_conntrack_in(unsigned int hooknum, 150static unsigned int ipv4_conntrack_in(const struct nf_hook_ops *ops,
151 struct sk_buff *skb, 151 struct sk_buff *skb,
152 const struct net_device *in, 152 const struct net_device *in,
153 const struct net_device *out, 153 const struct net_device *out,
154 int (*okfn)(struct sk_buff *)) 154 int (*okfn)(struct sk_buff *))
155{ 155{
156 return nf_conntrack_in(dev_net(in), PF_INET, hooknum, skb); 156 return nf_conntrack_in(dev_net(in), PF_INET, ops->hooknum, skb);
157} 157}
158 158
159static unsigned int ipv4_conntrack_local(unsigned int hooknum, 159static unsigned int ipv4_conntrack_local(const struct nf_hook_ops *ops,
160 struct sk_buff *skb, 160 struct sk_buff *skb,
161 const struct net_device *in, 161 const struct net_device *in,
162 const struct net_device *out, 162 const struct net_device *out,
@@ -166,7 +166,7 @@ static unsigned int ipv4_conntrack_local(unsigned int hooknum,
166 if (skb->len < sizeof(struct iphdr) || 166 if (skb->len < sizeof(struct iphdr) ||
167 ip_hdrlen(skb) < sizeof(struct iphdr)) 167 ip_hdrlen(skb) < sizeof(struct iphdr))
168 return NF_ACCEPT; 168 return NF_ACCEPT;
169 return nf_conntrack_in(dev_net(out), PF_INET, hooknum, skb); 169 return nf_conntrack_in(dev_net(out), PF_INET, ops->hooknum, skb);
170} 170}
171 171
172/* Connection tracking may drop packets, but never alters them, so 172/* Connection tracking may drop packets, but never alters them, so
diff --git a/net/ipv4/netfilter/nf_defrag_ipv4.c b/net/ipv4/netfilter/nf_defrag_ipv4.c
index 742815518b0f..12e13bd82b5b 100644
--- a/net/ipv4/netfilter/nf_defrag_ipv4.c
+++ b/net/ipv4/netfilter/nf_defrag_ipv4.c
@@ -60,7 +60,7 @@ static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
60 return IP_DEFRAG_CONNTRACK_OUT + zone; 60 return IP_DEFRAG_CONNTRACK_OUT + zone;
61} 61}
62 62
63static unsigned int ipv4_conntrack_defrag(unsigned int hooknum, 63static unsigned int ipv4_conntrack_defrag(const struct nf_hook_ops *ops,
64 struct sk_buff *skb, 64 struct sk_buff *skb,
65 const struct net_device *in, 65 const struct net_device *in,
66 const struct net_device *out, 66 const struct net_device *out,
@@ -83,7 +83,9 @@ static unsigned int ipv4_conntrack_defrag(unsigned int hooknum,
83#endif 83#endif
84 /* Gather fragments. */ 84 /* Gather fragments. */
85 if (ip_is_fragment(ip_hdr(skb))) { 85 if (ip_is_fragment(ip_hdr(skb))) {
86 enum ip_defrag_users user = nf_ct_defrag_user(hooknum, skb); 86 enum ip_defrag_users user =
87 nf_ct_defrag_user(ops->hooknum, skb);
88
87 if (nf_ct_ipv4_gather_frags(skb, user)) 89 if (nf_ct_ipv4_gather_frags(skb, user))
88 return NF_STOLEN; 90 return NF_STOLEN;
89 } 91 }
diff --git a/net/ipv4/netfilter/nf_tables_arp.c b/net/ipv4/netfilter/nf_tables_arp.c
new file mode 100644
index 000000000000..3e67ef1c676f
--- /dev/null
+++ b/net/ipv4/netfilter/nf_tables_arp.c
@@ -0,0 +1,102 @@
1/*
2 * Copyright (c) 2008-2010 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2013 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/netfilter_arp.h>
15#include <net/netfilter/nf_tables.h>
16
17static struct nft_af_info nft_af_arp __read_mostly = {
18 .family = NFPROTO_ARP,
19 .nhooks = NF_ARP_NUMHOOKS,
20 .owner = THIS_MODULE,
21};
22
23static int nf_tables_arp_init_net(struct net *net)
24{
25 net->nft.arp = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
26 if (net->nft.arp== NULL)
27 return -ENOMEM;
28
29 memcpy(net->nft.arp, &nft_af_arp, sizeof(nft_af_arp));
30
31 if (nft_register_afinfo(net, net->nft.arp) < 0)
32 goto err;
33
34 return 0;
35err:
36 kfree(net->nft.arp);
37 return -ENOMEM;
38}
39
40static void nf_tables_arp_exit_net(struct net *net)
41{
42 nft_unregister_afinfo(net->nft.arp);
43 kfree(net->nft.arp);
44}
45
46static struct pernet_operations nf_tables_arp_net_ops = {
47 .init = nf_tables_arp_init_net,
48 .exit = nf_tables_arp_exit_net,
49};
50
51static unsigned int
52nft_do_chain_arp(const struct nf_hook_ops *ops,
53 struct sk_buff *skb,
54 const struct net_device *in,
55 const struct net_device *out,
56 int (*okfn)(struct sk_buff *))
57{
58 struct nft_pktinfo pkt;
59
60 nft_set_pktinfo(&pkt, ops, skb, in, out);
61
62 return nft_do_chain_pktinfo(&pkt, ops);
63}
64
65static struct nf_chain_type filter_arp = {
66 .family = NFPROTO_ARP,
67 .name = "filter",
68 .type = NFT_CHAIN_T_DEFAULT,
69 .hook_mask = (1 << NF_ARP_IN) |
70 (1 << NF_ARP_OUT) |
71 (1 << NF_ARP_FORWARD),
72 .fn = {
73 [NF_ARP_IN] = nft_do_chain_arp,
74 [NF_ARP_OUT] = nft_do_chain_arp,
75 [NF_ARP_FORWARD] = nft_do_chain_arp,
76 },
77};
78
79static int __init nf_tables_arp_init(void)
80{
81 int ret;
82
83 nft_register_chain_type(&filter_arp);
84 ret = register_pernet_subsys(&nf_tables_arp_net_ops);
85 if (ret < 0)
86 nft_unregister_chain_type(&filter_arp);
87
88 return ret;
89}
90
91static void __exit nf_tables_arp_exit(void)
92{
93 unregister_pernet_subsys(&nf_tables_arp_net_ops);
94 nft_unregister_chain_type(&filter_arp);
95}
96
97module_init(nf_tables_arp_init);
98module_exit(nf_tables_arp_exit);
99
100MODULE_LICENSE("GPL");
101MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
102MODULE_ALIAS_NFT_FAMILY(3); /* NFPROTO_ARP */
diff --git a/net/ipv4/netfilter/nf_tables_ipv4.c b/net/ipv4/netfilter/nf_tables_ipv4.c
new file mode 100644
index 000000000000..0f4cbfeb19bd
--- /dev/null
+++ b/net/ipv4/netfilter/nf_tables_ipv4.c
@@ -0,0 +1,127 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/ip.h>
15#include <linux/netfilter_ipv4.h>
16#include <net/netfilter/nf_tables.h>
17#include <net/net_namespace.h>
18#include <net/ip.h>
19#include <net/netfilter/nf_tables_ipv4.h>
20
21static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops,
22 struct sk_buff *skb,
23 const struct net_device *in,
24 const struct net_device *out,
25 int (*okfn)(struct sk_buff *))
26{
27 struct nft_pktinfo pkt;
28
29 if (unlikely(skb->len < sizeof(struct iphdr) ||
30 ip_hdr(skb)->ihl < sizeof(struct iphdr) / 4)) {
31 if (net_ratelimit())
32 pr_info("nf_tables_ipv4: ignoring short SOCK_RAW "
33 "packet\n");
34 return NF_ACCEPT;
35 }
36 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
37
38 return nft_do_chain_pktinfo(&pkt, ops);
39}
40
41static struct nft_af_info nft_af_ipv4 __read_mostly = {
42 .family = NFPROTO_IPV4,
43 .nhooks = NF_INET_NUMHOOKS,
44 .owner = THIS_MODULE,
45 .hooks = {
46 [NF_INET_LOCAL_OUT] = nft_ipv4_output,
47 },
48};
49
50static int nf_tables_ipv4_init_net(struct net *net)
51{
52 net->nft.ipv4 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
53 if (net->nft.ipv4 == NULL)
54 return -ENOMEM;
55
56 memcpy(net->nft.ipv4, &nft_af_ipv4, sizeof(nft_af_ipv4));
57
58 if (nft_register_afinfo(net, net->nft.ipv4) < 0)
59 goto err;
60
61 return 0;
62err:
63 kfree(net->nft.ipv4);
64 return -ENOMEM;
65}
66
67static void nf_tables_ipv4_exit_net(struct net *net)
68{
69 nft_unregister_afinfo(net->nft.ipv4);
70 kfree(net->nft.ipv4);
71}
72
73static struct pernet_operations nf_tables_ipv4_net_ops = {
74 .init = nf_tables_ipv4_init_net,
75 .exit = nf_tables_ipv4_exit_net,
76};
77
78static unsigned int
79nft_do_chain_ipv4(const struct nf_hook_ops *ops,
80 struct sk_buff *skb,
81 const struct net_device *in,
82 const struct net_device *out,
83 int (*okfn)(struct sk_buff *))
84{
85 struct nft_pktinfo pkt;
86
87 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
88
89 return nft_do_chain_pktinfo(&pkt, ops);
90}
91
92static struct nf_chain_type filter_ipv4 = {
93 .family = NFPROTO_IPV4,
94 .name = "filter",
95 .type = NFT_CHAIN_T_DEFAULT,
96 .hook_mask = (1 << NF_INET_LOCAL_IN) |
97 (1 << NF_INET_LOCAL_OUT) |
98 (1 << NF_INET_FORWARD) |
99 (1 << NF_INET_PRE_ROUTING) |
100 (1 << NF_INET_POST_ROUTING),
101 .fn = {
102 [NF_INET_LOCAL_IN] = nft_do_chain_ipv4,
103 [NF_INET_LOCAL_OUT] = nft_ipv4_output,
104 [NF_INET_FORWARD] = nft_do_chain_ipv4,
105 [NF_INET_PRE_ROUTING] = nft_do_chain_ipv4,
106 [NF_INET_POST_ROUTING] = nft_do_chain_ipv4,
107 },
108};
109
110static int __init nf_tables_ipv4_init(void)
111{
112 nft_register_chain_type(&filter_ipv4);
113 return register_pernet_subsys(&nf_tables_ipv4_net_ops);
114}
115
116static void __exit nf_tables_ipv4_exit(void)
117{
118 unregister_pernet_subsys(&nf_tables_ipv4_net_ops);
119 nft_unregister_chain_type(&filter_ipv4);
120}
121
122module_init(nf_tables_ipv4_init);
123module_exit(nf_tables_ipv4_exit);
124
125MODULE_LICENSE("GPL");
126MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
127MODULE_ALIAS_NFT_FAMILY(AF_INET);
diff --git a/net/ipv4/netfilter/nft_chain_nat_ipv4.c b/net/ipv4/netfilter/nft_chain_nat_ipv4.c
new file mode 100644
index 000000000000..cf2c792cd971
--- /dev/null
+++ b/net/ipv4/netfilter/nft_chain_nat_ipv4.c
@@ -0,0 +1,205 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 * Copyright (c) 2012 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Development of this code funded by Astaro AG (http://www.astaro.com/)
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/list.h>
16#include <linux/skbuff.h>
17#include <linux/ip.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter_ipv4.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_conntrack.h>
22#include <net/netfilter/nf_nat.h>
23#include <net/netfilter/nf_nat_core.h>
24#include <net/netfilter/nf_tables.h>
25#include <net/netfilter/nf_tables_ipv4.h>
26#include <net/netfilter/nf_nat_l3proto.h>
27#include <net/ip.h>
28
29/*
30 * NAT chains
31 */
32
33static unsigned int nf_nat_fn(const struct nf_hook_ops *ops,
34 struct sk_buff *skb,
35 const struct net_device *in,
36 const struct net_device *out,
37 int (*okfn)(struct sk_buff *))
38{
39 enum ip_conntrack_info ctinfo;
40 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
41 struct nf_conn_nat *nat;
42 enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
43 struct nft_pktinfo pkt;
44 unsigned int ret;
45
46 if (ct == NULL || nf_ct_is_untracked(ct))
47 return NF_ACCEPT;
48
49 NF_CT_ASSERT(!(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)));
50
51 nat = nfct_nat(ct);
52 if (nat == NULL) {
53 /* Conntrack module was loaded late, can't add extension. */
54 if (nf_ct_is_confirmed(ct))
55 return NF_ACCEPT;
56 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
57 if (nat == NULL)
58 return NF_ACCEPT;
59 }
60
61 switch (ctinfo) {
62 case IP_CT_RELATED:
63 case IP_CT_RELATED + IP_CT_IS_REPLY:
64 if (ip_hdr(skb)->protocol == IPPROTO_ICMP) {
65 if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
66 ops->hooknum))
67 return NF_DROP;
68 else
69 return NF_ACCEPT;
70 }
71 /* Fall through */
72 case IP_CT_NEW:
73 if (nf_nat_initialized(ct, maniptype))
74 break;
75
76 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
77
78 ret = nft_do_chain_pktinfo(&pkt, ops);
79 if (ret != NF_ACCEPT)
80 return ret;
81 if (!nf_nat_initialized(ct, maniptype)) {
82 ret = nf_nat_alloc_null_binding(ct, ops->hooknum);
83 if (ret != NF_ACCEPT)
84 return ret;
85 }
86 default:
87 break;
88 }
89
90 return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
91}
92
93static unsigned int nf_nat_prerouting(const struct nf_hook_ops *ops,
94 struct sk_buff *skb,
95 const struct net_device *in,
96 const struct net_device *out,
97 int (*okfn)(struct sk_buff *))
98{
99 __be32 daddr = ip_hdr(skb)->daddr;
100 unsigned int ret;
101
102 ret = nf_nat_fn(ops, skb, in, out, okfn);
103 if (ret != NF_DROP && ret != NF_STOLEN &&
104 ip_hdr(skb)->daddr != daddr) {
105 skb_dst_drop(skb);
106 }
107 return ret;
108}
109
110static unsigned int nf_nat_postrouting(const struct nf_hook_ops *ops,
111 struct sk_buff *skb,
112 const struct net_device *in,
113 const struct net_device *out,
114 int (*okfn)(struct sk_buff *))
115{
116 enum ip_conntrack_info ctinfo __maybe_unused;
117 const struct nf_conn *ct __maybe_unused;
118 unsigned int ret;
119
120 ret = nf_nat_fn(ops, skb, in, out, okfn);
121#ifdef CONFIG_XFRM
122 if (ret != NF_DROP && ret != NF_STOLEN &&
123 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
124 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
125
126 if (ct->tuplehash[dir].tuple.src.u3.ip !=
127 ct->tuplehash[!dir].tuple.dst.u3.ip ||
128 ct->tuplehash[dir].tuple.src.u.all !=
129 ct->tuplehash[!dir].tuple.dst.u.all)
130 return nf_xfrm_me_harder(skb, AF_INET) == 0 ?
131 ret : NF_DROP;
132 }
133#endif
134 return ret;
135}
136
137static unsigned int nf_nat_output(const struct nf_hook_ops *ops,
138 struct sk_buff *skb,
139 const struct net_device *in,
140 const struct net_device *out,
141 int (*okfn)(struct sk_buff *))
142{
143 enum ip_conntrack_info ctinfo;
144 const struct nf_conn *ct;
145 unsigned int ret;
146
147 ret = nf_nat_fn(ops, skb, in, out, okfn);
148 if (ret != NF_DROP && ret != NF_STOLEN &&
149 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
150 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
151
152 if (ct->tuplehash[dir].tuple.dst.u3.ip !=
153 ct->tuplehash[!dir].tuple.src.u3.ip) {
154 if (ip_route_me_harder(skb, RTN_UNSPEC))
155 ret = NF_DROP;
156 }
157#ifdef CONFIG_XFRM
158 else if (ct->tuplehash[dir].tuple.dst.u.all !=
159 ct->tuplehash[!dir].tuple.src.u.all)
160 if (nf_xfrm_me_harder(skb, AF_INET))
161 ret = NF_DROP;
162#endif
163 }
164 return ret;
165}
166
167static struct nf_chain_type nft_chain_nat_ipv4 = {
168 .family = NFPROTO_IPV4,
169 .name = "nat",
170 .type = NFT_CHAIN_T_NAT,
171 .hook_mask = (1 << NF_INET_PRE_ROUTING) |
172 (1 << NF_INET_POST_ROUTING) |
173 (1 << NF_INET_LOCAL_OUT) |
174 (1 << NF_INET_LOCAL_IN),
175 .fn = {
176 [NF_INET_PRE_ROUTING] = nf_nat_prerouting,
177 [NF_INET_POST_ROUTING] = nf_nat_postrouting,
178 [NF_INET_LOCAL_OUT] = nf_nat_output,
179 [NF_INET_LOCAL_IN] = nf_nat_fn,
180 },
181 .me = THIS_MODULE,
182};
183
184static int __init nft_chain_nat_init(void)
185{
186 int err;
187
188 err = nft_register_chain_type(&nft_chain_nat_ipv4);
189 if (err < 0)
190 return err;
191
192 return 0;
193}
194
195static void __exit nft_chain_nat_exit(void)
196{
197 nft_unregister_chain_type(&nft_chain_nat_ipv4);
198}
199
200module_init(nft_chain_nat_init);
201module_exit(nft_chain_nat_exit);
202
203MODULE_LICENSE("GPL");
204MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
205MODULE_ALIAS_NFT_CHAIN(AF_INET, "nat");
diff --git a/net/ipv4/netfilter/nft_chain_route_ipv4.c b/net/ipv4/netfilter/nft_chain_route_ipv4.c
new file mode 100644
index 000000000000..4e6bf9a3d7aa
--- /dev/null
+++ b/net/ipv4/netfilter/nft_chain_route_ipv4.c
@@ -0,0 +1,90 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/list.h>
13#include <linux/skbuff.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter_ipv4.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables.h>
20#include <net/netfilter/nf_tables_ipv4.h>
21#include <net/route.h>
22#include <net/ip.h>
23
24static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
25 struct sk_buff *skb,
26 const struct net_device *in,
27 const struct net_device *out,
28 int (*okfn)(struct sk_buff *))
29{
30 unsigned int ret;
31 struct nft_pktinfo pkt;
32 u32 mark;
33 __be32 saddr, daddr;
34 u_int8_t tos;
35 const struct iphdr *iph;
36
37 /* root is playing with raw sockets. */
38 if (skb->len < sizeof(struct iphdr) ||
39 ip_hdrlen(skb) < sizeof(struct iphdr))
40 return NF_ACCEPT;
41
42 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
43
44 mark = skb->mark;
45 iph = ip_hdr(skb);
46 saddr = iph->saddr;
47 daddr = iph->daddr;
48 tos = iph->tos;
49
50 ret = nft_do_chain_pktinfo(&pkt, ops);
51 if (ret != NF_DROP && ret != NF_QUEUE) {
52 iph = ip_hdr(skb);
53
54 if (iph->saddr != saddr ||
55 iph->daddr != daddr ||
56 skb->mark != mark ||
57 iph->tos != tos)
58 if (ip_route_me_harder(skb, RTN_UNSPEC))
59 ret = NF_DROP;
60 }
61 return ret;
62}
63
64static struct nf_chain_type nft_chain_route_ipv4 = {
65 .family = NFPROTO_IPV4,
66 .name = "route",
67 .type = NFT_CHAIN_T_ROUTE,
68 .hook_mask = (1 << NF_INET_LOCAL_OUT),
69 .fn = {
70 [NF_INET_LOCAL_OUT] = nf_route_table_hook,
71 },
72 .me = THIS_MODULE,
73};
74
75static int __init nft_chain_route_init(void)
76{
77 return nft_register_chain_type(&nft_chain_route_ipv4);
78}
79
80static void __exit nft_chain_route_exit(void)
81{
82 nft_unregister_chain_type(&nft_chain_route_ipv4);
83}
84
85module_init(nft_chain_route_init);
86module_exit(nft_chain_route_exit);
87
88MODULE_LICENSE("GPL");
89MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
90MODULE_ALIAS_NFT_CHAIN(AF_INET, "route");
diff --git a/net/ipv4/netfilter/nft_reject_ipv4.c b/net/ipv4/netfilter/nft_reject_ipv4.c
new file mode 100644
index 000000000000..4a5e94ac314a
--- /dev/null
+++ b/net/ipv4/netfilter/nft_reject_ipv4.c
@@ -0,0 +1,123 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18#include <net/icmp.h>
19
20struct nft_reject {
21 enum nft_reject_types type:8;
22 u8 icmp_code;
23};
24
25static void nft_reject_eval(const struct nft_expr *expr,
26 struct nft_data data[NFT_REG_MAX + 1],
27 const struct nft_pktinfo *pkt)
28{
29 struct nft_reject *priv = nft_expr_priv(expr);
30
31 switch (priv->type) {
32 case NFT_REJECT_ICMP_UNREACH:
33 icmp_send(pkt->skb, ICMP_DEST_UNREACH, priv->icmp_code, 0);
34 break;
35 case NFT_REJECT_TCP_RST:
36 break;
37 }
38
39 data[NFT_REG_VERDICT].verdict = NF_DROP;
40}
41
42static const struct nla_policy nft_reject_policy[NFTA_REJECT_MAX + 1] = {
43 [NFTA_REJECT_TYPE] = { .type = NLA_U32 },
44 [NFTA_REJECT_ICMP_CODE] = { .type = NLA_U8 },
45};
46
47static int nft_reject_init(const struct nft_ctx *ctx,
48 const struct nft_expr *expr,
49 const struct nlattr * const tb[])
50{
51 struct nft_reject *priv = nft_expr_priv(expr);
52
53 if (tb[NFTA_REJECT_TYPE] == NULL)
54 return -EINVAL;
55
56 priv->type = ntohl(nla_get_be32(tb[NFTA_REJECT_TYPE]));
57 switch (priv->type) {
58 case NFT_REJECT_ICMP_UNREACH:
59 if (tb[NFTA_REJECT_ICMP_CODE] == NULL)
60 return -EINVAL;
61 priv->icmp_code = nla_get_u8(tb[NFTA_REJECT_ICMP_CODE]);
62 case NFT_REJECT_TCP_RST:
63 break;
64 default:
65 return -EINVAL;
66 }
67
68 return 0;
69}
70
71static int nft_reject_dump(struct sk_buff *skb, const struct nft_expr *expr)
72{
73 const struct nft_reject *priv = nft_expr_priv(expr);
74
75 if (nla_put_be32(skb, NFTA_REJECT_TYPE, htonl(priv->type)))
76 goto nla_put_failure;
77
78 switch (priv->type) {
79 case NFT_REJECT_ICMP_UNREACH:
80 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
81 goto nla_put_failure;
82 break;
83 }
84
85 return 0;
86
87nla_put_failure:
88 return -1;
89}
90
91static struct nft_expr_type nft_reject_type;
92static const struct nft_expr_ops nft_reject_ops = {
93 .type = &nft_reject_type,
94 .size = NFT_EXPR_SIZE(sizeof(struct nft_reject)),
95 .eval = nft_reject_eval,
96 .init = nft_reject_init,
97 .dump = nft_reject_dump,
98};
99
100static struct nft_expr_type nft_reject_type __read_mostly = {
101 .name = "reject",
102 .ops = &nft_reject_ops,
103 .policy = nft_reject_policy,
104 .maxattr = NFTA_REJECT_MAX,
105 .owner = THIS_MODULE,
106};
107
108static int __init nft_reject_module_init(void)
109{
110 return nft_register_expr(&nft_reject_type);
111}
112
113static void __exit nft_reject_module_exit(void)
114{
115 nft_unregister_expr(&nft_reject_type);
116}
117
118module_init(nft_reject_module_init);
119module_exit(nft_reject_module_exit);
120
121MODULE_LICENSE("GPL");
122MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
123MODULE_ALIAS_NFT_EXPR("reject");
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index d7d9882d4cae..242e7f4ed6f4 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -202,15 +202,14 @@ static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident)
202#if IS_ENABLED(CONFIG_IPV6) 202#if IS_ENABLED(CONFIG_IPV6)
203 } else if (skb->protocol == htons(ETH_P_IPV6) && 203 } else if (skb->protocol == htons(ETH_P_IPV6) &&
204 sk->sk_family == AF_INET6) { 204 sk->sk_family == AF_INET6) {
205 struct ipv6_pinfo *np = inet6_sk(sk);
206 205
207 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk, 206 pr_debug("found: %p: num=%d, daddr=%pI6c, dif=%d\n", sk,
208 (int) isk->inet_num, 207 (int) isk->inet_num,
209 &inet6_sk(sk)->rcv_saddr, 208 &sk->sk_v6_rcv_saddr,
210 sk->sk_bound_dev_if); 209 sk->sk_bound_dev_if);
211 210
212 if (!ipv6_addr_any(&np->rcv_saddr) && 211 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
213 !ipv6_addr_equal(&np->rcv_saddr, 212 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr,
214 &ipv6_hdr(skb)->daddr)) 213 &ipv6_hdr(skb)->daddr))
215 continue; 214 continue;
216#endif 215#endif
@@ -237,11 +236,11 @@ static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
237 unsigned int seq; 236 unsigned int seq;
238 237
239 do { 238 do {
240 seq = read_seqbegin(&sysctl_local_ports.lock); 239 seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock);
241 240
242 *low = data[0]; 241 *low = data[0];
243 *high = data[1]; 242 *high = data[1];
244 } while (read_seqretry(&sysctl_local_ports.lock, seq)); 243 } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq));
245} 244}
246 245
247 246
@@ -362,7 +361,7 @@ static void ping_set_saddr(struct sock *sk, struct sockaddr *saddr)
362 } else if (saddr->sa_family == AF_INET6) { 361 } else if (saddr->sa_family == AF_INET6) {
363 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr; 362 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) saddr;
364 struct ipv6_pinfo *np = inet6_sk(sk); 363 struct ipv6_pinfo *np = inet6_sk(sk);
365 np->rcv_saddr = np->saddr = addr->sin6_addr; 364 sk->sk_v6_rcv_saddr = np->saddr = addr->sin6_addr;
366#endif 365#endif
367 } 366 }
368} 367}
@@ -376,7 +375,7 @@ static void ping_clear_saddr(struct sock *sk, int dif)
376#if IS_ENABLED(CONFIG_IPV6) 375#if IS_ENABLED(CONFIG_IPV6)
377 } else if (sk->sk_family == AF_INET6) { 376 } else if (sk->sk_family == AF_INET6) {
378 struct ipv6_pinfo *np = inet6_sk(sk); 377 struct ipv6_pinfo *np = inet6_sk(sk);
379 memset(&np->rcv_saddr, 0, sizeof(np->rcv_saddr)); 378 memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
380 memset(&np->saddr, 0, sizeof(np->saddr)); 379 memset(&np->saddr, 0, sizeof(np->saddr));
381#endif 380#endif
382 } 381 }
@@ -416,10 +415,12 @@ int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
416 (int)sk->sk_bound_dev_if); 415 (int)sk->sk_bound_dev_if);
417 416
418 err = 0; 417 err = 0;
419 if ((sk->sk_family == AF_INET && isk->inet_rcv_saddr) || 418 if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
420 (sk->sk_family == AF_INET6 &&
421 !ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)))
422 sk->sk_userlocks |= SOCK_BINDADDR_LOCK; 419 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
420#if IS_ENABLED(CONFIG_IPV6)
421 if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
422 sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
423#endif
423 424
424 if (snum) 425 if (snum)
425 sk->sk_userlocks |= SOCK_BINDPORT_LOCK; 426 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
@@ -429,7 +430,7 @@ int ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
429 430
430#if IS_ENABLED(CONFIG_IPV6) 431#if IS_ENABLED(CONFIG_IPV6)
431 if (sk->sk_family == AF_INET6) 432 if (sk->sk_family == AF_INET6)
432 memset(&inet6_sk(sk)->daddr, 0, sizeof(inet6_sk(sk)->daddr)); 433 memset(&sk->sk_v6_daddr, 0, sizeof(sk->sk_v6_daddr));
433#endif 434#endif
434 435
435 sk_dst_reset(sk); 436 sk_dst_reset(sk);
@@ -713,6 +714,8 @@ int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
713 ipc.opt = NULL; 714 ipc.opt = NULL;
714 ipc.oif = sk->sk_bound_dev_if; 715 ipc.oif = sk->sk_bound_dev_if;
715 ipc.tx_flags = 0; 716 ipc.tx_flags = 0;
717 ipc.ttl = 0;
718 ipc.tos = -1;
716 719
717 sock_tx_timestamp(sk, &ipc.tx_flags); 720 sock_tx_timestamp(sk, &ipc.tx_flags);
718 721
@@ -744,7 +747,7 @@ int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
744 return -EINVAL; 747 return -EINVAL;
745 faddr = ipc.opt->opt.faddr; 748 faddr = ipc.opt->opt.faddr;
746 } 749 }
747 tos = RT_TOS(inet->tos); 750 tos = get_rttos(&ipc, inet);
748 if (sock_flag(sk, SOCK_LOCALROUTE) || 751 if (sock_flag(sk, SOCK_LOCALROUTE) ||
749 (msg->msg_flags & MSG_DONTROUTE) || 752 (msg->msg_flags & MSG_DONTROUTE) ||
750 (ipc.opt && ipc.opt->opt.is_strictroute)) { 753 (ipc.opt && ipc.opt->opt.is_strictroute)) {
@@ -769,7 +772,7 @@ int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
769 err = PTR_ERR(rt); 772 err = PTR_ERR(rt);
770 rt = NULL; 773 rt = NULL;
771 if (err == -ENETUNREACH) 774 if (err == -ENETUNREACH)
772 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); 775 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
773 goto out; 776 goto out;
774 } 777 }
775 778
@@ -827,8 +830,6 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
827{ 830{
828 struct inet_sock *isk = inet_sk(sk); 831 struct inet_sock *isk = inet_sk(sk);
829 int family = sk->sk_family; 832 int family = sk->sk_family;
830 struct sockaddr_in *sin;
831 struct sockaddr_in6 *sin6;
832 struct sk_buff *skb; 833 struct sk_buff *skb;
833 int copied, err; 834 int copied, err;
834 835
@@ -838,19 +839,13 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
838 if (flags & MSG_OOB) 839 if (flags & MSG_OOB)
839 goto out; 840 goto out;
840 841
841 if (addr_len) {
842 if (family == AF_INET)
843 *addr_len = sizeof(*sin);
844 else if (family == AF_INET6 && addr_len)
845 *addr_len = sizeof(*sin6);
846 }
847
848 if (flags & MSG_ERRQUEUE) { 842 if (flags & MSG_ERRQUEUE) {
849 if (family == AF_INET) { 843 if (family == AF_INET) {
850 return ip_recv_error(sk, msg, len); 844 return ip_recv_error(sk, msg, len, addr_len);
851#if IS_ENABLED(CONFIG_IPV6) 845#if IS_ENABLED(CONFIG_IPV6)
852 } else if (family == AF_INET6) { 846 } else if (family == AF_INET6) {
853 return pingv6_ops.ipv6_recv_error(sk, msg, len); 847 return pingv6_ops.ipv6_recv_error(sk, msg, len,
848 addr_len);
854#endif 849#endif
855 } 850 }
856 } 851 }
@@ -874,11 +869,15 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
874 869
875 /* Copy the address and add cmsg data. */ 870 /* Copy the address and add cmsg data. */
876 if (family == AF_INET) { 871 if (family == AF_INET) {
877 sin = (struct sockaddr_in *) msg->msg_name; 872 struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
878 sin->sin_family = AF_INET; 873
879 sin->sin_port = 0 /* skb->h.uh->source */; 874 if (sin) {
880 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 875 sin->sin_family = AF_INET;
881 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 876 sin->sin_port = 0 /* skb->h.uh->source */;
877 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
878 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
879 *addr_len = sizeof(*sin);
880 }
882 881
883 if (isk->cmsg_flags) 882 if (isk->cmsg_flags)
884 ip_cmsg_recv(msg, skb); 883 ip_cmsg_recv(msg, skb);
@@ -887,17 +886,21 @@ int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
887 } else if (family == AF_INET6) { 886 } else if (family == AF_INET6) {
888 struct ipv6_pinfo *np = inet6_sk(sk); 887 struct ipv6_pinfo *np = inet6_sk(sk);
889 struct ipv6hdr *ip6 = ipv6_hdr(skb); 888 struct ipv6hdr *ip6 = ipv6_hdr(skb);
890 sin6 = (struct sockaddr_in6 *) msg->msg_name; 889 struct sockaddr_in6 *sin6 =
891 sin6->sin6_family = AF_INET6; 890 (struct sockaddr_in6 *)msg->msg_name;
892 sin6->sin6_port = 0; 891
893 sin6->sin6_addr = ip6->saddr; 892 if (sin6) {
894 893 sin6->sin6_family = AF_INET6;
895 sin6->sin6_flowinfo = 0; 894 sin6->sin6_port = 0;
896 if (np->sndflow) 895 sin6->sin6_addr = ip6->saddr;
897 sin6->sin6_flowinfo = ip6_flowinfo(ip6); 896 sin6->sin6_flowinfo = 0;
898 897 if (np->sndflow)
899 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, 898 sin6->sin6_flowinfo = ip6_flowinfo(ip6);
900 IP6CB(skb)->iif); 899 sin6->sin6_scope_id =
900 ipv6_iface_scope_id(&sin6->sin6_addr,
901 IP6CB(skb)->iif);
902 *addr_len = sizeof(*sin6);
903 }
901 904
902 if (inet6_sk(sk)->rxopt.all) 905 if (inet6_sk(sk)->rxopt.all)
903 pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb); 906 pingv6_ops.ip6_datagram_recv_ctl(sk, msg, skb);
@@ -1073,7 +1076,7 @@ void ping_seq_stop(struct seq_file *seq, void *v)
1073EXPORT_SYMBOL_GPL(ping_seq_stop); 1076EXPORT_SYMBOL_GPL(ping_seq_stop);
1074 1077
1075static void ping_v4_format_sock(struct sock *sp, struct seq_file *f, 1078static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1076 int bucket, int *len) 1079 int bucket)
1077{ 1080{
1078 struct inet_sock *inet = inet_sk(sp); 1081 struct inet_sock *inet = inet_sk(sp);
1079 __be32 dest = inet->inet_daddr; 1082 __be32 dest = inet->inet_daddr;
@@ -1082,7 +1085,7 @@ static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1082 __u16 srcp = ntohs(inet->inet_sport); 1085 __u16 srcp = ntohs(inet->inet_sport);
1083 1086
1084 seq_printf(f, "%5d: %08X:%04X %08X:%04X" 1087 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
1085 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n", 1088 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
1086 bucket, src, srcp, dest, destp, sp->sk_state, 1089 bucket, src, srcp, dest, destp, sp->sk_state,
1087 sk_wmem_alloc_get(sp), 1090 sk_wmem_alloc_get(sp),
1088 sk_rmem_alloc_get(sp), 1091 sk_rmem_alloc_get(sp),
@@ -1090,23 +1093,22 @@ static void ping_v4_format_sock(struct sock *sp, struct seq_file *f,
1090 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 1093 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
1091 0, sock_i_ino(sp), 1094 0, sock_i_ino(sp),
1092 atomic_read(&sp->sk_refcnt), sp, 1095 atomic_read(&sp->sk_refcnt), sp,
1093 atomic_read(&sp->sk_drops), len); 1096 atomic_read(&sp->sk_drops));
1094} 1097}
1095 1098
1096static int ping_v4_seq_show(struct seq_file *seq, void *v) 1099static int ping_v4_seq_show(struct seq_file *seq, void *v)
1097{ 1100{
1101 seq_setwidth(seq, 127);
1098 if (v == SEQ_START_TOKEN) 1102 if (v == SEQ_START_TOKEN)
1099 seq_printf(seq, "%-127s\n", 1103 seq_puts(seq, " sl local_address rem_address st tx_queue "
1100 " sl local_address rem_address st tx_queue "
1101 "rx_queue tr tm->when retrnsmt uid timeout " 1104 "rx_queue tr tm->when retrnsmt uid timeout "
1102 "inode ref pointer drops"); 1105 "inode ref pointer drops");
1103 else { 1106 else {
1104 struct ping_iter_state *state = seq->private; 1107 struct ping_iter_state *state = seq->private;
1105 int len;
1106 1108
1107 ping_v4_format_sock(v, seq, state->bucket, &len); 1109 ping_v4_format_sock(v, seq, state->bucket);
1108 seq_printf(seq, "%*s\n", 127 - len, "");
1109 } 1110 }
1111 seq_pad(seq, '\n');
1110 return 0; 1112 return 0;
1111} 1113}
1112 1114
diff --git a/net/ipv4/protocol.c b/net/ipv4/protocol.c
index ce848461acbb..46d6a1c923a8 100644
--- a/net/ipv4/protocol.c
+++ b/net/ipv4/protocol.c
@@ -31,10 +31,6 @@
31const struct net_protocol __rcu *inet_protos[MAX_INET_PROTOS] __read_mostly; 31const struct net_protocol __rcu *inet_protos[MAX_INET_PROTOS] __read_mostly;
32const struct net_offload __rcu *inet_offloads[MAX_INET_PROTOS] __read_mostly; 32const struct net_offload __rcu *inet_offloads[MAX_INET_PROTOS] __read_mostly;
33 33
34/*
35 * Add a protocol handler to the hash tables
36 */
37
38int inet_add_protocol(const struct net_protocol *prot, unsigned char protocol) 34int inet_add_protocol(const struct net_protocol *prot, unsigned char protocol)
39{ 35{
40 if (!prot->netns_ok) { 36 if (!prot->netns_ok) {
@@ -55,10 +51,6 @@ int inet_add_offload(const struct net_offload *prot, unsigned char protocol)
55} 51}
56EXPORT_SYMBOL(inet_add_offload); 52EXPORT_SYMBOL(inet_add_offload);
57 53
58/*
59 * Remove a protocol from the hash tables.
60 */
61
62int inet_del_protocol(const struct net_protocol *prot, unsigned char protocol) 54int inet_del_protocol(const struct net_protocol *prot, unsigned char protocol)
63{ 55{
64 int ret; 56 int ret;
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index 193db03540ad..23c3e5b5bb53 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -299,7 +299,7 @@ static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
299{ 299{
300 /* Charge it to the socket. */ 300 /* Charge it to the socket. */
301 301
302 ipv4_pktinfo_prepare(skb); 302 ipv4_pktinfo_prepare(sk, skb);
303 if (sock_queue_rcv_skb(sk, skb) < 0) { 303 if (sock_queue_rcv_skb(sk, skb) < 0) {
304 kfree_skb(skb); 304 kfree_skb(skb);
305 return NET_RX_DROP; 305 return NET_RX_DROP;
@@ -519,6 +519,8 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
519 ipc.addr = inet->inet_saddr; 519 ipc.addr = inet->inet_saddr;
520 ipc.opt = NULL; 520 ipc.opt = NULL;
521 ipc.tx_flags = 0; 521 ipc.tx_flags = 0;
522 ipc.ttl = 0;
523 ipc.tos = -1;
522 ipc.oif = sk->sk_bound_dev_if; 524 ipc.oif = sk->sk_bound_dev_if;
523 525
524 if (msg->msg_controllen) { 526 if (msg->msg_controllen) {
@@ -558,7 +560,7 @@ static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
558 daddr = ipc.opt->opt.faddr; 560 daddr = ipc.opt->opt.faddr;
559 } 561 }
560 } 562 }
561 tos = RT_CONN_FLAGS(sk); 563 tos = get_rtconn_flags(&ipc, sk);
562 if (msg->msg_flags & MSG_DONTROUTE) 564 if (msg->msg_flags & MSG_DONTROUTE)
563 tos |= RTO_ONLINK; 565 tos |= RTO_ONLINK;
564 566
@@ -694,11 +696,8 @@ static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
694 if (flags & MSG_OOB) 696 if (flags & MSG_OOB)
695 goto out; 697 goto out;
696 698
697 if (addr_len)
698 *addr_len = sizeof(*sin);
699
700 if (flags & MSG_ERRQUEUE) { 699 if (flags & MSG_ERRQUEUE) {
701 err = ip_recv_error(sk, msg, len); 700 err = ip_recv_error(sk, msg, len, addr_len);
702 goto out; 701 goto out;
703 } 702 }
704 703
@@ -724,6 +723,7 @@ static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
724 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 723 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
725 sin->sin_port = 0; 724 sin->sin_port = 0;
726 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 725 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
726 *addr_len = sizeof(*sin);
727 } 727 }
728 if (inet->cmsg_flags) 728 if (inet->cmsg_flags)
729 ip_cmsg_recv(msg, skb); 729 ip_cmsg_recv(msg, skb);
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index 6011615e810d..f8da28278014 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -295,7 +295,7 @@ static int rt_cpu_seq_show(struct seq_file *seq, void *v)
295 seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x " 295 seq_printf(seq,"%08x %08x %08x %08x %08x %08x %08x %08x "
296 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n", 296 " %08x %08x %08x %08x %08x %08x %08x %08x %08x \n",
297 dst_entries_get_slow(&ipv4_dst_ops), 297 dst_entries_get_slow(&ipv4_dst_ops),
298 st->in_hit, 298 0, /* st->in_hit */
299 st->in_slow_tot, 299 st->in_slow_tot,
300 st->in_slow_mc, 300 st->in_slow_mc,
301 st->in_no_route, 301 st->in_no_route,
@@ -303,16 +303,16 @@ static int rt_cpu_seq_show(struct seq_file *seq, void *v)
303 st->in_martian_dst, 303 st->in_martian_dst,
304 st->in_martian_src, 304 st->in_martian_src,
305 305
306 st->out_hit, 306 0, /* st->out_hit */
307 st->out_slow_tot, 307 st->out_slow_tot,
308 st->out_slow_mc, 308 st->out_slow_mc,
309 309
310 st->gc_total, 310 0, /* st->gc_total */
311 st->gc_ignored, 311 0, /* st->gc_ignored */
312 st->gc_goal_miss, 312 0, /* st->gc_goal_miss */
313 st->gc_dst_overflow, 313 0, /* st->gc_dst_overflow */
314 st->in_hlist_search, 314 0, /* st->in_hlist_search */
315 st->out_hlist_search 315 0 /* st->out_hlist_search */
316 ); 316 );
317 return 0; 317 return 0;
318} 318}
@@ -1036,6 +1036,10 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
1036 bool new = false; 1036 bool new = false;
1037 1037
1038 bh_lock_sock(sk); 1038 bh_lock_sock(sk);
1039
1040 if (!ip_sk_accept_pmtu(sk))
1041 goto out;
1042
1039 rt = (struct rtable *) __sk_dst_get(sk); 1043 rt = (struct rtable *) __sk_dst_get(sk);
1040 1044
1041 if (sock_owned_by_user(sk) || !rt) { 1045 if (sock_owned_by_user(sk) || !rt) {
@@ -1772,8 +1776,12 @@ local_input:
1772 rth->dst.error= -err; 1776 rth->dst.error= -err;
1773 rth->rt_flags &= ~RTCF_LOCAL; 1777 rth->rt_flags &= ~RTCF_LOCAL;
1774 } 1778 }
1775 if (do_cache) 1779 if (do_cache) {
1776 rt_cache_route(&FIB_RES_NH(res), rth); 1780 if (unlikely(!rt_cache_route(&FIB_RES_NH(res), rth))) {
1781 rth->dst.flags |= DST_NOCACHE;
1782 rt_add_uncached_list(rth);
1783 }
1784 }
1777 skb_dst_set(skb, &rth->dst); 1785 skb_dst_set(skb, &rth->dst);
1778 err = 0; 1786 err = 0;
1779 goto out; 1787 goto out;
diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
index 14a15c49129d..b95331e6c077 100644
--- a/net/ipv4/syncookies.c
+++ b/net/ipv4/syncookies.c
@@ -25,15 +25,7 @@
25 25
26extern int sysctl_tcp_syncookies; 26extern int sysctl_tcp_syncookies;
27 27
28__u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS]; 28static u32 syncookie_secret[2][16-4+SHA_DIGEST_WORDS];
29EXPORT_SYMBOL(syncookie_secret);
30
31static __init int init_syncookies(void)
32{
33 get_random_bytes(syncookie_secret, sizeof(syncookie_secret));
34 return 0;
35}
36__initcall(init_syncookies);
37 29
38#define COOKIEBITS 24 /* Upper bits store count */ 30#define COOKIEBITS 24 /* Upper bits store count */
39#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1) 31#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
@@ -44,8 +36,11 @@ static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
44static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport, 36static u32 cookie_hash(__be32 saddr, __be32 daddr, __be16 sport, __be16 dport,
45 u32 count, int c) 37 u32 count, int c)
46{ 38{
47 __u32 *tmp = __get_cpu_var(ipv4_cookie_scratch); 39 __u32 *tmp;
40
41 net_get_random_once(syncookie_secret, sizeof(syncookie_secret));
48 42
43 tmp = __get_cpu_var(ipv4_cookie_scratch);
49 memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c])); 44 memcpy(tmp + 4, syncookie_secret[c], sizeof(syncookie_secret[c]));
50 tmp[0] = (__force u32)saddr; 45 tmp[0] = (__force u32)saddr;
51 tmp[1] = (__force u32)daddr; 46 tmp[1] = (__force u32)daddr;
@@ -89,8 +84,7 @@ __u32 cookie_init_timestamp(struct request_sock *req)
89 84
90 85
91static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport, 86static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
92 __be16 dport, __u32 sseq, __u32 count, 87 __be16 dport, __u32 sseq, __u32 data)
93 __u32 data)
94{ 88{
95 /* 89 /*
96 * Compute the secure sequence number. 90 * Compute the secure sequence number.
@@ -102,7 +96,7 @@ static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
102 * As an extra hack, we add a small "data" value that encodes the 96 * As an extra hack, we add a small "data" value that encodes the
103 * MSS into the second hash value. 97 * MSS into the second hash value.
104 */ 98 */
105 99 u32 count = tcp_cookie_time();
106 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) + 100 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
107 sseq + (count << COOKIEBITS) + 101 sseq + (count << COOKIEBITS) +
108 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data) 102 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
@@ -114,22 +108,21 @@ static __u32 secure_tcp_syn_cookie(__be32 saddr, __be32 daddr, __be16 sport,
114 * If the syncookie is bad, the data returned will be out of 108 * If the syncookie is bad, the data returned will be out of
115 * range. This must be checked by the caller. 109 * range. This must be checked by the caller.
116 * 110 *
117 * The count value used to generate the cookie must be within 111 * The count value used to generate the cookie must be less than
118 * "maxdiff" if the current (passed-in) "count". The return value 112 * MAX_SYNCOOKIE_AGE minutes in the past.
119 * is (__u32)-1 if this test fails. 113 * The return value (__u32)-1 if this test fails.
120 */ 114 */
121static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr, 115static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
122 __be16 sport, __be16 dport, __u32 sseq, 116 __be16 sport, __be16 dport, __u32 sseq)
123 __u32 count, __u32 maxdiff)
124{ 117{
125 __u32 diff; 118 u32 diff, count = tcp_cookie_time();
126 119
127 /* Strip away the layers from the cookie */ 120 /* Strip away the layers from the cookie */
128 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq; 121 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
129 122
130 /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */ 123 /* Cookie is now reduced to (count * 2^24) ^ (hash % 2^24) */
131 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS); 124 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) - 1 >> COOKIEBITS);
132 if (diff >= maxdiff) 125 if (diff >= MAX_SYNCOOKIE_AGE)
133 return (__u32)-1; 126 return (__u32)-1;
134 127
135 return (cookie - 128 return (cookie -
@@ -138,22 +131,22 @@ static __u32 check_tcp_syn_cookie(__u32 cookie, __be32 saddr, __be32 daddr,
138} 131}
139 132
140/* 133/*
141 * MSS Values are taken from the 2009 paper 134 * MSS Values are chosen based on the 2011 paper
142 * 'Measuring TCP Maximum Segment Size' by S. Alcock and R. Nelson: 135 * 'An Analysis of TCP Maximum Segement Sizes' by S. Alcock and R. Nelson.
143 * - values 1440 to 1460 accounted for 80% of observed mss values 136 * Values ..
144 * - values outside the 536-1460 range are rare (<0.2%). 137 * .. lower than 536 are rare (< 0.2%)
138 * .. between 537 and 1299 account for less than < 1.5% of observed values
139 * .. in the 1300-1349 range account for about 15 to 20% of observed mss values
140 * .. exceeding 1460 are very rare (< 0.04%)
145 * 141 *
146 * Table must be sorted. 142 * 1460 is the single most frequently announced mss value (30 to 46% depending
143 * on monitor location). Table must be sorted.
147 */ 144 */
148static __u16 const msstab[] = { 145static __u16 const msstab[] = {
149 64,
150 512,
151 536, 146 536,
152 1024, 147 1300,
153 1440, 148 1440, /* 1440, 1452: PPPoE */
154 1460, 149 1460,
155 4312,
156 8960,
157}; 150};
158 151
159/* 152/*
@@ -173,7 +166,7 @@ u32 __cookie_v4_init_sequence(const struct iphdr *iph, const struct tcphdr *th,
173 166
174 return secure_tcp_syn_cookie(iph->saddr, iph->daddr, 167 return secure_tcp_syn_cookie(iph->saddr, iph->daddr,
175 th->source, th->dest, ntohl(th->seq), 168 th->source, th->dest, ntohl(th->seq),
176 jiffies / (HZ * 60), mssind); 169 mssind);
177} 170}
178EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence); 171EXPORT_SYMBOL_GPL(__cookie_v4_init_sequence);
179 172
@@ -189,13 +182,6 @@ __u32 cookie_v4_init_sequence(struct sock *sk, struct sk_buff *skb, __u16 *mssp)
189} 182}
190 183
191/* 184/*
192 * This (misnamed) value is the age of syncookie which is permitted.
193 * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
194 * sysctl_tcp_retries1. It's a rather complicated formula (exponential
195 * backoff) to compute at runtime so it's currently hardcoded here.
196 */
197#define COUNTER_TRIES 4
198/*
199 * Check if a ack sequence number is a valid syncookie. 185 * Check if a ack sequence number is a valid syncookie.
200 * Return the decoded mss if it is, or 0 if not. 186 * Return the decoded mss if it is, or 0 if not.
201 */ 187 */
@@ -204,9 +190,7 @@ int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
204{ 190{
205 __u32 seq = ntohl(th->seq) - 1; 191 __u32 seq = ntohl(th->seq) - 1;
206 __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr, 192 __u32 mssind = check_tcp_syn_cookie(cookie, iph->saddr, iph->daddr,
207 th->source, th->dest, seq, 193 th->source, th->dest, seq);
208 jiffies / (HZ * 60),
209 COUNTER_TRIES);
210 194
211 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0; 195 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
212} 196}
@@ -315,10 +299,10 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
315 treq->rcv_isn = ntohl(th->seq) - 1; 299 treq->rcv_isn = ntohl(th->seq) - 1;
316 treq->snt_isn = cookie; 300 treq->snt_isn = cookie;
317 req->mss = mss; 301 req->mss = mss;
318 ireq->loc_port = th->dest; 302 ireq->ir_num = ntohs(th->dest);
319 ireq->rmt_port = th->source; 303 ireq->ir_rmt_port = th->source;
320 ireq->loc_addr = ip_hdr(skb)->daddr; 304 ireq->ir_loc_addr = ip_hdr(skb)->daddr;
321 ireq->rmt_addr = ip_hdr(skb)->saddr; 305 ireq->ir_rmt_addr = ip_hdr(skb)->saddr;
322 ireq->ecn_ok = ecn_ok; 306 ireq->ecn_ok = ecn_ok;
323 ireq->snd_wscale = tcp_opt.snd_wscale; 307 ireq->snd_wscale = tcp_opt.snd_wscale;
324 ireq->sack_ok = tcp_opt.sack_ok; 308 ireq->sack_ok = tcp_opt.sack_ok;
@@ -358,8 +342,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb,
358 flowi4_init_output(&fl4, sk->sk_bound_dev_if, sk->sk_mark, 342 flowi4_init_output(&fl4, sk->sk_bound_dev_if, sk->sk_mark,
359 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP, 343 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, IPPROTO_TCP,
360 inet_sk_flowi_flags(sk), 344 inet_sk_flowi_flags(sk),
361 (opt && opt->srr) ? opt->faddr : ireq->rmt_addr, 345 (opt && opt->srr) ? opt->faddr : ireq->ir_rmt_addr,
362 ireq->loc_addr, th->source, th->dest); 346 ireq->ir_loc_addr, th->source, th->dest);
363 security_req_classify_flow(req, flowi4_to_flowi(&fl4)); 347 security_req_classify_flow(req, flowi4_to_flowi(&fl4));
364 rt = ip_route_output_key(sock_net(sk), &fl4); 348 rt = ip_route_output_key(sock_net(sk), &fl4);
365 if (IS_ERR(rt)) { 349 if (IS_ERR(rt)) {
diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
index 540279f4c531..3d69ec8dac57 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -43,12 +43,12 @@ static int ip_ping_group_range_min[] = { 0, 0 };
43static int ip_ping_group_range_max[] = { GID_T_MAX, GID_T_MAX }; 43static int ip_ping_group_range_max[] = { GID_T_MAX, GID_T_MAX };
44 44
45/* Update system visible IP port range */ 45/* Update system visible IP port range */
46static void set_local_port_range(int range[2]) 46static void set_local_port_range(struct net *net, int range[2])
47{ 47{
48 write_seqlock(&sysctl_local_ports.lock); 48 write_seqlock(&net->ipv4.sysctl_local_ports.lock);
49 sysctl_local_ports.range[0] = range[0]; 49 net->ipv4.sysctl_local_ports.range[0] = range[0];
50 sysctl_local_ports.range[1] = range[1]; 50 net->ipv4.sysctl_local_ports.range[1] = range[1];
51 write_sequnlock(&sysctl_local_ports.lock); 51 write_sequnlock(&net->ipv4.sysctl_local_ports.lock);
52} 52}
53 53
54/* Validate changes from /proc interface. */ 54/* Validate changes from /proc interface. */
@@ -56,6 +56,8 @@ static int ipv4_local_port_range(struct ctl_table *table, int write,
56 void __user *buffer, 56 void __user *buffer,
57 size_t *lenp, loff_t *ppos) 57 size_t *lenp, loff_t *ppos)
58{ 58{
59 struct net *net =
60 container_of(table->data, struct net, ipv4.sysctl_local_ports.range);
59 int ret; 61 int ret;
60 int range[2]; 62 int range[2];
61 struct ctl_table tmp = { 63 struct ctl_table tmp = {
@@ -66,14 +68,15 @@ static int ipv4_local_port_range(struct ctl_table *table, int write,
66 .extra2 = &ip_local_port_range_max, 68 .extra2 = &ip_local_port_range_max,
67 }; 69 };
68 70
69 inet_get_local_port_range(range, range + 1); 71 inet_get_local_port_range(net, &range[0], &range[1]);
72
70 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 73 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
71 74
72 if (write && ret == 0) { 75 if (write && ret == 0) {
73 if (range[1] < range[0]) 76 if (range[1] < range[0])
74 ret = -EINVAL; 77 ret = -EINVAL;
75 else 78 else
76 set_local_port_range(range); 79 set_local_port_range(net, range);
77 } 80 }
78 81
79 return ret; 82 return ret;
@@ -83,23 +86,27 @@ static int ipv4_local_port_range(struct ctl_table *table, int write,
83static void inet_get_ping_group_range_table(struct ctl_table *table, kgid_t *low, kgid_t *high) 86static void inet_get_ping_group_range_table(struct ctl_table *table, kgid_t *low, kgid_t *high)
84{ 87{
85 kgid_t *data = table->data; 88 kgid_t *data = table->data;
89 struct net *net =
90 container_of(table->data, struct net, ipv4.sysctl_ping_group_range);
86 unsigned int seq; 91 unsigned int seq;
87 do { 92 do {
88 seq = read_seqbegin(&sysctl_local_ports.lock); 93 seq = read_seqbegin(&net->ipv4.sysctl_local_ports.lock);
89 94
90 *low = data[0]; 95 *low = data[0];
91 *high = data[1]; 96 *high = data[1];
92 } while (read_seqretry(&sysctl_local_ports.lock, seq)); 97 } while (read_seqretry(&net->ipv4.sysctl_local_ports.lock, seq));
93} 98}
94 99
95/* Update system visible IP port range */ 100/* Update system visible IP port range */
96static void set_ping_group_range(struct ctl_table *table, kgid_t low, kgid_t high) 101static void set_ping_group_range(struct ctl_table *table, kgid_t low, kgid_t high)
97{ 102{
98 kgid_t *data = table->data; 103 kgid_t *data = table->data;
99 write_seqlock(&sysctl_local_ports.lock); 104 struct net *net =
105 container_of(table->data, struct net, ipv4.sysctl_ping_group_range);
106 write_seqlock(&net->ipv4.sysctl_local_ports.lock);
100 data[0] = low; 107 data[0] = low;
101 data[1] = high; 108 data[1] = high;
102 write_sequnlock(&sysctl_local_ports.lock); 109 write_sequnlock(&net->ipv4.sysctl_local_ports.lock);
103} 110}
104 111
105/* Validate changes from /proc interface. */ 112/* Validate changes from /proc interface. */
@@ -193,49 +200,6 @@ static int proc_allowed_congestion_control(struct ctl_table *ctl,
193 return ret; 200 return ret;
194} 201}
195 202
196static int ipv4_tcp_mem(struct ctl_table *ctl, int write,
197 void __user *buffer, size_t *lenp,
198 loff_t *ppos)
199{
200 int ret;
201 unsigned long vec[3];
202 struct net *net = current->nsproxy->net_ns;
203#ifdef CONFIG_MEMCG_KMEM
204 struct mem_cgroup *memcg;
205#endif
206
207 struct ctl_table tmp = {
208 .data = &vec,
209 .maxlen = sizeof(vec),
210 .mode = ctl->mode,
211 };
212
213 if (!write) {
214 ctl->data = &net->ipv4.sysctl_tcp_mem;
215 return proc_doulongvec_minmax(ctl, write, buffer, lenp, ppos);
216 }
217
218 ret = proc_doulongvec_minmax(&tmp, write, buffer, lenp, ppos);
219 if (ret)
220 return ret;
221
222#ifdef CONFIG_MEMCG_KMEM
223 rcu_read_lock();
224 memcg = mem_cgroup_from_task(current);
225
226 tcp_prot_mem(memcg, vec[0], 0);
227 tcp_prot_mem(memcg, vec[1], 1);
228 tcp_prot_mem(memcg, vec[2], 2);
229 rcu_read_unlock();
230#endif
231
232 net->ipv4.sysctl_tcp_mem[0] = vec[0];
233 net->ipv4.sysctl_tcp_mem[1] = vec[1];
234 net->ipv4.sysctl_tcp_mem[2] = vec[2];
235
236 return 0;
237}
238
239static int proc_tcp_fastopen_key(struct ctl_table *ctl, int write, 203static int proc_tcp_fastopen_key(struct ctl_table *ctl, int write,
240 void __user *buffer, size_t *lenp, 204 void __user *buffer, size_t *lenp,
241 loff_t *ppos) 205 loff_t *ppos)
@@ -267,6 +231,11 @@ static int proc_tcp_fastopen_key(struct ctl_table *ctl, int write,
267 ret = -EINVAL; 231 ret = -EINVAL;
268 goto bad_key; 232 goto bad_key;
269 } 233 }
234 /* Generate a dummy secret but don't publish it. This
235 * is needed so we don't regenerate a new key on the
236 * first invocation of tcp_fastopen_cookie_gen
237 */
238 tcp_fastopen_init_key_once(false);
270 tcp_fastopen_reset_cipher(user_key, TCP_FASTOPEN_KEY_LENGTH); 239 tcp_fastopen_reset_cipher(user_key, TCP_FASTOPEN_KEY_LENGTH);
271 } 240 }
272 241
@@ -475,13 +444,6 @@ static struct ctl_table ipv4_table[] = {
475 .proc_handler = proc_dointvec 444 .proc_handler = proc_dointvec
476 }, 445 },
477 { 446 {
478 .procname = "ip_local_port_range",
479 .data = &sysctl_local_ports.range,
480 .maxlen = sizeof(sysctl_local_ports.range),
481 .mode = 0644,
482 .proc_handler = ipv4_local_port_range,
483 },
484 {
485 .procname = "ip_local_reserved_ports", 447 .procname = "ip_local_reserved_ports",
486 .data = NULL, /* initialized in sysctl_ipv4_init */ 448 .data = NULL, /* initialized in sysctl_ipv4_init */
487 .maxlen = 65536, 449 .maxlen = 65536,
@@ -552,6 +514,13 @@ static struct ctl_table ipv4_table[] = {
552 .proc_handler = proc_dointvec 514 .proc_handler = proc_dointvec
553 }, 515 },
554 { 516 {
517 .procname = "tcp_mem",
518 .maxlen = sizeof(sysctl_tcp_mem),
519 .data = &sysctl_tcp_mem,
520 .mode = 0644,
521 .proc_handler = proc_doulongvec_minmax,
522 },
523 {
555 .procname = "tcp_wmem", 524 .procname = "tcp_wmem",
556 .data = &sysctl_tcp_wmem, 525 .data = &sysctl_tcp_wmem,
557 .maxlen = sizeof(sysctl_tcp_wmem), 526 .maxlen = sizeof(sysctl_tcp_wmem),
@@ -732,13 +701,6 @@ static struct ctl_table ipv4_table[] = {
732 .proc_handler = proc_allowed_congestion_control, 701 .proc_handler = proc_allowed_congestion_control,
733 }, 702 },
734 { 703 {
735 .procname = "tcp_max_ssthresh",
736 .data = &sysctl_tcp_max_ssthresh,
737 .maxlen = sizeof(int),
738 .mode = 0644,
739 .proc_handler = proc_dointvec,
740 },
741 {
742 .procname = "tcp_thin_linear_timeouts", 704 .procname = "tcp_thin_linear_timeouts",
743 .data = &sysctl_tcp_thin_linear_timeouts, 705 .data = &sysctl_tcp_thin_linear_timeouts,
744 .maxlen = sizeof(int), 706 .maxlen = sizeof(int),
@@ -854,10 +816,11 @@ static struct ctl_table ipv4_net_table[] = {
854 .proc_handler = proc_dointvec 816 .proc_handler = proc_dointvec
855 }, 817 },
856 { 818 {
857 .procname = "tcp_mem", 819 .procname = "ip_local_port_range",
858 .maxlen = sizeof(init_net.ipv4.sysctl_tcp_mem), 820 .maxlen = sizeof(init_net.ipv4.sysctl_local_ports.range),
821 .data = &init_net.ipv4.sysctl_local_ports.range,
859 .mode = 0644, 822 .mode = 0644,
860 .proc_handler = ipv4_tcp_mem, 823 .proc_handler = ipv4_local_port_range,
861 }, 824 },
862 { } 825 { }
863}; 826};
@@ -868,30 +831,15 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
868 831
869 table = ipv4_net_table; 832 table = ipv4_net_table;
870 if (!net_eq(net, &init_net)) { 833 if (!net_eq(net, &init_net)) {
834 int i;
835
871 table = kmemdup(table, sizeof(ipv4_net_table), GFP_KERNEL); 836 table = kmemdup(table, sizeof(ipv4_net_table), GFP_KERNEL);
872 if (table == NULL) 837 if (table == NULL)
873 goto err_alloc; 838 goto err_alloc;
874 839
875 table[0].data = 840 /* Update the variables to point into the current struct net */
876 &net->ipv4.sysctl_icmp_echo_ignore_all; 841 for (i = 0; i < ARRAY_SIZE(ipv4_net_table) - 1; i++)
877 table[1].data = 842 table[i].data += (void *)net - (void *)&init_net;
878 &net->ipv4.sysctl_icmp_echo_ignore_broadcasts;
879 table[2].data =
880 &net->ipv4.sysctl_icmp_ignore_bogus_error_responses;
881 table[3].data =
882 &net->ipv4.sysctl_icmp_errors_use_inbound_ifaddr;
883 table[4].data =
884 &net->ipv4.sysctl_icmp_ratelimit;
885 table[5].data =
886 &net->ipv4.sysctl_icmp_ratemask;
887 table[6].data =
888 &net->ipv4.sysctl_ping_group_range;
889 table[7].data =
890 &net->ipv4.sysctl_tcp_ecn;
891
892 /* Don't export sysctls to unprivileged users */
893 if (net->user_ns != &init_user_ns)
894 table[0].procname = NULL;
895 } 843 }
896 844
897 /* 845 /*
@@ -901,7 +849,12 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
901 net->ipv4.sysctl_ping_group_range[0] = make_kgid(&init_user_ns, 1); 849 net->ipv4.sysctl_ping_group_range[0] = make_kgid(&init_user_ns, 1);
902 net->ipv4.sysctl_ping_group_range[1] = make_kgid(&init_user_ns, 0); 850 net->ipv4.sysctl_ping_group_range[1] = make_kgid(&init_user_ns, 0);
903 851
904 tcp_init_mem(net); 852 /*
853 * Set defaults for local port range
854 */
855 seqlock_init(&net->ipv4.sysctl_local_ports.lock);
856 net->ipv4.sysctl_local_ports.range[0] = 32768;
857 net->ipv4.sysctl_local_ports.range[1] = 61000;
905 858
906 net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table); 859 net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table);
907 if (net->ipv4.ipv4_hdr == NULL) 860 if (net->ipv4.ipv4_hdr == NULL)
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 6e5617b9f9db..c4638e6f0238 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -288,9 +288,11 @@ int sysctl_tcp_min_tso_segs __read_mostly = 2;
288struct percpu_counter tcp_orphan_count; 288struct percpu_counter tcp_orphan_count;
289EXPORT_SYMBOL_GPL(tcp_orphan_count); 289EXPORT_SYMBOL_GPL(tcp_orphan_count);
290 290
291long sysctl_tcp_mem[3] __read_mostly;
291int sysctl_tcp_wmem[3] __read_mostly; 292int sysctl_tcp_wmem[3] __read_mostly;
292int sysctl_tcp_rmem[3] __read_mostly; 293int sysctl_tcp_rmem[3] __read_mostly;
293 294
295EXPORT_SYMBOL(sysctl_tcp_mem);
294EXPORT_SYMBOL(sysctl_tcp_rmem); 296EXPORT_SYMBOL(sysctl_tcp_rmem);
295EXPORT_SYMBOL(sysctl_tcp_wmem); 297EXPORT_SYMBOL(sysctl_tcp_wmem);
296 298
@@ -806,12 +808,6 @@ static unsigned int tcp_xmit_size_goal(struct sock *sk, u32 mss_now,
806 xmit_size_goal = min_t(u32, gso_size, 808 xmit_size_goal = min_t(u32, gso_size,
807 sk->sk_gso_max_size - 1 - hlen); 809 sk->sk_gso_max_size - 1 - hlen);
808 810
809 /* TSQ : try to have at least two segments in flight
810 * (one in NIC TX ring, another in Qdisc)
811 */
812 xmit_size_goal = min_t(u32, xmit_size_goal,
813 sysctl_tcp_limit_output_bytes >> 1);
814
815 xmit_size_goal = tcp_bound_to_half_wnd(tp, xmit_size_goal); 811 xmit_size_goal = tcp_bound_to_half_wnd(tp, xmit_size_goal);
816 812
817 /* We try hard to avoid divides here */ 813 /* We try hard to avoid divides here */
@@ -1429,7 +1425,7 @@ static void tcp_service_net_dma(struct sock *sk, bool wait)
1429 do { 1425 do {
1430 if (dma_async_is_tx_complete(tp->ucopy.dma_chan, 1426 if (dma_async_is_tx_complete(tp->ucopy.dma_chan,
1431 last_issued, &done, 1427 last_issued, &done,
1432 &used) == DMA_SUCCESS) { 1428 &used) == DMA_COMPLETE) {
1433 /* Safe to free early-copied skbs now */ 1429 /* Safe to free early-copied skbs now */
1434 __skb_queue_purge(&sk->sk_async_wait_queue); 1430 __skb_queue_purge(&sk->sk_async_wait_queue);
1435 break; 1431 break;
@@ -1437,7 +1433,7 @@ static void tcp_service_net_dma(struct sock *sk, bool wait)
1437 struct sk_buff *skb; 1433 struct sk_buff *skb;
1438 while ((skb = skb_peek(&sk->sk_async_wait_queue)) && 1434 while ((skb = skb_peek(&sk->sk_async_wait_queue)) &&
1439 (dma_async_is_complete(skb->dma_cookie, done, 1435 (dma_async_is_complete(skb->dma_cookie, done,
1440 used) == DMA_SUCCESS)) { 1436 used) == DMA_COMPLETE)) {
1441 __skb_dequeue(&sk->sk_async_wait_queue); 1437 __skb_dequeue(&sk->sk_async_wait_queue);
1442 kfree_skb(skb); 1438 kfree_skb(skb);
1443 } 1439 }
@@ -3097,13 +3093,13 @@ static int __init set_thash_entries(char *str)
3097} 3093}
3098__setup("thash_entries=", set_thash_entries); 3094__setup("thash_entries=", set_thash_entries);
3099 3095
3100void tcp_init_mem(struct net *net) 3096static void tcp_init_mem(void)
3101{ 3097{
3102 unsigned long limit = nr_free_buffer_pages() / 8; 3098 unsigned long limit = nr_free_buffer_pages() / 8;
3103 limit = max(limit, 128UL); 3099 limit = max(limit, 128UL);
3104 net->ipv4.sysctl_tcp_mem[0] = limit / 4 * 3; 3100 sysctl_tcp_mem[0] = limit / 4 * 3;
3105 net->ipv4.sysctl_tcp_mem[1] = limit; 3101 sysctl_tcp_mem[1] = limit;
3106 net->ipv4.sysctl_tcp_mem[2] = net->ipv4.sysctl_tcp_mem[0] * 2; 3102 sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
3107} 3103}
3108 3104
3109void __init tcp_init(void) 3105void __init tcp_init(void)
@@ -3137,10 +3133,9 @@ void __init tcp_init(void)
3137 &tcp_hashinfo.ehash_mask, 3133 &tcp_hashinfo.ehash_mask,
3138 0, 3134 0,
3139 thash_entries ? 0 : 512 * 1024); 3135 thash_entries ? 0 : 512 * 1024);
3140 for (i = 0; i <= tcp_hashinfo.ehash_mask; i++) { 3136 for (i = 0; i <= tcp_hashinfo.ehash_mask; i++)
3141 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i); 3137 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
3142 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].twchain, i); 3138
3143 }
3144 if (inet_ehash_locks_alloc(&tcp_hashinfo)) 3139 if (inet_ehash_locks_alloc(&tcp_hashinfo))
3145 panic("TCP: failed to alloc ehash_locks"); 3140 panic("TCP: failed to alloc ehash_locks");
3146 tcp_hashinfo.bhash = 3141 tcp_hashinfo.bhash =
@@ -3166,7 +3161,7 @@ void __init tcp_init(void)
3166 sysctl_tcp_max_orphans = cnt / 2; 3161 sysctl_tcp_max_orphans = cnt / 2;
3167 sysctl_max_syn_backlog = max(128, cnt / 256); 3162 sysctl_max_syn_backlog = max(128, cnt / 256);
3168 3163
3169 tcp_init_mem(&init_net); 3164 tcp_init_mem();
3170 /* Set per-socket limits to no more than 1/128 the pressure threshold */ 3165 /* Set per-socket limits to no more than 1/128 the pressure threshold */
3171 limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7); 3166 limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
3172 max_wshare = min(4UL*1024*1024, limit); 3167 max_wshare = min(4UL*1024*1024, limit);
diff --git a/net/ipv4/tcp_bic.c b/net/ipv4/tcp_bic.c
index f45e1c242440..821846fb0a7e 100644
--- a/net/ipv4/tcp_bic.c
+++ b/net/ipv4/tcp_bic.c
@@ -140,7 +140,8 @@ static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
140 ca->cnt = 1; 140 ca->cnt = 1;
141} 141}
142 142
143static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 143static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked,
144 u32 in_flight)
144{ 145{
145 struct tcp_sock *tp = tcp_sk(sk); 146 struct tcp_sock *tp = tcp_sk(sk);
146 struct bictcp *ca = inet_csk_ca(sk); 147 struct bictcp *ca = inet_csk_ca(sk);
@@ -149,7 +150,7 @@ static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
149 return; 150 return;
150 151
151 if (tp->snd_cwnd <= tp->snd_ssthresh) 152 if (tp->snd_cwnd <= tp->snd_ssthresh)
152 tcp_slow_start(tp); 153 tcp_slow_start(tp, acked);
153 else { 154 else {
154 bictcp_update(ca, tp->snd_cwnd); 155 bictcp_update(ca, tp->snd_cwnd);
155 tcp_cong_avoid_ai(tp, ca->cnt); 156 tcp_cong_avoid_ai(tp, ca->cnt);
diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
index 019c2389a341..ad37bf18ae4b 100644
--- a/net/ipv4/tcp_cong.c
+++ b/net/ipv4/tcp_cong.c
@@ -15,8 +15,6 @@
15#include <linux/gfp.h> 15#include <linux/gfp.h>
16#include <net/tcp.h> 16#include <net/tcp.h>
17 17
18int sysctl_tcp_max_ssthresh = 0;
19
20static DEFINE_SPINLOCK(tcp_cong_list_lock); 18static DEFINE_SPINLOCK(tcp_cong_list_lock);
21static LIST_HEAD(tcp_cong_list); 19static LIST_HEAD(tcp_cong_list);
22 20
@@ -299,35 +297,24 @@ bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight)
299} 297}
300EXPORT_SYMBOL_GPL(tcp_is_cwnd_limited); 298EXPORT_SYMBOL_GPL(tcp_is_cwnd_limited);
301 299
302/* 300/* Slow start is used when congestion window is no greater than the slow start
303 * Slow start is used when congestion window is less than slow start 301 * threshold. We base on RFC2581 and also handle stretch ACKs properly.
304 * threshold. This version implements the basic RFC2581 version 302 * We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but
305 * and optionally supports: 303 * something better;) a packet is only considered (s)acked in its entirety to
306 * RFC3742 Limited Slow Start - growth limited to max_ssthresh 304 * defend the ACK attacks described in the RFC. Slow start processes a stretch
307 * RFC3465 Appropriate Byte Counting - growth limited by bytes acknowledged 305 * ACK of degree N as if N acks of degree 1 are received back to back except
306 * ABC caps N to 2. Slow start exits when cwnd grows over ssthresh and
307 * returns the leftover acks to adjust cwnd in congestion avoidance mode.
308 */ 308 */
309void tcp_slow_start(struct tcp_sock *tp) 309int tcp_slow_start(struct tcp_sock *tp, u32 acked)
310{ 310{
311 int cnt; /* increase in packets */ 311 u32 cwnd = tp->snd_cwnd + acked;
312 unsigned int delta = 0;
313 u32 snd_cwnd = tp->snd_cwnd;
314
315 if (unlikely(!snd_cwnd)) {
316 pr_err_once("snd_cwnd is nul, please report this bug.\n");
317 snd_cwnd = 1U;
318 }
319 312
320 if (sysctl_tcp_max_ssthresh > 0 && tp->snd_cwnd > sysctl_tcp_max_ssthresh) 313 if (cwnd > tp->snd_ssthresh)
321 cnt = sysctl_tcp_max_ssthresh >> 1; /* limited slow start */ 314 cwnd = tp->snd_ssthresh + 1;
322 else 315 acked -= cwnd - tp->snd_cwnd;
323 cnt = snd_cwnd; /* exponential increase */ 316 tp->snd_cwnd = min(cwnd, tp->snd_cwnd_clamp);
324 317 return acked;
325 tp->snd_cwnd_cnt += cnt;
326 while (tp->snd_cwnd_cnt >= snd_cwnd) {
327 tp->snd_cwnd_cnt -= snd_cwnd;
328 delta++;
329 }
330 tp->snd_cwnd = min(snd_cwnd + delta, tp->snd_cwnd_clamp);
331} 318}
332EXPORT_SYMBOL_GPL(tcp_slow_start); 319EXPORT_SYMBOL_GPL(tcp_slow_start);
333 320
@@ -351,7 +338,7 @@ EXPORT_SYMBOL_GPL(tcp_cong_avoid_ai);
351/* This is Jacobson's slow start and congestion avoidance. 338/* This is Jacobson's slow start and congestion avoidance.
352 * SIGCOMM '88, p. 328. 339 * SIGCOMM '88, p. 328.
353 */ 340 */
354void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 341void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight)
355{ 342{
356 struct tcp_sock *tp = tcp_sk(sk); 343 struct tcp_sock *tp = tcp_sk(sk);
357 344
@@ -360,7 +347,7 @@ void tcp_reno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
360 347
361 /* In "safe" area, increase. */ 348 /* In "safe" area, increase. */
362 if (tp->snd_cwnd <= tp->snd_ssthresh) 349 if (tp->snd_cwnd <= tp->snd_ssthresh)
363 tcp_slow_start(tp); 350 tcp_slow_start(tp, acked);
364 /* In dangerous area, increase slowly. */ 351 /* In dangerous area, increase slowly. */
365 else 352 else
366 tcp_cong_avoid_ai(tp, tp->snd_cwnd); 353 tcp_cong_avoid_ai(tp, tp->snd_cwnd);
diff --git a/net/ipv4/tcp_cubic.c b/net/ipv4/tcp_cubic.c
index b6ae92a51f58..828e4c3ffbaf 100644
--- a/net/ipv4/tcp_cubic.c
+++ b/net/ipv4/tcp_cubic.c
@@ -304,7 +304,8 @@ static inline void bictcp_update(struct bictcp *ca, u32 cwnd)
304 ca->cnt = 1; 304 ca->cnt = 1;
305} 305}
306 306
307static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 307static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 acked,
308 u32 in_flight)
308{ 309{
309 struct tcp_sock *tp = tcp_sk(sk); 310 struct tcp_sock *tp = tcp_sk(sk);
310 struct bictcp *ca = inet_csk_ca(sk); 311 struct bictcp *ca = inet_csk_ca(sk);
@@ -315,7 +316,7 @@ static void bictcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
315 if (tp->snd_cwnd <= tp->snd_ssthresh) { 316 if (tp->snd_cwnd <= tp->snd_ssthresh) {
316 if (hystart && after(ack, ca->end_seq)) 317 if (hystart && after(ack, ca->end_seq))
317 bictcp_hystart_reset(sk); 318 bictcp_hystart_reset(sk);
318 tcp_slow_start(tp); 319 tcp_slow_start(tp, acked);
319 } else { 320 } else {
320 bictcp_update(ca, tp->snd_cwnd); 321 bictcp_update(ca, tp->snd_cwnd);
321 tcp_cong_avoid_ai(tp, ca->cnt); 322 tcp_cong_avoid_ai(tp, ca->cnt);
diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index ab7bd35bb312..f195d9316e55 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -8,12 +8,26 @@
8#include <net/inetpeer.h> 8#include <net/inetpeer.h>
9#include <net/tcp.h> 9#include <net/tcp.h>
10 10
11int sysctl_tcp_fastopen __read_mostly; 11int sysctl_tcp_fastopen __read_mostly = TFO_CLIENT_ENABLE;
12 12
13struct tcp_fastopen_context __rcu *tcp_fastopen_ctx; 13struct tcp_fastopen_context __rcu *tcp_fastopen_ctx;
14 14
15static DEFINE_SPINLOCK(tcp_fastopen_ctx_lock); 15static DEFINE_SPINLOCK(tcp_fastopen_ctx_lock);
16 16
17void tcp_fastopen_init_key_once(bool publish)
18{
19 static u8 key[TCP_FASTOPEN_KEY_LENGTH];
20
21 /* tcp_fastopen_reset_cipher publishes the new context
22 * atomically, so we allow this race happening here.
23 *
24 * All call sites of tcp_fastopen_cookie_gen also check
25 * for a valid cookie, so this is an acceptable risk.
26 */
27 if (net_get_random_once(key, sizeof(key)) && publish)
28 tcp_fastopen_reset_cipher(key, sizeof(key));
29}
30
17static void tcp_fastopen_ctx_free(struct rcu_head *head) 31static void tcp_fastopen_ctx_free(struct rcu_head *head)
18{ 32{
19 struct tcp_fastopen_context *ctx = 33 struct tcp_fastopen_context *ctx =
@@ -70,6 +84,8 @@ void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
70 __be32 path[4] = { src, dst, 0, 0 }; 84 __be32 path[4] = { src, dst, 0, 0 };
71 struct tcp_fastopen_context *ctx; 85 struct tcp_fastopen_context *ctx;
72 86
87 tcp_fastopen_init_key_once(true);
88
73 rcu_read_lock(); 89 rcu_read_lock();
74 ctx = rcu_dereference(tcp_fastopen_ctx); 90 ctx = rcu_dereference(tcp_fastopen_ctx);
75 if (ctx) { 91 if (ctx) {
@@ -78,14 +94,3 @@ void tcp_fastopen_cookie_gen(__be32 src, __be32 dst,
78 } 94 }
79 rcu_read_unlock(); 95 rcu_read_unlock();
80} 96}
81
82static int __init tcp_fastopen_init(void)
83{
84 __u8 key[TCP_FASTOPEN_KEY_LENGTH];
85
86 get_random_bytes(key, sizeof(key));
87 tcp_fastopen_reset_cipher(key, sizeof(key));
88 return 0;
89}
90
91late_initcall(tcp_fastopen_init);
diff --git a/net/ipv4/tcp_highspeed.c b/net/ipv4/tcp_highspeed.c
index 30f27f6b3655..8ed9305dfdf4 100644
--- a/net/ipv4/tcp_highspeed.c
+++ b/net/ipv4/tcp_highspeed.c
@@ -109,7 +109,7 @@ static void hstcp_init(struct sock *sk)
109 tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128); 109 tp->snd_cwnd_clamp = min_t(u32, tp->snd_cwnd_clamp, 0xffffffff/128);
110} 110}
111 111
112static void hstcp_cong_avoid(struct sock *sk, u32 adk, u32 in_flight) 112static void hstcp_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight)
113{ 113{
114 struct tcp_sock *tp = tcp_sk(sk); 114 struct tcp_sock *tp = tcp_sk(sk);
115 struct hstcp *ca = inet_csk_ca(sk); 115 struct hstcp *ca = inet_csk_ca(sk);
@@ -118,7 +118,7 @@ static void hstcp_cong_avoid(struct sock *sk, u32 adk, u32 in_flight)
118 return; 118 return;
119 119
120 if (tp->snd_cwnd <= tp->snd_ssthresh) 120 if (tp->snd_cwnd <= tp->snd_ssthresh)
121 tcp_slow_start(tp); 121 tcp_slow_start(tp, acked);
122 else { 122 else {
123 /* Update AIMD parameters. 123 /* Update AIMD parameters.
124 * 124 *
diff --git a/net/ipv4/tcp_htcp.c b/net/ipv4/tcp_htcp.c
index c1a8175361e8..4a194acfd923 100644
--- a/net/ipv4/tcp_htcp.c
+++ b/net/ipv4/tcp_htcp.c
@@ -227,7 +227,7 @@ static u32 htcp_recalc_ssthresh(struct sock *sk)
227 return max((tp->snd_cwnd * ca->beta) >> 7, 2U); 227 return max((tp->snd_cwnd * ca->beta) >> 7, 2U);
228} 228}
229 229
230static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 230static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight)
231{ 231{
232 struct tcp_sock *tp = tcp_sk(sk); 232 struct tcp_sock *tp = tcp_sk(sk);
233 struct htcp *ca = inet_csk_ca(sk); 233 struct htcp *ca = inet_csk_ca(sk);
@@ -236,7 +236,7 @@ static void htcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
236 return; 236 return;
237 237
238 if (tp->snd_cwnd <= tp->snd_ssthresh) 238 if (tp->snd_cwnd <= tp->snd_ssthresh)
239 tcp_slow_start(tp); 239 tcp_slow_start(tp, acked);
240 else { 240 else {
241 /* In dangerous area, increase slowly. 241 /* In dangerous area, increase slowly.
242 * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd 242 * In theory this is tp->snd_cwnd += alpha / tp->snd_cwnd
diff --git a/net/ipv4/tcp_hybla.c b/net/ipv4/tcp_hybla.c
index 57bdd17dff4d..478fe82611bf 100644
--- a/net/ipv4/tcp_hybla.c
+++ b/net/ipv4/tcp_hybla.c
@@ -85,7 +85,8 @@ static inline u32 hybla_fraction(u32 odds)
85 * o Give cwnd a new value based on the model proposed 85 * o Give cwnd a new value based on the model proposed
86 * o remember increments <1 86 * o remember increments <1
87 */ 87 */
88static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 88static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 acked,
89 u32 in_flight)
89{ 90{
90 struct tcp_sock *tp = tcp_sk(sk); 91 struct tcp_sock *tp = tcp_sk(sk);
91 struct hybla *ca = inet_csk_ca(sk); 92 struct hybla *ca = inet_csk_ca(sk);
@@ -102,7 +103,7 @@ static void hybla_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
102 return; 103 return;
103 104
104 if (!ca->hybla_en) { 105 if (!ca->hybla_en) {
105 tcp_reno_cong_avoid(sk, ack, in_flight); 106 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
106 return; 107 return;
107 } 108 }
108 109
diff --git a/net/ipv4/tcp_illinois.c b/net/ipv4/tcp_illinois.c
index 834857f3c871..8a520996f3d2 100644
--- a/net/ipv4/tcp_illinois.c
+++ b/net/ipv4/tcp_illinois.c
@@ -256,7 +256,8 @@ static void tcp_illinois_state(struct sock *sk, u8 new_state)
256/* 256/*
257 * Increase window in response to successful acknowledgment. 257 * Increase window in response to successful acknowledgment.
258 */ 258 */
259static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 259static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 acked,
260 u32 in_flight)
260{ 261{
261 struct tcp_sock *tp = tcp_sk(sk); 262 struct tcp_sock *tp = tcp_sk(sk);
262 struct illinois *ca = inet_csk_ca(sk); 263 struct illinois *ca = inet_csk_ca(sk);
@@ -270,7 +271,7 @@ static void tcp_illinois_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
270 271
271 /* In slow start */ 272 /* In slow start */
272 if (tp->snd_cwnd <= tp->snd_ssthresh) 273 if (tp->snd_cwnd <= tp->snd_ssthresh)
273 tcp_slow_start(tp); 274 tcp_slow_start(tp, acked);
274 275
275 else { 276 else {
276 u32 delta; 277 u32 delta;
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index a16b01b537ba..c53b7f35c51d 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -267,11 +267,31 @@ static bool TCP_ECN_rcv_ecn_echo(const struct tcp_sock *tp, const struct tcphdr
267 * 1. Tuning sk->sk_sndbuf, when connection enters established state. 267 * 1. Tuning sk->sk_sndbuf, when connection enters established state.
268 */ 268 */
269 269
270static void tcp_fixup_sndbuf(struct sock *sk) 270static void tcp_sndbuf_expand(struct sock *sk)
271{ 271{
272 int sndmem = SKB_TRUESIZE(tcp_sk(sk)->rx_opt.mss_clamp + MAX_TCP_HEADER); 272 const struct tcp_sock *tp = tcp_sk(sk);
273 int sndmem, per_mss;
274 u32 nr_segs;
275
276 /* Worst case is non GSO/TSO : each frame consumes one skb
277 * and skb->head is kmalloced using power of two area of memory
278 */
279 per_mss = max_t(u32, tp->rx_opt.mss_clamp, tp->mss_cache) +
280 MAX_TCP_HEADER +
281 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
282
283 per_mss = roundup_pow_of_two(per_mss) +
284 SKB_DATA_ALIGN(sizeof(struct sk_buff));
285
286 nr_segs = max_t(u32, TCP_INIT_CWND, tp->snd_cwnd);
287 nr_segs = max_t(u32, nr_segs, tp->reordering + 1);
288
289 /* Fast Recovery (RFC 5681 3.2) :
290 * Cubic needs 1.7 factor, rounded to 2 to include
291 * extra cushion (application might react slowly to POLLOUT)
292 */
293 sndmem = 2 * nr_segs * per_mss;
273 294
274 sndmem *= TCP_INIT_CWND;
275 if (sk->sk_sndbuf < sndmem) 295 if (sk->sk_sndbuf < sndmem)
276 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]); 296 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
277} 297}
@@ -355,6 +375,12 @@ static void tcp_fixup_rcvbuf(struct sock *sk)
355 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) * 375 rcvmem = 2 * SKB_TRUESIZE(mss + MAX_TCP_HEADER) *
356 tcp_default_init_rwnd(mss); 376 tcp_default_init_rwnd(mss);
357 377
378 /* Dynamic Right Sizing (DRS) has 2 to 3 RTT latency
379 * Allow enough cushion so that sender is not limited by our window
380 */
381 if (sysctl_tcp_moderate_rcvbuf)
382 rcvmem <<= 2;
383
358 if (sk->sk_rcvbuf < rcvmem) 384 if (sk->sk_rcvbuf < rcvmem)
359 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]); 385 sk->sk_rcvbuf = min(rcvmem, sysctl_tcp_rmem[2]);
360} 386}
@@ -370,9 +396,11 @@ void tcp_init_buffer_space(struct sock *sk)
370 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) 396 if (!(sk->sk_userlocks & SOCK_RCVBUF_LOCK))
371 tcp_fixup_rcvbuf(sk); 397 tcp_fixup_rcvbuf(sk);
372 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK)) 398 if (!(sk->sk_userlocks & SOCK_SNDBUF_LOCK))
373 tcp_fixup_sndbuf(sk); 399 tcp_sndbuf_expand(sk);
374 400
375 tp->rcvq_space.space = tp->rcv_wnd; 401 tp->rcvq_space.space = tp->rcv_wnd;
402 tp->rcvq_space.time = tcp_time_stamp;
403 tp->rcvq_space.seq = tp->copied_seq;
376 404
377 maxwin = tcp_full_space(sk); 405 maxwin = tcp_full_space(sk);
378 406
@@ -512,48 +540,62 @@ void tcp_rcv_space_adjust(struct sock *sk)
512{ 540{
513 struct tcp_sock *tp = tcp_sk(sk); 541 struct tcp_sock *tp = tcp_sk(sk);
514 int time; 542 int time;
515 int space; 543 int copied;
516
517 if (tp->rcvq_space.time == 0)
518 goto new_measure;
519 544
520 time = tcp_time_stamp - tp->rcvq_space.time; 545 time = tcp_time_stamp - tp->rcvq_space.time;
521 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0) 546 if (time < (tp->rcv_rtt_est.rtt >> 3) || tp->rcv_rtt_est.rtt == 0)
522 return; 547 return;
523 548
524 space = 2 * (tp->copied_seq - tp->rcvq_space.seq); 549 /* Number of bytes copied to user in last RTT */
550 copied = tp->copied_seq - tp->rcvq_space.seq;
551 if (copied <= tp->rcvq_space.space)
552 goto new_measure;
553
554 /* A bit of theory :
555 * copied = bytes received in previous RTT, our base window
556 * To cope with packet losses, we need a 2x factor
557 * To cope with slow start, and sender growing its cwin by 100 %
558 * every RTT, we need a 4x factor, because the ACK we are sending
559 * now is for the next RTT, not the current one :
560 * <prev RTT . ><current RTT .. ><next RTT .... >
561 */
562
563 if (sysctl_tcp_moderate_rcvbuf &&
564 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
565 int rcvwin, rcvmem, rcvbuf;
525 566
526 space = max(tp->rcvq_space.space, space); 567 /* minimal window to cope with packet losses, assuming
568 * steady state. Add some cushion because of small variations.
569 */
570 rcvwin = (copied << 1) + 16 * tp->advmss;
527 571
528 if (tp->rcvq_space.space != space) { 572 /* If rate increased by 25%,
529 int rcvmem; 573 * assume slow start, rcvwin = 3 * copied
574 * If rate increased by 50%,
575 * assume sender can use 2x growth, rcvwin = 4 * copied
576 */
577 if (copied >=
578 tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
579 if (copied >=
580 tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
581 rcvwin <<= 1;
582 else
583 rcvwin += (rcvwin >> 1);
584 }
530 585
531 tp->rcvq_space.space = space; 586 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
587 while (tcp_win_from_space(rcvmem) < tp->advmss)
588 rcvmem += 128;
532 589
533 if (sysctl_tcp_moderate_rcvbuf && 590 rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]);
534 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 591 if (rcvbuf > sk->sk_rcvbuf) {
535 int new_clamp = space; 592 sk->sk_rcvbuf = rcvbuf;
536 593
537 /* Receive space grows, normalize in order to 594 /* Make the window clamp follow along. */
538 * take into account packet headers and sk_buff 595 tp->window_clamp = rcvwin;
539 * structure overhead.
540 */
541 space /= tp->advmss;
542 if (!space)
543 space = 1;
544 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
545 while (tcp_win_from_space(rcvmem) < tp->advmss)
546 rcvmem += 128;
547 space *= rcvmem;
548 space = min(space, sysctl_tcp_rmem[2]);
549 if (space > sk->sk_rcvbuf) {
550 sk->sk_rcvbuf = space;
551
552 /* Make the window clamp follow along. */
553 tp->window_clamp = new_clamp;
554 }
555 } 596 }
556 } 597 }
598 tp->rcvq_space.space = copied;
557 599
558new_measure: 600new_measure:
559 tp->rcvq_space.seq = tp->copied_seq; 601 tp->rcvq_space.seq = tp->copied_seq;
@@ -713,7 +755,12 @@ static void tcp_update_pacing_rate(struct sock *sk)
713 if (tp->srtt > 8 + 2) 755 if (tp->srtt > 8 + 2)
714 do_div(rate, tp->srtt); 756 do_div(rate, tp->srtt);
715 757
716 sk->sk_pacing_rate = min_t(u64, rate, ~0U); 758 /* ACCESS_ONCE() is needed because sch_fq fetches sk_pacing_rate
759 * without any lock. We want to make sure compiler wont store
760 * intermediate values in this location.
761 */
762 ACCESS_ONCE(sk->sk_pacing_rate) = min_t(u64, rate,
763 sk->sk_max_pacing_rate);
717} 764}
718 765
719/* Calculate rto without backoff. This is the second half of Van Jacobson's 766/* Calculate rto without backoff. This is the second half of Van Jacobson's
@@ -2856,7 +2903,8 @@ static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2856 * left edge of the send window. 2903 * left edge of the send window.
2857 * See draft-ietf-tcplw-high-performance-00, section 3.3. 2904 * See draft-ietf-tcplw-high-performance-00, section 3.3.
2858 */ 2905 */
2859 if (seq_rtt < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr) 2906 if (seq_rtt < 0 && tp->rx_opt.saw_tstamp && tp->rx_opt.rcv_tsecr &&
2907 flag & FLAG_ACKED)
2860 seq_rtt = tcp_time_stamp - tp->rx_opt.rcv_tsecr; 2908 seq_rtt = tcp_time_stamp - tp->rx_opt.rcv_tsecr;
2861 2909
2862 if (seq_rtt < 0) 2910 if (seq_rtt < 0)
@@ -2871,20 +2919,25 @@ static inline bool tcp_ack_update_rtt(struct sock *sk, const int flag,
2871} 2919}
2872 2920
2873/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */ 2921/* Compute time elapsed between (last) SYNACK and the ACK completing 3WHS. */
2874static void tcp_synack_rtt_meas(struct sock *sk, struct request_sock *req) 2922static void tcp_synack_rtt_meas(struct sock *sk, const u32 synack_stamp)
2875{ 2923{
2876 struct tcp_sock *tp = tcp_sk(sk); 2924 struct tcp_sock *tp = tcp_sk(sk);
2877 s32 seq_rtt = -1; 2925 s32 seq_rtt = -1;
2878 2926
2879 if (tp->lsndtime && !tp->total_retrans) 2927 if (synack_stamp && !tp->total_retrans)
2880 seq_rtt = tcp_time_stamp - tp->lsndtime; 2928 seq_rtt = tcp_time_stamp - synack_stamp;
2881 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt, -1); 2929
2930 /* If the ACK acks both the SYNACK and the (Fast Open'd) data packets
2931 * sent in SYN_RECV, SYNACK RTT is the smooth RTT computed in tcp_ack()
2932 */
2933 if (!tp->srtt)
2934 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt, -1);
2882} 2935}
2883 2936
2884static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 2937static void tcp_cong_avoid(struct sock *sk, u32 ack, u32 acked, u32 in_flight)
2885{ 2938{
2886 const struct inet_connection_sock *icsk = inet_csk(sk); 2939 const struct inet_connection_sock *icsk = inet_csk(sk);
2887 icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight); 2940 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked, in_flight);
2888 tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp; 2941 tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
2889} 2942}
2890 2943
@@ -2973,7 +3026,7 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
2973 const struct inet_connection_sock *icsk = inet_csk(sk); 3026 const struct inet_connection_sock *icsk = inet_csk(sk);
2974 struct sk_buff *skb; 3027 struct sk_buff *skb;
2975 u32 now = tcp_time_stamp; 3028 u32 now = tcp_time_stamp;
2976 int fully_acked = true; 3029 bool fully_acked = true;
2977 int flag = 0; 3030 int flag = 0;
2978 u32 pkts_acked = 0; 3031 u32 pkts_acked = 0;
2979 u32 reord = tp->packets_out; 3032 u32 reord = tp->packets_out;
@@ -2981,6 +3034,7 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
2981 s32 seq_rtt = -1; 3034 s32 seq_rtt = -1;
2982 s32 ca_seq_rtt = -1; 3035 s32 ca_seq_rtt = -1;
2983 ktime_t last_ackt = net_invalid_timestamp(); 3036 ktime_t last_ackt = net_invalid_timestamp();
3037 bool rtt_update;
2984 3038
2985 while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) { 3039 while ((skb = tcp_write_queue_head(sk)) && skb != tcp_send_head(sk)) {
2986 struct tcp_skb_cb *scb = TCP_SKB_CB(skb); 3040 struct tcp_skb_cb *scb = TCP_SKB_CB(skb);
@@ -3057,14 +3111,13 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
3057 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED)) 3111 if (skb && (TCP_SKB_CB(skb)->sacked & TCPCB_SACKED_ACKED))
3058 flag |= FLAG_SACK_RENEGING; 3112 flag |= FLAG_SACK_RENEGING;
3059 3113
3060 if (tcp_ack_update_rtt(sk, flag, seq_rtt, sack_rtt) || 3114 rtt_update = tcp_ack_update_rtt(sk, flag, seq_rtt, sack_rtt);
3061 (flag & FLAG_ACKED))
3062 tcp_rearm_rto(sk);
3063 3115
3064 if (flag & FLAG_ACKED) { 3116 if (flag & FLAG_ACKED) {
3065 const struct tcp_congestion_ops *ca_ops 3117 const struct tcp_congestion_ops *ca_ops
3066 = inet_csk(sk)->icsk_ca_ops; 3118 = inet_csk(sk)->icsk_ca_ops;
3067 3119
3120 tcp_rearm_rto(sk);
3068 if (unlikely(icsk->icsk_mtup.probe_size && 3121 if (unlikely(icsk->icsk_mtup.probe_size &&
3069 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) { 3122 !after(tp->mtu_probe.probe_seq_end, tp->snd_una))) {
3070 tcp_mtup_probe_success(sk); 3123 tcp_mtup_probe_success(sk);
@@ -3103,6 +3156,13 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
3103 3156
3104 ca_ops->pkts_acked(sk, pkts_acked, rtt_us); 3157 ca_ops->pkts_acked(sk, pkts_acked, rtt_us);
3105 } 3158 }
3159 } else if (skb && rtt_update && sack_rtt >= 0 &&
3160 sack_rtt > (s32)(now - TCP_SKB_CB(skb)->when)) {
3161 /* Do not re-arm RTO if the sack RTT is measured from data sent
3162 * after when the head was last (re)transmitted. Otherwise the
3163 * timeout may continue to extend in loss recovery.
3164 */
3165 tcp_rearm_rto(sk);
3106 } 3166 }
3107 3167
3108#if FASTRETRANS_DEBUG > 0 3168#if FASTRETRANS_DEBUG > 0
@@ -3394,7 +3454,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
3394 3454
3395 /* Advance cwnd if state allows */ 3455 /* Advance cwnd if state allows */
3396 if (tcp_may_raise_cwnd(sk, flag)) 3456 if (tcp_may_raise_cwnd(sk, flag))
3397 tcp_cong_avoid(sk, ack, prior_in_flight); 3457 tcp_cong_avoid(sk, ack, acked, prior_in_flight);
3398 3458
3399 if (tcp_ack_is_dubious(sk, flag)) { 3459 if (tcp_ack_is_dubious(sk, flag)) {
3400 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP)); 3460 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
@@ -4704,15 +4764,7 @@ static void tcp_new_space(struct sock *sk)
4704 struct tcp_sock *tp = tcp_sk(sk); 4764 struct tcp_sock *tp = tcp_sk(sk);
4705 4765
4706 if (tcp_should_expand_sndbuf(sk)) { 4766 if (tcp_should_expand_sndbuf(sk)) {
4707 int sndmem = SKB_TRUESIZE(max_t(u32, 4767 tcp_sndbuf_expand(sk);
4708 tp->rx_opt.mss_clamp,
4709 tp->mss_cache) +
4710 MAX_TCP_HEADER);
4711 int demanded = max_t(unsigned int, tp->snd_cwnd,
4712 tp->reordering + 1);
4713 sndmem *= 2 * demanded;
4714 if (sndmem > sk->sk_sndbuf)
4715 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
4716 tp->snd_cwnd_stamp = tcp_time_stamp; 4768 tp->snd_cwnd_stamp = tcp_time_stamp;
4717 } 4769 }
4718 4770
@@ -5587,6 +5639,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5587 struct request_sock *req; 5639 struct request_sock *req;
5588 int queued = 0; 5640 int queued = 0;
5589 bool acceptable; 5641 bool acceptable;
5642 u32 synack_stamp;
5590 5643
5591 tp->rx_opt.saw_tstamp = 0; 5644 tp->rx_opt.saw_tstamp = 0;
5592 5645
@@ -5669,16 +5722,18 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5669 * so release it. 5722 * so release it.
5670 */ 5723 */
5671 if (req) { 5724 if (req) {
5725 synack_stamp = tcp_rsk(req)->snt_synack;
5672 tp->total_retrans = req->num_retrans; 5726 tp->total_retrans = req->num_retrans;
5673 reqsk_fastopen_remove(sk, req, false); 5727 reqsk_fastopen_remove(sk, req, false);
5674 } else { 5728 } else {
5729 synack_stamp = tp->lsndtime;
5675 /* Make sure socket is routed, for correct metrics. */ 5730 /* Make sure socket is routed, for correct metrics. */
5676 icsk->icsk_af_ops->rebuild_header(sk); 5731 icsk->icsk_af_ops->rebuild_header(sk);
5677 tcp_init_congestion_control(sk); 5732 tcp_init_congestion_control(sk);
5678 5733
5679 tcp_mtup_init(sk); 5734 tcp_mtup_init(sk);
5680 tcp_init_buffer_space(sk);
5681 tp->copied_seq = tp->rcv_nxt; 5735 tp->copied_seq = tp->rcv_nxt;
5736 tcp_init_buffer_space(sk);
5682 } 5737 }
5683 smp_mb(); 5738 smp_mb();
5684 tcp_set_state(sk, TCP_ESTABLISHED); 5739 tcp_set_state(sk, TCP_ESTABLISHED);
@@ -5694,7 +5749,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5694 tp->snd_una = TCP_SKB_CB(skb)->ack_seq; 5749 tp->snd_una = TCP_SKB_CB(skb)->ack_seq;
5695 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale; 5750 tp->snd_wnd = ntohs(th->window) << tp->rx_opt.snd_wscale;
5696 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq); 5751 tcp_init_wl(tp, TCP_SKB_CB(skb)->seq);
5697 tcp_synack_rtt_meas(sk, req); 5752 tcp_synack_rtt_meas(sk, synack_stamp);
5698 5753
5699 if (tp->rx_opt.tstamp_ok) 5754 if (tp->rx_opt.tstamp_ok)
5700 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED; 5755 tp->advmss -= TCPOLEN_TSTAMP_ALIGNED;
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index b14266bb91eb..067213924751 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -177,7 +177,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
177 if (IS_ERR(rt)) { 177 if (IS_ERR(rt)) {
178 err = PTR_ERR(rt); 178 err = PTR_ERR(rt);
179 if (err == -ENETUNREACH) 179 if (err == -ENETUNREACH)
180 IP_INC_STATS_BH(sock_net(sk), IPSTATS_MIB_OUTNOROUTES); 180 IP_INC_STATS(sock_net(sk), IPSTATS_MIB_OUTNOROUTES);
181 return err; 181 return err;
182 } 182 }
183 183
@@ -288,6 +288,7 @@ static void tcp_v4_mtu_reduced(struct sock *sk)
288 mtu = dst_mtu(dst); 288 mtu = dst_mtu(dst);
289 289
290 if (inet->pmtudisc != IP_PMTUDISC_DONT && 290 if (inet->pmtudisc != IP_PMTUDISC_DONT &&
291 ip_sk_accept_pmtu(sk) &&
291 inet_csk(sk)->icsk_pmtu_cookie > mtu) { 292 inet_csk(sk)->icsk_pmtu_cookie > mtu) {
292 tcp_sync_mss(sk, mtu); 293 tcp_sync_mss(sk, mtu);
293 294
@@ -835,11 +836,11 @@ static int tcp_v4_send_synack(struct sock *sk, struct dst_entry *dst,
835 skb = tcp_make_synack(sk, dst, req, NULL); 836 skb = tcp_make_synack(sk, dst, req, NULL);
836 837
837 if (skb) { 838 if (skb) {
838 __tcp_v4_send_check(skb, ireq->loc_addr, ireq->rmt_addr); 839 __tcp_v4_send_check(skb, ireq->ir_loc_addr, ireq->ir_rmt_addr);
839 840
840 skb_set_queue_mapping(skb, queue_mapping); 841 skb_set_queue_mapping(skb, queue_mapping);
841 err = ip_build_and_send_pkt(skb, sk, ireq->loc_addr, 842 err = ip_build_and_send_pkt(skb, sk, ireq->ir_loc_addr,
842 ireq->rmt_addr, 843 ireq->ir_rmt_addr,
843 ireq->opt); 844 ireq->opt);
844 err = net_xmit_eval(err); 845 err = net_xmit_eval(err);
845 if (!tcp_rsk(req)->snt_synack && !err) 846 if (!tcp_rsk(req)->snt_synack && !err)
@@ -972,7 +973,7 @@ static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
972{ 973{
973 union tcp_md5_addr *addr; 974 union tcp_md5_addr *addr;
974 975
975 addr = (union tcp_md5_addr *)&inet_rsk(req)->rmt_addr; 976 addr = (union tcp_md5_addr *)&inet_rsk(req)->ir_rmt_addr;
976 return tcp_md5_do_lookup(sk, addr, AF_INET); 977 return tcp_md5_do_lookup(sk, addr, AF_INET);
977} 978}
978 979
@@ -1149,8 +1150,8 @@ int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
1149 saddr = inet_sk(sk)->inet_saddr; 1150 saddr = inet_sk(sk)->inet_saddr;
1150 daddr = inet_sk(sk)->inet_daddr; 1151 daddr = inet_sk(sk)->inet_daddr;
1151 } else if (req) { 1152 } else if (req) {
1152 saddr = inet_rsk(req)->loc_addr; 1153 saddr = inet_rsk(req)->ir_loc_addr;
1153 daddr = inet_rsk(req)->rmt_addr; 1154 daddr = inet_rsk(req)->ir_rmt_addr;
1154 } else { 1155 } else {
1155 const struct iphdr *iph = ip_hdr(skb); 1156 const struct iphdr *iph = ip_hdr(skb);
1156 saddr = iph->saddr; 1157 saddr = iph->saddr;
@@ -1366,8 +1367,8 @@ static int tcp_v4_conn_req_fastopen(struct sock *sk,
1366 kfree_skb(skb_synack); 1367 kfree_skb(skb_synack);
1367 return -1; 1368 return -1;
1368 } 1369 }
1369 err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr, 1370 err = ip_build_and_send_pkt(skb_synack, sk, ireq->ir_loc_addr,
1370 ireq->rmt_addr, ireq->opt); 1371 ireq->ir_rmt_addr, ireq->opt);
1371 err = net_xmit_eval(err); 1372 err = net_xmit_eval(err);
1372 if (!err) 1373 if (!err)
1373 tcp_rsk(req)->snt_synack = tcp_time_stamp; 1374 tcp_rsk(req)->snt_synack = tcp_time_stamp;
@@ -1410,8 +1411,8 @@ static int tcp_v4_conn_req_fastopen(struct sock *sk,
1410 inet_csk(child)->icsk_af_ops->rebuild_header(child); 1411 inet_csk(child)->icsk_af_ops->rebuild_header(child);
1411 tcp_init_congestion_control(child); 1412 tcp_init_congestion_control(child);
1412 tcp_mtup_init(child); 1413 tcp_mtup_init(child);
1413 tcp_init_buffer_space(child);
1414 tcp_init_metrics(child); 1414 tcp_init_metrics(child);
1415 tcp_init_buffer_space(child);
1415 1416
1416 /* Queue the data carried in the SYN packet. We need to first 1417 /* Queue the data carried in the SYN packet. We need to first
1417 * bump skb's refcnt because the caller will attempt to free it. 1418 * bump skb's refcnt because the caller will attempt to free it.
@@ -1502,8 +1503,8 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1502 tcp_openreq_init(req, &tmp_opt, skb); 1503 tcp_openreq_init(req, &tmp_opt, skb);
1503 1504
1504 ireq = inet_rsk(req); 1505 ireq = inet_rsk(req);
1505 ireq->loc_addr = daddr; 1506 ireq->ir_loc_addr = daddr;
1506 ireq->rmt_addr = saddr; 1507 ireq->ir_rmt_addr = saddr;
1507 ireq->no_srccheck = inet_sk(sk)->transparent; 1508 ireq->no_srccheck = inet_sk(sk)->transparent;
1508 ireq->opt = tcp_v4_save_options(skb); 1509 ireq->opt = tcp_v4_save_options(skb);
1509 1510
@@ -1578,15 +1579,15 @@ int tcp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
1578 fastopen_cookie_present(&valid_foc) ? &valid_foc : NULL); 1579 fastopen_cookie_present(&valid_foc) ? &valid_foc : NULL);
1579 1580
1580 if (skb_synack) { 1581 if (skb_synack) {
1581 __tcp_v4_send_check(skb_synack, ireq->loc_addr, ireq->rmt_addr); 1582 __tcp_v4_send_check(skb_synack, ireq->ir_loc_addr, ireq->ir_rmt_addr);
1582 skb_set_queue_mapping(skb_synack, skb_get_queue_mapping(skb)); 1583 skb_set_queue_mapping(skb_synack, skb_get_queue_mapping(skb));
1583 } else 1584 } else
1584 goto drop_and_free; 1585 goto drop_and_free;
1585 1586
1586 if (likely(!do_fastopen)) { 1587 if (likely(!do_fastopen)) {
1587 int err; 1588 int err;
1588 err = ip_build_and_send_pkt(skb_synack, sk, ireq->loc_addr, 1589 err = ip_build_and_send_pkt(skb_synack, sk, ireq->ir_loc_addr,
1589 ireq->rmt_addr, ireq->opt); 1590 ireq->ir_rmt_addr, ireq->opt);
1590 err = net_xmit_eval(err); 1591 err = net_xmit_eval(err);
1591 if (err || want_cookie) 1592 if (err || want_cookie)
1592 goto drop_and_free; 1593 goto drop_and_free;
@@ -1644,9 +1645,9 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1644 newtp = tcp_sk(newsk); 1645 newtp = tcp_sk(newsk);
1645 newinet = inet_sk(newsk); 1646 newinet = inet_sk(newsk);
1646 ireq = inet_rsk(req); 1647 ireq = inet_rsk(req);
1647 newinet->inet_daddr = ireq->rmt_addr; 1648 newinet->inet_daddr = ireq->ir_rmt_addr;
1648 newinet->inet_rcv_saddr = ireq->loc_addr; 1649 newinet->inet_rcv_saddr = ireq->ir_loc_addr;
1649 newinet->inet_saddr = ireq->loc_addr; 1650 newinet->inet_saddr = ireq->ir_loc_addr;
1650 inet_opt = ireq->opt; 1651 inet_opt = ireq->opt;
1651 rcu_assign_pointer(newinet->inet_opt, inet_opt); 1652 rcu_assign_pointer(newinet->inet_opt, inet_opt);
1652 ireq->opt = NULL; 1653 ireq->opt = NULL;
@@ -2194,18 +2195,6 @@ EXPORT_SYMBOL(tcp_v4_destroy_sock);
2194#ifdef CONFIG_PROC_FS 2195#ifdef CONFIG_PROC_FS
2195/* Proc filesystem TCP sock list dumping. */ 2196/* Proc filesystem TCP sock list dumping. */
2196 2197
2197static inline struct inet_timewait_sock *tw_head(struct hlist_nulls_head *head)
2198{
2199 return hlist_nulls_empty(head) ? NULL :
2200 list_entry(head->first, struct inet_timewait_sock, tw_node);
2201}
2202
2203static inline struct inet_timewait_sock *tw_next(struct inet_timewait_sock *tw)
2204{
2205 return !is_a_nulls(tw->tw_node.next) ?
2206 hlist_nulls_entry(tw->tw_node.next, typeof(*tw), tw_node) : NULL;
2207}
2208
2209/* 2198/*
2210 * Get next listener socket follow cur. If cur is NULL, get first socket 2199 * Get next listener socket follow cur. If cur is NULL, get first socket
2211 * starting from bucket given in st->bucket; when st->bucket is zero the 2200 * starting from bucket given in st->bucket; when st->bucket is zero the
@@ -2309,10 +2298,9 @@ static void *listening_get_idx(struct seq_file *seq, loff_t *pos)
2309 return rc; 2298 return rc;
2310} 2299}
2311 2300
2312static inline bool empty_bucket(struct tcp_iter_state *st) 2301static inline bool empty_bucket(const struct tcp_iter_state *st)
2313{ 2302{
2314 return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain) && 2303 return hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].chain);
2315 hlist_nulls_empty(&tcp_hashinfo.ehash[st->bucket].twchain);
2316} 2304}
2317 2305
2318/* 2306/*
@@ -2329,7 +2317,6 @@ static void *established_get_first(struct seq_file *seq)
2329 for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) { 2317 for (; st->bucket <= tcp_hashinfo.ehash_mask; ++st->bucket) {
2330 struct sock *sk; 2318 struct sock *sk;
2331 struct hlist_nulls_node *node; 2319 struct hlist_nulls_node *node;
2332 struct inet_timewait_sock *tw;
2333 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket); 2320 spinlock_t *lock = inet_ehash_lockp(&tcp_hashinfo, st->bucket);
2334 2321
2335 /* Lockless fast path for the common case of empty buckets */ 2322 /* Lockless fast path for the common case of empty buckets */
@@ -2345,18 +2332,7 @@ static void *established_get_first(struct seq_file *seq)
2345 rc = sk; 2332 rc = sk;
2346 goto out; 2333 goto out;
2347 } 2334 }
2348 st->state = TCP_SEQ_STATE_TIME_WAIT;
2349 inet_twsk_for_each(tw, node,
2350 &tcp_hashinfo.ehash[st->bucket].twchain) {
2351 if (tw->tw_family != st->family ||
2352 !net_eq(twsk_net(tw), net)) {
2353 continue;
2354 }
2355 rc = tw;
2356 goto out;
2357 }
2358 spin_unlock_bh(lock); 2335 spin_unlock_bh(lock);
2359 st->state = TCP_SEQ_STATE_ESTABLISHED;
2360 } 2336 }
2361out: 2337out:
2362 return rc; 2338 return rc;
@@ -2365,7 +2341,6 @@ out:
2365static void *established_get_next(struct seq_file *seq, void *cur) 2341static void *established_get_next(struct seq_file *seq, void *cur)
2366{ 2342{
2367 struct sock *sk = cur; 2343 struct sock *sk = cur;
2368 struct inet_timewait_sock *tw;
2369 struct hlist_nulls_node *node; 2344 struct hlist_nulls_node *node;
2370 struct tcp_iter_state *st = seq->private; 2345 struct tcp_iter_state *st = seq->private;
2371 struct net *net = seq_file_net(seq); 2346 struct net *net = seq_file_net(seq);
@@ -2373,45 +2348,16 @@ static void *established_get_next(struct seq_file *seq, void *cur)
2373 ++st->num; 2348 ++st->num;
2374 ++st->offset; 2349 ++st->offset;
2375 2350
2376 if (st->state == TCP_SEQ_STATE_TIME_WAIT) { 2351 sk = sk_nulls_next(sk);
2377 tw = cur;
2378 tw = tw_next(tw);
2379get_tw:
2380 while (tw && (tw->tw_family != st->family || !net_eq(twsk_net(tw), net))) {
2381 tw = tw_next(tw);
2382 }
2383 if (tw) {
2384 cur = tw;
2385 goto out;
2386 }
2387 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
2388 st->state = TCP_SEQ_STATE_ESTABLISHED;
2389
2390 /* Look for next non empty bucket */
2391 st->offset = 0;
2392 while (++st->bucket <= tcp_hashinfo.ehash_mask &&
2393 empty_bucket(st))
2394 ;
2395 if (st->bucket > tcp_hashinfo.ehash_mask)
2396 return NULL;
2397
2398 spin_lock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
2399 sk = sk_nulls_head(&tcp_hashinfo.ehash[st->bucket].chain);
2400 } else
2401 sk = sk_nulls_next(sk);
2402 2352
2403 sk_nulls_for_each_from(sk, node) { 2353 sk_nulls_for_each_from(sk, node) {
2404 if (sk->sk_family == st->family && net_eq(sock_net(sk), net)) 2354 if (sk->sk_family == st->family && net_eq(sock_net(sk), net))
2405 goto found; 2355 return sk;
2406 } 2356 }
2407 2357
2408 st->state = TCP_SEQ_STATE_TIME_WAIT; 2358 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
2409 tw = tw_head(&tcp_hashinfo.ehash[st->bucket].twchain); 2359 ++st->bucket;
2410 goto get_tw; 2360 return established_get_first(seq);
2411found:
2412 cur = sk;
2413out:
2414 return cur;
2415} 2361}
2416 2362
2417static void *established_get_idx(struct seq_file *seq, loff_t pos) 2363static void *established_get_idx(struct seq_file *seq, loff_t pos)
@@ -2464,10 +2410,9 @@ static void *tcp_seek_last_pos(struct seq_file *seq)
2464 if (rc) 2410 if (rc)
2465 break; 2411 break;
2466 st->bucket = 0; 2412 st->bucket = 0;
2413 st->state = TCP_SEQ_STATE_ESTABLISHED;
2467 /* Fallthrough */ 2414 /* Fallthrough */
2468 case TCP_SEQ_STATE_ESTABLISHED: 2415 case TCP_SEQ_STATE_ESTABLISHED:
2469 case TCP_SEQ_STATE_TIME_WAIT:
2470 st->state = TCP_SEQ_STATE_ESTABLISHED;
2471 if (st->bucket > tcp_hashinfo.ehash_mask) 2416 if (st->bucket > tcp_hashinfo.ehash_mask)
2472 break; 2417 break;
2473 rc = established_get_first(seq); 2418 rc = established_get_first(seq);
@@ -2524,7 +2469,6 @@ static void *tcp_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2524 } 2469 }
2525 break; 2470 break;
2526 case TCP_SEQ_STATE_ESTABLISHED: 2471 case TCP_SEQ_STATE_ESTABLISHED:
2527 case TCP_SEQ_STATE_TIME_WAIT:
2528 rc = established_get_next(seq, v); 2472 rc = established_get_next(seq, v);
2529 break; 2473 break;
2530 } 2474 }
@@ -2548,7 +2492,6 @@ static void tcp_seq_stop(struct seq_file *seq, void *v)
2548 if (v != SEQ_START_TOKEN) 2492 if (v != SEQ_START_TOKEN)
2549 spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock); 2493 spin_unlock_bh(&tcp_hashinfo.listening_hash[st->bucket].lock);
2550 break; 2494 break;
2551 case TCP_SEQ_STATE_TIME_WAIT:
2552 case TCP_SEQ_STATE_ESTABLISHED: 2495 case TCP_SEQ_STATE_ESTABLISHED:
2553 if (v) 2496 if (v)
2554 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket)); 2497 spin_unlock_bh(inet_ehash_lockp(&tcp_hashinfo, st->bucket));
@@ -2598,18 +2541,18 @@ void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
2598EXPORT_SYMBOL(tcp_proc_unregister); 2541EXPORT_SYMBOL(tcp_proc_unregister);
2599 2542
2600static void get_openreq4(const struct sock *sk, const struct request_sock *req, 2543static void get_openreq4(const struct sock *sk, const struct request_sock *req,
2601 struct seq_file *f, int i, kuid_t uid, int *len) 2544 struct seq_file *f, int i, kuid_t uid)
2602{ 2545{
2603 const struct inet_request_sock *ireq = inet_rsk(req); 2546 const struct inet_request_sock *ireq = inet_rsk(req);
2604 long delta = req->expires - jiffies; 2547 long delta = req->expires - jiffies;
2605 2548
2606 seq_printf(f, "%4d: %08X:%04X %08X:%04X" 2549 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2607 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK%n", 2550 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
2608 i, 2551 i,
2609 ireq->loc_addr, 2552 ireq->ir_loc_addr,
2610 ntohs(inet_sk(sk)->inet_sport), 2553 ntohs(inet_sk(sk)->inet_sport),
2611 ireq->rmt_addr, 2554 ireq->ir_rmt_addr,
2612 ntohs(ireq->rmt_port), 2555 ntohs(ireq->ir_rmt_port),
2613 TCP_SYN_RECV, 2556 TCP_SYN_RECV,
2614 0, 0, /* could print option size, but that is af dependent. */ 2557 0, 0, /* could print option size, but that is af dependent. */
2615 1, /* timers active (only the expire timer) */ 2558 1, /* timers active (only the expire timer) */
@@ -2619,11 +2562,10 @@ static void get_openreq4(const struct sock *sk, const struct request_sock *req,
2619 0, /* non standard timer */ 2562 0, /* non standard timer */
2620 0, /* open_requests have no inode */ 2563 0, /* open_requests have no inode */
2621 atomic_read(&sk->sk_refcnt), 2564 atomic_read(&sk->sk_refcnt),
2622 req, 2565 req);
2623 len);
2624} 2566}
2625 2567
2626static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len) 2568static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
2627{ 2569{
2628 int timer_active; 2570 int timer_active;
2629 unsigned long timer_expires; 2571 unsigned long timer_expires;
@@ -2662,7 +2604,7 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
2662 rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0); 2604 rx_queue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
2663 2605
2664 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX " 2606 seq_printf(f, "%4d: %08X:%04X %08X:%04X %02X %08X:%08X %02X:%08lX "
2665 "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d%n", 2607 "%08X %5u %8d %lu %d %pK %lu %lu %u %u %d",
2666 i, src, srcp, dest, destp, sk->sk_state, 2608 i, src, srcp, dest, destp, sk->sk_state,
2667 tp->write_seq - tp->snd_una, 2609 tp->write_seq - tp->snd_una,
2668 rx_queue, 2610 rx_queue,
@@ -2679,12 +2621,11 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i, int *len)
2679 tp->snd_cwnd, 2621 tp->snd_cwnd,
2680 sk->sk_state == TCP_LISTEN ? 2622 sk->sk_state == TCP_LISTEN ?
2681 (fastopenq ? fastopenq->max_qlen : 0) : 2623 (fastopenq ? fastopenq->max_qlen : 0) :
2682 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh), 2624 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh));
2683 len);
2684} 2625}
2685 2626
2686static void get_timewait4_sock(const struct inet_timewait_sock *tw, 2627static void get_timewait4_sock(const struct inet_timewait_sock *tw,
2687 struct seq_file *f, int i, int *len) 2628 struct seq_file *f, int i)
2688{ 2629{
2689 __be32 dest, src; 2630 __be32 dest, src;
2690 __u16 destp, srcp; 2631 __u16 destp, srcp;
@@ -2696,10 +2637,10 @@ static void get_timewait4_sock(const struct inet_timewait_sock *tw,
2696 srcp = ntohs(tw->tw_sport); 2637 srcp = ntohs(tw->tw_sport);
2697 2638
2698 seq_printf(f, "%4d: %08X:%04X %08X:%04X" 2639 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2699 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK%n", 2640 " %02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK",
2700 i, src, srcp, dest, destp, tw->tw_substate, 0, 0, 2641 i, src, srcp, dest, destp, tw->tw_substate, 0, 0,
2701 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0, 2642 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
2702 atomic_read(&tw->tw_refcnt), tw, len); 2643 atomic_read(&tw->tw_refcnt), tw);
2703} 2644}
2704 2645
2705#define TMPSZ 150 2646#define TMPSZ 150
@@ -2707,11 +2648,11 @@ static void get_timewait4_sock(const struct inet_timewait_sock *tw,
2707static int tcp4_seq_show(struct seq_file *seq, void *v) 2648static int tcp4_seq_show(struct seq_file *seq, void *v)
2708{ 2649{
2709 struct tcp_iter_state *st; 2650 struct tcp_iter_state *st;
2710 int len; 2651 struct sock *sk = v;
2711 2652
2653 seq_setwidth(seq, TMPSZ - 1);
2712 if (v == SEQ_START_TOKEN) { 2654 if (v == SEQ_START_TOKEN) {
2713 seq_printf(seq, "%-*s\n", TMPSZ - 1, 2655 seq_puts(seq, " sl local_address rem_address st tx_queue "
2714 " sl local_address rem_address st tx_queue "
2715 "rx_queue tr tm->when retrnsmt uid timeout " 2656 "rx_queue tr tm->when retrnsmt uid timeout "
2716 "inode"); 2657 "inode");
2717 goto out; 2658 goto out;
@@ -2721,17 +2662,17 @@ static int tcp4_seq_show(struct seq_file *seq, void *v)
2721 switch (st->state) { 2662 switch (st->state) {
2722 case TCP_SEQ_STATE_LISTENING: 2663 case TCP_SEQ_STATE_LISTENING:
2723 case TCP_SEQ_STATE_ESTABLISHED: 2664 case TCP_SEQ_STATE_ESTABLISHED:
2724 get_tcp4_sock(v, seq, st->num, &len); 2665 if (sk->sk_state == TCP_TIME_WAIT)
2666 get_timewait4_sock(v, seq, st->num);
2667 else
2668 get_tcp4_sock(v, seq, st->num);
2725 break; 2669 break;
2726 case TCP_SEQ_STATE_OPENREQ: 2670 case TCP_SEQ_STATE_OPENREQ:
2727 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len); 2671 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid);
2728 break;
2729 case TCP_SEQ_STATE_TIME_WAIT:
2730 get_timewait4_sock(v, seq, st->num, &len);
2731 break; 2672 break;
2732 } 2673 }
2733 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
2734out: 2674out:
2675 seq_pad(seq, '\n');
2735 return 0; 2676 return 0;
2736} 2677}
2737 2678
@@ -2806,6 +2747,7 @@ struct proto tcp_prot = {
2806 .orphan_count = &tcp_orphan_count, 2747 .orphan_count = &tcp_orphan_count,
2807 .memory_allocated = &tcp_memory_allocated, 2748 .memory_allocated = &tcp_memory_allocated,
2808 .memory_pressure = &tcp_memory_pressure, 2749 .memory_pressure = &tcp_memory_pressure,
2750 .sysctl_mem = sysctl_tcp_mem,
2809 .sysctl_wmem = sysctl_tcp_wmem, 2751 .sysctl_wmem = sysctl_tcp_wmem,
2810 .sysctl_rmem = sysctl_tcp_rmem, 2752 .sysctl_rmem = sysctl_tcp_rmem,
2811 .max_header = MAX_TCP_HEADER, 2753 .max_header = MAX_TCP_HEADER,
diff --git a/net/ipv4/tcp_lp.c b/net/ipv4/tcp_lp.c
index 72f7218b03f5..991d62a2f9bb 100644
--- a/net/ipv4/tcp_lp.c
+++ b/net/ipv4/tcp_lp.c
@@ -115,12 +115,13 @@ static void tcp_lp_init(struct sock *sk)
115 * Will only call newReno CA when away from inference. 115 * Will only call newReno CA when away from inference.
116 * From TCP-LP's paper, this will be handled in additive increasement. 116 * From TCP-LP's paper, this will be handled in additive increasement.
117 */ 117 */
118static void tcp_lp_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 118static void tcp_lp_cong_avoid(struct sock *sk, u32 ack, u32 acked,
119 u32 in_flight)
119{ 120{
120 struct lp *lp = inet_csk_ca(sk); 121 struct lp *lp = inet_csk_ca(sk);
121 122
122 if (!(lp->flag & LP_WITHIN_INF)) 123 if (!(lp->flag & LP_WITHIN_INF))
123 tcp_reno_cong_avoid(sk, ack, in_flight); 124 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
124} 125}
125 126
126/** 127/**
diff --git a/net/ipv4/tcp_memcontrol.c b/net/ipv4/tcp_memcontrol.c
index 559d4ae6ebf4..f7e522c558ba 100644
--- a/net/ipv4/tcp_memcontrol.c
+++ b/net/ipv4/tcp_memcontrol.c
@@ -6,18 +6,6 @@
6#include <linux/memcontrol.h> 6#include <linux/memcontrol.h>
7#include <linux/module.h> 7#include <linux/module.h>
8 8
9static inline struct tcp_memcontrol *tcp_from_cgproto(struct cg_proto *cg_proto)
10{
11 return container_of(cg_proto, struct tcp_memcontrol, cg_proto);
12}
13
14static void memcg_tcp_enter_memory_pressure(struct sock *sk)
15{
16 if (sk->sk_cgrp->memory_pressure)
17 *sk->sk_cgrp->memory_pressure = 1;
18}
19EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
20
21int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss) 9int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
22{ 10{
23 /* 11 /*
@@ -27,34 +15,24 @@ int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
27 */ 15 */
28 struct res_counter *res_parent = NULL; 16 struct res_counter *res_parent = NULL;
29 struct cg_proto *cg_proto, *parent_cg; 17 struct cg_proto *cg_proto, *parent_cg;
30 struct tcp_memcontrol *tcp;
31 struct mem_cgroup *parent = parent_mem_cgroup(memcg); 18 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
32 struct net *net = current->nsproxy->net_ns;
33 19
34 cg_proto = tcp_prot.proto_cgroup(memcg); 20 cg_proto = tcp_prot.proto_cgroup(memcg);
35 if (!cg_proto) 21 if (!cg_proto)
36 return 0; 22 return 0;
37 23
38 tcp = tcp_from_cgproto(cg_proto); 24 cg_proto->sysctl_mem[0] = sysctl_tcp_mem[0];
39 25 cg_proto->sysctl_mem[1] = sysctl_tcp_mem[1];
40 tcp->tcp_prot_mem[0] = net->ipv4.sysctl_tcp_mem[0]; 26 cg_proto->sysctl_mem[2] = sysctl_tcp_mem[2];
41 tcp->tcp_prot_mem[1] = net->ipv4.sysctl_tcp_mem[1]; 27 cg_proto->memory_pressure = 0;
42 tcp->tcp_prot_mem[2] = net->ipv4.sysctl_tcp_mem[2]; 28 cg_proto->memcg = memcg;
43 tcp->tcp_memory_pressure = 0;
44 29
45 parent_cg = tcp_prot.proto_cgroup(parent); 30 parent_cg = tcp_prot.proto_cgroup(parent);
46 if (parent_cg) 31 if (parent_cg)
47 res_parent = parent_cg->memory_allocated; 32 res_parent = &parent_cg->memory_allocated;
48 33
49 res_counter_init(&tcp->tcp_memory_allocated, res_parent); 34 res_counter_init(&cg_proto->memory_allocated, res_parent);
50 percpu_counter_init(&tcp->tcp_sockets_allocated, 0); 35 percpu_counter_init(&cg_proto->sockets_allocated, 0);
51
52 cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure;
53 cg_proto->memory_pressure = &tcp->tcp_memory_pressure;
54 cg_proto->sysctl_mem = tcp->tcp_prot_mem;
55 cg_proto->memory_allocated = &tcp->tcp_memory_allocated;
56 cg_proto->sockets_allocated = &tcp->tcp_sockets_allocated;
57 cg_proto->memcg = memcg;
58 36
59 return 0; 37 return 0;
60} 38}
@@ -63,23 +41,18 @@ EXPORT_SYMBOL(tcp_init_cgroup);
63void tcp_destroy_cgroup(struct mem_cgroup *memcg) 41void tcp_destroy_cgroup(struct mem_cgroup *memcg)
64{ 42{
65 struct cg_proto *cg_proto; 43 struct cg_proto *cg_proto;
66 struct tcp_memcontrol *tcp;
67 44
68 cg_proto = tcp_prot.proto_cgroup(memcg); 45 cg_proto = tcp_prot.proto_cgroup(memcg);
69 if (!cg_proto) 46 if (!cg_proto)
70 return; 47 return;
71 48
72 tcp = tcp_from_cgproto(cg_proto); 49 percpu_counter_destroy(&cg_proto->sockets_allocated);
73 percpu_counter_destroy(&tcp->tcp_sockets_allocated);
74} 50}
75EXPORT_SYMBOL(tcp_destroy_cgroup); 51EXPORT_SYMBOL(tcp_destroy_cgroup);
76 52
77static int tcp_update_limit(struct mem_cgroup *memcg, u64 val) 53static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
78{ 54{
79 struct net *net = current->nsproxy->net_ns;
80 struct tcp_memcontrol *tcp;
81 struct cg_proto *cg_proto; 55 struct cg_proto *cg_proto;
82 u64 old_lim;
83 int i; 56 int i;
84 int ret; 57 int ret;
85 58
@@ -90,16 +63,13 @@ static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
90 if (val > RES_COUNTER_MAX) 63 if (val > RES_COUNTER_MAX)
91 val = RES_COUNTER_MAX; 64 val = RES_COUNTER_MAX;
92 65
93 tcp = tcp_from_cgproto(cg_proto); 66 ret = res_counter_set_limit(&cg_proto->memory_allocated, val);
94
95 old_lim = res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
96 ret = res_counter_set_limit(&tcp->tcp_memory_allocated, val);
97 if (ret) 67 if (ret)
98 return ret; 68 return ret;
99 69
100 for (i = 0; i < 3; i++) 70 for (i = 0; i < 3; i++)
101 tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT, 71 cg_proto->sysctl_mem[i] = min_t(long, val >> PAGE_SHIFT,
102 net->ipv4.sysctl_tcp_mem[i]); 72 sysctl_tcp_mem[i]);
103 73
104 if (val == RES_COUNTER_MAX) 74 if (val == RES_COUNTER_MAX)
105 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); 75 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
@@ -156,28 +126,24 @@ static int tcp_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
156 126
157static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val) 127static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
158{ 128{
159 struct tcp_memcontrol *tcp;
160 struct cg_proto *cg_proto; 129 struct cg_proto *cg_proto;
161 130
162 cg_proto = tcp_prot.proto_cgroup(memcg); 131 cg_proto = tcp_prot.proto_cgroup(memcg);
163 if (!cg_proto) 132 if (!cg_proto)
164 return default_val; 133 return default_val;
165 134
166 tcp = tcp_from_cgproto(cg_proto); 135 return res_counter_read_u64(&cg_proto->memory_allocated, type);
167 return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
168} 136}
169 137
170static u64 tcp_read_usage(struct mem_cgroup *memcg) 138static u64 tcp_read_usage(struct mem_cgroup *memcg)
171{ 139{
172 struct tcp_memcontrol *tcp;
173 struct cg_proto *cg_proto; 140 struct cg_proto *cg_proto;
174 141
175 cg_proto = tcp_prot.proto_cgroup(memcg); 142 cg_proto = tcp_prot.proto_cgroup(memcg);
176 if (!cg_proto) 143 if (!cg_proto)
177 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT; 144 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
178 145
179 tcp = tcp_from_cgproto(cg_proto); 146 return res_counter_read_u64(&cg_proto->memory_allocated, RES_USAGE);
180 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
181} 147}
182 148
183static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft) 149static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
@@ -205,54 +171,25 @@ static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
205static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event) 171static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
206{ 172{
207 struct mem_cgroup *memcg; 173 struct mem_cgroup *memcg;
208 struct tcp_memcontrol *tcp;
209 struct cg_proto *cg_proto; 174 struct cg_proto *cg_proto;
210 175
211 memcg = mem_cgroup_from_css(css); 176 memcg = mem_cgroup_from_css(css);
212 cg_proto = tcp_prot.proto_cgroup(memcg); 177 cg_proto = tcp_prot.proto_cgroup(memcg);
213 if (!cg_proto) 178 if (!cg_proto)
214 return 0; 179 return 0;
215 tcp = tcp_from_cgproto(cg_proto);
216 180
217 switch (event) { 181 switch (event) {
218 case RES_MAX_USAGE: 182 case RES_MAX_USAGE:
219 res_counter_reset_max(&tcp->tcp_memory_allocated); 183 res_counter_reset_max(&cg_proto->memory_allocated);
220 break; 184 break;
221 case RES_FAILCNT: 185 case RES_FAILCNT:
222 res_counter_reset_failcnt(&tcp->tcp_memory_allocated); 186 res_counter_reset_failcnt(&cg_proto->memory_allocated);
223 break; 187 break;
224 } 188 }
225 189
226 return 0; 190 return 0;
227} 191}
228 192
229unsigned long long tcp_max_memory(const struct mem_cgroup *memcg)
230{
231 struct tcp_memcontrol *tcp;
232 struct cg_proto *cg_proto;
233
234 cg_proto = tcp_prot.proto_cgroup((struct mem_cgroup *)memcg);
235 if (!cg_proto)
236 return 0;
237
238 tcp = tcp_from_cgproto(cg_proto);
239 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_LIMIT);
240}
241
242void tcp_prot_mem(struct mem_cgroup *memcg, long val, int idx)
243{
244 struct tcp_memcontrol *tcp;
245 struct cg_proto *cg_proto;
246
247 cg_proto = tcp_prot.proto_cgroup(memcg);
248 if (!cg_proto)
249 return;
250
251 tcp = tcp_from_cgproto(cg_proto);
252
253 tcp->tcp_prot_mem[idx] = val;
254}
255
256static struct cftype tcp_files[] = { 193static struct cftype tcp_files[] = {
257 { 194 {
258 .name = "kmem.tcp.limit_in_bytes", 195 .name = "kmem.tcp.limit_in_bytes",
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index 52f3c6b971d2..098b3a29f6f3 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -22,6 +22,9 @@
22 22
23int sysctl_tcp_nometrics_save __read_mostly; 23int sysctl_tcp_nometrics_save __read_mostly;
24 24
25static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *addr,
26 struct net *net, unsigned int hash);
27
25struct tcp_fastopen_metrics { 28struct tcp_fastopen_metrics {
26 u16 mss; 29 u16 mss;
27 u16 syn_loss:10; /* Recurring Fast Open SYN losses */ 30 u16 syn_loss:10; /* Recurring Fast Open SYN losses */
@@ -130,16 +133,41 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm, struct dst_entry *dst,
130 } 133 }
131} 134}
132 135
136#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
137
138static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
139{
140 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
141 tcpm_suck_dst(tm, dst, false);
142}
143
144#define TCP_METRICS_RECLAIM_DEPTH 5
145#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
146
133static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst, 147static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
134 struct inetpeer_addr *addr, 148 struct inetpeer_addr *addr,
135 unsigned int hash, 149 unsigned int hash)
136 bool reclaim)
137{ 150{
138 struct tcp_metrics_block *tm; 151 struct tcp_metrics_block *tm;
139 struct net *net; 152 struct net *net;
153 bool reclaim = false;
140 154
141 spin_lock_bh(&tcp_metrics_lock); 155 spin_lock_bh(&tcp_metrics_lock);
142 net = dev_net(dst->dev); 156 net = dev_net(dst->dev);
157
158 /* While waiting for the spin-lock the cache might have been populated
159 * with this entry and so we have to check again.
160 */
161 tm = __tcp_get_metrics(addr, net, hash);
162 if (tm == TCP_METRICS_RECLAIM_PTR) {
163 reclaim = true;
164 tm = NULL;
165 }
166 if (tm) {
167 tcpm_check_stamp(tm, dst);
168 goto out_unlock;
169 }
170
143 if (unlikely(reclaim)) { 171 if (unlikely(reclaim)) {
144 struct tcp_metrics_block *oldest; 172 struct tcp_metrics_block *oldest;
145 173
@@ -169,17 +197,6 @@ out_unlock:
169 return tm; 197 return tm;
170} 198}
171 199
172#define TCP_METRICS_TIMEOUT (60 * 60 * HZ)
173
174static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst)
175{
176 if (tm && unlikely(time_after(jiffies, tm->tcpm_stamp + TCP_METRICS_TIMEOUT)))
177 tcpm_suck_dst(tm, dst, false);
178}
179
180#define TCP_METRICS_RECLAIM_DEPTH 5
181#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
182
183static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth) 200static struct tcp_metrics_block *tcp_get_encode(struct tcp_metrics_block *tm, int depth)
184{ 201{
185 if (tm) 202 if (tm)
@@ -215,13 +232,15 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
215 addr.family = req->rsk_ops->family; 232 addr.family = req->rsk_ops->family;
216 switch (addr.family) { 233 switch (addr.family) {
217 case AF_INET: 234 case AF_INET:
218 addr.addr.a4 = inet_rsk(req)->rmt_addr; 235 addr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
219 hash = (__force unsigned int) addr.addr.a4; 236 hash = (__force unsigned int) addr.addr.a4;
220 break; 237 break;
238#if IS_ENABLED(CONFIG_IPV6)
221 case AF_INET6: 239 case AF_INET6:
222 *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr; 240 *(struct in6_addr *)addr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
223 hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr); 241 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
224 break; 242 break;
243#endif
225 default: 244 default:
226 return NULL; 245 return NULL;
227 } 246 }
@@ -240,7 +259,6 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
240 259
241static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw) 260static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
242{ 261{
243 struct inet6_timewait_sock *tw6;
244 struct tcp_metrics_block *tm; 262 struct tcp_metrics_block *tm;
245 struct inetpeer_addr addr; 263 struct inetpeer_addr addr;
246 unsigned int hash; 264 unsigned int hash;
@@ -252,11 +270,12 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
252 addr.addr.a4 = tw->tw_daddr; 270 addr.addr.a4 = tw->tw_daddr;
253 hash = (__force unsigned int) addr.addr.a4; 271 hash = (__force unsigned int) addr.addr.a4;
254 break; 272 break;
273#if IS_ENABLED(CONFIG_IPV6)
255 case AF_INET6: 274 case AF_INET6:
256 tw6 = inet6_twsk((struct sock *)tw); 275 *(struct in6_addr *)addr.addr.a6 = tw->tw_v6_daddr;
257 *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr; 276 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
258 hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
259 break; 277 break;
278#endif
260 default: 279 default:
261 return NULL; 280 return NULL;
262 } 281 }
@@ -280,7 +299,6 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
280 struct inetpeer_addr addr; 299 struct inetpeer_addr addr;
281 unsigned int hash; 300 unsigned int hash;
282 struct net *net; 301 struct net *net;
283 bool reclaim;
284 302
285 addr.family = sk->sk_family; 303 addr.family = sk->sk_family;
286 switch (addr.family) { 304 switch (addr.family) {
@@ -288,10 +306,12 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
288 addr.addr.a4 = inet_sk(sk)->inet_daddr; 306 addr.addr.a4 = inet_sk(sk)->inet_daddr;
289 hash = (__force unsigned int) addr.addr.a4; 307 hash = (__force unsigned int) addr.addr.a4;
290 break; 308 break;
309#if IS_ENABLED(CONFIG_IPV6)
291 case AF_INET6: 310 case AF_INET6:
292 *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr; 311 *(struct in6_addr *)addr.addr.a6 = sk->sk_v6_daddr;
293 hash = ipv6_addr_hash(&inet6_sk(sk)->daddr); 312 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
294 break; 313 break;
314#endif
295 default: 315 default:
296 return NULL; 316 return NULL;
297 } 317 }
@@ -300,13 +320,10 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
300 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 320 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log);
301 321
302 tm = __tcp_get_metrics(&addr, net, hash); 322 tm = __tcp_get_metrics(&addr, net, hash);
303 reclaim = false; 323 if (tm == TCP_METRICS_RECLAIM_PTR)
304 if (tm == TCP_METRICS_RECLAIM_PTR) {
305 reclaim = true;
306 tm = NULL; 324 tm = NULL;
307 }
308 if (!tm && create) 325 if (!tm && create)
309 tm = tcpm_new(dst, &addr, hash, reclaim); 326 tm = tcpm_new(dst, &addr, hash);
310 else 327 else
311 tcpm_check_stamp(tm, dst); 328 tcpm_check_stamp(tm, dst);
312 329
@@ -659,16 +676,20 @@ void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
659void tcp_fastopen_cache_set(struct sock *sk, u16 mss, 676void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
660 struct tcp_fastopen_cookie *cookie, bool syn_lost) 677 struct tcp_fastopen_cookie *cookie, bool syn_lost)
661{ 678{
679 struct dst_entry *dst = __sk_dst_get(sk);
662 struct tcp_metrics_block *tm; 680 struct tcp_metrics_block *tm;
663 681
682 if (!dst)
683 return;
664 rcu_read_lock(); 684 rcu_read_lock();
665 tm = tcp_get_metrics(sk, __sk_dst_get(sk), true); 685 tm = tcp_get_metrics(sk, dst, true);
666 if (tm) { 686 if (tm) {
667 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; 687 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
668 688
669 write_seqlock_bh(&fastopen_seqlock); 689 write_seqlock_bh(&fastopen_seqlock);
670 tfom->mss = mss; 690 if (mss)
671 if (cookie->len > 0) 691 tfom->mss = mss;
692 if (cookie && cookie->len > 0)
672 tfom->cookie = *cookie; 693 tfom->cookie = *cookie;
673 if (syn_lost) { 694 if (syn_lost) {
674 ++tfom->syn_loss; 695 ++tfom->syn_loss;
@@ -983,7 +1004,7 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
983 return 0; 1004 return 0;
984} 1005}
985 1006
986static struct genl_ops tcp_metrics_nl_ops[] = { 1007static const struct genl_ops tcp_metrics_nl_ops[] = {
987 { 1008 {
988 .cmd = TCP_METRICS_CMD_GET, 1009 .cmd = TCP_METRICS_CMD_GET,
989 .doit = tcp_metrics_nl_cmd_get, 1010 .doit = tcp_metrics_nl_cmd_get,
@@ -1074,8 +1095,7 @@ void __init tcp_metrics_init(void)
1074 if (ret < 0) 1095 if (ret < 0)
1075 goto cleanup; 1096 goto cleanup;
1076 ret = genl_register_family_with_ops(&tcp_metrics_nl_family, 1097 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1077 tcp_metrics_nl_ops, 1098 tcp_metrics_nl_ops);
1078 ARRAY_SIZE(tcp_metrics_nl_ops));
1079 if (ret < 0) 1099 if (ret < 0)
1080 goto cleanup_subsys; 1100 goto cleanup_subsys;
1081 return; 1101 return;
diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
index 58a3e69aef64..97b684159861 100644
--- a/net/ipv4/tcp_minisocks.c
+++ b/net/ipv4/tcp_minisocks.c
@@ -293,12 +293,9 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)
293#if IS_ENABLED(CONFIG_IPV6) 293#if IS_ENABLED(CONFIG_IPV6)
294 if (tw->tw_family == PF_INET6) { 294 if (tw->tw_family == PF_INET6) {
295 struct ipv6_pinfo *np = inet6_sk(sk); 295 struct ipv6_pinfo *np = inet6_sk(sk);
296 struct inet6_timewait_sock *tw6;
297 296
298 tw->tw_ipv6_offset = inet6_tw_offset(sk->sk_prot); 297 tw->tw_v6_daddr = sk->sk_v6_daddr;
299 tw6 = inet6_twsk((struct sock *)tw); 298 tw->tw_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
300 tw6->tw_v6_daddr = np->daddr;
301 tw6->tw_v6_rcv_saddr = np->rcv_saddr;
302 tw->tw_tclass = np->tclass; 299 tw->tw_tclass = np->tclass;
303 tw->tw_ipv6only = np->ipv6only; 300 tw->tw_ipv6only = np->ipv6only;
304 } 301 }
diff --git a/net/ipv4/tcp_offload.c b/net/ipv4/tcp_offload.c
index 3a7525e6c086..05606353c7e7 100644
--- a/net/ipv4/tcp_offload.c
+++ b/net/ipv4/tcp_offload.c
@@ -14,10 +14,11 @@
14#include <net/tcp.h> 14#include <net/tcp.h>
15#include <net/protocol.h> 15#include <net/protocol.h>
16 16
17struct sk_buff *tcp_tso_segment(struct sk_buff *skb, 17struct sk_buff *tcp_gso_segment(struct sk_buff *skb,
18 netdev_features_t features) 18 netdev_features_t features)
19{ 19{
20 struct sk_buff *segs = ERR_PTR(-EINVAL); 20 struct sk_buff *segs = ERR_PTR(-EINVAL);
21 unsigned int sum_truesize = 0;
21 struct tcphdr *th; 22 struct tcphdr *th;
22 unsigned int thlen; 23 unsigned int thlen;
23 unsigned int seq; 24 unsigned int seq;
@@ -56,6 +57,8 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
56 SKB_GSO_TCP_ECN | 57 SKB_GSO_TCP_ECN |
57 SKB_GSO_TCPV6 | 58 SKB_GSO_TCPV6 |
58 SKB_GSO_GRE | 59 SKB_GSO_GRE |
60 SKB_GSO_IPIP |
61 SKB_GSO_SIT |
59 SKB_GSO_MPLS | 62 SKB_GSO_MPLS |
60 SKB_GSO_UDP_TUNNEL | 63 SKB_GSO_UDP_TUNNEL |
61 0) || 64 0) ||
@@ -102,13 +105,7 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
102 if (copy_destructor) { 105 if (copy_destructor) {
103 skb->destructor = gso_skb->destructor; 106 skb->destructor = gso_skb->destructor;
104 skb->sk = gso_skb->sk; 107 skb->sk = gso_skb->sk;
105 /* {tcp|sock}_wfree() use exact truesize accounting : 108 sum_truesize += skb->truesize;
106 * sum(skb->truesize) MUST be exactly be gso_skb->truesize
107 * So we account mss bytes of 'true size' for each segment.
108 * The last segment will contain the remaining.
109 */
110 skb->truesize = mss;
111 gso_skb->truesize -= mss;
112 } 109 }
113 skb = skb->next; 110 skb = skb->next;
114 th = tcp_hdr(skb); 111 th = tcp_hdr(skb);
@@ -125,7 +122,9 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
125 if (copy_destructor) { 122 if (copy_destructor) {
126 swap(gso_skb->sk, skb->sk); 123 swap(gso_skb->sk, skb->sk);
127 swap(gso_skb->destructor, skb->destructor); 124 swap(gso_skb->destructor, skb->destructor);
128 swap(gso_skb->truesize, skb->truesize); 125 sum_truesize += skb->truesize;
126 atomic_add(sum_truesize - gso_skb->truesize,
127 &skb->sk->sk_wmem_alloc);
129 } 128 }
130 129
131 delta = htonl(oldlen + (skb_tail_pointer(skb) - 130 delta = htonl(oldlen + (skb_tail_pointer(skb) -
@@ -139,7 +138,7 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
139out: 138out:
140 return segs; 139 return segs;
141} 140}
142EXPORT_SYMBOL(tcp_tso_segment); 141EXPORT_SYMBOL(tcp_gso_segment);
143 142
144struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb) 143struct sk_buff **tcp_gro_receive(struct sk_buff **head, struct sk_buff *skb)
145{ 144{
@@ -275,33 +274,32 @@ static struct sk_buff **tcp4_gro_receive(struct sk_buff **head, struct sk_buff *
275{ 274{
276 const struct iphdr *iph = skb_gro_network_header(skb); 275 const struct iphdr *iph = skb_gro_network_header(skb);
277 __wsum wsum; 276 __wsum wsum;
278 __sum16 sum; 277
278 /* Don't bother verifying checksum if we're going to flush anyway. */
279 if (NAPI_GRO_CB(skb)->flush)
280 goto skip_csum;
281
282 wsum = skb->csum;
279 283
280 switch (skb->ip_summed) { 284 switch (skb->ip_summed) {
285 case CHECKSUM_NONE:
286 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb),
287 0);
288
289 /* fall through */
290
281 case CHECKSUM_COMPLETE: 291 case CHECKSUM_COMPLETE:
282 if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr, 292 if (!tcp_v4_check(skb_gro_len(skb), iph->saddr, iph->daddr,
283 skb->csum)) { 293 wsum)) {
284 skb->ip_summed = CHECKSUM_UNNECESSARY; 294 skb->ip_summed = CHECKSUM_UNNECESSARY;
285 break; 295 break;
286 } 296 }
287flush: 297
288 NAPI_GRO_CB(skb)->flush = 1; 298 NAPI_GRO_CB(skb)->flush = 1;
289 return NULL; 299 return NULL;
290
291 case CHECKSUM_NONE:
292 wsum = csum_tcpudp_nofold(iph->saddr, iph->daddr,
293 skb_gro_len(skb), IPPROTO_TCP, 0);
294 sum = csum_fold(skb_checksum(skb,
295 skb_gro_offset(skb),
296 skb_gro_len(skb),
297 wsum));
298 if (sum)
299 goto flush;
300
301 skb->ip_summed = CHECKSUM_UNNECESSARY;
302 break;
303 } 300 }
304 301
302skip_csum:
305 return tcp_gro_receive(head, skb); 303 return tcp_gro_receive(head, skb);
306} 304}
307 305
@@ -320,7 +318,7 @@ static int tcp4_gro_complete(struct sk_buff *skb)
320static const struct net_offload tcpv4_offload = { 318static const struct net_offload tcpv4_offload = {
321 .callbacks = { 319 .callbacks = {
322 .gso_send_check = tcp_v4_gso_send_check, 320 .gso_send_check = tcp_v4_gso_send_check,
323 .gso_segment = tcp_tso_segment, 321 .gso_segment = tcp_gso_segment,
324 .gro_receive = tcp4_gro_receive, 322 .gro_receive = tcp4_gro_receive,
325 .gro_complete = tcp4_gro_complete, 323 .gro_complete = tcp4_gro_complete,
326 }, 324 },
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index d46f2143305c..7820f3a7dd70 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -850,15 +850,15 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
850 850
851 BUG_ON(!skb || !tcp_skb_pcount(skb)); 851 BUG_ON(!skb || !tcp_skb_pcount(skb));
852 852
853 /* If congestion control is doing timestamping, we must 853 if (clone_it) {
854 * take such a timestamp before we potentially clone/copy.
855 */
856 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
857 __net_timestamp(skb);
858
859 if (likely(clone_it)) {
860 const struct sk_buff *fclone = skb + 1; 854 const struct sk_buff *fclone = skb + 1;
861 855
856 /* If congestion control is doing timestamping, we must
857 * take such a timestamp before we potentially clone/copy.
858 */
859 if (icsk->icsk_ca_ops->flags & TCP_CONG_RTT_STAMP)
860 __net_timestamp(skb);
861
862 if (unlikely(skb->fclone == SKB_FCLONE_ORIG && 862 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
863 fclone->fclone == SKB_FCLONE_CLONE)) 863 fclone->fclone == SKB_FCLONE_CLONE))
864 NET_INC_STATS_BH(sock_net(sk), 864 NET_INC_STATS_BH(sock_net(sk),
@@ -1875,8 +1875,12 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
1875 * - better RTT estimation and ACK scheduling 1875 * - better RTT estimation and ACK scheduling
1876 * - faster recovery 1876 * - faster recovery
1877 * - high rates 1877 * - high rates
1878 * Alas, some drivers / subsystems require a fair amount
1879 * of queued bytes to ensure line rate.
1880 * One example is wifi aggregation (802.11 AMPDU)
1878 */ 1881 */
1879 limit = max(skb->truesize, sk->sk_pacing_rate >> 10); 1882 limit = max_t(unsigned int, sysctl_tcp_limit_output_bytes,
1883 sk->sk_pacing_rate >> 10);
1880 1884
1881 if (atomic_read(&sk->sk_wmem_alloc) > limit) { 1885 if (atomic_read(&sk->sk_wmem_alloc) > limit) {
1882 set_bit(TSQ_THROTTLED, &tp->tsq_flags); 1886 set_bit(TSQ_THROTTLED, &tp->tsq_flags);
@@ -2353,21 +2357,6 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
2353 2357
2354 tcp_retrans_try_collapse(sk, skb, cur_mss); 2358 tcp_retrans_try_collapse(sk, skb, cur_mss);
2355 2359
2356 /* Some Solaris stacks overoptimize and ignore the FIN on a
2357 * retransmit when old data is attached. So strip it off
2358 * since it is cheap to do so and saves bytes on the network.
2359 */
2360 if (skb->len > 0 &&
2361 (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) &&
2362 tp->snd_una == (TCP_SKB_CB(skb)->end_seq - 1)) {
2363 if (!pskb_trim(skb, 0)) {
2364 /* Reuse, even though it does some unnecessary work */
2365 tcp_init_nondata_skb(skb, TCP_SKB_CB(skb)->end_seq - 1,
2366 TCP_SKB_CB(skb)->tcp_flags);
2367 skb->ip_summed = CHECKSUM_NONE;
2368 }
2369 }
2370
2371 /* Make a copy, if the first transmission SKB clone we made 2360 /* Make a copy, if the first transmission SKB clone we made
2372 * is still in somebody's hands, else make a clone. 2361 * is still in somebody's hands, else make a clone.
2373 */ 2362 */
@@ -2736,8 +2725,8 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2736 th->syn = 1; 2725 th->syn = 1;
2737 th->ack = 1; 2726 th->ack = 1;
2738 TCP_ECN_make_synack(req, th); 2727 TCP_ECN_make_synack(req, th);
2739 th->source = ireq->loc_port; 2728 th->source = htons(ireq->ir_num);
2740 th->dest = ireq->rmt_port; 2729 th->dest = ireq->ir_rmt_port;
2741 /* Setting of flags are superfluous here for callers (and ECE is 2730 /* Setting of flags are superfluous here for callers (and ECE is
2742 * not even correctly set) 2731 * not even correctly set)
2743 */ 2732 */
@@ -3108,7 +3097,6 @@ void tcp_send_window_probe(struct sock *sk)
3108{ 3097{
3109 if (sk->sk_state == TCP_ESTABLISHED) { 3098 if (sk->sk_state == TCP_ESTABLISHED) {
3110 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1; 3099 tcp_sk(sk)->snd_wl1 = tcp_sk(sk)->rcv_nxt - 1;
3111 tcp_sk(sk)->snd_nxt = tcp_sk(sk)->write_seq;
3112 tcp_xmit_probe_skb(sk, 0); 3100 tcp_xmit_probe_skb(sk, 0);
3113 } 3101 }
3114} 3102}
diff --git a/net/ipv4/tcp_probe.c b/net/ipv4/tcp_probe.c
index 611beab38a00..8b97d71e193b 100644
--- a/net/ipv4/tcp_probe.c
+++ b/net/ipv4/tcp_probe.c
@@ -101,22 +101,6 @@ static inline int tcp_probe_avail(void)
101 si4.sin_addr.s_addr = inet->inet_##mem##addr; \ 101 si4.sin_addr.s_addr = inet->inet_##mem##addr; \
102 } while (0) \ 102 } while (0) \
103 103
104#if IS_ENABLED(CONFIG_IPV6)
105#define tcp_probe_copy_fl_to_si6(inet, si6, mem) \
106 do { \
107 struct ipv6_pinfo *pi6 = inet->pinet6; \
108 si6.sin6_family = AF_INET6; \
109 si6.sin6_port = inet->inet_##mem##port; \
110 si6.sin6_addr = pi6->mem##addr; \
111 si6.sin6_flowinfo = 0; /* No need here. */ \
112 si6.sin6_scope_id = 0; /* No need here. */ \
113 } while (0)
114#else
115#define tcp_probe_copy_fl_to_si6(fl, si6, mem) \
116 do { \
117 memset(&si6, 0, sizeof(si6)); \
118 } while (0)
119#endif
120 104
121/* 105/*
122 * Hook inserted to be called before each receive packet. 106 * Hook inserted to be called before each receive packet.
@@ -147,8 +131,17 @@ static void jtcp_rcv_established(struct sock *sk, struct sk_buff *skb,
147 tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d); 131 tcp_probe_copy_fl_to_si4(inet, p->dst.v4, d);
148 break; 132 break;
149 case AF_INET6: 133 case AF_INET6:
150 tcp_probe_copy_fl_to_si6(inet, p->src.v6, s); 134 memset(&p->src.v6, 0, sizeof(p->src.v6));
151 tcp_probe_copy_fl_to_si6(inet, p->dst.v6, d); 135 memset(&p->dst.v6, 0, sizeof(p->dst.v6));
136#if IS_ENABLED(CONFIG_IPV6)
137 p->src.v6.sin6_family = AF_INET6;
138 p->src.v6.sin6_port = inet->inet_sport;
139 p->src.v6.sin6_addr = inet6_sk(sk)->saddr;
140
141 p->dst.v6.sin6_family = AF_INET6;
142 p->dst.v6.sin6_port = inet->inet_dport;
143 p->dst.v6.sin6_addr = sk->sk_v6_daddr;
144#endif
152 break; 145 break;
153 default: 146 default:
154 BUG(); 147 BUG();
diff --git a/net/ipv4/tcp_scalable.c b/net/ipv4/tcp_scalable.c
index 8ce55b8aaec8..19ea6c2951f3 100644
--- a/net/ipv4/tcp_scalable.c
+++ b/net/ipv4/tcp_scalable.c
@@ -15,7 +15,8 @@
15#define TCP_SCALABLE_AI_CNT 50U 15#define TCP_SCALABLE_AI_CNT 50U
16#define TCP_SCALABLE_MD_SCALE 3 16#define TCP_SCALABLE_MD_SCALE 3
17 17
18static void tcp_scalable_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 18static void tcp_scalable_cong_avoid(struct sock *sk, u32 ack, u32 acked,
19 u32 in_flight)
19{ 20{
20 struct tcp_sock *tp = tcp_sk(sk); 21 struct tcp_sock *tp = tcp_sk(sk);
21 22
@@ -23,7 +24,7 @@ static void tcp_scalable_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
23 return; 24 return;
24 25
25 if (tp->snd_cwnd <= tp->snd_ssthresh) 26 if (tp->snd_cwnd <= tp->snd_ssthresh)
26 tcp_slow_start(tp); 27 tcp_slow_start(tp, acked);
27 else 28 else
28 tcp_cong_avoid_ai(tp, min(tp->snd_cwnd, TCP_SCALABLE_AI_CNT)); 29 tcp_cong_avoid_ai(tp, min(tp->snd_cwnd, TCP_SCALABLE_AI_CNT));
29} 30}
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index 4b85e6f636c9..64f0354c84c7 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -156,12 +156,16 @@ static bool retransmits_timed_out(struct sock *sk,
156static int tcp_write_timeout(struct sock *sk) 156static int tcp_write_timeout(struct sock *sk)
157{ 157{
158 struct inet_connection_sock *icsk = inet_csk(sk); 158 struct inet_connection_sock *icsk = inet_csk(sk);
159 struct tcp_sock *tp = tcp_sk(sk);
159 int retry_until; 160 int retry_until;
160 bool do_reset, syn_set = false; 161 bool do_reset, syn_set = false;
161 162
162 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) { 163 if ((1 << sk->sk_state) & (TCPF_SYN_SENT | TCPF_SYN_RECV)) {
163 if (icsk->icsk_retransmits) 164 if (icsk->icsk_retransmits) {
164 dst_negative_advice(sk); 165 dst_negative_advice(sk);
166 if (tp->syn_fastopen || tp->syn_data)
167 tcp_fastopen_cache_set(sk, 0, NULL, true);
168 }
165 retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries; 169 retry_until = icsk->icsk_syn_retries ? : sysctl_tcp_syn_retries;
166 syn_set = true; 170 syn_set = true;
167 } else { 171 } else {
@@ -374,9 +378,8 @@ void tcp_retransmit_timer(struct sock *sk)
374 } 378 }
375#if IS_ENABLED(CONFIG_IPV6) 379#if IS_ENABLED(CONFIG_IPV6)
376 else if (sk->sk_family == AF_INET6) { 380 else if (sk->sk_family == AF_INET6) {
377 struct ipv6_pinfo *np = inet6_sk(sk);
378 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"), 381 LIMIT_NETDEBUG(KERN_DEBUG pr_fmt("Peer %pI6:%u/%u unexpectedly shrunk window %u:%u (repaired)\n"),
379 &np->daddr, 382 &sk->sk_v6_daddr,
380 ntohs(inet->inet_dport), inet->inet_num, 383 ntohs(inet->inet_dport), inet->inet_num,
381 tp->snd_una, tp->snd_nxt); 384 tp->snd_una, tp->snd_nxt);
382 } 385 }
diff --git a/net/ipv4/tcp_vegas.c b/net/ipv4/tcp_vegas.c
index 80fa2bfd7ede..06cae62bf208 100644
--- a/net/ipv4/tcp_vegas.c
+++ b/net/ipv4/tcp_vegas.c
@@ -163,13 +163,14 @@ static inline u32 tcp_vegas_ssthresh(struct tcp_sock *tp)
163 return min(tp->snd_ssthresh, tp->snd_cwnd-1); 163 return min(tp->snd_ssthresh, tp->snd_cwnd-1);
164} 164}
165 165
166static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 166static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked,
167 u32 in_flight)
167{ 168{
168 struct tcp_sock *tp = tcp_sk(sk); 169 struct tcp_sock *tp = tcp_sk(sk);
169 struct vegas *vegas = inet_csk_ca(sk); 170 struct vegas *vegas = inet_csk_ca(sk);
170 171
171 if (!vegas->doing_vegas_now) { 172 if (!vegas->doing_vegas_now) {
172 tcp_reno_cong_avoid(sk, ack, in_flight); 173 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
173 return; 174 return;
174 } 175 }
175 176
@@ -194,7 +195,7 @@ static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
194 /* We don't have enough RTT samples to do the Vegas 195 /* We don't have enough RTT samples to do the Vegas
195 * calculation, so we'll behave like Reno. 196 * calculation, so we'll behave like Reno.
196 */ 197 */
197 tcp_reno_cong_avoid(sk, ack, in_flight); 198 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
198 } else { 199 } else {
199 u32 rtt, diff; 200 u32 rtt, diff;
200 u64 target_cwnd; 201 u64 target_cwnd;
@@ -243,7 +244,7 @@ static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
243 244
244 } else if (tp->snd_cwnd <= tp->snd_ssthresh) { 245 } else if (tp->snd_cwnd <= tp->snd_ssthresh) {
245 /* Slow start. */ 246 /* Slow start. */
246 tcp_slow_start(tp); 247 tcp_slow_start(tp, acked);
247 } else { 248 } else {
248 /* Congestion avoidance. */ 249 /* Congestion avoidance. */
249 250
@@ -283,7 +284,7 @@ static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
283 } 284 }
284 /* Use normal slow start */ 285 /* Use normal slow start */
285 else if (tp->snd_cwnd <= tp->snd_ssthresh) 286 else if (tp->snd_cwnd <= tp->snd_ssthresh)
286 tcp_slow_start(tp); 287 tcp_slow_start(tp, acked);
287 288
288} 289}
289 290
diff --git a/net/ipv4/tcp_vegas.h b/net/ipv4/tcp_vegas.h
index 6c0eea2f8249..0531b99d8637 100644
--- a/net/ipv4/tcp_vegas.h
+++ b/net/ipv4/tcp_vegas.h
@@ -15,10 +15,10 @@ struct vegas {
15 u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */ 15 u32 baseRTT; /* the min of all Vegas RTT measurements seen (in usec) */
16}; 16};
17 17
18extern void tcp_vegas_init(struct sock *sk); 18void tcp_vegas_init(struct sock *sk);
19extern void tcp_vegas_state(struct sock *sk, u8 ca_state); 19void tcp_vegas_state(struct sock *sk, u8 ca_state);
20extern void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us); 20void tcp_vegas_pkts_acked(struct sock *sk, u32 cnt, s32 rtt_us);
21extern void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event); 21void tcp_vegas_cwnd_event(struct sock *sk, enum tcp_ca_event event);
22extern void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb); 22void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb);
23 23
24#endif /* __TCP_VEGAS_H */ 24#endif /* __TCP_VEGAS_H */
diff --git a/net/ipv4/tcp_veno.c b/net/ipv4/tcp_veno.c
index ac43cd747bce..326475a94865 100644
--- a/net/ipv4/tcp_veno.c
+++ b/net/ipv4/tcp_veno.c
@@ -114,13 +114,14 @@ static void tcp_veno_cwnd_event(struct sock *sk, enum tcp_ca_event event)
114 tcp_veno_init(sk); 114 tcp_veno_init(sk);
115} 115}
116 116
117static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 117static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 acked,
118 u32 in_flight)
118{ 119{
119 struct tcp_sock *tp = tcp_sk(sk); 120 struct tcp_sock *tp = tcp_sk(sk);
120 struct veno *veno = inet_csk_ca(sk); 121 struct veno *veno = inet_csk_ca(sk);
121 122
122 if (!veno->doing_veno_now) { 123 if (!veno->doing_veno_now) {
123 tcp_reno_cong_avoid(sk, ack, in_flight); 124 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
124 return; 125 return;
125 } 126 }
126 127
@@ -133,7 +134,7 @@ static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
133 /* We don't have enough rtt samples to do the Veno 134 /* We don't have enough rtt samples to do the Veno
134 * calculation, so we'll behave like Reno. 135 * calculation, so we'll behave like Reno.
135 */ 136 */
136 tcp_reno_cong_avoid(sk, ack, in_flight); 137 tcp_reno_cong_avoid(sk, ack, acked, in_flight);
137 } else { 138 } else {
138 u64 target_cwnd; 139 u64 target_cwnd;
139 u32 rtt; 140 u32 rtt;
@@ -152,7 +153,7 @@ static void tcp_veno_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
152 153
153 if (tp->snd_cwnd <= tp->snd_ssthresh) { 154 if (tp->snd_cwnd <= tp->snd_ssthresh) {
154 /* Slow start. */ 155 /* Slow start. */
155 tcp_slow_start(tp); 156 tcp_slow_start(tp, acked);
156 } else { 157 } else {
157 /* Congestion avoidance. */ 158 /* Congestion avoidance. */
158 if (veno->diff < beta) { 159 if (veno->diff < beta) {
diff --git a/net/ipv4/tcp_yeah.c b/net/ipv4/tcp_yeah.c
index 05c3b6f0e8e1..a347a078ee07 100644
--- a/net/ipv4/tcp_yeah.c
+++ b/net/ipv4/tcp_yeah.c
@@ -69,7 +69,8 @@ static void tcp_yeah_pkts_acked(struct sock *sk, u32 pkts_acked, s32 rtt_us)
69 tcp_vegas_pkts_acked(sk, pkts_acked, rtt_us); 69 tcp_vegas_pkts_acked(sk, pkts_acked, rtt_us);
70} 70}
71 71
72static void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 in_flight) 72static void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 acked,
73 u32 in_flight)
73{ 74{
74 struct tcp_sock *tp = tcp_sk(sk); 75 struct tcp_sock *tp = tcp_sk(sk);
75 struct yeah *yeah = inet_csk_ca(sk); 76 struct yeah *yeah = inet_csk_ca(sk);
@@ -78,7 +79,7 @@ static void tcp_yeah_cong_avoid(struct sock *sk, u32 ack, u32 in_flight)
78 return; 79 return;
79 80
80 if (tp->snd_cwnd <= tp->snd_ssthresh) 81 if (tp->snd_cwnd <= tp->snd_ssthresh)
81 tcp_slow_start(tp); 82 tcp_slow_start(tp, acked);
82 83
83 else if (!yeah->doing_reno_now) { 84 else if (!yeah->doing_reno_now) {
84 /* Scalable */ 85 /* Scalable */
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 0ca44df51ee9..a7e4729e974b 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -103,6 +103,7 @@
103#include <linux/seq_file.h> 103#include <linux/seq_file.h>
104#include <net/net_namespace.h> 104#include <net/net_namespace.h>
105#include <net/icmp.h> 105#include <net/icmp.h>
106#include <net/inet_hashtables.h>
106#include <net/route.h> 107#include <net/route.h>
107#include <net/checksum.h> 108#include <net/checksum.h>
108#include <net/xfrm.h> 109#include <net/xfrm.h>
@@ -219,7 +220,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
219 unsigned short first, last; 220 unsigned short first, last;
220 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN); 221 DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
221 222
222 inet_get_local_port_range(&low, &high); 223 inet_get_local_port_range(net, &low, &high);
223 remaining = (high - low) + 1; 224 remaining = (high - low) + 1;
224 225
225 rand = net_random(); 226 rand = net_random();
@@ -406,6 +407,18 @@ static inline int compute_score2(struct sock *sk, struct net *net,
406 return score; 407 return score;
407} 408}
408 409
410static unsigned int udp_ehashfn(struct net *net, const __be32 laddr,
411 const __u16 lport, const __be32 faddr,
412 const __be16 fport)
413{
414 static u32 udp_ehash_secret __read_mostly;
415
416 net_get_random_once(&udp_ehash_secret, sizeof(udp_ehash_secret));
417
418 return __inet_ehashfn(laddr, lport, faddr, fport,
419 udp_ehash_secret + net_hash_mix(net));
420}
421
409 422
410/* called with read_rcu_lock() */ 423/* called with read_rcu_lock() */
411static struct sock *udp4_lib_lookup2(struct net *net, 424static struct sock *udp4_lib_lookup2(struct net *net,
@@ -429,8 +442,8 @@ begin:
429 badness = score; 442 badness = score;
430 reuseport = sk->sk_reuseport; 443 reuseport = sk->sk_reuseport;
431 if (reuseport) { 444 if (reuseport) {
432 hash = inet_ehashfn(net, daddr, hnum, 445 hash = udp_ehashfn(net, daddr, hnum,
433 saddr, sport); 446 saddr, sport);
434 matches = 1; 447 matches = 1;
435 } 448 }
436 } else if (score == badness && reuseport) { 449 } else if (score == badness && reuseport) {
@@ -510,8 +523,8 @@ begin:
510 badness = score; 523 badness = score;
511 reuseport = sk->sk_reuseport; 524 reuseport = sk->sk_reuseport;
512 if (reuseport) { 525 if (reuseport) {
513 hash = inet_ehashfn(net, daddr, hnum, 526 hash = udp_ehashfn(net, daddr, hnum,
514 saddr, sport); 527 saddr, sport);
515 matches = 1; 528 matches = 1;
516 } 529 }
517 } else if (score == badness && reuseport) { 530 } else if (score == badness && reuseport) {
@@ -547,15 +560,11 @@ static inline struct sock *__udp4_lib_lookup_skb(struct sk_buff *skb,
547 __be16 sport, __be16 dport, 560 __be16 sport, __be16 dport,
548 struct udp_table *udptable) 561 struct udp_table *udptable)
549{ 562{
550 struct sock *sk;
551 const struct iphdr *iph = ip_hdr(skb); 563 const struct iphdr *iph = ip_hdr(skb);
552 564
553 if (unlikely(sk = skb_steal_sock(skb))) 565 return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
554 return sk; 566 iph->daddr, dport, inet_iif(skb),
555 else 567 udptable);
556 return __udp4_lib_lookup(dev_net(skb_dst(skb)->dev), iph->saddr, sport,
557 iph->daddr, dport, inet_iif(skb),
558 udptable);
559} 568}
560 569
561struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport, 570struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
@@ -565,6 +574,26 @@ struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
565} 574}
566EXPORT_SYMBOL_GPL(udp4_lib_lookup); 575EXPORT_SYMBOL_GPL(udp4_lib_lookup);
567 576
577static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
578 __be16 loc_port, __be32 loc_addr,
579 __be16 rmt_port, __be32 rmt_addr,
580 int dif, unsigned short hnum)
581{
582 struct inet_sock *inet = inet_sk(sk);
583
584 if (!net_eq(sock_net(sk), net) ||
585 udp_sk(sk)->udp_port_hash != hnum ||
586 (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
587 (inet->inet_dport != rmt_port && inet->inet_dport) ||
588 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
589 ipv6_only_sock(sk) ||
590 (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
591 return false;
592 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
593 return false;
594 return true;
595}
596
568static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, 597static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
569 __be16 loc_port, __be32 loc_addr, 598 __be16 loc_port, __be32 loc_addr,
570 __be16 rmt_port, __be32 rmt_addr, 599 __be16 rmt_port, __be32 rmt_addr,
@@ -575,20 +604,11 @@ static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
575 unsigned short hnum = ntohs(loc_port); 604 unsigned short hnum = ntohs(loc_port);
576 605
577 sk_nulls_for_each_from(s, node) { 606 sk_nulls_for_each_from(s, node) {
578 struct inet_sock *inet = inet_sk(s); 607 if (__udp_is_mcast_sock(net, s,
579 608 loc_port, loc_addr,
580 if (!net_eq(sock_net(s), net) || 609 rmt_port, rmt_addr,
581 udp_sk(s)->udp_port_hash != hnum || 610 dif, hnum))
582 (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 611 goto found;
583 (inet->inet_dport != rmt_port && inet->inet_dport) ||
584 (inet->inet_rcv_saddr &&
585 inet->inet_rcv_saddr != loc_addr) ||
586 ipv6_only_sock(s) ||
587 (s->sk_bound_dev_if && s->sk_bound_dev_if != dif))
588 continue;
589 if (!ip_mc_sf_allow(s, loc_addr, rmt_addr, dif))
590 continue;
591 goto found;
592 } 612 }
593 s = NULL; 613 s = NULL;
594found: 614found:
@@ -855,6 +875,8 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
855 875
856 ipc.opt = NULL; 876 ipc.opt = NULL;
857 ipc.tx_flags = 0; 877 ipc.tx_flags = 0;
878 ipc.ttl = 0;
879 ipc.tos = -1;
858 880
859 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 881 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
860 882
@@ -938,7 +960,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
938 faddr = ipc.opt->opt.faddr; 960 faddr = ipc.opt->opt.faddr;
939 connected = 0; 961 connected = 0;
940 } 962 }
941 tos = RT_TOS(inet->tos); 963 tos = get_rttos(&ipc, inet);
942 if (sock_flag(sk, SOCK_LOCALROUTE) || 964 if (sock_flag(sk, SOCK_LOCALROUTE) ||
943 (msg->msg_flags & MSG_DONTROUTE) || 965 (msg->msg_flags & MSG_DONTROUTE) ||
944 (ipc.opt && ipc.opt->opt.is_strictroute)) { 966 (ipc.opt && ipc.opt->opt.is_strictroute)) {
@@ -973,7 +995,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
973 err = PTR_ERR(rt); 995 err = PTR_ERR(rt);
974 rt = NULL; 996 rt = NULL;
975 if (err == -ENETUNREACH) 997 if (err == -ENETUNREACH)
976 IP_INC_STATS_BH(net, IPSTATS_MIB_OUTNOROUTES); 998 IP_INC_STATS(net, IPSTATS_MIB_OUTNOROUTES);
977 goto out; 999 goto out;
978 } 1000 }
979 1001
@@ -1072,6 +1094,9 @@ int udp_sendpage(struct sock *sk, struct page *page, int offset,
1072 struct udp_sock *up = udp_sk(sk); 1094 struct udp_sock *up = udp_sk(sk);
1073 int ret; 1095 int ret;
1074 1096
1097 if (flags & MSG_SENDPAGE_NOTLAST)
1098 flags |= MSG_MORE;
1099
1075 if (!up->pending) { 1100 if (!up->pending) {
1076 struct msghdr msg = { .msg_flags = flags|MSG_MORE }; 1101 struct msghdr msg = { .msg_flags = flags|MSG_MORE };
1077 1102
@@ -1209,14 +1234,8 @@ int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1209 int is_udplite = IS_UDPLITE(sk); 1234 int is_udplite = IS_UDPLITE(sk);
1210 bool slow; 1235 bool slow;
1211 1236
1212 /*
1213 * Check any passed addresses
1214 */
1215 if (addr_len)
1216 *addr_len = sizeof(*sin);
1217
1218 if (flags & MSG_ERRQUEUE) 1237 if (flags & MSG_ERRQUEUE)
1219 return ip_recv_error(sk, msg, len); 1238 return ip_recv_error(sk, msg, len, addr_len);
1220 1239
1221try_again: 1240try_again:
1222 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 1241 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
@@ -1276,6 +1295,7 @@ try_again:
1276 sin->sin_port = udp_hdr(skb)->source; 1295 sin->sin_port = udp_hdr(skb)->source;
1277 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 1296 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1278 memset(sin->sin_zero, 0, sizeof(sin->sin_zero)); 1297 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
1298 *addr_len = sizeof(*sin);
1279 } 1299 }
1280 if (inet->cmsg_flags) 1300 if (inet->cmsg_flags)
1281 ip_cmsg_recv(msg, skb); 1301 ip_cmsg_recv(msg, skb);
@@ -1403,8 +1423,10 @@ static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1403{ 1423{
1404 int rc; 1424 int rc;
1405 1425
1406 if (inet_sk(sk)->inet_daddr) 1426 if (inet_sk(sk)->inet_daddr) {
1407 sock_rps_save_rxhash(sk, skb); 1427 sock_rps_save_rxhash(sk, skb);
1428 sk_mark_napi_id(sk, skb);
1429 }
1408 1430
1409 rc = sock_queue_rcv_skb(sk, skb); 1431 rc = sock_queue_rcv_skb(sk, skb);
1410 if (rc < 0) { 1432 if (rc < 0) {
@@ -1528,7 +1550,7 @@ int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1528 1550
1529 rc = 0; 1551 rc = 0;
1530 1552
1531 ipv4_pktinfo_prepare(skb); 1553 ipv4_pktinfo_prepare(sk, skb);
1532 bh_lock_sock(sk); 1554 bh_lock_sock(sk);
1533 if (!sock_owned_by_user(sk)) 1555 if (!sock_owned_by_user(sk))
1534 rc = __udp_queue_rcv_skb(sk, skb); 1556 rc = __udp_queue_rcv_skb(sk, skb);
@@ -1577,6 +1599,18 @@ static void flush_stack(struct sock **stack, unsigned int count,
1577 kfree_skb(skb1); 1599 kfree_skb(skb1);
1578} 1600}
1579 1601
1602/* For TCP sockets, sk_rx_dst is protected by socket lock
1603 * For UDP, we use xchg() to guard against concurrent changes.
1604 */
1605static void udp_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
1606{
1607 struct dst_entry *old;
1608
1609 dst_hold(dst);
1610 old = xchg(&sk->sk_rx_dst, dst);
1611 dst_release(old);
1612}
1613
1580/* 1614/*
1581 * Multicasts and broadcasts go to each listener. 1615 * Multicasts and broadcasts go to each listener.
1582 * 1616 *
@@ -1705,16 +1739,33 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1705 if (udp4_csum_init(skb, uh, proto)) 1739 if (udp4_csum_init(skb, uh, proto))
1706 goto csum_error; 1740 goto csum_error;
1707 1741
1708 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1742 sk = skb_steal_sock(skb);
1709 return __udp4_lib_mcast_deliver(net, skb, uh, 1743 if (sk) {
1710 saddr, daddr, udptable); 1744 struct dst_entry *dst = skb_dst(skb);
1745 int ret;
1746
1747 if (unlikely(sk->sk_rx_dst != dst))
1748 udp_sk_rx_dst_set(sk, dst);
1749
1750 ret = udp_queue_rcv_skb(sk, skb);
1751 sock_put(sk);
1752 /* a return value > 0 means to resubmit the input, but
1753 * it wants the return to be -protocol, or 0
1754 */
1755 if (ret > 0)
1756 return -ret;
1757 return 0;
1758 } else {
1759 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1760 return __udp4_lib_mcast_deliver(net, skb, uh,
1761 saddr, daddr, udptable);
1711 1762
1712 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1763 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1764 }
1713 1765
1714 if (sk != NULL) { 1766 if (sk != NULL) {
1715 int ret; 1767 int ret;
1716 1768
1717 sk_mark_napi_id(sk, skb);
1718 ret = udp_queue_rcv_skb(sk, skb); 1769 ret = udp_queue_rcv_skb(sk, skb);
1719 sock_put(sk); 1770 sock_put(sk);
1720 1771
@@ -1768,6 +1819,138 @@ drop:
1768 return 0; 1819 return 0;
1769} 1820}
1770 1821
1822/* We can only early demux multicast if there is a single matching socket.
1823 * If more than one socket found returns NULL
1824 */
1825static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
1826 __be16 loc_port, __be32 loc_addr,
1827 __be16 rmt_port, __be32 rmt_addr,
1828 int dif)
1829{
1830 struct sock *sk, *result;
1831 struct hlist_nulls_node *node;
1832 unsigned short hnum = ntohs(loc_port);
1833 unsigned int count, slot = udp_hashfn(net, hnum, udp_table.mask);
1834 struct udp_hslot *hslot = &udp_table.hash[slot];
1835
1836 rcu_read_lock();
1837begin:
1838 count = 0;
1839 result = NULL;
1840 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
1841 if (__udp_is_mcast_sock(net, sk,
1842 loc_port, loc_addr,
1843 rmt_port, rmt_addr,
1844 dif, hnum)) {
1845 result = sk;
1846 ++count;
1847 }
1848 }
1849 /*
1850 * if the nulls value we got at the end of this lookup is
1851 * not the expected one, we must restart lookup.
1852 * We probably met an item that was moved to another chain.
1853 */
1854 if (get_nulls_value(node) != slot)
1855 goto begin;
1856
1857 if (result) {
1858 if (count != 1 ||
1859 unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1860 result = NULL;
1861 else if (unlikely(!__udp_is_mcast_sock(net, result,
1862 loc_port, loc_addr,
1863 rmt_port, rmt_addr,
1864 dif, hnum))) {
1865 sock_put(result);
1866 result = NULL;
1867 }
1868 }
1869 rcu_read_unlock();
1870 return result;
1871}
1872
1873/* For unicast we should only early demux connected sockets or we can
1874 * break forwarding setups. The chains here can be long so only check
1875 * if the first socket is an exact match and if not move on.
1876 */
1877static struct sock *__udp4_lib_demux_lookup(struct net *net,
1878 __be16 loc_port, __be32 loc_addr,
1879 __be16 rmt_port, __be32 rmt_addr,
1880 int dif)
1881{
1882 struct sock *sk, *result;
1883 struct hlist_nulls_node *node;
1884 unsigned short hnum = ntohs(loc_port);
1885 unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
1886 unsigned int slot2 = hash2 & udp_table.mask;
1887 struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1888 INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr)
1889 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
1890
1891 rcu_read_lock();
1892 result = NULL;
1893 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
1894 if (INET_MATCH(sk, net, acookie,
1895 rmt_addr, loc_addr, ports, dif))
1896 result = sk;
1897 /* Only check first socket in chain */
1898 break;
1899 }
1900
1901 if (result) {
1902 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1903 result = NULL;
1904 else if (unlikely(!INET_MATCH(sk, net, acookie,
1905 rmt_addr, loc_addr,
1906 ports, dif))) {
1907 sock_put(result);
1908 result = NULL;
1909 }
1910 }
1911 rcu_read_unlock();
1912 return result;
1913}
1914
1915void udp_v4_early_demux(struct sk_buff *skb)
1916{
1917 struct net *net = dev_net(skb->dev);
1918 const struct iphdr *iph;
1919 const struct udphdr *uh;
1920 struct sock *sk;
1921 struct dst_entry *dst;
1922 int dif = skb->dev->ifindex;
1923
1924 /* validate the packet */
1925 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
1926 return;
1927
1928 iph = ip_hdr(skb);
1929 uh = udp_hdr(skb);
1930
1931 if (skb->pkt_type == PACKET_BROADCAST ||
1932 skb->pkt_type == PACKET_MULTICAST)
1933 sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
1934 uh->source, iph->saddr, dif);
1935 else if (skb->pkt_type == PACKET_HOST)
1936 sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
1937 uh->source, iph->saddr, dif);
1938 else
1939 return;
1940
1941 if (!sk)
1942 return;
1943
1944 skb->sk = sk;
1945 skb->destructor = sock_edemux;
1946 dst = sk->sk_rx_dst;
1947
1948 if (dst)
1949 dst = dst_check(dst, 0);
1950 if (dst)
1951 skb_dst_set_noref(skb, dst);
1952}
1953
1771int udp_rcv(struct sk_buff *skb) 1954int udp_rcv(struct sk_buff *skb)
1772{ 1955{
1773 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1956 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
@@ -2150,7 +2333,7 @@ EXPORT_SYMBOL(udp_proc_unregister);
2150 2333
2151/* ------------------------------------------------------------------------ */ 2334/* ------------------------------------------------------------------------ */
2152static void udp4_format_sock(struct sock *sp, struct seq_file *f, 2335static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2153 int bucket, int *len) 2336 int bucket)
2154{ 2337{
2155 struct inet_sock *inet = inet_sk(sp); 2338 struct inet_sock *inet = inet_sk(sp);
2156 __be32 dest = inet->inet_daddr; 2339 __be32 dest = inet->inet_daddr;
@@ -2159,7 +2342,7 @@ static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2159 __u16 srcp = ntohs(inet->inet_sport); 2342 __u16 srcp = ntohs(inet->inet_sport);
2160 2343
2161 seq_printf(f, "%5d: %08X:%04X %08X:%04X" 2344 seq_printf(f, "%5d: %08X:%04X %08X:%04X"
2162 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d%n", 2345 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d",
2163 bucket, src, srcp, dest, destp, sp->sk_state, 2346 bucket, src, srcp, dest, destp, sp->sk_state,
2164 sk_wmem_alloc_get(sp), 2347 sk_wmem_alloc_get(sp),
2165 sk_rmem_alloc_get(sp), 2348 sk_rmem_alloc_get(sp),
@@ -2167,23 +2350,22 @@ static void udp4_format_sock(struct sock *sp, struct seq_file *f,
2167 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)), 2350 from_kuid_munged(seq_user_ns(f), sock_i_uid(sp)),
2168 0, sock_i_ino(sp), 2351 0, sock_i_ino(sp),
2169 atomic_read(&sp->sk_refcnt), sp, 2352 atomic_read(&sp->sk_refcnt), sp,
2170 atomic_read(&sp->sk_drops), len); 2353 atomic_read(&sp->sk_drops));
2171} 2354}
2172 2355
2173int udp4_seq_show(struct seq_file *seq, void *v) 2356int udp4_seq_show(struct seq_file *seq, void *v)
2174{ 2357{
2358 seq_setwidth(seq, 127);
2175 if (v == SEQ_START_TOKEN) 2359 if (v == SEQ_START_TOKEN)
2176 seq_printf(seq, "%-127s\n", 2360 seq_puts(seq, " sl local_address rem_address st tx_queue "
2177 " sl local_address rem_address st tx_queue "
2178 "rx_queue tr tm->when retrnsmt uid timeout " 2361 "rx_queue tr tm->when retrnsmt uid timeout "
2179 "inode ref pointer drops"); 2362 "inode ref pointer drops");
2180 else { 2363 else {
2181 struct udp_iter_state *state = seq->private; 2364 struct udp_iter_state *state = seq->private;
2182 int len;
2183 2365
2184 udp4_format_sock(v, seq, state->bucket, &len); 2366 udp4_format_sock(v, seq, state->bucket);
2185 seq_printf(seq, "%*s\n", 127 - len, "");
2186 } 2367 }
2368 seq_pad(seq, '\n');
2187 return 0; 2369 return 0;
2188} 2370}
2189 2371
@@ -2296,6 +2478,7 @@ struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
2296 netdev_features_t features) 2478 netdev_features_t features)
2297{ 2479{
2298 struct sk_buff *segs = ERR_PTR(-EINVAL); 2480 struct sk_buff *segs = ERR_PTR(-EINVAL);
2481 u16 mac_offset = skb->mac_header;
2299 int mac_len = skb->mac_len; 2482 int mac_len = skb->mac_len;
2300 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb); 2483 int tnl_hlen = skb_inner_mac_header(skb) - skb_transport_header(skb);
2301 __be16 protocol = skb->protocol; 2484 __be16 protocol = skb->protocol;
@@ -2315,8 +2498,11 @@ struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
2315 /* segment inner packet. */ 2498 /* segment inner packet. */
2316 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb); 2499 enc_features = skb->dev->hw_enc_features & netif_skb_features(skb);
2317 segs = skb_mac_gso_segment(skb, enc_features); 2500 segs = skb_mac_gso_segment(skb, enc_features);
2318 if (!segs || IS_ERR(segs)) 2501 if (!segs || IS_ERR(segs)) {
2502 skb_gso_error_unwind(skb, protocol, tnl_hlen, mac_offset,
2503 mac_len);
2319 goto out; 2504 goto out;
2505 }
2320 2506
2321 outer_hlen = skb_tnl_header_len(skb); 2507 outer_hlen = skb_tnl_header_len(skb);
2322 skb = segs; 2508 skb = segs;
diff --git a/net/ipv4/udp_impl.h b/net/ipv4/udp_impl.h
index 5a681e298b90..f3c27899f62b 100644
--- a/net/ipv4/udp_impl.h
+++ b/net/ipv4/udp_impl.h
@@ -5,30 +5,30 @@
5#include <net/protocol.h> 5#include <net/protocol.h>
6#include <net/inet_common.h> 6#include <net/inet_common.h>
7 7
8extern int __udp4_lib_rcv(struct sk_buff *, struct udp_table *, int ); 8int __udp4_lib_rcv(struct sk_buff *, struct udp_table *, int);
9extern void __udp4_lib_err(struct sk_buff *, u32, struct udp_table *); 9void __udp4_lib_err(struct sk_buff *, u32, struct udp_table *);
10 10
11extern int udp_v4_get_port(struct sock *sk, unsigned short snum); 11int udp_v4_get_port(struct sock *sk, unsigned short snum);
12 12
13extern int udp_setsockopt(struct sock *sk, int level, int optname, 13int udp_setsockopt(struct sock *sk, int level, int optname,
14 char __user *optval, unsigned int optlen); 14 char __user *optval, unsigned int optlen);
15extern int udp_getsockopt(struct sock *sk, int level, int optname, 15int udp_getsockopt(struct sock *sk, int level, int optname,
16 char __user *optval, int __user *optlen); 16 char __user *optval, int __user *optlen);
17 17
18#ifdef CONFIG_COMPAT 18#ifdef CONFIG_COMPAT
19extern int compat_udp_setsockopt(struct sock *sk, int level, int optname, 19int compat_udp_setsockopt(struct sock *sk, int level, int optname,
20 char __user *optval, unsigned int optlen); 20 char __user *optval, unsigned int optlen);
21extern int compat_udp_getsockopt(struct sock *sk, int level, int optname, 21int compat_udp_getsockopt(struct sock *sk, int level, int optname,
22 char __user *optval, int __user *optlen); 22 char __user *optval, int __user *optlen);
23#endif 23#endif
24extern int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 24int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
25 size_t len, int noblock, int flags, int *addr_len); 25 size_t len, int noblock, int flags, int *addr_len);
26extern int udp_sendpage(struct sock *sk, struct page *page, int offset, 26int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
27 size_t size, int flags); 27 int flags);
28extern int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 28int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
29extern void udp_destroy_sock(struct sock *sk); 29void udp_destroy_sock(struct sock *sk);
30 30
31#ifdef CONFIG_PROC_FS 31#ifdef CONFIG_PROC_FS
32extern int udp4_seq_show(struct seq_file *seq, void *v); 32int udp4_seq_show(struct seq_file *seq, void *v);
33#endif 33#endif
34#endif /* _UDP4_IMPL_H */ 34#endif /* _UDP4_IMPL_H */
diff --git a/net/ipv4/udp_offload.c b/net/ipv4/udp_offload.c
index f35eccaa855e..79c62bdcd3c5 100644
--- a/net/ipv4/udp_offload.c
+++ b/net/ipv4/udp_offload.c
@@ -41,6 +41,14 @@ static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
41{ 41{
42 struct sk_buff *segs = ERR_PTR(-EINVAL); 42 struct sk_buff *segs = ERR_PTR(-EINVAL);
43 unsigned int mss; 43 unsigned int mss;
44 int offset;
45 __wsum csum;
46
47 if (skb->encapsulation &&
48 skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL) {
49 segs = skb_udp_tunnel_segment(skb, features);
50 goto out;
51 }
44 52
45 mss = skb_shinfo(skb)->gso_size; 53 mss = skb_shinfo(skb)->gso_size;
46 if (unlikely(skb->len <= mss)) 54 if (unlikely(skb->len <= mss))
@@ -52,6 +60,7 @@ static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
52 60
53 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY | 61 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
54 SKB_GSO_UDP_TUNNEL | 62 SKB_GSO_UDP_TUNNEL |
63 SKB_GSO_IPIP |
55 SKB_GSO_GRE | SKB_GSO_MPLS) || 64 SKB_GSO_GRE | SKB_GSO_MPLS) ||
56 !(type & (SKB_GSO_UDP)))) 65 !(type & (SKB_GSO_UDP))))
57 goto out; 66 goto out;
@@ -62,27 +71,20 @@ static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
62 goto out; 71 goto out;
63 } 72 }
64 73
74 /* Do software UFO. Complete and fill in the UDP checksum as
75 * HW cannot do checksum of UDP packets sent as multiple
76 * IP fragments.
77 */
78 offset = skb_checksum_start_offset(skb);
79 csum = skb_checksum(skb, offset, skb->len - offset, 0);
80 offset += skb->csum_offset;
81 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
82 skb->ip_summed = CHECKSUM_NONE;
83
65 /* Fragment the skb. IP headers of the fragments are updated in 84 /* Fragment the skb. IP headers of the fragments are updated in
66 * inet_gso_segment() 85 * inet_gso_segment()
67 */ 86 */
68 if (skb->encapsulation && skb_shinfo(skb)->gso_type & SKB_GSO_UDP_TUNNEL) 87 segs = skb_segment(skb, features);
69 segs = skb_udp_tunnel_segment(skb, features);
70 else {
71 int offset;
72 __wsum csum;
73
74 /* Do software UFO. Complete and fill in the UDP checksum as
75 * HW cannot do checksum of UDP packets sent as multiple
76 * IP fragments.
77 */
78 offset = skb_checksum_start_offset(skb);
79 csum = skb_checksum(skb, offset, skb->len - offset, 0);
80 offset += skb->csum_offset;
81 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
82 skb->ip_summed = CHECKSUM_NONE;
83
84 segs = skb_segment(skb, features);
85 }
86out: 88out:
87 return segs; 89 return segs;
88} 90}
diff --git a/net/ipv4/xfrm4_mode_tunnel.c b/net/ipv4/xfrm4_mode_tunnel.c
index b5663c37f089..31b18152528f 100644
--- a/net/ipv4/xfrm4_mode_tunnel.c
+++ b/net/ipv4/xfrm4_mode_tunnel.c
@@ -16,13 +16,13 @@
16#include <net/xfrm.h> 16#include <net/xfrm.h>
17 17
18/* Informational hook. The decap is still done here. */ 18/* Informational hook. The decap is still done here. */
19static struct xfrm_tunnel __rcu *rcv_notify_handlers __read_mostly; 19static struct xfrm_tunnel_notifier __rcu *rcv_notify_handlers __read_mostly;
20static DEFINE_MUTEX(xfrm4_mode_tunnel_input_mutex); 20static DEFINE_MUTEX(xfrm4_mode_tunnel_input_mutex);
21 21
22int xfrm4_mode_tunnel_input_register(struct xfrm_tunnel *handler) 22int xfrm4_mode_tunnel_input_register(struct xfrm_tunnel_notifier *handler)
23{ 23{
24 struct xfrm_tunnel __rcu **pprev; 24 struct xfrm_tunnel_notifier __rcu **pprev;
25 struct xfrm_tunnel *t; 25 struct xfrm_tunnel_notifier *t;
26 int ret = -EEXIST; 26 int ret = -EEXIST;
27 int priority = handler->priority; 27 int priority = handler->priority;
28 28
@@ -50,10 +50,10 @@ err:
50} 50}
51EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_register); 51EXPORT_SYMBOL_GPL(xfrm4_mode_tunnel_input_register);
52 52
53int xfrm4_mode_tunnel_input_deregister(struct xfrm_tunnel *handler) 53int xfrm4_mode_tunnel_input_deregister(struct xfrm_tunnel_notifier *handler)
54{ 54{
55 struct xfrm_tunnel __rcu **pprev; 55 struct xfrm_tunnel_notifier __rcu **pprev;
56 struct xfrm_tunnel *t; 56 struct xfrm_tunnel_notifier *t;
57 int ret = -ENOENT; 57 int ret = -ENOENT;
58 58
59 mutex_lock(&xfrm4_mode_tunnel_input_mutex); 59 mutex_lock(&xfrm4_mode_tunnel_input_mutex);
@@ -134,7 +134,7 @@ static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
134 134
135static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) 135static int xfrm4_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
136{ 136{
137 struct xfrm_tunnel *handler; 137 struct xfrm_tunnel_notifier *handler;
138 int err = -EINVAL; 138 int err = -EINVAL;
139 139
140 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP) 140 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPIP)
diff --git a/net/ipv4/xfrm4_policy.c b/net/ipv4/xfrm4_policy.c
index ccde54248c8c..e1a63930a967 100644
--- a/net/ipv4/xfrm4_policy.c
+++ b/net/ipv4/xfrm4_policy.c
@@ -104,10 +104,14 @@ _decode_session4(struct sk_buff *skb, struct flowi *fl, int reverse)
104 const struct iphdr *iph = ip_hdr(skb); 104 const struct iphdr *iph = ip_hdr(skb);
105 u8 *xprth = skb_network_header(skb) + iph->ihl * 4; 105 u8 *xprth = skb_network_header(skb) + iph->ihl * 4;
106 struct flowi4 *fl4 = &fl->u.ip4; 106 struct flowi4 *fl4 = &fl->u.ip4;
107 int oif = 0;
108
109 if (skb_dst(skb))
110 oif = skb_dst(skb)->dev->ifindex;
107 111
108 memset(fl4, 0, sizeof(struct flowi4)); 112 memset(fl4, 0, sizeof(struct flowi4));
109 fl4->flowi4_mark = skb->mark; 113 fl4->flowi4_mark = skb->mark;
110 fl4->flowi4_oif = skb_dst(skb)->dev->ifindex; 114 fl4->flowi4_oif = reverse ? skb->skb_iif : oif;
111 115
112 if (!ip_is_fragment(iph)) { 116 if (!ip_is_fragment(iph)) {
113 switch (iph->protocol) { 117 switch (iph->protocol) {
@@ -236,7 +240,7 @@ static struct dst_ops xfrm4_dst_ops = {
236 .destroy = xfrm4_dst_destroy, 240 .destroy = xfrm4_dst_destroy,
237 .ifdown = xfrm4_dst_ifdown, 241 .ifdown = xfrm4_dst_ifdown,
238 .local_out = __ip_local_out, 242 .local_out = __ip_local_out,
239 .gc_thresh = 1024, 243 .gc_thresh = 32768,
240}; 244};
241 245
242static struct xfrm_policy_afinfo xfrm4_policy_afinfo = { 246static struct xfrm_policy_afinfo xfrm4_policy_afinfo = {
diff --git a/net/ipv6/Kconfig b/net/ipv6/Kconfig
index 11b13ea69db4..d92e5586783e 100644
--- a/net/ipv6/Kconfig
+++ b/net/ipv6/Kconfig
@@ -21,24 +21,6 @@ menuconfig IPV6
21 21
22if IPV6 22if IPV6
23 23
24config IPV6_PRIVACY
25 bool "IPv6: Privacy Extensions (RFC 3041) support"
26 ---help---
27 Privacy Extensions for Stateless Address Autoconfiguration in IPv6
28 support. With this option, additional periodically-altered
29 pseudo-random global-scope unicast address(es) will be assigned to
30 your interface(s).
31
32 We use our standard pseudo-random algorithm to generate the
33 randomized interface identifier, instead of one described in RFC 3041.
34
35 By default the kernel does not generate temporary addresses.
36 To use temporary addresses, do
37
38 echo 2 >/proc/sys/net/ipv6/conf/all/use_tempaddr
39
40 See <file:Documentation/networking/ip-sysctl.txt> for details.
41
42config IPV6_ROUTER_PREF 24config IPV6_ROUTER_PREF
43 bool "IPv6: Router Preference (RFC 4191) support" 25 bool "IPv6: Router Preference (RFC 4191) support"
44 ---help--- 26 ---help---
@@ -153,6 +135,17 @@ config INET6_XFRM_MODE_ROUTEOPTIMIZATION
153 ---help--- 135 ---help---
154 Support for MIPv6 route optimization mode. 136 Support for MIPv6 route optimization mode.
155 137
138config IPV6_VTI
139tristate "Virtual (secure) IPv6: tunneling"
140 select IPV6_TUNNEL
141 depends on INET6_XFRM_MODE_TUNNEL
142 ---help---
143 Tunneling means encapsulating data of one protocol type within
144 another protocol and sending it over a channel that understands the
145 encapsulating protocol. This can be used with xfrm mode tunnel to give
146 the notion of a secure tunnel for IPSEC and then use routing protocol
147 on top.
148
156config IPV6_SIT 149config IPV6_SIT
157 tristate "IPv6: IPv6-in-IPv4 tunnel (SIT driver)" 150 tristate "IPv6: IPv6-in-IPv4 tunnel (SIT driver)"
158 select INET_TUNNEL 151 select INET_TUNNEL
diff --git a/net/ipv6/Makefile b/net/ipv6/Makefile
index 470a9c008e9b..17bb830872db 100644
--- a/net/ipv6/Makefile
+++ b/net/ipv6/Makefile
@@ -36,6 +36,7 @@ obj-$(CONFIG_INET6_XFRM_MODE_BEET) += xfrm6_mode_beet.o
36obj-$(CONFIG_IPV6_MIP6) += mip6.o 36obj-$(CONFIG_IPV6_MIP6) += mip6.o
37obj-$(CONFIG_NETFILTER) += netfilter/ 37obj-$(CONFIG_NETFILTER) += netfilter/
38 38
39obj-$(CONFIG_IPV6_VTI) += ip6_vti.o
39obj-$(CONFIG_IPV6_SIT) += sit.o 40obj-$(CONFIG_IPV6_SIT) += sit.o
40obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel.o 41obj-$(CONFIG_IPV6_TUNNEL) += ip6_tunnel.o
41obj-$(CONFIG_IPV6_GRE) += ip6_gre.o 42obj-$(CONFIG_IPV6_GRE) += ip6_gre.o
diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index cd3fb301da38..4b6b720971b9 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -83,11 +83,7 @@
83#include <linux/if_tunnel.h> 83#include <linux/if_tunnel.h>
84#include <linux/rtnetlink.h> 84#include <linux/rtnetlink.h>
85#include <linux/netconf.h> 85#include <linux/netconf.h>
86
87#ifdef CONFIG_IPV6_PRIVACY
88#include <linux/random.h> 86#include <linux/random.h>
89#endif
90
91#include <linux/uaccess.h> 87#include <linux/uaccess.h>
92#include <asm/unaligned.h> 88#include <asm/unaligned.h>
93 89
@@ -124,11 +120,9 @@ static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
124} 120}
125#endif 121#endif
126 122
127#ifdef CONFIG_IPV6_PRIVACY
128static void __ipv6_regen_rndid(struct inet6_dev *idev); 123static void __ipv6_regen_rndid(struct inet6_dev *idev);
129static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); 124static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr);
130static void ipv6_regen_rndid(unsigned long data); 125static void ipv6_regen_rndid(unsigned long data);
131#endif
132 126
133static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 127static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
134static int ipv6_count_addresses(struct inet6_dev *idev); 128static int ipv6_count_addresses(struct inet6_dev *idev);
@@ -183,13 +177,11 @@ static struct ipv6_devconf ipv6_devconf __read_mostly = {
183 .rtr_solicits = MAX_RTR_SOLICITATIONS, 177 .rtr_solicits = MAX_RTR_SOLICITATIONS,
184 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 178 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
185 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 179 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
186#ifdef CONFIG_IPV6_PRIVACY
187 .use_tempaddr = 0, 180 .use_tempaddr = 0,
188 .temp_valid_lft = TEMP_VALID_LIFETIME, 181 .temp_valid_lft = TEMP_VALID_LIFETIME,
189 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 182 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
190 .regen_max_retry = REGEN_MAX_RETRY, 183 .regen_max_retry = REGEN_MAX_RETRY,
191 .max_desync_factor = MAX_DESYNC_FACTOR, 184 .max_desync_factor = MAX_DESYNC_FACTOR,
192#endif
193 .max_addresses = IPV6_MAX_ADDRESSES, 185 .max_addresses = IPV6_MAX_ADDRESSES,
194 .accept_ra_defrtr = 1, 186 .accept_ra_defrtr = 1,
195 .accept_ra_pinfo = 1, 187 .accept_ra_pinfo = 1,
@@ -221,13 +213,11 @@ static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
221 .rtr_solicits = MAX_RTR_SOLICITATIONS, 213 .rtr_solicits = MAX_RTR_SOLICITATIONS,
222 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, 214 .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL,
223 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY, 215 .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,
224#ifdef CONFIG_IPV6_PRIVACY
225 .use_tempaddr = 0, 216 .use_tempaddr = 0,
226 .temp_valid_lft = TEMP_VALID_LIFETIME, 217 .temp_valid_lft = TEMP_VALID_LIFETIME,
227 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, 218 .temp_prefered_lft = TEMP_PREFERRED_LIFETIME,
228 .regen_max_retry = REGEN_MAX_RETRY, 219 .regen_max_retry = REGEN_MAX_RETRY,
229 .max_desync_factor = MAX_DESYNC_FACTOR, 220 .max_desync_factor = MAX_DESYNC_FACTOR,
230#endif
231 .max_addresses = IPV6_MAX_ADDRESSES, 221 .max_addresses = IPV6_MAX_ADDRESSES,
232 .accept_ra_defrtr = 1, 222 .accept_ra_defrtr = 1,
233 .accept_ra_pinfo = 1, 223 .accept_ra_pinfo = 1,
@@ -281,10 +271,24 @@ static void addrconf_mod_dad_timer(struct inet6_ifaddr *ifp,
281 271
282static int snmp6_alloc_dev(struct inet6_dev *idev) 272static int snmp6_alloc_dev(struct inet6_dev *idev)
283{ 273{
274 int i;
275
284 if (snmp_mib_init((void __percpu **)idev->stats.ipv6, 276 if (snmp_mib_init((void __percpu **)idev->stats.ipv6,
285 sizeof(struct ipstats_mib), 277 sizeof(struct ipstats_mib),
286 __alignof__(struct ipstats_mib)) < 0) 278 __alignof__(struct ipstats_mib)) < 0)
287 goto err_ip; 279 goto err_ip;
280
281 for_each_possible_cpu(i) {
282 struct ipstats_mib *addrconf_stats;
283 addrconf_stats = per_cpu_ptr(idev->stats.ipv6[0], i);
284 u64_stats_init(&addrconf_stats->syncp);
285#if SNMP_ARRAY_SZ == 2
286 addrconf_stats = per_cpu_ptr(idev->stats.ipv6[1], i);
287 u64_stats_init(&addrconf_stats->syncp);
288#endif
289 }
290
291
288 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device), 292 idev->stats.icmpv6dev = kzalloc(sizeof(struct icmpv6_mib_device),
289 GFP_KERNEL); 293 GFP_KERNEL);
290 if (!idev->stats.icmpv6dev) 294 if (!idev->stats.icmpv6dev)
@@ -371,7 +375,6 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
371 } 375 }
372#endif 376#endif
373 377
374#ifdef CONFIG_IPV6_PRIVACY
375 INIT_LIST_HEAD(&ndev->tempaddr_list); 378 INIT_LIST_HEAD(&ndev->tempaddr_list);
376 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 379 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
377 if ((dev->flags&IFF_LOOPBACK) || 380 if ((dev->flags&IFF_LOOPBACK) ||
@@ -384,7 +387,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
384 in6_dev_hold(ndev); 387 in6_dev_hold(ndev);
385 ipv6_regen_rndid((unsigned long) ndev); 388 ipv6_regen_rndid((unsigned long) ndev);
386 } 389 }
387#endif 390
388 ndev->token = in6addr_any; 391 ndev->token = in6addr_any;
389 392
390 if (netif_running(dev) && addrconf_qdisc_ok(dev)) 393 if (netif_running(dev) && addrconf_qdisc_ok(dev))
@@ -865,12 +868,10 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
865 /* Add to inet6_dev unicast addr list. */ 868 /* Add to inet6_dev unicast addr list. */
866 ipv6_link_dev_addr(idev, ifa); 869 ipv6_link_dev_addr(idev, ifa);
867 870
868#ifdef CONFIG_IPV6_PRIVACY
869 if (ifa->flags&IFA_F_TEMPORARY) { 871 if (ifa->flags&IFA_F_TEMPORARY) {
870 list_add(&ifa->tmp_list, &idev->tempaddr_list); 872 list_add(&ifa->tmp_list, &idev->tempaddr_list);
871 in6_ifa_hold(ifa); 873 in6_ifa_hold(ifa);
872 } 874 }
873#endif
874 875
875 in6_ifa_hold(ifa); 876 in6_ifa_hold(ifa);
876 write_unlock(&idev->lock); 877 write_unlock(&idev->lock);
@@ -913,7 +914,7 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
913 spin_unlock_bh(&addrconf_hash_lock); 914 spin_unlock_bh(&addrconf_hash_lock);
914 915
915 write_lock_bh(&idev->lock); 916 write_lock_bh(&idev->lock);
916#ifdef CONFIG_IPV6_PRIVACY 917
917 if (ifp->flags&IFA_F_TEMPORARY) { 918 if (ifp->flags&IFA_F_TEMPORARY) {
918 list_del(&ifp->tmp_list); 919 list_del(&ifp->tmp_list);
919 if (ifp->ifpub) { 920 if (ifp->ifpub) {
@@ -922,7 +923,6 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
922 } 923 }
923 __in6_ifa_put(ifp); 924 __in6_ifa_put(ifp);
924 } 925 }
925#endif
926 926
927 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 927 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
928 if (ifa == ifp) { 928 if (ifa == ifp) {
@@ -1013,7 +1013,6 @@ out:
1013 in6_ifa_put(ifp); 1013 in6_ifa_put(ifp);
1014} 1014}
1015 1015
1016#ifdef CONFIG_IPV6_PRIVACY
1017static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 1016static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1018{ 1017{
1019 struct inet6_dev *idev = ifp->idev; 1018 struct inet6_dev *idev = ifp->idev;
@@ -1116,7 +1115,6 @@ retry:
1116out: 1115out:
1117 return ret; 1116 return ret;
1118} 1117}
1119#endif
1120 1118
1121/* 1119/*
1122 * Choose an appropriate source address (RFC3484) 1120 * Choose an appropriate source address (RFC3484)
@@ -1131,9 +1129,7 @@ enum {
1131#endif 1129#endif
1132 IPV6_SADDR_RULE_OIF, 1130 IPV6_SADDR_RULE_OIF,
1133 IPV6_SADDR_RULE_LABEL, 1131 IPV6_SADDR_RULE_LABEL,
1134#ifdef CONFIG_IPV6_PRIVACY
1135 IPV6_SADDR_RULE_PRIVACY, 1132 IPV6_SADDR_RULE_PRIVACY,
1136#endif
1137 IPV6_SADDR_RULE_ORCHID, 1133 IPV6_SADDR_RULE_ORCHID,
1138 IPV6_SADDR_RULE_PREFIX, 1134 IPV6_SADDR_RULE_PREFIX,
1139 IPV6_SADDR_RULE_MAX 1135 IPV6_SADDR_RULE_MAX
@@ -1247,7 +1243,6 @@ static int ipv6_get_saddr_eval(struct net *net,
1247 &score->ifa->addr, score->addr_type, 1243 &score->ifa->addr, score->addr_type,
1248 score->ifa->idev->dev->ifindex) == dst->label; 1244 score->ifa->idev->dev->ifindex) == dst->label;
1249 break; 1245 break;
1250#ifdef CONFIG_IPV6_PRIVACY
1251 case IPV6_SADDR_RULE_PRIVACY: 1246 case IPV6_SADDR_RULE_PRIVACY:
1252 { 1247 {
1253 /* Rule 7: Prefer public address 1248 /* Rule 7: Prefer public address
@@ -1259,7 +1254,6 @@ static int ipv6_get_saddr_eval(struct net *net,
1259 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1254 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1260 break; 1255 break;
1261 } 1256 }
1262#endif
1263 case IPV6_SADDR_RULE_ORCHID: 1257 case IPV6_SADDR_RULE_ORCHID:
1264 /* Rule 8-: Prefer ORCHID vs ORCHID or 1258 /* Rule 8-: Prefer ORCHID vs ORCHID or
1265 * non-ORCHID vs non-ORCHID 1259 * non-ORCHID vs non-ORCHID
@@ -1588,7 +1582,6 @@ static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1588 if (dad_failed) 1582 if (dad_failed)
1589 ipv6_ifa_notify(0, ifp); 1583 ipv6_ifa_notify(0, ifp);
1590 in6_ifa_put(ifp); 1584 in6_ifa_put(ifp);
1591#ifdef CONFIG_IPV6_PRIVACY
1592 } else if (ifp->flags&IFA_F_TEMPORARY) { 1585 } else if (ifp->flags&IFA_F_TEMPORARY) {
1593 struct inet6_ifaddr *ifpub; 1586 struct inet6_ifaddr *ifpub;
1594 spin_lock_bh(&ifp->lock); 1587 spin_lock_bh(&ifp->lock);
@@ -1602,7 +1595,6 @@ static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1602 spin_unlock_bh(&ifp->lock); 1595 spin_unlock_bh(&ifp->lock);
1603 } 1596 }
1604 ipv6_del_addr(ifp); 1597 ipv6_del_addr(ifp);
1605#endif
1606 } else 1598 } else
1607 ipv6_del_addr(ifp); 1599 ipv6_del_addr(ifp);
1608} 1600}
@@ -1679,7 +1671,7 @@ void addrconf_leave_solict(struct inet6_dev *idev, const struct in6_addr *addr)
1679static void addrconf_join_anycast(struct inet6_ifaddr *ifp) 1671static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1680{ 1672{
1681 struct in6_addr addr; 1673 struct in6_addr addr;
1682 if (ifp->prefix_len == 127) /* RFC 6164 */ 1674 if (ifp->prefix_len >= 127) /* RFC 6164 */
1683 return; 1675 return;
1684 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1676 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1685 if (ipv6_addr_any(&addr)) 1677 if (ipv6_addr_any(&addr))
@@ -1690,7 +1682,7 @@ static void addrconf_join_anycast(struct inet6_ifaddr *ifp)
1690static void addrconf_leave_anycast(struct inet6_ifaddr *ifp) 1682static void addrconf_leave_anycast(struct inet6_ifaddr *ifp)
1691{ 1683{
1692 struct in6_addr addr; 1684 struct in6_addr addr;
1693 if (ifp->prefix_len == 127) /* RFC 6164 */ 1685 if (ifp->prefix_len >= 127) /* RFC 6164 */
1694 return; 1686 return;
1695 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len); 1687 ipv6_addr_prefix(&addr, &ifp->addr, ifp->prefix_len);
1696 if (ipv6_addr_any(&addr)) 1688 if (ipv6_addr_any(&addr))
@@ -1851,7 +1843,6 @@ static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1851 return err; 1843 return err;
1852} 1844}
1853 1845
1854#ifdef CONFIG_IPV6_PRIVACY
1855/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 1846/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1856static void __ipv6_regen_rndid(struct inet6_dev *idev) 1847static void __ipv6_regen_rndid(struct inet6_dev *idev)
1857{ 1848{
@@ -1919,7 +1910,6 @@ static void __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmp
1919 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0) 1910 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1920 __ipv6_regen_rndid(idev); 1911 __ipv6_regen_rndid(idev);
1921} 1912}
1922#endif
1923 1913
1924/* 1914/*
1925 * Add prefix route. 1915 * Add prefix route.
@@ -2006,23 +1996,6 @@ static void addrconf_add_mroute(struct net_device *dev)
2006 ip6_route_add(&cfg); 1996 ip6_route_add(&cfg);
2007} 1997}
2008 1998
2009#if IS_ENABLED(CONFIG_IPV6_SIT)
2010static void sit_route_add(struct net_device *dev)
2011{
2012 struct fib6_config cfg = {
2013 .fc_table = RT6_TABLE_MAIN,
2014 .fc_metric = IP6_RT_PRIO_ADDRCONF,
2015 .fc_ifindex = dev->ifindex,
2016 .fc_dst_len = 96,
2017 .fc_flags = RTF_UP | RTF_NONEXTHOP,
2018 .fc_nlinfo.nl_net = dev_net(dev),
2019 };
2020
2021 /* prefix length - 96 bits "::d.d.d.d" */
2022 ip6_route_add(&cfg);
2023}
2024#endif
2025
2026static struct inet6_dev *addrconf_add_dev(struct net_device *dev) 1999static struct inet6_dev *addrconf_add_dev(struct net_device *dev)
2027{ 2000{
2028 struct inet6_dev *idev; 2001 struct inet6_dev *idev;
@@ -2207,9 +2180,7 @@ ok:
2207 if (ifp) { 2180 if (ifp) {
2208 int flags; 2181 int flags;
2209 unsigned long now; 2182 unsigned long now;
2210#ifdef CONFIG_IPV6_PRIVACY
2211 struct inet6_ifaddr *ift; 2183 struct inet6_ifaddr *ift;
2212#endif
2213 u32 stored_lft; 2184 u32 stored_lft;
2214 2185
2215 /* update lifetime (RFC2462 5.5.3 e) */ 2186 /* update lifetime (RFC2462 5.5.3 e) */
@@ -2250,7 +2221,6 @@ ok:
2250 } else 2221 } else
2251 spin_unlock(&ifp->lock); 2222 spin_unlock(&ifp->lock);
2252 2223
2253#ifdef CONFIG_IPV6_PRIVACY
2254 read_lock_bh(&in6_dev->lock); 2224 read_lock_bh(&in6_dev->lock);
2255 /* update all temporary addresses in the list */ 2225 /* update all temporary addresses in the list */
2256 list_for_each_entry(ift, &in6_dev->tempaddr_list, 2226 list_for_each_entry(ift, &in6_dev->tempaddr_list,
@@ -2315,7 +2285,7 @@ ok:
2315 } else { 2285 } else {
2316 read_unlock_bh(&in6_dev->lock); 2286 read_unlock_bh(&in6_dev->lock);
2317 } 2287 }
2318#endif 2288
2319 in6_ifa_put(ifp); 2289 in6_ifa_put(ifp);
2320 addrconf_verify(0); 2290 addrconf_verify(0);
2321 } 2291 }
@@ -2539,7 +2509,8 @@ static void add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
2539 struct inet6_ifaddr *ifp; 2509 struct inet6_ifaddr *ifp;
2540 2510
2541 ifp = ipv6_add_addr(idev, addr, NULL, plen, 2511 ifp = ipv6_add_addr(idev, addr, NULL, plen,
2542 scope, IFA_F_PERMANENT, 0, 0); 2512 scope, IFA_F_PERMANENT,
2513 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2543 if (!IS_ERR(ifp)) { 2514 if (!IS_ERR(ifp)) {
2544 spin_lock_bh(&ifp->lock); 2515 spin_lock_bh(&ifp->lock);
2545 ifp->flags &= ~IFA_F_TENTATIVE; 2516 ifp->flags &= ~IFA_F_TENTATIVE;
@@ -2555,7 +2526,8 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
2555 struct in6_addr addr; 2526 struct in6_addr addr;
2556 struct net_device *dev; 2527 struct net_device *dev;
2557 struct net *net = dev_net(idev->dev); 2528 struct net *net = dev_net(idev->dev);
2558 int scope; 2529 int scope, plen;
2530 u32 pflags = 0;
2559 2531
2560 ASSERT_RTNL(); 2532 ASSERT_RTNL();
2561 2533
@@ -2565,12 +2537,16 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
2565 if (idev->dev->flags&IFF_POINTOPOINT) { 2537 if (idev->dev->flags&IFF_POINTOPOINT) {
2566 addr.s6_addr32[0] = htonl(0xfe800000); 2538 addr.s6_addr32[0] = htonl(0xfe800000);
2567 scope = IFA_LINK; 2539 scope = IFA_LINK;
2540 plen = 64;
2568 } else { 2541 } else {
2569 scope = IPV6_ADDR_COMPATv4; 2542 scope = IPV6_ADDR_COMPATv4;
2543 plen = 96;
2544 pflags |= RTF_NONEXTHOP;
2570 } 2545 }
2571 2546
2572 if (addr.s6_addr32[3]) { 2547 if (addr.s6_addr32[3]) {
2573 add_addr(idev, &addr, 128, scope); 2548 add_addr(idev, &addr, plen, scope);
2549 addrconf_prefix_route(&addr, plen, idev->dev, 0, pflags);
2574 return; 2550 return;
2575 } 2551 }
2576 2552
@@ -2582,7 +2558,6 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
2582 int flag = scope; 2558 int flag = scope;
2583 2559
2584 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) { 2560 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
2585 int plen;
2586 2561
2587 addr.s6_addr32[3] = ifa->ifa_local; 2562 addr.s6_addr32[3] = ifa->ifa_local;
2588 2563
@@ -2593,12 +2568,10 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
2593 continue; 2568 continue;
2594 flag |= IFA_HOST; 2569 flag |= IFA_HOST;
2595 } 2570 }
2596 if (idev->dev->flags&IFF_POINTOPOINT)
2597 plen = 64;
2598 else
2599 plen = 96;
2600 2571
2601 add_addr(idev, &addr, plen, flag); 2572 add_addr(idev, &addr, plen, flag);
2573 addrconf_prefix_route(&addr, plen, idev->dev, 0,
2574 pflags);
2602 } 2575 }
2603 } 2576 }
2604 } 2577 }
@@ -2641,7 +2614,7 @@ static void init_loopback(struct net_device *dev)
2641 if (sp_ifa->rt) 2614 if (sp_ifa->rt)
2642 continue; 2615 continue;
2643 2616
2644 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, 0); 2617 sp_rt = addrconf_dst_alloc(idev, &sp_ifa->addr, false);
2645 2618
2646 /* Failure cases are ignored */ 2619 /* Failure cases are ignored */
2647 if (!IS_ERR(sp_rt)) { 2620 if (!IS_ERR(sp_rt)) {
@@ -2665,7 +2638,8 @@ static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr
2665#endif 2638#endif
2666 2639
2667 2640
2668 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 0, 0); 2641 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2642 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2669 if (!IS_ERR(ifp)) { 2643 if (!IS_ERR(ifp)) {
2670 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0); 2644 addrconf_prefix_route(&ifp->addr, ifp->prefix_len, idev->dev, 0, 0);
2671 addrconf_dad_start(ifp); 2645 addrconf_dad_start(ifp);
@@ -2724,7 +2698,6 @@ static void addrconf_sit_config(struct net_device *dev)
2724 struct in6_addr addr; 2698 struct in6_addr addr;
2725 2699
2726 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2700 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2727 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2728 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2701 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2729 addrconf_add_linklocal(idev, &addr); 2702 addrconf_add_linklocal(idev, &addr);
2730 return; 2703 return;
@@ -2734,8 +2707,6 @@ static void addrconf_sit_config(struct net_device *dev)
2734 2707
2735 if (dev->flags&IFF_POINTOPOINT) 2708 if (dev->flags&IFF_POINTOPOINT)
2736 addrconf_add_mroute(dev); 2709 addrconf_add_mroute(dev);
2737 else
2738 sit_route_add(dev);
2739} 2710}
2740#endif 2711#endif
2741 2712
@@ -2753,8 +2724,6 @@ static void addrconf_gre_config(struct net_device *dev)
2753 } 2724 }
2754 2725
2755 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2726 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2756 addrconf_prefix_route(&addr, 64, dev, 0, 0);
2757
2758 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev)) 2727 if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
2759 addrconf_add_linklocal(idev, &addr); 2728 addrconf_add_linklocal(idev, &addr);
2760} 2729}
@@ -2995,7 +2964,6 @@ static int addrconf_ifdown(struct net_device *dev, int how)
2995 if (!how) 2964 if (!how)
2996 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 2965 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2997 2966
2998#ifdef CONFIG_IPV6_PRIVACY
2999 if (how && del_timer(&idev->regen_timer)) 2967 if (how && del_timer(&idev->regen_timer))
3000 in6_dev_put(idev); 2968 in6_dev_put(idev);
3001 2969
@@ -3015,7 +2983,6 @@ static int addrconf_ifdown(struct net_device *dev, int how)
3015 in6_ifa_put(ifa); 2983 in6_ifa_put(ifa);
3016 write_lock_bh(&idev->lock); 2984 write_lock_bh(&idev->lock);
3017 } 2985 }
3018#endif
3019 2986
3020 while (!list_empty(&idev->addr_list)) { 2987 while (!list_empty(&idev->addr_list)) {
3021 ifa = list_first_entry(&idev->addr_list, 2988 ifa = list_first_entry(&idev->addr_list,
@@ -3222,6 +3189,22 @@ out:
3222 in6_ifa_put(ifp); 3189 in6_ifa_put(ifp);
3223} 3190}
3224 3191
3192/* ifp->idev must be at least read locked */
3193static bool ipv6_lonely_lladdr(struct inet6_ifaddr *ifp)
3194{
3195 struct inet6_ifaddr *ifpiter;
3196 struct inet6_dev *idev = ifp->idev;
3197
3198 list_for_each_entry(ifpiter, &idev->addr_list, if_list) {
3199 if (ifp != ifpiter && ifpiter->scope == IFA_LINK &&
3200 (ifpiter->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|
3201 IFA_F_OPTIMISTIC|IFA_F_DADFAILED)) ==
3202 IFA_F_PERMANENT)
3203 return false;
3204 }
3205 return true;
3206}
3207
3225static void addrconf_dad_completed(struct inet6_ifaddr *ifp) 3208static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3226{ 3209{
3227 struct net_device *dev = ifp->idev->dev; 3210 struct net_device *dev = ifp->idev->dev;
@@ -3241,14 +3224,11 @@ static void addrconf_dad_completed(struct inet6_ifaddr *ifp)
3241 */ 3224 */
3242 3225
3243 read_lock_bh(&ifp->idev->lock); 3226 read_lock_bh(&ifp->idev->lock);
3244 spin_lock(&ifp->lock); 3227 send_mld = ifp->scope == IFA_LINK && ipv6_lonely_lladdr(ifp);
3245 send_mld = ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL &&
3246 ifp->idev->valid_ll_addr_cnt == 1;
3247 send_rs = send_mld && 3228 send_rs = send_mld &&
3248 ipv6_accept_ra(ifp->idev) && 3229 ipv6_accept_ra(ifp->idev) &&
3249 ifp->idev->cnf.rtr_solicits > 0 && 3230 ifp->idev->cnf.rtr_solicits > 0 &&
3250 (dev->flags&IFF_LOOPBACK) == 0; 3231 (dev->flags&IFF_LOOPBACK) == 0;
3251 spin_unlock(&ifp->lock);
3252 read_unlock_bh(&ifp->idev->lock); 3232 read_unlock_bh(&ifp->idev->lock);
3253 3233
3254 /* While dad is in progress mld report's source address is in6_addrany. 3234 /* While dad is in progress mld report's source address is in6_addrany.
@@ -3491,7 +3471,12 @@ restart:
3491 &inet6_addr_lst[i], addr_lst) { 3471 &inet6_addr_lst[i], addr_lst) {
3492 unsigned long age; 3472 unsigned long age;
3493 3473
3494 if (ifp->flags & IFA_F_PERMANENT) 3474 /* When setting preferred_lft to a value not zero or
3475 * infinity, while valid_lft is infinity
3476 * IFA_F_PERMANENT has a non-infinity life time.
3477 */
3478 if ((ifp->flags & IFA_F_PERMANENT) &&
3479 (ifp->prefered_lft == INFINITY_LIFE_TIME))
3495 continue; 3480 continue;
3496 3481
3497 spin_lock(&ifp->lock); 3482 spin_lock(&ifp->lock);
@@ -3516,7 +3501,8 @@ restart:
3516 ifp->flags |= IFA_F_DEPRECATED; 3501 ifp->flags |= IFA_F_DEPRECATED;
3517 } 3502 }
3518 3503
3519 if (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)) 3504 if ((ifp->valid_lft != INFINITY_LIFE_TIME) &&
3505 (time_before(ifp->tstamp + ifp->valid_lft * HZ, next)))
3520 next = ifp->tstamp + ifp->valid_lft * HZ; 3506 next = ifp->tstamp + ifp->valid_lft * HZ;
3521 3507
3522 spin_unlock(&ifp->lock); 3508 spin_unlock(&ifp->lock);
@@ -3528,7 +3514,6 @@ restart:
3528 in6_ifa_put(ifp); 3514 in6_ifa_put(ifp);
3529 goto restart; 3515 goto restart;
3530 } 3516 }
3531#ifdef CONFIG_IPV6_PRIVACY
3532 } else if ((ifp->flags&IFA_F_TEMPORARY) && 3517 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3533 !(ifp->flags&IFA_F_TENTATIVE)) { 3518 !(ifp->flags&IFA_F_TENTATIVE)) {
3534 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 3519 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
@@ -3556,7 +3541,6 @@ restart:
3556 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 3541 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next))
3557 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ; 3542 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3558 spin_unlock(&ifp->lock); 3543 spin_unlock(&ifp->lock);
3559#endif
3560 } else { 3544 } else {
3561 /* ifp->prefered_lft <= ifp->valid_lft */ 3545 /* ifp->prefered_lft <= ifp->valid_lft */
3562 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3546 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
@@ -3798,7 +3782,8 @@ static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
3798 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 3782 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
3799 ifa->idev->dev->ifindex); 3783 ifa->idev->dev->ifindex);
3800 3784
3801 if (!(ifa->flags&IFA_F_PERMANENT)) { 3785 if (!((ifa->flags&IFA_F_PERMANENT) &&
3786 (ifa->prefered_lft == INFINITY_LIFE_TIME))) {
3802 preferred = ifa->prefered_lft; 3787 preferred = ifa->prefered_lft;
3803 valid = ifa->valid_lft; 3788 valid = ifa->valid_lft;
3804 if (preferred != INFINITY_LIFE_TIME) { 3789 if (preferred != INFINITY_LIFE_TIME) {
@@ -4128,13 +4113,11 @@ static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4128 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4113 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4129 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4114 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4130 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 4115 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4131#ifdef CONFIG_IPV6_PRIVACY
4132 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 4116 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4133 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 4117 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4134 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 4118 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4135 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 4119 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4136 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 4120 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4137#endif
4138 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 4121 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4139 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 4122 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4140 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4123 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
@@ -4542,19 +4525,6 @@ errout:
4542 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err); 4525 rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
4543} 4526}
4544 4527
4545static void update_valid_ll_addr_cnt(struct inet6_ifaddr *ifp, int count)
4546{
4547 write_lock_bh(&ifp->idev->lock);
4548 spin_lock(&ifp->lock);
4549 if (((ifp->flags & (IFA_F_PERMANENT|IFA_F_TENTATIVE|IFA_F_OPTIMISTIC|
4550 IFA_F_DADFAILED)) == IFA_F_PERMANENT) &&
4551 (ipv6_addr_type(&ifp->addr) & IPV6_ADDR_LINKLOCAL))
4552 ifp->idev->valid_ll_addr_cnt += count;
4553 WARN_ON(ifp->idev->valid_ll_addr_cnt < 0);
4554 spin_unlock(&ifp->lock);
4555 write_unlock_bh(&ifp->idev->lock);
4556}
4557
4558static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp) 4528static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4559{ 4529{
4560 struct net *net = dev_net(ifp->idev->dev); 4530 struct net *net = dev_net(ifp->idev->dev);
@@ -4563,8 +4533,6 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4563 4533
4564 switch (event) { 4534 switch (event) {
4565 case RTM_NEWADDR: 4535 case RTM_NEWADDR:
4566 update_valid_ll_addr_cnt(ifp, 1);
4567
4568 /* 4536 /*
4569 * If the address was optimistic 4537 * If the address was optimistic
4570 * we inserted the route at the start of 4538 * we inserted the route at the start of
@@ -4580,8 +4548,6 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
4580 ifp->idev->dev, 0, 0); 4548 ifp->idev->dev, 0, 0);
4581 break; 4549 break;
4582 case RTM_DELADDR: 4550 case RTM_DELADDR:
4583 update_valid_ll_addr_cnt(ifp, -1);
4584
4585 if (ifp->idev->cnf.forwarding) 4551 if (ifp->idev->cnf.forwarding)
4586 addrconf_leave_anycast(ifp); 4552 addrconf_leave_anycast(ifp);
4587 addrconf_leave_solict(ifp->idev, &ifp->addr); 4553 addrconf_leave_solict(ifp->idev, &ifp->addr);
@@ -4828,7 +4794,6 @@ static struct addrconf_sysctl_table
4828 .mode = 0644, 4794 .mode = 0644,
4829 .proc_handler = proc_dointvec_ms_jiffies, 4795 .proc_handler = proc_dointvec_ms_jiffies,
4830 }, 4796 },
4831#ifdef CONFIG_IPV6_PRIVACY
4832 { 4797 {
4833 .procname = "use_tempaddr", 4798 .procname = "use_tempaddr",
4834 .data = &ipv6_devconf.use_tempaddr, 4799 .data = &ipv6_devconf.use_tempaddr,
@@ -4864,7 +4829,6 @@ static struct addrconf_sysctl_table
4864 .mode = 0644, 4829 .mode = 0644,
4865 .proc_handler = proc_dointvec, 4830 .proc_handler = proc_dointvec,
4866 }, 4831 },
4867#endif
4868 { 4832 {
4869 .procname = "max_addresses", 4833 .procname = "max_addresses",
4870 .data = &ipv6_devconf.max_addresses, 4834 .data = &ipv6_devconf.max_addresses,
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index 7c96100b021e..4fbdb7046d28 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -110,11 +110,6 @@ static int inet6_create(struct net *net, struct socket *sock, int protocol,
110 int try_loading_module = 0; 110 int try_loading_module = 0;
111 int err; 111 int err;
112 112
113 if (sock->type != SOCK_RAW &&
114 sock->type != SOCK_DGRAM &&
115 !inet_ehash_secret)
116 build_ehash_secret();
117
118 /* Look for the requested type/protocol pair. */ 113 /* Look for the requested type/protocol pair. */
119lookup_protocol: 114lookup_protocol:
120 err = -ESOCKTNOSUPPORT; 115 err = -ESOCKTNOSUPPORT;
@@ -364,7 +359,7 @@ int inet6_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
364 inet->inet_rcv_saddr = v4addr; 359 inet->inet_rcv_saddr = v4addr;
365 inet->inet_saddr = v4addr; 360 inet->inet_saddr = v4addr;
366 361
367 np->rcv_saddr = addr->sin6_addr; 362 sk->sk_v6_rcv_saddr = addr->sin6_addr;
368 363
369 if (!(addr_type & IPV6_ADDR_MULTICAST)) 364 if (!(addr_type & IPV6_ADDR_MULTICAST))
370 np->saddr = addr->sin6_addr; 365 np->saddr = addr->sin6_addr;
@@ -461,14 +456,14 @@ int inet6_getname(struct socket *sock, struct sockaddr *uaddr,
461 peer == 1) 456 peer == 1)
462 return -ENOTCONN; 457 return -ENOTCONN;
463 sin->sin6_port = inet->inet_dport; 458 sin->sin6_port = inet->inet_dport;
464 sin->sin6_addr = np->daddr; 459 sin->sin6_addr = sk->sk_v6_daddr;
465 if (np->sndflow) 460 if (np->sndflow)
466 sin->sin6_flowinfo = np->flow_label; 461 sin->sin6_flowinfo = np->flow_label;
467 } else { 462 } else {
468 if (ipv6_addr_any(&np->rcv_saddr)) 463 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
469 sin->sin6_addr = np->saddr; 464 sin->sin6_addr = np->saddr;
470 else 465 else
471 sin->sin6_addr = np->rcv_saddr; 466 sin->sin6_addr = sk->sk_v6_rcv_saddr;
472 467
473 sin->sin6_port = inet->inet_sport; 468 sin->sin6_port = inet->inet_sport;
474 } 469 }
@@ -655,7 +650,7 @@ int inet6_sk_rebuild_header(struct sock *sk)
655 650
656 memset(&fl6, 0, sizeof(fl6)); 651 memset(&fl6, 0, sizeof(fl6));
657 fl6.flowi6_proto = sk->sk_protocol; 652 fl6.flowi6_proto = sk->sk_protocol;
658 fl6.daddr = np->daddr; 653 fl6.daddr = sk->sk_v6_daddr;
659 fl6.saddr = np->saddr; 654 fl6.saddr = np->saddr;
660 fl6.flowlabel = np->flow_label; 655 fl6.flowlabel = np->flow_label;
661 fl6.flowi6_oif = sk->sk_bound_dev_if; 656 fl6.flowi6_oif = sk->sk_bound_dev_if;
@@ -719,6 +714,8 @@ static void ipv6_packet_cleanup(void)
719 714
720static int __net_init ipv6_init_mibs(struct net *net) 715static int __net_init ipv6_init_mibs(struct net *net)
721{ 716{
717 int i;
718
722 if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6, 719 if (snmp_mib_init((void __percpu **)net->mib.udp_stats_in6,
723 sizeof(struct udp_mib), 720 sizeof(struct udp_mib),
724 __alignof__(struct udp_mib)) < 0) 721 __alignof__(struct udp_mib)) < 0)
@@ -731,6 +728,18 @@ static int __net_init ipv6_init_mibs(struct net *net)
731 sizeof(struct ipstats_mib), 728 sizeof(struct ipstats_mib),
732 __alignof__(struct ipstats_mib)) < 0) 729 __alignof__(struct ipstats_mib)) < 0)
733 goto err_ip_mib; 730 goto err_ip_mib;
731
732 for_each_possible_cpu(i) {
733 struct ipstats_mib *af_inet6_stats;
734 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics[0], i);
735 u64_stats_init(&af_inet6_stats->syncp);
736#if SNMP_ARRAY_SZ == 2
737 af_inet6_stats = per_cpu_ptr(net->mib.ipv6_statistics[1], i);
738 u64_stats_init(&af_inet6_stats->syncp);
739#endif
740 }
741
742
734 if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics, 743 if (snmp_mib_init((void __percpu **)net->mib.icmpv6_statistics,
735 sizeof(struct icmpv6_mib), 744 sizeof(struct icmpv6_mib),
736 __alignof__(struct icmpv6_mib)) < 0) 745 __alignof__(struct icmpv6_mib)) < 0)
@@ -870,8 +879,6 @@ static int __init inet6_init(void)
870 if (err) 879 if (err)
871 goto out_sock_register_fail; 880 goto out_sock_register_fail;
872 881
873 tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
874
875 /* 882 /*
876 * ipngwg API draft makes clear that the correct semantics 883 * ipngwg API draft makes clear that the correct semantics
877 * for TCP and UDP is to consider one TCP and UDP instance 884 * for TCP and UDP is to consider one TCP and UDP instance
@@ -965,10 +972,10 @@ out:
965 972
966#ifdef CONFIG_SYSCTL 973#ifdef CONFIG_SYSCTL
967sysctl_fail: 974sysctl_fail:
968 ipv6_packet_cleanup(); 975 pingv6_exit();
969#endif 976#endif
970pingv6_fail: 977pingv6_fail:
971 pingv6_exit(); 978 ipv6_packet_cleanup();
972ipv6_packet_fail: 979ipv6_packet_fail:
973 tcpv6_exit(); 980 tcpv6_exit();
974tcpv6_fail: 981tcpv6_fail:
@@ -1028,52 +1035,4 @@ out_unregister_tcp_proto:
1028} 1035}
1029module_init(inet6_init); 1036module_init(inet6_init);
1030 1037
1031static void __exit inet6_exit(void)
1032{
1033 if (disable_ipv6_mod)
1034 return;
1035
1036 /* First of all disallow new sockets creation. */
1037 sock_unregister(PF_INET6);
1038 /* Disallow any further netlink messages */
1039 rtnl_unregister_all(PF_INET6);
1040
1041 udpv6_exit();
1042 udplitev6_exit();
1043 tcpv6_exit();
1044
1045 /* Cleanup code parts. */
1046 ipv6_packet_cleanup();
1047 ipv6_frag_exit();
1048 ipv6_exthdrs_exit();
1049 addrconf_cleanup();
1050 ip6_flowlabel_cleanup();
1051 ndisc_late_cleanup();
1052 ip6_route_cleanup();
1053#ifdef CONFIG_PROC_FS
1054
1055 /* Cleanup code parts. */
1056 if6_proc_exit();
1057 ipv6_misc_proc_exit();
1058 udplite6_proc_exit();
1059 raw6_proc_exit();
1060#endif
1061 ipv6_netfilter_fini();
1062 ipv6_stub = NULL;
1063 igmp6_cleanup();
1064 ndisc_cleanup();
1065 ip6_mr_cleanup();
1066 icmpv6_cleanup();
1067 rawv6_exit();
1068
1069 unregister_pernet_subsys(&inet6_net_ops);
1070 proto_unregister(&rawv6_prot);
1071 proto_unregister(&udplitev6_prot);
1072 proto_unregister(&udpv6_prot);
1073 proto_unregister(&tcpv6_prot);
1074
1075 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1076}
1077module_exit(inet6_exit);
1078
1079MODULE_ALIAS_NETPROTO(PF_INET6); 1038MODULE_ALIAS_NETPROTO(PF_INET6);
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 48b6bd2a9a14..93b1aa34c432 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -73,7 +73,6 @@ int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
73 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 73 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
74 if (flowlabel == NULL) 74 if (flowlabel == NULL)
75 return -EINVAL; 75 return -EINVAL;
76 usin->sin6_addr = flowlabel->dst;
77 } 76 }
78 } 77 }
79 78
@@ -107,16 +106,16 @@ ipv4_connected:
107 if (err) 106 if (err)
108 goto out; 107 goto out;
109 108
110 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr); 109 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
111 110
112 if (ipv6_addr_any(&np->saddr) || 111 if (ipv6_addr_any(&np->saddr) ||
113 ipv6_mapped_addr_any(&np->saddr)) 112 ipv6_mapped_addr_any(&np->saddr))
114 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 113 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
115 114
116 if (ipv6_addr_any(&np->rcv_saddr) || 115 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
117 ipv6_mapped_addr_any(&np->rcv_saddr)) { 116 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
118 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 117 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
119 &np->rcv_saddr); 118 &sk->sk_v6_rcv_saddr);
120 if (sk->sk_prot->rehash) 119 if (sk->sk_prot->rehash)
121 sk->sk_prot->rehash(sk); 120 sk->sk_prot->rehash(sk);
122 } 121 }
@@ -145,7 +144,7 @@ ipv4_connected:
145 } 144 }
146 } 145 }
147 146
148 np->daddr = *daddr; 147 sk->sk_v6_daddr = *daddr;
149 np->flow_label = fl6.flowlabel; 148 np->flow_label = fl6.flowlabel;
150 149
151 inet->inet_dport = usin->sin6_port; 150 inet->inet_dport = usin->sin6_port;
@@ -156,7 +155,7 @@ ipv4_connected:
156 */ 155 */
157 156
158 fl6.flowi6_proto = sk->sk_protocol; 157 fl6.flowi6_proto = sk->sk_protocol;
159 fl6.daddr = np->daddr; 158 fl6.daddr = sk->sk_v6_daddr;
160 fl6.saddr = np->saddr; 159 fl6.saddr = np->saddr;
161 fl6.flowi6_oif = sk->sk_bound_dev_if; 160 fl6.flowi6_oif = sk->sk_bound_dev_if;
162 fl6.flowi6_mark = sk->sk_mark; 161 fl6.flowi6_mark = sk->sk_mark;
@@ -183,16 +182,16 @@ ipv4_connected:
183 if (ipv6_addr_any(&np->saddr)) 182 if (ipv6_addr_any(&np->saddr))
184 np->saddr = fl6.saddr; 183 np->saddr = fl6.saddr;
185 184
186 if (ipv6_addr_any(&np->rcv_saddr)) { 185 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
187 np->rcv_saddr = fl6.saddr; 186 sk->sk_v6_rcv_saddr = fl6.saddr;
188 inet->inet_rcv_saddr = LOOPBACK4_IPV6; 187 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
189 if (sk->sk_prot->rehash) 188 if (sk->sk_prot->rehash)
190 sk->sk_prot->rehash(sk); 189 sk->sk_prot->rehash(sk);
191 } 190 }
192 191
193 ip6_dst_store(sk, dst, 192 ip6_dst_store(sk, dst,
194 ipv6_addr_equal(&fl6.daddr, &np->daddr) ? 193 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
195 &np->daddr : NULL, 194 &sk->sk_v6_daddr : NULL,
196#ifdef CONFIG_IPV6_SUBTREES 195#ifdef CONFIG_IPV6_SUBTREES
197 ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 196 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
198 &np->saddr : 197 &np->saddr :
@@ -318,7 +317,7 @@ void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
318/* 317/*
319 * Handle MSG_ERRQUEUE 318 * Handle MSG_ERRQUEUE
320 */ 319 */
321int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) 320int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
322{ 321{
323 struct ipv6_pinfo *np = inet6_sk(sk); 322 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct sock_exterr_skb *serr; 323 struct sock_exterr_skb *serr;
@@ -369,6 +368,7 @@ int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
369 &sin->sin6_addr); 368 &sin->sin6_addr);
370 sin->sin6_scope_id = 0; 369 sin->sin6_scope_id = 0;
371 } 370 }
371 *addr_len = sizeof(*sin);
372 } 372 }
373 373
374 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); 374 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
@@ -377,6 +377,7 @@ int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
377 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) { 377 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
378 sin->sin6_family = AF_INET6; 378 sin->sin6_family = AF_INET6;
379 sin->sin6_flowinfo = 0; 379 sin->sin6_flowinfo = 0;
380 sin->sin6_port = 0;
380 if (skb->protocol == htons(ETH_P_IPV6)) { 381 if (skb->protocol == htons(ETH_P_IPV6)) {
381 sin->sin6_addr = ipv6_hdr(skb)->saddr; 382 sin->sin6_addr = ipv6_hdr(skb)->saddr;
382 if (np->rxopt.all) 383 if (np->rxopt.all)
@@ -423,7 +424,8 @@ EXPORT_SYMBOL_GPL(ipv6_recv_error);
423/* 424/*
424 * Handle IPV6_RECVPATHMTU 425 * Handle IPV6_RECVPATHMTU
425 */ 426 */
426int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len) 427int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
428 int *addr_len)
427{ 429{
428 struct ipv6_pinfo *np = inet6_sk(sk); 430 struct ipv6_pinfo *np = inet6_sk(sk);
429 struct sk_buff *skb; 431 struct sk_buff *skb;
@@ -457,6 +459,7 @@ int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
457 sin->sin6_port = 0; 459 sin->sin6_port = 0;
458 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id; 460 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
459 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr; 461 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
462 *addr_len = sizeof(*sin);
460 } 463 }
461 464
462 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info); 465 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
@@ -883,11 +886,10 @@ EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
883void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp, 886void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
884 __u16 srcp, __u16 destp, int bucket) 887 __u16 srcp, __u16 destp, int bucket)
885{ 888{
886 struct ipv6_pinfo *np = inet6_sk(sp);
887 const struct in6_addr *dest, *src; 889 const struct in6_addr *dest, *src;
888 890
889 dest = &np->daddr; 891 dest = &sp->sk_v6_daddr;
890 src = &np->rcv_saddr; 892 src = &sp->sk_v6_rcv_saddr;
891 seq_printf(seq, 893 seq_printf(seq,
892 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 894 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
893 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n", 895 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c
index e67e63f9858d..b8719df0366e 100644
--- a/net/ipv6/esp6.c
+++ b/net/ipv6/esp6.c
@@ -164,10 +164,9 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
164 u8 *iv; 164 u8 *iv;
165 u8 *tail; 165 u8 *tail;
166 __be32 *seqhi; 166 __be32 *seqhi;
167 struct esp_data *esp = x->data;
168 167
169 /* skb is pure payload to encrypt */ 168 /* skb is pure payload to encrypt */
170 aead = esp->aead; 169 aead = x->data;
171 alen = crypto_aead_authsize(aead); 170 alen = crypto_aead_authsize(aead);
172 171
173 tfclen = 0; 172 tfclen = 0;
@@ -181,8 +180,6 @@ static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
181 } 180 }
182 blksize = ALIGN(crypto_aead_blocksize(aead), 4); 181 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
183 clen = ALIGN(skb->len + 2 + tfclen, blksize); 182 clen = ALIGN(skb->len + 2 + tfclen, blksize);
184 if (esp->padlen)
185 clen = ALIGN(clen, esp->padlen);
186 plen = clen - skb->len - tfclen; 183 plen = clen - skb->len - tfclen;
187 184
188 err = skb_cow_data(skb, tfclen + plen + alen, &trailer); 185 err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
@@ -271,8 +268,7 @@ error:
271static int esp_input_done2(struct sk_buff *skb, int err) 268static int esp_input_done2(struct sk_buff *skb, int err)
272{ 269{
273 struct xfrm_state *x = xfrm_input_state(skb); 270 struct xfrm_state *x = xfrm_input_state(skb);
274 struct esp_data *esp = x->data; 271 struct crypto_aead *aead = x->data;
275 struct crypto_aead *aead = esp->aead;
276 int alen = crypto_aead_authsize(aead); 272 int alen = crypto_aead_authsize(aead);
277 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead); 273 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
278 int elen = skb->len - hlen; 274 int elen = skb->len - hlen;
@@ -325,8 +321,7 @@ static void esp_input_done(struct crypto_async_request *base, int err)
325static int esp6_input(struct xfrm_state *x, struct sk_buff *skb) 321static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
326{ 322{
327 struct ip_esp_hdr *esph; 323 struct ip_esp_hdr *esph;
328 struct esp_data *esp = x->data; 324 struct crypto_aead *aead = x->data;
329 struct crypto_aead *aead = esp->aead;
330 struct aead_request *req; 325 struct aead_request *req;
331 struct sk_buff *trailer; 326 struct sk_buff *trailer;
332 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead); 327 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
@@ -414,9 +409,8 @@ out:
414 409
415static u32 esp6_get_mtu(struct xfrm_state *x, int mtu) 410static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
416{ 411{
417 struct esp_data *esp = x->data; 412 struct crypto_aead *aead = x->data;
418 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4); 413 u32 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
419 u32 align = max_t(u32, blksize, esp->padlen);
420 unsigned int net_adj; 414 unsigned int net_adj;
421 415
422 if (x->props.mode != XFRM_MODE_TUNNEL) 416 if (x->props.mode != XFRM_MODE_TUNNEL)
@@ -424,8 +418,8 @@ static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
424 else 418 else
425 net_adj = 0; 419 net_adj = 0;
426 420
427 return ((mtu - x->props.header_len - crypto_aead_authsize(esp->aead) - 421 return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
428 net_adj) & ~(align - 1)) + net_adj - 2; 422 net_adj) & ~(blksize - 1)) + net_adj - 2;
429} 423}
430 424
431static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 425static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
@@ -454,18 +448,16 @@ static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
454 448
455static void esp6_destroy(struct xfrm_state *x) 449static void esp6_destroy(struct xfrm_state *x)
456{ 450{
457 struct esp_data *esp = x->data; 451 struct crypto_aead *aead = x->data;
458 452
459 if (!esp) 453 if (!aead)
460 return; 454 return;
461 455
462 crypto_free_aead(esp->aead); 456 crypto_free_aead(aead);
463 kfree(esp);
464} 457}
465 458
466static int esp_init_aead(struct xfrm_state *x) 459static int esp_init_aead(struct xfrm_state *x)
467{ 460{
468 struct esp_data *esp = x->data;
469 struct crypto_aead *aead; 461 struct crypto_aead *aead;
470 int err; 462 int err;
471 463
@@ -474,7 +466,7 @@ static int esp_init_aead(struct xfrm_state *x)
474 if (IS_ERR(aead)) 466 if (IS_ERR(aead))
475 goto error; 467 goto error;
476 468
477 esp->aead = aead; 469 x->data = aead;
478 470
479 err = crypto_aead_setkey(aead, x->aead->alg_key, 471 err = crypto_aead_setkey(aead, x->aead->alg_key,
480 (x->aead->alg_key_len + 7) / 8); 472 (x->aead->alg_key_len + 7) / 8);
@@ -491,7 +483,6 @@ error:
491 483
492static int esp_init_authenc(struct xfrm_state *x) 484static int esp_init_authenc(struct xfrm_state *x)
493{ 485{
494 struct esp_data *esp = x->data;
495 struct crypto_aead *aead; 486 struct crypto_aead *aead;
496 struct crypto_authenc_key_param *param; 487 struct crypto_authenc_key_param *param;
497 struct rtattr *rta; 488 struct rtattr *rta;
@@ -526,7 +517,7 @@ static int esp_init_authenc(struct xfrm_state *x)
526 if (IS_ERR(aead)) 517 if (IS_ERR(aead))
527 goto error; 518 goto error;
528 519
529 esp->aead = aead; 520 x->data = aead;
530 521
531 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) + 522 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
532 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param)); 523 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
@@ -581,7 +572,6 @@ error:
581 572
582static int esp6_init_state(struct xfrm_state *x) 573static int esp6_init_state(struct xfrm_state *x)
583{ 574{
584 struct esp_data *esp;
585 struct crypto_aead *aead; 575 struct crypto_aead *aead;
586 u32 align; 576 u32 align;
587 int err; 577 int err;
@@ -589,11 +579,7 @@ static int esp6_init_state(struct xfrm_state *x)
589 if (x->encap) 579 if (x->encap)
590 return -EINVAL; 580 return -EINVAL;
591 581
592 esp = kzalloc(sizeof(*esp), GFP_KERNEL); 582 x->data = NULL;
593 if (esp == NULL)
594 return -ENOMEM;
595
596 x->data = esp;
597 583
598 if (x->aead) 584 if (x->aead)
599 err = esp_init_aead(x); 585 err = esp_init_aead(x);
@@ -603,9 +589,7 @@ static int esp6_init_state(struct xfrm_state *x)
603 if (err) 589 if (err)
604 goto error; 590 goto error;
605 591
606 aead = esp->aead; 592 aead = x->data;
607
608 esp->padlen = 0;
609 593
610 x->props.header_len = sizeof(struct ip_esp_hdr) + 594 x->props.header_len = sizeof(struct ip_esp_hdr) +
611 crypto_aead_ivsize(aead); 595 crypto_aead_ivsize(aead);
@@ -625,9 +609,7 @@ static int esp6_init_state(struct xfrm_state *x)
625 } 609 }
626 610
627 align = ALIGN(crypto_aead_blocksize(aead), 4); 611 align = ALIGN(crypto_aead_blocksize(aead), 4);
628 if (esp->padlen) 612 x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
629 align = max_t(u32, align, esp->padlen);
630 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
631 613
632error: 614error:
633 return err; 615 return err;
diff --git a/net/ipv6/fib6_rules.c b/net/ipv6/fib6_rules.c
index e27591635f92..3fd0a578329e 100644
--- a/net/ipv6/fib6_rules.c
+++ b/net/ipv6/fib6_rules.c
@@ -122,7 +122,11 @@ out:
122static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg) 122static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
123{ 123{
124 struct rt6_info *rt = (struct rt6_info *) arg->result; 124 struct rt6_info *rt = (struct rt6_info *) arg->result;
125 struct net_device *dev = rt->rt6i_idev->dev; 125 struct net_device *dev = NULL;
126
127 if (rt->rt6i_idev)
128 dev = rt->rt6i_idev->dev;
129
126 /* do not accept result if the route does 130 /* do not accept result if the route does
127 * not meet the required prefix length 131 * not meet the required prefix length
128 */ 132 */
diff --git a/net/ipv6/inet6_connection_sock.c b/net/ipv6/inet6_connection_sock.c
index e4311cbc8b4e..77bb8afb141d 100644
--- a/net/ipv6/inet6_connection_sock.c
+++ b/net/ipv6/inet6_connection_sock.c
@@ -70,20 +70,20 @@ struct dst_entry *inet6_csk_route_req(struct sock *sk,
70 struct flowi6 *fl6, 70 struct flowi6 *fl6,
71 const struct request_sock *req) 71 const struct request_sock *req)
72{ 72{
73 struct inet6_request_sock *treq = inet6_rsk(req); 73 struct inet_request_sock *ireq = inet_rsk(req);
74 struct ipv6_pinfo *np = inet6_sk(sk); 74 struct ipv6_pinfo *np = inet6_sk(sk);
75 struct in6_addr *final_p, final; 75 struct in6_addr *final_p, final;
76 struct dst_entry *dst; 76 struct dst_entry *dst;
77 77
78 memset(fl6, 0, sizeof(*fl6)); 78 memset(fl6, 0, sizeof(*fl6));
79 fl6->flowi6_proto = IPPROTO_TCP; 79 fl6->flowi6_proto = IPPROTO_TCP;
80 fl6->daddr = treq->rmt_addr; 80 fl6->daddr = ireq->ir_v6_rmt_addr;
81 final_p = fl6_update_dst(fl6, np->opt, &final); 81 final_p = fl6_update_dst(fl6, np->opt, &final);
82 fl6->saddr = treq->loc_addr; 82 fl6->saddr = ireq->ir_v6_loc_addr;
83 fl6->flowi6_oif = treq->iif; 83 fl6->flowi6_oif = ireq->ir_iif;
84 fl6->flowi6_mark = sk->sk_mark; 84 fl6->flowi6_mark = sk->sk_mark;
85 fl6->fl6_dport = inet_rsk(req)->rmt_port; 85 fl6->fl6_dport = ireq->ir_rmt_port;
86 fl6->fl6_sport = inet_rsk(req)->loc_port; 86 fl6->fl6_sport = htons(ireq->ir_num);
87 security_req_classify_flow(req, flowi6_to_flowi(fl6)); 87 security_req_classify_flow(req, flowi6_to_flowi(fl6));
88 88
89 dst = ip6_dst_lookup_flow(sk, fl6, final_p, false); 89 dst = ip6_dst_lookup_flow(sk, fl6, final_p, false);
@@ -129,13 +129,13 @@ struct request_sock *inet6_csk_search_req(const struct sock *sk,
129 lopt->nr_table_entries)]; 129 lopt->nr_table_entries)];
130 (req = *prev) != NULL; 130 (req = *prev) != NULL;
131 prev = &req->dl_next) { 131 prev = &req->dl_next) {
132 const struct inet6_request_sock *treq = inet6_rsk(req); 132 const struct inet_request_sock *ireq = inet_rsk(req);
133 133
134 if (inet_rsk(req)->rmt_port == rport && 134 if (ireq->ir_rmt_port == rport &&
135 req->rsk_ops->family == AF_INET6 && 135 req->rsk_ops->family == AF_INET6 &&
136 ipv6_addr_equal(&treq->rmt_addr, raddr) && 136 ipv6_addr_equal(&ireq->ir_v6_rmt_addr, raddr) &&
137 ipv6_addr_equal(&treq->loc_addr, laddr) && 137 ipv6_addr_equal(&ireq->ir_v6_loc_addr, laddr) &&
138 (!treq->iif || treq->iif == iif)) { 138 (!ireq->ir_iif || ireq->ir_iif == iif)) {
139 WARN_ON(req->sk != NULL); 139 WARN_ON(req->sk != NULL);
140 *prevp = prev; 140 *prevp = prev;
141 return req; 141 return req;
@@ -153,8 +153,8 @@ void inet6_csk_reqsk_queue_hash_add(struct sock *sk,
153{ 153{
154 struct inet_connection_sock *icsk = inet_csk(sk); 154 struct inet_connection_sock *icsk = inet_csk(sk);
155 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; 155 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
156 const u32 h = inet6_synq_hash(&inet6_rsk(req)->rmt_addr, 156 const u32 h = inet6_synq_hash(&inet_rsk(req)->ir_v6_rmt_addr,
157 inet_rsk(req)->rmt_port, 157 inet_rsk(req)->ir_rmt_port,
158 lopt->hash_rnd, lopt->nr_table_entries); 158 lopt->hash_rnd, lopt->nr_table_entries);
159 159
160 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout); 160 reqsk_queue_hash_req(&icsk->icsk_accept_queue, h, req, timeout);
@@ -165,11 +165,10 @@ EXPORT_SYMBOL_GPL(inet6_csk_reqsk_queue_hash_add);
165 165
166void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr) 166void inet6_csk_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
167{ 167{
168 struct ipv6_pinfo *np = inet6_sk(sk);
169 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr; 168 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
170 169
171 sin6->sin6_family = AF_INET6; 170 sin6->sin6_family = AF_INET6;
172 sin6->sin6_addr = np->daddr; 171 sin6->sin6_addr = sk->sk_v6_daddr;
173 sin6->sin6_port = inet_sk(sk)->inet_dport; 172 sin6->sin6_port = inet_sk(sk)->inet_dport;
174 /* We do not store received flowlabel for TCP */ 173 /* We do not store received flowlabel for TCP */
175 sin6->sin6_flowinfo = 0; 174 sin6->sin6_flowinfo = 0;
@@ -203,7 +202,7 @@ static struct dst_entry *inet6_csk_route_socket(struct sock *sk,
203 202
204 memset(fl6, 0, sizeof(*fl6)); 203 memset(fl6, 0, sizeof(*fl6));
205 fl6->flowi6_proto = sk->sk_protocol; 204 fl6->flowi6_proto = sk->sk_protocol;
206 fl6->daddr = np->daddr; 205 fl6->daddr = sk->sk_v6_daddr;
207 fl6->saddr = np->saddr; 206 fl6->saddr = np->saddr;
208 fl6->flowlabel = np->flow_label; 207 fl6->flowlabel = np->flow_label;
209 IP6_ECN_flow_xmit(sk, fl6->flowlabel); 208 IP6_ECN_flow_xmit(sk, fl6->flowlabel);
@@ -245,7 +244,7 @@ int inet6_csk_xmit(struct sk_buff *skb, struct flowi *fl_unused)
245 skb_dst_set_noref(skb, dst); 244 skb_dst_set_noref(skb, dst);
246 245
247 /* Restore final destination back after routing done */ 246 /* Restore final destination back after routing done */
248 fl6.daddr = np->daddr; 247 fl6.daddr = sk->sk_v6_daddr;
249 248
250 res = ip6_xmit(sk, skb, &fl6, np->opt, np->tclass); 249 res = ip6_xmit(sk, skb, &fl6, np->opt, np->tclass);
251 rcu_read_unlock(); 250 rcu_read_unlock();
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 066640e0ba8e..262e13c02ec2 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -23,6 +23,39 @@
23#include <net/secure_seq.h> 23#include <net/secure_seq.h>
24#include <net/ip.h> 24#include <net/ip.h>
25 25
26static unsigned int inet6_ehashfn(struct net *net,
27 const struct in6_addr *laddr,
28 const u16 lport,
29 const struct in6_addr *faddr,
30 const __be16 fport)
31{
32 static u32 inet6_ehash_secret __read_mostly;
33 static u32 ipv6_hash_secret __read_mostly;
34
35 u32 lhash, fhash;
36
37 net_get_random_once(&inet6_ehash_secret, sizeof(inet6_ehash_secret));
38 net_get_random_once(&ipv6_hash_secret, sizeof(ipv6_hash_secret));
39
40 lhash = (__force u32)laddr->s6_addr32[3];
41 fhash = __ipv6_addr_jhash(faddr, ipv6_hash_secret);
42
43 return __inet6_ehashfn(lhash, lport, fhash, fport,
44 inet6_ehash_secret + net_hash_mix(net));
45}
46
47static int inet6_sk_ehashfn(const struct sock *sk)
48{
49 const struct inet_sock *inet = inet_sk(sk);
50 const struct in6_addr *laddr = &sk->sk_v6_rcv_saddr;
51 const struct in6_addr *faddr = &sk->sk_v6_daddr;
52 const __u16 lport = inet->inet_num;
53 const __be16 fport = inet->inet_dport;
54 struct net *net = sock_net(sk);
55
56 return inet6_ehashfn(net, laddr, lport, faddr, fport);
57}
58
26int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw) 59int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
27{ 60{
28 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; 61 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
@@ -89,43 +122,22 @@ begin:
89 sk_nulls_for_each_rcu(sk, node, &head->chain) { 122 sk_nulls_for_each_rcu(sk, node, &head->chain) {
90 if (sk->sk_hash != hash) 123 if (sk->sk_hash != hash)
91 continue; 124 continue;
92 if (likely(INET6_MATCH(sk, net, saddr, daddr, ports, dif))) { 125 if (!INET6_MATCH(sk, net, saddr, daddr, ports, dif))
93 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
94 goto begintw;
95 if (unlikely(!INET6_MATCH(sk, net, saddr, daddr,
96 ports, dif))) {
97 sock_put(sk);
98 goto begin;
99 }
100 goto out;
101 }
102 }
103 if (get_nulls_value(node) != slot)
104 goto begin;
105
106begintw:
107 /* Must check for a TIME_WAIT'er before going to listener hash. */
108 sk_nulls_for_each_rcu(sk, node, &head->twchain) {
109 if (sk->sk_hash != hash)
110 continue; 126 continue;
111 if (likely(INET6_TW_MATCH(sk, net, saddr, daddr, 127 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt)))
112 ports, dif))) {
113 if (unlikely(!atomic_inc_not_zero(&sk->sk_refcnt))) {
114 sk = NULL;
115 goto out;
116 }
117 if (unlikely(!INET6_TW_MATCH(sk, net, saddr, daddr,
118 ports, dif))) {
119 inet_twsk_put(inet_twsk(sk));
120 goto begintw;
121 }
122 goto out; 128 goto out;
129
130 if (unlikely(!INET6_MATCH(sk, net, saddr, daddr, ports, dif))) {
131 sock_gen_put(sk);
132 goto begin;
123 } 133 }
134 goto found;
124 } 135 }
125 if (get_nulls_value(node) != slot) 136 if (get_nulls_value(node) != slot)
126 goto begintw; 137 goto begin;
127 sk = NULL;
128out: 138out:
139 sk = NULL;
140found:
129 rcu_read_unlock(); 141 rcu_read_unlock();
130 return sk; 142 return sk;
131} 143}
@@ -140,11 +152,10 @@ static inline int compute_score(struct sock *sk, struct net *net,
140 152
141 if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum && 153 if (net_eq(sock_net(sk), net) && inet_sk(sk)->inet_num == hnum &&
142 sk->sk_family == PF_INET6) { 154 sk->sk_family == PF_INET6) {
143 const struct ipv6_pinfo *np = inet6_sk(sk);
144 155
145 score = 1; 156 score = 1;
146 if (!ipv6_addr_any(&np->rcv_saddr)) { 157 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
147 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 158 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
148 return -1; 159 return -1;
149 score++; 160 score++;
150 } 161 }
@@ -236,9 +247,8 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
236{ 247{
237 struct inet_hashinfo *hinfo = death_row->hashinfo; 248 struct inet_hashinfo *hinfo = death_row->hashinfo;
238 struct inet_sock *inet = inet_sk(sk); 249 struct inet_sock *inet = inet_sk(sk);
239 const struct ipv6_pinfo *np = inet6_sk(sk); 250 const struct in6_addr *daddr = &sk->sk_v6_rcv_saddr;
240 const struct in6_addr *daddr = &np->rcv_saddr; 251 const struct in6_addr *saddr = &sk->sk_v6_daddr;
241 const struct in6_addr *saddr = &np->daddr;
242 const int dif = sk->sk_bound_dev_if; 252 const int dif = sk->sk_bound_dev_if;
243 const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport); 253 const __portpair ports = INET_COMBINED_PORTS(inet->inet_dport, lport);
244 struct net *net = sock_net(sk); 254 struct net *net = sock_net(sk);
@@ -248,38 +258,28 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
248 spinlock_t *lock = inet_ehash_lockp(hinfo, hash); 258 spinlock_t *lock = inet_ehash_lockp(hinfo, hash);
249 struct sock *sk2; 259 struct sock *sk2;
250 const struct hlist_nulls_node *node; 260 const struct hlist_nulls_node *node;
251 struct inet_timewait_sock *tw; 261 struct inet_timewait_sock *tw = NULL;
252 int twrefcnt = 0; 262 int twrefcnt = 0;
253 263
254 spin_lock(lock); 264 spin_lock(lock);
255 265
256 /* Check TIME-WAIT sockets first. */
257 sk_nulls_for_each(sk2, node, &head->twchain) {
258 if (sk2->sk_hash != hash)
259 continue;
260
261 if (likely(INET6_TW_MATCH(sk2, net, saddr, daddr,
262 ports, dif))) {
263 tw = inet_twsk(sk2);
264 if (twsk_unique(sk, sk2, twp))
265 goto unique;
266 else
267 goto not_unique;
268 }
269 }
270 tw = NULL;
271
272 /* And established part... */
273 sk_nulls_for_each(sk2, node, &head->chain) { 266 sk_nulls_for_each(sk2, node, &head->chain) {
274 if (sk2->sk_hash != hash) 267 if (sk2->sk_hash != hash)
275 continue; 268 continue;
276 if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports, dif))) 269
270 if (likely(INET6_MATCH(sk2, net, saddr, daddr, ports, dif))) {
271 if (sk2->sk_state == TCP_TIME_WAIT) {
272 tw = inet_twsk(sk2);
273 if (twsk_unique(sk, sk2, twp))
274 break;
275 }
277 goto not_unique; 276 goto not_unique;
277 }
278 } 278 }
279 279
280unique:
281 /* Must record num and sport now. Otherwise we will see 280 /* Must record num and sport now. Otherwise we will see
282 * in hash table socket with a funny identity. */ 281 * in hash table socket with a funny identity.
282 */
283 inet->inet_num = lport; 283 inet->inet_num = lport;
284 inet->inet_sport = htons(lport); 284 inet->inet_sport = htons(lport);
285 sk->sk_hash = hash; 285 sk->sk_hash = hash;
@@ -312,9 +312,9 @@ not_unique:
312static inline u32 inet6_sk_port_offset(const struct sock *sk) 312static inline u32 inet6_sk_port_offset(const struct sock *sk)
313{ 313{
314 const struct inet_sock *inet = inet_sk(sk); 314 const struct inet_sock *inet = inet_sk(sk);
315 const struct ipv6_pinfo *np = inet6_sk(sk); 315
316 return secure_ipv6_port_ephemeral(np->rcv_saddr.s6_addr32, 316 return secure_ipv6_port_ephemeral(sk->sk_v6_rcv_saddr.s6_addr32,
317 np->daddr.s6_addr32, 317 sk->sk_v6_daddr.s6_addr32,
318 inet->inet_dport); 318 inet->inet_dport);
319} 319}
320 320
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index 5bec666aba61..5550a8113a6d 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -1529,25 +1529,6 @@ static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1529 fib6_walk(&c.w); 1529 fib6_walk(&c.w);
1530} 1530}
1531 1531
1532void fib6_clean_all_ro(struct net *net, int (*func)(struct rt6_info *, void *arg),
1533 int prune, void *arg)
1534{
1535 struct fib6_table *table;
1536 struct hlist_head *head;
1537 unsigned int h;
1538
1539 rcu_read_lock();
1540 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1541 head = &net->ipv6.fib_table_hash[h];
1542 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1543 read_lock_bh(&table->tb6_lock);
1544 fib6_clean_tree(net, &table->tb6_root,
1545 func, prune, arg);
1546 read_unlock_bh(&table->tb6_lock);
1547 }
1548 }
1549 rcu_read_unlock();
1550}
1551void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg), 1532void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *arg),
1552 int prune, void *arg) 1533 int prune, void *arg)
1553{ 1534{
@@ -1782,3 +1763,189 @@ void fib6_gc_cleanup(void)
1782 unregister_pernet_subsys(&fib6_net_ops); 1763 unregister_pernet_subsys(&fib6_net_ops);
1783 kmem_cache_destroy(fib6_node_kmem); 1764 kmem_cache_destroy(fib6_node_kmem);
1784} 1765}
1766
1767#ifdef CONFIG_PROC_FS
1768
1769struct ipv6_route_iter {
1770 struct seq_net_private p;
1771 struct fib6_walker_t w;
1772 loff_t skip;
1773 struct fib6_table *tbl;
1774 __u32 sernum;
1775};
1776
1777static int ipv6_route_seq_show(struct seq_file *seq, void *v)
1778{
1779 struct rt6_info *rt = v;
1780 struct ipv6_route_iter *iter = seq->private;
1781
1782 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
1783
1784#ifdef CONFIG_IPV6_SUBTREES
1785 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
1786#else
1787 seq_puts(seq, "00000000000000000000000000000000 00 ");
1788#endif
1789 if (rt->rt6i_flags & RTF_GATEWAY)
1790 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
1791 else
1792 seq_puts(seq, "00000000000000000000000000000000");
1793
1794 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
1795 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
1796 rt->dst.__use, rt->rt6i_flags,
1797 rt->dst.dev ? rt->dst.dev->name : "");
1798 iter->w.leaf = NULL;
1799 return 0;
1800}
1801
1802static int ipv6_route_yield(struct fib6_walker_t *w)
1803{
1804 struct ipv6_route_iter *iter = w->args;
1805
1806 if (!iter->skip)
1807 return 1;
1808
1809 do {
1810 iter->w.leaf = iter->w.leaf->dst.rt6_next;
1811 iter->skip--;
1812 if (!iter->skip && iter->w.leaf)
1813 return 1;
1814 } while (iter->w.leaf);
1815
1816 return 0;
1817}
1818
1819static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter)
1820{
1821 memset(&iter->w, 0, sizeof(iter->w));
1822 iter->w.func = ipv6_route_yield;
1823 iter->w.root = &iter->tbl->tb6_root;
1824 iter->w.state = FWS_INIT;
1825 iter->w.node = iter->w.root;
1826 iter->w.args = iter;
1827 iter->sernum = iter->w.root->fn_sernum;
1828 INIT_LIST_HEAD(&iter->w.lh);
1829 fib6_walker_link(&iter->w);
1830}
1831
1832static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
1833 struct net *net)
1834{
1835 unsigned int h;
1836 struct hlist_node *node;
1837
1838 if (tbl) {
1839 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
1840 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
1841 } else {
1842 h = 0;
1843 node = NULL;
1844 }
1845
1846 while (!node && h < FIB6_TABLE_HASHSZ) {
1847 node = rcu_dereference_bh(
1848 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
1849 }
1850 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
1851}
1852
1853static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
1854{
1855 if (iter->sernum != iter->w.root->fn_sernum) {
1856 iter->sernum = iter->w.root->fn_sernum;
1857 iter->w.state = FWS_INIT;
1858 iter->w.node = iter->w.root;
1859 WARN_ON(iter->w.skip);
1860 iter->w.skip = iter->w.count;
1861 }
1862}
1863
1864static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1865{
1866 int r;
1867 struct rt6_info *n;
1868 struct net *net = seq_file_net(seq);
1869 struct ipv6_route_iter *iter = seq->private;
1870
1871 if (!v)
1872 goto iter_table;
1873
1874 n = ((struct rt6_info *)v)->dst.rt6_next;
1875 if (n) {
1876 ++*pos;
1877 return n;
1878 }
1879
1880iter_table:
1881 ipv6_route_check_sernum(iter);
1882 read_lock(&iter->tbl->tb6_lock);
1883 r = fib6_walk_continue(&iter->w);
1884 read_unlock(&iter->tbl->tb6_lock);
1885 if (r > 0) {
1886 if (v)
1887 ++*pos;
1888 return iter->w.leaf;
1889 } else if (r < 0) {
1890 fib6_walker_unlink(&iter->w);
1891 return NULL;
1892 }
1893 fib6_walker_unlink(&iter->w);
1894
1895 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
1896 if (!iter->tbl)
1897 return NULL;
1898
1899 ipv6_route_seq_setup_walk(iter);
1900 goto iter_table;
1901}
1902
1903static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
1904 __acquires(RCU_BH)
1905{
1906 struct net *net = seq_file_net(seq);
1907 struct ipv6_route_iter *iter = seq->private;
1908
1909 rcu_read_lock_bh();
1910 iter->tbl = ipv6_route_seq_next_table(NULL, net);
1911 iter->skip = *pos;
1912
1913 if (iter->tbl) {
1914 ipv6_route_seq_setup_walk(iter);
1915 return ipv6_route_seq_next(seq, NULL, pos);
1916 } else {
1917 return NULL;
1918 }
1919}
1920
1921static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
1922{
1923 struct fib6_walker_t *w = &iter->w;
1924 return w->node && !(w->state == FWS_U && w->node == w->root);
1925}
1926
1927static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
1928 __releases(RCU_BH)
1929{
1930 struct ipv6_route_iter *iter = seq->private;
1931
1932 if (ipv6_route_iter_active(iter))
1933 fib6_walker_unlink(&iter->w);
1934
1935 rcu_read_unlock_bh();
1936}
1937
1938static const struct seq_operations ipv6_route_seq_ops = {
1939 .start = ipv6_route_seq_start,
1940 .next = ipv6_route_seq_next,
1941 .stop = ipv6_route_seq_stop,
1942 .show = ipv6_route_seq_show
1943};
1944
1945int ipv6_route_open(struct inode *inode, struct file *file)
1946{
1947 return seq_open_net(inode, file, &ipv6_route_seq_ops,
1948 sizeof(struct ipv6_route_iter));
1949}
1950
1951#endif /* CONFIG_PROC_FS */
diff --git a/net/ipv6/ip6_flowlabel.c b/net/ipv6/ip6_flowlabel.c
index 46e88433ec7d..e7fb7106550f 100644
--- a/net/ipv6/ip6_flowlabel.c
+++ b/net/ipv6/ip6_flowlabel.c
@@ -41,7 +41,7 @@
41#define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified 41#define FL_MIN_LINGER 6 /* Minimal linger. It is set to 6sec specified
42 in old IPv6 RFC. Well, it was reasonable value. 42 in old IPv6 RFC. Well, it was reasonable value.
43 */ 43 */
44#define FL_MAX_LINGER 60 /* Maximal linger timeout */ 44#define FL_MAX_LINGER 150 /* Maximal linger timeout */
45 45
46/* FL hash table */ 46/* FL hash table */
47 47
@@ -345,6 +345,8 @@ static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned lo
345 expires = check_linger(expires); 345 expires = check_linger(expires);
346 if (!expires) 346 if (!expires)
347 return -EPERM; 347 return -EPERM;
348
349 spin_lock_bh(&ip6_fl_lock);
348 fl->lastuse = jiffies; 350 fl->lastuse = jiffies;
349 if (time_before(fl->linger, linger)) 351 if (time_before(fl->linger, linger))
350 fl->linger = linger; 352 fl->linger = linger;
@@ -352,6 +354,8 @@ static int fl6_renew(struct ip6_flowlabel *fl, unsigned long linger, unsigned lo
352 expires = fl->linger; 354 expires = fl->linger;
353 if (time_before(fl->expires, fl->lastuse + expires)) 355 if (time_before(fl->expires, fl->lastuse + expires))
354 fl->expires = fl->lastuse + expires; 356 fl->expires = fl->lastuse + expires;
357 spin_unlock_bh(&ip6_fl_lock);
358
355 return 0; 359 return 0;
356} 360}
357 361
@@ -453,8 +457,10 @@ static int mem_check(struct sock *sk)
453 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK) 457 if (room > FL_MAX_SIZE - FL_MAX_PER_SOCK)
454 return 0; 458 return 0;
455 459
460 rcu_read_lock_bh();
456 for_each_sk_fl_rcu(np, sfl) 461 for_each_sk_fl_rcu(np, sfl)
457 count++; 462 count++;
463 rcu_read_unlock_bh();
458 464
459 if (room <= 0 || 465 if (room <= 0 ||
460 ((count >= FL_MAX_PER_SOCK || 466 ((count >= FL_MAX_PER_SOCK ||
@@ -465,34 +471,6 @@ static int mem_check(struct sock *sk)
465 return 0; 471 return 0;
466} 472}
467 473
468static bool ipv6_hdr_cmp(struct ipv6_opt_hdr *h1, struct ipv6_opt_hdr *h2)
469{
470 if (h1 == h2)
471 return false;
472 if (h1 == NULL || h2 == NULL)
473 return true;
474 if (h1->hdrlen != h2->hdrlen)
475 return true;
476 return memcmp(h1+1, h2+1, ((h1->hdrlen+1)<<3) - sizeof(*h1));
477}
478
479static bool ipv6_opt_cmp(struct ipv6_txoptions *o1, struct ipv6_txoptions *o2)
480{
481 if (o1 == o2)
482 return false;
483 if (o1 == NULL || o2 == NULL)
484 return true;
485 if (o1->opt_nflen != o2->opt_nflen)
486 return true;
487 if (ipv6_hdr_cmp(o1->hopopt, o2->hopopt))
488 return true;
489 if (ipv6_hdr_cmp(o1->dst0opt, o2->dst0opt))
490 return true;
491 if (ipv6_hdr_cmp((struct ipv6_opt_hdr *)o1->srcrt, (struct ipv6_opt_hdr *)o2->srcrt))
492 return true;
493 return false;
494}
495
496static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl, 474static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
497 struct ip6_flowlabel *fl) 475 struct ip6_flowlabel *fl)
498{ 476{
@@ -503,6 +481,32 @@ static inline void fl_link(struct ipv6_pinfo *np, struct ipv6_fl_socklist *sfl,
503 spin_unlock_bh(&ip6_sk_fl_lock); 481 spin_unlock_bh(&ip6_sk_fl_lock);
504} 482}
505 483
484int ipv6_flowlabel_opt_get(struct sock *sk, struct in6_flowlabel_req *freq)
485{
486 struct ipv6_pinfo *np = inet6_sk(sk);
487 struct ipv6_fl_socklist *sfl;
488
489 rcu_read_lock_bh();
490
491 for_each_sk_fl_rcu(np, sfl) {
492 if (sfl->fl->label == (np->flow_label & IPV6_FLOWLABEL_MASK)) {
493 spin_lock_bh(&ip6_fl_lock);
494 freq->flr_label = sfl->fl->label;
495 freq->flr_dst = sfl->fl->dst;
496 freq->flr_share = sfl->fl->share;
497 freq->flr_expires = (sfl->fl->expires - jiffies) / HZ;
498 freq->flr_linger = sfl->fl->linger / HZ;
499
500 spin_unlock_bh(&ip6_fl_lock);
501 rcu_read_unlock_bh();
502 return 0;
503 }
504 }
505 rcu_read_unlock_bh();
506
507 return -ENOENT;
508}
509
506int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen) 510int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
507{ 511{
508 int uninitialized_var(err); 512 int uninitialized_var(err);
@@ -603,11 +607,6 @@ recheck:
603 uid_eq(fl1->owner.uid, fl->owner.uid))) 607 uid_eq(fl1->owner.uid, fl->owner.uid)))
604 goto release; 608 goto release;
605 609
606 err = -EINVAL;
607 if (!ipv6_addr_equal(&fl1->dst, &fl->dst) ||
608 ipv6_opt_cmp(fl1->opt, fl->opt))
609 goto release;
610
611 err = -ENOMEM; 610 err = -ENOMEM;
612 if (sfl1 == NULL) 611 if (sfl1 == NULL)
613 goto release; 612 goto release;
diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c
index bf4a9a084de5..8acb28621f9c 100644
--- a/net/ipv6/ip6_gre.c
+++ b/net/ipv6/ip6_gre.c
@@ -1252,6 +1252,7 @@ static void ip6gre_tunnel_setup(struct net_device *dev)
1252static int ip6gre_tunnel_init(struct net_device *dev) 1252static int ip6gre_tunnel_init(struct net_device *dev)
1253{ 1253{
1254 struct ip6_tnl *tunnel; 1254 struct ip6_tnl *tunnel;
1255 int i;
1255 1256
1256 tunnel = netdev_priv(dev); 1257 tunnel = netdev_priv(dev);
1257 1258
@@ -1269,6 +1270,13 @@ static int ip6gre_tunnel_init(struct net_device *dev)
1269 if (!dev->tstats) 1270 if (!dev->tstats)
1270 return -ENOMEM; 1271 return -ENOMEM;
1271 1272
1273 for_each_possible_cpu(i) {
1274 struct pcpu_tstats *ip6gre_tunnel_stats;
1275 ip6gre_tunnel_stats = per_cpu_ptr(dev->tstats, i);
1276 u64_stats_init(&ip6gre_tunnel_stats->syncp);
1277 }
1278
1279
1272 return 0; 1280 return 0;
1273} 1281}
1274 1282
@@ -1449,6 +1457,7 @@ static void ip6gre_netlink_parms(struct nlattr *data[],
1449static int ip6gre_tap_init(struct net_device *dev) 1457static int ip6gre_tap_init(struct net_device *dev)
1450{ 1458{
1451 struct ip6_tnl *tunnel; 1459 struct ip6_tnl *tunnel;
1460 int i;
1452 1461
1453 tunnel = netdev_priv(dev); 1462 tunnel = netdev_priv(dev);
1454 1463
@@ -1462,6 +1471,12 @@ static int ip6gre_tap_init(struct net_device *dev)
1462 if (!dev->tstats) 1471 if (!dev->tstats)
1463 return -ENOMEM; 1472 return -ENOMEM;
1464 1473
1474 for_each_possible_cpu(i) {
1475 struct pcpu_tstats *ip6gre_tap_stats;
1476 ip6gre_tap_stats = per_cpu_ptr(dev->tstats, i);
1477 u64_stats_init(&ip6gre_tap_stats->syncp);
1478 }
1479
1465 return 0; 1480 return 0;
1466} 1481}
1467 1482
diff --git a/net/ipv6/ip6_offload.c b/net/ipv6/ip6_offload.c
index d82de7228100..4b851692b1f6 100644
--- a/net/ipv6/ip6_offload.c
+++ b/net/ipv6/ip6_offload.c
@@ -66,7 +66,6 @@ static int ipv6_gso_send_check(struct sk_buff *skb)
66 __skb_pull(skb, sizeof(*ipv6h)); 66 __skb_pull(skb, sizeof(*ipv6h));
67 err = -EPROTONOSUPPORT; 67 err = -EPROTONOSUPPORT;
68 68
69 rcu_read_lock();
70 ops = rcu_dereference(inet6_offloads[ 69 ops = rcu_dereference(inet6_offloads[
71 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]); 70 ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr)]);
72 71
@@ -74,7 +73,6 @@ static int ipv6_gso_send_check(struct sk_buff *skb)
74 skb_reset_transport_header(skb); 73 skb_reset_transport_header(skb);
75 err = ops->callbacks.gso_send_check(skb); 74 err = ops->callbacks.gso_send_check(skb);
76 } 75 }
77 rcu_read_unlock();
78 76
79out: 77out:
80 return err; 78 return err;
@@ -92,46 +90,58 @@ static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
92 u8 *prevhdr; 90 u8 *prevhdr;
93 int offset = 0; 91 int offset = 0;
94 bool tunnel; 92 bool tunnel;
93 int nhoff;
95 94
96 if (unlikely(skb_shinfo(skb)->gso_type & 95 if (unlikely(skb_shinfo(skb)->gso_type &
97 ~(SKB_GSO_UDP | 96 ~(SKB_GSO_UDP |
98 SKB_GSO_DODGY | 97 SKB_GSO_DODGY |
99 SKB_GSO_TCP_ECN | 98 SKB_GSO_TCP_ECN |
100 SKB_GSO_GRE | 99 SKB_GSO_GRE |
100 SKB_GSO_IPIP |
101 SKB_GSO_SIT |
101 SKB_GSO_UDP_TUNNEL | 102 SKB_GSO_UDP_TUNNEL |
102 SKB_GSO_MPLS | 103 SKB_GSO_MPLS |
103 SKB_GSO_TCPV6 | 104 SKB_GSO_TCPV6 |
104 0))) 105 0)))
105 goto out; 106 goto out;
106 107
108 skb_reset_network_header(skb);
109 nhoff = skb_network_header(skb) - skb_mac_header(skb);
107 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h)))) 110 if (unlikely(!pskb_may_pull(skb, sizeof(*ipv6h))))
108 goto out; 111 goto out;
109 112
110 tunnel = skb->encapsulation; 113 tunnel = SKB_GSO_CB(skb)->encap_level > 0;
114 if (tunnel)
115 features = skb->dev->hw_enc_features & netif_skb_features(skb);
116 SKB_GSO_CB(skb)->encap_level += sizeof(*ipv6h);
117
111 ipv6h = ipv6_hdr(skb); 118 ipv6h = ipv6_hdr(skb);
112 __skb_pull(skb, sizeof(*ipv6h)); 119 __skb_pull(skb, sizeof(*ipv6h));
113 segs = ERR_PTR(-EPROTONOSUPPORT); 120 segs = ERR_PTR(-EPROTONOSUPPORT);
114 121
115 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr); 122 proto = ipv6_gso_pull_exthdrs(skb, ipv6h->nexthdr);
116 rcu_read_lock(); 123
117 ops = rcu_dereference(inet6_offloads[proto]); 124 ops = rcu_dereference(inet6_offloads[proto]);
118 if (likely(ops && ops->callbacks.gso_segment)) { 125 if (likely(ops && ops->callbacks.gso_segment)) {
119 skb_reset_transport_header(skb); 126 skb_reset_transport_header(skb);
120 segs = ops->callbacks.gso_segment(skb, features); 127 segs = ops->callbacks.gso_segment(skb, features);
121 } 128 }
122 rcu_read_unlock();
123 129
124 if (IS_ERR(segs)) 130 if (IS_ERR(segs))
125 goto out; 131 goto out;
126 132
127 for (skb = segs; skb; skb = skb->next) { 133 for (skb = segs; skb; skb = skb->next) {
128 ipv6h = ipv6_hdr(skb); 134 ipv6h = (struct ipv6hdr *)(skb_mac_header(skb) + nhoff);
129 ipv6h->payload_len = htons(skb->len - skb->mac_len - 135 ipv6h->payload_len = htons(skb->len - nhoff - sizeof(*ipv6h));
130 sizeof(*ipv6h)); 136 if (tunnel) {
137 skb_reset_inner_headers(skb);
138 skb->encapsulation = 1;
139 }
140 skb->network_header = (u8 *)ipv6h - skb->head;
141
131 if (!tunnel && proto == IPPROTO_UDP) { 142 if (!tunnel && proto == IPPROTO_UDP) {
132 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 143 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
133 fptr = (struct frag_hdr *)(skb_network_header(skb) + 144 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
134 unfrag_ip6hlen);
135 fptr->frag_off = htons(offset); 145 fptr->frag_off = htons(offset);
136 if (skb->next != NULL) 146 if (skb->next != NULL)
137 fptr->frag_off |= htons(IP6_MF); 147 fptr->frag_off |= htons(IP6_MF);
@@ -267,6 +277,13 @@ static struct packet_offload ipv6_packet_offload __read_mostly = {
267 }, 277 },
268}; 278};
269 279
280static const struct net_offload sit_offload = {
281 .callbacks = {
282 .gso_send_check = ipv6_gso_send_check,
283 .gso_segment = ipv6_gso_segment,
284 },
285};
286
270static int __init ipv6_offload_init(void) 287static int __init ipv6_offload_init(void)
271{ 288{
272 289
@@ -278,6 +295,9 @@ static int __init ipv6_offload_init(void)
278 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__); 295 pr_crit("%s: Cannot add EXTHDRS protocol offload\n", __func__);
279 296
280 dev_add_offload(&ipv6_packet_offload); 297 dev_add_offload(&ipv6_packet_offload);
298
299 inet_add_offload(&sit_offload, IPPROTO_IPV6);
300
281 return 0; 301 return 0;
282} 302}
283 303
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 91fb4e8212f5..e6f931997996 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -116,8 +116,8 @@ static int ip6_finish_output2(struct sk_buff *skb)
116 } 116 }
117 rcu_read_unlock_bh(); 117 rcu_read_unlock_bh();
118 118
119 IP6_INC_STATS_BH(dev_net(dst->dev), 119 IP6_INC_STATS(dev_net(dst->dev),
120 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); 120 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
121 kfree_skb(skb); 121 kfree_skb(skb);
122 return -EINVAL; 122 return -EINVAL;
123} 123}
@@ -125,7 +125,8 @@ static int ip6_finish_output2(struct sk_buff *skb)
125static int ip6_finish_output(struct sk_buff *skb) 125static int ip6_finish_output(struct sk_buff *skb)
126{ 126{
127 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) || 127 if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
128 dst_allfrag(skb_dst(skb))) 128 dst_allfrag(skb_dst(skb)) ||
129 (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
129 return ip6_fragment(skb, ip6_finish_output2); 130 return ip6_fragment(skb, ip6_finish_output2);
130 else 131 else
131 return ip6_finish_output2(skb); 132 return ip6_finish_output2(skb);
@@ -909,7 +910,7 @@ static int ip6_dst_lookup_tail(struct sock *sk,
909 910
910out_err_release: 911out_err_release:
911 if (err == -ENETUNREACH) 912 if (err == -ENETUNREACH)
912 IP6_INC_STATS_BH(net, NULL, IPSTATS_MIB_OUTNOROUTES); 913 IP6_INC_STATS(net, NULL, IPSTATS_MIB_OUTNOROUTES);
913 dst_release(*dst); 914 dst_release(*dst);
914 *dst = NULL; 915 *dst = NULL;
915 return err; 916 return err;
@@ -1192,11 +1193,35 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
1192 1193
1193 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len + 1194 fragheaderlen = sizeof(struct ipv6hdr) + rt->rt6i_nfheader_len +
1194 (opt ? opt->opt_nflen : 0); 1195 (opt ? opt->opt_nflen : 0);
1195 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen - sizeof(struct frag_hdr); 1196 maxfraglen = ((mtu - fragheaderlen) & ~7) + fragheaderlen -
1197 sizeof(struct frag_hdr);
1196 1198
1197 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) { 1199 if (mtu <= sizeof(struct ipv6hdr) + IPV6_MAXPLEN) {
1198 if (cork->length + length > sizeof(struct ipv6hdr) + IPV6_MAXPLEN - fragheaderlen) { 1200 unsigned int maxnonfragsize, headersize;
1199 ipv6_local_error(sk, EMSGSIZE, fl6, mtu-exthdrlen); 1201
1202 headersize = sizeof(struct ipv6hdr) +
1203 (opt ? opt->tot_len : 0) +
1204 (dst_allfrag(&rt->dst) ?
1205 sizeof(struct frag_hdr) : 0) +
1206 rt->rt6i_nfheader_len;
1207
1208 maxnonfragsize = (np->pmtudisc >= IPV6_PMTUDISC_DO) ?
1209 mtu : sizeof(struct ipv6hdr) + IPV6_MAXPLEN;
1210
1211 /* dontfrag active */
1212 if ((cork->length + length > mtu - headersize) && dontfrag &&
1213 (sk->sk_protocol == IPPROTO_UDP ||
1214 sk->sk_protocol == IPPROTO_RAW)) {
1215 ipv6_local_rxpmtu(sk, fl6, mtu - headersize +
1216 sizeof(struct ipv6hdr));
1217 goto emsgsize;
1218 }
1219
1220 if (cork->length + length > maxnonfragsize - headersize) {
1221emsgsize:
1222 ipv6_local_error(sk, EMSGSIZE, fl6,
1223 mtu - headersize +
1224 sizeof(struct ipv6hdr));
1200 return -EMSGSIZE; 1225 return -EMSGSIZE;
1201 } 1226 }
1202 } 1227 }
@@ -1221,12 +1246,6 @@ int ip6_append_data(struct sock *sk, int getfrag(void *from, char *to,
1221 * --yoshfuji 1246 * --yoshfuji
1222 */ 1247 */
1223 1248
1224 if ((length > mtu) && dontfrag && (sk->sk_protocol == IPPROTO_UDP ||
1225 sk->sk_protocol == IPPROTO_RAW)) {
1226 ipv6_local_rxpmtu(sk, fl6, mtu-exthdrlen);
1227 return -EMSGSIZE;
1228 }
1229
1230 skb = skb_peek_tail(&sk->sk_write_queue); 1249 skb = skb_peek_tail(&sk->sk_write_queue);
1231 cork->length += length; 1250 cork->length += length;
1232 if (((length > mtu) || 1251 if (((length > mtu) ||
diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c
index 583b77e2f69b..7881965a8248 100644
--- a/net/ipv6/ip6_tunnel.c
+++ b/net/ipv6/ip6_tunnel.c
@@ -103,16 +103,25 @@ struct ip6_tnl_net {
103 103
104static struct net_device_stats *ip6_get_stats(struct net_device *dev) 104static struct net_device_stats *ip6_get_stats(struct net_device *dev)
105{ 105{
106 struct pcpu_tstats sum = { 0 }; 106 struct pcpu_tstats tmp, sum = { 0 };
107 int i; 107 int i;
108 108
109 for_each_possible_cpu(i) { 109 for_each_possible_cpu(i) {
110 unsigned int start;
110 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i); 111 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
111 112
112 sum.rx_packets += tstats->rx_packets; 113 do {
113 sum.rx_bytes += tstats->rx_bytes; 114 start = u64_stats_fetch_begin_bh(&tstats->syncp);
114 sum.tx_packets += tstats->tx_packets; 115 tmp.rx_packets = tstats->rx_packets;
115 sum.tx_bytes += tstats->tx_bytes; 116 tmp.rx_bytes = tstats->rx_bytes;
117 tmp.tx_packets = tstats->tx_packets;
118 tmp.tx_bytes = tstats->tx_bytes;
119 } while (u64_stats_fetch_retry_bh(&tstats->syncp, start));
120
121 sum.rx_packets += tmp.rx_packets;
122 sum.rx_bytes += tmp.rx_bytes;
123 sum.tx_packets += tmp.tx_packets;
124 sum.tx_bytes += tmp.tx_bytes;
116 } 125 }
117 dev->stats.rx_packets = sum.rx_packets; 126 dev->stats.rx_packets = sum.rx_packets;
118 dev->stats.rx_bytes = sum.rx_bytes; 127 dev->stats.rx_bytes = sum.rx_bytes;
@@ -824,8 +833,10 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
824 } 833 }
825 834
826 tstats = this_cpu_ptr(t->dev->tstats); 835 tstats = this_cpu_ptr(t->dev->tstats);
836 u64_stats_update_begin(&tstats->syncp);
827 tstats->rx_packets++; 837 tstats->rx_packets++;
828 tstats->rx_bytes += skb->len; 838 tstats->rx_bytes += skb->len;
839 u64_stats_update_end(&tstats->syncp);
829 840
830 netif_rx(skb); 841 netif_rx(skb);
831 842
@@ -1494,12 +1505,19 @@ static inline int
1494ip6_tnl_dev_init_gen(struct net_device *dev) 1505ip6_tnl_dev_init_gen(struct net_device *dev)
1495{ 1506{
1496 struct ip6_tnl *t = netdev_priv(dev); 1507 struct ip6_tnl *t = netdev_priv(dev);
1508 int i;
1497 1509
1498 t->dev = dev; 1510 t->dev = dev;
1499 t->net = dev_net(dev); 1511 t->net = dev_net(dev);
1500 dev->tstats = alloc_percpu(struct pcpu_tstats); 1512 dev->tstats = alloc_percpu(struct pcpu_tstats);
1501 if (!dev->tstats) 1513 if (!dev->tstats)
1502 return -ENOMEM; 1514 return -ENOMEM;
1515
1516 for_each_possible_cpu(i) {
1517 struct pcpu_tstats *ip6_tnl_stats;
1518 ip6_tnl_stats = per_cpu_ptr(dev->tstats, i);
1519 u64_stats_init(&ip6_tnl_stats->syncp);
1520 }
1503 return 0; 1521 return 0;
1504} 1522}
1505 1523
@@ -1635,6 +1653,15 @@ static int ip6_tnl_changelink(struct net_device *dev, struct nlattr *tb[],
1635 return ip6_tnl_update(t, &p); 1653 return ip6_tnl_update(t, &p);
1636} 1654}
1637 1655
1656static void ip6_tnl_dellink(struct net_device *dev, struct list_head *head)
1657{
1658 struct net *net = dev_net(dev);
1659 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1660
1661 if (dev != ip6n->fb_tnl_dev)
1662 unregister_netdevice_queue(dev, head);
1663}
1664
1638static size_t ip6_tnl_get_size(const struct net_device *dev) 1665static size_t ip6_tnl_get_size(const struct net_device *dev)
1639{ 1666{
1640 return 1667 return
@@ -1699,6 +1726,7 @@ static struct rtnl_link_ops ip6_link_ops __read_mostly = {
1699 .validate = ip6_tnl_validate, 1726 .validate = ip6_tnl_validate,
1700 .newlink = ip6_tnl_newlink, 1727 .newlink = ip6_tnl_newlink,
1701 .changelink = ip6_tnl_changelink, 1728 .changelink = ip6_tnl_changelink,
1729 .dellink = ip6_tnl_dellink,
1702 .get_size = ip6_tnl_get_size, 1730 .get_size = ip6_tnl_get_size,
1703 .fill_info = ip6_tnl_fill_info, 1731 .fill_info = ip6_tnl_fill_info,
1704}; 1732};
@@ -1715,9 +1743,9 @@ static struct xfrm6_tunnel ip6ip6_handler __read_mostly = {
1715 .priority = 1, 1743 .priority = 1,
1716}; 1744};
1717 1745
1718static void __net_exit ip6_tnl_destroy_tunnels(struct ip6_tnl_net *ip6n) 1746static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1719{ 1747{
1720 struct net *net = dev_net(ip6n->fb_tnl_dev); 1748 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1721 struct net_device *dev, *aux; 1749 struct net_device *dev, *aux;
1722 int h; 1750 int h;
1723 struct ip6_tnl *t; 1751 struct ip6_tnl *t;
@@ -1785,10 +1813,8 @@ err_alloc_dev:
1785 1813
1786static void __net_exit ip6_tnl_exit_net(struct net *net) 1814static void __net_exit ip6_tnl_exit_net(struct net *net)
1787{ 1815{
1788 struct ip6_tnl_net *ip6n = net_generic(net, ip6_tnl_net_id);
1789
1790 rtnl_lock(); 1816 rtnl_lock();
1791 ip6_tnl_destroy_tunnels(ip6n); 1817 ip6_tnl_destroy_tunnels(net);
1792 rtnl_unlock(); 1818 rtnl_unlock();
1793} 1819}
1794 1820
diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c
new file mode 100644
index 000000000000..7b42d5ef868d
--- /dev/null
+++ b/net/ipv6/ip6_vti.c
@@ -0,0 +1,1044 @@
1/*
2 * IPv6 virtual tunneling interface
3 *
4 * Copyright (C) 2013 secunet Security Networks AG
5 *
6 * Author:
7 * Steffen Klassert <steffen.klassert@secunet.com>
8 *
9 * Based on:
10 * net/ipv6/ip6_tunnel.c
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
16 */
17
18#include <linux/module.h>
19#include <linux/capability.h>
20#include <linux/errno.h>
21#include <linux/types.h>
22#include <linux/sockios.h>
23#include <linux/icmp.h>
24#include <linux/if.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/if_tunnel.h>
28#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/icmpv6.h>
33#include <linux/init.h>
34#include <linux/route.h>
35#include <linux/rtnetlink.h>
36#include <linux/netfilter_ipv6.h>
37#include <linux/slab.h>
38#include <linux/hash.h>
39
40#include <linux/uaccess.h>
41#include <linux/atomic.h>
42
43#include <net/icmp.h>
44#include <net/ip.h>
45#include <net/ip_tunnels.h>
46#include <net/ipv6.h>
47#include <net/ip6_route.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
50#include <net/xfrm.h>
51#include <net/net_namespace.h>
52#include <net/netns/generic.h>
53
54#define HASH_SIZE_SHIFT 5
55#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
56
57static u32 HASH(const struct in6_addr *addr1, const struct in6_addr *addr2)
58{
59 u32 hash = ipv6_addr_hash(addr1) ^ ipv6_addr_hash(addr2);
60
61 return hash_32(hash, HASH_SIZE_SHIFT);
62}
63
64static int vti6_dev_init(struct net_device *dev);
65static void vti6_dev_setup(struct net_device *dev);
66static struct rtnl_link_ops vti6_link_ops __read_mostly;
67
68static int vti6_net_id __read_mostly;
69struct vti6_net {
70 /* the vti6 tunnel fallback device */
71 struct net_device *fb_tnl_dev;
72 /* lists for storing tunnels in use */
73 struct ip6_tnl __rcu *tnls_r_l[HASH_SIZE];
74 struct ip6_tnl __rcu *tnls_wc[1];
75 struct ip6_tnl __rcu **tnls[2];
76};
77
78#define for_each_vti6_tunnel_rcu(start) \
79 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
80
81/**
82 * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
83 * @net: network namespace
84 * @remote: the address of the tunnel exit-point
85 * @local: the address of the tunnel entry-point
86 *
87 * Return:
88 * tunnel matching given end-points if found,
89 * else fallback tunnel if its device is up,
90 * else %NULL
91 **/
92static struct ip6_tnl *
93vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
94 const struct in6_addr *local)
95{
96 unsigned int hash = HASH(remote, local);
97 struct ip6_tnl *t;
98 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
99
100 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
101 if (ipv6_addr_equal(local, &t->parms.laddr) &&
102 ipv6_addr_equal(remote, &t->parms.raddr) &&
103 (t->dev->flags & IFF_UP))
104 return t;
105 }
106 t = rcu_dereference(ip6n->tnls_wc[0]);
107 if (t && (t->dev->flags & IFF_UP))
108 return t;
109
110 return NULL;
111}
112
113/**
114 * vti6_tnl_bucket - get head of list matching given tunnel parameters
115 * @p: parameters containing tunnel end-points
116 *
117 * Description:
118 * vti6_tnl_bucket() returns the head of the list matching the
119 * &struct in6_addr entries laddr and raddr in @p.
120 *
121 * Return: head of IPv6 tunnel list
122 **/
123static struct ip6_tnl __rcu **
124vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
125{
126 const struct in6_addr *remote = &p->raddr;
127 const struct in6_addr *local = &p->laddr;
128 unsigned int h = 0;
129 int prio = 0;
130
131 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
132 prio = 1;
133 h = HASH(remote, local);
134 }
135 return &ip6n->tnls[prio][h];
136}
137
138static void
139vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
140{
141 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
142
143 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
144 rcu_assign_pointer(*tp, t);
145}
146
147static void
148vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
149{
150 struct ip6_tnl __rcu **tp;
151 struct ip6_tnl *iter;
152
153 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
154 (iter = rtnl_dereference(*tp)) != NULL;
155 tp = &iter->next) {
156 if (t == iter) {
157 rcu_assign_pointer(*tp, t->next);
158 break;
159 }
160 }
161}
162
163static void vti6_dev_free(struct net_device *dev)
164{
165 free_percpu(dev->tstats);
166 free_netdev(dev);
167}
168
169static int vti6_tnl_create2(struct net_device *dev)
170{
171 struct ip6_tnl *t = netdev_priv(dev);
172 struct net *net = dev_net(dev);
173 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
174 int err;
175
176 err = vti6_dev_init(dev);
177 if (err < 0)
178 goto out;
179
180 err = register_netdevice(dev);
181 if (err < 0)
182 goto out;
183
184 strcpy(t->parms.name, dev->name);
185 dev->rtnl_link_ops = &vti6_link_ops;
186
187 dev_hold(dev);
188 vti6_tnl_link(ip6n, t);
189
190 return 0;
191
192out:
193 return err;
194}
195
196static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
197{
198 struct net_device *dev;
199 struct ip6_tnl *t;
200 char name[IFNAMSIZ];
201 int err;
202
203 if (p->name[0])
204 strlcpy(name, p->name, IFNAMSIZ);
205 else
206 sprintf(name, "ip6_vti%%d");
207
208 dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
209 if (dev == NULL)
210 goto failed;
211
212 dev_net_set(dev, net);
213
214 t = netdev_priv(dev);
215 t->parms = *p;
216 t->net = dev_net(dev);
217
218 err = vti6_tnl_create2(dev);
219 if (err < 0)
220 goto failed_free;
221
222 return t;
223
224failed_free:
225 vti6_dev_free(dev);
226failed:
227 return NULL;
228}
229
230/**
231 * vti6_locate - find or create tunnel matching given parameters
232 * @net: network namespace
233 * @p: tunnel parameters
234 * @create: != 0 if allowed to create new tunnel if no match found
235 *
236 * Description:
237 * vti6_locate() first tries to locate an existing tunnel
238 * based on @parms. If this is unsuccessful, but @create is set a new
239 * tunnel device is created and registered for use.
240 *
241 * Return:
242 * matching tunnel or NULL
243 **/
244static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
245 int create)
246{
247 const struct in6_addr *remote = &p->raddr;
248 const struct in6_addr *local = &p->laddr;
249 struct ip6_tnl __rcu **tp;
250 struct ip6_tnl *t;
251 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
252
253 for (tp = vti6_tnl_bucket(ip6n, p);
254 (t = rtnl_dereference(*tp)) != NULL;
255 tp = &t->next) {
256 if (ipv6_addr_equal(local, &t->parms.laddr) &&
257 ipv6_addr_equal(remote, &t->parms.raddr))
258 return t;
259 }
260 if (!create)
261 return NULL;
262 return vti6_tnl_create(net, p);
263}
264
265/**
266 * vti6_dev_uninit - tunnel device uninitializer
267 * @dev: the device to be destroyed
268 *
269 * Description:
270 * vti6_dev_uninit() removes tunnel from its list
271 **/
272static void vti6_dev_uninit(struct net_device *dev)
273{
274 struct ip6_tnl *t = netdev_priv(dev);
275 struct net *net = dev_net(dev);
276 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
277
278 if (dev == ip6n->fb_tnl_dev)
279 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
280 else
281 vti6_tnl_unlink(ip6n, t);
282 ip6_tnl_dst_reset(t);
283 dev_put(dev);
284}
285
286static int vti6_rcv(struct sk_buff *skb)
287{
288 struct ip6_tnl *t;
289 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
290
291 rcu_read_lock();
292
293 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
294 &ipv6h->daddr)) != NULL) {
295 struct pcpu_tstats *tstats;
296
297 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
298 rcu_read_unlock();
299 goto discard;
300 }
301
302 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
303 rcu_read_unlock();
304 return 0;
305 }
306
307 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
308 t->dev->stats.rx_dropped++;
309 rcu_read_unlock();
310 goto discard;
311 }
312
313 tstats = this_cpu_ptr(t->dev->tstats);
314 u64_stats_update_begin(&tstats->syncp);
315 tstats->rx_packets++;
316 tstats->rx_bytes += skb->len;
317 u64_stats_update_end(&tstats->syncp);
318
319 skb->mark = 0;
320 secpath_reset(skb);
321 skb->dev = t->dev;
322
323 rcu_read_unlock();
324 return 0;
325 }
326 rcu_read_unlock();
327 return 1;
328
329discard:
330 kfree_skb(skb);
331 return 0;
332}
333
334/**
335 * vti6_addr_conflict - compare packet addresses to tunnel's own
336 * @t: the outgoing tunnel device
337 * @hdr: IPv6 header from the incoming packet
338 *
339 * Description:
340 * Avoid trivial tunneling loop by checking that tunnel exit-point
341 * doesn't match source of incoming packet.
342 *
343 * Return:
344 * 1 if conflict,
345 * 0 else
346 **/
347static inline bool
348vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
349{
350 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
351}
352
353/**
354 * vti6_xmit - send a packet
355 * @skb: the outgoing socket buffer
356 * @dev: the outgoing tunnel device
357 **/
358static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
359{
360 struct net *net = dev_net(dev);
361 struct ip6_tnl *t = netdev_priv(dev);
362 struct net_device_stats *stats = &t->dev->stats;
363 struct dst_entry *dst = NULL, *ndst = NULL;
364 struct flowi6 fl6;
365 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
366 struct net_device *tdev;
367 int err = -1;
368
369 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
370 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
371 return err;
372
373 dst = ip6_tnl_dst_check(t);
374 if (!dst) {
375 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
376
377 ndst = ip6_route_output(net, NULL, &fl6);
378
379 if (ndst->error)
380 goto tx_err_link_failure;
381 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
382 if (IS_ERR(ndst)) {
383 err = PTR_ERR(ndst);
384 ndst = NULL;
385 goto tx_err_link_failure;
386 }
387 dst = ndst;
388 }
389
390 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
391 goto tx_err_link_failure;
392
393 tdev = dst->dev;
394
395 if (tdev == dev) {
396 stats->collisions++;
397 net_warn_ratelimited("%s: Local routing loop detected!\n",
398 t->parms.name);
399 goto tx_err_dst_release;
400 }
401
402
403 skb_dst_drop(skb);
404 skb_dst_set_noref(skb, dst);
405
406 ip6tunnel_xmit(skb, dev);
407 if (ndst) {
408 dev->mtu = dst_mtu(ndst);
409 ip6_tnl_dst_store(t, ndst);
410 }
411
412 return 0;
413tx_err_link_failure:
414 stats->tx_carrier_errors++;
415 dst_link_failure(skb);
416tx_err_dst_release:
417 dst_release(ndst);
418 return err;
419}
420
421static netdev_tx_t
422vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
423{
424 struct ip6_tnl *t = netdev_priv(dev);
425 struct net_device_stats *stats = &t->dev->stats;
426 int ret;
427
428 switch (skb->protocol) {
429 case htons(ETH_P_IPV6):
430 ret = vti6_xmit(skb, dev);
431 break;
432 default:
433 goto tx_err;
434 }
435
436 if (ret < 0)
437 goto tx_err;
438
439 return NETDEV_TX_OK;
440
441tx_err:
442 stats->tx_errors++;
443 stats->tx_dropped++;
444 kfree_skb(skb);
445 return NETDEV_TX_OK;
446}
447
448static void vti6_link_config(struct ip6_tnl *t)
449{
450 struct dst_entry *dst;
451 struct net_device *dev = t->dev;
452 struct __ip6_tnl_parm *p = &t->parms;
453 struct flowi6 *fl6 = &t->fl.u.ip6;
454
455 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
456 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
457
458 /* Set up flowi template */
459 fl6->saddr = p->laddr;
460 fl6->daddr = p->raddr;
461 fl6->flowi6_oif = p->link;
462 fl6->flowi6_mark = be32_to_cpu(p->i_key);
463 fl6->flowi6_proto = p->proto;
464 fl6->flowlabel = 0;
465
466 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
467 IP6_TNL_F_CAP_PER_PACKET);
468 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
469
470 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
471 dev->flags |= IFF_POINTOPOINT;
472 else
473 dev->flags &= ~IFF_POINTOPOINT;
474
475 dev->iflink = p->link;
476
477 if (p->flags & IP6_TNL_F_CAP_XMIT) {
478
479 dst = ip6_route_output(dev_net(dev), NULL, fl6);
480 if (dst->error)
481 return;
482
483 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
484 NULL, 0);
485 if (IS_ERR(dst))
486 return;
487
488 if (dst->dev) {
489 dev->hard_header_len = dst->dev->hard_header_len;
490
491 dev->mtu = dst_mtu(dst);
492
493 if (dev->mtu < IPV6_MIN_MTU)
494 dev->mtu = IPV6_MIN_MTU;
495 }
496 dst_release(dst);
497 }
498}
499
500/**
501 * vti6_tnl_change - update the tunnel parameters
502 * @t: tunnel to be changed
503 * @p: tunnel configuration parameters
504 *
505 * Description:
506 * vti6_tnl_change() updates the tunnel parameters
507 **/
508static int
509vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
510{
511 t->parms.laddr = p->laddr;
512 t->parms.raddr = p->raddr;
513 t->parms.link = p->link;
514 t->parms.i_key = p->i_key;
515 t->parms.o_key = p->o_key;
516 t->parms.proto = p->proto;
517 ip6_tnl_dst_reset(t);
518 vti6_link_config(t);
519 return 0;
520}
521
522static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
523{
524 struct net *net = dev_net(t->dev);
525 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
526 int err;
527
528 vti6_tnl_unlink(ip6n, t);
529 synchronize_net();
530 err = vti6_tnl_change(t, p);
531 vti6_tnl_link(ip6n, t);
532 netdev_state_change(t->dev);
533 return err;
534}
535
536static void
537vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
538{
539 p->laddr = u->laddr;
540 p->raddr = u->raddr;
541 p->link = u->link;
542 p->i_key = u->i_key;
543 p->o_key = u->o_key;
544 p->proto = u->proto;
545
546 memcpy(p->name, u->name, sizeof(u->name));
547}
548
549static void
550vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
551{
552 u->laddr = p->laddr;
553 u->raddr = p->raddr;
554 u->link = p->link;
555 u->i_key = p->i_key;
556 u->o_key = p->o_key;
557 u->proto = p->proto;
558
559 memcpy(u->name, p->name, sizeof(u->name));
560}
561
562/**
563 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
564 * @dev: virtual device associated with tunnel
565 * @ifr: parameters passed from userspace
566 * @cmd: command to be performed
567 *
568 * Description:
569 * vti6_ioctl() is used for managing vti6 tunnels
570 * from userspace.
571 *
572 * The possible commands are the following:
573 * %SIOCGETTUNNEL: get tunnel parameters for device
574 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
575 * %SIOCCHGTUNNEL: change tunnel parameters to those given
576 * %SIOCDELTUNNEL: delete tunnel
577 *
578 * The fallback device "ip6_vti0", created during module
579 * initialization, can be used for creating other tunnel devices.
580 *
581 * Return:
582 * 0 on success,
583 * %-EFAULT if unable to copy data to or from userspace,
584 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
585 * %-EINVAL if passed tunnel parameters are invalid,
586 * %-EEXIST if changing a tunnel's parameters would cause a conflict
587 * %-ENODEV if attempting to change or delete a nonexisting device
588 **/
589static int
590vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
591{
592 int err = 0;
593 struct ip6_tnl_parm2 p;
594 struct __ip6_tnl_parm p1;
595 struct ip6_tnl *t = NULL;
596 struct net *net = dev_net(dev);
597 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
598
599 switch (cmd) {
600 case SIOCGETTUNNEL:
601 if (dev == ip6n->fb_tnl_dev) {
602 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
603 err = -EFAULT;
604 break;
605 }
606 vti6_parm_from_user(&p1, &p);
607 t = vti6_locate(net, &p1, 0);
608 } else {
609 memset(&p, 0, sizeof(p));
610 }
611 if (t == NULL)
612 t = netdev_priv(dev);
613 vti6_parm_to_user(&p, &t->parms);
614 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
615 err = -EFAULT;
616 break;
617 case SIOCADDTUNNEL:
618 case SIOCCHGTUNNEL:
619 err = -EPERM;
620 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
621 break;
622 err = -EFAULT;
623 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
624 break;
625 err = -EINVAL;
626 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
627 break;
628 vti6_parm_from_user(&p1, &p);
629 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
630 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
631 if (t != NULL) {
632 if (t->dev != dev) {
633 err = -EEXIST;
634 break;
635 }
636 } else
637 t = netdev_priv(dev);
638
639 err = vti6_update(t, &p1);
640 }
641 if (t) {
642 err = 0;
643 vti6_parm_to_user(&p, &t->parms);
644 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
645 err = -EFAULT;
646
647 } else
648 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
649 break;
650 case SIOCDELTUNNEL:
651 err = -EPERM;
652 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
653 break;
654
655 if (dev == ip6n->fb_tnl_dev) {
656 err = -EFAULT;
657 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
658 break;
659 err = -ENOENT;
660 vti6_parm_from_user(&p1, &p);
661 t = vti6_locate(net, &p1, 0);
662 if (t == NULL)
663 break;
664 err = -EPERM;
665 if (t->dev == ip6n->fb_tnl_dev)
666 break;
667 dev = t->dev;
668 }
669 err = 0;
670 unregister_netdevice(dev);
671 break;
672 default:
673 err = -EINVAL;
674 }
675 return err;
676}
677
678/**
679 * vti6_tnl_change_mtu - change mtu manually for tunnel device
680 * @dev: virtual device associated with tunnel
681 * @new_mtu: the new mtu
682 *
683 * Return:
684 * 0 on success,
685 * %-EINVAL if mtu too small
686 **/
687static int vti6_change_mtu(struct net_device *dev, int new_mtu)
688{
689 if (new_mtu < IPV6_MIN_MTU)
690 return -EINVAL;
691
692 dev->mtu = new_mtu;
693 return 0;
694}
695
696static const struct net_device_ops vti6_netdev_ops = {
697 .ndo_uninit = vti6_dev_uninit,
698 .ndo_start_xmit = vti6_tnl_xmit,
699 .ndo_do_ioctl = vti6_ioctl,
700 .ndo_change_mtu = vti6_change_mtu,
701 .ndo_get_stats64 = ip_tunnel_get_stats64,
702};
703
704/**
705 * vti6_dev_setup - setup virtual tunnel device
706 * @dev: virtual device associated with tunnel
707 *
708 * Description:
709 * Initialize function pointers and device parameters
710 **/
711static void vti6_dev_setup(struct net_device *dev)
712{
713 struct ip6_tnl *t;
714
715 dev->netdev_ops = &vti6_netdev_ops;
716 dev->destructor = vti6_dev_free;
717
718 dev->type = ARPHRD_TUNNEL6;
719 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
720 dev->mtu = ETH_DATA_LEN;
721 t = netdev_priv(dev);
722 dev->flags |= IFF_NOARP;
723 dev->addr_len = sizeof(struct in6_addr);
724 dev->features |= NETIF_F_NETNS_LOCAL;
725 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
726}
727
728/**
729 * vti6_dev_init_gen - general initializer for all tunnel devices
730 * @dev: virtual device associated with tunnel
731 **/
732static inline int vti6_dev_init_gen(struct net_device *dev)
733{
734 struct ip6_tnl *t = netdev_priv(dev);
735 int i;
736
737 t->dev = dev;
738 t->net = dev_net(dev);
739 dev->tstats = alloc_percpu(struct pcpu_tstats);
740 if (!dev->tstats)
741 return -ENOMEM;
742 for_each_possible_cpu(i) {
743 struct pcpu_tstats *stats;
744 stats = per_cpu_ptr(dev->tstats, i);
745 u64_stats_init(&stats->syncp);
746 }
747 return 0;
748}
749
750/**
751 * vti6_dev_init - initializer for all non fallback tunnel devices
752 * @dev: virtual device associated with tunnel
753 **/
754static int vti6_dev_init(struct net_device *dev)
755{
756 struct ip6_tnl *t = netdev_priv(dev);
757 int err = vti6_dev_init_gen(dev);
758
759 if (err)
760 return err;
761 vti6_link_config(t);
762 return 0;
763}
764
765/**
766 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
767 * @dev: fallback device
768 *
769 * Return: 0
770 **/
771static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
772{
773 struct ip6_tnl *t = netdev_priv(dev);
774 struct net *net = dev_net(dev);
775 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
776 int err = vti6_dev_init_gen(dev);
777
778 if (err)
779 return err;
780
781 t->parms.proto = IPPROTO_IPV6;
782 dev_hold(dev);
783
784 vti6_link_config(t);
785
786 rcu_assign_pointer(ip6n->tnls_wc[0], t);
787 return 0;
788}
789
790static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
791{
792 return 0;
793}
794
795static void vti6_netlink_parms(struct nlattr *data[],
796 struct __ip6_tnl_parm *parms)
797{
798 memset(parms, 0, sizeof(*parms));
799
800 if (!data)
801 return;
802
803 if (data[IFLA_VTI_LINK])
804 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
805
806 if (data[IFLA_VTI_LOCAL])
807 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
808 sizeof(struct in6_addr));
809
810 if (data[IFLA_VTI_REMOTE])
811 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
812 sizeof(struct in6_addr));
813
814 if (data[IFLA_VTI_IKEY])
815 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
816
817 if (data[IFLA_VTI_OKEY])
818 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
819}
820
821static int vti6_newlink(struct net *src_net, struct net_device *dev,
822 struct nlattr *tb[], struct nlattr *data[])
823{
824 struct net *net = dev_net(dev);
825 struct ip6_tnl *nt;
826
827 nt = netdev_priv(dev);
828 vti6_netlink_parms(data, &nt->parms);
829
830 nt->parms.proto = IPPROTO_IPV6;
831
832 if (vti6_locate(net, &nt->parms, 0))
833 return -EEXIST;
834
835 return vti6_tnl_create2(dev);
836}
837
838static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
839 struct nlattr *data[])
840{
841 struct ip6_tnl *t;
842 struct __ip6_tnl_parm p;
843 struct net *net = dev_net(dev);
844 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
845
846 if (dev == ip6n->fb_tnl_dev)
847 return -EINVAL;
848
849 vti6_netlink_parms(data, &p);
850
851 t = vti6_locate(net, &p, 0);
852
853 if (t) {
854 if (t->dev != dev)
855 return -EEXIST;
856 } else
857 t = netdev_priv(dev);
858
859 return vti6_update(t, &p);
860}
861
862static size_t vti6_get_size(const struct net_device *dev)
863{
864 return
865 /* IFLA_VTI_LINK */
866 nla_total_size(4) +
867 /* IFLA_VTI_LOCAL */
868 nla_total_size(sizeof(struct in6_addr)) +
869 /* IFLA_VTI_REMOTE */
870 nla_total_size(sizeof(struct in6_addr)) +
871 /* IFLA_VTI_IKEY */
872 nla_total_size(4) +
873 /* IFLA_VTI_OKEY */
874 nla_total_size(4) +
875 0;
876}
877
878static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
879{
880 struct ip6_tnl *tunnel = netdev_priv(dev);
881 struct __ip6_tnl_parm *parm = &tunnel->parms;
882
883 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
884 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
885 &parm->laddr) ||
886 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
887 &parm->raddr) ||
888 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
889 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
890 goto nla_put_failure;
891 return 0;
892
893nla_put_failure:
894 return -EMSGSIZE;
895}
896
897static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
898 [IFLA_VTI_LINK] = { .type = NLA_U32 },
899 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
900 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
901 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
902 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
903};
904
905static struct rtnl_link_ops vti6_link_ops __read_mostly = {
906 .kind = "vti6",
907 .maxtype = IFLA_VTI_MAX,
908 .policy = vti6_policy,
909 .priv_size = sizeof(struct ip6_tnl),
910 .setup = vti6_dev_setup,
911 .validate = vti6_validate,
912 .newlink = vti6_newlink,
913 .changelink = vti6_changelink,
914 .get_size = vti6_get_size,
915 .fill_info = vti6_fill_info,
916};
917
918static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
919 .handler = vti6_rcv,
920 .priority = 1,
921};
922
923static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
924{
925 int h;
926 struct ip6_tnl *t;
927 LIST_HEAD(list);
928
929 for (h = 0; h < HASH_SIZE; h++) {
930 t = rtnl_dereference(ip6n->tnls_r_l[h]);
931 while (t != NULL) {
932 unregister_netdevice_queue(t->dev, &list);
933 t = rtnl_dereference(t->next);
934 }
935 }
936
937 t = rtnl_dereference(ip6n->tnls_wc[0]);
938 unregister_netdevice_queue(t->dev, &list);
939 unregister_netdevice_many(&list);
940}
941
942static int __net_init vti6_init_net(struct net *net)
943{
944 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
945 struct ip6_tnl *t = NULL;
946 int err;
947
948 ip6n->tnls[0] = ip6n->tnls_wc;
949 ip6n->tnls[1] = ip6n->tnls_r_l;
950
951 err = -ENOMEM;
952 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
953 vti6_dev_setup);
954
955 if (!ip6n->fb_tnl_dev)
956 goto err_alloc_dev;
957 dev_net_set(ip6n->fb_tnl_dev, net);
958
959 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
960 if (err < 0)
961 goto err_register;
962
963 err = register_netdev(ip6n->fb_tnl_dev);
964 if (err < 0)
965 goto err_register;
966
967 t = netdev_priv(ip6n->fb_tnl_dev);
968
969 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
970 return 0;
971
972err_register:
973 vti6_dev_free(ip6n->fb_tnl_dev);
974err_alloc_dev:
975 return err;
976}
977
978static void __net_exit vti6_exit_net(struct net *net)
979{
980 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
981
982 rtnl_lock();
983 vti6_destroy_tunnels(ip6n);
984 rtnl_unlock();
985}
986
987static struct pernet_operations vti6_net_ops = {
988 .init = vti6_init_net,
989 .exit = vti6_exit_net,
990 .id = &vti6_net_id,
991 .size = sizeof(struct vti6_net),
992};
993
994/**
995 * vti6_tunnel_init - register protocol and reserve needed resources
996 *
997 * Return: 0 on success
998 **/
999static int __init vti6_tunnel_init(void)
1000{
1001 int err;
1002
1003 err = register_pernet_device(&vti6_net_ops);
1004 if (err < 0)
1005 goto out_pernet;
1006
1007 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1008 if (err < 0) {
1009 pr_err("%s: can't register vti6\n", __func__);
1010 goto out;
1011 }
1012 err = rtnl_link_register(&vti6_link_ops);
1013 if (err < 0)
1014 goto rtnl_link_failed;
1015
1016 return 0;
1017
1018rtnl_link_failed:
1019 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1020out:
1021 unregister_pernet_device(&vti6_net_ops);
1022out_pernet:
1023 return err;
1024}
1025
1026/**
1027 * vti6_tunnel_cleanup - free resources and unregister protocol
1028 **/
1029static void __exit vti6_tunnel_cleanup(void)
1030{
1031 rtnl_link_unregister(&vti6_link_ops);
1032 if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1033 pr_info("%s: can't deregister vti6\n", __func__);
1034
1035 unregister_pernet_device(&vti6_net_ops);
1036}
1037
1038module_init(vti6_tunnel_init);
1039module_exit(vti6_tunnel_cleanup);
1040MODULE_LICENSE("GPL");
1041MODULE_ALIAS_RTNL_LINK("vti6");
1042MODULE_ALIAS_NETDEV("ip6_vti0");
1043MODULE_AUTHOR("Steffen Klassert");
1044MODULE_DESCRIPTION("IPv6 virtual tunnel interface");
diff --git a/net/ipv6/ip6mr.c b/net/ipv6/ip6mr.c
index f365310bfcca..0eb4038a4d63 100644
--- a/net/ipv6/ip6mr.c
+++ b/net/ipv6/ip6mr.c
@@ -141,9 +141,12 @@ static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
141static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6, 141static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
142 struct mr6_table **mrt) 142 struct mr6_table **mrt)
143{ 143{
144 struct ip6mr_result res;
145 struct fib_lookup_arg arg = { .result = &res, };
146 int err; 144 int err;
145 struct ip6mr_result res;
146 struct fib_lookup_arg arg = {
147 .result = &res,
148 .flags = FIB_LOOKUP_NOREF,
149 };
147 150
148 err = fib_rules_lookup(net->ipv6.mr6_rules_ops, 151 err = fib_rules_lookup(net->ipv6.mr6_rules_ops,
149 flowi6_to_flowi(flp6), 0, &arg); 152 flowi6_to_flowi(flp6), 0, &arg);
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index d1e2e8ef29c5..1c6ce3119ff8 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -174,7 +174,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
174 } 174 }
175 175
176 if (ipv6_only_sock(sk) || 176 if (ipv6_only_sock(sk) ||
177 !ipv6_addr_v4mapped(&np->daddr)) { 177 !ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
178 retv = -EADDRNOTAVAIL; 178 retv = -EADDRNOTAVAIL;
179 break; 179 break;
180 } 180 }
@@ -1011,7 +1011,7 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1011 struct in6_pktinfo src_info; 1011 struct in6_pktinfo src_info;
1012 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1012 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1013 np->sticky_pktinfo.ipi6_ifindex; 1013 np->sticky_pktinfo.ipi6_ifindex;
1014 src_info.ipi6_addr = np->mcast_oif ? np->daddr : np->sticky_pktinfo.ipi6_addr; 1014 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr : np->sticky_pktinfo.ipi6_addr;
1015 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info); 1015 put_cmsg(&msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
1016 } 1016 }
1017 if (np->rxopt.bits.rxhlim) { 1017 if (np->rxopt.bits.rxhlim) {
@@ -1026,7 +1026,8 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1026 struct in6_pktinfo src_info; 1026 struct in6_pktinfo src_info;
1027 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif : 1027 src_info.ipi6_ifindex = np->mcast_oif ? np->mcast_oif :
1028 np->sticky_pktinfo.ipi6_ifindex; 1028 np->sticky_pktinfo.ipi6_ifindex;
1029 src_info.ipi6_addr = np->mcast_oif ? np->daddr : np->sticky_pktinfo.ipi6_addr; 1029 src_info.ipi6_addr = np->mcast_oif ? sk->sk_v6_daddr :
1030 np->sticky_pktinfo.ipi6_addr;
1030 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info); 1031 put_cmsg(&msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
1031 } 1032 }
1032 if (np->rxopt.bits.rxohlim) { 1033 if (np->rxopt.bits.rxohlim) {
@@ -1211,6 +1212,34 @@ static int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
1211 val = np->sndflow; 1212 val = np->sndflow;
1212 break; 1213 break;
1213 1214
1215 case IPV6_FLOWLABEL_MGR:
1216 {
1217 struct in6_flowlabel_req freq;
1218
1219 if (len < sizeof(freq))
1220 return -EINVAL;
1221
1222 if (copy_from_user(&freq, optval, sizeof(freq)))
1223 return -EFAULT;
1224
1225 if (freq.flr_action != IPV6_FL_A_GET)
1226 return -EINVAL;
1227
1228 len = sizeof(freq);
1229 memset(&freq, 0, sizeof(freq));
1230
1231 val = ipv6_flowlabel_opt_get(sk, &freq);
1232 if (val < 0)
1233 return val;
1234
1235 if (put_user(len, optlen))
1236 return -EFAULT;
1237 if (copy_to_user(optval, &freq, len))
1238 return -EFAULT;
1239
1240 return 0;
1241 }
1242
1214 case IPV6_ADDR_PREFERENCES: 1243 case IPV6_ADDR_PREFERENCES:
1215 val = 0; 1244 val = 0;
1216 1245
diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c
index f8a55ff1971b..300865171394 100644
--- a/net/ipv6/ndisc.c
+++ b/net/ipv6/ndisc.c
@@ -1277,6 +1277,9 @@ skip_linkparms:
1277 ri->prefix_len == 0) 1277 ri->prefix_len == 0)
1278 continue; 1278 continue;
1279#endif 1279#endif
1280 if (ri->prefix_len == 0 &&
1281 !in6_dev->cnf.accept_ra_defrtr)
1282 continue;
1280 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen) 1283 if (ri->prefix_len > in6_dev->cnf.accept_ra_rt_info_max_plen)
1281 continue; 1284 continue;
1282 rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3, 1285 rt6_route_rcv(skb->dev, (u8*)p, (p->nd_opt_len) << 3,
@@ -1726,8 +1729,8 @@ int __init ndisc_init(void)
1726 &ndisc_ifinfo_sysctl_change); 1729 &ndisc_ifinfo_sysctl_change);
1727 if (err) 1730 if (err)
1728 goto out_unregister_pernet; 1731 goto out_unregister_pernet;
1729#endif
1730out: 1732out:
1733#endif
1731 return err; 1734 return err;
1732 1735
1733#ifdef CONFIG_SYSCTL 1736#ifdef CONFIG_SYSCTL
diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig
index a7f842b29b67..7702f9e90a04 100644
--- a/net/ipv6/netfilter/Kconfig
+++ b/net/ipv6/netfilter/Kconfig
@@ -25,6 +25,19 @@ config NF_CONNTRACK_IPV6
25 25
26 To compile it as a module, choose M here. If unsure, say N. 26 To compile it as a module, choose M here. If unsure, say N.
27 27
28config NF_TABLES_IPV6
29 depends on NF_TABLES
30 tristate "IPv6 nf_tables support"
31
32config NFT_CHAIN_ROUTE_IPV6
33 depends on NF_TABLES_IPV6
34 tristate "IPv6 nf_tables route chain support"
35
36config NFT_CHAIN_NAT_IPV6
37 depends on NF_TABLES_IPV6
38 depends on NF_NAT_IPV6 && NFT_NAT
39 tristate "IPv6 nf_tables nat chain support"
40
28config IP6_NF_IPTABLES 41config IP6_NF_IPTABLES
29 tristate "IP6 tables support (required for filtering)" 42 tristate "IP6 tables support (required for filtering)"
30 depends on INET && IPV6 43 depends on INET && IPV6
diff --git a/net/ipv6/netfilter/Makefile b/net/ipv6/netfilter/Makefile
index 2b53738f798c..d1b4928f34f7 100644
--- a/net/ipv6/netfilter/Makefile
+++ b/net/ipv6/netfilter/Makefile
@@ -23,6 +23,11 @@ obj-$(CONFIG_NF_NAT_IPV6) += nf_nat_ipv6.o
23nf_defrag_ipv6-y := nf_defrag_ipv6_hooks.o nf_conntrack_reasm.o 23nf_defrag_ipv6-y := nf_defrag_ipv6_hooks.o nf_conntrack_reasm.o
24obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o 24obj-$(CONFIG_NF_DEFRAG_IPV6) += nf_defrag_ipv6.o
25 25
26# nf_tables
27obj-$(CONFIG_NF_TABLES_IPV6) += nf_tables_ipv6.o
28obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o
29obj-$(CONFIG_NFT_CHAIN_NAT_IPV6) += nft_chain_nat_ipv6.o
30
26# matches 31# matches
27obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o 32obj-$(CONFIG_IP6_NF_MATCH_AH) += ip6t_ah.o
28obj-$(CONFIG_IP6_NF_MATCH_EUI64) += ip6t_eui64.o 33obj-$(CONFIG_IP6_NF_MATCH_EUI64) += ip6t_eui64.o
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index 44400c216dc6..710238f58aa9 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -349,6 +349,11 @@ ip6t_do_table(struct sk_buff *skb,
349 local_bh_disable(); 349 local_bh_disable();
350 addend = xt_write_recseq_begin(); 350 addend = xt_write_recseq_begin();
351 private = table->private; 351 private = table->private;
352 /*
353 * Ensure we load private-> members after we've fetched the base
354 * pointer.
355 */
356 smp_read_barrier_depends();
352 cpu = smp_processor_id(); 357 cpu = smp_processor_id();
353 table_base = private->entries[cpu]; 358 table_base = private->entries[cpu];
354 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu]; 359 jumpstack = (struct ip6t_entry **)private->jumpstack[cpu];
diff --git a/net/ipv6/netfilter/ip6t_REJECT.c b/net/ipv6/netfilter/ip6t_REJECT.c
index 56eef30ee5f6..da00a2ecde55 100644
--- a/net/ipv6/netfilter/ip6t_REJECT.c
+++ b/net/ipv6/netfilter/ip6t_REJECT.c
@@ -39,7 +39,7 @@ MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv6");
39MODULE_LICENSE("GPL"); 39MODULE_LICENSE("GPL");
40 40
41/* Send RST reply */ 41/* Send RST reply */
42static void send_reset(struct net *net, struct sk_buff *oldskb) 42static void send_reset(struct net *net, struct sk_buff *oldskb, int hook)
43{ 43{
44 struct sk_buff *nskb; 44 struct sk_buff *nskb;
45 struct tcphdr otcph, *tcph; 45 struct tcphdr otcph, *tcph;
@@ -88,8 +88,7 @@ static void send_reset(struct net *net, struct sk_buff *oldskb)
88 } 88 }
89 89
90 /* Check checksum. */ 90 /* Check checksum. */
91 if (csum_ipv6_magic(&oip6h->saddr, &oip6h->daddr, otcplen, IPPROTO_TCP, 91 if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
92 skb_checksum(oldskb, tcphoff, otcplen, 0))) {
93 pr_debug("TCP checksum is invalid\n"); 92 pr_debug("TCP checksum is invalid\n");
94 return; 93 return;
95 } 94 }
@@ -227,7 +226,7 @@ reject_tg6(struct sk_buff *skb, const struct xt_action_param *par)
227 /* Do nothing */ 226 /* Do nothing */
228 break; 227 break;
229 case IP6T_TCP_RESET: 228 case IP6T_TCP_RESET:
230 send_reset(net, skb); 229 send_reset(net, skb, par->hooknum);
231 break; 230 break;
232 default: 231 default:
233 net_info_ratelimited("case %u not handled yet\n", reject->with); 232 net_info_ratelimited("case %u not handled yet\n", reject->with);
diff --git a/net/ipv6/netfilter/ip6t_SYNPROXY.c b/net/ipv6/netfilter/ip6t_SYNPROXY.c
index 2748b042da72..a0d17270117c 100644
--- a/net/ipv6/netfilter/ip6t_SYNPROXY.c
+++ b/net/ipv6/netfilter/ip6t_SYNPROXY.c
@@ -259,6 +259,7 @@ synproxy_recv_client_ack(const struct synproxy_net *snet,
259 259
260 this_cpu_inc(snet->stats->cookie_valid); 260 this_cpu_inc(snet->stats->cookie_valid);
261 opts->mss = mss; 261 opts->mss = mss;
262 opts->options |= XT_SYNPROXY_OPT_MSS;
262 263
263 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP) 264 if (opts->options & XT_SYNPROXY_OPT_TIMESTAMP)
264 synproxy_check_timestamp_cookie(opts); 265 synproxy_check_timestamp_cookie(opts);
@@ -312,7 +313,7 @@ synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
312 return XT_CONTINUE; 313 return XT_CONTINUE;
313} 314}
314 315
315static unsigned int ipv6_synproxy_hook(unsigned int hooknum, 316static unsigned int ipv6_synproxy_hook(const struct nf_hook_ops *ops,
316 struct sk_buff *skb, 317 struct sk_buff *skb,
317 const struct net_device *in, 318 const struct net_device *in,
318 const struct net_device *out, 319 const struct net_device *out,
@@ -445,6 +446,7 @@ static void synproxy_tg6_destroy(const struct xt_tgdtor_param *par)
445static struct xt_target synproxy_tg6_reg __read_mostly = { 446static struct xt_target synproxy_tg6_reg __read_mostly = {
446 .name = "SYNPROXY", 447 .name = "SYNPROXY",
447 .family = NFPROTO_IPV6, 448 .family = NFPROTO_IPV6,
449 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
448 .target = synproxy_tg6, 450 .target = synproxy_tg6,
449 .targetsize = sizeof(struct xt_synproxy_info), 451 .targetsize = sizeof(struct xt_synproxy_info),
450 .checkentry = synproxy_tg6_check, 452 .checkentry = synproxy_tg6_check,
diff --git a/net/ipv6/netfilter/ip6table_filter.c b/net/ipv6/netfilter/ip6table_filter.c
index 29b44b14c5ea..ca7f6c128086 100644
--- a/net/ipv6/netfilter/ip6table_filter.c
+++ b/net/ipv6/netfilter/ip6table_filter.c
@@ -32,13 +32,14 @@ static const struct xt_table packet_filter = {
32 32
33/* The work comes in here from netfilter.c. */ 33/* The work comes in here from netfilter.c. */
34static unsigned int 34static unsigned int
35ip6table_filter_hook(unsigned int hook, struct sk_buff *skb, 35ip6table_filter_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
36 const struct net_device *in, const struct net_device *out, 36 const struct net_device *in, const struct net_device *out,
37 int (*okfn)(struct sk_buff *)) 37 int (*okfn)(struct sk_buff *))
38{ 38{
39 const struct net *net = dev_net((in != NULL) ? in : out); 39 const struct net *net = dev_net((in != NULL) ? in : out);
40 40
41 return ip6t_do_table(skb, hook, in, out, net->ipv6.ip6table_filter); 41 return ip6t_do_table(skb, ops->hooknum, in, out,
42 net->ipv6.ip6table_filter);
42} 43}
43 44
44static struct nf_hook_ops *filter_ops __read_mostly; 45static struct nf_hook_ops *filter_ops __read_mostly;
diff --git a/net/ipv6/netfilter/ip6table_mangle.c b/net/ipv6/netfilter/ip6table_mangle.c
index c705907ae6ab..307bbb782d14 100644
--- a/net/ipv6/netfilter/ip6table_mangle.c
+++ b/net/ipv6/netfilter/ip6table_mangle.c
@@ -76,17 +76,17 @@ ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
76 76
77/* The work comes in here from netfilter.c. */ 77/* The work comes in here from netfilter.c. */
78static unsigned int 78static unsigned int
79ip6table_mangle_hook(unsigned int hook, struct sk_buff *skb, 79ip6table_mangle_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
80 const struct net_device *in, const struct net_device *out, 80 const struct net_device *in, const struct net_device *out,
81 int (*okfn)(struct sk_buff *)) 81 int (*okfn)(struct sk_buff *))
82{ 82{
83 if (hook == NF_INET_LOCAL_OUT) 83 if (ops->hooknum == NF_INET_LOCAL_OUT)
84 return ip6t_mangle_out(skb, out); 84 return ip6t_mangle_out(skb, out);
85 if (hook == NF_INET_POST_ROUTING) 85 if (ops->hooknum == NF_INET_POST_ROUTING)
86 return ip6t_do_table(skb, hook, in, out, 86 return ip6t_do_table(skb, ops->hooknum, in, out,
87 dev_net(out)->ipv6.ip6table_mangle); 87 dev_net(out)->ipv6.ip6table_mangle);
88 /* INPUT/FORWARD */ 88 /* INPUT/FORWARD */
89 return ip6t_do_table(skb, hook, in, out, 89 return ip6t_do_table(skb, ops->hooknum, in, out,
90 dev_net(in)->ipv6.ip6table_mangle); 90 dev_net(in)->ipv6.ip6table_mangle);
91} 91}
92 92
diff --git a/net/ipv6/netfilter/ip6table_nat.c b/net/ipv6/netfilter/ip6table_nat.c
index 9b076d2d3a7b..84c7f33d0cf8 100644
--- a/net/ipv6/netfilter/ip6table_nat.c
+++ b/net/ipv6/netfilter/ip6table_nat.c
@@ -63,7 +63,7 @@ static unsigned int nf_nat_rule_find(struct sk_buff *skb, unsigned int hooknum,
63} 63}
64 64
65static unsigned int 65static unsigned int
66nf_nat_ipv6_fn(unsigned int hooknum, 66nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
67 struct sk_buff *skb, 67 struct sk_buff *skb,
68 const struct net_device *in, 68 const struct net_device *in,
69 const struct net_device *out, 69 const struct net_device *out,
@@ -72,7 +72,7 @@ nf_nat_ipv6_fn(unsigned int hooknum,
72 struct nf_conn *ct; 72 struct nf_conn *ct;
73 enum ip_conntrack_info ctinfo; 73 enum ip_conntrack_info ctinfo;
74 struct nf_conn_nat *nat; 74 struct nf_conn_nat *nat;
75 enum nf_nat_manip_type maniptype = HOOK2MANIP(hooknum); 75 enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
76 __be16 frag_off; 76 __be16 frag_off;
77 int hdrlen; 77 int hdrlen;
78 u8 nexthdr; 78 u8 nexthdr;
@@ -111,7 +111,8 @@ nf_nat_ipv6_fn(unsigned int hooknum,
111 111
112 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) { 112 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
113 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo, 113 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
114 hooknum, hdrlen)) 114 ops->hooknum,
115 hdrlen))
115 return NF_DROP; 116 return NF_DROP;
116 else 117 else
117 return NF_ACCEPT; 118 return NF_ACCEPT;
@@ -124,14 +125,14 @@ nf_nat_ipv6_fn(unsigned int hooknum,
124 if (!nf_nat_initialized(ct, maniptype)) { 125 if (!nf_nat_initialized(ct, maniptype)) {
125 unsigned int ret; 126 unsigned int ret;
126 127
127 ret = nf_nat_rule_find(skb, hooknum, in, out, ct); 128 ret = nf_nat_rule_find(skb, ops->hooknum, in, out, ct);
128 if (ret != NF_ACCEPT) 129 if (ret != NF_ACCEPT)
129 return ret; 130 return ret;
130 } else { 131 } else {
131 pr_debug("Already setup manip %s for ct %p\n", 132 pr_debug("Already setup manip %s for ct %p\n",
132 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST", 133 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
133 ct); 134 ct);
134 if (nf_nat_oif_changed(hooknum, ctinfo, nat, out)) 135 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
135 goto oif_changed; 136 goto oif_changed;
136 } 137 }
137 break; 138 break;
@@ -140,11 +141,11 @@ nf_nat_ipv6_fn(unsigned int hooknum,
140 /* ESTABLISHED */ 141 /* ESTABLISHED */
141 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || 142 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
142 ctinfo == IP_CT_ESTABLISHED_REPLY); 143 ctinfo == IP_CT_ESTABLISHED_REPLY);
143 if (nf_nat_oif_changed(hooknum, ctinfo, nat, out)) 144 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out))
144 goto oif_changed; 145 goto oif_changed;
145 } 146 }
146 147
147 return nf_nat_packet(ct, ctinfo, hooknum, skb); 148 return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
148 149
149oif_changed: 150oif_changed:
150 nf_ct_kill_acct(ct, ctinfo, skb); 151 nf_ct_kill_acct(ct, ctinfo, skb);
@@ -152,7 +153,7 @@ oif_changed:
152} 153}
153 154
154static unsigned int 155static unsigned int
155nf_nat_ipv6_in(unsigned int hooknum, 156nf_nat_ipv6_in(const struct nf_hook_ops *ops,
156 struct sk_buff *skb, 157 struct sk_buff *skb,
157 const struct net_device *in, 158 const struct net_device *in,
158 const struct net_device *out, 159 const struct net_device *out,
@@ -161,7 +162,7 @@ nf_nat_ipv6_in(unsigned int hooknum,
161 unsigned int ret; 162 unsigned int ret;
162 struct in6_addr daddr = ipv6_hdr(skb)->daddr; 163 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
163 164
164 ret = nf_nat_ipv6_fn(hooknum, skb, in, out, okfn); 165 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
165 if (ret != NF_DROP && ret != NF_STOLEN && 166 if (ret != NF_DROP && ret != NF_STOLEN &&
166 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr)) 167 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
167 skb_dst_drop(skb); 168 skb_dst_drop(skb);
@@ -170,7 +171,7 @@ nf_nat_ipv6_in(unsigned int hooknum,
170} 171}
171 172
172static unsigned int 173static unsigned int
173nf_nat_ipv6_out(unsigned int hooknum, 174nf_nat_ipv6_out(const struct nf_hook_ops *ops,
174 struct sk_buff *skb, 175 struct sk_buff *skb,
175 const struct net_device *in, 176 const struct net_device *in,
176 const struct net_device *out, 177 const struct net_device *out,
@@ -187,7 +188,7 @@ nf_nat_ipv6_out(unsigned int hooknum,
187 if (skb->len < sizeof(struct ipv6hdr)) 188 if (skb->len < sizeof(struct ipv6hdr))
188 return NF_ACCEPT; 189 return NF_ACCEPT;
189 190
190 ret = nf_nat_ipv6_fn(hooknum, skb, in, out, okfn); 191 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
191#ifdef CONFIG_XFRM 192#ifdef CONFIG_XFRM
192 if (ret != NF_DROP && ret != NF_STOLEN && 193 if (ret != NF_DROP && ret != NF_STOLEN &&
193 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) && 194 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
@@ -209,7 +210,7 @@ nf_nat_ipv6_out(unsigned int hooknum,
209} 210}
210 211
211static unsigned int 212static unsigned int
212nf_nat_ipv6_local_fn(unsigned int hooknum, 213nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops,
213 struct sk_buff *skb, 214 struct sk_buff *skb,
214 const struct net_device *in, 215 const struct net_device *in,
215 const struct net_device *out, 216 const struct net_device *out,
@@ -224,7 +225,7 @@ nf_nat_ipv6_local_fn(unsigned int hooknum,
224 if (skb->len < sizeof(struct ipv6hdr)) 225 if (skb->len < sizeof(struct ipv6hdr))
225 return NF_ACCEPT; 226 return NF_ACCEPT;
226 227
227 ret = nf_nat_ipv6_fn(hooknum, skb, in, out, okfn); 228 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
228 if (ret != NF_DROP && ret != NF_STOLEN && 229 if (ret != NF_DROP && ret != NF_STOLEN &&
229 (ct = nf_ct_get(skb, &ctinfo)) != NULL) { 230 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
230 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); 231 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
diff --git a/net/ipv6/netfilter/ip6table_raw.c b/net/ipv6/netfilter/ip6table_raw.c
index 9a626d86720f..5274740acecc 100644
--- a/net/ipv6/netfilter/ip6table_raw.c
+++ b/net/ipv6/netfilter/ip6table_raw.c
@@ -19,13 +19,14 @@ static const struct xt_table packet_raw = {
19 19
20/* The work comes in here from netfilter.c. */ 20/* The work comes in here from netfilter.c. */
21static unsigned int 21static unsigned int
22ip6table_raw_hook(unsigned int hook, struct sk_buff *skb, 22ip6table_raw_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
23 const struct net_device *in, const struct net_device *out, 23 const struct net_device *in, const struct net_device *out,
24 int (*okfn)(struct sk_buff *)) 24 int (*okfn)(struct sk_buff *))
25{ 25{
26 const struct net *net = dev_net((in != NULL) ? in : out); 26 const struct net *net = dev_net((in != NULL) ? in : out);
27 27
28 return ip6t_do_table(skb, hook, in, out, net->ipv6.ip6table_raw); 28 return ip6t_do_table(skb, ops->hooknum, in, out,
29 net->ipv6.ip6table_raw);
29} 30}
30 31
31static struct nf_hook_ops *rawtable_ops __read_mostly; 32static struct nf_hook_ops *rawtable_ops __read_mostly;
diff --git a/net/ipv6/netfilter/ip6table_security.c b/net/ipv6/netfilter/ip6table_security.c
index ce88d1d7e525..ab3b0219ecfa 100644
--- a/net/ipv6/netfilter/ip6table_security.c
+++ b/net/ipv6/netfilter/ip6table_security.c
@@ -36,14 +36,15 @@ static const struct xt_table security_table = {
36}; 36};
37 37
38static unsigned int 38static unsigned int
39ip6table_security_hook(unsigned int hook, struct sk_buff *skb, 39ip6table_security_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
40 const struct net_device *in, 40 const struct net_device *in,
41 const struct net_device *out, 41 const struct net_device *out,
42 int (*okfn)(struct sk_buff *)) 42 int (*okfn)(struct sk_buff *))
43{ 43{
44 const struct net *net = dev_net((in != NULL) ? in : out); 44 const struct net *net = dev_net((in != NULL) ? in : out);
45 45
46 return ip6t_do_table(skb, hook, in, out, net->ipv6.ip6table_security); 46 return ip6t_do_table(skb, ops->hooknum, in, out,
47 net->ipv6.ip6table_security);
47} 48}
48 49
49static struct nf_hook_ops *sectbl_ops __read_mostly; 50static struct nf_hook_ops *sectbl_ops __read_mostly;
diff --git a/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c b/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
index d6e4dd8b58df..4cbc6b290dd5 100644
--- a/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
+++ b/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
@@ -95,7 +95,7 @@ static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
95 return NF_ACCEPT; 95 return NF_ACCEPT;
96} 96}
97 97
98static unsigned int ipv6_helper(unsigned int hooknum, 98static unsigned int ipv6_helper(const struct nf_hook_ops *ops,
99 struct sk_buff *skb, 99 struct sk_buff *skb,
100 const struct net_device *in, 100 const struct net_device *in,
101 const struct net_device *out, 101 const struct net_device *out,
@@ -133,7 +133,7 @@ static unsigned int ipv6_helper(unsigned int hooknum,
133 return helper->help(skb, protoff, ct, ctinfo); 133 return helper->help(skb, protoff, ct, ctinfo);
134} 134}
135 135
136static unsigned int ipv6_confirm(unsigned int hooknum, 136static unsigned int ipv6_confirm(const struct nf_hook_ops *ops,
137 struct sk_buff *skb, 137 struct sk_buff *skb,
138 const struct net_device *in, 138 const struct net_device *in,
139 const struct net_device *out, 139 const struct net_device *out,
@@ -169,66 +169,16 @@ out:
169 return nf_conntrack_confirm(skb); 169 return nf_conntrack_confirm(skb);
170} 170}
171 171
172static unsigned int __ipv6_conntrack_in(struct net *net, 172static unsigned int ipv6_conntrack_in(const struct nf_hook_ops *ops,
173 unsigned int hooknum,
174 struct sk_buff *skb,
175 const struct net_device *in,
176 const struct net_device *out,
177 int (*okfn)(struct sk_buff *))
178{
179 struct sk_buff *reasm = skb->nfct_reasm;
180 const struct nf_conn_help *help;
181 struct nf_conn *ct;
182 enum ip_conntrack_info ctinfo;
183
184 /* This packet is fragmented and has reassembled packet. */
185 if (reasm) {
186 /* Reassembled packet isn't parsed yet ? */
187 if (!reasm->nfct) {
188 unsigned int ret;
189
190 ret = nf_conntrack_in(net, PF_INET6, hooknum, reasm);
191 if (ret != NF_ACCEPT)
192 return ret;
193 }
194
195 /* Conntrack helpers need the entire reassembled packet in the
196 * POST_ROUTING hook. In case of unconfirmed connections NAT
197 * might reassign a helper, so the entire packet is also
198 * required.
199 */
200 ct = nf_ct_get(reasm, &ctinfo);
201 if (ct != NULL && !nf_ct_is_untracked(ct)) {
202 help = nfct_help(ct);
203 if ((help && help->helper) || !nf_ct_is_confirmed(ct)) {
204 nf_conntrack_get_reasm(reasm);
205 NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, reasm,
206 (struct net_device *)in,
207 (struct net_device *)out,
208 okfn, NF_IP6_PRI_CONNTRACK + 1);
209 return NF_DROP_ERR(-ECANCELED);
210 }
211 }
212
213 nf_conntrack_get(reasm->nfct);
214 skb->nfct = reasm->nfct;
215 skb->nfctinfo = reasm->nfctinfo;
216 return NF_ACCEPT;
217 }
218
219 return nf_conntrack_in(net, PF_INET6, hooknum, skb);
220}
221
222static unsigned int ipv6_conntrack_in(unsigned int hooknum,
223 struct sk_buff *skb, 173 struct sk_buff *skb,
224 const struct net_device *in, 174 const struct net_device *in,
225 const struct net_device *out, 175 const struct net_device *out,
226 int (*okfn)(struct sk_buff *)) 176 int (*okfn)(struct sk_buff *))
227{ 177{
228 return __ipv6_conntrack_in(dev_net(in), hooknum, skb, in, out, okfn); 178 return nf_conntrack_in(dev_net(in), PF_INET6, ops->hooknum, skb);
229} 179}
230 180
231static unsigned int ipv6_conntrack_local(unsigned int hooknum, 181static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops,
232 struct sk_buff *skb, 182 struct sk_buff *skb,
233 const struct net_device *in, 183 const struct net_device *in,
234 const struct net_device *out, 184 const struct net_device *out,
@@ -239,7 +189,7 @@ static unsigned int ipv6_conntrack_local(unsigned int hooknum,
239 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n"); 189 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
240 return NF_ACCEPT; 190 return NF_ACCEPT;
241 } 191 }
242 return __ipv6_conntrack_in(dev_net(out), hooknum, skb, in, out, okfn); 192 return nf_conntrack_in(dev_net(out), PF_INET6, ops->hooknum, skb);
243} 193}
244 194
245static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = { 195static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
@@ -297,9 +247,9 @@ ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
297 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 }; 247 struct nf_conntrack_tuple tuple = { .src.l3num = NFPROTO_IPV6 };
298 struct nf_conn *ct; 248 struct nf_conn *ct;
299 249
300 tuple.src.u3.in6 = inet6->rcv_saddr; 250 tuple.src.u3.in6 = sk->sk_v6_rcv_saddr;
301 tuple.src.u.tcp.port = inet->inet_sport; 251 tuple.src.u.tcp.port = inet->inet_sport;
302 tuple.dst.u3.in6 = inet6->daddr; 252 tuple.dst.u3.in6 = sk->sk_v6_daddr;
303 tuple.dst.u.tcp.port = inet->inet_dport; 253 tuple.dst.u.tcp.port = inet->inet_dport;
304 tuple.dst.protonum = sk->sk_protocol; 254 tuple.dst.protonum = sk->sk_protocol;
305 255
diff --git a/net/ipv6/netfilter/nf_conntrack_reasm.c b/net/ipv6/netfilter/nf_conntrack_reasm.c
index dffdc1a389c5..767ab8da8218 100644
--- a/net/ipv6/netfilter/nf_conntrack_reasm.c
+++ b/net/ipv6/netfilter/nf_conntrack_reasm.c
@@ -144,12 +144,24 @@ static inline u8 ip6_frag_ecn(const struct ipv6hdr *ipv6h)
144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK); 144 return 1 << (ipv6_get_dsfield(ipv6h) & INET_ECN_MASK);
145} 145}
146 146
147static unsigned int nf_hash_frag(__be32 id, const struct in6_addr *saddr,
148 const struct in6_addr *daddr)
149{
150 u32 c;
151
152 net_get_random_once(&nf_frags.rnd, sizeof(nf_frags.rnd));
153 c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
154 (__force u32)id, nf_frags.rnd);
155 return c & (INETFRAGS_HASHSZ - 1);
156}
157
158
147static unsigned int nf_hashfn(struct inet_frag_queue *q) 159static unsigned int nf_hashfn(struct inet_frag_queue *q)
148{ 160{
149 const struct frag_queue *nq; 161 const struct frag_queue *nq;
150 162
151 nq = container_of(q, struct frag_queue, q); 163 nq = container_of(q, struct frag_queue, q);
152 return inet6_hash_frag(nq->id, &nq->saddr, &nq->daddr, nf_frags.rnd); 164 return nf_hash_frag(nq->id, &nq->saddr, &nq->daddr);
153} 165}
154 166
155static void nf_skb_free(struct sk_buff *skb) 167static void nf_skb_free(struct sk_buff *skb)
@@ -185,7 +197,7 @@ static inline struct frag_queue *fq_find(struct net *net, __be32 id,
185 arg.ecn = ecn; 197 arg.ecn = ecn;
186 198
187 read_lock_bh(&nf_frags.lock); 199 read_lock_bh(&nf_frags.lock);
188 hash = inet6_hash_frag(id, src, dst, nf_frags.rnd); 200 hash = nf_hash_frag(id, src, dst);
189 201
190 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash); 202 q = inet_frag_find(&net->nf_frag.frags, &nf_frags, &arg, hash);
191 local_bh_enable(); 203 local_bh_enable();
@@ -621,31 +633,16 @@ ret_orig:
621 return skb; 633 return skb;
622} 634}
623 635
624void nf_ct_frag6_output(unsigned int hooknum, struct sk_buff *skb, 636void nf_ct_frag6_consume_orig(struct sk_buff *skb)
625 struct net_device *in, struct net_device *out,
626 int (*okfn)(struct sk_buff *))
627{ 637{
628 struct sk_buff *s, *s2; 638 struct sk_buff *s, *s2;
629 unsigned int ret = 0;
630 639
631 for (s = NFCT_FRAG6_CB(skb)->orig; s;) { 640 for (s = NFCT_FRAG6_CB(skb)->orig; s;) {
632 nf_conntrack_put_reasm(s->nfct_reasm);
633 nf_conntrack_get_reasm(skb);
634 s->nfct_reasm = skb;
635
636 s2 = s->next; 641 s2 = s->next;
637 s->next = NULL; 642 s->next = NULL;
638 643 consume_skb(s);
639 if (ret != -ECANCELED)
640 ret = NF_HOOK_THRESH(NFPROTO_IPV6, hooknum, s,
641 in, out, okfn,
642 NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
643 else
644 kfree_skb(s);
645
646 s = s2; 644 s = s2;
647 } 645 }
648 nf_conntrack_put_reasm(skb);
649} 646}
650 647
651static int nf_ct_net_init(struct net *net) 648static int nf_ct_net_init(struct net *net)
diff --git a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
index aacd121fe8c5..7b9a748c6bac 100644
--- a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
+++ b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
@@ -52,7 +52,7 @@ static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
52 52
53} 53}
54 54
55static unsigned int ipv6_defrag(unsigned int hooknum, 55static unsigned int ipv6_defrag(const struct nf_hook_ops *ops,
56 struct sk_buff *skb, 56 struct sk_buff *skb,
57 const struct net_device *in, 57 const struct net_device *in,
58 const struct net_device *out, 58 const struct net_device *out,
@@ -66,7 +66,7 @@ static unsigned int ipv6_defrag(unsigned int hooknum,
66 return NF_ACCEPT; 66 return NF_ACCEPT;
67#endif 67#endif
68 68
69 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb)); 69 reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(ops->hooknum, skb));
70 /* queued */ 70 /* queued */
71 if (reasm == NULL) 71 if (reasm == NULL)
72 return NF_STOLEN; 72 return NF_STOLEN;
@@ -75,8 +75,11 @@ static unsigned int ipv6_defrag(unsigned int hooknum,
75 if (reasm == skb) 75 if (reasm == skb)
76 return NF_ACCEPT; 76 return NF_ACCEPT;
77 77
78 nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in, 78 nf_ct_frag6_consume_orig(reasm);
79 (struct net_device *)out, okfn); 79
80 NF_HOOK_THRESH(NFPROTO_IPV6, ops->hooknum, reasm,
81 (struct net_device *) in, (struct net_device *) out,
82 okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
80 83
81 return NF_STOLEN; 84 return NF_STOLEN;
82} 85}
diff --git a/net/ipv6/netfilter/nf_tables_ipv6.c b/net/ipv6/netfilter/nf_tables_ipv6.c
new file mode 100644
index 000000000000..d77db8a13505
--- /dev/null
+++ b/net/ipv6/netfilter/nf_tables_ipv6.c
@@ -0,0 +1,127 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012-2013 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/ipv6.h>
15#include <linux/netfilter_ipv6.h>
16#include <net/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_ipv6.h>
18
19static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops,
20 struct sk_buff *skb,
21 const struct net_device *in,
22 const struct net_device *out,
23 int (*okfn)(struct sk_buff *))
24{
25 struct nft_pktinfo pkt;
26
27 if (unlikely(skb->len < sizeof(struct ipv6hdr))) {
28 if (net_ratelimit())
29 pr_info("nf_tables_ipv6: ignoring short SOCK_RAW "
30 "packet\n");
31 return NF_ACCEPT;
32 }
33 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
34 return NF_DROP;
35
36 return nft_do_chain_pktinfo(&pkt, ops);
37}
38
39static struct nft_af_info nft_af_ipv6 __read_mostly = {
40 .family = NFPROTO_IPV6,
41 .nhooks = NF_INET_NUMHOOKS,
42 .owner = THIS_MODULE,
43 .hooks = {
44 [NF_INET_LOCAL_OUT] = nft_ipv6_output,
45 },
46};
47
48static int nf_tables_ipv6_init_net(struct net *net)
49{
50 net->nft.ipv6 = kmalloc(sizeof(struct nft_af_info), GFP_KERNEL);
51 if (net->nft.ipv6 == NULL)
52 return -ENOMEM;
53
54 memcpy(net->nft.ipv6, &nft_af_ipv6, sizeof(nft_af_ipv6));
55
56 if (nft_register_afinfo(net, net->nft.ipv6) < 0)
57 goto err;
58
59 return 0;
60err:
61 kfree(net->nft.ipv6);
62 return -ENOMEM;
63}
64
65static void nf_tables_ipv6_exit_net(struct net *net)
66{
67 nft_unregister_afinfo(net->nft.ipv6);
68 kfree(net->nft.ipv6);
69}
70
71static struct pernet_operations nf_tables_ipv6_net_ops = {
72 .init = nf_tables_ipv6_init_net,
73 .exit = nf_tables_ipv6_exit_net,
74};
75
76static unsigned int
77nft_do_chain_ipv6(const struct nf_hook_ops *ops,
78 struct sk_buff *skb,
79 const struct net_device *in,
80 const struct net_device *out,
81 int (*okfn)(struct sk_buff *))
82{
83 struct nft_pktinfo pkt;
84
85 /* malformed packet, drop it */
86 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
87 return NF_DROP;
88
89 return nft_do_chain_pktinfo(&pkt, ops);
90}
91
92static struct nf_chain_type filter_ipv6 = {
93 .family = NFPROTO_IPV6,
94 .name = "filter",
95 .type = NFT_CHAIN_T_DEFAULT,
96 .hook_mask = (1 << NF_INET_LOCAL_IN) |
97 (1 << NF_INET_LOCAL_OUT) |
98 (1 << NF_INET_FORWARD) |
99 (1 << NF_INET_PRE_ROUTING) |
100 (1 << NF_INET_POST_ROUTING),
101 .fn = {
102 [NF_INET_LOCAL_IN] = nft_do_chain_ipv6,
103 [NF_INET_LOCAL_OUT] = nft_ipv6_output,
104 [NF_INET_FORWARD] = nft_do_chain_ipv6,
105 [NF_INET_PRE_ROUTING] = nft_do_chain_ipv6,
106 [NF_INET_POST_ROUTING] = nft_do_chain_ipv6,
107 },
108};
109
110static int __init nf_tables_ipv6_init(void)
111{
112 nft_register_chain_type(&filter_ipv6);
113 return register_pernet_subsys(&nf_tables_ipv6_net_ops);
114}
115
116static void __exit nf_tables_ipv6_exit(void)
117{
118 unregister_pernet_subsys(&nf_tables_ipv6_net_ops);
119 nft_unregister_chain_type(&filter_ipv6);
120}
121
122module_init(nf_tables_ipv6_init);
123module_exit(nf_tables_ipv6_exit);
124
125MODULE_LICENSE("GPL");
126MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
127MODULE_ALIAS_NFT_FAMILY(AF_INET6);
diff --git a/net/ipv6/netfilter/nft_chain_nat_ipv6.c b/net/ipv6/netfilter/nft_chain_nat_ipv6.c
new file mode 100644
index 000000000000..e86dcd70dc76
--- /dev/null
+++ b/net/ipv6/netfilter/nft_chain_nat_ipv6.c
@@ -0,0 +1,211 @@
1/*
2 * Copyright (c) 2011 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter_ipv6.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_conntrack.h>
20#include <net/netfilter/nf_nat.h>
21#include <net/netfilter/nf_nat_core.h>
22#include <net/netfilter/nf_tables.h>
23#include <net/netfilter/nf_tables_ipv6.h>
24#include <net/netfilter/nf_nat_l3proto.h>
25#include <net/ipv6.h>
26
27/*
28 * IPv6 NAT chains
29 */
30
31static unsigned int nf_nat_ipv6_fn(const struct nf_hook_ops *ops,
32 struct sk_buff *skb,
33 const struct net_device *in,
34 const struct net_device *out,
35 int (*okfn)(struct sk_buff *))
36{
37 enum ip_conntrack_info ctinfo;
38 struct nf_conn *ct = nf_ct_get(skb, &ctinfo);
39 struct nf_conn_nat *nat;
40 enum nf_nat_manip_type maniptype = HOOK2MANIP(ops->hooknum);
41 __be16 frag_off;
42 int hdrlen;
43 u8 nexthdr;
44 struct nft_pktinfo pkt;
45 unsigned int ret;
46
47 if (ct == NULL || nf_ct_is_untracked(ct))
48 return NF_ACCEPT;
49
50 nat = nfct_nat(ct);
51 if (nat == NULL) {
52 /* Conntrack module was loaded late, can't add extension. */
53 if (nf_ct_is_confirmed(ct))
54 return NF_ACCEPT;
55 nat = nf_ct_ext_add(ct, NF_CT_EXT_NAT, GFP_ATOMIC);
56 if (nat == NULL)
57 return NF_ACCEPT;
58 }
59
60 switch (ctinfo) {
61 case IP_CT_RELATED:
62 case IP_CT_RELATED + IP_CT_IS_REPLY:
63 nexthdr = ipv6_hdr(skb)->nexthdr;
64 hdrlen = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr),
65 &nexthdr, &frag_off);
66
67 if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
68 if (!nf_nat_icmpv6_reply_translation(skb, ct, ctinfo,
69 ops->hooknum,
70 hdrlen))
71 return NF_DROP;
72 else
73 return NF_ACCEPT;
74 }
75 /* Fall through */
76 case IP_CT_NEW:
77 if (nf_nat_initialized(ct, maniptype))
78 break;
79
80 nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out);
81
82 ret = nft_do_chain_pktinfo(&pkt, ops);
83 if (ret != NF_ACCEPT)
84 return ret;
85 if (!nf_nat_initialized(ct, maniptype)) {
86 ret = nf_nat_alloc_null_binding(ct, ops->hooknum);
87 if (ret != NF_ACCEPT)
88 return ret;
89 }
90 default:
91 break;
92 }
93
94 return nf_nat_packet(ct, ctinfo, ops->hooknum, skb);
95}
96
97static unsigned int nf_nat_ipv6_prerouting(const struct nf_hook_ops *ops,
98 struct sk_buff *skb,
99 const struct net_device *in,
100 const struct net_device *out,
101 int (*okfn)(struct sk_buff *))
102{
103 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
104 unsigned int ret;
105
106 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
107 if (ret != NF_DROP && ret != NF_STOLEN &&
108 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
109 skb_dst_drop(skb);
110
111 return ret;
112}
113
114static unsigned int nf_nat_ipv6_postrouting(const struct nf_hook_ops *ops,
115 struct sk_buff *skb,
116 const struct net_device *in,
117 const struct net_device *out,
118 int (*okfn)(struct sk_buff *))
119{
120 enum ip_conntrack_info ctinfo __maybe_unused;
121 const struct nf_conn *ct __maybe_unused;
122 unsigned int ret;
123
124 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
125#ifdef CONFIG_XFRM
126 if (ret != NF_DROP && ret != NF_STOLEN &&
127 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
128 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
129 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
130
131 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.src.u3,
132 &ct->tuplehash[!dir].tuple.dst.u3) ||
133 (ct->tuplehash[dir].tuple.src.u.all !=
134 ct->tuplehash[!dir].tuple.dst.u.all))
135 if (nf_xfrm_me_harder(skb, AF_INET6) < 0)
136 ret = NF_DROP;
137 }
138#endif
139 return ret;
140}
141
142static unsigned int nf_nat_ipv6_output(const struct nf_hook_ops *ops,
143 struct sk_buff *skb,
144 const struct net_device *in,
145 const struct net_device *out,
146 int (*okfn)(struct sk_buff *))
147{
148 enum ip_conntrack_info ctinfo;
149 const struct nf_conn *ct;
150 unsigned int ret;
151
152 ret = nf_nat_ipv6_fn(ops, skb, in, out, okfn);
153 if (ret != NF_DROP && ret != NF_STOLEN &&
154 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
155 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
156
157 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
158 &ct->tuplehash[!dir].tuple.src.u3)) {
159 if (ip6_route_me_harder(skb))
160 ret = NF_DROP;
161 }
162#ifdef CONFIG_XFRM
163 else if (!(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
164 ct->tuplehash[dir].tuple.dst.u.all !=
165 ct->tuplehash[!dir].tuple.src.u.all)
166 if (nf_xfrm_me_harder(skb, AF_INET6))
167 ret = NF_DROP;
168#endif
169 }
170 return ret;
171}
172
173static struct nf_chain_type nft_chain_nat_ipv6 = {
174 .family = NFPROTO_IPV6,
175 .name = "nat",
176 .type = NFT_CHAIN_T_NAT,
177 .hook_mask = (1 << NF_INET_PRE_ROUTING) |
178 (1 << NF_INET_POST_ROUTING) |
179 (1 << NF_INET_LOCAL_OUT) |
180 (1 << NF_INET_LOCAL_IN),
181 .fn = {
182 [NF_INET_PRE_ROUTING] = nf_nat_ipv6_prerouting,
183 [NF_INET_POST_ROUTING] = nf_nat_ipv6_postrouting,
184 [NF_INET_LOCAL_OUT] = nf_nat_ipv6_output,
185 [NF_INET_LOCAL_IN] = nf_nat_ipv6_fn,
186 },
187 .me = THIS_MODULE,
188};
189
190static int __init nft_chain_nat_ipv6_init(void)
191{
192 int err;
193
194 err = nft_register_chain_type(&nft_chain_nat_ipv6);
195 if (err < 0)
196 return err;
197
198 return 0;
199}
200
201static void __exit nft_chain_nat_ipv6_exit(void)
202{
203 nft_unregister_chain_type(&nft_chain_nat_ipv6);
204}
205
206module_init(nft_chain_nat_ipv6_init);
207module_exit(nft_chain_nat_ipv6_exit);
208
209MODULE_LICENSE("GPL");
210MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
211MODULE_ALIAS_NFT_CHAIN(AF_INET6, "nat");
diff --git a/net/ipv6/netfilter/nft_chain_route_ipv6.c b/net/ipv6/netfilter/nft_chain_route_ipv6.c
new file mode 100644
index 000000000000..3fe40f0456ad
--- /dev/null
+++ b/net/ipv6/netfilter/nft_chain_route_ipv6.c
@@ -0,0 +1,88 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/list.h>
15#include <linux/skbuff.h>
16#include <linux/netlink.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter_ipv6.h>
19#include <linux/netfilter/nfnetlink.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_tables.h>
22#include <net/netfilter/nf_tables_ipv6.h>
23#include <net/route.h>
24
25static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
26 struct sk_buff *skb,
27 const struct net_device *in,
28 const struct net_device *out,
29 int (*okfn)(struct sk_buff *))
30{
31 unsigned int ret;
32 struct nft_pktinfo pkt;
33 struct in6_addr saddr, daddr;
34 u_int8_t hop_limit;
35 u32 mark, flowlabel;
36
37 /* malformed packet, drop it */
38 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0)
39 return NF_DROP;
40
41 /* save source/dest address, mark, hoplimit, flowlabel, priority */
42 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
43 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
44 mark = skb->mark;
45 hop_limit = ipv6_hdr(skb)->hop_limit;
46
47 /* flowlabel and prio (includes version, which shouldn't change either */
48 flowlabel = *((u32 *)ipv6_hdr(skb));
49
50 ret = nft_do_chain_pktinfo(&pkt, ops);
51 if (ret != NF_DROP && ret != NF_QUEUE &&
52 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
53 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
54 skb->mark != mark ||
55 ipv6_hdr(skb)->hop_limit != hop_limit ||
56 flowlabel != *((u_int32_t *)ipv6_hdr(skb))))
57 return ip6_route_me_harder(skb) == 0 ? ret : NF_DROP;
58
59 return ret;
60}
61
62static struct nf_chain_type nft_chain_route_ipv6 = {
63 .family = NFPROTO_IPV6,
64 .name = "route",
65 .type = NFT_CHAIN_T_ROUTE,
66 .hook_mask = (1 << NF_INET_LOCAL_OUT),
67 .fn = {
68 [NF_INET_LOCAL_OUT] = nf_route_table_hook,
69 },
70 .me = THIS_MODULE,
71};
72
73static int __init nft_chain_route_init(void)
74{
75 return nft_register_chain_type(&nft_chain_route_ipv6);
76}
77
78static void __exit nft_chain_route_exit(void)
79{
80 nft_unregister_chain_type(&nft_chain_route_ipv6);
81}
82
83module_init(nft_chain_route_init);
84module_exit(nft_chain_route_exit);
85
86MODULE_LICENSE("GPL");
87MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
88MODULE_ALIAS_NFT_CHAIN(AF_INET6, "route");
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index 18f19df4189f..a83243c3d656 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -57,7 +57,8 @@ static struct inet_protosw pingv6_protosw = {
57 57
58 58
59/* Compatibility glue so we can support IPv6 when it's compiled as a module */ 59/* Compatibility glue so we can support IPv6 when it's compiled as a module */
60static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len) 60static int dummy_ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len,
61 int *addr_len)
61{ 62{
62 return -EAFNOSUPPORT; 63 return -EAFNOSUPPORT;
63} 64}
@@ -116,7 +117,7 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
116 } else { 117 } else {
117 if (sk->sk_state != TCP_ESTABLISHED) 118 if (sk->sk_state != TCP_ESTABLISHED)
118 return -EDESTADDRREQ; 119 return -EDESTADDRREQ;
119 daddr = &np->daddr; 120 daddr = &sk->sk_v6_daddr;
120 } 121 }
121 122
122 if (!iif) 123 if (!iif)
diff --git a/net/ipv6/protocol.c b/net/ipv6/protocol.c
index 22d1bd4670da..e048cf1bb6a2 100644
--- a/net/ipv6/protocol.c
+++ b/net/ipv6/protocol.c
@@ -36,10 +36,6 @@ int inet6_add_protocol(const struct inet6_protocol *prot, unsigned char protocol
36} 36}
37EXPORT_SYMBOL(inet6_add_protocol); 37EXPORT_SYMBOL(inet6_add_protocol);
38 38
39/*
40 * Remove a protocol from the hash tables.
41 */
42
43int inet6_del_protocol(const struct inet6_protocol *prot, unsigned char protocol) 39int inet6_del_protocol(const struct inet6_protocol *prot, unsigned char protocol)
44{ 40{
45 int ret; 41 int ret;
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index a4ed2416399e..b6bb87e55805 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -77,20 +77,19 @@ static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
77 77
78 sk_for_each_from(sk) 78 sk_for_each_from(sk)
79 if (inet_sk(sk)->inet_num == num) { 79 if (inet_sk(sk)->inet_num == num) {
80 struct ipv6_pinfo *np = inet6_sk(sk);
81 80
82 if (!net_eq(sock_net(sk), net)) 81 if (!net_eq(sock_net(sk), net))
83 continue; 82 continue;
84 83
85 if (!ipv6_addr_any(&np->daddr) && 84 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
86 !ipv6_addr_equal(&np->daddr, rmt_addr)) 85 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
87 continue; 86 continue;
88 87
89 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) 88 if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
90 continue; 89 continue;
91 90
92 if (!ipv6_addr_any(&np->rcv_saddr)) { 91 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
93 if (ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 92 if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
94 goto found; 93 goto found;
95 if (is_multicast && 94 if (is_multicast &&
96 inet6_mc_check(sk, loc_addr, rmt_addr)) 95 inet6_mc_check(sk, loc_addr, rmt_addr))
@@ -302,7 +301,7 @@ static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
302 } 301 }
303 302
304 inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 303 inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
305 np->rcv_saddr = addr->sin6_addr; 304 sk->sk_v6_rcv_saddr = addr->sin6_addr;
306 if (!(addr_type & IPV6_ADDR_MULTICAST)) 305 if (!(addr_type & IPV6_ADDR_MULTICAST))
307 np->saddr = addr->sin6_addr; 306 np->saddr = addr->sin6_addr;
308 err = 0; 307 err = 0;
@@ -466,14 +465,11 @@ static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
466 if (flags & MSG_OOB) 465 if (flags & MSG_OOB)
467 return -EOPNOTSUPP; 466 return -EOPNOTSUPP;
468 467
469 if (addr_len)
470 *addr_len=sizeof(*sin6);
471
472 if (flags & MSG_ERRQUEUE) 468 if (flags & MSG_ERRQUEUE)
473 return ipv6_recv_error(sk, msg, len); 469 return ipv6_recv_error(sk, msg, len, addr_len);
474 470
475 if (np->rxpmtu && np->rxopt.bits.rxpmtu) 471 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
476 return ipv6_recv_rxpmtu(sk, msg, len); 472 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
477 473
478 skb = skb_recv_datagram(sk, flags, noblock, &err); 474 skb = skb_recv_datagram(sk, flags, noblock, &err);
479 if (!skb) 475 if (!skb)
@@ -507,6 +503,7 @@ static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
507 sin6->sin6_flowinfo = 0; 503 sin6->sin6_flowinfo = 0;
508 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr, 504 sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
509 IP6CB(skb)->iif); 505 IP6CB(skb)->iif);
506 *addr_len = sizeof(*sin6);
510 } 507 }
511 508
512 sock_recv_ts_and_drops(msg, sk, skb); 509 sock_recv_ts_and_drops(msg, sk, skb);
@@ -795,7 +792,6 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
795 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 792 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
796 if (flowlabel == NULL) 793 if (flowlabel == NULL)
797 return -EINVAL; 794 return -EINVAL;
798 daddr = &flowlabel->dst;
799 } 795 }
800 } 796 }
801 797
@@ -804,8 +800,8 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
804 * sk->sk_dst_cache. 800 * sk->sk_dst_cache.
805 */ 801 */
806 if (sk->sk_state == TCP_ESTABLISHED && 802 if (sk->sk_state == TCP_ESTABLISHED &&
807 ipv6_addr_equal(daddr, &np->daddr)) 803 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
808 daddr = &np->daddr; 804 daddr = &sk->sk_v6_daddr;
809 805
810 if (addr_len >= sizeof(struct sockaddr_in6) && 806 if (addr_len >= sizeof(struct sockaddr_in6) &&
811 sin6->sin6_scope_id && 807 sin6->sin6_scope_id &&
@@ -816,7 +812,7 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
816 return -EDESTADDRREQ; 812 return -EDESTADDRREQ;
817 813
818 proto = inet->inet_num; 814 proto = inet->inet_num;
819 daddr = &np->daddr; 815 daddr = &sk->sk_v6_daddr;
820 fl6.flowlabel = np->flow_label; 816 fl6.flowlabel = np->flow_label;
821 } 817 }
822 818
diff --git a/net/ipv6/reassembly.c b/net/ipv6/reassembly.c
index 1aeb473b2cc6..cc85a9ba5010 100644
--- a/net/ipv6/reassembly.c
+++ b/net/ipv6/reassembly.c
@@ -82,24 +82,24 @@ static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
82 * callers should be careful not to use the hash value outside the ipfrag_lock 82 * callers should be careful not to use the hash value outside the ipfrag_lock
83 * as doing so could race with ipfrag_hash_rnd being recalculated. 83 * as doing so could race with ipfrag_hash_rnd being recalculated.
84 */ 84 */
85unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr, 85static unsigned int inet6_hash_frag(__be32 id, const struct in6_addr *saddr,
86 const struct in6_addr *daddr, u32 rnd) 86 const struct in6_addr *daddr)
87{ 87{
88 u32 c; 88 u32 c;
89 89
90 net_get_random_once(&ip6_frags.rnd, sizeof(ip6_frags.rnd));
90 c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr), 91 c = jhash_3words(ipv6_addr_hash(saddr), ipv6_addr_hash(daddr),
91 (__force u32)id, rnd); 92 (__force u32)id, ip6_frags.rnd);
92 93
93 return c & (INETFRAGS_HASHSZ - 1); 94 return c & (INETFRAGS_HASHSZ - 1);
94} 95}
95EXPORT_SYMBOL_GPL(inet6_hash_frag);
96 96
97static unsigned int ip6_hashfn(struct inet_frag_queue *q) 97static unsigned int ip6_hashfn(struct inet_frag_queue *q)
98{ 98{
99 struct frag_queue *fq; 99 struct frag_queue *fq;
100 100
101 fq = container_of(q, struct frag_queue, q); 101 fq = container_of(q, struct frag_queue, q);
102 return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr, ip6_frags.rnd); 102 return inet6_hash_frag(fq->id, &fq->saddr, &fq->daddr);
103} 103}
104 104
105bool ip6_frag_match(struct inet_frag_queue *q, void *a) 105bool ip6_frag_match(struct inet_frag_queue *q, void *a)
@@ -193,7 +193,7 @@ fq_find(struct net *net, __be32 id, const struct in6_addr *src,
193 arg.ecn = ecn; 193 arg.ecn = ecn;
194 194
195 read_lock(&ip6_frags.lock); 195 read_lock(&ip6_frags.lock);
196 hash = inet6_hash_frag(id, src, dst, ip6_frags.rnd); 196 hash = inet6_hash_frag(id, src, dst);
197 197
198 q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash); 198 q = inet_frag_find(&net->ipv6.frags, &ip6_frags, &arg, hash);
199 if (IS_ERR_OR_NULL(q)) { 199 if (IS_ERR_OR_NULL(q)) {
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index f54e3a101098..4b4944c3e4c4 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -84,6 +84,8 @@ static int ip6_dst_gc(struct dst_ops *ops);
84 84
85static int ip6_pkt_discard(struct sk_buff *skb); 85static int ip6_pkt_discard(struct sk_buff *skb);
86static int ip6_pkt_discard_out(struct sk_buff *skb); 86static int ip6_pkt_discard_out(struct sk_buff *skb);
87static int ip6_pkt_prohibit(struct sk_buff *skb);
88static int ip6_pkt_prohibit_out(struct sk_buff *skb);
87static void ip6_link_failure(struct sk_buff *skb); 89static void ip6_link_failure(struct sk_buff *skb);
88static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk, 90static void ip6_rt_update_pmtu(struct dst_entry *dst, struct sock *sk,
89 struct sk_buff *skb, u32 mtu); 91 struct sk_buff *skb, u32 mtu);
@@ -234,9 +236,6 @@ static const struct rt6_info ip6_null_entry_template = {
234 236
235#ifdef CONFIG_IPV6_MULTIPLE_TABLES 237#ifdef CONFIG_IPV6_MULTIPLE_TABLES
236 238
237static int ip6_pkt_prohibit(struct sk_buff *skb);
238static int ip6_pkt_prohibit_out(struct sk_buff *skb);
239
240static const struct rt6_info ip6_prohibit_entry_template = { 239static const struct rt6_info ip6_prohibit_entry_template = {
241 .dst = { 240 .dst = {
242 .__refcnt = ATOMIC_INIT(1), 241 .__refcnt = ATOMIC_INIT(1),
@@ -619,7 +618,7 @@ static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
619 goto out; 618 goto out;
620 619
621 m = rt6_score_route(rt, oif, strict); 620 m = rt6_score_route(rt, oif, strict);
622 if (m == RT6_NUD_FAIL_SOFT && !IS_ENABLED(CONFIG_IPV6_ROUTER_PREF)) { 621 if (m == RT6_NUD_FAIL_SOFT) {
623 match_do_rr = true; 622 match_do_rr = true;
624 m = 0; /* lowest valid score */ 623 m = 0; /* lowest valid score */
625 } else if (m < 0) { 624 } else if (m < 0) {
@@ -731,8 +730,11 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
731 prefix = &prefix_buf; 730 prefix = &prefix_buf;
732 } 731 }
733 732
734 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 733 if (rinfo->prefix_len == 0)
735 dev->ifindex); 734 rt = rt6_get_dflt_router(gwaddr, dev);
735 else
736 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
737 gwaddr, dev->ifindex);
736 738
737 if (rt && !lifetime) { 739 if (rt && !lifetime) {
738 ip6_del_rt(rt); 740 ip6_del_rt(rt);
@@ -871,11 +873,9 @@ static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
871 rt = ip6_rt_copy(ort, daddr); 873 rt = ip6_rt_copy(ort, daddr);
872 874
873 if (rt) { 875 if (rt) {
874 if (!(rt->rt6i_flags & RTF_GATEWAY)) { 876 if (ort->rt6i_dst.plen != 128 &&
875 if (ort->rt6i_dst.plen != 128 && 877 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
876 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 878 rt->rt6i_flags |= RTF_ANYCAST;
877 rt->rt6i_flags |= RTF_ANYCAST;
878 }
879 879
880 rt->rt6i_flags |= RTF_CACHE; 880 rt->rt6i_flags |= RTF_CACHE;
881 881
@@ -1087,10 +1087,13 @@ static struct dst_entry *ip6_dst_check(struct dst_entry *dst, u32 cookie)
1087 if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev))) 1087 if (rt->rt6i_genid != rt_genid_ipv6(dev_net(rt->dst.dev)))
1088 return NULL; 1088 return NULL;
1089 1089
1090 if (rt->rt6i_node && (rt->rt6i_node->fn_sernum == cookie)) 1090 if (!rt->rt6i_node || (rt->rt6i_node->fn_sernum != cookie))
1091 return dst; 1091 return NULL;
1092 1092
1093 return NULL; 1093 if (rt6_check_expired(rt))
1094 return NULL;
1095
1096 return dst;
1094} 1097}
1095 1098
1096static struct dst_entry *ip6_negative_advice(struct dst_entry *dst) 1099static struct dst_entry *ip6_negative_advice(struct dst_entry *dst)
@@ -1160,7 +1163,6 @@ void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1160 memset(&fl6, 0, sizeof(fl6)); 1163 memset(&fl6, 0, sizeof(fl6));
1161 fl6.flowi6_oif = oif; 1164 fl6.flowi6_oif = oif;
1162 fl6.flowi6_mark = mark; 1165 fl6.flowi6_mark = mark;
1163 fl6.flowi6_flags = 0;
1164 fl6.daddr = iph->daddr; 1166 fl6.daddr = iph->daddr;
1165 fl6.saddr = iph->saddr; 1167 fl6.saddr = iph->saddr;
1166 fl6.flowlabel = ip6_flowinfo(iph); 1168 fl6.flowlabel = ip6_flowinfo(iph);
@@ -1259,7 +1261,6 @@ void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1259 memset(&fl6, 0, sizeof(fl6)); 1261 memset(&fl6, 0, sizeof(fl6));
1260 fl6.flowi6_oif = oif; 1262 fl6.flowi6_oif = oif;
1261 fl6.flowi6_mark = mark; 1263 fl6.flowi6_mark = mark;
1262 fl6.flowi6_flags = 0;
1263 fl6.daddr = iph->daddr; 1264 fl6.daddr = iph->daddr;
1264 fl6.saddr = iph->saddr; 1265 fl6.saddr = iph->saddr;
1265 fl6.flowlabel = ip6_flowinfo(iph); 1266 fl6.flowlabel = ip6_flowinfo(iph);
@@ -1281,7 +1282,6 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1281 memset(&fl6, 0, sizeof(fl6)); 1282 memset(&fl6, 0, sizeof(fl6));
1282 fl6.flowi6_oif = oif; 1283 fl6.flowi6_oif = oif;
1283 fl6.flowi6_mark = mark; 1284 fl6.flowi6_mark = mark;
1284 fl6.flowi6_flags = 0;
1285 fl6.daddr = msg->dest; 1285 fl6.daddr = msg->dest;
1286 fl6.saddr = iph->daddr; 1286 fl6.saddr = iph->daddr;
1287 1287
@@ -1564,21 +1564,24 @@ int ip6_route_add(struct fib6_config *cfg)
1564 goto out; 1564 goto out;
1565 } 1565 }
1566 } 1566 }
1567 rt->dst.output = ip6_pkt_discard_out;
1568 rt->dst.input = ip6_pkt_discard;
1569 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP; 1567 rt->rt6i_flags = RTF_REJECT|RTF_NONEXTHOP;
1570 switch (cfg->fc_type) { 1568 switch (cfg->fc_type) {
1571 case RTN_BLACKHOLE: 1569 case RTN_BLACKHOLE:
1572 rt->dst.error = -EINVAL; 1570 rt->dst.error = -EINVAL;
1571 rt->dst.output = dst_discard;
1572 rt->dst.input = dst_discard;
1573 break; 1573 break;
1574 case RTN_PROHIBIT: 1574 case RTN_PROHIBIT:
1575 rt->dst.error = -EACCES; 1575 rt->dst.error = -EACCES;
1576 rt->dst.output = ip6_pkt_prohibit_out;
1577 rt->dst.input = ip6_pkt_prohibit;
1576 break; 1578 break;
1577 case RTN_THROW: 1579 case RTN_THROW:
1578 rt->dst.error = -EAGAIN;
1579 break;
1580 default: 1580 default:
1581 rt->dst.error = -ENETUNREACH; 1581 rt->dst.error = (cfg->fc_type == RTN_THROW) ? -EAGAIN
1582 : -ENETUNREACH;
1583 rt->dst.output = ip6_pkt_discard_out;
1584 rt->dst.input = ip6_pkt_discard;
1582 break; 1585 break;
1583 } 1586 }
1584 goto install_route; 1587 goto install_route;
@@ -1902,9 +1905,7 @@ static struct rt6_info *ip6_rt_copy(struct rt6_info *ort,
1902 else 1905 else
1903 rt->rt6i_gateway = *dest; 1906 rt->rt6i_gateway = *dest;
1904 rt->rt6i_flags = ort->rt6i_flags; 1907 rt->rt6i_flags = ort->rt6i_flags;
1905 if ((ort->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) == 1908 rt6_set_from(rt, ort);
1906 (RTF_DEFAULT | RTF_ADDRCONF))
1907 rt6_set_from(rt, ort);
1908 rt->rt6i_metric = 0; 1909 rt->rt6i_metric = 0;
1909 1910
1910#ifdef CONFIG_IPV6_SUBTREES 1911#ifdef CONFIG_IPV6_SUBTREES
@@ -2143,8 +2144,6 @@ static int ip6_pkt_discard_out(struct sk_buff *skb)
2143 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES); 2144 return ip6_pkt_drop(skb, ICMPV6_NOROUTE, IPSTATS_MIB_OUTNOROUTES);
2144} 2145}
2145 2146
2146#ifdef CONFIG_IPV6_MULTIPLE_TABLES
2147
2148static int ip6_pkt_prohibit(struct sk_buff *skb) 2147static int ip6_pkt_prohibit(struct sk_buff *skb)
2149{ 2148{
2150 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES); 2149 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_INNOROUTES);
@@ -2156,8 +2155,6 @@ static int ip6_pkt_prohibit_out(struct sk_buff *skb)
2156 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES); 2155 return ip6_pkt_drop(skb, ICMPV6_ADM_PROHIBITED, IPSTATS_MIB_OUTNOROUTES);
2157} 2156}
2158 2157
2159#endif
2160
2161/* 2158/*
2162 * Allocate a dst for local (unicast / anycast) address. 2159 * Allocate a dst for local (unicast / anycast) address.
2163 */ 2160 */
@@ -2167,12 +2164,10 @@ struct rt6_info *addrconf_dst_alloc(struct inet6_dev *idev,
2167 bool anycast) 2164 bool anycast)
2168{ 2165{
2169 struct net *net = dev_net(idev->dev); 2166 struct net *net = dev_net(idev->dev);
2170 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev, 0, NULL); 2167 struct rt6_info *rt = ip6_dst_alloc(net, net->loopback_dev,
2171 2168 DST_NOCOUNT, NULL);
2172 if (!rt) { 2169 if (!rt)
2173 net_warn_ratelimited("Maximum number of routes reached, consider increasing route/max_size\n");
2174 return ERR_PTR(-ENOMEM); 2170 return ERR_PTR(-ENOMEM);
2175 }
2176 2171
2177 in6_dev_hold(idev); 2172 in6_dev_hold(idev);
2178 2173
@@ -2828,56 +2823,12 @@ static int ip6_route_dev_notify(struct notifier_block *this,
2828 2823
2829#ifdef CONFIG_PROC_FS 2824#ifdef CONFIG_PROC_FS
2830 2825
2831struct rt6_proc_arg
2832{
2833 char *buffer;
2834 int offset;
2835 int length;
2836 int skip;
2837 int len;
2838};
2839
2840static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2841{
2842 struct seq_file *m = p_arg;
2843
2844 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2845
2846#ifdef CONFIG_IPV6_SUBTREES
2847 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2848#else
2849 seq_puts(m, "00000000000000000000000000000000 00 ");
2850#endif
2851 if (rt->rt6i_flags & RTF_GATEWAY) {
2852 seq_printf(m, "%pi6", &rt->rt6i_gateway);
2853 } else {
2854 seq_puts(m, "00000000000000000000000000000000");
2855 }
2856 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2857 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2858 rt->dst.__use, rt->rt6i_flags,
2859 rt->dst.dev ? rt->dst.dev->name : "");
2860 return 0;
2861}
2862
2863static int ipv6_route_show(struct seq_file *m, void *v)
2864{
2865 struct net *net = (struct net *)m->private;
2866 fib6_clean_all_ro(net, rt6_info_route, 0, m);
2867 return 0;
2868}
2869
2870static int ipv6_route_open(struct inode *inode, struct file *file)
2871{
2872 return single_open_net(inode, file, ipv6_route_show);
2873}
2874
2875static const struct file_operations ipv6_route_proc_fops = { 2826static const struct file_operations ipv6_route_proc_fops = {
2876 .owner = THIS_MODULE, 2827 .owner = THIS_MODULE,
2877 .open = ipv6_route_open, 2828 .open = ipv6_route_open,
2878 .read = seq_read, 2829 .read = seq_read,
2879 .llseek = seq_lseek, 2830 .llseek = seq_lseek,
2880 .release = single_release_net, 2831 .release = seq_release_net,
2881}; 2832};
2882 2833
2883static int rt6_stats_seq_show(struct seq_file *seq, void *v) 2834static int rt6_stats_seq_show(struct seq_file *seq, void *v)
diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c
index 19269453a8ea..d3005b34476a 100644
--- a/net/ipv6/sit.c
+++ b/net/ipv6/sit.c
@@ -478,14 +478,44 @@ static void ipip6_tunnel_uninit(struct net_device *dev)
478 dev_put(dev); 478 dev_put(dev);
479} 479}
480 480
481/* Generate icmpv6 with type/code ICMPV6_DEST_UNREACH/ICMPV6_ADDR_UNREACH
482 * if sufficient data bytes are available
483 */
484static int ipip6_err_gen_icmpv6_unreach(struct sk_buff *skb)
485{
486 const struct iphdr *iph = (const struct iphdr *) skb->data;
487 struct rt6_info *rt;
488 struct sk_buff *skb2;
489
490 if (!pskb_may_pull(skb, iph->ihl * 4 + sizeof(struct ipv6hdr) + 8))
491 return 1;
492
493 skb2 = skb_clone(skb, GFP_ATOMIC);
494
495 if (!skb2)
496 return 1;
497
498 skb_dst_drop(skb2);
499 skb_pull(skb2, iph->ihl * 4);
500 skb_reset_network_header(skb2);
501
502 rt = rt6_lookup(dev_net(skb->dev), &ipv6_hdr(skb2)->saddr, NULL, 0, 0);
503
504 if (rt && rt->dst.dev)
505 skb2->dev = rt->dst.dev;
506
507 icmpv6_send(skb2, ICMPV6_DEST_UNREACH, ICMPV6_ADDR_UNREACH, 0);
508
509 if (rt)
510 ip6_rt_put(rt);
511
512 kfree_skb(skb2);
513
514 return 0;
515}
481 516
482static int ipip6_err(struct sk_buff *skb, u32 info) 517static int ipip6_err(struct sk_buff *skb, u32 info)
483{ 518{
484
485/* All the routers (except for Linux) return only
486 8 bytes of packet payload. It means, that precise relaying of
487 ICMP in the real Internet is absolutely infeasible.
488 */
489 const struct iphdr *iph = (const struct iphdr *)skb->data; 519 const struct iphdr *iph = (const struct iphdr *)skb->data;
490 const int type = icmp_hdr(skb)->type; 520 const int type = icmp_hdr(skb)->type;
491 const int code = icmp_hdr(skb)->code; 521 const int code = icmp_hdr(skb)->code;
@@ -500,7 +530,6 @@ static int ipip6_err(struct sk_buff *skb, u32 info)
500 case ICMP_DEST_UNREACH: 530 case ICMP_DEST_UNREACH:
501 switch (code) { 531 switch (code) {
502 case ICMP_SR_FAILED: 532 case ICMP_SR_FAILED:
503 case ICMP_PORT_UNREACH:
504 /* Impossible event. */ 533 /* Impossible event. */
505 return 0; 534 return 0;
506 default: 535 default:
@@ -545,6 +574,9 @@ static int ipip6_err(struct sk_buff *skb, u32 info)
545 goto out; 574 goto out;
546 575
547 err = 0; 576 err = 0;
577 if (!ipip6_err_gen_icmpv6_unreach(skb))
578 goto out;
579
548 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED) 580 if (t->parms.iph.ttl == 0 && type == ICMP_TIME_EXCEEDED)
549 goto out; 581 goto out;
550 582
@@ -670,8 +702,10 @@ static int ipip6_rcv(struct sk_buff *skb)
670 } 702 }
671 703
672 tstats = this_cpu_ptr(tunnel->dev->tstats); 704 tstats = this_cpu_ptr(tunnel->dev->tstats);
705 u64_stats_update_begin(&tstats->syncp);
673 tstats->rx_packets++; 706 tstats->rx_packets++;
674 tstats->rx_bytes += skb->len; 707 tstats->rx_bytes += skb->len;
708 u64_stats_update_end(&tstats->syncp);
675 709
676 netif_rx(skb); 710 netif_rx(skb);
677 711
@@ -892,7 +926,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
892 if (tunnel->parms.iph.daddr && skb_dst(skb)) 926 if (tunnel->parms.iph.daddr && skb_dst(skb))
893 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu); 927 skb_dst(skb)->ops->update_pmtu(skb_dst(skb), NULL, skb, mtu);
894 928
895 if (skb->len > mtu) { 929 if (skb->len > mtu && !skb_is_gso(skb)) {
896 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu); 930 icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
897 ip_rt_put(rt); 931 ip_rt_put(rt);
898 goto tx_error; 932 goto tx_error;
@@ -919,7 +953,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
919 if (!new_skb) { 953 if (!new_skb) {
920 ip_rt_put(rt); 954 ip_rt_put(rt);
921 dev->stats.tx_dropped++; 955 dev->stats.tx_dropped++;
922 dev_kfree_skb(skb); 956 kfree_skb(skb);
923 return NETDEV_TX_OK; 957 return NETDEV_TX_OK;
924 } 958 }
925 if (skb->sk) 959 if (skb->sk)
@@ -933,9 +967,10 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
933 ttl = iph6->hop_limit; 967 ttl = iph6->hop_limit;
934 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); 968 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
935 969
936 if (likely(!skb->encapsulation)) { 970 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_SIT);
937 skb_reset_inner_headers(skb); 971 if (IS_ERR(skb)) {
938 skb->encapsulation = 1; 972 ip_rt_put(rt);
973 goto out;
939 } 974 }
940 975
941 err = iptunnel_xmit(rt, skb, fl4.saddr, fl4.daddr, IPPROTO_IPV6, tos, 976 err = iptunnel_xmit(rt, skb, fl4.saddr, fl4.daddr, IPPROTO_IPV6, tos,
@@ -946,8 +981,9 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
946tx_error_icmp: 981tx_error_icmp:
947 dst_link_failure(skb); 982 dst_link_failure(skb);
948tx_error: 983tx_error:
984 kfree_skb(skb);
985out:
949 dev->stats.tx_errors++; 986 dev->stats.tx_errors++;
950 dev_kfree_skb(skb);
951 return NETDEV_TX_OK; 987 return NETDEV_TX_OK;
952} 988}
953 989
@@ -956,13 +992,15 @@ static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
956 struct ip_tunnel *tunnel = netdev_priv(dev); 992 struct ip_tunnel *tunnel = netdev_priv(dev);
957 const struct iphdr *tiph = &tunnel->parms.iph; 993 const struct iphdr *tiph = &tunnel->parms.iph;
958 994
959 if (likely(!skb->encapsulation)) { 995 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
960 skb_reset_inner_headers(skb); 996 if (IS_ERR(skb))
961 skb->encapsulation = 1; 997 goto out;
962 }
963 998
964 ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP); 999 ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
965 return NETDEV_TX_OK; 1000 return NETDEV_TX_OK;
1001out:
1002 dev->stats.tx_errors++;
1003 return NETDEV_TX_OK;
966} 1004}
967 1005
968static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb, 1006static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
@@ -983,7 +1021,7 @@ static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
983 1021
984tx_err: 1022tx_err:
985 dev->stats.tx_errors++; 1023 dev->stats.tx_errors++;
986 dev_kfree_skb(skb); 1024 kfree_skb(skb);
987 return NETDEV_TX_OK; 1025 return NETDEV_TX_OK;
988 1026
989} 1027}
@@ -1292,6 +1330,12 @@ static void ipip6_dev_free(struct net_device *dev)
1292 free_netdev(dev); 1330 free_netdev(dev);
1293} 1331}
1294 1332
1333#define SIT_FEATURES (NETIF_F_SG | \
1334 NETIF_F_FRAGLIST | \
1335 NETIF_F_HIGHDMA | \
1336 NETIF_F_GSO_SOFTWARE | \
1337 NETIF_F_HW_CSUM)
1338
1295static void ipip6_tunnel_setup(struct net_device *dev) 1339static void ipip6_tunnel_setup(struct net_device *dev)
1296{ 1340{
1297 dev->netdev_ops = &ipip6_netdev_ops; 1341 dev->netdev_ops = &ipip6_netdev_ops;
@@ -1305,11 +1349,14 @@ static void ipip6_tunnel_setup(struct net_device *dev)
1305 dev->iflink = 0; 1349 dev->iflink = 0;
1306 dev->addr_len = 4; 1350 dev->addr_len = 4;
1307 dev->features |= NETIF_F_LLTX; 1351 dev->features |= NETIF_F_LLTX;
1352 dev->features |= SIT_FEATURES;
1353 dev->hw_features |= SIT_FEATURES;
1308} 1354}
1309 1355
1310static int ipip6_tunnel_init(struct net_device *dev) 1356static int ipip6_tunnel_init(struct net_device *dev)
1311{ 1357{
1312 struct ip_tunnel *tunnel = netdev_priv(dev); 1358 struct ip_tunnel *tunnel = netdev_priv(dev);
1359 int i;
1313 1360
1314 tunnel->dev = dev; 1361 tunnel->dev = dev;
1315 tunnel->net = dev_net(dev); 1362 tunnel->net = dev_net(dev);
@@ -1322,6 +1369,12 @@ static int ipip6_tunnel_init(struct net_device *dev)
1322 if (!dev->tstats) 1369 if (!dev->tstats)
1323 return -ENOMEM; 1370 return -ENOMEM;
1324 1371
1372 for_each_possible_cpu(i) {
1373 struct pcpu_tstats *ipip6_tunnel_stats;
1374 ipip6_tunnel_stats = per_cpu_ptr(dev->tstats, i);
1375 u64_stats_init(&ipip6_tunnel_stats->syncp);
1376 }
1377
1325 return 0; 1378 return 0;
1326} 1379}
1327 1380
@@ -1331,6 +1384,7 @@ static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1331 struct iphdr *iph = &tunnel->parms.iph; 1384 struct iphdr *iph = &tunnel->parms.iph;
1332 struct net *net = dev_net(dev); 1385 struct net *net = dev_net(dev);
1333 struct sit_net *sitn = net_generic(net, sit_net_id); 1386 struct sit_net *sitn = net_generic(net, sit_net_id);
1387 int i;
1334 1388
1335 tunnel->dev = dev; 1389 tunnel->dev = dev;
1336 tunnel->net = dev_net(dev); 1390 tunnel->net = dev_net(dev);
@@ -1344,6 +1398,13 @@ static int __net_init ipip6_fb_tunnel_init(struct net_device *dev)
1344 dev->tstats = alloc_percpu(struct pcpu_tstats); 1398 dev->tstats = alloc_percpu(struct pcpu_tstats);
1345 if (!dev->tstats) 1399 if (!dev->tstats)
1346 return -ENOMEM; 1400 return -ENOMEM;
1401
1402 for_each_possible_cpu(i) {
1403 struct pcpu_tstats *ipip6_fb_stats;
1404 ipip6_fb_stats = per_cpu_ptr(dev->tstats, i);
1405 u64_stats_init(&ipip6_fb_stats->syncp);
1406 }
1407
1347 dev_hold(dev); 1408 dev_hold(dev);
1348 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel); 1409 rcu_assign_pointer(sitn->tunnels_wc[0], tunnel);
1349 return 0; 1410 return 0;
@@ -1594,6 +1655,15 @@ static const struct nla_policy ipip6_policy[IFLA_IPTUN_MAX + 1] = {
1594#endif 1655#endif
1595}; 1656};
1596 1657
1658static void ipip6_dellink(struct net_device *dev, struct list_head *head)
1659{
1660 struct net *net = dev_net(dev);
1661 struct sit_net *sitn = net_generic(net, sit_net_id);
1662
1663 if (dev != sitn->fb_tunnel_dev)
1664 unregister_netdevice_queue(dev, head);
1665}
1666
1597static struct rtnl_link_ops sit_link_ops __read_mostly = { 1667static struct rtnl_link_ops sit_link_ops __read_mostly = {
1598 .kind = "sit", 1668 .kind = "sit",
1599 .maxtype = IFLA_IPTUN_MAX, 1669 .maxtype = IFLA_IPTUN_MAX,
@@ -1605,6 +1675,7 @@ static struct rtnl_link_ops sit_link_ops __read_mostly = {
1605 .changelink = ipip6_changelink, 1675 .changelink = ipip6_changelink,
1606 .get_size = ipip6_get_size, 1676 .get_size = ipip6_get_size,
1607 .fill_info = ipip6_fill_info, 1677 .fill_info = ipip6_fill_info,
1678 .dellink = ipip6_dellink,
1608}; 1679};
1609 1680
1610static struct xfrm_tunnel sit_handler __read_mostly = { 1681static struct xfrm_tunnel sit_handler __read_mostly = {
@@ -1619,9 +1690,10 @@ static struct xfrm_tunnel ipip_handler __read_mostly = {
1619 .priority = 2, 1690 .priority = 2,
1620}; 1691};
1621 1692
1622static void __net_exit sit_destroy_tunnels(struct sit_net *sitn, struct list_head *head) 1693static void __net_exit sit_destroy_tunnels(struct net *net,
1694 struct list_head *head)
1623{ 1695{
1624 struct net *net = dev_net(sitn->fb_tunnel_dev); 1696 struct sit_net *sitn = net_generic(net, sit_net_id);
1625 struct net_device *dev, *aux; 1697 struct net_device *dev, *aux;
1626 int prio; 1698 int prio;
1627 1699
@@ -1696,11 +1768,10 @@ err_alloc_dev:
1696 1768
1697static void __net_exit sit_exit_net(struct net *net) 1769static void __net_exit sit_exit_net(struct net *net)
1698{ 1770{
1699 struct sit_net *sitn = net_generic(net, sit_net_id);
1700 LIST_HEAD(list); 1771 LIST_HEAD(list);
1701 1772
1702 rtnl_lock(); 1773 rtnl_lock();
1703 sit_destroy_tunnels(sitn, &list); 1774 sit_destroy_tunnels(net, &list);
1704 unregister_netdevice_many(&list); 1775 unregister_netdevice_many(&list);
1705 rtnl_unlock(); 1776 rtnl_unlock();
1706} 1777}
diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c
index bf63ac8a49b9..535a3ad262f1 100644
--- a/net/ipv6/syncookies.c
+++ b/net/ipv6/syncookies.c
@@ -24,26 +24,23 @@
24#define COOKIEBITS 24 /* Upper bits store count */ 24#define COOKIEBITS 24 /* Upper bits store count */
25#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1) 25#define COOKIEMASK (((__u32)1 << COOKIEBITS) - 1)
26 26
27/* Table must be sorted. */ 27static u32 syncookie6_secret[2][16-4+SHA_DIGEST_WORDS];
28
29/* RFC 2460, Section 8.3:
30 * [ipv6 tcp] MSS must be computed as the maximum packet size minus 60 [..]
31 *
32 * Due to IPV6_MIN_MTU=1280 the lowest possible MSS is 1220, which allows
33 * using higher values than ipv4 tcp syncookies.
34 * The other values are chosen based on ethernet (1500 and 9k MTU), plus
35 * one that accounts for common encap (PPPoe) overhead. Table must be sorted.
36 */
28static __u16 const msstab[] = { 37static __u16 const msstab[] = {
29 64, 38 1280 - 60, /* IPV6_MIN_MTU - 60 */
30 512,
31 536,
32 1280 - 60,
33 1480 - 60, 39 1480 - 60,
34 1500 - 60, 40 1500 - 60,
35 4460 - 60,
36 9000 - 60, 41 9000 - 60,
37}; 42};
38 43
39/*
40 * This (misnamed) value is the age of syncookie which is permitted.
41 * Its ideal value should be dependent on TCP_TIMEOUT_INIT and
42 * sysctl_tcp_retries1. It's a rather complicated formula (exponential
43 * backoff) to compute at runtime so it's currently hardcoded here.
44 */
45#define COUNTER_TRIES 4
46
47static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb, 44static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
48 struct request_sock *req, 45 struct request_sock *req,
49 struct dst_entry *dst) 46 struct dst_entry *dst)
@@ -66,14 +63,18 @@ static DEFINE_PER_CPU(__u32 [16 + 5 + SHA_WORKSPACE_WORDS],
66static u32 cookie_hash(const struct in6_addr *saddr, const struct in6_addr *daddr, 63static u32 cookie_hash(const struct in6_addr *saddr, const struct in6_addr *daddr,
67 __be16 sport, __be16 dport, u32 count, int c) 64 __be16 sport, __be16 dport, u32 count, int c)
68{ 65{
69 __u32 *tmp = __get_cpu_var(ipv6_cookie_scratch); 66 __u32 *tmp;
67
68 net_get_random_once(syncookie6_secret, sizeof(syncookie6_secret));
69
70 tmp = __get_cpu_var(ipv6_cookie_scratch);
70 71
71 /* 72 /*
72 * we have 320 bits of information to hash, copy in the remaining 73 * we have 320 bits of information to hash, copy in the remaining
73 * 192 bits required for sha_transform, from the syncookie_secret 74 * 192 bits required for sha_transform, from the syncookie6_secret
74 * and overwrite the digest with the secret 75 * and overwrite the digest with the secret
75 */ 76 */
76 memcpy(tmp + 10, syncookie_secret[c], 44); 77 memcpy(tmp + 10, syncookie6_secret[c], 44);
77 memcpy(tmp, saddr, 16); 78 memcpy(tmp, saddr, 16);
78 memcpy(tmp + 4, daddr, 16); 79 memcpy(tmp + 4, daddr, 16);
79 tmp[8] = ((__force u32)sport << 16) + (__force u32)dport; 80 tmp[8] = ((__force u32)sport << 16) + (__force u32)dport;
@@ -86,8 +87,9 @@ static u32 cookie_hash(const struct in6_addr *saddr, const struct in6_addr *dadd
86static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr, 87static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
87 const struct in6_addr *daddr, 88 const struct in6_addr *daddr,
88 __be16 sport, __be16 dport, __u32 sseq, 89 __be16 sport, __be16 dport, __u32 sseq,
89 __u32 count, __u32 data) 90 __u32 data)
90{ 91{
92 u32 count = tcp_cookie_time();
91 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) + 93 return (cookie_hash(saddr, daddr, sport, dport, 0, 0) +
92 sseq + (count << COOKIEBITS) + 94 sseq + (count << COOKIEBITS) +
93 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data) 95 ((cookie_hash(saddr, daddr, sport, dport, count, 1) + data)
@@ -96,15 +98,14 @@ static __u32 secure_tcp_syn_cookie(const struct in6_addr *saddr,
96 98
97static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr, 99static __u32 check_tcp_syn_cookie(__u32 cookie, const struct in6_addr *saddr,
98 const struct in6_addr *daddr, __be16 sport, 100 const struct in6_addr *daddr, __be16 sport,
99 __be16 dport, __u32 sseq, __u32 count, 101 __be16 dport, __u32 sseq)
100 __u32 maxdiff)
101{ 102{
102 __u32 diff; 103 __u32 diff, count = tcp_cookie_time();
103 104
104 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq; 105 cookie -= cookie_hash(saddr, daddr, sport, dport, 0, 0) + sseq;
105 106
106 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS); 107 diff = (count - (cookie >> COOKIEBITS)) & ((__u32) -1 >> COOKIEBITS);
107 if (diff >= maxdiff) 108 if (diff >= MAX_SYNCOOKIE_AGE)
108 return (__u32)-1; 109 return (__u32)-1;
109 110
110 return (cookie - 111 return (cookie -
@@ -125,8 +126,7 @@ u32 __cookie_v6_init_sequence(const struct ipv6hdr *iph,
125 *mssp = msstab[mssind]; 126 *mssp = msstab[mssind];
126 127
127 return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source, 128 return secure_tcp_syn_cookie(&iph->saddr, &iph->daddr, th->source,
128 th->dest, ntohl(th->seq), 129 th->dest, ntohl(th->seq), mssind);
129 jiffies / (HZ * 60), mssind);
130} 130}
131EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence); 131EXPORT_SYMBOL_GPL(__cookie_v6_init_sequence);
132 132
@@ -146,8 +146,7 @@ int __cookie_v6_check(const struct ipv6hdr *iph, const struct tcphdr *th,
146{ 146{
147 __u32 seq = ntohl(th->seq) - 1; 147 __u32 seq = ntohl(th->seq) - 1;
148 __u32 mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr, 148 __u32 mssind = check_tcp_syn_cookie(cookie, &iph->saddr, &iph->daddr,
149 th->source, th->dest, seq, 149 th->source, th->dest, seq);
150 jiffies / (HZ * 60), COUNTER_TRIES);
151 150
152 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0; 151 return mssind < ARRAY_SIZE(msstab) ? msstab[mssind] : 0;
153} 152}
@@ -157,7 +156,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
157{ 156{
158 struct tcp_options_received tcp_opt; 157 struct tcp_options_received tcp_opt;
159 struct inet_request_sock *ireq; 158 struct inet_request_sock *ireq;
160 struct inet6_request_sock *ireq6;
161 struct tcp_request_sock *treq; 159 struct tcp_request_sock *treq;
162 struct ipv6_pinfo *np = inet6_sk(sk); 160 struct ipv6_pinfo *np = inet6_sk(sk);
163 struct tcp_sock *tp = tcp_sk(sk); 161 struct tcp_sock *tp = tcp_sk(sk);
@@ -194,7 +192,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
194 goto out; 192 goto out;
195 193
196 ireq = inet_rsk(req); 194 ireq = inet_rsk(req);
197 ireq6 = inet6_rsk(req);
198 treq = tcp_rsk(req); 195 treq = tcp_rsk(req);
199 treq->listener = NULL; 196 treq->listener = NULL;
200 197
@@ -202,22 +199,22 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
202 goto out_free; 199 goto out_free;
203 200
204 req->mss = mss; 201 req->mss = mss;
205 ireq->rmt_port = th->source; 202 ireq->ir_rmt_port = th->source;
206 ireq->loc_port = th->dest; 203 ireq->ir_num = ntohs(th->dest);
207 ireq6->rmt_addr = ipv6_hdr(skb)->saddr; 204 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
208 ireq6->loc_addr = ipv6_hdr(skb)->daddr; 205 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
209 if (ipv6_opt_accepted(sk, skb) || 206 if (ipv6_opt_accepted(sk, skb) ||
210 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || 207 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
211 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) { 208 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
212 atomic_inc(&skb->users); 209 atomic_inc(&skb->users);
213 ireq6->pktopts = skb; 210 ireq->pktopts = skb;
214 } 211 }
215 212
216 ireq6->iif = sk->sk_bound_dev_if; 213 ireq->ir_iif = sk->sk_bound_dev_if;
217 /* So that link locals have meaning */ 214 /* So that link locals have meaning */
218 if (!sk->sk_bound_dev_if && 215 if (!sk->sk_bound_dev_if &&
219 ipv6_addr_type(&ireq6->rmt_addr) & IPV6_ADDR_LINKLOCAL) 216 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
220 ireq6->iif = inet6_iif(skb); 217 ireq->ir_iif = inet6_iif(skb);
221 218
222 req->expires = 0UL; 219 req->expires = 0UL;
223 req->num_retrans = 0; 220 req->num_retrans = 0;
@@ -241,12 +238,12 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
241 struct flowi6 fl6; 238 struct flowi6 fl6;
242 memset(&fl6, 0, sizeof(fl6)); 239 memset(&fl6, 0, sizeof(fl6));
243 fl6.flowi6_proto = IPPROTO_TCP; 240 fl6.flowi6_proto = IPPROTO_TCP;
244 fl6.daddr = ireq6->rmt_addr; 241 fl6.daddr = ireq->ir_v6_rmt_addr;
245 final_p = fl6_update_dst(&fl6, np->opt, &final); 242 final_p = fl6_update_dst(&fl6, np->opt, &final);
246 fl6.saddr = ireq6->loc_addr; 243 fl6.saddr = ireq->ir_v6_loc_addr;
247 fl6.flowi6_oif = sk->sk_bound_dev_if; 244 fl6.flowi6_oif = sk->sk_bound_dev_if;
248 fl6.flowi6_mark = sk->sk_mark; 245 fl6.flowi6_mark = sk->sk_mark;
249 fl6.fl6_dport = inet_rsk(req)->rmt_port; 246 fl6.fl6_dport = ireq->ir_rmt_port;
250 fl6.fl6_sport = inet_sk(sk)->inet_sport; 247 fl6.fl6_sport = inet_sk(sk)->inet_sport;
251 security_req_classify_flow(req, flowi6_to_flowi(&fl6)); 248 security_req_classify_flow(req, flowi6_to_flowi(&fl6));
252 249
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 5c71501fc917..f67033b4bb66 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -156,7 +156,6 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
156 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 156 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
157 if (flowlabel == NULL) 157 if (flowlabel == NULL)
158 return -EINVAL; 158 return -EINVAL;
159 usin->sin6_addr = flowlabel->dst;
160 fl6_sock_release(flowlabel); 159 fl6_sock_release(flowlabel);
161 } 160 }
162 } 161 }
@@ -192,13 +191,13 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
192 } 191 }
193 192
194 if (tp->rx_opt.ts_recent_stamp && 193 if (tp->rx_opt.ts_recent_stamp &&
195 !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) { 194 !ipv6_addr_equal(&sk->sk_v6_daddr, &usin->sin6_addr)) {
196 tp->rx_opt.ts_recent = 0; 195 tp->rx_opt.ts_recent = 0;
197 tp->rx_opt.ts_recent_stamp = 0; 196 tp->rx_opt.ts_recent_stamp = 0;
198 tp->write_seq = 0; 197 tp->write_seq = 0;
199 } 198 }
200 199
201 np->daddr = usin->sin6_addr; 200 sk->sk_v6_daddr = usin->sin6_addr;
202 np->flow_label = fl6.flowlabel; 201 np->flow_label = fl6.flowlabel;
203 202
204 /* 203 /*
@@ -237,17 +236,17 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
237 } else { 236 } else {
238 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 237 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
239 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 238 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
240 &np->rcv_saddr); 239 &sk->sk_v6_rcv_saddr);
241 } 240 }
242 241
243 return err; 242 return err;
244 } 243 }
245 244
246 if (!ipv6_addr_any(&np->rcv_saddr)) 245 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
247 saddr = &np->rcv_saddr; 246 saddr = &sk->sk_v6_rcv_saddr;
248 247
249 fl6.flowi6_proto = IPPROTO_TCP; 248 fl6.flowi6_proto = IPPROTO_TCP;
250 fl6.daddr = np->daddr; 249 fl6.daddr = sk->sk_v6_daddr;
251 fl6.saddr = saddr ? *saddr : np->saddr; 250 fl6.saddr = saddr ? *saddr : np->saddr;
252 fl6.flowi6_oif = sk->sk_bound_dev_if; 251 fl6.flowi6_oif = sk->sk_bound_dev_if;
253 fl6.flowi6_mark = sk->sk_mark; 252 fl6.flowi6_mark = sk->sk_mark;
@@ -266,7 +265,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
266 265
267 if (saddr == NULL) { 266 if (saddr == NULL) {
268 saddr = &fl6.saddr; 267 saddr = &fl6.saddr;
269 np->rcv_saddr = *saddr; 268 sk->sk_v6_rcv_saddr = *saddr;
270 } 269 }
271 270
272 /* set the source address */ 271 /* set the source address */
@@ -279,7 +278,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
279 rt = (struct rt6_info *) dst; 278 rt = (struct rt6_info *) dst;
280 if (tcp_death_row.sysctl_tw_recycle && 279 if (tcp_death_row.sysctl_tw_recycle &&
281 !tp->rx_opt.ts_recent_stamp && 280 !tp->rx_opt.ts_recent_stamp &&
282 ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) 281 ipv6_addr_equal(&rt->rt6i_dst.addr, &sk->sk_v6_daddr))
283 tcp_fetch_timewait_stamp(sk, dst); 282 tcp_fetch_timewait_stamp(sk, dst);
284 283
285 icsk->icsk_ext_hdr_len = 0; 284 icsk->icsk_ext_hdr_len = 0;
@@ -298,7 +297,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
298 297
299 if (!tp->write_seq && likely(!tp->repair)) 298 if (!tp->write_seq && likely(!tp->repair))
300 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32, 299 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
301 np->daddr.s6_addr32, 300 sk->sk_v6_daddr.s6_addr32,
302 inet->inet_sport, 301 inet->inet_sport,
303 inet->inet_dport); 302 inet->inet_dport);
304 303
@@ -465,7 +464,7 @@ static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
465 struct request_sock *req, 464 struct request_sock *req,
466 u16 queue_mapping) 465 u16 queue_mapping)
467{ 466{
468 struct inet6_request_sock *treq = inet6_rsk(req); 467 struct inet_request_sock *ireq = inet_rsk(req);
469 struct ipv6_pinfo *np = inet6_sk(sk); 468 struct ipv6_pinfo *np = inet6_sk(sk);
470 struct sk_buff * skb; 469 struct sk_buff * skb;
471 int err = -ENOMEM; 470 int err = -ENOMEM;
@@ -477,9 +476,10 @@ static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
477 skb = tcp_make_synack(sk, dst, req, NULL); 476 skb = tcp_make_synack(sk, dst, req, NULL);
478 477
479 if (skb) { 478 if (skb) {
480 __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr); 479 __tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
480 &ireq->ir_v6_rmt_addr);
481 481
482 fl6->daddr = treq->rmt_addr; 482 fl6->daddr = ireq->ir_v6_rmt_addr;
483 skb_set_queue_mapping(skb, queue_mapping); 483 skb_set_queue_mapping(skb, queue_mapping);
484 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass); 484 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
485 err = net_xmit_eval(err); 485 err = net_xmit_eval(err);
@@ -502,7 +502,7 @@ static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req)
502 502
503static void tcp_v6_reqsk_destructor(struct request_sock *req) 503static void tcp_v6_reqsk_destructor(struct request_sock *req)
504{ 504{
505 kfree_skb(inet6_rsk(req)->pktopts); 505 kfree_skb(inet_rsk(req)->pktopts);
506} 506}
507 507
508#ifdef CONFIG_TCP_MD5SIG 508#ifdef CONFIG_TCP_MD5SIG
@@ -515,13 +515,13 @@ static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
515static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk, 515static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
516 struct sock *addr_sk) 516 struct sock *addr_sk)
517{ 517{
518 return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr); 518 return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr);
519} 519}
520 520
521static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk, 521static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
522 struct request_sock *req) 522 struct request_sock *req)
523{ 523{
524 return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr); 524 return tcp_v6_md5_do_lookup(sk, &inet_rsk(req)->ir_v6_rmt_addr);
525} 525}
526 526
527static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval, 527static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
@@ -621,10 +621,10 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
621 621
622 if (sk) { 622 if (sk) {
623 saddr = &inet6_sk(sk)->saddr; 623 saddr = &inet6_sk(sk)->saddr;
624 daddr = &inet6_sk(sk)->daddr; 624 daddr = &sk->sk_v6_daddr;
625 } else if (req) { 625 } else if (req) {
626 saddr = &inet6_rsk(req)->loc_addr; 626 saddr = &inet_rsk(req)->ir_v6_loc_addr;
627 daddr = &inet6_rsk(req)->rmt_addr; 627 daddr = &inet_rsk(req)->ir_v6_rmt_addr;
628 } else { 628 } else {
629 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 629 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
630 saddr = &ip6h->saddr; 630 saddr = &ip6h->saddr;
@@ -949,7 +949,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
949{ 949{
950 struct tcp_options_received tmp_opt; 950 struct tcp_options_received tmp_opt;
951 struct request_sock *req; 951 struct request_sock *req;
952 struct inet6_request_sock *treq; 952 struct inet_request_sock *ireq;
953 struct ipv6_pinfo *np = inet6_sk(sk); 953 struct ipv6_pinfo *np = inet6_sk(sk);
954 struct tcp_sock *tp = tcp_sk(sk); 954 struct tcp_sock *tp = tcp_sk(sk);
955 __u32 isn = TCP_SKB_CB(skb)->when; 955 __u32 isn = TCP_SKB_CB(skb)->when;
@@ -994,25 +994,25 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
994 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; 994 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
995 tcp_openreq_init(req, &tmp_opt, skb); 995 tcp_openreq_init(req, &tmp_opt, skb);
996 996
997 treq = inet6_rsk(req); 997 ireq = inet_rsk(req);
998 treq->rmt_addr = ipv6_hdr(skb)->saddr; 998 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
999 treq->loc_addr = ipv6_hdr(skb)->daddr; 999 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
1000 if (!want_cookie || tmp_opt.tstamp_ok) 1000 if (!want_cookie || tmp_opt.tstamp_ok)
1001 TCP_ECN_create_request(req, skb, sock_net(sk)); 1001 TCP_ECN_create_request(req, skb, sock_net(sk));
1002 1002
1003 treq->iif = sk->sk_bound_dev_if; 1003 ireq->ir_iif = sk->sk_bound_dev_if;
1004 1004
1005 /* So that link locals have meaning */ 1005 /* So that link locals have meaning */
1006 if (!sk->sk_bound_dev_if && 1006 if (!sk->sk_bound_dev_if &&
1007 ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL) 1007 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
1008 treq->iif = inet6_iif(skb); 1008 ireq->ir_iif = inet6_iif(skb);
1009 1009
1010 if (!isn) { 1010 if (!isn) {
1011 if (ipv6_opt_accepted(sk, skb) || 1011 if (ipv6_opt_accepted(sk, skb) ||
1012 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || 1012 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
1013 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) { 1013 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
1014 atomic_inc(&skb->users); 1014 atomic_inc(&skb->users);
1015 treq->pktopts = skb; 1015 ireq->pktopts = skb;
1016 } 1016 }
1017 1017
1018 if (want_cookie) { 1018 if (want_cookie) {
@@ -1051,7 +1051,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1051 * to the moment of synflood. 1051 * to the moment of synflood.
1052 */ 1052 */
1053 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n", 1053 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
1054 &treq->rmt_addr, ntohs(tcp_hdr(skb)->source)); 1054 &ireq->ir_v6_rmt_addr, ntohs(tcp_hdr(skb)->source));
1055 goto drop_and_release; 1055 goto drop_and_release;
1056 } 1056 }
1057 1057
@@ -1086,7 +1086,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1086 struct request_sock *req, 1086 struct request_sock *req,
1087 struct dst_entry *dst) 1087 struct dst_entry *dst)
1088{ 1088{
1089 struct inet6_request_sock *treq; 1089 struct inet_request_sock *ireq;
1090 struct ipv6_pinfo *newnp, *np = inet6_sk(sk); 1090 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1091 struct tcp6_sock *newtcp6sk; 1091 struct tcp6_sock *newtcp6sk;
1092 struct inet_sock *newinet; 1092 struct inet_sock *newinet;
@@ -1116,11 +1116,11 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1116 1116
1117 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 1117 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1118 1118
1119 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr); 1119 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
1120 1120
1121 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); 1121 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1122 1122
1123 newnp->rcv_saddr = newnp->saddr; 1123 newsk->sk_v6_rcv_saddr = newnp->saddr;
1124 1124
1125 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; 1125 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1126 newsk->sk_backlog_rcv = tcp_v4_do_rcv; 1126 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
@@ -1151,7 +1151,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1151 return newsk; 1151 return newsk;
1152 } 1152 }
1153 1153
1154 treq = inet6_rsk(req); 1154 ireq = inet_rsk(req);
1155 1155
1156 if (sk_acceptq_is_full(sk)) 1156 if (sk_acceptq_is_full(sk))
1157 goto out_overflow; 1157 goto out_overflow;
@@ -1185,10 +1185,10 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1185 1185
1186 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 1186 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1187 1187
1188 newnp->daddr = treq->rmt_addr; 1188 newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
1189 newnp->saddr = treq->loc_addr; 1189 newnp->saddr = ireq->ir_v6_loc_addr;
1190 newnp->rcv_saddr = treq->loc_addr; 1190 newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
1191 newsk->sk_bound_dev_if = treq->iif; 1191 newsk->sk_bound_dev_if = ireq->ir_iif;
1192 1192
1193 /* Now IPv6 options... 1193 /* Now IPv6 options...
1194 1194
@@ -1203,11 +1203,11 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1203 1203
1204 /* Clone pktoptions received with SYN */ 1204 /* Clone pktoptions received with SYN */
1205 newnp->pktoptions = NULL; 1205 newnp->pktoptions = NULL;
1206 if (treq->pktopts != NULL) { 1206 if (ireq->pktopts != NULL) {
1207 newnp->pktoptions = skb_clone(treq->pktopts, 1207 newnp->pktoptions = skb_clone(ireq->pktopts,
1208 sk_gfp_atomic(sk, GFP_ATOMIC)); 1208 sk_gfp_atomic(sk, GFP_ATOMIC));
1209 consume_skb(treq->pktopts); 1209 consume_skb(ireq->pktopts);
1210 treq->pktopts = NULL; 1210 ireq->pktopts = NULL;
1211 if (newnp->pktoptions) 1211 if (newnp->pktoptions)
1212 skb_set_owner_r(newnp->pktoptions, newsk); 1212 skb_set_owner_r(newnp->pktoptions, newsk);
1213 } 1213 }
@@ -1244,13 +1244,13 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1244 1244
1245#ifdef CONFIG_TCP_MD5SIG 1245#ifdef CONFIG_TCP_MD5SIG
1246 /* Copy over the MD5 key from the original socket */ 1246 /* Copy over the MD5 key from the original socket */
1247 if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) { 1247 if ((key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr)) != NULL) {
1248 /* We're using one, so create a matching key 1248 /* We're using one, so create a matching key
1249 * on the newsk structure. If we fail to get 1249 * on the newsk structure. If we fail to get
1250 * memory, then we end up not copying the key 1250 * memory, then we end up not copying the key
1251 * across. Shucks. 1251 * across. Shucks.
1252 */ 1252 */
1253 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newnp->daddr, 1253 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr,
1254 AF_INET6, key->key, key->keylen, 1254 AF_INET6, key->key, key->keylen,
1255 sk_gfp_atomic(sk, GFP_ATOMIC)); 1255 sk_gfp_atomic(sk, GFP_ATOMIC));
1256 } 1256 }
@@ -1722,8 +1722,8 @@ static void get_openreq6(struct seq_file *seq,
1722 const struct sock *sk, struct request_sock *req, int i, kuid_t uid) 1722 const struct sock *sk, struct request_sock *req, int i, kuid_t uid)
1723{ 1723{
1724 int ttd = req->expires - jiffies; 1724 int ttd = req->expires - jiffies;
1725 const struct in6_addr *src = &inet6_rsk(req)->loc_addr; 1725 const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
1726 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr; 1726 const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
1727 1727
1728 if (ttd < 0) 1728 if (ttd < 0)
1729 ttd = 0; 1729 ttd = 0;
@@ -1734,10 +1734,10 @@ static void get_openreq6(struct seq_file *seq,
1734 i, 1734 i,
1735 src->s6_addr32[0], src->s6_addr32[1], 1735 src->s6_addr32[0], src->s6_addr32[1],
1736 src->s6_addr32[2], src->s6_addr32[3], 1736 src->s6_addr32[2], src->s6_addr32[3],
1737 ntohs(inet_rsk(req)->loc_port), 1737 inet_rsk(req)->ir_num,
1738 dest->s6_addr32[0], dest->s6_addr32[1], 1738 dest->s6_addr32[0], dest->s6_addr32[1],
1739 dest->s6_addr32[2], dest->s6_addr32[3], 1739 dest->s6_addr32[2], dest->s6_addr32[3],
1740 ntohs(inet_rsk(req)->rmt_port), 1740 ntohs(inet_rsk(req)->ir_rmt_port),
1741 TCP_SYN_RECV, 1741 TCP_SYN_RECV,
1742 0,0, /* could print option size, but that is af dependent. */ 1742 0,0, /* could print option size, but that is af dependent. */
1743 1, /* timers active (only the expire timer) */ 1743 1, /* timers active (only the expire timer) */
@@ -1758,10 +1758,9 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1758 const struct inet_sock *inet = inet_sk(sp); 1758 const struct inet_sock *inet = inet_sk(sp);
1759 const struct tcp_sock *tp = tcp_sk(sp); 1759 const struct tcp_sock *tp = tcp_sk(sp);
1760 const struct inet_connection_sock *icsk = inet_csk(sp); 1760 const struct inet_connection_sock *icsk = inet_csk(sp);
1761 const struct ipv6_pinfo *np = inet6_sk(sp);
1762 1761
1763 dest = &np->daddr; 1762 dest = &sp->sk_v6_daddr;
1764 src = &np->rcv_saddr; 1763 src = &sp->sk_v6_rcv_saddr;
1765 destp = ntohs(inet->inet_dport); 1764 destp = ntohs(inet->inet_dport);
1766 srcp = ntohs(inet->inet_sport); 1765 srcp = ntohs(inet->inet_sport);
1767 1766
@@ -1810,11 +1809,10 @@ static void get_timewait6_sock(struct seq_file *seq,
1810{ 1809{
1811 const struct in6_addr *dest, *src; 1810 const struct in6_addr *dest, *src;
1812 __u16 destp, srcp; 1811 __u16 destp, srcp;
1813 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw); 1812 s32 delta = tw->tw_ttd - inet_tw_time_stamp();
1814 long delta = tw->tw_ttd - jiffies;
1815 1813
1816 dest = &tw6->tw_v6_daddr; 1814 dest = &tw->tw_v6_daddr;
1817 src = &tw6->tw_v6_rcv_saddr; 1815 src = &tw->tw_v6_rcv_saddr;
1818 destp = ntohs(tw->tw_dport); 1816 destp = ntohs(tw->tw_dport);
1819 srcp = ntohs(tw->tw_sport); 1817 srcp = ntohs(tw->tw_sport);
1820 1818
@@ -1834,6 +1832,7 @@ static void get_timewait6_sock(struct seq_file *seq,
1834static int tcp6_seq_show(struct seq_file *seq, void *v) 1832static int tcp6_seq_show(struct seq_file *seq, void *v)
1835{ 1833{
1836 struct tcp_iter_state *st; 1834 struct tcp_iter_state *st;
1835 struct sock *sk = v;
1837 1836
1838 if (v == SEQ_START_TOKEN) { 1837 if (v == SEQ_START_TOKEN) {
1839 seq_puts(seq, 1838 seq_puts(seq,
@@ -1849,14 +1848,14 @@ static int tcp6_seq_show(struct seq_file *seq, void *v)
1849 switch (st->state) { 1848 switch (st->state) {
1850 case TCP_SEQ_STATE_LISTENING: 1849 case TCP_SEQ_STATE_LISTENING:
1851 case TCP_SEQ_STATE_ESTABLISHED: 1850 case TCP_SEQ_STATE_ESTABLISHED:
1852 get_tcp6_sock(seq, v, st->num); 1851 if (sk->sk_state == TCP_TIME_WAIT)
1852 get_timewait6_sock(seq, v, st->num);
1853 else
1854 get_tcp6_sock(seq, v, st->num);
1853 break; 1855 break;
1854 case TCP_SEQ_STATE_OPENREQ: 1856 case TCP_SEQ_STATE_OPENREQ:
1855 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid); 1857 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
1856 break; 1858 break;
1857 case TCP_SEQ_STATE_TIME_WAIT:
1858 get_timewait6_sock(seq, v, st->num);
1859 break;
1860 } 1859 }
1861out: 1860out:
1862 return 0; 1861 return 0;
@@ -1929,6 +1928,7 @@ struct proto tcpv6_prot = {
1929 .memory_allocated = &tcp_memory_allocated, 1928 .memory_allocated = &tcp_memory_allocated,
1930 .memory_pressure = &tcp_memory_pressure, 1929 .memory_pressure = &tcp_memory_pressure,
1931 .orphan_count = &tcp_orphan_count, 1930 .orphan_count = &tcp_orphan_count,
1931 .sysctl_mem = sysctl_tcp_mem,
1932 .sysctl_wmem = sysctl_tcp_wmem, 1932 .sysctl_wmem = sysctl_tcp_wmem,
1933 .sysctl_rmem = sysctl_tcp_rmem, 1933 .sysctl_rmem = sysctl_tcp_rmem,
1934 .max_header = MAX_TCP_HEADER, 1934 .max_header = MAX_TCP_HEADER,
diff --git a/net/ipv6/tcpv6_offload.c b/net/ipv6/tcpv6_offload.c
index 2ec6bf6a0aa0..6d18157dc32c 100644
--- a/net/ipv6/tcpv6_offload.c
+++ b/net/ipv6/tcpv6_offload.c
@@ -37,34 +37,32 @@ static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
37{ 37{
38 const struct ipv6hdr *iph = skb_gro_network_header(skb); 38 const struct ipv6hdr *iph = skb_gro_network_header(skb);
39 __wsum wsum; 39 __wsum wsum;
40 __sum16 sum; 40
41 /* Don't bother verifying checksum if we're going to flush anyway. */
42 if (NAPI_GRO_CB(skb)->flush)
43 goto skip_csum;
44
45 wsum = skb->csum;
41 46
42 switch (skb->ip_summed) { 47 switch (skb->ip_summed) {
48 case CHECKSUM_NONE:
49 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb),
50 wsum);
51
52 /* fall through */
53
43 case CHECKSUM_COMPLETE: 54 case CHECKSUM_COMPLETE:
44 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr, 55 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
45 skb->csum)) { 56 wsum)) {
46 skb->ip_summed = CHECKSUM_UNNECESSARY; 57 skb->ip_summed = CHECKSUM_UNNECESSARY;
47 break; 58 break;
48 } 59 }
49flush: 60
50 NAPI_GRO_CB(skb)->flush = 1; 61 NAPI_GRO_CB(skb)->flush = 1;
51 return NULL; 62 return NULL;
52
53 case CHECKSUM_NONE:
54 wsum = ~csum_unfold(csum_ipv6_magic(&iph->saddr, &iph->daddr,
55 skb_gro_len(skb),
56 IPPROTO_TCP, 0));
57 sum = csum_fold(skb_checksum(skb,
58 skb_gro_offset(skb),
59 skb_gro_len(skb),
60 wsum));
61 if (sum)
62 goto flush;
63
64 skb->ip_summed = CHECKSUM_UNNECESSARY;
65 break;
66 } 63 }
67 64
65skip_csum:
68 return tcp_gro_receive(head, skb); 66 return tcp_gro_receive(head, skb);
69} 67}
70 68
@@ -83,7 +81,7 @@ static int tcp6_gro_complete(struct sk_buff *skb)
83static const struct net_offload tcpv6_offload = { 81static const struct net_offload tcpv6_offload = {
84 .callbacks = { 82 .callbacks = {
85 .gso_send_check = tcp_v6_gso_send_check, 83 .gso_send_check = tcp_v6_gso_send_check,
86 .gso_segment = tcp_tso_segment, 84 .gso_segment = tcp_gso_segment,
87 .gro_receive = tcp6_gro_receive, 85 .gro_receive = tcp6_gro_receive,
88 .gro_complete = tcp6_gro_complete, 86 .gro_complete = tcp6_gro_complete,
89 }, 87 },
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index 18786098fd41..089c741a3992 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -53,22 +53,42 @@
53#include <trace/events/skb.h> 53#include <trace/events/skb.h>
54#include "udp_impl.h" 54#include "udp_impl.h"
55 55
56static unsigned int udp6_ehashfn(struct net *net,
57 const struct in6_addr *laddr,
58 const u16 lport,
59 const struct in6_addr *faddr,
60 const __be16 fport)
61{
62 static u32 udp6_ehash_secret __read_mostly;
63 static u32 udp_ipv6_hash_secret __read_mostly;
64
65 u32 lhash, fhash;
66
67 net_get_random_once(&udp6_ehash_secret,
68 sizeof(udp6_ehash_secret));
69 net_get_random_once(&udp_ipv6_hash_secret,
70 sizeof(udp_ipv6_hash_secret));
71
72 lhash = (__force u32)laddr->s6_addr32[3];
73 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
74
75 return __inet6_ehashfn(lhash, lport, fhash, fport,
76 udp_ipv6_hash_secret + net_hash_mix(net));
77}
78
56int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2) 79int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
57{ 80{
58 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
59 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2); 81 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
60 __be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
61 __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
62 int sk_ipv6only = ipv6_only_sock(sk); 82 int sk_ipv6only = ipv6_only_sock(sk);
63 int sk2_ipv6only = inet_v6_ipv6only(sk2); 83 int sk2_ipv6only = inet_v6_ipv6only(sk2);
64 int addr_type = ipv6_addr_type(sk_rcv_saddr6); 84 int addr_type = ipv6_addr_type(&sk->sk_v6_rcv_saddr);
65 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED; 85 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
66 86
67 /* if both are mapped, treat as IPv4 */ 87 /* if both are mapped, treat as IPv4 */
68 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED) 88 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
69 return (!sk2_ipv6only && 89 return (!sk2_ipv6only &&
70 (!sk1_rcv_saddr || !sk2_rcv_saddr || 90 (!sk->sk_rcv_saddr || !sk2->sk_rcv_saddr ||
71 sk1_rcv_saddr == sk2_rcv_saddr)); 91 sk->sk_rcv_saddr == sk2->sk_rcv_saddr));
72 92
73 if (addr_type2 == IPV6_ADDR_ANY && 93 if (addr_type2 == IPV6_ADDR_ANY &&
74 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED)) 94 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
@@ -79,7 +99,7 @@ int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
79 return 1; 99 return 1;
80 100
81 if (sk2_rcv_saddr6 && 101 if (sk2_rcv_saddr6 &&
82 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6)) 102 ipv6_addr_equal(&sk->sk_v6_rcv_saddr, sk2_rcv_saddr6))
83 return 1; 103 return 1;
84 104
85 return 0; 105 return 0;
@@ -107,7 +127,7 @@ int udp_v6_get_port(struct sock *sk, unsigned short snum)
107 unsigned int hash2_nulladdr = 127 unsigned int hash2_nulladdr =
108 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum); 128 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
109 unsigned int hash2_partial = 129 unsigned int hash2_partial =
110 udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0); 130 udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
111 131
112 /* precompute partial secondary hash */ 132 /* precompute partial secondary hash */
113 udp_sk(sk)->udp_portaddr_hash = hash2_partial; 133 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
@@ -117,7 +137,7 @@ int udp_v6_get_port(struct sock *sk, unsigned short snum)
117static void udp_v6_rehash(struct sock *sk) 137static void udp_v6_rehash(struct sock *sk)
118{ 138{
119 u16 new_hash = udp6_portaddr_hash(sock_net(sk), 139 u16 new_hash = udp6_portaddr_hash(sock_net(sk),
120 &inet6_sk(sk)->rcv_saddr, 140 &sk->sk_v6_rcv_saddr,
121 inet_sk(sk)->inet_num); 141 inet_sk(sk)->inet_num);
122 142
123 udp_lib_rehash(sk, new_hash); 143 udp_lib_rehash(sk, new_hash);
@@ -133,7 +153,6 @@ static inline int compute_score(struct sock *sk, struct net *net,
133 153
134 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 154 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
135 sk->sk_family == PF_INET6) { 155 sk->sk_family == PF_INET6) {
136 struct ipv6_pinfo *np = inet6_sk(sk);
137 struct inet_sock *inet = inet_sk(sk); 156 struct inet_sock *inet = inet_sk(sk);
138 157
139 score = 0; 158 score = 0;
@@ -142,13 +161,13 @@ static inline int compute_score(struct sock *sk, struct net *net,
142 return -1; 161 return -1;
143 score++; 162 score++;
144 } 163 }
145 if (!ipv6_addr_any(&np->rcv_saddr)) { 164 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
146 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 165 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
147 return -1; 166 return -1;
148 score++; 167 score++;
149 } 168 }
150 if (!ipv6_addr_any(&np->daddr)) { 169 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
151 if (!ipv6_addr_equal(&np->daddr, saddr)) 170 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
152 return -1; 171 return -1;
153 score++; 172 score++;
154 } 173 }
@@ -171,10 +190,9 @@ static inline int compute_score2(struct sock *sk, struct net *net,
171 190
172 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum && 191 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
173 sk->sk_family == PF_INET6) { 192 sk->sk_family == PF_INET6) {
174 struct ipv6_pinfo *np = inet6_sk(sk);
175 struct inet_sock *inet = inet_sk(sk); 193 struct inet_sock *inet = inet_sk(sk);
176 194
177 if (!ipv6_addr_equal(&np->rcv_saddr, daddr)) 195 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
178 return -1; 196 return -1;
179 score = 0; 197 score = 0;
180 if (inet->inet_dport) { 198 if (inet->inet_dport) {
@@ -182,8 +200,8 @@ static inline int compute_score2(struct sock *sk, struct net *net,
182 return -1; 200 return -1;
183 score++; 201 score++;
184 } 202 }
185 if (!ipv6_addr_any(&np->daddr)) { 203 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
186 if (!ipv6_addr_equal(&np->daddr, saddr)) 204 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
187 return -1; 205 return -1;
188 score++; 206 score++;
189 } 207 }
@@ -219,8 +237,8 @@ begin:
219 badness = score; 237 badness = score;
220 reuseport = sk->sk_reuseport; 238 reuseport = sk->sk_reuseport;
221 if (reuseport) { 239 if (reuseport) {
222 hash = inet6_ehashfn(net, daddr, hnum, 240 hash = udp6_ehashfn(net, daddr, hnum,
223 saddr, sport); 241 saddr, sport);
224 matches = 1; 242 matches = 1;
225 } else if (score == SCORE2_MAX) 243 } else if (score == SCORE2_MAX)
226 goto exact_match; 244 goto exact_match;
@@ -300,8 +318,8 @@ begin:
300 badness = score; 318 badness = score;
301 reuseport = sk->sk_reuseport; 319 reuseport = sk->sk_reuseport;
302 if (reuseport) { 320 if (reuseport) {
303 hash = inet6_ehashfn(net, daddr, hnum, 321 hash = udp6_ehashfn(net, daddr, hnum,
304 saddr, sport); 322 saddr, sport);
305 matches = 1; 323 matches = 1;
306 } 324 }
307 } else if (score == badness && reuseport) { 325 } else if (score == badness && reuseport) {
@@ -374,14 +392,11 @@ int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
374 int is_udp4; 392 int is_udp4;
375 bool slow; 393 bool slow;
376 394
377 if (addr_len)
378 *addr_len = sizeof(struct sockaddr_in6);
379
380 if (flags & MSG_ERRQUEUE) 395 if (flags & MSG_ERRQUEUE)
381 return ipv6_recv_error(sk, msg, len); 396 return ipv6_recv_error(sk, msg, len, addr_len);
382 397
383 if (np->rxpmtu && np->rxopt.bits.rxpmtu) 398 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
384 return ipv6_recv_rxpmtu(sk, msg, len); 399 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
385 400
386try_again: 401try_again:
387 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0), 402 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
@@ -462,7 +477,7 @@ try_again:
462 ipv6_iface_scope_id(&sin6->sin6_addr, 477 ipv6_iface_scope_id(&sin6->sin6_addr,
463 IP6CB(skb)->iif); 478 IP6CB(skb)->iif);
464 } 479 }
465 480 *addr_len = sizeof(*sin6);
466 } 481 }
467 if (is_udp4) { 482 if (is_udp4) {
468 if (inet->cmsg_flags) 483 if (inet->cmsg_flags)
@@ -551,8 +566,10 @@ static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
551{ 566{
552 int rc; 567 int rc;
553 568
554 if (!ipv6_addr_any(&inet6_sk(sk)->daddr)) 569 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
555 sock_rps_save_rxhash(sk, skb); 570 sock_rps_save_rxhash(sk, skb);
571 sk_mark_napi_id(sk, skb);
572 }
556 573
557 rc = sock_queue_rcv_skb(sk, skb); 574 rc = sock_queue_rcv_skb(sk, skb);
558 if (rc < 0) { 575 if (rc < 0) {
@@ -690,20 +707,19 @@ static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
690 707
691 if (udp_sk(s)->udp_port_hash == num && 708 if (udp_sk(s)->udp_port_hash == num &&
692 s->sk_family == PF_INET6) { 709 s->sk_family == PF_INET6) {
693 struct ipv6_pinfo *np = inet6_sk(s);
694 if (inet->inet_dport) { 710 if (inet->inet_dport) {
695 if (inet->inet_dport != rmt_port) 711 if (inet->inet_dport != rmt_port)
696 continue; 712 continue;
697 } 713 }
698 if (!ipv6_addr_any(&np->daddr) && 714 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
699 !ipv6_addr_equal(&np->daddr, rmt_addr)) 715 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
700 continue; 716 continue;
701 717
702 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) 718 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
703 continue; 719 continue;
704 720
705 if (!ipv6_addr_any(&np->rcv_saddr)) { 721 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
706 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 722 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
707 continue; 723 continue;
708 } 724 }
709 if (!inet6_mc_check(s, loc_addr, rmt_addr)) 725 if (!inet6_mc_check(s, loc_addr, rmt_addr))
@@ -846,7 +862,6 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
846 if (sk != NULL) { 862 if (sk != NULL) {
847 int ret; 863 int ret;
848 864
849 sk_mark_napi_id(sk, skb);
850 ret = udpv6_queue_rcv_skb(sk, skb); 865 ret = udpv6_queue_rcv_skb(sk, skb);
851 sock_put(sk); 866 sock_put(sk);
852 867
@@ -1064,7 +1079,7 @@ int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1064 } else if (!up->pending) { 1079 } else if (!up->pending) {
1065 if (sk->sk_state != TCP_ESTABLISHED) 1080 if (sk->sk_state != TCP_ESTABLISHED)
1066 return -EDESTADDRREQ; 1081 return -EDESTADDRREQ;
1067 daddr = &np->daddr; 1082 daddr = &sk->sk_v6_daddr;
1068 } else 1083 } else
1069 daddr = NULL; 1084 daddr = NULL;
1070 1085
@@ -1125,7 +1140,6 @@ do_udp_sendmsg:
1125 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1140 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1126 if (flowlabel == NULL) 1141 if (flowlabel == NULL)
1127 return -EINVAL; 1142 return -EINVAL;
1128 daddr = &flowlabel->dst;
1129 } 1143 }
1130 } 1144 }
1131 1145
@@ -1134,8 +1148,8 @@ do_udp_sendmsg:
1134 * sk->sk_dst_cache. 1148 * sk->sk_dst_cache.
1135 */ 1149 */
1136 if (sk->sk_state == TCP_ESTABLISHED && 1150 if (sk->sk_state == TCP_ESTABLISHED &&
1137 ipv6_addr_equal(daddr, &np->daddr)) 1151 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1138 daddr = &np->daddr; 1152 daddr = &sk->sk_v6_daddr;
1139 1153
1140 if (addr_len >= sizeof(struct sockaddr_in6) && 1154 if (addr_len >= sizeof(struct sockaddr_in6) &&
1141 sin6->sin6_scope_id && 1155 sin6->sin6_scope_id &&
@@ -1146,7 +1160,7 @@ do_udp_sendmsg:
1146 return -EDESTADDRREQ; 1160 return -EDESTADDRREQ;
1147 1161
1148 fl6.fl6_dport = inet->inet_dport; 1162 fl6.fl6_dport = inet->inet_dport;
1149 daddr = &np->daddr; 1163 daddr = &sk->sk_v6_daddr;
1150 fl6.flowlabel = np->flow_label; 1164 fl6.flowlabel = np->flow_label;
1151 connected = 1; 1165 connected = 1;
1152 } 1166 }
@@ -1261,8 +1275,8 @@ do_append_data:
1261 if (dst) { 1275 if (dst) {
1262 if (connected) { 1276 if (connected) {
1263 ip6_dst_store(sk, dst, 1277 ip6_dst_store(sk, dst,
1264 ipv6_addr_equal(&fl6.daddr, &np->daddr) ? 1278 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1265 &np->daddr : NULL, 1279 &sk->sk_v6_daddr : NULL,
1266#ifdef CONFIG_IPV6_SUBTREES 1280#ifdef CONFIG_IPV6_SUBTREES
1267 ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 1281 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1268 &np->saddr : 1282 &np->saddr :
diff --git a/net/ipv6/udp_impl.h b/net/ipv6/udp_impl.h
index 4691ed50a928..c779c3c90b9d 100644
--- a/net/ipv6/udp_impl.h
+++ b/net/ipv6/udp_impl.h
@@ -7,33 +7,32 @@
7#include <net/inet_common.h> 7#include <net/inet_common.h>
8#include <net/transp_v6.h> 8#include <net/transp_v6.h>
9 9
10extern int __udp6_lib_rcv(struct sk_buff *, struct udp_table *, int ); 10int __udp6_lib_rcv(struct sk_buff *, struct udp_table *, int);
11extern void __udp6_lib_err(struct sk_buff *, struct inet6_skb_parm *, 11void __udp6_lib_err(struct sk_buff *, struct inet6_skb_parm *, u8, u8, int,
12 u8 , u8 , int , __be32 , struct udp_table *); 12 __be32, struct udp_table *);
13 13
14extern int udp_v6_get_port(struct sock *sk, unsigned short snum); 14int udp_v6_get_port(struct sock *sk, unsigned short snum);
15 15
16extern int udpv6_getsockopt(struct sock *sk, int level, int optname, 16int udpv6_getsockopt(struct sock *sk, int level, int optname,
17 char __user *optval, int __user *optlen); 17 char __user *optval, int __user *optlen);
18extern int udpv6_setsockopt(struct sock *sk, int level, int optname, 18int udpv6_setsockopt(struct sock *sk, int level, int optname,
19 char __user *optval, unsigned int optlen); 19 char __user *optval, unsigned int optlen);
20#ifdef CONFIG_COMPAT 20#ifdef CONFIG_COMPAT
21extern int compat_udpv6_setsockopt(struct sock *sk, int level, int optname, 21int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
22 char __user *optval, unsigned int optlen); 22 char __user *optval, unsigned int optlen);
23extern int compat_udpv6_getsockopt(struct sock *sk, int level, int optname, 23int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
24 char __user *optval, int __user *optlen); 24 char __user *optval, int __user *optlen);
25#endif 25#endif
26extern int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 26int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
27 struct msghdr *msg, size_t len); 27 size_t len);
28extern int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 28int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
29 struct msghdr *msg, size_t len, 29 size_t len, int noblock, int flags, int *addr_len);
30 int noblock, int flags, int *addr_len); 30int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
31extern int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb); 31void udpv6_destroy_sock(struct sock *sk);
32extern void udpv6_destroy_sock(struct sock *sk);
33 32
34extern void udp_v6_clear_sk(struct sock *sk, int size); 33void udp_v6_clear_sk(struct sock *sk, int size);
35 34
36#ifdef CONFIG_PROC_FS 35#ifdef CONFIG_PROC_FS
37extern int udp6_seq_show(struct seq_file *seq, void *v); 36int udp6_seq_show(struct seq_file *seq, void *v);
38#endif 37#endif
39#endif /* _UDP6_IMPL_H */ 38#endif /* _UDP6_IMPL_H */
diff --git a/net/ipv6/udp_offload.c b/net/ipv6/udp_offload.c
index 60559511bd9c..e7359f9eaa8d 100644
--- a/net/ipv6/udp_offload.c
+++ b/net/ipv6/udp_offload.c
@@ -64,6 +64,8 @@ static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
64 SKB_GSO_DODGY | 64 SKB_GSO_DODGY |
65 SKB_GSO_UDP_TUNNEL | 65 SKB_GSO_UDP_TUNNEL |
66 SKB_GSO_GRE | 66 SKB_GSO_GRE |
67 SKB_GSO_IPIP |
68 SKB_GSO_SIT |
67 SKB_GSO_MPLS) || 69 SKB_GSO_MPLS) ||
68 !(type & (SKB_GSO_UDP)))) 70 !(type & (SKB_GSO_UDP))))
69 goto out; 71 goto out;
@@ -88,7 +90,7 @@ static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
88 90
89 /* Check if there is enough headroom to insert fragment header. */ 91 /* Check if there is enough headroom to insert fragment header. */
90 tnl_hlen = skb_tnl_header_len(skb); 92 tnl_hlen = skb_tnl_header_len(skb);
91 if (skb_headroom(skb) < (tnl_hlen + frag_hdr_sz)) { 93 if (skb->mac_header < (tnl_hlen + frag_hdr_sz)) {
92 if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz)) 94 if (gso_pskb_expand_head(skb, tnl_hlen + frag_hdr_sz))
93 goto out; 95 goto out;
94 } 96 }
diff --git a/net/ipv6/xfrm6_mode_tunnel.c b/net/ipv6/xfrm6_mode_tunnel.c
index 4770d515c2c8..cb04f7a16b5e 100644
--- a/net/ipv6/xfrm6_mode_tunnel.c
+++ b/net/ipv6/xfrm6_mode_tunnel.c
@@ -18,6 +18,65 @@
18#include <net/ipv6.h> 18#include <net/ipv6.h>
19#include <net/xfrm.h> 19#include <net/xfrm.h>
20 20
21/* Informational hook. The decap is still done here. */
22static struct xfrm_tunnel_notifier __rcu *rcv_notify_handlers __read_mostly;
23static DEFINE_MUTEX(xfrm6_mode_tunnel_input_mutex);
24
25int xfrm6_mode_tunnel_input_register(struct xfrm_tunnel_notifier *handler)
26{
27 struct xfrm_tunnel_notifier __rcu **pprev;
28 struct xfrm_tunnel_notifier *t;
29 int ret = -EEXIST;
30 int priority = handler->priority;
31
32 mutex_lock(&xfrm6_mode_tunnel_input_mutex);
33
34 for (pprev = &rcv_notify_handlers;
35 (t = rcu_dereference_protected(*pprev,
36 lockdep_is_held(&xfrm6_mode_tunnel_input_mutex))) != NULL;
37 pprev = &t->next) {
38 if (t->priority > priority)
39 break;
40 if (t->priority == priority)
41 goto err;
42
43 }
44
45 handler->next = *pprev;
46 rcu_assign_pointer(*pprev, handler);
47
48 ret = 0;
49
50err:
51 mutex_unlock(&xfrm6_mode_tunnel_input_mutex);
52 return ret;
53}
54EXPORT_SYMBOL_GPL(xfrm6_mode_tunnel_input_register);
55
56int xfrm6_mode_tunnel_input_deregister(struct xfrm_tunnel_notifier *handler)
57{
58 struct xfrm_tunnel_notifier __rcu **pprev;
59 struct xfrm_tunnel_notifier *t;
60 int ret = -ENOENT;
61
62 mutex_lock(&xfrm6_mode_tunnel_input_mutex);
63 for (pprev = &rcv_notify_handlers;
64 (t = rcu_dereference_protected(*pprev,
65 lockdep_is_held(&xfrm6_mode_tunnel_input_mutex))) != NULL;
66 pprev = &t->next) {
67 if (t == handler) {
68 *pprev = handler->next;
69 ret = 0;
70 break;
71 }
72 }
73 mutex_unlock(&xfrm6_mode_tunnel_input_mutex);
74 synchronize_net();
75
76 return ret;
77}
78EXPORT_SYMBOL_GPL(xfrm6_mode_tunnel_input_deregister);
79
21static inline void ipip6_ecn_decapsulate(struct sk_buff *skb) 80static inline void ipip6_ecn_decapsulate(struct sk_buff *skb)
22{ 81{
23 const struct ipv6hdr *outer_iph = ipv6_hdr(skb); 82 const struct ipv6hdr *outer_iph = ipv6_hdr(skb);
@@ -63,8 +122,15 @@ static int xfrm6_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
63 return 0; 122 return 0;
64} 123}
65 124
125#define for_each_input_rcu(head, handler) \
126 for (handler = rcu_dereference(head); \
127 handler != NULL; \
128 handler = rcu_dereference(handler->next))
129
130
66static int xfrm6_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb) 131static int xfrm6_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
67{ 132{
133 struct xfrm_tunnel_notifier *handler;
68 int err = -EINVAL; 134 int err = -EINVAL;
69 135
70 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPV6) 136 if (XFRM_MODE_SKB_CB(skb)->protocol != IPPROTO_IPV6)
@@ -72,6 +138,9 @@ static int xfrm6_mode_tunnel_input(struct xfrm_state *x, struct sk_buff *skb)
72 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr))) 138 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
73 goto out; 139 goto out;
74 140
141 for_each_input_rcu(rcv_notify_handlers, handler)
142 handler->handler(skb);
143
75 err = skb_unclone(skb, GFP_ATOMIC); 144 err = skb_unclone(skb, GFP_ATOMIC);
76 if (err) 145 if (err)
77 goto out; 146 goto out;
diff --git a/net/ipv6/xfrm6_policy.c b/net/ipv6/xfrm6_policy.c
index 08ed2772b7aa..5f8e128c512d 100644
--- a/net/ipv6/xfrm6_policy.c
+++ b/net/ipv6/xfrm6_policy.c
@@ -135,10 +135,14 @@ _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse)
135 struct ipv6_opt_hdr *exthdr; 135 struct ipv6_opt_hdr *exthdr;
136 const unsigned char *nh = skb_network_header(skb); 136 const unsigned char *nh = skb_network_header(skb);
137 u8 nexthdr = nh[IP6CB(skb)->nhoff]; 137 u8 nexthdr = nh[IP6CB(skb)->nhoff];
138 int oif = 0;
139
140 if (skb_dst(skb))
141 oif = skb_dst(skb)->dev->ifindex;
138 142
139 memset(fl6, 0, sizeof(struct flowi6)); 143 memset(fl6, 0, sizeof(struct flowi6));
140 fl6->flowi6_mark = skb->mark; 144 fl6->flowi6_mark = skb->mark;
141 fl6->flowi6_oif = skb_dst(skb)->dev->ifindex; 145 fl6->flowi6_oif = reverse ? skb->skb_iif : oif;
142 146
143 fl6->daddr = reverse ? hdr->saddr : hdr->daddr; 147 fl6->daddr = reverse ? hdr->saddr : hdr->daddr;
144 fl6->saddr = reverse ? hdr->daddr : hdr->saddr; 148 fl6->saddr = reverse ? hdr->daddr : hdr->saddr;
@@ -285,7 +289,7 @@ static struct dst_ops xfrm6_dst_ops = {
285 .destroy = xfrm6_dst_destroy, 289 .destroy = xfrm6_dst_destroy,
286 .ifdown = xfrm6_dst_ifdown, 290 .ifdown = xfrm6_dst_ifdown,
287 .local_out = __ip6_local_out, 291 .local_out = __ip6_local_out,
288 .gc_thresh = 1024, 292 .gc_thresh = 32768,
289}; 293};
290 294
291static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { 295static struct xfrm_policy_afinfo xfrm6_policy_afinfo = {
diff --git a/net/ipx/af_ipx.c b/net/ipx/af_ipx.c
index 7a1e0fc1bd4d..e096025b477f 100644
--- a/net/ipx/af_ipx.c
+++ b/net/ipx/af_ipx.c
@@ -1823,8 +1823,6 @@ static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
1823 if (skb->tstamp.tv64) 1823 if (skb->tstamp.tv64)
1824 sk->sk_stamp = skb->tstamp; 1824 sk->sk_stamp = skb->tstamp;
1825 1825
1826 msg->msg_namelen = sizeof(*sipx);
1827
1828 if (sipx) { 1826 if (sipx) {
1829 sipx->sipx_family = AF_IPX; 1827 sipx->sipx_family = AF_IPX;
1830 sipx->sipx_port = ipx->ipx_source.sock; 1828 sipx->sipx_port = ipx->ipx_source.sock;
@@ -1832,6 +1830,7 @@ static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock,
1832 sipx->sipx_network = IPX_SKB_CB(skb)->ipx_source_net; 1830 sipx->sipx_network = IPX_SKB_CB(skb)->ipx_source_net;
1833 sipx->sipx_type = ipx->ipx_type; 1831 sipx->sipx_type = ipx->ipx_type;
1834 sipx->sipx_zero = 0; 1832 sipx->sipx_zero = 0;
1833 msg->msg_namelen = sizeof(*sipx);
1835 } 1834 }
1836 rc = copied; 1835 rc = copied;
1837 1836
diff --git a/net/irda/af_irda.c b/net/irda/af_irda.c
index 0578d4fa00a9..de7db23049f1 100644
--- a/net/irda/af_irda.c
+++ b/net/irda/af_irda.c
@@ -1385,8 +1385,6 @@ static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
1385 1385
1386 IRDA_DEBUG(4, "%s()\n", __func__); 1386 IRDA_DEBUG(4, "%s()\n", __func__);
1387 1387
1388 msg->msg_namelen = 0;
1389
1390 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 1388 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1391 flags & MSG_DONTWAIT, &err); 1389 flags & MSG_DONTWAIT, &err);
1392 if (!skb) 1390 if (!skb)
@@ -1451,8 +1449,6 @@ static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
1451 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size); 1449 target = sock_rcvlowat(sk, flags & MSG_WAITALL, size);
1452 timeo = sock_rcvtimeo(sk, noblock); 1450 timeo = sock_rcvtimeo(sk, noblock);
1453 1451
1454 msg->msg_namelen = 0;
1455
1456 do { 1452 do {
1457 int chunk; 1453 int chunk;
1458 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue); 1454 struct sk_buff *skb = skb_dequeue(&sk->sk_receive_queue);
@@ -2563,9 +2559,8 @@ bed:
2563 jiffies + msecs_to_jiffies(val)); 2559 jiffies + msecs_to_jiffies(val));
2564 2560
2565 /* Wait for IR-LMP to call us back */ 2561 /* Wait for IR-LMP to call us back */
2566 __wait_event_interruptible(self->query_wait, 2562 err = __wait_event_interruptible(self->query_wait,
2567 (self->cachedaddr != 0 || self->errno == -ETIME), 2563 (self->cachedaddr != 0 || self->errno == -ETIME));
2568 err);
2569 2564
2570 /* If watchdog is still activated, kill it! */ 2565 /* If watchdog is still activated, kill it! */
2571 del_timer(&(self->watchdog)); 2566 del_timer(&(self->watchdog));
diff --git a/net/irda/irnet/irnet.h b/net/irda/irnet/irnet.h
index 564eb0b8afa3..8d65bb9477fc 100644
--- a/net/irda/irnet/irnet.h
+++ b/net/irda/irnet/irnet.h
@@ -509,16 +509,11 @@ typedef struct irnet_ctrl_channel
509 */ 509 */
510 510
511/* -------------------------- IRDA PART -------------------------- */ 511/* -------------------------- IRDA PART -------------------------- */
512extern int 512int irda_irnet_create(irnet_socket *); /* Initialise an IrNET socket */
513 irda_irnet_create(irnet_socket *); /* Initialise a IrNET socket */ 513int irda_irnet_connect(irnet_socket *); /* Try to connect over IrDA */
514extern int 514void irda_irnet_destroy(irnet_socket *); /* Teardown an IrNET socket */
515 irda_irnet_connect(irnet_socket *); /* Try to connect over IrDA */ 515int irda_irnet_init(void); /* Initialise IrDA part of IrNET */
516extern void 516void irda_irnet_cleanup(void); /* Teardown IrDA part of IrNET */
517 irda_irnet_destroy(irnet_socket *); /* Teardown a IrNET socket */
518extern int
519 irda_irnet_init(void); /* Initialise IrDA part of IrNET */
520extern void
521 irda_irnet_cleanup(void); /* Teardown IrDA part of IrNET */
522 517
523/**************************** VARIABLES ****************************/ 518/**************************** VARIABLES ****************************/
524 519
diff --git a/net/irda/irnetlink.c b/net/irda/irnetlink.c
index c32971269280..a37b81fe0479 100644
--- a/net/irda/irnetlink.c
+++ b/net/irda/irnetlink.c
@@ -131,7 +131,7 @@ static const struct nla_policy irda_nl_policy[IRDA_NL_ATTR_MAX + 1] = {
131 [IRDA_NL_ATTR_MODE] = { .type = NLA_U32 }, 131 [IRDA_NL_ATTR_MODE] = { .type = NLA_U32 },
132}; 132};
133 133
134static struct genl_ops irda_nl_ops[] = { 134static const struct genl_ops irda_nl_ops[] = {
135 { 135 {
136 .cmd = IRDA_NL_CMD_SET_MODE, 136 .cmd = IRDA_NL_CMD_SET_MODE,
137 .doit = irda_nl_set_mode, 137 .doit = irda_nl_set_mode,
@@ -149,8 +149,7 @@ static struct genl_ops irda_nl_ops[] = {
149 149
150int irda_nl_register(void) 150int irda_nl_register(void)
151{ 151{
152 return genl_register_family_with_ops(&irda_nl_family, 152 return genl_register_family_with_ops(&irda_nl_family, irda_nl_ops);
153 irda_nl_ops, ARRAY_SIZE(irda_nl_ops));
154} 153}
155 154
156void irda_nl_unregister(void) 155void irda_nl_unregister(void)
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 168aff5e60de..c4b7218058b6 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1324,8 +1324,6 @@ static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1324 int err = 0; 1324 int err = 0;
1325 u32 offset; 1325 u32 offset;
1326 1326
1327 msg->msg_namelen = 0;
1328
1329 if ((sk->sk_state == IUCV_DISCONN) && 1327 if ((sk->sk_state == IUCV_DISCONN) &&
1330 skb_queue_empty(&iucv->backlog_skb_q) && 1328 skb_queue_empty(&iucv->backlog_skb_q) &&
1331 skb_queue_empty(&sk->sk_receive_queue) && 1329 skb_queue_empty(&sk->sk_receive_queue) &&
diff --git a/net/key/af_key.c b/net/key/af_key.c
index 911ef03bf8fb..545f047868ad 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -3616,7 +3616,6 @@ static int pfkey_recvmsg(struct kiocb *kiocb,
3616 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT)) 3616 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
3617 goto out; 3617 goto out;
3618 3618
3619 msg->msg_namelen = 0;
3620 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err); 3619 skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
3621 if (skb == NULL) 3620 if (skb == NULL)
3622 goto out; 3621 goto out;
diff --git a/net/l2tp/l2tp_core.c b/net/l2tp/l2tp_core.c
index b076e8309bc2..9af77d9c0ec9 100644
--- a/net/l2tp/l2tp_core.c
+++ b/net/l2tp/l2tp_core.c
@@ -1181,7 +1181,7 @@ static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1181 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) { 1181 !(skb_dst(skb)->dev->features & NETIF_F_IPV6_CSUM)) {
1182 __wsum csum = skb_checksum(skb, 0, udp_len, 0); 1182 __wsum csum = skb_checksum(skb, 0, udp_len, 0);
1183 skb->ip_summed = CHECKSUM_UNNECESSARY; 1183 skb->ip_summed = CHECKSUM_UNNECESSARY;
1184 uh->check = csum_ipv6_magic(&np->saddr, &np->daddr, udp_len, 1184 uh->check = csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr, udp_len,
1185 IPPROTO_UDP, csum); 1185 IPPROTO_UDP, csum);
1186 if (uh->check == 0) 1186 if (uh->check == 0)
1187 uh->check = CSUM_MANGLED_0; 1187 uh->check = CSUM_MANGLED_0;
@@ -1189,7 +1189,7 @@ static void l2tp_xmit_ipv6_csum(struct sock *sk, struct sk_buff *skb,
1189 skb->ip_summed = CHECKSUM_PARTIAL; 1189 skb->ip_summed = CHECKSUM_PARTIAL;
1190 skb->csum_start = skb_transport_header(skb) - skb->head; 1190 skb->csum_start = skb_transport_header(skb) - skb->head;
1191 skb->csum_offset = offsetof(struct udphdr, check); 1191 skb->csum_offset = offsetof(struct udphdr, check);
1192 uh->check = ~csum_ipv6_magic(&np->saddr, &np->daddr, 1192 uh->check = ~csum_ipv6_magic(&np->saddr, &sk->sk_v6_daddr,
1193 udp_len, IPPROTO_UDP, 0); 1193 udp_len, IPPROTO_UDP, 0);
1194 } 1194 }
1195} 1195}
@@ -1713,13 +1713,13 @@ int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32
1713 struct ipv6_pinfo *np = inet6_sk(sk); 1713 struct ipv6_pinfo *np = inet6_sk(sk);
1714 1714
1715 if (ipv6_addr_v4mapped(&np->saddr) && 1715 if (ipv6_addr_v4mapped(&np->saddr) &&
1716 ipv6_addr_v4mapped(&np->daddr)) { 1716 ipv6_addr_v4mapped(&sk->sk_v6_daddr)) {
1717 struct inet_sock *inet = inet_sk(sk); 1717 struct inet_sock *inet = inet_sk(sk);
1718 1718
1719 tunnel->v4mapped = true; 1719 tunnel->v4mapped = true;
1720 inet->inet_saddr = np->saddr.s6_addr32[3]; 1720 inet->inet_saddr = np->saddr.s6_addr32[3];
1721 inet->inet_rcv_saddr = np->rcv_saddr.s6_addr32[3]; 1721 inet->inet_rcv_saddr = sk->sk_v6_rcv_saddr.s6_addr32[3];
1722 inet->inet_daddr = np->daddr.s6_addr32[3]; 1722 inet->inet_daddr = sk->sk_v6_daddr.s6_addr32[3];
1723 } else { 1723 } else {
1724 tunnel->v4mapped = false; 1724 tunnel->v4mapped = false;
1725 } 1725 }
diff --git a/net/l2tp/l2tp_core.h b/net/l2tp/l2tp_core.h
index 6f251cbc2ed7..1ee9f6965d68 100644
--- a/net/l2tp/l2tp_core.h
+++ b/net/l2tp/l2tp_core.h
@@ -238,29 +238,40 @@ out:
238 return tunnel; 238 return tunnel;
239} 239}
240 240
241extern struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel); 241struct sock *l2tp_tunnel_sock_lookup(struct l2tp_tunnel *tunnel);
242extern void l2tp_tunnel_sock_put(struct sock *sk); 242void l2tp_tunnel_sock_put(struct sock *sk);
243extern struct l2tp_session *l2tp_session_find(struct net *net, struct l2tp_tunnel *tunnel, u32 session_id); 243struct l2tp_session *l2tp_session_find(struct net *net,
244extern struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth); 244 struct l2tp_tunnel *tunnel,
245extern struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname); 245 u32 session_id);
246extern struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id); 246struct l2tp_session *l2tp_session_find_nth(struct l2tp_tunnel *tunnel, int nth);
247extern struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth); 247struct l2tp_session *l2tp_session_find_by_ifname(struct net *net, char *ifname);
248 248struct l2tp_tunnel *l2tp_tunnel_find(struct net *net, u32 tunnel_id);
249extern int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id, u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg, struct l2tp_tunnel **tunnelp); 249struct l2tp_tunnel *l2tp_tunnel_find_nth(struct net *net, int nth);
250extern void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel); 250
251extern int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel); 251int l2tp_tunnel_create(struct net *net, int fd, int version, u32 tunnel_id,
252extern struct l2tp_session *l2tp_session_create(int priv_size, struct l2tp_tunnel *tunnel, u32 session_id, u32 peer_session_id, struct l2tp_session_cfg *cfg); 252 u32 peer_tunnel_id, struct l2tp_tunnel_cfg *cfg,
253extern void __l2tp_session_unhash(struct l2tp_session *session); 253 struct l2tp_tunnel **tunnelp);
254extern int l2tp_session_delete(struct l2tp_session *session); 254void l2tp_tunnel_closeall(struct l2tp_tunnel *tunnel);
255extern void l2tp_session_free(struct l2tp_session *session); 255int l2tp_tunnel_delete(struct l2tp_tunnel *tunnel);
256extern void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb, unsigned char *ptr, unsigned char *optr, u16 hdrflags, int length, int (*payload_hook)(struct sk_buff *skb)); 256struct l2tp_session *l2tp_session_create(int priv_size,
257extern int l2tp_session_queue_purge(struct l2tp_session *session); 257 struct l2tp_tunnel *tunnel,
258extern int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb); 258 u32 session_id, u32 peer_session_id,
259 259 struct l2tp_session_cfg *cfg);
260extern int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb, int hdr_len); 260void __l2tp_session_unhash(struct l2tp_session *session);
261 261int l2tp_session_delete(struct l2tp_session *session);
262extern int l2tp_nl_register_ops(enum l2tp_pwtype pw_type, const struct l2tp_nl_cmd_ops *ops); 262void l2tp_session_free(struct l2tp_session *session);
263extern void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type); 263void l2tp_recv_common(struct l2tp_session *session, struct sk_buff *skb,
264 unsigned char *ptr, unsigned char *optr, u16 hdrflags,
265 int length, int (*payload_hook)(struct sk_buff *skb));
266int l2tp_session_queue_purge(struct l2tp_session *session);
267int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb);
268
269int l2tp_xmit_skb(struct l2tp_session *session, struct sk_buff *skb,
270 int hdr_len);
271
272int l2tp_nl_register_ops(enum l2tp_pwtype pw_type,
273 const struct l2tp_nl_cmd_ops *ops);
274void l2tp_nl_unregister_ops(enum l2tp_pwtype pw_type);
264 275
265/* Session reference counts. Incremented when code obtains a reference 276/* Session reference counts. Incremented when code obtains a reference
266 * to a session. 277 * to a session.
diff --git a/net/l2tp/l2tp_debugfs.c b/net/l2tp/l2tp_debugfs.c
index 072d7202e182..2d6760a2ae34 100644
--- a/net/l2tp/l2tp_debugfs.c
+++ b/net/l2tp/l2tp_debugfs.c
@@ -127,9 +127,10 @@ static void l2tp_dfs_seq_tunnel_show(struct seq_file *m, void *v)
127 127
128#if IS_ENABLED(CONFIG_IPV6) 128#if IS_ENABLED(CONFIG_IPV6)
129 if (tunnel->sock->sk_family == AF_INET6) { 129 if (tunnel->sock->sk_family == AF_INET6) {
130 struct ipv6_pinfo *np = inet6_sk(tunnel->sock); 130 const struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
131
131 seq_printf(m, " from %pI6c to %pI6c\n", 132 seq_printf(m, " from %pI6c to %pI6c\n",
132 &np->saddr, &np->daddr); 133 &np->saddr, &tunnel->sock->sk_v6_daddr);
133 } else 134 } else
134#endif 135#endif
135 seq_printf(m, " from %pI4 to %pI4\n", 136 seq_printf(m, " from %pI4 to %pI4\n",
diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c
index 571db8dd2292..da1a1cee1a08 100644
--- a/net/l2tp/l2tp_ip.c
+++ b/net/l2tp/l2tp_ip.c
@@ -518,9 +518,6 @@ static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m
518 if (flags & MSG_OOB) 518 if (flags & MSG_OOB)
519 goto out; 519 goto out;
520 520
521 if (addr_len)
522 *addr_len = sizeof(*sin);
523
524 skb = skb_recv_datagram(sk, flags, noblock, &err); 521 skb = skb_recv_datagram(sk, flags, noblock, &err);
525 if (!skb) 522 if (!skb)
526 goto out; 523 goto out;
@@ -543,6 +540,7 @@ static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *m
543 sin->sin_addr.s_addr = ip_hdr(skb)->saddr; 540 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
544 sin->sin_port = 0; 541 sin->sin_port = 0;
545 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); 542 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
543 *addr_len = sizeof(*sin);
546 } 544 }
547 if (inet->cmsg_flags) 545 if (inet->cmsg_flags)
548 ip_cmsg_recv(msg, skb); 546 ip_cmsg_recv(msg, skb);
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index b8a6039314e8..bb6e206ea70b 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -63,7 +63,7 @@ static struct sock *__l2tp_ip6_bind_lookup(struct net *net,
63 struct sock *sk; 63 struct sock *sk;
64 64
65 sk_for_each_bound(sk, &l2tp_ip6_bind_table) { 65 sk_for_each_bound(sk, &l2tp_ip6_bind_table) {
66 struct in6_addr *addr = inet6_rcv_saddr(sk); 66 const struct in6_addr *addr = inet6_rcv_saddr(sk);
67 struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk); 67 struct l2tp_ip6_sock *l2tp = l2tp_ip6_sk(sk);
68 68
69 if (l2tp == NULL) 69 if (l2tp == NULL)
@@ -331,7 +331,7 @@ static int l2tp_ip6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
331 rcu_read_unlock(); 331 rcu_read_unlock();
332 332
333 inet->inet_rcv_saddr = inet->inet_saddr = v4addr; 333 inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
334 np->rcv_saddr = addr->l2tp_addr; 334 sk->sk_v6_rcv_saddr = addr->l2tp_addr;
335 np->saddr = addr->l2tp_addr; 335 np->saddr = addr->l2tp_addr;
336 336
337 l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id; 337 l2tp_ip6_sk(sk)->conn_id = addr->l2tp_conn_id;
@@ -421,14 +421,14 @@ static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
421 if (!lsk->peer_conn_id) 421 if (!lsk->peer_conn_id)
422 return -ENOTCONN; 422 return -ENOTCONN;
423 lsa->l2tp_conn_id = lsk->peer_conn_id; 423 lsa->l2tp_conn_id = lsk->peer_conn_id;
424 lsa->l2tp_addr = np->daddr; 424 lsa->l2tp_addr = sk->sk_v6_daddr;
425 if (np->sndflow) 425 if (np->sndflow)
426 lsa->l2tp_flowinfo = np->flow_label; 426 lsa->l2tp_flowinfo = np->flow_label;
427 } else { 427 } else {
428 if (ipv6_addr_any(&np->rcv_saddr)) 428 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr))
429 lsa->l2tp_addr = np->saddr; 429 lsa->l2tp_addr = np->saddr;
430 else 430 else
431 lsa->l2tp_addr = np->rcv_saddr; 431 lsa->l2tp_addr = sk->sk_v6_rcv_saddr;
432 432
433 lsa->l2tp_conn_id = lsk->conn_id; 433 lsa->l2tp_conn_id = lsk->conn_id;
434 } 434 }
@@ -528,7 +528,6 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
528 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 528 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
529 if (flowlabel == NULL) 529 if (flowlabel == NULL)
530 return -EINVAL; 530 return -EINVAL;
531 daddr = &flowlabel->dst;
532 } 531 }
533 } 532 }
534 533
@@ -537,8 +536,8 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
537 * sk->sk_dst_cache. 536 * sk->sk_dst_cache.
538 */ 537 */
539 if (sk->sk_state == TCP_ESTABLISHED && 538 if (sk->sk_state == TCP_ESTABLISHED &&
540 ipv6_addr_equal(daddr, &np->daddr)) 539 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
541 daddr = &np->daddr; 540 daddr = &sk->sk_v6_daddr;
542 541
543 if (addr_len >= sizeof(struct sockaddr_in6) && 542 if (addr_len >= sizeof(struct sockaddr_in6) &&
544 lsa->l2tp_scope_id && 543 lsa->l2tp_scope_id &&
@@ -548,7 +547,7 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
548 if (sk->sk_state != TCP_ESTABLISHED) 547 if (sk->sk_state != TCP_ESTABLISHED)
549 return -EDESTADDRREQ; 548 return -EDESTADDRREQ;
550 549
551 daddr = &np->daddr; 550 daddr = &sk->sk_v6_daddr;
552 fl6.flowlabel = np->flow_label; 551 fl6.flowlabel = np->flow_label;
553 } 552 }
554 553
@@ -665,7 +664,7 @@ static int l2tp_ip6_recvmsg(struct kiocb *iocb, struct sock *sk,
665 *addr_len = sizeof(*lsa); 664 *addr_len = sizeof(*lsa);
666 665
667 if (flags & MSG_ERRQUEUE) 666 if (flags & MSG_ERRQUEUE)
668 return ipv6_recv_error(sk, msg, len); 667 return ipv6_recv_error(sk, msg, len, addr_len);
669 668
670 skb = skb_recv_datagram(sk, flags, noblock, &err); 669 skb = skb_recv_datagram(sk, flags, noblock, &err);
671 if (!skb) 670 if (!skb)
diff --git a/net/l2tp/l2tp_netlink.c b/net/l2tp/l2tp_netlink.c
index 0825ff26e113..4cfd722e9153 100644
--- a/net/l2tp/l2tp_netlink.c
+++ b/net/l2tp/l2tp_netlink.c
@@ -306,8 +306,8 @@ static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int fla
306 if (np) { 306 if (np) {
307 if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr), 307 if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr),
308 &np->saddr) || 308 &np->saddr) ||
309 nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(np->daddr), 309 nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(sk->sk_v6_daddr),
310 &np->daddr)) 310 &sk->sk_v6_daddr))
311 goto nla_put_failure; 311 goto nla_put_failure;
312 } else 312 } else
313#endif 313#endif
@@ -793,7 +793,7 @@ static struct nla_policy l2tp_nl_policy[L2TP_ATTR_MAX + 1] = {
793 }, 793 },
794}; 794};
795 795
796static struct genl_ops l2tp_nl_ops[] = { 796static const struct genl_ops l2tp_nl_ops[] = {
797 { 797 {
798 .cmd = L2TP_CMD_NOOP, 798 .cmd = L2TP_CMD_NOOP,
799 .doit = l2tp_nl_cmd_noop, 799 .doit = l2tp_nl_cmd_noop,
@@ -887,13 +887,8 @@ EXPORT_SYMBOL_GPL(l2tp_nl_unregister_ops);
887 887
888static int l2tp_nl_init(void) 888static int l2tp_nl_init(void)
889{ 889{
890 int err;
891
892 pr_info("L2TP netlink interface\n"); 890 pr_info("L2TP netlink interface\n");
893 err = genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops, 891 return genl_register_family_with_ops(&l2tp_nl_family, l2tp_nl_ops);
894 ARRAY_SIZE(l2tp_nl_ops));
895
896 return err;
897} 892}
898 893
899static void l2tp_nl_cleanup(void) 894static void l2tp_nl_cleanup(void)
diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c
index 8c46b271064a..be5fadf34739 100644
--- a/net/l2tp/l2tp_ppp.c
+++ b/net/l2tp/l2tp_ppp.c
@@ -197,8 +197,6 @@ static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock,
197 if (sk->sk_state & PPPOX_BOUND) 197 if (sk->sk_state & PPPOX_BOUND)
198 goto end; 198 goto end;
199 199
200 msg->msg_namelen = 0;
201
202 err = 0; 200 err = 0;
203 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 201 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
204 flags & MSG_DONTWAIT, &err); 202 flags & MSG_DONTWAIT, &err);
@@ -910,8 +908,8 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
910#if IS_ENABLED(CONFIG_IPV6) 908#if IS_ENABLED(CONFIG_IPV6)
911 } else if ((tunnel->version == 2) && 909 } else if ((tunnel->version == 2) &&
912 (tunnel->sock->sk_family == AF_INET6)) { 910 (tunnel->sock->sk_family == AF_INET6)) {
913 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
914 struct sockaddr_pppol2tpin6 sp; 911 struct sockaddr_pppol2tpin6 sp;
912
915 len = sizeof(sp); 913 len = sizeof(sp);
916 memset(&sp, 0, len); 914 memset(&sp, 0, len);
917 sp.sa_family = AF_PPPOX; 915 sp.sa_family = AF_PPPOX;
@@ -924,13 +922,13 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
924 sp.pppol2tp.d_session = session->peer_session_id; 922 sp.pppol2tp.d_session = session->peer_session_id;
925 sp.pppol2tp.addr.sin6_family = AF_INET6; 923 sp.pppol2tp.addr.sin6_family = AF_INET6;
926 sp.pppol2tp.addr.sin6_port = inet->inet_dport; 924 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
927 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 925 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
928 sizeof(np->daddr)); 926 sizeof(tunnel->sock->sk_v6_daddr));
929 memcpy(uaddr, &sp, len); 927 memcpy(uaddr, &sp, len);
930 } else if ((tunnel->version == 3) && 928 } else if ((tunnel->version == 3) &&
931 (tunnel->sock->sk_family == AF_INET6)) { 929 (tunnel->sock->sk_family == AF_INET6)) {
932 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
933 struct sockaddr_pppol2tpv3in6 sp; 930 struct sockaddr_pppol2tpv3in6 sp;
931
934 len = sizeof(sp); 932 len = sizeof(sp);
935 memset(&sp, 0, len); 933 memset(&sp, 0, len);
936 sp.sa_family = AF_PPPOX; 934 sp.sa_family = AF_PPPOX;
@@ -943,8 +941,8 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
943 sp.pppol2tp.d_session = session->peer_session_id; 941 sp.pppol2tp.d_session = session->peer_session_id;
944 sp.pppol2tp.addr.sin6_family = AF_INET6; 942 sp.pppol2tp.addr.sin6_family = AF_INET6;
945 sp.pppol2tp.addr.sin6_port = inet->inet_dport; 943 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
946 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 944 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
947 sizeof(np->daddr)); 945 sizeof(tunnel->sock->sk_v6_daddr));
948 memcpy(uaddr, &sp, len); 946 memcpy(uaddr, &sp, len);
949#endif 947#endif
950 } else if (tunnel->version == 3) { 948 } else if (tunnel->version == 3) {
diff --git a/net/llc/af_llc.c b/net/llc/af_llc.c
index 6cba486353e8..c71b699eb555 100644
--- a/net/llc/af_llc.c
+++ b/net/llc/af_llc.c
@@ -715,13 +715,11 @@ static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
715 unsigned long cpu_flags; 715 unsigned long cpu_flags;
716 size_t copied = 0; 716 size_t copied = 0;
717 u32 peek_seq = 0; 717 u32 peek_seq = 0;
718 u32 *seq; 718 u32 *seq, skb_len;
719 unsigned long used; 719 unsigned long used;
720 int target; /* Read at least this many bytes */ 720 int target; /* Read at least this many bytes */
721 long timeo; 721 long timeo;
722 722
723 msg->msg_namelen = 0;
724
725 lock_sock(sk); 723 lock_sock(sk);
726 copied = -ENOTCONN; 724 copied = -ENOTCONN;
727 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN)) 725 if (unlikely(sk->sk_type == SOCK_STREAM && sk->sk_state == TCP_LISTEN))
@@ -814,6 +812,7 @@ static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
814 } 812 }
815 continue; 813 continue;
816 found_ok_skb: 814 found_ok_skb:
815 skb_len = skb->len;
817 /* Ok so how much can we use? */ 816 /* Ok so how much can we use? */
818 used = skb->len - offset; 817 used = skb->len - offset;
819 if (len < used) 818 if (len < used)
@@ -846,7 +845,7 @@ static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock,
846 } 845 }
847 846
848 /* Partial read */ 847 /* Partial read */
849 if (used + offset < skb->len) 848 if (used + offset < skb_len)
850 continue; 849 continue;
851 } while (len > 0); 850 } while (len > 0);
852 851
diff --git a/net/mac80211/Kconfig b/net/mac80211/Kconfig
index 62535fe9f570..97b5dcad5025 100644
--- a/net/mac80211/Kconfig
+++ b/net/mac80211/Kconfig
@@ -4,6 +4,7 @@ config MAC80211
4 select CRYPTO 4 select CRYPTO
5 select CRYPTO_ARC4 5 select CRYPTO_ARC4
6 select CRYPTO_AES 6 select CRYPTO_AES
7 select CRYPTO_CCM
7 select CRC32 8 select CRC32
8 select AVERAGE 9 select AVERAGE
9 ---help--- 10 ---help---
@@ -258,6 +259,17 @@ config MAC80211_MESH_SYNC_DEBUG
258 259
259 Do not select this option. 260 Do not select this option.
260 261
262config MAC80211_MESH_CSA_DEBUG
263 bool "Verbose mesh channel switch debugging"
264 depends on MAC80211_DEBUG_MENU
265 depends on MAC80211_MESH
266 ---help---
267 Selecting this option causes mac80211 to print out very verbose mesh
268 channel switch debugging messages (when mac80211 is taking part in a
269 mesh network).
270
271 Do not select this option.
272
261config MAC80211_MESH_PS_DEBUG 273config MAC80211_MESH_PS_DEBUG
262 bool "Verbose mesh powersave debugging" 274 bool "Verbose mesh powersave debugging"
263 depends on MAC80211_DEBUG_MENU 275 depends on MAC80211_DEBUG_MENU
diff --git a/net/mac80211/aes_ccm.c b/net/mac80211/aes_ccm.c
index be7614b9ed27..7c7df475a401 100644
--- a/net/mac80211/aes_ccm.c
+++ b/net/mac80211/aes_ccm.c
@@ -2,6 +2,8 @@
2 * Copyright 2003-2004, Instant802 Networks, Inc. 2 * Copyright 2003-2004, Instant802 Networks, Inc.
3 * Copyright 2005-2006, Devicescape Software, Inc. 3 * Copyright 2005-2006, Devicescape Software, Inc.
4 * 4 *
5 * Rewrite: Copyright (C) 2013 Linaro Ltd <ard.biesheuvel@linaro.org>
6 *
5 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as 8 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation. 9 * published by the Free Software Foundation.
@@ -17,134 +19,75 @@
17#include "key.h" 19#include "key.h"
18#include "aes_ccm.h" 20#include "aes_ccm.h"
19 21
20static void aes_ccm_prepare(struct crypto_cipher *tfm, u8 *scratch, u8 *a) 22void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
23 u8 *data, size_t data_len, u8 *mic)
21{ 24{
22 int i; 25 struct scatterlist assoc, pt, ct[2];
23 u8 *b_0, *aad, *b, *s_0; 26 struct {
24 27 struct aead_request req;
25 b_0 = scratch + 3 * AES_BLOCK_SIZE; 28 u8 priv[crypto_aead_reqsize(tfm)];
26 aad = scratch + 4 * AES_BLOCK_SIZE; 29 } aead_req;
27 b = scratch;
28 s_0 = scratch + AES_BLOCK_SIZE;
29
30 crypto_cipher_encrypt_one(tfm, b, b_0);
31 30
32 /* Extra Authenticate-only data (always two AES blocks) */ 31 memset(&aead_req, 0, sizeof(aead_req));
33 for (i = 0; i < AES_BLOCK_SIZE; i++)
34 aad[i] ^= b[i];
35 crypto_cipher_encrypt_one(tfm, b, aad);
36 32
37 aad += AES_BLOCK_SIZE; 33 sg_init_one(&pt, data, data_len);
34 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
35 sg_init_table(ct, 2);
36 sg_set_buf(&ct[0], data, data_len);
37 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
38 38
39 for (i = 0; i < AES_BLOCK_SIZE; i++) 39 aead_request_set_tfm(&aead_req.req, tfm);
40 aad[i] ^= b[i]; 40 aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
41 crypto_cipher_encrypt_one(tfm, a, aad); 41 aead_request_set_crypt(&aead_req.req, &pt, ct, data_len, b_0);
42 42
43 /* Mask out bits from auth-only-b_0 */ 43 crypto_aead_encrypt(&aead_req.req);
44 b_0[0] &= 0x07;
45
46 /* S_0 is used to encrypt T (= MIC) */
47 b_0[14] = 0;
48 b_0[15] = 0;
49 crypto_cipher_encrypt_one(tfm, s_0, b_0);
50} 44}
51 45
52 46int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
53void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch, 47 u8 *data, size_t data_len, u8 *mic)
54 u8 *data, size_t data_len,
55 u8 *cdata, u8 *mic)
56{ 48{
57 int i, j, last_len, num_blocks; 49 struct scatterlist assoc, pt, ct[2];
58 u8 *pos, *cpos, *b, *s_0, *e, *b_0; 50 struct {
59 51 struct aead_request req;
60 b = scratch; 52 u8 priv[crypto_aead_reqsize(tfm)];
61 s_0 = scratch + AES_BLOCK_SIZE; 53 } aead_req;
62 e = scratch + 2 * AES_BLOCK_SIZE; 54
63 b_0 = scratch + 3 * AES_BLOCK_SIZE; 55 memset(&aead_req, 0, sizeof(aead_req));
64 56
65 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE); 57 sg_init_one(&pt, data, data_len);
66 last_len = data_len % AES_BLOCK_SIZE; 58 sg_init_one(&assoc, &aad[2], be16_to_cpup((__be16 *)aad));
67 aes_ccm_prepare(tfm, scratch, b); 59 sg_init_table(ct, 2);
68 60 sg_set_buf(&ct[0], data, data_len);
69 /* Process payload blocks */ 61 sg_set_buf(&ct[1], mic, IEEE80211_CCMP_MIC_LEN);
70 pos = data; 62
71 cpos = cdata; 63 aead_request_set_tfm(&aead_req.req, tfm);
72 for (j = 1; j <= num_blocks; j++) { 64 aead_request_set_assoc(&aead_req.req, &assoc, assoc.length);
73 int blen = (j == num_blocks && last_len) ? 65 aead_request_set_crypt(&aead_req.req, ct, &pt,
74 last_len : AES_BLOCK_SIZE; 66 data_len + IEEE80211_CCMP_MIC_LEN, b_0);
75 67
76 /* Authentication followed by encryption */ 68 return crypto_aead_decrypt(&aead_req.req);
77 for (i = 0; i < blen; i++)
78 b[i] ^= pos[i];
79 crypto_cipher_encrypt_one(tfm, b, b);
80
81 b_0[14] = (j >> 8) & 0xff;
82 b_0[15] = j & 0xff;
83 crypto_cipher_encrypt_one(tfm, e, b_0);
84 for (i = 0; i < blen; i++)
85 *cpos++ = *pos++ ^ e[i];
86 }
87
88 for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++)
89 mic[i] = b[i] ^ s_0[i];
90} 69}
91 70
92 71struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[])
93int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch,
94 u8 *cdata, size_t data_len, u8 *mic, u8 *data)
95{ 72{
96 int i, j, last_len, num_blocks; 73 struct crypto_aead *tfm;
97 u8 *pos, *cpos, *b, *s_0, *a, *b_0; 74 int err;
98
99 b = scratch;
100 s_0 = scratch + AES_BLOCK_SIZE;
101 a = scratch + 2 * AES_BLOCK_SIZE;
102 b_0 = scratch + 3 * AES_BLOCK_SIZE;
103
104 num_blocks = DIV_ROUND_UP(data_len, AES_BLOCK_SIZE);
105 last_len = data_len % AES_BLOCK_SIZE;
106 aes_ccm_prepare(tfm, scratch, a);
107
108 /* Process payload blocks */
109 cpos = cdata;
110 pos = data;
111 for (j = 1; j <= num_blocks; j++) {
112 int blen = (j == num_blocks && last_len) ?
113 last_len : AES_BLOCK_SIZE;
114
115 /* Decryption followed by authentication */
116 b_0[14] = (j >> 8) & 0xff;
117 b_0[15] = j & 0xff;
118 crypto_cipher_encrypt_one(tfm, b, b_0);
119 for (i = 0; i < blen; i++) {
120 *pos = *cpos++ ^ b[i];
121 a[i] ^= *pos++;
122 }
123 crypto_cipher_encrypt_one(tfm, a, a);
124 }
125
126 for (i = 0; i < IEEE80211_CCMP_MIC_LEN; i++) {
127 if ((mic[i] ^ s_0[i]) != a[i])
128 return -1;
129 }
130
131 return 0;
132}
133 75
76 tfm = crypto_alloc_aead("ccm(aes)", 0, CRYPTO_ALG_ASYNC);
77 if (IS_ERR(tfm))
78 return tfm;
134 79
135struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[]) 80 err = crypto_aead_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
136{ 81 if (!err)
137 struct crypto_cipher *tfm; 82 err = crypto_aead_setauthsize(tfm, IEEE80211_CCMP_MIC_LEN);
83 if (!err)
84 return tfm;
138 85
139 tfm = crypto_alloc_cipher("aes", 0, CRYPTO_ALG_ASYNC); 86 crypto_free_aead(tfm);
140 if (!IS_ERR(tfm)) 87 return ERR_PTR(err);
141 crypto_cipher_setkey(tfm, key, WLAN_KEY_LEN_CCMP);
142
143 return tfm;
144} 88}
145 89
146 90void ieee80211_aes_key_free(struct crypto_aead *tfm)
147void ieee80211_aes_key_free(struct crypto_cipher *tfm)
148{ 91{
149 crypto_free_cipher(tfm); 92 crypto_free_aead(tfm);
150} 93}
diff --git a/net/mac80211/aes_ccm.h b/net/mac80211/aes_ccm.h
index 5b7d744e2370..2c7ab1948a2e 100644
--- a/net/mac80211/aes_ccm.h
+++ b/net/mac80211/aes_ccm.h
@@ -12,13 +12,11 @@
12 12
13#include <linux/crypto.h> 13#include <linux/crypto.h>
14 14
15struct crypto_cipher *ieee80211_aes_key_setup_encrypt(const u8 key[]); 15struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[]);
16void ieee80211_aes_ccm_encrypt(struct crypto_cipher *tfm, u8 *scratch, 16void ieee80211_aes_ccm_encrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
17 u8 *data, size_t data_len, 17 u8 *data, size_t data_len, u8 *mic);
18 u8 *cdata, u8 *mic); 18int ieee80211_aes_ccm_decrypt(struct crypto_aead *tfm, u8 *b_0, u8 *aad,
19int ieee80211_aes_ccm_decrypt(struct crypto_cipher *tfm, u8 *scratch, 19 u8 *data, size_t data_len, u8 *mic);
20 u8 *cdata, size_t data_len, 20void ieee80211_aes_key_free(struct crypto_aead *tfm);
21 u8 *mic, u8 *data);
22void ieee80211_aes_key_free(struct crypto_cipher *tfm);
23 21
24#endif /* AES_CCM_H */ 22#endif /* AES_CCM_H */
diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c
index 629dee7ec9bf..364ce0c5962f 100644
--- a/net/mac80211/cfg.c
+++ b/net/mac80211/cfg.c
@@ -1059,6 +1059,7 @@ static int ieee80211_stop_ap(struct wiphy *wiphy, struct net_device *dev)
1059 /* abort any running channel switch */ 1059 /* abort any running channel switch */
1060 sdata->vif.csa_active = false; 1060 sdata->vif.csa_active = false;
1061 cancel_work_sync(&sdata->csa_finalize_work); 1061 cancel_work_sync(&sdata->csa_finalize_work);
1062 cancel_work_sync(&sdata->u.ap.request_smps_work);
1062 1063
1063 /* turn off carrier for this interface and dependent VLANs */ 1064 /* turn off carrier for this interface and dependent VLANs */
1064 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list) 1065 list_for_each_entry(vlan, &sdata->u.ap.vlans, u.vlan.list)
@@ -1342,8 +1343,8 @@ static int sta_apply_parameters(struct ieee80211_local *local,
1342 sta->plink_state = params->plink_state; 1343 sta->plink_state = params->plink_state;
1343 1344
1344 ieee80211_mps_sta_status_update(sta); 1345 ieee80211_mps_sta_status_update(sta);
1345 changed |= 1346 changed |= ieee80211_mps_set_sta_local_pm(sta,
1346 ieee80211_mps_local_status_update(sdata); 1347 NL80211_MESH_POWER_UNKNOWN);
1347 break; 1348 break;
1348 default: 1349 default:
1349 /* nothing */ 1350 /* nothing */
@@ -1367,7 +1368,7 @@ static int sta_apply_parameters(struct ieee80211_local *local,
1367 changed |= 1368 changed |=
1368 ieee80211_mps_set_sta_local_pm(sta, 1369 ieee80211_mps_set_sta_local_pm(sta,
1369 params->local_pm); 1370 params->local_pm);
1370 ieee80211_bss_info_change_notify(sdata, changed); 1371 ieee80211_mbss_info_change_notify(sdata, changed);
1371#endif 1372#endif
1372 } 1373 }
1373 1374
@@ -1553,6 +1554,20 @@ static int ieee80211_change_station(struct wiphy *wiphy,
1553 1554
1554 mutex_unlock(&local->sta_mtx); 1555 mutex_unlock(&local->sta_mtx);
1555 1556
1557 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
1558 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) &&
1559 sta->known_smps_mode != sta->sdata->bss->req_smps &&
1560 test_sta_flag(sta, WLAN_STA_AUTHORIZED) &&
1561 sta_info_tx_streams(sta) != 1) {
1562 ht_dbg(sta->sdata,
1563 "%pM just authorized and MIMO capable - update SMPS\n",
1564 sta->sta.addr);
1565 ieee80211_send_smps_action(sta->sdata,
1566 sta->sdata->bss->req_smps,
1567 sta->sta.addr,
1568 sta->sdata->vif.bss_conf.bssid);
1569 }
1570
1556 if (sdata->vif.type == NL80211_IFTYPE_STATION && 1571 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
1557 params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) { 1572 params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)) {
1558 ieee80211_recalc_ps(local, -1); 1573 ieee80211_recalc_ps(local, -1);
@@ -2337,8 +2352,92 @@ static int ieee80211_testmode_dump(struct wiphy *wiphy,
2337} 2352}
2338#endif 2353#endif
2339 2354
2340int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata, 2355int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
2341 enum ieee80211_smps_mode smps_mode) 2356 enum ieee80211_smps_mode smps_mode)
2357{
2358 struct sta_info *sta;
2359 enum ieee80211_smps_mode old_req;
2360 int i;
2361
2362 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP))
2363 return -EINVAL;
2364
2365 if (sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
2366 return 0;
2367
2368 old_req = sdata->u.ap.req_smps;
2369 sdata->u.ap.req_smps = smps_mode;
2370
2371 /* AUTOMATIC doesn't mean much for AP - don't allow it */
2372 if (old_req == smps_mode ||
2373 smps_mode == IEEE80211_SMPS_AUTOMATIC)
2374 return 0;
2375
2376 /* If no associated stations, there's no need to do anything */
2377 if (!atomic_read(&sdata->u.ap.num_mcast_sta)) {
2378 sdata->smps_mode = smps_mode;
2379 ieee80211_queue_work(&sdata->local->hw, &sdata->recalc_smps);
2380 return 0;
2381 }
2382
2383 ht_dbg(sdata,
2384 "SMSP %d requested in AP mode, sending Action frame to %d stations\n",
2385 smps_mode, atomic_read(&sdata->u.ap.num_mcast_sta));
2386
2387 mutex_lock(&sdata->local->sta_mtx);
2388 for (i = 0; i < STA_HASH_SIZE; i++) {
2389 for (sta = rcu_dereference_protected(sdata->local->sta_hash[i],
2390 lockdep_is_held(&sdata->local->sta_mtx));
2391 sta;
2392 sta = rcu_dereference_protected(sta->hnext,
2393 lockdep_is_held(&sdata->local->sta_mtx))) {
2394 /*
2395 * Only stations associated to our AP and
2396 * associated VLANs
2397 */
2398 if (sta->sdata->bss != &sdata->u.ap)
2399 continue;
2400
2401 /* This station doesn't support MIMO - skip it */
2402 if (sta_info_tx_streams(sta) == 1)
2403 continue;
2404
2405 /*
2406 * Don't wake up a STA just to send the action frame
2407 * unless we are getting more restrictive.
2408 */
2409 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
2410 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
2411 smps_mode)) {
2412 ht_dbg(sdata,
2413 "Won't send SMPS to sleeping STA %pM\n",
2414 sta->sta.addr);
2415 continue;
2416 }
2417
2418 /*
2419 * If the STA is not authorized, wait until it gets
2420 * authorized and the action frame will be sent then.
2421 */
2422 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2423 continue;
2424
2425 ht_dbg(sdata, "Sending SMPS to %pM\n", sta->sta.addr);
2426 ieee80211_send_smps_action(sdata, smps_mode,
2427 sta->sta.addr,
2428 sdata->vif.bss_conf.bssid);
2429 }
2430 }
2431 mutex_unlock(&sdata->local->sta_mtx);
2432
2433 sdata->smps_mode = smps_mode;
2434 ieee80211_queue_work(&sdata->local->hw, &sdata->recalc_smps);
2435
2436 return 0;
2437}
2438
2439int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
2440 enum ieee80211_smps_mode smps_mode)
2342{ 2441{
2343 const u8 *ap; 2442 const u8 *ap;
2344 enum ieee80211_smps_mode old_req; 2443 enum ieee80211_smps_mode old_req;
@@ -2346,6 +2445,9 @@ int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata,
2346 2445
2347 lockdep_assert_held(&sdata->wdev.mtx); 2446 lockdep_assert_held(&sdata->wdev.mtx);
2348 2447
2448 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_STATION))
2449 return -EINVAL;
2450
2349 old_req = sdata->u.mgd.req_smps; 2451 old_req = sdata->u.mgd.req_smps;
2350 sdata->u.mgd.req_smps = smps_mode; 2452 sdata->u.mgd.req_smps = smps_mode;
2351 2453
@@ -2386,8 +2488,7 @@ static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2386 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 2488 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2387 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); 2489 struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
2388 2490
2389 if (sdata->vif.type != NL80211_IFTYPE_STATION && 2491 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2390 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2391 return -EOPNOTSUPP; 2492 return -EOPNOTSUPP;
2392 2493
2393 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS)) 2494 if (!(local->hw.flags & IEEE80211_HW_SUPPORTS_PS))
@@ -2402,7 +2503,7 @@ static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2402 2503
2403 /* no change, but if automatic follow powersave */ 2504 /* no change, but if automatic follow powersave */
2404 sdata_lock(sdata); 2505 sdata_lock(sdata);
2405 __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps); 2506 __ieee80211_request_smps_mgd(sdata, sdata->u.mgd.req_smps);
2406 sdata_unlock(sdata); 2507 sdata_unlock(sdata);
2407 2508
2408 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) 2509 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
@@ -2860,35 +2961,55 @@ void ieee80211_csa_finalize_work(struct work_struct *work)
2860 container_of(work, struct ieee80211_sub_if_data, 2961 container_of(work, struct ieee80211_sub_if_data,
2861 csa_finalize_work); 2962 csa_finalize_work);
2862 struct ieee80211_local *local = sdata->local; 2963 struct ieee80211_local *local = sdata->local;
2863 int err, changed; 2964 int err, changed = 0;
2864 2965
2865 if (!ieee80211_sdata_running(sdata)) 2966 if (!ieee80211_sdata_running(sdata))
2866 return; 2967 return;
2867 2968
2868 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
2869 return;
2870
2871 sdata->radar_required = sdata->csa_radar_required; 2969 sdata->radar_required = sdata->csa_radar_required;
2872 err = ieee80211_vif_change_channel(sdata, &local->csa_chandef, 2970 err = ieee80211_vif_change_channel(sdata, &local->csa_chandef,
2873 &changed); 2971 &changed);
2874 if (WARN_ON(err < 0)) 2972 if (WARN_ON(err < 0))
2875 return; 2973 return;
2876 2974
2877 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon); 2975 if (!local->use_chanctx) {
2878 if (err < 0) 2976 local->_oper_chandef = local->csa_chandef;
2879 return; 2977 ieee80211_hw_config(local, 0);
2978 }
2880 2979
2881 changed |= err; 2980 ieee80211_bss_info_change_notify(sdata, changed);
2882 kfree(sdata->u.ap.next_beacon); 2981
2883 sdata->u.ap.next_beacon = NULL; 2982 switch (sdata->vif.type) {
2983 case NL80211_IFTYPE_AP:
2984 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon);
2985 if (err < 0)
2986 return;
2987 changed |= err;
2988 kfree(sdata->u.ap.next_beacon);
2989 sdata->u.ap.next_beacon = NULL;
2990
2991 ieee80211_bss_info_change_notify(sdata, err);
2992 break;
2993 case NL80211_IFTYPE_ADHOC:
2994 ieee80211_ibss_finish_csa(sdata);
2995 break;
2996#ifdef CONFIG_MAC80211_MESH
2997 case NL80211_IFTYPE_MESH_POINT:
2998 err = ieee80211_mesh_finish_csa(sdata);
2999 if (err < 0)
3000 return;
3001 break;
3002#endif
3003 default:
3004 WARN_ON(1);
3005 return;
3006 }
2884 sdata->vif.csa_active = false; 3007 sdata->vif.csa_active = false;
2885 3008
2886 ieee80211_wake_queues_by_reason(&sdata->local->hw, 3009 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2887 IEEE80211_MAX_QUEUE_MAP, 3010 IEEE80211_MAX_QUEUE_MAP,
2888 IEEE80211_QUEUE_STOP_REASON_CSA); 3011 IEEE80211_QUEUE_STOP_REASON_CSA);
2889 3012
2890 ieee80211_bss_info_change_notify(sdata, changed);
2891
2892 cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef); 3013 cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef);
2893} 3014}
2894 3015
@@ -2899,6 +3020,7 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2899 struct ieee80211_local *local = sdata->local; 3020 struct ieee80211_local *local = sdata->local;
2900 struct ieee80211_chanctx_conf *chanctx_conf; 3021 struct ieee80211_chanctx_conf *chanctx_conf;
2901 struct ieee80211_chanctx *chanctx; 3022 struct ieee80211_chanctx *chanctx;
3023 struct ieee80211_if_mesh __maybe_unused *ifmsh;
2902 int err, num_chanctx; 3024 int err, num_chanctx;
2903 3025
2904 if (!list_empty(&local->roc_list) || local->scanning) 3026 if (!list_empty(&local->roc_list) || local->scanning)
@@ -2936,20 +3058,84 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2936 if (sdata->vif.csa_active) 3058 if (sdata->vif.csa_active)
2937 return -EBUSY; 3059 return -EBUSY;
2938 3060
2939 /* only handle AP for now. */
2940 switch (sdata->vif.type) { 3061 switch (sdata->vif.type) {
2941 case NL80211_IFTYPE_AP: 3062 case NL80211_IFTYPE_AP:
3063 sdata->csa_counter_offset_beacon =
3064 params->counter_offset_beacon;
3065 sdata->csa_counter_offset_presp = params->counter_offset_presp;
3066 sdata->u.ap.next_beacon =
3067 cfg80211_beacon_dup(&params->beacon_after);
3068 if (!sdata->u.ap.next_beacon)
3069 return -ENOMEM;
3070
3071 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
3072 if (err < 0) {
3073 kfree(sdata->u.ap.next_beacon);
3074 return err;
3075 }
2942 break; 3076 break;
3077 case NL80211_IFTYPE_ADHOC:
3078 if (!sdata->vif.bss_conf.ibss_joined)
3079 return -EINVAL;
3080
3081 if (params->chandef.width != sdata->u.ibss.chandef.width)
3082 return -EINVAL;
3083
3084 switch (params->chandef.width) {
3085 case NL80211_CHAN_WIDTH_40:
3086 if (cfg80211_get_chandef_type(&params->chandef) !=
3087 cfg80211_get_chandef_type(&sdata->u.ibss.chandef))
3088 return -EINVAL;
3089 case NL80211_CHAN_WIDTH_5:
3090 case NL80211_CHAN_WIDTH_10:
3091 case NL80211_CHAN_WIDTH_20_NOHT:
3092 case NL80211_CHAN_WIDTH_20:
3093 break;
3094 default:
3095 return -EINVAL;
3096 }
3097
3098 /* changes into another band are not supported */
3099 if (sdata->u.ibss.chandef.chan->band !=
3100 params->chandef.chan->band)
3101 return -EINVAL;
3102
3103 err = ieee80211_ibss_csa_beacon(sdata, params);
3104 if (err < 0)
3105 return err;
3106 break;
3107#ifdef CONFIG_MAC80211_MESH
3108 case NL80211_IFTYPE_MESH_POINT:
3109 ifmsh = &sdata->u.mesh;
3110
3111 if (!ifmsh->mesh_id)
3112 return -EINVAL;
3113
3114 if (params->chandef.width != sdata->vif.bss_conf.chandef.width)
3115 return -EINVAL;
3116
3117 /* changes into another band are not supported */
3118 if (sdata->vif.bss_conf.chandef.chan->band !=
3119 params->chandef.chan->band)
3120 return -EINVAL;
3121
3122 ifmsh->chsw_init = true;
3123 if (!ifmsh->pre_value)
3124 ifmsh->pre_value = 1;
3125 else
3126 ifmsh->pre_value++;
3127
3128 err = ieee80211_mesh_csa_beacon(sdata, params, true);
3129 if (err < 0) {
3130 ifmsh->chsw_init = false;
3131 return err;
3132 }
3133 break;
3134#endif
2943 default: 3135 default:
2944 return -EOPNOTSUPP; 3136 return -EOPNOTSUPP;
2945 } 3137 }
2946 3138
2947 sdata->u.ap.next_beacon = cfg80211_beacon_dup(&params->beacon_after);
2948 if (!sdata->u.ap.next_beacon)
2949 return -ENOMEM;
2950
2951 sdata->csa_counter_offset_beacon = params->counter_offset_beacon;
2952 sdata->csa_counter_offset_presp = params->counter_offset_presp;
2953 sdata->csa_radar_required = params->radar_required; 3139 sdata->csa_radar_required = params->radar_required;
2954 3140
2955 if (params->block_tx) 3141 if (params->block_tx)
@@ -2957,10 +3143,6 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2957 IEEE80211_MAX_QUEUE_MAP, 3143 IEEE80211_MAX_QUEUE_MAP,
2958 IEEE80211_QUEUE_STOP_REASON_CSA); 3144 IEEE80211_QUEUE_STOP_REASON_CSA);
2959 3145
2960 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
2961 if (err < 0)
2962 return err;
2963
2964 local->csa_chandef = params->chandef; 3146 local->csa_chandef = params->chandef;
2965 sdata->vif.csa_active = true; 3147 sdata->vif.csa_active = true;
2966 3148
@@ -3014,7 +3196,8 @@ static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
3014 need_offchan = true; 3196 need_offchan = true;
3015 if (!ieee80211_is_action(mgmt->frame_control) || 3197 if (!ieee80211_is_action(mgmt->frame_control) ||
3016 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC || 3198 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
3017 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED) 3199 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
3200 mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
3018 break; 3201 break;
3019 rcu_read_lock(); 3202 rcu_read_lock();
3020 sta = sta_info_get(sdata, mgmt->da); 3203 sta = sta_info_get(sdata, mgmt->da);
diff --git a/net/mac80211/chan.c b/net/mac80211/chan.c
index 3a4764b2869e..03ba6b5c5373 100644
--- a/net/mac80211/chan.c
+++ b/net/mac80211/chan.c
@@ -453,11 +453,6 @@ int ieee80211_vif_change_channel(struct ieee80211_sub_if_data *sdata,
453 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL; 453 chanctx_changed |= IEEE80211_CHANCTX_CHANGE_CHANNEL;
454 drv_change_chanctx(local, ctx, chanctx_changed); 454 drv_change_chanctx(local, ctx, chanctx_changed);
455 455
456 if (!local->use_chanctx) {
457 local->_oper_chandef = *chandef;
458 ieee80211_hw_config(local, 0);
459 }
460
461 ieee80211_recalc_chanctx_chantype(local, ctx); 456 ieee80211_recalc_chanctx_chantype(local, ctx);
462 ieee80211_recalc_smps_chanctx(local, ctx); 457 ieee80211_recalc_smps_chanctx(local, ctx);
463 ieee80211_recalc_radar_chanctx(local, ctx); 458 ieee80211_recalc_radar_chanctx(local, ctx);
diff --git a/net/mac80211/debug.h b/net/mac80211/debug.h
index 4ccc5ed6237d..493d68061f0c 100644
--- a/net/mac80211/debug.h
+++ b/net/mac80211/debug.h
@@ -44,6 +44,12 @@
44#define MAC80211_MESH_SYNC_DEBUG 0 44#define MAC80211_MESH_SYNC_DEBUG 0
45#endif 45#endif
46 46
47#ifdef CONFIG_MAC80211_MESH_CSA_DEBUG
48#define MAC80211_MESH_CSA_DEBUG 1
49#else
50#define MAC80211_MESH_CSA_DEBUG 0
51#endif
52
47#ifdef CONFIG_MAC80211_MESH_PS_DEBUG 53#ifdef CONFIG_MAC80211_MESH_PS_DEBUG
48#define MAC80211_MESH_PS_DEBUG 1 54#define MAC80211_MESH_PS_DEBUG 1
49#else 55#else
@@ -157,6 +163,10 @@ do { \
157 _sdata_dbg(MAC80211_MESH_SYNC_DEBUG, \ 163 _sdata_dbg(MAC80211_MESH_SYNC_DEBUG, \
158 sdata, fmt, ##__VA_ARGS__) 164 sdata, fmt, ##__VA_ARGS__)
159 165
166#define mcsa_dbg(sdata, fmt, ...) \
167 _sdata_dbg(MAC80211_MESH_CSA_DEBUG, \
168 sdata, fmt, ##__VA_ARGS__)
169
160#define mps_dbg(sdata, fmt, ...) \ 170#define mps_dbg(sdata, fmt, ...) \
161 _sdata_dbg(MAC80211_MESH_PS_DEBUG, \ 171 _sdata_dbg(MAC80211_MESH_PS_DEBUG, \
162 sdata, fmt, ##__VA_ARGS__) 172 sdata, fmt, ##__VA_ARGS__)
diff --git a/net/mac80211/debugfs.c b/net/mac80211/debugfs.c
index b0e32d628114..5c090e41d9bb 100644
--- a/net/mac80211/debugfs.c
+++ b/net/mac80211/debugfs.c
@@ -103,54 +103,57 @@ static ssize_t hwflags_read(struct file *file, char __user *user_buf,
103 if (!buf) 103 if (!buf)
104 return 0; 104 return 0;
105 105
106 sf += snprintf(buf, mxln - sf, "0x%x\n", local->hw.flags); 106 sf += scnprintf(buf, mxln - sf, "0x%x\n", local->hw.flags);
107 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL) 107 if (local->hw.flags & IEEE80211_HW_HAS_RATE_CONTROL)
108 sf += snprintf(buf + sf, mxln - sf, "HAS_RATE_CONTROL\n"); 108 sf += scnprintf(buf + sf, mxln - sf, "HAS_RATE_CONTROL\n");
109 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS) 109 if (local->hw.flags & IEEE80211_HW_RX_INCLUDES_FCS)
110 sf += snprintf(buf + sf, mxln - sf, "RX_INCLUDES_FCS\n"); 110 sf += scnprintf(buf + sf, mxln - sf, "RX_INCLUDES_FCS\n");
111 if (local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING) 111 if (local->hw.flags & IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING)
112 sf += snprintf(buf + sf, mxln - sf, 112 sf += scnprintf(buf + sf, mxln - sf,
113 "HOST_BCAST_PS_BUFFERING\n"); 113 "HOST_BCAST_PS_BUFFERING\n");
114 if (local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE) 114 if (local->hw.flags & IEEE80211_HW_2GHZ_SHORT_SLOT_INCAPABLE)
115 sf += snprintf(buf + sf, mxln - sf, 115 sf += scnprintf(buf + sf, mxln - sf,
116 "2GHZ_SHORT_SLOT_INCAPABLE\n"); 116 "2GHZ_SHORT_SLOT_INCAPABLE\n");
117 if (local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE) 117 if (local->hw.flags & IEEE80211_HW_2GHZ_SHORT_PREAMBLE_INCAPABLE)
118 sf += snprintf(buf + sf, mxln - sf, 118 sf += scnprintf(buf + sf, mxln - sf,
119 "2GHZ_SHORT_PREAMBLE_INCAPABLE\n"); 119 "2GHZ_SHORT_PREAMBLE_INCAPABLE\n");
120 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) 120 if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
121 sf += snprintf(buf + sf, mxln - sf, "SIGNAL_UNSPEC\n"); 121 sf += scnprintf(buf + sf, mxln - sf, "SIGNAL_UNSPEC\n");
122 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) 122 if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
123 sf += snprintf(buf + sf, mxln - sf, "SIGNAL_DBM\n"); 123 sf += scnprintf(buf + sf, mxln - sf, "SIGNAL_DBM\n");
124 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC) 124 if (local->hw.flags & IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC)
125 sf += snprintf(buf + sf, mxln - sf, "NEED_DTIM_BEFORE_ASSOC\n"); 125 sf += scnprintf(buf + sf, mxln - sf,
126 "NEED_DTIM_BEFORE_ASSOC\n");
126 if (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT) 127 if (local->hw.flags & IEEE80211_HW_SPECTRUM_MGMT)
127 sf += snprintf(buf + sf, mxln - sf, "SPECTRUM_MGMT\n"); 128 sf += scnprintf(buf + sf, mxln - sf, "SPECTRUM_MGMT\n");
128 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION) 129 if (local->hw.flags & IEEE80211_HW_AMPDU_AGGREGATION)
129 sf += snprintf(buf + sf, mxln - sf, "AMPDU_AGGREGATION\n"); 130 sf += scnprintf(buf + sf, mxln - sf, "AMPDU_AGGREGATION\n");
130 if (local->hw.flags & IEEE80211_HW_SUPPORTS_PS) 131 if (local->hw.flags & IEEE80211_HW_SUPPORTS_PS)
131 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_PS\n"); 132 sf += scnprintf(buf + sf, mxln - sf, "SUPPORTS_PS\n");
132 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK) 133 if (local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK)
133 sf += snprintf(buf + sf, mxln - sf, "PS_NULLFUNC_STACK\n"); 134 sf += scnprintf(buf + sf, mxln - sf, "PS_NULLFUNC_STACK\n");
134 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) 135 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
135 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_DYNAMIC_PS\n"); 136 sf += scnprintf(buf + sf, mxln - sf, "SUPPORTS_DYNAMIC_PS\n");
136 if (local->hw.flags & IEEE80211_HW_MFP_CAPABLE) 137 if (local->hw.flags & IEEE80211_HW_MFP_CAPABLE)
137 sf += snprintf(buf + sf, mxln - sf, "MFP_CAPABLE\n"); 138 sf += scnprintf(buf + sf, mxln - sf, "MFP_CAPABLE\n");
138 if (local->hw.flags & IEEE80211_HW_SUPPORTS_STATIC_SMPS) 139 if (local->hw.flags & IEEE80211_HW_SUPPORTS_STATIC_SMPS)
139 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_STATIC_SMPS\n"); 140 sf += scnprintf(buf + sf, mxln - sf, "SUPPORTS_STATIC_SMPS\n");
140 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS) 141 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS)
141 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_DYNAMIC_SMPS\n"); 142 sf += scnprintf(buf + sf, mxln - sf,
143 "SUPPORTS_DYNAMIC_SMPS\n");
142 if (local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD) 144 if (local->hw.flags & IEEE80211_HW_SUPPORTS_UAPSD)
143 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_UAPSD\n"); 145 sf += scnprintf(buf + sf, mxln - sf, "SUPPORTS_UAPSD\n");
144 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) 146 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
145 sf += snprintf(buf + sf, mxln - sf, "REPORTS_TX_ACK_STATUS\n"); 147 sf += scnprintf(buf + sf, mxln - sf,
148 "REPORTS_TX_ACK_STATUS\n");
146 if (local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR) 149 if (local->hw.flags & IEEE80211_HW_CONNECTION_MONITOR)
147 sf += snprintf(buf + sf, mxln - sf, "CONNECTION_MONITOR\n"); 150 sf += scnprintf(buf + sf, mxln - sf, "CONNECTION_MONITOR\n");
148 if (local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK) 151 if (local->hw.flags & IEEE80211_HW_SUPPORTS_PER_STA_GTK)
149 sf += snprintf(buf + sf, mxln - sf, "SUPPORTS_PER_STA_GTK\n"); 152 sf += scnprintf(buf + sf, mxln - sf, "SUPPORTS_PER_STA_GTK\n");
150 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS) 153 if (local->hw.flags & IEEE80211_HW_AP_LINK_PS)
151 sf += snprintf(buf + sf, mxln - sf, "AP_LINK_PS\n"); 154 sf += scnprintf(buf + sf, mxln - sf, "AP_LINK_PS\n");
152 if (local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW) 155 if (local->hw.flags & IEEE80211_HW_TX_AMPDU_SETUP_IN_HW)
153 sf += snprintf(buf + sf, mxln - sf, "TX_AMPDU_SETUP_IN_HW\n"); 156 sf += scnprintf(buf + sf, mxln - sf, "TX_AMPDU_SETUP_IN_HW\n");
154 157
155 rv = simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf)); 158 rv = simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
156 kfree(buf); 159 kfree(buf);
diff --git a/net/mac80211/debugfs_netdev.c b/net/mac80211/debugfs_netdev.c
index cafe614ef93d..04b5a14c8a05 100644
--- a/net/mac80211/debugfs_netdev.c
+++ b/net/mac80211/debugfs_netdev.c
@@ -224,12 +224,15 @@ static int ieee80211_set_smps(struct ieee80211_sub_if_data *sdata,
224 smps_mode == IEEE80211_SMPS_AUTOMATIC)) 224 smps_mode == IEEE80211_SMPS_AUTOMATIC))
225 return -EINVAL; 225 return -EINVAL;
226 226
227 /* supported only on managed interfaces for now */ 227 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
228 if (sdata->vif.type != NL80211_IFTYPE_STATION) 228 sdata->vif.type != NL80211_IFTYPE_AP)
229 return -EOPNOTSUPP; 229 return -EOPNOTSUPP;
230 230
231 sdata_lock(sdata); 231 sdata_lock(sdata);
232 err = __ieee80211_request_smps(sdata, smps_mode); 232 if (sdata->vif.type == NL80211_IFTYPE_STATION)
233 err = __ieee80211_request_smps_mgd(sdata, smps_mode);
234 else
235 err = __ieee80211_request_smps_ap(sdata, smps_mode);
233 sdata_unlock(sdata); 236 sdata_unlock(sdata);
234 237
235 return err; 238 return err;
@@ -245,12 +248,15 @@ static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
245static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_sub_if_data *sdata, 248static ssize_t ieee80211_if_fmt_smps(const struct ieee80211_sub_if_data *sdata,
246 char *buf, int buflen) 249 char *buf, int buflen)
247{ 250{
248 if (sdata->vif.type != NL80211_IFTYPE_STATION) 251 if (sdata->vif.type == NL80211_IFTYPE_STATION)
249 return -EOPNOTSUPP; 252 return snprintf(buf, buflen, "request: %s\nused: %s\n",
250 253 smps_modes[sdata->u.mgd.req_smps],
251 return snprintf(buf, buflen, "request: %s\nused: %s\n", 254 smps_modes[sdata->smps_mode]);
252 smps_modes[sdata->u.mgd.req_smps], 255 if (sdata->vif.type == NL80211_IFTYPE_AP)
253 smps_modes[sdata->smps_mode]); 256 return snprintf(buf, buflen, "request: %s\nused: %s\n",
257 smps_modes[sdata->u.ap.req_smps],
258 smps_modes[sdata->smps_mode]);
259 return -EINVAL;
254} 260}
255 261
256static ssize_t ieee80211_if_parse_smps(struct ieee80211_sub_if_data *sdata, 262static ssize_t ieee80211_if_parse_smps(struct ieee80211_sub_if_data *sdata,
@@ -563,6 +569,7 @@ static void add_sta_files(struct ieee80211_sub_if_data *sdata)
563static void add_ap_files(struct ieee80211_sub_if_data *sdata) 569static void add_ap_files(struct ieee80211_sub_if_data *sdata)
564{ 570{
565 DEBUGFS_ADD(num_mcast_sta); 571 DEBUGFS_ADD(num_mcast_sta);
572 DEBUGFS_ADD_MODE(smps, 0600);
566 DEBUGFS_ADD(num_sta_ps); 573 DEBUGFS_ADD(num_sta_ps);
567 DEBUGFS_ADD(dtim_count); 574 DEBUGFS_ADD(dtim_count);
568 DEBUGFS_ADD(num_buffered_multicast); 575 DEBUGFS_ADD(num_buffered_multicast);
diff --git a/net/mac80211/driver-ops.h b/net/mac80211/driver-ops.h
index b3ea11f3d526..5d03c47c0a4c 100644
--- a/net/mac80211/driver-ops.h
+++ b/net/mac80211/driver-ops.h
@@ -1085,4 +1085,31 @@ drv_channel_switch_beacon(struct ieee80211_sub_if_data *sdata,
1085 } 1085 }
1086} 1086}
1087 1087
1088static inline int drv_join_ibss(struct ieee80211_local *local,
1089 struct ieee80211_sub_if_data *sdata)
1090{
1091 int ret = 0;
1092
1093 might_sleep();
1094 check_sdata_in_driver(sdata);
1095
1096 trace_drv_join_ibss(local, sdata, &sdata->vif.bss_conf);
1097 if (local->ops->join_ibss)
1098 ret = local->ops->join_ibss(&local->hw, &sdata->vif);
1099 trace_drv_return_int(local, ret);
1100 return ret;
1101}
1102
1103static inline void drv_leave_ibss(struct ieee80211_local *local,
1104 struct ieee80211_sub_if_data *sdata)
1105{
1106 might_sleep();
1107 check_sdata_in_driver(sdata);
1108
1109 trace_drv_leave_ibss(local, sdata);
1110 if (local->ops->leave_ibss)
1111 local->ops->leave_ibss(&local->hw, &sdata->vif);
1112 trace_drv_return_void(local);
1113}
1114
1088#endif /* __MAC80211_DRIVER_OPS */ 1115#endif /* __MAC80211_DRIVER_OPS */
diff --git a/net/mac80211/ht.c b/net/mac80211/ht.c
index 529bf58bc145..9a8be8f69224 100644
--- a/net/mac80211/ht.c
+++ b/net/mac80211/ht.c
@@ -448,14 +448,25 @@ int ieee80211_send_smps_action(struct ieee80211_sub_if_data *sdata,
448 return 0; 448 return 0;
449} 449}
450 450
451void ieee80211_request_smps_work(struct work_struct *work) 451void ieee80211_request_smps_mgd_work(struct work_struct *work)
452{ 452{
453 struct ieee80211_sub_if_data *sdata = 453 struct ieee80211_sub_if_data *sdata =
454 container_of(work, struct ieee80211_sub_if_data, 454 container_of(work, struct ieee80211_sub_if_data,
455 u.mgd.request_smps_work); 455 u.mgd.request_smps_work);
456 456
457 sdata_lock(sdata); 457 sdata_lock(sdata);
458 __ieee80211_request_smps(sdata, sdata->u.mgd.driver_smps_mode); 458 __ieee80211_request_smps_mgd(sdata, sdata->u.mgd.driver_smps_mode);
459 sdata_unlock(sdata);
460}
461
462void ieee80211_request_smps_ap_work(struct work_struct *work)
463{
464 struct ieee80211_sub_if_data *sdata =
465 container_of(work, struct ieee80211_sub_if_data,
466 u.ap.request_smps_work);
467
468 sdata_lock(sdata);
469 __ieee80211_request_smps_ap(sdata, sdata->u.ap.driver_smps_mode);
459 sdata_unlock(sdata); 470 sdata_unlock(sdata);
460} 471}
461 472
@@ -464,19 +475,29 @@ void ieee80211_request_smps(struct ieee80211_vif *vif,
464{ 475{
465 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 476 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
466 477
467 if (WARN_ON(vif->type != NL80211_IFTYPE_STATION)) 478 if (WARN_ON_ONCE(vif->type != NL80211_IFTYPE_STATION &&
479 vif->type != NL80211_IFTYPE_AP))
468 return; 480 return;
469 481
470 if (WARN_ON(smps_mode == IEEE80211_SMPS_OFF)) 482 if (WARN_ON(smps_mode == IEEE80211_SMPS_OFF))
471 smps_mode = IEEE80211_SMPS_AUTOMATIC; 483 smps_mode = IEEE80211_SMPS_AUTOMATIC;
472 484
473 if (sdata->u.mgd.driver_smps_mode == smps_mode) 485 if (vif->type == NL80211_IFTYPE_STATION) {
474 return; 486 if (sdata->u.mgd.driver_smps_mode == smps_mode)
475 487 return;
476 sdata->u.mgd.driver_smps_mode = smps_mode; 488 sdata->u.mgd.driver_smps_mode = smps_mode;
477 489 ieee80211_queue_work(&sdata->local->hw,
478 ieee80211_queue_work(&sdata->local->hw, 490 &sdata->u.mgd.request_smps_work);
479 &sdata->u.mgd.request_smps_work); 491 } else {
492 /* AUTOMATIC is meaningless in AP mode */
493 if (WARN_ON_ONCE(smps_mode == IEEE80211_SMPS_AUTOMATIC))
494 return;
495 if (sdata->u.ap.driver_smps_mode == smps_mode)
496 return;
497 sdata->u.ap.driver_smps_mode = smps_mode;
498 ieee80211_queue_work(&sdata->local->hw,
499 &sdata->u.ap.request_smps_work);
500 }
480} 501}
481/* this might change ... don't want non-open drivers using it */ 502/* this might change ... don't want non-open drivers using it */
482EXPORT_SYMBOL_GPL(ieee80211_request_smps); 503EXPORT_SYMBOL_GPL(ieee80211_request_smps);
diff --git a/net/mac80211/ibss.c b/net/mac80211/ibss.c
index a12afe77bb26..27a39de89679 100644
--- a/net/mac80211/ibss.c
+++ b/net/mac80211/ibss.c
@@ -39,7 +39,8 @@ ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
39 const int beacon_int, const u32 basic_rates, 39 const int beacon_int, const u32 basic_rates,
40 const u16 capability, u64 tsf, 40 const u16 capability, u64 tsf,
41 struct cfg80211_chan_def *chandef, 41 struct cfg80211_chan_def *chandef,
42 bool *have_higher_than_11mbit) 42 bool *have_higher_than_11mbit,
43 struct cfg80211_csa_settings *csa_settings)
43{ 44{
44 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 45 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
45 struct ieee80211_local *local = sdata->local; 46 struct ieee80211_local *local = sdata->local;
@@ -59,6 +60,7 @@ ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
59 2 + 8 /* max Supported Rates */ + 60 2 + 8 /* max Supported Rates */ +
60 3 /* max DS params */ + 61 3 /* max DS params */ +
61 4 /* IBSS params */ + 62 4 /* IBSS params */ +
63 5 /* Channel Switch Announcement */ +
62 2 + (IEEE80211_MAX_SUPP_RATES - 8) + 64 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
63 2 + sizeof(struct ieee80211_ht_cap) + 65 2 + sizeof(struct ieee80211_ht_cap) +
64 2 + sizeof(struct ieee80211_ht_operation) + 66 2 + sizeof(struct ieee80211_ht_operation) +
@@ -135,6 +137,16 @@ ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
135 *pos++ = 0; 137 *pos++ = 0;
136 *pos++ = 0; 138 *pos++ = 0;
137 139
140 if (csa_settings) {
141 *pos++ = WLAN_EID_CHANNEL_SWITCH;
142 *pos++ = 3;
143 *pos++ = csa_settings->block_tx ? 1 : 0;
144 *pos++ = ieee80211_frequency_to_channel(
145 csa_settings->chandef.chan->center_freq);
146 sdata->csa_counter_offset_beacon = (pos - presp->head);
147 *pos++ = csa_settings->count;
148 }
149
138 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */ 150 /* put the remaining rates in WLAN_EID_EXT_SUPP_RATES */
139 if (rates_n > 8) { 151 if (rates_n > 8) {
140 *pos++ = WLAN_EID_EXT_SUPP_RATES; 152 *pos++ = WLAN_EID_EXT_SUPP_RATES;
@@ -217,6 +229,8 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
217 struct beacon_data *presp; 229 struct beacon_data *presp;
218 enum nl80211_bss_scan_width scan_width; 230 enum nl80211_bss_scan_width scan_width;
219 bool have_higher_than_11mbit; 231 bool have_higher_than_11mbit;
232 bool radar_required = false;
233 int err;
220 234
221 sdata_assert_lock(sdata); 235 sdata_assert_lock(sdata);
222 236
@@ -235,6 +249,7 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
235 ieee80211_bss_info_change_notify(sdata, 249 ieee80211_bss_info_change_notify(sdata,
236 BSS_CHANGED_IBSS | 250 BSS_CHANGED_IBSS |
237 BSS_CHANGED_BEACON_ENABLED); 251 BSS_CHANGED_BEACON_ENABLED);
252 drv_leave_ibss(local, sdata);
238 } 253 }
239 254
240 presp = rcu_dereference_protected(ifibss->presp, 255 presp = rcu_dereference_protected(ifibss->presp,
@@ -259,6 +274,23 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
259 } 274 }
260 chandef.width = NL80211_CHAN_WIDTH_20; 275 chandef.width = NL80211_CHAN_WIDTH_20;
261 chandef.center_freq1 = chan->center_freq; 276 chandef.center_freq1 = chan->center_freq;
277 /* check again for downgraded chandef */
278 if (!cfg80211_reg_can_beacon(local->hw.wiphy, &chandef)) {
279 sdata_info(sdata,
280 "Failed to join IBSS, beacons forbidden\n");
281 return;
282 }
283 }
284
285 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
286 &chandef);
287 if (err > 0) {
288 if (!ifibss->userspace_handles_dfs) {
289 sdata_info(sdata,
290 "Failed to join IBSS, DFS channel without control program\n");
291 return;
292 }
293 radar_required = true;
262 } 294 }
263 295
264 ieee80211_vif_release_channel(sdata); 296 ieee80211_vif_release_channel(sdata);
@@ -276,13 +308,14 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
276 308
277 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates, 309 presp = ieee80211_ibss_build_presp(sdata, beacon_int, basic_rates,
278 capability, tsf, &chandef, 310 capability, tsf, &chandef,
279 &have_higher_than_11mbit); 311 &have_higher_than_11mbit, NULL);
280 if (!presp) 312 if (!presp)
281 return; 313 return;
282 314
283 rcu_assign_pointer(ifibss->presp, presp); 315 rcu_assign_pointer(ifibss->presp, presp);
284 mgmt = (void *)presp->head; 316 mgmt = (void *)presp->head;
285 317
318 sdata->radar_required = radar_required;
286 sdata->vif.bss_conf.enable_beacon = true; 319 sdata->vif.bss_conf.enable_beacon = true;
287 sdata->vif.bss_conf.beacon_int = beacon_int; 320 sdata->vif.bss_conf.beacon_int = beacon_int;
288 sdata->vif.bss_conf.basic_rates = basic_rates; 321 sdata->vif.bss_conf.basic_rates = basic_rates;
@@ -317,11 +350,26 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
317 else 350 else
318 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE; 351 sdata->flags &= ~IEEE80211_SDATA_OPERATING_GMODE;
319 352
353 ieee80211_set_wmm_default(sdata, true);
354
320 sdata->vif.bss_conf.ibss_joined = true; 355 sdata->vif.bss_conf.ibss_joined = true;
321 sdata->vif.bss_conf.ibss_creator = creator; 356 sdata->vif.bss_conf.ibss_creator = creator;
322 ieee80211_bss_info_change_notify(sdata, bss_change);
323 357
324 ieee80211_set_wmm_default(sdata, true); 358 err = drv_join_ibss(local, sdata);
359 if (err) {
360 sdata->vif.bss_conf.ibss_joined = false;
361 sdata->vif.bss_conf.ibss_creator = false;
362 sdata->vif.bss_conf.enable_beacon = false;
363 sdata->vif.bss_conf.ssid_len = 0;
364 RCU_INIT_POINTER(ifibss->presp, NULL);
365 kfree_rcu(presp, rcu_head);
366 ieee80211_vif_release_channel(sdata);
367 sdata_info(sdata, "Failed to join IBSS, driver failure: %d\n",
368 err);
369 return;
370 }
371
372 ieee80211_bss_info_change_notify(sdata, bss_change);
325 373
326 ifibss->state = IEEE80211_IBSS_MLME_JOINED; 374 ifibss->state = IEEE80211_IBSS_MLME_JOINED;
327 mod_timer(&ifibss->timer, 375 mod_timer(&ifibss->timer,
@@ -416,6 +464,115 @@ static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
416 tsf, false); 464 tsf, false);
417} 465}
418 466
467int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
468 struct cfg80211_csa_settings *csa_settings)
469{
470 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
471 struct beacon_data *presp, *old_presp;
472 struct cfg80211_bss *cbss;
473 const struct cfg80211_bss_ies *ies;
474 u16 capability;
475 u64 tsf;
476 int ret = 0;
477
478 sdata_assert_lock(sdata);
479
480 capability = WLAN_CAPABILITY_IBSS;
481
482 if (ifibss->privacy)
483 capability |= WLAN_CAPABILITY_PRIVACY;
484
485 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
486 ifibss->bssid, ifibss->ssid,
487 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
488 WLAN_CAPABILITY_PRIVACY,
489 capability);
490
491 if (WARN_ON(!cbss)) {
492 ret = -EINVAL;
493 goto out;
494 }
495
496 rcu_read_lock();
497 ies = rcu_dereference(cbss->ies);
498 tsf = ies->tsf;
499 rcu_read_unlock();
500 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
501
502 old_presp = rcu_dereference_protected(ifibss->presp,
503 lockdep_is_held(&sdata->wdev.mtx));
504
505 presp = ieee80211_ibss_build_presp(sdata,
506 sdata->vif.bss_conf.beacon_int,
507 sdata->vif.bss_conf.basic_rates,
508 capability, tsf, &ifibss->chandef,
509 NULL, csa_settings);
510 if (!presp) {
511 ret = -ENOMEM;
512 goto out;
513 }
514
515 rcu_assign_pointer(ifibss->presp, presp);
516 if (old_presp)
517 kfree_rcu(old_presp, rcu_head);
518
519 /* it might not send the beacon for a while. send an action frame
520 * immediately to announce the channel switch.
521 */
522 if (csa_settings)
523 ieee80211_send_action_csa(sdata, csa_settings);
524
525 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
526 out:
527 return ret;
528}
529
530int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
531{
532 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
533 struct cfg80211_bss *cbss;
534 int err;
535 u16 capability;
536
537 sdata_lock(sdata);
538 /* update cfg80211 bss information with the new channel */
539 if (!is_zero_ether_addr(ifibss->bssid)) {
540 capability = WLAN_CAPABILITY_IBSS;
541
542 if (ifibss->privacy)
543 capability |= WLAN_CAPABILITY_PRIVACY;
544
545 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
546 ifibss->chandef.chan,
547 ifibss->bssid, ifibss->ssid,
548 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
549 WLAN_CAPABILITY_PRIVACY,
550 capability);
551 /* XXX: should not really modify cfg80211 data */
552 if (cbss) {
553 cbss->channel = sdata->local->csa_chandef.chan;
554 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
555 }
556 }
557
558 ifibss->chandef = sdata->local->csa_chandef;
559
560 /* generate the beacon */
561 err = ieee80211_ibss_csa_beacon(sdata, NULL);
562 sdata_unlock(sdata);
563 if (err < 0)
564 return err;
565
566 return 0;
567}
568
569void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata)
570{
571 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
572
573 cancel_work_sync(&ifibss->csa_connection_drop_work);
574}
575
419static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta) 576static struct sta_info *ieee80211_ibss_finish_sta(struct sta_info *sta)
420 __acquires(RCU) 577 __acquires(RCU)
421{ 578{
@@ -499,6 +656,319 @@ ieee80211_ibss_add_sta(struct ieee80211_sub_if_data *sdata, const u8 *bssid,
499 return ieee80211_ibss_finish_sta(sta); 656 return ieee80211_ibss_finish_sta(sta);
500} 657}
501 658
659static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
660{
661 struct ieee80211_local *local = sdata->local;
662 int active = 0;
663 struct sta_info *sta;
664
665 sdata_assert_lock(sdata);
666
667 rcu_read_lock();
668
669 list_for_each_entry_rcu(sta, &local->sta_list, list) {
670 if (sta->sdata == sdata &&
671 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
672 jiffies)) {
673 active++;
674 break;
675 }
676 }
677
678 rcu_read_unlock();
679
680 return active;
681}
682
683static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
684{
685 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
686 struct ieee80211_local *local = sdata->local;
687 struct cfg80211_bss *cbss;
688 struct beacon_data *presp;
689 struct sta_info *sta;
690 int active_ibss;
691 u16 capability;
692
693 active_ibss = ieee80211_sta_active_ibss(sdata);
694
695 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
696 capability = WLAN_CAPABILITY_IBSS;
697
698 if (ifibss->privacy)
699 capability |= WLAN_CAPABILITY_PRIVACY;
700
701 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
702 ifibss->bssid, ifibss->ssid,
703 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
704 WLAN_CAPABILITY_PRIVACY,
705 capability);
706
707 if (cbss) {
708 cfg80211_unlink_bss(local->hw.wiphy, cbss);
709 cfg80211_put_bss(sdata->local->hw.wiphy, cbss);
710 }
711 }
712
713 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
714
715 sta_info_flush(sdata);
716
717 spin_lock_bh(&ifibss->incomplete_lock);
718 while (!list_empty(&ifibss->incomplete_stations)) {
719 sta = list_first_entry(&ifibss->incomplete_stations,
720 struct sta_info, list);
721 list_del(&sta->list);
722 spin_unlock_bh(&ifibss->incomplete_lock);
723
724 sta_info_free(local, sta);
725 spin_lock_bh(&ifibss->incomplete_lock);
726 }
727 spin_unlock_bh(&ifibss->incomplete_lock);
728
729 netif_carrier_off(sdata->dev);
730
731 sdata->vif.bss_conf.ibss_joined = false;
732 sdata->vif.bss_conf.ibss_creator = false;
733 sdata->vif.bss_conf.enable_beacon = false;
734 sdata->vif.bss_conf.ssid_len = 0;
735
736 /* remove beacon */
737 presp = rcu_dereference_protected(ifibss->presp,
738 lockdep_is_held(&sdata->wdev.mtx));
739 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
740 if (presp)
741 kfree_rcu(presp, rcu_head);
742
743 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
744 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
745 BSS_CHANGED_IBSS);
746 drv_leave_ibss(local, sdata);
747 ieee80211_vif_release_channel(sdata);
748}
749
750static void ieee80211_csa_connection_drop_work(struct work_struct *work)
751{
752 struct ieee80211_sub_if_data *sdata =
753 container_of(work, struct ieee80211_sub_if_data,
754 u.ibss.csa_connection_drop_work);
755
756 ieee80211_ibss_disconnect(sdata);
757 synchronize_rcu();
758 skb_queue_purge(&sdata->skb_queue);
759
760 /* trigger a scan to find another IBSS network to join */
761 ieee80211_queue_work(&sdata->local->hw, &sdata->work);
762}
763
764static void ieee80211_ibss_csa_mark_radar(struct ieee80211_sub_if_data *sdata)
765{
766 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
767 int err;
768
769 /* if the current channel is a DFS channel, mark the channel as
770 * unavailable.
771 */
772 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
773 &ifibss->chandef);
774 if (err > 0)
775 cfg80211_radar_event(sdata->local->hw.wiphy, &ifibss->chandef,
776 GFP_ATOMIC);
777}
778
779static bool
780ieee80211_ibss_process_chanswitch(struct ieee80211_sub_if_data *sdata,
781 struct ieee802_11_elems *elems,
782 bool beacon)
783{
784 struct cfg80211_csa_settings params;
785 struct ieee80211_csa_ie csa_ie;
786 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
787 struct ieee80211_chanctx_conf *chanctx_conf;
788 struct ieee80211_chanctx *chanctx;
789 enum nl80211_channel_type ch_type;
790 int err, num_chanctx;
791 u32 sta_flags;
792
793 if (sdata->vif.csa_active)
794 return true;
795
796 if (!sdata->vif.bss_conf.ibss_joined)
797 return false;
798
799 sta_flags = IEEE80211_STA_DISABLE_VHT;
800 switch (ifibss->chandef.width) {
801 case NL80211_CHAN_WIDTH_5:
802 case NL80211_CHAN_WIDTH_10:
803 case NL80211_CHAN_WIDTH_20_NOHT:
804 sta_flags |= IEEE80211_STA_DISABLE_HT;
805 /* fall through */
806 case NL80211_CHAN_WIDTH_20:
807 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
808 break;
809 default:
810 break;
811 }
812
813 memset(&params, 0, sizeof(params));
814 memset(&csa_ie, 0, sizeof(csa_ie));
815 err = ieee80211_parse_ch_switch_ie(sdata, elems, beacon,
816 ifibss->chandef.chan->band,
817 sta_flags, ifibss->bssid, &csa_ie);
818 /* can't switch to destination channel, fail */
819 if (err < 0)
820 goto disconnect;
821
822 /* did not contain a CSA */
823 if (err)
824 return false;
825
826 /* channel switch is not supported, disconnect */
827 if (!(sdata->local->hw.wiphy->flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
828 goto disconnect;
829
830 params.count = csa_ie.count;
831 params.chandef = csa_ie.chandef;
832
833 if (ifibss->chandef.chan->band != params.chandef.chan->band)
834 goto disconnect;
835
836 switch (ifibss->chandef.width) {
837 case NL80211_CHAN_WIDTH_20_NOHT:
838 case NL80211_CHAN_WIDTH_20:
839 case NL80211_CHAN_WIDTH_40:
840 /* keep our current HT mode (HT20/HT40+/HT40-), even if
841 * another mode has been announced. The mode is not adopted
842 * within the beacon while doing CSA and we should therefore
843 * keep the mode which we announce.
844 */
845 ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
846 cfg80211_chandef_create(&params.chandef, params.chandef.chan,
847 ch_type);
848 break;
849 case NL80211_CHAN_WIDTH_5:
850 case NL80211_CHAN_WIDTH_10:
851 if (params.chandef.width != ifibss->chandef.width) {
852 sdata_info(sdata,
853 "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
854 ifibss->bssid,
855 params.chandef.chan->center_freq,
856 params.chandef.width,
857 params.chandef.center_freq1,
858 params.chandef.center_freq2);
859 goto disconnect;
860 }
861 break;
862 default:
863 /* should not happen, sta_flags should prevent VHT modes. */
864 WARN_ON(1);
865 goto disconnect;
866 }
867
868 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef)) {
869 sdata_info(sdata,
870 "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
871 ifibss->bssid,
872 params.chandef.chan->center_freq,
873 params.chandef.width,
874 params.chandef.center_freq1,
875 params.chandef.center_freq2);
876 goto disconnect;
877 }
878
879 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
880 &params.chandef);
881 if (err < 0)
882 goto disconnect;
883 if (err) {
884 /* IBSS-DFS only allowed with a control program */
885 if (!ifibss->userspace_handles_dfs)
886 goto disconnect;
887
888 params.radar_required = true;
889 }
890
891 rcu_read_lock();
892 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
893 if (!chanctx_conf) {
894 rcu_read_unlock();
895 goto disconnect;
896 }
897
898 /* don't handle for multi-VIF cases */
899 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
900 if (chanctx->refcount > 1) {
901 rcu_read_unlock();
902 goto disconnect;
903 }
904 num_chanctx = 0;
905 list_for_each_entry_rcu(chanctx, &sdata->local->chanctx_list, list)
906 num_chanctx++;
907
908 if (num_chanctx > 1) {
909 rcu_read_unlock();
910 goto disconnect;
911 }
912 rcu_read_unlock();
913
914 /* all checks done, now perform the channel switch. */
915 ibss_dbg(sdata,
916 "received channel switch announcement to go to channel %d MHz\n",
917 params.chandef.chan->center_freq);
918
919 params.block_tx = !!csa_ie.mode;
920
921 ieee80211_ibss_csa_beacon(sdata, &params);
922 sdata->csa_radar_required = params.radar_required;
923
924 if (params.block_tx)
925 ieee80211_stop_queues_by_reason(&sdata->local->hw,
926 IEEE80211_MAX_QUEUE_MAP,
927 IEEE80211_QUEUE_STOP_REASON_CSA);
928
929 sdata->local->csa_chandef = params.chandef;
930 sdata->vif.csa_active = true;
931
932 ieee80211_bss_info_change_notify(sdata, err);
933 drv_channel_switch_beacon(sdata, &params.chandef);
934
935 ieee80211_ibss_csa_mark_radar(sdata);
936
937 return true;
938disconnect:
939 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
940 ieee80211_queue_work(&sdata->local->hw,
941 &ifibss->csa_connection_drop_work);
942
943 ieee80211_ibss_csa_mark_radar(sdata);
944
945 return true;
946}
947
948static void
949ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
950 struct ieee80211_mgmt *mgmt, size_t len,
951 struct ieee80211_rx_status *rx_status,
952 struct ieee802_11_elems *elems)
953{
954 int required_len;
955
956 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
957 return;
958
959 /* CSA is the only action we handle for now */
960 if (mgmt->u.action.u.measurement.action_code !=
961 WLAN_ACTION_SPCT_CHL_SWITCH)
962 return;
963
964 required_len = IEEE80211_MIN_ACTION_SIZE +
965 sizeof(mgmt->u.action.u.chan_switch);
966 if (len < required_len)
967 return;
968
969 ieee80211_ibss_process_chanswitch(sdata, elems, false);
970}
971
502static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata, 972static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
503 struct ieee80211_mgmt *mgmt, 973 struct ieee80211_mgmt *mgmt,
504 size_t len) 974 size_t len)
@@ -661,10 +1131,6 @@ static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
661 1131
662 /* check if we need to merge IBSS */ 1132 /* check if we need to merge IBSS */
663 1133
664 /* we use a fixed BSSID */
665 if (sdata->u.ibss.fixed_bssid)
666 goto put_bss;
667
668 /* not an IBSS */ 1134 /* not an IBSS */
669 if (!(cbss->capability & WLAN_CAPABILITY_IBSS)) 1135 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
670 goto put_bss; 1136 goto put_bss;
@@ -680,10 +1146,18 @@ static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
680 sdata->u.ibss.ssid_len)) 1146 sdata->u.ibss.ssid_len))
681 goto put_bss; 1147 goto put_bss;
682 1148
1149 /* process channel switch */
1150 if (ieee80211_ibss_process_chanswitch(sdata, elems, true))
1151 goto put_bss;
1152
683 /* same BSSID */ 1153 /* same BSSID */
684 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid)) 1154 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
685 goto put_bss; 1155 goto put_bss;
686 1156
1157 /* we use a fixed BSSID */
1158 if (sdata->u.ibss.fixed_bssid)
1159 goto put_bss;
1160
687 if (ieee80211_have_rx_timestamp(rx_status)) { 1161 if (ieee80211_have_rx_timestamp(rx_status)) {
688 /* time when timestamp field was received */ 1162 /* time when timestamp field was received */
689 rx_timestamp = 1163 rx_timestamp =
@@ -775,30 +1249,6 @@ void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
775 ieee80211_queue_work(&local->hw, &sdata->work); 1249 ieee80211_queue_work(&local->hw, &sdata->work);
776} 1250}
777 1251
778static int ieee80211_sta_active_ibss(struct ieee80211_sub_if_data *sdata)
779{
780 struct ieee80211_local *local = sdata->local;
781 int active = 0;
782 struct sta_info *sta;
783
784 sdata_assert_lock(sdata);
785
786 rcu_read_lock();
787
788 list_for_each_entry_rcu(sta, &local->sta_list, list) {
789 if (sta->sdata == sdata &&
790 time_after(sta->last_rx + IEEE80211_IBSS_MERGE_INTERVAL,
791 jiffies)) {
792 active++;
793 break;
794 }
795 }
796
797 rcu_read_unlock();
798
799 return active;
800}
801
802static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata) 1252static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
803{ 1253{
804 struct ieee80211_local *local = sdata->local; 1254 struct ieee80211_local *local = sdata->local;
@@ -1076,6 +1526,8 @@ void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1076 struct ieee80211_rx_status *rx_status; 1526 struct ieee80211_rx_status *rx_status;
1077 struct ieee80211_mgmt *mgmt; 1527 struct ieee80211_mgmt *mgmt;
1078 u16 fc; 1528 u16 fc;
1529 struct ieee802_11_elems elems;
1530 int ies_len;
1079 1531
1080 rx_status = IEEE80211_SKB_RXCB(skb); 1532 rx_status = IEEE80211_SKB_RXCB(skb);
1081 mgmt = (struct ieee80211_mgmt *) skb->data; 1533 mgmt = (struct ieee80211_mgmt *) skb->data;
@@ -1101,6 +1553,27 @@ void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1101 case IEEE80211_STYPE_DEAUTH: 1553 case IEEE80211_STYPE_DEAUTH:
1102 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len); 1554 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1103 break; 1555 break;
1556 case IEEE80211_STYPE_ACTION:
1557 switch (mgmt->u.action.category) {
1558 case WLAN_CATEGORY_SPECTRUM_MGMT:
1559 ies_len = skb->len -
1560 offsetof(struct ieee80211_mgmt,
1561 u.action.u.chan_switch.variable);
1562
1563 if (ies_len < 0)
1564 break;
1565
1566 ieee802_11_parse_elems(
1567 mgmt->u.action.u.chan_switch.variable,
1568 ies_len, true, &elems);
1569
1570 if (elems.parse_error)
1571 break;
1572
1573 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
1574 rx_status, &elems);
1575 break;
1576 }
1104 } 1577 }
1105 1578
1106 mgmt_out: 1579 mgmt_out:
@@ -1167,6 +1640,8 @@ void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1167 (unsigned long) sdata); 1640 (unsigned long) sdata);
1168 INIT_LIST_HEAD(&ifibss->incomplete_stations); 1641 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1169 spin_lock_init(&ifibss->incomplete_lock); 1642 spin_lock_init(&ifibss->incomplete_lock);
1643 INIT_WORK(&ifibss->csa_connection_drop_work,
1644 ieee80211_csa_connection_drop_work);
1170} 1645}
1171 1646
1172/* scan finished notification */ 1647/* scan finished notification */
@@ -1202,6 +1677,7 @@ int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1202 1677
1203 sdata->u.ibss.privacy = params->privacy; 1678 sdata->u.ibss.privacy = params->privacy;
1204 sdata->u.ibss.control_port = params->control_port; 1679 sdata->u.ibss.control_port = params->control_port;
1680 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1205 sdata->u.ibss.basic_rates = params->basic_rates; 1681 sdata->u.ibss.basic_rates = params->basic_rates;
1206 1682
1207 /* fix basic_rates if channel does not support these rates */ 1683 /* fix basic_rates if channel does not support these rates */
@@ -1265,73 +1741,19 @@ int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1265int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata) 1741int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1266{ 1742{
1267 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 1743 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
1268 struct ieee80211_local *local = sdata->local;
1269 struct cfg80211_bss *cbss;
1270 u16 capability;
1271 int active_ibss;
1272 struct sta_info *sta;
1273 struct beacon_data *presp;
1274
1275 active_ibss = ieee80211_sta_active_ibss(sdata);
1276
1277 if (!active_ibss && !is_zero_ether_addr(ifibss->bssid)) {
1278 capability = WLAN_CAPABILITY_IBSS;
1279 1744
1280 if (ifibss->privacy) 1745 ieee80211_ibss_disconnect(sdata);
1281 capability |= WLAN_CAPABILITY_PRIVACY;
1282
1283 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
1284 ifibss->bssid, ifibss->ssid,
1285 ifibss->ssid_len, WLAN_CAPABILITY_IBSS |
1286 WLAN_CAPABILITY_PRIVACY,
1287 capability);
1288
1289 if (cbss) {
1290 cfg80211_unlink_bss(local->hw.wiphy, cbss);
1291 cfg80211_put_bss(local->hw.wiphy, cbss);
1292 }
1293 }
1294
1295 ifibss->state = IEEE80211_IBSS_MLME_SEARCH;
1296 memset(ifibss->bssid, 0, ETH_ALEN);
1297 ifibss->ssid_len = 0; 1746 ifibss->ssid_len = 0;
1298 1747 memset(ifibss->bssid, 0, ETH_ALEN);
1299 sta_info_flush(sdata);
1300
1301 spin_lock_bh(&ifibss->incomplete_lock);
1302 while (!list_empty(&ifibss->incomplete_stations)) {
1303 sta = list_first_entry(&ifibss->incomplete_stations,
1304 struct sta_info, list);
1305 list_del(&sta->list);
1306 spin_unlock_bh(&ifibss->incomplete_lock);
1307
1308 sta_info_free(local, sta);
1309 spin_lock_bh(&ifibss->incomplete_lock);
1310 }
1311 spin_unlock_bh(&ifibss->incomplete_lock);
1312
1313 netif_carrier_off(sdata->dev);
1314 1748
1315 /* remove beacon */ 1749 /* remove beacon */
1316 kfree(sdata->u.ibss.ie); 1750 kfree(sdata->u.ibss.ie);
1317 presp = rcu_dereference_protected(ifibss->presp,
1318 lockdep_is_held(&sdata->wdev.mtx));
1319 RCU_INIT_POINTER(sdata->u.ibss.presp, NULL);
1320 1751
1321 /* on the next join, re-program HT parameters */ 1752 /* on the next join, re-program HT parameters */
1322 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa)); 1753 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1323 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask)); 1754 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1324 1755
1325 sdata->vif.bss_conf.ibss_joined = false;
1326 sdata->vif.bss_conf.ibss_creator = false;
1327 sdata->vif.bss_conf.enable_beacon = false;
1328 sdata->vif.bss_conf.ssid_len = 0;
1329 clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
1330 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED |
1331 BSS_CHANGED_IBSS);
1332 ieee80211_vif_release_channel(sdata);
1333 synchronize_rcu(); 1756 synchronize_rcu();
1334 kfree(presp);
1335 1757
1336 skb_queue_purge(&sdata->skb_queue); 1758 skb_queue_purge(&sdata->skb_queue);
1337 1759
diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h
index 611abfcfb5eb..4aea4e791113 100644
--- a/net/mac80211/ieee80211_i.h
+++ b/net/mac80211/ieee80211_i.h
@@ -262,6 +262,10 @@ struct ieee80211_if_ap {
262 262
263 struct ps_data ps; 263 struct ps_data ps;
264 atomic_t num_mcast_sta; /* number of stations receiving multicast */ 264 atomic_t num_mcast_sta; /* number of stations receiving multicast */
265 enum ieee80211_smps_mode req_smps, /* requested smps mode */
266 driver_smps_mode; /* smps mode request */
267
268 struct work_struct request_smps_work;
265}; 269};
266 270
267struct ieee80211_if_wds { 271struct ieee80211_if_wds {
@@ -322,7 +326,6 @@ struct ieee80211_roc_work {
322 326
323/* flags used in struct ieee80211_if_managed.flags */ 327/* flags used in struct ieee80211_if_managed.flags */
324enum ieee80211_sta_flags { 328enum ieee80211_sta_flags {
325 IEEE80211_STA_BEACON_POLL = BIT(0),
326 IEEE80211_STA_CONNECTION_POLL = BIT(1), 329 IEEE80211_STA_CONNECTION_POLL = BIT(1),
327 IEEE80211_STA_CONTROL_PORT = BIT(2), 330 IEEE80211_STA_CONTROL_PORT = BIT(2),
328 IEEE80211_STA_DISABLE_HT = BIT(4), 331 IEEE80211_STA_DISABLE_HT = BIT(4),
@@ -335,6 +338,7 @@ enum ieee80211_sta_flags {
335 IEEE80211_STA_DISABLE_VHT = BIT(11), 338 IEEE80211_STA_DISABLE_VHT = BIT(11),
336 IEEE80211_STA_DISABLE_80P80MHZ = BIT(12), 339 IEEE80211_STA_DISABLE_80P80MHZ = BIT(12),
337 IEEE80211_STA_DISABLE_160MHZ = BIT(13), 340 IEEE80211_STA_DISABLE_160MHZ = BIT(13),
341 IEEE80211_STA_DISABLE_WMM = BIT(14),
338}; 342};
339 343
340struct ieee80211_mgd_auth_data { 344struct ieee80211_mgd_auth_data {
@@ -487,6 +491,7 @@ struct ieee80211_if_managed {
487 491
488struct ieee80211_if_ibss { 492struct ieee80211_if_ibss {
489 struct timer_list timer; 493 struct timer_list timer;
494 struct work_struct csa_connection_drop_work;
490 495
491 unsigned long last_scan_completed; 496 unsigned long last_scan_completed;
492 497
@@ -497,6 +502,7 @@ struct ieee80211_if_ibss {
497 bool privacy; 502 bool privacy;
498 503
499 bool control_port; 504 bool control_port;
505 bool userspace_handles_dfs;
500 506
501 u8 bssid[ETH_ALEN] __aligned(2); 507 u8 bssid[ETH_ALEN] __aligned(2);
502 u8 ssid[IEEE80211_MAX_SSID_LEN]; 508 u8 ssid[IEEE80211_MAX_SSID_LEN];
@@ -538,6 +544,11 @@ struct ieee80211_mesh_sync_ops {
538 /* add other framework functions here */ 544 /* add other framework functions here */
539}; 545};
540 546
547struct mesh_csa_settings {
548 struct rcu_head rcu_head;
549 struct cfg80211_csa_settings settings;
550};
551
541struct ieee80211_if_mesh { 552struct ieee80211_if_mesh {
542 struct timer_list housekeeping_timer; 553 struct timer_list housekeeping_timer;
543 struct timer_list mesh_path_timer; 554 struct timer_list mesh_path_timer;
@@ -598,6 +609,11 @@ struct ieee80211_if_mesh {
598 int ps_peers_light_sleep; 609 int ps_peers_light_sleep;
599 int ps_peers_deep_sleep; 610 int ps_peers_deep_sleep;
600 struct ps_data ps; 611 struct ps_data ps;
612 /* Channel Switching Support */
613 struct mesh_csa_settings __rcu *csa;
614 bool chsw_init;
615 u8 chsw_ttl;
616 u16 pre_value;
601}; 617};
602 618
603#ifdef CONFIG_MAC80211_MESH 619#ifdef CONFIG_MAC80211_MESH
@@ -1206,6 +1222,15 @@ struct ieee80211_ra_tid {
1206 u16 tid; 1222 u16 tid;
1207}; 1223};
1208 1224
1225/* this struct holds the value parsing from channel switch IE */
1226struct ieee80211_csa_ie {
1227 struct cfg80211_chan_def chandef;
1228 u8 mode;
1229 u8 count;
1230 u8 ttl;
1231 u16 pre_value;
1232};
1233
1209/* Parsed Information Elements */ 1234/* Parsed Information Elements */
1210struct ieee802_11_elems { 1235struct ieee802_11_elems {
1211 const u8 *ie_start; 1236 const u8 *ie_start;
@@ -1242,6 +1267,7 @@ struct ieee802_11_elems {
1242 const struct ieee80211_timeout_interval_ie *timeout_int; 1267 const struct ieee80211_timeout_interval_ie *timeout_int;
1243 const u8 *opmode_notif; 1268 const u8 *opmode_notif;
1244 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs; 1269 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1270 const struct ieee80211_mesh_chansw_params_ie *mesh_chansw_params_ie;
1245 1271
1246 /* length of them, respectively */ 1272 /* length of them, respectively */
1247 u8 ssid_len; 1273 u8 ssid_len;
@@ -1333,11 +1359,19 @@ int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata);
1333void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata); 1359void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata);
1334void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, 1360void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1335 struct sk_buff *skb); 1361 struct sk_buff *skb);
1362int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
1363 struct cfg80211_csa_settings *csa_settings);
1364int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata);
1365void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata);
1336 1366
1337/* mesh code */ 1367/* mesh code */
1338void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata); 1368void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata);
1339void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, 1369void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1340 struct sk_buff *skb); 1370 struct sk_buff *skb);
1371int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1372 struct cfg80211_csa_settings *csa_settings,
1373 bool csa_action);
1374int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata);
1341 1375
1342/* scan/BSS handling */ 1376/* scan/BSS handling */
1343void ieee80211_scan_work(struct work_struct *work); 1377void ieee80211_scan_work(struct work_struct *work);
@@ -1434,7 +1468,10 @@ void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
1434int ieee80211_send_smps_action(struct ieee80211_sub_if_data *sdata, 1468int ieee80211_send_smps_action(struct ieee80211_sub_if_data *sdata,
1435 enum ieee80211_smps_mode smps, const u8 *da, 1469 enum ieee80211_smps_mode smps, const u8 *da,
1436 const u8 *bssid); 1470 const u8 *bssid);
1437void ieee80211_request_smps_work(struct work_struct *work); 1471void ieee80211_request_smps_ap_work(struct work_struct *work);
1472void ieee80211_request_smps_mgd_work(struct work_struct *work);
1473bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
1474 enum ieee80211_smps_mode smps_mode_new);
1438 1475
1439void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid, 1476void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
1440 u16 initiator, u16 reason, bool stop); 1477 u16 initiator, u16 reason, bool stop);
@@ -1484,6 +1521,28 @@ void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
1484void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata, 1521void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1485 struct ieee80211_mgmt *mgmt, 1522 struct ieee80211_mgmt *mgmt,
1486 size_t len); 1523 size_t len);
1524/**
1525 * ieee80211_parse_ch_switch_ie - parses channel switch IEs
1526 * @sdata: the sdata of the interface which has received the frame
1527 * @elems: parsed 802.11 elements received with the frame
1528 * @beacon: indicates if the frame was a beacon or probe response
1529 * @current_band: indicates the current band
1530 * @sta_flags: contains information about own capabilities and restrictions
1531 * to decide which channel switch announcements can be accepted. Only the
1532 * following subset of &enum ieee80211_sta_flags are evaluated:
1533 * %IEEE80211_STA_DISABLE_HT, %IEEE80211_STA_DISABLE_VHT,
1534 * %IEEE80211_STA_DISABLE_40MHZ, %IEEE80211_STA_DISABLE_80P80MHZ,
1535 * %IEEE80211_STA_DISABLE_160MHZ.
1536 * @bssid: the currently connected bssid (for reporting)
1537 * @csa_ie: parsed 802.11 csa elements on count, mode, chandef and mesh ttl.
1538 All of them will be filled with if success only.
1539 * Return: 0 on success, <0 on error and >0 if there is nothing to parse.
1540 */
1541int ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data *sdata,
1542 struct ieee802_11_elems *elems, bool beacon,
1543 enum ieee80211_band current_band,
1544 u32 sta_flags, u8 *bssid,
1545 struct ieee80211_csa_ie *csa_ie);
1487 1546
1488/* Suspend/resume and hw reconfiguration */ 1547/* Suspend/resume and hw reconfiguration */
1489int ieee80211_reconfig(struct ieee80211_local *local); 1548int ieee80211_reconfig(struct ieee80211_local *local);
@@ -1629,8 +1688,10 @@ void ieee80211_send_probe_req(struct ieee80211_sub_if_data *sdata, u8 *dst,
1629u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata, 1688u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
1630 struct ieee802_11_elems *elems, 1689 struct ieee802_11_elems *elems,
1631 enum ieee80211_band band, u32 *basic_rates); 1690 enum ieee80211_band band, u32 *basic_rates);
1632int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata, 1691int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
1633 enum ieee80211_smps_mode smps_mode); 1692 enum ieee80211_smps_mode smps_mode);
1693int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
1694 enum ieee80211_smps_mode smps_mode);
1634void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata); 1695void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata);
1635 1696
1636size_t ieee80211_ie_split(const u8 *ies, size_t ielen, 1697size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
@@ -1657,6 +1718,7 @@ int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
1657void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan, 1718void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
1658 const struct ieee80211_ht_operation *ht_oper, 1719 const struct ieee80211_ht_operation *ht_oper,
1659 struct cfg80211_chan_def *chandef); 1720 struct cfg80211_chan_def *chandef);
1721u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c);
1660 1722
1661int __must_check 1723int __must_check
1662ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, 1724ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
@@ -1685,6 +1747,8 @@ void ieee80211_dfs_cac_timer(unsigned long data);
1685void ieee80211_dfs_cac_timer_work(struct work_struct *work); 1747void ieee80211_dfs_cac_timer_work(struct work_struct *work);
1686void ieee80211_dfs_cac_cancel(struct ieee80211_local *local); 1748void ieee80211_dfs_cac_cancel(struct ieee80211_local *local);
1687void ieee80211_dfs_radar_detected_work(struct work_struct *work); 1749void ieee80211_dfs_radar_detected_work(struct work_struct *work);
1750int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
1751 struct cfg80211_csa_settings *csa_settings);
1688 1752
1689#ifdef CONFIG_MAC80211_NOINLINE 1753#ifdef CONFIG_MAC80211_NOINLINE
1690#define debug_noinline noinline 1754#define debug_noinline noinline
diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c
index fcecd633514e..a0757913046e 100644
--- a/net/mac80211/iface.c
+++ b/net/mac80211/iface.c
@@ -766,6 +766,10 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
766 if (sdata->vif.type == NL80211_IFTYPE_STATION) 766 if (sdata->vif.type == NL80211_IFTYPE_STATION)
767 ieee80211_mgd_stop(sdata); 767 ieee80211_mgd_stop(sdata);
768 768
769 if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
770 ieee80211_ibss_stop(sdata);
771
772
769 /* 773 /*
770 * Remove all stations associated with this interface. 774 * Remove all stations associated with this interface.
771 * 775 *
@@ -1057,7 +1061,8 @@ static void ieee80211_uninit(struct net_device *dev)
1057} 1061}
1058 1062
1059static u16 ieee80211_netdev_select_queue(struct net_device *dev, 1063static u16 ieee80211_netdev_select_queue(struct net_device *dev,
1060 struct sk_buff *skb) 1064 struct sk_buff *skb,
1065 void *accel_priv)
1061{ 1066{
1062 return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb); 1067 return ieee80211_select_queue(IEEE80211_DEV_TO_SUB_IF(dev), skb);
1063} 1068}
@@ -1074,7 +1079,8 @@ static const struct net_device_ops ieee80211_dataif_ops = {
1074}; 1079};
1075 1080
1076static u16 ieee80211_monitor_select_queue(struct net_device *dev, 1081static u16 ieee80211_monitor_select_queue(struct net_device *dev,
1077 struct sk_buff *skb) 1082 struct sk_buff *skb,
1083 void *accel_priv)
1078{ 1084{
1079 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 1085 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
1080 struct ieee80211_local *local = sdata->local; 1086 struct ieee80211_local *local = sdata->local;
@@ -1289,7 +1295,10 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1289 case NL80211_IFTYPE_AP: 1295 case NL80211_IFTYPE_AP:
1290 skb_queue_head_init(&sdata->u.ap.ps.bc_buf); 1296 skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1291 INIT_LIST_HEAD(&sdata->u.ap.vlans); 1297 INIT_LIST_HEAD(&sdata->u.ap.vlans);
1298 INIT_WORK(&sdata->u.ap.request_smps_work,
1299 ieee80211_request_smps_ap_work);
1292 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1300 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1301 sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
1293 break; 1302 break;
1294 case NL80211_IFTYPE_P2P_CLIENT: 1303 case NL80211_IFTYPE_P2P_CLIENT:
1295 type = NL80211_IFTYPE_STATION; 1304 type = NL80211_IFTYPE_STATION;
@@ -1318,7 +1327,6 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1318 sdata->vif.bss_conf.bssid = NULL; 1327 sdata->vif.bss_conf.bssid = NULL;
1319 break; 1328 break;
1320 case NL80211_IFTYPE_AP_VLAN: 1329 case NL80211_IFTYPE_AP_VLAN:
1321 break;
1322 case NL80211_IFTYPE_P2P_DEVICE: 1330 case NL80211_IFTYPE_P2P_DEVICE:
1323 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1331 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1324 break; 1332 break;
diff --git a/net/mac80211/key.c b/net/mac80211/key.c
index 620677e897bd..3e51dd7d98b3 100644
--- a/net/mac80211/key.c
+++ b/net/mac80211/key.c
@@ -879,7 +879,7 @@ ieee80211_gtk_rekey_add(struct ieee80211_vif *vif,
879 keyconf->keylen, keyconf->key, 879 keyconf->keylen, keyconf->key,
880 0, NULL); 880 0, NULL);
881 if (IS_ERR(key)) 881 if (IS_ERR(key))
882 return ERR_PTR(PTR_ERR(key)); 882 return ERR_CAST(key);
883 883
884 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED) 884 if (sdata->u.mgd.mfp != IEEE80211_MFP_DISABLED)
885 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT; 885 key->conf.flags |= IEEE80211_KEY_FLAG_RX_MGMT;
diff --git a/net/mac80211/key.h b/net/mac80211/key.h
index 036d57e76a5e..aaae0ed37004 100644
--- a/net/mac80211/key.h
+++ b/net/mac80211/key.h
@@ -83,7 +83,7 @@ struct ieee80211_key {
83 * Management frames. 83 * Management frames.
84 */ 84 */
85 u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN]; 85 u8 rx_pn[IEEE80211_NUM_TIDS + 1][IEEE80211_CCMP_PN_LEN];
86 struct crypto_cipher *tfm; 86 struct crypto_aead *tfm;
87 u32 replays; /* dot11RSNAStatsCCMPReplays */ 87 u32 replays; /* dot11RSNAStatsCCMPReplays */
88 } ccmp; 88 } ccmp;
89 struct { 89 struct {
diff --git a/net/mac80211/main.c b/net/mac80211/main.c
index 21d5d44444d0..7d1c3ac48ed9 100644
--- a/net/mac80211/main.c
+++ b/net/mac80211/main.c
@@ -940,6 +940,8 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
940 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n", 940 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
941 result); 941 result);
942 942
943 local->hw.conf.flags = IEEE80211_CONF_IDLE;
944
943 ieee80211_led_init(local); 945 ieee80211_led_init(local);
944 946
945 rtnl_lock(); 947 rtnl_lock();
@@ -1047,6 +1049,7 @@ void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1047 1049
1048 cancel_work_sync(&local->restart_work); 1050 cancel_work_sync(&local->restart_work);
1049 cancel_work_sync(&local->reconfig_filter); 1051 cancel_work_sync(&local->reconfig_filter);
1052 flush_work(&local->sched_scan_stopped_work);
1050 1053
1051 ieee80211_clear_tx_pending(local); 1054 ieee80211_clear_tx_pending(local);
1052 rate_control_deinitialize(local); 1055 rate_control_deinitialize(local);
diff --git a/net/mac80211/mesh.c b/net/mac80211/mesh.c
index 707ac61d63e5..ba105257d03f 100644
--- a/net/mac80211/mesh.c
+++ b/net/mac80211/mesh.c
@@ -12,6 +12,7 @@
12#include <asm/unaligned.h> 12#include <asm/unaligned.h>
13#include "ieee80211_i.h" 13#include "ieee80211_i.h"
14#include "mesh.h" 14#include "mesh.h"
15#include "driver-ops.h"
15 16
16static int mesh_allocated; 17static int mesh_allocated;
17static struct kmem_cache *rm_cache; 18static struct kmem_cache *rm_cache;
@@ -610,6 +611,7 @@ ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
610 struct sk_buff *skb; 611 struct sk_buff *skb;
611 struct ieee80211_mgmt *mgmt; 612 struct ieee80211_mgmt *mgmt;
612 struct ieee80211_chanctx_conf *chanctx_conf; 613 struct ieee80211_chanctx_conf *chanctx_conf;
614 struct mesh_csa_settings *csa;
613 enum ieee80211_band band; 615 enum ieee80211_band band;
614 u8 *pos; 616 u8 *pos;
615 struct ieee80211_sub_if_data *sdata; 617 struct ieee80211_sub_if_data *sdata;
@@ -624,6 +626,10 @@ ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
624 626
625 head_len = hdr_len + 627 head_len = hdr_len +
626 2 + /* NULL SSID */ 628 2 + /* NULL SSID */
629 /* Channel Switch Announcement */
630 2 + sizeof(struct ieee80211_channel_sw_ie) +
631 /* Mesh Channel Swith Parameters */
632 2 + sizeof(struct ieee80211_mesh_chansw_params_ie) +
627 2 + 8 + /* supported rates */ 633 2 + 8 + /* supported rates */
628 2 + 3; /* DS params */ 634 2 + 3; /* DS params */
629 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) + 635 tail_len = 2 + (IEEE80211_MAX_SUPP_RATES - 8) +
@@ -665,6 +671,38 @@ ieee80211_mesh_build_beacon(struct ieee80211_if_mesh *ifmsh)
665 *pos++ = WLAN_EID_SSID; 671 *pos++ = WLAN_EID_SSID;
666 *pos++ = 0x0; 672 *pos++ = 0x0;
667 673
674 rcu_read_lock();
675 csa = rcu_dereference(ifmsh->csa);
676 if (csa) {
677 __le16 pre_value;
678
679 pos = skb_put(skb, 13);
680 memset(pos, 0, 13);
681 *pos++ = WLAN_EID_CHANNEL_SWITCH;
682 *pos++ = 3;
683 *pos++ = 0x0;
684 *pos++ = ieee80211_frequency_to_channel(
685 csa->settings.chandef.chan->center_freq);
686 sdata->csa_counter_offset_beacon = hdr_len + 6;
687 *pos++ = csa->settings.count;
688 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM;
689 *pos++ = 6;
690 if (ifmsh->chsw_init) {
691 *pos++ = ifmsh->mshcfg.dot11MeshTTL;
692 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
693 } else {
694 *pos++ = ifmsh->chsw_ttl;
695 }
696 *pos++ |= csa->settings.block_tx ?
697 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
698 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos);
699 pos += 2;
700 pre_value = cpu_to_le16(ifmsh->pre_value);
701 memcpy(pos, &pre_value, 2);
702 pos += 2;
703 }
704 rcu_read_unlock();
705
668 if (ieee80211_add_srates_ie(sdata, skb, true, band) || 706 if (ieee80211_add_srates_ie(sdata, skb, true, band) ||
669 mesh_add_ds_params_ie(sdata, skb)) 707 mesh_add_ds_params_ie(sdata, skb))
670 goto out_free; 708 goto out_free;
@@ -812,6 +850,132 @@ void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
812 ieee80211_configure_filter(local); 850 ieee80211_configure_filter(local);
813} 851}
814 852
853static bool
854ieee80211_mesh_process_chnswitch(struct ieee80211_sub_if_data *sdata,
855 struct ieee802_11_elems *elems, bool beacon)
856{
857 struct cfg80211_csa_settings params;
858 struct ieee80211_csa_ie csa_ie;
859 struct ieee80211_chanctx_conf *chanctx_conf;
860 struct ieee80211_chanctx *chanctx;
861 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
862 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
863 int err, num_chanctx;
864 u32 sta_flags;
865
866 if (sdata->vif.csa_active)
867 return true;
868
869 if (!ifmsh->mesh_id)
870 return false;
871
872 sta_flags = IEEE80211_STA_DISABLE_VHT;
873 switch (sdata->vif.bss_conf.chandef.width) {
874 case NL80211_CHAN_WIDTH_20_NOHT:
875 sta_flags |= IEEE80211_STA_DISABLE_HT;
876 case NL80211_CHAN_WIDTH_20:
877 sta_flags |= IEEE80211_STA_DISABLE_40MHZ;
878 break;
879 default:
880 break;
881 }
882
883 memset(&params, 0, sizeof(params));
884 memset(&csa_ie, 0, sizeof(csa_ie));
885 err = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, band,
886 sta_flags, sdata->vif.addr,
887 &csa_ie);
888 if (err < 0)
889 return false;
890 if (err)
891 return false;
892
893 params.chandef = csa_ie.chandef;
894 params.count = csa_ie.count;
895
896 if (sdata->vif.bss_conf.chandef.chan->band !=
897 params.chandef.chan->band)
898 return false;
899
900 if (!cfg80211_chandef_usable(sdata->local->hw.wiphy, &params.chandef,
901 IEEE80211_CHAN_DISABLED)) {
902 sdata_info(sdata,
903 "mesh STA %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), aborting\n",
904 sdata->vif.addr,
905 params.chandef.chan->center_freq,
906 params.chandef.width,
907 params.chandef.center_freq1,
908 params.chandef.center_freq2);
909 return false;
910 }
911
912 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
913 &params.chandef);
914 if (err < 0)
915 return false;
916 if (err) {
917 params.radar_required = true;
918 /* TODO: DFS not (yet) supported */
919 return false;
920 }
921
922 rcu_read_lock();
923 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
924 if (!chanctx_conf)
925 goto failed_chswitch;
926
927 /* don't handle for multi-VIF cases */
928 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
929 if (chanctx->refcount > 1)
930 goto failed_chswitch;
931
932 num_chanctx = 0;
933 list_for_each_entry_rcu(chanctx, &sdata->local->chanctx_list, list)
934 num_chanctx++;
935
936 if (num_chanctx > 1)
937 goto failed_chswitch;
938
939 rcu_read_unlock();
940
941 mcsa_dbg(sdata,
942 "received channel switch announcement to go to channel %d MHz\n",
943 params.chandef.chan->center_freq);
944
945 params.block_tx = csa_ie.mode & WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT;
946 if (beacon) {
947 ifmsh->chsw_ttl = csa_ie.ttl - 1;
948 if (ifmsh->pre_value >= csa_ie.pre_value)
949 return false;
950 ifmsh->pre_value = csa_ie.pre_value;
951 }
952
953 if (ifmsh->chsw_ttl < ifmsh->mshcfg.dot11MeshTTL) {
954 if (ieee80211_mesh_csa_beacon(sdata, &params, false) < 0)
955 return false;
956 } else {
957 return false;
958 }
959
960 sdata->csa_radar_required = params.radar_required;
961
962 if (params.block_tx)
963 ieee80211_stop_queues_by_reason(&sdata->local->hw,
964 IEEE80211_MAX_QUEUE_MAP,
965 IEEE80211_QUEUE_STOP_REASON_CSA);
966
967 sdata->local->csa_chandef = params.chandef;
968 sdata->vif.csa_active = true;
969
970 ieee80211_bss_info_change_notify(sdata, err);
971 drv_channel_switch_beacon(sdata, &params.chandef);
972
973 return true;
974failed_chswitch:
975 rcu_read_unlock();
976 return false;
977}
978
815static void 979static void
816ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata, 980ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
817 struct ieee80211_mgmt *mgmt, size_t len) 981 struct ieee80211_mgmt *mgmt, size_t len)
@@ -918,6 +1082,141 @@ static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
918 if (ifmsh->sync_ops) 1082 if (ifmsh->sync_ops)
919 ifmsh->sync_ops->rx_bcn_presp(sdata, 1083 ifmsh->sync_ops->rx_bcn_presp(sdata,
920 stype, mgmt, &elems, rx_status); 1084 stype, mgmt, &elems, rx_status);
1085
1086 if (!ifmsh->chsw_init)
1087 ieee80211_mesh_process_chnswitch(sdata, &elems, true);
1088}
1089
1090int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1091{
1092 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1093 struct mesh_csa_settings *tmp_csa_settings;
1094 int ret = 0;
1095
1096 /* Reset the TTL value and Initiator flag */
1097 ifmsh->chsw_init = false;
1098 ifmsh->chsw_ttl = 0;
1099
1100 /* Remove the CSA and MCSP elements from the beacon */
1101 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1102 rcu_assign_pointer(ifmsh->csa, NULL);
1103 kfree_rcu(tmp_csa_settings, rcu_head);
1104 ret = ieee80211_mesh_rebuild_beacon(sdata);
1105 if (ret)
1106 return -EINVAL;
1107
1108 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1109
1110 mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1111 sdata->vif.bss_conf.chandef.chan->center_freq);
1112 return 0;
1113}
1114
1115int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1116 struct cfg80211_csa_settings *csa_settings,
1117 bool csa_action)
1118{
1119 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1120 struct mesh_csa_settings *tmp_csa_settings;
1121 int ret = 0;
1122
1123 tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1124 GFP_ATOMIC);
1125 if (!tmp_csa_settings)
1126 return -ENOMEM;
1127
1128 memcpy(&tmp_csa_settings->settings, csa_settings,
1129 sizeof(struct cfg80211_csa_settings));
1130
1131 rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1132
1133 ret = ieee80211_mesh_rebuild_beacon(sdata);
1134 if (ret) {
1135 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1136 rcu_assign_pointer(ifmsh->csa, NULL);
1137 kfree_rcu(tmp_csa_settings, rcu_head);
1138 return ret;
1139 }
1140
1141 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1142
1143 if (csa_action)
1144 ieee80211_send_action_csa(sdata, csa_settings);
1145
1146 return 0;
1147}
1148
1149static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1150 struct ieee80211_mgmt *mgmt, size_t len)
1151{
1152 struct ieee80211_mgmt *mgmt_fwd;
1153 struct sk_buff *skb;
1154 struct ieee80211_local *local = sdata->local;
1155 u8 *pos = mgmt->u.action.u.chan_switch.variable;
1156 size_t offset_ttl;
1157
1158 skb = dev_alloc_skb(local->tx_headroom + len);
1159 if (!skb)
1160 return -ENOMEM;
1161 skb_reserve(skb, local->tx_headroom);
1162 mgmt_fwd = (struct ieee80211_mgmt *) skb_put(skb, len);
1163
1164 /* offset_ttl is based on whether the secondary channel
1165 * offset is available or not. Substract 1 from the mesh TTL
1166 * and disable the initiator flag before forwarding.
1167 */
1168 offset_ttl = (len < 42) ? 7 : 10;
1169 *(pos + offset_ttl) -= 1;
1170 *(pos + offset_ttl + 1) &= ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1171
1172 memcpy(mgmt_fwd, mgmt, len);
1173 eth_broadcast_addr(mgmt_fwd->da);
1174 memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1175 memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1176
1177 ieee80211_tx_skb(sdata, skb);
1178 return 0;
1179}
1180
1181static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1182 struct ieee80211_mgmt *mgmt, size_t len)
1183{
1184 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1185 struct ieee802_11_elems elems;
1186 u16 pre_value;
1187 bool fwd_csa = true;
1188 size_t baselen;
1189 u8 *pos;
1190
1191 if (mgmt->u.action.u.measurement.action_code !=
1192 WLAN_ACTION_SPCT_CHL_SWITCH)
1193 return;
1194
1195 pos = mgmt->u.action.u.chan_switch.variable;
1196 baselen = offsetof(struct ieee80211_mgmt,
1197 u.action.u.chan_switch.variable);
1198 ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1199
1200 ifmsh->chsw_ttl = elems.mesh_chansw_params_ie->mesh_ttl;
1201 if (!--ifmsh->chsw_ttl)
1202 fwd_csa = false;
1203
1204 pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
1205 if (ifmsh->pre_value >= pre_value)
1206 return;
1207
1208 ifmsh->pre_value = pre_value;
1209
1210 if (!ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
1211 mcsa_dbg(sdata, "Failed to process CSA action frame");
1212 return;
1213 }
1214
1215 /* forward or re-broadcast the CSA frame */
1216 if (fwd_csa) {
1217 if (mesh_fwd_csa_frame(sdata, mgmt, len) < 0)
1218 mcsa_dbg(sdata, "Failed to forward the CSA frame");
1219 }
921} 1220}
922 1221
923static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata, 1222static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
@@ -939,6 +1238,9 @@ static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
939 if (mesh_action_is_path_sel(mgmt)) 1238 if (mesh_action_is_path_sel(mgmt))
940 mesh_rx_path_sel_frame(sdata, mgmt, len); 1239 mesh_rx_path_sel_frame(sdata, mgmt, len);
941 break; 1240 break;
1241 case WLAN_CATEGORY_SPECTRUM_MGMT:
1242 mesh_rx_csa_frame(sdata, mgmt, len);
1243 break;
942 } 1244 }
943} 1245}
944 1246
@@ -1056,13 +1358,11 @@ void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1056 (unsigned long) sdata); 1358 (unsigned long) sdata);
1057 1359
1058 ifmsh->accepting_plinks = true; 1360 ifmsh->accepting_plinks = true;
1059 ifmsh->preq_id = 0;
1060 ifmsh->sn = 0;
1061 ifmsh->num_gates = 0;
1062 atomic_set(&ifmsh->mpaths, 0); 1361 atomic_set(&ifmsh->mpaths, 0);
1063 mesh_rmc_init(sdata); 1362 mesh_rmc_init(sdata);
1064 ifmsh->last_preq = jiffies; 1363 ifmsh->last_preq = jiffies;
1065 ifmsh->next_perr = jiffies; 1364 ifmsh->next_perr = jiffies;
1365 ifmsh->chsw_init = false;
1066 /* Allocate all mesh structures when creating the first mesh interface. */ 1366 /* Allocate all mesh structures when creating the first mesh interface. */
1067 if (!mesh_allocated) 1367 if (!mesh_allocated)
1068 ieee80211s_init(); 1368 ieee80211s_init();
diff --git a/net/mac80211/mesh_plink.c b/net/mac80211/mesh_plink.c
index 6b65d5055f5b..4301aa5aa227 100644
--- a/net/mac80211/mesh_plink.c
+++ b/net/mac80211/mesh_plink.c
@@ -222,7 +222,8 @@ static u32 __mesh_plink_deactivate(struct sta_info *sta)
222 mesh_path_flush_by_nexthop(sta); 222 mesh_path_flush_by_nexthop(sta);
223 223
224 ieee80211_mps_sta_status_update(sta); 224 ieee80211_mps_sta_status_update(sta);
225 changed |= ieee80211_mps_local_status_update(sdata); 225 changed |= ieee80211_mps_set_sta_local_pm(sta,
226 NL80211_MESH_POWER_UNKNOWN);
226 227
227 return changed; 228 return changed;
228} 229}
diff --git a/net/mac80211/mesh_ps.c b/net/mac80211/mesh_ps.c
index 22290a929b94..0f79b78b5e86 100644
--- a/net/mac80211/mesh_ps.c
+++ b/net/mac80211/mesh_ps.c
@@ -152,6 +152,9 @@ u32 ieee80211_mps_set_sta_local_pm(struct sta_info *sta,
152{ 152{
153 struct ieee80211_sub_if_data *sdata = sta->sdata; 153 struct ieee80211_sub_if_data *sdata = sta->sdata;
154 154
155 if (sta->local_pm == pm)
156 return 0;
157
155 mps_dbg(sdata, "local STA operates in mode %d with %pM\n", 158 mps_dbg(sdata, "local STA operates in mode %d with %pM\n",
156 pm, sta->sta.addr); 159 pm, sta->sta.addr);
157 160
@@ -245,6 +248,14 @@ void ieee80211_mps_sta_status_update(struct sta_info *sta)
245 248
246 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE); 249 do_buffer = (pm != NL80211_MESH_POWER_ACTIVE);
247 250
251 /* clear the MPSP flags for non-peers or active STA */
252 if (sta->plink_state != NL80211_PLINK_ESTAB) {
253 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
254 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
255 } else if (!do_buffer) {
256 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
257 }
258
248 /* Don't let the same PS state be set twice */ 259 /* Don't let the same PS state be set twice */
249 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer) 260 if (test_sta_flag(sta, WLAN_STA_PS_STA) == do_buffer)
250 return; 261 return;
@@ -257,14 +268,6 @@ void ieee80211_mps_sta_status_update(struct sta_info *sta)
257 } else { 268 } else {
258 ieee80211_sta_ps_deliver_wakeup(sta); 269 ieee80211_sta_ps_deliver_wakeup(sta);
259 } 270 }
260
261 /* clear the MPSP flags for non-peers or active STA */
262 if (sta->plink_state != NL80211_PLINK_ESTAB) {
263 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
264 clear_sta_flag(sta, WLAN_STA_MPSP_RECIPIENT);
265 } else if (!do_buffer) {
266 clear_sta_flag(sta, WLAN_STA_MPSP_OWNER);
267 }
268} 271}
269 272
270static void mps_set_sta_peer_pm(struct sta_info *sta, 273static void mps_set_sta_peer_pm(struct sta_info *sta,
@@ -444,8 +447,7 @@ static void mpsp_qos_null_append(struct sta_info *sta,
444 */ 447 */
445static void mps_frame_deliver(struct sta_info *sta, int n_frames) 448static void mps_frame_deliver(struct sta_info *sta, int n_frames)
446{ 449{
447 struct ieee80211_sub_if_data *sdata = sta->sdata; 450 struct ieee80211_local *local = sta->sdata->local;
448 struct ieee80211_local *local = sdata->local;
449 int ac; 451 int ac;
450 struct sk_buff_head frames; 452 struct sk_buff_head frames;
451 struct sk_buff *skb; 453 struct sk_buff *skb;
@@ -558,10 +560,10 @@ void ieee80211_mpsp_trigger_process(u8 *qc, struct sta_info *sta,
558} 560}
559 561
560/** 562/**
561 * ieee80211_mps_frame_release - release buffered frames in response to beacon 563 * ieee80211_mps_frame_release - release frames buffered due to mesh power save
562 * 564 *
563 * @sta: mesh STA 565 * @sta: mesh STA
564 * @elems: beacon IEs 566 * @elems: IEs of beacon or probe response
565 * 567 *
566 * For peers if we have individually-addressed frames buffered or the peer 568 * For peers if we have individually-addressed frames buffered or the peer
567 * indicates buffered frames, send a corresponding MPSP trigger frame. Since 569 * indicates buffered frames, send a corresponding MPSP trigger frame. Since
@@ -588,9 +590,10 @@ void ieee80211_mps_frame_release(struct sta_info *sta,
588 (!elems->awake_window || !le16_to_cpu(*elems->awake_window))) 590 (!elems->awake_window || !le16_to_cpu(*elems->awake_window)))
589 return; 591 return;
590 592
591 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) 593 if (!test_sta_flag(sta, WLAN_STA_MPSP_OWNER))
592 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) + 594 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
593 skb_queue_len(&sta->tx_filtered[ac]); 595 buffer_local += skb_queue_len(&sta->ps_tx_buf[ac]) +
596 skb_queue_len(&sta->tx_filtered[ac]);
594 597
595 if (!has_buffered && !buffer_local) 598 if (!has_buffered && !buffer_local)
596 return; 599 return;
diff --git a/net/mac80211/mlme.c b/net/mac80211/mlme.c
index 86e4ad56b573..b3a3ce316656 100644
--- a/net/mac80211/mlme.c
+++ b/net/mac80211/mlme.c
@@ -145,66 +145,6 @@ static int ecw2cw(int ecw)
145 return (1 << ecw) - 1; 145 return (1 << ecw) - 1;
146} 146}
147 147
148static u32 chandef_downgrade(struct cfg80211_chan_def *c)
149{
150 u32 ret;
151 int tmp;
152
153 switch (c->width) {
154 case NL80211_CHAN_WIDTH_20:
155 c->width = NL80211_CHAN_WIDTH_20_NOHT;
156 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
157 break;
158 case NL80211_CHAN_WIDTH_40:
159 c->width = NL80211_CHAN_WIDTH_20;
160 c->center_freq1 = c->chan->center_freq;
161 ret = IEEE80211_STA_DISABLE_40MHZ |
162 IEEE80211_STA_DISABLE_VHT;
163 break;
164 case NL80211_CHAN_WIDTH_80:
165 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
166 /* n_P40 */
167 tmp /= 2;
168 /* freq_P40 */
169 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
170 c->width = NL80211_CHAN_WIDTH_40;
171 ret = IEEE80211_STA_DISABLE_VHT;
172 break;
173 case NL80211_CHAN_WIDTH_80P80:
174 c->center_freq2 = 0;
175 c->width = NL80211_CHAN_WIDTH_80;
176 ret = IEEE80211_STA_DISABLE_80P80MHZ |
177 IEEE80211_STA_DISABLE_160MHZ;
178 break;
179 case NL80211_CHAN_WIDTH_160:
180 /* n_P20 */
181 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
182 /* n_P80 */
183 tmp /= 4;
184 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
185 c->width = NL80211_CHAN_WIDTH_80;
186 ret = IEEE80211_STA_DISABLE_80P80MHZ |
187 IEEE80211_STA_DISABLE_160MHZ;
188 break;
189 default:
190 case NL80211_CHAN_WIDTH_20_NOHT:
191 WARN_ON_ONCE(1);
192 c->width = NL80211_CHAN_WIDTH_20_NOHT;
193 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
194 break;
195 case NL80211_CHAN_WIDTH_5:
196 case NL80211_CHAN_WIDTH_10:
197 WARN_ON_ONCE(1);
198 /* keep c->width */
199 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
200 break;
201 }
202
203 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
204
205 return ret;
206}
207
208static u32 148static u32
209ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata, 149ieee80211_determine_chantype(struct ieee80211_sub_if_data *sdata,
210 struct ieee80211_supported_band *sband, 150 struct ieee80211_supported_band *sband,
@@ -352,7 +292,7 @@ out:
352 break; 292 break;
353 } 293 }
354 294
355 ret |= chandef_downgrade(chandef); 295 ret |= ieee80211_chandef_downgrade(chandef);
356 } 296 }
357 297
358 if (chandef->width != vht_chandef.width && !tracking) 298 if (chandef->width != vht_chandef.width && !tracking)
@@ -406,13 +346,13 @@ static int ieee80211_config_bw(struct ieee80211_sub_if_data *sdata,
406 */ 346 */
407 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ && 347 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
408 chandef.width == NL80211_CHAN_WIDTH_80P80) 348 chandef.width == NL80211_CHAN_WIDTH_80P80)
409 flags |= chandef_downgrade(&chandef); 349 flags |= ieee80211_chandef_downgrade(&chandef);
410 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ && 350 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
411 chandef.width == NL80211_CHAN_WIDTH_160) 351 chandef.width == NL80211_CHAN_WIDTH_160)
412 flags |= chandef_downgrade(&chandef); 352 flags |= ieee80211_chandef_downgrade(&chandef);
413 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ && 353 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
414 chandef.width > NL80211_CHAN_WIDTH_20) 354 chandef.width > NL80211_CHAN_WIDTH_20)
415 flags |= chandef_downgrade(&chandef); 355 flags |= ieee80211_chandef_downgrade(&chandef);
416 356
417 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef)) 357 if (cfg80211_chandef_identical(&chandef, &sdata->vif.bss_conf.chandef))
418 return 0; 358 return 0;
@@ -893,8 +833,7 @@ void ieee80211_send_nullfunc(struct ieee80211_local *local,
893 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS) 833 if (local->hw.flags & IEEE80211_HW_REPORTS_TX_ACK_STATUS)
894 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS; 834 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
895 835
896 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL | 836 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
897 IEEE80211_STA_CONNECTION_POLL))
898 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE; 837 IEEE80211_SKB_CB(skb)->flags |= IEEE80211_TX_CTL_USE_MINRATE;
899 838
900 ieee80211_tx_skb(sdata, skb); 839 ieee80211_tx_skb(sdata, skb);
@@ -937,6 +876,8 @@ static void ieee80211_chswitch_work(struct work_struct *work)
937 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work); 876 container_of(work, struct ieee80211_sub_if_data, u.mgd.chswitch_work);
938 struct ieee80211_local *local = sdata->local; 877 struct ieee80211_local *local = sdata->local;
939 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 878 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
879 u32 changed = 0;
880 int ret;
940 881
941 if (!ieee80211_sdata_running(sdata)) 882 if (!ieee80211_sdata_running(sdata))
942 return; 883 return;
@@ -945,24 +886,39 @@ static void ieee80211_chswitch_work(struct work_struct *work)
945 if (!ifmgd->associated) 886 if (!ifmgd->associated)
946 goto out; 887 goto out;
947 888
948 local->_oper_chandef = local->csa_chandef; 889 ret = ieee80211_vif_change_channel(sdata, &local->csa_chandef,
890 &changed);
891 if (ret) {
892 sdata_info(sdata,
893 "vif channel switch failed, disconnecting\n");
894 ieee80211_queue_work(&sdata->local->hw,
895 &ifmgd->csa_connection_drop_work);
896 goto out;
897 }
949 898
950 if (!local->ops->channel_switch) { 899 if (!local->use_chanctx) {
951 /* call "hw_config" only if doing sw channel switch */ 900 local->_oper_chandef = local->csa_chandef;
952 ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL); 901 /* Call "hw_config" only if doing sw channel switch.
953 } else { 902 * Otherwise update the channel directly
954 /* update the device channel directly */ 903 */
955 local->hw.conf.chandef = local->_oper_chandef; 904 if (!local->ops->channel_switch)
905 ieee80211_hw_config(local, 0);
906 else
907 local->hw.conf.chandef = local->_oper_chandef;
956 } 908 }
957 909
958 /* XXX: shouldn't really modify cfg80211-owned data! */ 910 /* XXX: shouldn't really modify cfg80211-owned data! */
959 ifmgd->associated->channel = local->_oper_chandef.chan; 911 ifmgd->associated->channel = local->csa_chandef.chan;
960 912
961 /* XXX: wait for a beacon first? */ 913 /* XXX: wait for a beacon first? */
962 ieee80211_wake_queues_by_reason(&local->hw, 914 ieee80211_wake_queues_by_reason(&local->hw,
963 IEEE80211_MAX_QUEUE_MAP, 915 IEEE80211_MAX_QUEUE_MAP,
964 IEEE80211_QUEUE_STOP_REASON_CSA); 916 IEEE80211_QUEUE_STOP_REASON_CSA);
917
918 ieee80211_bss_info_change_notify(sdata, changed);
919
965 out: 920 out:
921 sdata->vif.csa_active = false;
966 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED; 922 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
967 sdata_unlock(sdata); 923 sdata_unlock(sdata);
968} 924}
@@ -1000,20 +956,10 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1000 struct ieee80211_local *local = sdata->local; 956 struct ieee80211_local *local = sdata->local;
1001 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 957 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
1002 struct cfg80211_bss *cbss = ifmgd->associated; 958 struct cfg80211_bss *cbss = ifmgd->associated;
1003 struct ieee80211_bss *bss;
1004 struct ieee80211_chanctx *chanctx; 959 struct ieee80211_chanctx *chanctx;
1005 enum ieee80211_band new_band; 960 enum ieee80211_band current_band;
1006 int new_freq; 961 struct ieee80211_csa_ie csa_ie;
1007 u8 new_chan_no; 962 int res;
1008 u8 count;
1009 u8 mode;
1010 struct ieee80211_channel *new_chan;
1011 struct cfg80211_chan_def new_chandef = {};
1012 struct cfg80211_chan_def new_vht_chandef = {};
1013 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1014 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
1015 const struct ieee80211_ht_operation *ht_oper;
1016 int secondary_channel_offset = -1;
1017 963
1018 sdata_assert_lock(sdata); 964 sdata_assert_lock(sdata);
1019 965
@@ -1027,181 +973,53 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1027 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED) 973 if (ifmgd->flags & IEEE80211_STA_CSA_RECEIVED)
1028 return; 974 return;
1029 975
1030 sec_chan_offs = elems->sec_chan_offs; 976 current_band = cbss->channel->band;
1031 wide_bw_chansw_ie = elems->wide_bw_chansw_ie; 977 memset(&csa_ie, 0, sizeof(csa_ie));
1032 ht_oper = elems->ht_operation; 978 res = ieee80211_parse_ch_switch_ie(sdata, elems, beacon, current_band,
1033 979 ifmgd->flags,
1034 if (ifmgd->flags & (IEEE80211_STA_DISABLE_HT | 980 ifmgd->associated->bssid, &csa_ie);
1035 IEEE80211_STA_DISABLE_40MHZ)) { 981 if (res < 0)
1036 sec_chan_offs = NULL; 982 ieee80211_queue_work(&local->hw,
1037 wide_bw_chansw_ie = NULL; 983 &ifmgd->csa_connection_drop_work);
1038 /* only used for bandwidth here */ 984 if (res)
1039 ht_oper = NULL;
1040 }
1041
1042 if (ifmgd->flags & IEEE80211_STA_DISABLE_VHT)
1043 wide_bw_chansw_ie = NULL;
1044
1045 if (elems->ext_chansw_ie) {
1046 if (!ieee80211_operating_class_to_band(
1047 elems->ext_chansw_ie->new_operating_class,
1048 &new_band)) {
1049 sdata_info(sdata,
1050 "cannot understand ECSA IE operating class %d, disconnecting\n",
1051 elems->ext_chansw_ie->new_operating_class);
1052 ieee80211_queue_work(&local->hw,
1053 &ifmgd->csa_connection_drop_work);
1054 }
1055 new_chan_no = elems->ext_chansw_ie->new_ch_num;
1056 count = elems->ext_chansw_ie->count;
1057 mode = elems->ext_chansw_ie->mode;
1058 } else if (elems->ch_switch_ie) {
1059 new_band = cbss->channel->band;
1060 new_chan_no = elems->ch_switch_ie->new_ch_num;
1061 count = elems->ch_switch_ie->count;
1062 mode = elems->ch_switch_ie->mode;
1063 } else {
1064 /* nothing here we understand */
1065 return; 985 return;
1066 }
1067
1068 bss = (void *)cbss->priv;
1069 986
1070 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band); 987 if (!cfg80211_chandef_usable(local->hw.wiphy, &csa_ie.chandef,
1071 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq); 988 IEEE80211_CHAN_DISABLED)) {
1072 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
1073 sdata_info(sdata, 989 sdata_info(sdata,
1074 "AP %pM switches to unsupported channel (%d MHz), disconnecting\n", 990 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1075 ifmgd->associated->bssid, new_freq); 991 ifmgd->associated->bssid,
992 csa_ie.chandef.chan->center_freq,
993 csa_ie.chandef.width, csa_ie.chandef.center_freq1,
994 csa_ie.chandef.center_freq2);
1076 ieee80211_queue_work(&local->hw, 995 ieee80211_queue_work(&local->hw,
1077 &ifmgd->csa_connection_drop_work); 996 &ifmgd->csa_connection_drop_work);
1078 return; 997 return;
1079 } 998 }
1080 999
1081 if (!beacon && sec_chan_offs) { 1000 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED;
1082 secondary_channel_offset = sec_chan_offs->sec_chan_offs; 1001 sdata->vif.csa_active = true;
1083 } else if (beacon && ht_oper) {
1084 secondary_channel_offset =
1085 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
1086 } else if (!(ifmgd->flags & IEEE80211_STA_DISABLE_HT)) {
1087 /*
1088 * If it's not a beacon, HT is enabled and the IE not present,
1089 * it's 20 MHz, 802.11-2012 8.5.2.6:
1090 * This element [the Secondary Channel Offset Element] is
1091 * present when switching to a 40 MHz channel. It may be
1092 * present when switching to a 20 MHz channel (in which
1093 * case the secondary channel offset is set to SCN).
1094 */
1095 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
1096 }
1097
1098 switch (secondary_channel_offset) {
1099 default:
1100 /* secondary_channel_offset was present but is invalid */
1101 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
1102 cfg80211_chandef_create(&new_chandef, new_chan,
1103 NL80211_CHAN_HT20);
1104 break;
1105 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
1106 cfg80211_chandef_create(&new_chandef, new_chan,
1107 NL80211_CHAN_HT40PLUS);
1108 break;
1109 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
1110 cfg80211_chandef_create(&new_chandef, new_chan,
1111 NL80211_CHAN_HT40MINUS);
1112 break;
1113 case -1:
1114 cfg80211_chandef_create(&new_chandef, new_chan,
1115 NL80211_CHAN_NO_HT);
1116 /* keep width for 5/10 MHz channels */
1117 switch (sdata->vif.bss_conf.chandef.width) {
1118 case NL80211_CHAN_WIDTH_5:
1119 case NL80211_CHAN_WIDTH_10:
1120 new_chandef.width = sdata->vif.bss_conf.chandef.width;
1121 break;
1122 default:
1123 break;
1124 }
1125 break;
1126 }
1127 1002
1128 if (wide_bw_chansw_ie) { 1003 mutex_lock(&local->chanctx_mtx);
1129 new_vht_chandef.chan = new_chan; 1004 if (local->use_chanctx) {
1130 new_vht_chandef.center_freq1 = 1005 u32 num_chanctx = 0;
1131 ieee80211_channel_to_frequency( 1006 list_for_each_entry(chanctx, &local->chanctx_list, list)
1132 wide_bw_chansw_ie->new_center_freq_seg0, 1007 num_chanctx++;
1133 new_band);
1134 1008
1135 switch (wide_bw_chansw_ie->new_channel_width) { 1009 if (num_chanctx > 1 ||
1136 default: 1010 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
1137 /* hmmm, ignore VHT and use HT if present */
1138 case IEEE80211_VHT_CHANWIDTH_USE_HT:
1139 new_vht_chandef.chan = NULL;
1140 break;
1141 case IEEE80211_VHT_CHANWIDTH_80MHZ:
1142 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
1143 break;
1144 case IEEE80211_VHT_CHANWIDTH_160MHZ:
1145 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
1146 break;
1147 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
1148 /* field is otherwise reserved */
1149 new_vht_chandef.center_freq2 =
1150 ieee80211_channel_to_frequency(
1151 wide_bw_chansw_ie->new_center_freq_seg1,
1152 new_band);
1153 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
1154 break;
1155 }
1156 if (ifmgd->flags & IEEE80211_STA_DISABLE_80P80MHZ &&
1157 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
1158 chandef_downgrade(&new_vht_chandef);
1159 if (ifmgd->flags & IEEE80211_STA_DISABLE_160MHZ &&
1160 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
1161 chandef_downgrade(&new_vht_chandef);
1162 if (ifmgd->flags & IEEE80211_STA_DISABLE_40MHZ &&
1163 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
1164 chandef_downgrade(&new_vht_chandef);
1165 }
1166
1167 /* if VHT data is there validate & use it */
1168 if (new_vht_chandef.chan) {
1169 if (!cfg80211_chandef_compatible(&new_vht_chandef,
1170 &new_chandef)) {
1171 sdata_info(sdata, 1011 sdata_info(sdata,
1172 "AP %pM CSA has inconsistent channel data, disconnecting\n", 1012 "not handling chan-switch with channel contexts\n");
1173 ifmgd->associated->bssid);
1174 ieee80211_queue_work(&local->hw, 1013 ieee80211_queue_work(&local->hw,
1175 &ifmgd->csa_connection_drop_work); 1014 &ifmgd->csa_connection_drop_work);
1015 mutex_unlock(&local->chanctx_mtx);
1176 return; 1016 return;
1177 } 1017 }
1178 new_chandef = new_vht_chandef;
1179 }
1180
1181 if (!cfg80211_chandef_usable(local->hw.wiphy, &new_chandef,
1182 IEEE80211_CHAN_DISABLED)) {
1183 sdata_info(sdata,
1184 "AP %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
1185 ifmgd->associated->bssid, new_freq,
1186 new_chandef.width, new_chandef.center_freq1,
1187 new_chandef.center_freq2);
1188 ieee80211_queue_work(&local->hw,
1189 &ifmgd->csa_connection_drop_work);
1190 return;
1191 } 1018 }
1192 1019
1193 ifmgd->flags |= IEEE80211_STA_CSA_RECEIVED; 1020 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1194
1195 if (local->use_chanctx) {
1196 sdata_info(sdata,
1197 "not handling channel switch with channel contexts\n");
1198 ieee80211_queue_work(&local->hw, 1021 ieee80211_queue_work(&local->hw,
1199 &ifmgd->csa_connection_drop_work); 1022 &ifmgd->csa_connection_drop_work);
1200 return;
1201 }
1202
1203 mutex_lock(&local->chanctx_mtx);
1204 if (WARN_ON(!rcu_access_pointer(sdata->vif.chanctx_conf))) {
1205 mutex_unlock(&local->chanctx_mtx); 1023 mutex_unlock(&local->chanctx_mtx);
1206 return; 1024 return;
1207 } 1025 }
@@ -1217,9 +1035,9 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1217 } 1035 }
1218 mutex_unlock(&local->chanctx_mtx); 1036 mutex_unlock(&local->chanctx_mtx);
1219 1037
1220 local->csa_chandef = new_chandef; 1038 local->csa_chandef = csa_ie.chandef;
1221 1039
1222 if (mode) 1040 if (csa_ie.mode)
1223 ieee80211_stop_queues_by_reason(&local->hw, 1041 ieee80211_stop_queues_by_reason(&local->hw,
1224 IEEE80211_MAX_QUEUE_MAP, 1042 IEEE80211_MAX_QUEUE_MAP,
1225 IEEE80211_QUEUE_STOP_REASON_CSA); 1043 IEEE80211_QUEUE_STOP_REASON_CSA);
@@ -1228,9 +1046,9 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1228 /* use driver's channel switch callback */ 1046 /* use driver's channel switch callback */
1229 struct ieee80211_channel_switch ch_switch = { 1047 struct ieee80211_channel_switch ch_switch = {
1230 .timestamp = timestamp, 1048 .timestamp = timestamp,
1231 .block_tx = mode, 1049 .block_tx = csa_ie.mode,
1232 .chandef = new_chandef, 1050 .chandef = csa_ie.chandef,
1233 .count = count, 1051 .count = csa_ie.count,
1234 }; 1052 };
1235 1053
1236 drv_channel_switch(local, &ch_switch); 1054 drv_channel_switch(local, &ch_switch);
@@ -1238,11 +1056,11 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1238 } 1056 }
1239 1057
1240 /* channel switch handled in software */ 1058 /* channel switch handled in software */
1241 if (count <= 1) 1059 if (csa_ie.count <= 1)
1242 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work); 1060 ieee80211_queue_work(&local->hw, &ifmgd->chswitch_work);
1243 else 1061 else
1244 mod_timer(&ifmgd->chswitch_timer, 1062 mod_timer(&ifmgd->chswitch_timer,
1245 TU_TO_EXP_TIME(count * cbss->beacon_interval)); 1063 TU_TO_EXP_TIME(csa_ie.count * cbss->beacon_interval));
1246} 1064}
1247 1065
1248static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata, 1066static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
@@ -1374,8 +1192,7 @@ static bool ieee80211_powersave_allowed(struct ieee80211_sub_if_data *sdata)
1374 if (!mgd->associated) 1192 if (!mgd->associated)
1375 return false; 1193 return false;
1376 1194
1377 if (mgd->flags & (IEEE80211_STA_BEACON_POLL | 1195 if (mgd->flags & IEEE80211_STA_CONNECTION_POLL)
1378 IEEE80211_STA_CONNECTION_POLL))
1379 return false; 1196 return false;
1380 1197
1381 if (!mgd->have_beacon) 1198 if (!mgd->have_beacon)
@@ -1691,8 +1508,7 @@ static void __ieee80211_stop_poll(struct ieee80211_sub_if_data *sdata)
1691{ 1508{
1692 lockdep_assert_held(&sdata->local->mtx); 1509 lockdep_assert_held(&sdata->local->mtx);
1693 1510
1694 sdata->u.mgd.flags &= ~(IEEE80211_STA_CONNECTION_POLL | 1511 sdata->u.mgd.flags &= ~IEEE80211_STA_CONNECTION_POLL;
1695 IEEE80211_STA_BEACON_POLL);
1696 ieee80211_run_deferred_scan(sdata->local); 1512 ieee80211_run_deferred_scan(sdata->local);
1697} 1513}
1698 1514
@@ -1954,11 +1770,8 @@ static void ieee80211_reset_ap_probe(struct ieee80211_sub_if_data *sdata)
1954 struct ieee80211_local *local = sdata->local; 1770 struct ieee80211_local *local = sdata->local;
1955 1771
1956 mutex_lock(&local->mtx); 1772 mutex_lock(&local->mtx);
1957 if (!(ifmgd->flags & (IEEE80211_STA_BEACON_POLL | 1773 if (!(ifmgd->flags & IEEE80211_STA_CONNECTION_POLL))
1958 IEEE80211_STA_CONNECTION_POLL))) { 1774 goto out;
1959 mutex_unlock(&local->mtx);
1960 return;
1961 }
1962 1775
1963 __ieee80211_stop_poll(sdata); 1776 __ieee80211_stop_poll(sdata);
1964 1777
@@ -2094,14 +1907,10 @@ static void ieee80211_mgd_probe_ap(struct ieee80211_sub_if_data *sdata,
2094 * because otherwise we would reset the timer every time and 1907 * because otherwise we would reset the timer every time and
2095 * never check whether we received a probe response! 1908 * never check whether we received a probe response!
2096 */ 1909 */
2097 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL | 1910 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL)
2098 IEEE80211_STA_CONNECTION_POLL))
2099 already = true; 1911 already = true;
2100 1912
2101 if (beacon) 1913 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2102 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2103 else
2104 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2105 1914
2106 mutex_unlock(&sdata->local->mtx); 1915 mutex_unlock(&sdata->local->mtx);
2107 1916
@@ -2174,6 +1983,7 @@ static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2174 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, 1983 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2175 true, frame_buf); 1984 true, frame_buf);
2176 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED; 1985 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1986 sdata->vif.csa_active = false;
2177 ieee80211_wake_queues_by_reason(&sdata->local->hw, 1987 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2178 IEEE80211_MAX_QUEUE_MAP, 1988 IEEE80211_MAX_QUEUE_MAP,
2179 IEEE80211_QUEUE_STOP_REASON_CSA); 1989 IEEE80211_QUEUE_STOP_REASON_CSA);
@@ -2717,7 +2527,7 @@ static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2717 */ 2527 */
2718 ifmgd->wmm_last_param_set = -1; 2528 ifmgd->wmm_last_param_set = -1;
2719 2529
2720 if (elems.wmm_param) 2530 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
2721 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param, 2531 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2722 elems.wmm_param_len); 2532 elems.wmm_param_len);
2723 else 2533 else
@@ -3061,17 +2871,10 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3061 } 2871 }
3062 } 2872 }
3063 2873
3064 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) { 2874 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
3065 mlme_dbg_ratelimited(sdata, 2875 mlme_dbg_ratelimited(sdata,
3066 "cancelling AP probe due to a received beacon\n"); 2876 "cancelling AP probe due to a received beacon\n");
3067 mutex_lock(&local->mtx); 2877 ieee80211_reset_ap_probe(sdata);
3068 ifmgd->flags &= ~IEEE80211_STA_BEACON_POLL;
3069 ieee80211_run_deferred_scan(local);
3070 mutex_unlock(&local->mtx);
3071
3072 mutex_lock(&local->iflist_mtx);
3073 ieee80211_recalc_ps(local, -1);
3074 mutex_unlock(&local->iflist_mtx);
3075 } 2878 }
3076 2879
3077 /* 2880 /*
@@ -3152,7 +2955,8 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3152 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime, 2955 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3153 &elems, true); 2956 &elems, true);
3154 2957
3155 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param, 2958 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
2959 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3156 elems.wmm_param_len)) 2960 elems.wmm_param_len))
3157 changed |= BSS_CHANGED_QOS; 2961 changed |= BSS_CHANGED_QOS;
3158 2962
@@ -3543,8 +3347,7 @@ void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3543 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started) 3347 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3544 run_again(sdata, ifmgd->assoc_data->timeout); 3348 run_again(sdata, ifmgd->assoc_data->timeout);
3545 3349
3546 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL | 3350 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
3547 IEEE80211_STA_CONNECTION_POLL) &&
3548 ifmgd->associated) { 3351 ifmgd->associated) {
3549 u8 bssid[ETH_ALEN]; 3352 u8 bssid[ETH_ALEN];
3550 int max_tries; 3353 int max_tries;
@@ -3697,7 +3500,7 @@ void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3697 ieee80211_beacon_connection_loss_work); 3500 ieee80211_beacon_connection_loss_work);
3698 INIT_WORK(&ifmgd->csa_connection_drop_work, 3501 INIT_WORK(&ifmgd->csa_connection_drop_work,
3699 ieee80211_csa_connection_drop_work); 3502 ieee80211_csa_connection_drop_work);
3700 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work); 3503 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
3701 setup_timer(&ifmgd->timer, ieee80211_sta_timer, 3504 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3702 (unsigned long) sdata); 3505 (unsigned long) sdata);
3703 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 3506 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
@@ -3876,7 +3679,7 @@ static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3876 return ret; 3679 return ret;
3877 3680
3878 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) { 3681 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3879 ifmgd->flags |= chandef_downgrade(&chandef); 3682 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
3880 ret = ieee80211_vif_use_channel(sdata, &chandef, 3683 ret = ieee80211_vif_use_channel(sdata, &chandef,
3881 IEEE80211_CHANCTX_SHARED); 3684 IEEE80211_CHANCTX_SHARED);
3882 } 3685 }
@@ -4135,6 +3938,44 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4135 return err; 3938 return err;
4136} 3939}
4137 3940
3941static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
3942 const u8 *wmm_param, int len)
3943{
3944 const u8 *pos;
3945 size_t left;
3946
3947 if (len < 8)
3948 return false;
3949
3950 if (wmm_param[5] != 1 /* version */)
3951 return false;
3952
3953 pos = wmm_param + 8;
3954 left = len - 8;
3955
3956 for (; left >= 4; left -= 4, pos += 4) {
3957 u8 aifsn = pos[0] & 0x0f;
3958 u8 ecwmin = pos[1] & 0x0f;
3959 u8 ecwmax = (pos[1] & 0xf0) >> 4;
3960 int aci = (pos[0] >> 5) & 0x03;
3961
3962 if (aifsn < 2) {
3963 sdata_info(sdata,
3964 "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
3965 aifsn, aci);
3966 return false;
3967 }
3968 if (ecwmin > ecwmax) {
3969 sdata_info(sdata,
3970 "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
3971 ecwmin, ecwmax, aci);
3972 return false;
3973 }
3974 }
3975
3976 return true;
3977}
3978
4138int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata, 3979int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4139 struct cfg80211_assoc_request *req) 3980 struct cfg80211_assoc_request *req)
4140{ 3981{
@@ -4192,9 +4033,45 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4192 } 4033 }
4193 4034
4194 /* prepare assoc data */ 4035 /* prepare assoc data */
4195 4036
4196 ifmgd->beacon_crc_valid = false; 4037 ifmgd->beacon_crc_valid = false;
4197 4038
4039 assoc_data->wmm = bss->wmm_used &&
4040 (local->hw.queues >= IEEE80211_NUM_ACS);
4041 if (assoc_data->wmm) {
4042 /* try to check validity of WMM params IE */
4043 const struct cfg80211_bss_ies *ies;
4044 const u8 *wp, *start, *end;
4045
4046 rcu_read_lock();
4047 ies = rcu_dereference(req->bss->ies);
4048 start = ies->data;
4049 end = start + ies->len;
4050
4051 while (true) {
4052 wp = cfg80211_find_vendor_ie(
4053 WLAN_OUI_MICROSOFT,
4054 WLAN_OUI_TYPE_MICROSOFT_WMM,
4055 start, end - start);
4056 if (!wp)
4057 break;
4058 start = wp + wp[1] + 2;
4059 /* if this IE is too short, try the next */
4060 if (wp[1] <= 4)
4061 continue;
4062 /* if this IE is WMM params, we found what we wanted */
4063 if (wp[6] == 1)
4064 break;
4065 }
4066
4067 if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
4068 wp[1] - 2)) {
4069 assoc_data->wmm = false;
4070 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
4071 }
4072 rcu_read_unlock();
4073 }
4074
4198 /* 4075 /*
4199 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode. 4076 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode.
4200 * We still associate in non-HT mode (11a/b/g) if any one of these 4077 * We still associate in non-HT mode (11a/b/g) if any one of these
@@ -4224,18 +4101,22 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4224 /* Also disable HT if we don't support it or the AP doesn't use WMM */ 4101 /* Also disable HT if we don't support it or the AP doesn't use WMM */
4225 sband = local->hw.wiphy->bands[req->bss->channel->band]; 4102 sband = local->hw.wiphy->bands[req->bss->channel->band];
4226 if (!sband->ht_cap.ht_supported || 4103 if (!sband->ht_cap.ht_supported ||
4227 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) { 4104 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4105 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4228 ifmgd->flags |= IEEE80211_STA_DISABLE_HT; 4106 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4229 if (!bss->wmm_used) 4107 if (!bss->wmm_used &&
4108 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4230 netdev_info(sdata->dev, 4109 netdev_info(sdata->dev,
4231 "disabling HT as WMM/QoS is not supported by the AP\n"); 4110 "disabling HT as WMM/QoS is not supported by the AP\n");
4232 } 4111 }
4233 4112
4234 /* disable VHT if we don't support it or the AP doesn't use WMM */ 4113 /* disable VHT if we don't support it or the AP doesn't use WMM */
4235 if (!sband->vht_cap.vht_supported || 4114 if (!sband->vht_cap.vht_supported ||
4236 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) { 4115 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4116 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4237 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT; 4117 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4238 if (!bss->wmm_used) 4118 if (!bss->wmm_used &&
4119 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4239 netdev_info(sdata->dev, 4120 netdev_info(sdata->dev,
4240 "disabling VHT as WMM/QoS is not supported by the AP\n"); 4121 "disabling VHT as WMM/QoS is not supported by the AP\n");
4241 } 4122 }
@@ -4264,8 +4145,6 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4264 sdata->smps_mode = ifmgd->req_smps; 4145 sdata->smps_mode = ifmgd->req_smps;
4265 4146
4266 assoc_data->capability = req->bss->capability; 4147 assoc_data->capability = req->bss->capability;
4267 assoc_data->wmm = bss->wmm_used &&
4268 (local->hw.queues >= IEEE80211_NUM_ACS);
4269 assoc_data->supp_rates = bss->supp_rates; 4148 assoc_data->supp_rates = bss->supp_rates;
4270 assoc_data->supp_rates_len = bss->supp_rates_len; 4149 assoc_data->supp_rates_len = bss->supp_rates_len;
4271 4150
diff --git a/net/mac80211/rate.c b/net/mac80211/rate.c
index e126605cec66..22b223f13c9f 100644
--- a/net/mac80211/rate.c
+++ b/net/mac80211/rate.c
@@ -235,7 +235,8 @@ static void rc_send_low_basicrate(s8 *idx, u32 basic_rates,
235static void __rate_control_send_low(struct ieee80211_hw *hw, 235static void __rate_control_send_low(struct ieee80211_hw *hw,
236 struct ieee80211_supported_band *sband, 236 struct ieee80211_supported_band *sband,
237 struct ieee80211_sta *sta, 237 struct ieee80211_sta *sta,
238 struct ieee80211_tx_info *info) 238 struct ieee80211_tx_info *info,
239 u32 rate_mask)
239{ 240{
240 int i; 241 int i;
241 u32 rate_flags = 242 u32 rate_flags =
@@ -247,6 +248,12 @@ static void __rate_control_send_low(struct ieee80211_hw *hw,
247 248
248 info->control.rates[0].idx = 0; 249 info->control.rates[0].idx = 0;
249 for (i = 0; i < sband->n_bitrates; i++) { 250 for (i = 0; i < sband->n_bitrates; i++) {
251 if (!(rate_mask & BIT(i)))
252 continue;
253
254 if ((rate_flags & sband->bitrates[i].flags) != rate_flags)
255 continue;
256
250 if (!rate_supported(sta, sband->band, i)) 257 if (!rate_supported(sta, sband->band, i))
251 continue; 258 continue;
252 259
@@ -274,7 +281,8 @@ bool rate_control_send_low(struct ieee80211_sta *pubsta,
274 bool use_basicrate = false; 281 bool use_basicrate = false;
275 282
276 if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) { 283 if (!pubsta || !priv_sta || rc_no_data_or_no_ack_use_min(txrc)) {
277 __rate_control_send_low(txrc->hw, sband, pubsta, info); 284 __rate_control_send_low(txrc->hw, sband, pubsta, info,
285 txrc->rate_idx_mask);
278 286
279 if (!pubsta && txrc->bss) { 287 if (!pubsta && txrc->bss) {
280 mcast_rate = txrc->bss_conf->mcast_rate[sband->band]; 288 mcast_rate = txrc->bss_conf->mcast_rate[sband->band];
@@ -656,7 +664,8 @@ void ieee80211_get_tx_rates(struct ieee80211_vif *vif,
656 rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates); 664 rate_control_apply_mask(sdata, sta, sband, info, dest, max_rates);
657 665
658 if (dest[0].idx < 0) 666 if (dest[0].idx < 0)
659 __rate_control_send_low(&sdata->local->hw, sband, sta, info); 667 __rate_control_send_low(&sdata->local->hw, sband, sta, info,
668 sdata->rc_rateidx_mask[info->band]);
660 669
661 if (sta) 670 if (sta)
662 rate_fixup_ratelist(vif, sband, info, dest, max_rates); 671 rate_fixup_ratelist(vif, sband, info, dest, max_rates);
diff --git a/net/mac80211/rate.h b/net/mac80211/rate.h
index 5dedc56c94db..505bc0dea074 100644
--- a/net/mac80211/rate.h
+++ b/net/mac80211/rate.h
@@ -144,8 +144,8 @@ void rate_control_deinitialize(struct ieee80211_local *local);
144 144
145/* Rate control algorithms */ 145/* Rate control algorithms */
146#ifdef CONFIG_MAC80211_RC_PID 146#ifdef CONFIG_MAC80211_RC_PID
147extern int rc80211_pid_init(void); 147int rc80211_pid_init(void);
148extern void rc80211_pid_exit(void); 148void rc80211_pid_exit(void);
149#else 149#else
150static inline int rc80211_pid_init(void) 150static inline int rc80211_pid_init(void)
151{ 151{
@@ -157,8 +157,8 @@ static inline void rc80211_pid_exit(void)
157#endif 157#endif
158 158
159#ifdef CONFIG_MAC80211_RC_MINSTREL 159#ifdef CONFIG_MAC80211_RC_MINSTREL
160extern int rc80211_minstrel_init(void); 160int rc80211_minstrel_init(void);
161extern void rc80211_minstrel_exit(void); 161void rc80211_minstrel_exit(void);
162#else 162#else
163static inline int rc80211_minstrel_init(void) 163static inline int rc80211_minstrel_init(void)
164{ 164{
@@ -170,8 +170,8 @@ static inline void rc80211_minstrel_exit(void)
170#endif 170#endif
171 171
172#ifdef CONFIG_MAC80211_RC_MINSTREL_HT 172#ifdef CONFIG_MAC80211_RC_MINSTREL_HT
173extern int rc80211_minstrel_ht_init(void); 173int rc80211_minstrel_ht_init(void);
174extern void rc80211_minstrel_ht_exit(void); 174void rc80211_minstrel_ht_exit(void);
175#else 175#else
176static inline int rc80211_minstrel_ht_init(void) 176static inline int rc80211_minstrel_ht_init(void)
177{ 177{
diff --git a/net/mac80211/rc80211_minstrel.c b/net/mac80211/rc80211_minstrel.c
index 8b5f7ef7c0c9..7fa1b36e6202 100644
--- a/net/mac80211/rc80211_minstrel.c
+++ b/net/mac80211/rc80211_minstrel.c
@@ -203,6 +203,15 @@ minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
203 memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate)); 203 memcpy(mi->max_tp_rate, tmp_tp_rate, sizeof(mi->max_tp_rate));
204 mi->max_prob_rate = tmp_prob_rate; 204 mi->max_prob_rate = tmp_prob_rate;
205 205
206#ifdef CONFIG_MAC80211_DEBUGFS
207 /* use fixed index if set */
208 if (mp->fixed_rate_idx != -1) {
209 mi->max_tp_rate[0] = mp->fixed_rate_idx;
210 mi->max_tp_rate[1] = mp->fixed_rate_idx;
211 mi->max_prob_rate = mp->fixed_rate_idx;
212 }
213#endif
214
206 /* Reset update timer */ 215 /* Reset update timer */
207 mi->stats_update = jiffies; 216 mi->stats_update = jiffies;
208 217
@@ -310,6 +319,11 @@ minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
310 /* increase sum packet counter */ 319 /* increase sum packet counter */
311 mi->packet_count++; 320 mi->packet_count++;
312 321
322#ifdef CONFIG_MAC80211_DEBUGFS
323 if (mp->fixed_rate_idx != -1)
324 return;
325#endif
326
313 delta = (mi->packet_count * sampling_ratio / 100) - 327 delta = (mi->packet_count * sampling_ratio / 100) -
314 (mi->sample_count + mi->sample_deferred / 2); 328 (mi->sample_count + mi->sample_deferred / 2);
315 329
diff --git a/net/mac80211/rc80211_minstrel_ht.c b/net/mac80211/rc80211_minstrel_ht.c
index 7c323f27ba23..4096ff6cc24f 100644
--- a/net/mac80211/rc80211_minstrel_ht.c
+++ b/net/mac80211/rc80211_minstrel_ht.c
@@ -226,7 +226,7 @@ minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
226 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len); 226 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
227 227
228 nsecs += minstrel_mcs_groups[group].duration[rate]; 228 nsecs += minstrel_mcs_groups[group].duration[rate];
229 tp = 1000000 * ((mr->probability * 1000) / nsecs); 229 tp = 1000000 * ((prob * 1000) / nsecs);
230 230
231 mr->cur_tp = MINSTREL_TRUNC(tp); 231 mr->cur_tp = MINSTREL_TRUNC(tp);
232} 232}
@@ -277,13 +277,15 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
277 if (!(mg->supported & BIT(i))) 277 if (!(mg->supported & BIT(i)))
278 continue; 278 continue;
279 279
280 index = MCS_GROUP_RATES * group + i;
281
280 /* initialize rates selections starting indexes */ 282 /* initialize rates selections starting indexes */
281 if (!mg_rates_valid) { 283 if (!mg_rates_valid) {
282 mg->max_tp_rate = mg->max_tp_rate2 = 284 mg->max_tp_rate = mg->max_tp_rate2 =
283 mg->max_prob_rate = i; 285 mg->max_prob_rate = i;
284 if (!mi_rates_valid) { 286 if (!mi_rates_valid) {
285 mi->max_tp_rate = mi->max_tp_rate2 = 287 mi->max_tp_rate = mi->max_tp_rate2 =
286 mi->max_prob_rate = i; 288 mi->max_prob_rate = index;
287 mi_rates_valid = true; 289 mi_rates_valid = true;
288 } 290 }
289 mg_rates_valid = true; 291 mg_rates_valid = true;
@@ -291,7 +293,6 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
291 293
292 mr = &mg->rates[i]; 294 mr = &mg->rates[i];
293 mr->retry_updated = false; 295 mr->retry_updated = false;
294 index = MCS_GROUP_RATES * group + i;
295 minstrel_calc_rate_ewma(mr); 296 minstrel_calc_rate_ewma(mr);
296 minstrel_ht_calc_tp(mi, group, i); 297 minstrel_ht_calc_tp(mi, group, i);
297 298
@@ -365,6 +366,14 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
365 } 366 }
366 } 367 }
367 368
369#ifdef CONFIG_MAC80211_DEBUGFS
370 /* use fixed index if set */
371 if (mp->fixed_rate_idx != -1) {
372 mi->max_tp_rate = mp->fixed_rate_idx;
373 mi->max_tp_rate2 = mp->fixed_rate_idx;
374 mi->max_prob_rate = mp->fixed_rate_idx;
375 }
376#endif
368 377
369 mi->stats_update = jiffies; 378 mi->stats_update = jiffies;
370} 379}
@@ -774,6 +783,11 @@ minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
774 info->flags |= mi->tx_flags; 783 info->flags |= mi->tx_flags;
775 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble); 784 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
776 785
786#ifdef CONFIG_MAC80211_DEBUGFS
787 if (mp->fixed_rate_idx != -1)
788 return;
789#endif
790
777 /* Don't use EAPOL frames for sampling on non-mrr hw */ 791 /* Don't use EAPOL frames for sampling on non-mrr hw */
778 if (mp->hw->max_rates == 1 && 792 if (mp->hw->max_rates == 1 &&
779 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)) 793 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
@@ -781,16 +795,6 @@ minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
781 else 795 else
782 sample_idx = minstrel_get_sample_rate(mp, mi); 796 sample_idx = minstrel_get_sample_rate(mp, mi);
783 797
784#ifdef CONFIG_MAC80211_DEBUGFS
785 /* use fixed index if set */
786 if (mp->fixed_rate_idx != -1) {
787 mi->max_tp_rate = mp->fixed_rate_idx;
788 mi->max_tp_rate2 = mp->fixed_rate_idx;
789 mi->max_prob_rate = mp->fixed_rate_idx;
790 sample_idx = -1;
791 }
792#endif
793
794 mi->total_packets++; 798 mi->total_packets++;
795 799
796 /* wraparound */ 800 /* wraparound */
diff --git a/net/mac80211/rc80211_pid_debugfs.c b/net/mac80211/rc80211_pid_debugfs.c
index c97a0657c043..6ff134650a84 100644
--- a/net/mac80211/rc80211_pid_debugfs.c
+++ b/net/mac80211/rc80211_pid_debugfs.c
@@ -167,29 +167,29 @@ static ssize_t rate_control_pid_events_read(struct file *file, char __user *buf,
167 * provide large enough buffers. */ 167 * provide large enough buffers. */
168 length = length < RC_PID_PRINT_BUF_SIZE ? 168 length = length < RC_PID_PRINT_BUF_SIZE ?
169 length : RC_PID_PRINT_BUF_SIZE; 169 length : RC_PID_PRINT_BUF_SIZE;
170 p = snprintf(pb, length, "%u %lu ", ev->id, ev->timestamp); 170 p = scnprintf(pb, length, "%u %lu ", ev->id, ev->timestamp);
171 switch (ev->type) { 171 switch (ev->type) {
172 case RC_PID_EVENT_TYPE_TX_STATUS: 172 case RC_PID_EVENT_TYPE_TX_STATUS:
173 p += snprintf(pb + p, length - p, "tx_status %u %u", 173 p += scnprintf(pb + p, length - p, "tx_status %u %u",
174 !(ev->data.flags & IEEE80211_TX_STAT_ACK), 174 !(ev->data.flags & IEEE80211_TX_STAT_ACK),
175 ev->data.tx_status.status.rates[0].idx); 175 ev->data.tx_status.status.rates[0].idx);
176 break; 176 break;
177 case RC_PID_EVENT_TYPE_RATE_CHANGE: 177 case RC_PID_EVENT_TYPE_RATE_CHANGE:
178 p += snprintf(pb + p, length - p, "rate_change %d %d", 178 p += scnprintf(pb + p, length - p, "rate_change %d %d",
179 ev->data.index, ev->data.rate); 179 ev->data.index, ev->data.rate);
180 break; 180 break;
181 case RC_PID_EVENT_TYPE_TX_RATE: 181 case RC_PID_EVENT_TYPE_TX_RATE:
182 p += snprintf(pb + p, length - p, "tx_rate %d %d", 182 p += scnprintf(pb + p, length - p, "tx_rate %d %d",
183 ev->data.index, ev->data.rate); 183 ev->data.index, ev->data.rate);
184 break; 184 break;
185 case RC_PID_EVENT_TYPE_PF_SAMPLE: 185 case RC_PID_EVENT_TYPE_PF_SAMPLE:
186 p += snprintf(pb + p, length - p, 186 p += scnprintf(pb + p, length - p,
187 "pf_sample %d %d %d %d", 187 "pf_sample %d %d %d %d",
188 ev->data.pf_sample, ev->data.prop_err, 188 ev->data.pf_sample, ev->data.prop_err,
189 ev->data.int_err, ev->data.der_err); 189 ev->data.int_err, ev->data.der_err);
190 break; 190 break;
191 } 191 }
192 p += snprintf(pb + p, length - p, "\n"); 192 p += scnprintf(pb + p, length - p, "\n");
193 193
194 spin_unlock_irqrestore(&events->lock, status); 194 spin_unlock_irqrestore(&events->lock, status);
195 195
diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
index 674eac1f996c..2b0debb0422b 100644
--- a/net/mac80211/rx.c
+++ b/net/mac80211/rx.c
@@ -911,7 +911,8 @@ static void ieee80211_rx_reorder_ampdu(struct ieee80211_rx_data *rx,
911 u16 sc; 911 u16 sc;
912 u8 tid, ack_policy; 912 u8 tid, ack_policy;
913 913
914 if (!ieee80211_is_data_qos(hdr->frame_control)) 914 if (!ieee80211_is_data_qos(hdr->frame_control) ||
915 is_multicast_ether_addr(hdr->addr1))
915 goto dont_reorder; 916 goto dont_reorder;
916 917
917 /* 918 /*
@@ -995,8 +996,9 @@ ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
995 rx->sta->num_duplicates++; 996 rx->sta->num_duplicates++;
996 } 997 }
997 return RX_DROP_UNUSABLE; 998 return RX_DROP_UNUSABLE;
998 } else 999 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
999 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl; 1000 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1001 }
1000 } 1002 }
1001 1003
1002 if (unlikely(rx->skb->len < 16)) { 1004 if (unlikely(rx->skb->len < 16)) {
@@ -2402,7 +2404,8 @@ ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2402 return RX_DROP_UNUSABLE; 2404 return RX_DROP_UNUSABLE;
2403 2405
2404 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC && 2406 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2405 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED) 2407 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2408 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2406 return RX_DROP_UNUSABLE; 2409 return RX_DROP_UNUSABLE;
2407 2410
2408 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH)) 2411 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
@@ -2566,31 +2569,49 @@ ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2566 2569
2567 goto queue; 2570 goto queue;
2568 case WLAN_CATEGORY_SPECTRUM_MGMT: 2571 case WLAN_CATEGORY_SPECTRUM_MGMT:
2569 if (status->band != IEEE80211_BAND_5GHZ)
2570 break;
2571
2572 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2573 break;
2574
2575 /* verify action_code is present */ 2572 /* verify action_code is present */
2576 if (len < IEEE80211_MIN_ACTION_SIZE + 1) 2573 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2577 break; 2574 break;
2578 2575
2579 switch (mgmt->u.action.u.measurement.action_code) { 2576 switch (mgmt->u.action.u.measurement.action_code) {
2580 case WLAN_ACTION_SPCT_MSR_REQ: 2577 case WLAN_ACTION_SPCT_MSR_REQ:
2578 if (status->band != IEEE80211_BAND_5GHZ)
2579 break;
2580
2581 if (len < (IEEE80211_MIN_ACTION_SIZE + 2581 if (len < (IEEE80211_MIN_ACTION_SIZE +
2582 sizeof(mgmt->u.action.u.measurement))) 2582 sizeof(mgmt->u.action.u.measurement)))
2583 break; 2583 break;
2584
2585 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2586 break;
2587
2584 ieee80211_process_measurement_req(sdata, mgmt, len); 2588 ieee80211_process_measurement_req(sdata, mgmt, len);
2585 goto handled; 2589 goto handled;
2586 case WLAN_ACTION_SPCT_CHL_SWITCH: 2590 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2587 if (sdata->vif.type != NL80211_IFTYPE_STATION) 2591 u8 *bssid;
2592 if (len < (IEEE80211_MIN_ACTION_SIZE +
2593 sizeof(mgmt->u.action.u.chan_switch)))
2594 break;
2595
2596 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2597 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2598 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2588 break; 2599 break;
2589 2600
2590 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) 2601 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2602 bssid = sdata->u.mgd.bssid;
2603 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2604 bssid = sdata->u.ibss.bssid;
2605 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2606 bssid = mgmt->sa;
2607 else
2608 break;
2609
2610 if (!ether_addr_equal(mgmt->bssid, bssid))
2591 break; 2611 break;
2592 2612
2593 goto queue; 2613 goto queue;
2614 }
2594 } 2615 }
2595 break; 2616 break;
2596 case WLAN_CATEGORY_SA_QUERY: 2617 case WLAN_CATEGORY_SA_QUERY:
diff --git a/net/mac80211/scan.c b/net/mac80211/scan.c
index d2d17a449224..bcc4833d7542 100644
--- a/net/mac80211/scan.c
+++ b/net/mac80211/scan.c
@@ -394,8 +394,7 @@ static bool ieee80211_can_scan(struct ieee80211_local *local,
394 return false; 394 return false;
395 395
396 if (sdata->vif.type == NL80211_IFTYPE_STATION && 396 if (sdata->vif.type == NL80211_IFTYPE_STATION &&
397 sdata->u.mgd.flags & (IEEE80211_STA_BEACON_POLL | 397 sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
398 IEEE80211_STA_CONNECTION_POLL))
399 return false; 398 return false;
400 399
401 return true; 400 return true;
@@ -1089,6 +1088,6 @@ void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
1089 1088
1090 trace_api_sched_scan_stopped(local); 1089 trace_api_sched_scan_stopped(local);
1091 1090
1092 ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work); 1091 schedule_work(&local->sched_scan_stopped_work);
1093} 1092}
1094EXPORT_SYMBOL(ieee80211_sched_scan_stopped); 1093EXPORT_SYMBOL(ieee80211_sched_scan_stopped);
diff --git a/net/mac80211/spectmgmt.c b/net/mac80211/spectmgmt.c
index 578eea3fc04d..6ab009070084 100644
--- a/net/mac80211/spectmgmt.c
+++ b/net/mac80211/spectmgmt.c
@@ -21,6 +21,177 @@
21#include "sta_info.h" 21#include "sta_info.h"
22#include "wme.h" 22#include "wme.h"
23 23
24int ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data *sdata,
25 struct ieee802_11_elems *elems, bool beacon,
26 enum ieee80211_band current_band,
27 u32 sta_flags, u8 *bssid,
28 struct ieee80211_csa_ie *csa_ie)
29{
30 enum ieee80211_band new_band;
31 int new_freq;
32 u8 new_chan_no;
33 struct ieee80211_channel *new_chan;
34 struct cfg80211_chan_def new_vht_chandef = {};
35 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
36 const struct ieee80211_wide_bw_chansw_ie *wide_bw_chansw_ie;
37 const struct ieee80211_ht_operation *ht_oper;
38 int secondary_channel_offset = -1;
39
40 sec_chan_offs = elems->sec_chan_offs;
41 wide_bw_chansw_ie = elems->wide_bw_chansw_ie;
42 ht_oper = elems->ht_operation;
43
44 if (sta_flags & (IEEE80211_STA_DISABLE_HT |
45 IEEE80211_STA_DISABLE_40MHZ)) {
46 sec_chan_offs = NULL;
47 wide_bw_chansw_ie = NULL;
48 /* only used for bandwidth here */
49 ht_oper = NULL;
50 }
51
52 if (sta_flags & IEEE80211_STA_DISABLE_VHT)
53 wide_bw_chansw_ie = NULL;
54
55 if (elems->ext_chansw_ie) {
56 if (!ieee80211_operating_class_to_band(
57 elems->ext_chansw_ie->new_operating_class,
58 &new_band)) {
59 sdata_info(sdata,
60 "cannot understand ECSA IE operating class %d, disconnecting\n",
61 elems->ext_chansw_ie->new_operating_class);
62 return -EINVAL;
63 }
64 new_chan_no = elems->ext_chansw_ie->new_ch_num;
65 csa_ie->count = elems->ext_chansw_ie->count;
66 csa_ie->mode = elems->ext_chansw_ie->mode;
67 } else if (elems->ch_switch_ie) {
68 new_band = current_band;
69 new_chan_no = elems->ch_switch_ie->new_ch_num;
70 csa_ie->count = elems->ch_switch_ie->count;
71 csa_ie->mode = elems->ch_switch_ie->mode;
72 } else {
73 /* nothing here we understand */
74 return 1;
75 }
76
77 /* Mesh Channel Switch Parameters Element */
78 if (elems->mesh_chansw_params_ie) {
79 csa_ie->ttl = elems->mesh_chansw_params_ie->mesh_ttl;
80 csa_ie->mode = elems->mesh_chansw_params_ie->mesh_flags;
81 csa_ie->pre_value = le16_to_cpu(
82 elems->mesh_chansw_params_ie->mesh_pre_value);
83 }
84
85 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
86 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
87 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
88 sdata_info(sdata,
89 "BSS %pM switches to unsupported channel (%d MHz), disconnecting\n",
90 bssid, new_freq);
91 return -EINVAL;
92 }
93
94 if (!beacon && sec_chan_offs) {
95 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
96 } else if (beacon && ht_oper) {
97 secondary_channel_offset =
98 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
99 } else if (!(sta_flags & IEEE80211_STA_DISABLE_HT)) {
100 /* If it's not a beacon, HT is enabled and the IE not present,
101 * it's 20 MHz, 802.11-2012 8.5.2.6:
102 * This element [the Secondary Channel Offset Element] is
103 * present when switching to a 40 MHz channel. It may be
104 * present when switching to a 20 MHz channel (in which
105 * case the secondary channel offset is set to SCN).
106 */
107 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
108 }
109
110 switch (secondary_channel_offset) {
111 default:
112 /* secondary_channel_offset was present but is invalid */
113 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
114 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
115 NL80211_CHAN_HT20);
116 break;
117 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
118 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
119 NL80211_CHAN_HT40PLUS);
120 break;
121 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
122 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
123 NL80211_CHAN_HT40MINUS);
124 break;
125 case -1:
126 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
127 NL80211_CHAN_NO_HT);
128 /* keep width for 5/10 MHz channels */
129 switch (sdata->vif.bss_conf.chandef.width) {
130 case NL80211_CHAN_WIDTH_5:
131 case NL80211_CHAN_WIDTH_10:
132 csa_ie->chandef.width =
133 sdata->vif.bss_conf.chandef.width;
134 break;
135 default:
136 break;
137 }
138 break;
139 }
140
141 if (wide_bw_chansw_ie) {
142 new_vht_chandef.chan = new_chan;
143 new_vht_chandef.center_freq1 =
144 ieee80211_channel_to_frequency(
145 wide_bw_chansw_ie->new_center_freq_seg0,
146 new_band);
147
148 switch (wide_bw_chansw_ie->new_channel_width) {
149 default:
150 /* hmmm, ignore VHT and use HT if present */
151 case IEEE80211_VHT_CHANWIDTH_USE_HT:
152 new_vht_chandef.chan = NULL;
153 break;
154 case IEEE80211_VHT_CHANWIDTH_80MHZ:
155 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
156 break;
157 case IEEE80211_VHT_CHANWIDTH_160MHZ:
158 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
159 break;
160 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
161 /* field is otherwise reserved */
162 new_vht_chandef.center_freq2 =
163 ieee80211_channel_to_frequency(
164 wide_bw_chansw_ie->new_center_freq_seg1,
165 new_band);
166 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
167 break;
168 }
169 if (sta_flags & IEEE80211_STA_DISABLE_80P80MHZ &&
170 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
171 ieee80211_chandef_downgrade(&new_vht_chandef);
172 if (sta_flags & IEEE80211_STA_DISABLE_160MHZ &&
173 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
174 ieee80211_chandef_downgrade(&new_vht_chandef);
175 if (sta_flags & IEEE80211_STA_DISABLE_40MHZ &&
176 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
177 ieee80211_chandef_downgrade(&new_vht_chandef);
178 }
179
180 /* if VHT data is there validate & use it */
181 if (new_vht_chandef.chan) {
182 if (!cfg80211_chandef_compatible(&new_vht_chandef,
183 &csa_ie->chandef)) {
184 sdata_info(sdata,
185 "BSS %pM: CSA has inconsistent channel data, disconnecting\n",
186 bssid);
187 return -EINVAL;
188 }
189 csa_ie->chandef = new_vht_chandef;
190 }
191
192 return 0;
193}
194
24static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata, 195static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
25 struct ieee80211_msrment_ie *request_ie, 196 struct ieee80211_msrment_ie *request_ie,
26 const u8 *da, const u8 *bssid, 197 const u8 *da, const u8 *bssid,
diff --git a/net/mac80211/sta_info.c b/net/mac80211/sta_info.c
index aeb967a0aeed..1eb66e26e49d 100644
--- a/net/mac80211/sta_info.c
+++ b/net/mac80211/sta_info.c
@@ -385,6 +385,30 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
385 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX); 385 sta->last_seq_ctrl[i] = cpu_to_le16(USHRT_MAX);
386 386
387 sta->sta.smps_mode = IEEE80211_SMPS_OFF; 387 sta->sta.smps_mode = IEEE80211_SMPS_OFF;
388 if (sdata->vif.type == NL80211_IFTYPE_AP ||
389 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
390 struct ieee80211_supported_band *sband =
391 local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)];
392 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
393 IEEE80211_HT_CAP_SM_PS_SHIFT;
394 /*
395 * Assume that hostapd advertises our caps in the beacon and
396 * this is the known_smps_mode for a station that just assciated
397 */
398 switch (smps) {
399 case WLAN_HT_SMPS_CONTROL_DISABLED:
400 sta->known_smps_mode = IEEE80211_SMPS_OFF;
401 break;
402 case WLAN_HT_SMPS_CONTROL_STATIC:
403 sta->known_smps_mode = IEEE80211_SMPS_STATIC;
404 break;
405 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
406 sta->known_smps_mode = IEEE80211_SMPS_DYNAMIC;
407 break;
408 default:
409 WARN_ON(1);
410 }
411 }
388 412
389 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 413 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
390 414
@@ -1069,6 +1093,19 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1069 1093
1070 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta); 1094 ieee80211_add_pending_skbs_fn(local, &pending, clear_sta_ps_flags, sta);
1071 1095
1096 /* This station just woke up and isn't aware of our SMPS state */
1097 if (!ieee80211_smps_is_restrictive(sta->known_smps_mode,
1098 sdata->smps_mode) &&
1099 sta->known_smps_mode != sdata->bss->req_smps &&
1100 sta_info_tx_streams(sta) != 1) {
1101 ht_dbg(sdata,
1102 "%pM just woke up and MIMO capable - update SMPS\n",
1103 sta->sta.addr);
1104 ieee80211_send_smps_action(sdata, sdata->bss->req_smps,
1105 sta->sta.addr,
1106 sdata->vif.bss_conf.bssid);
1107 }
1108
1072 local->total_ps_buffered -= buffered; 1109 local->total_ps_buffered -= buffered;
1073 1110
1074 sta_info_recalc_tim(sta); 1111 sta_info_recalc_tim(sta);
@@ -1520,3 +1557,38 @@ int sta_info_move_state(struct sta_info *sta,
1520 1557
1521 return 0; 1558 return 0;
1522} 1559}
1560
1561u8 sta_info_tx_streams(struct sta_info *sta)
1562{
1563 struct ieee80211_sta_ht_cap *ht_cap = &sta->sta.ht_cap;
1564 u8 rx_streams;
1565
1566 if (!sta->sta.ht_cap.ht_supported)
1567 return 1;
1568
1569 if (sta->sta.vht_cap.vht_supported) {
1570 int i;
1571 u16 tx_mcs_map =
1572 le16_to_cpu(sta->sta.vht_cap.vht_mcs.tx_mcs_map);
1573
1574 for (i = 7; i >= 0; i--)
1575 if ((tx_mcs_map & (0x3 << (i * 2))) !=
1576 IEEE80211_VHT_MCS_NOT_SUPPORTED)
1577 return i + 1;
1578 }
1579
1580 if (ht_cap->mcs.rx_mask[3])
1581 rx_streams = 4;
1582 else if (ht_cap->mcs.rx_mask[2])
1583 rx_streams = 3;
1584 else if (ht_cap->mcs.rx_mask[1])
1585 rx_streams = 2;
1586 else
1587 rx_streams = 1;
1588
1589 if (!(ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_RX_DIFF))
1590 return rx_streams;
1591
1592 return ((ht_cap->mcs.tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1593 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT) + 1;
1594}
diff --git a/net/mac80211/sta_info.h b/net/mac80211/sta_info.h
index 4208dbd5861f..3ef06a26b9cb 100644
--- a/net/mac80211/sta_info.h
+++ b/net/mac80211/sta_info.h
@@ -301,6 +301,8 @@ struct sta_ampdu_mlme {
301 * @chains: chains ever used for RX from this station 301 * @chains: chains ever used for RX from this station
302 * @chain_signal_last: last signal (per chain) 302 * @chain_signal_last: last signal (per chain)
303 * @chain_signal_avg: signal average (per chain) 303 * @chain_signal_avg: signal average (per chain)
304 * @known_smps_mode: the smps_mode the client thinks we are in. Relevant for
305 * AP only.
304 */ 306 */
305struct sta_info { 307struct sta_info {
306 /* General information, mostly static */ 308 /* General information, mostly static */
@@ -411,6 +413,8 @@ struct sta_info {
411 unsigned int lost_packets; 413 unsigned int lost_packets;
412 unsigned int beacon_loss_count; 414 unsigned int beacon_loss_count;
413 415
416 enum ieee80211_smps_mode known_smps_mode;
417
414 /* keep last! */ 418 /* keep last! */
415 struct ieee80211_sta sta; 419 struct ieee80211_sta sta;
416}; 420};
@@ -613,6 +617,7 @@ void sta_set_rate_info_rx(struct sta_info *sta,
613 struct rate_info *rinfo); 617 struct rate_info *rinfo);
614void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata, 618void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
615 unsigned long exp_time); 619 unsigned long exp_time);
620u8 sta_info_tx_streams(struct sta_info *sta);
616 621
617void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta); 622void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta);
618void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta); 623void ieee80211_sta_ps_deliver_poll_response(struct sta_info *sta);
diff --git a/net/mac80211/status.c b/net/mac80211/status.c
index 78dc2e99027e..52a152b01b06 100644
--- a/net/mac80211/status.c
+++ b/net/mac80211/status.c
@@ -194,29 +194,36 @@ static void ieee80211_frame_acked(struct sta_info *sta, struct sk_buff *skb)
194 if (ieee80211_is_action(mgmt->frame_control) && 194 if (ieee80211_is_action(mgmt->frame_control) &&
195 mgmt->u.action.category == WLAN_CATEGORY_HT && 195 mgmt->u.action.category == WLAN_CATEGORY_HT &&
196 mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS && 196 mgmt->u.action.u.ht_smps.action == WLAN_HT_ACTION_SMPS &&
197 sdata->vif.type == NL80211_IFTYPE_STATION &&
198 ieee80211_sdata_running(sdata)) { 197 ieee80211_sdata_running(sdata)) {
199 /* 198 enum ieee80211_smps_mode smps_mode;
200 * This update looks racy, but isn't -- if we come 199
201 * here we've definitely got a station that we're
202 * talking to, and on a managed interface that can
203 * only be the AP. And the only other place updating
204 * this variable in managed mode is before association.
205 */
206 switch (mgmt->u.action.u.ht_smps.smps_control) { 200 switch (mgmt->u.action.u.ht_smps.smps_control) {
207 case WLAN_HT_SMPS_CONTROL_DYNAMIC: 201 case WLAN_HT_SMPS_CONTROL_DYNAMIC:
208 sdata->smps_mode = IEEE80211_SMPS_DYNAMIC; 202 smps_mode = IEEE80211_SMPS_DYNAMIC;
209 break; 203 break;
210 case WLAN_HT_SMPS_CONTROL_STATIC: 204 case WLAN_HT_SMPS_CONTROL_STATIC:
211 sdata->smps_mode = IEEE80211_SMPS_STATIC; 205 smps_mode = IEEE80211_SMPS_STATIC;
212 break; 206 break;
213 case WLAN_HT_SMPS_CONTROL_DISABLED: 207 case WLAN_HT_SMPS_CONTROL_DISABLED:
214 default: /* shouldn't happen since we don't send that */ 208 default: /* shouldn't happen since we don't send that */
215 sdata->smps_mode = IEEE80211_SMPS_OFF; 209 smps_mode = IEEE80211_SMPS_OFF;
216 break; 210 break;
217 } 211 }
218 212
219 ieee80211_queue_work(&local->hw, &sdata->recalc_smps); 213 if (sdata->vif.type == NL80211_IFTYPE_STATION) {
214 /*
215 * This update looks racy, but isn't -- if we come
216 * here we've definitely got a station that we're
217 * talking to, and on a managed interface that can
218 * only be the AP. And the only other place updating
219 * this variable in managed mode is before association.
220 */
221 sdata->smps_mode = smps_mode;
222 ieee80211_queue_work(&local->hw, &sdata->recalc_smps);
223 } else if (sdata->vif.type == NL80211_IFTYPE_AP ||
224 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
225 sta->known_smps_mode = smps_mode;
226 }
220 } 227 }
221} 228}
222 229
diff --git a/net/mac80211/trace.h b/net/mac80211/trace.h
index 1aba645882bd..d4cee98533fd 100644
--- a/net/mac80211/trace.h
+++ b/net/mac80211/trace.h
@@ -77,13 +77,13 @@ DECLARE_EVENT_CLASS(local_sdata_addr_evt,
77 TP_STRUCT__entry( 77 TP_STRUCT__entry(
78 LOCAL_ENTRY 78 LOCAL_ENTRY
79 VIF_ENTRY 79 VIF_ENTRY
80 __array(char, addr, 6) 80 __array(char, addr, ETH_ALEN)
81 ), 81 ),
82 82
83 TP_fast_assign( 83 TP_fast_assign(
84 LOCAL_ASSIGN; 84 LOCAL_ASSIGN;
85 VIF_ASSIGN; 85 VIF_ASSIGN;
86 memcpy(__entry->addr, sdata->vif.addr, 6); 86 memcpy(__entry->addr, sdata->vif.addr, ETH_ALEN);
87 ), 87 ),
88 88
89 TP_printk( 89 TP_printk(
@@ -1475,6 +1475,41 @@ DEFINE_EVENT(local_sdata_evt, drv_ipv6_addr_change,
1475); 1475);
1476#endif 1476#endif
1477 1477
1478TRACE_EVENT(drv_join_ibss,
1479 TP_PROTO(struct ieee80211_local *local,
1480 struct ieee80211_sub_if_data *sdata,
1481 struct ieee80211_bss_conf *info),
1482
1483 TP_ARGS(local, sdata, info),
1484
1485 TP_STRUCT__entry(
1486 LOCAL_ENTRY
1487 VIF_ENTRY
1488 __field(u8, dtimper)
1489 __field(u16, bcnint)
1490 __dynamic_array(u8, ssid, info->ssid_len);
1491 ),
1492
1493 TP_fast_assign(
1494 LOCAL_ASSIGN;
1495 VIF_ASSIGN;
1496 __entry->dtimper = info->dtim_period;
1497 __entry->bcnint = info->beacon_int;
1498 memcpy(__get_dynamic_array(ssid), info->ssid, info->ssid_len);
1499 ),
1500
1501 TP_printk(
1502 LOCAL_PR_FMT VIF_PR_FMT,
1503 LOCAL_PR_ARG, VIF_PR_ARG
1504 )
1505);
1506
1507DEFINE_EVENT(local_sdata_evt, drv_leave_ibss,
1508 TP_PROTO(struct ieee80211_local *local,
1509 struct ieee80211_sub_if_data *sdata),
1510 TP_ARGS(local, sdata)
1511);
1512
1478/* 1513/*
1479 * Tracing for API calls that drivers call. 1514 * Tracing for API calls that drivers call.
1480 */ 1515 */
diff --git a/net/mac80211/tx.c b/net/mac80211/tx.c
index 70b5a05c0a4e..ca7fa7f0613d 100644
--- a/net/mac80211/tx.c
+++ b/net/mac80211/tx.c
@@ -463,7 +463,6 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
463{ 463{
464 struct sta_info *sta = tx->sta; 464 struct sta_info *sta = tx->sta;
465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 465 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
466 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
467 struct ieee80211_local *local = tx->local; 466 struct ieee80211_local *local = tx->local;
468 467
469 if (unlikely(!sta)) 468 if (unlikely(!sta))
@@ -474,15 +473,6 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
474 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) { 473 !(info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER))) {
475 int ac = skb_get_queue_mapping(tx->skb); 474 int ac = skb_get_queue_mapping(tx->skb);
476 475
477 /* only deauth, disassoc and action are bufferable MMPDUs */
478 if (ieee80211_is_mgmt(hdr->frame_control) &&
479 !ieee80211_is_deauth(hdr->frame_control) &&
480 !ieee80211_is_disassoc(hdr->frame_control) &&
481 !ieee80211_is_action(hdr->frame_control)) {
482 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
483 return TX_CONTINUE;
484 }
485
486 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n", 476 ps_dbg(sta->sdata, "STA %pM aid %d: PS buffer for AC %d\n",
487 sta->sta.addr, sta->sta.aid, ac); 477 sta->sta.addr, sta->sta.aid, ac);
488 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER) 478 if (tx->local->total_ps_buffered >= TOTAL_MAX_TX_BUFFER)
@@ -525,9 +515,22 @@ ieee80211_tx_h_unicast_ps_buf(struct ieee80211_tx_data *tx)
525static ieee80211_tx_result debug_noinline 515static ieee80211_tx_result debug_noinline
526ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx) 516ieee80211_tx_h_ps_buf(struct ieee80211_tx_data *tx)
527{ 517{
518 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
519 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
520
528 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED)) 521 if (unlikely(tx->flags & IEEE80211_TX_PS_BUFFERED))
529 return TX_CONTINUE; 522 return TX_CONTINUE;
530 523
524 /* only deauth, disassoc and action are bufferable MMPDUs */
525 if (ieee80211_is_mgmt(hdr->frame_control) &&
526 !ieee80211_is_deauth(hdr->frame_control) &&
527 !ieee80211_is_disassoc(hdr->frame_control) &&
528 !ieee80211_is_action(hdr->frame_control)) {
529 if (tx->flags & IEEE80211_TX_UNICAST)
530 info->flags |= IEEE80211_TX_CTL_NO_PS_BUFFER;
531 return TX_CONTINUE;
532 }
533
531 if (tx->flags & IEEE80211_TX_UNICAST) 534 if (tx->flags & IEEE80211_TX_UNICAST)
532 return ieee80211_tx_h_unicast_ps_buf(tx); 535 return ieee80211_tx_h_unicast_ps_buf(tx);
533 else 536 else
@@ -1367,6 +1370,35 @@ static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1367 return 0; 1370 return 0;
1368} 1371}
1369 1372
1373bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1374 struct ieee80211_vif *vif, struct sk_buff *skb,
1375 int band, struct ieee80211_sta **sta)
1376{
1377 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1378 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1379 struct ieee80211_tx_data tx;
1380
1381 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1382 return false;
1383
1384 info->band = band;
1385 info->control.vif = vif;
1386 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1387
1388 if (invoke_tx_handlers(&tx))
1389 return false;
1390
1391 if (sta) {
1392 if (tx.sta)
1393 *sta = &tx.sta->sta;
1394 else
1395 *sta = NULL;
1396 }
1397
1398 return true;
1399}
1400EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1401
1370/* 1402/*
1371 * Returns false if the frame couldn't be transmitted but was queued instead. 1403 * Returns false if the frame couldn't be transmitted but was queued instead.
1372 */ 1404 */
@@ -1982,7 +2014,7 @@ netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1982 * EAPOL frames from the local station. 2014 * EAPOL frames from the local station.
1983 */ 2015 */
1984 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) && 2016 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
1985 !is_multicast_ether_addr(hdr.addr1) && !authorized && 2017 !multicast && !authorized &&
1986 (cpu_to_be16(ethertype) != sdata->control_port_protocol || 2018 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
1987 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) { 2019 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
1988#ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2020#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
@@ -2358,15 +2390,35 @@ static void ieee80211_update_csa(struct ieee80211_sub_if_data *sdata,
2358 struct probe_resp *resp; 2390 struct probe_resp *resp;
2359 int counter_offset_beacon = sdata->csa_counter_offset_beacon; 2391 int counter_offset_beacon = sdata->csa_counter_offset_beacon;
2360 int counter_offset_presp = sdata->csa_counter_offset_presp; 2392 int counter_offset_presp = sdata->csa_counter_offset_presp;
2393 u8 *beacon_data;
2394 size_t beacon_data_len;
2395
2396 switch (sdata->vif.type) {
2397 case NL80211_IFTYPE_AP:
2398 beacon_data = beacon->tail;
2399 beacon_data_len = beacon->tail_len;
2400 break;
2401 case NL80211_IFTYPE_ADHOC:
2402 beacon_data = beacon->head;
2403 beacon_data_len = beacon->head_len;
2404 break;
2405 case NL80211_IFTYPE_MESH_POINT:
2406 beacon_data = beacon->head;
2407 beacon_data_len = beacon->head_len;
2408 break;
2409 default:
2410 return;
2411 }
2412 if (WARN_ON(counter_offset_beacon >= beacon_data_len))
2413 return;
2361 2414
2362 /* warn if the driver did not check for/react to csa completeness */ 2415 /* warn if the driver did not check for/react to csa completeness */
2363 if (WARN_ON(((u8 *)beacon->tail)[counter_offset_beacon] == 0)) 2416 if (WARN_ON(beacon_data[counter_offset_beacon] == 0))
2364 return; 2417 return;
2365 2418
2366 ((u8 *)beacon->tail)[counter_offset_beacon]--; 2419 beacon_data[counter_offset_beacon]--;
2367 2420
2368 if (sdata->vif.type == NL80211_IFTYPE_AP && 2421 if (sdata->vif.type == NL80211_IFTYPE_AP && counter_offset_presp) {
2369 counter_offset_presp) {
2370 rcu_read_lock(); 2422 rcu_read_lock();
2371 resp = rcu_dereference(sdata->u.ap.probe_resp); 2423 resp = rcu_dereference(sdata->u.ap.probe_resp);
2372 2424
@@ -2401,6 +2453,24 @@ bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2401 goto out; 2453 goto out;
2402 beacon_data = beacon->tail; 2454 beacon_data = beacon->tail;
2403 beacon_data_len = beacon->tail_len; 2455 beacon_data_len = beacon->tail_len;
2456 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2457 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2458
2459 beacon = rcu_dereference(ifibss->presp);
2460 if (!beacon)
2461 goto out;
2462
2463 beacon_data = beacon->head;
2464 beacon_data_len = beacon->head_len;
2465 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2466 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2467
2468 beacon = rcu_dereference(ifmsh->beacon);
2469 if (!beacon)
2470 goto out;
2471
2472 beacon_data = beacon->head;
2473 beacon_data_len = beacon->head_len;
2404 } else { 2474 } else {
2405 WARN_ON(1); 2475 WARN_ON(1);
2406 goto out; 2476 goto out;
@@ -2485,6 +2555,10 @@ struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2485 if (!presp) 2555 if (!presp)
2486 goto out; 2556 goto out;
2487 2557
2558 if (sdata->vif.csa_active)
2559 ieee80211_update_csa(sdata, presp);
2560
2561
2488 skb = dev_alloc_skb(local->tx_headroom + presp->head_len); 2562 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
2489 if (!skb) 2563 if (!skb)
2490 goto out; 2564 goto out;
@@ -2502,6 +2576,9 @@ struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2502 if (!bcn) 2576 if (!bcn)
2503 goto out; 2577 goto out;
2504 2578
2579 if (sdata->vif.csa_active)
2580 ieee80211_update_csa(sdata, bcn);
2581
2505 if (ifmsh->sync_ops) 2582 if (ifmsh->sync_ops)
2506 ifmsh->sync_ops->adjust_tbtt( 2583 ifmsh->sync_ops->adjust_tbtt(
2507 sdata); 2584 sdata);
diff --git a/net/mac80211/util.c b/net/mac80211/util.c
index 69e4ef5348a0..9f9b9bd3fd44 100644
--- a/net/mac80211/util.c
+++ b/net/mac80211/util.c
@@ -300,9 +300,6 @@ void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
300 if (!sdata->dev) 300 if (!sdata->dev)
301 continue; 301 continue;
302 302
303 if (test_bit(SDATA_STATE_OFFCHANNEL, &sdata->state))
304 continue;
305
306 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE && 303 if (sdata->vif.cab_queue != IEEE80211_INVAL_HW_QUEUE &&
307 local->queue_stop_reasons[sdata->vif.cab_queue] != 0) 304 local->queue_stop_reasons[sdata->vif.cab_queue] != 0)
308 continue; 305 continue;
@@ -567,18 +564,15 @@ void ieee80211_flush_queues(struct ieee80211_local *local,
567 IEEE80211_QUEUE_STOP_REASON_FLUSH); 564 IEEE80211_QUEUE_STOP_REASON_FLUSH);
568} 565}
569 566
570void ieee80211_iterate_active_interfaces( 567static void __iterate_active_interfaces(struct ieee80211_local *local,
571 struct ieee80211_hw *hw, u32 iter_flags, 568 u32 iter_flags,
572 void (*iterator)(void *data, u8 *mac, 569 void (*iterator)(void *data, u8 *mac,
573 struct ieee80211_vif *vif), 570 struct ieee80211_vif *vif),
574 void *data) 571 void *data)
575{ 572{
576 struct ieee80211_local *local = hw_to_local(hw);
577 struct ieee80211_sub_if_data *sdata; 573 struct ieee80211_sub_if_data *sdata;
578 574
579 mutex_lock(&local->iflist_mtx); 575 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
580
581 list_for_each_entry(sdata, &local->interfaces, list) {
582 switch (sdata->vif.type) { 576 switch (sdata->vif.type) {
583 case NL80211_IFTYPE_MONITOR: 577 case NL80211_IFTYPE_MONITOR:
584 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE)) 578 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE))
@@ -597,13 +591,25 @@ void ieee80211_iterate_active_interfaces(
597 &sdata->vif); 591 &sdata->vif);
598 } 592 }
599 593
600 sdata = rcu_dereference_protected(local->monitor_sdata, 594 sdata = rcu_dereference_check(local->monitor_sdata,
601 lockdep_is_held(&local->iflist_mtx)); 595 lockdep_is_held(&local->iflist_mtx) ||
596 lockdep_rtnl_is_held());
602 if (sdata && 597 if (sdata &&
603 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || 598 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
604 sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 599 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
605 iterator(data, sdata->vif.addr, &sdata->vif); 600 iterator(data, sdata->vif.addr, &sdata->vif);
601}
606 602
603void ieee80211_iterate_active_interfaces(
604 struct ieee80211_hw *hw, u32 iter_flags,
605 void (*iterator)(void *data, u8 *mac,
606 struct ieee80211_vif *vif),
607 void *data)
608{
609 struct ieee80211_local *local = hw_to_local(hw);
610
611 mutex_lock(&local->iflist_mtx);
612 __iterate_active_interfaces(local, iter_flags, iterator, data);
607 mutex_unlock(&local->iflist_mtx); 613 mutex_unlock(&local->iflist_mtx);
608} 614}
609EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces); 615EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces);
@@ -615,38 +621,26 @@ void ieee80211_iterate_active_interfaces_atomic(
615 void *data) 621 void *data)
616{ 622{
617 struct ieee80211_local *local = hw_to_local(hw); 623 struct ieee80211_local *local = hw_to_local(hw);
618 struct ieee80211_sub_if_data *sdata;
619 624
620 rcu_read_lock(); 625 rcu_read_lock();
626 __iterate_active_interfaces(local, iter_flags, iterator, data);
627 rcu_read_unlock();
628}
629EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
621 630
622 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 631void ieee80211_iterate_active_interfaces_rtnl(
623 switch (sdata->vif.type) { 632 struct ieee80211_hw *hw, u32 iter_flags,
624 case NL80211_IFTYPE_MONITOR: 633 void (*iterator)(void *data, u8 *mac,
625 if (!(sdata->u.mntr_flags & MONITOR_FLAG_ACTIVE)) 634 struct ieee80211_vif *vif),
626 continue; 635 void *data)
627 break; 636{
628 case NL80211_IFTYPE_AP_VLAN: 637 struct ieee80211_local *local = hw_to_local(hw);
629 continue;
630 default:
631 break;
632 }
633 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
634 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
635 continue;
636 if (ieee80211_sdata_running(sdata))
637 iterator(data, sdata->vif.addr,
638 &sdata->vif);
639 }
640 638
641 sdata = rcu_dereference(local->monitor_sdata); 639 ASSERT_RTNL();
642 if (sdata &&
643 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL ||
644 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
645 iterator(data, sdata->vif.addr, &sdata->vif);
646 640
647 rcu_read_unlock(); 641 __iterate_active_interfaces(local, iter_flags, iterator, data);
648} 642}
649EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic); 643EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
650 644
651/* 645/*
652 * Nothing should have been stuffed into the workqueue during 646 * Nothing should have been stuffed into the workqueue during
@@ -746,6 +740,7 @@ u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
746 case WLAN_EID_TIMEOUT_INTERVAL: 740 case WLAN_EID_TIMEOUT_INTERVAL:
747 case WLAN_EID_SECONDARY_CHANNEL_OFFSET: 741 case WLAN_EID_SECONDARY_CHANNEL_OFFSET:
748 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH: 742 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
743 case WLAN_EID_CHAN_SWITCH_PARAM:
749 /* 744 /*
750 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible 745 * not listing WLAN_EID_CHANNEL_SWITCH_WRAPPER -- it seems possible
751 * that if the content gets bigger it might be needed more than once 746 * that if the content gets bigger it might be needed more than once
@@ -911,6 +906,14 @@ u32 ieee802_11_parse_elems_crc(const u8 *start, size_t len, bool action,
911 } 906 }
912 elems->sec_chan_offs = (void *)pos; 907 elems->sec_chan_offs = (void *)pos;
913 break; 908 break;
909 case WLAN_EID_CHAN_SWITCH_PARAM:
910 if (elen !=
911 sizeof(*elems->mesh_chansw_params_ie)) {
912 elem_parse_failed = true;
913 break;
914 }
915 elems->mesh_chansw_params_ie = (void *)pos;
916 break;
914 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH: 917 case WLAN_EID_WIDE_BW_CHANNEL_SWITCH:
915 if (!action || 918 if (!action ||
916 elen != sizeof(*elems->wide_bw_chansw_ie)) { 919 elen != sizeof(*elems->wide_bw_chansw_ie)) {
@@ -1007,14 +1010,21 @@ void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1007 */ 1010 */
1008 enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION); 1011 enable_qos = (sdata->vif.type != NL80211_IFTYPE_STATION);
1009 1012
1010 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 1013 /* Set defaults according to 802.11-2007 Table 7-37 */
1011 /* Set defaults according to 802.11-2007 Table 7-37 */ 1014 aCWmax = 1023;
1012 aCWmax = 1023; 1015 if (use_11b)
1013 if (use_11b) 1016 aCWmin = 31;
1014 aCWmin = 31; 1017 else
1015 else 1018 aCWmin = 15;
1016 aCWmin = 15;
1017 1019
1020 /* Confiure old 802.11b/g medium access rules. */
1021 qparam.cw_max = aCWmax;
1022 qparam.cw_min = aCWmin;
1023 qparam.txop = 0;
1024 qparam.aifs = 2;
1025
1026 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
1027 /* Update if QoS is enabled. */
1018 if (enable_qos) { 1028 if (enable_qos) {
1019 switch (ac) { 1029 switch (ac) {
1020 case IEEE80211_AC_BK: 1030 case IEEE80211_AC_BK:
@@ -1050,12 +1060,6 @@ void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1050 qparam.aifs = 2; 1060 qparam.aifs = 2;
1051 break; 1061 break;
1052 } 1062 }
1053 } else {
1054 /* Confiure old 802.11b/g medium access rules. */
1055 qparam.cw_max = aCWmax;
1056 qparam.cw_min = aCWmin;
1057 qparam.txop = 0;
1058 qparam.aifs = 2;
1059 } 1063 }
1060 1064
1061 qparam.uapsd = false; 1065 qparam.uapsd = false;
@@ -1084,8 +1088,8 @@ void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1084 struct ieee80211_mgmt *mgmt; 1088 struct ieee80211_mgmt *mgmt;
1085 int err; 1089 int err;
1086 1090
1087 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 1091 /* 24 + 6 = header + auth_algo + auth_transaction + status_code */
1088 sizeof(*mgmt) + 6 + extra_len); 1092 skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24 + 6 + extra_len);
1089 if (!skb) 1093 if (!skb)
1090 return; 1094 return;
1091 1095
@@ -2274,17 +2278,15 @@ void ieee80211_dfs_radar_detected_work(struct work_struct *work)
2274{ 2278{
2275 struct ieee80211_local *local = 2279 struct ieee80211_local *local =
2276 container_of(work, struct ieee80211_local, radar_detected_work); 2280 container_of(work, struct ieee80211_local, radar_detected_work);
2277 struct cfg80211_chan_def chandef; 2281 struct cfg80211_chan_def chandef = local->hw.conf.chandef;
2278 2282
2279 ieee80211_dfs_cac_cancel(local); 2283 ieee80211_dfs_cac_cancel(local);
2280 2284
2281 if (local->use_chanctx) 2285 if (local->use_chanctx)
2282 /* currently not handled */ 2286 /* currently not handled */
2283 WARN_ON(1); 2287 WARN_ON(1);
2284 else { 2288 else
2285 chandef = local->hw.conf.chandef;
2286 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL); 2289 cfg80211_radar_event(local->hw.wiphy, &chandef, GFP_KERNEL);
2287 }
2288} 2290}
2289 2291
2290void ieee80211_radar_detected(struct ieee80211_hw *hw) 2292void ieee80211_radar_detected(struct ieee80211_hw *hw)
@@ -2296,3 +2298,170 @@ void ieee80211_radar_detected(struct ieee80211_hw *hw)
2296 ieee80211_queue_work(hw, &local->radar_detected_work); 2298 ieee80211_queue_work(hw, &local->radar_detected_work);
2297} 2299}
2298EXPORT_SYMBOL(ieee80211_radar_detected); 2300EXPORT_SYMBOL(ieee80211_radar_detected);
2301
2302u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2303{
2304 u32 ret;
2305 int tmp;
2306
2307 switch (c->width) {
2308 case NL80211_CHAN_WIDTH_20:
2309 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2310 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2311 break;
2312 case NL80211_CHAN_WIDTH_40:
2313 c->width = NL80211_CHAN_WIDTH_20;
2314 c->center_freq1 = c->chan->center_freq;
2315 ret = IEEE80211_STA_DISABLE_40MHZ |
2316 IEEE80211_STA_DISABLE_VHT;
2317 break;
2318 case NL80211_CHAN_WIDTH_80:
2319 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2320 /* n_P40 */
2321 tmp /= 2;
2322 /* freq_P40 */
2323 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2324 c->width = NL80211_CHAN_WIDTH_40;
2325 ret = IEEE80211_STA_DISABLE_VHT;
2326 break;
2327 case NL80211_CHAN_WIDTH_80P80:
2328 c->center_freq2 = 0;
2329 c->width = NL80211_CHAN_WIDTH_80;
2330 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2331 IEEE80211_STA_DISABLE_160MHZ;
2332 break;
2333 case NL80211_CHAN_WIDTH_160:
2334 /* n_P20 */
2335 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2336 /* n_P80 */
2337 tmp /= 4;
2338 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2339 c->width = NL80211_CHAN_WIDTH_80;
2340 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2341 IEEE80211_STA_DISABLE_160MHZ;
2342 break;
2343 default:
2344 case NL80211_CHAN_WIDTH_20_NOHT:
2345 WARN_ON_ONCE(1);
2346 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2347 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2348 break;
2349 case NL80211_CHAN_WIDTH_5:
2350 case NL80211_CHAN_WIDTH_10:
2351 WARN_ON_ONCE(1);
2352 /* keep c->width */
2353 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2354 break;
2355 }
2356
2357 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2358
2359 return ret;
2360}
2361
2362/*
2363 * Returns true if smps_mode_new is strictly more restrictive than
2364 * smps_mode_old.
2365 */
2366bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2367 enum ieee80211_smps_mode smps_mode_new)
2368{
2369 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2370 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2371 return false;
2372
2373 switch (smps_mode_old) {
2374 case IEEE80211_SMPS_STATIC:
2375 return false;
2376 case IEEE80211_SMPS_DYNAMIC:
2377 return smps_mode_new == IEEE80211_SMPS_STATIC;
2378 case IEEE80211_SMPS_OFF:
2379 return smps_mode_new != IEEE80211_SMPS_OFF;
2380 default:
2381 WARN_ON(1);
2382 }
2383
2384 return false;
2385}
2386
2387int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2388 struct cfg80211_csa_settings *csa_settings)
2389{
2390 struct sk_buff *skb;
2391 struct ieee80211_mgmt *mgmt;
2392 struct ieee80211_local *local = sdata->local;
2393 int freq;
2394 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2395 sizeof(mgmt->u.action.u.chan_switch);
2396 u8 *pos;
2397
2398 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2399 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2400 return -EOPNOTSUPP;
2401
2402 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2403 5 + /* channel switch announcement element */
2404 3 + /* secondary channel offset element */
2405 8); /* mesh channel switch parameters element */
2406 if (!skb)
2407 return -ENOMEM;
2408
2409 skb_reserve(skb, local->tx_headroom);
2410 mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2411 memset(mgmt, 0, hdr_len);
2412 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2413 IEEE80211_STYPE_ACTION);
2414
2415 eth_broadcast_addr(mgmt->da);
2416 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2417 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2418 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2419 } else {
2420 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2421 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2422 }
2423 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2424 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2425 pos = skb_put(skb, 5);
2426 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
2427 *pos++ = 3; /* IE length */
2428 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
2429 freq = csa_settings->chandef.chan->center_freq;
2430 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
2431 *pos++ = csa_settings->count; /* count */
2432
2433 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2434 enum nl80211_channel_type ch_type;
2435
2436 skb_put(skb, 3);
2437 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
2438 *pos++ = 1; /* IE length */
2439 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2440 if (ch_type == NL80211_CHAN_HT40PLUS)
2441 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2442 else
2443 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2444 }
2445
2446 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2447 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2448 __le16 pre_value;
2449
2450 skb_put(skb, 8);
2451 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
2452 *pos++ = 6; /* IE length */
2453 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
2454 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
2455 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
2456 *pos++ |= csa_settings->block_tx ?
2457 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
2458 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
2459 pos += 2;
2460 pre_value = cpu_to_le16(ifmsh->pre_value);
2461 memcpy(pos, &pre_value, 2); /* Precedence Value */
2462 pos += 2;
2463 }
2464
2465 ieee80211_tx_skb(sdata, skb);
2466 return 0;
2467}
diff --git a/net/mac80211/vht.c b/net/mac80211/vht.c
index 97c289414e32..de0112785aae 100644
--- a/net/mac80211/vht.c
+++ b/net/mac80211/vht.c
@@ -185,13 +185,13 @@ ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
185 if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) { 185 if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE) {
186 vht_cap->cap |= cap_info & 186 vht_cap->cap |= cap_info &
187 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE | 187 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
188 IEEE80211_VHT_CAP_BEAMFORMER_ANTENNAS_MAX |
189 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MAX); 188 IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MAX);
190 } 189 }
191 190
192 if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE) 191 if (own_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
193 vht_cap->cap |= cap_info & 192 vht_cap->cap |= cap_info &
194 IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE; 193 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
194 IEEE80211_VHT_CAP_BEAMFORMEE_STS_MAX);
195 195
196 if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE) 196 if (own_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
197 vht_cap->cap |= cap_info & 197 vht_cap->cap |= cap_info &
diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c
index c9edfcb7a13b..d65728220763 100644
--- a/net/mac80211/wpa.c
+++ b/net/mac80211/wpa.c
@@ -301,22 +301,16 @@ ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
301} 301}
302 302
303 303
304static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch, 304static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
305 int encrypted) 305 int encrypted)
306{ 306{
307 __le16 mask_fc; 307 __le16 mask_fc;
308 int a4_included, mgmt; 308 int a4_included, mgmt;
309 u8 qos_tid; 309 u8 qos_tid;
310 u8 *b_0, *aad; 310 u16 len_a;
311 u16 data_len, len_a;
312 unsigned int hdrlen; 311 unsigned int hdrlen;
313 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 312 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
314 313
315 memset(scratch, 0, 6 * AES_BLOCK_SIZE);
316
317 b_0 = scratch + 3 * AES_BLOCK_SIZE;
318 aad = scratch + 4 * AES_BLOCK_SIZE;
319
320 /* 314 /*
321 * Mask FC: zero subtype b4 b5 b6 (if not mgmt) 315 * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
322 * Retry, PwrMgt, MoreData; set Protected 316 * Retry, PwrMgt, MoreData; set Protected
@@ -338,20 +332,21 @@ static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
338 else 332 else
339 qos_tid = 0; 333 qos_tid = 0;
340 334
341 data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN; 335 /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
342 if (encrypted) 336 * mode authentication are not allowed to collide, yet both are derived
343 data_len -= IEEE80211_CCMP_MIC_LEN; 337 * from this vector b_0. We only set L := 1 here to indicate that the
338 * data size can be represented in (L+1) bytes. The CCM layer will take
339 * care of storing the data length in the top (L+1) bytes and setting
340 * and clearing the other bits as is required to derive the two IVs.
341 */
342 b_0[0] = 0x1;
344 343
345 /* First block, b_0 */
346 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
347 /* Nonce: Nonce Flags | A2 | PN 344 /* Nonce: Nonce Flags | A2 | PN
348 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7) 345 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
349 */ 346 */
350 b_0[1] = qos_tid | (mgmt << 4); 347 b_0[1] = qos_tid | (mgmt << 4);
351 memcpy(&b_0[2], hdr->addr2, ETH_ALEN); 348 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
352 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN); 349 memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
353 /* l(m) */
354 put_unaligned_be16(data_len, &b_0[14]);
355 350
356 /* AAD (extra authenticate-only data) / masked 802.11 header 351 /* AAD (extra authenticate-only data) / masked 802.11 header
357 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */ 352 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
@@ -407,7 +402,8 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
407 u8 *pos; 402 u8 *pos;
408 u8 pn[6]; 403 u8 pn[6];
409 u64 pn64; 404 u64 pn64;
410 u8 scratch[6 * AES_BLOCK_SIZE]; 405 u8 aad[2 * AES_BLOCK_SIZE];
406 u8 b_0[AES_BLOCK_SIZE];
411 407
412 if (info->control.hw_key && 408 if (info->control.hw_key &&
413 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) && 409 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
@@ -460,9 +456,9 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
460 return 0; 456 return 0;
461 457
462 pos += IEEE80211_CCMP_HDR_LEN; 458 pos += IEEE80211_CCMP_HDR_LEN;
463 ccmp_special_blocks(skb, pn, scratch, 0); 459 ccmp_special_blocks(skb, pn, b_0, aad, 0);
464 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, pos, len, 460 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
465 pos, skb_put(skb, IEEE80211_CCMP_MIC_LEN)); 461 skb_put(skb, IEEE80211_CCMP_MIC_LEN));
466 462
467 return 0; 463 return 0;
468} 464}
@@ -525,16 +521,16 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
525 } 521 }
526 522
527 if (!(status->flag & RX_FLAG_DECRYPTED)) { 523 if (!(status->flag & RX_FLAG_DECRYPTED)) {
528 u8 scratch[6 * AES_BLOCK_SIZE]; 524 u8 aad[2 * AES_BLOCK_SIZE];
525 u8 b_0[AES_BLOCK_SIZE];
529 /* hardware didn't decrypt/verify MIC */ 526 /* hardware didn't decrypt/verify MIC */
530 ccmp_special_blocks(skb, pn, scratch, 1); 527 ccmp_special_blocks(skb, pn, b_0, aad, 1);
531 528
532 if (ieee80211_aes_ccm_decrypt( 529 if (ieee80211_aes_ccm_decrypt(
533 key->u.ccmp.tfm, scratch, 530 key->u.ccmp.tfm, b_0, aad,
534 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN, 531 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
535 data_len, 532 data_len,
536 skb->data + skb->len - IEEE80211_CCMP_MIC_LEN, 533 skb->data + skb->len - IEEE80211_CCMP_MIC_LEN))
537 skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN))
538 return RX_DROP_UNUSABLE; 534 return RX_DROP_UNUSABLE;
539 } 535 }
540 536
diff --git a/net/mac802154/ieee802154_dev.c b/net/mac802154/ieee802154_dev.c
index b7c7f815deae..52ae6646a411 100644
--- a/net/mac802154/ieee802154_dev.c
+++ b/net/mac802154/ieee802154_dev.c
@@ -174,8 +174,7 @@ ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
174 174
175 if (!ops || !ops->xmit || !ops->ed || !ops->start || 175 if (!ops || !ops->xmit || !ops->ed || !ops->start ||
176 !ops->stop || !ops->set_channel) { 176 !ops->stop || !ops->set_channel) {
177 printk(KERN_ERR 177 pr_err("undefined IEEE802.15.4 device operations\n");
178 "undefined IEEE802.15.4 device operations\n");
179 return NULL; 178 return NULL;
180 } 179 }
181 180
@@ -201,8 +200,7 @@ ieee802154_alloc_device(size_t priv_data_len, struct ieee802154_ops *ops)
201 200
202 phy = wpan_phy_alloc(priv_size); 201 phy = wpan_phy_alloc(priv_size);
203 if (!phy) { 202 if (!phy) {
204 printk(KERN_ERR 203 pr_err("failure to allocate master IEEE802.15.4 device\n");
205 "failure to allocate master IEEE802.15.4 device\n");
206 return NULL; 204 return NULL;
207 } 205 }
208 206
diff --git a/net/mac802154/wpan.c b/net/mac802154/wpan.c
index 2ca2f4dceab7..e24bcf977296 100644
--- a/net/mac802154/wpan.c
+++ b/net/mac802154/wpan.c
@@ -208,6 +208,8 @@ static int mac802154_header_create(struct sk_buff *skb,
208 head[1] = fc >> 8; 208 head[1] = fc >> 8;
209 209
210 memcpy(skb_push(skb, pos), head, pos); 210 memcpy(skb_push(skb, pos), head, pos);
211 skb_reset_mac_header(skb);
212 skb->mac_len = pos;
211 213
212 return pos; 214 return pos;
213} 215}
diff --git a/net/mpls/mpls_gso.c b/net/mpls/mpls_gso.c
index 1bec1219ab81..851cd880b0c0 100644
--- a/net/mpls/mpls_gso.c
+++ b/net/mpls/mpls_gso.c
@@ -33,6 +33,7 @@ static struct sk_buff *mpls_gso_segment(struct sk_buff *skb,
33 SKB_GSO_DODGY | 33 SKB_GSO_DODGY |
34 SKB_GSO_TCP_ECN | 34 SKB_GSO_TCP_ECN |
35 SKB_GSO_GRE | 35 SKB_GSO_GRE |
36 SKB_GSO_IPIP |
36 SKB_GSO_MPLS))) 37 SKB_GSO_MPLS)))
37 goto out; 38 goto out;
38 39
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index 6e839b6dff2b..c3398cd99b94 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -413,6 +413,58 @@ config NETFILTER_SYNPROXY
413 413
414endif # NF_CONNTRACK 414endif # NF_CONNTRACK
415 415
416config NF_TABLES
417 depends on NETFILTER_NETLINK
418 tristate "Netfilter nf_tables support"
419
420config NFT_EXTHDR
421 depends on NF_TABLES
422 tristate "Netfilter nf_tables IPv6 exthdr module"
423
424config NFT_META
425 depends on NF_TABLES
426 tristate "Netfilter nf_tables meta module"
427
428config NFT_CT
429 depends on NF_TABLES
430 depends on NF_CONNTRACK
431 tristate "Netfilter nf_tables conntrack module"
432
433config NFT_RBTREE
434 depends on NF_TABLES
435 tristate "Netfilter nf_tables rbtree set module"
436
437config NFT_HASH
438 depends on NF_TABLES
439 tristate "Netfilter nf_tables hash set module"
440
441config NFT_COUNTER
442 depends on NF_TABLES
443 tristate "Netfilter nf_tables counter module"
444
445config NFT_LOG
446 depends on NF_TABLES
447 tristate "Netfilter nf_tables log module"
448
449config NFT_LIMIT
450 depends on NF_TABLES
451 tristate "Netfilter nf_tables limit module"
452
453config NFT_NAT
454 depends on NF_TABLES
455 depends on NF_CONNTRACK
456 depends on NF_NAT
457 tristate "Netfilter nf_tables nat module"
458
459config NFT_COMPAT
460 depends on NF_TABLES
461 depends on NETFILTER_XTABLES
462 tristate "Netfilter x_tables over nf_tables module"
463 help
464 This is required if you intend to use any of existing
465 x_tables match/target extensions over the nf_tables
466 framework.
467
416config NETFILTER_XTABLES 468config NETFILTER_XTABLES
417 tristate "Netfilter Xtables support (required for ip_tables)" 469 tristate "Netfilter Xtables support (required for ip_tables)"
418 default m if NETFILTER_ADVANCED=n 470 default m if NETFILTER_ADVANCED=n
@@ -857,7 +909,7 @@ config NETFILTER_XT_MATCH_CONNLABEL
857 connection simultaneously. 909 connection simultaneously.
858 910
859config NETFILTER_XT_MATCH_CONNLIMIT 911config NETFILTER_XT_MATCH_CONNLIMIT
860 tristate '"connlimit" match support"' 912 tristate '"connlimit" match support'
861 depends on NF_CONNTRACK 913 depends on NF_CONNTRACK
862 depends on NETFILTER_ADVANCED 914 depends on NETFILTER_ADVANCED
863 ---help--- 915 ---help---
diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
index c3a0a12907f6..394483b2c193 100644
--- a/net/netfilter/Makefile
+++ b/net/netfilter/Makefile
@@ -64,6 +64,24 @@ obj-$(CONFIG_NF_NAT_TFTP) += nf_nat_tftp.o
64# SYNPROXY 64# SYNPROXY
65obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o 65obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o
66 66
67# nf_tables
68nf_tables-objs += nf_tables_core.o nf_tables_api.o
69nf_tables-objs += nft_immediate.o nft_cmp.o nft_lookup.o
70nf_tables-objs += nft_bitwise.o nft_byteorder.o nft_payload.o
71
72obj-$(CONFIG_NF_TABLES) += nf_tables.o
73obj-$(CONFIG_NFT_COMPAT) += nft_compat.o
74obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o
75obj-$(CONFIG_NFT_META) += nft_meta.o
76obj-$(CONFIG_NFT_CT) += nft_ct.o
77obj-$(CONFIG_NFT_LIMIT) += nft_limit.o
78obj-$(CONFIG_NFT_NAT) += nft_nat.o
79#nf_tables-objs += nft_meta_target.o
80obj-$(CONFIG_NFT_RBTREE) += nft_rbtree.o
81obj-$(CONFIG_NFT_HASH) += nft_hash.o
82obj-$(CONFIG_NFT_COUNTER) += nft_counter.o
83obj-$(CONFIG_NFT_LOG) += nft_log.o
84
67# generic X tables 85# generic X tables
68obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o 86obj-$(CONFIG_NETFILTER_XTABLES) += x_tables.o xt_tcpudp.o
69 87
diff --git a/net/netfilter/core.c b/net/netfilter/core.c
index 593b16ea45e0..1fbab0cdd302 100644
--- a/net/netfilter/core.c
+++ b/net/netfilter/core.c
@@ -146,7 +146,7 @@ unsigned int nf_iterate(struct list_head *head,
146 /* Optimization: we don't need to hold module 146 /* Optimization: we don't need to hold module
147 reference here, since function can't sleep. --RR */ 147 reference here, since function can't sleep. --RR */
148repeat: 148repeat:
149 verdict = (*elemp)->hook(hook, skb, indev, outdev, okfn); 149 verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn);
150 if (verdict != NF_ACCEPT) { 150 if (verdict != NF_ACCEPT) {
151#ifdef CONFIG_NETFILTER_DEBUG 151#ifdef CONFIG_NETFILTER_DEBUG
152 if (unlikely((verdict & NF_VERDICT_MASK) 152 if (unlikely((verdict & NF_VERDICT_MASK)
diff --git a/net/netfilter/ipset/Kconfig b/net/netfilter/ipset/Kconfig
index ba36c283d837..a2d6263b6c64 100644
--- a/net/netfilter/ipset/Kconfig
+++ b/net/netfilter/ipset/Kconfig
@@ -1,7 +1,7 @@
1menuconfig IP_SET 1menuconfig IP_SET
2 tristate "IP set support" 2 tristate "IP set support"
3 depends on INET && NETFILTER 3 depends on INET && NETFILTER
4 depends on NETFILTER_NETLINK 4 select NETFILTER_NETLINK
5 help 5 help
6 This option adds IP set support to the kernel. 6 This option adds IP set support to the kernel.
7 In order to define and use the sets, you need the userspace utility 7 In order to define and use the sets, you need the userspace utility
@@ -90,6 +90,15 @@ config IP_SET_HASH_IPPORTNET
90 90
91 To compile it as a module, choose M here. If unsure, say N. 91 To compile it as a module, choose M here. If unsure, say N.
92 92
93config IP_SET_HASH_NETPORTNET
94 tristate "hash:net,port,net set support"
95 depends on IP_SET
96 help
97 This option adds the hash:net,port,net set type support, by which
98 one can store two IPv4/IPv6 subnets, and a protocol/port in a set.
99
100 To compile it as a module, choose M here. If unsure, say N.
101
93config IP_SET_HASH_NET 102config IP_SET_HASH_NET
94 tristate "hash:net set support" 103 tristate "hash:net set support"
95 depends on IP_SET 104 depends on IP_SET
@@ -99,6 +108,15 @@ config IP_SET_HASH_NET
99 108
100 To compile it as a module, choose M here. If unsure, say N. 109 To compile it as a module, choose M here. If unsure, say N.
101 110
111config IP_SET_HASH_NETNET
112 tristate "hash:net,net set support"
113 depends on IP_SET
114 help
115 This option adds the hash:net,net set type support, by which
116 one can store IPv4/IPv6 network address/prefix pairs in a set.
117
118 To compile it as a module, choose M here. If unsure, say N.
119
102config IP_SET_HASH_NETPORT 120config IP_SET_HASH_NETPORT
103 tristate "hash:net,port set support" 121 tristate "hash:net,port set support"
104 depends on IP_SET 122 depends on IP_SET
diff --git a/net/netfilter/ipset/Makefile b/net/netfilter/ipset/Makefile
index 6e965ecd5444..44b2d38476fa 100644
--- a/net/netfilter/ipset/Makefile
+++ b/net/netfilter/ipset/Makefile
@@ -20,6 +20,8 @@ obj-$(CONFIG_IP_SET_HASH_IPPORTNET) += ip_set_hash_ipportnet.o
20obj-$(CONFIG_IP_SET_HASH_NET) += ip_set_hash_net.o 20obj-$(CONFIG_IP_SET_HASH_NET) += ip_set_hash_net.o
21obj-$(CONFIG_IP_SET_HASH_NETPORT) += ip_set_hash_netport.o 21obj-$(CONFIG_IP_SET_HASH_NETPORT) += ip_set_hash_netport.o
22obj-$(CONFIG_IP_SET_HASH_NETIFACE) += ip_set_hash_netiface.o 22obj-$(CONFIG_IP_SET_HASH_NETIFACE) += ip_set_hash_netiface.o
23obj-$(CONFIG_IP_SET_HASH_NETNET) += ip_set_hash_netnet.o
24obj-$(CONFIG_IP_SET_HASH_NETPORTNET) += ip_set_hash_netportnet.o
23 25
24# list types 26# list types
25obj-$(CONFIG_IP_SET_LIST_SET) += ip_set_list_set.o 27obj-$(CONFIG_IP_SET_LIST_SET) += ip_set_list_set.o
diff --git a/net/netfilter/ipset/ip_set_bitmap_gen.h b/net/netfilter/ipset/ip_set_bitmap_gen.h
index 25243379b887..f2c7d83dc23f 100644
--- a/net/netfilter/ipset/ip_set_bitmap_gen.h
+++ b/net/netfilter/ipset/ip_set_bitmap_gen.h
@@ -8,38 +8,32 @@
8#ifndef __IP_SET_BITMAP_IP_GEN_H 8#ifndef __IP_SET_BITMAP_IP_GEN_H
9#define __IP_SET_BITMAP_IP_GEN_H 9#define __IP_SET_BITMAP_IP_GEN_H
10 10
11#define CONCAT(a, b) a##b 11#define mtype_do_test IPSET_TOKEN(MTYPE, _do_test)
12#define TOKEN(a,b) CONCAT(a, b) 12#define mtype_gc_test IPSET_TOKEN(MTYPE, _gc_test)
13 13#define mtype_is_filled IPSET_TOKEN(MTYPE, _is_filled)
14#define mtype_do_test TOKEN(MTYPE, _do_test) 14#define mtype_do_add IPSET_TOKEN(MTYPE, _do_add)
15#define mtype_gc_test TOKEN(MTYPE, _gc_test) 15#define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
16#define mtype_is_filled TOKEN(MTYPE, _is_filled) 16#define mtype_do_del IPSET_TOKEN(MTYPE, _do_del)
17#define mtype_do_add TOKEN(MTYPE, _do_add) 17#define mtype_do_list IPSET_TOKEN(MTYPE, _do_list)
18#define mtype_do_del TOKEN(MTYPE, _do_del) 18#define mtype_do_head IPSET_TOKEN(MTYPE, _do_head)
19#define mtype_do_list TOKEN(MTYPE, _do_list) 19#define mtype_adt_elem IPSET_TOKEN(MTYPE, _adt_elem)
20#define mtype_do_head TOKEN(MTYPE, _do_head) 20#define mtype_add_timeout IPSET_TOKEN(MTYPE, _add_timeout)
21#define mtype_adt_elem TOKEN(MTYPE, _adt_elem) 21#define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
22#define mtype_add_timeout TOKEN(MTYPE, _add_timeout) 22#define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
23#define mtype_gc_init TOKEN(MTYPE, _gc_init) 23#define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
24#define mtype_kadt TOKEN(MTYPE, _kadt) 24#define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
25#define mtype_uadt TOKEN(MTYPE, _uadt) 25#define mtype_flush IPSET_TOKEN(MTYPE, _flush)
26#define mtype_destroy TOKEN(MTYPE, _destroy) 26#define mtype_head IPSET_TOKEN(MTYPE, _head)
27#define mtype_flush TOKEN(MTYPE, _flush) 27#define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
28#define mtype_head TOKEN(MTYPE, _head) 28#define mtype_elem IPSET_TOKEN(MTYPE, _elem)
29#define mtype_same_set TOKEN(MTYPE, _same_set) 29#define mtype_test IPSET_TOKEN(MTYPE, _test)
30#define mtype_elem TOKEN(MTYPE, _elem) 30#define mtype_add IPSET_TOKEN(MTYPE, _add)
31#define mtype_test TOKEN(MTYPE, _test) 31#define mtype_del IPSET_TOKEN(MTYPE, _del)
32#define mtype_add TOKEN(MTYPE, _add) 32#define mtype_list IPSET_TOKEN(MTYPE, _list)
33#define mtype_del TOKEN(MTYPE, _del) 33#define mtype_gc IPSET_TOKEN(MTYPE, _gc)
34#define mtype_list TOKEN(MTYPE, _list)
35#define mtype_gc TOKEN(MTYPE, _gc)
36#define mtype MTYPE 34#define mtype MTYPE
37 35
38#define ext_timeout(e, m) \ 36#define get_ext(set, map, id) ((map)->extensions + (set)->dsize * (id))
39 (unsigned long *)((e) + (m)->offset[IPSET_OFFSET_TIMEOUT])
40#define ext_counter(e, m) \
41 (struct ip_set_counter *)((e) + (m)->offset[IPSET_OFFSET_COUNTER])
42#define get_ext(map, id) ((map)->extensions + (map)->dsize * (id))
43 37
44static void 38static void
45mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set)) 39mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
@@ -49,11 +43,22 @@ mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
49 init_timer(&map->gc); 43 init_timer(&map->gc);
50 map->gc.data = (unsigned long) set; 44 map->gc.data = (unsigned long) set;
51 map->gc.function = gc; 45 map->gc.function = gc;
52 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ; 46 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
53 add_timer(&map->gc); 47 add_timer(&map->gc);
54} 48}
55 49
56static void 50static void
51mtype_ext_cleanup(struct ip_set *set)
52{
53 struct mtype *map = set->data;
54 u32 id;
55
56 for (id = 0; id < map->elements; id++)
57 if (test_bit(id, map->members))
58 ip_set_ext_destroy(set, get_ext(set, map, id));
59}
60
61static void
57mtype_destroy(struct ip_set *set) 62mtype_destroy(struct ip_set *set)
58{ 63{
59 struct mtype *map = set->data; 64 struct mtype *map = set->data;
@@ -62,8 +67,11 @@ mtype_destroy(struct ip_set *set)
62 del_timer_sync(&map->gc); 67 del_timer_sync(&map->gc);
63 68
64 ip_set_free(map->members); 69 ip_set_free(map->members);
65 if (map->dsize) 70 if (set->dsize) {
71 if (set->extensions & IPSET_EXT_DESTROY)
72 mtype_ext_cleanup(set);
66 ip_set_free(map->extensions); 73 ip_set_free(map->extensions);
74 }
67 kfree(map); 75 kfree(map);
68 76
69 set->data = NULL; 77 set->data = NULL;
@@ -74,6 +82,8 @@ mtype_flush(struct ip_set *set)
74{ 82{
75 struct mtype *map = set->data; 83 struct mtype *map = set->data;
76 84
85 if (set->extensions & IPSET_EXT_DESTROY)
86 mtype_ext_cleanup(set);
77 memset(map->members, 0, map->memsize); 87 memset(map->members, 0, map->memsize);
78} 88}
79 89
@@ -91,12 +101,9 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)
91 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, 101 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
92 htonl(sizeof(*map) + 102 htonl(sizeof(*map) +
93 map->memsize + 103 map->memsize +
94 map->dsize * map->elements)) || 104 set->dsize * map->elements)))
95 (SET_WITH_TIMEOUT(set) && 105 goto nla_put_failure;
96 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout))) || 106 if (unlikely(ip_set_put_flags(skb, set)))
97 (SET_WITH_COUNTER(set) &&
98 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS,
99 htonl(IPSET_FLAG_WITH_COUNTERS))))
100 goto nla_put_failure; 107 goto nla_put_failure;
101 ipset_nest_end(skb, nested); 108 ipset_nest_end(skb, nested);
102 109
@@ -111,16 +118,16 @@ mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
111{ 118{
112 struct mtype *map = set->data; 119 struct mtype *map = set->data;
113 const struct mtype_adt_elem *e = value; 120 const struct mtype_adt_elem *e = value;
114 void *x = get_ext(map, e->id); 121 void *x = get_ext(set, map, e->id);
115 int ret = mtype_do_test(e, map); 122 int ret = mtype_do_test(e, map, set->dsize);
116 123
117 if (ret <= 0) 124 if (ret <= 0)
118 return ret; 125 return ret;
119 if (SET_WITH_TIMEOUT(set) && 126 if (SET_WITH_TIMEOUT(set) &&
120 ip_set_timeout_expired(ext_timeout(x, map))) 127 ip_set_timeout_expired(ext_timeout(x, set)))
121 return 0; 128 return 0;
122 if (SET_WITH_COUNTER(set)) 129 if (SET_WITH_COUNTER(set))
123 ip_set_update_counter(ext_counter(x, map), ext, mext, flags); 130 ip_set_update_counter(ext_counter(x, set), ext, mext, flags);
124 return 1; 131 return 1;
125} 132}
126 133
@@ -130,26 +137,30 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
130{ 137{
131 struct mtype *map = set->data; 138 struct mtype *map = set->data;
132 const struct mtype_adt_elem *e = value; 139 const struct mtype_adt_elem *e = value;
133 void *x = get_ext(map, e->id); 140 void *x = get_ext(set, map, e->id);
134 int ret = mtype_do_add(e, map, flags); 141 int ret = mtype_do_add(e, map, flags, set->dsize);
135 142
136 if (ret == IPSET_ADD_FAILED) { 143 if (ret == IPSET_ADD_FAILED) {
137 if (SET_WITH_TIMEOUT(set) && 144 if (SET_WITH_TIMEOUT(set) &&
138 ip_set_timeout_expired(ext_timeout(x, map))) 145 ip_set_timeout_expired(ext_timeout(x, set)))
139 ret = 0; 146 ret = 0;
140 else if (!(flags & IPSET_FLAG_EXIST)) 147 else if (!(flags & IPSET_FLAG_EXIST))
141 return -IPSET_ERR_EXIST; 148 return -IPSET_ERR_EXIST;
149 /* Element is re-added, cleanup extensions */
150 ip_set_ext_destroy(set, x);
142 } 151 }
143 152
144 if (SET_WITH_TIMEOUT(set)) 153 if (SET_WITH_TIMEOUT(set))
145#ifdef IP_SET_BITMAP_STORED_TIMEOUT 154#ifdef IP_SET_BITMAP_STORED_TIMEOUT
146 mtype_add_timeout(ext_timeout(x, map), e, ext, map, ret); 155 mtype_add_timeout(ext_timeout(x, set), e, ext, set, map, ret);
147#else 156#else
148 ip_set_timeout_set(ext_timeout(x, map), ext->timeout); 157 ip_set_timeout_set(ext_timeout(x, set), ext->timeout);
149#endif 158#endif
150 159
151 if (SET_WITH_COUNTER(set)) 160 if (SET_WITH_COUNTER(set))
152 ip_set_init_counter(ext_counter(x, map), ext); 161 ip_set_init_counter(ext_counter(x, set), ext);
162 if (SET_WITH_COMMENT(set))
163 ip_set_init_comment(ext_comment(x, set), ext);
153 return 0; 164 return 0;
154} 165}
155 166
@@ -159,16 +170,27 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
159{ 170{
160 struct mtype *map = set->data; 171 struct mtype *map = set->data;
161 const struct mtype_adt_elem *e = value; 172 const struct mtype_adt_elem *e = value;
162 const void *x = get_ext(map, e->id); 173 void *x = get_ext(set, map, e->id);
163 174
164 if (mtype_do_del(e, map) || 175 if (mtype_do_del(e, map))
165 (SET_WITH_TIMEOUT(set) && 176 return -IPSET_ERR_EXIST;
166 ip_set_timeout_expired(ext_timeout(x, map)))) 177
178 ip_set_ext_destroy(set, x);
179 if (SET_WITH_TIMEOUT(set) &&
180 ip_set_timeout_expired(ext_timeout(x, set)))
167 return -IPSET_ERR_EXIST; 181 return -IPSET_ERR_EXIST;
168 182
169 return 0; 183 return 0;
170} 184}
171 185
186#ifndef IP_SET_BITMAP_STORED_TIMEOUT
187static inline bool
188mtype_is_filled(const struct mtype_elem *x)
189{
190 return true;
191}
192#endif
193
172static int 194static int
173mtype_list(const struct ip_set *set, 195mtype_list(const struct ip_set *set,
174 struct sk_buff *skb, struct netlink_callback *cb) 196 struct sk_buff *skb, struct netlink_callback *cb)
@@ -176,20 +198,21 @@ mtype_list(const struct ip_set *set,
176 struct mtype *map = set->data; 198 struct mtype *map = set->data;
177 struct nlattr *adt, *nested; 199 struct nlattr *adt, *nested;
178 void *x; 200 void *x;
179 u32 id, first = cb->args[2]; 201 u32 id, first = cb->args[IPSET_CB_ARG0];
180 202
181 adt = ipset_nest_start(skb, IPSET_ATTR_ADT); 203 adt = ipset_nest_start(skb, IPSET_ATTR_ADT);
182 if (!adt) 204 if (!adt)
183 return -EMSGSIZE; 205 return -EMSGSIZE;
184 for (; cb->args[2] < map->elements; cb->args[2]++) { 206 for (; cb->args[IPSET_CB_ARG0] < map->elements;
185 id = cb->args[2]; 207 cb->args[IPSET_CB_ARG0]++) {
186 x = get_ext(map, id); 208 id = cb->args[IPSET_CB_ARG0];
209 x = get_ext(set, map, id);
187 if (!test_bit(id, map->members) || 210 if (!test_bit(id, map->members) ||
188 (SET_WITH_TIMEOUT(set) && 211 (SET_WITH_TIMEOUT(set) &&
189#ifdef IP_SET_BITMAP_STORED_TIMEOUT 212#ifdef IP_SET_BITMAP_STORED_TIMEOUT
190 mtype_is_filled((const struct mtype_elem *) x) && 213 mtype_is_filled((const struct mtype_elem *) x) &&
191#endif 214#endif
192 ip_set_timeout_expired(ext_timeout(x, map)))) 215 ip_set_timeout_expired(ext_timeout(x, set))))
193 continue; 216 continue;
194 nested = ipset_nest_start(skb, IPSET_ATTR_DATA); 217 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
195 if (!nested) { 218 if (!nested) {
@@ -199,40 +222,27 @@ mtype_list(const struct ip_set *set,
199 } else 222 } else
200 goto nla_put_failure; 223 goto nla_put_failure;
201 } 224 }
202 if (mtype_do_list(skb, map, id)) 225 if (mtype_do_list(skb, map, id, set->dsize))
203 goto nla_put_failure; 226 goto nla_put_failure;
204 if (SET_WITH_TIMEOUT(set)) { 227 if (ip_set_put_extensions(skb, set, x,
205#ifdef IP_SET_BITMAP_STORED_TIMEOUT 228 mtype_is_filled((const struct mtype_elem *) x)))
206 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
207 htonl(ip_set_timeout_stored(map, id,
208 ext_timeout(x, map)))))
209 goto nla_put_failure;
210#else
211 if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
212 htonl(ip_set_timeout_get(
213 ext_timeout(x, map)))))
214 goto nla_put_failure;
215#endif
216 }
217 if (SET_WITH_COUNTER(set) &&
218 ip_set_put_counter(skb, ext_counter(x, map)))
219 goto nla_put_failure; 229 goto nla_put_failure;
220 ipset_nest_end(skb, nested); 230 ipset_nest_end(skb, nested);
221 } 231 }
222 ipset_nest_end(skb, adt); 232 ipset_nest_end(skb, adt);
223 233
224 /* Set listing finished */ 234 /* Set listing finished */
225 cb->args[2] = 0; 235 cb->args[IPSET_CB_ARG0] = 0;
226 236
227 return 0; 237 return 0;
228 238
229nla_put_failure: 239nla_put_failure:
230 nla_nest_cancel(skb, nested); 240 nla_nest_cancel(skb, nested);
231 ipset_nest_end(skb, adt);
232 if (unlikely(id == first)) { 241 if (unlikely(id == first)) {
233 cb->args[2] = 0; 242 cb->args[IPSET_CB_ARG0] = 0;
234 return -EMSGSIZE; 243 return -EMSGSIZE;
235 } 244 }
245 ipset_nest_end(skb, adt);
236 return 0; 246 return 0;
237} 247}
238 248
@@ -241,21 +251,23 @@ mtype_gc(unsigned long ul_set)
241{ 251{
242 struct ip_set *set = (struct ip_set *) ul_set; 252 struct ip_set *set = (struct ip_set *) ul_set;
243 struct mtype *map = set->data; 253 struct mtype *map = set->data;
244 const void *x; 254 void *x;
245 u32 id; 255 u32 id;
246 256
247 /* We run parallel with other readers (test element) 257 /* We run parallel with other readers (test element)
248 * but adding/deleting new entries is locked out */ 258 * but adding/deleting new entries is locked out */
249 read_lock_bh(&set->lock); 259 read_lock_bh(&set->lock);
250 for (id = 0; id < map->elements; id++) 260 for (id = 0; id < map->elements; id++)
251 if (mtype_gc_test(id, map)) { 261 if (mtype_gc_test(id, map, set->dsize)) {
252 x = get_ext(map, id); 262 x = get_ext(set, map, id);
253 if (ip_set_timeout_expired(ext_timeout(x, map))) 263 if (ip_set_timeout_expired(ext_timeout(x, set))) {
254 clear_bit(id, map->members); 264 clear_bit(id, map->members);
265 ip_set_ext_destroy(set, x);
266 }
255 } 267 }
256 read_unlock_bh(&set->lock); 268 read_unlock_bh(&set->lock);
257 269
258 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ; 270 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
259 add_timer(&map->gc); 271 add_timer(&map->gc);
260} 272}
261 273
diff --git a/net/netfilter/ipset/ip_set_bitmap_ip.c b/net/netfilter/ipset/ip_set_bitmap_ip.c
index f1a8128bef01..6f1f9f494808 100644
--- a/net/netfilter/ipset/ip_set_bitmap_ip.c
+++ b/net/netfilter/ipset/ip_set_bitmap_ip.c
@@ -25,12 +25,13 @@
25#include <linux/netfilter/ipset/ip_set.h> 25#include <linux/netfilter/ipset/ip_set.h>
26#include <linux/netfilter/ipset/ip_set_bitmap.h> 26#include <linux/netfilter/ipset/ip_set_bitmap.h>
27 27
28#define REVISION_MIN 0 28#define IPSET_TYPE_REV_MIN 0
29#define REVISION_MAX 1 /* Counter support added */ 29/* 1 Counter support added */
30#define IPSET_TYPE_REV_MAX 2 /* Comment support added */
30 31
31MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33IP_SET_MODULE_DESC("bitmap:ip", REVISION_MIN, REVISION_MAX); 34IP_SET_MODULE_DESC("bitmap:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_bitmap:ip"); 35MODULE_ALIAS("ip_set_bitmap:ip");
35 36
36#define MTYPE bitmap_ip 37#define MTYPE bitmap_ip
@@ -44,10 +45,7 @@ struct bitmap_ip {
44 u32 elements; /* number of max elements in the set */ 45 u32 elements; /* number of max elements in the set */
45 u32 hosts; /* number of hosts in a subnet */ 46 u32 hosts; /* number of hosts in a subnet */
46 size_t memsize; /* members size */ 47 size_t memsize; /* members size */
47 size_t dsize; /* extensions struct size */
48 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
49 u8 netmask; /* subnet netmask */ 48 u8 netmask; /* subnet netmask */
50 u32 timeout; /* timeout parameter */
51 struct timer_list gc; /* garbage collection */ 49 struct timer_list gc; /* garbage collection */
52}; 50};
53 51
@@ -65,20 +63,21 @@ ip_to_id(const struct bitmap_ip *m, u32 ip)
65/* Common functions */ 63/* Common functions */
66 64
67static inline int 65static inline int
68bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map) 66bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e,
67 struct bitmap_ip *map, size_t dsize)
69{ 68{
70 return !!test_bit(e->id, map->members); 69 return !!test_bit(e->id, map->members);
71} 70}
72 71
73static inline int 72static inline int
74bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map) 73bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map, size_t dsize)
75{ 74{
76 return !!test_bit(id, map->members); 75 return !!test_bit(id, map->members);
77} 76}
78 77
79static inline int 78static inline int
80bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map, 79bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map,
81 u32 flags) 80 u32 flags, size_t dsize)
82{ 81{
83 return !!test_and_set_bit(e->id, map->members); 82 return !!test_and_set_bit(e->id, map->members);
84} 83}
@@ -90,7 +89,8 @@ bitmap_ip_do_del(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map)
90} 89}
91 90
92static inline int 91static inline int
93bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id) 92bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id,
93 size_t dsize)
94{ 94{
95 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, 95 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
96 htonl(map->first_ip + id * map->hosts)); 96 htonl(map->first_ip + id * map->hosts));
@@ -113,7 +113,7 @@ bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb,
113 struct bitmap_ip *map = set->data; 113 struct bitmap_ip *map = set->data;
114 ipset_adtfn adtfn = set->variant->adt[adt]; 114 ipset_adtfn adtfn = set->variant->adt[adt];
115 struct bitmap_ip_adt_elem e = { }; 115 struct bitmap_ip_adt_elem e = { };
116 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, map); 116 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
117 u32 ip; 117 u32 ip;
118 118
119 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC)); 119 ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC));
@@ -131,9 +131,9 @@ bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[],
131{ 131{
132 struct bitmap_ip *map = set->data; 132 struct bitmap_ip *map = set->data;
133 ipset_adtfn adtfn = set->variant->adt[adt]; 133 ipset_adtfn adtfn = set->variant->adt[adt];
134 u32 ip, ip_to; 134 u32 ip = 0, ip_to = 0;
135 struct bitmap_ip_adt_elem e = { }; 135 struct bitmap_ip_adt_elem e = { };
136 struct ip_set_ext ext = IP_SET_INIT_UEXT(map); 136 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
137 int ret = 0; 137 int ret = 0;
138 138
139 if (unlikely(!tb[IPSET_ATTR_IP] || 139 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -200,7 +200,7 @@ bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)
200 return x->first_ip == y->first_ip && 200 return x->first_ip == y->first_ip &&
201 x->last_ip == y->last_ip && 201 x->last_ip == y->last_ip &&
202 x->netmask == y->netmask && 202 x->netmask == y->netmask &&
203 x->timeout == y->timeout && 203 a->timeout == b->timeout &&
204 a->extensions == b->extensions; 204 a->extensions == b->extensions;
205} 205}
206 206
@@ -209,25 +209,6 @@ bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b)
209struct bitmap_ip_elem { 209struct bitmap_ip_elem {
210}; 210};
211 211
212/* Timeout variant */
213
214struct bitmap_ipt_elem {
215 unsigned long timeout;
216};
217
218/* Plain variant with counter */
219
220struct bitmap_ipc_elem {
221 struct ip_set_counter counter;
222};
223
224/* Timeout variant with counter */
225
226struct bitmap_ipct_elem {
227 unsigned long timeout;
228 struct ip_set_counter counter;
229};
230
231#include "ip_set_bitmap_gen.h" 212#include "ip_set_bitmap_gen.h"
232 213
233/* Create bitmap:ip type of sets */ 214/* Create bitmap:ip type of sets */
@@ -240,8 +221,8 @@ init_map_ip(struct ip_set *set, struct bitmap_ip *map,
240 map->members = ip_set_alloc(map->memsize); 221 map->members = ip_set_alloc(map->memsize);
241 if (!map->members) 222 if (!map->members)
242 return false; 223 return false;
243 if (map->dsize) { 224 if (set->dsize) {
244 map->extensions = ip_set_alloc(map->dsize * elements); 225 map->extensions = ip_set_alloc(set->dsize * elements);
245 if (!map->extensions) { 226 if (!map->extensions) {
246 kfree(map->members); 227 kfree(map->members);
247 return false; 228 return false;
@@ -252,7 +233,7 @@ init_map_ip(struct ip_set *set, struct bitmap_ip *map,
252 map->elements = elements; 233 map->elements = elements;
253 map->hosts = hosts; 234 map->hosts = hosts;
254 map->netmask = netmask; 235 map->netmask = netmask;
255 map->timeout = IPSET_NO_TIMEOUT; 236 set->timeout = IPSET_NO_TIMEOUT;
256 237
257 set->data = map; 238 set->data = map;
258 set->family = NFPROTO_IPV4; 239 set->family = NFPROTO_IPV4;
@@ -261,10 +242,11 @@ init_map_ip(struct ip_set *set, struct bitmap_ip *map,
261} 242}
262 243
263static int 244static int
264bitmap_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags) 245bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
246 u32 flags)
265{ 247{
266 struct bitmap_ip *map; 248 struct bitmap_ip *map;
267 u32 first_ip, last_ip, hosts, cadt_flags = 0; 249 u32 first_ip = 0, last_ip = 0, hosts;
268 u64 elements; 250 u64 elements;
269 u8 netmask = 32; 251 u8 netmask = 32;
270 int ret; 252 int ret;
@@ -336,61 +318,15 @@ bitmap_ip_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
336 318
337 map->memsize = bitmap_bytes(0, elements - 1); 319 map->memsize = bitmap_bytes(0, elements - 1);
338 set->variant = &bitmap_ip; 320 set->variant = &bitmap_ip;
339 if (tb[IPSET_ATTR_CADT_FLAGS]) 321 set->dsize = ip_set_elem_len(set, tb, 0);
340 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 322 if (!init_map_ip(set, map, first_ip, last_ip,
341 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) { 323 elements, hosts, netmask)) {
342 set->extensions |= IPSET_EXT_COUNTER; 324 kfree(map);
343 if (tb[IPSET_ATTR_TIMEOUT]) { 325 return -ENOMEM;
344 map->dsize = sizeof(struct bitmap_ipct_elem); 326 }
345 map->offset[IPSET_OFFSET_TIMEOUT] = 327 if (tb[IPSET_ATTR_TIMEOUT]) {
346 offsetof(struct bitmap_ipct_elem, timeout); 328 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
347 map->offset[IPSET_OFFSET_COUNTER] =
348 offsetof(struct bitmap_ipct_elem, counter);
349
350 if (!init_map_ip(set, map, first_ip, last_ip,
351 elements, hosts, netmask)) {
352 kfree(map);
353 return -ENOMEM;
354 }
355
356 map->timeout = ip_set_timeout_uget(
357 tb[IPSET_ATTR_TIMEOUT]);
358 set->extensions |= IPSET_EXT_TIMEOUT;
359
360 bitmap_ip_gc_init(set, bitmap_ip_gc);
361 } else {
362 map->dsize = sizeof(struct bitmap_ipc_elem);
363 map->offset[IPSET_OFFSET_COUNTER] =
364 offsetof(struct bitmap_ipc_elem, counter);
365
366 if (!init_map_ip(set, map, first_ip, last_ip,
367 elements, hosts, netmask)) {
368 kfree(map);
369 return -ENOMEM;
370 }
371 }
372 } else if (tb[IPSET_ATTR_TIMEOUT]) {
373 map->dsize = sizeof(struct bitmap_ipt_elem);
374 map->offset[IPSET_OFFSET_TIMEOUT] =
375 offsetof(struct bitmap_ipt_elem, timeout);
376
377 if (!init_map_ip(set, map, first_ip, last_ip,
378 elements, hosts, netmask)) {
379 kfree(map);
380 return -ENOMEM;
381 }
382
383 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
384 set->extensions |= IPSET_EXT_TIMEOUT;
385
386 bitmap_ip_gc_init(set, bitmap_ip_gc); 329 bitmap_ip_gc_init(set, bitmap_ip_gc);
387 } else {
388 map->dsize = 0;
389 if (!init_map_ip(set, map, first_ip, last_ip,
390 elements, hosts, netmask)) {
391 kfree(map);
392 return -ENOMEM;
393 }
394 } 330 }
395 return 0; 331 return 0;
396} 332}
@@ -401,8 +337,8 @@ static struct ip_set_type bitmap_ip_type __read_mostly = {
401 .features = IPSET_TYPE_IP, 337 .features = IPSET_TYPE_IP,
402 .dimension = IPSET_DIM_ONE, 338 .dimension = IPSET_DIM_ONE,
403 .family = NFPROTO_IPV4, 339 .family = NFPROTO_IPV4,
404 .revision_min = REVISION_MIN, 340 .revision_min = IPSET_TYPE_REV_MIN,
405 .revision_max = REVISION_MAX, 341 .revision_max = IPSET_TYPE_REV_MAX,
406 .create = bitmap_ip_create, 342 .create = bitmap_ip_create,
407 .create_policy = { 343 .create_policy = {
408 [IPSET_ATTR_IP] = { .type = NLA_NESTED }, 344 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
@@ -420,6 +356,7 @@ static struct ip_set_type bitmap_ip_type __read_mostly = {
420 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 356 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
421 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 357 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
422 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 358 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
359 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
423 }, 360 },
424 .me = THIS_MODULE, 361 .me = THIS_MODULE,
425}; 362};
diff --git a/net/netfilter/ipset/ip_set_bitmap_ipmac.c b/net/netfilter/ipset/ip_set_bitmap_ipmac.c
index 3b30e0bef890..740eabededd9 100644
--- a/net/netfilter/ipset/ip_set_bitmap_ipmac.c
+++ b/net/netfilter/ipset/ip_set_bitmap_ipmac.c
@@ -25,12 +25,13 @@
25#include <linux/netfilter/ipset/ip_set.h> 25#include <linux/netfilter/ipset/ip_set.h>
26#include <linux/netfilter/ipset/ip_set_bitmap.h> 26#include <linux/netfilter/ipset/ip_set_bitmap.h>
27 27
28#define REVISION_MIN 0 28#define IPSET_TYPE_REV_MIN 0
29#define REVISION_MAX 1 /* Counter support added */ 29/* 1 Counter support added */
30#define IPSET_TYPE_REV_MAX 2 /* Comment support added */
30 31
31MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33IP_SET_MODULE_DESC("bitmap:ip,mac", REVISION_MIN, REVISION_MAX); 34IP_SET_MODULE_DESC("bitmap:ip,mac", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_bitmap:ip,mac"); 35MODULE_ALIAS("ip_set_bitmap:ip,mac");
35 36
36#define MTYPE bitmap_ipmac 37#define MTYPE bitmap_ipmac
@@ -48,11 +49,8 @@ struct bitmap_ipmac {
48 u32 first_ip; /* host byte order, included in range */ 49 u32 first_ip; /* host byte order, included in range */
49 u32 last_ip; /* host byte order, included in range */ 50 u32 last_ip; /* host byte order, included in range */
50 u32 elements; /* number of max elements in the set */ 51 u32 elements; /* number of max elements in the set */
51 u32 timeout; /* timeout value */
52 struct timer_list gc; /* garbage collector */
53 size_t memsize; /* members size */ 52 size_t memsize; /* members size */
54 size_t dsize; /* size of element */ 53 struct timer_list gc; /* garbage collector */
55 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
56}; 54};
57 55
58/* ADT structure for generic function args */ 56/* ADT structure for generic function args */
@@ -82,13 +80,13 @@ get_elem(void *extensions, u16 id, size_t dsize)
82 80
83static inline int 81static inline int
84bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e, 82bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
85 const struct bitmap_ipmac *map) 83 const struct bitmap_ipmac *map, size_t dsize)
86{ 84{
87 const struct bitmap_ipmac_elem *elem; 85 const struct bitmap_ipmac_elem *elem;
88 86
89 if (!test_bit(e->id, map->members)) 87 if (!test_bit(e->id, map->members))
90 return 0; 88 return 0;
91 elem = get_elem(map->extensions, e->id, map->dsize); 89 elem = get_elem(map->extensions, e->id, dsize);
92 if (elem->filled == MAC_FILLED) 90 if (elem->filled == MAC_FILLED)
93 return e->ether == NULL || 91 return e->ether == NULL ||
94 ether_addr_equal(e->ether, elem->ether); 92 ether_addr_equal(e->ether, elem->ether);
@@ -97,13 +95,13 @@ bitmap_ipmac_do_test(const struct bitmap_ipmac_adt_elem *e,
97} 95}
98 96
99static inline int 97static inline int
100bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map) 98bitmap_ipmac_gc_test(u16 id, const struct bitmap_ipmac *map, size_t dsize)
101{ 99{
102 const struct bitmap_ipmac_elem *elem; 100 const struct bitmap_ipmac_elem *elem;
103 101
104 if (!test_bit(id, map->members)) 102 if (!test_bit(id, map->members))
105 return 0; 103 return 0;
106 elem = get_elem(map->extensions, id, map->dsize); 104 elem = get_elem(map->extensions, id, dsize);
107 /* Timer not started for the incomplete elements */ 105 /* Timer not started for the incomplete elements */
108 return elem->filled == MAC_FILLED; 106 return elem->filled == MAC_FILLED;
109} 107}
@@ -117,13 +115,13 @@ bitmap_ipmac_is_filled(const struct bitmap_ipmac_elem *elem)
117static inline int 115static inline int
118bitmap_ipmac_add_timeout(unsigned long *timeout, 116bitmap_ipmac_add_timeout(unsigned long *timeout,
119 const struct bitmap_ipmac_adt_elem *e, 117 const struct bitmap_ipmac_adt_elem *e,
120 const struct ip_set_ext *ext, 118 const struct ip_set_ext *ext, struct ip_set *set,
121 struct bitmap_ipmac *map, int mode) 119 struct bitmap_ipmac *map, int mode)
122{ 120{
123 u32 t = ext->timeout; 121 u32 t = ext->timeout;
124 122
125 if (mode == IPSET_ADD_START_STORED_TIMEOUT) { 123 if (mode == IPSET_ADD_START_STORED_TIMEOUT) {
126 if (t == map->timeout) 124 if (t == set->timeout)
127 /* Timeout was not specified, get stored one */ 125 /* Timeout was not specified, get stored one */
128 t = *timeout; 126 t = *timeout;
129 ip_set_timeout_set(timeout, t); 127 ip_set_timeout_set(timeout, t);
@@ -142,11 +140,11 @@ bitmap_ipmac_add_timeout(unsigned long *timeout,
142 140
143static inline int 141static inline int
144bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e, 142bitmap_ipmac_do_add(const struct bitmap_ipmac_adt_elem *e,
145 struct bitmap_ipmac *map, u32 flags) 143 struct bitmap_ipmac *map, u32 flags, size_t dsize)
146{ 144{
147 struct bitmap_ipmac_elem *elem; 145 struct bitmap_ipmac_elem *elem;
148 146
149 elem = get_elem(map->extensions, e->id, map->dsize); 147 elem = get_elem(map->extensions, e->id, dsize);
150 if (test_and_set_bit(e->id, map->members)) { 148 if (test_and_set_bit(e->id, map->members)) {
151 if (elem->filled == MAC_FILLED) { 149 if (elem->filled == MAC_FILLED) {
152 if (e->ether && (flags & IPSET_FLAG_EXIST)) 150 if (e->ether && (flags & IPSET_FLAG_EXIST))
@@ -178,22 +176,12 @@ bitmap_ipmac_do_del(const struct bitmap_ipmac_adt_elem *e,
178 return !test_and_clear_bit(e->id, map->members); 176 return !test_and_clear_bit(e->id, map->members);
179} 177}
180 178
181static inline unsigned long
182ip_set_timeout_stored(struct bitmap_ipmac *map, u32 id, unsigned long *timeout)
183{
184 const struct bitmap_ipmac_elem *elem =
185 get_elem(map->extensions, id, map->dsize);
186
187 return elem->filled == MAC_FILLED ? ip_set_timeout_get(timeout) :
188 *timeout;
189}
190
191static inline int 179static inline int
192bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map, 180bitmap_ipmac_do_list(struct sk_buff *skb, const struct bitmap_ipmac *map,
193 u32 id) 181 u32 id, size_t dsize)
194{ 182{
195 const struct bitmap_ipmac_elem *elem = 183 const struct bitmap_ipmac_elem *elem =
196 get_elem(map->extensions, id, map->dsize); 184 get_elem(map->extensions, id, dsize);
197 185
198 return nla_put_ipaddr4(skb, IPSET_ATTR_IP, 186 return nla_put_ipaddr4(skb, IPSET_ATTR_IP,
199 htonl(map->first_ip + id)) || 187 htonl(map->first_ip + id)) ||
@@ -216,7 +204,7 @@ bitmap_ipmac_kadt(struct ip_set *set, const struct sk_buff *skb,
216 struct bitmap_ipmac *map = set->data; 204 struct bitmap_ipmac *map = set->data;
217 ipset_adtfn adtfn = set->variant->adt[adt]; 205 ipset_adtfn adtfn = set->variant->adt[adt];
218 struct bitmap_ipmac_adt_elem e = {}; 206 struct bitmap_ipmac_adt_elem e = {};
219 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, map); 207 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
220 u32 ip; 208 u32 ip;
221 209
222 /* MAC can be src only */ 210 /* MAC can be src only */
@@ -245,8 +233,8 @@ bitmap_ipmac_uadt(struct ip_set *set, struct nlattr *tb[],
245 const struct bitmap_ipmac *map = set->data; 233 const struct bitmap_ipmac *map = set->data;
246 ipset_adtfn adtfn = set->variant->adt[adt]; 234 ipset_adtfn adtfn = set->variant->adt[adt];
247 struct bitmap_ipmac_adt_elem e = {}; 235 struct bitmap_ipmac_adt_elem e = {};
248 struct ip_set_ext ext = IP_SET_INIT_UEXT(map); 236 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
249 u32 ip; 237 u32 ip = 0;
250 int ret = 0; 238 int ret = 0;
251 239
252 if (unlikely(!tb[IPSET_ATTR_IP] || 240 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -285,43 +273,12 @@ bitmap_ipmac_same_set(const struct ip_set *a, const struct ip_set *b)
285 273
286 return x->first_ip == y->first_ip && 274 return x->first_ip == y->first_ip &&
287 x->last_ip == y->last_ip && 275 x->last_ip == y->last_ip &&
288 x->timeout == y->timeout && 276 a->timeout == b->timeout &&
289 a->extensions == b->extensions; 277 a->extensions == b->extensions;
290} 278}
291 279
292/* Plain variant */ 280/* Plain variant */
293 281
294/* Timeout variant */
295
296struct bitmap_ipmact_elem {
297 struct {
298 unsigned char ether[ETH_ALEN];
299 unsigned char filled;
300 } __attribute__ ((aligned));
301 unsigned long timeout;
302};
303
304/* Plain variant with counter */
305
306struct bitmap_ipmacc_elem {
307 struct {
308 unsigned char ether[ETH_ALEN];
309 unsigned char filled;
310 } __attribute__ ((aligned));
311 struct ip_set_counter counter;
312};
313
314/* Timeout variant with counter */
315
316struct bitmap_ipmacct_elem {
317 struct {
318 unsigned char ether[ETH_ALEN];
319 unsigned char filled;
320 } __attribute__ ((aligned));
321 unsigned long timeout;
322 struct ip_set_counter counter;
323};
324
325#include "ip_set_bitmap_gen.h" 282#include "ip_set_bitmap_gen.h"
326 283
327/* Create bitmap:ip,mac type of sets */ 284/* Create bitmap:ip,mac type of sets */
@@ -330,11 +287,11 @@ static bool
330init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map, 287init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
331 u32 first_ip, u32 last_ip, u32 elements) 288 u32 first_ip, u32 last_ip, u32 elements)
332{ 289{
333 map->members = ip_set_alloc((last_ip - first_ip + 1) * map->dsize); 290 map->members = ip_set_alloc(map->memsize);
334 if (!map->members) 291 if (!map->members)
335 return false; 292 return false;
336 if (map->dsize) { 293 if (set->dsize) {
337 map->extensions = ip_set_alloc(map->dsize * elements); 294 map->extensions = ip_set_alloc(set->dsize * elements);
338 if (!map->extensions) { 295 if (!map->extensions) {
339 kfree(map->members); 296 kfree(map->members);
340 return false; 297 return false;
@@ -343,7 +300,7 @@ init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
343 map->first_ip = first_ip; 300 map->first_ip = first_ip;
344 map->last_ip = last_ip; 301 map->last_ip = last_ip;
345 map->elements = elements; 302 map->elements = elements;
346 map->timeout = IPSET_NO_TIMEOUT; 303 set->timeout = IPSET_NO_TIMEOUT;
347 304
348 set->data = map; 305 set->data = map;
349 set->family = NFPROTO_IPV4; 306 set->family = NFPROTO_IPV4;
@@ -352,10 +309,10 @@ init_map_ipmac(struct ip_set *set, struct bitmap_ipmac *map,
352} 309}
353 310
354static int 311static int
355bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[], 312bitmap_ipmac_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
356 u32 flags) 313 u32 flags)
357{ 314{
358 u32 first_ip, last_ip, cadt_flags = 0; 315 u32 first_ip = 0, last_ip = 0;
359 u64 elements; 316 u64 elements;
360 struct bitmap_ipmac *map; 317 struct bitmap_ipmac *map;
361 int ret; 318 int ret;
@@ -399,57 +356,15 @@ bitmap_ipmac_create(struct ip_set *set, struct nlattr *tb[],
399 356
400 map->memsize = bitmap_bytes(0, elements - 1); 357 map->memsize = bitmap_bytes(0, elements - 1);
401 set->variant = &bitmap_ipmac; 358 set->variant = &bitmap_ipmac;
402 if (tb[IPSET_ATTR_CADT_FLAGS]) 359 set->dsize = ip_set_elem_len(set, tb,
403 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 360 sizeof(struct bitmap_ipmac_elem));
404 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) { 361 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
405 set->extensions |= IPSET_EXT_COUNTER; 362 kfree(map);
406 if (tb[IPSET_ATTR_TIMEOUT]) { 363 return -ENOMEM;
407 map->dsize = sizeof(struct bitmap_ipmacct_elem); 364 }
408 map->offset[IPSET_OFFSET_TIMEOUT] = 365 if (tb[IPSET_ATTR_TIMEOUT]) {
409 offsetof(struct bitmap_ipmacct_elem, timeout); 366 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
410 map->offset[IPSET_OFFSET_COUNTER] =
411 offsetof(struct bitmap_ipmacct_elem, counter);
412
413 if (!init_map_ipmac(set, map, first_ip, last_ip,
414 elements)) {
415 kfree(map);
416 return -ENOMEM;
417 }
418 map->timeout = ip_set_timeout_uget(
419 tb[IPSET_ATTR_TIMEOUT]);
420 set->extensions |= IPSET_EXT_TIMEOUT;
421 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
422 } else {
423 map->dsize = sizeof(struct bitmap_ipmacc_elem);
424 map->offset[IPSET_OFFSET_COUNTER] =
425 offsetof(struct bitmap_ipmacc_elem, counter);
426
427 if (!init_map_ipmac(set, map, first_ip, last_ip,
428 elements)) {
429 kfree(map);
430 return -ENOMEM;
431 }
432 }
433 } else if (tb[IPSET_ATTR_TIMEOUT]) {
434 map->dsize = sizeof(struct bitmap_ipmact_elem);
435 map->offset[IPSET_OFFSET_TIMEOUT] =
436 offsetof(struct bitmap_ipmact_elem, timeout);
437
438 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
439 kfree(map);
440 return -ENOMEM;
441 }
442 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
443 set->extensions |= IPSET_EXT_TIMEOUT;
444 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc); 367 bitmap_ipmac_gc_init(set, bitmap_ipmac_gc);
445 } else {
446 map->dsize = sizeof(struct bitmap_ipmac_elem);
447
448 if (!init_map_ipmac(set, map, first_ip, last_ip, elements)) {
449 kfree(map);
450 return -ENOMEM;
451 }
452 set->variant = &bitmap_ipmac;
453 } 368 }
454 return 0; 369 return 0;
455} 370}
@@ -460,8 +375,8 @@ static struct ip_set_type bitmap_ipmac_type = {
460 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC, 375 .features = IPSET_TYPE_IP | IPSET_TYPE_MAC,
461 .dimension = IPSET_DIM_TWO, 376 .dimension = IPSET_DIM_TWO,
462 .family = NFPROTO_IPV4, 377 .family = NFPROTO_IPV4,
463 .revision_min = REVISION_MIN, 378 .revision_min = IPSET_TYPE_REV_MIN,
464 .revision_max = REVISION_MAX, 379 .revision_max = IPSET_TYPE_REV_MAX,
465 .create = bitmap_ipmac_create, 380 .create = bitmap_ipmac_create,
466 .create_policy = { 381 .create_policy = {
467 [IPSET_ATTR_IP] = { .type = NLA_NESTED }, 382 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
@@ -478,6 +393,7 @@ static struct ip_set_type bitmap_ipmac_type = {
478 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 393 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
479 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 394 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
480 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 395 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
396 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
481 }, 397 },
482 .me = THIS_MODULE, 398 .me = THIS_MODULE,
483}; 399};
diff --git a/net/netfilter/ipset/ip_set_bitmap_port.c b/net/netfilter/ipset/ip_set_bitmap_port.c
index 8207d1fda528..cf99676e69f8 100644
--- a/net/netfilter/ipset/ip_set_bitmap_port.c
+++ b/net/netfilter/ipset/ip_set_bitmap_port.c
@@ -20,12 +20,13 @@
20#include <linux/netfilter/ipset/ip_set_bitmap.h> 20#include <linux/netfilter/ipset/ip_set_bitmap.h>
21#include <linux/netfilter/ipset/ip_set_getport.h> 21#include <linux/netfilter/ipset/ip_set_getport.h>
22 22
23#define REVISION_MIN 0 23#define IPSET_TYPE_REV_MIN 0
24#define REVISION_MAX 1 /* Counter support added */ 24/* 1 Counter support added */
25#define IPSET_TYPE_REV_MAX 2 /* Comment support added */
25 26
26MODULE_LICENSE("GPL"); 27MODULE_LICENSE("GPL");
27MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 28MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
28IP_SET_MODULE_DESC("bitmap:port", REVISION_MIN, REVISION_MAX); 29IP_SET_MODULE_DESC("bitmap:port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
29MODULE_ALIAS("ip_set_bitmap:port"); 30MODULE_ALIAS("ip_set_bitmap:port");
30 31
31#define MTYPE bitmap_port 32#define MTYPE bitmap_port
@@ -38,9 +39,6 @@ struct bitmap_port {
38 u16 last_port; /* host byte order, included in range */ 39 u16 last_port; /* host byte order, included in range */
39 u32 elements; /* number of max elements in the set */ 40 u32 elements; /* number of max elements in the set */
40 size_t memsize; /* members size */ 41 size_t memsize; /* members size */
41 size_t dsize; /* extensions struct size */
42 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
43 u32 timeout; /* timeout parameter */
44 struct timer_list gc; /* garbage collection */ 42 struct timer_list gc; /* garbage collection */
45}; 43};
46 44
@@ -59,20 +57,20 @@ port_to_id(const struct bitmap_port *m, u16 port)
59 57
60static inline int 58static inline int
61bitmap_port_do_test(const struct bitmap_port_adt_elem *e, 59bitmap_port_do_test(const struct bitmap_port_adt_elem *e,
62 const struct bitmap_port *map) 60 const struct bitmap_port *map, size_t dsize)
63{ 61{
64 return !!test_bit(e->id, map->members); 62 return !!test_bit(e->id, map->members);
65} 63}
66 64
67static inline int 65static inline int
68bitmap_port_gc_test(u16 id, const struct bitmap_port *map) 66bitmap_port_gc_test(u16 id, const struct bitmap_port *map, size_t dsize)
69{ 67{
70 return !!test_bit(id, map->members); 68 return !!test_bit(id, map->members);
71} 69}
72 70
73static inline int 71static inline int
74bitmap_port_do_add(const struct bitmap_port_adt_elem *e, 72bitmap_port_do_add(const struct bitmap_port_adt_elem *e,
75 struct bitmap_port *map, u32 flags) 73 struct bitmap_port *map, u32 flags, size_t dsize)
76{ 74{
77 return !!test_and_set_bit(e->id, map->members); 75 return !!test_and_set_bit(e->id, map->members);
78} 76}
@@ -85,7 +83,8 @@ bitmap_port_do_del(const struct bitmap_port_adt_elem *e,
85} 83}
86 84
87static inline int 85static inline int
88bitmap_port_do_list(struct sk_buff *skb, const struct bitmap_port *map, u32 id) 86bitmap_port_do_list(struct sk_buff *skb, const struct bitmap_port *map, u32 id,
87 size_t dsize)
89{ 88{
90 return nla_put_net16(skb, IPSET_ATTR_PORT, 89 return nla_put_net16(skb, IPSET_ATTR_PORT,
91 htons(map->first_port + id)); 90 htons(map->first_port + id));
@@ -106,7 +105,7 @@ bitmap_port_kadt(struct ip_set *set, const struct sk_buff *skb,
106 struct bitmap_port *map = set->data; 105 struct bitmap_port *map = set->data;
107 ipset_adtfn adtfn = set->variant->adt[adt]; 106 ipset_adtfn adtfn = set->variant->adt[adt];
108 struct bitmap_port_adt_elem e = {}; 107 struct bitmap_port_adt_elem e = {};
109 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, map); 108 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
110 __be16 __port; 109 __be16 __port;
111 u16 port = 0; 110 u16 port = 0;
112 111
@@ -131,7 +130,7 @@ bitmap_port_uadt(struct ip_set *set, struct nlattr *tb[],
131 struct bitmap_port *map = set->data; 130 struct bitmap_port *map = set->data;
132 ipset_adtfn adtfn = set->variant->adt[adt]; 131 ipset_adtfn adtfn = set->variant->adt[adt];
133 struct bitmap_port_adt_elem e = {}; 132 struct bitmap_port_adt_elem e = {};
134 struct ip_set_ext ext = IP_SET_INIT_UEXT(map); 133 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
135 u32 port; /* wraparound */ 134 u32 port; /* wraparound */
136 u16 port_to; 135 u16 port_to;
137 int ret = 0; 136 int ret = 0;
@@ -191,7 +190,7 @@ bitmap_port_same_set(const struct ip_set *a, const struct ip_set *b)
191 190
192 return x->first_port == y->first_port && 191 return x->first_port == y->first_port &&
193 x->last_port == y->last_port && 192 x->last_port == y->last_port &&
194 x->timeout == y->timeout && 193 a->timeout == b->timeout &&
195 a->extensions == b->extensions; 194 a->extensions == b->extensions;
196} 195}
197 196
@@ -200,25 +199,6 @@ bitmap_port_same_set(const struct ip_set *a, const struct ip_set *b)
200struct bitmap_port_elem { 199struct bitmap_port_elem {
201}; 200};
202 201
203/* Timeout variant */
204
205struct bitmap_portt_elem {
206 unsigned long timeout;
207};
208
209/* Plain variant with counter */
210
211struct bitmap_portc_elem {
212 struct ip_set_counter counter;
213};
214
215/* Timeout variant with counter */
216
217struct bitmap_portct_elem {
218 unsigned long timeout;
219 struct ip_set_counter counter;
220};
221
222#include "ip_set_bitmap_gen.h" 202#include "ip_set_bitmap_gen.h"
223 203
224/* Create bitmap:ip type of sets */ 204/* Create bitmap:ip type of sets */
@@ -230,8 +210,8 @@ init_map_port(struct ip_set *set, struct bitmap_port *map,
230 map->members = ip_set_alloc(map->memsize); 210 map->members = ip_set_alloc(map->memsize);
231 if (!map->members) 211 if (!map->members)
232 return false; 212 return false;
233 if (map->dsize) { 213 if (set->dsize) {
234 map->extensions = ip_set_alloc(map->dsize * map->elements); 214 map->extensions = ip_set_alloc(set->dsize * map->elements);
235 if (!map->extensions) { 215 if (!map->extensions) {
236 kfree(map->members); 216 kfree(map->members);
237 return false; 217 return false;
@@ -239,7 +219,7 @@ init_map_port(struct ip_set *set, struct bitmap_port *map,
239 } 219 }
240 map->first_port = first_port; 220 map->first_port = first_port;
241 map->last_port = last_port; 221 map->last_port = last_port;
242 map->timeout = IPSET_NO_TIMEOUT; 222 set->timeout = IPSET_NO_TIMEOUT;
243 223
244 set->data = map; 224 set->data = map;
245 set->family = NFPROTO_UNSPEC; 225 set->family = NFPROTO_UNSPEC;
@@ -248,11 +228,11 @@ init_map_port(struct ip_set *set, struct bitmap_port *map,
248} 228}
249 229
250static int 230static int
251bitmap_port_create(struct ip_set *set, struct nlattr *tb[], u32 flags) 231bitmap_port_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
232 u32 flags)
252{ 233{
253 struct bitmap_port *map; 234 struct bitmap_port *map;
254 u16 first_port, last_port; 235 u16 first_port, last_port;
255 u32 cadt_flags = 0;
256 236
257 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_PORT) || 237 if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
258 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT_TO) || 238 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT_TO) ||
@@ -274,55 +254,16 @@ bitmap_port_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
274 return -ENOMEM; 254 return -ENOMEM;
275 255
276 map->elements = last_port - first_port + 1; 256 map->elements = last_port - first_port + 1;
277 map->memsize = map->elements * sizeof(unsigned long); 257 map->memsize = bitmap_bytes(0, map->elements);
278 set->variant = &bitmap_port; 258 set->variant = &bitmap_port;
279 if (tb[IPSET_ATTR_CADT_FLAGS]) 259 set->dsize = ip_set_elem_len(set, tb, 0);
280 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]); 260 if (!init_map_port(set, map, first_port, last_port)) {
281 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) { 261 kfree(map);
282 set->extensions |= IPSET_EXT_COUNTER; 262 return -ENOMEM;
283 if (tb[IPSET_ATTR_TIMEOUT]) { 263 }
284 map->dsize = sizeof(struct bitmap_portct_elem); 264 if (tb[IPSET_ATTR_TIMEOUT]) {
285 map->offset[IPSET_OFFSET_TIMEOUT] = 265 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
286 offsetof(struct bitmap_portct_elem, timeout);
287 map->offset[IPSET_OFFSET_COUNTER] =
288 offsetof(struct bitmap_portct_elem, counter);
289 if (!init_map_port(set, map, first_port, last_port)) {
290 kfree(map);
291 return -ENOMEM;
292 }
293
294 map->timeout =
295 ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
296 set->extensions |= IPSET_EXT_TIMEOUT;
297 bitmap_port_gc_init(set, bitmap_port_gc);
298 } else {
299 map->dsize = sizeof(struct bitmap_portc_elem);
300 map->offset[IPSET_OFFSET_COUNTER] =
301 offsetof(struct bitmap_portc_elem, counter);
302 if (!init_map_port(set, map, first_port, last_port)) {
303 kfree(map);
304 return -ENOMEM;
305 }
306 }
307 } else if (tb[IPSET_ATTR_TIMEOUT]) {
308 map->dsize = sizeof(struct bitmap_portt_elem);
309 map->offset[IPSET_OFFSET_TIMEOUT] =
310 offsetof(struct bitmap_portt_elem, timeout);
311 if (!init_map_port(set, map, first_port, last_port)) {
312 kfree(map);
313 return -ENOMEM;
314 }
315
316 map->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
317 set->extensions |= IPSET_EXT_TIMEOUT;
318 bitmap_port_gc_init(set, bitmap_port_gc); 266 bitmap_port_gc_init(set, bitmap_port_gc);
319 } else {
320 map->dsize = 0;
321 if (!init_map_port(set, map, first_port, last_port)) {
322 kfree(map);
323 return -ENOMEM;
324 }
325
326 } 267 }
327 return 0; 268 return 0;
328} 269}
@@ -333,8 +274,8 @@ static struct ip_set_type bitmap_port_type = {
333 .features = IPSET_TYPE_PORT, 274 .features = IPSET_TYPE_PORT,
334 .dimension = IPSET_DIM_ONE, 275 .dimension = IPSET_DIM_ONE,
335 .family = NFPROTO_UNSPEC, 276 .family = NFPROTO_UNSPEC,
336 .revision_min = REVISION_MIN, 277 .revision_min = IPSET_TYPE_REV_MIN,
337 .revision_max = REVISION_MAX, 278 .revision_max = IPSET_TYPE_REV_MAX,
338 .create = bitmap_port_create, 279 .create = bitmap_port_create,
339 .create_policy = { 280 .create_policy = {
340 [IPSET_ATTR_PORT] = { .type = NLA_U16 }, 281 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
@@ -349,6 +290,7 @@ static struct ip_set_type bitmap_port_type = {
349 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 290 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
350 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 291 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
351 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 292 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
293 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
352 }, 294 },
353 .me = THIS_MODULE, 295 .me = THIS_MODULE,
354}; 296};
diff --git a/net/netfilter/ipset/ip_set_core.c b/net/netfilter/ipset/ip_set_core.c
index f2e30fb31e78..bac7e01df67f 100644
--- a/net/netfilter/ipset/ip_set_core.c
+++ b/net/netfilter/ipset/ip_set_core.c
@@ -17,6 +17,8 @@
17#include <linux/spinlock.h> 17#include <linux/spinlock.h>
18#include <linux/rculist.h> 18#include <linux/rculist.h>
19#include <net/netlink.h> 19#include <net/netlink.h>
20#include <net/net_namespace.h>
21#include <net/netns/generic.h>
20 22
21#include <linux/netfilter.h> 23#include <linux/netfilter.h>
22#include <linux/netfilter/x_tables.h> 24#include <linux/netfilter/x_tables.h>
@@ -27,8 +29,17 @@ static LIST_HEAD(ip_set_type_list); /* all registered set types */
27static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */ 29static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
28static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */ 30static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
29 31
30static struct ip_set * __rcu *ip_set_list; /* all individual sets */ 32struct ip_set_net {
31static ip_set_id_t ip_set_max = CONFIG_IP_SET_MAX; /* max number of sets */ 33 struct ip_set * __rcu *ip_set_list; /* all individual sets */
34 ip_set_id_t ip_set_max; /* max number of sets */
35 int is_deleted; /* deleted by ip_set_net_exit */
36};
37static int ip_set_net_id __read_mostly;
38
39static inline struct ip_set_net *ip_set_pernet(struct net *net)
40{
41 return net_generic(net, ip_set_net_id);
42}
32 43
33#define IP_SET_INC 64 44#define IP_SET_INC 64
34#define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0) 45#define STREQ(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
@@ -45,8 +56,8 @@ MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
45/* When the nfnl mutex is held: */ 56/* When the nfnl mutex is held: */
46#define nfnl_dereference(p) \ 57#define nfnl_dereference(p) \
47 rcu_dereference_protected(p, 1) 58 rcu_dereference_protected(p, 1)
48#define nfnl_set(id) \ 59#define nfnl_set(inst, id) \
49 nfnl_dereference(ip_set_list)[id] 60 nfnl_dereference((inst)->ip_set_list)[id]
50 61
51/* 62/*
52 * The set types are implemented in modules and registered set types 63 * The set types are implemented in modules and registered set types
@@ -315,6 +326,60 @@ ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
315} 326}
316EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6); 327EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
317 328
329typedef void (*destroyer)(void *);
330/* ipset data extension types, in size order */
331
332const struct ip_set_ext_type ip_set_extensions[] = {
333 [IPSET_EXT_ID_COUNTER] = {
334 .type = IPSET_EXT_COUNTER,
335 .flag = IPSET_FLAG_WITH_COUNTERS,
336 .len = sizeof(struct ip_set_counter),
337 .align = __alignof__(struct ip_set_counter),
338 },
339 [IPSET_EXT_ID_TIMEOUT] = {
340 .type = IPSET_EXT_TIMEOUT,
341 .len = sizeof(unsigned long),
342 .align = __alignof__(unsigned long),
343 },
344 [IPSET_EXT_ID_COMMENT] = {
345 .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
346 .flag = IPSET_FLAG_WITH_COMMENT,
347 .len = sizeof(struct ip_set_comment),
348 .align = __alignof__(struct ip_set_comment),
349 .destroy = (destroyer) ip_set_comment_free,
350 },
351};
352EXPORT_SYMBOL_GPL(ip_set_extensions);
353
354static inline bool
355add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
356{
357 return ip_set_extensions[id].flag ?
358 (flags & ip_set_extensions[id].flag) :
359 !!tb[IPSET_ATTR_TIMEOUT];
360}
361
362size_t
363ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len)
364{
365 enum ip_set_ext_id id;
366 size_t offset = 0;
367 u32 cadt_flags = 0;
368
369 if (tb[IPSET_ATTR_CADT_FLAGS])
370 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
371 for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
372 if (!add_extension(id, cadt_flags, tb))
373 continue;
374 offset += ALIGN(len + offset, ip_set_extensions[id].align);
375 set->offset[id] = offset;
376 set->extensions |= ip_set_extensions[id].type;
377 offset += ip_set_extensions[id].len;
378 }
379 return len + offset;
380}
381EXPORT_SYMBOL_GPL(ip_set_elem_len);
382
318int 383int
319ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[], 384ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
320 struct ip_set_ext *ext) 385 struct ip_set_ext *ext)
@@ -334,6 +399,12 @@ ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
334 ext->packets = be64_to_cpu(nla_get_be64( 399 ext->packets = be64_to_cpu(nla_get_be64(
335 tb[IPSET_ATTR_PACKETS])); 400 tb[IPSET_ATTR_PACKETS]));
336 } 401 }
402 if (tb[IPSET_ATTR_COMMENT]) {
403 if (!(set->extensions & IPSET_EXT_COMMENT))
404 return -IPSET_ERR_COMMENT;
405 ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
406 }
407
337 return 0; 408 return 0;
338} 409}
339EXPORT_SYMBOL_GPL(ip_set_get_extensions); 410EXPORT_SYMBOL_GPL(ip_set_get_extensions);
@@ -374,13 +445,14 @@ __ip_set_put(struct ip_set *set)
374 */ 445 */
375 446
376static inline struct ip_set * 447static inline struct ip_set *
377ip_set_rcu_get(ip_set_id_t index) 448ip_set_rcu_get(struct net *net, ip_set_id_t index)
378{ 449{
379 struct ip_set *set; 450 struct ip_set *set;
451 struct ip_set_net *inst = ip_set_pernet(net);
380 452
381 rcu_read_lock(); 453 rcu_read_lock();
382 /* ip_set_list itself needs to be protected */ 454 /* ip_set_list itself needs to be protected */
383 set = rcu_dereference(ip_set_list)[index]; 455 set = rcu_dereference(inst->ip_set_list)[index];
384 rcu_read_unlock(); 456 rcu_read_unlock();
385 457
386 return set; 458 return set;
@@ -390,7 +462,8 @@ int
390ip_set_test(ip_set_id_t index, const struct sk_buff *skb, 462ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
391 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 463 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
392{ 464{
393 struct ip_set *set = ip_set_rcu_get(index); 465 struct ip_set *set = ip_set_rcu_get(
466 dev_net(par->in ? par->in : par->out), index);
394 int ret = 0; 467 int ret = 0;
395 468
396 BUG_ON(set == NULL); 469 BUG_ON(set == NULL);
@@ -428,7 +501,8 @@ int
428ip_set_add(ip_set_id_t index, const struct sk_buff *skb, 501ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
429 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 502 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
430{ 503{
431 struct ip_set *set = ip_set_rcu_get(index); 504 struct ip_set *set = ip_set_rcu_get(
505 dev_net(par->in ? par->in : par->out), index);
432 int ret; 506 int ret;
433 507
434 BUG_ON(set == NULL); 508 BUG_ON(set == NULL);
@@ -450,7 +524,8 @@ int
450ip_set_del(ip_set_id_t index, const struct sk_buff *skb, 524ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
451 const struct xt_action_param *par, struct ip_set_adt_opt *opt) 525 const struct xt_action_param *par, struct ip_set_adt_opt *opt)
452{ 526{
453 struct ip_set *set = ip_set_rcu_get(index); 527 struct ip_set *set = ip_set_rcu_get(
528 dev_net(par->in ? par->in : par->out), index);
454 int ret = 0; 529 int ret = 0;
455 530
456 BUG_ON(set == NULL); 531 BUG_ON(set == NULL);
@@ -474,14 +549,15 @@ EXPORT_SYMBOL_GPL(ip_set_del);
474 * 549 *
475 */ 550 */
476ip_set_id_t 551ip_set_id_t
477ip_set_get_byname(const char *name, struct ip_set **set) 552ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
478{ 553{
479 ip_set_id_t i, index = IPSET_INVALID_ID; 554 ip_set_id_t i, index = IPSET_INVALID_ID;
480 struct ip_set *s; 555 struct ip_set *s;
556 struct ip_set_net *inst = ip_set_pernet(net);
481 557
482 rcu_read_lock(); 558 rcu_read_lock();
483 for (i = 0; i < ip_set_max; i++) { 559 for (i = 0; i < inst->ip_set_max; i++) {
484 s = rcu_dereference(ip_set_list)[i]; 560 s = rcu_dereference(inst->ip_set_list)[i];
485 if (s != NULL && STREQ(s->name, name)) { 561 if (s != NULL && STREQ(s->name, name)) {
486 __ip_set_get(s); 562 __ip_set_get(s);
487 index = i; 563 index = i;
@@ -501,17 +577,26 @@ EXPORT_SYMBOL_GPL(ip_set_get_byname);
501 * to be valid, after calling this function. 577 * to be valid, after calling this function.
502 * 578 *
503 */ 579 */
504void 580
505ip_set_put_byindex(ip_set_id_t index) 581static inline void
582__ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
506{ 583{
507 struct ip_set *set; 584 struct ip_set *set;
508 585
509 rcu_read_lock(); 586 rcu_read_lock();
510 set = rcu_dereference(ip_set_list)[index]; 587 set = rcu_dereference(inst->ip_set_list)[index];
511 if (set != NULL) 588 if (set != NULL)
512 __ip_set_put(set); 589 __ip_set_put(set);
513 rcu_read_unlock(); 590 rcu_read_unlock();
514} 591}
592
593void
594ip_set_put_byindex(struct net *net, ip_set_id_t index)
595{
596 struct ip_set_net *inst = ip_set_pernet(net);
597
598 __ip_set_put_byindex(inst, index);
599}
515EXPORT_SYMBOL_GPL(ip_set_put_byindex); 600EXPORT_SYMBOL_GPL(ip_set_put_byindex);
516 601
517/* 602/*
@@ -522,9 +607,9 @@ EXPORT_SYMBOL_GPL(ip_set_put_byindex);
522 * 607 *
523 */ 608 */
524const char * 609const char *
525ip_set_name_byindex(ip_set_id_t index) 610ip_set_name_byindex(struct net *net, ip_set_id_t index)
526{ 611{
527 const struct ip_set *set = ip_set_rcu_get(index); 612 const struct ip_set *set = ip_set_rcu_get(net, index);
528 613
529 BUG_ON(set == NULL); 614 BUG_ON(set == NULL);
530 BUG_ON(set->ref == 0); 615 BUG_ON(set->ref == 0);
@@ -546,14 +631,15 @@ EXPORT_SYMBOL_GPL(ip_set_name_byindex);
546 * The nfnl mutex is used in the function. 631 * The nfnl mutex is used in the function.
547 */ 632 */
548ip_set_id_t 633ip_set_id_t
549ip_set_nfnl_get(const char *name) 634ip_set_nfnl_get(struct net *net, const char *name)
550{ 635{
551 ip_set_id_t i, index = IPSET_INVALID_ID; 636 ip_set_id_t i, index = IPSET_INVALID_ID;
552 struct ip_set *s; 637 struct ip_set *s;
638 struct ip_set_net *inst = ip_set_pernet(net);
553 639
554 nfnl_lock(NFNL_SUBSYS_IPSET); 640 nfnl_lock(NFNL_SUBSYS_IPSET);
555 for (i = 0; i < ip_set_max; i++) { 641 for (i = 0; i < inst->ip_set_max; i++) {
556 s = nfnl_set(i); 642 s = nfnl_set(inst, i);
557 if (s != NULL && STREQ(s->name, name)) { 643 if (s != NULL && STREQ(s->name, name)) {
558 __ip_set_get(s); 644 __ip_set_get(s);
559 index = i; 645 index = i;
@@ -573,15 +659,16 @@ EXPORT_SYMBOL_GPL(ip_set_nfnl_get);
573 * The nfnl mutex is used in the function. 659 * The nfnl mutex is used in the function.
574 */ 660 */
575ip_set_id_t 661ip_set_id_t
576ip_set_nfnl_get_byindex(ip_set_id_t index) 662ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
577{ 663{
578 struct ip_set *set; 664 struct ip_set *set;
665 struct ip_set_net *inst = ip_set_pernet(net);
579 666
580 if (index > ip_set_max) 667 if (index > inst->ip_set_max)
581 return IPSET_INVALID_ID; 668 return IPSET_INVALID_ID;
582 669
583 nfnl_lock(NFNL_SUBSYS_IPSET); 670 nfnl_lock(NFNL_SUBSYS_IPSET);
584 set = nfnl_set(index); 671 set = nfnl_set(inst, index);
585 if (set) 672 if (set)
586 __ip_set_get(set); 673 __ip_set_get(set);
587 else 674 else
@@ -600,13 +687,17 @@ EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
600 * The nfnl mutex is used in the function. 687 * The nfnl mutex is used in the function.
601 */ 688 */
602void 689void
603ip_set_nfnl_put(ip_set_id_t index) 690ip_set_nfnl_put(struct net *net, ip_set_id_t index)
604{ 691{
605 struct ip_set *set; 692 struct ip_set *set;
693 struct ip_set_net *inst = ip_set_pernet(net);
694
606 nfnl_lock(NFNL_SUBSYS_IPSET); 695 nfnl_lock(NFNL_SUBSYS_IPSET);
607 set = nfnl_set(index); 696 if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
608 if (set != NULL) 697 set = nfnl_set(inst, index);
609 __ip_set_put(set); 698 if (set != NULL)
699 __ip_set_put(set);
700 }
610 nfnl_unlock(NFNL_SUBSYS_IPSET); 701 nfnl_unlock(NFNL_SUBSYS_IPSET);
611} 702}
612EXPORT_SYMBOL_GPL(ip_set_nfnl_put); 703EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
@@ -664,14 +755,14 @@ static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
664}; 755};
665 756
666static struct ip_set * 757static struct ip_set *
667find_set_and_id(const char *name, ip_set_id_t *id) 758find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
668{ 759{
669 struct ip_set *set = NULL; 760 struct ip_set *set = NULL;
670 ip_set_id_t i; 761 ip_set_id_t i;
671 762
672 *id = IPSET_INVALID_ID; 763 *id = IPSET_INVALID_ID;
673 for (i = 0; i < ip_set_max; i++) { 764 for (i = 0; i < inst->ip_set_max; i++) {
674 set = nfnl_set(i); 765 set = nfnl_set(inst, i);
675 if (set != NULL && STREQ(set->name, name)) { 766 if (set != NULL && STREQ(set->name, name)) {
676 *id = i; 767 *id = i;
677 break; 768 break;
@@ -681,22 +772,23 @@ find_set_and_id(const char *name, ip_set_id_t *id)
681} 772}
682 773
683static inline struct ip_set * 774static inline struct ip_set *
684find_set(const char *name) 775find_set(struct ip_set_net *inst, const char *name)
685{ 776{
686 ip_set_id_t id; 777 ip_set_id_t id;
687 778
688 return find_set_and_id(name, &id); 779 return find_set_and_id(inst, name, &id);
689} 780}
690 781
691static int 782static int
692find_free_id(const char *name, ip_set_id_t *index, struct ip_set **set) 783find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
784 struct ip_set **set)
693{ 785{
694 struct ip_set *s; 786 struct ip_set *s;
695 ip_set_id_t i; 787 ip_set_id_t i;
696 788
697 *index = IPSET_INVALID_ID; 789 *index = IPSET_INVALID_ID;
698 for (i = 0; i < ip_set_max; i++) { 790 for (i = 0; i < inst->ip_set_max; i++) {
699 s = nfnl_set(i); 791 s = nfnl_set(inst, i);
700 if (s == NULL) { 792 if (s == NULL) {
701 if (*index == IPSET_INVALID_ID) 793 if (*index == IPSET_INVALID_ID)
702 *index = i; 794 *index = i;
@@ -725,6 +817,8 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
725 const struct nlmsghdr *nlh, 817 const struct nlmsghdr *nlh,
726 const struct nlattr * const attr[]) 818 const struct nlattr * const attr[])
727{ 819{
820 struct net *net = sock_net(ctnl);
821 struct ip_set_net *inst = ip_set_pernet(net);
728 struct ip_set *set, *clash = NULL; 822 struct ip_set *set, *clash = NULL;
729 ip_set_id_t index = IPSET_INVALID_ID; 823 ip_set_id_t index = IPSET_INVALID_ID;
730 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {}; 824 struct nlattr *tb[IPSET_ATTR_CREATE_MAX+1] = {};
@@ -783,7 +877,7 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
783 goto put_out; 877 goto put_out;
784 } 878 }
785 879
786 ret = set->type->create(set, tb, flags); 880 ret = set->type->create(net, set, tb, flags);
787 if (ret != 0) 881 if (ret != 0)
788 goto put_out; 882 goto put_out;
789 883
@@ -794,7 +888,7 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
794 * by the nfnl mutex. Find the first free index in ip_set_list 888 * by the nfnl mutex. Find the first free index in ip_set_list
795 * and check clashing. 889 * and check clashing.
796 */ 890 */
797 ret = find_free_id(set->name, &index, &clash); 891 ret = find_free_id(inst, set->name, &index, &clash);
798 if (ret == -EEXIST) { 892 if (ret == -EEXIST) {
799 /* If this is the same set and requested, ignore error */ 893 /* If this is the same set and requested, ignore error */
800 if ((flags & IPSET_FLAG_EXIST) && 894 if ((flags & IPSET_FLAG_EXIST) &&
@@ -807,9 +901,9 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
807 goto cleanup; 901 goto cleanup;
808 } else if (ret == -IPSET_ERR_MAX_SETS) { 902 } else if (ret == -IPSET_ERR_MAX_SETS) {
809 struct ip_set **list, **tmp; 903 struct ip_set **list, **tmp;
810 ip_set_id_t i = ip_set_max + IP_SET_INC; 904 ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
811 905
812 if (i < ip_set_max || i == IPSET_INVALID_ID) 906 if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
813 /* Wraparound */ 907 /* Wraparound */
814 goto cleanup; 908 goto cleanup;
815 909
@@ -817,14 +911,14 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
817 if (!list) 911 if (!list)
818 goto cleanup; 912 goto cleanup;
819 /* nfnl mutex is held, both lists are valid */ 913 /* nfnl mutex is held, both lists are valid */
820 tmp = nfnl_dereference(ip_set_list); 914 tmp = nfnl_dereference(inst->ip_set_list);
821 memcpy(list, tmp, sizeof(struct ip_set *) * ip_set_max); 915 memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
822 rcu_assign_pointer(ip_set_list, list); 916 rcu_assign_pointer(inst->ip_set_list, list);
823 /* Make sure all current packets have passed through */ 917 /* Make sure all current packets have passed through */
824 synchronize_net(); 918 synchronize_net();
825 /* Use new list */ 919 /* Use new list */
826 index = ip_set_max; 920 index = inst->ip_set_max;
827 ip_set_max = i; 921 inst->ip_set_max = i;
828 kfree(tmp); 922 kfree(tmp);
829 ret = 0; 923 ret = 0;
830 } else if (ret) 924 } else if (ret)
@@ -834,7 +928,7 @@ ip_set_create(struct sock *ctnl, struct sk_buff *skb,
834 * Finally! Add our shiny new set to the list, and be done. 928 * Finally! Add our shiny new set to the list, and be done.
835 */ 929 */
836 pr_debug("create: '%s' created with index %u!\n", set->name, index); 930 pr_debug("create: '%s' created with index %u!\n", set->name, index);
837 nfnl_set(index) = set; 931 nfnl_set(inst, index) = set;
838 932
839 return ret; 933 return ret;
840 934
@@ -857,12 +951,12 @@ ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
857}; 951};
858 952
859static void 953static void
860ip_set_destroy_set(ip_set_id_t index) 954ip_set_destroy_set(struct ip_set_net *inst, ip_set_id_t index)
861{ 955{
862 struct ip_set *set = nfnl_set(index); 956 struct ip_set *set = nfnl_set(inst, index);
863 957
864 pr_debug("set: %s\n", set->name); 958 pr_debug("set: %s\n", set->name);
865 nfnl_set(index) = NULL; 959 nfnl_set(inst, index) = NULL;
866 960
867 /* Must call it without holding any lock */ 961 /* Must call it without holding any lock */
868 set->variant->destroy(set); 962 set->variant->destroy(set);
@@ -875,6 +969,7 @@ ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
875 const struct nlmsghdr *nlh, 969 const struct nlmsghdr *nlh,
876 const struct nlattr * const attr[]) 970 const struct nlattr * const attr[])
877{ 971{
972 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
878 struct ip_set *s; 973 struct ip_set *s;
879 ip_set_id_t i; 974 ip_set_id_t i;
880 int ret = 0; 975 int ret = 0;
@@ -894,21 +989,22 @@ ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
894 */ 989 */
895 read_lock_bh(&ip_set_ref_lock); 990 read_lock_bh(&ip_set_ref_lock);
896 if (!attr[IPSET_ATTR_SETNAME]) { 991 if (!attr[IPSET_ATTR_SETNAME]) {
897 for (i = 0; i < ip_set_max; i++) { 992 for (i = 0; i < inst->ip_set_max; i++) {
898 s = nfnl_set(i); 993 s = nfnl_set(inst, i);
899 if (s != NULL && s->ref) { 994 if (s != NULL && s->ref) {
900 ret = -IPSET_ERR_BUSY; 995 ret = -IPSET_ERR_BUSY;
901 goto out; 996 goto out;
902 } 997 }
903 } 998 }
904 read_unlock_bh(&ip_set_ref_lock); 999 read_unlock_bh(&ip_set_ref_lock);
905 for (i = 0; i < ip_set_max; i++) { 1000 for (i = 0; i < inst->ip_set_max; i++) {
906 s = nfnl_set(i); 1001 s = nfnl_set(inst, i);
907 if (s != NULL) 1002 if (s != NULL)
908 ip_set_destroy_set(i); 1003 ip_set_destroy_set(inst, i);
909 } 1004 }
910 } else { 1005 } else {
911 s = find_set_and_id(nla_data(attr[IPSET_ATTR_SETNAME]), &i); 1006 s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1007 &i);
912 if (s == NULL) { 1008 if (s == NULL) {
913 ret = -ENOENT; 1009 ret = -ENOENT;
914 goto out; 1010 goto out;
@@ -918,7 +1014,7 @@ ip_set_destroy(struct sock *ctnl, struct sk_buff *skb,
918 } 1014 }
919 read_unlock_bh(&ip_set_ref_lock); 1015 read_unlock_bh(&ip_set_ref_lock);
920 1016
921 ip_set_destroy_set(i); 1017 ip_set_destroy_set(inst, i);
922 } 1018 }
923 return 0; 1019 return 0;
924out: 1020out:
@@ -943,6 +1039,7 @@ ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
943 const struct nlmsghdr *nlh, 1039 const struct nlmsghdr *nlh,
944 const struct nlattr * const attr[]) 1040 const struct nlattr * const attr[])
945{ 1041{
1042 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
946 struct ip_set *s; 1043 struct ip_set *s;
947 ip_set_id_t i; 1044 ip_set_id_t i;
948 1045
@@ -950,13 +1047,13 @@ ip_set_flush(struct sock *ctnl, struct sk_buff *skb,
950 return -IPSET_ERR_PROTOCOL; 1047 return -IPSET_ERR_PROTOCOL;
951 1048
952 if (!attr[IPSET_ATTR_SETNAME]) { 1049 if (!attr[IPSET_ATTR_SETNAME]) {
953 for (i = 0; i < ip_set_max; i++) { 1050 for (i = 0; i < inst->ip_set_max; i++) {
954 s = nfnl_set(i); 1051 s = nfnl_set(inst, i);
955 if (s != NULL) 1052 if (s != NULL)
956 ip_set_flush_set(s); 1053 ip_set_flush_set(s);
957 } 1054 }
958 } else { 1055 } else {
959 s = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1056 s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
960 if (s == NULL) 1057 if (s == NULL)
961 return -ENOENT; 1058 return -ENOENT;
962 1059
@@ -982,6 +1079,7 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
982 const struct nlmsghdr *nlh, 1079 const struct nlmsghdr *nlh,
983 const struct nlattr * const attr[]) 1080 const struct nlattr * const attr[])
984{ 1081{
1082 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
985 struct ip_set *set, *s; 1083 struct ip_set *set, *s;
986 const char *name2; 1084 const char *name2;
987 ip_set_id_t i; 1085 ip_set_id_t i;
@@ -992,7 +1090,7 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
992 attr[IPSET_ATTR_SETNAME2] == NULL)) 1090 attr[IPSET_ATTR_SETNAME2] == NULL))
993 return -IPSET_ERR_PROTOCOL; 1091 return -IPSET_ERR_PROTOCOL;
994 1092
995 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1093 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
996 if (set == NULL) 1094 if (set == NULL)
997 return -ENOENT; 1095 return -ENOENT;
998 1096
@@ -1003,8 +1101,8 @@ ip_set_rename(struct sock *ctnl, struct sk_buff *skb,
1003 } 1101 }
1004 1102
1005 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]); 1103 name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
1006 for (i = 0; i < ip_set_max; i++) { 1104 for (i = 0; i < inst->ip_set_max; i++) {
1007 s = nfnl_set(i); 1105 s = nfnl_set(inst, i);
1008 if (s != NULL && STREQ(s->name, name2)) { 1106 if (s != NULL && STREQ(s->name, name2)) {
1009 ret = -IPSET_ERR_EXIST_SETNAME2; 1107 ret = -IPSET_ERR_EXIST_SETNAME2;
1010 goto out; 1108 goto out;
@@ -1031,6 +1129,7 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1031 const struct nlmsghdr *nlh, 1129 const struct nlmsghdr *nlh,
1032 const struct nlattr * const attr[]) 1130 const struct nlattr * const attr[])
1033{ 1131{
1132 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1034 struct ip_set *from, *to; 1133 struct ip_set *from, *to;
1035 ip_set_id_t from_id, to_id; 1134 ip_set_id_t from_id, to_id;
1036 char from_name[IPSET_MAXNAMELEN]; 1135 char from_name[IPSET_MAXNAMELEN];
@@ -1040,11 +1139,13 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1040 attr[IPSET_ATTR_SETNAME2] == NULL)) 1139 attr[IPSET_ATTR_SETNAME2] == NULL))
1041 return -IPSET_ERR_PROTOCOL; 1140 return -IPSET_ERR_PROTOCOL;
1042 1141
1043 from = find_set_and_id(nla_data(attr[IPSET_ATTR_SETNAME]), &from_id); 1142 from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
1143 &from_id);
1044 if (from == NULL) 1144 if (from == NULL)
1045 return -ENOENT; 1145 return -ENOENT;
1046 1146
1047 to = find_set_and_id(nla_data(attr[IPSET_ATTR_SETNAME2]), &to_id); 1147 to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
1148 &to_id);
1048 if (to == NULL) 1149 if (to == NULL)
1049 return -IPSET_ERR_EXIST_SETNAME2; 1150 return -IPSET_ERR_EXIST_SETNAME2;
1050 1151
@@ -1061,8 +1162,8 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1061 1162
1062 write_lock_bh(&ip_set_ref_lock); 1163 write_lock_bh(&ip_set_ref_lock);
1063 swap(from->ref, to->ref); 1164 swap(from->ref, to->ref);
1064 nfnl_set(from_id) = to; 1165 nfnl_set(inst, from_id) = to;
1065 nfnl_set(to_id) = from; 1166 nfnl_set(inst, to_id) = from;
1066 write_unlock_bh(&ip_set_ref_lock); 1167 write_unlock_bh(&ip_set_ref_lock);
1067 1168
1068 return 0; 1169 return 0;
@@ -1081,9 +1182,12 @@ ip_set_swap(struct sock *ctnl, struct sk_buff *skb,
1081static int 1182static int
1082ip_set_dump_done(struct netlink_callback *cb) 1183ip_set_dump_done(struct netlink_callback *cb)
1083{ 1184{
1084 if (cb->args[2]) { 1185 struct ip_set_net *inst = (struct ip_set_net *)cb->args[IPSET_CB_NET];
1085 pr_debug("release set %s\n", nfnl_set(cb->args[1])->name); 1186 if (cb->args[IPSET_CB_ARG0]) {
1086 ip_set_put_byindex((ip_set_id_t) cb->args[1]); 1187 pr_debug("release set %s\n",
1188 nfnl_set(inst, cb->args[IPSET_CB_INDEX])->name);
1189 __ip_set_put_byindex(inst,
1190 (ip_set_id_t) cb->args[IPSET_CB_INDEX]);
1087 } 1191 }
1088 return 0; 1192 return 0;
1089} 1193}
@@ -1101,7 +1205,7 @@ dump_attrs(struct nlmsghdr *nlh)
1101} 1205}
1102 1206
1103static int 1207static int
1104dump_init(struct netlink_callback *cb) 1208dump_init(struct netlink_callback *cb, struct ip_set_net *inst)
1105{ 1209{
1106 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb); 1210 struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
1107 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg)); 1211 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
@@ -1114,21 +1218,22 @@ dump_init(struct netlink_callback *cb)
1114 nla_parse(cda, IPSET_ATTR_CMD_MAX, 1218 nla_parse(cda, IPSET_ATTR_CMD_MAX,
1115 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy); 1219 attr, nlh->nlmsg_len - min_len, ip_set_setname_policy);
1116 1220
1117 /* cb->args[0] : dump single set/all sets 1221 /* cb->args[IPSET_CB_NET]: net namespace
1118 * [1] : set index 1222 * [IPSET_CB_DUMP]: dump single set/all sets
1119 * [..]: type specific 1223 * [IPSET_CB_INDEX]: set index
1224 * [IPSET_CB_ARG0]: type specific
1120 */ 1225 */
1121 1226
1122 if (cda[IPSET_ATTR_SETNAME]) { 1227 if (cda[IPSET_ATTR_SETNAME]) {
1123 struct ip_set *set; 1228 struct ip_set *set;
1124 1229
1125 set = find_set_and_id(nla_data(cda[IPSET_ATTR_SETNAME]), 1230 set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
1126 &index); 1231 &index);
1127 if (set == NULL) 1232 if (set == NULL)
1128 return -ENOENT; 1233 return -ENOENT;
1129 1234
1130 dump_type = DUMP_ONE; 1235 dump_type = DUMP_ONE;
1131 cb->args[1] = index; 1236 cb->args[IPSET_CB_INDEX] = index;
1132 } else 1237 } else
1133 dump_type = DUMP_ALL; 1238 dump_type = DUMP_ALL;
1134 1239
@@ -1136,7 +1241,8 @@ dump_init(struct netlink_callback *cb)
1136 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]); 1241 u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
1137 dump_type |= (f << 16); 1242 dump_type |= (f << 16);
1138 } 1243 }
1139 cb->args[0] = dump_type; 1244 cb->args[IPSET_CB_NET] = (unsigned long)inst;
1245 cb->args[IPSET_CB_DUMP] = dump_type;
1140 1246
1141 return 0; 1247 return 0;
1142} 1248}
@@ -1148,11 +1254,12 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1148 struct ip_set *set = NULL; 1254 struct ip_set *set = NULL;
1149 struct nlmsghdr *nlh = NULL; 1255 struct nlmsghdr *nlh = NULL;
1150 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0; 1256 unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
1257 struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
1151 u32 dump_type, dump_flags; 1258 u32 dump_type, dump_flags;
1152 int ret = 0; 1259 int ret = 0;
1153 1260
1154 if (!cb->args[0]) { 1261 if (!cb->args[IPSET_CB_DUMP]) {
1155 ret = dump_init(cb); 1262 ret = dump_init(cb, inst);
1156 if (ret < 0) { 1263 if (ret < 0) {
1157 nlh = nlmsg_hdr(cb->skb); 1264 nlh = nlmsg_hdr(cb->skb);
1158 /* We have to create and send the error message 1265 /* We have to create and send the error message
@@ -1163,18 +1270,19 @@ ip_set_dump_start(struct sk_buff *skb, struct netlink_callback *cb)
1163 } 1270 }
1164 } 1271 }
1165 1272
1166 if (cb->args[1] >= ip_set_max) 1273 if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
1167 goto out; 1274 goto out;
1168 1275
1169 dump_type = DUMP_TYPE(cb->args[0]); 1276 dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
1170 dump_flags = DUMP_FLAGS(cb->args[0]); 1277 dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
1171 max = dump_type == DUMP_ONE ? cb->args[1] + 1 : ip_set_max; 1278 max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
1279 : inst->ip_set_max;
1172dump_last: 1280dump_last:
1173 pr_debug("args[0]: %u %u args[1]: %ld\n", 1281 pr_debug("dump type, flag: %u %u index: %ld\n",
1174 dump_type, dump_flags, cb->args[1]); 1282 dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
1175 for (; cb->args[1] < max; cb->args[1]++) { 1283 for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
1176 index = (ip_set_id_t) cb->args[1]; 1284 index = (ip_set_id_t) cb->args[IPSET_CB_INDEX];
1177 set = nfnl_set(index); 1285 set = nfnl_set(inst, index);
1178 if (set == NULL) { 1286 if (set == NULL) {
1179 if (dump_type == DUMP_ONE) { 1287 if (dump_type == DUMP_ONE) {
1180 ret = -ENOENT; 1288 ret = -ENOENT;
@@ -1190,7 +1298,7 @@ dump_last:
1190 !!(set->type->features & IPSET_DUMP_LAST))) 1298 !!(set->type->features & IPSET_DUMP_LAST)))
1191 continue; 1299 continue;
1192 pr_debug("List set: %s\n", set->name); 1300 pr_debug("List set: %s\n", set->name);
1193 if (!cb->args[2]) { 1301 if (!cb->args[IPSET_CB_ARG0]) {
1194 /* Start listing: make sure set won't be destroyed */ 1302 /* Start listing: make sure set won't be destroyed */
1195 pr_debug("reference set\n"); 1303 pr_debug("reference set\n");
1196 __ip_set_get(set); 1304 __ip_set_get(set);
@@ -1207,7 +1315,7 @@ dump_last:
1207 goto nla_put_failure; 1315 goto nla_put_failure;
1208 if (dump_flags & IPSET_FLAG_LIST_SETNAME) 1316 if (dump_flags & IPSET_FLAG_LIST_SETNAME)
1209 goto next_set; 1317 goto next_set;
1210 switch (cb->args[2]) { 1318 switch (cb->args[IPSET_CB_ARG0]) {
1211 case 0: 1319 case 0:
1212 /* Core header data */ 1320 /* Core header data */
1213 if (nla_put_string(skb, IPSET_ATTR_TYPENAME, 1321 if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
@@ -1227,7 +1335,7 @@ dump_last:
1227 read_lock_bh(&set->lock); 1335 read_lock_bh(&set->lock);
1228 ret = set->variant->list(set, skb, cb); 1336 ret = set->variant->list(set, skb, cb);
1229 read_unlock_bh(&set->lock); 1337 read_unlock_bh(&set->lock);
1230 if (!cb->args[2]) 1338 if (!cb->args[IPSET_CB_ARG0])
1231 /* Set is done, proceed with next one */ 1339 /* Set is done, proceed with next one */
1232 goto next_set; 1340 goto next_set;
1233 goto release_refcount; 1341 goto release_refcount;
@@ -1236,8 +1344,8 @@ dump_last:
1236 /* If we dump all sets, continue with dumping last ones */ 1344 /* If we dump all sets, continue with dumping last ones */
1237 if (dump_type == DUMP_ALL) { 1345 if (dump_type == DUMP_ALL) {
1238 dump_type = DUMP_LAST; 1346 dump_type = DUMP_LAST;
1239 cb->args[0] = dump_type | (dump_flags << 16); 1347 cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
1240 cb->args[1] = 0; 1348 cb->args[IPSET_CB_INDEX] = 0;
1241 goto dump_last; 1349 goto dump_last;
1242 } 1350 }
1243 goto out; 1351 goto out;
@@ -1246,15 +1354,15 @@ nla_put_failure:
1246 ret = -EFAULT; 1354 ret = -EFAULT;
1247next_set: 1355next_set:
1248 if (dump_type == DUMP_ONE) 1356 if (dump_type == DUMP_ONE)
1249 cb->args[1] = IPSET_INVALID_ID; 1357 cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
1250 else 1358 else
1251 cb->args[1]++; 1359 cb->args[IPSET_CB_INDEX]++;
1252release_refcount: 1360release_refcount:
1253 /* If there was an error or set is done, release set */ 1361 /* If there was an error or set is done, release set */
1254 if (ret || !cb->args[2]) { 1362 if (ret || !cb->args[IPSET_CB_ARG0]) {
1255 pr_debug("release set %s\n", nfnl_set(index)->name); 1363 pr_debug("release set %s\n", nfnl_set(inst, index)->name);
1256 ip_set_put_byindex(index); 1364 __ip_set_put_byindex(inst, index);
1257 cb->args[2] = 0; 1365 cb->args[IPSET_CB_ARG0] = 0;
1258 } 1366 }
1259out: 1367out:
1260 if (nlh) { 1368 if (nlh) {
@@ -1356,6 +1464,7 @@ ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1356 const struct nlmsghdr *nlh, 1464 const struct nlmsghdr *nlh,
1357 const struct nlattr * const attr[]) 1465 const struct nlattr * const attr[])
1358{ 1466{
1467 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1359 struct ip_set *set; 1468 struct ip_set *set;
1360 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {}; 1469 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1361 const struct nlattr *nla; 1470 const struct nlattr *nla;
@@ -1374,7 +1483,7 @@ ip_set_uadd(struct sock *ctnl, struct sk_buff *skb,
1374 attr[IPSET_ATTR_LINENO] == NULL)))) 1483 attr[IPSET_ATTR_LINENO] == NULL))))
1375 return -IPSET_ERR_PROTOCOL; 1484 return -IPSET_ERR_PROTOCOL;
1376 1485
1377 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1486 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1378 if (set == NULL) 1487 if (set == NULL)
1379 return -ENOENT; 1488 return -ENOENT;
1380 1489
@@ -1410,6 +1519,7 @@ ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1410 const struct nlmsghdr *nlh, 1519 const struct nlmsghdr *nlh,
1411 const struct nlattr * const attr[]) 1520 const struct nlattr * const attr[])
1412{ 1521{
1522 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1413 struct ip_set *set; 1523 struct ip_set *set;
1414 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {}; 1524 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1415 const struct nlattr *nla; 1525 const struct nlattr *nla;
@@ -1428,7 +1538,7 @@ ip_set_udel(struct sock *ctnl, struct sk_buff *skb,
1428 attr[IPSET_ATTR_LINENO] == NULL)))) 1538 attr[IPSET_ATTR_LINENO] == NULL))))
1429 return -IPSET_ERR_PROTOCOL; 1539 return -IPSET_ERR_PROTOCOL;
1430 1540
1431 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1541 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1432 if (set == NULL) 1542 if (set == NULL)
1433 return -ENOENT; 1543 return -ENOENT;
1434 1544
@@ -1464,6 +1574,7 @@ ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1464 const struct nlmsghdr *nlh, 1574 const struct nlmsghdr *nlh,
1465 const struct nlattr * const attr[]) 1575 const struct nlattr * const attr[])
1466{ 1576{
1577 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1467 struct ip_set *set; 1578 struct ip_set *set;
1468 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {}; 1579 struct nlattr *tb[IPSET_ATTR_ADT_MAX+1] = {};
1469 int ret = 0; 1580 int ret = 0;
@@ -1474,7 +1585,7 @@ ip_set_utest(struct sock *ctnl, struct sk_buff *skb,
1474 !flag_nested(attr[IPSET_ATTR_DATA]))) 1585 !flag_nested(attr[IPSET_ATTR_DATA])))
1475 return -IPSET_ERR_PROTOCOL; 1586 return -IPSET_ERR_PROTOCOL;
1476 1587
1477 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1588 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1478 if (set == NULL) 1589 if (set == NULL)
1479 return -ENOENT; 1590 return -ENOENT;
1480 1591
@@ -1499,6 +1610,7 @@ ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1499 const struct nlmsghdr *nlh, 1610 const struct nlmsghdr *nlh,
1500 const struct nlattr * const attr[]) 1611 const struct nlattr * const attr[])
1501{ 1612{
1613 struct ip_set_net *inst = ip_set_pernet(sock_net(ctnl));
1502 const struct ip_set *set; 1614 const struct ip_set *set;
1503 struct sk_buff *skb2; 1615 struct sk_buff *skb2;
1504 struct nlmsghdr *nlh2; 1616 struct nlmsghdr *nlh2;
@@ -1508,7 +1620,7 @@ ip_set_header(struct sock *ctnl, struct sk_buff *skb,
1508 attr[IPSET_ATTR_SETNAME] == NULL)) 1620 attr[IPSET_ATTR_SETNAME] == NULL))
1509 return -IPSET_ERR_PROTOCOL; 1621 return -IPSET_ERR_PROTOCOL;
1510 1622
1511 set = find_set(nla_data(attr[IPSET_ATTR_SETNAME])); 1623 set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
1512 if (set == NULL) 1624 if (set == NULL)
1513 return -ENOENT; 1625 return -ENOENT;
1514 1626
@@ -1733,8 +1845,10 @@ ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1733 unsigned int *op; 1845 unsigned int *op;
1734 void *data; 1846 void *data;
1735 int copylen = *len, ret = 0; 1847 int copylen = *len, ret = 0;
1848 struct net *net = sock_net(sk);
1849 struct ip_set_net *inst = ip_set_pernet(net);
1736 1850
1737 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) 1851 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
1738 return -EPERM; 1852 return -EPERM;
1739 if (optval != SO_IP_SET) 1853 if (optval != SO_IP_SET)
1740 return -EBADF; 1854 return -EBADF;
@@ -1783,22 +1897,39 @@ ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
1783 } 1897 }
1784 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0'; 1898 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1785 nfnl_lock(NFNL_SUBSYS_IPSET); 1899 nfnl_lock(NFNL_SUBSYS_IPSET);
1786 find_set_and_id(req_get->set.name, &id); 1900 find_set_and_id(inst, req_get->set.name, &id);
1787 req_get->set.index = id; 1901 req_get->set.index = id;
1788 nfnl_unlock(NFNL_SUBSYS_IPSET); 1902 nfnl_unlock(NFNL_SUBSYS_IPSET);
1789 goto copy; 1903 goto copy;
1790 } 1904 }
1905 case IP_SET_OP_GET_FNAME: {
1906 struct ip_set_req_get_set_family *req_get = data;
1907 ip_set_id_t id;
1908
1909 if (*len != sizeof(struct ip_set_req_get_set_family)) {
1910 ret = -EINVAL;
1911 goto done;
1912 }
1913 req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
1914 nfnl_lock(NFNL_SUBSYS_IPSET);
1915 find_set_and_id(inst, req_get->set.name, &id);
1916 req_get->set.index = id;
1917 if (id != IPSET_INVALID_ID)
1918 req_get->family = nfnl_set(inst, id)->family;
1919 nfnl_unlock(NFNL_SUBSYS_IPSET);
1920 goto copy;
1921 }
1791 case IP_SET_OP_GET_BYINDEX: { 1922 case IP_SET_OP_GET_BYINDEX: {
1792 struct ip_set_req_get_set *req_get = data; 1923 struct ip_set_req_get_set *req_get = data;
1793 struct ip_set *set; 1924 struct ip_set *set;
1794 1925
1795 if (*len != sizeof(struct ip_set_req_get_set) || 1926 if (*len != sizeof(struct ip_set_req_get_set) ||
1796 req_get->set.index >= ip_set_max) { 1927 req_get->set.index >= inst->ip_set_max) {
1797 ret = -EINVAL; 1928 ret = -EINVAL;
1798 goto done; 1929 goto done;
1799 } 1930 }
1800 nfnl_lock(NFNL_SUBSYS_IPSET); 1931 nfnl_lock(NFNL_SUBSYS_IPSET);
1801 set = nfnl_set(req_get->set.index); 1932 set = nfnl_set(inst, req_get->set.index);
1802 strncpy(req_get->set.name, set ? set->name : "", 1933 strncpy(req_get->set.name, set ? set->name : "",
1803 IPSET_MAXNAMELEN); 1934 IPSET_MAXNAMELEN);
1804 nfnl_unlock(NFNL_SUBSYS_IPSET); 1935 nfnl_unlock(NFNL_SUBSYS_IPSET);
@@ -1827,49 +1958,81 @@ static struct nf_sockopt_ops so_set __read_mostly = {
1827 .owner = THIS_MODULE, 1958 .owner = THIS_MODULE,
1828}; 1959};
1829 1960
1830static int __init 1961static int __net_init
1831ip_set_init(void) 1962ip_set_net_init(struct net *net)
1832{ 1963{
1964 struct ip_set_net *inst = ip_set_pernet(net);
1833 struct ip_set **list; 1965 struct ip_set **list;
1834 int ret;
1835 1966
1836 if (max_sets) 1967 inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
1837 ip_set_max = max_sets; 1968 if (inst->ip_set_max >= IPSET_INVALID_ID)
1838 if (ip_set_max >= IPSET_INVALID_ID) 1969 inst->ip_set_max = IPSET_INVALID_ID - 1;
1839 ip_set_max = IPSET_INVALID_ID - 1;
1840 1970
1841 list = kzalloc(sizeof(struct ip_set *) * ip_set_max, GFP_KERNEL); 1971 list = kzalloc(sizeof(struct ip_set *) * inst->ip_set_max, GFP_KERNEL);
1842 if (!list) 1972 if (!list)
1843 return -ENOMEM; 1973 return -ENOMEM;
1974 inst->is_deleted = 0;
1975 rcu_assign_pointer(inst->ip_set_list, list);
1976 pr_notice("ip_set: protocol %u\n", IPSET_PROTOCOL);
1977 return 0;
1978}
1844 1979
1845 rcu_assign_pointer(ip_set_list, list); 1980static void __net_exit
1846 ret = nfnetlink_subsys_register(&ip_set_netlink_subsys); 1981ip_set_net_exit(struct net *net)
1982{
1983 struct ip_set_net *inst = ip_set_pernet(net);
1984
1985 struct ip_set *set = NULL;
1986 ip_set_id_t i;
1987
1988 inst->is_deleted = 1; /* flag for ip_set_nfnl_put */
1989
1990 for (i = 0; i < inst->ip_set_max; i++) {
1991 set = nfnl_set(inst, i);
1992 if (set != NULL)
1993 ip_set_destroy_set(inst, i);
1994 }
1995 kfree(rcu_dereference_protected(inst->ip_set_list, 1));
1996}
1997
1998static struct pernet_operations ip_set_net_ops = {
1999 .init = ip_set_net_init,
2000 .exit = ip_set_net_exit,
2001 .id = &ip_set_net_id,
2002 .size = sizeof(struct ip_set_net)
2003};
2004
2005
2006static int __init
2007ip_set_init(void)
2008{
2009 int ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
1847 if (ret != 0) { 2010 if (ret != 0) {
1848 pr_err("ip_set: cannot register with nfnetlink.\n"); 2011 pr_err("ip_set: cannot register with nfnetlink.\n");
1849 kfree(list);
1850 return ret; 2012 return ret;
1851 } 2013 }
1852 ret = nf_register_sockopt(&so_set); 2014 ret = nf_register_sockopt(&so_set);
1853 if (ret != 0) { 2015 if (ret != 0) {
1854 pr_err("SO_SET registry failed: %d\n", ret); 2016 pr_err("SO_SET registry failed: %d\n", ret);
1855 nfnetlink_subsys_unregister(&ip_set_netlink_subsys); 2017 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1856 kfree(list);
1857 return ret; 2018 return ret;
1858 } 2019 }
1859 2020 ret = register_pernet_subsys(&ip_set_net_ops);
1860 pr_notice("ip_set: protocol %u\n", IPSET_PROTOCOL); 2021 if (ret) {
2022 pr_err("ip_set: cannot register pernet_subsys.\n");
2023 nf_unregister_sockopt(&so_set);
2024 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
2025 return ret;
2026 }
1861 return 0; 2027 return 0;
1862} 2028}
1863 2029
1864static void __exit 2030static void __exit
1865ip_set_fini(void) 2031ip_set_fini(void)
1866{ 2032{
1867 struct ip_set **list = rcu_dereference_protected(ip_set_list, 1); 2033 unregister_pernet_subsys(&ip_set_net_ops);
1868
1869 /* There can't be any existing set */
1870 nf_unregister_sockopt(&so_set); 2034 nf_unregister_sockopt(&so_set);
1871 nfnetlink_subsys_unregister(&ip_set_netlink_subsys); 2035 nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
1872 kfree(list);
1873 pr_debug("these are the famous last words\n"); 2036 pr_debug("these are the famous last words\n");
1874} 2037}
1875 2038
diff --git a/net/netfilter/ipset/ip_set_getport.c b/net/netfilter/ipset/ip_set_getport.c
index dac156f819ac..29fb01ddff93 100644
--- a/net/netfilter/ipset/ip_set_getport.c
+++ b/net/netfilter/ipset/ip_set_getport.c
@@ -102,9 +102,25 @@ ip_set_get_ip4_port(const struct sk_buff *skb, bool src,
102 int protocol = iph->protocol; 102 int protocol = iph->protocol;
103 103
104 /* See comments at tcp_match in ip_tables.c */ 104 /* See comments at tcp_match in ip_tables.c */
105 if (protocol <= 0 || (ntohs(iph->frag_off) & IP_OFFSET)) 105 if (protocol <= 0)
106 return false; 106 return false;
107 107
108 if (ntohs(iph->frag_off) & IP_OFFSET)
109 switch (protocol) {
110 case IPPROTO_TCP:
111 case IPPROTO_SCTP:
112 case IPPROTO_UDP:
113 case IPPROTO_UDPLITE:
114 case IPPROTO_ICMP:
115 /* Port info not available for fragment offset > 0 */
116 return false;
117 default:
118 /* Other protocols doesn't have ports,
119 so we can match fragments */
120 *proto = protocol;
121 return true;
122 }
123
108 return get_port(skb, protocol, protooff, src, port, proto); 124 return get_port(skb, protocol, protooff, src, port, proto);
109} 125}
110EXPORT_SYMBOL_GPL(ip_set_get_ip4_port); 126EXPORT_SYMBOL_GPL(ip_set_get_ip4_port);
diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 707bc520d629..be6932ad3a86 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -15,8 +15,7 @@
15#define rcu_dereference_bh(p) rcu_dereference(p) 15#define rcu_dereference_bh(p) rcu_dereference(p)
16#endif 16#endif
17 17
18#define CONCAT(a, b) a##b 18#define rcu_dereference_bh_nfnl(p) rcu_dereference_bh_check(p, 1)
19#define TOKEN(a, b) CONCAT(a, b)
20 19
21/* Hashing which uses arrays to resolve clashing. The hash table is resized 20/* Hashing which uses arrays to resolve clashing. The hash table is resized
22 * (doubled) when searching becomes too long. 21 * (doubled) when searching becomes too long.
@@ -78,10 +77,14 @@ struct htable {
78 77
79#define hbucket(h, i) (&((h)->bucket[i])) 78#define hbucket(h, i) (&((h)->bucket[i]))
80 79
80#ifndef IPSET_NET_COUNT
81#define IPSET_NET_COUNT 1
82#endif
83
81/* Book-keeping of the prefixes added to the set */ 84/* Book-keeping of the prefixes added to the set */
82struct net_prefixes { 85struct net_prefixes {
83 u8 cidr; /* the different cidr values in the set */ 86 u32 nets[IPSET_NET_COUNT]; /* number of elements per cidr */
84 u32 nets; /* number of elements per cidr */ 87 u8 cidr[IPSET_NET_COUNT]; /* the different cidr values in the set */
85}; 88};
86 89
87/* Compute the hash table size */ 90/* Compute the hash table size */
@@ -114,23 +117,6 @@ htable_bits(u32 hashsize)
114 return bits; 117 return bits;
115} 118}
116 119
117/* Destroy the hashtable part of the set */
118static void
119ahash_destroy(struct htable *t)
120{
121 struct hbucket *n;
122 u32 i;
123
124 for (i = 0; i < jhash_size(t->htable_bits); i++) {
125 n = hbucket(t, i);
126 if (n->size)
127 /* FIXME: use slab cache */
128 kfree(n->value);
129 }
130
131 ip_set_free(t);
132}
133
134static int 120static int
135hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize) 121hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
136{ 122{
@@ -156,30 +142,30 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
156} 142}
157 143
158#ifdef IP_SET_HASH_WITH_NETS 144#ifdef IP_SET_HASH_WITH_NETS
145#if IPSET_NET_COUNT > 1
146#define __CIDR(cidr, i) (cidr[i])
147#else
148#define __CIDR(cidr, i) (cidr)
149#endif
159#ifdef IP_SET_HASH_WITH_NETS_PACKED 150#ifdef IP_SET_HASH_WITH_NETS_PACKED
160/* When cidr is packed with nomatch, cidr - 1 is stored in the entry */ 151/* When cidr is packed with nomatch, cidr - 1 is stored in the entry */
161#define CIDR(cidr) (cidr + 1) 152#define CIDR(cidr, i) (__CIDR(cidr, i) + 1)
162#else 153#else
163#define CIDR(cidr) (cidr) 154#define CIDR(cidr, i) (__CIDR(cidr, i))
164#endif 155#endif
165 156
166#define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128) 157#define SET_HOST_MASK(family) (family == AF_INET ? 32 : 128)
167 158
168#ifdef IP_SET_HASH_WITH_MULTI 159#ifdef IP_SET_HASH_WITH_MULTI
169#define NETS_LENGTH(family) (SET_HOST_MASK(family) + 1) 160#define NLEN(family) (SET_HOST_MASK(family) + 1)
170#else 161#else
171#define NETS_LENGTH(family) SET_HOST_MASK(family) 162#define NLEN(family) SET_HOST_MASK(family)
172#endif 163#endif
173 164
174#else 165#else
175#define NETS_LENGTH(family) 0 166#define NLEN(family) 0
176#endif /* IP_SET_HASH_WITH_NETS */ 167#endif /* IP_SET_HASH_WITH_NETS */
177 168
178#define ext_timeout(e, h) \
179(unsigned long *)(((void *)(e)) + (h)->offset[IPSET_OFFSET_TIMEOUT])
180#define ext_counter(e, h) \
181(struct ip_set_counter *)(((void *)(e)) + (h)->offset[IPSET_OFFSET_COUNTER])
182
183#endif /* _IP_SET_HASH_GEN_H */ 169#endif /* _IP_SET_HASH_GEN_H */
184 170
185/* Family dependent templates */ 171/* Family dependent templates */
@@ -194,6 +180,8 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
194#undef mtype_data_next 180#undef mtype_data_next
195#undef mtype_elem 181#undef mtype_elem
196 182
183#undef mtype_ahash_destroy
184#undef mtype_ext_cleanup
197#undef mtype_add_cidr 185#undef mtype_add_cidr
198#undef mtype_del_cidr 186#undef mtype_del_cidr
199#undef mtype_ahash_memsize 187#undef mtype_ahash_memsize
@@ -220,41 +208,43 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
220 208
221#undef HKEY 209#undef HKEY
222 210
223#define mtype_data_equal TOKEN(MTYPE, _data_equal) 211#define mtype_data_equal IPSET_TOKEN(MTYPE, _data_equal)
224#ifdef IP_SET_HASH_WITH_NETS 212#ifdef IP_SET_HASH_WITH_NETS
225#define mtype_do_data_match TOKEN(MTYPE, _do_data_match) 213#define mtype_do_data_match IPSET_TOKEN(MTYPE, _do_data_match)
226#else 214#else
227#define mtype_do_data_match(d) 1 215#define mtype_do_data_match(d) 1
228#endif 216#endif
229#define mtype_data_set_flags TOKEN(MTYPE, _data_set_flags) 217#define mtype_data_set_flags IPSET_TOKEN(MTYPE, _data_set_flags)
230#define mtype_data_reset_flags TOKEN(MTYPE, _data_reset_flags) 218#define mtype_data_reset_elem IPSET_TOKEN(MTYPE, _data_reset_elem)
231#define mtype_data_netmask TOKEN(MTYPE, _data_netmask) 219#define mtype_data_reset_flags IPSET_TOKEN(MTYPE, _data_reset_flags)
232#define mtype_data_list TOKEN(MTYPE, _data_list) 220#define mtype_data_netmask IPSET_TOKEN(MTYPE, _data_netmask)
233#define mtype_data_next TOKEN(MTYPE, _data_next) 221#define mtype_data_list IPSET_TOKEN(MTYPE, _data_list)
234#define mtype_elem TOKEN(MTYPE, _elem) 222#define mtype_data_next IPSET_TOKEN(MTYPE, _data_next)
235#define mtype_add_cidr TOKEN(MTYPE, _add_cidr) 223#define mtype_elem IPSET_TOKEN(MTYPE, _elem)
236#define mtype_del_cidr TOKEN(MTYPE, _del_cidr) 224#define mtype_ahash_destroy IPSET_TOKEN(MTYPE, _ahash_destroy)
237#define mtype_ahash_memsize TOKEN(MTYPE, _ahash_memsize) 225#define mtype_ext_cleanup IPSET_TOKEN(MTYPE, _ext_cleanup)
238#define mtype_flush TOKEN(MTYPE, _flush) 226#define mtype_add_cidr IPSET_TOKEN(MTYPE, _add_cidr)
239#define mtype_destroy TOKEN(MTYPE, _destroy) 227#define mtype_del_cidr IPSET_TOKEN(MTYPE, _del_cidr)
240#define mtype_gc_init TOKEN(MTYPE, _gc_init) 228#define mtype_ahash_memsize IPSET_TOKEN(MTYPE, _ahash_memsize)
241#define mtype_same_set TOKEN(MTYPE, _same_set) 229#define mtype_flush IPSET_TOKEN(MTYPE, _flush)
242#define mtype_kadt TOKEN(MTYPE, _kadt) 230#define mtype_destroy IPSET_TOKEN(MTYPE, _destroy)
243#define mtype_uadt TOKEN(MTYPE, _uadt) 231#define mtype_gc_init IPSET_TOKEN(MTYPE, _gc_init)
232#define mtype_same_set IPSET_TOKEN(MTYPE, _same_set)
233#define mtype_kadt IPSET_TOKEN(MTYPE, _kadt)
234#define mtype_uadt IPSET_TOKEN(MTYPE, _uadt)
244#define mtype MTYPE 235#define mtype MTYPE
245 236
246#define mtype_elem TOKEN(MTYPE, _elem) 237#define mtype_add IPSET_TOKEN(MTYPE, _add)
247#define mtype_add TOKEN(MTYPE, _add) 238#define mtype_del IPSET_TOKEN(MTYPE, _del)
248#define mtype_del TOKEN(MTYPE, _del) 239#define mtype_test_cidrs IPSET_TOKEN(MTYPE, _test_cidrs)
249#define mtype_test_cidrs TOKEN(MTYPE, _test_cidrs) 240#define mtype_test IPSET_TOKEN(MTYPE, _test)
250#define mtype_test TOKEN(MTYPE, _test) 241#define mtype_expire IPSET_TOKEN(MTYPE, _expire)
251#define mtype_expire TOKEN(MTYPE, _expire) 242#define mtype_resize IPSET_TOKEN(MTYPE, _resize)
252#define mtype_resize TOKEN(MTYPE, _resize) 243#define mtype_head IPSET_TOKEN(MTYPE, _head)
253#define mtype_head TOKEN(MTYPE, _head) 244#define mtype_list IPSET_TOKEN(MTYPE, _list)
254#define mtype_list TOKEN(MTYPE, _list) 245#define mtype_gc IPSET_TOKEN(MTYPE, _gc)
255#define mtype_gc TOKEN(MTYPE, _gc) 246#define mtype_variant IPSET_TOKEN(MTYPE, _variant)
256#define mtype_variant TOKEN(MTYPE, _variant) 247#define mtype_data_match IPSET_TOKEN(MTYPE, _data_match)
257#define mtype_data_match TOKEN(MTYPE, _data_match)
258 248
259#ifndef HKEY_DATALEN 249#ifndef HKEY_DATALEN
260#define HKEY_DATALEN sizeof(struct mtype_elem) 250#define HKEY_DATALEN sizeof(struct mtype_elem)
@@ -269,13 +259,10 @@ hbucket_elem_add(struct hbucket *n, u8 ahash_max, size_t dsize)
269 259
270/* The generic hash structure */ 260/* The generic hash structure */
271struct htype { 261struct htype {
272 struct htable *table; /* the hash table */ 262 struct htable __rcu *table; /* the hash table */
273 u32 maxelem; /* max elements in the hash */ 263 u32 maxelem; /* max elements in the hash */
274 u32 elements; /* current element (vs timeout) */ 264 u32 elements; /* current element (vs timeout) */
275 u32 initval; /* random jhash init value */ 265 u32 initval; /* random jhash init value */
276 u32 timeout; /* timeout value, if enabled */
277 size_t dsize; /* data struct size */
278 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
279 struct timer_list gc; /* garbage collection when timeout enabled */ 266 struct timer_list gc; /* garbage collection when timeout enabled */
280 struct mtype_elem next; /* temporary storage for uadd */ 267 struct mtype_elem next; /* temporary storage for uadd */
281#ifdef IP_SET_HASH_WITH_MULTI 268#ifdef IP_SET_HASH_WITH_MULTI
@@ -297,49 +284,49 @@ struct htype {
297/* Network cidr size book keeping when the hash stores different 284/* Network cidr size book keeping when the hash stores different
298 * sized networks */ 285 * sized networks */
299static void 286static void
300mtype_add_cidr(struct htype *h, u8 cidr, u8 nets_length) 287mtype_add_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
301{ 288{
302 int i, j; 289 int i, j;
303 290
304 /* Add in increasing prefix order, so larger cidr first */ 291 /* Add in increasing prefix order, so larger cidr first */
305 for (i = 0, j = -1; i < nets_length && h->nets[i].nets; i++) { 292 for (i = 0, j = -1; i < nets_length && h->nets[i].nets[n]; i++) {
306 if (j != -1) 293 if (j != -1)
307 continue; 294 continue;
308 else if (h->nets[i].cidr < cidr) 295 else if (h->nets[i].cidr[n] < cidr)
309 j = i; 296 j = i;
310 else if (h->nets[i].cidr == cidr) { 297 else if (h->nets[i].cidr[n] == cidr) {
311 h->nets[i].nets++; 298 h->nets[i].nets[n]++;
312 return; 299 return;
313 } 300 }
314 } 301 }
315 if (j != -1) { 302 if (j != -1) {
316 for (; i > j; i--) { 303 for (; i > j; i--) {
317 h->nets[i].cidr = h->nets[i - 1].cidr; 304 h->nets[i].cidr[n] = h->nets[i - 1].cidr[n];
318 h->nets[i].nets = h->nets[i - 1].nets; 305 h->nets[i].nets[n] = h->nets[i - 1].nets[n];
319 } 306 }
320 } 307 }
321 h->nets[i].cidr = cidr; 308 h->nets[i].cidr[n] = cidr;
322 h->nets[i].nets = 1; 309 h->nets[i].nets[n] = 1;
323} 310}
324 311
325static void 312static void
326mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length) 313mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length, u8 n)
327{ 314{
328 u8 i, j, net_end = nets_length - 1; 315 u8 i, j, net_end = nets_length - 1;
329 316
330 for (i = 0; i < nets_length; i++) { 317 for (i = 0; i < nets_length; i++) {
331 if (h->nets[i].cidr != cidr) 318 if (h->nets[i].cidr[n] != cidr)
332 continue; 319 continue;
333 if (h->nets[i].nets > 1 || i == net_end || 320 if (h->nets[i].nets[n] > 1 || i == net_end ||
334 h->nets[i + 1].nets == 0) { 321 h->nets[i + 1].nets[n] == 0) {
335 h->nets[i].nets--; 322 h->nets[i].nets[n]--;
336 return; 323 return;
337 } 324 }
338 for (j = i; j < net_end && h->nets[j].nets; j++) { 325 for (j = i; j < net_end && h->nets[j].nets[n]; j++) {
339 h->nets[j].cidr = h->nets[j + 1].cidr; 326 h->nets[j].cidr[n] = h->nets[j + 1].cidr[n];
340 h->nets[j].nets = h->nets[j + 1].nets; 327 h->nets[j].nets[n] = h->nets[j + 1].nets[n];
341 } 328 }
342 h->nets[j].nets = 0; 329 h->nets[j].nets[n] = 0;
343 return; 330 return;
344 } 331 }
345} 332}
@@ -347,10 +334,10 @@ mtype_del_cidr(struct htype *h, u8 cidr, u8 nets_length)
347 334
348/* Calculate the actual memory size of the set data */ 335/* Calculate the actual memory size of the set data */
349static size_t 336static size_t
350mtype_ahash_memsize(const struct htype *h, u8 nets_length) 337mtype_ahash_memsize(const struct htype *h, const struct htable *t,
338 u8 nets_length, size_t dsize)
351{ 339{
352 u32 i; 340 u32 i;
353 struct htable *t = h->table;
354 size_t memsize = sizeof(*h) 341 size_t memsize = sizeof(*h)
355 + sizeof(*t) 342 + sizeof(*t)
356#ifdef IP_SET_HASH_WITH_NETS 343#ifdef IP_SET_HASH_WITH_NETS
@@ -359,35 +346,70 @@ mtype_ahash_memsize(const struct htype *h, u8 nets_length)
359 + jhash_size(t->htable_bits) * sizeof(struct hbucket); 346 + jhash_size(t->htable_bits) * sizeof(struct hbucket);
360 347
361 for (i = 0; i < jhash_size(t->htable_bits); i++) 348 for (i = 0; i < jhash_size(t->htable_bits); i++)
362 memsize += t->bucket[i].size * h->dsize; 349 memsize += t->bucket[i].size * dsize;
363 350
364 return memsize; 351 return memsize;
365} 352}
366 353
354/* Get the ith element from the array block n */
355#define ahash_data(n, i, dsize) \
356 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
357
358static void
359mtype_ext_cleanup(struct ip_set *set, struct hbucket *n)
360{
361 int i;
362
363 for (i = 0; i < n->pos; i++)
364 ip_set_ext_destroy(set, ahash_data(n, i, set->dsize));
365}
366
367/* Flush a hash type of set: destroy all elements */ 367/* Flush a hash type of set: destroy all elements */
368static void 368static void
369mtype_flush(struct ip_set *set) 369mtype_flush(struct ip_set *set)
370{ 370{
371 struct htype *h = set->data; 371 struct htype *h = set->data;
372 struct htable *t = h->table; 372 struct htable *t;
373 struct hbucket *n; 373 struct hbucket *n;
374 u32 i; 374 u32 i;
375 375
376 t = rcu_dereference_bh_nfnl(h->table);
376 for (i = 0; i < jhash_size(t->htable_bits); i++) { 377 for (i = 0; i < jhash_size(t->htable_bits); i++) {
377 n = hbucket(t, i); 378 n = hbucket(t, i);
378 if (n->size) { 379 if (n->size) {
380 if (set->extensions & IPSET_EXT_DESTROY)
381 mtype_ext_cleanup(set, n);
379 n->size = n->pos = 0; 382 n->size = n->pos = 0;
380 /* FIXME: use slab cache */ 383 /* FIXME: use slab cache */
381 kfree(n->value); 384 kfree(n->value);
382 } 385 }
383 } 386 }
384#ifdef IP_SET_HASH_WITH_NETS 387#ifdef IP_SET_HASH_WITH_NETS
385 memset(h->nets, 0, sizeof(struct net_prefixes) 388 memset(h->nets, 0, sizeof(struct net_prefixes) * NLEN(set->family));
386 * NETS_LENGTH(set->family));
387#endif 389#endif
388 h->elements = 0; 390 h->elements = 0;
389} 391}
390 392
393/* Destroy the hashtable part of the set */
394static void
395mtype_ahash_destroy(struct ip_set *set, struct htable *t, bool ext_destroy)
396{
397 struct hbucket *n;
398 u32 i;
399
400 for (i = 0; i < jhash_size(t->htable_bits); i++) {
401 n = hbucket(t, i);
402 if (n->size) {
403 if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
404 mtype_ext_cleanup(set, n);
405 /* FIXME: use slab cache */
406 kfree(n->value);
407 }
408 }
409
410 ip_set_free(t);
411}
412
391/* Destroy a hash type of set */ 413/* Destroy a hash type of set */
392static void 414static void
393mtype_destroy(struct ip_set *set) 415mtype_destroy(struct ip_set *set)
@@ -397,7 +419,7 @@ mtype_destroy(struct ip_set *set)
397 if (set->extensions & IPSET_EXT_TIMEOUT) 419 if (set->extensions & IPSET_EXT_TIMEOUT)
398 del_timer_sync(&h->gc); 420 del_timer_sync(&h->gc);
399 421
400 ahash_destroy(h->table); 422 mtype_ahash_destroy(set, rcu_dereference_bh_nfnl(h->table), true);
401#ifdef IP_SET_HASH_WITH_RBTREE 423#ifdef IP_SET_HASH_WITH_RBTREE
402 rbtree_destroy(&h->rbtree); 424 rbtree_destroy(&h->rbtree);
403#endif 425#endif
@@ -414,10 +436,10 @@ mtype_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
414 init_timer(&h->gc); 436 init_timer(&h->gc);
415 h->gc.data = (unsigned long) set; 437 h->gc.data = (unsigned long) set;
416 h->gc.function = gc; 438 h->gc.function = gc;
417 h->gc.expires = jiffies + IPSET_GC_PERIOD(h->timeout) * HZ; 439 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
418 add_timer(&h->gc); 440 add_timer(&h->gc);
419 pr_debug("gc initialized, run in every %u\n", 441 pr_debug("gc initialized, run in every %u\n",
420 IPSET_GC_PERIOD(h->timeout)); 442 IPSET_GC_PERIOD(set->timeout));
421} 443}
422 444
423static bool 445static bool
@@ -428,37 +450,40 @@ mtype_same_set(const struct ip_set *a, const struct ip_set *b)
428 450
429 /* Resizing changes htable_bits, so we ignore it */ 451 /* Resizing changes htable_bits, so we ignore it */
430 return x->maxelem == y->maxelem && 452 return x->maxelem == y->maxelem &&
431 x->timeout == y->timeout && 453 a->timeout == b->timeout &&
432#ifdef IP_SET_HASH_WITH_NETMASK 454#ifdef IP_SET_HASH_WITH_NETMASK
433 x->netmask == y->netmask && 455 x->netmask == y->netmask &&
434#endif 456#endif
435 a->extensions == b->extensions; 457 a->extensions == b->extensions;
436} 458}
437 459
438/* Get the ith element from the array block n */
439#define ahash_data(n, i, dsize) \
440 ((struct mtype_elem *)((n)->value + ((i) * (dsize))))
441
442/* Delete expired elements from the hashtable */ 460/* Delete expired elements from the hashtable */
443static void 461static void
444mtype_expire(struct htype *h, u8 nets_length, size_t dsize) 462mtype_expire(struct ip_set *set, struct htype *h, u8 nets_length, size_t dsize)
445{ 463{
446 struct htable *t = h->table; 464 struct htable *t;
447 struct hbucket *n; 465 struct hbucket *n;
448 struct mtype_elem *data; 466 struct mtype_elem *data;
449 u32 i; 467 u32 i;
450 int j; 468 int j;
469#ifdef IP_SET_HASH_WITH_NETS
470 u8 k;
471#endif
451 472
473 rcu_read_lock_bh();
474 t = rcu_dereference_bh(h->table);
452 for (i = 0; i < jhash_size(t->htable_bits); i++) { 475 for (i = 0; i < jhash_size(t->htable_bits); i++) {
453 n = hbucket(t, i); 476 n = hbucket(t, i);
454 for (j = 0; j < n->pos; j++) { 477 for (j = 0; j < n->pos; j++) {
455 data = ahash_data(n, j, dsize); 478 data = ahash_data(n, j, dsize);
456 if (ip_set_timeout_expired(ext_timeout(data, h))) { 479 if (ip_set_timeout_expired(ext_timeout(data, set))) {
457 pr_debug("expired %u/%u\n", i, j); 480 pr_debug("expired %u/%u\n", i, j);
458#ifdef IP_SET_HASH_WITH_NETS 481#ifdef IP_SET_HASH_WITH_NETS
459 mtype_del_cidr(h, CIDR(data->cidr), 482 for (k = 0; k < IPSET_NET_COUNT; k++)
460 nets_length); 483 mtype_del_cidr(h, CIDR(data->cidr, k),
484 nets_length, k);
461#endif 485#endif
486 ip_set_ext_destroy(set, data);
462 if (j != n->pos - 1) 487 if (j != n->pos - 1)
463 /* Not last one */ 488 /* Not last one */
464 memcpy(data, 489 memcpy(data,
@@ -481,6 +506,7 @@ mtype_expire(struct htype *h, u8 nets_length, size_t dsize)
481 n->value = tmp; 506 n->value = tmp;
482 } 507 }
483 } 508 }
509 rcu_read_unlock_bh();
484} 510}
485 511
486static void 512static void
@@ -491,10 +517,10 @@ mtype_gc(unsigned long ul_set)
491 517
492 pr_debug("called\n"); 518 pr_debug("called\n");
493 write_lock_bh(&set->lock); 519 write_lock_bh(&set->lock);
494 mtype_expire(h, NETS_LENGTH(set->family), h->dsize); 520 mtype_expire(set, h, NLEN(set->family), set->dsize);
495 write_unlock_bh(&set->lock); 521 write_unlock_bh(&set->lock);
496 522
497 h->gc.expires = jiffies + IPSET_GC_PERIOD(h->timeout) * HZ; 523 h->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
498 add_timer(&h->gc); 524 add_timer(&h->gc);
499} 525}
500 526
@@ -505,7 +531,7 @@ static int
505mtype_resize(struct ip_set *set, bool retried) 531mtype_resize(struct ip_set *set, bool retried)
506{ 532{
507 struct htype *h = set->data; 533 struct htype *h = set->data;
508 struct htable *t, *orig = h->table; 534 struct htable *t, *orig = rcu_dereference_bh_nfnl(h->table);
509 u8 htable_bits = orig->htable_bits; 535 u8 htable_bits = orig->htable_bits;
510#ifdef IP_SET_HASH_WITH_NETS 536#ifdef IP_SET_HASH_WITH_NETS
511 u8 flags; 537 u8 flags;
@@ -520,8 +546,7 @@ mtype_resize(struct ip_set *set, bool retried)
520 if (SET_WITH_TIMEOUT(set) && !retried) { 546 if (SET_WITH_TIMEOUT(set) && !retried) {
521 i = h->elements; 547 i = h->elements;
522 write_lock_bh(&set->lock); 548 write_lock_bh(&set->lock);
523 mtype_expire(set->data, NETS_LENGTH(set->family), 549 mtype_expire(set, set->data, NLEN(set->family), set->dsize);
524 h->dsize);
525 write_unlock_bh(&set->lock); 550 write_unlock_bh(&set->lock);
526 if (h->elements < i) 551 if (h->elements < i)
527 return 0; 552 return 0;
@@ -548,25 +573,25 @@ retry:
548 for (i = 0; i < jhash_size(orig->htable_bits); i++) { 573 for (i = 0; i < jhash_size(orig->htable_bits); i++) {
549 n = hbucket(orig, i); 574 n = hbucket(orig, i);
550 for (j = 0; j < n->pos; j++) { 575 for (j = 0; j < n->pos; j++) {
551 data = ahash_data(n, j, h->dsize); 576 data = ahash_data(n, j, set->dsize);
552#ifdef IP_SET_HASH_WITH_NETS 577#ifdef IP_SET_HASH_WITH_NETS
553 flags = 0; 578 flags = 0;
554 mtype_data_reset_flags(data, &flags); 579 mtype_data_reset_flags(data, &flags);
555#endif 580#endif
556 m = hbucket(t, HKEY(data, h->initval, htable_bits)); 581 m = hbucket(t, HKEY(data, h->initval, htable_bits));
557 ret = hbucket_elem_add(m, AHASH_MAX(h), h->dsize); 582 ret = hbucket_elem_add(m, AHASH_MAX(h), set->dsize);
558 if (ret < 0) { 583 if (ret < 0) {
559#ifdef IP_SET_HASH_WITH_NETS 584#ifdef IP_SET_HASH_WITH_NETS
560 mtype_data_reset_flags(data, &flags); 585 mtype_data_reset_flags(data, &flags);
561#endif 586#endif
562 read_unlock_bh(&set->lock); 587 read_unlock_bh(&set->lock);
563 ahash_destroy(t); 588 mtype_ahash_destroy(set, t, false);
564 if (ret == -EAGAIN) 589 if (ret == -EAGAIN)
565 goto retry; 590 goto retry;
566 return ret; 591 return ret;
567 } 592 }
568 d = ahash_data(m, m->pos++, h->dsize); 593 d = ahash_data(m, m->pos++, set->dsize);
569 memcpy(d, data, h->dsize); 594 memcpy(d, data, set->dsize);
570#ifdef IP_SET_HASH_WITH_NETS 595#ifdef IP_SET_HASH_WITH_NETS
571 mtype_data_reset_flags(d, &flags); 596 mtype_data_reset_flags(d, &flags);
572#endif 597#endif
@@ -581,7 +606,7 @@ retry:
581 606
582 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name, 607 pr_debug("set %s resized from %u (%p) to %u (%p)\n", set->name,
583 orig->htable_bits, orig, t->htable_bits, t); 608 orig->htable_bits, orig, t->htable_bits, t);
584 ahash_destroy(orig); 609 mtype_ahash_destroy(set, orig, false);
585 610
586 return 0; 611 return 0;
587} 612}
@@ -604,7 +629,7 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
604 629
605 if (SET_WITH_TIMEOUT(set) && h->elements >= h->maxelem) 630 if (SET_WITH_TIMEOUT(set) && h->elements >= h->maxelem)
606 /* FIXME: when set is full, we slow down here */ 631 /* FIXME: when set is full, we slow down here */
607 mtype_expire(h, NETS_LENGTH(set->family), h->dsize); 632 mtype_expire(set, h, NLEN(set->family), set->dsize);
608 633
609 if (h->elements >= h->maxelem) { 634 if (h->elements >= h->maxelem) {
610 if (net_ratelimit()) 635 if (net_ratelimit())
@@ -618,11 +643,11 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
618 key = HKEY(value, h->initval, t->htable_bits); 643 key = HKEY(value, h->initval, t->htable_bits);
619 n = hbucket(t, key); 644 n = hbucket(t, key);
620 for (i = 0; i < n->pos; i++) { 645 for (i = 0; i < n->pos; i++) {
621 data = ahash_data(n, i, h->dsize); 646 data = ahash_data(n, i, set->dsize);
622 if (mtype_data_equal(data, d, &multi)) { 647 if (mtype_data_equal(data, d, &multi)) {
623 if (flag_exist || 648 if (flag_exist ||
624 (SET_WITH_TIMEOUT(set) && 649 (SET_WITH_TIMEOUT(set) &&
625 ip_set_timeout_expired(ext_timeout(data, h)))) { 650 ip_set_timeout_expired(ext_timeout(data, set)))) {
626 /* Just the extensions could be overwritten */ 651 /* Just the extensions could be overwritten */
627 j = i; 652 j = i;
628 goto reuse_slot; 653 goto reuse_slot;
@@ -633,30 +658,37 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
633 } 658 }
634 /* Reuse first timed out entry */ 659 /* Reuse first timed out entry */
635 if (SET_WITH_TIMEOUT(set) && 660 if (SET_WITH_TIMEOUT(set) &&
636 ip_set_timeout_expired(ext_timeout(data, h)) && 661 ip_set_timeout_expired(ext_timeout(data, set)) &&
637 j != AHASH_MAX(h) + 1) 662 j != AHASH_MAX(h) + 1)
638 j = i; 663 j = i;
639 } 664 }
640reuse_slot: 665reuse_slot:
641 if (j != AHASH_MAX(h) + 1) { 666 if (j != AHASH_MAX(h) + 1) {
642 /* Fill out reused slot */ 667 /* Fill out reused slot */
643 data = ahash_data(n, j, h->dsize); 668 data = ahash_data(n, j, set->dsize);
644#ifdef IP_SET_HASH_WITH_NETS 669#ifdef IP_SET_HASH_WITH_NETS
645 mtype_del_cidr(h, CIDR(data->cidr), NETS_LENGTH(set->family)); 670 for (i = 0; i < IPSET_NET_COUNT; i++) {
646 mtype_add_cidr(h, CIDR(d->cidr), NETS_LENGTH(set->family)); 671 mtype_del_cidr(h, CIDR(data->cidr, i),
672 NLEN(set->family), i);
673 mtype_add_cidr(h, CIDR(d->cidr, i),
674 NLEN(set->family), i);
675 }
647#endif 676#endif
677 ip_set_ext_destroy(set, data);
648 } else { 678 } else {
649 /* Use/create a new slot */ 679 /* Use/create a new slot */
650 TUNE_AHASH_MAX(h, multi); 680 TUNE_AHASH_MAX(h, multi);
651 ret = hbucket_elem_add(n, AHASH_MAX(h), h->dsize); 681 ret = hbucket_elem_add(n, AHASH_MAX(h), set->dsize);
652 if (ret != 0) { 682 if (ret != 0) {
653 if (ret == -EAGAIN) 683 if (ret == -EAGAIN)
654 mtype_data_next(&h->next, d); 684 mtype_data_next(&h->next, d);
655 goto out; 685 goto out;
656 } 686 }
657 data = ahash_data(n, n->pos++, h->dsize); 687 data = ahash_data(n, n->pos++, set->dsize);
658#ifdef IP_SET_HASH_WITH_NETS 688#ifdef IP_SET_HASH_WITH_NETS
659 mtype_add_cidr(h, CIDR(d->cidr), NETS_LENGTH(set->family)); 689 for (i = 0; i < IPSET_NET_COUNT; i++)
690 mtype_add_cidr(h, CIDR(d->cidr, i), NLEN(set->family),
691 i);
660#endif 692#endif
661 h->elements++; 693 h->elements++;
662 } 694 }
@@ -665,9 +697,11 @@ reuse_slot:
665 mtype_data_set_flags(data, flags); 697 mtype_data_set_flags(data, flags);
666#endif 698#endif
667 if (SET_WITH_TIMEOUT(set)) 699 if (SET_WITH_TIMEOUT(set))
668 ip_set_timeout_set(ext_timeout(data, h), ext->timeout); 700 ip_set_timeout_set(ext_timeout(data, set), ext->timeout);
669 if (SET_WITH_COUNTER(set)) 701 if (SET_WITH_COUNTER(set))
670 ip_set_init_counter(ext_counter(data, h), ext); 702 ip_set_init_counter(ext_counter(data, set), ext);
703 if (SET_WITH_COMMENT(set))
704 ip_set_init_comment(ext_comment(data, set), ext);
671 705
672out: 706out:
673 rcu_read_unlock_bh(); 707 rcu_read_unlock_bh();
@@ -682,47 +716,60 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
682 struct ip_set_ext *mext, u32 flags) 716 struct ip_set_ext *mext, u32 flags)
683{ 717{
684 struct htype *h = set->data; 718 struct htype *h = set->data;
685 struct htable *t = h->table; 719 struct htable *t;
686 const struct mtype_elem *d = value; 720 const struct mtype_elem *d = value;
687 struct mtype_elem *data; 721 struct mtype_elem *data;
688 struct hbucket *n; 722 struct hbucket *n;
689 int i; 723 int i, ret = -IPSET_ERR_EXIST;
724#ifdef IP_SET_HASH_WITH_NETS
725 u8 j;
726#endif
690 u32 key, multi = 0; 727 u32 key, multi = 0;
691 728
729 rcu_read_lock_bh();
730 t = rcu_dereference_bh(h->table);
692 key = HKEY(value, h->initval, t->htable_bits); 731 key = HKEY(value, h->initval, t->htable_bits);
693 n = hbucket(t, key); 732 n = hbucket(t, key);
694 for (i = 0; i < n->pos; i++) { 733 for (i = 0; i < n->pos; i++) {
695 data = ahash_data(n, i, h->dsize); 734 data = ahash_data(n, i, set->dsize);
696 if (!mtype_data_equal(data, d, &multi)) 735 if (!mtype_data_equal(data, d, &multi))
697 continue; 736 continue;
698 if (SET_WITH_TIMEOUT(set) && 737 if (SET_WITH_TIMEOUT(set) &&
699 ip_set_timeout_expired(ext_timeout(data, h))) 738 ip_set_timeout_expired(ext_timeout(data, set)))
700 return -IPSET_ERR_EXIST; 739 goto out;
701 if (i != n->pos - 1) 740 if (i != n->pos - 1)
702 /* Not last one */ 741 /* Not last one */
703 memcpy(data, ahash_data(n, n->pos - 1, h->dsize), 742 memcpy(data, ahash_data(n, n->pos - 1, set->dsize),
704 h->dsize); 743 set->dsize);
705 744
706 n->pos--; 745 n->pos--;
707 h->elements--; 746 h->elements--;
708#ifdef IP_SET_HASH_WITH_NETS 747#ifdef IP_SET_HASH_WITH_NETS
709 mtype_del_cidr(h, CIDR(d->cidr), NETS_LENGTH(set->family)); 748 for (j = 0; j < IPSET_NET_COUNT; j++)
749 mtype_del_cidr(h, CIDR(d->cidr, j), NLEN(set->family),
750 j);
710#endif 751#endif
752 ip_set_ext_destroy(set, data);
711 if (n->pos + AHASH_INIT_SIZE < n->size) { 753 if (n->pos + AHASH_INIT_SIZE < n->size) {
712 void *tmp = kzalloc((n->size - AHASH_INIT_SIZE) 754 void *tmp = kzalloc((n->size - AHASH_INIT_SIZE)
713 * h->dsize, 755 * set->dsize,
714 GFP_ATOMIC); 756 GFP_ATOMIC);
715 if (!tmp) 757 if (!tmp) {
716 return 0; 758 ret = 0;
759 goto out;
760 }
717 n->size -= AHASH_INIT_SIZE; 761 n->size -= AHASH_INIT_SIZE;
718 memcpy(tmp, n->value, n->size * h->dsize); 762 memcpy(tmp, n->value, n->size * set->dsize);
719 kfree(n->value); 763 kfree(n->value);
720 n->value = tmp; 764 n->value = tmp;
721 } 765 }
722 return 0; 766 ret = 0;
767 goto out;
723 } 768 }
724 769
725 return -IPSET_ERR_EXIST; 770out:
771 rcu_read_unlock_bh();
772 return ret;
726} 773}
727 774
728static inline int 775static inline int
@@ -730,8 +777,7 @@ mtype_data_match(struct mtype_elem *data, const struct ip_set_ext *ext,
730 struct ip_set_ext *mext, struct ip_set *set, u32 flags) 777 struct ip_set_ext *mext, struct ip_set *set, u32 flags)
731{ 778{
732 if (SET_WITH_COUNTER(set)) 779 if (SET_WITH_COUNTER(set))
733 ip_set_update_counter(ext_counter(data, 780 ip_set_update_counter(ext_counter(data, set),
734 (struct htype *)(set->data)),
735 ext, mext, flags); 781 ext, mext, flags);
736 return mtype_do_data_match(data); 782 return mtype_do_data_match(data);
737} 783}
@@ -745,25 +791,38 @@ mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
745 struct ip_set_ext *mext, u32 flags) 791 struct ip_set_ext *mext, u32 flags)
746{ 792{
747 struct htype *h = set->data; 793 struct htype *h = set->data;
748 struct htable *t = h->table; 794 struct htable *t = rcu_dereference_bh(h->table);
749 struct hbucket *n; 795 struct hbucket *n;
750 struct mtype_elem *data; 796 struct mtype_elem *data;
797#if IPSET_NET_COUNT == 2
798 struct mtype_elem orig = *d;
799 int i, j = 0, k;
800#else
751 int i, j = 0; 801 int i, j = 0;
802#endif
752 u32 key, multi = 0; 803 u32 key, multi = 0;
753 u8 nets_length = NETS_LENGTH(set->family); 804 u8 nets_length = NLEN(set->family);
754 805
755 pr_debug("test by nets\n"); 806 pr_debug("test by nets\n");
756 for (; j < nets_length && h->nets[j].nets && !multi; j++) { 807 for (; j < nets_length && h->nets[j].nets[0] && !multi; j++) {
757 mtype_data_netmask(d, h->nets[j].cidr); 808#if IPSET_NET_COUNT == 2
809 mtype_data_reset_elem(d, &orig);
810 mtype_data_netmask(d, h->nets[j].cidr[0], false);
811 for (k = 0; k < nets_length && h->nets[k].nets[1] && !multi;
812 k++) {
813 mtype_data_netmask(d, h->nets[k].cidr[1], true);
814#else
815 mtype_data_netmask(d, h->nets[j].cidr[0]);
816#endif
758 key = HKEY(d, h->initval, t->htable_bits); 817 key = HKEY(d, h->initval, t->htable_bits);
759 n = hbucket(t, key); 818 n = hbucket(t, key);
760 for (i = 0; i < n->pos; i++) { 819 for (i = 0; i < n->pos; i++) {
761 data = ahash_data(n, i, h->dsize); 820 data = ahash_data(n, i, set->dsize);
762 if (!mtype_data_equal(data, d, &multi)) 821 if (!mtype_data_equal(data, d, &multi))
763 continue; 822 continue;
764 if (SET_WITH_TIMEOUT(set)) { 823 if (SET_WITH_TIMEOUT(set)) {
765 if (!ip_set_timeout_expired( 824 if (!ip_set_timeout_expired(
766 ext_timeout(data, h))) 825 ext_timeout(data, set)))
767 return mtype_data_match(data, ext, 826 return mtype_data_match(data, ext,
768 mext, set, 827 mext, set,
769 flags); 828 flags);
@@ -774,6 +833,9 @@ mtype_test_cidrs(struct ip_set *set, struct mtype_elem *d,
774 return mtype_data_match(data, ext, 833 return mtype_data_match(data, ext,
775 mext, set, flags); 834 mext, set, flags);
776 } 835 }
836#if IPSET_NET_COUNT == 2
837 }
838#endif
777 } 839 }
778 return 0; 840 return 0;
779} 841}
@@ -785,30 +847,41 @@ mtype_test(struct ip_set *set, void *value, const struct ip_set_ext *ext,
785 struct ip_set_ext *mext, u32 flags) 847 struct ip_set_ext *mext, u32 flags)
786{ 848{
787 struct htype *h = set->data; 849 struct htype *h = set->data;
788 struct htable *t = h->table; 850 struct htable *t;
789 struct mtype_elem *d = value; 851 struct mtype_elem *d = value;
790 struct hbucket *n; 852 struct hbucket *n;
791 struct mtype_elem *data; 853 struct mtype_elem *data;
792 int i; 854 int i, ret = 0;
793 u32 key, multi = 0; 855 u32 key, multi = 0;
794 856
857 rcu_read_lock_bh();
858 t = rcu_dereference_bh(h->table);
795#ifdef IP_SET_HASH_WITH_NETS 859#ifdef IP_SET_HASH_WITH_NETS
796 /* If we test an IP address and not a network address, 860 /* If we test an IP address and not a network address,
797 * try all possible network sizes */ 861 * try all possible network sizes */
798 if (CIDR(d->cidr) == SET_HOST_MASK(set->family)) 862 for (i = 0; i < IPSET_NET_COUNT; i++)
799 return mtype_test_cidrs(set, d, ext, mext, flags); 863 if (CIDR(d->cidr, i) != SET_HOST_MASK(set->family))
864 break;
865 if (i == IPSET_NET_COUNT) {
866 ret = mtype_test_cidrs(set, d, ext, mext, flags);
867 goto out;
868 }
800#endif 869#endif
801 870
802 key = HKEY(d, h->initval, t->htable_bits); 871 key = HKEY(d, h->initval, t->htable_bits);
803 n = hbucket(t, key); 872 n = hbucket(t, key);
804 for (i = 0; i < n->pos; i++) { 873 for (i = 0; i < n->pos; i++) {
805 data = ahash_data(n, i, h->dsize); 874 data = ahash_data(n, i, set->dsize);
806 if (mtype_data_equal(data, d, &multi) && 875 if (mtype_data_equal(data, d, &multi) &&
807 !(SET_WITH_TIMEOUT(set) && 876 !(SET_WITH_TIMEOUT(set) &&
808 ip_set_timeout_expired(ext_timeout(data, h)))) 877 ip_set_timeout_expired(ext_timeout(data, set)))) {
809 return mtype_data_match(data, ext, mext, set, flags); 878 ret = mtype_data_match(data, ext, mext, set, flags);
879 goto out;
880 }
810 } 881 }
811 return 0; 882out:
883 rcu_read_unlock_bh();
884 return ret;
812} 885}
813 886
814/* Reply a HEADER request: fill out the header part of the set */ 887/* Reply a HEADER request: fill out the header part of the set */
@@ -816,18 +889,18 @@ static int
816mtype_head(struct ip_set *set, struct sk_buff *skb) 889mtype_head(struct ip_set *set, struct sk_buff *skb)
817{ 890{
818 const struct htype *h = set->data; 891 const struct htype *h = set->data;
892 const struct htable *t;
819 struct nlattr *nested; 893 struct nlattr *nested;
820 size_t memsize; 894 size_t memsize;
821 895
822 read_lock_bh(&set->lock); 896 t = rcu_dereference_bh_nfnl(h->table);
823 memsize = mtype_ahash_memsize(h, NETS_LENGTH(set->family)); 897 memsize = mtype_ahash_memsize(h, t, NLEN(set->family), set->dsize);
824 read_unlock_bh(&set->lock);
825 898
826 nested = ipset_nest_start(skb, IPSET_ATTR_DATA); 899 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
827 if (!nested) 900 if (!nested)
828 goto nla_put_failure; 901 goto nla_put_failure;
829 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE, 902 if (nla_put_net32(skb, IPSET_ATTR_HASHSIZE,
830 htonl(jhash_size(h->table->htable_bits))) || 903 htonl(jhash_size(t->htable_bits))) ||
831 nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem))) 904 nla_put_net32(skb, IPSET_ATTR_MAXELEM, htonl(h->maxelem)))
832 goto nla_put_failure; 905 goto nla_put_failure;
833#ifdef IP_SET_HASH_WITH_NETMASK 906#ifdef IP_SET_HASH_WITH_NETMASK
@@ -836,12 +909,9 @@ mtype_head(struct ip_set *set, struct sk_buff *skb)
836 goto nla_put_failure; 909 goto nla_put_failure;
837#endif 910#endif
838 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) || 911 if (nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
839 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)) || 912 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, htonl(memsize)))
840 ((set->extensions & IPSET_EXT_TIMEOUT) && 913 goto nla_put_failure;
841 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(h->timeout))) || 914 if (unlikely(ip_set_put_flags(skb, set)))
842 ((set->extensions & IPSET_EXT_COUNTER) &&
843 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS,
844 htonl(IPSET_FLAG_WITH_COUNTERS))))
845 goto nla_put_failure; 915 goto nla_put_failure;
846 ipset_nest_end(skb, nested); 916 ipset_nest_end(skb, nested);
847 917
@@ -856,11 +926,11 @@ mtype_list(const struct ip_set *set,
856 struct sk_buff *skb, struct netlink_callback *cb) 926 struct sk_buff *skb, struct netlink_callback *cb)
857{ 927{
858 const struct htype *h = set->data; 928 const struct htype *h = set->data;
859 const struct htable *t = h->table; 929 const struct htable *t = rcu_dereference_bh_nfnl(h->table);
860 struct nlattr *atd, *nested; 930 struct nlattr *atd, *nested;
861 const struct hbucket *n; 931 const struct hbucket *n;
862 const struct mtype_elem *e; 932 const struct mtype_elem *e;
863 u32 first = cb->args[2]; 933 u32 first = cb->args[IPSET_CB_ARG0];
864 /* We assume that one hash bucket fills into one page */ 934 /* We assume that one hash bucket fills into one page */
865 void *incomplete; 935 void *incomplete;
866 int i; 936 int i;
@@ -869,20 +939,22 @@ mtype_list(const struct ip_set *set,
869 if (!atd) 939 if (!atd)
870 return -EMSGSIZE; 940 return -EMSGSIZE;
871 pr_debug("list hash set %s\n", set->name); 941 pr_debug("list hash set %s\n", set->name);
872 for (; cb->args[2] < jhash_size(t->htable_bits); cb->args[2]++) { 942 for (; cb->args[IPSET_CB_ARG0] < jhash_size(t->htable_bits);
943 cb->args[IPSET_CB_ARG0]++) {
873 incomplete = skb_tail_pointer(skb); 944 incomplete = skb_tail_pointer(skb);
874 n = hbucket(t, cb->args[2]); 945 n = hbucket(t, cb->args[IPSET_CB_ARG0]);
875 pr_debug("cb->args[2]: %lu, t %p n %p\n", cb->args[2], t, n); 946 pr_debug("cb->arg bucket: %lu, t %p n %p\n",
947 cb->args[IPSET_CB_ARG0], t, n);
876 for (i = 0; i < n->pos; i++) { 948 for (i = 0; i < n->pos; i++) {
877 e = ahash_data(n, i, h->dsize); 949 e = ahash_data(n, i, set->dsize);
878 if (SET_WITH_TIMEOUT(set) && 950 if (SET_WITH_TIMEOUT(set) &&
879 ip_set_timeout_expired(ext_timeout(e, h))) 951 ip_set_timeout_expired(ext_timeout(e, set)))
880 continue; 952 continue;
881 pr_debug("list hash %lu hbucket %p i %u, data %p\n", 953 pr_debug("list hash %lu hbucket %p i %u, data %p\n",
882 cb->args[2], n, i, e); 954 cb->args[IPSET_CB_ARG0], n, i, e);
883 nested = ipset_nest_start(skb, IPSET_ATTR_DATA); 955 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
884 if (!nested) { 956 if (!nested) {
885 if (cb->args[2] == first) { 957 if (cb->args[IPSET_CB_ARG0] == first) {
886 nla_nest_cancel(skb, atd); 958 nla_nest_cancel(skb, atd);
887 return -EMSGSIZE; 959 return -EMSGSIZE;
888 } else 960 } else
@@ -890,43 +962,37 @@ mtype_list(const struct ip_set *set,
890 } 962 }
891 if (mtype_data_list(skb, e)) 963 if (mtype_data_list(skb, e))
892 goto nla_put_failure; 964 goto nla_put_failure;
893 if (SET_WITH_TIMEOUT(set) && 965 if (ip_set_put_extensions(skb, set, e, true))
894 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
895 htonl(ip_set_timeout_get(
896 ext_timeout(e, h)))))
897 goto nla_put_failure;
898 if (SET_WITH_COUNTER(set) &&
899 ip_set_put_counter(skb, ext_counter(e, h)))
900 goto nla_put_failure; 966 goto nla_put_failure;
901 ipset_nest_end(skb, nested); 967 ipset_nest_end(skb, nested);
902 } 968 }
903 } 969 }
904 ipset_nest_end(skb, atd); 970 ipset_nest_end(skb, atd);
905 /* Set listing finished */ 971 /* Set listing finished */
906 cb->args[2] = 0; 972 cb->args[IPSET_CB_ARG0] = 0;
907 973
908 return 0; 974 return 0;
909 975
910nla_put_failure: 976nla_put_failure:
911 nlmsg_trim(skb, incomplete); 977 nlmsg_trim(skb, incomplete);
912 ipset_nest_end(skb, atd); 978 if (unlikely(first == cb->args[IPSET_CB_ARG0])) {
913 if (unlikely(first == cb->args[2])) {
914 pr_warning("Can't list set %s: one bucket does not fit into " 979 pr_warning("Can't list set %s: one bucket does not fit into "
915 "a message. Please report it!\n", set->name); 980 "a message. Please report it!\n", set->name);
916 cb->args[2] = 0; 981 cb->args[IPSET_CB_ARG0] = 0;
917 return -EMSGSIZE; 982 return -EMSGSIZE;
918 } 983 }
984 ipset_nest_end(skb, atd);
919 return 0; 985 return 0;
920} 986}
921 987
922static int 988static int
923TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb, 989IPSET_TOKEN(MTYPE, _kadt)(struct ip_set *set, const struct sk_buff *skb,
924 const struct xt_action_param *par, 990 const struct xt_action_param *par,
925 enum ipset_adt adt, struct ip_set_adt_opt *opt); 991 enum ipset_adt adt, struct ip_set_adt_opt *opt);
926 992
927static int 993static int
928TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[], 994IPSET_TOKEN(MTYPE, _uadt)(struct ip_set *set, struct nlattr *tb[],
929 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried); 995 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried);
930 996
931static const struct ip_set_type_variant mtype_variant = { 997static const struct ip_set_type_variant mtype_variant = {
932 .kadt = mtype_kadt, 998 .kadt = mtype_kadt,
@@ -946,16 +1012,17 @@ static const struct ip_set_type_variant mtype_variant = {
946 1012
947#ifdef IP_SET_EMIT_CREATE 1013#ifdef IP_SET_EMIT_CREATE
948static int 1014static int
949TOKEN(HTYPE, _create)(struct ip_set *set, struct nlattr *tb[], u32 flags) 1015IPSET_TOKEN(HTYPE, _create)(struct net *net, struct ip_set *set,
1016 struct nlattr *tb[], u32 flags)
950{ 1017{
951 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM; 1018 u32 hashsize = IPSET_DEFAULT_HASHSIZE, maxelem = IPSET_DEFAULT_MAXELEM;
952 u32 cadt_flags = 0;
953 u8 hbits; 1019 u8 hbits;
954#ifdef IP_SET_HASH_WITH_NETMASK 1020#ifdef IP_SET_HASH_WITH_NETMASK
955 u8 netmask; 1021 u8 netmask;
956#endif 1022#endif
957 size_t hsize; 1023 size_t hsize;
958 struct HTYPE *h; 1024 struct HTYPE *h;
1025 struct htable *t;
959 1026
960 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6)) 1027 if (!(set->family == NFPROTO_IPV4 || set->family == NFPROTO_IPV6))
961 return -IPSET_ERR_INVALID_FAMILY; 1028 return -IPSET_ERR_INVALID_FAMILY;
@@ -1005,7 +1072,7 @@ TOKEN(HTYPE, _create)(struct ip_set *set, struct nlattr *tb[], u32 flags)
1005 h->netmask = netmask; 1072 h->netmask = netmask;
1006#endif 1073#endif
1007 get_random_bytes(&h->initval, sizeof(h->initval)); 1074 get_random_bytes(&h->initval, sizeof(h->initval));
1008 h->timeout = IPSET_NO_TIMEOUT; 1075 set->timeout = IPSET_NO_TIMEOUT;
1009 1076
1010 hbits = htable_bits(hashsize); 1077 hbits = htable_bits(hashsize);
1011 hsize = htable_size(hbits); 1078 hsize = htable_size(hbits);
@@ -1013,91 +1080,37 @@ TOKEN(HTYPE, _create)(struct ip_set *set, struct nlattr *tb[], u32 flags)
1013 kfree(h); 1080 kfree(h);
1014 return -ENOMEM; 1081 return -ENOMEM;
1015 } 1082 }
1016 h->table = ip_set_alloc(hsize); 1083 t = ip_set_alloc(hsize);
1017 if (!h->table) { 1084 if (!t) {
1018 kfree(h); 1085 kfree(h);
1019 return -ENOMEM; 1086 return -ENOMEM;
1020 } 1087 }
1021 h->table->htable_bits = hbits; 1088 t->htable_bits = hbits;
1089 rcu_assign_pointer(h->table, t);
1022 1090
1023 set->data = h; 1091 set->data = h;
1024 if (set->family == NFPROTO_IPV4) 1092 if (set->family == NFPROTO_IPV4) {
1025 set->variant = &TOKEN(HTYPE, 4_variant); 1093 set->variant = &IPSET_TOKEN(HTYPE, 4_variant);
1026 else 1094 set->dsize = ip_set_elem_len(set, tb,
1027 set->variant = &TOKEN(HTYPE, 6_variant); 1095 sizeof(struct IPSET_TOKEN(HTYPE, 4_elem)));
1028
1029 if (tb[IPSET_ATTR_CADT_FLAGS])
1030 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
1031 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) {
1032 set->extensions |= IPSET_EXT_COUNTER;
1033 if (tb[IPSET_ATTR_TIMEOUT]) {
1034 h->timeout =
1035 ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1036 set->extensions |= IPSET_EXT_TIMEOUT;
1037 if (set->family == NFPROTO_IPV4) {
1038 h->dsize =
1039 sizeof(struct TOKEN(HTYPE, 4ct_elem));
1040 h->offset[IPSET_OFFSET_TIMEOUT] =
1041 offsetof(struct TOKEN(HTYPE, 4ct_elem),
1042 timeout);
1043 h->offset[IPSET_OFFSET_COUNTER] =
1044 offsetof(struct TOKEN(HTYPE, 4ct_elem),
1045 counter);
1046 TOKEN(HTYPE, 4_gc_init)(set,
1047 TOKEN(HTYPE, 4_gc));
1048 } else {
1049 h->dsize =
1050 sizeof(struct TOKEN(HTYPE, 6ct_elem));
1051 h->offset[IPSET_OFFSET_TIMEOUT] =
1052 offsetof(struct TOKEN(HTYPE, 6ct_elem),
1053 timeout);
1054 h->offset[IPSET_OFFSET_COUNTER] =
1055 offsetof(struct TOKEN(HTYPE, 6ct_elem),
1056 counter);
1057 TOKEN(HTYPE, 6_gc_init)(set,
1058 TOKEN(HTYPE, 6_gc));
1059 }
1060 } else {
1061 if (set->family == NFPROTO_IPV4) {
1062 h->dsize =
1063 sizeof(struct TOKEN(HTYPE, 4c_elem));
1064 h->offset[IPSET_OFFSET_COUNTER] =
1065 offsetof(struct TOKEN(HTYPE, 4c_elem),
1066 counter);
1067 } else {
1068 h->dsize =
1069 sizeof(struct TOKEN(HTYPE, 6c_elem));
1070 h->offset[IPSET_OFFSET_COUNTER] =
1071 offsetof(struct TOKEN(HTYPE, 6c_elem),
1072 counter);
1073 }
1074 }
1075 } else if (tb[IPSET_ATTR_TIMEOUT]) {
1076 h->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1077 set->extensions |= IPSET_EXT_TIMEOUT;
1078 if (set->family == NFPROTO_IPV4) {
1079 h->dsize = sizeof(struct TOKEN(HTYPE, 4t_elem));
1080 h->offset[IPSET_OFFSET_TIMEOUT] =
1081 offsetof(struct TOKEN(HTYPE, 4t_elem),
1082 timeout);
1083 TOKEN(HTYPE, 4_gc_init)(set, TOKEN(HTYPE, 4_gc));
1084 } else {
1085 h->dsize = sizeof(struct TOKEN(HTYPE, 6t_elem));
1086 h->offset[IPSET_OFFSET_TIMEOUT] =
1087 offsetof(struct TOKEN(HTYPE, 6t_elem),
1088 timeout);
1089 TOKEN(HTYPE, 6_gc_init)(set, TOKEN(HTYPE, 6_gc));
1090 }
1091 } else { 1096 } else {
1097 set->variant = &IPSET_TOKEN(HTYPE, 6_variant);
1098 set->dsize = ip_set_elem_len(set, tb,
1099 sizeof(struct IPSET_TOKEN(HTYPE, 6_elem)));
1100 }
1101 if (tb[IPSET_ATTR_TIMEOUT]) {
1102 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
1092 if (set->family == NFPROTO_IPV4) 1103 if (set->family == NFPROTO_IPV4)
1093 h->dsize = sizeof(struct TOKEN(HTYPE, 4_elem)); 1104 IPSET_TOKEN(HTYPE, 4_gc_init)(set,
1105 IPSET_TOKEN(HTYPE, 4_gc));
1094 else 1106 else
1095 h->dsize = sizeof(struct TOKEN(HTYPE, 6_elem)); 1107 IPSET_TOKEN(HTYPE, 6_gc_init)(set,
1108 IPSET_TOKEN(HTYPE, 6_gc));
1096 } 1109 }
1097 1110
1098 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n", 1111 pr_debug("create %s hashsize %u (%u) maxelem %u: %p(%p)\n",
1099 set->name, jhash_size(h->table->htable_bits), 1112 set->name, jhash_size(t->htable_bits),
1100 h->table->htable_bits, h->maxelem, set->data, h->table); 1113 t->htable_bits, h->maxelem, set->data, t);
1101 1114
1102 return 0; 1115 return 0;
1103} 1116}
diff --git a/net/netfilter/ipset/ip_set_hash_ip.c b/net/netfilter/ipset/ip_set_hash_ip.c
index c74e6e14cd93..e65fc2423d56 100644
--- a/net/netfilter/ipset/ip_set_hash_ip.c
+++ b/net/netfilter/ipset/ip_set_hash_ip.c
@@ -23,19 +23,20 @@
23#include <linux/netfilter/ipset/ip_set.h> 23#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_hash.h> 24#include <linux/netfilter/ipset/ip_set_hash.h>
25 25
26#define REVISION_MIN 0 26#define IPSET_TYPE_REV_MIN 0
27#define REVISION_MAX 1 /* Counters support */ 27/* 1 Counters support */
28#define IPSET_TYPE_REV_MAX 2 /* Comments support */
28 29
29MODULE_LICENSE("GPL"); 30MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 31MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
31IP_SET_MODULE_DESC("hash:ip", REVISION_MIN, REVISION_MAX); 32IP_SET_MODULE_DESC("hash:ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32MODULE_ALIAS("ip_set_hash:ip"); 33MODULE_ALIAS("ip_set_hash:ip");
33 34
34/* Type specific function prefix */ 35/* Type specific function prefix */
35#define HTYPE hash_ip 36#define HTYPE hash_ip
36#define IP_SET_HASH_WITH_NETMASK 37#define IP_SET_HASH_WITH_NETMASK
37 38
38/* IPv4 variants */ 39/* IPv4 variant */
39 40
40/* Member elements */ 41/* Member elements */
41struct hash_ip4_elem { 42struct hash_ip4_elem {
@@ -43,22 +44,6 @@ struct hash_ip4_elem {
43 __be32 ip; 44 __be32 ip;
44}; 45};
45 46
46struct hash_ip4t_elem {
47 __be32 ip;
48 unsigned long timeout;
49};
50
51struct hash_ip4c_elem {
52 __be32 ip;
53 struct ip_set_counter counter;
54};
55
56struct hash_ip4ct_elem {
57 __be32 ip;
58 struct ip_set_counter counter;
59 unsigned long timeout;
60};
61
62/* Common functions */ 47/* Common functions */
63 48
64static inline bool 49static inline bool
@@ -99,7 +84,7 @@ hash_ip4_kadt(struct ip_set *set, const struct sk_buff *skb,
99 const struct hash_ip *h = set->data; 84 const struct hash_ip *h = set->data;
100 ipset_adtfn adtfn = set->variant->adt[adt]; 85 ipset_adtfn adtfn = set->variant->adt[adt];
101 struct hash_ip4_elem e = {}; 86 struct hash_ip4_elem e = {};
102 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 87 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
103 __be32 ip; 88 __be32 ip;
104 89
105 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip); 90 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &ip);
@@ -118,8 +103,8 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
118 const struct hash_ip *h = set->data; 103 const struct hash_ip *h = set->data;
119 ipset_adtfn adtfn = set->variant->adt[adt]; 104 ipset_adtfn adtfn = set->variant->adt[adt];
120 struct hash_ip4_elem e = {}; 105 struct hash_ip4_elem e = {};
121 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 106 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
122 u32 ip, ip_to, hosts; 107 u32 ip = 0, ip_to = 0, hosts;
123 int ret = 0; 108 int ret = 0;
124 109
125 if (unlikely(!tb[IPSET_ATTR_IP] || 110 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -178,29 +163,13 @@ hash_ip4_uadt(struct ip_set *set, struct nlattr *tb[],
178 return ret; 163 return ret;
179} 164}
180 165
181/* IPv6 variants */ 166/* IPv6 variant */
182 167
183/* Member elements */ 168/* Member elements */
184struct hash_ip6_elem { 169struct hash_ip6_elem {
185 union nf_inet_addr ip; 170 union nf_inet_addr ip;
186}; 171};
187 172
188struct hash_ip6t_elem {
189 union nf_inet_addr ip;
190 unsigned long timeout;
191};
192
193struct hash_ip6c_elem {
194 union nf_inet_addr ip;
195 struct ip_set_counter counter;
196};
197
198struct hash_ip6ct_elem {
199 union nf_inet_addr ip;
200 struct ip_set_counter counter;
201 unsigned long timeout;
202};
203
204/* Common functions */ 173/* Common functions */
205 174
206static inline bool 175static inline bool
@@ -253,7 +222,7 @@ hash_ip6_kadt(struct ip_set *set, const struct sk_buff *skb,
253 const struct hash_ip *h = set->data; 222 const struct hash_ip *h = set->data;
254 ipset_adtfn adtfn = set->variant->adt[adt]; 223 ipset_adtfn adtfn = set->variant->adt[adt];
255 struct hash_ip6_elem e = {}; 224 struct hash_ip6_elem e = {};
256 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 225 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
257 226
258 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6); 227 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
259 hash_ip6_netmask(&e.ip, h->netmask); 228 hash_ip6_netmask(&e.ip, h->netmask);
@@ -270,7 +239,7 @@ hash_ip6_uadt(struct ip_set *set, struct nlattr *tb[],
270 const struct hash_ip *h = set->data; 239 const struct hash_ip *h = set->data;
271 ipset_adtfn adtfn = set->variant->adt[adt]; 240 ipset_adtfn adtfn = set->variant->adt[adt];
272 struct hash_ip6_elem e = {}; 241 struct hash_ip6_elem e = {};
273 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 242 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
274 int ret; 243 int ret;
275 244
276 if (unlikely(!tb[IPSET_ATTR_IP] || 245 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -304,8 +273,8 @@ static struct ip_set_type hash_ip_type __read_mostly = {
304 .features = IPSET_TYPE_IP, 273 .features = IPSET_TYPE_IP,
305 .dimension = IPSET_DIM_ONE, 274 .dimension = IPSET_DIM_ONE,
306 .family = NFPROTO_UNSPEC, 275 .family = NFPROTO_UNSPEC,
307 .revision_min = REVISION_MIN, 276 .revision_min = IPSET_TYPE_REV_MIN,
308 .revision_max = REVISION_MAX, 277 .revision_max = IPSET_TYPE_REV_MAX,
309 .create = hash_ip_create, 278 .create = hash_ip_create,
310 .create_policy = { 279 .create_policy = {
311 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 280 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -324,6 +293,7 @@ static struct ip_set_type hash_ip_type __read_mostly = {
324 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 293 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
325 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 294 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
326 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 295 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
296 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
327 }, 297 },
328 .me = THIS_MODULE, 298 .me = THIS_MODULE,
329}; 299};
diff --git a/net/netfilter/ipset/ip_set_hash_ipport.c b/net/netfilter/ipset/ip_set_hash_ipport.c
index 7a2d2bd98d04..525a595dd1fe 100644
--- a/net/netfilter/ipset/ip_set_hash_ipport.c
+++ b/net/netfilter/ipset/ip_set_hash_ipport.c
@@ -24,19 +24,20 @@
24#include <linux/netfilter/ipset/ip_set_getport.h> 24#include <linux/netfilter/ipset/ip_set_getport.h>
25#include <linux/netfilter/ipset/ip_set_hash.h> 25#include <linux/netfilter/ipset/ip_set_hash.h>
26 26
27#define REVISION_MIN 0 27#define IPSET_TYPE_REV_MIN 0
28/* 1 SCTP and UDPLITE support added */ 28/* 1 SCTP and UDPLITE support added */
29#define REVISION_MAX 2 /* Counters support added */ 29/* 2 Counters support added */
30#define IPSET_TYPE_REV_MAX 3 /* Comments support added */
30 31
31MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33IP_SET_MODULE_DESC("hash:ip,port", REVISION_MIN, REVISION_MAX); 34IP_SET_MODULE_DESC("hash:ip,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_hash:ip,port"); 35MODULE_ALIAS("ip_set_hash:ip,port");
35 36
36/* Type specific function prefix */ 37/* Type specific function prefix */
37#define HTYPE hash_ipport 38#define HTYPE hash_ipport
38 39
39/* IPv4 variants */ 40/* IPv4 variant */
40 41
41/* Member elements */ 42/* Member elements */
42struct hash_ipport4_elem { 43struct hash_ipport4_elem {
@@ -46,31 +47,6 @@ struct hash_ipport4_elem {
46 u8 padding; 47 u8 padding;
47}; 48};
48 49
49struct hash_ipport4t_elem {
50 __be32 ip;
51 __be16 port;
52 u8 proto;
53 u8 padding;
54 unsigned long timeout;
55};
56
57struct hash_ipport4c_elem {
58 __be32 ip;
59 __be16 port;
60 u8 proto;
61 u8 padding;
62 struct ip_set_counter counter;
63};
64
65struct hash_ipport4ct_elem {
66 __be32 ip;
67 __be16 port;
68 u8 proto;
69 u8 padding;
70 struct ip_set_counter counter;
71 unsigned long timeout;
72};
73
74/* Common functions */ 50/* Common functions */
75 51
76static inline bool 52static inline bool
@@ -116,10 +92,9 @@ hash_ipport4_kadt(struct ip_set *set, const struct sk_buff *skb,
116 const struct xt_action_param *par, 92 const struct xt_action_param *par,
117 enum ipset_adt adt, struct ip_set_adt_opt *opt) 93 enum ipset_adt adt, struct ip_set_adt_opt *opt)
118{ 94{
119 const struct hash_ipport *h = set->data;
120 ipset_adtfn adtfn = set->variant->adt[adt]; 95 ipset_adtfn adtfn = set->variant->adt[adt];
121 struct hash_ipport4_elem e = { }; 96 struct hash_ipport4_elem e = { };
122 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 97 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
123 98
124 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 99 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
125 &e.port, &e.proto)) 100 &e.port, &e.proto))
@@ -136,8 +111,8 @@ hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
136 const struct hash_ipport *h = set->data; 111 const struct hash_ipport *h = set->data;
137 ipset_adtfn adtfn = set->variant->adt[adt]; 112 ipset_adtfn adtfn = set->variant->adt[adt];
138 struct hash_ipport4_elem e = { }; 113 struct hash_ipport4_elem e = { };
139 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 114 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
140 u32 ip, ip_to, p = 0, port, port_to; 115 u32 ip, ip_to = 0, p = 0, port, port_to;
141 bool with_ports = false; 116 bool with_ports = false;
142 int ret; 117 int ret;
143 118
@@ -222,7 +197,7 @@ hash_ipport4_uadt(struct ip_set *set, struct nlattr *tb[],
222 return ret; 197 return ret;
223} 198}
224 199
225/* IPv6 variants */ 200/* IPv6 variant */
226 201
227struct hash_ipport6_elem { 202struct hash_ipport6_elem {
228 union nf_inet_addr ip; 203 union nf_inet_addr ip;
@@ -231,31 +206,6 @@ struct hash_ipport6_elem {
231 u8 padding; 206 u8 padding;
232}; 207};
233 208
234struct hash_ipport6t_elem {
235 union nf_inet_addr ip;
236 __be16 port;
237 u8 proto;
238 u8 padding;
239 unsigned long timeout;
240};
241
242struct hash_ipport6c_elem {
243 union nf_inet_addr ip;
244 __be16 port;
245 u8 proto;
246 u8 padding;
247 struct ip_set_counter counter;
248};
249
250struct hash_ipport6ct_elem {
251 union nf_inet_addr ip;
252 __be16 port;
253 u8 proto;
254 u8 padding;
255 struct ip_set_counter counter;
256 unsigned long timeout;
257};
258
259/* Common functions */ 209/* Common functions */
260 210
261static inline bool 211static inline bool
@@ -306,10 +256,9 @@ hash_ipport6_kadt(struct ip_set *set, const struct sk_buff *skb,
306 const struct xt_action_param *par, 256 const struct xt_action_param *par,
307 enum ipset_adt adt, struct ip_set_adt_opt *opt) 257 enum ipset_adt adt, struct ip_set_adt_opt *opt)
308{ 258{
309 const struct hash_ipport *h = set->data;
310 ipset_adtfn adtfn = set->variant->adt[adt]; 259 ipset_adtfn adtfn = set->variant->adt[adt];
311 struct hash_ipport6_elem e = { }; 260 struct hash_ipport6_elem e = { };
312 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 261 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
313 262
314 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 263 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
315 &e.port, &e.proto)) 264 &e.port, &e.proto))
@@ -326,7 +275,7 @@ hash_ipport6_uadt(struct ip_set *set, struct nlattr *tb[],
326 const struct hash_ipport *h = set->data; 275 const struct hash_ipport *h = set->data;
327 ipset_adtfn adtfn = set->variant->adt[adt]; 276 ipset_adtfn adtfn = set->variant->adt[adt];
328 struct hash_ipport6_elem e = { }; 277 struct hash_ipport6_elem e = { };
329 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 278 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
330 u32 port, port_to; 279 u32 port, port_to;
331 bool with_ports = false; 280 bool with_ports = false;
332 int ret; 281 int ret;
@@ -396,8 +345,8 @@ static struct ip_set_type hash_ipport_type __read_mostly = {
396 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT, 345 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT,
397 .dimension = IPSET_DIM_TWO, 346 .dimension = IPSET_DIM_TWO,
398 .family = NFPROTO_UNSPEC, 347 .family = NFPROTO_UNSPEC,
399 .revision_min = REVISION_MIN, 348 .revision_min = IPSET_TYPE_REV_MIN,
400 .revision_max = REVISION_MAX, 349 .revision_max = IPSET_TYPE_REV_MAX,
401 .create = hash_ipport_create, 350 .create = hash_ipport_create,
402 .create_policy = { 351 .create_policy = {
403 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 352 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -419,6 +368,7 @@ static struct ip_set_type hash_ipport_type __read_mostly = {
419 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 368 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
420 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 369 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
421 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 370 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
371 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
422 }, 372 },
423 .me = THIS_MODULE, 373 .me = THIS_MODULE,
424}; 374};
diff --git a/net/netfilter/ipset/ip_set_hash_ipportip.c b/net/netfilter/ipset/ip_set_hash_ipportip.c
index 34e8a1acce42..f5636631466e 100644
--- a/net/netfilter/ipset/ip_set_hash_ipportip.c
+++ b/net/netfilter/ipset/ip_set_hash_ipportip.c
@@ -24,19 +24,20 @@
24#include <linux/netfilter/ipset/ip_set_getport.h> 24#include <linux/netfilter/ipset/ip_set_getport.h>
25#include <linux/netfilter/ipset/ip_set_hash.h> 25#include <linux/netfilter/ipset/ip_set_hash.h>
26 26
27#define REVISION_MIN 0 27#define IPSET_TYPE_REV_MIN 0
28/* 1 SCTP and UDPLITE support added */ 28/* 1 SCTP and UDPLITE support added */
29#define REVISION_MAX 2 /* Counters support added */ 29/* 2 Counters support added */
30#define IPSET_TYPE_REV_MAX 3 /* Comments support added */
30 31
31MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33IP_SET_MODULE_DESC("hash:ip,port,ip", REVISION_MIN, REVISION_MAX); 34IP_SET_MODULE_DESC("hash:ip,port,ip", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_hash:ip,port,ip"); 35MODULE_ALIAS("ip_set_hash:ip,port,ip");
35 36
36/* Type specific function prefix */ 37/* Type specific function prefix */
37#define HTYPE hash_ipportip 38#define HTYPE hash_ipportip
38 39
39/* IPv4 variants */ 40/* IPv4 variant */
40 41
41/* Member elements */ 42/* Member elements */
42struct hash_ipportip4_elem { 43struct hash_ipportip4_elem {
@@ -47,34 +48,6 @@ struct hash_ipportip4_elem {
47 u8 padding; 48 u8 padding;
48}; 49};
49 50
50struct hash_ipportip4t_elem {
51 __be32 ip;
52 __be32 ip2;
53 __be16 port;
54 u8 proto;
55 u8 padding;
56 unsigned long timeout;
57};
58
59struct hash_ipportip4c_elem {
60 __be32 ip;
61 __be32 ip2;
62 __be16 port;
63 u8 proto;
64 u8 padding;
65 struct ip_set_counter counter;
66};
67
68struct hash_ipportip4ct_elem {
69 __be32 ip;
70 __be32 ip2;
71 __be16 port;
72 u8 proto;
73 u8 padding;
74 struct ip_set_counter counter;
75 unsigned long timeout;
76};
77
78static inline bool 51static inline bool
79hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1, 52hash_ipportip4_data_equal(const struct hash_ipportip4_elem *ip1,
80 const struct hash_ipportip4_elem *ip2, 53 const struct hash_ipportip4_elem *ip2,
@@ -120,10 +93,9 @@ hash_ipportip4_kadt(struct ip_set *set, const struct sk_buff *skb,
120 const struct xt_action_param *par, 93 const struct xt_action_param *par,
121 enum ipset_adt adt, struct ip_set_adt_opt *opt) 94 enum ipset_adt adt, struct ip_set_adt_opt *opt)
122{ 95{
123 const struct hash_ipportip *h = set->data;
124 ipset_adtfn adtfn = set->variant->adt[adt]; 96 ipset_adtfn adtfn = set->variant->adt[adt];
125 struct hash_ipportip4_elem e = { }; 97 struct hash_ipportip4_elem e = { };
126 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 98 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
127 99
128 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 100 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
129 &e.port, &e.proto)) 101 &e.port, &e.proto))
@@ -141,8 +113,8 @@ hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
141 const struct hash_ipportip *h = set->data; 113 const struct hash_ipportip *h = set->data;
142 ipset_adtfn adtfn = set->variant->adt[adt]; 114 ipset_adtfn adtfn = set->variant->adt[adt];
143 struct hash_ipportip4_elem e = { }; 115 struct hash_ipportip4_elem e = { };
144 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 116 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
145 u32 ip, ip_to, p = 0, port, port_to; 117 u32 ip, ip_to = 0, p = 0, port, port_to;
146 bool with_ports = false; 118 bool with_ports = false;
147 int ret; 119 int ret;
148 120
@@ -231,7 +203,7 @@ hash_ipportip4_uadt(struct ip_set *set, struct nlattr *tb[],
231 return ret; 203 return ret;
232} 204}
233 205
234/* IPv6 variants */ 206/* IPv6 variant */
235 207
236struct hash_ipportip6_elem { 208struct hash_ipportip6_elem {
237 union nf_inet_addr ip; 209 union nf_inet_addr ip;
@@ -241,34 +213,6 @@ struct hash_ipportip6_elem {
241 u8 padding; 213 u8 padding;
242}; 214};
243 215
244struct hash_ipportip6t_elem {
245 union nf_inet_addr ip;
246 union nf_inet_addr ip2;
247 __be16 port;
248 u8 proto;
249 u8 padding;
250 unsigned long timeout;
251};
252
253struct hash_ipportip6c_elem {
254 union nf_inet_addr ip;
255 union nf_inet_addr ip2;
256 __be16 port;
257 u8 proto;
258 u8 padding;
259 struct ip_set_counter counter;
260};
261
262struct hash_ipportip6ct_elem {
263 union nf_inet_addr ip;
264 union nf_inet_addr ip2;
265 __be16 port;
266 u8 proto;
267 u8 padding;
268 struct ip_set_counter counter;
269 unsigned long timeout;
270};
271
272/* Common functions */ 216/* Common functions */
273 217
274static inline bool 218static inline bool
@@ -319,10 +263,9 @@ hash_ipportip6_kadt(struct ip_set *set, const struct sk_buff *skb,
319 const struct xt_action_param *par, 263 const struct xt_action_param *par,
320 enum ipset_adt adt, struct ip_set_adt_opt *opt) 264 enum ipset_adt adt, struct ip_set_adt_opt *opt)
321{ 265{
322 const struct hash_ipportip *h = set->data;
323 ipset_adtfn adtfn = set->variant->adt[adt]; 266 ipset_adtfn adtfn = set->variant->adt[adt];
324 struct hash_ipportip6_elem e = { }; 267 struct hash_ipportip6_elem e = { };
325 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 268 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
326 269
327 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC, 270 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
328 &e.port, &e.proto)) 271 &e.port, &e.proto))
@@ -340,7 +283,7 @@ hash_ipportip6_uadt(struct ip_set *set, struct nlattr *tb[],
340 const struct hash_ipportip *h = set->data; 283 const struct hash_ipportip *h = set->data;
341 ipset_adtfn adtfn = set->variant->adt[adt]; 284 ipset_adtfn adtfn = set->variant->adt[adt];
342 struct hash_ipportip6_elem e = { }; 285 struct hash_ipportip6_elem e = { };
343 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 286 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
344 u32 port, port_to; 287 u32 port, port_to;
345 bool with_ports = false; 288 bool with_ports = false;
346 int ret; 289 int ret;
@@ -414,8 +357,8 @@ static struct ip_set_type hash_ipportip_type __read_mostly = {
414 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2, 357 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2,
415 .dimension = IPSET_DIM_THREE, 358 .dimension = IPSET_DIM_THREE,
416 .family = NFPROTO_UNSPEC, 359 .family = NFPROTO_UNSPEC,
417 .revision_min = REVISION_MIN, 360 .revision_min = IPSET_TYPE_REV_MIN,
418 .revision_max = REVISION_MAX, 361 .revision_max = IPSET_TYPE_REV_MAX,
419 .create = hash_ipportip_create, 362 .create = hash_ipportip_create,
420 .create_policy = { 363 .create_policy = {
421 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 364 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -437,6 +380,7 @@ static struct ip_set_type hash_ipportip_type __read_mostly = {
437 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 380 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
438 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 381 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
439 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 382 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
383 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
440 }, 384 },
441 .me = THIS_MODULE, 385 .me = THIS_MODULE,
442}; 386};
diff --git a/net/netfilter/ipset/ip_set_hash_ipportnet.c b/net/netfilter/ipset/ip_set_hash_ipportnet.c
index f15f3e28b9c3..5d87fe8a41ff 100644
--- a/net/netfilter/ipset/ip_set_hash_ipportnet.c
+++ b/net/netfilter/ipset/ip_set_hash_ipportnet.c
@@ -24,15 +24,16 @@
24#include <linux/netfilter/ipset/ip_set_getport.h> 24#include <linux/netfilter/ipset/ip_set_getport.h>
25#include <linux/netfilter/ipset/ip_set_hash.h> 25#include <linux/netfilter/ipset/ip_set_hash.h>
26 26
27#define REVISION_MIN 0 27#define IPSET_TYPE_REV_MIN 0
28/* 1 SCTP and UDPLITE support added */ 28/* 1 SCTP and UDPLITE support added */
29/* 2 Range as input support for IPv4 added */ 29/* 2 Range as input support for IPv4 added */
30/* 3 nomatch flag support added */ 30/* 3 nomatch flag support added */
31#define REVISION_MAX 4 /* Counters support added */ 31/* 4 Counters support added */
32#define IPSET_TYPE_REV_MAX 5 /* Comments support added */
32 33
33MODULE_LICENSE("GPL"); 34MODULE_LICENSE("GPL");
34MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 35MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
35IP_SET_MODULE_DESC("hash:ip,port,net", REVISION_MIN, REVISION_MAX); 36IP_SET_MODULE_DESC("hash:ip,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
36MODULE_ALIAS("ip_set_hash:ip,port,net"); 37MODULE_ALIAS("ip_set_hash:ip,port,net");
37 38
38/* Type specific function prefix */ 39/* Type specific function prefix */
@@ -46,7 +47,7 @@ MODULE_ALIAS("ip_set_hash:ip,port,net");
46#define IP_SET_HASH_WITH_PROTO 47#define IP_SET_HASH_WITH_PROTO
47#define IP_SET_HASH_WITH_NETS 48#define IP_SET_HASH_WITH_NETS
48 49
49/* IPv4 variants */ 50/* IPv4 variant */
50 51
51/* Member elements */ 52/* Member elements */
52struct hash_ipportnet4_elem { 53struct hash_ipportnet4_elem {
@@ -58,37 +59,6 @@ struct hash_ipportnet4_elem {
58 u8 proto; 59 u8 proto;
59}; 60};
60 61
61struct hash_ipportnet4t_elem {
62 __be32 ip;
63 __be32 ip2;
64 __be16 port;
65 u8 cidr:7;
66 u8 nomatch:1;
67 u8 proto;
68 unsigned long timeout;
69};
70
71struct hash_ipportnet4c_elem {
72 __be32 ip;
73 __be32 ip2;
74 __be16 port;
75 u8 cidr:7;
76 u8 nomatch:1;
77 u8 proto;
78 struct ip_set_counter counter;
79};
80
81struct hash_ipportnet4ct_elem {
82 __be32 ip;
83 __be32 ip2;
84 __be16 port;
85 u8 cidr:7;
86 u8 nomatch:1;
87 u8 proto;
88 struct ip_set_counter counter;
89 unsigned long timeout;
90};
91
92/* Common functions */ 62/* Common functions */
93 63
94static inline bool 64static inline bool
@@ -170,9 +140,9 @@ hash_ipportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
170 const struct hash_ipportnet *h = set->data; 140 const struct hash_ipportnet *h = set->data;
171 ipset_adtfn adtfn = set->variant->adt[adt]; 141 ipset_adtfn adtfn = set->variant->adt[adt];
172 struct hash_ipportnet4_elem e = { 142 struct hash_ipportnet4_elem e = {
173 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1 143 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
174 }; 144 };
175 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 145 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
176 146
177 if (adt == IPSET_TEST) 147 if (adt == IPSET_TEST)
178 e.cidr = HOST_MASK - 1; 148 e.cidr = HOST_MASK - 1;
@@ -195,9 +165,9 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
195 const struct hash_ipportnet *h = set->data; 165 const struct hash_ipportnet *h = set->data;
196 ipset_adtfn adtfn = set->variant->adt[adt]; 166 ipset_adtfn adtfn = set->variant->adt[adt];
197 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 }; 167 struct hash_ipportnet4_elem e = { .cidr = HOST_MASK - 1 };
198 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 168 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
199 u32 ip, ip_to, p = 0, port, port_to; 169 u32 ip = 0, ip_to = 0, p = 0, port, port_to;
200 u32 ip2_from, ip2_to, ip2_last, ip2; 170 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
201 bool with_ports = false; 171 bool with_ports = false;
202 u8 cidr; 172 u8 cidr;
203 int ret; 173 int ret;
@@ -272,7 +242,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
272 if (ip > ip_to) 242 if (ip > ip_to)
273 swap(ip, ip_to); 243 swap(ip, ip_to);
274 } else if (tb[IPSET_ATTR_CIDR]) { 244 } else if (tb[IPSET_ATTR_CIDR]) {
275 u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]); 245 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
276 246
277 if (!cidr || cidr > 32) 247 if (!cidr || cidr > 32)
278 return -IPSET_ERR_INVALID_CIDR; 248 return -IPSET_ERR_INVALID_CIDR;
@@ -306,9 +276,9 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
306 : port; 276 : port;
307 for (; p <= port_to; p++) { 277 for (; p <= port_to; p++) {
308 e.port = htons(p); 278 e.port = htons(p);
309 ip2 = retried 279 ip2 = retried &&
310 && ip == ntohl(h->next.ip) 280 ip == ntohl(h->next.ip) &&
311 && p == ntohs(h->next.port) 281 p == ntohs(h->next.port)
312 ? ntohl(h->next.ip2) : ip2_from; 282 ? ntohl(h->next.ip2) : ip2_from;
313 while (!after(ip2, ip2_to)) { 283 while (!after(ip2, ip2_to)) {
314 e.ip2 = htonl(ip2); 284 e.ip2 = htonl(ip2);
@@ -328,7 +298,7 @@ hash_ipportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
328 return ret; 298 return ret;
329} 299}
330 300
331/* IPv6 variants */ 301/* IPv6 variant */
332 302
333struct hash_ipportnet6_elem { 303struct hash_ipportnet6_elem {
334 union nf_inet_addr ip; 304 union nf_inet_addr ip;
@@ -339,37 +309,6 @@ struct hash_ipportnet6_elem {
339 u8 proto; 309 u8 proto;
340}; 310};
341 311
342struct hash_ipportnet6t_elem {
343 union nf_inet_addr ip;
344 union nf_inet_addr ip2;
345 __be16 port;
346 u8 cidr:7;
347 u8 nomatch:1;
348 u8 proto;
349 unsigned long timeout;
350};
351
352struct hash_ipportnet6c_elem {
353 union nf_inet_addr ip;
354 union nf_inet_addr ip2;
355 __be16 port;
356 u8 cidr:7;
357 u8 nomatch:1;
358 u8 proto;
359 struct ip_set_counter counter;
360};
361
362struct hash_ipportnet6ct_elem {
363 union nf_inet_addr ip;
364 union nf_inet_addr ip2;
365 __be16 port;
366 u8 cidr:7;
367 u8 nomatch:1;
368 u8 proto;
369 struct ip_set_counter counter;
370 unsigned long timeout;
371};
372
373/* Common functions */ 312/* Common functions */
374 313
375static inline bool 314static inline bool
@@ -454,9 +393,9 @@ hash_ipportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
454 const struct hash_ipportnet *h = set->data; 393 const struct hash_ipportnet *h = set->data;
455 ipset_adtfn adtfn = set->variant->adt[adt]; 394 ipset_adtfn adtfn = set->variant->adt[adt];
456 struct hash_ipportnet6_elem e = { 395 struct hash_ipportnet6_elem e = {
457 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1 396 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
458 }; 397 };
459 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 398 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
460 399
461 if (adt == IPSET_TEST) 400 if (adt == IPSET_TEST)
462 e.cidr = HOST_MASK - 1; 401 e.cidr = HOST_MASK - 1;
@@ -479,7 +418,7 @@ hash_ipportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
479 const struct hash_ipportnet *h = set->data; 418 const struct hash_ipportnet *h = set->data;
480 ipset_adtfn adtfn = set->variant->adt[adt]; 419 ipset_adtfn adtfn = set->variant->adt[adt];
481 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 }; 420 struct hash_ipportnet6_elem e = { .cidr = HOST_MASK - 1 };
482 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 421 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
483 u32 port, port_to; 422 u32 port, port_to;
484 bool with_ports = false; 423 bool with_ports = false;
485 u8 cidr; 424 u8 cidr;
@@ -574,8 +513,8 @@ static struct ip_set_type hash_ipportnet_type __read_mostly = {
574 IPSET_TYPE_NOMATCH, 513 IPSET_TYPE_NOMATCH,
575 .dimension = IPSET_DIM_THREE, 514 .dimension = IPSET_DIM_THREE,
576 .family = NFPROTO_UNSPEC, 515 .family = NFPROTO_UNSPEC,
577 .revision_min = REVISION_MIN, 516 .revision_min = IPSET_TYPE_REV_MIN,
578 .revision_max = REVISION_MAX, 517 .revision_max = IPSET_TYPE_REV_MAX,
579 .create = hash_ipportnet_create, 518 .create = hash_ipportnet_create,
580 .create_policy = { 519 .create_policy = {
581 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 520 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -600,6 +539,7 @@ static struct ip_set_type hash_ipportnet_type __read_mostly = {
600 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 539 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
601 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 540 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
602 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 541 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
542 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
603 }, 543 },
604 .me = THIS_MODULE, 544 .me = THIS_MODULE,
605}; 545};
diff --git a/net/netfilter/ipset/ip_set_hash_net.c b/net/netfilter/ipset/ip_set_hash_net.c
index 223e9f546d0f..8295cf4f9fdc 100644
--- a/net/netfilter/ipset/ip_set_hash_net.c
+++ b/net/netfilter/ipset/ip_set_hash_net.c
@@ -22,21 +22,22 @@
22#include <linux/netfilter/ipset/ip_set.h> 22#include <linux/netfilter/ipset/ip_set.h>
23#include <linux/netfilter/ipset/ip_set_hash.h> 23#include <linux/netfilter/ipset/ip_set_hash.h>
24 24
25#define REVISION_MIN 0 25#define IPSET_TYPE_REV_MIN 0
26/* 1 Range as input support for IPv4 added */ 26/* 1 Range as input support for IPv4 added */
27/* 2 nomatch flag support added */ 27/* 2 nomatch flag support added */
28#define REVISION_MAX 3 /* Counters support added */ 28/* 3 Counters support added */
29#define IPSET_TYPE_REV_MAX 4 /* Comments support added */
29 30
30MODULE_LICENSE("GPL"); 31MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
32IP_SET_MODULE_DESC("hash:net", REVISION_MIN, REVISION_MAX); 33IP_SET_MODULE_DESC("hash:net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33MODULE_ALIAS("ip_set_hash:net"); 34MODULE_ALIAS("ip_set_hash:net");
34 35
35/* Type specific function prefix */ 36/* Type specific function prefix */
36#define HTYPE hash_net 37#define HTYPE hash_net
37#define IP_SET_HASH_WITH_NETS 38#define IP_SET_HASH_WITH_NETS
38 39
39/* IPv4 variants */ 40/* IPv4 variant */
40 41
41/* Member elements */ 42/* Member elements */
42struct hash_net4_elem { 43struct hash_net4_elem {
@@ -46,31 +47,6 @@ struct hash_net4_elem {
46 u8 cidr; 47 u8 cidr;
47}; 48};
48 49
49struct hash_net4t_elem {
50 __be32 ip;
51 u16 padding0;
52 u8 nomatch;
53 u8 cidr;
54 unsigned long timeout;
55};
56
57struct hash_net4c_elem {
58 __be32 ip;
59 u16 padding0;
60 u8 nomatch;
61 u8 cidr;
62 struct ip_set_counter counter;
63};
64
65struct hash_net4ct_elem {
66 __be32 ip;
67 u16 padding0;
68 u8 nomatch;
69 u8 cidr;
70 struct ip_set_counter counter;
71 unsigned long timeout;
72};
73
74/* Common functions */ 50/* Common functions */
75 51
76static inline bool 52static inline bool
@@ -143,9 +119,9 @@ hash_net4_kadt(struct ip_set *set, const struct sk_buff *skb,
143 const struct hash_net *h = set->data; 119 const struct hash_net *h = set->data;
144 ipset_adtfn adtfn = set->variant->adt[adt]; 120 ipset_adtfn adtfn = set->variant->adt[adt];
145 struct hash_net4_elem e = { 121 struct hash_net4_elem e = {
146 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK 122 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
147 }; 123 };
148 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 124 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
149 125
150 if (e.cidr == 0) 126 if (e.cidr == 0)
151 return -EINVAL; 127 return -EINVAL;
@@ -165,8 +141,8 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
165 const struct hash_net *h = set->data; 141 const struct hash_net *h = set->data;
166 ipset_adtfn adtfn = set->variant->adt[adt]; 142 ipset_adtfn adtfn = set->variant->adt[adt];
167 struct hash_net4_elem e = { .cidr = HOST_MASK }; 143 struct hash_net4_elem e = { .cidr = HOST_MASK };
168 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 144 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
169 u32 ip = 0, ip_to, last; 145 u32 ip = 0, ip_to = 0, last;
170 int ret; 146 int ret;
171 147
172 if (unlikely(!tb[IPSET_ATTR_IP] || 148 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -228,7 +204,7 @@ hash_net4_uadt(struct ip_set *set, struct nlattr *tb[],
228 return ret; 204 return ret;
229} 205}
230 206
231/* IPv6 variants */ 207/* IPv6 variant */
232 208
233struct hash_net6_elem { 209struct hash_net6_elem {
234 union nf_inet_addr ip; 210 union nf_inet_addr ip;
@@ -237,31 +213,6 @@ struct hash_net6_elem {
237 u8 cidr; 213 u8 cidr;
238}; 214};
239 215
240struct hash_net6t_elem {
241 union nf_inet_addr ip;
242 u16 padding0;
243 u8 nomatch;
244 u8 cidr;
245 unsigned long timeout;
246};
247
248struct hash_net6c_elem {
249 union nf_inet_addr ip;
250 u16 padding0;
251 u8 nomatch;
252 u8 cidr;
253 struct ip_set_counter counter;
254};
255
256struct hash_net6ct_elem {
257 union nf_inet_addr ip;
258 u16 padding0;
259 u8 nomatch;
260 u8 cidr;
261 struct ip_set_counter counter;
262 unsigned long timeout;
263};
264
265/* Common functions */ 216/* Common functions */
266 217
267static inline bool 218static inline bool
@@ -338,9 +289,9 @@ hash_net6_kadt(struct ip_set *set, const struct sk_buff *skb,
338 const struct hash_net *h = set->data; 289 const struct hash_net *h = set->data;
339 ipset_adtfn adtfn = set->variant->adt[adt]; 290 ipset_adtfn adtfn = set->variant->adt[adt];
340 struct hash_net6_elem e = { 291 struct hash_net6_elem e = {
341 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK 292 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
342 }; 293 };
343 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 294 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
344 295
345 if (e.cidr == 0) 296 if (e.cidr == 0)
346 return -EINVAL; 297 return -EINVAL;
@@ -357,10 +308,9 @@ static int
357hash_net6_uadt(struct ip_set *set, struct nlattr *tb[], 308hash_net6_uadt(struct ip_set *set, struct nlattr *tb[],
358 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) 309 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
359{ 310{
360 const struct hash_net *h = set->data;
361 ipset_adtfn adtfn = set->variant->adt[adt]; 311 ipset_adtfn adtfn = set->variant->adt[adt];
362 struct hash_net6_elem e = { .cidr = HOST_MASK }; 312 struct hash_net6_elem e = { .cidr = HOST_MASK };
363 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 313 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
364 int ret; 314 int ret;
365 315
366 if (unlikely(!tb[IPSET_ATTR_IP] || 316 if (unlikely(!tb[IPSET_ATTR_IP] ||
@@ -406,8 +356,8 @@ static struct ip_set_type hash_net_type __read_mostly = {
406 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH, 356 .features = IPSET_TYPE_IP | IPSET_TYPE_NOMATCH,
407 .dimension = IPSET_DIM_ONE, 357 .dimension = IPSET_DIM_ONE,
408 .family = NFPROTO_UNSPEC, 358 .family = NFPROTO_UNSPEC,
409 .revision_min = REVISION_MIN, 359 .revision_min = IPSET_TYPE_REV_MIN,
410 .revision_max = REVISION_MAX, 360 .revision_max = IPSET_TYPE_REV_MAX,
411 .create = hash_net_create, 361 .create = hash_net_create,
412 .create_policy = { 362 .create_policy = {
413 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 363 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -425,6 +375,7 @@ static struct ip_set_type hash_net_type __read_mostly = {
425 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 375 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
426 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 376 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
427 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 377 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
378 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
428 }, 379 },
429 .me = THIS_MODULE, 380 .me = THIS_MODULE,
430}; 381};
diff --git a/net/netfilter/ipset/ip_set_hash_netiface.c b/net/netfilter/ipset/ip_set_hash_netiface.c
index 7d798d5d5cd3..3f64a66bf5d9 100644
--- a/net/netfilter/ipset/ip_set_hash_netiface.c
+++ b/net/netfilter/ipset/ip_set_hash_netiface.c
@@ -23,14 +23,15 @@
23#include <linux/netfilter/ipset/ip_set.h> 23#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_hash.h> 24#include <linux/netfilter/ipset/ip_set_hash.h>
25 25
26#define REVISION_MIN 0 26#define IPSET_TYPE_REV_MIN 0
27/* 1 nomatch flag support added */ 27/* 1 nomatch flag support added */
28/* 2 /0 support added */ 28/* 2 /0 support added */
29#define REVISION_MAX 3 /* Counters support added */ 29/* 3 Counters support added */
30#define IPSET_TYPE_REV_MAX 4 /* Comments support added */
30 31
31MODULE_LICENSE("GPL"); 32MODULE_LICENSE("GPL");
32MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
33IP_SET_MODULE_DESC("hash:net,iface", REVISION_MIN, REVISION_MAX); 34IP_SET_MODULE_DESC("hash:net,iface", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
34MODULE_ALIAS("ip_set_hash:net,iface"); 35MODULE_ALIAS("ip_set_hash:net,iface");
35 36
36/* Interface name rbtree */ 37/* Interface name rbtree */
@@ -134,7 +135,7 @@ iface_add(struct rb_root *root, const char **iface)
134 135
135#define STREQ(a, b) (strcmp(a, b) == 0) 136#define STREQ(a, b) (strcmp(a, b) == 0)
136 137
137/* IPv4 variants */ 138/* IPv4 variant */
138 139
139struct hash_netiface4_elem_hashed { 140struct hash_netiface4_elem_hashed {
140 __be32 ip; 141 __be32 ip;
@@ -144,7 +145,7 @@ struct hash_netiface4_elem_hashed {
144 u8 elem; 145 u8 elem;
145}; 146};
146 147
147/* Member elements without timeout */ 148/* Member elements */
148struct hash_netiface4_elem { 149struct hash_netiface4_elem {
149 __be32 ip; 150 __be32 ip;
150 u8 physdev; 151 u8 physdev;
@@ -154,37 +155,6 @@ struct hash_netiface4_elem {
154 const char *iface; 155 const char *iface;
155}; 156};
156 157
157struct hash_netiface4t_elem {
158 __be32 ip;
159 u8 physdev;
160 u8 cidr;
161 u8 nomatch;
162 u8 elem;
163 const char *iface;
164 unsigned long timeout;
165};
166
167struct hash_netiface4c_elem {
168 __be32 ip;
169 u8 physdev;
170 u8 cidr;
171 u8 nomatch;
172 u8 elem;
173 const char *iface;
174 struct ip_set_counter counter;
175};
176
177struct hash_netiface4ct_elem {
178 __be32 ip;
179 u8 physdev;
180 u8 cidr;
181 u8 nomatch;
182 u8 elem;
183 const char *iface;
184 struct ip_set_counter counter;
185 unsigned long timeout;
186};
187
188/* Common functions */ 158/* Common functions */
189 159
190static inline bool 160static inline bool
@@ -265,10 +235,10 @@ hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
265 struct hash_netiface *h = set->data; 235 struct hash_netiface *h = set->data;
266 ipset_adtfn adtfn = set->variant->adt[adt]; 236 ipset_adtfn adtfn = set->variant->adt[adt];
267 struct hash_netiface4_elem e = { 237 struct hash_netiface4_elem e = {
268 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK, 238 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
269 .elem = 1, 239 .elem = 1,
270 }; 240 };
271 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 241 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
272 int ret; 242 int ret;
273 243
274 if (e.cidr == 0) 244 if (e.cidr == 0)
@@ -319,8 +289,8 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
319 struct hash_netiface *h = set->data; 289 struct hash_netiface *h = set->data;
320 ipset_adtfn adtfn = set->variant->adt[adt]; 290 ipset_adtfn adtfn = set->variant->adt[adt];
321 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 }; 291 struct hash_netiface4_elem e = { .cidr = HOST_MASK, .elem = 1 };
322 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 292 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
323 u32 ip = 0, ip_to, last; 293 u32 ip = 0, ip_to = 0, last;
324 char iface[IFNAMSIZ]; 294 char iface[IFNAMSIZ];
325 int ret; 295 int ret;
326 296
@@ -399,7 +369,7 @@ hash_netiface4_uadt(struct ip_set *set, struct nlattr *tb[],
399 return ret; 369 return ret;
400} 370}
401 371
402/* IPv6 variants */ 372/* IPv6 variant */
403 373
404struct hash_netiface6_elem_hashed { 374struct hash_netiface6_elem_hashed {
405 union nf_inet_addr ip; 375 union nf_inet_addr ip;
@@ -418,37 +388,6 @@ struct hash_netiface6_elem {
418 const char *iface; 388 const char *iface;
419}; 389};
420 390
421struct hash_netiface6t_elem {
422 union nf_inet_addr ip;
423 u8 physdev;
424 u8 cidr;
425 u8 nomatch;
426 u8 elem;
427 const char *iface;
428 unsigned long timeout;
429};
430
431struct hash_netiface6c_elem {
432 union nf_inet_addr ip;
433 u8 physdev;
434 u8 cidr;
435 u8 nomatch;
436 u8 elem;
437 const char *iface;
438 struct ip_set_counter counter;
439};
440
441struct hash_netiface6ct_elem {
442 union nf_inet_addr ip;
443 u8 physdev;
444 u8 cidr;
445 u8 nomatch;
446 u8 elem;
447 const char *iface;
448 struct ip_set_counter counter;
449 unsigned long timeout;
450};
451
452/* Common functions */ 391/* Common functions */
453 392
454static inline bool 393static inline bool
@@ -534,10 +473,10 @@ hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
534 struct hash_netiface *h = set->data; 473 struct hash_netiface *h = set->data;
535 ipset_adtfn adtfn = set->variant->adt[adt]; 474 ipset_adtfn adtfn = set->variant->adt[adt];
536 struct hash_netiface6_elem e = { 475 struct hash_netiface6_elem e = {
537 .cidr = h->nets[0].cidr ? h->nets[0].cidr : HOST_MASK, 476 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
538 .elem = 1, 477 .elem = 1,
539 }; 478 };
540 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 479 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
541 int ret; 480 int ret;
542 481
543 if (e.cidr == 0) 482 if (e.cidr == 0)
@@ -584,7 +523,7 @@ hash_netiface6_uadt(struct ip_set *set, struct nlattr *tb[],
584 struct hash_netiface *h = set->data; 523 struct hash_netiface *h = set->data;
585 ipset_adtfn adtfn = set->variant->adt[adt]; 524 ipset_adtfn adtfn = set->variant->adt[adt];
586 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 }; 525 struct hash_netiface6_elem e = { .cidr = HOST_MASK, .elem = 1 };
587 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 526 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
588 char iface[IFNAMSIZ]; 527 char iface[IFNAMSIZ];
589 int ret; 528 int ret;
590 529
@@ -645,8 +584,8 @@ static struct ip_set_type hash_netiface_type __read_mostly = {
645 IPSET_TYPE_NOMATCH, 584 IPSET_TYPE_NOMATCH,
646 .dimension = IPSET_DIM_TWO, 585 .dimension = IPSET_DIM_TWO,
647 .family = NFPROTO_UNSPEC, 586 .family = NFPROTO_UNSPEC,
648 .revision_min = REVISION_MIN, 587 .revision_min = IPSET_TYPE_REV_MIN,
649 .revision_max = REVISION_MAX, 588 .revision_max = IPSET_TYPE_REV_MAX,
650 .create = hash_netiface_create, 589 .create = hash_netiface_create,
651 .create_policy = { 590 .create_policy = {
652 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 591 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -668,6 +607,7 @@ static struct ip_set_type hash_netiface_type __read_mostly = {
668 [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, 607 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
669 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 608 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
670 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 609 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
610 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
671 }, 611 },
672 .me = THIS_MODULE, 612 .me = THIS_MODULE,
673}; 613};
diff --git a/net/netfilter/ipset/ip_set_hash_netnet.c b/net/netfilter/ipset/ip_set_hash_netnet.c
new file mode 100644
index 000000000000..6226803fc490
--- /dev/null
+++ b/net/netfilter/ipset/ip_set_hash_netnet.c
@@ -0,0 +1,481 @@
1/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 * Copyright (C) 2013 Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9/* Kernel module implementing an IP set type: the hash:net type */
10
11#include <linux/jhash.h>
12#include <linux/module.h>
13#include <linux/ip.h>
14#include <linux/skbuff.h>
15#include <linux/errno.h>
16#include <linux/random.h>
17#include <net/ip.h>
18#include <net/ipv6.h>
19#include <net/netlink.h>
20
21#include <linux/netfilter.h>
22#include <linux/netfilter/ipset/pfxlen.h>
23#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_hash.h>
25
26#define IPSET_TYPE_REV_MIN 0
27#define IPSET_TYPE_REV_MAX 0
28
29MODULE_LICENSE("GPL");
30MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
31IP_SET_MODULE_DESC("hash:net,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
32MODULE_ALIAS("ip_set_hash:net,net");
33
34/* Type specific function prefix */
35#define HTYPE hash_netnet
36#define IP_SET_HASH_WITH_NETS
37#define IPSET_NET_COUNT 2
38
39/* IPv4 variants */
40
41/* Member elements */
42struct hash_netnet4_elem {
43 union {
44 __be32 ip[2];
45 __be64 ipcmp;
46 };
47 u8 nomatch;
48 union {
49 u8 cidr[2];
50 u16 ccmp;
51 };
52};
53
54/* Common functions */
55
56static inline bool
57hash_netnet4_data_equal(const struct hash_netnet4_elem *ip1,
58 const struct hash_netnet4_elem *ip2,
59 u32 *multi)
60{
61 return ip1->ipcmp == ip2->ipcmp &&
62 ip1->ccmp == ip2->ccmp;
63}
64
65static inline int
66hash_netnet4_do_data_match(const struct hash_netnet4_elem *elem)
67{
68 return elem->nomatch ? -ENOTEMPTY : 1;
69}
70
71static inline void
72hash_netnet4_data_set_flags(struct hash_netnet4_elem *elem, u32 flags)
73{
74 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
75}
76
77static inline void
78hash_netnet4_data_reset_flags(struct hash_netnet4_elem *elem, u8 *flags)
79{
80 swap(*flags, elem->nomatch);
81}
82
83static inline void
84hash_netnet4_data_reset_elem(struct hash_netnet4_elem *elem,
85 struct hash_netnet4_elem *orig)
86{
87 elem->ip[1] = orig->ip[1];
88}
89
90static inline void
91hash_netnet4_data_netmask(struct hash_netnet4_elem *elem, u8 cidr, bool inner)
92{
93 if (inner) {
94 elem->ip[1] &= ip_set_netmask(cidr);
95 elem->cidr[1] = cidr;
96 } else {
97 elem->ip[0] &= ip_set_netmask(cidr);
98 elem->cidr[0] = cidr;
99 }
100}
101
102static bool
103hash_netnet4_data_list(struct sk_buff *skb,
104 const struct hash_netnet4_elem *data)
105{
106 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
107
108 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
109 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
110 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
111 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
112 (flags &&
113 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
114 goto nla_put_failure;
115 return 0;
116
117nla_put_failure:
118 return 1;
119}
120
121static inline void
122hash_netnet4_data_next(struct hash_netnet4_elem *next,
123 const struct hash_netnet4_elem *d)
124{
125 next->ipcmp = d->ipcmp;
126}
127
128#define MTYPE hash_netnet4
129#define PF 4
130#define HOST_MASK 32
131#include "ip_set_hash_gen.h"
132
133static int
134hash_netnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
135 const struct xt_action_param *par,
136 enum ipset_adt adt, struct ip_set_adt_opt *opt)
137{
138 const struct hash_netnet *h = set->data;
139 ipset_adtfn adtfn = set->variant->adt[adt];
140 struct hash_netnet4_elem e = { };
141 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
142
143 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
144 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
145 if (adt == IPSET_TEST)
146 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
147
148 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
149 ip4addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1]);
150 e.ip[0] &= ip_set_netmask(e.cidr[0]);
151 e.ip[1] &= ip_set_netmask(e.cidr[1]);
152
153 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
154}
155
156static int
157hash_netnet4_uadt(struct ip_set *set, struct nlattr *tb[],
158 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
159{
160 const struct hash_netnet *h = set->data;
161 ipset_adtfn adtfn = set->variant->adt[adt];
162 struct hash_netnet4_elem e = { };
163 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
164 u32 ip = 0, ip_to = 0, last;
165 u32 ip2 = 0, ip2_from = 0, ip2_to = 0, last2;
166 u8 cidr, cidr2;
167 int ret;
168
169 e.cidr[0] = e.cidr[1] = HOST_MASK;
170 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
171 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
172 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
173 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
174 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
175 return -IPSET_ERR_PROTOCOL;
176
177 if (tb[IPSET_ATTR_LINENO])
178 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
179
180 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
181 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
182 ip_set_get_extensions(set, tb, &ext);
183 if (ret)
184 return ret;
185
186 if (tb[IPSET_ATTR_CIDR]) {
187 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
188 if (!cidr || cidr > HOST_MASK)
189 return -IPSET_ERR_INVALID_CIDR;
190 e.cidr[0] = cidr;
191 }
192
193 if (tb[IPSET_ATTR_CIDR2]) {
194 cidr2 = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
195 if (!cidr2 || cidr2 > HOST_MASK)
196 return -IPSET_ERR_INVALID_CIDR;
197 e.cidr[1] = cidr2;
198 }
199
200 if (tb[IPSET_ATTR_CADT_FLAGS]) {
201 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
202 if (cadt_flags & IPSET_FLAG_NOMATCH)
203 flags |= (IPSET_FLAG_NOMATCH << 16);
204 }
205
206 if (adt == IPSET_TEST || !(tb[IPSET_ATTR_IP_TO] &&
207 tb[IPSET_ATTR_IP2_TO])) {
208 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
209 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
210 ret = adtfn(set, &e, &ext, &ext, flags);
211 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
212 ip_set_eexist(ret, flags) ? 0 : ret;
213 }
214
215 ip_to = ip;
216 if (tb[IPSET_ATTR_IP_TO]) {
217 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
218 if (ret)
219 return ret;
220 if (ip_to < ip)
221 swap(ip, ip_to);
222 if (ip + UINT_MAX == ip_to)
223 return -IPSET_ERR_HASH_RANGE;
224 }
225
226 ip2_to = ip2_from;
227 if (tb[IPSET_ATTR_IP2_TO]) {
228 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
229 if (ret)
230 return ret;
231 if (ip2_to < ip2_from)
232 swap(ip2_from, ip2_to);
233 if (ip2_from + UINT_MAX == ip2_to)
234 return -IPSET_ERR_HASH_RANGE;
235
236 }
237
238 if (retried)
239 ip = ntohl(h->next.ip[0]);
240
241 while (!after(ip, ip_to)) {
242 e.ip[0] = htonl(ip);
243 last = ip_set_range_to_cidr(ip, ip_to, &cidr);
244 e.cidr[0] = cidr;
245 ip2 = (retried &&
246 ip == ntohl(h->next.ip[0])) ? ntohl(h->next.ip[1])
247 : ip2_from;
248 while (!after(ip2, ip2_to)) {
249 e.ip[1] = htonl(ip2);
250 last2 = ip_set_range_to_cidr(ip2, ip2_to, &cidr2);
251 e.cidr[1] = cidr2;
252 ret = adtfn(set, &e, &ext, &ext, flags);
253 if (ret && !ip_set_eexist(ret, flags))
254 return ret;
255 else
256 ret = 0;
257 ip2 = last2 + 1;
258 }
259 ip = last + 1;
260 }
261 return ret;
262}
263
264/* IPv6 variants */
265
266struct hash_netnet6_elem {
267 union nf_inet_addr ip[2];
268 u8 nomatch;
269 union {
270 u8 cidr[2];
271 u16 ccmp;
272 };
273};
274
275/* Common functions */
276
277static inline bool
278hash_netnet6_data_equal(const struct hash_netnet6_elem *ip1,
279 const struct hash_netnet6_elem *ip2,
280 u32 *multi)
281{
282 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
283 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
284 ip1->ccmp == ip2->ccmp;
285}
286
287static inline int
288hash_netnet6_do_data_match(const struct hash_netnet6_elem *elem)
289{
290 return elem->nomatch ? -ENOTEMPTY : 1;
291}
292
293static inline void
294hash_netnet6_data_set_flags(struct hash_netnet6_elem *elem, u32 flags)
295{
296 elem->nomatch = (flags >> 16) & IPSET_FLAG_NOMATCH;
297}
298
299static inline void
300hash_netnet6_data_reset_flags(struct hash_netnet6_elem *elem, u8 *flags)
301{
302 swap(*flags, elem->nomatch);
303}
304
305static inline void
306hash_netnet6_data_reset_elem(struct hash_netnet6_elem *elem,
307 struct hash_netnet6_elem *orig)
308{
309 elem->ip[1] = orig->ip[1];
310}
311
312static inline void
313hash_netnet6_data_netmask(struct hash_netnet6_elem *elem, u8 cidr, bool inner)
314{
315 if (inner) {
316 ip6_netmask(&elem->ip[1], cidr);
317 elem->cidr[1] = cidr;
318 } else {
319 ip6_netmask(&elem->ip[0], cidr);
320 elem->cidr[0] = cidr;
321 }
322}
323
324static bool
325hash_netnet6_data_list(struct sk_buff *skb,
326 const struct hash_netnet6_elem *data)
327{
328 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
329
330 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
331 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
332 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
333 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
334 (flags &&
335 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
336 goto nla_put_failure;
337 return 0;
338
339nla_put_failure:
340 return 1;
341}
342
343static inline void
344hash_netnet6_data_next(struct hash_netnet4_elem *next,
345 const struct hash_netnet6_elem *d)
346{
347}
348
349#undef MTYPE
350#undef PF
351#undef HOST_MASK
352
353#define MTYPE hash_netnet6
354#define PF 6
355#define HOST_MASK 128
356#define IP_SET_EMIT_CREATE
357#include "ip_set_hash_gen.h"
358
359static int
360hash_netnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
361 const struct xt_action_param *par,
362 enum ipset_adt adt, struct ip_set_adt_opt *opt)
363{
364 const struct hash_netnet *h = set->data;
365 ipset_adtfn adtfn = set->variant->adt[adt];
366 struct hash_netnet6_elem e = { };
367 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
368
369 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
370 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
371 if (adt == IPSET_TEST)
372 e.ccmp = (HOST_MASK << (sizeof(u8)*8)) | HOST_MASK;
373
374 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
375 ip6addrptr(skb, opt->flags & IPSET_DIM_TWO_SRC, &e.ip[1].in6);
376 ip6_netmask(&e.ip[0], e.cidr[0]);
377 ip6_netmask(&e.ip[1], e.cidr[1]);
378
379 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
380}
381
382static int
383hash_netnet6_uadt(struct ip_set *set, struct nlattr *tb[],
384 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
385{
386 ipset_adtfn adtfn = set->variant->adt[adt];
387 struct hash_netnet6_elem e = { };
388 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
389 int ret;
390
391 e.cidr[0] = e.cidr[1] = HOST_MASK;
392 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
393 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
394 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
395 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
396 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
397 return -IPSET_ERR_PROTOCOL;
398 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
399 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
400
401 if (tb[IPSET_ATTR_LINENO])
402 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
403
404 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
405 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
406 ip_set_get_extensions(set, tb, &ext);
407 if (ret)
408 return ret;
409
410 if (tb[IPSET_ATTR_CIDR])
411 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
412
413 if (tb[IPSET_ATTR_CIDR2])
414 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
415
416 if (!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
417 e.cidr[1] > HOST_MASK)
418 return -IPSET_ERR_INVALID_CIDR;
419
420 ip6_netmask(&e.ip[0], e.cidr[0]);
421 ip6_netmask(&e.ip[1], e.cidr[1]);
422
423 if (tb[IPSET_ATTR_CADT_FLAGS]) {
424 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
425 if (cadt_flags & IPSET_FLAG_NOMATCH)
426 flags |= (IPSET_FLAG_NOMATCH << 16);
427 }
428
429 ret = adtfn(set, &e, &ext, &ext, flags);
430
431 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
432 ip_set_eexist(ret, flags) ? 0 : ret;
433}
434
435static struct ip_set_type hash_netnet_type __read_mostly = {
436 .name = "hash:net,net",
437 .protocol = IPSET_PROTOCOL,
438 .features = IPSET_TYPE_IP | IPSET_TYPE_IP2 | IPSET_TYPE_NOMATCH,
439 .dimension = IPSET_DIM_TWO,
440 .family = NFPROTO_UNSPEC,
441 .revision_min = IPSET_TYPE_REV_MIN,
442 .revision_max = IPSET_TYPE_REV_MAX,
443 .create = hash_netnet_create,
444 .create_policy = {
445 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
446 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
447 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
448 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
449 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
450 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
451 },
452 .adt_policy = {
453 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
454 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
455 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
456 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
457 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
458 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
459 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
460 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
461 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
462 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
463 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
464 },
465 .me = THIS_MODULE,
466};
467
468static int __init
469hash_netnet_init(void)
470{
471 return ip_set_type_register(&hash_netnet_type);
472}
473
474static void __exit
475hash_netnet_fini(void)
476{
477 ip_set_type_unregister(&hash_netnet_type);
478}
479
480module_init(hash_netnet_init);
481module_exit(hash_netnet_fini);
diff --git a/net/netfilter/ipset/ip_set_hash_netport.c b/net/netfilter/ipset/ip_set_hash_netport.c
index 09d6690bee6f..7097fb0141bf 100644
--- a/net/netfilter/ipset/ip_set_hash_netport.c
+++ b/net/netfilter/ipset/ip_set_hash_netport.c
@@ -23,15 +23,16 @@
23#include <linux/netfilter/ipset/ip_set_getport.h> 23#include <linux/netfilter/ipset/ip_set_getport.h>
24#include <linux/netfilter/ipset/ip_set_hash.h> 24#include <linux/netfilter/ipset/ip_set_hash.h>
25 25
26#define REVISION_MIN 0 26#define IPSET_TYPE_REV_MIN 0
27/* 1 SCTP and UDPLITE support added */ 27/* 1 SCTP and UDPLITE support added */
28/* 2 Range as input support for IPv4 added */ 28/* 2 Range as input support for IPv4 added */
29/* 3 nomatch flag support added */ 29/* 3 nomatch flag support added */
30#define REVISION_MAX 4 /* Counters support added */ 30/* 4 Counters support added */
31#define IPSET_TYPE_REV_MAX 5 /* Comments support added */
31 32
32MODULE_LICENSE("GPL"); 33MODULE_LICENSE("GPL");
33MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 34MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
34IP_SET_MODULE_DESC("hash:net,port", REVISION_MIN, REVISION_MAX); 35IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
35MODULE_ALIAS("ip_set_hash:net,port"); 36MODULE_ALIAS("ip_set_hash:net,port");
36 37
37/* Type specific function prefix */ 38/* Type specific function prefix */
@@ -45,7 +46,7 @@ MODULE_ALIAS("ip_set_hash:net,port");
45 */ 46 */
46#define IP_SET_HASH_WITH_NETS_PACKED 47#define IP_SET_HASH_WITH_NETS_PACKED
47 48
48/* IPv4 variants */ 49/* IPv4 variant */
49 50
50/* Member elements */ 51/* Member elements */
51struct hash_netport4_elem { 52struct hash_netport4_elem {
@@ -56,34 +57,6 @@ struct hash_netport4_elem {
56 u8 nomatch:1; 57 u8 nomatch:1;
57}; 58};
58 59
59struct hash_netport4t_elem {
60 __be32 ip;
61 __be16 port;
62 u8 proto;
63 u8 cidr:7;
64 u8 nomatch:1;
65 unsigned long timeout;
66};
67
68struct hash_netport4c_elem {
69 __be32 ip;
70 __be16 port;
71 u8 proto;
72 u8 cidr:7;
73 u8 nomatch:1;
74 struct ip_set_counter counter;
75};
76
77struct hash_netport4ct_elem {
78 __be32 ip;
79 __be16 port;
80 u8 proto;
81 u8 cidr:7;
82 u8 nomatch:1;
83 struct ip_set_counter counter;
84 unsigned long timeout;
85};
86
87/* Common functions */ 60/* Common functions */
88 61
89static inline bool 62static inline bool
@@ -162,9 +135,9 @@ hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
162 const struct hash_netport *h = set->data; 135 const struct hash_netport *h = set->data;
163 ipset_adtfn adtfn = set->variant->adt[adt]; 136 ipset_adtfn adtfn = set->variant->adt[adt];
164 struct hash_netport4_elem e = { 137 struct hash_netport4_elem e = {
165 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1 138 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
166 }; 139 };
167 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 140 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
168 141
169 if (adt == IPSET_TEST) 142 if (adt == IPSET_TEST)
170 e.cidr = HOST_MASK - 1; 143 e.cidr = HOST_MASK - 1;
@@ -186,8 +159,8 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
186 const struct hash_netport *h = set->data; 159 const struct hash_netport *h = set->data;
187 ipset_adtfn adtfn = set->variant->adt[adt]; 160 ipset_adtfn adtfn = set->variant->adt[adt];
188 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 }; 161 struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
189 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 162 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
190 u32 port, port_to, p = 0, ip = 0, ip_to, last; 163 u32 port, port_to, p = 0, ip = 0, ip_to = 0, last;
191 bool with_ports = false; 164 bool with_ports = false;
192 u8 cidr; 165 u8 cidr;
193 int ret; 166 int ret;
@@ -287,7 +260,7 @@ hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
287 return ret; 260 return ret;
288} 261}
289 262
290/* IPv6 variants */ 263/* IPv6 variant */
291 264
292struct hash_netport6_elem { 265struct hash_netport6_elem {
293 union nf_inet_addr ip; 266 union nf_inet_addr ip;
@@ -297,34 +270,6 @@ struct hash_netport6_elem {
297 u8 nomatch:1; 270 u8 nomatch:1;
298}; 271};
299 272
300struct hash_netport6t_elem {
301 union nf_inet_addr ip;
302 __be16 port;
303 u8 proto;
304 u8 cidr:7;
305 u8 nomatch:1;
306 unsigned long timeout;
307};
308
309struct hash_netport6c_elem {
310 union nf_inet_addr ip;
311 __be16 port;
312 u8 proto;
313 u8 cidr:7;
314 u8 nomatch:1;
315 struct ip_set_counter counter;
316};
317
318struct hash_netport6ct_elem {
319 union nf_inet_addr ip;
320 __be16 port;
321 u8 proto;
322 u8 cidr:7;
323 u8 nomatch:1;
324 struct ip_set_counter counter;
325 unsigned long timeout;
326};
327
328/* Common functions */ 273/* Common functions */
329 274
330static inline bool 275static inline bool
@@ -407,9 +352,9 @@ hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
407 const struct hash_netport *h = set->data; 352 const struct hash_netport *h = set->data;
408 ipset_adtfn adtfn = set->variant->adt[adt]; 353 ipset_adtfn adtfn = set->variant->adt[adt];
409 struct hash_netport6_elem e = { 354 struct hash_netport6_elem e = {
410 .cidr = h->nets[0].cidr ? h->nets[0].cidr - 1 : HOST_MASK - 1, 355 .cidr = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK) - 1,
411 }; 356 };
412 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, h); 357 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
413 358
414 if (adt == IPSET_TEST) 359 if (adt == IPSET_TEST)
415 e.cidr = HOST_MASK - 1; 360 e.cidr = HOST_MASK - 1;
@@ -431,7 +376,7 @@ hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
431 const struct hash_netport *h = set->data; 376 const struct hash_netport *h = set->data;
432 ipset_adtfn adtfn = set->variant->adt[adt]; 377 ipset_adtfn adtfn = set->variant->adt[adt];
433 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 }; 378 struct hash_netport6_elem e = { .cidr = HOST_MASK - 1 };
434 struct ip_set_ext ext = IP_SET_INIT_UEXT(h); 379 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
435 u32 port, port_to; 380 u32 port, port_to;
436 bool with_ports = false; 381 bool with_ports = false;
437 u8 cidr; 382 u8 cidr;
@@ -518,8 +463,8 @@ static struct ip_set_type hash_netport_type __read_mostly = {
518 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH, 463 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
519 .dimension = IPSET_DIM_TWO, 464 .dimension = IPSET_DIM_TWO,
520 .family = NFPROTO_UNSPEC, 465 .family = NFPROTO_UNSPEC,
521 .revision_min = REVISION_MIN, 466 .revision_min = IPSET_TYPE_REV_MIN,
522 .revision_max = REVISION_MAX, 467 .revision_max = IPSET_TYPE_REV_MAX,
523 .create = hash_netport_create, 468 .create = hash_netport_create,
524 .create_policy = { 469 .create_policy = {
525 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 }, 470 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
@@ -542,6 +487,7 @@ static struct ip_set_type hash_netport_type __read_mostly = {
542 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 487 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
543 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 488 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
544 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 489 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
490 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
545 }, 491 },
546 .me = THIS_MODULE, 492 .me = THIS_MODULE,
547}; 493};
diff --git a/net/netfilter/ipset/ip_set_hash_netportnet.c b/net/netfilter/ipset/ip_set_hash_netportnet.c
new file mode 100644
index 000000000000..703d1192a6a2
--- /dev/null
+++ b/net/netfilter/ipset/ip_set_hash_netportnet.c
@@ -0,0 +1,586 @@
1/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 as
5 * published by the Free Software Foundation.
6 */
7
8/* Kernel module implementing an IP set type: the hash:ip,port,net type */
9
10#include <linux/jhash.h>
11#include <linux/module.h>
12#include <linux/ip.h>
13#include <linux/skbuff.h>
14#include <linux/errno.h>
15#include <linux/random.h>
16#include <net/ip.h>
17#include <net/ipv6.h>
18#include <net/netlink.h>
19#include <net/tcp.h>
20
21#include <linux/netfilter.h>
22#include <linux/netfilter/ipset/pfxlen.h>
23#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_getport.h>
25#include <linux/netfilter/ipset/ip_set_hash.h>
26
27#define IPSET_TYPE_REV_MIN 0
28#define IPSET_TYPE_REV_MAX 0 /* Comments support added */
29
30MODULE_LICENSE("GPL");
31MODULE_AUTHOR("Oliver Smith <oliver@8.c.9.b.0.7.4.0.1.0.0.2.ip6.arpa>");
32IP_SET_MODULE_DESC("hash:net,port,net", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
33MODULE_ALIAS("ip_set_hash:net,port,net");
34
35/* Type specific function prefix */
36#define HTYPE hash_netportnet
37#define IP_SET_HASH_WITH_PROTO
38#define IP_SET_HASH_WITH_NETS
39#define IPSET_NET_COUNT 2
40
41/* IPv4 variant */
42
43/* Member elements */
44struct hash_netportnet4_elem {
45 union {
46 __be32 ip[2];
47 __be64 ipcmp;
48 };
49 __be16 port;
50 union {
51 u8 cidr[2];
52 u16 ccmp;
53 };
54 u8 nomatch:1;
55 u8 proto;
56};
57
58/* Common functions */
59
60static inline bool
61hash_netportnet4_data_equal(const struct hash_netportnet4_elem *ip1,
62 const struct hash_netportnet4_elem *ip2,
63 u32 *multi)
64{
65 return ip1->ipcmp == ip2->ipcmp &&
66 ip1->ccmp == ip2->ccmp &&
67 ip1->port == ip2->port &&
68 ip1->proto == ip2->proto;
69}
70
71static inline int
72hash_netportnet4_do_data_match(const struct hash_netportnet4_elem *elem)
73{
74 return elem->nomatch ? -ENOTEMPTY : 1;
75}
76
77static inline void
78hash_netportnet4_data_set_flags(struct hash_netportnet4_elem *elem, u32 flags)
79{
80 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
81}
82
83static inline void
84hash_netportnet4_data_reset_flags(struct hash_netportnet4_elem *elem, u8 *flags)
85{
86 swap(*flags, elem->nomatch);
87}
88
89static inline void
90hash_netportnet4_data_reset_elem(struct hash_netportnet4_elem *elem,
91 struct hash_netportnet4_elem *orig)
92{
93 elem->ip[1] = orig->ip[1];
94}
95
96static inline void
97hash_netportnet4_data_netmask(struct hash_netportnet4_elem *elem,
98 u8 cidr, bool inner)
99{
100 if (inner) {
101 elem->ip[1] &= ip_set_netmask(cidr);
102 elem->cidr[1] = cidr;
103 } else {
104 elem->ip[0] &= ip_set_netmask(cidr);
105 elem->cidr[0] = cidr;
106 }
107}
108
109static bool
110hash_netportnet4_data_list(struct sk_buff *skb,
111 const struct hash_netportnet4_elem *data)
112{
113 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
114
115 if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip[0]) ||
116 nla_put_ipaddr4(skb, IPSET_ATTR_IP2, data->ip[1]) ||
117 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
118 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
119 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
120 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
121 (flags &&
122 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
123 goto nla_put_failure;
124 return 0;
125
126nla_put_failure:
127 return 1;
128}
129
130static inline void
131hash_netportnet4_data_next(struct hash_netportnet4_elem *next,
132 const struct hash_netportnet4_elem *d)
133{
134 next->ipcmp = d->ipcmp;
135 next->port = d->port;
136}
137
138#define MTYPE hash_netportnet4
139#define PF 4
140#define HOST_MASK 32
141#include "ip_set_hash_gen.h"
142
143static int
144hash_netportnet4_kadt(struct ip_set *set, const struct sk_buff *skb,
145 const struct xt_action_param *par,
146 enum ipset_adt adt, struct ip_set_adt_opt *opt)
147{
148 const struct hash_netportnet *h = set->data;
149 ipset_adtfn adtfn = set->variant->adt[adt];
150 struct hash_netportnet4_elem e = { };
151 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
152
153 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
154 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
155 if (adt == IPSET_TEST)
156 e.ccmp = (HOST_MASK << (sizeof(e.cidr[0]) * 8)) | HOST_MASK;
157
158 if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
159 &e.port, &e.proto))
160 return -EINVAL;
161
162 ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0]);
163 ip4addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1]);
164 e.ip[0] &= ip_set_netmask(e.cidr[0]);
165 e.ip[1] &= ip_set_netmask(e.cidr[1]);
166
167 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
168}
169
170static int
171hash_netportnet4_uadt(struct ip_set *set, struct nlattr *tb[],
172 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
173{
174 const struct hash_netportnet *h = set->data;
175 ipset_adtfn adtfn = set->variant->adt[adt];
176 struct hash_netportnet4_elem e = { };
177 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
178 u32 ip = 0, ip_to = 0, ip_last, p = 0, port, port_to;
179 u32 ip2_from = 0, ip2_to = 0, ip2_last, ip2;
180 bool with_ports = false;
181 u8 cidr, cidr2;
182 int ret;
183
184 e.cidr[0] = e.cidr[1] = HOST_MASK;
185 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
186 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
187 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
188 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
189 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
190 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
191 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
192 return -IPSET_ERR_PROTOCOL;
193
194 if (tb[IPSET_ATTR_LINENO])
195 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
196
197 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip) ||
198 ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2], &ip2_from) ||
199 ip_set_get_extensions(set, tb, &ext);
200 if (ret)
201 return ret;
202
203 if (tb[IPSET_ATTR_CIDR]) {
204 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
205 if (!cidr || cidr > HOST_MASK)
206 return -IPSET_ERR_INVALID_CIDR;
207 e.cidr[0] = cidr;
208 }
209
210 if (tb[IPSET_ATTR_CIDR2]) {
211 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
212 if (!cidr || cidr > HOST_MASK)
213 return -IPSET_ERR_INVALID_CIDR;
214 e.cidr[1] = cidr;
215 }
216
217 if (tb[IPSET_ATTR_PORT])
218 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
219 else
220 return -IPSET_ERR_PROTOCOL;
221
222 if (tb[IPSET_ATTR_PROTO]) {
223 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
224 with_ports = ip_set_proto_with_ports(e.proto);
225
226 if (e.proto == 0)
227 return -IPSET_ERR_INVALID_PROTO;
228 } else
229 return -IPSET_ERR_MISSING_PROTO;
230
231 if (!(with_ports || e.proto == IPPROTO_ICMP))
232 e.port = 0;
233
234 if (tb[IPSET_ATTR_CADT_FLAGS]) {
235 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
236 if (cadt_flags & IPSET_FLAG_NOMATCH)
237 flags |= (IPSET_FLAG_NOMATCH << 16);
238 }
239
240 with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
241 if (adt == IPSET_TEST ||
242 !(tb[IPSET_ATTR_IP_TO] || with_ports || tb[IPSET_ATTR_IP2_TO])) {
243 e.ip[0] = htonl(ip & ip_set_hostmask(e.cidr[0]));
244 e.ip[1] = htonl(ip2_from & ip_set_hostmask(e.cidr[1]));
245 ret = adtfn(set, &e, &ext, &ext, flags);
246 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
247 ip_set_eexist(ret, flags) ? 0 : ret;
248 }
249
250 ip_to = ip;
251 if (tb[IPSET_ATTR_IP_TO]) {
252 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
253 if (ret)
254 return ret;
255 if (ip > ip_to)
256 swap(ip, ip_to);
257 if (unlikely(ip + UINT_MAX == ip_to))
258 return -IPSET_ERR_HASH_RANGE;
259 }
260
261 port_to = port = ntohs(e.port);
262 if (tb[IPSET_ATTR_PORT_TO]) {
263 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
264 if (port > port_to)
265 swap(port, port_to);
266 }
267
268 ip2_to = ip2_from;
269 if (tb[IPSET_ATTR_IP2_TO]) {
270 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP2_TO], &ip2_to);
271 if (ret)
272 return ret;
273 if (ip2_from > ip2_to)
274 swap(ip2_from, ip2_to);
275 if (unlikely(ip2_from + UINT_MAX == ip2_to))
276 return -IPSET_ERR_HASH_RANGE;
277 }
278
279 if (retried)
280 ip = ntohl(h->next.ip[0]);
281
282 while (!after(ip, ip_to)) {
283 e.ip[0] = htonl(ip);
284 ip_last = ip_set_range_to_cidr(ip, ip_to, &cidr);
285 e.cidr[0] = cidr;
286 p = retried && ip == ntohl(h->next.ip[0]) ? ntohs(h->next.port)
287 : port;
288 for (; p <= port_to; p++) {
289 e.port = htons(p);
290 ip2 = (retried && ip == ntohl(h->next.ip[0]) &&
291 p == ntohs(h->next.port)) ? ntohl(h->next.ip[1])
292 : ip2_from;
293 while (!after(ip2, ip2_to)) {
294 e.ip[1] = htonl(ip2);
295 ip2_last = ip_set_range_to_cidr(ip2, ip2_to,
296 &cidr2);
297 e.cidr[1] = cidr2;
298 ret = adtfn(set, &e, &ext, &ext, flags);
299 if (ret && !ip_set_eexist(ret, flags))
300 return ret;
301 else
302 ret = 0;
303 ip2 = ip2_last + 1;
304 }
305 }
306 ip = ip_last + 1;
307 }
308 return ret;
309}
310
311/* IPv6 variant */
312
313struct hash_netportnet6_elem {
314 union nf_inet_addr ip[2];
315 __be16 port;
316 union {
317 u8 cidr[2];
318 u16 ccmp;
319 };
320 u8 nomatch:1;
321 u8 proto;
322};
323
324/* Common functions */
325
326static inline bool
327hash_netportnet6_data_equal(const struct hash_netportnet6_elem *ip1,
328 const struct hash_netportnet6_elem *ip2,
329 u32 *multi)
330{
331 return ipv6_addr_equal(&ip1->ip[0].in6, &ip2->ip[0].in6) &&
332 ipv6_addr_equal(&ip1->ip[1].in6, &ip2->ip[1].in6) &&
333 ip1->ccmp == ip2->ccmp &&
334 ip1->port == ip2->port &&
335 ip1->proto == ip2->proto;
336}
337
338static inline int
339hash_netportnet6_do_data_match(const struct hash_netportnet6_elem *elem)
340{
341 return elem->nomatch ? -ENOTEMPTY : 1;
342}
343
344static inline void
345hash_netportnet6_data_set_flags(struct hash_netportnet6_elem *elem, u32 flags)
346{
347 elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
348}
349
350static inline void
351hash_netportnet6_data_reset_flags(struct hash_netportnet6_elem *elem, u8 *flags)
352{
353 swap(*flags, elem->nomatch);
354}
355
356static inline void
357hash_netportnet6_data_reset_elem(struct hash_netportnet6_elem *elem,
358 struct hash_netportnet6_elem *orig)
359{
360 elem->ip[1] = orig->ip[1];
361}
362
363static inline void
364hash_netportnet6_data_netmask(struct hash_netportnet6_elem *elem,
365 u8 cidr, bool inner)
366{
367 if (inner) {
368 ip6_netmask(&elem->ip[1], cidr);
369 elem->cidr[1] = cidr;
370 } else {
371 ip6_netmask(&elem->ip[0], cidr);
372 elem->cidr[0] = cidr;
373 }
374}
375
376static bool
377hash_netportnet6_data_list(struct sk_buff *skb,
378 const struct hash_netportnet6_elem *data)
379{
380 u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
381
382 if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip[0].in6) ||
383 nla_put_ipaddr6(skb, IPSET_ATTR_IP2, &data->ip[1].in6) ||
384 nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
385 nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr[0]) ||
386 nla_put_u8(skb, IPSET_ATTR_CIDR2, data->cidr[1]) ||
387 nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
388 (flags &&
389 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
390 goto nla_put_failure;
391 return 0;
392
393nla_put_failure:
394 return 1;
395}
396
397static inline void
398hash_netportnet6_data_next(struct hash_netportnet4_elem *next,
399 const struct hash_netportnet6_elem *d)
400{
401 next->port = d->port;
402}
403
404#undef MTYPE
405#undef PF
406#undef HOST_MASK
407
408#define MTYPE hash_netportnet6
409#define PF 6
410#define HOST_MASK 128
411#define IP_SET_EMIT_CREATE
412#include "ip_set_hash_gen.h"
413
414static int
415hash_netportnet6_kadt(struct ip_set *set, const struct sk_buff *skb,
416 const struct xt_action_param *par,
417 enum ipset_adt adt, struct ip_set_adt_opt *opt)
418{
419 const struct hash_netportnet *h = set->data;
420 ipset_adtfn adtfn = set->variant->adt[adt];
421 struct hash_netportnet6_elem e = { };
422 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
423
424 e.cidr[0] = IP_SET_INIT_CIDR(h->nets[0].cidr[0], HOST_MASK);
425 e.cidr[1] = IP_SET_INIT_CIDR(h->nets[0].cidr[1], HOST_MASK);
426 if (adt == IPSET_TEST)
427 e.ccmp = (HOST_MASK << (sizeof(u8) * 8)) | HOST_MASK;
428
429 if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
430 &e.port, &e.proto))
431 return -EINVAL;
432
433 ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip[0].in6);
434 ip6addrptr(skb, opt->flags & IPSET_DIM_THREE_SRC, &e.ip[1].in6);
435 ip6_netmask(&e.ip[0], e.cidr[0]);
436 ip6_netmask(&e.ip[1], e.cidr[1]);
437
438 return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
439}
440
441static int
442hash_netportnet6_uadt(struct ip_set *set, struct nlattr *tb[],
443 enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
444{
445 const struct hash_netportnet *h = set->data;
446 ipset_adtfn adtfn = set->variant->adt[adt];
447 struct hash_netportnet6_elem e = { };
448 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
449 u32 port, port_to;
450 bool with_ports = false;
451 int ret;
452
453 e.cidr[0] = e.cidr[1] = HOST_MASK;
454 if (unlikely(!tb[IPSET_ATTR_IP] || !tb[IPSET_ATTR_IP2] ||
455 !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
456 !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
457 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
458 !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS) ||
459 !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
460 !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES)))
461 return -IPSET_ERR_PROTOCOL;
462 if (unlikely(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_IP2_TO]))
463 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
464
465 if (tb[IPSET_ATTR_LINENO])
466 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
467
468 ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip[0]) ||
469 ip_set_get_ipaddr6(tb[IPSET_ATTR_IP2], &e.ip[1]) ||
470 ip_set_get_extensions(set, tb, &ext);
471 if (ret)
472 return ret;
473
474 if (tb[IPSET_ATTR_CIDR])
475 e.cidr[0] = nla_get_u8(tb[IPSET_ATTR_CIDR]);
476
477 if (tb[IPSET_ATTR_CIDR2])
478 e.cidr[1] = nla_get_u8(tb[IPSET_ATTR_CIDR2]);
479
480 if (unlikely(!e.cidr[0] || e.cidr[0] > HOST_MASK || !e.cidr[1] ||
481 e.cidr[1] > HOST_MASK))
482 return -IPSET_ERR_INVALID_CIDR;
483
484 ip6_netmask(&e.ip[0], e.cidr[0]);
485 ip6_netmask(&e.ip[1], e.cidr[1]);
486
487 if (tb[IPSET_ATTR_PORT])
488 e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
489 else
490 return -IPSET_ERR_PROTOCOL;
491
492 if (tb[IPSET_ATTR_PROTO]) {
493 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
494 with_ports = ip_set_proto_with_ports(e.proto);
495
496 if (e.proto == 0)
497 return -IPSET_ERR_INVALID_PROTO;
498 } else
499 return -IPSET_ERR_MISSING_PROTO;
500
501 if (!(with_ports || e.proto == IPPROTO_ICMPV6))
502 e.port = 0;
503
504 if (tb[IPSET_ATTR_CADT_FLAGS]) {
505 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
506 if (cadt_flags & IPSET_FLAG_NOMATCH)
507 flags |= (IPSET_FLAG_NOMATCH << 16);
508 }
509
510 if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
511 ret = adtfn(set, &e, &ext, &ext, flags);
512 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
513 ip_set_eexist(ret, flags) ? 0 : ret;
514 }
515
516 port = ntohs(e.port);
517 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
518 if (port > port_to)
519 swap(port, port_to);
520
521 if (retried)
522 port = ntohs(h->next.port);
523 for (; port <= port_to; port++) {
524 e.port = htons(port);
525 ret = adtfn(set, &e, &ext, &ext, flags);
526
527 if (ret && !ip_set_eexist(ret, flags))
528 return ret;
529 else
530 ret = 0;
531 }
532 return ret;
533}
534
535static struct ip_set_type hash_netportnet_type __read_mostly = {
536 .name = "hash:net,port,net",
537 .protocol = IPSET_PROTOCOL,
538 .features = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_IP2 |
539 IPSET_TYPE_NOMATCH,
540 .dimension = IPSET_DIM_THREE,
541 .family = NFPROTO_UNSPEC,
542 .revision_min = IPSET_TYPE_REV_MIN,
543 .revision_max = IPSET_TYPE_REV_MAX,
544 .create = hash_netportnet_create,
545 .create_policy = {
546 [IPSET_ATTR_HASHSIZE] = { .type = NLA_U32 },
547 [IPSET_ATTR_MAXELEM] = { .type = NLA_U32 },
548 [IPSET_ATTR_PROBES] = { .type = NLA_U8 },
549 [IPSET_ATTR_RESIZE] = { .type = NLA_U8 },
550 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
551 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
552 },
553 .adt_policy = {
554 [IPSET_ATTR_IP] = { .type = NLA_NESTED },
555 [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED },
556 [IPSET_ATTR_IP2] = { .type = NLA_NESTED },
557 [IPSET_ATTR_IP2_TO] = { .type = NLA_NESTED },
558 [IPSET_ATTR_PORT] = { .type = NLA_U16 },
559 [IPSET_ATTR_PORT_TO] = { .type = NLA_U16 },
560 [IPSET_ATTR_CIDR] = { .type = NLA_U8 },
561 [IPSET_ATTR_CIDR2] = { .type = NLA_U8 },
562 [IPSET_ATTR_PROTO] = { .type = NLA_U8 },
563 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
564 [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 },
565 [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
566 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
567 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
568 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
569 },
570 .me = THIS_MODULE,
571};
572
573static int __init
574hash_netportnet_init(void)
575{
576 return ip_set_type_register(&hash_netportnet_type);
577}
578
579static void __exit
580hash_netportnet_fini(void)
581{
582 ip_set_type_unregister(&hash_netportnet_type);
583}
584
585module_init(hash_netportnet_init);
586module_exit(hash_netportnet_fini);
diff --git a/net/netfilter/ipset/ip_set_list_set.c b/net/netfilter/ipset/ip_set_list_set.c
index 979b8c90e422..3e2317f3cf68 100644
--- a/net/netfilter/ipset/ip_set_list_set.c
+++ b/net/netfilter/ipset/ip_set_list_set.c
@@ -15,12 +15,13 @@
15#include <linux/netfilter/ipset/ip_set.h> 15#include <linux/netfilter/ipset/ip_set.h>
16#include <linux/netfilter/ipset/ip_set_list.h> 16#include <linux/netfilter/ipset/ip_set_list.h>
17 17
18#define REVISION_MIN 0 18#define IPSET_TYPE_REV_MIN 0
19#define REVISION_MAX 1 /* Counters support added */ 19/* 1 Counters support added */
20#define IPSET_TYPE_REV_MAX 2 /* Comments support added */
20 21
21MODULE_LICENSE("GPL"); 22MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>"); 23MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
23IP_SET_MODULE_DESC("list:set", REVISION_MIN, REVISION_MAX); 24IP_SET_MODULE_DESC("list:set", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
24MODULE_ALIAS("ip_set_list:set"); 25MODULE_ALIAS("ip_set_list:set");
25 26
26/* Member elements */ 27/* Member elements */
@@ -28,28 +29,6 @@ struct set_elem {
28 ip_set_id_t id; 29 ip_set_id_t id;
29}; 30};
30 31
31struct sett_elem {
32 struct {
33 ip_set_id_t id;
34 } __attribute__ ((aligned));
35 unsigned long timeout;
36};
37
38struct setc_elem {
39 struct {
40 ip_set_id_t id;
41 } __attribute__ ((aligned));
42 struct ip_set_counter counter;
43};
44
45struct setct_elem {
46 struct {
47 ip_set_id_t id;
48 } __attribute__ ((aligned));
49 struct ip_set_counter counter;
50 unsigned long timeout;
51};
52
53struct set_adt_elem { 32struct set_adt_elem {
54 ip_set_id_t id; 33 ip_set_id_t id;
55 ip_set_id_t refid; 34 ip_set_id_t refid;
@@ -58,24 +37,14 @@ struct set_adt_elem {
58 37
59/* Type structure */ 38/* Type structure */
60struct list_set { 39struct list_set {
61 size_t dsize; /* element size */
62 size_t offset[IPSET_OFFSET_MAX]; /* Offsets to extensions */
63 u32 size; /* size of set list array */ 40 u32 size; /* size of set list array */
64 u32 timeout; /* timeout value */
65 struct timer_list gc; /* garbage collection */ 41 struct timer_list gc; /* garbage collection */
42 struct net *net; /* namespace */
66 struct set_elem members[0]; /* the set members */ 43 struct set_elem members[0]; /* the set members */
67}; 44};
68 45
69static inline struct set_elem * 46#define list_set_elem(set, map, id) \
70list_set_elem(const struct list_set *map, u32 id) 47 (struct set_elem *)((void *)(map)->members + (id) * (set)->dsize)
71{
72 return (struct set_elem *)((void *)map->members + id * map->dsize);
73}
74
75#define ext_timeout(e, m) \
76(unsigned long *)((void *)(e) + (m)->offset[IPSET_OFFSET_TIMEOUT])
77#define ext_counter(e, m) \
78(struct ip_set_counter *)((void *)(e) + (m)->offset[IPSET_OFFSET_COUNTER])
79 48
80static int 49static int
81list_set_ktest(struct ip_set *set, const struct sk_buff *skb, 50list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
@@ -92,16 +61,16 @@ list_set_ktest(struct ip_set *set, const struct sk_buff *skb,
92 if (opt->cmdflags & IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE) 61 if (opt->cmdflags & IPSET_FLAG_SKIP_SUBCOUNTER_UPDATE)
93 opt->cmdflags &= ~IPSET_FLAG_SKIP_COUNTER_UPDATE; 62 opt->cmdflags &= ~IPSET_FLAG_SKIP_COUNTER_UPDATE;
94 for (i = 0; i < map->size; i++) { 63 for (i = 0; i < map->size; i++) {
95 e = list_set_elem(map, i); 64 e = list_set_elem(set, map, i);
96 if (e->id == IPSET_INVALID_ID) 65 if (e->id == IPSET_INVALID_ID)
97 return 0; 66 return 0;
98 if (SET_WITH_TIMEOUT(set) && 67 if (SET_WITH_TIMEOUT(set) &&
99 ip_set_timeout_expired(ext_timeout(e, map))) 68 ip_set_timeout_expired(ext_timeout(e, set)))
100 continue; 69 continue;
101 ret = ip_set_test(e->id, skb, par, opt); 70 ret = ip_set_test(e->id, skb, par, opt);
102 if (ret > 0) { 71 if (ret > 0) {
103 if (SET_WITH_COUNTER(set)) 72 if (SET_WITH_COUNTER(set))
104 ip_set_update_counter(ext_counter(e, map), 73 ip_set_update_counter(ext_counter(e, set),
105 ext, &opt->ext, 74 ext, &opt->ext,
106 cmdflags); 75 cmdflags);
107 return ret; 76 return ret;
@@ -121,11 +90,11 @@ list_set_kadd(struct ip_set *set, const struct sk_buff *skb,
121 int ret; 90 int ret;
122 91
123 for (i = 0; i < map->size; i++) { 92 for (i = 0; i < map->size; i++) {
124 e = list_set_elem(map, i); 93 e = list_set_elem(set, map, i);
125 if (e->id == IPSET_INVALID_ID) 94 if (e->id == IPSET_INVALID_ID)
126 return 0; 95 return 0;
127 if (SET_WITH_TIMEOUT(set) && 96 if (SET_WITH_TIMEOUT(set) &&
128 ip_set_timeout_expired(ext_timeout(e, map))) 97 ip_set_timeout_expired(ext_timeout(e, set)))
129 continue; 98 continue;
130 ret = ip_set_add(e->id, skb, par, opt); 99 ret = ip_set_add(e->id, skb, par, opt);
131 if (ret == 0) 100 if (ret == 0)
@@ -145,11 +114,11 @@ list_set_kdel(struct ip_set *set, const struct sk_buff *skb,
145 int ret; 114 int ret;
146 115
147 for (i = 0; i < map->size; i++) { 116 for (i = 0; i < map->size; i++) {
148 e = list_set_elem(map, i); 117 e = list_set_elem(set, map, i);
149 if (e->id == IPSET_INVALID_ID) 118 if (e->id == IPSET_INVALID_ID)
150 return 0; 119 return 0;
151 if (SET_WITH_TIMEOUT(set) && 120 if (SET_WITH_TIMEOUT(set) &&
152 ip_set_timeout_expired(ext_timeout(e, map))) 121 ip_set_timeout_expired(ext_timeout(e, set)))
153 continue; 122 continue;
154 ret = ip_set_del(e->id, skb, par, opt); 123 ret = ip_set_del(e->id, skb, par, opt);
155 if (ret == 0) 124 if (ret == 0)
@@ -163,8 +132,7 @@ list_set_kadt(struct ip_set *set, const struct sk_buff *skb,
163 const struct xt_action_param *par, 132 const struct xt_action_param *par,
164 enum ipset_adt adt, struct ip_set_adt_opt *opt) 133 enum ipset_adt adt, struct ip_set_adt_opt *opt)
165{ 134{
166 struct list_set *map = set->data; 135 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
167 struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, map);
168 136
169 switch (adt) { 137 switch (adt) {
170 case IPSET_TEST: 138 case IPSET_TEST:
@@ -188,10 +156,10 @@ id_eq(const struct ip_set *set, u32 i, ip_set_id_t id)
188 if (i >= map->size) 156 if (i >= map->size)
189 return 0; 157 return 0;
190 158
191 e = list_set_elem(map, i); 159 e = list_set_elem(set, map, i);
192 return !!(e->id == id && 160 return !!(e->id == id &&
193 !(SET_WITH_TIMEOUT(set) && 161 !(SET_WITH_TIMEOUT(set) &&
194 ip_set_timeout_expired(ext_timeout(e, map)))); 162 ip_set_timeout_expired(ext_timeout(e, set))));
195} 163}
196 164
197static int 165static int
@@ -199,28 +167,36 @@ list_set_add(struct ip_set *set, u32 i, struct set_adt_elem *d,
199 const struct ip_set_ext *ext) 167 const struct ip_set_ext *ext)
200{ 168{
201 struct list_set *map = set->data; 169 struct list_set *map = set->data;
202 struct set_elem *e = list_set_elem(map, i); 170 struct set_elem *e = list_set_elem(set, map, i);
203 171
204 if (e->id != IPSET_INVALID_ID) { 172 if (e->id != IPSET_INVALID_ID) {
205 if (i == map->size - 1) 173 if (i == map->size - 1) {
206 /* Last element replaced: e.g. add new,before,last */ 174 /* Last element replaced: e.g. add new,before,last */
207 ip_set_put_byindex(e->id); 175 ip_set_put_byindex(map->net, e->id);
208 else { 176 ip_set_ext_destroy(set, e);
209 struct set_elem *x = list_set_elem(map, map->size - 1); 177 } else {
178 struct set_elem *x = list_set_elem(set, map,
179 map->size - 1);
210 180
211 /* Last element pushed off */ 181 /* Last element pushed off */
212 if (x->id != IPSET_INVALID_ID) 182 if (x->id != IPSET_INVALID_ID) {
213 ip_set_put_byindex(x->id); 183 ip_set_put_byindex(map->net, x->id);
214 memmove(list_set_elem(map, i + 1), e, 184 ip_set_ext_destroy(set, x);
215 map->dsize * (map->size - (i + 1))); 185 }
186 memmove(list_set_elem(set, map, i + 1), e,
187 set->dsize * (map->size - (i + 1)));
188 /* Extensions must be initialized to zero */
189 memset(e, 0, set->dsize);
216 } 190 }
217 } 191 }
218 192
219 e->id = d->id; 193 e->id = d->id;
220 if (SET_WITH_TIMEOUT(set)) 194 if (SET_WITH_TIMEOUT(set))
221 ip_set_timeout_set(ext_timeout(e, map), ext->timeout); 195 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
222 if (SET_WITH_COUNTER(set)) 196 if (SET_WITH_COUNTER(set))
223 ip_set_init_counter(ext_counter(e, map), ext); 197 ip_set_init_counter(ext_counter(e, set), ext);
198 if (SET_WITH_COMMENT(set))
199 ip_set_init_comment(ext_comment(e, set), ext);
224 return 0; 200 return 0;
225} 201}
226 202
@@ -228,16 +204,17 @@ static int
228list_set_del(struct ip_set *set, u32 i) 204list_set_del(struct ip_set *set, u32 i)
229{ 205{
230 struct list_set *map = set->data; 206 struct list_set *map = set->data;
231 struct set_elem *e = list_set_elem(map, i); 207 struct set_elem *e = list_set_elem(set, map, i);
232 208
233 ip_set_put_byindex(e->id); 209 ip_set_put_byindex(map->net, e->id);
210 ip_set_ext_destroy(set, e);
234 211
235 if (i < map->size - 1) 212 if (i < map->size - 1)
236 memmove(e, list_set_elem(map, i + 1), 213 memmove(e, list_set_elem(set, map, i + 1),
237 map->dsize * (map->size - (i + 1))); 214 set->dsize * (map->size - (i + 1)));
238 215
239 /* Last element */ 216 /* Last element */
240 e = list_set_elem(map, map->size - 1); 217 e = list_set_elem(set, map, map->size - 1);
241 e->id = IPSET_INVALID_ID; 218 e->id = IPSET_INVALID_ID;
242 return 0; 219 return 0;
243} 220}
@@ -247,13 +224,16 @@ set_cleanup_entries(struct ip_set *set)
247{ 224{
248 struct list_set *map = set->data; 225 struct list_set *map = set->data;
249 struct set_elem *e; 226 struct set_elem *e;
250 u32 i; 227 u32 i = 0;
251 228
252 for (i = 0; i < map->size; i++) { 229 while (i < map->size) {
253 e = list_set_elem(map, i); 230 e = list_set_elem(set, map, i);
254 if (e->id != IPSET_INVALID_ID && 231 if (e->id != IPSET_INVALID_ID &&
255 ip_set_timeout_expired(ext_timeout(e, map))) 232 ip_set_timeout_expired(ext_timeout(e, set)))
256 list_set_del(set, i); 233 list_set_del(set, i);
234 /* Check element moved to position i in next loop */
235 else
236 i++;
257 } 237 }
258} 238}
259 239
@@ -268,11 +248,11 @@ list_set_utest(struct ip_set *set, void *value, const struct ip_set_ext *ext,
268 int ret; 248 int ret;
269 249
270 for (i = 0; i < map->size; i++) { 250 for (i = 0; i < map->size; i++) {
271 e = list_set_elem(map, i); 251 e = list_set_elem(set, map, i);
272 if (e->id == IPSET_INVALID_ID) 252 if (e->id == IPSET_INVALID_ID)
273 return 0; 253 return 0;
274 else if (SET_WITH_TIMEOUT(set) && 254 else if (SET_WITH_TIMEOUT(set) &&
275 ip_set_timeout_expired(ext_timeout(e, map))) 255 ip_set_timeout_expired(ext_timeout(e, set)))
276 continue; 256 continue;
277 else if (e->id != d->id) 257 else if (e->id != d->id)
278 continue; 258 continue;
@@ -299,14 +279,14 @@ list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
299 bool flag_exist = flags & IPSET_FLAG_EXIST; 279 bool flag_exist = flags & IPSET_FLAG_EXIST;
300 u32 i, ret = 0; 280 u32 i, ret = 0;
301 281
282 if (SET_WITH_TIMEOUT(set))
283 set_cleanup_entries(set);
284
302 /* Check already added element */ 285 /* Check already added element */
303 for (i = 0; i < map->size; i++) { 286 for (i = 0; i < map->size; i++) {
304 e = list_set_elem(map, i); 287 e = list_set_elem(set, map, i);
305 if (e->id == IPSET_INVALID_ID) 288 if (e->id == IPSET_INVALID_ID)
306 goto insert; 289 goto insert;
307 else if (SET_WITH_TIMEOUT(set) &&
308 ip_set_timeout_expired(ext_timeout(e, map)))
309 continue;
310 else if (e->id != d->id) 290 else if (e->id != d->id)
311 continue; 291 continue;
312 292
@@ -319,18 +299,22 @@ list_set_uadd(struct ip_set *set, void *value, const struct ip_set_ext *ext,
319 /* Can't re-add */ 299 /* Can't re-add */
320 return -IPSET_ERR_EXIST; 300 return -IPSET_ERR_EXIST;
321 /* Update extensions */ 301 /* Update extensions */
302 ip_set_ext_destroy(set, e);
303
322 if (SET_WITH_TIMEOUT(set)) 304 if (SET_WITH_TIMEOUT(set))
323 ip_set_timeout_set(ext_timeout(e, map), ext->timeout); 305 ip_set_timeout_set(ext_timeout(e, set), ext->timeout);
324 if (SET_WITH_COUNTER(set)) 306 if (SET_WITH_COUNTER(set))
325 ip_set_init_counter(ext_counter(e, map), ext); 307 ip_set_init_counter(ext_counter(e, set), ext);
308 if (SET_WITH_COMMENT(set))
309 ip_set_init_comment(ext_comment(e, set), ext);
326 /* Set is already added to the list */ 310 /* Set is already added to the list */
327 ip_set_put_byindex(d->id); 311 ip_set_put_byindex(map->net, d->id);
328 return 0; 312 return 0;
329 } 313 }
330insert: 314insert:
331 ret = -IPSET_ERR_LIST_FULL; 315 ret = -IPSET_ERR_LIST_FULL;
332 for (i = 0; i < map->size && ret == -IPSET_ERR_LIST_FULL; i++) { 316 for (i = 0; i < map->size && ret == -IPSET_ERR_LIST_FULL; i++) {
333 e = list_set_elem(map, i); 317 e = list_set_elem(set, map, i);
334 if (e->id == IPSET_INVALID_ID) 318 if (e->id == IPSET_INVALID_ID)
335 ret = d->before != 0 ? -IPSET_ERR_REF_EXIST 319 ret = d->before != 0 ? -IPSET_ERR_REF_EXIST
336 : list_set_add(set, i, d, ext); 320 : list_set_add(set, i, d, ext);
@@ -355,12 +339,12 @@ list_set_udel(struct ip_set *set, void *value, const struct ip_set_ext *ext,
355 u32 i; 339 u32 i;
356 340
357 for (i = 0; i < map->size; i++) { 341 for (i = 0; i < map->size; i++) {
358 e = list_set_elem(map, i); 342 e = list_set_elem(set, map, i);
359 if (e->id == IPSET_INVALID_ID) 343 if (e->id == IPSET_INVALID_ID)
360 return d->before != 0 ? -IPSET_ERR_REF_EXIST 344 return d->before != 0 ? -IPSET_ERR_REF_EXIST
361 : -IPSET_ERR_EXIST; 345 : -IPSET_ERR_EXIST;
362 else if (SET_WITH_TIMEOUT(set) && 346 else if (SET_WITH_TIMEOUT(set) &&
363 ip_set_timeout_expired(ext_timeout(e, map))) 347 ip_set_timeout_expired(ext_timeout(e, set)))
364 continue; 348 continue;
365 else if (e->id != d->id) 349 else if (e->id != d->id)
366 continue; 350 continue;
@@ -386,7 +370,7 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],
386 struct list_set *map = set->data; 370 struct list_set *map = set->data;
387 ipset_adtfn adtfn = set->variant->adt[adt]; 371 ipset_adtfn adtfn = set->variant->adt[adt];
388 struct set_adt_elem e = { .refid = IPSET_INVALID_ID }; 372 struct set_adt_elem e = { .refid = IPSET_INVALID_ID };
389 struct ip_set_ext ext = IP_SET_INIT_UEXT(map); 373 struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
390 struct ip_set *s; 374 struct ip_set *s;
391 int ret = 0; 375 int ret = 0;
392 376
@@ -403,7 +387,7 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],
403 ret = ip_set_get_extensions(set, tb, &ext); 387 ret = ip_set_get_extensions(set, tb, &ext);
404 if (ret) 388 if (ret)
405 return ret; 389 return ret;
406 e.id = ip_set_get_byname(nla_data(tb[IPSET_ATTR_NAME]), &s); 390 e.id = ip_set_get_byname(map->net, nla_data(tb[IPSET_ATTR_NAME]), &s);
407 if (e.id == IPSET_INVALID_ID) 391 if (e.id == IPSET_INVALID_ID)
408 return -IPSET_ERR_NAME; 392 return -IPSET_ERR_NAME;
409 /* "Loop detection" */ 393 /* "Loop detection" */
@@ -423,7 +407,8 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],
423 } 407 }
424 408
425 if (tb[IPSET_ATTR_NAMEREF]) { 409 if (tb[IPSET_ATTR_NAMEREF]) {
426 e.refid = ip_set_get_byname(nla_data(tb[IPSET_ATTR_NAMEREF]), 410 e.refid = ip_set_get_byname(map->net,
411 nla_data(tb[IPSET_ATTR_NAMEREF]),
427 &s); 412 &s);
428 if (e.refid == IPSET_INVALID_ID) { 413 if (e.refid == IPSET_INVALID_ID) {
429 ret = -IPSET_ERR_NAMEREF; 414 ret = -IPSET_ERR_NAMEREF;
@@ -439,9 +424,9 @@ list_set_uadt(struct ip_set *set, struct nlattr *tb[],
439 424
440finish: 425finish:
441 if (e.refid != IPSET_INVALID_ID) 426 if (e.refid != IPSET_INVALID_ID)
442 ip_set_put_byindex(e.refid); 427 ip_set_put_byindex(map->net, e.refid);
443 if (adt != IPSET_ADD || ret) 428 if (adt != IPSET_ADD || ret)
444 ip_set_put_byindex(e.id); 429 ip_set_put_byindex(map->net, e.id);
445 430
446 return ip_set_eexist(ret, flags) ? 0 : ret; 431 return ip_set_eexist(ret, flags) ? 0 : ret;
447} 432}
@@ -454,9 +439,10 @@ list_set_flush(struct ip_set *set)
454 u32 i; 439 u32 i;
455 440
456 for (i = 0; i < map->size; i++) { 441 for (i = 0; i < map->size; i++) {
457 e = list_set_elem(map, i); 442 e = list_set_elem(set, map, i);
458 if (e->id != IPSET_INVALID_ID) { 443 if (e->id != IPSET_INVALID_ID) {
459 ip_set_put_byindex(e->id); 444 ip_set_put_byindex(map->net, e->id);
445 ip_set_ext_destroy(set, e);
460 e->id = IPSET_INVALID_ID; 446 e->id = IPSET_INVALID_ID;
461 } 447 }
462 } 448 }
@@ -485,14 +471,11 @@ list_set_head(struct ip_set *set, struct sk_buff *skb)
485 if (!nested) 471 if (!nested)
486 goto nla_put_failure; 472 goto nla_put_failure;
487 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) || 473 if (nla_put_net32(skb, IPSET_ATTR_SIZE, htonl(map->size)) ||
488 (SET_WITH_TIMEOUT(set) &&
489 nla_put_net32(skb, IPSET_ATTR_TIMEOUT, htonl(map->timeout))) ||
490 (SET_WITH_COUNTER(set) &&
491 nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS,
492 htonl(IPSET_FLAG_WITH_COUNTERS))) ||
493 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) || 474 nla_put_net32(skb, IPSET_ATTR_REFERENCES, htonl(set->ref - 1)) ||
494 nla_put_net32(skb, IPSET_ATTR_MEMSIZE, 475 nla_put_net32(skb, IPSET_ATTR_MEMSIZE,
495 htonl(sizeof(*map) + map->size * map->dsize))) 476 htonl(sizeof(*map) + map->size * set->dsize)))
477 goto nla_put_failure;
478 if (unlikely(ip_set_put_flags(skb, set)))
496 goto nla_put_failure; 479 goto nla_put_failure;
497 ipset_nest_end(skb, nested); 480 ipset_nest_end(skb, nested);
498 481
@@ -507,19 +490,20 @@ list_set_list(const struct ip_set *set,
507{ 490{
508 const struct list_set *map = set->data; 491 const struct list_set *map = set->data;
509 struct nlattr *atd, *nested; 492 struct nlattr *atd, *nested;
510 u32 i, first = cb->args[2]; 493 u32 i, first = cb->args[IPSET_CB_ARG0];
511 const struct set_elem *e; 494 const struct set_elem *e;
512 495
513 atd = ipset_nest_start(skb, IPSET_ATTR_ADT); 496 atd = ipset_nest_start(skb, IPSET_ATTR_ADT);
514 if (!atd) 497 if (!atd)
515 return -EMSGSIZE; 498 return -EMSGSIZE;
516 for (; cb->args[2] < map->size; cb->args[2]++) { 499 for (; cb->args[IPSET_CB_ARG0] < map->size;
517 i = cb->args[2]; 500 cb->args[IPSET_CB_ARG0]++) {
518 e = list_set_elem(map, i); 501 i = cb->args[IPSET_CB_ARG0];
502 e = list_set_elem(set, map, i);
519 if (e->id == IPSET_INVALID_ID) 503 if (e->id == IPSET_INVALID_ID)
520 goto finish; 504 goto finish;
521 if (SET_WITH_TIMEOUT(set) && 505 if (SET_WITH_TIMEOUT(set) &&
522 ip_set_timeout_expired(ext_timeout(e, map))) 506 ip_set_timeout_expired(ext_timeout(e, set)))
523 continue; 507 continue;
524 nested = ipset_nest_start(skb, IPSET_ATTR_DATA); 508 nested = ipset_nest_start(skb, IPSET_ATTR_DATA);
525 if (!nested) { 509 if (!nested) {
@@ -530,31 +514,25 @@ list_set_list(const struct ip_set *set,
530 goto nla_put_failure; 514 goto nla_put_failure;
531 } 515 }
532 if (nla_put_string(skb, IPSET_ATTR_NAME, 516 if (nla_put_string(skb, IPSET_ATTR_NAME,
533 ip_set_name_byindex(e->id))) 517 ip_set_name_byindex(map->net, e->id)))
534 goto nla_put_failure;
535 if (SET_WITH_TIMEOUT(set) &&
536 nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
537 htonl(ip_set_timeout_get(
538 ext_timeout(e, map)))))
539 goto nla_put_failure; 518 goto nla_put_failure;
540 if (SET_WITH_COUNTER(set) && 519 if (ip_set_put_extensions(skb, set, e, true))
541 ip_set_put_counter(skb, ext_counter(e, map)))
542 goto nla_put_failure; 520 goto nla_put_failure;
543 ipset_nest_end(skb, nested); 521 ipset_nest_end(skb, nested);
544 } 522 }
545finish: 523finish:
546 ipset_nest_end(skb, atd); 524 ipset_nest_end(skb, atd);
547 /* Set listing finished */ 525 /* Set listing finished */
548 cb->args[2] = 0; 526 cb->args[IPSET_CB_ARG0] = 0;
549 return 0; 527 return 0;
550 528
551nla_put_failure: 529nla_put_failure:
552 nla_nest_cancel(skb, nested); 530 nla_nest_cancel(skb, nested);
553 ipset_nest_end(skb, atd);
554 if (unlikely(i == first)) { 531 if (unlikely(i == first)) {
555 cb->args[2] = 0; 532 cb->args[IPSET_CB_ARG0] = 0;
556 return -EMSGSIZE; 533 return -EMSGSIZE;
557 } 534 }
535 ipset_nest_end(skb, atd);
558 return 0; 536 return 0;
559} 537}
560 538
@@ -565,7 +543,7 @@ list_set_same_set(const struct ip_set *a, const struct ip_set *b)
565 const struct list_set *y = b->data; 543 const struct list_set *y = b->data;
566 544
567 return x->size == y->size && 545 return x->size == y->size &&
568 x->timeout == y->timeout && 546 a->timeout == b->timeout &&
569 a->extensions == b->extensions; 547 a->extensions == b->extensions;
570} 548}
571 549
@@ -594,7 +572,7 @@ list_set_gc(unsigned long ul_set)
594 set_cleanup_entries(set); 572 set_cleanup_entries(set);
595 write_unlock_bh(&set->lock); 573 write_unlock_bh(&set->lock);
596 574
597 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ; 575 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
598 add_timer(&map->gc); 576 add_timer(&map->gc);
599} 577}
600 578
@@ -606,43 +584,40 @@ list_set_gc_init(struct ip_set *set, void (*gc)(unsigned long ul_set))
606 init_timer(&map->gc); 584 init_timer(&map->gc);
607 map->gc.data = (unsigned long) set; 585 map->gc.data = (unsigned long) set;
608 map->gc.function = gc; 586 map->gc.function = gc;
609 map->gc.expires = jiffies + IPSET_GC_PERIOD(map->timeout) * HZ; 587 map->gc.expires = jiffies + IPSET_GC_PERIOD(set->timeout) * HZ;
610 add_timer(&map->gc); 588 add_timer(&map->gc);
611} 589}
612 590
613/* Create list:set type of sets */ 591/* Create list:set type of sets */
614 592
615static struct list_set * 593static bool
616init_list_set(struct ip_set *set, u32 size, size_t dsize, 594init_list_set(struct net *net, struct ip_set *set, u32 size)
617 unsigned long timeout)
618{ 595{
619 struct list_set *map; 596 struct list_set *map;
620 struct set_elem *e; 597 struct set_elem *e;
621 u32 i; 598 u32 i;
622 599
623 map = kzalloc(sizeof(*map) + size * dsize, GFP_KERNEL); 600 map = kzalloc(sizeof(*map) + size * set->dsize, GFP_KERNEL);
624 if (!map) 601 if (!map)
625 return NULL; 602 return false;
626 603
627 map->size = size; 604 map->size = size;
628 map->dsize = dsize; 605 map->net = net;
629 map->timeout = timeout;
630 set->data = map; 606 set->data = map;
631 607
632 for (i = 0; i < size; i++) { 608 for (i = 0; i < size; i++) {
633 e = list_set_elem(map, i); 609 e = list_set_elem(set, map, i);
634 e->id = IPSET_INVALID_ID; 610 e->id = IPSET_INVALID_ID;
635 } 611 }
636 612
637 return map; 613 return true;
638} 614}
639 615
640static int 616static int
641list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags) 617list_set_create(struct net *net, struct ip_set *set, struct nlattr *tb[],
618 u32 flags)
642{ 619{
643 struct list_set *map; 620 u32 size = IP_SET_LIST_DEFAULT_SIZE;
644 u32 size = IP_SET_LIST_DEFAULT_SIZE, cadt_flags = 0;
645 unsigned long timeout = 0;
646 621
647 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) || 622 if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_SIZE) ||
648 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || 623 !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
@@ -654,45 +629,13 @@ list_set_create(struct ip_set *set, struct nlattr *tb[], u32 flags)
654 if (size < IP_SET_LIST_MIN_SIZE) 629 if (size < IP_SET_LIST_MIN_SIZE)
655 size = IP_SET_LIST_MIN_SIZE; 630 size = IP_SET_LIST_MIN_SIZE;
656 631
657 if (tb[IPSET_ATTR_CADT_FLAGS])
658 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
659 if (tb[IPSET_ATTR_TIMEOUT])
660 timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
661 set->variant = &set_variant; 632 set->variant = &set_variant;
662 if (cadt_flags & IPSET_FLAG_WITH_COUNTERS) { 633 set->dsize = ip_set_elem_len(set, tb, sizeof(struct set_elem));
663 set->extensions |= IPSET_EXT_COUNTER; 634 if (!init_list_set(net, set, size))
664 if (tb[IPSET_ATTR_TIMEOUT]) { 635 return -ENOMEM;
665 map = init_list_set(set, size, 636 if (tb[IPSET_ATTR_TIMEOUT]) {
666 sizeof(struct setct_elem), timeout); 637 set->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
667 if (!map)
668 return -ENOMEM;
669 set->extensions |= IPSET_EXT_TIMEOUT;
670 map->offset[IPSET_OFFSET_TIMEOUT] =
671 offsetof(struct setct_elem, timeout);
672 map->offset[IPSET_OFFSET_COUNTER] =
673 offsetof(struct setct_elem, counter);
674 list_set_gc_init(set, list_set_gc);
675 } else {
676 map = init_list_set(set, size,
677 sizeof(struct setc_elem), 0);
678 if (!map)
679 return -ENOMEM;
680 map->offset[IPSET_OFFSET_COUNTER] =
681 offsetof(struct setc_elem, counter);
682 }
683 } else if (tb[IPSET_ATTR_TIMEOUT]) {
684 map = init_list_set(set, size,
685 sizeof(struct sett_elem), timeout);
686 if (!map)
687 return -ENOMEM;
688 set->extensions |= IPSET_EXT_TIMEOUT;
689 map->offset[IPSET_OFFSET_TIMEOUT] =
690 offsetof(struct sett_elem, timeout);
691 list_set_gc_init(set, list_set_gc); 638 list_set_gc_init(set, list_set_gc);
692 } else {
693 map = init_list_set(set, size, sizeof(struct set_elem), 0);
694 if (!map)
695 return -ENOMEM;
696 } 639 }
697 return 0; 640 return 0;
698} 641}
@@ -703,8 +646,8 @@ static struct ip_set_type list_set_type __read_mostly = {
703 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST, 646 .features = IPSET_TYPE_NAME | IPSET_DUMP_LAST,
704 .dimension = IPSET_DIM_ONE, 647 .dimension = IPSET_DIM_ONE,
705 .family = NFPROTO_UNSPEC, 648 .family = NFPROTO_UNSPEC,
706 .revision_min = REVISION_MIN, 649 .revision_min = IPSET_TYPE_REV_MIN,
707 .revision_max = REVISION_MAX, 650 .revision_max = IPSET_TYPE_REV_MAX,
708 .create = list_set_create, 651 .create = list_set_create,
709 .create_policy = { 652 .create_policy = {
710 [IPSET_ATTR_SIZE] = { .type = NLA_U32 }, 653 [IPSET_ATTR_SIZE] = { .type = NLA_U32 },
@@ -721,6 +664,7 @@ static struct ip_set_type list_set_type __read_mostly = {
721 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, 664 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
722 [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, 665 [IPSET_ATTR_BYTES] = { .type = NLA_U64 },
723 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, 666 [IPSET_ATTR_PACKETS] = { .type = NLA_U64 },
667 [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING },
724 }, 668 },
725 .me = THIS_MODULE, 669 .me = THIS_MODULE,
726}; 670};
diff --git a/net/netfilter/ipvs/ip_vs_core.c b/net/netfilter/ipvs/ip_vs_core.c
index 74fd00c27210..4f26ee46b51f 100644
--- a/net/netfilter/ipvs/ip_vs_core.c
+++ b/net/netfilter/ipvs/ip_vs_core.c
@@ -1139,12 +1139,6 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1139 ip_vs_fill_iph_skb(af, skb, &iph); 1139 ip_vs_fill_iph_skb(af, skb, &iph);
1140#ifdef CONFIG_IP_VS_IPV6 1140#ifdef CONFIG_IP_VS_IPV6
1141 if (af == AF_INET6) { 1141 if (af == AF_INET6) {
1142 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1143 struct sk_buff *reasm = skb_nfct_reasm(skb);
1144 /* Save fw mark for coming frags */
1145 reasm->ipvs_property = 1;
1146 reasm->mark = skb->mark;
1147 }
1148 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { 1142 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1149 int related; 1143 int related;
1150 int verdict = ip_vs_out_icmp_v6(skb, &related, 1144 int verdict = ip_vs_out_icmp_v6(skb, &related,
@@ -1239,11 +1233,11 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1239 * Check if packet is reply for established ip_vs_conn. 1233 * Check if packet is reply for established ip_vs_conn.
1240 */ 1234 */
1241static unsigned int 1235static unsigned int
1242ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb, 1236ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1243 const struct net_device *in, const struct net_device *out, 1237 const struct net_device *in, const struct net_device *out,
1244 int (*okfn)(struct sk_buff *)) 1238 int (*okfn)(struct sk_buff *))
1245{ 1239{
1246 return ip_vs_out(hooknum, skb, AF_INET); 1240 return ip_vs_out(ops->hooknum, skb, AF_INET);
1247} 1241}
1248 1242
1249/* 1243/*
@@ -1251,11 +1245,11 @@ ip_vs_reply4(unsigned int hooknum, struct sk_buff *skb,
1251 * Check if packet is reply for established ip_vs_conn. 1245 * Check if packet is reply for established ip_vs_conn.
1252 */ 1246 */
1253static unsigned int 1247static unsigned int
1254ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb, 1248ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1255 const struct net_device *in, const struct net_device *out, 1249 const struct net_device *in, const struct net_device *out,
1256 int (*okfn)(struct sk_buff *)) 1250 int (*okfn)(struct sk_buff *))
1257{ 1251{
1258 return ip_vs_out(hooknum, skb, AF_INET); 1252 return ip_vs_out(ops->hooknum, skb, AF_INET);
1259} 1253}
1260 1254
1261#ifdef CONFIG_IP_VS_IPV6 1255#ifdef CONFIG_IP_VS_IPV6
@@ -1266,11 +1260,11 @@ ip_vs_local_reply4(unsigned int hooknum, struct sk_buff *skb,
1266 * Check if packet is reply for established ip_vs_conn. 1260 * Check if packet is reply for established ip_vs_conn.
1267 */ 1261 */
1268static unsigned int 1262static unsigned int
1269ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb, 1263ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1270 const struct net_device *in, const struct net_device *out, 1264 const struct net_device *in, const struct net_device *out,
1271 int (*okfn)(struct sk_buff *)) 1265 int (*okfn)(struct sk_buff *))
1272{ 1266{
1273 return ip_vs_out(hooknum, skb, AF_INET6); 1267 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1274} 1268}
1275 1269
1276/* 1270/*
@@ -1278,11 +1272,11 @@ ip_vs_reply6(unsigned int hooknum, struct sk_buff *skb,
1278 * Check if packet is reply for established ip_vs_conn. 1272 * Check if packet is reply for established ip_vs_conn.
1279 */ 1273 */
1280static unsigned int 1274static unsigned int
1281ip_vs_local_reply6(unsigned int hooknum, struct sk_buff *skb, 1275ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1282 const struct net_device *in, const struct net_device *out, 1276 const struct net_device *in, const struct net_device *out,
1283 int (*okfn)(struct sk_buff *)) 1277 int (*okfn)(struct sk_buff *))
1284{ 1278{
1285 return ip_vs_out(hooknum, skb, AF_INET6); 1279 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1286} 1280}
1287 1281
1288#endif 1282#endif
@@ -1614,12 +1608,6 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1614 1608
1615#ifdef CONFIG_IP_VS_IPV6 1609#ifdef CONFIG_IP_VS_IPV6
1616 if (af == AF_INET6) { 1610 if (af == AF_INET6) {
1617 if (!iph.fragoffs && skb_nfct_reasm(skb)) {
1618 struct sk_buff *reasm = skb_nfct_reasm(skb);
1619 /* Save fw mark for coming frags. */
1620 reasm->ipvs_property = 1;
1621 reasm->mark = skb->mark;
1622 }
1623 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) { 1611 if (unlikely(iph.protocol == IPPROTO_ICMPV6)) {
1624 int related; 1612 int related;
1625 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum, 1613 int verdict = ip_vs_in_icmp_v6(skb, &related, hooknum,
@@ -1671,9 +1659,8 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1671 /* sorry, all this trouble for a no-hit :) */ 1659 /* sorry, all this trouble for a no-hit :) */
1672 IP_VS_DBG_PKT(12, af, pp, skb, 0, 1660 IP_VS_DBG_PKT(12, af, pp, skb, 0,
1673 "ip_vs_in: packet continues traversal as normal"); 1661 "ip_vs_in: packet continues traversal as normal");
1674 if (iph.fragoffs && !skb_nfct_reasm(skb)) { 1662 if (iph.fragoffs) {
1675 /* Fragment that couldn't be mapped to a conn entry 1663 /* Fragment that couldn't be mapped to a conn entry
1676 * and don't have any pointer to a reasm skb
1677 * is missing module nf_defrag_ipv6 1664 * is missing module nf_defrag_ipv6
1678 */ 1665 */
1679 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n"); 1666 IP_VS_DBG_RL("Unhandled frag, load nf_defrag_ipv6\n");
@@ -1733,12 +1720,12 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1733 * Schedule and forward packets from remote clients 1720 * Schedule and forward packets from remote clients
1734 */ 1721 */
1735static unsigned int 1722static unsigned int
1736ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb, 1723ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1737 const struct net_device *in, 1724 const struct net_device *in,
1738 const struct net_device *out, 1725 const struct net_device *out,
1739 int (*okfn)(struct sk_buff *)) 1726 int (*okfn)(struct sk_buff *))
1740{ 1727{
1741 return ip_vs_in(hooknum, skb, AF_INET); 1728 return ip_vs_in(ops->hooknum, skb, AF_INET);
1742} 1729}
1743 1730
1744/* 1731/*
@@ -1746,58 +1733,26 @@ ip_vs_remote_request4(unsigned int hooknum, struct sk_buff *skb,
1746 * Schedule and forward packets from local clients 1733 * Schedule and forward packets from local clients
1747 */ 1734 */
1748static unsigned int 1735static unsigned int
1749ip_vs_local_request4(unsigned int hooknum, struct sk_buff *skb, 1736ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1750 const struct net_device *in, const struct net_device *out, 1737 const struct net_device *in, const struct net_device *out,
1751 int (*okfn)(struct sk_buff *)) 1738 int (*okfn)(struct sk_buff *))
1752{ 1739{
1753 return ip_vs_in(hooknum, skb, AF_INET); 1740 return ip_vs_in(ops->hooknum, skb, AF_INET);
1754} 1741}
1755 1742
1756#ifdef CONFIG_IP_VS_IPV6 1743#ifdef CONFIG_IP_VS_IPV6
1757 1744
1758/* 1745/*
1759 * AF_INET6 fragment handling
1760 * Copy info from first fragment, to the rest of them.
1761 */
1762static unsigned int
1763ip_vs_preroute_frag6(unsigned int hooknum, struct sk_buff *skb,
1764 const struct net_device *in,
1765 const struct net_device *out,
1766 int (*okfn)(struct sk_buff *))
1767{
1768 struct sk_buff *reasm = skb_nfct_reasm(skb);
1769 struct net *net;
1770
1771 /* Skip if not a "replay" from nf_ct_frag6_output or first fragment.
1772 * ipvs_property is set when checking first fragment
1773 * in ip_vs_in() and ip_vs_out().
1774 */
1775 if (reasm)
1776 IP_VS_DBG(2, "Fragment recv prop:%d\n", reasm->ipvs_property);
1777 if (!reasm || !reasm->ipvs_property)
1778 return NF_ACCEPT;
1779
1780 net = skb_net(skb);
1781 if (!net_ipvs(net)->enable)
1782 return NF_ACCEPT;
1783
1784 /* Copy stored fw mark, saved in ip_vs_{in,out} */
1785 skb->mark = reasm->mark;
1786
1787 return NF_ACCEPT;
1788}
1789
1790/*
1791 * AF_INET6 handler in NF_INET_LOCAL_IN chain 1746 * AF_INET6 handler in NF_INET_LOCAL_IN chain
1792 * Schedule and forward packets from remote clients 1747 * Schedule and forward packets from remote clients
1793 */ 1748 */
1794static unsigned int 1749static unsigned int
1795ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb, 1750ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1796 const struct net_device *in, 1751 const struct net_device *in,
1797 const struct net_device *out, 1752 const struct net_device *out,
1798 int (*okfn)(struct sk_buff *)) 1753 int (*okfn)(struct sk_buff *))
1799{ 1754{
1800 return ip_vs_in(hooknum, skb, AF_INET6); 1755 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1801} 1756}
1802 1757
1803/* 1758/*
@@ -1805,11 +1760,11 @@ ip_vs_remote_request6(unsigned int hooknum, struct sk_buff *skb,
1805 * Schedule and forward packets from local clients 1760 * Schedule and forward packets from local clients
1806 */ 1761 */
1807static unsigned int 1762static unsigned int
1808ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb, 1763ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1809 const struct net_device *in, const struct net_device *out, 1764 const struct net_device *in, const struct net_device *out,
1810 int (*okfn)(struct sk_buff *)) 1765 int (*okfn)(struct sk_buff *))
1811{ 1766{
1812 return ip_vs_in(hooknum, skb, AF_INET6); 1767 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1813} 1768}
1814 1769
1815#endif 1770#endif
@@ -1825,7 +1780,7 @@ ip_vs_local_request6(unsigned int hooknum, struct sk_buff *skb,
1825 * and send them to ip_vs_in_icmp. 1780 * and send them to ip_vs_in_icmp.
1826 */ 1781 */
1827static unsigned int 1782static unsigned int
1828ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb, 1783ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1829 const struct net_device *in, const struct net_device *out, 1784 const struct net_device *in, const struct net_device *out,
1830 int (*okfn)(struct sk_buff *)) 1785 int (*okfn)(struct sk_buff *))
1831{ 1786{
@@ -1842,12 +1797,12 @@ ip_vs_forward_icmp(unsigned int hooknum, struct sk_buff *skb,
1842 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable)) 1797 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1843 return NF_ACCEPT; 1798 return NF_ACCEPT;
1844 1799
1845 return ip_vs_in_icmp(skb, &r, hooknum); 1800 return ip_vs_in_icmp(skb, &r, ops->hooknum);
1846} 1801}
1847 1802
1848#ifdef CONFIG_IP_VS_IPV6 1803#ifdef CONFIG_IP_VS_IPV6
1849static unsigned int 1804static unsigned int
1850ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb, 1805ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1851 const struct net_device *in, const struct net_device *out, 1806 const struct net_device *in, const struct net_device *out,
1852 int (*okfn)(struct sk_buff *)) 1807 int (*okfn)(struct sk_buff *))
1853{ 1808{
@@ -1866,7 +1821,7 @@ ip_vs_forward_icmp_v6(unsigned int hooknum, struct sk_buff *skb,
1866 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable)) 1821 if (unlikely(sysctl_backup_only(ipvs) || !ipvs->enable))
1867 return NF_ACCEPT; 1822 return NF_ACCEPT;
1868 1823
1869 return ip_vs_in_icmp_v6(skb, &r, hooknum, &iphdr); 1824 return ip_vs_in_icmp_v6(skb, &r, ops->hooknum, &iphdr);
1870} 1825}
1871#endif 1826#endif
1872 1827
@@ -1924,14 +1879,6 @@ static struct nf_hook_ops ip_vs_ops[] __read_mostly = {
1924 .priority = 100, 1879 .priority = 100,
1925 }, 1880 },
1926#ifdef CONFIG_IP_VS_IPV6 1881#ifdef CONFIG_IP_VS_IPV6
1927 /* After mangle & nat fetch 2:nd fragment and following */
1928 {
1929 .hook = ip_vs_preroute_frag6,
1930 .owner = THIS_MODULE,
1931 .pf = NFPROTO_IPV6,
1932 .hooknum = NF_INET_PRE_ROUTING,
1933 .priority = NF_IP6_PRI_NAT_DST + 1,
1934 },
1935 /* After packet filtering, change source only for VS/NAT */ 1882 /* After packet filtering, change source only for VS/NAT */
1936 { 1883 {
1937 .hook = ip_vs_reply6, 1884 .hook = ip_vs_reply6,
diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c
index a3df9bddc4f7..35be035ee0ce 100644
--- a/net/netfilter/ipvs/ip_vs_ctl.c
+++ b/net/netfilter/ipvs/ip_vs_ctl.c
@@ -704,7 +704,7 @@ static void ip_vs_dest_free(struct ip_vs_dest *dest)
704 __ip_vs_dst_cache_reset(dest); 704 __ip_vs_dst_cache_reset(dest);
705 __ip_vs_svc_put(svc, false); 705 __ip_vs_svc_put(svc, false);
706 free_percpu(dest->stats.cpustats); 706 free_percpu(dest->stats.cpustats);
707 kfree(dest); 707 ip_vs_dest_put_and_free(dest);
708} 708}
709 709
710/* 710/*
@@ -842,7 +842,7 @@ ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
842 struct ip_vs_dest **dest_p) 842 struct ip_vs_dest **dest_p)
843{ 843{
844 struct ip_vs_dest *dest; 844 struct ip_vs_dest *dest;
845 unsigned int atype; 845 unsigned int atype, i;
846 846
847 EnterFunction(2); 847 EnterFunction(2);
848 848
@@ -869,6 +869,12 @@ ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest,
869 if (!dest->stats.cpustats) 869 if (!dest->stats.cpustats)
870 goto err_alloc; 870 goto err_alloc;
871 871
872 for_each_possible_cpu(i) {
873 struct ip_vs_cpu_stats *ip_vs_dest_stats;
874 ip_vs_dest_stats = per_cpu_ptr(dest->stats.cpustats, i);
875 u64_stats_init(&ip_vs_dest_stats->syncp);
876 }
877
872 dest->af = svc->af; 878 dest->af = svc->af;
873 dest->protocol = svc->protocol; 879 dest->protocol = svc->protocol;
874 dest->vaddr = svc->addr; 880 dest->vaddr = svc->addr;
@@ -1134,7 +1140,7 @@ static int
1134ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u, 1140ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1135 struct ip_vs_service **svc_p) 1141 struct ip_vs_service **svc_p)
1136{ 1142{
1137 int ret = 0; 1143 int ret = 0, i;
1138 struct ip_vs_scheduler *sched = NULL; 1144 struct ip_vs_scheduler *sched = NULL;
1139 struct ip_vs_pe *pe = NULL; 1145 struct ip_vs_pe *pe = NULL;
1140 struct ip_vs_service *svc = NULL; 1146 struct ip_vs_service *svc = NULL;
@@ -1184,6 +1190,13 @@ ip_vs_add_service(struct net *net, struct ip_vs_service_user_kern *u,
1184 goto out_err; 1190 goto out_err;
1185 } 1191 }
1186 1192
1193 for_each_possible_cpu(i) {
1194 struct ip_vs_cpu_stats *ip_vs_stats;
1195 ip_vs_stats = per_cpu_ptr(svc->stats.cpustats, i);
1196 u64_stats_init(&ip_vs_stats->syncp);
1197 }
1198
1199
1187 /* I'm the first user of the service */ 1200 /* I'm the first user of the service */
1188 atomic_set(&svc->refcnt, 0); 1201 atomic_set(&svc->refcnt, 0);
1189 1202
@@ -3567,7 +3580,7 @@ out:
3567} 3580}
3568 3581
3569 3582
3570static struct genl_ops ip_vs_genl_ops[] __read_mostly = { 3583static const struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3571 { 3584 {
3572 .cmd = IPVS_CMD_NEW_SERVICE, 3585 .cmd = IPVS_CMD_NEW_SERVICE,
3573 .flags = GENL_ADMIN_PERM, 3586 .flags = GENL_ADMIN_PERM,
@@ -3666,7 +3679,7 @@ static struct genl_ops ip_vs_genl_ops[] __read_mostly = {
3666static int __init ip_vs_genl_register(void) 3679static int __init ip_vs_genl_register(void)
3667{ 3680{
3668 return genl_register_family_with_ops(&ip_vs_genl_family, 3681 return genl_register_family_with_ops(&ip_vs_genl_family,
3669 ip_vs_genl_ops, ARRAY_SIZE(ip_vs_genl_ops)); 3682 ip_vs_genl_ops);
3670} 3683}
3671 3684
3672static void ip_vs_genl_unregister(void) 3685static void ip_vs_genl_unregister(void)
@@ -3780,7 +3793,7 @@ static struct notifier_block ip_vs_dst_notifier = {
3780 3793
3781int __net_init ip_vs_control_net_init(struct net *net) 3794int __net_init ip_vs_control_net_init(struct net *net)
3782{ 3795{
3783 int idx; 3796 int i, idx;
3784 struct netns_ipvs *ipvs = net_ipvs(net); 3797 struct netns_ipvs *ipvs = net_ipvs(net);
3785 3798
3786 /* Initialize rs_table */ 3799 /* Initialize rs_table */
@@ -3799,6 +3812,12 @@ int __net_init ip_vs_control_net_init(struct net *net)
3799 if (!ipvs->tot_stats.cpustats) 3812 if (!ipvs->tot_stats.cpustats)
3800 return -ENOMEM; 3813 return -ENOMEM;
3801 3814
3815 for_each_possible_cpu(i) {
3816 struct ip_vs_cpu_stats *ipvs_tot_stats;
3817 ipvs_tot_stats = per_cpu_ptr(ipvs->tot_stats.cpustats, i);
3818 u64_stats_init(&ipvs_tot_stats->syncp);
3819 }
3820
3802 spin_lock_init(&ipvs->tot_stats.lock); 3821 spin_lock_init(&ipvs->tot_stats.lock);
3803 3822
3804 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops); 3823 proc_create("ip_vs", 0, net->proc_net, &ip_vs_info_fops);
@@ -3820,10 +3839,6 @@ void __net_exit ip_vs_control_net_cleanup(struct net *net)
3820{ 3839{
3821 struct netns_ipvs *ipvs = net_ipvs(net); 3840 struct netns_ipvs *ipvs = net_ipvs(net);
3822 3841
3823 /* Some dest can be in grace period even before cleanup, we have to
3824 * defer ip_vs_trash_cleanup until ip_vs_dest_wait_readers is called.
3825 */
3826 rcu_barrier();
3827 ip_vs_trash_cleanup(net); 3842 ip_vs_trash_cleanup(net);
3828 ip_vs_stop_estimator(net, &ipvs->tot_stats); 3843 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3829 ip_vs_control_net_cleanup_sysctl(net); 3844 ip_vs_control_net_cleanup_sysctl(net);
diff --git a/net/netfilter/ipvs/ip_vs_lblc.c b/net/netfilter/ipvs/ip_vs_lblc.c
index eff13c94498e..ca056a331e60 100644
--- a/net/netfilter/ipvs/ip_vs_lblc.c
+++ b/net/netfilter/ipvs/ip_vs_lblc.c
@@ -136,7 +136,7 @@ static void ip_vs_lblc_rcu_free(struct rcu_head *head)
136 struct ip_vs_lblc_entry, 136 struct ip_vs_lblc_entry,
137 rcu_head); 137 rcu_head);
138 138
139 ip_vs_dest_put(en->dest); 139 ip_vs_dest_put_and_free(en->dest);
140 kfree(en); 140 kfree(en);
141} 141}
142 142
diff --git a/net/netfilter/ipvs/ip_vs_lblcr.c b/net/netfilter/ipvs/ip_vs_lblcr.c
index 0b8550089a2e..3f21a2f47de1 100644
--- a/net/netfilter/ipvs/ip_vs_lblcr.c
+++ b/net/netfilter/ipvs/ip_vs_lblcr.c
@@ -130,7 +130,7 @@ static void ip_vs_lblcr_elem_rcu_free(struct rcu_head *head)
130 struct ip_vs_dest_set_elem *e; 130 struct ip_vs_dest_set_elem *e;
131 131
132 e = container_of(head, struct ip_vs_dest_set_elem, rcu_head); 132 e = container_of(head, struct ip_vs_dest_set_elem, rcu_head);
133 ip_vs_dest_put(e->dest); 133 ip_vs_dest_put_and_free(e->dest);
134 kfree(e); 134 kfree(e);
135} 135}
136 136
diff --git a/net/netfilter/ipvs/ip_vs_nfct.c b/net/netfilter/ipvs/ip_vs_nfct.c
index c8beafd401aa..5a355a46d1dc 100644
--- a/net/netfilter/ipvs/ip_vs_nfct.c
+++ b/net/netfilter/ipvs/ip_vs_nfct.c
@@ -63,6 +63,7 @@
63#include <net/ip_vs.h> 63#include <net/ip_vs.h>
64#include <net/netfilter/nf_conntrack_core.h> 64#include <net/netfilter/nf_conntrack_core.h>
65#include <net/netfilter/nf_conntrack_expect.h> 65#include <net/netfilter/nf_conntrack_expect.h>
66#include <net/netfilter/nf_conntrack_seqadj.h>
66#include <net/netfilter/nf_conntrack_helper.h> 67#include <net/netfilter/nf_conntrack_helper.h>
67#include <net/netfilter/nf_conntrack_zones.h> 68#include <net/netfilter/nf_conntrack_zones.h>
68 69
@@ -97,6 +98,11 @@ ip_vs_update_conntrack(struct sk_buff *skb, struct ip_vs_conn *cp, int outin)
97 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) 98 if (CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL)
98 return; 99 return;
99 100
101 /* Applications may adjust TCP seqs */
102 if (cp->app && nf_ct_protonum(ct) == IPPROTO_TCP &&
103 !nfct_seqadj(ct) && !nfct_seqadj_ext_add(ct))
104 return;
105
100 /* 106 /*
101 * The connection is not yet in the hashtable, so we update it. 107 * The connection is not yet in the hashtable, so we update it.
102 * CIP->VIP will remain the same, so leave the tuple in 108 * CIP->VIP will remain the same, so leave the tuple in
diff --git a/net/netfilter/ipvs/ip_vs_pe_sip.c b/net/netfilter/ipvs/ip_vs_pe_sip.c
index 9ef22bdce9f1..bed5f7042529 100644
--- a/net/netfilter/ipvs/ip_vs_pe_sip.c
+++ b/net/netfilter/ipvs/ip_vs_pe_sip.c
@@ -65,7 +65,6 @@ static int get_callid(const char *dptr, unsigned int dataoff,
65static int 65static int
66ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb) 66ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
67{ 67{
68 struct sk_buff *reasm = skb_nfct_reasm(skb);
69 struct ip_vs_iphdr iph; 68 struct ip_vs_iphdr iph;
70 unsigned int dataoff, datalen, matchoff, matchlen; 69 unsigned int dataoff, datalen, matchoff, matchlen;
71 const char *dptr; 70 const char *dptr;
@@ -79,15 +78,10 @@ ip_vs_sip_fill_param(struct ip_vs_conn_param *p, struct sk_buff *skb)
79 /* todo: IPv6 fragments: 78 /* todo: IPv6 fragments:
80 * I think this only should be done for the first fragment. /HS 79 * I think this only should be done for the first fragment. /HS
81 */ 80 */
82 if (reasm) { 81 dataoff = iph.len + sizeof(struct udphdr);
83 skb = reasm;
84 dataoff = iph.thoff_reasm + sizeof(struct udphdr);
85 } else
86 dataoff = iph.len + sizeof(struct udphdr);
87 82
88 if (dataoff >= skb->len) 83 if (dataoff >= skb->len)
89 return -EINVAL; 84 return -EINVAL;
90 /* todo: Check if this will mess-up the reasm skb !!! /HS */
91 retc = skb_linearize(skb); 85 retc = skb_linearize(skb);
92 if (retc < 0) 86 if (retc < 0)
93 return retc; 87 return retc;
diff --git a/net/netfilter/ipvs/ip_vs_proto_sctp.c b/net/netfilter/ipvs/ip_vs_proto_sctp.c
index 23e596e438b3..2f7ea7564044 100644
--- a/net/netfilter/ipvs/ip_vs_proto_sctp.c
+++ b/net/netfilter/ipvs/ip_vs_proto_sctp.c
@@ -20,13 +20,18 @@ sctp_conn_schedule(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,
20 sctp_sctphdr_t *sh, _sctph; 20 sctp_sctphdr_t *sh, _sctph;
21 21
22 sh = skb_header_pointer(skb, iph->len, sizeof(_sctph), &_sctph); 22 sh = skb_header_pointer(skb, iph->len, sizeof(_sctph), &_sctph);
23 if (sh == NULL) 23 if (sh == NULL) {
24 *verdict = NF_DROP;
24 return 0; 25 return 0;
26 }
25 27
26 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t), 28 sch = skb_header_pointer(skb, iph->len + sizeof(sctp_sctphdr_t),
27 sizeof(_schunkh), &_schunkh); 29 sizeof(_schunkh), &_schunkh);
28 if (sch == NULL) 30 if (sch == NULL) {
31 *verdict = NF_DROP;
29 return 0; 32 return 0;
33 }
34
30 net = skb_net(skb); 35 net = skb_net(skb);
31 ipvs = net_ipvs(net); 36 ipvs = net_ipvs(net);
32 rcu_read_lock(); 37 rcu_read_lock();
@@ -76,6 +81,7 @@ sctp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
76{ 81{
77 sctp_sctphdr_t *sctph; 82 sctp_sctphdr_t *sctph;
78 unsigned int sctphoff = iph->len; 83 unsigned int sctphoff = iph->len;
84 bool payload_csum = false;
79 85
80#ifdef CONFIG_IP_VS_IPV6 86#ifdef CONFIG_IP_VS_IPV6
81 if (cp->af == AF_INET6 && iph->fragoffs) 87 if (cp->af == AF_INET6 && iph->fragoffs)
@@ -87,19 +93,31 @@ sctp_snat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
87 return 0; 93 return 0;
88 94
89 if (unlikely(cp->app != NULL)) { 95 if (unlikely(cp->app != NULL)) {
96 int ret;
97
90 /* Some checks before mangling */ 98 /* Some checks before mangling */
91 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) 99 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
92 return 0; 100 return 0;
93 101
94 /* Call application helper if needed */ 102 /* Call application helper if needed */
95 if (!ip_vs_app_pkt_out(cp, skb)) 103 ret = ip_vs_app_pkt_out(cp, skb);
104 if (ret == 0)
96 return 0; 105 return 0;
106 /* ret=2: csum update is needed after payload mangling */
107 if (ret == 2)
108 payload_csum = true;
97 } 109 }
98 110
99 sctph = (void *) skb_network_header(skb) + sctphoff; 111 sctph = (void *) skb_network_header(skb) + sctphoff;
100 sctph->source = cp->vport;
101 112
102 sctp_nat_csum(skb, sctph, sctphoff); 113 /* Only update csum if we really have to */
114 if (sctph->source != cp->vport || payload_csum ||
115 skb->ip_summed == CHECKSUM_PARTIAL) {
116 sctph->source = cp->vport;
117 sctp_nat_csum(skb, sctph, sctphoff);
118 } else {
119 skb->ip_summed = CHECKSUM_UNNECESSARY;
120 }
103 121
104 return 1; 122 return 1;
105} 123}
@@ -110,6 +128,7 @@ sctp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
110{ 128{
111 sctp_sctphdr_t *sctph; 129 sctp_sctphdr_t *sctph;
112 unsigned int sctphoff = iph->len; 130 unsigned int sctphoff = iph->len;
131 bool payload_csum = false;
113 132
114#ifdef CONFIG_IP_VS_IPV6 133#ifdef CONFIG_IP_VS_IPV6
115 if (cp->af == AF_INET6 && iph->fragoffs) 134 if (cp->af == AF_INET6 && iph->fragoffs)
@@ -121,19 +140,32 @@ sctp_dnat_handler(struct sk_buff *skb, struct ip_vs_protocol *pp,
121 return 0; 140 return 0;
122 141
123 if (unlikely(cp->app != NULL)) { 142 if (unlikely(cp->app != NULL)) {
143 int ret;
144
124 /* Some checks before mangling */ 145 /* Some checks before mangling */
125 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp)) 146 if (pp->csum_check && !pp->csum_check(cp->af, skb, pp))
126 return 0; 147 return 0;
127 148
128 /* Call application helper if needed */ 149 /* Call application helper if needed */
129 if (!ip_vs_app_pkt_in(cp, skb)) 150 ret = ip_vs_app_pkt_in(cp, skb);
151 if (ret == 0)
130 return 0; 152 return 0;
153 /* ret=2: csum update is needed after payload mangling */
154 if (ret == 2)
155 payload_csum = true;
131 } 156 }
132 157
133 sctph = (void *) skb_network_header(skb) + sctphoff; 158 sctph = (void *) skb_network_header(skb) + sctphoff;
134 sctph->dest = cp->dport;
135 159
136 sctp_nat_csum(skb, sctph, sctphoff); 160 /* Only update csum if we really have to */
161 if (sctph->dest != cp->dport || payload_csum ||
162 (skb->ip_summed == CHECKSUM_PARTIAL &&
163 !(skb_dst(skb)->dev->features & NETIF_F_SCTP_CSUM))) {
164 sctph->dest = cp->dport;
165 sctp_nat_csum(skb, sctph, sctphoff);
166 } else if (skb->ip_summed != CHECKSUM_PARTIAL) {
167 skb->ip_summed = CHECKSUM_UNNECESSARY;
168 }
137 169
138 return 1; 170 return 1;
139} 171}
diff --git a/net/netfilter/ipvs/ip_vs_sh.c b/net/netfilter/ipvs/ip_vs_sh.c
index 3588faebe529..cc65b2f42cd4 100644
--- a/net/netfilter/ipvs/ip_vs_sh.c
+++ b/net/netfilter/ipvs/ip_vs_sh.c
@@ -115,27 +115,46 @@ ip_vs_sh_get(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
115} 115}
116 116
117 117
118/* As ip_vs_sh_get, but with fallback if selected server is unavailable */ 118/* As ip_vs_sh_get, but with fallback if selected server is unavailable
119 *
120 * The fallback strategy loops around the table starting from a "random"
121 * point (in fact, it is chosen to be the original hash value to make the
122 * algorithm deterministic) to find a new server.
123 */
119static inline struct ip_vs_dest * 124static inline struct ip_vs_dest *
120ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s, 125ip_vs_sh_get_fallback(struct ip_vs_service *svc, struct ip_vs_sh_state *s,
121 const union nf_inet_addr *addr, __be16 port) 126 const union nf_inet_addr *addr, __be16 port)
122{ 127{
123 unsigned int offset; 128 unsigned int offset, roffset;
124 unsigned int hash; 129 unsigned int hash, ihash;
125 struct ip_vs_dest *dest; 130 struct ip_vs_dest *dest;
126 131
132 /* first try the dest it's supposed to go to */
133 ihash = ip_vs_sh_hashkey(svc->af, addr, port, 0);
134 dest = rcu_dereference(s->buckets[ihash].dest);
135 if (!dest)
136 return NULL;
137 if (!is_unavailable(dest))
138 return dest;
139
140 IP_VS_DBG_BUF(6, "SH: selected unavailable server %s:%d, reselecting",
141 IP_VS_DBG_ADDR(svc->af, &dest->addr), ntohs(dest->port));
142
143 /* if the original dest is unavailable, loop around the table
144 * starting from ihash to find a new dest
145 */
127 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) { 146 for (offset = 0; offset < IP_VS_SH_TAB_SIZE; offset++) {
128 hash = ip_vs_sh_hashkey(svc->af, addr, port, offset); 147 roffset = (offset + ihash) % IP_VS_SH_TAB_SIZE;
148 hash = ip_vs_sh_hashkey(svc->af, addr, port, roffset);
129 dest = rcu_dereference(s->buckets[hash].dest); 149 dest = rcu_dereference(s->buckets[hash].dest);
130 if (!dest) 150 if (!dest)
131 break; 151 break;
132 if (is_unavailable(dest)) 152 if (!is_unavailable(dest))
133 IP_VS_DBG_BUF(6, "SH: selected unavailable server "
134 "%s:%d (offset %d)",
135 IP_VS_DBG_ADDR(svc->af, &dest->addr),
136 ntohs(dest->port), offset);
137 else
138 return dest; 153 return dest;
154 IP_VS_DBG_BUF(6, "SH: selected unavailable "
155 "server %s:%d (offset %d), reselecting",
156 IP_VS_DBG_ADDR(svc->af, &dest->addr),
157 ntohs(dest->port), roffset);
139 } 158 }
140 159
141 return NULL; 160 return NULL;
diff --git a/net/netfilter/ipvs/ip_vs_sync.c b/net/netfilter/ipvs/ip_vs_sync.c
index f4484719f3e6..f63c2388f38d 100644
--- a/net/netfilter/ipvs/ip_vs_sync.c
+++ b/net/netfilter/ipvs/ip_vs_sync.c
@@ -1637,12 +1637,9 @@ static int sync_thread_master(void *data)
1637 continue; 1637 continue;
1638 } 1638 }
1639 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) { 1639 while (ip_vs_send_sync_msg(tinfo->sock, sb->mesg) < 0) {
1640 int ret = 0; 1640 int ret = __wait_event_interruptible(*sk_sleep(sk),
1641
1642 __wait_event_interruptible(*sk_sleep(sk),
1643 sock_writeable(sk) || 1641 sock_writeable(sk) ||
1644 kthread_should_stop(), 1642 kthread_should_stop());
1645 ret);
1646 if (unlikely(kthread_should_stop())) 1643 if (unlikely(kthread_should_stop()))
1647 goto done; 1644 goto done;
1648 } 1645 }
diff --git a/net/netfilter/nf_conntrack_acct.c b/net/netfilter/nf_conntrack_acct.c
index 2d3030ab5b61..a4b5e2a435ac 100644
--- a/net/netfilter/nf_conntrack_acct.c
+++ b/net/netfilter/nf_conntrack_acct.c
@@ -39,21 +39,23 @@ static struct ctl_table acct_sysctl_table[] = {
39unsigned int 39unsigned int
40seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir) 40seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
41{ 41{
42 struct nf_conn_counter *acct; 42 struct nf_conn_acct *acct;
43 struct nf_conn_counter *counter;
43 44
44 acct = nf_conn_acct_find(ct); 45 acct = nf_conn_acct_find(ct);
45 if (!acct) 46 if (!acct)
46 return 0; 47 return 0;
47 48
49 counter = acct->counter;
48 return seq_printf(s, "packets=%llu bytes=%llu ", 50 return seq_printf(s, "packets=%llu bytes=%llu ",
49 (unsigned long long)atomic64_read(&acct[dir].packets), 51 (unsigned long long)atomic64_read(&counter[dir].packets),
50 (unsigned long long)atomic64_read(&acct[dir].bytes)); 52 (unsigned long long)atomic64_read(&counter[dir].bytes));
51}; 53};
52EXPORT_SYMBOL_GPL(seq_print_acct); 54EXPORT_SYMBOL_GPL(seq_print_acct);
53 55
54static struct nf_ct_ext_type acct_extend __read_mostly = { 56static struct nf_ct_ext_type acct_extend __read_mostly = {
55 .len = sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]), 57 .len = sizeof(struct nf_conn_acct),
56 .align = __alignof__(struct nf_conn_counter[IP_CT_DIR_MAX]), 58 .align = __alignof__(struct nf_conn_acct),
57 .id = NF_CT_EXT_ACCT, 59 .id = NF_CT_EXT_ACCT,
58}; 60};
59 61
diff --git a/net/netfilter/nf_conntrack_core.c b/net/netfilter/nf_conntrack_core.c
index 5d892febd64c..43549eb7a7be 100644
--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -764,9 +764,10 @@ void nf_conntrack_free(struct nf_conn *ct)
764 struct net *net = nf_ct_net(ct); 764 struct net *net = nf_ct_net(ct);
765 765
766 nf_ct_ext_destroy(ct); 766 nf_ct_ext_destroy(ct);
767 atomic_dec(&net->ct.count);
768 nf_ct_ext_free(ct); 767 nf_ct_ext_free(ct);
769 kmem_cache_free(net->ct.nf_conntrack_cachep, ct); 768 kmem_cache_free(net->ct.nf_conntrack_cachep, ct);
769 smp_mb__before_atomic_dec();
770 atomic_dec(&net->ct.count);
770} 771}
771EXPORT_SYMBOL_GPL(nf_conntrack_free); 772EXPORT_SYMBOL_GPL(nf_conntrack_free);
772 773
@@ -1109,12 +1110,14 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,
1109 1110
1110acct: 1111acct:
1111 if (do_acct) { 1112 if (do_acct) {
1112 struct nf_conn_counter *acct; 1113 struct nf_conn_acct *acct;
1113 1114
1114 acct = nf_conn_acct_find(ct); 1115 acct = nf_conn_acct_find(ct);
1115 if (acct) { 1116 if (acct) {
1116 atomic64_inc(&acct[CTINFO2DIR(ctinfo)].packets); 1117 struct nf_conn_counter *counter = acct->counter;
1117 atomic64_add(skb->len, &acct[CTINFO2DIR(ctinfo)].bytes); 1118
1119 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1120 atomic64_add(skb->len, &counter[CTINFO2DIR(ctinfo)].bytes);
1118 } 1121 }
1119 } 1122 }
1120} 1123}
@@ -1126,13 +1129,15 @@ bool __nf_ct_kill_acct(struct nf_conn *ct,
1126 int do_acct) 1129 int do_acct)
1127{ 1130{
1128 if (do_acct) { 1131 if (do_acct) {
1129 struct nf_conn_counter *acct; 1132 struct nf_conn_acct *acct;
1130 1133
1131 acct = nf_conn_acct_find(ct); 1134 acct = nf_conn_acct_find(ct);
1132 if (acct) { 1135 if (acct) {
1133 atomic64_inc(&acct[CTINFO2DIR(ctinfo)].packets); 1136 struct nf_conn_counter *counter = acct->counter;
1137
1138 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1134 atomic64_add(skb->len - skb_network_offset(skb), 1139 atomic64_add(skb->len - skb_network_offset(skb),
1135 &acct[CTINFO2DIR(ctinfo)].bytes); 1140 &counter[CTINFO2DIR(ctinfo)].bytes);
1136 } 1141 }
1137 } 1142 }
1138 1143
diff --git a/net/netfilter/nf_conntrack_netlink.c b/net/netfilter/nf_conntrack_netlink.c
index eea936b70d15..08870b859046 100644
--- a/net/netfilter/nf_conntrack_netlink.c
+++ b/net/netfilter/nf_conntrack_netlink.c
@@ -211,13 +211,23 @@ nla_put_failure:
211} 211}
212 212
213static int 213static int
214dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes, 214dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
215 enum ip_conntrack_dir dir) 215 enum ip_conntrack_dir dir, int type)
216{ 216{
217 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; 217 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
218 struct nf_conn_counter *counter = acct->counter;
218 struct nlattr *nest_count; 219 struct nlattr *nest_count;
220 u64 pkts, bytes;
219 221
220 nest_count = nla_nest_start(skb, type | NLA_F_NESTED); 222 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
223 pkts = atomic64_xchg(&counter[dir].packets, 0);
224 bytes = atomic64_xchg(&counter[dir].bytes, 0);
225 } else {
226 pkts = atomic64_read(&counter[dir].packets);
227 bytes = atomic64_read(&counter[dir].bytes);
228 }
229
230 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
221 if (!nest_count) 231 if (!nest_count)
222 goto nla_put_failure; 232 goto nla_put_failure;
223 233
@@ -234,24 +244,19 @@ nla_put_failure:
234} 244}
235 245
236static int 246static int
237ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct, 247ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
238 enum ip_conntrack_dir dir, int type)
239{ 248{
240 struct nf_conn_counter *acct; 249 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
241 u64 pkts, bytes;
242 250
243 acct = nf_conn_acct_find(ct);
244 if (!acct) 251 if (!acct)
245 return 0; 252 return 0;
246 253
247 if (type == IPCTNL_MSG_CT_GET_CTRZERO) { 254 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
248 pkts = atomic64_xchg(&acct[dir].packets, 0); 255 return -1;
249 bytes = atomic64_xchg(&acct[dir].bytes, 0); 256 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
250 } else { 257 return -1;
251 pkts = atomic64_read(&acct[dir].packets); 258
252 bytes = atomic64_read(&acct[dir].bytes); 259 return 0;
253 }
254 return dump_counters(skb, pkts, bytes, dir);
255} 260}
256 261
257static int 262static int
@@ -488,8 +493,7 @@ ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
488 493
489 if (ctnetlink_dump_status(skb, ct) < 0 || 494 if (ctnetlink_dump_status(skb, ct) < 0 ||
490 ctnetlink_dump_timeout(skb, ct) < 0 || 495 ctnetlink_dump_timeout(skb, ct) < 0 ||
491 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 || 496 ctnetlink_dump_acct(skb, ct, type) < 0 ||
492 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
493 ctnetlink_dump_timestamp(skb, ct) < 0 || 497 ctnetlink_dump_timestamp(skb, ct) < 0 ||
494 ctnetlink_dump_protoinfo(skb, ct) < 0 || 498 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
495 ctnetlink_dump_helpinfo(skb, ct) < 0 || 499 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
@@ -530,7 +534,7 @@ ctnetlink_proto_size(const struct nf_conn *ct)
530} 534}
531 535
532static inline size_t 536static inline size_t
533ctnetlink_counters_size(const struct nf_conn *ct) 537ctnetlink_acct_size(const struct nf_conn *ct)
534{ 538{
535 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT)) 539 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
536 return 0; 540 return 0;
@@ -579,7 +583,7 @@ ctnetlink_nlmsg_size(const struct nf_conn *ct)
579 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */ 583 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
580 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */ 584 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
581 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */ 585 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
582 + ctnetlink_counters_size(ct) 586 + ctnetlink_acct_size(ct)
583 + ctnetlink_timestamp_size(ct) 587 + ctnetlink_timestamp_size(ct)
584 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */ 588 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
585 + nla_total_size(0) /* CTA_PROTOINFO */ 589 + nla_total_size(0) /* CTA_PROTOINFO */
@@ -673,10 +677,7 @@ ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
673 goto nla_put_failure; 677 goto nla_put_failure;
674 678
675 if (events & (1 << IPCT_DESTROY)) { 679 if (events & (1 << IPCT_DESTROY)) {
676 if (ctnetlink_dump_counters(skb, ct, 680 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
677 IP_CT_DIR_ORIGINAL, type) < 0 ||
678 ctnetlink_dump_counters(skb, ct,
679 IP_CT_DIR_REPLY, type) < 0 ||
680 ctnetlink_dump_timestamp(skb, ct) < 0) 681 ctnetlink_dump_timestamp(skb, ct) < 0)
681 goto nla_put_failure; 682 goto nla_put_failure;
682 } else { 683 } else {
diff --git a/net/netfilter/nf_conntrack_seqadj.c b/net/netfilter/nf_conntrack_seqadj.c
index 5f9bfd060dea..f6e2ae91a80b 100644
--- a/net/netfilter/nf_conntrack_seqadj.c
+++ b/net/netfilter/nf_conntrack_seqadj.c
@@ -36,13 +36,18 @@ int nf_ct_seqadj_set(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
36 if (off == 0) 36 if (off == 0)
37 return 0; 37 return 0;
38 38
39 if (unlikely(!seqadj)) {
40 WARN_ONCE(1, "Missing nfct_seqadj_ext_add() setup call\n");
41 return 0;
42 }
43
39 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status); 44 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
40 45
41 spin_lock_bh(&ct->lock); 46 spin_lock_bh(&ct->lock);
42 this_way = &seqadj->seq[dir]; 47 this_way = &seqadj->seq[dir];
43 if (this_way->offset_before == this_way->offset_after || 48 if (this_way->offset_before == this_way->offset_after ||
44 before(this_way->correction_pos, seq)) { 49 before(this_way->correction_pos, ntohl(seq))) {
45 this_way->correction_pos = seq; 50 this_way->correction_pos = ntohl(seq);
46 this_way->offset_before = this_way->offset_after; 51 this_way->offset_before = this_way->offset_after;
47 this_way->offset_after += off; 52 this_way->offset_after += off;
48 } 53 }
diff --git a/net/netfilter/nf_conntrack_sip.c b/net/netfilter/nf_conntrack_sip.c
index e0c4373b4747..466410eaa482 100644
--- a/net/netfilter/nf_conntrack_sip.c
+++ b/net/netfilter/nf_conntrack_sip.c
@@ -52,66 +52,8 @@ module_param(sip_direct_media, int, 0600);
52MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling " 52MODULE_PARM_DESC(sip_direct_media, "Expect Media streams between signalling "
53 "endpoints only (default 1)"); 53 "endpoints only (default 1)");
54 54
55unsigned int (*nf_nat_sip_hook)(struct sk_buff *skb, unsigned int protoff, 55const struct nf_nat_sip_hooks *nf_nat_sip_hooks;
56 unsigned int dataoff, const char **dptr, 56EXPORT_SYMBOL_GPL(nf_nat_sip_hooks);
57 unsigned int *datalen) __read_mostly;
58EXPORT_SYMBOL_GPL(nf_nat_sip_hook);
59
60void (*nf_nat_sip_seq_adjust_hook)(struct sk_buff *skb, unsigned int protoff,
61 s16 off) __read_mostly;
62EXPORT_SYMBOL_GPL(nf_nat_sip_seq_adjust_hook);
63
64unsigned int (*nf_nat_sip_expect_hook)(struct sk_buff *skb,
65 unsigned int protoff,
66 unsigned int dataoff,
67 const char **dptr,
68 unsigned int *datalen,
69 struct nf_conntrack_expect *exp,
70 unsigned int matchoff,
71 unsigned int matchlen) __read_mostly;
72EXPORT_SYMBOL_GPL(nf_nat_sip_expect_hook);
73
74unsigned int (*nf_nat_sdp_addr_hook)(struct sk_buff *skb, unsigned int protoff,
75 unsigned int dataoff,
76 const char **dptr,
77 unsigned int *datalen,
78 unsigned int sdpoff,
79 enum sdp_header_types type,
80 enum sdp_header_types term,
81 const union nf_inet_addr *addr)
82 __read_mostly;
83EXPORT_SYMBOL_GPL(nf_nat_sdp_addr_hook);
84
85unsigned int (*nf_nat_sdp_port_hook)(struct sk_buff *skb, unsigned int protoff,
86 unsigned int dataoff,
87 const char **dptr,
88 unsigned int *datalen,
89 unsigned int matchoff,
90 unsigned int matchlen,
91 u_int16_t port) __read_mostly;
92EXPORT_SYMBOL_GPL(nf_nat_sdp_port_hook);
93
94unsigned int (*nf_nat_sdp_session_hook)(struct sk_buff *skb,
95 unsigned int protoff,
96 unsigned int dataoff,
97 const char **dptr,
98 unsigned int *datalen,
99 unsigned int sdpoff,
100 const union nf_inet_addr *addr)
101 __read_mostly;
102EXPORT_SYMBOL_GPL(nf_nat_sdp_session_hook);
103
104unsigned int (*nf_nat_sdp_media_hook)(struct sk_buff *skb, unsigned int protoff,
105 unsigned int dataoff,
106 const char **dptr,
107 unsigned int *datalen,
108 struct nf_conntrack_expect *rtp_exp,
109 struct nf_conntrack_expect *rtcp_exp,
110 unsigned int mediaoff,
111 unsigned int medialen,
112 union nf_inet_addr *rtp_addr)
113 __read_mostly;
114EXPORT_SYMBOL_GPL(nf_nat_sdp_media_hook);
115 57
116static int string_len(const struct nf_conn *ct, const char *dptr, 58static int string_len(const struct nf_conn *ct, const char *dptr,
117 const char *limit, int *shift) 59 const char *limit, int *shift)
@@ -914,8 +856,7 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
914 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP; 856 int direct_rtp = 0, skip_expect = 0, ret = NF_DROP;
915 u_int16_t base_port; 857 u_int16_t base_port;
916 __be16 rtp_port, rtcp_port; 858 __be16 rtp_port, rtcp_port;
917 typeof(nf_nat_sdp_port_hook) nf_nat_sdp_port; 859 const struct nf_nat_sip_hooks *hooks;
918 typeof(nf_nat_sdp_media_hook) nf_nat_sdp_media;
919 860
920 saddr = NULL; 861 saddr = NULL;
921 if (sip_direct_media) { 862 if (sip_direct_media) {
@@ -966,22 +907,23 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
966#endif 907#endif
967 skip_expect = 1; 908 skip_expect = 1;
968 } while (!skip_expect); 909 } while (!skip_expect);
969 rcu_read_unlock();
970 910
971 base_port = ntohs(tuple.dst.u.udp.port) & ~1; 911 base_port = ntohs(tuple.dst.u.udp.port) & ~1;
972 rtp_port = htons(base_port); 912 rtp_port = htons(base_port);
973 rtcp_port = htons(base_port + 1); 913 rtcp_port = htons(base_port + 1);
974 914
975 if (direct_rtp) { 915 if (direct_rtp) {
976 nf_nat_sdp_port = rcu_dereference(nf_nat_sdp_port_hook); 916 hooks = rcu_dereference(nf_nat_sip_hooks);
977 if (nf_nat_sdp_port && 917 if (hooks &&
978 !nf_nat_sdp_port(skb, protoff, dataoff, dptr, datalen, 918 !hooks->sdp_port(skb, protoff, dataoff, dptr, datalen,
979 mediaoff, medialen, ntohs(rtp_port))) 919 mediaoff, medialen, ntohs(rtp_port)))
980 goto err1; 920 goto err1;
981 } 921 }
982 922
983 if (skip_expect) 923 if (skip_expect) {
924 rcu_read_unlock();
984 return NF_ACCEPT; 925 return NF_ACCEPT;
926 }
985 927
986 rtp_exp = nf_ct_expect_alloc(ct); 928 rtp_exp = nf_ct_expect_alloc(ct);
987 if (rtp_exp == NULL) 929 if (rtp_exp == NULL)
@@ -995,10 +937,10 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
995 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr, 937 nf_ct_expect_init(rtcp_exp, class, nf_ct_l3num(ct), saddr, daddr,
996 IPPROTO_UDP, NULL, &rtcp_port); 938 IPPROTO_UDP, NULL, &rtcp_port);
997 939
998 nf_nat_sdp_media = rcu_dereference(nf_nat_sdp_media_hook); 940 hooks = rcu_dereference(nf_nat_sip_hooks);
999 if (nf_nat_sdp_media && ct->status & IPS_NAT_MASK && !direct_rtp) 941 if (hooks && ct->status & IPS_NAT_MASK && !direct_rtp)
1000 ret = nf_nat_sdp_media(skb, protoff, dataoff, dptr, datalen, 942 ret = hooks->sdp_media(skb, protoff, dataoff, dptr,
1001 rtp_exp, rtcp_exp, 943 datalen, rtp_exp, rtcp_exp,
1002 mediaoff, medialen, daddr); 944 mediaoff, medialen, daddr);
1003 else { 945 else {
1004 if (nf_ct_expect_related(rtp_exp) == 0) { 946 if (nf_ct_expect_related(rtp_exp) == 0) {
@@ -1012,6 +954,7 @@ static int set_expected_rtp_rtcp(struct sk_buff *skb, unsigned int protoff,
1012err2: 954err2:
1013 nf_ct_expect_put(rtp_exp); 955 nf_ct_expect_put(rtp_exp);
1014err1: 956err1:
957 rcu_read_unlock();
1015 return ret; 958 return ret;
1016} 959}
1017 960
@@ -1051,13 +994,12 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1051 unsigned int caddr_len, maddr_len; 994 unsigned int caddr_len, maddr_len;
1052 unsigned int i; 995 unsigned int i;
1053 union nf_inet_addr caddr, maddr, rtp_addr; 996 union nf_inet_addr caddr, maddr, rtp_addr;
997 const struct nf_nat_sip_hooks *hooks;
1054 unsigned int port; 998 unsigned int port;
1055 const struct sdp_media_type *t; 999 const struct sdp_media_type *t;
1056 int ret = NF_ACCEPT; 1000 int ret = NF_ACCEPT;
1057 typeof(nf_nat_sdp_addr_hook) nf_nat_sdp_addr;
1058 typeof(nf_nat_sdp_session_hook) nf_nat_sdp_session;
1059 1001
1060 nf_nat_sdp_addr = rcu_dereference(nf_nat_sdp_addr_hook); 1002 hooks = rcu_dereference(nf_nat_sip_hooks);
1061 1003
1062 /* Find beginning of session description */ 1004 /* Find beginning of session description */
1063 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen, 1005 if (ct_sip_get_sdp_header(ct, *dptr, 0, *datalen,
@@ -1125,10 +1067,11 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1125 } 1067 }
1126 1068
1127 /* Update media connection address if present */ 1069 /* Update media connection address if present */
1128 if (maddr_len && nf_nat_sdp_addr && ct->status & IPS_NAT_MASK) { 1070 if (maddr_len && hooks && ct->status & IPS_NAT_MASK) {
1129 ret = nf_nat_sdp_addr(skb, protoff, dataoff, 1071 ret = hooks->sdp_addr(skb, protoff, dataoff,
1130 dptr, datalen, mediaoff, 1072 dptr, datalen, mediaoff,
1131 SDP_HDR_CONNECTION, SDP_HDR_MEDIA, 1073 SDP_HDR_CONNECTION,
1074 SDP_HDR_MEDIA,
1132 &rtp_addr); 1075 &rtp_addr);
1133 if (ret != NF_ACCEPT) { 1076 if (ret != NF_ACCEPT) {
1134 nf_ct_helper_log(skb, ct, "cannot mangle SDP"); 1077 nf_ct_helper_log(skb, ct, "cannot mangle SDP");
@@ -1139,10 +1082,11 @@ static int process_sdp(struct sk_buff *skb, unsigned int protoff,
1139 } 1082 }
1140 1083
1141 /* Update session connection and owner addresses */ 1084 /* Update session connection and owner addresses */
1142 nf_nat_sdp_session = rcu_dereference(nf_nat_sdp_session_hook); 1085 hooks = rcu_dereference(nf_nat_sip_hooks);
1143 if (nf_nat_sdp_session && ct->status & IPS_NAT_MASK) 1086 if (hooks && ct->status & IPS_NAT_MASK)
1144 ret = nf_nat_sdp_session(skb, protoff, dataoff, 1087 ret = hooks->sdp_session(skb, protoff, dataoff,
1145 dptr, datalen, sdpoff, &rtp_addr); 1088 dptr, datalen, sdpoff,
1089 &rtp_addr);
1146 1090
1147 return ret; 1091 return ret;
1148} 1092}
@@ -1242,11 +1186,11 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1242 unsigned int matchoff, matchlen; 1186 unsigned int matchoff, matchlen;
1243 struct nf_conntrack_expect *exp; 1187 struct nf_conntrack_expect *exp;
1244 union nf_inet_addr *saddr, daddr; 1188 union nf_inet_addr *saddr, daddr;
1189 const struct nf_nat_sip_hooks *hooks;
1245 __be16 port; 1190 __be16 port;
1246 u8 proto; 1191 u8 proto;
1247 unsigned int expires = 0; 1192 unsigned int expires = 0;
1248 int ret; 1193 int ret;
1249 typeof(nf_nat_sip_expect_hook) nf_nat_sip_expect;
1250 1194
1251 /* Expected connections can not register again. */ 1195 /* Expected connections can not register again. */
1252 if (ct->status & IPS_EXPECTED) 1196 if (ct->status & IPS_EXPECTED)
@@ -1309,10 +1253,10 @@ static int process_register_request(struct sk_buff *skb, unsigned int protoff,
1309 exp->helper = nfct_help(ct)->helper; 1253 exp->helper = nfct_help(ct)->helper;
1310 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE; 1254 exp->flags = NF_CT_EXPECT_PERMANENT | NF_CT_EXPECT_INACTIVE;
1311 1255
1312 nf_nat_sip_expect = rcu_dereference(nf_nat_sip_expect_hook); 1256 hooks = rcu_dereference(nf_nat_sip_hooks);
1313 if (nf_nat_sip_expect && ct->status & IPS_NAT_MASK) 1257 if (hooks && ct->status & IPS_NAT_MASK)
1314 ret = nf_nat_sip_expect(skb, protoff, dataoff, dptr, datalen, 1258 ret = hooks->expect(skb, protoff, dataoff, dptr, datalen,
1315 exp, matchoff, matchlen); 1259 exp, matchoff, matchlen);
1316 else { 1260 else {
1317 if (nf_ct_expect_related(exp) != 0) { 1261 if (nf_ct_expect_related(exp) != 0) {
1318 nf_ct_helper_log(skb, ct, "cannot add expectation"); 1262 nf_ct_helper_log(skb, ct, "cannot add expectation");
@@ -1515,7 +1459,7 @@ static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1515 unsigned int protoff, unsigned int dataoff, 1459 unsigned int protoff, unsigned int dataoff,
1516 const char **dptr, unsigned int *datalen) 1460 const char **dptr, unsigned int *datalen)
1517{ 1461{
1518 typeof(nf_nat_sip_hook) nf_nat_sip; 1462 const struct nf_nat_sip_hooks *hooks;
1519 int ret; 1463 int ret;
1520 1464
1521 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0) 1465 if (strnicmp(*dptr, "SIP/2.0 ", strlen("SIP/2.0 ")) != 0)
@@ -1524,9 +1468,9 @@ static int process_sip_msg(struct sk_buff *skb, struct nf_conn *ct,
1524 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen); 1468 ret = process_sip_response(skb, protoff, dataoff, dptr, datalen);
1525 1469
1526 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) { 1470 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1527 nf_nat_sip = rcu_dereference(nf_nat_sip_hook); 1471 hooks = rcu_dereference(nf_nat_sip_hooks);
1528 if (nf_nat_sip && !nf_nat_sip(skb, protoff, dataoff, 1472 if (hooks && !hooks->msg(skb, protoff, dataoff,
1529 dptr, datalen)) { 1473 dptr, datalen)) {
1530 nf_ct_helper_log(skb, ct, "cannot NAT SIP message"); 1474 nf_ct_helper_log(skb, ct, "cannot NAT SIP message");
1531 ret = NF_DROP; 1475 ret = NF_DROP;
1532 } 1476 }
@@ -1546,7 +1490,6 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1546 s16 diff, tdiff = 0; 1490 s16 diff, tdiff = 0;
1547 int ret = NF_ACCEPT; 1491 int ret = NF_ACCEPT;
1548 bool term; 1492 bool term;
1549 typeof(nf_nat_sip_seq_adjust_hook) nf_nat_sip_seq_adjust;
1550 1493
1551 if (ctinfo != IP_CT_ESTABLISHED && 1494 if (ctinfo != IP_CT_ESTABLISHED &&
1552 ctinfo != IP_CT_ESTABLISHED_REPLY) 1495 ctinfo != IP_CT_ESTABLISHED_REPLY)
@@ -1610,9 +1553,11 @@ static int sip_help_tcp(struct sk_buff *skb, unsigned int protoff,
1610 } 1553 }
1611 1554
1612 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) { 1555 if (ret == NF_ACCEPT && ct->status & IPS_NAT_MASK) {
1613 nf_nat_sip_seq_adjust = rcu_dereference(nf_nat_sip_seq_adjust_hook); 1556 const struct nf_nat_sip_hooks *hooks;
1614 if (nf_nat_sip_seq_adjust) 1557
1615 nf_nat_sip_seq_adjust(skb, protoff, tdiff); 1558 hooks = rcu_dereference(nf_nat_sip_hooks);
1559 if (hooks)
1560 hooks->seq_adjust(skb, protoff, tdiff);
1616 } 1561 }
1617 1562
1618 return ret; 1563 return ret;
diff --git a/net/netfilter/nf_conntrack_timestamp.c b/net/netfilter/nf_conntrack_timestamp.c
index 902fb0a6b38a..7a394df0deb7 100644
--- a/net/netfilter/nf_conntrack_timestamp.c
+++ b/net/netfilter/nf_conntrack_timestamp.c
@@ -97,7 +97,6 @@ int nf_conntrack_tstamp_pernet_init(struct net *net)
97void nf_conntrack_tstamp_pernet_fini(struct net *net) 97void nf_conntrack_tstamp_pernet_fini(struct net *net)
98{ 98{
99 nf_conntrack_tstamp_fini_sysctl(net); 99 nf_conntrack_tstamp_fini_sysctl(net);
100 nf_ct_extend_unregister(&tstamp_extend);
101} 100}
102 101
103int nf_conntrack_tstamp_init(void) 102int nf_conntrack_tstamp_init(void)
diff --git a/net/netfilter/nf_internals.h b/net/netfilter/nf_internals.h
index 3deec997be89..61a3c927e63c 100644
--- a/net/netfilter/nf_internals.h
+++ b/net/netfilter/nf_internals.h
@@ -13,26 +13,20 @@
13 13
14 14
15/* core.c */ 15/* core.c */
16extern unsigned int nf_iterate(struct list_head *head, 16unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb,
17 struct sk_buff *skb, 17 unsigned int hook, const struct net_device *indev,
18 unsigned int hook, 18 const struct net_device *outdev,
19 const struct net_device *indev, 19 struct nf_hook_ops **elemp,
20 const struct net_device *outdev, 20 int (*okfn)(struct sk_buff *), int hook_thresh);
21 struct nf_hook_ops **elemp,
22 int (*okfn)(struct sk_buff *),
23 int hook_thresh);
24 21
25/* nf_queue.c */ 22/* nf_queue.c */
26extern int nf_queue(struct sk_buff *skb, 23int nf_queue(struct sk_buff *skb, struct nf_hook_ops *elem, u_int8_t pf,
27 struct nf_hook_ops *elem, 24 unsigned int hook, struct net_device *indev,
28 u_int8_t pf, unsigned int hook, 25 struct net_device *outdev, int (*okfn)(struct sk_buff *),
29 struct net_device *indev, 26 unsigned int queuenum);
30 struct net_device *outdev, 27int __init netfilter_queue_init(void);
31 int (*okfn)(struct sk_buff *),
32 unsigned int queuenum);
33extern int __init netfilter_queue_init(void);
34 28
35/* nf_log.c */ 29/* nf_log.c */
36extern int __init netfilter_log_init(void); 30int __init netfilter_log_init(void);
37 31
38#endif 32#endif
diff --git a/net/netfilter/nf_nat_core.c b/net/netfilter/nf_nat_core.c
index 6f0f4f7f68a5..63a815402211 100644
--- a/net/netfilter/nf_nat_core.c
+++ b/net/netfilter/nf_nat_core.c
@@ -432,6 +432,26 @@ nf_nat_setup_info(struct nf_conn *ct,
432} 432}
433EXPORT_SYMBOL(nf_nat_setup_info); 433EXPORT_SYMBOL(nf_nat_setup_info);
434 434
435unsigned int
436nf_nat_alloc_null_binding(struct nf_conn *ct, unsigned int hooknum)
437{
438 /* Force range to this IP; let proto decide mapping for
439 * per-proto parts (hence not IP_NAT_RANGE_PROTO_SPECIFIED).
440 * Use reply in case it's already been mangled (eg local packet).
441 */
442 union nf_inet_addr ip =
443 (HOOK2MANIP(hooknum) == NF_NAT_MANIP_SRC ?
444 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3 :
445 ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3);
446 struct nf_nat_range range = {
447 .flags = NF_NAT_RANGE_MAP_IPS,
448 .min_addr = ip,
449 .max_addr = ip,
450 };
451 return nf_nat_setup_info(ct, &range, HOOK2MANIP(hooknum));
452}
453EXPORT_SYMBOL_GPL(nf_nat_alloc_null_binding);
454
435/* Do packet manipulations according to nf_nat_setup_info. */ 455/* Do packet manipulations according to nf_nat_setup_info. */
436unsigned int nf_nat_packet(struct nf_conn *ct, 456unsigned int nf_nat_packet(struct nf_conn *ct,
437 enum ip_conntrack_info ctinfo, 457 enum ip_conntrack_info ctinfo,
diff --git a/net/netfilter/nf_nat_irc.c b/net/netfilter/nf_nat_irc.c
index f02b3605823e..1fb2258c3535 100644
--- a/net/netfilter/nf_nat_irc.c
+++ b/net/netfilter/nf_nat_irc.c
@@ -34,10 +34,14 @@ static unsigned int help(struct sk_buff *skb,
34 struct nf_conntrack_expect *exp) 34 struct nf_conntrack_expect *exp)
35{ 35{
36 char buffer[sizeof("4294967296 65635")]; 36 char buffer[sizeof("4294967296 65635")];
37 struct nf_conn *ct = exp->master;
38 union nf_inet_addr newaddr;
37 u_int16_t port; 39 u_int16_t port;
38 unsigned int ret; 40 unsigned int ret;
39 41
40 /* Reply comes from server. */ 42 /* Reply comes from server. */
43 newaddr = ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3;
44
41 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port; 45 exp->saved_proto.tcp.port = exp->tuple.dst.u.tcp.port;
42 exp->dir = IP_CT_DIR_REPLY; 46 exp->dir = IP_CT_DIR_REPLY;
43 exp->expectfn = nf_nat_follow_master; 47 exp->expectfn = nf_nat_follow_master;
@@ -57,17 +61,35 @@ static unsigned int help(struct sk_buff *skb,
57 } 61 }
58 62
59 if (port == 0) { 63 if (port == 0) {
60 nf_ct_helper_log(skb, exp->master, "all ports in use"); 64 nf_ct_helper_log(skb, ct, "all ports in use");
61 return NF_DROP; 65 return NF_DROP;
62 } 66 }
63 67
64 ret = nf_nat_mangle_tcp_packet(skb, exp->master, ctinfo, 68 /* strlen("\1DCC CHAT chat AAAAAAAA P\1\n")=27
65 protoff, matchoff, matchlen, buffer, 69 * strlen("\1DCC SCHAT chat AAAAAAAA P\1\n")=28
66 strlen(buffer)); 70 * strlen("\1DCC SEND F AAAAAAAA P S\1\n")=26
71 * strlen("\1DCC MOVE F AAAAAAAA P S\1\n")=26
72 * strlen("\1DCC TSEND F AAAAAAAA P S\1\n")=27
73 *
74 * AAAAAAAAA: bound addr (1.0.0.0==16777216, min 8 digits,
75 * 255.255.255.255==4294967296, 10 digits)
76 * P: bound port (min 1 d, max 5d (65635))
77 * F: filename (min 1 d )
78 * S: size (min 1 d )
79 * 0x01, \n: terminators
80 */
81 /* AAA = "us", ie. where server normally talks to. */
82 snprintf(buffer, sizeof(buffer), "%u %u", ntohl(newaddr.ip), port);
83 pr_debug("nf_nat_irc: inserting '%s' == %pI4, port %u\n",
84 buffer, &newaddr.ip, port);
85
86 ret = nf_nat_mangle_tcp_packet(skb, ct, ctinfo, protoff, matchoff,
87 matchlen, buffer, strlen(buffer));
67 if (ret != NF_ACCEPT) { 88 if (ret != NF_ACCEPT) {
68 nf_ct_helper_log(skb, exp->master, "cannot mangle packet"); 89 nf_ct_helper_log(skb, ct, "cannot mangle packet");
69 nf_ct_unexpect_related(exp); 90 nf_ct_unexpect_related(exp);
70 } 91 }
92
71 return ret; 93 return ret;
72} 94}
73 95
diff --git a/net/netfilter/nf_nat_sip.c b/net/netfilter/nf_nat_sip.c
index f9790405b7ff..b4d691db955e 100644
--- a/net/netfilter/nf_nat_sip.c
+++ b/net/netfilter/nf_nat_sip.c
@@ -625,33 +625,26 @@ static struct nf_ct_helper_expectfn sip_nat = {
625 625
626static void __exit nf_nat_sip_fini(void) 626static void __exit nf_nat_sip_fini(void)
627{ 627{
628 RCU_INIT_POINTER(nf_nat_sip_hook, NULL); 628 RCU_INIT_POINTER(nf_nat_sip_hooks, NULL);
629 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, NULL); 629
630 RCU_INIT_POINTER(nf_nat_sip_expect_hook, NULL);
631 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, NULL);
632 RCU_INIT_POINTER(nf_nat_sdp_port_hook, NULL);
633 RCU_INIT_POINTER(nf_nat_sdp_session_hook, NULL);
634 RCU_INIT_POINTER(nf_nat_sdp_media_hook, NULL);
635 nf_ct_helper_expectfn_unregister(&sip_nat); 630 nf_ct_helper_expectfn_unregister(&sip_nat);
636 synchronize_rcu(); 631 synchronize_rcu();
637} 632}
638 633
634static const struct nf_nat_sip_hooks sip_hooks = {
635 .msg = nf_nat_sip,
636 .seq_adjust = nf_nat_sip_seq_adjust,
637 .expect = nf_nat_sip_expect,
638 .sdp_addr = nf_nat_sdp_addr,
639 .sdp_port = nf_nat_sdp_port,
640 .sdp_session = nf_nat_sdp_session,
641 .sdp_media = nf_nat_sdp_media,
642};
643
639static int __init nf_nat_sip_init(void) 644static int __init nf_nat_sip_init(void)
640{ 645{
641 BUG_ON(nf_nat_sip_hook != NULL); 646 BUG_ON(nf_nat_sip_hooks != NULL);
642 BUG_ON(nf_nat_sip_seq_adjust_hook != NULL); 647 RCU_INIT_POINTER(nf_nat_sip_hooks, &sip_hooks);
643 BUG_ON(nf_nat_sip_expect_hook != NULL);
644 BUG_ON(nf_nat_sdp_addr_hook != NULL);
645 BUG_ON(nf_nat_sdp_port_hook != NULL);
646 BUG_ON(nf_nat_sdp_session_hook != NULL);
647 BUG_ON(nf_nat_sdp_media_hook != NULL);
648 RCU_INIT_POINTER(nf_nat_sip_hook, nf_nat_sip);
649 RCU_INIT_POINTER(nf_nat_sip_seq_adjust_hook, nf_nat_sip_seq_adjust);
650 RCU_INIT_POINTER(nf_nat_sip_expect_hook, nf_nat_sip_expect);
651 RCU_INIT_POINTER(nf_nat_sdp_addr_hook, nf_nat_sdp_addr);
652 RCU_INIT_POINTER(nf_nat_sdp_port_hook, nf_nat_sdp_port);
653 RCU_INIT_POINTER(nf_nat_sdp_session_hook, nf_nat_sdp_session);
654 RCU_INIT_POINTER(nf_nat_sdp_media_hook, nf_nat_sdp_media);
655 nf_ct_helper_expectfn_register(&sip_nat); 648 nf_ct_helper_expectfn_register(&sip_nat);
656 return 0; 649 return 0;
657} 650}
diff --git a/net/netfilter/nf_synproxy_core.c b/net/netfilter/nf_synproxy_core.c
index cdf4567ba9b3..9858e3e51a3a 100644
--- a/net/netfilter/nf_synproxy_core.c
+++ b/net/netfilter/nf_synproxy_core.c
@@ -151,9 +151,10 @@ void synproxy_init_timestamp_cookie(const struct xt_synproxy_info *info,
151 opts->tsecr = opts->tsval; 151 opts->tsecr = opts->tsval;
152 opts->tsval = tcp_time_stamp & ~0x3f; 152 opts->tsval = tcp_time_stamp & ~0x3f;
153 153
154 if (opts->options & XT_SYNPROXY_OPT_WSCALE) 154 if (opts->options & XT_SYNPROXY_OPT_WSCALE) {
155 opts->tsval |= info->wscale; 155 opts->tsval |= opts->wscale;
156 else 156 opts->wscale = info->wscale;
157 } else
157 opts->tsval |= 0xf; 158 opts->tsval |= 0xf;
158 159
159 if (opts->options & XT_SYNPROXY_OPT_SACK_PERM) 160 if (opts->options & XT_SYNPROXY_OPT_SACK_PERM)
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
new file mode 100644
index 000000000000..71a9f49a768b
--- /dev/null
+++ b/net/netfilter/nf_tables_api.c
@@ -0,0 +1,3309 @@
1/*
2 * Copyright (c) 2007-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/skbuff.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables_core.h>
20#include <net/netfilter/nf_tables.h>
21#include <net/net_namespace.h>
22#include <net/sock.h>
23
24static LIST_HEAD(nf_tables_expressions);
25
26/**
27 * nft_register_afinfo - register nf_tables address family info
28 *
29 * @afi: address family info to register
30 *
31 * Register the address family for use with nf_tables. Returns zero on
32 * success or a negative errno code otherwise.
33 */
34int nft_register_afinfo(struct net *net, struct nft_af_info *afi)
35{
36 INIT_LIST_HEAD(&afi->tables);
37 nfnl_lock(NFNL_SUBSYS_NFTABLES);
38 list_add_tail(&afi->list, &net->nft.af_info);
39 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
40 return 0;
41}
42EXPORT_SYMBOL_GPL(nft_register_afinfo);
43
44/**
45 * nft_unregister_afinfo - unregister nf_tables address family info
46 *
47 * @afi: address family info to unregister
48 *
49 * Unregister the address family for use with nf_tables.
50 */
51void nft_unregister_afinfo(struct nft_af_info *afi)
52{
53 nfnl_lock(NFNL_SUBSYS_NFTABLES);
54 list_del(&afi->list);
55 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
56}
57EXPORT_SYMBOL_GPL(nft_unregister_afinfo);
58
59static struct nft_af_info *nft_afinfo_lookup(struct net *net, int family)
60{
61 struct nft_af_info *afi;
62
63 list_for_each_entry(afi, &net->nft.af_info, list) {
64 if (afi->family == family)
65 return afi;
66 }
67 return NULL;
68}
69
70static struct nft_af_info *
71nf_tables_afinfo_lookup(struct net *net, int family, bool autoload)
72{
73 struct nft_af_info *afi;
74
75 afi = nft_afinfo_lookup(net, family);
76 if (afi != NULL)
77 return afi;
78#ifdef CONFIG_MODULES
79 if (autoload) {
80 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
81 request_module("nft-afinfo-%u", family);
82 nfnl_lock(NFNL_SUBSYS_NFTABLES);
83 afi = nft_afinfo_lookup(net, family);
84 if (afi != NULL)
85 return ERR_PTR(-EAGAIN);
86 }
87#endif
88 return ERR_PTR(-EAFNOSUPPORT);
89}
90
91/*
92 * Tables
93 */
94
95static struct nft_table *nft_table_lookup(const struct nft_af_info *afi,
96 const struct nlattr *nla)
97{
98 struct nft_table *table;
99
100 list_for_each_entry(table, &afi->tables, list) {
101 if (!nla_strcmp(nla, table->name))
102 return table;
103 }
104 return NULL;
105}
106
107static struct nft_table *nf_tables_table_lookup(const struct nft_af_info *afi,
108 const struct nlattr *nla)
109{
110 struct nft_table *table;
111
112 if (nla == NULL)
113 return ERR_PTR(-EINVAL);
114
115 table = nft_table_lookup(afi, nla);
116 if (table != NULL)
117 return table;
118
119 return ERR_PTR(-ENOENT);
120}
121
122static inline u64 nf_tables_alloc_handle(struct nft_table *table)
123{
124 return ++table->hgenerator;
125}
126
127static struct nf_chain_type *chain_type[AF_MAX][NFT_CHAIN_T_MAX];
128
129static int __nf_tables_chain_type_lookup(int family, const struct nlattr *nla)
130{
131 int i;
132
133 for (i=0; i<NFT_CHAIN_T_MAX; i++) {
134 if (chain_type[family][i] != NULL &&
135 !nla_strcmp(nla, chain_type[family][i]->name))
136 return i;
137 }
138 return -1;
139}
140
141static int nf_tables_chain_type_lookup(const struct nft_af_info *afi,
142 const struct nlattr *nla,
143 bool autoload)
144{
145 int type;
146
147 type = __nf_tables_chain_type_lookup(afi->family, nla);
148#ifdef CONFIG_MODULES
149 if (type < 0 && autoload) {
150 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
151 request_module("nft-chain-%u-%*.s", afi->family,
152 nla_len(nla)-1, (const char *)nla_data(nla));
153 nfnl_lock(NFNL_SUBSYS_NFTABLES);
154 type = __nf_tables_chain_type_lookup(afi->family, nla);
155 }
156#endif
157 return type;
158}
159
160static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
161 [NFTA_TABLE_NAME] = { .type = NLA_STRING },
162 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
163};
164
165static int nf_tables_fill_table_info(struct sk_buff *skb, u32 portid, u32 seq,
166 int event, u32 flags, int family,
167 const struct nft_table *table)
168{
169 struct nlmsghdr *nlh;
170 struct nfgenmsg *nfmsg;
171
172 event |= NFNL_SUBSYS_NFTABLES << 8;
173 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
174 if (nlh == NULL)
175 goto nla_put_failure;
176
177 nfmsg = nlmsg_data(nlh);
178 nfmsg->nfgen_family = family;
179 nfmsg->version = NFNETLINK_V0;
180 nfmsg->res_id = 0;
181
182 if (nla_put_string(skb, NFTA_TABLE_NAME, table->name) ||
183 nla_put_be32(skb, NFTA_TABLE_FLAGS, htonl(table->flags)))
184 goto nla_put_failure;
185
186 return nlmsg_end(skb, nlh);
187
188nla_put_failure:
189 nlmsg_trim(skb, nlh);
190 return -1;
191}
192
193static int nf_tables_table_notify(const struct sk_buff *oskb,
194 const struct nlmsghdr *nlh,
195 const struct nft_table *table,
196 int event, int family)
197{
198 struct sk_buff *skb;
199 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
200 u32 seq = nlh ? nlh->nlmsg_seq : 0;
201 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
202 bool report;
203 int err;
204
205 report = nlh ? nlmsg_report(nlh) : false;
206 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
207 return 0;
208
209 err = -ENOBUFS;
210 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
211 if (skb == NULL)
212 goto err;
213
214 err = nf_tables_fill_table_info(skb, portid, seq, event, 0,
215 family, table);
216 if (err < 0) {
217 kfree_skb(skb);
218 goto err;
219 }
220
221 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
222 GFP_KERNEL);
223err:
224 if (err < 0)
225 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
226 return err;
227}
228
229static int nf_tables_dump_tables(struct sk_buff *skb,
230 struct netlink_callback *cb)
231{
232 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
233 const struct nft_af_info *afi;
234 const struct nft_table *table;
235 unsigned int idx = 0, s_idx = cb->args[0];
236 struct net *net = sock_net(skb->sk);
237 int family = nfmsg->nfgen_family;
238
239 list_for_each_entry(afi, &net->nft.af_info, list) {
240 if (family != NFPROTO_UNSPEC && family != afi->family)
241 continue;
242
243 list_for_each_entry(table, &afi->tables, list) {
244 if (idx < s_idx)
245 goto cont;
246 if (idx > s_idx)
247 memset(&cb->args[1], 0,
248 sizeof(cb->args) - sizeof(cb->args[0]));
249 if (nf_tables_fill_table_info(skb,
250 NETLINK_CB(cb->skb).portid,
251 cb->nlh->nlmsg_seq,
252 NFT_MSG_NEWTABLE,
253 NLM_F_MULTI,
254 afi->family, table) < 0)
255 goto done;
256cont:
257 idx++;
258 }
259 }
260done:
261 cb->args[0] = idx;
262 return skb->len;
263}
264
265static int nf_tables_gettable(struct sock *nlsk, struct sk_buff *skb,
266 const struct nlmsghdr *nlh,
267 const struct nlattr * const nla[])
268{
269 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
270 const struct nft_af_info *afi;
271 const struct nft_table *table;
272 struct sk_buff *skb2;
273 struct net *net = sock_net(skb->sk);
274 int family = nfmsg->nfgen_family;
275 int err;
276
277 if (nlh->nlmsg_flags & NLM_F_DUMP) {
278 struct netlink_dump_control c = {
279 .dump = nf_tables_dump_tables,
280 };
281 return netlink_dump_start(nlsk, skb, nlh, &c);
282 }
283
284 afi = nf_tables_afinfo_lookup(net, family, false);
285 if (IS_ERR(afi))
286 return PTR_ERR(afi);
287
288 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
289 if (IS_ERR(table))
290 return PTR_ERR(table);
291
292 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
293 if (!skb2)
294 return -ENOMEM;
295
296 err = nf_tables_fill_table_info(skb2, NETLINK_CB(skb).portid,
297 nlh->nlmsg_seq, NFT_MSG_NEWTABLE, 0,
298 family, table);
299 if (err < 0)
300 goto err;
301
302 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
303
304err:
305 kfree_skb(skb2);
306 return err;
307}
308
309static int nf_tables_table_enable(struct nft_table *table)
310{
311 struct nft_chain *chain;
312 int err, i = 0;
313
314 list_for_each_entry(chain, &table->chains, list) {
315 if (!(chain->flags & NFT_BASE_CHAIN))
316 continue;
317
318 err = nf_register_hook(&nft_base_chain(chain)->ops);
319 if (err < 0)
320 goto err;
321
322 i++;
323 }
324 return 0;
325err:
326 list_for_each_entry(chain, &table->chains, list) {
327 if (!(chain->flags & NFT_BASE_CHAIN))
328 continue;
329
330 if (i-- <= 0)
331 break;
332
333 nf_unregister_hook(&nft_base_chain(chain)->ops);
334 }
335 return err;
336}
337
338static int nf_tables_table_disable(struct nft_table *table)
339{
340 struct nft_chain *chain;
341
342 list_for_each_entry(chain, &table->chains, list) {
343 if (chain->flags & NFT_BASE_CHAIN)
344 nf_unregister_hook(&nft_base_chain(chain)->ops);
345 }
346
347 return 0;
348}
349
350static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
351 const struct nlmsghdr *nlh,
352 const struct nlattr * const nla[],
353 struct nft_af_info *afi, struct nft_table *table)
354{
355 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
356 int family = nfmsg->nfgen_family, ret = 0;
357
358 if (nla[NFTA_TABLE_FLAGS]) {
359 __be32 flags;
360
361 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
362 if (flags & ~NFT_TABLE_F_DORMANT)
363 return -EINVAL;
364
365 if ((flags & NFT_TABLE_F_DORMANT) &&
366 !(table->flags & NFT_TABLE_F_DORMANT)) {
367 ret = nf_tables_table_disable(table);
368 if (ret >= 0)
369 table->flags |= NFT_TABLE_F_DORMANT;
370 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
371 table->flags & NFT_TABLE_F_DORMANT) {
372 ret = nf_tables_table_enable(table);
373 if (ret >= 0)
374 table->flags &= ~NFT_TABLE_F_DORMANT;
375 }
376 if (ret < 0)
377 goto err;
378 }
379
380 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
381err:
382 return ret;
383}
384
385static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
386 const struct nlmsghdr *nlh,
387 const struct nlattr * const nla[])
388{
389 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
390 const struct nlattr *name;
391 struct nft_af_info *afi;
392 struct nft_table *table;
393 struct net *net = sock_net(skb->sk);
394 int family = nfmsg->nfgen_family;
395
396 afi = nf_tables_afinfo_lookup(net, family, true);
397 if (IS_ERR(afi))
398 return PTR_ERR(afi);
399
400 name = nla[NFTA_TABLE_NAME];
401 table = nf_tables_table_lookup(afi, name);
402 if (IS_ERR(table)) {
403 if (PTR_ERR(table) != -ENOENT)
404 return PTR_ERR(table);
405 table = NULL;
406 }
407
408 if (table != NULL) {
409 if (nlh->nlmsg_flags & NLM_F_EXCL)
410 return -EEXIST;
411 if (nlh->nlmsg_flags & NLM_F_REPLACE)
412 return -EOPNOTSUPP;
413 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
414 }
415
416 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
417 if (table == NULL)
418 return -ENOMEM;
419
420 nla_strlcpy(table->name, name, nla_len(name));
421 INIT_LIST_HEAD(&table->chains);
422 INIT_LIST_HEAD(&table->sets);
423
424 if (nla[NFTA_TABLE_FLAGS]) {
425 __be32 flags;
426
427 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
428 if (flags & ~NFT_TABLE_F_DORMANT) {
429 kfree(table);
430 return -EINVAL;
431 }
432
433 table->flags |= flags;
434 }
435
436 list_add_tail(&table->list, &afi->tables);
437 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
438 return 0;
439}
440
441static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
442 const struct nlmsghdr *nlh,
443 const struct nlattr * const nla[])
444{
445 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
446 struct nft_af_info *afi;
447 struct nft_table *table;
448 struct net *net = sock_net(skb->sk);
449 int family = nfmsg->nfgen_family;
450
451 afi = nf_tables_afinfo_lookup(net, family, false);
452 if (IS_ERR(afi))
453 return PTR_ERR(afi);
454
455 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
456 if (IS_ERR(table))
457 return PTR_ERR(table);
458
459 if (table->use)
460 return -EBUSY;
461
462 list_del(&table->list);
463 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
464 kfree(table);
465 return 0;
466}
467
468int nft_register_chain_type(struct nf_chain_type *ctype)
469{
470 int err = 0;
471
472 nfnl_lock(NFNL_SUBSYS_NFTABLES);
473 if (chain_type[ctype->family][ctype->type] != NULL) {
474 err = -EBUSY;
475 goto out;
476 }
477
478 if (!try_module_get(ctype->me))
479 goto out;
480
481 chain_type[ctype->family][ctype->type] = ctype;
482out:
483 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
484 return err;
485}
486EXPORT_SYMBOL_GPL(nft_register_chain_type);
487
488void nft_unregister_chain_type(struct nf_chain_type *ctype)
489{
490 nfnl_lock(NFNL_SUBSYS_NFTABLES);
491 chain_type[ctype->family][ctype->type] = NULL;
492 module_put(ctype->me);
493 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
494}
495EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
496
497/*
498 * Chains
499 */
500
501static struct nft_chain *
502nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
503{
504 struct nft_chain *chain;
505
506 list_for_each_entry(chain, &table->chains, list) {
507 if (chain->handle == handle)
508 return chain;
509 }
510
511 return ERR_PTR(-ENOENT);
512}
513
514static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
515 const struct nlattr *nla)
516{
517 struct nft_chain *chain;
518
519 if (nla == NULL)
520 return ERR_PTR(-EINVAL);
521
522 list_for_each_entry(chain, &table->chains, list) {
523 if (!nla_strcmp(nla, chain->name))
524 return chain;
525 }
526
527 return ERR_PTR(-ENOENT);
528}
529
530static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
531 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
532 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
533 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
534 .len = NFT_CHAIN_MAXNAMELEN - 1 },
535 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
536 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
537 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
538 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
539};
540
541static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
542 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
543 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
544};
545
546static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
547{
548 struct nft_stats *cpu_stats, total;
549 struct nlattr *nest;
550 int cpu;
551
552 memset(&total, 0, sizeof(total));
553 for_each_possible_cpu(cpu) {
554 cpu_stats = per_cpu_ptr(stats, cpu);
555 total.pkts += cpu_stats->pkts;
556 total.bytes += cpu_stats->bytes;
557 }
558 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
559 if (nest == NULL)
560 goto nla_put_failure;
561
562 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
563 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
564 goto nla_put_failure;
565
566 nla_nest_end(skb, nest);
567 return 0;
568
569nla_put_failure:
570 return -ENOSPC;
571}
572
573static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
574 int event, u32 flags, int family,
575 const struct nft_table *table,
576 const struct nft_chain *chain)
577{
578 struct nlmsghdr *nlh;
579 struct nfgenmsg *nfmsg;
580
581 event |= NFNL_SUBSYS_NFTABLES << 8;
582 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
583 if (nlh == NULL)
584 goto nla_put_failure;
585
586 nfmsg = nlmsg_data(nlh);
587 nfmsg->nfgen_family = family;
588 nfmsg->version = NFNETLINK_V0;
589 nfmsg->res_id = 0;
590
591 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
592 goto nla_put_failure;
593 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
594 goto nla_put_failure;
595 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
596 goto nla_put_failure;
597
598 if (chain->flags & NFT_BASE_CHAIN) {
599 const struct nft_base_chain *basechain = nft_base_chain(chain);
600 const struct nf_hook_ops *ops = &basechain->ops;
601 struct nlattr *nest;
602
603 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
604 if (nest == NULL)
605 goto nla_put_failure;
606 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
607 goto nla_put_failure;
608 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
609 goto nla_put_failure;
610 nla_nest_end(skb, nest);
611
612 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
613 htonl(basechain->policy)))
614 goto nla_put_failure;
615
616 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
617 chain_type[ops->pf][nft_base_chain(chain)->type]->name))
618 goto nla_put_failure;
619
620 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
621 goto nla_put_failure;
622 }
623
624 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
625 goto nla_put_failure;
626
627 return nlmsg_end(skb, nlh);
628
629nla_put_failure:
630 nlmsg_trim(skb, nlh);
631 return -1;
632}
633
634static int nf_tables_chain_notify(const struct sk_buff *oskb,
635 const struct nlmsghdr *nlh,
636 const struct nft_table *table,
637 const struct nft_chain *chain,
638 int event, int family)
639{
640 struct sk_buff *skb;
641 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
642 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
643 u32 seq = nlh ? nlh->nlmsg_seq : 0;
644 bool report;
645 int err;
646
647 report = nlh ? nlmsg_report(nlh) : false;
648 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
649 return 0;
650
651 err = -ENOBUFS;
652 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
653 if (skb == NULL)
654 goto err;
655
656 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
657 table, chain);
658 if (err < 0) {
659 kfree_skb(skb);
660 goto err;
661 }
662
663 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
664 GFP_KERNEL);
665err:
666 if (err < 0)
667 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
668 return err;
669}
670
671static int nf_tables_dump_chains(struct sk_buff *skb,
672 struct netlink_callback *cb)
673{
674 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
675 const struct nft_af_info *afi;
676 const struct nft_table *table;
677 const struct nft_chain *chain;
678 unsigned int idx = 0, s_idx = cb->args[0];
679 struct net *net = sock_net(skb->sk);
680 int family = nfmsg->nfgen_family;
681
682 list_for_each_entry(afi, &net->nft.af_info, list) {
683 if (family != NFPROTO_UNSPEC && family != afi->family)
684 continue;
685
686 list_for_each_entry(table, &afi->tables, list) {
687 list_for_each_entry(chain, &table->chains, list) {
688 if (idx < s_idx)
689 goto cont;
690 if (idx > s_idx)
691 memset(&cb->args[1], 0,
692 sizeof(cb->args) - sizeof(cb->args[0]));
693 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
694 cb->nlh->nlmsg_seq,
695 NFT_MSG_NEWCHAIN,
696 NLM_F_MULTI,
697 afi->family, table, chain) < 0)
698 goto done;
699cont:
700 idx++;
701 }
702 }
703 }
704done:
705 cb->args[0] = idx;
706 return skb->len;
707}
708
709
710static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
711 const struct nlmsghdr *nlh,
712 const struct nlattr * const nla[])
713{
714 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
715 const struct nft_af_info *afi;
716 const struct nft_table *table;
717 const struct nft_chain *chain;
718 struct sk_buff *skb2;
719 struct net *net = sock_net(skb->sk);
720 int family = nfmsg->nfgen_family;
721 int err;
722
723 if (nlh->nlmsg_flags & NLM_F_DUMP) {
724 struct netlink_dump_control c = {
725 .dump = nf_tables_dump_chains,
726 };
727 return netlink_dump_start(nlsk, skb, nlh, &c);
728 }
729
730 afi = nf_tables_afinfo_lookup(net, family, false);
731 if (IS_ERR(afi))
732 return PTR_ERR(afi);
733
734 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
735 if (IS_ERR(table))
736 return PTR_ERR(table);
737
738 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
739 if (IS_ERR(chain))
740 return PTR_ERR(chain);
741
742 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
743 if (!skb2)
744 return -ENOMEM;
745
746 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
747 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
748 family, table, chain);
749 if (err < 0)
750 goto err;
751
752 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
753
754err:
755 kfree_skb(skb2);
756 return err;
757}
758
759static int
760nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
761{
762 switch (ntohl(nla_get_be32(attr))) {
763 case NF_DROP:
764 chain->policy = NF_DROP;
765 break;
766 case NF_ACCEPT:
767 chain->policy = NF_ACCEPT;
768 break;
769 default:
770 return -EINVAL;
771 }
772 return 0;
773}
774
775static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
776 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
777 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
778};
779
780static int
781nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
782{
783 struct nlattr *tb[NFTA_COUNTER_MAX+1];
784 struct nft_stats __percpu *newstats;
785 struct nft_stats *stats;
786 int err;
787
788 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
789 if (err < 0)
790 return err;
791
792 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
793 return -EINVAL;
794
795 newstats = alloc_percpu(struct nft_stats);
796 if (newstats == NULL)
797 return -ENOMEM;
798
799 /* Restore old counters on this cpu, no problem. Per-cpu statistics
800 * are not exposed to userspace.
801 */
802 stats = this_cpu_ptr(newstats);
803 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
804 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
805
806 if (chain->stats) {
807 /* nfnl_lock is held, add some nfnl function for this, later */
808 struct nft_stats __percpu *oldstats =
809 rcu_dereference_protected(chain->stats, 1);
810
811 rcu_assign_pointer(chain->stats, newstats);
812 synchronize_rcu();
813 free_percpu(oldstats);
814 } else
815 rcu_assign_pointer(chain->stats, newstats);
816
817 return 0;
818}
819
820static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
821 const struct nlmsghdr *nlh,
822 const struct nlattr * const nla[])
823{
824 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
825 const struct nlattr * uninitialized_var(name);
826 const struct nft_af_info *afi;
827 struct nft_table *table;
828 struct nft_chain *chain;
829 struct nft_base_chain *basechain = NULL;
830 struct nlattr *ha[NFTA_HOOK_MAX + 1];
831 struct net *net = sock_net(skb->sk);
832 int family = nfmsg->nfgen_family;
833 u64 handle = 0;
834 int err;
835 bool create;
836
837 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
838
839 afi = nf_tables_afinfo_lookup(net, family, true);
840 if (IS_ERR(afi))
841 return PTR_ERR(afi);
842
843 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
844 if (IS_ERR(table))
845 return PTR_ERR(table);
846
847 if (table->use == UINT_MAX)
848 return -EOVERFLOW;
849
850 chain = NULL;
851 name = nla[NFTA_CHAIN_NAME];
852
853 if (nla[NFTA_CHAIN_HANDLE]) {
854 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
855 chain = nf_tables_chain_lookup_byhandle(table, handle);
856 if (IS_ERR(chain))
857 return PTR_ERR(chain);
858 } else {
859 chain = nf_tables_chain_lookup(table, name);
860 if (IS_ERR(chain)) {
861 if (PTR_ERR(chain) != -ENOENT)
862 return PTR_ERR(chain);
863 chain = NULL;
864 }
865 }
866
867 if (chain != NULL) {
868 if (nlh->nlmsg_flags & NLM_F_EXCL)
869 return -EEXIST;
870 if (nlh->nlmsg_flags & NLM_F_REPLACE)
871 return -EOPNOTSUPP;
872
873 if (nla[NFTA_CHAIN_HANDLE] && name &&
874 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
875 return -EEXIST;
876
877 if (nla[NFTA_CHAIN_POLICY]) {
878 if (!(chain->flags & NFT_BASE_CHAIN))
879 return -EOPNOTSUPP;
880
881 err = nf_tables_chain_policy(nft_base_chain(chain),
882 nla[NFTA_CHAIN_POLICY]);
883 if (err < 0)
884 return err;
885 }
886
887 if (nla[NFTA_CHAIN_COUNTERS]) {
888 if (!(chain->flags & NFT_BASE_CHAIN))
889 return -EOPNOTSUPP;
890
891 err = nf_tables_counters(nft_base_chain(chain),
892 nla[NFTA_CHAIN_COUNTERS]);
893 if (err < 0)
894 return err;
895 }
896
897 if (nla[NFTA_CHAIN_HANDLE] && name)
898 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
899
900 goto notify;
901 }
902
903 if (nla[NFTA_CHAIN_HOOK]) {
904 struct nf_hook_ops *ops;
905 nf_hookfn *hookfn;
906 u32 hooknum;
907 int type = NFT_CHAIN_T_DEFAULT;
908
909 if (nla[NFTA_CHAIN_TYPE]) {
910 type = nf_tables_chain_type_lookup(afi,
911 nla[NFTA_CHAIN_TYPE],
912 create);
913 if (type < 0)
914 return -ENOENT;
915 }
916
917 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
918 nft_hook_policy);
919 if (err < 0)
920 return err;
921 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
922 ha[NFTA_HOOK_PRIORITY] == NULL)
923 return -EINVAL;
924
925 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
926 if (hooknum >= afi->nhooks)
927 return -EINVAL;
928
929 hookfn = chain_type[family][type]->fn[hooknum];
930 if (hookfn == NULL)
931 return -EOPNOTSUPP;
932
933 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
934 if (basechain == NULL)
935 return -ENOMEM;
936
937 basechain->type = type;
938 chain = &basechain->chain;
939
940 ops = &basechain->ops;
941 ops->pf = family;
942 ops->owner = afi->owner;
943 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
944 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
945 ops->priv = chain;
946 ops->hook = hookfn;
947 if (afi->hooks[ops->hooknum])
948 ops->hook = afi->hooks[ops->hooknum];
949
950 chain->flags |= NFT_BASE_CHAIN;
951
952 if (nla[NFTA_CHAIN_POLICY]) {
953 err = nf_tables_chain_policy(basechain,
954 nla[NFTA_CHAIN_POLICY]);
955 if (err < 0) {
956 free_percpu(basechain->stats);
957 kfree(basechain);
958 return err;
959 }
960 } else
961 basechain->policy = NF_ACCEPT;
962
963 if (nla[NFTA_CHAIN_COUNTERS]) {
964 err = nf_tables_counters(basechain,
965 nla[NFTA_CHAIN_COUNTERS]);
966 if (err < 0) {
967 free_percpu(basechain->stats);
968 kfree(basechain);
969 return err;
970 }
971 } else {
972 struct nft_stats __percpu *newstats;
973
974 newstats = alloc_percpu(struct nft_stats);
975 if (newstats == NULL)
976 return -ENOMEM;
977
978 rcu_assign_pointer(nft_base_chain(chain)->stats,
979 newstats);
980 }
981 } else {
982 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
983 if (chain == NULL)
984 return -ENOMEM;
985 }
986
987 INIT_LIST_HEAD(&chain->rules);
988 chain->handle = nf_tables_alloc_handle(table);
989 chain->net = net;
990 chain->table = table;
991 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
992
993 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
994 chain->flags & NFT_BASE_CHAIN) {
995 err = nf_register_hook(&nft_base_chain(chain)->ops);
996 if (err < 0) {
997 free_percpu(basechain->stats);
998 kfree(basechain);
999 return err;
1000 }
1001 }
1002 list_add_tail(&chain->list, &table->chains);
1003 table->use++;
1004notify:
1005 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
1006 family);
1007 return 0;
1008}
1009
1010static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1011{
1012 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1013
1014 BUG_ON(chain->use > 0);
1015
1016 if (chain->flags & NFT_BASE_CHAIN) {
1017 free_percpu(nft_base_chain(chain)->stats);
1018 kfree(nft_base_chain(chain));
1019 } else
1020 kfree(chain);
1021}
1022
1023static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1024 const struct nlmsghdr *nlh,
1025 const struct nlattr * const nla[])
1026{
1027 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1028 const struct nft_af_info *afi;
1029 struct nft_table *table;
1030 struct nft_chain *chain;
1031 struct net *net = sock_net(skb->sk);
1032 int family = nfmsg->nfgen_family;
1033
1034 afi = nf_tables_afinfo_lookup(net, family, false);
1035 if (IS_ERR(afi))
1036 return PTR_ERR(afi);
1037
1038 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1039 if (IS_ERR(table))
1040 return PTR_ERR(table);
1041
1042 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1043 if (IS_ERR(chain))
1044 return PTR_ERR(chain);
1045
1046 if (!list_empty(&chain->rules))
1047 return -EBUSY;
1048
1049 list_del(&chain->list);
1050 table->use--;
1051
1052 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1053 chain->flags & NFT_BASE_CHAIN)
1054 nf_unregister_hook(&nft_base_chain(chain)->ops);
1055
1056 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1057 family);
1058
1059 /* Make sure all rule references are gone before this is released */
1060 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1061 return 0;
1062}
1063
1064static void nft_ctx_init(struct nft_ctx *ctx,
1065 const struct sk_buff *skb,
1066 const struct nlmsghdr *nlh,
1067 const struct nft_af_info *afi,
1068 const struct nft_table *table,
1069 const struct nft_chain *chain,
1070 const struct nlattr * const *nla)
1071{
1072 ctx->net = sock_net(skb->sk);
1073 ctx->skb = skb;
1074 ctx->nlh = nlh;
1075 ctx->afi = afi;
1076 ctx->table = table;
1077 ctx->chain = chain;
1078 ctx->nla = nla;
1079}
1080
1081/*
1082 * Expressions
1083 */
1084
1085/**
1086 * nft_register_expr - register nf_tables expr type
1087 * @ops: expr type
1088 *
1089 * Registers the expr type for use with nf_tables. Returns zero on
1090 * success or a negative errno code otherwise.
1091 */
1092int nft_register_expr(struct nft_expr_type *type)
1093{
1094 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1095 list_add_tail(&type->list, &nf_tables_expressions);
1096 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1097 return 0;
1098}
1099EXPORT_SYMBOL_GPL(nft_register_expr);
1100
1101/**
1102 * nft_unregister_expr - unregister nf_tables expr type
1103 * @ops: expr type
1104 *
1105 * Unregisters the expr typefor use with nf_tables.
1106 */
1107void nft_unregister_expr(struct nft_expr_type *type)
1108{
1109 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1110 list_del(&type->list);
1111 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1112}
1113EXPORT_SYMBOL_GPL(nft_unregister_expr);
1114
1115static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1116{
1117 const struct nft_expr_type *type;
1118
1119 list_for_each_entry(type, &nf_tables_expressions, list) {
1120 if (!nla_strcmp(nla, type->name))
1121 return type;
1122 }
1123 return NULL;
1124}
1125
1126static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1127{
1128 const struct nft_expr_type *type;
1129
1130 if (nla == NULL)
1131 return ERR_PTR(-EINVAL);
1132
1133 type = __nft_expr_type_get(nla);
1134 if (type != NULL && try_module_get(type->owner))
1135 return type;
1136
1137#ifdef CONFIG_MODULES
1138 if (type == NULL) {
1139 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1140 request_module("nft-expr-%.*s",
1141 nla_len(nla), (char *)nla_data(nla));
1142 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1143 if (__nft_expr_type_get(nla))
1144 return ERR_PTR(-EAGAIN);
1145 }
1146#endif
1147 return ERR_PTR(-ENOENT);
1148}
1149
1150static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1151 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1152 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1153};
1154
1155static int nf_tables_fill_expr_info(struct sk_buff *skb,
1156 const struct nft_expr *expr)
1157{
1158 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1159 goto nla_put_failure;
1160
1161 if (expr->ops->dump) {
1162 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1163 if (data == NULL)
1164 goto nla_put_failure;
1165 if (expr->ops->dump(skb, expr) < 0)
1166 goto nla_put_failure;
1167 nla_nest_end(skb, data);
1168 }
1169
1170 return skb->len;
1171
1172nla_put_failure:
1173 return -1;
1174};
1175
1176struct nft_expr_info {
1177 const struct nft_expr_ops *ops;
1178 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1179};
1180
1181static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1182 const struct nlattr *nla,
1183 struct nft_expr_info *info)
1184{
1185 const struct nft_expr_type *type;
1186 const struct nft_expr_ops *ops;
1187 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1188 int err;
1189
1190 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1191 if (err < 0)
1192 return err;
1193
1194 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1195 if (IS_ERR(type))
1196 return PTR_ERR(type);
1197
1198 if (tb[NFTA_EXPR_DATA]) {
1199 err = nla_parse_nested(info->tb, type->maxattr,
1200 tb[NFTA_EXPR_DATA], type->policy);
1201 if (err < 0)
1202 goto err1;
1203 } else
1204 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1205
1206 if (type->select_ops != NULL) {
1207 ops = type->select_ops(ctx,
1208 (const struct nlattr * const *)info->tb);
1209 if (IS_ERR(ops)) {
1210 err = PTR_ERR(ops);
1211 goto err1;
1212 }
1213 } else
1214 ops = type->ops;
1215
1216 info->ops = ops;
1217 return 0;
1218
1219err1:
1220 module_put(type->owner);
1221 return err;
1222}
1223
1224static int nf_tables_newexpr(const struct nft_ctx *ctx,
1225 const struct nft_expr_info *info,
1226 struct nft_expr *expr)
1227{
1228 const struct nft_expr_ops *ops = info->ops;
1229 int err;
1230
1231 expr->ops = ops;
1232 if (ops->init) {
1233 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1234 if (err < 0)
1235 goto err1;
1236 }
1237
1238 return 0;
1239
1240err1:
1241 expr->ops = NULL;
1242 return err;
1243}
1244
1245static void nf_tables_expr_destroy(struct nft_expr *expr)
1246{
1247 if (expr->ops->destroy)
1248 expr->ops->destroy(expr);
1249 module_put(expr->ops->type->owner);
1250}
1251
1252/*
1253 * Rules
1254 */
1255
1256static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1257 u64 handle)
1258{
1259 struct nft_rule *rule;
1260
1261 // FIXME: this sucks
1262 list_for_each_entry(rule, &chain->rules, list) {
1263 if (handle == rule->handle)
1264 return rule;
1265 }
1266
1267 return ERR_PTR(-ENOENT);
1268}
1269
1270static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1271 const struct nlattr *nla)
1272{
1273 if (nla == NULL)
1274 return ERR_PTR(-EINVAL);
1275
1276 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1277}
1278
1279static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1280 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1281 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1282 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1283 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1284 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1285 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1286 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1287};
1288
1289static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1290 int event, u32 flags, int family,
1291 const struct nft_table *table,
1292 const struct nft_chain *chain,
1293 const struct nft_rule *rule)
1294{
1295 struct nlmsghdr *nlh;
1296 struct nfgenmsg *nfmsg;
1297 const struct nft_expr *expr, *next;
1298 struct nlattr *list;
1299 const struct nft_rule *prule;
1300 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1301
1302 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1303 flags);
1304 if (nlh == NULL)
1305 goto nla_put_failure;
1306
1307 nfmsg = nlmsg_data(nlh);
1308 nfmsg->nfgen_family = family;
1309 nfmsg->version = NFNETLINK_V0;
1310 nfmsg->res_id = 0;
1311
1312 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1313 goto nla_put_failure;
1314 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1315 goto nla_put_failure;
1316 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1317 goto nla_put_failure;
1318
1319 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1320 prule = list_entry(rule->list.prev, struct nft_rule, list);
1321 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1322 cpu_to_be64(prule->handle)))
1323 goto nla_put_failure;
1324 }
1325
1326 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1327 if (list == NULL)
1328 goto nla_put_failure;
1329 nft_rule_for_each_expr(expr, next, rule) {
1330 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1331 if (elem == NULL)
1332 goto nla_put_failure;
1333 if (nf_tables_fill_expr_info(skb, expr) < 0)
1334 goto nla_put_failure;
1335 nla_nest_end(skb, elem);
1336 }
1337 nla_nest_end(skb, list);
1338
1339 return nlmsg_end(skb, nlh);
1340
1341nla_put_failure:
1342 nlmsg_trim(skb, nlh);
1343 return -1;
1344}
1345
1346static int nf_tables_rule_notify(const struct sk_buff *oskb,
1347 const struct nlmsghdr *nlh,
1348 const struct nft_table *table,
1349 const struct nft_chain *chain,
1350 const struct nft_rule *rule,
1351 int event, u32 flags, int family)
1352{
1353 struct sk_buff *skb;
1354 u32 portid = NETLINK_CB(oskb).portid;
1355 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1356 u32 seq = nlh->nlmsg_seq;
1357 bool report;
1358 int err;
1359
1360 report = nlmsg_report(nlh);
1361 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1362 return 0;
1363
1364 err = -ENOBUFS;
1365 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1366 if (skb == NULL)
1367 goto err;
1368
1369 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1370 family, table, chain, rule);
1371 if (err < 0) {
1372 kfree_skb(skb);
1373 goto err;
1374 }
1375
1376 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1377 GFP_KERNEL);
1378err:
1379 if (err < 0)
1380 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1381 return err;
1382}
1383
1384static inline bool
1385nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1386{
1387 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1388}
1389
1390static inline int gencursor_next(struct net *net)
1391{
1392 return net->nft.gencursor+1 == 1 ? 1 : 0;
1393}
1394
1395static inline int
1396nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1397{
1398 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1399}
1400
1401static inline void
1402nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1403{
1404 /* Now inactive, will be active in the future */
1405 rule->genmask = (1 << net->nft.gencursor);
1406}
1407
1408static inline void
1409nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1410{
1411 rule->genmask = (1 << gencursor_next(net));
1412}
1413
1414static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1415{
1416 rule->genmask = 0;
1417}
1418
1419static int nf_tables_dump_rules(struct sk_buff *skb,
1420 struct netlink_callback *cb)
1421{
1422 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1423 const struct nft_af_info *afi;
1424 const struct nft_table *table;
1425 const struct nft_chain *chain;
1426 const struct nft_rule *rule;
1427 unsigned int idx = 0, s_idx = cb->args[0];
1428 struct net *net = sock_net(skb->sk);
1429 int family = nfmsg->nfgen_family;
1430 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1431 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1432
1433 list_for_each_entry(afi, &net->nft.af_info, list) {
1434 if (family != NFPROTO_UNSPEC && family != afi->family)
1435 continue;
1436
1437 list_for_each_entry(table, &afi->tables, list) {
1438 list_for_each_entry(chain, &table->chains, list) {
1439 list_for_each_entry(rule, &chain->rules, list) {
1440 if (!nft_rule_is_active(net, rule))
1441 goto cont;
1442 if (idx < s_idx)
1443 goto cont;
1444 if (idx > s_idx)
1445 memset(&cb->args[1], 0,
1446 sizeof(cb->args) - sizeof(cb->args[0]));
1447 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1448 cb->nlh->nlmsg_seq,
1449 NFT_MSG_NEWRULE,
1450 NLM_F_MULTI | NLM_F_APPEND,
1451 afi->family, table, chain, rule) < 0)
1452 goto done;
1453cont:
1454 idx++;
1455 }
1456 }
1457 }
1458 }
1459done:
1460 /* Invalidate this dump, a transition to the new generation happened */
1461 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1462 return -EBUSY;
1463
1464 cb->args[0] = idx;
1465 return skb->len;
1466}
1467
1468static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1469 const struct nlmsghdr *nlh,
1470 const struct nlattr * const nla[])
1471{
1472 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1473 const struct nft_af_info *afi;
1474 const struct nft_table *table;
1475 const struct nft_chain *chain;
1476 const struct nft_rule *rule;
1477 struct sk_buff *skb2;
1478 struct net *net = sock_net(skb->sk);
1479 int family = nfmsg->nfgen_family;
1480 int err;
1481
1482 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1483 struct netlink_dump_control c = {
1484 .dump = nf_tables_dump_rules,
1485 };
1486 return netlink_dump_start(nlsk, skb, nlh, &c);
1487 }
1488
1489 afi = nf_tables_afinfo_lookup(net, family, false);
1490 if (IS_ERR(afi))
1491 return PTR_ERR(afi);
1492
1493 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1494 if (IS_ERR(table))
1495 return PTR_ERR(table);
1496
1497 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1498 if (IS_ERR(chain))
1499 return PTR_ERR(chain);
1500
1501 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1502 if (IS_ERR(rule))
1503 return PTR_ERR(rule);
1504
1505 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1506 if (!skb2)
1507 return -ENOMEM;
1508
1509 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1510 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1511 family, table, chain, rule);
1512 if (err < 0)
1513 goto err;
1514
1515 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1516
1517err:
1518 kfree_skb(skb2);
1519 return err;
1520}
1521
1522static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1523{
1524 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1525 struct nft_expr *expr;
1526
1527 /*
1528 * Careful: some expressions might not be initialized in case this
1529 * is called on error from nf_tables_newrule().
1530 */
1531 expr = nft_expr_first(rule);
1532 while (expr->ops && expr != nft_expr_last(rule)) {
1533 nf_tables_expr_destroy(expr);
1534 expr = nft_expr_next(expr);
1535 }
1536 kfree(rule);
1537}
1538
1539static void nf_tables_rule_destroy(struct nft_rule *rule)
1540{
1541 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1542}
1543
1544#define NFT_RULE_MAXEXPRS 128
1545
1546static struct nft_expr_info *info;
1547
1548static struct nft_rule_trans *
1549nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1550{
1551 struct nft_rule_trans *rupd;
1552
1553 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1554 if (rupd == NULL)
1555 return NULL;
1556
1557 rupd->chain = ctx->chain;
1558 rupd->table = ctx->table;
1559 rupd->rule = rule;
1560 rupd->family = ctx->afi->family;
1561 rupd->nlh = ctx->nlh;
1562 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1563
1564 return rupd;
1565}
1566
1567static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1568 const struct nlmsghdr *nlh,
1569 const struct nlattr * const nla[])
1570{
1571 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1572 const struct nft_af_info *afi;
1573 struct net *net = sock_net(skb->sk);
1574 struct nft_table *table;
1575 struct nft_chain *chain;
1576 struct nft_rule *rule, *old_rule = NULL;
1577 struct nft_rule_trans *repl = NULL;
1578 struct nft_expr *expr;
1579 struct nft_ctx ctx;
1580 struct nlattr *tmp;
1581 unsigned int size, i, n;
1582 int err, rem;
1583 bool create;
1584 u64 handle, pos_handle;
1585
1586 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1587
1588 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1589 if (IS_ERR(afi))
1590 return PTR_ERR(afi);
1591
1592 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1593 if (IS_ERR(table))
1594 return PTR_ERR(table);
1595
1596 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1597 if (IS_ERR(chain))
1598 return PTR_ERR(chain);
1599
1600 if (nla[NFTA_RULE_HANDLE]) {
1601 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1602 rule = __nf_tables_rule_lookup(chain, handle);
1603 if (IS_ERR(rule))
1604 return PTR_ERR(rule);
1605
1606 if (nlh->nlmsg_flags & NLM_F_EXCL)
1607 return -EEXIST;
1608 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1609 old_rule = rule;
1610 else
1611 return -EOPNOTSUPP;
1612 } else {
1613 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1614 return -EINVAL;
1615 handle = nf_tables_alloc_handle(table);
1616 }
1617
1618 if (nla[NFTA_RULE_POSITION]) {
1619 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1620 return -EOPNOTSUPP;
1621
1622 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1623 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1624 if (IS_ERR(old_rule))
1625 return PTR_ERR(old_rule);
1626 }
1627
1628 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1629
1630 n = 0;
1631 size = 0;
1632 if (nla[NFTA_RULE_EXPRESSIONS]) {
1633 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1634 err = -EINVAL;
1635 if (nla_type(tmp) != NFTA_LIST_ELEM)
1636 goto err1;
1637 if (n == NFT_RULE_MAXEXPRS)
1638 goto err1;
1639 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1640 if (err < 0)
1641 goto err1;
1642 size += info[n].ops->size;
1643 n++;
1644 }
1645 }
1646
1647 err = -ENOMEM;
1648 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1649 if (rule == NULL)
1650 goto err1;
1651
1652 nft_rule_activate_next(net, rule);
1653
1654 rule->handle = handle;
1655 rule->dlen = size;
1656
1657 expr = nft_expr_first(rule);
1658 for (i = 0; i < n; i++) {
1659 err = nf_tables_newexpr(&ctx, &info[i], expr);
1660 if (err < 0)
1661 goto err2;
1662 info[i].ops = NULL;
1663 expr = nft_expr_next(expr);
1664 }
1665
1666 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1667 if (nft_rule_is_active_next(net, old_rule)) {
1668 repl = nf_tables_trans_add(old_rule, &ctx);
1669 if (repl == NULL) {
1670 err = -ENOMEM;
1671 goto err2;
1672 }
1673 nft_rule_disactivate_next(net, old_rule);
1674 list_add_tail(&rule->list, &old_rule->list);
1675 } else {
1676 err = -ENOENT;
1677 goto err2;
1678 }
1679 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1680 if (old_rule)
1681 list_add_rcu(&rule->list, &old_rule->list);
1682 else
1683 list_add_tail_rcu(&rule->list, &chain->rules);
1684 else {
1685 if (old_rule)
1686 list_add_tail_rcu(&rule->list, &old_rule->list);
1687 else
1688 list_add_rcu(&rule->list, &chain->rules);
1689 }
1690
1691 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1692 err = -ENOMEM;
1693 goto err3;
1694 }
1695 return 0;
1696
1697err3:
1698 list_del_rcu(&rule->list);
1699 if (repl) {
1700 list_del_rcu(&repl->rule->list);
1701 list_del(&repl->list);
1702 nft_rule_clear(net, repl->rule);
1703 kfree(repl);
1704 }
1705err2:
1706 nf_tables_rule_destroy(rule);
1707err1:
1708 for (i = 0; i < n; i++) {
1709 if (info[i].ops != NULL)
1710 module_put(info[i].ops->type->owner);
1711 }
1712 return err;
1713}
1714
1715static int
1716nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1717{
1718 /* You cannot delete the same rule twice */
1719 if (nft_rule_is_active_next(ctx->net, rule)) {
1720 if (nf_tables_trans_add(rule, ctx) == NULL)
1721 return -ENOMEM;
1722 nft_rule_disactivate_next(ctx->net, rule);
1723 return 0;
1724 }
1725 return -ENOENT;
1726}
1727
1728static int nf_table_delrule_by_chain(struct nft_ctx *ctx)
1729{
1730 struct nft_rule *rule;
1731 int err;
1732
1733 list_for_each_entry(rule, &ctx->chain->rules, list) {
1734 err = nf_tables_delrule_one(ctx, rule);
1735 if (err < 0)
1736 return err;
1737 }
1738 return 0;
1739}
1740
1741static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1742 const struct nlmsghdr *nlh,
1743 const struct nlattr * const nla[])
1744{
1745 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1746 const struct nft_af_info *afi;
1747 struct net *net = sock_net(skb->sk);
1748 const struct nft_table *table;
1749 struct nft_chain *chain = NULL;
1750 struct nft_rule *rule;
1751 int family = nfmsg->nfgen_family, err = 0;
1752 struct nft_ctx ctx;
1753
1754 afi = nf_tables_afinfo_lookup(net, family, false);
1755 if (IS_ERR(afi))
1756 return PTR_ERR(afi);
1757
1758 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1759 if (IS_ERR(table))
1760 return PTR_ERR(table);
1761
1762 if (nla[NFTA_RULE_CHAIN]) {
1763 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1764 if (IS_ERR(chain))
1765 return PTR_ERR(chain);
1766 }
1767
1768 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1769
1770 if (chain) {
1771 if (nla[NFTA_RULE_HANDLE]) {
1772 rule = nf_tables_rule_lookup(chain,
1773 nla[NFTA_RULE_HANDLE]);
1774 if (IS_ERR(rule))
1775 return PTR_ERR(rule);
1776
1777 err = nf_tables_delrule_one(&ctx, rule);
1778 } else {
1779 err = nf_table_delrule_by_chain(&ctx);
1780 }
1781 } else {
1782 list_for_each_entry(chain, &table->chains, list) {
1783 ctx.chain = chain;
1784 err = nf_table_delrule_by_chain(&ctx);
1785 if (err < 0)
1786 break;
1787 }
1788 }
1789
1790 return err;
1791}
1792
1793static int nf_tables_commit(struct sk_buff *skb)
1794{
1795 struct net *net = sock_net(skb->sk);
1796 struct nft_rule_trans *rupd, *tmp;
1797
1798 /* Bump generation counter, invalidate any dump in progress */
1799 net->nft.genctr++;
1800
1801 /* A new generation has just started */
1802 net->nft.gencursor = gencursor_next(net);
1803
1804 /* Make sure all packets have left the previous generation before
1805 * purging old rules.
1806 */
1807 synchronize_rcu();
1808
1809 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1810 /* Delete this rule from the dirty list */
1811 list_del(&rupd->list);
1812
1813 /* This rule was inactive in the past and just became active.
1814 * Clear the next bit of the genmask since its meaning has
1815 * changed, now it is the future.
1816 */
1817 if (nft_rule_is_active(net, rupd->rule)) {
1818 nft_rule_clear(net, rupd->rule);
1819 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1820 rupd->chain, rupd->rule,
1821 NFT_MSG_NEWRULE, 0,
1822 rupd->family);
1823 kfree(rupd);
1824 continue;
1825 }
1826
1827 /* This rule is in the past, get rid of it */
1828 list_del_rcu(&rupd->rule->list);
1829 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1830 rupd->rule, NFT_MSG_DELRULE, 0,
1831 rupd->family);
1832 nf_tables_rule_destroy(rupd->rule);
1833 kfree(rupd);
1834 }
1835
1836 return 0;
1837}
1838
1839static int nf_tables_abort(struct sk_buff *skb)
1840{
1841 struct net *net = sock_net(skb->sk);
1842 struct nft_rule_trans *rupd, *tmp;
1843
1844 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1845 /* Delete all rules from the dirty list */
1846 list_del(&rupd->list);
1847
1848 if (!nft_rule_is_active_next(net, rupd->rule)) {
1849 nft_rule_clear(net, rupd->rule);
1850 kfree(rupd);
1851 continue;
1852 }
1853
1854 /* This rule is inactive, get rid of it */
1855 list_del_rcu(&rupd->rule->list);
1856 nf_tables_rule_destroy(rupd->rule);
1857 kfree(rupd);
1858 }
1859 return 0;
1860}
1861
1862/*
1863 * Sets
1864 */
1865
1866static LIST_HEAD(nf_tables_set_ops);
1867
1868int nft_register_set(struct nft_set_ops *ops)
1869{
1870 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1871 list_add_tail(&ops->list, &nf_tables_set_ops);
1872 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1873 return 0;
1874}
1875EXPORT_SYMBOL_GPL(nft_register_set);
1876
1877void nft_unregister_set(struct nft_set_ops *ops)
1878{
1879 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1880 list_del(&ops->list);
1881 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1882}
1883EXPORT_SYMBOL_GPL(nft_unregister_set);
1884
1885static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1886{
1887 const struct nft_set_ops *ops;
1888 u32 features;
1889
1890#ifdef CONFIG_MODULES
1891 if (list_empty(&nf_tables_set_ops)) {
1892 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1893 request_module("nft-set");
1894 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1895 if (!list_empty(&nf_tables_set_ops))
1896 return ERR_PTR(-EAGAIN);
1897 }
1898#endif
1899 features = 0;
1900 if (nla[NFTA_SET_FLAGS] != NULL) {
1901 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1902 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1903 }
1904
1905 // FIXME: implement selection properly
1906 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1907 if ((ops->features & features) != features)
1908 continue;
1909 if (!try_module_get(ops->owner))
1910 continue;
1911 return ops;
1912 }
1913
1914 return ERR_PTR(-EOPNOTSUPP);
1915}
1916
1917static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1918 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1919 [NFTA_SET_NAME] = { .type = NLA_STRING },
1920 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1921 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1922 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1923 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1924 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1925};
1926
1927static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1928 const struct sk_buff *skb,
1929 const struct nlmsghdr *nlh,
1930 const struct nlattr * const nla[])
1931{
1932 struct net *net = sock_net(skb->sk);
1933 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1934 const struct nft_af_info *afi;
1935 const struct nft_table *table = NULL;
1936
1937 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1938 if (IS_ERR(afi))
1939 return PTR_ERR(afi);
1940
1941 if (nla[NFTA_SET_TABLE] != NULL) {
1942 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1943 if (IS_ERR(table))
1944 return PTR_ERR(table);
1945 }
1946
1947 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1948 return 0;
1949}
1950
1951struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1952 const struct nlattr *nla)
1953{
1954 struct nft_set *set;
1955
1956 if (nla == NULL)
1957 return ERR_PTR(-EINVAL);
1958
1959 list_for_each_entry(set, &table->sets, list) {
1960 if (!nla_strcmp(nla, set->name))
1961 return set;
1962 }
1963 return ERR_PTR(-ENOENT);
1964}
1965
1966static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1967 const char *name)
1968{
1969 const struct nft_set *i;
1970 const char *p;
1971 unsigned long *inuse;
1972 unsigned int n = 0;
1973
1974 p = strnchr(name, IFNAMSIZ, '%');
1975 if (p != NULL) {
1976 if (p[1] != 'd' || strchr(p + 2, '%'))
1977 return -EINVAL;
1978
1979 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1980 if (inuse == NULL)
1981 return -ENOMEM;
1982
1983 list_for_each_entry(i, &ctx->table->sets, list) {
1984 if (!sscanf(i->name, name, &n))
1985 continue;
1986 if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1987 continue;
1988 set_bit(n, inuse);
1989 }
1990
1991 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1992 free_page((unsigned long)inuse);
1993 }
1994
1995 snprintf(set->name, sizeof(set->name), name, n);
1996 list_for_each_entry(i, &ctx->table->sets, list) {
1997 if (!strcmp(set->name, i->name))
1998 return -ENFILE;
1999 }
2000 return 0;
2001}
2002
2003static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2004 const struct nft_set *set, u16 event, u16 flags)
2005{
2006 struct nfgenmsg *nfmsg;
2007 struct nlmsghdr *nlh;
2008 u32 portid = NETLINK_CB(ctx->skb).portid;
2009 u32 seq = ctx->nlh->nlmsg_seq;
2010
2011 event |= NFNL_SUBSYS_NFTABLES << 8;
2012 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2013 flags);
2014 if (nlh == NULL)
2015 goto nla_put_failure;
2016
2017 nfmsg = nlmsg_data(nlh);
2018 nfmsg->nfgen_family = ctx->afi->family;
2019 nfmsg->version = NFNETLINK_V0;
2020 nfmsg->res_id = 0;
2021
2022 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
2023 goto nla_put_failure;
2024 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
2025 goto nla_put_failure;
2026 if (set->flags != 0)
2027 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2028 goto nla_put_failure;
2029
2030 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2031 goto nla_put_failure;
2032 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2033 goto nla_put_failure;
2034 if (set->flags & NFT_SET_MAP) {
2035 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2036 goto nla_put_failure;
2037 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2038 goto nla_put_failure;
2039 }
2040
2041 return nlmsg_end(skb, nlh);
2042
2043nla_put_failure:
2044 nlmsg_trim(skb, nlh);
2045 return -1;
2046}
2047
2048static int nf_tables_set_notify(const struct nft_ctx *ctx,
2049 const struct nft_set *set,
2050 int event)
2051{
2052 struct sk_buff *skb;
2053 u32 portid = NETLINK_CB(ctx->skb).portid;
2054 bool report;
2055 int err;
2056
2057 report = nlmsg_report(ctx->nlh);
2058 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2059 return 0;
2060
2061 err = -ENOBUFS;
2062 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2063 if (skb == NULL)
2064 goto err;
2065
2066 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2067 if (err < 0) {
2068 kfree_skb(skb);
2069 goto err;
2070 }
2071
2072 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2073 GFP_KERNEL);
2074err:
2075 if (err < 0)
2076 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2077 return err;
2078}
2079
2080static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2081 struct netlink_callback *cb)
2082{
2083 const struct nft_set *set;
2084 unsigned int idx = 0, s_idx = cb->args[0];
2085
2086 if (cb->args[1])
2087 return skb->len;
2088
2089 list_for_each_entry(set, &ctx->table->sets, list) {
2090 if (idx < s_idx)
2091 goto cont;
2092 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2093 NLM_F_MULTI) < 0) {
2094 cb->args[0] = idx;
2095 goto done;
2096 }
2097cont:
2098 idx++;
2099 }
2100 cb->args[1] = 1;
2101done:
2102 return skb->len;
2103}
2104
2105static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2106 struct netlink_callback *cb)
2107{
2108 const struct nft_set *set;
2109 unsigned int idx, s_idx = cb->args[0];
2110 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2111
2112 if (cb->args[1])
2113 return skb->len;
2114
2115 list_for_each_entry(table, &ctx->afi->tables, list) {
2116 if (cur_table) {
2117 if (cur_table != table)
2118 continue;
2119
2120 cur_table = NULL;
2121 }
2122 ctx->table = table;
2123 idx = 0;
2124 list_for_each_entry(set, &ctx->table->sets, list) {
2125 if (idx < s_idx)
2126 goto cont;
2127 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2128 NLM_F_MULTI) < 0) {
2129 cb->args[0] = idx;
2130 cb->args[2] = (unsigned long) table;
2131 goto done;
2132 }
2133cont:
2134 idx++;
2135 }
2136 }
2137 cb->args[1] = 1;
2138done:
2139 return skb->len;
2140}
2141
2142static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2143{
2144 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2145 struct nlattr *nla[NFTA_SET_MAX + 1];
2146 struct nft_ctx ctx;
2147 int err, ret;
2148
2149 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2150 nft_set_policy);
2151 if (err < 0)
2152 return err;
2153
2154 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2155 if (err < 0)
2156 return err;
2157
2158 if (ctx.table == NULL)
2159 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2160 else
2161 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2162
2163 return ret;
2164}
2165
2166static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2167 const struct nlmsghdr *nlh,
2168 const struct nlattr * const nla[])
2169{
2170 const struct nft_set *set;
2171 struct nft_ctx ctx;
2172 struct sk_buff *skb2;
2173 int err;
2174
2175 /* Verify existance before starting dump */
2176 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2177 if (err < 0)
2178 return err;
2179
2180 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2181 struct netlink_dump_control c = {
2182 .dump = nf_tables_dump_sets,
2183 };
2184 return netlink_dump_start(nlsk, skb, nlh, &c);
2185 }
2186
2187 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2188 if (IS_ERR(set))
2189 return PTR_ERR(set);
2190
2191 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2192 if (skb2 == NULL)
2193 return -ENOMEM;
2194
2195 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2196 if (err < 0)
2197 goto err;
2198
2199 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2200
2201err:
2202 kfree_skb(skb2);
2203 return err;
2204}
2205
2206static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2207 const struct nlmsghdr *nlh,
2208 const struct nlattr * const nla[])
2209{
2210 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2211 const struct nft_set_ops *ops;
2212 const struct nft_af_info *afi;
2213 struct net *net = sock_net(skb->sk);
2214 struct nft_table *table;
2215 struct nft_set *set;
2216 struct nft_ctx ctx;
2217 char name[IFNAMSIZ];
2218 unsigned int size;
2219 bool create;
2220 u32 ktype, klen, dlen, dtype, flags;
2221 int err;
2222
2223 if (nla[NFTA_SET_TABLE] == NULL ||
2224 nla[NFTA_SET_NAME] == NULL ||
2225 nla[NFTA_SET_KEY_LEN] == NULL)
2226 return -EINVAL;
2227
2228 ktype = NFT_DATA_VALUE;
2229 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2230 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2231 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2232 return -EINVAL;
2233 }
2234
2235 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2236 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2237 return -EINVAL;
2238
2239 flags = 0;
2240 if (nla[NFTA_SET_FLAGS] != NULL) {
2241 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2242 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2243 NFT_SET_INTERVAL | NFT_SET_MAP))
2244 return -EINVAL;
2245 }
2246
2247 dtype = 0;
2248 dlen = 0;
2249 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2250 if (!(flags & NFT_SET_MAP))
2251 return -EINVAL;
2252
2253 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2254 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2255 dtype != NFT_DATA_VERDICT)
2256 return -EINVAL;
2257
2258 if (dtype != NFT_DATA_VERDICT) {
2259 if (nla[NFTA_SET_DATA_LEN] == NULL)
2260 return -EINVAL;
2261 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2262 if (dlen == 0 ||
2263 dlen > FIELD_SIZEOF(struct nft_data, data))
2264 return -EINVAL;
2265 } else
2266 dlen = sizeof(struct nft_data);
2267 } else if (flags & NFT_SET_MAP)
2268 return -EINVAL;
2269
2270 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2271
2272 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2273 if (IS_ERR(afi))
2274 return PTR_ERR(afi);
2275
2276 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2277 if (IS_ERR(table))
2278 return PTR_ERR(table);
2279
2280 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2281
2282 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2283 if (IS_ERR(set)) {
2284 if (PTR_ERR(set) != -ENOENT)
2285 return PTR_ERR(set);
2286 set = NULL;
2287 }
2288
2289 if (set != NULL) {
2290 if (nlh->nlmsg_flags & NLM_F_EXCL)
2291 return -EEXIST;
2292 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2293 return -EOPNOTSUPP;
2294 return 0;
2295 }
2296
2297 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2298 return -ENOENT;
2299
2300 ops = nft_select_set_ops(nla);
2301 if (IS_ERR(ops))
2302 return PTR_ERR(ops);
2303
2304 size = 0;
2305 if (ops->privsize != NULL)
2306 size = ops->privsize(nla);
2307
2308 err = -ENOMEM;
2309 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2310 if (set == NULL)
2311 goto err1;
2312
2313 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2314 err = nf_tables_set_alloc_name(&ctx, set, name);
2315 if (err < 0)
2316 goto err2;
2317
2318 INIT_LIST_HEAD(&set->bindings);
2319 set->ops = ops;
2320 set->ktype = ktype;
2321 set->klen = klen;
2322 set->dtype = dtype;
2323 set->dlen = dlen;
2324 set->flags = flags;
2325
2326 err = ops->init(set, nla);
2327 if (err < 0)
2328 goto err2;
2329
2330 list_add_tail(&set->list, &table->sets);
2331 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2332 return 0;
2333
2334err2:
2335 kfree(set);
2336err1:
2337 module_put(ops->owner);
2338 return err;
2339}
2340
2341static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2342{
2343 list_del(&set->list);
2344 if (!(set->flags & NFT_SET_ANONYMOUS))
2345 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2346
2347 set->ops->destroy(set);
2348 module_put(set->ops->owner);
2349 kfree(set);
2350}
2351
2352static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2353 const struct nlmsghdr *nlh,
2354 const struct nlattr * const nla[])
2355{
2356 struct nft_set *set;
2357 struct nft_ctx ctx;
2358 int err;
2359
2360 if (nla[NFTA_SET_TABLE] == NULL)
2361 return -EINVAL;
2362
2363 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2364 if (err < 0)
2365 return err;
2366
2367 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2368 if (IS_ERR(set))
2369 return PTR_ERR(set);
2370 if (!list_empty(&set->bindings))
2371 return -EBUSY;
2372
2373 nf_tables_set_destroy(&ctx, set);
2374 return 0;
2375}
2376
2377static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2378 const struct nft_set *set,
2379 const struct nft_set_iter *iter,
2380 const struct nft_set_elem *elem)
2381{
2382 enum nft_registers dreg;
2383
2384 dreg = nft_type_to_reg(set->dtype);
2385 return nft_validate_data_load(ctx, dreg, &elem->data,
2386 set->dtype == NFT_DATA_VERDICT ?
2387 NFT_DATA_VERDICT : NFT_DATA_VALUE);
2388}
2389
2390int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2391 struct nft_set_binding *binding)
2392{
2393 struct nft_set_binding *i;
2394 struct nft_set_iter iter;
2395
2396 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2397 return -EBUSY;
2398
2399 if (set->flags & NFT_SET_MAP) {
2400 /* If the set is already bound to the same chain all
2401 * jumps are already validated for that chain.
2402 */
2403 list_for_each_entry(i, &set->bindings, list) {
2404 if (i->chain == binding->chain)
2405 goto bind;
2406 }
2407
2408 iter.skip = 0;
2409 iter.count = 0;
2410 iter.err = 0;
2411 iter.fn = nf_tables_bind_check_setelem;
2412
2413 set->ops->walk(ctx, set, &iter);
2414 if (iter.err < 0) {
2415 /* Destroy anonymous sets if binding fails */
2416 if (set->flags & NFT_SET_ANONYMOUS)
2417 nf_tables_set_destroy(ctx, set);
2418
2419 return iter.err;
2420 }
2421 }
2422bind:
2423 binding->chain = ctx->chain;
2424 list_add_tail(&binding->list, &set->bindings);
2425 return 0;
2426}
2427
2428void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2429 struct nft_set_binding *binding)
2430{
2431 list_del(&binding->list);
2432
2433 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2434 nf_tables_set_destroy(ctx, set);
2435}
2436
2437/*
2438 * Set elements
2439 */
2440
2441static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2442 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2443 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2444 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2445};
2446
2447static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2448 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2449 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2450 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2451};
2452
2453static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2454 const struct sk_buff *skb,
2455 const struct nlmsghdr *nlh,
2456 const struct nlattr * const nla[])
2457{
2458 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2459 const struct nft_af_info *afi;
2460 const struct nft_table *table;
2461 struct net *net = sock_net(skb->sk);
2462
2463 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2464 if (IS_ERR(afi))
2465 return PTR_ERR(afi);
2466
2467 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2468 if (IS_ERR(table))
2469 return PTR_ERR(table);
2470
2471 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2472 return 0;
2473}
2474
2475static int nf_tables_fill_setelem(struct sk_buff *skb,
2476 const struct nft_set *set,
2477 const struct nft_set_elem *elem)
2478{
2479 unsigned char *b = skb_tail_pointer(skb);
2480 struct nlattr *nest;
2481
2482 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2483 if (nest == NULL)
2484 goto nla_put_failure;
2485
2486 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2487 set->klen) < 0)
2488 goto nla_put_failure;
2489
2490 if (set->flags & NFT_SET_MAP &&
2491 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2492 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2493 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2494 set->dlen) < 0)
2495 goto nla_put_failure;
2496
2497 if (elem->flags != 0)
2498 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2499 goto nla_put_failure;
2500
2501 nla_nest_end(skb, nest);
2502 return 0;
2503
2504nla_put_failure:
2505 nlmsg_trim(skb, b);
2506 return -EMSGSIZE;
2507}
2508
2509struct nft_set_dump_args {
2510 const struct netlink_callback *cb;
2511 struct nft_set_iter iter;
2512 struct sk_buff *skb;
2513};
2514
2515static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2516 const struct nft_set *set,
2517 const struct nft_set_iter *iter,
2518 const struct nft_set_elem *elem)
2519{
2520 struct nft_set_dump_args *args;
2521
2522 args = container_of(iter, struct nft_set_dump_args, iter);
2523 return nf_tables_fill_setelem(args->skb, set, elem);
2524}
2525
2526static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2527{
2528 const struct nft_set *set;
2529 struct nft_set_dump_args args;
2530 struct nft_ctx ctx;
2531 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2532 struct nfgenmsg *nfmsg;
2533 struct nlmsghdr *nlh;
2534 struct nlattr *nest;
2535 u32 portid, seq;
2536 int event, err;
2537
2538 nfmsg = nlmsg_data(cb->nlh);
2539 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2540 nft_set_elem_list_policy);
2541 if (err < 0)
2542 return err;
2543
2544 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2545 if (err < 0)
2546 return err;
2547
2548 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2549 if (IS_ERR(set))
2550 return PTR_ERR(set);
2551
2552 event = NFT_MSG_NEWSETELEM;
2553 event |= NFNL_SUBSYS_NFTABLES << 8;
2554 portid = NETLINK_CB(cb->skb).portid;
2555 seq = cb->nlh->nlmsg_seq;
2556
2557 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2558 NLM_F_MULTI);
2559 if (nlh == NULL)
2560 goto nla_put_failure;
2561
2562 nfmsg = nlmsg_data(nlh);
2563 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2564 nfmsg->version = NFNETLINK_V0;
2565 nfmsg->res_id = 0;
2566
2567 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2568 goto nla_put_failure;
2569 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2570 goto nla_put_failure;
2571
2572 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2573 if (nest == NULL)
2574 goto nla_put_failure;
2575
2576 args.cb = cb;
2577 args.skb = skb;
2578 args.iter.skip = cb->args[0];
2579 args.iter.count = 0;
2580 args.iter.err = 0;
2581 args.iter.fn = nf_tables_dump_setelem;
2582 set->ops->walk(&ctx, set, &args.iter);
2583
2584 nla_nest_end(skb, nest);
2585 nlmsg_end(skb, nlh);
2586
2587 if (args.iter.err && args.iter.err != -EMSGSIZE)
2588 return args.iter.err;
2589 if (args.iter.count == cb->args[0])
2590 return 0;
2591
2592 cb->args[0] = args.iter.count;
2593 return skb->len;
2594
2595nla_put_failure:
2596 return -ENOSPC;
2597}
2598
2599static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2600 const struct nlmsghdr *nlh,
2601 const struct nlattr * const nla[])
2602{
2603 const struct nft_set *set;
2604 struct nft_ctx ctx;
2605 int err;
2606
2607 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2608 if (err < 0)
2609 return err;
2610
2611 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2612 if (IS_ERR(set))
2613 return PTR_ERR(set);
2614
2615 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2616 struct netlink_dump_control c = {
2617 .dump = nf_tables_dump_set,
2618 };
2619 return netlink_dump_start(nlsk, skb, nlh, &c);
2620 }
2621 return -EOPNOTSUPP;
2622}
2623
2624static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2625 const struct nlattr *attr)
2626{
2627 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2628 struct nft_data_desc d1, d2;
2629 struct nft_set_elem elem;
2630 struct nft_set_binding *binding;
2631 enum nft_registers dreg;
2632 int err;
2633
2634 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2635 nft_set_elem_policy);
2636 if (err < 0)
2637 return err;
2638
2639 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2640 return -EINVAL;
2641
2642 elem.flags = 0;
2643 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2644 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2645 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2646 return -EINVAL;
2647 }
2648
2649 if (set->flags & NFT_SET_MAP) {
2650 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2651 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2652 return -EINVAL;
2653 } else {
2654 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2655 return -EINVAL;
2656 }
2657
2658 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2659 if (err < 0)
2660 goto err1;
2661 err = -EINVAL;
2662 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2663 goto err2;
2664
2665 err = -EEXIST;
2666 if (set->ops->get(set, &elem) == 0)
2667 goto err2;
2668
2669 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2670 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2671 if (err < 0)
2672 goto err2;
2673
2674 err = -EINVAL;
2675 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2676 goto err3;
2677
2678 dreg = nft_type_to_reg(set->dtype);
2679 list_for_each_entry(binding, &set->bindings, list) {
2680 struct nft_ctx bind_ctx = {
2681 .afi = ctx->afi,
2682 .table = ctx->table,
2683 .chain = binding->chain,
2684 };
2685
2686 err = nft_validate_data_load(&bind_ctx, dreg,
2687 &elem.data, d2.type);
2688 if (err < 0)
2689 goto err3;
2690 }
2691 }
2692
2693 err = set->ops->insert(set, &elem);
2694 if (err < 0)
2695 goto err3;
2696
2697 return 0;
2698
2699err3:
2700 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2701 nft_data_uninit(&elem.data, d2.type);
2702err2:
2703 nft_data_uninit(&elem.key, d1.type);
2704err1:
2705 return err;
2706}
2707
2708static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2709 const struct nlmsghdr *nlh,
2710 const struct nlattr * const nla[])
2711{
2712 const struct nlattr *attr;
2713 struct nft_set *set;
2714 struct nft_ctx ctx;
2715 int rem, err;
2716
2717 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2718 if (err < 0)
2719 return err;
2720
2721 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2722 if (IS_ERR(set))
2723 return PTR_ERR(set);
2724 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2725 return -EBUSY;
2726
2727 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2728 err = nft_add_set_elem(&ctx, set, attr);
2729 if (err < 0)
2730 return err;
2731 }
2732 return 0;
2733}
2734
2735static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2736 const struct nlattr *attr)
2737{
2738 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2739 struct nft_data_desc desc;
2740 struct nft_set_elem elem;
2741 int err;
2742
2743 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2744 nft_set_elem_policy);
2745 if (err < 0)
2746 goto err1;
2747
2748 err = -EINVAL;
2749 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2750 goto err1;
2751
2752 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2753 if (err < 0)
2754 goto err1;
2755
2756 err = -EINVAL;
2757 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2758 goto err2;
2759
2760 err = set->ops->get(set, &elem);
2761 if (err < 0)
2762 goto err2;
2763
2764 set->ops->remove(set, &elem);
2765
2766 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2767 if (set->flags & NFT_SET_MAP)
2768 nft_data_uninit(&elem.data, set->dtype);
2769
2770err2:
2771 nft_data_uninit(&elem.key, desc.type);
2772err1:
2773 return err;
2774}
2775
2776static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2777 const struct nlmsghdr *nlh,
2778 const struct nlattr * const nla[])
2779{
2780 const struct nlattr *attr;
2781 struct nft_set *set;
2782 struct nft_ctx ctx;
2783 int rem, err;
2784
2785 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2786 if (err < 0)
2787 return err;
2788
2789 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2790 if (IS_ERR(set))
2791 return PTR_ERR(set);
2792 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2793 return -EBUSY;
2794
2795 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2796 err = nft_del_setelem(&ctx, set, attr);
2797 if (err < 0)
2798 return err;
2799 }
2800 return 0;
2801}
2802
2803static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2804 [NFT_MSG_NEWTABLE] = {
2805 .call = nf_tables_newtable,
2806 .attr_count = NFTA_TABLE_MAX,
2807 .policy = nft_table_policy,
2808 },
2809 [NFT_MSG_GETTABLE] = {
2810 .call = nf_tables_gettable,
2811 .attr_count = NFTA_TABLE_MAX,
2812 .policy = nft_table_policy,
2813 },
2814 [NFT_MSG_DELTABLE] = {
2815 .call = nf_tables_deltable,
2816 .attr_count = NFTA_TABLE_MAX,
2817 .policy = nft_table_policy,
2818 },
2819 [NFT_MSG_NEWCHAIN] = {
2820 .call = nf_tables_newchain,
2821 .attr_count = NFTA_CHAIN_MAX,
2822 .policy = nft_chain_policy,
2823 },
2824 [NFT_MSG_GETCHAIN] = {
2825 .call = nf_tables_getchain,
2826 .attr_count = NFTA_CHAIN_MAX,
2827 .policy = nft_chain_policy,
2828 },
2829 [NFT_MSG_DELCHAIN] = {
2830 .call = nf_tables_delchain,
2831 .attr_count = NFTA_CHAIN_MAX,
2832 .policy = nft_chain_policy,
2833 },
2834 [NFT_MSG_NEWRULE] = {
2835 .call_batch = nf_tables_newrule,
2836 .attr_count = NFTA_RULE_MAX,
2837 .policy = nft_rule_policy,
2838 },
2839 [NFT_MSG_GETRULE] = {
2840 .call = nf_tables_getrule,
2841 .attr_count = NFTA_RULE_MAX,
2842 .policy = nft_rule_policy,
2843 },
2844 [NFT_MSG_DELRULE] = {
2845 .call_batch = nf_tables_delrule,
2846 .attr_count = NFTA_RULE_MAX,
2847 .policy = nft_rule_policy,
2848 },
2849 [NFT_MSG_NEWSET] = {
2850 .call = nf_tables_newset,
2851 .attr_count = NFTA_SET_MAX,
2852 .policy = nft_set_policy,
2853 },
2854 [NFT_MSG_GETSET] = {
2855 .call = nf_tables_getset,
2856 .attr_count = NFTA_SET_MAX,
2857 .policy = nft_set_policy,
2858 },
2859 [NFT_MSG_DELSET] = {
2860 .call = nf_tables_delset,
2861 .attr_count = NFTA_SET_MAX,
2862 .policy = nft_set_policy,
2863 },
2864 [NFT_MSG_NEWSETELEM] = {
2865 .call = nf_tables_newsetelem,
2866 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2867 .policy = nft_set_elem_list_policy,
2868 },
2869 [NFT_MSG_GETSETELEM] = {
2870 .call = nf_tables_getsetelem,
2871 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2872 .policy = nft_set_elem_list_policy,
2873 },
2874 [NFT_MSG_DELSETELEM] = {
2875 .call = nf_tables_delsetelem,
2876 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2877 .policy = nft_set_elem_list_policy,
2878 },
2879};
2880
2881static const struct nfnetlink_subsystem nf_tables_subsys = {
2882 .name = "nf_tables",
2883 .subsys_id = NFNL_SUBSYS_NFTABLES,
2884 .cb_count = NFT_MSG_MAX,
2885 .cb = nf_tables_cb,
2886 .commit = nf_tables_commit,
2887 .abort = nf_tables_abort,
2888};
2889
2890/*
2891 * Loop detection - walk through the ruleset beginning at the destination chain
2892 * of a new jump until either the source chain is reached (loop) or all
2893 * reachable chains have been traversed.
2894 *
2895 * The loop check is performed whenever a new jump verdict is added to an
2896 * expression or verdict map or a verdict map is bound to a new chain.
2897 */
2898
2899static int nf_tables_check_loops(const struct nft_ctx *ctx,
2900 const struct nft_chain *chain);
2901
2902static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2903 const struct nft_set *set,
2904 const struct nft_set_iter *iter,
2905 const struct nft_set_elem *elem)
2906{
2907 switch (elem->data.verdict) {
2908 case NFT_JUMP:
2909 case NFT_GOTO:
2910 return nf_tables_check_loops(ctx, elem->data.chain);
2911 default:
2912 return 0;
2913 }
2914}
2915
2916static int nf_tables_check_loops(const struct nft_ctx *ctx,
2917 const struct nft_chain *chain)
2918{
2919 const struct nft_rule *rule;
2920 const struct nft_expr *expr, *last;
2921 const struct nft_set *set;
2922 struct nft_set_binding *binding;
2923 struct nft_set_iter iter;
2924
2925 if (ctx->chain == chain)
2926 return -ELOOP;
2927
2928 list_for_each_entry(rule, &chain->rules, list) {
2929 nft_rule_for_each_expr(expr, last, rule) {
2930 const struct nft_data *data = NULL;
2931 int err;
2932
2933 if (!expr->ops->validate)
2934 continue;
2935
2936 err = expr->ops->validate(ctx, expr, &data);
2937 if (err < 0)
2938 return err;
2939
2940 if (data == NULL)
2941 continue;
2942
2943 switch (data->verdict) {
2944 case NFT_JUMP:
2945 case NFT_GOTO:
2946 err = nf_tables_check_loops(ctx, data->chain);
2947 if (err < 0)
2948 return err;
2949 default:
2950 break;
2951 }
2952 }
2953 }
2954
2955 list_for_each_entry(set, &ctx->table->sets, list) {
2956 if (!(set->flags & NFT_SET_MAP) ||
2957 set->dtype != NFT_DATA_VERDICT)
2958 continue;
2959
2960 list_for_each_entry(binding, &set->bindings, list) {
2961 if (binding->chain != chain)
2962 continue;
2963
2964 iter.skip = 0;
2965 iter.count = 0;
2966 iter.err = 0;
2967 iter.fn = nf_tables_loop_check_setelem;
2968
2969 set->ops->walk(ctx, set, &iter);
2970 if (iter.err < 0)
2971 return iter.err;
2972 }
2973 }
2974
2975 return 0;
2976}
2977
2978/**
2979 * nft_validate_input_register - validate an expressions' input register
2980 *
2981 * @reg: the register number
2982 *
2983 * Validate that the input register is one of the general purpose
2984 * registers.
2985 */
2986int nft_validate_input_register(enum nft_registers reg)
2987{
2988 if (reg <= NFT_REG_VERDICT)
2989 return -EINVAL;
2990 if (reg > NFT_REG_MAX)
2991 return -ERANGE;
2992 return 0;
2993}
2994EXPORT_SYMBOL_GPL(nft_validate_input_register);
2995
2996/**
2997 * nft_validate_output_register - validate an expressions' output register
2998 *
2999 * @reg: the register number
3000 *
3001 * Validate that the output register is one of the general purpose
3002 * registers or the verdict register.
3003 */
3004int nft_validate_output_register(enum nft_registers reg)
3005{
3006 if (reg < NFT_REG_VERDICT)
3007 return -EINVAL;
3008 if (reg > NFT_REG_MAX)
3009 return -ERANGE;
3010 return 0;
3011}
3012EXPORT_SYMBOL_GPL(nft_validate_output_register);
3013
3014/**
3015 * nft_validate_data_load - validate an expressions' data load
3016 *
3017 * @ctx: context of the expression performing the load
3018 * @reg: the destination register number
3019 * @data: the data to load
3020 * @type: the data type
3021 *
3022 * Validate that a data load uses the appropriate data type for
3023 * the destination register. A value of NULL for the data means
3024 * that its runtime gathered data, which is always of type
3025 * NFT_DATA_VALUE.
3026 */
3027int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
3028 const struct nft_data *data,
3029 enum nft_data_types type)
3030{
3031 int err;
3032
3033 switch (reg) {
3034 case NFT_REG_VERDICT:
3035 if (data == NULL || type != NFT_DATA_VERDICT)
3036 return -EINVAL;
3037
3038 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3039 err = nf_tables_check_loops(ctx, data->chain);
3040 if (err < 0)
3041 return err;
3042
3043 if (ctx->chain->level + 1 > data->chain->level) {
3044 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3045 return -EMLINK;
3046 data->chain->level = ctx->chain->level + 1;
3047 }
3048 }
3049
3050 return 0;
3051 default:
3052 if (data != NULL && type != NFT_DATA_VALUE)
3053 return -EINVAL;
3054 return 0;
3055 }
3056}
3057EXPORT_SYMBOL_GPL(nft_validate_data_load);
3058
3059static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3060 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3061 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3062 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3063};
3064
3065static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3066 struct nft_data_desc *desc, const struct nlattr *nla)
3067{
3068 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3069 struct nft_chain *chain;
3070 int err;
3071
3072 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3073 if (err < 0)
3074 return err;
3075
3076 if (!tb[NFTA_VERDICT_CODE])
3077 return -EINVAL;
3078 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3079
3080 switch (data->verdict) {
3081 case NF_ACCEPT:
3082 case NF_DROP:
3083 case NF_QUEUE:
3084 case NFT_CONTINUE:
3085 case NFT_BREAK:
3086 case NFT_RETURN:
3087 desc->len = sizeof(data->verdict);
3088 break;
3089 case NFT_JUMP:
3090 case NFT_GOTO:
3091 if (!tb[NFTA_VERDICT_CHAIN])
3092 return -EINVAL;
3093 chain = nf_tables_chain_lookup(ctx->table,
3094 tb[NFTA_VERDICT_CHAIN]);
3095 if (IS_ERR(chain))
3096 return PTR_ERR(chain);
3097 if (chain->flags & NFT_BASE_CHAIN)
3098 return -EOPNOTSUPP;
3099
3100 chain->use++;
3101 data->chain = chain;
3102 desc->len = sizeof(data);
3103 break;
3104 default:
3105 return -EINVAL;
3106 }
3107
3108 desc->type = NFT_DATA_VERDICT;
3109 return 0;
3110}
3111
3112static void nft_verdict_uninit(const struct nft_data *data)
3113{
3114 switch (data->verdict) {
3115 case NFT_JUMP:
3116 case NFT_GOTO:
3117 data->chain->use--;
3118 break;
3119 }
3120}
3121
3122static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3123{
3124 struct nlattr *nest;
3125
3126 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3127 if (!nest)
3128 goto nla_put_failure;
3129
3130 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3131 goto nla_put_failure;
3132
3133 switch (data->verdict) {
3134 case NFT_JUMP:
3135 case NFT_GOTO:
3136 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3137 goto nla_put_failure;
3138 }
3139 nla_nest_end(skb, nest);
3140 return 0;
3141
3142nla_put_failure:
3143 return -1;
3144}
3145
3146static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3147 struct nft_data_desc *desc, const struct nlattr *nla)
3148{
3149 unsigned int len;
3150
3151 len = nla_len(nla);
3152 if (len == 0)
3153 return -EINVAL;
3154 if (len > sizeof(data->data))
3155 return -EOVERFLOW;
3156
3157 nla_memcpy(data->data, nla, sizeof(data->data));
3158 desc->type = NFT_DATA_VALUE;
3159 desc->len = len;
3160 return 0;
3161}
3162
3163static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3164 unsigned int len)
3165{
3166 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3167}
3168
3169static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3170 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3171 .len = FIELD_SIZEOF(struct nft_data, data) },
3172 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3173};
3174
3175/**
3176 * nft_data_init - parse nf_tables data netlink attributes
3177 *
3178 * @ctx: context of the expression using the data
3179 * @data: destination struct nft_data
3180 * @desc: data description
3181 * @nla: netlink attribute containing data
3182 *
3183 * Parse the netlink data attributes and initialize a struct nft_data.
3184 * The type and length of data are returned in the data description.
3185 *
3186 * The caller can indicate that it only wants to accept data of type
3187 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3188 */
3189int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3190 struct nft_data_desc *desc, const struct nlattr *nla)
3191{
3192 struct nlattr *tb[NFTA_DATA_MAX + 1];
3193 int err;
3194
3195 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3196 if (err < 0)
3197 return err;
3198
3199 if (tb[NFTA_DATA_VALUE])
3200 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3201 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3202 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3203 return -EINVAL;
3204}
3205EXPORT_SYMBOL_GPL(nft_data_init);
3206
3207/**
3208 * nft_data_uninit - release a nft_data item
3209 *
3210 * @data: struct nft_data to release
3211 * @type: type of data
3212 *
3213 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3214 * all others need to be released by calling this function.
3215 */
3216void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3217{
3218 switch (type) {
3219 case NFT_DATA_VALUE:
3220 return;
3221 case NFT_DATA_VERDICT:
3222 return nft_verdict_uninit(data);
3223 default:
3224 WARN_ON(1);
3225 }
3226}
3227EXPORT_SYMBOL_GPL(nft_data_uninit);
3228
3229int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3230 enum nft_data_types type, unsigned int len)
3231{
3232 struct nlattr *nest;
3233 int err;
3234
3235 nest = nla_nest_start(skb, attr);
3236 if (nest == NULL)
3237 return -1;
3238
3239 switch (type) {
3240 case NFT_DATA_VALUE:
3241 err = nft_value_dump(skb, data, len);
3242 break;
3243 case NFT_DATA_VERDICT:
3244 err = nft_verdict_dump(skb, data);
3245 break;
3246 default:
3247 err = -EINVAL;
3248 WARN_ON(1);
3249 }
3250
3251 nla_nest_end(skb, nest);
3252 return err;
3253}
3254EXPORT_SYMBOL_GPL(nft_data_dump);
3255
3256static int nf_tables_init_net(struct net *net)
3257{
3258 INIT_LIST_HEAD(&net->nft.af_info);
3259 INIT_LIST_HEAD(&net->nft.commit_list);
3260 return 0;
3261}
3262
3263static struct pernet_operations nf_tables_net_ops = {
3264 .init = nf_tables_init_net,
3265};
3266
3267static int __init nf_tables_module_init(void)
3268{
3269 int err;
3270
3271 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3272 GFP_KERNEL);
3273 if (info == NULL) {
3274 err = -ENOMEM;
3275 goto err1;
3276 }
3277
3278 err = nf_tables_core_module_init();
3279 if (err < 0)
3280 goto err2;
3281
3282 err = nfnetlink_subsys_register(&nf_tables_subsys);
3283 if (err < 0)
3284 goto err3;
3285
3286 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3287 return register_pernet_subsys(&nf_tables_net_ops);
3288err3:
3289 nf_tables_core_module_exit();
3290err2:
3291 kfree(info);
3292err1:
3293 return err;
3294}
3295
3296static void __exit nf_tables_module_exit(void)
3297{
3298 unregister_pernet_subsys(&nf_tables_net_ops);
3299 nfnetlink_subsys_unregister(&nf_tables_subsys);
3300 nf_tables_core_module_exit();
3301 kfree(info);
3302}
3303
3304module_init(nf_tables_module_init);
3305module_exit(nf_tables_module_exit);
3306
3307MODULE_LICENSE("GPL");
3308MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3309MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFTABLES);
diff --git a/net/netfilter/nf_tables_core.c b/net/netfilter/nf_tables_core.c
new file mode 100644
index 000000000000..cb9e685caae1
--- /dev/null
+++ b/net/netfilter/nf_tables_core.c
@@ -0,0 +1,270 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/module.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/rculist.h>
15#include <linux/skbuff.h>
16#include <linux/netlink.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter/nfnetlink.h>
19#include <linux/netfilter/nf_tables.h>
20#include <net/netfilter/nf_tables_core.h>
21#include <net/netfilter/nf_tables.h>
22#include <net/netfilter/nf_log.h>
23
24static void nft_cmp_fast_eval(const struct nft_expr *expr,
25 struct nft_data data[NFT_REG_MAX + 1])
26{
27 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
28 u32 mask;
29
30 mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - priv->len);
31 if ((data[priv->sreg].data[0] & mask) == priv->data)
32 return;
33 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
34}
35
36static bool nft_payload_fast_eval(const struct nft_expr *expr,
37 struct nft_data data[NFT_REG_MAX + 1],
38 const struct nft_pktinfo *pkt)
39{
40 const struct nft_payload *priv = nft_expr_priv(expr);
41 const struct sk_buff *skb = pkt->skb;
42 struct nft_data *dest = &data[priv->dreg];
43 unsigned char *ptr;
44
45 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
46 ptr = skb_network_header(skb);
47 else
48 ptr = skb_network_header(skb) + pkt->xt.thoff;
49
50 ptr += priv->offset;
51
52 if (unlikely(ptr + priv->len >= skb_tail_pointer(skb)))
53 return false;
54
55 if (priv->len == 2)
56 *(u16 *)dest->data = *(u16 *)ptr;
57 else if (priv->len == 4)
58 *(u32 *)dest->data = *(u32 *)ptr;
59 else
60 *(u8 *)dest->data = *(u8 *)ptr;
61 return true;
62}
63
64struct nft_jumpstack {
65 const struct nft_chain *chain;
66 const struct nft_rule *rule;
67 int rulenum;
68};
69
70static inline void
71nft_chain_stats(const struct nft_chain *this, const struct nft_pktinfo *pkt,
72 struct nft_jumpstack *jumpstack, unsigned int stackptr)
73{
74 struct nft_stats __percpu *stats;
75 const struct nft_chain *chain = stackptr ? jumpstack[0].chain : this;
76
77 rcu_read_lock_bh();
78 stats = rcu_dereference(nft_base_chain(chain)->stats);
79 __this_cpu_inc(stats->pkts);
80 __this_cpu_add(stats->bytes, pkt->skb->len);
81 rcu_read_unlock_bh();
82}
83
84enum nft_trace {
85 NFT_TRACE_RULE,
86 NFT_TRACE_RETURN,
87 NFT_TRACE_POLICY,
88};
89
90static const char *const comments[] = {
91 [NFT_TRACE_RULE] = "rule",
92 [NFT_TRACE_RETURN] = "return",
93 [NFT_TRACE_POLICY] = "policy",
94};
95
96static struct nf_loginfo trace_loginfo = {
97 .type = NF_LOG_TYPE_LOG,
98 .u = {
99 .log = {
100 .level = 4,
101 .logflags = NF_LOG_MASK,
102 },
103 },
104};
105
106static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
107 const struct nft_chain *chain,
108 int rulenum, enum nft_trace type)
109{
110 struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
111
112 nf_log_packet(net, pkt->xt.family, pkt->hooknum, pkt->skb, pkt->in,
113 pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ",
114 chain->table->name, chain->name, comments[type],
115 rulenum);
116}
117
118unsigned int
119nft_do_chain_pktinfo(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)
120{
121 const struct nft_chain *chain = ops->priv;
122 const struct nft_rule *rule;
123 const struct nft_expr *expr, *last;
124 struct nft_data data[NFT_REG_MAX + 1];
125 unsigned int stackptr = 0;
126 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
127 int rulenum = 0;
128 /*
129 * Cache cursor to avoid problems in case that the cursor is updated
130 * while traversing the ruleset.
131 */
132 unsigned int gencursor = ACCESS_ONCE(chain->net->nft.gencursor);
133
134do_chain:
135 rule = list_entry(&chain->rules, struct nft_rule, list);
136next_rule:
137 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
138 list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
139
140 /* This rule is not active, skip. */
141 if (unlikely(rule->genmask & (1 << gencursor)))
142 continue;
143
144 rulenum++;
145
146 nft_rule_for_each_expr(expr, last, rule) {
147 if (expr->ops == &nft_cmp_fast_ops)
148 nft_cmp_fast_eval(expr, data);
149 else if (expr->ops != &nft_payload_fast_ops ||
150 !nft_payload_fast_eval(expr, data, pkt))
151 expr->ops->eval(expr, data, pkt);
152
153 if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE)
154 break;
155 }
156
157 switch (data[NFT_REG_VERDICT].verdict) {
158 case NFT_BREAK:
159 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
160 /* fall through */
161 case NFT_CONTINUE:
162 continue;
163 }
164 break;
165 }
166
167 switch (data[NFT_REG_VERDICT].verdict) {
168 case NF_ACCEPT:
169 case NF_DROP:
170 case NF_QUEUE:
171 if (unlikely(pkt->skb->nf_trace))
172 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
173
174 return data[NFT_REG_VERDICT].verdict;
175 case NFT_JUMP:
176 if (unlikely(pkt->skb->nf_trace))
177 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
178
179 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
180 jumpstack[stackptr].chain = chain;
181 jumpstack[stackptr].rule = rule;
182 jumpstack[stackptr].rulenum = rulenum;
183 stackptr++;
184 /* fall through */
185 case NFT_GOTO:
186 chain = data[NFT_REG_VERDICT].chain;
187 goto do_chain;
188 case NFT_RETURN:
189 if (unlikely(pkt->skb->nf_trace))
190 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
191
192 /* fall through */
193 case NFT_CONTINUE:
194 break;
195 default:
196 WARN_ON(1);
197 }
198
199 if (stackptr > 0) {
200 if (unlikely(pkt->skb->nf_trace))
201 nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_RETURN);
202
203 stackptr--;
204 chain = jumpstack[stackptr].chain;
205 rule = jumpstack[stackptr].rule;
206 rulenum = jumpstack[stackptr].rulenum;
207 goto next_rule;
208 }
209 nft_chain_stats(chain, pkt, jumpstack, stackptr);
210
211 if (unlikely(pkt->skb->nf_trace))
212 nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_POLICY);
213
214 return nft_base_chain(chain)->policy;
215}
216EXPORT_SYMBOL_GPL(nft_do_chain_pktinfo);
217
218int __init nf_tables_core_module_init(void)
219{
220 int err;
221
222 err = nft_immediate_module_init();
223 if (err < 0)
224 goto err1;
225
226 err = nft_cmp_module_init();
227 if (err < 0)
228 goto err2;
229
230 err = nft_lookup_module_init();
231 if (err < 0)
232 goto err3;
233
234 err = nft_bitwise_module_init();
235 if (err < 0)
236 goto err4;
237
238 err = nft_byteorder_module_init();
239 if (err < 0)
240 goto err5;
241
242 err = nft_payload_module_init();
243 if (err < 0)
244 goto err6;
245
246 return 0;
247
248err6:
249 nft_byteorder_module_exit();
250err5:
251 nft_bitwise_module_exit();
252err4:
253 nft_lookup_module_exit();
254err3:
255 nft_cmp_module_exit();
256err2:
257 nft_immediate_module_exit();
258err1:
259 return err;
260}
261
262void nf_tables_core_module_exit(void)
263{
264 nft_payload_module_exit();
265 nft_byteorder_module_exit();
266 nft_bitwise_module_exit();
267 nft_lookup_module_exit();
268 nft_cmp_module_exit();
269 nft_immediate_module_exit();
270}
diff --git a/net/netfilter/nfnetlink.c b/net/netfilter/nfnetlink.c
index 572d87dc116f..046aa13b4fea 100644
--- a/net/netfilter/nfnetlink.c
+++ b/net/netfilter/nfnetlink.c
@@ -147,9 +147,6 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
147 const struct nfnetlink_subsystem *ss; 147 const struct nfnetlink_subsystem *ss;
148 int type, err; 148 int type, err;
149 149
150 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
151 return -EPERM;
152
153 /* All the messages must at least contain nfgenmsg */ 150 /* All the messages must at least contain nfgenmsg */
154 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg)) 151 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
155 return 0; 152 return 0;
@@ -217,9 +214,181 @@ replay:
217 } 214 }
218} 215}
219 216
217static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,
218 u_int16_t subsys_id)
219{
220 struct sk_buff *nskb, *oskb = skb;
221 struct net *net = sock_net(skb->sk);
222 const struct nfnetlink_subsystem *ss;
223 const struct nfnl_callback *nc;
224 bool success = true, done = false;
225 int err;
226
227 if (subsys_id >= NFNL_SUBSYS_COUNT)
228 return netlink_ack(skb, nlh, -EINVAL);
229replay:
230 nskb = netlink_skb_clone(oskb, GFP_KERNEL);
231 if (!nskb)
232 return netlink_ack(oskb, nlh, -ENOMEM);
233
234 nskb->sk = oskb->sk;
235 skb = nskb;
236
237 nfnl_lock(subsys_id);
238 ss = rcu_dereference_protected(table[subsys_id].subsys,
239 lockdep_is_held(&table[subsys_id].mutex));
240 if (!ss) {
241#ifdef CONFIG_MODULES
242 nfnl_unlock(subsys_id);
243 request_module("nfnetlink-subsys-%d", subsys_id);
244 nfnl_lock(subsys_id);
245 ss = rcu_dereference_protected(table[subsys_id].subsys,
246 lockdep_is_held(&table[subsys_id].mutex));
247 if (!ss)
248#endif
249 {
250 nfnl_unlock(subsys_id);
251 kfree_skb(nskb);
252 return netlink_ack(skb, nlh, -EOPNOTSUPP);
253 }
254 }
255
256 if (!ss->commit || !ss->abort) {
257 nfnl_unlock(subsys_id);
258 kfree_skb(nskb);
259 return netlink_ack(skb, nlh, -EOPNOTSUPP);
260 }
261
262 while (skb->len >= nlmsg_total_size(0)) {
263 int msglen, type;
264
265 nlh = nlmsg_hdr(skb);
266 err = 0;
267
268 if (nlh->nlmsg_len < NLMSG_HDRLEN) {
269 err = -EINVAL;
270 goto ack;
271 }
272
273 /* Only requests are handled by the kernel */
274 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
275 err = -EINVAL;
276 goto ack;
277 }
278
279 type = nlh->nlmsg_type;
280 if (type == NFNL_MSG_BATCH_BEGIN) {
281 /* Malformed: Batch begin twice */
282 success = false;
283 goto done;
284 } else if (type == NFNL_MSG_BATCH_END) {
285 done = true;
286 goto done;
287 } else if (type < NLMSG_MIN_TYPE) {
288 err = -EINVAL;
289 goto ack;
290 }
291
292 /* We only accept a batch with messages for the same
293 * subsystem.
294 */
295 if (NFNL_SUBSYS_ID(type) != subsys_id) {
296 err = -EINVAL;
297 goto ack;
298 }
299
300 nc = nfnetlink_find_client(type, ss);
301 if (!nc) {
302 err = -EINVAL;
303 goto ack;
304 }
305
306 {
307 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
308 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
309 struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
310 struct nlattr *attr = (void *)nlh + min_len;
311 int attrlen = nlh->nlmsg_len - min_len;
312
313 err = nla_parse(cda, ss->cb[cb_id].attr_count,
314 attr, attrlen, ss->cb[cb_id].policy);
315 if (err < 0)
316 goto ack;
317
318 if (nc->call_batch) {
319 err = nc->call_batch(net->nfnl, skb, nlh,
320 (const struct nlattr **)cda);
321 }
322
323 /* The lock was released to autoload some module, we
324 * have to abort and start from scratch using the
325 * original skb.
326 */
327 if (err == -EAGAIN) {
328 ss->abort(skb);
329 nfnl_unlock(subsys_id);
330 kfree_skb(nskb);
331 goto replay;
332 }
333 }
334ack:
335 if (nlh->nlmsg_flags & NLM_F_ACK || err) {
336 /* We don't stop processing the batch on errors, thus,
337 * userspace gets all the errors that the batch
338 * triggers.
339 */
340 netlink_ack(skb, nlh, err);
341 if (err)
342 success = false;
343 }
344
345 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
346 if (msglen > skb->len)
347 msglen = skb->len;
348 skb_pull(skb, msglen);
349 }
350done:
351 if (success && done)
352 ss->commit(skb);
353 else
354 ss->abort(skb);
355
356 nfnl_unlock(subsys_id);
357 kfree_skb(nskb);
358}
359
220static void nfnetlink_rcv(struct sk_buff *skb) 360static void nfnetlink_rcv(struct sk_buff *skb)
221{ 361{
222 netlink_rcv_skb(skb, &nfnetlink_rcv_msg); 362 struct nlmsghdr *nlh = nlmsg_hdr(skb);
363 struct net *net = sock_net(skb->sk);
364 int msglen;
365
366 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
367 skb->len < nlh->nlmsg_len)
368 return;
369
370 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) {
371 netlink_ack(skb, nlh, -EPERM);
372 return;
373 }
374
375 if (nlh->nlmsg_type == NFNL_MSG_BATCH_BEGIN) {
376 struct nfgenmsg *nfgenmsg;
377
378 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
379 if (msglen > skb->len)
380 msglen = skb->len;
381
382 if (nlh->nlmsg_len < NLMSG_HDRLEN ||
383 skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))
384 return;
385
386 nfgenmsg = nlmsg_data(nlh);
387 skb_pull(skb, msglen);
388 nfnetlink_rcv_batch(skb, nlh, nfgenmsg->res_id);
389 } else {
390 netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
391 }
223} 392}
224 393
225#ifdef CONFIG_MODULES 394#ifdef CONFIG_MODULES
diff --git a/net/netfilter/nfnetlink_cttimeout.c b/net/netfilter/nfnetlink_cttimeout.c
index 50580494148d..476accd17145 100644
--- a/net/netfilter/nfnetlink_cttimeout.c
+++ b/net/netfilter/nfnetlink_cttimeout.c
@@ -49,10 +49,8 @@ static const struct nla_policy cttimeout_nla_policy[CTA_TIMEOUT_MAX+1] = {
49}; 49};
50 50
51static int 51static int
52ctnl_timeout_parse_policy(struct ctnl_timeout *timeout, 52ctnl_timeout_parse_policy(void *timeouts, struct nf_conntrack_l4proto *l4proto,
53 struct nf_conntrack_l4proto *l4proto, 53 struct net *net, const struct nlattr *attr)
54 struct net *net,
55 const struct nlattr *attr)
56{ 54{
57 int ret = 0; 55 int ret = 0;
58 56
@@ -64,8 +62,7 @@ ctnl_timeout_parse_policy(struct ctnl_timeout *timeout,
64 if (ret < 0) 62 if (ret < 0)
65 return ret; 63 return ret;
66 64
67 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, 65 ret = l4proto->ctnl_timeout.nlattr_to_obj(tb, net, timeouts);
68 &timeout->data);
69 } 66 }
70 return ret; 67 return ret;
71} 68}
@@ -123,7 +120,8 @@ cttimeout_new_timeout(struct sock *ctnl, struct sk_buff *skb,
123 goto err_proto_put; 120 goto err_proto_put;
124 } 121 }
125 122
126 ret = ctnl_timeout_parse_policy(matching, l4proto, net, 123 ret = ctnl_timeout_parse_policy(&matching->data,
124 l4proto, net,
127 cda[CTA_TIMEOUT_DATA]); 125 cda[CTA_TIMEOUT_DATA]);
128 return ret; 126 return ret;
129 } 127 }
@@ -138,7 +136,7 @@ cttimeout_new_timeout(struct sock *ctnl, struct sk_buff *skb,
138 goto err_proto_put; 136 goto err_proto_put;
139 } 137 }
140 138
141 ret = ctnl_timeout_parse_policy(timeout, l4proto, net, 139 ret = ctnl_timeout_parse_policy(&timeout->data, l4proto, net,
142 cda[CTA_TIMEOUT_DATA]); 140 cda[CTA_TIMEOUT_DATA]);
143 if (ret < 0) 141 if (ret < 0)
144 goto err; 142 goto err;
@@ -342,6 +340,147 @@ cttimeout_del_timeout(struct sock *ctnl, struct sk_buff *skb,
342 return ret; 340 return ret;
343} 341}
344 342
343static int
344cttimeout_default_set(struct sock *ctnl, struct sk_buff *skb,
345 const struct nlmsghdr *nlh,
346 const struct nlattr * const cda[])
347{
348 __u16 l3num;
349 __u8 l4num;
350 struct nf_conntrack_l4proto *l4proto;
351 struct net *net = sock_net(skb->sk);
352 unsigned int *timeouts;
353 int ret;
354
355 if (!cda[CTA_TIMEOUT_L3PROTO] ||
356 !cda[CTA_TIMEOUT_L4PROTO] ||
357 !cda[CTA_TIMEOUT_DATA])
358 return -EINVAL;
359
360 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO]));
361 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]);
362 l4proto = nf_ct_l4proto_find_get(l3num, l4num);
363
364 /* This protocol is not supported, skip. */
365 if (l4proto->l4proto != l4num) {
366 ret = -EOPNOTSUPP;
367 goto err;
368 }
369
370 timeouts = l4proto->get_timeouts(net);
371
372 ret = ctnl_timeout_parse_policy(timeouts, l4proto, net,
373 cda[CTA_TIMEOUT_DATA]);
374 if (ret < 0)
375 goto err;
376
377 nf_ct_l4proto_put(l4proto);
378 return 0;
379err:
380 nf_ct_l4proto_put(l4proto);
381 return ret;
382}
383
384static int
385cttimeout_default_fill_info(struct net *net, struct sk_buff *skb, u32 portid,
386 u32 seq, u32 type, int event,
387 struct nf_conntrack_l4proto *l4proto)
388{
389 struct nlmsghdr *nlh;
390 struct nfgenmsg *nfmsg;
391 unsigned int flags = portid ? NLM_F_MULTI : 0;
392
393 event |= NFNL_SUBSYS_CTNETLINK_TIMEOUT << 8;
394 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
395 if (nlh == NULL)
396 goto nlmsg_failure;
397
398 nfmsg = nlmsg_data(nlh);
399 nfmsg->nfgen_family = AF_UNSPEC;
400 nfmsg->version = NFNETLINK_V0;
401 nfmsg->res_id = 0;
402
403 if (nla_put_be16(skb, CTA_TIMEOUT_L3PROTO, htons(l4proto->l3proto)) ||
404 nla_put_u8(skb, CTA_TIMEOUT_L4PROTO, l4proto->l4proto))
405 goto nla_put_failure;
406
407 if (likely(l4proto->ctnl_timeout.obj_to_nlattr)) {
408 struct nlattr *nest_parms;
409 unsigned int *timeouts = l4proto->get_timeouts(net);
410 int ret;
411
412 nest_parms = nla_nest_start(skb,
413 CTA_TIMEOUT_DATA | NLA_F_NESTED);
414 if (!nest_parms)
415 goto nla_put_failure;
416
417 ret = l4proto->ctnl_timeout.obj_to_nlattr(skb, timeouts);
418 if (ret < 0)
419 goto nla_put_failure;
420
421 nla_nest_end(skb, nest_parms);
422 }
423
424 nlmsg_end(skb, nlh);
425 return skb->len;
426
427nlmsg_failure:
428nla_put_failure:
429 nlmsg_cancel(skb, nlh);
430 return -1;
431}
432
433static int cttimeout_default_get(struct sock *ctnl, struct sk_buff *skb,
434 const struct nlmsghdr *nlh,
435 const struct nlattr * const cda[])
436{
437 __u16 l3num;
438 __u8 l4num;
439 struct nf_conntrack_l4proto *l4proto;
440 struct net *net = sock_net(skb->sk);
441 struct sk_buff *skb2;
442 int ret, err;
443
444 if (!cda[CTA_TIMEOUT_L3PROTO] || !cda[CTA_TIMEOUT_L4PROTO])
445 return -EINVAL;
446
447 l3num = ntohs(nla_get_be16(cda[CTA_TIMEOUT_L3PROTO]));
448 l4num = nla_get_u8(cda[CTA_TIMEOUT_L4PROTO]);
449 l4proto = nf_ct_l4proto_find_get(l3num, l4num);
450
451 /* This protocol is not supported, skip. */
452 if (l4proto->l4proto != l4num) {
453 err = -EOPNOTSUPP;
454 goto err;
455 }
456
457 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
458 if (skb2 == NULL) {
459 err = -ENOMEM;
460 goto err;
461 }
462
463 ret = cttimeout_default_fill_info(net, skb2, NETLINK_CB(skb).portid,
464 nlh->nlmsg_seq,
465 NFNL_MSG_TYPE(nlh->nlmsg_type),
466 IPCTNL_MSG_TIMEOUT_DEFAULT_SET,
467 l4proto);
468 if (ret <= 0) {
469 kfree_skb(skb2);
470 err = -ENOMEM;
471 goto err;
472 }
473 ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
474 if (ret > 0)
475 ret = 0;
476
477 /* this avoids a loop in nfnetlink. */
478 return ret == -EAGAIN ? -ENOBUFS : ret;
479err:
480 nf_ct_l4proto_put(l4proto);
481 return err;
482}
483
345#ifdef CONFIG_NF_CONNTRACK_TIMEOUT 484#ifdef CONFIG_NF_CONNTRACK_TIMEOUT
346static struct ctnl_timeout *ctnl_timeout_find_get(const char *name) 485static struct ctnl_timeout *ctnl_timeout_find_get(const char *name)
347{ 486{
@@ -384,6 +523,12 @@ static const struct nfnl_callback cttimeout_cb[IPCTNL_MSG_TIMEOUT_MAX] = {
384 [IPCTNL_MSG_TIMEOUT_DELETE] = { .call = cttimeout_del_timeout, 523 [IPCTNL_MSG_TIMEOUT_DELETE] = { .call = cttimeout_del_timeout,
385 .attr_count = CTA_TIMEOUT_MAX, 524 .attr_count = CTA_TIMEOUT_MAX,
386 .policy = cttimeout_nla_policy }, 525 .policy = cttimeout_nla_policy },
526 [IPCTNL_MSG_TIMEOUT_DEFAULT_SET]= { .call = cttimeout_default_set,
527 .attr_count = CTA_TIMEOUT_MAX,
528 .policy = cttimeout_nla_policy },
529 [IPCTNL_MSG_TIMEOUT_DEFAULT_GET]= { .call = cttimeout_default_get,
530 .attr_count = CTA_TIMEOUT_MAX,
531 .policy = cttimeout_nla_policy },
387}; 532};
388 533
389static const struct nfnetlink_subsystem cttimeout_subsys = { 534static const struct nfnetlink_subsystem cttimeout_subsys = {
diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index d92cc317bf8b..a155d19a225e 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -319,7 +319,8 @@ nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
319} 319}
320 320
321static struct sk_buff * 321static struct sk_buff *
322nfulnl_alloc_skb(u32 peer_portid, unsigned int inst_size, unsigned int pkt_size) 322nfulnl_alloc_skb(struct net *net, u32 peer_portid, unsigned int inst_size,
323 unsigned int pkt_size)
323{ 324{
324 struct sk_buff *skb; 325 struct sk_buff *skb;
325 unsigned int n; 326 unsigned int n;
@@ -328,13 +329,13 @@ nfulnl_alloc_skb(u32 peer_portid, unsigned int inst_size, unsigned int pkt_size)
328 * message. WARNING: has to be <= 128k due to slab restrictions */ 329 * message. WARNING: has to be <= 128k due to slab restrictions */
329 330
330 n = max(inst_size, pkt_size); 331 n = max(inst_size, pkt_size);
331 skb = nfnetlink_alloc_skb(&init_net, n, peer_portid, GFP_ATOMIC); 332 skb = nfnetlink_alloc_skb(net, n, peer_portid, GFP_ATOMIC);
332 if (!skb) { 333 if (!skb) {
333 if (n > pkt_size) { 334 if (n > pkt_size) {
334 /* try to allocate only as much as we need for current 335 /* try to allocate only as much as we need for current
335 * packet */ 336 * packet */
336 337
337 skb = nfnetlink_alloc_skb(&init_net, pkt_size, 338 skb = nfnetlink_alloc_skb(net, pkt_size,
338 peer_portid, GFP_ATOMIC); 339 peer_portid, GFP_ATOMIC);
339 if (!skb) 340 if (!skb)
340 pr_err("nfnetlink_log: can't even alloc %u bytes\n", 341 pr_err("nfnetlink_log: can't even alloc %u bytes\n",
@@ -702,8 +703,8 @@ nfulnl_log_packet(struct net *net,
702 } 703 }
703 704
704 if (!inst->skb) { 705 if (!inst->skb) {
705 inst->skb = nfulnl_alloc_skb(inst->peer_portid, inst->nlbufsiz, 706 inst->skb = nfulnl_alloc_skb(net, inst->peer_portid,
706 size); 707 inst->nlbufsiz, size);
707 if (!inst->skb) 708 if (!inst->skb)
708 goto alloc_failure; 709 goto alloc_failure;
709 } 710 }
@@ -1052,6 +1053,7 @@ static void __net_exit nfnl_log_net_exit(struct net *net)
1052#ifdef CONFIG_PROC_FS 1053#ifdef CONFIG_PROC_FS
1053 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter); 1054 remove_proc_entry("nfnetlink_log", net->nf.proc_netfilter);
1054#endif 1055#endif
1056 nf_log_unset(net, &nfulnl_logger);
1055} 1057}
1056 1058
1057static struct pernet_operations nfnl_log_net_ops = { 1059static struct pernet_operations nfnl_log_net_ops = {
diff --git a/net/netfilter/nfnetlink_queue_core.c b/net/netfilter/nfnetlink_queue_core.c
index ae2e5c11d01a..21258cf70091 100644
--- a/net/netfilter/nfnetlink_queue_core.c
+++ b/net/netfilter/nfnetlink_queue_core.c
@@ -298,7 +298,7 @@ nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
298} 298}
299 299
300static struct sk_buff * 300static struct sk_buff *
301nfqnl_build_packet_message(struct nfqnl_instance *queue, 301nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
302 struct nf_queue_entry *entry, 302 struct nf_queue_entry *entry,
303 __be32 **packet_id_ptr) 303 __be32 **packet_id_ptr)
304{ 304{
@@ -372,7 +372,7 @@ nfqnl_build_packet_message(struct nfqnl_instance *queue,
372 if (queue->flags & NFQA_CFG_F_CONNTRACK) 372 if (queue->flags & NFQA_CFG_F_CONNTRACK)
373 ct = nfqnl_ct_get(entskb, &size, &ctinfo); 373 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
374 374
375 skb = nfnetlink_alloc_skb(&init_net, size, queue->peer_portid, 375 skb = nfnetlink_alloc_skb(net, size, queue->peer_portid,
376 GFP_ATOMIC); 376 GFP_ATOMIC);
377 if (!skb) 377 if (!skb)
378 return NULL; 378 return NULL;
@@ -525,7 +525,7 @@ __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
525 __be32 *packet_id_ptr; 525 __be32 *packet_id_ptr;
526 int failopen = 0; 526 int failopen = 0;
527 527
528 nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr); 528 nskb = nfqnl_build_packet_message(net, queue, entry, &packet_id_ptr);
529 if (nskb == NULL) { 529 if (nskb == NULL) {
530 err = -ENOMEM; 530 err = -ENOMEM;
531 goto err_out; 531 goto err_out;
diff --git a/net/netfilter/nft_bitwise.c b/net/netfilter/nft_bitwise.c
new file mode 100644
index 000000000000..4fb6ee2c1106
--- /dev/null
+++ b/net/netfilter/nft_bitwise.c
@@ -0,0 +1,146 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_bitwise {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 u8 len;
24 struct nft_data mask;
25 struct nft_data xor;
26};
27
28static void nft_bitwise_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 const struct nft_bitwise *priv = nft_expr_priv(expr);
33 const struct nft_data *src = &data[priv->sreg];
34 struct nft_data *dst = &data[priv->dreg];
35 unsigned int i;
36
37 for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++) {
38 dst->data[i] = (src->data[i] & priv->mask.data[i]) ^
39 priv->xor.data[i];
40 }
41}
42
43static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = {
44 [NFTA_BITWISE_SREG] = { .type = NLA_U32 },
45 [NFTA_BITWISE_DREG] = { .type = NLA_U32 },
46 [NFTA_BITWISE_LEN] = { .type = NLA_U32 },
47 [NFTA_BITWISE_MASK] = { .type = NLA_NESTED },
48 [NFTA_BITWISE_XOR] = { .type = NLA_NESTED },
49};
50
51static int nft_bitwise_init(const struct nft_ctx *ctx,
52 const struct nft_expr *expr,
53 const struct nlattr * const tb[])
54{
55 struct nft_bitwise *priv = nft_expr_priv(expr);
56 struct nft_data_desc d1, d2;
57 int err;
58
59 if (tb[NFTA_BITWISE_SREG] == NULL ||
60 tb[NFTA_BITWISE_DREG] == NULL ||
61 tb[NFTA_BITWISE_LEN] == NULL ||
62 tb[NFTA_BITWISE_MASK] == NULL ||
63 tb[NFTA_BITWISE_XOR] == NULL)
64 return -EINVAL;
65
66 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_SREG]));
67 err = nft_validate_input_register(priv->sreg);
68 if (err < 0)
69 return err;
70
71 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_DREG]));
72 err = nft_validate_output_register(priv->dreg);
73 if (err < 0)
74 return err;
75 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
76 if (err < 0)
77 return err;
78
79 priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
80
81 err = nft_data_init(NULL, &priv->mask, &d1, tb[NFTA_BITWISE_MASK]);
82 if (err < 0)
83 return err;
84 if (d1.len != priv->len)
85 return -EINVAL;
86
87 err = nft_data_init(NULL, &priv->xor, &d2, tb[NFTA_BITWISE_XOR]);
88 if (err < 0)
89 return err;
90 if (d2.len != priv->len)
91 return -EINVAL;
92
93 return 0;
94}
95
96static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
97{
98 const struct nft_bitwise *priv = nft_expr_priv(expr);
99
100 if (nla_put_be32(skb, NFTA_BITWISE_SREG, htonl(priv->sreg)))
101 goto nla_put_failure;
102 if (nla_put_be32(skb, NFTA_BITWISE_DREG, htonl(priv->dreg)))
103 goto nla_put_failure;
104 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
105 goto nla_put_failure;
106
107 if (nft_data_dump(skb, NFTA_BITWISE_MASK, &priv->mask,
108 NFT_DATA_VALUE, priv->len) < 0)
109 goto nla_put_failure;
110
111 if (nft_data_dump(skb, NFTA_BITWISE_XOR, &priv->xor,
112 NFT_DATA_VALUE, priv->len) < 0)
113 goto nla_put_failure;
114
115 return 0;
116
117nla_put_failure:
118 return -1;
119}
120
121static struct nft_expr_type nft_bitwise_type;
122static const struct nft_expr_ops nft_bitwise_ops = {
123 .type = &nft_bitwise_type,
124 .size = NFT_EXPR_SIZE(sizeof(struct nft_bitwise)),
125 .eval = nft_bitwise_eval,
126 .init = nft_bitwise_init,
127 .dump = nft_bitwise_dump,
128};
129
130static struct nft_expr_type nft_bitwise_type __read_mostly = {
131 .name = "bitwise",
132 .ops = &nft_bitwise_ops,
133 .policy = nft_bitwise_policy,
134 .maxattr = NFTA_BITWISE_MAX,
135 .owner = THIS_MODULE,
136};
137
138int __init nft_bitwise_module_init(void)
139{
140 return nft_register_expr(&nft_bitwise_type);
141}
142
143void nft_bitwise_module_exit(void)
144{
145 nft_unregister_expr(&nft_bitwise_type);
146}
diff --git a/net/netfilter/nft_byteorder.c b/net/netfilter/nft_byteorder.c
new file mode 100644
index 000000000000..c39ed8d29df1
--- /dev/null
+++ b/net/netfilter/nft_byteorder.c
@@ -0,0 +1,173 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_byteorder {
21 enum nft_registers sreg:8;
22 enum nft_registers dreg:8;
23 enum nft_byteorder_ops op:8;
24 u8 len;
25 u8 size;
26};
27
28static void nft_byteorder_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 const struct nft_byteorder *priv = nft_expr_priv(expr);
33 struct nft_data *src = &data[priv->sreg], *dst = &data[priv->dreg];
34 union { u32 u32; u16 u16; } *s, *d;
35 unsigned int i;
36
37 s = (void *)src->data;
38 d = (void *)dst->data;
39
40 switch (priv->size) {
41 case 4:
42 switch (priv->op) {
43 case NFT_BYTEORDER_NTOH:
44 for (i = 0; i < priv->len / 4; i++)
45 d[i].u32 = ntohl((__force __be32)s[i].u32);
46 break;
47 case NFT_BYTEORDER_HTON:
48 for (i = 0; i < priv->len / 4; i++)
49 d[i].u32 = (__force __u32)htonl(s[i].u32);
50 break;
51 }
52 break;
53 case 2:
54 switch (priv->op) {
55 case NFT_BYTEORDER_NTOH:
56 for (i = 0; i < priv->len / 2; i++)
57 d[i].u16 = ntohs((__force __be16)s[i].u16);
58 break;
59 case NFT_BYTEORDER_HTON:
60 for (i = 0; i < priv->len / 2; i++)
61 d[i].u16 = (__force __u16)htons(s[i].u16);
62 break;
63 }
64 break;
65 }
66}
67
68static const struct nla_policy nft_byteorder_policy[NFTA_BYTEORDER_MAX + 1] = {
69 [NFTA_BYTEORDER_SREG] = { .type = NLA_U32 },
70 [NFTA_BYTEORDER_DREG] = { .type = NLA_U32 },
71 [NFTA_BYTEORDER_OP] = { .type = NLA_U32 },
72 [NFTA_BYTEORDER_LEN] = { .type = NLA_U32 },
73 [NFTA_BYTEORDER_SIZE] = { .type = NLA_U32 },
74};
75
76static int nft_byteorder_init(const struct nft_ctx *ctx,
77 const struct nft_expr *expr,
78 const struct nlattr * const tb[])
79{
80 struct nft_byteorder *priv = nft_expr_priv(expr);
81 int err;
82
83 if (tb[NFTA_BYTEORDER_SREG] == NULL ||
84 tb[NFTA_BYTEORDER_DREG] == NULL ||
85 tb[NFTA_BYTEORDER_LEN] == NULL ||
86 tb[NFTA_BYTEORDER_SIZE] == NULL ||
87 tb[NFTA_BYTEORDER_OP] == NULL)
88 return -EINVAL;
89
90 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SREG]));
91 err = nft_validate_input_register(priv->sreg);
92 if (err < 0)
93 return err;
94
95 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_DREG]));
96 err = nft_validate_output_register(priv->dreg);
97 if (err < 0)
98 return err;
99 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
100 if (err < 0)
101 return err;
102
103 priv->op = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_OP]));
104 switch (priv->op) {
105 case NFT_BYTEORDER_NTOH:
106 case NFT_BYTEORDER_HTON:
107 break;
108 default:
109 return -EINVAL;
110 }
111
112 priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
113 if (priv->len == 0 || priv->len > FIELD_SIZEOF(struct nft_data, data))
114 return -EINVAL;
115
116 priv->size = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SIZE]));
117 switch (priv->size) {
118 case 2:
119 case 4:
120 break;
121 default:
122 return -EINVAL;
123 }
124
125 return 0;
126}
127
128static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr)
129{
130 const struct nft_byteorder *priv = nft_expr_priv(expr);
131
132 if (nla_put_be32(skb, NFTA_BYTEORDER_SREG, htonl(priv->sreg)))
133 goto nla_put_failure;
134 if (nla_put_be32(skb, NFTA_BYTEORDER_DREG, htonl(priv->dreg)))
135 goto nla_put_failure;
136 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
137 goto nla_put_failure;
138 if (nla_put_be32(skb, NFTA_BYTEORDER_LEN, htonl(priv->len)))
139 goto nla_put_failure;
140 if (nla_put_be32(skb, NFTA_BYTEORDER_SIZE, htonl(priv->size)))
141 goto nla_put_failure;
142 return 0;
143
144nla_put_failure:
145 return -1;
146}
147
148static struct nft_expr_type nft_byteorder_type;
149static const struct nft_expr_ops nft_byteorder_ops = {
150 .type = &nft_byteorder_type,
151 .size = NFT_EXPR_SIZE(sizeof(struct nft_byteorder)),
152 .eval = nft_byteorder_eval,
153 .init = nft_byteorder_init,
154 .dump = nft_byteorder_dump,
155};
156
157static struct nft_expr_type nft_byteorder_type __read_mostly = {
158 .name = "byteorder",
159 .ops = &nft_byteorder_ops,
160 .policy = nft_byteorder_policy,
161 .maxattr = NFTA_BYTEORDER_MAX,
162 .owner = THIS_MODULE,
163};
164
165int __init nft_byteorder_module_init(void)
166{
167 return nft_register_expr(&nft_byteorder_type);
168}
169
170void nft_byteorder_module_exit(void)
171{
172 nft_unregister_expr(&nft_byteorder_type);
173}
diff --git a/net/netfilter/nft_cmp.c b/net/netfilter/nft_cmp.c
new file mode 100644
index 000000000000..954925db414d
--- /dev/null
+++ b/net/netfilter/nft_cmp.c
@@ -0,0 +1,223 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_cmp_expr {
21 struct nft_data data;
22 enum nft_registers sreg:8;
23 u8 len;
24 enum nft_cmp_ops op:8;
25};
26
27static void nft_cmp_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1],
29 const struct nft_pktinfo *pkt)
30{
31 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
32 int d;
33
34 d = nft_data_cmp(&data[priv->sreg], &priv->data, priv->len);
35 switch (priv->op) {
36 case NFT_CMP_EQ:
37 if (d != 0)
38 goto mismatch;
39 break;
40 case NFT_CMP_NEQ:
41 if (d == 0)
42 goto mismatch;
43 break;
44 case NFT_CMP_LT:
45 if (d == 0)
46 goto mismatch;
47 case NFT_CMP_LTE:
48 if (d > 0)
49 goto mismatch;
50 break;
51 case NFT_CMP_GT:
52 if (d == 0)
53 goto mismatch;
54 case NFT_CMP_GTE:
55 if (d < 0)
56 goto mismatch;
57 break;
58 }
59 return;
60
61mismatch:
62 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
63}
64
65static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
66 [NFTA_CMP_SREG] = { .type = NLA_U32 },
67 [NFTA_CMP_OP] = { .type = NLA_U32 },
68 [NFTA_CMP_DATA] = { .type = NLA_NESTED },
69};
70
71static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
72 const struct nlattr * const tb[])
73{
74 struct nft_cmp_expr *priv = nft_expr_priv(expr);
75 struct nft_data_desc desc;
76 int err;
77
78 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
79 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
80
81 err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
82 BUG_ON(err < 0);
83
84 priv->len = desc.len;
85 return 0;
86}
87
88static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
89{
90 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
91
92 if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg)))
93 goto nla_put_failure;
94 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
95 goto nla_put_failure;
96
97 if (nft_data_dump(skb, NFTA_CMP_DATA, &priv->data,
98 NFT_DATA_VALUE, priv->len) < 0)
99 goto nla_put_failure;
100 return 0;
101
102nla_put_failure:
103 return -1;
104}
105
106static struct nft_expr_type nft_cmp_type;
107static const struct nft_expr_ops nft_cmp_ops = {
108 .type = &nft_cmp_type,
109 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_expr)),
110 .eval = nft_cmp_eval,
111 .init = nft_cmp_init,
112 .dump = nft_cmp_dump,
113};
114
115static int nft_cmp_fast_init(const struct nft_ctx *ctx,
116 const struct nft_expr *expr,
117 const struct nlattr * const tb[])
118{
119 struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
120 struct nft_data_desc desc;
121 struct nft_data data;
122 u32 mask;
123 int err;
124
125 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
126
127 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
128 BUG_ON(err < 0);
129 desc.len *= BITS_PER_BYTE;
130
131 mask = ~0U >> (sizeof(priv->data) * BITS_PER_BYTE - desc.len);
132 priv->data = data.data[0] & mask;
133 priv->len = desc.len;
134 return 0;
135}
136
137static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
138{
139 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
140 struct nft_data data;
141
142 if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg)))
143 goto nla_put_failure;
144 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ)))
145 goto nla_put_failure;
146
147 data.data[0] = priv->data;
148 if (nft_data_dump(skb, NFTA_CMP_DATA, &data,
149 NFT_DATA_VALUE, priv->len / BITS_PER_BYTE) < 0)
150 goto nla_put_failure;
151 return 0;
152
153nla_put_failure:
154 return -1;
155}
156
157const struct nft_expr_ops nft_cmp_fast_ops = {
158 .type = &nft_cmp_type,
159 .size = NFT_EXPR_SIZE(sizeof(struct nft_cmp_fast_expr)),
160 .eval = NULL, /* inlined */
161 .init = nft_cmp_fast_init,
162 .dump = nft_cmp_fast_dump,
163};
164
165static const struct nft_expr_ops *
166nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
167{
168 struct nft_data_desc desc;
169 struct nft_data data;
170 enum nft_registers sreg;
171 enum nft_cmp_ops op;
172 int err;
173
174 if (tb[NFTA_CMP_SREG] == NULL ||
175 tb[NFTA_CMP_OP] == NULL ||
176 tb[NFTA_CMP_DATA] == NULL)
177 return ERR_PTR(-EINVAL);
178
179 sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG]));
180 err = nft_validate_input_register(sreg);
181 if (err < 0)
182 return ERR_PTR(err);
183
184 op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
185 switch (op) {
186 case NFT_CMP_EQ:
187 case NFT_CMP_NEQ:
188 case NFT_CMP_LT:
189 case NFT_CMP_LTE:
190 case NFT_CMP_GT:
191 case NFT_CMP_GTE:
192 break;
193 default:
194 return ERR_PTR(-EINVAL);
195 }
196
197 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
198 if (err < 0)
199 return ERR_PTR(err);
200
201 if (desc.len <= sizeof(u32) && op == NFT_CMP_EQ)
202 return &nft_cmp_fast_ops;
203 else
204 return &nft_cmp_ops;
205}
206
207static struct nft_expr_type nft_cmp_type __read_mostly = {
208 .name = "cmp",
209 .select_ops = nft_cmp_select_ops,
210 .policy = nft_cmp_policy,
211 .maxattr = NFTA_CMP_MAX,
212 .owner = THIS_MODULE,
213};
214
215int __init nft_cmp_module_init(void)
216{
217 return nft_register_expr(&nft_cmp_type);
218}
219
220void nft_cmp_module_exit(void)
221{
222 nft_unregister_expr(&nft_cmp_type);
223}
diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c
new file mode 100644
index 000000000000..da0c1f4ada12
--- /dev/null
+++ b/net/netfilter/nft_compat.c
@@ -0,0 +1,775 @@
1/*
2 * (C) 2012-2013 by Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This software has been sponsored by Sophos Astaro <http://www.sophos.com>
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nfnetlink.h>
17#include <linux/netfilter/nf_tables.h>
18#include <linux/netfilter/nf_tables_compat.h>
19#include <linux/netfilter/x_tables.h>
20#include <linux/netfilter_ipv4/ip_tables.h>
21#include <linux/netfilter_ipv6/ip6_tables.h>
22#include <asm/uaccess.h> /* for set_fs */
23#include <net/netfilter/nf_tables.h>
24
25union nft_entry {
26 struct ipt_entry e4;
27 struct ip6t_entry e6;
28};
29
30static inline void
31nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
32{
33 par->target = xt;
34 par->targinfo = xt_info;
35 par->hotdrop = false;
36}
37
38static void nft_target_eval(const struct nft_expr *expr,
39 struct nft_data data[NFT_REG_MAX + 1],
40 const struct nft_pktinfo *pkt)
41{
42 void *info = nft_expr_priv(expr);
43 struct xt_target *target = expr->ops->data;
44 struct sk_buff *skb = pkt->skb;
45 int ret;
46
47 nft_compat_set_par((struct xt_action_param *)&pkt->xt, target, info);
48
49 ret = target->target(skb, &pkt->xt);
50
51 if (pkt->xt.hotdrop)
52 ret = NF_DROP;
53
54 switch(ret) {
55 case XT_CONTINUE:
56 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
57 break;
58 default:
59 data[NFT_REG_VERDICT].verdict = ret;
60 break;
61 }
62 return;
63}
64
65static const struct nla_policy nft_target_policy[NFTA_TARGET_MAX + 1] = {
66 [NFTA_TARGET_NAME] = { .type = NLA_NUL_STRING },
67 [NFTA_TARGET_REV] = { .type = NLA_U32 },
68 [NFTA_TARGET_INFO] = { .type = NLA_BINARY },
69};
70
71static void
72nft_target_set_tgchk_param(struct xt_tgchk_param *par,
73 const struct nft_ctx *ctx,
74 struct xt_target *target, void *info,
75 union nft_entry *entry, u8 proto, bool inv)
76{
77 par->net = &init_net;
78 par->table = ctx->table->name;
79 switch (ctx->afi->family) {
80 case AF_INET:
81 entry->e4.ip.proto = proto;
82 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
83 break;
84 case AF_INET6:
85 entry->e6.ipv6.proto = proto;
86 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
87 break;
88 }
89 par->entryinfo = entry;
90 par->target = target;
91 par->targinfo = info;
92 if (ctx->chain->flags & NFT_BASE_CHAIN) {
93 const struct nft_base_chain *basechain =
94 nft_base_chain(ctx->chain);
95 const struct nf_hook_ops *ops = &basechain->ops;
96
97 par->hook_mask = 1 << ops->hooknum;
98 }
99 par->family = ctx->afi->family;
100}
101
102static void target_compat_from_user(struct xt_target *t, void *in, void *out)
103{
104#ifdef CONFIG_COMPAT
105 if (t->compat_from_user) {
106 int pad;
107
108 t->compat_from_user(out, in);
109 pad = XT_ALIGN(t->targetsize) - t->targetsize;
110 if (pad > 0)
111 memset(out + t->targetsize, 0, pad);
112 } else
113#endif
114 memcpy(out, in, XT_ALIGN(t->targetsize));
115}
116
117static inline int nft_compat_target_offset(struct xt_target *target)
118{
119#ifdef CONFIG_COMPAT
120 return xt_compat_target_offset(target);
121#else
122 return 0;
123#endif
124}
125
126static const struct nla_policy nft_rule_compat_policy[NFTA_RULE_COMPAT_MAX + 1] = {
127 [NFTA_RULE_COMPAT_PROTO] = { .type = NLA_U32 },
128 [NFTA_RULE_COMPAT_FLAGS] = { .type = NLA_U32 },
129};
130
131static int nft_parse_compat(const struct nlattr *attr, u8 *proto, bool *inv)
132{
133 struct nlattr *tb[NFTA_RULE_COMPAT_MAX+1];
134 u32 flags;
135 int err;
136
137 err = nla_parse_nested(tb, NFTA_RULE_COMPAT_MAX, attr,
138 nft_rule_compat_policy);
139 if (err < 0)
140 return err;
141
142 if (!tb[NFTA_RULE_COMPAT_PROTO] || !tb[NFTA_RULE_COMPAT_FLAGS])
143 return -EINVAL;
144
145 flags = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_FLAGS]));
146 if (flags & ~NFT_RULE_COMPAT_F_MASK)
147 return -EINVAL;
148 if (flags & NFT_RULE_COMPAT_F_INV)
149 *inv = true;
150
151 *proto = ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
152 return 0;
153}
154
155static int
156nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
157 const struct nlattr * const tb[])
158{
159 void *info = nft_expr_priv(expr);
160 struct xt_target *target = expr->ops->data;
161 struct xt_tgchk_param par;
162 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
163 u8 proto = 0;
164 bool inv = false;
165 union nft_entry e = {};
166 int ret;
167
168 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
169
170 if (ctx->nla[NFTA_RULE_COMPAT]) {
171 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
172 if (ret < 0)
173 goto err;
174 }
175
176 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
177
178 ret = xt_check_target(&par, size, proto, inv);
179 if (ret < 0)
180 goto err;
181
182 /* The standard target cannot be used */
183 if (target->target == NULL) {
184 ret = -EINVAL;
185 goto err;
186 }
187
188 return 0;
189err:
190 module_put(target->me);
191 return ret;
192}
193
194static void
195nft_target_destroy(const struct nft_expr *expr)
196{
197 struct xt_target *target = expr->ops->data;
198
199 module_put(target->me);
200}
201
202static int
203target_dump_info(struct sk_buff *skb, const struct xt_target *t, const void *in)
204{
205 int ret;
206
207#ifdef CONFIG_COMPAT
208 if (t->compat_to_user) {
209 mm_segment_t old_fs;
210 void *out;
211
212 out = kmalloc(XT_ALIGN(t->targetsize), GFP_ATOMIC);
213 if (out == NULL)
214 return -ENOMEM;
215
216 /* We want to reuse existing compat_to_user */
217 old_fs = get_fs();
218 set_fs(KERNEL_DS);
219 t->compat_to_user(out, in);
220 set_fs(old_fs);
221 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), out);
222 kfree(out);
223 } else
224#endif
225 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), in);
226
227 return ret;
228}
229
230static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
231{
232 const struct xt_target *target = expr->ops->data;
233 void *info = nft_expr_priv(expr);
234
235 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
236 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
237 target_dump_info(skb, target, info))
238 goto nla_put_failure;
239
240 return 0;
241
242nla_put_failure:
243 return -1;
244}
245
246static int nft_target_validate(const struct nft_ctx *ctx,
247 const struct nft_expr *expr,
248 const struct nft_data **data)
249{
250 struct xt_target *target = expr->ops->data;
251 unsigned int hook_mask = 0;
252
253 if (ctx->chain->flags & NFT_BASE_CHAIN) {
254 const struct nft_base_chain *basechain =
255 nft_base_chain(ctx->chain);
256 const struct nf_hook_ops *ops = &basechain->ops;
257
258 hook_mask = 1 << ops->hooknum;
259 if (hook_mask & target->hooks)
260 return 0;
261
262 /* This target is being called from an invalid chain */
263 return -EINVAL;
264 }
265 return 0;
266}
267
268static void nft_match_eval(const struct nft_expr *expr,
269 struct nft_data data[NFT_REG_MAX + 1],
270 const struct nft_pktinfo *pkt)
271{
272 void *info = nft_expr_priv(expr);
273 struct xt_match *match = expr->ops->data;
274 struct sk_buff *skb = pkt->skb;
275 bool ret;
276
277 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
278
279 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
280
281 if (pkt->xt.hotdrop) {
282 data[NFT_REG_VERDICT].verdict = NF_DROP;
283 return;
284 }
285
286 switch(ret) {
287 case true:
288 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
289 break;
290 case false:
291 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
292 break;
293 }
294}
295
296static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
297 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
298 [NFTA_MATCH_REV] = { .type = NLA_U32 },
299 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
300};
301
302/* struct xt_mtchk_param and xt_tgchk_param look very similar */
303static void
304nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
305 struct xt_match *match, void *info,
306 union nft_entry *entry, u8 proto, bool inv)
307{
308 par->net = &init_net;
309 par->table = ctx->table->name;
310 switch (ctx->afi->family) {
311 case AF_INET:
312 entry->e4.ip.proto = proto;
313 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
314 break;
315 case AF_INET6:
316 entry->e6.ipv6.proto = proto;
317 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
318 break;
319 }
320 par->entryinfo = entry;
321 par->match = match;
322 par->matchinfo = info;
323 if (ctx->chain->flags & NFT_BASE_CHAIN) {
324 const struct nft_base_chain *basechain =
325 nft_base_chain(ctx->chain);
326 const struct nf_hook_ops *ops = &basechain->ops;
327
328 par->hook_mask = 1 << ops->hooknum;
329 }
330 par->family = ctx->afi->family;
331}
332
333static void match_compat_from_user(struct xt_match *m, void *in, void *out)
334{
335#ifdef CONFIG_COMPAT
336 if (m->compat_from_user) {
337 int pad;
338
339 m->compat_from_user(out, in);
340 pad = XT_ALIGN(m->matchsize) - m->matchsize;
341 if (pad > 0)
342 memset(out + m->matchsize, 0, pad);
343 } else
344#endif
345 memcpy(out, in, XT_ALIGN(m->matchsize));
346}
347
348static int
349nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
350 const struct nlattr * const tb[])
351{
352 void *info = nft_expr_priv(expr);
353 struct xt_match *match = expr->ops->data;
354 struct xt_mtchk_param par;
355 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
356 u8 proto = 0;
357 bool inv = false;
358 union nft_entry e = {};
359 int ret;
360
361 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
362
363 if (ctx->nla[NFTA_RULE_COMPAT]) {
364 ret = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &proto, &inv);
365 if (ret < 0)
366 goto err;
367 }
368
369 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
370
371 ret = xt_check_match(&par, size, proto, inv);
372 if (ret < 0)
373 goto err;
374
375 return 0;
376err:
377 module_put(match->me);
378 return ret;
379}
380
381static void
382nft_match_destroy(const struct nft_expr *expr)
383{
384 struct xt_match *match = expr->ops->data;
385
386 module_put(match->me);
387}
388
389static int
390match_dump_info(struct sk_buff *skb, const struct xt_match *m, const void *in)
391{
392 int ret;
393
394#ifdef CONFIG_COMPAT
395 if (m->compat_to_user) {
396 mm_segment_t old_fs;
397 void *out;
398
399 out = kmalloc(XT_ALIGN(m->matchsize), GFP_ATOMIC);
400 if (out == NULL)
401 return -ENOMEM;
402
403 /* We want to reuse existing compat_to_user */
404 old_fs = get_fs();
405 set_fs(KERNEL_DS);
406 m->compat_to_user(out, in);
407 set_fs(old_fs);
408 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), out);
409 kfree(out);
410 } else
411#endif
412 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), in);
413
414 return ret;
415}
416
417static inline int nft_compat_match_offset(struct xt_match *match)
418{
419#ifdef CONFIG_COMPAT
420 return xt_compat_match_offset(match);
421#else
422 return 0;
423#endif
424}
425
426static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
427{
428 void *info = nft_expr_priv(expr);
429 struct xt_match *match = expr->ops->data;
430
431 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
432 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
433 match_dump_info(skb, match, info))
434 goto nla_put_failure;
435
436 return 0;
437
438nla_put_failure:
439 return -1;
440}
441
442static int nft_match_validate(const struct nft_ctx *ctx,
443 const struct nft_expr *expr,
444 const struct nft_data **data)
445{
446 struct xt_match *match = expr->ops->data;
447 unsigned int hook_mask = 0;
448
449 if (ctx->chain->flags & NFT_BASE_CHAIN) {
450 const struct nft_base_chain *basechain =
451 nft_base_chain(ctx->chain);
452 const struct nf_hook_ops *ops = &basechain->ops;
453
454 hook_mask = 1 << ops->hooknum;
455 if (hook_mask & match->hooks)
456 return 0;
457
458 /* This match is being called from an invalid chain */
459 return -EINVAL;
460 }
461 return 0;
462}
463
464static int
465nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
466 int event, u16 family, const char *name,
467 int rev, int target)
468{
469 struct nlmsghdr *nlh;
470 struct nfgenmsg *nfmsg;
471 unsigned int flags = portid ? NLM_F_MULTI : 0;
472
473 event |= NFNL_SUBSYS_NFT_COMPAT << 8;
474 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
475 if (nlh == NULL)
476 goto nlmsg_failure;
477
478 nfmsg = nlmsg_data(nlh);
479 nfmsg->nfgen_family = family;
480 nfmsg->version = NFNETLINK_V0;
481 nfmsg->res_id = 0;
482
483 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
484 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
485 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
486 goto nla_put_failure;
487
488 nlmsg_end(skb, nlh);
489 return skb->len;
490
491nlmsg_failure:
492nla_put_failure:
493 nlmsg_cancel(skb, nlh);
494 return -1;
495}
496
497static int
498nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb,
499 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
500{
501 int ret = 0, target;
502 struct nfgenmsg *nfmsg;
503 const char *fmt;
504 const char *name;
505 u32 rev;
506 struct sk_buff *skb2;
507
508 if (tb[NFTA_COMPAT_NAME] == NULL ||
509 tb[NFTA_COMPAT_REV] == NULL ||
510 tb[NFTA_COMPAT_TYPE] == NULL)
511 return -EINVAL;
512
513 name = nla_data(tb[NFTA_COMPAT_NAME]);
514 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
515 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
516
517 nfmsg = nlmsg_data(nlh);
518
519 switch(nfmsg->nfgen_family) {
520 case AF_INET:
521 fmt = "ipt_%s";
522 break;
523 case AF_INET6:
524 fmt = "ip6t_%s";
525 break;
526 default:
527 pr_err("nft_compat: unsupported protocol %d\n",
528 nfmsg->nfgen_family);
529 return -EINVAL;
530 }
531
532 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
533 rev, target, &ret),
534 fmt, name);
535
536 if (ret < 0)
537 return ret;
538
539 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
540 if (skb2 == NULL)
541 return -ENOMEM;
542
543 /* include the best revision for this extension in the message */
544 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
545 nlh->nlmsg_seq,
546 NFNL_MSG_TYPE(nlh->nlmsg_type),
547 NFNL_MSG_COMPAT_GET,
548 nfmsg->nfgen_family,
549 name, ret, target) <= 0) {
550 kfree_skb(skb2);
551 return -ENOSPC;
552 }
553
554 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
555 MSG_DONTWAIT);
556 if (ret > 0)
557 ret = 0;
558
559 return ret == -EAGAIN ? -ENOBUFS : ret;
560}
561
562static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
563 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
564 .len = NFT_COMPAT_NAME_MAX-1 },
565 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
566 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
567};
568
569static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
570 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
571 .attr_count = NFTA_COMPAT_MAX,
572 .policy = nfnl_compat_policy_get },
573};
574
575static const struct nfnetlink_subsystem nfnl_compat_subsys = {
576 .name = "nft-compat",
577 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
578 .cb_count = NFNL_MSG_COMPAT_MAX,
579 .cb = nfnl_nft_compat_cb,
580};
581
582static LIST_HEAD(nft_match_list);
583
584struct nft_xt {
585 struct list_head head;
586 struct nft_expr_ops ops;
587};
588
589static struct nft_expr_type nft_match_type;
590
591static const struct nft_expr_ops *
592nft_match_select_ops(const struct nft_ctx *ctx,
593 const struct nlattr * const tb[])
594{
595 struct nft_xt *nft_match;
596 struct xt_match *match;
597 char *mt_name;
598 __u32 rev, family;
599
600 if (tb[NFTA_MATCH_NAME] == NULL ||
601 tb[NFTA_MATCH_REV] == NULL ||
602 tb[NFTA_MATCH_INFO] == NULL)
603 return ERR_PTR(-EINVAL);
604
605 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
606 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
607 family = ctx->afi->family;
608
609 /* Re-use the existing match if it's already loaded. */
610 list_for_each_entry(nft_match, &nft_match_list, head) {
611 struct xt_match *match = nft_match->ops.data;
612
613 if (strcmp(match->name, mt_name) == 0 &&
614 match->revision == rev && match->family == family)
615 return &nft_match->ops;
616 }
617
618 match = xt_request_find_match(family, mt_name, rev);
619 if (IS_ERR(match))
620 return ERR_PTR(-ENOENT);
621
622 /* This is the first time we use this match, allocate operations */
623 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
624 if (nft_match == NULL)
625 return ERR_PTR(-ENOMEM);
626
627 nft_match->ops.type = &nft_match_type;
628 nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize) +
629 nft_compat_match_offset(match));
630 nft_match->ops.eval = nft_match_eval;
631 nft_match->ops.init = nft_match_init;
632 nft_match->ops.destroy = nft_match_destroy;
633 nft_match->ops.dump = nft_match_dump;
634 nft_match->ops.validate = nft_match_validate;
635 nft_match->ops.data = match;
636
637 list_add(&nft_match->head, &nft_match_list);
638
639 return &nft_match->ops;
640}
641
642static void nft_match_release(void)
643{
644 struct nft_xt *nft_match, *tmp;
645
646 list_for_each_entry_safe(nft_match, tmp, &nft_match_list, head)
647 kfree(nft_match);
648}
649
650static struct nft_expr_type nft_match_type __read_mostly = {
651 .name = "match",
652 .select_ops = nft_match_select_ops,
653 .policy = nft_match_policy,
654 .maxattr = NFTA_MATCH_MAX,
655 .owner = THIS_MODULE,
656};
657
658static LIST_HEAD(nft_target_list);
659
660static struct nft_expr_type nft_target_type;
661
662static const struct nft_expr_ops *
663nft_target_select_ops(const struct nft_ctx *ctx,
664 const struct nlattr * const tb[])
665{
666 struct nft_xt *nft_target;
667 struct xt_target *target;
668 char *tg_name;
669 __u32 rev, family;
670
671 if (tb[NFTA_TARGET_NAME] == NULL ||
672 tb[NFTA_TARGET_REV] == NULL ||
673 tb[NFTA_TARGET_INFO] == NULL)
674 return ERR_PTR(-EINVAL);
675
676 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
677 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
678 family = ctx->afi->family;
679
680 /* Re-use the existing target if it's already loaded. */
681 list_for_each_entry(nft_target, &nft_match_list, head) {
682 struct xt_target *target = nft_target->ops.data;
683
684 if (strcmp(target->name, tg_name) == 0 &&
685 target->revision == rev && target->family == family)
686 return &nft_target->ops;
687 }
688
689 target = xt_request_find_target(family, tg_name, rev);
690 if (IS_ERR(target))
691 return ERR_PTR(-ENOENT);
692
693 /* This is the first time we use this target, allocate operations */
694 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
695 if (nft_target == NULL)
696 return ERR_PTR(-ENOMEM);
697
698 nft_target->ops.type = &nft_target_type;
699 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize) +
700 nft_compat_target_offset(target));
701 nft_target->ops.eval = nft_target_eval;
702 nft_target->ops.init = nft_target_init;
703 nft_target->ops.destroy = nft_target_destroy;
704 nft_target->ops.dump = nft_target_dump;
705 nft_target->ops.validate = nft_target_validate;
706 nft_target->ops.data = target;
707
708 list_add(&nft_target->head, &nft_target_list);
709
710 return &nft_target->ops;
711}
712
713static void nft_target_release(void)
714{
715 struct nft_xt *nft_target, *tmp;
716
717 list_for_each_entry_safe(nft_target, tmp, &nft_target_list, head)
718 kfree(nft_target);
719}
720
721static struct nft_expr_type nft_target_type __read_mostly = {
722 .name = "target",
723 .select_ops = nft_target_select_ops,
724 .policy = nft_target_policy,
725 .maxattr = NFTA_TARGET_MAX,
726 .owner = THIS_MODULE,
727};
728
729static int __init nft_compat_module_init(void)
730{
731 int ret;
732
733 ret = nft_register_expr(&nft_match_type);
734 if (ret < 0)
735 return ret;
736
737 ret = nft_register_expr(&nft_target_type);
738 if (ret < 0)
739 goto err_match;
740
741 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
742 if (ret < 0) {
743 pr_err("nft_compat: cannot register with nfnetlink.\n");
744 goto err_target;
745 }
746
747 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
748
749 return ret;
750
751err_target:
752 nft_unregister_expr(&nft_target_type);
753err_match:
754 nft_unregister_expr(&nft_match_type);
755 return ret;
756}
757
758static void __exit nft_compat_module_exit(void)
759{
760 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
761 nft_unregister_expr(&nft_target_type);
762 nft_unregister_expr(&nft_match_type);
763 nft_match_release();
764 nft_target_release();
765}
766
767MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
768
769module_init(nft_compat_module_init);
770module_exit(nft_compat_module_exit);
771
772MODULE_LICENSE("GPL");
773MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
774MODULE_ALIAS_NFT_EXPR("match");
775MODULE_ALIAS_NFT_EXPR("target");
diff --git a/net/netfilter/nft_counter.c b/net/netfilter/nft_counter.c
new file mode 100644
index 000000000000..c89ee486ce54
--- /dev/null
+++ b/net/netfilter/nft_counter.c
@@ -0,0 +1,113 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/seqlock.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_counter {
21 seqlock_t lock;
22 u64 bytes;
23 u64 packets;
24};
25
26static void nft_counter_eval(const struct nft_expr *expr,
27 struct nft_data data[NFT_REG_MAX + 1],
28 const struct nft_pktinfo *pkt)
29{
30 struct nft_counter *priv = nft_expr_priv(expr);
31
32 write_seqlock_bh(&priv->lock);
33 priv->bytes += pkt->skb->len;
34 priv->packets++;
35 write_sequnlock_bh(&priv->lock);
36}
37
38static int nft_counter_dump(struct sk_buff *skb, const struct nft_expr *expr)
39{
40 struct nft_counter *priv = nft_expr_priv(expr);
41 unsigned int seq;
42 u64 bytes;
43 u64 packets;
44
45 do {
46 seq = read_seqbegin(&priv->lock);
47 bytes = priv->bytes;
48 packets = priv->packets;
49 } while (read_seqretry(&priv->lock, seq));
50
51 if (nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(bytes)))
52 goto nla_put_failure;
53 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(packets)))
54 goto nla_put_failure;
55 return 0;
56
57nla_put_failure:
58 return -1;
59}
60
61static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
62 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
63 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
64};
65
66static int nft_counter_init(const struct nft_ctx *ctx,
67 const struct nft_expr *expr,
68 const struct nlattr * const tb[])
69{
70 struct nft_counter *priv = nft_expr_priv(expr);
71
72 if (tb[NFTA_COUNTER_PACKETS])
73 priv->packets = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
74 if (tb[NFTA_COUNTER_BYTES])
75 priv->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
76
77 seqlock_init(&priv->lock);
78 return 0;
79}
80
81static struct nft_expr_type nft_counter_type;
82static const struct nft_expr_ops nft_counter_ops = {
83 .type = &nft_counter_type,
84 .size = NFT_EXPR_SIZE(sizeof(struct nft_counter)),
85 .eval = nft_counter_eval,
86 .init = nft_counter_init,
87 .dump = nft_counter_dump,
88};
89
90static struct nft_expr_type nft_counter_type __read_mostly = {
91 .name = "counter",
92 .ops = &nft_counter_ops,
93 .policy = nft_counter_policy,
94 .maxattr = NFTA_COUNTER_MAX,
95 .owner = THIS_MODULE,
96};
97
98static int __init nft_counter_module_init(void)
99{
100 return nft_register_expr(&nft_counter_type);
101}
102
103static void __exit nft_counter_module_exit(void)
104{
105 nft_unregister_expr(&nft_counter_type);
106}
107
108module_init(nft_counter_module_init);
109module_exit(nft_counter_module_exit);
110
111MODULE_LICENSE("GPL");
112MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
113MODULE_ALIAS_NFT_EXPR("counter");
diff --git a/net/netfilter/nft_ct.c b/net/netfilter/nft_ct.c
new file mode 100644
index 000000000000..955f4e6e7089
--- /dev/null
+++ b/net/netfilter/nft_ct.c
@@ -0,0 +1,258 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18#include <net/netfilter/nf_conntrack.h>
19#include <net/netfilter/nf_conntrack_tuple.h>
20#include <net/netfilter/nf_conntrack_helper.h>
21
22struct nft_ct {
23 enum nft_ct_keys key:8;
24 enum ip_conntrack_dir dir:8;
25 enum nft_registers dreg:8;
26 uint8_t family;
27};
28
29static void nft_ct_eval(const struct nft_expr *expr,
30 struct nft_data data[NFT_REG_MAX + 1],
31 const struct nft_pktinfo *pkt)
32{
33 const struct nft_ct *priv = nft_expr_priv(expr);
34 struct nft_data *dest = &data[priv->dreg];
35 enum ip_conntrack_info ctinfo;
36 const struct nf_conn *ct;
37 const struct nf_conn_help *help;
38 const struct nf_conntrack_tuple *tuple;
39 const struct nf_conntrack_helper *helper;
40 long diff;
41 unsigned int state;
42
43 ct = nf_ct_get(pkt->skb, &ctinfo);
44
45 switch (priv->key) {
46 case NFT_CT_STATE:
47 if (ct == NULL)
48 state = NF_CT_STATE_INVALID_BIT;
49 else if (nf_ct_is_untracked(ct))
50 state = NF_CT_STATE_UNTRACKED_BIT;
51 else
52 state = NF_CT_STATE_BIT(ctinfo);
53 dest->data[0] = state;
54 return;
55 }
56
57 if (ct == NULL)
58 goto err;
59
60 switch (priv->key) {
61 case NFT_CT_DIRECTION:
62 dest->data[0] = CTINFO2DIR(ctinfo);
63 return;
64 case NFT_CT_STATUS:
65 dest->data[0] = ct->status;
66 return;
67#ifdef CONFIG_NF_CONNTRACK_MARK
68 case NFT_CT_MARK:
69 dest->data[0] = ct->mark;
70 return;
71#endif
72#ifdef CONFIG_NF_CONNTRACK_SECMARK
73 case NFT_CT_SECMARK:
74 dest->data[0] = ct->secmark;
75 return;
76#endif
77 case NFT_CT_EXPIRATION:
78 diff = (long)jiffies - (long)ct->timeout.expires;
79 if (diff < 0)
80 diff = 0;
81 dest->data[0] = jiffies_to_msecs(diff);
82 return;
83 case NFT_CT_HELPER:
84 if (ct->master == NULL)
85 goto err;
86 help = nfct_help(ct->master);
87 if (help == NULL)
88 goto err;
89 helper = rcu_dereference(help->helper);
90 if (helper == NULL)
91 goto err;
92 if (strlen(helper->name) >= sizeof(dest->data))
93 goto err;
94 strncpy((char *)dest->data, helper->name, sizeof(dest->data));
95 return;
96 }
97
98 tuple = &ct->tuplehash[priv->dir].tuple;
99 switch (priv->key) {
100 case NFT_CT_L3PROTOCOL:
101 dest->data[0] = nf_ct_l3num(ct);
102 return;
103 case NFT_CT_SRC:
104 memcpy(dest->data, tuple->src.u3.all,
105 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
106 return;
107 case NFT_CT_DST:
108 memcpy(dest->data, tuple->dst.u3.all,
109 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
110 return;
111 case NFT_CT_PROTOCOL:
112 dest->data[0] = nf_ct_protonum(ct);
113 return;
114 case NFT_CT_PROTO_SRC:
115 dest->data[0] = (__force __u16)tuple->src.u.all;
116 return;
117 case NFT_CT_PROTO_DST:
118 dest->data[0] = (__force __u16)tuple->dst.u.all;
119 return;
120 }
121 return;
122err:
123 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
124}
125
126static const struct nla_policy nft_ct_policy[NFTA_CT_MAX + 1] = {
127 [NFTA_CT_DREG] = { .type = NLA_U32 },
128 [NFTA_CT_KEY] = { .type = NLA_U32 },
129 [NFTA_CT_DIRECTION] = { .type = NLA_U8 },
130};
131
132static int nft_ct_init(const struct nft_ctx *ctx,
133 const struct nft_expr *expr,
134 const struct nlattr * const tb[])
135{
136 struct nft_ct *priv = nft_expr_priv(expr);
137 int err;
138
139 if (tb[NFTA_CT_DREG] == NULL ||
140 tb[NFTA_CT_KEY] == NULL)
141 return -EINVAL;
142
143 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
144 if (tb[NFTA_CT_DIRECTION] != NULL) {
145 priv->dir = nla_get_u8(tb[NFTA_CT_DIRECTION]);
146 switch (priv->dir) {
147 case IP_CT_DIR_ORIGINAL:
148 case IP_CT_DIR_REPLY:
149 break;
150 default:
151 return -EINVAL;
152 }
153 }
154
155 switch (priv->key) {
156 case NFT_CT_STATE:
157 case NFT_CT_DIRECTION:
158 case NFT_CT_STATUS:
159#ifdef CONFIG_NF_CONNTRACK_MARK
160 case NFT_CT_MARK:
161#endif
162#ifdef CONFIG_NF_CONNTRACK_SECMARK
163 case NFT_CT_SECMARK:
164#endif
165 case NFT_CT_EXPIRATION:
166 case NFT_CT_HELPER:
167 if (tb[NFTA_CT_DIRECTION] != NULL)
168 return -EINVAL;
169 break;
170 case NFT_CT_PROTOCOL:
171 case NFT_CT_SRC:
172 case NFT_CT_DST:
173 case NFT_CT_PROTO_SRC:
174 case NFT_CT_PROTO_DST:
175 if (tb[NFTA_CT_DIRECTION] == NULL)
176 return -EINVAL;
177 break;
178 default:
179 return -EOPNOTSUPP;
180 }
181
182 err = nf_ct_l3proto_try_module_get(ctx->afi->family);
183 if (err < 0)
184 return err;
185 priv->family = ctx->afi->family;
186
187 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG]));
188 err = nft_validate_output_register(priv->dreg);
189 if (err < 0)
190 goto err1;
191
192 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
193 if (err < 0)
194 goto err1;
195 return 0;
196
197err1:
198 nf_ct_l3proto_module_put(ctx->afi->family);
199 return err;
200}
201
202static void nft_ct_destroy(const struct nft_expr *expr)
203{
204 struct nft_ct *priv = nft_expr_priv(expr);
205
206 nf_ct_l3proto_module_put(priv->family);
207}
208
209static int nft_ct_dump(struct sk_buff *skb, const struct nft_expr *expr)
210{
211 const struct nft_ct *priv = nft_expr_priv(expr);
212
213 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg)))
214 goto nla_put_failure;
215 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
216 goto nla_put_failure;
217 if (nla_put_u8(skb, NFTA_CT_DIRECTION, priv->dir))
218 goto nla_put_failure;
219 return 0;
220
221nla_put_failure:
222 return -1;
223}
224
225static struct nft_expr_type nft_ct_type;
226static const struct nft_expr_ops nft_ct_ops = {
227 .type = &nft_ct_type,
228 .size = NFT_EXPR_SIZE(sizeof(struct nft_ct)),
229 .eval = nft_ct_eval,
230 .init = nft_ct_init,
231 .destroy = nft_ct_destroy,
232 .dump = nft_ct_dump,
233};
234
235static struct nft_expr_type nft_ct_type __read_mostly = {
236 .name = "ct",
237 .ops = &nft_ct_ops,
238 .policy = nft_ct_policy,
239 .maxattr = NFTA_CT_MAX,
240 .owner = THIS_MODULE,
241};
242
243static int __init nft_ct_module_init(void)
244{
245 return nft_register_expr(&nft_ct_type);
246}
247
248static void __exit nft_ct_module_exit(void)
249{
250 nft_unregister_expr(&nft_ct_type);
251}
252
253module_init(nft_ct_module_init);
254module_exit(nft_ct_module_exit);
255
256MODULE_LICENSE("GPL");
257MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
258MODULE_ALIAS_NFT_EXPR("ct");
diff --git a/net/netfilter/nft_expr_template.c b/net/netfilter/nft_expr_template.c
new file mode 100644
index 000000000000..b6eed4d5a096
--- /dev/null
+++ b/net/netfilter/nft_expr_template.c
@@ -0,0 +1,94 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/netlink.h>
14#include <linux/netfilter.h>
15#include <linux/netfilter/nf_tables.h>
16#include <net/netfilter/nf_tables.h>
17
18struct nft_template {
19
20};
21
22static void nft_template_eval(const struct nft_expr *expr,
23 struct nft_data data[NFT_REG_MAX + 1],
24 const struct nft_pktinfo *pkt)
25{
26 struct nft_template *priv = nft_expr_priv(expr);
27
28}
29
30static const struct nla_policy nft_template_policy[NFTA_TEMPLATE_MAX + 1] = {
31 [NFTA_TEMPLATE_ATTR] = { .type = NLA_U32 },
32};
33
34static int nft_template_init(const struct nft_ctx *ctx,
35 const struct nft_expr *expr,
36 const struct nlattr * const tb[])
37{
38 struct nft_template *priv = nft_expr_priv(expr);
39
40 return 0;
41}
42
43static void nft_template_destroy(const struct nft_ctx *ctx,
44 const struct nft_expr *expr)
45{
46 struct nft_template *priv = nft_expr_priv(expr);
47
48}
49
50static int nft_template_dump(struct sk_buff *skb, const struct nft_expr *expr)
51{
52 const struct nft_template *priv = nft_expr_priv(expr);
53
54 NLA_PUT_BE32(skb, NFTA_TEMPLATE_ATTR, priv->field);
55 return 0;
56
57nla_put_failure:
58 return -1;
59}
60
61static struct nft_expr_type nft_template_type;
62static const struct nft_expr_ops nft_template_ops = {
63 .type = &nft_template_type,
64 .size = NFT_EXPR_SIZE(sizeof(struct nft_template)),
65 .eval = nft_template_eval,
66 .init = nft_template_init,
67 .destroy = nft_template_destroy,
68 .dump = nft_template_dump,
69};
70
71static struct nft_expr_type nft_template_type __read_mostly = {
72 .name = "template",
73 .ops = &nft_template_ops,
74 .policy = nft_template_policy,
75 .maxattr = NFTA_TEMPLATE_MAX,
76 .owner = THIS_MODULE,
77};
78
79static int __init nft_template_module_init(void)
80{
81 return nft_register_expr(&nft_template_type);
82}
83
84static void __exit nft_template_module_exit(void)
85{
86 nft_unregister_expr(&nft_template_type);
87}
88
89module_init(nft_template_module_init);
90module_exit(nft_template_module_exit);
91
92MODULE_LICENSE("GPL");
93MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
94MODULE_ALIAS_NFT_EXPR("template");
diff --git a/net/netfilter/nft_exthdr.c b/net/netfilter/nft_exthdr.c
new file mode 100644
index 000000000000..55c939f5371f
--- /dev/null
+++ b/net/netfilter/nft_exthdr.c
@@ -0,0 +1,133 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18// FIXME:
19#include <net/ipv6.h>
20
21struct nft_exthdr {
22 u8 type;
23 u8 offset;
24 u8 len;
25 enum nft_registers dreg:8;
26};
27
28static void nft_exthdr_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 struct nft_exthdr *priv = nft_expr_priv(expr);
33 struct nft_data *dest = &data[priv->dreg];
34 unsigned int offset = 0;
35 int err;
36
37 err = ipv6_find_hdr(pkt->skb, &offset, priv->type, NULL, NULL);
38 if (err < 0)
39 goto err;
40 offset += priv->offset;
41
42 if (skb_copy_bits(pkt->skb, offset, dest->data, priv->len) < 0)
43 goto err;
44 return;
45err:
46 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
47}
48
49static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
50 [NFTA_EXTHDR_DREG] = { .type = NLA_U32 },
51 [NFTA_EXTHDR_TYPE] = { .type = NLA_U8 },
52 [NFTA_EXTHDR_OFFSET] = { .type = NLA_U32 },
53 [NFTA_EXTHDR_LEN] = { .type = NLA_U32 },
54};
55
56static int nft_exthdr_init(const struct nft_ctx *ctx,
57 const struct nft_expr *expr,
58 const struct nlattr * const tb[])
59{
60 struct nft_exthdr *priv = nft_expr_priv(expr);
61 int err;
62
63 if (tb[NFTA_EXTHDR_DREG] == NULL ||
64 tb[NFTA_EXTHDR_TYPE] == NULL ||
65 tb[NFTA_EXTHDR_OFFSET] == NULL ||
66 tb[NFTA_EXTHDR_LEN] == NULL)
67 return -EINVAL;
68
69 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
70 priv->offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET]));
71 priv->len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN]));
72 if (priv->len == 0 ||
73 priv->len > FIELD_SIZEOF(struct nft_data, data))
74 return -EINVAL;
75
76 priv->dreg = ntohl(nla_get_be32(tb[NFTA_EXTHDR_DREG]));
77 err = nft_validate_output_register(priv->dreg);
78 if (err < 0)
79 return err;
80 return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
81}
82
83static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
84{
85 const struct nft_exthdr *priv = nft_expr_priv(expr);
86
87 if (nla_put_be32(skb, NFTA_EXTHDR_DREG, htonl(priv->dreg)))
88 goto nla_put_failure;
89 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
90 goto nla_put_failure;
91 if (nla_put_be32(skb, NFTA_EXTHDR_OFFSET, htonl(priv->offset)))
92 goto nla_put_failure;
93 if (nla_put_be32(skb, NFTA_EXTHDR_LEN, htonl(priv->len)))
94 goto nla_put_failure;
95 return 0;
96
97nla_put_failure:
98 return -1;
99}
100
101static struct nft_expr_type nft_exthdr_type;
102static const struct nft_expr_ops nft_exthdr_ops = {
103 .type = &nft_exthdr_type,
104 .size = NFT_EXPR_SIZE(sizeof(struct nft_exthdr)),
105 .eval = nft_exthdr_eval,
106 .init = nft_exthdr_init,
107 .dump = nft_exthdr_dump,
108};
109
110static struct nft_expr_type nft_exthdr_type __read_mostly = {
111 .name = "exthdr",
112 .ops = &nft_exthdr_ops,
113 .policy = nft_exthdr_policy,
114 .maxattr = NFTA_EXTHDR_MAX,
115 .owner = THIS_MODULE,
116};
117
118static int __init nft_exthdr_module_init(void)
119{
120 return nft_register_expr(&nft_exthdr_type);
121}
122
123static void __exit nft_exthdr_module_exit(void)
124{
125 nft_unregister_expr(&nft_exthdr_type);
126}
127
128module_init(nft_exthdr_module_init);
129module_exit(nft_exthdr_module_exit);
130
131MODULE_LICENSE("GPL");
132MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
133MODULE_ALIAS_NFT_EXPR("exthdr");
diff --git a/net/netfilter/nft_hash.c b/net/netfilter/nft_hash.c
new file mode 100644
index 000000000000..3d3f8fce10a5
--- /dev/null
+++ b/net/netfilter/nft_hash.c
@@ -0,0 +1,231 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
15#include <linux/jhash.h>
16#include <linux/netlink.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables.h>
20
21struct nft_hash {
22 struct hlist_head *hash;
23 unsigned int hsize;
24};
25
26struct nft_hash_elem {
27 struct hlist_node hnode;
28 struct nft_data key;
29 struct nft_data data[];
30};
31
32static u32 nft_hash_rnd __read_mostly;
33static bool nft_hash_rnd_initted __read_mostly;
34
35static unsigned int nft_hash_data(const struct nft_data *data,
36 unsigned int hsize, unsigned int len)
37{
38 unsigned int h;
39
40 h = jhash(data->data, len, nft_hash_rnd);
41 return ((u64)h * hsize) >> 32;
42}
43
44static bool nft_hash_lookup(const struct nft_set *set,
45 const struct nft_data *key,
46 struct nft_data *data)
47{
48 const struct nft_hash *priv = nft_set_priv(set);
49 const struct nft_hash_elem *he;
50 unsigned int h;
51
52 h = nft_hash_data(key, priv->hsize, set->klen);
53 hlist_for_each_entry(he, &priv->hash[h], hnode) {
54 if (nft_data_cmp(&he->key, key, set->klen))
55 continue;
56 if (set->flags & NFT_SET_MAP)
57 nft_data_copy(data, he->data);
58 return true;
59 }
60 return false;
61}
62
63static void nft_hash_elem_destroy(const struct nft_set *set,
64 struct nft_hash_elem *he)
65{
66 nft_data_uninit(&he->key, NFT_DATA_VALUE);
67 if (set->flags & NFT_SET_MAP)
68 nft_data_uninit(he->data, set->dtype);
69 kfree(he);
70}
71
72static int nft_hash_insert(const struct nft_set *set,
73 const struct nft_set_elem *elem)
74{
75 struct nft_hash *priv = nft_set_priv(set);
76 struct nft_hash_elem *he;
77 unsigned int size, h;
78
79 if (elem->flags != 0)
80 return -EINVAL;
81
82 size = sizeof(*he);
83 if (set->flags & NFT_SET_MAP)
84 size += sizeof(he->data[0]);
85
86 he = kzalloc(size, GFP_KERNEL);
87 if (he == NULL)
88 return -ENOMEM;
89
90 nft_data_copy(&he->key, &elem->key);
91 if (set->flags & NFT_SET_MAP)
92 nft_data_copy(he->data, &elem->data);
93
94 h = nft_hash_data(&he->key, priv->hsize, set->klen);
95 hlist_add_head_rcu(&he->hnode, &priv->hash[h]);
96 return 0;
97}
98
99static void nft_hash_remove(const struct nft_set *set,
100 const struct nft_set_elem *elem)
101{
102 struct nft_hash_elem *he = elem->cookie;
103
104 hlist_del_rcu(&he->hnode);
105 kfree(he);
106}
107
108static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem)
109{
110 const struct nft_hash *priv = nft_set_priv(set);
111 struct nft_hash_elem *he;
112 unsigned int h;
113
114 h = nft_hash_data(&elem->key, priv->hsize, set->klen);
115 hlist_for_each_entry(he, &priv->hash[h], hnode) {
116 if (nft_data_cmp(&he->key, &elem->key, set->klen))
117 continue;
118
119 elem->cookie = he;
120 elem->flags = 0;
121 if (set->flags & NFT_SET_MAP)
122 nft_data_copy(&elem->data, he->data);
123 return 0;
124 }
125 return -ENOENT;
126}
127
128static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
129 struct nft_set_iter *iter)
130{
131 const struct nft_hash *priv = nft_set_priv(set);
132 const struct nft_hash_elem *he;
133 struct nft_set_elem elem;
134 unsigned int i;
135
136 for (i = 0; i < priv->hsize; i++) {
137 hlist_for_each_entry(he, &priv->hash[i], hnode) {
138 if (iter->count < iter->skip)
139 goto cont;
140
141 memcpy(&elem.key, &he->key, sizeof(elem.key));
142 if (set->flags & NFT_SET_MAP)
143 memcpy(&elem.data, he->data, sizeof(elem.data));
144 elem.flags = 0;
145
146 iter->err = iter->fn(ctx, set, iter, &elem);
147 if (iter->err < 0)
148 return;
149cont:
150 iter->count++;
151 }
152 }
153}
154
155static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
156{
157 return sizeof(struct nft_hash);
158}
159
160static int nft_hash_init(const struct nft_set *set,
161 const struct nlattr * const tb[])
162{
163 struct nft_hash *priv = nft_set_priv(set);
164 unsigned int cnt, i;
165
166 if (unlikely(!nft_hash_rnd_initted)) {
167 get_random_bytes(&nft_hash_rnd, 4);
168 nft_hash_rnd_initted = true;
169 }
170
171 /* Aim for a load factor of 0.75 */
172 // FIXME: temporarily broken until we have set descriptions
173 cnt = 100;
174 cnt = cnt * 4 / 3;
175
176 priv->hash = kcalloc(cnt, sizeof(struct hlist_head), GFP_KERNEL);
177 if (priv->hash == NULL)
178 return -ENOMEM;
179 priv->hsize = cnt;
180
181 for (i = 0; i < cnt; i++)
182 INIT_HLIST_HEAD(&priv->hash[i]);
183
184 return 0;
185}
186
187static void nft_hash_destroy(const struct nft_set *set)
188{
189 const struct nft_hash *priv = nft_set_priv(set);
190 const struct hlist_node *next;
191 struct nft_hash_elem *elem;
192 unsigned int i;
193
194 for (i = 0; i < priv->hsize; i++) {
195 hlist_for_each_entry_safe(elem, next, &priv->hash[i], hnode) {
196 hlist_del(&elem->hnode);
197 nft_hash_elem_destroy(set, elem);
198 }
199 }
200 kfree(priv->hash);
201}
202
203static struct nft_set_ops nft_hash_ops __read_mostly = {
204 .privsize = nft_hash_privsize,
205 .init = nft_hash_init,
206 .destroy = nft_hash_destroy,
207 .get = nft_hash_get,
208 .insert = nft_hash_insert,
209 .remove = nft_hash_remove,
210 .lookup = nft_hash_lookup,
211 .walk = nft_hash_walk,
212 .features = NFT_SET_MAP,
213 .owner = THIS_MODULE,
214};
215
216static int __init nft_hash_module_init(void)
217{
218 return nft_register_set(&nft_hash_ops);
219}
220
221static void __exit nft_hash_module_exit(void)
222{
223 nft_unregister_set(&nft_hash_ops);
224}
225
226module_init(nft_hash_module_init);
227module_exit(nft_hash_module_exit);
228
229MODULE_LICENSE("GPL");
230MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
231MODULE_ALIAS_NFT_SET();
diff --git a/net/netfilter/nft_immediate.c b/net/netfilter/nft_immediate.c
new file mode 100644
index 000000000000..f169501f1ad4
--- /dev/null
+++ b/net/netfilter/nft_immediate.c
@@ -0,0 +1,132 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_immediate_expr {
21 struct nft_data data;
22 enum nft_registers dreg:8;
23 u8 dlen;
24};
25
26static void nft_immediate_eval(const struct nft_expr *expr,
27 struct nft_data data[NFT_REG_MAX + 1],
28 const struct nft_pktinfo *pkt)
29{
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
31
32 nft_data_copy(&data[priv->dreg], &priv->data);
33}
34
35static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
36 [NFTA_IMMEDIATE_DREG] = { .type = NLA_U32 },
37 [NFTA_IMMEDIATE_DATA] = { .type = NLA_NESTED },
38};
39
40static int nft_immediate_init(const struct nft_ctx *ctx,
41 const struct nft_expr *expr,
42 const struct nlattr * const tb[])
43{
44 struct nft_immediate_expr *priv = nft_expr_priv(expr);
45 struct nft_data_desc desc;
46 int err;
47
48 if (tb[NFTA_IMMEDIATE_DREG] == NULL ||
49 tb[NFTA_IMMEDIATE_DATA] == NULL)
50 return -EINVAL;
51
52 priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG]));
53 err = nft_validate_output_register(priv->dreg);
54 if (err < 0)
55 return err;
56
57 err = nft_data_init(ctx, &priv->data, &desc, tb[NFTA_IMMEDIATE_DATA]);
58 if (err < 0)
59 return err;
60 priv->dlen = desc.len;
61
62 err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type);
63 if (err < 0)
64 goto err1;
65
66 return 0;
67
68err1:
69 nft_data_uninit(&priv->data, desc.type);
70 return err;
71}
72
73static void nft_immediate_destroy(const struct nft_expr *expr)
74{
75 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
76 return nft_data_uninit(&priv->data, nft_dreg_to_type(priv->dreg));
77}
78
79static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
80{
81 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
82
83 if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg)))
84 goto nla_put_failure;
85
86 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
87 nft_dreg_to_type(priv->dreg), priv->dlen);
88
89nla_put_failure:
90 return -1;
91}
92
93static int nft_immediate_validate(const struct nft_ctx *ctx,
94 const struct nft_expr *expr,
95 const struct nft_data **data)
96{
97 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
98
99 if (priv->dreg == NFT_REG_VERDICT)
100 *data = &priv->data;
101
102 return 0;
103}
104
105static struct nft_expr_type nft_imm_type;
106static const struct nft_expr_ops nft_imm_ops = {
107 .type = &nft_imm_type,
108 .size = NFT_EXPR_SIZE(sizeof(struct nft_immediate_expr)),
109 .eval = nft_immediate_eval,
110 .init = nft_immediate_init,
111 .destroy = nft_immediate_destroy,
112 .dump = nft_immediate_dump,
113 .validate = nft_immediate_validate,
114};
115
116static struct nft_expr_type nft_imm_type __read_mostly = {
117 .name = "immediate",
118 .ops = &nft_imm_ops,
119 .policy = nft_immediate_policy,
120 .maxattr = NFTA_IMMEDIATE_MAX,
121 .owner = THIS_MODULE,
122};
123
124int __init nft_immediate_module_init(void)
125{
126 return nft_register_expr(&nft_imm_type);
127}
128
129void nft_immediate_module_exit(void)
130{
131 nft_unregister_expr(&nft_imm_type);
132}
diff --git a/net/netfilter/nft_limit.c b/net/netfilter/nft_limit.c
new file mode 100644
index 000000000000..85da5bd02f64
--- /dev/null
+++ b/net/netfilter/nft_limit.c
@@ -0,0 +1,119 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/spinlock.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
20static DEFINE_SPINLOCK(limit_lock);
21
22struct nft_limit {
23 u64 tokens;
24 u64 rate;
25 u64 unit;
26 unsigned long stamp;
27};
28
29static void nft_limit_eval(const struct nft_expr *expr,
30 struct nft_data data[NFT_REG_MAX + 1],
31 const struct nft_pktinfo *pkt)
32{
33 struct nft_limit *priv = nft_expr_priv(expr);
34
35 spin_lock_bh(&limit_lock);
36 if (time_after_eq(jiffies, priv->stamp)) {
37 priv->tokens = priv->rate;
38 priv->stamp = jiffies + priv->unit * HZ;
39 }
40
41 if (priv->tokens >= 1) {
42 priv->tokens--;
43 spin_unlock_bh(&limit_lock);
44 return;
45 }
46 spin_unlock_bh(&limit_lock);
47
48 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
49}
50
51static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
52 [NFTA_LIMIT_RATE] = { .type = NLA_U64 },
53 [NFTA_LIMIT_UNIT] = { .type = NLA_U64 },
54};
55
56static int nft_limit_init(const struct nft_ctx *ctx,
57 const struct nft_expr *expr,
58 const struct nlattr * const tb[])
59{
60 struct nft_limit *priv = nft_expr_priv(expr);
61
62 if (tb[NFTA_LIMIT_RATE] == NULL ||
63 tb[NFTA_LIMIT_UNIT] == NULL)
64 return -EINVAL;
65
66 priv->rate = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_RATE]));
67 priv->unit = be64_to_cpu(nla_get_be64(tb[NFTA_LIMIT_UNIT]));
68 priv->stamp = jiffies + priv->unit * HZ;
69 priv->tokens = priv->rate;
70 return 0;
71}
72
73static int nft_limit_dump(struct sk_buff *skb, const struct nft_expr *expr)
74{
75 const struct nft_limit *priv = nft_expr_priv(expr);
76
77 if (nla_put_be64(skb, NFTA_LIMIT_RATE, cpu_to_be64(priv->rate)))
78 goto nla_put_failure;
79 if (nla_put_be64(skb, NFTA_LIMIT_UNIT, cpu_to_be64(priv->unit)))
80 goto nla_put_failure;
81 return 0;
82
83nla_put_failure:
84 return -1;
85}
86
87static struct nft_expr_type nft_limit_type;
88static const struct nft_expr_ops nft_limit_ops = {
89 .type = &nft_limit_type,
90 .size = NFT_EXPR_SIZE(sizeof(struct nft_limit)),
91 .eval = nft_limit_eval,
92 .init = nft_limit_init,
93 .dump = nft_limit_dump,
94};
95
96static struct nft_expr_type nft_limit_type __read_mostly = {
97 .name = "limit",
98 .ops = &nft_limit_ops,
99 .policy = nft_limit_policy,
100 .maxattr = NFTA_LIMIT_MAX,
101 .owner = THIS_MODULE,
102};
103
104static int __init nft_limit_module_init(void)
105{
106 return nft_register_expr(&nft_limit_type);
107}
108
109static void __exit nft_limit_module_exit(void)
110{
111 nft_unregister_expr(&nft_limit_type);
112}
113
114module_init(nft_limit_module_init);
115module_exit(nft_limit_module_exit);
116
117MODULE_LICENSE("GPL");
118MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
119MODULE_ALIAS_NFT_EXPR("limit");
diff --git a/net/netfilter/nft_log.c b/net/netfilter/nft_log.c
new file mode 100644
index 000000000000..57cad072a13e
--- /dev/null
+++ b/net/netfilter/nft_log.c
@@ -0,0 +1,146 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables.h>
18#include <net/netfilter/nf_log.h>
19#include <linux/netdevice.h>
20
21static const char *nft_log_null_prefix = "";
22
23struct nft_log {
24 struct nf_loginfo loginfo;
25 char *prefix;
26 int family;
27};
28
29static void nft_log_eval(const struct nft_expr *expr,
30 struct nft_data data[NFT_REG_MAX + 1],
31 const struct nft_pktinfo *pkt)
32{
33 const struct nft_log *priv = nft_expr_priv(expr);
34 struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
35
36 nf_log_packet(net, priv->family, pkt->hooknum, pkt->skb, pkt->in,
37 pkt->out, &priv->loginfo, "%s", priv->prefix);
38}
39
40static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
41 [NFTA_LOG_GROUP] = { .type = NLA_U16 },
42 [NFTA_LOG_PREFIX] = { .type = NLA_STRING },
43 [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
44 [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
45};
46
47static int nft_log_init(const struct nft_ctx *ctx,
48 const struct nft_expr *expr,
49 const struct nlattr * const tb[])
50{
51 struct nft_log *priv = nft_expr_priv(expr);
52 struct nf_loginfo *li = &priv->loginfo;
53 const struct nlattr *nla;
54
55 priv->family = ctx->afi->family;
56
57 nla = tb[NFTA_LOG_PREFIX];
58 if (nla != NULL) {
59 priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
60 if (priv->prefix == NULL)
61 return -ENOMEM;
62 nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
63 } else
64 priv->prefix = (char *)nft_log_null_prefix;
65
66 li->type = NF_LOG_TYPE_ULOG;
67 if (tb[NFTA_LOG_GROUP] != NULL)
68 li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
69
70 if (tb[NFTA_LOG_SNAPLEN] != NULL)
71 li->u.ulog.copy_len = ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
72 if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
73 li->u.ulog.qthreshold =
74 ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
75 }
76
77 return 0;
78}
79
80static void nft_log_destroy(const struct nft_expr *expr)
81{
82 struct nft_log *priv = nft_expr_priv(expr);
83
84 if (priv->prefix != nft_log_null_prefix)
85 kfree(priv->prefix);
86}
87
88static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
89{
90 const struct nft_log *priv = nft_expr_priv(expr);
91 const struct nf_loginfo *li = &priv->loginfo;
92
93 if (priv->prefix != nft_log_null_prefix)
94 if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
95 goto nla_put_failure;
96 if (li->u.ulog.group)
97 if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
98 goto nla_put_failure;
99 if (li->u.ulog.copy_len)
100 if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
101 htonl(li->u.ulog.copy_len)))
102 goto nla_put_failure;
103 if (li->u.ulog.qthreshold)
104 if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
105 htons(li->u.ulog.qthreshold)))
106 goto nla_put_failure;
107 return 0;
108
109nla_put_failure:
110 return -1;
111}
112
113static struct nft_expr_type nft_log_type;
114static const struct nft_expr_ops nft_log_ops = {
115 .type = &nft_log_type,
116 .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
117 .eval = nft_log_eval,
118 .init = nft_log_init,
119 .destroy = nft_log_destroy,
120 .dump = nft_log_dump,
121};
122
123static struct nft_expr_type nft_log_type __read_mostly = {
124 .name = "log",
125 .ops = &nft_log_ops,
126 .policy = nft_log_policy,
127 .maxattr = NFTA_LOG_MAX,
128 .owner = THIS_MODULE,
129};
130
131static int __init nft_log_module_init(void)
132{
133 return nft_register_expr(&nft_log_type);
134}
135
136static void __exit nft_log_module_exit(void)
137{
138 nft_unregister_expr(&nft_log_type);
139}
140
141module_init(nft_log_module_init);
142module_exit(nft_log_module_exit);
143
144MODULE_LICENSE("GPL");
145MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
146MODULE_ALIAS_NFT_EXPR("log");
diff --git a/net/netfilter/nft_lookup.c b/net/netfilter/nft_lookup.c
new file mode 100644
index 000000000000..8a6116b75b5a
--- /dev/null
+++ b/net/netfilter/nft_lookup.c
@@ -0,0 +1,141 @@
1/*
2 * Copyright (c) 2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/rbtree.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_lookup {
21 struct nft_set *set;
22 enum nft_registers sreg:8;
23 enum nft_registers dreg:8;
24 struct nft_set_binding binding;
25};
26
27static void nft_lookup_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1],
29 const struct nft_pktinfo *pkt)
30{
31 const struct nft_lookup *priv = nft_expr_priv(expr);
32 const struct nft_set *set = priv->set;
33
34 if (set->ops->lookup(set, &data[priv->sreg], &data[priv->dreg]))
35 return;
36 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
37}
38
39static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
40 [NFTA_LOOKUP_SET] = { .type = NLA_STRING },
41 [NFTA_LOOKUP_SREG] = { .type = NLA_U32 },
42 [NFTA_LOOKUP_DREG] = { .type = NLA_U32 },
43};
44
45static int nft_lookup_init(const struct nft_ctx *ctx,
46 const struct nft_expr *expr,
47 const struct nlattr * const tb[])
48{
49 struct nft_lookup *priv = nft_expr_priv(expr);
50 struct nft_set *set;
51 int err;
52
53 if (tb[NFTA_LOOKUP_SET] == NULL ||
54 tb[NFTA_LOOKUP_SREG] == NULL)
55 return -EINVAL;
56
57 set = nf_tables_set_lookup(ctx->table, tb[NFTA_LOOKUP_SET]);
58 if (IS_ERR(set))
59 return PTR_ERR(set);
60
61 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG]));
62 err = nft_validate_input_register(priv->sreg);
63 if (err < 0)
64 return err;
65
66 if (tb[NFTA_LOOKUP_DREG] != NULL) {
67 if (!(set->flags & NFT_SET_MAP))
68 return -EINVAL;
69
70 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG]));
71 err = nft_validate_output_register(priv->dreg);
72 if (err < 0)
73 return err;
74
75 if (priv->dreg == NFT_REG_VERDICT) {
76 if (set->dtype != NFT_DATA_VERDICT)
77 return -EINVAL;
78 } else if (set->dtype == NFT_DATA_VERDICT)
79 return -EINVAL;
80 } else if (set->flags & NFT_SET_MAP)
81 return -EINVAL;
82
83 err = nf_tables_bind_set(ctx, set, &priv->binding);
84 if (err < 0)
85 return err;
86
87 priv->set = set;
88 return 0;
89}
90
91static void nft_lookup_destroy(const struct nft_expr *expr)
92{
93 struct nft_lookup *priv = nft_expr_priv(expr);
94
95 nf_tables_unbind_set(NULL, priv->set, &priv->binding);
96}
97
98static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
99{
100 const struct nft_lookup *priv = nft_expr_priv(expr);
101
102 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
103 goto nla_put_failure;
104 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg)))
105 goto nla_put_failure;
106 if (priv->set->flags & NFT_SET_MAP)
107 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg)))
108 goto nla_put_failure;
109 return 0;
110
111nla_put_failure:
112 return -1;
113}
114
115static struct nft_expr_type nft_lookup_type;
116static const struct nft_expr_ops nft_lookup_ops = {
117 .type = &nft_lookup_type,
118 .size = NFT_EXPR_SIZE(sizeof(struct nft_lookup)),
119 .eval = nft_lookup_eval,
120 .init = nft_lookup_init,
121 .destroy = nft_lookup_destroy,
122 .dump = nft_lookup_dump,
123};
124
125static struct nft_expr_type nft_lookup_type __read_mostly = {
126 .name = "lookup",
127 .ops = &nft_lookup_ops,
128 .policy = nft_lookup_policy,
129 .maxattr = NFTA_LOOKUP_MAX,
130 .owner = THIS_MODULE,
131};
132
133int __init nft_lookup_module_init(void)
134{
135 return nft_register_expr(&nft_lookup_type);
136}
137
138void nft_lookup_module_exit(void)
139{
140 nft_unregister_expr(&nft_lookup_type);
141}
diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c
new file mode 100644
index 000000000000..8c28220a90b3
--- /dev/null
+++ b/net/netfilter/nft_meta.c
@@ -0,0 +1,228 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/dst.h>
18#include <net/sock.h>
19#include <net/tcp_states.h> /* for TCP_TIME_WAIT */
20#include <net/netfilter/nf_tables.h>
21
22struct nft_meta {
23 enum nft_meta_keys key:8;
24 enum nft_registers dreg:8;
25};
26
27static void nft_meta_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1],
29 const struct nft_pktinfo *pkt)
30{
31 const struct nft_meta *priv = nft_expr_priv(expr);
32 const struct sk_buff *skb = pkt->skb;
33 const struct net_device *in = pkt->in, *out = pkt->out;
34 struct nft_data *dest = &data[priv->dreg];
35
36 switch (priv->key) {
37 case NFT_META_LEN:
38 dest->data[0] = skb->len;
39 break;
40 case NFT_META_PROTOCOL:
41 *(__be16 *)dest->data = skb->protocol;
42 break;
43 case NFT_META_PRIORITY:
44 dest->data[0] = skb->priority;
45 break;
46 case NFT_META_MARK:
47 dest->data[0] = skb->mark;
48 break;
49 case NFT_META_IIF:
50 if (in == NULL)
51 goto err;
52 dest->data[0] = in->ifindex;
53 break;
54 case NFT_META_OIF:
55 if (out == NULL)
56 goto err;
57 dest->data[0] = out->ifindex;
58 break;
59 case NFT_META_IIFNAME:
60 if (in == NULL)
61 goto err;
62 strncpy((char *)dest->data, in->name, sizeof(dest->data));
63 break;
64 case NFT_META_OIFNAME:
65 if (out == NULL)
66 goto err;
67 strncpy((char *)dest->data, out->name, sizeof(dest->data));
68 break;
69 case NFT_META_IIFTYPE:
70 if (in == NULL)
71 goto err;
72 *(u16 *)dest->data = in->type;
73 break;
74 case NFT_META_OIFTYPE:
75 if (out == NULL)
76 goto err;
77 *(u16 *)dest->data = out->type;
78 break;
79 case NFT_META_SKUID:
80 if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT)
81 goto err;
82
83 read_lock_bh(&skb->sk->sk_callback_lock);
84 if (skb->sk->sk_socket == NULL ||
85 skb->sk->sk_socket->file == NULL) {
86 read_unlock_bh(&skb->sk->sk_callback_lock);
87 goto err;
88 }
89
90 dest->data[0] =
91 from_kuid_munged(&init_user_ns,
92 skb->sk->sk_socket->file->f_cred->fsuid);
93 read_unlock_bh(&skb->sk->sk_callback_lock);
94 break;
95 case NFT_META_SKGID:
96 if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT)
97 goto err;
98
99 read_lock_bh(&skb->sk->sk_callback_lock);
100 if (skb->sk->sk_socket == NULL ||
101 skb->sk->sk_socket->file == NULL) {
102 read_unlock_bh(&skb->sk->sk_callback_lock);
103 goto err;
104 }
105 dest->data[0] =
106 from_kgid_munged(&init_user_ns,
107 skb->sk->sk_socket->file->f_cred->fsgid);
108 read_unlock_bh(&skb->sk->sk_callback_lock);
109 break;
110#ifdef CONFIG_NET_CLS_ROUTE
111 case NFT_META_RTCLASSID: {
112 const struct dst_entry *dst = skb_dst(skb);
113
114 if (dst == NULL)
115 goto err;
116 dest->data[0] = dst->tclassid;
117 break;
118 }
119#endif
120#ifdef CONFIG_NETWORK_SECMARK
121 case NFT_META_SECMARK:
122 dest->data[0] = skb->secmark;
123 break;
124#endif
125 default:
126 WARN_ON(1);
127 goto err;
128 }
129 return;
130
131err:
132 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
133}
134
135static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
136 [NFTA_META_DREG] = { .type = NLA_U32 },
137 [NFTA_META_KEY] = { .type = NLA_U32 },
138};
139
140static int nft_meta_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
141 const struct nlattr * const tb[])
142{
143 struct nft_meta *priv = nft_expr_priv(expr);
144 int err;
145
146 if (tb[NFTA_META_DREG] == NULL ||
147 tb[NFTA_META_KEY] == NULL)
148 return -EINVAL;
149
150 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
151 switch (priv->key) {
152 case NFT_META_LEN:
153 case NFT_META_PROTOCOL:
154 case NFT_META_PRIORITY:
155 case NFT_META_MARK:
156 case NFT_META_IIF:
157 case NFT_META_OIF:
158 case NFT_META_IIFNAME:
159 case NFT_META_OIFNAME:
160 case NFT_META_IIFTYPE:
161 case NFT_META_OIFTYPE:
162 case NFT_META_SKUID:
163 case NFT_META_SKGID:
164#ifdef CONFIG_NET_CLS_ROUTE
165 case NFT_META_RTCLASSID:
166#endif
167#ifdef CONFIG_NETWORK_SECMARK
168 case NFT_META_SECMARK:
169#endif
170 break;
171 default:
172 return -EOPNOTSUPP;
173 }
174
175 priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG]));
176 err = nft_validate_output_register(priv->dreg);
177 if (err < 0)
178 return err;
179 return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
180}
181
182static int nft_meta_dump(struct sk_buff *skb, const struct nft_expr *expr)
183{
184 const struct nft_meta *priv = nft_expr_priv(expr);
185
186 if (nla_put_be32(skb, NFTA_META_DREG, htonl(priv->dreg)))
187 goto nla_put_failure;
188 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
189 goto nla_put_failure;
190 return 0;
191
192nla_put_failure:
193 return -1;
194}
195
196static struct nft_expr_type nft_meta_type;
197static const struct nft_expr_ops nft_meta_ops = {
198 .type = &nft_meta_type,
199 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
200 .eval = nft_meta_eval,
201 .init = nft_meta_init,
202 .dump = nft_meta_dump,
203};
204
205static struct nft_expr_type nft_meta_type __read_mostly = {
206 .name = "meta",
207 .ops = &nft_meta_ops,
208 .policy = nft_meta_policy,
209 .maxattr = NFTA_META_MAX,
210 .owner = THIS_MODULE,
211};
212
213static int __init nft_meta_module_init(void)
214{
215 return nft_register_expr(&nft_meta_type);
216}
217
218static void __exit nft_meta_module_exit(void)
219{
220 nft_unregister_expr(&nft_meta_type);
221}
222
223module_init(nft_meta_module_init);
224module_exit(nft_meta_module_exit);
225
226MODULE_LICENSE("GPL");
227MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
228MODULE_ALIAS_NFT_EXPR("meta");
diff --git a/net/netfilter/nft_meta_target.c b/net/netfilter/nft_meta_target.c
new file mode 100644
index 000000000000..71177df75ffb
--- /dev/null
+++ b/net/netfilter/nft_meta_target.c
@@ -0,0 +1,117 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/list.h>
14#include <linux/rbtree.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19
20struct nft_meta {
21 enum nft_meta_keys key;
22};
23
24static void nft_meta_eval(const struct nft_expr *expr,
25 struct nft_data *nfres,
26 struct nft_data *data,
27 const struct nft_pktinfo *pkt)
28{
29 const struct nft_meta *meta = nft_expr_priv(expr);
30 struct sk_buff *skb = pkt->skb;
31 u32 val = data->data[0];
32
33 switch (meta->key) {
34 case NFT_META_MARK:
35 skb->mark = val;
36 break;
37 case NFT_META_PRIORITY:
38 skb->priority = val;
39 break;
40 case NFT_META_NFTRACE:
41 skb->nf_trace = val;
42 break;
43#ifdef CONFIG_NETWORK_SECMARK
44 case NFT_META_SECMARK:
45 skb->secmark = val;
46 break;
47#endif
48 default:
49 WARN_ON(1);
50 }
51}
52
53static const struct nla_policy nft_meta_policy[NFTA_META_MAX + 1] = {
54 [NFTA_META_KEY] = { .type = NLA_U32 },
55};
56
57static int nft_meta_init(const struct nft_expr *expr, struct nlattr *tb[])
58{
59 struct nft_meta *meta = nft_expr_priv(expr);
60
61 if (tb[NFTA_META_KEY] == NULL)
62 return -EINVAL;
63
64 meta->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
65 switch (meta->key) {
66 case NFT_META_MARK:
67 case NFT_META_PRIORITY:
68 case NFT_META_NFTRACE:
69#ifdef CONFIG_NETWORK_SECMARK
70 case NFT_META_SECMARK:
71#endif
72 break;
73 default:
74 return -EINVAL;
75 }
76
77 return 0;
78}
79
80static int nft_meta_dump(struct sk_buff *skb, const struct nft_expr *expr)
81{
82 struct nft_meta *meta = nft_expr_priv(expr);
83
84 NLA_PUT_BE32(skb, NFTA_META_KEY, htonl(meta->key));
85 return 0;
86
87nla_put_failure:
88 return -1;
89}
90
91static struct nft_expr_ops meta_target __read_mostly = {
92 .name = "meta",
93 .size = NFT_EXPR_SIZE(sizeof(struct nft_meta)),
94 .owner = THIS_MODULE,
95 .eval = nft_meta_eval,
96 .init = nft_meta_init,
97 .dump = nft_meta_dump,
98 .policy = nft_meta_policy,
99 .maxattr = NFTA_META_MAX,
100};
101
102static int __init nft_meta_target_init(void)
103{
104 return nft_register_expr(&meta_target);
105}
106
107static void __exit nft_meta_target_exit(void)
108{
109 nft_unregister_expr(&meta_target);
110}
111
112module_init(nft_meta_target_init);
113module_exit(nft_meta_target_exit);
114
115MODULE_LICENSE("GPL");
116MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
117MODULE_ALIAS_NFT_EXPR("meta");
diff --git a/net/netfilter/nft_nat.c b/net/netfilter/nft_nat.c
new file mode 100644
index 000000000000..d3b1ffe26181
--- /dev/null
+++ b/net/netfilter/nft_nat.c
@@ -0,0 +1,224 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 * Copyright (c) 2012 Intel Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/skbuff.h>
15#include <linux/ip.h>
16#include <linux/string.h>
17#include <linux/netlink.h>
18#include <linux/netfilter.h>
19#include <linux/netfilter_ipv4.h>
20#include <linux/netfilter/nfnetlink.h>
21#include <linux/netfilter/nf_tables.h>
22#include <net/netfilter/nf_conntrack.h>
23#include <net/netfilter/nf_nat.h>
24#include <net/netfilter/nf_nat_core.h>
25#include <net/netfilter/nf_tables.h>
26#include <net/netfilter/nf_nat_l3proto.h>
27#include <net/ip.h>
28
29struct nft_nat {
30 enum nft_registers sreg_addr_min:8;
31 enum nft_registers sreg_addr_max:8;
32 enum nft_registers sreg_proto_min:8;
33 enum nft_registers sreg_proto_max:8;
34 int family;
35 enum nf_nat_manip_type type;
36};
37
38static void nft_nat_eval(const struct nft_expr *expr,
39 struct nft_data data[NFT_REG_MAX + 1],
40 const struct nft_pktinfo *pkt)
41{
42 const struct nft_nat *priv = nft_expr_priv(expr);
43 enum ip_conntrack_info ctinfo;
44 struct nf_conn *ct = nf_ct_get(pkt->skb, &ctinfo);
45 struct nf_nat_range range;
46
47 memset(&range, 0, sizeof(range));
48 if (priv->sreg_addr_min) {
49 if (priv->family == AF_INET) {
50 range.min_addr.ip = (__force __be32)
51 data[priv->sreg_addr_min].data[0];
52 range.max_addr.ip = (__force __be32)
53 data[priv->sreg_addr_max].data[0];
54
55 } else {
56 memcpy(range.min_addr.ip6,
57 data[priv->sreg_addr_min].data,
58 sizeof(struct nft_data));
59 memcpy(range.max_addr.ip6,
60 data[priv->sreg_addr_max].data,
61 sizeof(struct nft_data));
62 }
63 range.flags |= NF_NAT_RANGE_MAP_IPS;
64 }
65
66 if (priv->sreg_proto_min) {
67 range.min_proto.all = (__force __be16)
68 data[priv->sreg_proto_min].data[0];
69 range.max_proto.all = (__force __be16)
70 data[priv->sreg_proto_max].data[0];
71 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
72 }
73
74 data[NFT_REG_VERDICT].verdict =
75 nf_nat_setup_info(ct, &range, priv->type);
76}
77
78static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
79 [NFTA_NAT_TYPE] = { .type = NLA_U32 },
80 [NFTA_NAT_FAMILY] = { .type = NLA_U32 },
81 [NFTA_NAT_REG_ADDR_MIN] = { .type = NLA_U32 },
82 [NFTA_NAT_REG_ADDR_MAX] = { .type = NLA_U32 },
83 [NFTA_NAT_REG_PROTO_MIN] = { .type = NLA_U32 },
84 [NFTA_NAT_REG_PROTO_MAX] = { .type = NLA_U32 },
85};
86
87static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
88 const struct nlattr * const tb[])
89{
90 struct nft_nat *priv = nft_expr_priv(expr);
91 int err;
92
93 if (tb[NFTA_NAT_TYPE] == NULL)
94 return -EINVAL;
95
96 switch (ntohl(nla_get_be32(tb[NFTA_NAT_TYPE]))) {
97 case NFT_NAT_SNAT:
98 priv->type = NF_NAT_MANIP_SRC;
99 break;
100 case NFT_NAT_DNAT:
101 priv->type = NF_NAT_MANIP_DST;
102 break;
103 default:
104 return -EINVAL;
105 }
106
107 if (tb[NFTA_NAT_FAMILY] == NULL)
108 return -EINVAL;
109
110 priv->family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
111 if (priv->family != AF_INET && priv->family != AF_INET6)
112 return -EINVAL;
113
114 if (tb[NFTA_NAT_REG_ADDR_MIN]) {
115 priv->sreg_addr_min = ntohl(nla_get_be32(
116 tb[NFTA_NAT_REG_ADDR_MIN]));
117 err = nft_validate_input_register(priv->sreg_addr_min);
118 if (err < 0)
119 return err;
120 }
121
122 if (tb[NFTA_NAT_REG_ADDR_MAX]) {
123 priv->sreg_addr_max = ntohl(nla_get_be32(
124 tb[NFTA_NAT_REG_ADDR_MAX]));
125 err = nft_validate_input_register(priv->sreg_addr_max);
126 if (err < 0)
127 return err;
128 } else
129 priv->sreg_addr_max = priv->sreg_addr_min;
130
131 if (tb[NFTA_NAT_REG_PROTO_MIN]) {
132 priv->sreg_proto_min = ntohl(nla_get_be32(
133 tb[NFTA_NAT_REG_PROTO_MIN]));
134 err = nft_validate_input_register(priv->sreg_proto_min);
135 if (err < 0)
136 return err;
137 }
138
139 if (tb[NFTA_NAT_REG_PROTO_MAX]) {
140 priv->sreg_proto_max = ntohl(nla_get_be32(
141 tb[NFTA_NAT_REG_PROTO_MAX]));
142 err = nft_validate_input_register(priv->sreg_proto_max);
143 if (err < 0)
144 return err;
145 } else
146 priv->sreg_proto_max = priv->sreg_proto_min;
147
148 return 0;
149}
150
151static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
152{
153 const struct nft_nat *priv = nft_expr_priv(expr);
154
155 switch (priv->type) {
156 case NF_NAT_MANIP_SRC:
157 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_SNAT)))
158 goto nla_put_failure;
159 break;
160 case NF_NAT_MANIP_DST:
161 if (nla_put_be32(skb, NFTA_NAT_TYPE, htonl(NFT_NAT_DNAT)))
162 goto nla_put_failure;
163 break;
164 }
165
166 if (nla_put_be32(skb, NFTA_NAT_FAMILY, htonl(priv->family)))
167 goto nla_put_failure;
168 if (nla_put_be32(skb,
169 NFTA_NAT_REG_ADDR_MIN, htonl(priv->sreg_addr_min)))
170 goto nla_put_failure;
171 if (nla_put_be32(skb,
172 NFTA_NAT_REG_ADDR_MAX, htonl(priv->sreg_addr_max)))
173 goto nla_put_failure;
174 if (nla_put_be32(skb,
175 NFTA_NAT_REG_PROTO_MIN, htonl(priv->sreg_proto_min)))
176 goto nla_put_failure;
177 if (nla_put_be32(skb,
178 NFTA_NAT_REG_PROTO_MAX, htonl(priv->sreg_proto_max)))
179 goto nla_put_failure;
180 return 0;
181
182nla_put_failure:
183 return -1;
184}
185
186static struct nft_expr_type nft_nat_type;
187static const struct nft_expr_ops nft_nat_ops = {
188 .type = &nft_nat_type,
189 .size = NFT_EXPR_SIZE(sizeof(struct nft_nat)),
190 .eval = nft_nat_eval,
191 .init = nft_nat_init,
192 .dump = nft_nat_dump,
193};
194
195static struct nft_expr_type nft_nat_type __read_mostly = {
196 .name = "nat",
197 .ops = &nft_nat_ops,
198 .policy = nft_nat_policy,
199 .maxattr = NFTA_NAT_MAX,
200 .owner = THIS_MODULE,
201};
202
203static int __init nft_nat_module_init(void)
204{
205 int err;
206
207 err = nft_register_expr(&nft_nat_type);
208 if (err < 0)
209 return err;
210
211 return 0;
212}
213
214static void __exit nft_nat_module_exit(void)
215{
216 nft_unregister_expr(&nft_nat_type);
217}
218
219module_init(nft_nat_module_init);
220module_exit(nft_nat_module_exit);
221
222MODULE_LICENSE("GPL");
223MODULE_AUTHOR("Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>");
224MODULE_ALIAS_NFT_EXPR("nat");
diff --git a/net/netfilter/nft_payload.c b/net/netfilter/nft_payload.c
new file mode 100644
index 000000000000..a2aeb318678f
--- /dev/null
+++ b/net/netfilter/nft_payload.c
@@ -0,0 +1,160 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter/nf_tables.h>
17#include <net/netfilter/nf_tables_core.h>
18#include <net/netfilter/nf_tables.h>
19
20static void nft_payload_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1],
22 const struct nft_pktinfo *pkt)
23{
24 const struct nft_payload *priv = nft_expr_priv(expr);
25 const struct sk_buff *skb = pkt->skb;
26 struct nft_data *dest = &data[priv->dreg];
27 int offset;
28
29 switch (priv->base) {
30 case NFT_PAYLOAD_LL_HEADER:
31 if (!skb_mac_header_was_set(skb))
32 goto err;
33 offset = skb_mac_header(skb) - skb->data;
34 break;
35 case NFT_PAYLOAD_NETWORK_HEADER:
36 offset = skb_network_offset(skb);
37 break;
38 case NFT_PAYLOAD_TRANSPORT_HEADER:
39 offset = pkt->xt.thoff;
40 break;
41 default:
42 BUG();
43 }
44 offset += priv->offset;
45
46 if (skb_copy_bits(skb, offset, dest->data, priv->len) < 0)
47 goto err;
48 return;
49err:
50 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
51}
52
53static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
54 [NFTA_PAYLOAD_DREG] = { .type = NLA_U32 },
55 [NFTA_PAYLOAD_BASE] = { .type = NLA_U32 },
56 [NFTA_PAYLOAD_OFFSET] = { .type = NLA_U32 },
57 [NFTA_PAYLOAD_LEN] = { .type = NLA_U32 },
58};
59
60static int nft_payload_init(const struct nft_ctx *ctx,
61 const struct nft_expr *expr,
62 const struct nlattr * const tb[])
63{
64 struct nft_payload *priv = nft_expr_priv(expr);
65 int err;
66
67 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
68 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
69 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
70
71 priv->dreg = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_DREG]));
72 err = nft_validate_output_register(priv->dreg);
73 if (err < 0)
74 return err;
75 return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
76}
77
78static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
79{
80 const struct nft_payload *priv = nft_expr_priv(expr);
81
82 if (nla_put_be32(skb, NFTA_PAYLOAD_DREG, htonl(priv->dreg)) ||
83 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
84 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
85 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
86 goto nla_put_failure;
87 return 0;
88
89nla_put_failure:
90 return -1;
91}
92
93static struct nft_expr_type nft_payload_type;
94static const struct nft_expr_ops nft_payload_ops = {
95 .type = &nft_payload_type,
96 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
97 .eval = nft_payload_eval,
98 .init = nft_payload_init,
99 .dump = nft_payload_dump,
100};
101
102const struct nft_expr_ops nft_payload_fast_ops = {
103 .type = &nft_payload_type,
104 .size = NFT_EXPR_SIZE(sizeof(struct nft_payload)),
105 .eval = nft_payload_eval,
106 .init = nft_payload_init,
107 .dump = nft_payload_dump,
108};
109
110static const struct nft_expr_ops *
111nft_payload_select_ops(const struct nft_ctx *ctx,
112 const struct nlattr * const tb[])
113{
114 enum nft_payload_bases base;
115 unsigned int offset, len;
116
117 if (tb[NFTA_PAYLOAD_DREG] == NULL ||
118 tb[NFTA_PAYLOAD_BASE] == NULL ||
119 tb[NFTA_PAYLOAD_OFFSET] == NULL ||
120 tb[NFTA_PAYLOAD_LEN] == NULL)
121 return ERR_PTR(-EINVAL);
122
123 base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
124 switch (base) {
125 case NFT_PAYLOAD_LL_HEADER:
126 case NFT_PAYLOAD_NETWORK_HEADER:
127 case NFT_PAYLOAD_TRANSPORT_HEADER:
128 break;
129 default:
130 return ERR_PTR(-EOPNOTSUPP);
131 }
132
133 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
134 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
135 if (len == 0 || len > FIELD_SIZEOF(struct nft_data, data))
136 return ERR_PTR(-EINVAL);
137
138 if (len <= 4 && IS_ALIGNED(offset, len) && base != NFT_PAYLOAD_LL_HEADER)
139 return &nft_payload_fast_ops;
140 else
141 return &nft_payload_ops;
142}
143
144static struct nft_expr_type nft_payload_type __read_mostly = {
145 .name = "payload",
146 .select_ops = nft_payload_select_ops,
147 .policy = nft_payload_policy,
148 .maxattr = NFTA_PAYLOAD_MAX,
149 .owner = THIS_MODULE,
150};
151
152int __init nft_payload_module_init(void)
153{
154 return nft_register_expr(&nft_payload_type);
155}
156
157void nft_payload_module_exit(void)
158{
159 nft_unregister_expr(&nft_payload_type);
160}
diff --git a/net/netfilter/nft_rbtree.c b/net/netfilter/nft_rbtree.c
new file mode 100644
index 000000000000..ca0c1b231bfe
--- /dev/null
+++ b/net/netfilter/nft_rbtree.c
@@ -0,0 +1,247 @@
1/*
2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11#include <linux/kernel.h>
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/list.h>
15#include <linux/rbtree.h>
16#include <linux/netlink.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables.h>
20
21struct nft_rbtree {
22 struct rb_root root;
23};
24
25struct nft_rbtree_elem {
26 struct rb_node node;
27 u16 flags;
28 struct nft_data key;
29 struct nft_data data[];
30};
31
32static bool nft_rbtree_lookup(const struct nft_set *set,
33 const struct nft_data *key,
34 struct nft_data *data)
35{
36 const struct nft_rbtree *priv = nft_set_priv(set);
37 const struct nft_rbtree_elem *rbe, *interval = NULL;
38 const struct rb_node *parent = priv->root.rb_node;
39 int d;
40
41 while (parent != NULL) {
42 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
43
44 d = nft_data_cmp(&rbe->key, key, set->klen);
45 if (d < 0) {
46 parent = parent->rb_left;
47 interval = rbe;
48 } else if (d > 0)
49 parent = parent->rb_right;
50 else {
51found:
52 if (rbe->flags & NFT_SET_ELEM_INTERVAL_END)
53 goto out;
54 if (set->flags & NFT_SET_MAP)
55 nft_data_copy(data, rbe->data);
56 return true;
57 }
58 }
59
60 if (set->flags & NFT_SET_INTERVAL && interval != NULL) {
61 rbe = interval;
62 goto found;
63 }
64out:
65 return false;
66}
67
68static void nft_rbtree_elem_destroy(const struct nft_set *set,
69 struct nft_rbtree_elem *rbe)
70{
71 nft_data_uninit(&rbe->key, NFT_DATA_VALUE);
72 if (set->flags & NFT_SET_MAP)
73 nft_data_uninit(rbe->data, set->dtype);
74 kfree(rbe);
75}
76
77static int __nft_rbtree_insert(const struct nft_set *set,
78 struct nft_rbtree_elem *new)
79{
80 struct nft_rbtree *priv = nft_set_priv(set);
81 struct nft_rbtree_elem *rbe;
82 struct rb_node *parent, **p;
83 int d;
84
85 parent = NULL;
86 p = &priv->root.rb_node;
87 while (*p != NULL) {
88 parent = *p;
89 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
90 d = nft_data_cmp(&rbe->key, &new->key, set->klen);
91 if (d < 0)
92 p = &parent->rb_left;
93 else if (d > 0)
94 p = &parent->rb_right;
95 else
96 return -EEXIST;
97 }
98 rb_link_node(&new->node, parent, p);
99 rb_insert_color(&new->node, &priv->root);
100 return 0;
101}
102
103static int nft_rbtree_insert(const struct nft_set *set,
104 const struct nft_set_elem *elem)
105{
106 struct nft_rbtree_elem *rbe;
107 unsigned int size;
108 int err;
109
110 size = sizeof(*rbe);
111 if (set->flags & NFT_SET_MAP)
112 size += sizeof(rbe->data[0]);
113
114 rbe = kzalloc(size, GFP_KERNEL);
115 if (rbe == NULL)
116 return -ENOMEM;
117
118 rbe->flags = elem->flags;
119 nft_data_copy(&rbe->key, &elem->key);
120 if (set->flags & NFT_SET_MAP)
121 nft_data_copy(rbe->data, &elem->data);
122
123 err = __nft_rbtree_insert(set, rbe);
124 if (err < 0)
125 kfree(rbe);
126 return err;
127}
128
129static void nft_rbtree_remove(const struct nft_set *set,
130 const struct nft_set_elem *elem)
131{
132 struct nft_rbtree *priv = nft_set_priv(set);
133 struct nft_rbtree_elem *rbe = elem->cookie;
134
135 rb_erase(&rbe->node, &priv->root);
136 kfree(rbe);
137}
138
139static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem)
140{
141 const struct nft_rbtree *priv = nft_set_priv(set);
142 const struct rb_node *parent = priv->root.rb_node;
143 struct nft_rbtree_elem *rbe;
144 int d;
145
146 while (parent != NULL) {
147 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
148
149 d = nft_data_cmp(&rbe->key, &elem->key, set->klen);
150 if (d < 0)
151 parent = parent->rb_left;
152 else if (d > 0)
153 parent = parent->rb_right;
154 else {
155 elem->cookie = rbe;
156 if (set->flags & NFT_SET_MAP)
157 nft_data_copy(&elem->data, rbe->data);
158 elem->flags = rbe->flags;
159 return 0;
160 }
161 }
162 return -ENOENT;
163}
164
165static void nft_rbtree_walk(const struct nft_ctx *ctx,
166 const struct nft_set *set,
167 struct nft_set_iter *iter)
168{
169 const struct nft_rbtree *priv = nft_set_priv(set);
170 const struct nft_rbtree_elem *rbe;
171 struct nft_set_elem elem;
172 struct rb_node *node;
173
174 for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
175 if (iter->count < iter->skip)
176 goto cont;
177
178 rbe = rb_entry(node, struct nft_rbtree_elem, node);
179 nft_data_copy(&elem.key, &rbe->key);
180 if (set->flags & NFT_SET_MAP)
181 nft_data_copy(&elem.data, rbe->data);
182 elem.flags = rbe->flags;
183
184 iter->err = iter->fn(ctx, set, iter, &elem);
185 if (iter->err < 0)
186 return;
187cont:
188 iter->count++;
189 }
190}
191
192static unsigned int nft_rbtree_privsize(const struct nlattr * const nla[])
193{
194 return sizeof(struct nft_rbtree);
195}
196
197static int nft_rbtree_init(const struct nft_set *set,
198 const struct nlattr * const nla[])
199{
200 struct nft_rbtree *priv = nft_set_priv(set);
201
202 priv->root = RB_ROOT;
203 return 0;
204}
205
206static void nft_rbtree_destroy(const struct nft_set *set)
207{
208 struct nft_rbtree *priv = nft_set_priv(set);
209 struct nft_rbtree_elem *rbe;
210 struct rb_node *node;
211
212 while ((node = priv->root.rb_node) != NULL) {
213 rb_erase(node, &priv->root);
214 rbe = rb_entry(node, struct nft_rbtree_elem, node);
215 nft_rbtree_elem_destroy(set, rbe);
216 }
217}
218
219static struct nft_set_ops nft_rbtree_ops __read_mostly = {
220 .privsize = nft_rbtree_privsize,
221 .init = nft_rbtree_init,
222 .destroy = nft_rbtree_destroy,
223 .insert = nft_rbtree_insert,
224 .remove = nft_rbtree_remove,
225 .get = nft_rbtree_get,
226 .lookup = nft_rbtree_lookup,
227 .walk = nft_rbtree_walk,
228 .features = NFT_SET_INTERVAL | NFT_SET_MAP,
229 .owner = THIS_MODULE,
230};
231
232static int __init nft_rbtree_module_init(void)
233{
234 return nft_register_set(&nft_rbtree_ops);
235}
236
237static void __exit nft_rbtree_module_exit(void)
238{
239 nft_unregister_set(&nft_rbtree_ops);
240}
241
242module_init(nft_rbtree_module_init);
243module_exit(nft_rbtree_module_exit);
244
245MODULE_LICENSE("GPL");
246MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
247MODULE_ALIAS_NFT_SET();
diff --git a/net/netfilter/x_tables.c b/net/netfilter/x_tables.c
index 8b03028cca69..227aa11e8409 100644
--- a/net/netfilter/x_tables.c
+++ b/net/netfilter/x_tables.c
@@ -845,8 +845,13 @@ xt_replace_table(struct xt_table *table,
845 return NULL; 845 return NULL;
846 } 846 }
847 847
848 table->private = newinfo;
849 newinfo->initial_entries = private->initial_entries; 848 newinfo->initial_entries = private->initial_entries;
849 /*
850 * Ensure contents of newinfo are visible before assigning to
851 * private.
852 */
853 smp_wmb();
854 table->private = newinfo;
850 855
851 /* 856 /*
852 * Even though table entries have now been swapped, other CPU's 857 * Even though table entries have now been swapped, other CPU's
diff --git a/net/netfilter/xt_NFQUEUE.c b/net/netfilter/xt_NFQUEUE.c
index 1e2fae32f81b..ed00fef58996 100644
--- a/net/netfilter/xt_NFQUEUE.c
+++ b/net/netfilter/xt_NFQUEUE.c
@@ -147,6 +147,7 @@ nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
147{ 147{
148 const struct xt_NFQ_info_v3 *info = par->targinfo; 148 const struct xt_NFQ_info_v3 *info = par->targinfo;
149 u32 queue = info->queuenum; 149 u32 queue = info->queuenum;
150 int ret;
150 151
151 if (info->queues_total > 1) { 152 if (info->queues_total > 1) {
152 if (info->flags & NFQ_FLAG_CPU_FANOUT) { 153 if (info->flags & NFQ_FLAG_CPU_FANOUT) {
@@ -157,7 +158,11 @@ nfqueue_tg_v3(struct sk_buff *skb, const struct xt_action_param *par)
157 queue = nfqueue_hash(skb, par); 158 queue = nfqueue_hash(skb, par);
158 } 159 }
159 160
160 return NF_QUEUE_NR(queue); 161 ret = NF_QUEUE_NR(queue);
162 if (info->flags & NFQ_FLAG_BYPASS)
163 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
164
165 return ret;
161} 166}
162 167
163static struct xt_target nfqueue_tg_reg[] __read_mostly = { 168static struct xt_target nfqueue_tg_reg[] __read_mostly = {
diff --git a/net/netfilter/xt_TCPMSS.c b/net/netfilter/xt_TCPMSS.c
index cd24290f3b2f..e762de5ee89b 100644
--- a/net/netfilter/xt_TCPMSS.c
+++ b/net/netfilter/xt_TCPMSS.c
@@ -43,10 +43,42 @@ optlen(const u_int8_t *opt, unsigned int offset)
43 return opt[offset+1]; 43 return opt[offset+1];
44} 44}
45 45
46static u_int32_t tcpmss_reverse_mtu(struct net *net,
47 const struct sk_buff *skb,
48 unsigned int family)
49{
50 struct flowi fl;
51 const struct nf_afinfo *ai;
52 struct rtable *rt = NULL;
53 u_int32_t mtu = ~0U;
54
55 if (family == PF_INET) {
56 struct flowi4 *fl4 = &fl.u.ip4;
57 memset(fl4, 0, sizeof(*fl4));
58 fl4->daddr = ip_hdr(skb)->saddr;
59 } else {
60 struct flowi6 *fl6 = &fl.u.ip6;
61
62 memset(fl6, 0, sizeof(*fl6));
63 fl6->daddr = ipv6_hdr(skb)->saddr;
64 }
65 rcu_read_lock();
66 ai = nf_get_afinfo(family);
67 if (ai != NULL)
68 ai->route(net, (struct dst_entry **)&rt, &fl, false);
69 rcu_read_unlock();
70
71 if (rt != NULL) {
72 mtu = dst_mtu(&rt->dst);
73 dst_release(&rt->dst);
74 }
75 return mtu;
76}
77
46static int 78static int
47tcpmss_mangle_packet(struct sk_buff *skb, 79tcpmss_mangle_packet(struct sk_buff *skb,
48 const struct xt_action_param *par, 80 const struct xt_action_param *par,
49 unsigned int in_mtu, 81 unsigned int family,
50 unsigned int tcphoff, 82 unsigned int tcphoff,
51 unsigned int minlen) 83 unsigned int minlen)
52{ 84{
@@ -76,6 +108,9 @@ tcpmss_mangle_packet(struct sk_buff *skb,
76 return -1; 108 return -1;
77 109
78 if (info->mss == XT_TCPMSS_CLAMP_PMTU) { 110 if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
111 struct net *net = dev_net(par->in ? par->in : par->out);
112 unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
113
79 if (dst_mtu(skb_dst(skb)) <= minlen) { 114 if (dst_mtu(skb_dst(skb)) <= minlen) {
80 net_err_ratelimited("unknown or invalid path-MTU (%u)\n", 115 net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
81 dst_mtu(skb_dst(skb))); 116 dst_mtu(skb_dst(skb)));
@@ -165,37 +200,6 @@ tcpmss_mangle_packet(struct sk_buff *skb,
165 return TCPOLEN_MSS; 200 return TCPOLEN_MSS;
166} 201}
167 202
168static u_int32_t tcpmss_reverse_mtu(const struct sk_buff *skb,
169 unsigned int family)
170{
171 struct flowi fl;
172 const struct nf_afinfo *ai;
173 struct rtable *rt = NULL;
174 u_int32_t mtu = ~0U;
175
176 if (family == PF_INET) {
177 struct flowi4 *fl4 = &fl.u.ip4;
178 memset(fl4, 0, sizeof(*fl4));
179 fl4->daddr = ip_hdr(skb)->saddr;
180 } else {
181 struct flowi6 *fl6 = &fl.u.ip6;
182
183 memset(fl6, 0, sizeof(*fl6));
184 fl6->daddr = ipv6_hdr(skb)->saddr;
185 }
186 rcu_read_lock();
187 ai = nf_get_afinfo(family);
188 if (ai != NULL)
189 ai->route(&init_net, (struct dst_entry **)&rt, &fl, false);
190 rcu_read_unlock();
191
192 if (rt != NULL) {
193 mtu = dst_mtu(&rt->dst);
194 dst_release(&rt->dst);
195 }
196 return mtu;
197}
198
199static unsigned int 203static unsigned int
200tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par) 204tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par)
201{ 205{
@@ -204,7 +208,7 @@ tcpmss_tg4(struct sk_buff *skb, const struct xt_action_param *par)
204 int ret; 208 int ret;
205 209
206 ret = tcpmss_mangle_packet(skb, par, 210 ret = tcpmss_mangle_packet(skb, par,
207 tcpmss_reverse_mtu(skb, PF_INET), 211 PF_INET,
208 iph->ihl * 4, 212 iph->ihl * 4,
209 sizeof(*iph) + sizeof(struct tcphdr)); 213 sizeof(*iph) + sizeof(struct tcphdr));
210 if (ret < 0) 214 if (ret < 0)
@@ -233,7 +237,7 @@ tcpmss_tg6(struct sk_buff *skb, const struct xt_action_param *par)
233 if (tcphoff < 0) 237 if (tcphoff < 0)
234 return NF_DROP; 238 return NF_DROP;
235 ret = tcpmss_mangle_packet(skb, par, 239 ret = tcpmss_mangle_packet(skb, par,
236 tcpmss_reverse_mtu(skb, PF_INET6), 240 PF_INET6,
237 tcphoff, 241 tcphoff,
238 sizeof(*ipv6h) + sizeof(struct tcphdr)); 242 sizeof(*ipv6h) + sizeof(struct tcphdr));
239 if (ret < 0) 243 if (ret < 0)
diff --git a/net/netfilter/xt_TPROXY.c b/net/netfilter/xt_TPROXY.c
index 5d8a3a3cd5a7..ef8a926752a9 100644
--- a/net/netfilter/xt_TPROXY.c
+++ b/net/netfilter/xt_TPROXY.c
@@ -200,7 +200,7 @@ nf_tproxy_get_sock_v6(struct net *net, const u8 protocol,
200 in->ifindex); 200 in->ifindex);
201 if (sk) { 201 if (sk) {
202 int connected = (sk->sk_state == TCP_ESTABLISHED); 202 int connected = (sk->sk_state == TCP_ESTABLISHED);
203 int wildcard = ipv6_addr_any(&inet6_sk(sk)->rcv_saddr); 203 int wildcard = ipv6_addr_any(&sk->sk_v6_rcv_saddr);
204 204
205 /* NOTE: we return listeners even if bound to 205 /* NOTE: we return listeners even if bound to
206 * 0.0.0.0, those are filtered out in 206 * 0.0.0.0, those are filtered out in
diff --git a/net/netfilter/xt_connbytes.c b/net/netfilter/xt_connbytes.c
index e595e07a759b..1e634615ab9d 100644
--- a/net/netfilter/xt_connbytes.c
+++ b/net/netfilter/xt_connbytes.c
@@ -26,16 +26,18 @@ connbytes_mt(const struct sk_buff *skb, struct xt_action_param *par)
26 u_int64_t what = 0; /* initialize to make gcc happy */ 26 u_int64_t what = 0; /* initialize to make gcc happy */
27 u_int64_t bytes = 0; 27 u_int64_t bytes = 0;
28 u_int64_t pkts = 0; 28 u_int64_t pkts = 0;
29 const struct nf_conn_acct *acct;
29 const struct nf_conn_counter *counters; 30 const struct nf_conn_counter *counters;
30 31
31 ct = nf_ct_get(skb, &ctinfo); 32 ct = nf_ct_get(skb, &ctinfo);
32 if (!ct) 33 if (!ct)
33 return false; 34 return false;
34 35
35 counters = nf_conn_acct_find(ct); 36 acct = nf_conn_acct_find(ct);
36 if (!counters) 37 if (!acct)
37 return false; 38 return false;
38 39
40 counters = acct->counter;
39 switch (sinfo->what) { 41 switch (sinfo->what) {
40 case XT_CONNBYTES_PKTS: 42 case XT_CONNBYTES_PKTS:
41 switch (sinfo->direction) { 43 switch (sinfo->direction) {
diff --git a/net/netfilter/xt_hashlimit.c b/net/netfilter/xt_hashlimit.c
index 9ff035c71403..a3910fc2122b 100644
--- a/net/netfilter/xt_hashlimit.c
+++ b/net/netfilter/xt_hashlimit.c
@@ -325,21 +325,24 @@ static void htable_gc(unsigned long htlong)
325 add_timer(&ht->timer); 325 add_timer(&ht->timer);
326} 326}
327 327
328static void htable_destroy(struct xt_hashlimit_htable *hinfo) 328static void htable_remove_proc_entry(struct xt_hashlimit_htable *hinfo)
329{ 329{
330 struct hashlimit_net *hashlimit_net = hashlimit_pernet(hinfo->net); 330 struct hashlimit_net *hashlimit_net = hashlimit_pernet(hinfo->net);
331 struct proc_dir_entry *parent; 331 struct proc_dir_entry *parent;
332 332
333 del_timer_sync(&hinfo->timer);
334
335 if (hinfo->family == NFPROTO_IPV4) 333 if (hinfo->family == NFPROTO_IPV4)
336 parent = hashlimit_net->ipt_hashlimit; 334 parent = hashlimit_net->ipt_hashlimit;
337 else 335 else
338 parent = hashlimit_net->ip6t_hashlimit; 336 parent = hashlimit_net->ip6t_hashlimit;
339 337
340 if(parent != NULL) 338 if (parent != NULL)
341 remove_proc_entry(hinfo->name, parent); 339 remove_proc_entry(hinfo->name, parent);
340}
342 341
342static void htable_destroy(struct xt_hashlimit_htable *hinfo)
343{
344 del_timer_sync(&hinfo->timer);
345 htable_remove_proc_entry(hinfo);
343 htable_selective_cleanup(hinfo, select_all); 346 htable_selective_cleanup(hinfo, select_all);
344 kfree(hinfo->name); 347 kfree(hinfo->name);
345 vfree(hinfo); 348 vfree(hinfo);
@@ -883,21 +886,15 @@ static int __net_init hashlimit_proc_net_init(struct net *net)
883static void __net_exit hashlimit_proc_net_exit(struct net *net) 886static void __net_exit hashlimit_proc_net_exit(struct net *net)
884{ 887{
885 struct xt_hashlimit_htable *hinfo; 888 struct xt_hashlimit_htable *hinfo;
886 struct proc_dir_entry *pde;
887 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net); 889 struct hashlimit_net *hashlimit_net = hashlimit_pernet(net);
888 890
889 /* recent_net_exit() is called before recent_mt_destroy(). Make sure 891 /* hashlimit_net_exit() is called before hashlimit_mt_destroy().
890 * that the parent xt_recent proc entry is is empty before trying to 892 * Make sure that the parent ipt_hashlimit and ip6t_hashlimit proc
891 * remove it. 893 * entries is empty before trying to remove it.
892 */ 894 */
893 mutex_lock(&hashlimit_mutex); 895 mutex_lock(&hashlimit_mutex);
894 pde = hashlimit_net->ipt_hashlimit;
895 if (pde == NULL)
896 pde = hashlimit_net->ip6t_hashlimit;
897
898 hlist_for_each_entry(hinfo, &hashlimit_net->htables, node) 896 hlist_for_each_entry(hinfo, &hashlimit_net->htables, node)
899 remove_proc_entry(hinfo->name, pde); 897 htable_remove_proc_entry(hinfo);
900
901 hashlimit_net->ipt_hashlimit = NULL; 898 hashlimit_net->ipt_hashlimit = NULL;
902 hashlimit_net->ip6t_hashlimit = NULL; 899 hashlimit_net->ip6t_hashlimit = NULL;
903 mutex_unlock(&hashlimit_mutex); 900 mutex_unlock(&hashlimit_mutex);
diff --git a/net/netfilter/xt_set.c b/net/netfilter/xt_set.c
index 31790e789e22..80c2e2d603e0 100644
--- a/net/netfilter/xt_set.c
+++ b/net/netfilter/xt_set.c
@@ -81,17 +81,17 @@ set_match_v0_checkentry(const struct xt_mtchk_param *par)
81 struct xt_set_info_match_v0 *info = par->matchinfo; 81 struct xt_set_info_match_v0 *info = par->matchinfo;
82 ip_set_id_t index; 82 ip_set_id_t index;
83 83
84 index = ip_set_nfnl_get_byindex(info->match_set.index); 84 index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
85 85
86 if (index == IPSET_INVALID_ID) { 86 if (index == IPSET_INVALID_ID) {
87 pr_warning("Cannot find set indentified by id %u to match\n", 87 pr_warning("Cannot find set identified by id %u to match\n",
88 info->match_set.index); 88 info->match_set.index);
89 return -ENOENT; 89 return -ENOENT;
90 } 90 }
91 if (info->match_set.u.flags[IPSET_DIM_MAX-1] != 0) { 91 if (info->match_set.u.flags[IPSET_DIM_MAX-1] != 0) {
92 pr_warning("Protocol error: set match dimension " 92 pr_warning("Protocol error: set match dimension "
93 "is over the limit!\n"); 93 "is over the limit!\n");
94 ip_set_nfnl_put(info->match_set.index); 94 ip_set_nfnl_put(par->net, info->match_set.index);
95 return -ERANGE; 95 return -ERANGE;
96 } 96 }
97 97
@@ -106,9 +106,104 @@ set_match_v0_destroy(const struct xt_mtdtor_param *par)
106{ 106{
107 struct xt_set_info_match_v0 *info = par->matchinfo; 107 struct xt_set_info_match_v0 *info = par->matchinfo;
108 108
109 ip_set_nfnl_put(info->match_set.index); 109 ip_set_nfnl_put(par->net, info->match_set.index);
110} 110}
111 111
112/* Revision 1 match */
113
114static bool
115set_match_v1(const struct sk_buff *skb, struct xt_action_param *par)
116{
117 const struct xt_set_info_match_v1 *info = par->matchinfo;
118 ADT_OPT(opt, par->family, info->match_set.dim,
119 info->match_set.flags, 0, UINT_MAX);
120
121 if (opt.flags & IPSET_RETURN_NOMATCH)
122 opt.cmdflags |= IPSET_FLAG_RETURN_NOMATCH;
123
124 return match_set(info->match_set.index, skb, par, &opt,
125 info->match_set.flags & IPSET_INV_MATCH);
126}
127
128static int
129set_match_v1_checkentry(const struct xt_mtchk_param *par)
130{
131 struct xt_set_info_match_v1 *info = par->matchinfo;
132 ip_set_id_t index;
133
134 index = ip_set_nfnl_get_byindex(par->net, info->match_set.index);
135
136 if (index == IPSET_INVALID_ID) {
137 pr_warning("Cannot find set identified by id %u to match\n",
138 info->match_set.index);
139 return -ENOENT;
140 }
141 if (info->match_set.dim > IPSET_DIM_MAX) {
142 pr_warning("Protocol error: set match dimension "
143 "is over the limit!\n");
144 ip_set_nfnl_put(par->net, info->match_set.index);
145 return -ERANGE;
146 }
147
148 return 0;
149}
150
151static void
152set_match_v1_destroy(const struct xt_mtdtor_param *par)
153{
154 struct xt_set_info_match_v1 *info = par->matchinfo;
155
156 ip_set_nfnl_put(par->net, info->match_set.index);
157}
158
159/* Revision 3 match */
160
161static bool
162match_counter(u64 counter, const struct ip_set_counter_match *info)
163{
164 switch (info->op) {
165 case IPSET_COUNTER_NONE:
166 return true;
167 case IPSET_COUNTER_EQ:
168 return counter == info->value;
169 case IPSET_COUNTER_NE:
170 return counter != info->value;
171 case IPSET_COUNTER_LT:
172 return counter < info->value;
173 case IPSET_COUNTER_GT:
174 return counter > info->value;
175 }
176 return false;
177}
178
179static bool
180set_match_v3(const struct sk_buff *skb, struct xt_action_param *par)
181{
182 const struct xt_set_info_match_v3 *info = par->matchinfo;
183 ADT_OPT(opt, par->family, info->match_set.dim,
184 info->match_set.flags, info->flags, UINT_MAX);
185 int ret;
186
187 if (info->packets.op != IPSET_COUNTER_NONE ||
188 info->bytes.op != IPSET_COUNTER_NONE)
189 opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS;
190
191 ret = match_set(info->match_set.index, skb, par, &opt,
192 info->match_set.flags & IPSET_INV_MATCH);
193
194 if (!(ret && opt.cmdflags & IPSET_FLAG_MATCH_COUNTERS))
195 return ret;
196
197 if (!match_counter(opt.ext.packets, &info->packets))
198 return 0;
199 return match_counter(opt.ext.bytes, &info->bytes);
200}
201
202#define set_match_v3_checkentry set_match_v1_checkentry
203#define set_match_v3_destroy set_match_v1_destroy
204
205/* Revision 0 interface: backward compatible with netfilter/iptables */
206
112static unsigned int 207static unsigned int
113set_target_v0(struct sk_buff *skb, const struct xt_action_param *par) 208set_target_v0(struct sk_buff *skb, const struct xt_action_param *par)
114{ 209{
@@ -133,7 +228,7 @@ set_target_v0_checkentry(const struct xt_tgchk_param *par)
133 ip_set_id_t index; 228 ip_set_id_t index;
134 229
135 if (info->add_set.index != IPSET_INVALID_ID) { 230 if (info->add_set.index != IPSET_INVALID_ID) {
136 index = ip_set_nfnl_get_byindex(info->add_set.index); 231 index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
137 if (index == IPSET_INVALID_ID) { 232 if (index == IPSET_INVALID_ID) {
138 pr_warning("Cannot find add_set index %u as target\n", 233 pr_warning("Cannot find add_set index %u as target\n",
139 info->add_set.index); 234 info->add_set.index);
@@ -142,12 +237,12 @@ set_target_v0_checkentry(const struct xt_tgchk_param *par)
142 } 237 }
143 238
144 if (info->del_set.index != IPSET_INVALID_ID) { 239 if (info->del_set.index != IPSET_INVALID_ID) {
145 index = ip_set_nfnl_get_byindex(info->del_set.index); 240 index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
146 if (index == IPSET_INVALID_ID) { 241 if (index == IPSET_INVALID_ID) {
147 pr_warning("Cannot find del_set index %u as target\n", 242 pr_warning("Cannot find del_set index %u as target\n",
148 info->del_set.index); 243 info->del_set.index);
149 if (info->add_set.index != IPSET_INVALID_ID) 244 if (info->add_set.index != IPSET_INVALID_ID)
150 ip_set_nfnl_put(info->add_set.index); 245 ip_set_nfnl_put(par->net, info->add_set.index);
151 return -ENOENT; 246 return -ENOENT;
152 } 247 }
153 } 248 }
@@ -156,9 +251,9 @@ set_target_v0_checkentry(const struct xt_tgchk_param *par)
156 pr_warning("Protocol error: SET target dimension " 251 pr_warning("Protocol error: SET target dimension "
157 "is over the limit!\n"); 252 "is over the limit!\n");
158 if (info->add_set.index != IPSET_INVALID_ID) 253 if (info->add_set.index != IPSET_INVALID_ID)
159 ip_set_nfnl_put(info->add_set.index); 254 ip_set_nfnl_put(par->net, info->add_set.index);
160 if (info->del_set.index != IPSET_INVALID_ID) 255 if (info->del_set.index != IPSET_INVALID_ID)
161 ip_set_nfnl_put(info->del_set.index); 256 ip_set_nfnl_put(par->net, info->del_set.index);
162 return -ERANGE; 257 return -ERANGE;
163 } 258 }
164 259
@@ -175,57 +270,12 @@ set_target_v0_destroy(const struct xt_tgdtor_param *par)
175 const struct xt_set_info_target_v0 *info = par->targinfo; 270 const struct xt_set_info_target_v0 *info = par->targinfo;
176 271
177 if (info->add_set.index != IPSET_INVALID_ID) 272 if (info->add_set.index != IPSET_INVALID_ID)
178 ip_set_nfnl_put(info->add_set.index); 273 ip_set_nfnl_put(par->net, info->add_set.index);
179 if (info->del_set.index != IPSET_INVALID_ID) 274 if (info->del_set.index != IPSET_INVALID_ID)
180 ip_set_nfnl_put(info->del_set.index); 275 ip_set_nfnl_put(par->net, info->del_set.index);
181}
182
183/* Revision 1 match and target */
184
185static bool
186set_match_v1(const struct sk_buff *skb, struct xt_action_param *par)
187{
188 const struct xt_set_info_match_v1 *info = par->matchinfo;
189 ADT_OPT(opt, par->family, info->match_set.dim,
190 info->match_set.flags, 0, UINT_MAX);
191
192 if (opt.flags & IPSET_RETURN_NOMATCH)
193 opt.cmdflags |= IPSET_FLAG_RETURN_NOMATCH;
194
195 return match_set(info->match_set.index, skb, par, &opt,
196 info->match_set.flags & IPSET_INV_MATCH);
197}
198
199static int
200set_match_v1_checkentry(const struct xt_mtchk_param *par)
201{
202 struct xt_set_info_match_v1 *info = par->matchinfo;
203 ip_set_id_t index;
204
205 index = ip_set_nfnl_get_byindex(info->match_set.index);
206
207 if (index == IPSET_INVALID_ID) {
208 pr_warning("Cannot find set indentified by id %u to match\n",
209 info->match_set.index);
210 return -ENOENT;
211 }
212 if (info->match_set.dim > IPSET_DIM_MAX) {
213 pr_warning("Protocol error: set match dimension "
214 "is over the limit!\n");
215 ip_set_nfnl_put(info->match_set.index);
216 return -ERANGE;
217 }
218
219 return 0;
220} 276}
221 277
222static void 278/* Revision 1 target */
223set_match_v1_destroy(const struct xt_mtdtor_param *par)
224{
225 struct xt_set_info_match_v1 *info = par->matchinfo;
226
227 ip_set_nfnl_put(info->match_set.index);
228}
229 279
230static unsigned int 280static unsigned int
231set_target_v1(struct sk_buff *skb, const struct xt_action_param *par) 281set_target_v1(struct sk_buff *skb, const struct xt_action_param *par)
@@ -251,7 +301,7 @@ set_target_v1_checkentry(const struct xt_tgchk_param *par)
251 ip_set_id_t index; 301 ip_set_id_t index;
252 302
253 if (info->add_set.index != IPSET_INVALID_ID) { 303 if (info->add_set.index != IPSET_INVALID_ID) {
254 index = ip_set_nfnl_get_byindex(info->add_set.index); 304 index = ip_set_nfnl_get_byindex(par->net, info->add_set.index);
255 if (index == IPSET_INVALID_ID) { 305 if (index == IPSET_INVALID_ID) {
256 pr_warning("Cannot find add_set index %u as target\n", 306 pr_warning("Cannot find add_set index %u as target\n",
257 info->add_set.index); 307 info->add_set.index);
@@ -260,12 +310,12 @@ set_target_v1_checkentry(const struct xt_tgchk_param *par)
260 } 310 }
261 311
262 if (info->del_set.index != IPSET_INVALID_ID) { 312 if (info->del_set.index != IPSET_INVALID_ID) {
263 index = ip_set_nfnl_get_byindex(info->del_set.index); 313 index = ip_set_nfnl_get_byindex(par->net, info->del_set.index);
264 if (index == IPSET_INVALID_ID) { 314 if (index == IPSET_INVALID_ID) {
265 pr_warning("Cannot find del_set index %u as target\n", 315 pr_warning("Cannot find del_set index %u as target\n",
266 info->del_set.index); 316 info->del_set.index);
267 if (info->add_set.index != IPSET_INVALID_ID) 317 if (info->add_set.index != IPSET_INVALID_ID)
268 ip_set_nfnl_put(info->add_set.index); 318 ip_set_nfnl_put(par->net, info->add_set.index);
269 return -ENOENT; 319 return -ENOENT;
270 } 320 }
271 } 321 }
@@ -274,9 +324,9 @@ set_target_v1_checkentry(const struct xt_tgchk_param *par)
274 pr_warning("Protocol error: SET target dimension " 324 pr_warning("Protocol error: SET target dimension "
275 "is over the limit!\n"); 325 "is over the limit!\n");
276 if (info->add_set.index != IPSET_INVALID_ID) 326 if (info->add_set.index != IPSET_INVALID_ID)
277 ip_set_nfnl_put(info->add_set.index); 327 ip_set_nfnl_put(par->net, info->add_set.index);
278 if (info->del_set.index != IPSET_INVALID_ID) 328 if (info->del_set.index != IPSET_INVALID_ID)
279 ip_set_nfnl_put(info->del_set.index); 329 ip_set_nfnl_put(par->net, info->del_set.index);
280 return -ERANGE; 330 return -ERANGE;
281 } 331 }
282 332
@@ -289,9 +339,9 @@ set_target_v1_destroy(const struct xt_tgdtor_param *par)
289 const struct xt_set_info_target_v1 *info = par->targinfo; 339 const struct xt_set_info_target_v1 *info = par->targinfo;
290 340
291 if (info->add_set.index != IPSET_INVALID_ID) 341 if (info->add_set.index != IPSET_INVALID_ID)
292 ip_set_nfnl_put(info->add_set.index); 342 ip_set_nfnl_put(par->net, info->add_set.index);
293 if (info->del_set.index != IPSET_INVALID_ID) 343 if (info->del_set.index != IPSET_INVALID_ID)
294 ip_set_nfnl_put(info->del_set.index); 344 ip_set_nfnl_put(par->net, info->del_set.index);
295} 345}
296 346
297/* Revision 2 target */ 347/* Revision 2 target */
@@ -320,52 +370,6 @@ set_target_v2(struct sk_buff *skb, const struct xt_action_param *par)
320#define set_target_v2_checkentry set_target_v1_checkentry 370#define set_target_v2_checkentry set_target_v1_checkentry
321#define set_target_v2_destroy set_target_v1_destroy 371#define set_target_v2_destroy set_target_v1_destroy
322 372
323/* Revision 3 match */
324
325static bool
326match_counter(u64 counter, const struct ip_set_counter_match *info)
327{
328 switch (info->op) {
329 case IPSET_COUNTER_NONE:
330 return true;
331 case IPSET_COUNTER_EQ:
332 return counter == info->value;
333 case IPSET_COUNTER_NE:
334 return counter != info->value;
335 case IPSET_COUNTER_LT:
336 return counter < info->value;
337 case IPSET_COUNTER_GT:
338 return counter > info->value;
339 }
340 return false;
341}
342
343static bool
344set_match_v3(const struct sk_buff *skb, struct xt_action_param *par)
345{
346 const struct xt_set_info_match_v3 *info = par->matchinfo;
347 ADT_OPT(opt, par->family, info->match_set.dim,
348 info->match_set.flags, info->flags, UINT_MAX);
349 int ret;
350
351 if (info->packets.op != IPSET_COUNTER_NONE ||
352 info->bytes.op != IPSET_COUNTER_NONE)
353 opt.cmdflags |= IPSET_FLAG_MATCH_COUNTERS;
354
355 ret = match_set(info->match_set.index, skb, par, &opt,
356 info->match_set.flags & IPSET_INV_MATCH);
357
358 if (!(ret && opt.cmdflags & IPSET_FLAG_MATCH_COUNTERS))
359 return ret;
360
361 if (!match_counter(opt.ext.packets, &info->packets))
362 return 0;
363 return match_counter(opt.ext.bytes, &info->bytes);
364}
365
366#define set_match_v3_checkentry set_match_v1_checkentry
367#define set_match_v3_destroy set_match_v1_destroy
368
369static struct xt_match set_matches[] __read_mostly = { 373static struct xt_match set_matches[] __read_mostly = {
370 { 374 {
371 .name = "set", 375 .name = "set",
diff --git a/net/netfilter/xt_socket.c b/net/netfilter/xt_socket.c
index 06df2b9110f5..1ba67931eb1b 100644
--- a/net/netfilter/xt_socket.c
+++ b/net/netfilter/xt_socket.c
@@ -35,15 +35,6 @@
35#include <net/netfilter/nf_conntrack.h> 35#include <net/netfilter/nf_conntrack.h>
36#endif 36#endif
37 37
38static void
39xt_socket_put_sk(struct sock *sk)
40{
41 if (sk->sk_state == TCP_TIME_WAIT)
42 inet_twsk_put(inet_twsk(sk));
43 else
44 sock_put(sk);
45}
46
47static int 38static int
48extract_icmp4_fields(const struct sk_buff *skb, 39extract_icmp4_fields(const struct sk_buff *skb,
49 u8 *protocol, 40 u8 *protocol,
@@ -216,7 +207,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
216 inet_twsk(sk)->tw_transparent)); 207 inet_twsk(sk)->tw_transparent));
217 208
218 if (sk != skb->sk) 209 if (sk != skb->sk)
219 xt_socket_put_sk(sk); 210 sock_gen_put(sk);
220 211
221 if (wildcard || !transparent) 212 if (wildcard || !transparent)
222 sk = NULL; 213 sk = NULL;
@@ -370,7 +361,7 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
370 */ 361 */
371 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) && 362 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
372 sk->sk_state != TCP_TIME_WAIT && 363 sk->sk_state != TCP_TIME_WAIT &&
373 ipv6_addr_any(&inet6_sk(sk)->rcv_saddr)); 364 ipv6_addr_any(&sk->sk_v6_rcv_saddr));
374 365
375 /* Ignore non-transparent sockets, 366 /* Ignore non-transparent sockets,
376 if XT_SOCKET_TRANSPARENT is used */ 367 if XT_SOCKET_TRANSPARENT is used */
@@ -381,7 +372,7 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
381 inet_twsk(sk)->tw_transparent)); 372 inet_twsk(sk)->tw_transparent));
382 373
383 if (sk != skb->sk) 374 if (sk != skb->sk)
384 xt_socket_put_sk(sk); 375 sock_gen_put(sk);
385 376
386 if (wildcard || !transparent) 377 if (wildcard || !transparent)
387 sk = NULL; 378 sk = NULL;
diff --git a/net/netlabel/netlabel_cipso_v4.c b/net/netlabel/netlabel_cipso_v4.c
index a1100640495d..69345cebe3a3 100644
--- a/net/netlabel/netlabel_cipso_v4.c
+++ b/net/netlabel/netlabel_cipso_v4.c
@@ -737,7 +737,7 @@ static int netlbl_cipsov4_remove(struct sk_buff *skb, struct genl_info *info)
737 * NetLabel Generic NETLINK Command Definitions 737 * NetLabel Generic NETLINK Command Definitions
738 */ 738 */
739 739
740static struct genl_ops netlbl_cipsov4_ops[] = { 740static const struct genl_ops netlbl_cipsov4_ops[] = {
741 { 741 {
742 .cmd = NLBL_CIPSOV4_C_ADD, 742 .cmd = NLBL_CIPSOV4_C_ADD,
743 .flags = GENL_ADMIN_PERM, 743 .flags = GENL_ADMIN_PERM,
@@ -783,5 +783,5 @@ static struct genl_ops netlbl_cipsov4_ops[] = {
783int __init netlbl_cipsov4_genl_init(void) 783int __init netlbl_cipsov4_genl_init(void)
784{ 784{
785 return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family, 785 return genl_register_family_with_ops(&netlbl_cipsov4_gnl_family,
786 netlbl_cipsov4_ops, ARRAY_SIZE(netlbl_cipsov4_ops)); 786 netlbl_cipsov4_ops);
787} 787}
diff --git a/net/netlabel/netlabel_kapi.c b/net/netlabel/netlabel_kapi.c
index 96a458e12f60..dce1bebf7aec 100644
--- a/net/netlabel/netlabel_kapi.c
+++ b/net/netlabel/netlabel_kapi.c
@@ -817,7 +817,7 @@ int netlbl_req_setattr(struct request_sock *req,
817 switch (req->rsk_ops->family) { 817 switch (req->rsk_ops->family) {
818 case AF_INET: 818 case AF_INET:
819 entry = netlbl_domhsh_getentry_af4(secattr->domain, 819 entry = netlbl_domhsh_getentry_af4(secattr->domain,
820 inet_rsk(req)->rmt_addr); 820 inet_rsk(req)->ir_rmt_addr);
821 if (entry == NULL) { 821 if (entry == NULL) {
822 ret_val = -ENOENT; 822 ret_val = -ENOENT;
823 goto req_setattr_return; 823 goto req_setattr_return;
diff --git a/net/netlabel/netlabel_mgmt.c b/net/netlabel/netlabel_mgmt.c
index dd1c37d7acbc..8ef83ee97c6a 100644
--- a/net/netlabel/netlabel_mgmt.c
+++ b/net/netlabel/netlabel_mgmt.c
@@ -705,7 +705,7 @@ version_failure:
705 * NetLabel Generic NETLINK Command Definitions 705 * NetLabel Generic NETLINK Command Definitions
706 */ 706 */
707 707
708static struct genl_ops netlbl_mgmt_genl_ops[] = { 708static const struct genl_ops netlbl_mgmt_genl_ops[] = {
709 { 709 {
710 .cmd = NLBL_MGMT_C_ADD, 710 .cmd = NLBL_MGMT_C_ADD,
711 .flags = GENL_ADMIN_PERM, 711 .flags = GENL_ADMIN_PERM,
@@ -779,5 +779,5 @@ static struct genl_ops netlbl_mgmt_genl_ops[] = {
779int __init netlbl_mgmt_genl_init(void) 779int __init netlbl_mgmt_genl_init(void)
780{ 780{
781 return genl_register_family_with_ops(&netlbl_mgmt_gnl_family, 781 return genl_register_family_with_ops(&netlbl_mgmt_gnl_family,
782 netlbl_mgmt_genl_ops, ARRAY_SIZE(netlbl_mgmt_genl_ops)); 782 netlbl_mgmt_genl_ops);
783} 783}
diff --git a/net/netlabel/netlabel_unlabeled.c b/net/netlabel/netlabel_unlabeled.c
index 8f0897407a2c..43817d73ccf9 100644
--- a/net/netlabel/netlabel_unlabeled.c
+++ b/net/netlabel/netlabel_unlabeled.c
@@ -1323,7 +1323,7 @@ unlabel_staticlistdef_return:
1323 * NetLabel Generic NETLINK Command Definitions 1323 * NetLabel Generic NETLINK Command Definitions
1324 */ 1324 */
1325 1325
1326static struct genl_ops netlbl_unlabel_genl_ops[] = { 1326static const struct genl_ops netlbl_unlabel_genl_ops[] = {
1327 { 1327 {
1328 .cmd = NLBL_UNLABEL_C_STATICADD, 1328 .cmd = NLBL_UNLABEL_C_STATICADD,
1329 .flags = GENL_ADMIN_PERM, 1329 .flags = GENL_ADMIN_PERM,
@@ -1397,7 +1397,7 @@ static struct genl_ops netlbl_unlabel_genl_ops[] = {
1397int __init netlbl_unlabel_genl_init(void) 1397int __init netlbl_unlabel_genl_init(void)
1398{ 1398{
1399 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family, 1399 return genl_register_family_with_ops(&netlbl_unlabel_gnl_family,
1400 netlbl_unlabel_genl_ops, ARRAY_SIZE(netlbl_unlabel_genl_ops)); 1400 netlbl_unlabel_genl_ops);
1401} 1401}
1402 1402
1403/* 1403/*
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 8df7f64c6db3..bca50b95c182 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -2017,7 +2017,7 @@ out:
2017 * netlink_set_err - report error to broadcast listeners 2017 * netlink_set_err - report error to broadcast listeners
2018 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() 2018 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
2019 * @portid: the PORTID of a process that we want to skip (if any) 2019 * @portid: the PORTID of a process that we want to skip (if any)
2020 * @groups: the broadcast group that will notice the error 2020 * @group: the broadcast group that will notice the error
2021 * @code: error code, must be negative (as usual in kernelspace) 2021 * @code: error code, must be negative (as usual in kernelspace)
2022 * 2022 *
2023 * This function returns the number of broadcast listeners that have set the 2023 * This function returns the number of broadcast listeners that have set the
@@ -2335,8 +2335,6 @@ static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2335 } 2335 }
2336#endif 2336#endif
2337 2337
2338 msg->msg_namelen = 0;
2339
2340 copied = data_skb->len; 2338 copied = data_skb->len;
2341 if (len < copied) { 2339 if (len < copied) {
2342 msg->msg_flags |= MSG_TRUNC; 2340 msg->msg_flags |= MSG_TRUNC;
diff --git a/net/netlink/genetlink.c b/net/netlink/genetlink.c
index 0c741cec4d0d..713671ae45af 100644
--- a/net/netlink/genetlink.c
+++ b/net/netlink/genetlink.c
@@ -65,12 +65,27 @@ static struct list_head family_ht[GENL_FAM_TAB_SIZE];
65 * To avoid an allocation at boot of just one unsigned long, 65 * To avoid an allocation at boot of just one unsigned long,
66 * declare it global instead. 66 * declare it global instead.
67 * Bit 0 is marked as already used since group 0 is invalid. 67 * Bit 0 is marked as already used since group 0 is invalid.
68 * Bit 1 is marked as already used since the drop-monitor code
69 * abuses the API and thinks it can statically use group 1.
70 * That group will typically conflict with other groups that
71 * any proper users use.
72 * Bit 16 is marked as used since it's used for generic netlink
73 * and the code no longer marks pre-reserved IDs as used.
74 * Bit 17 is marked as already used since the VFS quota code
75 * also abused this API and relied on family == group ID, we
76 * cater to that by giving it a static family and group ID.
77 * Bit 18 is marked as already used since the PMCRAID driver
78 * did the same thing as the VFS quota code (maybe copied?)
68 */ 79 */
69static unsigned long mc_group_start = 0x1; 80static unsigned long mc_group_start = 0x3 | BIT(GENL_ID_CTRL) |
81 BIT(GENL_ID_VFS_DQUOT) |
82 BIT(GENL_ID_PMCRAID);
70static unsigned long *mc_groups = &mc_group_start; 83static unsigned long *mc_groups = &mc_group_start;
71static unsigned long mc_groups_longs = 1; 84static unsigned long mc_groups_longs = 1;
72 85
73static int genl_ctrl_event(int event, void *data); 86static int genl_ctrl_event(int event, struct genl_family *family,
87 const struct genl_multicast_group *grp,
88 int grp_id);
74 89
75static inline unsigned int genl_family_hash(unsigned int id) 90static inline unsigned int genl_family_hash(unsigned int id)
76{ 91{
@@ -106,13 +121,13 @@ static struct genl_family *genl_family_find_byname(char *name)
106 return NULL; 121 return NULL;
107} 122}
108 123
109static struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family) 124static const struct genl_ops *genl_get_cmd(u8 cmd, struct genl_family *family)
110{ 125{
111 struct genl_ops *ops; 126 int i;
112 127
113 list_for_each_entry(ops, &family->ops_list, ops_list) 128 for (i = 0; i < family->n_ops; i++)
114 if (ops->cmd == cmd) 129 if (family->ops[i].cmd == cmd)
115 return ops; 130 return &family->ops[i];
116 131
117 return NULL; 132 return NULL;
118} 133}
@@ -126,7 +141,9 @@ static u16 genl_generate_id(void)
126 int i; 141 int i;
127 142
128 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) { 143 for (i = 0; i <= GENL_MAX_ID - GENL_MIN_ID; i++) {
129 if (!genl_family_find_byid(id_gen_idx)) 144 if (id_gen_idx != GENL_ID_VFS_DQUOT &&
145 id_gen_idx != GENL_ID_PMCRAID &&
146 !genl_family_find_byid(id_gen_idx))
130 return id_gen_idx; 147 return id_gen_idx;
131 if (++id_gen_idx > GENL_MAX_ID) 148 if (++id_gen_idx > GENL_MAX_ID)
132 id_gen_idx = GENL_MIN_ID; 149 id_gen_idx = GENL_MIN_ID;
@@ -135,62 +152,113 @@ static u16 genl_generate_id(void)
135 return 0; 152 return 0;
136} 153}
137 154
138static struct genl_multicast_group notify_grp; 155static int genl_allocate_reserve_groups(int n_groups, int *first_id)
139
140/**
141 * genl_register_mc_group - register a multicast group
142 *
143 * Registers the specified multicast group and notifies userspace
144 * about the new group.
145 *
146 * Returns 0 on success or a negative error code.
147 *
148 * @family: The generic netlink family the group shall be registered for.
149 * @grp: The group to register, must have a name.
150 */
151int genl_register_mc_group(struct genl_family *family,
152 struct genl_multicast_group *grp)
153{ 156{
154 int id;
155 unsigned long *new_groups; 157 unsigned long *new_groups;
156 int err = 0; 158 int start = 0;
159 int i;
160 int id;
161 bool fits;
162
163 do {
164 if (start == 0)
165 id = find_first_zero_bit(mc_groups,
166 mc_groups_longs *
167 BITS_PER_LONG);
168 else
169 id = find_next_zero_bit(mc_groups,
170 mc_groups_longs * BITS_PER_LONG,
171 start);
172
173 fits = true;
174 for (i = id;
175 i < min_t(int, id + n_groups,
176 mc_groups_longs * BITS_PER_LONG);
177 i++) {
178 if (test_bit(i, mc_groups)) {
179 start = i;
180 fits = false;
181 break;
182 }
183 }
157 184
158 BUG_ON(grp->name[0] == '\0'); 185 if (id >= mc_groups_longs * BITS_PER_LONG) {
159 BUG_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL); 186 unsigned long new_longs = mc_groups_longs +
187 BITS_TO_LONGS(n_groups);
188 size_t nlen = new_longs * sizeof(unsigned long);
189
190 if (mc_groups == &mc_group_start) {
191 new_groups = kzalloc(nlen, GFP_KERNEL);
192 if (!new_groups)
193 return -ENOMEM;
194 mc_groups = new_groups;
195 *mc_groups = mc_group_start;
196 } else {
197 new_groups = krealloc(mc_groups, nlen,
198 GFP_KERNEL);
199 if (!new_groups)
200 return -ENOMEM;
201 mc_groups = new_groups;
202 for (i = 0; i < BITS_TO_LONGS(n_groups); i++)
203 mc_groups[mc_groups_longs + i] = 0;
204 }
205 mc_groups_longs = new_longs;
206 }
207 } while (!fits);
160 208
161 genl_lock_all(); 209 for (i = id; i < id + n_groups; i++)
210 set_bit(i, mc_groups);
211 *first_id = id;
212 return 0;
213}
162 214
163 /* special-case our own group */ 215static struct genl_family genl_ctrl;
164 if (grp == &notify_grp)
165 id = GENL_ID_CTRL;
166 else
167 id = find_first_zero_bit(mc_groups,
168 mc_groups_longs * BITS_PER_LONG);
169 216
217static int genl_validate_assign_mc_groups(struct genl_family *family)
218{
219 int first_id;
220 int n_groups = family->n_mcgrps;
221 int err = 0, i;
222 bool groups_allocated = false;
170 223
171 if (id >= mc_groups_longs * BITS_PER_LONG) { 224 if (!n_groups)
172 size_t nlen = (mc_groups_longs + 1) * sizeof(unsigned long); 225 return 0;
173 226
174 if (mc_groups == &mc_group_start) { 227 for (i = 0; i < n_groups; i++) {
175 new_groups = kzalloc(nlen, GFP_KERNEL); 228 const struct genl_multicast_group *grp = &family->mcgrps[i];
176 if (!new_groups) { 229
177 err = -ENOMEM; 230 if (WARN_ON(grp->name[0] == '\0'))
178 goto out; 231 return -EINVAL;
179 } 232 if (WARN_ON(memchr(grp->name, '\0', GENL_NAMSIZ) == NULL))
180 mc_groups = new_groups; 233 return -EINVAL;
181 *mc_groups = mc_group_start; 234 }
182 } else { 235
183 new_groups = krealloc(mc_groups, nlen, GFP_KERNEL); 236 /* special-case our own group and hacks */
184 if (!new_groups) { 237 if (family == &genl_ctrl) {
185 err = -ENOMEM; 238 first_id = GENL_ID_CTRL;
186 goto out; 239 BUG_ON(n_groups != 1);
187 } 240 } else if (strcmp(family->name, "NET_DM") == 0) {
188 mc_groups = new_groups; 241 first_id = 1;
189 mc_groups[mc_groups_longs] = 0; 242 BUG_ON(n_groups != 1);
190 } 243 } else if (family->id == GENL_ID_VFS_DQUOT) {
191 mc_groups_longs++; 244 first_id = GENL_ID_VFS_DQUOT;
245 BUG_ON(n_groups != 1);
246 } else if (family->id == GENL_ID_PMCRAID) {
247 first_id = GENL_ID_PMCRAID;
248 BUG_ON(n_groups != 1);
249 } else {
250 groups_allocated = true;
251 err = genl_allocate_reserve_groups(n_groups, &first_id);
252 if (err)
253 return err;
192 } 254 }
193 255
256 family->mcgrp_offset = first_id;
257
258 /* if still initializing, can't and don't need to to realloc bitmaps */
259 if (!init_net.genl_sock)
260 return 0;
261
194 if (family->netnsok) { 262 if (family->netnsok) {
195 struct net *net; 263 struct net *net;
196 264
@@ -206,9 +274,7 @@ int genl_register_mc_group(struct genl_family *family,
206 * number of _possible_ groups has been 274 * number of _possible_ groups has been
207 * increased on some sockets which is ok. 275 * increased on some sockets which is ok.
208 */ 276 */
209 rcu_read_unlock(); 277 break;
210 netlink_table_ungrab();
211 goto out;
212 } 278 }
213 } 279 }
214 rcu_read_unlock(); 280 rcu_read_unlock();
@@ -216,152 +282,67 @@ int genl_register_mc_group(struct genl_family *family,
216 } else { 282 } else {
217 err = netlink_change_ngroups(init_net.genl_sock, 283 err = netlink_change_ngroups(init_net.genl_sock,
218 mc_groups_longs * BITS_PER_LONG); 284 mc_groups_longs * BITS_PER_LONG);
219 if (err)
220 goto out;
221 } 285 }
222 286
223 grp->id = id; 287 if (groups_allocated && err) {
224 set_bit(id, mc_groups); 288 for (i = 0; i < family->n_mcgrps; i++)
225 list_add_tail(&grp->list, &family->mcast_groups); 289 clear_bit(family->mcgrp_offset + i, mc_groups);
226 grp->family = family; 290 }
227 291
228 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, grp);
229 out:
230 genl_unlock_all();
231 return err; 292 return err;
232} 293}
233EXPORT_SYMBOL(genl_register_mc_group);
234 294
235static void __genl_unregister_mc_group(struct genl_family *family, 295static void genl_unregister_mc_groups(struct genl_family *family)
236 struct genl_multicast_group *grp)
237{ 296{
238 struct net *net; 297 struct net *net;
239 BUG_ON(grp->family != family); 298 int i;
240 299
241 netlink_table_grab(); 300 netlink_table_grab();
242 rcu_read_lock(); 301 rcu_read_lock();
243 for_each_net_rcu(net) 302 for_each_net_rcu(net) {
244 __netlink_clear_multicast_users(net->genl_sock, grp->id); 303 for (i = 0; i < family->n_mcgrps; i++)
304 __netlink_clear_multicast_users(
305 net->genl_sock, family->mcgrp_offset + i);
306 }
245 rcu_read_unlock(); 307 rcu_read_unlock();
246 netlink_table_ungrab(); 308 netlink_table_ungrab();
247 309
248 clear_bit(grp->id, mc_groups); 310 for (i = 0; i < family->n_mcgrps; i++) {
249 list_del(&grp->list); 311 int grp_id = family->mcgrp_offset + i;
250 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, grp);
251 grp->id = 0;
252 grp->family = NULL;
253}
254 312
255/** 313 if (grp_id != 1)
256 * genl_unregister_mc_group - unregister a multicast group 314 clear_bit(grp_id, mc_groups);
257 * 315 genl_ctrl_event(CTRL_CMD_DELMCAST_GRP, family,
258 * Unregisters the specified multicast group and notifies userspace 316 &family->mcgrps[i], grp_id);
259 * about it. All current listeners on the group are removed. 317 }
260 *
261 * Note: It is not necessary to unregister all multicast groups before
262 * unregistering the family, unregistering the family will cause
263 * all assigned multicast groups to be unregistered automatically.
264 *
265 * @family: Generic netlink family the group belongs to.
266 * @grp: The group to unregister, must have been registered successfully
267 * previously.
268 */
269void genl_unregister_mc_group(struct genl_family *family,
270 struct genl_multicast_group *grp)
271{
272 genl_lock_all();
273 __genl_unregister_mc_group(family, grp);
274 genl_unlock_all();
275} 318}
276EXPORT_SYMBOL(genl_unregister_mc_group);
277 319
278static void genl_unregister_mc_groups(struct genl_family *family) 320static int genl_validate_ops(struct genl_family *family)
279{ 321{
280 struct genl_multicast_group *grp, *tmp; 322 const struct genl_ops *ops = family->ops;
323 unsigned int n_ops = family->n_ops;
324 int i, j;
281 325
282 list_for_each_entry_safe(grp, tmp, &family->mcast_groups, list) 326 if (WARN_ON(n_ops && !ops))
283 __genl_unregister_mc_group(family, grp); 327 return -EINVAL;
284}
285
286/**
287 * genl_register_ops - register generic netlink operations
288 * @family: generic netlink family
289 * @ops: operations to be registered
290 *
291 * Registers the specified operations and assigns them to the specified
292 * family. Either a doit or dumpit callback must be specified or the
293 * operation will fail. Only one operation structure per command
294 * identifier may be registered.
295 *
296 * See include/net/genetlink.h for more documenation on the operations
297 * structure.
298 *
299 * Returns 0 on success or a negative error code.
300 */
301int genl_register_ops(struct genl_family *family, struct genl_ops *ops)
302{
303 int err = -EINVAL;
304 328
305 if (ops->dumpit == NULL && ops->doit == NULL) 329 if (!n_ops)
306 goto errout; 330 return 0;
307 331
308 if (genl_get_cmd(ops->cmd, family)) { 332 for (i = 0; i < n_ops; i++) {
309 err = -EEXIST; 333 if (ops[i].dumpit == NULL && ops[i].doit == NULL)
310 goto errout; 334 return -EINVAL;
335 for (j = i + 1; j < n_ops; j++)
336 if (ops[i].cmd == ops[j].cmd)
337 return -EINVAL;
311 } 338 }
312 339
313 if (ops->dumpit) 340 /* family is not registered yet, so no locking needed */
314 ops->flags |= GENL_CMD_CAP_DUMP; 341 family->ops = ops;
315 if (ops->doit) 342 family->n_ops = n_ops;
316 ops->flags |= GENL_CMD_CAP_DO;
317 if (ops->policy)
318 ops->flags |= GENL_CMD_CAP_HASPOL;
319 343
320 genl_lock_all(); 344 return 0;
321 list_add_tail(&ops->ops_list, &family->ops_list);
322 genl_unlock_all();
323
324 genl_ctrl_event(CTRL_CMD_NEWOPS, ops);
325 err = 0;
326errout:
327 return err;
328}
329EXPORT_SYMBOL(genl_register_ops);
330
331/**
332 * genl_unregister_ops - unregister generic netlink operations
333 * @family: generic netlink family
334 * @ops: operations to be unregistered
335 *
336 * Unregisters the specified operations and unassigns them from the
337 * specified family. The operation blocks until the current message
338 * processing has finished and doesn't start again until the
339 * unregister process has finished.
340 *
341 * Note: It is not necessary to unregister all operations before
342 * unregistering the family, unregistering the family will cause
343 * all assigned operations to be unregistered automatically.
344 *
345 * Returns 0 on success or a negative error code.
346 */
347int genl_unregister_ops(struct genl_family *family, struct genl_ops *ops)
348{
349 struct genl_ops *rc;
350
351 genl_lock_all();
352 list_for_each_entry(rc, &family->ops_list, ops_list) {
353 if (rc == ops) {
354 list_del(&ops->ops_list);
355 genl_unlock_all();
356 genl_ctrl_event(CTRL_CMD_DELOPS, ops);
357 return 0;
358 }
359 }
360 genl_unlock_all();
361
362 return -ENOENT;
363} 345}
364EXPORT_SYMBOL(genl_unregister_ops);
365 346
366/** 347/**
367 * __genl_register_family - register a generic netlink family 348 * __genl_register_family - register a generic netlink family
@@ -372,11 +353,14 @@ EXPORT_SYMBOL(genl_unregister_ops);
372 * The family id may equal GENL_ID_GENERATE causing an unique id to 353 * The family id may equal GENL_ID_GENERATE causing an unique id to
373 * be automatically generated and assigned. 354 * be automatically generated and assigned.
374 * 355 *
356 * The family's ops array must already be assigned, you can use the
357 * genl_register_family_with_ops() helper function.
358 *
375 * Return 0 on success or a negative error code. 359 * Return 0 on success or a negative error code.
376 */ 360 */
377int __genl_register_family(struct genl_family *family) 361int __genl_register_family(struct genl_family *family)
378{ 362{
379 int err = -EINVAL; 363 int err = -EINVAL, i;
380 364
381 if (family->id && family->id < GENL_MIN_ID) 365 if (family->id && family->id < GENL_MIN_ID)
382 goto errout; 366 goto errout;
@@ -384,8 +368,9 @@ int __genl_register_family(struct genl_family *family)
384 if (family->id > GENL_MAX_ID) 368 if (family->id > GENL_MAX_ID)
385 goto errout; 369 goto errout;
386 370
387 INIT_LIST_HEAD(&family->ops_list); 371 err = genl_validate_ops(family);
388 INIT_LIST_HEAD(&family->mcast_groups); 372 if (err)
373 return err;
389 374
390 genl_lock_all(); 375 genl_lock_all();
391 376
@@ -418,10 +403,18 @@ int __genl_register_family(struct genl_family *family)
418 } else 403 } else
419 family->attrbuf = NULL; 404 family->attrbuf = NULL;
420 405
406 err = genl_validate_assign_mc_groups(family);
407 if (err)
408 goto errout_locked;
409
421 list_add_tail(&family->family_list, genl_family_chain(family->id)); 410 list_add_tail(&family->family_list, genl_family_chain(family->id));
422 genl_unlock_all(); 411 genl_unlock_all();
423 412
424 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family); 413 /* send all events */
414 genl_ctrl_event(CTRL_CMD_NEWFAMILY, family, NULL, 0);
415 for (i = 0; i < family->n_mcgrps; i++)
416 genl_ctrl_event(CTRL_CMD_NEWMCAST_GRP, family,
417 &family->mcgrps[i], family->mcgrp_offset + i);
425 418
426 return 0; 419 return 0;
427 420
@@ -433,52 +426,6 @@ errout:
433EXPORT_SYMBOL(__genl_register_family); 426EXPORT_SYMBOL(__genl_register_family);
434 427
435/** 428/**
436 * __genl_register_family_with_ops - register a generic netlink family
437 * @family: generic netlink family
438 * @ops: operations to be registered
439 * @n_ops: number of elements to register
440 *
441 * Registers the specified family and operations from the specified table.
442 * Only one family may be registered with the same family name or identifier.
443 *
444 * The family id may equal GENL_ID_GENERATE causing an unique id to
445 * be automatically generated and assigned.
446 *
447 * Either a doit or dumpit callback must be specified for every registered
448 * operation or the function will fail. Only one operation structure per
449 * command identifier may be registered.
450 *
451 * See include/net/genetlink.h for more documenation on the operations
452 * structure.
453 *
454 * This is equivalent to calling genl_register_family() followed by
455 * genl_register_ops() for every operation entry in the table taking
456 * care to unregister the family on error path.
457 *
458 * Return 0 on success or a negative error code.
459 */
460int __genl_register_family_with_ops(struct genl_family *family,
461 struct genl_ops *ops, size_t n_ops)
462{
463 int err, i;
464
465 err = __genl_register_family(family);
466 if (err)
467 return err;
468
469 for (i = 0; i < n_ops; ++i, ++ops) {
470 err = genl_register_ops(family, ops);
471 if (err)
472 goto err_out;
473 }
474 return 0;
475err_out:
476 genl_unregister_family(family);
477 return err;
478}
479EXPORT_SYMBOL(__genl_register_family_with_ops);
480
481/**
482 * genl_unregister_family - unregister generic netlink family 429 * genl_unregister_family - unregister generic netlink family
483 * @family: generic netlink family 430 * @family: generic netlink family
484 * 431 *
@@ -499,11 +446,11 @@ int genl_unregister_family(struct genl_family *family)
499 continue; 446 continue;
500 447
501 list_del(&rc->family_list); 448 list_del(&rc->family_list);
502 INIT_LIST_HEAD(&family->ops_list); 449 family->n_ops = 0;
503 genl_unlock_all(); 450 genl_unlock_all();
504 451
505 kfree(family->attrbuf); 452 kfree(family->attrbuf);
506 genl_ctrl_event(CTRL_CMD_DELFAMILY, family); 453 genl_ctrl_event(CTRL_CMD_DELFAMILY, family, NULL, 0);
507 return 0; 454 return 0;
508 } 455 }
509 456
@@ -546,7 +493,8 @@ EXPORT_SYMBOL(genlmsg_put);
546 493
547static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb) 494static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
548{ 495{
549 struct genl_ops *ops = cb->data; 496 /* our ops are always const - netlink API doesn't propagate that */
497 const struct genl_ops *ops = cb->data;
550 int rc; 498 int rc;
551 499
552 genl_lock(); 500 genl_lock();
@@ -557,7 +505,8 @@ static int genl_lock_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
557 505
558static int genl_lock_done(struct netlink_callback *cb) 506static int genl_lock_done(struct netlink_callback *cb)
559{ 507{
560 struct genl_ops *ops = cb->data; 508 /* our ops are always const - netlink API doesn't propagate that */
509 const struct genl_ops *ops = cb->data;
561 int rc = 0; 510 int rc = 0;
562 511
563 if (ops->done) { 512 if (ops->done) {
@@ -572,7 +521,7 @@ static int genl_family_rcv_msg(struct genl_family *family,
572 struct sk_buff *skb, 521 struct sk_buff *skb,
573 struct nlmsghdr *nlh) 522 struct nlmsghdr *nlh)
574{ 523{
575 struct genl_ops *ops; 524 const struct genl_ops *ops;
576 struct net *net = sock_net(skb->sk); 525 struct net *net = sock_net(skb->sk);
577 struct genl_info info; 526 struct genl_info info;
578 struct genlmsghdr *hdr = nlmsg_data(nlh); 527 struct genlmsghdr *hdr = nlmsg_data(nlh);
@@ -604,7 +553,8 @@ static int genl_family_rcv_msg(struct genl_family *family,
604 if (!family->parallel_ops) { 553 if (!family->parallel_ops) {
605 struct netlink_dump_control c = { 554 struct netlink_dump_control c = {
606 .module = family->module, 555 .module = family->module,
607 .data = ops, 556 /* we have const, but the netlink API doesn't */
557 .data = (void *)ops,
608 .dump = genl_lock_dumpit, 558 .dump = genl_lock_dumpit,
609 .done = genl_lock_done, 559 .done = genl_lock_done,
610 }; 560 };
@@ -726,24 +676,32 @@ static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
726 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr)) 676 nla_put_u32(skb, CTRL_ATTR_MAXATTR, family->maxattr))
727 goto nla_put_failure; 677 goto nla_put_failure;
728 678
729 if (!list_empty(&family->ops_list)) { 679 if (family->n_ops) {
730 struct nlattr *nla_ops; 680 struct nlattr *nla_ops;
731 struct genl_ops *ops; 681 int i;
732 int idx = 1;
733 682
734 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS); 683 nla_ops = nla_nest_start(skb, CTRL_ATTR_OPS);
735 if (nla_ops == NULL) 684 if (nla_ops == NULL)
736 goto nla_put_failure; 685 goto nla_put_failure;
737 686
738 list_for_each_entry(ops, &family->ops_list, ops_list) { 687 for (i = 0; i < family->n_ops; i++) {
739 struct nlattr *nest; 688 struct nlattr *nest;
689 const struct genl_ops *ops = &family->ops[i];
690 u32 op_flags = ops->flags;
740 691
741 nest = nla_nest_start(skb, idx++); 692 if (ops->dumpit)
693 op_flags |= GENL_CMD_CAP_DUMP;
694 if (ops->doit)
695 op_flags |= GENL_CMD_CAP_DO;
696 if (ops->policy)
697 op_flags |= GENL_CMD_CAP_HASPOL;
698
699 nest = nla_nest_start(skb, i + 1);
742 if (nest == NULL) 700 if (nest == NULL)
743 goto nla_put_failure; 701 goto nla_put_failure;
744 702
745 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) || 703 if (nla_put_u32(skb, CTRL_ATTR_OP_ID, ops->cmd) ||
746 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, ops->flags)) 704 nla_put_u32(skb, CTRL_ATTR_OP_FLAGS, op_flags))
747 goto nla_put_failure; 705 goto nla_put_failure;
748 706
749 nla_nest_end(skb, nest); 707 nla_nest_end(skb, nest);
@@ -752,23 +710,26 @@ static int ctrl_fill_info(struct genl_family *family, u32 portid, u32 seq,
752 nla_nest_end(skb, nla_ops); 710 nla_nest_end(skb, nla_ops);
753 } 711 }
754 712
755 if (!list_empty(&family->mcast_groups)) { 713 if (family->n_mcgrps) {
756 struct genl_multicast_group *grp;
757 struct nlattr *nla_grps; 714 struct nlattr *nla_grps;
758 int idx = 1; 715 int i;
759 716
760 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); 717 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
761 if (nla_grps == NULL) 718 if (nla_grps == NULL)
762 goto nla_put_failure; 719 goto nla_put_failure;
763 720
764 list_for_each_entry(grp, &family->mcast_groups, list) { 721 for (i = 0; i < family->n_mcgrps; i++) {
765 struct nlattr *nest; 722 struct nlattr *nest;
723 const struct genl_multicast_group *grp;
724
725 grp = &family->mcgrps[i];
766 726
767 nest = nla_nest_start(skb, idx++); 727 nest = nla_nest_start(skb, i + 1);
768 if (nest == NULL) 728 if (nest == NULL)
769 goto nla_put_failure; 729 goto nla_put_failure;
770 730
771 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) || 731 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID,
732 family->mcgrp_offset + i) ||
772 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, 733 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
773 grp->name)) 734 grp->name))
774 goto nla_put_failure; 735 goto nla_put_failure;
@@ -785,9 +746,10 @@ nla_put_failure:
785 return -EMSGSIZE; 746 return -EMSGSIZE;
786} 747}
787 748
788static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid, 749static int ctrl_fill_mcgrp_info(struct genl_family *family,
789 u32 seq, u32 flags, struct sk_buff *skb, 750 const struct genl_multicast_group *grp,
790 u8 cmd) 751 int grp_id, u32 portid, u32 seq, u32 flags,
752 struct sk_buff *skb, u8 cmd)
791{ 753{
792 void *hdr; 754 void *hdr;
793 struct nlattr *nla_grps; 755 struct nlattr *nla_grps;
@@ -797,8 +759,8 @@ static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
797 if (hdr == NULL) 759 if (hdr == NULL)
798 return -1; 760 return -1;
799 761
800 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, grp->family->name) || 762 if (nla_put_string(skb, CTRL_ATTR_FAMILY_NAME, family->name) ||
801 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, grp->family->id)) 763 nla_put_u16(skb, CTRL_ATTR_FAMILY_ID, family->id))
802 goto nla_put_failure; 764 goto nla_put_failure;
803 765
804 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS); 766 nla_grps = nla_nest_start(skb, CTRL_ATTR_MCAST_GROUPS);
@@ -809,7 +771,7 @@ static int ctrl_fill_mcgrp_info(struct genl_multicast_group *grp, u32 portid,
809 if (nest == NULL) 771 if (nest == NULL)
810 goto nla_put_failure; 772 goto nla_put_failure;
811 773
812 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp->id) || 774 if (nla_put_u32(skb, CTRL_ATTR_MCAST_GRP_ID, grp_id) ||
813 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME, 775 nla_put_string(skb, CTRL_ATTR_MCAST_GRP_NAME,
814 grp->name)) 776 grp->name))
815 goto nla_put_failure; 777 goto nla_put_failure;
@@ -875,8 +837,10 @@ static struct sk_buff *ctrl_build_family_msg(struct genl_family *family,
875 return skb; 837 return skb;
876} 838}
877 839
878static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp, 840static struct sk_buff *
879 u32 portid, int seq, u8 cmd) 841ctrl_build_mcgrp_msg(struct genl_family *family,
842 const struct genl_multicast_group *grp,
843 int grp_id, u32 portid, int seq, u8 cmd)
880{ 844{
881 struct sk_buff *skb; 845 struct sk_buff *skb;
882 int err; 846 int err;
@@ -885,7 +849,8 @@ static struct sk_buff *ctrl_build_mcgrp_msg(struct genl_multicast_group *grp,
885 if (skb == NULL) 849 if (skb == NULL)
886 return ERR_PTR(-ENOBUFS); 850 return ERR_PTR(-ENOBUFS);
887 851
888 err = ctrl_fill_mcgrp_info(grp, portid, seq, 0, skb, cmd); 852 err = ctrl_fill_mcgrp_info(family, grp, grp_id, portid,
853 seq, 0, skb, cmd);
889 if (err < 0) { 854 if (err < 0) {
890 nlmsg_free(skb); 855 nlmsg_free(skb);
891 return ERR_PTR(err); 856 return ERR_PTR(err);
@@ -947,11 +912,11 @@ static int ctrl_getfamily(struct sk_buff *skb, struct genl_info *info)
947 return genlmsg_reply(msg, info); 912 return genlmsg_reply(msg, info);
948} 913}
949 914
950static int genl_ctrl_event(int event, void *data) 915static int genl_ctrl_event(int event, struct genl_family *family,
916 const struct genl_multicast_group *grp,
917 int grp_id)
951{ 918{
952 struct sk_buff *msg; 919 struct sk_buff *msg;
953 struct genl_family *family;
954 struct genl_multicast_group *grp;
955 920
956 /* genl is still initialising */ 921 /* genl is still initialising */
957 if (!init_net.genl_sock) 922 if (!init_net.genl_sock)
@@ -960,14 +925,13 @@ static int genl_ctrl_event(int event, void *data)
960 switch (event) { 925 switch (event) {
961 case CTRL_CMD_NEWFAMILY: 926 case CTRL_CMD_NEWFAMILY:
962 case CTRL_CMD_DELFAMILY: 927 case CTRL_CMD_DELFAMILY:
963 family = data; 928 WARN_ON(grp);
964 msg = ctrl_build_family_msg(family, 0, 0, event); 929 msg = ctrl_build_family_msg(family, 0, 0, event);
965 break; 930 break;
966 case CTRL_CMD_NEWMCAST_GRP: 931 case CTRL_CMD_NEWMCAST_GRP:
967 case CTRL_CMD_DELMCAST_GRP: 932 case CTRL_CMD_DELMCAST_GRP:
968 grp = data; 933 BUG_ON(!grp);
969 family = grp->family; 934 msg = ctrl_build_mcgrp_msg(family, grp, grp_id, 0, 0, event);
970 msg = ctrl_build_mcgrp_msg(data, 0, 0, event);
971 break; 935 break;
972 default: 936 default:
973 return -EINVAL; 937 return -EINVAL;
@@ -977,26 +941,29 @@ static int genl_ctrl_event(int event, void *data)
977 return PTR_ERR(msg); 941 return PTR_ERR(msg);
978 942
979 if (!family->netnsok) { 943 if (!family->netnsok) {
980 genlmsg_multicast_netns(&init_net, msg, 0, 944 genlmsg_multicast_netns(&genl_ctrl, &init_net, msg, 0,
981 GENL_ID_CTRL, GFP_KERNEL); 945 0, GFP_KERNEL);
982 } else { 946 } else {
983 rcu_read_lock(); 947 rcu_read_lock();
984 genlmsg_multicast_allns(msg, 0, GENL_ID_CTRL, GFP_ATOMIC); 948 genlmsg_multicast_allns(&genl_ctrl, msg, 0,
949 0, GFP_ATOMIC);
985 rcu_read_unlock(); 950 rcu_read_unlock();
986 } 951 }
987 952
988 return 0; 953 return 0;
989} 954}
990 955
991static struct genl_ops genl_ctrl_ops = { 956static struct genl_ops genl_ctrl_ops[] = {
992 .cmd = CTRL_CMD_GETFAMILY, 957 {
993 .doit = ctrl_getfamily, 958 .cmd = CTRL_CMD_GETFAMILY,
994 .dumpit = ctrl_dumpfamily, 959 .doit = ctrl_getfamily,
995 .policy = ctrl_policy, 960 .dumpit = ctrl_dumpfamily,
961 .policy = ctrl_policy,
962 },
996}; 963};
997 964
998static struct genl_multicast_group notify_grp = { 965static struct genl_multicast_group genl_ctrl_groups[] = {
999 .name = "notify", 966 { .name = "notify", },
1000}; 967};
1001 968
1002static int __net_init genl_pernet_init(struct net *net) 969static int __net_init genl_pernet_init(struct net *net)
@@ -1036,7 +1003,8 @@ static int __init genl_init(void)
1036 for (i = 0; i < GENL_FAM_TAB_SIZE; i++) 1003 for (i = 0; i < GENL_FAM_TAB_SIZE; i++)
1037 INIT_LIST_HEAD(&family_ht[i]); 1004 INIT_LIST_HEAD(&family_ht[i]);
1038 1005
1039 err = genl_register_family_with_ops(&genl_ctrl, &genl_ctrl_ops, 1); 1006 err = genl_register_family_with_ops_groups(&genl_ctrl, genl_ctrl_ops,
1007 genl_ctrl_groups);
1040 if (err < 0) 1008 if (err < 0)
1041 goto problem; 1009 goto problem;
1042 1010
@@ -1044,10 +1012,6 @@ static int __init genl_init(void)
1044 if (err) 1012 if (err)
1045 goto problem; 1013 goto problem;
1046 1014
1047 err = genl_register_mc_group(&genl_ctrl, &notify_grp);
1048 if (err < 0)
1049 goto problem;
1050
1051 return 0; 1015 return 0;
1052 1016
1053problem: 1017problem:
@@ -1085,14 +1049,18 @@ static int genlmsg_mcast(struct sk_buff *skb, u32 portid, unsigned long group,
1085 return err; 1049 return err;
1086} 1050}
1087 1051
1088int genlmsg_multicast_allns(struct sk_buff *skb, u32 portid, unsigned int group, 1052int genlmsg_multicast_allns(struct genl_family *family, struct sk_buff *skb,
1089 gfp_t flags) 1053 u32 portid, unsigned int group, gfp_t flags)
1090{ 1054{
1055 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1056 return -EINVAL;
1057 group = family->mcgrp_offset + group;
1091 return genlmsg_mcast(skb, portid, group, flags); 1058 return genlmsg_mcast(skb, portid, group, flags);
1092} 1059}
1093EXPORT_SYMBOL(genlmsg_multicast_allns); 1060EXPORT_SYMBOL(genlmsg_multicast_allns);
1094 1061
1095void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group, 1062void genl_notify(struct genl_family *family,
1063 struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1096 struct nlmsghdr *nlh, gfp_t flags) 1064 struct nlmsghdr *nlh, gfp_t flags)
1097{ 1065{
1098 struct sock *sk = net->genl_sock; 1066 struct sock *sk = net->genl_sock;
@@ -1101,6 +1069,9 @@ void genl_notify(struct sk_buff *skb, struct net *net, u32 portid, u32 group,
1101 if (nlh) 1069 if (nlh)
1102 report = nlmsg_report(nlh); 1070 report = nlmsg_report(nlh);
1103 1071
1072 if (WARN_ON_ONCE(group >= family->n_mcgrps))
1073 return;
1074 group = family->mcgrp_offset + group;
1104 nlmsg_notify(sk, skb, portid, group, report, flags); 1075 nlmsg_notify(sk, skb, portid, group, report, flags);
1105} 1076}
1106EXPORT_SYMBOL(genl_notify); 1077EXPORT_SYMBOL(genl_notify);
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
index 698814bfa7ad..53c19a35fc6d 100644
--- a/net/netrom/af_netrom.c
+++ b/net/netrom/af_netrom.c
@@ -1179,10 +1179,9 @@ static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1179 sax->sax25_family = AF_NETROM; 1179 sax->sax25_family = AF_NETROM;
1180 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call, 1180 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1181 AX25_ADDR_LEN); 1181 AX25_ADDR_LEN);
1182 msg->msg_namelen = sizeof(*sax);
1182 } 1183 }
1183 1184
1184 msg->msg_namelen = sizeof(*sax);
1185
1186 skb_free_datagram(sk, skb); 1185 skb_free_datagram(sk, skb);
1187 1186
1188 release_sock(sk); 1187 release_sock(sk);
diff --git a/net/nfc/Kconfig b/net/nfc/Kconfig
index 5948b2fc72f6..6e0fa0cce198 100644
--- a/net/nfc/Kconfig
+++ b/net/nfc/Kconfig
@@ -14,6 +14,20 @@ menuconfig NFC
14 To compile this support as a module, choose M here: the module will 14 To compile this support as a module, choose M here: the module will
15 be called nfc. 15 be called nfc.
16 16
17config NFC_DIGITAL
18 depends on NFC
19 select CRC_CCITT
20 select CRC_ITU_T
21 tristate "NFC Digital Protocol stack support"
22 default n
23 help
24 Say Y if you want to build NFC digital protocol stack support.
25 This is needed by NFC chipsets whose firmware only implement
26 the NFC analog layer.
27
28 To compile this support as a module, choose M here: the module will
29 be called nfc_digital.
30
17source "net/nfc/nci/Kconfig" 31source "net/nfc/nci/Kconfig"
18source "net/nfc/hci/Kconfig" 32source "net/nfc/hci/Kconfig"
19 33
diff --git a/net/nfc/Makefile b/net/nfc/Makefile
index a76f4533cb6c..2555ff8e7219 100644
--- a/net/nfc/Makefile
+++ b/net/nfc/Makefile
@@ -5,7 +5,9 @@
5obj-$(CONFIG_NFC) += nfc.o 5obj-$(CONFIG_NFC) += nfc.o
6obj-$(CONFIG_NFC_NCI) += nci/ 6obj-$(CONFIG_NFC_NCI) += nci/
7obj-$(CONFIG_NFC_HCI) += hci/ 7obj-$(CONFIG_NFC_HCI) += hci/
8obj-$(CONFIG_NFC_DIGITAL) += nfc_digital.o
8 9
9nfc-objs := core.o netlink.o af_nfc.o rawsock.o llcp_core.o llcp_commands.o \ 10nfc-objs := core.o netlink.o af_nfc.o rawsock.o llcp_core.o llcp_commands.o \
10 llcp_sock.o 11 llcp_sock.o
11 12
13nfc_digital-objs := digital_core.o digital_technology.o digital_dep.o
diff --git a/net/nfc/core.c b/net/nfc/core.c
index e92923cf3e03..83b9927e7d19 100644
--- a/net/nfc/core.c
+++ b/net/nfc/core.c
@@ -384,6 +384,19 @@ int nfc_dep_link_is_up(struct nfc_dev *dev, u32 target_idx,
384{ 384{
385 dev->dep_link_up = true; 385 dev->dep_link_up = true;
386 386
387 if (!dev->active_target && rf_mode == NFC_RF_INITIATOR) {
388 struct nfc_target *target;
389
390 target = nfc_find_target(dev, target_idx);
391 if (target == NULL)
392 return -ENOTCONN;
393
394 dev->active_target = target;
395 }
396
397 dev->polling = false;
398 dev->rf_mode = rf_mode;
399
387 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode); 400 nfc_llcp_mac_is_up(dev, target_idx, comm_mode, rf_mode);
388 401
389 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode); 402 return nfc_genl_dep_link_up_event(dev, target_idx, comm_mode, rf_mode);
@@ -536,7 +549,7 @@ error:
536 return rc; 549 return rc;
537} 550}
538 551
539static struct nfc_se *find_se(struct nfc_dev *dev, u32 se_idx) 552struct nfc_se *nfc_find_se(struct nfc_dev *dev, u32 se_idx)
540{ 553{
541 struct nfc_se *se, *n; 554 struct nfc_se *se, *n;
542 555
@@ -546,6 +559,7 @@ static struct nfc_se *find_se(struct nfc_dev *dev, u32 se_idx)
546 559
547 return NULL; 560 return NULL;
548} 561}
562EXPORT_SYMBOL(nfc_find_se);
549 563
550int nfc_enable_se(struct nfc_dev *dev, u32 se_idx) 564int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
551{ 565{
@@ -577,7 +591,7 @@ int nfc_enable_se(struct nfc_dev *dev, u32 se_idx)
577 goto error; 591 goto error;
578 } 592 }
579 593
580 se = find_se(dev, se_idx); 594 se = nfc_find_se(dev, se_idx);
581 if (!se) { 595 if (!se) {
582 rc = -EINVAL; 596 rc = -EINVAL;
583 goto error; 597 goto error;
@@ -622,7 +636,7 @@ int nfc_disable_se(struct nfc_dev *dev, u32 se_idx)
622 goto error; 636 goto error;
623 } 637 }
624 638
625 se = find_se(dev, se_idx); 639 se = nfc_find_se(dev, se_idx);
626 if (!se) { 640 if (!se) {
627 rc = -EINVAL; 641 rc = -EINVAL;
628 goto error; 642 goto error;
@@ -881,7 +895,7 @@ int nfc_add_se(struct nfc_dev *dev, u32 se_idx, u16 type)
881 895
882 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx); 896 pr_debug("%s se index %d\n", dev_name(&dev->dev), se_idx);
883 897
884 se = find_se(dev, se_idx); 898 se = nfc_find_se(dev, se_idx);
885 if (se) 899 if (se)
886 return -EALREADY; 900 return -EALREADY;
887 901
diff --git a/net/nfc/digital.h b/net/nfc/digital.h
new file mode 100644
index 000000000000..08b29b55ea63
--- /dev/null
+++ b/net/nfc/digital.h
@@ -0,0 +1,170 @@
1/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#ifndef __DIGITAL_H
17#define __DIGITAL_H
18
19#include <net/nfc/nfc.h>
20#include <net/nfc/digital.h>
21
22#include <linux/crc-ccitt.h>
23#include <linux/crc-itu-t.h>
24
25#define PROTOCOL_ERR(req) pr_err("%d: NFC Digital Protocol error: %s\n", \
26 __LINE__, req)
27
28#define DIGITAL_CMD_IN_SEND 0
29#define DIGITAL_CMD_TG_SEND 1
30#define DIGITAL_CMD_TG_LISTEN 2
31#define DIGITAL_CMD_TG_LISTEN_MDAA 3
32
33#define DIGITAL_MAX_HEADER_LEN 7
34#define DIGITAL_CRC_LEN 2
35
36#define DIGITAL_SENSF_NFCID2_NFC_DEP_B1 0x01
37#define DIGITAL_SENSF_NFCID2_NFC_DEP_B2 0xFE
38
39#define DIGITAL_SENS_RES_NFC_DEP 0x0100
40#define DIGITAL_SEL_RES_NFC_DEP 0x40
41#define DIGITAL_SENSF_FELICA_SC 0xFFFF
42
43#define DIGITAL_DRV_CAPS_IN_CRC(ddev) \
44 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_IN_CRC)
45#define DIGITAL_DRV_CAPS_TG_CRC(ddev) \
46 ((ddev)->driver_capabilities & NFC_DIGITAL_DRV_CAPS_TG_CRC)
47
48struct digital_data_exch {
49 data_exchange_cb_t cb;
50 void *cb_context;
51};
52
53struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
54 unsigned int len);
55
56int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
57 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
58 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
59 void *cb_context);
60
61int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
62static inline int digital_in_send_cmd(struct nfc_digital_dev *ddev,
63 struct sk_buff *skb, u16 timeout,
64 nfc_digital_cmd_complete_t cmd_cb,
65 void *cb_context)
66{
67 return digital_send_cmd(ddev, DIGITAL_CMD_IN_SEND, skb, NULL, timeout,
68 cmd_cb, cb_context);
69}
70
71void digital_poll_next_tech(struct nfc_digital_dev *ddev);
72
73int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech);
74int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech);
75
76int digital_target_found(struct nfc_digital_dev *ddev,
77 struct nfc_target *target, u8 protocol);
78
79int digital_in_recv_mifare_res(struct sk_buff *resp);
80
81int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
82 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
83 size_t gb_len);
84int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
85 struct nfc_target *target, struct sk_buff *skb,
86 struct digital_data_exch *data_exch);
87
88int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param);
89static inline int digital_tg_send_cmd(struct nfc_digital_dev *ddev,
90 struct sk_buff *skb, u16 timeout,
91 nfc_digital_cmd_complete_t cmd_cb, void *cb_context)
92{
93 return digital_send_cmd(ddev, DIGITAL_CMD_TG_SEND, skb, NULL, timeout,
94 cmd_cb, cb_context);
95}
96
97void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
98 struct sk_buff *resp);
99
100void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
101 struct sk_buff *resp);
102
103static inline int digital_tg_listen(struct nfc_digital_dev *ddev, u16 timeout,
104 nfc_digital_cmd_complete_t cb, void *arg)
105{
106 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN, NULL, NULL,
107 timeout, cb, arg);
108}
109
110void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
111 struct sk_buff *resp);
112
113int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb);
114
115int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech);
116int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech);
117
118typedef u16 (*crc_func_t)(u16, const u8 *, size_t);
119
120#define CRC_A_INIT 0x6363
121#define CRC_B_INIT 0xFFFF
122#define CRC_F_INIT 0x0000
123
124void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
125 u8 bitwise_inv, u8 msb_first);
126
127static inline void digital_skb_add_crc_a(struct sk_buff *skb)
128{
129 digital_skb_add_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
130}
131
132static inline void digital_skb_add_crc_b(struct sk_buff *skb)
133{
134 digital_skb_add_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
135}
136
137static inline void digital_skb_add_crc_f(struct sk_buff *skb)
138{
139 digital_skb_add_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
140}
141
142static inline void digital_skb_add_crc_none(struct sk_buff *skb)
143{
144 return;
145}
146
147int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
148 u16 crc_init, u8 bitwise_inv, u8 msb_first);
149
150static inline int digital_skb_check_crc_a(struct sk_buff *skb)
151{
152 return digital_skb_check_crc(skb, crc_ccitt, CRC_A_INIT, 0, 0);
153}
154
155static inline int digital_skb_check_crc_b(struct sk_buff *skb)
156{
157 return digital_skb_check_crc(skb, crc_ccitt, CRC_B_INIT, 1, 0);
158}
159
160static inline int digital_skb_check_crc_f(struct sk_buff *skb)
161{
162 return digital_skb_check_crc(skb, crc_itu_t, CRC_F_INIT, 0, 1);
163}
164
165static inline int digital_skb_check_crc_none(struct sk_buff *skb)
166{
167 return 0;
168}
169
170#endif /* __DIGITAL_H */
diff --git a/net/nfc/digital_core.c b/net/nfc/digital_core.c
new file mode 100644
index 000000000000..09fc95439955
--- /dev/null
+++ b/net/nfc/digital_core.c
@@ -0,0 +1,737 @@
1/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18#include <linux/module.h>
19
20#include "digital.h"
21
22#define DIGITAL_PROTO_NFCA_RF_TECH \
23 (NFC_PROTO_JEWEL_MASK | NFC_PROTO_MIFARE_MASK | NFC_PROTO_NFC_DEP_MASK)
24
25#define DIGITAL_PROTO_NFCF_RF_TECH \
26 (NFC_PROTO_FELICA_MASK | NFC_PROTO_NFC_DEP_MASK)
27
28struct digital_cmd {
29 struct list_head queue;
30
31 u8 type;
32 u8 pending;
33
34 u16 timeout;
35 struct sk_buff *req;
36 struct sk_buff *resp;
37 struct digital_tg_mdaa_params *mdaa_params;
38
39 nfc_digital_cmd_complete_t cmd_cb;
40 void *cb_context;
41};
42
43struct sk_buff *digital_skb_alloc(struct nfc_digital_dev *ddev,
44 unsigned int len)
45{
46 struct sk_buff *skb;
47
48 skb = alloc_skb(len + ddev->tx_headroom + ddev->tx_tailroom,
49 GFP_KERNEL);
50 if (skb)
51 skb_reserve(skb, ddev->tx_headroom);
52
53 return skb;
54}
55
56void digital_skb_add_crc(struct sk_buff *skb, crc_func_t crc_func, u16 init,
57 u8 bitwise_inv, u8 msb_first)
58{
59 u16 crc;
60
61 crc = crc_func(init, skb->data, skb->len);
62
63 if (bitwise_inv)
64 crc = ~crc;
65
66 if (msb_first)
67 crc = __fswab16(crc);
68
69 *skb_put(skb, 1) = crc & 0xFF;
70 *skb_put(skb, 1) = (crc >> 8) & 0xFF;
71}
72
73int digital_skb_check_crc(struct sk_buff *skb, crc_func_t crc_func,
74 u16 crc_init, u8 bitwise_inv, u8 msb_first)
75{
76 int rc;
77 u16 crc;
78
79 if (skb->len <= 2)
80 return -EIO;
81
82 crc = crc_func(crc_init, skb->data, skb->len - 2);
83
84 if (bitwise_inv)
85 crc = ~crc;
86
87 if (msb_first)
88 crc = __swab16(crc);
89
90 rc = (skb->data[skb->len - 2] - (crc & 0xFF)) +
91 (skb->data[skb->len - 1] - ((crc >> 8) & 0xFF));
92
93 if (rc)
94 return -EIO;
95
96 skb_trim(skb, skb->len - 2);
97
98 return 0;
99}
100
101static inline void digital_switch_rf(struct nfc_digital_dev *ddev, bool on)
102{
103 ddev->ops->switch_rf(ddev, on);
104}
105
106static inline void digital_abort_cmd(struct nfc_digital_dev *ddev)
107{
108 ddev->ops->abort_cmd(ddev);
109}
110
111static void digital_wq_cmd_complete(struct work_struct *work)
112{
113 struct digital_cmd *cmd;
114 struct nfc_digital_dev *ddev = container_of(work,
115 struct nfc_digital_dev,
116 cmd_complete_work);
117
118 mutex_lock(&ddev->cmd_lock);
119
120 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
121 queue);
122 if (!cmd) {
123 mutex_unlock(&ddev->cmd_lock);
124 return;
125 }
126
127 list_del(&cmd->queue);
128
129 mutex_unlock(&ddev->cmd_lock);
130
131 if (!IS_ERR(cmd->resp))
132 print_hex_dump_debug("DIGITAL RX: ", DUMP_PREFIX_NONE, 16, 1,
133 cmd->resp->data, cmd->resp->len, false);
134
135 cmd->cmd_cb(ddev, cmd->cb_context, cmd->resp);
136
137 kfree(cmd->mdaa_params);
138 kfree(cmd);
139
140 schedule_work(&ddev->cmd_work);
141}
142
143static void digital_send_cmd_complete(struct nfc_digital_dev *ddev,
144 void *arg, struct sk_buff *resp)
145{
146 struct digital_cmd *cmd = arg;
147
148 cmd->resp = resp;
149
150 schedule_work(&ddev->cmd_complete_work);
151}
152
153static void digital_wq_cmd(struct work_struct *work)
154{
155 int rc;
156 struct digital_cmd *cmd;
157 struct digital_tg_mdaa_params *params;
158 struct nfc_digital_dev *ddev = container_of(work,
159 struct nfc_digital_dev,
160 cmd_work);
161
162 mutex_lock(&ddev->cmd_lock);
163
164 cmd = list_first_entry_or_null(&ddev->cmd_queue, struct digital_cmd,
165 queue);
166 if (!cmd || cmd->pending) {
167 mutex_unlock(&ddev->cmd_lock);
168 return;
169 }
170
171 mutex_unlock(&ddev->cmd_lock);
172
173 if (cmd->req)
174 print_hex_dump_debug("DIGITAL TX: ", DUMP_PREFIX_NONE, 16, 1,
175 cmd->req->data, cmd->req->len, false);
176
177 switch (cmd->type) {
178 case DIGITAL_CMD_IN_SEND:
179 rc = ddev->ops->in_send_cmd(ddev, cmd->req, cmd->timeout,
180 digital_send_cmd_complete, cmd);
181 break;
182
183 case DIGITAL_CMD_TG_SEND:
184 rc = ddev->ops->tg_send_cmd(ddev, cmd->req, cmd->timeout,
185 digital_send_cmd_complete, cmd);
186 break;
187
188 case DIGITAL_CMD_TG_LISTEN:
189 rc = ddev->ops->tg_listen(ddev, cmd->timeout,
190 digital_send_cmd_complete, cmd);
191 break;
192
193 case DIGITAL_CMD_TG_LISTEN_MDAA:
194 params = cmd->mdaa_params;
195
196 rc = ddev->ops->tg_listen_mdaa(ddev, params, cmd->timeout,
197 digital_send_cmd_complete, cmd);
198 break;
199
200 default:
201 pr_err("Unknown cmd type %d\n", cmd->type);
202 return;
203 }
204
205 if (!rc)
206 return;
207
208 pr_err("in_send_command returned err %d\n", rc);
209
210 mutex_lock(&ddev->cmd_lock);
211 list_del(&cmd->queue);
212 mutex_unlock(&ddev->cmd_lock);
213
214 kfree_skb(cmd->req);
215 kfree(cmd->mdaa_params);
216 kfree(cmd);
217
218 schedule_work(&ddev->cmd_work);
219}
220
221int digital_send_cmd(struct nfc_digital_dev *ddev, u8 cmd_type,
222 struct sk_buff *skb, struct digital_tg_mdaa_params *params,
223 u16 timeout, nfc_digital_cmd_complete_t cmd_cb,
224 void *cb_context)
225{
226 struct digital_cmd *cmd;
227
228 cmd = kzalloc(sizeof(struct digital_cmd), GFP_KERNEL);
229 if (!cmd)
230 return -ENOMEM;
231
232 cmd->type = cmd_type;
233 cmd->timeout = timeout;
234 cmd->req = skb;
235 cmd->mdaa_params = params;
236 cmd->cmd_cb = cmd_cb;
237 cmd->cb_context = cb_context;
238 INIT_LIST_HEAD(&cmd->queue);
239
240 mutex_lock(&ddev->cmd_lock);
241 list_add_tail(&cmd->queue, &ddev->cmd_queue);
242 mutex_unlock(&ddev->cmd_lock);
243
244 schedule_work(&ddev->cmd_work);
245
246 return 0;
247}
248
249int digital_in_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
250{
251 int rc;
252
253 rc = ddev->ops->in_configure_hw(ddev, type, param);
254 if (rc)
255 pr_err("in_configure_hw failed: %d\n", rc);
256
257 return rc;
258}
259
260int digital_tg_configure_hw(struct nfc_digital_dev *ddev, int type, int param)
261{
262 int rc;
263
264 rc = ddev->ops->tg_configure_hw(ddev, type, param);
265 if (rc)
266 pr_err("tg_configure_hw failed: %d\n", rc);
267
268 return rc;
269}
270
271static int digital_tg_listen_mdaa(struct nfc_digital_dev *ddev, u8 rf_tech)
272{
273 struct digital_tg_mdaa_params *params;
274
275 params = kzalloc(sizeof(struct digital_tg_mdaa_params), GFP_KERNEL);
276 if (!params)
277 return -ENOMEM;
278
279 params->sens_res = DIGITAL_SENS_RES_NFC_DEP;
280 get_random_bytes(params->nfcid1, sizeof(params->nfcid1));
281 params->sel_res = DIGITAL_SEL_RES_NFC_DEP;
282
283 params->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
284 params->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
285 get_random_bytes(params->nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
286 params->sc = DIGITAL_SENSF_FELICA_SC;
287
288 return digital_send_cmd(ddev, DIGITAL_CMD_TG_LISTEN_MDAA, NULL, params,
289 500, digital_tg_recv_atr_req, NULL);
290}
291
292int digital_target_found(struct nfc_digital_dev *ddev,
293 struct nfc_target *target, u8 protocol)
294{
295 int rc;
296 u8 framing;
297 u8 rf_tech;
298 int (*check_crc)(struct sk_buff *skb);
299 void (*add_crc)(struct sk_buff *skb);
300
301 rf_tech = ddev->poll_techs[ddev->poll_tech_index].rf_tech;
302
303 switch (protocol) {
304 case NFC_PROTO_JEWEL:
305 framing = NFC_DIGITAL_FRAMING_NFCA_T1T;
306 check_crc = digital_skb_check_crc_b;
307 add_crc = digital_skb_add_crc_b;
308 break;
309
310 case NFC_PROTO_MIFARE:
311 framing = NFC_DIGITAL_FRAMING_NFCA_T2T;
312 check_crc = digital_skb_check_crc_a;
313 add_crc = digital_skb_add_crc_a;
314 break;
315
316 case NFC_PROTO_FELICA:
317 framing = NFC_DIGITAL_FRAMING_NFCF_T3T;
318 check_crc = digital_skb_check_crc_f;
319 add_crc = digital_skb_add_crc_f;
320 break;
321
322 case NFC_PROTO_NFC_DEP:
323 if (rf_tech == NFC_DIGITAL_RF_TECH_106A) {
324 framing = NFC_DIGITAL_FRAMING_NFCA_NFC_DEP;
325 check_crc = digital_skb_check_crc_a;
326 add_crc = digital_skb_add_crc_a;
327 } else {
328 framing = NFC_DIGITAL_FRAMING_NFCF_NFC_DEP;
329 check_crc = digital_skb_check_crc_f;
330 add_crc = digital_skb_add_crc_f;
331 }
332 break;
333
334 default:
335 pr_err("Invalid protocol %d\n", protocol);
336 return -EINVAL;
337 }
338
339 pr_debug("rf_tech=%d, protocol=%d\n", rf_tech, protocol);
340
341 ddev->curr_rf_tech = rf_tech;
342 ddev->curr_protocol = protocol;
343
344 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
345 ddev->skb_add_crc = digital_skb_add_crc_none;
346 ddev->skb_check_crc = digital_skb_check_crc_none;
347 } else {
348 ddev->skb_add_crc = add_crc;
349 ddev->skb_check_crc = check_crc;
350 }
351
352 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING, framing);
353 if (rc)
354 return rc;
355
356 target->supported_protocols = (1 << protocol);
357 rc = nfc_targets_found(ddev->nfc_dev, target, 1);
358 if (rc)
359 return rc;
360
361 ddev->poll_tech_count = 0;
362
363 return 0;
364}
365
366void digital_poll_next_tech(struct nfc_digital_dev *ddev)
367{
368 digital_switch_rf(ddev, 0);
369
370 mutex_lock(&ddev->poll_lock);
371
372 if (!ddev->poll_tech_count) {
373 mutex_unlock(&ddev->poll_lock);
374 return;
375 }
376
377 ddev->poll_tech_index = (ddev->poll_tech_index + 1) %
378 ddev->poll_tech_count;
379
380 mutex_unlock(&ddev->poll_lock);
381
382 schedule_work(&ddev->poll_work);
383}
384
385static void digital_wq_poll(struct work_struct *work)
386{
387 int rc;
388 struct digital_poll_tech *poll_tech;
389 struct nfc_digital_dev *ddev = container_of(work,
390 struct nfc_digital_dev,
391 poll_work);
392 mutex_lock(&ddev->poll_lock);
393
394 if (!ddev->poll_tech_count) {
395 mutex_unlock(&ddev->poll_lock);
396 return;
397 }
398
399 poll_tech = &ddev->poll_techs[ddev->poll_tech_index];
400
401 mutex_unlock(&ddev->poll_lock);
402
403 rc = poll_tech->poll_func(ddev, poll_tech->rf_tech);
404 if (rc)
405 digital_poll_next_tech(ddev);
406}
407
408static void digital_add_poll_tech(struct nfc_digital_dev *ddev, u8 rf_tech,
409 digital_poll_t poll_func)
410{
411 struct digital_poll_tech *poll_tech;
412
413 if (ddev->poll_tech_count >= NFC_DIGITAL_POLL_MODE_COUNT_MAX)
414 return;
415
416 poll_tech = &ddev->poll_techs[ddev->poll_tech_count++];
417
418 poll_tech->rf_tech = rf_tech;
419 poll_tech->poll_func = poll_func;
420}
421
422/**
423 * start_poll operation
424 *
425 * For every supported protocol, the corresponding polling function is added
426 * to the table of polling technologies (ddev->poll_techs[]) using
427 * digital_add_poll_tech().
428 * When a polling function fails (by timeout or protocol error) the next one is
429 * schedule by digital_poll_next_tech() on the poll workqueue (ddev->poll_work).
430 */
431static int digital_start_poll(struct nfc_dev *nfc_dev, __u32 im_protocols,
432 __u32 tm_protocols)
433{
434 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
435 u32 matching_im_protocols, matching_tm_protocols;
436
437 pr_debug("protocols: im 0x%x, tm 0x%x, supported 0x%x\n", im_protocols,
438 tm_protocols, ddev->protocols);
439
440 matching_im_protocols = ddev->protocols & im_protocols;
441 matching_tm_protocols = ddev->protocols & tm_protocols;
442
443 if (!matching_im_protocols && !matching_tm_protocols) {
444 pr_err("Unknown protocol\n");
445 return -EINVAL;
446 }
447
448 if (ddev->poll_tech_count) {
449 pr_err("Already polling\n");
450 return -EBUSY;
451 }
452
453 if (ddev->curr_protocol) {
454 pr_err("A target is already active\n");
455 return -EBUSY;
456 }
457
458 ddev->poll_tech_count = 0;
459 ddev->poll_tech_index = 0;
460
461 if (matching_im_protocols & DIGITAL_PROTO_NFCA_RF_TECH)
462 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
463 digital_in_send_sens_req);
464
465 if (im_protocols & DIGITAL_PROTO_NFCF_RF_TECH) {
466 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
467 digital_in_send_sensf_req);
468
469 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
470 digital_in_send_sensf_req);
471 }
472
473 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
474 if (ddev->ops->tg_listen_mdaa) {
475 digital_add_poll_tech(ddev, 0,
476 digital_tg_listen_mdaa);
477 } else {
478 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_106A,
479 digital_tg_listen_nfca);
480
481 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_212F,
482 digital_tg_listen_nfcf);
483
484 digital_add_poll_tech(ddev, NFC_DIGITAL_RF_TECH_424F,
485 digital_tg_listen_nfcf);
486 }
487 }
488
489 if (!ddev->poll_tech_count) {
490 pr_err("Unsupported protocols: im=0x%x, tm=0x%x\n",
491 matching_im_protocols, matching_tm_protocols);
492 return -EINVAL;
493 }
494
495 schedule_work(&ddev->poll_work);
496
497 return 0;
498}
499
500static void digital_stop_poll(struct nfc_dev *nfc_dev)
501{
502 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
503
504 mutex_lock(&ddev->poll_lock);
505
506 if (!ddev->poll_tech_count) {
507 pr_err("Polling operation was not running\n");
508 mutex_unlock(&ddev->poll_lock);
509 return;
510 }
511
512 ddev->poll_tech_count = 0;
513
514 mutex_unlock(&ddev->poll_lock);
515
516 cancel_work_sync(&ddev->poll_work);
517
518 digital_abort_cmd(ddev);
519}
520
521static int digital_dev_up(struct nfc_dev *nfc_dev)
522{
523 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
524
525 digital_switch_rf(ddev, 1);
526
527 return 0;
528}
529
530static int digital_dev_down(struct nfc_dev *nfc_dev)
531{
532 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
533
534 digital_switch_rf(ddev, 0);
535
536 return 0;
537}
538
539static int digital_dep_link_up(struct nfc_dev *nfc_dev,
540 struct nfc_target *target,
541 __u8 comm_mode, __u8 *gb, size_t gb_len)
542{
543 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
544
545 return digital_in_send_atr_req(ddev, target, comm_mode, gb, gb_len);
546}
547
548static int digital_dep_link_down(struct nfc_dev *nfc_dev)
549{
550 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
551
552 ddev->curr_protocol = 0;
553
554 return 0;
555}
556
557static int digital_activate_target(struct nfc_dev *nfc_dev,
558 struct nfc_target *target, __u32 protocol)
559{
560 return 0;
561}
562
563static void digital_deactivate_target(struct nfc_dev *nfc_dev,
564 struct nfc_target *target)
565{
566 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
567
568 ddev->curr_protocol = 0;
569}
570
571static int digital_tg_send(struct nfc_dev *dev, struct sk_buff *skb)
572{
573 struct nfc_digital_dev *ddev = nfc_get_drvdata(dev);
574
575 return digital_tg_send_dep_res(ddev, skb);
576}
577
578static void digital_in_send_complete(struct nfc_digital_dev *ddev, void *arg,
579 struct sk_buff *resp)
580{
581 struct digital_data_exch *data_exch = arg;
582 int rc;
583
584 if (IS_ERR(resp)) {
585 rc = PTR_ERR(resp);
586 goto done;
587 }
588
589 if (ddev->curr_protocol == NFC_PROTO_MIFARE)
590 rc = digital_in_recv_mifare_res(resp);
591 else
592 rc = ddev->skb_check_crc(resp);
593
594 if (rc) {
595 kfree_skb(resp);
596 resp = NULL;
597 }
598
599done:
600 data_exch->cb(data_exch->cb_context, resp, rc);
601
602 kfree(data_exch);
603}
604
605static int digital_in_send(struct nfc_dev *nfc_dev, struct nfc_target *target,
606 struct sk_buff *skb, data_exchange_cb_t cb,
607 void *cb_context)
608{
609 struct nfc_digital_dev *ddev = nfc_get_drvdata(nfc_dev);
610 struct digital_data_exch *data_exch;
611
612 data_exch = kzalloc(sizeof(struct digital_data_exch), GFP_KERNEL);
613 if (!data_exch) {
614 pr_err("Failed to allocate data_exch struct\n");
615 return -ENOMEM;
616 }
617
618 data_exch->cb = cb;
619 data_exch->cb_context = cb_context;
620
621 if (ddev->curr_protocol == NFC_PROTO_NFC_DEP)
622 return digital_in_send_dep_req(ddev, target, skb, data_exch);
623
624 ddev->skb_add_crc(skb);
625
626 return digital_in_send_cmd(ddev, skb, 500, digital_in_send_complete,
627 data_exch);
628}
629
630static struct nfc_ops digital_nfc_ops = {
631 .dev_up = digital_dev_up,
632 .dev_down = digital_dev_down,
633 .start_poll = digital_start_poll,
634 .stop_poll = digital_stop_poll,
635 .dep_link_up = digital_dep_link_up,
636 .dep_link_down = digital_dep_link_down,
637 .activate_target = digital_activate_target,
638 .deactivate_target = digital_deactivate_target,
639 .tm_send = digital_tg_send,
640 .im_transceive = digital_in_send,
641};
642
643struct nfc_digital_dev *nfc_digital_allocate_device(struct nfc_digital_ops *ops,
644 __u32 supported_protocols,
645 __u32 driver_capabilities,
646 int tx_headroom, int tx_tailroom)
647{
648 struct nfc_digital_dev *ddev;
649
650 if (!ops->in_configure_hw || !ops->in_send_cmd || !ops->tg_listen ||
651 !ops->tg_configure_hw || !ops->tg_send_cmd || !ops->abort_cmd ||
652 !ops->switch_rf)
653 return NULL;
654
655 ddev = kzalloc(sizeof(struct nfc_digital_dev), GFP_KERNEL);
656 if (!ddev)
657 return NULL;
658
659 ddev->driver_capabilities = driver_capabilities;
660 ddev->ops = ops;
661
662 mutex_init(&ddev->cmd_lock);
663 INIT_LIST_HEAD(&ddev->cmd_queue);
664
665 INIT_WORK(&ddev->cmd_work, digital_wq_cmd);
666 INIT_WORK(&ddev->cmd_complete_work, digital_wq_cmd_complete);
667
668 mutex_init(&ddev->poll_lock);
669 INIT_WORK(&ddev->poll_work, digital_wq_poll);
670
671 if (supported_protocols & NFC_PROTO_JEWEL_MASK)
672 ddev->protocols |= NFC_PROTO_JEWEL_MASK;
673 if (supported_protocols & NFC_PROTO_MIFARE_MASK)
674 ddev->protocols |= NFC_PROTO_MIFARE_MASK;
675 if (supported_protocols & NFC_PROTO_FELICA_MASK)
676 ddev->protocols |= NFC_PROTO_FELICA_MASK;
677 if (supported_protocols & NFC_PROTO_NFC_DEP_MASK)
678 ddev->protocols |= NFC_PROTO_NFC_DEP_MASK;
679
680 ddev->tx_headroom = tx_headroom + DIGITAL_MAX_HEADER_LEN;
681 ddev->tx_tailroom = tx_tailroom + DIGITAL_CRC_LEN;
682
683 ddev->nfc_dev = nfc_allocate_device(&digital_nfc_ops, ddev->protocols,
684 ddev->tx_headroom,
685 ddev->tx_tailroom);
686 if (!ddev->nfc_dev) {
687 pr_err("nfc_allocate_device failed\n");
688 goto free_dev;
689 }
690
691 nfc_set_drvdata(ddev->nfc_dev, ddev);
692
693 return ddev;
694
695free_dev:
696 kfree(ddev);
697
698 return NULL;
699}
700EXPORT_SYMBOL(nfc_digital_allocate_device);
701
702void nfc_digital_free_device(struct nfc_digital_dev *ddev)
703{
704 nfc_free_device(ddev->nfc_dev);
705 kfree(ddev);
706}
707EXPORT_SYMBOL(nfc_digital_free_device);
708
709int nfc_digital_register_device(struct nfc_digital_dev *ddev)
710{
711 return nfc_register_device(ddev->nfc_dev);
712}
713EXPORT_SYMBOL(nfc_digital_register_device);
714
715void nfc_digital_unregister_device(struct nfc_digital_dev *ddev)
716{
717 struct digital_cmd *cmd, *n;
718
719 nfc_unregister_device(ddev->nfc_dev);
720
721 mutex_lock(&ddev->poll_lock);
722 ddev->poll_tech_count = 0;
723 mutex_unlock(&ddev->poll_lock);
724
725 cancel_work_sync(&ddev->poll_work);
726 cancel_work_sync(&ddev->cmd_work);
727 cancel_work_sync(&ddev->cmd_complete_work);
728
729 list_for_each_entry_safe(cmd, n, &ddev->cmd_queue, queue) {
730 list_del(&cmd->queue);
731 kfree(cmd->mdaa_params);
732 kfree(cmd);
733 }
734}
735EXPORT_SYMBOL(nfc_digital_unregister_device);
736
737MODULE_LICENSE("GPL");
diff --git a/net/nfc/digital_dep.c b/net/nfc/digital_dep.c
new file mode 100644
index 000000000000..07bbc24fb4c7
--- /dev/null
+++ b/net/nfc/digital_dep.c
@@ -0,0 +1,729 @@
1/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18#include "digital.h"
19
20#define DIGITAL_NFC_DEP_FRAME_DIR_OUT 0xD4
21#define DIGITAL_NFC_DEP_FRAME_DIR_IN 0xD5
22
23#define DIGITAL_NFC_DEP_NFCA_SOD_SB 0xF0
24
25#define DIGITAL_CMD_ATR_REQ 0x00
26#define DIGITAL_CMD_ATR_RES 0x01
27#define DIGITAL_CMD_PSL_REQ 0x04
28#define DIGITAL_CMD_PSL_RES 0x05
29#define DIGITAL_CMD_DEP_REQ 0x06
30#define DIGITAL_CMD_DEP_RES 0x07
31
32#define DIGITAL_ATR_REQ_MIN_SIZE 16
33#define DIGITAL_ATR_REQ_MAX_SIZE 64
34
35#define DIGITAL_NFCID3_LEN ((u8)8)
36#define DIGITAL_LR_BITS_PAYLOAD_SIZE_254B 0x30
37#define DIGITAL_GB_BIT 0x02
38
39#define DIGITAL_NFC_DEP_PFB_TYPE(pfb) ((pfb) & 0xE0)
40
41#define DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT 0x10
42
43#define DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb) \
44 ((pfb) & DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT)
45#define DIGITAL_NFC_DEP_MI_BIT_SET(pfb) ((pfb) & 0x10)
46#define DIGITAL_NFC_DEP_NAD_BIT_SET(pfb) ((pfb) & 0x08)
47#define DIGITAL_NFC_DEP_DID_BIT_SET(pfb) ((pfb) & 0x04)
48#define DIGITAL_NFC_DEP_PFB_PNI(pfb) ((pfb) & 0x03)
49
50#define DIGITAL_NFC_DEP_PFB_I_PDU 0x00
51#define DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU 0x40
52#define DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU 0x80
53
54struct digital_atr_req {
55 u8 dir;
56 u8 cmd;
57 u8 nfcid3[10];
58 u8 did;
59 u8 bs;
60 u8 br;
61 u8 pp;
62 u8 gb[0];
63} __packed;
64
65struct digital_atr_res {
66 u8 dir;
67 u8 cmd;
68 u8 nfcid3[10];
69 u8 did;
70 u8 bs;
71 u8 br;
72 u8 to;
73 u8 pp;
74 u8 gb[0];
75} __packed;
76
77struct digital_psl_req {
78 u8 dir;
79 u8 cmd;
80 u8 did;
81 u8 brs;
82 u8 fsl;
83} __packed;
84
85struct digital_psl_res {
86 u8 dir;
87 u8 cmd;
88 u8 did;
89} __packed;
90
91struct digital_dep_req_res {
92 u8 dir;
93 u8 cmd;
94 u8 pfb;
95} __packed;
96
97static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
98 struct sk_buff *resp);
99
100static void digital_skb_push_dep_sod(struct nfc_digital_dev *ddev,
101 struct sk_buff *skb)
102{
103 skb_push(skb, sizeof(u8));
104
105 skb->data[0] = skb->len;
106
107 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
108 *skb_push(skb, sizeof(u8)) = DIGITAL_NFC_DEP_NFCA_SOD_SB;
109}
110
111static int digital_skb_pull_dep_sod(struct nfc_digital_dev *ddev,
112 struct sk_buff *skb)
113{
114 u8 size;
115
116 if (skb->len < 2)
117 return -EIO;
118
119 if (ddev->curr_rf_tech == NFC_DIGITAL_RF_TECH_106A)
120 skb_pull(skb, sizeof(u8));
121
122 size = skb->data[0];
123 if (size != skb->len)
124 return -EIO;
125
126 skb_pull(skb, sizeof(u8));
127
128 return 0;
129}
130
131static void digital_in_recv_atr_res(struct nfc_digital_dev *ddev, void *arg,
132 struct sk_buff *resp)
133{
134 struct nfc_target *target = arg;
135 struct digital_atr_res *atr_res;
136 u8 gb_len;
137 int rc;
138
139 if (IS_ERR(resp)) {
140 rc = PTR_ERR(resp);
141 resp = NULL;
142 goto exit;
143 }
144
145 rc = ddev->skb_check_crc(resp);
146 if (rc) {
147 PROTOCOL_ERR("14.4.1.6");
148 goto exit;
149 }
150
151 rc = digital_skb_pull_dep_sod(ddev, resp);
152 if (rc) {
153 PROTOCOL_ERR("14.4.1.2");
154 goto exit;
155 }
156
157 if (resp->len < sizeof(struct digital_atr_res)) {
158 rc = -EIO;
159 goto exit;
160 }
161
162 gb_len = resp->len - sizeof(struct digital_atr_res);
163
164 atr_res = (struct digital_atr_res *)resp->data;
165
166 rc = nfc_set_remote_general_bytes(ddev->nfc_dev, atr_res->gb, gb_len);
167 if (rc)
168 goto exit;
169
170 rc = nfc_dep_link_is_up(ddev->nfc_dev, target->idx, NFC_COMM_ACTIVE,
171 NFC_RF_INITIATOR);
172
173 ddev->curr_nfc_dep_pni = 0;
174
175exit:
176 dev_kfree_skb(resp);
177
178 if (rc)
179 ddev->curr_protocol = 0;
180}
181
182int digital_in_send_atr_req(struct nfc_digital_dev *ddev,
183 struct nfc_target *target, __u8 comm_mode, __u8 *gb,
184 size_t gb_len)
185{
186 struct sk_buff *skb;
187 struct digital_atr_req *atr_req;
188 uint size;
189
190 size = DIGITAL_ATR_REQ_MIN_SIZE + gb_len;
191
192 if (size > DIGITAL_ATR_REQ_MAX_SIZE) {
193 PROTOCOL_ERR("14.6.1.1");
194 return -EINVAL;
195 }
196
197 skb = digital_skb_alloc(ddev, size);
198 if (!skb)
199 return -ENOMEM;
200
201 skb_put(skb, sizeof(struct digital_atr_req));
202
203 atr_req = (struct digital_atr_req *)skb->data;
204 memset(atr_req, 0, sizeof(struct digital_atr_req));
205
206 atr_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
207 atr_req->cmd = DIGITAL_CMD_ATR_REQ;
208 if (target->nfcid2_len)
209 memcpy(atr_req->nfcid3, target->nfcid2,
210 max(target->nfcid2_len, DIGITAL_NFCID3_LEN));
211 else
212 get_random_bytes(atr_req->nfcid3, DIGITAL_NFCID3_LEN);
213
214 atr_req->did = 0;
215 atr_req->bs = 0;
216 atr_req->br = 0;
217
218 atr_req->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
219
220 if (gb_len) {
221 atr_req->pp |= DIGITAL_GB_BIT;
222 memcpy(skb_put(skb, gb_len), gb, gb_len);
223 }
224
225 digital_skb_push_dep_sod(ddev, skb);
226
227 ddev->skb_add_crc(skb);
228
229 digital_in_send_cmd(ddev, skb, 500, digital_in_recv_atr_res, target);
230
231 return 0;
232}
233
234static int digital_in_send_rtox(struct nfc_digital_dev *ddev,
235 struct digital_data_exch *data_exch, u8 rtox)
236{
237 struct digital_dep_req_res *dep_req;
238 struct sk_buff *skb;
239 int rc;
240
241 skb = digital_skb_alloc(ddev, 1);
242 if (!skb)
243 return -ENOMEM;
244
245 *skb_put(skb, 1) = rtox;
246
247 skb_push(skb, sizeof(struct digital_dep_req_res));
248
249 dep_req = (struct digital_dep_req_res *)skb->data;
250
251 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
252 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
253 dep_req->pfb = DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU |
254 DIGITAL_NFC_DEP_PFB_TIMEOUT_BIT;
255
256 digital_skb_push_dep_sod(ddev, skb);
257
258 ddev->skb_add_crc(skb);
259
260 rc = digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
261 data_exch);
262
263 return rc;
264}
265
266static void digital_in_recv_dep_res(struct nfc_digital_dev *ddev, void *arg,
267 struct sk_buff *resp)
268{
269 struct digital_data_exch *data_exch = arg;
270 struct digital_dep_req_res *dep_res;
271 u8 pfb;
272 uint size;
273 int rc;
274
275 if (IS_ERR(resp)) {
276 rc = PTR_ERR(resp);
277 resp = NULL;
278 goto exit;
279 }
280
281 rc = ddev->skb_check_crc(resp);
282 if (rc) {
283 PROTOCOL_ERR("14.4.1.6");
284 goto error;
285 }
286
287 rc = digital_skb_pull_dep_sod(ddev, resp);
288 if (rc) {
289 PROTOCOL_ERR("14.4.1.2");
290 goto exit;
291 }
292
293 dep_res = (struct digital_dep_req_res *)resp->data;
294
295 if (resp->len < sizeof(struct digital_dep_req_res) ||
296 dep_res->dir != DIGITAL_NFC_DEP_FRAME_DIR_IN ||
297 dep_res->cmd != DIGITAL_CMD_DEP_RES) {
298 rc = -EIO;
299 goto error;
300 }
301
302 pfb = dep_res->pfb;
303
304 switch (DIGITAL_NFC_DEP_PFB_TYPE(pfb)) {
305 case DIGITAL_NFC_DEP_PFB_I_PDU:
306 if (DIGITAL_NFC_DEP_PFB_PNI(pfb) != ddev->curr_nfc_dep_pni) {
307 PROTOCOL_ERR("14.12.3.3");
308 rc = -EIO;
309 goto error;
310 }
311
312 ddev->curr_nfc_dep_pni =
313 DIGITAL_NFC_DEP_PFB_PNI(ddev->curr_nfc_dep_pni + 1);
314 rc = 0;
315 break;
316
317 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
318 pr_err("Received a ACK/NACK PDU\n");
319 rc = -EIO;
320 goto error;
321
322 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
323 if (!DIGITAL_NFC_DEP_PFB_IS_TIMEOUT(pfb)) {
324 rc = -EINVAL;
325 goto error;
326 }
327
328 rc = digital_in_send_rtox(ddev, data_exch, resp->data[3]);
329 if (rc)
330 goto error;
331
332 kfree_skb(resp);
333 return;
334 }
335
336 if (DIGITAL_NFC_DEP_MI_BIT_SET(pfb)) {
337 pr_err("MI bit set. Chained PDU not supported\n");
338 rc = -EIO;
339 goto error;
340 }
341
342 size = sizeof(struct digital_dep_req_res);
343
344 if (DIGITAL_NFC_DEP_DID_BIT_SET(pfb))
345 size++;
346
347 if (size > resp->len) {
348 rc = -EIO;
349 goto error;
350 }
351
352 skb_pull(resp, size);
353
354exit:
355 data_exch->cb(data_exch->cb_context, resp, rc);
356
357error:
358 kfree(data_exch);
359
360 if (rc)
361 kfree_skb(resp);
362}
363
364int digital_in_send_dep_req(struct nfc_digital_dev *ddev,
365 struct nfc_target *target, struct sk_buff *skb,
366 struct digital_data_exch *data_exch)
367{
368 struct digital_dep_req_res *dep_req;
369
370 skb_push(skb, sizeof(struct digital_dep_req_res));
371
372 dep_req = (struct digital_dep_req_res *)skb->data;
373 dep_req->dir = DIGITAL_NFC_DEP_FRAME_DIR_OUT;
374 dep_req->cmd = DIGITAL_CMD_DEP_REQ;
375 dep_req->pfb = ddev->curr_nfc_dep_pni;
376
377 digital_skb_push_dep_sod(ddev, skb);
378
379 ddev->skb_add_crc(skb);
380
381 return digital_in_send_cmd(ddev, skb, 1500, digital_in_recv_dep_res,
382 data_exch);
383}
384
385static void digital_tg_recv_dep_req(struct nfc_digital_dev *ddev, void *arg,
386 struct sk_buff *resp)
387{
388 int rc;
389 struct digital_dep_req_res *dep_req;
390 size_t size;
391
392 if (IS_ERR(resp)) {
393 rc = PTR_ERR(resp);
394 resp = NULL;
395 goto exit;
396 }
397
398 rc = ddev->skb_check_crc(resp);
399 if (rc) {
400 PROTOCOL_ERR("14.4.1.6");
401 goto exit;
402 }
403
404 rc = digital_skb_pull_dep_sod(ddev, resp);
405 if (rc) {
406 PROTOCOL_ERR("14.4.1.2");
407 goto exit;
408 }
409
410 size = sizeof(struct digital_dep_req_res);
411 dep_req = (struct digital_dep_req_res *)resp->data;
412
413 if (resp->len < size || dep_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
414 dep_req->cmd != DIGITAL_CMD_DEP_REQ) {
415 rc = -EIO;
416 goto exit;
417 }
418
419 if (DIGITAL_NFC_DEP_DID_BIT_SET(dep_req->pfb))
420 size++;
421
422 if (resp->len < size) {
423 rc = -EIO;
424 goto exit;
425 }
426
427 switch (DIGITAL_NFC_DEP_PFB_TYPE(dep_req->pfb)) {
428 case DIGITAL_NFC_DEP_PFB_I_PDU:
429 pr_debug("DIGITAL_NFC_DEP_PFB_I_PDU\n");
430 ddev->curr_nfc_dep_pni = DIGITAL_NFC_DEP_PFB_PNI(dep_req->pfb);
431 break;
432 case DIGITAL_NFC_DEP_PFB_ACK_NACK_PDU:
433 pr_err("Received a ACK/NACK PDU\n");
434 rc = -EINVAL;
435 goto exit;
436 break;
437 case DIGITAL_NFC_DEP_PFB_SUPERVISOR_PDU:
438 pr_err("Received a SUPERVISOR PDU\n");
439 rc = -EINVAL;
440 goto exit;
441 break;
442 }
443
444 skb_pull(resp, size);
445
446 rc = nfc_tm_data_received(ddev->nfc_dev, resp);
447
448exit:
449 if (rc)
450 kfree_skb(resp);
451}
452
453int digital_tg_send_dep_res(struct nfc_digital_dev *ddev, struct sk_buff *skb)
454{
455 struct digital_dep_req_res *dep_res;
456
457 skb_push(skb, sizeof(struct digital_dep_req_res));
458 dep_res = (struct digital_dep_req_res *)skb->data;
459
460 dep_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
461 dep_res->cmd = DIGITAL_CMD_DEP_RES;
462 dep_res->pfb = ddev->curr_nfc_dep_pni;
463
464 digital_skb_push_dep_sod(ddev, skb);
465
466 ddev->skb_add_crc(skb);
467
468 return digital_tg_send_cmd(ddev, skb, 1500, digital_tg_recv_dep_req,
469 NULL);
470}
471
472static void digital_tg_send_psl_res_complete(struct nfc_digital_dev *ddev,
473 void *arg, struct sk_buff *resp)
474{
475 u8 rf_tech = PTR_ERR(arg);
476
477 if (IS_ERR(resp))
478 return;
479
480 digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
481
482 digital_tg_listen(ddev, 1500, digital_tg_recv_dep_req, NULL);
483
484 dev_kfree_skb(resp);
485}
486
487static int digital_tg_send_psl_res(struct nfc_digital_dev *ddev, u8 did,
488 u8 rf_tech)
489{
490 struct digital_psl_res *psl_res;
491 struct sk_buff *skb;
492 int rc;
493
494 skb = digital_skb_alloc(ddev, sizeof(struct digital_psl_res));
495 if (!skb)
496 return -ENOMEM;
497
498 skb_put(skb, sizeof(struct digital_psl_res));
499
500 psl_res = (struct digital_psl_res *)skb->data;
501
502 psl_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
503 psl_res->cmd = DIGITAL_CMD_PSL_RES;
504 psl_res->did = did;
505
506 digital_skb_push_dep_sod(ddev, skb);
507
508 ddev->skb_add_crc(skb);
509
510 rc = digital_tg_send_cmd(ddev, skb, 0, digital_tg_send_psl_res_complete,
511 ERR_PTR(rf_tech));
512
513 if (rc)
514 kfree_skb(skb);
515
516 return rc;
517}
518
519static void digital_tg_recv_psl_req(struct nfc_digital_dev *ddev, void *arg,
520 struct sk_buff *resp)
521{
522 int rc;
523 struct digital_psl_req *psl_req;
524 u8 rf_tech;
525 u8 dsi;
526
527 if (IS_ERR(resp)) {
528 rc = PTR_ERR(resp);
529 resp = NULL;
530 goto exit;
531 }
532
533 rc = ddev->skb_check_crc(resp);
534 if (rc) {
535 PROTOCOL_ERR("14.4.1.6");
536 goto exit;
537 }
538
539 rc = digital_skb_pull_dep_sod(ddev, resp);
540 if (rc) {
541 PROTOCOL_ERR("14.4.1.2");
542 goto exit;
543 }
544
545 psl_req = (struct digital_psl_req *)resp->data;
546
547 if (resp->len != sizeof(struct digital_psl_req) ||
548 psl_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
549 psl_req->cmd != DIGITAL_CMD_PSL_REQ) {
550 rc = -EIO;
551 goto exit;
552 }
553
554 dsi = (psl_req->brs >> 3) & 0x07;
555 switch (dsi) {
556 case 0:
557 rf_tech = NFC_DIGITAL_RF_TECH_106A;
558 break;
559 case 1:
560 rf_tech = NFC_DIGITAL_RF_TECH_212F;
561 break;
562 case 2:
563 rf_tech = NFC_DIGITAL_RF_TECH_424F;
564 break;
565 default:
566 pr_err("Unsuported dsi value %d\n", dsi);
567 goto exit;
568 }
569
570 rc = digital_tg_send_psl_res(ddev, psl_req->did, rf_tech);
571
572exit:
573 kfree_skb(resp);
574}
575
576static void digital_tg_send_atr_res_complete(struct nfc_digital_dev *ddev,
577 void *arg, struct sk_buff *resp)
578{
579 int offset;
580
581 if (IS_ERR(resp)) {
582 digital_poll_next_tech(ddev);
583 return;
584 }
585
586 offset = 2;
587 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB)
588 offset++;
589
590 if (resp->data[offset] == DIGITAL_CMD_PSL_REQ)
591 digital_tg_recv_psl_req(ddev, arg, resp);
592 else
593 digital_tg_recv_dep_req(ddev, arg, resp);
594}
595
596static int digital_tg_send_atr_res(struct nfc_digital_dev *ddev,
597 struct digital_atr_req *atr_req)
598{
599 struct digital_atr_res *atr_res;
600 struct sk_buff *skb;
601 u8 *gb;
602 size_t gb_len;
603 int rc;
604
605 gb = nfc_get_local_general_bytes(ddev->nfc_dev, &gb_len);
606 if (!gb)
607 gb_len = 0;
608
609 skb = digital_skb_alloc(ddev, sizeof(struct digital_atr_res) + gb_len);
610 if (!skb)
611 return -ENOMEM;
612
613 skb_put(skb, sizeof(struct digital_atr_res));
614 atr_res = (struct digital_atr_res *)skb->data;
615
616 memset(atr_res, 0, sizeof(struct digital_atr_res));
617
618 atr_res->dir = DIGITAL_NFC_DEP_FRAME_DIR_IN;
619 atr_res->cmd = DIGITAL_CMD_ATR_RES;
620 memcpy(atr_res->nfcid3, atr_req->nfcid3, sizeof(atr_req->nfcid3));
621 atr_res->to = 8;
622 atr_res->pp = DIGITAL_LR_BITS_PAYLOAD_SIZE_254B;
623 if (gb_len) {
624 skb_put(skb, gb_len);
625
626 atr_res->pp |= DIGITAL_GB_BIT;
627 memcpy(atr_res->gb, gb, gb_len);
628 }
629
630 digital_skb_push_dep_sod(ddev, skb);
631
632 ddev->skb_add_crc(skb);
633
634 rc = digital_tg_send_cmd(ddev, skb, 999,
635 digital_tg_send_atr_res_complete, NULL);
636 if (rc) {
637 kfree_skb(skb);
638 return rc;
639 }
640
641 return rc;
642}
643
644void digital_tg_recv_atr_req(struct nfc_digital_dev *ddev, void *arg,
645 struct sk_buff *resp)
646{
647 int rc;
648 struct digital_atr_req *atr_req;
649 size_t gb_len, min_size;
650
651 if (IS_ERR(resp)) {
652 rc = PTR_ERR(resp);
653 resp = NULL;
654 goto exit;
655 }
656
657 if (!resp->len) {
658 rc = -EIO;
659 goto exit;
660 }
661
662 if (resp->data[0] == DIGITAL_NFC_DEP_NFCA_SOD_SB) {
663 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 2;
664
665 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_106A;
666 ddev->skb_add_crc = digital_skb_add_crc_a;
667 ddev->skb_check_crc = digital_skb_check_crc_a;
668 } else {
669 min_size = DIGITAL_ATR_REQ_MIN_SIZE + 1;
670
671 ddev->curr_rf_tech = NFC_DIGITAL_RF_TECH_212F;
672 ddev->skb_add_crc = digital_skb_add_crc_f;
673 ddev->skb_check_crc = digital_skb_check_crc_f;
674 }
675
676 if (resp->len < min_size) {
677 rc = -EIO;
678 goto exit;
679 }
680
681 if (DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
682 ddev->skb_add_crc = digital_skb_add_crc_none;
683 ddev->skb_check_crc = digital_skb_check_crc_none;
684 }
685
686 rc = ddev->skb_check_crc(resp);
687 if (rc) {
688 PROTOCOL_ERR("14.4.1.6");
689 goto exit;
690 }
691
692 rc = digital_skb_pull_dep_sod(ddev, resp);
693 if (rc) {
694 PROTOCOL_ERR("14.4.1.2");
695 goto exit;
696 }
697
698 atr_req = (struct digital_atr_req *)resp->data;
699
700 if (atr_req->dir != DIGITAL_NFC_DEP_FRAME_DIR_OUT ||
701 atr_req->cmd != DIGITAL_CMD_ATR_REQ) {
702 rc = -EINVAL;
703 goto exit;
704 }
705
706 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
707 NFC_DIGITAL_FRAMING_NFC_DEP_ACTIVATED);
708 if (rc)
709 goto exit;
710
711 rc = digital_tg_send_atr_res(ddev, atr_req);
712 if (rc)
713 goto exit;
714
715 gb_len = resp->len - sizeof(struct digital_atr_req);
716 rc = nfc_tm_activated(ddev->nfc_dev, NFC_PROTO_NFC_DEP_MASK,
717 NFC_COMM_PASSIVE, atr_req->gb, gb_len);
718 if (rc)
719 goto exit;
720
721 ddev->poll_tech_count = 0;
722
723 rc = 0;
724exit:
725 if (rc)
726 digital_poll_next_tech(ddev);
727
728 dev_kfree_skb(resp);
729}
diff --git a/net/nfc/digital_technology.c b/net/nfc/digital_technology.c
new file mode 100644
index 000000000000..251c8c753ebe
--- /dev/null
+++ b/net/nfc/digital_technology.c
@@ -0,0 +1,770 @@
1/*
2 * NFC Digital Protocol stack
3 * Copyright (c) 2013, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#define pr_fmt(fmt) "digital: %s: " fmt, __func__
17
18#include "digital.h"
19
20#define DIGITAL_CMD_SENS_REQ 0x26
21#define DIGITAL_CMD_ALL_REQ 0x52
22#define DIGITAL_CMD_SEL_REQ_CL1 0x93
23#define DIGITAL_CMD_SEL_REQ_CL2 0x95
24#define DIGITAL_CMD_SEL_REQ_CL3 0x97
25
26#define DIGITAL_SDD_REQ_SEL_PAR 0x20
27
28#define DIGITAL_SDD_RES_CT 0x88
29#define DIGITAL_SDD_RES_LEN 5
30
31#define DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res) (!((sel_res) & 0x04))
32#define DIGITAL_SEL_RES_IS_T2T(sel_res) (!((sel_res) & 0x60))
33#define DIGITAL_SEL_RES_IS_NFC_DEP(sel_res) ((sel_res) & 0x40)
34
35#define DIGITAL_SENS_RES_IS_T1T(sens_res) (((sens_res) & 0x0C00) == 0x0C00)
36#define DIGITAL_SENS_RES_IS_VALID(sens_res) \
37 ((!((sens_res) & 0x001F) && (((sens_res) & 0x0C00) == 0x0C00)) || \
38 (((sens_res) & 0x001F) && ((sens_res) & 0x0C00) != 0x0C00))
39
40#define DIGITAL_MIFARE_READ_RES_LEN 16
41#define DIGITAL_MIFARE_ACK_RES 0x0A
42
43#define DIGITAL_CMD_SENSF_REQ 0x00
44#define DIGITAL_CMD_SENSF_RES 0x01
45
46#define DIGITAL_SENSF_RES_MIN_LENGTH 17
47#define DIGITAL_SENSF_RES_RD_AP_B1 0x00
48#define DIGITAL_SENSF_RES_RD_AP_B2 0x8F
49
50#define DIGITAL_SENSF_REQ_RC_NONE 0
51#define DIGITAL_SENSF_REQ_RC_SC 1
52#define DIGITAL_SENSF_REQ_RC_AP 2
53
54struct digital_sdd_res {
55 u8 nfcid1[4];
56 u8 bcc;
57} __packed;
58
59struct digital_sel_req {
60 u8 sel_cmd;
61 u8 b2;
62 u8 nfcid1[4];
63 u8 bcc;
64} __packed;
65
66struct digital_sensf_req {
67 u8 cmd;
68 u8 sc1;
69 u8 sc2;
70 u8 rc;
71 u8 tsn;
72} __packed;
73
74struct digital_sensf_res {
75 u8 cmd;
76 u8 nfcid2[8];
77 u8 pad0[2];
78 u8 pad1[3];
79 u8 mrti_check;
80 u8 mrti_update;
81 u8 pad2;
82 u8 rd[2];
83} __packed;
84
85static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
86 struct nfc_target *target);
87
88static void digital_in_recv_sel_res(struct nfc_digital_dev *ddev, void *arg,
89 struct sk_buff *resp)
90{
91 struct nfc_target *target = arg;
92 int rc;
93 u8 sel_res;
94 u8 nfc_proto;
95
96 if (IS_ERR(resp)) {
97 rc = PTR_ERR(resp);
98 resp = NULL;
99 goto exit;
100 }
101
102 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
103 rc = digital_skb_check_crc_a(resp);
104 if (rc) {
105 PROTOCOL_ERR("4.4.1.3");
106 goto exit;
107 }
108 }
109
110 if (!resp->len) {
111 rc = -EIO;
112 goto exit;
113 }
114
115 sel_res = resp->data[0];
116
117 if (!DIGITAL_SEL_RES_NFCID1_COMPLETE(sel_res)) {
118 rc = digital_in_send_sdd_req(ddev, target);
119 if (rc)
120 goto exit;
121
122 goto exit_free_skb;
123 }
124
125 if (DIGITAL_SEL_RES_IS_T2T(sel_res)) {
126 nfc_proto = NFC_PROTO_MIFARE;
127 } else if (DIGITAL_SEL_RES_IS_NFC_DEP(sel_res)) {
128 nfc_proto = NFC_PROTO_NFC_DEP;
129 } else {
130 rc = -EOPNOTSUPP;
131 goto exit;
132 }
133
134 target->sel_res = sel_res;
135
136 rc = digital_target_found(ddev, target, nfc_proto);
137
138exit:
139 kfree(target);
140
141exit_free_skb:
142 dev_kfree_skb(resp);
143
144 if (rc)
145 digital_poll_next_tech(ddev);
146}
147
148static int digital_in_send_sel_req(struct nfc_digital_dev *ddev,
149 struct nfc_target *target,
150 struct digital_sdd_res *sdd_res)
151{
152 struct sk_buff *skb;
153 struct digital_sel_req *sel_req;
154 u8 sel_cmd;
155 int rc;
156
157 skb = digital_skb_alloc(ddev, sizeof(struct digital_sel_req));
158 if (!skb)
159 return -ENOMEM;
160
161 skb_put(skb, sizeof(struct digital_sel_req));
162 sel_req = (struct digital_sel_req *)skb->data;
163
164 if (target->nfcid1_len <= 4)
165 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
166 else if (target->nfcid1_len < 10)
167 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
168 else
169 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
170
171 sel_req->sel_cmd = sel_cmd;
172 sel_req->b2 = 0x70;
173 memcpy(sel_req->nfcid1, sdd_res->nfcid1, 4);
174 sel_req->bcc = sdd_res->bcc;
175
176 if (DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
177 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
178 NFC_DIGITAL_FRAMING_NFCA_STANDARD_WITH_CRC_A);
179 if (rc)
180 goto exit;
181 } else {
182 digital_skb_add_crc_a(skb);
183 }
184
185 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sel_res,
186 target);
187exit:
188 if (rc)
189 kfree_skb(skb);
190
191 return rc;
192}
193
194static void digital_in_recv_sdd_res(struct nfc_digital_dev *ddev, void *arg,
195 struct sk_buff *resp)
196{
197 struct nfc_target *target = arg;
198 struct digital_sdd_res *sdd_res;
199 int rc;
200 u8 offset, size;
201 u8 i, bcc;
202
203 if (IS_ERR(resp)) {
204 rc = PTR_ERR(resp);
205 resp = NULL;
206 goto exit;
207 }
208
209 if (resp->len < DIGITAL_SDD_RES_LEN) {
210 PROTOCOL_ERR("4.7.2.8");
211 rc = -EINVAL;
212 goto exit;
213 }
214
215 sdd_res = (struct digital_sdd_res *)resp->data;
216
217 for (i = 0, bcc = 0; i < 4; i++)
218 bcc ^= sdd_res->nfcid1[i];
219
220 if (bcc != sdd_res->bcc) {
221 PROTOCOL_ERR("4.7.2.6");
222 rc = -EINVAL;
223 goto exit;
224 }
225
226 if (sdd_res->nfcid1[0] == DIGITAL_SDD_RES_CT) {
227 offset = 1;
228 size = 3;
229 } else {
230 offset = 0;
231 size = 4;
232 }
233
234 memcpy(target->nfcid1 + target->nfcid1_len, sdd_res->nfcid1 + offset,
235 size);
236 target->nfcid1_len += size;
237
238 rc = digital_in_send_sel_req(ddev, target, sdd_res);
239
240exit:
241 dev_kfree_skb(resp);
242
243 if (rc) {
244 kfree(target);
245 digital_poll_next_tech(ddev);
246 }
247}
248
249static int digital_in_send_sdd_req(struct nfc_digital_dev *ddev,
250 struct nfc_target *target)
251{
252 int rc;
253 struct sk_buff *skb;
254 u8 sel_cmd;
255
256 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
257 NFC_DIGITAL_FRAMING_NFCA_STANDARD);
258 if (rc)
259 return rc;
260
261 skb = digital_skb_alloc(ddev, 2);
262 if (!skb)
263 return -ENOMEM;
264
265 if (target->nfcid1_len == 0)
266 sel_cmd = DIGITAL_CMD_SEL_REQ_CL1;
267 else if (target->nfcid1_len == 3)
268 sel_cmd = DIGITAL_CMD_SEL_REQ_CL2;
269 else
270 sel_cmd = DIGITAL_CMD_SEL_REQ_CL3;
271
272 *skb_put(skb, sizeof(u8)) = sel_cmd;
273 *skb_put(skb, sizeof(u8)) = DIGITAL_SDD_REQ_SEL_PAR;
274
275 return digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sdd_res,
276 target);
277}
278
279static void digital_in_recv_sens_res(struct nfc_digital_dev *ddev, void *arg,
280 struct sk_buff *resp)
281{
282 struct nfc_target *target = NULL;
283 int rc;
284
285 if (IS_ERR(resp)) {
286 rc = PTR_ERR(resp);
287 resp = NULL;
288 goto exit;
289 }
290
291 if (resp->len < sizeof(u16)) {
292 rc = -EIO;
293 goto exit;
294 }
295
296 target = kzalloc(sizeof(struct nfc_target), GFP_KERNEL);
297 if (!target) {
298 rc = -ENOMEM;
299 goto exit;
300 }
301
302 target->sens_res = __le16_to_cpu(*(__le16 *)resp->data);
303
304 if (!DIGITAL_SENS_RES_IS_VALID(target->sens_res)) {
305 PROTOCOL_ERR("4.6.3.3");
306 rc = -EINVAL;
307 goto exit;
308 }
309
310 if (DIGITAL_SENS_RES_IS_T1T(target->sens_res))
311 rc = digital_target_found(ddev, target, NFC_PROTO_JEWEL);
312 else
313 rc = digital_in_send_sdd_req(ddev, target);
314
315exit:
316 dev_kfree_skb(resp);
317
318 if (rc) {
319 kfree(target);
320 digital_poll_next_tech(ddev);
321 }
322}
323
324int digital_in_send_sens_req(struct nfc_digital_dev *ddev, u8 rf_tech)
325{
326 struct sk_buff *skb;
327 int rc;
328
329 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH,
330 NFC_DIGITAL_RF_TECH_106A);
331 if (rc)
332 return rc;
333
334 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
335 NFC_DIGITAL_FRAMING_NFCA_SHORT);
336 if (rc)
337 return rc;
338
339 skb = digital_skb_alloc(ddev, 1);
340 if (!skb)
341 return -ENOMEM;
342
343 *skb_put(skb, sizeof(u8)) = DIGITAL_CMD_SENS_REQ;
344
345 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sens_res, NULL);
346 if (rc)
347 kfree_skb(skb);
348
349 return rc;
350}
351
352int digital_in_recv_mifare_res(struct sk_buff *resp)
353{
354 /* Successful READ command response is 16 data bytes + 2 CRC bytes long.
355 * Since the driver can't differentiate a ACK/NACK response from a valid
356 * READ response, the CRC calculation must be handled at digital level
357 * even if the driver supports it for this technology.
358 */
359 if (resp->len == DIGITAL_MIFARE_READ_RES_LEN + DIGITAL_CRC_LEN) {
360 if (digital_skb_check_crc_a(resp)) {
361 PROTOCOL_ERR("9.4.1.2");
362 return -EIO;
363 }
364
365 return 0;
366 }
367
368 /* ACK response (i.e. successful WRITE). */
369 if (resp->len == 1 && resp->data[0] == DIGITAL_MIFARE_ACK_RES) {
370 resp->data[0] = 0;
371 return 0;
372 }
373
374 /* NACK and any other responses are treated as error. */
375 return -EIO;
376}
377
378static void digital_in_recv_sensf_res(struct nfc_digital_dev *ddev, void *arg,
379 struct sk_buff *resp)
380{
381 int rc;
382 u8 proto;
383 struct nfc_target target;
384 struct digital_sensf_res *sensf_res;
385
386 if (IS_ERR(resp)) {
387 rc = PTR_ERR(resp);
388 resp = NULL;
389 goto exit;
390 }
391
392 if (resp->len < DIGITAL_SENSF_RES_MIN_LENGTH) {
393 rc = -EIO;
394 goto exit;
395 }
396
397 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev)) {
398 rc = digital_skb_check_crc_f(resp);
399 if (rc) {
400 PROTOCOL_ERR("6.4.1.8");
401 goto exit;
402 }
403 }
404
405 skb_pull(resp, 1);
406
407 memset(&target, 0, sizeof(struct nfc_target));
408
409 sensf_res = (struct digital_sensf_res *)resp->data;
410
411 memcpy(target.sensf_res, sensf_res, resp->len);
412 target.sensf_res_len = resp->len;
413
414 memcpy(target.nfcid2, sensf_res->nfcid2, NFC_NFCID2_MAXSIZE);
415 target.nfcid2_len = NFC_NFCID2_MAXSIZE;
416
417 if (target.nfcid2[0] == DIGITAL_SENSF_NFCID2_NFC_DEP_B1 &&
418 target.nfcid2[1] == DIGITAL_SENSF_NFCID2_NFC_DEP_B2)
419 proto = NFC_PROTO_NFC_DEP;
420 else
421 proto = NFC_PROTO_FELICA;
422
423 rc = digital_target_found(ddev, &target, proto);
424
425exit:
426 dev_kfree_skb(resp);
427
428 if (rc)
429 digital_poll_next_tech(ddev);
430}
431
432int digital_in_send_sensf_req(struct nfc_digital_dev *ddev, u8 rf_tech)
433{
434 struct digital_sensf_req *sensf_req;
435 struct sk_buff *skb;
436 int rc;
437 u8 size;
438
439 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
440 if (rc)
441 return rc;
442
443 rc = digital_in_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
444 NFC_DIGITAL_FRAMING_NFCF);
445 if (rc)
446 return rc;
447
448 size = sizeof(struct digital_sensf_req);
449
450 skb = digital_skb_alloc(ddev, size);
451 if (!skb)
452 return -ENOMEM;
453
454 skb_put(skb, size);
455
456 sensf_req = (struct digital_sensf_req *)skb->data;
457 sensf_req->cmd = DIGITAL_CMD_SENSF_REQ;
458 sensf_req->sc1 = 0xFF;
459 sensf_req->sc2 = 0xFF;
460 sensf_req->rc = 0;
461 sensf_req->tsn = 0;
462
463 *skb_push(skb, 1) = size + 1;
464
465 if (!DIGITAL_DRV_CAPS_IN_CRC(ddev))
466 digital_skb_add_crc_f(skb);
467
468 rc = digital_in_send_cmd(ddev, skb, 30, digital_in_recv_sensf_res,
469 NULL);
470 if (rc)
471 kfree_skb(skb);
472
473 return rc;
474}
475
476static int digital_tg_send_sel_res(struct nfc_digital_dev *ddev)
477{
478 struct sk_buff *skb;
479 int rc;
480
481 skb = digital_skb_alloc(ddev, 1);
482 if (!skb)
483 return -ENOMEM;
484
485 *skb_put(skb, 1) = DIGITAL_SEL_RES_NFC_DEP;
486
487 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
488 digital_skb_add_crc_a(skb);
489
490 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_atr_req,
491 NULL);
492 if (rc)
493 kfree_skb(skb);
494
495 return rc;
496}
497
498static void digital_tg_recv_sel_req(struct nfc_digital_dev *ddev, void *arg,
499 struct sk_buff *resp)
500{
501 int rc;
502
503 if (IS_ERR(resp)) {
504 rc = PTR_ERR(resp);
505 resp = NULL;
506 goto exit;
507 }
508
509 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
510 rc = digital_skb_check_crc_a(resp);
511 if (rc) {
512 PROTOCOL_ERR("4.4.1.3");
513 goto exit;
514 }
515 }
516
517 /* Silently ignore SEL_REQ content and send a SEL_RES for NFC-DEP */
518
519 rc = digital_tg_send_sel_res(ddev);
520
521exit:
522 if (rc)
523 digital_poll_next_tech(ddev);
524
525 dev_kfree_skb(resp);
526}
527
528static int digital_tg_send_sdd_res(struct nfc_digital_dev *ddev)
529{
530 struct sk_buff *skb;
531 struct digital_sdd_res *sdd_res;
532 int rc, i;
533
534 skb = digital_skb_alloc(ddev, sizeof(struct digital_sdd_res));
535 if (!skb)
536 return -ENOMEM;
537
538 skb_put(skb, sizeof(struct digital_sdd_res));
539 sdd_res = (struct digital_sdd_res *)skb->data;
540
541 sdd_res->nfcid1[0] = 0x08;
542 get_random_bytes(sdd_res->nfcid1 + 1, 3);
543
544 sdd_res->bcc = 0;
545 for (i = 0; i < 4; i++)
546 sdd_res->bcc ^= sdd_res->nfcid1[i];
547
548 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sel_req,
549 NULL);
550 if (rc)
551 kfree_skb(skb);
552
553 return rc;
554}
555
556static void digital_tg_recv_sdd_req(struct nfc_digital_dev *ddev, void *arg,
557 struct sk_buff *resp)
558{
559 u8 *sdd_req;
560 int rc;
561
562 if (IS_ERR(resp)) {
563 rc = PTR_ERR(resp);
564 resp = NULL;
565 goto exit;
566 }
567
568 sdd_req = resp->data;
569
570 if (resp->len < 2 || sdd_req[0] != DIGITAL_CMD_SEL_REQ_CL1 ||
571 sdd_req[1] != DIGITAL_SDD_REQ_SEL_PAR) {
572 rc = -EINVAL;
573 goto exit;
574 }
575
576 rc = digital_tg_send_sdd_res(ddev);
577
578exit:
579 if (rc)
580 digital_poll_next_tech(ddev);
581
582 dev_kfree_skb(resp);
583}
584
585static int digital_tg_send_sens_res(struct nfc_digital_dev *ddev)
586{
587 struct sk_buff *skb;
588 u8 *sens_res;
589 int rc;
590
591 skb = digital_skb_alloc(ddev, 2);
592 if (!skb)
593 return -ENOMEM;
594
595 sens_res = skb_put(skb, 2);
596
597 sens_res[0] = (DIGITAL_SENS_RES_NFC_DEP >> 8) & 0xFF;
598 sens_res[1] = DIGITAL_SENS_RES_NFC_DEP & 0xFF;
599
600 rc = digital_tg_send_cmd(ddev, skb, 300, digital_tg_recv_sdd_req,
601 NULL);
602 if (rc)
603 kfree_skb(skb);
604
605 return rc;
606}
607
608void digital_tg_recv_sens_req(struct nfc_digital_dev *ddev, void *arg,
609 struct sk_buff *resp)
610{
611 u8 sens_req;
612 int rc;
613
614 if (IS_ERR(resp)) {
615 rc = PTR_ERR(resp);
616 resp = NULL;
617 goto exit;
618 }
619
620 sens_req = resp->data[0];
621
622 if (!resp->len || (sens_req != DIGITAL_CMD_SENS_REQ &&
623 sens_req != DIGITAL_CMD_ALL_REQ)) {
624 rc = -EINVAL;
625 goto exit;
626 }
627
628 rc = digital_tg_send_sens_res(ddev);
629
630exit:
631 if (rc)
632 digital_poll_next_tech(ddev);
633
634 dev_kfree_skb(resp);
635}
636
637static int digital_tg_send_sensf_res(struct nfc_digital_dev *ddev,
638 struct digital_sensf_req *sensf_req)
639{
640 struct sk_buff *skb;
641 u8 size;
642 int rc;
643 struct digital_sensf_res *sensf_res;
644
645 size = sizeof(struct digital_sensf_res);
646
647 if (sensf_req->rc != DIGITAL_SENSF_REQ_RC_NONE)
648 size -= sizeof(sensf_res->rd);
649
650 skb = digital_skb_alloc(ddev, size);
651 if (!skb)
652 return -ENOMEM;
653
654 skb_put(skb, size);
655
656 sensf_res = (struct digital_sensf_res *)skb->data;
657
658 memset(sensf_res, 0, size);
659
660 sensf_res->cmd = DIGITAL_CMD_SENSF_RES;
661 sensf_res->nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
662 sensf_res->nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
663 get_random_bytes(&sensf_res->nfcid2[2], 6);
664
665 switch (sensf_req->rc) {
666 case DIGITAL_SENSF_REQ_RC_SC:
667 sensf_res->rd[0] = sensf_req->sc1;
668 sensf_res->rd[1] = sensf_req->sc2;
669 break;
670 case DIGITAL_SENSF_REQ_RC_AP:
671 sensf_res->rd[0] = DIGITAL_SENSF_RES_RD_AP_B1;
672 sensf_res->rd[1] = DIGITAL_SENSF_RES_RD_AP_B2;
673 break;
674 }
675
676 *skb_push(skb, sizeof(u8)) = size + 1;
677
678 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev))
679 digital_skb_add_crc_f(skb);
680
681 rc = digital_tg_send_cmd(ddev, skb, 300,
682 digital_tg_recv_atr_req, NULL);
683 if (rc)
684 kfree_skb(skb);
685
686 return rc;
687}
688
689void digital_tg_recv_sensf_req(struct nfc_digital_dev *ddev, void *arg,
690 struct sk_buff *resp)
691{
692 struct digital_sensf_req *sensf_req;
693 int rc;
694
695 if (IS_ERR(resp)) {
696 rc = PTR_ERR(resp);
697 resp = NULL;
698 goto exit;
699 }
700
701 if (!DIGITAL_DRV_CAPS_TG_CRC(ddev)) {
702 rc = digital_skb_check_crc_f(resp);
703 if (rc) {
704 PROTOCOL_ERR("6.4.1.8");
705 goto exit;
706 }
707 }
708
709 if (resp->len != sizeof(struct digital_sensf_req) + 1) {
710 rc = -EINVAL;
711 goto exit;
712 }
713
714 skb_pull(resp, 1);
715 sensf_req = (struct digital_sensf_req *)resp->data;
716
717 if (sensf_req->cmd != DIGITAL_CMD_SENSF_REQ) {
718 rc = -EINVAL;
719 goto exit;
720 }
721
722 rc = digital_tg_send_sensf_res(ddev, sensf_req);
723
724exit:
725 if (rc)
726 digital_poll_next_tech(ddev);
727
728 dev_kfree_skb(resp);
729}
730
731int digital_tg_listen_nfca(struct nfc_digital_dev *ddev, u8 rf_tech)
732{
733 int rc;
734
735 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
736 if (rc)
737 return rc;
738
739 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
740 NFC_DIGITAL_FRAMING_NFCA_NFC_DEP);
741 if (rc)
742 return rc;
743
744 return digital_tg_listen(ddev, 300, digital_tg_recv_sens_req, NULL);
745}
746
747int digital_tg_listen_nfcf(struct nfc_digital_dev *ddev, u8 rf_tech)
748{
749 int rc;
750 u8 *nfcid2;
751
752 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_RF_TECH, rf_tech);
753 if (rc)
754 return rc;
755
756 rc = digital_tg_configure_hw(ddev, NFC_DIGITAL_CONFIG_FRAMING,
757 NFC_DIGITAL_FRAMING_NFCF_NFC_DEP);
758 if (rc)
759 return rc;
760
761 nfcid2 = kzalloc(NFC_NFCID2_MAXSIZE, GFP_KERNEL);
762 if (!nfcid2)
763 return -ENOMEM;
764
765 nfcid2[0] = DIGITAL_SENSF_NFCID2_NFC_DEP_B1;
766 nfcid2[1] = DIGITAL_SENSF_NFCID2_NFC_DEP_B2;
767 get_random_bytes(nfcid2 + 2, NFC_NFCID2_MAXSIZE - 2);
768
769 return digital_tg_listen(ddev, 300, digital_tg_recv_sensf_req, nfcid2);
770}
diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c
index d308402b67d8..824c6056bf82 100644
--- a/net/nfc/llcp_sock.c
+++ b/net/nfc/llcp_sock.c
@@ -807,8 +807,6 @@ static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
807 807
808 pr_debug("%p %zu\n", sk, len); 808 pr_debug("%p %zu\n", sk, len);
809 809
810 msg->msg_namelen = 0;
811
812 lock_sock(sk); 810 lock_sock(sk);
813 811
814 if (sk->sk_state == LLCP_CLOSED && 812 if (sk->sk_state == LLCP_CLOSED &&
diff --git a/net/nfc/nci/spi.c b/net/nfc/nci/spi.c
index c7cf37ba7298..f1d426f10cce 100644
--- a/net/nfc/nci/spi.c
+++ b/net/nfc/nci/spi.c
@@ -21,11 +21,8 @@
21#include <linux/export.h> 21#include <linux/export.h>
22#include <linux/spi/spi.h> 22#include <linux/spi/spi.h>
23#include <linux/crc-ccitt.h> 23#include <linux/crc-ccitt.h>
24#include <linux/nfc.h>
25#include <net/nfc/nci_core.h> 24#include <net/nfc/nci_core.h>
26 25
27#define NCI_SPI_HDR_LEN 4
28#define NCI_SPI_CRC_LEN 2
29#define NCI_SPI_ACK_SHIFT 6 26#define NCI_SPI_ACK_SHIFT 6
30#define NCI_SPI_MSB_PAYLOAD_MASK 0x3F 27#define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
31 28
@@ -41,54 +38,48 @@
41 38
42#define CRC_INIT 0xFFFF 39#define CRC_INIT 0xFFFF
43 40
44static int nci_spi_open(struct nci_dev *nci_dev) 41static int __nci_spi_send(struct nci_spi *nspi, struct sk_buff *skb,
45{ 42 int cs_change)
46 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
47
48 return ndev->ops->open(ndev);
49}
50
51static int nci_spi_close(struct nci_dev *nci_dev)
52{
53 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
54
55 return ndev->ops->close(ndev);
56}
57
58static int __nci_spi_send(struct nci_spi_dev *ndev, struct sk_buff *skb)
59{ 43{
60 struct spi_message m; 44 struct spi_message m;
61 struct spi_transfer t; 45 struct spi_transfer t;
62 46
63 t.tx_buf = skb->data; 47 memset(&t, 0, sizeof(struct spi_transfer));
64 t.len = skb->len; 48 /* a NULL skb means we just want the SPI chip select line to raise */
65 t.cs_change = 0; 49 if (skb) {
66 t.delay_usecs = ndev->xfer_udelay; 50 t.tx_buf = skb->data;
51 t.len = skb->len;
52 } else {
53 /* still set tx_buf non NULL to make the driver happy */
54 t.tx_buf = &t;
55 t.len = 0;
56 }
57 t.cs_change = cs_change;
58 t.delay_usecs = nspi->xfer_udelay;
67 59
68 spi_message_init(&m); 60 spi_message_init(&m);
69 spi_message_add_tail(&t, &m); 61 spi_message_add_tail(&t, &m);
70 62
71 return spi_sync(ndev->spi, &m); 63 return spi_sync(nspi->spi, &m);
72} 64}
73 65
74static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb) 66int nci_spi_send(struct nci_spi *nspi,
67 struct completion *write_handshake_completion,
68 struct sk_buff *skb)
75{ 69{
76 struct nci_spi_dev *ndev = nci_get_drvdata(nci_dev);
77 unsigned int payload_len = skb->len; 70 unsigned int payload_len = skb->len;
78 unsigned char *hdr; 71 unsigned char *hdr;
79 int ret; 72 int ret;
80 long completion_rc; 73 long completion_rc;
81 74
82 ndev->ops->deassert_int(ndev);
83
84 /* add the NCI SPI header to the start of the buffer */ 75 /* add the NCI SPI header to the start of the buffer */
85 hdr = skb_push(skb, NCI_SPI_HDR_LEN); 76 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
86 hdr[0] = NCI_SPI_DIRECT_WRITE; 77 hdr[0] = NCI_SPI_DIRECT_WRITE;
87 hdr[1] = ndev->acknowledge_mode; 78 hdr[1] = nspi->acknowledge_mode;
88 hdr[2] = payload_len >> 8; 79 hdr[2] = payload_len >> 8;
89 hdr[3] = payload_len & 0xFF; 80 hdr[3] = payload_len & 0xFF;
90 81
91 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) { 82 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
92 u16 crc; 83 u16 crc;
93 84
94 crc = crc_ccitt(CRC_INIT, skb->data, skb->len); 85 crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
@@ -96,123 +87,77 @@ static int nci_spi_send(struct nci_dev *nci_dev, struct sk_buff *skb)
96 *skb_put(skb, 1) = crc & 0xFF; 87 *skb_put(skb, 1) = crc & 0xFF;
97 } 88 }
98 89
99 ret = __nci_spi_send(ndev, skb); 90 if (write_handshake_completion) {
91 /* Trick SPI driver to raise chip select */
92 ret = __nci_spi_send(nspi, NULL, 1);
93 if (ret)
94 goto done;
100 95
101 kfree_skb(skb); 96 /* wait for NFC chip hardware handshake to complete */
102 ndev->ops->assert_int(ndev); 97 if (wait_for_completion_timeout(write_handshake_completion,
98 msecs_to_jiffies(1000)) == 0) {
99 ret = -ETIME;
100 goto done;
101 }
102 }
103 103
104 if (ret != 0 || ndev->acknowledge_mode == NCI_SPI_CRC_DISABLED) 104 ret = __nci_spi_send(nspi, skb, 0);
105 if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
105 goto done; 106 goto done;
106 107
107 init_completion(&ndev->req_completion); 108 init_completion(&nspi->req_completion);
108 completion_rc = 109 completion_rc = wait_for_completion_interruptible_timeout(
109 wait_for_completion_interruptible_timeout(&ndev->req_completion, 110 &nspi->req_completion,
110 NCI_SPI_SEND_TIMEOUT); 111 NCI_SPI_SEND_TIMEOUT);
111 112
112 if (completion_rc <= 0 || ndev->req_result == ACKNOWLEDGE_NACK) 113 if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK)
113 ret = -EIO; 114 ret = -EIO;
114 115
115done: 116done:
117 kfree_skb(skb);
118
116 return ret; 119 return ret;
117} 120}
118 121EXPORT_SYMBOL_GPL(nci_spi_send);
119static struct nci_ops nci_spi_ops = {
120 .open = nci_spi_open,
121 .close = nci_spi_close,
122 .send = nci_spi_send,
123};
124 122
125/* ---- Interface to NCI SPI drivers ---- */ 123/* ---- Interface to NCI SPI drivers ---- */
126 124
127/** 125/**
128 * nci_spi_allocate_device - allocate a new nci spi device 126 * nci_spi_allocate_spi - allocate a new nci spi
129 * 127 *
130 * @spi: SPI device 128 * @spi: SPI device
131 * @ops: device operations 129 * @acknowledge_mode: Acknowledge mode used by the NFC device
132 * @supported_protocols: NFC protocols supported by the device
133 * @supported_se: NFC Secure Elements supported by the device
134 * @acknowledge_mode: Acknowledge mode used by the device
135 * @delay: delay between transactions in us 130 * @delay: delay between transactions in us
131 * @ndev: nci dev to send incoming nci frames to
136 */ 132 */
137struct nci_spi_dev *nci_spi_allocate_device(struct spi_device *spi, 133struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
138 struct nci_spi_ops *ops, 134 u8 acknowledge_mode, unsigned int delay,
139 u32 supported_protocols, 135 struct nci_dev *ndev)
140 u32 supported_se,
141 u8 acknowledge_mode,
142 unsigned int delay)
143{ 136{
144 struct nci_spi_dev *ndev; 137 struct nci_spi *nspi;
145 int tailroom = 0;
146 138
147 if (!ops->open || !ops->close || !ops->assert_int || !ops->deassert_int) 139 nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL);
140 if (!nspi)
148 return NULL; 141 return NULL;
149 142
150 if (!supported_protocols) 143 nspi->acknowledge_mode = acknowledge_mode;
151 return NULL; 144 nspi->xfer_udelay = delay;
152
153 ndev = devm_kzalloc(&spi->dev, sizeof(struct nci_dev), GFP_KERNEL);
154 if (!ndev)
155 return NULL;
156 145
157 ndev->ops = ops; 146 nspi->spi = spi;
158 ndev->acknowledge_mode = acknowledge_mode; 147 nspi->ndev = ndev;
159 ndev->xfer_udelay = delay;
160 148
161 if (acknowledge_mode == NCI_SPI_CRC_ENABLED) 149 return nspi;
162 tailroom += NCI_SPI_CRC_LEN;
163
164 ndev->nci_dev = nci_allocate_device(&nci_spi_ops, supported_protocols,
165 NCI_SPI_HDR_LEN, tailroom);
166 if (!ndev->nci_dev)
167 return NULL;
168
169 nci_set_drvdata(ndev->nci_dev, ndev);
170
171 return ndev;
172} 150}
173EXPORT_SYMBOL_GPL(nci_spi_allocate_device); 151EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
174 152
175/** 153static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
176 * nci_spi_free_device - deallocate nci spi device
177 *
178 * @ndev: The nci spi device to deallocate
179 */
180void nci_spi_free_device(struct nci_spi_dev *ndev)
181{
182 nci_free_device(ndev->nci_dev);
183}
184EXPORT_SYMBOL_GPL(nci_spi_free_device);
185
186/**
187 * nci_spi_register_device - register a nci spi device in the nfc subsystem
188 *
189 * @pdev: The nci spi device to register
190 */
191int nci_spi_register_device(struct nci_spi_dev *ndev)
192{
193 return nci_register_device(ndev->nci_dev);
194}
195EXPORT_SYMBOL_GPL(nci_spi_register_device);
196
197/**
198 * nci_spi_unregister_device - unregister a nci spi device in the nfc subsystem
199 *
200 * @dev: The nci spi device to unregister
201 */
202void nci_spi_unregister_device(struct nci_spi_dev *ndev)
203{
204 nci_unregister_device(ndev->nci_dev);
205}
206EXPORT_SYMBOL_GPL(nci_spi_unregister_device);
207
208static int send_acknowledge(struct nci_spi_dev *ndev, u8 acknowledge)
209{ 154{
210 struct sk_buff *skb; 155 struct sk_buff *skb;
211 unsigned char *hdr; 156 unsigned char *hdr;
212 u16 crc; 157 u16 crc;
213 int ret; 158 int ret;
214 159
215 skb = nci_skb_alloc(ndev->nci_dev, 0, GFP_KERNEL); 160 skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
216 161
217 /* add the NCI SPI header to the start of the buffer */ 162 /* add the NCI SPI header to the start of the buffer */
218 hdr = skb_push(skb, NCI_SPI_HDR_LEN); 163 hdr = skb_push(skb, NCI_SPI_HDR_LEN);
@@ -225,14 +170,14 @@ static int send_acknowledge(struct nci_spi_dev *ndev, u8 acknowledge)
225 *skb_put(skb, 1) = crc >> 8; 170 *skb_put(skb, 1) = crc >> 8;
226 *skb_put(skb, 1) = crc & 0xFF; 171 *skb_put(skb, 1) = crc & 0xFF;
227 172
228 ret = __nci_spi_send(ndev, skb); 173 ret = __nci_spi_send(nspi, skb, 0);
229 174
230 kfree_skb(skb); 175 kfree_skb(skb);
231 176
232 return ret; 177 return ret;
233} 178}
234 179
235static struct sk_buff *__nci_spi_recv_frame(struct nci_spi_dev *ndev) 180static struct sk_buff *__nci_spi_read(struct nci_spi *nspi)
236{ 181{
237 struct sk_buff *skb; 182 struct sk_buff *skb;
238 struct spi_message m; 183 struct spi_message m;
@@ -242,43 +187,49 @@ static struct sk_buff *__nci_spi_recv_frame(struct nci_spi_dev *ndev)
242 int ret; 187 int ret;
243 188
244 spi_message_init(&m); 189 spi_message_init(&m);
190
191 memset(&tx, 0, sizeof(struct spi_transfer));
245 req[0] = NCI_SPI_DIRECT_READ; 192 req[0] = NCI_SPI_DIRECT_READ;
246 req[1] = ndev->acknowledge_mode; 193 req[1] = nspi->acknowledge_mode;
247 tx.tx_buf = req; 194 tx.tx_buf = req;
248 tx.len = 2; 195 tx.len = 2;
249 tx.cs_change = 0; 196 tx.cs_change = 0;
250 spi_message_add_tail(&tx, &m); 197 spi_message_add_tail(&tx, &m);
198
199 memset(&rx, 0, sizeof(struct spi_transfer));
251 rx.rx_buf = resp_hdr; 200 rx.rx_buf = resp_hdr;
252 rx.len = 2; 201 rx.len = 2;
253 rx.cs_change = 1; 202 rx.cs_change = 1;
254 spi_message_add_tail(&rx, &m); 203 spi_message_add_tail(&rx, &m);
255 ret = spi_sync(ndev->spi, &m);
256 204
205 ret = spi_sync(nspi->spi, &m);
257 if (ret) 206 if (ret)
258 return NULL; 207 return NULL;
259 208
260 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) 209 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
261 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) + 210 rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
262 resp_hdr[1] + NCI_SPI_CRC_LEN; 211 resp_hdr[1] + NCI_SPI_CRC_LEN;
263 else 212 else
264 rx_len = (resp_hdr[0] << 8) | resp_hdr[1]; 213 rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
265 214
266 skb = nci_skb_alloc(ndev->nci_dev, rx_len, GFP_KERNEL); 215 skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
267 if (!skb) 216 if (!skb)
268 return NULL; 217 return NULL;
269 218
270 spi_message_init(&m); 219 spi_message_init(&m);
220
221 memset(&rx, 0, sizeof(struct spi_transfer));
271 rx.rx_buf = skb_put(skb, rx_len); 222 rx.rx_buf = skb_put(skb, rx_len);
272 rx.len = rx_len; 223 rx.len = rx_len;
273 rx.cs_change = 0; 224 rx.cs_change = 0;
274 rx.delay_usecs = ndev->xfer_udelay; 225 rx.delay_usecs = nspi->xfer_udelay;
275 spi_message_add_tail(&rx, &m); 226 spi_message_add_tail(&rx, &m);
276 ret = spi_sync(ndev->spi, &m);
277 227
228 ret = spi_sync(nspi->spi, &m);
278 if (ret) 229 if (ret)
279 goto receive_error; 230 goto receive_error;
280 231
281 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) { 232 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
282 *skb_push(skb, 1) = resp_hdr[1]; 233 *skb_push(skb, 1) = resp_hdr[1];
283 *skb_push(skb, 1) = resp_hdr[0]; 234 *skb_push(skb, 1) = resp_hdr[0];
284 } 235 }
@@ -318,61 +269,53 @@ static u8 nci_spi_get_ack(struct sk_buff *skb)
318} 269}
319 270
320/** 271/**
321 * nci_spi_recv_frame - receive frame from NCI SPI drivers 272 * nci_spi_read - read frame from NCI SPI drivers
322 * 273 *
323 * @ndev: The nci spi device 274 * @nspi: The nci spi
324 * Context: can sleep 275 * Context: can sleep
325 * 276 *
326 * This call may only be used from a context that may sleep. The sleep 277 * This call may only be used from a context that may sleep. The sleep
327 * is non-interruptible, and has no timeout. 278 * is non-interruptible, and has no timeout.
328 * 279 *
329 * It returns zero on success, else a negative error code. 280 * It returns an allocated skb containing the frame on success, or NULL.
330 */ 281 */
331int nci_spi_recv_frame(struct nci_spi_dev *ndev) 282struct sk_buff *nci_spi_read(struct nci_spi *nspi)
332{ 283{
333 struct sk_buff *skb; 284 struct sk_buff *skb;
334 int ret = 0;
335
336 ndev->ops->deassert_int(ndev);
337 285
338 /* Retrieve frame from SPI */ 286 /* Retrieve frame from SPI */
339 skb = __nci_spi_recv_frame(ndev); 287 skb = __nci_spi_read(nspi);
340 if (!skb) { 288 if (!skb)
341 ret = -EIO;
342 goto done; 289 goto done;
343 }
344 290
345 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) { 291 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
346 if (!nci_spi_check_crc(skb)) { 292 if (!nci_spi_check_crc(skb)) {
347 send_acknowledge(ndev, ACKNOWLEDGE_NACK); 293 send_acknowledge(nspi, ACKNOWLEDGE_NACK);
348 goto done; 294 goto done;
349 } 295 }
350 296
351 /* In case of acknowledged mode: if ACK or NACK received, 297 /* In case of acknowledged mode: if ACK or NACK received,
352 * unblock completion of latest frame sent. 298 * unblock completion of latest frame sent.
353 */ 299 */
354 ndev->req_result = nci_spi_get_ack(skb); 300 nspi->req_result = nci_spi_get_ack(skb);
355 if (ndev->req_result) 301 if (nspi->req_result)
356 complete(&ndev->req_completion); 302 complete(&nspi->req_completion);
357 } 303 }
358 304
359 /* If there is no payload (ACK/NACK only frame), 305 /* If there is no payload (ACK/NACK only frame),
360 * free the socket buffer 306 * free the socket buffer
361 */ 307 */
362 if (skb->len == 0) { 308 if (!skb->len) {
363 kfree_skb(skb); 309 kfree_skb(skb);
310 skb = NULL;
364 goto done; 311 goto done;
365 } 312 }
366 313
367 if (ndev->acknowledge_mode == NCI_SPI_CRC_ENABLED) 314 if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
368 send_acknowledge(ndev, ACKNOWLEDGE_ACK); 315 send_acknowledge(nspi, ACKNOWLEDGE_ACK);
369
370 /* Forward skb to NCI core layer */
371 ret = nci_recv_frame(ndev->nci_dev, skb);
372 316
373done: 317done:
374 ndev->ops->assert_int(ndev);
375 318
376 return ret; 319 return skb;
377} 320}
378EXPORT_SYMBOL_GPL(nci_spi_recv_frame); 321EXPORT_SYMBOL_GPL(nci_spi_read);
diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index 68063b2025da..a9b2342d5253 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -30,8 +30,8 @@
30#include "nfc.h" 30#include "nfc.h"
31#include "llcp.h" 31#include "llcp.h"
32 32
33static struct genl_multicast_group nfc_genl_event_mcgrp = { 33static const struct genl_multicast_group nfc_genl_mcgrps[] = {
34 .name = NFC_GENL_MCAST_EVENT_NAME, 34 { .name = NFC_GENL_MCAST_EVENT_NAME, },
35}; 35};
36 36
37static struct genl_family nfc_genl_family = { 37static struct genl_family nfc_genl_family = {
@@ -58,6 +58,7 @@ static const struct nla_policy nfc_genl_policy[NFC_ATTR_MAX + 1] = {
58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED }, 58 [NFC_ATTR_LLC_SDP] = { .type = NLA_NESTED },
59 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING, 59 [NFC_ATTR_FIRMWARE_NAME] = { .type = NLA_STRING,
60 .len = NFC_FIRMWARE_NAME_MAXSIZE }, 60 .len = NFC_FIRMWARE_NAME_MAXSIZE },
61 [NFC_ATTR_SE_APDU] = { .type = NLA_BINARY },
61}; 62};
62 63
63static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = { 64static const struct nla_policy nfc_sdp_genl_policy[NFC_SDP_ATTR_MAX + 1] = {
@@ -193,7 +194,7 @@ int nfc_genl_targets_found(struct nfc_dev *dev)
193 194
194 genlmsg_end(msg, hdr); 195 genlmsg_end(msg, hdr);
195 196
196 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 197 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
197 198
198nla_put_failure: 199nla_put_failure:
199 genlmsg_cancel(msg, hdr); 200 genlmsg_cancel(msg, hdr);
@@ -222,7 +223,7 @@ int nfc_genl_target_lost(struct nfc_dev *dev, u32 target_idx)
222 223
223 genlmsg_end(msg, hdr); 224 genlmsg_end(msg, hdr);
224 225
225 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 226 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
226 227
227 return 0; 228 return 0;
228 229
@@ -254,7 +255,7 @@ int nfc_genl_tm_activated(struct nfc_dev *dev, u32 protocol)
254 255
255 genlmsg_end(msg, hdr); 256 genlmsg_end(msg, hdr);
256 257
257 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 258 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
258 259
259 return 0; 260 return 0;
260 261
@@ -284,7 +285,7 @@ int nfc_genl_tm_deactivated(struct nfc_dev *dev)
284 285
285 genlmsg_end(msg, hdr); 286 genlmsg_end(msg, hdr);
286 287
287 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 288 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
288 289
289 return 0; 290 return 0;
290 291
@@ -317,7 +318,7 @@ int nfc_genl_device_added(struct nfc_dev *dev)
317 318
318 genlmsg_end(msg, hdr); 319 genlmsg_end(msg, hdr);
319 320
320 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 321 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
321 322
322 return 0; 323 return 0;
323 324
@@ -347,7 +348,7 @@ int nfc_genl_device_removed(struct nfc_dev *dev)
347 348
348 genlmsg_end(msg, hdr); 349 genlmsg_end(msg, hdr);
349 350
350 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 351 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
351 352
352 return 0; 353 return 0;
353 354
@@ -413,7 +414,7 @@ int nfc_genl_llc_send_sdres(struct nfc_dev *dev, struct hlist_head *sdres_list)
413 414
414 genlmsg_end(msg, hdr); 415 genlmsg_end(msg, hdr);
415 416
416 return genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 417 return genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
417 418
418nla_put_failure: 419nla_put_failure:
419 genlmsg_cancel(msg, hdr); 420 genlmsg_cancel(msg, hdr);
@@ -447,7 +448,7 @@ int nfc_genl_se_added(struct nfc_dev *dev, u32 se_idx, u16 type)
447 448
448 genlmsg_end(msg, hdr); 449 genlmsg_end(msg, hdr);
449 450
450 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 451 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
451 452
452 return 0; 453 return 0;
453 454
@@ -478,7 +479,7 @@ int nfc_genl_se_removed(struct nfc_dev *dev, u32 se_idx)
478 479
479 genlmsg_end(msg, hdr); 480 genlmsg_end(msg, hdr);
480 481
481 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 482 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
482 483
483 return 0; 484 return 0;
484 485
@@ -599,7 +600,7 @@ int nfc_genl_dep_link_up_event(struct nfc_dev *dev, u32 target_idx,
599 600
600 dev->dep_link_up = true; 601 dev->dep_link_up = true;
601 602
602 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 603 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
603 604
604 return 0; 605 return 0;
605 606
@@ -631,7 +632,7 @@ int nfc_genl_dep_link_down_event(struct nfc_dev *dev)
631 632
632 genlmsg_end(msg, hdr); 633 genlmsg_end(msg, hdr);
633 634
634 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_ATOMIC); 635 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_ATOMIC);
635 636
636 return 0; 637 return 0;
637 638
@@ -1136,7 +1137,7 @@ int nfc_genl_fw_download_done(struct nfc_dev *dev, const char *firmware_name,
1136 1137
1137 genlmsg_end(msg, hdr); 1138 genlmsg_end(msg, hdr);
1138 1139
1139 genlmsg_multicast(msg, 0, nfc_genl_event_mcgrp.id, GFP_KERNEL); 1140 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1140 1141
1141 return 0; 1142 return 0;
1142 1143
@@ -1278,7 +1279,92 @@ static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1278 return 0; 1279 return 0;
1279} 1280}
1280 1281
1281static struct genl_ops nfc_genl_ops[] = { 1282struct se_io_ctx {
1283 u32 dev_idx;
1284 u32 se_idx;
1285};
1286
1287static void se_io_cb(void *context, u8 *apdu, size_t apdu_len, int err)
1288{
1289 struct se_io_ctx *ctx = context;
1290 struct sk_buff *msg;
1291 void *hdr;
1292
1293 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1294 if (!msg) {
1295 kfree(ctx);
1296 return;
1297 }
1298
1299 hdr = genlmsg_put(msg, 0, 0, &nfc_genl_family, 0,
1300 NFC_CMD_SE_IO);
1301 if (!hdr)
1302 goto free_msg;
1303
1304 if (nla_put_u32(msg, NFC_ATTR_DEVICE_INDEX, ctx->dev_idx) ||
1305 nla_put_u32(msg, NFC_ATTR_SE_INDEX, ctx->se_idx) ||
1306 nla_put(msg, NFC_ATTR_SE_APDU, apdu_len, apdu))
1307 goto nla_put_failure;
1308
1309 genlmsg_end(msg, hdr);
1310
1311 genlmsg_multicast(&nfc_genl_family, msg, 0, 0, GFP_KERNEL);
1312
1313 kfree(ctx);
1314
1315 return;
1316
1317nla_put_failure:
1318 genlmsg_cancel(msg, hdr);
1319free_msg:
1320 nlmsg_free(msg);
1321 kfree(ctx);
1322
1323 return;
1324}
1325
1326static int nfc_genl_se_io(struct sk_buff *skb, struct genl_info *info)
1327{
1328 struct nfc_dev *dev;
1329 struct se_io_ctx *ctx;
1330 u32 dev_idx, se_idx;
1331 u8 *apdu;
1332 size_t apdu_len;
1333
1334 if (!info->attrs[NFC_ATTR_DEVICE_INDEX] ||
1335 !info->attrs[NFC_ATTR_SE_INDEX] ||
1336 !info->attrs[NFC_ATTR_SE_APDU])
1337 return -EINVAL;
1338
1339 dev_idx = nla_get_u32(info->attrs[NFC_ATTR_DEVICE_INDEX]);
1340 se_idx = nla_get_u32(info->attrs[NFC_ATTR_SE_INDEX]);
1341
1342 dev = nfc_get_device(dev_idx);
1343 if (!dev)
1344 return -ENODEV;
1345
1346 if (!dev->ops || !dev->ops->se_io)
1347 return -ENOTSUPP;
1348
1349 apdu_len = nla_len(info->attrs[NFC_ATTR_SE_APDU]);
1350 if (apdu_len == 0)
1351 return -EINVAL;
1352
1353 apdu = nla_data(info->attrs[NFC_ATTR_SE_APDU]);
1354 if (!apdu)
1355 return -EINVAL;
1356
1357 ctx = kzalloc(sizeof(struct se_io_ctx), GFP_KERNEL);
1358 if (!ctx)
1359 return -ENOMEM;
1360
1361 ctx->dev_idx = dev_idx;
1362 ctx->se_idx = se_idx;
1363
1364 return dev->ops->se_io(dev, se_idx, apdu, apdu_len, se_io_cb, ctx);
1365}
1366
1367static const struct genl_ops nfc_genl_ops[] = {
1282 { 1368 {
1283 .cmd = NFC_CMD_GET_DEVICE, 1369 .cmd = NFC_CMD_GET_DEVICE,
1284 .doit = nfc_genl_get_device, 1370 .doit = nfc_genl_get_device,
@@ -1358,6 +1444,11 @@ static struct genl_ops nfc_genl_ops[] = {
1358 .done = nfc_genl_dump_ses_done, 1444 .done = nfc_genl_dump_ses_done,
1359 .policy = nfc_genl_policy, 1445 .policy = nfc_genl_policy,
1360 }, 1446 },
1447 {
1448 .cmd = NFC_CMD_SE_IO,
1449 .doit = nfc_genl_se_io,
1450 .policy = nfc_genl_policy,
1451 },
1361}; 1452};
1362 1453
1363 1454
@@ -1445,16 +1536,15 @@ int __init nfc_genl_init(void)
1445{ 1536{
1446 int rc; 1537 int rc;
1447 1538
1448 rc = genl_register_family_with_ops(&nfc_genl_family, nfc_genl_ops, 1539 rc = genl_register_family_with_ops_groups(&nfc_genl_family,
1449 ARRAY_SIZE(nfc_genl_ops)); 1540 nfc_genl_ops,
1541 nfc_genl_mcgrps);
1450 if (rc) 1542 if (rc)
1451 return rc; 1543 return rc;
1452 1544
1453 rc = genl_register_mc_group(&nfc_genl_family, &nfc_genl_event_mcgrp);
1454
1455 netlink_register_notifier(&nl_notifier); 1545 netlink_register_notifier(&nl_notifier);
1456 1546
1457 return rc; 1547 return 0;
1458} 1548}
1459 1549
1460/** 1550/**
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 313bf1bc848a..66bcd2eb5773 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -142,11 +142,11 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
142 142
143 err = rawsock_add_header(skb); 143 err = rawsock_add_header(skb);
144 if (err) 144 if (err)
145 goto error; 145 goto error_skb;
146 146
147 err = sock_queue_rcv_skb(sk, skb); 147 err = sock_queue_rcv_skb(sk, skb);
148 if (err) 148 if (err)
149 goto error; 149 goto error_skb;
150 150
151 spin_lock_bh(&sk->sk_write_queue.lock); 151 spin_lock_bh(&sk->sk_write_queue.lock);
152 if (!skb_queue_empty(&sk->sk_write_queue)) 152 if (!skb_queue_empty(&sk->sk_write_queue))
@@ -158,6 +158,9 @@ static void rawsock_data_exchange_complete(void *context, struct sk_buff *skb,
158 sock_put(sk); 158 sock_put(sk);
159 return; 159 return;
160 160
161error_skb:
162 kfree_skb(skb);
163
161error: 164error:
162 rawsock_report_error(sk, err); 165 rawsock_report_error(sk, err);
163 sock_put(sk); 166 sock_put(sk);
@@ -241,8 +244,6 @@ static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock,
241 if (!skb) 244 if (!skb)
242 return rc; 245 return rc;
243 246
244 msg->msg_namelen = 0;
245
246 copied = skb->len; 247 copied = skb->len;
247 if (len < copied) { 248 if (len < copied) {
248 msg->msg_flags |= MSG_TRUNC; 249 msg->msg_flags |= MSG_TRUNC;
diff --git a/net/openvswitch/Makefile b/net/openvswitch/Makefile
index ea36e99089af..3591cb5dae91 100644
--- a/net/openvswitch/Makefile
+++ b/net/openvswitch/Makefile
@@ -9,6 +9,8 @@ openvswitch-y := \
9 datapath.o \ 9 datapath.o \
10 dp_notify.o \ 10 dp_notify.o \
11 flow.o \ 11 flow.o \
12 flow_netlink.o \
13 flow_table.o \
12 vport.o \ 14 vport.o \
13 vport-internal_dev.o \ 15 vport-internal_dev.o \
14 vport-netdev.o 16 vport-netdev.o
diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c
index 2aa13bd7f2b2..6f5e1dd3be2d 100644
--- a/net/openvswitch/datapath.c
+++ b/net/openvswitch/datapath.c
@@ -55,21 +55,17 @@
55 55
56#include "datapath.h" 56#include "datapath.h"
57#include "flow.h" 57#include "flow.h"
58#include "flow_netlink.h"
58#include "vport-internal_dev.h" 59#include "vport-internal_dev.h"
59#include "vport-netdev.h" 60#include "vport-netdev.h"
60 61
61
62#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
63static void rehash_flow_table(struct work_struct *work);
64static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
65
66int ovs_net_id __read_mostly; 62int ovs_net_id __read_mostly;
67 63
68static void ovs_notify(struct sk_buff *skb, struct genl_info *info, 64static void ovs_notify(struct genl_family *family,
69 struct genl_multicast_group *grp) 65 struct sk_buff *skb, struct genl_info *info)
70{ 66{
71 genl_notify(skb, genl_info_net(info), info->snd_portid, 67 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
72 grp->id, info->nlhdr, GFP_KERNEL); 68 0, info->nlhdr, GFP_KERNEL);
73} 69}
74 70
75/** 71/**
@@ -165,7 +161,7 @@ static void destroy_dp_rcu(struct rcu_head *rcu)
165{ 161{
166 struct datapath *dp = container_of(rcu, struct datapath, rcu); 162 struct datapath *dp = container_of(rcu, struct datapath, rcu);
167 163
168 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false); 164 ovs_flow_tbl_destroy(&dp->table);
169 free_percpu(dp->stats_percpu); 165 free_percpu(dp->stats_percpu);
170 release_net(ovs_dp_get_net(dp)); 166 release_net(ovs_dp_get_net(dp));
171 kfree(dp->ports); 167 kfree(dp->ports);
@@ -225,6 +221,7 @@ void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
225 struct dp_stats_percpu *stats; 221 struct dp_stats_percpu *stats;
226 struct sw_flow_key key; 222 struct sw_flow_key key;
227 u64 *stats_counter; 223 u64 *stats_counter;
224 u32 n_mask_hit;
228 int error; 225 int error;
229 226
230 stats = this_cpu_ptr(dp->stats_percpu); 227 stats = this_cpu_ptr(dp->stats_percpu);
@@ -237,7 +234,7 @@ void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
237 } 234 }
238 235
239 /* Look up flow. */ 236 /* Look up flow. */
240 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key); 237 flow = ovs_flow_tbl_lookup(&dp->table, &key, &n_mask_hit);
241 if (unlikely(!flow)) { 238 if (unlikely(!flow)) {
242 struct dp_upcall_info upcall; 239 struct dp_upcall_info upcall;
243 240
@@ -262,6 +259,7 @@ out:
262 /* Update datapath statistics. */ 259 /* Update datapath statistics. */
263 u64_stats_update_begin(&stats->sync); 260 u64_stats_update_begin(&stats->sync);
264 (*stats_counter)++; 261 (*stats_counter)++;
262 stats->n_mask_hit += n_mask_hit;
265 u64_stats_update_end(&stats->sync); 263 u64_stats_update_end(&stats->sync);
266} 264}
267 265
@@ -435,7 +433,7 @@ static int queue_userspace_packet(struct net *net, int dp_ifindex,
435 upcall->dp_ifindex = dp_ifindex; 433 upcall->dp_ifindex = dp_ifindex;
436 434
437 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); 435 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
438 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb); 436 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
439 nla_nest_end(user_skb, nla); 437 nla_nest_end(user_skb, nla);
440 438
441 if (upcall_info->userdata) 439 if (upcall_info->userdata)
@@ -455,398 +453,6 @@ out:
455 return err; 453 return err;
456} 454}
457 455
458/* Called with ovs_mutex. */
459static int flush_flows(struct datapath *dp)
460{
461 struct flow_table *old_table;
462 struct flow_table *new_table;
463
464 old_table = ovsl_dereference(dp->table);
465 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
466 if (!new_table)
467 return -ENOMEM;
468
469 rcu_assign_pointer(dp->table, new_table);
470
471 ovs_flow_tbl_destroy(old_table, true);
472 return 0;
473}
474
475static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
476{
477
478 struct sw_flow_actions *acts;
479 int new_acts_size;
480 int req_size = NLA_ALIGN(attr_len);
481 int next_offset = offsetof(struct sw_flow_actions, actions) +
482 (*sfa)->actions_len;
483
484 if (req_size <= (ksize(*sfa) - next_offset))
485 goto out;
486
487 new_acts_size = ksize(*sfa) * 2;
488
489 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
490 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
491 return ERR_PTR(-EMSGSIZE);
492 new_acts_size = MAX_ACTIONS_BUFSIZE;
493 }
494
495 acts = ovs_flow_actions_alloc(new_acts_size);
496 if (IS_ERR(acts))
497 return (void *)acts;
498
499 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
500 acts->actions_len = (*sfa)->actions_len;
501 kfree(*sfa);
502 *sfa = acts;
503
504out:
505 (*sfa)->actions_len += req_size;
506 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
507}
508
509static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
510{
511 struct nlattr *a;
512
513 a = reserve_sfa_size(sfa, nla_attr_size(len));
514 if (IS_ERR(a))
515 return PTR_ERR(a);
516
517 a->nla_type = attrtype;
518 a->nla_len = nla_attr_size(len);
519
520 if (data)
521 memcpy(nla_data(a), data, len);
522 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
523
524 return 0;
525}
526
527static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
528{
529 int used = (*sfa)->actions_len;
530 int err;
531
532 err = add_action(sfa, attrtype, NULL, 0);
533 if (err)
534 return err;
535
536 return used;
537}
538
539static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
540{
541 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
542
543 a->nla_len = sfa->actions_len - st_offset;
544}
545
546static int validate_and_copy_actions(const struct nlattr *attr,
547 const struct sw_flow_key *key, int depth,
548 struct sw_flow_actions **sfa);
549
550static int validate_and_copy_sample(const struct nlattr *attr,
551 const struct sw_flow_key *key, int depth,
552 struct sw_flow_actions **sfa)
553{
554 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
555 const struct nlattr *probability, *actions;
556 const struct nlattr *a;
557 int rem, start, err, st_acts;
558
559 memset(attrs, 0, sizeof(attrs));
560 nla_for_each_nested(a, attr, rem) {
561 int type = nla_type(a);
562 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
563 return -EINVAL;
564 attrs[type] = a;
565 }
566 if (rem)
567 return -EINVAL;
568
569 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
570 if (!probability || nla_len(probability) != sizeof(u32))
571 return -EINVAL;
572
573 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
574 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
575 return -EINVAL;
576
577 /* validation done, copy sample action. */
578 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
579 if (start < 0)
580 return start;
581 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
582 if (err)
583 return err;
584 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
585 if (st_acts < 0)
586 return st_acts;
587
588 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
589 if (err)
590 return err;
591
592 add_nested_action_end(*sfa, st_acts);
593 add_nested_action_end(*sfa, start);
594
595 return 0;
596}
597
598static int validate_tp_port(const struct sw_flow_key *flow_key)
599{
600 if (flow_key->eth.type == htons(ETH_P_IP)) {
601 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
602 return 0;
603 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
604 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
605 return 0;
606 }
607
608 return -EINVAL;
609}
610
611static int validate_and_copy_set_tun(const struct nlattr *attr,
612 struct sw_flow_actions **sfa)
613{
614 struct sw_flow_match match;
615 struct sw_flow_key key;
616 int err, start;
617
618 ovs_match_init(&match, &key, NULL);
619 err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
620 if (err)
621 return err;
622
623 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
624 if (start < 0)
625 return start;
626
627 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
628 sizeof(match.key->tun_key));
629 add_nested_action_end(*sfa, start);
630
631 return err;
632}
633
634static int validate_set(const struct nlattr *a,
635 const struct sw_flow_key *flow_key,
636 struct sw_flow_actions **sfa,
637 bool *set_tun)
638{
639 const struct nlattr *ovs_key = nla_data(a);
640 int key_type = nla_type(ovs_key);
641
642 /* There can be only one key in a action */
643 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
644 return -EINVAL;
645
646 if (key_type > OVS_KEY_ATTR_MAX ||
647 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
648 ovs_key_lens[key_type] != -1))
649 return -EINVAL;
650
651 switch (key_type) {
652 const struct ovs_key_ipv4 *ipv4_key;
653 const struct ovs_key_ipv6 *ipv6_key;
654 int err;
655
656 case OVS_KEY_ATTR_PRIORITY:
657 case OVS_KEY_ATTR_SKB_MARK:
658 case OVS_KEY_ATTR_ETHERNET:
659 break;
660
661 case OVS_KEY_ATTR_TUNNEL:
662 *set_tun = true;
663 err = validate_and_copy_set_tun(a, sfa);
664 if (err)
665 return err;
666 break;
667
668 case OVS_KEY_ATTR_IPV4:
669 if (flow_key->eth.type != htons(ETH_P_IP))
670 return -EINVAL;
671
672 if (!flow_key->ip.proto)
673 return -EINVAL;
674
675 ipv4_key = nla_data(ovs_key);
676 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
677 return -EINVAL;
678
679 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
680 return -EINVAL;
681
682 break;
683
684 case OVS_KEY_ATTR_IPV6:
685 if (flow_key->eth.type != htons(ETH_P_IPV6))
686 return -EINVAL;
687
688 if (!flow_key->ip.proto)
689 return -EINVAL;
690
691 ipv6_key = nla_data(ovs_key);
692 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
693 return -EINVAL;
694
695 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
696 return -EINVAL;
697
698 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
699 return -EINVAL;
700
701 break;
702
703 case OVS_KEY_ATTR_TCP:
704 if (flow_key->ip.proto != IPPROTO_TCP)
705 return -EINVAL;
706
707 return validate_tp_port(flow_key);
708
709 case OVS_KEY_ATTR_UDP:
710 if (flow_key->ip.proto != IPPROTO_UDP)
711 return -EINVAL;
712
713 return validate_tp_port(flow_key);
714
715 case OVS_KEY_ATTR_SCTP:
716 if (flow_key->ip.proto != IPPROTO_SCTP)
717 return -EINVAL;
718
719 return validate_tp_port(flow_key);
720
721 default:
722 return -EINVAL;
723 }
724
725 return 0;
726}
727
728static int validate_userspace(const struct nlattr *attr)
729{
730 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
731 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
732 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
733 };
734 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
735 int error;
736
737 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
738 attr, userspace_policy);
739 if (error)
740 return error;
741
742 if (!a[OVS_USERSPACE_ATTR_PID] ||
743 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
744 return -EINVAL;
745
746 return 0;
747}
748
749static int copy_action(const struct nlattr *from,
750 struct sw_flow_actions **sfa)
751{
752 int totlen = NLA_ALIGN(from->nla_len);
753 struct nlattr *to;
754
755 to = reserve_sfa_size(sfa, from->nla_len);
756 if (IS_ERR(to))
757 return PTR_ERR(to);
758
759 memcpy(to, from, totlen);
760 return 0;
761}
762
763static int validate_and_copy_actions(const struct nlattr *attr,
764 const struct sw_flow_key *key,
765 int depth,
766 struct sw_flow_actions **sfa)
767{
768 const struct nlattr *a;
769 int rem, err;
770
771 if (depth >= SAMPLE_ACTION_DEPTH)
772 return -EOVERFLOW;
773
774 nla_for_each_nested(a, attr, rem) {
775 /* Expected argument lengths, (u32)-1 for variable length. */
776 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
777 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
778 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
779 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
780 [OVS_ACTION_ATTR_POP_VLAN] = 0,
781 [OVS_ACTION_ATTR_SET] = (u32)-1,
782 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
783 };
784 const struct ovs_action_push_vlan *vlan;
785 int type = nla_type(a);
786 bool skip_copy;
787
788 if (type > OVS_ACTION_ATTR_MAX ||
789 (action_lens[type] != nla_len(a) &&
790 action_lens[type] != (u32)-1))
791 return -EINVAL;
792
793 skip_copy = false;
794 switch (type) {
795 case OVS_ACTION_ATTR_UNSPEC:
796 return -EINVAL;
797
798 case OVS_ACTION_ATTR_USERSPACE:
799 err = validate_userspace(a);
800 if (err)
801 return err;
802 break;
803
804 case OVS_ACTION_ATTR_OUTPUT:
805 if (nla_get_u32(a) >= DP_MAX_PORTS)
806 return -EINVAL;
807 break;
808
809
810 case OVS_ACTION_ATTR_POP_VLAN:
811 break;
812
813 case OVS_ACTION_ATTR_PUSH_VLAN:
814 vlan = nla_data(a);
815 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
816 return -EINVAL;
817 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
818 return -EINVAL;
819 break;
820
821 case OVS_ACTION_ATTR_SET:
822 err = validate_set(a, key, sfa, &skip_copy);
823 if (err)
824 return err;
825 break;
826
827 case OVS_ACTION_ATTR_SAMPLE:
828 err = validate_and_copy_sample(a, key, depth, sfa);
829 if (err)
830 return err;
831 skip_copy = true;
832 break;
833
834 default:
835 return -EINVAL;
836 }
837 if (!skip_copy) {
838 err = copy_action(a, sfa);
839 if (err)
840 return err;
841 }
842 }
843
844 if (rem > 0)
845 return -EINVAL;
846
847 return 0;
848}
849
850static void clear_stats(struct sw_flow *flow) 456static void clear_stats(struct sw_flow *flow)
851{ 457{
852 flow->used = 0; 458 flow->used = 0;
@@ -902,15 +508,16 @@ static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
902 if (err) 508 if (err)
903 goto err_flow_free; 509 goto err_flow_free;
904 510
905 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]); 511 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
906 if (err) 512 if (err)
907 goto err_flow_free; 513 goto err_flow_free;
908 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS])); 514 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
909 err = PTR_ERR(acts); 515 err = PTR_ERR(acts);
910 if (IS_ERR(acts)) 516 if (IS_ERR(acts))
911 goto err_flow_free; 517 goto err_flow_free;
912 518
913 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts); 519 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
520 &flow->key, 0, &acts);
914 rcu_assign_pointer(flow->sf_acts, acts); 521 rcu_assign_pointer(flow->sf_acts, acts);
915 if (err) 522 if (err)
916 goto err_flow_free; 523 goto err_flow_free;
@@ -950,7 +557,7 @@ static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
950 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, 557 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
951}; 558};
952 559
953static struct genl_ops dp_packet_genl_ops[] = { 560static const struct genl_ops dp_packet_genl_ops[] = {
954 { .cmd = OVS_PACKET_CMD_EXECUTE, 561 { .cmd = OVS_PACKET_CMD_EXECUTE,
955 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 562 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
956 .policy = packet_policy, 563 .policy = packet_policy,
@@ -958,15 +565,18 @@ static struct genl_ops dp_packet_genl_ops[] = {
958 } 565 }
959}; 566};
960 567
961static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) 568static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
569 struct ovs_dp_megaflow_stats *mega_stats)
962{ 570{
963 struct flow_table *table;
964 int i; 571 int i;
965 572
966 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held()); 573 memset(mega_stats, 0, sizeof(*mega_stats));
967 stats->n_flows = ovs_flow_tbl_count(table); 574
575 stats->n_flows = ovs_flow_tbl_count(&dp->table);
576 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
968 577
969 stats->n_hit = stats->n_missed = stats->n_lost = 0; 578 stats->n_hit = stats->n_missed = stats->n_lost = 0;
579
970 for_each_possible_cpu(i) { 580 for_each_possible_cpu(i) {
971 const struct dp_stats_percpu *percpu_stats; 581 const struct dp_stats_percpu *percpu_stats;
972 struct dp_stats_percpu local_stats; 582 struct dp_stats_percpu local_stats;
@@ -982,6 +592,7 @@ static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
982 stats->n_hit += local_stats.n_hit; 592 stats->n_hit += local_stats.n_hit;
983 stats->n_missed += local_stats.n_missed; 593 stats->n_missed += local_stats.n_missed;
984 stats->n_lost += local_stats.n_lost; 594 stats->n_lost += local_stats.n_lost;
595 mega_stats->n_mask_hit += local_stats.n_mask_hit;
985 } 596 }
986} 597}
987 598
@@ -1005,100 +616,6 @@ static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1005 .name = OVS_FLOW_MCGROUP 616 .name = OVS_FLOW_MCGROUP
1006}; 617};
1007 618
1008static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1009static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1010{
1011 const struct nlattr *a;
1012 struct nlattr *start;
1013 int err = 0, rem;
1014
1015 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1016 if (!start)
1017 return -EMSGSIZE;
1018
1019 nla_for_each_nested(a, attr, rem) {
1020 int type = nla_type(a);
1021 struct nlattr *st_sample;
1022
1023 switch (type) {
1024 case OVS_SAMPLE_ATTR_PROBABILITY:
1025 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1026 return -EMSGSIZE;
1027 break;
1028 case OVS_SAMPLE_ATTR_ACTIONS:
1029 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1030 if (!st_sample)
1031 return -EMSGSIZE;
1032 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1033 if (err)
1034 return err;
1035 nla_nest_end(skb, st_sample);
1036 break;
1037 }
1038 }
1039
1040 nla_nest_end(skb, start);
1041 return err;
1042}
1043
1044static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1045{
1046 const struct nlattr *ovs_key = nla_data(a);
1047 int key_type = nla_type(ovs_key);
1048 struct nlattr *start;
1049 int err;
1050
1051 switch (key_type) {
1052 case OVS_KEY_ATTR_IPV4_TUNNEL:
1053 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1054 if (!start)
1055 return -EMSGSIZE;
1056
1057 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1058 nla_data(ovs_key));
1059 if (err)
1060 return err;
1061 nla_nest_end(skb, start);
1062 break;
1063 default:
1064 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1065 return -EMSGSIZE;
1066 break;
1067 }
1068
1069 return 0;
1070}
1071
1072static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1073{
1074 const struct nlattr *a;
1075 int rem, err;
1076
1077 nla_for_each_attr(a, attr, len, rem) {
1078 int type = nla_type(a);
1079
1080 switch (type) {
1081 case OVS_ACTION_ATTR_SET:
1082 err = set_action_to_attr(a, skb);
1083 if (err)
1084 return err;
1085 break;
1086
1087 case OVS_ACTION_ATTR_SAMPLE:
1088 err = sample_action_to_attr(a, skb);
1089 if (err)
1090 return err;
1091 break;
1092 default:
1093 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1094 return -EMSGSIZE;
1095 break;
1096 }
1097 }
1098
1099 return 0;
1100}
1101
1102static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) 619static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1103{ 620{
1104 return NLMSG_ALIGN(sizeof(struct ovs_header)) 621 return NLMSG_ALIGN(sizeof(struct ovs_header))
@@ -1135,8 +652,7 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1135 if (!nla) 652 if (!nla)
1136 goto nla_put_failure; 653 goto nla_put_failure;
1137 654
1138 err = ovs_flow_to_nlattrs(&flow->unmasked_key, 655 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
1139 &flow->unmasked_key, skb);
1140 if (err) 656 if (err)
1141 goto error; 657 goto error;
1142 nla_nest_end(skb, nla); 658 nla_nest_end(skb, nla);
@@ -1145,7 +661,7 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1145 if (!nla) 661 if (!nla)
1146 goto nla_put_failure; 662 goto nla_put_failure;
1147 663
1148 err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb); 664 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
1149 if (err) 665 if (err)
1150 goto error; 666 goto error;
1151 667
@@ -1155,7 +671,7 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1155 used = flow->used; 671 used = flow->used;
1156 stats.n_packets = flow->packet_count; 672 stats.n_packets = flow->packet_count;
1157 stats.n_bytes = flow->byte_count; 673 stats.n_bytes = flow->byte_count;
1158 tcp_flags = flow->tcp_flags; 674 tcp_flags = (u8)ntohs(flow->tcp_flags);
1159 spin_unlock_bh(&flow->lock); 675 spin_unlock_bh(&flow->lock);
1160 676
1161 if (used && 677 if (used &&
@@ -1188,7 +704,8 @@ static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1188 sf_acts = rcu_dereference_check(flow->sf_acts, 704 sf_acts = rcu_dereference_check(flow->sf_acts,
1189 lockdep_ovsl_is_held()); 705 lockdep_ovsl_is_held());
1190 706
1191 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb); 707 err = ovs_nla_put_actions(sf_acts->actions,
708 sf_acts->actions_len, skb);
1192 if (!err) 709 if (!err)
1193 nla_nest_end(skb, start); 710 nla_nest_end(skb, start);
1194 else { 711 else {
@@ -1234,6 +751,14 @@ static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1234 return skb; 751 return skb;
1235} 752}
1236 753
754static struct sw_flow *__ovs_flow_tbl_lookup(struct flow_table *tbl,
755 const struct sw_flow_key *key)
756{
757 u32 __always_unused n_mask_hit;
758
759 return ovs_flow_tbl_lookup(tbl, key, &n_mask_hit);
760}
761
1237static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) 762static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1238{ 763{
1239 struct nlattr **a = info->attrs; 764 struct nlattr **a = info->attrs;
@@ -1243,7 +768,6 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1243 struct sw_flow_mask mask; 768 struct sw_flow_mask mask;
1244 struct sk_buff *reply; 769 struct sk_buff *reply;
1245 struct datapath *dp; 770 struct datapath *dp;
1246 struct flow_table *table;
1247 struct sw_flow_actions *acts = NULL; 771 struct sw_flow_actions *acts = NULL;
1248 struct sw_flow_match match; 772 struct sw_flow_match match;
1249 int error; 773 int error;
@@ -1254,21 +778,21 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1254 goto error; 778 goto error;
1255 779
1256 ovs_match_init(&match, &key, &mask); 780 ovs_match_init(&match, &key, &mask);
1257 error = ovs_match_from_nlattrs(&match, 781 error = ovs_nla_get_match(&match,
1258 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]); 782 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1259 if (error) 783 if (error)
1260 goto error; 784 goto error;
1261 785
1262 /* Validate actions. */ 786 /* Validate actions. */
1263 if (a[OVS_FLOW_ATTR_ACTIONS]) { 787 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1264 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS])); 788 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1265 error = PTR_ERR(acts); 789 error = PTR_ERR(acts);
1266 if (IS_ERR(acts)) 790 if (IS_ERR(acts))
1267 goto error; 791 goto error;
1268 792
1269 ovs_flow_key_mask(&masked_key, &key, &mask); 793 ovs_flow_mask_key(&masked_key, &key, &mask);
1270 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], 794 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1271 &masked_key, 0, &acts); 795 &masked_key, 0, &acts);
1272 if (error) { 796 if (error) {
1273 OVS_NLERR("Flow actions may not be safe on all matching packets.\n"); 797 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1274 goto err_kfree; 798 goto err_kfree;
@@ -1284,29 +808,14 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1284 if (!dp) 808 if (!dp)
1285 goto err_unlock_ovs; 809 goto err_unlock_ovs;
1286 810
1287 table = ovsl_dereference(dp->table);
1288
1289 /* Check if this is a duplicate flow */ 811 /* Check if this is a duplicate flow */
1290 flow = ovs_flow_lookup(table, &key); 812 flow = __ovs_flow_tbl_lookup(&dp->table, &key);
1291 if (!flow) { 813 if (!flow) {
1292 struct sw_flow_mask *mask_p;
1293 /* Bail out if we're not allowed to create a new flow. */ 814 /* Bail out if we're not allowed to create a new flow. */
1294 error = -ENOENT; 815 error = -ENOENT;
1295 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) 816 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1296 goto err_unlock_ovs; 817 goto err_unlock_ovs;
1297 818
1298 /* Expand table, if necessary, to make room. */
1299 if (ovs_flow_tbl_need_to_expand(table)) {
1300 struct flow_table *new_table;
1301
1302 new_table = ovs_flow_tbl_expand(table);
1303 if (!IS_ERR(new_table)) {
1304 rcu_assign_pointer(dp->table, new_table);
1305 ovs_flow_tbl_destroy(table, true);
1306 table = ovsl_dereference(dp->table);
1307 }
1308 }
1309
1310 /* Allocate flow. */ 819 /* Allocate flow. */
1311 flow = ovs_flow_alloc(); 820 flow = ovs_flow_alloc();
1312 if (IS_ERR(flow)) { 821 if (IS_ERR(flow)) {
@@ -1317,25 +826,14 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1317 826
1318 flow->key = masked_key; 827 flow->key = masked_key;
1319 flow->unmasked_key = key; 828 flow->unmasked_key = key;
1320
1321 /* Make sure mask is unique in the system */
1322 mask_p = ovs_sw_flow_mask_find(table, &mask);
1323 if (!mask_p) {
1324 /* Allocate a new mask if none exsits. */
1325 mask_p = ovs_sw_flow_mask_alloc();
1326 if (!mask_p)
1327 goto err_flow_free;
1328 mask_p->key = mask.key;
1329 mask_p->range = mask.range;
1330 ovs_sw_flow_mask_insert(table, mask_p);
1331 }
1332
1333 ovs_sw_flow_mask_add_ref(mask_p);
1334 flow->mask = mask_p;
1335 rcu_assign_pointer(flow->sf_acts, acts); 829 rcu_assign_pointer(flow->sf_acts, acts);
1336 830
1337 /* Put flow in bucket. */ 831 /* Put flow in bucket. */
1338 ovs_flow_insert(table, flow); 832 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
833 if (error) {
834 acts = NULL;
835 goto err_flow_free;
836 }
1339 837
1340 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 838 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1341 info->snd_seq, OVS_FLOW_CMD_NEW); 839 info->snd_seq, OVS_FLOW_CMD_NEW);
@@ -1356,7 +854,7 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1356 854
1357 /* The unmasked key has to be the same for flow updates. */ 855 /* The unmasked key has to be the same for flow updates. */
1358 error = -EINVAL; 856 error = -EINVAL;
1359 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) { 857 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
1360 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n"); 858 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1361 goto err_unlock_ovs; 859 goto err_unlock_ovs;
1362 } 860 }
@@ -1364,7 +862,7 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1364 /* Update actions. */ 862 /* Update actions. */
1365 old_acts = ovsl_dereference(flow->sf_acts); 863 old_acts = ovsl_dereference(flow->sf_acts);
1366 rcu_assign_pointer(flow->sf_acts, acts); 864 rcu_assign_pointer(flow->sf_acts, acts);
1367 ovs_flow_deferred_free_acts(old_acts); 865 ovs_nla_free_flow_actions(old_acts);
1368 866
1369 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, 867 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1370 info->snd_seq, OVS_FLOW_CMD_NEW); 868 info->snd_seq, OVS_FLOW_CMD_NEW);
@@ -1379,10 +877,10 @@ static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1379 ovs_unlock(); 877 ovs_unlock();
1380 878
1381 if (!IS_ERR(reply)) 879 if (!IS_ERR(reply))
1382 ovs_notify(reply, info, &ovs_dp_flow_multicast_group); 880 ovs_notify(&dp_flow_genl_family, reply, info);
1383 else 881 else
1384 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 882 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1385 ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); 883 0, PTR_ERR(reply));
1386 return 0; 884 return 0;
1387 885
1388err_flow_free: 886err_flow_free:
@@ -1403,7 +901,6 @@ static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1403 struct sk_buff *reply; 901 struct sk_buff *reply;
1404 struct sw_flow *flow; 902 struct sw_flow *flow;
1405 struct datapath *dp; 903 struct datapath *dp;
1406 struct flow_table *table;
1407 struct sw_flow_match match; 904 struct sw_flow_match match;
1408 int err; 905 int err;
1409 906
@@ -1413,7 +910,7 @@ static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1413 } 910 }
1414 911
1415 ovs_match_init(&match, &key, NULL); 912 ovs_match_init(&match, &key, NULL);
1416 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL); 913 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1417 if (err) 914 if (err)
1418 return err; 915 return err;
1419 916
@@ -1424,9 +921,8 @@ static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1424 goto unlock; 921 goto unlock;
1425 } 922 }
1426 923
1427 table = ovsl_dereference(dp->table); 924 flow = __ovs_flow_tbl_lookup(&dp->table, &key);
1428 flow = ovs_flow_lookup_unmasked_key(table, &match); 925 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1429 if (!flow) {
1430 err = -ENOENT; 926 err = -ENOENT;
1431 goto unlock; 927 goto unlock;
1432 } 928 }
@@ -1453,7 +949,6 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1453 struct sk_buff *reply; 949 struct sk_buff *reply;
1454 struct sw_flow *flow; 950 struct sw_flow *flow;
1455 struct datapath *dp; 951 struct datapath *dp;
1456 struct flow_table *table;
1457 struct sw_flow_match match; 952 struct sw_flow_match match;
1458 int err; 953 int err;
1459 954
@@ -1465,18 +960,17 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1465 } 960 }
1466 961
1467 if (!a[OVS_FLOW_ATTR_KEY]) { 962 if (!a[OVS_FLOW_ATTR_KEY]) {
1468 err = flush_flows(dp); 963 err = ovs_flow_tbl_flush(&dp->table);
1469 goto unlock; 964 goto unlock;
1470 } 965 }
1471 966
1472 ovs_match_init(&match, &key, NULL); 967 ovs_match_init(&match, &key, NULL);
1473 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL); 968 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1474 if (err) 969 if (err)
1475 goto unlock; 970 goto unlock;
1476 971
1477 table = ovsl_dereference(dp->table); 972 flow = __ovs_flow_tbl_lookup(&dp->table, &key);
1478 flow = ovs_flow_lookup_unmasked_key(table, &match); 973 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1479 if (!flow) {
1480 err = -ENOENT; 974 err = -ENOENT;
1481 goto unlock; 975 goto unlock;
1482 } 976 }
@@ -1487,7 +981,7 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1487 goto unlock; 981 goto unlock;
1488 } 982 }
1489 983
1490 ovs_flow_remove(table, flow); 984 ovs_flow_tbl_remove(&dp->table, flow);
1491 985
1492 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, 986 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1493 info->snd_seq, 0, OVS_FLOW_CMD_DEL); 987 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
@@ -1496,7 +990,7 @@ static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1496 ovs_flow_free(flow, true); 990 ovs_flow_free(flow, true);
1497 ovs_unlock(); 991 ovs_unlock();
1498 992
1499 ovs_notify(reply, info, &ovs_dp_flow_multicast_group); 993 ovs_notify(&dp_flow_genl_family, reply, info);
1500 return 0; 994 return 0;
1501unlock: 995unlock:
1502 ovs_unlock(); 996 ovs_unlock();
@@ -1506,8 +1000,8 @@ unlock:
1506static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) 1000static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1507{ 1001{
1508 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); 1002 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1003 struct table_instance *ti;
1509 struct datapath *dp; 1004 struct datapath *dp;
1510 struct flow_table *table;
1511 1005
1512 rcu_read_lock(); 1006 rcu_read_lock();
1513 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); 1007 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
@@ -1516,14 +1010,14 @@ static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1516 return -ENODEV; 1010 return -ENODEV;
1517 } 1011 }
1518 1012
1519 table = rcu_dereference(dp->table); 1013 ti = rcu_dereference(dp->table.ti);
1520 for (;;) { 1014 for (;;) {
1521 struct sw_flow *flow; 1015 struct sw_flow *flow;
1522 u32 bucket, obj; 1016 u32 bucket, obj;
1523 1017
1524 bucket = cb->args[0]; 1018 bucket = cb->args[0];
1525 obj = cb->args[1]; 1019 obj = cb->args[1];
1526 flow = ovs_flow_dump_next(table, &bucket, &obj); 1020 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1527 if (!flow) 1021 if (!flow)
1528 break; 1022 break;
1529 1023
@@ -1540,7 +1034,7 @@ static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1540 return skb->len; 1034 return skb->len;
1541} 1035}
1542 1036
1543static struct genl_ops dp_flow_genl_ops[] = { 1037static const struct genl_ops dp_flow_genl_ops[] = {
1544 { .cmd = OVS_FLOW_CMD_NEW, 1038 { .cmd = OVS_FLOW_CMD_NEW,
1545 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1039 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1546 .policy = flow_policy, 1040 .policy = flow_policy,
@@ -1589,6 +1083,7 @@ static size_t ovs_dp_cmd_msg_size(void)
1589 1083
1590 msgsize += nla_total_size(IFNAMSIZ); 1084 msgsize += nla_total_size(IFNAMSIZ);
1591 msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); 1085 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1086 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1592 1087
1593 return msgsize; 1088 return msgsize;
1594} 1089}
@@ -1598,6 +1093,7 @@ static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1598{ 1093{
1599 struct ovs_header *ovs_header; 1094 struct ovs_header *ovs_header;
1600 struct ovs_dp_stats dp_stats; 1095 struct ovs_dp_stats dp_stats;
1096 struct ovs_dp_megaflow_stats dp_megaflow_stats;
1601 int err; 1097 int err;
1602 1098
1603 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, 1099 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
@@ -1613,8 +1109,14 @@ static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1613 if (err) 1109 if (err)
1614 goto nla_put_failure; 1110 goto nla_put_failure;
1615 1111
1616 get_dp_stats(dp, &dp_stats); 1112 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1617 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) 1113 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1114 &dp_stats))
1115 goto nla_put_failure;
1116
1117 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1118 sizeof(struct ovs_dp_megaflow_stats),
1119 &dp_megaflow_stats))
1618 goto nla_put_failure; 1120 goto nla_put_failure;
1619 1121
1620 return genlmsg_end(skb, ovs_header); 1122 return genlmsg_end(skb, ovs_header);
@@ -1687,9 +1189,8 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1687 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); 1189 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1688 1190
1689 /* Allocate table. */ 1191 /* Allocate table. */
1690 err = -ENOMEM; 1192 err = ovs_flow_tbl_init(&dp->table);
1691 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); 1193 if (err)
1692 if (!dp->table)
1693 goto err_free_dp; 1194 goto err_free_dp;
1694 1195
1695 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); 1196 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
@@ -1698,8 +1199,14 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1698 goto err_destroy_table; 1199 goto err_destroy_table;
1699 } 1200 }
1700 1201
1202 for_each_possible_cpu(i) {
1203 struct dp_stats_percpu *dpath_stats;
1204 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1205 u64_stats_init(&dpath_stats->sync);
1206 }
1207
1701 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), 1208 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1702 GFP_KERNEL); 1209 GFP_KERNEL);
1703 if (!dp->ports) { 1210 if (!dp->ports) {
1704 err = -ENOMEM; 1211 err = -ENOMEM;
1705 goto err_destroy_percpu; 1212 goto err_destroy_percpu;
@@ -1736,7 +1243,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1736 1243
1737 ovs_unlock(); 1244 ovs_unlock();
1738 1245
1739 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1246 ovs_notify(&dp_datapath_genl_family, reply, info);
1740 return 0; 1247 return 0;
1741 1248
1742err_destroy_local_port: 1249err_destroy_local_port:
@@ -1746,7 +1253,7 @@ err_destroy_ports_array:
1746err_destroy_percpu: 1253err_destroy_percpu:
1747 free_percpu(dp->stats_percpu); 1254 free_percpu(dp->stats_percpu);
1748err_destroy_table: 1255err_destroy_table:
1749 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false); 1256 ovs_flow_tbl_destroy(&dp->table);
1750err_free_dp: 1257err_free_dp:
1751 release_net(ovs_dp_get_net(dp)); 1258 release_net(ovs_dp_get_net(dp));
1752 kfree(dp); 1259 kfree(dp);
@@ -1801,7 +1308,7 @@ static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1801 __dp_destroy(dp); 1308 __dp_destroy(dp);
1802 ovs_unlock(); 1309 ovs_unlock();
1803 1310
1804 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1311 ovs_notify(&dp_datapath_genl_family, reply, info);
1805 1312
1806 return 0; 1313 return 0;
1807unlock: 1314unlock:
@@ -1825,14 +1332,14 @@ static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1825 info->snd_seq, OVS_DP_CMD_NEW); 1332 info->snd_seq, OVS_DP_CMD_NEW);
1826 if (IS_ERR(reply)) { 1333 if (IS_ERR(reply)) {
1827 err = PTR_ERR(reply); 1334 err = PTR_ERR(reply);
1828 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 1335 genl_set_err(&dp_datapath_genl_family, sock_net(skb->sk), 0,
1829 ovs_dp_datapath_multicast_group.id, err); 1336 0, err);
1830 err = 0; 1337 err = 0;
1831 goto unlock; 1338 goto unlock;
1832 } 1339 }
1833 1340
1834 ovs_unlock(); 1341 ovs_unlock();
1835 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); 1342 ovs_notify(&dp_datapath_genl_family, reply, info);
1836 1343
1837 return 0; 1344 return 0;
1838unlock: 1345unlock:
@@ -1891,7 +1398,7 @@ static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1891 return skb->len; 1398 return skb->len;
1892} 1399}
1893 1400
1894static struct genl_ops dp_datapath_genl_ops[] = { 1401static const struct genl_ops dp_datapath_genl_ops[] = {
1895 { .cmd = OVS_DP_CMD_NEW, 1402 { .cmd = OVS_DP_CMD_NEW,
1896 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1403 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1897 .policy = datapath_policy, 1404 .policy = datapath_policy,
@@ -1924,7 +1431,7 @@ static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1924 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, 1431 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1925}; 1432};
1926 1433
1927static struct genl_family dp_vport_genl_family = { 1434struct genl_family dp_vport_genl_family = {
1928 .id = GENL_ID_GENERATE, 1435 .id = GENL_ID_GENERATE,
1929 .hdrsize = sizeof(struct ovs_header), 1436 .hdrsize = sizeof(struct ovs_header),
1930 .name = OVS_VPORT_FAMILY, 1437 .name = OVS_VPORT_FAMILY,
@@ -2094,7 +1601,7 @@ static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2094 goto exit_unlock; 1601 goto exit_unlock;
2095 } 1602 }
2096 1603
2097 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1604 ovs_notify(&dp_vport_genl_family, reply, info);
2098 1605
2099exit_unlock: 1606exit_unlock:
2100 ovs_unlock(); 1607 ovs_unlock();
@@ -2141,7 +1648,7 @@ static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2141 BUG_ON(err < 0); 1648 BUG_ON(err < 0);
2142 1649
2143 ovs_unlock(); 1650 ovs_unlock();
2144 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1651 ovs_notify(&dp_vport_genl_family, reply, info);
2145 return 0; 1652 return 0;
2146 1653
2147exit_free: 1654exit_free:
@@ -2178,7 +1685,7 @@ static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2178 err = 0; 1685 err = 0;
2179 ovs_dp_detach_port(vport); 1686 ovs_dp_detach_port(vport);
2180 1687
2181 ovs_notify(reply, info, &ovs_dp_vport_multicast_group); 1688 ovs_notify(&dp_vport_genl_family, reply, info);
2182 1689
2183exit_unlock: 1690exit_unlock:
2184 ovs_unlock(); 1691 ovs_unlock();
@@ -2252,7 +1759,7 @@ out:
2252 return skb->len; 1759 return skb->len;
2253} 1760}
2254 1761
2255static struct genl_ops dp_vport_genl_ops[] = { 1762static const struct genl_ops dp_vport_genl_ops[] = {
2256 { .cmd = OVS_VPORT_CMD_NEW, 1763 { .cmd = OVS_VPORT_CMD_NEW,
2257 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ 1764 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2258 .policy = vport_policy, 1765 .policy = vport_policy,
@@ -2278,9 +1785,9 @@ static struct genl_ops dp_vport_genl_ops[] = {
2278 1785
2279struct genl_family_and_ops { 1786struct genl_family_and_ops {
2280 struct genl_family *family; 1787 struct genl_family *family;
2281 struct genl_ops *ops; 1788 const struct genl_ops *ops;
2282 int n_ops; 1789 int n_ops;
2283 struct genl_multicast_group *group; 1790 const struct genl_multicast_group *group;
2284}; 1791};
2285 1792
2286static const struct genl_family_and_ops dp_genl_families[] = { 1793static const struct genl_family_and_ops dp_genl_families[] = {
@@ -2316,17 +1823,14 @@ static int dp_register_genl(void)
2316 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { 1823 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2317 const struct genl_family_and_ops *f = &dp_genl_families[i]; 1824 const struct genl_family_and_ops *f = &dp_genl_families[i];
2318 1825
2319 err = genl_register_family_with_ops(f->family, f->ops, 1826 f->family->ops = f->ops;
2320 f->n_ops); 1827 f->family->n_ops = f->n_ops;
1828 f->family->mcgrps = f->group;
1829 f->family->n_mcgrps = f->group ? 1 : 0;
1830 err = genl_register_family(f->family);
2321 if (err) 1831 if (err)
2322 goto error; 1832 goto error;
2323 n_registered++; 1833 n_registered++;
2324
2325 if (f->group) {
2326 err = genl_register_mc_group(f->family, f->group);
2327 if (err)
2328 goto error;
2329 }
2330 } 1834 }
2331 1835
2332 return 0; 1836 return 0;
@@ -2336,32 +1840,6 @@ error:
2336 return err; 1840 return err;
2337} 1841}
2338 1842
2339static void rehash_flow_table(struct work_struct *work)
2340{
2341 struct datapath *dp;
2342 struct net *net;
2343
2344 ovs_lock();
2345 rtnl_lock();
2346 for_each_net(net) {
2347 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2348
2349 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2350 struct flow_table *old_table = ovsl_dereference(dp->table);
2351 struct flow_table *new_table;
2352
2353 new_table = ovs_flow_tbl_rehash(old_table);
2354 if (!IS_ERR(new_table)) {
2355 rcu_assign_pointer(dp->table, new_table);
2356 ovs_flow_tbl_destroy(old_table, true);
2357 }
2358 }
2359 }
2360 rtnl_unlock();
2361 ovs_unlock();
2362 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2363}
2364
2365static int __net_init ovs_init_net(struct net *net) 1843static int __net_init ovs_init_net(struct net *net)
2366{ 1844{
2367 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1845 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
@@ -2419,8 +1897,6 @@ static int __init dp_init(void)
2419 if (err < 0) 1897 if (err < 0)
2420 goto error_unreg_notifier; 1898 goto error_unreg_notifier;
2421 1899
2422 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2423
2424 return 0; 1900 return 0;
2425 1901
2426error_unreg_notifier: 1902error_unreg_notifier:
@@ -2437,7 +1913,6 @@ error:
2437 1913
2438static void dp_cleanup(void) 1914static void dp_cleanup(void)
2439{ 1915{
2440 cancel_delayed_work_sync(&rehash_flow_wq);
2441 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 1916 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2442 unregister_netdevice_notifier(&ovs_dp_device_notifier); 1917 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2443 unregister_pernet_device(&ovs_net_ops); 1918 unregister_pernet_device(&ovs_net_ops);
diff --git a/net/openvswitch/datapath.h b/net/openvswitch/datapath.h
index 4d109c176ef3..4067ea41be28 100644
--- a/net/openvswitch/datapath.h
+++ b/net/openvswitch/datapath.h
@@ -27,6 +27,7 @@
27#include <linux/u64_stats_sync.h> 27#include <linux/u64_stats_sync.h>
28 28
29#include "flow.h" 29#include "flow.h"
30#include "flow_table.h"
30#include "vport.h" 31#include "vport.h"
31 32
32#define DP_MAX_PORTS USHRT_MAX 33#define DP_MAX_PORTS USHRT_MAX
@@ -45,11 +46,15 @@
45 * @n_lost: Number of received packets that had no matching flow in the flow 46 * @n_lost: Number of received packets that had no matching flow in the flow
46 * table that could not be sent to userspace (normally due to an overflow in 47 * table that could not be sent to userspace (normally due to an overflow in
47 * one of the datapath's queues). 48 * one of the datapath's queues).
49 * @n_mask_hit: Number of masks looked up for flow match.
50 * @n_mask_hit / (@n_hit + @n_missed) will be the average masks looked
51 * up per packet.
48 */ 52 */
49struct dp_stats_percpu { 53struct dp_stats_percpu {
50 u64 n_hit; 54 u64 n_hit;
51 u64 n_missed; 55 u64 n_missed;
52 u64 n_lost; 56 u64 n_lost;
57 u64 n_mask_hit;
53 struct u64_stats_sync sync; 58 struct u64_stats_sync sync;
54}; 59};
55 60
@@ -57,7 +62,7 @@ struct dp_stats_percpu {
57 * struct datapath - datapath for flow-based packet switching 62 * struct datapath - datapath for flow-based packet switching
58 * @rcu: RCU callback head for deferred destruction. 63 * @rcu: RCU callback head for deferred destruction.
59 * @list_node: Element in global 'dps' list. 64 * @list_node: Element in global 'dps' list.
60 * @table: Current flow table. Protected by ovs_mutex and RCU. 65 * @table: flow table.
61 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by 66 * @ports: Hash table for ports. %OVSP_LOCAL port always exists. Protected by
62 * ovs_mutex and RCU. 67 * ovs_mutex and RCU.
63 * @stats_percpu: Per-CPU datapath statistics. 68 * @stats_percpu: Per-CPU datapath statistics.
@@ -71,7 +76,7 @@ struct datapath {
71 struct list_head list_node; 76 struct list_head list_node;
72 77
73 /* Flow table. */ 78 /* Flow table. */
74 struct flow_table __rcu *table; 79 struct flow_table table;
75 80
76 /* Switch ports. */ 81 /* Switch ports. */
77 struct hlist_head *ports; 82 struct hlist_head *ports;
@@ -172,6 +177,7 @@ static inline struct vport *ovs_vport_ovsl(const struct datapath *dp, int port_n
172} 177}
173 178
174extern struct notifier_block ovs_dp_device_notifier; 179extern struct notifier_block ovs_dp_device_notifier;
180extern struct genl_family dp_vport_genl_family;
175extern struct genl_multicast_group ovs_dp_vport_multicast_group; 181extern struct genl_multicast_group ovs_dp_vport_multicast_group;
176 182
177void ovs_dp_process_received_packet(struct vport *, struct sk_buff *); 183void ovs_dp_process_received_packet(struct vport *, struct sk_buff *);
diff --git a/net/openvswitch/dp_notify.c b/net/openvswitch/dp_notify.c
index c3235675f359..2c631fe76be1 100644
--- a/net/openvswitch/dp_notify.c
+++ b/net/openvswitch/dp_notify.c
@@ -34,15 +34,14 @@ static void dp_detach_port_notify(struct vport *vport)
34 OVS_VPORT_CMD_DEL); 34 OVS_VPORT_CMD_DEL);
35 ovs_dp_detach_port(vport); 35 ovs_dp_detach_port(vport);
36 if (IS_ERR(notify)) { 36 if (IS_ERR(notify)) {
37 netlink_set_err(ovs_dp_get_net(dp)->genl_sock, 0, 37 genl_set_err(&dp_vport_genl_family, ovs_dp_get_net(dp), 0,
38 ovs_dp_vport_multicast_group.id, 38 0, PTR_ERR(notify));
39 PTR_ERR(notify));
40 return; 39 return;
41 } 40 }
42 41
43 genlmsg_multicast_netns(ovs_dp_get_net(dp), notify, 0, 42 genlmsg_multicast_netns(&dp_vport_genl_family,
44 ovs_dp_vport_multicast_group.id, 43 ovs_dp_get_net(dp), notify, 0,
45 GFP_KERNEL); 44 0, GFP_KERNEL);
46} 45}
47 46
48void ovs_dp_notify_wq(struct work_struct *work) 47void ovs_dp_notify_wq(struct work_struct *work)
@@ -65,8 +64,7 @@ void ovs_dp_notify_wq(struct work_struct *work)
65 continue; 64 continue;
66 65
67 netdev_vport = netdev_vport_priv(vport); 66 netdev_vport = netdev_vport_priv(vport);
68 if (netdev_vport->dev->reg_state == NETREG_UNREGISTERED || 67 if (!(netdev_vport->dev->priv_flags & IFF_OVS_DATAPATH))
69 netdev_vport->dev->reg_state == NETREG_UNREGISTERING)
70 dp_detach_port_notify(vport); 68 dp_detach_port_notify(vport);
71 } 69 }
72 } 70 }
@@ -88,6 +86,10 @@ static int dp_device_event(struct notifier_block *unused, unsigned long event,
88 return NOTIFY_DONE; 86 return NOTIFY_DONE;
89 87
90 if (event == NETDEV_UNREGISTER) { 88 if (event == NETDEV_UNREGISTER) {
89 /* upper_dev_unlink and decrement promisc immediately */
90 ovs_netdev_detach_dev(vport);
91
92 /* schedule vport destroy, dev_put and genl notification */
91 ovs_net = net_generic(dev_net(dev), ovs_net_id); 93 ovs_net = net_generic(dev_net(dev), ovs_net_id);
92 queue_work(system_wq, &ovs_net->dp_notify_work); 94 queue_work(system_wq, &ovs_net->dp_notify_work);
93 } 95 }
diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c
index 410db90db73d..b409f5279601 100644
--- a/net/openvswitch/flow.c
+++ b/net/openvswitch/flow.c
@@ -45,202 +45,38 @@
45#include <net/ipv6.h> 45#include <net/ipv6.h>
46#include <net/ndisc.h> 46#include <net/ndisc.h>
47 47
48static struct kmem_cache *flow_cache; 48u64 ovs_flow_used_time(unsigned long flow_jiffies)
49
50static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
51 struct sw_flow_key_range *range, u8 val);
52
53static void update_range__(struct sw_flow_match *match,
54 size_t offset, size_t size, bool is_mask)
55{ 49{
56 struct sw_flow_key_range *range = NULL; 50 struct timespec cur_ts;
57 size_t start = rounddown(offset, sizeof(long)); 51 u64 cur_ms, idle_ms;
58 size_t end = roundup(offset + size, sizeof(long));
59
60 if (!is_mask)
61 range = &match->range;
62 else if (match->mask)
63 range = &match->mask->range;
64
65 if (!range)
66 return;
67
68 if (range->start == range->end) {
69 range->start = start;
70 range->end = end;
71 return;
72 }
73
74 if (range->start > start)
75 range->start = start;
76 52
77 if (range->end < end) 53 ktime_get_ts(&cur_ts);
78 range->end = end; 54 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
79} 55 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
56 cur_ts.tv_nsec / NSEC_PER_MSEC;
80 57
81#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \ 58 return cur_ms - idle_ms;
82 do { \
83 update_range__(match, offsetof(struct sw_flow_key, field), \
84 sizeof((match)->key->field), is_mask); \
85 if (is_mask) { \
86 if ((match)->mask) \
87 (match)->mask->key.field = value; \
88 } else { \
89 (match)->key->field = value; \
90 } \
91 } while (0)
92
93#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
94 do { \
95 update_range__(match, offsetof(struct sw_flow_key, field), \
96 len, is_mask); \
97 if (is_mask) { \
98 if ((match)->mask) \
99 memcpy(&(match)->mask->key.field, value_p, len);\
100 } else { \
101 memcpy(&(match)->key->field, value_p, len); \
102 } \
103 } while (0)
104
105static u16 range_n_bytes(const struct sw_flow_key_range *range)
106{
107 return range->end - range->start;
108} 59}
109 60
110void ovs_match_init(struct sw_flow_match *match, 61#define TCP_FLAGS_BE16(tp) (*(__be16 *)&tcp_flag_word(tp) & htons(0x0FFF))
111 struct sw_flow_key *key,
112 struct sw_flow_mask *mask)
113{
114 memset(match, 0, sizeof(*match));
115 match->key = key;
116 match->mask = mask;
117 62
118 memset(key, 0, sizeof(*key)); 63void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
119
120 if (mask) {
121 memset(&mask->key, 0, sizeof(mask->key));
122 mask->range.start = mask->range.end = 0;
123 }
124}
125
126static bool ovs_match_validate(const struct sw_flow_match *match,
127 u64 key_attrs, u64 mask_attrs)
128{ 64{
129 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET; 65 __be16 tcp_flags = 0;
130 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
131
132 /* The following mask attributes allowed only if they
133 * pass the validation tests. */
134 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
135 | (1 << OVS_KEY_ATTR_IPV6)
136 | (1 << OVS_KEY_ATTR_TCP)
137 | (1 << OVS_KEY_ATTR_UDP)
138 | (1 << OVS_KEY_ATTR_SCTP)
139 | (1 << OVS_KEY_ATTR_ICMP)
140 | (1 << OVS_KEY_ATTR_ICMPV6)
141 | (1 << OVS_KEY_ATTR_ARP)
142 | (1 << OVS_KEY_ATTR_ND));
143
144 /* Always allowed mask fields. */
145 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
146 | (1 << OVS_KEY_ATTR_IN_PORT)
147 | (1 << OVS_KEY_ATTR_ETHERTYPE));
148
149 /* Check key attributes. */
150 if (match->key->eth.type == htons(ETH_P_ARP)
151 || match->key->eth.type == htons(ETH_P_RARP)) {
152 key_expected |= 1 << OVS_KEY_ATTR_ARP;
153 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
154 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
155 }
156 66
157 if (match->key->eth.type == htons(ETH_P_IP)) { 67 if ((flow->key.eth.type == htons(ETH_P_IP) ||
158 key_expected |= 1 << OVS_KEY_ATTR_IPV4; 68 flow->key.eth.type == htons(ETH_P_IPV6)) &&
159 if (match->mask && (match->mask->key.eth.type == htons(0xffff))) 69 flow->key.ip.proto == IPPROTO_TCP &&
160 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4; 70 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
161 71 tcp_flags = TCP_FLAGS_BE16(tcp_hdr(skb));
162 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
163 if (match->key->ip.proto == IPPROTO_UDP) {
164 key_expected |= 1 << OVS_KEY_ATTR_UDP;
165 if (match->mask && (match->mask->key.ip.proto == 0xff))
166 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
167 }
168
169 if (match->key->ip.proto == IPPROTO_SCTP) {
170 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
171 if (match->mask && (match->mask->key.ip.proto == 0xff))
172 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
173 }
174
175 if (match->key->ip.proto == IPPROTO_TCP) {
176 key_expected |= 1 << OVS_KEY_ATTR_TCP;
177 if (match->mask && (match->mask->key.ip.proto == 0xff))
178 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
179 }
180
181 if (match->key->ip.proto == IPPROTO_ICMP) {
182 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
183 if (match->mask && (match->mask->key.ip.proto == 0xff))
184 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
185 }
186 }
187 }
188
189 if (match->key->eth.type == htons(ETH_P_IPV6)) {
190 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
191 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
192 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
193
194 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
195 if (match->key->ip.proto == IPPROTO_UDP) {
196 key_expected |= 1 << OVS_KEY_ATTR_UDP;
197 if (match->mask && (match->mask->key.ip.proto == 0xff))
198 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
199 }
200
201 if (match->key->ip.proto == IPPROTO_SCTP) {
202 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
203 if (match->mask && (match->mask->key.ip.proto == 0xff))
204 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
205 }
206
207 if (match->key->ip.proto == IPPROTO_TCP) {
208 key_expected |= 1 << OVS_KEY_ATTR_TCP;
209 if (match->mask && (match->mask->key.ip.proto == 0xff))
210 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
211 }
212
213 if (match->key->ip.proto == IPPROTO_ICMPV6) {
214 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
215 if (match->mask && (match->mask->key.ip.proto == 0xff))
216 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
217
218 if (match->key->ipv6.tp.src ==
219 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
220 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
221 key_expected |= 1 << OVS_KEY_ATTR_ND;
222 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
223 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
224 }
225 }
226 }
227 }
228
229 if ((key_attrs & key_expected) != key_expected) {
230 /* Key attributes check failed. */
231 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
232 key_attrs, key_expected);
233 return false;
234 }
235
236 if ((mask_attrs & mask_allowed) != mask_attrs) {
237 /* Mask attributes check failed. */
238 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
239 mask_attrs, mask_allowed);
240 return false;
241 } 72 }
242 73
243 return true; 74 spin_lock(&flow->lock);
75 flow->used = jiffies;
76 flow->packet_count++;
77 flow->byte_count += skb->len;
78 flow->tcp_flags |= tcp_flags;
79 spin_unlock(&flow->lock);
244} 80}
245 81
246static int check_header(struct sk_buff *skb, int len) 82static int check_header(struct sk_buff *skb, int len)
@@ -311,19 +147,6 @@ static bool icmphdr_ok(struct sk_buff *skb)
311 sizeof(struct icmphdr)); 147 sizeof(struct icmphdr));
312} 148}
313 149
314u64 ovs_flow_used_time(unsigned long flow_jiffies)
315{
316 struct timespec cur_ts;
317 u64 cur_ms, idle_ms;
318
319 ktime_get_ts(&cur_ts);
320 idle_ms = jiffies_to_msecs(jiffies - flow_jiffies);
321 cur_ms = (u64)cur_ts.tv_sec * MSEC_PER_SEC +
322 cur_ts.tv_nsec / NSEC_PER_MSEC;
323
324 return cur_ms - idle_ms;
325}
326
327static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key) 150static int parse_ipv6hdr(struct sk_buff *skb, struct sw_flow_key *key)
328{ 151{
329 unsigned int nh_ofs = skb_network_offset(skb); 152 unsigned int nh_ofs = skb_network_offset(skb);
@@ -372,311 +195,6 @@ static bool icmp6hdr_ok(struct sk_buff *skb)
372 sizeof(struct icmp6hdr)); 195 sizeof(struct icmp6hdr));
373} 196}
374 197
375void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
376 const struct sw_flow_mask *mask)
377{
378 const long *m = (long *)((u8 *)&mask->key + mask->range.start);
379 const long *s = (long *)((u8 *)src + mask->range.start);
380 long *d = (long *)((u8 *)dst + mask->range.start);
381 int i;
382
383 /* The memory outside of the 'mask->range' are not set since
384 * further operations on 'dst' only uses contents within
385 * 'mask->range'.
386 */
387 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
388 *d++ = *s++ & *m++;
389}
390
391#define TCP_FLAGS_OFFSET 13
392#define TCP_FLAG_MASK 0x3f
393
394void ovs_flow_used(struct sw_flow *flow, struct sk_buff *skb)
395{
396 u8 tcp_flags = 0;
397
398 if ((flow->key.eth.type == htons(ETH_P_IP) ||
399 flow->key.eth.type == htons(ETH_P_IPV6)) &&
400 flow->key.ip.proto == IPPROTO_TCP &&
401 likely(skb->len >= skb_transport_offset(skb) + sizeof(struct tcphdr))) {
402 u8 *tcp = (u8 *)tcp_hdr(skb);
403 tcp_flags = *(tcp + TCP_FLAGS_OFFSET) & TCP_FLAG_MASK;
404 }
405
406 spin_lock(&flow->lock);
407 flow->used = jiffies;
408 flow->packet_count++;
409 flow->byte_count += skb->len;
410 flow->tcp_flags |= tcp_flags;
411 spin_unlock(&flow->lock);
412}
413
414struct sw_flow_actions *ovs_flow_actions_alloc(int size)
415{
416 struct sw_flow_actions *sfa;
417
418 if (size > MAX_ACTIONS_BUFSIZE)
419 return ERR_PTR(-EINVAL);
420
421 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
422 if (!sfa)
423 return ERR_PTR(-ENOMEM);
424
425 sfa->actions_len = 0;
426 return sfa;
427}
428
429struct sw_flow *ovs_flow_alloc(void)
430{
431 struct sw_flow *flow;
432
433 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
434 if (!flow)
435 return ERR_PTR(-ENOMEM);
436
437 spin_lock_init(&flow->lock);
438 flow->sf_acts = NULL;
439 flow->mask = NULL;
440
441 return flow;
442}
443
444static struct hlist_head *find_bucket(struct flow_table *table, u32 hash)
445{
446 hash = jhash_1word(hash, table->hash_seed);
447 return flex_array_get(table->buckets,
448 (hash & (table->n_buckets - 1)));
449}
450
451static struct flex_array *alloc_buckets(unsigned int n_buckets)
452{
453 struct flex_array *buckets;
454 int i, err;
455
456 buckets = flex_array_alloc(sizeof(struct hlist_head),
457 n_buckets, GFP_KERNEL);
458 if (!buckets)
459 return NULL;
460
461 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
462 if (err) {
463 flex_array_free(buckets);
464 return NULL;
465 }
466
467 for (i = 0; i < n_buckets; i++)
468 INIT_HLIST_HEAD((struct hlist_head *)
469 flex_array_get(buckets, i));
470
471 return buckets;
472}
473
474static void free_buckets(struct flex_array *buckets)
475{
476 flex_array_free(buckets);
477}
478
479static struct flow_table *__flow_tbl_alloc(int new_size)
480{
481 struct flow_table *table = kmalloc(sizeof(*table), GFP_KERNEL);
482
483 if (!table)
484 return NULL;
485
486 table->buckets = alloc_buckets(new_size);
487
488 if (!table->buckets) {
489 kfree(table);
490 return NULL;
491 }
492 table->n_buckets = new_size;
493 table->count = 0;
494 table->node_ver = 0;
495 table->keep_flows = false;
496 get_random_bytes(&table->hash_seed, sizeof(u32));
497 table->mask_list = NULL;
498
499 return table;
500}
501
502static void __flow_tbl_destroy(struct flow_table *table)
503{
504 int i;
505
506 if (table->keep_flows)
507 goto skip_flows;
508
509 for (i = 0; i < table->n_buckets; i++) {
510 struct sw_flow *flow;
511 struct hlist_head *head = flex_array_get(table->buckets, i);
512 struct hlist_node *n;
513 int ver = table->node_ver;
514
515 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
516 hlist_del(&flow->hash_node[ver]);
517 ovs_flow_free(flow, false);
518 }
519 }
520
521 BUG_ON(!list_empty(table->mask_list));
522 kfree(table->mask_list);
523
524skip_flows:
525 free_buckets(table->buckets);
526 kfree(table);
527}
528
529struct flow_table *ovs_flow_tbl_alloc(int new_size)
530{
531 struct flow_table *table = __flow_tbl_alloc(new_size);
532
533 if (!table)
534 return NULL;
535
536 table->mask_list = kmalloc(sizeof(struct list_head), GFP_KERNEL);
537 if (!table->mask_list) {
538 table->keep_flows = true;
539 __flow_tbl_destroy(table);
540 return NULL;
541 }
542 INIT_LIST_HEAD(table->mask_list);
543
544 return table;
545}
546
547static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
548{
549 struct flow_table *table = container_of(rcu, struct flow_table, rcu);
550
551 __flow_tbl_destroy(table);
552}
553
554void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred)
555{
556 if (!table)
557 return;
558
559 if (deferred)
560 call_rcu(&table->rcu, flow_tbl_destroy_rcu_cb);
561 else
562 __flow_tbl_destroy(table);
563}
564
565struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *last)
566{
567 struct sw_flow *flow;
568 struct hlist_head *head;
569 int ver;
570 int i;
571
572 ver = table->node_ver;
573 while (*bucket < table->n_buckets) {
574 i = 0;
575 head = flex_array_get(table->buckets, *bucket);
576 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
577 if (i < *last) {
578 i++;
579 continue;
580 }
581 *last = i + 1;
582 return flow;
583 }
584 (*bucket)++;
585 *last = 0;
586 }
587
588 return NULL;
589}
590
591static void __tbl_insert(struct flow_table *table, struct sw_flow *flow)
592{
593 struct hlist_head *head;
594
595 head = find_bucket(table, flow->hash);
596 hlist_add_head_rcu(&flow->hash_node[table->node_ver], head);
597
598 table->count++;
599}
600
601static void flow_table_copy_flows(struct flow_table *old, struct flow_table *new)
602{
603 int old_ver;
604 int i;
605
606 old_ver = old->node_ver;
607 new->node_ver = !old_ver;
608
609 /* Insert in new table. */
610 for (i = 0; i < old->n_buckets; i++) {
611 struct sw_flow *flow;
612 struct hlist_head *head;
613
614 head = flex_array_get(old->buckets, i);
615
616 hlist_for_each_entry(flow, head, hash_node[old_ver])
617 __tbl_insert(new, flow);
618 }
619
620 new->mask_list = old->mask_list;
621 old->keep_flows = true;
622}
623
624static struct flow_table *__flow_tbl_rehash(struct flow_table *table, int n_buckets)
625{
626 struct flow_table *new_table;
627
628 new_table = __flow_tbl_alloc(n_buckets);
629 if (!new_table)
630 return ERR_PTR(-ENOMEM);
631
632 flow_table_copy_flows(table, new_table);
633
634 return new_table;
635}
636
637struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table)
638{
639 return __flow_tbl_rehash(table, table->n_buckets);
640}
641
642struct flow_table *ovs_flow_tbl_expand(struct flow_table *table)
643{
644 return __flow_tbl_rehash(table, table->n_buckets * 2);
645}
646
647static void __flow_free(struct sw_flow *flow)
648{
649 kfree((struct sf_flow_acts __force *)flow->sf_acts);
650 kmem_cache_free(flow_cache, flow);
651}
652
653static void rcu_free_flow_callback(struct rcu_head *rcu)
654{
655 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
656
657 __flow_free(flow);
658}
659
660void ovs_flow_free(struct sw_flow *flow, bool deferred)
661{
662 if (!flow)
663 return;
664
665 ovs_sw_flow_mask_del_ref(flow->mask, deferred);
666
667 if (deferred)
668 call_rcu(&flow->rcu, rcu_free_flow_callback);
669 else
670 __flow_free(flow);
671}
672
673/* Schedules 'sf_acts' to be freed after the next RCU grace period.
674 * The caller must hold rcu_read_lock for this to be sensible. */
675void ovs_flow_deferred_free_acts(struct sw_flow_actions *sf_acts)
676{
677 kfree_rcu(sf_acts, rcu);
678}
679
680static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key) 198static int parse_vlan(struct sk_buff *skb, struct sw_flow_key *key)
681{ 199{
682 struct qtag_prefix { 200 struct qtag_prefix {
@@ -910,6 +428,7 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
910 struct tcphdr *tcp = tcp_hdr(skb); 428 struct tcphdr *tcp = tcp_hdr(skb);
911 key->ipv4.tp.src = tcp->source; 429 key->ipv4.tp.src = tcp->source;
912 key->ipv4.tp.dst = tcp->dest; 430 key->ipv4.tp.dst = tcp->dest;
431 key->ipv4.tp.flags = TCP_FLAGS_BE16(tcp);
913 } 432 }
914 } else if (key->ip.proto == IPPROTO_UDP) { 433 } else if (key->ip.proto == IPPROTO_UDP) {
915 if (udphdr_ok(skb)) { 434 if (udphdr_ok(skb)) {
@@ -978,6 +497,7 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
978 struct tcphdr *tcp = tcp_hdr(skb); 497 struct tcphdr *tcp = tcp_hdr(skb);
979 key->ipv6.tp.src = tcp->source; 498 key->ipv6.tp.src = tcp->source;
980 key->ipv6.tp.dst = tcp->dest; 499 key->ipv6.tp.dst = tcp->dest;
500 key->ipv6.tp.flags = TCP_FLAGS_BE16(tcp);
981 } 501 }
982 } else if (key->ip.proto == NEXTHDR_UDP) { 502 } else if (key->ip.proto == NEXTHDR_UDP) {
983 if (udphdr_ok(skb)) { 503 if (udphdr_ok(skb)) {
@@ -1002,1080 +522,3 @@ int ovs_flow_extract(struct sk_buff *skb, u16 in_port, struct sw_flow_key *key)
1002 522
1003 return 0; 523 return 0;
1004} 524}
1005
1006static u32 ovs_flow_hash(const struct sw_flow_key *key, int key_start,
1007 int key_end)
1008{
1009 u32 *hash_key = (u32 *)((u8 *)key + key_start);
1010 int hash_u32s = (key_end - key_start) >> 2;
1011
1012 /* Make sure number of hash bytes are multiple of u32. */
1013 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
1014
1015 return jhash2(hash_key, hash_u32s, 0);
1016}
1017
1018static int flow_key_start(const struct sw_flow_key *key)
1019{
1020 if (key->tun_key.ipv4_dst)
1021 return 0;
1022 else
1023 return rounddown(offsetof(struct sw_flow_key, phy),
1024 sizeof(long));
1025}
1026
1027static bool __cmp_key(const struct sw_flow_key *key1,
1028 const struct sw_flow_key *key2, int key_start, int key_end)
1029{
1030 const long *cp1 = (long *)((u8 *)key1 + key_start);
1031 const long *cp2 = (long *)((u8 *)key2 + key_start);
1032 long diffs = 0;
1033 int i;
1034
1035 for (i = key_start; i < key_end; i += sizeof(long))
1036 diffs |= *cp1++ ^ *cp2++;
1037
1038 return diffs == 0;
1039}
1040
1041static bool __flow_cmp_masked_key(const struct sw_flow *flow,
1042 const struct sw_flow_key *key, int key_start, int key_end)
1043{
1044 return __cmp_key(&flow->key, key, key_start, key_end);
1045}
1046
1047static bool __flow_cmp_unmasked_key(const struct sw_flow *flow,
1048 const struct sw_flow_key *key, int key_start, int key_end)
1049{
1050 return __cmp_key(&flow->unmasked_key, key, key_start, key_end);
1051}
1052
1053bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
1054 const struct sw_flow_key *key, int key_end)
1055{
1056 int key_start;
1057 key_start = flow_key_start(key);
1058
1059 return __flow_cmp_unmasked_key(flow, key, key_start, key_end);
1060
1061}
1062
1063struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
1064 struct sw_flow_match *match)
1065{
1066 struct sw_flow_key *unmasked = match->key;
1067 int key_end = match->range.end;
1068 struct sw_flow *flow;
1069
1070 flow = ovs_flow_lookup(table, unmasked);
1071 if (flow && (!ovs_flow_cmp_unmasked_key(flow, unmasked, key_end)))
1072 flow = NULL;
1073
1074 return flow;
1075}
1076
1077static struct sw_flow *ovs_masked_flow_lookup(struct flow_table *table,
1078 const struct sw_flow_key *unmasked,
1079 struct sw_flow_mask *mask)
1080{
1081 struct sw_flow *flow;
1082 struct hlist_head *head;
1083 int key_start = mask->range.start;
1084 int key_end = mask->range.end;
1085 u32 hash;
1086 struct sw_flow_key masked_key;
1087
1088 ovs_flow_key_mask(&masked_key, unmasked, mask);
1089 hash = ovs_flow_hash(&masked_key, key_start, key_end);
1090 head = find_bucket(table, hash);
1091 hlist_for_each_entry_rcu(flow, head, hash_node[table->node_ver]) {
1092 if (flow->mask == mask &&
1093 __flow_cmp_masked_key(flow, &masked_key,
1094 key_start, key_end))
1095 return flow;
1096 }
1097 return NULL;
1098}
1099
1100struct sw_flow *ovs_flow_lookup(struct flow_table *tbl,
1101 const struct sw_flow_key *key)
1102{
1103 struct sw_flow *flow = NULL;
1104 struct sw_flow_mask *mask;
1105
1106 list_for_each_entry_rcu(mask, tbl->mask_list, list) {
1107 flow = ovs_masked_flow_lookup(tbl, key, mask);
1108 if (flow) /* Found */
1109 break;
1110 }
1111
1112 return flow;
1113}
1114
1115
1116void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow)
1117{
1118 flow->hash = ovs_flow_hash(&flow->key, flow->mask->range.start,
1119 flow->mask->range.end);
1120 __tbl_insert(table, flow);
1121}
1122
1123void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow)
1124{
1125 BUG_ON(table->count == 0);
1126 hlist_del_rcu(&flow->hash_node[table->node_ver]);
1127 table->count--;
1128}
1129
1130/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
1131const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
1132 [OVS_KEY_ATTR_ENCAP] = -1,
1133 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
1134 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
1135 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
1136 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
1137 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
1138 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
1139 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
1140 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
1141 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
1142 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
1143 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
1144 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
1145 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
1146 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
1147 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
1148 [OVS_KEY_ATTR_TUNNEL] = -1,
1149};
1150
1151static bool is_all_zero(const u8 *fp, size_t size)
1152{
1153 int i;
1154
1155 if (!fp)
1156 return false;
1157
1158 for (i = 0; i < size; i++)
1159 if (fp[i])
1160 return false;
1161
1162 return true;
1163}
1164
1165static int __parse_flow_nlattrs(const struct nlattr *attr,
1166 const struct nlattr *a[],
1167 u64 *attrsp, bool nz)
1168{
1169 const struct nlattr *nla;
1170 u32 attrs;
1171 int rem;
1172
1173 attrs = *attrsp;
1174 nla_for_each_nested(nla, attr, rem) {
1175 u16 type = nla_type(nla);
1176 int expected_len;
1177
1178 if (type > OVS_KEY_ATTR_MAX) {
1179 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
1180 type, OVS_KEY_ATTR_MAX);
1181 return -EINVAL;
1182 }
1183
1184 if (attrs & (1 << type)) {
1185 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
1186 return -EINVAL;
1187 }
1188
1189 expected_len = ovs_key_lens[type];
1190 if (nla_len(nla) != expected_len && expected_len != -1) {
1191 OVS_NLERR("Key attribute has unexpected length (type=%d"
1192 ", length=%d, expected=%d).\n", type,
1193 nla_len(nla), expected_len);
1194 return -EINVAL;
1195 }
1196
1197 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
1198 attrs |= 1 << type;
1199 a[type] = nla;
1200 }
1201 }
1202 if (rem) {
1203 OVS_NLERR("Message has %d unknown bytes.\n", rem);
1204 return -EINVAL;
1205 }
1206
1207 *attrsp = attrs;
1208 return 0;
1209}
1210
1211static int parse_flow_mask_nlattrs(const struct nlattr *attr,
1212 const struct nlattr *a[], u64 *attrsp)
1213{
1214 return __parse_flow_nlattrs(attr, a, attrsp, true);
1215}
1216
1217static int parse_flow_nlattrs(const struct nlattr *attr,
1218 const struct nlattr *a[], u64 *attrsp)
1219{
1220 return __parse_flow_nlattrs(attr, a, attrsp, false);
1221}
1222
1223int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
1224 struct sw_flow_match *match, bool is_mask)
1225{
1226 struct nlattr *a;
1227 int rem;
1228 bool ttl = false;
1229 __be16 tun_flags = 0;
1230
1231 nla_for_each_nested(a, attr, rem) {
1232 int type = nla_type(a);
1233 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
1234 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
1235 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
1236 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
1237 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
1238 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
1239 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
1240 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
1241 };
1242
1243 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
1244 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
1245 type, OVS_TUNNEL_KEY_ATTR_MAX);
1246 return -EINVAL;
1247 }
1248
1249 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
1250 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
1251 " length (type=%d, length=%d, expected=%d).\n",
1252 type, nla_len(a), ovs_tunnel_key_lens[type]);
1253 return -EINVAL;
1254 }
1255
1256 switch (type) {
1257 case OVS_TUNNEL_KEY_ATTR_ID:
1258 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
1259 nla_get_be64(a), is_mask);
1260 tun_flags |= TUNNEL_KEY;
1261 break;
1262 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
1263 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
1264 nla_get_be32(a), is_mask);
1265 break;
1266 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
1267 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
1268 nla_get_be32(a), is_mask);
1269 break;
1270 case OVS_TUNNEL_KEY_ATTR_TOS:
1271 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
1272 nla_get_u8(a), is_mask);
1273 break;
1274 case OVS_TUNNEL_KEY_ATTR_TTL:
1275 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
1276 nla_get_u8(a), is_mask);
1277 ttl = true;
1278 break;
1279 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
1280 tun_flags |= TUNNEL_DONT_FRAGMENT;
1281 break;
1282 case OVS_TUNNEL_KEY_ATTR_CSUM:
1283 tun_flags |= TUNNEL_CSUM;
1284 break;
1285 default:
1286 return -EINVAL;
1287 }
1288 }
1289
1290 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
1291
1292 if (rem > 0) {
1293 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
1294 return -EINVAL;
1295 }
1296
1297 if (!is_mask) {
1298 if (!match->key->tun_key.ipv4_dst) {
1299 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
1300 return -EINVAL;
1301 }
1302
1303 if (!ttl) {
1304 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
1305 return -EINVAL;
1306 }
1307 }
1308
1309 return 0;
1310}
1311
1312int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
1313 const struct ovs_key_ipv4_tunnel *tun_key,
1314 const struct ovs_key_ipv4_tunnel *output)
1315{
1316 struct nlattr *nla;
1317
1318 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
1319 if (!nla)
1320 return -EMSGSIZE;
1321
1322 if (output->tun_flags & TUNNEL_KEY &&
1323 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
1324 return -EMSGSIZE;
1325 if (output->ipv4_src &&
1326 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
1327 return -EMSGSIZE;
1328 if (output->ipv4_dst &&
1329 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
1330 return -EMSGSIZE;
1331 if (output->ipv4_tos &&
1332 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
1333 return -EMSGSIZE;
1334 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
1335 return -EMSGSIZE;
1336 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
1337 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
1338 return -EMSGSIZE;
1339 if ((output->tun_flags & TUNNEL_CSUM) &&
1340 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
1341 return -EMSGSIZE;
1342
1343 nla_nest_end(skb, nla);
1344 return 0;
1345}
1346
1347static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
1348 const struct nlattr **a, bool is_mask)
1349{
1350 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
1351 SW_FLOW_KEY_PUT(match, phy.priority,
1352 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
1353 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
1354 }
1355
1356 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
1357 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
1358
1359 if (is_mask)
1360 in_port = 0xffffffff; /* Always exact match in_port. */
1361 else if (in_port >= DP_MAX_PORTS)
1362 return -EINVAL;
1363
1364 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
1365 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
1366 } else if (!is_mask) {
1367 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
1368 }
1369
1370 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
1371 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
1372
1373 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
1374 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
1375 }
1376 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
1377 if (ovs_ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
1378 is_mask))
1379 return -EINVAL;
1380 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
1381 }
1382 return 0;
1383}
1384
1385static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
1386 const struct nlattr **a, bool is_mask)
1387{
1388 int err;
1389 u64 orig_attrs = attrs;
1390
1391 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
1392 if (err)
1393 return err;
1394
1395 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
1396 const struct ovs_key_ethernet *eth_key;
1397
1398 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
1399 SW_FLOW_KEY_MEMCPY(match, eth.src,
1400 eth_key->eth_src, ETH_ALEN, is_mask);
1401 SW_FLOW_KEY_MEMCPY(match, eth.dst,
1402 eth_key->eth_dst, ETH_ALEN, is_mask);
1403 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
1404 }
1405
1406 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
1407 __be16 tci;
1408
1409 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1410 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1411 if (is_mask)
1412 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
1413 else
1414 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
1415
1416 return -EINVAL;
1417 }
1418
1419 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
1420 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
1421 } else if (!is_mask)
1422 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
1423
1424 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
1425 __be16 eth_type;
1426
1427 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1428 if (is_mask) {
1429 /* Always exact match EtherType. */
1430 eth_type = htons(0xffff);
1431 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
1432 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
1433 ntohs(eth_type), ETH_P_802_3_MIN);
1434 return -EINVAL;
1435 }
1436
1437 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
1438 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1439 } else if (!is_mask) {
1440 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
1441 }
1442
1443 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1444 const struct ovs_key_ipv4 *ipv4_key;
1445
1446 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
1447 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
1448 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
1449 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
1450 return -EINVAL;
1451 }
1452 SW_FLOW_KEY_PUT(match, ip.proto,
1453 ipv4_key->ipv4_proto, is_mask);
1454 SW_FLOW_KEY_PUT(match, ip.tos,
1455 ipv4_key->ipv4_tos, is_mask);
1456 SW_FLOW_KEY_PUT(match, ip.ttl,
1457 ipv4_key->ipv4_ttl, is_mask);
1458 SW_FLOW_KEY_PUT(match, ip.frag,
1459 ipv4_key->ipv4_frag, is_mask);
1460 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1461 ipv4_key->ipv4_src, is_mask);
1462 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1463 ipv4_key->ipv4_dst, is_mask);
1464 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
1465 }
1466
1467 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
1468 const struct ovs_key_ipv6 *ipv6_key;
1469
1470 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
1471 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
1472 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
1473 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
1474 return -EINVAL;
1475 }
1476 SW_FLOW_KEY_PUT(match, ipv6.label,
1477 ipv6_key->ipv6_label, is_mask);
1478 SW_FLOW_KEY_PUT(match, ip.proto,
1479 ipv6_key->ipv6_proto, is_mask);
1480 SW_FLOW_KEY_PUT(match, ip.tos,
1481 ipv6_key->ipv6_tclass, is_mask);
1482 SW_FLOW_KEY_PUT(match, ip.ttl,
1483 ipv6_key->ipv6_hlimit, is_mask);
1484 SW_FLOW_KEY_PUT(match, ip.frag,
1485 ipv6_key->ipv6_frag, is_mask);
1486 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
1487 ipv6_key->ipv6_src,
1488 sizeof(match->key->ipv6.addr.src),
1489 is_mask);
1490 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
1491 ipv6_key->ipv6_dst,
1492 sizeof(match->key->ipv6.addr.dst),
1493 is_mask);
1494
1495 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
1496 }
1497
1498 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
1499 const struct ovs_key_arp *arp_key;
1500
1501 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
1502 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
1503 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
1504 arp_key->arp_op);
1505 return -EINVAL;
1506 }
1507
1508 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
1509 arp_key->arp_sip, is_mask);
1510 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
1511 arp_key->arp_tip, is_mask);
1512 SW_FLOW_KEY_PUT(match, ip.proto,
1513 ntohs(arp_key->arp_op), is_mask);
1514 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
1515 arp_key->arp_sha, ETH_ALEN, is_mask);
1516 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
1517 arp_key->arp_tha, ETH_ALEN, is_mask);
1518
1519 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
1520 }
1521
1522 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
1523 const struct ovs_key_tcp *tcp_key;
1524
1525 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
1526 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1527 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1528 tcp_key->tcp_src, is_mask);
1529 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1530 tcp_key->tcp_dst, is_mask);
1531 } else {
1532 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1533 tcp_key->tcp_src, is_mask);
1534 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1535 tcp_key->tcp_dst, is_mask);
1536 }
1537 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
1538 }
1539
1540 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
1541 const struct ovs_key_udp *udp_key;
1542
1543 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
1544 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1545 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1546 udp_key->udp_src, is_mask);
1547 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1548 udp_key->udp_dst, is_mask);
1549 } else {
1550 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1551 udp_key->udp_src, is_mask);
1552 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1553 udp_key->udp_dst, is_mask);
1554 }
1555 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
1556 }
1557
1558 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
1559 const struct ovs_key_sctp *sctp_key;
1560
1561 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
1562 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
1563 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1564 sctp_key->sctp_src, is_mask);
1565 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1566 sctp_key->sctp_dst, is_mask);
1567 } else {
1568 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1569 sctp_key->sctp_src, is_mask);
1570 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1571 sctp_key->sctp_dst, is_mask);
1572 }
1573 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
1574 }
1575
1576 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
1577 const struct ovs_key_icmp *icmp_key;
1578
1579 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
1580 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
1581 htons(icmp_key->icmp_type), is_mask);
1582 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
1583 htons(icmp_key->icmp_code), is_mask);
1584 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
1585 }
1586
1587 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
1588 const struct ovs_key_icmpv6 *icmpv6_key;
1589
1590 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
1591 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
1592 htons(icmpv6_key->icmpv6_type), is_mask);
1593 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
1594 htons(icmpv6_key->icmpv6_code), is_mask);
1595 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
1596 }
1597
1598 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
1599 const struct ovs_key_nd *nd_key;
1600
1601 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
1602 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
1603 nd_key->nd_target,
1604 sizeof(match->key->ipv6.nd.target),
1605 is_mask);
1606 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
1607 nd_key->nd_sll, ETH_ALEN, is_mask);
1608 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
1609 nd_key->nd_tll, ETH_ALEN, is_mask);
1610 attrs &= ~(1 << OVS_KEY_ATTR_ND);
1611 }
1612
1613 if (attrs != 0)
1614 return -EINVAL;
1615
1616 return 0;
1617}
1618
1619/**
1620 * ovs_match_from_nlattrs - parses Netlink attributes into a flow key and
1621 * mask. In case the 'mask' is NULL, the flow is treated as exact match
1622 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
1623 * does not include any don't care bit.
1624 * @match: receives the extracted flow match information.
1625 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1626 * sequence. The fields should of the packet that triggered the creation
1627 * of this flow.
1628 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
1629 * attribute specifies the mask field of the wildcarded flow.
1630 */
1631int ovs_match_from_nlattrs(struct sw_flow_match *match,
1632 const struct nlattr *key,
1633 const struct nlattr *mask)
1634{
1635 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1636 const struct nlattr *encap;
1637 u64 key_attrs = 0;
1638 u64 mask_attrs = 0;
1639 bool encap_valid = false;
1640 int err;
1641
1642 err = parse_flow_nlattrs(key, a, &key_attrs);
1643 if (err)
1644 return err;
1645
1646 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
1647 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
1648 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
1649 __be16 tci;
1650
1651 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
1652 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
1653 OVS_NLERR("Invalid Vlan frame.\n");
1654 return -EINVAL;
1655 }
1656
1657 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1658 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1659 encap = a[OVS_KEY_ATTR_ENCAP];
1660 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1661 encap_valid = true;
1662
1663 if (tci & htons(VLAN_TAG_PRESENT)) {
1664 err = parse_flow_nlattrs(encap, a, &key_attrs);
1665 if (err)
1666 return err;
1667 } else if (!tci) {
1668 /* Corner case for truncated 802.1Q header. */
1669 if (nla_len(encap)) {
1670 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
1671 return -EINVAL;
1672 }
1673 } else {
1674 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
1675 return -EINVAL;
1676 }
1677 }
1678
1679 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
1680 if (err)
1681 return err;
1682
1683 if (mask) {
1684 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
1685 if (err)
1686 return err;
1687
1688 if (mask_attrs & 1ULL << OVS_KEY_ATTR_ENCAP) {
1689 __be16 eth_type = 0;
1690 __be16 tci = 0;
1691
1692 if (!encap_valid) {
1693 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
1694 return -EINVAL;
1695 }
1696
1697 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
1698 if (a[OVS_KEY_ATTR_ETHERTYPE])
1699 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
1700
1701 if (eth_type == htons(0xffff)) {
1702 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
1703 encap = a[OVS_KEY_ATTR_ENCAP];
1704 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
1705 } else {
1706 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
1707 ntohs(eth_type));
1708 return -EINVAL;
1709 }
1710
1711 if (a[OVS_KEY_ATTR_VLAN])
1712 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
1713
1714 if (!(tci & htons(VLAN_TAG_PRESENT))) {
1715 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
1716 return -EINVAL;
1717 }
1718 }
1719
1720 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
1721 if (err)
1722 return err;
1723 } else {
1724 /* Populate exact match flow's key mask. */
1725 if (match->mask)
1726 ovs_sw_flow_mask_set(match->mask, &match->range, 0xff);
1727 }
1728
1729 if (!ovs_match_validate(match, key_attrs, mask_attrs))
1730 return -EINVAL;
1731
1732 return 0;
1733}
1734
1735/**
1736 * ovs_flow_metadata_from_nlattrs - parses Netlink attributes into a flow key.
1737 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
1738 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
1739 * sequence.
1740 *
1741 * This parses a series of Netlink attributes that form a flow key, which must
1742 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
1743 * get the metadata, that is, the parts of the flow key that cannot be
1744 * extracted from the packet itself.
1745 */
1746
1747int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
1748 const struct nlattr *attr)
1749{
1750 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
1751 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
1752 u64 attrs = 0;
1753 int err;
1754 struct sw_flow_match match;
1755
1756 flow->key.phy.in_port = DP_MAX_PORTS;
1757 flow->key.phy.priority = 0;
1758 flow->key.phy.skb_mark = 0;
1759 memset(tun_key, 0, sizeof(flow->key.tun_key));
1760
1761 err = parse_flow_nlattrs(attr, a, &attrs);
1762 if (err)
1763 return -EINVAL;
1764
1765 memset(&match, 0, sizeof(match));
1766 match.key = &flow->key;
1767
1768 err = metadata_from_nlattrs(&match, &attrs, a, false);
1769 if (err)
1770 return err;
1771
1772 return 0;
1773}
1774
1775int ovs_flow_to_nlattrs(const struct sw_flow_key *swkey,
1776 const struct sw_flow_key *output, struct sk_buff *skb)
1777{
1778 struct ovs_key_ethernet *eth_key;
1779 struct nlattr *nla, *encap;
1780 bool is_mask = (swkey != output);
1781
1782 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
1783 goto nla_put_failure;
1784
1785 if ((swkey->tun_key.ipv4_dst || is_mask) &&
1786 ovs_ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
1787 goto nla_put_failure;
1788
1789 if (swkey->phy.in_port == DP_MAX_PORTS) {
1790 if (is_mask && (output->phy.in_port == 0xffff))
1791 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
1792 goto nla_put_failure;
1793 } else {
1794 u16 upper_u16;
1795 upper_u16 = !is_mask ? 0 : 0xffff;
1796
1797 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
1798 (upper_u16 << 16) | output->phy.in_port))
1799 goto nla_put_failure;
1800 }
1801
1802 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
1803 goto nla_put_failure;
1804
1805 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
1806 if (!nla)
1807 goto nla_put_failure;
1808
1809 eth_key = nla_data(nla);
1810 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
1811 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
1812
1813 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
1814 __be16 eth_type;
1815 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
1816 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
1817 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
1818 goto nla_put_failure;
1819 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
1820 if (!swkey->eth.tci)
1821 goto unencap;
1822 } else
1823 encap = NULL;
1824
1825 if (swkey->eth.type == htons(ETH_P_802_2)) {
1826 /*
1827 * Ethertype 802.2 is represented in the netlink with omitted
1828 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
1829 * 0xffff in the mask attribute. Ethertype can also
1830 * be wildcarded.
1831 */
1832 if (is_mask && output->eth.type)
1833 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
1834 output->eth.type))
1835 goto nla_put_failure;
1836 goto unencap;
1837 }
1838
1839 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
1840 goto nla_put_failure;
1841
1842 if (swkey->eth.type == htons(ETH_P_IP)) {
1843 struct ovs_key_ipv4 *ipv4_key;
1844
1845 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
1846 if (!nla)
1847 goto nla_put_failure;
1848 ipv4_key = nla_data(nla);
1849 ipv4_key->ipv4_src = output->ipv4.addr.src;
1850 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
1851 ipv4_key->ipv4_proto = output->ip.proto;
1852 ipv4_key->ipv4_tos = output->ip.tos;
1853 ipv4_key->ipv4_ttl = output->ip.ttl;
1854 ipv4_key->ipv4_frag = output->ip.frag;
1855 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1856 struct ovs_key_ipv6 *ipv6_key;
1857
1858 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
1859 if (!nla)
1860 goto nla_put_failure;
1861 ipv6_key = nla_data(nla);
1862 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
1863 sizeof(ipv6_key->ipv6_src));
1864 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
1865 sizeof(ipv6_key->ipv6_dst));
1866 ipv6_key->ipv6_label = output->ipv6.label;
1867 ipv6_key->ipv6_proto = output->ip.proto;
1868 ipv6_key->ipv6_tclass = output->ip.tos;
1869 ipv6_key->ipv6_hlimit = output->ip.ttl;
1870 ipv6_key->ipv6_frag = output->ip.frag;
1871 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
1872 swkey->eth.type == htons(ETH_P_RARP)) {
1873 struct ovs_key_arp *arp_key;
1874
1875 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1876 if (!nla)
1877 goto nla_put_failure;
1878 arp_key = nla_data(nla);
1879 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1880 arp_key->arp_sip = output->ipv4.addr.src;
1881 arp_key->arp_tip = output->ipv4.addr.dst;
1882 arp_key->arp_op = htons(output->ip.proto);
1883 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1884 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1885 }
1886
1887 if ((swkey->eth.type == htons(ETH_P_IP) ||
1888 swkey->eth.type == htons(ETH_P_IPV6)) &&
1889 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1890
1891 if (swkey->ip.proto == IPPROTO_TCP) {
1892 struct ovs_key_tcp *tcp_key;
1893
1894 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1895 if (!nla)
1896 goto nla_put_failure;
1897 tcp_key = nla_data(nla);
1898 if (swkey->eth.type == htons(ETH_P_IP)) {
1899 tcp_key->tcp_src = output->ipv4.tp.src;
1900 tcp_key->tcp_dst = output->ipv4.tp.dst;
1901 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1902 tcp_key->tcp_src = output->ipv6.tp.src;
1903 tcp_key->tcp_dst = output->ipv6.tp.dst;
1904 }
1905 } else if (swkey->ip.proto == IPPROTO_UDP) {
1906 struct ovs_key_udp *udp_key;
1907
1908 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1909 if (!nla)
1910 goto nla_put_failure;
1911 udp_key = nla_data(nla);
1912 if (swkey->eth.type == htons(ETH_P_IP)) {
1913 udp_key->udp_src = output->ipv4.tp.src;
1914 udp_key->udp_dst = output->ipv4.tp.dst;
1915 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1916 udp_key->udp_src = output->ipv6.tp.src;
1917 udp_key->udp_dst = output->ipv6.tp.dst;
1918 }
1919 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1920 struct ovs_key_sctp *sctp_key;
1921
1922 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1923 if (!nla)
1924 goto nla_put_failure;
1925 sctp_key = nla_data(nla);
1926 if (swkey->eth.type == htons(ETH_P_IP)) {
1927 sctp_key->sctp_src = swkey->ipv4.tp.src;
1928 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1929 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1930 sctp_key->sctp_src = swkey->ipv6.tp.src;
1931 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1932 }
1933 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1934 swkey->ip.proto == IPPROTO_ICMP) {
1935 struct ovs_key_icmp *icmp_key;
1936
1937 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1938 if (!nla)
1939 goto nla_put_failure;
1940 icmp_key = nla_data(nla);
1941 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1942 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1943 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1944 swkey->ip.proto == IPPROTO_ICMPV6) {
1945 struct ovs_key_icmpv6 *icmpv6_key;
1946
1947 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1948 sizeof(*icmpv6_key));
1949 if (!nla)
1950 goto nla_put_failure;
1951 icmpv6_key = nla_data(nla);
1952 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1953 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1954
1955 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1956 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1957 struct ovs_key_nd *nd_key;
1958
1959 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1960 if (!nla)
1961 goto nla_put_failure;
1962 nd_key = nla_data(nla);
1963 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1964 sizeof(nd_key->nd_target));
1965 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1966 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1967 }
1968 }
1969 }
1970
1971unencap:
1972 if (encap)
1973 nla_nest_end(skb, encap);
1974
1975 return 0;
1976
1977nla_put_failure:
1978 return -EMSGSIZE;
1979}
1980
1981/* Initializes the flow module.
1982 * Returns zero if successful or a negative error code. */
1983int ovs_flow_init(void)
1984{
1985 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
1986 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
1987
1988 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
1989 0, NULL);
1990 if (flow_cache == NULL)
1991 return -ENOMEM;
1992
1993 return 0;
1994}
1995
1996/* Uninitializes the flow module. */
1997void ovs_flow_exit(void)
1998{
1999 kmem_cache_destroy(flow_cache);
2000}
2001
2002struct sw_flow_mask *ovs_sw_flow_mask_alloc(void)
2003{
2004 struct sw_flow_mask *mask;
2005
2006 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
2007 if (mask)
2008 mask->ref_count = 0;
2009
2010 return mask;
2011}
2012
2013void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *mask)
2014{
2015 mask->ref_count++;
2016}
2017
2018void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
2019{
2020 if (!mask)
2021 return;
2022
2023 BUG_ON(!mask->ref_count);
2024 mask->ref_count--;
2025
2026 if (!mask->ref_count) {
2027 list_del_rcu(&mask->list);
2028 if (deferred)
2029 kfree_rcu(mask, rcu);
2030 else
2031 kfree(mask);
2032 }
2033}
2034
2035static bool ovs_sw_flow_mask_equal(const struct sw_flow_mask *a,
2036 const struct sw_flow_mask *b)
2037{
2038 u8 *a_ = (u8 *)&a->key + a->range.start;
2039 u8 *b_ = (u8 *)&b->key + b->range.start;
2040
2041 return (a->range.end == b->range.end)
2042 && (a->range.start == b->range.start)
2043 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
2044}
2045
2046struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *tbl,
2047 const struct sw_flow_mask *mask)
2048{
2049 struct list_head *ml;
2050
2051 list_for_each(ml, tbl->mask_list) {
2052 struct sw_flow_mask *m;
2053 m = container_of(ml, struct sw_flow_mask, list);
2054 if (ovs_sw_flow_mask_equal(mask, m))
2055 return m;
2056 }
2057
2058 return NULL;
2059}
2060
2061/**
2062 * add a new mask into the mask list.
2063 * The caller needs to make sure that 'mask' is not the same
2064 * as any masks that are already on the list.
2065 */
2066void ovs_sw_flow_mask_insert(struct flow_table *tbl, struct sw_flow_mask *mask)
2067{
2068 list_add_rcu(&mask->list, tbl->mask_list);
2069}
2070
2071/**
2072 * Set 'range' fields in the mask to the value of 'val'.
2073 */
2074static void ovs_sw_flow_mask_set(struct sw_flow_mask *mask,
2075 struct sw_flow_key_range *range, u8 val)
2076{
2077 u8 *m = (u8 *)&mask->key + range->start;
2078
2079 mask->range = *range;
2080 memset(m, val, range_n_bytes(range));
2081}
diff --git a/net/openvswitch/flow.h b/net/openvswitch/flow.h
index 212fbf7510c4..1510f51dbf74 100644
--- a/net/openvswitch/flow.h
+++ b/net/openvswitch/flow.h
@@ -33,14 +33,6 @@
33#include <net/inet_ecn.h> 33#include <net/inet_ecn.h>
34 34
35struct sk_buff; 35struct sk_buff;
36struct sw_flow_mask;
37struct flow_table;
38
39struct sw_flow_actions {
40 struct rcu_head rcu;
41 u32 actions_len;
42 struct nlattr actions[];
43};
44 36
45/* Used to memset ovs_key_ipv4_tunnel padding. */ 37/* Used to memset ovs_key_ipv4_tunnel padding. */
46#define OVS_TUNNEL_KEY_SIZE \ 38#define OVS_TUNNEL_KEY_SIZE \
@@ -101,6 +93,7 @@ struct sw_flow_key {
101 struct { 93 struct {
102 __be16 src; /* TCP/UDP/SCTP source port. */ 94 __be16 src; /* TCP/UDP/SCTP source port. */
103 __be16 dst; /* TCP/UDP/SCTP destination port. */ 95 __be16 dst; /* TCP/UDP/SCTP destination port. */
96 __be16 flags; /* TCP flags. */
104 } tp; 97 } tp;
105 struct { 98 struct {
106 u8 sha[ETH_ALEN]; /* ARP source hardware address. */ 99 u8 sha[ETH_ALEN]; /* ARP source hardware address. */
@@ -117,6 +110,7 @@ struct sw_flow_key {
117 struct { 110 struct {
118 __be16 src; /* TCP/UDP/SCTP source port. */ 111 __be16 src; /* TCP/UDP/SCTP source port. */
119 __be16 dst; /* TCP/UDP/SCTP destination port. */ 112 __be16 dst; /* TCP/UDP/SCTP destination port. */
113 __be16 flags; /* TCP flags. */
120 } tp; 114 } tp;
121 struct { 115 struct {
122 struct in6_addr target; /* ND target address. */ 116 struct in6_addr target; /* ND target address. */
@@ -127,6 +121,31 @@ struct sw_flow_key {
127 }; 121 };
128} __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */ 122} __aligned(BITS_PER_LONG/8); /* Ensure that we can do comparisons as longs. */
129 123
124struct sw_flow_key_range {
125 size_t start;
126 size_t end;
127};
128
129struct sw_flow_mask {
130 int ref_count;
131 struct rcu_head rcu;
132 struct list_head list;
133 struct sw_flow_key_range range;
134 struct sw_flow_key key;
135};
136
137struct sw_flow_match {
138 struct sw_flow_key *key;
139 struct sw_flow_key_range range;
140 struct sw_flow_mask *mask;
141};
142
143struct sw_flow_actions {
144 struct rcu_head rcu;
145 u32 actions_len;
146 struct nlattr actions[];
147};
148
130struct sw_flow { 149struct sw_flow {
131 struct rcu_head rcu; 150 struct rcu_head rcu;
132 struct hlist_node hash_node[2]; 151 struct hlist_node hash_node[2];
@@ -141,23 +160,9 @@ struct sw_flow {
141 unsigned long used; /* Last used time (in jiffies). */ 160 unsigned long used; /* Last used time (in jiffies). */
142 u64 packet_count; /* Number of packets matched. */ 161 u64 packet_count; /* Number of packets matched. */
143 u64 byte_count; /* Number of bytes matched. */ 162 u64 byte_count; /* Number of bytes matched. */
144 u8 tcp_flags; /* Union of seen TCP flags. */ 163 __be16 tcp_flags; /* Union of seen TCP flags. */
145};
146
147struct sw_flow_key_range {
148 size_t start;
149 size_t end;
150}; 164};
151 165
152struct sw_flow_match {
153 struct sw_flow_key *key;
154 struct sw_flow_key_range range;
155 struct sw_flow_mask *mask;
156};
157
158void ovs_match_init(struct sw_flow_match *match,
159 struct sw_flow_key *key, struct sw_flow_mask *mask);
160
161struct arp_eth_header { 166struct arp_eth_header {
162 __be16 ar_hrd; /* format of hardware address */ 167 __be16 ar_hrd; /* format of hardware address */
163 __be16 ar_pro; /* format of protocol address */ 168 __be16 ar_pro; /* format of protocol address */
@@ -172,88 +177,9 @@ struct arp_eth_header {
172 unsigned char ar_tip[4]; /* target IP address */ 177 unsigned char ar_tip[4]; /* target IP address */
173} __packed; 178} __packed;
174 179
175int ovs_flow_init(void);
176void ovs_flow_exit(void);
177
178struct sw_flow *ovs_flow_alloc(void);
179void ovs_flow_deferred_free(struct sw_flow *);
180void ovs_flow_free(struct sw_flow *, bool deferred);
181
182struct sw_flow_actions *ovs_flow_actions_alloc(int actions_len);
183void ovs_flow_deferred_free_acts(struct sw_flow_actions *);
184
185int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
186void ovs_flow_used(struct sw_flow *, struct sk_buff *); 180void ovs_flow_used(struct sw_flow *, struct sk_buff *);
187u64 ovs_flow_used_time(unsigned long flow_jiffies); 181u64 ovs_flow_used_time(unsigned long flow_jiffies);
188int ovs_flow_to_nlattrs(const struct sw_flow_key *,
189 const struct sw_flow_key *, struct sk_buff *);
190int ovs_match_from_nlattrs(struct sw_flow_match *match,
191 const struct nlattr *,
192 const struct nlattr *);
193int ovs_flow_metadata_from_nlattrs(struct sw_flow *flow,
194 const struct nlattr *attr);
195 182
196#define MAX_ACTIONS_BUFSIZE (32 * 1024) 183int ovs_flow_extract(struct sk_buff *, u16 in_port, struct sw_flow_key *);
197#define TBL_MIN_BUCKETS 1024
198
199struct flow_table {
200 struct flex_array *buckets;
201 unsigned int count, n_buckets;
202 struct rcu_head rcu;
203 struct list_head *mask_list;
204 int node_ver;
205 u32 hash_seed;
206 bool keep_flows;
207};
208
209static inline int ovs_flow_tbl_count(struct flow_table *table)
210{
211 return table->count;
212}
213
214static inline int ovs_flow_tbl_need_to_expand(struct flow_table *table)
215{
216 return (table->count > table->n_buckets);
217}
218
219struct sw_flow *ovs_flow_lookup(struct flow_table *,
220 const struct sw_flow_key *);
221struct sw_flow *ovs_flow_lookup_unmasked_key(struct flow_table *table,
222 struct sw_flow_match *match);
223
224void ovs_flow_tbl_destroy(struct flow_table *table, bool deferred);
225struct flow_table *ovs_flow_tbl_alloc(int new_size);
226struct flow_table *ovs_flow_tbl_expand(struct flow_table *table);
227struct flow_table *ovs_flow_tbl_rehash(struct flow_table *table);
228
229void ovs_flow_insert(struct flow_table *table, struct sw_flow *flow);
230void ovs_flow_remove(struct flow_table *table, struct sw_flow *flow);
231
232struct sw_flow *ovs_flow_dump_next(struct flow_table *table, u32 *bucket, u32 *idx);
233extern const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1];
234int ovs_ipv4_tun_from_nlattr(const struct nlattr *attr,
235 struct sw_flow_match *match, bool is_mask);
236int ovs_ipv4_tun_to_nlattr(struct sk_buff *skb,
237 const struct ovs_key_ipv4_tunnel *tun_key,
238 const struct ovs_key_ipv4_tunnel *output);
239
240bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
241 const struct sw_flow_key *key, int key_end);
242
243struct sw_flow_mask {
244 int ref_count;
245 struct rcu_head rcu;
246 struct list_head list;
247 struct sw_flow_key_range range;
248 struct sw_flow_key key;
249};
250 184
251struct sw_flow_mask *ovs_sw_flow_mask_alloc(void);
252void ovs_sw_flow_mask_add_ref(struct sw_flow_mask *);
253void ovs_sw_flow_mask_del_ref(struct sw_flow_mask *, bool deferred);
254void ovs_sw_flow_mask_insert(struct flow_table *, struct sw_flow_mask *);
255struct sw_flow_mask *ovs_sw_flow_mask_find(const struct flow_table *,
256 const struct sw_flow_mask *);
257void ovs_flow_key_mask(struct sw_flow_key *dst, const struct sw_flow_key *src,
258 const struct sw_flow_mask *mask);
259#endif /* flow.h */ 185#endif /* flow.h */
diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c
new file mode 100644
index 000000000000..2bc1bc1aca3b
--- /dev/null
+++ b/net/openvswitch/flow_netlink.c
@@ -0,0 +1,1630 @@
1/*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#include "flow.h"
20#include "datapath.h"
21#include <linux/uaccess.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
26#include <net/llc_pdu.h>
27#include <linux/kernel.h>
28#include <linux/jhash.h>
29#include <linux/jiffies.h>
30#include <linux/llc.h>
31#include <linux/module.h>
32#include <linux/in.h>
33#include <linux/rcupdate.h>
34#include <linux/if_arp.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/sctp.h>
38#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
41#include <linux/icmpv6.h>
42#include <linux/rculist.h>
43#include <net/ip.h>
44#include <net/ipv6.h>
45#include <net/ndisc.h>
46
47#include "flow_netlink.h"
48
49static void update_range__(struct sw_flow_match *match,
50 size_t offset, size_t size, bool is_mask)
51{
52 struct sw_flow_key_range *range = NULL;
53 size_t start = rounddown(offset, sizeof(long));
54 size_t end = roundup(offset + size, sizeof(long));
55
56 if (!is_mask)
57 range = &match->range;
58 else if (match->mask)
59 range = &match->mask->range;
60
61 if (!range)
62 return;
63
64 if (range->start == range->end) {
65 range->start = start;
66 range->end = end;
67 return;
68 }
69
70 if (range->start > start)
71 range->start = start;
72
73 if (range->end < end)
74 range->end = end;
75}
76
77#define SW_FLOW_KEY_PUT(match, field, value, is_mask) \
78 do { \
79 update_range__(match, offsetof(struct sw_flow_key, field), \
80 sizeof((match)->key->field), is_mask); \
81 if (is_mask) { \
82 if ((match)->mask) \
83 (match)->mask->key.field = value; \
84 } else { \
85 (match)->key->field = value; \
86 } \
87 } while (0)
88
89#define SW_FLOW_KEY_MEMCPY(match, field, value_p, len, is_mask) \
90 do { \
91 update_range__(match, offsetof(struct sw_flow_key, field), \
92 len, is_mask); \
93 if (is_mask) { \
94 if ((match)->mask) \
95 memcpy(&(match)->mask->key.field, value_p, len);\
96 } else { \
97 memcpy(&(match)->key->field, value_p, len); \
98 } \
99 } while (0)
100
101static u16 range_n_bytes(const struct sw_flow_key_range *range)
102{
103 return range->end - range->start;
104}
105
106static bool match_validate(const struct sw_flow_match *match,
107 u64 key_attrs, u64 mask_attrs)
108{
109 u64 key_expected = 1 << OVS_KEY_ATTR_ETHERNET;
110 u64 mask_allowed = key_attrs; /* At most allow all key attributes */
111
112 /* The following mask attributes allowed only if they
113 * pass the validation tests. */
114 mask_allowed &= ~((1 << OVS_KEY_ATTR_IPV4)
115 | (1 << OVS_KEY_ATTR_IPV6)
116 | (1 << OVS_KEY_ATTR_TCP)
117 | (1 << OVS_KEY_ATTR_TCP_FLAGS)
118 | (1 << OVS_KEY_ATTR_UDP)
119 | (1 << OVS_KEY_ATTR_SCTP)
120 | (1 << OVS_KEY_ATTR_ICMP)
121 | (1 << OVS_KEY_ATTR_ICMPV6)
122 | (1 << OVS_KEY_ATTR_ARP)
123 | (1 << OVS_KEY_ATTR_ND));
124
125 /* Always allowed mask fields. */
126 mask_allowed |= ((1 << OVS_KEY_ATTR_TUNNEL)
127 | (1 << OVS_KEY_ATTR_IN_PORT)
128 | (1 << OVS_KEY_ATTR_ETHERTYPE));
129
130 /* Check key attributes. */
131 if (match->key->eth.type == htons(ETH_P_ARP)
132 || match->key->eth.type == htons(ETH_P_RARP)) {
133 key_expected |= 1 << OVS_KEY_ATTR_ARP;
134 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
135 mask_allowed |= 1 << OVS_KEY_ATTR_ARP;
136 }
137
138 if (match->key->eth.type == htons(ETH_P_IP)) {
139 key_expected |= 1 << OVS_KEY_ATTR_IPV4;
140 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
141 mask_allowed |= 1 << OVS_KEY_ATTR_IPV4;
142
143 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
144 if (match->key->ip.proto == IPPROTO_UDP) {
145 key_expected |= 1 << OVS_KEY_ATTR_UDP;
146 if (match->mask && (match->mask->key.ip.proto == 0xff))
147 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
148 }
149
150 if (match->key->ip.proto == IPPROTO_SCTP) {
151 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
152 if (match->mask && (match->mask->key.ip.proto == 0xff))
153 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
154 }
155
156 if (match->key->ip.proto == IPPROTO_TCP) {
157 key_expected |= 1 << OVS_KEY_ATTR_TCP;
158 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
159 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
160 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
161 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
162 }
163 }
164
165 if (match->key->ip.proto == IPPROTO_ICMP) {
166 key_expected |= 1 << OVS_KEY_ATTR_ICMP;
167 if (match->mask && (match->mask->key.ip.proto == 0xff))
168 mask_allowed |= 1 << OVS_KEY_ATTR_ICMP;
169 }
170 }
171 }
172
173 if (match->key->eth.type == htons(ETH_P_IPV6)) {
174 key_expected |= 1 << OVS_KEY_ATTR_IPV6;
175 if (match->mask && (match->mask->key.eth.type == htons(0xffff)))
176 mask_allowed |= 1 << OVS_KEY_ATTR_IPV6;
177
178 if (match->key->ip.frag != OVS_FRAG_TYPE_LATER) {
179 if (match->key->ip.proto == IPPROTO_UDP) {
180 key_expected |= 1 << OVS_KEY_ATTR_UDP;
181 if (match->mask && (match->mask->key.ip.proto == 0xff))
182 mask_allowed |= 1 << OVS_KEY_ATTR_UDP;
183 }
184
185 if (match->key->ip.proto == IPPROTO_SCTP) {
186 key_expected |= 1 << OVS_KEY_ATTR_SCTP;
187 if (match->mask && (match->mask->key.ip.proto == 0xff))
188 mask_allowed |= 1 << OVS_KEY_ATTR_SCTP;
189 }
190
191 if (match->key->ip.proto == IPPROTO_TCP) {
192 key_expected |= 1 << OVS_KEY_ATTR_TCP;
193 key_expected |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
194 if (match->mask && (match->mask->key.ip.proto == 0xff)) {
195 mask_allowed |= 1 << OVS_KEY_ATTR_TCP;
196 mask_allowed |= 1 << OVS_KEY_ATTR_TCP_FLAGS;
197 }
198 }
199
200 if (match->key->ip.proto == IPPROTO_ICMPV6) {
201 key_expected |= 1 << OVS_KEY_ATTR_ICMPV6;
202 if (match->mask && (match->mask->key.ip.proto == 0xff))
203 mask_allowed |= 1 << OVS_KEY_ATTR_ICMPV6;
204
205 if (match->key->ipv6.tp.src ==
206 htons(NDISC_NEIGHBOUR_SOLICITATION) ||
207 match->key->ipv6.tp.src == htons(NDISC_NEIGHBOUR_ADVERTISEMENT)) {
208 key_expected |= 1 << OVS_KEY_ATTR_ND;
209 if (match->mask && (match->mask->key.ipv6.tp.src == htons(0xffff)))
210 mask_allowed |= 1 << OVS_KEY_ATTR_ND;
211 }
212 }
213 }
214 }
215
216 if ((key_attrs & key_expected) != key_expected) {
217 /* Key attributes check failed. */
218 OVS_NLERR("Missing expected key attributes (key_attrs=%llx, expected=%llx).\n",
219 key_attrs, key_expected);
220 return false;
221 }
222
223 if ((mask_attrs & mask_allowed) != mask_attrs) {
224 /* Mask attributes check failed. */
225 OVS_NLERR("Contain more than allowed mask fields (mask_attrs=%llx, mask_allowed=%llx).\n",
226 mask_attrs, mask_allowed);
227 return false;
228 }
229
230 return true;
231}
232
233/* The size of the argument for each %OVS_KEY_ATTR_* Netlink attribute. */
234static const int ovs_key_lens[OVS_KEY_ATTR_MAX + 1] = {
235 [OVS_KEY_ATTR_ENCAP] = -1,
236 [OVS_KEY_ATTR_PRIORITY] = sizeof(u32),
237 [OVS_KEY_ATTR_IN_PORT] = sizeof(u32),
238 [OVS_KEY_ATTR_SKB_MARK] = sizeof(u32),
239 [OVS_KEY_ATTR_ETHERNET] = sizeof(struct ovs_key_ethernet),
240 [OVS_KEY_ATTR_VLAN] = sizeof(__be16),
241 [OVS_KEY_ATTR_ETHERTYPE] = sizeof(__be16),
242 [OVS_KEY_ATTR_IPV4] = sizeof(struct ovs_key_ipv4),
243 [OVS_KEY_ATTR_IPV6] = sizeof(struct ovs_key_ipv6),
244 [OVS_KEY_ATTR_TCP] = sizeof(struct ovs_key_tcp),
245 [OVS_KEY_ATTR_TCP_FLAGS] = sizeof(__be16),
246 [OVS_KEY_ATTR_UDP] = sizeof(struct ovs_key_udp),
247 [OVS_KEY_ATTR_SCTP] = sizeof(struct ovs_key_sctp),
248 [OVS_KEY_ATTR_ICMP] = sizeof(struct ovs_key_icmp),
249 [OVS_KEY_ATTR_ICMPV6] = sizeof(struct ovs_key_icmpv6),
250 [OVS_KEY_ATTR_ARP] = sizeof(struct ovs_key_arp),
251 [OVS_KEY_ATTR_ND] = sizeof(struct ovs_key_nd),
252 [OVS_KEY_ATTR_TUNNEL] = -1,
253};
254
255static bool is_all_zero(const u8 *fp, size_t size)
256{
257 int i;
258
259 if (!fp)
260 return false;
261
262 for (i = 0; i < size; i++)
263 if (fp[i])
264 return false;
265
266 return true;
267}
268
269static int __parse_flow_nlattrs(const struct nlattr *attr,
270 const struct nlattr *a[],
271 u64 *attrsp, bool nz)
272{
273 const struct nlattr *nla;
274 u64 attrs;
275 int rem;
276
277 attrs = *attrsp;
278 nla_for_each_nested(nla, attr, rem) {
279 u16 type = nla_type(nla);
280 int expected_len;
281
282 if (type > OVS_KEY_ATTR_MAX) {
283 OVS_NLERR("Unknown key attribute (type=%d, max=%d).\n",
284 type, OVS_KEY_ATTR_MAX);
285 return -EINVAL;
286 }
287
288 if (attrs & (1 << type)) {
289 OVS_NLERR("Duplicate key attribute (type %d).\n", type);
290 return -EINVAL;
291 }
292
293 expected_len = ovs_key_lens[type];
294 if (nla_len(nla) != expected_len && expected_len != -1) {
295 OVS_NLERR("Key attribute has unexpected length (type=%d"
296 ", length=%d, expected=%d).\n", type,
297 nla_len(nla), expected_len);
298 return -EINVAL;
299 }
300
301 if (!nz || !is_all_zero(nla_data(nla), expected_len)) {
302 attrs |= 1 << type;
303 a[type] = nla;
304 }
305 }
306 if (rem) {
307 OVS_NLERR("Message has %d unknown bytes.\n", rem);
308 return -EINVAL;
309 }
310
311 *attrsp = attrs;
312 return 0;
313}
314
315static int parse_flow_mask_nlattrs(const struct nlattr *attr,
316 const struct nlattr *a[], u64 *attrsp)
317{
318 return __parse_flow_nlattrs(attr, a, attrsp, true);
319}
320
321static int parse_flow_nlattrs(const struct nlattr *attr,
322 const struct nlattr *a[], u64 *attrsp)
323{
324 return __parse_flow_nlattrs(attr, a, attrsp, false);
325}
326
327static int ipv4_tun_from_nlattr(const struct nlattr *attr,
328 struct sw_flow_match *match, bool is_mask)
329{
330 struct nlattr *a;
331 int rem;
332 bool ttl = false;
333 __be16 tun_flags = 0;
334
335 nla_for_each_nested(a, attr, rem) {
336 int type = nla_type(a);
337 static const u32 ovs_tunnel_key_lens[OVS_TUNNEL_KEY_ATTR_MAX + 1] = {
338 [OVS_TUNNEL_KEY_ATTR_ID] = sizeof(u64),
339 [OVS_TUNNEL_KEY_ATTR_IPV4_SRC] = sizeof(u32),
340 [OVS_TUNNEL_KEY_ATTR_IPV4_DST] = sizeof(u32),
341 [OVS_TUNNEL_KEY_ATTR_TOS] = 1,
342 [OVS_TUNNEL_KEY_ATTR_TTL] = 1,
343 [OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT] = 0,
344 [OVS_TUNNEL_KEY_ATTR_CSUM] = 0,
345 };
346
347 if (type > OVS_TUNNEL_KEY_ATTR_MAX) {
348 OVS_NLERR("Unknown IPv4 tunnel attribute (type=%d, max=%d).\n",
349 type, OVS_TUNNEL_KEY_ATTR_MAX);
350 return -EINVAL;
351 }
352
353 if (ovs_tunnel_key_lens[type] != nla_len(a)) {
354 OVS_NLERR("IPv4 tunnel attribute type has unexpected "
355 " length (type=%d, length=%d, expected=%d).\n",
356 type, nla_len(a), ovs_tunnel_key_lens[type]);
357 return -EINVAL;
358 }
359
360 switch (type) {
361 case OVS_TUNNEL_KEY_ATTR_ID:
362 SW_FLOW_KEY_PUT(match, tun_key.tun_id,
363 nla_get_be64(a), is_mask);
364 tun_flags |= TUNNEL_KEY;
365 break;
366 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
367 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
368 nla_get_be32(a), is_mask);
369 break;
370 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
371 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
372 nla_get_be32(a), is_mask);
373 break;
374 case OVS_TUNNEL_KEY_ATTR_TOS:
375 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
376 nla_get_u8(a), is_mask);
377 break;
378 case OVS_TUNNEL_KEY_ATTR_TTL:
379 SW_FLOW_KEY_PUT(match, tun_key.ipv4_ttl,
380 nla_get_u8(a), is_mask);
381 ttl = true;
382 break;
383 case OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT:
384 tun_flags |= TUNNEL_DONT_FRAGMENT;
385 break;
386 case OVS_TUNNEL_KEY_ATTR_CSUM:
387 tun_flags |= TUNNEL_CSUM;
388 break;
389 default:
390 return -EINVAL;
391 }
392 }
393
394 SW_FLOW_KEY_PUT(match, tun_key.tun_flags, tun_flags, is_mask);
395
396 if (rem > 0) {
397 OVS_NLERR("IPv4 tunnel attribute has %d unknown bytes.\n", rem);
398 return -EINVAL;
399 }
400
401 if (!is_mask) {
402 if (!match->key->tun_key.ipv4_dst) {
403 OVS_NLERR("IPv4 tunnel destination address is zero.\n");
404 return -EINVAL;
405 }
406
407 if (!ttl) {
408 OVS_NLERR("IPv4 tunnel TTL not specified.\n");
409 return -EINVAL;
410 }
411 }
412
413 return 0;
414}
415
416static int ipv4_tun_to_nlattr(struct sk_buff *skb,
417 const struct ovs_key_ipv4_tunnel *tun_key,
418 const struct ovs_key_ipv4_tunnel *output)
419{
420 struct nlattr *nla;
421
422 nla = nla_nest_start(skb, OVS_KEY_ATTR_TUNNEL);
423 if (!nla)
424 return -EMSGSIZE;
425
426 if (output->tun_flags & TUNNEL_KEY &&
427 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
428 return -EMSGSIZE;
429 if (output->ipv4_src &&
430 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src))
431 return -EMSGSIZE;
432 if (output->ipv4_dst &&
433 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst))
434 return -EMSGSIZE;
435 if (output->ipv4_tos &&
436 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
437 return -EMSGSIZE;
438 if (nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TTL, output->ipv4_ttl))
439 return -EMSGSIZE;
440 if ((output->tun_flags & TUNNEL_DONT_FRAGMENT) &&
441 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
442 return -EMSGSIZE;
443 if ((output->tun_flags & TUNNEL_CSUM) &&
444 nla_put_flag(skb, OVS_TUNNEL_KEY_ATTR_CSUM))
445 return -EMSGSIZE;
446
447 nla_nest_end(skb, nla);
448 return 0;
449}
450
451
452static int metadata_from_nlattrs(struct sw_flow_match *match, u64 *attrs,
453 const struct nlattr **a, bool is_mask)
454{
455 if (*attrs & (1 << OVS_KEY_ATTR_PRIORITY)) {
456 SW_FLOW_KEY_PUT(match, phy.priority,
457 nla_get_u32(a[OVS_KEY_ATTR_PRIORITY]), is_mask);
458 *attrs &= ~(1 << OVS_KEY_ATTR_PRIORITY);
459 }
460
461 if (*attrs & (1 << OVS_KEY_ATTR_IN_PORT)) {
462 u32 in_port = nla_get_u32(a[OVS_KEY_ATTR_IN_PORT]);
463
464 if (is_mask)
465 in_port = 0xffffffff; /* Always exact match in_port. */
466 else if (in_port >= DP_MAX_PORTS)
467 return -EINVAL;
468
469 SW_FLOW_KEY_PUT(match, phy.in_port, in_port, is_mask);
470 *attrs &= ~(1 << OVS_KEY_ATTR_IN_PORT);
471 } else if (!is_mask) {
472 SW_FLOW_KEY_PUT(match, phy.in_port, DP_MAX_PORTS, is_mask);
473 }
474
475 if (*attrs & (1 << OVS_KEY_ATTR_SKB_MARK)) {
476 uint32_t mark = nla_get_u32(a[OVS_KEY_ATTR_SKB_MARK]);
477
478 SW_FLOW_KEY_PUT(match, phy.skb_mark, mark, is_mask);
479 *attrs &= ~(1 << OVS_KEY_ATTR_SKB_MARK);
480 }
481 if (*attrs & (1 << OVS_KEY_ATTR_TUNNEL)) {
482 if (ipv4_tun_from_nlattr(a[OVS_KEY_ATTR_TUNNEL], match,
483 is_mask))
484 return -EINVAL;
485 *attrs &= ~(1 << OVS_KEY_ATTR_TUNNEL);
486 }
487 return 0;
488}
489
490static int ovs_key_from_nlattrs(struct sw_flow_match *match, u64 attrs,
491 const struct nlattr **a, bool is_mask)
492{
493 int err;
494 u64 orig_attrs = attrs;
495
496 err = metadata_from_nlattrs(match, &attrs, a, is_mask);
497 if (err)
498 return err;
499
500 if (attrs & (1 << OVS_KEY_ATTR_ETHERNET)) {
501 const struct ovs_key_ethernet *eth_key;
502
503 eth_key = nla_data(a[OVS_KEY_ATTR_ETHERNET]);
504 SW_FLOW_KEY_MEMCPY(match, eth.src,
505 eth_key->eth_src, ETH_ALEN, is_mask);
506 SW_FLOW_KEY_MEMCPY(match, eth.dst,
507 eth_key->eth_dst, ETH_ALEN, is_mask);
508 attrs &= ~(1 << OVS_KEY_ATTR_ETHERNET);
509 }
510
511 if (attrs & (1 << OVS_KEY_ATTR_VLAN)) {
512 __be16 tci;
513
514 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
515 if (!(tci & htons(VLAN_TAG_PRESENT))) {
516 if (is_mask)
517 OVS_NLERR("VLAN TCI mask does not have exact match for VLAN_TAG_PRESENT bit.\n");
518 else
519 OVS_NLERR("VLAN TCI does not have VLAN_TAG_PRESENT bit set.\n");
520
521 return -EINVAL;
522 }
523
524 SW_FLOW_KEY_PUT(match, eth.tci, tci, is_mask);
525 attrs &= ~(1 << OVS_KEY_ATTR_VLAN);
526 } else if (!is_mask)
527 SW_FLOW_KEY_PUT(match, eth.tci, htons(0xffff), true);
528
529 if (attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) {
530 __be16 eth_type;
531
532 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
533 if (is_mask) {
534 /* Always exact match EtherType. */
535 eth_type = htons(0xffff);
536 } else if (ntohs(eth_type) < ETH_P_802_3_MIN) {
537 OVS_NLERR("EtherType is less than minimum (type=%x, min=%x).\n",
538 ntohs(eth_type), ETH_P_802_3_MIN);
539 return -EINVAL;
540 }
541
542 SW_FLOW_KEY_PUT(match, eth.type, eth_type, is_mask);
543 attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
544 } else if (!is_mask) {
545 SW_FLOW_KEY_PUT(match, eth.type, htons(ETH_P_802_2), is_mask);
546 }
547
548 if (attrs & (1 << OVS_KEY_ATTR_IPV4)) {
549 const struct ovs_key_ipv4 *ipv4_key;
550
551 ipv4_key = nla_data(a[OVS_KEY_ATTR_IPV4]);
552 if (!is_mask && ipv4_key->ipv4_frag > OVS_FRAG_TYPE_MAX) {
553 OVS_NLERR("Unknown IPv4 fragment type (value=%d, max=%d).\n",
554 ipv4_key->ipv4_frag, OVS_FRAG_TYPE_MAX);
555 return -EINVAL;
556 }
557 SW_FLOW_KEY_PUT(match, ip.proto,
558 ipv4_key->ipv4_proto, is_mask);
559 SW_FLOW_KEY_PUT(match, ip.tos,
560 ipv4_key->ipv4_tos, is_mask);
561 SW_FLOW_KEY_PUT(match, ip.ttl,
562 ipv4_key->ipv4_ttl, is_mask);
563 SW_FLOW_KEY_PUT(match, ip.frag,
564 ipv4_key->ipv4_frag, is_mask);
565 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
566 ipv4_key->ipv4_src, is_mask);
567 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
568 ipv4_key->ipv4_dst, is_mask);
569 attrs &= ~(1 << OVS_KEY_ATTR_IPV4);
570 }
571
572 if (attrs & (1 << OVS_KEY_ATTR_IPV6)) {
573 const struct ovs_key_ipv6 *ipv6_key;
574
575 ipv6_key = nla_data(a[OVS_KEY_ATTR_IPV6]);
576 if (!is_mask && ipv6_key->ipv6_frag > OVS_FRAG_TYPE_MAX) {
577 OVS_NLERR("Unknown IPv6 fragment type (value=%d, max=%d).\n",
578 ipv6_key->ipv6_frag, OVS_FRAG_TYPE_MAX);
579 return -EINVAL;
580 }
581 SW_FLOW_KEY_PUT(match, ipv6.label,
582 ipv6_key->ipv6_label, is_mask);
583 SW_FLOW_KEY_PUT(match, ip.proto,
584 ipv6_key->ipv6_proto, is_mask);
585 SW_FLOW_KEY_PUT(match, ip.tos,
586 ipv6_key->ipv6_tclass, is_mask);
587 SW_FLOW_KEY_PUT(match, ip.ttl,
588 ipv6_key->ipv6_hlimit, is_mask);
589 SW_FLOW_KEY_PUT(match, ip.frag,
590 ipv6_key->ipv6_frag, is_mask);
591 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.src,
592 ipv6_key->ipv6_src,
593 sizeof(match->key->ipv6.addr.src),
594 is_mask);
595 SW_FLOW_KEY_MEMCPY(match, ipv6.addr.dst,
596 ipv6_key->ipv6_dst,
597 sizeof(match->key->ipv6.addr.dst),
598 is_mask);
599
600 attrs &= ~(1 << OVS_KEY_ATTR_IPV6);
601 }
602
603 if (attrs & (1 << OVS_KEY_ATTR_ARP)) {
604 const struct ovs_key_arp *arp_key;
605
606 arp_key = nla_data(a[OVS_KEY_ATTR_ARP]);
607 if (!is_mask && (arp_key->arp_op & htons(0xff00))) {
608 OVS_NLERR("Unknown ARP opcode (opcode=%d).\n",
609 arp_key->arp_op);
610 return -EINVAL;
611 }
612
613 SW_FLOW_KEY_PUT(match, ipv4.addr.src,
614 arp_key->arp_sip, is_mask);
615 SW_FLOW_KEY_PUT(match, ipv4.addr.dst,
616 arp_key->arp_tip, is_mask);
617 SW_FLOW_KEY_PUT(match, ip.proto,
618 ntohs(arp_key->arp_op), is_mask);
619 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.sha,
620 arp_key->arp_sha, ETH_ALEN, is_mask);
621 SW_FLOW_KEY_MEMCPY(match, ipv4.arp.tha,
622 arp_key->arp_tha, ETH_ALEN, is_mask);
623
624 attrs &= ~(1 << OVS_KEY_ATTR_ARP);
625 }
626
627 if (attrs & (1 << OVS_KEY_ATTR_TCP)) {
628 const struct ovs_key_tcp *tcp_key;
629
630 tcp_key = nla_data(a[OVS_KEY_ATTR_TCP]);
631 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
632 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
633 tcp_key->tcp_src, is_mask);
634 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
635 tcp_key->tcp_dst, is_mask);
636 } else {
637 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
638 tcp_key->tcp_src, is_mask);
639 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
640 tcp_key->tcp_dst, is_mask);
641 }
642 attrs &= ~(1 << OVS_KEY_ATTR_TCP);
643 }
644
645 if (attrs & (1 << OVS_KEY_ATTR_TCP_FLAGS)) {
646 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
647 SW_FLOW_KEY_PUT(match, ipv4.tp.flags,
648 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
649 is_mask);
650 } else {
651 SW_FLOW_KEY_PUT(match, ipv6.tp.flags,
652 nla_get_be16(a[OVS_KEY_ATTR_TCP_FLAGS]),
653 is_mask);
654 }
655 attrs &= ~(1 << OVS_KEY_ATTR_TCP_FLAGS);
656 }
657
658 if (attrs & (1 << OVS_KEY_ATTR_UDP)) {
659 const struct ovs_key_udp *udp_key;
660
661 udp_key = nla_data(a[OVS_KEY_ATTR_UDP]);
662 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
663 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
664 udp_key->udp_src, is_mask);
665 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
666 udp_key->udp_dst, is_mask);
667 } else {
668 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
669 udp_key->udp_src, is_mask);
670 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
671 udp_key->udp_dst, is_mask);
672 }
673 attrs &= ~(1 << OVS_KEY_ATTR_UDP);
674 }
675
676 if (attrs & (1 << OVS_KEY_ATTR_SCTP)) {
677 const struct ovs_key_sctp *sctp_key;
678
679 sctp_key = nla_data(a[OVS_KEY_ATTR_SCTP]);
680 if (orig_attrs & (1 << OVS_KEY_ATTR_IPV4)) {
681 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
682 sctp_key->sctp_src, is_mask);
683 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
684 sctp_key->sctp_dst, is_mask);
685 } else {
686 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
687 sctp_key->sctp_src, is_mask);
688 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
689 sctp_key->sctp_dst, is_mask);
690 }
691 attrs &= ~(1 << OVS_KEY_ATTR_SCTP);
692 }
693
694 if (attrs & (1 << OVS_KEY_ATTR_ICMP)) {
695 const struct ovs_key_icmp *icmp_key;
696
697 icmp_key = nla_data(a[OVS_KEY_ATTR_ICMP]);
698 SW_FLOW_KEY_PUT(match, ipv4.tp.src,
699 htons(icmp_key->icmp_type), is_mask);
700 SW_FLOW_KEY_PUT(match, ipv4.tp.dst,
701 htons(icmp_key->icmp_code), is_mask);
702 attrs &= ~(1 << OVS_KEY_ATTR_ICMP);
703 }
704
705 if (attrs & (1 << OVS_KEY_ATTR_ICMPV6)) {
706 const struct ovs_key_icmpv6 *icmpv6_key;
707
708 icmpv6_key = nla_data(a[OVS_KEY_ATTR_ICMPV6]);
709 SW_FLOW_KEY_PUT(match, ipv6.tp.src,
710 htons(icmpv6_key->icmpv6_type), is_mask);
711 SW_FLOW_KEY_PUT(match, ipv6.tp.dst,
712 htons(icmpv6_key->icmpv6_code), is_mask);
713 attrs &= ~(1 << OVS_KEY_ATTR_ICMPV6);
714 }
715
716 if (attrs & (1 << OVS_KEY_ATTR_ND)) {
717 const struct ovs_key_nd *nd_key;
718
719 nd_key = nla_data(a[OVS_KEY_ATTR_ND]);
720 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.target,
721 nd_key->nd_target,
722 sizeof(match->key->ipv6.nd.target),
723 is_mask);
724 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.sll,
725 nd_key->nd_sll, ETH_ALEN, is_mask);
726 SW_FLOW_KEY_MEMCPY(match, ipv6.nd.tll,
727 nd_key->nd_tll, ETH_ALEN, is_mask);
728 attrs &= ~(1 << OVS_KEY_ATTR_ND);
729 }
730
731 if (attrs != 0)
732 return -EINVAL;
733
734 return 0;
735}
736
737static void sw_flow_mask_set(struct sw_flow_mask *mask,
738 struct sw_flow_key_range *range, u8 val)
739{
740 u8 *m = (u8 *)&mask->key + range->start;
741
742 mask->range = *range;
743 memset(m, val, range_n_bytes(range));
744}
745
746/**
747 * ovs_nla_get_match - parses Netlink attributes into a flow key and
748 * mask. In case the 'mask' is NULL, the flow is treated as exact match
749 * flow. Otherwise, it is treated as a wildcarded flow, except the mask
750 * does not include any don't care bit.
751 * @match: receives the extracted flow match information.
752 * @key: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
753 * sequence. The fields should of the packet that triggered the creation
754 * of this flow.
755 * @mask: Optional. Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink
756 * attribute specifies the mask field of the wildcarded flow.
757 */
758int ovs_nla_get_match(struct sw_flow_match *match,
759 const struct nlattr *key,
760 const struct nlattr *mask)
761{
762 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
763 const struct nlattr *encap;
764 u64 key_attrs = 0;
765 u64 mask_attrs = 0;
766 bool encap_valid = false;
767 int err;
768
769 err = parse_flow_nlattrs(key, a, &key_attrs);
770 if (err)
771 return err;
772
773 if ((key_attrs & (1 << OVS_KEY_ATTR_ETHERNET)) &&
774 (key_attrs & (1 << OVS_KEY_ATTR_ETHERTYPE)) &&
775 (nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]) == htons(ETH_P_8021Q))) {
776 __be16 tci;
777
778 if (!((key_attrs & (1 << OVS_KEY_ATTR_VLAN)) &&
779 (key_attrs & (1 << OVS_KEY_ATTR_ENCAP)))) {
780 OVS_NLERR("Invalid Vlan frame.\n");
781 return -EINVAL;
782 }
783
784 key_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
785 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
786 encap = a[OVS_KEY_ATTR_ENCAP];
787 key_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
788 encap_valid = true;
789
790 if (tci & htons(VLAN_TAG_PRESENT)) {
791 err = parse_flow_nlattrs(encap, a, &key_attrs);
792 if (err)
793 return err;
794 } else if (!tci) {
795 /* Corner case for truncated 802.1Q header. */
796 if (nla_len(encap)) {
797 OVS_NLERR("Truncated 802.1Q header has non-zero encap attribute.\n");
798 return -EINVAL;
799 }
800 } else {
801 OVS_NLERR("Encap attribute is set for a non-VLAN frame.\n");
802 return -EINVAL;
803 }
804 }
805
806 err = ovs_key_from_nlattrs(match, key_attrs, a, false);
807 if (err)
808 return err;
809
810 if (mask) {
811 err = parse_flow_mask_nlattrs(mask, a, &mask_attrs);
812 if (err)
813 return err;
814
815 if (mask_attrs & 1 << OVS_KEY_ATTR_ENCAP) {
816 __be16 eth_type = 0;
817 __be16 tci = 0;
818
819 if (!encap_valid) {
820 OVS_NLERR("Encap mask attribute is set for non-VLAN frame.\n");
821 return -EINVAL;
822 }
823
824 mask_attrs &= ~(1 << OVS_KEY_ATTR_ENCAP);
825 if (a[OVS_KEY_ATTR_ETHERTYPE])
826 eth_type = nla_get_be16(a[OVS_KEY_ATTR_ETHERTYPE]);
827
828 if (eth_type == htons(0xffff)) {
829 mask_attrs &= ~(1 << OVS_KEY_ATTR_ETHERTYPE);
830 encap = a[OVS_KEY_ATTR_ENCAP];
831 err = parse_flow_mask_nlattrs(encap, a, &mask_attrs);
832 } else {
833 OVS_NLERR("VLAN frames must have an exact match on the TPID (mask=%x).\n",
834 ntohs(eth_type));
835 return -EINVAL;
836 }
837
838 if (a[OVS_KEY_ATTR_VLAN])
839 tci = nla_get_be16(a[OVS_KEY_ATTR_VLAN]);
840
841 if (!(tci & htons(VLAN_TAG_PRESENT))) {
842 OVS_NLERR("VLAN tag present bit must have an exact match (tci_mask=%x).\n", ntohs(tci));
843 return -EINVAL;
844 }
845 }
846
847 err = ovs_key_from_nlattrs(match, mask_attrs, a, true);
848 if (err)
849 return err;
850 } else {
851 /* Populate exact match flow's key mask. */
852 if (match->mask)
853 sw_flow_mask_set(match->mask, &match->range, 0xff);
854 }
855
856 if (!match_validate(match, key_attrs, mask_attrs))
857 return -EINVAL;
858
859 return 0;
860}
861
862/**
863 * ovs_nla_get_flow_metadata - parses Netlink attributes into a flow key.
864 * @flow: Receives extracted in_port, priority, tun_key and skb_mark.
865 * @attr: Netlink attribute holding nested %OVS_KEY_ATTR_* Netlink attribute
866 * sequence.
867 *
868 * This parses a series of Netlink attributes that form a flow key, which must
869 * take the same form accepted by flow_from_nlattrs(), but only enough of it to
870 * get the metadata, that is, the parts of the flow key that cannot be
871 * extracted from the packet itself.
872 */
873
874int ovs_nla_get_flow_metadata(struct sw_flow *flow,
875 const struct nlattr *attr)
876{
877 struct ovs_key_ipv4_tunnel *tun_key = &flow->key.tun_key;
878 const struct nlattr *a[OVS_KEY_ATTR_MAX + 1];
879 u64 attrs = 0;
880 int err;
881 struct sw_flow_match match;
882
883 flow->key.phy.in_port = DP_MAX_PORTS;
884 flow->key.phy.priority = 0;
885 flow->key.phy.skb_mark = 0;
886 memset(tun_key, 0, sizeof(flow->key.tun_key));
887
888 err = parse_flow_nlattrs(attr, a, &attrs);
889 if (err)
890 return -EINVAL;
891
892 memset(&match, 0, sizeof(match));
893 match.key = &flow->key;
894
895 err = metadata_from_nlattrs(&match, &attrs, a, false);
896 if (err)
897 return err;
898
899 return 0;
900}
901
902int ovs_nla_put_flow(const struct sw_flow_key *swkey,
903 const struct sw_flow_key *output, struct sk_buff *skb)
904{
905 struct ovs_key_ethernet *eth_key;
906 struct nlattr *nla, *encap;
907 bool is_mask = (swkey != output);
908
909 if (nla_put_u32(skb, OVS_KEY_ATTR_PRIORITY, output->phy.priority))
910 goto nla_put_failure;
911
912 if ((swkey->tun_key.ipv4_dst || is_mask) &&
913 ipv4_tun_to_nlattr(skb, &swkey->tun_key, &output->tun_key))
914 goto nla_put_failure;
915
916 if (swkey->phy.in_port == DP_MAX_PORTS) {
917 if (is_mask && (output->phy.in_port == 0xffff))
918 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT, 0xffffffff))
919 goto nla_put_failure;
920 } else {
921 u16 upper_u16;
922 upper_u16 = !is_mask ? 0 : 0xffff;
923
924 if (nla_put_u32(skb, OVS_KEY_ATTR_IN_PORT,
925 (upper_u16 << 16) | output->phy.in_port))
926 goto nla_put_failure;
927 }
928
929 if (nla_put_u32(skb, OVS_KEY_ATTR_SKB_MARK, output->phy.skb_mark))
930 goto nla_put_failure;
931
932 nla = nla_reserve(skb, OVS_KEY_ATTR_ETHERNET, sizeof(*eth_key));
933 if (!nla)
934 goto nla_put_failure;
935
936 eth_key = nla_data(nla);
937 memcpy(eth_key->eth_src, output->eth.src, ETH_ALEN);
938 memcpy(eth_key->eth_dst, output->eth.dst, ETH_ALEN);
939
940 if (swkey->eth.tci || swkey->eth.type == htons(ETH_P_8021Q)) {
941 __be16 eth_type;
942 eth_type = !is_mask ? htons(ETH_P_8021Q) : htons(0xffff);
943 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, eth_type) ||
944 nla_put_be16(skb, OVS_KEY_ATTR_VLAN, output->eth.tci))
945 goto nla_put_failure;
946 encap = nla_nest_start(skb, OVS_KEY_ATTR_ENCAP);
947 if (!swkey->eth.tci)
948 goto unencap;
949 } else
950 encap = NULL;
951
952 if (swkey->eth.type == htons(ETH_P_802_2)) {
953 /*
954 * Ethertype 802.2 is represented in the netlink with omitted
955 * OVS_KEY_ATTR_ETHERTYPE in the flow key attribute, and
956 * 0xffff in the mask attribute. Ethertype can also
957 * be wildcarded.
958 */
959 if (is_mask && output->eth.type)
960 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE,
961 output->eth.type))
962 goto nla_put_failure;
963 goto unencap;
964 }
965
966 if (nla_put_be16(skb, OVS_KEY_ATTR_ETHERTYPE, output->eth.type))
967 goto nla_put_failure;
968
969 if (swkey->eth.type == htons(ETH_P_IP)) {
970 struct ovs_key_ipv4 *ipv4_key;
971
972 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV4, sizeof(*ipv4_key));
973 if (!nla)
974 goto nla_put_failure;
975 ipv4_key = nla_data(nla);
976 ipv4_key->ipv4_src = output->ipv4.addr.src;
977 ipv4_key->ipv4_dst = output->ipv4.addr.dst;
978 ipv4_key->ipv4_proto = output->ip.proto;
979 ipv4_key->ipv4_tos = output->ip.tos;
980 ipv4_key->ipv4_ttl = output->ip.ttl;
981 ipv4_key->ipv4_frag = output->ip.frag;
982 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
983 struct ovs_key_ipv6 *ipv6_key;
984
985 nla = nla_reserve(skb, OVS_KEY_ATTR_IPV6, sizeof(*ipv6_key));
986 if (!nla)
987 goto nla_put_failure;
988 ipv6_key = nla_data(nla);
989 memcpy(ipv6_key->ipv6_src, &output->ipv6.addr.src,
990 sizeof(ipv6_key->ipv6_src));
991 memcpy(ipv6_key->ipv6_dst, &output->ipv6.addr.dst,
992 sizeof(ipv6_key->ipv6_dst));
993 ipv6_key->ipv6_label = output->ipv6.label;
994 ipv6_key->ipv6_proto = output->ip.proto;
995 ipv6_key->ipv6_tclass = output->ip.tos;
996 ipv6_key->ipv6_hlimit = output->ip.ttl;
997 ipv6_key->ipv6_frag = output->ip.frag;
998 } else if (swkey->eth.type == htons(ETH_P_ARP) ||
999 swkey->eth.type == htons(ETH_P_RARP)) {
1000 struct ovs_key_arp *arp_key;
1001
1002 nla = nla_reserve(skb, OVS_KEY_ATTR_ARP, sizeof(*arp_key));
1003 if (!nla)
1004 goto nla_put_failure;
1005 arp_key = nla_data(nla);
1006 memset(arp_key, 0, sizeof(struct ovs_key_arp));
1007 arp_key->arp_sip = output->ipv4.addr.src;
1008 arp_key->arp_tip = output->ipv4.addr.dst;
1009 arp_key->arp_op = htons(output->ip.proto);
1010 memcpy(arp_key->arp_sha, output->ipv4.arp.sha, ETH_ALEN);
1011 memcpy(arp_key->arp_tha, output->ipv4.arp.tha, ETH_ALEN);
1012 }
1013
1014 if ((swkey->eth.type == htons(ETH_P_IP) ||
1015 swkey->eth.type == htons(ETH_P_IPV6)) &&
1016 swkey->ip.frag != OVS_FRAG_TYPE_LATER) {
1017
1018 if (swkey->ip.proto == IPPROTO_TCP) {
1019 struct ovs_key_tcp *tcp_key;
1020
1021 nla = nla_reserve(skb, OVS_KEY_ATTR_TCP, sizeof(*tcp_key));
1022 if (!nla)
1023 goto nla_put_failure;
1024 tcp_key = nla_data(nla);
1025 if (swkey->eth.type == htons(ETH_P_IP)) {
1026 tcp_key->tcp_src = output->ipv4.tp.src;
1027 tcp_key->tcp_dst = output->ipv4.tp.dst;
1028 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1029 output->ipv4.tp.flags))
1030 goto nla_put_failure;
1031 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1032 tcp_key->tcp_src = output->ipv6.tp.src;
1033 tcp_key->tcp_dst = output->ipv6.tp.dst;
1034 if (nla_put_be16(skb, OVS_KEY_ATTR_TCP_FLAGS,
1035 output->ipv6.tp.flags))
1036 goto nla_put_failure;
1037 }
1038 } else if (swkey->ip.proto == IPPROTO_UDP) {
1039 struct ovs_key_udp *udp_key;
1040
1041 nla = nla_reserve(skb, OVS_KEY_ATTR_UDP, sizeof(*udp_key));
1042 if (!nla)
1043 goto nla_put_failure;
1044 udp_key = nla_data(nla);
1045 if (swkey->eth.type == htons(ETH_P_IP)) {
1046 udp_key->udp_src = output->ipv4.tp.src;
1047 udp_key->udp_dst = output->ipv4.tp.dst;
1048 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1049 udp_key->udp_src = output->ipv6.tp.src;
1050 udp_key->udp_dst = output->ipv6.tp.dst;
1051 }
1052 } else if (swkey->ip.proto == IPPROTO_SCTP) {
1053 struct ovs_key_sctp *sctp_key;
1054
1055 nla = nla_reserve(skb, OVS_KEY_ATTR_SCTP, sizeof(*sctp_key));
1056 if (!nla)
1057 goto nla_put_failure;
1058 sctp_key = nla_data(nla);
1059 if (swkey->eth.type == htons(ETH_P_IP)) {
1060 sctp_key->sctp_src = swkey->ipv4.tp.src;
1061 sctp_key->sctp_dst = swkey->ipv4.tp.dst;
1062 } else if (swkey->eth.type == htons(ETH_P_IPV6)) {
1063 sctp_key->sctp_src = swkey->ipv6.tp.src;
1064 sctp_key->sctp_dst = swkey->ipv6.tp.dst;
1065 }
1066 } else if (swkey->eth.type == htons(ETH_P_IP) &&
1067 swkey->ip.proto == IPPROTO_ICMP) {
1068 struct ovs_key_icmp *icmp_key;
1069
1070 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMP, sizeof(*icmp_key));
1071 if (!nla)
1072 goto nla_put_failure;
1073 icmp_key = nla_data(nla);
1074 icmp_key->icmp_type = ntohs(output->ipv4.tp.src);
1075 icmp_key->icmp_code = ntohs(output->ipv4.tp.dst);
1076 } else if (swkey->eth.type == htons(ETH_P_IPV6) &&
1077 swkey->ip.proto == IPPROTO_ICMPV6) {
1078 struct ovs_key_icmpv6 *icmpv6_key;
1079
1080 nla = nla_reserve(skb, OVS_KEY_ATTR_ICMPV6,
1081 sizeof(*icmpv6_key));
1082 if (!nla)
1083 goto nla_put_failure;
1084 icmpv6_key = nla_data(nla);
1085 icmpv6_key->icmpv6_type = ntohs(output->ipv6.tp.src);
1086 icmpv6_key->icmpv6_code = ntohs(output->ipv6.tp.dst);
1087
1088 if (icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_SOLICITATION ||
1089 icmpv6_key->icmpv6_type == NDISC_NEIGHBOUR_ADVERTISEMENT) {
1090 struct ovs_key_nd *nd_key;
1091
1092 nla = nla_reserve(skb, OVS_KEY_ATTR_ND, sizeof(*nd_key));
1093 if (!nla)
1094 goto nla_put_failure;
1095 nd_key = nla_data(nla);
1096 memcpy(nd_key->nd_target, &output->ipv6.nd.target,
1097 sizeof(nd_key->nd_target));
1098 memcpy(nd_key->nd_sll, output->ipv6.nd.sll, ETH_ALEN);
1099 memcpy(nd_key->nd_tll, output->ipv6.nd.tll, ETH_ALEN);
1100 }
1101 }
1102 }
1103
1104unencap:
1105 if (encap)
1106 nla_nest_end(skb, encap);
1107
1108 return 0;
1109
1110nla_put_failure:
1111 return -EMSGSIZE;
1112}
1113
1114#define MAX_ACTIONS_BUFSIZE (32 * 1024)
1115
1116struct sw_flow_actions *ovs_nla_alloc_flow_actions(int size)
1117{
1118 struct sw_flow_actions *sfa;
1119
1120 if (size > MAX_ACTIONS_BUFSIZE)
1121 return ERR_PTR(-EINVAL);
1122
1123 sfa = kmalloc(sizeof(*sfa) + size, GFP_KERNEL);
1124 if (!sfa)
1125 return ERR_PTR(-ENOMEM);
1126
1127 sfa->actions_len = 0;
1128 return sfa;
1129}
1130
1131/* RCU callback used by ovs_nla_free_flow_actions. */
1132static void rcu_free_acts_callback(struct rcu_head *rcu)
1133{
1134 struct sw_flow_actions *sf_acts = container_of(rcu,
1135 struct sw_flow_actions, rcu);
1136 kfree(sf_acts);
1137}
1138
1139/* Schedules 'sf_acts' to be freed after the next RCU grace period.
1140 * The caller must hold rcu_read_lock for this to be sensible. */
1141void ovs_nla_free_flow_actions(struct sw_flow_actions *sf_acts)
1142{
1143 call_rcu(&sf_acts->rcu, rcu_free_acts_callback);
1144}
1145
1146static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa,
1147 int attr_len)
1148{
1149
1150 struct sw_flow_actions *acts;
1151 int new_acts_size;
1152 int req_size = NLA_ALIGN(attr_len);
1153 int next_offset = offsetof(struct sw_flow_actions, actions) +
1154 (*sfa)->actions_len;
1155
1156 if (req_size <= (ksize(*sfa) - next_offset))
1157 goto out;
1158
1159 new_acts_size = ksize(*sfa) * 2;
1160
1161 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
1162 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
1163 return ERR_PTR(-EMSGSIZE);
1164 new_acts_size = MAX_ACTIONS_BUFSIZE;
1165 }
1166
1167 acts = ovs_nla_alloc_flow_actions(new_acts_size);
1168 if (IS_ERR(acts))
1169 return (void *)acts;
1170
1171 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
1172 acts->actions_len = (*sfa)->actions_len;
1173 kfree(*sfa);
1174 *sfa = acts;
1175
1176out:
1177 (*sfa)->actions_len += req_size;
1178 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
1179}
1180
1181static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
1182{
1183 struct nlattr *a;
1184
1185 a = reserve_sfa_size(sfa, nla_attr_size(len));
1186 if (IS_ERR(a))
1187 return PTR_ERR(a);
1188
1189 a->nla_type = attrtype;
1190 a->nla_len = nla_attr_size(len);
1191
1192 if (data)
1193 memcpy(nla_data(a), data, len);
1194 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
1195
1196 return 0;
1197}
1198
1199static inline int add_nested_action_start(struct sw_flow_actions **sfa,
1200 int attrtype)
1201{
1202 int used = (*sfa)->actions_len;
1203 int err;
1204
1205 err = add_action(sfa, attrtype, NULL, 0);
1206 if (err)
1207 return err;
1208
1209 return used;
1210}
1211
1212static inline void add_nested_action_end(struct sw_flow_actions *sfa,
1213 int st_offset)
1214{
1215 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions +
1216 st_offset);
1217
1218 a->nla_len = sfa->actions_len - st_offset;
1219}
1220
1221static int validate_and_copy_sample(const struct nlattr *attr,
1222 const struct sw_flow_key *key, int depth,
1223 struct sw_flow_actions **sfa)
1224{
1225 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
1226 const struct nlattr *probability, *actions;
1227 const struct nlattr *a;
1228 int rem, start, err, st_acts;
1229
1230 memset(attrs, 0, sizeof(attrs));
1231 nla_for_each_nested(a, attr, rem) {
1232 int type = nla_type(a);
1233 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
1234 return -EINVAL;
1235 attrs[type] = a;
1236 }
1237 if (rem)
1238 return -EINVAL;
1239
1240 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
1241 if (!probability || nla_len(probability) != sizeof(u32))
1242 return -EINVAL;
1243
1244 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
1245 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
1246 return -EINVAL;
1247
1248 /* validation done, copy sample action. */
1249 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
1250 if (start < 0)
1251 return start;
1252 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY,
1253 nla_data(probability), sizeof(u32));
1254 if (err)
1255 return err;
1256 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
1257 if (st_acts < 0)
1258 return st_acts;
1259
1260 err = ovs_nla_copy_actions(actions, key, depth + 1, sfa);
1261 if (err)
1262 return err;
1263
1264 add_nested_action_end(*sfa, st_acts);
1265 add_nested_action_end(*sfa, start);
1266
1267 return 0;
1268}
1269
1270static int validate_tp_port(const struct sw_flow_key *flow_key)
1271{
1272 if (flow_key->eth.type == htons(ETH_P_IP)) {
1273 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
1274 return 0;
1275 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
1276 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
1277 return 0;
1278 }
1279
1280 return -EINVAL;
1281}
1282
1283void ovs_match_init(struct sw_flow_match *match,
1284 struct sw_flow_key *key,
1285 struct sw_flow_mask *mask)
1286{
1287 memset(match, 0, sizeof(*match));
1288 match->key = key;
1289 match->mask = mask;
1290
1291 memset(key, 0, sizeof(*key));
1292
1293 if (mask) {
1294 memset(&mask->key, 0, sizeof(mask->key));
1295 mask->range.start = mask->range.end = 0;
1296 }
1297}
1298
1299static int validate_and_copy_set_tun(const struct nlattr *attr,
1300 struct sw_flow_actions **sfa)
1301{
1302 struct sw_flow_match match;
1303 struct sw_flow_key key;
1304 int err, start;
1305
1306 ovs_match_init(&match, &key, NULL);
1307 err = ipv4_tun_from_nlattr(nla_data(attr), &match, false);
1308 if (err)
1309 return err;
1310
1311 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
1312 if (start < 0)
1313 return start;
1314
1315 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
1316 sizeof(match.key->tun_key));
1317 add_nested_action_end(*sfa, start);
1318
1319 return err;
1320}
1321
1322static int validate_set(const struct nlattr *a,
1323 const struct sw_flow_key *flow_key,
1324 struct sw_flow_actions **sfa,
1325 bool *set_tun)
1326{
1327 const struct nlattr *ovs_key = nla_data(a);
1328 int key_type = nla_type(ovs_key);
1329
1330 /* There can be only one key in a action */
1331 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
1332 return -EINVAL;
1333
1334 if (key_type > OVS_KEY_ATTR_MAX ||
1335 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
1336 ovs_key_lens[key_type] != -1))
1337 return -EINVAL;
1338
1339 switch (key_type) {
1340 const struct ovs_key_ipv4 *ipv4_key;
1341 const struct ovs_key_ipv6 *ipv6_key;
1342 int err;
1343
1344 case OVS_KEY_ATTR_PRIORITY:
1345 case OVS_KEY_ATTR_SKB_MARK:
1346 case OVS_KEY_ATTR_ETHERNET:
1347 break;
1348
1349 case OVS_KEY_ATTR_TUNNEL:
1350 *set_tun = true;
1351 err = validate_and_copy_set_tun(a, sfa);
1352 if (err)
1353 return err;
1354 break;
1355
1356 case OVS_KEY_ATTR_IPV4:
1357 if (flow_key->eth.type != htons(ETH_P_IP))
1358 return -EINVAL;
1359
1360 if (!flow_key->ip.proto)
1361 return -EINVAL;
1362
1363 ipv4_key = nla_data(ovs_key);
1364 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
1365 return -EINVAL;
1366
1367 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
1368 return -EINVAL;
1369
1370 break;
1371
1372 case OVS_KEY_ATTR_IPV6:
1373 if (flow_key->eth.type != htons(ETH_P_IPV6))
1374 return -EINVAL;
1375
1376 if (!flow_key->ip.proto)
1377 return -EINVAL;
1378
1379 ipv6_key = nla_data(ovs_key);
1380 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
1381 return -EINVAL;
1382
1383 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
1384 return -EINVAL;
1385
1386 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
1387 return -EINVAL;
1388
1389 break;
1390
1391 case OVS_KEY_ATTR_TCP:
1392 if (flow_key->ip.proto != IPPROTO_TCP)
1393 return -EINVAL;
1394
1395 return validate_tp_port(flow_key);
1396
1397 case OVS_KEY_ATTR_UDP:
1398 if (flow_key->ip.proto != IPPROTO_UDP)
1399 return -EINVAL;
1400
1401 return validate_tp_port(flow_key);
1402
1403 case OVS_KEY_ATTR_SCTP:
1404 if (flow_key->ip.proto != IPPROTO_SCTP)
1405 return -EINVAL;
1406
1407 return validate_tp_port(flow_key);
1408
1409 default:
1410 return -EINVAL;
1411 }
1412
1413 return 0;
1414}
1415
1416static int validate_userspace(const struct nlattr *attr)
1417{
1418 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
1419 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
1420 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
1421 };
1422 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
1423 int error;
1424
1425 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
1426 attr, userspace_policy);
1427 if (error)
1428 return error;
1429
1430 if (!a[OVS_USERSPACE_ATTR_PID] ||
1431 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
1432 return -EINVAL;
1433
1434 return 0;
1435}
1436
1437static int copy_action(const struct nlattr *from,
1438 struct sw_flow_actions **sfa)
1439{
1440 int totlen = NLA_ALIGN(from->nla_len);
1441 struct nlattr *to;
1442
1443 to = reserve_sfa_size(sfa, from->nla_len);
1444 if (IS_ERR(to))
1445 return PTR_ERR(to);
1446
1447 memcpy(to, from, totlen);
1448 return 0;
1449}
1450
1451int ovs_nla_copy_actions(const struct nlattr *attr,
1452 const struct sw_flow_key *key,
1453 int depth,
1454 struct sw_flow_actions **sfa)
1455{
1456 const struct nlattr *a;
1457 int rem, err;
1458
1459 if (depth >= SAMPLE_ACTION_DEPTH)
1460 return -EOVERFLOW;
1461
1462 nla_for_each_nested(a, attr, rem) {
1463 /* Expected argument lengths, (u32)-1 for variable length. */
1464 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
1465 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
1466 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
1467 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
1468 [OVS_ACTION_ATTR_POP_VLAN] = 0,
1469 [OVS_ACTION_ATTR_SET] = (u32)-1,
1470 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
1471 };
1472 const struct ovs_action_push_vlan *vlan;
1473 int type = nla_type(a);
1474 bool skip_copy;
1475
1476 if (type > OVS_ACTION_ATTR_MAX ||
1477 (action_lens[type] != nla_len(a) &&
1478 action_lens[type] != (u32)-1))
1479 return -EINVAL;
1480
1481 skip_copy = false;
1482 switch (type) {
1483 case OVS_ACTION_ATTR_UNSPEC:
1484 return -EINVAL;
1485
1486 case OVS_ACTION_ATTR_USERSPACE:
1487 err = validate_userspace(a);
1488 if (err)
1489 return err;
1490 break;
1491
1492 case OVS_ACTION_ATTR_OUTPUT:
1493 if (nla_get_u32(a) >= DP_MAX_PORTS)
1494 return -EINVAL;
1495 break;
1496
1497
1498 case OVS_ACTION_ATTR_POP_VLAN:
1499 break;
1500
1501 case OVS_ACTION_ATTR_PUSH_VLAN:
1502 vlan = nla_data(a);
1503 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
1504 return -EINVAL;
1505 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
1506 return -EINVAL;
1507 break;
1508
1509 case OVS_ACTION_ATTR_SET:
1510 err = validate_set(a, key, sfa, &skip_copy);
1511 if (err)
1512 return err;
1513 break;
1514
1515 case OVS_ACTION_ATTR_SAMPLE:
1516 err = validate_and_copy_sample(a, key, depth, sfa);
1517 if (err)
1518 return err;
1519 skip_copy = true;
1520 break;
1521
1522 default:
1523 return -EINVAL;
1524 }
1525 if (!skip_copy) {
1526 err = copy_action(a, sfa);
1527 if (err)
1528 return err;
1529 }
1530 }
1531
1532 if (rem > 0)
1533 return -EINVAL;
1534
1535 return 0;
1536}
1537
1538static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1539{
1540 const struct nlattr *a;
1541 struct nlattr *start;
1542 int err = 0, rem;
1543
1544 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1545 if (!start)
1546 return -EMSGSIZE;
1547
1548 nla_for_each_nested(a, attr, rem) {
1549 int type = nla_type(a);
1550 struct nlattr *st_sample;
1551
1552 switch (type) {
1553 case OVS_SAMPLE_ATTR_PROBABILITY:
1554 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY,
1555 sizeof(u32), nla_data(a)))
1556 return -EMSGSIZE;
1557 break;
1558 case OVS_SAMPLE_ATTR_ACTIONS:
1559 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1560 if (!st_sample)
1561 return -EMSGSIZE;
1562 err = ovs_nla_put_actions(nla_data(a), nla_len(a), skb);
1563 if (err)
1564 return err;
1565 nla_nest_end(skb, st_sample);
1566 break;
1567 }
1568 }
1569
1570 nla_nest_end(skb, start);
1571 return err;
1572}
1573
1574static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1575{
1576 const struct nlattr *ovs_key = nla_data(a);
1577 int key_type = nla_type(ovs_key);
1578 struct nlattr *start;
1579 int err;
1580
1581 switch (key_type) {
1582 case OVS_KEY_ATTR_IPV4_TUNNEL:
1583 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1584 if (!start)
1585 return -EMSGSIZE;
1586
1587 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1588 nla_data(ovs_key));
1589 if (err)
1590 return err;
1591 nla_nest_end(skb, start);
1592 break;
1593 default:
1594 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1595 return -EMSGSIZE;
1596 break;
1597 }
1598
1599 return 0;
1600}
1601
1602int ovs_nla_put_actions(const struct nlattr *attr, int len, struct sk_buff *skb)
1603{
1604 const struct nlattr *a;
1605 int rem, err;
1606
1607 nla_for_each_attr(a, attr, len, rem) {
1608 int type = nla_type(a);
1609
1610 switch (type) {
1611 case OVS_ACTION_ATTR_SET:
1612 err = set_action_to_attr(a, skb);
1613 if (err)
1614 return err;
1615 break;
1616
1617 case OVS_ACTION_ATTR_SAMPLE:
1618 err = sample_action_to_attr(a, skb);
1619 if (err)
1620 return err;
1621 break;
1622 default:
1623 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1624 return -EMSGSIZE;
1625 break;
1626 }
1627 }
1628
1629 return 0;
1630}
diff --git a/net/openvswitch/flow_netlink.h b/net/openvswitch/flow_netlink.h
new file mode 100644
index 000000000000..440151045d39
--- /dev/null
+++ b/net/openvswitch/flow_netlink.h
@@ -0,0 +1,60 @@
1/*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19
20#ifndef FLOW_NETLINK_H
21#define FLOW_NETLINK_H 1
22
23#include <linux/kernel.h>
24#include <linux/netlink.h>
25#include <linux/openvswitch.h>
26#include <linux/spinlock.h>
27#include <linux/types.h>
28#include <linux/rcupdate.h>
29#include <linux/if_ether.h>
30#include <linux/in6.h>
31#include <linux/jiffies.h>
32#include <linux/time.h>
33#include <linux/flex_array.h>
34
35#include <net/inet_ecn.h>
36#include <net/ip_tunnels.h>
37
38#include "flow.h"
39
40void ovs_match_init(struct sw_flow_match *match,
41 struct sw_flow_key *key, struct sw_flow_mask *mask);
42
43int ovs_nla_put_flow(const struct sw_flow_key *,
44 const struct sw_flow_key *, struct sk_buff *);
45int ovs_nla_get_flow_metadata(struct sw_flow *flow,
46 const struct nlattr *attr);
47int ovs_nla_get_match(struct sw_flow_match *match,
48 const struct nlattr *,
49 const struct nlattr *);
50
51int ovs_nla_copy_actions(const struct nlattr *attr,
52 const struct sw_flow_key *key, int depth,
53 struct sw_flow_actions **sfa);
54int ovs_nla_put_actions(const struct nlattr *attr,
55 int len, struct sk_buff *skb);
56
57struct sw_flow_actions *ovs_nla_alloc_flow_actions(int actions_len);
58void ovs_nla_free_flow_actions(struct sw_flow_actions *);
59
60#endif /* flow_netlink.h */
diff --git a/net/openvswitch/flow_table.c b/net/openvswitch/flow_table.c
new file mode 100644
index 000000000000..e42542706087
--- /dev/null
+++ b/net/openvswitch/flow_table.c
@@ -0,0 +1,592 @@
1/*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#include "flow.h"
20#include "datapath.h"
21#include <linux/uaccess.h>
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/if_ether.h>
25#include <linux/if_vlan.h>
26#include <net/llc_pdu.h>
27#include <linux/kernel.h>
28#include <linux/jhash.h>
29#include <linux/jiffies.h>
30#include <linux/llc.h>
31#include <linux/module.h>
32#include <linux/in.h>
33#include <linux/rcupdate.h>
34#include <linux/if_arp.h>
35#include <linux/ip.h>
36#include <linux/ipv6.h>
37#include <linux/sctp.h>
38#include <linux/tcp.h>
39#include <linux/udp.h>
40#include <linux/icmp.h>
41#include <linux/icmpv6.h>
42#include <linux/rculist.h>
43#include <net/ip.h>
44#include <net/ipv6.h>
45#include <net/ndisc.h>
46
47#include "datapath.h"
48
49#define TBL_MIN_BUCKETS 1024
50#define REHASH_INTERVAL (10 * 60 * HZ)
51
52static struct kmem_cache *flow_cache;
53
54static u16 range_n_bytes(const struct sw_flow_key_range *range)
55{
56 return range->end - range->start;
57}
58
59void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
60 const struct sw_flow_mask *mask)
61{
62 const long *m = (long *)((u8 *)&mask->key + mask->range.start);
63 const long *s = (long *)((u8 *)src + mask->range.start);
64 long *d = (long *)((u8 *)dst + mask->range.start);
65 int i;
66
67 /* The memory outside of the 'mask->range' are not set since
68 * further operations on 'dst' only uses contents within
69 * 'mask->range'.
70 */
71 for (i = 0; i < range_n_bytes(&mask->range); i += sizeof(long))
72 *d++ = *s++ & *m++;
73}
74
75struct sw_flow *ovs_flow_alloc(void)
76{
77 struct sw_flow *flow;
78
79 flow = kmem_cache_alloc(flow_cache, GFP_KERNEL);
80 if (!flow)
81 return ERR_PTR(-ENOMEM);
82
83 spin_lock_init(&flow->lock);
84 flow->sf_acts = NULL;
85 flow->mask = NULL;
86
87 return flow;
88}
89
90int ovs_flow_tbl_count(struct flow_table *table)
91{
92 return table->count;
93}
94
95static struct flex_array *alloc_buckets(unsigned int n_buckets)
96{
97 struct flex_array *buckets;
98 int i, err;
99
100 buckets = flex_array_alloc(sizeof(struct hlist_head),
101 n_buckets, GFP_KERNEL);
102 if (!buckets)
103 return NULL;
104
105 err = flex_array_prealloc(buckets, 0, n_buckets, GFP_KERNEL);
106 if (err) {
107 flex_array_free(buckets);
108 return NULL;
109 }
110
111 for (i = 0; i < n_buckets; i++)
112 INIT_HLIST_HEAD((struct hlist_head *)
113 flex_array_get(buckets, i));
114
115 return buckets;
116}
117
118static void flow_free(struct sw_flow *flow)
119{
120 kfree((struct sf_flow_acts __force *)flow->sf_acts);
121 kmem_cache_free(flow_cache, flow);
122}
123
124static void rcu_free_flow_callback(struct rcu_head *rcu)
125{
126 struct sw_flow *flow = container_of(rcu, struct sw_flow, rcu);
127
128 flow_free(flow);
129}
130
131static void rcu_free_sw_flow_mask_cb(struct rcu_head *rcu)
132{
133 struct sw_flow_mask *mask = container_of(rcu, struct sw_flow_mask, rcu);
134
135 kfree(mask);
136}
137
138static void flow_mask_del_ref(struct sw_flow_mask *mask, bool deferred)
139{
140 if (!mask)
141 return;
142
143 BUG_ON(!mask->ref_count);
144 mask->ref_count--;
145
146 if (!mask->ref_count) {
147 list_del_rcu(&mask->list);
148 if (deferred)
149 call_rcu(&mask->rcu, rcu_free_sw_flow_mask_cb);
150 else
151 kfree(mask);
152 }
153}
154
155void ovs_flow_free(struct sw_flow *flow, bool deferred)
156{
157 if (!flow)
158 return;
159
160 flow_mask_del_ref(flow->mask, deferred);
161
162 if (deferred)
163 call_rcu(&flow->rcu, rcu_free_flow_callback);
164 else
165 flow_free(flow);
166}
167
168static void free_buckets(struct flex_array *buckets)
169{
170 flex_array_free(buckets);
171}
172
173static void __table_instance_destroy(struct table_instance *ti)
174{
175 int i;
176
177 if (ti->keep_flows)
178 goto skip_flows;
179
180 for (i = 0; i < ti->n_buckets; i++) {
181 struct sw_flow *flow;
182 struct hlist_head *head = flex_array_get(ti->buckets, i);
183 struct hlist_node *n;
184 int ver = ti->node_ver;
185
186 hlist_for_each_entry_safe(flow, n, head, hash_node[ver]) {
187 hlist_del(&flow->hash_node[ver]);
188 ovs_flow_free(flow, false);
189 }
190 }
191
192skip_flows:
193 free_buckets(ti->buckets);
194 kfree(ti);
195}
196
197static struct table_instance *table_instance_alloc(int new_size)
198{
199 struct table_instance *ti = kmalloc(sizeof(*ti), GFP_KERNEL);
200
201 if (!ti)
202 return NULL;
203
204 ti->buckets = alloc_buckets(new_size);
205
206 if (!ti->buckets) {
207 kfree(ti);
208 return NULL;
209 }
210 ti->n_buckets = new_size;
211 ti->node_ver = 0;
212 ti->keep_flows = false;
213 get_random_bytes(&ti->hash_seed, sizeof(u32));
214
215 return ti;
216}
217
218int ovs_flow_tbl_init(struct flow_table *table)
219{
220 struct table_instance *ti;
221
222 ti = table_instance_alloc(TBL_MIN_BUCKETS);
223
224 if (!ti)
225 return -ENOMEM;
226
227 rcu_assign_pointer(table->ti, ti);
228 INIT_LIST_HEAD(&table->mask_list);
229 table->last_rehash = jiffies;
230 table->count = 0;
231 return 0;
232}
233
234static void flow_tbl_destroy_rcu_cb(struct rcu_head *rcu)
235{
236 struct table_instance *ti = container_of(rcu, struct table_instance, rcu);
237
238 __table_instance_destroy(ti);
239}
240
241static void table_instance_destroy(struct table_instance *ti, bool deferred)
242{
243 if (!ti)
244 return;
245
246 if (deferred)
247 call_rcu(&ti->rcu, flow_tbl_destroy_rcu_cb);
248 else
249 __table_instance_destroy(ti);
250}
251
252void ovs_flow_tbl_destroy(struct flow_table *table)
253{
254 struct table_instance *ti = ovsl_dereference(table->ti);
255
256 table_instance_destroy(ti, false);
257}
258
259struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *ti,
260 u32 *bucket, u32 *last)
261{
262 struct sw_flow *flow;
263 struct hlist_head *head;
264 int ver;
265 int i;
266
267 ver = ti->node_ver;
268 while (*bucket < ti->n_buckets) {
269 i = 0;
270 head = flex_array_get(ti->buckets, *bucket);
271 hlist_for_each_entry_rcu(flow, head, hash_node[ver]) {
272 if (i < *last) {
273 i++;
274 continue;
275 }
276 *last = i + 1;
277 return flow;
278 }
279 (*bucket)++;
280 *last = 0;
281 }
282
283 return NULL;
284}
285
286static struct hlist_head *find_bucket(struct table_instance *ti, u32 hash)
287{
288 hash = jhash_1word(hash, ti->hash_seed);
289 return flex_array_get(ti->buckets,
290 (hash & (ti->n_buckets - 1)));
291}
292
293static void table_instance_insert(struct table_instance *ti, struct sw_flow *flow)
294{
295 struct hlist_head *head;
296
297 head = find_bucket(ti, flow->hash);
298 hlist_add_head_rcu(&flow->hash_node[ti->node_ver], head);
299}
300
301static void flow_table_copy_flows(struct table_instance *old,
302 struct table_instance *new)
303{
304 int old_ver;
305 int i;
306
307 old_ver = old->node_ver;
308 new->node_ver = !old_ver;
309
310 /* Insert in new table. */
311 for (i = 0; i < old->n_buckets; i++) {
312 struct sw_flow *flow;
313 struct hlist_head *head;
314
315 head = flex_array_get(old->buckets, i);
316
317 hlist_for_each_entry(flow, head, hash_node[old_ver])
318 table_instance_insert(new, flow);
319 }
320
321 old->keep_flows = true;
322}
323
324static struct table_instance *table_instance_rehash(struct table_instance *ti,
325 int n_buckets)
326{
327 struct table_instance *new_ti;
328
329 new_ti = table_instance_alloc(n_buckets);
330 if (!new_ti)
331 return NULL;
332
333 flow_table_copy_flows(ti, new_ti);
334
335 return new_ti;
336}
337
338int ovs_flow_tbl_flush(struct flow_table *flow_table)
339{
340 struct table_instance *old_ti;
341 struct table_instance *new_ti;
342
343 old_ti = ovsl_dereference(flow_table->ti);
344 new_ti = table_instance_alloc(TBL_MIN_BUCKETS);
345 if (!new_ti)
346 return -ENOMEM;
347
348 rcu_assign_pointer(flow_table->ti, new_ti);
349 flow_table->last_rehash = jiffies;
350 flow_table->count = 0;
351
352 table_instance_destroy(old_ti, true);
353 return 0;
354}
355
356static u32 flow_hash(const struct sw_flow_key *key, int key_start,
357 int key_end)
358{
359 u32 *hash_key = (u32 *)((u8 *)key + key_start);
360 int hash_u32s = (key_end - key_start) >> 2;
361
362 /* Make sure number of hash bytes are multiple of u32. */
363 BUILD_BUG_ON(sizeof(long) % sizeof(u32));
364
365 return jhash2(hash_key, hash_u32s, 0);
366}
367
368static int flow_key_start(const struct sw_flow_key *key)
369{
370 if (key->tun_key.ipv4_dst)
371 return 0;
372 else
373 return rounddown(offsetof(struct sw_flow_key, phy),
374 sizeof(long));
375}
376
377static bool cmp_key(const struct sw_flow_key *key1,
378 const struct sw_flow_key *key2,
379 int key_start, int key_end)
380{
381 const long *cp1 = (long *)((u8 *)key1 + key_start);
382 const long *cp2 = (long *)((u8 *)key2 + key_start);
383 long diffs = 0;
384 int i;
385
386 for (i = key_start; i < key_end; i += sizeof(long))
387 diffs |= *cp1++ ^ *cp2++;
388
389 return diffs == 0;
390}
391
392static bool flow_cmp_masked_key(const struct sw_flow *flow,
393 const struct sw_flow_key *key,
394 int key_start, int key_end)
395{
396 return cmp_key(&flow->key, key, key_start, key_end);
397}
398
399bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
400 struct sw_flow_match *match)
401{
402 struct sw_flow_key *key = match->key;
403 int key_start = flow_key_start(key);
404 int key_end = match->range.end;
405
406 return cmp_key(&flow->unmasked_key, key, key_start, key_end);
407}
408
409static struct sw_flow *masked_flow_lookup(struct table_instance *ti,
410 const struct sw_flow_key *unmasked,
411 struct sw_flow_mask *mask)
412{
413 struct sw_flow *flow;
414 struct hlist_head *head;
415 int key_start = mask->range.start;
416 int key_end = mask->range.end;
417 u32 hash;
418 struct sw_flow_key masked_key;
419
420 ovs_flow_mask_key(&masked_key, unmasked, mask);
421 hash = flow_hash(&masked_key, key_start, key_end);
422 head = find_bucket(ti, hash);
423 hlist_for_each_entry_rcu(flow, head, hash_node[ti->node_ver]) {
424 if (flow->mask == mask && flow->hash == hash &&
425 flow_cmp_masked_key(flow, &masked_key,
426 key_start, key_end))
427 return flow;
428 }
429 return NULL;
430}
431
432struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *tbl,
433 const struct sw_flow_key *key,
434 u32 *n_mask_hit)
435{
436 struct table_instance *ti = rcu_dereference(tbl->ti);
437 struct sw_flow_mask *mask;
438 struct sw_flow *flow;
439
440 *n_mask_hit = 0;
441 list_for_each_entry_rcu(mask, &tbl->mask_list, list) {
442 (*n_mask_hit)++;
443 flow = masked_flow_lookup(ti, key, mask);
444 if (flow) /* Found */
445 return flow;
446 }
447 return NULL;
448}
449
450int ovs_flow_tbl_num_masks(const struct flow_table *table)
451{
452 struct sw_flow_mask *mask;
453 int num = 0;
454
455 list_for_each_entry(mask, &table->mask_list, list)
456 num++;
457
458 return num;
459}
460
461static struct table_instance *table_instance_expand(struct table_instance *ti)
462{
463 return table_instance_rehash(ti, ti->n_buckets * 2);
464}
465
466void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow)
467{
468 struct table_instance *ti = ovsl_dereference(table->ti);
469
470 BUG_ON(table->count == 0);
471 hlist_del_rcu(&flow->hash_node[ti->node_ver]);
472 table->count--;
473}
474
475static struct sw_flow_mask *mask_alloc(void)
476{
477 struct sw_flow_mask *mask;
478
479 mask = kmalloc(sizeof(*mask), GFP_KERNEL);
480 if (mask)
481 mask->ref_count = 0;
482
483 return mask;
484}
485
486static void mask_add_ref(struct sw_flow_mask *mask)
487{
488 mask->ref_count++;
489}
490
491static bool mask_equal(const struct sw_flow_mask *a,
492 const struct sw_flow_mask *b)
493{
494 u8 *a_ = (u8 *)&a->key + a->range.start;
495 u8 *b_ = (u8 *)&b->key + b->range.start;
496
497 return (a->range.end == b->range.end)
498 && (a->range.start == b->range.start)
499 && (memcmp(a_, b_, range_n_bytes(&a->range)) == 0);
500}
501
502static struct sw_flow_mask *flow_mask_find(const struct flow_table *tbl,
503 const struct sw_flow_mask *mask)
504{
505 struct list_head *ml;
506
507 list_for_each(ml, &tbl->mask_list) {
508 struct sw_flow_mask *m;
509 m = container_of(ml, struct sw_flow_mask, list);
510 if (mask_equal(mask, m))
511 return m;
512 }
513
514 return NULL;
515}
516
517/**
518 * add a new mask into the mask list.
519 * The caller needs to make sure that 'mask' is not the same
520 * as any masks that are already on the list.
521 */
522static int flow_mask_insert(struct flow_table *tbl, struct sw_flow *flow,
523 struct sw_flow_mask *new)
524{
525 struct sw_flow_mask *mask;
526 mask = flow_mask_find(tbl, new);
527 if (!mask) {
528 /* Allocate a new mask if none exsits. */
529 mask = mask_alloc();
530 if (!mask)
531 return -ENOMEM;
532 mask->key = new->key;
533 mask->range = new->range;
534 list_add_rcu(&mask->list, &tbl->mask_list);
535 }
536
537 mask_add_ref(mask);
538 flow->mask = mask;
539 return 0;
540}
541
542int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
543 struct sw_flow_mask *mask)
544{
545 struct table_instance *new_ti = NULL;
546 struct table_instance *ti;
547 int err;
548
549 err = flow_mask_insert(table, flow, mask);
550 if (err)
551 return err;
552
553 flow->hash = flow_hash(&flow->key, flow->mask->range.start,
554 flow->mask->range.end);
555 ti = ovsl_dereference(table->ti);
556 table_instance_insert(ti, flow);
557 table->count++;
558
559 /* Expand table, if necessary, to make room. */
560 if (table->count > ti->n_buckets)
561 new_ti = table_instance_expand(ti);
562 else if (time_after(jiffies, table->last_rehash + REHASH_INTERVAL))
563 new_ti = table_instance_rehash(ti, ti->n_buckets);
564
565 if (new_ti) {
566 rcu_assign_pointer(table->ti, new_ti);
567 table_instance_destroy(ti, true);
568 table->last_rehash = jiffies;
569 }
570 return 0;
571}
572
573/* Initializes the flow module.
574 * Returns zero if successful or a negative error code. */
575int ovs_flow_init(void)
576{
577 BUILD_BUG_ON(__alignof__(struct sw_flow_key) % __alignof__(long));
578 BUILD_BUG_ON(sizeof(struct sw_flow_key) % sizeof(long));
579
580 flow_cache = kmem_cache_create("sw_flow", sizeof(struct sw_flow), 0,
581 0, NULL);
582 if (flow_cache == NULL)
583 return -ENOMEM;
584
585 return 0;
586}
587
588/* Uninitializes the flow module. */
589void ovs_flow_exit(void)
590{
591 kmem_cache_destroy(flow_cache);
592}
diff --git a/net/openvswitch/flow_table.h b/net/openvswitch/flow_table.h
new file mode 100644
index 000000000000..fbe45d5ad07d
--- /dev/null
+++ b/net/openvswitch/flow_table.h
@@ -0,0 +1,81 @@
1/*
2 * Copyright (c) 2007-2013 Nicira, Inc.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#ifndef FLOW_TABLE_H
20#define FLOW_TABLE_H 1
21
22#include <linux/kernel.h>
23#include <linux/netlink.h>
24#include <linux/openvswitch.h>
25#include <linux/spinlock.h>
26#include <linux/types.h>
27#include <linux/rcupdate.h>
28#include <linux/if_ether.h>
29#include <linux/in6.h>
30#include <linux/jiffies.h>
31#include <linux/time.h>
32#include <linux/flex_array.h>
33
34#include <net/inet_ecn.h>
35#include <net/ip_tunnels.h>
36
37#include "flow.h"
38
39struct table_instance {
40 struct flex_array *buckets;
41 unsigned int n_buckets;
42 struct rcu_head rcu;
43 int node_ver;
44 u32 hash_seed;
45 bool keep_flows;
46};
47
48struct flow_table {
49 struct table_instance __rcu *ti;
50 struct list_head mask_list;
51 unsigned long last_rehash;
52 unsigned int count;
53};
54
55int ovs_flow_init(void);
56void ovs_flow_exit(void);
57
58struct sw_flow *ovs_flow_alloc(void);
59void ovs_flow_free(struct sw_flow *, bool deferred);
60
61int ovs_flow_tbl_init(struct flow_table *);
62int ovs_flow_tbl_count(struct flow_table *table);
63void ovs_flow_tbl_destroy(struct flow_table *table);
64int ovs_flow_tbl_flush(struct flow_table *flow_table);
65
66int ovs_flow_tbl_insert(struct flow_table *table, struct sw_flow *flow,
67 struct sw_flow_mask *mask);
68void ovs_flow_tbl_remove(struct flow_table *table, struct sw_flow *flow);
69int ovs_flow_tbl_num_masks(const struct flow_table *table);
70struct sw_flow *ovs_flow_tbl_dump_next(struct table_instance *table,
71 u32 *bucket, u32 *idx);
72struct sw_flow *ovs_flow_tbl_lookup(struct flow_table *,
73 const struct sw_flow_key *,
74 u32 *n_mask_hit);
75
76bool ovs_flow_cmp_unmasked_key(const struct sw_flow *flow,
77 struct sw_flow_match *match);
78
79void ovs_flow_mask_key(struct sw_flow_key *dst, const struct sw_flow_key *src,
80 const struct sw_flow_mask *mask);
81#endif /* flow_table.h */
diff --git a/net/openvswitch/vport-gre.c b/net/openvswitch/vport-gre.c
index c99dea543d64..a3d6951602db 100644
--- a/net/openvswitch/vport-gre.c
+++ b/net/openvswitch/vport-gre.c
@@ -24,8 +24,6 @@
24#include <linux/if_tunnel.h> 24#include <linux/if_tunnel.h>
25#include <linux/if_vlan.h> 25#include <linux/if_vlan.h>
26#include <linux/in.h> 26#include <linux/in.h>
27#include <linux/if_vlan.h>
28#include <linux/in.h>
29#include <linux/in_route.h> 27#include <linux/in_route.h>
30#include <linux/inetdevice.h> 28#include <linux/inetdevice.h>
31#include <linux/jhash.h> 29#include <linux/jhash.h>
diff --git a/net/openvswitch/vport-internal_dev.c b/net/openvswitch/vport-internal_dev.c
index 98d3edbbc235..729c68763fe7 100644
--- a/net/openvswitch/vport-internal_dev.c
+++ b/net/openvswitch/vport-internal_dev.c
@@ -134,7 +134,7 @@ static void do_setup(struct net_device *netdev)
134 netdev->tx_queue_len = 0; 134 netdev->tx_queue_len = 0;
135 135
136 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST | 136 netdev->features = NETIF_F_LLTX | NETIF_F_SG | NETIF_F_FRAGLIST |
137 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_TSO; 137 NETIF_F_HIGHDMA | NETIF_F_HW_CSUM | NETIF_F_GSO_SOFTWARE;
138 138
139 netdev->vlan_features = netdev->features; 139 netdev->vlan_features = netdev->features;
140 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX; 140 netdev->features |= NETIF_F_HW_VLAN_CTAG_TX;
diff --git a/net/openvswitch/vport-netdev.c b/net/openvswitch/vport-netdev.c
index 09d93c13cfd6..d21f77d875ba 100644
--- a/net/openvswitch/vport-netdev.c
+++ b/net/openvswitch/vport-netdev.c
@@ -150,15 +150,25 @@ static void free_port_rcu(struct rcu_head *rcu)
150 ovs_vport_free(vport_from_priv(netdev_vport)); 150 ovs_vport_free(vport_from_priv(netdev_vport));
151} 151}
152 152
153static void netdev_destroy(struct vport *vport) 153void ovs_netdev_detach_dev(struct vport *vport)
154{ 154{
155 struct netdev_vport *netdev_vport = netdev_vport_priv(vport); 155 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
156 156
157 rtnl_lock(); 157 ASSERT_RTNL();
158 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH; 158 netdev_vport->dev->priv_flags &= ~IFF_OVS_DATAPATH;
159 netdev_rx_handler_unregister(netdev_vport->dev); 159 netdev_rx_handler_unregister(netdev_vport->dev);
160 netdev_upper_dev_unlink(netdev_vport->dev, get_dpdev(vport->dp)); 160 netdev_upper_dev_unlink(netdev_vport->dev,
161 netdev_master_upper_dev_get(netdev_vport->dev));
161 dev_set_promiscuity(netdev_vport->dev, -1); 162 dev_set_promiscuity(netdev_vport->dev, -1);
163}
164
165static void netdev_destroy(struct vport *vport)
166{
167 struct netdev_vport *netdev_vport = netdev_vport_priv(vport);
168
169 rtnl_lock();
170 if (netdev_vport->dev->priv_flags & IFF_OVS_DATAPATH)
171 ovs_netdev_detach_dev(vport);
162 rtnl_unlock(); 172 rtnl_unlock();
163 173
164 call_rcu(&netdev_vport->rcu, free_port_rcu); 174 call_rcu(&netdev_vport->rcu, free_port_rcu);
diff --git a/net/openvswitch/vport-netdev.h b/net/openvswitch/vport-netdev.h
index dd298b5c5cdb..8df01c1127e5 100644
--- a/net/openvswitch/vport-netdev.h
+++ b/net/openvswitch/vport-netdev.h
@@ -39,5 +39,6 @@ netdev_vport_priv(const struct vport *vport)
39} 39}
40 40
41const char *ovs_netdev_get_name(const struct vport *); 41const char *ovs_netdev_get_name(const struct vport *);
42void ovs_netdev_detach_dev(struct vport *);
42 43
43#endif /* vport_netdev.h */ 44#endif /* vport_netdev.h */
diff --git a/net/openvswitch/vport-vxlan.c b/net/openvswitch/vport-vxlan.c
index a481c03e2861..e797a50ac2be 100644
--- a/net/openvswitch/vport-vxlan.c
+++ b/net/openvswitch/vport-vxlan.c
@@ -29,7 +29,6 @@
29#include <net/ip.h> 29#include <net/ip.h>
30#include <net/udp.h> 30#include <net/udp.h>
31#include <net/ip_tunnels.h> 31#include <net/ip_tunnels.h>
32#include <net/udp.h>
33#include <net/rtnetlink.h> 32#include <net/rtnetlink.h>
34#include <net/route.h> 33#include <net/route.h>
35#include <net/dsfield.h> 34#include <net/dsfield.h>
@@ -173,7 +172,7 @@ static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
173 172
174 skb->local_df = 1; 173 skb->local_df = 1;
175 174
176 inet_get_local_port_range(&port_min, &port_max); 175 inet_get_local_port_range(net, &port_min, &port_max);
177 src_port = vxlan_src_port(port_min, port_max, skb); 176 src_port = vxlan_src_port(port_min, port_max, skb);
178 177
179 err = vxlan_xmit_skb(vxlan_port->vs, rt, skb, 178 err = vxlan_xmit_skb(vxlan_port->vs, rt, skb,
diff --git a/net/openvswitch/vport.c b/net/openvswitch/vport.c
index 6f65dbe13812..d830a95f03a4 100644
--- a/net/openvswitch/vport.c
+++ b/net/openvswitch/vport.c
@@ -118,6 +118,7 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
118{ 118{
119 struct vport *vport; 119 struct vport *vport;
120 size_t alloc_size; 120 size_t alloc_size;
121 int i;
121 122
122 alloc_size = sizeof(struct vport); 123 alloc_size = sizeof(struct vport);
123 if (priv_size) { 124 if (priv_size) {
@@ -141,6 +142,13 @@ struct vport *ovs_vport_alloc(int priv_size, const struct vport_ops *ops,
141 return ERR_PTR(-ENOMEM); 142 return ERR_PTR(-ENOMEM);
142 } 143 }
143 144
145 for_each_possible_cpu(i) {
146 struct pcpu_tstats *vport_stats;
147 vport_stats = per_cpu_ptr(vport->percpu_stats, i);
148 u64_stats_init(&vport_stats->syncp);
149 }
150
151
144 spin_lock_init(&vport->stats_lock); 152 spin_lock_init(&vport->stats_lock);
145 153
146 return vport; 154 return vport;
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 2e8286b47c28..88cfbc189558 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -237,6 +237,30 @@ struct packet_skb_cb {
237static void __fanout_unlink(struct sock *sk, struct packet_sock *po); 237static void __fanout_unlink(struct sock *sk, struct packet_sock *po);
238static void __fanout_link(struct sock *sk, struct packet_sock *po); 238static void __fanout_link(struct sock *sk, struct packet_sock *po);
239 239
240static struct net_device *packet_cached_dev_get(struct packet_sock *po)
241{
242 struct net_device *dev;
243
244 rcu_read_lock();
245 dev = rcu_dereference(po->cached_dev);
246 if (likely(dev))
247 dev_hold(dev);
248 rcu_read_unlock();
249
250 return dev;
251}
252
253static void packet_cached_dev_assign(struct packet_sock *po,
254 struct net_device *dev)
255{
256 rcu_assign_pointer(po->cached_dev, dev);
257}
258
259static void packet_cached_dev_reset(struct packet_sock *po)
260{
261 RCU_INIT_POINTER(po->cached_dev, NULL);
262}
263
240/* register_prot_hook must be invoked with the po->bind_lock held, 264/* register_prot_hook must be invoked with the po->bind_lock held,
241 * or from a context in which asynchronous accesses to the packet 265 * or from a context in which asynchronous accesses to the packet
242 * socket is not possible (packet_create()). 266 * socket is not possible (packet_create()).
@@ -244,11 +268,13 @@ static void __fanout_link(struct sock *sk, struct packet_sock *po);
244static void register_prot_hook(struct sock *sk) 268static void register_prot_hook(struct sock *sk)
245{ 269{
246 struct packet_sock *po = pkt_sk(sk); 270 struct packet_sock *po = pkt_sk(sk);
271
247 if (!po->running) { 272 if (!po->running) {
248 if (po->fanout) 273 if (po->fanout)
249 __fanout_link(sk, po); 274 __fanout_link(sk, po);
250 else 275 else
251 dev_add_pack(&po->prot_hook); 276 dev_add_pack(&po->prot_hook);
277
252 sock_hold(sk); 278 sock_hold(sk);
253 po->running = 1; 279 po->running = 1;
254 } 280 }
@@ -266,10 +292,12 @@ static void __unregister_prot_hook(struct sock *sk, bool sync)
266 struct packet_sock *po = pkt_sk(sk); 292 struct packet_sock *po = pkt_sk(sk);
267 293
268 po->running = 0; 294 po->running = 0;
295
269 if (po->fanout) 296 if (po->fanout)
270 __fanout_unlink(sk, po); 297 __fanout_unlink(sk, po);
271 else 298 else
272 __dev_remove_pack(&po->prot_hook); 299 __dev_remove_pack(&po->prot_hook);
300
273 __sock_put(sk); 301 __sock_put(sk);
274 302
275 if (sync) { 303 if (sync) {
@@ -432,9 +460,9 @@ static void prb_shutdown_retire_blk_timer(struct packet_sock *po,
432 460
433 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc; 461 pkc = tx_ring ? &po->tx_ring.prb_bdqc : &po->rx_ring.prb_bdqc;
434 462
435 spin_lock(&rb_queue->lock); 463 spin_lock_bh(&rb_queue->lock);
436 pkc->delete_blk_timer = 1; 464 pkc->delete_blk_timer = 1;
437 spin_unlock(&rb_queue->lock); 465 spin_unlock_bh(&rb_queue->lock);
438 466
439 prb_del_retire_blk_timer(pkc); 467 prb_del_retire_blk_timer(pkc);
440} 468}
@@ -2057,7 +2085,6 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2057 struct sk_buff *skb; 2085 struct sk_buff *skb;
2058 struct net_device *dev; 2086 struct net_device *dev;
2059 __be16 proto; 2087 __be16 proto;
2060 bool need_rls_dev = false;
2061 int err, reserve = 0; 2088 int err, reserve = 0;
2062 void *ph; 2089 void *ph;
2063 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name; 2090 struct sockaddr_ll *saddr = (struct sockaddr_ll *)msg->msg_name;
@@ -2069,8 +2096,8 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2069 2096
2070 mutex_lock(&po->pg_vec_lock); 2097 mutex_lock(&po->pg_vec_lock);
2071 2098
2072 if (saddr == NULL) { 2099 if (likely(saddr == NULL)) {
2073 dev = po->prot_hook.dev; 2100 dev = packet_cached_dev_get(po);
2074 proto = po->num; 2101 proto = po->num;
2075 addr = NULL; 2102 addr = NULL;
2076 } else { 2103 } else {
@@ -2084,19 +2111,17 @@ static int tpacket_snd(struct packet_sock *po, struct msghdr *msg)
2084 proto = saddr->sll_protocol; 2111 proto = saddr->sll_protocol;
2085 addr = saddr->sll_addr; 2112 addr = saddr->sll_addr;
2086 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex); 2113 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex);
2087 need_rls_dev = true;
2088 } 2114 }
2089 2115
2090 err = -ENXIO; 2116 err = -ENXIO;
2091 if (unlikely(dev == NULL)) 2117 if (unlikely(dev == NULL))
2092 goto out; 2118 goto out;
2093
2094 reserve = dev->hard_header_len;
2095
2096 err = -ENETDOWN; 2119 err = -ENETDOWN;
2097 if (unlikely(!(dev->flags & IFF_UP))) 2120 if (unlikely(!(dev->flags & IFF_UP)))
2098 goto out_put; 2121 goto out_put;
2099 2122
2123 reserve = dev->hard_header_len;
2124
2100 size_max = po->tx_ring.frame_size 2125 size_max = po->tx_ring.frame_size
2101 - (po->tp_hdrlen - sizeof(struct sockaddr_ll)); 2126 - (po->tp_hdrlen - sizeof(struct sockaddr_ll));
2102 2127
@@ -2173,8 +2198,7 @@ out_status:
2173 __packet_set_status(po, ph, status); 2198 __packet_set_status(po, ph, status);
2174 kfree_skb(skb); 2199 kfree_skb(skb);
2175out_put: 2200out_put:
2176 if (need_rls_dev) 2201 dev_put(dev);
2177 dev_put(dev);
2178out: 2202out:
2179 mutex_unlock(&po->pg_vec_lock); 2203 mutex_unlock(&po->pg_vec_lock);
2180 return err; 2204 return err;
@@ -2212,7 +2236,6 @@ static int packet_snd(struct socket *sock,
2212 struct sk_buff *skb; 2236 struct sk_buff *skb;
2213 struct net_device *dev; 2237 struct net_device *dev;
2214 __be16 proto; 2238 __be16 proto;
2215 bool need_rls_dev = false;
2216 unsigned char *addr; 2239 unsigned char *addr;
2217 int err, reserve = 0; 2240 int err, reserve = 0;
2218 struct virtio_net_hdr vnet_hdr = { 0 }; 2241 struct virtio_net_hdr vnet_hdr = { 0 };
@@ -2227,8 +2250,8 @@ static int packet_snd(struct socket *sock,
2227 * Get and verify the address. 2250 * Get and verify the address.
2228 */ 2251 */
2229 2252
2230 if (saddr == NULL) { 2253 if (likely(saddr == NULL)) {
2231 dev = po->prot_hook.dev; 2254 dev = packet_cached_dev_get(po);
2232 proto = po->num; 2255 proto = po->num;
2233 addr = NULL; 2256 addr = NULL;
2234 } else { 2257 } else {
@@ -2240,19 +2263,17 @@ static int packet_snd(struct socket *sock,
2240 proto = saddr->sll_protocol; 2263 proto = saddr->sll_protocol;
2241 addr = saddr->sll_addr; 2264 addr = saddr->sll_addr;
2242 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex); 2265 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex);
2243 need_rls_dev = true;
2244 } 2266 }
2245 2267
2246 err = -ENXIO; 2268 err = -ENXIO;
2247 if (dev == NULL) 2269 if (unlikely(dev == NULL))
2248 goto out_unlock; 2270 goto out_unlock;
2249 if (sock->type == SOCK_RAW)
2250 reserve = dev->hard_header_len;
2251
2252 err = -ENETDOWN; 2271 err = -ENETDOWN;
2253 if (!(dev->flags & IFF_UP)) 2272 if (unlikely(!(dev->flags & IFF_UP)))
2254 goto out_unlock; 2273 goto out_unlock;
2255 2274
2275 if (sock->type == SOCK_RAW)
2276 reserve = dev->hard_header_len;
2256 if (po->has_vnet_hdr) { 2277 if (po->has_vnet_hdr) {
2257 vnet_hdr_len = sizeof(vnet_hdr); 2278 vnet_hdr_len = sizeof(vnet_hdr);
2258 2279
@@ -2386,15 +2407,14 @@ static int packet_snd(struct socket *sock,
2386 if (err > 0 && (err = net_xmit_errno(err)) != 0) 2407 if (err > 0 && (err = net_xmit_errno(err)) != 0)
2387 goto out_unlock; 2408 goto out_unlock;
2388 2409
2389 if (need_rls_dev) 2410 dev_put(dev);
2390 dev_put(dev);
2391 2411
2392 return len; 2412 return len;
2393 2413
2394out_free: 2414out_free:
2395 kfree_skb(skb); 2415 kfree_skb(skb);
2396out_unlock: 2416out_unlock:
2397 if (dev && need_rls_dev) 2417 if (dev)
2398 dev_put(dev); 2418 dev_put(dev);
2399out: 2419out:
2400 return err; 2420 return err;
@@ -2439,6 +2459,8 @@ static int packet_release(struct socket *sock)
2439 2459
2440 spin_lock(&po->bind_lock); 2460 spin_lock(&po->bind_lock);
2441 unregister_prot_hook(sk, false); 2461 unregister_prot_hook(sk, false);
2462 packet_cached_dev_reset(po);
2463
2442 if (po->prot_hook.dev) { 2464 if (po->prot_hook.dev) {
2443 dev_put(po->prot_hook.dev); 2465 dev_put(po->prot_hook.dev);
2444 po->prot_hook.dev = NULL; 2466 po->prot_hook.dev = NULL;
@@ -2494,14 +2516,17 @@ static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protoc
2494 2516
2495 spin_lock(&po->bind_lock); 2517 spin_lock(&po->bind_lock);
2496 unregister_prot_hook(sk, true); 2518 unregister_prot_hook(sk, true);
2519
2497 po->num = protocol; 2520 po->num = protocol;
2498 po->prot_hook.type = protocol; 2521 po->prot_hook.type = protocol;
2499 if (po->prot_hook.dev) 2522 if (po->prot_hook.dev)
2500 dev_put(po->prot_hook.dev); 2523 dev_put(po->prot_hook.dev);
2501 po->prot_hook.dev = dev;
2502 2524
2525 po->prot_hook.dev = dev;
2503 po->ifindex = dev ? dev->ifindex : 0; 2526 po->ifindex = dev ? dev->ifindex : 0;
2504 2527
2528 packet_cached_dev_assign(po, dev);
2529
2505 if (protocol == 0) 2530 if (protocol == 0)
2506 goto out_unlock; 2531 goto out_unlock;
2507 2532
@@ -2615,6 +2640,8 @@ static int packet_create(struct net *net, struct socket *sock, int protocol,
2615 sk->sk_family = PF_PACKET; 2640 sk->sk_family = PF_PACKET;
2616 po->num = proto; 2641 po->num = proto;
2617 2642
2643 packet_cached_dev_reset(po);
2644
2618 sk->sk_destruct = packet_sock_destruct; 2645 sk->sk_destruct = packet_sock_destruct;
2619 sk_refcnt_debug_inc(sk); 2646 sk_refcnt_debug_inc(sk);
2620 2647
@@ -2660,7 +2687,6 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2660 struct sock *sk = sock->sk; 2687 struct sock *sk = sock->sk;
2661 struct sk_buff *skb; 2688 struct sk_buff *skb;
2662 int copied, err; 2689 int copied, err;
2663 struct sockaddr_ll *sll;
2664 int vnet_hdr_len = 0; 2690 int vnet_hdr_len = 0;
2665 2691
2666 err = -EINVAL; 2692 err = -EINVAL;
@@ -2744,22 +2770,10 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2744 goto out_free; 2770 goto out_free;
2745 } 2771 }
2746 2772
2747 /* 2773 /* You lose any data beyond the buffer you gave. If it worries
2748 * If the address length field is there to be filled in, we fill 2774 * a user program they can ask the device for its MTU
2749 * it in now. 2775 * anyway.
2750 */ 2776 */
2751
2752 sll = &PACKET_SKB_CB(skb)->sa.ll;
2753 if (sock->type == SOCK_PACKET)
2754 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2755 else
2756 msg->msg_namelen = sll->sll_halen + offsetof(struct sockaddr_ll, sll_addr);
2757
2758 /*
2759 * You lose any data beyond the buffer you gave. If it worries a
2760 * user program they can ask the device for its MTU anyway.
2761 */
2762
2763 copied = skb->len; 2777 copied = skb->len;
2764 if (copied > len) { 2778 if (copied > len) {
2765 copied = len; 2779 copied = len;
@@ -2772,9 +2786,20 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2772 2786
2773 sock_recv_ts_and_drops(msg, sk, skb); 2787 sock_recv_ts_and_drops(msg, sk, skb);
2774 2788
2775 if (msg->msg_name) 2789 if (msg->msg_name) {
2790 /* If the address length field is there to be filled
2791 * in, we fill it in now.
2792 */
2793 if (sock->type == SOCK_PACKET) {
2794 msg->msg_namelen = sizeof(struct sockaddr_pkt);
2795 } else {
2796 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
2797 msg->msg_namelen = sll->sll_halen +
2798 offsetof(struct sockaddr_ll, sll_addr);
2799 }
2776 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, 2800 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa,
2777 msg->msg_namelen); 2801 msg->msg_namelen);
2802 }
2778 2803
2779 if (pkt_sk(sk)->auxdata) { 2804 if (pkt_sk(sk)->auxdata) {
2780 struct tpacket_auxdata aux; 2805 struct tpacket_auxdata aux;
@@ -3326,6 +3351,7 @@ static int packet_notifier(struct notifier_block *this,
3326 sk->sk_error_report(sk); 3351 sk->sk_error_report(sk);
3327 } 3352 }
3328 if (msg == NETDEV_UNREGISTER) { 3353 if (msg == NETDEV_UNREGISTER) {
3354 packet_cached_dev_reset(po);
3329 po->ifindex = -1; 3355 po->ifindex = -1;
3330 if (po->prot_hook.dev) 3356 if (po->prot_hook.dev)
3331 dev_put(po->prot_hook.dev); 3357 dev_put(po->prot_hook.dev);
diff --git a/net/packet/internal.h b/net/packet/internal.h
index c4e4b4561207..1035fa2d909c 100644
--- a/net/packet/internal.h
+++ b/net/packet/internal.h
@@ -113,6 +113,7 @@ struct packet_sock {
113 unsigned int tp_loss:1; 113 unsigned int tp_loss:1;
114 unsigned int tp_tx_has_off:1; 114 unsigned int tp_tx_has_off:1;
115 unsigned int tp_tstamp; 115 unsigned int tp_tstamp;
116 struct net_device __rcu *cached_dev;
116 struct packet_type prot_hook ____cacheline_aligned_in_smp; 117 struct packet_type prot_hook ____cacheline_aligned_in_smp;
117}; 118};
118 119
diff --git a/net/phonet/datagram.c b/net/phonet/datagram.c
index 12c30f3e643e..38946b26e471 100644
--- a/net/phonet/datagram.c
+++ b/net/phonet/datagram.c
@@ -139,9 +139,6 @@ static int pn_recvmsg(struct kiocb *iocb, struct sock *sk,
139 MSG_CMSG_COMPAT)) 139 MSG_CMSG_COMPAT))
140 goto out_nofree; 140 goto out_nofree;
141 141
142 if (addr_len)
143 *addr_len = sizeof(sa);
144
145 skb = skb_recv_datagram(sk, flags, noblock, &rval); 142 skb = skb_recv_datagram(sk, flags, noblock, &rval);
146 if (skb == NULL) 143 if (skb == NULL)
147 goto out_nofree; 144 goto out_nofree;
@@ -162,8 +159,10 @@ static int pn_recvmsg(struct kiocb *iocb, struct sock *sk,
162 159
163 rval = (flags & MSG_TRUNC) ? skb->len : copylen; 160 rval = (flags & MSG_TRUNC) ? skb->len : copylen;
164 161
165 if (msg->msg_name != NULL) 162 if (msg->msg_name != NULL) {
166 memcpy(msg->msg_name, &sa, sizeof(struct sockaddr_pn)); 163 memcpy(msg->msg_name, &sa, sizeof(sa));
164 *addr_len = sizeof(sa);
165 }
167 166
168out: 167out:
169 skb_free_datagram(sk, skb); 168 skb_free_datagram(sk, skb);
diff --git a/net/phonet/socket.c b/net/phonet/socket.c
index 77e38f733496..008214a3d5eb 100644
--- a/net/phonet/socket.c
+++ b/net/phonet/socket.c
@@ -595,26 +595,25 @@ static void pn_sock_seq_stop(struct seq_file *seq, void *v)
595 595
596static int pn_sock_seq_show(struct seq_file *seq, void *v) 596static int pn_sock_seq_show(struct seq_file *seq, void *v)
597{ 597{
598 int len; 598 seq_setwidth(seq, 127);
599
600 if (v == SEQ_START_TOKEN) 599 if (v == SEQ_START_TOKEN)
601 seq_printf(seq, "%s%n", "pt loc rem rs st tx_queue rx_queue " 600 seq_puts(seq, "pt loc rem rs st tx_queue rx_queue "
602 " uid inode ref pointer drops", &len); 601 " uid inode ref pointer drops");
603 else { 602 else {
604 struct sock *sk = v; 603 struct sock *sk = v;
605 struct pn_sock *pn = pn_sk(sk); 604 struct pn_sock *pn = pn_sk(sk);
606 605
607 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu " 606 seq_printf(seq, "%2d %04X:%04X:%02X %02X %08X:%08X %5d %lu "
608 "%d %pK %d%n", 607 "%d %pK %d",
609 sk->sk_protocol, pn->sobject, pn->dobject, 608 sk->sk_protocol, pn->sobject, pn->dobject,
610 pn->resource, sk->sk_state, 609 pn->resource, sk->sk_state,
611 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk), 610 sk_wmem_alloc_get(sk), sk_rmem_alloc_get(sk),
612 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)), 611 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
613 sock_i_ino(sk), 612 sock_i_ino(sk),
614 atomic_read(&sk->sk_refcnt), sk, 613 atomic_read(&sk->sk_refcnt), sk,
615 atomic_read(&sk->sk_drops), &len); 614 atomic_read(&sk->sk_drops));
616 } 615 }
617 seq_printf(seq, "%*s\n", 127 - len, ""); 616 seq_pad(seq, '\n');
618 return 0; 617 return 0;
619} 618}
620 619
@@ -785,20 +784,19 @@ static void pn_res_seq_stop(struct seq_file *seq, void *v)
785 784
786static int pn_res_seq_show(struct seq_file *seq, void *v) 785static int pn_res_seq_show(struct seq_file *seq, void *v)
787{ 786{
788 int len; 787 seq_setwidth(seq, 63);
789
790 if (v == SEQ_START_TOKEN) 788 if (v == SEQ_START_TOKEN)
791 seq_printf(seq, "%s%n", "rs uid inode", &len); 789 seq_puts(seq, "rs uid inode");
792 else { 790 else {
793 struct sock **psk = v; 791 struct sock **psk = v;
794 struct sock *sk = *psk; 792 struct sock *sk = *psk;
795 793
796 seq_printf(seq, "%02X %5u %lu%n", 794 seq_printf(seq, "%02X %5u %lu",
797 (int) (psk - pnres.sk), 795 (int) (psk - pnres.sk),
798 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)), 796 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
799 sock_i_ino(sk), &len); 797 sock_i_ino(sk));
800 } 798 }
801 seq_printf(seq, "%*s\n", 63 - len, ""); 799 seq_pad(seq, '\n');
802 return 0; 800 return 0;
803} 801}
804 802
diff --git a/net/rds/connection.c b/net/rds/connection.c
index 642ad42c416b..378c3a6acf84 100644
--- a/net/rds/connection.c
+++ b/net/rds/connection.c
@@ -51,10 +51,16 @@ static struct kmem_cache *rds_conn_slab;
51 51
52static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr) 52static struct hlist_head *rds_conn_bucket(__be32 laddr, __be32 faddr)
53{ 53{
54 static u32 rds_hash_secret __read_mostly;
55
56 unsigned long hash;
57
58 net_get_random_once(&rds_hash_secret, sizeof(rds_hash_secret));
59
54 /* Pass NULL, don't need struct net for hash */ 60 /* Pass NULL, don't need struct net for hash */
55 unsigned long hash = inet_ehashfn(NULL, 61 hash = __inet_ehashfn(be32_to_cpu(laddr), 0,
56 be32_to_cpu(laddr), 0, 62 be32_to_cpu(faddr), 0,
57 be32_to_cpu(faddr), 0); 63 rds_hash_secret);
58 return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK]; 64 return &rds_conn_hash[hash & RDS_CONNECTION_HASH_MASK];
59} 65}
60 66
diff --git a/net/rds/ib.c b/net/rds/ib.c
index b4c8b0022fee..ba2dffeff608 100644
--- a/net/rds/ib.c
+++ b/net/rds/ib.c
@@ -338,7 +338,8 @@ static int rds_ib_laddr_check(__be32 addr)
338 ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin); 338 ret = rdma_bind_addr(cm_id, (struct sockaddr *)&sin);
339 /* due to this, we will claim to support iWARP devices unless we 339 /* due to this, we will claim to support iWARP devices unless we
340 check node_type. */ 340 check node_type. */
341 if (ret || cm_id->device->node_type != RDMA_NODE_IB_CA) 341 if (ret || !cm_id->device ||
342 cm_id->device->node_type != RDMA_NODE_IB_CA)
342 ret = -EADDRNOTAVAIL; 343 ret = -EADDRNOTAVAIL;
343 344
344 rdsdebug("addr %pI4 ret %d node type %d\n", 345 rdsdebug("addr %pI4 ret %d node type %d\n",
diff --git a/net/rds/ib_recv.c b/net/rds/ib_recv.c
index 8eb9501e3d60..b7ebe23cdedf 100644
--- a/net/rds/ib_recv.c
+++ b/net/rds/ib_recv.c
@@ -421,8 +421,7 @@ static void rds_ib_recv_cache_put(struct list_head *new_item,
421 struct rds_ib_refill_cache *cache) 421 struct rds_ib_refill_cache *cache)
422{ 422{
423 unsigned long flags; 423 unsigned long flags;
424 struct list_head *old; 424 struct list_head *old, *chpfirst;
425 struct list_head __percpu *chpfirst;
426 425
427 local_irq_save(flags); 426 local_irq_save(flags);
428 427
@@ -432,7 +431,7 @@ static void rds_ib_recv_cache_put(struct list_head *new_item,
432 else /* put on front */ 431 else /* put on front */
433 list_add_tail(new_item, chpfirst); 432 list_add_tail(new_item, chpfirst);
434 433
435 __this_cpu_write(chpfirst, new_item); 434 __this_cpu_write(cache->percpu->first, new_item);
436 __this_cpu_inc(cache->percpu->count); 435 __this_cpu_inc(cache->percpu->count);
437 436
438 if (__this_cpu_read(cache->percpu->count) < RDS_IB_RECYCLE_BATCH_COUNT) 437 if (__this_cpu_read(cache->percpu->count) < RDS_IB_RECYCLE_BATCH_COUNT)
@@ -452,7 +451,7 @@ static void rds_ib_recv_cache_put(struct list_head *new_item,
452 } while (old); 451 } while (old);
453 452
454 453
455 __this_cpu_write(chpfirst, NULL); 454 __this_cpu_write(cache->percpu->first, NULL);
456 __this_cpu_write(cache->percpu->count, 0); 455 __this_cpu_write(cache->percpu->count, 0);
457end: 456end:
458 local_irq_restore(flags); 457 local_irq_restore(flags);
diff --git a/net/rds/ib_send.c b/net/rds/ib_send.c
index e59094981175..37be6e226d1b 100644
--- a/net/rds/ib_send.c
+++ b/net/rds/ib_send.c
@@ -552,9 +552,8 @@ int rds_ib_xmit(struct rds_connection *conn, struct rds_message *rm,
552 && rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) { 552 && rm->m_inc.i_hdr.h_flags & RDS_FLAG_CONG_BITMAP) {
553 rds_cong_map_updated(conn->c_fcong, ~(u64) 0); 553 rds_cong_map_updated(conn->c_fcong, ~(u64) 0);
554 scat = &rm->data.op_sg[sg]; 554 scat = &rm->data.op_sg[sg];
555 ret = sizeof(struct rds_header) + RDS_CONG_MAP_BYTES; 555 ret = max_t(int, RDS_CONG_MAP_BYTES, scat->length);
556 ret = min_t(int, ret, scat->length - conn->c_xmit_data_off); 556 return sizeof(struct rds_header) + ret;
557 return ret;
558 } 557 }
559 558
560 /* FIXME we may overallocate here */ 559 /* FIXME we may overallocate here */
diff --git a/net/rds/rds.h b/net/rds/rds.h
index ec1d731ecff0..48f8ffc60f8f 100644
--- a/net/rds/rds.h
+++ b/net/rds/rds.h
@@ -749,7 +749,7 @@ void rds_atomic_send_complete(struct rds_message *rm, int wc_status);
749int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm, 749int rds_cmsg_atomic(struct rds_sock *rs, struct rds_message *rm,
750 struct cmsghdr *cmsg); 750 struct cmsghdr *cmsg);
751 751
752extern void __rds_put_mr_final(struct rds_mr *mr); 752void __rds_put_mr_final(struct rds_mr *mr);
753static inline void rds_mr_put(struct rds_mr *mr) 753static inline void rds_mr_put(struct rds_mr *mr)
754{ 754{
755 if (atomic_dec_and_test(&mr->r_refcount)) 755 if (atomic_dec_and_test(&mr->r_refcount))
diff --git a/net/rds/recv.c b/net/rds/recv.c
index 9f0f17cf6bf9..de339b24ca14 100644
--- a/net/rds/recv.c
+++ b/net/rds/recv.c
@@ -410,8 +410,6 @@ int rds_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
410 410
411 rdsdebug("size %zu flags 0x%x timeo %ld\n", size, msg_flags, timeo); 411 rdsdebug("size %zu flags 0x%x timeo %ld\n", size, msg_flags, timeo);
412 412
413 msg->msg_namelen = 0;
414
415 if (msg_flags & MSG_OOB) 413 if (msg_flags & MSG_OOB)
416 goto out; 414 goto out;
417 415
diff --git a/net/rfkill/Kconfig b/net/rfkill/Kconfig
index 78efe895b663..4c10e7e6c9f6 100644
--- a/net/rfkill/Kconfig
+++ b/net/rfkill/Kconfig
@@ -36,7 +36,7 @@ config RFKILL_REGULATOR
36 36
37config RFKILL_GPIO 37config RFKILL_GPIO
38 tristate "GPIO RFKILL driver" 38 tristate "GPIO RFKILL driver"
39 depends on RFKILL && GPIOLIB && HAVE_CLK 39 depends on RFKILL && GPIOLIB
40 default n 40 default n
41 help 41 help
42 If you say yes here you get support of a generic gpio RFKILL 42 If you say yes here you get support of a generic gpio RFKILL
diff --git a/net/rfkill/rfkill-gpio.c b/net/rfkill/rfkill-gpio.c
index fb076cd6f808..5620d3c07479 100644
--- a/net/rfkill/rfkill-gpio.c
+++ b/net/rfkill/rfkill-gpio.c
@@ -24,27 +24,23 @@
24#include <linux/platform_device.h> 24#include <linux/platform_device.h>
25#include <linux/clk.h> 25#include <linux/clk.h>
26#include <linux/slab.h> 26#include <linux/slab.h>
27#include <linux/acpi.h>
28#include <linux/acpi_gpio.h>
27 29
28#include <linux/rfkill-gpio.h> 30#include <linux/rfkill-gpio.h>
29 31
30enum rfkill_gpio_clk_state { 32struct rfkill_gpio_data {
31 UNSPECIFIED = 0, 33 const char *name;
32 PWR_ENABLED, 34 enum rfkill_type type;
33 PWR_DISABLED 35 int reset_gpio;
34}; 36 int shutdown_gpio;
35 37
36#define PWR_CLK_SET(_RF, _EN) \ 38 struct rfkill *rfkill_dev;
37 ((_RF)->pwr_clk_enabled = (!(_EN) ? PWR_ENABLED : PWR_DISABLED)) 39 char *reset_name;
38#define PWR_CLK_ENABLED(_RF) ((_RF)->pwr_clk_enabled == PWR_ENABLED) 40 char *shutdown_name;
39#define PWR_CLK_DISABLED(_RF) ((_RF)->pwr_clk_enabled != PWR_ENABLED) 41 struct clk *clk;
40 42
41struct rfkill_gpio_data { 43 bool clk_enabled;
42 struct rfkill_gpio_platform_data *pdata;
43 struct rfkill *rfkill_dev;
44 char *reset_name;
45 char *shutdown_name;
46 enum rfkill_gpio_clk_state pwr_clk_enabled;
47 struct clk *pwr_clk;
48}; 44};
49 45
50static int rfkill_gpio_set_power(void *data, bool blocked) 46static int rfkill_gpio_set_power(void *data, bool blocked)
@@ -52,23 +48,22 @@ static int rfkill_gpio_set_power(void *data, bool blocked)
52 struct rfkill_gpio_data *rfkill = data; 48 struct rfkill_gpio_data *rfkill = data;
53 49
54 if (blocked) { 50 if (blocked) {
55 if (gpio_is_valid(rfkill->pdata->shutdown_gpio)) 51 if (gpio_is_valid(rfkill->shutdown_gpio))
56 gpio_direction_output(rfkill->pdata->shutdown_gpio, 0); 52 gpio_set_value(rfkill->shutdown_gpio, 0);
57 if (gpio_is_valid(rfkill->pdata->reset_gpio)) 53 if (gpio_is_valid(rfkill->reset_gpio))
58 gpio_direction_output(rfkill->pdata->reset_gpio, 0); 54 gpio_set_value(rfkill->reset_gpio, 0);
59 if (rfkill->pwr_clk && PWR_CLK_ENABLED(rfkill)) 55 if (!IS_ERR(rfkill->clk) && rfkill->clk_enabled)
60 clk_disable(rfkill->pwr_clk); 56 clk_disable(rfkill->clk);
61 } else { 57 } else {
62 if (rfkill->pwr_clk && PWR_CLK_DISABLED(rfkill)) 58 if (!IS_ERR(rfkill->clk) && !rfkill->clk_enabled)
63 clk_enable(rfkill->pwr_clk); 59 clk_enable(rfkill->clk);
64 if (gpio_is_valid(rfkill->pdata->reset_gpio)) 60 if (gpio_is_valid(rfkill->reset_gpio))
65 gpio_direction_output(rfkill->pdata->reset_gpio, 1); 61 gpio_set_value(rfkill->reset_gpio, 1);
66 if (gpio_is_valid(rfkill->pdata->shutdown_gpio)) 62 if (gpio_is_valid(rfkill->shutdown_gpio))
67 gpio_direction_output(rfkill->pdata->shutdown_gpio, 1); 63 gpio_set_value(rfkill->shutdown_gpio, 1);
68 } 64 }
69 65
70 if (rfkill->pwr_clk) 66 rfkill->clk_enabled = blocked;
71 PWR_CLK_SET(rfkill, blocked);
72 67
73 return 0; 68 return 0;
74} 69}
@@ -77,117 +72,112 @@ static const struct rfkill_ops rfkill_gpio_ops = {
77 .set_block = rfkill_gpio_set_power, 72 .set_block = rfkill_gpio_set_power,
78}; 73};
79 74
75static int rfkill_gpio_acpi_probe(struct device *dev,
76 struct rfkill_gpio_data *rfkill)
77{
78 const struct acpi_device_id *id;
79
80 id = acpi_match_device(dev->driver->acpi_match_table, dev);
81 if (!id)
82 return -ENODEV;
83
84 rfkill->name = dev_name(dev);
85 rfkill->type = (unsigned)id->driver_data;
86 rfkill->reset_gpio = acpi_get_gpio_by_index(dev, 0, NULL);
87 rfkill->shutdown_gpio = acpi_get_gpio_by_index(dev, 1, NULL);
88
89 return 0;
90}
91
80static int rfkill_gpio_probe(struct platform_device *pdev) 92static int rfkill_gpio_probe(struct platform_device *pdev)
81{ 93{
82 struct rfkill_gpio_data *rfkill;
83 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data; 94 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
95 struct rfkill_gpio_data *rfkill;
96 const char *clk_name = NULL;
84 int ret = 0; 97 int ret = 0;
85 int len = 0; 98 int len = 0;
86 99
87 if (!pdata) { 100 rfkill = devm_kzalloc(&pdev->dev, sizeof(*rfkill), GFP_KERNEL);
88 pr_warn("%s: No platform data specified\n", __func__); 101 if (!rfkill)
89 return -EINVAL; 102 return -ENOMEM;
103
104 if (ACPI_HANDLE(&pdev->dev)) {
105 ret = rfkill_gpio_acpi_probe(&pdev->dev, rfkill);
106 if (ret)
107 return ret;
108 } else if (pdata) {
109 clk_name = pdata->power_clk_name;
110 rfkill->name = pdata->name;
111 rfkill->type = pdata->type;
112 rfkill->reset_gpio = pdata->reset_gpio;
113 rfkill->shutdown_gpio = pdata->shutdown_gpio;
114 } else {
115 return -ENODEV;
90 } 116 }
91 117
92 /* make sure at-least one of the GPIO is defined and that 118 /* make sure at-least one of the GPIO is defined and that
93 * a name is specified for this instance */ 119 * a name is specified for this instance */
94 if (!pdata->name || (!gpio_is_valid(pdata->reset_gpio) && 120 if ((!gpio_is_valid(rfkill->reset_gpio) &&
95 !gpio_is_valid(pdata->shutdown_gpio))) { 121 !gpio_is_valid(rfkill->shutdown_gpio)) || !rfkill->name) {
96 pr_warn("%s: invalid platform data\n", __func__); 122 pr_warn("%s: invalid platform data\n", __func__);
97 return -EINVAL; 123 return -EINVAL;
98 } 124 }
99 125
100 rfkill = kzalloc(sizeof(*rfkill), GFP_KERNEL); 126 if (pdata && pdata->gpio_runtime_setup) {
101 if (!rfkill)
102 return -ENOMEM;
103
104 if (pdata->gpio_runtime_setup) {
105 ret = pdata->gpio_runtime_setup(pdev); 127 ret = pdata->gpio_runtime_setup(pdev);
106 if (ret) { 128 if (ret) {
107 pr_warn("%s: can't set up gpio\n", __func__); 129 pr_warn("%s: can't set up gpio\n", __func__);
108 goto fail_alloc; 130 return ret;
109 } 131 }
110 } 132 }
111 133
112 rfkill->pdata = pdata; 134 len = strlen(rfkill->name);
113 135 rfkill->reset_name = devm_kzalloc(&pdev->dev, len + 7, GFP_KERNEL);
114 len = strlen(pdata->name); 136 if (!rfkill->reset_name)
115 rfkill->reset_name = kzalloc(len + 7, GFP_KERNEL); 137 return -ENOMEM;
116 if (!rfkill->reset_name) {
117 ret = -ENOMEM;
118 goto fail_alloc;
119 }
120 138
121 rfkill->shutdown_name = kzalloc(len + 10, GFP_KERNEL); 139 rfkill->shutdown_name = devm_kzalloc(&pdev->dev, len + 10, GFP_KERNEL);
122 if (!rfkill->shutdown_name) { 140 if (!rfkill->shutdown_name)
123 ret = -ENOMEM; 141 return -ENOMEM;
124 goto fail_reset_name;
125 }
126 142
127 snprintf(rfkill->reset_name, len + 6 , "%s_reset", pdata->name); 143 snprintf(rfkill->reset_name, len + 6 , "%s_reset", rfkill->name);
128 snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", pdata->name); 144 snprintf(rfkill->shutdown_name, len + 9, "%s_shutdown", rfkill->name);
129 145
130 if (pdata->power_clk_name) { 146 rfkill->clk = devm_clk_get(&pdev->dev, clk_name);
131 rfkill->pwr_clk = clk_get(&pdev->dev, pdata->power_clk_name);
132 if (IS_ERR(rfkill->pwr_clk)) {
133 pr_warn("%s: can't find pwr_clk.\n", __func__);
134 ret = PTR_ERR(rfkill->pwr_clk);
135 goto fail_shutdown_name;
136 }
137 }
138 147
139 if (gpio_is_valid(pdata->reset_gpio)) { 148 if (gpio_is_valid(rfkill->reset_gpio)) {
140 ret = gpio_request(pdata->reset_gpio, rfkill->reset_name); 149 ret = devm_gpio_request_one(&pdev->dev, rfkill->reset_gpio,
150 0, rfkill->reset_name);
141 if (ret) { 151 if (ret) {
142 pr_warn("%s: failed to get reset gpio.\n", __func__); 152 pr_warn("%s: failed to get reset gpio.\n", __func__);
143 goto fail_clock; 153 return ret;
144 } 154 }
145 } 155 }
146 156
147 if (gpio_is_valid(pdata->shutdown_gpio)) { 157 if (gpio_is_valid(rfkill->shutdown_gpio)) {
148 ret = gpio_request(pdata->shutdown_gpio, rfkill->shutdown_name); 158 ret = devm_gpio_request_one(&pdev->dev, rfkill->shutdown_gpio,
159 0, rfkill->shutdown_name);
149 if (ret) { 160 if (ret) {
150 pr_warn("%s: failed to get shutdown gpio.\n", __func__); 161 pr_warn("%s: failed to get shutdown gpio.\n", __func__);
151 goto fail_reset; 162 return ret;
152 } 163 }
153 } 164 }
154 165
155 rfkill->rfkill_dev = rfkill_alloc(pdata->name, &pdev->dev, pdata->type, 166 rfkill->rfkill_dev = rfkill_alloc(rfkill->name, &pdev->dev,
156 &rfkill_gpio_ops, rfkill); 167 rfkill->type, &rfkill_gpio_ops,
157 if (!rfkill->rfkill_dev) { 168 rfkill);
158 ret = -ENOMEM; 169 if (!rfkill->rfkill_dev)
159 goto fail_shutdown; 170 return -ENOMEM;
160 }
161 171
162 ret = rfkill_register(rfkill->rfkill_dev); 172 ret = rfkill_register(rfkill->rfkill_dev);
163 if (ret < 0) 173 if (ret < 0)
164 goto fail_rfkill; 174 return ret;
165 175
166 platform_set_drvdata(pdev, rfkill); 176 platform_set_drvdata(pdev, rfkill);
167 177
168 dev_info(&pdev->dev, "%s device registered.\n", pdata->name); 178 dev_info(&pdev->dev, "%s device registered.\n", rfkill->name);
169 179
170 return 0; 180 return 0;
171
172fail_rfkill:
173 rfkill_destroy(rfkill->rfkill_dev);
174fail_shutdown:
175 if (gpio_is_valid(pdata->shutdown_gpio))
176 gpio_free(pdata->shutdown_gpio);
177fail_reset:
178 if (gpio_is_valid(pdata->reset_gpio))
179 gpio_free(pdata->reset_gpio);
180fail_clock:
181 if (rfkill->pwr_clk)
182 clk_put(rfkill->pwr_clk);
183fail_shutdown_name:
184 kfree(rfkill->shutdown_name);
185fail_reset_name:
186 kfree(rfkill->reset_name);
187fail_alloc:
188 kfree(rfkill);
189
190 return ret;
191} 181}
192 182
193static int rfkill_gpio_remove(struct platform_device *pdev) 183static int rfkill_gpio_remove(struct platform_device *pdev)
@@ -195,31 +185,26 @@ static int rfkill_gpio_remove(struct platform_device *pdev)
195 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev); 185 struct rfkill_gpio_data *rfkill = platform_get_drvdata(pdev);
196 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data; 186 struct rfkill_gpio_platform_data *pdata = pdev->dev.platform_data;
197 187
198 if (pdata->gpio_runtime_close) 188 if (pdata && pdata->gpio_runtime_close)
199 pdata->gpio_runtime_close(pdev); 189 pdata->gpio_runtime_close(pdev);
200 rfkill_unregister(rfkill->rfkill_dev); 190 rfkill_unregister(rfkill->rfkill_dev);
201 rfkill_destroy(rfkill->rfkill_dev); 191 rfkill_destroy(rfkill->rfkill_dev);
202 if (gpio_is_valid(rfkill->pdata->shutdown_gpio))
203 gpio_free(rfkill->pdata->shutdown_gpio);
204 if (gpio_is_valid(rfkill->pdata->reset_gpio))
205 gpio_free(rfkill->pdata->reset_gpio);
206 if (rfkill->pwr_clk && PWR_CLK_ENABLED(rfkill))
207 clk_disable(rfkill->pwr_clk);
208 if (rfkill->pwr_clk)
209 clk_put(rfkill->pwr_clk);
210 kfree(rfkill->shutdown_name);
211 kfree(rfkill->reset_name);
212 kfree(rfkill);
213 192
214 return 0; 193 return 0;
215} 194}
216 195
196static const struct acpi_device_id rfkill_acpi_match[] = {
197 { "BCM4752", RFKILL_TYPE_GPS },
198 { },
199};
200
217static struct platform_driver rfkill_gpio_driver = { 201static struct platform_driver rfkill_gpio_driver = {
218 .probe = rfkill_gpio_probe, 202 .probe = rfkill_gpio_probe,
219 .remove = rfkill_gpio_remove, 203 .remove = rfkill_gpio_remove,
220 .driver = { 204 .driver = {
221 .name = "rfkill_gpio", 205 .name = "rfkill_gpio",
222 .owner = THIS_MODULE, 206 .owner = THIS_MODULE,
207 .acpi_match_table = ACPI_PTR(rfkill_acpi_match),
223 }, 208 },
224}; 209};
225 210
diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
index e98fcfbe6007..62ced6516c58 100644
--- a/net/rose/af_rose.c
+++ b/net/rose/af_rose.c
@@ -1216,7 +1216,6 @@ static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1216{ 1216{
1217 struct sock *sk = sock->sk; 1217 struct sock *sk = sock->sk;
1218 struct rose_sock *rose = rose_sk(sk); 1218 struct rose_sock *rose = rose_sk(sk);
1219 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1220 size_t copied; 1219 size_t copied;
1221 unsigned char *asmptr; 1220 unsigned char *asmptr;
1222 struct sk_buff *skb; 1221 struct sk_buff *skb;
@@ -1252,24 +1251,19 @@ static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1252 1251
1253 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 1252 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1254 1253
1255 if (srose != NULL) { 1254 if (msg->msg_name) {
1256 memset(srose, 0, msg->msg_namelen); 1255 struct sockaddr_rose *srose;
1256 struct full_sockaddr_rose *full_srose = msg->msg_name;
1257
1258 memset(msg->msg_name, 0, sizeof(struct full_sockaddr_rose));
1259 srose = msg->msg_name;
1257 srose->srose_family = AF_ROSE; 1260 srose->srose_family = AF_ROSE;
1258 srose->srose_addr = rose->dest_addr; 1261 srose->srose_addr = rose->dest_addr;
1259 srose->srose_call = rose->dest_call; 1262 srose->srose_call = rose->dest_call;
1260 srose->srose_ndigis = rose->dest_ndigis; 1263 srose->srose_ndigis = rose->dest_ndigis;
1261 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) { 1264 for (n = 0 ; n < rose->dest_ndigis ; n++)
1262 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name; 1265 full_srose->srose_digis[n] = rose->dest_digis[n];
1263 for (n = 0 ; n < rose->dest_ndigis ; n++) 1266 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1264 full_srose->srose_digis[n] = rose->dest_digis[n];
1265 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1266 } else {
1267 if (rose->dest_ndigis >= 1) {
1268 srose->srose_ndigis = 1;
1269 srose->srose_digi = rose->dest_digis[0];
1270 }
1271 msg->msg_namelen = sizeof(struct sockaddr_rose);
1272 }
1273 } 1267 }
1274 1268
1275 skb_free_datagram(sk, skb); 1269 skb_free_datagram(sk, skb);
diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h
index a693aca2ae2e..5f43675ee1df 100644
--- a/net/rxrpc/ar-internal.h
+++ b/net/rxrpc/ar-internal.h
@@ -426,17 +426,16 @@ extern struct workqueue_struct *rxrpc_workqueue;
426/* 426/*
427 * ar-accept.c 427 * ar-accept.c
428 */ 428 */
429extern void rxrpc_accept_incoming_calls(struct work_struct *); 429void rxrpc_accept_incoming_calls(struct work_struct *);
430extern struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *, 430struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *, unsigned long);
431 unsigned long); 431int rxrpc_reject_call(struct rxrpc_sock *);
432extern int rxrpc_reject_call(struct rxrpc_sock *);
433 432
434/* 433/*
435 * ar-ack.c 434 * ar-ack.c
436 */ 435 */
437extern void __rxrpc_propose_ACK(struct rxrpc_call *, u8, __be32, bool); 436void __rxrpc_propose_ACK(struct rxrpc_call *, u8, __be32, bool);
438extern void rxrpc_propose_ACK(struct rxrpc_call *, u8, __be32, bool); 437void rxrpc_propose_ACK(struct rxrpc_call *, u8, __be32, bool);
439extern void rxrpc_process_call(struct work_struct *); 438void rxrpc_process_call(struct work_struct *);
440 439
441/* 440/*
442 * ar-call.c 441 * ar-call.c
@@ -445,19 +444,18 @@ extern struct kmem_cache *rxrpc_call_jar;
445extern struct list_head rxrpc_calls; 444extern struct list_head rxrpc_calls;
446extern rwlock_t rxrpc_call_lock; 445extern rwlock_t rxrpc_call_lock;
447 446
448extern struct rxrpc_call *rxrpc_get_client_call(struct rxrpc_sock *, 447struct rxrpc_call *rxrpc_get_client_call(struct rxrpc_sock *,
449 struct rxrpc_transport *, 448 struct rxrpc_transport *,
450 struct rxrpc_conn_bundle *, 449 struct rxrpc_conn_bundle *,
451 unsigned long, int, gfp_t); 450 unsigned long, int, gfp_t);
452extern struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *, 451struct rxrpc_call *rxrpc_incoming_call(struct rxrpc_sock *,
453 struct rxrpc_connection *, 452 struct rxrpc_connection *,
454 struct rxrpc_header *, gfp_t); 453 struct rxrpc_header *, gfp_t);
455extern struct rxrpc_call *rxrpc_find_server_call(struct rxrpc_sock *, 454struct rxrpc_call *rxrpc_find_server_call(struct rxrpc_sock *, unsigned long);
456 unsigned long); 455void rxrpc_release_call(struct rxrpc_call *);
457extern void rxrpc_release_call(struct rxrpc_call *); 456void rxrpc_release_calls_on_socket(struct rxrpc_sock *);
458extern void rxrpc_release_calls_on_socket(struct rxrpc_sock *); 457void __rxrpc_put_call(struct rxrpc_call *);
459extern void __rxrpc_put_call(struct rxrpc_call *); 458void __exit rxrpc_destroy_all_calls(void);
460extern void __exit rxrpc_destroy_all_calls(void);
461 459
462/* 460/*
463 * ar-connection.c 461 * ar-connection.c
@@ -465,19 +463,16 @@ extern void __exit rxrpc_destroy_all_calls(void);
465extern struct list_head rxrpc_connections; 463extern struct list_head rxrpc_connections;
466extern rwlock_t rxrpc_connection_lock; 464extern rwlock_t rxrpc_connection_lock;
467 465
468extern struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *, 466struct rxrpc_conn_bundle *rxrpc_get_bundle(struct rxrpc_sock *,
469 struct rxrpc_transport *, 467 struct rxrpc_transport *,
470 struct key *, 468 struct key *, __be16, gfp_t);
471 __be16, gfp_t); 469void rxrpc_put_bundle(struct rxrpc_transport *, struct rxrpc_conn_bundle *);
472extern void rxrpc_put_bundle(struct rxrpc_transport *, 470int rxrpc_connect_call(struct rxrpc_sock *, struct rxrpc_transport *,
473 struct rxrpc_conn_bundle *); 471 struct rxrpc_conn_bundle *, struct rxrpc_call *, gfp_t);
474extern int rxrpc_connect_call(struct rxrpc_sock *, struct rxrpc_transport *, 472void rxrpc_put_connection(struct rxrpc_connection *);
475 struct rxrpc_conn_bundle *, struct rxrpc_call *, 473void __exit rxrpc_destroy_all_connections(void);
476 gfp_t); 474struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *,
477extern void rxrpc_put_connection(struct rxrpc_connection *); 475 struct rxrpc_header *);
478extern void __exit rxrpc_destroy_all_connections(void);
479extern struct rxrpc_connection *rxrpc_find_connection(struct rxrpc_transport *,
480 struct rxrpc_header *);
481extern struct rxrpc_connection * 476extern struct rxrpc_connection *
482rxrpc_incoming_connection(struct rxrpc_transport *, struct rxrpc_header *, 477rxrpc_incoming_connection(struct rxrpc_transport *, struct rxrpc_header *,
483 gfp_t); 478 gfp_t);
@@ -485,15 +480,15 @@ rxrpc_incoming_connection(struct rxrpc_transport *, struct rxrpc_header *,
485/* 480/*
486 * ar-connevent.c 481 * ar-connevent.c
487 */ 482 */
488extern void rxrpc_process_connection(struct work_struct *); 483void rxrpc_process_connection(struct work_struct *);
489extern void rxrpc_reject_packet(struct rxrpc_local *, struct sk_buff *); 484void rxrpc_reject_packet(struct rxrpc_local *, struct sk_buff *);
490extern void rxrpc_reject_packets(struct work_struct *); 485void rxrpc_reject_packets(struct work_struct *);
491 486
492/* 487/*
493 * ar-error.c 488 * ar-error.c
494 */ 489 */
495extern void rxrpc_UDP_error_report(struct sock *); 490void rxrpc_UDP_error_report(struct sock *);
496extern void rxrpc_UDP_error_handler(struct work_struct *); 491void rxrpc_UDP_error_handler(struct work_struct *);
497 492
498/* 493/*
499 * ar-input.c 494 * ar-input.c
@@ -501,18 +496,17 @@ extern void rxrpc_UDP_error_handler(struct work_struct *);
501extern unsigned long rxrpc_ack_timeout; 496extern unsigned long rxrpc_ack_timeout;
502extern const char *rxrpc_pkts[]; 497extern const char *rxrpc_pkts[];
503 498
504extern void rxrpc_data_ready(struct sock *, int); 499void rxrpc_data_ready(struct sock *, int);
505extern int rxrpc_queue_rcv_skb(struct rxrpc_call *, struct sk_buff *, bool, 500int rxrpc_queue_rcv_skb(struct rxrpc_call *, struct sk_buff *, bool, bool);
506 bool); 501void rxrpc_fast_process_packet(struct rxrpc_call *, struct sk_buff *);
507extern void rxrpc_fast_process_packet(struct rxrpc_call *, struct sk_buff *);
508 502
509/* 503/*
510 * ar-local.c 504 * ar-local.c
511 */ 505 */
512extern rwlock_t rxrpc_local_lock; 506extern rwlock_t rxrpc_local_lock;
513extern struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *); 507struct rxrpc_local *rxrpc_lookup_local(struct sockaddr_rxrpc *);
514extern void rxrpc_put_local(struct rxrpc_local *); 508void rxrpc_put_local(struct rxrpc_local *);
515extern void __exit rxrpc_destroy_all_locals(void); 509void __exit rxrpc_destroy_all_locals(void);
516 510
517/* 511/*
518 * ar-key.c 512 * ar-key.c
@@ -520,31 +514,29 @@ extern void __exit rxrpc_destroy_all_locals(void);
520extern struct key_type key_type_rxrpc; 514extern struct key_type key_type_rxrpc;
521extern struct key_type key_type_rxrpc_s; 515extern struct key_type key_type_rxrpc_s;
522 516
523extern int rxrpc_request_key(struct rxrpc_sock *, char __user *, int); 517int rxrpc_request_key(struct rxrpc_sock *, char __user *, int);
524extern int rxrpc_server_keyring(struct rxrpc_sock *, char __user *, int); 518int rxrpc_server_keyring(struct rxrpc_sock *, char __user *, int);
525extern int rxrpc_get_server_data_key(struct rxrpc_connection *, const void *, 519int rxrpc_get_server_data_key(struct rxrpc_connection *, const void *, time_t,
526 time_t, u32); 520 u32);
527 521
528/* 522/*
529 * ar-output.c 523 * ar-output.c
530 */ 524 */
531extern int rxrpc_resend_timeout; 525extern int rxrpc_resend_timeout;
532 526
533extern int rxrpc_send_packet(struct rxrpc_transport *, struct sk_buff *); 527int rxrpc_send_packet(struct rxrpc_transport *, struct sk_buff *);
534extern int rxrpc_client_sendmsg(struct kiocb *, struct rxrpc_sock *, 528int rxrpc_client_sendmsg(struct kiocb *, struct rxrpc_sock *,
535 struct rxrpc_transport *, struct msghdr *, 529 struct rxrpc_transport *, struct msghdr *, size_t);
536 size_t); 530int rxrpc_server_sendmsg(struct kiocb *, struct rxrpc_sock *, struct msghdr *,
537extern int rxrpc_server_sendmsg(struct kiocb *, struct rxrpc_sock *, 531 size_t);
538 struct msghdr *, size_t);
539 532
540/* 533/*
541 * ar-peer.c 534 * ar-peer.c
542 */ 535 */
543extern struct rxrpc_peer *rxrpc_get_peer(struct sockaddr_rxrpc *, gfp_t); 536struct rxrpc_peer *rxrpc_get_peer(struct sockaddr_rxrpc *, gfp_t);
544extern void rxrpc_put_peer(struct rxrpc_peer *); 537void rxrpc_put_peer(struct rxrpc_peer *);
545extern struct rxrpc_peer *rxrpc_find_peer(struct rxrpc_local *, 538struct rxrpc_peer *rxrpc_find_peer(struct rxrpc_local *, __be32, __be16);
546 __be32, __be16); 539void __exit rxrpc_destroy_all_peers(void);
547extern void __exit rxrpc_destroy_all_peers(void);
548 540
549/* 541/*
550 * ar-proc.c 542 * ar-proc.c
@@ -556,38 +548,36 @@ extern const struct file_operations rxrpc_connection_seq_fops;
556/* 548/*
557 * ar-recvmsg.c 549 * ar-recvmsg.c
558 */ 550 */
559extern void rxrpc_remove_user_ID(struct rxrpc_sock *, struct rxrpc_call *); 551void rxrpc_remove_user_ID(struct rxrpc_sock *, struct rxrpc_call *);
560extern int rxrpc_recvmsg(struct kiocb *, struct socket *, struct msghdr *, 552int rxrpc_recvmsg(struct kiocb *, struct socket *, struct msghdr *, size_t,
561 size_t, int); 553 int);
562 554
563/* 555/*
564 * ar-security.c 556 * ar-security.c
565 */ 557 */
566extern int rxrpc_register_security(struct rxrpc_security *); 558int rxrpc_register_security(struct rxrpc_security *);
567extern void rxrpc_unregister_security(struct rxrpc_security *); 559void rxrpc_unregister_security(struct rxrpc_security *);
568extern int rxrpc_init_client_conn_security(struct rxrpc_connection *); 560int rxrpc_init_client_conn_security(struct rxrpc_connection *);
569extern int rxrpc_init_server_conn_security(struct rxrpc_connection *); 561int rxrpc_init_server_conn_security(struct rxrpc_connection *);
570extern int rxrpc_secure_packet(const struct rxrpc_call *, struct sk_buff *, 562int rxrpc_secure_packet(const struct rxrpc_call *, struct sk_buff *, size_t,
571 size_t, void *); 563 void *);
572extern int rxrpc_verify_packet(const struct rxrpc_call *, struct sk_buff *, 564int rxrpc_verify_packet(const struct rxrpc_call *, struct sk_buff *, u32 *);
573 u32 *); 565void rxrpc_clear_conn_security(struct rxrpc_connection *);
574extern void rxrpc_clear_conn_security(struct rxrpc_connection *);
575 566
576/* 567/*
577 * ar-skbuff.c 568 * ar-skbuff.c
578 */ 569 */
579extern void rxrpc_packet_destructor(struct sk_buff *); 570void rxrpc_packet_destructor(struct sk_buff *);
580 571
581/* 572/*
582 * ar-transport.c 573 * ar-transport.c
583 */ 574 */
584extern struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *, 575struct rxrpc_transport *rxrpc_get_transport(struct rxrpc_local *,
585 struct rxrpc_peer *, 576 struct rxrpc_peer *, gfp_t);
586 gfp_t); 577void rxrpc_put_transport(struct rxrpc_transport *);
587extern void rxrpc_put_transport(struct rxrpc_transport *); 578void __exit rxrpc_destroy_all_transports(void);
588extern void __exit rxrpc_destroy_all_transports(void); 579struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *,
589extern struct rxrpc_transport *rxrpc_find_transport(struct rxrpc_local *, 580 struct rxrpc_peer *);
590 struct rxrpc_peer *);
591 581
592/* 582/*
593 * debug tracing 583 * debug tracing
diff --git a/net/rxrpc/ar-recvmsg.c b/net/rxrpc/ar-recvmsg.c
index 4b48687c3890..898492a8d61b 100644
--- a/net/rxrpc/ar-recvmsg.c
+++ b/net/rxrpc/ar-recvmsg.c
@@ -143,10 +143,13 @@ int rxrpc_recvmsg(struct kiocb *iocb, struct socket *sock,
143 143
144 /* copy the peer address and timestamp */ 144 /* copy the peer address and timestamp */
145 if (!continue_call) { 145 if (!continue_call) {
146 if (msg->msg_name && msg->msg_namelen > 0) 146 if (msg->msg_name) {
147 size_t len =
148 sizeof(call->conn->trans->peer->srx);
147 memcpy(msg->msg_name, 149 memcpy(msg->msg_name,
148 &call->conn->trans->peer->srx, 150 &call->conn->trans->peer->srx, len);
149 sizeof(call->conn->trans->peer->srx)); 151 msg->msg_namelen = len;
152 }
150 sock_recv_ts_and_drops(msg, &rx->sk, skb); 153 sock_recv_ts_and_drops(msg, &rx->sk, skb);
151 } 154 }
152 155
diff --git a/net/sched/Kconfig b/net/sched/Kconfig
index c03a32a0418e..ad1f1d819203 100644
--- a/net/sched/Kconfig
+++ b/net/sched/Kconfig
@@ -443,6 +443,16 @@ config NET_CLS_CGROUP
443 To compile this code as a module, choose M here: the 443 To compile this code as a module, choose M here: the
444 module will be called cls_cgroup. 444 module will be called cls_cgroup.
445 445
446config NET_CLS_BPF
447 tristate "BPF-based classifier"
448 select NET_CLS
449 ---help---
450 If you say Y here, you will be able to classify packets based on
451 programmable BPF (JIT'ed) filters as an alternative to ematches.
452
453 To compile this code as a module, choose M here: the module will
454 be called cls_bpf.
455
446config NET_EMATCH 456config NET_EMATCH
447 bool "Extended Matches" 457 bool "Extended Matches"
448 select NET_CLS 458 select NET_CLS
diff --git a/net/sched/Makefile b/net/sched/Makefile
index e5f9abe9a5db..35fa47a494ab 100644
--- a/net/sched/Makefile
+++ b/net/sched/Makefile
@@ -50,6 +50,7 @@ obj-$(CONFIG_NET_CLS_RSVP6) += cls_rsvp6.o
50obj-$(CONFIG_NET_CLS_BASIC) += cls_basic.o 50obj-$(CONFIG_NET_CLS_BASIC) += cls_basic.o
51obj-$(CONFIG_NET_CLS_FLOW) += cls_flow.o 51obj-$(CONFIG_NET_CLS_FLOW) += cls_flow.o
52obj-$(CONFIG_NET_CLS_CGROUP) += cls_cgroup.o 52obj-$(CONFIG_NET_CLS_CGROUP) += cls_cgroup.o
53obj-$(CONFIG_NET_CLS_BPF) += cls_bpf.o
53obj-$(CONFIG_NET_EMATCH) += ematch.o 54obj-$(CONFIG_NET_EMATCH) += ematch.o
54obj-$(CONFIG_NET_EMATCH_CMP) += em_cmp.o 55obj-$(CONFIG_NET_EMATCH_CMP) += em_cmp.o
55obj-$(CONFIG_NET_EMATCH_NBYTE) += em_nbyte.o 56obj-$(CONFIG_NET_EMATCH_NBYTE) += em_nbyte.o
diff --git a/net/sched/act_api.c b/net/sched/act_api.c
index fd7072827a40..69cb848e8345 100644
--- a/net/sched/act_api.c
+++ b/net/sched/act_api.c
@@ -270,6 +270,16 @@ int tcf_register_action(struct tc_action_ops *act)
270{ 270{
271 struct tc_action_ops *a, **ap; 271 struct tc_action_ops *a, **ap;
272 272
273 /* Must supply act, dump, cleanup and init */
274 if (!act->act || !act->dump || !act->cleanup || !act->init)
275 return -EINVAL;
276
277 /* Supply defaults */
278 if (!act->lookup)
279 act->lookup = tcf_hash_search;
280 if (!act->walk)
281 act->walk = tcf_generic_walker;
282
273 write_lock(&act_mod_lock); 283 write_lock(&act_mod_lock);
274 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) { 284 for (ap = &act_base; (a = *ap) != NULL; ap = &a->next) {
275 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) { 285 if (act->type == a->type || (strcmp(act->kind, a->kind) == 0)) {
@@ -381,7 +391,7 @@ int tcf_action_exec(struct sk_buff *skb, const struct tc_action *act,
381 } 391 }
382 while ((a = act) != NULL) { 392 while ((a = act) != NULL) {
383repeat: 393repeat:
384 if (a->ops && a->ops->act) { 394 if (a->ops) {
385 ret = a->ops->act(skb, a, res); 395 ret = a->ops->act(skb, a, res);
386 if (TC_MUNGED & skb->tc_verd) { 396 if (TC_MUNGED & skb->tc_verd) {
387 /* copied already, allow trampling */ 397 /* copied already, allow trampling */
@@ -405,7 +415,7 @@ void tcf_action_destroy(struct tc_action *act, int bind)
405 struct tc_action *a; 415 struct tc_action *a;
406 416
407 for (a = act; a; a = act) { 417 for (a = act; a; a = act) {
408 if (a->ops && a->ops->cleanup) { 418 if (a->ops) {
409 if (a->ops->cleanup(a, bind) == ACT_P_DELETED) 419 if (a->ops->cleanup(a, bind) == ACT_P_DELETED)
410 module_put(a->ops->owner); 420 module_put(a->ops->owner);
411 act = act->next; 421 act = act->next;
@@ -424,7 +434,7 @@ tcf_action_dump_old(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
424{ 434{
425 int err = -EINVAL; 435 int err = -EINVAL;
426 436
427 if (a->ops == NULL || a->ops->dump == NULL) 437 if (a->ops == NULL)
428 return err; 438 return err;
429 return a->ops->dump(skb, a, bind, ref); 439 return a->ops->dump(skb, a, bind, ref);
430} 440}
@@ -436,7 +446,7 @@ tcf_action_dump_1(struct sk_buff *skb, struct tc_action *a, int bind, int ref)
436 unsigned char *b = skb_tail_pointer(skb); 446 unsigned char *b = skb_tail_pointer(skb);
437 struct nlattr *nest; 447 struct nlattr *nest;
438 448
439 if (a->ops == NULL || a->ops->dump == NULL) 449 if (a->ops == NULL)
440 return err; 450 return err;
441 451
442 if (nla_put_string(skb, TCA_KIND, a->ops->kind)) 452 if (nla_put_string(skb, TCA_KIND, a->ops->kind))
@@ -723,8 +733,6 @@ tcf_action_get_1(struct nlattr *nla, struct nlmsghdr *n, u32 portid)
723 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]); 733 a->ops = tc_lookup_action(tb[TCA_ACT_KIND]);
724 if (a->ops == NULL) 734 if (a->ops == NULL)
725 goto err_free; 735 goto err_free;
726 if (a->ops->lookup == NULL)
727 goto err_mod;
728 err = -ENOENT; 736 err = -ENOENT;
729 if (a->ops->lookup(a, index) == 0) 737 if (a->ops->lookup(a, index) == 0)
730 goto err_mod; 738 goto err_mod;
@@ -1084,12 +1092,6 @@ tc_dump_action(struct sk_buff *skb, struct netlink_callback *cb)
1084 memset(&a, 0, sizeof(struct tc_action)); 1092 memset(&a, 0, sizeof(struct tc_action));
1085 a.ops = a_o; 1093 a.ops = a_o;
1086 1094
1087 if (a_o->walk == NULL) {
1088 WARN(1, "tc_dump_action: %s !capable of dumping table\n",
1089 a_o->kind);
1090 goto out_module_put;
1091 }
1092
1093 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1095 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1094 cb->nlh->nlmsg_type, sizeof(*t), 0); 1096 cb->nlh->nlmsg_type, sizeof(*t), 0);
1095 if (!nlh) 1097 if (!nlh)
diff --git a/net/sched/act_csum.c b/net/sched/act_csum.c
index 3a4c0caa1f7d..11fe1a416433 100644
--- a/net/sched/act_csum.c
+++ b/net/sched/act_csum.c
@@ -77,16 +77,16 @@ static int tcf_csum_init(struct net *n, struct nlattr *nla, struct nlattr *est,
77 &csum_idx_gen, &csum_hash_info); 77 &csum_idx_gen, &csum_hash_info);
78 if (IS_ERR(pc)) 78 if (IS_ERR(pc))
79 return PTR_ERR(pc); 79 return PTR_ERR(pc);
80 p = to_tcf_csum(pc);
81 ret = ACT_P_CREATED; 80 ret = ACT_P_CREATED;
82 } else { 81 } else {
83 p = to_tcf_csum(pc); 82 if (bind)/* dont override defaults */
84 if (!ovr) { 83 return 0;
85 tcf_hash_release(pc, bind, &csum_hash_info); 84 tcf_hash_release(pc, bind, &csum_hash_info);
85 if (!ovr)
86 return -EEXIST; 86 return -EEXIST;
87 }
88 } 87 }
89 88
89 p = to_tcf_csum(pc);
90 spin_lock_bh(&p->tcf_lock); 90 spin_lock_bh(&p->tcf_lock);
91 p->tcf_action = parm->action; 91 p->tcf_action = parm->action;
92 p->update_flags = parm->update_flags; 92 p->update_flags = parm->update_flags;
@@ -585,9 +585,7 @@ static struct tc_action_ops act_csum_ops = {
585 .act = tcf_csum, 585 .act = tcf_csum,
586 .dump = tcf_csum_dump, 586 .dump = tcf_csum_dump,
587 .cleanup = tcf_csum_cleanup, 587 .cleanup = tcf_csum_cleanup,
588 .lookup = tcf_hash_search,
589 .init = tcf_csum_init, 588 .init = tcf_csum_init,
590 .walk = tcf_generic_walker
591}; 589};
592 590
593MODULE_DESCRIPTION("Checksum updating actions"); 591MODULE_DESCRIPTION("Checksum updating actions");
diff --git a/net/sched/act_gact.c b/net/sched/act_gact.c
index fd2b3cff5fa2..eb9ba60ebab4 100644
--- a/net/sched/act_gact.c
+++ b/net/sched/act_gact.c
@@ -102,10 +102,11 @@ static int tcf_gact_init(struct net *net, struct nlattr *nla,
102 return PTR_ERR(pc); 102 return PTR_ERR(pc);
103 ret = ACT_P_CREATED; 103 ret = ACT_P_CREATED;
104 } else { 104 } else {
105 if (!ovr) { 105 if (bind)/* dont override defaults */
106 tcf_hash_release(pc, bind, &gact_hash_info); 106 return 0;
107 tcf_hash_release(pc, bind, &gact_hash_info);
108 if (!ovr)
107 return -EEXIST; 109 return -EEXIST;
108 }
109 } 110 }
110 111
111 gact = to_gact(pc); 112 gact = to_gact(pc);
@@ -206,9 +207,7 @@ static struct tc_action_ops act_gact_ops = {
206 .act = tcf_gact, 207 .act = tcf_gact,
207 .dump = tcf_gact_dump, 208 .dump = tcf_gact_dump,
208 .cleanup = tcf_gact_cleanup, 209 .cleanup = tcf_gact_cleanup,
209 .lookup = tcf_hash_search,
210 .init = tcf_gact_init, 210 .init = tcf_gact_init,
211 .walk = tcf_generic_walker
212}; 211};
213 212
214MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); 213MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
diff --git a/net/sched/act_ipt.c b/net/sched/act_ipt.c
index 60d88b6b9560..dcbfe8ce04a6 100644
--- a/net/sched/act_ipt.c
+++ b/net/sched/act_ipt.c
@@ -141,10 +141,12 @@ static int tcf_ipt_init(struct net *net, struct nlattr *nla, struct nlattr *est,
141 return PTR_ERR(pc); 141 return PTR_ERR(pc);
142 ret = ACT_P_CREATED; 142 ret = ACT_P_CREATED;
143 } else { 143 } else {
144 if (!ovr) { 144 if (bind)/* dont override defaults */
145 tcf_ipt_release(to_ipt(pc), bind); 145 return 0;
146 tcf_ipt_release(to_ipt(pc), bind);
147
148 if (!ovr)
146 return -EEXIST; 149 return -EEXIST;
147 }
148 } 150 }
149 ipt = to_ipt(pc); 151 ipt = to_ipt(pc);
150 152
@@ -298,9 +300,7 @@ static struct tc_action_ops act_ipt_ops = {
298 .act = tcf_ipt, 300 .act = tcf_ipt,
299 .dump = tcf_ipt_dump, 301 .dump = tcf_ipt_dump,
300 .cleanup = tcf_ipt_cleanup, 302 .cleanup = tcf_ipt_cleanup,
301 .lookup = tcf_hash_search,
302 .init = tcf_ipt_init, 303 .init = tcf_ipt_init,
303 .walk = tcf_generic_walker
304}; 304};
305 305
306static struct tc_action_ops act_xt_ops = { 306static struct tc_action_ops act_xt_ops = {
@@ -312,9 +312,7 @@ static struct tc_action_ops act_xt_ops = {
312 .act = tcf_ipt, 312 .act = tcf_ipt,
313 .dump = tcf_ipt_dump, 313 .dump = tcf_ipt_dump,
314 .cleanup = tcf_ipt_cleanup, 314 .cleanup = tcf_ipt_cleanup,
315 .lookup = tcf_hash_search,
316 .init = tcf_ipt_init, 315 .init = tcf_ipt_init,
317 .walk = tcf_generic_walker
318}; 316};
319 317
320MODULE_AUTHOR("Jamal Hadi Salim(2002-13)"); 318MODULE_AUTHOR("Jamal Hadi Salim(2002-13)");
diff --git a/net/sched/act_mirred.c b/net/sched/act_mirred.c
index 977c10e0631b..252378121ce7 100644
--- a/net/sched/act_mirred.c
+++ b/net/sched/act_mirred.c
@@ -271,9 +271,7 @@ static struct tc_action_ops act_mirred_ops = {
271 .act = tcf_mirred, 271 .act = tcf_mirred,
272 .dump = tcf_mirred_dump, 272 .dump = tcf_mirred_dump,
273 .cleanup = tcf_mirred_cleanup, 273 .cleanup = tcf_mirred_cleanup,
274 .lookup = tcf_hash_search,
275 .init = tcf_mirred_init, 274 .init = tcf_mirred_init,
276 .walk = tcf_generic_walker
277}; 275};
278 276
279MODULE_AUTHOR("Jamal Hadi Salim(2002)"); 277MODULE_AUTHOR("Jamal Hadi Salim(2002)");
diff --git a/net/sched/act_nat.c b/net/sched/act_nat.c
index 876f0ef29694..76869538d028 100644
--- a/net/sched/act_nat.c
+++ b/net/sched/act_nat.c
@@ -70,15 +70,15 @@ static int tcf_nat_init(struct net *net, struct nlattr *nla, struct nlattr *est,
70 &nat_idx_gen, &nat_hash_info); 70 &nat_idx_gen, &nat_hash_info);
71 if (IS_ERR(pc)) 71 if (IS_ERR(pc))
72 return PTR_ERR(pc); 72 return PTR_ERR(pc);
73 p = to_tcf_nat(pc);
74 ret = ACT_P_CREATED; 73 ret = ACT_P_CREATED;
75 } else { 74 } else {
76 p = to_tcf_nat(pc); 75 if (bind)
77 if (!ovr) { 76 return 0;
78 tcf_hash_release(pc, bind, &nat_hash_info); 77 tcf_hash_release(pc, bind, &nat_hash_info);
78 if (!ovr)
79 return -EEXIST; 79 return -EEXIST;
80 }
81 } 80 }
81 p = to_tcf_nat(pc);
82 82
83 spin_lock_bh(&p->tcf_lock); 83 spin_lock_bh(&p->tcf_lock);
84 p->old_addr = parm->old_addr; 84 p->old_addr = parm->old_addr;
@@ -308,9 +308,7 @@ static struct tc_action_ops act_nat_ops = {
308 .act = tcf_nat, 308 .act = tcf_nat,
309 .dump = tcf_nat_dump, 309 .dump = tcf_nat_dump,
310 .cleanup = tcf_nat_cleanup, 310 .cleanup = tcf_nat_cleanup,
311 .lookup = tcf_hash_search,
312 .init = tcf_nat_init, 311 .init = tcf_nat_init,
313 .walk = tcf_generic_walker
314}; 312};
315 313
316MODULE_DESCRIPTION("Stateless NAT actions"); 314MODULE_DESCRIPTION("Stateless NAT actions");
diff --git a/net/sched/act_pedit.c b/net/sched/act_pedit.c
index 7ed78c9e505c..7aa2dcd989f8 100644
--- a/net/sched/act_pedit.c
+++ b/net/sched/act_pedit.c
@@ -84,10 +84,12 @@ static int tcf_pedit_init(struct net *net, struct nlattr *nla,
84 ret = ACT_P_CREATED; 84 ret = ACT_P_CREATED;
85 } else { 85 } else {
86 p = to_pedit(pc); 86 p = to_pedit(pc);
87 if (!ovr) { 87 tcf_hash_release(pc, bind, &pedit_hash_info);
88 tcf_hash_release(pc, bind, &pedit_hash_info); 88 if (bind)
89 return 0;
90 if (!ovr)
89 return -EEXIST; 91 return -EEXIST;
90 } 92
91 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) { 93 if (p->tcfp_nkeys && p->tcfp_nkeys != parm->nkeys) {
92 keys = kmalloc(ksize, GFP_KERNEL); 94 keys = kmalloc(ksize, GFP_KERNEL);
93 if (keys == NULL) 95 if (keys == NULL)
@@ -243,9 +245,7 @@ static struct tc_action_ops act_pedit_ops = {
243 .act = tcf_pedit, 245 .act = tcf_pedit,
244 .dump = tcf_pedit_dump, 246 .dump = tcf_pedit_dump,
245 .cleanup = tcf_pedit_cleanup, 247 .cleanup = tcf_pedit_cleanup,
246 .lookup = tcf_hash_search,
247 .init = tcf_pedit_init, 248 .init = tcf_pedit_init,
248 .walk = tcf_generic_walker
249}; 249};
250 250
251MODULE_AUTHOR("Jamal Hadi Salim(2002-4)"); 251MODULE_AUTHOR("Jamal Hadi Salim(2002-4)");
diff --git a/net/sched/act_police.c b/net/sched/act_police.c
index 189e3c5b3d09..ef246d87e68b 100644
--- a/net/sched/act_police.c
+++ b/net/sched/act_police.c
@@ -177,10 +177,12 @@ static int tcf_act_police_locate(struct net *net, struct nlattr *nla,
177 if (bind) { 177 if (bind) {
178 police->tcf_bindcnt += 1; 178 police->tcf_bindcnt += 1;
179 police->tcf_refcnt += 1; 179 police->tcf_refcnt += 1;
180 return 0;
180 } 181 }
181 if (ovr) 182 if (ovr)
182 goto override; 183 goto override;
183 return ret; 184 /* not replacing */
185 return -EEXIST;
184 } 186 }
185 } 187 }
186 188
@@ -231,14 +233,14 @@ override:
231 } 233 }
232 if (R_tab) { 234 if (R_tab) {
233 police->rate_present = true; 235 police->rate_present = true;
234 psched_ratecfg_precompute(&police->rate, &R_tab->rate); 236 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
235 qdisc_put_rtab(R_tab); 237 qdisc_put_rtab(R_tab);
236 } else { 238 } else {
237 police->rate_present = false; 239 police->rate_present = false;
238 } 240 }
239 if (P_tab) { 241 if (P_tab) {
240 police->peak_present = true; 242 police->peak_present = true;
241 psched_ratecfg_precompute(&police->peak, &P_tab->rate); 243 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
242 qdisc_put_rtab(P_tab); 244 qdisc_put_rtab(P_tab);
243 } else { 245 } else {
244 police->peak_present = false; 246 police->peak_present = false;
@@ -407,7 +409,6 @@ static struct tc_action_ops act_police_ops = {
407 .act = tcf_act_police, 409 .act = tcf_act_police,
408 .dump = tcf_act_police_dump, 410 .dump = tcf_act_police_dump,
409 .cleanup = tcf_act_police_cleanup, 411 .cleanup = tcf_act_police_cleanup,
410 .lookup = tcf_hash_search,
411 .init = tcf_act_police_locate, 412 .init = tcf_act_police_locate,
412 .walk = tcf_act_police_walker 413 .walk = tcf_act_police_walker
413}; 414};
diff --git a/net/sched/act_simple.c b/net/sched/act_simple.c
index 7725eb4ab756..f7b45ab85388 100644
--- a/net/sched/act_simple.c
+++ b/net/sched/act_simple.c
@@ -142,10 +142,13 @@ static int tcf_simp_init(struct net *net, struct nlattr *nla,
142 ret = ACT_P_CREATED; 142 ret = ACT_P_CREATED;
143 } else { 143 } else {
144 d = to_defact(pc); 144 d = to_defact(pc);
145 if (!ovr) { 145
146 tcf_simp_release(d, bind); 146 if (bind)
147 return 0;
148 tcf_simp_release(d, bind);
149 if (!ovr)
147 return -EEXIST; 150 return -EEXIST;
148 } 151
149 reset_policy(d, defdata, parm); 152 reset_policy(d, defdata, parm);
150 } 153 }
151 154
@@ -201,7 +204,6 @@ static struct tc_action_ops act_simp_ops = {
201 .dump = tcf_simp_dump, 204 .dump = tcf_simp_dump,
202 .cleanup = tcf_simp_cleanup, 205 .cleanup = tcf_simp_cleanup,
203 .init = tcf_simp_init, 206 .init = tcf_simp_init,
204 .walk = tcf_generic_walker,
205}; 207};
206 208
207MODULE_AUTHOR("Jamal Hadi Salim(2005)"); 209MODULE_AUTHOR("Jamal Hadi Salim(2005)");
diff --git a/net/sched/act_skbedit.c b/net/sched/act_skbedit.c
index cb4221171f93..8fe9d25c3008 100644
--- a/net/sched/act_skbedit.c
+++ b/net/sched/act_skbedit.c
@@ -120,10 +120,11 @@ static int tcf_skbedit_init(struct net *net, struct nlattr *nla,
120 ret = ACT_P_CREATED; 120 ret = ACT_P_CREATED;
121 } else { 121 } else {
122 d = to_skbedit(pc); 122 d = to_skbedit(pc);
123 if (!ovr) { 123 if (bind)
124 tcf_hash_release(pc, bind, &skbedit_hash_info); 124 return 0;
125 tcf_hash_release(pc, bind, &skbedit_hash_info);
126 if (!ovr)
125 return -EEXIST; 127 return -EEXIST;
126 }
127 } 128 }
128 129
129 spin_lock_bh(&d->tcf_lock); 130 spin_lock_bh(&d->tcf_lock);
@@ -203,7 +204,6 @@ static struct tc_action_ops act_skbedit_ops = {
203 .dump = tcf_skbedit_dump, 204 .dump = tcf_skbedit_dump,
204 .cleanup = tcf_skbedit_cleanup, 205 .cleanup = tcf_skbedit_cleanup,
205 .init = tcf_skbedit_init, 206 .init = tcf_skbedit_init,
206 .walk = tcf_generic_walker,
207}; 207};
208 208
209MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>"); 209MODULE_AUTHOR("Alexander Duyck, <alexander.h.duyck@intel.com>");
diff --git a/net/sched/cls_basic.c b/net/sched/cls_basic.c
index d76a35d0dc85..636d9131d870 100644
--- a/net/sched/cls_basic.c
+++ b/net/sched/cls_basic.c
@@ -137,7 +137,7 @@ static int basic_set_parms(struct net *net, struct tcf_proto *tp,
137 struct nlattr **tb, 137 struct nlattr **tb,
138 struct nlattr *est) 138 struct nlattr *est)
139{ 139{
140 int err = -EINVAL; 140 int err;
141 struct tcf_exts e; 141 struct tcf_exts e;
142 struct tcf_ematch_tree t; 142 struct tcf_ematch_tree t;
143 143
diff --git a/net/sched/cls_bpf.c b/net/sched/cls_bpf.c
new file mode 100644
index 000000000000..1002a8226281
--- /dev/null
+++ b/net/sched/cls_bpf.c
@@ -0,0 +1,385 @@
1/*
2 * Berkeley Packet Filter based traffic classifier
3 *
4 * Might be used to classify traffic through flexible, user-defined and
5 * possibly JIT-ed BPF filters for traffic control as an alternative to
6 * ematches.
7 *
8 * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/skbuff.h>
18#include <linux/filter.h>
19#include <net/rtnetlink.h>
20#include <net/pkt_cls.h>
21#include <net/sock.h>
22
23MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
25MODULE_DESCRIPTION("TC BPF based classifier");
26
27struct cls_bpf_head {
28 struct list_head plist;
29 u32 hgen;
30};
31
32struct cls_bpf_prog {
33 struct sk_filter *filter;
34 struct sock_filter *bpf_ops;
35 struct tcf_exts exts;
36 struct tcf_result res;
37 struct list_head link;
38 u32 handle;
39 u16 bpf_len;
40};
41
42static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
43 [TCA_BPF_CLASSID] = { .type = NLA_U32 },
44 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
45 [TCA_BPF_OPS] = { .type = NLA_BINARY,
46 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
47};
48
49static const struct tcf_ext_map bpf_ext_map = {
50 .action = TCA_BPF_ACT,
51 .police = TCA_BPF_POLICE,
52};
53
54static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
55 struct tcf_result *res)
56{
57 struct cls_bpf_head *head = tp->root;
58 struct cls_bpf_prog *prog;
59 int ret;
60
61 list_for_each_entry(prog, &head->plist, link) {
62 int filter_res = SK_RUN_FILTER(prog->filter, skb);
63
64 if (filter_res == 0)
65 continue;
66
67 *res = prog->res;
68 if (filter_res != -1)
69 res->classid = filter_res;
70
71 ret = tcf_exts_exec(skb, &prog->exts, res);
72 if (ret < 0)
73 continue;
74
75 return ret;
76 }
77
78 return -1;
79}
80
81static int cls_bpf_init(struct tcf_proto *tp)
82{
83 struct cls_bpf_head *head;
84
85 head = kzalloc(sizeof(*head), GFP_KERNEL);
86 if (head == NULL)
87 return -ENOBUFS;
88
89 INIT_LIST_HEAD(&head->plist);
90 tp->root = head;
91
92 return 0;
93}
94
95static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
96{
97 tcf_unbind_filter(tp, &prog->res);
98 tcf_exts_destroy(tp, &prog->exts);
99
100 sk_unattached_filter_destroy(prog->filter);
101
102 kfree(prog->bpf_ops);
103 kfree(prog);
104}
105
106static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
107{
108 struct cls_bpf_head *head = tp->root;
109 struct cls_bpf_prog *prog, *todel = (struct cls_bpf_prog *) arg;
110
111 list_for_each_entry(prog, &head->plist, link) {
112 if (prog == todel) {
113 tcf_tree_lock(tp);
114 list_del(&prog->link);
115 tcf_tree_unlock(tp);
116
117 cls_bpf_delete_prog(tp, prog);
118 return 0;
119 }
120 }
121
122 return -ENOENT;
123}
124
125static void cls_bpf_destroy(struct tcf_proto *tp)
126{
127 struct cls_bpf_head *head = tp->root;
128 struct cls_bpf_prog *prog, *tmp;
129
130 list_for_each_entry_safe(prog, tmp, &head->plist, link) {
131 list_del(&prog->link);
132 cls_bpf_delete_prog(tp, prog);
133 }
134
135 kfree(head);
136}
137
138static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
139{
140 struct cls_bpf_head *head = tp->root;
141 struct cls_bpf_prog *prog;
142 unsigned long ret = 0UL;
143
144 if (head == NULL)
145 return 0UL;
146
147 list_for_each_entry(prog, &head->plist, link) {
148 if (prog->handle == handle) {
149 ret = (unsigned long) prog;
150 break;
151 }
152 }
153
154 return ret;
155}
156
157static void cls_bpf_put(struct tcf_proto *tp, unsigned long f)
158{
159}
160
161static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
162 struct cls_bpf_prog *prog,
163 unsigned long base, struct nlattr **tb,
164 struct nlattr *est)
165{
166 struct sock_filter *bpf_ops, *bpf_old;
167 struct tcf_exts exts;
168 struct sock_fprog tmp;
169 struct sk_filter *fp, *fp_old;
170 u16 bpf_size, bpf_len;
171 u32 classid;
172 int ret;
173
174 if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
175 return -EINVAL;
176
177 ret = tcf_exts_validate(net, tp, tb, est, &exts, &bpf_ext_map);
178 if (ret < 0)
179 return ret;
180
181 classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
182 bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
183 if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
184 ret = -EINVAL;
185 goto errout;
186 }
187
188 bpf_size = bpf_len * sizeof(*bpf_ops);
189 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
190 if (bpf_ops == NULL) {
191 ret = -ENOMEM;
192 goto errout;
193 }
194
195 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
196
197 tmp.len = bpf_len;
198 tmp.filter = (struct sock_filter __user *) bpf_ops;
199
200 ret = sk_unattached_filter_create(&fp, &tmp);
201 if (ret)
202 goto errout_free;
203
204 tcf_tree_lock(tp);
205 fp_old = prog->filter;
206 bpf_old = prog->bpf_ops;
207
208 prog->bpf_len = bpf_len;
209 prog->bpf_ops = bpf_ops;
210 prog->filter = fp;
211 prog->res.classid = classid;
212 tcf_tree_unlock(tp);
213
214 tcf_bind_filter(tp, &prog->res, base);
215 tcf_exts_change(tp, &prog->exts, &exts);
216
217 if (fp_old)
218 sk_unattached_filter_destroy(fp_old);
219 if (bpf_old)
220 kfree(bpf_old);
221
222 return 0;
223
224errout_free:
225 kfree(bpf_ops);
226errout:
227 tcf_exts_destroy(tp, &exts);
228 return ret;
229}
230
231static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
232 struct cls_bpf_head *head)
233{
234 unsigned int i = 0x80000000;
235
236 do {
237 if (++head->hgen == 0x7FFFFFFF)
238 head->hgen = 1;
239 } while (--i > 0 && cls_bpf_get(tp, head->hgen));
240 if (i == 0)
241 pr_err("Insufficient number of handles\n");
242
243 return i;
244}
245
246static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
247 struct tcf_proto *tp, unsigned long base,
248 u32 handle, struct nlattr **tca,
249 unsigned long *arg)
250{
251 struct cls_bpf_head *head = tp->root;
252 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) *arg;
253 struct nlattr *tb[TCA_BPF_MAX + 1];
254 int ret;
255
256 if (tca[TCA_OPTIONS] == NULL)
257 return -EINVAL;
258
259 ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
260 if (ret < 0)
261 return ret;
262
263 if (prog != NULL) {
264 if (handle && prog->handle != handle)
265 return -EINVAL;
266 return cls_bpf_modify_existing(net, tp, prog, base, tb,
267 tca[TCA_RATE]);
268 }
269
270 prog = kzalloc(sizeof(*prog), GFP_KERNEL);
271 if (prog == NULL)
272 return -ENOBUFS;
273
274 if (handle == 0)
275 prog->handle = cls_bpf_grab_new_handle(tp, head);
276 else
277 prog->handle = handle;
278 if (prog->handle == 0) {
279 ret = -EINVAL;
280 goto errout;
281 }
282
283 ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE]);
284 if (ret < 0)
285 goto errout;
286
287 tcf_tree_lock(tp);
288 list_add(&prog->link, &head->plist);
289 tcf_tree_unlock(tp);
290
291 *arg = (unsigned long) prog;
292
293 return 0;
294errout:
295 if (*arg == 0UL && prog)
296 kfree(prog);
297
298 return ret;
299}
300
301static int cls_bpf_dump(struct tcf_proto *tp, unsigned long fh,
302 struct sk_buff *skb, struct tcmsg *tm)
303{
304 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
305 struct nlattr *nest, *nla;
306
307 if (prog == NULL)
308 return skb->len;
309
310 tm->tcm_handle = prog->handle;
311
312 nest = nla_nest_start(skb, TCA_OPTIONS);
313 if (nest == NULL)
314 goto nla_put_failure;
315
316 if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
317 goto nla_put_failure;
318 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
319 goto nla_put_failure;
320
321 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
322 sizeof(struct sock_filter));
323 if (nla == NULL)
324 goto nla_put_failure;
325
326 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
327
328 if (tcf_exts_dump(skb, &prog->exts, &bpf_ext_map) < 0)
329 goto nla_put_failure;
330
331 nla_nest_end(skb, nest);
332
333 if (tcf_exts_dump_stats(skb, &prog->exts, &bpf_ext_map) < 0)
334 goto nla_put_failure;
335
336 return skb->len;
337
338nla_put_failure:
339 nla_nest_cancel(skb, nest);
340 return -1;
341}
342
343static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
344{
345 struct cls_bpf_head *head = tp->root;
346 struct cls_bpf_prog *prog;
347
348 list_for_each_entry(prog, &head->plist, link) {
349 if (arg->count < arg->skip)
350 goto skip;
351 if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
352 arg->stop = 1;
353 break;
354 }
355skip:
356 arg->count++;
357 }
358}
359
360static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
361 .kind = "bpf",
362 .owner = THIS_MODULE,
363 .classify = cls_bpf_classify,
364 .init = cls_bpf_init,
365 .destroy = cls_bpf_destroy,
366 .get = cls_bpf_get,
367 .put = cls_bpf_put,
368 .change = cls_bpf_change,
369 .delete = cls_bpf_delete,
370 .walk = cls_bpf_walk,
371 .dump = cls_bpf_dump,
372};
373
374static int __init cls_bpf_init_mod(void)
375{
376 return register_tcf_proto_ops(&cls_bpf_ops);
377}
378
379static void __exit cls_bpf_exit_mod(void)
380{
381 unregister_tcf_proto_ops(&cls_bpf_ops);
382}
383
384module_init(cls_bpf_init_mod);
385module_exit(cls_bpf_exit_mod);
diff --git a/net/sched/cls_cgroup.c b/net/sched/cls_cgroup.c
index 867b4a3e3980..16006c92c3fd 100644
--- a/net/sched/cls_cgroup.c
+++ b/net/sched/cls_cgroup.c
@@ -72,11 +72,11 @@ static void cgrp_attach(struct cgroup_subsys_state *css,
72 struct cgroup_taskset *tset) 72 struct cgroup_taskset *tset)
73{ 73{
74 struct task_struct *p; 74 struct task_struct *p;
75 void *v; 75 struct cgroup_cls_state *cs = css_cls_state(css);
76 void *v = (void *)(unsigned long)cs->classid;
76 77
77 cgroup_taskset_for_each(p, css, tset) { 78 cgroup_taskset_for_each(p, css, tset) {
78 task_lock(p); 79 task_lock(p);
79 v = (void *)(unsigned long)task_cls_classid(p);
80 iterate_fd(p->files, 0, update_classid, v); 80 iterate_fd(p->files, 0, update_classid, v);
81 task_unlock(p); 81 task_unlock(p);
82 } 82 }
diff --git a/net/sched/em_ipset.c b/net/sched/em_ipset.c
index 938b7cbf5627..527aeb7a3ff0 100644
--- a/net/sched/em_ipset.c
+++ b/net/sched/em_ipset.c
@@ -24,11 +24,12 @@ static int em_ipset_change(struct tcf_proto *tp, void *data, int data_len,
24{ 24{
25 struct xt_set_info *set = data; 25 struct xt_set_info *set = data;
26 ip_set_id_t index; 26 ip_set_id_t index;
27 struct net *net = dev_net(qdisc_dev(tp->q));
27 28
28 if (data_len != sizeof(*set)) 29 if (data_len != sizeof(*set))
29 return -EINVAL; 30 return -EINVAL;
30 31
31 index = ip_set_nfnl_get_byindex(set->index); 32 index = ip_set_nfnl_get_byindex(net, set->index);
32 if (index == IPSET_INVALID_ID) 33 if (index == IPSET_INVALID_ID)
33 return -ENOENT; 34 return -ENOENT;
34 35
@@ -37,7 +38,7 @@ static int em_ipset_change(struct tcf_proto *tp, void *data, int data_len,
37 if (em->data) 38 if (em->data)
38 return 0; 39 return 0;
39 40
40 ip_set_nfnl_put(index); 41 ip_set_nfnl_put(net, index);
41 return -ENOMEM; 42 return -ENOMEM;
42} 43}
43 44
@@ -45,7 +46,7 @@ static void em_ipset_destroy(struct tcf_proto *p, struct tcf_ematch *em)
45{ 46{
46 const struct xt_set_info *set = (const void *) em->data; 47 const struct xt_set_info *set = (const void *) em->data;
47 if (set) { 48 if (set) {
48 ip_set_nfnl_put(set->index); 49 ip_set_nfnl_put(dev_net(qdisc_dev(p->q)), set->index);
49 kfree((void *) em->data); 50 kfree((void *) em->data);
50 } 51 }
51} 52}
diff --git a/net/sched/em_meta.c b/net/sched/em_meta.c
index 7c3de6ffa516..e5cef9567225 100644
--- a/net/sched/em_meta.c
+++ b/net/sched/em_meta.c
@@ -793,8 +793,10 @@ static int em_meta_change(struct tcf_proto *tp, void *data, int len,
793 goto errout; 793 goto errout;
794 794
795 meta = kzalloc(sizeof(*meta), GFP_KERNEL); 795 meta = kzalloc(sizeof(*meta), GFP_KERNEL);
796 if (meta == NULL) 796 if (meta == NULL) {
797 err = -ENOMEM;
797 goto errout; 798 goto errout;
799 }
798 800
799 memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left)); 801 memcpy(&meta->lvalue.hdr, &hdr->left, sizeof(hdr->left));
800 memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right)); 802 memcpy(&meta->rvalue.hdr, &hdr->right, sizeof(hdr->right));
diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
index 2adda7fa2d39..cd81505662b8 100644
--- a/net/sched/sch_api.c
+++ b/net/sched/sch_api.c
@@ -737,9 +737,11 @@ void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
737 const struct Qdisc_class_ops *cops; 737 const struct Qdisc_class_ops *cops;
738 unsigned long cl; 738 unsigned long cl;
739 u32 parentid; 739 u32 parentid;
740 int drops;
740 741
741 if (n == 0) 742 if (n == 0)
742 return; 743 return;
744 drops = max_t(int, n, 0);
743 while ((parentid = sch->parent)) { 745 while ((parentid = sch->parent)) {
744 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS)) 746 if (TC_H_MAJ(parentid) == TC_H_MAJ(TC_H_INGRESS))
745 return; 747 return;
@@ -756,6 +758,7 @@ void qdisc_tree_decrease_qlen(struct Qdisc *sch, unsigned int n)
756 cops->put(sch, cl); 758 cops->put(sch, cl);
757 } 759 }
758 sch->q.qlen -= n; 760 sch->q.qlen -= n;
761 sch->qstats.drops += drops;
759 } 762 }
760} 763}
761EXPORT_SYMBOL(qdisc_tree_decrease_qlen); 764EXPORT_SYMBOL(qdisc_tree_decrease_qlen);
diff --git a/net/sched/sch_fq.c b/net/sched/sch_fq.c
index a9dfdda9ed1d..95d843961907 100644
--- a/net/sched/sch_fq.c
+++ b/net/sched/sch_fq.c
@@ -88,7 +88,7 @@ struct fq_sched_data {
88 struct fq_flow internal; /* for non classified or high prio packets */ 88 struct fq_flow internal; /* for non classified or high prio packets */
89 u32 quantum; 89 u32 quantum;
90 u32 initial_quantum; 90 u32 initial_quantum;
91 u32 flow_default_rate;/* rate per flow : bytes per second */ 91 u32 flow_refill_delay;
92 u32 flow_max_rate; /* optional max rate per flow */ 92 u32 flow_max_rate; /* optional max rate per flow */
93 u32 flow_plimit; /* max packets per flow */ 93 u32 flow_plimit; /* max packets per flow */
94 struct rb_root *fq_root; 94 struct rb_root *fq_root;
@@ -115,6 +115,7 @@ static struct fq_flow detached, throttled;
115static void fq_flow_set_detached(struct fq_flow *f) 115static void fq_flow_set_detached(struct fq_flow *f)
116{ 116{
117 f->next = &detached; 117 f->next = &detached;
118 f->age = jiffies;
118} 119}
119 120
120static bool fq_flow_is_detached(const struct fq_flow *f) 121static bool fq_flow_is_detached(const struct fq_flow *f)
@@ -209,21 +210,15 @@ static void fq_gc(struct fq_sched_data *q,
209 } 210 }
210} 211}
211 212
212static const u8 prio2band[TC_PRIO_MAX + 1] = {
213 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
214};
215
216static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q) 213static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
217{ 214{
218 struct rb_node **p, *parent; 215 struct rb_node **p, *parent;
219 struct sock *sk = skb->sk; 216 struct sock *sk = skb->sk;
220 struct rb_root *root; 217 struct rb_root *root;
221 struct fq_flow *f; 218 struct fq_flow *f;
222 int band;
223 219
224 /* warning: no starvation prevention... */ 220 /* warning: no starvation prevention... */
225 band = prio2band[skb->priority & TC_PRIO_MAX]; 221 if (unlikely((skb->priority & TC_PRIO_MAX) == TC_PRIO_CONTROL))
226 if (unlikely(band == 0))
227 return &q->internal; 222 return &q->internal;
228 223
229 if (unlikely(!sk)) { 224 if (unlikely(!sk)) {
@@ -255,6 +250,7 @@ static struct fq_flow *fq_classify(struct sk_buff *skb, struct fq_sched_data *q)
255 f->socket_hash != sk->sk_hash)) { 250 f->socket_hash != sk->sk_hash)) {
256 f->credit = q->initial_quantum; 251 f->credit = q->initial_quantum;
257 f->socket_hash = sk->sk_hash; 252 f->socket_hash = sk->sk_hash;
253 f->time_next_packet = 0ULL;
258 } 254 }
259 return f; 255 return f;
260 } 256 }
@@ -372,17 +368,20 @@ static int fq_enqueue(struct sk_buff *skb, struct Qdisc *sch)
372 } 368 }
373 369
374 f->qlen++; 370 f->qlen++;
375 flow_queue_add(f, skb);
376 if (skb_is_retransmit(skb)) 371 if (skb_is_retransmit(skb))
377 q->stat_tcp_retrans++; 372 q->stat_tcp_retrans++;
378 sch->qstats.backlog += qdisc_pkt_len(skb); 373 sch->qstats.backlog += qdisc_pkt_len(skb);
379 if (fq_flow_is_detached(f)) { 374 if (fq_flow_is_detached(f)) {
380 fq_flow_add_tail(&q->new_flows, f); 375 fq_flow_add_tail(&q->new_flows, f);
381 if (q->quantum > f->credit) 376 if (time_after(jiffies, f->age + q->flow_refill_delay))
382 f->credit = q->quantum; 377 f->credit = max_t(u32, f->credit, q->quantum);
383 q->inactive_flows--; 378 q->inactive_flows--;
384 qdisc_unthrottled(sch); 379 qdisc_unthrottled(sch);
385 } 380 }
381
382 /* Note: this overwrites f->age */
383 flow_queue_add(f, skb);
384
386 if (unlikely(f == &q->internal)) { 385 if (unlikely(f == &q->internal)) {
387 q->stat_internal_packets++; 386 q->stat_internal_packets++;
388 qdisc_unthrottled(sch); 387 qdisc_unthrottled(sch);
@@ -460,7 +459,6 @@ begin:
460 fq_flow_add_tail(&q->old_flows, f); 459 fq_flow_add_tail(&q->old_flows, f);
461 } else { 460 } else {
462 fq_flow_set_detached(f); 461 fq_flow_set_detached(f);
463 f->age = jiffies;
464 q->inactive_flows++; 462 q->inactive_flows++;
465 } 463 }
466 goto begin; 464 goto begin;
@@ -614,6 +612,7 @@ static const struct nla_policy fq_policy[TCA_FQ_MAX + 1] = {
614 [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 }, 612 [TCA_FQ_FLOW_DEFAULT_RATE] = { .type = NLA_U32 },
615 [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 }, 613 [TCA_FQ_FLOW_MAX_RATE] = { .type = NLA_U32 },
616 [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 }, 614 [TCA_FQ_BUCKETS_LOG] = { .type = NLA_U32 },
615 [TCA_FQ_FLOW_REFILL_DELAY] = { .type = NLA_U32 },
617}; 616};
618 617
619static int fq_change(struct Qdisc *sch, struct nlattr *opt) 618static int fq_change(struct Qdisc *sch, struct nlattr *opt)
@@ -655,7 +654,8 @@ static int fq_change(struct Qdisc *sch, struct nlattr *opt)
655 q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]); 654 q->initial_quantum = nla_get_u32(tb[TCA_FQ_INITIAL_QUANTUM]);
656 655
657 if (tb[TCA_FQ_FLOW_DEFAULT_RATE]) 656 if (tb[TCA_FQ_FLOW_DEFAULT_RATE])
658 q->flow_default_rate = nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]); 657 pr_warn_ratelimited("sch_fq: defrate %u ignored.\n",
658 nla_get_u32(tb[TCA_FQ_FLOW_DEFAULT_RATE]));
659 659
660 if (tb[TCA_FQ_FLOW_MAX_RATE]) 660 if (tb[TCA_FQ_FLOW_MAX_RATE])
661 q->flow_max_rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]); 661 q->flow_max_rate = nla_get_u32(tb[TCA_FQ_FLOW_MAX_RATE]);
@@ -669,6 +669,12 @@ static int fq_change(struct Qdisc *sch, struct nlattr *opt)
669 err = -EINVAL; 669 err = -EINVAL;
670 } 670 }
671 671
672 if (tb[TCA_FQ_FLOW_REFILL_DELAY]) {
673 u32 usecs_delay = nla_get_u32(tb[TCA_FQ_FLOW_REFILL_DELAY]) ;
674
675 q->flow_refill_delay = usecs_to_jiffies(usecs_delay);
676 }
677
672 if (!err) 678 if (!err)
673 err = fq_resize(q, fq_log); 679 err = fq_resize(q, fq_log);
674 680
@@ -704,7 +710,7 @@ static int fq_init(struct Qdisc *sch, struct nlattr *opt)
704 q->flow_plimit = 100; 710 q->flow_plimit = 100;
705 q->quantum = 2 * psched_mtu(qdisc_dev(sch)); 711 q->quantum = 2 * psched_mtu(qdisc_dev(sch));
706 q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch)); 712 q->initial_quantum = 10 * psched_mtu(qdisc_dev(sch));
707 q->flow_default_rate = 0; 713 q->flow_refill_delay = msecs_to_jiffies(40);
708 q->flow_max_rate = ~0U; 714 q->flow_max_rate = ~0U;
709 q->rate_enable = 1; 715 q->rate_enable = 1;
710 q->new_flows.first = NULL; 716 q->new_flows.first = NULL;
@@ -731,15 +737,16 @@ static int fq_dump(struct Qdisc *sch, struct sk_buff *skb)
731 if (opts == NULL) 737 if (opts == NULL)
732 goto nla_put_failure; 738 goto nla_put_failure;
733 739
734 /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore, 740 /* TCA_FQ_FLOW_DEFAULT_RATE is not used anymore */
735 * do not bother giving its value 741
736 */
737 if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) || 742 if (nla_put_u32(skb, TCA_FQ_PLIMIT, sch->limit) ||
738 nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) || 743 nla_put_u32(skb, TCA_FQ_FLOW_PLIMIT, q->flow_plimit) ||
739 nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) || 744 nla_put_u32(skb, TCA_FQ_QUANTUM, q->quantum) ||
740 nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) || 745 nla_put_u32(skb, TCA_FQ_INITIAL_QUANTUM, q->initial_quantum) ||
741 nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) || 746 nla_put_u32(skb, TCA_FQ_RATE_ENABLE, q->rate_enable) ||
742 nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE, q->flow_max_rate) || 747 nla_put_u32(skb, TCA_FQ_FLOW_MAX_RATE, q->flow_max_rate) ||
748 nla_put_u32(skb, TCA_FQ_FLOW_REFILL_DELAY,
749 jiffies_to_usecs(q->flow_refill_delay)) ||
743 nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log)) 750 nla_put_u32(skb, TCA_FQ_BUCKETS_LOG, q->fq_trees_log))
744 goto nla_put_failure; 751 goto nla_put_failure;
745 752
diff --git a/net/sched/sch_generic.c b/net/sched/sch_generic.c
index a74e278654aa..7fc899a943a8 100644
--- a/net/sched/sch_generic.c
+++ b/net/sched/sch_generic.c
@@ -829,7 +829,7 @@ void dev_deactivate_many(struct list_head *head)
829 struct net_device *dev; 829 struct net_device *dev;
830 bool sync_needed = false; 830 bool sync_needed = false;
831 831
832 list_for_each_entry(dev, head, unreg_list) { 832 list_for_each_entry(dev, head, close_list) {
833 netdev_for_each_tx_queue(dev, dev_deactivate_queue, 833 netdev_for_each_tx_queue(dev, dev_deactivate_queue,
834 &noop_qdisc); 834 &noop_qdisc);
835 if (dev_ingress_queue(dev)) 835 if (dev_ingress_queue(dev))
@@ -848,7 +848,7 @@ void dev_deactivate_many(struct list_head *head)
848 synchronize_net(); 848 synchronize_net();
849 849
850 /* Wait for outstanding qdisc_run calls. */ 850 /* Wait for outstanding qdisc_run calls. */
851 list_for_each_entry(dev, head, unreg_list) 851 list_for_each_entry(dev, head, close_list)
852 while (some_qdisc_is_busy(dev)) 852 while (some_qdisc_is_busy(dev))
853 yield(); 853 yield();
854} 854}
@@ -857,7 +857,7 @@ void dev_deactivate(struct net_device *dev)
857{ 857{
858 LIST_HEAD(single); 858 LIST_HEAD(single);
859 859
860 list_add(&dev->unreg_list, &single); 860 list_add(&dev->close_list, &single);
861 dev_deactivate_many(&single); 861 dev_deactivate_many(&single);
862 list_del(&single); 862 list_del(&single);
863} 863}
@@ -910,11 +910,12 @@ void dev_shutdown(struct net_device *dev)
910} 910}
911 911
912void psched_ratecfg_precompute(struct psched_ratecfg *r, 912void psched_ratecfg_precompute(struct psched_ratecfg *r,
913 const struct tc_ratespec *conf) 913 const struct tc_ratespec *conf,
914 u64 rate64)
914{ 915{
915 memset(r, 0, sizeof(*r)); 916 memset(r, 0, sizeof(*r));
916 r->overhead = conf->overhead; 917 r->overhead = conf->overhead;
917 r->rate_bytes_ps = conf->rate; 918 r->rate_bytes_ps = max_t(u64, conf->rate, rate64);
918 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK); 919 r->linklayer = (conf->linklayer & TC_LINKLAYER_MASK);
919 r->mult = 1; 920 r->mult = 1;
920 /* 921 /*
diff --git a/net/sched/sch_htb.c b/net/sched/sch_htb.c
index 863846cc5513..717b2108f852 100644
--- a/net/sched/sch_htb.c
+++ b/net/sched/sch_htb.c
@@ -997,6 +997,8 @@ static const struct nla_policy htb_policy[TCA_HTB_MAX + 1] = {
997 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 997 [TCA_HTB_CTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
998 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 998 [TCA_HTB_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
999 [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 }, 999 [TCA_HTB_DIRECT_QLEN] = { .type = NLA_U32 },
1000 [TCA_HTB_RATE64] = { .type = NLA_U64 },
1001 [TCA_HTB_CEIL64] = { .type = NLA_U64 },
1000}; 1002};
1001 1003
1002static void htb_work_func(struct work_struct *work) 1004static void htb_work_func(struct work_struct *work)
@@ -1114,6 +1116,12 @@ static int htb_dump_class(struct Qdisc *sch, unsigned long arg,
1114 opt.level = cl->level; 1116 opt.level = cl->level;
1115 if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt)) 1117 if (nla_put(skb, TCA_HTB_PARMS, sizeof(opt), &opt))
1116 goto nla_put_failure; 1118 goto nla_put_failure;
1119 if ((cl->rate.rate_bytes_ps >= (1ULL << 32)) &&
1120 nla_put_u64(skb, TCA_HTB_RATE64, cl->rate.rate_bytes_ps))
1121 goto nla_put_failure;
1122 if ((cl->ceil.rate_bytes_ps >= (1ULL << 32)) &&
1123 nla_put_u64(skb, TCA_HTB_CEIL64, cl->ceil.rate_bytes_ps))
1124 goto nla_put_failure;
1117 1125
1118 nla_nest_end(skb, nest); 1126 nla_nest_end(skb, nest);
1119 spin_unlock_bh(root_lock); 1127 spin_unlock_bh(root_lock);
@@ -1332,6 +1340,7 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
1332 struct qdisc_rate_table *rtab = NULL, *ctab = NULL; 1340 struct qdisc_rate_table *rtab = NULL, *ctab = NULL;
1333 struct nlattr *tb[TCA_HTB_MAX + 1]; 1341 struct nlattr *tb[TCA_HTB_MAX + 1];
1334 struct tc_htb_opt *hopt; 1342 struct tc_htb_opt *hopt;
1343 u64 rate64, ceil64;
1335 1344
1336 /* extract all subattrs from opt attr */ 1345 /* extract all subattrs from opt attr */
1337 if (!opt) 1346 if (!opt)
@@ -1468,11 +1477,22 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
1468 sch_tree_lock(sch); 1477 sch_tree_lock(sch);
1469 } 1478 }
1470 1479
1480 rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
1481
1482 ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
1483
1484 psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
1485 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
1486
1471 /* it used to be a nasty bug here, we have to check that node 1487 /* it used to be a nasty bug here, we have to check that node
1472 * is really leaf before changing cl->un.leaf ! 1488 * is really leaf before changing cl->un.leaf !
1473 */ 1489 */
1474 if (!cl->level) { 1490 if (!cl->level) {
1475 cl->quantum = hopt->rate.rate / q->rate2quantum; 1491 u64 quantum = cl->rate.rate_bytes_ps;
1492
1493 do_div(quantum, q->rate2quantum);
1494 cl->quantum = min_t(u64, quantum, INT_MAX);
1495
1476 if (!hopt->quantum && cl->quantum < 1000) { 1496 if (!hopt->quantum && cl->quantum < 1000) {
1477 pr_warning( 1497 pr_warning(
1478 "HTB: quantum of class %X is small. Consider r2q change.\n", 1498 "HTB: quantum of class %X is small. Consider r2q change.\n",
@@ -1491,9 +1511,6 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
1491 cl->prio = TC_HTB_NUMPRIO - 1; 1511 cl->prio = TC_HTB_NUMPRIO - 1;
1492 } 1512 }
1493 1513
1494 psched_ratecfg_precompute(&cl->rate, &hopt->rate);
1495 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil);
1496
1497 cl->buffer = PSCHED_TICKS2NS(hopt->buffer); 1514 cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
1498 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer); 1515 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
1499 1516
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index b87e83d07478..bccd52b36e97 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -215,10 +215,10 @@ static bool loss_4state(struct netem_sched_data *q)
215 if (rnd < clg->a4) { 215 if (rnd < clg->a4) {
216 clg->state = 4; 216 clg->state = 4;
217 return true; 217 return true;
218 } else if (clg->a4 < rnd && rnd < clg->a1) { 218 } else if (clg->a4 < rnd && rnd < clg->a1 + clg->a4) {
219 clg->state = 3; 219 clg->state = 3;
220 return true; 220 return true;
221 } else if (clg->a1 < rnd) 221 } else if (clg->a1 + clg->a4 < rnd)
222 clg->state = 1; 222 clg->state = 1;
223 223
224 break; 224 break;
@@ -235,7 +235,6 @@ static bool loss_4state(struct netem_sched_data *q)
235 clg->state = 2; 235 clg->state = 2;
236 else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) { 236 else if (clg->a3 < rnd && rnd < clg->a2 + clg->a3) {
237 clg->state = 1; 237 clg->state = 1;
238 return true;
239 } else if (clg->a2 + clg->a3 < rnd) { 238 } else if (clg->a2 + clg->a3 < rnd) {
240 clg->state = 3; 239 clg->state = 3;
241 return true; 240 return true;
@@ -269,10 +268,11 @@ static bool loss_gilb_ell(struct netem_sched_data *q)
269 clg->state = 2; 268 clg->state = 2;
270 if (net_random() < clg->a4) 269 if (net_random() < clg->a4)
271 return true; 270 return true;
271 break;
272 case 2: 272 case 2:
273 if (net_random() < clg->a2) 273 if (net_random() < clg->a2)
274 clg->state = 1; 274 clg->state = 1;
275 if (clg->a3 > net_random()) 275 if (net_random() > clg->a3)
276 return true; 276 return true;
277 } 277 }
278 278
diff --git a/net/sched/sch_tbf.c b/net/sched/sch_tbf.c
index 1aaf1b6e51a2..887e672f9d7d 100644
--- a/net/sched/sch_tbf.c
+++ b/net/sched/sch_tbf.c
@@ -21,6 +21,7 @@
21#include <net/netlink.h> 21#include <net/netlink.h>
22#include <net/sch_generic.h> 22#include <net/sch_generic.h>
23#include <net/pkt_sched.h> 23#include <net/pkt_sched.h>
24#include <net/tcp.h>
24 25
25 26
26/* Simple Token Bucket Filter. 27/* Simple Token Bucket Filter.
@@ -117,6 +118,48 @@ struct tbf_sched_data {
117}; 118};
118 119
119 120
121/* Time to Length, convert time in ns to length in bytes
122 * to determinate how many bytes can be sent in given time.
123 */
124static u64 psched_ns_t2l(const struct psched_ratecfg *r,
125 u64 time_in_ns)
126{
127 /* The formula is :
128 * len = (time_in_ns * r->rate_bytes_ps) / NSEC_PER_SEC
129 */
130 u64 len = time_in_ns * r->rate_bytes_ps;
131
132 do_div(len, NSEC_PER_SEC);
133
134 if (unlikely(r->linklayer == TC_LINKLAYER_ATM)) {
135 do_div(len, 53);
136 len = len * 48;
137 }
138
139 if (len > r->overhead)
140 len -= r->overhead;
141 else
142 len = 0;
143
144 return len;
145}
146
147/*
148 * Return length of individual segments of a gso packet,
149 * including all headers (MAC, IP, TCP/UDP)
150 */
151static unsigned int skb_gso_seglen(const struct sk_buff *skb)
152{
153 unsigned int hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
154 const struct skb_shared_info *shinfo = skb_shinfo(skb);
155
156 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
157 hdr_len += tcp_hdrlen(skb);
158 else
159 hdr_len += sizeof(struct udphdr);
160 return hdr_len + shinfo->gso_size;
161}
162
120/* GSO packet is too big, segment it so that tbf can transmit 163/* GSO packet is too big, segment it so that tbf can transmit
121 * each segment in time 164 * each segment in time
122 */ 165 */
@@ -136,12 +179,8 @@ static int tbf_segment(struct sk_buff *skb, struct Qdisc *sch)
136 while (segs) { 179 while (segs) {
137 nskb = segs->next; 180 nskb = segs->next;
138 segs->next = NULL; 181 segs->next = NULL;
139 if (likely(segs->len <= q->max_size)) { 182 qdisc_skb_cb(segs)->pkt_len = segs->len;
140 qdisc_skb_cb(segs)->pkt_len = segs->len; 183 ret = qdisc_enqueue(segs, q->qdisc);
141 ret = qdisc_enqueue(segs, q->qdisc);
142 } else {
143 ret = qdisc_reshape_fail(skb, sch);
144 }
145 if (ret != NET_XMIT_SUCCESS) { 184 if (ret != NET_XMIT_SUCCESS) {
146 if (net_xmit_drop_count(ret)) 185 if (net_xmit_drop_count(ret))
147 sch->qstats.drops++; 186 sch->qstats.drops++;
@@ -163,7 +202,7 @@ static int tbf_enqueue(struct sk_buff *skb, struct Qdisc *sch)
163 int ret; 202 int ret;
164 203
165 if (qdisc_pkt_len(skb) > q->max_size) { 204 if (qdisc_pkt_len(skb) > q->max_size) {
166 if (skb_is_gso(skb)) 205 if (skb_is_gso(skb) && skb_gso_seglen(skb) <= q->max_size)
167 return tbf_segment(skb, sch); 206 return tbf_segment(skb, sch);
168 return qdisc_reshape_fail(skb, sch); 207 return qdisc_reshape_fail(skb, sch);
169 } 208 }
@@ -266,20 +305,24 @@ static const struct nla_policy tbf_policy[TCA_TBF_MAX + 1] = {
266 [TCA_TBF_PARMS] = { .len = sizeof(struct tc_tbf_qopt) }, 305 [TCA_TBF_PARMS] = { .len = sizeof(struct tc_tbf_qopt) },
267 [TCA_TBF_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 306 [TCA_TBF_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
268 [TCA_TBF_PTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 307 [TCA_TBF_PTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
308 [TCA_TBF_RATE64] = { .type = NLA_U64 },
309 [TCA_TBF_PRATE64] = { .type = NLA_U64 },
269}; 310};
270 311
271static int tbf_change(struct Qdisc *sch, struct nlattr *opt) 312static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
272{ 313{
273 int err; 314 int err;
274 struct tbf_sched_data *q = qdisc_priv(sch); 315 struct tbf_sched_data *q = qdisc_priv(sch);
275 struct nlattr *tb[TCA_TBF_PTAB + 1]; 316 struct nlattr *tb[TCA_TBF_MAX + 1];
276 struct tc_tbf_qopt *qopt; 317 struct tc_tbf_qopt *qopt;
277 struct qdisc_rate_table *rtab = NULL;
278 struct qdisc_rate_table *ptab = NULL;
279 struct Qdisc *child = NULL; 318 struct Qdisc *child = NULL;
280 int max_size, n; 319 struct psched_ratecfg rate;
320 struct psched_ratecfg peak;
321 u64 max_size;
322 s64 buffer, mtu;
323 u64 rate64 = 0, prate64 = 0;
281 324
282 err = nla_parse_nested(tb, TCA_TBF_PTAB, opt, tbf_policy); 325 err = nla_parse_nested(tb, TCA_TBF_MAX, opt, tbf_policy);
283 if (err < 0) 326 if (err < 0)
284 return err; 327 return err;
285 328
@@ -288,33 +331,13 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
288 goto done; 331 goto done;
289 332
290 qopt = nla_data(tb[TCA_TBF_PARMS]); 333 qopt = nla_data(tb[TCA_TBF_PARMS]);
291 rtab = qdisc_get_rtab(&qopt->rate, tb[TCA_TBF_RTAB]); 334 if (qopt->rate.linklayer == TC_LINKLAYER_UNAWARE)
292 if (rtab == NULL) 335 qdisc_put_rtab(qdisc_get_rtab(&qopt->rate,
293 goto done; 336 tb[TCA_TBF_RTAB]));
294 337
295 if (qopt->peakrate.rate) { 338 if (qopt->peakrate.linklayer == TC_LINKLAYER_UNAWARE)
296 if (qopt->peakrate.rate > qopt->rate.rate) 339 qdisc_put_rtab(qdisc_get_rtab(&qopt->peakrate,
297 ptab = qdisc_get_rtab(&qopt->peakrate, tb[TCA_TBF_PTAB]); 340 tb[TCA_TBF_PTAB]));
298 if (ptab == NULL)
299 goto done;
300 }
301
302 for (n = 0; n < 256; n++)
303 if (rtab->data[n] > qopt->buffer)
304 break;
305 max_size = (n << qopt->rate.cell_log) - 1;
306 if (ptab) {
307 int size;
308
309 for (n = 0; n < 256; n++)
310 if (ptab->data[n] > qopt->mtu)
311 break;
312 size = (n << qopt->peakrate.cell_log) - 1;
313 if (size < max_size)
314 max_size = size;
315 }
316 if (max_size < 0)
317 goto done;
318 341
319 if (q->qdisc != &noop_qdisc) { 342 if (q->qdisc != &noop_qdisc) {
320 err = fifo_set_limit(q->qdisc, qopt->limit); 343 err = fifo_set_limit(q->qdisc, qopt->limit);
@@ -328,6 +351,39 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
328 } 351 }
329 } 352 }
330 353
354 buffer = min_t(u64, PSCHED_TICKS2NS(qopt->buffer), ~0U);
355 mtu = min_t(u64, PSCHED_TICKS2NS(qopt->mtu), ~0U);
356
357 if (tb[TCA_TBF_RATE64])
358 rate64 = nla_get_u64(tb[TCA_TBF_RATE64]);
359 psched_ratecfg_precompute(&rate, &qopt->rate, rate64);
360
361 max_size = min_t(u64, psched_ns_t2l(&rate, buffer), ~0U);
362
363 if (qopt->peakrate.rate) {
364 if (tb[TCA_TBF_PRATE64])
365 prate64 = nla_get_u64(tb[TCA_TBF_PRATE64]);
366 psched_ratecfg_precompute(&peak, &qopt->peakrate, prate64);
367 if (peak.rate_bytes_ps <= rate.rate_bytes_ps) {
368 pr_warn_ratelimited("sch_tbf: peakrate %llu is lower than or equals to rate %llu !\n",
369 peak.rate_bytes_ps, rate.rate_bytes_ps);
370 err = -EINVAL;
371 goto done;
372 }
373
374 max_size = min_t(u64, max_size, psched_ns_t2l(&peak, mtu));
375 }
376
377 if (max_size < psched_mtu(qdisc_dev(sch)))
378 pr_warn_ratelimited("sch_tbf: burst %llu is lower than device %s mtu (%u) !\n",
379 max_size, qdisc_dev(sch)->name,
380 psched_mtu(qdisc_dev(sch)));
381
382 if (!max_size) {
383 err = -EINVAL;
384 goto done;
385 }
386
331 sch_tree_lock(sch); 387 sch_tree_lock(sch);
332 if (child) { 388 if (child) {
333 qdisc_tree_decrease_qlen(q->qdisc, q->qdisc->q.qlen); 389 qdisc_tree_decrease_qlen(q->qdisc, q->qdisc->q.qlen);
@@ -341,9 +397,9 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
341 q->tokens = q->buffer; 397 q->tokens = q->buffer;
342 q->ptokens = q->mtu; 398 q->ptokens = q->mtu;
343 399
344 psched_ratecfg_precompute(&q->rate, &rtab->rate); 400 memcpy(&q->rate, &rate, sizeof(struct psched_ratecfg));
345 if (ptab) { 401 if (qopt->peakrate.rate) {
346 psched_ratecfg_precompute(&q->peak, &ptab->rate); 402 memcpy(&q->peak, &peak, sizeof(struct psched_ratecfg));
347 q->peak_present = true; 403 q->peak_present = true;
348 } else { 404 } else {
349 q->peak_present = false; 405 q->peak_present = false;
@@ -352,10 +408,6 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
352 sch_tree_unlock(sch); 408 sch_tree_unlock(sch);
353 err = 0; 409 err = 0;
354done: 410done:
355 if (rtab)
356 qdisc_put_rtab(rtab);
357 if (ptab)
358 qdisc_put_rtab(ptab);
359 return err; 411 return err;
360} 412}
361 413
@@ -402,6 +454,13 @@ static int tbf_dump(struct Qdisc *sch, struct sk_buff *skb)
402 opt.buffer = PSCHED_NS2TICKS(q->buffer); 454 opt.buffer = PSCHED_NS2TICKS(q->buffer);
403 if (nla_put(skb, TCA_TBF_PARMS, sizeof(opt), &opt)) 455 if (nla_put(skb, TCA_TBF_PARMS, sizeof(opt), &opt))
404 goto nla_put_failure; 456 goto nla_put_failure;
457 if (q->rate.rate_bytes_ps >= (1ULL << 32) &&
458 nla_put_u64(skb, TCA_TBF_RATE64, q->rate.rate_bytes_ps))
459 goto nla_put_failure;
460 if (q->peak_present &&
461 q->peak.rate_bytes_ps >= (1ULL << 32) &&
462 nla_put_u64(skb, TCA_TBF_PRATE64, q->peak.rate_bytes_ps))
463 goto nla_put_failure;
405 464
406 nla_nest_end(skb, nest); 465 nla_nest_end(skb, nest);
407 return skb->len; 466 return skb->len;
diff --git a/net/sctp/associola.c b/net/sctp/associola.c
index cef509985192..31ed008c8e13 100644
--- a/net/sctp/associola.c
+++ b/net/sctp/associola.c
@@ -154,8 +154,7 @@ static struct sctp_association *sctp_association_init(struct sctp_association *a
154 154
155 asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0; 155 asoc->timeouts[SCTP_EVENT_TIMEOUT_HEARTBEAT] = 0;
156 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = asoc->sackdelay; 156 asoc->timeouts[SCTP_EVENT_TIMEOUT_SACK] = asoc->sackdelay;
157 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = 157 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE] = sp->autoclose * HZ;
158 min_t(unsigned long, sp->autoclose, net->sctp.max_autoclose) * HZ;
159 158
160 /* Initializes the timers */ 159 /* Initializes the timers */
161 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i) 160 for (i = SCTP_EVENT_TIMEOUT_NONE; i < SCTP_NUM_TIMEOUT_TYPES; ++i)
@@ -291,8 +290,6 @@ static struct sctp_association *sctp_association_init(struct sctp_association *a
291 asoc->peer.ipv6_address = 1; 290 asoc->peer.ipv6_address = 1;
292 INIT_LIST_HEAD(&asoc->asocs); 291 INIT_LIST_HEAD(&asoc->asocs);
293 292
294 asoc->autoclose = sp->autoclose;
295
296 asoc->default_stream = sp->default_stream; 293 asoc->default_stream = sp->default_stream;
297 asoc->default_ppid = sp->default_ppid; 294 asoc->default_ppid = sp->default_ppid;
298 asoc->default_flags = sp->default_flags; 295 asoc->default_flags = sp->default_flags;
@@ -602,7 +599,7 @@ void sctp_assoc_rm_peer(struct sctp_association *asoc,
602 599
603 /* Start a T3 timer here in case it wasn't running so 600 /* Start a T3 timer here in case it wasn't running so
604 * that these migrated packets have a chance to get 601 * that these migrated packets have a chance to get
605 * retrnasmitted. 602 * retransmitted.
606 */ 603 */
607 if (!timer_pending(&active->T3_rtx_timer)) 604 if (!timer_pending(&active->T3_rtx_timer))
608 if (!mod_timer(&active->T3_rtx_timer, 605 if (!mod_timer(&active->T3_rtx_timer,
@@ -665,7 +662,7 @@ struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
665 /* Set the path max_retrans. */ 662 /* Set the path max_retrans. */
666 peer->pathmaxrxt = asoc->pathmaxrxt; 663 peer->pathmaxrxt = asoc->pathmaxrxt;
667 664
668 /* And the partial failure retrnas threshold */ 665 /* And the partial failure retrans threshold */
669 peer->pf_retrans = asoc->pf_retrans; 666 peer->pf_retrans = asoc->pf_retrans;
670 667
671 /* Initialize the peer's SACK delay timeout based on the 668 /* Initialize the peer's SACK delay timeout based on the
@@ -907,8 +904,8 @@ void sctp_assoc_control_transport(struct sctp_association *asoc,
907 if (!first || t->last_time_heard > first->last_time_heard) { 904 if (!first || t->last_time_heard > first->last_time_heard) {
908 second = first; 905 second = first;
909 first = t; 906 first = t;
910 } 907 } else if (!second ||
911 if (!second || t->last_time_heard > second->last_time_heard) 908 t->last_time_heard > second->last_time_heard)
912 second = t; 909 second = t;
913 } 910 }
914 911
@@ -929,6 +926,8 @@ void sctp_assoc_control_transport(struct sctp_association *asoc,
929 first = asoc->peer.primary_path; 926 first = asoc->peer.primary_path;
930 } 927 }
931 928
929 if (!second)
930 second = first;
932 /* If we failed to find a usable transport, just camp on the 931 /* If we failed to find a usable transport, just camp on the
933 * primary, even if it is inactive. 932 * primary, even if it is inactive.
934 */ 933 */
diff --git a/net/sctp/auth.c b/net/sctp/auth.c
index 8c4fa5dec824..46b5977978a1 100644
--- a/net/sctp/auth.c
+++ b/net/sctp/auth.c
@@ -539,18 +539,14 @@ struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
539 for (i = 0; i < n_elt; i++) { 539 for (i = 0; i < n_elt; i++) {
540 id = ntohs(hmacs->hmac_ids[i]); 540 id = ntohs(hmacs->hmac_ids[i]);
541 541
542 /* Check the id is in the supported range */ 542 /* Check the id is in the supported range. And
543 if (id > SCTP_AUTH_HMAC_ID_MAX) { 543 * see if we support the id. Supported IDs have name and
544 id = 0; 544 * length fields set, so that we can allocate and use
545 continue;
546 }
547
548 /* See is we support the id. Supported IDs have name and
549 * length fields set, so that we can allocated and use
550 * them. We can safely just check for name, for without the 545 * them. We can safely just check for name, for without the
551 * name, we can't allocate the TFM. 546 * name, we can't allocate the TFM.
552 */ 547 */
553 if (!sctp_hmac_list[id].hmac_name) { 548 if (id > SCTP_AUTH_HMAC_ID_MAX ||
549 !sctp_hmac_list[id].hmac_name) {
554 id = 0; 550 id = 0;
555 continue; 551 continue;
556 } 552 }
diff --git a/net/sctp/chunk.c b/net/sctp/chunk.c
index 7bd5ed4a8657..f2044fcb9dd1 100644
--- a/net/sctp/chunk.c
+++ b/net/sctp/chunk.c
@@ -201,7 +201,7 @@ struct sctp_datamsg *sctp_datamsg_from_user(struct sctp_association *asoc,
201 201
202 max = asoc->frag_point; 202 max = asoc->frag_point;
203 /* If the the peer requested that we authenticate DATA chunks 203 /* If the the peer requested that we authenticate DATA chunks
204 * we need to accound for bundling of the AUTH chunks along with 204 * we need to account for bundling of the AUTH chunks along with
205 * DATA. 205 * DATA.
206 */ 206 */
207 if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) { 207 if (sctp_auth_send_cid(SCTP_CID_DATA, asoc)) {
diff --git a/net/sctp/ipv6.c b/net/sctp/ipv6.c
index e7b2d4fe2b6a..7567e6f1a920 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -279,7 +279,9 @@ static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
279 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port)); 279 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
280 rcu_read_lock(); 280 rcu_read_lock();
281 list_for_each_entry_rcu(laddr, &bp->address_list, list) { 281 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
282 if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC)) 282 if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
283 (laddr->state != SCTP_ADDR_SRC &&
284 !asoc->src_out_of_asoc_ok))
283 continue; 285 continue;
284 286
285 /* Do not compare against v4 addrs */ 287 /* Do not compare against v4 addrs */
@@ -426,20 +428,20 @@ static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
426{ 428{
427 addr->v6.sin6_family = AF_INET6; 429 addr->v6.sin6_family = AF_INET6;
428 addr->v6.sin6_port = 0; 430 addr->v6.sin6_port = 0;
429 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr; 431 addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
430} 432}
431 433
432/* Initialize sk->sk_rcv_saddr from sctp_addr. */ 434/* Initialize sk->sk_rcv_saddr from sctp_addr. */
433static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 435static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
434{ 436{
435 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) { 437 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
436 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0; 438 sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
437 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0; 439 sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
438 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff); 440 sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
439 inet6_sk(sk)->rcv_saddr.s6_addr32[3] = 441 sk->sk_v6_rcv_saddr.s6_addr32[3] =
440 addr->v4.sin_addr.s_addr; 442 addr->v4.sin_addr.s_addr;
441 } else { 443 } else {
442 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr; 444 sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
443 } 445 }
444} 446}
445 447
@@ -447,12 +449,12 @@ static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
447static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 449static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
448{ 450{
449 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) { 451 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
450 inet6_sk(sk)->daddr.s6_addr32[0] = 0; 452 sk->sk_v6_daddr.s6_addr32[0] = 0;
451 inet6_sk(sk)->daddr.s6_addr32[1] = 0; 453 sk->sk_v6_daddr.s6_addr32[1] = 0;
452 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff); 454 sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
453 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr; 455 sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
454 } else { 456 } else {
455 inet6_sk(sk)->daddr = addr->v6.sin6_addr; 457 sk->sk_v6_daddr = addr->v6.sin6_addr;
456 } 458 }
457} 459}
458 460
diff --git a/net/sctp/objcnt.c b/net/sctp/objcnt.c
index 5ea573b37648..647396baa56f 100644
--- a/net/sctp/objcnt.c
+++ b/net/sctp/objcnt.c
@@ -79,12 +79,13 @@ static sctp_dbg_objcnt_entry_t sctp_dbg_objcnt[] = {
79 */ 79 */
80static int sctp_objcnt_seq_show(struct seq_file *seq, void *v) 80static int sctp_objcnt_seq_show(struct seq_file *seq, void *v)
81{ 81{
82 int i, len; 82 int i;
83 83
84 i = (int)*(loff_t *)v; 84 i = (int)*(loff_t *)v;
85 seq_printf(seq, "%s: %d%n", sctp_dbg_objcnt[i].label, 85 seq_setwidth(seq, 127);
86 atomic_read(sctp_dbg_objcnt[i].counter), &len); 86 seq_printf(seq, "%s: %d", sctp_dbg_objcnt[i].label,
87 seq_printf(seq, "%*s\n", 127 - len, ""); 87 atomic_read(sctp_dbg_objcnt[i].counter));
88 seq_pad(seq, '\n');
88 return 0; 89 return 0;
89} 90}
90 91
diff --git a/net/sctp/output.c b/net/sctp/output.c
index 319137340d15..0fb140f8f088 100644
--- a/net/sctp/output.c
+++ b/net/sctp/output.c
@@ -390,7 +390,6 @@ int sctp_packet_transmit(struct sctp_packet *packet)
390 __u8 has_data = 0; 390 __u8 has_data = 0;
391 struct dst_entry *dst = tp->dst; 391 struct dst_entry *dst = tp->dst;
392 unsigned char *auth = NULL; /* pointer to auth in skb data */ 392 unsigned char *auth = NULL; /* pointer to auth in skb data */
393 __u32 cksum_buf_len = sizeof(struct sctphdr);
394 393
395 pr_debug("%s: packet:%p\n", __func__, packet); 394 pr_debug("%s: packet:%p\n", __func__, packet);
396 395
@@ -475,10 +474,11 @@ int sctp_packet_transmit(struct sctp_packet *packet)
475 * for a given destination transport address. 474 * for a given destination transport address.
476 */ 475 */
477 476
478 if (!tp->rto_pending) { 477 if (!chunk->resent && !tp->rto_pending) {
479 chunk->rtt_in_progress = 1; 478 chunk->rtt_in_progress = 1;
480 tp->rto_pending = 1; 479 tp->rto_pending = 1;
481 } 480 }
481
482 has_data = 1; 482 has_data = 1;
483 } 483 }
484 484
@@ -493,7 +493,6 @@ int sctp_packet_transmit(struct sctp_packet *packet)
493 if (chunk == packet->auth) 493 if (chunk == packet->auth)
494 auth = skb_tail_pointer(nskb); 494 auth = skb_tail_pointer(nskb);
495 495
496 cksum_buf_len += chunk->skb->len;
497 memcpy(skb_put(nskb, chunk->skb->len), 496 memcpy(skb_put(nskb, chunk->skb->len),
498 chunk->skb->data, chunk->skb->len); 497 chunk->skb->data, chunk->skb->len);
499 498
@@ -538,12 +537,7 @@ int sctp_packet_transmit(struct sctp_packet *packet)
538 if (!sctp_checksum_disable) { 537 if (!sctp_checksum_disable) {
539 if (!(dst->dev->features & NETIF_F_SCTP_CSUM) || 538 if (!(dst->dev->features & NETIF_F_SCTP_CSUM) ||
540 (dst_xfrm(dst) != NULL) || packet->ipfragok) { 539 (dst_xfrm(dst) != NULL) || packet->ipfragok) {
541 __u32 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len); 540 sh->checksum = sctp_compute_cksum(nskb, 0);
542
543 /* 3) Put the resultant value into the checksum field in the
544 * common header, and leave the rest of the bits unchanged.
545 */
546 sh->checksum = sctp_end_cksum(crc32);
547 } else { 541 } else {
548 /* no need to seed pseudo checksum for SCTP */ 542 /* no need to seed pseudo checksum for SCTP */
549 nskb->ip_summed = CHECKSUM_PARTIAL; 543 nskb->ip_summed = CHECKSUM_PARTIAL;
@@ -587,7 +581,8 @@ int sctp_packet_transmit(struct sctp_packet *packet)
587 unsigned long timeout; 581 unsigned long timeout;
588 582
589 /* Restart the AUTOCLOSE timer when sending data. */ 583 /* Restart the AUTOCLOSE timer when sending data. */
590 if (sctp_state(asoc, ESTABLISHED) && asoc->autoclose) { 584 if (sctp_state(asoc, ESTABLISHED) &&
585 asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
591 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; 586 timer = &asoc->timers[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
592 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]; 587 timeout = asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE];
593 588
diff --git a/net/sctp/outqueue.c b/net/sctp/outqueue.c
index 94df75877869..59268f6e2c36 100644
--- a/net/sctp/outqueue.c
+++ b/net/sctp/outqueue.c
@@ -208,8 +208,6 @@ void sctp_outq_init(struct sctp_association *asoc, struct sctp_outq *q)
208 INIT_LIST_HEAD(&q->retransmit); 208 INIT_LIST_HEAD(&q->retransmit);
209 INIT_LIST_HEAD(&q->sacked); 209 INIT_LIST_HEAD(&q->sacked);
210 INIT_LIST_HEAD(&q->abandoned); 210 INIT_LIST_HEAD(&q->abandoned);
211
212 q->empty = 1;
213} 211}
214 212
215/* Free the outqueue structure and any related pending chunks. 213/* Free the outqueue structure and any related pending chunks.
@@ -332,7 +330,6 @@ int sctp_outq_tail(struct sctp_outq *q, struct sctp_chunk *chunk)
332 SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS); 330 SCTP_INC_STATS(net, SCTP_MIB_OUTUNORDERCHUNKS);
333 else 331 else
334 SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS); 332 SCTP_INC_STATS(net, SCTP_MIB_OUTORDERCHUNKS);
335 q->empty = 0;
336 break; 333 break;
337 } 334 }
338 } else { 335 } else {
@@ -446,6 +443,8 @@ void sctp_retransmit_mark(struct sctp_outq *q,
446 transport->rto_pending = 0; 443 transport->rto_pending = 0;
447 } 444 }
448 445
446 chunk->resent = 1;
447
449 /* Move the chunk to the retransmit queue. The chunks 448 /* Move the chunk to the retransmit queue. The chunks
450 * on the retransmit queue are always kept in order. 449 * on the retransmit queue are always kept in order.
451 */ 450 */
@@ -652,7 +651,6 @@ redo:
652 if (chunk->fast_retransmit == SCTP_NEED_FRTX) 651 if (chunk->fast_retransmit == SCTP_NEED_FRTX)
653 chunk->fast_retransmit = SCTP_DONT_FRTX; 652 chunk->fast_retransmit = SCTP_DONT_FRTX;
654 653
655 q->empty = 0;
656 q->asoc->stats.rtxchunks++; 654 q->asoc->stats.rtxchunks++;
657 break; 655 break;
658 } 656 }
@@ -1063,8 +1061,6 @@ static int sctp_outq_flush(struct sctp_outq *q, int rtx_timeout)
1063 1061
1064 sctp_transport_reset_timers(transport); 1062 sctp_transport_reset_timers(transport);
1065 1063
1066 q->empty = 0;
1067
1068 /* Only let one DATA chunk get bundled with a 1064 /* Only let one DATA chunk get bundled with a
1069 * COOKIE-ECHO chunk. 1065 * COOKIE-ECHO chunk.
1070 */ 1066 */
@@ -1273,29 +1269,17 @@ int sctp_outq_sack(struct sctp_outq *q, struct sctp_chunk *chunk)
1273 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn, 1269 "advertised peer ack point:0x%x\n", __func__, asoc, ctsn,
1274 asoc->adv_peer_ack_point); 1270 asoc->adv_peer_ack_point);
1275 1271
1276 /* See if all chunks are acked. 1272 return sctp_outq_is_empty(q);
1277 * Make sure the empty queue handler will get run later.
1278 */
1279 q->empty = (list_empty(&q->out_chunk_list) &&
1280 list_empty(&q->retransmit));
1281 if (!q->empty)
1282 goto finish;
1283
1284 list_for_each_entry(transport, transport_list, transports) {
1285 q->empty = q->empty && list_empty(&transport->transmitted);
1286 if (!q->empty)
1287 goto finish;
1288 }
1289
1290 pr_debug("%s: sack queue is empty\n", __func__);
1291finish:
1292 return q->empty;
1293} 1273}
1294 1274
1295/* Is the outqueue empty? */ 1275/* Is the outqueue empty?
1276 * The queue is empty when we have not pending data, no in-flight data
1277 * and nothing pending retransmissions.
1278 */
1296int sctp_outq_is_empty(const struct sctp_outq *q) 1279int sctp_outq_is_empty(const struct sctp_outq *q)
1297{ 1280{
1298 return q->empty; 1281 return q->out_qlen == 0 && q->outstanding_bytes == 0 &&
1282 list_empty(&q->retransmit);
1299} 1283}
1300 1284
1301/******************************************************************** 1285/********************************************************************
@@ -1375,6 +1359,7 @@ static void sctp_check_transmitted(struct sctp_outq *q,
1375 * instance). 1359 * instance).
1376 */ 1360 */
1377 if (!tchunk->tsn_gap_acked && 1361 if (!tchunk->tsn_gap_acked &&
1362 !tchunk->resent &&
1378 tchunk->rtt_in_progress) { 1363 tchunk->rtt_in_progress) {
1379 tchunk->rtt_in_progress = 0; 1364 tchunk->rtt_in_progress = 0;
1380 rtt = jiffies - tchunk->sent_at; 1365 rtt = jiffies - tchunk->sent_at;
@@ -1391,7 +1376,8 @@ static void sctp_check_transmitted(struct sctp_outq *q,
1391 */ 1376 */
1392 if (!tchunk->tsn_gap_acked) { 1377 if (!tchunk->tsn_gap_acked) {
1393 tchunk->tsn_gap_acked = 1; 1378 tchunk->tsn_gap_acked = 1;
1394 *highest_new_tsn_in_sack = tsn; 1379 if (TSN_lt(*highest_new_tsn_in_sack, tsn))
1380 *highest_new_tsn_in_sack = tsn;
1395 bytes_acked += sctp_data_size(tchunk); 1381 bytes_acked += sctp_data_size(tchunk);
1396 if (!tchunk->transport) 1382 if (!tchunk->transport)
1397 migrate_bytes += sctp_data_size(tchunk); 1383 migrate_bytes += sctp_data_size(tchunk);
diff --git a/net/sctp/probe.c b/net/sctp/probe.c
index 53c452efb40b..5e68b94ee640 100644
--- a/net/sctp/probe.c
+++ b/net/sctp/probe.c
@@ -38,6 +38,7 @@
38#include <net/sctp/sctp.h> 38#include <net/sctp/sctp.h>
39#include <net/sctp/sm.h> 39#include <net/sctp/sm.h>
40 40
41MODULE_SOFTDEP("pre: sctp");
41MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>"); 42MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>");
42MODULE_DESCRIPTION("SCTP snooper"); 43MODULE_DESCRIPTION("SCTP snooper");
43MODULE_LICENSE("GPL"); 44MODULE_LICENSE("GPL");
@@ -182,6 +183,20 @@ static struct jprobe sctp_recv_probe = {
182 .entry = jsctp_sf_eat_sack, 183 .entry = jsctp_sf_eat_sack,
183}; 184};
184 185
186static __init int sctp_setup_jprobe(void)
187{
188 int ret = register_jprobe(&sctp_recv_probe);
189
190 if (ret) {
191 if (request_module("sctp"))
192 goto out;
193 ret = register_jprobe(&sctp_recv_probe);
194 }
195
196out:
197 return ret;
198}
199
185static __init int sctpprobe_init(void) 200static __init int sctpprobe_init(void)
186{ 201{
187 int ret = -ENOMEM; 202 int ret = -ENOMEM;
@@ -202,7 +217,7 @@ static __init int sctpprobe_init(void)
202 &sctpprobe_fops)) 217 &sctpprobe_fops))
203 goto free_kfifo; 218 goto free_kfifo;
204 219
205 ret = register_jprobe(&sctp_recv_probe); 220 ret = sctp_setup_jprobe();
206 if (ret) 221 if (ret)
207 goto remove_proc; 222 goto remove_proc;
208 223
diff --git a/net/sctp/sm_make_chunk.c b/net/sctp/sm_make_chunk.c
index d244a23ab8d3..fe690320b1e4 100644
--- a/net/sctp/sm_make_chunk.c
+++ b/net/sctp/sm_make_chunk.c
@@ -1297,6 +1297,13 @@ struct sctp_chunk *sctp_make_auth(const struct sctp_association *asoc)
1297 1297
1298/* Turn an skb into a chunk. 1298/* Turn an skb into a chunk.
1299 * FIXME: Eventually move the structure directly inside the skb->cb[]. 1299 * FIXME: Eventually move the structure directly inside the skb->cb[].
1300 *
1301 * sctpimpguide-05.txt Section 2.8.2
1302 * M1) Each time a new DATA chunk is transmitted
1303 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1304 * 'TSN.Missing.Report' count will be used to determine missing chunks
1305 * and when to fast retransmit.
1306 *
1300 */ 1307 */
1301struct sctp_chunk *sctp_chunkify(struct sk_buff *skb, 1308struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1302 const struct sctp_association *asoc, 1309 const struct sctp_association *asoc,
@@ -1314,29 +1321,9 @@ struct sctp_chunk *sctp_chunkify(struct sk_buff *skb,
1314 INIT_LIST_HEAD(&retval->list); 1321 INIT_LIST_HEAD(&retval->list);
1315 retval->skb = skb; 1322 retval->skb = skb;
1316 retval->asoc = (struct sctp_association *)asoc; 1323 retval->asoc = (struct sctp_association *)asoc;
1317 retval->has_tsn = 0;
1318 retval->has_ssn = 0;
1319 retval->rtt_in_progress = 0;
1320 retval->sent_at = 0;
1321 retval->singleton = 1; 1324 retval->singleton = 1;
1322 retval->end_of_packet = 0;
1323 retval->ecn_ce_done = 0;
1324 retval->pdiscard = 0;
1325
1326 /* sctpimpguide-05.txt Section 2.8.2
1327 * M1) Each time a new DATA chunk is transmitted
1328 * set the 'TSN.Missing.Report' count for that TSN to 0. The
1329 * 'TSN.Missing.Report' count will be used to determine missing chunks
1330 * and when to fast retransmit.
1331 */
1332 retval->tsn_missing_report = 0;
1333 retval->tsn_gap_acked = 0;
1334 retval->fast_retransmit = SCTP_CAN_FRTX;
1335 1325
1336 /* If this is a fragmented message, track all fragments 1326 retval->fast_retransmit = SCTP_CAN_FRTX;
1337 * of the message (for SEND_FAILED).
1338 */
1339 retval->msg = NULL;
1340 1327
1341 /* Polish the bead hole. */ 1328 /* Polish the bead hole. */
1342 INIT_LIST_HEAD(&retval->transmitted_list); 1329 INIT_LIST_HEAD(&retval->transmitted_list);
diff --git a/net/sctp/sm_sideeffect.c b/net/sctp/sm_sideeffect.c
index 666c66842799..1a6eef39ab2f 100644
--- a/net/sctp/sm_sideeffect.c
+++ b/net/sctp/sm_sideeffect.c
@@ -860,7 +860,6 @@ static void sctp_cmd_delete_tcb(sctp_cmd_seq_t *cmds,
860 (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK)) 860 (!asoc->temp) && (sk->sk_shutdown != SHUTDOWN_MASK))
861 return; 861 return;
862 862
863 BUG_ON(asoc->peer.primary_path == NULL);
864 sctp_unhash_established(asoc); 863 sctp_unhash_established(asoc);
865 sctp_association_free(asoc); 864 sctp_association_free(asoc);
866} 865}
diff --git a/net/sctp/sm_statefuns.c b/net/sctp/sm_statefuns.c
index dfe3f36ff2aa..a26065be7289 100644
--- a/net/sctp/sm_statefuns.c
+++ b/net/sctp/sm_statefuns.c
@@ -820,7 +820,7 @@ sctp_disposition_t sctp_sf_do_5_1D_ce(struct net *net,
820 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS); 820 SCTP_INC_STATS(net, SCTP_MIB_PASSIVEESTABS);
821 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL()); 821 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
822 822
823 if (new_asoc->autoclose) 823 if (new_asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START, 824 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
825 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 825 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
826 826
@@ -908,7 +908,7 @@ sctp_disposition_t sctp_sf_do_5_1E_ca(struct net *net,
908 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB); 908 SCTP_INC_STATS(net, SCTP_MIB_CURRESTAB);
909 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS); 909 SCTP_INC_STATS(net, SCTP_MIB_ACTIVEESTABS);
910 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL()); 910 sctp_add_cmd_sf(commands, SCTP_CMD_HB_TIMERS_START, SCTP_NULL());
911 if (asoc->autoclose) 911 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
912 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START, 912 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_START,
913 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 913 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
914 914
@@ -2970,7 +2970,7 @@ sctp_disposition_t sctp_sf_eat_data_6_2(struct net *net,
2970 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM) 2970 if (chunk->chunk_hdr->flags & SCTP_DATA_SACK_IMM)
2971 force = SCTP_FORCE(); 2971 force = SCTP_FORCE();
2972 2972
2973 if (asoc->autoclose) { 2973 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
2974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 2974 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
2975 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 2975 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
2976 } 2976 }
@@ -3878,7 +3878,7 @@ sctp_disposition_t sctp_sf_eat_fwd_tsn(struct net *net,
3878 SCTP_CHUNK(chunk)); 3878 SCTP_CHUNK(chunk));
3879 3879
3880 /* Count this as receiving DATA. */ 3880 /* Count this as receiving DATA. */
3881 if (asoc->autoclose) { 3881 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE]) {
3882 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 3882 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
3883 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 3883 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
3884 } 3884 }
@@ -5267,7 +5267,7 @@ sctp_disposition_t sctp_sf_do_9_2_start_shutdown(
5267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 5267 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD)); 5268 SCTP_TO(SCTP_EVENT_TIMEOUT_T5_SHUTDOWN_GUARD));
5269 5269
5270 if (asoc->autoclose) 5270 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP, 5271 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5272 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 5272 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5273 5273
@@ -5346,7 +5346,7 @@ sctp_disposition_t sctp_sf_do_9_2_shutdown_ack(
5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART, 5346 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_RESTART,
5347 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN)); 5347 SCTP_TO(SCTP_EVENT_TIMEOUT_T2_SHUTDOWN));
5348 5348
5349 if (asoc->autoclose) 5349 if (asoc->timeouts[SCTP_EVENT_TIMEOUT_AUTOCLOSE])
5350 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP, 5350 sctp_add_cmd_sf(commands, SCTP_CMD_TIMER_STOP,
5351 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE)); 5351 SCTP_TO(SCTP_EVENT_TIMEOUT_AUTOCLOSE));
5352 5352
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index 911b71b26b0e..42b709c95cf3 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -2196,6 +2196,7 @@ static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2196 unsigned int optlen) 2196 unsigned int optlen)
2197{ 2197{
2198 struct sctp_sock *sp = sctp_sk(sk); 2198 struct sctp_sock *sp = sctp_sk(sk);
2199 struct net *net = sock_net(sk);
2199 2200
2200 /* Applicable to UDP-style socket only */ 2201 /* Applicable to UDP-style socket only */
2201 if (sctp_style(sk, TCP)) 2202 if (sctp_style(sk, TCP))
@@ -2205,6 +2206,9 @@ static int sctp_setsockopt_autoclose(struct sock *sk, char __user *optval,
2205 if (copy_from_user(&sp->autoclose, optval, optlen)) 2206 if (copy_from_user(&sp->autoclose, optval, optlen))
2206 return -EFAULT; 2207 return -EFAULT;
2207 2208
2209 if (sp->autoclose > net->sctp.max_autoclose)
2210 sp->autoclose = net->sctp.max_autoclose;
2211
2208 return 0; 2212 return 0;
2209} 2213}
2210 2214
@@ -2811,6 +2815,8 @@ static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigne
2811{ 2815{
2812 struct sctp_rtoinfo rtoinfo; 2816 struct sctp_rtoinfo rtoinfo;
2813 struct sctp_association *asoc; 2817 struct sctp_association *asoc;
2818 unsigned long rto_min, rto_max;
2819 struct sctp_sock *sp = sctp_sk(sk);
2814 2820
2815 if (optlen != sizeof (struct sctp_rtoinfo)) 2821 if (optlen != sizeof (struct sctp_rtoinfo))
2816 return -EINVAL; 2822 return -EINVAL;
@@ -2824,26 +2830,36 @@ static int sctp_setsockopt_rtoinfo(struct sock *sk, char __user *optval, unsigne
2824 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP)) 2830 if (!asoc && rtoinfo.srto_assoc_id && sctp_style(sk, UDP))
2825 return -EINVAL; 2831 return -EINVAL;
2826 2832
2833 rto_max = rtoinfo.srto_max;
2834 rto_min = rtoinfo.srto_min;
2835
2836 if (rto_max)
2837 rto_max = asoc ? msecs_to_jiffies(rto_max) : rto_max;
2838 else
2839 rto_max = asoc ? asoc->rto_max : sp->rtoinfo.srto_max;
2840
2841 if (rto_min)
2842 rto_min = asoc ? msecs_to_jiffies(rto_min) : rto_min;
2843 else
2844 rto_min = asoc ? asoc->rto_min : sp->rtoinfo.srto_min;
2845
2846 if (rto_min > rto_max)
2847 return -EINVAL;
2848
2827 if (asoc) { 2849 if (asoc) {
2828 if (rtoinfo.srto_initial != 0) 2850 if (rtoinfo.srto_initial != 0)
2829 asoc->rto_initial = 2851 asoc->rto_initial =
2830 msecs_to_jiffies(rtoinfo.srto_initial); 2852 msecs_to_jiffies(rtoinfo.srto_initial);
2831 if (rtoinfo.srto_max != 0) 2853 asoc->rto_max = rto_max;
2832 asoc->rto_max = msecs_to_jiffies(rtoinfo.srto_max); 2854 asoc->rto_min = rto_min;
2833 if (rtoinfo.srto_min != 0)
2834 asoc->rto_min = msecs_to_jiffies(rtoinfo.srto_min);
2835 } else { 2855 } else {
2836 /* If there is no association or the association-id = 0 2856 /* If there is no association or the association-id = 0
2837 * set the values to the endpoint. 2857 * set the values to the endpoint.
2838 */ 2858 */
2839 struct sctp_sock *sp = sctp_sk(sk);
2840
2841 if (rtoinfo.srto_initial != 0) 2859 if (rtoinfo.srto_initial != 0)
2842 sp->rtoinfo.srto_initial = rtoinfo.srto_initial; 2860 sp->rtoinfo.srto_initial = rtoinfo.srto_initial;
2843 if (rtoinfo.srto_max != 0) 2861 sp->rtoinfo.srto_max = rto_max;
2844 sp->rtoinfo.srto_max = rtoinfo.srto_max; 2862 sp->rtoinfo.srto_min = rto_min;
2845 if (rtoinfo.srto_min != 0)
2846 sp->rtoinfo.srto_min = rtoinfo.srto_min;
2847 } 2863 }
2848 2864
2849 return 0; 2865 return 0;
@@ -5890,7 +5906,7 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5890 int low, high, remaining, index; 5906 int low, high, remaining, index;
5891 unsigned int rover; 5907 unsigned int rover;
5892 5908
5893 inet_get_local_port_range(&low, &high); 5909 inet_get_local_port_range(sock_net(sk), &low, &high);
5894 remaining = (high - low) + 1; 5910 remaining = (high - low) + 1;
5895 rover = net_random() % remaining + low; 5911 rover = net_random() % remaining + low;
5896 5912
diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
index 6b36561a1b3b..b0565afb61c7 100644
--- a/net/sctp/sysctl.c
+++ b/net/sctp/sysctl.c
@@ -56,11 +56,16 @@ extern long sysctl_sctp_mem[3];
56extern int sysctl_sctp_rmem[3]; 56extern int sysctl_sctp_rmem[3];
57extern int sysctl_sctp_wmem[3]; 57extern int sysctl_sctp_wmem[3];
58 58
59static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, 59static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write,
60 int write, 60 void __user *buffer, size_t *lenp,
61 loff_t *ppos);
62static int proc_sctp_do_rto_min(struct ctl_table *ctl, int write,
63 void __user *buffer, size_t *lenp,
64 loff_t *ppos);
65static int proc_sctp_do_rto_max(struct ctl_table *ctl, int write,
61 void __user *buffer, size_t *lenp, 66 void __user *buffer, size_t *lenp,
62
63 loff_t *ppos); 67 loff_t *ppos);
68
64static struct ctl_table sctp_table[] = { 69static struct ctl_table sctp_table[] = {
65 { 70 {
66 .procname = "sctp_mem", 71 .procname = "sctp_mem",
@@ -102,17 +107,17 @@ static struct ctl_table sctp_net_table[] = {
102 .data = &init_net.sctp.rto_min, 107 .data = &init_net.sctp.rto_min,
103 .maxlen = sizeof(unsigned int), 108 .maxlen = sizeof(unsigned int),
104 .mode = 0644, 109 .mode = 0644,
105 .proc_handler = proc_dointvec_minmax, 110 .proc_handler = proc_sctp_do_rto_min,
106 .extra1 = &one, 111 .extra1 = &one,
107 .extra2 = &timer_max 112 .extra2 = &init_net.sctp.rto_max
108 }, 113 },
109 { 114 {
110 .procname = "rto_max", 115 .procname = "rto_max",
111 .data = &init_net.sctp.rto_max, 116 .data = &init_net.sctp.rto_max,
112 .maxlen = sizeof(unsigned int), 117 .maxlen = sizeof(unsigned int),
113 .mode = 0644, 118 .mode = 0644,
114 .proc_handler = proc_dointvec_minmax, 119 .proc_handler = proc_sctp_do_rto_max,
115 .extra1 = &one, 120 .extra1 = &init_net.sctp.rto_min,
116 .extra2 = &timer_max 121 .extra2 = &timer_max
117 }, 122 },
118 { 123 {
@@ -294,8 +299,7 @@ static struct ctl_table sctp_net_table[] = {
294 { /* sentinel */ } 299 { /* sentinel */ }
295}; 300};
296 301
297static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, 302static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write,
298 int write,
299 void __user *buffer, size_t *lenp, 303 void __user *buffer, size_t *lenp,
300 loff_t *ppos) 304 loff_t *ppos)
301{ 305{
@@ -342,6 +346,60 @@ static int proc_sctp_do_hmac_alg(struct ctl_table *ctl,
342 return ret; 346 return ret;
343} 347}
344 348
349static int proc_sctp_do_rto_min(struct ctl_table *ctl, int write,
350 void __user *buffer, size_t *lenp,
351 loff_t *ppos)
352{
353 struct net *net = current->nsproxy->net_ns;
354 int new_value;
355 struct ctl_table tbl;
356 unsigned int min = *(unsigned int *) ctl->extra1;
357 unsigned int max = *(unsigned int *) ctl->extra2;
358 int ret;
359
360 memset(&tbl, 0, sizeof(struct ctl_table));
361 tbl.maxlen = sizeof(unsigned int);
362
363 if (write)
364 tbl.data = &new_value;
365 else
366 tbl.data = &net->sctp.rto_min;
367 ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
368 if (write) {
369 if (ret || new_value > max || new_value < min)
370 return -EINVAL;
371 net->sctp.rto_min = new_value;
372 }
373 return ret;
374}
375
376static int proc_sctp_do_rto_max(struct ctl_table *ctl, int write,
377 void __user *buffer, size_t *lenp,
378 loff_t *ppos)
379{
380 struct net *net = current->nsproxy->net_ns;
381 int new_value;
382 struct ctl_table tbl;
383 unsigned int min = *(unsigned int *) ctl->extra1;
384 unsigned int max = *(unsigned int *) ctl->extra2;
385 int ret;
386
387 memset(&tbl, 0, sizeof(struct ctl_table));
388 tbl.maxlen = sizeof(unsigned int);
389
390 if (write)
391 tbl.data = &new_value;
392 else
393 tbl.data = &net->sctp.rto_max;
394 ret = proc_dointvec(&tbl, write, buffer, lenp, ppos);
395 if (write) {
396 if (ret || new_value > max || new_value < min)
397 return -EINVAL;
398 net->sctp.rto_max = new_value;
399 }
400 return ret;
401}
402
345int sctp_sysctl_net_register(struct net *net) 403int sctp_sysctl_net_register(struct net *net)
346{ 404{
347 struct ctl_table *table; 405 struct ctl_table *table;
diff --git a/net/sctp/transport.c b/net/sctp/transport.c
index e332efb124cc..efc46ffed1fd 100644
--- a/net/sctp/transport.c
+++ b/net/sctp/transport.c
@@ -573,7 +573,7 @@ void sctp_transport_burst_limited(struct sctp_transport *t)
573 u32 old_cwnd = t->cwnd; 573 u32 old_cwnd = t->cwnd;
574 u32 max_burst_bytes; 574 u32 max_burst_bytes;
575 575
576 if (t->burst_limited) 576 if (t->burst_limited || asoc->max_burst == 0)
577 return; 577 return;
578 578
579 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu); 579 max_burst_bytes = t->flight_size + (asoc->max_burst * asoc->pathmtu);
diff --git a/net/socket.c b/net/socket.c
index c226aceee65b..e83c416708af 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -221,12 +221,13 @@ static int move_addr_to_user(struct sockaddr_storage *kaddr, int klen,
221 int err; 221 int err;
222 int len; 222 int len;
223 223
224 BUG_ON(klen > sizeof(struct sockaddr_storage));
224 err = get_user(len, ulen); 225 err = get_user(len, ulen);
225 if (err) 226 if (err)
226 return err; 227 return err;
227 if (len > klen) 228 if (len > klen)
228 len = klen; 229 len = klen;
229 if (len < 0 || len > sizeof(struct sockaddr_storage)) 230 if (len < 0)
230 return -EINVAL; 231 return -EINVAL;
231 if (len) { 232 if (len) {
232 if (audit_sockaddr(klen, kaddr)) 233 if (audit_sockaddr(klen, kaddr))
@@ -1840,8 +1841,10 @@ SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1840 msg.msg_iov = &iov; 1841 msg.msg_iov = &iov;
1841 iov.iov_len = size; 1842 iov.iov_len = size;
1842 iov.iov_base = ubuf; 1843 iov.iov_base = ubuf;
1843 msg.msg_name = (struct sockaddr *)&address; 1844 /* Save some cycles and don't copy the address if not needed */
1844 msg.msg_namelen = sizeof(address); 1845 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1846 /* We assume all kernel code knows the size of sockaddr_storage */
1847 msg.msg_namelen = 0;
1845 if (sock->file->f_flags & O_NONBLOCK) 1848 if (sock->file->f_flags & O_NONBLOCK)
1846 flags |= MSG_DONTWAIT; 1849 flags |= MSG_DONTWAIT;
1847 err = sock_recvmsg(sock, &msg, size, flags); 1850 err = sock_recvmsg(sock, &msg, size, flags);
@@ -1970,7 +1973,7 @@ static int copy_msghdr_from_user(struct msghdr *kmsg,
1970 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr))) 1973 if (copy_from_user(kmsg, umsg, sizeof(struct msghdr)))
1971 return -EFAULT; 1974 return -EFAULT;
1972 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage)) 1975 if (kmsg->msg_namelen > sizeof(struct sockaddr_storage))
1973 return -EINVAL; 1976 kmsg->msg_namelen = sizeof(struct sockaddr_storage);
1974 return 0; 1977 return 0;
1975} 1978}
1976 1979
@@ -2221,16 +2224,14 @@ static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2221 goto out; 2224 goto out;
2222 } 2225 }
2223 2226
2224 /* 2227 /* Save the user-mode address (verify_iovec will change the
2225 * Save the user-mode address (verify_iovec will change the 2228 * kernel msghdr to use the kernel address space)
2226 * kernel msghdr to use the kernel address space)
2227 */ 2229 */
2228
2229 uaddr = (__force void __user *)msg_sys->msg_name; 2230 uaddr = (__force void __user *)msg_sys->msg_name;
2230 uaddr_len = COMPAT_NAMELEN(msg); 2231 uaddr_len = COMPAT_NAMELEN(msg);
2231 if (MSG_CMSG_COMPAT & flags) { 2232 if (MSG_CMSG_COMPAT & flags)
2232 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE); 2233 err = verify_compat_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2233 } else 2234 else
2234 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE); 2235 err = verify_iovec(msg_sys, iov, &addr, VERIFY_WRITE);
2235 if (err < 0) 2236 if (err < 0)
2236 goto out_freeiov; 2237 goto out_freeiov;
@@ -2239,6 +2240,9 @@ static int ___sys_recvmsg(struct socket *sock, struct msghdr __user *msg,
2239 cmsg_ptr = (unsigned long)msg_sys->msg_control; 2240 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2240 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 2241 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
2241 2242
2243 /* We assume all kernel code knows the size of sockaddr_storage */
2244 msg_sys->msg_namelen = 0;
2245
2242 if (sock->file->f_flags & O_NONBLOCK) 2246 if (sock->file->f_flags & O_NONBLOCK)
2243 flags |= MSG_DONTWAIT; 2247 flags |= MSG_DONTWAIT;
2244 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys, 2248 err = (nosec ? sock_recvmsg_nosec : sock_recvmsg)(sock, msg_sys,
diff --git a/net/sunrpc/auth_gss/auth_gss.c b/net/sunrpc/auth_gss/auth_gss.c
index 084656671d6e..42fdfc634e56 100644
--- a/net/sunrpc/auth_gss/auth_gss.c
+++ b/net/sunrpc/auth_gss/auth_gss.c
@@ -420,41 +420,53 @@ static void gss_encode_v0_msg(struct gss_upcall_msg *gss_msg)
420 memcpy(gss_msg->databuf, &uid, sizeof(uid)); 420 memcpy(gss_msg->databuf, &uid, sizeof(uid));
421 gss_msg->msg.data = gss_msg->databuf; 421 gss_msg->msg.data = gss_msg->databuf;
422 gss_msg->msg.len = sizeof(uid); 422 gss_msg->msg.len = sizeof(uid);
423 BUG_ON(sizeof(uid) > UPCALL_BUF_LEN); 423
424 BUILD_BUG_ON(sizeof(uid) > sizeof(gss_msg->databuf));
424} 425}
425 426
426static void gss_encode_v1_msg(struct gss_upcall_msg *gss_msg, 427static int gss_encode_v1_msg(struct gss_upcall_msg *gss_msg,
427 const char *service_name, 428 const char *service_name,
428 const char *target_name) 429 const char *target_name)
429{ 430{
430 struct gss_api_mech *mech = gss_msg->auth->mech; 431 struct gss_api_mech *mech = gss_msg->auth->mech;
431 char *p = gss_msg->databuf; 432 char *p = gss_msg->databuf;
432 int len = 0; 433 size_t buflen = sizeof(gss_msg->databuf);
433 434 int len;
434 gss_msg->msg.len = sprintf(gss_msg->databuf, "mech=%s uid=%d ", 435
435 mech->gm_name, 436 len = scnprintf(p, buflen, "mech=%s uid=%d ", mech->gm_name,
436 from_kuid(&init_user_ns, gss_msg->uid)); 437 from_kuid(&init_user_ns, gss_msg->uid));
437 p += gss_msg->msg.len; 438 buflen -= len;
439 p += len;
440 gss_msg->msg.len = len;
438 if (target_name) { 441 if (target_name) {
439 len = sprintf(p, "target=%s ", target_name); 442 len = scnprintf(p, buflen, "target=%s ", target_name);
443 buflen -= len;
440 p += len; 444 p += len;
441 gss_msg->msg.len += len; 445 gss_msg->msg.len += len;
442 } 446 }
443 if (service_name != NULL) { 447 if (service_name != NULL) {
444 len = sprintf(p, "service=%s ", service_name); 448 len = scnprintf(p, buflen, "service=%s ", service_name);
449 buflen -= len;
445 p += len; 450 p += len;
446 gss_msg->msg.len += len; 451 gss_msg->msg.len += len;
447 } 452 }
448 if (mech->gm_upcall_enctypes) { 453 if (mech->gm_upcall_enctypes) {
449 len = sprintf(p, "enctypes=%s ", mech->gm_upcall_enctypes); 454 len = scnprintf(p, buflen, "enctypes=%s ",
455 mech->gm_upcall_enctypes);
456 buflen -= len;
450 p += len; 457 p += len;
451 gss_msg->msg.len += len; 458 gss_msg->msg.len += len;
452 } 459 }
453 len = sprintf(p, "\n"); 460 len = scnprintf(p, buflen, "\n");
461 if (len == 0)
462 goto out_overflow;
454 gss_msg->msg.len += len; 463 gss_msg->msg.len += len;
455 464
456 gss_msg->msg.data = gss_msg->databuf; 465 gss_msg->msg.data = gss_msg->databuf;
457 BUG_ON(gss_msg->msg.len > UPCALL_BUF_LEN); 466 return 0;
467out_overflow:
468 WARN_ON_ONCE(1);
469 return -ENOMEM;
458} 470}
459 471
460static struct gss_upcall_msg * 472static struct gss_upcall_msg *
@@ -463,15 +475,15 @@ gss_alloc_msg(struct gss_auth *gss_auth,
463{ 475{
464 struct gss_upcall_msg *gss_msg; 476 struct gss_upcall_msg *gss_msg;
465 int vers; 477 int vers;
478 int err = -ENOMEM;
466 479
467 gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS); 480 gss_msg = kzalloc(sizeof(*gss_msg), GFP_NOFS);
468 if (gss_msg == NULL) 481 if (gss_msg == NULL)
469 return ERR_PTR(-ENOMEM); 482 goto err;
470 vers = get_pipe_version(gss_auth->net); 483 vers = get_pipe_version(gss_auth->net);
471 if (vers < 0) { 484 err = vers;
472 kfree(gss_msg); 485 if (err < 0)
473 return ERR_PTR(vers); 486 goto err_free_msg;
474 }
475 gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe; 487 gss_msg->pipe = gss_auth->gss_pipe[vers]->pipe;
476 INIT_LIST_HEAD(&gss_msg->list); 488 INIT_LIST_HEAD(&gss_msg->list);
477 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq"); 489 rpc_init_wait_queue(&gss_msg->rpc_waitqueue, "RPCSEC_GSS upcall waitq");
@@ -482,10 +494,17 @@ gss_alloc_msg(struct gss_auth *gss_auth,
482 switch (vers) { 494 switch (vers) {
483 case 0: 495 case 0:
484 gss_encode_v0_msg(gss_msg); 496 gss_encode_v0_msg(gss_msg);
497 break;
485 default: 498 default:
486 gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name); 499 err = gss_encode_v1_msg(gss_msg, service_name, gss_auth->target_name);
500 if (err)
501 goto err_free_msg;
487 }; 502 };
488 return gss_msg; 503 return gss_msg;
504err_free_msg:
505 kfree(gss_msg);
506err:
507 return ERR_PTR(err);
489} 508}
490 509
491static struct gss_upcall_msg * 510static struct gss_upcall_msg *
@@ -1498,7 +1517,7 @@ out:
1498static int 1517static int
1499gss_refresh_null(struct rpc_task *task) 1518gss_refresh_null(struct rpc_task *task)
1500{ 1519{
1501 return -EACCES; 1520 return 0;
1502} 1521}
1503 1522
1504static __be32 * 1523static __be32 *
diff --git a/net/sunrpc/auth_gss/gss_krb5_unseal.c b/net/sunrpc/auth_gss/gss_krb5_unseal.c
index 6cd930f3678f..6c981ddc19f8 100644
--- a/net/sunrpc/auth_gss/gss_krb5_unseal.c
+++ b/net/sunrpc/auth_gss/gss_krb5_unseal.c
@@ -150,7 +150,6 @@ gss_verify_mic_v2(struct krb5_ctx *ctx,
150 struct xdr_netobj cksumobj = {.len = sizeof(cksumdata), 150 struct xdr_netobj cksumobj = {.len = sizeof(cksumdata),
151 .data = cksumdata}; 151 .data = cksumdata};
152 s32 now; 152 s32 now;
153 u64 seqnum;
154 u8 *ptr = read_token->data; 153 u8 *ptr = read_token->data;
155 u8 *cksumkey; 154 u8 *cksumkey;
156 u8 flags; 155 u8 flags;
@@ -197,9 +196,10 @@ gss_verify_mic_v2(struct krb5_ctx *ctx,
197 if (now > ctx->endtime) 196 if (now > ctx->endtime)
198 return GSS_S_CONTEXT_EXPIRED; 197 return GSS_S_CONTEXT_EXPIRED;
199 198
200 /* do sequencing checks */ 199 /*
201 200 * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
202 seqnum = be64_to_cpup((__be64 *)ptr + 8); 201 * doesn't want it checked; see page 6 of rfc 2203.
202 */
203 203
204 return GSS_S_COMPLETE; 204 return GSS_S_COMPLETE;
205} 205}
diff --git a/net/sunrpc/auth_gss/gss_krb5_wrap.c b/net/sunrpc/auth_gss/gss_krb5_wrap.c
index 1da52d1406fc..42560e55d978 100644
--- a/net/sunrpc/auth_gss/gss_krb5_wrap.c
+++ b/net/sunrpc/auth_gss/gss_krb5_wrap.c
@@ -489,7 +489,6 @@ static u32
489gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf) 489gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
490{ 490{
491 s32 now; 491 s32 now;
492 u64 seqnum;
493 u8 *ptr; 492 u8 *ptr;
494 u8 flags = 0x00; 493 u8 flags = 0x00;
495 u16 ec, rrc; 494 u16 ec, rrc;
@@ -525,7 +524,10 @@ gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
525 ec = be16_to_cpup((__be16 *)(ptr + 4)); 524 ec = be16_to_cpup((__be16 *)(ptr + 4));
526 rrc = be16_to_cpup((__be16 *)(ptr + 6)); 525 rrc = be16_to_cpup((__be16 *)(ptr + 6));
527 526
528 seqnum = be64_to_cpup((__be64 *)(ptr + 8)); 527 /*
528 * NOTE: the sequence number at ptr + 8 is skipped, rpcsec_gss
529 * doesn't want it checked; see page 6 of rfc 2203.
530 */
529 531
530 if (rrc != 0) 532 if (rrc != 0)
531 rotate_left(offset + 16, buf, rrc); 533 rotate_left(offset + 16, buf, rrc);
@@ -574,8 +576,8 @@ gss_unwrap_kerberos_v2(struct krb5_ctx *kctx, int offset, struct xdr_buf *buf)
574 buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip; 576 buf->head[0].iov_len -= GSS_KRB5_TOK_HDR_LEN + headskip;
575 buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip; 577 buf->len -= GSS_KRB5_TOK_HDR_LEN + headskip;
576 578
577 /* Trim off the checksum blob */ 579 /* Trim off the trailing "extra count" and checksum blob */
578 xdr_buf_trim(buf, GSS_KRB5_TOK_HDR_LEN + tailskip); 580 xdr_buf_trim(buf, ec + GSS_KRB5_TOK_HDR_LEN + tailskip);
579 return GSS_S_COMPLETE; 581 return GSS_S_COMPLETE;
580} 582}
581 583
diff --git a/net/sunrpc/auth_gss/gss_rpc_upcall.c b/net/sunrpc/auth_gss/gss_rpc_upcall.c
index f1eb0d16666c..458f85e9b0ba 100644
--- a/net/sunrpc/auth_gss/gss_rpc_upcall.c
+++ b/net/sunrpc/auth_gss/gss_rpc_upcall.c
@@ -298,7 +298,8 @@ int gssp_accept_sec_context_upcall(struct net *net,
298 if (res.context_handle) { 298 if (res.context_handle) {
299 data->out_handle = rctxh.exported_context_token; 299 data->out_handle = rctxh.exported_context_token;
300 data->mech_oid.len = rctxh.mech.len; 300 data->mech_oid.len = rctxh.mech.len;
301 memcpy(data->mech_oid.data, rctxh.mech.data, 301 if (rctxh.mech.data)
302 memcpy(data->mech_oid.data, rctxh.mech.data,
302 data->mech_oid.len); 303 data->mech_oid.len);
303 client_name = rctxh.src_name.display_name; 304 client_name = rctxh.src_name.display_name;
304 } 305 }
diff --git a/net/sunrpc/auth_gss/gss_rpc_xdr.c b/net/sunrpc/auth_gss/gss_rpc_xdr.c
index f0f78c5f1c7d..1ec19f6f0c2b 100644
--- a/net/sunrpc/auth_gss/gss_rpc_xdr.c
+++ b/net/sunrpc/auth_gss/gss_rpc_xdr.c
@@ -559,6 +559,8 @@ static int gssx_enc_cred(struct xdr_stream *xdr,
559 559
560 /* cred->elements */ 560 /* cred->elements */
561 err = dummy_enc_credel_array(xdr, &cred->elements); 561 err = dummy_enc_credel_array(xdr, &cred->elements);
562 if (err)
563 return err;
562 564
563 /* cred->cred_handle_reference */ 565 /* cred->cred_handle_reference */
564 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference); 566 err = gssx_enc_buffer(xdr, &cred->cred_handle_reference);
@@ -740,22 +742,20 @@ void gssx_enc_accept_sec_context(struct rpc_rqst *req,
740 goto done; 742 goto done;
741 743
742 /* arg->context_handle */ 744 /* arg->context_handle */
743 if (arg->context_handle) { 745 if (arg->context_handle)
744 err = gssx_enc_ctx(xdr, arg->context_handle); 746 err = gssx_enc_ctx(xdr, arg->context_handle);
745 if (err) 747 else
746 goto done;
747 } else {
748 err = gssx_enc_bool(xdr, 0); 748 err = gssx_enc_bool(xdr, 0);
749 } 749 if (err)
750 goto done;
750 751
751 /* arg->cred_handle */ 752 /* arg->cred_handle */
752 if (arg->cred_handle) { 753 if (arg->cred_handle)
753 err = gssx_enc_cred(xdr, arg->cred_handle); 754 err = gssx_enc_cred(xdr, arg->cred_handle);
754 if (err) 755 else
755 goto done;
756 } else {
757 err = gssx_enc_bool(xdr, 0); 756 err = gssx_enc_bool(xdr, 0);
758 } 757 if (err)
758 goto done;
759 759
760 /* arg->input_token */ 760 /* arg->input_token */
761 err = gssx_enc_in_token(xdr, &arg->input_token); 761 err = gssx_enc_in_token(xdr, &arg->input_token);
@@ -763,13 +763,12 @@ void gssx_enc_accept_sec_context(struct rpc_rqst *req,
763 goto done; 763 goto done;
764 764
765 /* arg->input_cb */ 765 /* arg->input_cb */
766 if (arg->input_cb) { 766 if (arg->input_cb)
767 err = gssx_enc_cb(xdr, arg->input_cb); 767 err = gssx_enc_cb(xdr, arg->input_cb);
768 if (err) 768 else
769 goto done;
770 } else {
771 err = gssx_enc_bool(xdr, 0); 769 err = gssx_enc_bool(xdr, 0);
772 } 770 if (err)
771 goto done;
773 772
774 err = gssx_enc_bool(xdr, arg->ret_deleg_cred); 773 err = gssx_enc_bool(xdr, arg->ret_deleg_cred);
775 if (err) 774 if (err)
diff --git a/net/sunrpc/auth_gss/svcauth_gss.c b/net/sunrpc/auth_gss/svcauth_gss.c
index 09fb638bcaa4..008cdade5aae 100644
--- a/net/sunrpc/auth_gss/svcauth_gss.c
+++ b/net/sunrpc/auth_gss/svcauth_gss.c
@@ -1167,8 +1167,8 @@ static int gss_proxy_save_rsc(struct cache_detail *cd,
1167 if (!ud->found_creds) { 1167 if (!ud->found_creds) {
1168 /* userspace seem buggy, we should always get at least a 1168 /* userspace seem buggy, we should always get at least a
1169 * mapping to nobody */ 1169 * mapping to nobody */
1170 dprintk("RPC: No creds found, marking Negative!\n"); 1170 dprintk("RPC: No creds found!\n");
1171 set_bit(CACHE_NEGATIVE, &rsci.h.flags); 1171 goto out;
1172 } else { 1172 } else {
1173 1173
1174 /* steal creds */ 1174 /* steal creds */
diff --git a/net/sunrpc/clnt.c b/net/sunrpc/clnt.c
index 77479606a971..f09b7db2c492 100644
--- a/net/sunrpc/clnt.c
+++ b/net/sunrpc/clnt.c
@@ -25,12 +25,12 @@
25#include <linux/namei.h> 25#include <linux/namei.h>
26#include <linux/mount.h> 26#include <linux/mount.h>
27#include <linux/slab.h> 27#include <linux/slab.h>
28#include <linux/rcupdate.h>
28#include <linux/utsname.h> 29#include <linux/utsname.h>
29#include <linux/workqueue.h> 30#include <linux/workqueue.h>
30#include <linux/in.h> 31#include <linux/in.h>
31#include <linux/in6.h> 32#include <linux/in6.h>
32#include <linux/un.h> 33#include <linux/un.h>
33#include <linux/rcupdate.h>
34 34
35#include <linux/sunrpc/clnt.h> 35#include <linux/sunrpc/clnt.h>
36#include <linux/sunrpc/addr.h> 36#include <linux/sunrpc/addr.h>
@@ -264,6 +264,26 @@ void rpc_clients_notifier_unregister(void)
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block); 264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265} 265}
266 266
267static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
268 struct rpc_xprt *xprt,
269 const struct rpc_timeout *timeout)
270{
271 struct rpc_xprt *old;
272
273 spin_lock(&clnt->cl_lock);
274 old = rcu_dereference_protected(clnt->cl_xprt,
275 lockdep_is_held(&clnt->cl_lock));
276
277 if (!xprt_bound(xprt))
278 clnt->cl_autobind = 1;
279
280 clnt->cl_timeout = timeout;
281 rcu_assign_pointer(clnt->cl_xprt, xprt);
282 spin_unlock(&clnt->cl_lock);
283
284 return old;
285}
286
267static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename) 287static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
268{ 288{
269 clnt->cl_nodelen = strlen(nodename); 289 clnt->cl_nodelen = strlen(nodename);
@@ -272,12 +292,13 @@ static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
272 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen); 292 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
273} 293}
274 294
275static int rpc_client_register(const struct rpc_create_args *args, 295static int rpc_client_register(struct rpc_clnt *clnt,
276 struct rpc_clnt *clnt) 296 rpc_authflavor_t pseudoflavor,
297 const char *client_name)
277{ 298{
278 struct rpc_auth_create_args auth_args = { 299 struct rpc_auth_create_args auth_args = {
279 .pseudoflavor = args->authflavor, 300 .pseudoflavor = pseudoflavor,
280 .target_name = args->client_name, 301 .target_name = client_name,
281 }; 302 };
282 struct rpc_auth *auth; 303 struct rpc_auth *auth;
283 struct net *net = rpc_net_ns(clnt); 304 struct net *net = rpc_net_ns(clnt);
@@ -298,7 +319,7 @@ static int rpc_client_register(const struct rpc_create_args *args,
298 auth = rpcauth_create(&auth_args, clnt); 319 auth = rpcauth_create(&auth_args, clnt);
299 if (IS_ERR(auth)) { 320 if (IS_ERR(auth)) {
300 dprintk("RPC: Couldn't create auth handle (flavor %u)\n", 321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
301 args->authflavor); 322 pseudoflavor);
302 err = PTR_ERR(auth); 323 err = PTR_ERR(auth);
303 goto err_auth; 324 goto err_auth;
304 } 325 }
@@ -337,7 +358,8 @@ static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
337{ 358{
338 const struct rpc_program *program = args->program; 359 const struct rpc_program *program = args->program;
339 const struct rpc_version *version; 360 const struct rpc_version *version;
340 struct rpc_clnt *clnt = NULL; 361 struct rpc_clnt *clnt = NULL;
362 const struct rpc_timeout *timeout;
341 int err; 363 int err;
342 364
343 /* sanity check the name before trying to print it */ 365 /* sanity check the name before trying to print it */
@@ -365,7 +387,6 @@ static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
365 if (err) 387 if (err)
366 goto out_no_clid; 388 goto out_no_clid;
367 389
368 rcu_assign_pointer(clnt->cl_xprt, xprt);
369 clnt->cl_procinfo = version->procs; 390 clnt->cl_procinfo = version->procs;
370 clnt->cl_maxproc = version->nrprocs; 391 clnt->cl_maxproc = version->nrprocs;
371 clnt->cl_prog = args->prognumber ? : program->number; 392 clnt->cl_prog = args->prognumber ? : program->number;
@@ -380,16 +401,15 @@ static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
380 INIT_LIST_HEAD(&clnt->cl_tasks); 401 INIT_LIST_HEAD(&clnt->cl_tasks);
381 spin_lock_init(&clnt->cl_lock); 402 spin_lock_init(&clnt->cl_lock);
382 403
383 if (!xprt_bound(xprt)) 404 timeout = xprt->timeout;
384 clnt->cl_autobind = 1;
385
386 clnt->cl_timeout = xprt->timeout;
387 if (args->timeout != NULL) { 405 if (args->timeout != NULL) {
388 memcpy(&clnt->cl_timeout_default, args->timeout, 406 memcpy(&clnt->cl_timeout_default, args->timeout,
389 sizeof(clnt->cl_timeout_default)); 407 sizeof(clnt->cl_timeout_default));
390 clnt->cl_timeout = &clnt->cl_timeout_default; 408 timeout = &clnt->cl_timeout_default;
391 } 409 }
392 410
411 rpc_clnt_set_transport(clnt, xprt, timeout);
412
393 clnt->cl_rtt = &clnt->cl_rtt_default; 413 clnt->cl_rtt = &clnt->cl_rtt_default;
394 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval); 414 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
395 415
@@ -398,7 +418,7 @@ static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
398 /* save the nodename */ 418 /* save the nodename */
399 rpc_clnt_set_nodename(clnt, utsname()->nodename); 419 rpc_clnt_set_nodename(clnt, utsname()->nodename);
400 420
401 err = rpc_client_register(args, clnt); 421 err = rpc_client_register(clnt, args->authflavor, args->client_name);
402 if (err) 422 if (err)
403 goto out_no_path; 423 goto out_no_path;
404 if (parent) 424 if (parent)
@@ -600,6 +620,80 @@ rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
600} 620}
601EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth); 621EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
602 622
623/**
624 * rpc_switch_client_transport: switch the RPC transport on the fly
625 * @clnt: pointer to a struct rpc_clnt
626 * @args: pointer to the new transport arguments
627 * @timeout: pointer to the new timeout parameters
628 *
629 * This function allows the caller to switch the RPC transport for the
630 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
631 * server, for instance. It assumes that the caller has ensured that
632 * there are no active RPC tasks by using some form of locking.
633 *
634 * Returns zero if "clnt" is now using the new xprt. Otherwise a
635 * negative errno is returned, and "clnt" continues to use the old
636 * xprt.
637 */
638int rpc_switch_client_transport(struct rpc_clnt *clnt,
639 struct xprt_create *args,
640 const struct rpc_timeout *timeout)
641{
642 const struct rpc_timeout *old_timeo;
643 rpc_authflavor_t pseudoflavor;
644 struct rpc_xprt *xprt, *old;
645 struct rpc_clnt *parent;
646 int err;
647
648 xprt = xprt_create_transport(args);
649 if (IS_ERR(xprt)) {
650 dprintk("RPC: failed to create new xprt for clnt %p\n",
651 clnt);
652 return PTR_ERR(xprt);
653 }
654
655 pseudoflavor = clnt->cl_auth->au_flavor;
656
657 old_timeo = clnt->cl_timeout;
658 old = rpc_clnt_set_transport(clnt, xprt, timeout);
659
660 rpc_unregister_client(clnt);
661 __rpc_clnt_remove_pipedir(clnt);
662
663 /*
664 * A new transport was created. "clnt" therefore
665 * becomes the root of a new cl_parent tree. clnt's
666 * children, if it has any, still point to the old xprt.
667 */
668 parent = clnt->cl_parent;
669 clnt->cl_parent = clnt;
670
671 /*
672 * The old rpc_auth cache cannot be re-used. GSS
673 * contexts in particular are between a single
674 * client and server.
675 */
676 err = rpc_client_register(clnt, pseudoflavor, NULL);
677 if (err)
678 goto out_revert;
679
680 synchronize_rcu();
681 if (parent != clnt)
682 rpc_release_client(parent);
683 xprt_put(old);
684 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
685 return 0;
686
687out_revert:
688 rpc_clnt_set_transport(clnt, old, old_timeo);
689 clnt->cl_parent = parent;
690 rpc_client_register(clnt, pseudoflavor, NULL);
691 xprt_put(xprt);
692 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
693 return err;
694}
695EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
696
603/* 697/*
604 * Kill all tasks for the given client. 698 * Kill all tasks for the given client.
605 * XXX: kill their descendants as well? 699 * XXX: kill their descendants as well?
@@ -656,14 +750,16 @@ EXPORT_SYMBOL_GPL(rpc_shutdown_client);
656/* 750/*
657 * Free an RPC client 751 * Free an RPC client
658 */ 752 */
659static void 753static struct rpc_clnt *
660rpc_free_client(struct rpc_clnt *clnt) 754rpc_free_client(struct rpc_clnt *clnt)
661{ 755{
756 struct rpc_clnt *parent = NULL;
757
662 dprintk_rcu("RPC: destroying %s client for %s\n", 758 dprintk_rcu("RPC: destroying %s client for %s\n",
663 clnt->cl_program->name, 759 clnt->cl_program->name,
664 rcu_dereference(clnt->cl_xprt)->servername); 760 rcu_dereference(clnt->cl_xprt)->servername);
665 if (clnt->cl_parent != clnt) 761 if (clnt->cl_parent != clnt)
666 rpc_release_client(clnt->cl_parent); 762 parent = clnt->cl_parent;
667 rpc_clnt_remove_pipedir(clnt); 763 rpc_clnt_remove_pipedir(clnt);
668 rpc_unregister_client(clnt); 764 rpc_unregister_client(clnt);
669 rpc_free_iostats(clnt->cl_metrics); 765 rpc_free_iostats(clnt->cl_metrics);
@@ -672,18 +768,17 @@ rpc_free_client(struct rpc_clnt *clnt)
672 rpciod_down(); 768 rpciod_down();
673 rpc_free_clid(clnt); 769 rpc_free_clid(clnt);
674 kfree(clnt); 770 kfree(clnt);
771 return parent;
675} 772}
676 773
677/* 774/*
678 * Free an RPC client 775 * Free an RPC client
679 */ 776 */
680static void 777static struct rpc_clnt *
681rpc_free_auth(struct rpc_clnt *clnt) 778rpc_free_auth(struct rpc_clnt *clnt)
682{ 779{
683 if (clnt->cl_auth == NULL) { 780 if (clnt->cl_auth == NULL)
684 rpc_free_client(clnt); 781 return rpc_free_client(clnt);
685 return;
686 }
687 782
688 /* 783 /*
689 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to 784 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
@@ -694,7 +789,8 @@ rpc_free_auth(struct rpc_clnt *clnt)
694 rpcauth_release(clnt->cl_auth); 789 rpcauth_release(clnt->cl_auth);
695 clnt->cl_auth = NULL; 790 clnt->cl_auth = NULL;
696 if (atomic_dec_and_test(&clnt->cl_count)) 791 if (atomic_dec_and_test(&clnt->cl_count))
697 rpc_free_client(clnt); 792 return rpc_free_client(clnt);
793 return NULL;
698} 794}
699 795
700/* 796/*
@@ -705,10 +801,13 @@ rpc_release_client(struct rpc_clnt *clnt)
705{ 801{
706 dprintk("RPC: rpc_release_client(%p)\n", clnt); 802 dprintk("RPC: rpc_release_client(%p)\n", clnt);
707 803
708 if (list_empty(&clnt->cl_tasks)) 804 do {
709 wake_up(&destroy_wait); 805 if (list_empty(&clnt->cl_tasks))
710 if (atomic_dec_and_test(&clnt->cl_count)) 806 wake_up(&destroy_wait);
711 rpc_free_auth(clnt); 807 if (!atomic_dec_and_test(&clnt->cl_count))
808 break;
809 clnt = rpc_free_auth(clnt);
810 } while (clnt != NULL);
712} 811}
713EXPORT_SYMBOL_GPL(rpc_release_client); 812EXPORT_SYMBOL_GPL(rpc_release_client);
714 813
@@ -772,6 +871,8 @@ void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
772 atomic_inc(&clnt->cl_count); 871 atomic_inc(&clnt->cl_count);
773 if (clnt->cl_softrtry) 872 if (clnt->cl_softrtry)
774 task->tk_flags |= RPC_TASK_SOFT; 873 task->tk_flags |= RPC_TASK_SOFT;
874 if (clnt->cl_noretranstimeo)
875 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
775 if (sk_memalloc_socks()) { 876 if (sk_memalloc_socks()) {
776 struct rpc_xprt *xprt; 877 struct rpc_xprt *xprt;
777 878
@@ -1690,6 +1791,7 @@ call_connect_status(struct rpc_task *task)
1690 dprint_status(task); 1791 dprint_status(task);
1691 1792
1692 trace_rpc_connect_status(task, status); 1793 trace_rpc_connect_status(task, status);
1794 task->tk_status = 0;
1693 switch (status) { 1795 switch (status) {
1694 /* if soft mounted, test if we've timed out */ 1796 /* if soft mounted, test if we've timed out */
1695 case -ETIMEDOUT: 1797 case -ETIMEDOUT:
@@ -1698,12 +1800,14 @@ call_connect_status(struct rpc_task *task)
1698 case -ECONNREFUSED: 1800 case -ECONNREFUSED:
1699 case -ECONNRESET: 1801 case -ECONNRESET:
1700 case -ENETUNREACH: 1802 case -ENETUNREACH:
1803 /* retry with existing socket, after a delay */
1804 rpc_delay(task, 3*HZ);
1701 if (RPC_IS_SOFTCONN(task)) 1805 if (RPC_IS_SOFTCONN(task))
1702 break; 1806 break;
1703 /* retry with existing socket, after a delay */
1704 case 0:
1705 case -EAGAIN: 1807 case -EAGAIN:
1706 task->tk_status = 0; 1808 task->tk_action = call_bind;
1809 return;
1810 case 0:
1707 clnt->cl_stats->netreconn++; 1811 clnt->cl_stats->netreconn++;
1708 task->tk_action = call_transmit; 1812 task->tk_action = call_transmit;
1709 return; 1813 return;
@@ -1717,13 +1821,14 @@ call_connect_status(struct rpc_task *task)
1717static void 1821static void
1718call_transmit(struct rpc_task *task) 1822call_transmit(struct rpc_task *task)
1719{ 1823{
1824 int is_retrans = RPC_WAS_SENT(task);
1825
1720 dprint_status(task); 1826 dprint_status(task);
1721 1827
1722 task->tk_action = call_status; 1828 task->tk_action = call_status;
1723 if (task->tk_status < 0) 1829 if (task->tk_status < 0)
1724 return; 1830 return;
1725 task->tk_status = xprt_prepare_transmit(task); 1831 if (!xprt_prepare_transmit(task))
1726 if (task->tk_status != 0)
1727 return; 1832 return;
1728 task->tk_action = call_transmit_status; 1833 task->tk_action = call_transmit_status;
1729 /* Encode here so that rpcsec_gss can use correct sequence number. */ 1834 /* Encode here so that rpcsec_gss can use correct sequence number. */
@@ -1742,6 +1847,8 @@ call_transmit(struct rpc_task *task)
1742 xprt_transmit(task); 1847 xprt_transmit(task);
1743 if (task->tk_status < 0) 1848 if (task->tk_status < 0)
1744 return; 1849 return;
1850 if (is_retrans)
1851 task->tk_client->cl_stats->rpcretrans++;
1745 /* 1852 /*
1746 * On success, ensure that we call xprt_end_transmit() before sleeping 1853 * On success, ensure that we call xprt_end_transmit() before sleeping
1747 * in order to allow access to the socket to other RPC requests. 1854 * in order to allow access to the socket to other RPC requests.
@@ -1811,8 +1918,7 @@ call_bc_transmit(struct rpc_task *task)
1811{ 1918{
1812 struct rpc_rqst *req = task->tk_rqstp; 1919 struct rpc_rqst *req = task->tk_rqstp;
1813 1920
1814 task->tk_status = xprt_prepare_transmit(task); 1921 if (!xprt_prepare_transmit(task)) {
1815 if (task->tk_status == -EAGAIN) {
1816 /* 1922 /*
1817 * Could not reserve the transport. Try again after the 1923 * Could not reserve the transport. Try again after the
1818 * transport is released. 1924 * transport is released.
@@ -1900,7 +2006,8 @@ call_status(struct rpc_task *task)
1900 rpc_delay(task, 3*HZ); 2006 rpc_delay(task, 3*HZ);
1901 case -ETIMEDOUT: 2007 case -ETIMEDOUT:
1902 task->tk_action = call_timeout; 2008 task->tk_action = call_timeout;
1903 if (task->tk_client->cl_discrtry) 2009 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
2010 && task->tk_client->cl_discrtry)
1904 xprt_conditional_disconnect(req->rq_xprt, 2011 xprt_conditional_disconnect(req->rq_xprt,
1905 req->rq_connect_cookie); 2012 req->rq_connect_cookie);
1906 break; 2013 break;
@@ -1982,7 +2089,6 @@ call_timeout(struct rpc_task *task)
1982 rpcauth_invalcred(task); 2089 rpcauth_invalcred(task);
1983 2090
1984retry: 2091retry:
1985 clnt->cl_stats->rpcretrans++;
1986 task->tk_action = call_bind; 2092 task->tk_action = call_bind;
1987 task->tk_status = 0; 2093 task->tk_status = 0;
1988} 2094}
@@ -2025,7 +2131,6 @@ call_decode(struct rpc_task *task)
2025 if (req->rq_rcv_buf.len < 12) { 2131 if (req->rq_rcv_buf.len < 12) {
2026 if (!RPC_IS_SOFT(task)) { 2132 if (!RPC_IS_SOFT(task)) {
2027 task->tk_action = call_bind; 2133 task->tk_action = call_bind;
2028 clnt->cl_stats->rpcretrans++;
2029 goto out_retry; 2134 goto out_retry;
2030 } 2135 }
2031 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n", 2136 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
diff --git a/net/sunrpc/rpc_pipe.c b/net/sunrpc/rpc_pipe.c
index f94567b45bb3..bf04b30a788a 100644
--- a/net/sunrpc/rpc_pipe.c
+++ b/net/sunrpc/rpc_pipe.c
@@ -471,15 +471,6 @@ struct rpc_filelist {
471 umode_t mode; 471 umode_t mode;
472}; 472};
473 473
474static int rpc_delete_dentry(const struct dentry *dentry)
475{
476 return 1;
477}
478
479static const struct dentry_operations rpc_dentry_operations = {
480 .d_delete = rpc_delete_dentry,
481};
482
483static struct inode * 474static struct inode *
484rpc_get_inode(struct super_block *sb, umode_t mode) 475rpc_get_inode(struct super_block *sb, umode_t mode)
485{ 476{
@@ -519,8 +510,8 @@ static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
519 d_add(dentry, inode); 510 d_add(dentry, inode);
520 return 0; 511 return 0;
521out_err: 512out_err:
522 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n", 513 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
523 __FILE__, __func__, dentry->d_name.name); 514 __FILE__, __func__, dentry);
524 dput(dentry); 515 dput(dentry);
525 return -ENOMEM; 516 return -ENOMEM;
526} 517}
@@ -755,8 +746,8 @@ static int rpc_populate(struct dentry *parent,
755out_bad: 746out_bad:
756 __rpc_depopulate(parent, files, start, eof); 747 __rpc_depopulate(parent, files, start, eof);
757 mutex_unlock(&dir->i_mutex); 748 mutex_unlock(&dir->i_mutex);
758 printk(KERN_WARNING "%s: %s failed to populate directory %s\n", 749 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
759 __FILE__, __func__, parent->d_name.name); 750 __FILE__, __func__, parent);
760 return err; 751 return err;
761} 752}
762 753
@@ -852,8 +843,8 @@ out:
852 return dentry; 843 return dentry;
853out_err: 844out_err:
854 dentry = ERR_PTR(err); 845 dentry = ERR_PTR(err);
855 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n", 846 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
856 __FILE__, __func__, parent->d_name.name, name, 847 __FILE__, __func__, parent, name,
857 err); 848 err);
858 goto out; 849 goto out;
859} 850}
@@ -1266,7 +1257,7 @@ rpc_fill_super(struct super_block *sb, void *data, int silent)
1266 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 1257 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
1267 sb->s_magic = RPCAUTH_GSSMAGIC; 1258 sb->s_magic = RPCAUTH_GSSMAGIC;
1268 sb->s_op = &s_ops; 1259 sb->s_op = &s_ops;
1269 sb->s_d_op = &rpc_dentry_operations; 1260 sb->s_d_op = &simple_dentry_operations;
1270 sb->s_time_gran = 1; 1261 sb->s_time_gran = 1;
1271 1262
1272 inode = rpc_get_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO); 1263 inode = rpc_get_inode(sb, S_IFDIR | S_IRUGO | S_IXUGO);
diff --git a/net/sunrpc/svc.c b/net/sunrpc/svc.c
index b974571126fe..e7fbe368b4a3 100644
--- a/net/sunrpc/svc.c
+++ b/net/sunrpc/svc.c
@@ -1104,8 +1104,6 @@ svc_process_common(struct svc_rqst *rqstp, struct kvec *argv, struct kvec *resv)
1104 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */ 1104 rqstp->rq_vers = vers = svc_getnl(argv); /* version number */
1105 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */ 1105 rqstp->rq_proc = proc = svc_getnl(argv); /* procedure number */
1106 1106
1107 progp = serv->sv_program;
1108
1109 for (progp = serv->sv_program; progp; progp = progp->pg_next) 1107 for (progp = serv->sv_program; progp; progp = progp->pg_next)
1110 if (prog == progp->pg_prog) 1108 if (prog == progp->pg_prog)
1111 break; 1109 break;
diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index 9c9caaa5e0d3..b6e59f0a9475 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -291,12 +291,14 @@ static int svc_one_sock_name(struct svc_sock *svsk, char *buf, int remaining)
291 &inet_sk(sk)->inet_rcv_saddr, 291 &inet_sk(sk)->inet_rcv_saddr,
292 inet_sk(sk)->inet_num); 292 inet_sk(sk)->inet_num);
293 break; 293 break;
294#if IS_ENABLED(CONFIG_IPV6)
294 case PF_INET6: 295 case PF_INET6:
295 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n", 296 len = snprintf(buf, remaining, "ipv6 %s %pI6 %d\n",
296 proto_name, 297 proto_name,
297 &inet6_sk(sk)->rcv_saddr, 298 &sk->sk_v6_rcv_saddr,
298 inet_sk(sk)->inet_num); 299 inet_sk(sk)->inet_num);
299 break; 300 break;
301#endif
300 default: 302 default:
301 len = snprintf(buf, remaining, "*unknown-%d*\n", 303 len = snprintf(buf, remaining, "*unknown-%d*\n",
302 sk->sk_family); 304 sk->sk_family);
diff --git a/net/sunrpc/xprt.c b/net/sunrpc/xprt.c
index 095363eee764..04199bc8416f 100644
--- a/net/sunrpc/xprt.c
+++ b/net/sunrpc/xprt.c
@@ -205,10 +205,8 @@ int xprt_reserve_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
205 goto out_sleep; 205 goto out_sleep;
206 } 206 }
207 xprt->snd_task = task; 207 xprt->snd_task = task;
208 if (req != NULL) { 208 if (req != NULL)
209 req->rq_bytes_sent = 0;
210 req->rq_ntrans++; 209 req->rq_ntrans++;
211 }
212 210
213 return 1; 211 return 1;
214 212
@@ -263,7 +261,6 @@ int xprt_reserve_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
263 } 261 }
264 if (__xprt_get_cong(xprt, task)) { 262 if (__xprt_get_cong(xprt, task)) {
265 xprt->snd_task = task; 263 xprt->snd_task = task;
266 req->rq_bytes_sent = 0;
267 req->rq_ntrans++; 264 req->rq_ntrans++;
268 return 1; 265 return 1;
269 } 266 }
@@ -300,10 +297,8 @@ static bool __xprt_lock_write_func(struct rpc_task *task, void *data)
300 297
301 req = task->tk_rqstp; 298 req = task->tk_rqstp;
302 xprt->snd_task = task; 299 xprt->snd_task = task;
303 if (req) { 300 if (req)
304 req->rq_bytes_sent = 0;
305 req->rq_ntrans++; 301 req->rq_ntrans++;
306 }
307 return true; 302 return true;
308} 303}
309 304
@@ -329,7 +324,6 @@ static bool __xprt_lock_write_cong_func(struct rpc_task *task, void *data)
329 } 324 }
330 if (__xprt_get_cong(xprt, task)) { 325 if (__xprt_get_cong(xprt, task)) {
331 xprt->snd_task = task; 326 xprt->snd_task = task;
332 req->rq_bytes_sent = 0;
333 req->rq_ntrans++; 327 req->rq_ntrans++;
334 return true; 328 return true;
335 } 329 }
@@ -358,6 +352,11 @@ out_unlock:
358void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task) 352void xprt_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
359{ 353{
360 if (xprt->snd_task == task) { 354 if (xprt->snd_task == task) {
355 if (task != NULL) {
356 struct rpc_rqst *req = task->tk_rqstp;
357 if (req != NULL)
358 req->rq_bytes_sent = 0;
359 }
361 xprt_clear_locked(xprt); 360 xprt_clear_locked(xprt);
362 __xprt_lock_write_next(xprt); 361 __xprt_lock_write_next(xprt);
363 } 362 }
@@ -375,6 +374,11 @@ EXPORT_SYMBOL_GPL(xprt_release_xprt);
375void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task) 374void xprt_release_xprt_cong(struct rpc_xprt *xprt, struct rpc_task *task)
376{ 375{
377 if (xprt->snd_task == task) { 376 if (xprt->snd_task == task) {
377 if (task != NULL) {
378 struct rpc_rqst *req = task->tk_rqstp;
379 if (req != NULL)
380 req->rq_bytes_sent = 0;
381 }
378 xprt_clear_locked(xprt); 382 xprt_clear_locked(xprt);
379 __xprt_lock_write_next_cong(xprt); 383 __xprt_lock_write_next_cong(xprt);
380 } 384 }
@@ -854,24 +858,36 @@ static inline int xprt_has_timer(struct rpc_xprt *xprt)
854 * @task: RPC task about to send a request 858 * @task: RPC task about to send a request
855 * 859 *
856 */ 860 */
857int xprt_prepare_transmit(struct rpc_task *task) 861bool xprt_prepare_transmit(struct rpc_task *task)
858{ 862{
859 struct rpc_rqst *req = task->tk_rqstp; 863 struct rpc_rqst *req = task->tk_rqstp;
860 struct rpc_xprt *xprt = req->rq_xprt; 864 struct rpc_xprt *xprt = req->rq_xprt;
861 int err = 0; 865 bool ret = false;
862 866
863 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid); 867 dprintk("RPC: %5u xprt_prepare_transmit\n", task->tk_pid);
864 868
865 spin_lock_bh(&xprt->transport_lock); 869 spin_lock_bh(&xprt->transport_lock);
866 if (req->rq_reply_bytes_recvd && !req->rq_bytes_sent) { 870 if (!req->rq_bytes_sent) {
867 err = req->rq_reply_bytes_recvd; 871 if (req->rq_reply_bytes_recvd) {
872 task->tk_status = req->rq_reply_bytes_recvd;
873 goto out_unlock;
874 }
875 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
876 && xprt_connected(xprt)
877 && req->rq_connect_cookie == xprt->connect_cookie) {
878 xprt->ops->set_retrans_timeout(task);
879 rpc_sleep_on(&xprt->pending, task, xprt_timer);
880 goto out_unlock;
881 }
882 }
883 if (!xprt->ops->reserve_xprt(xprt, task)) {
884 task->tk_status = -EAGAIN;
868 goto out_unlock; 885 goto out_unlock;
869 } 886 }
870 if (!xprt->ops->reserve_xprt(xprt, task)) 887 ret = true;
871 err = -EAGAIN;
872out_unlock: 888out_unlock:
873 spin_unlock_bh(&xprt->transport_lock); 889 spin_unlock_bh(&xprt->transport_lock);
874 return err; 890 return ret;
875} 891}
876 892
877void xprt_end_transmit(struct rpc_task *task) 893void xprt_end_transmit(struct rpc_task *task)
@@ -912,7 +928,6 @@ void xprt_transmit(struct rpc_task *task)
912 } else if (!req->rq_bytes_sent) 928 } else if (!req->rq_bytes_sent)
913 return; 929 return;
914 930
915 req->rq_connect_cookie = xprt->connect_cookie;
916 req->rq_xtime = ktime_get(); 931 req->rq_xtime = ktime_get();
917 status = xprt->ops->send_request(task); 932 status = xprt->ops->send_request(task);
918 if (status != 0) { 933 if (status != 0) {
@@ -938,12 +953,14 @@ void xprt_transmit(struct rpc_task *task)
938 /* Don't race with disconnect */ 953 /* Don't race with disconnect */
939 if (!xprt_connected(xprt)) 954 if (!xprt_connected(xprt))
940 task->tk_status = -ENOTCONN; 955 task->tk_status = -ENOTCONN;
941 else if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task)) { 956 else {
942 /* 957 /*
943 * Sleep on the pending queue since 958 * Sleep on the pending queue since
944 * we're expecting a reply. 959 * we're expecting a reply.
945 */ 960 */
946 rpc_sleep_on(&xprt->pending, task, xprt_timer); 961 if (!req->rq_reply_bytes_recvd && rpc_reply_expected(task))
962 rpc_sleep_on(&xprt->pending, task, xprt_timer);
963 req->rq_connect_cookie = xprt->connect_cookie;
947 } 964 }
948 spin_unlock_bh(&xprt->transport_lock); 965 spin_unlock_bh(&xprt->transport_lock);
949} 966}
@@ -1087,11 +1104,9 @@ struct rpc_xprt *xprt_alloc(struct net *net, size_t size,
1087 for (i = 0; i < num_prealloc; i++) { 1104 for (i = 0; i < num_prealloc; i++) {
1088 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL); 1105 req = kzalloc(sizeof(struct rpc_rqst), GFP_KERNEL);
1089 if (!req) 1106 if (!req)
1090 break; 1107 goto out_free;
1091 list_add(&req->rq_list, &xprt->free); 1108 list_add(&req->rq_list, &xprt->free);
1092 } 1109 }
1093 if (i < num_prealloc)
1094 goto out_free;
1095 if (max_alloc > num_prealloc) 1110 if (max_alloc > num_prealloc)
1096 xprt->max_reqs = max_alloc; 1111 xprt->max_reqs = max_alloc;
1097 else 1112 else
@@ -1186,6 +1201,12 @@ static void xprt_request_init(struct rpc_task *task, struct rpc_xprt *xprt)
1186 req->rq_xprt = xprt; 1201 req->rq_xprt = xprt;
1187 req->rq_buffer = NULL; 1202 req->rq_buffer = NULL;
1188 req->rq_xid = xprt_alloc_xid(xprt); 1203 req->rq_xid = xprt_alloc_xid(xprt);
1204 req->rq_connect_cookie = xprt->connect_cookie - 1;
1205 req->rq_bytes_sent = 0;
1206 req->rq_snd_buf.len = 0;
1207 req->rq_snd_buf.buflen = 0;
1208 req->rq_rcv_buf.len = 0;
1209 req->rq_rcv_buf.buflen = 0;
1189 req->rq_release_snd_buf = NULL; 1210 req->rq_release_snd_buf = NULL;
1190 xprt_reset_majortimeo(req); 1211 xprt_reset_majortimeo(req);
1191 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid, 1212 dprintk("RPC: %5u reserved req %p xid %08x\n", task->tk_pid,
diff --git a/net/sunrpc/xprtsock.c b/net/sunrpc/xprtsock.c
index ee03d35677d9..dd9d295813cf 100644
--- a/net/sunrpc/xprtsock.c
+++ b/net/sunrpc/xprtsock.c
@@ -393,8 +393,10 @@ static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen,
393 return kernel_sendmsg(sock, &msg, NULL, 0, 0); 393 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
394} 394}
395 395
396static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more) 396static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy)
397{ 397{
398 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
399 int offset, size_t size, int flags);
398 struct page **ppage; 400 struct page **ppage;
399 unsigned int remainder; 401 unsigned int remainder;
400 int err, sent = 0; 402 int err, sent = 0;
@@ -403,6 +405,9 @@ static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned i
403 base += xdr->page_base; 405 base += xdr->page_base;
404 ppage = xdr->pages + (base >> PAGE_SHIFT); 406 ppage = xdr->pages + (base >> PAGE_SHIFT);
405 base &= ~PAGE_MASK; 407 base &= ~PAGE_MASK;
408 do_sendpage = sock->ops->sendpage;
409 if (!zerocopy)
410 do_sendpage = sock_no_sendpage;
406 for(;;) { 411 for(;;) {
407 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder); 412 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
408 int flags = XS_SENDMSG_FLAGS; 413 int flags = XS_SENDMSG_FLAGS;
@@ -410,7 +415,7 @@ static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned i
410 remainder -= len; 415 remainder -= len;
411 if (remainder != 0 || more) 416 if (remainder != 0 || more)
412 flags |= MSG_MORE; 417 flags |= MSG_MORE;
413 err = sock->ops->sendpage(sock, *ppage, base, len, flags); 418 err = do_sendpage(sock, *ppage, base, len, flags);
414 if (remainder == 0 || err != len) 419 if (remainder == 0 || err != len)
415 break; 420 break;
416 sent += err; 421 sent += err;
@@ -431,9 +436,10 @@ static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned i
431 * @addrlen: UDP only -- length of destination address 436 * @addrlen: UDP only -- length of destination address
432 * @xdr: buffer containing this request 437 * @xdr: buffer containing this request
433 * @base: starting position in the buffer 438 * @base: starting position in the buffer
439 * @zerocopy: true if it is safe to use sendpage()
434 * 440 *
435 */ 441 */
436static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base) 442static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy)
437{ 443{
438 unsigned int remainder = xdr->len - base; 444 unsigned int remainder = xdr->len - base;
439 int err, sent = 0; 445 int err, sent = 0;
@@ -461,7 +467,7 @@ static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen,
461 if (base < xdr->page_len) { 467 if (base < xdr->page_len) {
462 unsigned int len = xdr->page_len - base; 468 unsigned int len = xdr->page_len - base;
463 remainder -= len; 469 remainder -= len;
464 err = xs_send_pagedata(sock, xdr, base, remainder != 0); 470 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy);
465 if (remainder == 0 || err != len) 471 if (remainder == 0 || err != len)
466 goto out; 472 goto out;
467 sent += err; 473 sent += err;
@@ -564,7 +570,7 @@ static int xs_local_send_request(struct rpc_task *task)
564 req->rq_svec->iov_base, req->rq_svec->iov_len); 570 req->rq_svec->iov_base, req->rq_svec->iov_len);
565 571
566 status = xs_sendpages(transport->sock, NULL, 0, 572 status = xs_sendpages(transport->sock, NULL, 0,
567 xdr, req->rq_bytes_sent); 573 xdr, req->rq_bytes_sent, true);
568 dprintk("RPC: %s(%u) = %d\n", 574 dprintk("RPC: %s(%u) = %d\n",
569 __func__, xdr->len - req->rq_bytes_sent, status); 575 __func__, xdr->len - req->rq_bytes_sent, status);
570 if (likely(status >= 0)) { 576 if (likely(status >= 0)) {
@@ -620,7 +626,7 @@ static int xs_udp_send_request(struct rpc_task *task)
620 status = xs_sendpages(transport->sock, 626 status = xs_sendpages(transport->sock,
621 xs_addr(xprt), 627 xs_addr(xprt),
622 xprt->addrlen, xdr, 628 xprt->addrlen, xdr,
623 req->rq_bytes_sent); 629 req->rq_bytes_sent, true);
624 630
625 dprintk("RPC: xs_udp_send_request(%u) = %d\n", 631 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
626 xdr->len - req->rq_bytes_sent, status); 632 xdr->len - req->rq_bytes_sent, status);
@@ -693,6 +699,7 @@ static int xs_tcp_send_request(struct rpc_task *task)
693 struct rpc_xprt *xprt = req->rq_xprt; 699 struct rpc_xprt *xprt = req->rq_xprt;
694 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt); 700 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
695 struct xdr_buf *xdr = &req->rq_snd_buf; 701 struct xdr_buf *xdr = &req->rq_snd_buf;
702 bool zerocopy = true;
696 int status; 703 int status;
697 704
698 xs_encode_stream_record_marker(&req->rq_snd_buf); 705 xs_encode_stream_record_marker(&req->rq_snd_buf);
@@ -700,13 +707,20 @@ static int xs_tcp_send_request(struct rpc_task *task)
700 xs_pktdump("packet data:", 707 xs_pktdump("packet data:",
701 req->rq_svec->iov_base, 708 req->rq_svec->iov_base,
702 req->rq_svec->iov_len); 709 req->rq_svec->iov_len);
710 /* Don't use zero copy if this is a resend. If the RPC call
711 * completes while the socket holds a reference to the pages,
712 * then we may end up resending corrupted data.
713 */
714 if (task->tk_flags & RPC_TASK_SENT)
715 zerocopy = false;
703 716
704 /* Continue transmitting the packet/record. We must be careful 717 /* Continue transmitting the packet/record. We must be careful
705 * to cope with writespace callbacks arriving _after_ we have 718 * to cope with writespace callbacks arriving _after_ we have
706 * called sendmsg(). */ 719 * called sendmsg(). */
707 while (1) { 720 while (1) {
708 status = xs_sendpages(transport->sock, 721 status = xs_sendpages(transport->sock,
709 NULL, 0, xdr, req->rq_bytes_sent); 722 NULL, 0, xdr, req->rq_bytes_sent,
723 zerocopy);
710 724
711 dprintk("RPC: xs_tcp_send_request(%u) = %d\n", 725 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
712 xdr->len - req->rq_bytes_sent, status); 726 xdr->len - req->rq_bytes_sent, status);
@@ -835,6 +849,8 @@ static void xs_close(struct rpc_xprt *xprt)
835 849
836 dprintk("RPC: xs_close xprt %p\n", xprt); 850 dprintk("RPC: xs_close xprt %p\n", xprt);
837 851
852 cancel_delayed_work_sync(&transport->connect_worker);
853
838 xs_reset_transport(transport); 854 xs_reset_transport(transport);
839 xprt->reestablish_timeout = 0; 855 xprt->reestablish_timeout = 0;
840 856
@@ -854,14 +870,6 @@ static void xs_tcp_close(struct rpc_xprt *xprt)
854 xs_tcp_shutdown(xprt); 870 xs_tcp_shutdown(xprt);
855} 871}
856 872
857static void xs_local_destroy(struct rpc_xprt *xprt)
858{
859 xs_close(xprt);
860 xs_free_peer_addresses(xprt);
861 xprt_free(xprt);
862 module_put(THIS_MODULE);
863}
864
865/** 873/**
866 * xs_destroy - prepare to shutdown a transport 874 * xs_destroy - prepare to shutdown a transport
867 * @xprt: doomed transport 875 * @xprt: doomed transport
@@ -869,13 +877,12 @@ static void xs_local_destroy(struct rpc_xprt *xprt)
869 */ 877 */
870static void xs_destroy(struct rpc_xprt *xprt) 878static void xs_destroy(struct rpc_xprt *xprt)
871{ 879{
872 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
873
874 dprintk("RPC: xs_destroy xprt %p\n", xprt); 880 dprintk("RPC: xs_destroy xprt %p\n", xprt);
875 881
876 cancel_delayed_work_sync(&transport->connect_worker); 882 xs_close(xprt);
877 883 xs_free_peer_addresses(xprt);
878 xs_local_destroy(xprt); 884 xprt_free(xprt);
885 module_put(THIS_MODULE);
879} 886}
880 887
881static inline struct rpc_xprt *xprt_from_sock(struct sock *sk) 888static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
@@ -1511,6 +1518,7 @@ static void xs_tcp_state_change(struct sock *sk)
1511 transport->tcp_copied = 0; 1518 transport->tcp_copied = 0;
1512 transport->tcp_flags = 1519 transport->tcp_flags =
1513 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID; 1520 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1521 xprt->connect_cookie++;
1514 1522
1515 xprt_wake_pending_tasks(xprt, -EAGAIN); 1523 xprt_wake_pending_tasks(xprt, -EAGAIN);
1516 } 1524 }
@@ -1816,6 +1824,10 @@ static inline void xs_reclassify_socket(int family, struct socket *sock)
1816} 1824}
1817#endif 1825#endif
1818 1826
1827static void xs_dummy_setup_socket(struct work_struct *work)
1828{
1829}
1830
1819static struct socket *xs_create_sock(struct rpc_xprt *xprt, 1831static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1820 struct sock_xprt *transport, int family, int type, int protocol) 1832 struct sock_xprt *transport, int family, int type, int protocol)
1821{ 1833{
@@ -2112,6 +2124,19 @@ static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2112 2124
2113 if (!transport->inet) { 2125 if (!transport->inet) {
2114 struct sock *sk = sock->sk; 2126 struct sock *sk = sock->sk;
2127 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2128 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2129 unsigned int opt_on = 1;
2130
2131 /* TCP Keepalive options */
2132 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2133 (char *)&opt_on, sizeof(opt_on));
2134 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2135 (char *)&keepidle, sizeof(keepidle));
2136 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2137 (char *)&keepidle, sizeof(keepidle));
2138 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2139 (char *)&keepcnt, sizeof(keepcnt));
2115 2140
2116 write_lock_bh(&sk->sk_callback_lock); 2141 write_lock_bh(&sk->sk_callback_lock);
2117 2142
@@ -2151,7 +2176,6 @@ static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2151 case 0: 2176 case 0:
2152 case -EINPROGRESS: 2177 case -EINPROGRESS:
2153 /* SYN_SENT! */ 2178 /* SYN_SENT! */
2154 xprt->connect_cookie++;
2155 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO) 2179 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2156 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO; 2180 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2157 } 2181 }
@@ -2498,7 +2522,7 @@ static struct rpc_xprt_ops xs_local_ops = {
2498 .send_request = xs_local_send_request, 2522 .send_request = xs_local_send_request,
2499 .set_retrans_timeout = xprt_set_retrans_timeout_def, 2523 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2500 .close = xs_close, 2524 .close = xs_close,
2501 .destroy = xs_local_destroy, 2525 .destroy = xs_destroy,
2502 .print_stats = xs_local_print_stats, 2526 .print_stats = xs_local_print_stats,
2503}; 2527};
2504 2528
@@ -2655,6 +2679,9 @@ static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2655 xprt->ops = &xs_local_ops; 2679 xprt->ops = &xs_local_ops;
2656 xprt->timeout = &xs_local_default_timeout; 2680 xprt->timeout = &xs_local_default_timeout;
2657 2681
2682 INIT_DELAYED_WORK(&transport->connect_worker,
2683 xs_dummy_setup_socket);
2684
2658 switch (sun->sun_family) { 2685 switch (sun->sun_family) {
2659 case AF_LOCAL: 2686 case AF_LOCAL:
2660 if (sun->sun_path[0] != '/') { 2687 if (sun->sun_path[0] != '/') {
@@ -2859,8 +2886,8 @@ static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2859 if (args->bc_xprt->xpt_bc_xprt) { 2886 if (args->bc_xprt->xpt_bc_xprt) {
2860 /* 2887 /*
2861 * This server connection already has a backchannel 2888 * This server connection already has a backchannel
2862 * export; we can't create a new one, as we wouldn't be 2889 * transport; we can't create a new one, as we wouldn't
2863 * able to match replies based on xid any more. So, 2890 * be able to match replies based on xid any more. So,
2864 * reuse the already-existing one: 2891 * reuse the already-existing one:
2865 */ 2892 */
2866 return args->bc_xprt->xpt_bc_xprt; 2893 return args->bc_xprt->xpt_bc_xprt;
diff --git a/net/tipc/bcast.c b/net/tipc/bcast.c
index 716de1ac6cb5..0d4402587fdf 100644
--- a/net/tipc/bcast.c
+++ b/net/tipc/bcast.c
@@ -480,18 +480,24 @@ receive:
480 tipc_node_unlock(node); 480 tipc_node_unlock(node);
481 tipc_link_recv_bundle(buf); 481 tipc_link_recv_bundle(buf);
482 } else if (msg_user(msg) == MSG_FRAGMENTER) { 482 } else if (msg_user(msg) == MSG_FRAGMENTER) {
483 int ret = tipc_link_recv_fragment(&node->bclink.defragm, 483 int ret;
484 &buf, &msg); 484 ret = tipc_link_recv_fragment(&node->bclink.reasm_head,
485 if (ret < 0) 485 &node->bclink.reasm_tail,
486 &buf);
487 if (ret == LINK_REASM_ERROR)
486 goto unlock; 488 goto unlock;
487 spin_lock_bh(&bc_lock); 489 spin_lock_bh(&bc_lock);
488 bclink_accept_pkt(node, seqno); 490 bclink_accept_pkt(node, seqno);
489 bcl->stats.recv_fragments++; 491 bcl->stats.recv_fragments++;
490 if (ret > 0) 492 if (ret == LINK_REASM_COMPLETE) {
491 bcl->stats.recv_fragmented++; 493 bcl->stats.recv_fragmented++;
494 /* Point msg to inner header */
495 msg = buf_msg(buf);
496 spin_unlock_bh(&bc_lock);
497 goto receive;
498 }
492 spin_unlock_bh(&bc_lock); 499 spin_unlock_bh(&bc_lock);
493 tipc_node_unlock(node); 500 tipc_node_unlock(node);
494 tipc_net_route_msg(buf);
495 } else if (msg_user(msg) == NAME_DISTRIBUTOR) { 501 } else if (msg_user(msg) == NAME_DISTRIBUTOR) {
496 spin_lock_bh(&bc_lock); 502 spin_lock_bh(&bc_lock);
497 bclink_accept_pkt(node, seqno); 503 bclink_accept_pkt(node, seqno);
diff --git a/net/tipc/bearer.c b/net/tipc/bearer.c
index 609c30c80816..3f9707a16d06 100644
--- a/net/tipc/bearer.c
+++ b/net/tipc/bearer.c
@@ -387,7 +387,7 @@ restart:
387 387
388 b_ptr = &tipc_bearers[bearer_id]; 388 b_ptr = &tipc_bearers[bearer_id];
389 strcpy(b_ptr->name, name); 389 strcpy(b_ptr->name, name);
390 res = m_ptr->enable_bearer(b_ptr); 390 res = m_ptr->enable_media(b_ptr);
391 if (res) { 391 if (res) {
392 pr_warn("Bearer <%s> rejected, enable failure (%d)\n", 392 pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
393 name, -res); 393 name, -res);
@@ -420,23 +420,15 @@ exit:
420} 420}
421 421
422/** 422/**
423 * tipc_block_bearer - Block the bearer with the given name, and reset all its links 423 * tipc_block_bearer - Block the bearer, and reset all its links
424 */ 424 */
425int tipc_block_bearer(const char *name) 425int tipc_block_bearer(struct tipc_bearer *b_ptr)
426{ 426{
427 struct tipc_bearer *b_ptr = NULL;
428 struct tipc_link *l_ptr; 427 struct tipc_link *l_ptr;
429 struct tipc_link *temp_l_ptr; 428 struct tipc_link *temp_l_ptr;
430 429
431 read_lock_bh(&tipc_net_lock); 430 read_lock_bh(&tipc_net_lock);
432 b_ptr = tipc_bearer_find(name); 431 pr_info("Blocking bearer <%s>\n", b_ptr->name);
433 if (!b_ptr) {
434 pr_warn("Attempt to block unknown bearer <%s>\n", name);
435 read_unlock_bh(&tipc_net_lock);
436 return -EINVAL;
437 }
438
439 pr_info("Blocking bearer <%s>\n", name);
440 spin_lock_bh(&b_ptr->lock); 432 spin_lock_bh(&b_ptr->lock);
441 b_ptr->blocked = 1; 433 b_ptr->blocked = 1;
442 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) { 434 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
@@ -465,7 +457,7 @@ static void bearer_disable(struct tipc_bearer *b_ptr)
465 pr_info("Disabling bearer <%s>\n", b_ptr->name); 457 pr_info("Disabling bearer <%s>\n", b_ptr->name);
466 spin_lock_bh(&b_ptr->lock); 458 spin_lock_bh(&b_ptr->lock);
467 b_ptr->blocked = 1; 459 b_ptr->blocked = 1;
468 b_ptr->media->disable_bearer(b_ptr); 460 b_ptr->media->disable_media(b_ptr);
469 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) { 461 list_for_each_entry_safe(l_ptr, temp_l_ptr, &b_ptr->links, link_list) {
470 tipc_link_delete(l_ptr); 462 tipc_link_delete(l_ptr);
471 } 463 }
diff --git a/net/tipc/bearer.h b/net/tipc/bearer.h
index 09c869adcfcf..e5e04be6fffa 100644
--- a/net/tipc/bearer.h
+++ b/net/tipc/bearer.h
@@ -75,8 +75,8 @@ struct tipc_bearer;
75/** 75/**
76 * struct tipc_media - TIPC media information available to internal users 76 * struct tipc_media - TIPC media information available to internal users
77 * @send_msg: routine which handles buffer transmission 77 * @send_msg: routine which handles buffer transmission
78 * @enable_bearer: routine which enables a bearer 78 * @enable_media: routine which enables a media
79 * @disable_bearer: routine which disables a bearer 79 * @disable_media: routine which disables a media
80 * @addr2str: routine which converts media address to string 80 * @addr2str: routine which converts media address to string
81 * @addr2msg: routine which converts media address to protocol message area 81 * @addr2msg: routine which converts media address to protocol message area
82 * @msg2addr: routine which converts media address from protocol message area 82 * @msg2addr: routine which converts media address from protocol message area
@@ -91,8 +91,8 @@ struct tipc_media {
91 int (*send_msg)(struct sk_buff *buf, 91 int (*send_msg)(struct sk_buff *buf,
92 struct tipc_bearer *b_ptr, 92 struct tipc_bearer *b_ptr,
93 struct tipc_media_addr *dest); 93 struct tipc_media_addr *dest);
94 int (*enable_bearer)(struct tipc_bearer *b_ptr); 94 int (*enable_media)(struct tipc_bearer *b_ptr);
95 void (*disable_bearer)(struct tipc_bearer *b_ptr); 95 void (*disable_media)(struct tipc_bearer *b_ptr);
96 int (*addr2str)(struct tipc_media_addr *a, char *str_buf, int str_size); 96 int (*addr2str)(struct tipc_media_addr *a, char *str_buf, int str_size);
97 int (*addr2msg)(struct tipc_media_addr *a, char *msg_area); 97 int (*addr2msg)(struct tipc_media_addr *a, char *msg_area);
98 int (*msg2addr)(const struct tipc_bearer *b_ptr, 98 int (*msg2addr)(const struct tipc_bearer *b_ptr,
@@ -163,7 +163,7 @@ int tipc_register_media(struct tipc_media *m_ptr);
163 163
164void tipc_recv_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr); 164void tipc_recv_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr);
165 165
166int tipc_block_bearer(const char *name); 166int tipc_block_bearer(struct tipc_bearer *b_ptr);
167void tipc_continue(struct tipc_bearer *tb_ptr); 167void tipc_continue(struct tipc_bearer *tb_ptr);
168 168
169int tipc_enable_bearer(const char *bearer_name, u32 disc_domain, u32 priority); 169int tipc_enable_bearer(const char *bearer_name, u32 disc_domain, u32 priority);
diff --git a/net/tipc/core.c b/net/tipc/core.c
index fd4eeeaa972a..c6d3f75a9e1b 100644
--- a/net/tipc/core.c
+++ b/net/tipc/core.c
@@ -113,7 +113,6 @@ err:
113static void tipc_core_stop(void) 113static void tipc_core_stop(void)
114{ 114{
115 tipc_netlink_stop(); 115 tipc_netlink_stop();
116 tipc_handler_stop();
117 tipc_cfg_stop(); 116 tipc_cfg_stop();
118 tipc_subscr_stop(); 117 tipc_subscr_stop();
119 tipc_nametbl_stop(); 118 tipc_nametbl_stop();
@@ -146,9 +145,10 @@ static int tipc_core_start(void)
146 res = tipc_subscr_start(); 145 res = tipc_subscr_start();
147 if (!res) 146 if (!res)
148 res = tipc_cfg_init(); 147 res = tipc_cfg_init();
149 if (res) 148 if (res) {
149 tipc_handler_stop();
150 tipc_core_stop(); 150 tipc_core_stop();
151 151 }
152 return res; 152 return res;
153} 153}
154 154
@@ -178,6 +178,7 @@ static int __init tipc_init(void)
178 178
179static void __exit tipc_exit(void) 179static void __exit tipc_exit(void)
180{ 180{
181 tipc_handler_stop();
181 tipc_core_stop_net(); 182 tipc_core_stop_net();
182 tipc_core_stop(); 183 tipc_core_stop();
183 pr_info("Deactivated\n"); 184 pr_info("Deactivated\n");
diff --git a/net/tipc/core.h b/net/tipc/core.h
index be72f8cebc53..94895d4e86ab 100644
--- a/net/tipc/core.h
+++ b/net/tipc/core.h
@@ -90,21 +90,21 @@ extern int tipc_random __read_mostly;
90/* 90/*
91 * Routines available to privileged subsystems 91 * Routines available to privileged subsystems
92 */ 92 */
93extern int tipc_core_start_net(unsigned long); 93int tipc_core_start_net(unsigned long);
94extern int tipc_handler_start(void); 94int tipc_handler_start(void);
95extern void tipc_handler_stop(void); 95void tipc_handler_stop(void);
96extern int tipc_netlink_start(void); 96int tipc_netlink_start(void);
97extern void tipc_netlink_stop(void); 97void tipc_netlink_stop(void);
98extern int tipc_socket_init(void); 98int tipc_socket_init(void);
99extern void tipc_socket_stop(void); 99void tipc_socket_stop(void);
100extern int tipc_sock_create_local(int type, struct socket **res); 100int tipc_sock_create_local(int type, struct socket **res);
101extern void tipc_sock_release_local(struct socket *sock); 101void tipc_sock_release_local(struct socket *sock);
102extern int tipc_sock_accept_local(struct socket *sock, 102int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
103 struct socket **newsock, int flags); 103 int flags);
104 104
105#ifdef CONFIG_SYSCTL 105#ifdef CONFIG_SYSCTL
106extern int tipc_register_sysctl(void); 106int tipc_register_sysctl(void);
107extern void tipc_unregister_sysctl(void); 107void tipc_unregister_sysctl(void);
108#else 108#else
109#define tipc_register_sysctl() 0 109#define tipc_register_sysctl() 0
110#define tipc_unregister_sysctl() 110#define tipc_unregister_sysctl()
@@ -201,6 +201,6 @@ static inline struct tipc_msg *buf_msg(struct sk_buff *skb)
201 return (struct tipc_msg *)skb->data; 201 return (struct tipc_msg *)skb->data;
202} 202}
203 203
204extern struct sk_buff *tipc_buf_acquire(u32 size); 204struct sk_buff *tipc_buf_acquire(u32 size);
205 205
206#endif 206#endif
diff --git a/net/tipc/eth_media.c b/net/tipc/eth_media.c
index 40ea40cf6204..f80d59f5a161 100644
--- a/net/tipc/eth_media.c
+++ b/net/tipc/eth_media.c
@@ -2,7 +2,7 @@
2 * net/tipc/eth_media.c: Ethernet bearer support for TIPC 2 * net/tipc/eth_media.c: Ethernet bearer support for TIPC
3 * 3 *
4 * Copyright (c) 2001-2007, Ericsson AB 4 * Copyright (c) 2001-2007, Ericsson AB
5 * Copyright (c) 2005-2008, 2011, Wind River Systems 5 * Copyright (c) 2005-2008, 2011-2013, Wind River Systems
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
@@ -37,19 +37,19 @@
37#include "core.h" 37#include "core.h"
38#include "bearer.h" 38#include "bearer.h"
39 39
40#define MAX_ETH_BEARERS MAX_BEARERS 40#define MAX_ETH_MEDIA MAX_BEARERS
41 41
42#define ETH_ADDR_OFFSET 4 /* message header offset of MAC address */ 42#define ETH_ADDR_OFFSET 4 /* message header offset of MAC address */
43 43
44/** 44/**
45 * struct eth_bearer - Ethernet bearer data structure 45 * struct eth_media - Ethernet bearer data structure
46 * @bearer: ptr to associated "generic" bearer structure 46 * @bearer: ptr to associated "generic" bearer structure
47 * @dev: ptr to associated Ethernet network device 47 * @dev: ptr to associated Ethernet network device
48 * @tipc_packet_type: used in binding TIPC to Ethernet driver 48 * @tipc_packet_type: used in binding TIPC to Ethernet driver
49 * @setup: work item used when enabling bearer 49 * @setup: work item used when enabling bearer
50 * @cleanup: work item used when disabling bearer 50 * @cleanup: work item used when disabling bearer
51 */ 51 */
52struct eth_bearer { 52struct eth_media {
53 struct tipc_bearer *bearer; 53 struct tipc_bearer *bearer;
54 struct net_device *dev; 54 struct net_device *dev;
55 struct packet_type tipc_packet_type; 55 struct packet_type tipc_packet_type;
@@ -58,7 +58,7 @@ struct eth_bearer {
58}; 58};
59 59
60static struct tipc_media eth_media_info; 60static struct tipc_media eth_media_info;
61static struct eth_bearer eth_bearers[MAX_ETH_BEARERS]; 61static struct eth_media eth_media_array[MAX_ETH_MEDIA];
62static int eth_started; 62static int eth_started;
63 63
64static int recv_notification(struct notifier_block *nb, unsigned long evt, 64static int recv_notification(struct notifier_block *nb, unsigned long evt,
@@ -100,7 +100,7 @@ static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
100 if (!clone) 100 if (!clone)
101 return 0; 101 return 0;
102 102
103 dev = ((struct eth_bearer *)(tb_ptr->usr_handle))->dev; 103 dev = ((struct eth_media *)(tb_ptr->usr_handle))->dev;
104 delta = dev->hard_header_len - skb_headroom(buf); 104 delta = dev->hard_header_len - skb_headroom(buf);
105 105
106 if ((delta > 0) && 106 if ((delta > 0) &&
@@ -128,43 +128,43 @@ static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
128static int recv_msg(struct sk_buff *buf, struct net_device *dev, 128static int recv_msg(struct sk_buff *buf, struct net_device *dev,
129 struct packet_type *pt, struct net_device *orig_dev) 129 struct packet_type *pt, struct net_device *orig_dev)
130{ 130{
131 struct eth_bearer *eb_ptr = (struct eth_bearer *)pt->af_packet_priv; 131 struct eth_media *eb_ptr = (struct eth_media *)pt->af_packet_priv;
132 132
133 if (!net_eq(dev_net(dev), &init_net)) { 133 if (!net_eq(dev_net(dev), &init_net)) {
134 kfree_skb(buf); 134 kfree_skb(buf);
135 return 0; 135 return NET_RX_DROP;
136 } 136 }
137 137
138 if (likely(eb_ptr->bearer)) { 138 if (likely(eb_ptr->bearer)) {
139 if (likely(buf->pkt_type <= PACKET_BROADCAST)) { 139 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
140 buf->next = NULL; 140 buf->next = NULL;
141 tipc_recv_msg(buf, eb_ptr->bearer); 141 tipc_recv_msg(buf, eb_ptr->bearer);
142 return 0; 142 return NET_RX_SUCCESS;
143 } 143 }
144 } 144 }
145 kfree_skb(buf); 145 kfree_skb(buf);
146 return 0; 146 return NET_RX_DROP;
147} 147}
148 148
149/** 149/**
150 * setup_bearer - setup association between Ethernet bearer and interface 150 * setup_media - setup association between Ethernet bearer and interface
151 */ 151 */
152static void setup_bearer(struct work_struct *work) 152static void setup_media(struct work_struct *work)
153{ 153{
154 struct eth_bearer *eb_ptr = 154 struct eth_media *eb_ptr =
155 container_of(work, struct eth_bearer, setup); 155 container_of(work, struct eth_media, setup);
156 156
157 dev_add_pack(&eb_ptr->tipc_packet_type); 157 dev_add_pack(&eb_ptr->tipc_packet_type);
158} 158}
159 159
160/** 160/**
161 * enable_bearer - attach TIPC bearer to an Ethernet interface 161 * enable_media - attach TIPC bearer to an Ethernet interface
162 */ 162 */
163static int enable_bearer(struct tipc_bearer *tb_ptr) 163static int enable_media(struct tipc_bearer *tb_ptr)
164{ 164{
165 struct net_device *dev; 165 struct net_device *dev;
166 struct eth_bearer *eb_ptr = &eth_bearers[0]; 166 struct eth_media *eb_ptr = &eth_media_array[0];
167 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS]; 167 struct eth_media *stop = &eth_media_array[MAX_ETH_MEDIA];
168 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1; 168 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
169 int pending_dev = 0; 169 int pending_dev = 0;
170 170
@@ -188,7 +188,7 @@ static int enable_bearer(struct tipc_bearer *tb_ptr)
188 eb_ptr->tipc_packet_type.func = recv_msg; 188 eb_ptr->tipc_packet_type.func = recv_msg;
189 eb_ptr->tipc_packet_type.af_packet_priv = eb_ptr; 189 eb_ptr->tipc_packet_type.af_packet_priv = eb_ptr;
190 INIT_LIST_HEAD(&(eb_ptr->tipc_packet_type.list)); 190 INIT_LIST_HEAD(&(eb_ptr->tipc_packet_type.list));
191 INIT_WORK(&eb_ptr->setup, setup_bearer); 191 INIT_WORK(&eb_ptr->setup, setup_media);
192 schedule_work(&eb_ptr->setup); 192 schedule_work(&eb_ptr->setup);
193 193
194 /* Associate TIPC bearer with Ethernet bearer */ 194 /* Associate TIPC bearer with Ethernet bearer */
@@ -205,14 +205,14 @@ static int enable_bearer(struct tipc_bearer *tb_ptr)
205} 205}
206 206
207/** 207/**
208 * cleanup_bearer - break association between Ethernet bearer and interface 208 * cleanup_media - break association between Ethernet bearer and interface
209 * 209 *
210 * This routine must be invoked from a work queue because it can sleep. 210 * This routine must be invoked from a work queue because it can sleep.
211 */ 211 */
212static void cleanup_bearer(struct work_struct *work) 212static void cleanup_media(struct work_struct *work)
213{ 213{
214 struct eth_bearer *eb_ptr = 214 struct eth_media *eb_ptr =
215 container_of(work, struct eth_bearer, cleanup); 215 container_of(work, struct eth_media, cleanup);
216 216
217 dev_remove_pack(&eb_ptr->tipc_packet_type); 217 dev_remove_pack(&eb_ptr->tipc_packet_type);
218 dev_put(eb_ptr->dev); 218 dev_put(eb_ptr->dev);
@@ -220,18 +220,18 @@ static void cleanup_bearer(struct work_struct *work)
220} 220}
221 221
222/** 222/**
223 * disable_bearer - detach TIPC bearer from an Ethernet interface 223 * disable_media - detach TIPC bearer from an Ethernet interface
224 * 224 *
225 * Mark Ethernet bearer as inactive so that incoming buffers are thrown away, 225 * Mark Ethernet bearer as inactive so that incoming buffers are thrown away,
226 * then get worker thread to complete bearer cleanup. (Can't do cleanup 226 * then get worker thread to complete bearer cleanup. (Can't do cleanup
227 * here because cleanup code needs to sleep and caller holds spinlocks.) 227 * here because cleanup code needs to sleep and caller holds spinlocks.)
228 */ 228 */
229static void disable_bearer(struct tipc_bearer *tb_ptr) 229static void disable_media(struct tipc_bearer *tb_ptr)
230{ 230{
231 struct eth_bearer *eb_ptr = (struct eth_bearer *)tb_ptr->usr_handle; 231 struct eth_media *eb_ptr = (struct eth_media *)tb_ptr->usr_handle;
232 232
233 eb_ptr->bearer = NULL; 233 eb_ptr->bearer = NULL;
234 INIT_WORK(&eb_ptr->cleanup, cleanup_bearer); 234 INIT_WORK(&eb_ptr->cleanup, cleanup_media);
235 schedule_work(&eb_ptr->cleanup); 235 schedule_work(&eb_ptr->cleanup);
236} 236}
237 237
@@ -245,8 +245,8 @@ static int recv_notification(struct notifier_block *nb, unsigned long evt,
245 void *ptr) 245 void *ptr)
246{ 246{
247 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 247 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
248 struct eth_bearer *eb_ptr = &eth_bearers[0]; 248 struct eth_media *eb_ptr = &eth_media_array[0];
249 struct eth_bearer *stop = &eth_bearers[MAX_ETH_BEARERS]; 249 struct eth_media *stop = &eth_media_array[MAX_ETH_MEDIA];
250 250
251 if (!net_eq(dev_net(dev), &init_net)) 251 if (!net_eq(dev_net(dev), &init_net))
252 return NOTIFY_DONE; 252 return NOTIFY_DONE;
@@ -265,17 +265,17 @@ static int recv_notification(struct notifier_block *nb, unsigned long evt,
265 if (netif_carrier_ok(dev)) 265 if (netif_carrier_ok(dev))
266 tipc_continue(eb_ptr->bearer); 266 tipc_continue(eb_ptr->bearer);
267 else 267 else
268 tipc_block_bearer(eb_ptr->bearer->name); 268 tipc_block_bearer(eb_ptr->bearer);
269 break; 269 break;
270 case NETDEV_UP: 270 case NETDEV_UP:
271 tipc_continue(eb_ptr->bearer); 271 tipc_continue(eb_ptr->bearer);
272 break; 272 break;
273 case NETDEV_DOWN: 273 case NETDEV_DOWN:
274 tipc_block_bearer(eb_ptr->bearer->name); 274 tipc_block_bearer(eb_ptr->bearer);
275 break; 275 break;
276 case NETDEV_CHANGEMTU: 276 case NETDEV_CHANGEMTU:
277 case NETDEV_CHANGEADDR: 277 case NETDEV_CHANGEADDR:
278 tipc_block_bearer(eb_ptr->bearer->name); 278 tipc_block_bearer(eb_ptr->bearer);
279 tipc_continue(eb_ptr->bearer); 279 tipc_continue(eb_ptr->bearer);
280 break; 280 break;
281 case NETDEV_UNREGISTER: 281 case NETDEV_UNREGISTER:
@@ -327,8 +327,8 @@ static int eth_msg2addr(const struct tipc_bearer *tb_ptr,
327 */ 327 */
328static struct tipc_media eth_media_info = { 328static struct tipc_media eth_media_info = {
329 .send_msg = send_msg, 329 .send_msg = send_msg,
330 .enable_bearer = enable_bearer, 330 .enable_media = enable_media,
331 .disable_bearer = disable_bearer, 331 .disable_media = disable_media,
332 .addr2str = eth_addr2str, 332 .addr2str = eth_addr2str,
333 .addr2msg = eth_addr2msg, 333 .addr2msg = eth_addr2msg,
334 .msg2addr = eth_msg2addr, 334 .msg2addr = eth_msg2addr,
diff --git a/net/tipc/handler.c b/net/tipc/handler.c
index b36f0fcd9bdf..e4bc8a296744 100644
--- a/net/tipc/handler.c
+++ b/net/tipc/handler.c
@@ -56,12 +56,13 @@ unsigned int tipc_k_signal(Handler routine, unsigned long argument)
56{ 56{
57 struct queue_item *item; 57 struct queue_item *item;
58 58
59 spin_lock_bh(&qitem_lock);
59 if (!handler_enabled) { 60 if (!handler_enabled) {
60 pr_err("Signal request ignored by handler\n"); 61 pr_err("Signal request ignored by handler\n");
62 spin_unlock_bh(&qitem_lock);
61 return -ENOPROTOOPT; 63 return -ENOPROTOOPT;
62 } 64 }
63 65
64 spin_lock_bh(&qitem_lock);
65 item = kmem_cache_alloc(tipc_queue_item_cache, GFP_ATOMIC); 66 item = kmem_cache_alloc(tipc_queue_item_cache, GFP_ATOMIC);
66 if (!item) { 67 if (!item) {
67 pr_err("Signal queue out of memory\n"); 68 pr_err("Signal queue out of memory\n");
@@ -112,10 +113,14 @@ void tipc_handler_stop(void)
112 struct list_head *l, *n; 113 struct list_head *l, *n;
113 struct queue_item *item; 114 struct queue_item *item;
114 115
115 if (!handler_enabled) 116 spin_lock_bh(&qitem_lock);
117 if (!handler_enabled) {
118 spin_unlock_bh(&qitem_lock);
116 return; 119 return;
117 120 }
118 handler_enabled = 0; 121 handler_enabled = 0;
122 spin_unlock_bh(&qitem_lock);
123
119 tasklet_kill(&tipc_tasklet); 124 tasklet_kill(&tipc_tasklet);
120 125
121 spin_lock_bh(&qitem_lock); 126 spin_lock_bh(&qitem_lock);
diff --git a/net/tipc/ib_media.c b/net/tipc/ib_media.c
index 9934a32bfa87..c13989297464 100644
--- a/net/tipc/ib_media.c
+++ b/net/tipc/ib_media.c
@@ -42,17 +42,17 @@
42#include "core.h" 42#include "core.h"
43#include "bearer.h" 43#include "bearer.h"
44 44
45#define MAX_IB_BEARERS MAX_BEARERS 45#define MAX_IB_MEDIA MAX_BEARERS
46 46
47/** 47/**
48 * struct ib_bearer - Infiniband bearer data structure 48 * struct ib_media - Infiniband media data structure
49 * @bearer: ptr to associated "generic" bearer structure 49 * @bearer: ptr to associated "generic" bearer structure
50 * @dev: ptr to associated Infiniband network device 50 * @dev: ptr to associated Infiniband network device
51 * @tipc_packet_type: used in binding TIPC to Infiniband driver 51 * @tipc_packet_type: used in binding TIPC to Infiniband driver
52 * @cleanup: work item used when disabling bearer 52 * @cleanup: work item used when disabling bearer
53 */ 53 */
54 54
55struct ib_bearer { 55struct ib_media {
56 struct tipc_bearer *bearer; 56 struct tipc_bearer *bearer;
57 struct net_device *dev; 57 struct net_device *dev;
58 struct packet_type tipc_packet_type; 58 struct packet_type tipc_packet_type;
@@ -61,7 +61,7 @@ struct ib_bearer {
61}; 61};
62 62
63static struct tipc_media ib_media_info; 63static struct tipc_media ib_media_info;
64static struct ib_bearer ib_bearers[MAX_IB_BEARERS]; 64static struct ib_media ib_media_array[MAX_IB_MEDIA];
65static int ib_started; 65static int ib_started;
66 66
67/** 67/**
@@ -93,7 +93,7 @@ static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
93 if (!clone) 93 if (!clone)
94 return 0; 94 return 0;
95 95
96 dev = ((struct ib_bearer *)(tb_ptr->usr_handle))->dev; 96 dev = ((struct ib_media *)(tb_ptr->usr_handle))->dev;
97 delta = dev->hard_header_len - skb_headroom(buf); 97 delta = dev->hard_header_len - skb_headroom(buf);
98 98
99 if ((delta > 0) && 99 if ((delta > 0) &&
@@ -121,43 +121,43 @@ static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
121static int recv_msg(struct sk_buff *buf, struct net_device *dev, 121static int recv_msg(struct sk_buff *buf, struct net_device *dev,
122 struct packet_type *pt, struct net_device *orig_dev) 122 struct packet_type *pt, struct net_device *orig_dev)
123{ 123{
124 struct ib_bearer *ib_ptr = (struct ib_bearer *)pt->af_packet_priv; 124 struct ib_media *ib_ptr = (struct ib_media *)pt->af_packet_priv;
125 125
126 if (!net_eq(dev_net(dev), &init_net)) { 126 if (!net_eq(dev_net(dev), &init_net)) {
127 kfree_skb(buf); 127 kfree_skb(buf);
128 return 0; 128 return NET_RX_DROP;
129 } 129 }
130 130
131 if (likely(ib_ptr->bearer)) { 131 if (likely(ib_ptr->bearer)) {
132 if (likely(buf->pkt_type <= PACKET_BROADCAST)) { 132 if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
133 buf->next = NULL; 133 buf->next = NULL;
134 tipc_recv_msg(buf, ib_ptr->bearer); 134 tipc_recv_msg(buf, ib_ptr->bearer);
135 return 0; 135 return NET_RX_SUCCESS;
136 } 136 }
137 } 137 }
138 kfree_skb(buf); 138 kfree_skb(buf);
139 return 0; 139 return NET_RX_DROP;
140} 140}
141 141
142/** 142/**
143 * setup_bearer - setup association between InfiniBand bearer and interface 143 * setup_bearer - setup association between InfiniBand bearer and interface
144 */ 144 */
145static void setup_bearer(struct work_struct *work) 145static void setup_media(struct work_struct *work)
146{ 146{
147 struct ib_bearer *ib_ptr = 147 struct ib_media *ib_ptr =
148 container_of(work, struct ib_bearer, setup); 148 container_of(work, struct ib_media, setup);
149 149
150 dev_add_pack(&ib_ptr->tipc_packet_type); 150 dev_add_pack(&ib_ptr->tipc_packet_type);
151} 151}
152 152
153/** 153/**
154 * enable_bearer - attach TIPC bearer to an InfiniBand interface 154 * enable_media - attach TIPC bearer to an InfiniBand interface
155 */ 155 */
156static int enable_bearer(struct tipc_bearer *tb_ptr) 156static int enable_media(struct tipc_bearer *tb_ptr)
157{ 157{
158 struct net_device *dev; 158 struct net_device *dev;
159 struct ib_bearer *ib_ptr = &ib_bearers[0]; 159 struct ib_media *ib_ptr = &ib_media_array[0];
160 struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS]; 160 struct ib_media *stop = &ib_media_array[MAX_IB_MEDIA];
161 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1; 161 char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
162 int pending_dev = 0; 162 int pending_dev = 0;
163 163
@@ -181,7 +181,7 @@ static int enable_bearer(struct tipc_bearer *tb_ptr)
181 ib_ptr->tipc_packet_type.func = recv_msg; 181 ib_ptr->tipc_packet_type.func = recv_msg;
182 ib_ptr->tipc_packet_type.af_packet_priv = ib_ptr; 182 ib_ptr->tipc_packet_type.af_packet_priv = ib_ptr;
183 INIT_LIST_HEAD(&(ib_ptr->tipc_packet_type.list)); 183 INIT_LIST_HEAD(&(ib_ptr->tipc_packet_type.list));
184 INIT_WORK(&ib_ptr->setup, setup_bearer); 184 INIT_WORK(&ib_ptr->setup, setup_media);
185 schedule_work(&ib_ptr->setup); 185 schedule_work(&ib_ptr->setup);
186 186
187 /* Associate TIPC bearer with InfiniBand bearer */ 187 /* Associate TIPC bearer with InfiniBand bearer */
@@ -204,8 +204,8 @@ static int enable_bearer(struct tipc_bearer *tb_ptr)
204 */ 204 */
205static void cleanup_bearer(struct work_struct *work) 205static void cleanup_bearer(struct work_struct *work)
206{ 206{
207 struct ib_bearer *ib_ptr = 207 struct ib_media *ib_ptr =
208 container_of(work, struct ib_bearer, cleanup); 208 container_of(work, struct ib_media, cleanup);
209 209
210 dev_remove_pack(&ib_ptr->tipc_packet_type); 210 dev_remove_pack(&ib_ptr->tipc_packet_type);
211 dev_put(ib_ptr->dev); 211 dev_put(ib_ptr->dev);
@@ -213,15 +213,15 @@ static void cleanup_bearer(struct work_struct *work)
213} 213}
214 214
215/** 215/**
216 * disable_bearer - detach TIPC bearer from an InfiniBand interface 216 * disable_media - detach TIPC bearer from an InfiniBand interface
217 * 217 *
218 * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away, 218 * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
219 * then get worker thread to complete bearer cleanup. (Can't do cleanup 219 * then get worker thread to complete bearer cleanup. (Can't do cleanup
220 * here because cleanup code needs to sleep and caller holds spinlocks.) 220 * here because cleanup code needs to sleep and caller holds spinlocks.)
221 */ 221 */
222static void disable_bearer(struct tipc_bearer *tb_ptr) 222static void disable_media(struct tipc_bearer *tb_ptr)
223{ 223{
224 struct ib_bearer *ib_ptr = (struct ib_bearer *)tb_ptr->usr_handle; 224 struct ib_media *ib_ptr = (struct ib_media *)tb_ptr->usr_handle;
225 225
226 ib_ptr->bearer = NULL; 226 ib_ptr->bearer = NULL;
227 INIT_WORK(&ib_ptr->cleanup, cleanup_bearer); 227 INIT_WORK(&ib_ptr->cleanup, cleanup_bearer);
@@ -238,8 +238,8 @@ static int recv_notification(struct notifier_block *nb, unsigned long evt,
238 void *ptr) 238 void *ptr)
239{ 239{
240 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 240 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
241 struct ib_bearer *ib_ptr = &ib_bearers[0]; 241 struct ib_media *ib_ptr = &ib_media_array[0];
242 struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS]; 242 struct ib_media *stop = &ib_media_array[MAX_IB_MEDIA];
243 243
244 if (!net_eq(dev_net(dev), &init_net)) 244 if (!net_eq(dev_net(dev), &init_net))
245 return NOTIFY_DONE; 245 return NOTIFY_DONE;
@@ -258,17 +258,17 @@ static int recv_notification(struct notifier_block *nb, unsigned long evt,
258 if (netif_carrier_ok(dev)) 258 if (netif_carrier_ok(dev))
259 tipc_continue(ib_ptr->bearer); 259 tipc_continue(ib_ptr->bearer);
260 else 260 else
261 tipc_block_bearer(ib_ptr->bearer->name); 261 tipc_block_bearer(ib_ptr->bearer);
262 break; 262 break;
263 case NETDEV_UP: 263 case NETDEV_UP:
264 tipc_continue(ib_ptr->bearer); 264 tipc_continue(ib_ptr->bearer);
265 break; 265 break;
266 case NETDEV_DOWN: 266 case NETDEV_DOWN:
267 tipc_block_bearer(ib_ptr->bearer->name); 267 tipc_block_bearer(ib_ptr->bearer);
268 break; 268 break;
269 case NETDEV_CHANGEMTU: 269 case NETDEV_CHANGEMTU:
270 case NETDEV_CHANGEADDR: 270 case NETDEV_CHANGEADDR:
271 tipc_block_bearer(ib_ptr->bearer->name); 271 tipc_block_bearer(ib_ptr->bearer);
272 tipc_continue(ib_ptr->bearer); 272 tipc_continue(ib_ptr->bearer);
273 break; 273 break;
274 case NETDEV_UNREGISTER: 274 case NETDEV_UNREGISTER:
@@ -323,8 +323,8 @@ static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
323 */ 323 */
324static struct tipc_media ib_media_info = { 324static struct tipc_media ib_media_info = {
325 .send_msg = send_msg, 325 .send_msg = send_msg,
326 .enable_bearer = enable_bearer, 326 .enable_media = enable_media,
327 .disable_bearer = disable_bearer, 327 .disable_media = disable_media,
328 .addr2str = ib_addr2str, 328 .addr2str = ib_addr2str,
329 .addr2msg = ib_addr2msg, 329 .addr2msg = ib_addr2msg,
330 .msg2addr = ib_msg2addr, 330 .msg2addr = ib_msg2addr,
diff --git a/net/tipc/link.c b/net/tipc/link.c
index 0cc3d9015c5d..13b987745820 100644
--- a/net/tipc/link.c
+++ b/net/tipc/link.c
@@ -75,20 +75,6 @@ static const char *link_unk_evt = "Unknown link event ";
75 */ 75 */
76#define START_CHANGEOVER 100000u 76#define START_CHANGEOVER 100000u
77 77
78/**
79 * struct tipc_link_name - deconstructed link name
80 * @addr_local: network address of node at this end
81 * @if_local: name of interface at this end
82 * @addr_peer: network address of node at far end
83 * @if_peer: name of interface at far end
84 */
85struct tipc_link_name {
86 u32 addr_local;
87 char if_local[TIPC_MAX_IF_NAME];
88 u32 addr_peer;
89 char if_peer[TIPC_MAX_IF_NAME];
90};
91
92static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr, 78static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
93 struct sk_buff *buf); 79 struct sk_buff *buf);
94static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf); 80static void link_recv_proto_msg(struct tipc_link *l_ptr, struct sk_buff *buf);
@@ -97,8 +83,7 @@ static int link_recv_changeover_msg(struct tipc_link **l_ptr,
97static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance); 83static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance);
98static int link_send_sections_long(struct tipc_port *sender, 84static int link_send_sections_long(struct tipc_port *sender,
99 struct iovec const *msg_sect, 85 struct iovec const *msg_sect,
100 u32 num_sect, unsigned int total_len, 86 unsigned int len, u32 destnode);
101 u32 destnode);
102static void link_state_event(struct tipc_link *l_ptr, u32 event); 87static void link_state_event(struct tipc_link *l_ptr, u32 event);
103static void link_reset_statistics(struct tipc_link *l_ptr); 88static void link_reset_statistics(struct tipc_link *l_ptr);
104static void link_print(struct tipc_link *l_ptr, const char *str); 89static void link_print(struct tipc_link *l_ptr, const char *str);
@@ -161,72 +146,6 @@ int tipc_link_is_active(struct tipc_link *l_ptr)
161} 146}
162 147
163/** 148/**
164 * link_name_validate - validate & (optionally) deconstruct tipc_link name
165 * @name: ptr to link name string
166 * @name_parts: ptr to area for link name components (or NULL if not needed)
167 *
168 * Returns 1 if link name is valid, otherwise 0.
169 */
170static int link_name_validate(const char *name,
171 struct tipc_link_name *name_parts)
172{
173 char name_copy[TIPC_MAX_LINK_NAME];
174 char *addr_local;
175 char *if_local;
176 char *addr_peer;
177 char *if_peer;
178 char dummy;
179 u32 z_local, c_local, n_local;
180 u32 z_peer, c_peer, n_peer;
181 u32 if_local_len;
182 u32 if_peer_len;
183
184 /* copy link name & ensure length is OK */
185 name_copy[TIPC_MAX_LINK_NAME - 1] = 0;
186 /* need above in case non-Posix strncpy() doesn't pad with nulls */
187 strncpy(name_copy, name, TIPC_MAX_LINK_NAME);
188 if (name_copy[TIPC_MAX_LINK_NAME - 1] != 0)
189 return 0;
190
191 /* ensure all component parts of link name are present */
192 addr_local = name_copy;
193 if_local = strchr(addr_local, ':');
194 if (if_local == NULL)
195 return 0;
196 *(if_local++) = 0;
197 addr_peer = strchr(if_local, '-');
198 if (addr_peer == NULL)
199 return 0;
200 *(addr_peer++) = 0;
201 if_local_len = addr_peer - if_local;
202 if_peer = strchr(addr_peer, ':');
203 if (if_peer == NULL)
204 return 0;
205 *(if_peer++) = 0;
206 if_peer_len = strlen(if_peer) + 1;
207
208 /* validate component parts of link name */
209 if ((sscanf(addr_local, "%u.%u.%u%c",
210 &z_local, &c_local, &n_local, &dummy) != 3) ||
211 (sscanf(addr_peer, "%u.%u.%u%c",
212 &z_peer, &c_peer, &n_peer, &dummy) != 3) ||
213 (z_local > 255) || (c_local > 4095) || (n_local > 4095) ||
214 (z_peer > 255) || (c_peer > 4095) || (n_peer > 4095) ||
215 (if_local_len <= 1) || (if_local_len > TIPC_MAX_IF_NAME) ||
216 (if_peer_len <= 1) || (if_peer_len > TIPC_MAX_IF_NAME))
217 return 0;
218
219 /* return link name components, if necessary */
220 if (name_parts) {
221 name_parts->addr_local = tipc_addr(z_local, c_local, n_local);
222 strcpy(name_parts->if_local, if_local);
223 name_parts->addr_peer = tipc_addr(z_peer, c_peer, n_peer);
224 strcpy(name_parts->if_peer, if_peer);
225 }
226 return 1;
227}
228
229/**
230 * link_timeout - handle expiration of link timer 149 * link_timeout - handle expiration of link timer
231 * @l_ptr: pointer to link 150 * @l_ptr: pointer to link
232 * 151 *
@@ -485,15 +404,9 @@ static void link_release_outqueue(struct tipc_link *l_ptr)
485 */ 404 */
486void tipc_link_reset_fragments(struct tipc_link *l_ptr) 405void tipc_link_reset_fragments(struct tipc_link *l_ptr)
487{ 406{
488 struct sk_buff *buf = l_ptr->defragm_buf; 407 kfree_skb(l_ptr->reasm_head);
489 struct sk_buff *next; 408 l_ptr->reasm_head = NULL;
490 409 l_ptr->reasm_tail = NULL;
491 while (buf) {
492 next = buf->next;
493 kfree_skb(buf);
494 buf = next;
495 }
496 l_ptr->defragm_buf = NULL;
497} 410}
498 411
499/** 412/**
@@ -1065,8 +978,7 @@ static int link_send_buf_fast(struct tipc_link *l_ptr, struct sk_buff *buf,
1065 */ 978 */
1066int tipc_link_send_sections_fast(struct tipc_port *sender, 979int tipc_link_send_sections_fast(struct tipc_port *sender,
1067 struct iovec const *msg_sect, 980 struct iovec const *msg_sect,
1068 const u32 num_sect, unsigned int total_len, 981 unsigned int len, u32 destaddr)
1069 u32 destaddr)
1070{ 982{
1071 struct tipc_msg *hdr = &sender->phdr; 983 struct tipc_msg *hdr = &sender->phdr;
1072 struct tipc_link *l_ptr; 984 struct tipc_link *l_ptr;
@@ -1080,8 +992,7 @@ again:
1080 * Try building message using port's max_pkt hint. 992 * Try building message using port's max_pkt hint.
1081 * (Must not hold any locks while building message.) 993 * (Must not hold any locks while building message.)
1082 */ 994 */
1083 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, 995 res = tipc_msg_build(hdr, msg_sect, len, sender->max_pkt, &buf);
1084 sender->max_pkt, &buf);
1085 /* Exit if build request was invalid */ 996 /* Exit if build request was invalid */
1086 if (unlikely(res < 0)) 997 if (unlikely(res < 0))
1087 return res; 998 return res;
@@ -1121,8 +1032,7 @@ exit:
1121 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt) 1032 if ((msg_hdr_sz(hdr) + res) <= sender->max_pkt)
1122 goto again; 1033 goto again;
1123 1034
1124 return link_send_sections_long(sender, msg_sect, 1035 return link_send_sections_long(sender, msg_sect, len,
1125 num_sect, total_len,
1126 destaddr); 1036 destaddr);
1127 } 1037 }
1128 tipc_node_unlock(node); 1038 tipc_node_unlock(node);
@@ -1133,8 +1043,8 @@ exit:
1133 if (buf) 1043 if (buf)
1134 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE); 1044 return tipc_reject_msg(buf, TIPC_ERR_NO_NODE);
1135 if (res >= 0) 1045 if (res >= 0)
1136 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect, 1046 return tipc_port_reject_sections(sender, hdr, msg_sect,
1137 total_len, TIPC_ERR_NO_NODE); 1047 len, TIPC_ERR_NO_NODE);
1138 return res; 1048 return res;
1139} 1049}
1140 1050
@@ -1154,18 +1064,17 @@ exit:
1154 */ 1064 */
1155static int link_send_sections_long(struct tipc_port *sender, 1065static int link_send_sections_long(struct tipc_port *sender,
1156 struct iovec const *msg_sect, 1066 struct iovec const *msg_sect,
1157 u32 num_sect, unsigned int total_len, 1067 unsigned int len, u32 destaddr)
1158 u32 destaddr)
1159{ 1068{
1160 struct tipc_link *l_ptr; 1069 struct tipc_link *l_ptr;
1161 struct tipc_node *node; 1070 struct tipc_node *node;
1162 struct tipc_msg *hdr = &sender->phdr; 1071 struct tipc_msg *hdr = &sender->phdr;
1163 u32 dsz = total_len; 1072 u32 dsz = len;
1164 u32 max_pkt, fragm_sz, rest; 1073 u32 max_pkt, fragm_sz, rest;
1165 struct tipc_msg fragm_hdr; 1074 struct tipc_msg fragm_hdr;
1166 struct sk_buff *buf, *buf_chain, *prev; 1075 struct sk_buff *buf, *buf_chain, *prev;
1167 u32 fragm_crs, fragm_rest, hsz, sect_rest; 1076 u32 fragm_crs, fragm_rest, hsz, sect_rest;
1168 const unchar *sect_crs; 1077 const unchar __user *sect_crs;
1169 int curr_sect; 1078 int curr_sect;
1170 u32 fragm_no; 1079 u32 fragm_no;
1171 int res = 0; 1080 int res = 0;
@@ -1207,7 +1116,7 @@ again:
1207 1116
1208 if (!sect_rest) { 1117 if (!sect_rest) {
1209 sect_rest = msg_sect[++curr_sect].iov_len; 1118 sect_rest = msg_sect[++curr_sect].iov_len;
1210 sect_crs = (const unchar *)msg_sect[curr_sect].iov_base; 1119 sect_crs = msg_sect[curr_sect].iov_base;
1211 } 1120 }
1212 1121
1213 if (sect_rest < fragm_rest) 1122 if (sect_rest < fragm_rest)
@@ -1283,8 +1192,8 @@ reject:
1283 buf = buf_chain->next; 1192 buf = buf_chain->next;
1284 kfree_skb(buf_chain); 1193 kfree_skb(buf_chain);
1285 } 1194 }
1286 return tipc_port_reject_sections(sender, hdr, msg_sect, num_sect, 1195 return tipc_port_reject_sections(sender, hdr, msg_sect,
1287 total_len, TIPC_ERR_NO_NODE); 1196 len, TIPC_ERR_NO_NODE);
1288 } 1197 }
1289 1198
1290 /* Append chain of fragments to send queue & send them */ 1199 /* Append chain of fragments to send queue & send them */
@@ -1589,18 +1498,19 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1589 int type; 1498 int type;
1590 1499
1591 head = head->next; 1500 head = head->next;
1501 buf->next = NULL;
1592 1502
1593 /* Ensure bearer is still enabled */ 1503 /* Ensure bearer is still enabled */
1594 if (unlikely(!b_ptr->active)) 1504 if (unlikely(!b_ptr->active))
1595 goto cont; 1505 goto discard;
1596 1506
1597 /* Ensure message is well-formed */ 1507 /* Ensure message is well-formed */
1598 if (unlikely(!link_recv_buf_validate(buf))) 1508 if (unlikely(!link_recv_buf_validate(buf)))
1599 goto cont; 1509 goto discard;
1600 1510
1601 /* Ensure message data is a single contiguous unit */ 1511 /* Ensure message data is a single contiguous unit */
1602 if (unlikely(skb_linearize(buf))) 1512 if (unlikely(skb_linearize(buf)))
1603 goto cont; 1513 goto discard;
1604 1514
1605 /* Handle arrival of a non-unicast link message */ 1515 /* Handle arrival of a non-unicast link message */
1606 msg = buf_msg(buf); 1516 msg = buf_msg(buf);
@@ -1616,20 +1526,18 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1616 /* Discard unicast link messages destined for another node */ 1526 /* Discard unicast link messages destined for another node */
1617 if (unlikely(!msg_short(msg) && 1527 if (unlikely(!msg_short(msg) &&
1618 (msg_destnode(msg) != tipc_own_addr))) 1528 (msg_destnode(msg) != tipc_own_addr)))
1619 goto cont; 1529 goto discard;
1620 1530
1621 /* Locate neighboring node that sent message */ 1531 /* Locate neighboring node that sent message */
1622 n_ptr = tipc_node_find(msg_prevnode(msg)); 1532 n_ptr = tipc_node_find(msg_prevnode(msg));
1623 if (unlikely(!n_ptr)) 1533 if (unlikely(!n_ptr))
1624 goto cont; 1534 goto discard;
1625 tipc_node_lock(n_ptr); 1535 tipc_node_lock(n_ptr);
1626 1536
1627 /* Locate unicast link endpoint that should handle message */ 1537 /* Locate unicast link endpoint that should handle message */
1628 l_ptr = n_ptr->links[b_ptr->identity]; 1538 l_ptr = n_ptr->links[b_ptr->identity];
1629 if (unlikely(!l_ptr)) { 1539 if (unlikely(!l_ptr))
1630 tipc_node_unlock(n_ptr); 1540 goto unlock_discard;
1631 goto cont;
1632 }
1633 1541
1634 /* Verify that communication with node is currently allowed */ 1542 /* Verify that communication with node is currently allowed */
1635 if ((n_ptr->block_setup & WAIT_PEER_DOWN) && 1543 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
@@ -1639,10 +1547,8 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1639 !msg_redundant_link(msg)) 1547 !msg_redundant_link(msg))
1640 n_ptr->block_setup &= ~WAIT_PEER_DOWN; 1548 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1641 1549
1642 if (n_ptr->block_setup) { 1550 if (n_ptr->block_setup)
1643 tipc_node_unlock(n_ptr); 1551 goto unlock_discard;
1644 goto cont;
1645 }
1646 1552
1647 /* Validate message sequence number info */ 1553 /* Validate message sequence number info */
1648 seq_no = msg_seqno(msg); 1554 seq_no = msg_seqno(msg);
@@ -1678,98 +1584,100 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1678 1584
1679 /* Now (finally!) process the incoming message */ 1585 /* Now (finally!) process the incoming message */
1680protocol_check: 1586protocol_check:
1681 if (likely(link_working_working(l_ptr))) { 1587 if (unlikely(!link_working_working(l_ptr))) {
1682 if (likely(seq_no == mod(l_ptr->next_in_no))) { 1588 if (msg_user(msg) == LINK_PROTOCOL) {
1683 l_ptr->next_in_no++; 1589 link_recv_proto_msg(l_ptr, buf);
1684 if (unlikely(l_ptr->oldest_deferred_in)) 1590 head = link_insert_deferred_queue(l_ptr, head);
1685 head = link_insert_deferred_queue(l_ptr, 1591 tipc_node_unlock(n_ptr);
1686 head); 1592 continue;
1687deliver: 1593 }
1688 if (likely(msg_isdata(msg))) { 1594
1689 tipc_node_unlock(n_ptr); 1595 /* Traffic message. Conditionally activate link */
1690 tipc_port_recv_msg(buf); 1596 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1691 continue; 1597
1692 } 1598 if (link_working_working(l_ptr)) {
1693 switch (msg_user(msg)) { 1599 /* Re-insert buffer in front of queue */
1694 int ret; 1600 buf->next = head;
1695 case MSG_BUNDLER: 1601 head = buf;
1696 l_ptr->stats.recv_bundles++;
1697 l_ptr->stats.recv_bundled +=
1698 msg_msgcnt(msg);
1699 tipc_node_unlock(n_ptr);
1700 tipc_link_recv_bundle(buf);
1701 continue;
1702 case NAME_DISTRIBUTOR:
1703 n_ptr->bclink.recv_permitted = true;
1704 tipc_node_unlock(n_ptr);
1705 tipc_named_recv(buf);
1706 continue;
1707 case BCAST_PROTOCOL:
1708 tipc_link_recv_sync(n_ptr, buf);
1709 tipc_node_unlock(n_ptr);
1710 continue;
1711 case CONN_MANAGER:
1712 tipc_node_unlock(n_ptr);
1713 tipc_port_recv_proto_msg(buf);
1714 continue;
1715 case MSG_FRAGMENTER:
1716 l_ptr->stats.recv_fragments++;
1717 ret = tipc_link_recv_fragment(
1718 &l_ptr->defragm_buf,
1719 &buf, &msg);
1720 if (ret == 1) {
1721 l_ptr->stats.recv_fragmented++;
1722 goto deliver;
1723 }
1724 if (ret == -1)
1725 l_ptr->next_in_no--;
1726 break;
1727 case CHANGEOVER_PROTOCOL:
1728 type = msg_type(msg);
1729 if (link_recv_changeover_msg(&l_ptr,
1730 &buf)) {
1731 msg = buf_msg(buf);
1732 seq_no = msg_seqno(msg);
1733 if (type == ORIGINAL_MSG)
1734 goto deliver;
1735 goto protocol_check;
1736 }
1737 break;
1738 default:
1739 kfree_skb(buf);
1740 buf = NULL;
1741 break;
1742 }
1743 tipc_node_unlock(n_ptr); 1602 tipc_node_unlock(n_ptr);
1744 tipc_net_route_msg(buf);
1745 continue; 1603 continue;
1746 } 1604 }
1605 goto unlock_discard;
1606 }
1607
1608 /* Link is now in state WORKING_WORKING */
1609 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
1747 link_handle_out_of_seq_msg(l_ptr, buf); 1610 link_handle_out_of_seq_msg(l_ptr, buf);
1748 head = link_insert_deferred_queue(l_ptr, head); 1611 head = link_insert_deferred_queue(l_ptr, head);
1749 tipc_node_unlock(n_ptr); 1612 tipc_node_unlock(n_ptr);
1750 continue; 1613 continue;
1751 } 1614 }
1752 1615 l_ptr->next_in_no++;
1753 /* Link is not in state WORKING_WORKING */ 1616 if (unlikely(l_ptr->oldest_deferred_in))
1754 if (msg_user(msg) == LINK_PROTOCOL) {
1755 link_recv_proto_msg(l_ptr, buf);
1756 head = link_insert_deferred_queue(l_ptr, head); 1617 head = link_insert_deferred_queue(l_ptr, head);
1618deliver:
1619 if (likely(msg_isdata(msg))) {
1757 tipc_node_unlock(n_ptr); 1620 tipc_node_unlock(n_ptr);
1621 tipc_port_recv_msg(buf);
1758 continue; 1622 continue;
1759 } 1623 }
1760 1624 switch (msg_user(msg)) {
1761 /* Traffic message. Conditionally activate link */ 1625 int ret;
1762 link_state_event(l_ptr, TRAFFIC_MSG_EVT); 1626 case MSG_BUNDLER:
1763 1627 l_ptr->stats.recv_bundles++;
1764 if (link_working_working(l_ptr)) { 1628 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1765 /* Re-insert buffer in front of queue */ 1629 tipc_node_unlock(n_ptr);
1766 buf->next = head; 1630 tipc_link_recv_bundle(buf);
1767 head = buf; 1631 continue;
1632 case NAME_DISTRIBUTOR:
1633 n_ptr->bclink.recv_permitted = true;
1634 tipc_node_unlock(n_ptr);
1635 tipc_named_recv(buf);
1636 continue;
1637 case BCAST_PROTOCOL:
1638 tipc_link_recv_sync(n_ptr, buf);
1639 tipc_node_unlock(n_ptr);
1640 continue;
1641 case CONN_MANAGER:
1642 tipc_node_unlock(n_ptr);
1643 tipc_port_recv_proto_msg(buf);
1644 continue;
1645 case MSG_FRAGMENTER:
1646 l_ptr->stats.recv_fragments++;
1647 ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
1648 &l_ptr->reasm_tail,
1649 &buf);
1650 if (ret == LINK_REASM_COMPLETE) {
1651 l_ptr->stats.recv_fragmented++;
1652 msg = buf_msg(buf);
1653 goto deliver;
1654 }
1655 if (ret == LINK_REASM_ERROR)
1656 tipc_link_reset(l_ptr);
1768 tipc_node_unlock(n_ptr); 1657 tipc_node_unlock(n_ptr);
1769 continue; 1658 continue;
1659 case CHANGEOVER_PROTOCOL:
1660 type = msg_type(msg);
1661 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1662 msg = buf_msg(buf);
1663 seq_no = msg_seqno(msg);
1664 if (type == ORIGINAL_MSG)
1665 goto deliver;
1666 goto protocol_check;
1667 }
1668 break;
1669 default:
1670 kfree_skb(buf);
1671 buf = NULL;
1672 break;
1770 } 1673 }
1771 tipc_node_unlock(n_ptr); 1674 tipc_node_unlock(n_ptr);
1772cont: 1675 tipc_net_route_msg(buf);
1676 continue;
1677unlock_discard:
1678
1679 tipc_node_unlock(n_ptr);
1680discard:
1773 kfree_skb(buf); 1681 kfree_skb(buf);
1774 } 1682 }
1775 read_unlock_bh(&tipc_net_lock); 1683 read_unlock_bh(&tipc_net_lock);
@@ -2432,114 +2340,48 @@ static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
2432} 2340}
2433 2341
2434/* 2342/*
2435 * A pending message being re-assembled must store certain values
2436 * to handle subsequent fragments correctly. The following functions
2437 * help storing these values in unused, available fields in the
2438 * pending message. This makes dynamic memory allocation unnecessary.
2439 */
2440static void set_long_msg_seqno(struct sk_buff *buf, u32 seqno)
2441{
2442 msg_set_seqno(buf_msg(buf), seqno);
2443}
2444
2445static u32 get_fragm_size(struct sk_buff *buf)
2446{
2447 return msg_ack(buf_msg(buf));
2448}
2449
2450static void set_fragm_size(struct sk_buff *buf, u32 sz)
2451{
2452 msg_set_ack(buf_msg(buf), sz);
2453}
2454
2455static u32 get_expected_frags(struct sk_buff *buf)
2456{
2457 return msg_bcast_ack(buf_msg(buf));
2458}
2459
2460static void set_expected_frags(struct sk_buff *buf, u32 exp)
2461{
2462 msg_set_bcast_ack(buf_msg(buf), exp);
2463}
2464
2465/*
2466 * tipc_link_recv_fragment(): Called with node lock on. Returns 2343 * tipc_link_recv_fragment(): Called with node lock on. Returns
2467 * the reassembled buffer if message is complete. 2344 * the reassembled buffer if message is complete.
2468 */ 2345 */
2469int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb, 2346int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
2470 struct tipc_msg **m) 2347 struct sk_buff **fbuf)
2471{ 2348{
2472 struct sk_buff *prev = NULL; 2349 struct sk_buff *frag = *fbuf;
2473 struct sk_buff *fbuf = *fb; 2350 struct tipc_msg *msg = buf_msg(frag);
2474 struct tipc_msg *fragm = buf_msg(fbuf); 2351 u32 fragid = msg_type(msg);
2475 struct sk_buff *pbuf = *pending; 2352 bool headstolen;
2476 u32 long_msg_seq_no = msg_long_msgno(fragm); 2353 int delta;
2477 2354
2478 *fb = NULL; 2355 skb_pull(frag, msg_hdr_sz(msg));
2479 2356 if (fragid == FIRST_FRAGMENT) {
2480 /* Is there an incomplete message waiting for this fragment? */ 2357 if (*head || skb_unclone(frag, GFP_ATOMIC))
2481 while (pbuf && ((buf_seqno(pbuf) != long_msg_seq_no) || 2358 goto out_free;
2482 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) { 2359 *head = frag;
2483 prev = pbuf; 2360 skb_frag_list_init(*head);
2484 pbuf = pbuf->next;
2485 }
2486
2487 if (!pbuf && (msg_type(fragm) == FIRST_FRAGMENT)) {
2488 struct tipc_msg *imsg = (struct tipc_msg *)msg_data(fragm);
2489 u32 msg_sz = msg_size(imsg);
2490 u32 fragm_sz = msg_data_sz(fragm);
2491 u32 exp_fragm_cnt;
2492 u32 max = TIPC_MAX_USER_MSG_SIZE + NAMED_H_SIZE;
2493
2494 if (msg_type(imsg) == TIPC_MCAST_MSG)
2495 max = TIPC_MAX_USER_MSG_SIZE + MCAST_H_SIZE;
2496 if (fragm_sz == 0 || msg_size(imsg) > max) {
2497 kfree_skb(fbuf);
2498 return 0;
2499 }
2500 exp_fragm_cnt = msg_sz / fragm_sz + !!(msg_sz % fragm_sz);
2501 pbuf = tipc_buf_acquire(msg_size(imsg));
2502 if (pbuf != NULL) {
2503 pbuf->next = *pending;
2504 *pending = pbuf;
2505 skb_copy_to_linear_data(pbuf, imsg,
2506 msg_data_sz(fragm));
2507 /* Prepare buffer for subsequent fragments. */
2508 set_long_msg_seqno(pbuf, long_msg_seq_no);
2509 set_fragm_size(pbuf, fragm_sz);
2510 set_expected_frags(pbuf, exp_fragm_cnt - 1);
2511 } else {
2512 pr_debug("Link unable to reassemble fragmented message\n");
2513 kfree_skb(fbuf);
2514 return -1;
2515 }
2516 kfree_skb(fbuf);
2517 return 0;
2518 } else if (pbuf && (msg_type(fragm) != FIRST_FRAGMENT)) {
2519 u32 dsz = msg_data_sz(fragm);
2520 u32 fsz = get_fragm_size(pbuf);
2521 u32 crs = ((msg_fragm_no(fragm) - 1) * fsz);
2522 u32 exp_frags = get_expected_frags(pbuf) - 1;
2523 skb_copy_to_linear_data_offset(pbuf, crs,
2524 msg_data(fragm), dsz);
2525 kfree_skb(fbuf);
2526
2527 /* Is message complete? */
2528 if (exp_frags == 0) {
2529 if (prev)
2530 prev->next = pbuf->next;
2531 else
2532 *pending = pbuf->next;
2533 msg_reset_reroute_cnt(buf_msg(pbuf));
2534 *fb = pbuf;
2535 *m = buf_msg(pbuf);
2536 return 1;
2537 }
2538 set_expected_frags(pbuf, exp_frags);
2539 return 0; 2361 return 0;
2362 } else if (*head &&
2363 skb_try_coalesce(*head, frag, &headstolen, &delta)) {
2364 kfree_skb_partial(frag, headstolen);
2365 } else {
2366 if (!*head)
2367 goto out_free;
2368 if (!skb_has_frag_list(*head))
2369 skb_shinfo(*head)->frag_list = frag;
2370 else
2371 (*tail)->next = frag;
2372 *tail = frag;
2373 (*head)->truesize += frag->truesize;
2374 }
2375 if (fragid == LAST_FRAGMENT) {
2376 *fbuf = *head;
2377 *tail = *head = NULL;
2378 return LINK_REASM_COMPLETE;
2540 } 2379 }
2541 kfree_skb(fbuf);
2542 return 0; 2380 return 0;
2381out_free:
2382 pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
2383 kfree_skb(*fbuf);
2384 return LINK_REASM_ERROR;
2543} 2385}
2544 2386
2545static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance) 2387static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
@@ -2585,25 +2427,21 @@ void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
2585static struct tipc_link *link_find_link(const char *name, 2427static struct tipc_link *link_find_link(const char *name,
2586 struct tipc_node **node) 2428 struct tipc_node **node)
2587{ 2429{
2588 struct tipc_link_name link_name_parts;
2589 struct tipc_bearer *b_ptr;
2590 struct tipc_link *l_ptr; 2430 struct tipc_link *l_ptr;
2431 struct tipc_node *n_ptr;
2432 int i;
2591 2433
2592 if (!link_name_validate(name, &link_name_parts)) 2434 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2593 return NULL; 2435 for (i = 0; i < MAX_BEARERS; i++) {
2594 2436 l_ptr = n_ptr->links[i];
2595 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local); 2437 if (l_ptr && !strcmp(l_ptr->name, name))
2596 if (!b_ptr) 2438 goto found;
2597 return NULL; 2439 }
2598 2440 }
2599 *node = tipc_node_find(link_name_parts.addr_peer); 2441 l_ptr = NULL;
2600 if (!*node) 2442 n_ptr = NULL;
2601 return NULL; 2443found:
2602 2444 *node = n_ptr;
2603 l_ptr = (*node)->links[b_ptr->identity];
2604 if (!l_ptr || strcmp(l_ptr->name, name))
2605 return NULL;
2606
2607 return l_ptr; 2445 return l_ptr;
2608} 2446}
2609 2447
@@ -2646,6 +2484,7 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2646 struct tipc_link *l_ptr; 2484 struct tipc_link *l_ptr;
2647 struct tipc_bearer *b_ptr; 2485 struct tipc_bearer *b_ptr;
2648 struct tipc_media *m_ptr; 2486 struct tipc_media *m_ptr;
2487 int res = 0;
2649 2488
2650 l_ptr = link_find_link(name, &node); 2489 l_ptr = link_find_link(name, &node);
2651 if (l_ptr) { 2490 if (l_ptr) {
@@ -2668,9 +2507,12 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2668 case TIPC_CMD_SET_LINK_WINDOW: 2507 case TIPC_CMD_SET_LINK_WINDOW:
2669 tipc_link_set_queue_limits(l_ptr, new_value); 2508 tipc_link_set_queue_limits(l_ptr, new_value);
2670 break; 2509 break;
2510 default:
2511 res = -EINVAL;
2512 break;
2671 } 2513 }
2672 tipc_node_unlock(node); 2514 tipc_node_unlock(node);
2673 return 0; 2515 return res;
2674 } 2516 }
2675 2517
2676 b_ptr = tipc_bearer_find(name); 2518 b_ptr = tipc_bearer_find(name);
@@ -2678,15 +2520,18 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2678 switch (cmd) { 2520 switch (cmd) {
2679 case TIPC_CMD_SET_LINK_TOL: 2521 case TIPC_CMD_SET_LINK_TOL:
2680 b_ptr->tolerance = new_value; 2522 b_ptr->tolerance = new_value;
2681 return 0; 2523 break;
2682 case TIPC_CMD_SET_LINK_PRI: 2524 case TIPC_CMD_SET_LINK_PRI:
2683 b_ptr->priority = new_value; 2525 b_ptr->priority = new_value;
2684 return 0; 2526 break;
2685 case TIPC_CMD_SET_LINK_WINDOW: 2527 case TIPC_CMD_SET_LINK_WINDOW:
2686 b_ptr->window = new_value; 2528 b_ptr->window = new_value;
2687 return 0; 2529 break;
2530 default:
2531 res = -EINVAL;
2532 break;
2688 } 2533 }
2689 return -EINVAL; 2534 return res;
2690 } 2535 }
2691 2536
2692 m_ptr = tipc_media_find(name); 2537 m_ptr = tipc_media_find(name);
@@ -2695,15 +2540,18 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2695 switch (cmd) { 2540 switch (cmd) {
2696 case TIPC_CMD_SET_LINK_TOL: 2541 case TIPC_CMD_SET_LINK_TOL:
2697 m_ptr->tolerance = new_value; 2542 m_ptr->tolerance = new_value;
2698 return 0; 2543 break;
2699 case TIPC_CMD_SET_LINK_PRI: 2544 case TIPC_CMD_SET_LINK_PRI:
2700 m_ptr->priority = new_value; 2545 m_ptr->priority = new_value;
2701 return 0; 2546 break;
2702 case TIPC_CMD_SET_LINK_WINDOW: 2547 case TIPC_CMD_SET_LINK_WINDOW:
2703 m_ptr->window = new_value; 2548 m_ptr->window = new_value;
2704 return 0; 2549 break;
2550 default:
2551 res = -EINVAL;
2552 break;
2705 } 2553 }
2706 return -EINVAL; 2554 return res;
2707} 2555}
2708 2556
2709struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space, 2557struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space,
diff --git a/net/tipc/link.h b/net/tipc/link.h
index c048ed1cbd76..8a6c1026644d 100644
--- a/net/tipc/link.h
+++ b/net/tipc/link.h
@@ -41,6 +41,12 @@
41#include "node.h" 41#include "node.h"
42 42
43/* 43/*
44 * Link reassembly status codes
45 */
46#define LINK_REASM_ERROR -1
47#define LINK_REASM_COMPLETE 1
48
49/*
44 * Out-of-range value for link sequence numbers 50 * Out-of-range value for link sequence numbers
45 */ 51 */
46#define INVALID_LINK_SEQ 0x10000 52#define INVALID_LINK_SEQ 0x10000
@@ -134,7 +140,8 @@ struct tipc_stats {
134 * @next_out: ptr to first unsent outbound message in queue 140 * @next_out: ptr to first unsent outbound message in queue
135 * @waiting_ports: linked list of ports waiting for link congestion to abate 141 * @waiting_ports: linked list of ports waiting for link congestion to abate
136 * @long_msg_seq_no: next identifier to use for outbound fragmented messages 142 * @long_msg_seq_no: next identifier to use for outbound fragmented messages
137 * @defragm_buf: list of partially reassembled inbound message fragments 143 * @reasm_head: list head of partially reassembled inbound message fragments
144 * @reasm_tail: last fragment received
138 * @stats: collects statistics regarding link activity 145 * @stats: collects statistics regarding link activity
139 */ 146 */
140struct tipc_link { 147struct tipc_link {
@@ -196,9 +203,10 @@ struct tipc_link {
196 struct sk_buff *next_out; 203 struct sk_buff *next_out;
197 struct list_head waiting_ports; 204 struct list_head waiting_ports;
198 205
199 /* Fragmentation/defragmentation */ 206 /* Fragmentation/reassembly */
200 u32 long_msg_seq_no; 207 u32 long_msg_seq_no;
201 struct sk_buff *defragm_buf; 208 struct sk_buff *reasm_head;
209 struct sk_buff *reasm_tail;
202 210
203 /* Statistics */ 211 /* Statistics */
204 struct tipc_stats stats; 212 struct tipc_stats stats;
@@ -227,13 +235,11 @@ int tipc_link_send_buf(struct tipc_link *l_ptr, struct sk_buff *buf);
227u32 tipc_link_get_max_pkt(u32 dest, u32 selector); 235u32 tipc_link_get_max_pkt(u32 dest, u32 selector);
228int tipc_link_send_sections_fast(struct tipc_port *sender, 236int tipc_link_send_sections_fast(struct tipc_port *sender,
229 struct iovec const *msg_sect, 237 struct iovec const *msg_sect,
230 const u32 num_sect, 238 unsigned int len, u32 destnode);
231 unsigned int total_len,
232 u32 destnode);
233void tipc_link_recv_bundle(struct sk_buff *buf); 239void tipc_link_recv_bundle(struct sk_buff *buf);
234int tipc_link_recv_fragment(struct sk_buff **pending, 240int tipc_link_recv_fragment(struct sk_buff **reasm_head,
235 struct sk_buff **fb, 241 struct sk_buff **reasm_tail,
236 struct tipc_msg **msg); 242 struct sk_buff **fbuf);
237void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ, int prob, 243void tipc_link_send_proto_msg(struct tipc_link *l_ptr, u32 msg_typ, int prob,
238 u32 gap, u32 tolerance, u32 priority, 244 u32 gap, u32 tolerance, u32 priority,
239 u32 acked_mtu); 245 u32 acked_mtu);
diff --git a/net/tipc/msg.c b/net/tipc/msg.c
index ced60e2fc4f7..e525f8ce1dee 100644
--- a/net/tipc/msg.c
+++ b/net/tipc/msg.c
@@ -73,13 +73,13 @@ void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize,
73 * Returns message data size or errno 73 * Returns message data size or errno
74 */ 74 */
75int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect, 75int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
76 u32 num_sect, unsigned int total_len, int max_size, 76 unsigned int len, int max_size, struct sk_buff **buf)
77 struct sk_buff **buf)
78{ 77{
79 int dsz, sz, hsz, pos, res, cnt; 78 int dsz, sz, hsz;
79 unsigned char *to;
80 80
81 dsz = total_len; 81 dsz = len;
82 pos = hsz = msg_hdr_sz(hdr); 82 hsz = msg_hdr_sz(hdr);
83 sz = hsz + dsz; 83 sz = hsz + dsz;
84 msg_set_size(hdr, sz); 84 msg_set_size(hdr, sz);
85 if (unlikely(sz > max_size)) { 85 if (unlikely(sz > max_size)) {
@@ -91,16 +91,11 @@ int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
91 if (!(*buf)) 91 if (!(*buf))
92 return -ENOMEM; 92 return -ENOMEM;
93 skb_copy_to_linear_data(*buf, hdr, hsz); 93 skb_copy_to_linear_data(*buf, hdr, hsz);
94 for (res = 1, cnt = 0; res && (cnt < num_sect); cnt++) { 94 to = (*buf)->data + hsz;
95 skb_copy_to_linear_data_offset(*buf, pos, 95 if (len && memcpy_fromiovecend(to, msg_sect, 0, dsz)) {
96 msg_sect[cnt].iov_base, 96 kfree_skb(*buf);
97 msg_sect[cnt].iov_len); 97 *buf = NULL;
98 pos += msg_sect[cnt].iov_len; 98 return -EFAULT;
99 } 99 }
100 if (likely(res)) 100 return dsz;
101 return dsz;
102
103 kfree_skb(*buf);
104 *buf = NULL;
105 return -EFAULT;
106} 101}
diff --git a/net/tipc/msg.h b/net/tipc/msg.h
index 5e4ccf5c27df..76d1269b9443 100644
--- a/net/tipc/msg.h
+++ b/net/tipc/msg.h
@@ -554,12 +554,6 @@ static inline void msg_set_last_bcast(struct tipc_msg *m, u32 n)
554 msg_set_bits(m, 4, 16, 0xffff, n); 554 msg_set_bits(m, 4, 16, 0xffff, n);
555} 555}
556 556
557
558static inline u32 msg_fragm_no(struct tipc_msg *m)
559{
560 return msg_bits(m, 4, 16, 0xffff);
561}
562
563static inline void msg_set_fragm_no(struct tipc_msg *m, u32 n) 557static inline void msg_set_fragm_no(struct tipc_msg *m, u32 n)
564{ 558{
565 msg_set_bits(m, 4, 16, 0xffff, n); 559 msg_set_bits(m, 4, 16, 0xffff, n);
@@ -576,12 +570,6 @@ static inline void msg_set_next_sent(struct tipc_msg *m, u32 n)
576 msg_set_bits(m, 4, 0, 0xffff, n); 570 msg_set_bits(m, 4, 0, 0xffff, n);
577} 571}
578 572
579
580static inline u32 msg_long_msgno(struct tipc_msg *m)
581{
582 return msg_bits(m, 4, 0, 0xffff);
583}
584
585static inline void msg_set_long_msgno(struct tipc_msg *m, u32 n) 573static inline void msg_set_long_msgno(struct tipc_msg *m, u32 n)
586{ 574{
587 msg_set_bits(m, 4, 0, 0xffff, n); 575 msg_set_bits(m, 4, 0, 0xffff, n);
@@ -722,6 +710,5 @@ u32 tipc_msg_tot_importance(struct tipc_msg *m);
722void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize, 710void tipc_msg_init(struct tipc_msg *m, u32 user, u32 type, u32 hsize,
723 u32 destnode); 711 u32 destnode);
724int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect, 712int tipc_msg_build(struct tipc_msg *hdr, struct iovec const *msg_sect,
725 u32 num_sect, unsigned int total_len, int max_size, 713 unsigned int len, int max_size, struct sk_buff **buf);
726 struct sk_buff **buf);
727#endif 714#endif
diff --git a/net/tipc/netlink.c b/net/tipc/netlink.c
index 8bcd4985d0fb..9f72a6376362 100644
--- a/net/tipc/netlink.c
+++ b/net/tipc/netlink.c
@@ -76,9 +76,11 @@ static struct genl_family tipc_genl_family = {
76 .maxattr = 0, 76 .maxattr = 0,
77}; 77};
78 78
79static struct genl_ops tipc_genl_ops = { 79static struct genl_ops tipc_genl_ops[] = {
80 .cmd = TIPC_GENL_CMD, 80 {
81 .doit = handle_cmd, 81 .cmd = TIPC_GENL_CMD,
82 .doit = handle_cmd,
83 },
82}; 84};
83 85
84static int tipc_genl_family_registered; 86static int tipc_genl_family_registered;
@@ -87,8 +89,7 @@ int tipc_netlink_start(void)
87{ 89{
88 int res; 90 int res;
89 91
90 res = genl_register_family_with_ops(&tipc_genl_family, 92 res = genl_register_family_with_ops(&tipc_genl_family, tipc_genl_ops);
91 &tipc_genl_ops, 1);
92 if (res) { 93 if (res) {
93 pr_err("Failed to register netlink interface\n"); 94 pr_err("Failed to register netlink interface\n");
94 return res; 95 return res;
diff --git a/net/tipc/node.c b/net/tipc/node.c
index 6e6c434872e8..25100c0a6fe8 100644
--- a/net/tipc/node.c
+++ b/net/tipc/node.c
@@ -298,9 +298,10 @@ static void node_lost_contact(struct tipc_node *n_ptr)
298 } 298 }
299 n_ptr->bclink.deferred_size = 0; 299 n_ptr->bclink.deferred_size = 0;
300 300
301 if (n_ptr->bclink.defragm) { 301 if (n_ptr->bclink.reasm_head) {
302 kfree_skb(n_ptr->bclink.defragm); 302 kfree_skb(n_ptr->bclink.reasm_head);
303 n_ptr->bclink.defragm = NULL; 303 n_ptr->bclink.reasm_head = NULL;
304 n_ptr->bclink.reasm_tail = NULL;
304 } 305 }
305 306
306 tipc_bclink_remove_node(n_ptr->addr); 307 tipc_bclink_remove_node(n_ptr->addr);
diff --git a/net/tipc/node.h b/net/tipc/node.h
index 3c189b35b102..e5e96c04e167 100644
--- a/net/tipc/node.h
+++ b/net/tipc/node.h
@@ -74,7 +74,8 @@
74 * @deferred_size: number of OOS b'cast messages in deferred queue 74 * @deferred_size: number of OOS b'cast messages in deferred queue
75 * @deferred_head: oldest OOS b'cast message received from node 75 * @deferred_head: oldest OOS b'cast message received from node
76 * @deferred_tail: newest OOS b'cast message received from node 76 * @deferred_tail: newest OOS b'cast message received from node
77 * @defragm: list of partially reassembled b'cast message fragments from node 77 * @reasm_head: broadcast reassembly queue head from node
78 * @reasm_tail: last broadcast fragment received from node
78 * @recv_permitted: true if node is allowed to receive b'cast messages 79 * @recv_permitted: true if node is allowed to receive b'cast messages
79 */ 80 */
80struct tipc_node { 81struct tipc_node {
@@ -98,7 +99,8 @@ struct tipc_node {
98 u32 deferred_size; 99 u32 deferred_size;
99 struct sk_buff *deferred_head; 100 struct sk_buff *deferred_head;
100 struct sk_buff *deferred_tail; 101 struct sk_buff *deferred_tail;
101 struct sk_buff *defragm; 102 struct sk_buff *reasm_head;
103 struct sk_buff *reasm_tail;
102 bool recv_permitted; 104 bool recv_permitted;
103 } bclink; 105 } bclink;
104}; 106};
diff --git a/net/tipc/port.c b/net/tipc/port.c
index b3ed2fcab4fb..d43f3182b1d4 100644
--- a/net/tipc/port.c
+++ b/net/tipc/port.c
@@ -90,8 +90,7 @@ int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
90 * tipc_multicast - send a multicast message to local and remote destinations 90 * tipc_multicast - send a multicast message to local and remote destinations
91 */ 91 */
92int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, 92int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
93 u32 num_sect, struct iovec const *msg_sect, 93 struct iovec const *msg_sect, unsigned int len)
94 unsigned int total_len)
95{ 94{
96 struct tipc_msg *hdr; 95 struct tipc_msg *hdr;
97 struct sk_buff *buf; 96 struct sk_buff *buf;
@@ -114,8 +113,7 @@ int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
114 msg_set_namelower(hdr, seq->lower); 113 msg_set_namelower(hdr, seq->lower);
115 msg_set_nameupper(hdr, seq->upper); 114 msg_set_nameupper(hdr, seq->upper);
116 msg_set_hdr_sz(hdr, MCAST_H_SIZE); 115 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
117 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE, 116 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
118 &buf);
119 if (unlikely(!buf)) 117 if (unlikely(!buf))
120 return res; 118 return res;
121 119
@@ -253,18 +251,15 @@ struct tipc_port *tipc_createport(struct sock *sk,
253 return p_ptr; 251 return p_ptr;
254} 252}
255 253
256int tipc_deleteport(u32 ref) 254int tipc_deleteport(struct tipc_port *p_ptr)
257{ 255{
258 struct tipc_port *p_ptr;
259 struct sk_buff *buf = NULL; 256 struct sk_buff *buf = NULL;
260 257
261 tipc_withdraw(ref, 0, NULL); 258 tipc_withdraw(p_ptr, 0, NULL);
262 p_ptr = tipc_port_lock(ref);
263 if (!p_ptr)
264 return -EINVAL;
265 259
266 tipc_ref_discard(ref); 260 spin_lock_bh(p_ptr->lock);
267 tipc_port_unlock(p_ptr); 261 tipc_ref_discard(p_ptr->ref);
262 spin_unlock_bh(p_ptr->lock);
268 263
269 k_cancel_timer(&p_ptr->timer); 264 k_cancel_timer(&p_ptr->timer);
270 if (p_ptr->connected) { 265 if (p_ptr->connected) {
@@ -436,14 +431,13 @@ exit:
436} 431}
437 432
438int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr, 433int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
439 struct iovec const *msg_sect, u32 num_sect, 434 struct iovec const *msg_sect, unsigned int len,
440 unsigned int total_len, int err) 435 int err)
441{ 436{
442 struct sk_buff *buf; 437 struct sk_buff *buf;
443 int res; 438 int res;
444 439
445 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE, 440 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
446 &buf);
447 if (!buf) 441 if (!buf)
448 return res; 442 return res;
449 443
@@ -707,47 +701,36 @@ int tipc_set_portimportance(u32 ref, unsigned int imp)
707} 701}
708 702
709 703
710int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq) 704int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
705 struct tipc_name_seq const *seq)
711{ 706{
712 struct tipc_port *p_ptr;
713 struct publication *publ; 707 struct publication *publ;
714 u32 key; 708 u32 key;
715 int res = -EINVAL;
716 709
717 p_ptr = tipc_port_lock(ref); 710 if (p_ptr->connected)
718 if (!p_ptr)
719 return -EINVAL; 711 return -EINVAL;
712 key = p_ptr->ref + p_ptr->pub_count + 1;
713 if (key == p_ptr->ref)
714 return -EADDRINUSE;
720 715
721 if (p_ptr->connected)
722 goto exit;
723 key = ref + p_ptr->pub_count + 1;
724 if (key == ref) {
725 res = -EADDRINUSE;
726 goto exit;
727 }
728 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper, 716 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
729 scope, p_ptr->ref, key); 717 scope, p_ptr->ref, key);
730 if (publ) { 718 if (publ) {
731 list_add(&publ->pport_list, &p_ptr->publications); 719 list_add(&publ->pport_list, &p_ptr->publications);
732 p_ptr->pub_count++; 720 p_ptr->pub_count++;
733 p_ptr->published = 1; 721 p_ptr->published = 1;
734 res = 0; 722 return 0;
735 } 723 }
736exit: 724 return -EINVAL;
737 tipc_port_unlock(p_ptr);
738 return res;
739} 725}
740 726
741int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq) 727int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
728 struct tipc_name_seq const *seq)
742{ 729{
743 struct tipc_port *p_ptr;
744 struct publication *publ; 730 struct publication *publ;
745 struct publication *tpubl; 731 struct publication *tpubl;
746 int res = -EINVAL; 732 int res = -EINVAL;
747 733
748 p_ptr = tipc_port_lock(ref);
749 if (!p_ptr)
750 return -EINVAL;
751 if (!seq) { 734 if (!seq) {
752 list_for_each_entry_safe(publ, tpubl, 735 list_for_each_entry_safe(publ, tpubl,
753 &p_ptr->publications, pport_list) { 736 &p_ptr->publications, pport_list) {
@@ -774,7 +757,6 @@ int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
774 } 757 }
775 if (list_empty(&p_ptr->publications)) 758 if (list_empty(&p_ptr->publications))
776 p_ptr->published = 0; 759 p_ptr->published = 0;
777 tipc_port_unlock(p_ptr);
778 return res; 760 return res;
779} 761}
780 762
@@ -918,15 +900,14 @@ int tipc_port_recv_msg(struct sk_buff *buf)
918 * tipc_port_recv_sections(): Concatenate and deliver sectioned 900 * tipc_port_recv_sections(): Concatenate and deliver sectioned
919 * message for this node. 901 * message for this node.
920 */ 902 */
921static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect, 903static int tipc_port_recv_sections(struct tipc_port *sender,
922 struct iovec const *msg_sect, 904 struct iovec const *msg_sect,
923 unsigned int total_len) 905 unsigned int len)
924{ 906{
925 struct sk_buff *buf; 907 struct sk_buff *buf;
926 int res; 908 int res;
927 909
928 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len, 910 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
929 MAX_MSG_SIZE, &buf);
930 if (likely(buf)) 911 if (likely(buf))
931 tipc_port_recv_msg(buf); 912 tipc_port_recv_msg(buf);
932 return res; 913 return res;
@@ -935,8 +916,7 @@ static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_se
935/** 916/**
936 * tipc_send - send message sections on connection 917 * tipc_send - send message sections on connection
937 */ 918 */
938int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect, 919int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
939 unsigned int total_len)
940{ 920{
941 struct tipc_port *p_ptr; 921 struct tipc_port *p_ptr;
942 u32 destnode; 922 u32 destnode;
@@ -950,11 +930,10 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
950 if (!tipc_port_congested(p_ptr)) { 930 if (!tipc_port_congested(p_ptr)) {
951 destnode = port_peernode(p_ptr); 931 destnode = port_peernode(p_ptr);
952 if (likely(!in_own_node(destnode))) 932 if (likely(!in_own_node(destnode)))
953 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, 933 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
954 total_len, destnode); 934 len, destnode);
955 else 935 else
956 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect, 936 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
957 total_len);
958 937
959 if (likely(res != -ELINKCONG)) { 938 if (likely(res != -ELINKCONG)) {
960 p_ptr->congested = 0; 939 p_ptr->congested = 0;
@@ -965,7 +944,7 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
965 } 944 }
966 if (port_unreliable(p_ptr)) { 945 if (port_unreliable(p_ptr)) {
967 p_ptr->congested = 0; 946 p_ptr->congested = 0;
968 return total_len; 947 return len;
969 } 948 }
970 return -ELINKCONG; 949 return -ELINKCONG;
971} 950}
@@ -974,8 +953,7 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
974 * tipc_send2name - send message sections to port name 953 * tipc_send2name - send message sections to port name
975 */ 954 */
976int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain, 955int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
977 unsigned int num_sect, struct iovec const *msg_sect, 956 struct iovec const *msg_sect, unsigned int len)
978 unsigned int total_len)
979{ 957{
980 struct tipc_port *p_ptr; 958 struct tipc_port *p_ptr;
981 struct tipc_msg *msg; 959 struct tipc_msg *msg;
@@ -999,36 +977,32 @@ int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
999 977
1000 if (likely(destport || destnode)) { 978 if (likely(destport || destnode)) {
1001 if (likely(in_own_node(destnode))) 979 if (likely(in_own_node(destnode)))
1002 res = tipc_port_recv_sections(p_ptr, num_sect, 980 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
1003 msg_sect, total_len);
1004 else if (tipc_own_addr) 981 else if (tipc_own_addr)
1005 res = tipc_link_send_sections_fast(p_ptr, msg_sect, 982 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1006 num_sect, total_len, 983 len, destnode);
1007 destnode);
1008 else 984 else
1009 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, 985 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
1010 num_sect, total_len, 986 len, TIPC_ERR_NO_NODE);
1011 TIPC_ERR_NO_NODE);
1012 if (likely(res != -ELINKCONG)) { 987 if (likely(res != -ELINKCONG)) {
1013 if (res > 0) 988 if (res > 0)
1014 p_ptr->sent++; 989 p_ptr->sent++;
1015 return res; 990 return res;
1016 } 991 }
1017 if (port_unreliable(p_ptr)) { 992 if (port_unreliable(p_ptr)) {
1018 return total_len; 993 return len;
1019 } 994 }
1020 return -ELINKCONG; 995 return -ELINKCONG;
1021 } 996 }
1022 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect, 997 return tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1023 total_len, TIPC_ERR_NO_NAME); 998 TIPC_ERR_NO_NAME);
1024} 999}
1025 1000
1026/** 1001/**
1027 * tipc_send2port - send message sections to port identity 1002 * tipc_send2port - send message sections to port identity
1028 */ 1003 */
1029int tipc_send2port(u32 ref, struct tipc_portid const *dest, 1004int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1030 unsigned int num_sect, struct iovec const *msg_sect, 1005 struct iovec const *msg_sect, unsigned int len)
1031 unsigned int total_len)
1032{ 1006{
1033 struct tipc_port *p_ptr; 1007 struct tipc_port *p_ptr;
1034 struct tipc_msg *msg; 1008 struct tipc_msg *msg;
@@ -1046,21 +1020,20 @@ int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1046 msg_set_hdr_sz(msg, BASIC_H_SIZE); 1020 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1047 1021
1048 if (in_own_node(dest->node)) 1022 if (in_own_node(dest->node))
1049 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect, 1023 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
1050 total_len);
1051 else if (tipc_own_addr) 1024 else if (tipc_own_addr)
1052 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, 1025 res = tipc_link_send_sections_fast(p_ptr, msg_sect, len,
1053 total_len, dest->node); 1026 dest->node);
1054 else 1027 else
1055 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect, 1028 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1056 total_len, TIPC_ERR_NO_NODE); 1029 TIPC_ERR_NO_NODE);
1057 if (likely(res != -ELINKCONG)) { 1030 if (likely(res != -ELINKCONG)) {
1058 if (res > 0) 1031 if (res > 0)
1059 p_ptr->sent++; 1032 p_ptr->sent++;
1060 return res; 1033 return res;
1061 } 1034 }
1062 if (port_unreliable(p_ptr)) { 1035 if (port_unreliable(p_ptr)) {
1063 return total_len; 1036 return len;
1064 } 1037 }
1065 return -ELINKCONG; 1038 return -ELINKCONG;
1066} 1039}
diff --git a/net/tipc/port.h b/net/tipc/port.h
index 5a7026b9c345..34f12bd4074e 100644
--- a/net/tipc/port.h
+++ b/net/tipc/port.h
@@ -116,7 +116,7 @@ int tipc_reject_msg(struct sk_buff *buf, u32 err);
116 116
117void tipc_acknowledge(u32 port_ref, u32 ack); 117void tipc_acknowledge(u32 port_ref, u32 ack);
118 118
119int tipc_deleteport(u32 portref); 119int tipc_deleteport(struct tipc_port *p_ptr);
120 120
121int tipc_portimportance(u32 portref, unsigned int *importance); 121int tipc_portimportance(u32 portref, unsigned int *importance);
122int tipc_set_portimportance(u32 portref, unsigned int importance); 122int tipc_set_portimportance(u32 portref, unsigned int importance);
@@ -127,9 +127,9 @@ int tipc_set_portunreliable(u32 portref, unsigned int isunreliable);
127int tipc_portunreturnable(u32 portref, unsigned int *isunreturnable); 127int tipc_portunreturnable(u32 portref, unsigned int *isunreturnable);
128int tipc_set_portunreturnable(u32 portref, unsigned int isunreturnable); 128int tipc_set_portunreturnable(u32 portref, unsigned int isunreturnable);
129 129
130int tipc_publish(u32 portref, unsigned int scope, 130int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
131 struct tipc_name_seq const *name_seq); 131 struct tipc_name_seq const *name_seq);
132int tipc_withdraw(u32 portref, unsigned int scope, 132int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
133 struct tipc_name_seq const *name_seq); 133 struct tipc_name_seq const *name_seq);
134 134
135int tipc_connect(u32 portref, struct tipc_portid const *port); 135int tipc_connect(u32 portref, struct tipc_portid const *port);
@@ -151,24 +151,20 @@ int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg);
151 * TIPC messaging routines 151 * TIPC messaging routines
152 */ 152 */
153int tipc_port_recv_msg(struct sk_buff *buf); 153int tipc_port_recv_msg(struct sk_buff *buf);
154int tipc_send(u32 portref, unsigned int num_sect, struct iovec const *msg_sect, 154int tipc_send(u32 portref, struct iovec const *msg_sect, unsigned int len);
155 unsigned int total_len);
156 155
157int tipc_send2name(u32 portref, struct tipc_name const *name, u32 domain, 156int tipc_send2name(u32 portref, struct tipc_name const *name, u32 domain,
158 unsigned int num_sect, struct iovec const *msg_sect, 157 struct iovec const *msg_sect, unsigned int len);
159 unsigned int total_len);
160 158
161int tipc_send2port(u32 portref, struct tipc_portid const *dest, 159int tipc_send2port(u32 portref, struct tipc_portid const *dest,
162 unsigned int num_sect, struct iovec const *msg_sect, 160 struct iovec const *msg_sect, unsigned int len);
163 unsigned int total_len);
164 161
165int tipc_multicast(u32 portref, struct tipc_name_seq const *seq, 162int tipc_multicast(u32 portref, struct tipc_name_seq const *seq,
166 unsigned int section_count, struct iovec const *msg, 163 struct iovec const *msg, unsigned int len);
167 unsigned int total_len);
168 164
169int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr, 165int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
170 struct iovec const *msg_sect, u32 num_sect, 166 struct iovec const *msg_sect, unsigned int len,
171 unsigned int total_len, int err); 167 int err);
172struct sk_buff *tipc_port_get_ports(void); 168struct sk_buff *tipc_port_get_ports(void);
173void tipc_port_recv_proto_msg(struct sk_buff *buf); 169void tipc_port_recv_proto_msg(struct sk_buff *buf);
174void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp); 170void tipc_port_recv_mcast(struct sk_buff *buf, struct tipc_port_list *dp);
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index 6cc7ddd2fb7c..e741416d1d24 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -338,7 +338,7 @@ static int release(struct socket *sock)
338 buf = __skb_dequeue(&sk->sk_receive_queue); 338 buf = __skb_dequeue(&sk->sk_receive_queue);
339 if (buf == NULL) 339 if (buf == NULL)
340 break; 340 break;
341 if (TIPC_SKB_CB(buf)->handle != 0) 341 if (TIPC_SKB_CB(buf)->handle != NULL)
342 kfree_skb(buf); 342 kfree_skb(buf);
343 else { 343 else {
344 if ((sock->state == SS_CONNECTING) || 344 if ((sock->state == SS_CONNECTING) ||
@@ -354,7 +354,7 @@ static int release(struct socket *sock)
354 * Delete TIPC port; this ensures no more messages are queued 354 * Delete TIPC port; this ensures no more messages are queued
355 * (also disconnects an active connection & sends a 'FIN-' to peer) 355 * (also disconnects an active connection & sends a 'FIN-' to peer)
356 */ 356 */
357 res = tipc_deleteport(tport->ref); 357 res = tipc_deleteport(tport);
358 358
359 /* Discard any remaining (connection-based) messages in receive queue */ 359 /* Discard any remaining (connection-based) messages in receive queue */
360 __skb_queue_purge(&sk->sk_receive_queue); 360 __skb_queue_purge(&sk->sk_receive_queue);
@@ -386,30 +386,46 @@ static int release(struct socket *sock)
386 */ 386 */
387static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len) 387static int bind(struct socket *sock, struct sockaddr *uaddr, int uaddr_len)
388{ 388{
389 struct sock *sk = sock->sk;
389 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr; 390 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
390 u32 portref = tipc_sk_port(sock->sk)->ref; 391 struct tipc_port *tport = tipc_sk_port(sock->sk);
392 int res = -EINVAL;
391 393
392 if (unlikely(!uaddr_len)) 394 lock_sock(sk);
393 return tipc_withdraw(portref, 0, NULL); 395 if (unlikely(!uaddr_len)) {
396 res = tipc_withdraw(tport, 0, NULL);
397 goto exit;
398 }
394 399
395 if (uaddr_len < sizeof(struct sockaddr_tipc)) 400 if (uaddr_len < sizeof(struct sockaddr_tipc)) {
396 return -EINVAL; 401 res = -EINVAL;
397 if (addr->family != AF_TIPC) 402 goto exit;
398 return -EAFNOSUPPORT; 403 }
404 if (addr->family != AF_TIPC) {
405 res = -EAFNOSUPPORT;
406 goto exit;
407 }
399 408
400 if (addr->addrtype == TIPC_ADDR_NAME) 409 if (addr->addrtype == TIPC_ADDR_NAME)
401 addr->addr.nameseq.upper = addr->addr.nameseq.lower; 410 addr->addr.nameseq.upper = addr->addr.nameseq.lower;
402 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) 411 else if (addr->addrtype != TIPC_ADDR_NAMESEQ) {
403 return -EAFNOSUPPORT; 412 res = -EAFNOSUPPORT;
413 goto exit;
414 }
404 415
405 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) && 416 if ((addr->addr.nameseq.type < TIPC_RESERVED_TYPES) &&
406 (addr->addr.nameseq.type != TIPC_TOP_SRV) && 417 (addr->addr.nameseq.type != TIPC_TOP_SRV) &&
407 (addr->addr.nameseq.type != TIPC_CFG_SRV)) 418 (addr->addr.nameseq.type != TIPC_CFG_SRV)) {
408 return -EACCES; 419 res = -EACCES;
420 goto exit;
421 }
409 422
410 return (addr->scope > 0) ? 423 res = (addr->scope > 0) ?
411 tipc_publish(portref, addr->scope, &addr->addr.nameseq) : 424 tipc_publish(tport, addr->scope, &addr->addr.nameseq) :
412 tipc_withdraw(portref, -addr->scope, &addr->addr.nameseq); 425 tipc_withdraw(tport, -addr->scope, &addr->addr.nameseq);
426exit:
427 release_sock(sk);
428 return res;
413} 429}
414 430
415/** 431/**
@@ -622,13 +638,11 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
622 res = tipc_send2name(tport->ref, 638 res = tipc_send2name(tport->ref,
623 &dest->addr.name.name, 639 &dest->addr.name.name,
624 dest->addr.name.domain, 640 dest->addr.name.domain,
625 m->msg_iovlen,
626 m->msg_iov, 641 m->msg_iov,
627 total_len); 642 total_len);
628 } else if (dest->addrtype == TIPC_ADDR_ID) { 643 } else if (dest->addrtype == TIPC_ADDR_ID) {
629 res = tipc_send2port(tport->ref, 644 res = tipc_send2port(tport->ref,
630 &dest->addr.id, 645 &dest->addr.id,
631 m->msg_iovlen,
632 m->msg_iov, 646 m->msg_iov,
633 total_len); 647 total_len);
634 } else if (dest->addrtype == TIPC_ADDR_MCAST) { 648 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
@@ -641,7 +655,6 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
641 break; 655 break;
642 res = tipc_multicast(tport->ref, 656 res = tipc_multicast(tport->ref,
643 &dest->addr.nameseq, 657 &dest->addr.nameseq,
644 m->msg_iovlen,
645 m->msg_iov, 658 m->msg_iov,
646 total_len); 659 total_len);
647 } 660 }
@@ -707,8 +720,7 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
707 break; 720 break;
708 } 721 }
709 722
710 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov, 723 res = tipc_send(tport->ref, m->msg_iov, total_len);
711 total_len);
712 if (likely(res != -ELINKCONG)) 724 if (likely(res != -ELINKCONG))
713 break; 725 break;
714 if (timeout_val <= 0L) { 726 if (timeout_val <= 0L) {
@@ -984,9 +996,6 @@ static int recv_msg(struct kiocb *iocb, struct socket *sock,
984 goto exit; 996 goto exit;
985 } 997 }
986 998
987 /* will be updated in set_orig_addr() if needed */
988 m->msg_namelen = 0;
989
990 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 999 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
991restart: 1000restart:
992 1001
@@ -1095,9 +1104,6 @@ static int recv_stream(struct kiocb *iocb, struct socket *sock,
1095 goto exit; 1104 goto exit;
1096 } 1105 }
1097 1106
1098 /* will be updated in set_orig_addr() if needed */
1099 m->msg_namelen = 0;
1100
1101 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len); 1107 target = sock_rcvlowat(sk, flags & MSG_WAITALL, buf_len);
1102 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT); 1108 timeout = sock_rcvtimeo(sk, flags & MSG_DONTWAIT);
1103 1109
@@ -1368,7 +1374,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1368 return TIPC_ERR_OVERLOAD; 1374 return TIPC_ERR_OVERLOAD;
1369 1375
1370 /* Enqueue message */ 1376 /* Enqueue message */
1371 TIPC_SKB_CB(buf)->handle = 0; 1377 TIPC_SKB_CB(buf)->handle = NULL;
1372 __skb_queue_tail(&sk->sk_receive_queue, buf); 1378 __skb_queue_tail(&sk->sk_receive_queue, buf);
1373 skb_set_owner_r(buf, sk); 1379 skb_set_owner_r(buf, sk);
1374 1380
@@ -1691,7 +1697,7 @@ restart:
1691 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ 1697 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1692 buf = __skb_dequeue(&sk->sk_receive_queue); 1698 buf = __skb_dequeue(&sk->sk_receive_queue);
1693 if (buf) { 1699 if (buf) {
1694 if (TIPC_SKB_CB(buf)->handle != 0) { 1700 if (TIPC_SKB_CB(buf)->handle != NULL) {
1695 kfree_skb(buf); 1701 kfree_skb(buf);
1696 goto restart; 1702 goto restart;
1697 } 1703 }
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index c1f403bed683..a427623ee574 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -530,13 +530,17 @@ static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *,
530static int unix_seqpacket_recvmsg(struct kiocb *, struct socket *, 530static int unix_seqpacket_recvmsg(struct kiocb *, struct socket *,
531 struct msghdr *, size_t, int); 531 struct msghdr *, size_t, int);
532 532
533static void unix_set_peek_off(struct sock *sk, int val) 533static int unix_set_peek_off(struct sock *sk, int val)
534{ 534{
535 struct unix_sock *u = unix_sk(sk); 535 struct unix_sock *u = unix_sk(sk);
536 536
537 mutex_lock(&u->readlock); 537 if (mutex_lock_interruptible(&u->readlock))
538 return -EINTR;
539
538 sk->sk_peek_off = val; 540 sk->sk_peek_off = val;
539 mutex_unlock(&u->readlock); 541 mutex_unlock(&u->readlock);
542
543 return 0;
540} 544}
541 545
542 546
@@ -714,7 +718,9 @@ static int unix_autobind(struct socket *sock)
714 int err; 718 int err;
715 unsigned int retries = 0; 719 unsigned int retries = 0;
716 720
717 mutex_lock(&u->readlock); 721 err = mutex_lock_interruptible(&u->readlock);
722 if (err)
723 return err;
718 724
719 err = 0; 725 err = 0;
720 if (u->addr) 726 if (u->addr)
@@ -873,7 +879,9 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
873 goto out; 879 goto out;
874 addr_len = err; 880 addr_len = err;
875 881
876 mutex_lock(&u->readlock); 882 err = mutex_lock_interruptible(&u->readlock);
883 if (err)
884 goto out;
877 885
878 err = -EINVAL; 886 err = -EINVAL;
879 if (u->addr) 887 if (u->addr)
@@ -1754,7 +1762,6 @@ static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1754{ 1762{
1755 struct unix_sock *u = unix_sk(sk); 1763 struct unix_sock *u = unix_sk(sk);
1756 1764
1757 msg->msg_namelen = 0;
1758 if (u->addr) { 1765 if (u->addr) {
1759 msg->msg_namelen = u->addr->len; 1766 msg->msg_namelen = u->addr->len;
1760 memcpy(msg->msg_name, u->addr->name, u->addr->len); 1767 memcpy(msg->msg_name, u->addr->name, u->addr->len);
@@ -1778,8 +1785,6 @@ static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock,
1778 if (flags&MSG_OOB) 1785 if (flags&MSG_OOB)
1779 goto out; 1786 goto out;
1780 1787
1781 msg->msg_namelen = 0;
1782
1783 err = mutex_lock_interruptible(&u->readlock); 1788 err = mutex_lock_interruptible(&u->readlock);
1784 if (err) { 1789 if (err) {
1785 err = sock_intr_errno(sock_rcvtimeo(sk, noblock)); 1790 err = sock_intr_errno(sock_rcvtimeo(sk, noblock));
@@ -1924,8 +1929,6 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
1924 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size); 1929 target = sock_rcvlowat(sk, flags&MSG_WAITALL, size);
1925 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT); 1930 timeo = sock_rcvtimeo(sk, flags&MSG_DONTWAIT);
1926 1931
1927 msg->msg_namelen = 0;
1928
1929 /* Lock the socket to prevent queue disordering 1932 /* Lock the socket to prevent queue disordering
1930 * while sleeps in memcpy_tomsg 1933 * while sleeps in memcpy_tomsg
1931 */ 1934 */
diff --git a/net/vmw_vsock/Kconfig b/net/vmw_vsock/Kconfig
index b5fa7e40cdcb..14810abedc2e 100644
--- a/net/vmw_vsock/Kconfig
+++ b/net/vmw_vsock/Kconfig
@@ -6,7 +6,7 @@ config VSOCKETS
6 tristate "Virtual Socket protocol" 6 tristate "Virtual Socket protocol"
7 help 7 help
8 Virtual Socket Protocol is a socket protocol similar to TCP/IP 8 Virtual Socket Protocol is a socket protocol similar to TCP/IP
9 allowing comunication between Virtual Machines and hypervisor 9 allowing communication between Virtual Machines and hypervisor
10 or host. 10 or host.
11 11
12 You should also select one or more hypervisor-specific transports 12 You should also select one or more hypervisor-specific transports
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 545c08b8a1d4..5adfd94c5b85 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -1662,8 +1662,6 @@ vsock_stream_recvmsg(struct kiocb *kiocb,
1662 vsk = vsock_sk(sk); 1662 vsk = vsock_sk(sk);
1663 err = 0; 1663 err = 0;
1664 1664
1665 msg->msg_namelen = 0;
1666
1667 lock_sock(sk); 1665 lock_sock(sk);
1668 1666
1669 if (sk->sk_state != SS_CONNECTED) { 1667 if (sk->sk_state != SS_CONNECTED) {
diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c
index 9d6986634e0b..687360da62d9 100644
--- a/net/vmw_vsock/vmci_transport.c
+++ b/net/vmw_vsock/vmci_transport.c
@@ -1746,8 +1746,6 @@ static int vmci_transport_dgram_dequeue(struct kiocb *kiocb,
1746 if (flags & MSG_OOB || flags & MSG_ERRQUEUE) 1746 if (flags & MSG_OOB || flags & MSG_ERRQUEUE)
1747 return -EOPNOTSUPP; 1747 return -EOPNOTSUPP;
1748 1748
1749 msg->msg_namelen = 0;
1750
1751 /* Retrieve the head sk_buff from the socket's receive queue. */ 1749 /* Retrieve the head sk_buff from the socket's receive queue. */
1752 err = 0; 1750 err = 0;
1753 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err); 1751 skb = skb_recv_datagram(&vsk->sk, flags, noblock, &err);
diff --git a/net/wimax/op-msg.c b/net/wimax/op-msg.c
index 0694d62e4dbc..c278b3356f75 100644
--- a/net/wimax/op-msg.c
+++ b/net/wimax/op-msg.c
@@ -279,7 +279,7 @@ int wimax_msg_send(struct wimax_dev *wimax_dev, struct sk_buff *skb)
279 279
280 d_printf(1, dev, "CTX: wimax msg, %zu bytes\n", size); 280 d_printf(1, dev, "CTX: wimax msg, %zu bytes\n", size);
281 d_dump(2, dev, msg, size); 281 d_dump(2, dev, msg, size);
282 genlmsg_multicast(skb, 0, wimax_gnl_mcg.id, GFP_KERNEL); 282 genlmsg_multicast(&wimax_gnl_family, skb, 0, 0, GFP_KERNEL);
283 d_printf(1, dev, "CTX: genl multicast done\n"); 283 d_printf(1, dev, "CTX: genl multicast done\n");
284 return 0; 284 return 0;
285} 285}
@@ -321,17 +321,6 @@ int wimax_msg(struct wimax_dev *wimax_dev, const char *pipe_name,
321} 321}
322EXPORT_SYMBOL_GPL(wimax_msg); 322EXPORT_SYMBOL_GPL(wimax_msg);
323 323
324
325static const struct nla_policy wimax_gnl_msg_policy[WIMAX_GNL_ATTR_MAX + 1] = {
326 [WIMAX_GNL_MSG_IFIDX] = {
327 .type = NLA_U32,
328 },
329 [WIMAX_GNL_MSG_DATA] = {
330 .type = NLA_UNSPEC, /* libnl doesn't grok BINARY yet */
331 },
332};
333
334
335/* 324/*
336 * Relays a message from user space to the driver 325 * Relays a message from user space to the driver
337 * 326 *
@@ -340,7 +329,6 @@ static const struct nla_policy wimax_gnl_msg_policy[WIMAX_GNL_ATTR_MAX + 1] = {
340 * 329 *
341 * This call will block while handling/relaying the message. 330 * This call will block while handling/relaying the message.
342 */ 331 */
343static
344int wimax_gnl_doit_msg_from_user(struct sk_buff *skb, struct genl_info *info) 332int wimax_gnl_doit_msg_from_user(struct sk_buff *skb, struct genl_info *info)
345{ 333{
346 int result, ifindex; 334 int result, ifindex;
@@ -418,16 +406,3 @@ error_no_wimax_dev:
418 return result; 406 return result;
419} 407}
420 408
421
422/*
423 * Generic Netlink glue
424 */
425
426struct genl_ops wimax_gnl_msg_from_user = {
427 .cmd = WIMAX_GNL_OP_MSG_FROM_USER,
428 .flags = GENL_ADMIN_PERM,
429 .policy = wimax_gnl_msg_policy,
430 .doit = wimax_gnl_doit_msg_from_user,
431 .dumpit = NULL,
432};
433
diff --git a/net/wimax/op-reset.c b/net/wimax/op-reset.c
index 7ceffe39d70e..eb4580784d9d 100644
--- a/net/wimax/op-reset.c
+++ b/net/wimax/op-reset.c
@@ -92,13 +92,6 @@ int wimax_reset(struct wimax_dev *wimax_dev)
92EXPORT_SYMBOL(wimax_reset); 92EXPORT_SYMBOL(wimax_reset);
93 93
94 94
95static const struct nla_policy wimax_gnl_reset_policy[WIMAX_GNL_ATTR_MAX + 1] = {
96 [WIMAX_GNL_RESET_IFIDX] = {
97 .type = NLA_U32,
98 },
99};
100
101
102/* 95/*
103 * Exporting to user space over generic netlink 96 * Exporting to user space over generic netlink
104 * 97 *
@@ -106,7 +99,6 @@ static const struct nla_policy wimax_gnl_reset_policy[WIMAX_GNL_ATTR_MAX + 1] =
106 * 99 *
107 * No attributes. 100 * No attributes.
108 */ 101 */
109static
110int wimax_gnl_doit_reset(struct sk_buff *skb, struct genl_info *info) 102int wimax_gnl_doit_reset(struct sk_buff *skb, struct genl_info *info)
111{ 103{
112 int result, ifindex; 104 int result, ifindex;
@@ -130,12 +122,3 @@ error_no_wimax_dev:
130 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result); 122 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
131 return result; 123 return result;
132} 124}
133
134
135struct genl_ops wimax_gnl_reset = {
136 .cmd = WIMAX_GNL_OP_RESET,
137 .flags = GENL_ADMIN_PERM,
138 .policy = wimax_gnl_reset_policy,
139 .doit = wimax_gnl_doit_reset,
140 .dumpit = NULL,
141};
diff --git a/net/wimax/op-rfkill.c b/net/wimax/op-rfkill.c
index 7ab60babdd22..403078d670a9 100644
--- a/net/wimax/op-rfkill.c
+++ b/net/wimax/op-rfkill.c
@@ -411,17 +411,6 @@ void wimax_rfkill_rm(struct wimax_dev *wimax_dev)
411 * just query). 411 * just query).
412 */ 412 */
413 413
414static const struct nla_policy wimax_gnl_rfkill_policy[WIMAX_GNL_ATTR_MAX + 1] = {
415 [WIMAX_GNL_RFKILL_IFIDX] = {
416 .type = NLA_U32,
417 },
418 [WIMAX_GNL_RFKILL_STATE] = {
419 .type = NLA_U32 /* enum wimax_rf_state */
420 },
421};
422
423
424static
425int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info) 414int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info)
426{ 415{
427 int result, ifindex; 416 int result, ifindex;
@@ -457,13 +446,3 @@ error_no_wimax_dev:
457 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result); 446 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
458 return result; 447 return result;
459} 448}
460
461
462struct genl_ops wimax_gnl_rfkill = {
463 .cmd = WIMAX_GNL_OP_RFKILL,
464 .flags = GENL_ADMIN_PERM,
465 .policy = wimax_gnl_rfkill_policy,
466 .doit = wimax_gnl_doit_rfkill,
467 .dumpit = NULL,
468};
469
diff --git a/net/wimax/op-state-get.c b/net/wimax/op-state-get.c
index aff8776e2d41..995c08c827b5 100644
--- a/net/wimax/op-state-get.c
+++ b/net/wimax/op-state-get.c
@@ -33,13 +33,6 @@
33#include "debug-levels.h" 33#include "debug-levels.h"
34 34
35 35
36static const struct nla_policy wimax_gnl_state_get_policy[WIMAX_GNL_ATTR_MAX + 1] = {
37 [WIMAX_GNL_STGET_IFIDX] = {
38 .type = NLA_U32,
39 },
40};
41
42
43/* 36/*
44 * Exporting to user space over generic netlink 37 * Exporting to user space over generic netlink
45 * 38 *
@@ -48,7 +41,6 @@ static const struct nla_policy wimax_gnl_state_get_policy[WIMAX_GNL_ATTR_MAX + 1
48 * 41 *
49 * No attributes. 42 * No attributes.
50 */ 43 */
51static
52int wimax_gnl_doit_state_get(struct sk_buff *skb, struct genl_info *info) 44int wimax_gnl_doit_state_get(struct sk_buff *skb, struct genl_info *info)
53{ 45{
54 int result, ifindex; 46 int result, ifindex;
@@ -72,12 +64,3 @@ error_no_wimax_dev:
72 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result); 64 d_fnend(3, NULL, "(skb %p info %p) = %d\n", skb, info, result);
73 return result; 65 return result;
74} 66}
75
76
77struct genl_ops wimax_gnl_state_get = {
78 .cmd = WIMAX_GNL_OP_STATE_GET,
79 .flags = GENL_ADMIN_PERM,
80 .policy = wimax_gnl_state_get_policy,
81 .doit = wimax_gnl_doit_state_get,
82 .dumpit = NULL,
83};
diff --git a/net/wimax/stack.c b/net/wimax/stack.c
index a6470ac39498..ec8b577db135 100644
--- a/net/wimax/stack.c
+++ b/net/wimax/stack.c
@@ -116,8 +116,9 @@ struct sk_buff *wimax_gnl_re_state_change_alloc(
116 dev_err(dev, "RE_STCH: can't create message\n"); 116 dev_err(dev, "RE_STCH: can't create message\n");
117 goto error_new; 117 goto error_new;
118 } 118 }
119 data = genlmsg_put(report_skb, 0, wimax_gnl_mcg.id, &wimax_gnl_family, 119 /* FIXME: sending a group ID as the seq is wrong */
120 0, WIMAX_GNL_RE_STATE_CHANGE); 120 data = genlmsg_put(report_skb, 0, wimax_gnl_family.mcgrp_offset,
121 &wimax_gnl_family, 0, WIMAX_GNL_RE_STATE_CHANGE);
121 if (data == NULL) { 122 if (data == NULL) {
122 dev_err(dev, "RE_STCH: can't put data into message\n"); 123 dev_err(dev, "RE_STCH: can't put data into message\n");
123 goto error_put; 124 goto error_put;
@@ -177,7 +178,7 @@ int wimax_gnl_re_state_change_send(
177 goto out; 178 goto out;
178 } 179 }
179 genlmsg_end(report_skb, header); 180 genlmsg_end(report_skb, header);
180 genlmsg_multicast(report_skb, 0, wimax_gnl_mcg.id, GFP_KERNEL); 181 genlmsg_multicast(&wimax_gnl_family, report_skb, 0, 0, GFP_KERNEL);
181out: 182out:
182 d_fnend(3, dev, "(wimax_dev %p report_skb %p) = %d\n", 183 d_fnend(3, dev, "(wimax_dev %p report_skb %p) = %d\n",
183 wimax_dev, report_skb, result); 184 wimax_dev, report_skb, result);
@@ -402,22 +403,44 @@ void wimax_dev_init(struct wimax_dev *wimax_dev)
402} 403}
403EXPORT_SYMBOL_GPL(wimax_dev_init); 404EXPORT_SYMBOL_GPL(wimax_dev_init);
404 405
405/* 406static const struct nla_policy wimax_gnl_policy[WIMAX_GNL_ATTR_MAX + 1] = {
406 * This extern is declared here because it's easier to keep track -- 407 [WIMAX_GNL_RESET_IFIDX] = { .type = NLA_U32, },
407 * both declarations are a list of the same 408 [WIMAX_GNL_RFKILL_IFIDX] = { .type = NLA_U32, },
408 */ 409 [WIMAX_GNL_RFKILL_STATE] = {
409extern struct genl_ops 410 .type = NLA_U32 /* enum wimax_rf_state */
410 wimax_gnl_msg_from_user, 411 },
411 wimax_gnl_reset, 412 [WIMAX_GNL_STGET_IFIDX] = { .type = NLA_U32, },
412 wimax_gnl_rfkill, 413 [WIMAX_GNL_MSG_IFIDX] = { .type = NLA_U32, },
413 wimax_gnl_state_get; 414 [WIMAX_GNL_MSG_DATA] = {
415 .type = NLA_UNSPEC, /* libnl doesn't grok BINARY yet */
416 },
417};
414 418
415static 419static const struct genl_ops wimax_gnl_ops[] = {
416struct genl_ops *wimax_gnl_ops[] = { 420 {
417 &wimax_gnl_msg_from_user, 421 .cmd = WIMAX_GNL_OP_MSG_FROM_USER,
418 &wimax_gnl_reset, 422 .flags = GENL_ADMIN_PERM,
419 &wimax_gnl_rfkill, 423 .policy = wimax_gnl_policy,
420 &wimax_gnl_state_get, 424 .doit = wimax_gnl_doit_msg_from_user,
425 },
426 {
427 .cmd = WIMAX_GNL_OP_RESET,
428 .flags = GENL_ADMIN_PERM,
429 .policy = wimax_gnl_policy,
430 .doit = wimax_gnl_doit_reset,
431 },
432 {
433 .cmd = WIMAX_GNL_OP_RFKILL,
434 .flags = GENL_ADMIN_PERM,
435 .policy = wimax_gnl_policy,
436 .doit = wimax_gnl_doit_rfkill,
437 },
438 {
439 .cmd = WIMAX_GNL_OP_STATE_GET,
440 .flags = GENL_ADMIN_PERM,
441 .policy = wimax_gnl_policy,
442 .doit = wimax_gnl_doit_state_get,
443 },
421}; 444};
422 445
423 446
@@ -557,8 +580,8 @@ struct genl_family wimax_gnl_family = {
557 .maxattr = WIMAX_GNL_ATTR_MAX, 580 .maxattr = WIMAX_GNL_ATTR_MAX,
558}; 581};
559 582
560struct genl_multicast_group wimax_gnl_mcg = { 583static const struct genl_multicast_group wimax_gnl_mcgrps[] = {
561 .name = "msg", 584 { .name = "msg", },
562}; 585};
563 586
564 587
@@ -567,7 +590,7 @@ struct genl_multicast_group wimax_gnl_mcg = {
567static 590static
568int __init wimax_subsys_init(void) 591int __init wimax_subsys_init(void)
569{ 592{
570 int result, cnt; 593 int result;
571 594
572 d_fnstart(4, NULL, "()\n"); 595 d_fnstart(4, NULL, "()\n");
573 d_parse_params(D_LEVEL, D_LEVEL_SIZE, wimax_debug_params, 596 d_parse_params(D_LEVEL, D_LEVEL_SIZE, wimax_debug_params,
@@ -575,38 +598,18 @@ int __init wimax_subsys_init(void)
575 598
576 snprintf(wimax_gnl_family.name, sizeof(wimax_gnl_family.name), 599 snprintf(wimax_gnl_family.name, sizeof(wimax_gnl_family.name),
577 "WiMAX"); 600 "WiMAX");
578 result = genl_register_family(&wimax_gnl_family); 601 result = genl_register_family_with_ops_groups(&wimax_gnl_family,
602 wimax_gnl_ops,
603 wimax_gnl_mcgrps);
579 if (unlikely(result < 0)) { 604 if (unlikely(result < 0)) {
580 printk(KERN_ERR "cannot register generic netlink family: %d\n", 605 printk(KERN_ERR "cannot register generic netlink family: %d\n",
581 result); 606 result);
582 goto error_register_family; 607 goto error_register_family;
583 } 608 }
584 609
585 for (cnt = 0; cnt < ARRAY_SIZE(wimax_gnl_ops); cnt++) {
586 result = genl_register_ops(&wimax_gnl_family,
587 wimax_gnl_ops[cnt]);
588 d_printf(4, NULL, "registering generic netlink op code "
589 "%u: %d\n", wimax_gnl_ops[cnt]->cmd, result);
590 if (unlikely(result < 0)) {
591 printk(KERN_ERR "cannot register generic netlink op "
592 "code %u: %d\n",
593 wimax_gnl_ops[cnt]->cmd, result);
594 goto error_register_ops;
595 }
596 }
597
598 result = genl_register_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
599 if (result < 0)
600 goto error_mc_group;
601 d_fnend(4, NULL, "() = 0\n"); 610 d_fnend(4, NULL, "() = 0\n");
602 return 0; 611 return 0;
603 612
604error_mc_group:
605error_register_ops:
606 for (cnt--; cnt >= 0; cnt--)
607 genl_unregister_ops(&wimax_gnl_family,
608 wimax_gnl_ops[cnt]);
609 genl_unregister_family(&wimax_gnl_family);
610error_register_family: 613error_register_family:
611 d_fnend(4, NULL, "() = %d\n", result); 614 d_fnend(4, NULL, "() = %d\n", result);
612 return result; 615 return result;
@@ -619,12 +622,7 @@ module_init(wimax_subsys_init);
619static 622static
620void __exit wimax_subsys_exit(void) 623void __exit wimax_subsys_exit(void)
621{ 624{
622 int cnt;
623 wimax_id_table_release(); 625 wimax_id_table_release();
624 genl_unregister_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
625 for (cnt = ARRAY_SIZE(wimax_gnl_ops) - 1; cnt >= 0; cnt--)
626 genl_unregister_ops(&wimax_gnl_family,
627 wimax_gnl_ops[cnt]);
628 genl_unregister_family(&wimax_gnl_family); 626 genl_unregister_family(&wimax_gnl_family);
629} 627}
630module_exit(wimax_subsys_exit); 628module_exit(wimax_subsys_exit);
diff --git a/net/wimax/wimax-internal.h b/net/wimax/wimax-internal.h
index 1e743d214856..b445b82020a8 100644
--- a/net/wimax/wimax-internal.h
+++ b/net/wimax/wimax-internal.h
@@ -63,11 +63,11 @@ void __wimax_state_set(struct wimax_dev *wimax_dev, enum wimax_st state)
63{ 63{
64 wimax_dev->state = state; 64 wimax_dev->state = state;
65} 65}
66extern void __wimax_state_change(struct wimax_dev *, enum wimax_st); 66void __wimax_state_change(struct wimax_dev *, enum wimax_st);
67 67
68#ifdef CONFIG_DEBUG_FS 68#ifdef CONFIG_DEBUG_FS
69extern int wimax_debugfs_add(struct wimax_dev *); 69int wimax_debugfs_add(struct wimax_dev *);
70extern void wimax_debugfs_rm(struct wimax_dev *); 70void wimax_debugfs_rm(struct wimax_dev *);
71#else 71#else
72static inline int wimax_debugfs_add(struct wimax_dev *wimax_dev) 72static inline int wimax_debugfs_add(struct wimax_dev *wimax_dev)
73{ 73{
@@ -76,16 +76,22 @@ static inline int wimax_debugfs_add(struct wimax_dev *wimax_dev)
76static inline void wimax_debugfs_rm(struct wimax_dev *wimax_dev) {} 76static inline void wimax_debugfs_rm(struct wimax_dev *wimax_dev) {}
77#endif 77#endif
78 78
79extern void wimax_id_table_add(struct wimax_dev *); 79void wimax_id_table_add(struct wimax_dev *);
80extern struct wimax_dev *wimax_dev_get_by_genl_info(struct genl_info *, int); 80struct wimax_dev *wimax_dev_get_by_genl_info(struct genl_info *, int);
81extern void wimax_id_table_rm(struct wimax_dev *); 81void wimax_id_table_rm(struct wimax_dev *);
82extern void wimax_id_table_release(void); 82void wimax_id_table_release(void);
83 83
84extern int wimax_rfkill_add(struct wimax_dev *); 84int wimax_rfkill_add(struct wimax_dev *);
85extern void wimax_rfkill_rm(struct wimax_dev *); 85void wimax_rfkill_rm(struct wimax_dev *);
86 86
87/* generic netlink */
87extern struct genl_family wimax_gnl_family; 88extern struct genl_family wimax_gnl_family;
88extern struct genl_multicast_group wimax_gnl_mcg; 89
90/* ops */
91int wimax_gnl_doit_msg_from_user(struct sk_buff *skb, struct genl_info *info);
92int wimax_gnl_doit_reset(struct sk_buff *skb, struct genl_info *info);
93int wimax_gnl_doit_rfkill(struct sk_buff *skb, struct genl_info *info);
94int wimax_gnl_doit_state_get(struct sk_buff *skb, struct genl_info *info);
89 95
90#endif /* #ifdef __KERNEL__ */ 96#endif /* #ifdef __KERNEL__ */
91#endif /* #ifndef __WIMAX_INTERNAL_H__ */ 97#endif /* #ifndef __WIMAX_INTERNAL_H__ */
diff --git a/net/wireless/chan.c b/net/wireless/chan.c
index 50f6195c8b70..9b8cc877eb19 100644
--- a/net/wireless/chan.c
+++ b/net/wireless/chan.c
@@ -328,6 +328,7 @@ int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
328 return cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq2, 328 return cfg80211_get_chans_dfs_required(wiphy, chandef->center_freq2,
329 width); 329 width);
330} 330}
331EXPORT_SYMBOL(cfg80211_chandef_dfs_required);
331 332
332static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy, 333static bool cfg80211_secondary_chans_ok(struct wiphy *wiphy,
333 u32 center_freq, u32 bandwidth, 334 u32 center_freq, u32 bandwidth,
@@ -503,7 +504,8 @@ cfg80211_get_chan_state(struct wireless_dev *wdev,
503 case NL80211_IFTYPE_ADHOC: 504 case NL80211_IFTYPE_ADHOC:
504 if (wdev->current_bss) { 505 if (wdev->current_bss) {
505 *chan = wdev->current_bss->pub.channel; 506 *chan = wdev->current_bss->pub.channel;
506 *chanmode = wdev->ibss_fixed 507 *chanmode = (wdev->ibss_fixed &&
508 !wdev->ibss_dfs_possible)
507 ? CHAN_MODE_SHARED 509 ? CHAN_MODE_SHARED
508 : CHAN_MODE_EXCLUSIVE; 510 : CHAN_MODE_EXCLUSIVE;
509 return; 511 return;
diff --git a/net/wireless/core.c b/net/wireless/core.c
index aff959e5a1b3..52b865fb7351 100644
--- a/net/wireless/core.c
+++ b/net/wireless/core.c
@@ -451,6 +451,15 @@ int wiphy_register(struct wiphy *wiphy)
451 int i; 451 int i;
452 u16 ifmodes = wiphy->interface_modes; 452 u16 ifmodes = wiphy->interface_modes;
453 453
454 /* support for 5/10 MHz is broken due to nl80211 API mess - disable */
455 wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_5_10_MHZ;
456
457 /*
458 * There are major locking problems in nl80211/mac80211 for CSA,
459 * disable for all drivers until this has been reworked.
460 */
461 wiphy->flags &= ~WIPHY_FLAG_HAS_CHANNEL_SWITCH;
462
454#ifdef CONFIG_PM 463#ifdef CONFIG_PM
455 if (WARN_ON(wiphy->wowlan && 464 if (WARN_ON(wiphy->wowlan &&
456 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) && 465 (wiphy->wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
diff --git a/net/wireless/core.h b/net/wireless/core.h
index 3159e9c284c5..af10e59af2d8 100644
--- a/net/wireless/core.h
+++ b/net/wireless/core.h
@@ -234,10 +234,10 @@ struct cfg80211_beacon_registration {
234}; 234};
235 235
236/* free object */ 236/* free object */
237extern void cfg80211_dev_free(struct cfg80211_registered_device *rdev); 237void cfg80211_dev_free(struct cfg80211_registered_device *rdev);
238 238
239extern int cfg80211_dev_rename(struct cfg80211_registered_device *rdev, 239int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
240 char *newname); 240 char *newname);
241 241
242void ieee80211_set_bitrate_flags(struct wiphy *wiphy); 242void ieee80211_set_bitrate_flags(struct wiphy *wiphy);
243 243
@@ -382,15 +382,6 @@ int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
382 enum cfg80211_chan_mode chanmode, 382 enum cfg80211_chan_mode chanmode,
383 u8 radar_detect); 383 u8 radar_detect);
384 384
385/**
386 * cfg80211_chandef_dfs_required - checks if radar detection is required
387 * @wiphy: the wiphy to validate against
388 * @chandef: the channel definition to check
389 * Return: 1 if radar detection is required, 0 if it is not, < 0 on error
390 */
391int cfg80211_chandef_dfs_required(struct wiphy *wiphy,
392 const struct cfg80211_chan_def *c);
393
394void cfg80211_set_dfs_state(struct wiphy *wiphy, 385void cfg80211_set_dfs_state(struct wiphy *wiphy,
395 const struct cfg80211_chan_def *chandef, 386 const struct cfg80211_chan_def *chandef,
396 enum nl80211_dfs_state dfs_state); 387 enum nl80211_dfs_state dfs_state);
diff --git a/net/wireless/debugfs.c b/net/wireless/debugfs.c
index 90d050036624..454157717efa 100644
--- a/net/wireless/debugfs.c
+++ b/net/wireless/debugfs.c
@@ -47,17 +47,19 @@ static int ht_print_chan(struct ieee80211_channel *chan,
47 return 0; 47 return 0;
48 48
49 if (chan->flags & IEEE80211_CHAN_DISABLED) 49 if (chan->flags & IEEE80211_CHAN_DISABLED)
50 return snprintf(buf + offset, 50 return scnprintf(buf + offset,
51 buf_size - offset, 51 buf_size - offset,
52 "%d Disabled\n", 52 "%d Disabled\n",
53 chan->center_freq); 53 chan->center_freq);
54 54
55 return snprintf(buf + offset, 55 return scnprintf(buf + offset,
56 buf_size - offset, 56 buf_size - offset,
57 "%d HT40 %c%c\n", 57 "%d HT40 %c%c\n",
58 chan->center_freq, 58 chan->center_freq,
59 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) ? ' ' : '-', 59 (chan->flags & IEEE80211_CHAN_NO_HT40MINUS) ?
60 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) ? ' ' : '+'); 60 ' ' : '-',
61 (chan->flags & IEEE80211_CHAN_NO_HT40PLUS) ?
62 ' ' : '+');
61} 63}
62 64
63static ssize_t ht40allow_map_read(struct file *file, 65static ssize_t ht40allow_map_read(struct file *file,
diff --git a/net/wireless/genregdb.awk b/net/wireless/genregdb.awk
index 9392f8cbb901..42ed274e81f4 100644
--- a/net/wireless/genregdb.awk
+++ b/net/wireless/genregdb.awk
@@ -46,6 +46,12 @@ BEGIN {
46 sub(/:/, "", country) 46 sub(/:/, "", country)
47 printf "static const struct ieee80211_regdomain regdom_%s = {\n", country 47 printf "static const struct ieee80211_regdomain regdom_%s = {\n", country
48 printf "\t.alpha2 = \"%s\",\n", country 48 printf "\t.alpha2 = \"%s\",\n", country
49 if ($NF ~ /DFS-ETSI/)
50 printf "\t.dfs_region = NL80211_DFS_ETSI,\n"
51 else if ($NF ~ /DFS-FCC/)
52 printf "\t.dfs_region = NL80211_DFS_FCC,\n"
53 else if ($NF ~ /DFS-JP/)
54 printf "\t.dfs_region = NL80211_DFS_JP,\n"
49 printf "\t.reg_rules = {\n" 55 printf "\t.reg_rules = {\n"
50 active = 1 56 active = 1
51 regdb = regdb "\t&regdom_" country ",\n" 57 regdb = regdb "\t&regdom_" country ",\n"
diff --git a/net/wireless/ibss.c b/net/wireless/ibss.c
index 403fe29c024d..89737ee2669a 100644
--- a/net/wireless/ibss.c
+++ b/net/wireless/ibss.c
@@ -83,6 +83,8 @@ int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
83 struct cfg80211_cached_keys *connkeys) 83 struct cfg80211_cached_keys *connkeys)
84{ 84{
85 struct wireless_dev *wdev = dev->ieee80211_ptr; 85 struct wireless_dev *wdev = dev->ieee80211_ptr;
86 struct ieee80211_channel *check_chan;
87 u8 radar_detect_width = 0;
86 int err; 88 int err;
87 89
88 ASSERT_WDEV_LOCK(wdev); 90 ASSERT_WDEV_LOCK(wdev);
@@ -114,14 +116,28 @@ int __cfg80211_join_ibss(struct cfg80211_registered_device *rdev,
114 wdev->connect_keys = connkeys; 116 wdev->connect_keys = connkeys;
115 117
116 wdev->ibss_fixed = params->channel_fixed; 118 wdev->ibss_fixed = params->channel_fixed;
119 wdev->ibss_dfs_possible = params->userspace_handles_dfs;
117#ifdef CONFIG_CFG80211_WEXT 120#ifdef CONFIG_CFG80211_WEXT
118 wdev->wext.ibss.chandef = params->chandef; 121 wdev->wext.ibss.chandef = params->chandef;
119#endif 122#endif
123 check_chan = params->chandef.chan;
124 if (params->userspace_handles_dfs) {
125 /* use channel NULL to check for radar even if the current
126 * channel is not a radar channel - it might decide to change
127 * to DFS channel later.
128 */
129 radar_detect_width = BIT(params->chandef.width);
130 check_chan = NULL;
131 }
132
133 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
134 check_chan,
135 (params->channel_fixed &&
136 !radar_detect_width)
137 ? CHAN_MODE_SHARED
138 : CHAN_MODE_EXCLUSIVE,
139 radar_detect_width);
120 140
121 err = cfg80211_can_use_chan(rdev, wdev, params->chandef.chan,
122 params->channel_fixed
123 ? CHAN_MODE_SHARED
124 : CHAN_MODE_EXCLUSIVE);
125 if (err) { 141 if (err) {
126 wdev->connect_keys = NULL; 142 wdev->connect_keys = NULL;
127 return err; 143 return err;
@@ -246,7 +262,7 @@ int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
246 262
247 /* try to find an IBSS channel if none requested ... */ 263 /* try to find an IBSS channel if none requested ... */
248 if (!wdev->wext.ibss.chandef.chan) { 264 if (!wdev->wext.ibss.chandef.chan) {
249 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 265 struct ieee80211_channel *new_chan = NULL;
250 266
251 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 267 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
252 struct ieee80211_supported_band *sband; 268 struct ieee80211_supported_band *sband;
@@ -262,18 +278,19 @@ int cfg80211_ibss_wext_join(struct cfg80211_registered_device *rdev,
262 continue; 278 continue;
263 if (chan->flags & IEEE80211_CHAN_DISABLED) 279 if (chan->flags & IEEE80211_CHAN_DISABLED)
264 continue; 280 continue;
265 wdev->wext.ibss.chandef.chan = chan; 281 new_chan = chan;
266 wdev->wext.ibss.chandef.center_freq1 =
267 chan->center_freq;
268 break; 282 break;
269 } 283 }
270 284
271 if (wdev->wext.ibss.chandef.chan) 285 if (new_chan)
272 break; 286 break;
273 } 287 }
274 288
275 if (!wdev->wext.ibss.chandef.chan) 289 if (!new_chan)
276 return -EINVAL; 290 return -EINVAL;
291
292 cfg80211_chandef_create(&wdev->wext.ibss.chandef, new_chan,
293 NL80211_CHAN_NO_HT);
277 } 294 }
278 295
279 /* don't join -- SSID is not there */ 296 /* don't join -- SSID is not there */
@@ -347,9 +364,8 @@ int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
347 return err; 364 return err;
348 365
349 if (chan) { 366 if (chan) {
350 wdev->wext.ibss.chandef.chan = chan; 367 cfg80211_chandef_create(&wdev->wext.ibss.chandef, chan,
351 wdev->wext.ibss.chandef.width = NL80211_CHAN_WIDTH_20_NOHT; 368 NL80211_CHAN_NO_HT);
352 wdev->wext.ibss.chandef.center_freq1 = freq;
353 wdev->wext.ibss.channel_fixed = true; 369 wdev->wext.ibss.channel_fixed = true;
354 } else { 370 } else {
355 /* cfg80211_ibss_wext_join will pick one if needed */ 371 /* cfg80211_ibss_wext_join will pick one if needed */
diff --git a/net/wireless/mlme.c b/net/wireless/mlme.c
index 8d49c1ce3dea..6a6b1c8e907d 100644
--- a/net/wireless/mlme.c
+++ b/net/wireless/mlme.c
@@ -707,11 +707,13 @@ void cfg80211_dfs_channels_update_work(struct work_struct *work)
707 if (c->dfs_state != NL80211_DFS_UNAVAILABLE) 707 if (c->dfs_state != NL80211_DFS_UNAVAILABLE)
708 continue; 708 continue;
709 709
710 timeout = c->dfs_state_entered + 710 timeout = c->dfs_state_entered + msecs_to_jiffies(
711 IEEE80211_DFS_MIN_NOP_TIME_MS; 711 IEEE80211_DFS_MIN_NOP_TIME_MS);
712 712
713 if (time_after_eq(jiffies, timeout)) { 713 if (time_after_eq(jiffies, timeout)) {
714 c->dfs_state = NL80211_DFS_USABLE; 714 c->dfs_state = NL80211_DFS_USABLE;
715 c->dfs_state_entered = jiffies;
716
715 cfg80211_chandef_create(&chandef, c, 717 cfg80211_chandef_create(&chandef, c,
716 NL80211_CHAN_NO_HT); 718 NL80211_CHAN_NO_HT);
717 719
diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
index 626dc3b5fd8d..138dc3bb8b67 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -30,9 +30,9 @@ static int nl80211_crypto_settings(struct cfg80211_registered_device *rdev,
30 struct cfg80211_crypto_settings *settings, 30 struct cfg80211_crypto_settings *settings,
31 int cipher_limit); 31 int cipher_limit);
32 32
33static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb, 33static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
34 struct genl_info *info); 34 struct genl_info *info);
35static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb, 35static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
36 struct genl_info *info); 36 struct genl_info *info);
37 37
38/* the netlink family */ 38/* the netlink family */
@@ -47,6 +47,25 @@ static struct genl_family nl80211_fam = {
47 .post_doit = nl80211_post_doit, 47 .post_doit = nl80211_post_doit,
48}; 48};
49 49
50/* multicast groups */
51enum nl80211_multicast_groups {
52 NL80211_MCGRP_CONFIG,
53 NL80211_MCGRP_SCAN,
54 NL80211_MCGRP_REGULATORY,
55 NL80211_MCGRP_MLME,
56 NL80211_MCGRP_TESTMODE /* keep last - ifdef! */
57};
58
59static const struct genl_multicast_group nl80211_mcgrps[] = {
60 [NL80211_MCGRP_CONFIG] = { .name = "config", },
61 [NL80211_MCGRP_SCAN] = { .name = "scan", },
62 [NL80211_MCGRP_REGULATORY] = { .name = "regulatory", },
63 [NL80211_MCGRP_MLME] = { .name = "mlme", },
64#ifdef CONFIG_NL80211_TESTMODE
65 [NL80211_MCGRP_TESTMODE] = { .name = "testmode", }
66#endif
67};
68
50/* returns ERR_PTR values */ 69/* returns ERR_PTR values */
51static struct wireless_dev * 70static struct wireless_dev *
52__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs) 71__cfg80211_wdev_from_attrs(struct net *netns, struct nlattr **attrs)
@@ -354,6 +373,9 @@ static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
354 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED }, 373 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
355 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 }, 374 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
356 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 }, 375 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
376 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
377 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
378 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
357}; 379};
358 380
359/* policy for the key attributes */ 381/* policy for the key attributes */
@@ -2665,7 +2687,7 @@ static int nl80211_get_key(struct sk_buff *skb, struct genl_info *info)
2665 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0, 2687 hdr = nl80211hdr_put(msg, info->snd_portid, info->snd_seq, 0,
2666 NL80211_CMD_NEW_KEY); 2688 NL80211_CMD_NEW_KEY);
2667 if (!hdr) 2689 if (!hdr)
2668 return -ENOBUFS; 2690 goto nla_put_failure;
2669 2691
2670 cookie.msg = msg; 2692 cookie.msg = msg;
2671 cookie.idx = key_idx; 2693 cookie.idx = key_idx;
@@ -3896,9 +3918,45 @@ static int nl80211_parse_sta_wme(struct genl_info *info,
3896 return 0; 3918 return 0;
3897} 3919}
3898 3920
3921static int nl80211_parse_sta_channel_info(struct genl_info *info,
3922 struct station_parameters *params)
3923{
3924 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3925 params->supported_channels =
3926 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3927 params->supported_channels_len =
3928 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3929 /*
3930 * Need to include at least one (first channel, number of
3931 * channels) tuple for each subband, and must have proper
3932 * tuples for the rest of the data as well.
3933 */
3934 if (params->supported_channels_len < 2)
3935 return -EINVAL;
3936 if (params->supported_channels_len % 2)
3937 return -EINVAL;
3938 }
3939
3940 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3941 params->supported_oper_classes =
3942 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3943 params->supported_oper_classes_len =
3944 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3945 /*
3946 * The value of the Length field of the Supported Operating
3947 * Classes element is between 2 and 253.
3948 */
3949 if (params->supported_oper_classes_len < 2 ||
3950 params->supported_oper_classes_len > 253)
3951 return -EINVAL;
3952 }
3953 return 0;
3954}
3955
3899static int nl80211_set_station_tdls(struct genl_info *info, 3956static int nl80211_set_station_tdls(struct genl_info *info,
3900 struct station_parameters *params) 3957 struct station_parameters *params)
3901{ 3958{
3959 int err;
3902 /* Dummy STA entry gets updated once the peer capabilities are known */ 3960 /* Dummy STA entry gets updated once the peer capabilities are known */
3903 if (info->attrs[NL80211_ATTR_PEER_AID]) 3961 if (info->attrs[NL80211_ATTR_PEER_AID])
3904 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]); 3962 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
@@ -3909,6 +3967,10 @@ static int nl80211_set_station_tdls(struct genl_info *info,
3909 params->vht_capa = 3967 params->vht_capa =
3910 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]); 3968 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3911 3969
3970 err = nl80211_parse_sta_channel_info(info, params);
3971 if (err)
3972 return err;
3973
3912 return nl80211_parse_sta_wme(info, params); 3974 return nl80211_parse_sta_wme(info, params);
3913} 3975}
3914 3976
@@ -4089,6 +4151,10 @@ static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4089 return -EINVAL; 4151 return -EINVAL;
4090 } 4152 }
4091 4153
4154 err = nl80211_parse_sta_channel_info(info, &params);
4155 if (err)
4156 return err;
4157
4092 err = nl80211_parse_sta_wme(info, &params); 4158 err = nl80211_parse_sta_wme(info, &params);
4093 if (err) 4159 if (err)
4094 return err; 4160 return err;
@@ -5283,6 +5349,10 @@ static int nl80211_trigger_scan(struct sk_buff *skb, struct genl_info *info)
5283 err = -EINVAL; 5349 err = -EINVAL;
5284 goto out_free; 5350 goto out_free;
5285 } 5351 }
5352
5353 if (!wiphy->bands[band])
5354 continue;
5355
5286 err = ieee80211_get_ratemask(wiphy->bands[band], 5356 err = ieee80211_get_ratemask(wiphy->bands[band],
5287 nla_data(attr), 5357 nla_data(attr),
5288 nla_len(attr), 5358 nla_len(attr),
@@ -5591,6 +5661,9 @@ static int nl80211_start_radar_detection(struct sk_buff *skb,
5591 if (err) 5661 if (err)
5592 return err; 5662 return err;
5593 5663
5664 if (netif_carrier_ok(dev))
5665 return -EBUSY;
5666
5594 if (wdev->cac_started) 5667 if (wdev->cac_started)
5595 return -EBUSY; 5668 return -EBUSY;
5596 5669
@@ -5634,15 +5707,27 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5634 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1]; 5707 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5635 u8 radar_detect_width = 0; 5708 u8 radar_detect_width = 0;
5636 int err; 5709 int err;
5710 bool need_new_beacon = false;
5637 5711
5638 if (!rdev->ops->channel_switch || 5712 if (!rdev->ops->channel_switch ||
5639 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)) 5713 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5640 return -EOPNOTSUPP; 5714 return -EOPNOTSUPP;
5641 5715
5642 /* may add IBSS support later */ 5716 switch (dev->ieee80211_ptr->iftype) {
5643 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP && 5717 case NL80211_IFTYPE_AP:
5644 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) 5718 case NL80211_IFTYPE_P2P_GO:
5719 need_new_beacon = true;
5720
5721 /* useless if AP is not running */
5722 if (!wdev->beacon_interval)
5723 return -EINVAL;
5724 break;
5725 case NL80211_IFTYPE_ADHOC:
5726 case NL80211_IFTYPE_MESH_POINT:
5727 break;
5728 default:
5645 return -EOPNOTSUPP; 5729 return -EOPNOTSUPP;
5730 }
5646 5731
5647 memset(&params, 0, sizeof(params)); 5732 memset(&params, 0, sizeof(params));
5648 5733
@@ -5651,15 +5736,14 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5651 return -EINVAL; 5736 return -EINVAL;
5652 5737
5653 /* only important for AP, IBSS and mesh create IEs internally */ 5738 /* only important for AP, IBSS and mesh create IEs internally */
5654 if (!info->attrs[NL80211_ATTR_CSA_IES]) 5739 if (need_new_beacon && !info->attrs[NL80211_ATTR_CSA_IES])
5655 return -EINVAL;
5656
5657 /* useless if AP is not running */
5658 if (!wdev->beacon_interval)
5659 return -EINVAL; 5740 return -EINVAL;
5660 5741
5661 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]); 5742 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5662 5743
5744 if (!need_new_beacon)
5745 goto skip_beacons;
5746
5663 err = nl80211_parse_beacon(info->attrs, &params.beacon_after); 5747 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5664 if (err) 5748 if (err)
5665 return err; 5749 return err;
@@ -5699,6 +5783,7 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5699 return -EINVAL; 5783 return -EINVAL;
5700 } 5784 }
5701 5785
5786skip_beacons:
5702 err = nl80211_parse_chandef(rdev, info, &params.chandef); 5787 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5703 if (err) 5788 if (err)
5704 return err; 5789 return err;
@@ -5706,12 +5791,17 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5706 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef)) 5791 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5707 return -EINVAL; 5792 return -EINVAL;
5708 5793
5709 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef); 5794 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
5710 if (err < 0) { 5795 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5711 return err; 5796 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
5712 } else if (err) { 5797 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5713 radar_detect_width = BIT(params.chandef.width); 5798 &params.chandef);
5714 params.radar_required = true; 5799 if (err < 0) {
5800 return err;
5801 } else if (err) {
5802 radar_detect_width = BIT(params.chandef.width);
5803 params.radar_required = true;
5804 }
5715 } 5805 }
5716 5806
5717 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype, 5807 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
@@ -6535,6 +6625,9 @@ static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6535 ibss.control_port = 6625 ibss.control_port =
6536 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]); 6626 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6537 6627
6628 ibss.userspace_handles_dfs =
6629 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6630
6538 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys); 6631 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
6539 if (err) 6632 if (err)
6540 kfree(connkeys); 6633 kfree(connkeys);
@@ -6586,10 +6679,6 @@ static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
6586 6679
6587 6680
6588#ifdef CONFIG_NL80211_TESTMODE 6681#ifdef CONFIG_NL80211_TESTMODE
6589static struct genl_multicast_group nl80211_testmode_mcgrp = {
6590 .name = "testmode",
6591};
6592
6593static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info) 6682static int nl80211_testmode_do(struct sk_buff *skb, struct genl_info *info)
6594{ 6683{
6595 struct cfg80211_registered_device *rdev = info->user_ptr[0]; 6684 struct cfg80211_registered_device *rdev = info->user_ptr[0];
@@ -6798,8 +6887,8 @@ void cfg80211_testmode_event(struct sk_buff *skb, gfp_t gfp)
6798 6887
6799 nla_nest_end(skb, data); 6888 nla_nest_end(skb, data);
6800 genlmsg_end(skb, hdr); 6889 genlmsg_end(skb, hdr);
6801 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), skb, 0, 6890 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), skb, 0,
6802 nl80211_testmode_mcgrp.id, gfp); 6891 NL80211_MCGRP_TESTMODE, gfp);
6803} 6892}
6804EXPORT_SYMBOL(cfg80211_testmode_event); 6893EXPORT_SYMBOL(cfg80211_testmode_event);
6805#endif 6894#endif
@@ -8781,7 +8870,7 @@ static int nl80211_crit_protocol_stop(struct sk_buff *skb,
8781#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\ 8870#define NL80211_FLAG_NEED_WDEV_UP (NL80211_FLAG_NEED_WDEV |\
8782 NL80211_FLAG_CHECK_NETDEV_UP) 8871 NL80211_FLAG_CHECK_NETDEV_UP)
8783 8872
8784static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb, 8873static int nl80211_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
8785 struct genl_info *info) 8874 struct genl_info *info)
8786{ 8875{
8787 struct cfg80211_registered_device *rdev; 8876 struct cfg80211_registered_device *rdev;
@@ -8850,7 +8939,7 @@ static int nl80211_pre_doit(struct genl_ops *ops, struct sk_buff *skb,
8850 return 0; 8939 return 0;
8851} 8940}
8852 8941
8853static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb, 8942static void nl80211_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
8854 struct genl_info *info) 8943 struct genl_info *info)
8855{ 8944{
8856 if (info->user_ptr[1]) { 8945 if (info->user_ptr[1]) {
@@ -8867,7 +8956,7 @@ static void nl80211_post_doit(struct genl_ops *ops, struct sk_buff *skb,
8867 rtnl_unlock(); 8956 rtnl_unlock();
8868} 8957}
8869 8958
8870static struct genl_ops nl80211_ops[] = { 8959static const struct genl_ops nl80211_ops[] = {
8871 { 8960 {
8872 .cmd = NL80211_CMD_GET_WIPHY, 8961 .cmd = NL80211_CMD_GET_WIPHY,
8873 .doit = nl80211_get_wiphy, 8962 .doit = nl80211_get_wiphy,
@@ -9496,21 +9585,6 @@ static struct genl_ops nl80211_ops[] = {
9496 }, 9585 },
9497}; 9586};
9498 9587
9499static struct genl_multicast_group nl80211_mlme_mcgrp = {
9500 .name = "mlme",
9501};
9502
9503/* multicast groups */
9504static struct genl_multicast_group nl80211_config_mcgrp = {
9505 .name = "config",
9506};
9507static struct genl_multicast_group nl80211_scan_mcgrp = {
9508 .name = "scan",
9509};
9510static struct genl_multicast_group nl80211_regulatory_mcgrp = {
9511 .name = "regulatory",
9512};
9513
9514/* notification functions */ 9588/* notification functions */
9515 9589
9516void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev) 9590void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
@@ -9527,8 +9601,8 @@ void nl80211_notify_dev_rename(struct cfg80211_registered_device *rdev)
9527 return; 9601 return;
9528 } 9602 }
9529 9603
9530 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9604 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9531 nl80211_config_mcgrp.id, GFP_KERNEL); 9605 NL80211_MCGRP_CONFIG, GFP_KERNEL);
9532} 9606}
9533 9607
9534static int nl80211_add_scan_req(struct sk_buff *msg, 9608static int nl80211_add_scan_req(struct sk_buff *msg,
@@ -9563,8 +9637,9 @@ static int nl80211_add_scan_req(struct sk_buff *msg,
9563 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie)) 9637 nla_put(msg, NL80211_ATTR_IE, req->ie_len, req->ie))
9564 goto nla_put_failure; 9638 goto nla_put_failure;
9565 9639
9566 if (req->flags) 9640 if (req->flags &&
9567 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags); 9641 nla_put_u32(msg, NL80211_ATTR_SCAN_FLAGS, req->flags))
9642 goto nla_put_failure;
9568 9643
9569 return 0; 9644 return 0;
9570 nla_put_failure: 9645 nla_put_failure:
@@ -9637,8 +9712,8 @@ void nl80211_send_scan_start(struct cfg80211_registered_device *rdev,
9637 return; 9712 return;
9638 } 9713 }
9639 9714
9640 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9715 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9641 nl80211_scan_mcgrp.id, GFP_KERNEL); 9716 NL80211_MCGRP_SCAN, GFP_KERNEL);
9642} 9717}
9643 9718
9644void nl80211_send_scan_done(struct cfg80211_registered_device *rdev, 9719void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
@@ -9656,8 +9731,8 @@ void nl80211_send_scan_done(struct cfg80211_registered_device *rdev,
9656 return; 9731 return;
9657 } 9732 }
9658 9733
9659 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9734 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9660 nl80211_scan_mcgrp.id, GFP_KERNEL); 9735 NL80211_MCGRP_SCAN, GFP_KERNEL);
9661} 9736}
9662 9737
9663void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev, 9738void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
@@ -9675,8 +9750,8 @@ void nl80211_send_scan_aborted(struct cfg80211_registered_device *rdev,
9675 return; 9750 return;
9676 } 9751 }
9677 9752
9678 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9753 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9679 nl80211_scan_mcgrp.id, GFP_KERNEL); 9754 NL80211_MCGRP_SCAN, GFP_KERNEL);
9680} 9755}
9681 9756
9682void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev, 9757void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
@@ -9694,8 +9769,8 @@ void nl80211_send_sched_scan_results(struct cfg80211_registered_device *rdev,
9694 return; 9769 return;
9695 } 9770 }
9696 9771
9697 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9772 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9698 nl80211_scan_mcgrp.id, GFP_KERNEL); 9773 NL80211_MCGRP_SCAN, GFP_KERNEL);
9699} 9774}
9700 9775
9701void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev, 9776void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
@@ -9712,8 +9787,8 @@ void nl80211_send_sched_scan(struct cfg80211_registered_device *rdev,
9712 return; 9787 return;
9713 } 9788 }
9714 9789
9715 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9790 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9716 nl80211_scan_mcgrp.id, GFP_KERNEL); 9791 NL80211_MCGRP_SCAN, GFP_KERNEL);
9717} 9792}
9718 9793
9719/* 9794/*
@@ -9767,8 +9842,8 @@ void nl80211_send_reg_change_event(struct regulatory_request *request)
9767 genlmsg_end(msg, hdr); 9842 genlmsg_end(msg, hdr);
9768 9843
9769 rcu_read_lock(); 9844 rcu_read_lock();
9770 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id, 9845 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
9771 GFP_ATOMIC); 9846 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
9772 rcu_read_unlock(); 9847 rcu_read_unlock();
9773 9848
9774 return; 9849 return;
@@ -9803,8 +9878,8 @@ static void nl80211_send_mlme_event(struct cfg80211_registered_device *rdev,
9803 9878
9804 genlmsg_end(msg, hdr); 9879 genlmsg_end(msg, hdr);
9805 9880
9806 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9881 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9807 nl80211_mlme_mcgrp.id, gfp); 9882 NL80211_MCGRP_MLME, gfp);
9808 return; 9883 return;
9809 9884
9810 nla_put_failure: 9885 nla_put_failure:
@@ -9891,8 +9966,8 @@ static void nl80211_send_mlme_timeout(struct cfg80211_registered_device *rdev,
9891 9966
9892 genlmsg_end(msg, hdr); 9967 genlmsg_end(msg, hdr);
9893 9968
9894 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 9969 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9895 nl80211_mlme_mcgrp.id, gfp); 9970 NL80211_MCGRP_MLME, gfp);
9896 return; 9971 return;
9897 9972
9898 nla_put_failure: 9973 nla_put_failure:
@@ -9947,8 +10022,8 @@ void nl80211_send_connect_result(struct cfg80211_registered_device *rdev,
9947 10022
9948 genlmsg_end(msg, hdr); 10023 genlmsg_end(msg, hdr);
9949 10024
9950 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10025 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9951 nl80211_mlme_mcgrp.id, gfp); 10026 NL80211_MCGRP_MLME, gfp);
9952 return; 10027 return;
9953 10028
9954 nla_put_failure: 10029 nla_put_failure:
@@ -9986,8 +10061,8 @@ void nl80211_send_roamed(struct cfg80211_registered_device *rdev,
9986 10061
9987 genlmsg_end(msg, hdr); 10062 genlmsg_end(msg, hdr);
9988 10063
9989 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10064 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
9990 nl80211_mlme_mcgrp.id, gfp); 10065 NL80211_MCGRP_MLME, gfp);
9991 return; 10066 return;
9992 10067
9993 nla_put_failure: 10068 nla_put_failure:
@@ -10024,8 +10099,8 @@ void nl80211_send_disconnected(struct cfg80211_registered_device *rdev,
10024 10099
10025 genlmsg_end(msg, hdr); 10100 genlmsg_end(msg, hdr);
10026 10101
10027 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10102 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10028 nl80211_mlme_mcgrp.id, GFP_KERNEL); 10103 NL80211_MCGRP_MLME, GFP_KERNEL);
10029 return; 10104 return;
10030 10105
10031 nla_put_failure: 10106 nla_put_failure:
@@ -10058,8 +10133,8 @@ void nl80211_send_ibss_bssid(struct cfg80211_registered_device *rdev,
10058 10133
10059 genlmsg_end(msg, hdr); 10134 genlmsg_end(msg, hdr);
10060 10135
10061 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10136 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10062 nl80211_mlme_mcgrp.id, gfp); 10137 NL80211_MCGRP_MLME, gfp);
10063 return; 10138 return;
10064 10139
10065 nla_put_failure: 10140 nla_put_failure:
@@ -10099,8 +10174,8 @@ void cfg80211_notify_new_peer_candidate(struct net_device *dev, const u8 *addr,
10099 10174
10100 genlmsg_end(msg, hdr); 10175 genlmsg_end(msg, hdr);
10101 10176
10102 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10177 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10103 nl80211_mlme_mcgrp.id, gfp); 10178 NL80211_MCGRP_MLME, gfp);
10104 return; 10179 return;
10105 10180
10106 nla_put_failure: 10181 nla_put_failure:
@@ -10138,8 +10213,8 @@ void nl80211_michael_mic_failure(struct cfg80211_registered_device *rdev,
10138 10213
10139 genlmsg_end(msg, hdr); 10214 genlmsg_end(msg, hdr);
10140 10215
10141 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10216 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10142 nl80211_mlme_mcgrp.id, gfp); 10217 NL80211_MCGRP_MLME, gfp);
10143 return; 10218 return;
10144 10219
10145 nla_put_failure: 10220 nla_put_failure:
@@ -10191,8 +10266,8 @@ void nl80211_send_beacon_hint_event(struct wiphy *wiphy,
10191 genlmsg_end(msg, hdr); 10266 genlmsg_end(msg, hdr);
10192 10267
10193 rcu_read_lock(); 10268 rcu_read_lock();
10194 genlmsg_multicast_allns(msg, 0, nl80211_regulatory_mcgrp.id, 10269 genlmsg_multicast_allns(&nl80211_fam, msg, 0,
10195 GFP_ATOMIC); 10270 NL80211_MCGRP_REGULATORY, GFP_ATOMIC);
10196 rcu_read_unlock(); 10271 rcu_read_unlock();
10197 10272
10198 return; 10273 return;
@@ -10237,8 +10312,8 @@ static void nl80211_send_remain_on_chan_event(
10237 10312
10238 genlmsg_end(msg, hdr); 10313 genlmsg_end(msg, hdr);
10239 10314
10240 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10315 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10241 nl80211_mlme_mcgrp.id, gfp); 10316 NL80211_MCGRP_MLME, gfp);
10242 return; 10317 return;
10243 10318
10244 nla_put_failure: 10319 nla_put_failure:
@@ -10292,8 +10367,8 @@ void cfg80211_new_sta(struct net_device *dev, const u8 *mac_addr,
10292 return; 10367 return;
10293 } 10368 }
10294 10369
10295 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10370 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10296 nl80211_mlme_mcgrp.id, gfp); 10371 NL80211_MCGRP_MLME, gfp);
10297} 10372}
10298EXPORT_SYMBOL(cfg80211_new_sta); 10373EXPORT_SYMBOL(cfg80211_new_sta);
10299 10374
@@ -10322,8 +10397,8 @@ void cfg80211_del_sta(struct net_device *dev, const u8 *mac_addr, gfp_t gfp)
10322 10397
10323 genlmsg_end(msg, hdr); 10398 genlmsg_end(msg, hdr);
10324 10399
10325 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10400 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10326 nl80211_mlme_mcgrp.id, gfp); 10401 NL80211_MCGRP_MLME, gfp);
10327 return; 10402 return;
10328 10403
10329 nla_put_failure: 10404 nla_put_failure:
@@ -10358,8 +10433,8 @@ void cfg80211_conn_failed(struct net_device *dev, const u8 *mac_addr,
10358 10433
10359 genlmsg_end(msg, hdr); 10434 genlmsg_end(msg, hdr);
10360 10435
10361 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10436 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10362 nl80211_mlme_mcgrp.id, gfp); 10437 NL80211_MCGRP_MLME, gfp);
10363 return; 10438 return;
10364 10439
10365 nla_put_failure: 10440 nla_put_failure:
@@ -10520,8 +10595,8 @@ void cfg80211_mgmt_tx_status(struct wireless_dev *wdev, u64 cookie,
10520 10595
10521 genlmsg_end(msg, hdr); 10596 genlmsg_end(msg, hdr);
10522 10597
10523 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10598 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10524 nl80211_mlme_mcgrp.id, gfp); 10599 NL80211_MCGRP_MLME, gfp);
10525 return; 10600 return;
10526 10601
10527 nla_put_failure: 10602 nla_put_failure:
@@ -10569,8 +10644,8 @@ void cfg80211_cqm_rssi_notify(struct net_device *dev,
10569 10644
10570 genlmsg_end(msg, hdr); 10645 genlmsg_end(msg, hdr);
10571 10646
10572 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10647 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10573 nl80211_mlme_mcgrp.id, gfp); 10648 NL80211_MCGRP_MLME, gfp);
10574 return; 10649 return;
10575 10650
10576 nla_put_failure: 10651 nla_put_failure:
@@ -10614,8 +10689,8 @@ static void nl80211_gtk_rekey_notify(struct cfg80211_registered_device *rdev,
10614 10689
10615 genlmsg_end(msg, hdr); 10690 genlmsg_end(msg, hdr);
10616 10691
10617 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10692 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10618 nl80211_mlme_mcgrp.id, gfp); 10693 NL80211_MCGRP_MLME, gfp);
10619 return; 10694 return;
10620 10695
10621 nla_put_failure: 10696 nla_put_failure:
@@ -10672,8 +10747,8 @@ nl80211_pmksa_candidate_notify(struct cfg80211_registered_device *rdev,
10672 10747
10673 genlmsg_end(msg, hdr); 10748 genlmsg_end(msg, hdr);
10674 10749
10675 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10750 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10676 nl80211_mlme_mcgrp.id, gfp); 10751 NL80211_MCGRP_MLME, gfp);
10677 return; 10752 return;
10678 10753
10679 nla_put_failure: 10754 nla_put_failure:
@@ -10719,8 +10794,8 @@ static void nl80211_ch_switch_notify(struct cfg80211_registered_device *rdev,
10719 10794
10720 genlmsg_end(msg, hdr); 10795 genlmsg_end(msg, hdr);
10721 10796
10722 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10797 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10723 nl80211_mlme_mcgrp.id, gfp); 10798 NL80211_MCGRP_MLME, gfp);
10724 return; 10799 return;
10725 10800
10726 nla_put_failure: 10801 nla_put_failure:
@@ -10740,7 +10815,9 @@ void cfg80211_ch_switch_notify(struct net_device *dev,
10740 wdev_lock(wdev); 10815 wdev_lock(wdev);
10741 10816
10742 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP && 10817 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10743 wdev->iftype != NL80211_IFTYPE_P2P_GO)) 10818 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10819 wdev->iftype != NL80211_IFTYPE_ADHOC &&
10820 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10744 goto out; 10821 goto out;
10745 10822
10746 wdev->channel = chandef->chan; 10823 wdev->channel = chandef->chan;
@@ -10794,8 +10871,8 @@ void cfg80211_cqm_txe_notify(struct net_device *dev,
10794 10871
10795 genlmsg_end(msg, hdr); 10872 genlmsg_end(msg, hdr);
10796 10873
10797 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10874 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10798 nl80211_mlme_mcgrp.id, gfp); 10875 NL80211_MCGRP_MLME, gfp);
10799 return; 10876 return;
10800 10877
10801 nla_put_failure: 10878 nla_put_failure:
@@ -10843,8 +10920,8 @@ nl80211_radar_notify(struct cfg80211_registered_device *rdev,
10843 10920
10844 genlmsg_end(msg, hdr); 10921 genlmsg_end(msg, hdr);
10845 10922
10846 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10923 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10847 nl80211_mlme_mcgrp.id, gfp); 10924 NL80211_MCGRP_MLME, gfp);
10848 return; 10925 return;
10849 10926
10850 nla_put_failure: 10927 nla_put_failure:
@@ -10890,8 +10967,8 @@ void cfg80211_cqm_pktloss_notify(struct net_device *dev,
10890 10967
10891 genlmsg_end(msg, hdr); 10968 genlmsg_end(msg, hdr);
10892 10969
10893 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 10970 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10894 nl80211_mlme_mcgrp.id, gfp); 10971 NL80211_MCGRP_MLME, gfp);
10895 return; 10972 return;
10896 10973
10897 nla_put_failure: 10974 nla_put_failure:
@@ -10930,8 +11007,8 @@ void cfg80211_probe_status(struct net_device *dev, const u8 *addr,
10930 11007
10931 genlmsg_end(msg, hdr); 11008 genlmsg_end(msg, hdr);
10932 11009
10933 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 11010 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
10934 nl80211_mlme_mcgrp.id, gfp); 11011 NL80211_MCGRP_MLME, gfp);
10935 return; 11012 return;
10936 11013
10937 nla_put_failure: 11014 nla_put_failure:
@@ -11021,6 +11098,8 @@ void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11021 struct nlattr *reasons; 11098 struct nlattr *reasons;
11022 11099
11023 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS); 11100 reasons = nla_nest_start(msg, NL80211_ATTR_WOWLAN_TRIGGERS);
11101 if (!reasons)
11102 goto free_msg;
11024 11103
11025 if (wakeup->disconnect && 11104 if (wakeup->disconnect &&
11026 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT)) 11105 nla_put_flag(msg, NL80211_WOWLAN_TRIG_DISCONNECT))
@@ -11046,16 +11125,18 @@ void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11046 wakeup->pattern_idx)) 11125 wakeup->pattern_idx))
11047 goto free_msg; 11126 goto free_msg;
11048 11127
11049 if (wakeup->tcp_match) 11128 if (wakeup->tcp_match &&
11050 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH); 11129 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_MATCH))
11130 goto free_msg;
11051 11131
11052 if (wakeup->tcp_connlost) 11132 if (wakeup->tcp_connlost &&
11053 nla_put_flag(msg, 11133 nla_put_flag(msg, NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST))
11054 NL80211_WOWLAN_TRIG_WAKEUP_TCP_CONNLOST); 11134 goto free_msg;
11055 11135
11056 if (wakeup->tcp_nomoretokens) 11136 if (wakeup->tcp_nomoretokens &&
11057 nla_put_flag(msg, 11137 nla_put_flag(msg,
11058 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS); 11138 NL80211_WOWLAN_TRIG_WAKEUP_TCP_NOMORETOKENS))
11139 goto free_msg;
11059 11140
11060 if (wakeup->packet) { 11141 if (wakeup->packet) {
11061 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211; 11142 u32 pkt_attr = NL80211_WOWLAN_TRIG_WAKEUP_PKT_80211;
@@ -11082,8 +11163,8 @@ void cfg80211_report_wowlan_wakeup(struct wireless_dev *wdev,
11082 11163
11083 genlmsg_end(msg, hdr); 11164 genlmsg_end(msg, hdr);
11084 11165
11085 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 11166 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11086 nl80211_mlme_mcgrp.id, gfp); 11167 NL80211_MCGRP_MLME, gfp);
11087 return; 11168 return;
11088 11169
11089 free_msg: 11170 free_msg:
@@ -11124,8 +11205,8 @@ void cfg80211_tdls_oper_request(struct net_device *dev, const u8 *peer,
11124 11205
11125 genlmsg_end(msg, hdr); 11206 genlmsg_end(msg, hdr);
11126 11207
11127 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 11208 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11128 nl80211_mlme_mcgrp.id, gfp); 11209 NL80211_MCGRP_MLME, gfp);
11129 return; 11210 return;
11130 11211
11131 nla_put_failure: 11212 nla_put_failure:
@@ -11191,24 +11272,29 @@ void cfg80211_ft_event(struct net_device *netdev,
11191 return; 11272 return;
11192 11273
11193 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT); 11274 hdr = nl80211hdr_put(msg, 0, 0, 0, NL80211_CMD_FT_EVENT);
11194 if (!hdr) { 11275 if (!hdr)
11195 nlmsg_free(msg); 11276 goto out;
11196 return;
11197 }
11198 11277
11199 nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx); 11278 if (nla_put_u32(msg, NL80211_ATTR_WIPHY, rdev->wiphy_idx) ||
11200 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex); 11279 nla_put_u32(msg, NL80211_ATTR_IFINDEX, netdev->ifindex) ||
11201 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap); 11280 nla_put(msg, NL80211_ATTR_MAC, ETH_ALEN, ft_event->target_ap))
11202 if (ft_event->ies) 11281 goto out;
11203 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies); 11282
11204 if (ft_event->ric_ies) 11283 if (ft_event->ies &&
11205 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len, 11284 nla_put(msg, NL80211_ATTR_IE, ft_event->ies_len, ft_event->ies))
11206 ft_event->ric_ies); 11285 goto out;
11286 if (ft_event->ric_ies &&
11287 nla_put(msg, NL80211_ATTR_IE_RIC, ft_event->ric_ies_len,
11288 ft_event->ric_ies))
11289 goto out;
11207 11290
11208 genlmsg_end(msg, hdr); 11291 genlmsg_end(msg, hdr);
11209 11292
11210 genlmsg_multicast_netns(wiphy_net(&rdev->wiphy), msg, 0, 11293 genlmsg_multicast_netns(&nl80211_fam, wiphy_net(&rdev->wiphy), msg, 0,
11211 nl80211_mlme_mcgrp.id, GFP_KERNEL); 11294 NL80211_MCGRP_MLME, GFP_KERNEL);
11295 return;
11296 out:
11297 nlmsg_free(msg);
11212} 11298}
11213EXPORT_SYMBOL(cfg80211_ft_event); 11299EXPORT_SYMBOL(cfg80211_ft_event);
11214 11300
@@ -11257,33 +11343,11 @@ int nl80211_init(void)
11257{ 11343{
11258 int err; 11344 int err;
11259 11345
11260 err = genl_register_family_with_ops(&nl80211_fam, 11346 err = genl_register_family_with_ops_groups(&nl80211_fam, nl80211_ops,
11261 nl80211_ops, ARRAY_SIZE(nl80211_ops)); 11347 nl80211_mcgrps);
11262 if (err) 11348 if (err)
11263 return err; 11349 return err;
11264 11350
11265 err = genl_register_mc_group(&nl80211_fam, &nl80211_config_mcgrp);
11266 if (err)
11267 goto err_out;
11268
11269 err = genl_register_mc_group(&nl80211_fam, &nl80211_scan_mcgrp);
11270 if (err)
11271 goto err_out;
11272
11273 err = genl_register_mc_group(&nl80211_fam, &nl80211_regulatory_mcgrp);
11274 if (err)
11275 goto err_out;
11276
11277 err = genl_register_mc_group(&nl80211_fam, &nl80211_mlme_mcgrp);
11278 if (err)
11279 goto err_out;
11280
11281#ifdef CONFIG_NL80211_TESTMODE
11282 err = genl_register_mc_group(&nl80211_fam, &nl80211_testmode_mcgrp);
11283 if (err)
11284 goto err_out;
11285#endif
11286
11287 err = netlink_register_notifier(&nl80211_netlink_notifier); 11351 err = netlink_register_notifier(&nl80211_netlink_notifier);
11288 if (err) 11352 if (err)
11289 goto err_out; 11353 goto err_out;
diff --git a/net/wireless/radiotap.c b/net/wireless/radiotap.c
index a271c27fac77..722da616438c 100644
--- a/net/wireless/radiotap.c
+++ b/net/wireless/radiotap.c
@@ -124,6 +124,10 @@ int ieee80211_radiotap_iterator_init(
124 /* find payload start allowing for extended bitmap(s) */ 124 /* find payload start allowing for extended bitmap(s) */
125 125
126 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) { 126 if (iterator->_bitmap_shifter & (1<<IEEE80211_RADIOTAP_EXT)) {
127 if ((unsigned long)iterator->_arg -
128 (unsigned long)iterator->_rtheader + sizeof(uint32_t) >
129 (unsigned long)iterator->_max_length)
130 return -EINVAL;
127 while (get_unaligned_le32(iterator->_arg) & 131 while (get_unaligned_le32(iterator->_arg) &
128 (1 << IEEE80211_RADIOTAP_EXT)) { 132 (1 << IEEE80211_RADIOTAP_EXT)) {
129 iterator->_arg += sizeof(uint32_t); 133 iterator->_arg += sizeof(uint32_t);
diff --git a/net/wireless/reg.c b/net/wireless/reg.c
index de06d5d1287f..7da67fd0b418 100644
--- a/net/wireless/reg.c
+++ b/net/wireless/reg.c
@@ -172,11 +172,21 @@ static const struct ieee80211_regdomain world_regdom = {
172 NL80211_RRF_NO_IBSS | 172 NL80211_RRF_NO_IBSS |
173 NL80211_RRF_NO_OFDM), 173 NL80211_RRF_NO_OFDM),
174 /* IEEE 802.11a, channel 36..48 */ 174 /* IEEE 802.11a, channel 36..48 */
175 REG_RULE(5180-10, 5240+10, 80, 6, 20, 175 REG_RULE(5180-10, 5240+10, 160, 6, 20,
176 NL80211_RRF_PASSIVE_SCAN | 176 NL80211_RRF_PASSIVE_SCAN |
177 NL80211_RRF_NO_IBSS), 177 NL80211_RRF_NO_IBSS),
178 178
179 /* NB: 5260 MHz - 5700 MHz requires DFS */ 179 /* IEEE 802.11a, channel 52..64 - DFS required */
180 REG_RULE(5260-10, 5320+10, 160, 6, 20,
181 NL80211_RRF_PASSIVE_SCAN |
182 NL80211_RRF_NO_IBSS |
183 NL80211_RRF_DFS),
184
185 /* IEEE 802.11a, channel 100..144 - DFS required */
186 REG_RULE(5500-10, 5720+10, 160, 6, 20,
187 NL80211_RRF_PASSIVE_SCAN |
188 NL80211_RRF_NO_IBSS |
189 NL80211_RRF_DFS),
180 190
181 /* IEEE 802.11a, channel 149..165 */ 191 /* IEEE 802.11a, channel 149..165 */
182 REG_RULE(5745-10, 5825+10, 80, 6, 20, 192 REG_RULE(5745-10, 5825+10, 80, 6, 20,
@@ -758,24 +768,25 @@ const struct ieee80211_reg_rule *freq_reg_info(struct wiphy *wiphy,
758} 768}
759EXPORT_SYMBOL(freq_reg_info); 769EXPORT_SYMBOL(freq_reg_info);
760 770
761#ifdef CONFIG_CFG80211_REG_DEBUG 771const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
762static const char *reg_initiator_name(enum nl80211_reg_initiator initiator)
763{ 772{
764 switch (initiator) { 773 switch (initiator) {
765 case NL80211_REGDOM_SET_BY_CORE: 774 case NL80211_REGDOM_SET_BY_CORE:
766 return "Set by core"; 775 return "core";
767 case NL80211_REGDOM_SET_BY_USER: 776 case NL80211_REGDOM_SET_BY_USER:
768 return "Set by user"; 777 return "user";
769 case NL80211_REGDOM_SET_BY_DRIVER: 778 case NL80211_REGDOM_SET_BY_DRIVER:
770 return "Set by driver"; 779 return "driver";
771 case NL80211_REGDOM_SET_BY_COUNTRY_IE: 780 case NL80211_REGDOM_SET_BY_COUNTRY_IE:
772 return "Set by country IE"; 781 return "country IE";
773 default: 782 default:
774 WARN_ON(1); 783 WARN_ON(1);
775 return "Set by bug"; 784 return "bug";
776 } 785 }
777} 786}
787EXPORT_SYMBOL(reg_initiator_name);
778 788
789#ifdef CONFIG_CFG80211_REG_DEBUG
779static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan, 790static void chan_reg_rule_print_dbg(struct ieee80211_channel *chan,
780 const struct ieee80211_reg_rule *reg_rule) 791 const struct ieee80211_reg_rule *reg_rule)
781{ 792{
@@ -962,6 +973,13 @@ static bool reg_dev_ignore_cell_hint(struct wiphy *wiphy)
962} 973}
963#endif 974#endif
964 975
976static bool wiphy_strict_alpha2_regd(struct wiphy *wiphy)
977{
978 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY &&
979 !(wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY))
980 return true;
981 return false;
982}
965 983
966static bool ignore_reg_update(struct wiphy *wiphy, 984static bool ignore_reg_update(struct wiphy *wiphy,
967 enum nl80211_reg_initiator initiator) 985 enum nl80211_reg_initiator initiator)
@@ -969,14 +987,17 @@ static bool ignore_reg_update(struct wiphy *wiphy,
969 struct regulatory_request *lr = get_last_request(); 987 struct regulatory_request *lr = get_last_request();
970 988
971 if (!lr) { 989 if (!lr) {
972 REG_DBG_PRINT("Ignoring regulatory request %s since last_request is not set\n", 990 REG_DBG_PRINT("Ignoring regulatory request set by %s "
991 "since last_request is not set\n",
973 reg_initiator_name(initiator)); 992 reg_initiator_name(initiator));
974 return true; 993 return true;
975 } 994 }
976 995
977 if (initiator == NL80211_REGDOM_SET_BY_CORE && 996 if (initiator == NL80211_REGDOM_SET_BY_CORE &&
978 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) { 997 wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
979 REG_DBG_PRINT("Ignoring regulatory request %s since the driver uses its own custom regulatory domain\n", 998 REG_DBG_PRINT("Ignoring regulatory request set by %s "
999 "since the driver uses its own custom "
1000 "regulatory domain\n",
980 reg_initiator_name(initiator)); 1001 reg_initiator_name(initiator));
981 return true; 1002 return true;
982 } 1003 }
@@ -985,10 +1006,12 @@ static bool ignore_reg_update(struct wiphy *wiphy,
985 * wiphy->regd will be set once the device has its own 1006 * wiphy->regd will be set once the device has its own
986 * desired regulatory domain set 1007 * desired regulatory domain set
987 */ 1008 */
988 if (wiphy->flags & WIPHY_FLAG_STRICT_REGULATORY && !wiphy->regd && 1009 if (wiphy_strict_alpha2_regd(wiphy) && !wiphy->regd &&
989 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE && 1010 initiator != NL80211_REGDOM_SET_BY_COUNTRY_IE &&
990 !is_world_regdom(lr->alpha2)) { 1011 !is_world_regdom(lr->alpha2)) {
991 REG_DBG_PRINT("Ignoring regulatory request %s since the driver requires its own regulatory domain to be set first\n", 1012 REG_DBG_PRINT("Ignoring regulatory request set by %s "
1013 "since the driver requires its own regulatory "
1014 "domain to be set first\n",
992 reg_initiator_name(initiator)); 1015 reg_initiator_name(initiator));
993 return true; 1016 return true;
994 } 1017 }
@@ -1689,8 +1712,8 @@ int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
1689} 1712}
1690EXPORT_SYMBOL(regulatory_hint); 1713EXPORT_SYMBOL(regulatory_hint);
1691 1714
1692void regulatory_hint_11d(struct wiphy *wiphy, enum ieee80211_band band, 1715void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
1693 const u8 *country_ie, u8 country_ie_len) 1716 const u8 *country_ie, u8 country_ie_len)
1694{ 1717{
1695 char alpha2[2]; 1718 char alpha2[2];
1696 enum environment_cap env = ENVIRON_ANY; 1719 enum environment_cap env = ENVIRON_ANY;
diff --git a/net/wireless/reg.h b/net/wireless/reg.h
index af2d5f8a5d82..9677e3c13da9 100644
--- a/net/wireless/reg.h
+++ b/net/wireless/reg.h
@@ -58,7 +58,7 @@ int regulatory_hint_found_beacon(struct wiphy *wiphy,
58 gfp_t gfp); 58 gfp_t gfp);
59 59
60/** 60/**
61 * regulatory_hint_11d - hints a country IE as a regulatory domain 61 * regulatory_hint_country_ie - hints a country IE as a regulatory domain
62 * @wiphy: the wireless device giving the hint (used only for reporting 62 * @wiphy: the wireless device giving the hint (used only for reporting
63 * conflicts) 63 * conflicts)
64 * @band: the band on which the country IE was received on. This determines 64 * @band: the band on which the country IE was received on. This determines
@@ -78,7 +78,7 @@ int regulatory_hint_found_beacon(struct wiphy *wiphy,
78 * not observed. For this reason if a triplet is seen with channel 78 * not observed. For this reason if a triplet is seen with channel
79 * information for a band the BSS is not present in it will be ignored. 79 * information for a band the BSS is not present in it will be ignored.
80 */ 80 */
81void regulatory_hint_11d(struct wiphy *wiphy, 81void regulatory_hint_country_ie(struct wiphy *wiphy,
82 enum ieee80211_band band, 82 enum ieee80211_band band,
83 const u8 *country_ie, 83 const u8 *country_ie,
84 u8 country_ie_len); 84 u8 country_ie_len);
diff --git a/net/wireless/scan.c b/net/wireless/scan.c
index eeb71480f1af..d4397eba5408 100644
--- a/net/wireless/scan.c
+++ b/net/wireless/scan.c
@@ -254,10 +254,10 @@ void __cfg80211_sched_scan_results(struct work_struct *wk)
254 rdev = container_of(wk, struct cfg80211_registered_device, 254 rdev = container_of(wk, struct cfg80211_registered_device,
255 sched_scan_results_wk); 255 sched_scan_results_wk);
256 256
257 request = rdev->sched_scan_req;
258
259 rtnl_lock(); 257 rtnl_lock();
260 258
259 request = rdev->sched_scan_req;
260
261 /* we don't have sched_scan_req anymore if the scan is stopping */ 261 /* we don't have sched_scan_req anymore if the scan is stopping */
262 if (request) { 262 if (request) {
263 if (request->flags & NL80211_SCAN_FLAG_FLUSH) { 263 if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
diff --git a/net/wireless/sme.c b/net/wireless/sme.c
index 20e86a95dc4e..d3c5bd7c6b51 100644
--- a/net/wireless/sme.c
+++ b/net/wireless/sme.c
@@ -632,6 +632,16 @@ void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
632 } 632 }
633#endif 633#endif
634 634
635 if (!bss && (status == WLAN_STATUS_SUCCESS)) {
636 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect);
637 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
638 wdev->ssid, wdev->ssid_len,
639 WLAN_CAPABILITY_ESS,
640 WLAN_CAPABILITY_ESS);
641 if (bss)
642 cfg80211_hold_bss(bss_from_pub(bss));
643 }
644
635 if (wdev->current_bss) { 645 if (wdev->current_bss) {
636 cfg80211_unhold_bss(wdev->current_bss); 646 cfg80211_unhold_bss(wdev->current_bss);
637 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub); 647 cfg80211_put_bss(wdev->wiphy, &wdev->current_bss->pub);
@@ -649,16 +659,8 @@ void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
649 return; 659 return;
650 } 660 }
651 661
652 if (!bss) { 662 if (WARN_ON(!bss))
653 WARN_ON_ONCE(!wiphy_to_dev(wdev->wiphy)->ops->connect); 663 return;
654 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
655 wdev->ssid, wdev->ssid_len,
656 WLAN_CAPABILITY_ESS,
657 WLAN_CAPABILITY_ESS);
658 if (WARN_ON(!bss))
659 return;
660 cfg80211_hold_bss(bss_from_pub(bss));
661 }
662 664
663 wdev->current_bss = bss_from_pub(bss); 665 wdev->current_bss = bss_from_pub(bss);
664 666
@@ -682,8 +684,8 @@ void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
682 * - country_ie + 2, the start of the country ie data, and 684 * - country_ie + 2, the start of the country ie data, and
683 * - and country_ie[1] which is the IE length 685 * - and country_ie[1] which is the IE length
684 */ 686 */
685 regulatory_hint_11d(wdev->wiphy, bss->channel->band, 687 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
686 country_ie + 2, country_ie[1]); 688 country_ie + 2, country_ie[1]);
687 kfree(country_ie); 689 kfree(country_ie);
688} 690}
689 691
diff --git a/net/wireless/sysfs.h b/net/wireless/sysfs.h
index 65acbebd3711..b533ed71daff 100644
--- a/net/wireless/sysfs.h
+++ b/net/wireless/sysfs.h
@@ -1,8 +1,8 @@
1#ifndef __WIRELESS_SYSFS_H 1#ifndef __WIRELESS_SYSFS_H
2#define __WIRELESS_SYSFS_H 2#define __WIRELESS_SYSFS_H
3 3
4extern int wiphy_sysfs_init(void); 4int wiphy_sysfs_init(void);
5extern void wiphy_sysfs_exit(void); 5void wiphy_sysfs_exit(void);
6 6
7extern struct class ieee80211_class; 7extern struct class ieee80211_class;
8 8
diff --git a/net/wireless/util.c b/net/wireless/util.c
index ce090c1c5e4f..935dea9485da 100644
--- a/net/wireless/util.c
+++ b/net/wireless/util.c
@@ -10,6 +10,7 @@
10#include <net/cfg80211.h> 10#include <net/cfg80211.h>
11#include <net/ip.h> 11#include <net/ip.h>
12#include <net/dsfield.h> 12#include <net/dsfield.h>
13#include <linux/if_vlan.h>
13#include "core.h" 14#include "core.h"
14#include "rdev-ops.h" 15#include "rdev-ops.h"
15 16
@@ -691,6 +692,7 @@ EXPORT_SYMBOL(ieee80211_amsdu_to_8023s);
691unsigned int cfg80211_classify8021d(struct sk_buff *skb) 692unsigned int cfg80211_classify8021d(struct sk_buff *skb)
692{ 693{
693 unsigned int dscp; 694 unsigned int dscp;
695 unsigned char vlan_priority;
694 696
695 /* skb->priority values from 256->263 are magic values to 697 /* skb->priority values from 256->263 are magic values to
696 * directly indicate a specific 802.1d priority. This is used 698 * directly indicate a specific 802.1d priority. This is used
@@ -700,6 +702,13 @@ unsigned int cfg80211_classify8021d(struct sk_buff *skb)
700 if (skb->priority >= 256 && skb->priority <= 263) 702 if (skb->priority >= 256 && skb->priority <= 263)
701 return skb->priority - 256; 703 return skb->priority - 256;
702 704
705 if (vlan_tx_tag_present(skb)) {
706 vlan_priority = (vlan_tx_tag_get(skb) & VLAN_PRIO_MASK)
707 >> VLAN_PRIO_SHIFT;
708 if (vlan_priority > 0)
709 return vlan_priority;
710 }
711
703 switch (skb->protocol) { 712 switch (skb->protocol) {
704 case htons(ETH_P_IP): 713 case htons(ETH_P_IP):
705 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc; 714 dscp = ipv4_get_dsfield(ip_hdr(skb)) & 0xfc;
@@ -1240,7 +1249,7 @@ int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1240 enum cfg80211_chan_mode chmode; 1249 enum cfg80211_chan_mode chmode;
1241 int num_different_channels = 0; 1250 int num_different_channels = 0;
1242 int total = 1; 1251 int total = 1;
1243 bool radar_required; 1252 bool radar_required = false;
1244 int i, j; 1253 int i, j;
1245 1254
1246 ASSERT_RTNL(); 1255 ASSERT_RTNL();
@@ -1255,14 +1264,20 @@ int cfg80211_can_use_iftype_chan(struct cfg80211_registered_device *rdev,
1255 case NL80211_IFTYPE_MESH_POINT: 1264 case NL80211_IFTYPE_MESH_POINT:
1256 case NL80211_IFTYPE_P2P_GO: 1265 case NL80211_IFTYPE_P2P_GO:
1257 case NL80211_IFTYPE_WDS: 1266 case NL80211_IFTYPE_WDS:
1258 radar_required = !!(chan && 1267 /* if the interface could potentially choose a DFS channel,
1259 (chan->flags & IEEE80211_CHAN_RADAR)); 1268 * then mark DFS as required.
1269 */
1270 if (!chan) {
1271 if (chanmode != CHAN_MODE_UNDEFINED && radar_detect)
1272 radar_required = true;
1273 break;
1274 }
1275 radar_required = !!(chan->flags & IEEE80211_CHAN_RADAR);
1260 break; 1276 break;
1261 case NL80211_IFTYPE_P2P_CLIENT: 1277 case NL80211_IFTYPE_P2P_CLIENT:
1262 case NL80211_IFTYPE_STATION: 1278 case NL80211_IFTYPE_STATION:
1263 case NL80211_IFTYPE_P2P_DEVICE: 1279 case NL80211_IFTYPE_P2P_DEVICE:
1264 case NL80211_IFTYPE_MONITOR: 1280 case NL80211_IFTYPE_MONITOR:
1265 radar_required = false;
1266 break; 1281 break;
1267 case NUM_NL80211_IFTYPES: 1282 case NUM_NL80211_IFTYPES:
1268 case NL80211_IFTYPE_UNSPECIFIED: 1283 case NL80211_IFTYPE_UNSPECIFIED:
diff --git a/net/x25/Kconfig b/net/x25/Kconfig
index c959312c45e3..e2fa133f9fba 100644
--- a/net/x25/Kconfig
+++ b/net/x25/Kconfig
@@ -16,8 +16,8 @@ config X25
16 if you want that) and the lower level data link layer protocol LAPB 16 if you want that) and the lower level data link layer protocol LAPB
17 (say Y to "LAPB Data Link Driver" below if you want that). 17 (say Y to "LAPB Data Link Driver" below if you want that).
18 18
19 You can read more about X.25 at <http://www.sangoma.com/x25.htm> and 19 You can read more about X.25 at <http://www.sangoma.com/tutorials/x25/> and
20 <http://www.cisco.com/univercd/cc/td/doc/product/software/ios11/cbook/cx25.htm>. 20 <http://docwiki.cisco.com/wiki/X.25>.
21 Information about X.25 for Linux is contained in the files 21 Information about X.25 for Linux is contained in the files
22 <file:Documentation/networking/x25.txt> and 22 <file:Documentation/networking/x25.txt> and
23 <file:Documentation/networking/x25-iface.txt>. 23 <file:Documentation/networking/x25-iface.txt>.
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index 45a3ab5612c1..7622789d3750 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -1340,10 +1340,9 @@ static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1340 if (sx25) { 1340 if (sx25) {
1341 sx25->sx25_family = AF_X25; 1341 sx25->sx25_family = AF_X25;
1342 sx25->sx25_addr = x25->dest_addr; 1342 sx25->sx25_addr = x25->dest_addr;
1343 msg->msg_namelen = sizeof(*sx25);
1343 } 1344 }
1344 1345
1345 msg->msg_namelen = sizeof(struct sockaddr_x25);
1346
1347 x25_check_rbuf(sk); 1346 x25_check_rbuf(sk);
1348 rc = copied; 1347 rc = copied;
1349out_free_dgram: 1348out_free_dgram:
diff --git a/net/xfrm/xfrm_algo.c b/net/xfrm/xfrm_algo.c
index ab4ef72f0b1d..debe733386f8 100644
--- a/net/xfrm/xfrm_algo.c
+++ b/net/xfrm/xfrm_algo.c
@@ -802,17 +802,4 @@ int xfrm_count_pfkey_enc_supported(void)
802} 802}
803EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported); 803EXPORT_SYMBOL_GPL(xfrm_count_pfkey_enc_supported);
804 804
805#if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
806
807void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
808{
809 if (tail != skb) {
810 skb->data_len += len;
811 skb->len += len;
812 }
813 return skb_put(tail, len);
814}
815EXPORT_SYMBOL_GPL(pskb_put);
816#endif
817
818MODULE_LICENSE("GPL"); 805MODULE_LICENSE("GPL");
diff --git a/net/xfrm/xfrm_hash.h b/net/xfrm/xfrm_hash.h
index 716502ada53b..0622d319e1f2 100644
--- a/net/xfrm/xfrm_hash.h
+++ b/net/xfrm/xfrm_hash.h
@@ -130,7 +130,7 @@ static inline unsigned int __addr_hash(const xfrm_address_t *daddr,
130 return h & hmask; 130 return h & hmask;
131} 131}
132 132
133extern struct hlist_head *xfrm_hash_alloc(unsigned int sz); 133struct hlist_head *xfrm_hash_alloc(unsigned int sz);
134extern void xfrm_hash_free(struct hlist_head *n, unsigned int sz); 134void xfrm_hash_free(struct hlist_head *n, unsigned int sz);
135 135
136#endif /* _XFRM_HASH_H */ 136#endif /* _XFRM_HASH_H */
diff --git a/net/xfrm/xfrm_ipcomp.c b/net/xfrm/xfrm_ipcomp.c
index 2906d520eea7..ccfdc7115a83 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -141,14 +141,14 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
141 const int plen = skb->len; 141 const int plen = skb->len;
142 int dlen = IPCOMP_SCRATCH_SIZE; 142 int dlen = IPCOMP_SCRATCH_SIZE;
143 u8 *start = skb->data; 143 u8 *start = skb->data;
144 const int cpu = get_cpu(); 144 struct crypto_comp *tfm;
145 u8 *scratch = *per_cpu_ptr(ipcomp_scratches, cpu); 145 u8 *scratch;
146 struct crypto_comp *tfm = *per_cpu_ptr(ipcd->tfms, cpu);
147 int err; 146 int err;
148 147
149 local_bh_disable(); 148 local_bh_disable();
149 scratch = *this_cpu_ptr(ipcomp_scratches);
150 tfm = *this_cpu_ptr(ipcd->tfms);
150 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen); 151 err = crypto_comp_compress(tfm, start, plen, scratch, &dlen);
151 local_bh_enable();
152 if (err) 152 if (err)
153 goto out; 153 goto out;
154 154
@@ -158,13 +158,13 @@ static int ipcomp_compress(struct xfrm_state *x, struct sk_buff *skb)
158 } 158 }
159 159
160 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen); 160 memcpy(start + sizeof(struct ip_comp_hdr), scratch, dlen);
161 put_cpu(); 161 local_bh_enable();
162 162
163 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr)); 163 pskb_trim(skb, dlen + sizeof(struct ip_comp_hdr));
164 return 0; 164 return 0;
165 165
166out: 166out:
167 put_cpu(); 167 local_bh_enable();
168 return err; 168 return err;
169} 169}
170 170
@@ -220,8 +220,8 @@ static void ipcomp_free_scratches(void)
220 220
221static void * __percpu *ipcomp_alloc_scratches(void) 221static void * __percpu *ipcomp_alloc_scratches(void)
222{ 222{
223 int i;
224 void * __percpu *scratches; 223 void * __percpu *scratches;
224 int i;
225 225
226 if (ipcomp_scratch_users++) 226 if (ipcomp_scratch_users++)
227 return ipcomp_scratches; 227 return ipcomp_scratches;
@@ -233,7 +233,9 @@ static void * __percpu *ipcomp_alloc_scratches(void)
233 ipcomp_scratches = scratches; 233 ipcomp_scratches = scratches;
234 234
235 for_each_possible_cpu(i) { 235 for_each_possible_cpu(i) {
236 void *scratch = vmalloc(IPCOMP_SCRATCH_SIZE); 236 void *scratch;
237
238 scratch = vmalloc_node(IPCOMP_SCRATCH_SIZE, cpu_to_node(i));
237 if (!scratch) 239 if (!scratch)
238 return NULL; 240 return NULL;
239 *per_cpu_ptr(scratches, i) = scratch; 241 *per_cpu_ptr(scratches, i) = scratch;
diff --git a/net/xfrm/xfrm_policy.c b/net/xfrm/xfrm_policy.c
index 76e1873811d4..9a91f7431c41 100644
--- a/net/xfrm/xfrm_policy.c
+++ b/net/xfrm/xfrm_policy.c
@@ -1844,6 +1844,13 @@ static int xdst_queue_output(struct sk_buff *skb)
1844 struct xfrm_dst *xdst = (struct xfrm_dst *) dst; 1844 struct xfrm_dst *xdst = (struct xfrm_dst *) dst;
1845 struct xfrm_policy *pol = xdst->pols[0]; 1845 struct xfrm_policy *pol = xdst->pols[0];
1846 struct xfrm_policy_queue *pq = &pol->polq; 1846 struct xfrm_policy_queue *pq = &pol->polq;
1847 const struct sk_buff *fclone = skb + 1;
1848
1849 if (unlikely(skb->fclone == SKB_FCLONE_ORIG &&
1850 fclone->fclone == SKB_FCLONE_CLONE)) {
1851 kfree_skb(skb);
1852 return 0;
1853 }
1847 1854
1848 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) { 1855 if (pq->hold_queue.qlen > XFRM_MAX_QUEUE_LEN) {
1849 kfree_skb(skb); 1856 kfree_skb(skb);
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index b9c3f9e943a9..68c2f357a183 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -468,7 +468,7 @@ expired:
468 } 468 }
469 469
470 err = __xfrm_state_delete(x); 470 err = __xfrm_state_delete(x);
471 if (!err && x->id.spi) 471 if (!err)
472 km_state_expired(x, 1, 0); 472 km_state_expired(x, 1, 0);
473 473
474 xfrm_audit_state_delete(x, err ? 0 : 1, 474 xfrm_audit_state_delete(x, err ? 0 : 1,
@@ -815,7 +815,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
815 xfrm_state_look_at(pol, x, fl, encap_family, 815 xfrm_state_look_at(pol, x, fl, encap_family,
816 &best, &acquire_in_progress, &error); 816 &best, &acquire_in_progress, &error);
817 } 817 }
818 if (best) 818 if (best || acquire_in_progress)
819 goto found; 819 goto found;
820 820
821 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family); 821 h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
@@ -824,7 +824,7 @@ xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
824 x->props.reqid == tmpl->reqid && 824 x->props.reqid == tmpl->reqid &&
825 (mark & x->mark.m) == x->mark.v && 825 (mark & x->mark.m) == x->mark.v &&
826 !(x->props.flags & XFRM_STATE_WILDRECV) && 826 !(x->props.flags & XFRM_STATE_WILDRECV) &&
827 xfrm_state_addr_check(x, daddr, saddr, encap_family) && 827 xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
828 tmpl->mode == x->props.mode && 828 tmpl->mode == x->props.mode &&
829 tmpl->id.proto == x->id.proto && 829 tmpl->id.proto == x->id.proto &&
830 (tmpl->id.spi == x->id.spi || !tmpl->id.spi)) 830 (tmpl->id.spi == x->id.spi || !tmpl->id.spi))