aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorIngo Molnar <mingo@kernel.org>2013-11-14 02:28:30 -0500
committerIngo Molnar <mingo@kernel.org>2013-11-14 02:28:30 -0500
commit555a098af6086256bc64d3806519f37ccae936e0 (patch)
tree4f13126483fc1202d23f1ba55045fac41d19118e /net
parentd969135aae1434547f41853f0e8eaa622e8b8816 (diff)
parentf0d55cc1a65852e6647d4f5d707c1c9b5471ce3c (diff)
Merge branch 'linus' into perf/urgent
Merge dependencies to apply a fix. Signed-off-by: Ingo Molnar <mingo@kernel.org>
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.c23
-rw-r--r--net/9p/trans_fd.c4
-rw-r--r--net/Kconfig1
-rw-r--r--net/Makefile1
-rw-r--r--net/ax25/af_ax25.c2
-rw-r--r--net/batman-adv/Makefile3
-rw-r--r--net/batman-adv/bat_iv_ogm.c510
-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.c231
-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.c689
-rw-r--r--net/batman-adv/main.h68
-rw-r--r--net/batman-adv/network-coding.c71
-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.h356
-rw-r--r--net/batman-adv/routing.c557
-rw-r--r--net/batman-adv/routing.h13
-rw-r--r--net/batman-adv/send.c239
-rw-r--r--net/batman-adv/send.h53
-rw-r--r--net/batman-adv/soft-interface.c269
-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.c2086
-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.c73
-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.c210
-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.c538
-rw-r--r--net/bluetooth/l2cap_sock.c222
-rw-r--r--net/bluetooth/mgmt.c1609
-rw-r--r--net/bluetooth/rfcomm/core.c33
-rw-r--r--net/bluetooth/rfcomm/sock.c49
-rw-r--r--net/bluetooth/sco.c57
-rw-r--r--net/bluetooth/smp.c98
-rw-r--r--net/bluetooth/smp.h146
-rw-r--r--net/bridge/br_multicast.c4
-rw-r--r--net/bridge/br_netfilter.c24
-rw-r--r--net/bridge/br_private.h300
-rw-r--r--net/bridge/br_private_stp.h24
-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/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/cfpkt_skbuff.c12
-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/core/datagram.c2
-rw-r--r--net/core/dev.c557
-rw-r--r--net/core/dev_addr_lists.c4
-rw-r--r--net/core/ethtool.c3
-rw-r--r--net/core/fib_rules.c3
-rw-r--r--net/core/flow_dissector.c79
-rw-r--r--net/core/iovec.c2
-rw-r--r--net/core/neighbour.c2
-rw-r--r--net/core/net-sysfs.c2
-rw-r--r--net/core/netprio_cgroup.c3
-rw-r--r--net/core/rtnetlink.c12
-rw-r--r--net/core/secure_seq.c16
-rw-r--r--net/core/skbuff.c144
-rw-r--r--net/core/sock.c45
-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.c83
-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/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.c503
-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.c457
-rw-r--r--net/hsr/hsr_netlink.h30
-rw-r--r--net/ieee802154/6lowpan.c50
-rw-r--r--net/ipv4/af_inet.c94
-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_semantics.c2
-rw-r--r--net/ipv4/fib_trie.c15
-rw-r--r--net/ipv4/gre_demux.c29
-rw-r--r--net/ipv4/gre_offload.c3
-rw-r--r--net/ipv4/icmp.c5
-rw-r--r--net/ipv4/inet_connection_sock.c54
-rw-r--r--net/ipv4/inet_diag.c120
-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_output.c33
-rw-r--r--net/ipv4/ip_sockglue.c27
-rw-r--r--net/ipv4/ip_tunnel_core.c33
-rw-r--r--net/ipv4/ip_vti.c67
-rw-r--r--net/ipv4/ipip.c11
-rw-r--r--net/ipv4/netfilter/Kconfig21
-rw-r--r--net/ipv4/netfilter/Makefile6
-rw-r--r--net/ipv4/netfilter/arptable_filter.c5
-rw-r--r--net/ipv4/netfilter/ipt_CLUSTERIP.c112
-rw-r--r--net/ipv4/netfilter/ipt_SYNPROXY.c2
-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.c29
-rw-r--r--net/ipv4/raw.c6
-rw-r--r--net/ipv4/route.c20
-rw-r--r--net/ipv4/syncookies.c80
-rw-r--r--net/ipv4/sysctl_net_ipv4.c137
-rw-r--r--net/ipv4/tcp.c17
-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.c137
-rw-r--r--net/ipv4/tcp_ipv4.c125
-rw-r--r--net/ipv4/tcp_lp.c5
-rw-r--r--net/ipv4/tcp_memcontrol.c90
-rw-r--r--net/ipv4/tcp_metrics.c27
-rw-r--r--net/ipv4/tcp_minisocks.c7
-rw-r--r--net/ipv4/tcp_offload.c8
-rw-r--r--net/ipv4/tcp_output.c33
-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.c235
-rw-r--r--net/ipv4/udp_impl.h36
-rw-r--r--net/ipv4/udp_offload.c1
-rw-r--r--net/ipv4/xfrm4_mode_tunnel.c16
-rw-r--r--net/ipv6/Kconfig29
-rw-r--r--net/ipv6/Makefile1
-rw-r--r--net/ipv6/addrconf.c41
-rw-r--r--net/ipv6/af_inet6.c65
-rw-r--r--net/ipv6/datagram.c25
-rw-r--r--net/ipv6/esp6.c48
-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_offload.c40
-rw-r--r--net/ipv6/ip6_output.c3
-rw-r--r--net/ipv6/ip6_vti.c1056
-rw-r--r--net/ipv6/ipv6_sockglue.c35
-rw-r--r--net/ipv6/netfilter/Kconfig13
-rw-r--r--net/ipv6/netfilter/Makefile5
-rw-r--r--net/ipv6/netfilter/ip6t_REJECT.c7
-rw-r--r--net/ipv6/netfilter/ip6t_SYNPROXY.c2
-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.c2
-rw-r--r--net/ipv6/raw.c17
-rw-r--r--net/ipv6/reassembly.c12
-rw-r--r--net/ipv6/route.c66
-rw-r--r--net/ipv6/sit.c28
-rw-r--r--net/ipv6/syncookies.c75
-rw-r--r--net/ipv6/tcp_ipv6.c113
-rw-r--r--net/ipv6/tcpv6_offload.c2
-rw-r--r--net/ipv6/udp.c88
-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/irda/irnet/irnet.h15
-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_ip6.c16
-rw-r--r--net/l2tp/l2tp_netlink.c4
-rw-r--r--net/l2tp/l2tp_ppp.c12
-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.c234
-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.c594
-rw-r--r--net/mac80211/ieee80211_i.h71
-rw-r--r--net/mac80211/iface.c7
-rw-r--r--net/mac80211/key.c2
-rw-r--r--net/mac80211/key.h2
-rw-r--r--net/mac80211/mesh.c302
-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.c23
-rw-r--r--net/mac80211/rc80211_pid_debugfs.c26
-rw-r--r--net/mac80211/rx.c42
-rw-r--r--net/mac80211/scan.c3
-rw-r--r--net/mac80211/spectmgmt.c169
-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.c84
-rw-r--r--net/mac80211/util.c286
-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/Kconfig52
-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.c6
-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_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/nf_conntrack_acct.c12
-rw-r--r--net/netfilter/nf_conntrack_core.c16
-rw-r--r--net/netfilter/nf_conntrack_netlink.c51
-rw-r--r--net/netfilter/nf_conntrack_sip.c133
-rw-r--r--net/netfilter/nf_internals.h28
-rw-r--r--net/netfilter/nf_nat_core.c20
-rw-r--r--net/netfilter/nf_nat_sip.c35
-rw-r--r--net/netfilter/nf_tables_api.c3275
-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.c11
-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.c768
-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/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_set.c222
-rw-r--r--net/netfilter/xt_socket.c15
-rw-r--r--net/netlabel/netlabel_kapi.c2
-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/nci/spi.c239
-rw-r--r--net/nfc/netlink.c91
-rw-r--r--net/nfc/rawsock.c7
-rw-r--r--net/openvswitch/Makefile2
-rw-r--r--net/openvswitch/datapath.c668
-rw-r--r--net/openvswitch/datapath.h9
-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-vxlan.c3
-rw-r--r--net/rds/connection.c12
-rw-r--r--net/rds/rds.h2
-rw-r--r--net/rfkill/Kconfig2
-rw-r--r--net/rfkill/rfkill-gpio.c211
-rw-r--r--net/rxrpc/ar-internal.h150
-rw-r--r--net/sched/Kconfig10
-rw-r--r--net/sched/Makefile1
-rw-r--r--net/sched/act_police.c4
-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_generic.c13
-rw-r--r--net/sched/sch_htb.c17
-rw-r--r--net/sched/sch_netem.c1
-rw-r--r--net/sched/sch_tbf.c22
-rw-r--r--net/sctp/associola.c4
-rw-r--r--net/sctp/auth.c14
-rw-r--r--net/sctp/chunk.c2
-rw-r--r--net/sctp/ipv6.c22
-rw-r--r--net/sctp/output.c9
-rw-r--r--net/sctp/sm_make_chunk.c29
-rw-r--r--net/sctp/socket.c2
-rw-r--r--net/sunrpc/rpc_pipe.c12
-rw-r--r--net/sunrpc/svcsock.c4
-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.h28
-rw-r--r--net/tipc/eth_media.c68
-rw-r--r--net/tipc/ib_media.c58
-rw-r--r--net/tipc/link.c492
-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/node.c7
-rw-r--r--net/tipc/node.h6
-rw-r--r--net/tipc/port.c66
-rw-r--r--net/tipc/port.h16
-rw-r--r--net/tipc/socket.c12
-rw-r--r--net/wimax/wimax-internal.h18
-rw-r--r--net/wireless/chan.c4
-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.c24
-rw-r--r--net/wireless/mlme.c6
-rw-r--r--net/wireless/nl80211.c102
-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.c4
-rw-r--r--net/wireless/sysfs.h4
-rw-r--r--net/wireless/util.c23
-rw-r--r--net/xfrm/xfrm_algo.c13
-rw-r--r--net/xfrm/xfrm_hash.h4
-rw-r--r--net/xfrm/xfrm_ipcomp.c6
-rw-r--r--net/xfrm/xfrm_policy.c7
-rw-r--r--net/xfrm/xfrm_state.c6
430 files changed, 35448 insertions, 13918 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..8db1b985dbf1 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
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/Kconfig b/net/Kconfig
index b50dacc072f0..0715db64a5c3 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -220,6 +220,7 @@ 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
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/ax25/af_ax25.c b/net/ax25/af_ax25.c
index 4b4d2b779ec1..a00123ebb0ae 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -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..a2b480a90872 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);
@@ -135,9 +311,8 @@ static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
135 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION; 311 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
136 batadv_ogm_packet->header.ttl = 2; 312 batadv_ogm_packet->header.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
@@ -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->header.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;
@@ -601,7 +776,7 @@ 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->header.ttl <= 1) {
607 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); 782 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
@@ -621,7 +796,7 @@ 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->header.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);
@@ -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->header.ttl;
813 neigh_node->last_ttl = batadv_ogm_packet->header.ttl; 976 neigh_node->last_ttl = batadv_ogm_packet->header.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;
@@ -1122,13 +1262,11 @@ 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),
1131 batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
1132 batadv_ogm_packet->header.ttl, 1270 batadv_ogm_packet->header.ttl,
1133 batadv_ogm_packet->header.version, has_directlink_flag); 1271 batadv_ogm_packet->header.version, has_directlink_flag);
1134 1272
@@ -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;
@@ -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..6c8c3934bd7b 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)
@@ -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..271d321b3a04
--- /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->header.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.header.packet_type = BATADV_UNICAST_FRAG;
448 frag_header.header.version = BATADV_COMPAT_VERSION;
449 frag_header.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..29ae4efe3543 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->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->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->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..c51a5e568f0a 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;
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
@@ -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, icmph.dst) != 4);
430 BUILD_BUG_ON(offsetof(struct batadv_icmp_packet_rr, icmph.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->header.packet_type = BATADV_UNICAST_TVLV;
1123 unicast_tvlv_packet->header.version = BATADV_COMPAT_VERSION;
1124 unicast_tvlv_packet->header.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..351e199bc0af 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:
@@ -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.
@@ -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..207459b62966 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 96
81/* TT_QUERY subtypes */ 97/**
82#define BATADV_TT_QUERY_TYPE_MASK 0x3 98 * enum batadv_tt_data_flags - flags for tt data tvlv
83 99 * @BATADV_TT_OGM_DIFF: TT diff propagated through OGM
84enum batadv_tt_query_packettype { 100 * @BATADV_TT_REQUEST: TT request message
85 BATADV_TT_REQUEST = 0, 101 * @BATADV_TT_RESPONSE: TT response message
86 BATADV_TT_RESPONSE = 1, 102 * @BATADV_TT_FULL_TABLE: contains full table to replace existing table
87}; 103 */
88 104enum batadv_tt_data_flags {
89/* TT_QUERY flags */ 105 BATADV_TT_OGM_DIFF = BIT(0),
90enum batadv_tt_query_flags { 106 BATADV_TT_REQUEST = BIT(1),
91 BATADV_TT_FULL_TABLE = BIT(2), 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,22 @@ 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
116/* the destination hardware field in the ARP frame is used to 158/* the destination hardware field in the ARP frame is used to
117 * transport the claim type and the group id 159 * transport the claim type and the group id
118 */ 160 */
@@ -131,47 +173,74 @@ struct batadv_header {
131 */ 173 */
132}; 174};
133 175
176/**
177 * struct batadv_ogm_packet - ogm (routing protocol) packet
178 * @header: common batman packet header
179 * @flags: contains routing relevant flags - see enum batadv_iv_flags
180 * @tvlv_len: length of tvlv data following the ogm header
181 */
134struct batadv_ogm_packet { 182struct batadv_ogm_packet {
135 struct batadv_header header; 183 struct batadv_header header;
136 uint8_t flags; /* 0x40: DIRECTLINK flag, 0x20 VIS_SERVER flag... */ 184 uint8_t flags;
137 __be32 seqno; 185 __be32 seqno;
138 uint8_t orig[ETH_ALEN]; 186 uint8_t orig[ETH_ALEN];
139 uint8_t prev_sender[ETH_ALEN]; 187 uint8_t prev_sender[ETH_ALEN];
140 uint8_t gw_flags; /* flags related to gateway class */ 188 uint8_t reserved;
141 uint8_t tq; 189 uint8_t tq;
142 uint8_t tt_num_changes; 190 __be16 tvlv_len;
143 uint8_t ttvn; /* translation table version number */ 191 /* __packed is not needed as the struct size is divisible by 4,
144 __be16 tt_crc; 192 * and the largest data type in this struct has a size of 4.
145} __packed; 193 */
194};
146 195
147#define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet) 196#define BATADV_OGM_HLEN sizeof(struct batadv_ogm_packet)
148 197
149struct batadv_icmp_packet { 198/**
199 * batadv_icmp_header - common ICMP header
200 * @header: common batman header
201 * @msg_type: ICMP packet type
202 * @dst: address of the destination node
203 * @orig: address of the source node
204 * @uid: local ICMP socket identifier
205 */
206struct batadv_icmp_header {
150 struct batadv_header header; 207 struct batadv_header header;
151 uint8_t msg_type; /* see ICMP message types above */ 208 uint8_t msg_type; /* see ICMP message types above */
152 uint8_t dst[ETH_ALEN]; 209 uint8_t dst[ETH_ALEN];
153 uint8_t orig[ETH_ALEN]; 210 uint8_t orig[ETH_ALEN];
154 __be16 seqno;
155 uint8_t uid; 211 uint8_t uid;
212};
213
214/**
215 * batadv_icmp_packet - ICMP packet
216 * @icmph: common ICMP header
217 * @reserved: not used - useful for alignment
218 * @seqno: ICMP sequence number
219 */
220struct batadv_icmp_packet {
221 struct batadv_icmp_header icmph;
156 uint8_t reserved; 222 uint8_t reserved;
223 __be16 seqno;
157}; 224};
158 225
159#define BATADV_RR_LEN 16 226#define BATADV_RR_LEN 16
160 227
161/* icmp_packet_rr must start with all fields from imcp_packet 228/**
162 * as this is assumed by code that handles ICMP packets 229 * batadv_icmp_packet_rr - ICMP RouteRecord packet
230 * @icmph: common ICMP header
231 * @rr_cur: number of entries the rr array
232 * @seqno: ICMP sequence number
233 * @rr: route record array
163 */ 234 */
164struct batadv_icmp_packet_rr { 235struct batadv_icmp_packet_rr {
165 struct batadv_header header; 236 struct batadv_icmp_header icmph;
166 uint8_t msg_type; /* see ICMP message types above */
167 uint8_t dst[ETH_ALEN];
168 uint8_t orig[ETH_ALEN];
169 __be16 seqno;
170 uint8_t uid;
171 uint8_t rr_cur; 237 uint8_t rr_cur;
238 __be16 seqno;
172 uint8_t rr[BATADV_RR_LEN][ETH_ALEN]; 239 uint8_t rr[BATADV_RR_LEN][ETH_ALEN];
173}; 240};
174 241
242#define BATADV_ICMP_MAX_PACKET_SIZE sizeof(struct batadv_icmp_packet_rr)
243
175/* All packet headers in front of an ethernet header have to be completely 244/* 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 245 * divisible by 2 but not by 4 to make the payload after the ethernet
177 * header again 4 bytes boundary aligned. 246 * header again 4 bytes boundary aligned.
@@ -209,15 +278,32 @@ struct batadv_unicast_4addr_packet {
209 */ 278 */
210}; 279};
211 280
212struct batadv_unicast_frag_packet { 281/**
213 struct batadv_header header; 282 * struct batadv_frag_packet - fragmented packet
214 uint8_t ttvn; /* destination translation table version number */ 283 * @header: common batman packet header with type, compatversion, and ttl
215 uint8_t dest[ETH_ALEN]; 284 * @dest: final destination used when routing fragments
216 uint8_t flags; 285 * @orig: originator of the fragment used when merging the packet
217 uint8_t align; 286 * @no: fragment number within this sequence
218 uint8_t orig[ETH_ALEN]; 287 * @reserved: reserved byte for alignment
219 __be16 seqno; 288 * @seqno: sequence identification
220} __packed; 289 * @total_size: size of the merged packet
290 */
291struct batadv_frag_packet {
292 struct batadv_header header;
293#if defined(__BIG_ENDIAN_BITFIELD)
294 uint8_t no:4;
295 uint8_t reserved:4;
296#elif defined(__LITTLE_ENDIAN_BITFIELD)
297 uint8_t reserved:4;
298 uint8_t no:4;
299#else
300#error "unknown bitfield endianess"
301#endif
302 uint8_t dest[ETH_ALEN];
303 uint8_t orig[ETH_ALEN];
304 __be16 seqno;
305 __be16 total_size;
306};
221 307
222struct batadv_bcast_packet { 308struct batadv_bcast_packet {
223 struct batadv_header header; 309 struct batadv_header header;
@@ -231,54 +317,6 @@ struct batadv_bcast_packet {
231 317
232#pragma pack() 318#pragma pack()
233 319
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/** 320/**
283 * struct batadv_coded_packet - network coded packet 321 * struct batadv_coded_packet - network coded packet
284 * @header: common batman packet header and ttl of first included packet 322 * @header: common batman packet header and ttl of first included packet
@@ -311,4 +349,96 @@ struct batadv_coded_packet {
311 __be16 coded_len; 349 __be16 coded_len;
312}; 350};
313 351
352/**
353 * struct batadv_unicast_tvlv - generic unicast packet with tvlv payload
354 * @header: common batman packet header
355 * @reserved: reserved field (for packet alignment)
356 * @src: address of the source
357 * @dst: address of the destination
358 * @tvlv_len: length of tvlv data following the unicast tvlv header
359 * @align: 2 bytes to align the header to a 4 byte boundry
360 */
361struct batadv_unicast_tvlv_packet {
362 struct batadv_header header;
363 uint8_t reserved;
364 uint8_t dst[ETH_ALEN];
365 uint8_t src[ETH_ALEN];
366 __be16 tvlv_len;
367 uint16_t align;
368};
369
370/**
371 * struct batadv_tvlv_hdr - base tvlv header struct
372 * @type: tvlv container type (see batadv_tvlv_type)
373 * @version: tvlv container version
374 * @len: tvlv container length
375 */
376struct batadv_tvlv_hdr {
377 uint8_t type;
378 uint8_t version;
379 __be16 len;
380};
381
382/**
383 * struct batadv_tvlv_gateway_data - gateway data propagated through gw tvlv
384 * container
385 * @bandwidth_down: advertised uplink download bandwidth
386 * @bandwidth_up: advertised uplink upload bandwidth
387 */
388struct batadv_tvlv_gateway_data {
389 __be32 bandwidth_down;
390 __be32 bandwidth_up;
391};
392
393/**
394 * struct batadv_tvlv_tt_data - tt data propagated through the tt tvlv container
395 * @flags: translation table flags (see batadv_tt_data_flags)
396 * @ttvn: translation table version number
397 * @vlan_num: number of announced VLANs. In the TVLV this struct is followed by
398 * one batadv_tvlv_tt_vlan_data object per announced vlan
399 */
400struct batadv_tvlv_tt_data {
401 uint8_t flags;
402 uint8_t ttvn;
403 __be16 num_vlan;
404};
405
406/**
407 * struct batadv_tvlv_tt_vlan_data - vlan specific tt data propagated through
408 * the tt tvlv container
409 * @crc: crc32 checksum of the entries belonging to this vlan
410 * @vid: vlan identifier
411 * @reserved: unused, useful for alignment purposes
412 */
413struct batadv_tvlv_tt_vlan_data {
414 __be32 crc;
415 __be16 vid;
416 uint16_t reserved;
417};
418
419/**
420 * struct batadv_tvlv_tt_change - translation table diff data
421 * @flags: status indicators concerning the non-mesh client (see
422 * batadv_tt_client_flags)
423 * @reserved: reserved field
424 * @addr: mac address of non-mesh client that triggered this tt change
425 * @vid: VLAN identifier
426 */
427struct batadv_tvlv_tt_change {
428 uint8_t flags;
429 uint8_t reserved;
430 uint8_t addr[ETH_ALEN];
431 __be16 vid;
432};
433
434/**
435 * struct batadv_tvlv_roam_adv - roaming advertisement
436 * @client: mac address of roaming client
437 * @vid: VLAN identifier
438 */
439struct batadv_tvlv_roam_adv {
440 uint8_t client[ETH_ALEN];
441 __be16 vid;
442};
443
314#endif /* _NET_BATMAN_ADV_PACKET_H_ */ 444#endif /* _NET_BATMAN_ADV_PACKET_H_ */
diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
index 0439395d7ba5..d4114d775ad6 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->header.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);
@@ -307,9 +338,9 @@ static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
307 icmp_packet = (struct batadv_icmp_packet *)skb->data; 338 icmp_packet = (struct batadv_icmp_packet *)skb->data;
308 339
309 /* send TTL exceeded if packet is an echo request (traceroute) */ 340 /* send TTL exceeded if packet is an echo request (traceroute) */
310 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) { 341 if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
311 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n", 342 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
312 icmp_packet->orig, icmp_packet->dst); 343 icmp_packet->icmph.orig, icmp_packet->icmph.dst);
313 goto out; 344 goto out;
314 } 345 }
315 346
@@ -318,7 +349,7 @@ static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
318 goto out; 349 goto out;
319 350
320 /* get routing information */ 351 /* get routing information */
321 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); 352 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
322 if (!orig_node) 353 if (!orig_node)
323 goto out; 354 goto out;
324 355
@@ -328,10 +359,11 @@ static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
328 359
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->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
332 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); 363 memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
333 icmp_packet->msg_type = BATADV_TTL_EXCEEDED; 364 ETH_ALEN);
334 icmp_packet->header.ttl = BATADV_TTL; 365 icmp_packet->icmph.msg_type = BATADV_TTL_EXCEEDED;
366 icmp_packet->icmph.header.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->header.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->header.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,11 +702,9 @@ 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
@@ -793,46 +721,12 @@ 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 */
729 unicast_packet = (struct batadv_unicast_packet *)skb->data;
836 unicast_packet->header.ttl--; 730 unicast_packet->header.ttl--;
837 731
838 switch (unicast_packet->header.packet_type) { 732 switch (unicast_packet->header.packet_type) {
@@ -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{
@@ -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);
1107
1108 /* Add fragment to buffer and merge if possible. */
1109 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1110 goto out;
1141 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)
@@ -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..c83be5ebaa28 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->header.version = BATADV_COMPAT_VERSION;
165 /* batman packet type: unicast */
166 unicast_packet->header.packet_type = BATADV_UNICAST;
167 /* set unicast ttl */
168 unicast_packet->header.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.header.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);
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..36f050876f82 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 ...
@@ -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,12 +328,12 @@ 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_header *batadv_header = (struct batadv_header *)skb->data;
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 is_bcast = (batadv_header->packet_type == BATADV_BCAST);
@@ -326,13 +345,12 @@ void batadv_interface_rx(struct net_device *soft_iface,
326 skb_pull_rcsum(skb, hdr_size); 345 skb_pull_rcsum(skb, hdr_size);
327 skb_reset_mac_header(skb); 346 skb_reset_mac_header(skb);
328 347
348 vid = batadv_get_vid(skb, hdr_size);
329 ethhdr = eth_hdr(skb); 349 ethhdr = eth_hdr(skb);
330 350
331 switch (ntohs(ethhdr->h_proto)) { 351 switch (ntohs(ethhdr->h_proto)) {
332 case ETH_P_8021Q: 352 case ETH_P_8021Q:
333 vhdr = (struct vlan_ethhdr *)skb->data; 353 vhdr = (struct vlan_ethhdr *)skb->data;
334 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
335 vid |= BATADV_VLAN_HAS_TAG;
336 354
337 if (vhdr->h_vlan_encapsulated_proto != ethertype) 355 if (vhdr->h_vlan_encapsulated_proto != ethertype)
338 break; 356 break;
@@ -368,9 +386,10 @@ void batadv_interface_rx(struct net_device *soft_iface,
368 386
369 if (orig_node) 387 if (orig_node)
370 batadv_tt_add_temporary_global_entry(bat_priv, orig_node, 388 batadv_tt_add_temporary_global_entry(bat_priv, orig_node,
371 ethhdr->h_source); 389 ethhdr->h_source, vid);
372 390
373 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest)) 391 if (batadv_is_ap_isolated(bat_priv, ethhdr->h_source, ethhdr->h_dest,
392 vid))
374 goto dropped; 393 goto dropped;
375 394
376 netif_rx(skb); 395 netif_rx(skb);
@@ -382,6 +401,177 @@ out:
382 return; 401 return;
383} 402}
384 403
404/**
405 * batadv_softif_vlan_free_ref - decrease the vlan object refcounter and
406 * possibly free it
407 * @softif_vlan: the vlan object to release
408 */
409void batadv_softif_vlan_free_ref(struct batadv_softif_vlan *softif_vlan)
410{
411 if (atomic_dec_and_test(&softif_vlan->refcount))
412 kfree_rcu(softif_vlan, rcu);
413}
414
415/**
416 * batadv_softif_vlan_get - get the vlan object for a specific vid
417 * @bat_priv: the bat priv with all the soft interface information
418 * @vid: the identifier of the vlan object to retrieve
419 *
420 * Returns the private data of the vlan matching the vid passed as argument or
421 * NULL otherwise. The refcounter of the returned object is incremented by 1.
422 */
423struct batadv_softif_vlan *batadv_softif_vlan_get(struct batadv_priv *bat_priv,
424 unsigned short vid)
425{
426 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
427
428 rcu_read_lock();
429 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
430 if (vlan_tmp->vid != vid)
431 continue;
432
433 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
434 continue;
435
436 vlan = vlan_tmp;
437 break;
438 }
439 rcu_read_unlock();
440
441 return vlan;
442}
443
444/**
445 * batadv_create_vlan - allocate the needed resources for a new vlan
446 * @bat_priv: the bat priv with all the soft interface information
447 * @vid: the VLAN identifier
448 *
449 * Returns 0 on success, a negative error otherwise.
450 */
451int batadv_softif_create_vlan(struct batadv_priv *bat_priv, unsigned short vid)
452{
453 struct batadv_softif_vlan *vlan;
454 int err;
455
456 vlan = batadv_softif_vlan_get(bat_priv, vid);
457 if (vlan) {
458 batadv_softif_vlan_free_ref(vlan);
459 return -EEXIST;
460 }
461
462 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
463 if (!vlan)
464 return -ENOMEM;
465
466 vlan->vid = vid;
467 atomic_set(&vlan->refcount, 1);
468
469 atomic_set(&vlan->ap_isolation, 0);
470
471 err = batadv_sysfs_add_vlan(bat_priv->soft_iface, vlan);
472 if (err) {
473 kfree(vlan);
474 return err;
475 }
476
477 /* add a new TT local entry. This one will be marked with the NOPURGE
478 * flag
479 */
480 batadv_tt_local_add(bat_priv->soft_iface,
481 bat_priv->soft_iface->dev_addr, vid,
482 BATADV_NULL_IFINDEX);
483
484 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
485 hlist_add_head_rcu(&vlan->list, &bat_priv->softif_vlan_list);
486 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
487
488 return 0;
489}
490
491/**
492 * batadv_softif_destroy_vlan - remove and destroy a softif_vlan object
493 * @bat_priv: the bat priv with all the soft interface information
494 * @vlan: the object to remove
495 */
496static void batadv_softif_destroy_vlan(struct batadv_priv *bat_priv,
497 struct batadv_softif_vlan *vlan)
498{
499 spin_lock_bh(&bat_priv->softif_vlan_list_lock);
500 hlist_del_rcu(&vlan->list);
501 spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
502
503 batadv_sysfs_del_vlan(bat_priv, vlan);
504
505 /* explicitly remove the associated TT local entry because it is marked
506 * with the NOPURGE flag
507 */
508 batadv_tt_local_remove(bat_priv, bat_priv->soft_iface->dev_addr,
509 vlan->vid, "vlan interface destroyed", false);
510
511 batadv_softif_vlan_free_ref(vlan);
512}
513
514/**
515 * batadv_interface_add_vid - ndo_add_vid API implementation
516 * @dev: the netdev of the mesh interface
517 * @vid: identifier of the new vlan
518 *
519 * Set up all the internal structures for handling the new vlan on top of the
520 * mesh interface
521 *
522 * Returns 0 on success or a negative error code in case of failure.
523 */
524static int batadv_interface_add_vid(struct net_device *dev, __be16 proto,
525 unsigned short vid)
526{
527 struct batadv_priv *bat_priv = netdev_priv(dev);
528
529 /* only 802.1Q vlans are supported.
530 * batman-adv does not know how to handle other types
531 */
532 if (proto != htons(ETH_P_8021Q))
533 return -EINVAL;
534
535 vid |= BATADV_VLAN_HAS_TAG;
536
537 return batadv_softif_create_vlan(bat_priv, vid);
538}
539
540/**
541 * batadv_interface_kill_vid - ndo_kill_vid API implementation
542 * @dev: the netdev of the mesh interface
543 * @vid: identifier of the deleted vlan
544 *
545 * Destroy all the internal structures used to handle the vlan identified by vid
546 * on top of the mesh interface
547 *
548 * Returns 0 on success, -EINVAL if the specified prototype is not ETH_P_8021Q
549 * or -ENOENT if the specified vlan id wasn't registered.
550 */
551static int batadv_interface_kill_vid(struct net_device *dev, __be16 proto,
552 unsigned short vid)
553{
554 struct batadv_priv *bat_priv = netdev_priv(dev);
555 struct batadv_softif_vlan *vlan;
556
557 /* only 802.1Q vlans are supported. batman-adv does not know how to
558 * handle other types
559 */
560 if (proto != htons(ETH_P_8021Q))
561 return -EINVAL;
562
563 vlan = batadv_softif_vlan_get(bat_priv, vid | BATADV_VLAN_HAS_TAG);
564 if (!vlan)
565 return -ENOENT;
566
567 batadv_softif_destroy_vlan(bat_priv, vlan);
568
569 /* finally free the vlan object */
570 batadv_softif_vlan_free_ref(vlan);
571
572 return 0;
573}
574
385/* batman-adv network devices have devices nesting below it and are a special 575/* 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 576 * "super class" of normal network devices; split their locks off into a
387 * separate class since they always nest. 577 * separate class since they always nest.
@@ -421,6 +611,7 @@ static void batadv_set_lockdep_class(struct net_device *dev)
421 */ 611 */
422static void batadv_softif_destroy_finish(struct work_struct *work) 612static void batadv_softif_destroy_finish(struct work_struct *work)
423{ 613{
614 struct batadv_softif_vlan *vlan;
424 struct batadv_priv *bat_priv; 615 struct batadv_priv *bat_priv;
425 struct net_device *soft_iface; 616 struct net_device *soft_iface;
426 617
@@ -428,6 +619,13 @@ static void batadv_softif_destroy_finish(struct work_struct *work)
428 cleanup_work); 619 cleanup_work);
429 soft_iface = bat_priv->soft_iface; 620 soft_iface = bat_priv->soft_iface;
430 621
622 /* destroy the "untagged" VLAN */
623 vlan = batadv_softif_vlan_get(bat_priv, BATADV_NO_FLAGS);
624 if (vlan) {
625 batadv_softif_destroy_vlan(bat_priv, vlan);
626 batadv_softif_vlan_free_ref(vlan);
627 }
628
431 batadv_sysfs_del_meshif(soft_iface); 629 batadv_sysfs_del_meshif(soft_iface);
432 630
433 rtnl_lock(); 631 rtnl_lock();
@@ -444,6 +642,7 @@ static void batadv_softif_destroy_finish(struct work_struct *work)
444static int batadv_softif_init_late(struct net_device *dev) 642static int batadv_softif_init_late(struct net_device *dev)
445{ 643{
446 struct batadv_priv *bat_priv; 644 struct batadv_priv *bat_priv;
645 uint32_t random_seqno;
447 int ret; 646 int ret;
448 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM; 647 size_t cnt_len = sizeof(uint64_t) * BATADV_CNT_NUM;
449 648
@@ -468,17 +667,17 @@ static int batadv_softif_init_late(struct net_device *dev)
468#ifdef CONFIG_BATMAN_ADV_DAT 667#ifdef CONFIG_BATMAN_ADV_DAT
469 atomic_set(&bat_priv->distributed_arp_table, 1); 668 atomic_set(&bat_priv->distributed_arp_table, 1);
470#endif 669#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); 670 atomic_set(&bat_priv->gw_mode, BATADV_GW_MODE_OFF);
474 atomic_set(&bat_priv->gw_sel_class, 20); 671 atomic_set(&bat_priv->gw_sel_class, 20);
475 atomic_set(&bat_priv->gw_bandwidth, 41); 672 atomic_set(&bat_priv->gw.bandwidth_down, 100);
673 atomic_set(&bat_priv->gw.bandwidth_up, 20);
476 atomic_set(&bat_priv->orig_interval, 1000); 674 atomic_set(&bat_priv->orig_interval, 1000);
477 atomic_set(&bat_priv->hop_penalty, 30); 675 atomic_set(&bat_priv->hop_penalty, 30);
478#ifdef CONFIG_BATMAN_ADV_DEBUG 676#ifdef CONFIG_BATMAN_ADV_DEBUG
479 atomic_set(&bat_priv->log_level, 0); 677 atomic_set(&bat_priv->log_level, 0);
480#endif 678#endif
481 atomic_set(&bat_priv->fragmentation, 1); 679 atomic_set(&bat_priv->fragmentation, 1);
680 atomic_set(&bat_priv->packet_size_max, ETH_DATA_LEN);
482 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN); 681 atomic_set(&bat_priv->bcast_queue_left, BATADV_BCAST_QUEUE_LEN);
483 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN); 682 atomic_set(&bat_priv->batman_queue_left, BATADV_BATMAN_QUEUE_LEN);
484 683
@@ -493,6 +692,10 @@ static int batadv_softif_init_late(struct net_device *dev)
493 bat_priv->tt.last_changeset = NULL; 692 bat_priv->tt.last_changeset = NULL;
494 bat_priv->tt.last_changeset_len = 0; 693 bat_priv->tt.last_changeset_len = 0;
495 694
695 /* randomize initial seqno to avoid collision */
696 get_random_bytes(&random_seqno, sizeof(random_seqno));
697 atomic_set(&bat_priv->frag_seqno, random_seqno);
698
496 bat_priv->primary_if = NULL; 699 bat_priv->primary_if = NULL;
497 bat_priv->num_ifaces = 0; 700 bat_priv->num_ifaces = 0;
498 701
@@ -578,8 +781,11 @@ static const struct net_device_ops batadv_netdev_ops = {
578 .ndo_open = batadv_interface_open, 781 .ndo_open = batadv_interface_open,
579 .ndo_stop = batadv_interface_release, 782 .ndo_stop = batadv_interface_release,
580 .ndo_get_stats = batadv_interface_stats, 783 .ndo_get_stats = batadv_interface_stats,
784 .ndo_vlan_rx_add_vid = batadv_interface_add_vid,
785 .ndo_vlan_rx_kill_vid = batadv_interface_kill_vid,
581 .ndo_set_mac_address = batadv_interface_set_mac_addr, 786 .ndo_set_mac_address = batadv_interface_set_mac_addr,
582 .ndo_change_mtu = batadv_interface_change_mtu, 787 .ndo_change_mtu = batadv_interface_change_mtu,
788 .ndo_set_rx_mode = batadv_interface_set_rx_mode,
583 .ndo_start_xmit = batadv_interface_tx, 789 .ndo_start_xmit = batadv_interface_tx,
584 .ndo_validate_addr = eth_validate_addr, 790 .ndo_validate_addr = eth_validate_addr,
585 .ndo_add_slave = batadv_softif_slave_add, 791 .ndo_add_slave = batadv_softif_slave_add,
@@ -616,6 +822,7 @@ static void batadv_softif_init_early(struct net_device *dev)
616 822
617 dev->netdev_ops = &batadv_netdev_ops; 823 dev->netdev_ops = &batadv_netdev_ops;
618 dev->destructor = batadv_softif_free; 824 dev->destructor = batadv_softif_free;
825 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
619 dev->tx_queue_len = 0; 826 dev->tx_queue_len = 0;
620 827
621 /* can't call min_mtu, because the needed variables 828 /* can't call min_mtu, because the needed variables
@@ -623,7 +830,7 @@ static void batadv_softif_init_early(struct net_device *dev)
623 */ 830 */
624 dev->mtu = ETH_DATA_LEN; 831 dev->mtu = ETH_DATA_LEN;
625 /* reserve more space in the skbuff for our header */ 832 /* reserve more space in the skbuff for our header */
626 dev->hard_header_len = BATADV_HEADER_LEN; 833 dev->hard_header_len = batadv_max_header_len();
627 834
628 /* generate random address */ 835 /* generate random address */
629 eth_hw_addr_random(dev); 836 eth_hw_addr_random(dev);
@@ -760,6 +967,12 @@ static const struct {
760 { "mgmt_tx_bytes" }, 967 { "mgmt_tx_bytes" },
761 { "mgmt_rx" }, 968 { "mgmt_rx" },
762 { "mgmt_rx_bytes" }, 969 { "mgmt_rx_bytes" },
970 { "frag_tx" },
971 { "frag_tx_bytes" },
972 { "frag_rx" },
973 { "frag_rx_bytes" },
974 { "frag_fwd" },
975 { "frag_fwd_bytes" },
763 { "tt_request_tx" }, 976 { "tt_request_tx" },
764 { "tt_request_rx" }, 977 { "tt_request_rx" },
765 { "tt_response_tx" }, 978 { "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..4add57d4857f 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,13 @@ 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 tt_change_node->change.reserved = 0;
188 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN); 337 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
338 tt_change_node->change.vid = htons(common->vid);
189 339
190 del_op_requested = flags & BATADV_TT_CLIENT_DEL; 340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
191 341
@@ -208,6 +358,13 @@ static void batadv_tt_local_event(struct batadv_priv *bat_priv,
208 goto del; 358 goto del;
209 if (del_op_requested && !del_op_entry) 359 if (del_op_requested && !del_op_entry)
210 goto del; 360 goto del;
361
362 /* this is a second add in the same originator interval. It
363 * means that flags have been changed: update them!
364 */
365 if (!del_op_requested && !del_op_entry)
366 entry->change.flags = flags;
367
211 continue; 368 continue;
212del: 369del:
213 list_del(&entry->list); 370 list_del(&entry->list);
@@ -229,9 +386,55 @@ unlock:
229 atomic_inc(&bat_priv->tt.local_changes); 386 atomic_inc(&bat_priv->tt.local_changes);
230} 387}
231 388
232int batadv_tt_len(int changes_num) 389/**
390 * batadv_tt_len - compute length in bytes of given number of tt changes
391 * @changes_num: number of tt changes
392 *
393 * Returns computed length in bytes.
394 */
395static int batadv_tt_len(int changes_num)
233{ 396{
234 return changes_num * sizeof(struct batadv_tt_change); 397 return changes_num * sizeof(struct batadv_tvlv_tt_change);
398}
399
400/**
401 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
402 * @tt_len: available space
403 *
404 * Returns the number of entries.
405 */
406static uint16_t batadv_tt_entries(uint16_t tt_len)
407{
408 return tt_len / batadv_tt_len(1);
409}
410
411/**
412 * batadv_tt_local_table_transmit_size - calculates the local translation table
413 * size when transmitted over the air
414 * @bat_priv: the bat priv with all the soft interface information
415 *
416 * Returns local translation table size in bytes.
417 */
418static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
419{
420 uint16_t num_vlan = 0, tt_local_entries = 0;
421 struct batadv_softif_vlan *vlan;
422 int hdr_size;
423
424 rcu_read_lock();
425 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
426 num_vlan++;
427 tt_local_entries += atomic_read(&vlan->tt.num_entries);
428 }
429 rcu_read_unlock();
430
431 /* header size of tvlv encapsulated tt response payload */
432 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
433 hdr_size += sizeof(struct batadv_tvlv_hdr);
434 hdr_size += sizeof(struct batadv_tvlv_tt_data);
435 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
436
437 return hdr_size + batadv_tt_len(tt_local_entries);
235} 438}
236 439
237static int batadv_tt_local_init(struct batadv_priv *bat_priv) 440static int batadv_tt_local_init(struct batadv_priv *bat_priv)
@@ -255,33 +458,51 @@ static void batadv_tt_global_free(struct batadv_priv *bat_priv,
255 const char *message) 458 const char *message)
256{ 459{
257 batadv_dbg(BATADV_DBG_TT, bat_priv, 460 batadv_dbg(BATADV_DBG_TT, bat_priv,
258 "Deleting global tt entry %pM: %s\n", 461 "Deleting global tt entry %pM (vid: %d): %s\n",
259 tt_global->common.addr, message); 462 tt_global->common.addr,
463 BATADV_PRINT_VID(tt_global->common.vid), message);
260 464
261 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt, 465 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
262 batadv_choose_orig, tt_global->common.addr); 466 batadv_choose_tt, &tt_global->common);
263 batadv_tt_global_entry_free_ref(tt_global); 467 batadv_tt_global_entry_free_ref(tt_global);
264} 468}
265 469
266void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr, 470/**
267 int ifindex) 471 * batadv_tt_local_add - add a new client to the local table or update an
472 * existing client
473 * @soft_iface: netdev struct of the mesh interface
474 * @addr: the mac address of the client to add
475 * @vid: VLAN identifier
476 * @ifindex: index of the interface where the client is connected to (useful to
477 * identify wireless clients)
478 *
479 * Returns true if the client was successfully added, false otherwise.
480 */
481bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
482 unsigned short vid, int ifindex)
268{ 483{
269 struct batadv_priv *bat_priv = netdev_priv(soft_iface); 484 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
270 struct batadv_tt_local_entry *tt_local; 485 struct batadv_tt_local_entry *tt_local;
271 struct batadv_tt_global_entry *tt_global; 486 struct batadv_tt_global_entry *tt_global;
487 struct net_device *in_dev = NULL;
272 struct hlist_head *head; 488 struct hlist_head *head;
273 struct batadv_tt_orig_list_entry *orig_entry; 489 struct batadv_tt_orig_list_entry *orig_entry;
274 int hash_added; 490 int hash_added, table_size, packet_size_max;
275 bool roamed_back = false; 491 bool ret = false, roamed_back = false;
492 uint8_t remote_flags;
493
494 if (ifindex != BATADV_NULL_IFINDEX)
495 in_dev = dev_get_by_index(&init_net, ifindex);
276 496
277 tt_local = batadv_tt_local_hash_find(bat_priv, addr); 497 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
278 tt_global = batadv_tt_global_hash_find(bat_priv, addr); 498 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
279 499
280 if (tt_local) { 500 if (tt_local) {
281 tt_local->last_seen = jiffies; 501 tt_local->last_seen = jiffies;
282 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) { 502 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
283 batadv_dbg(BATADV_DBG_TT, bat_priv, 503 batadv_dbg(BATADV_DBG_TT, bat_priv,
284 "Re-adding pending client %pM\n", addr); 504 "Re-adding pending client %pM (vid: %d)\n",
505 addr, BATADV_PRINT_VID(vid));
285 /* whatever the reason why the PENDING flag was set, 506 /* whatever the reason why the PENDING flag was set,
286 * this is a client which was enqueued to be removed in 507 * this is a client which was enqueued to be removed in
287 * this orig_interval. Since it popped up again, the 508 * this orig_interval. Since it popped up again, the
@@ -293,8 +514,8 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
293 514
294 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) { 515 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
295 batadv_dbg(BATADV_DBG_TT, bat_priv, 516 batadv_dbg(BATADV_DBG_TT, bat_priv,
296 "Roaming client %pM came back to its original location\n", 517 "Roaming client %pM (vid: %d) came back to its original location\n",
297 addr); 518 addr, BATADV_PRINT_VID(vid));
298 /* the ROAM flag is set because this client roamed away 519 /* the ROAM flag is set because this client roamed away
299 * and the node got a roaming_advertisement message. Now 520 * and the node got a roaming_advertisement message. Now
300 * that the client popped up again at its original 521 * that the client popped up again at its original
@@ -306,12 +527,24 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
306 goto check_roaming; 527 goto check_roaming;
307 } 528 }
308 529
530 /* Ignore the client if we cannot send it in a full table response. */
531 table_size = batadv_tt_local_table_transmit_size(bat_priv);
532 table_size += batadv_tt_len(1);
533 packet_size_max = atomic_read(&bat_priv->packet_size_max);
534 if (table_size > packet_size_max) {
535 net_ratelimited_function(batadv_info, soft_iface,
536 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
537 table_size, packet_size_max, addr);
538 goto out;
539 }
540
309 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC); 541 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
310 if (!tt_local) 542 if (!tt_local)
311 goto out; 543 goto out;
312 544
313 batadv_dbg(BATADV_DBG_TT, bat_priv, 545 batadv_dbg(BATADV_DBG_TT, bat_priv,
314 "Creating new local tt entry: %pM (ttvn: %d)\n", addr, 546 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
547 addr, BATADV_PRINT_VID(vid),
315 (uint8_t)atomic_read(&bat_priv->tt.vn)); 548 (uint8_t)atomic_read(&bat_priv->tt.vn));
316 549
317 memcpy(tt_local->common.addr, addr, ETH_ALEN); 550 memcpy(tt_local->common.addr, addr, ETH_ALEN);
@@ -320,7 +553,8 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
320 * (consistency check) 553 * (consistency check)
321 */ 554 */
322 tt_local->common.flags = BATADV_TT_CLIENT_NEW; 555 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
323 if (batadv_is_wifi_iface(ifindex)) 556 tt_local->common.vid = vid;
557 if (batadv_is_wifi_netdev(in_dev))
324 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI; 558 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
325 atomic_set(&tt_local->common.refcount, 2); 559 atomic_set(&tt_local->common.refcount, 2);
326 tt_local->last_seen = jiffies; 560 tt_local->last_seen = jiffies;
@@ -331,7 +565,7 @@ void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
331 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE; 565 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
332 566
333 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt, 567 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
334 batadv_choose_orig, &tt_local->common, 568 batadv_choose_tt, &tt_local->common,
335 &tt_local->common.hash_entry); 569 &tt_local->common.hash_entry);
336 570
337 if (unlikely(hash_added != 0)) { 571 if (unlikely(hash_added != 0)) {
@@ -353,6 +587,7 @@ check_roaming:
353 rcu_read_lock(); 587 rcu_read_lock();
354 hlist_for_each_entry_rcu(orig_entry, head, list) { 588 hlist_for_each_entry_rcu(orig_entry, head, list) {
355 batadv_send_roam_adv(bat_priv, tt_global->common.addr, 589 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
590 tt_global->common.vid,
356 orig_entry->orig_node); 591 orig_entry->orig_node);
357 } 592 }
358 rcu_read_unlock(); 593 rcu_read_unlock();
@@ -369,78 +604,219 @@ check_roaming:
369 } 604 }
370 } 605 }
371 606
607 /* store the current remote flags before altering them. This helps
608 * understanding is flags are changing or not
609 */
610 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
611
612 if (batadv_is_wifi_netdev(in_dev))
613 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
614 else
615 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
616
617 /* if any "dynamic" flag has been modified, resend an ADD event for this
618 * entry so that all the nodes can get the new flags
619 */
620 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
621 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
622
623 ret = true;
372out: 624out:
625 if (in_dev)
626 dev_put(in_dev);
373 if (tt_local) 627 if (tt_local)
374 batadv_tt_local_entry_free_ref(tt_local); 628 batadv_tt_local_entry_free_ref(tt_local);
375 if (tt_global) 629 if (tt_global)
376 batadv_tt_global_entry_free_ref(tt_global); 630 batadv_tt_global_entry_free_ref(tt_global);
631 return ret;
377} 632}
378 633
379static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff, 634/**
380 int *packet_buff_len, 635 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
381 int min_packet_len, 636 * within a TT Response directed to another node
382 int new_packet_len) 637 * @orig_node: originator for which the TT data has to be prepared
638 * @tt_data: uninitialised pointer to the address of the TVLV buffer
639 * @tt_change: uninitialised pointer to the address of the area where the TT
640 * changed can be stored
641 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
642 * function reserves the amount of space needed to send the entire global TT
643 * table. In case of success the value is updated with the real amount of
644 * reserved bytes
645
646 * Allocate the needed amount of memory for the entire TT TVLV and write its
647 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
648 * objects, one per active VLAN served by the originator node.
649 *
650 * Return the size of the allocated buffer or 0 in case of failure.
651 */
652static uint16_t
653batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
654 struct batadv_tvlv_tt_data **tt_data,
655 struct batadv_tvlv_tt_change **tt_change,
656 int32_t *tt_len)
383{ 657{
384 unsigned char *new_buff; 658 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
659 struct batadv_tvlv_tt_vlan_data *tt_vlan;
660 struct batadv_orig_node_vlan *vlan;
661 uint8_t *tt_change_ptr;
385 662
386 new_buff = kmalloc(new_packet_len, GFP_ATOMIC); 663 rcu_read_lock();
664 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
665 num_vlan++;
666 num_entries += atomic_read(&vlan->tt.num_entries);
667 }
387 668
388 /* keep old buffer if kmalloc should fail */ 669 change_offset = sizeof(**tt_data);
389 if (new_buff) { 670 change_offset += num_vlan * sizeof(*tt_vlan);
390 memcpy(new_buff, *packet_buff, min_packet_len); 671
391 kfree(*packet_buff); 672 /* if tt_len is negative, allocate the space needed by the full table */
392 *packet_buff = new_buff; 673 if (*tt_len < 0)
393 *packet_buff_len = new_packet_len; 674 *tt_len = batadv_tt_len(num_entries);
675
676 tvlv_len = *tt_len;
677 tvlv_len += change_offset;
678
679 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
680 if (!*tt_data) {
681 *tt_len = 0;
682 goto out;
394 } 683 }
684
685 (*tt_data)->flags = BATADV_NO_FLAGS;
686 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
687 (*tt_data)->num_vlan = htons(num_vlan);
688
689 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
690 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
691 tt_vlan->vid = htons(vlan->vid);
692 tt_vlan->crc = htonl(vlan->tt.crc);
693
694 tt_vlan++;
695 }
696
697 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
698 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
699
700out:
701 rcu_read_unlock();
702 return tvlv_len;
395} 703}
396 704
397static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv, 705/**
398 unsigned char **packet_buff, 706 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
399 int *packet_buff_len, 707 * node
400 int min_packet_len) 708 * @bat_priv: the bat priv with all the soft interface information
401{ 709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
402 int req_len; 710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changes can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire local TT
714 * table. In case of success the value is updated with the real amount of
715 * reserved bytes
716 *
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN.
720 *
721 * Return the size of the allocated buffer or 0 in case of failure.
722 */
723static uint16_t
724batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
725 struct batadv_tvlv_tt_data **tt_data,
726 struct batadv_tvlv_tt_change **tt_change,
727 int32_t *tt_len)
728{
729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
730 struct batadv_softif_vlan *vlan;
731 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
732 uint8_t *tt_change_ptr;
733 int change_offset;
403 734
404 req_len = min_packet_len; 735 rcu_read_lock();
405 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes)); 736 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
737 num_vlan++;
738 num_entries += atomic_read(&vlan->tt.num_entries);
739 }
406 740
407 /* if we have too many changes for one packet don't send any 741 change_offset = sizeof(**tt_data);
408 * and wait for the tt table request which will be fragmented 742 change_offset += num_vlan * sizeof(*tt_vlan);
409 */ 743
410 if (req_len > bat_priv->soft_iface->mtu) 744 /* if tt_len is negative, allocate the space needed by the full table */
411 req_len = min_packet_len; 745 if (*tt_len < 0)
746 *tt_len = batadv_tt_len(num_entries);
747
748 tvlv_len = *tt_len;
749 tvlv_len += change_offset;
750
751 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
752 if (!*tt_data) {
753 tvlv_len = 0;
754 goto out;
755 }
756
757 (*tt_data)->flags = BATADV_NO_FLAGS;
758 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
759 (*tt_data)->num_vlan = htons(num_vlan);
760
761 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
762 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
763 tt_vlan->vid = htons(vlan->vid);
764 tt_vlan->crc = htonl(vlan->tt.crc);
412 765
413 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len, 766 tt_vlan++;
414 min_packet_len, req_len); 767 }
768
769 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
770 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
771
772out:
773 rcu_read_unlock();
774 return tvlv_len;
415} 775}
416 776
417static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv, 777/**
418 unsigned char **packet_buff, 778 * batadv_tt_tvlv_container_update - update the translation table tvlv container
419 int *packet_buff_len, 779 * after local tt changes have been committed
420 int min_packet_len) 780 * @bat_priv: the bat priv with all the soft interface information
781 */
782static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
421{ 783{
422 struct batadv_tt_change_node *entry, *safe; 784 struct batadv_tt_change_node *entry, *safe;
423 int count = 0, tot_changes = 0, new_len; 785 struct batadv_tvlv_tt_data *tt_data;
424 unsigned char *tt_buff; 786 struct batadv_tvlv_tt_change *tt_change;
787 int tt_diff_len, tt_change_len = 0;
788 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
789 uint16_t tvlv_len;
425 790
426 batadv_tt_prepare_packet_buff(bat_priv, packet_buff, 791 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
427 packet_buff_len, min_packet_len); 792 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
428 793
429 new_len = *packet_buff_len - min_packet_len; 794 /* if we have too many changes for one packet don't send any
430 tt_buff = *packet_buff + min_packet_len; 795 * and wait for the tt table request which will be fragmented
796 */
797 if (tt_diff_len > bat_priv->soft_iface->mtu)
798 tt_diff_len = 0;
799
800 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
801 &tt_change, &tt_diff_len);
802 if (!tvlv_len)
803 return;
431 804
432 if (new_len > 0) 805 tt_data->flags = BATADV_TT_OGM_DIFF;
433 tot_changes = new_len / batadv_tt_len(1); 806
807 if (tt_diff_len == 0)
808 goto container_register;
434 809
435 spin_lock_bh(&bat_priv->tt.changes_list_lock); 810 spin_lock_bh(&bat_priv->tt.changes_list_lock);
436 atomic_set(&bat_priv->tt.local_changes, 0); 811 atomic_set(&bat_priv->tt.local_changes, 0);
437 812
438 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list, 813 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
439 list) { 814 list) {
440 if (count < tot_changes) { 815 if (tt_diff_entries_count < tt_diff_entries_num) {
441 memcpy(tt_buff + batadv_tt_len(count), 816 memcpy(tt_change + tt_diff_entries_count,
442 &entry->change, sizeof(struct batadv_tt_change)); 817 &entry->change,
443 count++; 818 sizeof(struct batadv_tvlv_tt_change));
819 tt_diff_entries_count++;
444 } 820 }
445 list_del(&entry->list); 821 list_del(&entry->list);
446 kfree(entry); 822 kfree(entry);
@@ -452,20 +828,25 @@ static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
452 kfree(bat_priv->tt.last_changeset); 828 kfree(bat_priv->tt.last_changeset);
453 bat_priv->tt.last_changeset_len = 0; 829 bat_priv->tt.last_changeset_len = 0;
454 bat_priv->tt.last_changeset = NULL; 830 bat_priv->tt.last_changeset = NULL;
831 tt_change_len = batadv_tt_len(tt_diff_entries_count);
455 /* check whether this new OGM has no changes due to size problems */ 832 /* check whether this new OGM has no changes due to size problems */
456 if (new_len > 0) { 833 if (tt_diff_entries_count > 0) {
457 /* if kmalloc() fails we will reply with the full table 834 /* if kmalloc() fails we will reply with the full table
458 * instead of providing the diff 835 * instead of providing the diff
459 */ 836 */
460 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC); 837 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
461 if (bat_priv->tt.last_changeset) { 838 if (bat_priv->tt.last_changeset) {
462 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len); 839 memcpy(bat_priv->tt.last_changeset,
463 bat_priv->tt.last_changeset_len = new_len; 840 tt_change, tt_change_len);
841 bat_priv->tt.last_changeset_len = tt_diff_len;
464 } 842 }
465 } 843 }
466 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 844 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
467 845
468 return count; 846container_register:
847 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
848 tvlv_len);
849 kfree(tt_data);
469} 850}
470 851
471int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset) 852int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
@@ -476,7 +857,9 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
476 struct batadv_tt_common_entry *tt_common_entry; 857 struct batadv_tt_common_entry *tt_common_entry;
477 struct batadv_tt_local_entry *tt_local; 858 struct batadv_tt_local_entry *tt_local;
478 struct batadv_hard_iface *primary_if; 859 struct batadv_hard_iface *primary_if;
860 struct batadv_softif_vlan *vlan;
479 struct hlist_head *head; 861 struct hlist_head *head;
862 unsigned short vid;
480 uint32_t i; 863 uint32_t i;
481 int last_seen_secs; 864 int last_seen_secs;
482 int last_seen_msecs; 865 int last_seen_msecs;
@@ -489,11 +872,10 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
489 goto out; 872 goto out;
490 873
491 seq_printf(seq, 874 seq_printf(seq,
492 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n", 875 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
493 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn), 876 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
494 bat_priv->tt.local_crc); 877 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
495 seq_printf(seq, " %-13s %-7s %-10s\n", "Client", "Flags", 878 "Flags", "Last seen", "CRC");
496 "Last seen");
497 879
498 for (i = 0; i < hash->size; i++) { 880 for (i = 0; i < hash->size; i++) {
499 head = &hash->table[i]; 881 head = &hash->table[i];
@@ -504,6 +886,7 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
504 tt_local = container_of(tt_common_entry, 886 tt_local = container_of(tt_common_entry,
505 struct batadv_tt_local_entry, 887 struct batadv_tt_local_entry,
506 common); 888 common);
889 vid = tt_common_entry->vid;
507 last_seen_jiffies = jiffies - tt_local->last_seen; 890 last_seen_jiffies = jiffies - tt_local->last_seen;
508 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); 891 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
509 last_seen_secs = last_seen_msecs / 1000; 892 last_seen_secs = last_seen_msecs / 1000;
@@ -511,8 +894,17 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
511 894
512 no_purge = tt_common_entry->flags & np_flag; 895 no_purge = tt_common_entry->flags & np_flag;
513 896
514 seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n", 897 vlan = batadv_softif_vlan_get(bat_priv, vid);
898 if (!vlan) {
899 seq_printf(seq, "Cannot retrieve VLAN %d\n",
900 BATADV_PRINT_VID(vid));
901 continue;
902 }
903
904 seq_printf(seq,
905 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
515 tt_common_entry->addr, 906 tt_common_entry->addr,
907 BATADV_PRINT_VID(tt_common_entry->vid),
516 (tt_common_entry->flags & 908 (tt_common_entry->flags &
517 BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 909 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
518 no_purge ? 'P' : '.', 910 no_purge ? 'P' : '.',
@@ -523,7 +915,10 @@ int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
523 (tt_common_entry->flags & 915 (tt_common_entry->flags &
524 BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 916 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
525 no_purge ? 0 : last_seen_secs, 917 no_purge ? 0 : last_seen_secs,
526 no_purge ? 0 : last_seen_msecs); 918 no_purge ? 0 : last_seen_msecs,
919 vlan->tt.crc);
920
921 batadv_softif_vlan_free_ref(vlan);
527 } 922 }
528 rcu_read_unlock(); 923 rcu_read_unlock();
529 } 924 }
@@ -547,27 +942,29 @@ batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
547 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING; 942 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
548 943
549 batadv_dbg(BATADV_DBG_TT, bat_priv, 944 batadv_dbg(BATADV_DBG_TT, bat_priv,
550 "Local tt entry (%pM) pending to be removed: %s\n", 945 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
551 tt_local_entry->common.addr, message); 946 tt_local_entry->common.addr,
947 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
552} 948}
553 949
554/** 950/**
555 * batadv_tt_local_remove - logically remove an entry from the local table 951 * batadv_tt_local_remove - logically remove an entry from the local table
556 * @bat_priv: the bat priv with all the soft interface information 952 * @bat_priv: the bat priv with all the soft interface information
557 * @addr: the MAC address of the client to remove 953 * @addr: the MAC address of the client to remove
954 * @vid: VLAN identifier
558 * @message: message to append to the log on deletion 955 * @message: message to append to the log on deletion
559 * @roaming: true if the deletion is due to a roaming event 956 * @roaming: true if the deletion is due to a roaming event
560 * 957 *
561 * Returns the flags assigned to the local entry before being deleted 958 * Returns the flags assigned to the local entry before being deleted
562 */ 959 */
563uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv, 960uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
564 const uint8_t *addr, const char *message, 961 const uint8_t *addr, unsigned short vid,
565 bool roaming) 962 const char *message, bool roaming)
566{ 963{
567 struct batadv_tt_local_entry *tt_local_entry; 964 struct batadv_tt_local_entry *tt_local_entry;
568 uint16_t flags, curr_flags = BATADV_NO_FLAGS; 965 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
569 966
570 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 967 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
571 if (!tt_local_entry) 968 if (!tt_local_entry)
572 goto out; 969 goto out;
573 970
@@ -603,8 +1000,16 @@ out:
603 return curr_flags; 1000 return curr_flags;
604} 1001}
605 1002
1003/**
1004 * batadv_tt_local_purge_list - purge inactive tt local entries
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @head: pointer to the list containing the local tt entries
1007 * @timeout: parameter deciding whether a given tt local entry is considered
1008 * inactive or not
1009 */
606static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv, 1010static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
607 struct hlist_head *head) 1011 struct hlist_head *head,
1012 int timeout)
608{ 1013{
609 struct batadv_tt_local_entry *tt_local_entry; 1014 struct batadv_tt_local_entry *tt_local_entry;
610 struct batadv_tt_common_entry *tt_common_entry; 1015 struct batadv_tt_common_entry *tt_common_entry;
@@ -622,8 +1027,7 @@ static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
622 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) 1027 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
623 continue; 1028 continue;
624 1029
625 if (!batadv_has_timed_out(tt_local_entry->last_seen, 1030 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
626 BATADV_TT_LOCAL_TIMEOUT))
627 continue; 1031 continue;
628 1032
629 batadv_tt_local_set_pending(bat_priv, tt_local_entry, 1033 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
@@ -631,7 +1035,14 @@ static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
631 } 1035 }
632} 1036}
633 1037
634static void batadv_tt_local_purge(struct batadv_priv *bat_priv) 1038/**
1039 * batadv_tt_local_purge - purge inactive tt local entries
1040 * @bat_priv: the bat priv with all the soft interface information
1041 * @timeout: parameter deciding whether a given tt local entry is considered
1042 * inactive or not
1043 */
1044static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1045 int timeout)
635{ 1046{
636 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 1047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
637 struct hlist_head *head; 1048 struct hlist_head *head;
@@ -643,7 +1054,7 @@ static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
643 list_lock = &hash->list_locks[i]; 1054 list_lock = &hash->list_locks[i];
644 1055
645 spin_lock_bh(list_lock); 1056 spin_lock_bh(list_lock);
646 batadv_tt_local_purge_list(bat_priv, head); 1057 batadv_tt_local_purge_list(bat_priv, head, timeout);
647 spin_unlock_bh(list_lock); 1058 spin_unlock_bh(list_lock);
648 } 1059 }
649} 1060}
@@ -784,7 +1195,7 @@ batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
784 1195
785 INIT_HLIST_NODE(&orig_entry->list); 1196 INIT_HLIST_NODE(&orig_entry->list);
786 atomic_inc(&orig_node->refcount); 1197 atomic_inc(&orig_node->refcount);
787 atomic_inc(&orig_node->tt_size); 1198 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
788 orig_entry->orig_node = orig_node; 1199 orig_entry->orig_node = orig_node;
789 orig_entry->ttvn = ttvn; 1200 orig_entry->ttvn = ttvn;
790 atomic_set(&orig_entry->refcount, 2); 1201 atomic_set(&orig_entry->refcount, 2);
@@ -803,6 +1214,7 @@ out:
803 * @bat_priv: the bat priv with all the soft interface information 1214 * @bat_priv: the bat priv with all the soft interface information
804 * @orig_node: the originator announcing the client 1215 * @orig_node: the originator announcing the client
805 * @tt_addr: the mac address of the non-mesh client 1216 * @tt_addr: the mac address of the non-mesh client
1217 * @vid: VLAN identifier
806 * @flags: TT flags that have to be set for this non-mesh client 1218 * @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 1219 * @ttvn: the tt version number ever announcing this non-mesh client
808 * 1220 *
@@ -813,21 +1225,28 @@ out:
813 * If a TT local entry exists for this non-mesh client remove it. 1225 * If a TT local entry exists for this non-mesh client remove it.
814 * 1226 *
815 * The caller must hold orig_node refcount. 1227 * The caller must hold orig_node refcount.
1228 *
1229 * Return true if the new entry has been added, false otherwise
816 */ 1230 */
817int batadv_tt_global_add(struct batadv_priv *bat_priv, 1231static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
818 struct batadv_orig_node *orig_node, 1232 struct batadv_orig_node *orig_node,
819 const unsigned char *tt_addr, uint16_t flags, 1233 const unsigned char *tt_addr,
820 uint8_t ttvn) 1234 unsigned short vid, uint16_t flags,
1235 uint8_t ttvn)
821{ 1236{
822 struct batadv_tt_global_entry *tt_global_entry; 1237 struct batadv_tt_global_entry *tt_global_entry;
823 struct batadv_tt_local_entry *tt_local_entry; 1238 struct batadv_tt_local_entry *tt_local_entry;
824 int ret = 0; 1239 bool ret = false;
825 int hash_added; 1240 int hash_added;
826 struct batadv_tt_common_entry *common; 1241 struct batadv_tt_common_entry *common;
827 uint16_t local_flags; 1242 uint16_t local_flags;
828 1243
829 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr); 1244 /* ignore global entries from backbone nodes */
830 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr); 1245 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1246 return true;
1247
1248 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1249 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
831 1250
832 /* if the node already has a local client for this entry, it has to wait 1251 /* 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 1252 * for a roaming advertisement instead of manually messing up the global
@@ -844,6 +1263,7 @@ int batadv_tt_global_add(struct batadv_priv *bat_priv,
844 1263
845 common = &tt_global_entry->common; 1264 common = &tt_global_entry->common;
846 memcpy(common->addr, tt_addr, ETH_ALEN); 1265 memcpy(common->addr, tt_addr, ETH_ALEN);
1266 common->vid = vid;
847 1267
848 common->flags = flags; 1268 common->flags = flags;
849 tt_global_entry->roam_at = 0; 1269 tt_global_entry->roam_at = 0;
@@ -861,7 +1281,7 @@ int batadv_tt_global_add(struct batadv_priv *bat_priv,
861 1281
862 hash_added = batadv_hash_add(bat_priv->tt.global_hash, 1282 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
863 batadv_compare_tt, 1283 batadv_compare_tt,
864 batadv_choose_orig, common, 1284 batadv_choose_tt, common,
865 &common->hash_entry); 1285 &common->hash_entry);
866 1286
867 if (unlikely(hash_added != 0)) { 1287 if (unlikely(hash_added != 0)) {
@@ -920,14 +1340,15 @@ add_orig_entry:
920 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn); 1340 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
921 1341
922 batadv_dbg(BATADV_DBG_TT, bat_priv, 1342 batadv_dbg(BATADV_DBG_TT, bat_priv,
923 "Creating new global tt entry: %pM (via %pM)\n", 1343 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
924 common->addr, orig_node->orig); 1344 common->addr, BATADV_PRINT_VID(common->vid),
925 ret = 1; 1345 orig_node->orig);
1346 ret = true;
926 1347
927out_remove: 1348out_remove:
928 1349
929 /* remove address from local hash if present */ 1350 /* remove address from local hash if present */
930 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, 1351 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
931 "global tt received", 1352 "global tt received",
932 flags & BATADV_TT_CLIENT_ROAM); 1353 flags & BATADV_TT_CLIENT_ROAM);
933 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI; 1354 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
@@ -947,18 +1368,20 @@ out:
947} 1368}
948 1369
949/* batadv_transtable_best_orig - Get best originator list entry from tt entry 1370/* batadv_transtable_best_orig - Get best originator list entry from tt entry
1371 * @bat_priv: the bat priv with all the soft interface information
950 * @tt_global_entry: global translation table entry to be analyzed 1372 * @tt_global_entry: global translation table entry to be analyzed
951 * 1373 *
952 * This functon assumes the caller holds rcu_read_lock(). 1374 * This functon assumes the caller holds rcu_read_lock().
953 * Returns best originator list entry or NULL on errors. 1375 * Returns best originator list entry or NULL on errors.
954 */ 1376 */
955static struct batadv_tt_orig_list_entry * 1377static struct batadv_tt_orig_list_entry *
956batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry) 1378batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1379 struct batadv_tt_global_entry *tt_global_entry)
957{ 1380{
958 struct batadv_neigh_node *router = NULL; 1381 struct batadv_neigh_node *router, *best_router = NULL;
1382 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
959 struct hlist_head *head; 1383 struct hlist_head *head;
960 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL; 1384 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
961 int best_tq = 0;
962 1385
963 head = &tt_global_entry->orig_list; 1386 head = &tt_global_entry->orig_list;
964 hlist_for_each_entry_rcu(orig_entry, head, list) { 1387 hlist_for_each_entry_rcu(orig_entry, head, list) {
@@ -966,64 +1389,104 @@ batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
966 if (!router) 1389 if (!router)
967 continue; 1390 continue;
968 1391
969 if (router->tq_avg > best_tq) { 1392 if (best_router &&
970 best_entry = orig_entry; 1393 bao->bat_neigh_cmp(router, best_router) <= 0) {
971 best_tq = router->tq_avg; 1394 batadv_neigh_node_free_ref(router);
1395 continue;
972 } 1396 }
973 1397
974 batadv_neigh_node_free_ref(router); 1398 /* release the refcount for the "old" best */
1399 if (best_router)
1400 batadv_neigh_node_free_ref(best_router);
1401
1402 best_entry = orig_entry;
1403 best_router = router;
975 } 1404 }
976 1405
1406 if (best_router)
1407 batadv_neigh_node_free_ref(best_router);
1408
977 return best_entry; 1409 return best_entry;
978} 1410}
979 1411
980/* batadv_tt_global_print_entry - print all orig nodes who announce the address 1412/* batadv_tt_global_print_entry - print all orig nodes who announce the address
981 * for this global entry 1413 * for this global entry
1414 * @bat_priv: the bat priv with all the soft interface information
982 * @tt_global_entry: global translation table entry to be printed 1415 * @tt_global_entry: global translation table entry to be printed
983 * @seq: debugfs table seq_file struct 1416 * @seq: debugfs table seq_file struct
984 * 1417 *
985 * This functon assumes the caller holds rcu_read_lock(). 1418 * This functon assumes the caller holds rcu_read_lock().
986 */ 1419 */
987static void 1420static void
988batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry, 1421batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1422 struct batadv_tt_global_entry *tt_global_entry,
989 struct seq_file *seq) 1423 struct seq_file *seq)
990{ 1424{
991 struct hlist_head *head;
992 struct batadv_tt_orig_list_entry *orig_entry, *best_entry; 1425 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
993 struct batadv_tt_common_entry *tt_common_entry; 1426 struct batadv_tt_common_entry *tt_common_entry;
994 uint16_t flags; 1427 struct batadv_orig_node_vlan *vlan;
1428 struct hlist_head *head;
995 uint8_t last_ttvn; 1429 uint8_t last_ttvn;
1430 uint16_t flags;
996 1431
997 tt_common_entry = &tt_global_entry->common; 1432 tt_common_entry = &tt_global_entry->common;
998 flags = tt_common_entry->flags; 1433 flags = tt_common_entry->flags;
999 1434
1000 best_entry = batadv_transtable_best_orig(tt_global_entry); 1435 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1001 if (best_entry) { 1436 if (best_entry) {
1437 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1438 tt_common_entry->vid);
1439 if (!vlan) {
1440 seq_printf(seq,
1441 " * Cannot retrieve VLAN %d for originator %pM\n",
1442 BATADV_PRINT_VID(tt_common_entry->vid),
1443 best_entry->orig_node->orig);
1444 goto print_list;
1445 }
1446
1002 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn); 1447 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1003 seq_printf(seq, 1448 seq_printf(seq,
1004 " %c %pM (%3u) via %pM (%3u) (%#.4x) [%c%c%c]\n", 1449 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1005 '*', tt_global_entry->common.addr, 1450 '*', tt_global_entry->common.addr,
1451 BATADV_PRINT_VID(tt_global_entry->common.vid),
1006 best_entry->ttvn, best_entry->orig_node->orig, 1452 best_entry->ttvn, best_entry->orig_node->orig,
1007 last_ttvn, best_entry->orig_node->tt_crc, 1453 last_ttvn, vlan->tt.crc,
1008 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 1454 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1009 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 1455 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1010 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.')); 1456 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1457
1458 batadv_orig_node_vlan_free_ref(vlan);
1011 } 1459 }
1012 1460
1461print_list:
1013 head = &tt_global_entry->orig_list; 1462 head = &tt_global_entry->orig_list;
1014 1463
1015 hlist_for_each_entry_rcu(orig_entry, head, list) { 1464 hlist_for_each_entry_rcu(orig_entry, head, list) {
1016 if (best_entry == orig_entry) 1465 if (best_entry == orig_entry)
1017 continue; 1466 continue;
1018 1467
1468 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1469 tt_common_entry->vid);
1470 if (!vlan) {
1471 seq_printf(seq,
1472 " + Cannot retrieve VLAN %d for originator %pM\n",
1473 BATADV_PRINT_VID(tt_common_entry->vid),
1474 orig_entry->orig_node->orig);
1475 continue;
1476 }
1477
1019 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn); 1478 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1020 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n", 1479 seq_printf(seq,
1480 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1021 '+', tt_global_entry->common.addr, 1481 '+', tt_global_entry->common.addr,
1482 BATADV_PRINT_VID(tt_global_entry->common.vid),
1022 orig_entry->ttvn, orig_entry->orig_node->orig, 1483 orig_entry->ttvn, orig_entry->orig_node->orig,
1023 last_ttvn, 1484 last_ttvn, vlan->tt.crc,
1024 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'), 1485 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1025 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'), 1486 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1026 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.')); 1487 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1488
1489 batadv_orig_node_vlan_free_ref(vlan);
1027 } 1490 }
1028} 1491}
1029 1492
@@ -1045,9 +1508,9 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1045 seq_printf(seq, 1508 seq_printf(seq,
1046 "Globally announced TT entries received via the mesh %s\n", 1509 "Globally announced TT entries received via the mesh %s\n",
1047 net_dev->name); 1510 net_dev->name);
1048 seq_printf(seq, " %-13s %s %-15s %s (%-6s) %s\n", 1511 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1049 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC", 1512 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1050 "Flags"); 1513 "CRC", "Flags");
1051 1514
1052 for (i = 0; i < hash->size; i++) { 1515 for (i = 0; i < hash->size; i++) {
1053 head = &hash->table[i]; 1516 head = &hash->table[i];
@@ -1058,7 +1521,7 @@ int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1058 tt_global = container_of(tt_common_entry, 1521 tt_global = container_of(tt_common_entry,
1059 struct batadv_tt_global_entry, 1522 struct batadv_tt_global_entry,
1060 common); 1523 common);
1061 batadv_tt_global_print_entry(tt_global, seq); 1524 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1062 } 1525 }
1063 rcu_read_unlock(); 1526 rcu_read_unlock();
1064 } 1527 }
@@ -1080,6 +1543,8 @@ batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1080 head = &tt_global_entry->orig_list; 1543 head = &tt_global_entry->orig_list;
1081 hlist_for_each_entry_safe(orig_entry, safe, head, list) { 1544 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1082 hlist_del_rcu(&orig_entry->list); 1545 hlist_del_rcu(&orig_entry->list);
1546 batadv_tt_global_size_dec(orig_entry->orig_node,
1547 tt_global_entry->common.vid);
1083 batadv_tt_orig_list_entry_free_ref(orig_entry); 1548 batadv_tt_orig_list_entry_free_ref(orig_entry);
1084 } 1549 }
1085 spin_unlock_bh(&tt_global_entry->list_lock); 1550 spin_unlock_bh(&tt_global_entry->list_lock);
@@ -1094,16 +1559,21 @@ batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1094 struct hlist_head *head; 1559 struct hlist_head *head;
1095 struct hlist_node *safe; 1560 struct hlist_node *safe;
1096 struct batadv_tt_orig_list_entry *orig_entry; 1561 struct batadv_tt_orig_list_entry *orig_entry;
1562 unsigned short vid;
1097 1563
1098 spin_lock_bh(&tt_global_entry->list_lock); 1564 spin_lock_bh(&tt_global_entry->list_lock);
1099 head = &tt_global_entry->orig_list; 1565 head = &tt_global_entry->orig_list;
1100 hlist_for_each_entry_safe(orig_entry, safe, head, list) { 1566 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1101 if (orig_entry->orig_node == orig_node) { 1567 if (orig_entry->orig_node == orig_node) {
1568 vid = tt_global_entry->common.vid;
1102 batadv_dbg(BATADV_DBG_TT, bat_priv, 1569 batadv_dbg(BATADV_DBG_TT, bat_priv,
1103 "Deleting %pM from global tt entry %pM: %s\n", 1570 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1104 orig_node->orig, 1571 orig_node->orig,
1105 tt_global_entry->common.addr, message); 1572 tt_global_entry->common.addr,
1573 BATADV_PRINT_VID(vid), message);
1106 hlist_del_rcu(&orig_entry->list); 1574 hlist_del_rcu(&orig_entry->list);
1575 batadv_tt_global_size_dec(orig_node,
1576 tt_global_entry->common.vid);
1107 batadv_tt_orig_list_entry_free_ref(orig_entry); 1577 batadv_tt_orig_list_entry_free_ref(orig_entry);
1108 } 1578 }
1109 } 1579 }
@@ -1150,17 +1620,25 @@ batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1150 orig_node, message); 1620 orig_node, message);
1151} 1621}
1152 1622
1153 1623/**
1154 1624 * batadv_tt_global_del - remove a client from the global table
1625 * @bat_priv: the bat priv with all the soft interface information
1626 * @orig_node: an originator serving this client
1627 * @addr: the mac address of the client
1628 * @vid: VLAN identifier
1629 * @message: a message explaining the reason for deleting the client to print
1630 * for debugging purpose
1631 * @roaming: true if the deletion has been triggered by a roaming event
1632 */
1155static void batadv_tt_global_del(struct batadv_priv *bat_priv, 1633static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1156 struct batadv_orig_node *orig_node, 1634 struct batadv_orig_node *orig_node,
1157 const unsigned char *addr, 1635 const unsigned char *addr, unsigned short vid,
1158 const char *message, bool roaming) 1636 const char *message, bool roaming)
1159{ 1637{
1160 struct batadv_tt_global_entry *tt_global_entry; 1638 struct batadv_tt_global_entry *tt_global_entry;
1161 struct batadv_tt_local_entry *local_entry = NULL; 1639 struct batadv_tt_local_entry *local_entry = NULL;
1162 1640
1163 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 1641 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1164 if (!tt_global_entry) 1642 if (!tt_global_entry)
1165 goto out; 1643 goto out;
1166 1644
@@ -1189,7 +1667,8 @@ static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1189 * the global entry, since it is useless now. 1667 * the global entry, since it is useless now.
1190 */ 1668 */
1191 local_entry = batadv_tt_local_hash_find(bat_priv, 1669 local_entry = batadv_tt_local_hash_find(bat_priv,
1192 tt_global_entry->common.addr); 1670 tt_global_entry->common.addr,
1671 vid);
1193 if (local_entry) { 1672 if (local_entry) {
1194 /* local entry exists, case 2: client roamed to us. */ 1673 /* local entry exists, case 2: client roamed to us. */
1195 batadv_tt_global_del_orig_list(tt_global_entry); 1674 batadv_tt_global_del_orig_list(tt_global_entry);
@@ -1207,8 +1686,18 @@ out:
1207 batadv_tt_local_entry_free_ref(local_entry); 1686 batadv_tt_local_entry_free_ref(local_entry);
1208} 1687}
1209 1688
1689/**
1690 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1691 * given originator matching the provided vid
1692 * @bat_priv: the bat priv with all the soft interface information
1693 * @orig_node: the originator owning the entries to remove
1694 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1695 * removed
1696 * @message: debug message to print as "reason"
1697 */
1210void batadv_tt_global_del_orig(struct batadv_priv *bat_priv, 1698void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1211 struct batadv_orig_node *orig_node, 1699 struct batadv_orig_node *orig_node,
1700 int32_t match_vid,
1212 const char *message) 1701 const char *message)
1213{ 1702{
1214 struct batadv_tt_global_entry *tt_global; 1703 struct batadv_tt_global_entry *tt_global;
@@ -1218,6 +1707,7 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1218 struct hlist_node *safe; 1707 struct hlist_node *safe;
1219 struct hlist_head *head; 1708 struct hlist_head *head;
1220 spinlock_t *list_lock; /* protects write access to the hash lists */ 1709 spinlock_t *list_lock; /* protects write access to the hash lists */
1710 unsigned short vid;
1221 1711
1222 if (!hash) 1712 if (!hash)
1223 return; 1713 return;
@@ -1229,6 +1719,10 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1229 spin_lock_bh(list_lock); 1719 spin_lock_bh(list_lock);
1230 hlist_for_each_entry_safe(tt_common_entry, safe, 1720 hlist_for_each_entry_safe(tt_common_entry, safe,
1231 head, hash_entry) { 1721 head, hash_entry) {
1722 /* remove only matching entries */
1723 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1724 continue;
1725
1232 tt_global = container_of(tt_common_entry, 1726 tt_global = container_of(tt_common_entry,
1233 struct batadv_tt_global_entry, 1727 struct batadv_tt_global_entry,
1234 common); 1728 common);
@@ -1237,9 +1731,11 @@ void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1237 orig_node, message); 1731 orig_node, message);
1238 1732
1239 if (hlist_empty(&tt_global->orig_list)) { 1733 if (hlist_empty(&tt_global->orig_list)) {
1734 vid = tt_global->common.vid;
1240 batadv_dbg(BATADV_DBG_TT, bat_priv, 1735 batadv_dbg(BATADV_DBG_TT, bat_priv,
1241 "Deleting global tt entry %pM: %s\n", 1736 "Deleting global tt entry %pM (vid: %d): %s\n",
1242 tt_global->common.addr, message); 1737 tt_global->common.addr,
1738 BATADV_PRINT_VID(vid), message);
1243 hlist_del_rcu(&tt_common_entry->hash_entry); 1739 hlist_del_rcu(&tt_common_entry->hash_entry);
1244 batadv_tt_global_entry_free_ref(tt_global); 1740 batadv_tt_global_entry_free_ref(tt_global);
1245 } 1741 }
@@ -1297,8 +1793,10 @@ static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1297 continue; 1793 continue;
1298 1794
1299 batadv_dbg(BATADV_DBG_TT, bat_priv, 1795 batadv_dbg(BATADV_DBG_TT, bat_priv,
1300 "Deleting global tt entry (%pM): %s\n", 1796 "Deleting global tt entry %pM (vid: %d): %s\n",
1301 tt_global->common.addr, msg); 1797 tt_global->common.addr,
1798 BATADV_PRINT_VID(tt_global->common.vid),
1799 msg);
1302 1800
1303 hlist_del_rcu(&tt_common->hash_entry); 1801 hlist_del_rcu(&tt_common->hash_entry);
1304 1802
@@ -1357,23 +1855,49 @@ _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1357 return ret; 1855 return ret;
1358} 1856}
1359 1857
1858/**
1859 * batadv_transtable_search - get the mesh destination for a given client
1860 * @bat_priv: the bat priv with all the soft interface information
1861 * @src: mac address of the source client
1862 * @addr: mac address of the destination client
1863 * @vid: VLAN identifier
1864 *
1865 * Returns a pointer to the originator that was selected as destination in the
1866 * mesh for contacting the client 'addr', NULL otherwise.
1867 * In case of multiple originators serving the same client, the function returns
1868 * the best one (best in terms of metric towards the destination node).
1869 *
1870 * If the two clients are AP isolated the function returns NULL.
1871 */
1360struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv, 1872struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1361 const uint8_t *src, 1873 const uint8_t *src,
1362 const uint8_t *addr) 1874 const uint8_t *addr,
1875 unsigned short vid)
1363{ 1876{
1364 struct batadv_tt_local_entry *tt_local_entry = NULL; 1877 struct batadv_tt_local_entry *tt_local_entry = NULL;
1365 struct batadv_tt_global_entry *tt_global_entry = NULL; 1878 struct batadv_tt_global_entry *tt_global_entry = NULL;
1366 struct batadv_orig_node *orig_node = NULL; 1879 struct batadv_orig_node *orig_node = NULL;
1367 struct batadv_tt_orig_list_entry *best_entry; 1880 struct batadv_tt_orig_list_entry *best_entry;
1881 bool ap_isolation_enabled = false;
1882 struct batadv_softif_vlan *vlan;
1368 1883
1369 if (src && atomic_read(&bat_priv->ap_isolation)) { 1884 /* 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); 1885 * setting in the proper VLAN private data structure
1886 */
1887 vlan = batadv_softif_vlan_get(bat_priv, vid);
1888 if (vlan) {
1889 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1890 batadv_softif_vlan_free_ref(vlan);
1891 }
1892
1893 if (src && ap_isolation_enabled) {
1894 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
1371 if (!tt_local_entry || 1895 if (!tt_local_entry ||
1372 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)) 1896 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1373 goto out; 1897 goto out;
1374 } 1898 }
1375 1899
1376 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 1900 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1377 if (!tt_global_entry) 1901 if (!tt_global_entry)
1378 goto out; 1902 goto out;
1379 1903
@@ -1385,7 +1909,7 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1385 goto out; 1909 goto out;
1386 1910
1387 rcu_read_lock(); 1911 rcu_read_lock();
1388 best_entry = batadv_transtable_best_orig(tt_global_entry); 1912 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1389 /* found anything? */ 1913 /* found anything? */
1390 if (best_entry) 1914 if (best_entry)
1391 orig_node = best_entry->orig_node; 1915 orig_node = best_entry->orig_node;
@@ -1402,17 +1926,40 @@ out:
1402 return orig_node; 1926 return orig_node;
1403} 1927}
1404 1928
1405/* Calculates the checksum of the local table of a given orig_node */ 1929/**
1406static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv, 1930 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1407 struct batadv_orig_node *orig_node) 1931 * to the given orig_node
1932 * @bat_priv: the bat priv with all the soft interface information
1933 * @orig_node: originator for which the CRC should be computed
1934 * @vid: VLAN identifier for which the CRC32 has to be computed
1935 *
1936 * This function computes the checksum for the global table corresponding to a
1937 * specific originator. In particular, the checksum is computed as follows: For
1938 * each client connected to the originator the CRC32C of the MAC address and the
1939 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1940 * together.
1941 *
1942 * The idea behind is that CRC32C should be used as much as possible in order to
1943 * produce a unique hash of the table, but since the order which is used to feed
1944 * the CRC32C function affects the result and since every node in the network
1945 * probably sorts the clients differently, the hash function cannot be directly
1946 * computed over the entire table. Hence the CRC32C is used only on
1947 * the single client entry, while all the results are then xor'ed together
1948 * because the XOR operation can combine them all while trying to reduce the
1949 * noise as much as possible.
1950 *
1951 * Returns the checksum of the global table of a given originator.
1952 */
1953static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1954 struct batadv_orig_node *orig_node,
1955 unsigned short vid)
1408{ 1956{
1409 uint16_t total = 0, total_one;
1410 struct batadv_hashtable *hash = bat_priv->tt.global_hash; 1957 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1411 struct batadv_tt_common_entry *tt_common; 1958 struct batadv_tt_common_entry *tt_common;
1412 struct batadv_tt_global_entry *tt_global; 1959 struct batadv_tt_global_entry *tt_global;
1413 struct hlist_head *head; 1960 struct hlist_head *head;
1414 uint32_t i; 1961 uint32_t i, crc_tmp, crc = 0;
1415 int j; 1962 uint8_t flags;
1416 1963
1417 for (i = 0; i < hash->size; i++) { 1964 for (i = 0; i < hash->size; i++) {
1418 head = &hash->table[i]; 1965 head = &hash->table[i];
@@ -1422,6 +1969,12 @@ static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1422 tt_global = container_of(tt_common, 1969 tt_global = container_of(tt_common,
1423 struct batadv_tt_global_entry, 1970 struct batadv_tt_global_entry,
1424 common); 1971 common);
1972 /* compute the CRC only for entries belonging to the
1973 * VLAN identified by the vid passed as parameter
1974 */
1975 if (tt_common->vid != vid)
1976 continue;
1977
1425 /* Roaming clients are in the global table for 1978 /* Roaming clients are in the global table for
1426 * consistency only. They don't have to be 1979 * consistency only. They don't have to be
1427 * taken into account while computing the 1980 * taken into account while computing the
@@ -1443,48 +1996,74 @@ static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1443 orig_node)) 1996 orig_node))
1444 continue; 1997 continue;
1445 1998
1446 total_one = 0; 1999 crc_tmp = crc32c(0, &tt_common->vid,
1447 for (j = 0; j < ETH_ALEN; j++) 2000 sizeof(tt_common->vid));
1448 total_one = crc16_byte(total_one, 2001
1449 tt_common->addr[j]); 2002 /* compute the CRC on flags that have to be kept in sync
1450 total ^= total_one; 2003 * among nodes
2004 */
2005 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2006 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2007
2008 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
1451 } 2009 }
1452 rcu_read_unlock(); 2010 rcu_read_unlock();
1453 } 2011 }
1454 2012
1455 return total; 2013 return crc;
1456} 2014}
1457 2015
1458/* Calculates the checksum of the local table */ 2016/**
1459static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv) 2017 * batadv_tt_local_crc - calculates the checksum of the local table
2018 * @bat_priv: the bat priv with all the soft interface information
2019 * @vid: VLAN identifier for which the CRC32 has to be computed
2020 *
2021 * For details about the computation, please refer to the documentation for
2022 * batadv_tt_global_crc().
2023 *
2024 * Returns the checksum of the local table
2025 */
2026static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2027 unsigned short vid)
1460{ 2028{
1461 uint16_t total = 0, total_one;
1462 struct batadv_hashtable *hash = bat_priv->tt.local_hash; 2029 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1463 struct batadv_tt_common_entry *tt_common; 2030 struct batadv_tt_common_entry *tt_common;
1464 struct hlist_head *head; 2031 struct hlist_head *head;
1465 uint32_t i; 2032 uint32_t i, crc_tmp, crc = 0;
1466 int j; 2033 uint8_t flags;
1467 2034
1468 for (i = 0; i < hash->size; i++) { 2035 for (i = 0; i < hash->size; i++) {
1469 head = &hash->table[i]; 2036 head = &hash->table[i];
1470 2037
1471 rcu_read_lock(); 2038 rcu_read_lock();
1472 hlist_for_each_entry_rcu(tt_common, head, hash_entry) { 2039 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2040 /* compute the CRC only for entries belonging to the
2041 * VLAN identified by vid
2042 */
2043 if (tt_common->vid != vid)
2044 continue;
2045
1473 /* not yet committed clients have not to be taken into 2046 /* not yet committed clients have not to be taken into
1474 * account while computing the CRC 2047 * account while computing the CRC
1475 */ 2048 */
1476 if (tt_common->flags & BATADV_TT_CLIENT_NEW) 2049 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1477 continue; 2050 continue;
1478 total_one = 0; 2051
1479 for (j = 0; j < ETH_ALEN; j++) 2052 crc_tmp = crc32c(0, &tt_common->vid,
1480 total_one = crc16_byte(total_one, 2053 sizeof(tt_common->vid));
1481 tt_common->addr[j]); 2054
1482 total ^= total_one; 2055 /* compute the CRC on flags that have to be kept in sync
2056 * among nodes
2057 */
2058 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2059 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2060
2061 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
1483 } 2062 }
1484 rcu_read_unlock(); 2063 rcu_read_unlock();
1485 } 2064 }
1486 2065
1487 return total; 2066 return crc;
1488} 2067}
1489 2068
1490static void batadv_tt_req_list_free(struct batadv_priv *bat_priv) 2069static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
@@ -1503,11 +2082,9 @@ static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1503 2082
1504static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv, 2083static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1505 struct batadv_orig_node *orig_node, 2084 struct batadv_orig_node *orig_node,
1506 const unsigned char *tt_buff, 2085 const void *tt_buff,
1507 uint8_t tt_num_changes) 2086 uint16_t tt_buff_len)
1508{ 2087{
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 2088 /* Replace the old buffer only if I received something in the
1512 * last OGM (the OGM could carry no changes) 2089 * last OGM (the OGM could carry no changes)
1513 */ 2090 */
@@ -1569,9 +2146,14 @@ unlock:
1569 return tt_req_node; 2146 return tt_req_node;
1570} 2147}
1571 2148
1572/* data_ptr is useless here, but has to be kept to respect the prototype */ 2149/**
1573static int batadv_tt_local_valid_entry(const void *entry_ptr, 2150 * batadv_tt_local_valid - verify that given tt entry is a valid one
1574 const void *data_ptr) 2151 * @entry_ptr: to be checked local tt entry
2152 * @data_ptr: not used but definition required to satisfy the callback prototype
2153 *
2154 * Returns 1 if the entry is a valid, 0 otherwise.
2155 */
2156static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
1575{ 2157{
1576 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; 2158 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1577 2159
@@ -1598,41 +2180,30 @@ static int batadv_tt_global_valid(const void *entry_ptr,
1598 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node); 2180 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1599} 2181}
1600 2182
1601static struct sk_buff * 2183/**
1602batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn, 2184 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
1603 struct batadv_hashtable *hash, 2185 * specified tt hash
1604 struct batadv_priv *bat_priv, 2186 * @bat_priv: the bat priv with all the soft interface information
1605 int (*valid_cb)(const void *, const void *), 2187 * @hash: hash table containing the tt entries
1606 void *cb_data) 2188 * @tt_len: expected tvlv tt data buffer length in number of bytes
2189 * @tvlv_buff: pointer to the buffer to fill with the TT data
2190 * @valid_cb: function to filter tt change entries
2191 * @cb_data: data passed to the filter function as argument
2192 */
2193static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2194 struct batadv_hashtable *hash,
2195 void *tvlv_buff, uint16_t tt_len,
2196 int (*valid_cb)(const void *, const void *),
2197 void *cb_data)
1607{ 2198{
1608 struct batadv_tt_common_entry *tt_common_entry; 2199 struct batadv_tt_common_entry *tt_common_entry;
1609 struct batadv_tt_query_packet *tt_response; 2200 struct batadv_tvlv_tt_change *tt_change;
1610 struct batadv_tt_change *tt_change;
1611 struct hlist_head *head; 2201 struct hlist_head *head;
1612 struct sk_buff *skb = NULL; 2202 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; 2203 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 2204
1634 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size); 2205 tt_tot = batadv_tt_entries(tt_len);
1635 tt_count = 0; 2206 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
1636 2207
1637 rcu_read_lock(); 2208 rcu_read_lock();
1638 for (i = 0; i < hash->size; i++) { 2209 for (i = 0; i < hash->size; i++) {
@@ -1640,7 +2211,7 @@ batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1640 2211
1641 hlist_for_each_entry_rcu(tt_common_entry, 2212 hlist_for_each_entry_rcu(tt_common_entry,
1642 head, hash_entry) { 2213 head, hash_entry) {
1643 if (tt_count == tt_tot) 2214 if (tt_tot == tt_num_entries)
1644 break; 2215 break;
1645 2216
1646 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data))) 2217 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
@@ -1649,33 +2220,123 @@ batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1649 memcpy(tt_change->addr, tt_common_entry->addr, 2220 memcpy(tt_change->addr, tt_common_entry->addr,
1650 ETH_ALEN); 2221 ETH_ALEN);
1651 tt_change->flags = tt_common_entry->flags; 2222 tt_change->flags = tt_common_entry->flags;
2223 tt_change->vid = htons(tt_common_entry->vid);
2224 tt_change->reserved = 0;
1652 2225
1653 tt_count++; 2226 tt_num_entries++;
1654 tt_change++; 2227 tt_change++;
1655 } 2228 }
1656 } 2229 }
1657 rcu_read_unlock(); 2230 rcu_read_unlock();
2231}
1658 2232
1659 /* store in the message the number of entries we have successfully 2233/**
1660 * copied 2234 * batadv_tt_global_check_crc - check if all the CRCs are correct
1661 */ 2235 * @orig_node: originator for which the CRCs have to be checked
1662 tt_response->tt_data = htons(tt_count); 2236 * @tt_vlan: pointer to the first tvlv VLAN entry
2237 * @num_vlan: number of tvlv VLAN entries
2238 * @create: if true, create VLAN objects if not found
2239 *
2240 * Return true if all the received CRCs match the locally stored ones, false
2241 * otherwise
2242 */
2243static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2244 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2245 uint16_t num_vlan)
2246{
2247 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2248 struct batadv_orig_node_vlan *vlan;
2249 int i;
1663 2250
1664out: 2251 /* check if each received CRC matches the locally stored one */
1665 return skb; 2252 for (i = 0; i < num_vlan; i++) {
2253 tt_vlan_tmp = tt_vlan + i;
2254
2255 /* if orig_node is a backbone node for this VLAN, don't check
2256 * the CRC as we ignore all the global entries over it
2257 */
2258 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2259 orig_node->orig,
2260 ntohs(tt_vlan_tmp->vid)))
2261 continue;
2262
2263 vlan = batadv_orig_node_vlan_get(orig_node,
2264 ntohs(tt_vlan_tmp->vid));
2265 if (!vlan)
2266 return false;
2267
2268 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2269 return false;
2270 }
2271
2272 return true;
2273}
2274
2275/**
2276 * batadv_tt_local_update_crc - update all the local CRCs
2277 * @bat_priv: the bat priv with all the soft interface information
2278 */
2279static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2280{
2281 struct batadv_softif_vlan *vlan;
2282
2283 /* recompute the global CRC for each VLAN */
2284 rcu_read_lock();
2285 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2286 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2287 }
2288 rcu_read_unlock();
1666} 2289}
1667 2290
2291/**
2292 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2293 * @bat_priv: the bat priv with all the soft interface information
2294 * @orig_node: the orig_node for which the CRCs have to be updated
2295 */
2296static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2297 struct batadv_orig_node *orig_node)
2298{
2299 struct batadv_orig_node_vlan *vlan;
2300 uint32_t crc;
2301
2302 /* recompute the global CRC for each VLAN */
2303 rcu_read_lock();
2304 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2305 /* if orig_node is a backbone node for this VLAN, don't compute
2306 * the CRC as we ignore all the global entries over it
2307 */
2308 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2309 vlan->vid))
2310 continue;
2311
2312 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2313 vlan->tt.crc = crc;
2314 }
2315 rcu_read_unlock();
2316}
2317
2318/**
2319 * batadv_send_tt_request - send a TT Request message to a given node
2320 * @bat_priv: the bat priv with all the soft interface information
2321 * @dst_orig_node: the destination of the message
2322 * @ttvn: the version number that the source of the message is looking for
2323 * @tt_vlan: pointer to the first tvlv VLAN object to request
2324 * @num_vlan: number of tvlv VLAN entries
2325 * @full_table: ask for the entire translation table if true, while only for the
2326 * last TT diff otherwise
2327 */
1668static int batadv_send_tt_request(struct batadv_priv *bat_priv, 2328static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1669 struct batadv_orig_node *dst_orig_node, 2329 struct batadv_orig_node *dst_orig_node,
1670 uint8_t ttvn, uint16_t tt_crc, 2330 uint8_t ttvn,
1671 bool full_table) 2331 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2332 uint16_t num_vlan, bool full_table)
1672{ 2333{
1673 struct sk_buff *skb = NULL; 2334 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; 2335 struct batadv_tt_req_node *tt_req_node = NULL;
1677 int ret = 1; 2336 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
1678 size_t tt_req_len; 2337 struct batadv_hard_iface *primary_if;
2338 bool ret = false;
2339 int i, size;
1679 2340
1680 primary_if = batadv_primary_if_get_selected(bat_priv); 2341 primary_if = batadv_primary_if_get_selected(bat_priv);
1681 if (!primary_if) 2342 if (!primary_if)
@@ -1688,157 +2349,171 @@ static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1688 if (!tt_req_node) 2349 if (!tt_req_node)
1689 goto out; 2350 goto out;
1690 2351
1691 skb = netdev_alloc_skb_ip_align(NULL, sizeof(*tt_request) + ETH_HLEN); 2352 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
1692 if (!skb) 2353 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2354 if (!tvlv_tt_data)
1693 goto out; 2355 goto out;
1694 2356
1695 skb->priority = TC_PRIO_CONTROL; 2357 tvlv_tt_data->flags = BATADV_TT_REQUEST;
1696 skb_reserve(skb, ETH_HLEN); 2358 tvlv_tt_data->ttvn = ttvn;
2359 tvlv_tt_data->num_vlan = htons(num_vlan);
1697 2360
1698 tt_req_len = sizeof(*tt_request); 2361 /* 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); 2362 * nodes to ensure they have the correct table before replying
2363 */
2364 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2365 for (i = 0; i < num_vlan; i++) {
2366 tt_vlan_req->vid = tt_vlan->vid;
2367 tt_vlan_req->crc = tt_vlan->crc;
1700 2368
1701 tt_request->header.packet_type = BATADV_TT_QUERY; 2369 tt_vlan_req++;
1702 tt_request->header.version = BATADV_COMPAT_VERSION; 2370 tt_vlan++;
1703 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN); 2371 }
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 2372
1710 if (full_table) 2373 if (full_table)
1711 tt_request->flags |= BATADV_TT_FULL_TABLE; 2374 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1712 2375
1713 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n", 2376 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1714 dst_orig_node->orig, (full_table ? 'F' : '.')); 2377 dst_orig_node->orig, full_table ? 'F' : '.');
1715 2378
1716 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX); 2379 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1717 2380 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) 2381 dst_orig_node->orig, BATADV_TVLV_TT, 1,
1719 ret = 0; 2382 tvlv_tt_data, size);
2383 ret = true;
1720 2384
1721out: 2385out:
1722 if (primary_if) 2386 if (primary_if)
1723 batadv_hardif_free_ref(primary_if); 2387 batadv_hardif_free_ref(primary_if);
1724 if (ret)
1725 kfree_skb(skb);
1726 if (ret && tt_req_node) { 2388 if (ret && tt_req_node) {
1727 spin_lock_bh(&bat_priv->tt.req_list_lock); 2389 spin_lock_bh(&bat_priv->tt.req_list_lock);
1728 list_del(&tt_req_node->list); 2390 list_del(&tt_req_node->list);
1729 spin_unlock_bh(&bat_priv->tt.req_list_lock); 2391 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1730 kfree(tt_req_node); 2392 kfree(tt_req_node);
1731 } 2393 }
2394 kfree(tvlv_tt_data);
1732 return ret; 2395 return ret;
1733} 2396}
1734 2397
1735static bool 2398/**
1736batadv_send_other_tt_response(struct batadv_priv *bat_priv, 2399 * batadv_send_other_tt_response - send reply to tt request concerning another
1737 struct batadv_tt_query_packet *tt_request) 2400 * node's translation table
2401 * @bat_priv: the bat priv with all the soft interface information
2402 * @tt_data: tt data containing the tt request information
2403 * @req_src: mac address of tt request sender
2404 * @req_dst: mac address of tt request recipient
2405 *
2406 * Returns true if tt request reply was sent, false otherwise.
2407 */
2408static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2409 struct batadv_tvlv_tt_data *tt_data,
2410 uint8_t *req_src, uint8_t *req_dst)
1738{ 2411{
1739 struct batadv_orig_node *req_dst_orig_node; 2412 struct batadv_orig_node *req_dst_orig_node;
1740 struct batadv_orig_node *res_dst_orig_node = NULL; 2413 struct batadv_orig_node *res_dst_orig_node = NULL;
1741 uint8_t orig_ttvn, req_ttvn, ttvn; 2414 struct batadv_tvlv_tt_change *tt_change;
1742 int res, ret = false; 2415 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1743 unsigned char *tt_buff; 2416 struct batadv_tvlv_tt_vlan_data *tt_vlan;
1744 bool full_table; 2417 bool ret = false, full_table;
1745 uint16_t tt_len, tt_tot; 2418 uint8_t orig_ttvn, req_ttvn;
1746 struct sk_buff *skb = NULL; 2419 uint16_t tvlv_len;
1747 struct batadv_tt_query_packet *tt_response; 2420 int32_t tt_len;
1748 uint8_t *packet_pos;
1749 size_t len;
1750 2421
1751 batadv_dbg(BATADV_DBG_TT, bat_priv, 2422 batadv_dbg(BATADV_DBG_TT, bat_priv,
1752 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n", 2423 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1753 tt_request->src, tt_request->ttvn, tt_request->dst, 2424 req_src, tt_data->ttvn, req_dst,
1754 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 2425 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1755 2426
1756 /* Let's get the orig node of the REAL destination */ 2427 /* Let's get the orig node of the REAL destination */
1757 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst); 2428 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
1758 if (!req_dst_orig_node) 2429 if (!req_dst_orig_node)
1759 goto out; 2430 goto out;
1760 2431
1761 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 2432 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
1762 if (!res_dst_orig_node) 2433 if (!res_dst_orig_node)
1763 goto out; 2434 goto out;
1764 2435
1765 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 2436 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1766 req_ttvn = tt_request->ttvn; 2437 req_ttvn = tt_data->ttvn;
1767 2438
1768 /* I don't have the requested data */ 2439 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2440 /* this node doesn't have the requested data */
1769 if (orig_ttvn != req_ttvn || 2441 if (orig_ttvn != req_ttvn ||
1770 tt_request->tt_data != htons(req_dst_orig_node->tt_crc)) 2442 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2443 ntohs(tt_data->num_vlan)))
1771 goto out; 2444 goto out;
1772 2445
1773 /* If the full table has been explicitly requested */ 2446 /* If the full table has been explicitly requested */
1774 if (tt_request->flags & BATADV_TT_FULL_TABLE || 2447 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
1775 !req_dst_orig_node->tt_buff) 2448 !req_dst_orig_node->tt_buff)
1776 full_table = true; 2449 full_table = true;
1777 else 2450 else
1778 full_table = false; 2451 full_table = false;
1779 2452
1780 /* In this version, fragmentation is not implemented, then 2453 /* 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 2454 * TT entries fit a single packet as possible only
1782 */ 2455 */
1783 if (!full_table) { 2456 if (!full_table) {
1784 spin_lock_bh(&req_dst_orig_node->tt_buff_lock); 2457 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1785 tt_len = req_dst_orig_node->tt_buff_len; 2458 tt_len = req_dst_orig_node->tt_buff_len;
1786 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1787 2459
1788 len = sizeof(*tt_response) + tt_len; 2460 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
1789 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN); 2461 &tvlv_tt_data,
1790 if (!skb) 2462 &tt_change,
2463 &tt_len);
2464 if (!tt_len)
1791 goto unlock; 2465 goto unlock;
1792 2466
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 */ 2467 /* Copy the last orig_node's OGM buffer */
1802 memcpy(tt_buff, req_dst_orig_node->tt_buff, 2468 memcpy(tt_change, req_dst_orig_node->tt_buff,
1803 req_dst_orig_node->tt_buff_len); 2469 req_dst_orig_node->tt_buff_len);
1804
1805 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock); 2470 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1806 } else { 2471 } else {
1807 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size); 2472 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
1808 tt_len *= sizeof(struct batadv_tt_change); 2473 * in the initial part
1809 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn); 2474 */
1810 2475 tt_len = -1;
1811 skb = batadv_tt_response_fill_table(tt_len, ttvn, 2476 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
1812 bat_priv->tt.global_hash, 2477 &tvlv_tt_data,
1813 bat_priv, 2478 &tt_change,
1814 batadv_tt_global_valid, 2479 &tt_len);
1815 req_dst_orig_node); 2480 if (!tt_len)
1816 if (!skb)
1817 goto out; 2481 goto out;
1818 2482
1819 tt_response = (struct batadv_tt_query_packet *)skb->data; 2483 /* fill the rest of the tvlv with the real TT entries */
2484 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2485 tt_change, tt_len,
2486 batadv_tt_global_valid,
2487 req_dst_orig_node);
1820 } 2488 }
1821 2489
1822 tt_response->header.packet_type = BATADV_TT_QUERY; 2490 /* Don't send the response, if larger than fragmented packet. */
1823 tt_response->header.version = BATADV_COMPAT_VERSION; 2491 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
1824 tt_response->header.ttl = BATADV_TTL; 2492 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
1825 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN); 2493 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
1826 memcpy(tt_response->dst, tt_request->src, ETH_ALEN); 2494 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
1827 tt_response->flags = BATADV_TT_RESPONSE; 2495 res_dst_orig_node->orig);
2496 goto out;
2497 }
2498
2499 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2500 tvlv_tt_data->ttvn = req_ttvn;
1828 2501
1829 if (full_table) 2502 if (full_table)
1830 tt_response->flags |= BATADV_TT_FULL_TABLE; 2503 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1831 2504
1832 batadv_dbg(BATADV_DBG_TT, bat_priv, 2505 batadv_dbg(BATADV_DBG_TT, bat_priv,
1833 "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n", 2506 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
1834 res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn); 2507 res_dst_orig_node->orig, req_dst_orig_node->orig,
2508 full_table ? 'F' : '.', req_ttvn);
1835 2509
1836 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 2510 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1837 2511
1838 res = batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL); 2512 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
1839 if (res != NET_XMIT_DROP) 2513 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
1840 ret = true; 2514 tvlv_len);
1841 2515
2516 ret = true;
1842 goto out; 2517 goto out;
1843 2518
1844unlock: 2519unlock:
@@ -1849,37 +2524,43 @@ out:
1849 batadv_orig_node_free_ref(res_dst_orig_node); 2524 batadv_orig_node_free_ref(res_dst_orig_node);
1850 if (req_dst_orig_node) 2525 if (req_dst_orig_node)
1851 batadv_orig_node_free_ref(req_dst_orig_node); 2526 batadv_orig_node_free_ref(req_dst_orig_node);
1852 if (!ret) 2527 kfree(tvlv_tt_data);
1853 kfree_skb(skb);
1854 return ret; 2528 return ret;
1855} 2529}
1856 2530
1857static bool 2531/**
1858batadv_send_my_tt_response(struct batadv_priv *bat_priv, 2532 * batadv_send_my_tt_response - send reply to tt request concerning this node's
1859 struct batadv_tt_query_packet *tt_request) 2533 * translation table
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @tt_data: tt data containing the tt request information
2536 * @req_src: mac address of tt request sender
2537 *
2538 * Returns true if tt request reply was sent, false otherwise.
2539 */
2540static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2541 struct batadv_tvlv_tt_data *tt_data,
2542 uint8_t *req_src)
1860{ 2543{
1861 struct batadv_orig_node *orig_node; 2544 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
1862 struct batadv_hard_iface *primary_if = NULL; 2545 struct batadv_hard_iface *primary_if = NULL;
1863 uint8_t my_ttvn, req_ttvn, ttvn; 2546 struct batadv_tvlv_tt_change *tt_change;
1864 int ret = false; 2547 struct batadv_orig_node *orig_node;
1865 unsigned char *tt_buff; 2548 uint8_t my_ttvn, req_ttvn;
2549 uint16_t tvlv_len;
1866 bool full_table; 2550 bool full_table;
1867 uint16_t tt_len, tt_tot; 2551 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 2552
1873 batadv_dbg(BATADV_DBG_TT, bat_priv, 2553 batadv_dbg(BATADV_DBG_TT, bat_priv,
1874 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n", 2554 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1875 tt_request->src, tt_request->ttvn, 2555 req_src, tt_data->ttvn,
1876 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.')); 2556 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1877 2557
2558 spin_lock_bh(&bat_priv->tt.commit_lock);
1878 2559
1879 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 2560 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1880 req_ttvn = tt_request->ttvn; 2561 req_ttvn = tt_data->ttvn;
1881 2562
1882 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src); 2563 orig_node = batadv_orig_hash_find(bat_priv, req_src);
1883 if (!orig_node) 2564 if (!orig_node)
1884 goto out; 2565 goto out;
1885 2566
@@ -1890,103 +2571,104 @@ batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1890 /* If the full table has been explicitly requested or the gap 2571 /* If the full table has been explicitly requested or the gap
1891 * is too big send the whole local translation table 2572 * is too big send the whole local translation table
1892 */ 2573 */
1893 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn || 2574 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1894 !bat_priv->tt.last_changeset) 2575 !bat_priv->tt.last_changeset)
1895 full_table = true; 2576 full_table = true;
1896 else 2577 else
1897 full_table = false; 2578 full_table = false;
1898 2579
1899 /* In this version, fragmentation is not implemented, then 2580 /* 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 2581 * TT entries fit a single packet as possible only
1901 */ 2582 */
1902 if (!full_table) { 2583 if (!full_table) {
1903 spin_lock_bh(&bat_priv->tt.last_changeset_lock); 2584 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 2585
1907 len = sizeof(*tt_response) + tt_len; 2586 tt_len = bat_priv->tt.last_changeset_len;
1908 skb = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN); 2587 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
1909 if (!skb) 2588 &tvlv_tt_data,
2589 &tt_change,
2590 &tt_len);
2591 if (!tt_len)
1910 goto unlock; 2592 goto unlock;
1911 2593
1912 skb->priority = TC_PRIO_CONTROL; 2594 /* Copy the last orig_node's OGM buffer */
1913 skb_reserve(skb, ETH_HLEN); 2595 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); 2596 bat_priv->tt.last_changeset_len);
1922 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 2597 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1923 } else { 2598 } else {
1924 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num); 2599 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1925 tt_len *= sizeof(struct batadv_tt_change); 2600
1926 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn); 2601 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
1927 2602 * in the initial part
1928 skb = batadv_tt_response_fill_table(tt_len, ttvn, 2603 */
1929 bat_priv->tt.local_hash, 2604 tt_len = -1;
1930 bat_priv, 2605 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
1931 batadv_tt_local_valid_entry, 2606 &tvlv_tt_data,
1932 NULL); 2607 &tt_change,
1933 if (!skb) 2608 &tt_len);
2609 if (!tt_len)
1934 goto out; 2610 goto out;
1935 2611
1936 tt_response = (struct batadv_tt_query_packet *)skb->data; 2612 /* fill the rest of the tvlv with the real TT entries */
2613 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2614 tt_change, tt_len,
2615 batadv_tt_local_valid, NULL);
1937 } 2616 }
1938 2617
1939 tt_response->header.packet_type = BATADV_TT_QUERY; 2618 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
1940 tt_response->header.version = BATADV_COMPAT_VERSION; 2619 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 2620
1946 if (full_table) 2621 if (full_table)
1947 tt_response->flags |= BATADV_TT_FULL_TABLE; 2622 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
1948 2623
1949 batadv_dbg(BATADV_DBG_TT, bat_priv, 2624 batadv_dbg(BATADV_DBG_TT, bat_priv,
1950 "Sending TT_RESPONSE to %pM [%c]\n", 2625 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
1951 orig_node->orig, 2626 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
1952 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1953 2627
1954 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX); 2628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1955 2629
1956 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 2630 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
1957 ret = true; 2631 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2632 tvlv_len);
2633
1958 goto out; 2634 goto out;
1959 2635
1960unlock: 2636unlock:
1961 spin_unlock_bh(&bat_priv->tt.last_changeset_lock); 2637 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1962out: 2638out:
2639 spin_unlock_bh(&bat_priv->tt.commit_lock);
1963 if (orig_node) 2640 if (orig_node)
1964 batadv_orig_node_free_ref(orig_node); 2641 batadv_orig_node_free_ref(orig_node);
1965 if (primary_if) 2642 if (primary_if)
1966 batadv_hardif_free_ref(primary_if); 2643 batadv_hardif_free_ref(primary_if);
1967 if (!ret) 2644 kfree(tvlv_tt_data);
1968 kfree_skb(skb); 2645 /* 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; 2646 return true;
1971} 2647}
1972 2648
1973bool batadv_send_tt_response(struct batadv_priv *bat_priv, 2649/**
1974 struct batadv_tt_query_packet *tt_request) 2650 * batadv_send_tt_response - send reply to tt request
2651 * @bat_priv: the bat priv with all the soft interface information
2652 * @tt_data: tt data containing the tt request information
2653 * @req_src: mac address of tt request sender
2654 * @req_dst: mac address of tt request recipient
2655 *
2656 * Returns true if tt request reply was sent, false otherwise.
2657 */
2658static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2659 struct batadv_tvlv_tt_data *tt_data,
2660 uint8_t *req_src, uint8_t *req_dst)
1975{ 2661{
1976 if (batadv_is_my_mac(bat_priv, tt_request->dst)) { 2662 if (batadv_is_my_mac(bat_priv, req_dst))
1977 /* don't answer backbone gws! */ 2663 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)) 2664 else
1979 return true; 2665 return batadv_send_other_tt_response(bat_priv, tt_data,
1980 2666 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} 2667}
1986 2668
1987static void _batadv_tt_update_changes(struct batadv_priv *bat_priv, 2669static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1988 struct batadv_orig_node *orig_node, 2670 struct batadv_orig_node *orig_node,
1989 struct batadv_tt_change *tt_change, 2671 struct batadv_tvlv_tt_change *tt_change,
1990 uint16_t tt_num_changes, uint8_t ttvn) 2672 uint16_t tt_num_changes, uint8_t ttvn)
1991{ 2673{
1992 int i; 2674 int i;
@@ -1997,11 +2679,13 @@ static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1997 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM; 2679 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1998 batadv_tt_global_del(bat_priv, orig_node, 2680 batadv_tt_global_del(bat_priv, orig_node,
1999 (tt_change + i)->addr, 2681 (tt_change + i)->addr,
2682 ntohs((tt_change + i)->vid),
2000 "tt removed by changes", 2683 "tt removed by changes",
2001 roams); 2684 roams);
2002 } else { 2685 } else {
2003 if (!batadv_tt_global_add(bat_priv, orig_node, 2686 if (!batadv_tt_global_add(bat_priv, orig_node,
2004 (tt_change + i)->addr, 2687 (tt_change + i)->addr,
2688 ntohs((tt_change + i)->vid),
2005 (tt_change + i)->flags, ttvn)) 2689 (tt_change + i)->flags, ttvn))
2006 /* In case of problem while storing a 2690 /* In case of problem while storing a
2007 * global_entry, we stop the updating 2691 * global_entry, we stop the updating
@@ -2016,21 +2700,22 @@ static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2016} 2700}
2017 2701
2018static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv, 2702static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2019 struct batadv_tt_query_packet *tt_response) 2703 struct batadv_tvlv_tt_change *tt_change,
2704 uint8_t ttvn, uint8_t *resp_src,
2705 uint16_t num_entries)
2020{ 2706{
2021 struct batadv_orig_node *orig_node; 2707 struct batadv_orig_node *orig_node;
2022 2708
2023 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 2709 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2024 if (!orig_node) 2710 if (!orig_node)
2025 goto out; 2711 goto out;
2026 2712
2027 /* Purge the old table first.. */ 2713 /* Purge the old table first.. */
2028 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table"); 2714 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2715 "Received full table");
2029 2716
2030 _batadv_tt_update_changes(bat_priv, orig_node, 2717 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2031 (struct batadv_tt_change *)(tt_response + 1), 2718 ttvn);
2032 ntohs(tt_response->tt_data),
2033 tt_response->ttvn);
2034 2719
2035 spin_lock_bh(&orig_node->tt_buff_lock); 2720 spin_lock_bh(&orig_node->tt_buff_lock);
2036 kfree(orig_node->tt_buff); 2721 kfree(orig_node->tt_buff);
@@ -2038,7 +2723,7 @@ static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2038 orig_node->tt_buff = NULL; 2723 orig_node->tt_buff = NULL;
2039 spin_unlock_bh(&orig_node->tt_buff_lock); 2724 spin_unlock_bh(&orig_node->tt_buff_lock);
2040 2725
2041 atomic_set(&orig_node->last_ttvn, tt_response->ttvn); 2726 atomic_set(&orig_node->last_ttvn, ttvn);
2042 2727
2043out: 2728out:
2044 if (orig_node) 2729 if (orig_node)
@@ -2048,22 +2733,31 @@ out:
2048static void batadv_tt_update_changes(struct batadv_priv *bat_priv, 2733static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2049 struct batadv_orig_node *orig_node, 2734 struct batadv_orig_node *orig_node,
2050 uint16_t tt_num_changes, uint8_t ttvn, 2735 uint16_t tt_num_changes, uint8_t ttvn,
2051 struct batadv_tt_change *tt_change) 2736 struct batadv_tvlv_tt_change *tt_change)
2052{ 2737{
2053 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, 2738 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2054 tt_num_changes, ttvn); 2739 tt_num_changes, ttvn);
2055 2740
2056 batadv_tt_save_orig_buffer(bat_priv, orig_node, 2741 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2057 (unsigned char *)tt_change, tt_num_changes); 2742 batadv_tt_len(tt_num_changes));
2058 atomic_set(&orig_node->last_ttvn, ttvn); 2743 atomic_set(&orig_node->last_ttvn, ttvn);
2059} 2744}
2060 2745
2061bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr) 2746/**
2747 * batadv_is_my_client - check if a client is served by the local node
2748 * @bat_priv: the bat priv with all the soft interface information
2749 * @addr: the mac adress of the client to check
2750 * @vid: VLAN identifier
2751 *
2752 * Returns true if the client is served by this node, false otherwise.
2753 */
2754bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2755 unsigned short vid)
2062{ 2756{
2063 struct batadv_tt_local_entry *tt_local_entry; 2757 struct batadv_tt_local_entry *tt_local_entry;
2064 bool ret = false; 2758 bool ret = false;
2065 2759
2066 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 2760 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2067 if (!tt_local_entry) 2761 if (!tt_local_entry)
2068 goto out; 2762 goto out;
2069 /* Check if the client has been logically deleted (but is kept for 2763 /* Check if the client has been logically deleted (but is kept for
@@ -2079,72 +2773,68 @@ out:
2079 return ret; 2773 return ret;
2080} 2774}
2081 2775
2082void batadv_handle_tt_response(struct batadv_priv *bat_priv, 2776/**
2083 struct batadv_tt_query_packet *tt_response) 2777 * batadv_handle_tt_response - process incoming tt reply
2778 * @bat_priv: the bat priv with all the soft interface information
2779 * @tt_data: tt data containing the tt request information
2780 * @resp_src: mac address of tt reply sender
2781 * @num_entries: number of tt change entries appended to the tt data
2782 */
2783static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2784 struct batadv_tvlv_tt_data *tt_data,
2785 uint8_t *resp_src, uint16_t num_entries)
2084{ 2786{
2085 struct batadv_tt_req_node *node, *safe; 2787 struct batadv_tt_req_node *node, *safe;
2086 struct batadv_orig_node *orig_node = NULL; 2788 struct batadv_orig_node *orig_node = NULL;
2087 struct batadv_tt_change *tt_change; 2789 struct batadv_tvlv_tt_change *tt_change;
2790 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2791 uint16_t change_offset;
2088 2792
2089 batadv_dbg(BATADV_DBG_TT, bat_priv, 2793 batadv_dbg(BATADV_DBG_TT, bat_priv,
2090 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n", 2794 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2091 tt_response->src, tt_response->ttvn, 2795 resp_src, tt_data->ttvn, num_entries,
2092 ntohs(tt_response->tt_data), 2796 (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 2797
2099 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src); 2798 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2100 if (!orig_node) 2799 if (!orig_node)
2101 goto out; 2800 goto out;
2102 2801
2103 if (tt_response->flags & BATADV_TT_FULL_TABLE) { 2802 spin_lock_bh(&orig_node->tt_lock);
2104 batadv_tt_fill_gtable(bat_priv, tt_response); 2803
2804 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2805 change_offset *= ntohs(tt_data->num_vlan);
2806 change_offset += sizeof(*tt_data);
2807 tvlv_ptr += change_offset;
2808
2809 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2810 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2811 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2812 resp_src, num_entries);
2105 } else { 2813 } else {
2106 tt_change = (struct batadv_tt_change *)(tt_response + 1); 2814 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2107 batadv_tt_update_changes(bat_priv, orig_node, 2815 tt_data->ttvn, tt_change);
2108 ntohs(tt_response->tt_data),
2109 tt_response->ttvn, tt_change);
2110 } 2816 }
2111 2817
2818 /* Recalculate the CRC for this orig_node and store it */
2819 batadv_tt_global_update_crc(bat_priv, orig_node);
2820
2821 spin_unlock_bh(&orig_node->tt_lock);
2822
2112 /* Delete the tt_req_node from pending tt_requests list */ 2823 /* Delete the tt_req_node from pending tt_requests list */
2113 spin_lock_bh(&bat_priv->tt.req_list_lock); 2824 spin_lock_bh(&bat_priv->tt.req_list_lock);
2114 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) { 2825 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2115 if (!batadv_compare_eth(node->addr, tt_response->src)) 2826 if (!batadv_compare_eth(node->addr, resp_src))
2116 continue; 2827 continue;
2117 list_del(&node->list); 2828 list_del(&node->list);
2118 kfree(node); 2829 kfree(node);
2119 } 2830 }
2120 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2121 2831
2122 /* Recalculate the CRC for this orig_node and store it */ 2832 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2123 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2124out: 2833out:
2125 if (orig_node) 2834 if (orig_node)
2126 batadv_orig_node_free_ref(orig_node); 2835 batadv_orig_node_free_ref(orig_node);
2127} 2836}
2128 2837
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) 2838static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2149{ 2839{
2150 struct batadv_tt_roam_node *node, *safe; 2840 struct batadv_tt_roam_node *node, *safe;
@@ -2225,14 +2915,28 @@ unlock:
2225 return ret; 2915 return ret;
2226} 2916}
2227 2917
2918/**
2919 * batadv_send_roam_adv - send a roaming advertisement message
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @client: mac address of the roaming client
2922 * @vid: VLAN identifier
2923 * @orig_node: message destination
2924 *
2925 * Send a ROAMING_ADV message to the node which was previously serving this
2926 * client. This is done to inform the node that from now on all traffic destined
2927 * for this particular roamed client has to be forwarded to the sender of the
2928 * roaming message.
2929 */
2228static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client, 2930static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2931 unsigned short vid,
2229 struct batadv_orig_node *orig_node) 2932 struct batadv_orig_node *orig_node)
2230{ 2933{
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; 2934 struct batadv_hard_iface *primary_if;
2235 size_t len = sizeof(*roam_adv_packet); 2935 struct batadv_tvlv_roam_adv tvlv_roam;
2936
2937 primary_if = batadv_primary_if_get_selected(bat_priv);
2938 if (!primary_if)
2939 goto out;
2236 2940
2237 /* before going on we have to check whether the client has 2941 /* before going on we have to check whether the client has
2238 * already roamed to us too many times 2942 * already roamed to us too many times
@@ -2240,40 +2944,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)) 2944 if (!batadv_tt_check_roam_count(bat_priv, client))
2241 goto out; 2945 goto out;
2242 2946
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, 2947 batadv_dbg(BATADV_DBG_TT, bat_priv,
2265 "Sending ROAMING_ADV to %pM (client %pM)\n", 2948 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2266 orig_node->orig, client); 2949 orig_node->orig, client, BATADV_PRINT_VID(vid));
2267 2950
2268 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX); 2951 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2269 2952
2270 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP) 2953 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2271 ret = 0; 2954 tvlv_roam.vid = htons(vid);
2955
2956 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2957 orig_node->orig, BATADV_TVLV_ROAM, 1,
2958 &tvlv_roam, sizeof(tvlv_roam));
2272 2959
2273out: 2960out:
2274 if (ret && skb) 2961 if (primary_if)
2275 kfree_skb(skb); 2962 batadv_hardif_free_ref(primary_if);
2276 return;
2277} 2963}
2278 2964
2279static void batadv_tt_purge(struct work_struct *work) 2965static void batadv_tt_purge(struct work_struct *work)
@@ -2286,7 +2972,7 @@ static void batadv_tt_purge(struct work_struct *work)
2286 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work); 2972 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2287 bat_priv = container_of(priv_tt, struct batadv_priv, tt); 2973 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2288 2974
2289 batadv_tt_local_purge(bat_priv); 2975 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
2290 batadv_tt_global_purge(bat_priv); 2976 batadv_tt_global_purge(bat_priv);
2291 batadv_tt_req_purge(bat_priv); 2977 batadv_tt_req_purge(bat_priv);
2292 batadv_tt_roam_purge(bat_priv); 2978 batadv_tt_roam_purge(bat_priv);
@@ -2297,6 +2983,9 @@ static void batadv_tt_purge(struct work_struct *work)
2297 2983
2298void batadv_tt_free(struct batadv_priv *bat_priv) 2984void batadv_tt_free(struct batadv_priv *bat_priv)
2299{ 2985{
2986 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2987 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2988
2300 cancel_delayed_work_sync(&bat_priv->tt.work); 2989 cancel_delayed_work_sync(&bat_priv->tt.work);
2301 2990
2302 batadv_tt_local_table_free(bat_priv); 2991 batadv_tt_local_table_free(bat_priv);
@@ -2308,19 +2997,25 @@ void batadv_tt_free(struct batadv_priv *bat_priv)
2308 kfree(bat_priv->tt.last_changeset); 2997 kfree(bat_priv->tt.last_changeset);
2309} 2998}
2310 2999
2311/* This function will enable or disable the specified flags for all the entries 3000/**
2312 * in the given hash table and returns the number of modified entries 3001 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3002 * table and possibly count them in the TT size
3003 * @bat_priv: the bat priv with all the soft interface information
3004 * @flags: the flag to switch
3005 * @enable: whether to set or unset the flag
3006 * @count: whether to increase the TT size by the number of changed entries
2313 */ 3007 */
2314static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash, 3008static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
2315 uint16_t flags, bool enable) 3009 uint16_t flags, bool enable, bool count)
2316{ 3010{
2317 uint32_t i; 3011 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3012 struct batadv_tt_common_entry *tt_common_entry;
2318 uint16_t changed_num = 0; 3013 uint16_t changed_num = 0;
2319 struct hlist_head *head; 3014 struct hlist_head *head;
2320 struct batadv_tt_common_entry *tt_common_entry; 3015 uint32_t i;
2321 3016
2322 if (!hash) 3017 if (!hash)
2323 goto out; 3018 return;
2324 3019
2325 for (i = 0; i < hash->size; i++) { 3020 for (i = 0; i < hash->size; i++) {
2326 head = &hash->table[i]; 3021 head = &hash->table[i];
@@ -2338,11 +3033,15 @@ static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2338 tt_common_entry->flags &= ~flags; 3033 tt_common_entry->flags &= ~flags;
2339 } 3034 }
2340 changed_num++; 3035 changed_num++;
3036
3037 if (!count)
3038 continue;
3039
3040 batadv_tt_local_size_inc(bat_priv,
3041 tt_common_entry->vid);
2341 } 3042 }
2342 rcu_read_unlock(); 3043 rcu_read_unlock();
2343 } 3044 }
2344out:
2345 return changed_num;
2346} 3045}
2347 3046
2348/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */ 3047/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
@@ -2370,10 +3069,11 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2370 continue; 3069 continue;
2371 3070
2372 batadv_dbg(BATADV_DBG_TT, bat_priv, 3071 batadv_dbg(BATADV_DBG_TT, bat_priv,
2373 "Deleting local tt entry (%pM): pending\n", 3072 "Deleting local tt entry (%pM, vid: %d): pending\n",
2374 tt_common->addr); 3073 tt_common->addr,
3074 BATADV_PRINT_VID(tt_common->vid));
2375 3075
2376 atomic_dec(&bat_priv->tt.local_entry_num); 3076 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
2377 hlist_del_rcu(&tt_common->hash_entry); 3077 hlist_del_rcu(&tt_common->hash_entry);
2378 tt_local = container_of(tt_common, 3078 tt_local = container_of(tt_common,
2379 struct batadv_tt_local_entry, 3079 struct batadv_tt_local_entry,
@@ -2384,22 +3084,25 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2384 } 3084 }
2385} 3085}
2386 3086
2387static int batadv_tt_commit_changes(struct batadv_priv *bat_priv, 3087/**
2388 unsigned char **packet_buff, 3088 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
2389 int *packet_buff_len, int packet_min_len) 3089 * which have been queued in the time since the last commit
3090 * @bat_priv: the bat priv with all the soft interface information
3091 *
3092 * Caller must hold tt->commit_lock.
3093 */
3094static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
2390{ 3095{
2391 uint16_t changed_num = 0; 3096 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
2392 3097 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
2393 if (atomic_read(&bat_priv->tt.local_changes) < 1) 3098 batadv_tt_tvlv_container_update(bat_priv);
2394 return -ENOENT; 3099 return;
3100 }
2395 3101
2396 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash, 3102 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
2397 BATADV_TT_CLIENT_NEW, false);
2398 3103
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); 3104 batadv_tt_local_purge_pending_clients(bat_priv);
2402 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv); 3105 batadv_tt_local_update_crc(bat_priv);
2403 3106
2404 /* Increment the TTVN only once per OGM interval */ 3107 /* Increment the TTVN only once per OGM interval */
2405 atomic_inc(&bat_priv->tt.vn); 3108 atomic_inc(&bat_priv->tt.vn);
@@ -2409,49 +3112,38 @@ static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2409 3112
2410 /* reset the sending counter */ 3113 /* reset the sending counter */
2411 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX); 3114 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2412 3115 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} 3116}
2416 3117
2417/* when calling this function (hard_iface == primary_if) has to be true */ 3118/**
2418int batadv_tt_append_diff(struct batadv_priv *bat_priv, 3119 * batadv_tt_local_commit_changes - commit all pending local tt changes which
2419 unsigned char **packet_buff, int *packet_buff_len, 3120 * have been queued in the time since the last commit
2420 int packet_min_len) 3121 * @bat_priv: the bat priv with all the soft interface information
3122 */
3123void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
2421{ 3124{
2422 int tt_num_changes; 3125 spin_lock_bh(&bat_priv->tt.commit_lock);
2423 3126 batadv_tt_local_commit_changes_nolock(bat_priv);
2424 /* if at least one change happened */ 3127 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} 3128}
2439 3129
2440bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src, 3130bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2441 uint8_t *dst) 3131 uint8_t *dst, unsigned short vid)
2442{ 3132{
2443 struct batadv_tt_local_entry *tt_local_entry = NULL; 3133 struct batadv_tt_local_entry *tt_local_entry = NULL;
2444 struct batadv_tt_global_entry *tt_global_entry = NULL; 3134 struct batadv_tt_global_entry *tt_global_entry = NULL;
3135 struct batadv_softif_vlan *vlan;
2445 bool ret = false; 3136 bool ret = false;
2446 3137
2447 if (!atomic_read(&bat_priv->ap_isolation)) 3138 vlan = batadv_softif_vlan_get(bat_priv, vid);
3139 if (!vlan || !atomic_read(&vlan->ap_isolation))
2448 goto out; 3140 goto out;
2449 3141
2450 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst); 3142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
2451 if (!tt_local_entry) 3143 if (!tt_local_entry)
2452 goto out; 3144 goto out;
2453 3145
2454 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src); 3146 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
2455 if (!tt_global_entry) 3147 if (!tt_global_entry)
2456 goto out; 3148 goto out;
2457 3149
@@ -2461,6 +3153,8 @@ bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2461 ret = true; 3153 ret = true;
2462 3154
2463out: 3155out:
3156 if (vlan)
3157 batadv_softif_vlan_free_ref(vlan);
2464 if (tt_global_entry) 3158 if (tt_global_entry)
2465 batadv_tt_global_entry_free_ref(tt_global_entry); 3159 batadv_tt_global_entry_free_ref(tt_global_entry);
2466 if (tt_local_entry) 3160 if (tt_local_entry)
@@ -2468,19 +3162,29 @@ out:
2468 return ret; 3162 return ret;
2469} 3163}
2470 3164
2471void batadv_tt_update_orig(struct batadv_priv *bat_priv, 3165/**
2472 struct batadv_orig_node *orig_node, 3166 * batadv_tt_update_orig - update global translation table with new tt
2473 const unsigned char *tt_buff, uint8_t tt_num_changes, 3167 * information received via ogms
2474 uint8_t ttvn, uint16_t tt_crc) 3168 * @bat_priv: the bat priv with all the soft interface information
3169 * @orig: the orig_node of the ogm
3170 * @tt_vlan: pointer to the first tvlv VLAN entry
3171 * @tt_num_vlan: number of tvlv VLAN entries
3172 * @tt_change: pointer to the first entry in the TT buffer
3173 * @tt_num_changes: number of tt changes inside the tt buffer
3174 * @ttvn: translation table version number of this changeset
3175 * @tt_crc: crc32 checksum of orig node's translation table
3176 */
3177static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3178 struct batadv_orig_node *orig_node,
3179 const void *tt_buff, uint16_t tt_num_vlan,
3180 struct batadv_tvlv_tt_change *tt_change,
3181 uint16_t tt_num_changes, uint8_t ttvn)
2475{ 3182{
2476 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); 3183 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
3184 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2477 bool full_table = true; 3185 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 3186
3187 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 3188 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2485 * increased by one -> we can apply the attached changes 3189 * increased by one -> we can apply the attached changes
2486 */ 3190 */
@@ -2496,7 +3200,9 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2496 goto request_table; 3200 goto request_table;
2497 } 3201 }
2498 3202
2499 tt_change = (struct batadv_tt_change *)tt_buff; 3203 spin_lock_bh(&orig_node->tt_lock);
3204
3205 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
2500 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes, 3206 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2501 ttvn, tt_change); 3207 ttvn, tt_change);
2502 3208
@@ -2504,7 +3210,9 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2504 * prefer to recompute it to spot any possible inconsistency 3210 * prefer to recompute it to spot any possible inconsistency
2505 * in the global table 3211 * in the global table
2506 */ 3212 */
2507 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node); 3213 batadv_tt_global_update_crc(bat_priv, orig_node);
3214
3215 spin_unlock_bh(&orig_node->tt_lock);
2508 3216
2509 /* The ttvn alone is not enough to guarantee consistency 3217 /* The ttvn alone is not enough to guarantee consistency
2510 * because a single value could represent different states 3218 * because a single value could represent different states
@@ -2515,37 +3223,46 @@ void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2515 * checking the CRC value is mandatory to detect the 3223 * checking the CRC value is mandatory to detect the
2516 * inconsistency 3224 * inconsistency
2517 */ 3225 */
2518 if (orig_node->tt_crc != tt_crc) 3226 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3227 tt_num_vlan))
2519 goto request_table; 3228 goto request_table;
2520 } else { 3229 } else {
2521 /* if we missed more than one change or our tables are not 3230 /* if we missed more than one change or our tables are not
2522 * in sync anymore -> request fresh tt data 3231 * in sync anymore -> request fresh tt data
2523 */ 3232 */
2524 if (!orig_node->tt_initialised || ttvn != orig_ttvn || 3233 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2525 orig_node->tt_crc != tt_crc) { 3234 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3235 tt_num_vlan)) {
2526request_table: 3236request_table:
2527 batadv_dbg(BATADV_DBG_TT, bat_priv, 3237 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", 3238 "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, 3239 orig_node->orig, ttvn, orig_ttvn,
2530 orig_node->tt_crc, tt_num_changes); 3240 tt_num_changes);
2531 batadv_send_tt_request(bat_priv, orig_node, ttvn, 3241 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2532 tt_crc, full_table); 3242 tt_vlan, tt_num_vlan,
3243 full_table);
2533 return; 3244 return;
2534 } 3245 }
2535 } 3246 }
2536} 3247}
2537 3248
2538/* returns true whether we know that the client has moved from its old 3249/**
2539 * originator to another one. This entry is kept is still kept for consistency 3250 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
2540 * purposes 3251 * @bat_priv: the bat priv with all the soft interface information
3252 * @addr: the mac address of the client to check
3253 * @vid: VLAN identifier
3254 *
3255 * Returns true if we know that the client has moved from its old originator
3256 * to another one. This entry is still kept for consistency purposes and will be
3257 * deleted later by a DEL or because of timeout
2541 */ 3258 */
2542bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv, 3259bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2543 uint8_t *addr) 3260 uint8_t *addr, unsigned short vid)
2544{ 3261{
2545 struct batadv_tt_global_entry *tt_global_entry; 3262 struct batadv_tt_global_entry *tt_global_entry;
2546 bool ret = false; 3263 bool ret = false;
2547 3264
2548 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr); 3265 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2549 if (!tt_global_entry) 3266 if (!tt_global_entry)
2550 goto out; 3267 goto out;
2551 3268
@@ -2558,19 +3275,20 @@ out:
2558/** 3275/**
2559 * batadv_tt_local_client_is_roaming - tells whether the client is roaming 3276 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2560 * @bat_priv: the bat priv with all the soft interface information 3277 * @bat_priv: the bat priv with all the soft interface information
2561 * @addr: the MAC address of the local client to query 3278 * @addr: the mac address of the local client to query
3279 * @vid: VLAN identifier
2562 * 3280 *
2563 * Returns true if the local client is known to be roaming (it is not served by 3281 * 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 3282 * 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 3283 * to keep the latter consistent with the node TTVN
2566 */ 3284 */
2567bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv, 3285bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2568 uint8_t *addr) 3286 uint8_t *addr, unsigned short vid)
2569{ 3287{
2570 struct batadv_tt_local_entry *tt_local_entry; 3288 struct batadv_tt_local_entry *tt_local_entry;
2571 bool ret = false; 3289 bool ret = false;
2572 3290
2573 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr); 3291 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2574 if (!tt_local_entry) 3292 if (!tt_local_entry)
2575 goto out; 3293 goto out;
2576 3294
@@ -2582,26 +3300,268 @@ out:
2582 3300
2583bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv, 3301bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2584 struct batadv_orig_node *orig_node, 3302 struct batadv_orig_node *orig_node,
2585 const unsigned char *addr) 3303 const unsigned char *addr,
3304 unsigned short vid)
2586{ 3305{
2587 bool ret = false; 3306 bool ret = false;
2588 3307
2589 /* if the originator is a backbone node (meaning it belongs to the same 3308 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, 3309 BATADV_TT_CLIENT_TEMP,
2598 atomic_read(&orig_node->last_ttvn))) 3310 atomic_read(&orig_node->last_ttvn)))
2599 goto out; 3311 goto out;
2600 3312
2601 batadv_dbg(BATADV_DBG_TT, bat_priv, 3313 batadv_dbg(BATADV_DBG_TT, bat_priv,
2602 "Added temporary global client (addr: %pM orig: %pM)\n", 3314 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
2603 addr, orig_node->orig); 3315 addr, BATADV_PRINT_VID(vid), orig_node->orig);
2604 ret = true; 3316 ret = true;
2605out: 3317out:
2606 return ret; 3318 return ret;
2607} 3319}
3320
3321/**
3322 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3323 * maximum packet size that can be transported through the mesh
3324 * @soft_iface: netdev struct of the mesh interface
3325 *
3326 * Remove entries older than 'timeout' and half timeout if more entries need
3327 * to be removed.
3328 */
3329void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3330{
3331 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3332 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3333 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3334 bool reduced = false;
3335
3336 spin_lock_bh(&bat_priv->tt.commit_lock);
3337
3338 while (true) {
3339 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3340 if (packet_size_max >= table_size)
3341 break;
3342
3343 batadv_tt_local_purge(bat_priv, timeout);
3344 batadv_tt_local_purge_pending_clients(bat_priv);
3345
3346 timeout /= 2;
3347 reduced = true;
3348 net_ratelimited_function(batadv_info, soft_iface,
3349 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3350 packet_size_max);
3351 }
3352
3353 /* commit these changes immediately, to avoid synchronization problem
3354 * with the TTVN
3355 */
3356 if (reduced)
3357 batadv_tt_local_commit_changes_nolock(bat_priv);
3358
3359 spin_unlock_bh(&bat_priv->tt.commit_lock);
3360}
3361
3362/**
3363 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3364 * @bat_priv: the bat priv with all the soft interface information
3365 * @orig: the orig_node of the ogm
3366 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3367 * @tvlv_value: tvlv buffer containing the gateway data
3368 * @tvlv_value_len: tvlv buffer length
3369 */
3370static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3371 struct batadv_orig_node *orig,
3372 uint8_t flags, void *tvlv_value,
3373 uint16_t tvlv_value_len)
3374{
3375 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3376 struct batadv_tvlv_tt_change *tt_change;
3377 struct batadv_tvlv_tt_data *tt_data;
3378 uint16_t num_entries, num_vlan;
3379
3380 if (tvlv_value_len < sizeof(*tt_data))
3381 return;
3382
3383 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3384 tvlv_value_len -= sizeof(*tt_data);
3385
3386 num_vlan = ntohs(tt_data->num_vlan);
3387
3388 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3389 return;
3390
3391 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3392 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3393 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3394
3395 num_entries = batadv_tt_entries(tvlv_value_len);
3396
3397 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3398 num_entries, tt_data->ttvn);
3399}
3400
3401/**
3402 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3403 * container
3404 * @bat_priv: the bat priv with all the soft interface information
3405 * @src: mac address of tt tvlv sender
3406 * @dst: mac address of tt tvlv recipient
3407 * @tvlv_value: tvlv buffer containing the tt data
3408 * @tvlv_value_len: tvlv buffer length
3409 *
3410 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3411 * otherwise.
3412 */
3413static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3414 uint8_t *src, uint8_t *dst,
3415 void *tvlv_value,
3416 uint16_t tvlv_value_len)
3417{
3418 struct batadv_tvlv_tt_data *tt_data;
3419 uint16_t tt_vlan_len, tt_num_entries;
3420 char tt_flag;
3421 bool ret;
3422
3423 if (tvlv_value_len < sizeof(*tt_data))
3424 return NET_RX_SUCCESS;
3425
3426 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3427 tvlv_value_len -= sizeof(*tt_data);
3428
3429 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3430 tt_vlan_len *= ntohs(tt_data->num_vlan);
3431
3432 if (tvlv_value_len < tt_vlan_len)
3433 return NET_RX_SUCCESS;
3434
3435 tvlv_value_len -= tt_vlan_len;
3436 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3437
3438 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3439 case BATADV_TT_REQUEST:
3440 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3441
3442 /* If this node cannot provide a TT response the tt_request is
3443 * forwarded
3444 */
3445 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3446 if (!ret) {
3447 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3448 tt_flag = 'F';
3449 else
3450 tt_flag = '.';
3451
3452 batadv_dbg(BATADV_DBG_TT, bat_priv,
3453 "Routing TT_REQUEST to %pM [%c]\n",
3454 dst, tt_flag);
3455 /* tvlv API will re-route the packet */
3456 return NET_RX_DROP;
3457 }
3458 break;
3459 case BATADV_TT_RESPONSE:
3460 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3461
3462 if (batadv_is_my_mac(bat_priv, dst)) {
3463 batadv_handle_tt_response(bat_priv, tt_data,
3464 src, tt_num_entries);
3465 return NET_RX_SUCCESS;
3466 }
3467
3468 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3469 tt_flag = 'F';
3470 else
3471 tt_flag = '.';
3472
3473 batadv_dbg(BATADV_DBG_TT, bat_priv,
3474 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3475
3476 /* tvlv API will re-route the packet */
3477 return NET_RX_DROP;
3478 }
3479
3480 return NET_RX_SUCCESS;
3481}
3482
3483/**
3484 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3485 * @bat_priv: the bat priv with all the soft interface information
3486 * @src: mac address of tt tvlv sender
3487 * @dst: mac address of tt tvlv recipient
3488 * @tvlv_value: tvlv buffer containing the tt data
3489 * @tvlv_value_len: tvlv buffer length
3490 *
3491 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3492 * otherwise.
3493 */
3494static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3495 uint8_t *src, uint8_t *dst,
3496 void *tvlv_value,
3497 uint16_t tvlv_value_len)
3498{
3499 struct batadv_tvlv_roam_adv *roaming_adv;
3500 struct batadv_orig_node *orig_node = NULL;
3501
3502 /* If this node is not the intended recipient of the
3503 * roaming advertisement the packet is forwarded
3504 * (the tvlv API will re-route the packet).
3505 */
3506 if (!batadv_is_my_mac(bat_priv, dst))
3507 return NET_RX_DROP;
3508
3509 if (tvlv_value_len < sizeof(*roaming_adv))
3510 goto out;
3511
3512 orig_node = batadv_orig_hash_find(bat_priv, src);
3513 if (!orig_node)
3514 goto out;
3515
3516 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3517 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3518
3519 batadv_dbg(BATADV_DBG_TT, bat_priv,
3520 "Received ROAMING_ADV from %pM (client %pM)\n",
3521 src, roaming_adv->client);
3522
3523 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3524 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3525 atomic_read(&orig_node->last_ttvn) + 1);
3526
3527out:
3528 if (orig_node)
3529 batadv_orig_node_free_ref(orig_node);
3530 return NET_RX_SUCCESS;
3531}
3532
3533/**
3534 * batadv_tt_init - initialise the translation table internals
3535 * @bat_priv: the bat priv with all the soft interface information
3536 *
3537 * Return 0 on success or negative error number in case of failure.
3538 */
3539int batadv_tt_init(struct batadv_priv *bat_priv)
3540{
3541 int ret;
3542
3543 /* synchronized flags must be remote */
3544 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3545
3546 ret = batadv_tt_local_init(bat_priv);
3547 if (ret < 0)
3548 return ret;
3549
3550 ret = batadv_tt_global_init(bat_priv);
3551 if (ret < 0)
3552 return ret;
3553
3554 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3555 batadv_tt_tvlv_unicast_handler_v1,
3556 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3557
3558 batadv_tvlv_handler_register(bat_priv, NULL,
3559 batadv_roam_tvlv_unicast_handler_v1,
3560 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3561
3562 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3563 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3564 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3565
3566 return 1;
3567}
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..f6a1671ea2ff 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,12 @@ 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) {
228 msg->msg_namelen = 0;
229 return 0; 229 return 0;
230 }
230 return err; 231 return err;
231 } 232 }
232 233
@@ -238,9 +239,16 @@ int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
238 239
239 skb_reset_transport_header(skb); 240 skb_reset_transport_header(skb);
240 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 241 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
241 if (err == 0) 242 if (err == 0) {
242 sock_recv_ts_and_drops(msg, sk, skb); 243 sock_recv_ts_and_drops(msg, sk, skb);
243 244
245 if (bt_sk(sk)->skb_msg_name)
246 bt_sk(sk)->skb_msg_name(skb, msg->msg_name,
247 &msg->msg_namelen);
248 else
249 msg->msg_namelen = 0;
250 }
251
244 skb_free_datagram(sk, skb); 252 skb_free_datagram(sk, skb);
245 253
246 return err ? : copied; 254 return err ? : copied;
@@ -490,6 +498,7 @@ int bt_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
490} 498}
491EXPORT_SYMBOL(bt_sock_ioctl); 499EXPORT_SYMBOL(bt_sock_ioctl);
492 500
501/* This function expects the sk lock to be held when called */
493int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo) 502int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
494{ 503{
495 DECLARE_WAITQUEUE(wait, current); 504 DECLARE_WAITQUEUE(wait, current);
@@ -525,6 +534,46 @@ int bt_sock_wait_state(struct sock *sk, int state, unsigned long timeo)
525} 534}
526EXPORT_SYMBOL(bt_sock_wait_state); 535EXPORT_SYMBOL(bt_sock_wait_state);
527 536
537/* This function expects the sk lock to be held when called */
538int bt_sock_wait_ready(struct sock *sk, unsigned long flags)
539{
540 DECLARE_WAITQUEUE(wait, current);
541 unsigned long timeo;
542 int err = 0;
543
544 BT_DBG("sk %p", sk);
545
546 timeo = sock_sndtimeo(sk, flags & O_NONBLOCK);
547
548 add_wait_queue(sk_sleep(sk), &wait);
549 set_current_state(TASK_INTERRUPTIBLE);
550 while (test_bit(BT_SK_SUSPEND, &bt_sk(sk)->flags)) {
551 if (!timeo) {
552 err = -EAGAIN;
553 break;
554 }
555
556 if (signal_pending(current)) {
557 err = sock_intr_errno(timeo);
558 break;
559 }
560
561 release_sock(sk);
562 timeo = schedule_timeout(timeo);
563 lock_sock(sk);
564 set_current_state(TASK_INTERRUPTIBLE);
565
566 err = sock_error(sk);
567 if (err)
568 break;
569 }
570 __set_current_state(TASK_RUNNING);
571 remove_wait_queue(sk_sleep(sk), &wait);
572
573 return err;
574}
575EXPORT_SYMBOL(bt_sock_wait_ready);
576
528#ifdef CONFIG_PROC_FS 577#ifdef CONFIG_PROC_FS
529struct bt_seq_state { 578struct bt_seq_state {
530 struct bt_sock_list *l; 579 struct bt_sock_list *l;
@@ -563,7 +612,7 @@ static int bt_seq_show(struct seq_file *seq, void *v)
563 struct bt_sock_list *l = s->l; 612 struct bt_sock_list *l = s->l;
564 613
565 if (v == SEQ_START_TOKEN) { 614 if (v == SEQ_START_TOKEN) {
566 seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Src Dst Parent"); 615 seq_puts(seq ,"sk RefCnt Rmem Wmem User Inode Parent");
567 616
568 if (l->custom_seq_show) { 617 if (l->custom_seq_show) {
569 seq_putc(seq, ' '); 618 seq_putc(seq, ' ');
@@ -576,15 +625,13 @@ static int bt_seq_show(struct seq_file *seq, void *v)
576 struct bt_sock *bt = bt_sk(sk); 625 struct bt_sock *bt = bt_sk(sk);
577 626
578 seq_printf(seq, 627 seq_printf(seq,
579 "%pK %-6d %-6u %-6u %-6u %-6lu %pMR %pMR %-6lu", 628 "%pK %-6d %-6u %-6u %-6u %-6lu %-6lu",
580 sk, 629 sk,
581 atomic_read(&sk->sk_refcnt), 630 atomic_read(&sk->sk_refcnt),
582 sk_rmem_alloc_get(sk), 631 sk_rmem_alloc_get(sk),
583 sk_wmem_alloc_get(sk), 632 sk_wmem_alloc_get(sk),
584 from_kuid(seq_user_ns(seq), sock_i_uid(sk)), 633 from_kuid(seq_user_ns(seq), sock_i_uid(sk)),
585 sock_i_ino(sk), 634 sock_i_ino(sk),
586 &bt->src,
587 &bt->dst,
588 bt->parent? sock_i_ino(bt->parent): 0LU); 635 bt->parent? sock_i_ino(bt->parent): 0LU);
589 636
590 if (l->custom_seq_show) { 637 if (l->custom_seq_show) {
@@ -662,12 +709,17 @@ static struct net_proto_family bt_sock_family_ops = {
662 .create = bt_sock_create, 709 .create = bt_sock_create,
663}; 710};
664 711
712struct dentry *bt_debugfs;
713EXPORT_SYMBOL_GPL(bt_debugfs);
714
665static int __init bt_init(void) 715static int __init bt_init(void)
666{ 716{
667 int err; 717 int err;
668 718
669 BT_INFO("Core ver %s", VERSION); 719 BT_INFO("Core ver %s", VERSION);
670 720
721 bt_debugfs = debugfs_create_dir("bluetooth", NULL);
722
671 err = bt_sysfs_init(); 723 err = bt_sysfs_init();
672 if (err < 0) 724 if (err < 0)
673 return err; 725 return err;
@@ -708,7 +760,6 @@ error:
708 760
709static void __exit bt_exit(void) 761static void __exit bt_exit(void)
710{ 762{
711
712 sco_exit(); 763 sco_exit();
713 764
714 l2cap_exit(); 765 l2cap_exit();
@@ -718,6 +769,8 @@ static void __exit bt_exit(void)
718 sock_unregister(PF_BLUETOOTH); 769 sock_unregister(PF_BLUETOOTH);
719 770
720 bt_sysfs_cleanup(); 771 bt_sysfs_cleanup();
772
773 debugfs_remove_recursive(bt_debugfs);
721} 774}
722 775
723subsys_initcall(bt_init); 776subsys_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..71f0be173080 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,
@@ -767,6 +871,7 @@ static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
767 case HCI_CHANNEL_RAW: 871 case HCI_CHANNEL_RAW:
768 hci_sock_cmsg(sk, msg, skb); 872 hci_sock_cmsg(sk, msg, skb);
769 break; 873 break;
874 case HCI_CHANNEL_USER:
770 case HCI_CHANNEL_CONTROL: 875 case HCI_CHANNEL_CONTROL:
771 case HCI_CHANNEL_MONITOR: 876 case HCI_CHANNEL_MONITOR:
772 sock_recv_timestamp(msg, sk, skb); 877 sock_recv_timestamp(msg, sk, skb);
@@ -801,6 +906,7 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
801 906
802 switch (hci_pi(sk)->channel) { 907 switch (hci_pi(sk)->channel) {
803 case HCI_CHANNEL_RAW: 908 case HCI_CHANNEL_RAW:
909 case HCI_CHANNEL_USER:
804 break; 910 break;
805 case HCI_CHANNEL_CONTROL: 911 case HCI_CHANNEL_CONTROL:
806 err = mgmt_control(sk, msg, len); 912 err = mgmt_control(sk, msg, len);
@@ -835,9 +941,9 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
835 941
836 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data); 942 bt_cb(skb)->pkt_type = *((unsigned char *) skb->data);
837 skb_pull(skb, 1); 943 skb_pull(skb, 1);
838 skb->dev = (void *) hdev;
839 944
840 if (bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) { 945 if (hci_pi(sk)->channel == HCI_CHANNEL_RAW &&
946 bt_cb(skb)->pkt_type == HCI_COMMAND_PKT) {
841 u16 opcode = get_unaligned_le16(skb->data); 947 u16 opcode = get_unaligned_le16(skb->data);
842 u16 ogf = hci_opcode_ogf(opcode); 948 u16 ogf = hci_opcode_ogf(opcode);
843 u16 ocf = hci_opcode_ocf(opcode); 949 u16 ocf = hci_opcode_ocf(opcode);
@@ -868,6 +974,14 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
868 goto drop; 974 goto drop;
869 } 975 }
870 976
977 if (hci_pi(sk)->channel == HCI_CHANNEL_USER &&
978 bt_cb(skb)->pkt_type != HCI_COMMAND_PKT &&
979 bt_cb(skb)->pkt_type != HCI_ACLDATA_PKT &&
980 bt_cb(skb)->pkt_type != HCI_SCODATA_PKT) {
981 err = -EINVAL;
982 goto drop;
983 }
984
871 skb_queue_tail(&hdev->raw_q, skb); 985 skb_queue_tail(&hdev->raw_q, skb);
872 queue_work(hdev->workqueue, &hdev->tx_work); 986 queue_work(hdev->workqueue, &hdev->tx_work);
873 } 987 }
@@ -895,7 +1009,7 @@ static int hci_sock_setsockopt(struct socket *sock, int level, int optname,
895 lock_sock(sk); 1009 lock_sock(sk);
896 1010
897 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1011 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
898 err = -EINVAL; 1012 err = -EBADFD;
899 goto done; 1013 goto done;
900 } 1014 }
901 1015
@@ -981,7 +1095,7 @@ static int hci_sock_getsockopt(struct socket *sock, int level, int optname,
981 lock_sock(sk); 1095 lock_sock(sk);
982 1096
983 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) { 1097 if (hci_pi(sk)->channel != HCI_CHANNEL_RAW) {
984 err = -EINVAL; 1098 err = -EBADFD;
985 goto done; 1099 goto done;
986 } 1100 }
987 1101
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..0cef67707838 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}
221
222static void l2cap_state_change(struct l2cap_chan *chan, int state)
223{
224 struct sock *sk = chan->sk;
225
226 lock_sock(sk);
227 __l2cap_state_change(chan, state);
228 release_sock(sk);
229} 233}
230 234
231static inline void __l2cap_chan_set_err(struct l2cap_chan *chan, int err) 235static inline void l2cap_state_change_and_error(struct l2cap_chan *chan,
236 int state, int err)
232{ 237{
233 struct sock *sk = chan->sk; 238 chan->state = state;
234 239 chan->ops->state_change(chan, chan->state, err);
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() &&
1021 chan->chan_policy == BT_CHANNEL_POLICY_AMP_PREFERRED &&
1022 conn->fixed_chan_mask & L2CAP_FC_A2MP)
1023 return true;
1024 else
1025 return false; 1037 return false;
1038
1039 if (!(conn->fixed_chan_mask & L2CAP_FC_A2MP))
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);
1381
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;
1361 1385
1362 lock_sock(parent); 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)) {
@@ -2826,17 +2813,16 @@ static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2826 mutex_lock(&conn->chan_lock); 2813 mutex_lock(&conn->chan_lock);
2827 2814
2828 list_for_each_entry(chan, &conn->chan_l, list) { 2815 list_for_each_entry(chan, &conn->chan_l, list) {
2829 struct sock *sk = chan->sk;
2830 if (chan->chan_type != L2CAP_CHAN_RAW) 2816 if (chan->chan_type != L2CAP_CHAN_RAW)
2831 continue; 2817 continue;
2832 2818
2833 /* Don't send frame to the socket it came from */ 2819 /* Don't send frame to the channel it came from */
2834 if (skb->sk == sk) 2820 if (bt_cb(skb)->chan == chan)
2835 continue; 2821 continue;
2822
2836 nskb = skb_clone(skb, GFP_KERNEL); 2823 nskb = skb_clone(skb, GFP_KERNEL);
2837 if (!nskb) 2824 if (!nskb)
2838 continue; 2825 continue;
2839
2840 if (chan->ops->recv(chan, nskb)) 2826 if (chan->ops->recv(chan, nskb))
2841 kfree_skb(nskb); 2827 kfree_skb(nskb);
2842 } 2828 }
@@ -3043,8 +3029,8 @@ int l2cap_ertm_init(struct l2cap_chan *chan)
3043 3029
3044 skb_queue_head_init(&chan->tx_q); 3030 skb_queue_head_init(&chan->tx_q);
3045 3031
3046 chan->local_amp_id = 0; 3032 chan->local_amp_id = AMP_ID_BREDR;
3047 chan->move_id = 0; 3033 chan->move_id = AMP_ID_BREDR;
3048 chan->move_state = L2CAP_MOVE_STABLE; 3034 chan->move_state = L2CAP_MOVE_STABLE;
3049 chan->move_role = L2CAP_MOVE_ROLE_NONE; 3035 chan->move_role = L2CAP_MOVE_ROLE_NONE;
3050 3036
@@ -3084,20 +3070,20 @@ static inline __u8 l2cap_select_mode(__u8 mode, __u16 remote_feat_mask)
3084 } 3070 }
3085} 3071}
3086 3072
3087static inline bool __l2cap_ews_supported(struct l2cap_chan *chan) 3073static inline bool __l2cap_ews_supported(struct l2cap_conn *conn)
3088{ 3074{
3089 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_WINDOW; 3075 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_WINDOW;
3090} 3076}
3091 3077
3092static inline bool __l2cap_efs_supported(struct l2cap_chan *chan) 3078static inline bool __l2cap_efs_supported(struct l2cap_conn *conn)
3093{ 3079{
3094 return enable_hs && chan->conn->feat_mask & L2CAP_FEAT_EXT_FLOW; 3080 return conn->hs_enabled && conn->feat_mask & L2CAP_FEAT_EXT_FLOW;
3095} 3081}
3096 3082
3097static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan, 3083static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3098 struct l2cap_conf_rfc *rfc) 3084 struct l2cap_conf_rfc *rfc)
3099{ 3085{
3100 if (chan->local_amp_id && chan->hs_hcon) { 3086 if (chan->local_amp_id != AMP_ID_BREDR && chan->hs_hcon) {
3101 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to; 3087 u64 ertm_to = chan->hs_hcon->hdev->amp_be_flush_to;
3102 3088
3103 /* Class 1 devices have must have ERTM timeouts 3089 /* Class 1 devices have must have ERTM timeouts
@@ -3135,7 +3121,7 @@ static void __l2cap_set_ertm_timeouts(struct l2cap_chan *chan,
3135static inline void l2cap_txwin_setup(struct l2cap_chan *chan) 3121static inline void l2cap_txwin_setup(struct l2cap_chan *chan)
3136{ 3122{
3137 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW && 3123 if (chan->tx_win > L2CAP_DEFAULT_TX_WINDOW &&
3138 __l2cap_ews_supported(chan)) { 3124 __l2cap_ews_supported(chan->conn)) {
3139 /* use extended control field */ 3125 /* use extended control field */
3140 set_bit(FLAG_EXT_CTRL, &chan->flags); 3126 set_bit(FLAG_EXT_CTRL, &chan->flags);
3141 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW; 3127 chan->tx_win_max = L2CAP_DEFAULT_EXT_WINDOW;
@@ -3165,7 +3151,7 @@ static int l2cap_build_conf_req(struct l2cap_chan *chan, void *data)
3165 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state)) 3151 if (test_bit(CONF_STATE2_DEVICE, &chan->conf_state))
3166 break; 3152 break;
3167 3153
3168 if (__l2cap_efs_supported(chan)) 3154 if (__l2cap_efs_supported(chan->conn))
3169 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3155 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3170 3156
3171 /* fall through */ 3157 /* fall through */
@@ -3317,7 +3303,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
3317 break; 3303 break;
3318 3304
3319 case L2CAP_CONF_EWS: 3305 case L2CAP_CONF_EWS:
3320 if (!enable_hs) 3306 if (!chan->conn->hs_enabled)
3321 return -ECONNREFUSED; 3307 return -ECONNREFUSED;
3322 3308
3323 set_bit(FLAG_EXT_CTRL, &chan->flags); 3309 set_bit(FLAG_EXT_CTRL, &chan->flags);
@@ -3349,7 +3335,7 @@ static int l2cap_parse_conf_req(struct l2cap_chan *chan, void *data)
3349 } 3335 }
3350 3336
3351 if (remote_efs) { 3337 if (remote_efs) {
3352 if (__l2cap_efs_supported(chan)) 3338 if (__l2cap_efs_supported(chan->conn))
3353 set_bit(FLAG_EFS_ENABLE, &chan->flags); 3339 set_bit(FLAG_EFS_ENABLE, &chan->flags);
3354 else 3340 else
3355 return -ECONNREFUSED; 3341 return -ECONNREFUSED;
@@ -3715,7 +3701,6 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3715 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data; 3701 struct l2cap_conn_req *req = (struct l2cap_conn_req *) data;
3716 struct l2cap_conn_rsp rsp; 3702 struct l2cap_conn_rsp rsp;
3717 struct l2cap_chan *chan = NULL, *pchan; 3703 struct l2cap_chan *chan = NULL, *pchan;
3718 struct sock *parent, *sk = NULL;
3719 int result, status = L2CAP_CS_NO_INFO; 3704 int result, status = L2CAP_CS_NO_INFO;
3720 3705
3721 u16 dcid = 0, scid = __le16_to_cpu(req->scid); 3706 u16 dcid = 0, scid = __le16_to_cpu(req->scid);
@@ -3724,16 +3709,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); 3709 BT_DBG("psm 0x%2.2x scid 0x%4.4x", __le16_to_cpu(psm), scid);
3725 3710
3726 /* Check if we have socket listening on psm */ 3711 /* Check if we have socket listening on psm */
3727 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, conn->src, conn->dst); 3712 pchan = l2cap_global_chan_by_psm(BT_LISTEN, psm, &conn->hcon->src,
3713 &conn->hcon->dst);
3728 if (!pchan) { 3714 if (!pchan) {
3729 result = L2CAP_CR_BAD_PSM; 3715 result = L2CAP_CR_BAD_PSM;
3730 goto sendresp; 3716 goto sendresp;
3731 } 3717 }
3732 3718
3733 parent = pchan->sk;
3734
3735 mutex_lock(&conn->chan_lock); 3719 mutex_lock(&conn->chan_lock);
3736 lock_sock(parent); 3720 l2cap_chan_lock(pchan);
3737 3721
3738 /* Check if the ACL is secure enough (if not SDP) */ 3722 /* Check if the ACL is secure enough (if not SDP) */
3739 if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) && 3723 if (psm != __constant_cpu_to_le16(L2CAP_PSM_SDP) &&
@@ -3753,8 +3737,6 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3753 if (!chan) 3737 if (!chan)
3754 goto response; 3738 goto response;
3755 3739
3756 sk = chan->sk;
3757
3758 /* For certain devices (ex: HID mouse), support for authentication, 3740 /* For certain devices (ex: HID mouse), support for authentication,
3759 * pairing and bonding is optional. For such devices, inorder to avoid 3741 * pairing and bonding is optional. For such devices, inorder to avoid
3760 * the ACL alive for too long after L2CAP disconnection, reset the ACL 3742 * the ACL alive for too long after L2CAP disconnection, reset the ACL
@@ -3762,8 +3744,10 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3762 */ 3744 */
3763 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT; 3745 conn->hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
3764 3746
3765 bacpy(&bt_sk(sk)->src, conn->src); 3747 bacpy(&chan->src, &conn->hcon->src);
3766 bacpy(&bt_sk(sk)->dst, conn->dst); 3748 bacpy(&chan->dst, &conn->hcon->dst);
3749 chan->src_type = bdaddr_type(conn->hcon, conn->hcon->src_type);
3750 chan->dst_type = bdaddr_type(conn->hcon, conn->hcon->dst_type);
3767 chan->psm = psm; 3751 chan->psm = psm;
3768 chan->dcid = scid; 3752 chan->dcid = scid;
3769 chan->local_amp_id = amp_id; 3753 chan->local_amp_id = amp_id;
@@ -3772,14 +3756,14 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3772 3756
3773 dcid = chan->scid; 3757 dcid = chan->scid;
3774 3758
3775 __set_chan_timer(chan, sk->sk_sndtimeo); 3759 __set_chan_timer(chan, chan->ops->get_sndtimeo(chan));
3776 3760
3777 chan->ident = cmd->ident; 3761 chan->ident = cmd->ident;
3778 3762
3779 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) { 3763 if (conn->info_state & L2CAP_INFO_FEAT_MASK_REQ_DONE) {
3780 if (l2cap_chan_check_security(chan)) { 3764 if (l2cap_chan_check_security(chan)) {
3781 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) { 3765 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
3782 __l2cap_state_change(chan, BT_CONNECT2); 3766 l2cap_state_change(chan, BT_CONNECT2);
3783 result = L2CAP_CR_PEND; 3767 result = L2CAP_CR_PEND;
3784 status = L2CAP_CS_AUTHOR_PEND; 3768 status = L2CAP_CS_AUTHOR_PEND;
3785 chan->ops->defer(chan); 3769 chan->ops->defer(chan);
@@ -3788,28 +3772,28 @@ static struct l2cap_chan *l2cap_connect(struct l2cap_conn *conn,
3788 * The connection will succeed after the 3772 * The connection will succeed after the
3789 * physical link is up. 3773 * physical link is up.
3790 */ 3774 */
3791 if (amp_id) { 3775 if (amp_id == AMP_ID_BREDR) {
3792 __l2cap_state_change(chan, BT_CONNECT2); 3776 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; 3777 result = L2CAP_CR_SUCCESS;
3778 } else {
3779 l2cap_state_change(chan, BT_CONNECT2);
3780 result = L2CAP_CR_PEND;
3797 } 3781 }
3798 status = L2CAP_CS_NO_INFO; 3782 status = L2CAP_CS_NO_INFO;
3799 } 3783 }
3800 } else { 3784 } else {
3801 __l2cap_state_change(chan, BT_CONNECT2); 3785 l2cap_state_change(chan, BT_CONNECT2);
3802 result = L2CAP_CR_PEND; 3786 result = L2CAP_CR_PEND;
3803 status = L2CAP_CS_AUTHEN_PEND; 3787 status = L2CAP_CS_AUTHEN_PEND;
3804 } 3788 }
3805 } else { 3789 } else {
3806 __l2cap_state_change(chan, BT_CONNECT2); 3790 l2cap_state_change(chan, BT_CONNECT2);
3807 result = L2CAP_CR_PEND; 3791 result = L2CAP_CR_PEND;
3808 status = L2CAP_CS_NO_INFO; 3792 status = L2CAP_CS_NO_INFO;
3809 } 3793 }
3810 3794
3811response: 3795response:
3812 release_sock(parent); 3796 l2cap_chan_unlock(pchan);
3813 mutex_unlock(&conn->chan_lock); 3797 mutex_unlock(&conn->chan_lock);
3814 3798
3815sendresp: 3799sendresp:
@@ -3891,13 +3875,13 @@ static int l2cap_connect_create_rsp(struct l2cap_conn *conn,
3891 if (scid) { 3875 if (scid) {
3892 chan = __l2cap_get_chan_by_scid(conn, scid); 3876 chan = __l2cap_get_chan_by_scid(conn, scid);
3893 if (!chan) { 3877 if (!chan) {
3894 err = -EFAULT; 3878 err = -EBADSLT;
3895 goto unlock; 3879 goto unlock;
3896 } 3880 }
3897 } else { 3881 } else {
3898 chan = __l2cap_get_chan_by_ident(conn, cmd->ident); 3882 chan = __l2cap_get_chan_by_ident(conn, cmd->ident);
3899 if (!chan) { 3883 if (!chan) {
3900 err = -EFAULT; 3884 err = -EBADSLT;
3901 goto unlock; 3885 goto unlock;
3902 } 3886 }
3903 } 3887 }
@@ -3965,6 +3949,18 @@ static void l2cap_send_efs_conf_rsp(struct l2cap_chan *chan, void *data,
3965 L2CAP_CONF_SUCCESS, flags), data); 3949 L2CAP_CONF_SUCCESS, flags), data);
3966} 3950}
3967 3951
3952static void cmd_reject_invalid_cid(struct l2cap_conn *conn, u8 ident,
3953 u16 scid, u16 dcid)
3954{
3955 struct l2cap_cmd_rej_cid rej;
3956
3957 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_INVALID_CID);
3958 rej.scid = __cpu_to_le16(scid);
3959 rej.dcid = __cpu_to_le16(dcid);
3960
3961 l2cap_send_cmd(conn, ident, L2CAP_COMMAND_REJ, sizeof(rej), &rej);
3962}
3963
3968static inline int l2cap_config_req(struct l2cap_conn *conn, 3964static inline int l2cap_config_req(struct l2cap_conn *conn,
3969 struct l2cap_cmd_hdr *cmd, u16 cmd_len, 3965 struct l2cap_cmd_hdr *cmd, u16 cmd_len,
3970 u8 *data) 3966 u8 *data)
@@ -3984,18 +3980,14 @@ static inline int l2cap_config_req(struct l2cap_conn *conn,
3984 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags); 3980 BT_DBG("dcid 0x%4.4x flags 0x%2.2x", dcid, flags);
3985 3981
3986 chan = l2cap_get_chan_by_scid(conn, dcid); 3982 chan = l2cap_get_chan_by_scid(conn, dcid);
3987 if (!chan) 3983 if (!chan) {
3988 return -ENOENT; 3984 cmd_reject_invalid_cid(conn, cmd->ident, dcid, 0);
3985 return 0;
3986 }
3989 3987
3990 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) { 3988 if (chan->state != BT_CONFIG && chan->state != BT_CONNECT2) {
3991 struct l2cap_cmd_rej_cid rej; 3989 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
3992 3990 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; 3991 goto unlock;
4000 } 3992 }
4001 3993
@@ -4198,7 +4190,6 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
4198 struct l2cap_disconn_rsp rsp; 4190 struct l2cap_disconn_rsp rsp;
4199 u16 dcid, scid; 4191 u16 dcid, scid;
4200 struct l2cap_chan *chan; 4192 struct l2cap_chan *chan;
4201 struct sock *sk;
4202 4193
4203 if (cmd_len != sizeof(*req)) 4194 if (cmd_len != sizeof(*req))
4204 return -EPROTO; 4195 return -EPROTO;
@@ -4213,20 +4204,17 @@ static inline int l2cap_disconnect_req(struct l2cap_conn *conn,
4213 chan = __l2cap_get_chan_by_scid(conn, dcid); 4204 chan = __l2cap_get_chan_by_scid(conn, dcid);
4214 if (!chan) { 4205 if (!chan) {
4215 mutex_unlock(&conn->chan_lock); 4206 mutex_unlock(&conn->chan_lock);
4207 cmd_reject_invalid_cid(conn, cmd->ident, dcid, scid);
4216 return 0; 4208 return 0;
4217 } 4209 }
4218 4210
4219 l2cap_chan_lock(chan); 4211 l2cap_chan_lock(chan);
4220 4212
4221 sk = chan->sk;
4222
4223 rsp.dcid = cpu_to_le16(chan->scid); 4213 rsp.dcid = cpu_to_le16(chan->scid);
4224 rsp.scid = cpu_to_le16(chan->dcid); 4214 rsp.scid = cpu_to_le16(chan->dcid);
4225 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp); 4215 l2cap_send_cmd(conn, cmd->ident, L2CAP_DISCONN_RSP, sizeof(rsp), &rsp);
4226 4216
4227 lock_sock(sk); 4217 chan->ops->set_shutdown(chan);
4228 sk->sk_shutdown = SHUTDOWN_MASK;
4229 release_sock(sk);
4230 4218
4231 l2cap_chan_hold(chan); 4219 l2cap_chan_hold(chan);
4232 l2cap_chan_del(chan, ECONNRESET); 4220 l2cap_chan_del(chan, ECONNRESET);
@@ -4303,7 +4291,7 @@ static inline int l2cap_information_req(struct l2cap_conn *conn,
4303 if (!disable_ertm) 4291 if (!disable_ertm)
4304 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING 4292 feat_mask |= L2CAP_FEAT_ERTM | L2CAP_FEAT_STREAMING
4305 | L2CAP_FEAT_FCS; 4293 | L2CAP_FEAT_FCS;
4306 if (enable_hs) 4294 if (conn->hs_enabled)
4307 feat_mask |= L2CAP_FEAT_EXT_FLOW 4295 feat_mask |= L2CAP_FEAT_EXT_FLOW
4308 | L2CAP_FEAT_EXT_WINDOW; 4296 | L2CAP_FEAT_EXT_WINDOW;
4309 4297
@@ -4314,7 +4302,7 @@ static inline int l2cap_information_req(struct l2cap_conn *conn,
4314 u8 buf[12]; 4302 u8 buf[12];
4315 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf; 4303 struct l2cap_info_rsp *rsp = (struct l2cap_info_rsp *) buf;
4316 4304
4317 if (enable_hs) 4305 if (conn->hs_enabled)
4318 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP; 4306 l2cap_fixed_chan[0] |= L2CAP_FC_A2MP;
4319 else 4307 else
4320 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP; 4308 l2cap_fixed_chan[0] &= ~L2CAP_FC_A2MP;
@@ -4411,7 +4399,7 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn,
4411 if (cmd_len != sizeof(*req)) 4399 if (cmd_len != sizeof(*req))
4412 return -EPROTO; 4400 return -EPROTO;
4413 4401
4414 if (!enable_hs) 4402 if (!conn->hs_enabled)
4415 return -EINVAL; 4403 return -EINVAL;
4416 4404
4417 psm = le16_to_cpu(req->psm); 4405 psm = le16_to_cpu(req->psm);
@@ -4420,7 +4408,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); 4408 BT_DBG("psm 0x%2.2x, scid 0x%4.4x, amp_id %d", psm, scid, req->amp_id);
4421 4409
4422 /* For controller id 0 make BR/EDR connection */ 4410 /* For controller id 0 make BR/EDR connection */
4423 if (req->amp_id == HCI_BREDR_ID) { 4411 if (req->amp_id == AMP_ID_BREDR) {
4424 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP, 4412 l2cap_connect(conn, cmd, data, L2CAP_CREATE_CHAN_RSP,
4425 req->amp_id); 4413 req->amp_id);
4426 return 0; 4414 return 0;
@@ -4442,10 +4430,13 @@ static int l2cap_create_channel_req(struct l2cap_conn *conn,
4442 struct amp_mgr *mgr = conn->hcon->amp_mgr; 4430 struct amp_mgr *mgr = conn->hcon->amp_mgr;
4443 struct hci_conn *hs_hcon; 4431 struct hci_conn *hs_hcon;
4444 4432
4445 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK, conn->dst); 4433 hs_hcon = hci_conn_hash_lookup_ba(hdev, AMP_LINK,
4434 &conn->hcon->dst);
4446 if (!hs_hcon) { 4435 if (!hs_hcon) {
4447 hci_dev_put(hdev); 4436 hci_dev_put(hdev);
4448 return -EFAULT; 4437 cmd_reject_invalid_cid(conn, cmd->ident, chan->scid,
4438 chan->dcid);
4439 return 0;
4449 } 4440 }
4450 4441
4451 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon); 4442 BT_DBG("mgr %p bredr_chan %p hs_hcon %p", mgr, chan, hs_hcon);
@@ -4469,7 +4460,7 @@ error:
4469 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP, 4460 l2cap_send_cmd(conn, cmd->ident, L2CAP_CREATE_CHAN_RSP,
4470 sizeof(rsp), &rsp); 4461 sizeof(rsp), &rsp);
4471 4462
4472 return -EFAULT; 4463 return 0;
4473} 4464}
4474 4465
4475static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id) 4466static void l2cap_send_move_chan_req(struct l2cap_chan *chan, u8 dest_amp_id)
@@ -4655,7 +4646,7 @@ void l2cap_logical_cfm(struct l2cap_chan *chan, struct hci_chan *hchan,
4655 4646
4656 if (chan->state != BT_CONNECTED) { 4647 if (chan->state != BT_CONNECTED) {
4657 /* Ignore logical link if channel is on BR/EDR */ 4648 /* Ignore logical link if channel is on BR/EDR */
4658 if (chan->local_amp_id) 4649 if (chan->local_amp_id != AMP_ID_BREDR)
4659 l2cap_logical_finish_create(chan, hchan); 4650 l2cap_logical_finish_create(chan, hchan);
4660 } else { 4651 } else {
4661 l2cap_logical_finish_move(chan, hchan); 4652 l2cap_logical_finish_move(chan, hchan);
@@ -4666,7 +4657,7 @@ void l2cap_move_start(struct l2cap_chan *chan)
4666{ 4657{
4667 BT_DBG("chan %p", chan); 4658 BT_DBG("chan %p", chan);
4668 4659
4669 if (chan->local_amp_id == HCI_BREDR_ID) { 4660 if (chan->local_amp_id == AMP_ID_BREDR) {
4670 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED) 4661 if (chan->chan_policy != BT_CHANNEL_POLICY_AMP_PREFERRED)
4671 return; 4662 return;
4672 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR; 4663 chan->move_role = L2CAP_MOVE_ROLE_INITIATOR;
@@ -4723,7 +4714,7 @@ static void l2cap_do_create(struct l2cap_chan *chan, int result,
4723 sizeof(rsp), &rsp); 4714 sizeof(rsp), &rsp);
4724 4715
4725 if (result == L2CAP_CR_SUCCESS) { 4716 if (result == L2CAP_CR_SUCCESS) {
4726 __l2cap_state_change(chan, BT_CONFIG); 4717 l2cap_state_change(chan, BT_CONFIG);
4727 set_bit(CONF_REQ_SENT, &chan->conf_state); 4718 set_bit(CONF_REQ_SENT, &chan->conf_state);
4728 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn), 4719 l2cap_send_cmd(chan->conn, l2cap_get_ident(chan->conn),
4729 L2CAP_CONF_REQ, 4720 L2CAP_CONF_REQ,
@@ -4838,7 +4829,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4838 4829
4839 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id); 4830 BT_DBG("icid 0x%4.4x, dest_amp_id %d", icid, req->dest_amp_id);
4840 4831
4841 if (!enable_hs) 4832 if (!conn->hs_enabled)
4842 return -EINVAL; 4833 return -EINVAL;
4843 4834
4844 chan = l2cap_get_chan_by_dcid(conn, icid); 4835 chan = l2cap_get_chan_by_dcid(conn, icid);
@@ -4865,7 +4856,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4865 goto send_move_response; 4856 goto send_move_response;
4866 } 4857 }
4867 4858
4868 if (req->dest_amp_id) { 4859 if (req->dest_amp_id != AMP_ID_BREDR) {
4869 struct hci_dev *hdev; 4860 struct hci_dev *hdev;
4870 hdev = hci_dev_get(req->dest_amp_id); 4861 hdev = hci_dev_get(req->dest_amp_id);
4871 if (!hdev || hdev->dev_type != HCI_AMP || 4862 if (!hdev || hdev->dev_type != HCI_AMP ||
@@ -4885,7 +4876,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4885 */ 4876 */
4886 if ((__chan_is_moving(chan) || 4877 if ((__chan_is_moving(chan) ||
4887 chan->move_role != L2CAP_MOVE_ROLE_NONE) && 4878 chan->move_role != L2CAP_MOVE_ROLE_NONE) &&
4888 bacmp(conn->src, conn->dst) > 0) { 4879 bacmp(&conn->hcon->src, &conn->hcon->dst) > 0) {
4889 result = L2CAP_MR_COLLISION; 4880 result = L2CAP_MR_COLLISION;
4890 goto send_move_response; 4881 goto send_move_response;
4891 } 4882 }
@@ -4895,7 +4886,7 @@ static inline int l2cap_move_channel_req(struct l2cap_conn *conn,
4895 chan->move_id = req->dest_amp_id; 4886 chan->move_id = req->dest_amp_id;
4896 icid = chan->dcid; 4887 icid = chan->dcid;
4897 4888
4898 if (!req->dest_amp_id) { 4889 if (req->dest_amp_id == AMP_ID_BREDR) {
4899 /* Moving to BR/EDR */ 4890 /* Moving to BR/EDR */
4900 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) { 4891 if (test_bit(CONN_LOCAL_BUSY, &chan->conn_state)) {
4901 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY; 4892 chan->move_state = L2CAP_MOVE_WAIT_LOCAL_BUSY;
@@ -5087,7 +5078,7 @@ static int l2cap_move_channel_confirm(struct l2cap_conn *conn,
5087 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) { 5078 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM) {
5088 if (result == L2CAP_MC_CONFIRMED) { 5079 if (result == L2CAP_MC_CONFIRMED) {
5089 chan->local_amp_id = chan->move_id; 5080 chan->local_amp_id = chan->move_id;
5090 if (!chan->local_amp_id) 5081 if (chan->local_amp_id == AMP_ID_BREDR)
5091 __release_logical_link(chan); 5082 __release_logical_link(chan);
5092 } else { 5083 } else {
5093 chan->move_id = chan->local_amp_id; 5084 chan->move_id = chan->local_amp_id;
@@ -5127,7 +5118,7 @@ static inline int l2cap_move_channel_confirm_rsp(struct l2cap_conn *conn,
5127 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) { 5118 if (chan->move_state == L2CAP_MOVE_WAIT_CONFIRM_RSP) {
5128 chan->local_amp_id = chan->move_id; 5119 chan->local_amp_id = chan->move_id;
5129 5120
5130 if (!chan->local_amp_id && chan->hs_hchan) 5121 if (chan->local_amp_id == AMP_ID_BREDR && chan->hs_hchan)
5131 __release_logical_link(chan); 5122 __release_logical_link(chan);
5132 5123
5133 l2cap_move_done(chan); 5124 l2cap_move_done(chan);
@@ -5219,7 +5210,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5219 5210
5220 case L2CAP_CONN_RSP: 5211 case L2CAP_CONN_RSP:
5221 case L2CAP_CREATE_CHAN_RSP: 5212 case L2CAP_CREATE_CHAN_RSP:
5222 err = l2cap_connect_create_rsp(conn, cmd, cmd_len, data); 5213 l2cap_connect_create_rsp(conn, cmd, cmd_len, data);
5223 break; 5214 break;
5224 5215
5225 case L2CAP_CONF_REQ: 5216 case L2CAP_CONF_REQ:
@@ -5227,7 +5218,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5227 break; 5218 break;
5228 5219
5229 case L2CAP_CONF_RSP: 5220 case L2CAP_CONF_RSP:
5230 err = l2cap_config_rsp(conn, cmd, cmd_len, data); 5221 l2cap_config_rsp(conn, cmd, cmd_len, data);
5231 break; 5222 break;
5232 5223
5233 case L2CAP_DISCONN_REQ: 5224 case L2CAP_DISCONN_REQ:
@@ -5235,7 +5226,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5235 break; 5226 break;
5236 5227
5237 case L2CAP_DISCONN_RSP: 5228 case L2CAP_DISCONN_RSP:
5238 err = l2cap_disconnect_rsp(conn, cmd, cmd_len, data); 5229 l2cap_disconnect_rsp(conn, cmd, cmd_len, data);
5239 break; 5230 break;
5240 5231
5241 case L2CAP_ECHO_REQ: 5232 case L2CAP_ECHO_REQ:
@@ -5250,7 +5241,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5250 break; 5241 break;
5251 5242
5252 case L2CAP_INFO_RSP: 5243 case L2CAP_INFO_RSP:
5253 err = l2cap_information_rsp(conn, cmd, cmd_len, data); 5244 l2cap_information_rsp(conn, cmd, cmd_len, data);
5254 break; 5245 break;
5255 5246
5256 case L2CAP_CREATE_CHAN_REQ: 5247 case L2CAP_CREATE_CHAN_REQ:
@@ -5262,7 +5253,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5262 break; 5253 break;
5263 5254
5264 case L2CAP_MOVE_CHAN_RSP: 5255 case L2CAP_MOVE_CHAN_RSP:
5265 err = l2cap_move_channel_rsp(conn, cmd, cmd_len, data); 5256 l2cap_move_channel_rsp(conn, cmd, cmd_len, data);
5266 break; 5257 break;
5267 5258
5268 case L2CAP_MOVE_CHAN_CFM: 5259 case L2CAP_MOVE_CHAN_CFM:
@@ -5270,7 +5261,7 @@ static inline int l2cap_bredr_sig_cmd(struct l2cap_conn *conn,
5270 break; 5261 break;
5271 5262
5272 case L2CAP_MOVE_CHAN_CFM_RSP: 5263 case L2CAP_MOVE_CHAN_CFM_RSP:
5273 err = l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data); 5264 l2cap_move_channel_confirm_rsp(conn, cmd, cmd_len, data);
5274 break; 5265 break;
5275 5266
5276 default: 5267 default:
@@ -5304,51 +5295,48 @@ static inline int l2cap_le_sig_cmd(struct l2cap_conn *conn,
5304static inline void l2cap_le_sig_channel(struct l2cap_conn *conn, 5295static inline void l2cap_le_sig_channel(struct l2cap_conn *conn,
5305 struct sk_buff *skb) 5296 struct sk_buff *skb)
5306{ 5297{
5307 u8 *data = skb->data; 5298 struct hci_conn *hcon = conn->hcon;
5308 int len = skb->len; 5299 struct l2cap_cmd_hdr *cmd;
5309 struct l2cap_cmd_hdr cmd; 5300 u16 len;
5310 int err; 5301 int err;
5311 5302
5312 l2cap_raw_recv(conn, skb); 5303 if (hcon->type != LE_LINK)
5304 goto drop;
5313 5305
5314 while (len >= L2CAP_CMD_HDR_SIZE) { 5306 if (skb->len < L2CAP_CMD_HDR_SIZE)
5315 u16 cmd_len; 5307 goto drop;
5316 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
5317 data += L2CAP_CMD_HDR_SIZE;
5318 len -= L2CAP_CMD_HDR_SIZE;
5319 5308
5320 cmd_len = le16_to_cpu(cmd.len); 5309 cmd = (void *) skb->data;
5310 skb_pull(skb, L2CAP_CMD_HDR_SIZE);
5321 5311
5322 BT_DBG("code 0x%2.2x len %d id 0x%2.2x", cmd.code, cmd_len, 5312 len = le16_to_cpu(cmd->len);
5323 cmd.ident);
5324 5313
5325 if (cmd_len > len || !cmd.ident) { 5314 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 5315
5330 err = l2cap_le_sig_cmd(conn, &cmd, data); 5316 if (len != skb->len || !cmd->ident) {
5331 if (err) { 5317 BT_DBG("corrupted command");
5332 struct l2cap_cmd_rej_unk rej; 5318 goto drop;
5319 }
5333 5320
5334 BT_ERR("Wrong link type (%d)", err); 5321 err = l2cap_le_sig_cmd(conn, cmd, skb->data);
5322 if (err) {
5323 struct l2cap_cmd_rej_unk rej;
5335 5324
5336 /* FIXME: Map err to a valid reason */ 5325 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 5326
5342 data += cmd_len; 5327 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5343 len -= cmd_len; 5328 l2cap_send_cmd(conn, cmd->ident, L2CAP_COMMAND_REJ,
5329 sizeof(rej), &rej);
5344 } 5330 }
5345 5331
5332drop:
5346 kfree_skb(skb); 5333 kfree_skb(skb);
5347} 5334}
5348 5335
5349static inline void l2cap_sig_channel(struct l2cap_conn *conn, 5336static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5350 struct sk_buff *skb) 5337 struct sk_buff *skb)
5351{ 5338{
5339 struct hci_conn *hcon = conn->hcon;
5352 u8 *data = skb->data; 5340 u8 *data = skb->data;
5353 int len = skb->len; 5341 int len = skb->len;
5354 struct l2cap_cmd_hdr cmd; 5342 struct l2cap_cmd_hdr cmd;
@@ -5356,6 +5344,9 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5356 5344
5357 l2cap_raw_recv(conn, skb); 5345 l2cap_raw_recv(conn, skb);
5358 5346
5347 if (hcon->type != ACL_LINK)
5348 goto drop;
5349
5359 while (len >= L2CAP_CMD_HDR_SIZE) { 5350 while (len >= L2CAP_CMD_HDR_SIZE) {
5360 u16 cmd_len; 5351 u16 cmd_len;
5361 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE); 5352 memcpy(&cmd, data, L2CAP_CMD_HDR_SIZE);
@@ -5378,7 +5369,6 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5378 5369
5379 BT_ERR("Wrong link type (%d)", err); 5370 BT_ERR("Wrong link type (%d)", err);
5380 5371
5381 /* FIXME: Map err to a valid reason */
5382 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD); 5372 rej.reason = __constant_cpu_to_le16(L2CAP_REJ_NOT_UNDERSTOOD);
5383 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ, 5373 l2cap_send_cmd(conn, cmd.ident, L2CAP_COMMAND_REJ,
5384 sizeof(rej), &rej); 5374 sizeof(rej), &rej);
@@ -5388,6 +5378,7 @@ static inline void l2cap_sig_channel(struct l2cap_conn *conn,
5388 len -= cmd_len; 5378 len -= cmd_len;
5389 } 5379 }
5390 5380
5381drop:
5391 kfree_skb(skb); 5382 kfree_skb(skb);
5392} 5383}
5393 5384
@@ -5784,7 +5775,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5784 struct sk_buff *skb, u8 event) 5775 struct sk_buff *skb, u8 event)
5785{ 5776{
5786 int err = 0; 5777 int err = 0;
5787 bool skb_in_use = 0; 5778 bool skb_in_use = false;
5788 5779
5789 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 5780 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5790 event); 5781 event);
@@ -5805,7 +5796,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5805 control->txseq); 5796 control->txseq);
5806 5797
5807 chan->buffer_seq = chan->expected_tx_seq; 5798 chan->buffer_seq = chan->expected_tx_seq;
5808 skb_in_use = 1; 5799 skb_in_use = true;
5809 5800
5810 err = l2cap_reassemble_sdu(chan, skb, control); 5801 err = l2cap_reassemble_sdu(chan, skb, control);
5811 if (err) 5802 if (err)
@@ -5841,7 +5832,7 @@ static int l2cap_rx_state_recv(struct l2cap_chan *chan,
5841 * current frame is stored for later use. 5832 * current frame is stored for later use.
5842 */ 5833 */
5843 skb_queue_tail(&chan->srej_q, skb); 5834 skb_queue_tail(&chan->srej_q, skb);
5844 skb_in_use = 1; 5835 skb_in_use = true;
5845 BT_DBG("Queued %p (queue len %d)", skb, 5836 BT_DBG("Queued %p (queue len %d)", skb,
5846 skb_queue_len(&chan->srej_q)); 5837 skb_queue_len(&chan->srej_q));
5847 5838
@@ -5919,7 +5910,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5919{ 5910{
5920 int err = 0; 5911 int err = 0;
5921 u16 txseq = control->txseq; 5912 u16 txseq = control->txseq;
5922 bool skb_in_use = 0; 5913 bool skb_in_use = false;
5923 5914
5924 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb, 5915 BT_DBG("chan %p, control %p, skb %p, event %d", chan, control, skb,
5925 event); 5916 event);
@@ -5931,7 +5922,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5931 /* Keep frame for reassembly later */ 5922 /* Keep frame for reassembly later */
5932 l2cap_pass_to_tx(chan, control); 5923 l2cap_pass_to_tx(chan, control);
5933 skb_queue_tail(&chan->srej_q, skb); 5924 skb_queue_tail(&chan->srej_q, skb);
5934 skb_in_use = 1; 5925 skb_in_use = true;
5935 BT_DBG("Queued %p (queue len %d)", skb, 5926 BT_DBG("Queued %p (queue len %d)", skb,
5936 skb_queue_len(&chan->srej_q)); 5927 skb_queue_len(&chan->srej_q));
5937 5928
@@ -5942,7 +5933,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5942 5933
5943 l2cap_pass_to_tx(chan, control); 5934 l2cap_pass_to_tx(chan, control);
5944 skb_queue_tail(&chan->srej_q, skb); 5935 skb_queue_tail(&chan->srej_q, skb);
5945 skb_in_use = 1; 5936 skb_in_use = true;
5946 BT_DBG("Queued %p (queue len %d)", skb, 5937 BT_DBG("Queued %p (queue len %d)", skb,
5947 skb_queue_len(&chan->srej_q)); 5938 skb_queue_len(&chan->srej_q));
5948 5939
@@ -5957,7 +5948,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5957 * the missing frames. 5948 * the missing frames.
5958 */ 5949 */
5959 skb_queue_tail(&chan->srej_q, skb); 5950 skb_queue_tail(&chan->srej_q, skb);
5960 skb_in_use = 1; 5951 skb_in_use = true;
5961 BT_DBG("Queued %p (queue len %d)", skb, 5952 BT_DBG("Queued %p (queue len %d)", skb,
5962 skb_queue_len(&chan->srej_q)); 5953 skb_queue_len(&chan->srej_q));
5963 5954
@@ -5971,7 +5962,7 @@ static int l2cap_rx_state_srej_sent(struct l2cap_chan *chan,
5971 * SREJ'd frames. 5962 * SREJ'd frames.
5972 */ 5963 */
5973 skb_queue_tail(&chan->srej_q, skb); 5964 skb_queue_tail(&chan->srej_q, skb);
5974 skb_in_use = 1; 5965 skb_in_use = true;
5975 BT_DBG("Queued %p (queue len %d)", skb, 5966 BT_DBG("Queued %p (queue len %d)", skb,
5976 skb_queue_len(&chan->srej_q)); 5967 skb_queue_len(&chan->srej_q));
5977 5968
@@ -6380,9 +6371,13 @@ done:
6380static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm, 6371static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6381 struct sk_buff *skb) 6372 struct sk_buff *skb)
6382{ 6373{
6374 struct hci_conn *hcon = conn->hcon;
6383 struct l2cap_chan *chan; 6375 struct l2cap_chan *chan;
6384 6376
6385 chan = l2cap_global_chan_by_psm(0, psm, conn->src, conn->dst); 6377 if (hcon->type != ACL_LINK)
6378 goto drop;
6379
6380 chan = l2cap_global_chan_by_psm(0, psm, &hcon->src, &hcon->dst);
6386 if (!chan) 6381 if (!chan)
6387 goto drop; 6382 goto drop;
6388 6383
@@ -6394,6 +6389,10 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6394 if (chan->imtu < skb->len) 6389 if (chan->imtu < skb->len)
6395 goto drop; 6390 goto drop;
6396 6391
6392 /* Store remote BD_ADDR and PSM for msg_name */
6393 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst);
6394 bt_cb(skb)->psm = psm;
6395
6397 if (!chan->ops->recv(chan, skb)) 6396 if (!chan->ops->recv(chan, skb))
6398 return; 6397 return;
6399 6398
@@ -6404,15 +6403,22 @@ drop:
6404static void l2cap_att_channel(struct l2cap_conn *conn, 6403static void l2cap_att_channel(struct l2cap_conn *conn,
6405 struct sk_buff *skb) 6404 struct sk_buff *skb)
6406{ 6405{
6406 struct hci_conn *hcon = conn->hcon;
6407 struct l2cap_chan *chan; 6407 struct l2cap_chan *chan;
6408 6408
6409 if (hcon->type != LE_LINK)
6410 goto drop;
6411
6409 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT, 6412 chan = l2cap_global_chan_by_scid(BT_CONNECTED, L2CAP_CID_ATT,
6410 conn->src, conn->dst); 6413 &hcon->src, &hcon->dst);
6411 if (!chan) 6414 if (!chan)
6412 goto drop; 6415 goto drop;
6413 6416
6414 BT_DBG("chan %p, len %d", chan, skb->len); 6417 BT_DBG("chan %p, len %d", chan, skb->len);
6415 6418
6419 if (hci_blacklist_lookup(hcon->hdev, &hcon->dst, hcon->dst_type))
6420 goto drop;
6421
6416 if (chan->imtu < skb->len) 6422 if (chan->imtu < skb->len)
6417 goto drop; 6423 goto drop;
6418 6424
@@ -6441,9 +6447,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); 6447 BT_DBG("len %d, cid 0x%4.4x", len, cid);
6442 6448
6443 switch (cid) { 6449 switch (cid) {
6444 case L2CAP_CID_LE_SIGNALING:
6445 l2cap_le_sig_channel(conn, skb);
6446 break;
6447 case L2CAP_CID_SIGNALING: 6450 case L2CAP_CID_SIGNALING:
6448 l2cap_sig_channel(conn, skb); 6451 l2cap_sig_channel(conn, skb);
6449 break; 6452 break;
@@ -6458,6 +6461,10 @@ static void l2cap_recv_frame(struct l2cap_conn *conn, struct sk_buff *skb)
6458 l2cap_att_channel(conn, skb); 6461 l2cap_att_channel(conn, skb);
6459 break; 6462 break;
6460 6463
6464 case L2CAP_CID_LE_SIGNALING:
6465 l2cap_le_sig_channel(conn, skb);
6466 break;
6467
6461 case L2CAP_CID_SMP: 6468 case L2CAP_CID_SMP:
6462 if (smp_sig_channel(conn, skb)) 6469 if (smp_sig_channel(conn, skb))
6463 l2cap_conn_del(conn->hcon, EACCES); 6470 l2cap_conn_del(conn->hcon, EACCES);
@@ -6481,17 +6488,15 @@ int l2cap_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr)
6481 /* Find listening sockets and check their link_mode */ 6488 /* Find listening sockets and check their link_mode */
6482 read_lock(&chan_list_lock); 6489 read_lock(&chan_list_lock);
6483 list_for_each_entry(c, &chan_list, global_l) { 6490 list_for_each_entry(c, &chan_list, global_l) {
6484 struct sock *sk = c->sk;
6485
6486 if (c->state != BT_LISTEN) 6491 if (c->state != BT_LISTEN)
6487 continue; 6492 continue;
6488 6493
6489 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr)) { 6494 if (!bacmp(&c->src, &hdev->bdaddr)) {
6490 lm1 |= HCI_LM_ACCEPT; 6495 lm1 |= HCI_LM_ACCEPT;
6491 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 6496 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
6492 lm1 |= HCI_LM_MASTER; 6497 lm1 |= HCI_LM_MASTER;
6493 exact++; 6498 exact++;
6494 } else if (!bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 6499 } else if (!bacmp(&c->src, BDADDR_ANY)) {
6495 lm2 |= HCI_LM_ACCEPT; 6500 lm2 |= HCI_LM_ACCEPT;
6496 if (test_bit(FLAG_ROLE_SWITCH, &c->flags)) 6501 if (test_bit(FLAG_ROLE_SWITCH, &c->flags))
6497 lm2 |= HCI_LM_MASTER; 6502 lm2 |= HCI_LM_MASTER;
@@ -6597,11 +6602,7 @@ int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
6597 6602
6598 if (!status && (chan->state == BT_CONNECTED || 6603 if (!status && (chan->state == BT_CONNECTED ||
6599 chan->state == BT_CONFIG)) { 6604 chan->state == BT_CONFIG)) {
6600 struct sock *sk = chan->sk; 6605 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); 6606 l2cap_check_encryption(chan, encrypt);
6606 l2cap_chan_unlock(chan); 6607 l2cap_chan_unlock(chan);
6607 continue; 6608 continue;
@@ -6614,32 +6615,26 @@ int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
6614 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 6615 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
6615 } 6616 }
6616 } else if (chan->state == BT_CONNECT2) { 6617 } else if (chan->state == BT_CONNECT2) {
6617 struct sock *sk = chan->sk;
6618 struct l2cap_conn_rsp rsp; 6618 struct l2cap_conn_rsp rsp;
6619 __u16 res, stat; 6619 __u16 res, stat;
6620 6620
6621 lock_sock(sk);
6622
6623 if (!status) { 6621 if (!status) {
6624 if (test_bit(BT_SK_DEFER_SETUP, 6622 if (test_bit(FLAG_DEFER_SETUP, &chan->flags)) {
6625 &bt_sk(sk)->flags)) {
6626 res = L2CAP_CR_PEND; 6623 res = L2CAP_CR_PEND;
6627 stat = L2CAP_CS_AUTHOR_PEND; 6624 stat = L2CAP_CS_AUTHOR_PEND;
6628 chan->ops->defer(chan); 6625 chan->ops->defer(chan);
6629 } else { 6626 } else {
6630 __l2cap_state_change(chan, BT_CONFIG); 6627 l2cap_state_change(chan, BT_CONFIG);
6631 res = L2CAP_CR_SUCCESS; 6628 res = L2CAP_CR_SUCCESS;
6632 stat = L2CAP_CS_NO_INFO; 6629 stat = L2CAP_CS_NO_INFO;
6633 } 6630 }
6634 } else { 6631 } else {
6635 __l2cap_state_change(chan, BT_DISCONN); 6632 l2cap_state_change(chan, BT_DISCONN);
6636 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT); 6633 __set_chan_timer(chan, L2CAP_DISC_TIMEOUT);
6637 res = L2CAP_CR_SEC_BLOCK; 6634 res = L2CAP_CR_SEC_BLOCK;
6638 stat = L2CAP_CS_NO_INFO; 6635 stat = L2CAP_CS_NO_INFO;
6639 } 6636 }
6640 6637
6641 release_sock(sk);
6642
6643 rsp.scid = cpu_to_le16(chan->dcid); 6638 rsp.scid = cpu_to_le16(chan->dcid);
6644 rsp.dcid = cpu_to_le16(chan->scid); 6639 rsp.dcid = cpu_to_le16(chan->scid);
6645 rsp.result = cpu_to_le16(res); 6640 rsp.result = cpu_to_le16(res);
@@ -6756,9 +6751,13 @@ int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
6756 conn->rx_len -= skb->len; 6751 conn->rx_len -= skb->len;
6757 6752
6758 if (!conn->rx_len) { 6753 if (!conn->rx_len) {
6759 /* Complete frame received */ 6754 /* Complete frame received. l2cap_recv_frame
6760 l2cap_recv_frame(conn, conn->rx_skb); 6755 * takes ownership of the skb so set the global
6756 * rx_skb pointer to NULL first.
6757 */
6758 struct sk_buff *rx_skb = conn->rx_skb;
6761 conn->rx_skb = NULL; 6759 conn->rx_skb = NULL;
6760 l2cap_recv_frame(conn, rx_skb);
6762 } 6761 }
6763 break; 6762 break;
6764 } 6763 }
@@ -6775,10 +6774,8 @@ static int l2cap_debugfs_show(struct seq_file *f, void *p)
6775 read_lock(&chan_list_lock); 6774 read_lock(&chan_list_lock);
6776 6775
6777 list_for_each_entry(c, &chan_list, global_l) { 6776 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", 6777 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, 6778 &c->src, &c->dst,
6782 c->state, __le16_to_cpu(c->psm), 6779 c->state, __le16_to_cpu(c->psm),
6783 c->scid, c->dcid, c->imtu, c->omtu, 6780 c->scid, c->dcid, c->imtu, c->omtu,
6784 c->sec_level, c->mode); 6781 c->sec_level, c->mode);
@@ -6811,12 +6808,11 @@ int __init l2cap_init(void)
6811 if (err < 0) 6808 if (err < 0)
6812 return err; 6809 return err;
6813 6810
6814 if (bt_debugfs) { 6811 if (IS_ERR_OR_NULL(bt_debugfs))
6815 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs, 6812 return 0;
6816 NULL, &l2cap_debugfs_fops); 6813
6817 if (!l2cap_debugfs) 6814 l2cap_debugfs = debugfs_create_file("l2cap", 0444, bt_debugfs,
6818 BT_ERR("Failed to create L2CAP debug file"); 6815 NULL, &l2cap_debugfs_fops);
6819 }
6820 6816
6821 return 0; 6817 return 0;
6822} 6818}
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..94d06cbfbc18 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;
@@ -732,11 +732,11 @@ failed:
732 732
733void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst) 733void rfcomm_session_getaddr(struct rfcomm_session *s, bdaddr_t *src, bdaddr_t *dst)
734{ 734{
735 struct sock *sk = s->sock->sk; 735 struct l2cap_chan *chan = l2cap_pi(s->sock->sk)->chan;
736 if (src) 736 if (src)
737 bacpy(src, &bt_sk(sk)->src); 737 bacpy(src, &chan->src);
738 if (dst) 738 if (dst)
739 bacpy(dst, &bt_sk(sk)->dst); 739 bacpy(dst, &chan->dst);
740} 740}
741 741
742/* ---- RFCOMM frame sending ---- */ 742/* ---- RFCOMM frame sending ---- */
@@ -2112,12 +2112,11 @@ static int rfcomm_dlc_debugfs_show(struct seq_file *f, void *x)
2112 rfcomm_lock(); 2112 rfcomm_lock();
2113 2113
2114 list_for_each_entry(s, &session_list, list) { 2114 list_for_each_entry(s, &session_list, list) {
2115 struct l2cap_chan *chan = l2cap_pi(s->sock->sk)->chan;
2115 struct rfcomm_dlc *d; 2116 struct rfcomm_dlc *d;
2116 list_for_each_entry(d, &s->dlcs, list) { 2117 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", 2118 seq_printf(f, "%pMR %pMR %ld %d %d %d %d\n",
2120 &bt_sk(sk)->src, &bt_sk(sk)->dst, 2119 &chan->src, &chan->dst,
2121 d->state, d->dlci, d->mtu, 2120 d->state, d->dlci, d->mtu,
2122 d->rx_credits, d->tx_credits); 2121 d->rx_credits, d->tx_credits);
2123 } 2122 }
@@ -2155,13 +2154,6 @@ static int __init rfcomm_init(void)
2155 goto unregister; 2154 goto unregister;
2156 } 2155 }
2157 2156
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(); 2157 err = rfcomm_init_ttys();
2166 if (err < 0) 2158 if (err < 0)
2167 goto stop; 2159 goto stop;
@@ -2172,6 +2164,13 @@ static int __init rfcomm_init(void)
2172 2164
2173 BT_INFO("RFCOMM ver %s", VERSION); 2165 BT_INFO("RFCOMM ver %s", VERSION);
2174 2166
2167 if (IS_ERR_OR_NULL(bt_debugfs))
2168 return 0;
2169
2170 rfcomm_dlc_debugfs = debugfs_create_file("rfcomm_dlc", 0444,
2171 bt_debugfs, NULL,
2172 &rfcomm_dlc_debugfs_fops);
2173
2175 return 0; 2174 return 0;
2176 2175
2177cleanup: 2176cleanup:
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 30b3721dc6d7..c4d3d423f89b 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;
@@ -946,8 +953,8 @@ int rfcomm_connect_ind(struct rfcomm_session *s, u8 channel, struct rfcomm_dlc *
946 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM); 953 bt_sock_reclassify_lock(sk, BTPROTO_RFCOMM);
947 954
948 rfcomm_sock_init(sk, parent); 955 rfcomm_sock_init(sk, parent);
949 bacpy(&bt_sk(sk)->src, &src); 956 bacpy(&rfcomm_pi(sk)->src, &src);
950 bacpy(&bt_sk(sk)->dst, &dst); 957 bacpy(&rfcomm_pi(sk)->dst, &dst);
951 rfcomm_pi(sk)->channel = channel; 958 rfcomm_pi(sk)->channel = channel;
952 959
953 sk->sk_state = BT_CONFIG; 960 sk->sk_state = BT_CONFIG;
@@ -974,7 +981,7 @@ static int rfcomm_sock_debugfs_show(struct seq_file *f, void *p)
974 981
975 sk_for_each(sk, &rfcomm_sk_list.head) { 982 sk_for_each(sk, &rfcomm_sk_list.head) {
976 seq_printf(f, "%pMR %pMR %d %d\n", 983 seq_printf(f, "%pMR %pMR %d %d\n",
977 &bt_sk(sk)->src, &bt_sk(sk)->dst, 984 &rfcomm_pi(sk)->src, &rfcomm_pi(sk)->dst,
978 sk->sk_state, rfcomm_pi(sk)->channel); 985 sk->sk_state, rfcomm_pi(sk)->channel);
979 } 986 }
980 987
@@ -1044,15 +1051,15 @@ int __init rfcomm_init_sockets(void)
1044 goto error; 1051 goto error;
1045 } 1052 }
1046 1053
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"); 1054 BT_INFO("RFCOMM socket layer initialized");
1055 1055
1056 if (IS_ERR_OR_NULL(bt_debugfs))
1057 return 0;
1058
1059 rfcomm_sock_debugfs = debugfs_create_file("rfcomm", 0444,
1060 bt_debugfs, NULL,
1061 &rfcomm_sock_debugfs_fops);
1062
1056 return 0; 1063 return 0;
1057 1064
1058error: 1065error:
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 96bd388d93a4..12a0e51e21e1 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}
@@ -999,7 +995,7 @@ static void sco_conn_ready(struct sco_conn *conn)
999 } else { 995 } else {
1000 sco_conn_lock(conn); 996 sco_conn_lock(conn);
1001 997
1002 parent = sco_get_sock_listen(conn->src); 998 parent = sco_get_sock_listen(&conn->hcon->src);
1003 if (!parent) { 999 if (!parent) {
1004 sco_conn_unlock(conn); 1000 sco_conn_unlock(conn);
1005 return; 1001 return;
@@ -1017,8 +1013,8 @@ static void sco_conn_ready(struct sco_conn *conn)
1017 1013
1018 sco_sock_init(sk, parent); 1014 sco_sock_init(sk, parent);
1019 1015
1020 bacpy(&bt_sk(sk)->src, conn->src); 1016 bacpy(&sco_pi(sk)->src, &conn->hcon->src);
1021 bacpy(&bt_sk(sk)->dst, conn->dst); 1017 bacpy(&sco_pi(sk)->dst, &conn->hcon->dst);
1022 1018
1023 hci_conn_hold(conn->hcon); 1019 hci_conn_hold(conn->hcon);
1024 __sco_chan_add(conn, sk, parent); 1020 __sco_chan_add(conn, sk, parent);
@@ -1051,8 +1047,8 @@ int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1051 if (sk->sk_state != BT_LISTEN) 1047 if (sk->sk_state != BT_LISTEN)
1052 continue; 1048 continue;
1053 1049
1054 if (!bacmp(&bt_sk(sk)->src, &hdev->bdaddr) || 1050 if (!bacmp(&sco_pi(sk)->src, &hdev->bdaddr) ||
1055 !bacmp(&bt_sk(sk)->src, BDADDR_ANY)) { 1051 !bacmp(&sco_pi(sk)->src, BDADDR_ANY)) {
1056 lm |= HCI_LM_ACCEPT; 1052 lm |= HCI_LM_ACCEPT;
1057 1053
1058 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags)) 1054 if (test_bit(BT_SK_DEFER_SETUP, &bt_sk(sk)->flags))
@@ -1111,8 +1107,8 @@ static int sco_debugfs_show(struct seq_file *f, void *p)
1111 read_lock(&sco_sk_list.lock); 1107 read_lock(&sco_sk_list.lock);
1112 1108
1113 sk_for_each(sk, &sco_sk_list.head) { 1109 sk_for_each(sk, &sco_sk_list.head) {
1114 seq_printf(f, "%pMR %pMR %d\n", &bt_sk(sk)->src, 1110 seq_printf(f, "%pMR %pMR %d\n", &sco_pi(sk)->src,
1115 &bt_sk(sk)->dst, sk->sk_state); 1111 &sco_pi(sk)->dst, sk->sk_state);
1116 } 1112 }
1117 1113
1118 read_unlock(&sco_sk_list.lock); 1114 read_unlock(&sco_sk_list.lock);
@@ -1181,15 +1177,14 @@ int __init sco_init(void)
1181 goto error; 1177 goto error;
1182 } 1178 }
1183 1179
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"); 1180 BT_INFO("SCO socket layer initialized");
1192 1181
1182 if (IS_ERR_OR_NULL(bt_debugfs))
1183 return 0;
1184
1185 sco_debugfs = debugfs_create_file("sco", 0444, bt_debugfs,
1186 NULL, &sco_debugfs_fops);
1187
1193 return 0; 1188 return 0;
1194 1189
1195error: 1190error:
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index b5562abdd6e0..85a2796cac61 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;
@@ -835,9 +838,9 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
835 skb_pull(skb, sizeof(*rp)); 838 skb_pull(skb, sizeof(*rp));
836 839
837 hci_dev_lock(hdev); 840 hci_dev_lock(hdev);
838 authenticated = (conn->hcon->sec_level == BT_SECURITY_HIGH); 841 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
839 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 842 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, 843 authenticated, smp->tk, smp->enc_key_size,
841 rp->ediv, rp->rand); 844 rp->ediv, rp->rand);
842 smp_distribute_keys(conn, 1); 845 smp_distribute_keys(conn, 1);
843 hci_dev_unlock(hdev); 846 hci_dev_unlock(hdev);
@@ -847,16 +850,27 @@ static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
847 850
848int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb) 851int smp_sig_channel(struct l2cap_conn *conn, struct sk_buff *skb)
849{ 852{
850 __u8 code = skb->data[0]; 853 struct hci_conn *hcon = conn->hcon;
851 __u8 reason; 854 __u8 code, reason;
852 int err = 0; 855 int err = 0;
853 856
854 if (!test_bit(HCI_LE_ENABLED, &conn->hcon->hdev->dev_flags)) { 857 if (hcon->type != LE_LINK) {
858 kfree_skb(skb);
859 return 0;
860 }
861
862 if (skb->len < 1) {
863 kfree_skb(skb);
864 return -EILSEQ;
865 }
866
867 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) {
855 err = -ENOTSUPP; 868 err = -ENOTSUPP;
856 reason = SMP_PAIRING_NOTSUPP; 869 reason = SMP_PAIRING_NOTSUPP;
857 goto done; 870 goto done;
858 } 871 }
859 872
873 code = skb->data[0];
860 skb_pull(skb, sizeof(code)); 874 skb_pull(skb, sizeof(code));
861 875
862 /* 876 /*
@@ -974,7 +988,7 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
974 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc); 988 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
975 989
976 authenticated = hcon->sec_level == BT_SECURITY_HIGH; 990 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
977 hci_add_ltk(conn->hcon->hdev, conn->dst, hcon->dst_type, 991 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
978 HCI_SMP_LTK_SLAVE, 1, authenticated, 992 HCI_SMP_LTK_SLAVE, 1, authenticated,
979 enc.ltk, smp->enc_key_size, ediv, ident.rand); 993 enc.ltk, smp->enc_key_size, ediv, ident.rand);
980 994
@@ -996,10 +1010,10 @@ int smp_distribute_keys(struct l2cap_conn *conn, __u8 force)
996 1010
997 /* Just public address */ 1011 /* Just public address */
998 memset(&addrinfo, 0, sizeof(addrinfo)); 1012 memset(&addrinfo, 0, sizeof(addrinfo));
999 bacpy(&addrinfo.bdaddr, conn->src); 1013 bacpy(&addrinfo.bdaddr, &conn->hcon->src);
1000 1014
1001 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo), 1015 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1002 &addrinfo); 1016 &addrinfo);
1003 1017
1004 *keydist &= ~SMP_DIST_ID_KEY; 1018 *keydist &= ~SMP_DIST_ID_KEY;
1005 } 1019 }
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_multicast.c b/net/bridge/br_multicast.c
index 686284ff3d6a..4c214b2b88ef 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
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 2e8244efb262..229d820bdf0b 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,117 +375,99 @@ 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);
444 428
445/* br_ioctl.c */ 429/* br_ioctl.c */
446extern int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd); 430int 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); 431int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
432 void __user *arg);
448 433
449/* br_multicast.c */ 434/* br_multicast.c */
450#ifdef CONFIG_BRIDGE_IGMP_SNOOPING 435#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
451extern unsigned int br_mdb_rehash_seq; 436extern unsigned int br_mdb_rehash_seq;
452extern int br_multicast_rcv(struct net_bridge *br, 437int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
453 struct net_bridge_port *port, 438 struct sk_buff *skb, u16 vid);
454 struct sk_buff *skb, 439struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
455 u16 vid); 440 struct sk_buff *skb, u16 vid);
456extern struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, 441void br_multicast_add_port(struct net_bridge_port *port);
457 struct sk_buff *skb, u16 vid); 442void br_multicast_del_port(struct net_bridge_port *port);
458extern void br_multicast_add_port(struct net_bridge_port *port); 443void br_multicast_enable_port(struct net_bridge_port *port);
459extern void br_multicast_del_port(struct net_bridge_port *port); 444void br_multicast_disable_port(struct net_bridge_port *port);
460extern void br_multicast_enable_port(struct net_bridge_port *port); 445void br_multicast_init(struct net_bridge *br);
461extern void br_multicast_disable_port(struct net_bridge_port *port); 446void br_multicast_open(struct net_bridge *br);
462extern void br_multicast_init(struct net_bridge *br); 447void br_multicast_stop(struct net_bridge *br);
463extern void br_multicast_open(struct net_bridge *br); 448void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
464extern void br_multicast_stop(struct net_bridge *br); 449 struct sk_buff *skb);
465extern void br_multicast_deliver(struct net_bridge_mdb_entry *mdst, 450void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
466 struct sk_buff *skb); 451 struct sk_buff *skb, struct sk_buff *skb2);
467extern void br_multicast_forward(struct net_bridge_mdb_entry *mdst, 452int br_multicast_set_router(struct net_bridge *br, unsigned long val);
468 struct sk_buff *skb, struct sk_buff *skb2); 453int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
469extern int br_multicast_set_router(struct net_bridge *br, unsigned long val); 454int br_multicast_toggle(struct net_bridge *br, unsigned long val);
470extern int br_multicast_set_port_router(struct net_bridge_port *p, 455int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
471 unsigned long val); 456int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
472extern int br_multicast_toggle(struct net_bridge *br, unsigned long val); 457struct net_bridge_mdb_entry *
473extern int br_multicast_set_querier(struct net_bridge *br, unsigned long val); 458br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
474extern int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val); 459struct net_bridge_mdb_entry *
475extern struct net_bridge_mdb_entry *br_mdb_ip_get( 460br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
476 struct net_bridge_mdb_htable *mdb, 461 struct br_ip *group);
477 struct br_ip *dst); 462void br_multicast_free_pg(struct rcu_head *head);
478extern struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, 463struct net_bridge_port_group *
479 struct net_bridge_port *port, struct br_ip *group); 464br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
480extern void br_multicast_free_pg(struct rcu_head *head); 465 struct net_bridge_port_group __rcu *next,
481extern struct net_bridge_port_group *br_multicast_new_port_group( 466 unsigned char state);
482 struct net_bridge_port *port, 467void br_mdb_init(void);
483 struct br_ip *group, 468void br_mdb_uninit(void);
484 struct net_bridge_port_group __rcu *next, 469void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
485 unsigned char state); 470 struct br_ip *group, int type);
486extern void br_mdb_init(void);
487extern void br_mdb_uninit(void);
488extern void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
489 struct br_ip *group, int type);
490 471
491#define mlock_dereference(X, br) \ 472#define mlock_dereference(X, br) \
492 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) 473 rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
@@ -592,22 +573,21 @@ static inline void br_mdb_uninit(void)
592 573
593/* br_vlan.c */ 574/* br_vlan.c */
594#ifdef CONFIG_BRIDGE_VLAN_FILTERING 575#ifdef CONFIG_BRIDGE_VLAN_FILTERING
595extern bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v, 576bool br_allowed_ingress(struct net_bridge *br, struct net_port_vlans *v,
596 struct sk_buff *skb, u16 *vid); 577 struct sk_buff *skb, u16 *vid);
597extern bool br_allowed_egress(struct net_bridge *br, 578bool br_allowed_egress(struct net_bridge *br, const struct net_port_vlans *v,
598 const struct net_port_vlans *v, 579 const struct sk_buff *skb);
599 const struct sk_buff *skb); 580struct sk_buff *br_handle_vlan(struct net_bridge *br,
600extern struct sk_buff *br_handle_vlan(struct net_bridge *br, 581 const struct net_port_vlans *v,
601 const struct net_port_vlans *v, 582 struct sk_buff *skb);
602 struct sk_buff *skb); 583int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags);
603extern int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags); 584int br_vlan_delete(struct net_bridge *br, u16 vid);
604extern int br_vlan_delete(struct net_bridge *br, u16 vid); 585void br_vlan_flush(struct net_bridge *br);
605extern void br_vlan_flush(struct net_bridge *br); 586int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
606extern int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val); 587int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags);
607extern int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags); 588int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
608extern int nbp_vlan_delete(struct net_bridge_port *port, u16 vid); 589void nbp_vlan_flush(struct net_bridge_port *port);
609extern void nbp_vlan_flush(struct net_bridge_port *port); 590bool nbp_vlan_find(struct net_bridge_port *port, u16 vid);
610extern bool nbp_vlan_find(struct net_bridge_port *port, u16 vid);
611 591
612static inline struct net_port_vlans *br_get_vlan_info( 592static inline struct net_port_vlans *br_get_vlan_info(
613 const struct net_bridge *br) 593 const struct net_bridge *br)
@@ -726,9 +706,9 @@ static inline u16 br_get_pvid(const struct net_port_vlans *v)
726 706
727/* br_netfilter.c */ 707/* br_netfilter.c */
728#ifdef CONFIG_BRIDGE_NETFILTER 708#ifdef CONFIG_BRIDGE_NETFILTER
729extern int br_netfilter_init(void); 709int br_netfilter_init(void);
730extern void br_netfilter_fini(void); 710void br_netfilter_fini(void);
731extern void br_netfilter_rtable_init(struct net_bridge *); 711void br_netfilter_rtable_init(struct net_bridge *);
732#else 712#else
733#define br_netfilter_init() (0) 713#define br_netfilter_init() (0)
734#define br_netfilter_fini() do { } while(0) 714#define br_netfilter_fini() do { } while(0)
@@ -736,43 +716,39 @@ extern void br_netfilter_rtable_init(struct net_bridge *);
736#endif 716#endif
737 717
738/* br_stp.c */ 718/* br_stp.c */
739extern void br_log_state(const struct net_bridge_port *p); 719void br_log_state(const struct net_bridge_port *p);
740extern struct net_bridge_port *br_get_port(struct net_bridge *br, 720struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
741 u16 port_no); 721void br_init_port(struct net_bridge_port *p);
742extern void br_init_port(struct net_bridge_port *p); 722void br_become_designated_port(struct net_bridge_port *p);
743extern void br_become_designated_port(struct net_bridge_port *p);
744 723
745extern void __br_set_forward_delay(struct net_bridge *br, unsigned long t); 724void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
746extern int br_set_forward_delay(struct net_bridge *br, unsigned long x); 725int br_set_forward_delay(struct net_bridge *br, unsigned long x);
747extern int br_set_hello_time(struct net_bridge *br, unsigned long x); 726int br_set_hello_time(struct net_bridge *br, unsigned long x);
748extern int br_set_max_age(struct net_bridge *br, unsigned long x); 727int br_set_max_age(struct net_bridge *br, unsigned long x);
749 728
750 729
751/* br_stp_if.c */ 730/* br_stp_if.c */
752extern void br_stp_enable_bridge(struct net_bridge *br); 731void br_stp_enable_bridge(struct net_bridge *br);
753extern void br_stp_disable_bridge(struct net_bridge *br); 732void br_stp_disable_bridge(struct net_bridge *br);
754extern void br_stp_set_enabled(struct net_bridge *br, unsigned long val); 733void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
755extern void br_stp_enable_port(struct net_bridge_port *p); 734void br_stp_enable_port(struct net_bridge_port *p);
756extern void br_stp_disable_port(struct net_bridge_port *p); 735void br_stp_disable_port(struct net_bridge_port *p);
757extern bool br_stp_recalculate_bridge_id(struct net_bridge *br); 736bool br_stp_recalculate_bridge_id(struct net_bridge *br);
758extern void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a); 737void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
759extern void br_stp_set_bridge_priority(struct net_bridge *br, 738void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
760 u16 newprio); 739int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
761extern int br_stp_set_port_priority(struct net_bridge_port *p, 740int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
762 unsigned long newprio); 741ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
763extern int br_stp_set_path_cost(struct net_bridge_port *p,
764 unsigned long path_cost);
765extern ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
766 742
767/* br_stp_bpdu.c */ 743/* br_stp_bpdu.c */
768struct stp_proto; 744struct stp_proto;
769extern void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb, 745void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
770 struct net_device *dev); 746 struct net_device *dev);
771 747
772/* br_stp_timer.c */ 748/* br_stp_timer.c */
773extern void br_stp_timer_init(struct net_bridge *br); 749void br_stp_timer_init(struct net_bridge *br);
774extern void br_stp_port_timer_init(struct net_bridge_port *p); 750void br_stp_port_timer_init(struct net_bridge_port *p);
775extern unsigned long br_timer_value(const struct timer_list *timer); 751unsigned long br_timer_value(const struct timer_list *timer);
776 752
777/* br.c */ 753/* br.c */
778#if IS_ENABLED(CONFIG_ATM_LANE) 754#if IS_ENABLED(CONFIG_ATM_LANE)
@@ -781,23 +757,23 @@ extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr)
781 757
782/* br_netlink.c */ 758/* br_netlink.c */
783extern struct rtnl_link_ops br_link_ops; 759extern struct rtnl_link_ops br_link_ops;
784extern int br_netlink_init(void); 760int br_netlink_init(void);
785extern void br_netlink_fini(void); 761void br_netlink_fini(void);
786extern void br_ifinfo_notify(int event, struct net_bridge_port *port); 762void br_ifinfo_notify(int event, struct net_bridge_port *port);
787extern int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg); 763int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg);
788extern int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg); 764int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg);
789extern int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, 765int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
790 struct net_device *dev, u32 filter_mask); 766 u32 filter_mask);
791 767
792#ifdef CONFIG_SYSFS 768#ifdef CONFIG_SYSFS
793/* br_sysfs_if.c */ 769/* br_sysfs_if.c */
794extern const struct sysfs_ops brport_sysfs_ops; 770extern const struct sysfs_ops brport_sysfs_ops;
795extern int br_sysfs_addif(struct net_bridge_port *p); 771int br_sysfs_addif(struct net_bridge_port *p);
796extern int br_sysfs_renameif(struct net_bridge_port *p); 772int br_sysfs_renameif(struct net_bridge_port *p);
797 773
798/* br_sysfs_br.c */ 774/* br_sysfs_br.c */
799extern int br_sysfs_addbr(struct net_device *dev); 775int br_sysfs_addbr(struct net_device *dev);
800extern void br_sysfs_delbr(struct net_device *dev); 776void br_sysfs_delbr(struct net_device *dev);
801 777
802#else 778#else
803 779
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/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/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/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.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/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..8ffc52e01ece 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -1203,7 +1203,7 @@ void netdev_state_change(struct net_device *dev)
1203{ 1203{
1204 if (dev->flags & IFF_UP) { 1204 if (dev->flags & IFF_UP) {
1205 call_netdevice_notifiers(NETDEV_CHANGE, dev); 1205 call_netdevice_notifiers(NETDEV_CHANGE, dev);
1206 rtmsg_ifinfo(RTM_NEWLINK, dev, 0); 1206 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1207 } 1207 }
1208} 1208}
1209EXPORT_SYMBOL(netdev_state_change); 1209EXPORT_SYMBOL(netdev_state_change);
@@ -1293,7 +1293,7 @@ int dev_open(struct net_device *dev)
1293 if (ret < 0) 1293 if (ret < 0)
1294 return ret; 1294 return ret;
1295 1295
1296 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 1296 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1297 call_netdevice_notifiers(NETDEV_UP, dev); 1297 call_netdevice_notifiers(NETDEV_UP, dev);
1298 1298
1299 return ret; 1299 return ret;
@@ -1307,7 +1307,7 @@ static int __dev_close_many(struct list_head *head)
1307 ASSERT_RTNL(); 1307 ASSERT_RTNL();
1308 might_sleep(); 1308 might_sleep();
1309 1309
1310 list_for_each_entry(dev, head, unreg_list) { 1310 list_for_each_entry(dev, head, close_list) {
1311 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); 1311 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1312 1312
1313 clear_bit(__LINK_STATE_START, &dev->state); 1313 clear_bit(__LINK_STATE_START, &dev->state);
@@ -1323,7 +1323,7 @@ static int __dev_close_many(struct list_head *head)
1323 1323
1324 dev_deactivate_many(head); 1324 dev_deactivate_many(head);
1325 1325
1326 list_for_each_entry(dev, head, unreg_list) { 1326 list_for_each_entry(dev, head, close_list) {
1327 const struct net_device_ops *ops = dev->netdev_ops; 1327 const struct net_device_ops *ops = dev->netdev_ops;
1328 1328
1329 /* 1329 /*
@@ -1351,7 +1351,7 @@ static int __dev_close(struct net_device *dev)
1351 /* Temporarily disable netpoll until the interface is down */ 1351 /* Temporarily disable netpoll until the interface is down */
1352 netpoll_rx_disable(dev); 1352 netpoll_rx_disable(dev);
1353 1353
1354 list_add(&dev->unreg_list, &single); 1354 list_add(&dev->close_list, &single);
1355 retval = __dev_close_many(&single); 1355 retval = __dev_close_many(&single);
1356 list_del(&single); 1356 list_del(&single);
1357 1357
@@ -1362,21 +1362,20 @@ static int __dev_close(struct net_device *dev)
1362static int dev_close_many(struct list_head *head) 1362static int dev_close_many(struct list_head *head)
1363{ 1363{
1364 struct net_device *dev, *tmp; 1364 struct net_device *dev, *tmp;
1365 LIST_HEAD(tmp_list);
1366 1365
1367 list_for_each_entry_safe(dev, tmp, head, unreg_list) 1366 /* Remove the devices that don't need to be closed */
1367 list_for_each_entry_safe(dev, tmp, head, close_list)
1368 if (!(dev->flags & IFF_UP)) 1368 if (!(dev->flags & IFF_UP))
1369 list_move(&dev->unreg_list, &tmp_list); 1369 list_del_init(&dev->close_list);
1370 1370
1371 __dev_close_many(head); 1371 __dev_close_many(head);
1372 1372
1373 list_for_each_entry(dev, head, unreg_list) { 1373 list_for_each_entry_safe(dev, tmp, head, close_list) {
1374 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); 1374 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1375 call_netdevice_notifiers(NETDEV_DOWN, dev); 1375 call_netdevice_notifiers(NETDEV_DOWN, dev);
1376 list_del_init(&dev->close_list);
1376 } 1377 }
1377 1378
1378 /* rollback_registered_many needs the complete original list */
1379 list_splice(&tmp_list, head);
1380 return 0; 1379 return 0;
1381} 1380}
1382 1381
@@ -1397,7 +1396,7 @@ int dev_close(struct net_device *dev)
1397 /* Block netpoll rx while the interface is going down */ 1396 /* Block netpoll rx while the interface is going down */
1398 netpoll_rx_disable(dev); 1397 netpoll_rx_disable(dev);
1399 1398
1400 list_add(&dev->unreg_list, &single); 1399 list_add(&dev->close_list, &single);
1401 dev_close_many(&single); 1400 dev_close_many(&single);
1402 list_del(&single); 1401 list_del(&single);
1403 1402
@@ -2378,6 +2377,8 @@ struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2378 } 2377 }
2379 2378
2380 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); 2379 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
2380 SKB_GSO_CB(skb)->encap_level = 0;
2381
2381 skb_reset_mac_header(skb); 2382 skb_reset_mac_header(skb);
2382 skb_reset_mac_len(skb); 2383 skb_reset_mac_len(skb);
2383 2384
@@ -2537,7 +2538,7 @@ static inline int skb_needs_linearize(struct sk_buff *skb,
2537} 2538}
2538 2539
2539int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, 2540int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2540 struct netdev_queue *txq) 2541 struct netdev_queue *txq, void *accel_priv)
2541{ 2542{
2542 const struct net_device_ops *ops = dev->netdev_ops; 2543 const struct net_device_ops *ops = dev->netdev_ops;
2543 int rc = NETDEV_TX_OK; 2544 int rc = NETDEV_TX_OK;
@@ -2603,9 +2604,13 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev,
2603 dev_queue_xmit_nit(skb, dev); 2604 dev_queue_xmit_nit(skb, dev);
2604 2605
2605 skb_len = skb->len; 2606 skb_len = skb->len;
2606 rc = ops->ndo_start_xmit(skb, dev); 2607 if (accel_priv)
2608 rc = ops->ndo_dfwd_start_xmit(skb, dev, accel_priv);
2609 else
2610 rc = ops->ndo_start_xmit(skb, dev);
2611
2607 trace_net_dev_xmit(skb, rc, dev, skb_len); 2612 trace_net_dev_xmit(skb, rc, dev, skb_len);
2608 if (rc == NETDEV_TX_OK) 2613 if (rc == NETDEV_TX_OK && txq)
2609 txq_trans_update(txq); 2614 txq_trans_update(txq);
2610 return rc; 2615 return rc;
2611 } 2616 }
@@ -2621,7 +2626,10 @@ gso:
2621 dev_queue_xmit_nit(nskb, dev); 2626 dev_queue_xmit_nit(nskb, dev);
2622 2627
2623 skb_len = nskb->len; 2628 skb_len = nskb->len;
2624 rc = ops->ndo_start_xmit(nskb, dev); 2629 if (accel_priv)
2630 rc = ops->ndo_dfwd_start_xmit(nskb, dev, accel_priv);
2631 else
2632 rc = ops->ndo_start_xmit(nskb, dev);
2625 trace_net_dev_xmit(nskb, rc, dev, skb_len); 2633 trace_net_dev_xmit(nskb, rc, dev, skb_len);
2626 if (unlikely(rc != NETDEV_TX_OK)) { 2634 if (unlikely(rc != NETDEV_TX_OK)) {
2627 if (rc & ~NETDEV_TX_MASK) 2635 if (rc & ~NETDEV_TX_MASK)
@@ -2646,6 +2654,7 @@ out_kfree_skb:
2646out: 2654out:
2647 return rc; 2655 return rc;
2648} 2656}
2657EXPORT_SYMBOL_GPL(dev_hard_start_xmit);
2649 2658
2650static void qdisc_pkt_len_init(struct sk_buff *skb) 2659static void qdisc_pkt_len_init(struct sk_buff *skb)
2651{ 2660{
@@ -2853,7 +2862,7 @@ int dev_queue_xmit(struct sk_buff *skb)
2853 2862
2854 if (!netif_xmit_stopped(txq)) { 2863 if (!netif_xmit_stopped(txq)) {
2855 __this_cpu_inc(xmit_recursion); 2864 __this_cpu_inc(xmit_recursion);
2856 rc = dev_hard_start_xmit(skb, dev, txq); 2865 rc = dev_hard_start_xmit(skb, dev, txq, NULL);
2857 __this_cpu_dec(xmit_recursion); 2866 __this_cpu_dec(xmit_recursion);
2858 if (dev_xmit_complete(rc)) { 2867 if (dev_xmit_complete(rc)) {
2859 HARD_TX_UNLOCK(dev, txq); 2868 HARD_TX_UNLOCK(dev, txq);
@@ -4374,42 +4383,40 @@ struct netdev_adjacent {
4374 /* upper master flag, there can only be one master device per list */ 4383 /* upper master flag, there can only be one master device per list */
4375 bool master; 4384 bool master;
4376 4385
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 */ 4386 /* counter for the number of times this device was added to us */
4381 u16 ref_nr; 4387 u16 ref_nr;
4382 4388
4389 /* private field for the users */
4390 void *private;
4391
4383 struct list_head list; 4392 struct list_head list;
4384 struct rcu_head rcu; 4393 struct rcu_head rcu;
4385}; 4394};
4386 4395
4387static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev, 4396static struct netdev_adjacent *__netdev_find_adj_rcu(struct net_device *dev,
4388 struct net_device *adj_dev, 4397 struct net_device *adj_dev,
4389 bool upper) 4398 struct list_head *adj_list)
4390{ 4399{
4391 struct netdev_adjacent *adj; 4400 struct netdev_adjacent *adj;
4392 struct list_head *dev_list;
4393
4394 dev_list = upper ? &dev->upper_dev_list : &dev->lower_dev_list;
4395 4401
4396 list_for_each_entry(adj, dev_list, list) { 4402 list_for_each_entry_rcu(adj, adj_list, list) {
4397 if (adj->dev == adj_dev) 4403 if (adj->dev == adj_dev)
4398 return adj; 4404 return adj;
4399 } 4405 }
4400 return NULL; 4406 return NULL;
4401} 4407}
4402 4408
4403static inline struct netdev_adjacent *__netdev_find_upper(struct net_device *dev, 4409static struct netdev_adjacent *__netdev_find_adj(struct net_device *dev,
4404 struct net_device *udev) 4410 struct net_device *adj_dev,
4411 struct list_head *adj_list)
4405{ 4412{
4406 return __netdev_find_adj(dev, udev, true); 4413 struct netdev_adjacent *adj;
4407}
4408 4414
4409static inline struct netdev_adjacent *__netdev_find_lower(struct net_device *dev, 4415 list_for_each_entry(adj, adj_list, list) {
4410 struct net_device *ldev) 4416 if (adj->dev == adj_dev)
4411{ 4417 return adj;
4412 return __netdev_find_adj(dev, ldev, false); 4418 }
4419 return NULL;
4413} 4420}
4414 4421
4415/** 4422/**
@@ -4426,7 +4433,7 @@ bool netdev_has_upper_dev(struct net_device *dev,
4426{ 4433{
4427 ASSERT_RTNL(); 4434 ASSERT_RTNL();
4428 4435
4429 return __netdev_find_upper(dev, upper_dev); 4436 return __netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper);
4430} 4437}
4431EXPORT_SYMBOL(netdev_has_upper_dev); 4438EXPORT_SYMBOL(netdev_has_upper_dev);
4432 4439
@@ -4441,7 +4448,7 @@ bool netdev_has_any_upper_dev(struct net_device *dev)
4441{ 4448{
4442 ASSERT_RTNL(); 4449 ASSERT_RTNL();
4443 4450
4444 return !list_empty(&dev->upper_dev_list); 4451 return !list_empty(&dev->all_adj_list.upper);
4445} 4452}
4446EXPORT_SYMBOL(netdev_has_any_upper_dev); 4453EXPORT_SYMBOL(netdev_has_any_upper_dev);
4447 4454
@@ -4458,10 +4465,10 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4458 4465
4459 ASSERT_RTNL(); 4466 ASSERT_RTNL();
4460 4467
4461 if (list_empty(&dev->upper_dev_list)) 4468 if (list_empty(&dev->adj_list.upper))
4462 return NULL; 4469 return NULL;
4463 4470
4464 upper = list_first_entry(&dev->upper_dev_list, 4471 upper = list_first_entry(&dev->adj_list.upper,
4465 struct netdev_adjacent, list); 4472 struct netdev_adjacent, list);
4466 if (likely(upper->master)) 4473 if (likely(upper->master))
4467 return upper->dev; 4474 return upper->dev;
@@ -4469,15 +4476,26 @@ struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
4469} 4476}
4470EXPORT_SYMBOL(netdev_master_upper_dev_get); 4477EXPORT_SYMBOL(netdev_master_upper_dev_get);
4471 4478
4472/* netdev_upper_get_next_dev_rcu - Get the next dev from upper list 4479void *netdev_adjacent_get_private(struct list_head *adj_list)
4480{
4481 struct netdev_adjacent *adj;
4482
4483 adj = list_entry(adj_list, struct netdev_adjacent, list);
4484
4485 return adj->private;
4486}
4487EXPORT_SYMBOL(netdev_adjacent_get_private);
4488
4489/**
4490 * netdev_all_upper_get_next_dev_rcu - Get the next dev from upper list
4473 * @dev: device 4491 * @dev: device
4474 * @iter: list_head ** of the current position 4492 * @iter: list_head ** of the current position
4475 * 4493 *
4476 * Gets the next device from the dev's upper list, starting from iter 4494 * Gets the next device from the dev's upper list, starting from iter
4477 * position. The caller must hold RCU read lock. 4495 * position. The caller must hold RCU read lock.
4478 */ 4496 */
4479struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, 4497struct net_device *netdev_all_upper_get_next_dev_rcu(struct net_device *dev,
4480 struct list_head **iter) 4498 struct list_head **iter)
4481{ 4499{
4482 struct netdev_adjacent *upper; 4500 struct netdev_adjacent *upper;
4483 4501
@@ -4485,14 +4503,71 @@ struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
4485 4503
4486 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); 4504 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4487 4505
4488 if (&upper->list == &dev->upper_dev_list) 4506 if (&upper->list == &dev->all_adj_list.upper)
4489 return NULL; 4507 return NULL;
4490 4508
4491 *iter = &upper->list; 4509 *iter = &upper->list;
4492 4510
4493 return upper->dev; 4511 return upper->dev;
4494} 4512}
4495EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); 4513EXPORT_SYMBOL(netdev_all_upper_get_next_dev_rcu);
4514
4515/**
4516 * netdev_lower_get_next_private - Get the next ->private from the
4517 * lower neighbour list
4518 * @dev: device
4519 * @iter: list_head ** of the current position
4520 *
4521 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4522 * list, starting from iter position. The caller must hold either hold the
4523 * RTNL lock or its own locking that guarantees that the neighbour lower
4524 * list will remain unchainged.
4525 */
4526void *netdev_lower_get_next_private(struct net_device *dev,
4527 struct list_head **iter)
4528{
4529 struct netdev_adjacent *lower;
4530
4531 lower = list_entry(*iter, struct netdev_adjacent, list);
4532
4533 if (&lower->list == &dev->adj_list.lower)
4534 return NULL;
4535
4536 if (iter)
4537 *iter = lower->list.next;
4538
4539 return lower->private;
4540}
4541EXPORT_SYMBOL(netdev_lower_get_next_private);
4542
4543/**
4544 * netdev_lower_get_next_private_rcu - Get the next ->private from the
4545 * lower neighbour list, RCU
4546 * variant
4547 * @dev: device
4548 * @iter: list_head ** of the current position
4549 *
4550 * Gets the next netdev_adjacent->private from the dev's lower neighbour
4551 * list, starting from iter position. The caller must hold RCU read lock.
4552 */
4553void *netdev_lower_get_next_private_rcu(struct net_device *dev,
4554 struct list_head **iter)
4555{
4556 struct netdev_adjacent *lower;
4557
4558 WARN_ON_ONCE(!rcu_read_lock_held());
4559
4560 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
4561
4562 if (&lower->list == &dev->adj_list.lower)
4563 return NULL;
4564
4565 if (iter)
4566 *iter = &lower->list;
4567
4568 return lower->private;
4569}
4570EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
4496 4571
4497/** 4572/**
4498 * netdev_master_upper_dev_get_rcu - Get master upper device 4573 * netdev_master_upper_dev_get_rcu - Get master upper device
@@ -4505,7 +4580,7 @@ struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
4505{ 4580{
4506 struct netdev_adjacent *upper; 4581 struct netdev_adjacent *upper;
4507 4582
4508 upper = list_first_or_null_rcu(&dev->upper_dev_list, 4583 upper = list_first_or_null_rcu(&dev->adj_list.upper,
4509 struct netdev_adjacent, list); 4584 struct netdev_adjacent, list);
4510 if (upper && likely(upper->master)) 4585 if (upper && likely(upper->master))
4511 return upper->dev; 4586 return upper->dev;
@@ -4515,15 +4590,16 @@ EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
4515 4590
4516static int __netdev_adjacent_dev_insert(struct net_device *dev, 4591static int __netdev_adjacent_dev_insert(struct net_device *dev,
4517 struct net_device *adj_dev, 4592 struct net_device *adj_dev,
4518 bool neighbour, bool master, 4593 struct list_head *dev_list,
4519 bool upper) 4594 void *private, bool master)
4520{ 4595{
4521 struct netdev_adjacent *adj; 4596 struct netdev_adjacent *adj;
4597 char linkname[IFNAMSIZ+7];
4598 int ret;
4522 4599
4523 adj = __netdev_find_adj(dev, adj_dev, upper); 4600 adj = __netdev_find_adj(dev, adj_dev, dev_list);
4524 4601
4525 if (adj) { 4602 if (adj) {
4526 BUG_ON(neighbour);
4527 adj->ref_nr++; 4603 adj->ref_nr++;
4528 return 0; 4604 return 0;
4529 } 4605 }
@@ -4534,124 +4610,179 @@ static int __netdev_adjacent_dev_insert(struct net_device *dev,
4534 4610
4535 adj->dev = adj_dev; 4611 adj->dev = adj_dev;
4536 adj->master = master; 4612 adj->master = master;
4537 adj->neighbour = neighbour;
4538 adj->ref_nr = 1; 4613 adj->ref_nr = 1;
4539 4614 adj->private = private;
4540 dev_hold(adj_dev); 4615 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 4616
4545 if (!upper) { 4617 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); 4618 adj_dev->name, dev->name, adj_dev->name);
4547 return 0; 4619
4620 if (dev_list == &dev->adj_list.lower) {
4621 sprintf(linkname, "lower_%s", adj_dev->name);
4622 ret = sysfs_create_link(&(dev->dev.kobj),
4623 &(adj_dev->dev.kobj), linkname);
4624 if (ret)
4625 goto free_adj;
4626 } else if (dev_list == &dev->adj_list.upper) {
4627 sprintf(linkname, "upper_%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;
4548 } 4632 }
4549 4633
4550 /* Ensure that master upper link is always the first item in list. */ 4634 /* Ensure that master link is always the first item in list. */
4551 if (master) 4635 if (master) {
4552 list_add_rcu(&adj->list, &dev->upper_dev_list); 4636 ret = sysfs_create_link(&(dev->dev.kobj),
4553 else 4637 &(adj_dev->dev.kobj), "master");
4554 list_add_tail_rcu(&adj->list, &dev->upper_dev_list); 4638 if (ret)
4639 goto remove_symlinks;
4640
4641 list_add_rcu(&adj->list, dev_list);
4642 } else {
4643 list_add_tail_rcu(&adj->list, dev_list);
4644 }
4555 4645
4556 return 0; 4646 return 0;
4557}
4558 4647
4559static inline int __netdev_upper_dev_insert(struct net_device *dev, 4648remove_symlinks:
4560 struct net_device *udev, 4649 if (dev_list == &dev->adj_list.lower) {
4561 bool master, bool neighbour) 4650 sprintf(linkname, "lower_%s", adj_dev->name);
4562{ 4651 sysfs_remove_link(&(dev->dev.kobj), linkname);
4563 return __netdev_adjacent_dev_insert(dev, udev, neighbour, master, 4652 } else if (dev_list == &dev->adj_list.upper) {
4564 true); 4653 sprintf(linkname, "upper_%s", adj_dev->name);
4565} 4654 sysfs_remove_link(&(dev->dev.kobj), linkname);
4655 }
4566 4656
4567static inline int __netdev_lower_dev_insert(struct net_device *dev, 4657free_adj:
4568 struct net_device *ldev, 4658 kfree(adj);
4569 bool neighbour) 4659 dev_put(adj_dev);
4570{ 4660
4571 return __netdev_adjacent_dev_insert(dev, ldev, neighbour, false, 4661 return ret;
4572 false);
4573} 4662}
4574 4663
4575void __netdev_adjacent_dev_remove(struct net_device *dev, 4664void __netdev_adjacent_dev_remove(struct net_device *dev,
4576 struct net_device *adj_dev, bool upper) 4665 struct net_device *adj_dev,
4666 struct list_head *dev_list)
4577{ 4667{
4578 struct netdev_adjacent *adj; 4668 struct netdev_adjacent *adj;
4669 char linkname[IFNAMSIZ+7];
4579 4670
4580 if (upper) 4671 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 4672
4585 if (!adj) 4673 if (!adj) {
4674 pr_err("tried to remove device %s from %s\n",
4675 dev->name, adj_dev->name);
4586 BUG(); 4676 BUG();
4677 }
4587 4678
4588 if (adj->ref_nr > 1) { 4679 if (adj->ref_nr > 1) {
4680 pr_debug("%s to %s ref_nr-- = %d\n", dev->name, adj_dev->name,
4681 adj->ref_nr-1);
4589 adj->ref_nr--; 4682 adj->ref_nr--;
4590 return; 4683 return;
4591 } 4684 }
4592 4685
4686 if (adj->master)
4687 sysfs_remove_link(&(dev->dev.kobj), "master");
4688
4689 if (dev_list == &dev->adj_list.lower) {
4690 sprintf(linkname, "lower_%s", adj_dev->name);
4691 sysfs_remove_link(&(dev->dev.kobj), linkname);
4692 } else if (dev_list == &dev->adj_list.upper) {
4693 sprintf(linkname, "upper_%s", adj_dev->name);
4694 sysfs_remove_link(&(dev->dev.kobj), linkname);
4695 }
4696
4593 list_del_rcu(&adj->list); 4697 list_del_rcu(&adj->list);
4594 pr_debug("dev_put for %s, because of %s link removed from %s to %s\n", 4698 pr_debug("dev_put for %s, because link removed from %s to %s\n",
4595 adj_dev->name, upper ? "upper" : "lower", dev->name, 4699 adj_dev->name, dev->name, adj_dev->name);
4596 adj_dev->name);
4597 dev_put(adj_dev); 4700 dev_put(adj_dev);
4598 kfree_rcu(adj, rcu); 4701 kfree_rcu(adj, rcu);
4599} 4702}
4600 4703
4601static inline void __netdev_upper_dev_remove(struct net_device *dev, 4704int __netdev_adjacent_dev_link_lists(struct net_device *dev,
4602 struct net_device *udev) 4705 struct net_device *upper_dev,
4603{ 4706 struct list_head *up_list,
4604 return __netdev_adjacent_dev_remove(dev, udev, true); 4707 struct list_head *down_list,
4605} 4708 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{ 4709{
4617 int ret; 4710 int ret;
4618 4711
4619 ret = __netdev_upper_dev_insert(dev, upper_dev, master, neighbour); 4712 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, private,
4713 master);
4620 if (ret) 4714 if (ret)
4621 return ret; 4715 return ret;
4622 4716
4623 ret = __netdev_lower_dev_insert(upper_dev, dev, neighbour); 4717 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, private,
4718 false);
4624 if (ret) { 4719 if (ret) {
4625 __netdev_upper_dev_remove(dev, upper_dev); 4720 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4626 return ret; 4721 return ret;
4627 } 4722 }
4628 4723
4629 return 0; 4724 return 0;
4630} 4725}
4631 4726
4632static inline int __netdev_adjacent_dev_link(struct net_device *dev, 4727int __netdev_adjacent_dev_link(struct net_device *dev,
4633 struct net_device *udev) 4728 struct net_device *upper_dev)
4634{ 4729{
4635 return __netdev_adjacent_dev_insert_link(dev, udev, false, false); 4730 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
4731 &dev->all_adj_list.upper,
4732 &upper_dev->all_adj_list.lower,
4733 NULL, false);
4636} 4734}
4637 4735
4638static inline int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, 4736void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
4639 struct net_device *udev, 4737 struct net_device *upper_dev,
4640 bool master) 4738 struct list_head *up_list,
4739 struct list_head *down_list)
4641{ 4740{
4642 return __netdev_adjacent_dev_insert_link(dev, udev, master, true); 4741 __netdev_adjacent_dev_remove(dev, upper_dev, up_list);
4742 __netdev_adjacent_dev_remove(upper_dev, dev, down_list);
4643} 4743}
4644 4744
4645void __netdev_adjacent_dev_unlink(struct net_device *dev, 4745void __netdev_adjacent_dev_unlink(struct net_device *dev,
4646 struct net_device *upper_dev) 4746 struct net_device *upper_dev)
4647{ 4747{
4648 __netdev_upper_dev_remove(dev, upper_dev); 4748 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4649 __netdev_lower_dev_remove(upper_dev, dev); 4749 &dev->all_adj_list.upper,
4750 &upper_dev->all_adj_list.lower);
4650} 4751}
4651 4752
4753int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
4754 struct net_device *upper_dev,
4755 void *private, bool master)
4756{
4757 int ret = __netdev_adjacent_dev_link(dev, upper_dev);
4758
4759 if (ret)
4760 return ret;
4761
4762 ret = __netdev_adjacent_dev_link_lists(dev, upper_dev,
4763 &dev->adj_list.upper,
4764 &upper_dev->adj_list.lower,
4765 private, master);
4766 if (ret) {
4767 __netdev_adjacent_dev_unlink(dev, upper_dev);
4768 return ret;
4769 }
4770
4771 return 0;
4772}
4773
4774void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
4775 struct net_device *upper_dev)
4776{
4777 __netdev_adjacent_dev_unlink(dev, upper_dev);
4778 __netdev_adjacent_dev_unlink_lists(dev, upper_dev,
4779 &dev->adj_list.upper,
4780 &upper_dev->adj_list.lower);
4781}
4652 4782
4653static int __netdev_upper_dev_link(struct net_device *dev, 4783static int __netdev_upper_dev_link(struct net_device *dev,
4654 struct net_device *upper_dev, bool master) 4784 struct net_device *upper_dev, bool master,
4785 void *private)
4655{ 4786{
4656 struct netdev_adjacent *i, *j, *to_i, *to_j; 4787 struct netdev_adjacent *i, *j, *to_i, *to_j;
4657 int ret = 0; 4788 int ret = 0;
@@ -4662,26 +4793,29 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4662 return -EBUSY; 4793 return -EBUSY;
4663 4794
4664 /* To prevent loops, check if dev is not upper device to upper_dev. */ 4795 /* To prevent loops, check if dev is not upper device to upper_dev. */
4665 if (__netdev_find_upper(upper_dev, dev)) 4796 if (__netdev_find_adj(upper_dev, dev, &upper_dev->all_adj_list.upper))
4666 return -EBUSY; 4797 return -EBUSY;
4667 4798
4668 if (__netdev_find_upper(dev, upper_dev)) 4799 if (__netdev_find_adj(dev, upper_dev, &dev->all_adj_list.upper))
4669 return -EEXIST; 4800 return -EEXIST;
4670 4801
4671 if (master && netdev_master_upper_dev_get(dev)) 4802 if (master && netdev_master_upper_dev_get(dev))
4672 return -EBUSY; 4803 return -EBUSY;
4673 4804
4674 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, master); 4805 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, private,
4806 master);
4675 if (ret) 4807 if (ret)
4676 return ret; 4808 return ret;
4677 4809
4678 /* Now that we linked these devs, make all the upper_dev's 4810 /* 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 4811 * 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 4812 * versa, and don't forget the devices itself. All of these
4681 * links are non-neighbours. 4813 * links are non-neighbours.
4682 */ 4814 */
4683 list_for_each_entry(i, &dev->lower_dev_list, list) { 4815 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4684 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4816 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4817 pr_debug("Interlinking %s with %s, non-neighbour\n",
4818 i->dev->name, j->dev->name);
4685 ret = __netdev_adjacent_dev_link(i->dev, j->dev); 4819 ret = __netdev_adjacent_dev_link(i->dev, j->dev);
4686 if (ret) 4820 if (ret)
4687 goto rollback_mesh; 4821 goto rollback_mesh;
@@ -4689,14 +4823,18 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4689 } 4823 }
4690 4824
4691 /* add dev to every upper_dev's upper device */ 4825 /* add dev to every upper_dev's upper device */
4692 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4826 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4827 pr_debug("linking %s's upper device %s with %s\n",
4828 upper_dev->name, i->dev->name, dev->name);
4693 ret = __netdev_adjacent_dev_link(dev, i->dev); 4829 ret = __netdev_adjacent_dev_link(dev, i->dev);
4694 if (ret) 4830 if (ret)
4695 goto rollback_upper_mesh; 4831 goto rollback_upper_mesh;
4696 } 4832 }
4697 4833
4698 /* add upper_dev to every dev's lower device */ 4834 /* add upper_dev to every dev's lower device */
4699 list_for_each_entry(i, &dev->lower_dev_list, list) { 4835 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4836 pr_debug("linking %s's lower device %s with %s\n", dev->name,
4837 i->dev->name, upper_dev->name);
4700 ret = __netdev_adjacent_dev_link(i->dev, upper_dev); 4838 ret = __netdev_adjacent_dev_link(i->dev, upper_dev);
4701 if (ret) 4839 if (ret)
4702 goto rollback_lower_mesh; 4840 goto rollback_lower_mesh;
@@ -4707,7 +4845,7 @@ static int __netdev_upper_dev_link(struct net_device *dev,
4707 4845
4708rollback_lower_mesh: 4846rollback_lower_mesh:
4709 to_i = i; 4847 to_i = i;
4710 list_for_each_entry(i, &dev->lower_dev_list, list) { 4848 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4711 if (i == to_i) 4849 if (i == to_i)
4712 break; 4850 break;
4713 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4851 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
@@ -4717,7 +4855,7 @@ rollback_lower_mesh:
4717 4855
4718rollback_upper_mesh: 4856rollback_upper_mesh:
4719 to_i = i; 4857 to_i = i;
4720 list_for_each_entry(i, &upper_dev->upper_dev_list, list) { 4858 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list) {
4721 if (i == to_i) 4859 if (i == to_i)
4722 break; 4860 break;
4723 __netdev_adjacent_dev_unlink(dev, i->dev); 4861 __netdev_adjacent_dev_unlink(dev, i->dev);
@@ -4728,8 +4866,8 @@ rollback_upper_mesh:
4728rollback_mesh: 4866rollback_mesh:
4729 to_i = i; 4867 to_i = i;
4730 to_j = j; 4868 to_j = j;
4731 list_for_each_entry(i, &dev->lower_dev_list, list) { 4869 list_for_each_entry(i, &dev->all_adj_list.lower, list) {
4732 list_for_each_entry(j, &upper_dev->upper_dev_list, list) { 4870 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list) {
4733 if (i == to_i && j == to_j) 4871 if (i == to_i && j == to_j)
4734 break; 4872 break;
4735 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4873 __netdev_adjacent_dev_unlink(i->dev, j->dev);
@@ -4738,7 +4876,7 @@ rollback_mesh:
4738 break; 4876 break;
4739 } 4877 }
4740 4878
4741 __netdev_adjacent_dev_unlink(dev, upper_dev); 4879 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4742 4880
4743 return ret; 4881 return ret;
4744} 4882}
@@ -4756,7 +4894,7 @@ rollback_mesh:
4756int netdev_upper_dev_link(struct net_device *dev, 4894int netdev_upper_dev_link(struct net_device *dev,
4757 struct net_device *upper_dev) 4895 struct net_device *upper_dev)
4758{ 4896{
4759 return __netdev_upper_dev_link(dev, upper_dev, false); 4897 return __netdev_upper_dev_link(dev, upper_dev, false, NULL);
4760} 4898}
4761EXPORT_SYMBOL(netdev_upper_dev_link); 4899EXPORT_SYMBOL(netdev_upper_dev_link);
4762 4900
@@ -4774,10 +4912,18 @@ EXPORT_SYMBOL(netdev_upper_dev_link);
4774int netdev_master_upper_dev_link(struct net_device *dev, 4912int netdev_master_upper_dev_link(struct net_device *dev,
4775 struct net_device *upper_dev) 4913 struct net_device *upper_dev)
4776{ 4914{
4777 return __netdev_upper_dev_link(dev, upper_dev, true); 4915 return __netdev_upper_dev_link(dev, upper_dev, true, NULL);
4778} 4916}
4779EXPORT_SYMBOL(netdev_master_upper_dev_link); 4917EXPORT_SYMBOL(netdev_master_upper_dev_link);
4780 4918
4919int netdev_master_upper_dev_link_private(struct net_device *dev,
4920 struct net_device *upper_dev,
4921 void *private)
4922{
4923 return __netdev_upper_dev_link(dev, upper_dev, true, private);
4924}
4925EXPORT_SYMBOL(netdev_master_upper_dev_link_private);
4926
4781/** 4927/**
4782 * netdev_upper_dev_unlink - Removes a link to upper device 4928 * netdev_upper_dev_unlink - Removes a link to upper device
4783 * @dev: device 4929 * @dev: device
@@ -4792,29 +4938,59 @@ void netdev_upper_dev_unlink(struct net_device *dev,
4792 struct netdev_adjacent *i, *j; 4938 struct netdev_adjacent *i, *j;
4793 ASSERT_RTNL(); 4939 ASSERT_RTNL();
4794 4940
4795 __netdev_adjacent_dev_unlink(dev, upper_dev); 4941 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
4796 4942
4797 /* Here is the tricky part. We must remove all dev's lower 4943 /* Here is the tricky part. We must remove all dev's lower
4798 * devices from all upper_dev's upper devices and vice 4944 * devices from all upper_dev's upper devices and vice
4799 * versa, to maintain the graph relationship. 4945 * versa, to maintain the graph relationship.
4800 */ 4946 */
4801 list_for_each_entry(i, &dev->lower_dev_list, list) 4947 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4802 list_for_each_entry(j, &upper_dev->upper_dev_list, list) 4948 list_for_each_entry(j, &upper_dev->all_adj_list.upper, list)
4803 __netdev_adjacent_dev_unlink(i->dev, j->dev); 4949 __netdev_adjacent_dev_unlink(i->dev, j->dev);
4804 4950
4805 /* remove also the devices itself from lower/upper device 4951 /* remove also the devices itself from lower/upper device
4806 * list 4952 * list
4807 */ 4953 */
4808 list_for_each_entry(i, &dev->lower_dev_list, list) 4954 list_for_each_entry(i, &dev->all_adj_list.lower, list)
4809 __netdev_adjacent_dev_unlink(i->dev, upper_dev); 4955 __netdev_adjacent_dev_unlink(i->dev, upper_dev);
4810 4956
4811 list_for_each_entry(i, &upper_dev->upper_dev_list, list) 4957 list_for_each_entry(i, &upper_dev->all_adj_list.upper, list)
4812 __netdev_adjacent_dev_unlink(dev, i->dev); 4958 __netdev_adjacent_dev_unlink(dev, i->dev);
4813 4959
4814 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev); 4960 call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev);
4815} 4961}
4816EXPORT_SYMBOL(netdev_upper_dev_unlink); 4962EXPORT_SYMBOL(netdev_upper_dev_unlink);
4817 4963
4964void *netdev_lower_dev_get_private_rcu(struct net_device *dev,
4965 struct net_device *lower_dev)
4966{
4967 struct netdev_adjacent *lower;
4968
4969 if (!lower_dev)
4970 return NULL;
4971 lower = __netdev_find_adj_rcu(dev, lower_dev, &dev->adj_list.lower);
4972 if (!lower)
4973 return NULL;
4974
4975 return lower->private;
4976}
4977EXPORT_SYMBOL(netdev_lower_dev_get_private_rcu);
4978
4979void *netdev_lower_dev_get_private(struct net_device *dev,
4980 struct net_device *lower_dev)
4981{
4982 struct netdev_adjacent *lower;
4983
4984 if (!lower_dev)
4985 return NULL;
4986 lower = __netdev_find_adj(dev, lower_dev, &dev->adj_list.lower);
4987 if (!lower)
4988 return NULL;
4989
4990 return lower->private;
4991}
4992EXPORT_SYMBOL(netdev_lower_dev_get_private);
4993
4818static void dev_change_rx_flags(struct net_device *dev, int flags) 4994static void dev_change_rx_flags(struct net_device *dev, int flags)
4819{ 4995{
4820 const struct net_device_ops *ops = dev->netdev_ops; 4996 const struct net_device_ops *ops = dev->netdev_ops;
@@ -4823,7 +4999,7 @@ static void dev_change_rx_flags(struct net_device *dev, int flags)
4823 ops->ndo_change_rx_flags(dev, flags); 4999 ops->ndo_change_rx_flags(dev, flags);
4824} 5000}
4825 5001
4826static int __dev_set_promiscuity(struct net_device *dev, int inc) 5002static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
4827{ 5003{
4828 unsigned int old_flags = dev->flags; 5004 unsigned int old_flags = dev->flags;
4829 kuid_t uid; 5005 kuid_t uid;
@@ -4866,6 +5042,8 @@ static int __dev_set_promiscuity(struct net_device *dev, int inc)
4866 5042
4867 dev_change_rx_flags(dev, IFF_PROMISC); 5043 dev_change_rx_flags(dev, IFF_PROMISC);
4868 } 5044 }
5045 if (notify)
5046 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
4869 return 0; 5047 return 0;
4870} 5048}
4871 5049
@@ -4885,7 +5063,7 @@ int dev_set_promiscuity(struct net_device *dev, int inc)
4885 unsigned int old_flags = dev->flags; 5063 unsigned int old_flags = dev->flags;
4886 int err; 5064 int err;
4887 5065
4888 err = __dev_set_promiscuity(dev, inc); 5066 err = __dev_set_promiscuity(dev, inc, true);
4889 if (err < 0) 5067 if (err < 0)
4890 return err; 5068 return err;
4891 if (dev->flags != old_flags) 5069 if (dev->flags != old_flags)
@@ -4894,22 +5072,9 @@ int dev_set_promiscuity(struct net_device *dev, int inc)
4894} 5072}
4895EXPORT_SYMBOL(dev_set_promiscuity); 5073EXPORT_SYMBOL(dev_set_promiscuity);
4896 5074
4897/** 5075static 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{ 5076{
4912 unsigned int old_flags = dev->flags; 5077 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
4913 5078
4914 ASSERT_RTNL(); 5079 ASSERT_RTNL();
4915 5080
@@ -4932,9 +5097,30 @@ int dev_set_allmulti(struct net_device *dev, int inc)
4932 if (dev->flags ^ old_flags) { 5097 if (dev->flags ^ old_flags) {
4933 dev_change_rx_flags(dev, IFF_ALLMULTI); 5098 dev_change_rx_flags(dev, IFF_ALLMULTI);
4934 dev_set_rx_mode(dev); 5099 dev_set_rx_mode(dev);
5100 if (notify)
5101 __dev_notify_flags(dev, old_flags,
5102 dev->gflags ^ old_gflags);
4935 } 5103 }
4936 return 0; 5104 return 0;
4937} 5105}
5106
5107/**
5108 * dev_set_allmulti - update allmulti count on a device
5109 * @dev: device
5110 * @inc: modifier
5111 *
5112 * Add or remove reception of all multicast frames to a device. While the
5113 * count in the device remains above zero the interface remains listening
5114 * to all interfaces. Once it hits zero the device reverts back to normal
5115 * filtering operation. A negative @inc value is used to drop the counter
5116 * when releasing a resource needing all multicasts.
5117 * Return 0 if successful or a negative errno code on error.
5118 */
5119
5120int dev_set_allmulti(struct net_device *dev, int inc)
5121{
5122 return __dev_set_allmulti(dev, inc, true);
5123}
4938EXPORT_SYMBOL(dev_set_allmulti); 5124EXPORT_SYMBOL(dev_set_allmulti);
4939 5125
4940/* 5126/*
@@ -4959,10 +5145,10 @@ void __dev_set_rx_mode(struct net_device *dev)
4959 * therefore calling __dev_set_promiscuity here is safe. 5145 * therefore calling __dev_set_promiscuity here is safe.
4960 */ 5146 */
4961 if (!netdev_uc_empty(dev) && !dev->uc_promisc) { 5147 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
4962 __dev_set_promiscuity(dev, 1); 5148 __dev_set_promiscuity(dev, 1, false);
4963 dev->uc_promisc = true; 5149 dev->uc_promisc = true;
4964 } else if (netdev_uc_empty(dev) && dev->uc_promisc) { 5150 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
4965 __dev_set_promiscuity(dev, -1); 5151 __dev_set_promiscuity(dev, -1, false);
4966 dev->uc_promisc = false; 5152 dev->uc_promisc = false;
4967 } 5153 }
4968 } 5154 }
@@ -5051,9 +5237,13 @@ int __dev_change_flags(struct net_device *dev, unsigned int flags)
5051 5237
5052 if ((flags ^ dev->gflags) & IFF_PROMISC) { 5238 if ((flags ^ dev->gflags) & IFF_PROMISC) {
5053 int inc = (flags & IFF_PROMISC) ? 1 : -1; 5239 int inc = (flags & IFF_PROMISC) ? 1 : -1;
5240 unsigned int old_flags = dev->flags;
5054 5241
5055 dev->gflags ^= IFF_PROMISC; 5242 dev->gflags ^= IFF_PROMISC;
5056 dev_set_promiscuity(dev, inc); 5243
5244 if (__dev_set_promiscuity(dev, inc, false) >= 0)
5245 if (dev->flags != old_flags)
5246 dev_set_rx_mode(dev);
5057 } 5247 }
5058 5248
5059 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI 5249 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
@@ -5064,16 +5254,20 @@ int __dev_change_flags(struct net_device *dev, unsigned int flags)
5064 int inc = (flags & IFF_ALLMULTI) ? 1 : -1; 5254 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
5065 5255
5066 dev->gflags ^= IFF_ALLMULTI; 5256 dev->gflags ^= IFF_ALLMULTI;
5067 dev_set_allmulti(dev, inc); 5257 __dev_set_allmulti(dev, inc, false);
5068 } 5258 }
5069 5259
5070 return ret; 5260 return ret;
5071} 5261}
5072 5262
5073void __dev_notify_flags(struct net_device *dev, unsigned int old_flags) 5263void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
5264 unsigned int gchanges)
5074{ 5265{
5075 unsigned int changes = dev->flags ^ old_flags; 5266 unsigned int changes = dev->flags ^ old_flags;
5076 5267
5268 if (gchanges)
5269 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
5270
5077 if (changes & IFF_UP) { 5271 if (changes & IFF_UP) {
5078 if (dev->flags & IFF_UP) 5272 if (dev->flags & IFF_UP)
5079 call_netdevice_notifiers(NETDEV_UP, dev); 5273 call_netdevice_notifiers(NETDEV_UP, dev);
@@ -5102,17 +5296,14 @@ void __dev_notify_flags(struct net_device *dev, unsigned int old_flags)
5102int dev_change_flags(struct net_device *dev, unsigned int flags) 5296int dev_change_flags(struct net_device *dev, unsigned int flags)
5103{ 5297{
5104 int ret; 5298 int ret;
5105 unsigned int changes, old_flags = dev->flags; 5299 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
5106 5300
5107 ret = __dev_change_flags(dev, flags); 5301 ret = __dev_change_flags(dev, flags);
5108 if (ret < 0) 5302 if (ret < 0)
5109 return ret; 5303 return ret;
5110 5304
5111 changes = old_flags ^ dev->flags; 5305 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
5112 if (changes) 5306 __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; 5307 return ret;
5117} 5308}
5118EXPORT_SYMBOL(dev_change_flags); 5309EXPORT_SYMBOL(dev_change_flags);
@@ -5259,6 +5450,7 @@ static void net_set_todo(struct net_device *dev)
5259static void rollback_registered_many(struct list_head *head) 5450static void rollback_registered_many(struct list_head *head)
5260{ 5451{
5261 struct net_device *dev, *tmp; 5452 struct net_device *dev, *tmp;
5453 LIST_HEAD(close_head);
5262 5454
5263 BUG_ON(dev_boot_phase); 5455 BUG_ON(dev_boot_phase);
5264 ASSERT_RTNL(); 5456 ASSERT_RTNL();
@@ -5281,7 +5473,9 @@ static void rollback_registered_many(struct list_head *head)
5281 } 5473 }
5282 5474
5283 /* If device is running, close it first. */ 5475 /* If device is running, close it first. */
5284 dev_close_many(head); 5476 list_for_each_entry(dev, head, unreg_list)
5477 list_add_tail(&dev->close_list, &close_head);
5478 dev_close_many(&close_head);
5285 5479
5286 list_for_each_entry(dev, head, unreg_list) { 5480 list_for_each_entry(dev, head, unreg_list) {
5287 /* And unlink it from device chain. */ 5481 /* And unlink it from device chain. */
@@ -5304,7 +5498,7 @@ static void rollback_registered_many(struct list_head *head)
5304 5498
5305 if (!dev->rtnl_link_ops || 5499 if (!dev->rtnl_link_ops ||
5306 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 5500 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5307 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 5501 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
5308 5502
5309 /* 5503 /*
5310 * Flush the unicast and multicast chains 5504 * Flush the unicast and multicast chains
@@ -5703,7 +5897,7 @@ int register_netdevice(struct net_device *dev)
5703 */ 5897 */
5704 if (!dev->rtnl_link_ops || 5898 if (!dev->rtnl_link_ops ||
5705 dev->rtnl_link_state == RTNL_LINK_INITIALIZED) 5899 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
5706 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 5900 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
5707 5901
5708out: 5902out:
5709 return ret; 5903 return ret;
@@ -6010,6 +6204,16 @@ void netdev_set_default_ethtool_ops(struct net_device *dev,
6010} 6204}
6011EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); 6205EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
6012 6206
6207void netdev_freemem(struct net_device *dev)
6208{
6209 char *addr = (char *)dev - dev->padded;
6210
6211 if (is_vmalloc_addr(addr))
6212 vfree(addr);
6213 else
6214 kfree(addr);
6215}
6216
6013/** 6217/**
6014 * alloc_netdev_mqs - allocate network device 6218 * alloc_netdev_mqs - allocate network device
6015 * @sizeof_priv: size of private data to allocate space for 6219 * @sizeof_priv: size of private data to allocate space for
@@ -6053,7 +6257,9 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6053 /* ensure 32-byte alignment of whole construct */ 6257 /* ensure 32-byte alignment of whole construct */
6054 alloc_size += NETDEV_ALIGN - 1; 6258 alloc_size += NETDEV_ALIGN - 1;
6055 6259
6056 p = kzalloc(alloc_size, GFP_KERNEL); 6260 p = kzalloc(alloc_size, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
6261 if (!p)
6262 p = vzalloc(alloc_size);
6057 if (!p) 6263 if (!p)
6058 return NULL; 6264 return NULL;
6059 6265
@@ -6062,7 +6268,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6062 6268
6063 dev->pcpu_refcnt = alloc_percpu(int); 6269 dev->pcpu_refcnt = alloc_percpu(int);
6064 if (!dev->pcpu_refcnt) 6270 if (!dev->pcpu_refcnt)
6065 goto free_p; 6271 goto free_dev;
6066 6272
6067 if (dev_addr_init(dev)) 6273 if (dev_addr_init(dev))
6068 goto free_pcpu; 6274 goto free_pcpu;
@@ -6077,9 +6283,12 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
6077 6283
6078 INIT_LIST_HEAD(&dev->napi_list); 6284 INIT_LIST_HEAD(&dev->napi_list);
6079 INIT_LIST_HEAD(&dev->unreg_list); 6285 INIT_LIST_HEAD(&dev->unreg_list);
6286 INIT_LIST_HEAD(&dev->close_list);
6080 INIT_LIST_HEAD(&dev->link_watch_list); 6287 INIT_LIST_HEAD(&dev->link_watch_list);
6081 INIT_LIST_HEAD(&dev->upper_dev_list); 6288 INIT_LIST_HEAD(&dev->adj_list.upper);
6082 INIT_LIST_HEAD(&dev->lower_dev_list); 6289 INIT_LIST_HEAD(&dev->adj_list.lower);
6290 INIT_LIST_HEAD(&dev->all_adj_list.upper);
6291 INIT_LIST_HEAD(&dev->all_adj_list.lower);
6083 dev->priv_flags = IFF_XMIT_DST_RELEASE; 6292 dev->priv_flags = IFF_XMIT_DST_RELEASE;
6084 setup(dev); 6293 setup(dev);
6085 6294
@@ -6112,8 +6321,8 @@ free_pcpu:
6112 kfree(dev->_rx); 6321 kfree(dev->_rx);
6113#endif 6322#endif
6114 6323
6115free_p: 6324free_dev:
6116 kfree(p); 6325 netdev_freemem(dev);
6117 return NULL; 6326 return NULL;
6118} 6327}
6119EXPORT_SYMBOL(alloc_netdev_mqs); 6328EXPORT_SYMBOL(alloc_netdev_mqs);
@@ -6150,7 +6359,7 @@ void free_netdev(struct net_device *dev)
6150 6359
6151 /* Compatibility with error handling in drivers */ 6360 /* Compatibility with error handling in drivers */
6152 if (dev->reg_state == NETREG_UNINITIALIZED) { 6361 if (dev->reg_state == NETREG_UNINITIALIZED) {
6153 kfree((char *)dev - dev->padded); 6362 netdev_freemem(dev);
6154 return; 6363 return;
6155 } 6364 }
6156 6365
@@ -6312,7 +6521,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
6312 call_netdevice_notifiers(NETDEV_UNREGISTER, dev); 6521 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6313 rcu_barrier(); 6522 rcu_barrier();
6314 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); 6523 call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev);
6315 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); 6524 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U, GFP_KERNEL);
6316 6525
6317 /* 6526 /*
6318 * Flush the unicast and multicast chains 6527 * Flush the unicast and multicast chains
@@ -6351,7 +6560,7 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
6351 * Prevent userspace races by waiting until the network 6560 * Prevent userspace races by waiting until the network
6352 * device is fully setup before sending notifications. 6561 * device is fully setup before sending notifications.
6353 */ 6562 */
6354 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); 6563 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
6355 6564
6356 synchronize_net(); 6565 synchronize_net();
6357 err = 0; 6566 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/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/flow_dissector.c b/net/core/flow_dissector.c
index 143b6fdb9647..d6ef17322500 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
@@ -42,13 +68,13 @@ ip:
42 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph); 68 iph = skb_header_pointer(skb, nhoff, sizeof(_iph), &_iph);
43 if (!iph || iph->ihl < 5) 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 }
@@ -378,11 +411,3 @@ struct netdev_queue *netdev_pick_tx(struct net_device *dev,
378 skb_set_queue_mapping(skb, queue_index); 411 skb_set_queue_mapping(skb, queue_index);
379 return netdev_get_tx_queue(dev, queue_index); 412 return netdev_get_tx_queue(dev, queue_index);
380} 413}
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..4cdb7c48dad6 100644
--- a/net/core/iovec.c
+++ b/net/core/iovec.c
@@ -100,7 +100,7 @@ int memcpy_toiovecend(const struct iovec *iov, unsigned char *kdata,
100EXPORT_SYMBOL(memcpy_toiovecend); 100EXPORT_SYMBOL(memcpy_toiovecend);
101 101
102/* 102/*
103 * Copy iovec from kernel. Returns -EFAULT on error. 103 * Copy iovec to kernel. Returns -EFAULT on error.
104 */ 104 */
105 105
106int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov, 106int memcpy_fromiovecend(unsigned char *kdata, const struct iovec *iov,
diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index 6072610a8672..ca15f32821fb 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);
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index 325dee863e46..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)
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/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..8cec1e6b844d 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 *
@@ -2837,14 +2875,7 @@ struct sk_buff *skb_segment(struct sk_buff *skb, netdev_features_t features)
2837 __copy_skb_header(nskb, skb); 2875 __copy_skb_header(nskb, skb);
2838 nskb->mac_len = skb->mac_len; 2876 nskb->mac_len = skb->mac_len;
2839 2877
2840 /* nskb and skb might have different headroom */ 2878 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 2879
2849 skb_copy_from_linear_data_offset(skb, -tnl_hlen, 2880 skb_copy_from_linear_data_offset(skb, -tnl_hlen,
2850 nskb->data - tnl_hlen, 2881 nskb->data - tnl_hlen,
@@ -2936,32 +2967,30 @@ EXPORT_SYMBOL_GPL(skb_segment);
2936 2967
2937int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb) 2968int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2938{ 2969{
2939 struct sk_buff *p = *head; 2970 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); 2971 unsigned int offset = skb_gro_offset(skb);
2946 unsigned int headlen = skb_headlen(skb); 2972 unsigned int headlen = skb_headlen(skb);
2973 struct sk_buff *nskb, *lp, *p = *head;
2974 unsigned int len = skb_gro_len(skb);
2947 unsigned int delta_truesize; 2975 unsigned int delta_truesize;
2976 unsigned int headroom;
2948 2977
2949 if (p->len + len >= 65536) 2978 if (unlikely(p->len + len >= 65536))
2950 return -E2BIG; 2979 return -E2BIG;
2951 2980
2952 if (pinfo->frag_list) 2981 lp = NAPI_GRO_CB(p)->last ?: p;
2953 goto merge; 2982 pinfo = skb_shinfo(lp);
2954 else if (headlen <= offset) { 2983
2984 if (headlen <= offset) {
2955 skb_frag_t *frag; 2985 skb_frag_t *frag;
2956 skb_frag_t *frag2; 2986 skb_frag_t *frag2;
2957 int i = skbinfo->nr_frags; 2987 int i = skbinfo->nr_frags;
2958 int nr_frags = pinfo->nr_frags + i; 2988 int nr_frags = pinfo->nr_frags + i;
2959 2989
2960 offset -= headlen;
2961
2962 if (nr_frags > MAX_SKB_FRAGS) 2990 if (nr_frags > MAX_SKB_FRAGS)
2963 return -E2BIG; 2991 goto merge;
2964 2992
2993 offset -= headlen;
2965 pinfo->nr_frags = nr_frags; 2994 pinfo->nr_frags = nr_frags;
2966 skbinfo->nr_frags = 0; 2995 skbinfo->nr_frags = 0;
2967 2996
@@ -2992,7 +3021,7 @@ int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
2992 unsigned int first_offset; 3021 unsigned int first_offset;
2993 3022
2994 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS) 3023 if (nr_frags + 1 + skbinfo->nr_frags > MAX_SKB_FRAGS)
2995 return -E2BIG; 3024 goto merge;
2996 3025
2997 first_offset = skb->data - 3026 first_offset = skb->data -
2998 (unsigned char *)page_address(page) + 3027 (unsigned char *)page_address(page) +
@@ -3010,7 +3039,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)); 3039 delta_truesize = skb->truesize - SKB_DATA_ALIGN(sizeof(struct sk_buff));
3011 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3040 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3012 goto done; 3041 goto done;
3013 } else if (skb_gro_len(p) != pinfo->gso_size) 3042 }
3043 if (pinfo->frag_list)
3044 goto merge;
3045 if (skb_gro_len(p) != pinfo->gso_size)
3014 return -E2BIG; 3046 return -E2BIG;
3015 3047
3016 headroom = skb_headroom(p); 3048 headroom = skb_headroom(p);
@@ -3062,16 +3094,24 @@ merge:
3062 3094
3063 __skb_pull(skb, offset); 3095 __skb_pull(skb, offset);
3064 3096
3065 NAPI_GRO_CB(p)->last->next = skb; 3097 if (!NAPI_GRO_CB(p)->last)
3098 skb_shinfo(p)->frag_list = skb;
3099 else
3100 NAPI_GRO_CB(p)->last->next = skb;
3066 NAPI_GRO_CB(p)->last = skb; 3101 NAPI_GRO_CB(p)->last = skb;
3067 skb_header_release(skb); 3102 skb_header_release(skb);
3103 lp = p;
3068 3104
3069done: 3105done:
3070 NAPI_GRO_CB(p)->count++; 3106 NAPI_GRO_CB(p)->count++;
3071 p->data_len += len; 3107 p->data_len += len;
3072 p->truesize += delta_truesize; 3108 p->truesize += delta_truesize;
3073 p->len += len; 3109 p->len += len;
3074 3110 if (lp != p) {
3111 lp->data_len += len;
3112 lp->truesize += delta_truesize;
3113 lp->len += len;
3114 }
3075 NAPI_GRO_CB(skb)->same_flow = 1; 3115 NAPI_GRO_CB(skb)->same_flow = 1;
3076 return 0; 3116 return 0;
3077} 3117}
diff --git a/net/core/sock.c b/net/core/sock.c
index 0b39e7ae4383..ab20ed9b0f31 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);
@@ -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..4ac71ff7c2e4 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 }
@@ -885,7 +884,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
885 return -EINVAL; 884 return -EINVAL;
886 } 885 }
887 886
888 np->daddr = usin->sin6_addr; 887 sk->sk_v6_daddr = usin->sin6_addr;
889 np->flow_label = fl6.flowlabel; 888 np->flow_label = fl6.flowlabel;
890 889
891 /* 890 /*
@@ -915,16 +914,16 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
915 goto failure; 914 goto failure;
916 } 915 }
917 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 916 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
918 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &np->rcv_saddr); 917 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr);
919 918
920 return err; 919 return err;
921 } 920 }
922 921
923 if (!ipv6_addr_any(&np->rcv_saddr)) 922 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
924 saddr = &np->rcv_saddr; 923 saddr = &sk->sk_v6_rcv_saddr;
925 924
926 fl6.flowi6_proto = IPPROTO_DCCP; 925 fl6.flowi6_proto = IPPROTO_DCCP;
927 fl6.daddr = np->daddr; 926 fl6.daddr = sk->sk_v6_daddr;
928 fl6.saddr = saddr ? *saddr : np->saddr; 927 fl6.saddr = saddr ? *saddr : np->saddr;
929 fl6.flowi6_oif = sk->sk_bound_dev_if; 928 fl6.flowi6_oif = sk->sk_bound_dev_if;
930 fl6.fl6_dport = usin->sin6_port; 929 fl6.fl6_dport = usin->sin6_port;
@@ -941,7 +940,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
941 940
942 if (saddr == NULL) { 941 if (saddr == NULL) {
943 saddr = &fl6.saddr; 942 saddr = &fl6.saddr;
944 np->rcv_saddr = *saddr; 943 sk->sk_v6_rcv_saddr = *saddr;
945 } 944 }
946 945
947 /* set the source address */ 946 /* set the source address */
@@ -963,7 +962,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
963 goto late_failure; 962 goto late_failure;
964 963
965 dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32, 964 dp->dccps_iss = secure_dccpv6_sequence_number(np->saddr.s6_addr32,
966 np->daddr.s6_addr32, 965 sk->sk_v6_daddr.s6_addr32,
967 inet->inet_sport, 966 inet->inet_sport,
968 inet->inet_dport); 967 inet->inet_dport);
969 err = dccp_connect(sk); 968 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/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..003f5bb3acd2
--- /dev/null
+++ b/net/hsr/hsr_framereg.c
@@ -0,0 +1,503 @@
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 if ((int) b - a == 32768)
293 return false;
294
295 return (((s16) (b - a)) < 0);
296}
297#define seq_nr_before(a, b) seq_nr_after((b), (a))
298#define seq_nr_after_or_eq(a, b) (!seq_nr_before((a), (b)))
299#define seq_nr_before_or_eq(a, b) (!seq_nr_after((a), (b)))
300
301
302void hsr_register_frame_in(struct node_entry *node, enum hsr_dev_idx dev_idx)
303{
304 if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
305 WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
306 return;
307 }
308 node->time_in[dev_idx] = jiffies;
309 node->time_in_stale[dev_idx] = false;
310}
311
312
313/* 'skb' is a HSR Ethernet frame (with a HSR tag inserted), with a valid
314 * ethhdr->h_source address and skb->mac_header set.
315 *
316 * Return:
317 * 1 if frame can be shown to have been sent recently on this interface,
318 * 0 otherwise, or
319 * negative error code on error
320 */
321int hsr_register_frame_out(struct node_entry *node, enum hsr_dev_idx dev_idx,
322 struct sk_buff *skb)
323{
324 struct hsr_ethhdr *hsr_ethhdr;
325 u16 sequence_nr;
326
327 if ((dev_idx < 0) || (dev_idx >= HSR_MAX_DEV)) {
328 WARN_ONCE(1, "%s: Invalid dev_idx (%d)\n", __func__, dev_idx);
329 return -EINVAL;
330 }
331 if (!skb_mac_header_was_set(skb)) {
332 WARN_ONCE(1, "%s: Mac header not set\n", __func__);
333 return -EINVAL;
334 }
335 hsr_ethhdr = (struct hsr_ethhdr *) skb_mac_header(skb);
336
337 sequence_nr = ntohs(hsr_ethhdr->hsr_tag.sequence_nr);
338 if (seq_nr_before_or_eq(sequence_nr, node->seq_out[dev_idx]))
339 return 1;
340
341 node->seq_out[dev_idx] = sequence_nr;
342 return 0;
343}
344
345
346
347static bool is_late(struct node_entry *node, enum hsr_dev_idx dev_idx)
348{
349 enum hsr_dev_idx other;
350
351 if (node->time_in_stale[dev_idx])
352 return true;
353
354 if (dev_idx == HSR_DEV_SLAVE_A)
355 other = HSR_DEV_SLAVE_B;
356 else
357 other = HSR_DEV_SLAVE_A;
358
359 if (node->time_in_stale[other])
360 return false;
361
362 if (time_after(node->time_in[other], node->time_in[dev_idx] +
363 msecs_to_jiffies(MAX_SLAVE_DIFF)))
364 return true;
365
366 return false;
367}
368
369
370/* Remove stale sequence_nr records. Called by timer every
371 * HSR_LIFE_CHECK_INTERVAL (two seconds or so).
372 */
373void hsr_prune_nodes(struct hsr_priv *hsr_priv)
374{
375 struct node_entry *node;
376 unsigned long timestamp;
377 unsigned long time_a, time_b;
378
379 rcu_read_lock();
380 list_for_each_entry_rcu(node, &hsr_priv->node_db, mac_list) {
381 /* Shorthand */
382 time_a = node->time_in[HSR_DEV_SLAVE_A];
383 time_b = node->time_in[HSR_DEV_SLAVE_B];
384
385 /* Check for timestamps old enough to risk wrap-around */
386 if (time_after(jiffies, time_a + MAX_JIFFY_OFFSET/2))
387 node->time_in_stale[HSR_DEV_SLAVE_A] = true;
388 if (time_after(jiffies, time_b + MAX_JIFFY_OFFSET/2))
389 node->time_in_stale[HSR_DEV_SLAVE_B] = true;
390
391 /* Get age of newest frame from node.
392 * At least one time_in is OK here; nodes get pruned long
393 * before both time_ins can get stale
394 */
395 timestamp = time_a;
396 if (node->time_in_stale[HSR_DEV_SLAVE_A] ||
397 (!node->time_in_stale[HSR_DEV_SLAVE_B] &&
398 time_after(time_b, time_a)))
399 timestamp = time_b;
400
401 /* Warn of ring error only as long as we get frames at all */
402 if (time_is_after_jiffies(timestamp +
403 msecs_to_jiffies(1.5*MAX_SLAVE_DIFF))) {
404
405 if (is_late(node, HSR_DEV_SLAVE_A))
406 hsr_nl_ringerror(hsr_priv, node->MacAddressA,
407 HSR_DEV_SLAVE_A);
408 else if (is_late(node, HSR_DEV_SLAVE_B))
409 hsr_nl_ringerror(hsr_priv, node->MacAddressA,
410 HSR_DEV_SLAVE_B);
411 }
412
413 /* Prune old entries */
414 if (time_is_before_jiffies(timestamp +
415 msecs_to_jiffies(HSR_NODE_FORGET_TIME))) {
416 hsr_nl_nodedown(hsr_priv, node->MacAddressA);
417 list_del_rcu(&node->mac_list);
418 /* Note that we need to free this entry later: */
419 call_rcu(&node->rcu_head, node_entry_reclaim);
420 }
421 }
422 rcu_read_unlock();
423}
424
425
426void *hsr_get_next_node(struct hsr_priv *hsr_priv, void *_pos,
427 unsigned char addr[ETH_ALEN])
428{
429 struct node_entry *node;
430
431 if (!_pos) {
432 node = list_first_or_null_rcu(&hsr_priv->node_db,
433 struct node_entry, mac_list);
434 if (node)
435 memcpy(addr, node->MacAddressA, ETH_ALEN);
436 return node;
437 }
438
439 node = _pos;
440 list_for_each_entry_continue_rcu(node, &hsr_priv->node_db, mac_list) {
441 memcpy(addr, node->MacAddressA, ETH_ALEN);
442 return node;
443 }
444
445 return NULL;
446}
447
448
449int hsr_get_node_data(struct hsr_priv *hsr_priv,
450 const unsigned char *addr,
451 unsigned char addr_b[ETH_ALEN],
452 unsigned int *addr_b_ifindex,
453 int *if1_age,
454 u16 *if1_seq,
455 int *if2_age,
456 u16 *if2_seq)
457{
458 struct node_entry *node;
459 unsigned long tdiff;
460
461
462 rcu_read_lock();
463 node = find_node_by_AddrA(&hsr_priv->node_db, addr);
464 if (!node) {
465 rcu_read_unlock();
466 return -ENOENT; /* No such entry */
467 }
468
469 memcpy(addr_b, node->MacAddressB, ETH_ALEN);
470
471 tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_A];
472 if (node->time_in_stale[HSR_DEV_SLAVE_A])
473 *if1_age = INT_MAX;
474#if HZ <= MSEC_PER_SEC
475 else if (tdiff > msecs_to_jiffies(INT_MAX))
476 *if1_age = INT_MAX;
477#endif
478 else
479 *if1_age = jiffies_to_msecs(tdiff);
480
481 tdiff = jiffies - node->time_in[HSR_DEV_SLAVE_B];
482 if (node->time_in_stale[HSR_DEV_SLAVE_B])
483 *if2_age = INT_MAX;
484#if HZ <= MSEC_PER_SEC
485 else if (tdiff > msecs_to_jiffies(INT_MAX))
486 *if2_age = INT_MAX;
487#endif
488 else
489 *if2_age = jiffies_to_msecs(tdiff);
490
491 /* Present sequence numbers as if they were incoming on interface */
492 *if1_seq = node->seq_out[HSR_DEV_SLAVE_B];
493 *if2_seq = node->seq_out[HSR_DEV_SLAVE_A];
494
495 if ((node->AddrB_if != HSR_DEV_NONE) && hsr_priv->slave[node->AddrB_if])
496 *addr_b_ifindex = hsr_priv->slave[node->AddrB_if]->ifindex;
497 else
498 *addr_b_ifindex = -1;
499
500 rcu_read_unlock();
501
502 return 0;
503}
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..4e66bf61f585
--- /dev/null
+++ b/net/hsr/hsr_netlink.c
@@ -0,0 +1,457 @@
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};
27
28
29/* Here, it seems a netdevice has already been allocated for us, and the
30 * hsr_dev_setup routine has been executed. Nice!
31 */
32static int hsr_newlink(struct net *src_net, struct net_device *dev,
33 struct nlattr *tb[], struct nlattr *data[])
34{
35 struct net_device *link[2];
36 unsigned char multicast_spec;
37
38 if (!data[IFLA_HSR_SLAVE1]) {
39 netdev_info(dev, "IFLA_HSR_SLAVE1 missing!\n");
40 return -EINVAL;
41 }
42 link[0] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE1]));
43 if (!data[IFLA_HSR_SLAVE2]) {
44 netdev_info(dev, "IFLA_HSR_SLAVE2 missing!\n");
45 return -EINVAL;
46 }
47 link[1] = __dev_get_by_index(src_net, nla_get_u32(data[IFLA_HSR_SLAVE2]));
48
49 if (!link[0] || !link[1])
50 return -ENODEV;
51 if (link[0] == link[1])
52 return -EINVAL;
53
54 if (!data[IFLA_HSR_MULTICAST_SPEC])
55 multicast_spec = 0;
56 else
57 multicast_spec = nla_get_u8(data[IFLA_HSR_MULTICAST_SPEC]);
58
59 return hsr_dev_finalize(dev, link, multicast_spec);
60}
61
62static struct rtnl_link_ops hsr_link_ops __read_mostly = {
63 .kind = "hsr",
64 .maxtype = IFLA_HSR_MAX,
65 .policy = hsr_policy,
66 .priv_size = sizeof(struct hsr_priv),
67 .setup = hsr_dev_setup,
68 .newlink = hsr_newlink,
69};
70
71
72
73/* attribute policy */
74/* NLA_BINARY missing in libnl; use NLA_UNSPEC in userspace instead. */
75static const struct nla_policy hsr_genl_policy[HSR_A_MAX + 1] = {
76 [HSR_A_NODE_ADDR] = { .type = NLA_BINARY, .len = ETH_ALEN },
77 [HSR_A_NODE_ADDR_B] = { .type = NLA_BINARY, .len = ETH_ALEN },
78 [HSR_A_IFINDEX] = { .type = NLA_U32 },
79 [HSR_A_IF1_AGE] = { .type = NLA_U32 },
80 [HSR_A_IF2_AGE] = { .type = NLA_U32 },
81 [HSR_A_IF1_SEQ] = { .type = NLA_U16 },
82 [HSR_A_IF2_SEQ] = { .type = NLA_U16 },
83};
84
85static struct genl_family hsr_genl_family = {
86 .id = GENL_ID_GENERATE,
87 .hdrsize = 0,
88 .name = "HSR",
89 .version = 1,
90 .maxattr = HSR_A_MAX,
91};
92
93static struct genl_multicast_group hsr_network_genl_mcgrp = {
94 .name = "hsr-network",
95};
96
97
98
99/* This is called if for some node with MAC address addr, we only get frames
100 * over one of the slave interfaces. This would indicate an open network ring
101 * (i.e. a link has failed somewhere).
102 */
103void hsr_nl_ringerror(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN],
104 enum hsr_dev_idx dev_idx)
105{
106 struct sk_buff *skb;
107 void *msg_head;
108 int res;
109 int ifindex;
110
111 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
112 if (!skb)
113 goto fail;
114
115 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_RING_ERROR);
116 if (!msg_head)
117 goto nla_put_failure;
118
119 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
120 if (res < 0)
121 goto nla_put_failure;
122
123 if (hsr_priv->slave[dev_idx])
124 ifindex = hsr_priv->slave[dev_idx]->ifindex;
125 else
126 ifindex = -1;
127 res = nla_put_u32(skb, HSR_A_IFINDEX, ifindex);
128 if (res < 0)
129 goto nla_put_failure;
130
131 genlmsg_end(skb, msg_head);
132 genlmsg_multicast(skb, 0, hsr_network_genl_mcgrp.id, GFP_ATOMIC);
133
134 return;
135
136nla_put_failure:
137 kfree_skb(skb);
138
139fail:
140 netdev_warn(hsr_priv->dev, "Could not send HSR ring error message\n");
141}
142
143/* This is called when we haven't heard from the node with MAC address addr for
144 * some time (just before the node is removed from the node table/list).
145 */
146void hsr_nl_nodedown(struct hsr_priv *hsr_priv, unsigned char addr[ETH_ALEN])
147{
148 struct sk_buff *skb;
149 void *msg_head;
150 int res;
151
152 skb = genlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
153 if (!skb)
154 goto fail;
155
156 msg_head = genlmsg_put(skb, 0, 0, &hsr_genl_family, 0, HSR_C_NODE_DOWN);
157 if (!msg_head)
158 goto nla_put_failure;
159
160
161 res = nla_put(skb, HSR_A_NODE_ADDR, ETH_ALEN, addr);
162 if (res < 0)
163 goto nla_put_failure;
164
165 genlmsg_end(skb, msg_head);
166 genlmsg_multicast(skb, 0, hsr_network_genl_mcgrp.id, GFP_ATOMIC);
167
168 return;
169
170nla_put_failure:
171 kfree_skb(skb);
172
173fail:
174 netdev_warn(hsr_priv->dev, "Could not send HSR node down\n");
175}
176
177
178/* HSR_C_GET_NODE_STATUS lets userspace query the internal HSR node table
179 * about the status of a specific node in the network, defined by its MAC
180 * address.
181 *
182 * Input: hsr ifindex, node mac address
183 * Output: hsr ifindex, node mac address (copied from request),
184 * age of latest frame from node over slave 1, slave 2 [ms]
185 */
186static int hsr_get_node_status(struct sk_buff *skb_in, struct genl_info *info)
187{
188 /* For receiving */
189 struct nlattr *na;
190 struct net_device *hsr_dev;
191
192 /* For sending */
193 struct sk_buff *skb_out;
194 void *msg_head;
195 struct hsr_priv *hsr_priv;
196 unsigned char hsr_node_addr_b[ETH_ALEN];
197 int hsr_node_if1_age;
198 u16 hsr_node_if1_seq;
199 int hsr_node_if2_age;
200 u16 hsr_node_if2_seq;
201 int addr_b_ifindex;
202 int res;
203
204 if (!info)
205 goto invalid;
206
207 na = info->attrs[HSR_A_IFINDEX];
208 if (!na)
209 goto invalid;
210 na = info->attrs[HSR_A_NODE_ADDR];
211 if (!na)
212 goto invalid;
213
214 hsr_dev = __dev_get_by_index(genl_info_net(info),
215 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
216 if (!hsr_dev)
217 goto invalid;
218 if (!is_hsr_master(hsr_dev))
219 goto invalid;
220
221
222 /* Send reply */
223
224 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
225 if (!skb_out) {
226 res = -ENOMEM;
227 goto fail;
228 }
229
230 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
231 info->snd_seq, &hsr_genl_family, 0,
232 HSR_C_SET_NODE_STATUS);
233 if (!msg_head) {
234 res = -ENOMEM;
235 goto nla_put_failure;
236 }
237
238 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
239 if (res < 0)
240 goto nla_put_failure;
241
242 hsr_priv = netdev_priv(hsr_dev);
243 res = hsr_get_node_data(hsr_priv,
244 (unsigned char *) nla_data(info->attrs[HSR_A_NODE_ADDR]),
245 hsr_node_addr_b,
246 &addr_b_ifindex,
247 &hsr_node_if1_age,
248 &hsr_node_if1_seq,
249 &hsr_node_if2_age,
250 &hsr_node_if2_seq);
251 if (res < 0)
252 goto fail;
253
254 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN,
255 nla_data(info->attrs[HSR_A_NODE_ADDR]));
256 if (res < 0)
257 goto nla_put_failure;
258
259 if (addr_b_ifindex > -1) {
260 res = nla_put(skb_out, HSR_A_NODE_ADDR_B, ETH_ALEN,
261 hsr_node_addr_b);
262 if (res < 0)
263 goto nla_put_failure;
264
265 res = nla_put_u32(skb_out, HSR_A_ADDR_B_IFINDEX, addr_b_ifindex);
266 if (res < 0)
267 goto nla_put_failure;
268 }
269
270 res = nla_put_u32(skb_out, HSR_A_IF1_AGE, hsr_node_if1_age);
271 if (res < 0)
272 goto nla_put_failure;
273 res = nla_put_u16(skb_out, HSR_A_IF1_SEQ, hsr_node_if1_seq);
274 if (res < 0)
275 goto nla_put_failure;
276 if (hsr_priv->slave[0])
277 res = nla_put_u32(skb_out, HSR_A_IF1_IFINDEX,
278 hsr_priv->slave[0]->ifindex);
279 if (res < 0)
280 goto nla_put_failure;
281
282 res = nla_put_u32(skb_out, HSR_A_IF2_AGE, hsr_node_if2_age);
283 if (res < 0)
284 goto nla_put_failure;
285 res = nla_put_u16(skb_out, HSR_A_IF2_SEQ, hsr_node_if2_seq);
286 if (res < 0)
287 goto nla_put_failure;
288 if (hsr_priv->slave[1])
289 res = nla_put_u32(skb_out, HSR_A_IF2_IFINDEX,
290 hsr_priv->slave[1]->ifindex);
291
292 genlmsg_end(skb_out, msg_head);
293 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
294
295 return 0;
296
297invalid:
298 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
299 return 0;
300
301nla_put_failure:
302 kfree_skb(skb_out);
303 /* Fall through */
304
305fail:
306 return res;
307}
308
309static struct genl_ops hsr_ops_get_node_status = {
310 .cmd = HSR_C_GET_NODE_STATUS,
311 .flags = 0,
312 .policy = hsr_genl_policy,
313 .doit = hsr_get_node_status,
314 .dumpit = NULL,
315};
316
317
318/* Get a list of MacAddressA of all nodes known to this node (other than self).
319 */
320static int hsr_get_node_list(struct sk_buff *skb_in, struct genl_info *info)
321{
322 /* For receiving */
323 struct nlattr *na;
324 struct net_device *hsr_dev;
325
326 /* For sending */
327 struct sk_buff *skb_out;
328 void *msg_head;
329 struct hsr_priv *hsr_priv;
330 void *pos;
331 unsigned char addr[ETH_ALEN];
332 int res;
333
334 if (!info)
335 goto invalid;
336
337 na = info->attrs[HSR_A_IFINDEX];
338 if (!na)
339 goto invalid;
340
341 hsr_dev = __dev_get_by_index(genl_info_net(info),
342 nla_get_u32(info->attrs[HSR_A_IFINDEX]));
343 if (!hsr_dev)
344 goto invalid;
345 if (!is_hsr_master(hsr_dev))
346 goto invalid;
347
348
349 /* Send reply */
350
351 skb_out = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
352 if (!skb_out) {
353 res = -ENOMEM;
354 goto fail;
355 }
356
357 msg_head = genlmsg_put(skb_out, NETLINK_CB(skb_in).portid,
358 info->snd_seq, &hsr_genl_family, 0,
359 HSR_C_SET_NODE_LIST);
360 if (!msg_head) {
361 res = -ENOMEM;
362 goto nla_put_failure;
363 }
364
365 res = nla_put_u32(skb_out, HSR_A_IFINDEX, hsr_dev->ifindex);
366 if (res < 0)
367 goto nla_put_failure;
368
369 hsr_priv = netdev_priv(hsr_dev);
370
371 rcu_read_lock();
372 pos = hsr_get_next_node(hsr_priv, NULL, addr);
373 while (pos) {
374 res = nla_put(skb_out, HSR_A_NODE_ADDR, ETH_ALEN, addr);
375 if (res < 0) {
376 rcu_read_unlock();
377 goto nla_put_failure;
378 }
379 pos = hsr_get_next_node(hsr_priv, pos, addr);
380 }
381 rcu_read_unlock();
382
383 genlmsg_end(skb_out, msg_head);
384 genlmsg_unicast(genl_info_net(info), skb_out, info->snd_portid);
385
386 return 0;
387
388invalid:
389 netlink_ack(skb_in, nlmsg_hdr(skb_in), -EINVAL);
390 return 0;
391
392nla_put_failure:
393 kfree_skb(skb_out);
394 /* Fall through */
395
396fail:
397 return res;
398}
399
400
401static struct genl_ops hsr_ops_get_node_list = {
402 .cmd = HSR_C_GET_NODE_LIST,
403 .flags = 0,
404 .policy = hsr_genl_policy,
405 .doit = hsr_get_node_list,
406 .dumpit = NULL,
407};
408
409int __init hsr_netlink_init(void)
410{
411 int rc;
412
413 rc = rtnl_link_register(&hsr_link_ops);
414 if (rc)
415 goto fail_rtnl_link_register;
416
417 rc = genl_register_family(&hsr_genl_family);
418 if (rc)
419 goto fail_genl_register_family;
420
421 rc = genl_register_ops(&hsr_genl_family, &hsr_ops_get_node_status);
422 if (rc)
423 goto fail_genl_register_ops;
424
425 rc = genl_register_ops(&hsr_genl_family, &hsr_ops_get_node_list);
426 if (rc)
427 goto fail_genl_register_ops_node_list;
428
429 rc = genl_register_mc_group(&hsr_genl_family, &hsr_network_genl_mcgrp);
430 if (rc)
431 goto fail_genl_register_mc_group;
432
433 return 0;
434
435fail_genl_register_mc_group:
436 genl_unregister_ops(&hsr_genl_family, &hsr_ops_get_node_list);
437fail_genl_register_ops_node_list:
438 genl_unregister_ops(&hsr_genl_family, &hsr_ops_get_node_status);
439fail_genl_register_ops:
440 genl_unregister_family(&hsr_genl_family);
441fail_genl_register_family:
442 rtnl_link_unregister(&hsr_link_ops);
443fail_rtnl_link_register:
444
445 return rc;
446}
447
448void __exit hsr_netlink_exit(void)
449{
450 genl_unregister_mc_group(&hsr_genl_family, &hsr_network_genl_mcgrp);
451 genl_unregister_ops(&hsr_genl_family, &hsr_ops_get_node_status);
452 genl_unregister_family(&hsr_genl_family);
453
454 rtnl_link_unregister(&hsr_link_ops);
455}
456
457MODULE_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..426b5df1c98f 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",
@@ -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));
@@ -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/ipv4/af_inet.c b/net/ipv4/af_inet.c
index cfeb85cff4f0..68af9aac91d0 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:
@@ -1546,6 +1531,7 @@ static const struct net_protocol tcp_protocol = {
1546}; 1531};
1547 1532
1548static const struct net_protocol udp_protocol = { 1533static const struct net_protocol udp_protocol = {
1534 .early_demux = udp_v4_early_demux,
1549 .handler = udp_rcv, 1535 .handler = udp_rcv,
1550 .err_handler = udp_err, 1536 .err_handler = udp_err,
1551 .no_policy = 1, 1537 .no_policy = 1,
@@ -1646,6 +1632,13 @@ static struct packet_offload ip_packet_offload __read_mostly = {
1646 }, 1632 },
1647}; 1633};
1648 1634
1635static const struct net_offload ipip_offload = {
1636 .callbacks = {
1637 .gso_send_check = inet_gso_send_check,
1638 .gso_segment = inet_gso_segment,
1639 },
1640};
1641
1649static int __init ipv4_offload_init(void) 1642static int __init ipv4_offload_init(void)
1650{ 1643{
1651 /* 1644 /*
@@ -1657,6 +1650,7 @@ static int __init ipv4_offload_init(void)
1657 pr_crit("%s: Cannot add TCP protocol offload\n", __func__); 1650 pr_crit("%s: Cannot add TCP protocol offload\n", __func__);
1658 1651
1659 dev_add_offload(&ip_packet_offload); 1652 dev_add_offload(&ip_packet_offload);
1653 inet_add_offload(&ipip_offload, IPPROTO_IPIP);
1660 return 0; 1654 return 0;
1661} 1655}
1662 1656
@@ -1705,8 +1699,6 @@ static int __init inet_init(void)
1705 ip_static_sysctl_init(); 1699 ip_static_sysctl_init();
1706#endif 1700#endif
1707 1701
1708 tcp_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
1709
1710 /* 1702 /*
1711 * Add all the base protocols. 1703 * Add all the base protocols.
1712 */ 1704 */
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_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..ec9a9ef4ce50 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 {
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..e5d436188464 100644
--- a/net/ipv4/gre_offload.c
+++ b/net/ipv4/gre_offload.c
@@ -39,7 +39,8 @@ static struct sk_buff *gre_gso_segment(struct sk_buff *skb,
39 SKB_GSO_UDP | 39 SKB_GSO_UDP |
40 SKB_GSO_DODGY | 40 SKB_GSO_DODGY |
41 SKB_GSO_TCP_ECN | 41 SKB_GSO_TCP_ECN |
42 SKB_GSO_GRE))) 42 SKB_GSO_GRE |
43 SKB_GSO_IPIP)))
43 goto out; 44 goto out;
44 45
45 if (unlikely(!pskb_may_pull(skb, sizeof(*greh)))) 46 if (unlikely(!pskb_may_pull(skb, sizeof(*greh))))
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..56a964a553d2 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -121,13 +121,13 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
121 121
122#if IS_ENABLED(CONFIG_IPV6) 122#if IS_ENABLED(CONFIG_IPV6)
123 if (r->idiag_family == AF_INET6) { 123 if (r->idiag_family == AF_INET6) {
124 const struct ipv6_pinfo *np = inet6_sk(sk);
125 124
126 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr; 125 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
127 *(struct in6_addr *)r->id.idiag_dst = np->daddr; 126 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
128 127
129 if (ext & (1 << (INET_DIAG_TCLASS - 1))) 128 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
130 if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0) 129 if (nla_put_u8(skb, INET_DIAG_TCLASS,
130 inet6_sk(sk)->tclass) < 0)
131 goto errout; 131 goto errout;
132 } 132 }
133#endif 133#endif
@@ -222,7 +222,7 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
222 u32 portid, u32 seq, u16 nlmsg_flags, 222 u32 portid, u32 seq, u16 nlmsg_flags,
223 const struct nlmsghdr *unlh) 223 const struct nlmsghdr *unlh)
224{ 224{
225 long tmo; 225 s32 tmo;
226 struct inet_diag_msg *r; 226 struct inet_diag_msg *r;
227 struct nlmsghdr *nlh; 227 struct nlmsghdr *nlh;
228 228
@@ -234,7 +234,7 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
234 r = nlmsg_data(nlh); 234 r = nlmsg_data(nlh);
235 BUG_ON(tw->tw_state != TCP_TIME_WAIT); 235 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
236 236
237 tmo = tw->tw_ttd - jiffies; 237 tmo = tw->tw_ttd - inet_tw_time_stamp();
238 if (tmo < 0) 238 if (tmo < 0)
239 tmo = 0; 239 tmo = 0;
240 240
@@ -248,18 +248,15 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
248 r->id.idiag_dst[0] = tw->tw_daddr; 248 r->id.idiag_dst[0] = tw->tw_daddr;
249 r->idiag_state = tw->tw_substate; 249 r->idiag_state = tw->tw_substate;
250 r->idiag_timer = 3; 250 r->idiag_timer = 3;
251 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ); 251 r->idiag_expires = jiffies_to_msecs(tmo);
252 r->idiag_rqueue = 0; 252 r->idiag_rqueue = 0;
253 r->idiag_wqueue = 0; 253 r->idiag_wqueue = 0;
254 r->idiag_uid = 0; 254 r->idiag_uid = 0;
255 r->idiag_inode = 0; 255 r->idiag_inode = 0;
256#if IS_ENABLED(CONFIG_IPV6) 256#if IS_ENABLED(CONFIG_IPV6)
257 if (tw->tw_family == AF_INET6) { 257 if (tw->tw_family == AF_INET6) {
258 const struct inet6_timewait_sock *tw6 = 258 *(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr;
259 inet6_twsk((struct sock *)tw); 259 *(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 } 260 }
264#endif 261#endif
265 262
@@ -273,10 +270,11 @@ static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
273 const struct nlmsghdr *unlh) 270 const struct nlmsghdr *unlh)
274{ 271{
275 if (sk->sk_state == TCP_TIME_WAIT) 272 if (sk->sk_state == TCP_TIME_WAIT)
276 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk, 273 return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq,
277 skb, r, portid, seq, nlmsg_flags, 274 nlmsg_flags, unlh);
278 unlh); 275
279 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, nlmsg_flags, unlh); 276 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
277 nlmsg_flags, unlh);
280} 278}
281 279
282int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, 280int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
@@ -338,12 +336,9 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_s
338 err = 0; 336 err = 0;
339 337
340out: 338out:
341 if (sk) { 339 if (sk)
342 if (sk->sk_state == TCP_TIME_WAIT) 340 sock_gen_put(sk);
343 inet_twsk_put((struct inet_timewait_sock *)sk); 341
344 else
345 sock_put(sk);
346 }
347out_nosk: 342out_nosk:
348 return err; 343 return err;
349} 344}
@@ -489,10 +484,9 @@ int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
489 entry.family = sk->sk_family; 484 entry.family = sk->sk_family;
490#if IS_ENABLED(CONFIG_IPV6) 485#if IS_ENABLED(CONFIG_IPV6)
491 if (entry.family == AF_INET6) { 486 if (entry.family == AF_INET6) {
492 struct ipv6_pinfo *np = inet6_sk(sk);
493 487
494 entry.saddr = np->rcv_saddr.s6_addr32; 488 entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
495 entry.daddr = np->daddr.s6_addr32; 489 entry.daddr = sk->sk_v6_daddr.s6_addr32;
496 } else 490 } else
497#endif 491#endif
498 { 492 {
@@ -635,22 +629,22 @@ static int inet_csk_diag_dump(struct sock *sk,
635 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 629 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
636} 630}
637 631
638static int inet_twsk_diag_dump(struct inet_timewait_sock *tw, 632static int inet_twsk_diag_dump(struct sock *sk,
639 struct sk_buff *skb, 633 struct sk_buff *skb,
640 struct netlink_callback *cb, 634 struct netlink_callback *cb,
641 struct inet_diag_req_v2 *r, 635 struct inet_diag_req_v2 *r,
642 const struct nlattr *bc) 636 const struct nlattr *bc)
643{ 637{
638 struct inet_timewait_sock *tw = inet_twsk(sk);
639
644 if (bc != NULL) { 640 if (bc != NULL) {
645 struct inet_diag_entry entry; 641 struct inet_diag_entry entry;
646 642
647 entry.family = tw->tw_family; 643 entry.family = tw->tw_family;
648#if IS_ENABLED(CONFIG_IPV6) 644#if IS_ENABLED(CONFIG_IPV6)
649 if (tw->tw_family == AF_INET6) { 645 if (tw->tw_family == AF_INET6) {
650 struct inet6_timewait_sock *tw6 = 646 entry.saddr = tw->tw_v6_rcv_saddr.s6_addr32;
651 inet6_twsk((struct sock *)tw); 647 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 648 } else
655#endif 649#endif
656 { 650 {
@@ -682,12 +676,12 @@ static inline void inet_diag_req_addrs(const struct sock *sk,
682#if IS_ENABLED(CONFIG_IPV6) 676#if IS_ENABLED(CONFIG_IPV6)
683 if (sk->sk_family == AF_INET6) { 677 if (sk->sk_family == AF_INET6) {
684 if (req->rsk_ops->family == AF_INET6) { 678 if (req->rsk_ops->family == AF_INET6) {
685 entry->saddr = inet6_rsk(req)->loc_addr.s6_addr32; 679 entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
686 entry->daddr = inet6_rsk(req)->rmt_addr.s6_addr32; 680 entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
687 } else if (req->rsk_ops->family == AF_INET) { 681 } else if (req->rsk_ops->family == AF_INET) {
688 ipv6_addr_set_v4mapped(ireq->loc_addr, 682 ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
689 &entry->saddr_storage); 683 &entry->saddr_storage);
690 ipv6_addr_set_v4mapped(ireq->rmt_addr, 684 ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
691 &entry->daddr_storage); 685 &entry->daddr_storage);
692 entry->saddr = entry->saddr_storage.s6_addr32; 686 entry->saddr = entry->saddr_storage.s6_addr32;
693 entry->daddr = entry->daddr_storage.s6_addr32; 687 entry->daddr = entry->daddr_storage.s6_addr32;
@@ -695,8 +689,8 @@ static inline void inet_diag_req_addrs(const struct sock *sk,
695 } else 689 } else
696#endif 690#endif
697 { 691 {
698 entry->saddr = &ireq->loc_addr; 692 entry->saddr = &ireq->ir_loc_addr;
699 entry->daddr = &ireq->rmt_addr; 693 entry->daddr = &ireq->ir_rmt_addr;
700 } 694 }
701} 695}
702 696
@@ -731,9 +725,9 @@ static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
731 tmo = 0; 725 tmo = 0;
732 726
733 r->id.idiag_sport = inet->inet_sport; 727 r->id.idiag_sport = inet->inet_sport;
734 r->id.idiag_dport = ireq->rmt_port; 728 r->id.idiag_dport = ireq->ir_rmt_port;
735 r->id.idiag_src[0] = ireq->loc_addr; 729 r->id.idiag_src[0] = ireq->ir_loc_addr;
736 r->id.idiag_dst[0] = ireq->rmt_addr; 730 r->id.idiag_dst[0] = ireq->ir_rmt_addr;
737 r->idiag_expires = jiffies_to_msecs(tmo); 731 r->idiag_expires = jiffies_to_msecs(tmo);
738 r->idiag_rqueue = 0; 732 r->idiag_rqueue = 0;
739 r->idiag_wqueue = 0; 733 r->idiag_wqueue = 0;
@@ -792,13 +786,13 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
792 786
793 if (reqnum < s_reqnum) 787 if (reqnum < s_reqnum)
794 continue; 788 continue;
795 if (r->id.idiag_dport != ireq->rmt_port && 789 if (r->id.idiag_dport != ireq->ir_rmt_port &&
796 r->id.idiag_dport) 790 r->id.idiag_dport)
797 continue; 791 continue;
798 792
799 if (bc) { 793 if (bc) {
800 inet_diag_req_addrs(sk, req, &entry); 794 inet_diag_req_addrs(sk, req, &entry);
801 entry.dport = ntohs(ireq->rmt_port); 795 entry.dport = ntohs(ireq->ir_rmt_port);
802 796
803 if (!inet_diag_bc_run(bc, &entry)) 797 if (!inet_diag_bc_run(bc, &entry))
804 continue; 798 continue;
@@ -911,8 +905,7 @@ skip_listen_ht:
911 905
912 num = 0; 906 num = 0;
913 907
914 if (hlist_nulls_empty(&head->chain) && 908 if (hlist_nulls_empty(&head->chain))
915 hlist_nulls_empty(&head->twchain))
916 continue; 909 continue;
917 910
918 if (i > s_i) 911 if (i > s_i)
@@ -920,7 +913,7 @@ skip_listen_ht:
920 913
921 spin_lock_bh(lock); 914 spin_lock_bh(lock);
922 sk_nulls_for_each(sk, node, &head->chain) { 915 sk_nulls_for_each(sk, node, &head->chain) {
923 struct inet_sock *inet = inet_sk(sk); 916 int res;
924 917
925 if (!net_eq(sock_net(sk), net)) 918 if (!net_eq(sock_net(sk), net))
926 continue; 919 continue;
@@ -929,15 +922,19 @@ skip_listen_ht:
929 if (!(r->idiag_states & (1 << sk->sk_state))) 922 if (!(r->idiag_states & (1 << sk->sk_state)))
930 goto next_normal; 923 goto next_normal;
931 if (r->sdiag_family != AF_UNSPEC && 924 if (r->sdiag_family != AF_UNSPEC &&
932 sk->sk_family != r->sdiag_family) 925 sk->sk_family != r->sdiag_family)
933 goto next_normal; 926 goto next_normal;
934 if (r->id.idiag_sport != inet->inet_sport && 927 if (r->id.idiag_sport != htons(sk->sk_num) &&
935 r->id.idiag_sport) 928 r->id.idiag_sport)
936 goto next_normal; 929 goto next_normal;
937 if (r->id.idiag_dport != inet->inet_dport && 930 if (r->id.idiag_dport != sk->sk_dport &&
938 r->id.idiag_dport) 931 r->id.idiag_dport)
939 goto next_normal; 932 goto next_normal;
940 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) { 933 if (sk->sk_state == TCP_TIME_WAIT)
934 res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
935 else
936 res = inet_csk_diag_dump(sk, skb, cb, r, bc);
937 if (res < 0) {
941 spin_unlock_bh(lock); 938 spin_unlock_bh(lock);
942 goto done; 939 goto done;
943 } 940 }
@@ -945,33 +942,6 @@ next_normal:
945 ++num; 942 ++num;
946 } 943 }
947 944
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); 945 spin_unlock_bh(lock);
976 } 946 }
977 947
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_output.c b/net/ipv4/ip_output.c
index 3982eabf61e1..912402752f2f 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,8 +823,10 @@ 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-exthdrlen);
830 return -EMSGSIZE; 832 return -EMSGSIZE;
@@ -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,8 +1147,10 @@ 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, mtu);
1149 return -EMSGSIZE; 1155 return -EMSGSIZE;
1150 } 1156 }
@@ -1308,7 +1314,8 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1308 /* DF bit is set when we want to see DF on outgoing frames. 1314 /* 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 1315 * If local_df is set too, we still allow to fragment this frame
1310 * locally. */ 1316 * locally. */
1311 if (inet->pmtudisc >= IP_PMTUDISC_DO || 1317 if (inet->pmtudisc == IP_PMTUDISC_DO ||
1318 inet->pmtudisc == IP_PMTUDISC_PROBE ||
1312 (skb->len <= dst_mtu(&rt->dst) && 1319 (skb->len <= dst_mtu(&rt->dst) &&
1313 ip_dont_fragment(sk, &rt->dst))) 1320 ip_dont_fragment(sk, &rt->dst)))
1314 df = htons(IP_DF); 1321 df = htons(IP_DF);
@@ -1316,7 +1323,9 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1316 if (cork->flags & IPCORK_OPT) 1323 if (cork->flags & IPCORK_OPT)
1317 opt = cork->opt; 1324 opt = cork->opt;
1318 1325
1319 if (rt->rt_type == RTN_MULTICAST) 1326 if (cork->ttl != 0)
1327 ttl = cork->ttl;
1328 else if (rt->rt_type == RTN_MULTICAST)
1320 ttl = inet->mc_ttl; 1329 ttl = inet->mc_ttl;
1321 else 1330 else
1322 ttl = ip_select_ttl(inet, &rt->dst); 1331 ttl = ip_select_ttl(inet, &rt->dst);
@@ -1324,7 +1333,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1324 iph = ip_hdr(skb); 1333 iph = ip_hdr(skb);
1325 iph->version = 4; 1334 iph->version = 4;
1326 iph->ihl = 5; 1335 iph->ihl = 5;
1327 iph->tos = inet->tos; 1336 iph->tos = (cork->tos != -1) ? cork->tos : inet->tos;
1328 iph->frag_off = df; 1337 iph->frag_off = df;
1329 iph->ttl = ttl; 1338 iph->ttl = ttl;
1330 iph->protocol = sk->sk_protocol; 1339 iph->protocol = sk->sk_protocol;
@@ -1336,7 +1345,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1336 ip_options_build(skb, opt, cork->addr, rt, 0); 1345 ip_options_build(skb, opt, cork->addr, rt, 0);
1337 } 1346 }
1338 1347
1339 skb->priority = sk->sk_priority; 1348 skb->priority = (cork->tos != -1) ? cork->priority: sk->sk_priority;
1340 skb->mark = sk->sk_mark; 1349 skb->mark = sk->sk_mark;
1341 /* 1350 /*
1342 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec 1351 * Steal rt from cork.dst to avoid a pair of atomic_inc/atomic_dec
@@ -1486,6 +1495,8 @@ void ip_send_unicast_reply(struct net *net, struct sk_buff *skb, __be32 daddr,
1486 ipc.addr = daddr; 1495 ipc.addr = daddr;
1487 ipc.opt = NULL; 1496 ipc.opt = NULL;
1488 ipc.tx_flags = 0; 1497 ipc.tx_flags = 0;
1498 ipc.ttl = 0;
1499 ipc.tos = -1;
1489 1500
1490 if (replyopts.opt.opt.optlen) { 1501 if (replyopts.opt.opt.optlen) {
1491 ipc.opt = &replyopts.opt; 1502 ipc.opt = &replyopts.opt;
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index d9c4f113d709..3f858266fa7e 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 }
@@ -609,7 +627,7 @@ static int do_ip_setsockopt(struct sock *sk, int level,
609 inet->nodefrag = val ? 1 : 0; 627 inet->nodefrag = val ? 1 : 0;
610 break; 628 break;
611 case IP_MTU_DISCOVER: 629 case IP_MTU_DISCOVER:
612 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE) 630 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_INTERFACE)
613 goto e_inval; 631 goto e_inval;
614 inet->pmtudisc = val; 632 inet->pmtudisc = val;
615 break; 633 break;
@@ -1034,11 +1052,12 @@ e_inval:
1034 * destination in skb->cb[] before dst drop. 1052 * destination in skb->cb[] before dst drop.
1035 * This way, receiver doesnt make cache line misses to read rtable. 1053 * This way, receiver doesnt make cache line misses to read rtable.
1036 */ 1054 */
1037void ipv4_pktinfo_prepare(struct sk_buff *skb) 1055void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1038{ 1056{
1039 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); 1057 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1040 1058
1041 if (skb_rtable(skb)) { 1059 if ((inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) &&
1060 skb_rtable(skb)) {
1042 pktinfo->ipi_ifindex = inet_iif(skb); 1061 pktinfo->ipi_ifindex = inet_iif(skb);
1043 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); 1062 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1044 } else { 1063 } else {
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..5d9c845d288a 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{
@@ -304,9 +240,8 @@ static void __net_init vti_fb_tunnel_init(struct net_device *dev)
304 iph->ihl = 5; 240 iph->ihl = 5;
305} 241}
306 242
307static struct xfrm_tunnel vti_handler __read_mostly = { 243static struct xfrm_tunnel_notifier vti_handler __read_mostly = {
308 .handler = vti_rcv, 244 .handler = vti_rcv,
309 .err_handler = vti_err,
310 .priority = 1, 245 .priority = 1,
311}; 246};
312 247
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/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/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/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..01cffeaa0085 100644
--- a/net/ipv4/netfilter/ipt_SYNPROXY.c
+++ b/net/ipv4/netfilter/ipt_SYNPROXY.c
@@ -297,7 +297,7 @@ synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
297 return XT_CONTINUE; 297 return XT_CONTINUE;
298} 298}
299 299
300static unsigned int ipv4_synproxy_hook(unsigned int hooknum, 300static unsigned int ipv4_synproxy_hook(const struct nf_hook_ops *ops,
301 struct sk_buff *skb, 301 struct sk_buff *skb,
302 const struct net_device *in, 302 const struct net_device *in,
303 const struct net_device *out, 303 const struct net_device *out,
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..fff5ba1a33b7
--- /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, 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..9afbdb19f4a2 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)) {
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index 193db03540ad..41e1d2845c8f 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
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index 6011615e810d..f428935c50db 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) {
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..8e8529d3c8c9 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
@@ -3097,13 +3099,13 @@ static int __init set_thash_entries(char *str)
3097} 3099}
3098__setup("thash_entries=", set_thash_entries); 3100__setup("thash_entries=", set_thash_entries);
3099 3101
3100void tcp_init_mem(struct net *net) 3102static void tcp_init_mem(void)
3101{ 3103{
3102 unsigned long limit = nr_free_buffer_pages() / 8; 3104 unsigned long limit = nr_free_buffer_pages() / 8;
3103 limit = max(limit, 128UL); 3105 limit = max(limit, 128UL);
3104 net->ipv4.sysctl_tcp_mem[0] = limit / 4 * 3; 3106 sysctl_tcp_mem[0] = limit / 4 * 3;
3105 net->ipv4.sysctl_tcp_mem[1] = limit; 3107 sysctl_tcp_mem[1] = limit;
3106 net->ipv4.sysctl_tcp_mem[2] = net->ipv4.sysctl_tcp_mem[0] * 2; 3108 sysctl_tcp_mem[2] = sysctl_tcp_mem[0] * 2;
3107} 3109}
3108 3110
3109void __init tcp_init(void) 3111void __init tcp_init(void)
@@ -3137,10 +3139,9 @@ void __init tcp_init(void)
3137 &tcp_hashinfo.ehash_mask, 3139 &tcp_hashinfo.ehash_mask,
3138 0, 3140 0,
3139 thash_entries ? 0 : 512 * 1024); 3141 thash_entries ? 0 : 512 * 1024);
3140 for (i = 0; i <= tcp_hashinfo.ehash_mask; i++) { 3142 for (i = 0; i <= tcp_hashinfo.ehash_mask; i++)
3141 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i); 3143 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].chain, i);
3142 INIT_HLIST_NULLS_HEAD(&tcp_hashinfo.ehash[i].twchain, i); 3144
3143 }
3144 if (inet_ehash_locks_alloc(&tcp_hashinfo)) 3145 if (inet_ehash_locks_alloc(&tcp_hashinfo))
3145 panic("TCP: failed to alloc ehash_locks"); 3146 panic("TCP: failed to alloc ehash_locks");
3146 tcp_hashinfo.bhash = 3147 tcp_hashinfo.bhash =
@@ -3166,7 +3167,7 @@ void __init tcp_init(void)
3166 sysctl_tcp_max_orphans = cnt / 2; 3167 sysctl_tcp_max_orphans = cnt / 2;
3167 sysctl_max_syn_backlog = max(128, cnt / 256); 3168 sysctl_max_syn_backlog = max(128, cnt / 256);
3168 3169
3169 tcp_init_mem(&init_net); 3170 tcp_init_mem();
3170 /* Set per-socket limits to no more than 1/128 the pressure threshold */ 3171 /* Set per-socket limits to no more than 1/128 the pressure threshold */
3171 limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7); 3172 limit = nr_free_buffer_pages() << (PAGE_SHIFT - 7);
3172 max_wshare = min(4UL*1024*1024, limit); 3173 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 068c8fb0d158..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;
525 553
526 space = max(tp->rcvq_space.space, space); 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 */
527 562
528 if (tp->rcvq_space.space != space) { 563 if (sysctl_tcp_moderate_rcvbuf &&
529 int rcvmem; 564 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
565 int rcvwin, rcvmem, rcvbuf;
530 566
531 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;
532 571
533 if (sysctl_tcp_moderate_rcvbuf && 572 /* If rate increased by 25%,
534 !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) { 573 * assume slow start, rcvwin = 3 * copied
535 int new_clamp = space; 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 }
536 585
537 /* Receive space grows, normalize in order to 586 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
538 * take into account packet headers and sk_buff 587 while (tcp_win_from_space(rcvmem) < tp->advmss)
539 * structure overhead. 588 rcvmem += 128;
540 */ 589
541 space /= tp->advmss; 590 rcvbuf = min(rcvwin / tp->advmss * rcvmem, sysctl_tcp_rmem[2]);
542 if (!space) 591 if (rcvbuf > sk->sk_rcvbuf) {
543 space = 1; 592 sk->sk_rcvbuf = rcvbuf;
544 rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER); 593
545 while (tcp_win_from_space(rcvmem) < tp->advmss) 594 /* Make the window clamp follow along. */
546 rcvmem += 128; 595 tp->window_clamp = rcvwin;
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
@@ -2887,10 +2934,10 @@ static void tcp_synack_rtt_meas(struct sock *sk, const u32 synack_stamp)
2887 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt, -1); 2934 tcp_ack_update_rtt(sk, FLAG_SYN_ACKED, seq_rtt, -1);
2888} 2935}
2889 2936
2890static 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)
2891{ 2938{
2892 const struct inet_connection_sock *icsk = inet_csk(sk); 2939 const struct inet_connection_sock *icsk = inet_csk(sk);
2893 icsk->icsk_ca_ops->cong_avoid(sk, ack, in_flight); 2940 icsk->icsk_ca_ops->cong_avoid(sk, ack, acked, in_flight);
2894 tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp; 2941 tcp_sk(sk)->snd_cwnd_stamp = tcp_time_stamp;
2895} 2942}
2896 2943
@@ -2979,7 +3026,7 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
2979 const struct inet_connection_sock *icsk = inet_csk(sk); 3026 const struct inet_connection_sock *icsk = inet_csk(sk);
2980 struct sk_buff *skb; 3027 struct sk_buff *skb;
2981 u32 now = tcp_time_stamp; 3028 u32 now = tcp_time_stamp;
2982 int fully_acked = true; 3029 bool fully_acked = true;
2983 int flag = 0; 3030 int flag = 0;
2984 u32 pkts_acked = 0; 3031 u32 pkts_acked = 0;
2985 u32 reord = tp->packets_out; 3032 u32 reord = tp->packets_out;
@@ -3407,7 +3454,7 @@ static int tcp_ack(struct sock *sk, const struct sk_buff *skb, int flag)
3407 3454
3408 /* Advance cwnd if state allows */ 3455 /* Advance cwnd if state allows */
3409 if (tcp_may_raise_cwnd(sk, flag)) 3456 if (tcp_may_raise_cwnd(sk, flag))
3410 tcp_cong_avoid(sk, ack, prior_in_flight); 3457 tcp_cong_avoid(sk, ack, acked, prior_in_flight);
3411 3458
3412 if (tcp_ack_is_dubious(sk, flag)) { 3459 if (tcp_ack_is_dubious(sk, flag)) {
3413 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP)); 3460 is_dupack = !(flag & (FLAG_SND_UNA_ADVANCED | FLAG_NOT_DUP));
@@ -4717,15 +4764,7 @@ static void tcp_new_space(struct sock *sk)
4717 struct tcp_sock *tp = tcp_sk(sk); 4764 struct tcp_sock *tp = tcp_sk(sk);
4718 4765
4719 if (tcp_should_expand_sndbuf(sk)) { 4766 if (tcp_should_expand_sndbuf(sk)) {
4720 int sndmem = SKB_TRUESIZE(max_t(u32, 4767 tcp_sndbuf_expand(sk);
4721 tp->rx_opt.mss_clamp,
4722 tp->mss_cache) +
4723 MAX_TCP_HEADER);
4724 int demanded = max_t(unsigned int, tp->snd_cwnd,
4725 tp->reordering + 1);
4726 sndmem *= 2 * demanded;
4727 if (sndmem > sk->sk_sndbuf)
4728 sk->sk_sndbuf = min(sndmem, sysctl_tcp_wmem[2]);
4729 tp->snd_cwnd_stamp = tcp_time_stamp; 4768 tp->snd_cwnd_stamp = tcp_time_stamp;
4730 } 4769 }
4731 4770
@@ -5693,8 +5732,8 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5693 tcp_init_congestion_control(sk); 5732 tcp_init_congestion_control(sk);
5694 5733
5695 tcp_mtup_init(sk); 5734 tcp_mtup_init(sk);
5696 tcp_init_buffer_space(sk);
5697 tp->copied_seq = tp->rcv_nxt; 5735 tp->copied_seq = tp->rcv_nxt;
5736 tcp_init_buffer_space(sk);
5698 } 5737 }
5699 smp_mb(); 5738 smp_mb();
5700 tcp_set_state(sk, TCP_ESTABLISHED); 5739 tcp_set_state(sk, TCP_ESTABLISHED);
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index b14266bb91eb..14bba8a1c5a7 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -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));
@@ -2606,10 +2549,10 @@ static void get_openreq4(const struct sock *sk, const struct request_sock *req,
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%n",
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) */
@@ -2707,6 +2650,7 @@ static void get_timewait4_sock(const struct inet_timewait_sock *tw,
2707static int tcp4_seq_show(struct seq_file *seq, void *v) 2650static int tcp4_seq_show(struct seq_file *seq, void *v)
2708{ 2651{
2709 struct tcp_iter_state *st; 2652 struct tcp_iter_state *st;
2653 struct sock *sk = v;
2710 int len; 2654 int len;
2711 2655
2712 if (v == SEQ_START_TOKEN) { 2656 if (v == SEQ_START_TOKEN) {
@@ -2721,14 +2665,14 @@ static int tcp4_seq_show(struct seq_file *seq, void *v)
2721 switch (st->state) { 2665 switch (st->state) {
2722 case TCP_SEQ_STATE_LISTENING: 2666 case TCP_SEQ_STATE_LISTENING:
2723 case TCP_SEQ_STATE_ESTABLISHED: 2667 case TCP_SEQ_STATE_ESTABLISHED:
2724 get_tcp4_sock(v, seq, st->num, &len); 2668 if (sk->sk_state == TCP_TIME_WAIT)
2669 get_timewait4_sock(v, seq, st->num, &len);
2670 else
2671 get_tcp4_sock(v, seq, st->num, &len);
2725 break; 2672 break;
2726 case TCP_SEQ_STATE_OPENREQ: 2673 case TCP_SEQ_STATE_OPENREQ:
2727 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len); 2674 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid, &len);
2728 break; 2675 break;
2729 case TCP_SEQ_STATE_TIME_WAIT:
2730 get_timewait4_sock(v, seq, st->num, &len);
2731 break;
2732 } 2676 }
2733 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, ""); 2677 seq_printf(seq, "%*s\n", TMPSZ - 1 - len, "");
2734out: 2678out:
@@ -2806,6 +2750,7 @@ struct proto tcp_prot = {
2806 .orphan_count = &tcp_orphan_count, 2750 .orphan_count = &tcp_orphan_count,
2807 .memory_allocated = &tcp_memory_allocated, 2751 .memory_allocated = &tcp_memory_allocated,
2808 .memory_pressure = &tcp_memory_pressure, 2752 .memory_pressure = &tcp_memory_pressure,
2753 .sysctl_mem = sysctl_tcp_mem,
2809 .sysctl_wmem = sysctl_tcp_wmem, 2754 .sysctl_wmem = sysctl_tcp_wmem,
2810 .sysctl_rmem = sysctl_tcp_rmem, 2755 .sysctl_rmem = sysctl_tcp_rmem,
2811 .max_header = MAX_TCP_HEADER, 2756 .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..03e9154f7e68 100644
--- a/net/ipv4/tcp_memcontrol.c
+++ b/net/ipv4/tcp_memcontrol.c
@@ -6,15 +6,10 @@
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) 9static void memcg_tcp_enter_memory_pressure(struct sock *sk)
15{ 10{
16 if (sk->sk_cgrp->memory_pressure) 11 if (sk->sk_cgrp->memory_pressure)
17 *sk->sk_cgrp->memory_pressure = 1; 12 sk->sk_cgrp->memory_pressure = 1;
18} 13}
19EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure); 14EXPORT_SYMBOL(memcg_tcp_enter_memory_pressure);
20 15
@@ -27,34 +22,24 @@ int tcp_init_cgroup(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
27 */ 22 */
28 struct res_counter *res_parent = NULL; 23 struct res_counter *res_parent = NULL;
29 struct cg_proto *cg_proto, *parent_cg; 24 struct cg_proto *cg_proto, *parent_cg;
30 struct tcp_memcontrol *tcp;
31 struct mem_cgroup *parent = parent_mem_cgroup(memcg); 25 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
32 struct net *net = current->nsproxy->net_ns;
33 26
34 cg_proto = tcp_prot.proto_cgroup(memcg); 27 cg_proto = tcp_prot.proto_cgroup(memcg);
35 if (!cg_proto) 28 if (!cg_proto)
36 return 0; 29 return 0;
37 30
38 tcp = tcp_from_cgproto(cg_proto); 31 cg_proto->sysctl_mem[0] = sysctl_tcp_mem[0];
39 32 cg_proto->sysctl_mem[1] = sysctl_tcp_mem[1];
40 tcp->tcp_prot_mem[0] = net->ipv4.sysctl_tcp_mem[0]; 33 cg_proto->sysctl_mem[2] = sysctl_tcp_mem[2];
41 tcp->tcp_prot_mem[1] = net->ipv4.sysctl_tcp_mem[1]; 34 cg_proto->memory_pressure = 0;
42 tcp->tcp_prot_mem[2] = net->ipv4.sysctl_tcp_mem[2]; 35 cg_proto->memcg = memcg;
43 tcp->tcp_memory_pressure = 0;
44 36
45 parent_cg = tcp_prot.proto_cgroup(parent); 37 parent_cg = tcp_prot.proto_cgroup(parent);
46 if (parent_cg) 38 if (parent_cg)
47 res_parent = parent_cg->memory_allocated; 39 res_parent = &parent_cg->memory_allocated;
48
49 res_counter_init(&tcp->tcp_memory_allocated, res_parent);
50 percpu_counter_init(&tcp->tcp_sockets_allocated, 0);
51 40
52 cg_proto->enter_memory_pressure = memcg_tcp_enter_memory_pressure; 41 res_counter_init(&cg_proto->memory_allocated, res_parent);
53 cg_proto->memory_pressure = &tcp->tcp_memory_pressure; 42 percpu_counter_init(&cg_proto->sockets_allocated, 0);
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 43
59 return 0; 44 return 0;
60} 45}
@@ -63,21 +48,17 @@ EXPORT_SYMBOL(tcp_init_cgroup);
63void tcp_destroy_cgroup(struct mem_cgroup *memcg) 48void tcp_destroy_cgroup(struct mem_cgroup *memcg)
64{ 49{
65 struct cg_proto *cg_proto; 50 struct cg_proto *cg_proto;
66 struct tcp_memcontrol *tcp;
67 51
68 cg_proto = tcp_prot.proto_cgroup(memcg); 52 cg_proto = tcp_prot.proto_cgroup(memcg);
69 if (!cg_proto) 53 if (!cg_proto)
70 return; 54 return;
71 55
72 tcp = tcp_from_cgproto(cg_proto); 56 percpu_counter_destroy(&cg_proto->sockets_allocated);
73 percpu_counter_destroy(&tcp->tcp_sockets_allocated);
74} 57}
75EXPORT_SYMBOL(tcp_destroy_cgroup); 58EXPORT_SYMBOL(tcp_destroy_cgroup);
76 59
77static int tcp_update_limit(struct mem_cgroup *memcg, u64 val) 60static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
78{ 61{
79 struct net *net = current->nsproxy->net_ns;
80 struct tcp_memcontrol *tcp;
81 struct cg_proto *cg_proto; 62 struct cg_proto *cg_proto;
82 u64 old_lim; 63 u64 old_lim;
83 int i; 64 int i;
@@ -90,16 +71,14 @@ static int tcp_update_limit(struct mem_cgroup *memcg, u64 val)
90 if (val > RES_COUNTER_MAX) 71 if (val > RES_COUNTER_MAX)
91 val = RES_COUNTER_MAX; 72 val = RES_COUNTER_MAX;
92 73
93 tcp = tcp_from_cgproto(cg_proto); 74 old_lim = res_counter_read_u64(&cg_proto->memory_allocated, RES_LIMIT);
94 75 ret = res_counter_set_limit(&cg_proto->memory_allocated, val);
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) 76 if (ret)
98 return ret; 77 return ret;
99 78
100 for (i = 0; i < 3; i++) 79 for (i = 0; i < 3; i++)
101 tcp->tcp_prot_mem[i] = min_t(long, val >> PAGE_SHIFT, 80 cg_proto->sysctl_mem[i] = min_t(long, val >> PAGE_SHIFT,
102 net->ipv4.sysctl_tcp_mem[i]); 81 sysctl_tcp_mem[i]);
103 82
104 if (val == RES_COUNTER_MAX) 83 if (val == RES_COUNTER_MAX)
105 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags); 84 clear_bit(MEMCG_SOCK_ACTIVE, &cg_proto->flags);
@@ -156,28 +135,24 @@ static int tcp_cgroup_write(struct cgroup_subsys_state *css, struct cftype *cft,
156 135
157static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val) 136static u64 tcp_read_stat(struct mem_cgroup *memcg, int type, u64 default_val)
158{ 137{
159 struct tcp_memcontrol *tcp;
160 struct cg_proto *cg_proto; 138 struct cg_proto *cg_proto;
161 139
162 cg_proto = tcp_prot.proto_cgroup(memcg); 140 cg_proto = tcp_prot.proto_cgroup(memcg);
163 if (!cg_proto) 141 if (!cg_proto)
164 return default_val; 142 return default_val;
165 143
166 tcp = tcp_from_cgproto(cg_proto); 144 return res_counter_read_u64(&cg_proto->memory_allocated, type);
167 return res_counter_read_u64(&tcp->tcp_memory_allocated, type);
168} 145}
169 146
170static u64 tcp_read_usage(struct mem_cgroup *memcg) 147static u64 tcp_read_usage(struct mem_cgroup *memcg)
171{ 148{
172 struct tcp_memcontrol *tcp;
173 struct cg_proto *cg_proto; 149 struct cg_proto *cg_proto;
174 150
175 cg_proto = tcp_prot.proto_cgroup(memcg); 151 cg_proto = tcp_prot.proto_cgroup(memcg);
176 if (!cg_proto) 152 if (!cg_proto)
177 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT; 153 return atomic_long_read(&tcp_memory_allocated) << PAGE_SHIFT;
178 154
179 tcp = tcp_from_cgproto(cg_proto); 155 return res_counter_read_u64(&cg_proto->memory_allocated, RES_USAGE);
180 return res_counter_read_u64(&tcp->tcp_memory_allocated, RES_USAGE);
181} 156}
182 157
183static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft) 158static u64 tcp_cgroup_read(struct cgroup_subsys_state *css, struct cftype *cft)
@@ -205,54 +180,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) 180static int tcp_cgroup_reset(struct cgroup_subsys_state *css, unsigned int event)
206{ 181{
207 struct mem_cgroup *memcg; 182 struct mem_cgroup *memcg;
208 struct tcp_memcontrol *tcp;
209 struct cg_proto *cg_proto; 183 struct cg_proto *cg_proto;
210 184
211 memcg = mem_cgroup_from_css(css); 185 memcg = mem_cgroup_from_css(css);
212 cg_proto = tcp_prot.proto_cgroup(memcg); 186 cg_proto = tcp_prot.proto_cgroup(memcg);
213 if (!cg_proto) 187 if (!cg_proto)
214 return 0; 188 return 0;
215 tcp = tcp_from_cgproto(cg_proto);
216 189
217 switch (event) { 190 switch (event) {
218 case RES_MAX_USAGE: 191 case RES_MAX_USAGE:
219 res_counter_reset_max(&tcp->tcp_memory_allocated); 192 res_counter_reset_max(&cg_proto->memory_allocated);
220 break; 193 break;
221 case RES_FAILCNT: 194 case RES_FAILCNT:
222 res_counter_reset_failcnt(&tcp->tcp_memory_allocated); 195 res_counter_reset_failcnt(&cg_proto->memory_allocated);
223 break; 196 break;
224 } 197 }
225 198
226 return 0; 199 return 0;
227} 200}
228 201
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[] = { 202static struct cftype tcp_files[] = {
257 { 203 {
258 .name = "kmem.tcp.limit_in_bytes", 204 .name = "kmem.tcp.limit_in_bytes",
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index 52f3c6b971d2..2ab09cbae74d 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -215,13 +215,15 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
215 addr.family = req->rsk_ops->family; 215 addr.family = req->rsk_ops->family;
216 switch (addr.family) { 216 switch (addr.family) {
217 case AF_INET: 217 case AF_INET:
218 addr.addr.a4 = inet_rsk(req)->rmt_addr; 218 addr.addr.a4 = inet_rsk(req)->ir_rmt_addr;
219 hash = (__force unsigned int) addr.addr.a4; 219 hash = (__force unsigned int) addr.addr.a4;
220 break; 220 break;
221#if IS_ENABLED(CONFIG_IPV6)
221 case AF_INET6: 222 case AF_INET6:
222 *(struct in6_addr *)addr.addr.a6 = inet6_rsk(req)->rmt_addr; 223 *(struct in6_addr *)addr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr;
223 hash = ipv6_addr_hash(&inet6_rsk(req)->rmt_addr); 224 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
224 break; 225 break;
226#endif
225 default: 227 default:
226 return NULL; 228 return NULL;
227 } 229 }
@@ -240,7 +242,6 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
240 242
241static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw) 243static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock *tw)
242{ 244{
243 struct inet6_timewait_sock *tw6;
244 struct tcp_metrics_block *tm; 245 struct tcp_metrics_block *tm;
245 struct inetpeer_addr addr; 246 struct inetpeer_addr addr;
246 unsigned int hash; 247 unsigned int hash;
@@ -252,11 +253,12 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
252 addr.addr.a4 = tw->tw_daddr; 253 addr.addr.a4 = tw->tw_daddr;
253 hash = (__force unsigned int) addr.addr.a4; 254 hash = (__force unsigned int) addr.addr.a4;
254 break; 255 break;
256#if IS_ENABLED(CONFIG_IPV6)
255 case AF_INET6: 257 case AF_INET6:
256 tw6 = inet6_twsk((struct sock *)tw); 258 *(struct in6_addr *)addr.addr.a6 = tw->tw_v6_daddr;
257 *(struct in6_addr *)addr.addr.a6 = tw6->tw_v6_daddr; 259 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
258 hash = ipv6_addr_hash(&tw6->tw_v6_daddr);
259 break; 260 break;
261#endif
260 default: 262 default:
261 return NULL; 263 return NULL;
262 } 264 }
@@ -288,10 +290,12 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
288 addr.addr.a4 = inet_sk(sk)->inet_daddr; 290 addr.addr.a4 = inet_sk(sk)->inet_daddr;
289 hash = (__force unsigned int) addr.addr.a4; 291 hash = (__force unsigned int) addr.addr.a4;
290 break; 292 break;
293#if IS_ENABLED(CONFIG_IPV6)
291 case AF_INET6: 294 case AF_INET6:
292 *(struct in6_addr *)addr.addr.a6 = inet6_sk(sk)->daddr; 295 *(struct in6_addr *)addr.addr.a6 = sk->sk_v6_daddr;
293 hash = ipv6_addr_hash(&inet6_sk(sk)->daddr); 296 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
294 break; 297 break;
298#endif
295 default: 299 default:
296 return NULL; 300 return NULL;
297 } 301 }
@@ -667,8 +671,9 @@ void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
667 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen; 671 struct tcp_fastopen_metrics *tfom = &tm->tcpm_fastopen;
668 672
669 write_seqlock_bh(&fastopen_seqlock); 673 write_seqlock_bh(&fastopen_seqlock);
670 tfom->mss = mss; 674 if (mss)
671 if (cookie->len > 0) 675 tfom->mss = mss;
676 if (cookie && cookie->len > 0)
672 tfom->cookie = *cookie; 677 tfom->cookie = *cookie;
673 if (syn_lost) { 678 if (syn_lost) {
674 ++tfom->syn_loss; 679 ++tfom->syn_loss;
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 533c58a5cfb7..a2b68a108eae 100644
--- a/net/ipv4/tcp_offload.c
+++ b/net/ipv4/tcp_offload.c
@@ -14,7 +14,7 @@
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);
@@ -57,6 +57,8 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
57 SKB_GSO_TCP_ECN | 57 SKB_GSO_TCP_ECN |
58 SKB_GSO_TCPV6 | 58 SKB_GSO_TCPV6 |
59 SKB_GSO_GRE | 59 SKB_GSO_GRE |
60 SKB_GSO_IPIP |
61 SKB_GSO_SIT |
60 SKB_GSO_MPLS | 62 SKB_GSO_MPLS |
61 SKB_GSO_UDP_TUNNEL | 63 SKB_GSO_UDP_TUNNEL |
62 0) || 64 0) ||
@@ -136,7 +138,7 @@ struct sk_buff *tcp_tso_segment(struct sk_buff *skb,
136out: 138out:
137 return segs; 139 return segs;
138} 140}
139EXPORT_SYMBOL(tcp_tso_segment); 141EXPORT_SYMBOL(tcp_gso_segment);
140 142
141struct 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)
142{ 144{
@@ -317,7 +319,7 @@ static int tcp4_gro_complete(struct sk_buff *skb)
317static const struct net_offload tcpv4_offload = { 319static const struct net_offload tcpv4_offload = {
318 .callbacks = { 320 .callbacks = {
319 .gso_send_check = tcp_v4_gso_send_check, 321 .gso_send_check = tcp_v4_gso_send_check,
320 .gso_segment = tcp_tso_segment, 322 .gso_segment = tcp_gso_segment,
321 .gro_receive = tcp4_gro_receive, 323 .gro_receive = tcp4_gro_receive,
322 .gro_complete = tcp4_gro_complete, 324 .gro_complete = tcp4_gro_complete,
323 }, 325 },
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index d46f2143305c..672854664ff5 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),
@@ -2353,21 +2353,6 @@ int __tcp_retransmit_skb(struct sock *sk, struct sk_buff *skb)
2353 2353
2354 tcp_retrans_try_collapse(sk, skb, cur_mss); 2354 tcp_retrans_try_collapse(sk, skb, cur_mss);
2355 2355
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 2356 /* Make a copy, if the first transmission SKB clone we made
2372 * is still in somebody's hands, else make a clone. 2357 * is still in somebody's hands, else make a clone.
2373 */ 2358 */
@@ -2736,8 +2721,8 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2736 th->syn = 1; 2721 th->syn = 1;
2737 th->ack = 1; 2722 th->ack = 1;
2738 TCP_ECN_make_synack(req, th); 2723 TCP_ECN_make_synack(req, th);
2739 th->source = ireq->loc_port; 2724 th->source = htons(ireq->ir_num);
2740 th->dest = ireq->rmt_port; 2725 th->dest = ireq->ir_rmt_port;
2741 /* Setting of flags are superfluous here for callers (and ECE is 2726 /* Setting of flags are superfluous here for callers (and ECE is
2742 * not even correctly set) 2727 * not even correctly set)
2743 */ 2728 */
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..89909dd730dd 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) {
@@ -565,6 +578,26 @@ struct sock *udp4_lib_lookup(struct net *net, __be32 saddr, __be16 sport,
565} 578}
566EXPORT_SYMBOL_GPL(udp4_lib_lookup); 579EXPORT_SYMBOL_GPL(udp4_lib_lookup);
567 580
581static inline bool __udp_is_mcast_sock(struct net *net, struct sock *sk,
582 __be16 loc_port, __be32 loc_addr,
583 __be16 rmt_port, __be32 rmt_addr,
584 int dif, unsigned short hnum)
585{
586 struct inet_sock *inet = inet_sk(sk);
587
588 if (!net_eq(sock_net(sk), net) ||
589 udp_sk(sk)->udp_port_hash != hnum ||
590 (inet->inet_daddr && inet->inet_daddr != rmt_addr) ||
591 (inet->inet_dport != rmt_port && inet->inet_dport) ||
592 (inet->inet_rcv_saddr && inet->inet_rcv_saddr != loc_addr) ||
593 ipv6_only_sock(sk) ||
594 (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
595 return false;
596 if (!ip_mc_sf_allow(sk, loc_addr, rmt_addr, dif))
597 return false;
598 return true;
599}
600
568static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk, 601static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
569 __be16 loc_port, __be32 loc_addr, 602 __be16 loc_port, __be32 loc_addr,
570 __be16 rmt_port, __be32 rmt_addr, 603 __be16 rmt_port, __be32 rmt_addr,
@@ -575,20 +608,11 @@ static inline struct sock *udp_v4_mcast_next(struct net *net, struct sock *sk,
575 unsigned short hnum = ntohs(loc_port); 608 unsigned short hnum = ntohs(loc_port);
576 609
577 sk_nulls_for_each_from(s, node) { 610 sk_nulls_for_each_from(s, node) {
578 struct inet_sock *inet = inet_sk(s); 611 if (__udp_is_mcast_sock(net, s,
579 612 loc_port, loc_addr,
580 if (!net_eq(sock_net(s), net) || 613 rmt_port, rmt_addr,
581 udp_sk(s)->udp_port_hash != hnum || 614 dif, hnum))
582 (inet->inet_daddr && inet->inet_daddr != rmt_addr) || 615 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 } 616 }
593 s = NULL; 617 s = NULL;
594found: 618found:
@@ -855,6 +879,8 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
855 879
856 ipc.opt = NULL; 880 ipc.opt = NULL;
857 ipc.tx_flags = 0; 881 ipc.tx_flags = 0;
882 ipc.ttl = 0;
883 ipc.tos = -1;
858 884
859 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag; 885 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
860 886
@@ -938,7 +964,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
938 faddr = ipc.opt->opt.faddr; 964 faddr = ipc.opt->opt.faddr;
939 connected = 0; 965 connected = 0;
940 } 966 }
941 tos = RT_TOS(inet->tos); 967 tos = get_rttos(&ipc, inet);
942 if (sock_flag(sk, SOCK_LOCALROUTE) || 968 if (sock_flag(sk, SOCK_LOCALROUTE) ||
943 (msg->msg_flags & MSG_DONTROUTE) || 969 (msg->msg_flags & MSG_DONTROUTE) ||
944 (ipc.opt && ipc.opt->opt.is_strictroute)) { 970 (ipc.opt && ipc.opt->opt.is_strictroute)) {
@@ -1403,8 +1429,10 @@ static int __udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1403{ 1429{
1404 int rc; 1430 int rc;
1405 1431
1406 if (inet_sk(sk)->inet_daddr) 1432 if (inet_sk(sk)->inet_daddr) {
1407 sock_rps_save_rxhash(sk, skb); 1433 sock_rps_save_rxhash(sk, skb);
1434 sk_mark_napi_id(sk, skb);
1435 }
1408 1436
1409 rc = sock_queue_rcv_skb(sk, skb); 1437 rc = sock_queue_rcv_skb(sk, skb);
1410 if (rc < 0) { 1438 if (rc < 0) {
@@ -1528,7 +1556,7 @@ int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1528 1556
1529 rc = 0; 1557 rc = 0;
1530 1558
1531 ipv4_pktinfo_prepare(skb); 1559 ipv4_pktinfo_prepare(sk, skb);
1532 bh_lock_sock(sk); 1560 bh_lock_sock(sk);
1533 if (!sock_owned_by_user(sk)) 1561 if (!sock_owned_by_user(sk))
1534 rc = __udp_queue_rcv_skb(sk, skb); 1562 rc = __udp_queue_rcv_skb(sk, skb);
@@ -1577,6 +1605,14 @@ static void flush_stack(struct sock **stack, unsigned int count,
1577 kfree_skb(skb1); 1605 kfree_skb(skb1);
1578} 1606}
1579 1607
1608static void udp_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
1609{
1610 struct dst_entry *dst = skb_dst(skb);
1611
1612 dst_hold(dst);
1613 sk->sk_rx_dst = dst;
1614}
1615
1580/* 1616/*
1581 * Multicasts and broadcasts go to each listener. 1617 * Multicasts and broadcasts go to each listener.
1582 * 1618 *
@@ -1705,16 +1741,32 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1705 if (udp4_csum_init(skb, uh, proto)) 1741 if (udp4_csum_init(skb, uh, proto))
1706 goto csum_error; 1742 goto csum_error;
1707 1743
1708 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST)) 1744 if (skb->sk) {
1709 return __udp4_lib_mcast_deliver(net, skb, uh, 1745 int ret;
1710 saddr, daddr, udptable); 1746 sk = skb->sk;
1747
1748 if (unlikely(sk->sk_rx_dst == NULL))
1749 udp_sk_rx_dst_set(sk, skb);
1711 1750
1712 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1751 ret = udp_queue_rcv_skb(sk, skb);
1752
1753 /* a return value > 0 means to resubmit the input, but
1754 * it wants the return to be -protocol, or 0
1755 */
1756 if (ret > 0)
1757 return -ret;
1758 return 0;
1759 } else {
1760 if (rt->rt_flags & (RTCF_BROADCAST|RTCF_MULTICAST))
1761 return __udp4_lib_mcast_deliver(net, skb, uh,
1762 saddr, daddr, udptable);
1763
1764 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1765 }
1713 1766
1714 if (sk != NULL) { 1767 if (sk != NULL) {
1715 int ret; 1768 int ret;
1716 1769
1717 sk_mark_napi_id(sk, skb);
1718 ret = udp_queue_rcv_skb(sk, skb); 1770 ret = udp_queue_rcv_skb(sk, skb);
1719 sock_put(sk); 1771 sock_put(sk);
1720 1772
@@ -1768,6 +1820,135 @@ drop:
1768 return 0; 1820 return 0;
1769} 1821}
1770 1822
1823/* We can only early demux multicast if there is a single matching socket.
1824 * If more than one socket found returns NULL
1825 */
1826static struct sock *__udp4_lib_mcast_demux_lookup(struct net *net,
1827 __be16 loc_port, __be32 loc_addr,
1828 __be16 rmt_port, __be32 rmt_addr,
1829 int dif)
1830{
1831 struct sock *sk, *result;
1832 struct hlist_nulls_node *node;
1833 unsigned short hnum = ntohs(loc_port);
1834 unsigned int count, slot = udp_hashfn(net, hnum, udp_table.mask);
1835 struct udp_hslot *hslot = &udp_table.hash[slot];
1836
1837 rcu_read_lock();
1838begin:
1839 count = 0;
1840 result = NULL;
1841 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
1842 if (__udp_is_mcast_sock(net, sk,
1843 loc_port, loc_addr,
1844 rmt_port, rmt_addr,
1845 dif, hnum)) {
1846 result = sk;
1847 ++count;
1848 }
1849 }
1850 /*
1851 * if the nulls value we got at the end of this lookup is
1852 * not the expected one, we must restart lookup.
1853 * We probably met an item that was moved to another chain.
1854 */
1855 if (get_nulls_value(node) != slot)
1856 goto begin;
1857
1858 if (result) {
1859 if (count != 1 ||
1860 unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1861 result = NULL;
1862 else if (unlikely(!__udp_is_mcast_sock(net, result,
1863 loc_port, loc_addr,
1864 rmt_port, rmt_addr,
1865 dif, hnum))) {
1866 sock_put(result);
1867 result = NULL;
1868 }
1869 }
1870 rcu_read_unlock();
1871 return result;
1872}
1873
1874/* For unicast we should only early demux connected sockets or we can
1875 * break forwarding setups. The chains here can be long so only check
1876 * if the first socket is an exact match and if not move on.
1877 */
1878static struct sock *__udp4_lib_demux_lookup(struct net *net,
1879 __be16 loc_port, __be32 loc_addr,
1880 __be16 rmt_port, __be32 rmt_addr,
1881 int dif)
1882{
1883 struct sock *sk, *result;
1884 struct hlist_nulls_node *node;
1885 unsigned short hnum = ntohs(loc_port);
1886 unsigned int hash2 = udp4_portaddr_hash(net, loc_addr, hnum);
1887 unsigned int slot2 = hash2 & udp_table.mask;
1888 struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1889 INET_ADDR_COOKIE(acookie, rmt_addr, loc_addr)
1890 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
1891
1892 rcu_read_lock();
1893 result = NULL;
1894 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
1895 if (INET_MATCH(sk, net, acookie,
1896 rmt_addr, loc_addr, ports, dif))
1897 result = sk;
1898 /* Only check first socket in chain */
1899 break;
1900 }
1901
1902 if (result) {
1903 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
1904 result = NULL;
1905 else if (unlikely(!INET_MATCH(sk, net, acookie,
1906 rmt_addr, loc_addr,
1907 ports, dif))) {
1908 sock_put(result);
1909 result = NULL;
1910 }
1911 }
1912 rcu_read_unlock();
1913 return result;
1914}
1915
1916void udp_v4_early_demux(struct sk_buff *skb)
1917{
1918 const struct iphdr *iph = ip_hdr(skb);
1919 const struct udphdr *uh = udp_hdr(skb);
1920 struct sock *sk;
1921 struct dst_entry *dst;
1922 struct net *net = dev_net(skb->dev);
1923 int dif = skb->dev->ifindex;
1924
1925 /* validate the packet */
1926 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct udphdr)))
1927 return;
1928
1929 if (skb->pkt_type == PACKET_BROADCAST ||
1930 skb->pkt_type == PACKET_MULTICAST)
1931 sk = __udp4_lib_mcast_demux_lookup(net, uh->dest, iph->daddr,
1932 uh->source, iph->saddr, dif);
1933 else if (skb->pkt_type == PACKET_HOST)
1934 sk = __udp4_lib_demux_lookup(net, uh->dest, iph->daddr,
1935 uh->source, iph->saddr, dif);
1936 else
1937 return;
1938
1939 if (!sk)
1940 return;
1941
1942 skb->sk = sk;
1943 skb->destructor = sock_edemux;
1944 dst = sk->sk_rx_dst;
1945
1946 if (dst)
1947 dst = dst_check(dst, 0);
1948 if (dst)
1949 skb_dst_set_noref(skb, dst);
1950}
1951
1771int udp_rcv(struct sk_buff *skb) 1952int udp_rcv(struct sk_buff *skb)
1772{ 1953{
1773 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP); 1954 return __udp4_lib_rcv(skb, &udp_table, IPPROTO_UDP);
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..83206de2bc76 100644
--- a/net/ipv4/udp_offload.c
+++ b/net/ipv4/udp_offload.c
@@ -52,6 +52,7 @@ static struct sk_buff *udp4_ufo_fragment(struct sk_buff *skb,
52 52
53 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY | 53 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY |
54 SKB_GSO_UDP_TUNNEL | 54 SKB_GSO_UDP_TUNNEL |
55 SKB_GSO_IPIP |
55 SKB_GSO_GRE | SKB_GSO_MPLS) || 56 SKB_GSO_GRE | SKB_GSO_MPLS) ||
56 !(type & (SKB_GSO_UDP)))) 57 !(type & (SKB_GSO_UDP))))
57 goto out; 58 goto out;
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/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..542d09561ed6 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,
@@ -371,7 +361,6 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
371 } 361 }
372#endif 362#endif
373 363
374#ifdef CONFIG_IPV6_PRIVACY
375 INIT_LIST_HEAD(&ndev->tempaddr_list); 364 INIT_LIST_HEAD(&ndev->tempaddr_list);
376 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev); 365 setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
377 if ((dev->flags&IFF_LOOPBACK) || 366 if ((dev->flags&IFF_LOOPBACK) ||
@@ -384,7 +373,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
384 in6_dev_hold(ndev); 373 in6_dev_hold(ndev);
385 ipv6_regen_rndid((unsigned long) ndev); 374 ipv6_regen_rndid((unsigned long) ndev);
386 } 375 }
387#endif 376
388 ndev->token = in6addr_any; 377 ndev->token = in6addr_any;
389 378
390 if (netif_running(dev) && addrconf_qdisc_ok(dev)) 379 if (netif_running(dev) && addrconf_qdisc_ok(dev))
@@ -865,12 +854,10 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
865 /* Add to inet6_dev unicast addr list. */ 854 /* Add to inet6_dev unicast addr list. */
866 ipv6_link_dev_addr(idev, ifa); 855 ipv6_link_dev_addr(idev, ifa);
867 856
868#ifdef CONFIG_IPV6_PRIVACY
869 if (ifa->flags&IFA_F_TEMPORARY) { 857 if (ifa->flags&IFA_F_TEMPORARY) {
870 list_add(&ifa->tmp_list, &idev->tempaddr_list); 858 list_add(&ifa->tmp_list, &idev->tempaddr_list);
871 in6_ifa_hold(ifa); 859 in6_ifa_hold(ifa);
872 } 860 }
873#endif
874 861
875 in6_ifa_hold(ifa); 862 in6_ifa_hold(ifa);
876 write_unlock(&idev->lock); 863 write_unlock(&idev->lock);
@@ -913,7 +900,7 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
913 spin_unlock_bh(&addrconf_hash_lock); 900 spin_unlock_bh(&addrconf_hash_lock);
914 901
915 write_lock_bh(&idev->lock); 902 write_lock_bh(&idev->lock);
916#ifdef CONFIG_IPV6_PRIVACY 903
917 if (ifp->flags&IFA_F_TEMPORARY) { 904 if (ifp->flags&IFA_F_TEMPORARY) {
918 list_del(&ifp->tmp_list); 905 list_del(&ifp->tmp_list);
919 if (ifp->ifpub) { 906 if (ifp->ifpub) {
@@ -922,7 +909,6 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
922 } 909 }
923 __in6_ifa_put(ifp); 910 __in6_ifa_put(ifp);
924 } 911 }
925#endif
926 912
927 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) { 913 list_for_each_entry_safe(ifa, ifn, &idev->addr_list, if_list) {
928 if (ifa == ifp) { 914 if (ifa == ifp) {
@@ -1013,7 +999,6 @@ out:
1013 in6_ifa_put(ifp); 999 in6_ifa_put(ifp);
1014} 1000}
1015 1001
1016#ifdef CONFIG_IPV6_PRIVACY
1017static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift) 1002static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
1018{ 1003{
1019 struct inet6_dev *idev = ifp->idev; 1004 struct inet6_dev *idev = ifp->idev;
@@ -1116,7 +1101,6 @@ retry:
1116out: 1101out:
1117 return ret; 1102 return ret;
1118} 1103}
1119#endif
1120 1104
1121/* 1105/*
1122 * Choose an appropriate source address (RFC3484) 1106 * Choose an appropriate source address (RFC3484)
@@ -1131,9 +1115,7 @@ enum {
1131#endif 1115#endif
1132 IPV6_SADDR_RULE_OIF, 1116 IPV6_SADDR_RULE_OIF,
1133 IPV6_SADDR_RULE_LABEL, 1117 IPV6_SADDR_RULE_LABEL,
1134#ifdef CONFIG_IPV6_PRIVACY
1135 IPV6_SADDR_RULE_PRIVACY, 1118 IPV6_SADDR_RULE_PRIVACY,
1136#endif
1137 IPV6_SADDR_RULE_ORCHID, 1119 IPV6_SADDR_RULE_ORCHID,
1138 IPV6_SADDR_RULE_PREFIX, 1120 IPV6_SADDR_RULE_PREFIX,
1139 IPV6_SADDR_RULE_MAX 1121 IPV6_SADDR_RULE_MAX
@@ -1247,7 +1229,6 @@ static int ipv6_get_saddr_eval(struct net *net,
1247 &score->ifa->addr, score->addr_type, 1229 &score->ifa->addr, score->addr_type,
1248 score->ifa->idev->dev->ifindex) == dst->label; 1230 score->ifa->idev->dev->ifindex) == dst->label;
1249 break; 1231 break;
1250#ifdef CONFIG_IPV6_PRIVACY
1251 case IPV6_SADDR_RULE_PRIVACY: 1232 case IPV6_SADDR_RULE_PRIVACY:
1252 { 1233 {
1253 /* Rule 7: Prefer public address 1234 /* Rule 7: Prefer public address
@@ -1259,7 +1240,6 @@ static int ipv6_get_saddr_eval(struct net *net,
1259 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp; 1240 ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
1260 break; 1241 break;
1261 } 1242 }
1262#endif
1263 case IPV6_SADDR_RULE_ORCHID: 1243 case IPV6_SADDR_RULE_ORCHID:
1264 /* Rule 8-: Prefer ORCHID vs ORCHID or 1244 /* Rule 8-: Prefer ORCHID vs ORCHID or
1265 * non-ORCHID vs non-ORCHID 1245 * non-ORCHID vs non-ORCHID
@@ -1588,7 +1568,6 @@ static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1588 if (dad_failed) 1568 if (dad_failed)
1589 ipv6_ifa_notify(0, ifp); 1569 ipv6_ifa_notify(0, ifp);
1590 in6_ifa_put(ifp); 1570 in6_ifa_put(ifp);
1591#ifdef CONFIG_IPV6_PRIVACY
1592 } else if (ifp->flags&IFA_F_TEMPORARY) { 1571 } else if (ifp->flags&IFA_F_TEMPORARY) {
1593 struct inet6_ifaddr *ifpub; 1572 struct inet6_ifaddr *ifpub;
1594 spin_lock_bh(&ifp->lock); 1573 spin_lock_bh(&ifp->lock);
@@ -1602,7 +1581,6 @@ static void addrconf_dad_stop(struct inet6_ifaddr *ifp, int dad_failed)
1602 spin_unlock_bh(&ifp->lock); 1581 spin_unlock_bh(&ifp->lock);
1603 } 1582 }
1604 ipv6_del_addr(ifp); 1583 ipv6_del_addr(ifp);
1605#endif
1606 } else 1584 } else
1607 ipv6_del_addr(ifp); 1585 ipv6_del_addr(ifp);
1608} 1586}
@@ -1851,7 +1829,6 @@ static int ipv6_inherit_eui64(u8 *eui, struct inet6_dev *idev)
1851 return err; 1829 return err;
1852} 1830}
1853 1831
1854#ifdef CONFIG_IPV6_PRIVACY
1855/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */ 1832/* (re)generation of randomized interface identifier (RFC 3041 3.2, 3.5) */
1856static void __ipv6_regen_rndid(struct inet6_dev *idev) 1833static void __ipv6_regen_rndid(struct inet6_dev *idev)
1857{ 1834{
@@ -1919,7 +1896,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) 1896 if (tmpaddr && memcmp(idev->rndid, &tmpaddr->s6_addr[8], 8) == 0)
1920 __ipv6_regen_rndid(idev); 1897 __ipv6_regen_rndid(idev);
1921} 1898}
1922#endif
1923 1899
1924/* 1900/*
1925 * Add prefix route. 1901 * Add prefix route.
@@ -2207,9 +2183,7 @@ ok:
2207 if (ifp) { 2183 if (ifp) {
2208 int flags; 2184 int flags;
2209 unsigned long now; 2185 unsigned long now;
2210#ifdef CONFIG_IPV6_PRIVACY
2211 struct inet6_ifaddr *ift; 2186 struct inet6_ifaddr *ift;
2212#endif
2213 u32 stored_lft; 2187 u32 stored_lft;
2214 2188
2215 /* update lifetime (RFC2462 5.5.3 e) */ 2189 /* update lifetime (RFC2462 5.5.3 e) */
@@ -2250,7 +2224,6 @@ ok:
2250 } else 2224 } else
2251 spin_unlock(&ifp->lock); 2225 spin_unlock(&ifp->lock);
2252 2226
2253#ifdef CONFIG_IPV6_PRIVACY
2254 read_lock_bh(&in6_dev->lock); 2227 read_lock_bh(&in6_dev->lock);
2255 /* update all temporary addresses in the list */ 2228 /* update all temporary addresses in the list */
2256 list_for_each_entry(ift, &in6_dev->tempaddr_list, 2229 list_for_each_entry(ift, &in6_dev->tempaddr_list,
@@ -2315,7 +2288,7 @@ ok:
2315 } else { 2288 } else {
2316 read_unlock_bh(&in6_dev->lock); 2289 read_unlock_bh(&in6_dev->lock);
2317 } 2290 }
2318#endif 2291
2319 in6_ifa_put(ifp); 2292 in6_ifa_put(ifp);
2320 addrconf_verify(0); 2293 addrconf_verify(0);
2321 } 2294 }
@@ -2995,7 +2968,6 @@ static int addrconf_ifdown(struct net_device *dev, int how)
2995 if (!how) 2968 if (!how)
2996 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY); 2969 idev->if_flags &= ~(IF_RS_SENT|IF_RA_RCVD|IF_READY);
2997 2970
2998#ifdef CONFIG_IPV6_PRIVACY
2999 if (how && del_timer(&idev->regen_timer)) 2971 if (how && del_timer(&idev->regen_timer))
3000 in6_dev_put(idev); 2972 in6_dev_put(idev);
3001 2973
@@ -3015,7 +2987,6 @@ static int addrconf_ifdown(struct net_device *dev, int how)
3015 in6_ifa_put(ifa); 2987 in6_ifa_put(ifa);
3016 write_lock_bh(&idev->lock); 2988 write_lock_bh(&idev->lock);
3017 } 2989 }
3018#endif
3019 2990
3020 while (!list_empty(&idev->addr_list)) { 2991 while (!list_empty(&idev->addr_list)) {
3021 ifa = list_first_entry(&idev->addr_list, 2992 ifa = list_first_entry(&idev->addr_list,
@@ -3528,7 +3499,6 @@ restart:
3528 in6_ifa_put(ifp); 3499 in6_ifa_put(ifp);
3529 goto restart; 3500 goto restart;
3530 } 3501 }
3531#ifdef CONFIG_IPV6_PRIVACY
3532 } else if ((ifp->flags&IFA_F_TEMPORARY) && 3502 } else if ((ifp->flags&IFA_F_TEMPORARY) &&
3533 !(ifp->flags&IFA_F_TENTATIVE)) { 3503 !(ifp->flags&IFA_F_TENTATIVE)) {
3534 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry * 3504 unsigned long regen_advance = ifp->idev->cnf.regen_max_retry *
@@ -3556,7 +3526,6 @@ restart:
3556 } else if (time_before(ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ, next)) 3526 } 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; 3527 next = ifp->tstamp + ifp->prefered_lft * HZ - regen_advance * HZ;
3558 spin_unlock(&ifp->lock); 3528 spin_unlock(&ifp->lock);
3559#endif
3560 } else { 3529 } else {
3561 /* ifp->prefered_lft <= ifp->valid_lft */ 3530 /* ifp->prefered_lft <= ifp->valid_lft */
3562 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next)) 3531 if (time_before(ifp->tstamp + ifp->prefered_lft * HZ, next))
@@ -4128,13 +4097,11 @@ static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4128 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval); 4097 jiffies_to_msecs(cnf->mldv1_unsolicited_report_interval);
4129 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] = 4098 array[DEVCONF_MLDV2_UNSOLICITED_REPORT_INTERVAL] =
4130 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval); 4099 jiffies_to_msecs(cnf->mldv2_unsolicited_report_interval);
4131#ifdef CONFIG_IPV6_PRIVACY
4132 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr; 4100 array[DEVCONF_USE_TEMPADDR] = cnf->use_tempaddr;
4133 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft; 4101 array[DEVCONF_TEMP_VALID_LFT] = cnf->temp_valid_lft;
4134 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft; 4102 array[DEVCONF_TEMP_PREFERED_LFT] = cnf->temp_prefered_lft;
4135 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry; 4103 array[DEVCONF_REGEN_MAX_RETRY] = cnf->regen_max_retry;
4136 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor; 4104 array[DEVCONF_MAX_DESYNC_FACTOR] = cnf->max_desync_factor;
4137#endif
4138 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses; 4105 array[DEVCONF_MAX_ADDRESSES] = cnf->max_addresses;
4139 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr; 4106 array[DEVCONF_ACCEPT_RA_DEFRTR] = cnf->accept_ra_defrtr;
4140 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo; 4107 array[DEVCONF_ACCEPT_RA_PINFO] = cnf->accept_ra_pinfo;
@@ -4828,7 +4795,6 @@ static struct addrconf_sysctl_table
4828 .mode = 0644, 4795 .mode = 0644,
4829 .proc_handler = proc_dointvec_ms_jiffies, 4796 .proc_handler = proc_dointvec_ms_jiffies,
4830 }, 4797 },
4831#ifdef CONFIG_IPV6_PRIVACY
4832 { 4798 {
4833 .procname = "use_tempaddr", 4799 .procname = "use_tempaddr",
4834 .data = &ipv6_devconf.use_tempaddr, 4800 .data = &ipv6_devconf.use_tempaddr,
@@ -4864,7 +4830,6 @@ static struct addrconf_sysctl_table
4864 .mode = 0644, 4830 .mode = 0644,
4865 .proc_handler = proc_dointvec, 4831 .proc_handler = proc_dointvec,
4866 }, 4832 },
4867#endif
4868 { 4833 {
4869 .procname = "max_addresses", 4834 .procname = "max_addresses",
4870 .data = &ipv6_devconf.max_addresses, 4835 .data = &ipv6_devconf.max_addresses,
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index 7c96100b021e..6468bda1f2b9 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;
@@ -870,8 +865,6 @@ static int __init inet6_init(void)
870 if (err) 865 if (err)
871 goto out_sock_register_fail; 866 goto out_sock_register_fail;
872 867
873 tcpv6_prot.sysctl_mem = init_net.ipv4.sysctl_tcp_mem;
874
875 /* 868 /*
876 * ipngwg API draft makes clear that the correct semantics 869 * ipngwg API draft makes clear that the correct semantics
877 * for TCP and UDP is to consider one TCP and UDP instance 870 * for TCP and UDP is to consider one TCP and UDP instance
@@ -1028,52 +1021,4 @@ out_unregister_tcp_proto:
1028} 1021}
1029module_init(inet6_init); 1022module_init(inet6_init);
1030 1023
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); 1024MODULE_ALIAS_NETPROTO(PF_INET6);
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 48b6bd2a9a14..a454b0ff57c7 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -107,16 +107,16 @@ ipv4_connected:
107 if (err) 107 if (err)
108 goto out; 108 goto out;
109 109
110 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr); 110 ipv6_addr_set_v4mapped(inet->inet_daddr, &sk->sk_v6_daddr);
111 111
112 if (ipv6_addr_any(&np->saddr) || 112 if (ipv6_addr_any(&np->saddr) ||
113 ipv6_mapped_addr_any(&np->saddr)) 113 ipv6_mapped_addr_any(&np->saddr))
114 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 114 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
115 115
116 if (ipv6_addr_any(&np->rcv_saddr) || 116 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr) ||
117 ipv6_mapped_addr_any(&np->rcv_saddr)) { 117 ipv6_mapped_addr_any(&sk->sk_v6_rcv_saddr)) {
118 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 118 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
119 &np->rcv_saddr); 119 &sk->sk_v6_rcv_saddr);
120 if (sk->sk_prot->rehash) 120 if (sk->sk_prot->rehash)
121 sk->sk_prot->rehash(sk); 121 sk->sk_prot->rehash(sk);
122 } 122 }
@@ -145,7 +145,7 @@ ipv4_connected:
145 } 145 }
146 } 146 }
147 147
148 np->daddr = *daddr; 148 sk->sk_v6_daddr = *daddr;
149 np->flow_label = fl6.flowlabel; 149 np->flow_label = fl6.flowlabel;
150 150
151 inet->inet_dport = usin->sin6_port; 151 inet->inet_dport = usin->sin6_port;
@@ -156,7 +156,7 @@ ipv4_connected:
156 */ 156 */
157 157
158 fl6.flowi6_proto = sk->sk_protocol; 158 fl6.flowi6_proto = sk->sk_protocol;
159 fl6.daddr = np->daddr; 159 fl6.daddr = sk->sk_v6_daddr;
160 fl6.saddr = np->saddr; 160 fl6.saddr = np->saddr;
161 fl6.flowi6_oif = sk->sk_bound_dev_if; 161 fl6.flowi6_oif = sk->sk_bound_dev_if;
162 fl6.flowi6_mark = sk->sk_mark; 162 fl6.flowi6_mark = sk->sk_mark;
@@ -183,16 +183,16 @@ ipv4_connected:
183 if (ipv6_addr_any(&np->saddr)) 183 if (ipv6_addr_any(&np->saddr))
184 np->saddr = fl6.saddr; 184 np->saddr = fl6.saddr;
185 185
186 if (ipv6_addr_any(&np->rcv_saddr)) { 186 if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
187 np->rcv_saddr = fl6.saddr; 187 sk->sk_v6_rcv_saddr = fl6.saddr;
188 inet->inet_rcv_saddr = LOOPBACK4_IPV6; 188 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
189 if (sk->sk_prot->rehash) 189 if (sk->sk_prot->rehash)
190 sk->sk_prot->rehash(sk); 190 sk->sk_prot->rehash(sk);
191 } 191 }
192 192
193 ip6_dst_store(sk, dst, 193 ip6_dst_store(sk, dst,
194 ipv6_addr_equal(&fl6.daddr, &np->daddr) ? 194 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
195 &np->daddr : NULL, 195 &sk->sk_v6_daddr : NULL,
196#ifdef CONFIG_IPV6_SUBTREES 196#ifdef CONFIG_IPV6_SUBTREES
197 ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 197 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
198 &np->saddr : 198 &np->saddr :
@@ -883,11 +883,10 @@ EXPORT_SYMBOL_GPL(ip6_datagram_send_ctl);
883void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp, 883void ip6_dgram_sock_seq_show(struct seq_file *seq, struct sock *sp,
884 __u16 srcp, __u16 destp, int bucket) 884 __u16 srcp, __u16 destp, int bucket)
885{ 885{
886 struct ipv6_pinfo *np = inet6_sk(sp);
887 const struct in6_addr *dest, *src; 886 const struct in6_addr *dest, *src;
888 887
889 dest = &np->daddr; 888 dest = &sp->sk_v6_daddr;
890 src = &np->rcv_saddr; 889 src = &sp->sk_v6_rcv_saddr;
891 seq_printf(seq, 890 seq_printf(seq,
892 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " 891 "%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", 892 "%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/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_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..5e31a909a2b0 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -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);
diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c
new file mode 100644
index 000000000000..ed94ba61dda0
--- /dev/null
+++ b/net/ipv6/ip6_vti.c
@@ -0,0 +1,1056 @@
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
78static struct net_device_stats *vti6_get_stats(struct net_device *dev)
79{
80 struct pcpu_tstats sum = { 0 };
81 int i;
82
83 for_each_possible_cpu(i) {
84 const struct pcpu_tstats *tstats = per_cpu_ptr(dev->tstats, i);
85
86 sum.rx_packets += tstats->rx_packets;
87 sum.rx_bytes += tstats->rx_bytes;
88 sum.tx_packets += tstats->tx_packets;
89 sum.tx_bytes += tstats->tx_bytes;
90 }
91 dev->stats.rx_packets = sum.rx_packets;
92 dev->stats.rx_bytes = sum.rx_bytes;
93 dev->stats.tx_packets = sum.tx_packets;
94 dev->stats.tx_bytes = sum.tx_bytes;
95 return &dev->stats;
96}
97
98#define for_each_vti6_tunnel_rcu(start) \
99 for (t = rcu_dereference(start); t; t = rcu_dereference(t->next))
100
101/**
102 * vti6_tnl_lookup - fetch tunnel matching the end-point addresses
103 * @net: network namespace
104 * @remote: the address of the tunnel exit-point
105 * @local: the address of the tunnel entry-point
106 *
107 * Return:
108 * tunnel matching given end-points if found,
109 * else fallback tunnel if its device is up,
110 * else %NULL
111 **/
112static struct ip6_tnl *
113vti6_tnl_lookup(struct net *net, const struct in6_addr *remote,
114 const struct in6_addr *local)
115{
116 unsigned int hash = HASH(remote, local);
117 struct ip6_tnl *t;
118 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
119
120 for_each_vti6_tunnel_rcu(ip6n->tnls_r_l[hash]) {
121 if (ipv6_addr_equal(local, &t->parms.laddr) &&
122 ipv6_addr_equal(remote, &t->parms.raddr) &&
123 (t->dev->flags & IFF_UP))
124 return t;
125 }
126 t = rcu_dereference(ip6n->tnls_wc[0]);
127 if (t && (t->dev->flags & IFF_UP))
128 return t;
129
130 return NULL;
131}
132
133/**
134 * vti6_tnl_bucket - get head of list matching given tunnel parameters
135 * @p: parameters containing tunnel end-points
136 *
137 * Description:
138 * vti6_tnl_bucket() returns the head of the list matching the
139 * &struct in6_addr entries laddr and raddr in @p.
140 *
141 * Return: head of IPv6 tunnel list
142 **/
143static struct ip6_tnl __rcu **
144vti6_tnl_bucket(struct vti6_net *ip6n, const struct __ip6_tnl_parm *p)
145{
146 const struct in6_addr *remote = &p->raddr;
147 const struct in6_addr *local = &p->laddr;
148 unsigned int h = 0;
149 int prio = 0;
150
151 if (!ipv6_addr_any(remote) || !ipv6_addr_any(local)) {
152 prio = 1;
153 h = HASH(remote, local);
154 }
155 return &ip6n->tnls[prio][h];
156}
157
158static void
159vti6_tnl_link(struct vti6_net *ip6n, struct ip6_tnl *t)
160{
161 struct ip6_tnl __rcu **tp = vti6_tnl_bucket(ip6n, &t->parms);
162
163 rcu_assign_pointer(t->next , rtnl_dereference(*tp));
164 rcu_assign_pointer(*tp, t);
165}
166
167static void
168vti6_tnl_unlink(struct vti6_net *ip6n, struct ip6_tnl *t)
169{
170 struct ip6_tnl __rcu **tp;
171 struct ip6_tnl *iter;
172
173 for (tp = vti6_tnl_bucket(ip6n, &t->parms);
174 (iter = rtnl_dereference(*tp)) != NULL;
175 tp = &iter->next) {
176 if (t == iter) {
177 rcu_assign_pointer(*tp, t->next);
178 break;
179 }
180 }
181}
182
183static void vti6_dev_free(struct net_device *dev)
184{
185 free_percpu(dev->tstats);
186 free_netdev(dev);
187}
188
189static int vti6_tnl_create2(struct net_device *dev)
190{
191 struct ip6_tnl *t = netdev_priv(dev);
192 struct net *net = dev_net(dev);
193 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
194 int err;
195
196 err = vti6_dev_init(dev);
197 if (err < 0)
198 goto out;
199
200 err = register_netdevice(dev);
201 if (err < 0)
202 goto out;
203
204 strcpy(t->parms.name, dev->name);
205 dev->rtnl_link_ops = &vti6_link_ops;
206
207 dev_hold(dev);
208 vti6_tnl_link(ip6n, t);
209
210 return 0;
211
212out:
213 return err;
214}
215
216static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
217{
218 struct net_device *dev;
219 struct ip6_tnl *t;
220 char name[IFNAMSIZ];
221 int err;
222
223 if (p->name[0])
224 strlcpy(name, p->name, IFNAMSIZ);
225 else
226 sprintf(name, "ip6_vti%%d");
227
228 dev = alloc_netdev(sizeof(*t), name, vti6_dev_setup);
229 if (dev == NULL)
230 goto failed;
231
232 dev_net_set(dev, net);
233
234 t = netdev_priv(dev);
235 t->parms = *p;
236 t->net = dev_net(dev);
237
238 err = vti6_tnl_create2(dev);
239 if (err < 0)
240 goto failed_free;
241
242 return t;
243
244failed_free:
245 vti6_dev_free(dev);
246failed:
247 return NULL;
248}
249
250/**
251 * vti6_locate - find or create tunnel matching given parameters
252 * @net: network namespace
253 * @p: tunnel parameters
254 * @create: != 0 if allowed to create new tunnel if no match found
255 *
256 * Description:
257 * vti6_locate() first tries to locate an existing tunnel
258 * based on @parms. If this is unsuccessful, but @create is set a new
259 * tunnel device is created and registered for use.
260 *
261 * Return:
262 * matching tunnel or NULL
263 **/
264static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
265 int create)
266{
267 const struct in6_addr *remote = &p->raddr;
268 const struct in6_addr *local = &p->laddr;
269 struct ip6_tnl __rcu **tp;
270 struct ip6_tnl *t;
271 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
272
273 for (tp = vti6_tnl_bucket(ip6n, p);
274 (t = rtnl_dereference(*tp)) != NULL;
275 tp = &t->next) {
276 if (ipv6_addr_equal(local, &t->parms.laddr) &&
277 ipv6_addr_equal(remote, &t->parms.raddr))
278 return t;
279 }
280 if (!create)
281 return NULL;
282 return vti6_tnl_create(net, p);
283}
284
285/**
286 * vti6_dev_uninit - tunnel device uninitializer
287 * @dev: the device to be destroyed
288 *
289 * Description:
290 * vti6_dev_uninit() removes tunnel from its list
291 **/
292static void vti6_dev_uninit(struct net_device *dev)
293{
294 struct ip6_tnl *t = netdev_priv(dev);
295 struct net *net = dev_net(dev);
296 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
297
298 if (dev == ip6n->fb_tnl_dev)
299 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
300 else
301 vti6_tnl_unlink(ip6n, t);
302 ip6_tnl_dst_reset(t);
303 dev_put(dev);
304}
305
306static int vti6_rcv(struct sk_buff *skb)
307{
308 struct ip6_tnl *t;
309 const struct ipv6hdr *ipv6h = ipv6_hdr(skb);
310
311 rcu_read_lock();
312
313 if ((t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr,
314 &ipv6h->daddr)) != NULL) {
315 struct pcpu_tstats *tstats;
316
317 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
318 rcu_read_unlock();
319 goto discard;
320 }
321
322 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
323 rcu_read_unlock();
324 return 0;
325 }
326
327 if (!ip6_tnl_rcv_ctl(t, &ipv6h->daddr, &ipv6h->saddr)) {
328 t->dev->stats.rx_dropped++;
329 rcu_read_unlock();
330 goto discard;
331 }
332
333 tstats = this_cpu_ptr(t->dev->tstats);
334 tstats->rx_packets++;
335 tstats->rx_bytes += skb->len;
336
337 skb->mark = 0;
338 secpath_reset(skb);
339 skb->dev = t->dev;
340
341 rcu_read_unlock();
342 return 0;
343 }
344 rcu_read_unlock();
345 return 1;
346
347discard:
348 kfree_skb(skb);
349 return 0;
350}
351
352/**
353 * vti6_addr_conflict - compare packet addresses to tunnel's own
354 * @t: the outgoing tunnel device
355 * @hdr: IPv6 header from the incoming packet
356 *
357 * Description:
358 * Avoid trivial tunneling loop by checking that tunnel exit-point
359 * doesn't match source of incoming packet.
360 *
361 * Return:
362 * 1 if conflict,
363 * 0 else
364 **/
365static inline bool
366vti6_addr_conflict(const struct ip6_tnl *t, const struct ipv6hdr *hdr)
367{
368 return ipv6_addr_equal(&t->parms.raddr, &hdr->saddr);
369}
370
371/**
372 * vti6_xmit - send a packet
373 * @skb: the outgoing socket buffer
374 * @dev: the outgoing tunnel device
375 **/
376static int vti6_xmit(struct sk_buff *skb, struct net_device *dev)
377{
378 struct net *net = dev_net(dev);
379 struct ip6_tnl *t = netdev_priv(dev);
380 struct net_device_stats *stats = &t->dev->stats;
381 struct dst_entry *dst = NULL, *ndst = NULL;
382 struct flowi6 fl6;
383 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
384 struct net_device *tdev;
385 int err = -1;
386
387 if ((t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) ||
388 !ip6_tnl_xmit_ctl(t) || vti6_addr_conflict(t, ipv6h))
389 return err;
390
391 dst = ip6_tnl_dst_check(t);
392 if (!dst) {
393 memcpy(&fl6, &t->fl.u.ip6, sizeof(fl6));
394
395 ndst = ip6_route_output(net, NULL, &fl6);
396
397 if (ndst->error)
398 goto tx_err_link_failure;
399 ndst = xfrm_lookup(net, ndst, flowi6_to_flowi(&fl6), NULL, 0);
400 if (IS_ERR(ndst)) {
401 err = PTR_ERR(ndst);
402 ndst = NULL;
403 goto tx_err_link_failure;
404 }
405 dst = ndst;
406 }
407
408 if (!dst->xfrm || dst->xfrm->props.mode != XFRM_MODE_TUNNEL)
409 goto tx_err_link_failure;
410
411 tdev = dst->dev;
412
413 if (tdev == dev) {
414 stats->collisions++;
415 net_warn_ratelimited("%s: Local routing loop detected!\n",
416 t->parms.name);
417 goto tx_err_dst_release;
418 }
419
420
421 skb_dst_drop(skb);
422 skb_dst_set_noref(skb, dst);
423
424 ip6tunnel_xmit(skb, dev);
425 if (ndst) {
426 dev->mtu = dst_mtu(ndst);
427 ip6_tnl_dst_store(t, ndst);
428 }
429
430 return 0;
431tx_err_link_failure:
432 stats->tx_carrier_errors++;
433 dst_link_failure(skb);
434tx_err_dst_release:
435 dst_release(ndst);
436 return err;
437}
438
439static netdev_tx_t
440vti6_tnl_xmit(struct sk_buff *skb, struct net_device *dev)
441{
442 struct ip6_tnl *t = netdev_priv(dev);
443 struct net_device_stats *stats = &t->dev->stats;
444 int ret;
445
446 switch (skb->protocol) {
447 case htons(ETH_P_IPV6):
448 ret = vti6_xmit(skb, dev);
449 break;
450 default:
451 goto tx_err;
452 }
453
454 if (ret < 0)
455 goto tx_err;
456
457 return NETDEV_TX_OK;
458
459tx_err:
460 stats->tx_errors++;
461 stats->tx_dropped++;
462 kfree_skb(skb);
463 return NETDEV_TX_OK;
464}
465
466static void vti6_link_config(struct ip6_tnl *t)
467{
468 struct dst_entry *dst;
469 struct net_device *dev = t->dev;
470 struct __ip6_tnl_parm *p = &t->parms;
471 struct flowi6 *fl6 = &t->fl.u.ip6;
472
473 memcpy(dev->dev_addr, &p->laddr, sizeof(struct in6_addr));
474 memcpy(dev->broadcast, &p->raddr, sizeof(struct in6_addr));
475
476 /* Set up flowi template */
477 fl6->saddr = p->laddr;
478 fl6->daddr = p->raddr;
479 fl6->flowi6_oif = p->link;
480 fl6->flowi6_mark = be32_to_cpu(p->i_key);
481 fl6->flowi6_proto = p->proto;
482 fl6->flowlabel = 0;
483
484 p->flags &= ~(IP6_TNL_F_CAP_XMIT | IP6_TNL_F_CAP_RCV |
485 IP6_TNL_F_CAP_PER_PACKET);
486 p->flags |= ip6_tnl_get_cap(t, &p->laddr, &p->raddr);
487
488 if (p->flags & IP6_TNL_F_CAP_XMIT && p->flags & IP6_TNL_F_CAP_RCV)
489 dev->flags |= IFF_POINTOPOINT;
490 else
491 dev->flags &= ~IFF_POINTOPOINT;
492
493 dev->iflink = p->link;
494
495 if (p->flags & IP6_TNL_F_CAP_XMIT) {
496
497 dst = ip6_route_output(dev_net(dev), NULL, fl6);
498 if (dst->error)
499 return;
500
501 dst = xfrm_lookup(dev_net(dev), dst, flowi6_to_flowi(fl6),
502 NULL, 0);
503 if (IS_ERR(dst))
504 return;
505
506 if (dst->dev) {
507 dev->hard_header_len = dst->dev->hard_header_len;
508
509 dev->mtu = dst_mtu(dst);
510
511 if (dev->mtu < IPV6_MIN_MTU)
512 dev->mtu = IPV6_MIN_MTU;
513 }
514 dst_release(dst);
515 }
516}
517
518/**
519 * vti6_tnl_change - update the tunnel parameters
520 * @t: tunnel to be changed
521 * @p: tunnel configuration parameters
522 *
523 * Description:
524 * vti6_tnl_change() updates the tunnel parameters
525 **/
526static int
527vti6_tnl_change(struct ip6_tnl *t, const struct __ip6_tnl_parm *p)
528{
529 t->parms.laddr = p->laddr;
530 t->parms.raddr = p->raddr;
531 t->parms.link = p->link;
532 t->parms.i_key = p->i_key;
533 t->parms.o_key = p->o_key;
534 t->parms.proto = p->proto;
535 ip6_tnl_dst_reset(t);
536 vti6_link_config(t);
537 return 0;
538}
539
540static int vti6_update(struct ip6_tnl *t, struct __ip6_tnl_parm *p)
541{
542 struct net *net = dev_net(t->dev);
543 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
544 int err;
545
546 vti6_tnl_unlink(ip6n, t);
547 synchronize_net();
548 err = vti6_tnl_change(t, p);
549 vti6_tnl_link(ip6n, t);
550 netdev_state_change(t->dev);
551 return err;
552}
553
554static void
555vti6_parm_from_user(struct __ip6_tnl_parm *p, const struct ip6_tnl_parm2 *u)
556{
557 p->laddr = u->laddr;
558 p->raddr = u->raddr;
559 p->link = u->link;
560 p->i_key = u->i_key;
561 p->o_key = u->o_key;
562 p->proto = u->proto;
563
564 memcpy(p->name, u->name, sizeof(u->name));
565}
566
567static void
568vti6_parm_to_user(struct ip6_tnl_parm2 *u, const struct __ip6_tnl_parm *p)
569{
570 u->laddr = p->laddr;
571 u->raddr = p->raddr;
572 u->link = p->link;
573 u->i_key = p->i_key;
574 u->o_key = p->o_key;
575 u->proto = p->proto;
576
577 memcpy(u->name, p->name, sizeof(u->name));
578}
579
580/**
581 * vti6_tnl_ioctl - configure vti6 tunnels from userspace
582 * @dev: virtual device associated with tunnel
583 * @ifr: parameters passed from userspace
584 * @cmd: command to be performed
585 *
586 * Description:
587 * vti6_ioctl() is used for managing vti6 tunnels
588 * from userspace.
589 *
590 * The possible commands are the following:
591 * %SIOCGETTUNNEL: get tunnel parameters for device
592 * %SIOCADDTUNNEL: add tunnel matching given tunnel parameters
593 * %SIOCCHGTUNNEL: change tunnel parameters to those given
594 * %SIOCDELTUNNEL: delete tunnel
595 *
596 * The fallback device "ip6_vti0", created during module
597 * initialization, can be used for creating other tunnel devices.
598 *
599 * Return:
600 * 0 on success,
601 * %-EFAULT if unable to copy data to or from userspace,
602 * %-EPERM if current process hasn't %CAP_NET_ADMIN set
603 * %-EINVAL if passed tunnel parameters are invalid,
604 * %-EEXIST if changing a tunnel's parameters would cause a conflict
605 * %-ENODEV if attempting to change or delete a nonexisting device
606 **/
607static int
608vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
609{
610 int err = 0;
611 struct ip6_tnl_parm2 p;
612 struct __ip6_tnl_parm p1;
613 struct ip6_tnl *t = NULL;
614 struct net *net = dev_net(dev);
615 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
616
617 switch (cmd) {
618 case SIOCGETTUNNEL:
619 if (dev == ip6n->fb_tnl_dev) {
620 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p))) {
621 err = -EFAULT;
622 break;
623 }
624 vti6_parm_from_user(&p1, &p);
625 t = vti6_locate(net, &p1, 0);
626 } else {
627 memset(&p, 0, sizeof(p));
628 }
629 if (t == NULL)
630 t = netdev_priv(dev);
631 vti6_parm_to_user(&p, &t->parms);
632 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
633 err = -EFAULT;
634 break;
635 case SIOCADDTUNNEL:
636 case SIOCCHGTUNNEL:
637 err = -EPERM;
638 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
639 break;
640 err = -EFAULT;
641 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
642 break;
643 err = -EINVAL;
644 if (p.proto != IPPROTO_IPV6 && p.proto != 0)
645 break;
646 vti6_parm_from_user(&p1, &p);
647 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
648 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
649 if (t != NULL) {
650 if (t->dev != dev) {
651 err = -EEXIST;
652 break;
653 }
654 } else
655 t = netdev_priv(dev);
656
657 err = vti6_update(t, &p1);
658 }
659 if (t) {
660 err = 0;
661 vti6_parm_to_user(&p, &t->parms);
662 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
663 err = -EFAULT;
664
665 } else
666 err = (cmd == SIOCADDTUNNEL ? -ENOBUFS : -ENOENT);
667 break;
668 case SIOCDELTUNNEL:
669 err = -EPERM;
670 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
671 break;
672
673 if (dev == ip6n->fb_tnl_dev) {
674 err = -EFAULT;
675 if (copy_from_user(&p, ifr->ifr_ifru.ifru_data, sizeof(p)))
676 break;
677 err = -ENOENT;
678 vti6_parm_from_user(&p1, &p);
679 t = vti6_locate(net, &p1, 0);
680 if (t == NULL)
681 break;
682 err = -EPERM;
683 if (t->dev == ip6n->fb_tnl_dev)
684 break;
685 dev = t->dev;
686 }
687 err = 0;
688 unregister_netdevice(dev);
689 break;
690 default:
691 err = -EINVAL;
692 }
693 return err;
694}
695
696/**
697 * vti6_tnl_change_mtu - change mtu manually for tunnel device
698 * @dev: virtual device associated with tunnel
699 * @new_mtu: the new mtu
700 *
701 * Return:
702 * 0 on success,
703 * %-EINVAL if mtu too small
704 **/
705static int vti6_change_mtu(struct net_device *dev, int new_mtu)
706{
707 if (new_mtu < IPV6_MIN_MTU)
708 return -EINVAL;
709
710 dev->mtu = new_mtu;
711 return 0;
712}
713
714static const struct net_device_ops vti6_netdev_ops = {
715 .ndo_uninit = vti6_dev_uninit,
716 .ndo_start_xmit = vti6_tnl_xmit,
717 .ndo_do_ioctl = vti6_ioctl,
718 .ndo_change_mtu = vti6_change_mtu,
719 .ndo_get_stats = vti6_get_stats,
720};
721
722/**
723 * vti6_dev_setup - setup virtual tunnel device
724 * @dev: virtual device associated with tunnel
725 *
726 * Description:
727 * Initialize function pointers and device parameters
728 **/
729static void vti6_dev_setup(struct net_device *dev)
730{
731 struct ip6_tnl *t;
732
733 dev->netdev_ops = &vti6_netdev_ops;
734 dev->destructor = vti6_dev_free;
735
736 dev->type = ARPHRD_TUNNEL6;
737 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct ipv6hdr);
738 dev->mtu = ETH_DATA_LEN;
739 t = netdev_priv(dev);
740 dev->flags |= IFF_NOARP;
741 dev->addr_len = sizeof(struct in6_addr);
742 dev->features |= NETIF_F_NETNS_LOCAL;
743 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
744}
745
746/**
747 * vti6_dev_init_gen - general initializer for all tunnel devices
748 * @dev: virtual device associated with tunnel
749 **/
750static inline int vti6_dev_init_gen(struct net_device *dev)
751{
752 struct ip6_tnl *t = netdev_priv(dev);
753
754 t->dev = dev;
755 t->net = dev_net(dev);
756 dev->tstats = alloc_percpu(struct pcpu_tstats);
757 if (!dev->tstats)
758 return -ENOMEM;
759 return 0;
760}
761
762/**
763 * vti6_dev_init - initializer for all non fallback tunnel devices
764 * @dev: virtual device associated with tunnel
765 **/
766static int vti6_dev_init(struct net_device *dev)
767{
768 struct ip6_tnl *t = netdev_priv(dev);
769 int err = vti6_dev_init_gen(dev);
770
771 if (err)
772 return err;
773 vti6_link_config(t);
774 return 0;
775}
776
777/**
778 * vti6_fb_tnl_dev_init - initializer for fallback tunnel device
779 * @dev: fallback device
780 *
781 * Return: 0
782 **/
783static int __net_init vti6_fb_tnl_dev_init(struct net_device *dev)
784{
785 struct ip6_tnl *t = netdev_priv(dev);
786 struct net *net = dev_net(dev);
787 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
788 int err = vti6_dev_init_gen(dev);
789
790 if (err)
791 return err;
792
793 t->parms.proto = IPPROTO_IPV6;
794 dev_hold(dev);
795
796 vti6_link_config(t);
797
798 rcu_assign_pointer(ip6n->tnls_wc[0], t);
799 return 0;
800}
801
802static int vti6_validate(struct nlattr *tb[], struct nlattr *data[])
803{
804 return 0;
805}
806
807static void vti6_netlink_parms(struct nlattr *data[],
808 struct __ip6_tnl_parm *parms)
809{
810 memset(parms, 0, sizeof(*parms));
811
812 if (!data)
813 return;
814
815 if (data[IFLA_VTI_LINK])
816 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
817
818 if (data[IFLA_VTI_LOCAL])
819 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL],
820 sizeof(struct in6_addr));
821
822 if (data[IFLA_VTI_REMOTE])
823 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE],
824 sizeof(struct in6_addr));
825
826 if (data[IFLA_VTI_IKEY])
827 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
828
829 if (data[IFLA_VTI_OKEY])
830 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
831}
832
833static int vti6_newlink(struct net *src_net, struct net_device *dev,
834 struct nlattr *tb[], struct nlattr *data[])
835{
836 struct net *net = dev_net(dev);
837 struct ip6_tnl *nt;
838
839 nt = netdev_priv(dev);
840 vti6_netlink_parms(data, &nt->parms);
841
842 nt->parms.proto = IPPROTO_IPV6;
843
844 if (vti6_locate(net, &nt->parms, 0))
845 return -EEXIST;
846
847 return vti6_tnl_create2(dev);
848}
849
850static int vti6_changelink(struct net_device *dev, struct nlattr *tb[],
851 struct nlattr *data[])
852{
853 struct ip6_tnl *t;
854 struct __ip6_tnl_parm p;
855 struct net *net = dev_net(dev);
856 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
857
858 if (dev == ip6n->fb_tnl_dev)
859 return -EINVAL;
860
861 vti6_netlink_parms(data, &p);
862
863 t = vti6_locate(net, &p, 0);
864
865 if (t) {
866 if (t->dev != dev)
867 return -EEXIST;
868 } else
869 t = netdev_priv(dev);
870
871 return vti6_update(t, &p);
872}
873
874static size_t vti6_get_size(const struct net_device *dev)
875{
876 return
877 /* IFLA_VTI_LINK */
878 nla_total_size(4) +
879 /* IFLA_VTI_LOCAL */
880 nla_total_size(sizeof(struct in6_addr)) +
881 /* IFLA_VTI_REMOTE */
882 nla_total_size(sizeof(struct in6_addr)) +
883 /* IFLA_VTI_IKEY */
884 nla_total_size(4) +
885 /* IFLA_VTI_OKEY */
886 nla_total_size(4) +
887 0;
888}
889
890static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
891{
892 struct ip6_tnl *tunnel = netdev_priv(dev);
893 struct __ip6_tnl_parm *parm = &tunnel->parms;
894
895 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
896 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr),
897 &parm->laddr) ||
898 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
899 &parm->raddr) ||
900 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
901 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
902 goto nla_put_failure;
903 return 0;
904
905nla_put_failure:
906 return -EMSGSIZE;
907}
908
909static const struct nla_policy vti6_policy[IFLA_VTI_MAX + 1] = {
910 [IFLA_VTI_LINK] = { .type = NLA_U32 },
911 [IFLA_VTI_LOCAL] = { .len = sizeof(struct in6_addr) },
912 [IFLA_VTI_REMOTE] = { .len = sizeof(struct in6_addr) },
913 [IFLA_VTI_IKEY] = { .type = NLA_U32 },
914 [IFLA_VTI_OKEY] = { .type = NLA_U32 },
915};
916
917static struct rtnl_link_ops vti6_link_ops __read_mostly = {
918 .kind = "vti6",
919 .maxtype = IFLA_VTI_MAX,
920 .policy = vti6_policy,
921 .priv_size = sizeof(struct ip6_tnl),
922 .setup = vti6_dev_setup,
923 .validate = vti6_validate,
924 .newlink = vti6_newlink,
925 .changelink = vti6_changelink,
926 .get_size = vti6_get_size,
927 .fill_info = vti6_fill_info,
928};
929
930static struct xfrm_tunnel_notifier vti6_handler __read_mostly = {
931 .handler = vti6_rcv,
932 .priority = 1,
933};
934
935static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
936{
937 int h;
938 struct ip6_tnl *t;
939 LIST_HEAD(list);
940
941 for (h = 0; h < HASH_SIZE; h++) {
942 t = rtnl_dereference(ip6n->tnls_r_l[h]);
943 while (t != NULL) {
944 unregister_netdevice_queue(t->dev, &list);
945 t = rtnl_dereference(t->next);
946 }
947 }
948
949 t = rtnl_dereference(ip6n->tnls_wc[0]);
950 unregister_netdevice_queue(t->dev, &list);
951 unregister_netdevice_many(&list);
952}
953
954static int __net_init vti6_init_net(struct net *net)
955{
956 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
957 struct ip6_tnl *t = NULL;
958 int err;
959
960 ip6n->tnls[0] = ip6n->tnls_wc;
961 ip6n->tnls[1] = ip6n->tnls_r_l;
962
963 err = -ENOMEM;
964 ip6n->fb_tnl_dev = alloc_netdev(sizeof(struct ip6_tnl), "ip6_vti0",
965 vti6_dev_setup);
966
967 if (!ip6n->fb_tnl_dev)
968 goto err_alloc_dev;
969 dev_net_set(ip6n->fb_tnl_dev, net);
970
971 err = vti6_fb_tnl_dev_init(ip6n->fb_tnl_dev);
972 if (err < 0)
973 goto err_register;
974
975 err = register_netdev(ip6n->fb_tnl_dev);
976 if (err < 0)
977 goto err_register;
978
979 t = netdev_priv(ip6n->fb_tnl_dev);
980
981 strcpy(t->parms.name, ip6n->fb_tnl_dev->name);
982 return 0;
983
984err_register:
985 vti6_dev_free(ip6n->fb_tnl_dev);
986err_alloc_dev:
987 return err;
988}
989
990static void __net_exit vti6_exit_net(struct net *net)
991{
992 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
993
994 rtnl_lock();
995 vti6_destroy_tunnels(ip6n);
996 rtnl_unlock();
997}
998
999static struct pernet_operations vti6_net_ops = {
1000 .init = vti6_init_net,
1001 .exit = vti6_exit_net,
1002 .id = &vti6_net_id,
1003 .size = sizeof(struct vti6_net),
1004};
1005
1006/**
1007 * vti6_tunnel_init - register protocol and reserve needed resources
1008 *
1009 * Return: 0 on success
1010 **/
1011static int __init vti6_tunnel_init(void)
1012{
1013 int err;
1014
1015 err = register_pernet_device(&vti6_net_ops);
1016 if (err < 0)
1017 goto out_pernet;
1018
1019 err = xfrm6_mode_tunnel_input_register(&vti6_handler);
1020 if (err < 0) {
1021 pr_err("%s: can't register vti6\n", __func__);
1022 goto out;
1023 }
1024 err = rtnl_link_register(&vti6_link_ops);
1025 if (err < 0)
1026 goto rtnl_link_failed;
1027
1028 return 0;
1029
1030rtnl_link_failed:
1031 xfrm6_mode_tunnel_input_deregister(&vti6_handler);
1032out:
1033 unregister_pernet_device(&vti6_net_ops);
1034out_pernet:
1035 return err;
1036}
1037
1038/**
1039 * vti6_tunnel_cleanup - free resources and unregister protocol
1040 **/
1041static void __exit vti6_tunnel_cleanup(void)
1042{
1043 rtnl_link_unregister(&vti6_link_ops);
1044 if (xfrm6_mode_tunnel_input_deregister(&vti6_handler))
1045 pr_info("%s: can't deregister vti6\n", __func__);
1046
1047 unregister_pernet_device(&vti6_net_ops);
1048}
1049
1050module_init(vti6_tunnel_init);
1051module_exit(vti6_tunnel_cleanup);
1052MODULE_LICENSE("GPL");
1053MODULE_ALIAS_RTNL_LINK("vti6");
1054MODULE_ALIAS_NETDEV("ip6_vti0");
1055MODULE_AUTHOR("Steffen Klassert");
1056MODULE_DESCRIPTION("IPv6 virtual tunnel interface");
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/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/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..bf9f612c1bc2 100644
--- a/net/ipv6/netfilter/ip6t_SYNPROXY.c
+++ b/net/ipv6/netfilter/ip6t_SYNPROXY.c
@@ -312,7 +312,7 @@ synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
312 return XT_CONTINUE; 312 return XT_CONTINUE;
313} 313}
314 314
315static unsigned int ipv6_synproxy_hook(unsigned int hooknum, 315static unsigned int ipv6_synproxy_hook(const struct nf_hook_ops *ops,
316 struct sk_buff *skb, 316 struct sk_buff *skb,
317 const struct net_device *in, 317 const struct net_device *in,
318 const struct net_device *out, 318 const struct net_device *out,
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..8815e31a87fe 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -116,7 +116,7 @@ int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
116 } else { 116 } else {
117 if (sk->sk_state != TCP_ESTABLISHED) 117 if (sk->sk_state != TCP_ESTABLISHED)
118 return -EDESTADDRREQ; 118 return -EDESTADDRREQ;
119 daddr = &np->daddr; 119 daddr = &sk->sk_v6_daddr;
120 } 120 }
121 121
122 if (!iif) 122 if (!iif)
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index a4ed2416399e..3c00842b0079 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;
@@ -804,8 +803,8 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
804 * sk->sk_dst_cache. 803 * sk->sk_dst_cache.
805 */ 804 */
806 if (sk->sk_state == TCP_ESTABLISHED && 805 if (sk->sk_state == TCP_ESTABLISHED &&
807 ipv6_addr_equal(daddr, &np->daddr)) 806 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
808 daddr = &np->daddr; 807 daddr = &sk->sk_v6_daddr;
809 808
810 if (addr_len >= sizeof(struct sockaddr_in6) && 809 if (addr_len >= sizeof(struct sockaddr_in6) &&
811 sin6->sin6_scope_id && 810 sin6->sin6_scope_id &&
@@ -816,7 +815,7 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
816 return -EDESTADDRREQ; 815 return -EDESTADDRREQ;
817 816
818 proto = inet->inet_num; 817 proto = inet->inet_num;
819 daddr = &np->daddr; 818 daddr = &sk->sk_v6_daddr;
820 fl6.flowlabel = np->flow_label; 819 fl6.flowlabel = np->flow_label;
821 } 820 }
822 821
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 04e17b3309fb..7faa9d5e1503 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -619,7 +619,7 @@ static struct rt6_info *find_match(struct rt6_info *rt, int oif, int strict,
619 goto out; 619 goto out;
620 620
621 m = rt6_score_route(rt, oif, strict); 621 m = rt6_score_route(rt, oif, strict);
622 if (m == RT6_NUD_FAIL_SOFT && !IS_ENABLED(CONFIG_IPV6_ROUTER_PREF)) { 622 if (m == RT6_NUD_FAIL_SOFT) {
623 match_do_rr = true; 623 match_do_rr = true;
624 m = 0; /* lowest valid score */ 624 m = 0; /* lowest valid score */
625 } else if (m < 0) { 625 } else if (m < 0) {
@@ -731,8 +731,11 @@ int rt6_route_rcv(struct net_device *dev, u8 *opt, int len,
731 prefix = &prefix_buf; 731 prefix = &prefix_buf;
732 } 732 }
733 733
734 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len, gwaddr, 734 if (rinfo->prefix_len == 0)
735 dev->ifindex); 735 rt = rt6_get_dflt_router(gwaddr, dev);
736 else
737 rt = rt6_get_route_info(net, prefix, rinfo->prefix_len,
738 gwaddr, dev->ifindex);
736 739
737 if (rt && !lifetime) { 740 if (rt && !lifetime) {
738 ip6_del_rt(rt); 741 ip6_del_rt(rt);
@@ -871,11 +874,9 @@ static struct rt6_info *rt6_alloc_cow(struct rt6_info *ort,
871 rt = ip6_rt_copy(ort, daddr); 874 rt = ip6_rt_copy(ort, daddr);
872 875
873 if (rt) { 876 if (rt) {
874 if (!(rt->rt6i_flags & RTF_GATEWAY)) { 877 if (ort->rt6i_dst.plen != 128 &&
875 if (ort->rt6i_dst.plen != 128 && 878 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr))
876 ipv6_addr_equal(&ort->rt6i_dst.addr, daddr)) 879 rt->rt6i_flags |= RTF_ANYCAST;
877 rt->rt6i_flags |= RTF_ANYCAST;
878 }
879 880
880 rt->rt6i_flags |= RTF_CACHE; 881 rt->rt6i_flags |= RTF_CACHE;
881 882
@@ -1163,7 +1164,6 @@ void ip6_update_pmtu(struct sk_buff *skb, struct net *net, __be32 mtu,
1163 memset(&fl6, 0, sizeof(fl6)); 1164 memset(&fl6, 0, sizeof(fl6));
1164 fl6.flowi6_oif = oif; 1165 fl6.flowi6_oif = oif;
1165 fl6.flowi6_mark = mark; 1166 fl6.flowi6_mark = mark;
1166 fl6.flowi6_flags = 0;
1167 fl6.daddr = iph->daddr; 1167 fl6.daddr = iph->daddr;
1168 fl6.saddr = iph->saddr; 1168 fl6.saddr = iph->saddr;
1169 fl6.flowlabel = ip6_flowinfo(iph); 1169 fl6.flowlabel = ip6_flowinfo(iph);
@@ -1262,7 +1262,6 @@ void ip6_redirect(struct sk_buff *skb, struct net *net, int oif, u32 mark)
1262 memset(&fl6, 0, sizeof(fl6)); 1262 memset(&fl6, 0, sizeof(fl6));
1263 fl6.flowi6_oif = oif; 1263 fl6.flowi6_oif = oif;
1264 fl6.flowi6_mark = mark; 1264 fl6.flowi6_mark = mark;
1265 fl6.flowi6_flags = 0;
1266 fl6.daddr = iph->daddr; 1265 fl6.daddr = iph->daddr;
1267 fl6.saddr = iph->saddr; 1266 fl6.saddr = iph->saddr;
1268 fl6.flowlabel = ip6_flowinfo(iph); 1267 fl6.flowlabel = ip6_flowinfo(iph);
@@ -1284,7 +1283,6 @@ void ip6_redirect_no_header(struct sk_buff *skb, struct net *net, int oif,
1284 memset(&fl6, 0, sizeof(fl6)); 1283 memset(&fl6, 0, sizeof(fl6));
1285 fl6.flowi6_oif = oif; 1284 fl6.flowi6_oif = oif;
1286 fl6.flowi6_mark = mark; 1285 fl6.flowi6_mark = mark;
1287 fl6.flowi6_flags = 0;
1288 fl6.daddr = msg->dest; 1286 fl6.daddr = msg->dest;
1289 fl6.saddr = iph->daddr; 1287 fl6.saddr = iph->daddr;
1290 1288
@@ -2831,56 +2829,12 @@ static int ip6_route_dev_notify(struct notifier_block *this,
2831 2829
2832#ifdef CONFIG_PROC_FS 2830#ifdef CONFIG_PROC_FS
2833 2831
2834struct rt6_proc_arg
2835{
2836 char *buffer;
2837 int offset;
2838 int length;
2839 int skip;
2840 int len;
2841};
2842
2843static int rt6_info_route(struct rt6_info *rt, void *p_arg)
2844{
2845 struct seq_file *m = p_arg;
2846
2847 seq_printf(m, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2848
2849#ifdef CONFIG_IPV6_SUBTREES
2850 seq_printf(m, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2851#else
2852 seq_puts(m, "00000000000000000000000000000000 00 ");
2853#endif
2854 if (rt->rt6i_flags & RTF_GATEWAY) {
2855 seq_printf(m, "%pi6", &rt->rt6i_gateway);
2856 } else {
2857 seq_puts(m, "00000000000000000000000000000000");
2858 }
2859 seq_printf(m, " %08x %08x %08x %08x %8s\n",
2860 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2861 rt->dst.__use, rt->rt6i_flags,
2862 rt->dst.dev ? rt->dst.dev->name : "");
2863 return 0;
2864}
2865
2866static int ipv6_route_show(struct seq_file *m, void *v)
2867{
2868 struct net *net = (struct net *)m->private;
2869 fib6_clean_all_ro(net, rt6_info_route, 0, m);
2870 return 0;
2871}
2872
2873static int ipv6_route_open(struct inode *inode, struct file *file)
2874{
2875 return single_open_net(inode, file, ipv6_route_show);
2876}
2877
2878static const struct file_operations ipv6_route_proc_fops = { 2832static const struct file_operations ipv6_route_proc_fops = {
2879 .owner = THIS_MODULE, 2833 .owner = THIS_MODULE,
2880 .open = ipv6_route_open, 2834 .open = ipv6_route_open,
2881 .read = seq_read, 2835 .read = seq_read,
2882 .llseek = seq_lseek, 2836 .llseek = seq_lseek,
2883 .release = single_release_net, 2837 .release = seq_release_net,
2884}; 2838};
2885 2839
2886static int rt6_stats_seq_show(struct seq_file *seq, void *v) 2840static 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..3a9038dd818d 100644
--- a/net/ipv6/sit.c
+++ b/net/ipv6/sit.c
@@ -933,10 +933,9 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
933 ttl = iph6->hop_limit; 933 ttl = iph6->hop_limit;
934 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6)); 934 tos = INET_ECN_encapsulate(tos, ipv6_get_dsfield(iph6));
935 935
936 if (likely(!skb->encapsulation)) { 936 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_SIT);
937 skb_reset_inner_headers(skb); 937 if (IS_ERR(skb))
938 skb->encapsulation = 1; 938 goto out;
939 }
940 939
941 err = iptunnel_xmit(rt, skb, fl4.saddr, fl4.daddr, IPPROTO_IPV6, tos, 940 err = iptunnel_xmit(rt, skb, fl4.saddr, fl4.daddr, IPPROTO_IPV6, tos,
942 ttl, df, !net_eq(tunnel->net, dev_net(dev))); 941 ttl, df, !net_eq(tunnel->net, dev_net(dev)));
@@ -946,8 +945,9 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
946tx_error_icmp: 945tx_error_icmp:
947 dst_link_failure(skb); 946 dst_link_failure(skb);
948tx_error: 947tx_error:
949 dev->stats.tx_errors++;
950 dev_kfree_skb(skb); 948 dev_kfree_skb(skb);
949out:
950 dev->stats.tx_errors++;
951 return NETDEV_TX_OK; 951 return NETDEV_TX_OK;
952} 952}
953 953
@@ -956,13 +956,15 @@ static netdev_tx_t ipip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev)
956 struct ip_tunnel *tunnel = netdev_priv(dev); 956 struct ip_tunnel *tunnel = netdev_priv(dev);
957 const struct iphdr *tiph = &tunnel->parms.iph; 957 const struct iphdr *tiph = &tunnel->parms.iph;
958 958
959 if (likely(!skb->encapsulation)) { 959 skb = iptunnel_handle_offloads(skb, false, SKB_GSO_IPIP);
960 skb_reset_inner_headers(skb); 960 if (IS_ERR(skb))
961 skb->encapsulation = 1; 961 goto out;
962 }
963 962
964 ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP); 963 ip_tunnel_xmit(skb, dev, tiph, IPPROTO_IPIP);
965 return NETDEV_TX_OK; 964 return NETDEV_TX_OK;
965out:
966 dev->stats.tx_errors++;
967 return NETDEV_TX_OK;
966} 968}
967 969
968static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb, 970static netdev_tx_t sit_tunnel_xmit(struct sk_buff *skb,
@@ -1292,6 +1294,12 @@ static void ipip6_dev_free(struct net_device *dev)
1292 free_netdev(dev); 1294 free_netdev(dev);
1293} 1295}
1294 1296
1297#define SIT_FEATURES (NETIF_F_SG | \
1298 NETIF_F_FRAGLIST | \
1299 NETIF_F_HIGHDMA | \
1300 NETIF_F_GSO_SOFTWARE | \
1301 NETIF_F_HW_CSUM)
1302
1295static void ipip6_tunnel_setup(struct net_device *dev) 1303static void ipip6_tunnel_setup(struct net_device *dev)
1296{ 1304{
1297 dev->netdev_ops = &ipip6_netdev_ops; 1305 dev->netdev_ops = &ipip6_netdev_ops;
@@ -1305,6 +1313,8 @@ static void ipip6_tunnel_setup(struct net_device *dev)
1305 dev->iflink = 0; 1313 dev->iflink = 0;
1306 dev->addr_len = 4; 1314 dev->addr_len = 4;
1307 dev->features |= NETIF_F_LLTX; 1315 dev->features |= NETIF_F_LLTX;
1316 dev->features |= SIT_FEATURES;
1317 dev->hw_features |= SIT_FEATURES;
1308} 1318}
1309 1319
1310static int ipip6_tunnel_init(struct net_device *dev) 1320static int ipip6_tunnel_init(struct net_device *dev)
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..0740f93a114a 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -192,13 +192,13 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
192 } 192 }
193 193
194 if (tp->rx_opt.ts_recent_stamp && 194 if (tp->rx_opt.ts_recent_stamp &&
195 !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) { 195 !ipv6_addr_equal(&sk->sk_v6_daddr, &usin->sin6_addr)) {
196 tp->rx_opt.ts_recent = 0; 196 tp->rx_opt.ts_recent = 0;
197 tp->rx_opt.ts_recent_stamp = 0; 197 tp->rx_opt.ts_recent_stamp = 0;
198 tp->write_seq = 0; 198 tp->write_seq = 0;
199 } 199 }
200 200
201 np->daddr = usin->sin6_addr; 201 sk->sk_v6_daddr = usin->sin6_addr;
202 np->flow_label = fl6.flowlabel; 202 np->flow_label = fl6.flowlabel;
203 203
204 /* 204 /*
@@ -237,17 +237,17 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
237 } else { 237 } else {
238 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 238 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
239 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, 239 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
240 &np->rcv_saddr); 240 &sk->sk_v6_rcv_saddr);
241 } 241 }
242 242
243 return err; 243 return err;
244 } 244 }
245 245
246 if (!ipv6_addr_any(&np->rcv_saddr)) 246 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
247 saddr = &np->rcv_saddr; 247 saddr = &sk->sk_v6_rcv_saddr;
248 248
249 fl6.flowi6_proto = IPPROTO_TCP; 249 fl6.flowi6_proto = IPPROTO_TCP;
250 fl6.daddr = np->daddr; 250 fl6.daddr = sk->sk_v6_daddr;
251 fl6.saddr = saddr ? *saddr : np->saddr; 251 fl6.saddr = saddr ? *saddr : np->saddr;
252 fl6.flowi6_oif = sk->sk_bound_dev_if; 252 fl6.flowi6_oif = sk->sk_bound_dev_if;
253 fl6.flowi6_mark = sk->sk_mark; 253 fl6.flowi6_mark = sk->sk_mark;
@@ -266,7 +266,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
266 266
267 if (saddr == NULL) { 267 if (saddr == NULL) {
268 saddr = &fl6.saddr; 268 saddr = &fl6.saddr;
269 np->rcv_saddr = *saddr; 269 sk->sk_v6_rcv_saddr = *saddr;
270 } 270 }
271 271
272 /* set the source address */ 272 /* set the source address */
@@ -279,7 +279,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
279 rt = (struct rt6_info *) dst; 279 rt = (struct rt6_info *) dst;
280 if (tcp_death_row.sysctl_tw_recycle && 280 if (tcp_death_row.sysctl_tw_recycle &&
281 !tp->rx_opt.ts_recent_stamp && 281 !tp->rx_opt.ts_recent_stamp &&
282 ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) 282 ipv6_addr_equal(&rt->rt6i_dst.addr, &sk->sk_v6_daddr))
283 tcp_fetch_timewait_stamp(sk, dst); 283 tcp_fetch_timewait_stamp(sk, dst);
284 284
285 icsk->icsk_ext_hdr_len = 0; 285 icsk->icsk_ext_hdr_len = 0;
@@ -298,7 +298,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
298 298
299 if (!tp->write_seq && likely(!tp->repair)) 299 if (!tp->write_seq && likely(!tp->repair))
300 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32, 300 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
301 np->daddr.s6_addr32, 301 sk->sk_v6_daddr.s6_addr32,
302 inet->inet_sport, 302 inet->inet_sport,
303 inet->inet_dport); 303 inet->inet_dport);
304 304
@@ -465,7 +465,7 @@ static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
465 struct request_sock *req, 465 struct request_sock *req,
466 u16 queue_mapping) 466 u16 queue_mapping)
467{ 467{
468 struct inet6_request_sock *treq = inet6_rsk(req); 468 struct inet_request_sock *ireq = inet_rsk(req);
469 struct ipv6_pinfo *np = inet6_sk(sk); 469 struct ipv6_pinfo *np = inet6_sk(sk);
470 struct sk_buff * skb; 470 struct sk_buff * skb;
471 int err = -ENOMEM; 471 int err = -ENOMEM;
@@ -477,9 +477,10 @@ static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
477 skb = tcp_make_synack(sk, dst, req, NULL); 477 skb = tcp_make_synack(sk, dst, req, NULL);
478 478
479 if (skb) { 479 if (skb) {
480 __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr); 480 __tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
481 &ireq->ir_v6_rmt_addr);
481 482
482 fl6->daddr = treq->rmt_addr; 483 fl6->daddr = ireq->ir_v6_rmt_addr;
483 skb_set_queue_mapping(skb, queue_mapping); 484 skb_set_queue_mapping(skb, queue_mapping);
484 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass); 485 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
485 err = net_xmit_eval(err); 486 err = net_xmit_eval(err);
@@ -502,7 +503,7 @@ static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req)
502 503
503static void tcp_v6_reqsk_destructor(struct request_sock *req) 504static void tcp_v6_reqsk_destructor(struct request_sock *req)
504{ 505{
505 kfree_skb(inet6_rsk(req)->pktopts); 506 kfree_skb(inet_rsk(req)->pktopts);
506} 507}
507 508
508#ifdef CONFIG_TCP_MD5SIG 509#ifdef CONFIG_TCP_MD5SIG
@@ -515,13 +516,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, 516static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
516 struct sock *addr_sk) 517 struct sock *addr_sk)
517{ 518{
518 return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr); 519 return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr);
519} 520}
520 521
521static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk, 522static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
522 struct request_sock *req) 523 struct request_sock *req)
523{ 524{
524 return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr); 525 return tcp_v6_md5_do_lookup(sk, &inet_rsk(req)->ir_v6_rmt_addr);
525} 526}
526 527
527static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval, 528static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
@@ -621,10 +622,10 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
621 622
622 if (sk) { 623 if (sk) {
623 saddr = &inet6_sk(sk)->saddr; 624 saddr = &inet6_sk(sk)->saddr;
624 daddr = &inet6_sk(sk)->daddr; 625 daddr = &sk->sk_v6_daddr;
625 } else if (req) { 626 } else if (req) {
626 saddr = &inet6_rsk(req)->loc_addr; 627 saddr = &inet_rsk(req)->ir_v6_loc_addr;
627 daddr = &inet6_rsk(req)->rmt_addr; 628 daddr = &inet_rsk(req)->ir_v6_rmt_addr;
628 } else { 629 } else {
629 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 630 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
630 saddr = &ip6h->saddr; 631 saddr = &ip6h->saddr;
@@ -949,7 +950,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
949{ 950{
950 struct tcp_options_received tmp_opt; 951 struct tcp_options_received tmp_opt;
951 struct request_sock *req; 952 struct request_sock *req;
952 struct inet6_request_sock *treq; 953 struct inet_request_sock *ireq;
953 struct ipv6_pinfo *np = inet6_sk(sk); 954 struct ipv6_pinfo *np = inet6_sk(sk);
954 struct tcp_sock *tp = tcp_sk(sk); 955 struct tcp_sock *tp = tcp_sk(sk);
955 __u32 isn = TCP_SKB_CB(skb)->when; 956 __u32 isn = TCP_SKB_CB(skb)->when;
@@ -994,25 +995,25 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
994 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; 995 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
995 tcp_openreq_init(req, &tmp_opt, skb); 996 tcp_openreq_init(req, &tmp_opt, skb);
996 997
997 treq = inet6_rsk(req); 998 ireq = inet_rsk(req);
998 treq->rmt_addr = ipv6_hdr(skb)->saddr; 999 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
999 treq->loc_addr = ipv6_hdr(skb)->daddr; 1000 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
1000 if (!want_cookie || tmp_opt.tstamp_ok) 1001 if (!want_cookie || tmp_opt.tstamp_ok)
1001 TCP_ECN_create_request(req, skb, sock_net(sk)); 1002 TCP_ECN_create_request(req, skb, sock_net(sk));
1002 1003
1003 treq->iif = sk->sk_bound_dev_if; 1004 ireq->ir_iif = sk->sk_bound_dev_if;
1004 1005
1005 /* So that link locals have meaning */ 1006 /* So that link locals have meaning */
1006 if (!sk->sk_bound_dev_if && 1007 if (!sk->sk_bound_dev_if &&
1007 ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL) 1008 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
1008 treq->iif = inet6_iif(skb); 1009 ireq->ir_iif = inet6_iif(skb);
1009 1010
1010 if (!isn) { 1011 if (!isn) {
1011 if (ipv6_opt_accepted(sk, skb) || 1012 if (ipv6_opt_accepted(sk, skb) ||
1012 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || 1013 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
1013 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) { 1014 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
1014 atomic_inc(&skb->users); 1015 atomic_inc(&skb->users);
1015 treq->pktopts = skb; 1016 ireq->pktopts = skb;
1016 } 1017 }
1017 1018
1018 if (want_cookie) { 1019 if (want_cookie) {
@@ -1051,7 +1052,7 @@ static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1051 * to the moment of synflood. 1052 * to the moment of synflood.
1052 */ 1053 */
1053 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n", 1054 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
1054 &treq->rmt_addr, ntohs(tcp_hdr(skb)->source)); 1055 &ireq->ir_v6_rmt_addr, ntohs(tcp_hdr(skb)->source));
1055 goto drop_and_release; 1056 goto drop_and_release;
1056 } 1057 }
1057 1058
@@ -1086,7 +1087,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1086 struct request_sock *req, 1087 struct request_sock *req,
1087 struct dst_entry *dst) 1088 struct dst_entry *dst)
1088{ 1089{
1089 struct inet6_request_sock *treq; 1090 struct inet_request_sock *ireq;
1090 struct ipv6_pinfo *newnp, *np = inet6_sk(sk); 1091 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1091 struct tcp6_sock *newtcp6sk; 1092 struct tcp6_sock *newtcp6sk;
1092 struct inet_sock *newinet; 1093 struct inet_sock *newinet;
@@ -1116,11 +1117,11 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1116 1117
1117 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 1118 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1118 1119
1119 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr); 1120 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
1120 1121
1121 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); 1122 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1122 1123
1123 newnp->rcv_saddr = newnp->saddr; 1124 newsk->sk_v6_rcv_saddr = newnp->saddr;
1124 1125
1125 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; 1126 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1126 newsk->sk_backlog_rcv = tcp_v4_do_rcv; 1127 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
@@ -1151,7 +1152,7 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1151 return newsk; 1152 return newsk;
1152 } 1153 }
1153 1154
1154 treq = inet6_rsk(req); 1155 ireq = inet_rsk(req);
1155 1156
1156 if (sk_acceptq_is_full(sk)) 1157 if (sk_acceptq_is_full(sk))
1157 goto out_overflow; 1158 goto out_overflow;
@@ -1185,10 +1186,10 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1185 1186
1186 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 1187 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1187 1188
1188 newnp->daddr = treq->rmt_addr; 1189 newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
1189 newnp->saddr = treq->loc_addr; 1190 newnp->saddr = ireq->ir_v6_loc_addr;
1190 newnp->rcv_saddr = treq->loc_addr; 1191 newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
1191 newsk->sk_bound_dev_if = treq->iif; 1192 newsk->sk_bound_dev_if = ireq->ir_iif;
1192 1193
1193 /* Now IPv6 options... 1194 /* Now IPv6 options...
1194 1195
@@ -1203,11 +1204,11 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1203 1204
1204 /* Clone pktoptions received with SYN */ 1205 /* Clone pktoptions received with SYN */
1205 newnp->pktoptions = NULL; 1206 newnp->pktoptions = NULL;
1206 if (treq->pktopts != NULL) { 1207 if (ireq->pktopts != NULL) {
1207 newnp->pktoptions = skb_clone(treq->pktopts, 1208 newnp->pktoptions = skb_clone(ireq->pktopts,
1208 sk_gfp_atomic(sk, GFP_ATOMIC)); 1209 sk_gfp_atomic(sk, GFP_ATOMIC));
1209 consume_skb(treq->pktopts); 1210 consume_skb(ireq->pktopts);
1210 treq->pktopts = NULL; 1211 ireq->pktopts = NULL;
1211 if (newnp->pktoptions) 1212 if (newnp->pktoptions)
1212 skb_set_owner_r(newnp->pktoptions, newsk); 1213 skb_set_owner_r(newnp->pktoptions, newsk);
1213 } 1214 }
@@ -1244,13 +1245,13 @@ static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1244 1245
1245#ifdef CONFIG_TCP_MD5SIG 1246#ifdef CONFIG_TCP_MD5SIG
1246 /* Copy over the MD5 key from the original socket */ 1247 /* Copy over the MD5 key from the original socket */
1247 if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) { 1248 if ((key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr)) != NULL) {
1248 /* We're using one, so create a matching key 1249 /* We're using one, so create a matching key
1249 * on the newsk structure. If we fail to get 1250 * on the newsk structure. If we fail to get
1250 * memory, then we end up not copying the key 1251 * memory, then we end up not copying the key
1251 * across. Shucks. 1252 * across. Shucks.
1252 */ 1253 */
1253 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newnp->daddr, 1254 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr,
1254 AF_INET6, key->key, key->keylen, 1255 AF_INET6, key->key, key->keylen,
1255 sk_gfp_atomic(sk, GFP_ATOMIC)); 1256 sk_gfp_atomic(sk, GFP_ATOMIC));
1256 } 1257 }
@@ -1722,8 +1723,8 @@ static void get_openreq6(struct seq_file *seq,
1722 const struct sock *sk, struct request_sock *req, int i, kuid_t uid) 1723 const struct sock *sk, struct request_sock *req, int i, kuid_t uid)
1723{ 1724{
1724 int ttd = req->expires - jiffies; 1725 int ttd = req->expires - jiffies;
1725 const struct in6_addr *src = &inet6_rsk(req)->loc_addr; 1726 const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
1726 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr; 1727 const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
1727 1728
1728 if (ttd < 0) 1729 if (ttd < 0)
1729 ttd = 0; 1730 ttd = 0;
@@ -1734,10 +1735,10 @@ static void get_openreq6(struct seq_file *seq,
1734 i, 1735 i,
1735 src->s6_addr32[0], src->s6_addr32[1], 1736 src->s6_addr32[0], src->s6_addr32[1],
1736 src->s6_addr32[2], src->s6_addr32[3], 1737 src->s6_addr32[2], src->s6_addr32[3],
1737 ntohs(inet_rsk(req)->loc_port), 1738 inet_rsk(req)->ir_num,
1738 dest->s6_addr32[0], dest->s6_addr32[1], 1739 dest->s6_addr32[0], dest->s6_addr32[1],
1739 dest->s6_addr32[2], dest->s6_addr32[3], 1740 dest->s6_addr32[2], dest->s6_addr32[3],
1740 ntohs(inet_rsk(req)->rmt_port), 1741 ntohs(inet_rsk(req)->ir_rmt_port),
1741 TCP_SYN_RECV, 1742 TCP_SYN_RECV,
1742 0,0, /* could print option size, but that is af dependent. */ 1743 0,0, /* could print option size, but that is af dependent. */
1743 1, /* timers active (only the expire timer) */ 1744 1, /* timers active (only the expire timer) */
@@ -1758,10 +1759,9 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1758 const struct inet_sock *inet = inet_sk(sp); 1759 const struct inet_sock *inet = inet_sk(sp);
1759 const struct tcp_sock *tp = tcp_sk(sp); 1760 const struct tcp_sock *tp = tcp_sk(sp);
1760 const struct inet_connection_sock *icsk = inet_csk(sp); 1761 const struct inet_connection_sock *icsk = inet_csk(sp);
1761 const struct ipv6_pinfo *np = inet6_sk(sp);
1762 1762
1763 dest = &np->daddr; 1763 dest = &sp->sk_v6_daddr;
1764 src = &np->rcv_saddr; 1764 src = &sp->sk_v6_rcv_saddr;
1765 destp = ntohs(inet->inet_dport); 1765 destp = ntohs(inet->inet_dport);
1766 srcp = ntohs(inet->inet_sport); 1766 srcp = ntohs(inet->inet_sport);
1767 1767
@@ -1810,11 +1810,10 @@ static void get_timewait6_sock(struct seq_file *seq,
1810{ 1810{
1811 const struct in6_addr *dest, *src; 1811 const struct in6_addr *dest, *src;
1812 __u16 destp, srcp; 1812 __u16 destp, srcp;
1813 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw); 1813 s32 delta = tw->tw_ttd - inet_tw_time_stamp();
1814 long delta = tw->tw_ttd - jiffies;
1815 1814
1816 dest = &tw6->tw_v6_daddr; 1815 dest = &tw->tw_v6_daddr;
1817 src = &tw6->tw_v6_rcv_saddr; 1816 src = &tw->tw_v6_rcv_saddr;
1818 destp = ntohs(tw->tw_dport); 1817 destp = ntohs(tw->tw_dport);
1819 srcp = ntohs(tw->tw_sport); 1818 srcp = ntohs(tw->tw_sport);
1820 1819
@@ -1834,6 +1833,7 @@ static void get_timewait6_sock(struct seq_file *seq,
1834static int tcp6_seq_show(struct seq_file *seq, void *v) 1833static int tcp6_seq_show(struct seq_file *seq, void *v)
1835{ 1834{
1836 struct tcp_iter_state *st; 1835 struct tcp_iter_state *st;
1836 struct sock *sk = v;
1837 1837
1838 if (v == SEQ_START_TOKEN) { 1838 if (v == SEQ_START_TOKEN) {
1839 seq_puts(seq, 1839 seq_puts(seq,
@@ -1849,14 +1849,14 @@ static int tcp6_seq_show(struct seq_file *seq, void *v)
1849 switch (st->state) { 1849 switch (st->state) {
1850 case TCP_SEQ_STATE_LISTENING: 1850 case TCP_SEQ_STATE_LISTENING:
1851 case TCP_SEQ_STATE_ESTABLISHED: 1851 case TCP_SEQ_STATE_ESTABLISHED:
1852 get_tcp6_sock(seq, v, st->num); 1852 if (sk->sk_state == TCP_TIME_WAIT)
1853 get_timewait6_sock(seq, v, st->num);
1854 else
1855 get_tcp6_sock(seq, v, st->num);
1853 break; 1856 break;
1854 case TCP_SEQ_STATE_OPENREQ: 1857 case TCP_SEQ_STATE_OPENREQ:
1855 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid); 1858 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
1856 break; 1859 break;
1857 case TCP_SEQ_STATE_TIME_WAIT:
1858 get_timewait6_sock(seq, v, st->num);
1859 break;
1860 } 1860 }
1861out: 1861out:
1862 return 0; 1862 return 0;
@@ -1929,6 +1929,7 @@ struct proto tcpv6_prot = {
1929 .memory_allocated = &tcp_memory_allocated, 1929 .memory_allocated = &tcp_memory_allocated,
1930 .memory_pressure = &tcp_memory_pressure, 1930 .memory_pressure = &tcp_memory_pressure,
1931 .orphan_count = &tcp_orphan_count, 1931 .orphan_count = &tcp_orphan_count,
1932 .sysctl_mem = sysctl_tcp_mem,
1932 .sysctl_wmem = sysctl_tcp_wmem, 1933 .sysctl_wmem = sysctl_tcp_wmem,
1933 .sysctl_rmem = sysctl_tcp_rmem, 1934 .sysctl_rmem = sysctl_tcp_rmem,
1934 .max_header = MAX_TCP_HEADER, 1935 .max_header = MAX_TCP_HEADER,
diff --git a/net/ipv6/tcpv6_offload.c b/net/ipv6/tcpv6_offload.c
index 2ec6bf6a0aa0..c1097c798900 100644
--- a/net/ipv6/tcpv6_offload.c
+++ b/net/ipv6/tcpv6_offload.c
@@ -83,7 +83,7 @@ static int tcp6_gro_complete(struct sk_buff *skb)
83static const struct net_offload tcpv6_offload = { 83static const struct net_offload tcpv6_offload = {
84 .callbacks = { 84 .callbacks = {
85 .gso_send_check = tcp_v6_gso_send_check, 85 .gso_send_check = tcp_v6_gso_send_check,
86 .gso_segment = tcp_tso_segment, 86 .gso_segment = tcp_gso_segment,
87 .gro_receive = tcp6_gro_receive, 87 .gro_receive = tcp6_gro_receive,
88 .gro_complete = tcp6_gro_complete, 88 .gro_complete = tcp6_gro_complete,
89 }, 89 },
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index 18786098fd41..f3893e897f72 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) {
@@ -551,8 +569,10 @@ static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
551{ 569{
552 int rc; 570 int rc;
553 571
554 if (!ipv6_addr_any(&inet6_sk(sk)->daddr)) 572 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
555 sock_rps_save_rxhash(sk, skb); 573 sock_rps_save_rxhash(sk, skb);
574 sk_mark_napi_id(sk, skb);
575 }
556 576
557 rc = sock_queue_rcv_skb(sk, skb); 577 rc = sock_queue_rcv_skb(sk, skb);
558 if (rc < 0) { 578 if (rc < 0) {
@@ -690,20 +710,19 @@ static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
690 710
691 if (udp_sk(s)->udp_port_hash == num && 711 if (udp_sk(s)->udp_port_hash == num &&
692 s->sk_family == PF_INET6) { 712 s->sk_family == PF_INET6) {
693 struct ipv6_pinfo *np = inet6_sk(s);
694 if (inet->inet_dport) { 713 if (inet->inet_dport) {
695 if (inet->inet_dport != rmt_port) 714 if (inet->inet_dport != rmt_port)
696 continue; 715 continue;
697 } 716 }
698 if (!ipv6_addr_any(&np->daddr) && 717 if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
699 !ipv6_addr_equal(&np->daddr, rmt_addr)) 718 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
700 continue; 719 continue;
701 720
702 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif) 721 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
703 continue; 722 continue;
704 723
705 if (!ipv6_addr_any(&np->rcv_saddr)) { 724 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
706 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr)) 725 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
707 continue; 726 continue;
708 } 727 }
709 if (!inet6_mc_check(s, loc_addr, rmt_addr)) 728 if (!inet6_mc_check(s, loc_addr, rmt_addr))
@@ -846,7 +865,6 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
846 if (sk != NULL) { 865 if (sk != NULL) {
847 int ret; 866 int ret;
848 867
849 sk_mark_napi_id(sk, skb);
850 ret = udpv6_queue_rcv_skb(sk, skb); 868 ret = udpv6_queue_rcv_skb(sk, skb);
851 sock_put(sk); 869 sock_put(sk);
852 870
@@ -1064,7 +1082,7 @@ int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1064 } else if (!up->pending) { 1082 } else if (!up->pending) {
1065 if (sk->sk_state != TCP_ESTABLISHED) 1083 if (sk->sk_state != TCP_ESTABLISHED)
1066 return -EDESTADDRREQ; 1084 return -EDESTADDRREQ;
1067 daddr = &np->daddr; 1085 daddr = &sk->sk_v6_daddr;
1068 } else 1086 } else
1069 daddr = NULL; 1087 daddr = NULL;
1070 1088
@@ -1134,8 +1152,8 @@ do_udp_sendmsg:
1134 * sk->sk_dst_cache. 1152 * sk->sk_dst_cache.
1135 */ 1153 */
1136 if (sk->sk_state == TCP_ESTABLISHED && 1154 if (sk->sk_state == TCP_ESTABLISHED &&
1137 ipv6_addr_equal(daddr, &np->daddr)) 1155 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1138 daddr = &np->daddr; 1156 daddr = &sk->sk_v6_daddr;
1139 1157
1140 if (addr_len >= sizeof(struct sockaddr_in6) && 1158 if (addr_len >= sizeof(struct sockaddr_in6) &&
1141 sin6->sin6_scope_id && 1159 sin6->sin6_scope_id &&
@@ -1146,7 +1164,7 @@ do_udp_sendmsg:
1146 return -EDESTADDRREQ; 1164 return -EDESTADDRREQ;
1147 1165
1148 fl6.fl6_dport = inet->inet_dport; 1166 fl6.fl6_dport = inet->inet_dport;
1149 daddr = &np->daddr; 1167 daddr = &sk->sk_v6_daddr;
1150 fl6.flowlabel = np->flow_label; 1168 fl6.flowlabel = np->flow_label;
1151 connected = 1; 1169 connected = 1;
1152 } 1170 }
@@ -1261,8 +1279,8 @@ do_append_data:
1261 if (dst) { 1279 if (dst) {
1262 if (connected) { 1280 if (connected) {
1263 ip6_dst_store(sk, dst, 1281 ip6_dst_store(sk, dst,
1264 ipv6_addr_equal(&fl6.daddr, &np->daddr) ? 1282 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1265 &np->daddr : NULL, 1283 &sk->sk_v6_daddr : NULL,
1266#ifdef CONFIG_IPV6_SUBTREES 1284#ifdef CONFIG_IPV6_SUBTREES
1267 ipv6_addr_equal(&fl6.saddr, &np->saddr) ? 1285 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1268 &np->saddr : 1286 &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/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/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_ip6.c b/net/l2tp/l2tp_ip6.c
index b8a6039314e8..cfd65304be60 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 }
@@ -537,8 +537,8 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
537 * sk->sk_dst_cache. 537 * sk->sk_dst_cache.
538 */ 538 */
539 if (sk->sk_state == TCP_ESTABLISHED && 539 if (sk->sk_state == TCP_ESTABLISHED &&
540 ipv6_addr_equal(daddr, &np->daddr)) 540 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
541 daddr = &np->daddr; 541 daddr = &sk->sk_v6_daddr;
542 542
543 if (addr_len >= sizeof(struct sockaddr_in6) && 543 if (addr_len >= sizeof(struct sockaddr_in6) &&
544 lsa->l2tp_scope_id && 544 lsa->l2tp_scope_id &&
@@ -548,7 +548,7 @@ static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk,
548 if (sk->sk_state != TCP_ESTABLISHED) 548 if (sk->sk_state != TCP_ESTABLISHED)
549 return -EDESTADDRREQ; 549 return -EDESTADDRREQ;
550 550
551 daddr = &np->daddr; 551 daddr = &sk->sk_v6_daddr;
552 fl6.flowlabel = np->flow_label; 552 fl6.flowlabel = np->flow_label;
553 } 553 }
554 554
diff --git a/net/l2tp/l2tp_netlink.c b/net/l2tp/l2tp_netlink.c
index 0825ff26e113..be446d517bc9 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
diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c
index 8c46b271064a..ffda81ef1a70 100644
--- a/net/l2tp/l2tp_ppp.c
+++ b/net/l2tp/l2tp_ppp.c
@@ -910,8 +910,8 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
910#if IS_ENABLED(CONFIG_IPV6) 910#if IS_ENABLED(CONFIG_IPV6)
911 } else if ((tunnel->version == 2) && 911 } else if ((tunnel->version == 2) &&
912 (tunnel->sock->sk_family == AF_INET6)) { 912 (tunnel->sock->sk_family == AF_INET6)) {
913 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
914 struct sockaddr_pppol2tpin6 sp; 913 struct sockaddr_pppol2tpin6 sp;
914
915 len = sizeof(sp); 915 len = sizeof(sp);
916 memset(&sp, 0, len); 916 memset(&sp, 0, len);
917 sp.sa_family = AF_PPPOX; 917 sp.sa_family = AF_PPPOX;
@@ -924,13 +924,13 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
924 sp.pppol2tp.d_session = session->peer_session_id; 924 sp.pppol2tp.d_session = session->peer_session_id;
925 sp.pppol2tp.addr.sin6_family = AF_INET6; 925 sp.pppol2tp.addr.sin6_family = AF_INET6;
926 sp.pppol2tp.addr.sin6_port = inet->inet_dport; 926 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
927 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 927 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
928 sizeof(np->daddr)); 928 sizeof(tunnel->sock->sk_v6_daddr));
929 memcpy(uaddr, &sp, len); 929 memcpy(uaddr, &sp, len);
930 } else if ((tunnel->version == 3) && 930 } else if ((tunnel->version == 3) &&
931 (tunnel->sock->sk_family == AF_INET6)) { 931 (tunnel->sock->sk_family == AF_INET6)) {
932 struct ipv6_pinfo *np = inet6_sk(tunnel->sock);
933 struct sockaddr_pppol2tpv3in6 sp; 932 struct sockaddr_pppol2tpv3in6 sp;
933
934 len = sizeof(sp); 934 len = sizeof(sp);
935 memset(&sp, 0, len); 935 memset(&sp, 0, len);
936 sp.sa_family = AF_PPPOX; 936 sp.sa_family = AF_PPPOX;
@@ -943,8 +943,8 @@ static int pppol2tp_getname(struct socket *sock, struct sockaddr *uaddr,
943 sp.pppol2tp.d_session = session->peer_session_id; 943 sp.pppol2tp.d_session = session->peer_session_id;
944 sp.pppol2tp.addr.sin6_family = AF_INET6; 944 sp.pppol2tp.addr.sin6_family = AF_INET6;
945 sp.pppol2tp.addr.sin6_port = inet->inet_dport; 945 sp.pppol2tp.addr.sin6_port = inet->inet_dport;
946 memcpy(&sp.pppol2tp.addr.sin6_addr, &np->daddr, 946 memcpy(&sp.pppol2tp.addr.sin6_addr, &tunnel->sock->sk_v6_daddr,
947 sizeof(np->daddr)); 947 sizeof(tunnel->sock->sk_v6_daddr));
948 memcpy(uaddr, &sp, len); 948 memcpy(uaddr, &sp, len);
949#endif 949#endif
950 } else if (tunnel->version == 3) { 950 } else if (tunnel->version == 3) {
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..95667b088c5b 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 */
@@ -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
@@ -2402,7 +2504,7 @@ static int ieee80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *dev,
2402 2504
2403 /* no change, but if automatic follow powersave */ 2505 /* no change, but if automatic follow powersave */
2404 sdata_lock(sdata); 2506 sdata_lock(sdata);
2405 __ieee80211_request_smps(sdata, sdata->u.mgd.req_smps); 2507 __ieee80211_request_smps_mgd(sdata, sdata->u.mgd.req_smps);
2406 sdata_unlock(sdata); 2508 sdata_unlock(sdata);
2407 2509
2408 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS) 2510 if (local->hw.flags & IEEE80211_HW_SUPPORTS_DYNAMIC_PS)
@@ -2860,35 +2962,55 @@ void ieee80211_csa_finalize_work(struct work_struct *work)
2860 container_of(work, struct ieee80211_sub_if_data, 2962 container_of(work, struct ieee80211_sub_if_data,
2861 csa_finalize_work); 2963 csa_finalize_work);
2862 struct ieee80211_local *local = sdata->local; 2964 struct ieee80211_local *local = sdata->local;
2863 int err, changed; 2965 int err, changed = 0;
2864 2966
2865 if (!ieee80211_sdata_running(sdata)) 2967 if (!ieee80211_sdata_running(sdata))
2866 return; 2968 return;
2867 2969
2868 if (WARN_ON(sdata->vif.type != NL80211_IFTYPE_AP))
2869 return;
2870
2871 sdata->radar_required = sdata->csa_radar_required; 2970 sdata->radar_required = sdata->csa_radar_required;
2872 err = ieee80211_vif_change_channel(sdata, &local->csa_chandef, 2971 err = ieee80211_vif_change_channel(sdata, &local->csa_chandef,
2873 &changed); 2972 &changed);
2874 if (WARN_ON(err < 0)) 2973 if (WARN_ON(err < 0))
2875 return; 2974 return;
2876 2975
2877 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon); 2976 if (!local->use_chanctx) {
2878 if (err < 0) 2977 local->_oper_chandef = local->csa_chandef;
2879 return; 2978 ieee80211_hw_config(local, 0);
2979 }
2880 2980
2881 changed |= err; 2981 ieee80211_bss_info_change_notify(sdata, changed);
2882 kfree(sdata->u.ap.next_beacon); 2982
2883 sdata->u.ap.next_beacon = NULL; 2983 switch (sdata->vif.type) {
2984 case NL80211_IFTYPE_AP:
2985 err = ieee80211_assign_beacon(sdata, sdata->u.ap.next_beacon);
2986 if (err < 0)
2987 return;
2988 changed |= err;
2989 kfree(sdata->u.ap.next_beacon);
2990 sdata->u.ap.next_beacon = NULL;
2991
2992 ieee80211_bss_info_change_notify(sdata, err);
2993 break;
2994 case NL80211_IFTYPE_ADHOC:
2995 ieee80211_ibss_finish_csa(sdata);
2996 break;
2997#ifdef CONFIG_MAC80211_MESH
2998 case NL80211_IFTYPE_MESH_POINT:
2999 err = ieee80211_mesh_finish_csa(sdata);
3000 if (err < 0)
3001 return;
3002 break;
3003#endif
3004 default:
3005 WARN_ON(1);
3006 return;
3007 }
2884 sdata->vif.csa_active = false; 3008 sdata->vif.csa_active = false;
2885 3009
2886 ieee80211_wake_queues_by_reason(&sdata->local->hw, 3010 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2887 IEEE80211_MAX_QUEUE_MAP, 3011 IEEE80211_MAX_QUEUE_MAP,
2888 IEEE80211_QUEUE_STOP_REASON_CSA); 3012 IEEE80211_QUEUE_STOP_REASON_CSA);
2889 3013
2890 ieee80211_bss_info_change_notify(sdata, changed);
2891
2892 cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef); 3014 cfg80211_ch_switch_notify(sdata->dev, &local->csa_chandef);
2893} 3015}
2894 3016
@@ -2899,6 +3021,7 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2899 struct ieee80211_local *local = sdata->local; 3021 struct ieee80211_local *local = sdata->local;
2900 struct ieee80211_chanctx_conf *chanctx_conf; 3022 struct ieee80211_chanctx_conf *chanctx_conf;
2901 struct ieee80211_chanctx *chanctx; 3023 struct ieee80211_chanctx *chanctx;
3024 struct ieee80211_if_mesh __maybe_unused *ifmsh;
2902 int err, num_chanctx; 3025 int err, num_chanctx;
2903 3026
2904 if (!list_empty(&local->roc_list) || local->scanning) 3027 if (!list_empty(&local->roc_list) || local->scanning)
@@ -2936,20 +3059,76 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2936 if (sdata->vif.csa_active) 3059 if (sdata->vif.csa_active)
2937 return -EBUSY; 3060 return -EBUSY;
2938 3061
2939 /* only handle AP for now. */
2940 switch (sdata->vif.type) { 3062 switch (sdata->vif.type) {
2941 case NL80211_IFTYPE_AP: 3063 case NL80211_IFTYPE_AP:
3064 sdata->csa_counter_offset_beacon =
3065 params->counter_offset_beacon;
3066 sdata->csa_counter_offset_presp = params->counter_offset_presp;
3067 sdata->u.ap.next_beacon =
3068 cfg80211_beacon_dup(&params->beacon_after);
3069 if (!sdata->u.ap.next_beacon)
3070 return -ENOMEM;
3071
3072 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
3073 if (err < 0) {
3074 kfree(sdata->u.ap.next_beacon);
3075 return err;
3076 }
2942 break; 3077 break;
3078 case NL80211_IFTYPE_ADHOC:
3079 if (!sdata->vif.bss_conf.ibss_joined)
3080 return -EINVAL;
3081
3082 if (params->chandef.width != sdata->u.ibss.chandef.width)
3083 return -EINVAL;
3084
3085 switch (params->chandef.width) {
3086 case NL80211_CHAN_WIDTH_40:
3087 if (cfg80211_get_chandef_type(&params->chandef) !=
3088 cfg80211_get_chandef_type(&sdata->u.ibss.chandef))
3089 return -EINVAL;
3090 case NL80211_CHAN_WIDTH_5:
3091 case NL80211_CHAN_WIDTH_10:
3092 case NL80211_CHAN_WIDTH_20_NOHT:
3093 case NL80211_CHAN_WIDTH_20:
3094 break;
3095 default:
3096 return -EINVAL;
3097 }
3098
3099 /* changes into another band are not supported */
3100 if (sdata->u.ibss.chandef.chan->band !=
3101 params->chandef.chan->band)
3102 return -EINVAL;
3103
3104 err = ieee80211_ibss_csa_beacon(sdata, params);
3105 if (err < 0)
3106 return err;
3107 break;
3108#ifdef CONFIG_MAC80211_MESH
3109 case NL80211_IFTYPE_MESH_POINT:
3110 ifmsh = &sdata->u.mesh;
3111
3112 if (!ifmsh->mesh_id)
3113 return -EINVAL;
3114
3115 if (params->chandef.width != sdata->vif.bss_conf.chandef.width)
3116 return -EINVAL;
3117
3118 /* changes into another band are not supported */
3119 if (sdata->vif.bss_conf.chandef.chan->band !=
3120 params->chandef.chan->band)
3121 return -EINVAL;
3122
3123 err = ieee80211_mesh_csa_beacon(sdata, params, true);
3124 if (err < 0)
3125 return err;
3126 break;
3127#endif
2943 default: 3128 default:
2944 return -EOPNOTSUPP; 3129 return -EOPNOTSUPP;
2945 } 3130 }
2946 3131
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; 3132 sdata->csa_radar_required = params->radar_required;
2954 3133
2955 if (params->block_tx) 3134 if (params->block_tx)
@@ -2957,10 +3136,6 @@ static int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
2957 IEEE80211_MAX_QUEUE_MAP, 3136 IEEE80211_MAX_QUEUE_MAP,
2958 IEEE80211_QUEUE_STOP_REASON_CSA); 3137 IEEE80211_QUEUE_STOP_REASON_CSA);
2959 3138
2960 err = ieee80211_assign_beacon(sdata, &params->beacon_csa);
2961 if (err < 0)
2962 return err;
2963
2964 local->csa_chandef = params->chandef; 3139 local->csa_chandef = params->chandef;
2965 sdata->vif.csa_active = true; 3140 sdata->vif.csa_active = true;
2966 3141
@@ -3014,7 +3189,8 @@ static int ieee80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
3014 need_offchan = true; 3189 need_offchan = true;
3015 if (!ieee80211_is_action(mgmt->frame_control) || 3190 if (!ieee80211_is_action(mgmt->frame_control) ||
3016 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC || 3191 mgmt->u.action.category == WLAN_CATEGORY_PUBLIC ||
3017 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED) 3192 mgmt->u.action.category == WLAN_CATEGORY_SELF_PROTECTED ||
3193 mgmt->u.action.category == WLAN_CATEGORY_SPECTRUM_MGMT)
3018 break; 3194 break;
3019 rcu_read_lock(); 3195 rcu_read_lock();
3020 sta = sta_info_get(sdata, mgmt->da); 3196 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..531be040b9ae 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,315 @@ 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 params.count = csa_ie.count;
827 params.chandef = csa_ie.chandef;
828
829 if (ifibss->chandef.chan->band != params.chandef.chan->band)
830 goto disconnect;
831
832 switch (ifibss->chandef.width) {
833 case NL80211_CHAN_WIDTH_20_NOHT:
834 case NL80211_CHAN_WIDTH_20:
835 case NL80211_CHAN_WIDTH_40:
836 /* keep our current HT mode (HT20/HT40+/HT40-), even if
837 * another mode has been announced. The mode is not adopted
838 * within the beacon while doing CSA and we should therefore
839 * keep the mode which we announce.
840 */
841 ch_type = cfg80211_get_chandef_type(&ifibss->chandef);
842 cfg80211_chandef_create(&params.chandef, params.chandef.chan,
843 ch_type);
844 break;
845 case NL80211_CHAN_WIDTH_5:
846 case NL80211_CHAN_WIDTH_10:
847 if (params.chandef.width != ifibss->chandef.width) {
848 sdata_info(sdata,
849 "IBSS %pM received channel switch from incompatible channel width (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
850 ifibss->bssid,
851 params.chandef.chan->center_freq,
852 params.chandef.width,
853 params.chandef.center_freq1,
854 params.chandef.center_freq2);
855 goto disconnect;
856 }
857 break;
858 default:
859 /* should not happen, sta_flags should prevent VHT modes. */
860 WARN_ON(1);
861 goto disconnect;
862 }
863
864 if (!cfg80211_reg_can_beacon(sdata->local->hw.wiphy, &params.chandef)) {
865 sdata_info(sdata,
866 "IBSS %pM switches to unsupported channel (%d MHz, width:%d, CF1/2: %d/%d MHz), disconnecting\n",
867 ifibss->bssid,
868 params.chandef.chan->center_freq,
869 params.chandef.width,
870 params.chandef.center_freq1,
871 params.chandef.center_freq2);
872 goto disconnect;
873 }
874
875 err = cfg80211_chandef_dfs_required(sdata->local->hw.wiphy,
876 &params.chandef);
877 if (err < 0)
878 goto disconnect;
879 if (err) {
880 /* IBSS-DFS only allowed with a control program */
881 if (!ifibss->userspace_handles_dfs)
882 goto disconnect;
883
884 params.radar_required = true;
885 }
886
887 rcu_read_lock();
888 chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
889 if (!chanctx_conf) {
890 rcu_read_unlock();
891 goto disconnect;
892 }
893
894 /* don't handle for multi-VIF cases */
895 chanctx = container_of(chanctx_conf, struct ieee80211_chanctx, conf);
896 if (chanctx->refcount > 1) {
897 rcu_read_unlock();
898 goto disconnect;
899 }
900 num_chanctx = 0;
901 list_for_each_entry_rcu(chanctx, &sdata->local->chanctx_list, list)
902 num_chanctx++;
903
904 if (num_chanctx > 1) {
905 rcu_read_unlock();
906 goto disconnect;
907 }
908 rcu_read_unlock();
909
910 /* all checks done, now perform the channel switch. */
911 ibss_dbg(sdata,
912 "received channel switch announcement to go to channel %d MHz\n",
913 params.chandef.chan->center_freq);
914
915 params.block_tx = !!csa_ie.mode;
916
917 ieee80211_ibss_csa_beacon(sdata, &params);
918 sdata->csa_radar_required = params.radar_required;
919
920 if (params.block_tx)
921 ieee80211_stop_queues_by_reason(&sdata->local->hw,
922 IEEE80211_MAX_QUEUE_MAP,
923 IEEE80211_QUEUE_STOP_REASON_CSA);
924
925 sdata->local->csa_chandef = params.chandef;
926 sdata->vif.csa_active = true;
927
928 ieee80211_bss_info_change_notify(sdata, err);
929 drv_channel_switch_beacon(sdata, &params.chandef);
930
931 ieee80211_ibss_csa_mark_radar(sdata);
932
933 return true;
934disconnect:
935 ibss_dbg(sdata, "Can't handle channel switch, disconnect\n");
936 ieee80211_queue_work(&sdata->local->hw,
937 &ifibss->csa_connection_drop_work);
938
939 ieee80211_ibss_csa_mark_radar(sdata);
940
941 return true;
942}
943
944static void
945ieee80211_rx_mgmt_spectrum_mgmt(struct ieee80211_sub_if_data *sdata,
946 struct ieee80211_mgmt *mgmt, size_t len,
947 struct ieee80211_rx_status *rx_status,
948 struct ieee802_11_elems *elems)
949{
950 int required_len;
951
952 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
953 return;
954
955 /* CSA is the only action we handle for now */
956 if (mgmt->u.action.u.measurement.action_code !=
957 WLAN_ACTION_SPCT_CHL_SWITCH)
958 return;
959
960 required_len = IEEE80211_MIN_ACTION_SIZE +
961 sizeof(mgmt->u.action.u.chan_switch);
962 if (len < required_len)
963 return;
964
965 ieee80211_ibss_process_chanswitch(sdata, elems, false);
966}
967
502static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata, 968static void ieee80211_rx_mgmt_deauth_ibss(struct ieee80211_sub_if_data *sdata,
503 struct ieee80211_mgmt *mgmt, 969 struct ieee80211_mgmt *mgmt,
504 size_t len) 970 size_t len)
@@ -661,10 +1127,6 @@ static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
661 1127
662 /* check if we need to merge IBSS */ 1128 /* check if we need to merge IBSS */
663 1129
664 /* we use a fixed BSSID */
665 if (sdata->u.ibss.fixed_bssid)
666 goto put_bss;
667
668 /* not an IBSS */ 1130 /* not an IBSS */
669 if (!(cbss->capability & WLAN_CAPABILITY_IBSS)) 1131 if (!(cbss->capability & WLAN_CAPABILITY_IBSS))
670 goto put_bss; 1132 goto put_bss;
@@ -680,10 +1142,18 @@ static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
680 sdata->u.ibss.ssid_len)) 1142 sdata->u.ibss.ssid_len))
681 goto put_bss; 1143 goto put_bss;
682 1144
1145 /* process channel switch */
1146 if (ieee80211_ibss_process_chanswitch(sdata, elems, true))
1147 goto put_bss;
1148
683 /* same BSSID */ 1149 /* same BSSID */
684 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid)) 1150 if (ether_addr_equal(cbss->bssid, sdata->u.ibss.bssid))
685 goto put_bss; 1151 goto put_bss;
686 1152
1153 /* we use a fixed BSSID */
1154 if (sdata->u.ibss.fixed_bssid)
1155 goto put_bss;
1156
687 if (ieee80211_have_rx_timestamp(rx_status)) { 1157 if (ieee80211_have_rx_timestamp(rx_status)) {
688 /* time when timestamp field was received */ 1158 /* time when timestamp field was received */
689 rx_timestamp = 1159 rx_timestamp =
@@ -775,30 +1245,6 @@ void ieee80211_ibss_rx_no_sta(struct ieee80211_sub_if_data *sdata,
775 ieee80211_queue_work(&local->hw, &sdata->work); 1245 ieee80211_queue_work(&local->hw, &sdata->work);
776} 1246}
777 1247
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) 1248static void ieee80211_ibss_sta_expire(struct ieee80211_sub_if_data *sdata)
803{ 1249{
804 struct ieee80211_local *local = sdata->local; 1250 struct ieee80211_local *local = sdata->local;
@@ -1076,6 +1522,8 @@ void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1076 struct ieee80211_rx_status *rx_status; 1522 struct ieee80211_rx_status *rx_status;
1077 struct ieee80211_mgmt *mgmt; 1523 struct ieee80211_mgmt *mgmt;
1078 u16 fc; 1524 u16 fc;
1525 struct ieee802_11_elems elems;
1526 int ies_len;
1079 1527
1080 rx_status = IEEE80211_SKB_RXCB(skb); 1528 rx_status = IEEE80211_SKB_RXCB(skb);
1081 mgmt = (struct ieee80211_mgmt *) skb->data; 1529 mgmt = (struct ieee80211_mgmt *) skb->data;
@@ -1101,6 +1549,27 @@ void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1101 case IEEE80211_STYPE_DEAUTH: 1549 case IEEE80211_STYPE_DEAUTH:
1102 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len); 1550 ieee80211_rx_mgmt_deauth_ibss(sdata, mgmt, skb->len);
1103 break; 1551 break;
1552 case IEEE80211_STYPE_ACTION:
1553 switch (mgmt->u.action.category) {
1554 case WLAN_CATEGORY_SPECTRUM_MGMT:
1555 ies_len = skb->len -
1556 offsetof(struct ieee80211_mgmt,
1557 u.action.u.chan_switch.variable);
1558
1559 if (ies_len < 0)
1560 break;
1561
1562 ieee802_11_parse_elems(
1563 mgmt->u.action.u.chan_switch.variable,
1564 ies_len, true, &elems);
1565
1566 if (elems.parse_error)
1567 break;
1568
1569 ieee80211_rx_mgmt_spectrum_mgmt(sdata, mgmt, skb->len,
1570 rx_status, &elems);
1571 break;
1572 }
1104 } 1573 }
1105 1574
1106 mgmt_out: 1575 mgmt_out:
@@ -1167,6 +1636,8 @@ void ieee80211_ibss_setup_sdata(struct ieee80211_sub_if_data *sdata)
1167 (unsigned long) sdata); 1636 (unsigned long) sdata);
1168 INIT_LIST_HEAD(&ifibss->incomplete_stations); 1637 INIT_LIST_HEAD(&ifibss->incomplete_stations);
1169 spin_lock_init(&ifibss->incomplete_lock); 1638 spin_lock_init(&ifibss->incomplete_lock);
1639 INIT_WORK(&ifibss->csa_connection_drop_work,
1640 ieee80211_csa_connection_drop_work);
1170} 1641}
1171 1642
1172/* scan finished notification */ 1643/* scan finished notification */
@@ -1202,6 +1673,7 @@ int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1202 1673
1203 sdata->u.ibss.privacy = params->privacy; 1674 sdata->u.ibss.privacy = params->privacy;
1204 sdata->u.ibss.control_port = params->control_port; 1675 sdata->u.ibss.control_port = params->control_port;
1676 sdata->u.ibss.userspace_handles_dfs = params->userspace_handles_dfs;
1205 sdata->u.ibss.basic_rates = params->basic_rates; 1677 sdata->u.ibss.basic_rates = params->basic_rates;
1206 1678
1207 /* fix basic_rates if channel does not support these rates */ 1679 /* fix basic_rates if channel does not support these rates */
@@ -1265,73 +1737,19 @@ int ieee80211_ibss_join(struct ieee80211_sub_if_data *sdata,
1265int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata) 1737int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1266{ 1738{
1267 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 1739 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 1740
1280 if (ifibss->privacy) 1741 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; 1742 ifibss->ssid_len = 0;
1298 1743 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 1744
1315 /* remove beacon */ 1745 /* remove beacon */
1316 kfree(sdata->u.ibss.ie); 1746 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 1747
1321 /* on the next join, re-program HT parameters */ 1748 /* on the next join, re-program HT parameters */
1322 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa)); 1749 memset(&ifibss->ht_capa, 0, sizeof(ifibss->ht_capa));
1323 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask)); 1750 memset(&ifibss->ht_capa_mask, 0, sizeof(ifibss->ht_capa_mask));
1324 1751
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(); 1752 synchronize_rcu();
1334 kfree(presp);
1335 1753
1336 skb_queue_purge(&sdata->skb_queue); 1754 skb_queue_purge(&sdata->skb_queue);
1337 1755
diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h
index 611abfcfb5eb..29dc505be125 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,14 @@ 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};
1232
1209/* Parsed Information Elements */ 1233/* Parsed Information Elements */
1210struct ieee802_11_elems { 1234struct ieee802_11_elems {
1211 const u8 *ie_start; 1235 const u8 *ie_start;
@@ -1242,6 +1266,7 @@ struct ieee802_11_elems {
1242 const struct ieee80211_timeout_interval_ie *timeout_int; 1266 const struct ieee80211_timeout_interval_ie *timeout_int;
1243 const u8 *opmode_notif; 1267 const u8 *opmode_notif;
1244 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs; 1268 const struct ieee80211_sec_chan_offs_ie *sec_chan_offs;
1269 const struct ieee80211_mesh_chansw_params_ie *mesh_chansw_params_ie;
1245 1270
1246 /* length of them, respectively */ 1271 /* length of them, respectively */
1247 u8 ssid_len; 1272 u8 ssid_len;
@@ -1333,11 +1358,19 @@ int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata);
1333void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata); 1358void ieee80211_ibss_work(struct ieee80211_sub_if_data *sdata);
1334void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, 1359void ieee80211_ibss_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1335 struct sk_buff *skb); 1360 struct sk_buff *skb);
1361int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
1362 struct cfg80211_csa_settings *csa_settings);
1363int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata);
1364void ieee80211_ibss_stop(struct ieee80211_sub_if_data *sdata);
1336 1365
1337/* mesh code */ 1366/* mesh code */
1338void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata); 1367void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata);
1339void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata, 1368void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
1340 struct sk_buff *skb); 1369 struct sk_buff *skb);
1370int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1371 struct cfg80211_csa_settings *csa_settings,
1372 bool csa_action);
1373int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata);
1341 1374
1342/* scan/BSS handling */ 1375/* scan/BSS handling */
1343void ieee80211_scan_work(struct work_struct *work); 1376void ieee80211_scan_work(struct work_struct *work);
@@ -1434,7 +1467,10 @@ void ieee80211_send_delba(struct ieee80211_sub_if_data *sdata,
1434int ieee80211_send_smps_action(struct ieee80211_sub_if_data *sdata, 1467int ieee80211_send_smps_action(struct ieee80211_sub_if_data *sdata,
1435 enum ieee80211_smps_mode smps, const u8 *da, 1468 enum ieee80211_smps_mode smps, const u8 *da,
1436 const u8 *bssid); 1469 const u8 *bssid);
1437void ieee80211_request_smps_work(struct work_struct *work); 1470void ieee80211_request_smps_ap_work(struct work_struct *work);
1471void ieee80211_request_smps_mgd_work(struct work_struct *work);
1472bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
1473 enum ieee80211_smps_mode smps_mode_new);
1438 1474
1439void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid, 1475void ___ieee80211_stop_rx_ba_session(struct sta_info *sta, u16 tid,
1440 u16 initiator, u16 reason, bool stop); 1476 u16 initiator, u16 reason, bool stop);
@@ -1484,6 +1520,28 @@ void ieee80211_apply_vhtcap_overrides(struct ieee80211_sub_if_data *sdata,
1484void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata, 1520void ieee80211_process_measurement_req(struct ieee80211_sub_if_data *sdata,
1485 struct ieee80211_mgmt *mgmt, 1521 struct ieee80211_mgmt *mgmt,
1486 size_t len); 1522 size_t len);
1523/**
1524 * ieee80211_parse_ch_switch_ie - parses channel switch IEs
1525 * @sdata: the sdata of the interface which has received the frame
1526 * @elems: parsed 802.11 elements received with the frame
1527 * @beacon: indicates if the frame was a beacon or probe response
1528 * @current_band: indicates the current band
1529 * @sta_flags: contains information about own capabilities and restrictions
1530 * to decide which channel switch announcements can be accepted. Only the
1531 * following subset of &enum ieee80211_sta_flags are evaluated:
1532 * %IEEE80211_STA_DISABLE_HT, %IEEE80211_STA_DISABLE_VHT,
1533 * %IEEE80211_STA_DISABLE_40MHZ, %IEEE80211_STA_DISABLE_80P80MHZ,
1534 * %IEEE80211_STA_DISABLE_160MHZ.
1535 * @bssid: the currently connected bssid (for reporting)
1536 * @csa_ie: parsed 802.11 csa elements on count, mode, chandef and mesh ttl.
1537 All of them will be filled with if success only.
1538 * Return: 0 on success, <0 on error and >0 if there is nothing to parse.
1539 */
1540int ieee80211_parse_ch_switch_ie(struct ieee80211_sub_if_data *sdata,
1541 struct ieee802_11_elems *elems, bool beacon,
1542 enum ieee80211_band current_band,
1543 u32 sta_flags, u8 *bssid,
1544 struct ieee80211_csa_ie *csa_ie);
1487 1545
1488/* Suspend/resume and hw reconfiguration */ 1546/* Suspend/resume and hw reconfiguration */
1489int ieee80211_reconfig(struct ieee80211_local *local); 1547int ieee80211_reconfig(struct ieee80211_local *local);
@@ -1629,8 +1687,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, 1687u32 ieee80211_sta_get_rates(struct ieee80211_sub_if_data *sdata,
1630 struct ieee802_11_elems *elems, 1688 struct ieee802_11_elems *elems,
1631 enum ieee80211_band band, u32 *basic_rates); 1689 enum ieee80211_band band, u32 *basic_rates);
1632int __ieee80211_request_smps(struct ieee80211_sub_if_data *sdata, 1690int __ieee80211_request_smps_mgd(struct ieee80211_sub_if_data *sdata,
1633 enum ieee80211_smps_mode smps_mode); 1691 enum ieee80211_smps_mode smps_mode);
1692int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
1693 enum ieee80211_smps_mode smps_mode);
1634void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata); 1694void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata);
1635 1695
1636size_t ieee80211_ie_split(const u8 *ies, size_t ielen, 1696size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
@@ -1657,6 +1717,7 @@ int ieee80211_add_ext_srates_ie(struct ieee80211_sub_if_data *sdata,
1657void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan, 1717void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
1658 const struct ieee80211_ht_operation *ht_oper, 1718 const struct ieee80211_ht_operation *ht_oper,
1659 struct cfg80211_chan_def *chandef); 1719 struct cfg80211_chan_def *chandef);
1720u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c);
1660 1721
1661int __must_check 1722int __must_check
1662ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata, 1723ieee80211_vif_use_channel(struct ieee80211_sub_if_data *sdata,
@@ -1685,6 +1746,8 @@ void ieee80211_dfs_cac_timer(unsigned long data);
1685void ieee80211_dfs_cac_timer_work(struct work_struct *work); 1746void ieee80211_dfs_cac_timer_work(struct work_struct *work);
1686void ieee80211_dfs_cac_cancel(struct ieee80211_local *local); 1747void ieee80211_dfs_cac_cancel(struct ieee80211_local *local);
1687void ieee80211_dfs_radar_detected_work(struct work_struct *work); 1748void ieee80211_dfs_radar_detected_work(struct work_struct *work);
1749int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
1750 struct cfg80211_csa_settings *csa_settings);
1688 1751
1689#ifdef CONFIG_MAC80211_NOINLINE 1752#ifdef CONFIG_MAC80211_NOINLINE
1690#define debug_noinline noinline 1753#define debug_noinline noinline
diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c
index fcecd633514e..ff101ea1d9ae 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 *
@@ -1289,7 +1293,10 @@ static void ieee80211_setup_sdata(struct ieee80211_sub_if_data *sdata,
1289 case NL80211_IFTYPE_AP: 1293 case NL80211_IFTYPE_AP:
1290 skb_queue_head_init(&sdata->u.ap.ps.bc_buf); 1294 skb_queue_head_init(&sdata->u.ap.ps.bc_buf);
1291 INIT_LIST_HEAD(&sdata->u.ap.vlans); 1295 INIT_LIST_HEAD(&sdata->u.ap.vlans);
1296 INIT_WORK(&sdata->u.ap.request_smps_work,
1297 ieee80211_request_smps_ap_work);
1292 sdata->vif.bss_conf.bssid = sdata->vif.addr; 1298 sdata->vif.bss_conf.bssid = sdata->vif.addr;
1299 sdata->u.ap.req_smps = IEEE80211_SMPS_OFF;
1293 break; 1300 break;
1294 case NL80211_IFTYPE_P2P_CLIENT: 1301 case NL80211_IFTYPE_P2P_CLIENT:
1295 type = NL80211_IFTYPE_STATION; 1302 type = NL80211_IFTYPE_STATION;
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/mesh.c b/net/mac80211/mesh.c
index 707ac61d63e5..896fe3bd599e 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,127 @@ 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 else
949 ifmsh->chsw_ttl = 0;
950
951 if (ifmsh->chsw_ttl > 0)
952 if (ieee80211_mesh_csa_beacon(sdata, &params, false) < 0)
953 return false;
954
955 sdata->csa_radar_required = params.radar_required;
956
957 if (params.block_tx)
958 ieee80211_stop_queues_by_reason(&sdata->local->hw,
959 IEEE80211_MAX_QUEUE_MAP,
960 IEEE80211_QUEUE_STOP_REASON_CSA);
961
962 sdata->local->csa_chandef = params.chandef;
963 sdata->vif.csa_active = true;
964
965 ieee80211_bss_info_change_notify(sdata, err);
966 drv_channel_switch_beacon(sdata, &params.chandef);
967
968 return true;
969failed_chswitch:
970 rcu_read_unlock();
971 return false;
972}
973
815static void 974static void
816ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata, 975ieee80211_mesh_rx_probe_req(struct ieee80211_sub_if_data *sdata,
817 struct ieee80211_mgmt *mgmt, size_t len) 976 struct ieee80211_mgmt *mgmt, size_t len)
@@ -918,6 +1077,142 @@ static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
918 if (ifmsh->sync_ops) 1077 if (ifmsh->sync_ops)
919 ifmsh->sync_ops->rx_bcn_presp(sdata, 1078 ifmsh->sync_ops->rx_bcn_presp(sdata,
920 stype, mgmt, &elems, rx_status); 1079 stype, mgmt, &elems, rx_status);
1080
1081 if (!ifmsh->chsw_init)
1082 ieee80211_mesh_process_chnswitch(sdata, &elems, true);
1083}
1084
1085int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata)
1086{
1087 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1088 struct mesh_csa_settings *tmp_csa_settings;
1089 int ret = 0;
1090
1091 /* Reset the TTL value and Initiator flag */
1092 ifmsh->chsw_init = false;
1093 ifmsh->chsw_ttl = 0;
1094
1095 /* Remove the CSA and MCSP elements from the beacon */
1096 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1097 rcu_assign_pointer(ifmsh->csa, NULL);
1098 kfree_rcu(tmp_csa_settings, rcu_head);
1099 ret = ieee80211_mesh_rebuild_beacon(sdata);
1100 if (ret)
1101 return -EINVAL;
1102
1103 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1104
1105 mcsa_dbg(sdata, "complete switching to center freq %d MHz",
1106 sdata->vif.bss_conf.chandef.chan->center_freq);
1107 return 0;
1108}
1109
1110int ieee80211_mesh_csa_beacon(struct ieee80211_sub_if_data *sdata,
1111 struct cfg80211_csa_settings *csa_settings,
1112 bool csa_action)
1113{
1114 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1115 struct mesh_csa_settings *tmp_csa_settings;
1116 int ret = 0;
1117
1118 tmp_csa_settings = kmalloc(sizeof(*tmp_csa_settings),
1119 GFP_ATOMIC);
1120 if (!tmp_csa_settings)
1121 return -ENOMEM;
1122
1123 memcpy(&tmp_csa_settings->settings, csa_settings,
1124 sizeof(struct cfg80211_csa_settings));
1125
1126 rcu_assign_pointer(ifmsh->csa, tmp_csa_settings);
1127
1128 ret = ieee80211_mesh_rebuild_beacon(sdata);
1129 if (ret) {
1130 tmp_csa_settings = rcu_dereference(ifmsh->csa);
1131 rcu_assign_pointer(ifmsh->csa, NULL);
1132 kfree_rcu(tmp_csa_settings, rcu_head);
1133 return ret;
1134 }
1135
1136 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
1137
1138 if (csa_action)
1139 ieee80211_send_action_csa(sdata, csa_settings);
1140
1141 return 0;
1142}
1143
1144static int mesh_fwd_csa_frame(struct ieee80211_sub_if_data *sdata,
1145 struct ieee80211_mgmt *mgmt, size_t len)
1146{
1147 struct ieee80211_mgmt *mgmt_fwd;
1148 struct sk_buff *skb;
1149 struct ieee80211_local *local = sdata->local;
1150 u8 *pos = mgmt->u.action.u.chan_switch.variable;
1151 size_t offset_ttl;
1152
1153 skb = dev_alloc_skb(local->tx_headroom + len);
1154 if (!skb)
1155 return -ENOMEM;
1156 skb_reserve(skb, local->tx_headroom);
1157 mgmt_fwd = (struct ieee80211_mgmt *) skb_put(skb, len);
1158
1159 /* offset_ttl is based on whether the secondary channel
1160 * offset is available or not. Substract 1 from the mesh TTL
1161 * and disable the initiator flag before forwarding.
1162 */
1163 offset_ttl = (len < 42) ? 7 : 10;
1164 *(pos + offset_ttl) -= 1;
1165 *(pos + offset_ttl + 1) &= ~WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
1166 sdata->u.mesh.chsw_ttl = *(pos + offset_ttl);
1167
1168 memcpy(mgmt_fwd, mgmt, len);
1169 eth_broadcast_addr(mgmt_fwd->da);
1170 memcpy(mgmt_fwd->sa, sdata->vif.addr, ETH_ALEN);
1171 memcpy(mgmt_fwd->bssid, sdata->vif.addr, ETH_ALEN);
1172
1173 ieee80211_tx_skb(sdata, skb);
1174 return 0;
1175}
1176
1177static void mesh_rx_csa_frame(struct ieee80211_sub_if_data *sdata,
1178 struct ieee80211_mgmt *mgmt, size_t len)
1179{
1180 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
1181 struct ieee802_11_elems elems;
1182 u16 pre_value;
1183 bool fwd_csa = true;
1184 size_t baselen;
1185 u8 *pos, ttl;
1186
1187 if (mgmt->u.action.u.measurement.action_code !=
1188 WLAN_ACTION_SPCT_CHL_SWITCH)
1189 return;
1190
1191 pos = mgmt->u.action.u.chan_switch.variable;
1192 baselen = offsetof(struct ieee80211_mgmt,
1193 u.action.u.chan_switch.variable);
1194 ieee802_11_parse_elems(pos, len - baselen, false, &elems);
1195
1196 ttl = elems.mesh_chansw_params_ie->mesh_ttl;
1197 if (!--ttl)
1198 fwd_csa = false;
1199
1200 pre_value = le16_to_cpu(elems.mesh_chansw_params_ie->mesh_pre_value);
1201 if (ifmsh->pre_value >= pre_value)
1202 return;
1203
1204 ifmsh->pre_value = pre_value;
1205
1206 if (!ieee80211_mesh_process_chnswitch(sdata, &elems, false)) {
1207 mcsa_dbg(sdata, "Failed to process CSA action frame");
1208 return;
1209 }
1210
1211 /* forward or re-broadcast the CSA frame */
1212 if (fwd_csa) {
1213 if (mesh_fwd_csa_frame(sdata, mgmt, len) < 0)
1214 mcsa_dbg(sdata, "Failed to forward the CSA frame");
1215 }
921} 1216}
922 1217
923static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata, 1218static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
@@ -939,6 +1234,9 @@ static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
939 if (mesh_action_is_path_sel(mgmt)) 1234 if (mesh_action_is_path_sel(mgmt))
940 mesh_rx_path_sel_frame(sdata, mgmt, len); 1235 mesh_rx_path_sel_frame(sdata, mgmt, len);
941 break; 1236 break;
1237 case WLAN_CATEGORY_SPECTRUM_MGMT:
1238 mesh_rx_csa_frame(sdata, mgmt, len);
1239 break;
942 } 1240 }
943} 1241}
944 1242
@@ -1056,13 +1354,11 @@ void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
1056 (unsigned long) sdata); 1354 (unsigned long) sdata);
1057 1355
1058 ifmsh->accepting_plinks = true; 1356 ifmsh->accepting_plinks = true;
1059 ifmsh->preq_id = 0;
1060 ifmsh->sn = 0;
1061 ifmsh->num_gates = 0;
1062 atomic_set(&ifmsh->mpaths, 0); 1357 atomic_set(&ifmsh->mpaths, 0);
1063 mesh_rmc_init(sdata); 1358 mesh_rmc_init(sdata);
1064 ifmsh->last_preq = jiffies; 1359 ifmsh->last_preq = jiffies;
1065 ifmsh->next_perr = jiffies; 1360 ifmsh->next_perr = jiffies;
1361 ifmsh->chsw_init = false;
1066 /* Allocate all mesh structures when creating the first mesh interface. */ 1362 /* Allocate all mesh structures when creating the first mesh interface. */
1067 if (!mesh_allocated) 1363 if (!mesh_allocated)
1068 ieee80211s_init(); 1364 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..d7504ab61a34 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,15 +1907,9 @@ 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)
2102 ifmgd->flags |= IEEE80211_STA_BEACON_POLL;
2103 else
2104 ifmgd->flags |= IEEE80211_STA_CONNECTION_POLL;
2105
2106 mutex_unlock(&sdata->local->mtx); 1913 mutex_unlock(&sdata->local->mtx);
2107 1914
2108 if (already) 1915 if (already)
@@ -2174,6 +1981,7 @@ static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2174 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY, 1981 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY,
2175 true, frame_buf); 1982 true, frame_buf);
2176 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED; 1983 ifmgd->flags &= ~IEEE80211_STA_CSA_RECEIVED;
1984 sdata->vif.csa_active = false;
2177 ieee80211_wake_queues_by_reason(&sdata->local->hw, 1985 ieee80211_wake_queues_by_reason(&sdata->local->hw,
2178 IEEE80211_MAX_QUEUE_MAP, 1986 IEEE80211_MAX_QUEUE_MAP,
2179 IEEE80211_QUEUE_STOP_REASON_CSA); 1987 IEEE80211_QUEUE_STOP_REASON_CSA);
@@ -2717,7 +2525,7 @@ static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2717 */ 2525 */
2718 ifmgd->wmm_last_param_set = -1; 2526 ifmgd->wmm_last_param_set = -1;
2719 2527
2720 if (elems.wmm_param) 2528 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) && elems.wmm_param)
2721 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param, 2529 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
2722 elems.wmm_param_len); 2530 elems.wmm_param_len);
2723 else 2531 else
@@ -3061,17 +2869,10 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3061 } 2869 }
3062 } 2870 }
3063 2871
3064 if (ifmgd->flags & IEEE80211_STA_BEACON_POLL) { 2872 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL) {
3065 mlme_dbg_ratelimited(sdata, 2873 mlme_dbg_ratelimited(sdata,
3066 "cancelling AP probe due to a received beacon\n"); 2874 "cancelling AP probe due to a received beacon\n");
3067 mutex_lock(&local->mtx); 2875 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 } 2876 }
3076 2877
3077 /* 2878 /*
@@ -3152,7 +2953,8 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3152 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime, 2953 ieee80211_sta_process_chanswitch(sdata, rx_status->mactime,
3153 &elems, true); 2954 &elems, true);
3154 2955
3155 if (ieee80211_sta_wmm_params(local, sdata, elems.wmm_param, 2956 if (!(ifmgd->flags & IEEE80211_STA_DISABLE_WMM) &&
2957 ieee80211_sta_wmm_params(local, sdata, elems.wmm_param,
3156 elems.wmm_param_len)) 2958 elems.wmm_param_len))
3157 changed |= BSS_CHANGED_QOS; 2959 changed |= BSS_CHANGED_QOS;
3158 2960
@@ -3543,8 +3345,7 @@ void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3543 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started) 3345 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3544 run_again(sdata, ifmgd->assoc_data->timeout); 3346 run_again(sdata, ifmgd->assoc_data->timeout);
3545 3347
3546 if (ifmgd->flags & (IEEE80211_STA_BEACON_POLL | 3348 if (ifmgd->flags & IEEE80211_STA_CONNECTION_POLL &&
3547 IEEE80211_STA_CONNECTION_POLL) &&
3548 ifmgd->associated) { 3349 ifmgd->associated) {
3549 u8 bssid[ETH_ALEN]; 3350 u8 bssid[ETH_ALEN];
3550 int max_tries; 3351 int max_tries;
@@ -3697,7 +3498,7 @@ void ieee80211_sta_setup_sdata(struct ieee80211_sub_if_data *sdata)
3697 ieee80211_beacon_connection_loss_work); 3498 ieee80211_beacon_connection_loss_work);
3698 INIT_WORK(&ifmgd->csa_connection_drop_work, 3499 INIT_WORK(&ifmgd->csa_connection_drop_work,
3699 ieee80211_csa_connection_drop_work); 3500 ieee80211_csa_connection_drop_work);
3700 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_work); 3501 INIT_WORK(&ifmgd->request_smps_work, ieee80211_request_smps_mgd_work);
3701 setup_timer(&ifmgd->timer, ieee80211_sta_timer, 3502 setup_timer(&ifmgd->timer, ieee80211_sta_timer,
3702 (unsigned long) sdata); 3503 (unsigned long) sdata);
3703 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer, 3504 setup_timer(&ifmgd->bcn_mon_timer, ieee80211_sta_bcn_mon_timer,
@@ -3876,7 +3677,7 @@ static int ieee80211_prep_channel(struct ieee80211_sub_if_data *sdata,
3876 return ret; 3677 return ret;
3877 3678
3878 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) { 3679 while (ret && chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
3879 ifmgd->flags |= chandef_downgrade(&chandef); 3680 ifmgd->flags |= ieee80211_chandef_downgrade(&chandef);
3880 ret = ieee80211_vif_use_channel(sdata, &chandef, 3681 ret = ieee80211_vif_use_channel(sdata, &chandef,
3881 IEEE80211_CHANCTX_SHARED); 3682 IEEE80211_CHANCTX_SHARED);
3882 } 3683 }
@@ -4135,6 +3936,44 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4135 return err; 3936 return err;
4136} 3937}
4137 3938
3939static bool ieee80211_usable_wmm_params(struct ieee80211_sub_if_data *sdata,
3940 const u8 *wmm_param, int len)
3941{
3942 const u8 *pos;
3943 size_t left;
3944
3945 if (len < 8)
3946 return false;
3947
3948 if (wmm_param[5] != 1 /* version */)
3949 return false;
3950
3951 pos = wmm_param + 8;
3952 left = len - 8;
3953
3954 for (; left >= 4; left -= 4, pos += 4) {
3955 u8 aifsn = pos[0] & 0x0f;
3956 u8 ecwmin = pos[1] & 0x0f;
3957 u8 ecwmax = (pos[1] & 0xf0) >> 4;
3958 int aci = (pos[0] >> 5) & 0x03;
3959
3960 if (aifsn < 2) {
3961 sdata_info(sdata,
3962 "AP has invalid WMM params (AIFSN=%d for ACI %d), disabling WMM\n",
3963 aifsn, aci);
3964 return false;
3965 }
3966 if (ecwmin > ecwmax) {
3967 sdata_info(sdata,
3968 "AP has invalid WMM params (ECWmin/max=%d/%d for ACI %d), disabling WMM\n",
3969 ecwmin, ecwmax, aci);
3970 return false;
3971 }
3972 }
3973
3974 return true;
3975}
3976
4138int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata, 3977int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4139 struct cfg80211_assoc_request *req) 3978 struct cfg80211_assoc_request *req)
4140{ 3979{
@@ -4192,9 +4031,45 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4192 } 4031 }
4193 4032
4194 /* prepare assoc data */ 4033 /* prepare assoc data */
4195 4034
4196 ifmgd->beacon_crc_valid = false; 4035 ifmgd->beacon_crc_valid = false;
4197 4036
4037 assoc_data->wmm = bss->wmm_used &&
4038 (local->hw.queues >= IEEE80211_NUM_ACS);
4039 if (assoc_data->wmm) {
4040 /* try to check validity of WMM params IE */
4041 const struct cfg80211_bss_ies *ies;
4042 const u8 *wp, *start, *end;
4043
4044 rcu_read_lock();
4045 ies = rcu_dereference(req->bss->ies);
4046 start = ies->data;
4047 end = start + ies->len;
4048
4049 while (true) {
4050 wp = cfg80211_find_vendor_ie(
4051 WLAN_OUI_MICROSOFT,
4052 WLAN_OUI_TYPE_MICROSOFT_WMM,
4053 start, end - start);
4054 if (!wp)
4055 break;
4056 start = wp + wp[1] + 2;
4057 /* if this IE is too short, try the next */
4058 if (wp[1] <= 4)
4059 continue;
4060 /* if this IE is WMM params, we found what we wanted */
4061 if (wp[6] == 1)
4062 break;
4063 }
4064
4065 if (!wp || !ieee80211_usable_wmm_params(sdata, wp + 2,
4066 wp[1] - 2)) {
4067 assoc_data->wmm = false;
4068 ifmgd->flags |= IEEE80211_STA_DISABLE_WMM;
4069 }
4070 rcu_read_unlock();
4071 }
4072
4198 /* 4073 /*
4199 * IEEE802.11n does not allow TKIP/WEP as pairwise ciphers in HT mode. 4074 * 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 4075 * We still associate in non-HT mode (11a/b/g) if any one of these
@@ -4224,18 +4099,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 */ 4099 /* 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]; 4100 sband = local->hw.wiphy->bands[req->bss->channel->band];
4226 if (!sband->ht_cap.ht_supported || 4101 if (!sband->ht_cap.ht_supported ||
4227 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) { 4102 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4103 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4228 ifmgd->flags |= IEEE80211_STA_DISABLE_HT; 4104 ifmgd->flags |= IEEE80211_STA_DISABLE_HT;
4229 if (!bss->wmm_used) 4105 if (!bss->wmm_used &&
4106 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4230 netdev_info(sdata->dev, 4107 netdev_info(sdata->dev,
4231 "disabling HT as WMM/QoS is not supported by the AP\n"); 4108 "disabling HT as WMM/QoS is not supported by the AP\n");
4232 } 4109 }
4233 4110
4234 /* disable VHT if we don't support it or the AP doesn't use WMM */ 4111 /* disable VHT if we don't support it or the AP doesn't use WMM */
4235 if (!sband->vht_cap.vht_supported || 4112 if (!sband->vht_cap.vht_supported ||
4236 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used) { 4113 local->hw.queues < IEEE80211_NUM_ACS || !bss->wmm_used ||
4114 ifmgd->flags & IEEE80211_STA_DISABLE_WMM) {
4237 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT; 4115 ifmgd->flags |= IEEE80211_STA_DISABLE_VHT;
4238 if (!bss->wmm_used) 4116 if (!bss->wmm_used &&
4117 !(ifmgd->flags & IEEE80211_STA_DISABLE_WMM))
4239 netdev_info(sdata->dev, 4118 netdev_info(sdata->dev,
4240 "disabling VHT as WMM/QoS is not supported by the AP\n"); 4119 "disabling VHT as WMM/QoS is not supported by the AP\n");
4241 } 4120 }
@@ -4264,8 +4143,6 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4264 sdata->smps_mode = ifmgd->req_smps; 4143 sdata->smps_mode = ifmgd->req_smps;
4265 4144
4266 assoc_data->capability = req->bss->capability; 4145 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; 4146 assoc_data->supp_rates = bss->supp_rates;
4270 assoc_data->supp_rates_len = bss->supp_rates_len; 4147 assoc_data->supp_rates_len = bss->supp_rates_len;
4271 4148
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..5d60779a0c1b 100644
--- a/net/mac80211/rc80211_minstrel_ht.c
+++ b/net/mac80211/rc80211_minstrel_ht.c
@@ -365,6 +365,14 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
365 } 365 }
366 } 366 }
367 367
368#ifdef CONFIG_MAC80211_DEBUGFS
369 /* use fixed index if set */
370 if (mp->fixed_rate_idx != -1) {
371 mi->max_tp_rate = mp->fixed_rate_idx;
372 mi->max_tp_rate2 = mp->fixed_rate_idx;
373 mi->max_prob_rate = mp->fixed_rate_idx;
374 }
375#endif
368 376
369 mi->stats_update = jiffies; 377 mi->stats_update = jiffies;
370} 378}
@@ -774,6 +782,11 @@ minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
774 info->flags |= mi->tx_flags; 782 info->flags |= mi->tx_flags;
775 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble); 783 minstrel_ht_check_cck_shortpreamble(mp, mi, txrc->short_preamble);
776 784
785#ifdef CONFIG_MAC80211_DEBUGFS
786 if (mp->fixed_rate_idx != -1)
787 return;
788#endif
789
777 /* Don't use EAPOL frames for sampling on non-mrr hw */ 790 /* Don't use EAPOL frames for sampling on non-mrr hw */
778 if (mp->hw->max_rates == 1 && 791 if (mp->hw->max_rates == 1 &&
779 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)) 792 (info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO))
@@ -781,16 +794,6 @@ minstrel_ht_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
781 else 794 else
782 sample_idx = minstrel_get_sample_rate(mp, mi); 795 sample_idx = minstrel_get_sample_rate(mp, mi);
783 796
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++; 797 mi->total_packets++;
795 798
796 /* wraparound */ 799 /* 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..caecef870c0e 100644
--- a/net/mac80211/rx.c
+++ b/net/mac80211/rx.c
@@ -995,8 +995,9 @@ ieee80211_rx_h_check(struct ieee80211_rx_data *rx)
995 rx->sta->num_duplicates++; 995 rx->sta->num_duplicates++;
996 } 996 }
997 return RX_DROP_UNUSABLE; 997 return RX_DROP_UNUSABLE;
998 } else 998 } else if (!(status->flag & RX_FLAG_AMSDU_MORE)) {
999 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl; 999 rx->sta->last_seq_ctrl[rx->seqno_idx] = hdr->seq_ctrl;
1000 }
1000 } 1001 }
1001 1002
1002 if (unlikely(rx->skb->len < 16)) { 1003 if (unlikely(rx->skb->len < 16)) {
@@ -2402,7 +2403,8 @@ ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2402 return RX_DROP_UNUSABLE; 2403 return RX_DROP_UNUSABLE;
2403 2404
2404 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC && 2405 if (!rx->sta && mgmt->u.action.category != WLAN_CATEGORY_PUBLIC &&
2405 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED) 2406 mgmt->u.action.category != WLAN_CATEGORY_SELF_PROTECTED &&
2407 mgmt->u.action.category != WLAN_CATEGORY_SPECTRUM_MGMT)
2406 return RX_DROP_UNUSABLE; 2408 return RX_DROP_UNUSABLE;
2407 2409
2408 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH)) 2410 if (!(status->rx_flags & IEEE80211_RX_RA_MATCH))
@@ -2566,31 +2568,49 @@ ieee80211_rx_h_action(struct ieee80211_rx_data *rx)
2566 2568
2567 goto queue; 2569 goto queue;
2568 case WLAN_CATEGORY_SPECTRUM_MGMT: 2570 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 */ 2571 /* verify action_code is present */
2576 if (len < IEEE80211_MIN_ACTION_SIZE + 1) 2572 if (len < IEEE80211_MIN_ACTION_SIZE + 1)
2577 break; 2573 break;
2578 2574
2579 switch (mgmt->u.action.u.measurement.action_code) { 2575 switch (mgmt->u.action.u.measurement.action_code) {
2580 case WLAN_ACTION_SPCT_MSR_REQ: 2576 case WLAN_ACTION_SPCT_MSR_REQ:
2577 if (status->band != IEEE80211_BAND_5GHZ)
2578 break;
2579
2581 if (len < (IEEE80211_MIN_ACTION_SIZE + 2580 if (len < (IEEE80211_MIN_ACTION_SIZE +
2582 sizeof(mgmt->u.action.u.measurement))) 2581 sizeof(mgmt->u.action.u.measurement)))
2583 break; 2582 break;
2583
2584 if (sdata->vif.type != NL80211_IFTYPE_STATION)
2585 break;
2586
2584 ieee80211_process_measurement_req(sdata, mgmt, len); 2587 ieee80211_process_measurement_req(sdata, mgmt, len);
2585 goto handled; 2588 goto handled;
2586 case WLAN_ACTION_SPCT_CHL_SWITCH: 2589 case WLAN_ACTION_SPCT_CHL_SWITCH: {
2587 if (sdata->vif.type != NL80211_IFTYPE_STATION) 2590 u8 *bssid;
2591 if (len < (IEEE80211_MIN_ACTION_SIZE +
2592 sizeof(mgmt->u.action.u.chan_switch)))
2593 break;
2594
2595 if (sdata->vif.type != NL80211_IFTYPE_STATION &&
2596 sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2597 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2588 break; 2598 break;
2589 2599
2590 if (!ether_addr_equal(mgmt->bssid, sdata->u.mgd.bssid)) 2600 if (sdata->vif.type == NL80211_IFTYPE_STATION)
2601 bssid = sdata->u.mgd.bssid;
2602 else if (sdata->vif.type == NL80211_IFTYPE_ADHOC)
2603 bssid = sdata->u.ibss.bssid;
2604 else if (sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
2605 bssid = mgmt->sa;
2606 else
2607 break;
2608
2609 if (!ether_addr_equal(mgmt->bssid, bssid))
2591 break; 2610 break;
2592 2611
2593 goto queue; 2612 goto queue;
2613 }
2594 } 2614 }
2595 break; 2615 break;
2596 case WLAN_CATEGORY_SA_QUERY: 2616 case WLAN_CATEGORY_SA_QUERY:
diff --git a/net/mac80211/scan.c b/net/mac80211/scan.c
index d2d17a449224..5ad66a83ef7f 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;
diff --git a/net/mac80211/spectmgmt.c b/net/mac80211/spectmgmt.c
index 578eea3fc04d..a40da20b32e0 100644
--- a/net/mac80211/spectmgmt.c
+++ b/net/mac80211/spectmgmt.c
@@ -21,6 +21,175 @@
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 }
82
83 new_freq = ieee80211_channel_to_frequency(new_chan_no, new_band);
84 new_chan = ieee80211_get_channel(sdata->local->hw.wiphy, new_freq);
85 if (!new_chan || new_chan->flags & IEEE80211_CHAN_DISABLED) {
86 sdata_info(sdata,
87 "BSS %pM switches to unsupported channel (%d MHz), disconnecting\n",
88 bssid, new_freq);
89 return -EINVAL;
90 }
91
92 if (!beacon && sec_chan_offs) {
93 secondary_channel_offset = sec_chan_offs->sec_chan_offs;
94 } else if (beacon && ht_oper) {
95 secondary_channel_offset =
96 ht_oper->ht_param & IEEE80211_HT_PARAM_CHA_SEC_OFFSET;
97 } else if (!(sta_flags & IEEE80211_STA_DISABLE_HT)) {
98 /* If it's not a beacon, HT is enabled and the IE not present,
99 * it's 20 MHz, 802.11-2012 8.5.2.6:
100 * This element [the Secondary Channel Offset Element] is
101 * present when switching to a 40 MHz channel. It may be
102 * present when switching to a 20 MHz channel (in which
103 * case the secondary channel offset is set to SCN).
104 */
105 secondary_channel_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
106 }
107
108 switch (secondary_channel_offset) {
109 default:
110 /* secondary_channel_offset was present but is invalid */
111 case IEEE80211_HT_PARAM_CHA_SEC_NONE:
112 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
113 NL80211_CHAN_HT20);
114 break;
115 case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
116 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
117 NL80211_CHAN_HT40PLUS);
118 break;
119 case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
120 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
121 NL80211_CHAN_HT40MINUS);
122 break;
123 case -1:
124 cfg80211_chandef_create(&csa_ie->chandef, new_chan,
125 NL80211_CHAN_NO_HT);
126 /* keep width for 5/10 MHz channels */
127 switch (sdata->vif.bss_conf.chandef.width) {
128 case NL80211_CHAN_WIDTH_5:
129 case NL80211_CHAN_WIDTH_10:
130 csa_ie->chandef.width =
131 sdata->vif.bss_conf.chandef.width;
132 break;
133 default:
134 break;
135 }
136 break;
137 }
138
139 if (wide_bw_chansw_ie) {
140 new_vht_chandef.chan = new_chan;
141 new_vht_chandef.center_freq1 =
142 ieee80211_channel_to_frequency(
143 wide_bw_chansw_ie->new_center_freq_seg0,
144 new_band);
145
146 switch (wide_bw_chansw_ie->new_channel_width) {
147 default:
148 /* hmmm, ignore VHT and use HT if present */
149 case IEEE80211_VHT_CHANWIDTH_USE_HT:
150 new_vht_chandef.chan = NULL;
151 break;
152 case IEEE80211_VHT_CHANWIDTH_80MHZ:
153 new_vht_chandef.width = NL80211_CHAN_WIDTH_80;
154 break;
155 case IEEE80211_VHT_CHANWIDTH_160MHZ:
156 new_vht_chandef.width = NL80211_CHAN_WIDTH_160;
157 break;
158 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
159 /* field is otherwise reserved */
160 new_vht_chandef.center_freq2 =
161 ieee80211_channel_to_frequency(
162 wide_bw_chansw_ie->new_center_freq_seg1,
163 new_band);
164 new_vht_chandef.width = NL80211_CHAN_WIDTH_80P80;
165 break;
166 }
167 if (sta_flags & IEEE80211_STA_DISABLE_80P80MHZ &&
168 new_vht_chandef.width == NL80211_CHAN_WIDTH_80P80)
169 ieee80211_chandef_downgrade(&new_vht_chandef);
170 if (sta_flags & IEEE80211_STA_DISABLE_160MHZ &&
171 new_vht_chandef.width == NL80211_CHAN_WIDTH_160)
172 ieee80211_chandef_downgrade(&new_vht_chandef);
173 if (sta_flags & IEEE80211_STA_DISABLE_40MHZ &&
174 new_vht_chandef.width > NL80211_CHAN_WIDTH_20)
175 ieee80211_chandef_downgrade(&new_vht_chandef);
176 }
177
178 /* if VHT data is there validate & use it */
179 if (new_vht_chandef.chan) {
180 if (!cfg80211_chandef_compatible(&new_vht_chandef,
181 &csa_ie->chandef)) {
182 sdata_info(sdata,
183 "BSS %pM: CSA has inconsistent channel data, disconnecting\n",
184 bssid);
185 return -EINVAL;
186 }
187 csa_ie->chandef = new_vht_chandef;
188 }
189
190 return 0;
191}
192
24static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata, 193static void ieee80211_send_refuse_measurement_request(struct ieee80211_sub_if_data *sdata,
25 struct ieee80211_msrment_ie *request_ie, 194 struct ieee80211_msrment_ie *request_ie,
26 const u8 *da, const u8 *bssid, 195 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..c558b246ef00 100644
--- a/net/mac80211/tx.c
+++ b/net/mac80211/tx.c
@@ -1367,6 +1367,35 @@ static int invoke_tx_handlers(struct ieee80211_tx_data *tx)
1367 return 0; 1367 return 0;
1368} 1368}
1369 1369
1370bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1371 struct ieee80211_vif *vif, struct sk_buff *skb,
1372 int band, struct ieee80211_sta **sta)
1373{
1374 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1375 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1376 struct ieee80211_tx_data tx;
1377
1378 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP)
1379 return false;
1380
1381 info->band = band;
1382 info->control.vif = vif;
1383 info->hw_queue = vif->hw_queue[skb_get_queue_mapping(skb)];
1384
1385 if (invoke_tx_handlers(&tx))
1386 return false;
1387
1388 if (sta) {
1389 if (tx.sta)
1390 *sta = &tx.sta->sta;
1391 else
1392 *sta = NULL;
1393 }
1394
1395 return true;
1396}
1397EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1398
1370/* 1399/*
1371 * Returns false if the frame couldn't be transmitted but was queued instead. 1400 * Returns false if the frame couldn't be transmitted but was queued instead.
1372 */ 1401 */
@@ -1982,7 +2011,7 @@ netdev_tx_t ieee80211_subif_start_xmit(struct sk_buff *skb,
1982 * EAPOL frames from the local station. 2011 * EAPOL frames from the local station.
1983 */ 2012 */
1984 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) && 2013 if (unlikely(!ieee80211_vif_is_mesh(&sdata->vif) &&
1985 !is_multicast_ether_addr(hdr.addr1) && !authorized && 2014 !multicast && !authorized &&
1986 (cpu_to_be16(ethertype) != sdata->control_port_protocol || 2015 (cpu_to_be16(ethertype) != sdata->control_port_protocol ||
1987 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) { 2016 !ether_addr_equal(sdata->vif.addr, skb->data + ETH_ALEN)))) {
1988#ifdef CONFIG_MAC80211_VERBOSE_DEBUG 2017#ifdef CONFIG_MAC80211_VERBOSE_DEBUG
@@ -2358,15 +2387,35 @@ static void ieee80211_update_csa(struct ieee80211_sub_if_data *sdata,
2358 struct probe_resp *resp; 2387 struct probe_resp *resp;
2359 int counter_offset_beacon = sdata->csa_counter_offset_beacon; 2388 int counter_offset_beacon = sdata->csa_counter_offset_beacon;
2360 int counter_offset_presp = sdata->csa_counter_offset_presp; 2389 int counter_offset_presp = sdata->csa_counter_offset_presp;
2390 u8 *beacon_data;
2391 size_t beacon_data_len;
2392
2393 switch (sdata->vif.type) {
2394 case NL80211_IFTYPE_AP:
2395 beacon_data = beacon->tail;
2396 beacon_data_len = beacon->tail_len;
2397 break;
2398 case NL80211_IFTYPE_ADHOC:
2399 beacon_data = beacon->head;
2400 beacon_data_len = beacon->head_len;
2401 break;
2402 case NL80211_IFTYPE_MESH_POINT:
2403 beacon_data = beacon->head;
2404 beacon_data_len = beacon->head_len;
2405 break;
2406 default:
2407 return;
2408 }
2409 if (WARN_ON(counter_offset_beacon >= beacon_data_len))
2410 return;
2361 2411
2362 /* warn if the driver did not check for/react to csa completeness */ 2412 /* warn if the driver did not check for/react to csa completeness */
2363 if (WARN_ON(((u8 *)beacon->tail)[counter_offset_beacon] == 0)) 2413 if (WARN_ON(beacon_data[counter_offset_beacon] == 0))
2364 return; 2414 return;
2365 2415
2366 ((u8 *)beacon->tail)[counter_offset_beacon]--; 2416 beacon_data[counter_offset_beacon]--;
2367 2417
2368 if (sdata->vif.type == NL80211_IFTYPE_AP && 2418 if (sdata->vif.type == NL80211_IFTYPE_AP && counter_offset_presp) {
2369 counter_offset_presp) {
2370 rcu_read_lock(); 2419 rcu_read_lock();
2371 resp = rcu_dereference(sdata->u.ap.probe_resp); 2420 resp = rcu_dereference(sdata->u.ap.probe_resp);
2372 2421
@@ -2401,6 +2450,24 @@ bool ieee80211_csa_is_complete(struct ieee80211_vif *vif)
2401 goto out; 2450 goto out;
2402 beacon_data = beacon->tail; 2451 beacon_data = beacon->tail;
2403 beacon_data_len = beacon->tail_len; 2452 beacon_data_len = beacon->tail_len;
2453 } else if (vif->type == NL80211_IFTYPE_ADHOC) {
2454 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2455
2456 beacon = rcu_dereference(ifibss->presp);
2457 if (!beacon)
2458 goto out;
2459
2460 beacon_data = beacon->head;
2461 beacon_data_len = beacon->head_len;
2462 } else if (vif->type == NL80211_IFTYPE_MESH_POINT) {
2463 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2464
2465 beacon = rcu_dereference(ifmsh->beacon);
2466 if (!beacon)
2467 goto out;
2468
2469 beacon_data = beacon->head;
2470 beacon_data_len = beacon->head_len;
2404 } else { 2471 } else {
2405 WARN_ON(1); 2472 WARN_ON(1);
2406 goto out; 2473 goto out;
@@ -2485,6 +2552,10 @@ struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2485 if (!presp) 2552 if (!presp)
2486 goto out; 2553 goto out;
2487 2554
2555 if (sdata->vif.csa_active)
2556 ieee80211_update_csa(sdata, presp);
2557
2558
2488 skb = dev_alloc_skb(local->tx_headroom + presp->head_len); 2559 skb = dev_alloc_skb(local->tx_headroom + presp->head_len);
2489 if (!skb) 2560 if (!skb)
2490 goto out; 2561 goto out;
@@ -2502,6 +2573,9 @@ struct sk_buff *ieee80211_beacon_get_tim(struct ieee80211_hw *hw,
2502 if (!bcn) 2573 if (!bcn)
2503 goto out; 2574 goto out;
2504 2575
2576 if (sdata->vif.csa_active)
2577 ieee80211_update_csa(sdata, bcn);
2578
2505 if (ifmsh->sync_ops) 2579 if (ifmsh->sync_ops)
2506 ifmsh->sync_ops->adjust_tbtt( 2580 ifmsh->sync_ops->adjust_tbtt(
2507 sdata); 2581 sdata);
diff --git a/net/mac80211/util.c b/net/mac80211/util.c
index 69e4ef5348a0..592a18171f95 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}
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);
606 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; 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;
1017 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
@@ -2296,3 +2300,175 @@ void ieee80211_radar_detected(struct ieee80211_hw *hw)
2296 ieee80211_queue_work(hw, &local->radar_detected_work); 2300 ieee80211_queue_work(hw, &local->radar_detected_work);
2297} 2301}
2298EXPORT_SYMBOL(ieee80211_radar_detected); 2302EXPORT_SYMBOL(ieee80211_radar_detected);
2303
2304u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c)
2305{
2306 u32 ret;
2307 int tmp;
2308
2309 switch (c->width) {
2310 case NL80211_CHAN_WIDTH_20:
2311 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2312 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2313 break;
2314 case NL80211_CHAN_WIDTH_40:
2315 c->width = NL80211_CHAN_WIDTH_20;
2316 c->center_freq1 = c->chan->center_freq;
2317 ret = IEEE80211_STA_DISABLE_40MHZ |
2318 IEEE80211_STA_DISABLE_VHT;
2319 break;
2320 case NL80211_CHAN_WIDTH_80:
2321 tmp = (30 + c->chan->center_freq - c->center_freq1)/20;
2322 /* n_P40 */
2323 tmp /= 2;
2324 /* freq_P40 */
2325 c->center_freq1 = c->center_freq1 - 20 + 40 * tmp;
2326 c->width = NL80211_CHAN_WIDTH_40;
2327 ret = IEEE80211_STA_DISABLE_VHT;
2328 break;
2329 case NL80211_CHAN_WIDTH_80P80:
2330 c->center_freq2 = 0;
2331 c->width = NL80211_CHAN_WIDTH_80;
2332 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2333 IEEE80211_STA_DISABLE_160MHZ;
2334 break;
2335 case NL80211_CHAN_WIDTH_160:
2336 /* n_P20 */
2337 tmp = (70 + c->chan->center_freq - c->center_freq1)/20;
2338 /* n_P80 */
2339 tmp /= 4;
2340 c->center_freq1 = c->center_freq1 - 40 + 80 * tmp;
2341 c->width = NL80211_CHAN_WIDTH_80;
2342 ret = IEEE80211_STA_DISABLE_80P80MHZ |
2343 IEEE80211_STA_DISABLE_160MHZ;
2344 break;
2345 default:
2346 case NL80211_CHAN_WIDTH_20_NOHT:
2347 WARN_ON_ONCE(1);
2348 c->width = NL80211_CHAN_WIDTH_20_NOHT;
2349 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2350 break;
2351 case NL80211_CHAN_WIDTH_5:
2352 case NL80211_CHAN_WIDTH_10:
2353 WARN_ON_ONCE(1);
2354 /* keep c->width */
2355 ret = IEEE80211_STA_DISABLE_HT | IEEE80211_STA_DISABLE_VHT;
2356 break;
2357 }
2358
2359 WARN_ON_ONCE(!cfg80211_chandef_valid(c));
2360
2361 return ret;
2362}
2363
2364/*
2365 * Returns true if smps_mode_new is strictly more restrictive than
2366 * smps_mode_old.
2367 */
2368bool ieee80211_smps_is_restrictive(enum ieee80211_smps_mode smps_mode_old,
2369 enum ieee80211_smps_mode smps_mode_new)
2370{
2371 if (WARN_ON_ONCE(smps_mode_old == IEEE80211_SMPS_AUTOMATIC ||
2372 smps_mode_new == IEEE80211_SMPS_AUTOMATIC))
2373 return false;
2374
2375 switch (smps_mode_old) {
2376 case IEEE80211_SMPS_STATIC:
2377 return false;
2378 case IEEE80211_SMPS_DYNAMIC:
2379 return smps_mode_new == IEEE80211_SMPS_STATIC;
2380 case IEEE80211_SMPS_OFF:
2381 return smps_mode_new != IEEE80211_SMPS_OFF;
2382 default:
2383 WARN_ON(1);
2384 }
2385
2386 return false;
2387}
2388
2389int ieee80211_send_action_csa(struct ieee80211_sub_if_data *sdata,
2390 struct cfg80211_csa_settings *csa_settings)
2391{
2392 struct sk_buff *skb;
2393 struct ieee80211_mgmt *mgmt;
2394 struct ieee80211_local *local = sdata->local;
2395 int freq;
2396 int hdr_len = offsetof(struct ieee80211_mgmt, u.action.u.chan_switch) +
2397 sizeof(mgmt->u.action.u.chan_switch);
2398 u8 *pos;
2399
2400 if (sdata->vif.type != NL80211_IFTYPE_ADHOC &&
2401 sdata->vif.type != NL80211_IFTYPE_MESH_POINT)
2402 return -EOPNOTSUPP;
2403
2404 skb = dev_alloc_skb(local->tx_headroom + hdr_len +
2405 5 + /* channel switch announcement element */
2406 3 + /* secondary channel offset element */
2407 8); /* mesh channel switch parameters element */
2408 if (!skb)
2409 return -ENOMEM;
2410
2411 skb_reserve(skb, local->tx_headroom);
2412 mgmt = (struct ieee80211_mgmt *)skb_put(skb, hdr_len);
2413 memset(mgmt, 0, hdr_len);
2414 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT |
2415 IEEE80211_STYPE_ACTION);
2416
2417 eth_broadcast_addr(mgmt->da);
2418 memcpy(mgmt->sa, sdata->vif.addr, ETH_ALEN);
2419 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2420 memcpy(mgmt->bssid, sdata->vif.addr, ETH_ALEN);
2421 } else {
2422 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
2423 memcpy(mgmt->bssid, ifibss->bssid, ETH_ALEN);
2424 }
2425 mgmt->u.action.category = WLAN_CATEGORY_SPECTRUM_MGMT;
2426 mgmt->u.action.u.chan_switch.action_code = WLAN_ACTION_SPCT_CHL_SWITCH;
2427 pos = skb_put(skb, 5);
2428 *pos++ = WLAN_EID_CHANNEL_SWITCH; /* EID */
2429 *pos++ = 3; /* IE length */
2430 *pos++ = csa_settings->block_tx ? 1 : 0; /* CSA mode */
2431 freq = csa_settings->chandef.chan->center_freq;
2432 *pos++ = ieee80211_frequency_to_channel(freq); /* channel */
2433 *pos++ = csa_settings->count; /* count */
2434
2435 if (csa_settings->chandef.width == NL80211_CHAN_WIDTH_40) {
2436 enum nl80211_channel_type ch_type;
2437
2438 skb_put(skb, 3);
2439 *pos++ = WLAN_EID_SECONDARY_CHANNEL_OFFSET; /* EID */
2440 *pos++ = 1; /* IE length */
2441 ch_type = cfg80211_get_chandef_type(&csa_settings->chandef);
2442 if (ch_type == NL80211_CHAN_HT40PLUS)
2443 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2444 else
2445 *pos++ = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2446 }
2447
2448 if (ieee80211_vif_is_mesh(&sdata->vif)) {
2449 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
2450 __le16 pre_value;
2451
2452 skb_put(skb, 8);
2453 *pos++ = WLAN_EID_CHAN_SWITCH_PARAM; /* EID */
2454 *pos++ = 6; /* IE length */
2455 *pos++ = sdata->u.mesh.mshcfg.dot11MeshTTL; /* Mesh TTL */
2456 *pos = 0x00; /* Mesh Flag: Tx Restrict, Initiator, Reason */
2457 *pos |= WLAN_EID_CHAN_SWITCH_PARAM_INITIATOR;
2458 *pos++ |= csa_settings->block_tx ?
2459 WLAN_EID_CHAN_SWITCH_PARAM_TX_RESTRICT : 0x00;
2460 put_unaligned_le16(WLAN_REASON_MESH_CHAN, pos); /* Reason Cd */
2461 pos += 2;
2462 if (!ifmsh->pre_value)
2463 ifmsh->pre_value = 1;
2464 else
2465 ifmsh->pre_value++;
2466 pre_value = cpu_to_le16(ifmsh->pre_value);
2467 memcpy(pos, &pre_value, 2); /* Precedence Value */
2468 pos += 2;
2469 ifmsh->chsw_init = true;
2470 }
2471
2472 ieee80211_tx_skb(sdata, skb);
2473 return 0;
2474}
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..48acec17e27a 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
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..2bc2dec20b00
--- /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 ip2->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..62786a495cea 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/*
@@ -3820,10 +3820,6 @@ void __net_exit ip_vs_control_net_cleanup(struct net *net)
3820{ 3820{
3821 struct netns_ipvs *ipvs = net_ipvs(net); 3821 struct netns_ipvs *ipvs = net_ipvs(net);
3822 3822
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); 3823 ip_vs_trash_cleanup(net);
3828 ip_vs_stop_estimator(net, &ipvs->tot_stats); 3824 ip_vs_stop_estimator(net, &ipvs->tot_stats);
3829 ip_vs_control_net_cleanup_sysctl(net); 3825 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_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/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..e22d950c60b3 100644
--- a/net/netfilter/nf_conntrack_core.c
+++ b/net/netfilter/nf_conntrack_core.c
@@ -1109,12 +1109,14 @@ void __nf_ct_refresh_acct(struct nf_conn *ct,
1109 1109
1110acct: 1110acct:
1111 if (do_acct) { 1111 if (do_acct) {
1112 struct nf_conn_counter *acct; 1112 struct nf_conn_acct *acct;
1113 1113
1114 acct = nf_conn_acct_find(ct); 1114 acct = nf_conn_acct_find(ct);
1115 if (acct) { 1115 if (acct) {
1116 atomic64_inc(&acct[CTINFO2DIR(ctinfo)].packets); 1116 struct nf_conn_counter *counter = acct->counter;
1117 atomic64_add(skb->len, &acct[CTINFO2DIR(ctinfo)].bytes); 1117
1118 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1119 atomic64_add(skb->len, &counter[CTINFO2DIR(ctinfo)].bytes);
1118 } 1120 }
1119 } 1121 }
1120} 1122}
@@ -1126,13 +1128,15 @@ bool __nf_ct_kill_acct(struct nf_conn *ct,
1126 int do_acct) 1128 int do_acct)
1127{ 1129{
1128 if (do_acct) { 1130 if (do_acct) {
1129 struct nf_conn_counter *acct; 1131 struct nf_conn_acct *acct;
1130 1132
1131 acct = nf_conn_acct_find(ct); 1133 acct = nf_conn_acct_find(ct);
1132 if (acct) { 1134 if (acct) {
1133 atomic64_inc(&acct[CTINFO2DIR(ctinfo)].packets); 1135 struct nf_conn_counter *counter = acct->counter;
1136
1137 atomic64_inc(&counter[CTINFO2DIR(ctinfo)].packets);
1134 atomic64_add(skb->len - skb_network_offset(skb), 1138 atomic64_add(skb->len - skb_network_offset(skb),
1135 &acct[CTINFO2DIR(ctinfo)].bytes); 1139 &counter[CTINFO2DIR(ctinfo)].bytes);
1136 } 1140 }
1137 } 1141 }
1138 1142
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_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_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_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_tables_api.c b/net/netfilter/nf_tables_api.c
new file mode 100644
index 000000000000..dcddc49c0e08
--- /dev/null
+++ b/net/netfilter/nf_tables_api.c
@@ -0,0 +1,3275 @@
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 err = nf_register_hook(&nft_base_chain(chain)->ops);
316 if (err < 0)
317 goto err;
318
319 i++;
320 }
321 return 0;
322err:
323 list_for_each_entry(chain, &table->chains, list) {
324 if (i-- <= 0)
325 break;
326
327 nf_unregister_hook(&nft_base_chain(chain)->ops);
328 }
329 return err;
330}
331
332static int nf_tables_table_disable(struct nft_table *table)
333{
334 struct nft_chain *chain;
335
336 list_for_each_entry(chain, &table->chains, list)
337 nf_unregister_hook(&nft_base_chain(chain)->ops);
338
339 return 0;
340}
341
342static int nf_tables_updtable(struct sock *nlsk, struct sk_buff *skb,
343 const struct nlmsghdr *nlh,
344 const struct nlattr * const nla[],
345 struct nft_af_info *afi, struct nft_table *table)
346{
347 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
348 int family = nfmsg->nfgen_family, ret = 0;
349
350 if (nla[NFTA_TABLE_FLAGS]) {
351 __be32 flags;
352
353 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
354 if (flags & ~NFT_TABLE_F_DORMANT)
355 return -EINVAL;
356
357 if ((flags & NFT_TABLE_F_DORMANT) &&
358 !(table->flags & NFT_TABLE_F_DORMANT)) {
359 ret = nf_tables_table_disable(table);
360 if (ret >= 0)
361 table->flags |= NFT_TABLE_F_DORMANT;
362 } else if (!(flags & NFT_TABLE_F_DORMANT) &&
363 table->flags & NFT_TABLE_F_DORMANT) {
364 ret = nf_tables_table_enable(table);
365 if (ret >= 0)
366 table->flags &= ~NFT_TABLE_F_DORMANT;
367 }
368 if (ret < 0)
369 goto err;
370 }
371
372 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
373err:
374 return ret;
375}
376
377static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
378 const struct nlmsghdr *nlh,
379 const struct nlattr * const nla[])
380{
381 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
382 const struct nlattr *name;
383 struct nft_af_info *afi;
384 struct nft_table *table;
385 struct net *net = sock_net(skb->sk);
386 int family = nfmsg->nfgen_family;
387
388 afi = nf_tables_afinfo_lookup(net, family, true);
389 if (IS_ERR(afi))
390 return PTR_ERR(afi);
391
392 name = nla[NFTA_TABLE_NAME];
393 table = nf_tables_table_lookup(afi, name);
394 if (IS_ERR(table)) {
395 if (PTR_ERR(table) != -ENOENT)
396 return PTR_ERR(table);
397 table = NULL;
398 }
399
400 if (table != NULL) {
401 if (nlh->nlmsg_flags & NLM_F_EXCL)
402 return -EEXIST;
403 if (nlh->nlmsg_flags & NLM_F_REPLACE)
404 return -EOPNOTSUPP;
405 return nf_tables_updtable(nlsk, skb, nlh, nla, afi, table);
406 }
407
408 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL);
409 if (table == NULL)
410 return -ENOMEM;
411
412 nla_strlcpy(table->name, name, nla_len(name));
413 INIT_LIST_HEAD(&table->chains);
414 INIT_LIST_HEAD(&table->sets);
415
416 if (nla[NFTA_TABLE_FLAGS]) {
417 __be32 flags;
418
419 flags = ntohl(nla_get_be32(nla[NFTA_TABLE_FLAGS]));
420 if (flags & ~NFT_TABLE_F_DORMANT) {
421 kfree(table);
422 return -EINVAL;
423 }
424
425 table->flags |= flags;
426 }
427
428 list_add_tail(&table->list, &afi->tables);
429 nf_tables_table_notify(skb, nlh, table, NFT_MSG_NEWTABLE, family);
430 return 0;
431}
432
433static int nf_tables_deltable(struct sock *nlsk, struct sk_buff *skb,
434 const struct nlmsghdr *nlh,
435 const struct nlattr * const nla[])
436{
437 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
438 struct nft_af_info *afi;
439 struct nft_table *table;
440 struct net *net = sock_net(skb->sk);
441 int family = nfmsg->nfgen_family;
442
443 afi = nf_tables_afinfo_lookup(net, family, false);
444 if (IS_ERR(afi))
445 return PTR_ERR(afi);
446
447 table = nf_tables_table_lookup(afi, nla[NFTA_TABLE_NAME]);
448 if (IS_ERR(table))
449 return PTR_ERR(table);
450
451 if (table->use)
452 return -EBUSY;
453
454 list_del(&table->list);
455 nf_tables_table_notify(skb, nlh, table, NFT_MSG_DELTABLE, family);
456 kfree(table);
457 return 0;
458}
459
460int nft_register_chain_type(struct nf_chain_type *ctype)
461{
462 int err = 0;
463
464 nfnl_lock(NFNL_SUBSYS_NFTABLES);
465 if (chain_type[ctype->family][ctype->type] != NULL) {
466 err = -EBUSY;
467 goto out;
468 }
469
470 if (!try_module_get(ctype->me))
471 goto out;
472
473 chain_type[ctype->family][ctype->type] = ctype;
474out:
475 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
476 return err;
477}
478EXPORT_SYMBOL_GPL(nft_register_chain_type);
479
480void nft_unregister_chain_type(struct nf_chain_type *ctype)
481{
482 nfnl_lock(NFNL_SUBSYS_NFTABLES);
483 chain_type[ctype->family][ctype->type] = NULL;
484 module_put(ctype->me);
485 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
486}
487EXPORT_SYMBOL_GPL(nft_unregister_chain_type);
488
489/*
490 * Chains
491 */
492
493static struct nft_chain *
494nf_tables_chain_lookup_byhandle(const struct nft_table *table, u64 handle)
495{
496 struct nft_chain *chain;
497
498 list_for_each_entry(chain, &table->chains, list) {
499 if (chain->handle == handle)
500 return chain;
501 }
502
503 return ERR_PTR(-ENOENT);
504}
505
506static struct nft_chain *nf_tables_chain_lookup(const struct nft_table *table,
507 const struct nlattr *nla)
508{
509 struct nft_chain *chain;
510
511 if (nla == NULL)
512 return ERR_PTR(-EINVAL);
513
514 list_for_each_entry(chain, &table->chains, list) {
515 if (!nla_strcmp(nla, chain->name))
516 return chain;
517 }
518
519 return ERR_PTR(-ENOENT);
520}
521
522static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = {
523 [NFTA_CHAIN_TABLE] = { .type = NLA_STRING },
524 [NFTA_CHAIN_HANDLE] = { .type = NLA_U64 },
525 [NFTA_CHAIN_NAME] = { .type = NLA_STRING,
526 .len = NFT_CHAIN_MAXNAMELEN - 1 },
527 [NFTA_CHAIN_HOOK] = { .type = NLA_NESTED },
528 [NFTA_CHAIN_POLICY] = { .type = NLA_U32 },
529 [NFTA_CHAIN_TYPE] = { .type = NLA_NUL_STRING },
530 [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED },
531};
532
533static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX + 1] = {
534 [NFTA_HOOK_HOOKNUM] = { .type = NLA_U32 },
535 [NFTA_HOOK_PRIORITY] = { .type = NLA_U32 },
536};
537
538static int nft_dump_stats(struct sk_buff *skb, struct nft_stats __percpu *stats)
539{
540 struct nft_stats *cpu_stats, total;
541 struct nlattr *nest;
542 int cpu;
543
544 memset(&total, 0, sizeof(total));
545 for_each_possible_cpu(cpu) {
546 cpu_stats = per_cpu_ptr(stats, cpu);
547 total.pkts += cpu_stats->pkts;
548 total.bytes += cpu_stats->bytes;
549 }
550 nest = nla_nest_start(skb, NFTA_CHAIN_COUNTERS);
551 if (nest == NULL)
552 goto nla_put_failure;
553
554 if (nla_put_be64(skb, NFTA_COUNTER_PACKETS, cpu_to_be64(total.pkts)) ||
555 nla_put_be64(skb, NFTA_COUNTER_BYTES, cpu_to_be64(total.bytes)))
556 goto nla_put_failure;
557
558 nla_nest_end(skb, nest);
559 return 0;
560
561nla_put_failure:
562 return -ENOSPC;
563}
564
565static int nf_tables_fill_chain_info(struct sk_buff *skb, u32 portid, u32 seq,
566 int event, u32 flags, int family,
567 const struct nft_table *table,
568 const struct nft_chain *chain)
569{
570 struct nlmsghdr *nlh;
571 struct nfgenmsg *nfmsg;
572
573 event |= NFNL_SUBSYS_NFTABLES << 8;
574 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg), flags);
575 if (nlh == NULL)
576 goto nla_put_failure;
577
578 nfmsg = nlmsg_data(nlh);
579 nfmsg->nfgen_family = family;
580 nfmsg->version = NFNETLINK_V0;
581 nfmsg->res_id = 0;
582
583 if (nla_put_string(skb, NFTA_CHAIN_TABLE, table->name))
584 goto nla_put_failure;
585 if (nla_put_be64(skb, NFTA_CHAIN_HANDLE, cpu_to_be64(chain->handle)))
586 goto nla_put_failure;
587 if (nla_put_string(skb, NFTA_CHAIN_NAME, chain->name))
588 goto nla_put_failure;
589
590 if (chain->flags & NFT_BASE_CHAIN) {
591 const struct nft_base_chain *basechain = nft_base_chain(chain);
592 const struct nf_hook_ops *ops = &basechain->ops;
593 struct nlattr *nest;
594
595 nest = nla_nest_start(skb, NFTA_CHAIN_HOOK);
596 if (nest == NULL)
597 goto nla_put_failure;
598 if (nla_put_be32(skb, NFTA_HOOK_HOOKNUM, htonl(ops->hooknum)))
599 goto nla_put_failure;
600 if (nla_put_be32(skb, NFTA_HOOK_PRIORITY, htonl(ops->priority)))
601 goto nla_put_failure;
602 nla_nest_end(skb, nest);
603
604 if (nla_put_be32(skb, NFTA_CHAIN_POLICY,
605 htonl(basechain->policy)))
606 goto nla_put_failure;
607
608 if (nla_put_string(skb, NFTA_CHAIN_TYPE,
609 chain_type[ops->pf][nft_base_chain(chain)->type]->name))
610 goto nla_put_failure;
611
612 if (nft_dump_stats(skb, nft_base_chain(chain)->stats))
613 goto nla_put_failure;
614 }
615
616 if (nla_put_be32(skb, NFTA_CHAIN_USE, htonl(chain->use)))
617 goto nla_put_failure;
618
619 return nlmsg_end(skb, nlh);
620
621nla_put_failure:
622 nlmsg_trim(skb, nlh);
623 return -1;
624}
625
626static int nf_tables_chain_notify(const struct sk_buff *oskb,
627 const struct nlmsghdr *nlh,
628 const struct nft_table *table,
629 const struct nft_chain *chain,
630 int event, int family)
631{
632 struct sk_buff *skb;
633 u32 portid = oskb ? NETLINK_CB(oskb).portid : 0;
634 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
635 u32 seq = nlh ? nlh->nlmsg_seq : 0;
636 bool report;
637 int err;
638
639 report = nlh ? nlmsg_report(nlh) : false;
640 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
641 return 0;
642
643 err = -ENOBUFS;
644 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
645 if (skb == NULL)
646 goto err;
647
648 err = nf_tables_fill_chain_info(skb, portid, seq, event, 0, family,
649 table, chain);
650 if (err < 0) {
651 kfree_skb(skb);
652 goto err;
653 }
654
655 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
656 GFP_KERNEL);
657err:
658 if (err < 0)
659 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
660 return err;
661}
662
663static int nf_tables_dump_chains(struct sk_buff *skb,
664 struct netlink_callback *cb)
665{
666 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
667 const struct nft_af_info *afi;
668 const struct nft_table *table;
669 const struct nft_chain *chain;
670 unsigned int idx = 0, s_idx = cb->args[0];
671 struct net *net = sock_net(skb->sk);
672 int family = nfmsg->nfgen_family;
673
674 list_for_each_entry(afi, &net->nft.af_info, list) {
675 if (family != NFPROTO_UNSPEC && family != afi->family)
676 continue;
677
678 list_for_each_entry(table, &afi->tables, list) {
679 list_for_each_entry(chain, &table->chains, list) {
680 if (idx < s_idx)
681 goto cont;
682 if (idx > s_idx)
683 memset(&cb->args[1], 0,
684 sizeof(cb->args) - sizeof(cb->args[0]));
685 if (nf_tables_fill_chain_info(skb, NETLINK_CB(cb->skb).portid,
686 cb->nlh->nlmsg_seq,
687 NFT_MSG_NEWCHAIN,
688 NLM_F_MULTI,
689 afi->family, table, chain) < 0)
690 goto done;
691cont:
692 idx++;
693 }
694 }
695 }
696done:
697 cb->args[0] = idx;
698 return skb->len;
699}
700
701
702static int nf_tables_getchain(struct sock *nlsk, struct sk_buff *skb,
703 const struct nlmsghdr *nlh,
704 const struct nlattr * const nla[])
705{
706 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
707 const struct nft_af_info *afi;
708 const struct nft_table *table;
709 const struct nft_chain *chain;
710 struct sk_buff *skb2;
711 struct net *net = sock_net(skb->sk);
712 int family = nfmsg->nfgen_family;
713 int err;
714
715 if (nlh->nlmsg_flags & NLM_F_DUMP) {
716 struct netlink_dump_control c = {
717 .dump = nf_tables_dump_chains,
718 };
719 return netlink_dump_start(nlsk, skb, nlh, &c);
720 }
721
722 afi = nf_tables_afinfo_lookup(net, family, false);
723 if (IS_ERR(afi))
724 return PTR_ERR(afi);
725
726 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
727 if (IS_ERR(table))
728 return PTR_ERR(table);
729
730 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
731 if (IS_ERR(chain))
732 return PTR_ERR(chain);
733
734 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
735 if (!skb2)
736 return -ENOMEM;
737
738 err = nf_tables_fill_chain_info(skb2, NETLINK_CB(skb).portid,
739 nlh->nlmsg_seq, NFT_MSG_NEWCHAIN, 0,
740 family, table, chain);
741 if (err < 0)
742 goto err;
743
744 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
745
746err:
747 kfree_skb(skb2);
748 return err;
749}
750
751static int
752nf_tables_chain_policy(struct nft_base_chain *chain, const struct nlattr *attr)
753{
754 switch (ntohl(nla_get_be32(attr))) {
755 case NF_DROP:
756 chain->policy = NF_DROP;
757 break;
758 case NF_ACCEPT:
759 chain->policy = NF_ACCEPT;
760 break;
761 default:
762 return -EINVAL;
763 }
764 return 0;
765}
766
767static const struct nla_policy nft_counter_policy[NFTA_COUNTER_MAX + 1] = {
768 [NFTA_COUNTER_PACKETS] = { .type = NLA_U64 },
769 [NFTA_COUNTER_BYTES] = { .type = NLA_U64 },
770};
771
772static int
773nf_tables_counters(struct nft_base_chain *chain, const struct nlattr *attr)
774{
775 struct nlattr *tb[NFTA_COUNTER_MAX+1];
776 struct nft_stats __percpu *newstats;
777 struct nft_stats *stats;
778 int err;
779
780 err = nla_parse_nested(tb, NFTA_COUNTER_MAX, attr, nft_counter_policy);
781 if (err < 0)
782 return err;
783
784 if (!tb[NFTA_COUNTER_BYTES] || !tb[NFTA_COUNTER_PACKETS])
785 return -EINVAL;
786
787 newstats = alloc_percpu(struct nft_stats);
788 if (newstats == NULL)
789 return -ENOMEM;
790
791 /* Restore old counters on this cpu, no problem. Per-cpu statistics
792 * are not exposed to userspace.
793 */
794 stats = this_cpu_ptr(newstats);
795 stats->bytes = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_BYTES]));
796 stats->pkts = be64_to_cpu(nla_get_be64(tb[NFTA_COUNTER_PACKETS]));
797
798 if (chain->stats) {
799 /* nfnl_lock is held, add some nfnl function for this, later */
800 struct nft_stats __percpu *oldstats =
801 rcu_dereference_protected(chain->stats, 1);
802
803 rcu_assign_pointer(chain->stats, newstats);
804 synchronize_rcu();
805 free_percpu(oldstats);
806 } else
807 rcu_assign_pointer(chain->stats, newstats);
808
809 return 0;
810}
811
812static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
813 const struct nlmsghdr *nlh,
814 const struct nlattr * const nla[])
815{
816 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
817 const struct nlattr * uninitialized_var(name);
818 const struct nft_af_info *afi;
819 struct nft_table *table;
820 struct nft_chain *chain;
821 struct nft_base_chain *basechain = NULL;
822 struct nlattr *ha[NFTA_HOOK_MAX + 1];
823 struct net *net = sock_net(skb->sk);
824 int family = nfmsg->nfgen_family;
825 u64 handle = 0;
826 int err;
827 bool create;
828
829 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
830
831 afi = nf_tables_afinfo_lookup(net, family, true);
832 if (IS_ERR(afi))
833 return PTR_ERR(afi);
834
835 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
836 if (IS_ERR(table))
837 return PTR_ERR(table);
838
839 if (table->use == UINT_MAX)
840 return -EOVERFLOW;
841
842 chain = NULL;
843 name = nla[NFTA_CHAIN_NAME];
844
845 if (nla[NFTA_CHAIN_HANDLE]) {
846 handle = be64_to_cpu(nla_get_be64(nla[NFTA_CHAIN_HANDLE]));
847 chain = nf_tables_chain_lookup_byhandle(table, handle);
848 if (IS_ERR(chain))
849 return PTR_ERR(chain);
850 } else {
851 chain = nf_tables_chain_lookup(table, name);
852 if (IS_ERR(chain)) {
853 if (PTR_ERR(chain) != -ENOENT)
854 return PTR_ERR(chain);
855 chain = NULL;
856 }
857 }
858
859 if (chain != NULL) {
860 if (nlh->nlmsg_flags & NLM_F_EXCL)
861 return -EEXIST;
862 if (nlh->nlmsg_flags & NLM_F_REPLACE)
863 return -EOPNOTSUPP;
864
865 if (nla[NFTA_CHAIN_HANDLE] && name &&
866 !IS_ERR(nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME])))
867 return -EEXIST;
868
869 if (nla[NFTA_CHAIN_POLICY]) {
870 if (!(chain->flags & NFT_BASE_CHAIN))
871 return -EOPNOTSUPP;
872
873 err = nf_tables_chain_policy(nft_base_chain(chain),
874 nla[NFTA_CHAIN_POLICY]);
875 if (err < 0)
876 return err;
877 }
878
879 if (nla[NFTA_CHAIN_COUNTERS]) {
880 if (!(chain->flags & NFT_BASE_CHAIN))
881 return -EOPNOTSUPP;
882
883 err = nf_tables_counters(nft_base_chain(chain),
884 nla[NFTA_CHAIN_COUNTERS]);
885 if (err < 0)
886 return err;
887 }
888
889 if (nla[NFTA_CHAIN_HANDLE] && name)
890 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
891
892 goto notify;
893 }
894
895 if (nla[NFTA_CHAIN_HOOK]) {
896 struct nf_hook_ops *ops;
897 nf_hookfn *hookfn;
898 u32 hooknum;
899 int type = NFT_CHAIN_T_DEFAULT;
900
901 if (nla[NFTA_CHAIN_TYPE]) {
902 type = nf_tables_chain_type_lookup(afi,
903 nla[NFTA_CHAIN_TYPE],
904 create);
905 if (type < 0)
906 return -ENOENT;
907 }
908
909 err = nla_parse_nested(ha, NFTA_HOOK_MAX, nla[NFTA_CHAIN_HOOK],
910 nft_hook_policy);
911 if (err < 0)
912 return err;
913 if (ha[NFTA_HOOK_HOOKNUM] == NULL ||
914 ha[NFTA_HOOK_PRIORITY] == NULL)
915 return -EINVAL;
916
917 hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
918 if (hooknum >= afi->nhooks)
919 return -EINVAL;
920
921 hookfn = chain_type[family][type]->fn[hooknum];
922 if (hookfn == NULL)
923 return -EOPNOTSUPP;
924
925 basechain = kzalloc(sizeof(*basechain), GFP_KERNEL);
926 if (basechain == NULL)
927 return -ENOMEM;
928
929 basechain->type = type;
930 chain = &basechain->chain;
931
932 ops = &basechain->ops;
933 ops->pf = family;
934 ops->owner = afi->owner;
935 ops->hooknum = ntohl(nla_get_be32(ha[NFTA_HOOK_HOOKNUM]));
936 ops->priority = ntohl(nla_get_be32(ha[NFTA_HOOK_PRIORITY]));
937 ops->priv = chain;
938 ops->hook = hookfn;
939 if (afi->hooks[ops->hooknum])
940 ops->hook = afi->hooks[ops->hooknum];
941
942 chain->flags |= NFT_BASE_CHAIN;
943
944 if (nla[NFTA_CHAIN_POLICY]) {
945 err = nf_tables_chain_policy(basechain,
946 nla[NFTA_CHAIN_POLICY]);
947 if (err < 0) {
948 free_percpu(basechain->stats);
949 kfree(basechain);
950 return err;
951 }
952 } else
953 basechain->policy = NF_ACCEPT;
954
955 if (nla[NFTA_CHAIN_COUNTERS]) {
956 err = nf_tables_counters(basechain,
957 nla[NFTA_CHAIN_COUNTERS]);
958 if (err < 0) {
959 free_percpu(basechain->stats);
960 kfree(basechain);
961 return err;
962 }
963 } else {
964 struct nft_stats __percpu *newstats;
965
966 newstats = alloc_percpu(struct nft_stats);
967 if (newstats == NULL)
968 return -ENOMEM;
969
970 rcu_assign_pointer(nft_base_chain(chain)->stats,
971 newstats);
972 }
973 } else {
974 chain = kzalloc(sizeof(*chain), GFP_KERNEL);
975 if (chain == NULL)
976 return -ENOMEM;
977 }
978
979 INIT_LIST_HEAD(&chain->rules);
980 chain->handle = nf_tables_alloc_handle(table);
981 chain->net = net;
982 chain->table = table;
983 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
984
985 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
986 chain->flags & NFT_BASE_CHAIN) {
987 err = nf_register_hook(&nft_base_chain(chain)->ops);
988 if (err < 0) {
989 free_percpu(basechain->stats);
990 kfree(basechain);
991 return err;
992 }
993 }
994 list_add_tail(&chain->list, &table->chains);
995 table->use++;
996notify:
997 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_NEWCHAIN,
998 family);
999 return 0;
1000}
1001
1002static void nf_tables_rcu_chain_destroy(struct rcu_head *head)
1003{
1004 struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head);
1005
1006 BUG_ON(chain->use > 0);
1007
1008 if (chain->flags & NFT_BASE_CHAIN) {
1009 free_percpu(nft_base_chain(chain)->stats);
1010 kfree(nft_base_chain(chain));
1011 } else
1012 kfree(chain);
1013}
1014
1015static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,
1016 const struct nlmsghdr *nlh,
1017 const struct nlattr * const nla[])
1018{
1019 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1020 const struct nft_af_info *afi;
1021 struct nft_table *table;
1022 struct nft_chain *chain;
1023 struct net *net = sock_net(skb->sk);
1024 int family = nfmsg->nfgen_family;
1025
1026 afi = nf_tables_afinfo_lookup(net, family, false);
1027 if (IS_ERR(afi))
1028 return PTR_ERR(afi);
1029
1030 table = nf_tables_table_lookup(afi, nla[NFTA_CHAIN_TABLE]);
1031 if (IS_ERR(table))
1032 return PTR_ERR(table);
1033
1034 chain = nf_tables_chain_lookup(table, nla[NFTA_CHAIN_NAME]);
1035 if (IS_ERR(chain))
1036 return PTR_ERR(chain);
1037
1038 if (!list_empty(&chain->rules))
1039 return -EBUSY;
1040
1041 list_del(&chain->list);
1042 table->use--;
1043
1044 if (!(table->flags & NFT_TABLE_F_DORMANT) &&
1045 chain->flags & NFT_BASE_CHAIN)
1046 nf_unregister_hook(&nft_base_chain(chain)->ops);
1047
1048 nf_tables_chain_notify(skb, nlh, table, chain, NFT_MSG_DELCHAIN,
1049 family);
1050
1051 /* Make sure all rule references are gone before this is released */
1052 call_rcu(&chain->rcu_head, nf_tables_rcu_chain_destroy);
1053 return 0;
1054}
1055
1056static void nft_ctx_init(struct nft_ctx *ctx,
1057 const struct sk_buff *skb,
1058 const struct nlmsghdr *nlh,
1059 const struct nft_af_info *afi,
1060 const struct nft_table *table,
1061 const struct nft_chain *chain,
1062 const struct nlattr * const *nla)
1063{
1064 ctx->net = sock_net(skb->sk);
1065 ctx->skb = skb;
1066 ctx->nlh = nlh;
1067 ctx->afi = afi;
1068 ctx->table = table;
1069 ctx->chain = chain;
1070 ctx->nla = nla;
1071}
1072
1073/*
1074 * Expressions
1075 */
1076
1077/**
1078 * nft_register_expr - register nf_tables expr type
1079 * @ops: expr type
1080 *
1081 * Registers the expr type for use with nf_tables. Returns zero on
1082 * success or a negative errno code otherwise.
1083 */
1084int nft_register_expr(struct nft_expr_type *type)
1085{
1086 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1087 list_add_tail(&type->list, &nf_tables_expressions);
1088 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1089 return 0;
1090}
1091EXPORT_SYMBOL_GPL(nft_register_expr);
1092
1093/**
1094 * nft_unregister_expr - unregister nf_tables expr type
1095 * @ops: expr type
1096 *
1097 * Unregisters the expr typefor use with nf_tables.
1098 */
1099void nft_unregister_expr(struct nft_expr_type *type)
1100{
1101 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1102 list_del(&type->list);
1103 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1104}
1105EXPORT_SYMBOL_GPL(nft_unregister_expr);
1106
1107static const struct nft_expr_type *__nft_expr_type_get(struct nlattr *nla)
1108{
1109 const struct nft_expr_type *type;
1110
1111 list_for_each_entry(type, &nf_tables_expressions, list) {
1112 if (!nla_strcmp(nla, type->name))
1113 return type;
1114 }
1115 return NULL;
1116}
1117
1118static const struct nft_expr_type *nft_expr_type_get(struct nlattr *nla)
1119{
1120 const struct nft_expr_type *type;
1121
1122 if (nla == NULL)
1123 return ERR_PTR(-EINVAL);
1124
1125 type = __nft_expr_type_get(nla);
1126 if (type != NULL && try_module_get(type->owner))
1127 return type;
1128
1129#ifdef CONFIG_MODULES
1130 if (type == NULL) {
1131 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1132 request_module("nft-expr-%.*s",
1133 nla_len(nla), (char *)nla_data(nla));
1134 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1135 if (__nft_expr_type_get(nla))
1136 return ERR_PTR(-EAGAIN);
1137 }
1138#endif
1139 return ERR_PTR(-ENOENT);
1140}
1141
1142static const struct nla_policy nft_expr_policy[NFTA_EXPR_MAX + 1] = {
1143 [NFTA_EXPR_NAME] = { .type = NLA_STRING },
1144 [NFTA_EXPR_DATA] = { .type = NLA_NESTED },
1145};
1146
1147static int nf_tables_fill_expr_info(struct sk_buff *skb,
1148 const struct nft_expr *expr)
1149{
1150 if (nla_put_string(skb, NFTA_EXPR_NAME, expr->ops->type->name))
1151 goto nla_put_failure;
1152
1153 if (expr->ops->dump) {
1154 struct nlattr *data = nla_nest_start(skb, NFTA_EXPR_DATA);
1155 if (data == NULL)
1156 goto nla_put_failure;
1157 if (expr->ops->dump(skb, expr) < 0)
1158 goto nla_put_failure;
1159 nla_nest_end(skb, data);
1160 }
1161
1162 return skb->len;
1163
1164nla_put_failure:
1165 return -1;
1166};
1167
1168struct nft_expr_info {
1169 const struct nft_expr_ops *ops;
1170 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
1171};
1172
1173static int nf_tables_expr_parse(const struct nft_ctx *ctx,
1174 const struct nlattr *nla,
1175 struct nft_expr_info *info)
1176{
1177 const struct nft_expr_type *type;
1178 const struct nft_expr_ops *ops;
1179 struct nlattr *tb[NFTA_EXPR_MAX + 1];
1180 int err;
1181
1182 err = nla_parse_nested(tb, NFTA_EXPR_MAX, nla, nft_expr_policy);
1183 if (err < 0)
1184 return err;
1185
1186 type = nft_expr_type_get(tb[NFTA_EXPR_NAME]);
1187 if (IS_ERR(type))
1188 return PTR_ERR(type);
1189
1190 if (tb[NFTA_EXPR_DATA]) {
1191 err = nla_parse_nested(info->tb, type->maxattr,
1192 tb[NFTA_EXPR_DATA], type->policy);
1193 if (err < 0)
1194 goto err1;
1195 } else
1196 memset(info->tb, 0, sizeof(info->tb[0]) * (type->maxattr + 1));
1197
1198 if (type->select_ops != NULL) {
1199 ops = type->select_ops(ctx,
1200 (const struct nlattr * const *)info->tb);
1201 if (IS_ERR(ops)) {
1202 err = PTR_ERR(ops);
1203 goto err1;
1204 }
1205 } else
1206 ops = type->ops;
1207
1208 info->ops = ops;
1209 return 0;
1210
1211err1:
1212 module_put(type->owner);
1213 return err;
1214}
1215
1216static int nf_tables_newexpr(const struct nft_ctx *ctx,
1217 const struct nft_expr_info *info,
1218 struct nft_expr *expr)
1219{
1220 const struct nft_expr_ops *ops = info->ops;
1221 int err;
1222
1223 expr->ops = ops;
1224 if (ops->init) {
1225 err = ops->init(ctx, expr, (const struct nlattr **)info->tb);
1226 if (err < 0)
1227 goto err1;
1228 }
1229
1230 return 0;
1231
1232err1:
1233 expr->ops = NULL;
1234 return err;
1235}
1236
1237static void nf_tables_expr_destroy(struct nft_expr *expr)
1238{
1239 if (expr->ops->destroy)
1240 expr->ops->destroy(expr);
1241 module_put(expr->ops->type->owner);
1242}
1243
1244/*
1245 * Rules
1246 */
1247
1248static struct nft_rule *__nf_tables_rule_lookup(const struct nft_chain *chain,
1249 u64 handle)
1250{
1251 struct nft_rule *rule;
1252
1253 // FIXME: this sucks
1254 list_for_each_entry(rule, &chain->rules, list) {
1255 if (handle == rule->handle)
1256 return rule;
1257 }
1258
1259 return ERR_PTR(-ENOENT);
1260}
1261
1262static struct nft_rule *nf_tables_rule_lookup(const struct nft_chain *chain,
1263 const struct nlattr *nla)
1264{
1265 if (nla == NULL)
1266 return ERR_PTR(-EINVAL);
1267
1268 return __nf_tables_rule_lookup(chain, be64_to_cpu(nla_get_be64(nla)));
1269}
1270
1271static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = {
1272 [NFTA_RULE_TABLE] = { .type = NLA_STRING },
1273 [NFTA_RULE_CHAIN] = { .type = NLA_STRING,
1274 .len = NFT_CHAIN_MAXNAMELEN - 1 },
1275 [NFTA_RULE_HANDLE] = { .type = NLA_U64 },
1276 [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED },
1277 [NFTA_RULE_COMPAT] = { .type = NLA_NESTED },
1278 [NFTA_RULE_POSITION] = { .type = NLA_U64 },
1279};
1280
1281static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
1282 int event, u32 flags, int family,
1283 const struct nft_table *table,
1284 const struct nft_chain *chain,
1285 const struct nft_rule *rule)
1286{
1287 struct nlmsghdr *nlh;
1288 struct nfgenmsg *nfmsg;
1289 const struct nft_expr *expr, *next;
1290 struct nlattr *list;
1291 const struct nft_rule *prule;
1292 int type = event | NFNL_SUBSYS_NFTABLES << 8;
1293
1294 nlh = nlmsg_put(skb, portid, seq, type, sizeof(struct nfgenmsg),
1295 flags);
1296 if (nlh == NULL)
1297 goto nla_put_failure;
1298
1299 nfmsg = nlmsg_data(nlh);
1300 nfmsg->nfgen_family = family;
1301 nfmsg->version = NFNETLINK_V0;
1302 nfmsg->res_id = 0;
1303
1304 if (nla_put_string(skb, NFTA_RULE_TABLE, table->name))
1305 goto nla_put_failure;
1306 if (nla_put_string(skb, NFTA_RULE_CHAIN, chain->name))
1307 goto nla_put_failure;
1308 if (nla_put_be64(skb, NFTA_RULE_HANDLE, cpu_to_be64(rule->handle)))
1309 goto nla_put_failure;
1310
1311 if ((event != NFT_MSG_DELRULE) && (rule->list.prev != &chain->rules)) {
1312 prule = list_entry(rule->list.prev, struct nft_rule, list);
1313 if (nla_put_be64(skb, NFTA_RULE_POSITION,
1314 cpu_to_be64(prule->handle)))
1315 goto nla_put_failure;
1316 }
1317
1318 list = nla_nest_start(skb, NFTA_RULE_EXPRESSIONS);
1319 if (list == NULL)
1320 goto nla_put_failure;
1321 nft_rule_for_each_expr(expr, next, rule) {
1322 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM);
1323 if (elem == NULL)
1324 goto nla_put_failure;
1325 if (nf_tables_fill_expr_info(skb, expr) < 0)
1326 goto nla_put_failure;
1327 nla_nest_end(skb, elem);
1328 }
1329 nla_nest_end(skb, list);
1330
1331 return nlmsg_end(skb, nlh);
1332
1333nla_put_failure:
1334 nlmsg_trim(skb, nlh);
1335 return -1;
1336}
1337
1338static int nf_tables_rule_notify(const struct sk_buff *oskb,
1339 const struct nlmsghdr *nlh,
1340 const struct nft_table *table,
1341 const struct nft_chain *chain,
1342 const struct nft_rule *rule,
1343 int event, u32 flags, int family)
1344{
1345 struct sk_buff *skb;
1346 u32 portid = NETLINK_CB(oskb).portid;
1347 struct net *net = oskb ? sock_net(oskb->sk) : &init_net;
1348 u32 seq = nlh->nlmsg_seq;
1349 bool report;
1350 int err;
1351
1352 report = nlmsg_report(nlh);
1353 if (!report && !nfnetlink_has_listeners(net, NFNLGRP_NFTABLES))
1354 return 0;
1355
1356 err = -ENOBUFS;
1357 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
1358 if (skb == NULL)
1359 goto err;
1360
1361 err = nf_tables_fill_rule_info(skb, portid, seq, event, flags,
1362 family, table, chain, rule);
1363 if (err < 0) {
1364 kfree_skb(skb);
1365 goto err;
1366 }
1367
1368 err = nfnetlink_send(skb, net, portid, NFNLGRP_NFTABLES, report,
1369 GFP_KERNEL);
1370err:
1371 if (err < 0)
1372 nfnetlink_set_err(net, portid, NFNLGRP_NFTABLES, err);
1373 return err;
1374}
1375
1376static inline bool
1377nft_rule_is_active(struct net *net, const struct nft_rule *rule)
1378{
1379 return (rule->genmask & (1 << net->nft.gencursor)) == 0;
1380}
1381
1382static inline int gencursor_next(struct net *net)
1383{
1384 return net->nft.gencursor+1 == 1 ? 1 : 0;
1385}
1386
1387static inline int
1388nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
1389{
1390 return (rule->genmask & (1 << gencursor_next(net))) == 0;
1391}
1392
1393static inline void
1394nft_rule_activate_next(struct net *net, struct nft_rule *rule)
1395{
1396 /* Now inactive, will be active in the future */
1397 rule->genmask = (1 << net->nft.gencursor);
1398}
1399
1400static inline void
1401nft_rule_disactivate_next(struct net *net, struct nft_rule *rule)
1402{
1403 rule->genmask = (1 << gencursor_next(net));
1404}
1405
1406static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
1407{
1408 rule->genmask = 0;
1409}
1410
1411static int nf_tables_dump_rules(struct sk_buff *skb,
1412 struct netlink_callback *cb)
1413{
1414 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1415 const struct nft_af_info *afi;
1416 const struct nft_table *table;
1417 const struct nft_chain *chain;
1418 const struct nft_rule *rule;
1419 unsigned int idx = 0, s_idx = cb->args[0];
1420 struct net *net = sock_net(skb->sk);
1421 int family = nfmsg->nfgen_family;
1422 u8 genctr = ACCESS_ONCE(net->nft.genctr);
1423 u8 gencursor = ACCESS_ONCE(net->nft.gencursor);
1424
1425 list_for_each_entry(afi, &net->nft.af_info, list) {
1426 if (family != NFPROTO_UNSPEC && family != afi->family)
1427 continue;
1428
1429 list_for_each_entry(table, &afi->tables, list) {
1430 list_for_each_entry(chain, &table->chains, list) {
1431 list_for_each_entry(rule, &chain->rules, list) {
1432 if (!nft_rule_is_active(net, rule))
1433 goto cont;
1434 if (idx < s_idx)
1435 goto cont;
1436 if (idx > s_idx)
1437 memset(&cb->args[1], 0,
1438 sizeof(cb->args) - sizeof(cb->args[0]));
1439 if (nf_tables_fill_rule_info(skb, NETLINK_CB(cb->skb).portid,
1440 cb->nlh->nlmsg_seq,
1441 NFT_MSG_NEWRULE,
1442 NLM_F_MULTI | NLM_F_APPEND,
1443 afi->family, table, chain, rule) < 0)
1444 goto done;
1445cont:
1446 idx++;
1447 }
1448 }
1449 }
1450 }
1451done:
1452 /* Invalidate this dump, a transition to the new generation happened */
1453 if (gencursor != net->nft.gencursor || genctr != net->nft.genctr)
1454 return -EBUSY;
1455
1456 cb->args[0] = idx;
1457 return skb->len;
1458}
1459
1460static int nf_tables_getrule(struct sock *nlsk, struct sk_buff *skb,
1461 const struct nlmsghdr *nlh,
1462 const struct nlattr * const nla[])
1463{
1464 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1465 const struct nft_af_info *afi;
1466 const struct nft_table *table;
1467 const struct nft_chain *chain;
1468 const struct nft_rule *rule;
1469 struct sk_buff *skb2;
1470 struct net *net = sock_net(skb->sk);
1471 int family = nfmsg->nfgen_family;
1472 int err;
1473
1474 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1475 struct netlink_dump_control c = {
1476 .dump = nf_tables_dump_rules,
1477 };
1478 return netlink_dump_start(nlsk, skb, nlh, &c);
1479 }
1480
1481 afi = nf_tables_afinfo_lookup(net, family, false);
1482 if (IS_ERR(afi))
1483 return PTR_ERR(afi);
1484
1485 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1486 if (IS_ERR(table))
1487 return PTR_ERR(table);
1488
1489 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1490 if (IS_ERR(chain))
1491 return PTR_ERR(chain);
1492
1493 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1494 if (IS_ERR(rule))
1495 return PTR_ERR(rule);
1496
1497 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1498 if (!skb2)
1499 return -ENOMEM;
1500
1501 err = nf_tables_fill_rule_info(skb2, NETLINK_CB(skb).portid,
1502 nlh->nlmsg_seq, NFT_MSG_NEWRULE, 0,
1503 family, table, chain, rule);
1504 if (err < 0)
1505 goto err;
1506
1507 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
1508
1509err:
1510 kfree_skb(skb2);
1511 return err;
1512}
1513
1514static void nf_tables_rcu_rule_destroy(struct rcu_head *head)
1515{
1516 struct nft_rule *rule = container_of(head, struct nft_rule, rcu_head);
1517 struct nft_expr *expr;
1518
1519 /*
1520 * Careful: some expressions might not be initialized in case this
1521 * is called on error from nf_tables_newrule().
1522 */
1523 expr = nft_expr_first(rule);
1524 while (expr->ops && expr != nft_expr_last(rule)) {
1525 nf_tables_expr_destroy(expr);
1526 expr = nft_expr_next(expr);
1527 }
1528 kfree(rule);
1529}
1530
1531static void nf_tables_rule_destroy(struct nft_rule *rule)
1532{
1533 call_rcu(&rule->rcu_head, nf_tables_rcu_rule_destroy);
1534}
1535
1536#define NFT_RULE_MAXEXPRS 128
1537
1538static struct nft_expr_info *info;
1539
1540static struct nft_rule_trans *
1541nf_tables_trans_add(struct nft_rule *rule, const struct nft_ctx *ctx)
1542{
1543 struct nft_rule_trans *rupd;
1544
1545 rupd = kmalloc(sizeof(struct nft_rule_trans), GFP_KERNEL);
1546 if (rupd == NULL)
1547 return NULL;
1548
1549 rupd->chain = ctx->chain;
1550 rupd->table = ctx->table;
1551 rupd->rule = rule;
1552 rupd->family = ctx->afi->family;
1553 rupd->nlh = ctx->nlh;
1554 list_add_tail(&rupd->list, &ctx->net->nft.commit_list);
1555
1556 return rupd;
1557}
1558
1559static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb,
1560 const struct nlmsghdr *nlh,
1561 const struct nlattr * const nla[])
1562{
1563 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1564 const struct nft_af_info *afi;
1565 struct net *net = sock_net(skb->sk);
1566 struct nft_table *table;
1567 struct nft_chain *chain;
1568 struct nft_rule *rule, *old_rule = NULL;
1569 struct nft_rule_trans *repl = NULL;
1570 struct nft_expr *expr;
1571 struct nft_ctx ctx;
1572 struct nlattr *tmp;
1573 unsigned int size, i, n;
1574 int err, rem;
1575 bool create;
1576 u64 handle, pos_handle;
1577
1578 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
1579
1580 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
1581 if (IS_ERR(afi))
1582 return PTR_ERR(afi);
1583
1584 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1585 if (IS_ERR(table))
1586 return PTR_ERR(table);
1587
1588 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1589 if (IS_ERR(chain))
1590 return PTR_ERR(chain);
1591
1592 if (nla[NFTA_RULE_HANDLE]) {
1593 handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_HANDLE]));
1594 rule = __nf_tables_rule_lookup(chain, handle);
1595 if (IS_ERR(rule))
1596 return PTR_ERR(rule);
1597
1598 if (nlh->nlmsg_flags & NLM_F_EXCL)
1599 return -EEXIST;
1600 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1601 old_rule = rule;
1602 else
1603 return -EOPNOTSUPP;
1604 } else {
1605 if (!create || nlh->nlmsg_flags & NLM_F_REPLACE)
1606 return -EINVAL;
1607 handle = nf_tables_alloc_handle(table);
1608 }
1609
1610 if (nla[NFTA_RULE_POSITION]) {
1611 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1612 return -EOPNOTSUPP;
1613
1614 pos_handle = be64_to_cpu(nla_get_be64(nla[NFTA_RULE_POSITION]));
1615 old_rule = __nf_tables_rule_lookup(chain, pos_handle);
1616 if (IS_ERR(old_rule))
1617 return PTR_ERR(old_rule);
1618 }
1619
1620 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1621
1622 n = 0;
1623 size = 0;
1624 if (nla[NFTA_RULE_EXPRESSIONS]) {
1625 nla_for_each_nested(tmp, nla[NFTA_RULE_EXPRESSIONS], rem) {
1626 err = -EINVAL;
1627 if (nla_type(tmp) != NFTA_LIST_ELEM)
1628 goto err1;
1629 if (n == NFT_RULE_MAXEXPRS)
1630 goto err1;
1631 err = nf_tables_expr_parse(&ctx, tmp, &info[n]);
1632 if (err < 0)
1633 goto err1;
1634 size += info[n].ops->size;
1635 n++;
1636 }
1637 }
1638
1639 err = -ENOMEM;
1640 rule = kzalloc(sizeof(*rule) + size, GFP_KERNEL);
1641 if (rule == NULL)
1642 goto err1;
1643
1644 nft_rule_activate_next(net, rule);
1645
1646 rule->handle = handle;
1647 rule->dlen = size;
1648
1649 expr = nft_expr_first(rule);
1650 for (i = 0; i < n; i++) {
1651 err = nf_tables_newexpr(&ctx, &info[i], expr);
1652 if (err < 0)
1653 goto err2;
1654 info[i].ops = NULL;
1655 expr = nft_expr_next(expr);
1656 }
1657
1658 if (nlh->nlmsg_flags & NLM_F_REPLACE) {
1659 if (nft_rule_is_active_next(net, old_rule)) {
1660 repl = nf_tables_trans_add(old_rule, &ctx);
1661 if (repl == NULL) {
1662 err = -ENOMEM;
1663 goto err2;
1664 }
1665 nft_rule_disactivate_next(net, old_rule);
1666 list_add_tail(&rule->list, &old_rule->list);
1667 } else {
1668 err = -ENOENT;
1669 goto err2;
1670 }
1671 } else if (nlh->nlmsg_flags & NLM_F_APPEND)
1672 if (old_rule)
1673 list_add_rcu(&rule->list, &old_rule->list);
1674 else
1675 list_add_tail_rcu(&rule->list, &chain->rules);
1676 else {
1677 if (old_rule)
1678 list_add_tail_rcu(&rule->list, &old_rule->list);
1679 else
1680 list_add_rcu(&rule->list, &chain->rules);
1681 }
1682
1683 if (nf_tables_trans_add(rule, &ctx) == NULL) {
1684 err = -ENOMEM;
1685 goto err3;
1686 }
1687 return 0;
1688
1689err3:
1690 list_del_rcu(&rule->list);
1691 if (repl) {
1692 list_del_rcu(&repl->rule->list);
1693 list_del(&repl->list);
1694 nft_rule_clear(net, repl->rule);
1695 kfree(repl);
1696 }
1697err2:
1698 nf_tables_rule_destroy(rule);
1699err1:
1700 for (i = 0; i < n; i++) {
1701 if (info[i].ops != NULL)
1702 module_put(info[i].ops->type->owner);
1703 }
1704 return err;
1705}
1706
1707static int
1708nf_tables_delrule_one(struct nft_ctx *ctx, struct nft_rule *rule)
1709{
1710 /* You cannot delete the same rule twice */
1711 if (nft_rule_is_active_next(ctx->net, rule)) {
1712 if (nf_tables_trans_add(rule, ctx) == NULL)
1713 return -ENOMEM;
1714 nft_rule_disactivate_next(ctx->net, rule);
1715 return 0;
1716 }
1717 return -ENOENT;
1718}
1719
1720static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,
1721 const struct nlmsghdr *nlh,
1722 const struct nlattr * const nla[])
1723{
1724 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1725 const struct nft_af_info *afi;
1726 struct net *net = sock_net(skb->sk);
1727 const struct nft_table *table;
1728 struct nft_chain *chain;
1729 struct nft_rule *rule, *tmp;
1730 int family = nfmsg->nfgen_family, err = 0;
1731 struct nft_ctx ctx;
1732
1733 afi = nf_tables_afinfo_lookup(net, family, false);
1734 if (IS_ERR(afi))
1735 return PTR_ERR(afi);
1736
1737 table = nf_tables_table_lookup(afi, nla[NFTA_RULE_TABLE]);
1738 if (IS_ERR(table))
1739 return PTR_ERR(table);
1740
1741 chain = nf_tables_chain_lookup(table, nla[NFTA_RULE_CHAIN]);
1742 if (IS_ERR(chain))
1743 return PTR_ERR(chain);
1744
1745 nft_ctx_init(&ctx, skb, nlh, afi, table, chain, nla);
1746
1747 if (nla[NFTA_RULE_HANDLE]) {
1748 rule = nf_tables_rule_lookup(chain, nla[NFTA_RULE_HANDLE]);
1749 if (IS_ERR(rule))
1750 return PTR_ERR(rule);
1751
1752 err = nf_tables_delrule_one(&ctx, rule);
1753 } else {
1754 /* Remove all rules in this chain */
1755 list_for_each_entry_safe(rule, tmp, &chain->rules, list) {
1756 err = nf_tables_delrule_one(&ctx, rule);
1757 if (err < 0)
1758 break;
1759 }
1760 }
1761
1762 return err;
1763}
1764
1765static int nf_tables_commit(struct sk_buff *skb)
1766{
1767 struct net *net = sock_net(skb->sk);
1768 struct nft_rule_trans *rupd, *tmp;
1769
1770 /* Bump generation counter, invalidate any dump in progress */
1771 net->nft.genctr++;
1772
1773 /* A new generation has just started */
1774 net->nft.gencursor = gencursor_next(net);
1775
1776 /* Make sure all packets have left the previous generation before
1777 * purging old rules.
1778 */
1779 synchronize_rcu();
1780
1781 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1782 /* Delete this rule from the dirty list */
1783 list_del(&rupd->list);
1784
1785 /* This rule was inactive in the past and just became active.
1786 * Clear the next bit of the genmask since its meaning has
1787 * changed, now it is the future.
1788 */
1789 if (nft_rule_is_active(net, rupd->rule)) {
1790 nft_rule_clear(net, rupd->rule);
1791 nf_tables_rule_notify(skb, rupd->nlh, rupd->table,
1792 rupd->chain, rupd->rule,
1793 NFT_MSG_NEWRULE, 0,
1794 rupd->family);
1795 kfree(rupd);
1796 continue;
1797 }
1798
1799 /* This rule is in the past, get rid of it */
1800 list_del_rcu(&rupd->rule->list);
1801 nf_tables_rule_notify(skb, rupd->nlh, rupd->table, rupd->chain,
1802 rupd->rule, NFT_MSG_DELRULE, 0,
1803 rupd->family);
1804 nf_tables_rule_destroy(rupd->rule);
1805 kfree(rupd);
1806 }
1807
1808 return 0;
1809}
1810
1811static int nf_tables_abort(struct sk_buff *skb)
1812{
1813 struct net *net = sock_net(skb->sk);
1814 struct nft_rule_trans *rupd, *tmp;
1815
1816 list_for_each_entry_safe(rupd, tmp, &net->nft.commit_list, list) {
1817 /* Delete all rules from the dirty list */
1818 list_del(&rupd->list);
1819
1820 if (!nft_rule_is_active_next(net, rupd->rule)) {
1821 nft_rule_clear(net, rupd->rule);
1822 kfree(rupd);
1823 continue;
1824 }
1825
1826 /* This rule is inactive, get rid of it */
1827 list_del_rcu(&rupd->rule->list);
1828 nf_tables_rule_destroy(rupd->rule);
1829 kfree(rupd);
1830 }
1831 return 0;
1832}
1833
1834/*
1835 * Sets
1836 */
1837
1838static LIST_HEAD(nf_tables_set_ops);
1839
1840int nft_register_set(struct nft_set_ops *ops)
1841{
1842 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1843 list_add_tail(&ops->list, &nf_tables_set_ops);
1844 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1845 return 0;
1846}
1847EXPORT_SYMBOL_GPL(nft_register_set);
1848
1849void nft_unregister_set(struct nft_set_ops *ops)
1850{
1851 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1852 list_del(&ops->list);
1853 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1854}
1855EXPORT_SYMBOL_GPL(nft_unregister_set);
1856
1857static const struct nft_set_ops *nft_select_set_ops(const struct nlattr * const nla[])
1858{
1859 const struct nft_set_ops *ops;
1860 u32 features;
1861
1862#ifdef CONFIG_MODULES
1863 if (list_empty(&nf_tables_set_ops)) {
1864 nfnl_unlock(NFNL_SUBSYS_NFTABLES);
1865 request_module("nft-set");
1866 nfnl_lock(NFNL_SUBSYS_NFTABLES);
1867 if (!list_empty(&nf_tables_set_ops))
1868 return ERR_PTR(-EAGAIN);
1869 }
1870#endif
1871 features = 0;
1872 if (nla[NFTA_SET_FLAGS] != NULL) {
1873 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
1874 features &= NFT_SET_INTERVAL | NFT_SET_MAP;
1875 }
1876
1877 // FIXME: implement selection properly
1878 list_for_each_entry(ops, &nf_tables_set_ops, list) {
1879 if ((ops->features & features) != features)
1880 continue;
1881 if (!try_module_get(ops->owner))
1882 continue;
1883 return ops;
1884 }
1885
1886 return ERR_PTR(-EOPNOTSUPP);
1887}
1888
1889static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
1890 [NFTA_SET_TABLE] = { .type = NLA_STRING },
1891 [NFTA_SET_NAME] = { .type = NLA_STRING },
1892 [NFTA_SET_FLAGS] = { .type = NLA_U32 },
1893 [NFTA_SET_KEY_TYPE] = { .type = NLA_U32 },
1894 [NFTA_SET_KEY_LEN] = { .type = NLA_U32 },
1895 [NFTA_SET_DATA_TYPE] = { .type = NLA_U32 },
1896 [NFTA_SET_DATA_LEN] = { .type = NLA_U32 },
1897};
1898
1899static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,
1900 const struct sk_buff *skb,
1901 const struct nlmsghdr *nlh,
1902 const struct nlattr * const nla[])
1903{
1904 struct net *net = sock_net(skb->sk);
1905 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1906 const struct nft_af_info *afi;
1907 const struct nft_table *table = NULL;
1908
1909 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
1910 if (IS_ERR(afi))
1911 return PTR_ERR(afi);
1912
1913 if (nla[NFTA_SET_TABLE] != NULL) {
1914 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
1915 if (IS_ERR(table))
1916 return PTR_ERR(table);
1917 }
1918
1919 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
1920 return 0;
1921}
1922
1923struct nft_set *nf_tables_set_lookup(const struct nft_table *table,
1924 const struct nlattr *nla)
1925{
1926 struct nft_set *set;
1927
1928 if (nla == NULL)
1929 return ERR_PTR(-EINVAL);
1930
1931 list_for_each_entry(set, &table->sets, list) {
1932 if (!nla_strcmp(nla, set->name))
1933 return set;
1934 }
1935 return ERR_PTR(-ENOENT);
1936}
1937
1938static int nf_tables_set_alloc_name(struct nft_ctx *ctx, struct nft_set *set,
1939 const char *name)
1940{
1941 const struct nft_set *i;
1942 const char *p;
1943 unsigned long *inuse;
1944 unsigned int n = 0;
1945
1946 p = strnchr(name, IFNAMSIZ, '%');
1947 if (p != NULL) {
1948 if (p[1] != 'd' || strchr(p + 2, '%'))
1949 return -EINVAL;
1950
1951 inuse = (unsigned long *)get_zeroed_page(GFP_KERNEL);
1952 if (inuse == NULL)
1953 return -ENOMEM;
1954
1955 list_for_each_entry(i, &ctx->table->sets, list) {
1956 if (!sscanf(i->name, name, &n))
1957 continue;
1958 if (n < 0 || n > BITS_PER_LONG * PAGE_SIZE)
1959 continue;
1960 set_bit(n, inuse);
1961 }
1962
1963 n = find_first_zero_bit(inuse, BITS_PER_LONG * PAGE_SIZE);
1964 free_page((unsigned long)inuse);
1965 }
1966
1967 snprintf(set->name, sizeof(set->name), name, n);
1968 list_for_each_entry(i, &ctx->table->sets, list) {
1969 if (!strcmp(set->name, i->name))
1970 return -ENFILE;
1971 }
1972 return 0;
1973}
1974
1975static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
1976 const struct nft_set *set, u16 event, u16 flags)
1977{
1978 struct nfgenmsg *nfmsg;
1979 struct nlmsghdr *nlh;
1980 u32 portid = NETLINK_CB(ctx->skb).portid;
1981 u32 seq = ctx->nlh->nlmsg_seq;
1982
1983 event |= NFNL_SUBSYS_NFTABLES << 8;
1984 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
1985 flags);
1986 if (nlh == NULL)
1987 goto nla_put_failure;
1988
1989 nfmsg = nlmsg_data(nlh);
1990 nfmsg->nfgen_family = ctx->afi->family;
1991 nfmsg->version = NFNETLINK_V0;
1992 nfmsg->res_id = 0;
1993
1994 if (nla_put_string(skb, NFTA_SET_TABLE, ctx->table->name))
1995 goto nla_put_failure;
1996 if (nla_put_string(skb, NFTA_SET_NAME, set->name))
1997 goto nla_put_failure;
1998 if (set->flags != 0)
1999 if (nla_put_be32(skb, NFTA_SET_FLAGS, htonl(set->flags)))
2000 goto nla_put_failure;
2001
2002 if (nla_put_be32(skb, NFTA_SET_KEY_TYPE, htonl(set->ktype)))
2003 goto nla_put_failure;
2004 if (nla_put_be32(skb, NFTA_SET_KEY_LEN, htonl(set->klen)))
2005 goto nla_put_failure;
2006 if (set->flags & NFT_SET_MAP) {
2007 if (nla_put_be32(skb, NFTA_SET_DATA_TYPE, htonl(set->dtype)))
2008 goto nla_put_failure;
2009 if (nla_put_be32(skb, NFTA_SET_DATA_LEN, htonl(set->dlen)))
2010 goto nla_put_failure;
2011 }
2012
2013 return nlmsg_end(skb, nlh);
2014
2015nla_put_failure:
2016 nlmsg_trim(skb, nlh);
2017 return -1;
2018}
2019
2020static int nf_tables_set_notify(const struct nft_ctx *ctx,
2021 const struct nft_set *set,
2022 int event)
2023{
2024 struct sk_buff *skb;
2025 u32 portid = NETLINK_CB(ctx->skb).portid;
2026 bool report;
2027 int err;
2028
2029 report = nlmsg_report(ctx->nlh);
2030 if (!report && !nfnetlink_has_listeners(ctx->net, NFNLGRP_NFTABLES))
2031 return 0;
2032
2033 err = -ENOBUFS;
2034 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
2035 if (skb == NULL)
2036 goto err;
2037
2038 err = nf_tables_fill_set(skb, ctx, set, event, 0);
2039 if (err < 0) {
2040 kfree_skb(skb);
2041 goto err;
2042 }
2043
2044 err = nfnetlink_send(skb, ctx->net, portid, NFNLGRP_NFTABLES, report,
2045 GFP_KERNEL);
2046err:
2047 if (err < 0)
2048 nfnetlink_set_err(ctx->net, portid, NFNLGRP_NFTABLES, err);
2049 return err;
2050}
2051
2052static int nf_tables_dump_sets_table(struct nft_ctx *ctx, struct sk_buff *skb,
2053 struct netlink_callback *cb)
2054{
2055 const struct nft_set *set;
2056 unsigned int idx = 0, s_idx = cb->args[0];
2057
2058 if (cb->args[1])
2059 return skb->len;
2060
2061 list_for_each_entry(set, &ctx->table->sets, list) {
2062 if (idx < s_idx)
2063 goto cont;
2064 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2065 NLM_F_MULTI) < 0) {
2066 cb->args[0] = idx;
2067 goto done;
2068 }
2069cont:
2070 idx++;
2071 }
2072 cb->args[1] = 1;
2073done:
2074 return skb->len;
2075}
2076
2077static int nf_tables_dump_sets_all(struct nft_ctx *ctx, struct sk_buff *skb,
2078 struct netlink_callback *cb)
2079{
2080 const struct nft_set *set;
2081 unsigned int idx = 0, s_idx = cb->args[0];
2082 struct nft_table *table, *cur_table = (struct nft_table *)cb->args[2];
2083
2084 if (cb->args[1])
2085 return skb->len;
2086
2087 list_for_each_entry(table, &ctx->afi->tables, list) {
2088 if (cur_table && cur_table != table)
2089 continue;
2090
2091 ctx->table = table;
2092 list_for_each_entry(set, &ctx->table->sets, list) {
2093 if (idx < s_idx)
2094 goto cont;
2095 if (nf_tables_fill_set(skb, ctx, set, NFT_MSG_NEWSET,
2096 NLM_F_MULTI) < 0) {
2097 cb->args[0] = idx;
2098 cb->args[2] = (unsigned long) table;
2099 goto done;
2100 }
2101cont:
2102 idx++;
2103 }
2104 }
2105 cb->args[1] = 1;
2106done:
2107 return skb->len;
2108}
2109
2110static int nf_tables_dump_sets(struct sk_buff *skb, struct netlink_callback *cb)
2111{
2112 const struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2113 struct nlattr *nla[NFTA_SET_MAX + 1];
2114 struct nft_ctx ctx;
2115 int err, ret;
2116
2117 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_MAX,
2118 nft_set_policy);
2119 if (err < 0)
2120 return err;
2121
2122 err = nft_ctx_init_from_setattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2123 if (err < 0)
2124 return err;
2125
2126 if (ctx.table == NULL)
2127 ret = nf_tables_dump_sets_all(&ctx, skb, cb);
2128 else
2129 ret = nf_tables_dump_sets_table(&ctx, skb, cb);
2130
2131 return ret;
2132}
2133
2134static int nf_tables_getset(struct sock *nlsk, struct sk_buff *skb,
2135 const struct nlmsghdr *nlh,
2136 const struct nlattr * const nla[])
2137{
2138 const struct nft_set *set;
2139 struct nft_ctx ctx;
2140 struct sk_buff *skb2;
2141 int err;
2142
2143 /* Verify existance before starting dump */
2144 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2145 if (err < 0)
2146 return err;
2147
2148 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2149 struct netlink_dump_control c = {
2150 .dump = nf_tables_dump_sets,
2151 };
2152 return netlink_dump_start(nlsk, skb, nlh, &c);
2153 }
2154
2155 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2156 if (IS_ERR(set))
2157 return PTR_ERR(set);
2158
2159 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2160 if (skb2 == NULL)
2161 return -ENOMEM;
2162
2163 err = nf_tables_fill_set(skb2, &ctx, set, NFT_MSG_NEWSET, 0);
2164 if (err < 0)
2165 goto err;
2166
2167 return nlmsg_unicast(nlsk, skb2, NETLINK_CB(skb).portid);
2168
2169err:
2170 kfree_skb(skb2);
2171 return err;
2172}
2173
2174static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2175 const struct nlmsghdr *nlh,
2176 const struct nlattr * const nla[])
2177{
2178 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2179 const struct nft_set_ops *ops;
2180 const struct nft_af_info *afi;
2181 struct net *net = sock_net(skb->sk);
2182 struct nft_table *table;
2183 struct nft_set *set;
2184 struct nft_ctx ctx;
2185 char name[IFNAMSIZ];
2186 unsigned int size;
2187 bool create;
2188 u32 ktype, klen, dlen, dtype, flags;
2189 int err;
2190
2191 if (nla[NFTA_SET_TABLE] == NULL ||
2192 nla[NFTA_SET_NAME] == NULL ||
2193 nla[NFTA_SET_KEY_LEN] == NULL)
2194 return -EINVAL;
2195
2196 ktype = NFT_DATA_VALUE;
2197 if (nla[NFTA_SET_KEY_TYPE] != NULL) {
2198 ktype = ntohl(nla_get_be32(nla[NFTA_SET_KEY_TYPE]));
2199 if ((ktype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK)
2200 return -EINVAL;
2201 }
2202
2203 klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2204 if (klen == 0 || klen > FIELD_SIZEOF(struct nft_data, data))
2205 return -EINVAL;
2206
2207 flags = 0;
2208 if (nla[NFTA_SET_FLAGS] != NULL) {
2209 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2210 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2211 NFT_SET_INTERVAL | NFT_SET_MAP))
2212 return -EINVAL;
2213 }
2214
2215 dtype = 0;
2216 dlen = 0;
2217 if (nla[NFTA_SET_DATA_TYPE] != NULL) {
2218 if (!(flags & NFT_SET_MAP))
2219 return -EINVAL;
2220
2221 dtype = ntohl(nla_get_be32(nla[NFTA_SET_DATA_TYPE]));
2222 if ((dtype & NFT_DATA_RESERVED_MASK) == NFT_DATA_RESERVED_MASK &&
2223 dtype != NFT_DATA_VERDICT)
2224 return -EINVAL;
2225
2226 if (dtype != NFT_DATA_VERDICT) {
2227 if (nla[NFTA_SET_DATA_LEN] == NULL)
2228 return -EINVAL;
2229 dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2230 if (dlen == 0 ||
2231 dlen > FIELD_SIZEOF(struct nft_data, data))
2232 return -EINVAL;
2233 } else
2234 dlen = sizeof(struct nft_data);
2235 } else if (flags & NFT_SET_MAP)
2236 return -EINVAL;
2237
2238 create = nlh->nlmsg_flags & NLM_F_CREATE ? true : false;
2239
2240 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, create);
2241 if (IS_ERR(afi))
2242 return PTR_ERR(afi);
2243
2244 table = nf_tables_table_lookup(afi, nla[NFTA_SET_TABLE]);
2245 if (IS_ERR(table))
2246 return PTR_ERR(table);
2247
2248 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
2249
2250 set = nf_tables_set_lookup(table, nla[NFTA_SET_NAME]);
2251 if (IS_ERR(set)) {
2252 if (PTR_ERR(set) != -ENOENT)
2253 return PTR_ERR(set);
2254 set = NULL;
2255 }
2256
2257 if (set != NULL) {
2258 if (nlh->nlmsg_flags & NLM_F_EXCL)
2259 return -EEXIST;
2260 if (nlh->nlmsg_flags & NLM_F_REPLACE)
2261 return -EOPNOTSUPP;
2262 return 0;
2263 }
2264
2265 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
2266 return -ENOENT;
2267
2268 ops = nft_select_set_ops(nla);
2269 if (IS_ERR(ops))
2270 return PTR_ERR(ops);
2271
2272 size = 0;
2273 if (ops->privsize != NULL)
2274 size = ops->privsize(nla);
2275
2276 err = -ENOMEM;
2277 set = kzalloc(sizeof(*set) + size, GFP_KERNEL);
2278 if (set == NULL)
2279 goto err1;
2280
2281 nla_strlcpy(name, nla[NFTA_SET_NAME], sizeof(set->name));
2282 err = nf_tables_set_alloc_name(&ctx, set, name);
2283 if (err < 0)
2284 goto err2;
2285
2286 INIT_LIST_HEAD(&set->bindings);
2287 set->ops = ops;
2288 set->ktype = ktype;
2289 set->klen = klen;
2290 set->dtype = dtype;
2291 set->dlen = dlen;
2292 set->flags = flags;
2293
2294 err = ops->init(set, nla);
2295 if (err < 0)
2296 goto err2;
2297
2298 list_add_tail(&set->list, &table->sets);
2299 nf_tables_set_notify(&ctx, set, NFT_MSG_NEWSET);
2300 return 0;
2301
2302err2:
2303 kfree(set);
2304err1:
2305 module_put(ops->owner);
2306 return err;
2307}
2308
2309static void nf_tables_set_destroy(const struct nft_ctx *ctx, struct nft_set *set)
2310{
2311 list_del(&set->list);
2312 if (!(set->flags & NFT_SET_ANONYMOUS))
2313 nf_tables_set_notify(ctx, set, NFT_MSG_DELSET);
2314
2315 set->ops->destroy(set);
2316 module_put(set->ops->owner);
2317 kfree(set);
2318}
2319
2320static int nf_tables_delset(struct sock *nlsk, struct sk_buff *skb,
2321 const struct nlmsghdr *nlh,
2322 const struct nlattr * const nla[])
2323{
2324 struct nft_set *set;
2325 struct nft_ctx ctx;
2326 int err;
2327
2328 if (nla[NFTA_SET_TABLE] == NULL)
2329 return -EINVAL;
2330
2331 err = nft_ctx_init_from_setattr(&ctx, skb, nlh, nla);
2332 if (err < 0)
2333 return err;
2334
2335 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_NAME]);
2336 if (IS_ERR(set))
2337 return PTR_ERR(set);
2338 if (!list_empty(&set->bindings))
2339 return -EBUSY;
2340
2341 nf_tables_set_destroy(&ctx, set);
2342 return 0;
2343}
2344
2345static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2346 const struct nft_set *set,
2347 const struct nft_set_iter *iter,
2348 const struct nft_set_elem *elem)
2349{
2350 enum nft_registers dreg;
2351
2352 dreg = nft_type_to_reg(set->dtype);
2353 return nft_validate_data_load(ctx, dreg, &elem->data, set->dtype);
2354}
2355
2356int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2357 struct nft_set_binding *binding)
2358{
2359 struct nft_set_binding *i;
2360 struct nft_set_iter iter;
2361
2362 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2363 return -EBUSY;
2364
2365 if (set->flags & NFT_SET_MAP) {
2366 /* If the set is already bound to the same chain all
2367 * jumps are already validated for that chain.
2368 */
2369 list_for_each_entry(i, &set->bindings, list) {
2370 if (i->chain == binding->chain)
2371 goto bind;
2372 }
2373
2374 iter.skip = 0;
2375 iter.count = 0;
2376 iter.err = 0;
2377 iter.fn = nf_tables_bind_check_setelem;
2378
2379 set->ops->walk(ctx, set, &iter);
2380 if (iter.err < 0) {
2381 /* Destroy anonymous sets if binding fails */
2382 if (set->flags & NFT_SET_ANONYMOUS)
2383 nf_tables_set_destroy(ctx, set);
2384
2385 return iter.err;
2386 }
2387 }
2388bind:
2389 binding->chain = ctx->chain;
2390 list_add_tail(&binding->list, &set->bindings);
2391 return 0;
2392}
2393
2394void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2395 struct nft_set_binding *binding)
2396{
2397 list_del(&binding->list);
2398
2399 if (list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2400 nf_tables_set_destroy(ctx, set);
2401}
2402
2403/*
2404 * Set elements
2405 */
2406
2407static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2408 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2409 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2410 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2411};
2412
2413static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
2414 [NFTA_SET_ELEM_LIST_TABLE] = { .type = NLA_STRING },
2415 [NFTA_SET_ELEM_LIST_SET] = { .type = NLA_STRING },
2416 [NFTA_SET_ELEM_LIST_ELEMENTS] = { .type = NLA_NESTED },
2417};
2418
2419static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,
2420 const struct sk_buff *skb,
2421 const struct nlmsghdr *nlh,
2422 const struct nlattr * const nla[])
2423{
2424 const struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2425 const struct nft_af_info *afi;
2426 const struct nft_table *table;
2427 struct net *net = sock_net(skb->sk);
2428
2429 afi = nf_tables_afinfo_lookup(net, nfmsg->nfgen_family, false);
2430 if (IS_ERR(afi))
2431 return PTR_ERR(afi);
2432
2433 table = nf_tables_table_lookup(afi, nla[NFTA_SET_ELEM_LIST_TABLE]);
2434 if (IS_ERR(table))
2435 return PTR_ERR(table);
2436
2437 nft_ctx_init(ctx, skb, nlh, afi, table, NULL, nla);
2438 return 0;
2439}
2440
2441static int nf_tables_fill_setelem(struct sk_buff *skb,
2442 const struct nft_set *set,
2443 const struct nft_set_elem *elem)
2444{
2445 unsigned char *b = skb_tail_pointer(skb);
2446 struct nlattr *nest;
2447
2448 nest = nla_nest_start(skb, NFTA_LIST_ELEM);
2449 if (nest == NULL)
2450 goto nla_put_failure;
2451
2452 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE,
2453 set->klen) < 0)
2454 goto nla_put_failure;
2455
2456 if (set->flags & NFT_SET_MAP &&
2457 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) &&
2458 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2459 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2460 set->dlen) < 0)
2461 goto nla_put_failure;
2462
2463 if (elem->flags != 0)
2464 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags)))
2465 goto nla_put_failure;
2466
2467 nla_nest_end(skb, nest);
2468 return 0;
2469
2470nla_put_failure:
2471 nlmsg_trim(skb, b);
2472 return -EMSGSIZE;
2473}
2474
2475struct nft_set_dump_args {
2476 const struct netlink_callback *cb;
2477 struct nft_set_iter iter;
2478 struct sk_buff *skb;
2479};
2480
2481static int nf_tables_dump_setelem(const struct nft_ctx *ctx,
2482 const struct nft_set *set,
2483 const struct nft_set_iter *iter,
2484 const struct nft_set_elem *elem)
2485{
2486 struct nft_set_dump_args *args;
2487
2488 args = container_of(iter, struct nft_set_dump_args, iter);
2489 return nf_tables_fill_setelem(args->skb, set, elem);
2490}
2491
2492static int nf_tables_dump_set(struct sk_buff *skb, struct netlink_callback *cb)
2493{
2494 const struct nft_set *set;
2495 struct nft_set_dump_args args;
2496 struct nft_ctx ctx;
2497 struct nlattr *nla[NFTA_SET_ELEM_LIST_MAX + 1];
2498 struct nfgenmsg *nfmsg;
2499 struct nlmsghdr *nlh;
2500 struct nlattr *nest;
2501 u32 portid, seq;
2502 int event, err;
2503
2504 nfmsg = nlmsg_data(cb->nlh);
2505 err = nlmsg_parse(cb->nlh, sizeof(*nfmsg), nla, NFTA_SET_ELEM_LIST_MAX,
2506 nft_set_elem_list_policy);
2507 if (err < 0)
2508 return err;
2509
2510 err = nft_ctx_init_from_elemattr(&ctx, cb->skb, cb->nlh, (void *)nla);
2511 if (err < 0)
2512 return err;
2513
2514 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2515 if (IS_ERR(set))
2516 return PTR_ERR(set);
2517
2518 event = NFT_MSG_NEWSETELEM;
2519 event |= NFNL_SUBSYS_NFTABLES << 8;
2520 portid = NETLINK_CB(cb->skb).portid;
2521 seq = cb->nlh->nlmsg_seq;
2522
2523 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct nfgenmsg),
2524 NLM_F_MULTI);
2525 if (nlh == NULL)
2526 goto nla_put_failure;
2527
2528 nfmsg = nlmsg_data(nlh);
2529 nfmsg->nfgen_family = NFPROTO_UNSPEC;
2530 nfmsg->version = NFNETLINK_V0;
2531 nfmsg->res_id = 0;
2532
2533 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_TABLE, ctx.table->name))
2534 goto nla_put_failure;
2535 if (nla_put_string(skb, NFTA_SET_ELEM_LIST_SET, set->name))
2536 goto nla_put_failure;
2537
2538 nest = nla_nest_start(skb, NFTA_SET_ELEM_LIST_ELEMENTS);
2539 if (nest == NULL)
2540 goto nla_put_failure;
2541
2542 args.cb = cb;
2543 args.skb = skb;
2544 args.iter.skip = cb->args[0];
2545 args.iter.count = 0;
2546 args.iter.err = 0;
2547 args.iter.fn = nf_tables_dump_setelem;
2548 set->ops->walk(&ctx, set, &args.iter);
2549
2550 nla_nest_end(skb, nest);
2551 nlmsg_end(skb, nlh);
2552
2553 if (args.iter.err && args.iter.err != -EMSGSIZE)
2554 return args.iter.err;
2555 if (args.iter.count == cb->args[0])
2556 return 0;
2557
2558 cb->args[0] = args.iter.count;
2559 return skb->len;
2560
2561nla_put_failure:
2562 return -ENOSPC;
2563}
2564
2565static int nf_tables_getsetelem(struct sock *nlsk, struct sk_buff *skb,
2566 const struct nlmsghdr *nlh,
2567 const struct nlattr * const nla[])
2568{
2569 const struct nft_set *set;
2570 struct nft_ctx ctx;
2571 int err;
2572
2573 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2574 if (err < 0)
2575 return err;
2576
2577 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2578 if (IS_ERR(set))
2579 return PTR_ERR(set);
2580
2581 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2582 struct netlink_dump_control c = {
2583 .dump = nf_tables_dump_set,
2584 };
2585 return netlink_dump_start(nlsk, skb, nlh, &c);
2586 }
2587 return -EOPNOTSUPP;
2588}
2589
2590static int nft_add_set_elem(const struct nft_ctx *ctx, struct nft_set *set,
2591 const struct nlattr *attr)
2592{
2593 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2594 struct nft_data_desc d1, d2;
2595 struct nft_set_elem elem;
2596 struct nft_set_binding *binding;
2597 enum nft_registers dreg;
2598 int err;
2599
2600 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2601 nft_set_elem_policy);
2602 if (err < 0)
2603 return err;
2604
2605 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2606 return -EINVAL;
2607
2608 elem.flags = 0;
2609 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
2610 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
2611 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END)
2612 return -EINVAL;
2613 }
2614
2615 if (set->flags & NFT_SET_MAP) {
2616 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
2617 !(elem.flags & NFT_SET_ELEM_INTERVAL_END))
2618 return -EINVAL;
2619 } else {
2620 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2621 return -EINVAL;
2622 }
2623
2624 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]);
2625 if (err < 0)
2626 goto err1;
2627 err = -EINVAL;
2628 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
2629 goto err2;
2630
2631 err = -EEXIST;
2632 if (set->ops->get(set, &elem) == 0)
2633 goto err2;
2634
2635 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
2636 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]);
2637 if (err < 0)
2638 goto err2;
2639
2640 err = -EINVAL;
2641 if (set->dtype != NFT_DATA_VERDICT && d2.len != set->dlen)
2642 goto err3;
2643
2644 dreg = nft_type_to_reg(set->dtype);
2645 list_for_each_entry(binding, &set->bindings, list) {
2646 struct nft_ctx bind_ctx = {
2647 .afi = ctx->afi,
2648 .table = ctx->table,
2649 .chain = binding->chain,
2650 };
2651
2652 err = nft_validate_data_load(&bind_ctx, dreg,
2653 &elem.data, d2.type);
2654 if (err < 0)
2655 goto err3;
2656 }
2657 }
2658
2659 err = set->ops->insert(set, &elem);
2660 if (err < 0)
2661 goto err3;
2662
2663 return 0;
2664
2665err3:
2666 if (nla[NFTA_SET_ELEM_DATA] != NULL)
2667 nft_data_uninit(&elem.data, d2.type);
2668err2:
2669 nft_data_uninit(&elem.key, d1.type);
2670err1:
2671 return err;
2672}
2673
2674static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
2675 const struct nlmsghdr *nlh,
2676 const struct nlattr * const nla[])
2677{
2678 const struct nlattr *attr;
2679 struct nft_set *set;
2680 struct nft_ctx ctx;
2681 int rem, err;
2682
2683 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2684 if (err < 0)
2685 return err;
2686
2687 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2688 if (IS_ERR(set))
2689 return PTR_ERR(set);
2690 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2691 return -EBUSY;
2692
2693 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2694 err = nft_add_set_elem(&ctx, set, attr);
2695 if (err < 0)
2696 return err;
2697 }
2698 return 0;
2699}
2700
2701static int nft_del_setelem(const struct nft_ctx *ctx, struct nft_set *set,
2702 const struct nlattr *attr)
2703{
2704 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
2705 struct nft_data_desc desc;
2706 struct nft_set_elem elem;
2707 int err;
2708
2709 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
2710 nft_set_elem_policy);
2711 if (err < 0)
2712 goto err1;
2713
2714 err = -EINVAL;
2715 if (nla[NFTA_SET_ELEM_KEY] == NULL)
2716 goto err1;
2717
2718 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]);
2719 if (err < 0)
2720 goto err1;
2721
2722 err = -EINVAL;
2723 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
2724 goto err2;
2725
2726 err = set->ops->get(set, &elem);
2727 if (err < 0)
2728 goto err2;
2729
2730 set->ops->remove(set, &elem);
2731
2732 nft_data_uninit(&elem.key, NFT_DATA_VALUE);
2733 if (set->flags & NFT_SET_MAP)
2734 nft_data_uninit(&elem.data, set->dtype);
2735
2736err2:
2737 nft_data_uninit(&elem.key, desc.type);
2738err1:
2739 return err;
2740}
2741
2742static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
2743 const struct nlmsghdr *nlh,
2744 const struct nlattr * const nla[])
2745{
2746 const struct nlattr *attr;
2747 struct nft_set *set;
2748 struct nft_ctx ctx;
2749 int rem, err;
2750
2751 err = nft_ctx_init_from_elemattr(&ctx, skb, nlh, nla);
2752 if (err < 0)
2753 return err;
2754
2755 set = nf_tables_set_lookup(ctx.table, nla[NFTA_SET_ELEM_LIST_SET]);
2756 if (IS_ERR(set))
2757 return PTR_ERR(set);
2758 if (!list_empty(&set->bindings) && set->flags & NFT_SET_CONSTANT)
2759 return -EBUSY;
2760
2761 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
2762 err = nft_del_setelem(&ctx, set, attr);
2763 if (err < 0)
2764 return err;
2765 }
2766 return 0;
2767}
2768
2769static const struct nfnl_callback nf_tables_cb[NFT_MSG_MAX] = {
2770 [NFT_MSG_NEWTABLE] = {
2771 .call = nf_tables_newtable,
2772 .attr_count = NFTA_TABLE_MAX,
2773 .policy = nft_table_policy,
2774 },
2775 [NFT_MSG_GETTABLE] = {
2776 .call = nf_tables_gettable,
2777 .attr_count = NFTA_TABLE_MAX,
2778 .policy = nft_table_policy,
2779 },
2780 [NFT_MSG_DELTABLE] = {
2781 .call = nf_tables_deltable,
2782 .attr_count = NFTA_TABLE_MAX,
2783 .policy = nft_table_policy,
2784 },
2785 [NFT_MSG_NEWCHAIN] = {
2786 .call = nf_tables_newchain,
2787 .attr_count = NFTA_CHAIN_MAX,
2788 .policy = nft_chain_policy,
2789 },
2790 [NFT_MSG_GETCHAIN] = {
2791 .call = nf_tables_getchain,
2792 .attr_count = NFTA_CHAIN_MAX,
2793 .policy = nft_chain_policy,
2794 },
2795 [NFT_MSG_DELCHAIN] = {
2796 .call = nf_tables_delchain,
2797 .attr_count = NFTA_CHAIN_MAX,
2798 .policy = nft_chain_policy,
2799 },
2800 [NFT_MSG_NEWRULE] = {
2801 .call_batch = nf_tables_newrule,
2802 .attr_count = NFTA_RULE_MAX,
2803 .policy = nft_rule_policy,
2804 },
2805 [NFT_MSG_GETRULE] = {
2806 .call = nf_tables_getrule,
2807 .attr_count = NFTA_RULE_MAX,
2808 .policy = nft_rule_policy,
2809 },
2810 [NFT_MSG_DELRULE] = {
2811 .call_batch = nf_tables_delrule,
2812 .attr_count = NFTA_RULE_MAX,
2813 .policy = nft_rule_policy,
2814 },
2815 [NFT_MSG_NEWSET] = {
2816 .call = nf_tables_newset,
2817 .attr_count = NFTA_SET_MAX,
2818 .policy = nft_set_policy,
2819 },
2820 [NFT_MSG_GETSET] = {
2821 .call = nf_tables_getset,
2822 .attr_count = NFTA_SET_MAX,
2823 .policy = nft_set_policy,
2824 },
2825 [NFT_MSG_DELSET] = {
2826 .call = nf_tables_delset,
2827 .attr_count = NFTA_SET_MAX,
2828 .policy = nft_set_policy,
2829 },
2830 [NFT_MSG_NEWSETELEM] = {
2831 .call = nf_tables_newsetelem,
2832 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2833 .policy = nft_set_elem_list_policy,
2834 },
2835 [NFT_MSG_GETSETELEM] = {
2836 .call = nf_tables_getsetelem,
2837 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2838 .policy = nft_set_elem_list_policy,
2839 },
2840 [NFT_MSG_DELSETELEM] = {
2841 .call = nf_tables_delsetelem,
2842 .attr_count = NFTA_SET_ELEM_LIST_MAX,
2843 .policy = nft_set_elem_list_policy,
2844 },
2845};
2846
2847static const struct nfnetlink_subsystem nf_tables_subsys = {
2848 .name = "nf_tables",
2849 .subsys_id = NFNL_SUBSYS_NFTABLES,
2850 .cb_count = NFT_MSG_MAX,
2851 .cb = nf_tables_cb,
2852 .commit = nf_tables_commit,
2853 .abort = nf_tables_abort,
2854};
2855
2856/*
2857 * Loop detection - walk through the ruleset beginning at the destination chain
2858 * of a new jump until either the source chain is reached (loop) or all
2859 * reachable chains have been traversed.
2860 *
2861 * The loop check is performed whenever a new jump verdict is added to an
2862 * expression or verdict map or a verdict map is bound to a new chain.
2863 */
2864
2865static int nf_tables_check_loops(const struct nft_ctx *ctx,
2866 const struct nft_chain *chain);
2867
2868static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
2869 const struct nft_set *set,
2870 const struct nft_set_iter *iter,
2871 const struct nft_set_elem *elem)
2872{
2873 switch (elem->data.verdict) {
2874 case NFT_JUMP:
2875 case NFT_GOTO:
2876 return nf_tables_check_loops(ctx, elem->data.chain);
2877 default:
2878 return 0;
2879 }
2880}
2881
2882static int nf_tables_check_loops(const struct nft_ctx *ctx,
2883 const struct nft_chain *chain)
2884{
2885 const struct nft_rule *rule;
2886 const struct nft_expr *expr, *last;
2887 const struct nft_set *set;
2888 struct nft_set_binding *binding;
2889 struct nft_set_iter iter;
2890
2891 if (ctx->chain == chain)
2892 return -ELOOP;
2893
2894 list_for_each_entry(rule, &chain->rules, list) {
2895 nft_rule_for_each_expr(expr, last, rule) {
2896 const struct nft_data *data = NULL;
2897 int err;
2898
2899 if (!expr->ops->validate)
2900 continue;
2901
2902 err = expr->ops->validate(ctx, expr, &data);
2903 if (err < 0)
2904 return err;
2905
2906 if (data == NULL)
2907 continue;
2908
2909 switch (data->verdict) {
2910 case NFT_JUMP:
2911 case NFT_GOTO:
2912 err = nf_tables_check_loops(ctx, data->chain);
2913 if (err < 0)
2914 return err;
2915 default:
2916 break;
2917 }
2918 }
2919 }
2920
2921 list_for_each_entry(set, &ctx->table->sets, list) {
2922 if (!(set->flags & NFT_SET_MAP) ||
2923 set->dtype != NFT_DATA_VERDICT)
2924 continue;
2925
2926 list_for_each_entry(binding, &set->bindings, list) {
2927 if (binding->chain != chain)
2928 continue;
2929
2930 iter.skip = 0;
2931 iter.count = 0;
2932 iter.err = 0;
2933 iter.fn = nf_tables_loop_check_setelem;
2934
2935 set->ops->walk(ctx, set, &iter);
2936 if (iter.err < 0)
2937 return iter.err;
2938 }
2939 }
2940
2941 return 0;
2942}
2943
2944/**
2945 * nft_validate_input_register - validate an expressions' input register
2946 *
2947 * @reg: the register number
2948 *
2949 * Validate that the input register is one of the general purpose
2950 * registers.
2951 */
2952int nft_validate_input_register(enum nft_registers reg)
2953{
2954 if (reg <= NFT_REG_VERDICT)
2955 return -EINVAL;
2956 if (reg > NFT_REG_MAX)
2957 return -ERANGE;
2958 return 0;
2959}
2960EXPORT_SYMBOL_GPL(nft_validate_input_register);
2961
2962/**
2963 * nft_validate_output_register - validate an expressions' output register
2964 *
2965 * @reg: the register number
2966 *
2967 * Validate that the output register is one of the general purpose
2968 * registers or the verdict register.
2969 */
2970int nft_validate_output_register(enum nft_registers reg)
2971{
2972 if (reg < NFT_REG_VERDICT)
2973 return -EINVAL;
2974 if (reg > NFT_REG_MAX)
2975 return -ERANGE;
2976 return 0;
2977}
2978EXPORT_SYMBOL_GPL(nft_validate_output_register);
2979
2980/**
2981 * nft_validate_data_load - validate an expressions' data load
2982 *
2983 * @ctx: context of the expression performing the load
2984 * @reg: the destination register number
2985 * @data: the data to load
2986 * @type: the data type
2987 *
2988 * Validate that a data load uses the appropriate data type for
2989 * the destination register. A value of NULL for the data means
2990 * that its runtime gathered data, which is always of type
2991 * NFT_DATA_VALUE.
2992 */
2993int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg,
2994 const struct nft_data *data,
2995 enum nft_data_types type)
2996{
2997 int err;
2998
2999 switch (reg) {
3000 case NFT_REG_VERDICT:
3001 if (data == NULL || type != NFT_DATA_VERDICT)
3002 return -EINVAL;
3003
3004 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) {
3005 err = nf_tables_check_loops(ctx, data->chain);
3006 if (err < 0)
3007 return err;
3008
3009 if (ctx->chain->level + 1 > data->chain->level) {
3010 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3011 return -EMLINK;
3012 data->chain->level = ctx->chain->level + 1;
3013 }
3014 }
3015
3016 return 0;
3017 default:
3018 if (data != NULL && type != NFT_DATA_VALUE)
3019 return -EINVAL;
3020 return 0;
3021 }
3022}
3023EXPORT_SYMBOL_GPL(nft_validate_data_load);
3024
3025static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3026 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
3027 [NFTA_VERDICT_CHAIN] = { .type = NLA_STRING,
3028 .len = NFT_CHAIN_MAXNAMELEN - 1 },
3029};
3030
3031static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3032 struct nft_data_desc *desc, const struct nlattr *nla)
3033{
3034 struct nlattr *tb[NFTA_VERDICT_MAX + 1];
3035 struct nft_chain *chain;
3036 int err;
3037
3038 err = nla_parse_nested(tb, NFTA_VERDICT_MAX, nla, nft_verdict_policy);
3039 if (err < 0)
3040 return err;
3041
3042 if (!tb[NFTA_VERDICT_CODE])
3043 return -EINVAL;
3044 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3045
3046 switch (data->verdict) {
3047 case NF_ACCEPT:
3048 case NF_DROP:
3049 case NF_QUEUE:
3050 case NFT_CONTINUE:
3051 case NFT_BREAK:
3052 case NFT_RETURN:
3053 desc->len = sizeof(data->verdict);
3054 break;
3055 case NFT_JUMP:
3056 case NFT_GOTO:
3057 if (!tb[NFTA_VERDICT_CHAIN])
3058 return -EINVAL;
3059 chain = nf_tables_chain_lookup(ctx->table,
3060 tb[NFTA_VERDICT_CHAIN]);
3061 if (IS_ERR(chain))
3062 return PTR_ERR(chain);
3063 if (chain->flags & NFT_BASE_CHAIN)
3064 return -EOPNOTSUPP;
3065
3066 chain->use++;
3067 data->chain = chain;
3068 desc->len = sizeof(data);
3069 break;
3070 default:
3071 return -EINVAL;
3072 }
3073
3074 desc->type = NFT_DATA_VERDICT;
3075 return 0;
3076}
3077
3078static void nft_verdict_uninit(const struct nft_data *data)
3079{
3080 switch (data->verdict) {
3081 case NFT_JUMP:
3082 case NFT_GOTO:
3083 data->chain->use--;
3084 break;
3085 }
3086}
3087
3088static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
3089{
3090 struct nlattr *nest;
3091
3092 nest = nla_nest_start(skb, NFTA_DATA_VERDICT);
3093 if (!nest)
3094 goto nla_put_failure;
3095
3096 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict)))
3097 goto nla_put_failure;
3098
3099 switch (data->verdict) {
3100 case NFT_JUMP:
3101 case NFT_GOTO:
3102 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name))
3103 goto nla_put_failure;
3104 }
3105 nla_nest_end(skb, nest);
3106 return 0;
3107
3108nla_put_failure:
3109 return -1;
3110}
3111
3112static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
3113 struct nft_data_desc *desc, const struct nlattr *nla)
3114{
3115 unsigned int len;
3116
3117 len = nla_len(nla);
3118 if (len == 0)
3119 return -EINVAL;
3120 if (len > sizeof(data->data))
3121 return -EOVERFLOW;
3122
3123 nla_memcpy(data->data, nla, sizeof(data->data));
3124 desc->type = NFT_DATA_VALUE;
3125 desc->len = len;
3126 return 0;
3127}
3128
3129static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
3130 unsigned int len)
3131{
3132 return nla_put(skb, NFTA_DATA_VALUE, len, data->data);
3133}
3134
3135static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
3136 [NFTA_DATA_VALUE] = { .type = NLA_BINARY,
3137 .len = FIELD_SIZEOF(struct nft_data, data) },
3138 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
3139};
3140
3141/**
3142 * nft_data_init - parse nf_tables data netlink attributes
3143 *
3144 * @ctx: context of the expression using the data
3145 * @data: destination struct nft_data
3146 * @desc: data description
3147 * @nla: netlink attribute containing data
3148 *
3149 * Parse the netlink data attributes and initialize a struct nft_data.
3150 * The type and length of data are returned in the data description.
3151 *
3152 * The caller can indicate that it only wants to accept data of type
3153 * NFT_DATA_VALUE by passing NULL for the ctx argument.
3154 */
3155int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
3156 struct nft_data_desc *desc, const struct nlattr *nla)
3157{
3158 struct nlattr *tb[NFTA_DATA_MAX + 1];
3159 int err;
3160
3161 err = nla_parse_nested(tb, NFTA_DATA_MAX, nla, nft_data_policy);
3162 if (err < 0)
3163 return err;
3164
3165 if (tb[NFTA_DATA_VALUE])
3166 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]);
3167 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
3168 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
3169 return -EINVAL;
3170}
3171EXPORT_SYMBOL_GPL(nft_data_init);
3172
3173/**
3174 * nft_data_uninit - release a nft_data item
3175 *
3176 * @data: struct nft_data to release
3177 * @type: type of data
3178 *
3179 * Release a nft_data item. NFT_DATA_VALUE types can be silently discarded,
3180 * all others need to be released by calling this function.
3181 */
3182void nft_data_uninit(const struct nft_data *data, enum nft_data_types type)
3183{
3184 switch (type) {
3185 case NFT_DATA_VALUE:
3186 return;
3187 case NFT_DATA_VERDICT:
3188 return nft_verdict_uninit(data);
3189 default:
3190 WARN_ON(1);
3191 }
3192}
3193EXPORT_SYMBOL_GPL(nft_data_uninit);
3194
3195int nft_data_dump(struct sk_buff *skb, int attr, const struct nft_data *data,
3196 enum nft_data_types type, unsigned int len)
3197{
3198 struct nlattr *nest;
3199 int err;
3200
3201 nest = nla_nest_start(skb, attr);
3202 if (nest == NULL)
3203 return -1;
3204
3205 switch (type) {
3206 case NFT_DATA_VALUE:
3207 err = nft_value_dump(skb, data, len);
3208 break;
3209 case NFT_DATA_VERDICT:
3210 err = nft_verdict_dump(skb, data);
3211 break;
3212 default:
3213 err = -EINVAL;
3214 WARN_ON(1);
3215 }
3216
3217 nla_nest_end(skb, nest);
3218 return err;
3219}
3220EXPORT_SYMBOL_GPL(nft_data_dump);
3221
3222static int nf_tables_init_net(struct net *net)
3223{
3224 INIT_LIST_HEAD(&net->nft.af_info);
3225 INIT_LIST_HEAD(&net->nft.commit_list);
3226 return 0;
3227}
3228
3229static struct pernet_operations nf_tables_net_ops = {
3230 .init = nf_tables_init_net,
3231};
3232
3233static int __init nf_tables_module_init(void)
3234{
3235 int err;
3236
3237 info = kmalloc(sizeof(struct nft_expr_info) * NFT_RULE_MAXEXPRS,
3238 GFP_KERNEL);
3239 if (info == NULL) {
3240 err = -ENOMEM;
3241 goto err1;
3242 }
3243
3244 err = nf_tables_core_module_init();
3245 if (err < 0)
3246 goto err2;
3247
3248 err = nfnetlink_subsys_register(&nf_tables_subsys);
3249 if (err < 0)
3250 goto err3;
3251
3252 pr_info("nf_tables: (c) 2007-2009 Patrick McHardy <kaber@trash.net>\n");
3253 return register_pernet_subsys(&nf_tables_net_ops);
3254err3:
3255 nf_tables_core_module_exit();
3256err2:
3257 kfree(info);
3258err1:
3259 return err;
3260}
3261
3262static void __exit nf_tables_module_exit(void)
3263{
3264 unregister_pernet_subsys(&nf_tables_net_ops);
3265 nfnetlink_subsys_unregister(&nf_tables_subsys);
3266 nf_tables_core_module_exit();
3267 kfree(info);
3268}
3269
3270module_init(nf_tables_module_init);
3271module_exit(nf_tables_module_exit);
3272
3273MODULE_LICENSE("GPL");
3274MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
3275MODULE_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..3c4b69e5fe17 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 }
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..a82667c64729
--- /dev/null
+++ b/net/netfilter/nft_compat.c
@@ -0,0 +1,768 @@
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 u8 nft_parse_compat(const struct nlattr *attr, 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 return ntohl(nla_get_be32(tb[NFTA_RULE_COMPAT_PROTO]));
152}
153
154static int
155nft_target_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
156 const struct nlattr * const tb[])
157{
158 void *info = nft_expr_priv(expr);
159 struct xt_target *target = expr->ops->data;
160 struct xt_tgchk_param par;
161 size_t size = XT_ALIGN(nla_len(tb[NFTA_TARGET_INFO]));
162 u8 proto = 0;
163 bool inv = false;
164 union nft_entry e = {};
165 int ret;
166
167 target_compat_from_user(target, nla_data(tb[NFTA_TARGET_INFO]), info);
168
169 if (ctx->nla[NFTA_RULE_COMPAT])
170 proto = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &inv);
171
172 nft_target_set_tgchk_param(&par, ctx, target, info, &e, proto, inv);
173
174 ret = xt_check_target(&par, size, proto, inv);
175 if (ret < 0)
176 goto err;
177
178 /* The standard target cannot be used */
179 if (target->target == NULL) {
180 ret = -EINVAL;
181 goto err;
182 }
183
184 return 0;
185err:
186 module_put(target->me);
187 return ret;
188}
189
190static void
191nft_target_destroy(const struct nft_expr *expr)
192{
193 struct xt_target *target = expr->ops->data;
194
195 module_put(target->me);
196}
197
198static int
199target_dump_info(struct sk_buff *skb, const struct xt_target *t, const void *in)
200{
201 int ret;
202
203#ifdef CONFIG_COMPAT
204 if (t->compat_to_user) {
205 mm_segment_t old_fs;
206 void *out;
207
208 out = kmalloc(XT_ALIGN(t->targetsize), GFP_ATOMIC);
209 if (out == NULL)
210 return -ENOMEM;
211
212 /* We want to reuse existing compat_to_user */
213 old_fs = get_fs();
214 set_fs(KERNEL_DS);
215 t->compat_to_user(out, in);
216 set_fs(old_fs);
217 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), out);
218 kfree(out);
219 } else
220#endif
221 ret = nla_put(skb, NFTA_TARGET_INFO, XT_ALIGN(t->targetsize), in);
222
223 return ret;
224}
225
226static int nft_target_dump(struct sk_buff *skb, const struct nft_expr *expr)
227{
228 const struct xt_target *target = expr->ops->data;
229 void *info = nft_expr_priv(expr);
230
231 if (nla_put_string(skb, NFTA_TARGET_NAME, target->name) ||
232 nla_put_be32(skb, NFTA_TARGET_REV, htonl(target->revision)) ||
233 target_dump_info(skb, target, info))
234 goto nla_put_failure;
235
236 return 0;
237
238nla_put_failure:
239 return -1;
240}
241
242static int nft_target_validate(const struct nft_ctx *ctx,
243 const struct nft_expr *expr,
244 const struct nft_data **data)
245{
246 struct xt_target *target = expr->ops->data;
247 unsigned int hook_mask = 0;
248
249 if (ctx->chain->flags & NFT_BASE_CHAIN) {
250 const struct nft_base_chain *basechain =
251 nft_base_chain(ctx->chain);
252 const struct nf_hook_ops *ops = &basechain->ops;
253
254 hook_mask = 1 << ops->hooknum;
255 if (hook_mask & target->hooks)
256 return 0;
257
258 /* This target is being called from an invalid chain */
259 return -EINVAL;
260 }
261 return 0;
262}
263
264static void nft_match_eval(const struct nft_expr *expr,
265 struct nft_data data[NFT_REG_MAX + 1],
266 const struct nft_pktinfo *pkt)
267{
268 void *info = nft_expr_priv(expr);
269 struct xt_match *match = expr->ops->data;
270 struct sk_buff *skb = pkt->skb;
271 bool ret;
272
273 nft_compat_set_par((struct xt_action_param *)&pkt->xt, match, info);
274
275 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
276
277 if (pkt->xt.hotdrop) {
278 data[NFT_REG_VERDICT].verdict = NF_DROP;
279 return;
280 }
281
282 switch(ret) {
283 case true:
284 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE;
285 break;
286 case false:
287 data[NFT_REG_VERDICT].verdict = NFT_BREAK;
288 break;
289 }
290}
291
292static const struct nla_policy nft_match_policy[NFTA_MATCH_MAX + 1] = {
293 [NFTA_MATCH_NAME] = { .type = NLA_NUL_STRING },
294 [NFTA_MATCH_REV] = { .type = NLA_U32 },
295 [NFTA_MATCH_INFO] = { .type = NLA_BINARY },
296};
297
298/* struct xt_mtchk_param and xt_tgchk_param look very similar */
299static void
300nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
301 struct xt_match *match, void *info,
302 union nft_entry *entry, u8 proto, bool inv)
303{
304 par->net = &init_net;
305 par->table = ctx->table->name;
306 switch (ctx->afi->family) {
307 case AF_INET:
308 entry->e4.ip.proto = proto;
309 entry->e4.ip.invflags = inv ? IPT_INV_PROTO : 0;
310 break;
311 case AF_INET6:
312 entry->e6.ipv6.proto = proto;
313 entry->e6.ipv6.invflags = inv ? IP6T_INV_PROTO : 0;
314 break;
315 }
316 par->entryinfo = entry;
317 par->match = match;
318 par->matchinfo = info;
319 if (ctx->chain->flags & NFT_BASE_CHAIN) {
320 const struct nft_base_chain *basechain =
321 nft_base_chain(ctx->chain);
322 const struct nf_hook_ops *ops = &basechain->ops;
323
324 par->hook_mask = 1 << ops->hooknum;
325 }
326 par->family = ctx->afi->family;
327}
328
329static void match_compat_from_user(struct xt_match *m, void *in, void *out)
330{
331#ifdef CONFIG_COMPAT
332 if (m->compat_from_user) {
333 int pad;
334
335 m->compat_from_user(out, in);
336 pad = XT_ALIGN(m->matchsize) - m->matchsize;
337 if (pad > 0)
338 memset(out + m->matchsize, 0, pad);
339 } else
340#endif
341 memcpy(out, in, XT_ALIGN(m->matchsize));
342}
343
344static int
345nft_match_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
346 const struct nlattr * const tb[])
347{
348 void *info = nft_expr_priv(expr);
349 struct xt_match *match = expr->ops->data;
350 struct xt_mtchk_param par;
351 size_t size = XT_ALIGN(nla_len(tb[NFTA_MATCH_INFO]));
352 u8 proto = 0;
353 bool inv = false;
354 union nft_entry e = {};
355 int ret;
356
357 match_compat_from_user(match, nla_data(tb[NFTA_MATCH_INFO]), info);
358
359 if (ctx->nla[NFTA_RULE_COMPAT])
360 proto = nft_parse_compat(ctx->nla[NFTA_RULE_COMPAT], &inv);
361
362 nft_match_set_mtchk_param(&par, ctx, match, info, &e, proto, inv);
363
364 ret = xt_check_match(&par, size, proto, inv);
365 if (ret < 0)
366 goto err;
367
368 return 0;
369err:
370 module_put(match->me);
371 return ret;
372}
373
374static void
375nft_match_destroy(const struct nft_expr *expr)
376{
377 struct xt_match *match = expr->ops->data;
378
379 module_put(match->me);
380}
381
382static int
383match_dump_info(struct sk_buff *skb, const struct xt_match *m, const void *in)
384{
385 int ret;
386
387#ifdef CONFIG_COMPAT
388 if (m->compat_to_user) {
389 mm_segment_t old_fs;
390 void *out;
391
392 out = kmalloc(XT_ALIGN(m->matchsize), GFP_ATOMIC);
393 if (out == NULL)
394 return -ENOMEM;
395
396 /* We want to reuse existing compat_to_user */
397 old_fs = get_fs();
398 set_fs(KERNEL_DS);
399 m->compat_to_user(out, in);
400 set_fs(old_fs);
401 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), out);
402 kfree(out);
403 } else
404#endif
405 ret = nla_put(skb, NFTA_MATCH_INFO, XT_ALIGN(m->matchsize), in);
406
407 return ret;
408}
409
410static inline int nft_compat_match_offset(struct xt_match *match)
411{
412#ifdef CONFIG_COMPAT
413 return xt_compat_match_offset(match);
414#else
415 return 0;
416#endif
417}
418
419static int nft_match_dump(struct sk_buff *skb, const struct nft_expr *expr)
420{
421 void *info = nft_expr_priv(expr);
422 struct xt_match *match = expr->ops->data;
423
424 if (nla_put_string(skb, NFTA_MATCH_NAME, match->name) ||
425 nla_put_be32(skb, NFTA_MATCH_REV, htonl(match->revision)) ||
426 match_dump_info(skb, match, info))
427 goto nla_put_failure;
428
429 return 0;
430
431nla_put_failure:
432 return -1;
433}
434
435static int nft_match_validate(const struct nft_ctx *ctx,
436 const struct nft_expr *expr,
437 const struct nft_data **data)
438{
439 struct xt_match *match = expr->ops->data;
440 unsigned int hook_mask = 0;
441
442 if (ctx->chain->flags & NFT_BASE_CHAIN) {
443 const struct nft_base_chain *basechain =
444 nft_base_chain(ctx->chain);
445 const struct nf_hook_ops *ops = &basechain->ops;
446
447 hook_mask = 1 << ops->hooknum;
448 if (hook_mask & match->hooks)
449 return 0;
450
451 /* This match is being called from an invalid chain */
452 return -EINVAL;
453 }
454 return 0;
455}
456
457static int
458nfnl_compat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
459 int event, u16 family, const char *name,
460 int rev, int target)
461{
462 struct nlmsghdr *nlh;
463 struct nfgenmsg *nfmsg;
464 unsigned int flags = portid ? NLM_F_MULTI : 0;
465
466 event |= NFNL_SUBSYS_NFT_COMPAT << 8;
467 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
468 if (nlh == NULL)
469 goto nlmsg_failure;
470
471 nfmsg = nlmsg_data(nlh);
472 nfmsg->nfgen_family = family;
473 nfmsg->version = NFNETLINK_V0;
474 nfmsg->res_id = 0;
475
476 if (nla_put_string(skb, NFTA_COMPAT_NAME, name) ||
477 nla_put_be32(skb, NFTA_COMPAT_REV, htonl(rev)) ||
478 nla_put_be32(skb, NFTA_COMPAT_TYPE, htonl(target)))
479 goto nla_put_failure;
480
481 nlmsg_end(skb, nlh);
482 return skb->len;
483
484nlmsg_failure:
485nla_put_failure:
486 nlmsg_cancel(skb, nlh);
487 return -1;
488}
489
490static int
491nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb,
492 const struct nlmsghdr *nlh, const struct nlattr * const tb[])
493{
494 int ret = 0, target;
495 struct nfgenmsg *nfmsg;
496 const char *fmt;
497 const char *name;
498 u32 rev;
499 struct sk_buff *skb2;
500
501 if (tb[NFTA_COMPAT_NAME] == NULL ||
502 tb[NFTA_COMPAT_REV] == NULL ||
503 tb[NFTA_COMPAT_TYPE] == NULL)
504 return -EINVAL;
505
506 name = nla_data(tb[NFTA_COMPAT_NAME]);
507 rev = ntohl(nla_get_be32(tb[NFTA_COMPAT_REV]));
508 target = ntohl(nla_get_be32(tb[NFTA_COMPAT_TYPE]));
509
510 nfmsg = nlmsg_data(nlh);
511
512 switch(nfmsg->nfgen_family) {
513 case AF_INET:
514 fmt = "ipt_%s";
515 break;
516 case AF_INET6:
517 fmt = "ip6t_%s";
518 break;
519 default:
520 pr_err("nft_compat: unsupported protocol %d\n",
521 nfmsg->nfgen_family);
522 return -EINVAL;
523 }
524
525 try_then_request_module(xt_find_revision(nfmsg->nfgen_family, name,
526 rev, target, &ret),
527 fmt, name);
528
529 if (ret < 0)
530 return ret;
531
532 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
533 if (skb2 == NULL)
534 return -ENOMEM;
535
536 /* include the best revision for this extension in the message */
537 if (nfnl_compat_fill_info(skb2, NETLINK_CB(skb).portid,
538 nlh->nlmsg_seq,
539 NFNL_MSG_TYPE(nlh->nlmsg_type),
540 NFNL_MSG_COMPAT_GET,
541 nfmsg->nfgen_family,
542 name, ret, target) <= 0) {
543 kfree_skb(skb2);
544 return -ENOSPC;
545 }
546
547 ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid,
548 MSG_DONTWAIT);
549 if (ret > 0)
550 ret = 0;
551
552 return ret == -EAGAIN ? -ENOBUFS : ret;
553}
554
555static const struct nla_policy nfnl_compat_policy_get[NFTA_COMPAT_MAX+1] = {
556 [NFTA_COMPAT_NAME] = { .type = NLA_NUL_STRING,
557 .len = NFT_COMPAT_NAME_MAX-1 },
558 [NFTA_COMPAT_REV] = { .type = NLA_U32 },
559 [NFTA_COMPAT_TYPE] = { .type = NLA_U32 },
560};
561
562static const struct nfnl_callback nfnl_nft_compat_cb[NFNL_MSG_COMPAT_MAX] = {
563 [NFNL_MSG_COMPAT_GET] = { .call = nfnl_compat_get,
564 .attr_count = NFTA_COMPAT_MAX,
565 .policy = nfnl_compat_policy_get },
566};
567
568static const struct nfnetlink_subsystem nfnl_compat_subsys = {
569 .name = "nft-compat",
570 .subsys_id = NFNL_SUBSYS_NFT_COMPAT,
571 .cb_count = NFNL_MSG_COMPAT_MAX,
572 .cb = nfnl_nft_compat_cb,
573};
574
575static LIST_HEAD(nft_match_list);
576
577struct nft_xt {
578 struct list_head head;
579 struct nft_expr_ops ops;
580};
581
582static struct nft_expr_type nft_match_type;
583
584static const struct nft_expr_ops *
585nft_match_select_ops(const struct nft_ctx *ctx,
586 const struct nlattr * const tb[])
587{
588 struct nft_xt *nft_match;
589 struct xt_match *match;
590 char *mt_name;
591 __u32 rev, family;
592
593 if (tb[NFTA_MATCH_NAME] == NULL ||
594 tb[NFTA_MATCH_REV] == NULL ||
595 tb[NFTA_MATCH_INFO] == NULL)
596 return ERR_PTR(-EINVAL);
597
598 mt_name = nla_data(tb[NFTA_MATCH_NAME]);
599 rev = ntohl(nla_get_be32(tb[NFTA_MATCH_REV]));
600 family = ctx->afi->family;
601
602 /* Re-use the existing match if it's already loaded. */
603 list_for_each_entry(nft_match, &nft_match_list, head) {
604 struct xt_match *match = nft_match->ops.data;
605
606 if (strcmp(match->name, mt_name) == 0 &&
607 match->revision == rev && match->family == family)
608 return &nft_match->ops;
609 }
610
611 match = xt_request_find_match(family, mt_name, rev);
612 if (IS_ERR(match))
613 return ERR_PTR(-ENOENT);
614
615 /* This is the first time we use this match, allocate operations */
616 nft_match = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
617 if (nft_match == NULL)
618 return ERR_PTR(-ENOMEM);
619
620 nft_match->ops.type = &nft_match_type;
621 nft_match->ops.size = NFT_EXPR_SIZE(XT_ALIGN(match->matchsize) +
622 nft_compat_match_offset(match));
623 nft_match->ops.eval = nft_match_eval;
624 nft_match->ops.init = nft_match_init;
625 nft_match->ops.destroy = nft_match_destroy;
626 nft_match->ops.dump = nft_match_dump;
627 nft_match->ops.validate = nft_match_validate;
628 nft_match->ops.data = match;
629
630 list_add(&nft_match->head, &nft_match_list);
631
632 return &nft_match->ops;
633}
634
635static void nft_match_release(void)
636{
637 struct nft_xt *nft_match, *tmp;
638
639 list_for_each_entry_safe(nft_match, tmp, &nft_match_list, head)
640 kfree(nft_match);
641}
642
643static struct nft_expr_type nft_match_type __read_mostly = {
644 .name = "match",
645 .select_ops = nft_match_select_ops,
646 .policy = nft_match_policy,
647 .maxattr = NFTA_MATCH_MAX,
648 .owner = THIS_MODULE,
649};
650
651static LIST_HEAD(nft_target_list);
652
653static struct nft_expr_type nft_target_type;
654
655static const struct nft_expr_ops *
656nft_target_select_ops(const struct nft_ctx *ctx,
657 const struct nlattr * const tb[])
658{
659 struct nft_xt *nft_target;
660 struct xt_target *target;
661 char *tg_name;
662 __u32 rev, family;
663
664 if (tb[NFTA_TARGET_NAME] == NULL ||
665 tb[NFTA_TARGET_REV] == NULL ||
666 tb[NFTA_TARGET_INFO] == NULL)
667 return ERR_PTR(-EINVAL);
668
669 tg_name = nla_data(tb[NFTA_TARGET_NAME]);
670 rev = ntohl(nla_get_be32(tb[NFTA_TARGET_REV]));
671 family = ctx->afi->family;
672
673 /* Re-use the existing target if it's already loaded. */
674 list_for_each_entry(nft_target, &nft_match_list, head) {
675 struct xt_target *target = nft_target->ops.data;
676
677 if (strcmp(target->name, tg_name) == 0 &&
678 target->revision == rev && target->family == family)
679 return &nft_target->ops;
680 }
681
682 target = xt_request_find_target(family, tg_name, rev);
683 if (IS_ERR(target))
684 return ERR_PTR(-ENOENT);
685
686 /* This is the first time we use this target, allocate operations */
687 nft_target = kzalloc(sizeof(struct nft_xt), GFP_KERNEL);
688 if (nft_target == NULL)
689 return ERR_PTR(-ENOMEM);
690
691 nft_target->ops.type = &nft_target_type;
692 nft_target->ops.size = NFT_EXPR_SIZE(XT_ALIGN(target->targetsize) +
693 nft_compat_target_offset(target));
694 nft_target->ops.eval = nft_target_eval;
695 nft_target->ops.init = nft_target_init;
696 nft_target->ops.destroy = nft_target_destroy;
697 nft_target->ops.dump = nft_target_dump;
698 nft_target->ops.validate = nft_target_validate;
699 nft_target->ops.data = target;
700
701 list_add(&nft_target->head, &nft_target_list);
702
703 return &nft_target->ops;
704}
705
706static void nft_target_release(void)
707{
708 struct nft_xt *nft_target, *tmp;
709
710 list_for_each_entry_safe(nft_target, tmp, &nft_target_list, head)
711 kfree(nft_target);
712}
713
714static struct nft_expr_type nft_target_type __read_mostly = {
715 .name = "target",
716 .select_ops = nft_target_select_ops,
717 .policy = nft_target_policy,
718 .maxattr = NFTA_TARGET_MAX,
719 .owner = THIS_MODULE,
720};
721
722static int __init nft_compat_module_init(void)
723{
724 int ret;
725
726 ret = nft_register_expr(&nft_match_type);
727 if (ret < 0)
728 return ret;
729
730 ret = nft_register_expr(&nft_target_type);
731 if (ret < 0)
732 goto err_match;
733
734 ret = nfnetlink_subsys_register(&nfnl_compat_subsys);
735 if (ret < 0) {
736 pr_err("nft_compat: cannot register with nfnetlink.\n");
737 goto err_target;
738 }
739
740 pr_info("nf_tables_compat: (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>\n");
741
742 return ret;
743
744err_target:
745 nft_unregister_expr(&nft_target_type);
746err_match:
747 nft_unregister_expr(&nft_match_type);
748 return ret;
749}
750
751static void __exit nft_compat_module_exit(void)
752{
753 nfnetlink_subsys_unregister(&nfnl_compat_subsys);
754 nft_unregister_expr(&nft_target_type);
755 nft_unregister_expr(&nft_match_type);
756 nft_match_release();
757 nft_target_release();
758}
759
760MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_NFT_COMPAT);
761
762module_init(nft_compat_module_init);
763module_exit(nft_compat_module_exit);
764
765MODULE_LICENSE("GPL");
766MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
767MODULE_ALIAS_NFT_EXPR("match");
768MODULE_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..8e0bb75e7c51
--- /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;
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/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_set.c b/net/netfilter/xt_set.c
index 31790e789e22..e7c4e0e01ff5 100644
--- a/net/netfilter/xt_set.c
+++ b/net/netfilter/xt_set.c
@@ -81,7 +81,7 @@ 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 indentified by id %u to match\n",
@@ -91,7 +91,7 @@ set_match_v0_checkentry(const struct xt_mtchk_param *par)
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 indentified 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} 276}
182 277
183/* Revision 1 match and target */ 278/* Revision 1 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}
221
222static void
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_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/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..872529105abc 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) {
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/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..84b7e3ea7b7a 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -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] = {
@@ -1278,6 +1279,91 @@ static int nfc_genl_dump_ses_done(struct netlink_callback *cb)
1278 return 0; 1279 return 0;
1279} 1280}
1280 1281
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(msg, 0, nfc_genl_event_mcgrp.id, 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
1281static struct genl_ops nfc_genl_ops[] = { 1367static struct genl_ops nfc_genl_ops[] = {
1282 { 1368 {
1283 .cmd = NFC_CMD_GET_DEVICE, 1369 .cmd = NFC_CMD_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
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 313bf1bc848a..cd958b381f96 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);
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..1408adc2a2a7 100644
--- a/net/openvswitch/datapath.c
+++ b/net/openvswitch/datapath.c
@@ -55,14 +55,10 @@
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 sk_buff *skb, struct genl_info *info,
@@ -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;
@@ -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);
@@ -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);
@@ -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
@@ -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);
@@ -1699,7 +1200,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1699 } 1200 }
1700 1201
1701 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), 1202 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1702 GFP_KERNEL); 1203 GFP_KERNEL);
1703 if (!dp->ports) { 1204 if (!dp->ports) {
1704 err = -ENOMEM; 1205 err = -ENOMEM;
1705 goto err_destroy_percpu; 1206 goto err_destroy_percpu;
@@ -1746,7 +1247,7 @@ err_destroy_ports_array:
1746err_destroy_percpu: 1247err_destroy_percpu:
1747 free_percpu(dp->stats_percpu); 1248 free_percpu(dp->stats_percpu);
1748err_destroy_table: 1249err_destroy_table:
1749 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false); 1250 ovs_flow_tbl_destroy(&dp->table);
1750err_free_dp: 1251err_free_dp:
1751 release_net(ovs_dp_get_net(dp)); 1252 release_net(ovs_dp_get_net(dp));
1752 kfree(dp); 1253 kfree(dp);
@@ -2336,32 +1837,6 @@ error:
2336 return err; 1837 return err;
2337} 1838}
2338 1839
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) 1840static int __net_init ovs_init_net(struct net *net)
2366{ 1841{
2367 struct ovs_net *ovs_net = net_generic(net, ovs_net_id); 1842 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
@@ -2419,8 +1894,6 @@ static int __init dp_init(void)
2419 if (err < 0) 1894 if (err < 0)
2420 goto error_unreg_notifier; 1895 goto error_unreg_notifier;
2421 1896
2422 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2423
2424 return 0; 1897 return 0;
2425 1898
2426error_unreg_notifier: 1899error_unreg_notifier:
@@ -2437,7 +1910,6 @@ error:
2437 1910
2438static void dp_cleanup(void) 1911static void dp_cleanup(void)
2439{ 1912{
2440 cancel_delayed_work_sync(&rehash_flow_wq);
2441 dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); 1913 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2442 unregister_netdevice_notifier(&ovs_dp_device_notifier); 1914 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2443 unregister_pernet_device(&ovs_net_ops); 1915 unregister_pernet_device(&ovs_net_ops);
diff --git a/net/openvswitch/datapath.h b/net/openvswitch/datapath.h
index 4d109c176ef3..d3d14a58aa91 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;
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-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/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/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/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/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/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_police.c b/net/sched/act_police.c
index 189e3c5b3d09..272d8e924cf6 100644
--- a/net/sched/act_police.c
+++ b/net/sched/act_police.c
@@ -231,14 +231,14 @@ override:
231 } 231 }
232 if (R_tab) { 232 if (R_tab) {
233 police->rate_present = true; 233 police->rate_present = true;
234 psched_ratecfg_precompute(&police->rate, &R_tab->rate); 234 psched_ratecfg_precompute(&police->rate, &R_tab->rate, 0);
235 qdisc_put_rtab(R_tab); 235 qdisc_put_rtab(R_tab);
236 } else { 236 } else {
237 police->rate_present = false; 237 police->rate_present = false;
238 } 238 }
239 if (P_tab) { 239 if (P_tab) {
240 police->peak_present = true; 240 police->peak_present = true;
241 psched_ratecfg_precompute(&police->peak, &P_tab->rate); 241 psched_ratecfg_precompute(&police->peak, &P_tab->rate, 0);
242 qdisc_put_rtab(P_tab); 242 qdisc_put_rtab(P_tab);
243 } else { 243 } else {
244 police->peak_present = false; 244 police->peak_present = false;
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_generic.c b/net/sched/sch_generic.c
index a74e278654aa..922a09406ba7 100644
--- a/net/sched/sch_generic.c
+++ b/net/sched/sch_generic.c
@@ -126,7 +126,7 @@ int sch_direct_xmit(struct sk_buff *skb, struct Qdisc *q,
126 126
127 HARD_TX_LOCK(dev, txq, smp_processor_id()); 127 HARD_TX_LOCK(dev, txq, smp_processor_id());
128 if (!netif_xmit_frozen_or_stopped(txq)) 128 if (!netif_xmit_frozen_or_stopped(txq))
129 ret = dev_hard_start_xmit(skb, dev, txq); 129 ret = dev_hard_start_xmit(skb, dev, txq, NULL);
130 130
131 HARD_TX_UNLOCK(dev, txq); 131 HARD_TX_UNLOCK(dev, txq);
132 132
@@ -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..0e1e38b40025 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)
@@ -1491,8 +1500,12 @@ static int htb_change_class(struct Qdisc *sch, u32 classid,
1491 cl->prio = TC_HTB_NUMPRIO - 1; 1500 cl->prio = TC_HTB_NUMPRIO - 1;
1492 } 1501 }
1493 1502
1494 psched_ratecfg_precompute(&cl->rate, &hopt->rate); 1503 rate64 = tb[TCA_HTB_RATE64] ? nla_get_u64(tb[TCA_HTB_RATE64]) : 0;
1495 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil); 1504
1505 ceil64 = tb[TCA_HTB_CEIL64] ? nla_get_u64(tb[TCA_HTB_CEIL64]) : 0;
1506
1507 psched_ratecfg_precompute(&cl->rate, &hopt->rate, rate64);
1508 psched_ratecfg_precompute(&cl->ceil, &hopt->ceil, ceil64);
1496 1509
1497 cl->buffer = PSCHED_TICKS2NS(hopt->buffer); 1510 cl->buffer = PSCHED_TICKS2NS(hopt->buffer);
1498 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer); 1511 cl->cbuffer = PSCHED_TICKS2NS(hopt->cbuffer);
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index b87e83d07478..75c94e59a3bd 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -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;
diff --git a/net/sched/sch_tbf.c b/net/sched/sch_tbf.c
index 1aaf1b6e51a2..68f98595819c 100644
--- a/net/sched/sch_tbf.c
+++ b/net/sched/sch_tbf.c
@@ -266,20 +266,23 @@ static const struct nla_policy tbf_policy[TCA_TBF_MAX + 1] = {
266 [TCA_TBF_PARMS] = { .len = sizeof(struct tc_tbf_qopt) }, 266 [TCA_TBF_PARMS] = { .len = sizeof(struct tc_tbf_qopt) },
267 [TCA_TBF_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 267 [TCA_TBF_RTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
268 [TCA_TBF_PTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE }, 268 [TCA_TBF_PTAB] = { .type = NLA_BINARY, .len = TC_RTAB_SIZE },
269 [TCA_TBF_RATE64] = { .type = NLA_U64 },
270 [TCA_TBF_PRATE64] = { .type = NLA_U64 },
269}; 271};
270 272
271static int tbf_change(struct Qdisc *sch, struct nlattr *opt) 273static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
272{ 274{
273 int err; 275 int err;
274 struct tbf_sched_data *q = qdisc_priv(sch); 276 struct tbf_sched_data *q = qdisc_priv(sch);
275 struct nlattr *tb[TCA_TBF_PTAB + 1]; 277 struct nlattr *tb[TCA_TBF_MAX + 1];
276 struct tc_tbf_qopt *qopt; 278 struct tc_tbf_qopt *qopt;
277 struct qdisc_rate_table *rtab = NULL; 279 struct qdisc_rate_table *rtab = NULL;
278 struct qdisc_rate_table *ptab = NULL; 280 struct qdisc_rate_table *ptab = NULL;
279 struct Qdisc *child = NULL; 281 struct Qdisc *child = NULL;
280 int max_size, n; 282 int max_size, n;
283 u64 rate64 = 0, prate64 = 0;
281 284
282 err = nla_parse_nested(tb, TCA_TBF_PTAB, opt, tbf_policy); 285 err = nla_parse_nested(tb, TCA_TBF_MAX, opt, tbf_policy);
283 if (err < 0) 286 if (err < 0)
284 return err; 287 return err;
285 288
@@ -341,9 +344,13 @@ static int tbf_change(struct Qdisc *sch, struct nlattr *opt)
341 q->tokens = q->buffer; 344 q->tokens = q->buffer;
342 q->ptokens = q->mtu; 345 q->ptokens = q->mtu;
343 346
344 psched_ratecfg_precompute(&q->rate, &rtab->rate); 347 if (tb[TCA_TBF_RATE64])
348 rate64 = nla_get_u64(tb[TCA_TBF_RATE64]);
349 psched_ratecfg_precompute(&q->rate, &rtab->rate, rate64);
345 if (ptab) { 350 if (ptab) {
346 psched_ratecfg_precompute(&q->peak, &ptab->rate); 351 if (tb[TCA_TBF_PRATE64])
352 prate64 = nla_get_u64(tb[TCA_TBF_PRATE64]);
353 psched_ratecfg_precompute(&q->peak, &ptab->rate, prate64);
347 q->peak_present = true; 354 q->peak_present = true;
348 } else { 355 } else {
349 q->peak_present = false; 356 q->peak_present = false;
@@ -402,6 +409,13 @@ static int tbf_dump(struct Qdisc *sch, struct sk_buff *skb)
402 opt.buffer = PSCHED_NS2TICKS(q->buffer); 409 opt.buffer = PSCHED_NS2TICKS(q->buffer);
403 if (nla_put(skb, TCA_TBF_PARMS, sizeof(opt), &opt)) 410 if (nla_put(skb, TCA_TBF_PARMS, sizeof(opt), &opt))
404 goto nla_put_failure; 411 goto nla_put_failure;
412 if (q->rate.rate_bytes_ps >= (1ULL << 32) &&
413 nla_put_u64(skb, TCA_TBF_RATE64, q->rate.rate_bytes_ps))
414 goto nla_put_failure;
415 if (q->peak_present &&
416 q->peak.rate_bytes_ps >= (1ULL << 32) &&
417 nla_put_u64(skb, TCA_TBF_PRATE64, q->peak.rate_bytes_ps))
418 goto nla_put_failure;
405 419
406 nla_nest_end(skb, nest); 420 nla_nest_end(skb, nest);
407 return skb->len; 421 return skb->len;
diff --git a/net/sctp/associola.c b/net/sctp/associola.c
index cef509985192..c9b91cb1cb0d 100644
--- a/net/sctp/associola.c
+++ b/net/sctp/associola.c
@@ -602,7 +602,7 @@ void sctp_assoc_rm_peer(struct sctp_association *asoc,
602 602
603 /* Start a T3 timer here in case it wasn't running so 603 /* Start a T3 timer here in case it wasn't running so
604 * that these migrated packets have a chance to get 604 * that these migrated packets have a chance to get
605 * retrnasmitted. 605 * retransmitted.
606 */ 606 */
607 if (!timer_pending(&active->T3_rtx_timer)) 607 if (!timer_pending(&active->T3_rtx_timer))
608 if (!mod_timer(&active->T3_rtx_timer, 608 if (!mod_timer(&active->T3_rtx_timer,
@@ -665,7 +665,7 @@ struct sctp_transport *sctp_assoc_add_peer(struct sctp_association *asoc,
665 /* Set the path max_retrans. */ 665 /* Set the path max_retrans. */
666 peer->pathmaxrxt = asoc->pathmaxrxt; 666 peer->pathmaxrxt = asoc->pathmaxrxt;
667 667
668 /* And the partial failure retrnas threshold */ 668 /* And the partial failure retrans threshold */
669 peer->pf_retrans = asoc->pf_retrans; 669 peer->pf_retrans = asoc->pf_retrans;
670 670
671 /* Initialize the peer's SACK delay timeout based on the 671 /* Initialize the peer's SACK delay timeout based on the
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 96a55910262c..7567e6f1a920 100644
--- a/net/sctp/ipv6.c
+++ b/net/sctp/ipv6.c
@@ -428,20 +428,20 @@ static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
428{ 428{
429 addr->v6.sin6_family = AF_INET6; 429 addr->v6.sin6_family = AF_INET6;
430 addr->v6.sin6_port = 0; 430 addr->v6.sin6_port = 0;
431 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr; 431 addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
432} 432}
433 433
434/* Initialize sk->sk_rcv_saddr from sctp_addr. */ 434/* Initialize sk->sk_rcv_saddr from sctp_addr. */
435static 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)
436{ 436{
437 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) { 437 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
438 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0; 438 sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
439 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0; 439 sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
440 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff); 440 sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
441 inet6_sk(sk)->rcv_saddr.s6_addr32[3] = 441 sk->sk_v6_rcv_saddr.s6_addr32[3] =
442 addr->v4.sin_addr.s_addr; 442 addr->v4.sin_addr.s_addr;
443 } else { 443 } else {
444 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr; 444 sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
445 } 445 }
446} 446}
447 447
@@ -449,12 +449,12 @@ static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
449static 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)
450{ 450{
451 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) { 451 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
452 inet6_sk(sk)->daddr.s6_addr32[0] = 0; 452 sk->sk_v6_daddr.s6_addr32[0] = 0;
453 inet6_sk(sk)->daddr.s6_addr32[1] = 0; 453 sk->sk_v6_daddr.s6_addr32[1] = 0;
454 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff); 454 sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
455 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;
456 } else { 456 } else {
457 inet6_sk(sk)->daddr = addr->v6.sin6_addr; 457 sk->sk_v6_daddr = addr->v6.sin6_addr;
458 } 458 }
459} 459}
460 460
diff --git a/net/sctp/output.c b/net/sctp/output.c
index 319137340d15..e650978daf27 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
@@ -493,7 +492,6 @@ int sctp_packet_transmit(struct sctp_packet *packet)
493 if (chunk == packet->auth) 492 if (chunk == packet->auth)
494 auth = skb_tail_pointer(nskb); 493 auth = skb_tail_pointer(nskb);
495 494
496 cksum_buf_len += chunk->skb->len;
497 memcpy(skb_put(nskb, chunk->skb->len), 495 memcpy(skb_put(nskb, chunk->skb->len),
498 chunk->skb->data, chunk->skb->len); 496 chunk->skb->data, chunk->skb->len);
499 497
@@ -538,12 +536,7 @@ int sctp_packet_transmit(struct sctp_packet *packet)
538 if (!sctp_checksum_disable) { 536 if (!sctp_checksum_disable) {
539 if (!(dst->dev->features & NETIF_F_SCTP_CSUM) || 537 if (!(dst->dev->features & NETIF_F_SCTP_CSUM) ||
540 (dst_xfrm(dst) != NULL) || packet->ipfragok) { 538 (dst_xfrm(dst) != NULL) || packet->ipfragok) {
541 __u32 crc32 = sctp_start_cksum((__u8 *)sh, cksum_buf_len); 539 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 { 540 } else {
548 /* no need to seed pseudo checksum for SCTP */ 541 /* no need to seed pseudo checksum for SCTP */
549 nskb->ip_summed = CHECKSUM_PARTIAL; 542 nskb->ip_summed = CHECKSUM_PARTIAL;
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/socket.c b/net/sctp/socket.c
index 911b71b26b0e..72046b9729a8 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -5890,7 +5890,7 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
5890 int low, high, remaining, index; 5890 int low, high, remaining, index;
5891 unsigned int rover; 5891 unsigned int rover;
5892 5892
5893 inet_get_local_port_range(&low, &high); 5893 inet_get_local_port_range(sock_net(sk), &low, &high);
5894 remaining = (high - low) + 1; 5894 remaining = (high - low) + 1;
5895 rover = net_random() % remaining + low; 5895 rover = net_random() % remaining + low;
5896 5896
diff --git a/net/sunrpc/rpc_pipe.c b/net/sunrpc/rpc_pipe.c
index f94567b45bb3..d0d14a04dce1 100644
--- a/net/sunrpc/rpc_pipe.c
+++ b/net/sunrpc/rpc_pipe.c
@@ -519,8 +519,8 @@ static int __rpc_create_common(struct inode *dir, struct dentry *dentry,
519 d_add(dentry, inode); 519 d_add(dentry, inode);
520 return 0; 520 return 0;
521out_err: 521out_err:
522 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %s\n", 522 printk(KERN_WARNING "%s: %s failed to allocate inode for dentry %pd\n",
523 __FILE__, __func__, dentry->d_name.name); 523 __FILE__, __func__, dentry);
524 dput(dentry); 524 dput(dentry);
525 return -ENOMEM; 525 return -ENOMEM;
526} 526}
@@ -755,8 +755,8 @@ static int rpc_populate(struct dentry *parent,
755out_bad: 755out_bad:
756 __rpc_depopulate(parent, files, start, eof); 756 __rpc_depopulate(parent, files, start, eof);
757 mutex_unlock(&dir->i_mutex); 757 mutex_unlock(&dir->i_mutex);
758 printk(KERN_WARNING "%s: %s failed to populate directory %s\n", 758 printk(KERN_WARNING "%s: %s failed to populate directory %pd\n",
759 __FILE__, __func__, parent->d_name.name); 759 __FILE__, __func__, parent);
760 return err; 760 return err;
761} 761}
762 762
@@ -852,8 +852,8 @@ out:
852 return dentry; 852 return dentry;
853out_err: 853out_err:
854 dentry = ERR_PTR(err); 854 dentry = ERR_PTR(err);
855 printk(KERN_WARNING "%s: %s() failed to create pipe %s/%s (errno = %d)\n", 855 printk(KERN_WARNING "%s: %s() failed to create pipe %pd/%s (errno = %d)\n",
856 __FILE__, __func__, parent->d_name.name, name, 856 __FILE__, __func__, parent, name,
857 err); 857 err);
858 goto out; 858 goto out;
859} 859}
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/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.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/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..cf465d66ccde 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 */
@@ -1592,15 +1501,15 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1592 1501
1593 /* Ensure bearer is still enabled */ 1502 /* Ensure bearer is still enabled */
1594 if (unlikely(!b_ptr->active)) 1503 if (unlikely(!b_ptr->active))
1595 goto cont; 1504 goto discard;
1596 1505
1597 /* Ensure message is well-formed */ 1506 /* Ensure message is well-formed */
1598 if (unlikely(!link_recv_buf_validate(buf))) 1507 if (unlikely(!link_recv_buf_validate(buf)))
1599 goto cont; 1508 goto discard;
1600 1509
1601 /* Ensure message data is a single contiguous unit */ 1510 /* Ensure message data is a single contiguous unit */
1602 if (unlikely(skb_linearize(buf))) 1511 if (unlikely(skb_linearize(buf)))
1603 goto cont; 1512 goto discard;
1604 1513
1605 /* Handle arrival of a non-unicast link message */ 1514 /* Handle arrival of a non-unicast link message */
1606 msg = buf_msg(buf); 1515 msg = buf_msg(buf);
@@ -1616,20 +1525,18 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1616 /* Discard unicast link messages destined for another node */ 1525 /* Discard unicast link messages destined for another node */
1617 if (unlikely(!msg_short(msg) && 1526 if (unlikely(!msg_short(msg) &&
1618 (msg_destnode(msg) != tipc_own_addr))) 1527 (msg_destnode(msg) != tipc_own_addr)))
1619 goto cont; 1528 goto discard;
1620 1529
1621 /* Locate neighboring node that sent message */ 1530 /* Locate neighboring node that sent message */
1622 n_ptr = tipc_node_find(msg_prevnode(msg)); 1531 n_ptr = tipc_node_find(msg_prevnode(msg));
1623 if (unlikely(!n_ptr)) 1532 if (unlikely(!n_ptr))
1624 goto cont; 1533 goto discard;
1625 tipc_node_lock(n_ptr); 1534 tipc_node_lock(n_ptr);
1626 1535
1627 /* Locate unicast link endpoint that should handle message */ 1536 /* Locate unicast link endpoint that should handle message */
1628 l_ptr = n_ptr->links[b_ptr->identity]; 1537 l_ptr = n_ptr->links[b_ptr->identity];
1629 if (unlikely(!l_ptr)) { 1538 if (unlikely(!l_ptr))
1630 tipc_node_unlock(n_ptr); 1539 goto unlock_discard;
1631 goto cont;
1632 }
1633 1540
1634 /* Verify that communication with node is currently allowed */ 1541 /* Verify that communication with node is currently allowed */
1635 if ((n_ptr->block_setup & WAIT_PEER_DOWN) && 1542 if ((n_ptr->block_setup & WAIT_PEER_DOWN) &&
@@ -1639,10 +1546,8 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1639 !msg_redundant_link(msg)) 1546 !msg_redundant_link(msg))
1640 n_ptr->block_setup &= ~WAIT_PEER_DOWN; 1547 n_ptr->block_setup &= ~WAIT_PEER_DOWN;
1641 1548
1642 if (n_ptr->block_setup) { 1549 if (n_ptr->block_setup)
1643 tipc_node_unlock(n_ptr); 1550 goto unlock_discard;
1644 goto cont;
1645 }
1646 1551
1647 /* Validate message sequence number info */ 1552 /* Validate message sequence number info */
1648 seq_no = msg_seqno(msg); 1553 seq_no = msg_seqno(msg);
@@ -1678,98 +1583,100 @@ void tipc_recv_msg(struct sk_buff *head, struct tipc_bearer *b_ptr)
1678 1583
1679 /* Now (finally!) process the incoming message */ 1584 /* Now (finally!) process the incoming message */
1680protocol_check: 1585protocol_check:
1681 if (likely(link_working_working(l_ptr))) { 1586 if (unlikely(!link_working_working(l_ptr))) {
1682 if (likely(seq_no == mod(l_ptr->next_in_no))) { 1587 if (msg_user(msg) == LINK_PROTOCOL) {
1683 l_ptr->next_in_no++; 1588 link_recv_proto_msg(l_ptr, buf);
1684 if (unlikely(l_ptr->oldest_deferred_in)) 1589 head = link_insert_deferred_queue(l_ptr, head);
1685 head = link_insert_deferred_queue(l_ptr, 1590 tipc_node_unlock(n_ptr);
1686 head); 1591 continue;
1687deliver: 1592 }
1688 if (likely(msg_isdata(msg))) { 1593
1689 tipc_node_unlock(n_ptr); 1594 /* Traffic message. Conditionally activate link */
1690 tipc_port_recv_msg(buf); 1595 link_state_event(l_ptr, TRAFFIC_MSG_EVT);
1691 continue; 1596
1692 } 1597 if (link_working_working(l_ptr)) {
1693 switch (msg_user(msg)) { 1598 /* Re-insert buffer in front of queue */
1694 int ret; 1599 buf->next = head;
1695 case MSG_BUNDLER: 1600 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); 1601 tipc_node_unlock(n_ptr);
1744 tipc_net_route_msg(buf);
1745 continue; 1602 continue;
1746 } 1603 }
1604 goto unlock_discard;
1605 }
1606
1607 /* Link is now in state WORKING_WORKING */
1608 if (unlikely(seq_no != mod(l_ptr->next_in_no))) {
1747 link_handle_out_of_seq_msg(l_ptr, buf); 1609 link_handle_out_of_seq_msg(l_ptr, buf);
1748 head = link_insert_deferred_queue(l_ptr, head); 1610 head = link_insert_deferred_queue(l_ptr, head);
1749 tipc_node_unlock(n_ptr); 1611 tipc_node_unlock(n_ptr);
1750 continue; 1612 continue;
1751 } 1613 }
1752 1614 l_ptr->next_in_no++;
1753 /* Link is not in state WORKING_WORKING */ 1615 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); 1616 head = link_insert_deferred_queue(l_ptr, head);
1617deliver:
1618 if (likely(msg_isdata(msg))) {
1757 tipc_node_unlock(n_ptr); 1619 tipc_node_unlock(n_ptr);
1620 tipc_port_recv_msg(buf);
1758 continue; 1621 continue;
1759 } 1622 }
1760 1623 switch (msg_user(msg)) {
1761 /* Traffic message. Conditionally activate link */ 1624 int ret;
1762 link_state_event(l_ptr, TRAFFIC_MSG_EVT); 1625 case MSG_BUNDLER:
1763 1626 l_ptr->stats.recv_bundles++;
1764 if (link_working_working(l_ptr)) { 1627 l_ptr->stats.recv_bundled += msg_msgcnt(msg);
1765 /* Re-insert buffer in front of queue */ 1628 tipc_node_unlock(n_ptr);
1766 buf->next = head; 1629 tipc_link_recv_bundle(buf);
1767 head = buf; 1630 continue;
1631 case NAME_DISTRIBUTOR:
1632 n_ptr->bclink.recv_permitted = true;
1633 tipc_node_unlock(n_ptr);
1634 tipc_named_recv(buf);
1635 continue;
1636 case BCAST_PROTOCOL:
1637 tipc_link_recv_sync(n_ptr, buf);
1638 tipc_node_unlock(n_ptr);
1639 continue;
1640 case CONN_MANAGER:
1641 tipc_node_unlock(n_ptr);
1642 tipc_port_recv_proto_msg(buf);
1643 continue;
1644 case MSG_FRAGMENTER:
1645 l_ptr->stats.recv_fragments++;
1646 ret = tipc_link_recv_fragment(&l_ptr->reasm_head,
1647 &l_ptr->reasm_tail,
1648 &buf);
1649 if (ret == LINK_REASM_COMPLETE) {
1650 l_ptr->stats.recv_fragmented++;
1651 msg = buf_msg(buf);
1652 goto deliver;
1653 }
1654 if (ret == LINK_REASM_ERROR)
1655 tipc_link_reset(l_ptr);
1768 tipc_node_unlock(n_ptr); 1656 tipc_node_unlock(n_ptr);
1769 continue; 1657 continue;
1658 case CHANGEOVER_PROTOCOL:
1659 type = msg_type(msg);
1660 if (link_recv_changeover_msg(&l_ptr, &buf)) {
1661 msg = buf_msg(buf);
1662 seq_no = msg_seqno(msg);
1663 if (type == ORIGINAL_MSG)
1664 goto deliver;
1665 goto protocol_check;
1666 }
1667 break;
1668 default:
1669 kfree_skb(buf);
1670 buf = NULL;
1671 break;
1770 } 1672 }
1771 tipc_node_unlock(n_ptr); 1673 tipc_node_unlock(n_ptr);
1772cont: 1674 tipc_net_route_msg(buf);
1675 continue;
1676unlock_discard:
1677
1678 tipc_node_unlock(n_ptr);
1679discard:
1773 kfree_skb(buf); 1680 kfree_skb(buf);
1774 } 1681 }
1775 read_unlock_bh(&tipc_net_lock); 1682 read_unlock_bh(&tipc_net_lock);
@@ -2432,114 +2339,47 @@ static int link_send_long_buf(struct tipc_link *l_ptr, struct sk_buff *buf)
2432} 2339}
2433 2340
2434/* 2341/*
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 2342 * tipc_link_recv_fragment(): Called with node lock on. Returns
2467 * the reassembled buffer if message is complete. 2343 * the reassembled buffer if message is complete.
2468 */ 2344 */
2469int tipc_link_recv_fragment(struct sk_buff **pending, struct sk_buff **fb, 2345int tipc_link_recv_fragment(struct sk_buff **head, struct sk_buff **tail,
2470 struct tipc_msg **m) 2346 struct sk_buff **fbuf)
2471{ 2347{
2472 struct sk_buff *prev = NULL; 2348 struct sk_buff *frag = *fbuf;
2473 struct sk_buff *fbuf = *fb; 2349 struct tipc_msg *msg = buf_msg(frag);
2474 struct tipc_msg *fragm = buf_msg(fbuf); 2350 u32 fragid = msg_type(msg);
2475 struct sk_buff *pbuf = *pending; 2351 bool headstolen;
2476 u32 long_msg_seq_no = msg_long_msgno(fragm); 2352 int delta;
2477 2353
2478 *fb = NULL; 2354 skb_pull(frag, msg_hdr_sz(msg));
2479 2355 if (fragid == FIRST_FRAGMENT) {
2480 /* Is there an incomplete message waiting for this fragment? */ 2356 if (*head || skb_unclone(frag, GFP_ATOMIC))
2481 while (pbuf && ((buf_seqno(pbuf) != long_msg_seq_no) || 2357 goto out_free;
2482 (msg_orignode(fragm) != msg_orignode(buf_msg(pbuf))))) { 2358 *head = frag;
2483 prev = pbuf; 2359 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; 2360 return 0;
2361 } else if (skb_try_coalesce(*head, frag, &headstolen, &delta)) {
2362 kfree_skb_partial(frag, headstolen);
2363 } else {
2364 if (!*head)
2365 goto out_free;
2366 if (!skb_has_frag_list(*head))
2367 skb_shinfo(*head)->frag_list = frag;
2368 else
2369 (*tail)->next = frag;
2370 *tail = frag;
2371 (*head)->truesize += frag->truesize;
2372 }
2373 if (fragid == LAST_FRAGMENT) {
2374 *fbuf = *head;
2375 *tail = *head = NULL;
2376 return LINK_REASM_COMPLETE;
2540 } 2377 }
2541 kfree_skb(fbuf);
2542 return 0; 2378 return 0;
2379out_free:
2380 pr_warn_ratelimited("Link unable to reassemble fragmented message\n");
2381 kfree_skb(*fbuf);
2382 return LINK_REASM_ERROR;
2543} 2383}
2544 2384
2545static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance) 2385static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tolerance)
@@ -2585,25 +2425,21 @@ void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window)
2585static struct tipc_link *link_find_link(const char *name, 2425static struct tipc_link *link_find_link(const char *name,
2586 struct tipc_node **node) 2426 struct tipc_node **node)
2587{ 2427{
2588 struct tipc_link_name link_name_parts;
2589 struct tipc_bearer *b_ptr;
2590 struct tipc_link *l_ptr; 2428 struct tipc_link *l_ptr;
2429 struct tipc_node *n_ptr;
2430 int i;
2591 2431
2592 if (!link_name_validate(name, &link_name_parts)) 2432 list_for_each_entry(n_ptr, &tipc_node_list, list) {
2593 return NULL; 2433 for (i = 0; i < MAX_BEARERS; i++) {
2594 2434 l_ptr = n_ptr->links[i];
2595 b_ptr = tipc_bearer_find_interface(link_name_parts.if_local); 2435 if (l_ptr && !strcmp(l_ptr->name, name))
2596 if (!b_ptr) 2436 goto found;
2597 return NULL; 2437 }
2598 2438 }
2599 *node = tipc_node_find(link_name_parts.addr_peer); 2439 l_ptr = NULL;
2600 if (!*node) 2440 n_ptr = NULL;
2601 return NULL; 2441found:
2602 2442 *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; 2443 return l_ptr;
2608} 2444}
2609 2445
@@ -2646,6 +2482,7 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2646 struct tipc_link *l_ptr; 2482 struct tipc_link *l_ptr;
2647 struct tipc_bearer *b_ptr; 2483 struct tipc_bearer *b_ptr;
2648 struct tipc_media *m_ptr; 2484 struct tipc_media *m_ptr;
2485 int res = 0;
2649 2486
2650 l_ptr = link_find_link(name, &node); 2487 l_ptr = link_find_link(name, &node);
2651 if (l_ptr) { 2488 if (l_ptr) {
@@ -2668,9 +2505,12 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2668 case TIPC_CMD_SET_LINK_WINDOW: 2505 case TIPC_CMD_SET_LINK_WINDOW:
2669 tipc_link_set_queue_limits(l_ptr, new_value); 2506 tipc_link_set_queue_limits(l_ptr, new_value);
2670 break; 2507 break;
2508 default:
2509 res = -EINVAL;
2510 break;
2671 } 2511 }
2672 tipc_node_unlock(node); 2512 tipc_node_unlock(node);
2673 return 0; 2513 return res;
2674 } 2514 }
2675 2515
2676 b_ptr = tipc_bearer_find(name); 2516 b_ptr = tipc_bearer_find(name);
@@ -2678,15 +2518,18 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2678 switch (cmd) { 2518 switch (cmd) {
2679 case TIPC_CMD_SET_LINK_TOL: 2519 case TIPC_CMD_SET_LINK_TOL:
2680 b_ptr->tolerance = new_value; 2520 b_ptr->tolerance = new_value;
2681 return 0; 2521 break;
2682 case TIPC_CMD_SET_LINK_PRI: 2522 case TIPC_CMD_SET_LINK_PRI:
2683 b_ptr->priority = new_value; 2523 b_ptr->priority = new_value;
2684 return 0; 2524 break;
2685 case TIPC_CMD_SET_LINK_WINDOW: 2525 case TIPC_CMD_SET_LINK_WINDOW:
2686 b_ptr->window = new_value; 2526 b_ptr->window = new_value;
2687 return 0; 2527 break;
2528 default:
2529 res = -EINVAL;
2530 break;
2688 } 2531 }
2689 return -EINVAL; 2532 return res;
2690 } 2533 }
2691 2534
2692 m_ptr = tipc_media_find(name); 2535 m_ptr = tipc_media_find(name);
@@ -2695,15 +2538,18 @@ static int link_cmd_set_value(const char *name, u32 new_value, u16 cmd)
2695 switch (cmd) { 2538 switch (cmd) {
2696 case TIPC_CMD_SET_LINK_TOL: 2539 case TIPC_CMD_SET_LINK_TOL:
2697 m_ptr->tolerance = new_value; 2540 m_ptr->tolerance = new_value;
2698 return 0; 2541 break;
2699 case TIPC_CMD_SET_LINK_PRI: 2542 case TIPC_CMD_SET_LINK_PRI:
2700 m_ptr->priority = new_value; 2543 m_ptr->priority = new_value;
2701 return 0; 2544 break;
2702 case TIPC_CMD_SET_LINK_WINDOW: 2545 case TIPC_CMD_SET_LINK_WINDOW:
2703 m_ptr->window = new_value; 2546 m_ptr->window = new_value;
2704 return 0; 2547 break;
2548 default:
2549 res = -EINVAL;
2550 break;
2705 } 2551 }
2706 return -EINVAL; 2552 return res;
2707} 2553}
2708 2554
2709struct sk_buff *tipc_link_cmd_config(const void *req_tlv_area, int req_tlv_space, 2555struct 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/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..c081a7632302 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
@@ -436,14 +434,13 @@ exit:
436} 434}
437 435
438int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr, 436int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
439 struct iovec const *msg_sect, u32 num_sect, 437 struct iovec const *msg_sect, unsigned int len,
440 unsigned int total_len, int err) 438 int err)
441{ 439{
442 struct sk_buff *buf; 440 struct sk_buff *buf;
443 int res; 441 int res;
444 442
445 res = tipc_msg_build(hdr, msg_sect, num_sect, total_len, MAX_MSG_SIZE, 443 res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
446 &buf);
447 if (!buf) 444 if (!buf)
448 return res; 445 return res;
449 446
@@ -918,15 +915,14 @@ int tipc_port_recv_msg(struct sk_buff *buf)
918 * tipc_port_recv_sections(): Concatenate and deliver sectioned 915 * tipc_port_recv_sections(): Concatenate and deliver sectioned
919 * message for this node. 916 * message for this node.
920 */ 917 */
921static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect, 918static int tipc_port_recv_sections(struct tipc_port *sender,
922 struct iovec const *msg_sect, 919 struct iovec const *msg_sect,
923 unsigned int total_len) 920 unsigned int len)
924{ 921{
925 struct sk_buff *buf; 922 struct sk_buff *buf;
926 int res; 923 int res;
927 924
928 res = tipc_msg_build(&sender->phdr, msg_sect, num_sect, total_len, 925 res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
929 MAX_MSG_SIZE, &buf);
930 if (likely(buf)) 926 if (likely(buf))
931 tipc_port_recv_msg(buf); 927 tipc_port_recv_msg(buf);
932 return res; 928 return res;
@@ -935,8 +931,7 @@ static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_se
935/** 931/**
936 * tipc_send - send message sections on connection 932 * tipc_send - send message sections on connection
937 */ 933 */
938int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect, 934int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
939 unsigned int total_len)
940{ 935{
941 struct tipc_port *p_ptr; 936 struct tipc_port *p_ptr;
942 u32 destnode; 937 u32 destnode;
@@ -950,11 +945,10 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
950 if (!tipc_port_congested(p_ptr)) { 945 if (!tipc_port_congested(p_ptr)) {
951 destnode = port_peernode(p_ptr); 946 destnode = port_peernode(p_ptr);
952 if (likely(!in_own_node(destnode))) 947 if (likely(!in_own_node(destnode)))
953 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, 948 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
954 total_len, destnode); 949 len, destnode);
955 else 950 else
956 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect, 951 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
957 total_len);
958 952
959 if (likely(res != -ELINKCONG)) { 953 if (likely(res != -ELINKCONG)) {
960 p_ptr->congested = 0; 954 p_ptr->congested = 0;
@@ -965,7 +959,7 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
965 } 959 }
966 if (port_unreliable(p_ptr)) { 960 if (port_unreliable(p_ptr)) {
967 p_ptr->congested = 0; 961 p_ptr->congested = 0;
968 return total_len; 962 return len;
969 } 963 }
970 return -ELINKCONG; 964 return -ELINKCONG;
971} 965}
@@ -974,8 +968,7 @@ int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect,
974 * tipc_send2name - send message sections to port name 968 * tipc_send2name - send message sections to port name
975 */ 969 */
976int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain, 970int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
977 unsigned int num_sect, struct iovec const *msg_sect, 971 struct iovec const *msg_sect, unsigned int len)
978 unsigned int total_len)
979{ 972{
980 struct tipc_port *p_ptr; 973 struct tipc_port *p_ptr;
981 struct tipc_msg *msg; 974 struct tipc_msg *msg;
@@ -999,36 +992,32 @@ int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
999 992
1000 if (likely(destport || destnode)) { 993 if (likely(destport || destnode)) {
1001 if (likely(in_own_node(destnode))) 994 if (likely(in_own_node(destnode)))
1002 res = tipc_port_recv_sections(p_ptr, num_sect, 995 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
1003 msg_sect, total_len);
1004 else if (tipc_own_addr) 996 else if (tipc_own_addr)
1005 res = tipc_link_send_sections_fast(p_ptr, msg_sect, 997 res = tipc_link_send_sections_fast(p_ptr, msg_sect,
1006 num_sect, total_len, 998 len, destnode);
1007 destnode);
1008 else 999 else
1009 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, 1000 res = tipc_port_reject_sections(p_ptr, msg, msg_sect,
1010 num_sect, total_len, 1001 len, TIPC_ERR_NO_NODE);
1011 TIPC_ERR_NO_NODE);
1012 if (likely(res != -ELINKCONG)) { 1002 if (likely(res != -ELINKCONG)) {
1013 if (res > 0) 1003 if (res > 0)
1014 p_ptr->sent++; 1004 p_ptr->sent++;
1015 return res; 1005 return res;
1016 } 1006 }
1017 if (port_unreliable(p_ptr)) { 1007 if (port_unreliable(p_ptr)) {
1018 return total_len; 1008 return len;
1019 } 1009 }
1020 return -ELINKCONG; 1010 return -ELINKCONG;
1021 } 1011 }
1022 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect, 1012 return tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1023 total_len, TIPC_ERR_NO_NAME); 1013 TIPC_ERR_NO_NAME);
1024} 1014}
1025 1015
1026/** 1016/**
1027 * tipc_send2port - send message sections to port identity 1017 * tipc_send2port - send message sections to port identity
1028 */ 1018 */
1029int tipc_send2port(u32 ref, struct tipc_portid const *dest, 1019int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1030 unsigned int num_sect, struct iovec const *msg_sect, 1020 struct iovec const *msg_sect, unsigned int len)
1031 unsigned int total_len)
1032{ 1021{
1033 struct tipc_port *p_ptr; 1022 struct tipc_port *p_ptr;
1034 struct tipc_msg *msg; 1023 struct tipc_msg *msg;
@@ -1046,21 +1035,20 @@ int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1046 msg_set_hdr_sz(msg, BASIC_H_SIZE); 1035 msg_set_hdr_sz(msg, BASIC_H_SIZE);
1047 1036
1048 if (in_own_node(dest->node)) 1037 if (in_own_node(dest->node))
1049 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect, 1038 res = tipc_port_recv_sections(p_ptr, msg_sect, len);
1050 total_len);
1051 else if (tipc_own_addr) 1039 else if (tipc_own_addr)
1052 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, 1040 res = tipc_link_send_sections_fast(p_ptr, msg_sect, len,
1053 total_len, dest->node); 1041 dest->node);
1054 else 1042 else
1055 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect, 1043 res = tipc_port_reject_sections(p_ptr, msg, msg_sect, len,
1056 total_len, TIPC_ERR_NO_NODE); 1044 TIPC_ERR_NO_NODE);
1057 if (likely(res != -ELINKCONG)) { 1045 if (likely(res != -ELINKCONG)) {
1058 if (res > 0) 1046 if (res > 0)
1059 p_ptr->sent++; 1047 p_ptr->sent++;
1060 return res; 1048 return res;
1061 } 1049 }
1062 if (port_unreliable(p_ptr)) { 1050 if (port_unreliable(p_ptr)) {
1063 return total_len; 1051 return len;
1064 } 1052 }
1065 return -ELINKCONG; 1053 return -ELINKCONG;
1066} 1054}
diff --git a/net/tipc/port.h b/net/tipc/port.h
index 5a7026b9c345..912253597343 100644
--- a/net/tipc/port.h
+++ b/net/tipc/port.h
@@ -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..3906527259d1 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) ||
@@ -622,13 +622,11 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
622 res = tipc_send2name(tport->ref, 622 res = tipc_send2name(tport->ref,
623 &dest->addr.name.name, 623 &dest->addr.name.name,
624 dest->addr.name.domain, 624 dest->addr.name.domain,
625 m->msg_iovlen,
626 m->msg_iov, 625 m->msg_iov,
627 total_len); 626 total_len);
628 } else if (dest->addrtype == TIPC_ADDR_ID) { 627 } else if (dest->addrtype == TIPC_ADDR_ID) {
629 res = tipc_send2port(tport->ref, 628 res = tipc_send2port(tport->ref,
630 &dest->addr.id, 629 &dest->addr.id,
631 m->msg_iovlen,
632 m->msg_iov, 630 m->msg_iov,
633 total_len); 631 total_len);
634 } else if (dest->addrtype == TIPC_ADDR_MCAST) { 632 } else if (dest->addrtype == TIPC_ADDR_MCAST) {
@@ -641,7 +639,6 @@ static int send_msg(struct kiocb *iocb, struct socket *sock,
641 break; 639 break;
642 res = tipc_multicast(tport->ref, 640 res = tipc_multicast(tport->ref,
643 &dest->addr.nameseq, 641 &dest->addr.nameseq,
644 m->msg_iovlen,
645 m->msg_iov, 642 m->msg_iov,
646 total_len); 643 total_len);
647 } 644 }
@@ -707,8 +704,7 @@ static int send_packet(struct kiocb *iocb, struct socket *sock,
707 break; 704 break;
708 } 705 }
709 706
710 res = tipc_send(tport->ref, m->msg_iovlen, m->msg_iov, 707 res = tipc_send(tport->ref, m->msg_iov, total_len);
711 total_len);
712 if (likely(res != -ELINKCONG)) 708 if (likely(res != -ELINKCONG))
713 break; 709 break;
714 if (timeout_val <= 0L) { 710 if (timeout_val <= 0L) {
@@ -1368,7 +1364,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
1368 return TIPC_ERR_OVERLOAD; 1364 return TIPC_ERR_OVERLOAD;
1369 1365
1370 /* Enqueue message */ 1366 /* Enqueue message */
1371 TIPC_SKB_CB(buf)->handle = 0; 1367 TIPC_SKB_CB(buf)->handle = NULL;
1372 __skb_queue_tail(&sk->sk_receive_queue, buf); 1368 __skb_queue_tail(&sk->sk_receive_queue, buf);
1373 skb_set_owner_r(buf, sk); 1369 skb_set_owner_r(buf, sk);
1374 1370
@@ -1691,7 +1687,7 @@ restart:
1691 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */ 1687 /* Disconnect and send a 'FIN+' or 'FIN-' message to peer */
1692 buf = __skb_dequeue(&sk->sk_receive_queue); 1688 buf = __skb_dequeue(&sk->sk_receive_queue);
1693 if (buf) { 1689 if (buf) {
1694 if (TIPC_SKB_CB(buf)->handle != 0) { 1690 if (TIPC_SKB_CB(buf)->handle != NULL) {
1695 kfree_skb(buf); 1691 kfree_skb(buf);
1696 goto restart; 1692 goto restart;
1697 } 1693 }
diff --git a/net/wimax/wimax-internal.h b/net/wimax/wimax-internal.h
index 1e743d214856..5dcd9c067bf0 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,13 +76,13 @@ 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
87extern struct genl_family wimax_gnl_family; 87extern struct genl_family wimax_gnl_family;
88extern struct genl_multicast_group wimax_gnl_mcg; 88extern struct genl_multicast_group wimax_gnl_mcg;
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.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..9d797df56649 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;
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..a7f4e7902104 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -354,6 +354,9 @@ static const struct nla_policy nl80211_policy[NL80211_ATTR_MAX+1] = {
354 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED }, 354 [NL80211_ATTR_CSA_IES] = { .type = NLA_NESTED },
355 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 }, 355 [NL80211_ATTR_CSA_C_OFF_BEACON] = { .type = NLA_U16 },
356 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 }, 356 [NL80211_ATTR_CSA_C_OFF_PRESP] = { .type = NLA_U16 },
357 [NL80211_ATTR_STA_SUPPORTED_CHANNELS] = { .type = NLA_BINARY },
358 [NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES] = { .type = NLA_BINARY },
359 [NL80211_ATTR_HANDLE_DFS] = { .type = NLA_FLAG },
357}; 360};
358 361
359/* policy for the key attributes */ 362/* policy for the key attributes */
@@ -3896,9 +3899,45 @@ static int nl80211_parse_sta_wme(struct genl_info *info,
3896 return 0; 3899 return 0;
3897} 3900}
3898 3901
3902static int nl80211_parse_sta_channel_info(struct genl_info *info,
3903 struct station_parameters *params)
3904{
3905 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]) {
3906 params->supported_channels =
3907 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3908 params->supported_channels_len =
3909 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_CHANNELS]);
3910 /*
3911 * Need to include at least one (first channel, number of
3912 * channels) tuple for each subband, and must have proper
3913 * tuples for the rest of the data as well.
3914 */
3915 if (params->supported_channels_len < 2)
3916 return -EINVAL;
3917 if (params->supported_channels_len % 2)
3918 return -EINVAL;
3919 }
3920
3921 if (info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]) {
3922 params->supported_oper_classes =
3923 nla_data(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3924 params->supported_oper_classes_len =
3925 nla_len(info->attrs[NL80211_ATTR_STA_SUPPORTED_OPER_CLASSES]);
3926 /*
3927 * The value of the Length field of the Supported Operating
3928 * Classes element is between 2 and 253.
3929 */
3930 if (params->supported_oper_classes_len < 2 ||
3931 params->supported_oper_classes_len > 253)
3932 return -EINVAL;
3933 }
3934 return 0;
3935}
3936
3899static int nl80211_set_station_tdls(struct genl_info *info, 3937static int nl80211_set_station_tdls(struct genl_info *info,
3900 struct station_parameters *params) 3938 struct station_parameters *params)
3901{ 3939{
3940 int err;
3902 /* Dummy STA entry gets updated once the peer capabilities are known */ 3941 /* Dummy STA entry gets updated once the peer capabilities are known */
3903 if (info->attrs[NL80211_ATTR_PEER_AID]) 3942 if (info->attrs[NL80211_ATTR_PEER_AID])
3904 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]); 3943 params->aid = nla_get_u16(info->attrs[NL80211_ATTR_PEER_AID]);
@@ -3909,6 +3948,10 @@ static int nl80211_set_station_tdls(struct genl_info *info,
3909 params->vht_capa = 3948 params->vht_capa =
3910 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]); 3949 nla_data(info->attrs[NL80211_ATTR_VHT_CAPABILITY]);
3911 3950
3951 err = nl80211_parse_sta_channel_info(info, params);
3952 if (err)
3953 return err;
3954
3912 return nl80211_parse_sta_wme(info, params); 3955 return nl80211_parse_sta_wme(info, params);
3913} 3956}
3914 3957
@@ -4089,6 +4132,10 @@ static int nl80211_new_station(struct sk_buff *skb, struct genl_info *info)
4089 return -EINVAL; 4132 return -EINVAL;
4090 } 4133 }
4091 4134
4135 err = nl80211_parse_sta_channel_info(info, &params);
4136 if (err)
4137 return err;
4138
4092 err = nl80211_parse_sta_wme(info, &params); 4139 err = nl80211_parse_sta_wme(info, &params);
4093 if (err) 4140 if (err)
4094 return err; 4141 return err;
@@ -5591,6 +5638,9 @@ static int nl80211_start_radar_detection(struct sk_buff *skb,
5591 if (err) 5638 if (err)
5592 return err; 5639 return err;
5593 5640
5641 if (netif_carrier_ok(dev))
5642 return -EBUSY;
5643
5594 if (wdev->cac_started) 5644 if (wdev->cac_started)
5595 return -EBUSY; 5645 return -EBUSY;
5596 5646
@@ -5634,15 +5684,27 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5634 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1]; 5684 static struct nlattr *csa_attrs[NL80211_ATTR_MAX+1];
5635 u8 radar_detect_width = 0; 5685 u8 radar_detect_width = 0;
5636 int err; 5686 int err;
5687 bool need_new_beacon = false;
5637 5688
5638 if (!rdev->ops->channel_switch || 5689 if (!rdev->ops->channel_switch ||
5639 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH)) 5690 !(rdev->wiphy.flags & WIPHY_FLAG_HAS_CHANNEL_SWITCH))
5640 return -EOPNOTSUPP; 5691 return -EOPNOTSUPP;
5641 5692
5642 /* may add IBSS support later */ 5693 switch (dev->ieee80211_ptr->iftype) {
5643 if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_AP && 5694 case NL80211_IFTYPE_AP:
5644 dev->ieee80211_ptr->iftype != NL80211_IFTYPE_P2P_GO) 5695 case NL80211_IFTYPE_P2P_GO:
5696 need_new_beacon = true;
5697
5698 /* useless if AP is not running */
5699 if (!wdev->beacon_interval)
5700 return -EINVAL;
5701 break;
5702 case NL80211_IFTYPE_ADHOC:
5703 case NL80211_IFTYPE_MESH_POINT:
5704 break;
5705 default:
5645 return -EOPNOTSUPP; 5706 return -EOPNOTSUPP;
5707 }
5646 5708
5647 memset(&params, 0, sizeof(params)); 5709 memset(&params, 0, sizeof(params));
5648 5710
@@ -5651,15 +5713,14 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5651 return -EINVAL; 5713 return -EINVAL;
5652 5714
5653 /* only important for AP, IBSS and mesh create IEs internally */ 5715 /* only important for AP, IBSS and mesh create IEs internally */
5654 if (!info->attrs[NL80211_ATTR_CSA_IES]) 5716 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; 5717 return -EINVAL;
5660 5718
5661 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]); 5719 params.count = nla_get_u32(info->attrs[NL80211_ATTR_CH_SWITCH_COUNT]);
5662 5720
5721 if (!need_new_beacon)
5722 goto skip_beacons;
5723
5663 err = nl80211_parse_beacon(info->attrs, &params.beacon_after); 5724 err = nl80211_parse_beacon(info->attrs, &params.beacon_after);
5664 if (err) 5725 if (err)
5665 return err; 5726 return err;
@@ -5699,6 +5760,7 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5699 return -EINVAL; 5760 return -EINVAL;
5700 } 5761 }
5701 5762
5763skip_beacons:
5702 err = nl80211_parse_chandef(rdev, info, &params.chandef); 5764 err = nl80211_parse_chandef(rdev, info, &params.chandef);
5703 if (err) 5765 if (err)
5704 return err; 5766 return err;
@@ -5706,12 +5768,17 @@ static int nl80211_channel_switch(struct sk_buff *skb, struct genl_info *info)
5706 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef)) 5768 if (!cfg80211_reg_can_beacon(&rdev->wiphy, &params.chandef))
5707 return -EINVAL; 5769 return -EINVAL;
5708 5770
5709 err = cfg80211_chandef_dfs_required(wdev->wiphy, &params.chandef); 5771 if (dev->ieee80211_ptr->iftype == NL80211_IFTYPE_AP ||
5710 if (err < 0) { 5772 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_P2P_GO ||
5711 return err; 5773 dev->ieee80211_ptr->iftype == NL80211_IFTYPE_ADHOC) {
5712 } else if (err) { 5774 err = cfg80211_chandef_dfs_required(wdev->wiphy,
5713 radar_detect_width = BIT(params.chandef.width); 5775 &params.chandef);
5714 params.radar_required = true; 5776 if (err < 0) {
5777 return err;
5778 } else if (err) {
5779 radar_detect_width = BIT(params.chandef.width);
5780 params.radar_required = true;
5781 }
5715 } 5782 }
5716 5783
5717 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype, 5784 err = cfg80211_can_use_iftype_chan(rdev, wdev, wdev->iftype,
@@ -6535,6 +6602,9 @@ static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
6535 ibss.control_port = 6602 ibss.control_port =
6536 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]); 6603 nla_get_flag(info->attrs[NL80211_ATTR_CONTROL_PORT]);
6537 6604
6605 ibss.userspace_handles_dfs =
6606 nla_get_flag(info->attrs[NL80211_ATTR_HANDLE_DFS]);
6607
6538 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys); 6608 err = cfg80211_join_ibss(rdev, dev, &ibss, connkeys);
6539 if (err) 6609 if (err)
6540 kfree(connkeys); 6610 kfree(connkeys);
@@ -10740,7 +10810,9 @@ void cfg80211_ch_switch_notify(struct net_device *dev,
10740 wdev_lock(wdev); 10810 wdev_lock(wdev);
10741 10811
10742 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP && 10812 if (WARN_ON(wdev->iftype != NL80211_IFTYPE_AP &&
10743 wdev->iftype != NL80211_IFTYPE_P2P_GO)) 10813 wdev->iftype != NL80211_IFTYPE_P2P_GO &&
10814 wdev->iftype != NL80211_IFTYPE_ADHOC &&
10815 wdev->iftype != NL80211_IFTYPE_MESH_POINT))
10744 goto out; 10816 goto out;
10745 10817
10746 wdev->channel = chandef->chan; 10818 wdev->channel = chandef->chan;
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..65f800890d70 100644
--- a/net/wireless/sme.c
+++ b/net/wireless/sme.c
@@ -682,8 +682,8 @@ void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
682 * - country_ie + 2, the start of the country ie data, and 682 * - country_ie + 2, the start of the country ie data, and
683 * - and country_ie[1] which is the IE length 683 * - and country_ie[1] which is the IE length
684 */ 684 */
685 regulatory_hint_11d(wdev->wiphy, bss->channel->band, 685 regulatory_hint_country_ie(wdev->wiphy, bss->channel->band,
686 country_ie + 2, country_ie[1]); 686 country_ie + 2, country_ie[1]);
687 kfree(country_ie); 687 kfree(country_ie);
688} 688}
689 689
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/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 3be02b680268..ccfdc7115a83 100644
--- a/net/xfrm/xfrm_ipcomp.c
+++ b/net/xfrm/xfrm_ipcomp.c
@@ -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))