aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorDave Airlie <airlied@redhat.com>2015-04-19 21:32:26 -0400
committerDave Airlie <airlied@redhat.com>2015-04-19 23:05:20 -0400
commit2c33ce009ca2389dbf0535d0672214d09738e35e (patch)
tree6186a6458c3c160385d794a23eaf07c786a9e61b /net
parentcec32a47010647e8b0603726ebb75b990a4057a4 (diff)
parent09d51602cf84a1264946711dd4ea0dddbac599a1 (diff)
Merge Linus master into drm-next
The merge is clean, but the arm build fails afterwards, due to API changes in the regulator tree. I've included the patch into the merge to fix the build. Signed-off-by: Dave Airlie <airlied@redhat.com>
Diffstat (limited to 'net')
-rw-r--r--net/6lowpan/Kconfig57
-rw-r--r--net/6lowpan/Makefile13
-rw-r--r--net/6lowpan/iphc.c200
-rw-r--r--net/6lowpan/nhc.c241
-rw-r--r--net/6lowpan/nhc.h146
-rw-r--r--net/6lowpan/nhc_dest.c28
-rw-r--r--net/6lowpan/nhc_fragment.c27
-rw-r--r--net/6lowpan/nhc_hop.c27
-rw-r--r--net/6lowpan/nhc_ipv6.c27
-rw-r--r--net/6lowpan/nhc_mobility.c27
-rw-r--r--net/6lowpan/nhc_routing.c27
-rw-r--r--net/6lowpan/nhc_udp.c157
-rw-r--r--net/802/fc.c21
-rw-r--r--net/802/fddi.c26
-rw-r--r--net/802/hippi.c28
-rw-r--r--net/8021q/vlan.c16
-rw-r--r--net/8021q/vlan_dev.c47
-rw-r--r--net/9p/client.c262
-rw-r--r--net/9p/protocol.c28
-rw-r--r--net/9p/trans_common.c42
-rw-r--r--net/9p/trans_common.h2
-rw-r--r--net/9p/trans_fd.c7
-rw-r--r--net/9p/trans_rdma.c52
-rw-r--r--net/9p/trans_virtio.c142
-rw-r--r--net/Makefile2
-rw-r--r--net/appletalk/aarp.c6
-rw-r--r--net/appletalk/ddp.c7
-rw-r--r--net/atm/common.c7
-rw-r--r--net/atm/common.h7
-rw-r--r--net/atm/lec.c4
-rw-r--r--net/atm/mpoa_proc.c2
-rw-r--r--net/atm/signaling.c24
-rw-r--r--net/ax25/af_ax25.c7
-rw-r--r--net/ax25/ax25_ip.c30
-rw-r--r--net/batman-adv/gateway_client.c19
-rw-r--r--net/batman-adv/hard-interface.c5
-rw-r--r--net/bluetooth/Kconfig8
-rw-r--r--net/bluetooth/Makefile3
-rw-r--r--net/bluetooth/a2mp.c44
-rw-r--r--net/bluetooth/a2mp.h8
-rw-r--r--net/bluetooth/af_bluetooth.c20
-rw-r--r--net/bluetooth/bnep/bnep.h4
-rw-r--r--net/bluetooth/bnep/core.c70
-rw-r--r--net/bluetooth/bnep/netdev.c2
-rw-r--r--net/bluetooth/bnep/sock.c7
-rw-r--r--net/bluetooth/cmtp/capi.c2
-rw-r--r--net/bluetooth/cmtp/core.c15
-rw-r--r--net/bluetooth/hci_conn.c34
-rw-r--r--net/bluetooth/hci_core.c578
-rw-r--r--net/bluetooth/hci_debugfs.c98
-rw-r--r--net/bluetooth/hci_debugfs.h22
-rw-r--r--net/bluetooth/hci_event.c517
-rw-r--r--net/bluetooth/hci_request.c48
-rw-r--r--net/bluetooth/hci_request.h5
-rw-r--r--net/bluetooth/hci_sock.c329
-rw-r--r--net/bluetooth/hidp/core.c16
-rw-r--r--net/bluetooth/l2cap_core.c98
-rw-r--r--net/bluetooth/l2cap_sock.c18
-rw-r--r--net/bluetooth/mgmt.c3652
-rw-r--r--net/bluetooth/mgmt_util.c210
-rw-r--r--net/bluetooth/mgmt_util.h53
-rw-r--r--net/bluetooth/rfcomm/sock.c10
-rw-r--r--net/bluetooth/sco.c33
-rw-r--r--net/bluetooth/selftest.c35
-rw-r--r--net/bluetooth/smp.c415
-rw-r--r--net/bluetooth/smp.h1
-rw-r--r--net/bridge/br_device.c10
-rw-r--r--net/bridge/br_forward.c20
-rw-r--r--net/bridge/br_input.c33
-rw-r--r--net/bridge/br_multicast.c3
-rw-r--r--net/bridge/br_netfilter.c319
-rw-r--r--net/bridge/br_netlink.c131
-rw-r--r--net/bridge/br_nf_core.c1
-rw-r--r--net/bridge/br_private.h12
-rw-r--r--net/bridge/br_stp_bpdu.c5
-rw-r--r--net/bridge/br_sysfs_if.c2
-rw-r--r--net/bridge/netfilter/ebtable_filter.c14
-rw-r--r--net/bridge/netfilter/ebtable_nat.c14
-rw-r--r--net/bridge/netfilter/nf_tables_bridge.c28
-rw-r--r--net/bridge/netfilter/nft_meta_bridge.c26
-rw-r--r--net/bridge/netfilter/nft_reject_bridge.c92
-rw-r--r--net/caif/caif_socket.c17
-rw-r--r--net/can/bcm.c9
-rw-r--r--net/can/raw.c63
-rw-r--r--net/ceph/messenger.c9
-rw-r--r--net/compat.c30
-rw-r--r--net/core/datagram.c2
-rw-r--r--net/core/dev.c152
-rw-r--r--net/core/ethtool.c2
-rw-r--r--net/core/fib_rules.c25
-rw-r--r--net/core/filter.c483
-rw-r--r--net/core/link_watch.c4
-rw-r--r--net/core/neighbour.c112
-rw-r--r--net/core/net-sysfs.c125
-rw-r--r--net/core/net_namespace.c115
-rw-r--r--net/core/request_sock.c45
-rw-r--r--net/core/rtnetlink.c121
-rw-r--r--net/core/skbuff.c65
-rw-r--r--net/core/sock.c109
-rw-r--r--net/core/sock_diag.c37
-rw-r--r--net/core/sysctl_net_core.c2
-rw-r--r--net/dcb/dcbnl.c44
-rw-r--r--net/dccp/dccp.h12
-rw-r--r--net/dccp/diag.c7
-rw-r--r--net/dccp/ipv4.c99
-rw-r--r--net/dccp/ipv6.c86
-rw-r--r--net/dccp/minisocks.c26
-rw-r--r--net/dccp/probe.c3
-rw-r--r--net/dccp/proto.c7
-rw-r--r--net/dccp/timer.c24
-rw-r--r--net/decnet/af_decnet.c7
-rw-r--r--net/decnet/dn_neigh.c137
-rw-r--r--net/decnet/dn_nsp_in.c5
-rw-r--r--net/decnet/dn_route.c36
-rw-r--r--net/decnet/netfilter/dn_rtmsg.c4
-rw-r--r--net/dsa/Kconfig7
-rw-r--r--net/dsa/dsa.c257
-rw-r--r--net/dsa/dsa_priv.h9
-rw-r--r--net/dsa/slave.c327
-rw-r--r--net/ethernet/eth.c38
-rw-r--r--net/ieee802154/6lowpan/core.c8
-rw-r--r--net/ieee802154/core.c6
-rw-r--r--net/ieee802154/nl-mac.c1
-rw-r--r--net/ieee802154/socket.c21
-rw-r--r--net/ieee802154/sysfs.c49
-rw-r--r--net/ipv4/af_inet.c21
-rw-r--r--net/ipv4/arp.c150
-rw-r--r--net/ipv4/cipso_ipv4.c42
-rw-r--r--net/ipv4/devinet.c91
-rw-r--r--net/ipv4/esp4.c2
-rw-r--r--net/ipv4/fib_frontend.c134
-rw-r--r--net/ipv4/fib_lookup.h4
-rw-r--r--net/ipv4/fib_rules.c39
-rw-r--r--net/ipv4/fib_semantics.c43
-rw-r--r--net/ipv4/fib_trie.c1767
-rw-r--r--net/ipv4/fou.c233
-rw-r--r--net/ipv4/geneve.c14
-rw-r--r--net/ipv4/gre_offload.c4
-rw-r--r--net/ipv4/icmp.c6
-rw-r--r--net/ipv4/igmp.c72
-rw-r--r--net/ipv4/inet_connection_sock.c197
-rw-r--r--net/ipv4/inet_diag.c462
-rw-r--r--net/ipv4/inet_fragment.c4
-rw-r--r--net/ipv4/inet_hashtables.c72
-rw-r--r--net/ipv4/inet_timewait_sock.c277
-rw-r--r--net/ipv4/ip_forward.c8
-rw-r--r--net/ipv4/ip_fragment.c14
-rw-r--r--net/ipv4/ip_gre.c14
-rw-r--r--net/ipv4/ip_input.c17
-rw-r--r--net/ipv4/ip_options.c2
-rw-r--r--net/ipv4/ip_output.c89
-rw-r--r--net/ipv4/ip_sockglue.c34
-rw-r--r--net/ipv4/ip_tunnel.c21
-rw-r--r--net/ipv4/ip_tunnel_core.c3
-rw-r--r--net/ipv4/ip_vti.c12
-rw-r--r--net/ipv4/ipcomp.c2
-rw-r--r--net/ipv4/ipconfig.c6
-rw-r--r--net/ipv4/ipip.c12
-rw-r--r--net/ipv4/ipmr.c81
-rw-r--r--net/ipv4/netfilter.c4
-rw-r--r--net/ipv4/netfilter/Kconfig38
-rw-r--r--net/ipv4/netfilter/arp_tables.c11
-rw-r--r--net/ipv4/netfilter/arptable_filter.c7
-rw-r--r--net/ipv4/netfilter/ip_tables.c13
-rw-r--r--net/ipv4/netfilter/ipt_CLUSTERIP.c17
-rw-r--r--net/ipv4/netfilter/ipt_REJECT.c17
-rw-r--r--net/ipv4/netfilter/ipt_SYNPROXY.c6
-rw-r--r--net/ipv4/netfilter/iptable_filter.c8
-rw-r--r--net/ipv4/netfilter/iptable_mangle.c19
-rw-r--r--net/ipv4/netfilter/iptable_nat.c29
-rw-r--r--net/ipv4/netfilter/iptable_raw.c7
-rw-r--r--net/ipv4/netfilter/iptable_security.c8
-rw-r--r--net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c28
-rw-r--r--net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c4
-rw-r--r--net/ipv4/netfilter/nf_defrag_ipv4.c4
-rw-r--r--net/ipv4/netfilter/nf_log_arp.c4
-rw-r--r--net/ipv4/netfilter/nf_log_ipv4.c4
-rw-r--r--net/ipv4/netfilter/nf_nat_l3proto_ipv4.c33
-rw-r--r--net/ipv4/netfilter/nf_reject_ipv4.c29
-rw-r--r--net/ipv4/netfilter/nf_tables_arp.c6
-rw-r--r--net/ipv4/netfilter/nf_tables_ipv4.c12
-rw-r--r--net/ipv4/netfilter/nft_chain_nat_ipv4.c29
-rw-r--r--net/ipv4/netfilter/nft_chain_route_ipv4.c6
-rw-r--r--net/ipv4/netfilter/nft_masq_ipv4.c9
-rw-r--r--net/ipv4/netfilter/nft_redir_ipv4.c11
-rw-r--r--net/ipv4/netfilter/nft_reject_ipv4.c9
-rw-r--r--net/ipv4/ping.c17
-rw-r--r--net/ipv4/proc.c2
-rw-r--r--net/ipv4/raw.c20
-rw-r--r--net/ipv4/route.c40
-rw-r--r--net/ipv4/syncookies.c24
-rw-r--r--net/ipv4/sysctl_net_ipv4.c20
-rw-r--r--net/ipv4/tcp.c67
-rw-r--r--net/ipv4/tcp_cong.c2
-rw-r--r--net/ipv4/tcp_dctcp.c5
-rw-r--r--net/ipv4/tcp_diag.c6
-rw-r--r--net/ipv4/tcp_fastopen.c20
-rw-r--r--net/ipv4/tcp_illinois.c6
-rw-r--r--net/ipv4/tcp_input.c218
-rw-r--r--net/ipv4/tcp_ipv4.c223
-rw-r--r--net/ipv4/tcp_metrics.c208
-rw-r--r--net/ipv4/tcp_minisocks.c61
-rw-r--r--net/ipv4/tcp_offload.c4
-rw-r--r--net/ipv4/tcp_output.c177
-rw-r--r--net/ipv4/tcp_timer.c21
-rw-r--r--net/ipv4/tcp_vegas.c5
-rw-r--r--net/ipv4/tcp_vegas.h2
-rw-r--r--net/ipv4/tcp_westwood.c6
-rw-r--r--net/ipv4/udp.c44
-rw-r--r--net/ipv4/udp_diag.c24
-rw-r--r--net/ipv4/udp_impl.h4
-rw-r--r--net/ipv4/udp_offload.c4
-rw-r--r--net/ipv4/udp_tunnel.c4
-rw-r--r--net/ipv4/xfrm4_input.c7
-rw-r--r--net/ipv4/xfrm4_mode_tunnel.c2
-rw-r--r--net/ipv4/xfrm4_output.c12
-rw-r--r--net/ipv4/xfrm4_policy.c3
-rw-r--r--net/ipv6/addrconf.c433
-rw-r--r--net/ipv6/addrconf_core.c2
-rw-r--r--net/ipv6/addrlabel.c13
-rw-r--r--net/ipv6/af_inet6.c18
-rw-r--r--net/ipv6/ah6.c2
-rw-r--r--net/ipv6/anycast.c22
-rw-r--r--net/ipv6/datagram.c6
-rw-r--r--net/ipv6/esp6.c2
-rw-r--r--net/ipv6/exthdrs_core.c10
-rw-r--r--net/ipv6/fib6_rules.c21
-rw-r--r--net/ipv6/icmp.c13
-rw-r--r--net/ipv6/inet6_connection_sock.c23
-rw-r--r--net/ipv6/inet6_hashtables.c60
-rw-r--r--net/ipv6/ip6_fib.c4
-rw-r--r--net/ipv6/ip6_flowlabel.c25
-rw-r--r--net/ipv6/ip6_gre.c34
-rw-r--r--net/ipv6/ip6_input.c13
-rw-r--r--net/ipv6/ip6_offload.c2
-rw-r--r--net/ipv6/ip6_output.c63
-rw-r--r--net/ipv6/ip6_tunnel.c42
-rw-r--r--net/ipv6/ip6_udp_tunnel.c5
-rw-r--r--net/ipv6/ip6_vti.c30
-rw-r--r--net/ipv6/ip6mr.c74
-rw-r--r--net/ipv6/ipv6_sockglue.c42
-rw-r--r--net/ipv6/mcast.c75
-rw-r--r--net/ipv6/ndisc.c36
-rw-r--r--net/ipv6/netfilter.c4
-rw-r--r--net/ipv6/netfilter/Kconfig18
-rw-r--r--net/ipv6/netfilter/ip6_tables.c18
-rw-r--r--net/ipv6/netfilter/ip6t_REJECT.c3
-rw-r--r--net/ipv6/netfilter/ip6t_SYNPROXY.c6
-rw-r--r--net/ipv6/netfilter/ip6table_filter.c8
-rw-r--r--net/ipv6/netfilter/ip6table_mangle.c19
-rw-r--r--net/ipv6/netfilter/ip6table_nat.c29
-rw-r--r--net/ipv6/netfilter/ip6table_raw.c8
-rw-r--r--net/ipv6/netfilter/ip6table_security.c8
-rw-r--r--net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c32
-rw-r--r--net/ipv6/netfilter/nf_defrag_ipv6_hooks.c10
-rw-r--r--net/ipv6/netfilter/nf_log_ipv6.c4
-rw-r--r--net/ipv6/netfilter/nf_nat_l3proto_ipv6.c32
-rw-r--r--net/ipv6/netfilter/nf_reject_ipv6.c41
-rw-r--r--net/ipv6/netfilter/nf_tables_ipv6.c12
-rw-r--r--net/ipv6/netfilter/nft_chain_nat_ipv6.c29
-rw-r--r--net/ipv6/netfilter/nft_chain_route_ipv6.c6
-rw-r--r--net/ipv6/netfilter/nft_masq_ipv6.c7
-rw-r--r--net/ipv6/netfilter/nft_redir_ipv6.c11
-rw-r--r--net/ipv6/netfilter/nft_reject_ipv6.c6
-rw-r--r--net/ipv6/output_core.c37
-rw-r--r--net/ipv6/ping.c3
-rw-r--r--net/ipv6/raw.c28
-rw-r--r--net/ipv6/reassembly.c8
-rw-r--r--net/ipv6/route.c40
-rw-r--r--net/ipv6/sit.c42
-rw-r--r--net/ipv6/syncookies.c12
-rw-r--r--net/ipv6/sysctl_net_ipv6.c18
-rw-r--r--net/ipv6/tcp_ipv6.c169
-rw-r--r--net/ipv6/tcpv6_offload.c4
-rw-r--r--net/ipv6/udp.c43
-rw-r--r--net/ipv6/udp_impl.h7
-rw-r--r--net/ipv6/udp_offload.c4
-rw-r--r--net/ipv6/xfrm6_input.c3
-rw-r--r--net/ipv6/xfrm6_mode_beet.c4
-rw-r--r--net/ipv6/xfrm6_output.c15
-rw-r--r--net/ipv6/xfrm6_policy.c7
-rw-r--r--net/ipx/af_ipx.c7
-rw-r--r--net/irda/af_irda.c29
-rw-r--r--net/iucv/af_iucv.c8
-rw-r--r--net/key/af_key.c8
-rw-r--r--net/l2tp/l2tp_eth.c2
-rw-r--r--net/l2tp/l2tp_ip.c4
-rw-r--r--net/l2tp/l2tp_ip6.c8
-rw-r--r--net/l2tp/l2tp_netlink.c18
-rw-r--r--net/l2tp/l2tp_ppp.c7
-rw-r--r--net/llc/af_llc.c7
-rw-r--r--net/mac80211/aes_ccm.c12
-rw-r--r--net/mac80211/aes_gcm.c12
-rw-r--r--net/mac80211/aes_gmac.c4
-rw-r--r--net/mac80211/agg-rx.c8
-rw-r--r--net/mac80211/agg-tx.c58
-rw-r--r--net/mac80211/cfg.c99
-rw-r--r--net/mac80211/debugfs.c170
-rw-r--r--net/mac80211/debugfs_netdev.c2
-rw-r--r--net/mac80211/debugfs_sta.c134
-rw-r--r--net/mac80211/driver-ops.h24
-rw-r--r--net/mac80211/ht.c2
-rw-r--r--net/mac80211/ibss.c343
-rw-r--r--net/mac80211/ieee80211_i.h67
-rw-r--r--net/mac80211/iface.c27
-rw-r--r--net/mac80211/key.c1
-rw-r--r--net/mac80211/key.h2
-rw-r--r--net/mac80211/main.c16
-rw-r--r--net/mac80211/mesh.c5
-rw-r--r--net/mac80211/mesh_plink.c10
-rw-r--r--net/mac80211/mlme.c236
-rw-r--r--net/mac80211/pm.c28
-rw-r--r--net/mac80211/rc80211_minstrel.c125
-rw-r--r--net/mac80211/rc80211_minstrel.h49
-rw-r--r--net/mac80211/rc80211_minstrel_debugfs.c125
-rw-r--r--net/mac80211/rc80211_minstrel_ht.c223
-rw-r--r--net/mac80211/rc80211_minstrel_ht.h5
-rw-r--r--net/mac80211/rc80211_minstrel_ht_debugfs.c216
-rw-r--r--net/mac80211/rx.c39
-rw-r--r--net/mac80211/scan.c25
-rw-r--r--net/mac80211/sta_info.c226
-rw-r--r--net/mac80211/sta_info.h62
-rw-r--r--net/mac80211/status.c82
-rw-r--r--net/mac80211/tdls.c155
-rw-r--r--net/mac80211/trace.c1
-rw-r--r--net/mac80211/trace.h63
-rw-r--r--net/mac80211/trace_msg.h53
-rw-r--r--net/mac80211/tx.c381
-rw-r--r--net/mac80211/util.c190
-rw-r--r--net/mac80211/vht.c4
-rw-r--r--net/mac80211/wpa.c13
-rw-r--r--net/mac802154/driver-ops.h4
-rw-r--r--net/mac802154/iface.c20
-rw-r--r--net/mac802154/util.c13
-rw-r--r--net/mpls/Kconfig21
-rw-r--r--net/mpls/Makefile3
-rw-r--r--net/mpls/af_mpls.c1023
-rw-r--r--net/mpls/internal.h59
-rw-r--r--net/netfilter/Kconfig23
-rw-r--r--net/netfilter/Makefile2
-rw-r--r--net/netfilter/core.c31
-rw-r--r--net/netfilter/ipset/ip_set_hash_netiface.c32
-rw-r--r--net/netfilter/ipvs/ip_vs_core.c101
-rw-r--r--net/netfilter/ipvs/ip_vs_ctl.c182
-rw-r--r--net/netfilter/ipvs/ip_vs_est.c102
-rw-r--r--net/netfilter/ipvs/ip_vs_sync.c23
-rw-r--r--net/netfilter/ipvs/ip_vs_xmit.c15
-rw-r--r--net/netfilter/nf_conntrack_acct.c8
-rw-r--r--net/netfilter/nf_conntrack_amanda.c10
-rw-r--r--net/netfilter/nf_conntrack_expect.c4
-rw-r--r--net/netfilter/nf_internals.h11
-rw-r--r--net/netfilter/nf_log_common.c7
-rw-r--r--net/netfilter/nf_queue.c76
-rw-r--r--net/netfilter/nf_tables_api.c636
-rw-r--r--net/netfilter/nf_tables_core.c161
-rw-r--r--net/netfilter/nfnetlink_log.c35
-rw-r--r--net/netfilter/nfnetlink_queue_core.c69
-rw-r--r--net/netfilter/nft_bitwise.c37
-rw-r--r--net/netfilter/nft_byteorder.c40
-rw-r--r--net/netfilter/nft_cmp.c44
-rw-r--r--net/netfilter/nft_compat.c41
-rw-r--r--net/netfilter/nft_counter.c3
-rw-r--r--net/netfilter/nft_ct.c118
-rw-r--r--net/netfilter/nft_dynset.c265
-rw-r--r--net/netfilter/nft_expr_template.c94
-rw-r--r--net/netfilter/nft_exthdr.c23
-rw-r--r--net/netfilter/nft_hash.c327
-rw-r--r--net/netfilter/nft_immediate.c18
-rw-r--r--net/netfilter/nft_limit.c5
-rw-r--r--net/netfilter/nft_log.c4
-rw-r--r--net/netfilter/nft_lookup.c35
-rw-r--r--net/netfilter/nft_meta.c116
-rw-r--r--net/netfilter/nft_nat.c71
-rw-r--r--net/netfilter/nft_payload.c24
-rw-r--r--net/netfilter/nft_queue.c4
-rw-r--r--net/netfilter/nft_rbtree.c132
-rw-r--r--net/netfilter/nft_redir.c19
-rw-r--r--net/netfilter/nft_reject_inet.c11
-rw-r--r--net/netfilter/xt_TPROXY.c22
-rw-r--r--net/netfilter/xt_cgroup.c2
-rw-r--r--net/netfilter/xt_physdev.c35
-rw-r--r--net/netfilter/xt_set.c4
-rw-r--r--net/netfilter/xt_socket.c129
-rw-r--r--net/netfilter/xt_string.c3
-rw-r--r--net/netlabel/netlabel_mgmt.c20
-rw-r--r--net/netlabel/netlabel_unlabeled.c28
-rw-r--r--net/netlink/af_netlink.c95
-rw-r--r--net/netrom/af_netrom.c7
-rw-r--r--net/netrom/nr_dev.c31
-rw-r--r--net/nfc/llcp_sock.c8
-rw-r--r--net/nfc/nci/core.c11
-rw-r--r--net/nfc/netlink.c2
-rw-r--r--net/nfc/rawsock.c7
-rw-r--r--net/openvswitch/Kconfig1
-rw-r--r--net/openvswitch/datapath.c4
-rw-r--r--net/openvswitch/datapath.h4
-rw-r--r--net/openvswitch/flow.c4
-rw-r--r--net/openvswitch/flow_netlink.c10
-rw-r--r--net/openvswitch/vport-vxlan.c5
-rw-r--r--net/packet/af_packet.c62
-rw-r--r--net/packet/internal.h4
-rw-r--r--net/phonet/datagram.c8
-rw-r--r--net/phonet/pep.c8
-rw-r--r--net/phonet/socket.c6
-rw-r--r--net/rds/connection.c3
-rw-r--r--net/rds/rds.h8
-rw-r--r--net/rds/recv.c4
-rw-r--r--net/rds/send.c36
-rw-r--r--net/rose/af_rose.c7
-rw-r--r--net/rose/rose_dev.c53
-rw-r--r--net/rxrpc/af_rxrpc.c7
-rw-r--r--net/rxrpc/ar-input.c23
-rw-r--r--net/rxrpc/ar-internal.h12
-rw-r--r--net/rxrpc/ar-local.c98
-rw-r--r--net/rxrpc/ar-output.c73
-rw-r--r--net/rxrpc/ar-recvmsg.c6
-rw-r--r--net/sched/act_bpf.c298
-rw-r--r--net/sched/act_mirred.c2
-rw-r--r--net/sched/cls_api.c14
-rw-r--r--net/sched/cls_basic.c6
-rw-r--r--net/sched/cls_bpf.c224
-rw-r--r--net/sched/cls_cgroup.c6
-rw-r--r--net/sched/cls_flow.c6
-rw-r--r--net/sched/cls_fw.c34
-rw-r--r--net/sched/cls_route.c26
-rw-r--r--net/sched/cls_rsvp.h12
-rw-r--r--net/sched/cls_tcindex.c6
-rw-r--r--net/sched/cls_u32.c25
-rw-r--r--net/sched/em_text.c3
-rw-r--r--net/sched/sch_api.c14
-rw-r--r--net/sched/sch_fq.c4
-rw-r--r--net/sched/sch_ingress.c9
-rw-r--r--net/sched/sch_netem.c3
-rw-r--r--net/sctp/protocol.c3
-rw-r--r--net/sctp/socket.c13
-rw-r--r--net/sctp/sysctl.c4
-rw-r--r--net/socket.c178
-rw-r--r--net/sunrpc/Kconfig2
-rw-r--r--net/sunrpc/cache.c8
-rw-r--r--net/sunrpc/svcsock.c2
-rw-r--r--net/sunrpc/xprtrdma/verbs.c4
-rw-r--r--net/switchdev/switchdev.c217
-rw-r--r--net/tipc/Kconfig8
-rw-r--r--net/tipc/Makefile1
-rw-r--r--net/tipc/addr.c7
-rw-r--r--net/tipc/addr.h1
-rw-r--r--net/tipc/bcast.c95
-rw-r--r--net/tipc/bcast.h4
-rw-r--r--net/tipc/bearer.c13
-rw-r--r--net/tipc/bearer.h17
-rw-r--r--net/tipc/discover.c11
-rw-r--r--net/tipc/eth_media.c8
-rw-r--r--net/tipc/ib_media.c2
-rw-r--r--net/tipc/link.c862
-rw-r--r--net/tipc/link.h51
-rw-r--r--net/tipc/msg.c130
-rw-r--r--net/tipc/msg.h133
-rw-r--r--net/tipc/name_distr.c4
-rw-r--r--net/tipc/name_table.c4
-rw-r--r--net/tipc/node.c116
-rw-r--r--net/tipc/node.h18
-rw-r--r--net/tipc/server.c47
-rw-r--r--net/tipc/socket.c272
-rw-r--r--net/tipc/socket.h4
-rw-r--r--net/tipc/subscr.c23
-rw-r--r--net/tipc/udp_media.c448
-rw-r--r--net/unix/af_unix.c50
-rw-r--r--net/vmw_vsock/af_vsock.c20
-rw-r--r--net/vmw_vsock/vmci_transport.c3
-rw-r--r--net/wireless/Kconfig10
-rw-r--r--net/wireless/ibss.c4
-rw-r--r--net/wireless/mlme.c6
-rw-r--r--net/wireless/nl80211.c98
-rw-r--r--net/wireless/rdev-ops.h5
-rw-r--r--net/wireless/reg.c145
-rw-r--r--net/wireless/reg.h24
-rw-r--r--net/wireless/scan.c302
-rw-r--r--net/wireless/sme.c90
-rw-r--r--net/wireless/trace.h41
-rw-r--r--net/wireless/util.c171
-rw-r--r--net/wireless/wext-compat.c18
-rw-r--r--net/wireless/wext-compat.h6
-rw-r--r--net/wireless/wext-sme.c2
-rw-r--r--net/x25/af_x25.c6
-rw-r--r--net/xfrm/xfrm_input.c10
-rw-r--r--net/xfrm/xfrm_output.c16
-rw-r--r--net/xfrm/xfrm_state.c8
-rw-r--r--net/xfrm/xfrm_user.c5
488 files changed, 19903 insertions, 11872 deletions
diff --git a/net/6lowpan/Kconfig b/net/6lowpan/Kconfig
index e4a02ef55102..7fa0f382e7d1 100644
--- a/net/6lowpan/Kconfig
+++ b/net/6lowpan/Kconfig
@@ -1,6 +1,61 @@
1config 6LOWPAN 1menuconfig 6LOWPAN
2 tristate "6LoWPAN Support" 2 tristate "6LoWPAN Support"
3 depends on IPV6 3 depends on IPV6
4 ---help--- 4 ---help---
5 This enables IPv6 over Low power Wireless Personal Area Network - 5 This enables IPv6 over Low power Wireless Personal Area Network -
6 "6LoWPAN" which is supported by IEEE 802.15.4 or Bluetooth stacks. 6 "6LoWPAN" which is supported by IEEE 802.15.4 or Bluetooth stacks.
7
8menuconfig 6LOWPAN_NHC
9 tristate "Next Header Compression Support"
10 depends on 6LOWPAN
11 default y
12 ---help---
13 Support for next header compression.
14
15if 6LOWPAN_NHC
16
17config 6LOWPAN_NHC_DEST
18 tristate "Destination Options Header Support"
19 default y
20 ---help---
21 6LoWPAN IPv6 Destination Options Header compression according to
22 RFC6282.
23
24config 6LOWPAN_NHC_FRAGMENT
25 tristate "Fragment Header Support"
26 default y
27 ---help---
28 6LoWPAN IPv6 Fragment Header compression according to RFC6282.
29
30config 6LOWPAN_NHC_HOP
31 tristate "Hop-by-Hop Options Header Support"
32 default y
33 ---help---
34 6LoWPAN IPv6 Hop-by-Hop Options Header compression according to
35 RFC6282.
36
37config 6LOWPAN_NHC_IPV6
38 tristate "IPv6 Header Support"
39 default y
40 ---help---
41 6LoWPAN IPv6 Header compression according to RFC6282.
42
43config 6LOWPAN_NHC_MOBILITY
44 tristate "Mobility Header Support"
45 default y
46 ---help---
47 6LoWPAN IPv6 Mobility Header compression according to RFC6282.
48
49config 6LOWPAN_NHC_ROUTING
50 tristate "Routing Header Support"
51 default y
52 ---help---
53 6LoWPAN IPv6 Routing Header compression according to RFC6282.
54
55config 6LOWPAN_NHC_UDP
56 tristate "UDP Header Support"
57 default y
58 ---help---
59 6LoWPAN IPv6 UDP Header compression according to RFC6282.
60
61endif
diff --git a/net/6lowpan/Makefile b/net/6lowpan/Makefile
index 415886bb456a..eb8baa72adc8 100644
--- a/net/6lowpan/Makefile
+++ b/net/6lowpan/Makefile
@@ -1,3 +1,12 @@
1obj-$(CONFIG_6LOWPAN) := 6lowpan.o 1obj-$(CONFIG_6LOWPAN) += 6lowpan.o
2 2
36lowpan-y := iphc.o 36lowpan-y := iphc.o nhc.o
4
5#rfc6282 nhcs
6obj-$(CONFIG_6LOWPAN_NHC_DEST) += nhc_dest.o
7obj-$(CONFIG_6LOWPAN_NHC_FRAGMENT) += nhc_fragment.o
8obj-$(CONFIG_6LOWPAN_NHC_HOP) += nhc_hop.o
9obj-$(CONFIG_6LOWPAN_NHC_IPV6) += nhc_ipv6.o
10obj-$(CONFIG_6LOWPAN_NHC_MOBILITY) += nhc_mobility.o
11obj-$(CONFIG_6LOWPAN_NHC_ROUTING) += nhc_routing.o
12obj-$(CONFIG_6LOWPAN_NHC_UDP) += nhc_udp.o
diff --git a/net/6lowpan/iphc.c b/net/6lowpan/iphc.c
index 32ffec6ef164..94a375c04f21 100644
--- a/net/6lowpan/iphc.c
+++ b/net/6lowpan/iphc.c
@@ -54,6 +54,8 @@
54#include <net/ipv6.h> 54#include <net/ipv6.h>
55#include <net/af_ieee802154.h> 55#include <net/af_ieee802154.h>
56 56
57#include "nhc.h"
58
57/* Uncompress address function for source and 59/* Uncompress address function for source and
58 * destination address(non-multicast). 60 * destination address(non-multicast).
59 * 61 *
@@ -224,77 +226,6 @@ static int lowpan_uncompress_multicast_daddr(struct sk_buff *skb,
224 return 0; 226 return 0;
225} 227}
226 228
227static int uncompress_udp_header(struct sk_buff *skb, struct udphdr *uh)
228{
229 bool fail;
230 u8 tmp = 0, val = 0;
231
232 fail = lowpan_fetch_skb(skb, &tmp, sizeof(tmp));
233
234 if ((tmp & LOWPAN_NHC_UDP_MASK) == LOWPAN_NHC_UDP_ID) {
235 pr_debug("UDP header uncompression\n");
236 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
237 case LOWPAN_NHC_UDP_CS_P_00:
238 fail |= lowpan_fetch_skb(skb, &uh->source,
239 sizeof(uh->source));
240 fail |= lowpan_fetch_skb(skb, &uh->dest,
241 sizeof(uh->dest));
242 break;
243 case LOWPAN_NHC_UDP_CS_P_01:
244 fail |= lowpan_fetch_skb(skb, &uh->source,
245 sizeof(uh->source));
246 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
247 uh->dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
248 break;
249 case LOWPAN_NHC_UDP_CS_P_10:
250 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
251 uh->source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
252 fail |= lowpan_fetch_skb(skb, &uh->dest,
253 sizeof(uh->dest));
254 break;
255 case LOWPAN_NHC_UDP_CS_P_11:
256 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
257 uh->source = htons(LOWPAN_NHC_UDP_4BIT_PORT +
258 (val >> 4));
259 uh->dest = htons(LOWPAN_NHC_UDP_4BIT_PORT +
260 (val & 0x0f));
261 break;
262 default:
263 pr_debug("ERROR: unknown UDP format\n");
264 goto err;
265 }
266
267 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
268 ntohs(uh->source), ntohs(uh->dest));
269
270 /* checksum */
271 if (tmp & LOWPAN_NHC_UDP_CS_C) {
272 pr_debug_ratelimited("checksum elided currently not supported\n");
273 goto err;
274 } else {
275 fail |= lowpan_fetch_skb(skb, &uh->check,
276 sizeof(uh->check));
277 }
278
279 /* UDP length needs to be infered from the lower layers
280 * here, we obtain the hint from the remaining size of the
281 * frame
282 */
283 uh->len = htons(skb->len + sizeof(struct udphdr));
284 pr_debug("uncompressed UDP length: src = %d", ntohs(uh->len));
285 } else {
286 pr_debug("ERROR: unsupported NH format\n");
287 goto err;
288 }
289
290 if (fail)
291 goto err;
292
293 return 0;
294err:
295 return -EINVAL;
296}
297
298/* TTL uncompression values */ 229/* TTL uncompression values */
299static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 }; 230static const u8 lowpan_ttl_values[] = { 0, 1, 64, 255 };
300 231
@@ -425,29 +356,11 @@ lowpan_header_decompress(struct sk_buff *skb, struct net_device *dev,
425 return -EINVAL; 356 return -EINVAL;
426 } 357 }
427 358
428 /* UDP data uncompression */ 359 /* Next header data uncompression */
429 if (iphc0 & LOWPAN_IPHC_NH_C) { 360 if (iphc0 & LOWPAN_IPHC_NH_C) {
430 struct udphdr uh; 361 err = lowpan_nhc_do_uncompression(skb, dev, &hdr);
431 const int needed = sizeof(struct udphdr) + sizeof(hdr); 362 if (err < 0)
432
433 if (uncompress_udp_header(skb, &uh))
434 return -EINVAL;
435
436 /* replace the compressed UDP head by the uncompressed UDP
437 * header
438 */
439 err = skb_cow(skb, needed);
440 if (unlikely(err))
441 return err; 363 return err;
442
443 skb_push(skb, sizeof(struct udphdr));
444 skb_reset_transport_header(skb);
445 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
446
447 raw_dump_table(__func__, "raw UDP header dump",
448 (u8 *)&uh, sizeof(uh));
449
450 hdr.nexthdr = UIP_PROTO_UDP;
451 } else { 364 } else {
452 err = skb_cow(skb, sizeof(hdr)); 365 err = skb_cow(skb, sizeof(hdr));
453 if (unlikely(err)) 366 if (unlikely(err))
@@ -500,71 +413,6 @@ static u8 lowpan_compress_addr_64(u8 **hc_ptr, u8 shift,
500 return rol8(val, shift); 413 return rol8(val, shift);
501} 414}
502 415
503static void compress_udp_header(u8 **hc_ptr, struct sk_buff *skb)
504{
505 struct udphdr *uh;
506 u8 tmp;
507
508 /* In the case of RAW sockets the transport header is not set by
509 * the ip6 stack so we must set it ourselves
510 */
511 if (skb->transport_header == skb->network_header)
512 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
513
514 uh = udp_hdr(skb);
515
516 if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
517 LOWPAN_NHC_UDP_4BIT_PORT) &&
518 ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
519 LOWPAN_NHC_UDP_4BIT_PORT)) {
520 pr_debug("UDP header: both ports compression to 4 bits\n");
521 /* compression value */
522 tmp = LOWPAN_NHC_UDP_CS_P_11;
523 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
524 /* source and destination port */
525 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
526 ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
527 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
528 } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
529 LOWPAN_NHC_UDP_8BIT_PORT) {
530 pr_debug("UDP header: remove 8 bits of dest\n");
531 /* compression value */
532 tmp = LOWPAN_NHC_UDP_CS_P_01;
533 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
534 /* source port */
535 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
536 /* destination port */
537 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
538 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
539 } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
540 LOWPAN_NHC_UDP_8BIT_PORT) {
541 pr_debug("UDP header: remove 8 bits of source\n");
542 /* compression value */
543 tmp = LOWPAN_NHC_UDP_CS_P_10;
544 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
545 /* source port */
546 tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
547 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
548 /* destination port */
549 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
550 } else {
551 pr_debug("UDP header: can't compress\n");
552 /* compression value */
553 tmp = LOWPAN_NHC_UDP_CS_P_00;
554 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
555 /* source port */
556 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
557 /* destination port */
558 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
559 }
560
561 /* checksum is always inline */
562 lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
563
564 /* skip the UDP header */
565 skb_pull(skb, sizeof(struct udphdr));
566}
567
568int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev, 416int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
569 unsigned short type, const void *_daddr, 417 unsigned short type, const void *_daddr,
570 const void *_saddr, unsigned int len) 418 const void *_saddr, unsigned int len)
@@ -572,7 +420,7 @@ int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
572 u8 tmp, iphc0, iphc1, *hc_ptr; 420 u8 tmp, iphc0, iphc1, *hc_ptr;
573 struct ipv6hdr *hdr; 421 struct ipv6hdr *hdr;
574 u8 head[100] = {}; 422 u8 head[100] = {};
575 int addr_type; 423 int ret, addr_type;
576 424
577 if (type != ETH_P_IPV6) 425 if (type != ETH_P_IPV6)
578 return -EINVAL; 426 return -EINVAL;
@@ -649,13 +497,12 @@ int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
649 497
650 /* NOTE: payload length is always compressed */ 498 /* NOTE: payload length is always compressed */
651 499
652 /* Next Header is compress if UDP */ 500 /* Check if we provide the nhc format for nexthdr and compression
653 if (hdr->nexthdr == UIP_PROTO_UDP) 501 * functionality. If not nexthdr is handled inline and not compressed.
654 iphc0 |= LOWPAN_IPHC_NH_C; 502 */
655 503 ret = lowpan_nhc_check_compression(skb, hdr, &hc_ptr, &iphc0);
656 if ((iphc0 & LOWPAN_IPHC_NH_C) == 0) 504 if (ret < 0)
657 lowpan_push_hc_data(&hc_ptr, &hdr->nexthdr, 505 return ret;
658 sizeof(hdr->nexthdr));
659 506
660 /* Hop limit 507 /* Hop limit
661 * if 1: compress, encoding is 01 508 * if 1: compress, encoding is 01
@@ -741,9 +588,12 @@ int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
741 } 588 }
742 } 589 }
743 590
744 /* UDP header compression */ 591 /* next header compression */
745 if (hdr->nexthdr == UIP_PROTO_UDP) 592 if (iphc0 & LOWPAN_IPHC_NH_C) {
746 compress_udp_header(&hc_ptr, skb); 593 ret = lowpan_nhc_do_compression(skb, hdr, &hc_ptr);
594 if (ret < 0)
595 return ret;
596 }
747 597
748 head[0] = iphc0; 598 head[0] = iphc0;
749 head[1] = iphc1; 599 head[1] = iphc1;
@@ -761,4 +611,18 @@ int lowpan_header_compress(struct sk_buff *skb, struct net_device *dev,
761} 611}
762EXPORT_SYMBOL_GPL(lowpan_header_compress); 612EXPORT_SYMBOL_GPL(lowpan_header_compress);
763 613
614static int __init lowpan_module_init(void)
615{
616 request_module_nowait("nhc_dest");
617 request_module_nowait("nhc_fragment");
618 request_module_nowait("nhc_hop");
619 request_module_nowait("nhc_ipv6");
620 request_module_nowait("nhc_mobility");
621 request_module_nowait("nhc_routing");
622 request_module_nowait("nhc_udp");
623
624 return 0;
625}
626module_init(lowpan_module_init);
627
764MODULE_LICENSE("GPL"); 628MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc.c b/net/6lowpan/nhc.c
new file mode 100644
index 000000000000..fd20fc51a7c4
--- /dev/null
+++ b/net/6lowpan/nhc.c
@@ -0,0 +1,241 @@
1/*
2 * 6LoWPAN next header compression
3 *
4 *
5 * Authors:
6 * Alexander Aring <aar@pengutronix.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
14#include <linux/netdevice.h>
15
16#include <net/ipv6.h>
17
18#include "nhc.h"
19
20static struct rb_root rb_root = RB_ROOT;
21static struct lowpan_nhc *lowpan_nexthdr_nhcs[NEXTHDR_MAX];
22static DEFINE_SPINLOCK(lowpan_nhc_lock);
23
24static int lowpan_nhc_insert(struct lowpan_nhc *nhc)
25{
26 struct rb_node **new = &rb_root.rb_node, *parent = NULL;
27
28 /* Figure out where to put new node */
29 while (*new) {
30 struct lowpan_nhc *this = container_of(*new, struct lowpan_nhc,
31 node);
32 int result, len_dif, len;
33
34 len_dif = nhc->idlen - this->idlen;
35
36 if (nhc->idlen < this->idlen)
37 len = nhc->idlen;
38 else
39 len = this->idlen;
40
41 result = memcmp(nhc->id, this->id, len);
42 if (!result)
43 result = len_dif;
44
45 parent = *new;
46 if (result < 0)
47 new = &((*new)->rb_left);
48 else if (result > 0)
49 new = &((*new)->rb_right);
50 else
51 return -EEXIST;
52 }
53
54 /* Add new node and rebalance tree. */
55 rb_link_node(&nhc->node, parent, new);
56 rb_insert_color(&nhc->node, &rb_root);
57
58 return 0;
59}
60
61static void lowpan_nhc_remove(struct lowpan_nhc *nhc)
62{
63 rb_erase(&nhc->node, &rb_root);
64}
65
66static struct lowpan_nhc *lowpan_nhc_by_nhcid(const struct sk_buff *skb)
67{
68 struct rb_node *node = rb_root.rb_node;
69 const u8 *nhcid_skb_ptr = skb->data;
70
71 while (node) {
72 struct lowpan_nhc *nhc = container_of(node, struct lowpan_nhc,
73 node);
74 u8 nhcid_skb_ptr_masked[LOWPAN_NHC_MAX_ID_LEN];
75 int result, i;
76
77 if (nhcid_skb_ptr + nhc->idlen > skb->data + skb->len)
78 return NULL;
79
80 /* copy and mask afterwards the nhid value from skb */
81 memcpy(nhcid_skb_ptr_masked, nhcid_skb_ptr, nhc->idlen);
82 for (i = 0; i < nhc->idlen; i++)
83 nhcid_skb_ptr_masked[i] &= nhc->idmask[i];
84
85 result = memcmp(nhcid_skb_ptr_masked, nhc->id, nhc->idlen);
86 if (result < 0)
87 node = node->rb_left;
88 else if (result > 0)
89 node = node->rb_right;
90 else
91 return nhc;
92 }
93
94 return NULL;
95}
96
97int lowpan_nhc_check_compression(struct sk_buff *skb,
98 const struct ipv6hdr *hdr, u8 **hc_ptr,
99 u8 *iphc0)
100{
101 struct lowpan_nhc *nhc;
102
103 spin_lock_bh(&lowpan_nhc_lock);
104
105 nhc = lowpan_nexthdr_nhcs[hdr->nexthdr];
106 if (nhc && nhc->compress)
107 *iphc0 |= LOWPAN_IPHC_NH_C;
108 else
109 lowpan_push_hc_data(hc_ptr, &hdr->nexthdr,
110 sizeof(hdr->nexthdr));
111
112 spin_unlock_bh(&lowpan_nhc_lock);
113
114 return 0;
115}
116
117int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
118 u8 **hc_ptr)
119{
120 int ret;
121 struct lowpan_nhc *nhc;
122
123 spin_lock_bh(&lowpan_nhc_lock);
124
125 nhc = lowpan_nexthdr_nhcs[hdr->nexthdr];
126 /* check if the nhc module was removed in unlocked part.
127 * TODO: this is a workaround we should prevent unloading
128 * of nhc modules while unlocked part, this will always drop
129 * the lowpan packet but it's very unlikely.
130 *
131 * Solution isn't easy because we need to decide at
132 * lowpan_nhc_check_compression if we do a compression or not.
133 * Because the inline data which is added to skb, we can't move this
134 * handling.
135 */
136 if (unlikely(!nhc || !nhc->compress)) {
137 ret = -EINVAL;
138 goto out;
139 }
140
141 /* In the case of RAW sockets the transport header is not set by
142 * the ip6 stack so we must set it ourselves
143 */
144 if (skb->transport_header == skb->network_header)
145 skb_set_transport_header(skb, sizeof(struct ipv6hdr));
146
147 ret = nhc->compress(skb, hc_ptr);
148 if (ret < 0)
149 goto out;
150
151 /* skip the transport header */
152 skb_pull(skb, nhc->nexthdrlen);
153
154out:
155 spin_unlock_bh(&lowpan_nhc_lock);
156
157 return ret;
158}
159
160int lowpan_nhc_do_uncompression(struct sk_buff *skb, struct net_device *dev,
161 struct ipv6hdr *hdr)
162{
163 struct lowpan_nhc *nhc;
164 int ret;
165
166 spin_lock_bh(&lowpan_nhc_lock);
167
168 nhc = lowpan_nhc_by_nhcid(skb);
169 if (nhc) {
170 if (nhc->uncompress) {
171 ret = nhc->uncompress(skb, sizeof(struct ipv6hdr) +
172 nhc->nexthdrlen);
173 if (ret < 0) {
174 spin_unlock_bh(&lowpan_nhc_lock);
175 return ret;
176 }
177 } else {
178 spin_unlock_bh(&lowpan_nhc_lock);
179 netdev_warn(dev, "received nhc id for %s which is not implemented.\n",
180 nhc->name);
181 return -ENOTSUPP;
182 }
183 } else {
184 spin_unlock_bh(&lowpan_nhc_lock);
185 netdev_warn(dev, "received unknown nhc id which was not found.\n");
186 return -ENOENT;
187 }
188
189 hdr->nexthdr = nhc->nexthdr;
190 skb_reset_transport_header(skb);
191 raw_dump_table(__func__, "raw transport header dump",
192 skb_transport_header(skb), nhc->nexthdrlen);
193
194 spin_unlock_bh(&lowpan_nhc_lock);
195
196 return 0;
197}
198
199int lowpan_nhc_add(struct lowpan_nhc *nhc)
200{
201 int ret;
202
203 if (!nhc->idlen || !nhc->idsetup)
204 return -EINVAL;
205
206 WARN_ONCE(nhc->idlen > LOWPAN_NHC_MAX_ID_LEN,
207 "LOWPAN_NHC_MAX_ID_LEN should be updated to %zd.\n",
208 nhc->idlen);
209
210 nhc->idsetup(nhc);
211
212 spin_lock_bh(&lowpan_nhc_lock);
213
214 if (lowpan_nexthdr_nhcs[nhc->nexthdr]) {
215 ret = -EEXIST;
216 goto out;
217 }
218
219 ret = lowpan_nhc_insert(nhc);
220 if (ret < 0)
221 goto out;
222
223 lowpan_nexthdr_nhcs[nhc->nexthdr] = nhc;
224out:
225 spin_unlock_bh(&lowpan_nhc_lock);
226 return ret;
227}
228EXPORT_SYMBOL(lowpan_nhc_add);
229
230void lowpan_nhc_del(struct lowpan_nhc *nhc)
231{
232 spin_lock_bh(&lowpan_nhc_lock);
233
234 lowpan_nhc_remove(nhc);
235 lowpan_nexthdr_nhcs[nhc->nexthdr] = NULL;
236
237 spin_unlock_bh(&lowpan_nhc_lock);
238
239 synchronize_net();
240}
241EXPORT_SYMBOL(lowpan_nhc_del);
diff --git a/net/6lowpan/nhc.h b/net/6lowpan/nhc.h
new file mode 100644
index 000000000000..ed44938eb5de
--- /dev/null
+++ b/net/6lowpan/nhc.h
@@ -0,0 +1,146 @@
1#ifndef __6LOWPAN_NHC_H
2#define __6LOWPAN_NHC_H
3
4#include <linux/skbuff.h>
5#include <linux/rbtree.h>
6#include <linux/module.h>
7
8#include <net/6lowpan.h>
9#include <net/ipv6.h>
10
11#define LOWPAN_NHC_MAX_ID_LEN 1
12
13/**
14 * LOWPAN_NHC - helper macro to generate nh id fields and lowpan_nhc struct
15 *
16 * @__nhc: variable name of the lowpan_nhc struct.
17 * @_name: const char * of common header compression name.
18 * @_nexthdr: ipv6 nexthdr field for the header compression.
19 * @_nexthdrlen: ipv6 nexthdr len for the reserved space.
20 * @_idsetup: callback to setup id and mask values.
21 * @_idlen: len for the next header id and mask, should be always the same.
22 * @_uncompress: callback for uncompression call.
23 * @_compress: callback for compression call.
24 */
25#define LOWPAN_NHC(__nhc, _name, _nexthdr, \
26 _hdrlen, _idsetup, _idlen, \
27 _uncompress, _compress) \
28static u8 __nhc##_val[_idlen]; \
29static u8 __nhc##_mask[_idlen]; \
30static struct lowpan_nhc __nhc = { \
31 .name = _name, \
32 .nexthdr = _nexthdr, \
33 .nexthdrlen = _hdrlen, \
34 .id = __nhc##_val, \
35 .idmask = __nhc##_mask, \
36 .idlen = _idlen, \
37 .idsetup = _idsetup, \
38 .uncompress = _uncompress, \
39 .compress = _compress, \
40}
41
42#define module_lowpan_nhc(__nhc) \
43static int __init __nhc##_init(void) \
44{ \
45 return lowpan_nhc_add(&(__nhc)); \
46} \
47module_init(__nhc##_init); \
48static void __exit __nhc##_exit(void) \
49{ \
50 lowpan_nhc_del(&(__nhc)); \
51} \
52module_exit(__nhc##_exit);
53
54/**
55 * struct lowpan_nhc - hold 6lowpan next hdr compression ifnformation
56 *
57 * @node: holder for the rbtree.
58 * @name: name of the specific next header compression
59 * @nexthdr: next header value of the protocol which should be compressed.
60 * @nexthdrlen: ipv6 nexthdr len for the reserved space.
61 * @id: array for nhc id. Note this need to be in network byteorder.
62 * @mask: array for nhc id mask. Note this need to be in network byteorder.
63 * @len: the length of the next header id and mask.
64 * @setup: callback to setup fill the next header id value and mask.
65 * @compress: callback to do the header compression.
66 * @uncompress: callback to do the header uncompression.
67 */
68struct lowpan_nhc {
69 struct rb_node node;
70 const char *name;
71 const u8 nexthdr;
72 const size_t nexthdrlen;
73 u8 *id;
74 u8 *idmask;
75 const size_t idlen;
76
77 void (*idsetup)(struct lowpan_nhc *nhc);
78 int (*uncompress)(struct sk_buff *skb, size_t needed);
79 int (*compress)(struct sk_buff *skb, u8 **hc_ptr);
80};
81
82/**
83 * lowpan_nhc_by_nexthdr - return the 6lowpan nhc by ipv6 nexthdr.
84 *
85 * @nexthdr: ipv6 nexthdr value.
86 */
87struct lowpan_nhc *lowpan_nhc_by_nexthdr(u8 nexthdr);
88
89/**
90 * lowpan_nhc_check_compression - checks if we support compression format. If
91 * we support the nhc by nexthdr field, the 6LoWPAN iphc NHC bit will be
92 * set. If we don't support nexthdr will be added as inline data to the
93 * 6LoWPAN header.
94 *
95 * @skb: skb of 6LoWPAN header to read nhc and replace header.
96 * @hdr: ipv6hdr to check the nexthdr value
97 * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
98 * replaced header.
99 * @iphc0: iphc0 pointer to set the 6LoWPAN NHC bit
100 */
101int lowpan_nhc_check_compression(struct sk_buff *skb,
102 const struct ipv6hdr *hdr, u8 **hc_ptr,
103 u8 *iphc0);
104
105/**
106 * lowpan_nhc_do_compression - calling compress callback for nhc
107 *
108 * @skb: skb of 6LoWPAN header to read nhc and replace header.
109 * @hdr: ipv6hdr to set the nexthdr value
110 * @hc_ptr: pointer for 6LoWPAN header which should increment at the end of
111 * replaced header.
112 */
113int lowpan_nhc_do_compression(struct sk_buff *skb, const struct ipv6hdr *hdr,
114 u8 **hc_ptr);
115
116/**
117 * lowpan_nhc_do_uncompression - calling uncompress callback for nhc
118 *
119 * @nhc: 6LoWPAN nhc context, get by lowpan_nhc_by_ functions.
120 * @skb: skb of 6LoWPAN header, skb->data should be pointed to nhc id value.
121 * @dev: netdevice for print logging information.
122 * @hdr: ipv6hdr for setting nexthdr value.
123 */
124int lowpan_nhc_do_uncompression(struct sk_buff *skb, struct net_device *dev,
125 struct ipv6hdr *hdr);
126
127/**
128 * lowpan_nhc_add - register a next header compression to framework
129 *
130 * @nhc: nhc which should be add.
131 */
132int lowpan_nhc_add(struct lowpan_nhc *nhc);
133
134/**
135 * lowpan_nhc_del - delete a next header compression from framework
136 *
137 * @nhc: nhc which should be delete.
138 */
139void lowpan_nhc_del(struct lowpan_nhc *nhc);
140
141/**
142 * lowpan_nhc_init - adding all default nhcs
143 */
144void lowpan_nhc_init(void);
145
146#endif /* __6LOWPAN_NHC_H */
diff --git a/net/6lowpan/nhc_dest.c b/net/6lowpan/nhc_dest.c
new file mode 100644
index 000000000000..0b292c9646eb
--- /dev/null
+++ b/net/6lowpan/nhc_dest.c
@@ -0,0 +1,28 @@
1/*
2 * 6LoWPAN IPv6 Destination Options Header compression according to
3 * RFC6282
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 */
10
11#include "nhc.h"
12
13#define LOWPAN_NHC_DEST_IDLEN 1
14#define LOWPAN_NHC_DEST_ID_0 0xe6
15#define LOWPAN_NHC_DEST_MASK_0 0xfe
16
17static void dest_nhid_setup(struct lowpan_nhc *nhc)
18{
19 nhc->id[0] = LOWPAN_NHC_DEST_ID_0;
20 nhc->idmask[0] = LOWPAN_NHC_DEST_MASK_0;
21}
22
23LOWPAN_NHC(nhc_dest, "RFC6282 Destination Options", NEXTHDR_DEST, 0,
24 dest_nhid_setup, LOWPAN_NHC_DEST_IDLEN, NULL, NULL);
25
26module_lowpan_nhc(nhc_dest);
27MODULE_DESCRIPTION("6LoWPAN next header RFC6282 Destination Options compression");
28MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_fragment.c b/net/6lowpan/nhc_fragment.c
new file mode 100644
index 000000000000..473dbc58ef84
--- /dev/null
+++ b/net/6lowpan/nhc_fragment.c
@@ -0,0 +1,27 @@
1/*
2 * 6LoWPAN IPv6 Fragment Header compression according to RFC6282
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include "nhc.h"
11
12#define LOWPAN_NHC_FRAGMENT_IDLEN 1
13#define LOWPAN_NHC_FRAGMENT_ID_0 0xe4
14#define LOWPAN_NHC_FRAGMENT_MASK_0 0xfe
15
16static void fragment_nhid_setup(struct lowpan_nhc *nhc)
17{
18 nhc->id[0] = LOWPAN_NHC_FRAGMENT_ID_0;
19 nhc->idmask[0] = LOWPAN_NHC_FRAGMENT_MASK_0;
20}
21
22LOWPAN_NHC(nhc_fragment, "RFC6282 Fragment", NEXTHDR_FRAGMENT, 0,
23 fragment_nhid_setup, LOWPAN_NHC_FRAGMENT_IDLEN, NULL, NULL);
24
25module_lowpan_nhc(nhc_fragment);
26MODULE_DESCRIPTION("6LoWPAN next header RFC6282 Fragment compression");
27MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_hop.c b/net/6lowpan/nhc_hop.c
new file mode 100644
index 000000000000..1eb66be16f19
--- /dev/null
+++ b/net/6lowpan/nhc_hop.c
@@ -0,0 +1,27 @@
1/*
2 * 6LoWPAN IPv6 Hop-by-Hop Options Header compression according to RFC6282
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include "nhc.h"
11
12#define LOWPAN_NHC_HOP_IDLEN 1
13#define LOWPAN_NHC_HOP_ID_0 0xe0
14#define LOWPAN_NHC_HOP_MASK_0 0xfe
15
16static void hop_nhid_setup(struct lowpan_nhc *nhc)
17{
18 nhc->id[0] = LOWPAN_NHC_HOP_ID_0;
19 nhc->idmask[0] = LOWPAN_NHC_HOP_MASK_0;
20}
21
22LOWPAN_NHC(nhc_hop, "RFC6282 Hop-by-Hop Options", NEXTHDR_HOP, 0,
23 hop_nhid_setup, LOWPAN_NHC_HOP_IDLEN, NULL, NULL);
24
25module_lowpan_nhc(nhc_hop);
26MODULE_DESCRIPTION("6LoWPAN next header RFC6282 Hop-by-Hop Options compression");
27MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_ipv6.c b/net/6lowpan/nhc_ipv6.c
new file mode 100644
index 000000000000..2313d1600af3
--- /dev/null
+++ b/net/6lowpan/nhc_ipv6.c
@@ -0,0 +1,27 @@
1/*
2 * 6LoWPAN IPv6 Header compression according to RFC6282
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include "nhc.h"
11
12#define LOWPAN_NHC_IPV6_IDLEN 1
13#define LOWPAN_NHC_IPV6_ID_0 0xee
14#define LOWPAN_NHC_IPV6_MASK_0 0xfe
15
16static void ipv6_nhid_setup(struct lowpan_nhc *nhc)
17{
18 nhc->id[0] = LOWPAN_NHC_IPV6_ID_0;
19 nhc->idmask[0] = LOWPAN_NHC_IPV6_MASK_0;
20}
21
22LOWPAN_NHC(nhc_ipv6, "RFC6282 IPv6", NEXTHDR_IPV6, 0, ipv6_nhid_setup,
23 LOWPAN_NHC_IPV6_IDLEN, NULL, NULL);
24
25module_lowpan_nhc(nhc_ipv6);
26MODULE_DESCRIPTION("6LoWPAN next header RFC6282 IPv6 compression");
27MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_mobility.c b/net/6lowpan/nhc_mobility.c
new file mode 100644
index 000000000000..60d3f3886c98
--- /dev/null
+++ b/net/6lowpan/nhc_mobility.c
@@ -0,0 +1,27 @@
1/*
2 * 6LoWPAN IPv6 Mobility Header compression according to RFC6282
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include "nhc.h"
11
12#define LOWPAN_NHC_MOBILITY_IDLEN 1
13#define LOWPAN_NHC_MOBILITY_ID_0 0xe8
14#define LOWPAN_NHC_MOBILITY_MASK_0 0xfe
15
16static void mobility_nhid_setup(struct lowpan_nhc *nhc)
17{
18 nhc->id[0] = LOWPAN_NHC_MOBILITY_ID_0;
19 nhc->idmask[0] = LOWPAN_NHC_MOBILITY_MASK_0;
20}
21
22LOWPAN_NHC(nhc_mobility, "RFC6282 Mobility", NEXTHDR_MOBILITY, 0,
23 mobility_nhid_setup, LOWPAN_NHC_MOBILITY_IDLEN, NULL, NULL);
24
25module_lowpan_nhc(nhc_mobility);
26MODULE_DESCRIPTION("6LoWPAN next header RFC6282 Mobility compression");
27MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_routing.c b/net/6lowpan/nhc_routing.c
new file mode 100644
index 000000000000..c393280f11c4
--- /dev/null
+++ b/net/6lowpan/nhc_routing.c
@@ -0,0 +1,27 @@
1/*
2 * 6LoWPAN IPv6 Routing Header compression according to RFC6282
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include "nhc.h"
11
12#define LOWPAN_NHC_ROUTING_IDLEN 1
13#define LOWPAN_NHC_ROUTING_ID_0 0xe2
14#define LOWPAN_NHC_ROUTING_MASK_0 0xfe
15
16static void routing_nhid_setup(struct lowpan_nhc *nhc)
17{
18 nhc->id[0] = LOWPAN_NHC_ROUTING_ID_0;
19 nhc->idmask[0] = LOWPAN_NHC_ROUTING_MASK_0;
20}
21
22LOWPAN_NHC(nhc_routing, "RFC6282 Routing", NEXTHDR_ROUTING, 0,
23 routing_nhid_setup, LOWPAN_NHC_ROUTING_IDLEN, NULL, NULL);
24
25module_lowpan_nhc(nhc_routing);
26MODULE_DESCRIPTION("6LoWPAN next header RFC6282 Routing compression");
27MODULE_LICENSE("GPL");
diff --git a/net/6lowpan/nhc_udp.c b/net/6lowpan/nhc_udp.c
new file mode 100644
index 000000000000..c6bcaeb428ae
--- /dev/null
+++ b/net/6lowpan/nhc_udp.c
@@ -0,0 +1,157 @@
1/*
2 * 6LoWPAN IPv6 UDP compression according to RFC6282
3 *
4 *
5 * Authors:
6 * Alexander Aring <aar@pengutronix.de>
7 *
8 * Orignal written by:
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10 * Jon Smirl <jonsmirl@gmail.com>
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 "nhc.h"
19
20#define LOWPAN_NHC_UDP_IDLEN 1
21
22static int udp_uncompress(struct sk_buff *skb, size_t needed)
23{
24 u8 tmp = 0, val = 0;
25 struct udphdr uh;
26 bool fail;
27 int err;
28
29 fail = lowpan_fetch_skb(skb, &tmp, sizeof(tmp));
30
31 pr_debug("UDP header uncompression\n");
32 switch (tmp & LOWPAN_NHC_UDP_CS_P_11) {
33 case LOWPAN_NHC_UDP_CS_P_00:
34 fail |= lowpan_fetch_skb(skb, &uh.source, sizeof(uh.source));
35 fail |= lowpan_fetch_skb(skb, &uh.dest, sizeof(uh.dest));
36 break;
37 case LOWPAN_NHC_UDP_CS_P_01:
38 fail |= lowpan_fetch_skb(skb, &uh.source, sizeof(uh.source));
39 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
40 uh.dest = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
41 break;
42 case LOWPAN_NHC_UDP_CS_P_10:
43 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
44 uh.source = htons(val + LOWPAN_NHC_UDP_8BIT_PORT);
45 fail |= lowpan_fetch_skb(skb, &uh.dest, sizeof(uh.dest));
46 break;
47 case LOWPAN_NHC_UDP_CS_P_11:
48 fail |= lowpan_fetch_skb(skb, &val, sizeof(val));
49 uh.source = htons(LOWPAN_NHC_UDP_4BIT_PORT + (val >> 4));
50 uh.dest = htons(LOWPAN_NHC_UDP_4BIT_PORT + (val & 0x0f));
51 break;
52 default:
53 BUG();
54 }
55
56 pr_debug("uncompressed UDP ports: src = %d, dst = %d\n",
57 ntohs(uh.source), ntohs(uh.dest));
58
59 /* checksum */
60 if (tmp & LOWPAN_NHC_UDP_CS_C) {
61 pr_debug_ratelimited("checksum elided currently not supported\n");
62 fail = true;
63 } else {
64 fail |= lowpan_fetch_skb(skb, &uh.check, sizeof(uh.check));
65 }
66
67 if (fail)
68 return -EINVAL;
69
70 /* UDP length needs to be infered from the lower layers
71 * here, we obtain the hint from the remaining size of the
72 * frame
73 */
74 uh.len = htons(skb->len + sizeof(struct udphdr));
75 pr_debug("uncompressed UDP length: src = %d", ntohs(uh.len));
76
77 /* replace the compressed UDP head by the uncompressed UDP
78 * header
79 */
80 err = skb_cow(skb, needed);
81 if (unlikely(err))
82 return err;
83
84 skb_push(skb, sizeof(struct udphdr));
85 skb_copy_to_linear_data(skb, &uh, sizeof(struct udphdr));
86
87 return 0;
88}
89
90static int udp_compress(struct sk_buff *skb, u8 **hc_ptr)
91{
92 const struct udphdr *uh = udp_hdr(skb);
93 u8 tmp;
94
95 if (((ntohs(uh->source) & LOWPAN_NHC_UDP_4BIT_MASK) ==
96 LOWPAN_NHC_UDP_4BIT_PORT) &&
97 ((ntohs(uh->dest) & LOWPAN_NHC_UDP_4BIT_MASK) ==
98 LOWPAN_NHC_UDP_4BIT_PORT)) {
99 pr_debug("UDP header: both ports compression to 4 bits\n");
100 /* compression value */
101 tmp = LOWPAN_NHC_UDP_CS_P_11;
102 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
103 /* source and destination port */
104 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_4BIT_PORT +
105 ((ntohs(uh->source) - LOWPAN_NHC_UDP_4BIT_PORT) << 4);
106 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
107 } else if ((ntohs(uh->dest) & LOWPAN_NHC_UDP_8BIT_MASK) ==
108 LOWPAN_NHC_UDP_8BIT_PORT) {
109 pr_debug("UDP header: remove 8 bits of dest\n");
110 /* compression value */
111 tmp = LOWPAN_NHC_UDP_CS_P_01;
112 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
113 /* source port */
114 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
115 /* destination port */
116 tmp = ntohs(uh->dest) - LOWPAN_NHC_UDP_8BIT_PORT;
117 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
118 } else if ((ntohs(uh->source) & LOWPAN_NHC_UDP_8BIT_MASK) ==
119 LOWPAN_NHC_UDP_8BIT_PORT) {
120 pr_debug("UDP header: remove 8 bits of source\n");
121 /* compression value */
122 tmp = LOWPAN_NHC_UDP_CS_P_10;
123 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
124 /* source port */
125 tmp = ntohs(uh->source) - LOWPAN_NHC_UDP_8BIT_PORT;
126 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
127 /* destination port */
128 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
129 } else {
130 pr_debug("UDP header: can't compress\n");
131 /* compression value */
132 tmp = LOWPAN_NHC_UDP_CS_P_00;
133 lowpan_push_hc_data(hc_ptr, &tmp, sizeof(tmp));
134 /* source port */
135 lowpan_push_hc_data(hc_ptr, &uh->source, sizeof(uh->source));
136 /* destination port */
137 lowpan_push_hc_data(hc_ptr, &uh->dest, sizeof(uh->dest));
138 }
139
140 /* checksum is always inline */
141 lowpan_push_hc_data(hc_ptr, &uh->check, sizeof(uh->check));
142
143 return 0;
144}
145
146static void udp_nhid_setup(struct lowpan_nhc *nhc)
147{
148 nhc->id[0] = LOWPAN_NHC_UDP_ID;
149 nhc->idmask[0] = LOWPAN_NHC_UDP_MASK;
150}
151
152LOWPAN_NHC(nhc_udp, "RFC6282 UDP", NEXTHDR_UDP, sizeof(struct udphdr),
153 udp_nhid_setup, LOWPAN_NHC_UDP_IDLEN, udp_uncompress, udp_compress);
154
155module_lowpan_nhc(nhc_udp);
156MODULE_DESCRIPTION("6LoWPAN next header RFC6282 UDP compression");
157MODULE_LICENSE("GPL");
diff --git a/net/802/fc.c b/net/802/fc.c
index 7c174b6750cd..7b9219022418 100644
--- a/net/802/fc.c
+++ b/net/802/fc.c
@@ -75,29 +75,8 @@ static int fc_header(struct sk_buff *skb, struct net_device *dev,
75 return -hdr_len; 75 return -hdr_len;
76} 76}
77 77
78/*
79 * A neighbour discovery of some species (eg arp) has completed. We
80 * can now send the packet.
81 */
82
83static int fc_rebuild_header(struct sk_buff *skb)
84{
85#ifdef CONFIG_INET
86 struct fch_hdr *fch=(struct fch_hdr *)skb->data;
87 struct fcllc *fcllc=(struct fcllc *)(skb->data+sizeof(struct fch_hdr));
88 if(fcllc->ethertype != htons(ETH_P_IP)) {
89 printk("fc_rebuild_header: Don't know how to resolve type %04X addresses ?\n", ntohs(fcllc->ethertype));
90 return 0;
91 }
92 return arp_find(fch->daddr, skb);
93#else
94 return 0;
95#endif
96}
97
98static const struct header_ops fc_header_ops = { 78static const struct header_ops fc_header_ops = {
99 .create = fc_header, 79 .create = fc_header,
100 .rebuild = fc_rebuild_header,
101}; 80};
102 81
103static void fc_setup(struct net_device *dev) 82static void fc_setup(struct net_device *dev)
diff --git a/net/802/fddi.c b/net/802/fddi.c
index 59e7346f1193..7d3a0af954e8 100644
--- a/net/802/fddi.c
+++ b/net/802/fddi.c
@@ -87,31 +87,6 @@ static int fddi_header(struct sk_buff *skb, struct net_device *dev,
87 return -hl; 87 return -hl;
88} 88}
89 89
90
91/*
92 * Rebuild the FDDI MAC header. This is called after an ARP
93 * (or in future other address resolution) has completed on
94 * this sk_buff. We now let ARP fill in the other fields.
95 */
96
97static int fddi_rebuild_header(struct sk_buff *skb)
98{
99 struct fddihdr *fddi = (struct fddihdr *)skb->data;
100
101#ifdef CONFIG_INET
102 if (fddi->hdr.llc_snap.ethertype == htons(ETH_P_IP))
103 /* Try to get ARP to resolve the header and fill destination address */
104 return arp_find(fddi->daddr, skb);
105 else
106#endif
107 {
108 printk("%s: Don't know how to resolve type %04X addresses.\n",
109 skb->dev->name, ntohs(fddi->hdr.llc_snap.ethertype));
110 return 0;
111 }
112}
113
114
115/* 90/*
116 * Determine the packet's protocol ID and fill in skb fields. 91 * Determine the packet's protocol ID and fill in skb fields.
117 * This routine is called before an incoming packet is passed 92 * This routine is called before an incoming packet is passed
@@ -177,7 +152,6 @@ EXPORT_SYMBOL(fddi_change_mtu);
177 152
178static const struct header_ops fddi_header_ops = { 153static const struct header_ops fddi_header_ops = {
179 .create = fddi_header, 154 .create = fddi_header,
180 .rebuild = fddi_rebuild_header,
181}; 155};
182 156
183 157
diff --git a/net/802/hippi.c b/net/802/hippi.c
index 2e03f8259dd5..ade1a52cdcff 100644
--- a/net/802/hippi.c
+++ b/net/802/hippi.c
@@ -91,33 +91,6 @@ static int hippi_header(struct sk_buff *skb, struct net_device *dev,
91 91
92 92
93/* 93/*
94 * Rebuild the HIPPI MAC header. This is called after an ARP has
95 * completed on this sk_buff. We now let ARP fill in the other fields.
96 */
97
98static int hippi_rebuild_header(struct sk_buff *skb)
99{
100 struct hippi_hdr *hip = (struct hippi_hdr *)skb->data;
101
102 /*
103 * Only IP is currently supported
104 */
105
106 if(hip->snap.ethertype != htons(ETH_P_IP))
107 {
108 printk(KERN_DEBUG "%s: unable to resolve type %X addresses.\n",skb->dev->name,ntohs(hip->snap.ethertype));
109 return 0;
110 }
111
112 /*
113 * We don't support dynamic ARP on HIPPI, but we use the ARP
114 * static ARP tables to hold the I-FIELDs.
115 */
116 return arp_find(hip->le.daddr, skb);
117}
118
119
120/*
121 * Determine the packet's protocol ID. 94 * Determine the packet's protocol ID.
122 */ 95 */
123 96
@@ -186,7 +159,6 @@ EXPORT_SYMBOL(hippi_neigh_setup_dev);
186 159
187static const struct header_ops hippi_header_ops = { 160static const struct header_ops hippi_header_ops = {
188 .create = hippi_header, 161 .create = hippi_header,
189 .rebuild = hippi_rebuild_header,
190}; 162};
191 163
192 164
diff --git a/net/8021q/vlan.c b/net/8021q/vlan.c
index 64c6bed4a3d3..98a30a5b8664 100644
--- a/net/8021q/vlan.c
+++ b/net/8021q/vlan.c
@@ -413,7 +413,10 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
413 vlan_transfer_features(dev, vlandev); 413 vlan_transfer_features(dev, vlandev);
414 break; 414 break;
415 415
416 case NETDEV_DOWN: 416 case NETDEV_DOWN: {
417 struct net_device *tmp;
418 LIST_HEAD(close_list);
419
417 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 420 if (dev->features & NETIF_F_HW_VLAN_CTAG_FILTER)
418 vlan_vid_del(dev, htons(ETH_P_8021Q), 0); 421 vlan_vid_del(dev, htons(ETH_P_8021Q), 0);
419 422
@@ -425,11 +428,18 @@ static int vlan_device_event(struct notifier_block *unused, unsigned long event,
425 428
426 vlan = vlan_dev_priv(vlandev); 429 vlan = vlan_dev_priv(vlandev);
427 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING)) 430 if (!(vlan->flags & VLAN_FLAG_LOOSE_BINDING))
428 dev_change_flags(vlandev, flgs & ~IFF_UP); 431 list_add(&vlandev->close_list, &close_list);
432 }
433
434 dev_close_many(&close_list, false);
435
436 list_for_each_entry_safe(vlandev, tmp, &close_list, close_list) {
429 netif_stacked_transfer_operstate(dev, vlandev); 437 netif_stacked_transfer_operstate(dev, vlandev);
438 list_del_init(&vlandev->close_list);
430 } 439 }
440 list_del(&close_list);
431 break; 441 break;
432 442 }
433 case NETDEV_UP: 443 case NETDEV_UP:
434 /* Put all VLANs for this dev in the up state too. */ 444 /* Put all VLANs for this dev in the up state too. */
435 vlan_group_for_each_dev(grp, i, vlandev) { 445 vlan_group_for_each_dev(grp, i, vlandev) {
diff --git a/net/8021q/vlan_dev.c b/net/8021q/vlan_dev.c
index 118956448cf6..01d7ba840df8 100644
--- a/net/8021q/vlan_dev.c
+++ b/net/8021q/vlan_dev.c
@@ -37,39 +37,6 @@
37#include <linux/netpoll.h> 37#include <linux/netpoll.h>
38 38
39/* 39/*
40 * Rebuild the Ethernet MAC header. This is called after an ARP
41 * (or in future other address resolution) has completed on this
42 * sk_buff. We now let ARP fill in the other fields.
43 *
44 * This routine CANNOT use cached dst->neigh!
45 * Really, it is used only when dst->neigh is wrong.
46 *
47 * TODO: This needs a checkup, I'm ignorant here. --BLG
48 */
49static int vlan_dev_rebuild_header(struct sk_buff *skb)
50{
51 struct net_device *dev = skb->dev;
52 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)(skb->data);
53
54 switch (veth->h_vlan_encapsulated_proto) {
55#ifdef CONFIG_INET
56 case htons(ETH_P_IP):
57
58 /* TODO: Confirm this will work with VLAN headers... */
59 return arp_find(veth->h_dest, skb);
60#endif
61 default:
62 pr_debug("%s: unable to resolve type %X addresses\n",
63 dev->name, ntohs(veth->h_vlan_encapsulated_proto));
64
65 ether_addr_copy(veth->h_source, dev->dev_addr);
66 break;
67 }
68
69 return 0;
70}
71
72/*
73 * Create the VLAN header for an arbitrary protocol layer 40 * Create the VLAN header for an arbitrary protocol layer
74 * 41 *
75 * saddr=NULL means use device source address 42 * saddr=NULL means use device source address
@@ -534,7 +501,6 @@ static int vlan_dev_get_lock_subclass(struct net_device *dev)
534 501
535static const struct header_ops vlan_header_ops = { 502static const struct header_ops vlan_header_ops = {
536 .create = vlan_dev_hard_header, 503 .create = vlan_dev_hard_header,
537 .rebuild = vlan_dev_rebuild_header,
538 .parse = eth_header_parse, 504 .parse = eth_header_parse,
539}; 505};
540 506
@@ -554,7 +520,6 @@ static int vlan_passthru_hard_header(struct sk_buff *skb, struct net_device *dev
554 520
555static const struct header_ops vlan_passthru_header_ops = { 521static const struct header_ops vlan_passthru_header_ops = {
556 .create = vlan_passthru_hard_header, 522 .create = vlan_passthru_hard_header,
557 .rebuild = dev_rebuild_header,
558 .parse = eth_header_parse, 523 .parse = eth_header_parse,
559}; 524};
560 525
@@ -573,7 +538,6 @@ static int vlan_dev_init(struct net_device *dev)
573 /* IFF_BROADCAST|IFF_MULTICAST; ??? */ 538 /* IFF_BROADCAST|IFF_MULTICAST; ??? */
574 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | 539 dev->flags = real_dev->flags & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
575 IFF_MASTER | IFF_SLAVE); 540 IFF_MASTER | IFF_SLAVE);
576 dev->iflink = real_dev->ifindex;
577 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) | 541 dev->state = (real_dev->state & ((1<<__LINK_STATE_NOCARRIER) |
578 (1<<__LINK_STATE_DORMANT))) | 542 (1<<__LINK_STATE_DORMANT))) |
579 (1<<__LINK_STATE_PRESENT); 543 (1<<__LINK_STATE_PRESENT);
@@ -589,6 +553,7 @@ static int vlan_dev_init(struct net_device *dev)
589 if (dev->features & NETIF_F_VLAN_FEATURES) 553 if (dev->features & NETIF_F_VLAN_FEATURES)
590 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n"); 554 netdev_warn(real_dev, "VLAN features are set incorrectly. Q-in-Q configurations may not work correctly.\n");
591 555
556 dev->vlan_features = real_dev->vlan_features & ~NETIF_F_ALL_FCOE;
592 557
593 /* ipv6 shared card related stuff */ 558 /* ipv6 shared card related stuff */
594 dev->dev_id = real_dev->dev_id; 559 dev->dev_id = real_dev->dev_id;
@@ -767,6 +732,13 @@ static void vlan_dev_netpoll_cleanup(struct net_device *dev)
767} 732}
768#endif /* CONFIG_NET_POLL_CONTROLLER */ 733#endif /* CONFIG_NET_POLL_CONTROLLER */
769 734
735static int vlan_dev_get_iflink(const struct net_device *dev)
736{
737 struct net_device *real_dev = vlan_dev_priv(dev)->real_dev;
738
739 return real_dev->ifindex;
740}
741
770static const struct ethtool_ops vlan_ethtool_ops = { 742static const struct ethtool_ops vlan_ethtool_ops = {
771 .get_settings = vlan_ethtool_get_settings, 743 .get_settings = vlan_ethtool_get_settings,
772 .get_drvinfo = vlan_ethtool_get_drvinfo, 744 .get_drvinfo = vlan_ethtool_get_drvinfo,
@@ -803,6 +775,7 @@ static const struct net_device_ops vlan_netdev_ops = {
803#endif 775#endif
804 .ndo_fix_features = vlan_dev_fix_features, 776 .ndo_fix_features = vlan_dev_fix_features,
805 .ndo_get_lock_subclass = vlan_dev_get_lock_subclass, 777 .ndo_get_lock_subclass = vlan_dev_get_lock_subclass,
778 .ndo_get_iflink = vlan_dev_get_iflink,
806}; 779};
807 780
808static void vlan_dev_free(struct net_device *dev) 781static void vlan_dev_free(struct net_device *dev)
@@ -827,5 +800,5 @@ void vlan_setup(struct net_device *dev)
827 dev->destructor = vlan_dev_free; 800 dev->destructor = vlan_dev_free;
828 dev->ethtool_ops = &vlan_ethtool_ops; 801 dev->ethtool_ops = &vlan_ethtool_ops;
829 802
830 memset(dev->broadcast, 0, ETH_ALEN); 803 eth_zero_addr(dev->broadcast);
831} 804}
diff --git a/net/9p/client.c b/net/9p/client.c
index e86a9bea1d16..6f4c4c88db84 100644
--- a/net/9p/client.c
+++ b/net/9p/client.c
@@ -34,6 +34,7 @@
34#include <linux/slab.h> 34#include <linux/slab.h>
35#include <linux/sched.h> 35#include <linux/sched.h>
36#include <linux/uaccess.h> 36#include <linux/uaccess.h>
37#include <linux/uio.h>
37#include <net/9p/9p.h> 38#include <net/9p/9p.h>
38#include <linux/parser.h> 39#include <linux/parser.h>
39#include <net/9p/client.h> 40#include <net/9p/client.h>
@@ -555,7 +556,7 @@ out_err:
555 */ 556 */
556 557
557static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req, 558static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
558 char *uidata, int in_hdrlen, int kern_buf) 559 struct iov_iter *uidata, int in_hdrlen)
559{ 560{
560 int err; 561 int err;
561 int ecode; 562 int ecode;
@@ -591,16 +592,11 @@ static int p9_check_zc_errors(struct p9_client *c, struct p9_req_t *req,
591 ename = &req->rc->sdata[req->rc->offset]; 592 ename = &req->rc->sdata[req->rc->offset];
592 if (len > inline_len) { 593 if (len > inline_len) {
593 /* We have error in external buffer */ 594 /* We have error in external buffer */
594 if (kern_buf) { 595 err = copy_from_iter(ename + inline_len,
595 memcpy(ename + inline_len, uidata, 596 len - inline_len, uidata);
596 len - inline_len); 597 if (err != len - inline_len) {
597 } else { 598 err = -EFAULT;
598 err = copy_from_user(ename + inline_len, 599 goto out_err;
599 uidata, len - inline_len);
600 if (err) {
601 err = -EFAULT;
602 goto out_err;
603 }
604 } 600 }
605 } 601 }
606 ename = NULL; 602 ename = NULL;
@@ -806,8 +802,8 @@ reterr:
806 * p9_client_zc_rpc - issue a request and wait for a response 802 * p9_client_zc_rpc - issue a request and wait for a response
807 * @c: client session 803 * @c: client session
808 * @type: type of request 804 * @type: type of request
809 * @uidata: user bffer that should be ued for zero copy read 805 * @uidata: destination for zero copy read
810 * @uodata: user buffer that shoud be user for zero copy write 806 * @uodata: source for zero copy write
811 * @inlen: read buffer size 807 * @inlen: read buffer size
812 * @olen: write buffer size 808 * @olen: write buffer size
813 * @hdrlen: reader header size, This is the size of response protocol data 809 * @hdrlen: reader header size, This is the size of response protocol data
@@ -816,9 +812,10 @@ reterr:
816 * Returns request structure (which client must free using p9_free_req) 812 * Returns request structure (which client must free using p9_free_req)
817 */ 813 */
818static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type, 814static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
819 char *uidata, char *uodata, 815 struct iov_iter *uidata,
816 struct iov_iter *uodata,
820 int inlen, int olen, int in_hdrlen, 817 int inlen, int olen, int in_hdrlen,
821 int kern_buf, const char *fmt, ...) 818 const char *fmt, ...)
822{ 819{
823 va_list ap; 820 va_list ap;
824 int sigpending, err; 821 int sigpending, err;
@@ -841,12 +838,8 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
841 } else 838 } else
842 sigpending = 0; 839 sigpending = 0;
843 840
844 /* If we are called with KERNEL_DS force kern_buf */
845 if (segment_eq(get_fs(), KERNEL_DS))
846 kern_buf = 1;
847
848 err = c->trans_mod->zc_request(c, req, uidata, uodata, 841 err = c->trans_mod->zc_request(c, req, uidata, uodata,
849 inlen, olen, in_hdrlen, kern_buf); 842 inlen, olen, in_hdrlen);
850 if (err < 0) { 843 if (err < 0) {
851 if (err == -EIO) 844 if (err == -EIO)
852 c->status = Disconnected; 845 c->status = Disconnected;
@@ -876,7 +869,7 @@ static struct p9_req_t *p9_client_zc_rpc(struct p9_client *c, int8_t type,
876 if (err < 0) 869 if (err < 0)
877 goto reterr; 870 goto reterr;
878 871
879 err = p9_check_zc_errors(c, req, uidata, in_hdrlen, kern_buf); 872 err = p9_check_zc_errors(c, req, uidata, in_hdrlen);
880 trace_9p_client_res(c, type, req->rc->tag, err); 873 trace_9p_client_res(c, type, req->rc->tag, err);
881 if (!err) 874 if (!err)
882 return req; 875 return req;
@@ -1123,6 +1116,7 @@ struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
1123 fid = NULL; 1116 fid = NULL;
1124 goto error; 1117 goto error;
1125 } 1118 }
1119 fid->uid = n_uname;
1126 1120
1127 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid, 1121 req = p9_client_rpc(clnt, P9_TATTACH, "ddss?u", fid->fid,
1128 afid ? afid->fid : P9_NOFID, uname, aname, n_uname); 1122 afid ? afid->fid : P9_NOFID, uname, aname, n_uname);
@@ -1541,142 +1535,128 @@ error:
1541EXPORT_SYMBOL(p9_client_unlinkat); 1535EXPORT_SYMBOL(p9_client_unlinkat);
1542 1536
1543int 1537int
1544p9_client_read(struct p9_fid *fid, char *data, char __user *udata, u64 offset, 1538p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err)
1545 u32 count)
1546{ 1539{
1547 char *dataptr; 1540 struct p9_client *clnt = fid->clnt;
1548 int kernel_buf = 0;
1549 struct p9_req_t *req; 1541 struct p9_req_t *req;
1550 struct p9_client *clnt; 1542 int total = 0;
1551 int err, rsize, non_zc = 0;
1552
1553 1543
1554 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n", 1544 p9_debug(P9_DEBUG_9P, ">>> TREAD fid %d offset %llu %d\n",
1555 fid->fid, (unsigned long long) offset, count); 1545 fid->fid, (unsigned long long) offset, (int)iov_iter_count(to));
1556 err = 0; 1546
1557 clnt = fid->clnt; 1547 while (iov_iter_count(to)) {
1558 1548 int count = iov_iter_count(to);
1559 rsize = fid->iounit; 1549 int rsize, non_zc = 0;
1560 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ) 1550 char *dataptr;
1561 rsize = clnt->msize - P9_IOHDRSZ; 1551
1562 1552 rsize = fid->iounit;
1563 if (count < rsize) 1553 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1564 rsize = count; 1554 rsize = clnt->msize - P9_IOHDRSZ;
1565 1555
1566 /* Don't bother zerocopy for small IO (< 1024) */ 1556 if (count < rsize)
1567 if (clnt->trans_mod->zc_request && rsize > 1024) { 1557 rsize = count;
1568 char *indata; 1558
1569 if (data) { 1559 /* Don't bother zerocopy for small IO (< 1024) */
1570 kernel_buf = 1; 1560 if (clnt->trans_mod->zc_request && rsize > 1024) {
1571 indata = data; 1561 /*
1572 } else 1562 * response header len is 11
1573 indata = (__force char *)udata; 1563 * PDU Header(7) + IO Size (4)
1574 /* 1564 */
1575 * response header len is 11 1565 req = p9_client_zc_rpc(clnt, P9_TREAD, to, NULL, rsize,
1576 * PDU Header(7) + IO Size (4) 1566 0, 11, "dqd", fid->fid,
1577 */ 1567 offset, rsize);
1578 req = p9_client_zc_rpc(clnt, P9_TREAD, indata, NULL, rsize, 0, 1568 } else {
1579 11, kernel_buf, "dqd", fid->fid, 1569 non_zc = 1;
1580 offset, rsize); 1570 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset,
1581 } else { 1571 rsize);
1582 non_zc = 1; 1572 }
1583 req = p9_client_rpc(clnt, P9_TREAD, "dqd", fid->fid, offset, 1573 if (IS_ERR(req)) {
1584 rsize); 1574 *err = PTR_ERR(req);
1585 } 1575 break;
1586 if (IS_ERR(req)) { 1576 }
1587 err = PTR_ERR(req);
1588 goto error;
1589 }
1590 1577
1591 err = p9pdu_readf(req->rc, clnt->proto_version, "D", &count, &dataptr); 1578 *err = p9pdu_readf(req->rc, clnt->proto_version,
1592 if (err) { 1579 "D", &count, &dataptr);
1593 trace_9p_protocol_dump(clnt, req->rc); 1580 if (*err) {
1594 goto free_and_error; 1581 trace_9p_protocol_dump(clnt, req->rc);
1595 } 1582 p9_free_req(clnt, req);
1583 break;
1584 }
1596 1585
1597 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count); 1586 p9_debug(P9_DEBUG_9P, "<<< RREAD count %d\n", count);
1587 if (!count) {
1588 p9_free_req(clnt, req);
1589 break;
1590 }
1598 1591
1599 if (non_zc) { 1592 if (non_zc) {
1600 if (data) { 1593 int n = copy_to_iter(dataptr, count, to);
1601 memmove(data, dataptr, count); 1594 total += n;
1602 } else { 1595 offset += n;
1603 err = copy_to_user(udata, dataptr, count); 1596 if (n != count) {
1604 if (err) { 1597 *err = -EFAULT;
1605 err = -EFAULT; 1598 p9_free_req(clnt, req);
1606 goto free_and_error; 1599 break;
1607 } 1600 }
1601 } else {
1602 iov_iter_advance(to, count);
1603 total += count;
1604 offset += count;
1608 } 1605 }
1606 p9_free_req(clnt, req);
1609 } 1607 }
1610 p9_free_req(clnt, req); 1608 return total;
1611 return count;
1612
1613free_and_error:
1614 p9_free_req(clnt, req);
1615error:
1616 return err;
1617} 1609}
1618EXPORT_SYMBOL(p9_client_read); 1610EXPORT_SYMBOL(p9_client_read);
1619 1611
1620int 1612int
1621p9_client_write(struct p9_fid *fid, char *data, const char __user *udata, 1613p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err)
1622 u64 offset, u32 count)
1623{ 1614{
1624 int err, rsize; 1615 struct p9_client *clnt = fid->clnt;
1625 int kernel_buf = 0;
1626 struct p9_client *clnt;
1627 struct p9_req_t *req; 1616 struct p9_req_t *req;
1617 int total = 0;
1618
1619 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %zd\n",
1620 fid->fid, (unsigned long long) offset,
1621 iov_iter_count(from));
1622
1623 while (iov_iter_count(from)) {
1624 int count = iov_iter_count(from);
1625 int rsize = fid->iounit;
1626 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1627 rsize = clnt->msize - P9_IOHDRSZ;
1628
1629 if (count < rsize)
1630 rsize = count;
1631
1632 /* Don't bother zerocopy for small IO (< 1024) */
1633 if (clnt->trans_mod->zc_request && rsize > 1024) {
1634 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, from, 0,
1635 rsize, P9_ZC_HDR_SZ, "dqd",
1636 fid->fid, offset, rsize);
1637 } else {
1638 req = p9_client_rpc(clnt, P9_TWRITE, "dqV", fid->fid,
1639 offset, rsize, from);
1640 }
1641 if (IS_ERR(req)) {
1642 *err = PTR_ERR(req);
1643 break;
1644 }
1628 1645
1629 p9_debug(P9_DEBUG_9P, ">>> TWRITE fid %d offset %llu count %d\n", 1646 *err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
1630 fid->fid, (unsigned long long) offset, count); 1647 if (*err) {
1631 err = 0; 1648 trace_9p_protocol_dump(clnt, req->rc);
1632 clnt = fid->clnt; 1649 p9_free_req(clnt, req);
1633 1650 }
1634 rsize = fid->iounit;
1635 if (!rsize || rsize > clnt->msize-P9_IOHDRSZ)
1636 rsize = clnt->msize - P9_IOHDRSZ;
1637 1651
1638 if (count < rsize) 1652 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1639 rsize = count;
1640 1653
1641 /* Don't bother zerocopy for small IO (< 1024) */ 1654 p9_free_req(clnt, req);
1642 if (clnt->trans_mod->zc_request && rsize > 1024) { 1655 iov_iter_advance(from, count);
1643 char *odata; 1656 total += count;
1644 if (data) { 1657 offset += count;
1645 kernel_buf = 1;
1646 odata = data;
1647 } else
1648 odata = (char *)udata;
1649 req = p9_client_zc_rpc(clnt, P9_TWRITE, NULL, odata, 0, rsize,
1650 P9_ZC_HDR_SZ, kernel_buf, "dqd",
1651 fid->fid, offset, rsize);
1652 } else {
1653 if (data)
1654 req = p9_client_rpc(clnt, P9_TWRITE, "dqD", fid->fid,
1655 offset, rsize, data);
1656 else
1657 req = p9_client_rpc(clnt, P9_TWRITE, "dqU", fid->fid,
1658 offset, rsize, udata);
1659 }
1660 if (IS_ERR(req)) {
1661 err = PTR_ERR(req);
1662 goto error;
1663 }
1664
1665 err = p9pdu_readf(req->rc, clnt->proto_version, "d", &count);
1666 if (err) {
1667 trace_9p_protocol_dump(clnt, req->rc);
1668 goto free_and_error;
1669 } 1658 }
1670 1659 return total;
1671 p9_debug(P9_DEBUG_9P, "<<< RWRITE count %d\n", count);
1672
1673 p9_free_req(clnt, req);
1674 return count;
1675
1676free_and_error:
1677 p9_free_req(clnt, req);
1678error:
1679 return err;
1680} 1660}
1681EXPORT_SYMBOL(p9_client_write); 1661EXPORT_SYMBOL(p9_client_write);
1682 1662
@@ -2068,6 +2048,10 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2068 struct p9_client *clnt; 2048 struct p9_client *clnt;
2069 struct p9_req_t *req; 2049 struct p9_req_t *req;
2070 char *dataptr; 2050 char *dataptr;
2051 struct kvec kv = {.iov_base = data, .iov_len = count};
2052 struct iov_iter to;
2053
2054 iov_iter_kvec(&to, READ | ITER_KVEC, &kv, 1, count);
2071 2055
2072 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n", 2056 p9_debug(P9_DEBUG_9P, ">>> TREADDIR fid %d offset %llu count %d\n",
2073 fid->fid, (unsigned long long) offset, count); 2057 fid->fid, (unsigned long long) offset, count);
@@ -2088,8 +2072,8 @@ int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset)
2088 * response header len is 11 2072 * response header len is 11
2089 * PDU Header(7) + IO Size (4) 2073 * PDU Header(7) + IO Size (4)
2090 */ 2074 */
2091 req = p9_client_zc_rpc(clnt, P9_TREADDIR, data, NULL, rsize, 0, 2075 req = p9_client_zc_rpc(clnt, P9_TREADDIR, &to, NULL, rsize, 0,
2092 11, 1, "dqd", fid->fid, offset, rsize); 2076 11, "dqd", fid->fid, offset, rsize);
2093 } else { 2077 } else {
2094 non_zc = 1; 2078 non_zc = 1;
2095 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid, 2079 req = p9_client_rpc(clnt, P9_TREADDIR, "dqd", fid->fid,
diff --git a/net/9p/protocol.c b/net/9p/protocol.c
index ab9127ec5b7a..16d287565987 100644
--- a/net/9p/protocol.c
+++ b/net/9p/protocol.c
@@ -33,6 +33,7 @@
33#include <linux/sched.h> 33#include <linux/sched.h>
34#include <linux/stddef.h> 34#include <linux/stddef.h>
35#include <linux/types.h> 35#include <linux/types.h>
36#include <linux/uio.h>
36#include <net/9p/9p.h> 37#include <net/9p/9p.h>
37#include <net/9p/client.h> 38#include <net/9p/client.h>
38#include "protocol.h" 39#include "protocol.h"
@@ -69,10 +70,11 @@ static size_t pdu_write(struct p9_fcall *pdu, const void *data, size_t size)
69} 70}
70 71
71static size_t 72static size_t
72pdu_write_u(struct p9_fcall *pdu, const char __user *udata, size_t size) 73pdu_write_u(struct p9_fcall *pdu, struct iov_iter *from, size_t size)
73{ 74{
74 size_t len = min(pdu->capacity - pdu->size, size); 75 size_t len = min(pdu->capacity - pdu->size, size);
75 if (copy_from_user(&pdu->sdata[pdu->size], udata, len)) 76 struct iov_iter i = *from;
77 if (copy_from_iter(&pdu->sdata[pdu->size], len, &i) != len)
76 len = 0; 78 len = 0;
77 79
78 pdu->size += len; 80 pdu->size += len;
@@ -273,7 +275,7 @@ p9pdu_vreadf(struct p9_fcall *pdu, int proto_version, const char *fmt,
273 } 275 }
274 break; 276 break;
275 case 'R':{ 277 case 'R':{
276 int16_t *nwqid = va_arg(ap, int16_t *); 278 uint16_t *nwqid = va_arg(ap, uint16_t *);
277 struct p9_qid **wqids = 279 struct p9_qid **wqids =
278 va_arg(ap, struct p9_qid **); 280 va_arg(ap, struct p9_qid **);
279 281
@@ -437,23 +439,13 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
437 stbuf->extension, stbuf->n_uid, 439 stbuf->extension, stbuf->n_uid,
438 stbuf->n_gid, stbuf->n_muid); 440 stbuf->n_gid, stbuf->n_muid);
439 } break; 441 } break;
440 case 'D':{ 442 case 'V':{
441 uint32_t count = va_arg(ap, uint32_t); 443 uint32_t count = va_arg(ap, uint32_t);
442 const void *data = va_arg(ap, const void *); 444 struct iov_iter *from =
443 445 va_arg(ap, struct iov_iter *);
444 errcode = p9pdu_writef(pdu, proto_version, "d",
445 count);
446 if (!errcode && pdu_write(pdu, data, count))
447 errcode = -EFAULT;
448 }
449 break;
450 case 'U':{
451 int32_t count = va_arg(ap, int32_t);
452 const char __user *udata =
453 va_arg(ap, const void __user *);
454 errcode = p9pdu_writef(pdu, proto_version, "d", 446 errcode = p9pdu_writef(pdu, proto_version, "d",
455 count); 447 count);
456 if (!errcode && pdu_write_u(pdu, udata, count)) 448 if (!errcode && pdu_write_u(pdu, from, count))
457 errcode = -EFAULT; 449 errcode = -EFAULT;
458 } 450 }
459 break; 451 break;
@@ -479,7 +471,7 @@ p9pdu_vwritef(struct p9_fcall *pdu, int proto_version, const char *fmt,
479 } 471 }
480 break; 472 break;
481 case 'R':{ 473 case 'R':{
482 int16_t nwqid = va_arg(ap, int); 474 uint16_t nwqid = va_arg(ap, int);
483 struct p9_qid *wqids = 475 struct p9_qid *wqids =
484 va_arg(ap, struct p9_qid *); 476 va_arg(ap, struct p9_qid *);
485 477
diff --git a/net/9p/trans_common.c b/net/9p/trans_common.c
index 2ee3879161b1..38aa6345bdfa 100644
--- a/net/9p/trans_common.c
+++ b/net/9p/trans_common.c
@@ -12,12 +12,8 @@
12 * 12 *
13 */ 13 */
14 14
15#include <linux/slab.h> 15#include <linux/mm.h>
16#include <linux/module.h> 16#include <linux/module.h>
17#include <net/9p/9p.h>
18#include <net/9p/client.h>
19#include <linux/scatterlist.h>
20#include "trans_common.h"
21 17
22/** 18/**
23 * p9_release_req_pages - Release pages after the transaction. 19 * p9_release_req_pages - Release pages after the transaction.
@@ -31,39 +27,3 @@ void p9_release_pages(struct page **pages, int nr_pages)
31 put_page(pages[i]); 27 put_page(pages[i]);
32} 28}
33EXPORT_SYMBOL(p9_release_pages); 29EXPORT_SYMBOL(p9_release_pages);
34
35/**
36 * p9_nr_pages - Return number of pages needed to accommodate the payload.
37 */
38int p9_nr_pages(char *data, int len)
39{
40 unsigned long start_page, end_page;
41 start_page = (unsigned long)data >> PAGE_SHIFT;
42 end_page = ((unsigned long)data + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
43 return end_page - start_page;
44}
45EXPORT_SYMBOL(p9_nr_pages);
46
47/**
48 * payload_gup - Translates user buffer into kernel pages and
49 * pins them either for read/write through get_user_pages_fast().
50 * @req: Request to be sent to server.
51 * @pdata_off: data offset into the first page after translation (gup).
52 * @pdata_len: Total length of the IO. gup may not return requested # of pages.
53 * @nr_pages: number of pages to accommodate the payload
54 * @rw: Indicates if the pages are for read or write.
55 */
56
57int p9_payload_gup(char *data, int *nr_pages, struct page **pages, int write)
58{
59 int nr_mapped_pages;
60
61 nr_mapped_pages = get_user_pages_fast((unsigned long)data,
62 *nr_pages, write, pages);
63 if (nr_mapped_pages <= 0)
64 return nr_mapped_pages;
65
66 *nr_pages = nr_mapped_pages;
67 return 0;
68}
69EXPORT_SYMBOL(p9_payload_gup);
diff --git a/net/9p/trans_common.h b/net/9p/trans_common.h
index 173bb550a9eb..c43babb3f635 100644
--- a/net/9p/trans_common.h
+++ b/net/9p/trans_common.h
@@ -13,5 +13,3 @@
13 */ 13 */
14 14
15void p9_release_pages(struct page **, int); 15void p9_release_pages(struct page **, int);
16int p9_payload_gup(char *, int *, struct page **, int);
17int p9_nr_pages(char *, int);
diff --git a/net/9p/trans_fd.c b/net/9p/trans_fd.c
index 80d08f6664cb..bced8c074c12 100644
--- a/net/9p/trans_fd.c
+++ b/net/9p/trans_fd.c
@@ -734,6 +734,7 @@ static int parse_opts(char *params, struct p9_fd_opts *opts)
734 opts->port = P9_PORT; 734 opts->port = P9_PORT;
735 opts->rfd = ~0; 735 opts->rfd = ~0;
736 opts->wfd = ~0; 736 opts->wfd = ~0;
737 opts->privport = 0;
737 738
738 if (!params) 739 if (!params)
739 return 0; 740 return 0;
@@ -940,7 +941,7 @@ p9_fd_create_tcp(struct p9_client *client, const char *addr, char *args)
940 sin_server.sin_family = AF_INET; 941 sin_server.sin_family = AF_INET;
941 sin_server.sin_addr.s_addr = in_aton(addr); 942 sin_server.sin_addr.s_addr = in_aton(addr);
942 sin_server.sin_port = htons(opts.port); 943 sin_server.sin_port = htons(opts.port);
943 err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_INET, 944 err = __sock_create(current->nsproxy->net_ns, PF_INET,
944 SOCK_STREAM, IPPROTO_TCP, &csocket, 1); 945 SOCK_STREAM, IPPROTO_TCP, &csocket, 1);
945 if (err) { 946 if (err) {
946 pr_err("%s (%d): problem creating socket\n", 947 pr_err("%s (%d): problem creating socket\n",
@@ -988,7 +989,7 @@ p9_fd_create_unix(struct p9_client *client, const char *addr, char *args)
988 989
989 sun_server.sun_family = PF_UNIX; 990 sun_server.sun_family = PF_UNIX;
990 strcpy(sun_server.sun_path, addr); 991 strcpy(sun_server.sun_path, addr);
991 err = __sock_create(read_pnet(&current->nsproxy->net_ns), PF_UNIX, 992 err = __sock_create(current->nsproxy->net_ns, PF_UNIX,
992 SOCK_STREAM, 0, &csocket, 1); 993 SOCK_STREAM, 0, &csocket, 1);
993 if (err < 0) { 994 if (err < 0) {
994 pr_err("%s (%d): problem creating socket\n", 995 pr_err("%s (%d): problem creating socket\n",
@@ -1013,7 +1014,6 @@ p9_fd_create(struct p9_client *client, const char *addr, char *args)
1013{ 1014{
1014 int err; 1015 int err;
1015 struct p9_fd_opts opts; 1016 struct p9_fd_opts opts;
1016 struct p9_trans_fd *p;
1017 1017
1018 parse_opts(args, &opts); 1018 parse_opts(args, &opts);
1019 1019
@@ -1026,7 +1026,6 @@ p9_fd_create(struct p9_client *client, const char *addr, char *args)
1026 if (err < 0) 1026 if (err < 0)
1027 return err; 1027 return err;
1028 1028
1029 p = (struct p9_trans_fd *) client->trans;
1030 p9_conn_create(client); 1029 p9_conn_create(client);
1031 1030
1032 return 0; 1031 return 0;
diff --git a/net/9p/trans_rdma.c b/net/9p/trans_rdma.c
index 14ad43b5cf89..3533d2a53ab6 100644
--- a/net/9p/trans_rdma.c
+++ b/net/9p/trans_rdma.c
@@ -139,6 +139,7 @@ struct p9_rdma_opts {
139 int sq_depth; 139 int sq_depth;
140 int rq_depth; 140 int rq_depth;
141 long timeout; 141 long timeout;
142 int privport;
142}; 143};
143 144
144/* 145/*
@@ -146,7 +147,10 @@ struct p9_rdma_opts {
146 */ 147 */
147enum { 148enum {
148 /* Options that take integer arguments */ 149 /* Options that take integer arguments */
149 Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout, Opt_err, 150 Opt_port, Opt_rq_depth, Opt_sq_depth, Opt_timeout,
151 /* Options that take no argument */
152 Opt_privport,
153 Opt_err,
150}; 154};
151 155
152static match_table_t tokens = { 156static match_table_t tokens = {
@@ -154,6 +158,7 @@ static match_table_t tokens = {
154 {Opt_sq_depth, "sq=%u"}, 158 {Opt_sq_depth, "sq=%u"},
155 {Opt_rq_depth, "rq=%u"}, 159 {Opt_rq_depth, "rq=%u"},
156 {Opt_timeout, "timeout=%u"}, 160 {Opt_timeout, "timeout=%u"},
161 {Opt_privport, "privport"},
157 {Opt_err, NULL}, 162 {Opt_err, NULL},
158}; 163};
159 164
@@ -175,6 +180,7 @@ static int parse_opts(char *params, struct p9_rdma_opts *opts)
175 opts->sq_depth = P9_RDMA_SQ_DEPTH; 180 opts->sq_depth = P9_RDMA_SQ_DEPTH;
176 opts->rq_depth = P9_RDMA_RQ_DEPTH; 181 opts->rq_depth = P9_RDMA_RQ_DEPTH;
177 opts->timeout = P9_RDMA_TIMEOUT; 182 opts->timeout = P9_RDMA_TIMEOUT;
183 opts->privport = 0;
178 184
179 if (!params) 185 if (!params)
180 return 0; 186 return 0;
@@ -193,13 +199,13 @@ static int parse_opts(char *params, struct p9_rdma_opts *opts)
193 if (!*p) 199 if (!*p)
194 continue; 200 continue;
195 token = match_token(p, tokens, args); 201 token = match_token(p, tokens, args);
196 if (token == Opt_err) 202 if ((token != Opt_err) && (token != Opt_privport)) {
197 continue; 203 r = match_int(&args[0], &option);
198 r = match_int(&args[0], &option); 204 if (r < 0) {
199 if (r < 0) { 205 p9_debug(P9_DEBUG_ERROR,
200 p9_debug(P9_DEBUG_ERROR, 206 "integer field, but no integer?\n");
201 "integer field, but no integer?\n"); 207 continue;
202 continue; 208 }
203 } 209 }
204 switch (token) { 210 switch (token) {
205 case Opt_port: 211 case Opt_port:
@@ -214,6 +220,9 @@ static int parse_opts(char *params, struct p9_rdma_opts *opts)
214 case Opt_timeout: 220 case Opt_timeout:
215 opts->timeout = option; 221 opts->timeout = option;
216 break; 222 break;
223 case Opt_privport:
224 opts->privport = 1;
225 break;
217 default: 226 default:
218 continue; 227 continue;
219 } 228 }
@@ -607,6 +616,23 @@ static int rdma_cancelled(struct p9_client *client, struct p9_req_t *req)
607 return 0; 616 return 0;
608} 617}
609 618
619static int p9_rdma_bind_privport(struct p9_trans_rdma *rdma)
620{
621 struct sockaddr_in cl = {
622 .sin_family = AF_INET,
623 .sin_addr.s_addr = htonl(INADDR_ANY),
624 };
625 int port, err = -EINVAL;
626
627 for (port = P9_DEF_MAX_RESVPORT; port >= P9_DEF_MIN_RESVPORT; port--) {
628 cl.sin_port = htons((ushort)port);
629 err = rdma_bind_addr(rdma->cm_id, (struct sockaddr *)&cl);
630 if (err != -EADDRINUSE)
631 break;
632 }
633 return err;
634}
635
610/** 636/**
611 * trans_create_rdma - Transport method for creating atransport instance 637 * trans_create_rdma - Transport method for creating atransport instance
612 * @client: client instance 638 * @client: client instance
@@ -642,6 +668,16 @@ rdma_create_trans(struct p9_client *client, const char *addr, char *args)
642 /* Associate the client with the transport */ 668 /* Associate the client with the transport */
643 client->trans = rdma; 669 client->trans = rdma;
644 670
671 /* Bind to a privileged port if we need to */
672 if (opts.privport) {
673 err = p9_rdma_bind_privport(rdma);
674 if (err < 0) {
675 pr_err("%s (%d): problem binding to privport: %d\n",
676 __func__, task_pid_nr(current), -err);
677 goto error;
678 }
679 }
680
645 /* Resolve the server's address */ 681 /* Resolve the server's address */
646 rdma->addr.sin_family = AF_INET; 682 rdma->addr.sin_family = AF_INET;
647 rdma->addr.sin_addr.s_addr = in_aton(addr); 683 rdma->addr.sin_addr.s_addr = in_aton(addr);
diff --git a/net/9p/trans_virtio.c b/net/9p/trans_virtio.c
index 36a1a739ad68..9dd49ca67dbc 100644
--- a/net/9p/trans_virtio.c
+++ b/net/9p/trans_virtio.c
@@ -217,15 +217,15 @@ static int p9_virtio_cancel(struct p9_client *client, struct p9_req_t *req)
217 * @start: which segment of the sg_list to start at 217 * @start: which segment of the sg_list to start at
218 * @pdata: a list of pages to add into sg. 218 * @pdata: a list of pages to add into sg.
219 * @nr_pages: number of pages to pack into the scatter/gather list 219 * @nr_pages: number of pages to pack into the scatter/gather list
220 * @data: data to pack into scatter/gather list 220 * @offs: amount of data in the beginning of first page _not_ to pack
221 * @count: amount of data to pack into the scatter/gather list 221 * @count: amount of data to pack into the scatter/gather list
222 */ 222 */
223static int 223static int
224pack_sg_list_p(struct scatterlist *sg, int start, int limit, 224pack_sg_list_p(struct scatterlist *sg, int start, int limit,
225 struct page **pdata, int nr_pages, char *data, int count) 225 struct page **pdata, int nr_pages, size_t offs, int count)
226{ 226{
227 int i = 0, s; 227 int i = 0, s;
228 int data_off; 228 int data_off = offs;
229 int index = start; 229 int index = start;
230 230
231 BUG_ON(nr_pages > (limit - start)); 231 BUG_ON(nr_pages > (limit - start));
@@ -233,16 +233,14 @@ pack_sg_list_p(struct scatterlist *sg, int start, int limit,
233 * if the first page doesn't start at 233 * if the first page doesn't start at
234 * page boundary find the offset 234 * page boundary find the offset
235 */ 235 */
236 data_off = offset_in_page(data);
237 while (nr_pages) { 236 while (nr_pages) {
238 s = rest_of_page(data); 237 s = PAGE_SIZE - data_off;
239 if (s > count) 238 if (s > count)
240 s = count; 239 s = count;
241 /* Make sure we don't terminate early. */ 240 /* Make sure we don't terminate early. */
242 sg_unmark_end(&sg[index]); 241 sg_unmark_end(&sg[index]);
243 sg_set_page(&sg[index++], pdata[i++], s, data_off); 242 sg_set_page(&sg[index++], pdata[i++], s, data_off);
244 data_off = 0; 243 data_off = 0;
245 data += s;
246 count -= s; 244 count -= s;
247 nr_pages--; 245 nr_pages--;
248 } 246 }
@@ -314,11 +312,20 @@ req_retry:
314} 312}
315 313
316static int p9_get_mapped_pages(struct virtio_chan *chan, 314static int p9_get_mapped_pages(struct virtio_chan *chan,
317 struct page **pages, char *data, 315 struct page ***pages,
318 int nr_pages, int write, int kern_buf) 316 struct iov_iter *data,
317 int count,
318 size_t *offs,
319 int *need_drop)
319{ 320{
321 int nr_pages;
320 int err; 322 int err;
321 if (!kern_buf) { 323
324 if (!iov_iter_count(data))
325 return 0;
326
327 if (!(data->type & ITER_KVEC)) {
328 int n;
322 /* 329 /*
323 * We allow only p9_max_pages pinned. We wait for the 330 * We allow only p9_max_pages pinned. We wait for the
324 * Other zc request to finish here 331 * Other zc request to finish here
@@ -329,26 +336,49 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
329 if (err == -ERESTARTSYS) 336 if (err == -ERESTARTSYS)
330 return err; 337 return err;
331 } 338 }
332 err = p9_payload_gup(data, &nr_pages, pages, write); 339 n = iov_iter_get_pages_alloc(data, pages, count, offs);
333 if (err < 0) 340 if (n < 0)
334 return err; 341 return n;
342 *need_drop = 1;
343 nr_pages = DIV_ROUND_UP(n + *offs, PAGE_SIZE);
335 atomic_add(nr_pages, &vp_pinned); 344 atomic_add(nr_pages, &vp_pinned);
345 return n;
336 } else { 346 } else {
337 /* kernel buffer, no need to pin pages */ 347 /* kernel buffer, no need to pin pages */
338 int s, index = 0; 348 int index;
339 int count = nr_pages; 349 size_t len;
340 while (nr_pages) { 350 void *p;
341 s = rest_of_page(data); 351
342 if (is_vmalloc_addr(data)) 352 /* we'd already checked that it's non-empty */
343 pages[index++] = vmalloc_to_page(data); 353 while (1) {
354 len = iov_iter_single_seg_count(data);
355 if (likely(len)) {
356 p = data->kvec->iov_base + data->iov_offset;
357 break;
358 }
359 iov_iter_advance(data, 0);
360 }
361 if (len > count)
362 len = count;
363
364 nr_pages = DIV_ROUND_UP((unsigned long)p + len, PAGE_SIZE) -
365 (unsigned long)p / PAGE_SIZE;
366
367 *pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
368 if (!*pages)
369 return -ENOMEM;
370
371 *need_drop = 0;
372 p -= (*offs = (unsigned long)p % PAGE_SIZE);
373 for (index = 0; index < nr_pages; index++) {
374 if (is_vmalloc_addr(p))
375 (*pages)[index] = vmalloc_to_page(p);
344 else 376 else
345 pages[index++] = kmap_to_page(data); 377 (*pages)[index] = kmap_to_page(p);
346 data += s; 378 p += PAGE_SIZE;
347 nr_pages--;
348 } 379 }
349 nr_pages = count; 380 return len;
350 } 381 }
351 return nr_pages;
352} 382}
353 383
354/** 384/**
@@ -364,8 +394,8 @@ static int p9_get_mapped_pages(struct virtio_chan *chan,
364 */ 394 */
365static int 395static int
366p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req, 396p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
367 char *uidata, char *uodata, int inlen, 397 struct iov_iter *uidata, struct iov_iter *uodata,
368 int outlen, int in_hdr_len, int kern_buf) 398 int inlen, int outlen, int in_hdr_len)
369{ 399{
370 int in, out, err, out_sgs, in_sgs; 400 int in, out, err, out_sgs, in_sgs;
371 unsigned long flags; 401 unsigned long flags;
@@ -373,41 +403,32 @@ p9_virtio_zc_request(struct p9_client *client, struct p9_req_t *req,
373 struct page **in_pages = NULL, **out_pages = NULL; 403 struct page **in_pages = NULL, **out_pages = NULL;
374 struct virtio_chan *chan = client->trans; 404 struct virtio_chan *chan = client->trans;
375 struct scatterlist *sgs[4]; 405 struct scatterlist *sgs[4];
406 size_t offs;
407 int need_drop = 0;
376 408
377 p9_debug(P9_DEBUG_TRANS, "virtio request\n"); 409 p9_debug(P9_DEBUG_TRANS, "virtio request\n");
378 410
379 if (uodata) { 411 if (uodata) {
380 out_nr_pages = p9_nr_pages(uodata, outlen); 412 int n = p9_get_mapped_pages(chan, &out_pages, uodata,
381 out_pages = kmalloc(sizeof(struct page *) * out_nr_pages, 413 outlen, &offs, &need_drop);
382 GFP_NOFS); 414 if (n < 0)
383 if (!out_pages) { 415 return n;
384 err = -ENOMEM; 416 out_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
385 goto err_out; 417 if (n != outlen) {
418 __le32 v = cpu_to_le32(n);
419 memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
420 outlen = n;
386 } 421 }
387 out_nr_pages = p9_get_mapped_pages(chan, out_pages, uodata, 422 } else if (uidata) {
388 out_nr_pages, 0, kern_buf); 423 int n = p9_get_mapped_pages(chan, &in_pages, uidata,
389 if (out_nr_pages < 0) { 424 inlen, &offs, &need_drop);
390 err = out_nr_pages; 425 if (n < 0)
391 kfree(out_pages); 426 return n;
392 out_pages = NULL; 427 in_nr_pages = DIV_ROUND_UP(n + offs, PAGE_SIZE);
393 goto err_out; 428 if (n != inlen) {
394 } 429 __le32 v = cpu_to_le32(n);
395 } 430 memcpy(&req->tc->sdata[req->tc->size - 4], &v, 4);
396 if (uidata) { 431 inlen = n;
397 in_nr_pages = p9_nr_pages(uidata, inlen);
398 in_pages = kmalloc(sizeof(struct page *) * in_nr_pages,
399 GFP_NOFS);
400 if (!in_pages) {
401 err = -ENOMEM;
402 goto err_out;
403 }
404 in_nr_pages = p9_get_mapped_pages(chan, in_pages, uidata,
405 in_nr_pages, 1, kern_buf);
406 if (in_nr_pages < 0) {
407 err = in_nr_pages;
408 kfree(in_pages);
409 in_pages = NULL;
410 goto err_out;
411 } 432 }
412 } 433 }
413 req->status = REQ_STATUS_SENT; 434 req->status = REQ_STATUS_SENT;
@@ -426,7 +447,7 @@ req_retry_pinned:
426 if (out_pages) { 447 if (out_pages) {
427 sgs[out_sgs++] = chan->sg + out; 448 sgs[out_sgs++] = chan->sg + out;
428 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM, 449 out += pack_sg_list_p(chan->sg, out, VIRTQUEUE_NUM,
429 out_pages, out_nr_pages, uodata, outlen); 450 out_pages, out_nr_pages, offs, outlen);
430 } 451 }
431 452
432 /* 453 /*
@@ -444,7 +465,7 @@ req_retry_pinned:
444 if (in_pages) { 465 if (in_pages) {
445 sgs[out_sgs + in_sgs++] = chan->sg + out + in; 466 sgs[out_sgs + in_sgs++] = chan->sg + out + in;
446 in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM, 467 in += pack_sg_list_p(chan->sg, out + in, VIRTQUEUE_NUM,
447 in_pages, in_nr_pages, uidata, inlen); 468 in_pages, in_nr_pages, offs, inlen);
448 } 469 }
449 470
450 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs)); 471 BUG_ON(out_sgs + in_sgs > ARRAY_SIZE(sgs));
@@ -478,7 +499,7 @@ req_retry_pinned:
478 * Non kernel buffers are pinned, unpin them 499 * Non kernel buffers are pinned, unpin them
479 */ 500 */
480err_out: 501err_out:
481 if (!kern_buf) { 502 if (need_drop) {
482 if (in_pages) { 503 if (in_pages) {
483 p9_release_pages(in_pages, in_nr_pages); 504 p9_release_pages(in_pages, in_nr_pages);
484 atomic_sub(in_nr_pages, &vp_pinned); 505 atomic_sub(in_nr_pages, &vp_pinned);
@@ -504,7 +525,10 @@ static ssize_t p9_mount_tag_show(struct device *dev,
504 vdev = dev_to_virtio(dev); 525 vdev = dev_to_virtio(dev);
505 chan = vdev->priv; 526 chan = vdev->priv;
506 527
507 return snprintf(buf, chan->tag_len + 1, "%s", chan->tag); 528 memcpy(buf, chan->tag, chan->tag_len);
529 buf[chan->tag_len] = 0;
530
531 return chan->tag_len + 1;
508} 532}
509 533
510static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL); 534static DEVICE_ATTR(mount_tag, 0444, p9_mount_tag_show, NULL);
diff --git a/net/Makefile b/net/Makefile
index 38704bdf941a..3995613e5510 100644
--- a/net/Makefile
+++ b/net/Makefile
@@ -69,7 +69,7 @@ obj-$(CONFIG_BATMAN_ADV) += batman-adv/
69obj-$(CONFIG_NFC) += nfc/ 69obj-$(CONFIG_NFC) += nfc/
70obj-$(CONFIG_OPENVSWITCH) += openvswitch/ 70obj-$(CONFIG_OPENVSWITCH) += openvswitch/
71obj-$(CONFIG_VSOCKETS) += vmw_vsock/ 71obj-$(CONFIG_VSOCKETS) += vmw_vsock/
72obj-$(CONFIG_NET_MPLS_GSO) += mpls/ 72obj-$(CONFIG_MPLS) += mpls/
73obj-$(CONFIG_HSR) += hsr/ 73obj-$(CONFIG_HSR) += hsr/
74ifneq ($(CONFIG_NET_SWITCHDEV),) 74ifneq ($(CONFIG_NET_SWITCHDEV),)
75obj-y += switchdev/ 75obj-y += switchdev/
diff --git a/net/appletalk/aarp.c b/net/appletalk/aarp.c
index d1c55d8dd0a2..8ad3ec2610b6 100644
--- a/net/appletalk/aarp.c
+++ b/net/appletalk/aarp.c
@@ -141,7 +141,7 @@ static void __aarp_send_query(struct aarp_entry *a)
141 eah->pa_src_net = sat->s_net; 141 eah->pa_src_net = sat->s_net;
142 eah->pa_src_node = sat->s_node; 142 eah->pa_src_node = sat->s_node;
143 143
144 memset(eah->hw_dst, '\0', ETH_ALEN); 144 eth_zero_addr(eah->hw_dst);
145 145
146 eah->pa_dst_zero = 0; 146 eah->pa_dst_zero = 0;
147 eah->pa_dst_net = a->target_addr.s_net; 147 eah->pa_dst_net = a->target_addr.s_net;
@@ -189,7 +189,7 @@ static void aarp_send_reply(struct net_device *dev, struct atalk_addr *us,
189 eah->pa_src_node = us->s_node; 189 eah->pa_src_node = us->s_node;
190 190
191 if (!sha) 191 if (!sha)
192 memset(eah->hw_dst, '\0', ETH_ALEN); 192 eth_zero_addr(eah->hw_dst);
193 else 193 else
194 ether_addr_copy(eah->hw_dst, sha); 194 ether_addr_copy(eah->hw_dst, sha);
195 195
@@ -239,7 +239,7 @@ static void aarp_send_probe(struct net_device *dev, struct atalk_addr *us)
239 eah->pa_src_net = us->s_net; 239 eah->pa_src_net = us->s_net;
240 eah->pa_src_node = us->s_node; 240 eah->pa_src_node = us->s_node;
241 241
242 memset(eah->hw_dst, '\0', ETH_ALEN); 242 eth_zero_addr(eah->hw_dst);
243 243
244 eah->pa_dst_zero = 0; 244 eah->pa_dst_zero = 0;
245 eah->pa_dst_net = us->s_net; 245 eah->pa_dst_net = us->s_net;
diff --git a/net/appletalk/ddp.c b/net/appletalk/ddp.c
index 0d0766ea5ab1..3b7ad43c7dad 100644
--- a/net/appletalk/ddp.c
+++ b/net/appletalk/ddp.c
@@ -1559,8 +1559,7 @@ freeit:
1559 return 0; 1559 return 0;
1560} 1560}
1561 1561
1562static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 1562static int atalk_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1563 size_t len)
1564{ 1563{
1565 struct sock *sk = sock->sk; 1564 struct sock *sk = sock->sk;
1566 struct atalk_sock *at = at_sk(sk); 1565 struct atalk_sock *at = at_sk(sk);
@@ -1728,8 +1727,8 @@ out:
1728 return err ? : len; 1727 return err ? : len;
1729} 1728}
1730 1729
1731static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 1730static int atalk_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1732 size_t size, int flags) 1731 int flags)
1733{ 1732{
1734 struct sock *sk = sock->sk; 1733 struct sock *sk = sock->sk;
1735 struct ddpehdr *ddp; 1734 struct ddpehdr *ddp;
diff --git a/net/atm/common.c b/net/atm/common.c
index b84057e41bd6..ed0466637e13 100644
--- a/net/atm/common.c
+++ b/net/atm/common.c
@@ -523,8 +523,8 @@ int vcc_connect(struct socket *sock, int itf, short vpi, int vci)
523 return 0; 523 return 0;
524} 524}
525 525
526int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 526int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
527 size_t size, int flags) 527 int flags)
528{ 528{
529 struct sock *sk = sock->sk; 529 struct sock *sk = sock->sk;
530 struct atm_vcc *vcc; 530 struct atm_vcc *vcc;
@@ -569,8 +569,7 @@ int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
569 return copied; 569 return copied;
570} 570}
571 571
572int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 572int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t size)
573 size_t size)
574{ 573{
575 struct sock *sk = sock->sk; 574 struct sock *sk = sock->sk;
576 DEFINE_WAIT(wait); 575 DEFINE_WAIT(wait);
diff --git a/net/atm/common.h b/net/atm/common.h
index cc3c2dae4d79..4d6f5b2068ac 100644
--- a/net/atm/common.h
+++ b/net/atm/common.h
@@ -13,10 +13,9 @@
13int vcc_create(struct net *net, struct socket *sock, int protocol, int family); 13int vcc_create(struct net *net, struct socket *sock, int protocol, int family);
14int vcc_release(struct socket *sock); 14int vcc_release(struct socket *sock);
15int vcc_connect(struct socket *sock, int itf, short vpi, int vci); 15int vcc_connect(struct socket *sock, int itf, short vpi, int vci);
16int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 16int vcc_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
17 size_t size, int flags); 17 int flags);
18int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 18int vcc_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len);
19 size_t total_len);
20unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait); 19unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait);
21int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 20int vcc_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
22int vcc_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg); 21int vcc_compat_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg);
diff --git a/net/atm/lec.c b/net/atm/lec.c
index 4b98f897044a..cd3b37989057 100644
--- a/net/atm/lec.c
+++ b/net/atm/lec.c
@@ -2001,7 +2001,7 @@ lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
2001 if (entry == NULL) 2001 if (entry == NULL)
2002 goto out; 2002 goto out;
2003 memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN); 2003 memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2004 memset(entry->mac_addr, 0, ETH_ALEN); 2004 eth_zero_addr(entry->mac_addr);
2005 entry->recv_vcc = vcc; 2005 entry->recv_vcc = vcc;
2006 entry->old_recv_push = old_push; 2006 entry->old_recv_push = old_push;
2007 entry->status = ESI_UNKNOWN; 2007 entry->status = ESI_UNKNOWN;
@@ -2086,7 +2086,7 @@ lec_vcc_added(struct lec_priv *priv, const struct atmlec_ioc *ioc_data,
2086 entry->vcc = vcc; 2086 entry->vcc = vcc;
2087 entry->old_push = old_push; 2087 entry->old_push = old_push;
2088 memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN); 2088 memcpy(entry->atm_addr, ioc_data->atm_addr, ATM_ESA_LEN);
2089 memset(entry->mac_addr, 0, ETH_ALEN); 2089 eth_zero_addr(entry->mac_addr);
2090 entry->status = ESI_UNKNOWN; 2090 entry->status = ESI_UNKNOWN;
2091 hlist_add_head(&entry->next, &priv->lec_arp_empty_ones); 2091 hlist_add_head(&entry->next, &priv->lec_arp_empty_ones);
2092 entry->timer.expires = jiffies + priv->vcc_timeout_period; 2092 entry->timer.expires = jiffies + priv->vcc_timeout_period;
diff --git a/net/atm/mpoa_proc.c b/net/atm/mpoa_proc.c
index 5bdd300db0f7..2df34eb5d65f 100644
--- a/net/atm/mpoa_proc.c
+++ b/net/atm/mpoa_proc.c
@@ -272,7 +272,7 @@ static int parse_qos(const char *buff)
272 qos.rxtp.max_pcr = rx_pcr; 272 qos.rxtp.max_pcr = rx_pcr;
273 qos.rxtp.max_sdu = rx_sdu; 273 qos.rxtp.max_sdu = rx_sdu;
274 qos.aal = ATM_AAL5; 274 qos.aal = ATM_AAL5;
275 dprintk("parse_qos(): setting qos paramameters to tx=%d,%d rx=%d,%d\n", 275 dprintk("parse_qos(): setting qos parameters to tx=%d,%d rx=%d,%d\n",
276 qos.txtp.max_pcr, qos.txtp.max_sdu, 276 qos.txtp.max_pcr, qos.txtp.max_sdu,
277 qos.rxtp.max_pcr, qos.rxtp.max_sdu); 277 qos.rxtp.max_pcr, qos.rxtp.max_sdu);
278 278
diff --git a/net/atm/signaling.c b/net/atm/signaling.c
index 523bce72f698..4fd6af47383a 100644
--- a/net/atm/signaling.c
+++ b/net/atm/signaling.c
@@ -19,36 +19,15 @@
19#include "resources.h" 19#include "resources.h"
20#include "signaling.h" 20#include "signaling.h"
21 21
22#undef WAIT_FOR_DEMON /* #define this if system calls on SVC sockets
23 should block until the demon runs.
24 Danger: may cause nasty hangs if the demon
25 crashes. */
26
27struct atm_vcc *sigd = NULL; 22struct atm_vcc *sigd = NULL;
28#ifdef WAIT_FOR_DEMON
29static DECLARE_WAIT_QUEUE_HEAD(sigd_sleep);
30#endif
31 23
32static void sigd_put_skb(struct sk_buff *skb) 24static void sigd_put_skb(struct sk_buff *skb)
33{ 25{
34#ifdef WAIT_FOR_DEMON
35 DECLARE_WAITQUEUE(wait, current);
36
37 add_wait_queue(&sigd_sleep, &wait);
38 while (!sigd) {
39 set_current_state(TASK_UNINTERRUPTIBLE);
40 pr_debug("atmsvc: waiting for signaling daemon...\n");
41 schedule();
42 }
43 current->state = TASK_RUNNING;
44 remove_wait_queue(&sigd_sleep, &wait);
45#else
46 if (!sigd) { 26 if (!sigd) {
47 pr_debug("atmsvc: no signaling daemon\n"); 27 pr_debug("atmsvc: no signaling daemon\n");
48 kfree_skb(skb); 28 kfree_skb(skb);
49 return; 29 return;
50 } 30 }
51#endif
52 atm_force_charge(sigd, skb->truesize); 31 atm_force_charge(sigd, skb->truesize);
53 skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb); 32 skb_queue_tail(&sk_atm(sigd)->sk_receive_queue, skb);
54 sk_atm(sigd)->sk_data_ready(sk_atm(sigd)); 33 sk_atm(sigd)->sk_data_ready(sk_atm(sigd));
@@ -261,8 +240,5 @@ int sigd_attach(struct atm_vcc *vcc)
261 vcc_insert_socket(sk_atm(vcc)); 240 vcc_insert_socket(sk_atm(vcc));
262 set_bit(ATM_VF_META, &vcc->flags); 241 set_bit(ATM_VF_META, &vcc->flags);
263 set_bit(ATM_VF_READY, &vcc->flags); 242 set_bit(ATM_VF_READY, &vcc->flags);
264#ifdef WAIT_FOR_DEMON
265 wake_up(&sigd_sleep);
266#endif
267 return 0; 243 return 0;
268} 244}
diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c
index ca049a7c9287..330c1f4a5a0b 100644
--- a/net/ax25/af_ax25.c
+++ b/net/ax25/af_ax25.c
@@ -1432,8 +1432,7 @@ out:
1432 return err; 1432 return err;
1433} 1433}
1434 1434
1435static int ax25_sendmsg(struct kiocb *iocb, struct socket *sock, 1435static int ax25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1436 struct msghdr *msg, size_t len)
1437{ 1436{
1438 DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name); 1437 DECLARE_SOCKADDR(struct sockaddr_ax25 *, usax, msg->msg_name);
1439 struct sock *sk = sock->sk; 1438 struct sock *sk = sock->sk;
@@ -1599,8 +1598,8 @@ out:
1599 return err; 1598 return err;
1600} 1599}
1601 1600
1602static int ax25_recvmsg(struct kiocb *iocb, struct socket *sock, 1601static int ax25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1603 struct msghdr *msg, size_t size, int flags) 1602 int flags)
1604{ 1603{
1605 struct sock *sk = sock->sk; 1604 struct sock *sk = sock->sk;
1606 struct sk_buff *skb; 1605 struct sk_buff *skb;
diff --git a/net/ax25/ax25_ip.c b/net/ax25/ax25_ip.c
index 67de6b33f2c3..7c646bb2c6f7 100644
--- a/net/ax25/ax25_ip.c
+++ b/net/ax25/ax25_ip.c
@@ -46,9 +46,9 @@
46 46
47#ifdef CONFIG_INET 47#ifdef CONFIG_INET
48 48
49int ax25_hard_header(struct sk_buff *skb, struct net_device *dev, 49static int ax25_hard_header(struct sk_buff *skb, struct net_device *dev,
50 unsigned short type, const void *daddr, 50 unsigned short type, const void *daddr,
51 const void *saddr, unsigned int len) 51 const void *saddr, unsigned int len)
52{ 52{
53 unsigned char *buff; 53 unsigned char *buff;
54 54
@@ -100,7 +100,7 @@ int ax25_hard_header(struct sk_buff *skb, struct net_device *dev,
100 return -AX25_HEADER_LEN; /* Unfinished header */ 100 return -AX25_HEADER_LEN; /* Unfinished header */
101} 101}
102 102
103int ax25_rebuild_header(struct sk_buff *skb) 103netdev_tx_t ax25_ip_xmit(struct sk_buff *skb)
104{ 104{
105 struct sk_buff *ourskb; 105 struct sk_buff *ourskb;
106 unsigned char *bp = skb->data; 106 unsigned char *bp = skb->data;
@@ -115,9 +115,6 @@ int ax25_rebuild_header(struct sk_buff *skb)
115 dst = (ax25_address *)(bp + 1); 115 dst = (ax25_address *)(bp + 1);
116 src = (ax25_address *)(bp + 8); 116 src = (ax25_address *)(bp + 8);
117 117
118 if (arp_find(bp + 1, skb))
119 return 1;
120
121 route = ax25_get_route(dst, NULL); 118 route = ax25_get_route(dst, NULL);
122 if (route) { 119 if (route) {
123 digipeat = route->digipeat; 120 digipeat = route->digipeat;
@@ -129,6 +126,7 @@ int ax25_rebuild_header(struct sk_buff *skb)
129 dev = skb->dev; 126 dev = skb->dev;
130 127
131 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) { 128 if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) {
129 kfree_skb(skb);
132 goto put; 130 goto put;
133 } 131 }
134 132
@@ -212,31 +210,29 @@ put:
212 if (route) 210 if (route)
213 ax25_put_route(route); 211 ax25_put_route(route);
214 212
215 return 1; 213 return NETDEV_TX_OK;
216} 214}
217 215
218#else /* INET */ 216#else /* INET */
219 217
220int ax25_hard_header(struct sk_buff *skb, struct net_device *dev, 218static int ax25_hard_header(struct sk_buff *skb, struct net_device *dev,
221 unsigned short type, const void *daddr, 219 unsigned short type, const void *daddr,
222 const void *saddr, unsigned int len) 220 const void *saddr, unsigned int len)
223{ 221{
224 return -AX25_HEADER_LEN; 222 return -AX25_HEADER_LEN;
225} 223}
226 224
227int ax25_rebuild_header(struct sk_buff *skb) 225netdev_tx_t ax25_ip_xmit(struct sk_buff *skb)
228{ 226{
229 return 1; 227 kfree_skb(skb);
228 return NETDEV_TX_OK;
230} 229}
231
232#endif 230#endif
233 231
234const struct header_ops ax25_header_ops = { 232const struct header_ops ax25_header_ops = {
235 .create = ax25_hard_header, 233 .create = ax25_hard_header,
236 .rebuild = ax25_rebuild_header,
237}; 234};
238 235
239EXPORT_SYMBOL(ax25_hard_header);
240EXPORT_SYMBOL(ax25_rebuild_header);
241EXPORT_SYMBOL(ax25_header_ops); 236EXPORT_SYMBOL(ax25_header_ops);
237EXPORT_SYMBOL(ax25_ip_xmit);
242 238
diff --git a/net/batman-adv/gateway_client.c b/net/batman-adv/gateway_client.c
index 27649e85f3f6..090828cf1fa7 100644
--- a/net/batman-adv/gateway_client.c
+++ b/net/batman-adv/gateway_client.c
@@ -592,15 +592,16 @@ static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
592 592
593 curr_gw = batadv_gw_get_selected_gw_node(bat_priv); 593 curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
594 594
595 ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n", 595 seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
596 (curr_gw == gw_node ? "=>" : " "), 596 (curr_gw == gw_node ? "=>" : " "),
597 gw_node->orig_node->orig, 597 gw_node->orig_node->orig,
598 router_ifinfo->bat_iv.tq_avg, router->addr, 598 router_ifinfo->bat_iv.tq_avg, router->addr,
599 router->if_incoming->net_dev->name, 599 router->if_incoming->net_dev->name,
600 gw_node->bandwidth_down / 10, 600 gw_node->bandwidth_down / 10,
601 gw_node->bandwidth_down % 10, 601 gw_node->bandwidth_down % 10,
602 gw_node->bandwidth_up / 10, 602 gw_node->bandwidth_up / 10,
603 gw_node->bandwidth_up % 10); 603 gw_node->bandwidth_up % 10);
604 ret = seq_has_overflowed(seq) ? -1 : 0;
604 605
605 if (curr_gw) 606 if (curr_gw)
606 batadv_gw_node_free_ref(curr_gw); 607 batadv_gw_node_free_ref(curr_gw);
diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c
index fbda6b54baff..baf1f9843f2c 100644
--- a/net/batman-adv/hard-interface.c
+++ b/net/batman-adv/hard-interface.c
@@ -83,11 +83,12 @@ static bool batadv_is_on_batman_iface(const struct net_device *net_dev)
83 return true; 83 return true;
84 84
85 /* no more parents..stop recursion */ 85 /* no more parents..stop recursion */
86 if (net_dev->iflink == 0 || net_dev->iflink == net_dev->ifindex) 86 if (dev_get_iflink(net_dev) == 0 ||
87 dev_get_iflink(net_dev) == net_dev->ifindex)
87 return false; 88 return false;
88 89
89 /* recurse over the parent device */ 90 /* recurse over the parent device */
90 parent_dev = __dev_get_by_index(&init_net, net_dev->iflink); 91 parent_dev = __dev_get_by_index(&init_net, dev_get_iflink(net_dev));
91 /* if we got a NULL parent_dev there is something broken.. */ 92 /* if we got a NULL parent_dev there is something broken.. */
92 if (WARN(!parent_dev, "Cannot find parent device")) 93 if (WARN(!parent_dev, "Cannot find parent device"))
93 return false; 94 return false;
diff --git a/net/bluetooth/Kconfig b/net/bluetooth/Kconfig
index 7de74635a110..b8c794b87523 100644
--- a/net/bluetooth/Kconfig
+++ b/net/bluetooth/Kconfig
@@ -91,4 +91,12 @@ config BT_SELFTEST_SMP
91 Run test cases for SMP cryptographic functionality, including both 91 Run test cases for SMP cryptographic functionality, including both
92 legacy SMP as well as the Secure Connections features. 92 legacy SMP as well as the Secure Connections features.
93 93
94config BT_DEBUGFS
95 bool "Export Bluetooth internals in debugfs"
96 depends on BT && DEBUG_FS
97 default y
98 help
99 Provide extensive information about internal Bluetooth states
100 in debugfs.
101
94source "drivers/bluetooth/Kconfig" 102source "drivers/bluetooth/Kconfig"
diff --git a/net/bluetooth/Makefile b/net/bluetooth/Makefile
index 8e96e3072266..9a8ea232d28f 100644
--- a/net/bluetooth/Makefile
+++ b/net/bluetooth/Makefile
@@ -13,8 +13,9 @@ bluetooth_6lowpan-y := 6lowpan.o
13 13
14bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \ 14bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \
15 hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \ 15 hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \
16 a2mp.o amp.o ecc.o hci_request.o hci_debugfs.o 16 a2mp.o amp.o ecc.o hci_request.o mgmt_util.o
17 17
18bluetooth-$(CONFIG_BT_DEBUGFS) += hci_debugfs.o
18bluetooth-$(CONFIG_BT_SELFTEST) += selftest.o 19bluetooth-$(CONFIG_BT_SELFTEST) += selftest.o
19 20
20subdir-ccflags-y += -D__CHECK_ENDIAN__ 21subdir-ccflags-y += -D__CHECK_ENDIAN__
diff --git a/net/bluetooth/a2mp.c b/net/bluetooth/a2mp.c
index cedfbda15dad..5a04eb1a7e57 100644
--- a/net/bluetooth/a2mp.c
+++ b/net/bluetooth/a2mp.c
@@ -19,9 +19,11 @@
19#include "a2mp.h" 19#include "a2mp.h"
20#include "amp.h" 20#include "amp.h"
21 21
22#define A2MP_FEAT_EXT 0x8000
23
22/* Global AMP Manager list */ 24/* Global AMP Manager list */
23LIST_HEAD(amp_mgr_list); 25static LIST_HEAD(amp_mgr_list);
24DEFINE_MUTEX(amp_mgr_list_lock); 26static DEFINE_MUTEX(amp_mgr_list_lock);
25 27
26/* A2MP build & send command helper functions */ 28/* A2MP build & send command helper functions */
27static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data) 29static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
@@ -43,7 +45,7 @@ static struct a2mp_cmd *__a2mp_build(u8 code, u8 ident, u16 len, void *data)
43 return cmd; 45 return cmd;
44} 46}
45 47
46void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data) 48static void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
47{ 49{
48 struct l2cap_chan *chan = mgr->a2mp_chan; 50 struct l2cap_chan *chan = mgr->a2mp_chan;
49 struct a2mp_cmd *cmd; 51 struct a2mp_cmd *cmd;
@@ -67,7 +69,7 @@ void a2mp_send(struct amp_mgr *mgr, u8 code, u8 ident, u16 len, void *data)
67 kfree(cmd); 69 kfree(cmd);
68} 70}
69 71
70u8 __next_ident(struct amp_mgr *mgr) 72static u8 __next_ident(struct amp_mgr *mgr)
71{ 73{
72 if (++mgr->ident == 0) 74 if (++mgr->ident == 0)
73 mgr->ident = 1; 75 mgr->ident = 1;
@@ -75,6 +77,23 @@ u8 __next_ident(struct amp_mgr *mgr)
75 return mgr->ident; 77 return mgr->ident;
76} 78}
77 79
80static struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
81{
82 struct amp_mgr *mgr;
83
84 mutex_lock(&amp_mgr_list_lock);
85 list_for_each_entry(mgr, &amp_mgr_list, list) {
86 if (test_and_clear_bit(state, &mgr->state)) {
87 amp_mgr_get(mgr);
88 mutex_unlock(&amp_mgr_list_lock);
89 return mgr;
90 }
91 }
92 mutex_unlock(&amp_mgr_list_lock);
93
94 return NULL;
95}
96
78/* hci_dev_list shall be locked */ 97/* hci_dev_list shall be locked */
79static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl) 98static void __a2mp_add_cl(struct amp_mgr *mgr, struct a2mp_cl *cl)
80{ 99{
@@ -860,23 +879,6 @@ struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
860 return mgr->a2mp_chan; 879 return mgr->a2mp_chan;
861} 880}
862 881
863struct amp_mgr *amp_mgr_lookup_by_state(u8 state)
864{
865 struct amp_mgr *mgr;
866
867 mutex_lock(&amp_mgr_list_lock);
868 list_for_each_entry(mgr, &amp_mgr_list, list) {
869 if (test_and_clear_bit(state, &mgr->state)) {
870 amp_mgr_get(mgr);
871 mutex_unlock(&amp_mgr_list_lock);
872 return mgr;
873 }
874 }
875 mutex_unlock(&amp_mgr_list_lock);
876
877 return NULL;
878}
879
880void a2mp_send_getinfo_rsp(struct hci_dev *hdev) 882void a2mp_send_getinfo_rsp(struct hci_dev *hdev)
881{ 883{
882 struct amp_mgr *mgr; 884 struct amp_mgr *mgr;
diff --git a/net/bluetooth/a2mp.h b/net/bluetooth/a2mp.h
index 487b54c1308f..296f665adb09 100644
--- a/net/bluetooth/a2mp.h
+++ b/net/bluetooth/a2mp.h
@@ -17,8 +17,6 @@
17 17
18#include <net/bluetooth/l2cap.h> 18#include <net/bluetooth/l2cap.h>
19 19
20#define A2MP_FEAT_EXT 0x8000
21
22enum amp_mgr_state { 20enum amp_mgr_state {
23 READ_LOC_AMP_INFO, 21 READ_LOC_AMP_INFO,
24 READ_LOC_AMP_ASSOC, 22 READ_LOC_AMP_ASSOC,
@@ -131,16 +129,10 @@ struct a2mp_physlink_rsp {
131#define A2MP_STATUS_PHYS_LINK_EXISTS 0x05 129#define A2MP_STATUS_PHYS_LINK_EXISTS 0x05
132#define A2MP_STATUS_SECURITY_VIOLATION 0x06 130#define A2MP_STATUS_SECURITY_VIOLATION 0x06
133 131
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); 132struct amp_mgr *amp_mgr_get(struct amp_mgr *mgr);
138int amp_mgr_put(struct amp_mgr *mgr); 133int amp_mgr_put(struct amp_mgr *mgr);
139u8 __next_ident(struct amp_mgr *mgr);
140struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn, 134struct l2cap_chan *a2mp_channel_create(struct l2cap_conn *conn,
141 struct sk_buff *skb); 135 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); 136void a2mp_discover_amp(struct l2cap_chan *chan);
145void a2mp_send_getinfo_rsp(struct hci_dev *hdev); 137void a2mp_send_getinfo_rsp(struct hci_dev *hdev);
146void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status); 138void a2mp_send_getampassoc_rsp(struct hci_dev *hdev, u8 status);
diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index ce22e0cfa923..70f9d945faf7 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -210,8 +210,8 @@ struct sock *bt_accept_dequeue(struct sock *parent, struct socket *newsock)
210} 210}
211EXPORT_SYMBOL(bt_accept_dequeue); 211EXPORT_SYMBOL(bt_accept_dequeue);
212 212
213int bt_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 213int bt_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
214 struct msghdr *msg, size_t len, int flags) 214 int flags)
215{ 215{
216 int noblock = flags & MSG_DONTWAIT; 216 int noblock = flags & MSG_DONTWAIT;
217 struct sock *sk = sock->sk; 217 struct sock *sk = sock->sk;
@@ -283,8 +283,8 @@ static long bt_sock_data_wait(struct sock *sk, long timeo)
283 return timeo; 283 return timeo;
284} 284}
285 285
286int bt_sock_stream_recvmsg(struct kiocb *iocb, struct socket *sock, 286int bt_sock_stream_recvmsg(struct socket *sock, struct msghdr *msg,
287 struct msghdr *msg, size_t size, int flags) 287 size_t size, int flags)
288{ 288{
289 struct sock *sk = sock->sk; 289 struct sock *sk = sock->sk;
290 int err = 0; 290 int err = 0;
@@ -711,10 +711,9 @@ EXPORT_SYMBOL_GPL(bt_debugfs);
711 711
712static int __init bt_init(void) 712static int __init bt_init(void)
713{ 713{
714 struct sk_buff *skb;
715 int err; 714 int err;
716 715
717 BUILD_BUG_ON(sizeof(struct bt_skb_cb) > sizeof(skb->cb)); 716 sock_skb_cb_check_size(sizeof(struct bt_skb_cb));
718 717
719 BT_INFO("Core ver %s", VERSION); 718 BT_INFO("Core ver %s", VERSION);
720 719
@@ -750,6 +749,13 @@ static int __init bt_init(void)
750 goto sock_err; 749 goto sock_err;
751 } 750 }
752 751
752 err = mgmt_init();
753 if (err < 0) {
754 sco_exit();
755 l2cap_exit();
756 goto sock_err;
757 }
758
753 return 0; 759 return 0;
754 760
755sock_err: 761sock_err:
@@ -764,6 +770,8 @@ error:
764 770
765static void __exit bt_exit(void) 771static void __exit bt_exit(void)
766{ 772{
773 mgmt_exit();
774
767 sco_exit(); 775 sco_exit();
768 776
769 l2cap_exit(); 777 l2cap_exit();
diff --git a/net/bluetooth/bnep/bnep.h b/net/bluetooth/bnep/bnep.h
index 5a5b16f365e9..40854c99bc1e 100644
--- a/net/bluetooth/bnep/bnep.h
+++ b/net/bluetooth/bnep/bnep.h
@@ -111,6 +111,10 @@ struct bnep_ext_hdr {
111#define BNEPCONNDEL _IOW('B', 201, int) 111#define BNEPCONNDEL _IOW('B', 201, int)
112#define BNEPGETCONNLIST _IOR('B', 210, int) 112#define BNEPGETCONNLIST _IOR('B', 210, int)
113#define BNEPGETCONNINFO _IOR('B', 211, int) 113#define BNEPGETCONNINFO _IOR('B', 211, int)
114#define BNEPGETSUPPFEAT _IOR('B', 212, int)
115
116#define BNEP_SETUP_RESPONSE 0
117#define BNEP_SETUP_RSP_SENT 10
114 118
115struct bnep_connadd_req { 119struct bnep_connadd_req {
116 int sock; /* Connected socket */ 120 int sock; /* Connected socket */
diff --git a/net/bluetooth/bnep/core.c b/net/bluetooth/bnep/core.c
index 05f57e491ccb..1641367e54ca 100644
--- a/net/bluetooth/bnep/core.c
+++ b/net/bluetooth/bnep/core.c
@@ -231,7 +231,14 @@ static int bnep_rx_control(struct bnep_session *s, void *data, int len)
231 break; 231 break;
232 232
233 case BNEP_SETUP_CONN_REQ: 233 case BNEP_SETUP_CONN_REQ:
234 err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP, BNEP_CONN_NOT_ALLOWED); 234 /* Successful response should be sent only once */
235 if (test_bit(BNEP_SETUP_RESPONSE, &s->flags) &&
236 !test_and_set_bit(BNEP_SETUP_RSP_SENT, &s->flags))
237 err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP,
238 BNEP_SUCCESS);
239 else
240 err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP,
241 BNEP_CONN_NOT_ALLOWED);
235 break; 242 break;
236 243
237 default: { 244 default: {
@@ -239,7 +246,7 @@ static int bnep_rx_control(struct bnep_session *s, void *data, int len)
239 pkt[0] = BNEP_CONTROL; 246 pkt[0] = BNEP_CONTROL;
240 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD; 247 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
241 pkt[2] = cmd; 248 pkt[2] = cmd;
242 bnep_send(s, pkt, sizeof(pkt)); 249 err = bnep_send(s, pkt, sizeof(pkt));
243 } 250 }
244 break; 251 break;
245 } 252 }
@@ -292,29 +299,55 @@ static int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
292{ 299{
293 struct net_device *dev = s->dev; 300 struct net_device *dev = s->dev;
294 struct sk_buff *nskb; 301 struct sk_buff *nskb;
295 u8 type; 302 u8 type, ctrl_type;
296 303
297 dev->stats.rx_bytes += skb->len; 304 dev->stats.rx_bytes += skb->len;
298 305
299 type = *(u8 *) skb->data; 306 type = *(u8 *) skb->data;
300 skb_pull(skb, 1); 307 skb_pull(skb, 1);
308 ctrl_type = *(u8 *)skb->data;
301 309
302 if ((type & BNEP_TYPE_MASK) >= sizeof(__bnep_rx_hlen)) 310 if ((type & BNEP_TYPE_MASK) >= sizeof(__bnep_rx_hlen))
303 goto badframe; 311 goto badframe;
304 312
305 if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) { 313 if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
306 bnep_rx_control(s, skb->data, skb->len); 314 if (bnep_rx_control(s, skb->data, skb->len) < 0) {
307 kfree_skb(skb); 315 dev->stats.tx_errors++;
308 return 0; 316 kfree_skb(skb);
309 } 317 return 0;
318 }
310 319
311 skb_reset_mac_header(skb); 320 if (!(type & BNEP_EXT_HEADER)) {
321 kfree_skb(skb);
322 return 0;
323 }
312 324
313 /* Verify and pull out header */ 325 /* Verify and pull ctrl message since it's already processed */
314 if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK])) 326 switch (ctrl_type) {
315 goto badframe; 327 case BNEP_SETUP_CONN_REQ:
328 /* Pull: ctrl type (1 b), len (1 b), data (len bytes) */
329 if (!skb_pull(skb, 2 + *(u8 *)(skb->data + 1) * 2))
330 goto badframe;
331 break;
332 case BNEP_FILTER_MULTI_ADDR_SET:
333 case BNEP_FILTER_NET_TYPE_SET:
334 /* Pull: ctrl type (1 b), len (2 b), data (len bytes) */
335 if (!skb_pull(skb, 3 + *(u16 *)(skb->data + 1) * 2))
336 goto badframe;
337 break;
338 default:
339 kfree_skb(skb);
340 return 0;
341 }
342 } else {
343 skb_reset_mac_header(skb);
316 344
317 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2)); 345 /* Verify and pull out header */
346 if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
347 goto badframe;
348
349 s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
350 }
318 351
319 if (type & BNEP_EXT_HEADER) { 352 if (type & BNEP_EXT_HEADER) {
320 if (bnep_rx_extension(s, skb) < 0) 353 if (bnep_rx_extension(s, skb) < 0)
@@ -525,6 +558,7 @@ static struct device_type bnep_type = {
525 558
526int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock) 559int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
527{ 560{
561 u32 valid_flags = BIT(BNEP_SETUP_RESPONSE);
528 struct net_device *dev; 562 struct net_device *dev;
529 struct bnep_session *s, *ss; 563 struct bnep_session *s, *ss;
530 u8 dst[ETH_ALEN], src[ETH_ALEN]; 564 u8 dst[ETH_ALEN], src[ETH_ALEN];
@@ -535,6 +569,9 @@ int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
535 if (!l2cap_is_socket(sock)) 569 if (!l2cap_is_socket(sock))
536 return -EBADFD; 570 return -EBADFD;
537 571
572 if (req->flags & ~valid_flags)
573 return -EINVAL;
574
538 baswap((void *) dst, &l2cap_pi(sock->sk)->chan->dst); 575 baswap((void *) dst, &l2cap_pi(sock->sk)->chan->dst);
539 baswap((void *) src, &l2cap_pi(sock->sk)->chan->src); 576 baswap((void *) src, &l2cap_pi(sock->sk)->chan->src);
540 577
@@ -566,6 +603,7 @@ int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
566 s->sock = sock; 603 s->sock = sock;
567 s->role = req->role; 604 s->role = req->role;
568 s->state = BT_CONNECTED; 605 s->state = BT_CONNECTED;
606 s->flags = req->flags;
569 607
570 s->msg.msg_flags = MSG_NOSIGNAL; 608 s->msg.msg_flags = MSG_NOSIGNAL;
571 609
@@ -611,11 +649,15 @@ failed:
611 649
612int bnep_del_connection(struct bnep_conndel_req *req) 650int bnep_del_connection(struct bnep_conndel_req *req)
613{ 651{
652 u32 valid_flags = 0;
614 struct bnep_session *s; 653 struct bnep_session *s;
615 int err = 0; 654 int err = 0;
616 655
617 BT_DBG(""); 656 BT_DBG("");
618 657
658 if (req->flags & ~valid_flags)
659 return -EINVAL;
660
619 down_read(&bnep_session_sem); 661 down_read(&bnep_session_sem);
620 662
621 s = __bnep_get_session(req->dst); 663 s = __bnep_get_session(req->dst);
@@ -631,10 +673,12 @@ int bnep_del_connection(struct bnep_conndel_req *req)
631 673
632static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s) 674static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
633{ 675{
676 u32 valid_flags = BIT(BNEP_SETUP_RESPONSE);
677
634 memset(ci, 0, sizeof(*ci)); 678 memset(ci, 0, sizeof(*ci));
635 memcpy(ci->dst, s->eh.h_source, ETH_ALEN); 679 memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
636 strcpy(ci->device, s->dev->name); 680 strcpy(ci->device, s->dev->name);
637 ci->flags = s->flags; 681 ci->flags = s->flags & valid_flags;
638 ci->state = s->state; 682 ci->state = s->state;
639 ci->role = s->role; 683 ci->role = s->role;
640} 684}
diff --git a/net/bluetooth/bnep/netdev.c b/net/bluetooth/bnep/netdev.c
index 4b488ec26105..6ceb5d36a32b 100644
--- a/net/bluetooth/bnep/netdev.c
+++ b/net/bluetooth/bnep/netdev.c
@@ -218,7 +218,7 @@ static const struct net_device_ops bnep_netdev_ops = {
218void bnep_net_setup(struct net_device *dev) 218void bnep_net_setup(struct net_device *dev)
219{ 219{
220 220
221 memset(dev->broadcast, 0xff, ETH_ALEN); 221 eth_broadcast_addr(dev->broadcast);
222 dev->addr_len = ETH_ALEN; 222 dev->addr_len = ETH_ALEN;
223 223
224 ether_setup(dev); 224 ether_setup(dev);
diff --git a/net/bluetooth/bnep/sock.c b/net/bluetooth/bnep/sock.c
index 5f051290daba..bde2bdd9e929 100644
--- a/net/bluetooth/bnep/sock.c
+++ b/net/bluetooth/bnep/sock.c
@@ -57,6 +57,7 @@ static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long
57 struct bnep_conninfo ci; 57 struct bnep_conninfo ci;
58 struct socket *nsock; 58 struct socket *nsock;
59 void __user *argp = (void __user *)arg; 59 void __user *argp = (void __user *)arg;
60 __u32 supp_feat = BIT(BNEP_SETUP_RESPONSE);
60 int err; 61 int err;
61 62
62 BT_DBG("cmd %x arg %lx", cmd, arg); 63 BT_DBG("cmd %x arg %lx", cmd, arg);
@@ -120,6 +121,12 @@ static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long
120 121
121 return err; 122 return err;
122 123
124 case BNEPGETSUPPFEAT:
125 if (copy_to_user(argp, &supp_feat, sizeof(supp_feat)))
126 return -EFAULT;
127
128 return 0;
129
123 default: 130 default:
124 return -EINVAL; 131 return -EINVAL;
125 } 132 }
diff --git a/net/bluetooth/cmtp/capi.c b/net/bluetooth/cmtp/capi.c
index 75bd2c42e3e7..b0c6c6af76ef 100644
--- a/net/bluetooth/cmtp/capi.c
+++ b/net/bluetooth/cmtp/capi.c
@@ -333,7 +333,7 @@ void cmtp_recv_capimsg(struct cmtp_session *session, struct sk_buff *skb)
333 return; 333 return;
334 } 334 }
335 335
336 if (session->flags & (1 << CMTP_LOOPBACK)) { 336 if (session->flags & BIT(CMTP_LOOPBACK)) {
337 kfree_skb(skb); 337 kfree_skb(skb);
338 return; 338 return;
339 } 339 }
diff --git a/net/bluetooth/cmtp/core.c b/net/bluetooth/cmtp/core.c
index 278a194e6af4..298ed37010e6 100644
--- a/net/bluetooth/cmtp/core.c
+++ b/net/bluetooth/cmtp/core.c
@@ -75,10 +75,11 @@ static void __cmtp_unlink_session(struct cmtp_session *session)
75 75
76static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci) 76static void __cmtp_copy_session(struct cmtp_session *session, struct cmtp_conninfo *ci)
77{ 77{
78 u32 valid_flags = BIT(CMTP_LOOPBACK);
78 memset(ci, 0, sizeof(*ci)); 79 memset(ci, 0, sizeof(*ci));
79 bacpy(&ci->bdaddr, &session->bdaddr); 80 bacpy(&ci->bdaddr, &session->bdaddr);
80 81
81 ci->flags = session->flags; 82 ci->flags = session->flags & valid_flags;
82 ci->state = session->state; 83 ci->state = session->state;
83 84
84 ci->num = session->num; 85 ci->num = session->num;
@@ -313,7 +314,7 @@ static int cmtp_session(void *arg)
313 314
314 down_write(&cmtp_session_sem); 315 down_write(&cmtp_session_sem);
315 316
316 if (!(session->flags & (1 << CMTP_LOOPBACK))) 317 if (!(session->flags & BIT(CMTP_LOOPBACK)))
317 cmtp_detach_device(session); 318 cmtp_detach_device(session);
318 319
319 fput(session->sock->file); 320 fput(session->sock->file);
@@ -329,6 +330,7 @@ static int cmtp_session(void *arg)
329 330
330int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock) 331int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
331{ 332{
333 u32 valid_flags = BIT(CMTP_LOOPBACK);
332 struct cmtp_session *session, *s; 334 struct cmtp_session *session, *s;
333 int i, err; 335 int i, err;
334 336
@@ -337,6 +339,9 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
337 if (!l2cap_is_socket(sock)) 339 if (!l2cap_is_socket(sock))
338 return -EBADFD; 340 return -EBADFD;
339 341
342 if (req->flags & ~valid_flags)
343 return -EINVAL;
344
340 session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL); 345 session = kzalloc(sizeof(struct cmtp_session), GFP_KERNEL);
341 if (!session) 346 if (!session)
342 return -ENOMEM; 347 return -ENOMEM;
@@ -385,7 +390,7 @@ int cmtp_add_connection(struct cmtp_connadd_req *req, struct socket *sock)
385 goto unlink; 390 goto unlink;
386 } 391 }
387 392
388 if (!(session->flags & (1 << CMTP_LOOPBACK))) { 393 if (!(session->flags & BIT(CMTP_LOOPBACK))) {
389 err = cmtp_attach_device(session); 394 err = cmtp_attach_device(session);
390 if (err < 0) { 395 if (err < 0) {
391 atomic_inc(&session->terminate); 396 atomic_inc(&session->terminate);
@@ -409,11 +414,15 @@ failed:
409 414
410int cmtp_del_connection(struct cmtp_conndel_req *req) 415int cmtp_del_connection(struct cmtp_conndel_req *req)
411{ 416{
417 u32 valid_flags = 0;
412 struct cmtp_session *session; 418 struct cmtp_session *session;
413 int err = 0; 419 int err = 0;
414 420
415 BT_DBG(""); 421 BT_DBG("");
416 422
423 if (req->flags & ~valid_flags)
424 return -EINVAL;
425
417 down_read(&cmtp_session_sem); 426 down_read(&cmtp_session_sem);
418 427
419 session = __cmtp_get_session(&req->bdaddr); 428 session = __cmtp_get_session(&req->bdaddr);
diff --git a/net/bluetooth/hci_conn.c b/net/bluetooth/hci_conn.c
index c9b8fa544785..ee5e59839b02 100644
--- a/net/bluetooth/hci_conn.c
+++ b/net/bluetooth/hci_conn.c
@@ -309,7 +309,7 @@ void hci_sco_setup(struct hci_conn *conn, __u8 status)
309 else 309 else
310 hci_add_sco(sco, conn->handle); 310 hci_add_sco(sco, conn->handle);
311 } else { 311 } else {
312 hci_proto_connect_cfm(sco, status); 312 hci_connect_cfm(sco, status);
313 hci_conn_del(sco); 313 hci_conn_del(sco);
314 } 314 }
315} 315}
@@ -571,7 +571,7 @@ struct hci_dev *hci_get_route(bdaddr_t *dst, bdaddr_t *src)
571 571
572 list_for_each_entry(d, &hci_dev_list, list) { 572 list_for_each_entry(d, &hci_dev_list, list) {
573 if (!test_bit(HCI_UP, &d->flags) || 573 if (!test_bit(HCI_UP, &d->flags) ||
574 test_bit(HCI_USER_CHANNEL, &d->dev_flags) || 574 hci_dev_test_flag(d, HCI_USER_CHANNEL) ||
575 d->dev_type != HCI_BREDR) 575 d->dev_type != HCI_BREDR)
576 continue; 576 continue;
577 577
@@ -618,7 +618,7 @@ void hci_le_conn_failed(struct hci_conn *conn, u8 status)
618 mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type, 618 mgmt_connect_failed(hdev, &conn->dst, conn->type, conn->dst_type,
619 status); 619 status);
620 620
621 hci_proto_connect_cfm(conn, status); 621 hci_connect_cfm(conn, status);
622 622
623 hci_conn_del(conn); 623 hci_conn_del(conn);
624 624
@@ -700,7 +700,7 @@ static void hci_req_directed_advertising(struct hci_request *req,
700 * and write a new random address. The flag will be set back on 700 * and write a new random address. The flag will be set back on
701 * as soon as the SET_ADV_ENABLE HCI command completes. 701 * as soon as the SET_ADV_ENABLE HCI command completes.
702 */ 702 */
703 clear_bit(HCI_LE_ADV, &hdev->dev_flags); 703 hci_dev_clear_flag(hdev, HCI_LE_ADV);
704 704
705 /* Set require_privacy to false so that the remote device has a 705 /* Set require_privacy to false so that the remote device has a
706 * chance of identifying us. 706 * chance of identifying us.
@@ -733,6 +733,14 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
733 struct hci_request req; 733 struct hci_request req;
734 int err; 734 int err;
735 735
736 /* Let's make sure that le is enabled.*/
737 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
738 if (lmp_le_capable(hdev))
739 return ERR_PTR(-ECONNREFUSED);
740
741 return ERR_PTR(-EOPNOTSUPP);
742 }
743
736 /* Some devices send ATT messages as soon as the physical link is 744 /* Some devices send ATT messages as soon as the physical link is
737 * established. To be able to handle these ATT messages, the user- 745 * established. To be able to handle these ATT messages, the user-
738 * space first establishes the connection and then starts the pairing 746 * space first establishes the connection and then starts the pairing
@@ -791,7 +799,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
791 * anyway have to disable it in order to start directed 799 * anyway have to disable it in order to start directed
792 * advertising. 800 * advertising.
793 */ 801 */
794 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { 802 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
795 u8 enable = 0x00; 803 u8 enable = 0x00;
796 hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), 804 hci_req_add(&req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable),
797 &enable); 805 &enable);
@@ -802,7 +810,7 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
802 /* If we're active scanning most controllers are unable 810 /* If we're active scanning most controllers are unable
803 * to initiate advertising. Simply reject the attempt. 811 * to initiate advertising. Simply reject the attempt.
804 */ 812 */
805 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && 813 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
806 hdev->le_scan_type == LE_SCAN_ACTIVE) { 814 hdev->le_scan_type == LE_SCAN_ACTIVE) {
807 skb_queue_purge(&req.cmd_q); 815 skb_queue_purge(&req.cmd_q);
808 hci_conn_del(conn); 816 hci_conn_del(conn);
@@ -832,9 +840,9 @@ struct hci_conn *hci_connect_le(struct hci_dev *hdev, bdaddr_t *dst,
832 * handler for scan disabling knows to set the correct discovery 840 * handler for scan disabling knows to set the correct discovery
833 * state. 841 * state.
834 */ 842 */
835 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { 843 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
836 hci_req_add_le_scan_disable(&req); 844 hci_req_add_le_scan_disable(&req);
837 set_bit(HCI_LE_SCAN_INTERRUPTED, &hdev->dev_flags); 845 hci_dev_set_flag(hdev, HCI_LE_SCAN_INTERRUPTED);
838 } 846 }
839 847
840 hci_req_add_le_create_conn(&req, conn); 848 hci_req_add_le_create_conn(&req, conn);
@@ -856,8 +864,12 @@ struct hci_conn *hci_connect_acl(struct hci_dev *hdev, bdaddr_t *dst,
856{ 864{
857 struct hci_conn *acl; 865 struct hci_conn *acl;
858 866
859 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 867 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
868 if (lmp_bredr_capable(hdev))
869 return ERR_PTR(-ECONNREFUSED);
870
860 return ERR_PTR(-EOPNOTSUPP); 871 return ERR_PTR(-EOPNOTSUPP);
872 }
861 873
862 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst); 874 acl = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
863 if (!acl) { 875 if (!acl) {
@@ -930,7 +942,7 @@ int hci_conn_check_link_mode(struct hci_conn *conn)
930 * Connections is used and the link is encrypted with AES-CCM 942 * Connections is used and the link is encrypted with AES-CCM
931 * using a P-256 authenticated combination key. 943 * using a P-256 authenticated combination key.
932 */ 944 */
933 if (test_bit(HCI_SC_ONLY, &conn->hdev->flags)) { 945 if (hci_dev_test_flag(conn->hdev, HCI_SC_ONLY)) {
934 if (!hci_conn_sc_enabled(conn) || 946 if (!hci_conn_sc_enabled(conn) ||
935 !test_bit(HCI_CONN_AES_CCM, &conn->flags) || 947 !test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
936 conn->key_type != HCI_LK_AUTH_COMBINATION_P256) 948 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)
@@ -1139,7 +1151,7 @@ void hci_conn_hash_flush(struct hci_dev *hdev)
1139 list_for_each_entry_safe(c, n, &h->list, list) { 1151 list_for_each_entry_safe(c, n, &h->list, list) {
1140 c->state = BT_CLOSED; 1152 c->state = BT_CLOSED;
1141 1153
1142 hci_proto_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM); 1154 hci_disconn_cfm(c, HCI_ERROR_LOCAL_HOST_TERM);
1143 hci_conn_del(c); 1155 hci_conn_del(c);
1144 } 1156 }
1145} 1157}
diff --git a/net/bluetooth/hci_core.c b/net/bluetooth/hci_core.c
index 3322d3f4c85a..476709bd068a 100644
--- a/net/bluetooth/hci_core.c
+++ b/net/bluetooth/hci_core.c
@@ -51,7 +51,7 @@ DEFINE_RWLOCK(hci_dev_list_lock);
51 51
52/* HCI callback list */ 52/* HCI callback list */
53LIST_HEAD(hci_cb_list); 53LIST_HEAD(hci_cb_list);
54DEFINE_RWLOCK(hci_cb_list_lock); 54DEFINE_MUTEX(hci_cb_list_lock);
55 55
56/* HCI ID Numbering */ 56/* HCI ID Numbering */
57static DEFINE_IDA(hci_index_ida); 57static DEFINE_IDA(hci_index_ida);
@@ -80,7 +80,7 @@ static ssize_t dut_mode_read(struct file *file, char __user *user_buf,
80 struct hci_dev *hdev = file->private_data; 80 struct hci_dev *hdev = file->private_data;
81 char buf[3]; 81 char buf[3];
82 82
83 buf[0] = test_bit(HCI_DUT_MODE, &hdev->dbg_flags) ? 'Y': 'N'; 83 buf[0] = hci_dev_test_flag(hdev, HCI_DUT_MODE) ? 'Y': 'N';
84 buf[1] = '\n'; 84 buf[1] = '\n';
85 buf[2] = '\0'; 85 buf[2] = '\0';
86 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 86 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -106,7 +106,7 @@ static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
106 if (strtobool(buf, &enable)) 106 if (strtobool(buf, &enable))
107 return -EINVAL; 107 return -EINVAL;
108 108
109 if (enable == test_bit(HCI_DUT_MODE, &hdev->dbg_flags)) 109 if (enable == hci_dev_test_flag(hdev, HCI_DUT_MODE))
110 return -EALREADY; 110 return -EALREADY;
111 111
112 hci_req_lock(hdev); 112 hci_req_lock(hdev);
@@ -127,7 +127,7 @@ static ssize_t dut_mode_write(struct file *file, const char __user *user_buf,
127 if (err < 0) 127 if (err < 0)
128 return err; 128 return err;
129 129
130 change_bit(HCI_DUT_MODE, &hdev->dbg_flags); 130 hci_dev_change_flag(hdev, HCI_DUT_MODE);
131 131
132 return count; 132 return count;
133} 133}
@@ -141,13 +141,16 @@ static const struct file_operations dut_mode_fops = {
141 141
142/* ---- HCI requests ---- */ 142/* ---- HCI requests ---- */
143 143
144static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode) 144static void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
145 struct sk_buff *skb)
145{ 146{
146 BT_DBG("%s result 0x%2.2x", hdev->name, result); 147 BT_DBG("%s result 0x%2.2x", hdev->name, result);
147 148
148 if (hdev->req_status == HCI_REQ_PEND) { 149 if (hdev->req_status == HCI_REQ_PEND) {
149 hdev->req_result = result; 150 hdev->req_result = result;
150 hdev->req_status = HCI_REQ_DONE; 151 hdev->req_status = HCI_REQ_DONE;
152 if (skb)
153 hdev->req_skb = skb_get(skb);
151 wake_up_interruptible(&hdev->req_wait_q); 154 wake_up_interruptible(&hdev->req_wait_q);
152 } 155 }
153} 156}
@@ -163,66 +166,12 @@ static void hci_req_cancel(struct hci_dev *hdev, int err)
163 } 166 }
164} 167}
165 168
166static struct sk_buff *hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
167 u8 event)
168{
169 struct hci_ev_cmd_complete *ev;
170 struct hci_event_hdr *hdr;
171 struct sk_buff *skb;
172
173 hci_dev_lock(hdev);
174
175 skb = hdev->recv_evt;
176 hdev->recv_evt = NULL;
177
178 hci_dev_unlock(hdev);
179
180 if (!skb)
181 return ERR_PTR(-ENODATA);
182
183 if (skb->len < sizeof(*hdr)) {
184 BT_ERR("Too short HCI event");
185 goto failed;
186 }
187
188 hdr = (void *) skb->data;
189 skb_pull(skb, HCI_EVENT_HDR_SIZE);
190
191 if (event) {
192 if (hdr->evt != event)
193 goto failed;
194 return skb;
195 }
196
197 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
198 BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
199 goto failed;
200 }
201
202 if (skb->len < sizeof(*ev)) {
203 BT_ERR("Too short cmd_complete event");
204 goto failed;
205 }
206
207 ev = (void *) skb->data;
208 skb_pull(skb, sizeof(*ev));
209
210 if (opcode == __le16_to_cpu(ev->opcode))
211 return skb;
212
213 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
214 __le16_to_cpu(ev->opcode));
215
216failed:
217 kfree_skb(skb);
218 return ERR_PTR(-ENODATA);
219}
220
221struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen, 169struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
222 const void *param, u8 event, u32 timeout) 170 const void *param, u8 event, u32 timeout)
223{ 171{
224 DECLARE_WAITQUEUE(wait, current); 172 DECLARE_WAITQUEUE(wait, current);
225 struct hci_request req; 173 struct hci_request req;
174 struct sk_buff *skb;
226 int err = 0; 175 int err = 0;
227 176
228 BT_DBG("%s", hdev->name); 177 BT_DBG("%s", hdev->name);
@@ -236,7 +185,7 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
236 add_wait_queue(&hdev->req_wait_q, &wait); 185 add_wait_queue(&hdev->req_wait_q, &wait);
237 set_current_state(TASK_INTERRUPTIBLE); 186 set_current_state(TASK_INTERRUPTIBLE);
238 187
239 err = hci_req_run(&req, hci_req_sync_complete); 188 err = hci_req_run_skb(&req, hci_req_sync_complete);
240 if (err < 0) { 189 if (err < 0) {
241 remove_wait_queue(&hdev->req_wait_q, &wait); 190 remove_wait_queue(&hdev->req_wait_q, &wait);
242 set_current_state(TASK_RUNNING); 191 set_current_state(TASK_RUNNING);
@@ -265,13 +214,20 @@ struct sk_buff *__hci_cmd_sync_ev(struct hci_dev *hdev, u16 opcode, u32 plen,
265 } 214 }
266 215
267 hdev->req_status = hdev->req_result = 0; 216 hdev->req_status = hdev->req_result = 0;
217 skb = hdev->req_skb;
218 hdev->req_skb = NULL;
268 219
269 BT_DBG("%s end: err %d", hdev->name, err); 220 BT_DBG("%s end: err %d", hdev->name, err);
270 221
271 if (err < 0) 222 if (err < 0) {
223 kfree_skb(skb);
272 return ERR_PTR(err); 224 return ERR_PTR(err);
225 }
226
227 if (!skb)
228 return ERR_PTR(-ENODATA);
273 229
274 return hci_get_cmd_complete(hdev, opcode, event); 230 return skb;
275} 231}
276EXPORT_SYMBOL(__hci_cmd_sync_ev); 232EXPORT_SYMBOL(__hci_cmd_sync_ev);
277 233
@@ -303,7 +259,7 @@ static int __hci_req_sync(struct hci_dev *hdev,
303 add_wait_queue(&hdev->req_wait_q, &wait); 259 add_wait_queue(&hdev->req_wait_q, &wait);
304 set_current_state(TASK_INTERRUPTIBLE); 260 set_current_state(TASK_INTERRUPTIBLE);
305 261
306 err = hci_req_run(&req, hci_req_sync_complete); 262 err = hci_req_run_skb(&req, hci_req_sync_complete);
307 if (err < 0) { 263 if (err < 0) {
308 hdev->req_status = 0; 264 hdev->req_status = 0;
309 265
@@ -390,7 +346,7 @@ static void bredr_init(struct hci_request *req)
390 hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL); 346 hci_req_add(req, HCI_OP_READ_BD_ADDR, 0, NULL);
391} 347}
392 348
393static void amp_init(struct hci_request *req) 349static void amp_init1(struct hci_request *req)
394{ 350{
395 req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED; 351 req->hdev->flow_ctl_mode = HCI_FLOW_CTL_MODE_BLOCK_BASED;
396 352
@@ -400,9 +356,6 @@ static void amp_init(struct hci_request *req)
400 /* Read Local Supported Commands */ 356 /* Read Local Supported Commands */
401 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL); 357 hci_req_add(req, HCI_OP_READ_LOCAL_COMMANDS, 0, NULL);
402 358
403 /* Read Local Supported Features */
404 hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
405
406 /* Read Local AMP Info */ 359 /* Read Local AMP Info */
407 hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL); 360 hci_req_add(req, HCI_OP_READ_LOCAL_AMP_INFO, 0, NULL);
408 361
@@ -416,6 +369,16 @@ static void amp_init(struct hci_request *req)
416 hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL); 369 hci_req_add(req, HCI_OP_READ_LOCATION_DATA, 0, NULL);
417} 370}
418 371
372static void amp_init2(struct hci_request *req)
373{
374 /* Read Local Supported Features. Not all AMP controllers
375 * support this so it's placed conditionally in the second
376 * stage init.
377 */
378 if (req->hdev->commands[14] & 0x20)
379 hci_req_add(req, HCI_OP_READ_LOCAL_FEATURES, 0, NULL);
380}
381
419static void hci_init1_req(struct hci_request *req, unsigned long opt) 382static void hci_init1_req(struct hci_request *req, unsigned long opt)
420{ 383{
421 struct hci_dev *hdev = req->hdev; 384 struct hci_dev *hdev = req->hdev;
@@ -432,7 +395,7 @@ static void hci_init1_req(struct hci_request *req, unsigned long opt)
432 break; 395 break;
433 396
434 case HCI_AMP: 397 case HCI_AMP:
435 amp_init(req); 398 amp_init1(req);
436 break; 399 break;
437 400
438 default: 401 default:
@@ -494,7 +457,7 @@ static void le_setup(struct hci_request *req)
494 457
495 /* LE-only controllers have LE implicitly enabled */ 458 /* LE-only controllers have LE implicitly enabled */
496 if (!lmp_bredr_capable(hdev)) 459 if (!lmp_bredr_capable(hdev))
497 set_bit(HCI_LE_ENABLED, &hdev->dev_flags); 460 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
498} 461}
499 462
500static void hci_setup_event_mask(struct hci_request *req) 463static void hci_setup_event_mask(struct hci_request *req)
@@ -578,10 +541,13 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
578{ 541{
579 struct hci_dev *hdev = req->hdev; 542 struct hci_dev *hdev = req->hdev;
580 543
544 if (hdev->dev_type == HCI_AMP)
545 return amp_init2(req);
546
581 if (lmp_bredr_capable(hdev)) 547 if (lmp_bredr_capable(hdev))
582 bredr_setup(req); 548 bredr_setup(req);
583 else 549 else
584 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 550 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
585 551
586 if (lmp_le_capable(hdev)) 552 if (lmp_le_capable(hdev))
587 le_setup(req); 553 le_setup(req);
@@ -607,7 +573,7 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
607 */ 573 */
608 hdev->max_page = 0x01; 574 hdev->max_page = 0x01;
609 575
610 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 576 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
611 u8 mode = 0x01; 577 u8 mode = 0x01;
612 578
613 hci_req_add(req, HCI_OP_WRITE_SSP_MODE, 579 hci_req_add(req, HCI_OP_WRITE_SSP_MODE,
@@ -646,7 +612,7 @@ static void hci_init2_req(struct hci_request *req, unsigned long opt)
646 sizeof(cp), &cp); 612 sizeof(cp), &cp);
647 } 613 }
648 614
649 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) { 615 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
650 u8 enable = 1; 616 u8 enable = 1;
651 hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable), 617 hci_req_add(req, HCI_OP_WRITE_AUTH_ENABLE, sizeof(enable),
652 &enable); 618 &enable);
@@ -683,7 +649,7 @@ static void hci_set_le_support(struct hci_request *req)
683 649
684 memset(&cp, 0, sizeof(cp)); 650 memset(&cp, 0, sizeof(cp));
685 651
686 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 652 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
687 cp.le = 0x01; 653 cp.le = 0x01;
688 cp.simul = 0x00; 654 cp.simul = 0x00;
689 } 655 }
@@ -871,7 +837,7 @@ static void hci_init4_req(struct hci_request *req, unsigned long opt)
871 hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL); 837 hci_req_add(req, HCI_OP_READ_SYNC_TRAIN_PARAMS, 0, NULL);
872 838
873 /* Enable Secure Connections if supported and configured */ 839 /* Enable Secure Connections if supported and configured */
874 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && 840 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
875 bredr_sc_enabled(hdev)) { 841 bredr_sc_enabled(hdev)) {
876 u8 support = 0x01; 842 u8 support = 0x01;
877 843
@@ -891,22 +857,22 @@ static int __hci_init(struct hci_dev *hdev)
891 /* The Device Under Test (DUT) mode is special and available for 857 /* The Device Under Test (DUT) mode is special and available for
892 * all controller types. So just create it early on. 858 * all controller types. So just create it early on.
893 */ 859 */
894 if (test_bit(HCI_SETUP, &hdev->dev_flags)) { 860 if (hci_dev_test_flag(hdev, HCI_SETUP)) {
895 debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev, 861 debugfs_create_file("dut_mode", 0644, hdev->debugfs, hdev,
896 &dut_mode_fops); 862 &dut_mode_fops);
897 } 863 }
898 864
865 err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT);
866 if (err < 0)
867 return err;
868
899 /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode 869 /* HCI_BREDR covers both single-mode LE, BR/EDR and dual-mode
900 * BR/EDR/LE type controllers. AMP controllers only need the 870 * BR/EDR/LE type controllers. AMP controllers only need the
901 * first stage init. 871 * first two stages of init.
902 */ 872 */
903 if (hdev->dev_type != HCI_BREDR) 873 if (hdev->dev_type != HCI_BREDR)
904 return 0; 874 return 0;
905 875
906 err = __hci_req_sync(hdev, hci_init2_req, 0, HCI_INIT_TIMEOUT);
907 if (err < 0)
908 return err;
909
910 err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT); 876 err = __hci_req_sync(hdev, hci_init3_req, 0, HCI_INIT_TIMEOUT);
911 if (err < 0) 877 if (err < 0)
912 return err; 878 return err;
@@ -927,8 +893,8 @@ static int __hci_init(struct hci_dev *hdev)
927 * So only when in setup phase or config phase, create the debugfs 893 * So only when in setup phase or config phase, create the debugfs
928 * entries and register the SMP channels. 894 * entries and register the SMP channels.
929 */ 895 */
930 if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 896 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
931 !test_bit(HCI_CONFIG, &hdev->dev_flags)) 897 !hci_dev_test_flag(hdev, HCI_CONFIG))
932 return 0; 898 return 0;
933 899
934 hci_debugfs_create_common(hdev); 900 hci_debugfs_create_common(hdev);
@@ -1290,12 +1256,12 @@ int hci_inquiry(void __user *arg)
1290 if (!hdev) 1256 if (!hdev)
1291 return -ENODEV; 1257 return -ENODEV;
1292 1258
1293 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1259 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1294 err = -EBUSY; 1260 err = -EBUSY;
1295 goto done; 1261 goto done;
1296 } 1262 }
1297 1263
1298 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 1264 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1299 err = -EOPNOTSUPP; 1265 err = -EOPNOTSUPP;
1300 goto done; 1266 goto done;
1301 } 1267 }
@@ -1305,7 +1271,7 @@ int hci_inquiry(void __user *arg)
1305 goto done; 1271 goto done;
1306 } 1272 }
1307 1273
1308 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 1274 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1309 err = -EOPNOTSUPP; 1275 err = -EOPNOTSUPP;
1310 goto done; 1276 goto done;
1311 } 1277 }
@@ -1377,17 +1343,17 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1377 1343
1378 hci_req_lock(hdev); 1344 hci_req_lock(hdev);
1379 1345
1380 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) { 1346 if (hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1381 ret = -ENODEV; 1347 ret = -ENODEV;
1382 goto done; 1348 goto done;
1383 } 1349 }
1384 1350
1385 if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 1351 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
1386 !test_bit(HCI_CONFIG, &hdev->dev_flags)) { 1352 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
1387 /* Check for rfkill but allow the HCI setup stage to 1353 /* Check for rfkill but allow the HCI setup stage to
1388 * proceed (which in itself doesn't cause any RF activity). 1354 * proceed (which in itself doesn't cause any RF activity).
1389 */ 1355 */
1390 if (test_bit(HCI_RFKILLED, &hdev->dev_flags)) { 1356 if (hci_dev_test_flag(hdev, HCI_RFKILLED)) {
1391 ret = -ERFKILL; 1357 ret = -ERFKILL;
1392 goto done; 1358 goto done;
1393 } 1359 }
@@ -1404,7 +1370,7 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1404 * This check is only valid for BR/EDR controllers 1370 * This check is only valid for BR/EDR controllers
1405 * since AMP controllers do not have an address. 1371 * since AMP controllers do not have an address.
1406 */ 1372 */
1407 if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && 1373 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1408 hdev->dev_type == HCI_BREDR && 1374 hdev->dev_type == HCI_BREDR &&
1409 !bacmp(&hdev->bdaddr, BDADDR_ANY) && 1375 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
1410 !bacmp(&hdev->static_addr, BDADDR_ANY)) { 1376 !bacmp(&hdev->static_addr, BDADDR_ANY)) {
@@ -1426,7 +1392,7 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1426 atomic_set(&hdev->cmd_cnt, 1); 1392 atomic_set(&hdev->cmd_cnt, 1);
1427 set_bit(HCI_INIT, &hdev->flags); 1393 set_bit(HCI_INIT, &hdev->flags);
1428 1394
1429 if (test_bit(HCI_SETUP, &hdev->dev_flags)) { 1395 if (hci_dev_test_flag(hdev, HCI_SETUP)) {
1430 if (hdev->setup) 1396 if (hdev->setup)
1431 ret = hdev->setup(hdev); 1397 ret = hdev->setup(hdev);
1432 1398
@@ -1438,7 +1404,7 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1438 */ 1404 */
1439 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || 1405 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
1440 test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks)) 1406 test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks))
1441 set_bit(HCI_UNCONFIGURED, &hdev->dev_flags); 1407 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
1442 1408
1443 /* For an unconfigured controller it is required to 1409 /* For an unconfigured controller it is required to
1444 * read at least the version information provided by 1410 * read at least the version information provided by
@@ -1448,11 +1414,11 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1448 * also the original Bluetooth public device address 1414 * also the original Bluetooth public device address
1449 * will be read using the Read BD Address command. 1415 * will be read using the Read BD Address command.
1450 */ 1416 */
1451 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 1417 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
1452 ret = __hci_unconf_init(hdev); 1418 ret = __hci_unconf_init(hdev);
1453 } 1419 }
1454 1420
1455 if (test_bit(HCI_CONFIG, &hdev->dev_flags)) { 1421 if (hci_dev_test_flag(hdev, HCI_CONFIG)) {
1456 /* If public address change is configured, ensure that 1422 /* If public address change is configured, ensure that
1457 * the address gets programmed. If the driver does not 1423 * the address gets programmed. If the driver does not
1458 * support changing the public address, fail the power 1424 * support changing the public address, fail the power
@@ -1466,8 +1432,8 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1466 } 1432 }
1467 1433
1468 if (!ret) { 1434 if (!ret) {
1469 if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 1435 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1470 !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) 1436 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1471 ret = __hci_init(hdev); 1437 ret = __hci_init(hdev);
1472 } 1438 }
1473 1439
@@ -1475,13 +1441,13 @@ static int hci_dev_do_open(struct hci_dev *hdev)
1475 1441
1476 if (!ret) { 1442 if (!ret) {
1477 hci_dev_hold(hdev); 1443 hci_dev_hold(hdev);
1478 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 1444 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1479 set_bit(HCI_UP, &hdev->flags); 1445 set_bit(HCI_UP, &hdev->flags);
1480 hci_notify(hdev, HCI_DEV_UP); 1446 hci_notify(hdev, HCI_DEV_UP);
1481 if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 1447 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
1482 !test_bit(HCI_CONFIG, &hdev->dev_flags) && 1448 !hci_dev_test_flag(hdev, HCI_CONFIG) &&
1483 !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 1449 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1484 !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && 1450 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1485 hdev->dev_type == HCI_BREDR) { 1451 hdev->dev_type == HCI_BREDR) {
1486 hci_dev_lock(hdev); 1452 hci_dev_lock(hdev);
1487 mgmt_powered(hdev, 1); 1453 mgmt_powered(hdev, 1);
@@ -1533,8 +1499,8 @@ int hci_dev_open(__u16 dev)
1533 * HCI_USER_CHANNEL will be set first before attempting to 1499 * HCI_USER_CHANNEL will be set first before attempting to
1534 * open the device. 1500 * open the device.
1535 */ 1501 */
1536 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 1502 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1537 !test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1503 !hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1538 err = -EOPNOTSUPP; 1504 err = -EOPNOTSUPP;
1539 goto done; 1505 goto done;
1540 } 1506 }
@@ -1544,7 +1510,7 @@ int hci_dev_open(__u16 dev)
1544 * particularly important if the setup procedure has not yet 1510 * particularly important if the setup procedure has not yet
1545 * completed. 1511 * completed.
1546 */ 1512 */
1547 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 1513 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
1548 cancel_delayed_work(&hdev->power_off); 1514 cancel_delayed_work(&hdev->power_off);
1549 1515
1550 /* After this call it is guaranteed that the setup procedure 1516 /* After this call it is guaranteed that the setup procedure
@@ -1559,9 +1525,9 @@ int hci_dev_open(__u16 dev)
1559 * is in use this bit will be cleared again and userspace has 1525 * is in use this bit will be cleared again and userspace has
1560 * to explicitly enable it. 1526 * to explicitly enable it.
1561 */ 1527 */
1562 if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags) && 1528 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
1563 !test_bit(HCI_MGMT, &hdev->dev_flags)) 1529 !hci_dev_test_flag(hdev, HCI_MGMT))
1564 set_bit(HCI_BONDABLE, &hdev->dev_flags); 1530 hci_dev_set_flag(hdev, HCI_BONDABLE);
1565 1531
1566 err = hci_dev_do_open(hdev); 1532 err = hci_dev_do_open(hdev);
1567 1533
@@ -1591,6 +1557,12 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1591{ 1557{
1592 BT_DBG("%s %p", hdev->name, hdev); 1558 BT_DBG("%s %p", hdev->name, hdev);
1593 1559
1560 if (!hci_dev_test_flag(hdev, HCI_UNREGISTER)) {
1561 /* Execute vendor specific shutdown routine */
1562 if (hdev->shutdown)
1563 hdev->shutdown(hdev);
1564 }
1565
1594 cancel_delayed_work(&hdev->power_off); 1566 cancel_delayed_work(&hdev->power_off);
1595 1567
1596 hci_req_cancel(hdev, ENODEV); 1568 hci_req_cancel(hdev, ENODEV);
@@ -1609,17 +1581,17 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1609 if (hdev->discov_timeout > 0) { 1581 if (hdev->discov_timeout > 0) {
1610 cancel_delayed_work(&hdev->discov_off); 1582 cancel_delayed_work(&hdev->discov_off);
1611 hdev->discov_timeout = 0; 1583 hdev->discov_timeout = 0;
1612 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 1584 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1613 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1585 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1614 } 1586 }
1615 1587
1616 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 1588 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
1617 cancel_delayed_work(&hdev->service_cache); 1589 cancel_delayed_work(&hdev->service_cache);
1618 1590
1619 cancel_delayed_work_sync(&hdev->le_scan_disable); 1591 cancel_delayed_work_sync(&hdev->le_scan_disable);
1620 cancel_delayed_work_sync(&hdev->le_scan_restart); 1592 cancel_delayed_work_sync(&hdev->le_scan_restart);
1621 1593
1622 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 1594 if (hci_dev_test_flag(hdev, HCI_MGMT))
1623 cancel_delayed_work_sync(&hdev->rpa_expired); 1595 cancel_delayed_work_sync(&hdev->rpa_expired);
1624 1596
1625 /* Avoid potential lockdep warnings from the *_flush() calls by 1597 /* Avoid potential lockdep warnings from the *_flush() calls by
@@ -1631,7 +1603,7 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1631 1603
1632 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 1604 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1633 1605
1634 if (!test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 1606 if (!hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
1635 if (hdev->dev_type == HCI_BREDR) 1607 if (hdev->dev_type == HCI_BREDR)
1636 mgmt_powered(hdev, 0); 1608 mgmt_powered(hdev, 0);
1637 } 1609 }
@@ -1651,8 +1623,8 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1651 /* Reset device */ 1623 /* Reset device */
1652 skb_queue_purge(&hdev->cmd_q); 1624 skb_queue_purge(&hdev->cmd_q);
1653 atomic_set(&hdev->cmd_cnt, 1); 1625 atomic_set(&hdev->cmd_cnt, 1);
1654 if (!test_bit(HCI_AUTO_OFF, &hdev->dev_flags) && 1626 if (!hci_dev_test_flag(hdev, HCI_AUTO_OFF) &&
1655 !test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 1627 !hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1656 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) { 1628 test_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks)) {
1657 set_bit(HCI_INIT, &hdev->flags); 1629 set_bit(HCI_INIT, &hdev->flags);
1658 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT); 1630 __hci_req_sync(hdev, hci_reset_req, 0, HCI_CMD_TIMEOUT);
@@ -1674,16 +1646,13 @@ static int hci_dev_do_close(struct hci_dev *hdev)
1674 hdev->sent_cmd = NULL; 1646 hdev->sent_cmd = NULL;
1675 } 1647 }
1676 1648
1677 kfree_skb(hdev->recv_evt);
1678 hdev->recv_evt = NULL;
1679
1680 /* After this point our queues are empty 1649 /* After this point our queues are empty
1681 * and no tasks are scheduled. */ 1650 * and no tasks are scheduled. */
1682 hdev->close(hdev); 1651 hdev->close(hdev);
1683 1652
1684 /* Clear flags */ 1653 /* Clear flags */
1685 hdev->flags &= BIT(HCI_RAW); 1654 hdev->flags &= BIT(HCI_RAW);
1686 hdev->dev_flags &= ~HCI_PERSISTENT_MASK; 1655 hci_dev_clear_volatile_flags(hdev);
1687 1656
1688 /* Controller radio is available but is currently powered down */ 1657 /* Controller radio is available but is currently powered down */
1689 hdev->amp_status = AMP_STATUS_POWERED_DOWN; 1658 hdev->amp_status = AMP_STATUS_POWERED_DOWN;
@@ -1707,12 +1676,12 @@ int hci_dev_close(__u16 dev)
1707 if (!hdev) 1676 if (!hdev)
1708 return -ENODEV; 1677 return -ENODEV;
1709 1678
1710 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1679 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1711 err = -EBUSY; 1680 err = -EBUSY;
1712 goto done; 1681 goto done;
1713 } 1682 }
1714 1683
1715 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 1684 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF))
1716 cancel_delayed_work(&hdev->power_off); 1685 cancel_delayed_work(&hdev->power_off);
1717 1686
1718 err = hci_dev_do_close(hdev); 1687 err = hci_dev_do_close(hdev);
@@ -1770,12 +1739,12 @@ int hci_dev_reset(__u16 dev)
1770 goto done; 1739 goto done;
1771 } 1740 }
1772 1741
1773 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1742 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1774 err = -EBUSY; 1743 err = -EBUSY;
1775 goto done; 1744 goto done;
1776 } 1745 }
1777 1746
1778 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 1747 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1779 err = -EOPNOTSUPP; 1748 err = -EOPNOTSUPP;
1780 goto done; 1749 goto done;
1781 } 1750 }
@@ -1796,12 +1765,12 @@ int hci_dev_reset_stat(__u16 dev)
1796 if (!hdev) 1765 if (!hdev)
1797 return -ENODEV; 1766 return -ENODEV;
1798 1767
1799 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1768 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1800 ret = -EBUSY; 1769 ret = -EBUSY;
1801 goto done; 1770 goto done;
1802 } 1771 }
1803 1772
1804 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 1773 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1805 ret = -EOPNOTSUPP; 1774 ret = -EOPNOTSUPP;
1806 goto done; 1775 goto done;
1807 } 1776 }
@@ -1820,29 +1789,29 @@ static void hci_update_scan_state(struct hci_dev *hdev, u8 scan)
1820 BT_DBG("%s scan 0x%02x", hdev->name, scan); 1789 BT_DBG("%s scan 0x%02x", hdev->name, scan);
1821 1790
1822 if ((scan & SCAN_PAGE)) 1791 if ((scan & SCAN_PAGE))
1823 conn_changed = !test_and_set_bit(HCI_CONNECTABLE, 1792 conn_changed = !hci_dev_test_and_set_flag(hdev,
1824 &hdev->dev_flags); 1793 HCI_CONNECTABLE);
1825 else 1794 else
1826 conn_changed = test_and_clear_bit(HCI_CONNECTABLE, 1795 conn_changed = hci_dev_test_and_clear_flag(hdev,
1827 &hdev->dev_flags); 1796 HCI_CONNECTABLE);
1828 1797
1829 if ((scan & SCAN_INQUIRY)) { 1798 if ((scan & SCAN_INQUIRY)) {
1830 discov_changed = !test_and_set_bit(HCI_DISCOVERABLE, 1799 discov_changed = !hci_dev_test_and_set_flag(hdev,
1831 &hdev->dev_flags); 1800 HCI_DISCOVERABLE);
1832 } else { 1801 } else {
1833 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1802 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1834 discov_changed = test_and_clear_bit(HCI_DISCOVERABLE, 1803 discov_changed = hci_dev_test_and_clear_flag(hdev,
1835 &hdev->dev_flags); 1804 HCI_DISCOVERABLE);
1836 } 1805 }
1837 1806
1838 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 1807 if (!hci_dev_test_flag(hdev, HCI_MGMT))
1839 return; 1808 return;
1840 1809
1841 if (conn_changed || discov_changed) { 1810 if (conn_changed || discov_changed) {
1842 /* In case this was disabled through mgmt */ 1811 /* In case this was disabled through mgmt */
1843 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 1812 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
1844 1813
1845 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 1814 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1846 mgmt_update_adv_data(hdev); 1815 mgmt_update_adv_data(hdev);
1847 1816
1848 mgmt_new_settings(hdev); 1817 mgmt_new_settings(hdev);
@@ -1862,12 +1831,12 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
1862 if (!hdev) 1831 if (!hdev)
1863 return -ENODEV; 1832 return -ENODEV;
1864 1833
1865 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 1834 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1866 err = -EBUSY; 1835 err = -EBUSY;
1867 goto done; 1836 goto done;
1868 } 1837 }
1869 1838
1870 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 1839 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
1871 err = -EOPNOTSUPP; 1840 err = -EOPNOTSUPP;
1872 goto done; 1841 goto done;
1873 } 1842 }
@@ -1877,7 +1846,7 @@ int hci_dev_cmd(unsigned int cmd, void __user *arg)
1877 goto done; 1846 goto done;
1878 } 1847 }
1879 1848
1880 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 1849 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1881 err = -EOPNOTSUPP; 1850 err = -EOPNOTSUPP;
1882 goto done; 1851 goto done;
1883 } 1852 }
@@ -1981,7 +1950,7 @@ int hci_get_dev_list(void __user *arg)
1981 * is running, but in that case still indicate that the 1950 * is running, but in that case still indicate that the
1982 * device is actually down. 1951 * device is actually down.
1983 */ 1952 */
1984 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 1953 if (hci_dev_test_flag(hdev, HCI_AUTO_OFF))
1985 flags &= ~BIT(HCI_UP); 1954 flags &= ~BIT(HCI_UP);
1986 1955
1987 (dr + n)->dev_id = hdev->id; 1956 (dr + n)->dev_id = hdev->id;
@@ -2019,7 +1988,7 @@ int hci_get_dev_info(void __user *arg)
2019 * is running, but in that case still indicate that the 1988 * is running, but in that case still indicate that the
2020 * device is actually down. 1989 * device is actually down.
2021 */ 1990 */
2022 if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) 1991 if (hci_dev_test_flag(hdev, HCI_AUTO_OFF))
2023 flags = hdev->flags & ~BIT(HCI_UP); 1992 flags = hdev->flags & ~BIT(HCI_UP);
2024 else 1993 else
2025 flags = hdev->flags; 1994 flags = hdev->flags;
@@ -2062,16 +2031,16 @@ static int hci_rfkill_set_block(void *data, bool blocked)
2062 2031
2063 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked); 2032 BT_DBG("%p name %s blocked %d", hdev, hdev->name, blocked);
2064 2033
2065 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) 2034 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
2066 return -EBUSY; 2035 return -EBUSY;
2067 2036
2068 if (blocked) { 2037 if (blocked) {
2069 set_bit(HCI_RFKILLED, &hdev->dev_flags); 2038 hci_dev_set_flag(hdev, HCI_RFKILLED);
2070 if (!test_bit(HCI_SETUP, &hdev->dev_flags) && 2039 if (!hci_dev_test_flag(hdev, HCI_SETUP) &&
2071 !test_bit(HCI_CONFIG, &hdev->dev_flags)) 2040 !hci_dev_test_flag(hdev, HCI_CONFIG))
2072 hci_dev_do_close(hdev); 2041 hci_dev_do_close(hdev);
2073 } else { 2042 } else {
2074 clear_bit(HCI_RFKILLED, &hdev->dev_flags); 2043 hci_dev_clear_flag(hdev, HCI_RFKILLED);
2075 } 2044 }
2076 2045
2077 return 0; 2046 return 0;
@@ -2100,23 +2069,23 @@ static void hci_power_on(struct work_struct *work)
2100 * ignored and they need to be checked now. If they are still 2069 * ignored and they need to be checked now. If they are still
2101 * valid, it is important to turn the device back off. 2070 * valid, it is important to turn the device back off.
2102 */ 2071 */
2103 if (test_bit(HCI_RFKILLED, &hdev->dev_flags) || 2072 if (hci_dev_test_flag(hdev, HCI_RFKILLED) ||
2104 test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) || 2073 hci_dev_test_flag(hdev, HCI_UNCONFIGURED) ||
2105 (hdev->dev_type == HCI_BREDR && 2074 (hdev->dev_type == HCI_BREDR &&
2106 !bacmp(&hdev->bdaddr, BDADDR_ANY) && 2075 !bacmp(&hdev->bdaddr, BDADDR_ANY) &&
2107 !bacmp(&hdev->static_addr, BDADDR_ANY))) { 2076 !bacmp(&hdev->static_addr, BDADDR_ANY))) {
2108 clear_bit(HCI_AUTO_OFF, &hdev->dev_flags); 2077 hci_dev_clear_flag(hdev, HCI_AUTO_OFF);
2109 hci_dev_do_close(hdev); 2078 hci_dev_do_close(hdev);
2110 } else if (test_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 2079 } else if (hci_dev_test_flag(hdev, HCI_AUTO_OFF)) {
2111 queue_delayed_work(hdev->req_workqueue, &hdev->power_off, 2080 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
2112 HCI_AUTO_OFF_TIMEOUT); 2081 HCI_AUTO_OFF_TIMEOUT);
2113 } 2082 }
2114 2083
2115 if (test_and_clear_bit(HCI_SETUP, &hdev->dev_flags)) { 2084 if (hci_dev_test_and_clear_flag(hdev, HCI_SETUP)) {
2116 /* For unconfigured devices, set the HCI_RAW flag 2085 /* For unconfigured devices, set the HCI_RAW flag
2117 * so that userspace can easily identify them. 2086 * so that userspace can easily identify them.
2118 */ 2087 */
2119 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 2088 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
2120 set_bit(HCI_RAW, &hdev->flags); 2089 set_bit(HCI_RAW, &hdev->flags);
2121 2090
2122 /* For fully configured devices, this will send 2091 /* For fully configured devices, this will send
@@ -2127,11 +2096,11 @@ static void hci_power_on(struct work_struct *work)
2127 * and no event will be send. 2096 * and no event will be send.
2128 */ 2097 */
2129 mgmt_index_added(hdev); 2098 mgmt_index_added(hdev);
2130 } else if (test_and_clear_bit(HCI_CONFIG, &hdev->dev_flags)) { 2099 } else if (hci_dev_test_and_clear_flag(hdev, HCI_CONFIG)) {
2131 /* When the controller is now configured, then it 2100 /* When the controller is now configured, then it
2132 * is important to clear the HCI_RAW flag. 2101 * is important to clear the HCI_RAW flag.
2133 */ 2102 */
2134 if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 2103 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
2135 clear_bit(HCI_RAW, &hdev->flags); 2104 clear_bit(HCI_RAW, &hdev->flags);
2136 2105
2137 /* Powering on the controller with HCI_CONFIG set only 2106 /* Powering on the controller with HCI_CONFIG set only
@@ -2500,6 +2469,42 @@ void hci_remove_irk(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 addr_type)
2500 } 2469 }
2501} 2470}
2502 2471
2472bool hci_bdaddr_is_paired(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 type)
2473{
2474 struct smp_ltk *k;
2475 struct smp_irk *irk;
2476 u8 addr_type;
2477
2478 if (type == BDADDR_BREDR) {
2479 if (hci_find_link_key(hdev, bdaddr))
2480 return true;
2481 return false;
2482 }
2483
2484 /* Convert to HCI addr type which struct smp_ltk uses */
2485 if (type == BDADDR_LE_PUBLIC)
2486 addr_type = ADDR_LE_DEV_PUBLIC;
2487 else
2488 addr_type = ADDR_LE_DEV_RANDOM;
2489
2490 irk = hci_get_irk(hdev, bdaddr, addr_type);
2491 if (irk) {
2492 bdaddr = &irk->bdaddr;
2493 addr_type = irk->addr_type;
2494 }
2495
2496 rcu_read_lock();
2497 list_for_each_entry_rcu(k, &hdev->long_term_keys, list) {
2498 if (k->bdaddr_type == addr_type && !bacmp(bdaddr, &k->bdaddr)) {
2499 rcu_read_unlock();
2500 return true;
2501 }
2502 }
2503 rcu_read_unlock();
2504
2505 return false;
2506}
2507
2503/* HCI command timer function */ 2508/* HCI command timer function */
2504static void hci_cmd_timeout(struct work_struct *work) 2509static void hci_cmd_timeout(struct work_struct *work)
2505{ 2510{
@@ -2822,7 +2827,6 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
2822{ 2827{
2823 /* General inquiry access code (GIAC) */ 2828 /* General inquiry access code (GIAC) */
2824 u8 lap[3] = { 0x33, 0x8b, 0x9e }; 2829 u8 lap[3] = { 0x33, 0x8b, 0x9e };
2825 struct hci_request req;
2826 struct hci_cp_inquiry cp; 2830 struct hci_cp_inquiry cp;
2827 int err; 2831 int err;
2828 2832
@@ -2841,21 +2845,37 @@ static void le_scan_disable_work_complete(struct hci_dev *hdev, u8 status,
2841 break; 2845 break;
2842 2846
2843 case DISCOV_TYPE_INTERLEAVED: 2847 case DISCOV_TYPE_INTERLEAVED:
2844 hci_req_init(&req, hdev); 2848 hci_dev_lock(hdev);
2845 2849
2846 memset(&cp, 0, sizeof(cp)); 2850 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
2847 memcpy(&cp.lap, lap, sizeof(cp.lap)); 2851 &hdev->quirks)) {
2848 cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN; 2852 /* If we were running LE only scan, change discovery
2849 hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp); 2853 * state. If we were running both LE and BR/EDR inquiry
2854 * simultaneously, and BR/EDR inquiry is already
2855 * finished, stop discovery, otherwise BR/EDR inquiry
2856 * will stop discovery when finished.
2857 */
2858 if (!test_bit(HCI_INQUIRY, &hdev->flags))
2859 hci_discovery_set_state(hdev,
2860 DISCOVERY_STOPPED);
2861 } else {
2862 struct hci_request req;
2850 2863
2851 hci_dev_lock(hdev); 2864 hci_inquiry_cache_flush(hdev);
2852 2865
2853 hci_inquiry_cache_flush(hdev); 2866 hci_req_init(&req, hdev);
2854 2867
2855 err = hci_req_run(&req, inquiry_complete); 2868 memset(&cp, 0, sizeof(cp));
2856 if (err) { 2869 memcpy(&cp.lap, lap, sizeof(cp.lap));
2857 BT_ERR("Inquiry request failed: err %d", err); 2870 cp.length = DISCOV_INTERLEAVED_INQUIRY_LEN;
2858 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 2871 hci_req_add(&req, HCI_OP_INQUIRY, sizeof(cp), &cp);
2872
2873 err = hci_req_run(&req, inquiry_complete);
2874 if (err) {
2875 BT_ERR("Inquiry request failed: err %d", err);
2876 hci_discovery_set_state(hdev,
2877 DISCOVERY_STOPPED);
2878 }
2859 } 2879 }
2860 2880
2861 hci_dev_unlock(hdev); 2881 hci_dev_unlock(hdev);
@@ -2934,7 +2954,7 @@ static void le_scan_restart_work(struct work_struct *work)
2934 BT_DBG("%s", hdev->name); 2954 BT_DBG("%s", hdev->name);
2935 2955
2936 /* If controller is not scanning we are done. */ 2956 /* If controller is not scanning we are done. */
2937 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 2957 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
2938 return; 2958 return;
2939 2959
2940 hci_req_init(&req, hdev); 2960 hci_req_init(&req, hdev);
@@ -2967,9 +2987,9 @@ static void le_scan_restart_work(struct work_struct *work)
2967void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr, 2987void hci_copy_identity_address(struct hci_dev *hdev, bdaddr_t *bdaddr,
2968 u8 *bdaddr_type) 2988 u8 *bdaddr_type)
2969{ 2989{
2970 if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) || 2990 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
2971 !bacmp(&hdev->bdaddr, BDADDR_ANY) || 2991 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
2972 (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) && 2992 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
2973 bacmp(&hdev->static_addr, BDADDR_ANY))) { 2993 bacmp(&hdev->static_addr, BDADDR_ANY))) {
2974 bacpy(bdaddr, &hdev->static_addr); 2994 bacpy(bdaddr, &hdev->static_addr);
2975 *bdaddr_type = ADDR_LE_DEV_RANDOM; 2995 *bdaddr_type = ADDR_LE_DEV_RANDOM;
@@ -3059,6 +3079,7 @@ struct hci_dev *hci_alloc_dev(void)
3059 3079
3060 hci_init_sysfs(hdev); 3080 hci_init_sysfs(hdev);
3061 discovery_init(hdev); 3081 discovery_init(hdev);
3082 adv_info_init(hdev);
3062 3083
3063 return hdev; 3084 return hdev;
3064} 3085}
@@ -3137,16 +3158,16 @@ int hci_register_dev(struct hci_dev *hdev)
3137 } 3158 }
3138 3159
3139 if (hdev->rfkill && rfkill_blocked(hdev->rfkill)) 3160 if (hdev->rfkill && rfkill_blocked(hdev->rfkill))
3140 set_bit(HCI_RFKILLED, &hdev->dev_flags); 3161 hci_dev_set_flag(hdev, HCI_RFKILLED);
3141 3162
3142 set_bit(HCI_SETUP, &hdev->dev_flags); 3163 hci_dev_set_flag(hdev, HCI_SETUP);
3143 set_bit(HCI_AUTO_OFF, &hdev->dev_flags); 3164 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
3144 3165
3145 if (hdev->dev_type == HCI_BREDR) { 3166 if (hdev->dev_type == HCI_BREDR) {
3146 /* Assume BR/EDR support until proven otherwise (such as 3167 /* Assume BR/EDR support until proven otherwise (such as
3147 * through reading supported features during init. 3168 * through reading supported features during init.
3148 */ 3169 */
3149 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 3170 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
3150 } 3171 }
3151 3172
3152 write_lock(&hci_dev_list_lock); 3173 write_lock(&hci_dev_list_lock);
@@ -3157,7 +3178,7 @@ int hci_register_dev(struct hci_dev *hdev)
3157 * and should not be included in normal operation. 3178 * and should not be included in normal operation.
3158 */ 3179 */
3159 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 3180 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
3160 set_bit(HCI_UNCONFIGURED, &hdev->dev_flags); 3181 hci_dev_set_flag(hdev, HCI_UNCONFIGURED);
3161 3182
3162 hci_notify(hdev, HCI_DEV_REG); 3183 hci_notify(hdev, HCI_DEV_REG);
3163 hci_dev_hold(hdev); 3184 hci_dev_hold(hdev);
@@ -3179,11 +3200,11 @@ EXPORT_SYMBOL(hci_register_dev);
3179/* Unregister HCI device */ 3200/* Unregister HCI device */
3180void hci_unregister_dev(struct hci_dev *hdev) 3201void hci_unregister_dev(struct hci_dev *hdev)
3181{ 3202{
3182 int i, id; 3203 int id;
3183 3204
3184 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus); 3205 BT_DBG("%p name %s bus %d", hdev, hdev->name, hdev->bus);
3185 3206
3186 set_bit(HCI_UNREGISTER, &hdev->dev_flags); 3207 hci_dev_set_flag(hdev, HCI_UNREGISTER);
3187 3208
3188 id = hdev->id; 3209 id = hdev->id;
3189 3210
@@ -3193,14 +3214,11 @@ void hci_unregister_dev(struct hci_dev *hdev)
3193 3214
3194 hci_dev_do_close(hdev); 3215 hci_dev_do_close(hdev);
3195 3216
3196 for (i = 0; i < NUM_REASSEMBLY; i++)
3197 kfree_skb(hdev->reassembly[i]);
3198
3199 cancel_work_sync(&hdev->power_on); 3217 cancel_work_sync(&hdev->power_on);
3200 3218
3201 if (!test_bit(HCI_INIT, &hdev->flags) && 3219 if (!test_bit(HCI_INIT, &hdev->flags) &&
3202 !test_bit(HCI_SETUP, &hdev->dev_flags) && 3220 !hci_dev_test_flag(hdev, HCI_SETUP) &&
3203 !test_bit(HCI_CONFIG, &hdev->dev_flags)) { 3221 !hci_dev_test_flag(hdev, HCI_CONFIG)) {
3204 hci_dev_lock(hdev); 3222 hci_dev_lock(hdev);
3205 mgmt_index_removed(hdev); 3223 mgmt_index_removed(hdev);
3206 hci_dev_unlock(hdev); 3224 hci_dev_unlock(hdev);
@@ -3299,158 +3317,15 @@ int hci_recv_frame(struct hci_dev *hdev, struct sk_buff *skb)
3299} 3317}
3300EXPORT_SYMBOL(hci_recv_frame); 3318EXPORT_SYMBOL(hci_recv_frame);
3301 3319
3302static int hci_reassembly(struct hci_dev *hdev, int type, void *data,
3303 int count, __u8 index)
3304{
3305 int len = 0;
3306 int hlen = 0;
3307 int remain = count;
3308 struct sk_buff *skb;
3309 struct bt_skb_cb *scb;
3310
3311 if ((type < HCI_ACLDATA_PKT || type > HCI_EVENT_PKT) ||
3312 index >= NUM_REASSEMBLY)
3313 return -EILSEQ;
3314
3315 skb = hdev->reassembly[index];
3316
3317 if (!skb) {
3318 switch (type) {
3319 case HCI_ACLDATA_PKT:
3320 len = HCI_MAX_FRAME_SIZE;
3321 hlen = HCI_ACL_HDR_SIZE;
3322 break;
3323 case HCI_EVENT_PKT:
3324 len = HCI_MAX_EVENT_SIZE;
3325 hlen = HCI_EVENT_HDR_SIZE;
3326 break;
3327 case HCI_SCODATA_PKT:
3328 len = HCI_MAX_SCO_SIZE;
3329 hlen = HCI_SCO_HDR_SIZE;
3330 break;
3331 }
3332
3333 skb = bt_skb_alloc(len, GFP_ATOMIC);
3334 if (!skb)
3335 return -ENOMEM;
3336
3337 scb = (void *) skb->cb;
3338 scb->expect = hlen;
3339 scb->pkt_type = type;
3340
3341 hdev->reassembly[index] = skb;
3342 }
3343
3344 while (count) {
3345 scb = (void *) skb->cb;
3346 len = min_t(uint, scb->expect, count);
3347
3348 memcpy(skb_put(skb, len), data, len);
3349
3350 count -= len;
3351 data += len;
3352 scb->expect -= len;
3353 remain = count;
3354
3355 switch (type) {
3356 case HCI_EVENT_PKT:
3357 if (skb->len == HCI_EVENT_HDR_SIZE) {
3358 struct hci_event_hdr *h = hci_event_hdr(skb);
3359 scb->expect = h->plen;
3360
3361 if (skb_tailroom(skb) < scb->expect) {
3362 kfree_skb(skb);
3363 hdev->reassembly[index] = NULL;
3364 return -ENOMEM;
3365 }
3366 }
3367 break;
3368
3369 case HCI_ACLDATA_PKT:
3370 if (skb->len == HCI_ACL_HDR_SIZE) {
3371 struct hci_acl_hdr *h = hci_acl_hdr(skb);
3372 scb->expect = __le16_to_cpu(h->dlen);
3373
3374 if (skb_tailroom(skb) < scb->expect) {
3375 kfree_skb(skb);
3376 hdev->reassembly[index] = NULL;
3377 return -ENOMEM;
3378 }
3379 }
3380 break;
3381
3382 case HCI_SCODATA_PKT:
3383 if (skb->len == HCI_SCO_HDR_SIZE) {
3384 struct hci_sco_hdr *h = hci_sco_hdr(skb);
3385 scb->expect = h->dlen;
3386
3387 if (skb_tailroom(skb) < scb->expect) {
3388 kfree_skb(skb);
3389 hdev->reassembly[index] = NULL;
3390 return -ENOMEM;
3391 }
3392 }
3393 break;
3394 }
3395
3396 if (scb->expect == 0) {
3397 /* Complete frame */
3398
3399 bt_cb(skb)->pkt_type = type;
3400 hci_recv_frame(hdev, skb);
3401
3402 hdev->reassembly[index] = NULL;
3403 return remain;
3404 }
3405 }
3406
3407 return remain;
3408}
3409
3410#define STREAM_REASSEMBLY 0
3411
3412int hci_recv_stream_fragment(struct hci_dev *hdev, void *data, int count)
3413{
3414 int type;
3415 int rem = 0;
3416
3417 while (count) {
3418 struct sk_buff *skb = hdev->reassembly[STREAM_REASSEMBLY];
3419
3420 if (!skb) {
3421 struct { char type; } *pkt;
3422
3423 /* Start of the frame */
3424 pkt = data;
3425 type = pkt->type;
3426
3427 data++;
3428 count--;
3429 } else
3430 type = bt_cb(skb)->pkt_type;
3431
3432 rem = hci_reassembly(hdev, type, data, count,
3433 STREAM_REASSEMBLY);
3434 if (rem < 0)
3435 return rem;
3436
3437 data += (count - rem);
3438 count = rem;
3439 }
3440
3441 return rem;
3442}
3443EXPORT_SYMBOL(hci_recv_stream_fragment);
3444
3445/* ---- Interface to upper protocols ---- */ 3320/* ---- Interface to upper protocols ---- */
3446 3321
3447int hci_register_cb(struct hci_cb *cb) 3322int hci_register_cb(struct hci_cb *cb)
3448{ 3323{
3449 BT_DBG("%p name %s", cb, cb->name); 3324 BT_DBG("%p name %s", cb, cb->name);
3450 3325
3451 write_lock(&hci_cb_list_lock); 3326 mutex_lock(&hci_cb_list_lock);
3452 list_add(&cb->list, &hci_cb_list); 3327 list_add_tail(&cb->list, &hci_cb_list);
3453 write_unlock(&hci_cb_list_lock); 3328 mutex_unlock(&hci_cb_list_lock);
3454 3329
3455 return 0; 3330 return 0;
3456} 3331}
@@ -3460,9 +3335,9 @@ int hci_unregister_cb(struct hci_cb *cb)
3460{ 3335{
3461 BT_DBG("%p name %s", cb, cb->name); 3336 BT_DBG("%p name %s", cb, cb->name);
3462 3337
3463 write_lock(&hci_cb_list_lock); 3338 mutex_lock(&hci_cb_list_lock);
3464 list_del(&cb->list); 3339 list_del(&cb->list);
3465 write_unlock(&hci_cb_list_lock); 3340 mutex_unlock(&hci_cb_list_lock);
3466 3341
3467 return 0; 3342 return 0;
3468} 3343}
@@ -3495,11 +3370,6 @@ static void hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
3495 } 3370 }
3496} 3371}
3497 3372
3498bool hci_req_pending(struct hci_dev *hdev)
3499{
3500 return (hdev->req_status == HCI_REQ_PEND);
3501}
3502
3503/* Send HCI command */ 3373/* Send HCI command */
3504int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen, 3374int hci_send_cmd(struct hci_dev *hdev, __u16 opcode, __u32 plen,
3505 const void *param) 3375 const void *param)
@@ -3874,7 +3744,7 @@ static inline int __get_blocks(struct hci_dev *hdev, struct sk_buff *skb)
3874 3744
3875static void __check_timeout(struct hci_dev *hdev, unsigned int cnt) 3745static void __check_timeout(struct hci_dev *hdev, unsigned int cnt)
3876{ 3746{
3877 if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 3747 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
3878 /* ACL tx timeout must be longer than maximum 3748 /* ACL tx timeout must be longer than maximum
3879 * link supervision timeout (40.9 seconds) */ 3749 * link supervision timeout (40.9 seconds) */
3880 if (!cnt && time_after(jiffies, hdev->acl_last_tx + 3750 if (!cnt && time_after(jiffies, hdev->acl_last_tx +
@@ -4057,7 +3927,7 @@ static void hci_sched_le(struct hci_dev *hdev)
4057 if (!hci_conn_num(hdev, LE_LINK)) 3927 if (!hci_conn_num(hdev, LE_LINK))
4058 return; 3928 return;
4059 3929
4060 if (!test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 3930 if (!hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
4061 /* LE tx timeout must be longer than maximum 3931 /* LE tx timeout must be longer than maximum
4062 * link supervision timeout (40.9 seconds) */ 3932 * link supervision timeout (40.9 seconds) */
4063 if (!hdev->le_cnt && hdev->le_pkts && 3933 if (!hdev->le_cnt && hdev->le_pkts &&
@@ -4105,7 +3975,7 @@ static void hci_tx_work(struct work_struct *work)
4105 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt, 3975 BT_DBG("%s acl %d sco %d le %d", hdev->name, hdev->acl_cnt,
4106 hdev->sco_cnt, hdev->le_cnt); 3976 hdev->sco_cnt, hdev->le_cnt);
4107 3977
4108 if (!test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 3978 if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4109 /* Schedule queues and send stuff to HCI driver */ 3979 /* Schedule queues and send stuff to HCI driver */
4110 hci_sched_acl(hdev); 3980 hci_sched_acl(hdev);
4111 hci_sched_sco(hdev); 3981 hci_sched_sco(hdev);
@@ -4220,9 +4090,10 @@ static void hci_resend_last(struct hci_dev *hdev)
4220 queue_work(hdev->workqueue, &hdev->cmd_work); 4090 queue_work(hdev->workqueue, &hdev->cmd_work);
4221} 4091}
4222 4092
4223void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status) 4093void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
4094 hci_req_complete_t *req_complete,
4095 hci_req_complete_skb_t *req_complete_skb)
4224{ 4096{
4225 hci_req_complete_t req_complete = NULL;
4226 struct sk_buff *skb; 4097 struct sk_buff *skb;
4227 unsigned long flags; 4098 unsigned long flags;
4228 4099
@@ -4254,18 +4125,14 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
4254 * callback would be found in hdev->sent_cmd instead of the 4125 * callback would be found in hdev->sent_cmd instead of the
4255 * command queue (hdev->cmd_q). 4126 * command queue (hdev->cmd_q).
4256 */ 4127 */
4257 if (hdev->sent_cmd) { 4128 if (bt_cb(hdev->sent_cmd)->req.complete) {
4258 req_complete = bt_cb(hdev->sent_cmd)->req.complete; 4129 *req_complete = bt_cb(hdev->sent_cmd)->req.complete;
4259 4130 return;
4260 if (req_complete) { 4131 }
4261 /* We must set the complete callback to NULL to
4262 * avoid calling the callback more than once if
4263 * this function gets called again.
4264 */
4265 bt_cb(hdev->sent_cmd)->req.complete = NULL;
4266 4132
4267 goto call_complete; 4133 if (bt_cb(hdev->sent_cmd)->req.complete_skb) {
4268 } 4134 *req_complete_skb = bt_cb(hdev->sent_cmd)->req.complete_skb;
4135 return;
4269 } 4136 }
4270 4137
4271 /* Remove all pending commands belonging to this request */ 4138 /* Remove all pending commands belonging to this request */
@@ -4276,14 +4143,11 @@ void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status)
4276 break; 4143 break;
4277 } 4144 }
4278 4145
4279 req_complete = bt_cb(skb)->req.complete; 4146 *req_complete = bt_cb(skb)->req.complete;
4147 *req_complete_skb = bt_cb(skb)->req.complete_skb;
4280 kfree_skb(skb); 4148 kfree_skb(skb);
4281 } 4149 }
4282 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); 4150 spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
4283
4284call_complete:
4285 if (req_complete)
4286 req_complete(hdev, status, status ? opcode : HCI_OP_NOP);
4287} 4151}
4288 4152
4289static void hci_rx_work(struct work_struct *work) 4153static void hci_rx_work(struct work_struct *work)
@@ -4302,7 +4166,7 @@ static void hci_rx_work(struct work_struct *work)
4302 hci_send_to_sock(hdev, skb); 4166 hci_send_to_sock(hdev, skb);
4303 } 4167 }
4304 4168
4305 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 4169 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
4306 kfree_skb(skb); 4170 kfree_skb(skb);
4307 continue; 4171 continue;
4308 } 4172 }
diff --git a/net/bluetooth/hci_debugfs.c b/net/bluetooth/hci_debugfs.c
index 65261e5d4b84..7db4220941cc 100644
--- a/net/bluetooth/hci_debugfs.c
+++ b/net/bluetooth/hci_debugfs.c
@@ -28,6 +28,54 @@
28 28
29#include "hci_debugfs.h" 29#include "hci_debugfs.h"
30 30
31#define DEFINE_QUIRK_ATTRIBUTE(__name, __quirk) \
32static ssize_t __name ## _read(struct file *file, \
33 char __user *user_buf, \
34 size_t count, loff_t *ppos) \
35{ \
36 struct hci_dev *hdev = file->private_data; \
37 char buf[3]; \
38 \
39 buf[0] = test_bit(__quirk, &hdev->quirks) ? 'Y' : 'N'; \
40 buf[1] = '\n'; \
41 buf[2] = '\0'; \
42 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); \
43} \
44 \
45static ssize_t __name ## _write(struct file *file, \
46 const char __user *user_buf, \
47 size_t count, loff_t *ppos) \
48{ \
49 struct hci_dev *hdev = file->private_data; \
50 char buf[32]; \
51 size_t buf_size = min(count, (sizeof(buf) - 1)); \
52 bool enable; \
53 \
54 if (test_bit(HCI_UP, &hdev->flags)) \
55 return -EBUSY; \
56 \
57 if (copy_from_user(buf, user_buf, buf_size)) \
58 return -EFAULT; \
59 \
60 buf[buf_size] = '\0'; \
61 if (strtobool(buf, &enable)) \
62 return -EINVAL; \
63 \
64 if (enable == test_bit(__quirk, &hdev->quirks)) \
65 return -EALREADY; \
66 \
67 change_bit(__quirk, &hdev->quirks); \
68 \
69 return count; \
70} \
71 \
72static const struct file_operations __name ## _fops = { \
73 .open = simple_open, \
74 .read = __name ## _read, \
75 .write = __name ## _write, \
76 .llseek = default_llseek, \
77} \
78
31static int features_show(struct seq_file *f, void *ptr) 79static int features_show(struct seq_file *f, void *ptr)
32{ 80{
33 struct hci_dev *hdev = f->private; 81 struct hci_dev *hdev = f->private;
@@ -66,6 +114,30 @@ static const struct file_operations features_fops = {
66 .release = single_release, 114 .release = single_release,
67}; 115};
68 116
117static int device_id_show(struct seq_file *f, void *ptr)
118{
119 struct hci_dev *hdev = f->private;
120
121 hci_dev_lock(hdev);
122 seq_printf(f, "%4.4x:%4.4x:%4.4x:%4.4x\n", hdev->devid_source,
123 hdev->devid_vendor, hdev->devid_product, hdev->devid_version);
124 hci_dev_unlock(hdev);
125
126 return 0;
127}
128
129static int device_id_open(struct inode *inode, struct file *file)
130{
131 return single_open(file, device_id_show, inode->i_private);
132}
133
134static const struct file_operations device_id_fops = {
135 .open = device_id_open,
136 .read = seq_read,
137 .llseek = seq_lseek,
138 .release = single_release,
139};
140
69static int device_list_show(struct seq_file *f, void *ptr) 141static int device_list_show(struct seq_file *f, void *ptr)
70{ 142{
71 struct hci_dev *hdev = f->private; 143 struct hci_dev *hdev = f->private;
@@ -166,7 +238,7 @@ static int remote_oob_show(struct seq_file *f, void *ptr)
166 seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n", 238 seq_printf(f, "%pMR (type %u) %u %*phN %*phN %*phN %*phN\n",
167 &data->bdaddr, data->bdaddr_type, data->present, 239 &data->bdaddr, data->bdaddr_type, data->present,
168 16, data->hash192, 16, data->rand192, 240 16, data->hash192, 16, data->rand192,
169 16, data->hash256, 19, data->rand256); 241 16, data->hash256, 16, data->rand256);
170 } 242 }
171 hci_dev_unlock(hdev); 243 hci_dev_unlock(hdev);
172 244
@@ -247,7 +319,7 @@ static ssize_t use_debug_keys_read(struct file *file, char __user *user_buf,
247 struct hci_dev *hdev = file->private_data; 319 struct hci_dev *hdev = file->private_data;
248 char buf[3]; 320 char buf[3];
249 321
250 buf[0] = test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags) ? 'Y': 'N'; 322 buf[0] = hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS) ? 'Y': 'N';
251 buf[1] = '\n'; 323 buf[1] = '\n';
252 buf[2] = '\0'; 324 buf[2] = '\0';
253 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 325 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -265,7 +337,7 @@ static ssize_t sc_only_mode_read(struct file *file, char __user *user_buf,
265 struct hci_dev *hdev = file->private_data; 337 struct hci_dev *hdev = file->private_data;
266 char buf[3]; 338 char buf[3];
267 339
268 buf[0] = test_bit(HCI_SC_ONLY, &hdev->dev_flags) ? 'Y': 'N'; 340 buf[0] = hci_dev_test_flag(hdev, HCI_SC_ONLY) ? 'Y': 'N';
269 buf[1] = '\n'; 341 buf[1] = '\n';
270 buf[2] = '\0'; 342 buf[2] = '\0';
271 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 343 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -287,6 +359,8 @@ void hci_debugfs_create_common(struct hci_dev *hdev)
287 debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev); 359 debugfs_create_u16("hci_revision", 0444, hdev->debugfs, &hdev->hci_rev);
288 debugfs_create_u8("hardware_error", 0444, hdev->debugfs, 360 debugfs_create_u8("hardware_error", 0444, hdev->debugfs,
289 &hdev->hw_error_code); 361 &hdev->hw_error_code);
362 debugfs_create_file("device_id", 0444, hdev->debugfs, hdev,
363 &device_id_fops);
290 364
291 debugfs_create_file("device_list", 0444, hdev->debugfs, hdev, 365 debugfs_create_file("device_list", 0444, hdev->debugfs, hdev,
292 &device_list_fops); 366 &device_list_fops);
@@ -679,7 +753,7 @@ static ssize_t force_static_address_read(struct file *file,
679 struct hci_dev *hdev = file->private_data; 753 struct hci_dev *hdev = file->private_data;
680 char buf[3]; 754 char buf[3];
681 755
682 buf[0] = test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ? 'Y': 'N'; 756 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ? 'Y': 'N';
683 buf[1] = '\n'; 757 buf[1] = '\n';
684 buf[2] = '\0'; 758 buf[2] = '\0';
685 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 759 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -704,10 +778,10 @@ static ssize_t force_static_address_write(struct file *file,
704 if (strtobool(buf, &enable)) 778 if (strtobool(buf, &enable))
705 return -EINVAL; 779 return -EINVAL;
706 780
707 if (enable == test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags)) 781 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR))
708 return -EALREADY; 782 return -EALREADY;
709 783
710 change_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags); 784 hci_dev_change_flag(hdev, HCI_FORCE_STATIC_ADDR);
711 785
712 return count; 786 return count;
713} 787}
@@ -997,6 +1071,11 @@ static int adv_max_interval_get(void *data, u64 *val)
997DEFINE_SIMPLE_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get, 1071DEFINE_SIMPLE_ATTRIBUTE(adv_max_interval_fops, adv_max_interval_get,
998 adv_max_interval_set, "%llu\n"); 1072 adv_max_interval_set, "%llu\n");
999 1073
1074DEFINE_QUIRK_ATTRIBUTE(quirk_strict_duplicate_filter,
1075 HCI_QUIRK_STRICT_DUPLICATE_FILTER);
1076DEFINE_QUIRK_ATTRIBUTE(quirk_simultaneous_discovery,
1077 HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
1078
1000void hci_debugfs_create_le(struct hci_dev *hdev) 1079void hci_debugfs_create_le(struct hci_dev *hdev)
1001{ 1080{
1002 debugfs_create_file("identity", 0400, hdev->debugfs, hdev, 1081 debugfs_create_file("identity", 0400, hdev->debugfs, hdev,
@@ -1041,6 +1120,13 @@ void hci_debugfs_create_le(struct hci_dev *hdev)
1041 &adv_max_interval_fops); 1120 &adv_max_interval_fops);
1042 debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs, 1121 debugfs_create_u16("discov_interleaved_timeout", 0644, hdev->debugfs,
1043 &hdev->discov_interleaved_timeout); 1122 &hdev->discov_interleaved_timeout);
1123
1124 debugfs_create_file("quirk_strict_duplicate_filter", 0644,
1125 hdev->debugfs, hdev,
1126 &quirk_strict_duplicate_filter_fops);
1127 debugfs_create_file("quirk_simultaneous_discovery", 0644,
1128 hdev->debugfs, hdev,
1129 &quirk_simultaneous_discovery_fops);
1044} 1130}
1045 1131
1046void hci_debugfs_create_conn(struct hci_conn *conn) 1132void hci_debugfs_create_conn(struct hci_conn *conn)
diff --git a/net/bluetooth/hci_debugfs.h b/net/bluetooth/hci_debugfs.h
index fb68efe083c5..4444dc8cedc2 100644
--- a/net/bluetooth/hci_debugfs.h
+++ b/net/bluetooth/hci_debugfs.h
@@ -20,7 +20,29 @@
20 SOFTWARE IS DISCLAIMED. 20 SOFTWARE IS DISCLAIMED.
21*/ 21*/
22 22
23#if IS_ENABLED(CONFIG_BT_DEBUGFS)
24
23void hci_debugfs_create_common(struct hci_dev *hdev); 25void hci_debugfs_create_common(struct hci_dev *hdev);
24void hci_debugfs_create_bredr(struct hci_dev *hdev); 26void hci_debugfs_create_bredr(struct hci_dev *hdev);
25void hci_debugfs_create_le(struct hci_dev *hdev); 27void hci_debugfs_create_le(struct hci_dev *hdev);
26void hci_debugfs_create_conn(struct hci_conn *conn); 28void hci_debugfs_create_conn(struct hci_conn *conn);
29
30#else
31
32static inline void hci_debugfs_create_common(struct hci_dev *hdev)
33{
34}
35
36static inline void hci_debugfs_create_bredr(struct hci_dev *hdev)
37{
38}
39
40static inline void hci_debugfs_create_le(struct hci_dev *hdev)
41{
42}
43
44static inline void hci_debugfs_create_conn(struct hci_conn *conn)
45{
46}
47
48#endif
diff --git a/net/bluetooth/hci_event.c b/net/bluetooth/hci_event.c
index a3fb094822b6..7b61be73650f 100644
--- a/net/bluetooth/hci_event.c
+++ b/net/bluetooth/hci_event.c
@@ -70,7 +70,7 @@ static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
70 if (status) 70 if (status)
71 return; 71 return;
72 72
73 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags); 73 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
74} 74}
75 75
76static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb) 76static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
@@ -82,7 +82,7 @@ static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
82 if (status) 82 if (status)
83 return; 83 return;
84 84
85 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags); 85 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
86 86
87 hci_conn_check_pending(hdev); 87 hci_conn_check_pending(hdev);
88} 88}
@@ -198,7 +198,7 @@ static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
198 return; 198 return;
199 199
200 /* Reset all non-persistent flags */ 200 /* Reset all non-persistent flags */
201 hdev->dev_flags &= ~HCI_PERSISTENT_MASK; 201 hci_dev_clear_volatile_flags(hdev);
202 202
203 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 203 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
204 204
@@ -265,7 +265,7 @@ static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
265 265
266 hci_dev_lock(hdev); 266 hci_dev_lock(hdev);
267 267
268 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 268 if (hci_dev_test_flag(hdev, HCI_MGMT))
269 mgmt_set_local_name_complete(hdev, sent, status); 269 mgmt_set_local_name_complete(hdev, sent, status);
270 else if (!status) 270 else if (!status)
271 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH); 271 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
@@ -282,8 +282,8 @@ static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
282 if (rp->status) 282 if (rp->status)
283 return; 283 return;
284 284
285 if (test_bit(HCI_SETUP, &hdev->dev_flags) || 285 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
286 test_bit(HCI_CONFIG, &hdev->dev_flags)) 286 hci_dev_test_flag(hdev, HCI_CONFIG))
287 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH); 287 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
288} 288}
289 289
@@ -309,7 +309,7 @@ static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
309 clear_bit(HCI_AUTH, &hdev->flags); 309 clear_bit(HCI_AUTH, &hdev->flags);
310 } 310 }
311 311
312 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 312 if (hci_dev_test_flag(hdev, HCI_MGMT))
313 mgmt_auth_enable_complete(hdev, status); 313 mgmt_auth_enable_complete(hdev, status);
314 314
315 hci_dev_unlock(hdev); 315 hci_dev_unlock(hdev);
@@ -404,7 +404,7 @@ static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
404 if (status == 0) 404 if (status == 0)
405 memcpy(hdev->dev_class, sent, 3); 405 memcpy(hdev->dev_class, sent, 3);
406 406
407 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 407 if (hci_dev_test_flag(hdev, HCI_MGMT))
408 mgmt_set_class_of_dev_complete(hdev, sent, status); 408 mgmt_set_class_of_dev_complete(hdev, sent, status);
409 409
410 hci_dev_unlock(hdev); 410 hci_dev_unlock(hdev);
@@ -497,13 +497,13 @@ static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
497 hdev->features[1][0] &= ~LMP_HOST_SSP; 497 hdev->features[1][0] &= ~LMP_HOST_SSP;
498 } 498 }
499 499
500 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 500 if (hci_dev_test_flag(hdev, HCI_MGMT))
501 mgmt_ssp_enable_complete(hdev, sent->mode, status); 501 mgmt_ssp_enable_complete(hdev, sent->mode, status);
502 else if (!status) { 502 else if (!status) {
503 if (sent->mode) 503 if (sent->mode)
504 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 504 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
505 else 505 else
506 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 506 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
507 } 507 }
508 508
509 hci_dev_unlock(hdev); 509 hci_dev_unlock(hdev);
@@ -529,11 +529,11 @@ static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
529 hdev->features[1][0] &= ~LMP_HOST_SC; 529 hdev->features[1][0] &= ~LMP_HOST_SC;
530 } 530 }
531 531
532 if (!test_bit(HCI_MGMT, &hdev->dev_flags) && !status) { 532 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
533 if (sent->support) 533 if (sent->support)
534 set_bit(HCI_SC_ENABLED, &hdev->dev_flags); 534 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
535 else 535 else
536 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); 536 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
537 } 537 }
538 538
539 hci_dev_unlock(hdev); 539 hci_dev_unlock(hdev);
@@ -548,8 +548,8 @@ static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
548 if (rp->status) 548 if (rp->status)
549 return; 549 return;
550 550
551 if (test_bit(HCI_SETUP, &hdev->dev_flags) || 551 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
552 test_bit(HCI_CONFIG, &hdev->dev_flags)) { 552 hci_dev_test_flag(hdev, HCI_CONFIG)) {
553 hdev->hci_ver = rp->hci_ver; 553 hdev->hci_ver = rp->hci_ver;
554 hdev->hci_rev = __le16_to_cpu(rp->hci_rev); 554 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
555 hdev->lmp_ver = rp->lmp_ver; 555 hdev->lmp_ver = rp->lmp_ver;
@@ -568,8 +568,8 @@ static void hci_cc_read_local_commands(struct hci_dev *hdev,
568 if (rp->status) 568 if (rp->status)
569 return; 569 return;
570 570
571 if (test_bit(HCI_SETUP, &hdev->dev_flags) || 571 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
572 test_bit(HCI_CONFIG, &hdev->dev_flags)) 572 hci_dev_test_flag(hdev, HCI_CONFIG))
573 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands)); 573 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
574} 574}
575 575
@@ -691,7 +691,7 @@ static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
691 if (test_bit(HCI_INIT, &hdev->flags)) 691 if (test_bit(HCI_INIT, &hdev->flags))
692 bacpy(&hdev->bdaddr, &rp->bdaddr); 692 bacpy(&hdev->bdaddr, &rp->bdaddr);
693 693
694 if (test_bit(HCI_SETUP, &hdev->dev_flags)) 694 if (hci_dev_test_flag(hdev, HCI_SETUP))
695 bacpy(&hdev->setup_addr, &rp->bdaddr); 695 bacpy(&hdev->setup_addr, &rp->bdaddr);
696} 696}
697 697
@@ -900,7 +900,7 @@ static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
900 900
901 hci_dev_lock(hdev); 901 hci_dev_lock(hdev);
902 902
903 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 903 if (hci_dev_test_flag(hdev, HCI_MGMT))
904 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status); 904 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
905 905
906 if (rp->status) 906 if (rp->status)
@@ -926,7 +926,7 @@ static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
926 926
927 hci_dev_lock(hdev); 927 hci_dev_lock(hdev);
928 928
929 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 929 if (hci_dev_test_flag(hdev, HCI_MGMT))
930 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr, 930 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
931 rp->status); 931 rp->status);
932 932
@@ -985,7 +985,7 @@ static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
985 985
986 hci_dev_lock(hdev); 986 hci_dev_lock(hdev);
987 987
988 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 988 if (hci_dev_test_flag(hdev, HCI_MGMT))
989 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0, 989 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
990 rp->status); 990 rp->status);
991 991
@@ -1001,7 +1001,7 @@ static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
1001 1001
1002 hci_dev_lock(hdev); 1002 hci_dev_lock(hdev);
1003 1003
1004 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 1004 if (hci_dev_test_flag(hdev, HCI_MGMT))
1005 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr, 1005 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
1006 ACL_LINK, 0, rp->status); 1006 ACL_LINK, 0, rp->status);
1007 1007
@@ -1016,7 +1016,7 @@ static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1016 1016
1017 hci_dev_lock(hdev); 1017 hci_dev_lock(hdev);
1018 1018
1019 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 1019 if (hci_dev_test_flag(hdev, HCI_MGMT))
1020 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 1020 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
1021 0, rp->status); 1021 0, rp->status);
1022 1022
@@ -1032,7 +1032,7 @@ static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
1032 1032
1033 hci_dev_lock(hdev); 1033 hci_dev_lock(hdev);
1034 1034
1035 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 1035 if (hci_dev_test_flag(hdev, HCI_MGMT))
1036 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr, 1036 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
1037 ACL_LINK, 0, rp->status); 1037 ACL_LINK, 0, rp->status);
1038 1038
@@ -1045,11 +1045,6 @@ static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1045 struct hci_rp_read_local_oob_data *rp = (void *) skb->data; 1045 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1046 1046
1047 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status); 1047 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1048
1049 hci_dev_lock(hdev);
1050 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->rand, NULL, NULL,
1051 rp->status);
1052 hci_dev_unlock(hdev);
1053} 1048}
1054 1049
1055static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev, 1050static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
@@ -1058,15 +1053,8 @@ static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1058 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data; 1053 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1059 1054
1060 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status); 1055 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1061
1062 hci_dev_lock(hdev);
1063 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->rand192,
1064 rp->hash256, rp->rand256,
1065 rp->status);
1066 hci_dev_unlock(hdev);
1067} 1056}
1068 1057
1069
1070static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb) 1058static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1071{ 1059{
1072 __u8 status = *((__u8 *) skb->data); 1060 __u8 status = *((__u8 *) skb->data);
@@ -1109,7 +1097,7 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1109 if (*sent) { 1097 if (*sent) {
1110 struct hci_conn *conn; 1098 struct hci_conn *conn;
1111 1099
1112 set_bit(HCI_LE_ADV, &hdev->dev_flags); 1100 hci_dev_set_flag(hdev, HCI_LE_ADV);
1113 1101
1114 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); 1102 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1115 if (conn) 1103 if (conn)
@@ -1117,7 +1105,7 @@ static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1117 &conn->le_conn_timeout, 1105 &conn->le_conn_timeout,
1118 conn->conn_timeout); 1106 conn->conn_timeout);
1119 } else { 1107 } else {
1120 clear_bit(HCI_LE_ADV, &hdev->dev_flags); 1108 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1121 } 1109 }
1122 1110
1123 hci_dev_unlock(hdev); 1111 hci_dev_unlock(hdev);
@@ -1192,7 +1180,7 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1192 1180
1193 switch (cp->enable) { 1181 switch (cp->enable) {
1194 case LE_SCAN_ENABLE: 1182 case LE_SCAN_ENABLE:
1195 set_bit(HCI_LE_SCAN, &hdev->dev_flags); 1183 hci_dev_set_flag(hdev, HCI_LE_SCAN);
1196 if (hdev->le_scan_type == LE_SCAN_ACTIVE) 1184 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1197 clear_pending_adv_report(hdev); 1185 clear_pending_adv_report(hdev);
1198 break; 1186 break;
@@ -1217,7 +1205,7 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1217 */ 1205 */
1218 cancel_delayed_work(&hdev->le_scan_disable); 1206 cancel_delayed_work(&hdev->le_scan_disable);
1219 1207
1220 clear_bit(HCI_LE_SCAN, &hdev->dev_flags); 1208 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
1221 1209
1222 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we 1210 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1223 * interrupted scanning due to a connect request. Mark 1211 * interrupted scanning due to a connect request. Mark
@@ -1226,10 +1214,9 @@ static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1226 * been disabled because of active scanning, so 1214 * been disabled because of active scanning, so
1227 * re-enable it again if necessary. 1215 * re-enable it again if necessary.
1228 */ 1216 */
1229 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED, 1217 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
1230 &hdev->dev_flags))
1231 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 1218 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1232 else if (!test_bit(HCI_LE_ADV, &hdev->dev_flags) && 1219 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
1233 hdev->discovery.state == DISCOVERY_FINDING) 1220 hdev->discovery.state == DISCOVERY_FINDING)
1234 mgmt_reenable_advertising(hdev); 1221 mgmt_reenable_advertising(hdev);
1235 1222
@@ -1388,11 +1375,11 @@ static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1388 1375
1389 if (sent->le) { 1376 if (sent->le) {
1390 hdev->features[1][0] |= LMP_HOST_LE; 1377 hdev->features[1][0] |= LMP_HOST_LE;
1391 set_bit(HCI_LE_ENABLED, &hdev->dev_flags); 1378 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
1392 } else { 1379 } else {
1393 hdev->features[1][0] &= ~LMP_HOST_LE; 1380 hdev->features[1][0] &= ~LMP_HOST_LE;
1394 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags); 1381 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
1395 clear_bit(HCI_ADVERTISING, &hdev->dev_flags); 1382 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
1396 } 1383 }
1397 1384
1398 if (sent->simul) 1385 if (sent->simul)
@@ -1537,7 +1524,7 @@ static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
1537 if (conn && conn->state == BT_CONNECT) { 1524 if (conn && conn->state == BT_CONNECT) {
1538 if (status != 0x0c || conn->attempt > 2) { 1525 if (status != 0x0c || conn->attempt > 2) {
1539 conn->state = BT_CLOSED; 1526 conn->state = BT_CLOSED;
1540 hci_proto_connect_cfm(conn, status); 1527 hci_connect_cfm(conn, status);
1541 hci_conn_del(conn); 1528 hci_conn_del(conn);
1542 } else 1529 } else
1543 conn->state = BT_CONNECT2; 1530 conn->state = BT_CONNECT2;
@@ -1581,7 +1568,7 @@ static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
1581 if (sco) { 1568 if (sco) {
1582 sco->state = BT_CLOSED; 1569 sco->state = BT_CLOSED;
1583 1570
1584 hci_proto_connect_cfm(sco, status); 1571 hci_connect_cfm(sco, status);
1585 hci_conn_del(sco); 1572 hci_conn_del(sco);
1586 } 1573 }
1587 } 1574 }
@@ -1608,7 +1595,7 @@ static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1608 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 1595 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1609 if (conn) { 1596 if (conn) {
1610 if (conn->state == BT_CONFIG) { 1597 if (conn->state == BT_CONFIG) {
1611 hci_proto_connect_cfm(conn, status); 1598 hci_connect_cfm(conn, status);
1612 hci_conn_drop(conn); 1599 hci_conn_drop(conn);
1613 } 1600 }
1614 } 1601 }
@@ -1635,7 +1622,7 @@ static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1635 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 1622 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1636 if (conn) { 1623 if (conn) {
1637 if (conn->state == BT_CONFIG) { 1624 if (conn->state == BT_CONFIG) {
1638 hci_proto_connect_cfm(conn, status); 1625 hci_connect_cfm(conn, status);
1639 hci_conn_drop(conn); 1626 hci_conn_drop(conn);
1640 } 1627 }
1641 } 1628 }
@@ -1769,7 +1756,7 @@ static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1769 1756
1770 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr); 1757 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1771 1758
1772 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 1759 if (hci_dev_test_flag(hdev, HCI_MGMT))
1773 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0); 1760 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1774 1761
1775 if (!conn) 1762 if (!conn)
@@ -1811,7 +1798,7 @@ static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1811 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 1798 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1812 if (conn) { 1799 if (conn) {
1813 if (conn->state == BT_CONFIG) { 1800 if (conn->state == BT_CONFIG) {
1814 hci_proto_connect_cfm(conn, status); 1801 hci_connect_cfm(conn, status);
1815 hci_conn_drop(conn); 1802 hci_conn_drop(conn);
1816 } 1803 }
1817 } 1804 }
@@ -1838,7 +1825,7 @@ static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1838 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle)); 1825 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1839 if (conn) { 1826 if (conn) {
1840 if (conn->state == BT_CONFIG) { 1827 if (conn->state == BT_CONFIG) {
1841 hci_proto_connect_cfm(conn, status); 1828 hci_connect_cfm(conn, status);
1842 hci_conn_drop(conn); 1829 hci_conn_drop(conn);
1843 } 1830 }
1844 } 1831 }
@@ -1873,7 +1860,7 @@ static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1873 if (sco) { 1860 if (sco) {
1874 sco->state = BT_CLOSED; 1861 sco->state = BT_CLOSED;
1875 1862
1876 hci_proto_connect_cfm(sco, status); 1863 hci_connect_cfm(sco, status);
1877 hci_conn_del(sco); 1864 hci_conn_del(sco);
1878 } 1865 }
1879 } 1866 }
@@ -2049,6 +2036,33 @@ unlock:
2049 hci_dev_unlock(hdev); 2036 hci_dev_unlock(hdev);
2050} 2037}
2051 2038
2039static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2040{
2041 struct hci_cp_le_read_remote_features *cp;
2042 struct hci_conn *conn;
2043
2044 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2045
2046 if (!status)
2047 return;
2048
2049 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2050 if (!cp)
2051 return;
2052
2053 hci_dev_lock(hdev);
2054
2055 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2056 if (conn) {
2057 if (conn->state == BT_CONFIG) {
2058 hci_connect_cfm(conn, status);
2059 hci_conn_drop(conn);
2060 }
2061 }
2062
2063 hci_dev_unlock(hdev);
2064}
2065
2052static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status) 2066static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2053{ 2067{
2054 struct hci_cp_le_start_enc *cp; 2068 struct hci_cp_le_start_enc *cp;
@@ -2118,7 +2132,7 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2118 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */ 2132 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
2119 wake_up_bit(&hdev->flags, HCI_INQUIRY); 2133 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2120 2134
2121 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 2135 if (!hci_dev_test_flag(hdev, HCI_MGMT))
2122 return; 2136 return;
2123 2137
2124 hci_dev_lock(hdev); 2138 hci_dev_lock(hdev);
@@ -2127,7 +2141,16 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2127 goto unlock; 2141 goto unlock;
2128 2142
2129 if (list_empty(&discov->resolve)) { 2143 if (list_empty(&discov->resolve)) {
2130 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 2144 /* When BR/EDR inquiry is active and no LE scanning is in
2145 * progress, then change discovery state to indicate completion.
2146 *
2147 * When running LE scanning and BR/EDR inquiry simultaneously
2148 * and the LE scan already finished, then change the discovery
2149 * state to indicate completion.
2150 */
2151 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2152 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2153 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2131 goto unlock; 2154 goto unlock;
2132 } 2155 }
2133 2156
@@ -2136,7 +2159,16 @@ static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2136 e->name_state = NAME_PENDING; 2159 e->name_state = NAME_PENDING;
2137 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING); 2160 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2138 } else { 2161 } else {
2139 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 2162 /* When BR/EDR inquiry is active and no LE scanning is in
2163 * progress, then change discovery state to indicate completion.
2164 *
2165 * When running LE scanning and BR/EDR inquiry simultaneously
2166 * and the LE scan already finished, then change the discovery
2167 * state to indicate completion.
2168 */
2169 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2170 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2171 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2140 } 2172 }
2141 2173
2142unlock: 2174unlock:
@@ -2154,7 +2186,7 @@ static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
2154 if (!num_rsp) 2186 if (!num_rsp)
2155 return; 2187 return;
2156 2188
2157 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) 2189 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
2158 return; 2190 return;
2159 2191
2160 hci_dev_lock(hdev); 2192 hci_dev_lock(hdev);
@@ -2255,10 +2287,10 @@ static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2255 hci_sco_setup(conn, ev->status); 2287 hci_sco_setup(conn, ev->status);
2256 2288
2257 if (ev->status) { 2289 if (ev->status) {
2258 hci_proto_connect_cfm(conn, ev->status); 2290 hci_connect_cfm(conn, ev->status);
2259 hci_conn_del(conn); 2291 hci_conn_del(conn);
2260 } else if (ev->link_type != ACL_LINK) 2292 } else if (ev->link_type != ACL_LINK)
2261 hci_proto_connect_cfm(conn, ev->status); 2293 hci_connect_cfm(conn, ev->status);
2262 2294
2263unlock: 2295unlock:
2264 hci_dev_unlock(hdev); 2296 hci_dev_unlock(hdev);
@@ -2304,8 +2336,8 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2304 * connection. These features are only touched through mgmt so 2336 * connection. These features are only touched through mgmt so
2305 * only do the checks if HCI_MGMT is set. 2337 * only do the checks if HCI_MGMT is set.
2306 */ 2338 */
2307 if (test_bit(HCI_MGMT, &hdev->dev_flags) && 2339 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2308 !test_bit(HCI_CONNECTABLE, &hdev->dev_flags) && 2340 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
2309 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr, 2341 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2310 BDADDR_BREDR)) { 2342 BDADDR_BREDR)) {
2311 hci_reject_conn(hdev, &ev->bdaddr); 2343 hci_reject_conn(hdev, &ev->bdaddr);
@@ -2366,7 +2398,7 @@ static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
2366 &cp); 2398 &cp);
2367 } else { 2399 } else {
2368 conn->state = BT_CONNECT2; 2400 conn->state = BT_CONNECT2;
2369 hci_proto_connect_cfm(conn, 0); 2401 hci_connect_cfm(conn, 0);
2370 } 2402 }
2371} 2403}
2372 2404
@@ -2444,7 +2476,7 @@ static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2444 2476
2445 type = conn->type; 2477 type = conn->type;
2446 2478
2447 hci_proto_disconn_cfm(conn, ev->reason); 2479 hci_disconn_cfm(conn, ev->reason);
2448 hci_conn_del(conn); 2480 hci_conn_del(conn);
2449 2481
2450 /* Re-enable advertising if necessary, since it might 2482 /* Re-enable advertising if necessary, since it might
@@ -2501,7 +2533,7 @@ static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
2501 &cp); 2533 &cp);
2502 } else { 2534 } else {
2503 conn->state = BT_CONNECTED; 2535 conn->state = BT_CONNECTED;
2504 hci_proto_connect_cfm(conn, ev->status); 2536 hci_connect_cfm(conn, ev->status);
2505 hci_conn_drop(conn); 2537 hci_conn_drop(conn);
2506 } 2538 }
2507 } else { 2539 } else {
@@ -2542,7 +2574,7 @@ static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
2542 2574
2543 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr); 2575 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2544 2576
2545 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 2577 if (!hci_dev_test_flag(hdev, HCI_MGMT))
2546 goto check_auth; 2578 goto check_auth;
2547 2579
2548 if (ev->status == 0) 2580 if (ev->status == 0)
@@ -2608,7 +2640,7 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2608 * whenever the encryption procedure fails. 2640 * whenever the encryption procedure fails.
2609 */ 2641 */
2610 if (ev->status && conn->type == LE_LINK) 2642 if (ev->status && conn->type == LE_LINK)
2611 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 2643 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
2612 2644
2613 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags); 2645 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2614 2646
@@ -2626,15 +2658,15 @@ static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
2626 * connections that are not encrypted with AES-CCM 2658 * connections that are not encrypted with AES-CCM
2627 * using a P-256 authenticated combination key. 2659 * using a P-256 authenticated combination key.
2628 */ 2660 */
2629 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && 2661 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) &&
2630 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) || 2662 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2631 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) { 2663 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2632 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE); 2664 hci_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2633 hci_conn_drop(conn); 2665 hci_conn_drop(conn);
2634 goto unlock; 2666 goto unlock;
2635 } 2667 }
2636 2668
2637 hci_proto_connect_cfm(conn, ev->status); 2669 hci_connect_cfm(conn, ev->status);
2638 hci_conn_drop(conn); 2670 hci_conn_drop(conn);
2639 } else 2671 } else
2640 hci_encrypt_cfm(conn, ev->status, ev->encrypt); 2672 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
@@ -2707,7 +2739,7 @@ static void hci_remote_features_evt(struct hci_dev *hdev,
2707 2739
2708 if (!hci_outgoing_auth_needed(hdev, conn)) { 2740 if (!hci_outgoing_auth_needed(hdev, conn)) {
2709 conn->state = BT_CONNECTED; 2741 conn->state = BT_CONNECTED;
2710 hci_proto_connect_cfm(conn, ev->status); 2742 hci_connect_cfm(conn, ev->status);
2711 hci_conn_drop(conn); 2743 hci_conn_drop(conn);
2712 } 2744 }
2713 2745
@@ -2715,17 +2747,19 @@ unlock:
2715 hci_dev_unlock(hdev); 2747 hci_dev_unlock(hdev);
2716} 2748}
2717 2749
2718static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb) 2750static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
2751 u16 *opcode, u8 *status,
2752 hci_req_complete_t *req_complete,
2753 hci_req_complete_skb_t *req_complete_skb)
2719{ 2754{
2720 struct hci_ev_cmd_complete *ev = (void *) skb->data; 2755 struct hci_ev_cmd_complete *ev = (void *) skb->data;
2721 u8 status = skb->data[sizeof(*ev)];
2722 __u16 opcode;
2723 2756
2724 skb_pull(skb, sizeof(*ev)); 2757 *opcode = __le16_to_cpu(ev->opcode);
2758 *status = skb->data[sizeof(*ev)];
2725 2759
2726 opcode = __le16_to_cpu(ev->opcode); 2760 skb_pull(skb, sizeof(*ev));
2727 2761
2728 switch (opcode) { 2762 switch (*opcode) {
2729 case HCI_OP_INQUIRY_CANCEL: 2763 case HCI_OP_INQUIRY_CANCEL:
2730 hci_cc_inquiry_cancel(hdev, skb); 2764 hci_cc_inquiry_cancel(hdev, skb);
2731 break; 2765 break;
@@ -3003,32 +3037,36 @@ static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
3003 break; 3037 break;
3004 3038
3005 default: 3039 default:
3006 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); 3040 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
3007 break; 3041 break;
3008 } 3042 }
3009 3043
3010 if (opcode != HCI_OP_NOP) 3044 if (*opcode != HCI_OP_NOP)
3011 cancel_delayed_work(&hdev->cmd_timer); 3045 cancel_delayed_work(&hdev->cmd_timer);
3012 3046
3013 hci_req_cmd_complete(hdev, opcode, status); 3047 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3014
3015 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
3016 atomic_set(&hdev->cmd_cnt, 1); 3048 atomic_set(&hdev->cmd_cnt, 1);
3017 if (!skb_queue_empty(&hdev->cmd_q)) 3049
3018 queue_work(hdev->workqueue, &hdev->cmd_work); 3050 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3019 } 3051 req_complete_skb);
3052
3053 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3054 queue_work(hdev->workqueue, &hdev->cmd_work);
3020} 3055}
3021 3056
3022static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb) 3057static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3058 u16 *opcode, u8 *status,
3059 hci_req_complete_t *req_complete,
3060 hci_req_complete_skb_t *req_complete_skb)
3023{ 3061{
3024 struct hci_ev_cmd_status *ev = (void *) skb->data; 3062 struct hci_ev_cmd_status *ev = (void *) skb->data;
3025 __u16 opcode;
3026 3063
3027 skb_pull(skb, sizeof(*ev)); 3064 skb_pull(skb, sizeof(*ev));
3028 3065
3029 opcode = __le16_to_cpu(ev->opcode); 3066 *opcode = __le16_to_cpu(ev->opcode);
3067 *status = ev->status;
3030 3068
3031 switch (opcode) { 3069 switch (*opcode) {
3032 case HCI_OP_INQUIRY: 3070 case HCI_OP_INQUIRY:
3033 hci_cs_inquiry(hdev, ev->status); 3071 hci_cs_inquiry(hdev, ev->status);
3034 break; 3072 break;
@@ -3093,27 +3131,38 @@ static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
3093 hci_cs_le_create_conn(hdev, ev->status); 3131 hci_cs_le_create_conn(hdev, ev->status);
3094 break; 3132 break;
3095 3133
3134 case HCI_OP_LE_READ_REMOTE_FEATURES:
3135 hci_cs_le_read_remote_features(hdev, ev->status);
3136 break;
3137
3096 case HCI_OP_LE_START_ENC: 3138 case HCI_OP_LE_START_ENC:
3097 hci_cs_le_start_enc(hdev, ev->status); 3139 hci_cs_le_start_enc(hdev, ev->status);
3098 break; 3140 break;
3099 3141
3100 default: 3142 default:
3101 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode); 3143 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
3102 break; 3144 break;
3103 } 3145 }
3104 3146
3105 if (opcode != HCI_OP_NOP) 3147 if (*opcode != HCI_OP_NOP)
3106 cancel_delayed_work(&hdev->cmd_timer); 3148 cancel_delayed_work(&hdev->cmd_timer);
3107 3149
3150 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3151 atomic_set(&hdev->cmd_cnt, 1);
3152
3153 /* Indicate request completion if the command failed. Also, if
3154 * we're not waiting for a special event and we get a success
3155 * command status we should try to flag the request as completed
3156 * (since for this kind of commands there will not be a command
3157 * complete event).
3158 */
3108 if (ev->status || 3159 if (ev->status ||
3109 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event)) 3160 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
3110 hci_req_cmd_complete(hdev, opcode, ev->status); 3161 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3162 req_complete_skb);
3111 3163
3112 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) { 3164 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3113 atomic_set(&hdev->cmd_cnt, 1); 3165 queue_work(hdev->workqueue, &hdev->cmd_work);
3114 if (!skb_queue_empty(&hdev->cmd_q))
3115 queue_work(hdev->workqueue, &hdev->cmd_work);
3116 }
3117} 3166}
3118 3167
3119static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb) 3168static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
@@ -3331,11 +3380,11 @@ static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3331 hci_conn_drop(conn); 3380 hci_conn_drop(conn);
3332 } 3381 }
3333 3382
3334 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && 3383 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
3335 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) { 3384 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
3336 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY, 3385 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
3337 sizeof(ev->bdaddr), &ev->bdaddr); 3386 sizeof(ev->bdaddr), &ev->bdaddr);
3338 } else if (test_bit(HCI_MGMT, &hdev->dev_flags)) { 3387 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
3339 u8 secure; 3388 u8 secure;
3340 3389
3341 if (conn->pending_sec_level == BT_SECURITY_HIGH) 3390 if (conn->pending_sec_level == BT_SECURITY_HIGH)
@@ -3391,7 +3440,7 @@ static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3391 3440
3392 BT_DBG("%s", hdev->name); 3441 BT_DBG("%s", hdev->name);
3393 3442
3394 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 3443 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3395 return; 3444 return;
3396 3445
3397 hci_dev_lock(hdev); 3446 hci_dev_lock(hdev);
@@ -3465,7 +3514,7 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3465 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags); 3514 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
3466 conn_set_key(conn, ev->key_type, conn->pin_length); 3515 conn_set_key(conn, ev->key_type, conn->pin_length);
3467 3516
3468 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 3517 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3469 goto unlock; 3518 goto unlock;
3470 3519
3471 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key, 3520 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
@@ -3487,7 +3536,7 @@ static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3487 * store_hint being 0). 3536 * store_hint being 0).
3488 */ 3537 */
3489 if (key->type == HCI_LK_DEBUG_COMBINATION && 3538 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3490 !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) { 3539 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
3491 list_del_rcu(&key->list); 3540 list_del_rcu(&key->list);
3492 kfree_rcu(key, rcu); 3541 kfree_rcu(key, rcu);
3493 goto unlock; 3542 goto unlock;
@@ -3570,7 +3619,7 @@ static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3570 if (!num_rsp) 3619 if (!num_rsp)
3571 return; 3620 return;
3572 3621
3573 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) 3622 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3574 return; 3623 return;
3575 3624
3576 hci_dev_lock(hdev); 3625 hci_dev_lock(hdev);
@@ -3679,7 +3728,7 @@ static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3679 3728
3680 if (!hci_outgoing_auth_needed(hdev, conn)) { 3729 if (!hci_outgoing_auth_needed(hdev, conn)) {
3681 conn->state = BT_CONNECTED; 3730 conn->state = BT_CONNECTED;
3682 hci_proto_connect_cfm(conn, ev->status); 3731 hci_connect_cfm(conn, ev->status);
3683 hci_conn_drop(conn); 3732 hci_conn_drop(conn);
3684 } 3733 }
3685 3734
@@ -3738,7 +3787,7 @@ static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3738 break; 3787 break;
3739 } 3788 }
3740 3789
3741 hci_proto_connect_cfm(conn, ev->status); 3790 hci_connect_cfm(conn, ev->status);
3742 if (ev->status) 3791 if (ev->status)
3743 hci_conn_del(conn); 3792 hci_conn_del(conn);
3744 3793
@@ -3776,7 +3825,7 @@ static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3776 if (!num_rsp) 3825 if (!num_rsp)
3777 return; 3826 return;
3778 3827
3779 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) 3828 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
3780 return; 3829 return;
3781 3830
3782 hci_dev_lock(hdev); 3831 hci_dev_lock(hdev);
@@ -3794,7 +3843,7 @@ static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3794 data.rssi = info->rssi; 3843 data.rssi = info->rssi;
3795 data.ssp_mode = 0x01; 3844 data.ssp_mode = 0x01;
3796 3845
3797 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 3846 if (hci_dev_test_flag(hdev, HCI_MGMT))
3798 name_known = eir_has_data_type(info->data, 3847 name_known = eir_has_data_type(info->data,
3799 sizeof(info->data), 3848 sizeof(info->data),
3800 EIR_NAME_COMPLETE); 3849 EIR_NAME_COMPLETE);
@@ -3849,7 +3898,7 @@ static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3849 if (!ev->status) 3898 if (!ev->status)
3850 conn->state = BT_CONNECTED; 3899 conn->state = BT_CONNECTED;
3851 3900
3852 hci_proto_connect_cfm(conn, ev->status); 3901 hci_connect_cfm(conn, ev->status);
3853 hci_conn_drop(conn); 3902 hci_conn_drop(conn);
3854 } else { 3903 } else {
3855 hci_auth_cfm(conn, ev->status); 3904 hci_auth_cfm(conn, ev->status);
@@ -3890,41 +3939,37 @@ static u8 bredr_oob_data_present(struct hci_conn *conn)
3890 if (!data) 3939 if (!data)
3891 return 0x00; 3940 return 0x00;
3892 3941
3893 if (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)) { 3942 if (bredr_sc_enabled(hdev)) {
3894 if (bredr_sc_enabled(hdev)) { 3943 /* When Secure Connections is enabled, then just
3895 /* When Secure Connections is enabled, then just 3944 * return the present value stored with the OOB
3896 * return the present value stored with the OOB 3945 * data. The stored value contains the right present
3897 * data. The stored value contains the right present 3946 * information. However it can only be trusted when
3898 * information. However it can only be trusted when 3947 * not in Secure Connection Only mode.
3899 * not in Secure Connection Only mode. 3948 */
3900 */ 3949 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
3901 if (!test_bit(HCI_SC_ONLY, &hdev->dev_flags)) 3950 return data->present;
3902 return data->present;
3903
3904 /* When Secure Connections Only mode is enabled, then
3905 * the P-256 values are required. If they are not
3906 * available, then do not declare that OOB data is
3907 * present.
3908 */
3909 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
3910 !memcmp(data->hash256, ZERO_KEY, 16))
3911 return 0x00;
3912
3913 return 0x02;
3914 }
3915 3951
3916 /* When Secure Connections is not enabled or actually 3952 /* When Secure Connections Only mode is enabled, then
3917 * not supported by the hardware, then check that if 3953 * the P-256 values are required. If they are not
3918 * P-192 data values are present. 3954 * available, then do not declare that OOB data is
3955 * present.
3919 */ 3956 */
3920 if (!memcmp(data->rand192, ZERO_KEY, 16) || 3957 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
3921 !memcmp(data->hash192, ZERO_KEY, 16)) 3958 !memcmp(data->hash256, ZERO_KEY, 16))
3922 return 0x00; 3959 return 0x00;
3923 3960
3924 return 0x01; 3961 return 0x02;
3925 } 3962 }
3926 3963
3927 return 0x00; 3964 /* When Secure Connections is not enabled or actually
3965 * not supported by the hardware, then check that if
3966 * P-192 data values are present.
3967 */
3968 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
3969 !memcmp(data->hash192, ZERO_KEY, 16))
3970 return 0x00;
3971
3972 return 0x01;
3928} 3973}
3929 3974
3930static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 3975static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
@@ -3942,13 +3987,13 @@ static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3942 3987
3943 hci_conn_hold(conn); 3988 hci_conn_hold(conn);
3944 3989
3945 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 3990 if (!hci_dev_test_flag(hdev, HCI_MGMT))
3946 goto unlock; 3991 goto unlock;
3947 3992
3948 /* Allow pairing if we're pairable, the initiators of the 3993 /* Allow pairing if we're pairable, the initiators of the
3949 * pairing or if the remote is not requesting bonding. 3994 * pairing or if the remote is not requesting bonding.
3950 */ 3995 */
3951 if (test_bit(HCI_BONDABLE, &hdev->dev_flags) || 3996 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
3952 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) || 3997 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
3953 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) { 3998 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
3954 struct hci_cp_io_capability_reply cp; 3999 struct hci_cp_io_capability_reply cp;
@@ -3974,7 +4019,7 @@ static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
3974 /* If we're not bondable, force one of the non-bondable 4019 /* If we're not bondable, force one of the non-bondable
3975 * authentication requirement values. 4020 * authentication requirement values.
3976 */ 4021 */
3977 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags)) 4022 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
3978 conn->auth_type &= HCI_AT_NO_BONDING_MITM; 4023 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
3979 4024
3980 cp.authentication = conn->auth_type; 4025 cp.authentication = conn->auth_type;
@@ -4011,8 +4056,6 @@ static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
4011 4056
4012 conn->remote_cap = ev->capability; 4057 conn->remote_cap = ev->capability;
4013 conn->remote_auth = ev->authentication; 4058 conn->remote_auth = ev->authentication;
4014 if (ev->oob_data)
4015 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
4016 4059
4017unlock: 4060unlock:
4018 hci_dev_unlock(hdev); 4061 hci_dev_unlock(hdev);
@@ -4029,7 +4072,7 @@ static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4029 4072
4030 hci_dev_lock(hdev); 4073 hci_dev_lock(hdev);
4031 4074
4032 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 4075 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4033 goto unlock; 4076 goto unlock;
4034 4077
4035 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr); 4078 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
@@ -4100,7 +4143,7 @@ static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4100 4143
4101 BT_DBG("%s", hdev->name); 4144 BT_DBG("%s", hdev->name);
4102 4145
4103 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 4146 if (hci_dev_test_flag(hdev, HCI_MGMT))
4104 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0); 4147 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
4105} 4148}
4106 4149
@@ -4119,7 +4162,7 @@ static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4119 conn->passkey_notify = __le32_to_cpu(ev->passkey); 4162 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4120 conn->passkey_entered = 0; 4163 conn->passkey_entered = 0;
4121 4164
4122 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 4165 if (hci_dev_test_flag(hdev, HCI_MGMT))
4123 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type, 4166 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4124 conn->dst_type, conn->passkey_notify, 4167 conn->dst_type, conn->passkey_notify,
4125 conn->passkey_entered); 4168 conn->passkey_entered);
@@ -4157,7 +4200,7 @@ static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4157 return; 4200 return;
4158 } 4201 }
4159 4202
4160 if (test_bit(HCI_MGMT, &hdev->dev_flags)) 4203 if (hci_dev_test_flag(hdev, HCI_MGMT))
4161 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type, 4204 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4162 conn->dst_type, conn->passkey_notify, 4205 conn->dst_type, conn->passkey_notify,
4163 conn->passkey_entered); 4206 conn->passkey_entered);
@@ -4226,7 +4269,7 @@ static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4226 4269
4227 hci_dev_lock(hdev); 4270 hci_dev_lock(hdev);
4228 4271
4229 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 4272 if (!hci_dev_test_flag(hdev, HCI_MGMT))
4230 goto unlock; 4273 goto unlock;
4231 4274
4232 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR); 4275 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
@@ -4243,7 +4286,7 @@ static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4243 struct hci_cp_remote_oob_ext_data_reply cp; 4286 struct hci_cp_remote_oob_ext_data_reply cp;
4244 4287
4245 bacpy(&cp.bdaddr, &ev->bdaddr); 4288 bacpy(&cp.bdaddr, &ev->bdaddr);
4246 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { 4289 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
4247 memset(cp.hash192, 0, sizeof(cp.hash192)); 4290 memset(cp.hash192, 0, sizeof(cp.hash192));
4248 memset(cp.rand192, 0, sizeof(cp.rand192)); 4291 memset(cp.rand192, 0, sizeof(cp.rand192));
4249 } else { 4292 } else {
@@ -4409,7 +4452,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4409 /* All controllers implicitly stop advertising in the event of a 4452 /* All controllers implicitly stop advertising in the event of a
4410 * connection, so ensure that the state bit is cleared. 4453 * connection, so ensure that the state bit is cleared.
4411 */ 4454 */
4412 clear_bit(HCI_LE_ADV, &hdev->dev_flags); 4455 hci_dev_clear_flag(hdev, HCI_LE_ADV);
4413 4456
4414 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT); 4457 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
4415 if (!conn) { 4458 if (!conn) {
@@ -4432,7 +4475,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4432 if (conn->out) { 4475 if (conn->out) {
4433 conn->resp_addr_type = ev->bdaddr_type; 4476 conn->resp_addr_type = ev->bdaddr_type;
4434 bacpy(&conn->resp_addr, &ev->bdaddr); 4477 bacpy(&conn->resp_addr, &ev->bdaddr);
4435 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) { 4478 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
4436 conn->init_addr_type = ADDR_LE_DEV_RANDOM; 4479 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4437 bacpy(&conn->init_addr, &hdev->rpa); 4480 bacpy(&conn->init_addr, &hdev->rpa);
4438 } else { 4481 } else {
@@ -4503,7 +4546,7 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4503 4546
4504 conn->sec_level = BT_SECURITY_LOW; 4547 conn->sec_level = BT_SECURITY_LOW;
4505 conn->handle = __le16_to_cpu(ev->handle); 4548 conn->handle = __le16_to_cpu(ev->handle);
4506 conn->state = BT_CONNECTED; 4549 conn->state = BT_CONFIG;
4507 4550
4508 conn->le_conn_interval = le16_to_cpu(ev->interval); 4551 conn->le_conn_interval = le16_to_cpu(ev->interval);
4509 conn->le_conn_latency = le16_to_cpu(ev->latency); 4552 conn->le_conn_latency = le16_to_cpu(ev->latency);
@@ -4512,7 +4555,33 @@ static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4512 hci_debugfs_create_conn(conn); 4555 hci_debugfs_create_conn(conn);
4513 hci_conn_add_sysfs(conn); 4556 hci_conn_add_sysfs(conn);
4514 4557
4515 hci_proto_connect_cfm(conn, ev->status); 4558 if (!ev->status) {
4559 /* The remote features procedure is defined for master
4560 * role only. So only in case of an initiated connection
4561 * request the remote features.
4562 *
4563 * If the local controller supports slave-initiated features
4564 * exchange, then requesting the remote features in slave
4565 * role is possible. Otherwise just transition into the
4566 * connected state without requesting the remote features.
4567 */
4568 if (conn->out ||
4569 (hdev->le_features[0] & HCI_LE_SLAVE_FEATURES)) {
4570 struct hci_cp_le_read_remote_features cp;
4571
4572 cp.handle = __cpu_to_le16(conn->handle);
4573
4574 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
4575 sizeof(cp), &cp);
4576
4577 hci_conn_hold(conn);
4578 } else {
4579 conn->state = BT_CONNECTED;
4580 hci_connect_cfm(conn, ev->status);
4581 }
4582 } else {
4583 hci_connect_cfm(conn, ev->status);
4584 }
4516 4585
4517 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst, 4586 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
4518 conn->dst_type); 4587 conn->dst_type);
@@ -4658,7 +4727,7 @@ static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
4658 /* If the controller is not using resolvable random 4727 /* If the controller is not using resolvable random
4659 * addresses, then this report can be ignored. 4728 * addresses, then this report can be ignored.
4660 */ 4729 */
4661 if (!test_bit(HCI_PRIVACY, &hdev->dev_flags)) 4730 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
4662 return; 4731 return;
4663 4732
4664 /* If the local IRK of the controller does not match 4733 /* If the local IRK of the controller does not match
@@ -4814,6 +4883,48 @@ static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
4814 hci_dev_unlock(hdev); 4883 hci_dev_unlock(hdev);
4815} 4884}
4816 4885
4886static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
4887 struct sk_buff *skb)
4888{
4889 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
4890 struct hci_conn *conn;
4891
4892 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4893
4894 hci_dev_lock(hdev);
4895
4896 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4897 if (conn) {
4898 if (!ev->status)
4899 memcpy(conn->features[0], ev->features, 8);
4900
4901 if (conn->state == BT_CONFIG) {
4902 __u8 status;
4903
4904 /* If the local controller supports slave-initiated
4905 * features exchange, but the remote controller does
4906 * not, then it is possible that the error code 0x1a
4907 * for unsupported remote feature gets returned.
4908 *
4909 * In this specific case, allow the connection to
4910 * transition into connected state and mark it as
4911 * successful.
4912 */
4913 if ((hdev->le_features[0] & HCI_LE_SLAVE_FEATURES) &&
4914 !conn->out && ev->status == 0x1a)
4915 status = 0x00;
4916 else
4917 status = ev->status;
4918
4919 conn->state = BT_CONNECTED;
4920 hci_connect_cfm(conn, status);
4921 hci_conn_drop(conn);
4922 }
4923 }
4924
4925 hci_dev_unlock(hdev);
4926}
4927
4817static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb) 4928static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
4818{ 4929{
4819 struct hci_ev_le_ltk_req *ev = (void *) skb->data; 4930 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
@@ -4987,6 +5098,10 @@ static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
4987 hci_le_adv_report_evt(hdev, skb); 5098 hci_le_adv_report_evt(hdev, skb);
4988 break; 5099 break;
4989 5100
5101 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
5102 hci_le_remote_feat_complete_evt(hdev, skb);
5103 break;
5104
4990 case HCI_EV_LE_LTK_REQ: 5105 case HCI_EV_LE_LTK_REQ:
4991 hci_le_ltk_request_evt(hdev, skb); 5106 hci_le_ltk_request_evt(hdev, skb);
4992 break; 5107 break;
@@ -5020,32 +5135,79 @@ static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
5020 amp_read_loc_assoc_final_data(hdev, hcon); 5135 amp_read_loc_assoc_final_data(hdev, hcon);
5021} 5136}
5022 5137
5023void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb) 5138static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
5139 u8 event, struct sk_buff *skb)
5024{ 5140{
5025 struct hci_event_hdr *hdr = (void *) skb->data; 5141 struct hci_ev_cmd_complete *ev;
5026 __u8 event = hdr->evt; 5142 struct hci_event_hdr *hdr;
5027 5143
5028 hci_dev_lock(hdev); 5144 if (!skb)
5145 return false;
5029 5146
5030 /* Received events are (currently) only needed when a request is 5147 if (skb->len < sizeof(*hdr)) {
5031 * ongoing so avoid unnecessary memory allocation. 5148 BT_ERR("Too short HCI event");
5032 */ 5149 return false;
5033 if (hci_req_pending(hdev)) {
5034 kfree_skb(hdev->recv_evt);
5035 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
5036 } 5150 }
5037 5151
5038 hci_dev_unlock(hdev); 5152 hdr = (void *) skb->data;
5039
5040 skb_pull(skb, HCI_EVENT_HDR_SIZE); 5153 skb_pull(skb, HCI_EVENT_HDR_SIZE);
5041 5154
5155 if (event) {
5156 if (hdr->evt != event)
5157 return false;
5158 return true;
5159 }
5160
5161 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
5162 BT_DBG("Last event is not cmd complete (0x%2.2x)", hdr->evt);
5163 return false;
5164 }
5165
5166 if (skb->len < sizeof(*ev)) {
5167 BT_ERR("Too short cmd_complete event");
5168 return false;
5169 }
5170
5171 ev = (void *) skb->data;
5172 skb_pull(skb, sizeof(*ev));
5173
5174 if (opcode != __le16_to_cpu(ev->opcode)) {
5175 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
5176 __le16_to_cpu(ev->opcode));
5177 return false;
5178 }
5179
5180 return true;
5181}
5182
5183void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
5184{
5185 struct hci_event_hdr *hdr = (void *) skb->data;
5186 hci_req_complete_t req_complete = NULL;
5187 hci_req_complete_skb_t req_complete_skb = NULL;
5188 struct sk_buff *orig_skb = NULL;
5189 u8 status = 0, event = hdr->evt, req_evt = 0;
5190 u16 opcode = HCI_OP_NOP;
5191
5042 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) { 5192 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
5043 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data; 5193 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
5044 u16 opcode = __le16_to_cpu(cmd_hdr->opcode); 5194 opcode = __le16_to_cpu(cmd_hdr->opcode);
5045 5195 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
5046 hci_req_cmd_complete(hdev, opcode, 0); 5196 &req_complete_skb);
5197 req_evt = event;
5047 } 5198 }
5048 5199
5200 /* If it looks like we might end up having to call
5201 * req_complete_skb, store a pristine copy of the skb since the
5202 * various handlers may modify the original one through
5203 * skb_pull() calls, etc.
5204 */
5205 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
5206 event == HCI_EV_CMD_COMPLETE)
5207 orig_skb = skb_clone(skb, GFP_KERNEL);
5208
5209 skb_pull(skb, HCI_EVENT_HDR_SIZE);
5210
5049 switch (event) { 5211 switch (event) {
5050 case HCI_EV_INQUIRY_COMPLETE: 5212 case HCI_EV_INQUIRY_COMPLETE:
5051 hci_inquiry_complete_evt(hdev, skb); 5213 hci_inquiry_complete_evt(hdev, skb);
@@ -5088,11 +5250,13 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
5088 break; 5250 break;
5089 5251
5090 case HCI_EV_CMD_COMPLETE: 5252 case HCI_EV_CMD_COMPLETE:
5091 hci_cmd_complete_evt(hdev, skb); 5253 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
5254 &req_complete, &req_complete_skb);
5092 break; 5255 break;
5093 5256
5094 case HCI_EV_CMD_STATUS: 5257 case HCI_EV_CMD_STATUS:
5095 hci_cmd_status_evt(hdev, skb); 5258 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
5259 &req_complete_skb);
5096 break; 5260 break;
5097 5261
5098 case HCI_EV_HARDWARE_ERROR: 5262 case HCI_EV_HARDWARE_ERROR:
@@ -5224,6 +5388,17 @@ void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
5224 break; 5388 break;
5225 } 5389 }
5226 5390
5391 if (req_complete) {
5392 req_complete(hdev, status, opcode);
5393 } else if (req_complete_skb) {
5394 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
5395 kfree_skb(orig_skb);
5396 orig_skb = NULL;
5397 }
5398 req_complete_skb(hdev, status, opcode, orig_skb);
5399 }
5400
5401 kfree_skb(orig_skb);
5227 kfree_skb(skb); 5402 kfree_skb(skb);
5228 hdev->stat.evt_rx++; 5403 hdev->stat.evt_rx++;
5229} 5404}
diff --git a/net/bluetooth/hci_request.c b/net/bluetooth/hci_request.c
index b59f92c6df0c..d6025d6e6d59 100644
--- a/net/bluetooth/hci_request.c
+++ b/net/bluetooth/hci_request.c
@@ -34,7 +34,8 @@ void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
34 req->err = 0; 34 req->err = 0;
35} 35}
36 36
37int hci_req_run(struct hci_request *req, hci_req_complete_t complete) 37static int req_run(struct hci_request *req, hci_req_complete_t complete,
38 hci_req_complete_skb_t complete_skb)
38{ 39{
39 struct hci_dev *hdev = req->hdev; 40 struct hci_dev *hdev = req->hdev;
40 struct sk_buff *skb; 41 struct sk_buff *skb;
@@ -56,6 +57,7 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
56 57
57 skb = skb_peek_tail(&req->cmd_q); 58 skb = skb_peek_tail(&req->cmd_q);
58 bt_cb(skb)->req.complete = complete; 59 bt_cb(skb)->req.complete = complete;
60 bt_cb(skb)->req.complete_skb = complete_skb;
59 61
60 spin_lock_irqsave(&hdev->cmd_q.lock, flags); 62 spin_lock_irqsave(&hdev->cmd_q.lock, flags);
61 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); 63 skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
@@ -66,6 +68,16 @@ int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
66 return 0; 68 return 0;
67} 69}
68 70
71int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
72{
73 return req_run(req, complete, NULL);
74}
75
76int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
77{
78 return req_run(req, NULL, complete);
79}
80
69struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen, 81struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
70 const void *param) 82 const void *param)
71{ 83{
@@ -270,7 +282,7 @@ void hci_req_add_le_passive_scan(struct hci_request *req)
270 * and 0x01 (whitelist enabled) use the new filter policies 282 * and 0x01 (whitelist enabled) use the new filter policies
271 * 0x02 (no whitelist) and 0x03 (whitelist enabled). 283 * 0x02 (no whitelist) and 0x03 (whitelist enabled).
272 */ 284 */
273 if (test_bit(HCI_PRIVACY, &hdev->dev_flags) && 285 if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
274 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) 286 (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
275 filter_policy |= 0x02; 287 filter_policy |= 0x02;
276 288
@@ -304,10 +316,10 @@ static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
304 * In this kind of scenario skip the update and let the random 316 * In this kind of scenario skip the update and let the random
305 * address be updated at the next cycle. 317 * address be updated at the next cycle.
306 */ 318 */
307 if (test_bit(HCI_LE_ADV, &hdev->dev_flags) || 319 if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
308 hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) { 320 hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
309 BT_DBG("Deferring random address update"); 321 BT_DBG("Deferring random address update");
310 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 322 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
311 return; 323 return;
312 } 324 }
313 325
@@ -324,12 +336,12 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
324 * current RPA has expired or there is something else than 336 * current RPA has expired or there is something else than
325 * the current RPA in use, then generate a new one. 337 * the current RPA in use, then generate a new one.
326 */ 338 */
327 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) { 339 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
328 int to; 340 int to;
329 341
330 *own_addr_type = ADDR_LE_DEV_RANDOM; 342 *own_addr_type = ADDR_LE_DEV_RANDOM;
331 343
332 if (!test_and_clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags) && 344 if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
333 !bacmp(&hdev->random_addr, &hdev->rpa)) 345 !bacmp(&hdev->random_addr, &hdev->rpa))
334 return 0; 346 return 0;
335 347
@@ -383,9 +395,9 @@ int hci_update_random_address(struct hci_request *req, bool require_privacy,
383 * and a static address has been configured, then use that 395 * and a static address has been configured, then use that
384 * address instead of the public BR/EDR address. 396 * address instead of the public BR/EDR address.
385 */ 397 */
386 if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) || 398 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
387 !bacmp(&hdev->bdaddr, BDADDR_ANY) || 399 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
388 (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) && 400 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
389 bacmp(&hdev->static_addr, BDADDR_ANY))) { 401 bacmp(&hdev->static_addr, BDADDR_ANY))) {
390 *own_addr_type = ADDR_LE_DEV_RANDOM; 402 *own_addr_type = ADDR_LE_DEV_RANDOM;
391 if (bacmp(&hdev->static_addr, &hdev->random_addr)) 403 if (bacmp(&hdev->static_addr, &hdev->random_addr))
@@ -425,7 +437,7 @@ void __hci_update_page_scan(struct hci_request *req)
425 struct hci_dev *hdev = req->hdev; 437 struct hci_dev *hdev = req->hdev;
426 u8 scan; 438 u8 scan;
427 439
428 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 440 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
429 return; 441 return;
430 442
431 if (!hdev_is_powered(hdev)) 443 if (!hdev_is_powered(hdev))
@@ -434,7 +446,7 @@ void __hci_update_page_scan(struct hci_request *req)
434 if (mgmt_powering_down(hdev)) 446 if (mgmt_powering_down(hdev))
435 return; 447 return;
436 448
437 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) || 449 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
438 disconnected_whitelist_entries(hdev)) 450 disconnected_whitelist_entries(hdev))
439 scan = SCAN_PAGE; 451 scan = SCAN_PAGE;
440 else 452 else
@@ -443,7 +455,7 @@ void __hci_update_page_scan(struct hci_request *req)
443 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE)) 455 if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE))
444 return; 456 return;
445 457
446 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) 458 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
447 scan |= SCAN_INQUIRY; 459 scan |= SCAN_INQUIRY;
448 460
449 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 461 hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
@@ -471,14 +483,14 @@ void __hci_update_background_scan(struct hci_request *req)
471 483
472 if (!test_bit(HCI_UP, &hdev->flags) || 484 if (!test_bit(HCI_UP, &hdev->flags) ||
473 test_bit(HCI_INIT, &hdev->flags) || 485 test_bit(HCI_INIT, &hdev->flags) ||
474 test_bit(HCI_SETUP, &hdev->dev_flags) || 486 hci_dev_test_flag(hdev, HCI_SETUP) ||
475 test_bit(HCI_CONFIG, &hdev->dev_flags) || 487 hci_dev_test_flag(hdev, HCI_CONFIG) ||
476 test_bit(HCI_AUTO_OFF, &hdev->dev_flags) || 488 hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
477 test_bit(HCI_UNREGISTER, &hdev->dev_flags)) 489 hci_dev_test_flag(hdev, HCI_UNREGISTER))
478 return; 490 return;
479 491
480 /* No point in doing scanning if LE support hasn't been enabled */ 492 /* No point in doing scanning if LE support hasn't been enabled */
481 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 493 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
482 return; 494 return;
483 495
484 /* If discovery is active don't interfere with it */ 496 /* If discovery is active don't interfere with it */
@@ -502,7 +514,7 @@ void __hci_update_background_scan(struct hci_request *req)
502 */ 514 */
503 515
504 /* If controller is not scanning we are done. */ 516 /* If controller is not scanning we are done. */
505 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 517 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
506 return; 518 return;
507 519
508 hci_req_add_le_scan_disable(req); 520 hci_req_add_le_scan_disable(req);
@@ -524,7 +536,7 @@ void __hci_update_background_scan(struct hci_request *req)
524 /* If controller is currently scanning, we stop it to ensure we 536 /* If controller is currently scanning, we stop it to ensure we
525 * don't miss any advertising (due to duplicates filter). 537 * don't miss any advertising (due to duplicates filter).
526 */ 538 */
527 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 539 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
528 hci_req_add_le_scan_disable(req); 540 hci_req_add_le_scan_disable(req);
529 541
530 hci_req_add_le_passive_scan(req); 542 hci_req_add_le_passive_scan(req);
diff --git a/net/bluetooth/hci_request.h b/net/bluetooth/hci_request.h
index adf074d33544..bf6df92f42db 100644
--- a/net/bluetooth/hci_request.h
+++ b/net/bluetooth/hci_request.h
@@ -32,11 +32,14 @@ struct hci_request {
32 32
33void hci_req_init(struct hci_request *req, struct hci_dev *hdev); 33void hci_req_init(struct hci_request *req, struct hci_dev *hdev);
34int hci_req_run(struct hci_request *req, hci_req_complete_t complete); 34int hci_req_run(struct hci_request *req, hci_req_complete_t complete);
35int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete);
35void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, 36void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
36 const void *param); 37 const void *param);
37void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, 38void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
38 const void *param, u8 event); 39 const void *param, u8 event);
39void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status); 40void hci_req_cmd_complete(struct hci_dev *hdev, u16 opcode, u8 status,
41 hci_req_complete_t *req_complete,
42 hci_req_complete_skb_t *req_complete_skb);
40 43
41struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen, 44struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
42 const void *param); 45 const void *param);
diff --git a/net/bluetooth/hci_sock.c b/net/bluetooth/hci_sock.c
index 1d65c5be7c82..56f9edbf3d05 100644
--- a/net/bluetooth/hci_sock.c
+++ b/net/bluetooth/hci_sock.c
@@ -30,6 +30,12 @@
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/hci_mon.h> 32#include <net/bluetooth/hci_mon.h>
33#include <net/bluetooth/mgmt.h>
34
35#include "mgmt_util.h"
36
37static LIST_HEAD(mgmt_chan_list);
38static DEFINE_MUTEX(mgmt_chan_list_lock);
33 39
34static atomic_t monitor_promisc = ATOMIC_INIT(0); 40static atomic_t monitor_promisc = ATOMIC_INIT(0);
35 41
@@ -44,11 +50,32 @@ struct hci_pinfo {
44 struct hci_filter filter; 50 struct hci_filter filter;
45 __u32 cmsg_mask; 51 __u32 cmsg_mask;
46 unsigned short channel; 52 unsigned short channel;
53 unsigned long flags;
47}; 54};
48 55
49static inline int hci_test_bit(int nr, void *addr) 56void hci_sock_set_flag(struct sock *sk, int nr)
57{
58 set_bit(nr, &hci_pi(sk)->flags);
59}
60
61void hci_sock_clear_flag(struct sock *sk, int nr)
50{ 62{
51 return *((__u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31)); 63 clear_bit(nr, &hci_pi(sk)->flags);
64}
65
66int hci_sock_test_flag(struct sock *sk, int nr)
67{
68 return test_bit(nr, &hci_pi(sk)->flags);
69}
70
71unsigned short hci_sock_get_channel(struct sock *sk)
72{
73 return hci_pi(sk)->channel;
74}
75
76static inline int hci_test_bit(int nr, const void *addr)
77{
78 return *((const __u32 *) addr + (nr >> 5)) & ((__u32) 1 << (nr & 31));
52} 79}
53 80
54/* Security filter */ 81/* Security filter */
@@ -183,54 +210,31 @@ void hci_send_to_sock(struct hci_dev *hdev, struct sk_buff *skb)
183 kfree_skb(skb_copy); 210 kfree_skb(skb_copy);
184} 211}
185 212
186/* Send frame to control socket */ 213/* Send frame to sockets with specific channel */
187void hci_send_to_control(struct sk_buff *skb, struct sock *skip_sk) 214void hci_send_to_channel(unsigned short channel, struct sk_buff *skb,
215 int flag, struct sock *skip_sk)
188{ 216{
189 struct sock *sk; 217 struct sock *sk;
190 218
191 BT_DBG("len %d", skb->len); 219 BT_DBG("channel %u len %d", channel, skb->len);
192 220
193 read_lock(&hci_sk_list.lock); 221 read_lock(&hci_sk_list.lock);
194 222
195 sk_for_each(sk, &hci_sk_list.head) { 223 sk_for_each(sk, &hci_sk_list.head) {
196 struct sk_buff *nskb; 224 struct sk_buff *nskb;
197 225
198 /* Skip the original socket */ 226 /* Ignore socket without the flag set */
199 if (sk == skip_sk) 227 if (!hci_sock_test_flag(sk, flag))
200 continue;
201
202 if (sk->sk_state != BT_BOUND)
203 continue;
204
205 if (hci_pi(sk)->channel != HCI_CHANNEL_CONTROL)
206 continue; 228 continue;
207 229
208 nskb = skb_clone(skb, GFP_ATOMIC); 230 /* Skip the original socket */
209 if (!nskb) 231 if (sk == skip_sk)
210 continue; 232 continue;
211 233
212 if (sock_queue_rcv_skb(sk, nskb))
213 kfree_skb(nskb);
214 }
215
216 read_unlock(&hci_sk_list.lock);
217}
218
219static void queue_monitor_skb(struct sk_buff *skb)
220{
221 struct sock *sk;
222
223 BT_DBG("len %d", skb->len);
224
225 read_lock(&hci_sk_list.lock);
226
227 sk_for_each(sk, &hci_sk_list.head) {
228 struct sk_buff *nskb;
229
230 if (sk->sk_state != BT_BOUND) 234 if (sk->sk_state != BT_BOUND)
231 continue; 235 continue;
232 236
233 if (hci_pi(sk)->channel != HCI_CHANNEL_MONITOR) 237 if (hci_pi(sk)->channel != channel)
234 continue; 238 continue;
235 239
236 nskb = skb_clone(skb, GFP_ATOMIC); 240 nskb = skb_clone(skb, GFP_ATOMIC);
@@ -290,7 +294,8 @@ void hci_send_to_monitor(struct hci_dev *hdev, struct sk_buff *skb)
290 hdr->index = cpu_to_le16(hdev->id); 294 hdr->index = cpu_to_le16(hdev->id);
291 hdr->len = cpu_to_le16(skb->len); 295 hdr->len = cpu_to_le16(skb->len);
292 296
293 queue_monitor_skb(skb_copy); 297 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb_copy,
298 HCI_SOCK_TRUSTED, NULL);
294 kfree_skb(skb_copy); 299 kfree_skb(skb_copy);
295} 300}
296 301
@@ -397,7 +402,8 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
397 402
398 skb = create_monitor_event(hdev, event); 403 skb = create_monitor_event(hdev, event);
399 if (skb) { 404 if (skb) {
400 queue_monitor_skb(skb); 405 hci_send_to_channel(HCI_CHANNEL_MONITOR, skb,
406 HCI_SOCK_TRUSTED, NULL);
401 kfree_skb(skb); 407 kfree_skb(skb);
402 } 408 }
403 } 409 }
@@ -428,6 +434,56 @@ void hci_sock_dev_event(struct hci_dev *hdev, int event)
428 } 434 }
429} 435}
430 436
437static struct hci_mgmt_chan *__hci_mgmt_chan_find(unsigned short channel)
438{
439 struct hci_mgmt_chan *c;
440
441 list_for_each_entry(c, &mgmt_chan_list, list) {
442 if (c->channel == channel)
443 return c;
444 }
445
446 return NULL;
447}
448
449static struct hci_mgmt_chan *hci_mgmt_chan_find(unsigned short channel)
450{
451 struct hci_mgmt_chan *c;
452
453 mutex_lock(&mgmt_chan_list_lock);
454 c = __hci_mgmt_chan_find(channel);
455 mutex_unlock(&mgmt_chan_list_lock);
456
457 return c;
458}
459
460int hci_mgmt_chan_register(struct hci_mgmt_chan *c)
461{
462 if (c->channel < HCI_CHANNEL_CONTROL)
463 return -EINVAL;
464
465 mutex_lock(&mgmt_chan_list_lock);
466 if (__hci_mgmt_chan_find(c->channel)) {
467 mutex_unlock(&mgmt_chan_list_lock);
468 return -EALREADY;
469 }
470
471 list_add_tail(&c->list, &mgmt_chan_list);
472
473 mutex_unlock(&mgmt_chan_list_lock);
474
475 return 0;
476}
477EXPORT_SYMBOL(hci_mgmt_chan_register);
478
479void hci_mgmt_chan_unregister(struct hci_mgmt_chan *c)
480{
481 mutex_lock(&mgmt_chan_list_lock);
482 list_del(&c->list);
483 mutex_unlock(&mgmt_chan_list_lock);
484}
485EXPORT_SYMBOL(hci_mgmt_chan_unregister);
486
431static int hci_sock_release(struct socket *sock) 487static int hci_sock_release(struct socket *sock)
432{ 488{
433 struct sock *sk = sock->sk; 489 struct sock *sk = sock->sk;
@@ -448,7 +504,7 @@ static int hci_sock_release(struct socket *sock)
448 if (hdev) { 504 if (hdev) {
449 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) { 505 if (hci_pi(sk)->channel == HCI_CHANNEL_USER) {
450 mgmt_index_added(hdev); 506 mgmt_index_added(hdev);
451 clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags); 507 hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
452 hci_dev_close(hdev->id); 508 hci_dev_close(hdev->id);
453 } 509 }
454 510
@@ -508,10 +564,10 @@ static int hci_sock_bound_ioctl(struct sock *sk, unsigned int cmd,
508 if (!hdev) 564 if (!hdev)
509 return -EBADFD; 565 return -EBADFD;
510 566
511 if (test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) 567 if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
512 return -EBUSY; 568 return -EBUSY;
513 569
514 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 570 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
515 return -EOPNOTSUPP; 571 return -EOPNOTSUPP;
516 572
517 if (hdev->dev_type != HCI_BREDR) 573 if (hdev->dev_type != HCI_BREDR)
@@ -687,14 +743,14 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
687 743
688 if (test_bit(HCI_UP, &hdev->flags) || 744 if (test_bit(HCI_UP, &hdev->flags) ||
689 test_bit(HCI_INIT, &hdev->flags) || 745 test_bit(HCI_INIT, &hdev->flags) ||
690 test_bit(HCI_SETUP, &hdev->dev_flags) || 746 hci_dev_test_flag(hdev, HCI_SETUP) ||
691 test_bit(HCI_CONFIG, &hdev->dev_flags)) { 747 hci_dev_test_flag(hdev, HCI_CONFIG)) {
692 err = -EBUSY; 748 err = -EBUSY;
693 hci_dev_put(hdev); 749 hci_dev_put(hdev);
694 goto done; 750 goto done;
695 } 751 }
696 752
697 if (test_and_set_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 753 if (hci_dev_test_and_set_flag(hdev, HCI_USER_CHANNEL)) {
698 err = -EUSERS; 754 err = -EUSERS;
699 hci_dev_put(hdev); 755 hci_dev_put(hdev);
700 goto done; 756 goto done;
@@ -704,7 +760,7 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
704 760
705 err = hci_dev_open(hdev->id); 761 err = hci_dev_open(hdev->id);
706 if (err) { 762 if (err) {
707 clear_bit(HCI_USER_CHANNEL, &hdev->dev_flags); 763 hci_dev_clear_flag(hdev, HCI_USER_CHANNEL);
708 mgmt_index_added(hdev); 764 mgmt_index_added(hdev);
709 hci_dev_put(hdev); 765 hci_dev_put(hdev);
710 goto done; 766 goto done;
@@ -715,38 +771,62 @@ static int hci_sock_bind(struct socket *sock, struct sockaddr *addr,
715 hci_pi(sk)->hdev = hdev; 771 hci_pi(sk)->hdev = hdev;
716 break; 772 break;
717 773
718 case HCI_CHANNEL_CONTROL: 774 case HCI_CHANNEL_MONITOR:
719 if (haddr.hci_dev != HCI_DEV_NONE) { 775 if (haddr.hci_dev != HCI_DEV_NONE) {
720 err = -EINVAL; 776 err = -EINVAL;
721 goto done; 777 goto done;
722 } 778 }
723 779
724 if (!capable(CAP_NET_ADMIN)) { 780 if (!capable(CAP_NET_RAW)) {
725 err = -EPERM; 781 err = -EPERM;
726 goto done; 782 goto done;
727 } 783 }
728 784
785 /* The monitor interface is restricted to CAP_NET_RAW
786 * capabilities and with that implicitly trusted.
787 */
788 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
789
790 send_monitor_replay(sk);
791
792 atomic_inc(&monitor_promisc);
729 break; 793 break;
730 794
731 case HCI_CHANNEL_MONITOR: 795 default:
732 if (haddr.hci_dev != HCI_DEV_NONE) { 796 if (!hci_mgmt_chan_find(haddr.hci_channel)) {
733 err = -EINVAL; 797 err = -EINVAL;
734 goto done; 798 goto done;
735 } 799 }
736 800
737 if (!capable(CAP_NET_RAW)) { 801 if (haddr.hci_dev != HCI_DEV_NONE) {
738 err = -EPERM; 802 err = -EINVAL;
739 goto done; 803 goto done;
740 } 804 }
741 805
742 send_monitor_replay(sk); 806 /* Users with CAP_NET_ADMIN capabilities are allowed
743 807 * access to all management commands and events. For
744 atomic_inc(&monitor_promisc); 808 * untrusted users the interface is restricted and
809 * also only untrusted events are sent.
810 */
811 if (capable(CAP_NET_ADMIN))
812 hci_sock_set_flag(sk, HCI_SOCK_TRUSTED);
813
814 /* At the moment the index and unconfigured index events
815 * are enabled unconditionally. Setting them on each
816 * socket when binding keeps this functionality. They
817 * however might be cleared later and then sending of these
818 * events will be disabled, but that is then intentional.
819 *
820 * This also enables generic events that are safe to be
821 * received by untrusted users. Example for such events
822 * are changes to settings, class of device, name etc.
823 */
824 if (haddr.hci_channel == HCI_CHANNEL_CONTROL) {
825 hci_sock_set_flag(sk, HCI_MGMT_INDEX_EVENTS);
826 hci_sock_set_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
827 hci_sock_set_flag(sk, HCI_MGMT_GENERIC_EVENTS);
828 }
745 break; 829 break;
746
747 default:
748 err = -EINVAL;
749 goto done;
750 } 830 }
751 831
752 832
@@ -826,8 +906,8 @@ static void hci_sock_cmsg(struct sock *sk, struct msghdr *msg,
826 } 906 }
827} 907}
828 908
829static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 909static int hci_sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
830 struct msghdr *msg, size_t len, int flags) 910 int flags)
831{ 911{
832 int noblock = flags & MSG_DONTWAIT; 912 int noblock = flags & MSG_DONTWAIT;
833 struct sock *sk = sock->sk; 913 struct sock *sk = sock->sk;
@@ -860,10 +940,13 @@ static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
860 hci_sock_cmsg(sk, msg, skb); 940 hci_sock_cmsg(sk, msg, skb);
861 break; 941 break;
862 case HCI_CHANNEL_USER: 942 case HCI_CHANNEL_USER:
863 case HCI_CHANNEL_CONTROL:
864 case HCI_CHANNEL_MONITOR: 943 case HCI_CHANNEL_MONITOR:
865 sock_recv_timestamp(msg, sk, skb); 944 sock_recv_timestamp(msg, sk, skb);
866 break; 945 break;
946 default:
947 if (hci_mgmt_chan_find(hci_pi(sk)->channel))
948 sock_recv_timestamp(msg, sk, skb);
949 break;
867 } 950 }
868 951
869 skb_free_datagram(sk, skb); 952 skb_free_datagram(sk, skb);
@@ -871,10 +954,122 @@ static int hci_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
871 return err ? : copied; 954 return err ? : copied;
872} 955}
873 956
874static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 957static int hci_mgmt_cmd(struct hci_mgmt_chan *chan, struct sock *sk,
875 struct msghdr *msg, size_t len) 958 struct msghdr *msg, size_t msglen)
959{
960 void *buf;
961 u8 *cp;
962 struct mgmt_hdr *hdr;
963 u16 opcode, index, len;
964 struct hci_dev *hdev = NULL;
965 const struct hci_mgmt_handler *handler;
966 bool var_len, no_hdev;
967 int err;
968
969 BT_DBG("got %zu bytes", msglen);
970
971 if (msglen < sizeof(*hdr))
972 return -EINVAL;
973
974 buf = kmalloc(msglen, GFP_KERNEL);
975 if (!buf)
976 return -ENOMEM;
977
978 if (memcpy_from_msg(buf, msg, msglen)) {
979 err = -EFAULT;
980 goto done;
981 }
982
983 hdr = buf;
984 opcode = __le16_to_cpu(hdr->opcode);
985 index = __le16_to_cpu(hdr->index);
986 len = __le16_to_cpu(hdr->len);
987
988 if (len != msglen - sizeof(*hdr)) {
989 err = -EINVAL;
990 goto done;
991 }
992
993 if (opcode >= chan->handler_count ||
994 chan->handlers[opcode].func == NULL) {
995 BT_DBG("Unknown op %u", opcode);
996 err = mgmt_cmd_status(sk, index, opcode,
997 MGMT_STATUS_UNKNOWN_COMMAND);
998 goto done;
999 }
1000
1001 handler = &chan->handlers[opcode];
1002
1003 if (!hci_sock_test_flag(sk, HCI_SOCK_TRUSTED) &&
1004 !(handler->flags & HCI_MGMT_UNTRUSTED)) {
1005 err = mgmt_cmd_status(sk, index, opcode,
1006 MGMT_STATUS_PERMISSION_DENIED);
1007 goto done;
1008 }
1009
1010 if (index != MGMT_INDEX_NONE) {
1011 hdev = hci_dev_get(index);
1012 if (!hdev) {
1013 err = mgmt_cmd_status(sk, index, opcode,
1014 MGMT_STATUS_INVALID_INDEX);
1015 goto done;
1016 }
1017
1018 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
1019 hci_dev_test_flag(hdev, HCI_CONFIG) ||
1020 hci_dev_test_flag(hdev, HCI_USER_CHANNEL)) {
1021 err = mgmt_cmd_status(sk, index, opcode,
1022 MGMT_STATUS_INVALID_INDEX);
1023 goto done;
1024 }
1025
1026 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) &&
1027 !(handler->flags & HCI_MGMT_UNCONFIGURED)) {
1028 err = mgmt_cmd_status(sk, index, opcode,
1029 MGMT_STATUS_INVALID_INDEX);
1030 goto done;
1031 }
1032 }
1033
1034 no_hdev = (handler->flags & HCI_MGMT_NO_HDEV);
1035 if (no_hdev != !hdev) {
1036 err = mgmt_cmd_status(sk, index, opcode,
1037 MGMT_STATUS_INVALID_INDEX);
1038 goto done;
1039 }
1040
1041 var_len = (handler->flags & HCI_MGMT_VAR_LEN);
1042 if ((var_len && len < handler->data_len) ||
1043 (!var_len && len != handler->data_len)) {
1044 err = mgmt_cmd_status(sk, index, opcode,
1045 MGMT_STATUS_INVALID_PARAMS);
1046 goto done;
1047 }
1048
1049 if (hdev && chan->hdev_init)
1050 chan->hdev_init(sk, hdev);
1051
1052 cp = buf + sizeof(*hdr);
1053
1054 err = handler->func(sk, hdev, cp, len);
1055 if (err < 0)
1056 goto done;
1057
1058 err = msglen;
1059
1060done:
1061 if (hdev)
1062 hci_dev_put(hdev);
1063
1064 kfree(buf);
1065 return err;
1066}
1067
1068static int hci_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1069 size_t len)
876{ 1070{
877 struct sock *sk = sock->sk; 1071 struct sock *sk = sock->sk;
1072 struct hci_mgmt_chan *chan;
878 struct hci_dev *hdev; 1073 struct hci_dev *hdev;
879 struct sk_buff *skb; 1074 struct sk_buff *skb;
880 int err; 1075 int err;
@@ -896,14 +1091,18 @@ static int hci_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
896 case HCI_CHANNEL_RAW: 1091 case HCI_CHANNEL_RAW:
897 case HCI_CHANNEL_USER: 1092 case HCI_CHANNEL_USER:
898 break; 1093 break;
899 case HCI_CHANNEL_CONTROL:
900 err = mgmt_control(sk, msg, len);
901 goto done;
902 case HCI_CHANNEL_MONITOR: 1094 case HCI_CHANNEL_MONITOR:
903 err = -EOPNOTSUPP; 1095 err = -EOPNOTSUPP;
904 goto done; 1096 goto done;
905 default: 1097 default:
906 err = -EINVAL; 1098 mutex_lock(&mgmt_chan_list_lock);
1099 chan = __hci_mgmt_chan_find(hci_pi(sk)->channel);
1100 if (chan)
1101 err = hci_mgmt_cmd(chan, sk, msg, len);
1102 else
1103 err = -EINVAL;
1104
1105 mutex_unlock(&mgmt_chan_list_lock);
907 goto done; 1106 goto done;
908 } 1107 }
909 1108
diff --git a/net/bluetooth/hidp/core.c b/net/bluetooth/hidp/core.c
index 07348e142f16..9070dfd6b4ad 100644
--- a/net/bluetooth/hidp/core.c
+++ b/net/bluetooth/hidp/core.c
@@ -70,10 +70,11 @@ static void hidp_session_terminate(struct hidp_session *s);
70 70
71static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci) 71static void hidp_copy_session(struct hidp_session *session, struct hidp_conninfo *ci)
72{ 72{
73 u32 valid_flags = 0;
73 memset(ci, 0, sizeof(*ci)); 74 memset(ci, 0, sizeof(*ci));
74 bacpy(&ci->bdaddr, &session->bdaddr); 75 bacpy(&ci->bdaddr, &session->bdaddr);
75 76
76 ci->flags = session->flags; 77 ci->flags = session->flags & valid_flags;
77 ci->state = BT_CONNECTED; 78 ci->state = BT_CONNECTED;
78 79
79 if (session->input) { 80 if (session->input) {
@@ -907,7 +908,7 @@ static int hidp_session_new(struct hidp_session **out, const bdaddr_t *bdaddr,
907 kref_init(&session->ref); 908 kref_init(&session->ref);
908 atomic_set(&session->state, HIDP_SESSION_IDLING); 909 atomic_set(&session->state, HIDP_SESSION_IDLING);
909 init_waitqueue_head(&session->state_queue); 910 init_waitqueue_head(&session->state_queue);
910 session->flags = req->flags & (1 << HIDP_BLUETOOTH_VENDOR_ID); 911 session->flags = req->flags & BIT(HIDP_BLUETOOTH_VENDOR_ID);
911 912
912 /* connection management */ 913 /* connection management */
913 bacpy(&session->bdaddr, bdaddr); 914 bacpy(&session->bdaddr, bdaddr);
@@ -1312,6 +1313,8 @@ int hidp_connection_add(struct hidp_connadd_req *req,
1312 struct socket *ctrl_sock, 1313 struct socket *ctrl_sock,
1313 struct socket *intr_sock) 1314 struct socket *intr_sock)
1314{ 1315{
1316 u32 valid_flags = BIT(HIDP_VIRTUAL_CABLE_UNPLUG) |
1317 BIT(HIDP_BOOT_PROTOCOL_MODE);
1315 struct hidp_session *session; 1318 struct hidp_session *session;
1316 struct l2cap_conn *conn; 1319 struct l2cap_conn *conn;
1317 struct l2cap_chan *chan; 1320 struct l2cap_chan *chan;
@@ -1321,6 +1324,9 @@ int hidp_connection_add(struct hidp_connadd_req *req,
1321 if (ret) 1324 if (ret)
1322 return ret; 1325 return ret;
1323 1326
1327 if (req->flags & ~valid_flags)
1328 return -EINVAL;
1329
1324 chan = l2cap_pi(ctrl_sock->sk)->chan; 1330 chan = l2cap_pi(ctrl_sock->sk)->chan;
1325 conn = NULL; 1331 conn = NULL;
1326 l2cap_chan_lock(chan); 1332 l2cap_chan_lock(chan);
@@ -1351,13 +1357,17 @@ out_conn:
1351 1357
1352int hidp_connection_del(struct hidp_conndel_req *req) 1358int hidp_connection_del(struct hidp_conndel_req *req)
1353{ 1359{
1360 u32 valid_flags = BIT(HIDP_VIRTUAL_CABLE_UNPLUG);
1354 struct hidp_session *session; 1361 struct hidp_session *session;
1355 1362
1363 if (req->flags & ~valid_flags)
1364 return -EINVAL;
1365
1356 session = hidp_session_find(&req->bdaddr); 1366 session = hidp_session_find(&req->bdaddr);
1357 if (!session) 1367 if (!session)
1358 return -ENOENT; 1368 return -ENOENT;
1359 1369
1360 if (req->flags & (1 << HIDP_VIRTUAL_CABLE_UNPLUG)) 1370 if (req->flags & BIT(HIDP_VIRTUAL_CABLE_UNPLUG))
1361 hidp_send_ctrl_message(session, 1371 hidp_send_ctrl_message(session,
1362 HIDP_TRANS_HID_CONTROL | 1372 HIDP_TRANS_HID_CONTROL |
1363 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG, 1373 HIDP_CTRL_VIRTUAL_CABLE_UNPLUG,
diff --git a/net/bluetooth/l2cap_core.c b/net/bluetooth/l2cap_core.c
index 6ba33f9631e8..dad419782a12 100644
--- a/net/bluetooth/l2cap_core.c
+++ b/net/bluetooth/l2cap_core.c
@@ -292,7 +292,7 @@ static struct sk_buff *l2cap_ertm_seq_in_queue(struct sk_buff_head *head,
292 struct sk_buff *skb; 292 struct sk_buff *skb;
293 293
294 skb_queue_walk(head, skb) { 294 skb_queue_walk(head, skb) {
295 if (bt_cb(skb)->control.txseq == seq) 295 if (bt_cb(skb)->l2cap.txseq == seq)
296 return skb; 296 return skb;
297 } 297 }
298 298
@@ -954,11 +954,11 @@ static inline void __unpack_control(struct l2cap_chan *chan,
954{ 954{
955 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) { 955 if (test_bit(FLAG_EXT_CTRL, &chan->flags)) {
956 __unpack_extended_control(get_unaligned_le32(skb->data), 956 __unpack_extended_control(get_unaligned_le32(skb->data),
957 &bt_cb(skb)->control); 957 &bt_cb(skb)->l2cap);
958 skb_pull(skb, L2CAP_EXT_CTRL_SIZE); 958 skb_pull(skb, L2CAP_EXT_CTRL_SIZE);
959 } else { 959 } else {
960 __unpack_enhanced_control(get_unaligned_le16(skb->data), 960 __unpack_enhanced_control(get_unaligned_le16(skb->data),
961 &bt_cb(skb)->control); 961 &bt_cb(skb)->l2cap);
962 skb_pull(skb, L2CAP_ENH_CTRL_SIZE); 962 skb_pull(skb, L2CAP_ENH_CTRL_SIZE);
963 } 963 }
964} 964}
@@ -1200,8 +1200,8 @@ static void l2cap_move_setup(struct l2cap_chan *chan)
1200 1200
1201 chan->retry_count = 0; 1201 chan->retry_count = 0;
1202 skb_queue_walk(&chan->tx_q, skb) { 1202 skb_queue_walk(&chan->tx_q, skb) {
1203 if (bt_cb(skb)->control.retries) 1203 if (bt_cb(skb)->l2cap.retries)
1204 bt_cb(skb)->control.retries = 1; 1204 bt_cb(skb)->l2cap.retries = 1;
1205 else 1205 else
1206 break; 1206 break;
1207 } 1207 }
@@ -1244,6 +1244,13 @@ static void l2cap_move_done(struct l2cap_chan *chan)
1244 1244
1245static void l2cap_chan_ready(struct l2cap_chan *chan) 1245static void l2cap_chan_ready(struct l2cap_chan *chan)
1246{ 1246{
1247 /* The channel may have already been flagged as connected in
1248 * case of receiving data before the L2CAP info req/rsp
1249 * procedure is complete.
1250 */
1251 if (chan->state == BT_CONNECTED)
1252 return;
1253
1247 /* This clears all conf flags, including CONF_NOT_COMPLETE */ 1254 /* This clears all conf flags, including CONF_NOT_COMPLETE */
1248 chan->conf_state = 0; 1255 chan->conf_state = 0;
1249 __clear_chan_timer(chan); 1256 __clear_chan_timer(chan);
@@ -1839,8 +1846,8 @@ static void l2cap_streaming_send(struct l2cap_chan *chan,
1839 1846
1840 skb = skb_dequeue(&chan->tx_q); 1847 skb = skb_dequeue(&chan->tx_q);
1841 1848
1842 bt_cb(skb)->control.retries = 1; 1849 bt_cb(skb)->l2cap.retries = 1;
1843 control = &bt_cb(skb)->control; 1850 control = &bt_cb(skb)->l2cap;
1844 1851
1845 control->reqseq = 0; 1852 control->reqseq = 0;
1846 control->txseq = chan->next_tx_seq; 1853 control->txseq = chan->next_tx_seq;
@@ -1884,8 +1891,8 @@ static int l2cap_ertm_send(struct l2cap_chan *chan)
1884 1891
1885 skb = chan->tx_send_head; 1892 skb = chan->tx_send_head;
1886 1893
1887 bt_cb(skb)->control.retries = 1; 1894 bt_cb(skb)->l2cap.retries = 1;
1888 control = &bt_cb(skb)->control; 1895 control = &bt_cb(skb)->l2cap;
1889 1896
1890 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state)) 1897 if (test_and_clear_bit(CONN_SEND_FBIT, &chan->conn_state))
1891 control->final = 1; 1898 control->final = 1;
@@ -1956,11 +1963,11 @@ static void l2cap_ertm_resend(struct l2cap_chan *chan)
1956 continue; 1963 continue;
1957 } 1964 }
1958 1965
1959 bt_cb(skb)->control.retries++; 1966 bt_cb(skb)->l2cap.retries++;
1960 control = bt_cb(skb)->control; 1967 control = bt_cb(skb)->l2cap;
1961 1968
1962 if (chan->max_tx != 0 && 1969 if (chan->max_tx != 0 &&
1963 bt_cb(skb)->control.retries > chan->max_tx) { 1970 bt_cb(skb)->l2cap.retries > chan->max_tx) {
1964 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 1971 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
1965 l2cap_send_disconn_req(chan, ECONNRESET); 1972 l2cap_send_disconn_req(chan, ECONNRESET);
1966 l2cap_seq_list_clear(&chan->retrans_list); 1973 l2cap_seq_list_clear(&chan->retrans_list);
@@ -2038,7 +2045,7 @@ static void l2cap_retransmit_all(struct l2cap_chan *chan,
2038 2045
2039 if (chan->unacked_frames) { 2046 if (chan->unacked_frames) {
2040 skb_queue_walk(&chan->tx_q, skb) { 2047 skb_queue_walk(&chan->tx_q, skb) {
2041 if (bt_cb(skb)->control.txseq == control->reqseq || 2048 if (bt_cb(skb)->l2cap.txseq == control->reqseq ||
2042 skb == chan->tx_send_head) 2049 skb == chan->tx_send_head)
2043 break; 2050 break;
2044 } 2051 }
@@ -2048,7 +2055,7 @@ static void l2cap_retransmit_all(struct l2cap_chan *chan,
2048 break; 2055 break;
2049 2056
2050 l2cap_seq_list_append(&chan->retrans_list, 2057 l2cap_seq_list_append(&chan->retrans_list,
2051 bt_cb(skb)->control.txseq); 2058 bt_cb(skb)->l2cap.txseq);
2052 } 2059 }
2053 2060
2054 l2cap_ertm_resend(chan); 2061 l2cap_ertm_resend(chan);
@@ -2260,8 +2267,8 @@ static struct sk_buff *l2cap_create_iframe_pdu(struct l2cap_chan *chan,
2260 return ERR_PTR(err); 2267 return ERR_PTR(err);
2261 } 2268 }
2262 2269
2263 bt_cb(skb)->control.fcs = chan->fcs; 2270 bt_cb(skb)->l2cap.fcs = chan->fcs;
2264 bt_cb(skb)->control.retries = 0; 2271 bt_cb(skb)->l2cap.retries = 0;
2265 return skb; 2272 return skb;
2266} 2273}
2267 2274
@@ -2314,7 +2321,7 @@ static int l2cap_segment_sdu(struct l2cap_chan *chan,
2314 return PTR_ERR(skb); 2321 return PTR_ERR(skb);
2315 } 2322 }
2316 2323
2317 bt_cb(skb)->control.sar = sar; 2324 bt_cb(skb)->l2cap.sar = sar;
2318 __skb_queue_tail(seg_queue, skb); 2325 __skb_queue_tail(seg_queue, skb);
2319 2326
2320 len -= pdu_len; 2327 len -= pdu_len;
@@ -2849,7 +2856,7 @@ static void l2cap_raw_recv(struct l2cap_conn *conn, struct sk_buff *skb)
2849 continue; 2856 continue;
2850 2857
2851 /* Don't send frame to the channel it came from */ 2858 /* Don't send frame to the channel it came from */
2852 if (bt_cb(skb)->chan == chan) 2859 if (bt_cb(skb)->l2cap.chan == chan)
2853 continue; 2860 continue;
2854 2861
2855 nskb = skb_clone(skb, GFP_KERNEL); 2862 nskb = skb_clone(skb, GFP_KERNEL);
@@ -3893,7 +3900,7 @@ static int l2cap_connect_req(struct l2cap_conn *conn,
3893 return -EPROTO; 3900 return -EPROTO;
3894 3901
3895 hci_dev_lock(hdev); 3902 hci_dev_lock(hdev);
3896 if (test_bit(HCI_MGMT, &hdev->dev_flags) && 3903 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
3897 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags)) 3904 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &hcon->flags))
3898 mgmt_device_connected(hdev, hcon, 0, NULL, 0); 3905 mgmt_device_connected(hdev, hcon, 0, NULL, 0);
3899 hci_dev_unlock(hdev); 3906 hci_dev_unlock(hdev);
@@ -5911,7 +5918,7 @@ static int l2cap_rx_queued_iframes(struct l2cap_chan *chan)
5911 5918
5912 skb_unlink(skb, &chan->srej_q); 5919 skb_unlink(skb, &chan->srej_q);
5913 chan->buffer_seq = __next_seq(chan, chan->buffer_seq); 5920 chan->buffer_seq = __next_seq(chan, chan->buffer_seq);
5914 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->control); 5921 err = l2cap_reassemble_sdu(chan, skb, &bt_cb(skb)->l2cap);
5915 if (err) 5922 if (err)
5916 break; 5923 break;
5917 } 5924 }
@@ -5945,7 +5952,7 @@ static void l2cap_handle_srej(struct l2cap_chan *chan,
5945 return; 5952 return;
5946 } 5953 }
5947 5954
5948 if (chan->max_tx != 0 && bt_cb(skb)->control.retries >= chan->max_tx) { 5955 if (chan->max_tx != 0 && bt_cb(skb)->l2cap.retries >= chan->max_tx) {
5949 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 5956 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
5950 l2cap_send_disconn_req(chan, ECONNRESET); 5957 l2cap_send_disconn_req(chan, ECONNRESET);
5951 return; 5958 return;
@@ -5998,7 +6005,7 @@ static void l2cap_handle_rej(struct l2cap_chan *chan,
5998 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq); 6005 skb = l2cap_ertm_seq_in_queue(&chan->tx_q, control->reqseq);
5999 6006
6000 if (chan->max_tx && skb && 6007 if (chan->max_tx && skb &&
6001 bt_cb(skb)->control.retries >= chan->max_tx) { 6008 bt_cb(skb)->l2cap.retries >= chan->max_tx) {
6002 BT_DBG("Retry limit exceeded (%d)", chan->max_tx); 6009 BT_DBG("Retry limit exceeded (%d)", chan->max_tx);
6003 l2cap_send_disconn_req(chan, ECONNRESET); 6010 l2cap_send_disconn_req(chan, ECONNRESET);
6004 return; 6011 return;
@@ -6558,7 +6565,7 @@ static int l2cap_stream_rx(struct l2cap_chan *chan, struct l2cap_ctrl *control,
6558 6565
6559static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb) 6566static int l2cap_data_rcv(struct l2cap_chan *chan, struct sk_buff *skb)
6560{ 6567{
6561 struct l2cap_ctrl *control = &bt_cb(skb)->control; 6568 struct l2cap_ctrl *control = &bt_cb(skb)->l2cap;
6562 u16 len; 6569 u16 len;
6563 u8 event; 6570 u8 event;
6564 6571
@@ -6785,6 +6792,13 @@ static void l2cap_data_channel(struct l2cap_conn *conn, u16 cid,
6785 6792
6786 BT_DBG("chan %p, len %d", chan, skb->len); 6793 BT_DBG("chan %p, len %d", chan, skb->len);
6787 6794
6795 /* If we receive data on a fixed channel before the info req/rsp
6796 * procdure is done simply assume that the channel is supported
6797 * and mark it as ready.
6798 */
6799 if (chan->chan_type == L2CAP_CHAN_FIXED)
6800 l2cap_chan_ready(chan);
6801
6788 if (chan->state != BT_CONNECTED) 6802 if (chan->state != BT_CONNECTED)
6789 goto drop; 6803 goto drop;
6790 6804
@@ -6850,8 +6864,8 @@ static void l2cap_conless_channel(struct l2cap_conn *conn, __le16 psm,
6850 goto drop; 6864 goto drop;
6851 6865
6852 /* Store remote BD_ADDR and PSM for msg_name */ 6866 /* Store remote BD_ADDR and PSM for msg_name */
6853 bacpy(&bt_cb(skb)->bdaddr, &hcon->dst); 6867 bacpy(&bt_cb(skb)->l2cap.bdaddr, &hcon->dst);
6854 bt_cb(skb)->psm = psm; 6868 bt_cb(skb)->l2cap.psm = psm;
6855 6869
6856 if (!chan->ops->recv(chan, skb)) { 6870 if (!chan->ops->recv(chan, skb)) {
6857 l2cap_chan_put(chan); 6871 l2cap_chan_put(chan);
@@ -6973,12 +6987,12 @@ static struct l2cap_conn *l2cap_conn_add(struct hci_conn *hcon)
6973 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS; 6987 conn->local_fixed_chan = L2CAP_FC_SIG_BREDR | L2CAP_FC_CONNLESS;
6974 6988
6975 if (hcon->type == ACL_LINK && 6989 if (hcon->type == ACL_LINK &&
6976 test_bit(HCI_HS_ENABLED, &hcon->hdev->dev_flags)) 6990 hci_dev_test_flag(hcon->hdev, HCI_HS_ENABLED))
6977 conn->local_fixed_chan |= L2CAP_FC_A2MP; 6991 conn->local_fixed_chan |= L2CAP_FC_A2MP;
6978 6992
6979 if (test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags) && 6993 if (hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED) &&
6980 (bredr_sc_enabled(hcon->hdev) || 6994 (bredr_sc_enabled(hcon->hdev) ||
6981 test_bit(HCI_FORCE_BREDR_SMP, &hcon->hdev->dbg_flags))) 6995 hci_dev_test_flag(hcon->hdev, HCI_FORCE_BREDR_SMP)))
6982 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR; 6996 conn->local_fixed_chan |= L2CAP_FC_SMP_BREDR;
6983 6997
6984 mutex_init(&conn->ident_lock); 6998 mutex_init(&conn->ident_lock);
@@ -7098,7 +7112,7 @@ int l2cap_chan_connect(struct l2cap_chan *chan, __le16 psm, u16 cid,
7098 else 7112 else
7099 dst_type = ADDR_LE_DEV_RANDOM; 7113 dst_type = ADDR_LE_DEV_RANDOM;
7100 7114
7101 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 7115 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7102 role = HCI_ROLE_SLAVE; 7116 role = HCI_ROLE_SLAVE;
7103 else 7117 else
7104 role = HCI_ROLE_MASTER; 7118 role = HCI_ROLE_MASTER;
@@ -7238,13 +7252,16 @@ static struct l2cap_chan *l2cap_global_fixed_chan(struct l2cap_chan *c,
7238 return NULL; 7252 return NULL;
7239} 7253}
7240 7254
7241void l2cap_connect_cfm(struct hci_conn *hcon, u8 status) 7255static void l2cap_connect_cfm(struct hci_conn *hcon, u8 status)
7242{ 7256{
7243 struct hci_dev *hdev = hcon->hdev; 7257 struct hci_dev *hdev = hcon->hdev;
7244 struct l2cap_conn *conn; 7258 struct l2cap_conn *conn;
7245 struct l2cap_chan *pchan; 7259 struct l2cap_chan *pchan;
7246 u8 dst_type; 7260 u8 dst_type;
7247 7261
7262 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7263 return;
7264
7248 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); 7265 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
7249 7266
7250 if (status) { 7267 if (status) {
@@ -7307,8 +7324,11 @@ int l2cap_disconn_ind(struct hci_conn *hcon)
7307 return conn->disc_reason; 7324 return conn->disc_reason;
7308} 7325}
7309 7326
7310void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason) 7327static void l2cap_disconn_cfm(struct hci_conn *hcon, u8 reason)
7311{ 7328{
7329 if (hcon->type != ACL_LINK && hcon->type != LE_LINK)
7330 return;
7331
7312 BT_DBG("hcon %p reason %d", hcon, reason); 7332 BT_DBG("hcon %p reason %d", hcon, reason);
7313 7333
7314 l2cap_conn_del(hcon, bt_to_errno(reason)); 7334 l2cap_conn_del(hcon, bt_to_errno(reason));
@@ -7331,13 +7351,13 @@ static inline void l2cap_check_encryption(struct l2cap_chan *chan, u8 encrypt)
7331 } 7351 }
7332} 7352}
7333 7353
7334int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt) 7354static void l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
7335{ 7355{
7336 struct l2cap_conn *conn = hcon->l2cap_data; 7356 struct l2cap_conn *conn = hcon->l2cap_data;
7337 struct l2cap_chan *chan; 7357 struct l2cap_chan *chan;
7338 7358
7339 if (!conn) 7359 if (!conn)
7340 return 0; 7360 return;
7341 7361
7342 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt); 7362 BT_DBG("conn %p status 0x%2.2x encrypt %u", conn, status, encrypt);
7343 7363
@@ -7420,8 +7440,6 @@ int l2cap_security_cfm(struct hci_conn *hcon, u8 status, u8 encrypt)
7420 } 7440 }
7421 7441
7422 mutex_unlock(&conn->chan_lock); 7442 mutex_unlock(&conn->chan_lock);
7423
7424 return 0;
7425} 7443}
7426 7444
7427int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags) 7445int l2cap_recv_acldata(struct hci_conn *hcon, struct sk_buff *skb, u16 flags)
@@ -7529,6 +7547,13 @@ drop:
7529 return 0; 7547 return 0;
7530} 7548}
7531 7549
7550static struct hci_cb l2cap_cb = {
7551 .name = "L2CAP",
7552 .connect_cfm = l2cap_connect_cfm,
7553 .disconn_cfm = l2cap_disconn_cfm,
7554 .security_cfm = l2cap_security_cfm,
7555};
7556
7532static int l2cap_debugfs_show(struct seq_file *f, void *p) 7557static int l2cap_debugfs_show(struct seq_file *f, void *p)
7533{ 7558{
7534 struct l2cap_chan *c; 7559 struct l2cap_chan *c;
@@ -7570,6 +7595,8 @@ int __init l2cap_init(void)
7570 if (err < 0) 7595 if (err < 0)
7571 return err; 7596 return err;
7572 7597
7598 hci_register_cb(&l2cap_cb);
7599
7573 if (IS_ERR_OR_NULL(bt_debugfs)) 7600 if (IS_ERR_OR_NULL(bt_debugfs))
7574 return 0; 7601 return 0;
7575 7602
@@ -7587,6 +7614,7 @@ int __init l2cap_init(void)
7587void l2cap_exit(void) 7614void l2cap_exit(void)
7588{ 7615{
7589 debugfs_remove(l2cap_debugfs); 7616 debugfs_remove(l2cap_debugfs);
7617 hci_unregister_cb(&l2cap_cb);
7590 l2cap_cleanup_sockets(); 7618 l2cap_cleanup_sockets();
7591} 7619}
7592 7620
diff --git a/net/bluetooth/l2cap_sock.c b/net/bluetooth/l2cap_sock.c
index 60694f0f4c73..a7278f05eafb 100644
--- a/net/bluetooth/l2cap_sock.c
+++ b/net/bluetooth/l2cap_sock.c
@@ -944,8 +944,8 @@ static int l2cap_sock_setsockopt(struct socket *sock, int level, int optname,
944 return err; 944 return err;
945} 945}
946 946
947static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 947static int l2cap_sock_sendmsg(struct socket *sock, struct msghdr *msg,
948 struct msghdr *msg, size_t len) 948 size_t len)
949{ 949{
950 struct sock *sk = sock->sk; 950 struct sock *sk = sock->sk;
951 struct l2cap_chan *chan = l2cap_pi(sk)->chan; 951 struct l2cap_chan *chan = l2cap_pi(sk)->chan;
@@ -976,8 +976,8 @@ static int l2cap_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
976 return err; 976 return err;
977} 977}
978 978
979static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 979static int l2cap_sock_recvmsg(struct socket *sock, struct msghdr *msg,
980 struct msghdr *msg, size_t len, int flags) 980 size_t len, int flags)
981{ 981{
982 struct sock *sk = sock->sk; 982 struct sock *sk = sock->sk;
983 struct l2cap_pinfo *pi = l2cap_pi(sk); 983 struct l2cap_pinfo *pi = l2cap_pi(sk);
@@ -1004,9 +1004,9 @@ static int l2cap_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
1004 release_sock(sk); 1004 release_sock(sk);
1005 1005
1006 if (sock->type == SOCK_STREAM) 1006 if (sock->type == SOCK_STREAM)
1007 err = bt_sock_stream_recvmsg(iocb, sock, msg, len, flags); 1007 err = bt_sock_stream_recvmsg(sock, msg, len, flags);
1008 else 1008 else
1009 err = bt_sock_recvmsg(iocb, sock, msg, len, flags); 1009 err = bt_sock_recvmsg(sock, msg, len, flags);
1010 1010
1011 if (pi->chan->mode != L2CAP_MODE_ERTM) 1011 if (pi->chan->mode != L2CAP_MODE_ERTM)
1012 return err; 1012 return err;
@@ -1330,7 +1330,7 @@ static struct sk_buff *l2cap_sock_alloc_skb_cb(struct l2cap_chan *chan,
1330 1330
1331 skb->priority = sk->sk_priority; 1331 skb->priority = sk->sk_priority;
1332 1332
1333 bt_cb(skb)->chan = chan; 1333 bt_cb(skb)->l2cap.chan = chan;
1334 1334
1335 return skb; 1335 return skb;
1336} 1336}
@@ -1444,8 +1444,8 @@ static void l2cap_skb_msg_name(struct sk_buff *skb, void *msg_name,
1444 1444
1445 memset(la, 0, sizeof(struct sockaddr_l2)); 1445 memset(la, 0, sizeof(struct sockaddr_l2));
1446 la->l2_family = AF_BLUETOOTH; 1446 la->l2_family = AF_BLUETOOTH;
1447 la->l2_psm = bt_cb(skb)->psm; 1447 la->l2_psm = bt_cb(skb)->l2cap.psm;
1448 bacpy(&la->l2_bdaddr, &bt_cb(skb)->bdaddr); 1448 bacpy(&la->l2_bdaddr, &bt_cb(skb)->l2cap.bdaddr);
1449 1449
1450 *msg_namelen = sizeof(struct sockaddr_l2); 1450 *msg_namelen = sizeof(struct sockaddr_l2);
1451} 1451}
diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index 9ec5390c85eb..7fd87e7135b5 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -29,14 +29,16 @@
29 29
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/hci_sock.h>
32#include <net/bluetooth/l2cap.h> 33#include <net/bluetooth/l2cap.h>
33#include <net/bluetooth/mgmt.h> 34#include <net/bluetooth/mgmt.h>
34 35
35#include "hci_request.h" 36#include "hci_request.h"
36#include "smp.h" 37#include "smp.h"
38#include "mgmt_util.h"
37 39
38#define MGMT_VERSION 1 40#define MGMT_VERSION 1
39#define MGMT_REVISION 8 41#define MGMT_REVISION 9
40 42
41static const u16 mgmt_commands[] = { 43static const u16 mgmt_commands[] = {
42 MGMT_OP_READ_INDEX_LIST, 44 MGMT_OP_READ_INDEX_LIST,
@@ -95,6 +97,11 @@ static const u16 mgmt_commands[] = {
95 MGMT_OP_SET_EXTERNAL_CONFIG, 97 MGMT_OP_SET_EXTERNAL_CONFIG,
96 MGMT_OP_SET_PUBLIC_ADDRESS, 98 MGMT_OP_SET_PUBLIC_ADDRESS,
97 MGMT_OP_START_SERVICE_DISCOVERY, 99 MGMT_OP_START_SERVICE_DISCOVERY,
100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
101 MGMT_OP_READ_EXT_INDEX_LIST,
102 MGMT_OP_READ_ADV_FEATURES,
103 MGMT_OP_ADD_ADVERTISING,
104 MGMT_OP_REMOVE_ADVERTISING,
98}; 105};
99 106
100static const u16 mgmt_events[] = { 107static const u16 mgmt_events[] = {
@@ -127,6 +134,32 @@ static const u16 mgmt_events[] = {
127 MGMT_EV_UNCONF_INDEX_ADDED, 134 MGMT_EV_UNCONF_INDEX_ADDED,
128 MGMT_EV_UNCONF_INDEX_REMOVED, 135 MGMT_EV_UNCONF_INDEX_REMOVED,
129 MGMT_EV_NEW_CONFIG_OPTIONS, 136 MGMT_EV_NEW_CONFIG_OPTIONS,
137 MGMT_EV_EXT_INDEX_ADDED,
138 MGMT_EV_EXT_INDEX_REMOVED,
139 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
140 MGMT_EV_ADVERTISING_ADDED,
141 MGMT_EV_ADVERTISING_REMOVED,
142};
143
144static const u16 mgmt_untrusted_commands[] = {
145 MGMT_OP_READ_INDEX_LIST,
146 MGMT_OP_READ_INFO,
147 MGMT_OP_READ_UNCONF_INDEX_LIST,
148 MGMT_OP_READ_CONFIG_INFO,
149 MGMT_OP_READ_EXT_INDEX_LIST,
150};
151
152static const u16 mgmt_untrusted_events[] = {
153 MGMT_EV_INDEX_ADDED,
154 MGMT_EV_INDEX_REMOVED,
155 MGMT_EV_NEW_SETTINGS,
156 MGMT_EV_CLASS_OF_DEV_CHANGED,
157 MGMT_EV_LOCAL_NAME_CHANGED,
158 MGMT_EV_UNCONF_INDEX_ADDED,
159 MGMT_EV_UNCONF_INDEX_REMOVED,
160 MGMT_EV_NEW_CONFIG_OPTIONS,
161 MGMT_EV_EXT_INDEX_ADDED,
162 MGMT_EV_EXT_INDEX_REMOVED,
130}; 163};
131 164
132#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000) 165#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
@@ -134,17 +167,6 @@ static const u16 mgmt_events[] = {
134#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \ 167#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
135 "\x00\x00\x00\x00\x00\x00\x00\x00" 168 "\x00\x00\x00\x00\x00\x00\x00\x00"
136 169
137struct pending_cmd {
138 struct list_head list;
139 u16 opcode;
140 int index;
141 void *param;
142 size_t param_len;
143 struct sock *sk;
144 void *user_data;
145 int (*cmd_complete)(struct pending_cmd *cmd, u8 status);
146};
147
148/* HCI to MGMT error code conversion table */ 170/* HCI to MGMT error code conversion table */
149static u8 mgmt_status_table[] = { 171static u8 mgmt_status_table[] = {
150 MGMT_STATUS_SUCCESS, 172 MGMT_STATUS_SUCCESS,
@@ -218,98 +240,32 @@ static u8 mgmt_status(u8 hci_status)
218 return MGMT_STATUS_FAILED; 240 return MGMT_STATUS_FAILED;
219} 241}
220 242
221static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 data_len, 243static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
222 struct sock *skip_sk) 244 u16 len, int flag)
223{ 245{
224 struct sk_buff *skb; 246 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
225 struct mgmt_hdr *hdr; 247 flag, NULL);
226
227 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
228 if (!skb)
229 return -ENOMEM;
230
231 hdr = (void *) skb_put(skb, sizeof(*hdr));
232 hdr->opcode = cpu_to_le16(event);
233 if (hdev)
234 hdr->index = cpu_to_le16(hdev->id);
235 else
236 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
237 hdr->len = cpu_to_le16(data_len);
238
239 if (data)
240 memcpy(skb_put(skb, data_len), data, data_len);
241
242 /* Time stamp */
243 __net_timestamp(skb);
244
245 hci_send_to_control(skb, skip_sk);
246 kfree_skb(skb);
247
248 return 0;
249} 248}
250 249
251static int cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status) 250static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
251 u16 len, int flag, struct sock *skip_sk)
252{ 252{
253 struct sk_buff *skb; 253 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
254 struct mgmt_hdr *hdr; 254 flag, skip_sk);
255 struct mgmt_ev_cmd_status *ev;
256 int err;
257
258 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
259
260 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
261 if (!skb)
262 return -ENOMEM;
263
264 hdr = (void *) skb_put(skb, sizeof(*hdr));
265
266 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
267 hdr->index = cpu_to_le16(index);
268 hdr->len = cpu_to_le16(sizeof(*ev));
269
270 ev = (void *) skb_put(skb, sizeof(*ev));
271 ev->status = status;
272 ev->opcode = cpu_to_le16(cmd);
273
274 err = sock_queue_rcv_skb(sk, skb);
275 if (err < 0)
276 kfree_skb(skb);
277
278 return err;
279} 255}
280 256
281static int cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status, 257static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
282 void *rp, size_t rp_len) 258 u16 len, struct sock *skip_sk)
283{ 259{
284 struct sk_buff *skb; 260 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
285 struct mgmt_hdr *hdr; 261 HCI_MGMT_GENERIC_EVENTS, skip_sk);
286 struct mgmt_ev_cmd_complete *ev; 262}
287 int err;
288
289 BT_DBG("sock %p", sk);
290
291 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
292 if (!skb)
293 return -ENOMEM;
294
295 hdr = (void *) skb_put(skb, sizeof(*hdr));
296
297 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
298 hdr->index = cpu_to_le16(index);
299 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
300
301 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
302 ev->opcode = cpu_to_le16(cmd);
303 ev->status = status;
304
305 if (rp)
306 memcpy(ev->data, rp, rp_len);
307
308 err = sock_queue_rcv_skb(sk, skb);
309 if (err < 0)
310 kfree_skb(skb);
311 263
312 return err; 264static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
265 struct sock *skip_sk)
266{
267 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
268 HCI_SOCK_TRUSTED, skip_sk);
313} 269}
314 270
315static int read_version(struct sock *sk, struct hci_dev *hdev, void *data, 271static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
@@ -322,22 +278,28 @@ static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
322 rp.version = MGMT_VERSION; 278 rp.version = MGMT_VERSION;
323 rp.revision = cpu_to_le16(MGMT_REVISION); 279 rp.revision = cpu_to_le16(MGMT_REVISION);
324 280
325 return cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0, &rp, 281 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
326 sizeof(rp)); 282 &rp, sizeof(rp));
327} 283}
328 284
329static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data, 285static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
330 u16 data_len) 286 u16 data_len)
331{ 287{
332 struct mgmt_rp_read_commands *rp; 288 struct mgmt_rp_read_commands *rp;
333 const u16 num_commands = ARRAY_SIZE(mgmt_commands); 289 u16 num_commands, num_events;
334 const u16 num_events = ARRAY_SIZE(mgmt_events);
335 __le16 *opcode;
336 size_t rp_size; 290 size_t rp_size;
337 int i, err; 291 int i, err;
338 292
339 BT_DBG("sock %p", sk); 293 BT_DBG("sock %p", sk);
340 294
295 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
296 num_commands = ARRAY_SIZE(mgmt_commands);
297 num_events = ARRAY_SIZE(mgmt_events);
298 } else {
299 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
300 num_events = ARRAY_SIZE(mgmt_untrusted_events);
301 }
302
341 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16)); 303 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
342 304
343 rp = kmalloc(rp_size, GFP_KERNEL); 305 rp = kmalloc(rp_size, GFP_KERNEL);
@@ -347,14 +309,26 @@ static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
347 rp->num_commands = cpu_to_le16(num_commands); 309 rp->num_commands = cpu_to_le16(num_commands);
348 rp->num_events = cpu_to_le16(num_events); 310 rp->num_events = cpu_to_le16(num_events);
349 311
350 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++) 312 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
351 put_unaligned_le16(mgmt_commands[i], opcode); 313 __le16 *opcode = rp->opcodes;
314
315 for (i = 0; i < num_commands; i++, opcode++)
316 put_unaligned_le16(mgmt_commands[i], opcode);
317
318 for (i = 0; i < num_events; i++, opcode++)
319 put_unaligned_le16(mgmt_events[i], opcode);
320 } else {
321 __le16 *opcode = rp->opcodes;
322
323 for (i = 0; i < num_commands; i++, opcode++)
324 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
352 325
353 for (i = 0; i < num_events; i++, opcode++) 326 for (i = 0; i < num_events; i++, opcode++)
354 put_unaligned_le16(mgmt_events[i], opcode); 327 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
328 }
355 329
356 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0, rp, 330 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
357 rp_size); 331 rp, rp_size);
358 kfree(rp); 332 kfree(rp);
359 333
360 return err; 334 return err;
@@ -376,7 +350,7 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
376 count = 0; 350 count = 0;
377 list_for_each_entry(d, &hci_dev_list, list) { 351 list_for_each_entry(d, &hci_dev_list, list) {
378 if (d->dev_type == HCI_BREDR && 352 if (d->dev_type == HCI_BREDR &&
379 !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) 353 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
380 count++; 354 count++;
381 } 355 }
382 356
@@ -389,9 +363,9 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
389 363
390 count = 0; 364 count = 0;
391 list_for_each_entry(d, &hci_dev_list, list) { 365 list_for_each_entry(d, &hci_dev_list, list) {
392 if (test_bit(HCI_SETUP, &d->dev_flags) || 366 if (hci_dev_test_flag(d, HCI_SETUP) ||
393 test_bit(HCI_CONFIG, &d->dev_flags) || 367 hci_dev_test_flag(d, HCI_CONFIG) ||
394 test_bit(HCI_USER_CHANNEL, &d->dev_flags)) 368 hci_dev_test_flag(d, HCI_USER_CHANNEL))
395 continue; 369 continue;
396 370
397 /* Devices marked as raw-only are neither configured 371 /* Devices marked as raw-only are neither configured
@@ -401,7 +375,7 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
401 continue; 375 continue;
402 376
403 if (d->dev_type == HCI_BREDR && 377 if (d->dev_type == HCI_BREDR &&
404 !test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { 378 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
405 rp->index[count++] = cpu_to_le16(d->id); 379 rp->index[count++] = cpu_to_le16(d->id);
406 BT_DBG("Added hci%u", d->id); 380 BT_DBG("Added hci%u", d->id);
407 } 381 }
@@ -412,8 +386,8 @@ static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
412 386
413 read_unlock(&hci_dev_list_lock); 387 read_unlock(&hci_dev_list_lock);
414 388
415 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST, 0, rp, 389 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
416 rp_len); 390 0, rp, rp_len);
417 391
418 kfree(rp); 392 kfree(rp);
419 393
@@ -436,7 +410,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
436 count = 0; 410 count = 0;
437 list_for_each_entry(d, &hci_dev_list, list) { 411 list_for_each_entry(d, &hci_dev_list, list) {
438 if (d->dev_type == HCI_BREDR && 412 if (d->dev_type == HCI_BREDR &&
439 test_bit(HCI_UNCONFIGURED, &d->dev_flags)) 413 hci_dev_test_flag(d, HCI_UNCONFIGURED))
440 count++; 414 count++;
441 } 415 }
442 416
@@ -449,9 +423,9 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
449 423
450 count = 0; 424 count = 0;
451 list_for_each_entry(d, &hci_dev_list, list) { 425 list_for_each_entry(d, &hci_dev_list, list) {
452 if (test_bit(HCI_SETUP, &d->dev_flags) || 426 if (hci_dev_test_flag(d, HCI_SETUP) ||
453 test_bit(HCI_CONFIG, &d->dev_flags) || 427 hci_dev_test_flag(d, HCI_CONFIG) ||
454 test_bit(HCI_USER_CHANNEL, &d->dev_flags)) 428 hci_dev_test_flag(d, HCI_USER_CHANNEL))
455 continue; 429 continue;
456 430
457 /* Devices marked as raw-only are neither configured 431 /* Devices marked as raw-only are neither configured
@@ -461,7 +435,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
461 continue; 435 continue;
462 436
463 if (d->dev_type == HCI_BREDR && 437 if (d->dev_type == HCI_BREDR &&
464 test_bit(HCI_UNCONFIGURED, &d->dev_flags)) { 438 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
465 rp->index[count++] = cpu_to_le16(d->id); 439 rp->index[count++] = cpu_to_le16(d->id);
466 BT_DBG("Added hci%u", d->id); 440 BT_DBG("Added hci%u", d->id);
467 } 441 }
@@ -472,8 +446,84 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
472 446
473 read_unlock(&hci_dev_list_lock); 447 read_unlock(&hci_dev_list_lock);
474 448
475 err = cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_UNCONF_INDEX_LIST, 449 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
476 0, rp, rp_len); 450 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
451
452 kfree(rp);
453
454 return err;
455}
456
457static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
458 void *data, u16 data_len)
459{
460 struct mgmt_rp_read_ext_index_list *rp;
461 struct hci_dev *d;
462 size_t rp_len;
463 u16 count;
464 int err;
465
466 BT_DBG("sock %p", sk);
467
468 read_lock(&hci_dev_list_lock);
469
470 count = 0;
471 list_for_each_entry(d, &hci_dev_list, list) {
472 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
473 count++;
474 }
475
476 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
477 rp = kmalloc(rp_len, GFP_ATOMIC);
478 if (!rp) {
479 read_unlock(&hci_dev_list_lock);
480 return -ENOMEM;
481 }
482
483 count = 0;
484 list_for_each_entry(d, &hci_dev_list, list) {
485 if (hci_dev_test_flag(d, HCI_SETUP) ||
486 hci_dev_test_flag(d, HCI_CONFIG) ||
487 hci_dev_test_flag(d, HCI_USER_CHANNEL))
488 continue;
489
490 /* Devices marked as raw-only are neither configured
491 * nor unconfigured controllers.
492 */
493 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
494 continue;
495
496 if (d->dev_type == HCI_BREDR) {
497 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
498 rp->entry[count].type = 0x01;
499 else
500 rp->entry[count].type = 0x00;
501 } else if (d->dev_type == HCI_AMP) {
502 rp->entry[count].type = 0x02;
503 } else {
504 continue;
505 }
506
507 rp->entry[count].bus = d->bus;
508 rp->entry[count++].index = cpu_to_le16(d->id);
509 BT_DBG("Added hci%u", d->id);
510 }
511
512 rp->num_controllers = cpu_to_le16(count);
513 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
514
515 read_unlock(&hci_dev_list_lock);
516
517 /* If this command is called at least once, then all the
518 * default index and unconfigured index events are disabled
519 * and from now on only extended index events are used.
520 */
521 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
522 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
523 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
524
525 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
526 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
477 527
478 kfree(rp); 528 kfree(rp);
479 529
@@ -483,7 +533,7 @@ static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
483static bool is_configured(struct hci_dev *hdev) 533static bool is_configured(struct hci_dev *hdev)
484{ 534{
485 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && 535 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
486 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) 536 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
487 return false; 537 return false;
488 538
489 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && 539 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
@@ -498,7 +548,7 @@ static __le32 get_missing_options(struct hci_dev *hdev)
498 u32 options = 0; 548 u32 options = 0;
499 549
500 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) && 550 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
501 !test_bit(HCI_EXT_CONFIGURED, &hdev->dev_flags)) 551 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
502 options |= MGMT_OPTION_EXTERNAL_CONFIG; 552 options |= MGMT_OPTION_EXTERNAL_CONFIG;
503 553
504 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) && 554 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
@@ -512,16 +562,16 @@ static int new_options(struct hci_dev *hdev, struct sock *skip)
512{ 562{
513 __le32 options = get_missing_options(hdev); 563 __le32 options = get_missing_options(hdev);
514 564
515 return mgmt_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options, 565 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
516 sizeof(options), skip); 566 sizeof(options), skip);
517} 567}
518 568
519static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) 569static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
520{ 570{
521 __le32 options = get_missing_options(hdev); 571 __le32 options = get_missing_options(hdev);
522 572
523 return cmd_complete(sk, hdev->id, opcode, 0, &options, 573 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
524 sizeof(options)); 574 sizeof(options));
525} 575}
526 576
527static int read_config_info(struct sock *sk, struct hci_dev *hdev, 577static int read_config_info(struct sock *sk, struct hci_dev *hdev,
@@ -548,8 +598,8 @@ static int read_config_info(struct sock *sk, struct hci_dev *hdev,
548 598
549 hci_dev_unlock(hdev); 599 hci_dev_unlock(hdev);
550 600
551 return cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0, &rp, 601 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
552 sizeof(rp)); 602 &rp, sizeof(rp));
553} 603}
554 604
555static u32 get_supported_settings(struct hci_dev *hdev) 605static u32 get_supported_settings(struct hci_dev *hdev)
@@ -582,6 +632,7 @@ static u32 get_supported_settings(struct hci_dev *hdev)
582 settings |= MGMT_SETTING_ADVERTISING; 632 settings |= MGMT_SETTING_ADVERTISING;
583 settings |= MGMT_SETTING_SECURE_CONN; 633 settings |= MGMT_SETTING_SECURE_CONN;
584 settings |= MGMT_SETTING_PRIVACY; 634 settings |= MGMT_SETTING_PRIVACY;
635 settings |= MGMT_SETTING_STATIC_ADDRESS;
585 } 636 }
586 637
587 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) || 638 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
@@ -598,45 +649,64 @@ static u32 get_current_settings(struct hci_dev *hdev)
598 if (hdev_is_powered(hdev)) 649 if (hdev_is_powered(hdev))
599 settings |= MGMT_SETTING_POWERED; 650 settings |= MGMT_SETTING_POWERED;
600 651
601 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) 652 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
602 settings |= MGMT_SETTING_CONNECTABLE; 653 settings |= MGMT_SETTING_CONNECTABLE;
603 654
604 if (test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) 655 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
605 settings |= MGMT_SETTING_FAST_CONNECTABLE; 656 settings |= MGMT_SETTING_FAST_CONNECTABLE;
606 657
607 if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) 658 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
608 settings |= MGMT_SETTING_DISCOVERABLE; 659 settings |= MGMT_SETTING_DISCOVERABLE;
609 660
610 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) 661 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
611 settings |= MGMT_SETTING_BONDABLE; 662 settings |= MGMT_SETTING_BONDABLE;
612 663
613 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 664 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
614 settings |= MGMT_SETTING_BREDR; 665 settings |= MGMT_SETTING_BREDR;
615 666
616 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 667 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
617 settings |= MGMT_SETTING_LE; 668 settings |= MGMT_SETTING_LE;
618 669
619 if (test_bit(HCI_LINK_SECURITY, &hdev->dev_flags)) 670 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
620 settings |= MGMT_SETTING_LINK_SECURITY; 671 settings |= MGMT_SETTING_LINK_SECURITY;
621 672
622 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 673 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
623 settings |= MGMT_SETTING_SSP; 674 settings |= MGMT_SETTING_SSP;
624 675
625 if (test_bit(HCI_HS_ENABLED, &hdev->dev_flags)) 676 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
626 settings |= MGMT_SETTING_HS; 677 settings |= MGMT_SETTING_HS;
627 678
628 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 679 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
629 settings |= MGMT_SETTING_ADVERTISING; 680 settings |= MGMT_SETTING_ADVERTISING;
630 681
631 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) 682 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
632 settings |= MGMT_SETTING_SECURE_CONN; 683 settings |= MGMT_SETTING_SECURE_CONN;
633 684
634 if (test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) 685 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
635 settings |= MGMT_SETTING_DEBUG_KEYS; 686 settings |= MGMT_SETTING_DEBUG_KEYS;
636 687
637 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) 688 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
638 settings |= MGMT_SETTING_PRIVACY; 689 settings |= MGMT_SETTING_PRIVACY;
639 690
691 /* The current setting for static address has two purposes. The
692 * first is to indicate if the static address will be used and
693 * the second is to indicate if it is actually set.
694 *
695 * This means if the static address is not configured, this flag
696 * will never be set. If the address is configured, then if the
697 * address is actually used decides if the flag is set or not.
698 *
699 * For single mode LE only controllers and dual-mode controllers
700 * with BR/EDR disabled, the existence of the static address will
701 * be evaluated.
702 */
703 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
704 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
705 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
706 if (bacmp(&hdev->static_addr, BDADDR_ANY))
707 settings |= MGMT_SETTING_STATIC_ADDRESS;
708 }
709
640 return settings; 710 return settings;
641} 711}
642 712
@@ -750,35 +820,19 @@ static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
750 return ptr; 820 return ptr;
751} 821}
752 822
753static struct pending_cmd *mgmt_pending_find(u16 opcode, struct hci_dev *hdev) 823static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
754{ 824{
755 struct pending_cmd *cmd; 825 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
756
757 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
758 if (cmd->opcode == opcode)
759 return cmd;
760 }
761
762 return NULL;
763} 826}
764 827
765static struct pending_cmd *mgmt_pending_find_data(u16 opcode, 828static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
766 struct hci_dev *hdev, 829 struct hci_dev *hdev,
767 const void *data) 830 const void *data)
768{ 831{
769 struct pending_cmd *cmd; 832 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
770
771 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
772 if (cmd->user_data != data)
773 continue;
774 if (cmd->opcode == opcode)
775 return cmd;
776 }
777
778 return NULL;
779} 833}
780 834
781static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr) 835static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
782{ 836{
783 u8 ad_len = 0; 837 u8 ad_len = 0;
784 size_t name_len; 838 size_t name_len;
@@ -804,21 +858,36 @@ static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
804 return ad_len; 858 return ad_len;
805} 859}
806 860
807static void update_scan_rsp_data(struct hci_request *req) 861static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
862{
863 /* TODO: Set the appropriate entries based on advertising instance flags
864 * here once flags other than 0 are supported.
865 */
866 memcpy(ptr, hdev->adv_instance.scan_rsp_data,
867 hdev->adv_instance.scan_rsp_len);
868
869 return hdev->adv_instance.scan_rsp_len;
870}
871
872static void update_scan_rsp_data_for_instance(struct hci_request *req,
873 u8 instance)
808{ 874{
809 struct hci_dev *hdev = req->hdev; 875 struct hci_dev *hdev = req->hdev;
810 struct hci_cp_le_set_scan_rsp_data cp; 876 struct hci_cp_le_set_scan_rsp_data cp;
811 u8 len; 877 u8 len;
812 878
813 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 879 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
814 return; 880 return;
815 881
816 memset(&cp, 0, sizeof(cp)); 882 memset(&cp, 0, sizeof(cp));
817 883
818 len = create_scan_rsp_data(hdev, cp.data); 884 if (instance)
885 len = create_instance_scan_rsp_data(hdev, cp.data);
886 else
887 len = create_default_scan_rsp_data(hdev, cp.data);
819 888
820 if (hdev->scan_rsp_data_len == len && 889 if (hdev->scan_rsp_data_len == len &&
821 memcmp(cp.data, hdev->scan_rsp_data, len) == 0) 890 !memcmp(cp.data, hdev->scan_rsp_data, len))
822 return; 891 return;
823 892
824 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data)); 893 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
@@ -829,14 +898,33 @@ static void update_scan_rsp_data(struct hci_request *req)
829 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp); 898 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
830} 899}
831 900
901static void update_scan_rsp_data(struct hci_request *req)
902{
903 struct hci_dev *hdev = req->hdev;
904 u8 instance;
905
906 /* The "Set Advertising" setting supersedes the "Add Advertising"
907 * setting. Here we set the scan response data based on which
908 * setting was set. When neither apply, default to the global settings,
909 * represented by instance "0".
910 */
911 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
912 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
913 instance = 0x01;
914 else
915 instance = 0x00;
916
917 update_scan_rsp_data_for_instance(req, instance);
918}
919
832static u8 get_adv_discov_flags(struct hci_dev *hdev) 920static u8 get_adv_discov_flags(struct hci_dev *hdev)
833{ 921{
834 struct pending_cmd *cmd; 922 struct mgmt_pending_cmd *cmd;
835 923
836 /* If there's a pending mgmt command the flags will not yet have 924 /* If there's a pending mgmt command the flags will not yet have
837 * their final values, so check for this first. 925 * their final values, so check for this first.
838 */ 926 */
839 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); 927 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
840 if (cmd) { 928 if (cmd) {
841 struct mgmt_mode *cp = cmd->param; 929 struct mgmt_mode *cp = cmd->param;
842 if (cp->val == 0x01) 930 if (cp->val == 0x01)
@@ -844,39 +932,131 @@ static u8 get_adv_discov_flags(struct hci_dev *hdev)
844 else if (cp->val == 0x02) 932 else if (cp->val == 0x02)
845 return LE_AD_LIMITED; 933 return LE_AD_LIMITED;
846 } else { 934 } else {
847 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) 935 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
848 return LE_AD_LIMITED; 936 return LE_AD_LIMITED;
849 else if (test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) 937 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
850 return LE_AD_GENERAL; 938 return LE_AD_GENERAL;
851 } 939 }
852 940
853 return 0; 941 return 0;
854} 942}
855 943
856static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr) 944static u8 get_current_adv_instance(struct hci_dev *hdev)
945{
946 /* The "Set Advertising" setting supersedes the "Add Advertising"
947 * setting. Here we set the advertising data based on which
948 * setting was set. When neither apply, default to the global settings,
949 * represented by instance "0".
950 */
951 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
952 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
953 return 0x01;
954
955 return 0x00;
956}
957
958static bool get_connectable(struct hci_dev *hdev)
959{
960 struct mgmt_pending_cmd *cmd;
961
962 /* If there's a pending mgmt command the flag will not yet have
963 * it's final value, so check for this first.
964 */
965 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
966 if (cmd) {
967 struct mgmt_mode *cp = cmd->param;
968
969 return cp->val;
970 }
971
972 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
973}
974
975static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
976{
977 u32 flags;
978
979 if (instance > 0x01)
980 return 0;
981
982 if (instance == 0x01)
983 return hdev->adv_instance.flags;
984
985 /* Instance 0 always manages the "Tx Power" and "Flags" fields */
986 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
987
988 /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting corresponds
989 * to the "connectable" instance flag.
990 */
991 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
992 flags |= MGMT_ADV_FLAG_CONNECTABLE;
993
994 return flags;
995}
996
997static u8 get_adv_instance_scan_rsp_len(struct hci_dev *hdev, u8 instance)
998{
999 /* Ignore instance 0 and other unsupported instances */
1000 if (instance != 0x01)
1001 return 0;
1002
1003 /* TODO: Take into account the "appearance" and "local-name" flags here.
1004 * These are currently being ignored as they are not supported.
1005 */
1006 return hdev->adv_instance.scan_rsp_len;
1007}
1008
1009static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
857{ 1010{
858 u8 ad_len = 0, flags = 0; 1011 u8 ad_len = 0, flags = 0;
1012 u32 instance_flags = get_adv_instance_flags(hdev, instance);
859 1013
860 flags |= get_adv_discov_flags(hdev); 1014 /* The Add Advertising command allows userspace to set both the general
1015 * and limited discoverable flags.
1016 */
1017 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
1018 flags |= LE_AD_GENERAL;
861 1019
862 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1020 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
863 flags |= LE_AD_NO_BREDR; 1021 flags |= LE_AD_LIMITED;
864 1022
865 if (flags) { 1023 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
866 BT_DBG("adv flags 0x%02x", flags); 1024 /* If a discovery flag wasn't provided, simply use the global
1025 * settings.
1026 */
1027 if (!flags)
1028 flags |= get_adv_discov_flags(hdev);
867 1029
868 ptr[0] = 2; 1030 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
869 ptr[1] = EIR_FLAGS; 1031 flags |= LE_AD_NO_BREDR;
870 ptr[2] = flags;
871 1032
872 ad_len += 3; 1033 /* If flags would still be empty, then there is no need to
873 ptr += 3; 1034 * include the "Flags" AD field".
1035 */
1036 if (flags) {
1037 ptr[0] = 0x02;
1038 ptr[1] = EIR_FLAGS;
1039 ptr[2] = flags;
1040
1041 ad_len += 3;
1042 ptr += 3;
1043 }
874 } 1044 }
875 1045
876 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) { 1046 if (instance) {
877 ptr[0] = 2; 1047 memcpy(ptr, hdev->adv_instance.adv_data,
1048 hdev->adv_instance.adv_data_len);
1049
1050 ad_len += hdev->adv_instance.adv_data_len;
1051 ptr += hdev->adv_instance.adv_data_len;
1052 }
1053
1054 /* Provide Tx Power only if we can provide a valid value for it */
1055 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
1056 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
1057 ptr[0] = 0x02;
878 ptr[1] = EIR_TX_POWER; 1058 ptr[1] = EIR_TX_POWER;
879 ptr[2] = (u8) hdev->adv_tx_power; 1059 ptr[2] = (u8)hdev->adv_tx_power;
880 1060
881 ad_len += 3; 1061 ad_len += 3;
882 ptr += 3; 1062 ptr += 3;
@@ -885,19 +1065,20 @@ static u8 create_adv_data(struct hci_dev *hdev, u8 *ptr)
885 return ad_len; 1065 return ad_len;
886} 1066}
887 1067
888static void update_adv_data(struct hci_request *req) 1068static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
889{ 1069{
890 struct hci_dev *hdev = req->hdev; 1070 struct hci_dev *hdev = req->hdev;
891 struct hci_cp_le_set_adv_data cp; 1071 struct hci_cp_le_set_adv_data cp;
892 u8 len; 1072 u8 len;
893 1073
894 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 1074 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
895 return; 1075 return;
896 1076
897 memset(&cp, 0, sizeof(cp)); 1077 memset(&cp, 0, sizeof(cp));
898 1078
899 len = create_adv_data(hdev, cp.data); 1079 len = create_instance_adv_data(hdev, instance, cp.data);
900 1080
1081 /* There's nothing to do if the data hasn't changed */
901 if (hdev->adv_data_len == len && 1082 if (hdev->adv_data_len == len &&
902 memcmp(cp.data, hdev->adv_data, len) == 0) 1083 memcmp(cp.data, hdev->adv_data, len) == 0)
903 return; 1084 return;
@@ -910,6 +1091,14 @@ static void update_adv_data(struct hci_request *req)
910 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp); 1091 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
911} 1092}
912 1093
1094static void update_adv_data(struct hci_request *req)
1095{
1096 struct hci_dev *hdev = req->hdev;
1097 u8 instance = get_current_adv_instance(hdev);
1098
1099 update_adv_data_for_instance(req, instance);
1100}
1101
913int mgmt_update_adv_data(struct hci_dev *hdev) 1102int mgmt_update_adv_data(struct hci_dev *hdev)
914{ 1103{
915 struct hci_request req; 1104 struct hci_request req;
@@ -979,10 +1168,10 @@ static void update_eir(struct hci_request *req)
979 if (!lmp_ext_inq_capable(hdev)) 1168 if (!lmp_ext_inq_capable(hdev))
980 return; 1169 return;
981 1170
982 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 1171 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
983 return; 1172 return;
984 1173
985 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 1174 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
986 return; 1175 return;
987 1176
988 memset(&cp, 0, sizeof(cp)); 1177 memset(&cp, 0, sizeof(cp));
@@ -1018,17 +1207,17 @@ static void update_class(struct hci_request *req)
1018 if (!hdev_is_powered(hdev)) 1207 if (!hdev_is_powered(hdev))
1019 return; 1208 return;
1020 1209
1021 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1210 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1022 return; 1211 return;
1023 1212
1024 if (test_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 1213 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
1025 return; 1214 return;
1026 1215
1027 cod[0] = hdev->minor_class; 1216 cod[0] = hdev->minor_class;
1028 cod[1] = hdev->major_class; 1217 cod[1] = hdev->major_class;
1029 cod[2] = get_service_classes(hdev); 1218 cod[2] = get_service_classes(hdev);
1030 1219
1031 if (test_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags)) 1220 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
1032 cod[1] |= 0x20; 1221 cod[1] |= 0x20;
1033 1222
1034 if (memcmp(cod, hdev->dev_class, 3) == 0) 1223 if (memcmp(cod, hdev->dev_class, 3) == 0)
@@ -1037,22 +1226,6 @@ static void update_class(struct hci_request *req)
1037 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod); 1226 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
1038} 1227}
1039 1228
1040static bool get_connectable(struct hci_dev *hdev)
1041{
1042 struct pending_cmd *cmd;
1043
1044 /* If there's a pending mgmt command the flag will not yet have
1045 * it's final value, so check for this first.
1046 */
1047 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1048 if (cmd) {
1049 struct mgmt_mode *cp = cmd->param;
1050 return cp->val;
1051 }
1052
1053 return test_bit(HCI_CONNECTABLE, &hdev->dev_flags);
1054}
1055
1056static void disable_advertising(struct hci_request *req) 1229static void disable_advertising(struct hci_request *req)
1057{ 1230{
1058 u8 enable = 0x00; 1231 u8 enable = 0x00;
@@ -1066,11 +1239,13 @@ static void enable_advertising(struct hci_request *req)
1066 struct hci_cp_le_set_adv_param cp; 1239 struct hci_cp_le_set_adv_param cp;
1067 u8 own_addr_type, enable = 0x01; 1240 u8 own_addr_type, enable = 0x01;
1068 bool connectable; 1241 bool connectable;
1242 u8 instance;
1243 u32 flags;
1069 1244
1070 if (hci_conn_num(hdev, LE_LINK) > 0) 1245 if (hci_conn_num(hdev, LE_LINK) > 0)
1071 return; 1246 return;
1072 1247
1073 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) 1248 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
1074 disable_advertising(req); 1249 disable_advertising(req);
1075 1250
1076 /* Clear the HCI_LE_ADV bit temporarily so that the 1251 /* Clear the HCI_LE_ADV bit temporarily so that the
@@ -1078,9 +1253,16 @@ static void enable_advertising(struct hci_request *req)
1078 * and write a new random address. The flag will be set back on 1253 * and write a new random address. The flag will be set back on
1079 * as soon as the SET_ADV_ENABLE HCI command completes. 1254 * as soon as the SET_ADV_ENABLE HCI command completes.
1080 */ 1255 */
1081 clear_bit(HCI_LE_ADV, &hdev->dev_flags); 1256 hci_dev_clear_flag(hdev, HCI_LE_ADV);
1257
1258 instance = get_current_adv_instance(hdev);
1259 flags = get_adv_instance_flags(hdev, instance);
1082 1260
1083 connectable = get_connectable(hdev); 1261 /* If the "connectable" instance flag was not set, then choose between
1262 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1263 */
1264 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1265 get_connectable(hdev);
1084 1266
1085 /* Set require_privacy to true only when non-connectable 1267 /* Set require_privacy to true only when non-connectable
1086 * advertising is used. In that case it is fine to use a 1268 * advertising is used. In that case it is fine to use a
@@ -1092,7 +1274,14 @@ static void enable_advertising(struct hci_request *req)
1092 memset(&cp, 0, sizeof(cp)); 1274 memset(&cp, 0, sizeof(cp));
1093 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval); 1275 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1094 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval); 1276 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
1095 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND; 1277
1278 if (connectable)
1279 cp.type = LE_ADV_IND;
1280 else if (get_adv_instance_scan_rsp_len(hdev, instance))
1281 cp.type = LE_ADV_SCAN_IND;
1282 else
1283 cp.type = LE_ADV_NONCONN_IND;
1284
1096 cp.own_address_type = own_addr_type; 1285 cp.own_address_type = own_addr_type;
1097 cp.channel_map = hdev->le_adv_channel_map; 1286 cp.channel_map = hdev->le_adv_channel_map;
1098 1287
@@ -1107,7 +1296,7 @@ static void service_cache_off(struct work_struct *work)
1107 service_cache.work); 1296 service_cache.work);
1108 struct hci_request req; 1297 struct hci_request req;
1109 1298
1110 if (!test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) 1299 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
1111 return; 1300 return;
1112 1301
1113 hci_req_init(&req, hdev); 1302 hci_req_init(&req, hdev);
@@ -1130,9 +1319,9 @@ static void rpa_expired(struct work_struct *work)
1130 1319
1131 BT_DBG(""); 1320 BT_DBG("");
1132 1321
1133 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 1322 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1134 1323
1135 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 1324 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
1136 return; 1325 return;
1137 1326
1138 /* The generation of a new RPA and programming it into the 1327 /* The generation of a new RPA and programming it into the
@@ -1145,7 +1334,7 @@ static void rpa_expired(struct work_struct *work)
1145 1334
1146static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev) 1335static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
1147{ 1336{
1148 if (test_and_set_bit(HCI_MGMT, &hdev->dev_flags)) 1337 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
1149 return; 1338 return;
1150 1339
1151 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off); 1340 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
@@ -1156,7 +1345,7 @@ static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
1156 * for mgmt we require user-space to explicitly enable 1345 * for mgmt we require user-space to explicitly enable
1157 * it 1346 * it
1158 */ 1347 */
1159 clear_bit(HCI_BONDABLE, &hdev->dev_flags); 1348 hci_dev_clear_flag(hdev, HCI_BONDABLE);
1160} 1349}
1161 1350
1162static int read_controller_info(struct sock *sk, struct hci_dev *hdev, 1351static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
@@ -1185,73 +1374,16 @@ static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
1185 1374
1186 hci_dev_unlock(hdev); 1375 hci_dev_unlock(hdev);
1187 1376
1188 return cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp, 1377 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1189 sizeof(rp)); 1378 sizeof(rp));
1190}
1191
1192static void mgmt_pending_free(struct pending_cmd *cmd)
1193{
1194 sock_put(cmd->sk);
1195 kfree(cmd->param);
1196 kfree(cmd);
1197}
1198
1199static struct pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
1200 struct hci_dev *hdev, void *data,
1201 u16 len)
1202{
1203 struct pending_cmd *cmd;
1204
1205 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1206 if (!cmd)
1207 return NULL;
1208
1209 cmd->opcode = opcode;
1210 cmd->index = hdev->id;
1211
1212 cmd->param = kmemdup(data, len, GFP_KERNEL);
1213 if (!cmd->param) {
1214 kfree(cmd);
1215 return NULL;
1216 }
1217
1218 cmd->param_len = len;
1219
1220 cmd->sk = sk;
1221 sock_hold(sk);
1222
1223 list_add(&cmd->list, &hdev->mgmt_pending);
1224
1225 return cmd;
1226}
1227
1228static void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
1229 void (*cb)(struct pending_cmd *cmd,
1230 void *data),
1231 void *data)
1232{
1233 struct pending_cmd *cmd, *tmp;
1234
1235 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
1236 if (opcode > 0 && cmd->opcode != opcode)
1237 continue;
1238
1239 cb(cmd, data);
1240 }
1241}
1242
1243static void mgmt_pending_remove(struct pending_cmd *cmd)
1244{
1245 list_del(&cmd->list);
1246 mgmt_pending_free(cmd);
1247} 1379}
1248 1380
1249static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev) 1381static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
1250{ 1382{
1251 __le32 settings = cpu_to_le32(get_current_settings(hdev)); 1383 __le32 settings = cpu_to_le32(get_current_settings(hdev));
1252 1384
1253 return cmd_complete(sk, hdev->id, opcode, 0, &settings, 1385 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1254 sizeof(settings)); 1386 sizeof(settings));
1255} 1387}
1256 1388
1257static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode) 1389static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
@@ -1272,9 +1404,10 @@ static bool hci_stop_discovery(struct hci_request *req)
1272 1404
1273 switch (hdev->discovery.state) { 1405 switch (hdev->discovery.state) {
1274 case DISCOVERY_FINDING: 1406 case DISCOVERY_FINDING:
1275 if (test_bit(HCI_INQUIRY, &hdev->flags)) { 1407 if (test_bit(HCI_INQUIRY, &hdev->flags))
1276 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL); 1408 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
1277 } else { 1409
1410 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
1278 cancel_delayed_work(&hdev->le_scan_disable); 1411 cancel_delayed_work(&hdev->le_scan_disable);
1279 hci_req_add_le_scan_disable(req); 1412 hci_req_add_le_scan_disable(req);
1280 } 1413 }
@@ -1295,7 +1428,7 @@ static bool hci_stop_discovery(struct hci_request *req)
1295 1428
1296 default: 1429 default:
1297 /* Passive scanning */ 1430 /* Passive scanning */
1298 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags)) { 1431 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
1299 hci_req_add_le_scan_disable(req); 1432 hci_req_add_le_scan_disable(req);
1300 return true; 1433 return true;
1301 } 1434 }
@@ -1306,6 +1439,49 @@ static bool hci_stop_discovery(struct hci_request *req)
1306 return false; 1439 return false;
1307} 1440}
1308 1441
1442static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1443 u8 instance)
1444{
1445 struct mgmt_ev_advertising_added ev;
1446
1447 ev.instance = instance;
1448
1449 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1450}
1451
1452static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1453 u8 instance)
1454{
1455 struct mgmt_ev_advertising_removed ev;
1456
1457 ev.instance = instance;
1458
1459 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1460}
1461
1462static void clear_adv_instance(struct hci_dev *hdev)
1463{
1464 struct hci_request req;
1465
1466 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1467 return;
1468
1469 if (hdev->adv_instance.timeout)
1470 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
1471
1472 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
1473 advertising_removed(NULL, hdev, 1);
1474 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1475
1476 if (!hdev_is_powered(hdev) ||
1477 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1478 return;
1479
1480 hci_req_init(&req, hdev);
1481 disable_advertising(&req);
1482 hci_req_run(&req, NULL);
1483}
1484
1309static int clean_up_hci_state(struct hci_dev *hdev) 1485static int clean_up_hci_state(struct hci_dev *hdev)
1310{ 1486{
1311 struct hci_request req; 1487 struct hci_request req;
@@ -1321,7 +1497,10 @@ static int clean_up_hci_state(struct hci_dev *hdev)
1321 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); 1497 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1322 } 1498 }
1323 1499
1324 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) 1500 if (hdev->adv_instance.timeout)
1501 clear_adv_instance(hdev);
1502
1503 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
1325 disable_advertising(&req); 1504 disable_advertising(&req);
1326 1505
1327 discov_stopped = hci_stop_discovery(&req); 1506 discov_stopped = hci_stop_discovery(&req);
@@ -1369,24 +1548,24 @@ static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
1369 u16 len) 1548 u16 len)
1370{ 1549{
1371 struct mgmt_mode *cp = data; 1550 struct mgmt_mode *cp = data;
1372 struct pending_cmd *cmd; 1551 struct mgmt_pending_cmd *cmd;
1373 int err; 1552 int err;
1374 1553
1375 BT_DBG("request for %s", hdev->name); 1554 BT_DBG("request for %s", hdev->name);
1376 1555
1377 if (cp->val != 0x00 && cp->val != 0x01) 1556 if (cp->val != 0x00 && cp->val != 0x01)
1378 return cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, 1557 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1379 MGMT_STATUS_INVALID_PARAMS); 1558 MGMT_STATUS_INVALID_PARAMS);
1380 1559
1381 hci_dev_lock(hdev); 1560 hci_dev_lock(hdev);
1382 1561
1383 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) { 1562 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
1384 err = cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED, 1563 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1385 MGMT_STATUS_BUSY); 1564 MGMT_STATUS_BUSY);
1386 goto failed; 1565 goto failed;
1387 } 1566 }
1388 1567
1389 if (test_and_clear_bit(HCI_AUTO_OFF, &hdev->dev_flags)) { 1568 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
1390 cancel_delayed_work(&hdev->power_off); 1569 cancel_delayed_work(&hdev->power_off);
1391 1570
1392 if (cp->val) { 1571 if (cp->val) {
@@ -1433,11 +1612,10 @@ failed:
1433 1612
1434static int new_settings(struct hci_dev *hdev, struct sock *skip) 1613static int new_settings(struct hci_dev *hdev, struct sock *skip)
1435{ 1614{
1436 __le32 ev; 1615 __le32 ev = cpu_to_le32(get_current_settings(hdev));
1437
1438 ev = cpu_to_le32(get_current_settings(hdev));
1439 1616
1440 return mgmt_event(MGMT_EV_NEW_SETTINGS, hdev, &ev, sizeof(ev), skip); 1617 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1618 sizeof(ev), skip);
1441} 1619}
1442 1620
1443int mgmt_new_settings(struct hci_dev *hdev) 1621int mgmt_new_settings(struct hci_dev *hdev)
@@ -1451,7 +1629,7 @@ struct cmd_lookup {
1451 u8 mgmt_status; 1629 u8 mgmt_status;
1452}; 1630};
1453 1631
1454static void settings_rsp(struct pending_cmd *cmd, void *data) 1632static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
1455{ 1633{
1456 struct cmd_lookup *match = data; 1634 struct cmd_lookup *match = data;
1457 1635
@@ -1467,15 +1645,15 @@ static void settings_rsp(struct pending_cmd *cmd, void *data)
1467 mgmt_pending_free(cmd); 1645 mgmt_pending_free(cmd);
1468} 1646}
1469 1647
1470static void cmd_status_rsp(struct pending_cmd *cmd, void *data) 1648static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
1471{ 1649{
1472 u8 *status = data; 1650 u8 *status = data;
1473 1651
1474 cmd_status(cmd->sk, cmd->index, cmd->opcode, *status); 1652 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1475 mgmt_pending_remove(cmd); 1653 mgmt_pending_remove(cmd);
1476} 1654}
1477 1655
1478static void cmd_complete_rsp(struct pending_cmd *cmd, void *data) 1656static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
1479{ 1657{
1480 if (cmd->cmd_complete) { 1658 if (cmd->cmd_complete) {
1481 u8 *status = data; 1659 u8 *status = data;
@@ -1489,23 +1667,23 @@ static void cmd_complete_rsp(struct pending_cmd *cmd, void *data)
1489 cmd_status_rsp(cmd, data); 1667 cmd_status_rsp(cmd, data);
1490} 1668}
1491 1669
1492static int generic_cmd_complete(struct pending_cmd *cmd, u8 status) 1670static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1493{ 1671{
1494 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, 1672 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1495 cmd->param, cmd->param_len); 1673 cmd->param, cmd->param_len);
1496} 1674}
1497 1675
1498static int addr_cmd_complete(struct pending_cmd *cmd, u8 status) 1676static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1499{ 1677{
1500 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, cmd->param, 1678 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1501 sizeof(struct mgmt_addr_info)); 1679 cmd->param, sizeof(struct mgmt_addr_info));
1502} 1680}
1503 1681
1504static u8 mgmt_bredr_support(struct hci_dev *hdev) 1682static u8 mgmt_bredr_support(struct hci_dev *hdev)
1505{ 1683{
1506 if (!lmp_bredr_capable(hdev)) 1684 if (!lmp_bredr_capable(hdev))
1507 return MGMT_STATUS_NOT_SUPPORTED; 1685 return MGMT_STATUS_NOT_SUPPORTED;
1508 else if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1686 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1509 return MGMT_STATUS_REJECTED; 1687 return MGMT_STATUS_REJECTED;
1510 else 1688 else
1511 return MGMT_STATUS_SUCCESS; 1689 return MGMT_STATUS_SUCCESS;
@@ -1515,7 +1693,7 @@ static u8 mgmt_le_support(struct hci_dev *hdev)
1515{ 1693{
1516 if (!lmp_le_capable(hdev)) 1694 if (!lmp_le_capable(hdev))
1517 return MGMT_STATUS_NOT_SUPPORTED; 1695 return MGMT_STATUS_NOT_SUPPORTED;
1518 else if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 1696 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1519 return MGMT_STATUS_REJECTED; 1697 return MGMT_STATUS_REJECTED;
1520 else 1698 else
1521 return MGMT_STATUS_SUCCESS; 1699 return MGMT_STATUS_SUCCESS;
@@ -1524,7 +1702,7 @@ static u8 mgmt_le_support(struct hci_dev *hdev)
1524static void set_discoverable_complete(struct hci_dev *hdev, u8 status, 1702static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1525 u16 opcode) 1703 u16 opcode)
1526{ 1704{
1527 struct pending_cmd *cmd; 1705 struct mgmt_pending_cmd *cmd;
1528 struct mgmt_mode *cp; 1706 struct mgmt_mode *cp;
1529 struct hci_request req; 1707 struct hci_request req;
1530 bool changed; 1708 bool changed;
@@ -1533,21 +1711,20 @@ static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1533 1711
1534 hci_dev_lock(hdev); 1712 hci_dev_lock(hdev);
1535 1713
1536 cmd = mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev); 1714 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
1537 if (!cmd) 1715 if (!cmd)
1538 goto unlock; 1716 goto unlock;
1539 1717
1540 if (status) { 1718 if (status) {
1541 u8 mgmt_err = mgmt_status(status); 1719 u8 mgmt_err = mgmt_status(status);
1542 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); 1720 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1543 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1721 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1544 goto remove_cmd; 1722 goto remove_cmd;
1545 } 1723 }
1546 1724
1547 cp = cmd->param; 1725 cp = cmd->param;
1548 if (cp->val) { 1726 if (cp->val) {
1549 changed = !test_and_set_bit(HCI_DISCOVERABLE, 1727 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
1550 &hdev->dev_flags);
1551 1728
1552 if (hdev->discov_timeout > 0) { 1729 if (hdev->discov_timeout > 0) {
1553 int to = msecs_to_jiffies(hdev->discov_timeout * 1000); 1730 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
@@ -1555,8 +1732,7 @@ static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1555 to); 1732 to);
1556 } 1733 }
1557 } else { 1734 } else {
1558 changed = test_and_clear_bit(HCI_DISCOVERABLE, 1735 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
1559 &hdev->dev_flags);
1560 } 1736 }
1561 1737
1562 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev); 1738 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
@@ -1585,7 +1761,7 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1585 u16 len) 1761 u16 len)
1586{ 1762{
1587 struct mgmt_cp_set_discoverable *cp = data; 1763 struct mgmt_cp_set_discoverable *cp = data;
1588 struct pending_cmd *cmd; 1764 struct mgmt_pending_cmd *cmd;
1589 struct hci_request req; 1765 struct hci_request req;
1590 u16 timeout; 1766 u16 timeout;
1591 u8 scan; 1767 u8 scan;
@@ -1593,14 +1769,14 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1593 1769
1594 BT_DBG("request for %s", hdev->name); 1770 BT_DBG("request for %s", hdev->name);
1595 1771
1596 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && 1772 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1597 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1773 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1598 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1774 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1599 MGMT_STATUS_REJECTED); 1775 MGMT_STATUS_REJECTED);
1600 1776
1601 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 1777 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
1602 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1778 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1603 MGMT_STATUS_INVALID_PARAMS); 1779 MGMT_STATUS_INVALID_PARAMS);
1604 1780
1605 timeout = __le16_to_cpu(cp->timeout); 1781 timeout = __le16_to_cpu(cp->timeout);
1606 1782
@@ -1609,27 +1785,27 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1609 */ 1785 */
1610 if ((cp->val == 0x00 && timeout > 0) || 1786 if ((cp->val == 0x00 && timeout > 0) ||
1611 (cp->val == 0x02 && timeout == 0)) 1787 (cp->val == 0x02 && timeout == 0))
1612 return cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1788 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1613 MGMT_STATUS_INVALID_PARAMS); 1789 MGMT_STATUS_INVALID_PARAMS);
1614 1790
1615 hci_dev_lock(hdev); 1791 hci_dev_lock(hdev);
1616 1792
1617 if (!hdev_is_powered(hdev) && timeout > 0) { 1793 if (!hdev_is_powered(hdev) && timeout > 0) {
1618 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1794 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1619 MGMT_STATUS_NOT_POWERED); 1795 MGMT_STATUS_NOT_POWERED);
1620 goto failed; 1796 goto failed;
1621 } 1797 }
1622 1798
1623 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || 1799 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1624 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { 1800 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1625 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1801 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1626 MGMT_STATUS_BUSY); 1802 MGMT_STATUS_BUSY);
1627 goto failed; 1803 goto failed;
1628 } 1804 }
1629 1805
1630 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) { 1806 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
1631 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE, 1807 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1632 MGMT_STATUS_REJECTED); 1808 MGMT_STATUS_REJECTED);
1633 goto failed; 1809 goto failed;
1634 } 1810 }
1635 1811
@@ -1640,8 +1816,8 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1640 * not a valid operation since it requires a timeout 1816 * not a valid operation since it requires a timeout
1641 * and so no need to check HCI_LIMITED_DISCOVERABLE. 1817 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1642 */ 1818 */
1643 if (!!cp->val != test_bit(HCI_DISCOVERABLE, &hdev->dev_flags)) { 1819 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
1644 change_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 1820 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
1645 changed = true; 1821 changed = true;
1646 } 1822 }
1647 1823
@@ -1659,9 +1835,9 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1659 * value with the new value. And if only the timeout gets updated, 1835 * value with the new value. And if only the timeout gets updated,
1660 * then no need for any HCI transactions. 1836 * then no need for any HCI transactions.
1661 */ 1837 */
1662 if (!!cp->val == test_bit(HCI_DISCOVERABLE, &hdev->dev_flags) && 1838 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1663 (cp->val == 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE, 1839 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1664 &hdev->dev_flags)) { 1840 HCI_LIMITED_DISCOVERABLE)) {
1665 cancel_delayed_work(&hdev->discov_off); 1841 cancel_delayed_work(&hdev->discov_off);
1666 hdev->discov_timeout = timeout; 1842 hdev->discov_timeout = timeout;
1667 1843
@@ -1690,16 +1866,16 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1690 1866
1691 /* Limited discoverable mode */ 1867 /* Limited discoverable mode */
1692 if (cp->val == 0x02) 1868 if (cp->val == 0x02)
1693 set_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1869 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1694 else 1870 else
1695 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1871 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1696 1872
1697 hci_req_init(&req, hdev); 1873 hci_req_init(&req, hdev);
1698 1874
1699 /* The procedure for LE-only controllers is much simpler - just 1875 /* The procedure for LE-only controllers is much simpler - just
1700 * update the advertising data. 1876 * update the advertising data.
1701 */ 1877 */
1702 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1878 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1703 goto update_ad; 1879 goto update_ad;
1704 1880
1705 scan = SCAN_PAGE; 1881 scan = SCAN_PAGE;
@@ -1729,7 +1905,7 @@ static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1729 1905
1730 scan |= SCAN_INQUIRY; 1906 scan |= SCAN_INQUIRY;
1731 } else { 1907 } else {
1732 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 1908 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1733 } 1909 }
1734 1910
1735 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan); 1911 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
@@ -1752,7 +1928,7 @@ static void write_fast_connectable(struct hci_request *req, bool enable)
1752 struct hci_cp_write_page_scan_activity acp; 1928 struct hci_cp_write_page_scan_activity acp;
1753 u8 type; 1929 u8 type;
1754 1930
1755 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 1931 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1756 return; 1932 return;
1757 1933
1758 if (hdev->hci_ver < BLUETOOTH_VER_1_2) 1934 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
@@ -1784,7 +1960,7 @@ static void write_fast_connectable(struct hci_request *req, bool enable)
1784static void set_connectable_complete(struct hci_dev *hdev, u8 status, 1960static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1785 u16 opcode) 1961 u16 opcode)
1786{ 1962{
1787 struct pending_cmd *cmd; 1963 struct mgmt_pending_cmd *cmd;
1788 struct mgmt_mode *cp; 1964 struct mgmt_mode *cp;
1789 bool conn_changed, discov_changed; 1965 bool conn_changed, discov_changed;
1790 1966
@@ -1792,26 +1968,26 @@ static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1792 1968
1793 hci_dev_lock(hdev); 1969 hci_dev_lock(hdev);
1794 1970
1795 cmd = mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev); 1971 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
1796 if (!cmd) 1972 if (!cmd)
1797 goto unlock; 1973 goto unlock;
1798 1974
1799 if (status) { 1975 if (status) {
1800 u8 mgmt_err = mgmt_status(status); 1976 u8 mgmt_err = mgmt_status(status);
1801 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); 1977 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1802 goto remove_cmd; 1978 goto remove_cmd;
1803 } 1979 }
1804 1980
1805 cp = cmd->param; 1981 cp = cmd->param;
1806 if (cp->val) { 1982 if (cp->val) {
1807 conn_changed = !test_and_set_bit(HCI_CONNECTABLE, 1983 conn_changed = !hci_dev_test_and_set_flag(hdev,
1808 &hdev->dev_flags); 1984 HCI_CONNECTABLE);
1809 discov_changed = false; 1985 discov_changed = false;
1810 } else { 1986 } else {
1811 conn_changed = test_and_clear_bit(HCI_CONNECTABLE, 1987 conn_changed = hci_dev_test_and_clear_flag(hdev,
1812 &hdev->dev_flags); 1988 HCI_CONNECTABLE);
1813 discov_changed = test_and_clear_bit(HCI_DISCOVERABLE, 1989 discov_changed = hci_dev_test_and_clear_flag(hdev,
1814 &hdev->dev_flags); 1990 HCI_DISCOVERABLE);
1815 } 1991 }
1816 1992
1817 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev); 1993 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
@@ -1837,14 +2013,14 @@ static int set_connectable_update_settings(struct hci_dev *hdev,
1837 bool changed = false; 2013 bool changed = false;
1838 int err; 2014 int err;
1839 2015
1840 if (!!val != test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) 2016 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
1841 changed = true; 2017 changed = true;
1842 2018
1843 if (val) { 2019 if (val) {
1844 set_bit(HCI_CONNECTABLE, &hdev->dev_flags); 2020 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1845 } else { 2021 } else {
1846 clear_bit(HCI_CONNECTABLE, &hdev->dev_flags); 2022 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1847 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 2023 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1848 } 2024 }
1849 2025
1850 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev); 2026 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
@@ -1864,21 +2040,21 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1864 u16 len) 2040 u16 len)
1865{ 2041{
1866 struct mgmt_mode *cp = data; 2042 struct mgmt_mode *cp = data;
1867 struct pending_cmd *cmd; 2043 struct mgmt_pending_cmd *cmd;
1868 struct hci_request req; 2044 struct hci_request req;
1869 u8 scan; 2045 u8 scan;
1870 int err; 2046 int err;
1871 2047
1872 BT_DBG("request for %s", hdev->name); 2048 BT_DBG("request for %s", hdev->name);
1873 2049
1874 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && 2050 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1875 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 2051 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1876 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 2052 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1877 MGMT_STATUS_REJECTED); 2053 MGMT_STATUS_REJECTED);
1878 2054
1879 if (cp->val != 0x00 && cp->val != 0x01) 2055 if (cp->val != 0x00 && cp->val != 0x01)
1880 return cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 2056 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1881 MGMT_STATUS_INVALID_PARAMS); 2057 MGMT_STATUS_INVALID_PARAMS);
1882 2058
1883 hci_dev_lock(hdev); 2059 hci_dev_lock(hdev);
1884 2060
@@ -1887,10 +2063,10 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1887 goto failed; 2063 goto failed;
1888 } 2064 }
1889 2065
1890 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) || 2066 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1891 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) { 2067 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1892 err = cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE, 2068 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1893 MGMT_STATUS_BUSY); 2069 MGMT_STATUS_BUSY);
1894 goto failed; 2070 goto failed;
1895 } 2071 }
1896 2072
@@ -1906,10 +2082,10 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1906 * by-product of disabling connectable, we need to update the 2082 * by-product of disabling connectable, we need to update the
1907 * advertising flags. 2083 * advertising flags.
1908 */ 2084 */
1909 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 2085 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
1910 if (!cp->val) { 2086 if (!cp->val) {
1911 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 2087 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1912 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 2088 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1913 } 2089 }
1914 update_adv_data(&req); 2090 update_adv_data(&req);
1915 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) { 2091 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
@@ -1938,17 +2114,9 @@ static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1938 } 2114 }
1939 2115
1940no_scan_update: 2116no_scan_update:
1941 /* If we're going from non-connectable to connectable or
1942 * vice-versa when fast connectable is enabled ensure that fast
1943 * connectable gets disabled. write_fast_connectable won't do
1944 * anything if the page scan parameters are already what they
1945 * should be.
1946 */
1947 if (cp->val || test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags))
1948 write_fast_connectable(&req, false);
1949
1950 /* Update the advertising parameters if necessary */ 2117 /* Update the advertising parameters if necessary */
1951 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 2118 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2119 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1952 enable_advertising(&req); 2120 enable_advertising(&req);
1953 2121
1954 err = hci_req_run(&req, set_connectable_complete); 2122 err = hci_req_run(&req, set_connectable_complete);
@@ -1975,15 +2143,15 @@ static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
1975 BT_DBG("request for %s", hdev->name); 2143 BT_DBG("request for %s", hdev->name);
1976 2144
1977 if (cp->val != 0x00 && cp->val != 0x01) 2145 if (cp->val != 0x00 && cp->val != 0x01)
1978 return cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE, 2146 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1979 MGMT_STATUS_INVALID_PARAMS); 2147 MGMT_STATUS_INVALID_PARAMS);
1980 2148
1981 hci_dev_lock(hdev); 2149 hci_dev_lock(hdev);
1982 2150
1983 if (cp->val) 2151 if (cp->val)
1984 changed = !test_and_set_bit(HCI_BONDABLE, &hdev->dev_flags); 2152 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
1985 else 2153 else
1986 changed = test_and_clear_bit(HCI_BONDABLE, &hdev->dev_flags); 2154 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
1987 2155
1988 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev); 2156 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
1989 if (err < 0) 2157 if (err < 0)
@@ -2001,7 +2169,7 @@ static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2001 u16 len) 2169 u16 len)
2002{ 2170{
2003 struct mgmt_mode *cp = data; 2171 struct mgmt_mode *cp = data;
2004 struct pending_cmd *cmd; 2172 struct mgmt_pending_cmd *cmd;
2005 u8 val, status; 2173 u8 val, status;
2006 int err; 2174 int err;
2007 2175
@@ -2009,21 +2177,20 @@ static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2009 2177
2010 status = mgmt_bredr_support(hdev); 2178 status = mgmt_bredr_support(hdev);
2011 if (status) 2179 if (status)
2012 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 2180 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2013 status); 2181 status);
2014 2182
2015 if (cp->val != 0x00 && cp->val != 0x01) 2183 if (cp->val != 0x00 && cp->val != 0x01)
2016 return cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 2184 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2017 MGMT_STATUS_INVALID_PARAMS); 2185 MGMT_STATUS_INVALID_PARAMS);
2018 2186
2019 hci_dev_lock(hdev); 2187 hci_dev_lock(hdev);
2020 2188
2021 if (!hdev_is_powered(hdev)) { 2189 if (!hdev_is_powered(hdev)) {
2022 bool changed = false; 2190 bool changed = false;
2023 2191
2024 if (!!cp->val != test_bit(HCI_LINK_SECURITY, 2192 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
2025 &hdev->dev_flags)) { 2193 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
2026 change_bit(HCI_LINK_SECURITY, &hdev->dev_flags);
2027 changed = true; 2194 changed = true;
2028 } 2195 }
2029 2196
@@ -2037,9 +2204,9 @@ static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2037 goto failed; 2204 goto failed;
2038 } 2205 }
2039 2206
2040 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) { 2207 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
2041 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY, 2208 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2042 MGMT_STATUS_BUSY); 2209 MGMT_STATUS_BUSY);
2043 goto failed; 2210 goto failed;
2044 } 2211 }
2045 2212
@@ -2070,7 +2237,7 @@ failed:
2070static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2237static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2071{ 2238{
2072 struct mgmt_mode *cp = data; 2239 struct mgmt_mode *cp = data;
2073 struct pending_cmd *cmd; 2240 struct mgmt_pending_cmd *cmd;
2074 u8 status; 2241 u8 status;
2075 int err; 2242 int err;
2076 2243
@@ -2078,15 +2245,15 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2078 2245
2079 status = mgmt_bredr_support(hdev); 2246 status = mgmt_bredr_support(hdev);
2080 if (status) 2247 if (status)
2081 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status); 2248 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
2082 2249
2083 if (!lmp_ssp_capable(hdev)) 2250 if (!lmp_ssp_capable(hdev))
2084 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2251 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2085 MGMT_STATUS_NOT_SUPPORTED); 2252 MGMT_STATUS_NOT_SUPPORTED);
2086 2253
2087 if (cp->val != 0x00 && cp->val != 0x01) 2254 if (cp->val != 0x00 && cp->val != 0x01)
2088 return cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2255 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2089 MGMT_STATUS_INVALID_PARAMS); 2256 MGMT_STATUS_INVALID_PARAMS);
2090 2257
2091 hci_dev_lock(hdev); 2258 hci_dev_lock(hdev);
2092 2259
@@ -2094,16 +2261,16 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2094 bool changed; 2261 bool changed;
2095 2262
2096 if (cp->val) { 2263 if (cp->val) {
2097 changed = !test_and_set_bit(HCI_SSP_ENABLED, 2264 changed = !hci_dev_test_and_set_flag(hdev,
2098 &hdev->dev_flags); 2265 HCI_SSP_ENABLED);
2099 } else { 2266 } else {
2100 changed = test_and_clear_bit(HCI_SSP_ENABLED, 2267 changed = hci_dev_test_and_clear_flag(hdev,
2101 &hdev->dev_flags); 2268 HCI_SSP_ENABLED);
2102 if (!changed) 2269 if (!changed)
2103 changed = test_and_clear_bit(HCI_HS_ENABLED, 2270 changed = hci_dev_test_and_clear_flag(hdev,
2104 &hdev->dev_flags); 2271 HCI_HS_ENABLED);
2105 else 2272 else
2106 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 2273 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
2107 } 2274 }
2108 2275
2109 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 2276 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
@@ -2116,14 +2283,13 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2116 goto failed; 2283 goto failed;
2117 } 2284 }
2118 2285
2119 if (mgmt_pending_find(MGMT_OP_SET_SSP, hdev) || 2286 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
2120 mgmt_pending_find(MGMT_OP_SET_HS, hdev)) { 2287 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2121 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, 2288 MGMT_STATUS_BUSY);
2122 MGMT_STATUS_BUSY);
2123 goto failed; 2289 goto failed;
2124 } 2290 }
2125 2291
2126 if (!!cp->val == test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 2292 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
2127 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev); 2293 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2128 goto failed; 2294 goto failed;
2129 } 2295 }
@@ -2134,7 +2300,7 @@ static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2134 goto failed; 2300 goto failed;
2135 } 2301 }
2136 2302
2137 if (!cp->val && test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) 2303 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
2138 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, 2304 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2139 sizeof(cp->val), &cp->val); 2305 sizeof(cp->val), &cp->val);
2140 2306
@@ -2160,32 +2326,38 @@ static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2160 2326
2161 status = mgmt_bredr_support(hdev); 2327 status = mgmt_bredr_support(hdev);
2162 if (status) 2328 if (status)
2163 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status); 2329 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
2164 2330
2165 if (!lmp_ssp_capable(hdev)) 2331 if (!lmp_ssp_capable(hdev))
2166 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 2332 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2167 MGMT_STATUS_NOT_SUPPORTED); 2333 MGMT_STATUS_NOT_SUPPORTED);
2168 2334
2169 if (!test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 2335 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2170 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 2336 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2171 MGMT_STATUS_REJECTED); 2337 MGMT_STATUS_REJECTED);
2172 2338
2173 if (cp->val != 0x00 && cp->val != 0x01) 2339 if (cp->val != 0x00 && cp->val != 0x01)
2174 return cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 2340 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2175 MGMT_STATUS_INVALID_PARAMS); 2341 MGMT_STATUS_INVALID_PARAMS);
2176 2342
2177 hci_dev_lock(hdev); 2343 hci_dev_lock(hdev);
2178 2344
2345 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
2346 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2347 MGMT_STATUS_BUSY);
2348 goto unlock;
2349 }
2350
2179 if (cp->val) { 2351 if (cp->val) {
2180 changed = !test_and_set_bit(HCI_HS_ENABLED, &hdev->dev_flags); 2352 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
2181 } else { 2353 } else {
2182 if (hdev_is_powered(hdev)) { 2354 if (hdev_is_powered(hdev)) {
2183 err = cmd_status(sk, hdev->id, MGMT_OP_SET_HS, 2355 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2184 MGMT_STATUS_REJECTED); 2356 MGMT_STATUS_REJECTED);
2185 goto unlock; 2357 goto unlock;
2186 } 2358 }
2187 2359
2188 changed = test_and_clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 2360 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
2189 } 2361 }
2190 2362
2191 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev); 2363 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
@@ -2226,7 +2398,7 @@ static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2226 * has actually been enabled. During power on, the 2398 * has actually been enabled. During power on, the
2227 * update in powered_update_hci will take care of it. 2399 * update in powered_update_hci will take care of it.
2228 */ 2400 */
2229 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 2401 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2230 struct hci_request req; 2402 struct hci_request req;
2231 2403
2232 hci_req_init(&req, hdev); 2404 hci_req_init(&req, hdev);
@@ -2244,7 +2416,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2244{ 2416{
2245 struct mgmt_mode *cp = data; 2417 struct mgmt_mode *cp = data;
2246 struct hci_cp_write_le_host_supported hci_cp; 2418 struct hci_cp_write_le_host_supported hci_cp;
2247 struct pending_cmd *cmd; 2419 struct mgmt_pending_cmd *cmd;
2248 struct hci_request req; 2420 struct hci_request req;
2249 int err; 2421 int err;
2250 u8 val, enabled; 2422 u8 val, enabled;
@@ -2252,17 +2424,29 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2252 BT_DBG("request for %s", hdev->name); 2424 BT_DBG("request for %s", hdev->name);
2253 2425
2254 if (!lmp_le_capable(hdev)) 2426 if (!lmp_le_capable(hdev))
2255 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2427 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2256 MGMT_STATUS_NOT_SUPPORTED); 2428 MGMT_STATUS_NOT_SUPPORTED);
2257 2429
2258 if (cp->val != 0x00 && cp->val != 0x01) 2430 if (cp->val != 0x00 && cp->val != 0x01)
2259 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2431 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2260 MGMT_STATUS_INVALID_PARAMS); 2432 MGMT_STATUS_INVALID_PARAMS);
2433
2434 /* Bluetooth single mode LE only controllers or dual-mode
2435 * controllers configured as LE only devices, do not allow
2436 * switching LE off. These have either LE enabled explicitly
2437 * or BR/EDR has been previously switched off.
2438 *
2439 * When trying to enable an already enabled LE, then gracefully
2440 * send a positive response. Trying to disable it however will
2441 * result into rejection.
2442 */
2443 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2444 if (cp->val == 0x01)
2445 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2261 2446
2262 /* LE-only devices do not allow toggling LE on/off */ 2447 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2263 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) 2448 MGMT_STATUS_REJECTED);
2264 return cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2449 }
2265 MGMT_STATUS_REJECTED);
2266 2450
2267 hci_dev_lock(hdev); 2451 hci_dev_lock(hdev);
2268 2452
@@ -2272,13 +2456,13 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2272 if (!hdev_is_powered(hdev) || val == enabled) { 2456 if (!hdev_is_powered(hdev) || val == enabled) {
2273 bool changed = false; 2457 bool changed = false;
2274 2458
2275 if (val != test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 2459 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2276 change_bit(HCI_LE_ENABLED, &hdev->dev_flags); 2460 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
2277 changed = true; 2461 changed = true;
2278 } 2462 }
2279 2463
2280 if (!val && test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { 2464 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
2281 clear_bit(HCI_ADVERTISING, &hdev->dev_flags); 2465 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
2282 changed = true; 2466 changed = true;
2283 } 2467 }
2284 2468
@@ -2292,10 +2476,10 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2292 goto unlock; 2476 goto unlock;
2293 } 2477 }
2294 2478
2295 if (mgmt_pending_find(MGMT_OP_SET_LE, hdev) || 2479 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2296 mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev)) { 2480 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
2297 err = cmd_status(sk, hdev->id, MGMT_OP_SET_LE, 2481 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2298 MGMT_STATUS_BUSY); 2482 MGMT_STATUS_BUSY);
2299 goto unlock; 2483 goto unlock;
2300 } 2484 }
2301 2485
@@ -2313,7 +2497,7 @@ static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2313 hci_cp.le = val; 2497 hci_cp.le = val;
2314 hci_cp.simul = 0x00; 2498 hci_cp.simul = 0x00;
2315 } else { 2499 } else {
2316 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) 2500 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
2317 disable_advertising(&req); 2501 disable_advertising(&req);
2318 } 2502 }
2319 2503
@@ -2337,7 +2521,7 @@ unlock:
2337 */ 2521 */
2338static bool pending_eir_or_class(struct hci_dev *hdev) 2522static bool pending_eir_or_class(struct hci_dev *hdev)
2339{ 2523{
2340 struct pending_cmd *cmd; 2524 struct mgmt_pending_cmd *cmd;
2341 2525
2342 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 2526 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2343 switch (cmd->opcode) { 2527 switch (cmd->opcode) {
@@ -2373,16 +2557,16 @@ static u8 get_uuid_size(const u8 *uuid)
2373 2557
2374static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status) 2558static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2375{ 2559{
2376 struct pending_cmd *cmd; 2560 struct mgmt_pending_cmd *cmd;
2377 2561
2378 hci_dev_lock(hdev); 2562 hci_dev_lock(hdev);
2379 2563
2380 cmd = mgmt_pending_find(mgmt_op, hdev); 2564 cmd = pending_find(mgmt_op, hdev);
2381 if (!cmd) 2565 if (!cmd)
2382 goto unlock; 2566 goto unlock;
2383 2567
2384 cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(status), 2568 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2385 hdev->dev_class, 3); 2569 mgmt_status(status), hdev->dev_class, 3);
2386 2570
2387 mgmt_pending_remove(cmd); 2571 mgmt_pending_remove(cmd);
2388 2572
@@ -2400,7 +2584,7 @@ static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
2400static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 2584static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2401{ 2585{
2402 struct mgmt_cp_add_uuid *cp = data; 2586 struct mgmt_cp_add_uuid *cp = data;
2403 struct pending_cmd *cmd; 2587 struct mgmt_pending_cmd *cmd;
2404 struct hci_request req; 2588 struct hci_request req;
2405 struct bt_uuid *uuid; 2589 struct bt_uuid *uuid;
2406 int err; 2590 int err;
@@ -2410,8 +2594,8 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2410 hci_dev_lock(hdev); 2594 hci_dev_lock(hdev);
2411 2595
2412 if (pending_eir_or_class(hdev)) { 2596 if (pending_eir_or_class(hdev)) {
2413 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID, 2597 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2414 MGMT_STATUS_BUSY); 2598 MGMT_STATUS_BUSY);
2415 goto failed; 2599 goto failed;
2416 } 2600 }
2417 2601
@@ -2437,8 +2621,8 @@ static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2437 if (err != -ENODATA) 2621 if (err != -ENODATA)
2438 goto failed; 2622 goto failed;
2439 2623
2440 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0, 2624 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2441 hdev->dev_class, 3); 2625 hdev->dev_class, 3);
2442 goto failed; 2626 goto failed;
2443 } 2627 }
2444 2628
@@ -2460,7 +2644,7 @@ static bool enable_service_cache(struct hci_dev *hdev)
2460 if (!hdev_is_powered(hdev)) 2644 if (!hdev_is_powered(hdev))
2461 return false; 2645 return false;
2462 2646
2463 if (!test_and_set_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { 2647 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
2464 queue_delayed_work(hdev->workqueue, &hdev->service_cache, 2648 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2465 CACHE_TIMEOUT); 2649 CACHE_TIMEOUT);
2466 return true; 2650 return true;
@@ -2480,7 +2664,7 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2480 u16 len) 2664 u16 len)
2481{ 2665{
2482 struct mgmt_cp_remove_uuid *cp = data; 2666 struct mgmt_cp_remove_uuid *cp = data;
2483 struct pending_cmd *cmd; 2667 struct mgmt_pending_cmd *cmd;
2484 struct bt_uuid *match, *tmp; 2668 struct bt_uuid *match, *tmp;
2485 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 2669 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2486 struct hci_request req; 2670 struct hci_request req;
@@ -2491,8 +2675,8 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2491 hci_dev_lock(hdev); 2675 hci_dev_lock(hdev);
2492 2676
2493 if (pending_eir_or_class(hdev)) { 2677 if (pending_eir_or_class(hdev)) {
2494 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, 2678 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2495 MGMT_STATUS_BUSY); 2679 MGMT_STATUS_BUSY);
2496 goto unlock; 2680 goto unlock;
2497 } 2681 }
2498 2682
@@ -2500,8 +2684,9 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2500 hci_uuids_clear(hdev); 2684 hci_uuids_clear(hdev);
2501 2685
2502 if (enable_service_cache(hdev)) { 2686 if (enable_service_cache(hdev)) {
2503 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 2687 err = mgmt_cmd_complete(sk, hdev->id,
2504 0, hdev->dev_class, 3); 2688 MGMT_OP_REMOVE_UUID,
2689 0, hdev->dev_class, 3);
2505 goto unlock; 2690 goto unlock;
2506 } 2691 }
2507 2692
@@ -2520,8 +2705,8 @@ static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2520 } 2705 }
2521 2706
2522 if (found == 0) { 2707 if (found == 0) {
2523 err = cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID, 2708 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2524 MGMT_STATUS_INVALID_PARAMS); 2709 MGMT_STATUS_INVALID_PARAMS);
2525 goto unlock; 2710 goto unlock;
2526 } 2711 }
2527 2712
@@ -2536,8 +2721,8 @@ update_class:
2536 if (err != -ENODATA) 2721 if (err != -ENODATA)
2537 goto unlock; 2722 goto unlock;
2538 2723
2539 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0, 2724 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2540 hdev->dev_class, 3); 2725 hdev->dev_class, 3);
2541 goto unlock; 2726 goto unlock;
2542 } 2727 }
2543 2728
@@ -2565,27 +2750,27 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2565 u16 len) 2750 u16 len)
2566{ 2751{
2567 struct mgmt_cp_set_dev_class *cp = data; 2752 struct mgmt_cp_set_dev_class *cp = data;
2568 struct pending_cmd *cmd; 2753 struct mgmt_pending_cmd *cmd;
2569 struct hci_request req; 2754 struct hci_request req;
2570 int err; 2755 int err;
2571 2756
2572 BT_DBG("request for %s", hdev->name); 2757 BT_DBG("request for %s", hdev->name);
2573 2758
2574 if (!lmp_bredr_capable(hdev)) 2759 if (!lmp_bredr_capable(hdev))
2575 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 2760 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2576 MGMT_STATUS_NOT_SUPPORTED); 2761 MGMT_STATUS_NOT_SUPPORTED);
2577 2762
2578 hci_dev_lock(hdev); 2763 hci_dev_lock(hdev);
2579 2764
2580 if (pending_eir_or_class(hdev)) { 2765 if (pending_eir_or_class(hdev)) {
2581 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 2766 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2582 MGMT_STATUS_BUSY); 2767 MGMT_STATUS_BUSY);
2583 goto unlock; 2768 goto unlock;
2584 } 2769 }
2585 2770
2586 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) { 2771 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2587 err = cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 2772 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2588 MGMT_STATUS_INVALID_PARAMS); 2773 MGMT_STATUS_INVALID_PARAMS);
2589 goto unlock; 2774 goto unlock;
2590 } 2775 }
2591 2776
@@ -2593,14 +2778,14 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2593 hdev->minor_class = cp->minor; 2778 hdev->minor_class = cp->minor;
2594 2779
2595 if (!hdev_is_powered(hdev)) { 2780 if (!hdev_is_powered(hdev)) {
2596 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, 2781 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2597 hdev->dev_class, 3); 2782 hdev->dev_class, 3);
2598 goto unlock; 2783 goto unlock;
2599 } 2784 }
2600 2785
2601 hci_req_init(&req, hdev); 2786 hci_req_init(&req, hdev);
2602 2787
2603 if (test_and_clear_bit(HCI_SERVICE_CACHE, &hdev->dev_flags)) { 2788 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
2604 hci_dev_unlock(hdev); 2789 hci_dev_unlock(hdev);
2605 cancel_delayed_work_sync(&hdev->service_cache); 2790 cancel_delayed_work_sync(&hdev->service_cache);
2606 hci_dev_lock(hdev); 2791 hci_dev_lock(hdev);
@@ -2614,8 +2799,8 @@ static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2614 if (err != -ENODATA) 2799 if (err != -ENODATA)
2615 goto unlock; 2800 goto unlock;
2616 2801
2617 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0, 2802 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2618 hdev->dev_class, 3); 2803 hdev->dev_class, 3);
2619 goto unlock; 2804 goto unlock;
2620 } 2805 }
2621 2806
@@ -2645,15 +2830,15 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2645 BT_DBG("request for %s", hdev->name); 2830 BT_DBG("request for %s", hdev->name);
2646 2831
2647 if (!lmp_bredr_capable(hdev)) 2832 if (!lmp_bredr_capable(hdev))
2648 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 2833 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2649 MGMT_STATUS_NOT_SUPPORTED); 2834 MGMT_STATUS_NOT_SUPPORTED);
2650 2835
2651 key_count = __le16_to_cpu(cp->key_count); 2836 key_count = __le16_to_cpu(cp->key_count);
2652 if (key_count > max_key_count) { 2837 if (key_count > max_key_count) {
2653 BT_ERR("load_link_keys: too big key_count value %u", 2838 BT_ERR("load_link_keys: too big key_count value %u",
2654 key_count); 2839 key_count);
2655 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 2840 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2656 MGMT_STATUS_INVALID_PARAMS); 2841 MGMT_STATUS_INVALID_PARAMS);
2657 } 2842 }
2658 2843
2659 expected_len = sizeof(*cp) + key_count * 2844 expected_len = sizeof(*cp) + key_count *
@@ -2661,13 +2846,13 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2661 if (expected_len != len) { 2846 if (expected_len != len) {
2662 BT_ERR("load_link_keys: expected %u bytes, got %u bytes", 2847 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
2663 expected_len, len); 2848 expected_len, len);
2664 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 2849 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2665 MGMT_STATUS_INVALID_PARAMS); 2850 MGMT_STATUS_INVALID_PARAMS);
2666 } 2851 }
2667 2852
2668 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01) 2853 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
2669 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 2854 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2670 MGMT_STATUS_INVALID_PARAMS); 2855 MGMT_STATUS_INVALID_PARAMS);
2671 2856
2672 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys, 2857 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
2673 key_count); 2858 key_count);
@@ -2676,8 +2861,9 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2676 struct mgmt_link_key_info *key = &cp->keys[i]; 2861 struct mgmt_link_key_info *key = &cp->keys[i];
2677 2862
2678 if (key->addr.type != BDADDR_BREDR || key->type > 0x08) 2863 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
2679 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 2864 return mgmt_cmd_status(sk, hdev->id,
2680 MGMT_STATUS_INVALID_PARAMS); 2865 MGMT_OP_LOAD_LINK_KEYS,
2866 MGMT_STATUS_INVALID_PARAMS);
2681 } 2867 }
2682 2868
2683 hci_dev_lock(hdev); 2869 hci_dev_lock(hdev);
@@ -2685,11 +2871,10 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2685 hci_link_keys_clear(hdev); 2871 hci_link_keys_clear(hdev);
2686 2872
2687 if (cp->debug_keys) 2873 if (cp->debug_keys)
2688 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, 2874 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
2689 &hdev->dev_flags);
2690 else 2875 else
2691 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, 2876 changed = hci_dev_test_and_clear_flag(hdev,
2692 &hdev->dev_flags); 2877 HCI_KEEP_DEBUG_KEYS);
2693 2878
2694 if (changed) 2879 if (changed)
2695 new_settings(hdev, NULL); 2880 new_settings(hdev, NULL);
@@ -2707,7 +2892,7 @@ static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2707 key->type, key->pin_len, NULL); 2892 key->type, key->pin_len, NULL);
2708 } 2893 }
2709 2894
2710 cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0); 2895 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
2711 2896
2712 hci_dev_unlock(hdev); 2897 hci_dev_unlock(hdev);
2713 2898
@@ -2732,7 +2917,7 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2732 struct mgmt_cp_unpair_device *cp = data; 2917 struct mgmt_cp_unpair_device *cp = data;
2733 struct mgmt_rp_unpair_device rp; 2918 struct mgmt_rp_unpair_device rp;
2734 struct hci_cp_disconnect dc; 2919 struct hci_cp_disconnect dc;
2735 struct pending_cmd *cmd; 2920 struct mgmt_pending_cmd *cmd;
2736 struct hci_conn *conn; 2921 struct hci_conn *conn;
2737 int err; 2922 int err;
2738 2923
@@ -2741,20 +2926,21 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2741 rp.addr.type = cp->addr.type; 2926 rp.addr.type = cp->addr.type;
2742 2927
2743 if (!bdaddr_type_is_valid(cp->addr.type)) 2928 if (!bdaddr_type_is_valid(cp->addr.type))
2744 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 2929 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2745 MGMT_STATUS_INVALID_PARAMS, 2930 MGMT_STATUS_INVALID_PARAMS,
2746 &rp, sizeof(rp)); 2931 &rp, sizeof(rp));
2747 2932
2748 if (cp->disconnect != 0x00 && cp->disconnect != 0x01) 2933 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2749 return cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 2934 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2750 MGMT_STATUS_INVALID_PARAMS, 2935 MGMT_STATUS_INVALID_PARAMS,
2751 &rp, sizeof(rp)); 2936 &rp, sizeof(rp));
2752 2937
2753 hci_dev_lock(hdev); 2938 hci_dev_lock(hdev);
2754 2939
2755 if (!hdev_is_powered(hdev)) { 2940 if (!hdev_is_powered(hdev)) {
2756 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 2941 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2757 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); 2942 MGMT_STATUS_NOT_POWERED, &rp,
2943 sizeof(rp));
2758 goto unlock; 2944 goto unlock;
2759 } 2945 }
2760 2946
@@ -2804,8 +2990,9 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2804 } 2990 }
2805 2991
2806 if (err < 0) { 2992 if (err < 0) {
2807 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 2993 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2808 MGMT_STATUS_NOT_PAIRED, &rp, sizeof(rp)); 2994 MGMT_STATUS_NOT_PAIRED, &rp,
2995 sizeof(rp));
2809 goto unlock; 2996 goto unlock;
2810 } 2997 }
2811 2998
@@ -2813,8 +3000,8 @@ static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2813 * link is requested. 3000 * link is requested.
2814 */ 3001 */
2815 if (!conn) { 3002 if (!conn) {
2816 err = cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0, 3003 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2817 &rp, sizeof(rp)); 3004 &rp, sizeof(rp));
2818 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk); 3005 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
2819 goto unlock; 3006 goto unlock;
2820 } 3007 }
@@ -2844,7 +3031,7 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
2844{ 3031{
2845 struct mgmt_cp_disconnect *cp = data; 3032 struct mgmt_cp_disconnect *cp = data;
2846 struct mgmt_rp_disconnect rp; 3033 struct mgmt_rp_disconnect rp;
2847 struct pending_cmd *cmd; 3034 struct mgmt_pending_cmd *cmd;
2848 struct hci_conn *conn; 3035 struct hci_conn *conn;
2849 int err; 3036 int err;
2850 3037
@@ -2855,21 +3042,22 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
2855 rp.addr.type = cp->addr.type; 3042 rp.addr.type = cp->addr.type;
2856 3043
2857 if (!bdaddr_type_is_valid(cp->addr.type)) 3044 if (!bdaddr_type_is_valid(cp->addr.type))
2858 return cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3045 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2859 MGMT_STATUS_INVALID_PARAMS, 3046 MGMT_STATUS_INVALID_PARAMS,
2860 &rp, sizeof(rp)); 3047 &rp, sizeof(rp));
2861 3048
2862 hci_dev_lock(hdev); 3049 hci_dev_lock(hdev);
2863 3050
2864 if (!test_bit(HCI_UP, &hdev->flags)) { 3051 if (!test_bit(HCI_UP, &hdev->flags)) {
2865 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3052 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2866 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); 3053 MGMT_STATUS_NOT_POWERED, &rp,
3054 sizeof(rp));
2867 goto failed; 3055 goto failed;
2868 } 3056 }
2869 3057
2870 if (mgmt_pending_find(MGMT_OP_DISCONNECT, hdev)) { 3058 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
2871 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3059 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2872 MGMT_STATUS_BUSY, &rp, sizeof(rp)); 3060 MGMT_STATUS_BUSY, &rp, sizeof(rp));
2873 goto failed; 3061 goto failed;
2874 } 3062 }
2875 3063
@@ -2880,8 +3068,9 @@ static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
2880 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); 3068 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
2881 3069
2882 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) { 3070 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
2883 err = cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT, 3071 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2884 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); 3072 MGMT_STATUS_NOT_CONNECTED, &rp,
3073 sizeof(rp));
2885 goto failed; 3074 goto failed;
2886 } 3075 }
2887 3076
@@ -2935,8 +3124,8 @@ static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2935 hci_dev_lock(hdev); 3124 hci_dev_lock(hdev);
2936 3125
2937 if (!hdev_is_powered(hdev)) { 3126 if (!hdev_is_powered(hdev)) {
2938 err = cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 3127 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2939 MGMT_STATUS_NOT_POWERED); 3128 MGMT_STATUS_NOT_POWERED);
2940 goto unlock; 3129 goto unlock;
2941 } 3130 }
2942 3131
@@ -2969,8 +3158,8 @@ static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2969 /* Recalculate length in case of filtered SCO connections, etc */ 3158 /* Recalculate length in case of filtered SCO connections, etc */
2970 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info)); 3159 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
2971 3160
2972 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp, 3161 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
2973 rp_len); 3162 rp_len);
2974 3163
2975 kfree(rp); 3164 kfree(rp);
2976 3165
@@ -2982,7 +3171,7 @@ unlock:
2982static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev, 3171static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
2983 struct mgmt_cp_pin_code_neg_reply *cp) 3172 struct mgmt_cp_pin_code_neg_reply *cp)
2984{ 3173{
2985 struct pending_cmd *cmd; 3174 struct mgmt_pending_cmd *cmd;
2986 int err; 3175 int err;
2987 3176
2988 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp, 3177 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
@@ -3004,7 +3193,7 @@ static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3004 struct hci_conn *conn; 3193 struct hci_conn *conn;
3005 struct mgmt_cp_pin_code_reply *cp = data; 3194 struct mgmt_cp_pin_code_reply *cp = data;
3006 struct hci_cp_pin_code_reply reply; 3195 struct hci_cp_pin_code_reply reply;
3007 struct pending_cmd *cmd; 3196 struct mgmt_pending_cmd *cmd;
3008 int err; 3197 int err;
3009 3198
3010 BT_DBG(""); 3199 BT_DBG("");
@@ -3012,15 +3201,15 @@ static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3012 hci_dev_lock(hdev); 3201 hci_dev_lock(hdev);
3013 3202
3014 if (!hdev_is_powered(hdev)) { 3203 if (!hdev_is_powered(hdev)) {
3015 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3204 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3016 MGMT_STATUS_NOT_POWERED); 3205 MGMT_STATUS_NOT_POWERED);
3017 goto failed; 3206 goto failed;
3018 } 3207 }
3019 3208
3020 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr); 3209 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
3021 if (!conn) { 3210 if (!conn) {
3022 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3211 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3023 MGMT_STATUS_NOT_CONNECTED); 3212 MGMT_STATUS_NOT_CONNECTED);
3024 goto failed; 3213 goto failed;
3025 } 3214 }
3026 3215
@@ -3033,8 +3222,8 @@ static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3033 3222
3034 err = send_pin_code_neg_reply(sk, hdev, &ncp); 3223 err = send_pin_code_neg_reply(sk, hdev, &ncp);
3035 if (err >= 0) 3224 if (err >= 0)
3036 err = cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY, 3225 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3037 MGMT_STATUS_INVALID_PARAMS); 3226 MGMT_STATUS_INVALID_PARAMS);
3038 3227
3039 goto failed; 3228 goto failed;
3040 } 3229 }
@@ -3068,8 +3257,8 @@ static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3068 BT_DBG(""); 3257 BT_DBG("");
3069 3258
3070 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY) 3259 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
3071 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 3260 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3072 MGMT_STATUS_INVALID_PARAMS, NULL, 0); 3261 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
3073 3262
3074 hci_dev_lock(hdev); 3263 hci_dev_lock(hdev);
3075 3264
@@ -3080,14 +3269,14 @@ static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3080 3269
3081 hci_dev_unlock(hdev); 3270 hci_dev_unlock(hdev);
3082 3271
3083 return cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0, NULL, 3272 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3084 0); 3273 NULL, 0);
3085} 3274}
3086 3275
3087static struct pending_cmd *find_pairing(struct hci_conn *conn) 3276static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
3088{ 3277{
3089 struct hci_dev *hdev = conn->hdev; 3278 struct hci_dev *hdev = conn->hdev;
3090 struct pending_cmd *cmd; 3279 struct mgmt_pending_cmd *cmd;
3091 3280
3092 list_for_each_entry(cmd, &hdev->mgmt_pending, list) { 3281 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
3093 if (cmd->opcode != MGMT_OP_PAIR_DEVICE) 3282 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
@@ -3102,7 +3291,7 @@ static struct pending_cmd *find_pairing(struct hci_conn *conn)
3102 return NULL; 3291 return NULL;
3103} 3292}
3104 3293
3105static int pairing_complete(struct pending_cmd *cmd, u8 status) 3294static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
3106{ 3295{
3107 struct mgmt_rp_pair_device rp; 3296 struct mgmt_rp_pair_device rp;
3108 struct hci_conn *conn = cmd->user_data; 3297 struct hci_conn *conn = cmd->user_data;
@@ -3111,8 +3300,8 @@ static int pairing_complete(struct pending_cmd *cmd, u8 status)
3111 bacpy(&rp.addr.bdaddr, &conn->dst); 3300 bacpy(&rp.addr.bdaddr, &conn->dst);
3112 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type); 3301 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
3113 3302
3114 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE, status, 3303 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3115 &rp, sizeof(rp)); 3304 status, &rp, sizeof(rp));
3116 3305
3117 /* So we don't get further callbacks for this connection */ 3306 /* So we don't get further callbacks for this connection */
3118 conn->connect_cfm_cb = NULL; 3307 conn->connect_cfm_cb = NULL;
@@ -3134,7 +3323,7 @@ static int pairing_complete(struct pending_cmd *cmd, u8 status)
3134void mgmt_smp_complete(struct hci_conn *conn, bool complete) 3323void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3135{ 3324{
3136 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED; 3325 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3137 struct pending_cmd *cmd; 3326 struct mgmt_pending_cmd *cmd;
3138 3327
3139 cmd = find_pairing(conn); 3328 cmd = find_pairing(conn);
3140 if (cmd) { 3329 if (cmd) {
@@ -3145,7 +3334,7 @@ void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3145 3334
3146static void pairing_complete_cb(struct hci_conn *conn, u8 status) 3335static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3147{ 3336{
3148 struct pending_cmd *cmd; 3337 struct mgmt_pending_cmd *cmd;
3149 3338
3150 BT_DBG("status %u", status); 3339 BT_DBG("status %u", status);
3151 3340
@@ -3161,7 +3350,7 @@ static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3161 3350
3162static void le_pairing_complete_cb(struct hci_conn *conn, u8 status) 3351static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
3163{ 3352{
3164 struct pending_cmd *cmd; 3353 struct mgmt_pending_cmd *cmd;
3165 3354
3166 BT_DBG("status %u", status); 3355 BT_DBG("status %u", status);
3167 3356
@@ -3183,7 +3372,7 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3183{ 3372{
3184 struct mgmt_cp_pair_device *cp = data; 3373 struct mgmt_cp_pair_device *cp = data;
3185 struct mgmt_rp_pair_device rp; 3374 struct mgmt_rp_pair_device rp;
3186 struct pending_cmd *cmd; 3375 struct mgmt_pending_cmd *cmd;
3187 u8 sec_level, auth_type; 3376 u8 sec_level, auth_type;
3188 struct hci_conn *conn; 3377 struct hci_conn *conn;
3189 int err; 3378 int err;
@@ -3195,20 +3384,28 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3195 rp.addr.type = cp->addr.type; 3384 rp.addr.type = cp->addr.type;
3196 3385
3197 if (!bdaddr_type_is_valid(cp->addr.type)) 3386 if (!bdaddr_type_is_valid(cp->addr.type))
3198 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3387 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3199 MGMT_STATUS_INVALID_PARAMS, 3388 MGMT_STATUS_INVALID_PARAMS,
3200 &rp, sizeof(rp)); 3389 &rp, sizeof(rp));
3201 3390
3202 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY) 3391 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
3203 return cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3392 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3204 MGMT_STATUS_INVALID_PARAMS, 3393 MGMT_STATUS_INVALID_PARAMS,
3205 &rp, sizeof(rp)); 3394 &rp, sizeof(rp));
3206 3395
3207 hci_dev_lock(hdev); 3396 hci_dev_lock(hdev);
3208 3397
3209 if (!hdev_is_powered(hdev)) { 3398 if (!hdev_is_powered(hdev)) {
3210 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3399 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3211 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); 3400 MGMT_STATUS_NOT_POWERED, &rp,
3401 sizeof(rp));
3402 goto unlock;
3403 }
3404
3405 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3406 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3407 MGMT_STATUS_ALREADY_PAIRED, &rp,
3408 sizeof(rp));
3212 goto unlock; 3409 goto unlock;
3213 } 3410 }
3214 3411
@@ -3249,19 +3446,22 @@ static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3249 3446
3250 if (PTR_ERR(conn) == -EBUSY) 3447 if (PTR_ERR(conn) == -EBUSY)
3251 status = MGMT_STATUS_BUSY; 3448 status = MGMT_STATUS_BUSY;
3449 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3450 status = MGMT_STATUS_NOT_SUPPORTED;
3451 else if (PTR_ERR(conn) == -ECONNREFUSED)
3452 status = MGMT_STATUS_REJECTED;
3252 else 3453 else
3253 status = MGMT_STATUS_CONNECT_FAILED; 3454 status = MGMT_STATUS_CONNECT_FAILED;
3254 3455
3255 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3456 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3256 status, &rp, 3457 status, &rp, sizeof(rp));
3257 sizeof(rp));
3258 goto unlock; 3458 goto unlock;
3259 } 3459 }
3260 3460
3261 if (conn->connect_cfm_cb) { 3461 if (conn->connect_cfm_cb) {
3262 hci_conn_drop(conn); 3462 hci_conn_drop(conn);
3263 err = cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE, 3463 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3264 MGMT_STATUS_BUSY, &rp, sizeof(rp)); 3464 MGMT_STATUS_BUSY, &rp, sizeof(rp));
3265 goto unlock; 3465 goto unlock;
3266 } 3466 }
3267 3467
@@ -3305,7 +3505,7 @@ static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3305 u16 len) 3505 u16 len)
3306{ 3506{
3307 struct mgmt_addr_info *addr = data; 3507 struct mgmt_addr_info *addr = data;
3308 struct pending_cmd *cmd; 3508 struct mgmt_pending_cmd *cmd;
3309 struct hci_conn *conn; 3509 struct hci_conn *conn;
3310 int err; 3510 int err;
3311 3511
@@ -3314,31 +3514,31 @@ static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3314 hci_dev_lock(hdev); 3514 hci_dev_lock(hdev);
3315 3515
3316 if (!hdev_is_powered(hdev)) { 3516 if (!hdev_is_powered(hdev)) {
3317 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 3517 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3318 MGMT_STATUS_NOT_POWERED); 3518 MGMT_STATUS_NOT_POWERED);
3319 goto unlock; 3519 goto unlock;
3320 } 3520 }
3321 3521
3322 cmd = mgmt_pending_find(MGMT_OP_PAIR_DEVICE, hdev); 3522 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
3323 if (!cmd) { 3523 if (!cmd) {
3324 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 3524 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3325 MGMT_STATUS_INVALID_PARAMS); 3525 MGMT_STATUS_INVALID_PARAMS);
3326 goto unlock; 3526 goto unlock;
3327 } 3527 }
3328 3528
3329 conn = cmd->user_data; 3529 conn = cmd->user_data;
3330 3530
3331 if (bacmp(&addr->bdaddr, &conn->dst) != 0) { 3531 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
3332 err = cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 3532 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3333 MGMT_STATUS_INVALID_PARAMS); 3533 MGMT_STATUS_INVALID_PARAMS);
3334 goto unlock; 3534 goto unlock;
3335 } 3535 }
3336 3536
3337 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED); 3537 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3338 mgmt_pending_remove(cmd); 3538 mgmt_pending_remove(cmd);
3339 3539
3340 err = cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0, 3540 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3341 addr, sizeof(*addr)); 3541 addr, sizeof(*addr));
3342unlock: 3542unlock:
3343 hci_dev_unlock(hdev); 3543 hci_dev_unlock(hdev);
3344 return err; 3544 return err;
@@ -3348,16 +3548,16 @@ static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
3348 struct mgmt_addr_info *addr, u16 mgmt_op, 3548 struct mgmt_addr_info *addr, u16 mgmt_op,
3349 u16 hci_op, __le32 passkey) 3549 u16 hci_op, __le32 passkey)
3350{ 3550{
3351 struct pending_cmd *cmd; 3551 struct mgmt_pending_cmd *cmd;
3352 struct hci_conn *conn; 3552 struct hci_conn *conn;
3353 int err; 3553 int err;
3354 3554
3355 hci_dev_lock(hdev); 3555 hci_dev_lock(hdev);
3356 3556
3357 if (!hdev_is_powered(hdev)) { 3557 if (!hdev_is_powered(hdev)) {
3358 err = cmd_complete(sk, hdev->id, mgmt_op, 3558 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3359 MGMT_STATUS_NOT_POWERED, addr, 3559 MGMT_STATUS_NOT_POWERED, addr,
3360 sizeof(*addr)); 3560 sizeof(*addr));
3361 goto done; 3561 goto done;
3362 } 3562 }
3363 3563
@@ -3367,22 +3567,22 @@ static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
3367 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr); 3567 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
3368 3568
3369 if (!conn) { 3569 if (!conn) {
3370 err = cmd_complete(sk, hdev->id, mgmt_op, 3570 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3371 MGMT_STATUS_NOT_CONNECTED, addr, 3571 MGMT_STATUS_NOT_CONNECTED, addr,
3372 sizeof(*addr)); 3572 sizeof(*addr));
3373 goto done; 3573 goto done;
3374 } 3574 }
3375 3575
3376 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) { 3576 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
3377 err = smp_user_confirm_reply(conn, mgmt_op, passkey); 3577 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
3378 if (!err) 3578 if (!err)
3379 err = cmd_complete(sk, hdev->id, mgmt_op, 3579 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3380 MGMT_STATUS_SUCCESS, addr, 3580 MGMT_STATUS_SUCCESS, addr,
3381 sizeof(*addr)); 3581 sizeof(*addr));
3382 else 3582 else
3383 err = cmd_complete(sk, hdev->id, mgmt_op, 3583 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3384 MGMT_STATUS_FAILED, addr, 3584 MGMT_STATUS_FAILED, addr,
3385 sizeof(*addr)); 3585 sizeof(*addr));
3386 3586
3387 goto done; 3587 goto done;
3388 } 3588 }
@@ -3434,8 +3634,8 @@ static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3434 BT_DBG(""); 3634 BT_DBG("");
3435 3635
3436 if (len != sizeof(*cp)) 3636 if (len != sizeof(*cp))
3437 return cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY, 3637 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3438 MGMT_STATUS_INVALID_PARAMS); 3638 MGMT_STATUS_INVALID_PARAMS);
3439 3639
3440 return user_pairing_resp(sk, hdev, &cp->addr, 3640 return user_pairing_resp(sk, hdev, &cp->addr,
3441 MGMT_OP_USER_CONFIRM_REPLY, 3641 MGMT_OP_USER_CONFIRM_REPLY,
@@ -3491,24 +3691,24 @@ static void update_name(struct hci_request *req)
3491static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode) 3691static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
3492{ 3692{
3493 struct mgmt_cp_set_local_name *cp; 3693 struct mgmt_cp_set_local_name *cp;
3494 struct pending_cmd *cmd; 3694 struct mgmt_pending_cmd *cmd;
3495 3695
3496 BT_DBG("status 0x%02x", status); 3696 BT_DBG("status 0x%02x", status);
3497 3697
3498 hci_dev_lock(hdev); 3698 hci_dev_lock(hdev);
3499 3699
3500 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); 3700 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
3501 if (!cmd) 3701 if (!cmd)
3502 goto unlock; 3702 goto unlock;
3503 3703
3504 cp = cmd->param; 3704 cp = cmd->param;
3505 3705
3506 if (status) 3706 if (status)
3507 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 3707 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3508 mgmt_status(status)); 3708 mgmt_status(status));
3509 else 3709 else
3510 cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 3710 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3511 cp, sizeof(*cp)); 3711 cp, sizeof(*cp));
3512 3712
3513 mgmt_pending_remove(cmd); 3713 mgmt_pending_remove(cmd);
3514 3714
@@ -3520,7 +3720,7 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
3520 u16 len) 3720 u16 len)
3521{ 3721{
3522 struct mgmt_cp_set_local_name *cp = data; 3722 struct mgmt_cp_set_local_name *cp = data;
3523 struct pending_cmd *cmd; 3723 struct mgmt_pending_cmd *cmd;
3524 struct hci_request req; 3724 struct hci_request req;
3525 int err; 3725 int err;
3526 3726
@@ -3534,8 +3734,8 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
3534 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) && 3734 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3535 !memcmp(hdev->short_name, cp->short_name, 3735 !memcmp(hdev->short_name, cp->short_name,
3536 sizeof(hdev->short_name))) { 3736 sizeof(hdev->short_name))) {
3537 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 3737 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3538 data, len); 3738 data, len);
3539 goto failed; 3739 goto failed;
3540 } 3740 }
3541 3741
@@ -3544,13 +3744,13 @@ static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
3544 if (!hdev_is_powered(hdev)) { 3744 if (!hdev_is_powered(hdev)) {
3545 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name)); 3745 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3546 3746
3547 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0, 3747 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3548 data, len); 3748 data, len);
3549 if (err < 0) 3749 if (err < 0)
3550 goto failed; 3750 goto failed;
3551 3751
3552 err = mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data, len, 3752 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3553 sk); 3753 data, len, sk);
3554 3754
3555 goto failed; 3755 goto failed;
3556 } 3756 }
@@ -3585,10 +3785,70 @@ failed:
3585 return err; 3785 return err;
3586} 3786}
3587 3787
3788static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3789 u16 opcode, struct sk_buff *skb)
3790{
3791 struct mgmt_rp_read_local_oob_data mgmt_rp;
3792 size_t rp_size = sizeof(mgmt_rp);
3793 struct mgmt_pending_cmd *cmd;
3794
3795 BT_DBG("%s status %u", hdev->name, status);
3796
3797 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3798 if (!cmd)
3799 return;
3800
3801 if (status || !skb) {
3802 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3803 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3804 goto remove;
3805 }
3806
3807 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3808
3809 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3810 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3811
3812 if (skb->len < sizeof(*rp)) {
3813 mgmt_cmd_status(cmd->sk, hdev->id,
3814 MGMT_OP_READ_LOCAL_OOB_DATA,
3815 MGMT_STATUS_FAILED);
3816 goto remove;
3817 }
3818
3819 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3820 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3821
3822 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3823 } else {
3824 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3825
3826 if (skb->len < sizeof(*rp)) {
3827 mgmt_cmd_status(cmd->sk, hdev->id,
3828 MGMT_OP_READ_LOCAL_OOB_DATA,
3829 MGMT_STATUS_FAILED);
3830 goto remove;
3831 }
3832
3833 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3834 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3835
3836 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3837 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3838 }
3839
3840 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3841 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3842
3843remove:
3844 mgmt_pending_remove(cmd);
3845}
3846
3588static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev, 3847static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
3589 void *data, u16 data_len) 3848 void *data, u16 data_len)
3590{ 3849{
3591 struct pending_cmd *cmd; 3850 struct mgmt_pending_cmd *cmd;
3851 struct hci_request req;
3592 int err; 3852 int err;
3593 3853
3594 BT_DBG("%s", hdev->name); 3854 BT_DBG("%s", hdev->name);
@@ -3596,20 +3856,20 @@ static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
3596 hci_dev_lock(hdev); 3856 hci_dev_lock(hdev);
3597 3857
3598 if (!hdev_is_powered(hdev)) { 3858 if (!hdev_is_powered(hdev)) {
3599 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 3859 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3600 MGMT_STATUS_NOT_POWERED); 3860 MGMT_STATUS_NOT_POWERED);
3601 goto unlock; 3861 goto unlock;
3602 } 3862 }
3603 3863
3604 if (!lmp_ssp_capable(hdev)) { 3864 if (!lmp_ssp_capable(hdev)) {
3605 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 3865 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3606 MGMT_STATUS_NOT_SUPPORTED); 3866 MGMT_STATUS_NOT_SUPPORTED);
3607 goto unlock; 3867 goto unlock;
3608 } 3868 }
3609 3869
3610 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) { 3870 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
3611 err = cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 3871 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3612 MGMT_STATUS_BUSY); 3872 MGMT_STATUS_BUSY);
3613 goto unlock; 3873 goto unlock;
3614 } 3874 }
3615 3875
@@ -3619,12 +3879,14 @@ static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
3619 goto unlock; 3879 goto unlock;
3620 } 3880 }
3621 3881
3882 hci_req_init(&req, hdev);
3883
3622 if (bredr_sc_enabled(hdev)) 3884 if (bredr_sc_enabled(hdev))
3623 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 3885 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
3624 0, NULL);
3625 else 3886 else
3626 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL); 3887 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3627 3888
3889 err = hci_req_run_skb(&req, read_local_oob_data_complete);
3628 if (err < 0) 3890 if (err < 0)
3629 mgmt_pending_remove(cmd); 3891 mgmt_pending_remove(cmd);
3630 3892
@@ -3642,9 +3904,10 @@ static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3642 BT_DBG("%s ", hdev->name); 3904 BT_DBG("%s ", hdev->name);
3643 3905
3644 if (!bdaddr_type_is_valid(addr->type)) 3906 if (!bdaddr_type_is_valid(addr->type))
3645 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, 3907 return mgmt_cmd_complete(sk, hdev->id,
3646 MGMT_STATUS_INVALID_PARAMS, addr, 3908 MGMT_OP_ADD_REMOTE_OOB_DATA,
3647 sizeof(*addr)); 3909 MGMT_STATUS_INVALID_PARAMS,
3910 addr, sizeof(*addr));
3648 3911
3649 hci_dev_lock(hdev); 3912 hci_dev_lock(hdev);
3650 3913
@@ -3653,10 +3916,10 @@ static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3653 u8 status; 3916 u8 status;
3654 3917
3655 if (cp->addr.type != BDADDR_BREDR) { 3918 if (cp->addr.type != BDADDR_BREDR) {
3656 err = cmd_complete(sk, hdev->id, 3919 err = mgmt_cmd_complete(sk, hdev->id,
3657 MGMT_OP_ADD_REMOTE_OOB_DATA, 3920 MGMT_OP_ADD_REMOTE_OOB_DATA,
3658 MGMT_STATUS_INVALID_PARAMS, 3921 MGMT_STATUS_INVALID_PARAMS,
3659 &cp->addr, sizeof(cp->addr)); 3922 &cp->addr, sizeof(cp->addr));
3660 goto unlock; 3923 goto unlock;
3661 } 3924 }
3662 3925
@@ -3668,8 +3931,9 @@ static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3668 else 3931 else
3669 status = MGMT_STATUS_SUCCESS; 3932 status = MGMT_STATUS_SUCCESS;
3670 3933
3671 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, 3934 err = mgmt_cmd_complete(sk, hdev->id,
3672 status, &cp->addr, sizeof(cp->addr)); 3935 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3936 &cp->addr, sizeof(cp->addr));
3673 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) { 3937 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3674 struct mgmt_cp_add_remote_oob_ext_data *cp = data; 3938 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
3675 u8 *rand192, *hash192, *rand256, *hash256; 3939 u8 *rand192, *hash192, *rand256, *hash256;
@@ -3681,10 +3945,10 @@ static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3681 */ 3945 */
3682 if (memcmp(cp->rand192, ZERO_KEY, 16) || 3946 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3683 memcmp(cp->hash192, ZERO_KEY, 16)) { 3947 memcmp(cp->hash192, ZERO_KEY, 16)) {
3684 err = cmd_complete(sk, hdev->id, 3948 err = mgmt_cmd_complete(sk, hdev->id,
3685 MGMT_OP_ADD_REMOTE_OOB_DATA, 3949 MGMT_OP_ADD_REMOTE_OOB_DATA,
3686 MGMT_STATUS_INVALID_PARAMS, 3950 MGMT_STATUS_INVALID_PARAMS,
3687 addr, sizeof(*addr)); 3951 addr, sizeof(*addr));
3688 goto unlock; 3952 goto unlock;
3689 } 3953 }
3690 3954
@@ -3724,12 +3988,13 @@ static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3724 else 3988 else
3725 status = MGMT_STATUS_SUCCESS; 3989 status = MGMT_STATUS_SUCCESS;
3726 3990
3727 err = cmd_complete(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, 3991 err = mgmt_cmd_complete(sk, hdev->id,
3728 status, &cp->addr, sizeof(cp->addr)); 3992 MGMT_OP_ADD_REMOTE_OOB_DATA,
3993 status, &cp->addr, sizeof(cp->addr));
3729 } else { 3994 } else {
3730 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len); 3995 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
3731 err = cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA, 3996 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3732 MGMT_STATUS_INVALID_PARAMS); 3997 MGMT_STATUS_INVALID_PARAMS);
3733 } 3998 }
3734 3999
3735unlock: 4000unlock:
@@ -3747,9 +4012,10 @@ static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3747 BT_DBG("%s", hdev->name); 4012 BT_DBG("%s", hdev->name);
3748 4013
3749 if (cp->addr.type != BDADDR_BREDR) 4014 if (cp->addr.type != BDADDR_BREDR)
3750 return cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, 4015 return mgmt_cmd_complete(sk, hdev->id,
3751 MGMT_STATUS_INVALID_PARAMS, 4016 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3752 &cp->addr, sizeof(cp->addr)); 4017 MGMT_STATUS_INVALID_PARAMS,
4018 &cp->addr, sizeof(cp->addr));
3753 4019
3754 hci_dev_lock(hdev); 4020 hci_dev_lock(hdev);
3755 4021
@@ -3766,100 +4032,136 @@ static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
3766 status = MGMT_STATUS_SUCCESS; 4032 status = MGMT_STATUS_SUCCESS;
3767 4033
3768done: 4034done:
3769 err = cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA, 4035 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3770 status, &cp->addr, sizeof(cp->addr)); 4036 status, &cp->addr, sizeof(cp->addr));
3771 4037
3772 hci_dev_unlock(hdev); 4038 hci_dev_unlock(hdev);
3773 return err; 4039 return err;
3774} 4040}
3775 4041
3776static bool trigger_discovery(struct hci_request *req, u8 *status) 4042static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
3777{ 4043{
3778 struct hci_dev *hdev = req->hdev; 4044 struct hci_dev *hdev = req->hdev;
3779 struct hci_cp_le_set_scan_param param_cp; 4045 struct hci_cp_inquiry cp;
3780 struct hci_cp_le_set_scan_enable enable_cp;
3781 struct hci_cp_inquiry inq_cp;
3782 /* General inquiry access code (GIAC) */ 4046 /* General inquiry access code (GIAC) */
3783 u8 lap[3] = { 0x33, 0x8b, 0x9e }; 4047 u8 lap[3] = { 0x33, 0x8b, 0x9e };
4048
4049 *status = mgmt_bredr_support(hdev);
4050 if (*status)
4051 return false;
4052
4053 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
4054 *status = MGMT_STATUS_BUSY;
4055 return false;
4056 }
4057
4058 hci_inquiry_cache_flush(hdev);
4059
4060 memset(&cp, 0, sizeof(cp));
4061 memcpy(&cp.lap, lap, sizeof(cp.lap));
4062 cp.length = DISCOV_BREDR_INQUIRY_LEN;
4063
4064 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
4065
4066 return true;
4067}
4068
4069static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
4070{
4071 struct hci_dev *hdev = req->hdev;
4072 struct hci_cp_le_set_scan_param param_cp;
4073 struct hci_cp_le_set_scan_enable enable_cp;
3784 u8 own_addr_type; 4074 u8 own_addr_type;
3785 int err; 4075 int err;
3786 4076
3787 switch (hdev->discovery.type) { 4077 *status = mgmt_le_support(hdev);
3788 case DISCOV_TYPE_BREDR: 4078 if (*status)
3789 *status = mgmt_bredr_support(hdev); 4079 return false;
3790 if (*status)
3791 return false;
3792 4080
3793 if (test_bit(HCI_INQUIRY, &hdev->flags)) { 4081 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
3794 *status = MGMT_STATUS_BUSY; 4082 /* Don't let discovery abort an outgoing connection attempt
4083 * that's using directed advertising.
4084 */
4085 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
4086 *status = MGMT_STATUS_REJECTED;
3795 return false; 4087 return false;
3796 } 4088 }
3797 4089
3798 hci_inquiry_cache_flush(hdev); 4090 disable_advertising(req);
4091 }
3799 4092
3800 memset(&inq_cp, 0, sizeof(inq_cp)); 4093 /* If controller is scanning, it means the background scanning is
3801 memcpy(&inq_cp.lap, lap, sizeof(inq_cp.lap)); 4094 * running. Thus, we should temporarily stop it in order to set the
3802 inq_cp.length = DISCOV_BREDR_INQUIRY_LEN; 4095 * discovery scanning parameters.
3803 hci_req_add(req, HCI_OP_INQUIRY, sizeof(inq_cp), &inq_cp); 4096 */
3804 break; 4097 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4098 hci_req_add_le_scan_disable(req);
3805 4099
3806 case DISCOV_TYPE_LE: 4100 /* All active scans will be done with either a resolvable private
3807 case DISCOV_TYPE_INTERLEAVED: 4101 * address (when privacy feature has been enabled) or non-resolvable
3808 *status = mgmt_le_support(hdev); 4102 * private address.
3809 if (*status) 4103 */
3810 return false; 4104 err = hci_update_random_address(req, true, &own_addr_type);
4105 if (err < 0) {
4106 *status = MGMT_STATUS_FAILED;
4107 return false;
4108 }
3811 4109
3812 if (hdev->discovery.type == DISCOV_TYPE_INTERLEAVED && 4110 memset(&param_cp, 0, sizeof(param_cp));
3813 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 4111 param_cp.type = LE_SCAN_ACTIVE;
3814 *status = MGMT_STATUS_NOT_SUPPORTED; 4112 param_cp.interval = cpu_to_le16(interval);
4113 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4114 param_cp.own_address_type = own_addr_type;
4115
4116 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4117 &param_cp);
4118
4119 memset(&enable_cp, 0, sizeof(enable_cp));
4120 enable_cp.enable = LE_SCAN_ENABLE;
4121 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
4122
4123 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4124 &enable_cp);
4125
4126 return true;
4127}
4128
4129static bool trigger_discovery(struct hci_request *req, u8 *status)
4130{
4131 struct hci_dev *hdev = req->hdev;
4132
4133 switch (hdev->discovery.type) {
4134 case DISCOV_TYPE_BREDR:
4135 if (!trigger_bredr_inquiry(req, status))
3815 return false; 4136 return false;
3816 } 4137 break;
3817 4138
3818 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) { 4139 case DISCOV_TYPE_INTERLEAVED:
3819 /* Don't let discovery abort an outgoing 4140 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
3820 * connection attempt that's using directed 4141 &hdev->quirks)) {
3821 * advertising. 4142 /* During simultaneous discovery, we double LE scan
4143 * interval. We must leave some time for the controller
4144 * to do BR/EDR inquiry.
3822 */ 4145 */
3823 if (hci_conn_hash_lookup_state(hdev, LE_LINK, 4146 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
3824 BT_CONNECT)) { 4147 status))
3825 *status = MGMT_STATUS_REJECTED;
3826 return false; 4148 return false;
3827 }
3828 4149
3829 disable_advertising(req); 4150 if (!trigger_bredr_inquiry(req, status))
3830 } 4151 return false;
3831
3832 /* If controller is scanning, it means the background scanning
3833 * is running. Thus, we should temporarily stop it in order to
3834 * set the discovery scanning parameters.
3835 */
3836 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags))
3837 hci_req_add_le_scan_disable(req);
3838 4152
3839 memset(&param_cp, 0, sizeof(param_cp)); 4153 return true;
4154 }
3840 4155
3841 /* All active scans will be done with either a resolvable 4156 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
3842 * private address (when privacy feature has been enabled) 4157 *status = MGMT_STATUS_NOT_SUPPORTED;
3843 * or non-resolvable private address.
3844 */
3845 err = hci_update_random_address(req, true, &own_addr_type);
3846 if (err < 0) {
3847 *status = MGMT_STATUS_FAILED;
3848 return false; 4158 return false;
3849 } 4159 }
4160 /* fall through */
3850 4161
3851 param_cp.type = LE_SCAN_ACTIVE; 4162 case DISCOV_TYPE_LE:
3852 param_cp.interval = cpu_to_le16(DISCOV_LE_SCAN_INT); 4163 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
3853 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN); 4164 return false;
3854 param_cp.own_address_type = own_addr_type;
3855 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
3856 &param_cp);
3857
3858 memset(&enable_cp, 0, sizeof(enable_cp));
3859 enable_cp.enable = LE_SCAN_ENABLE;
3860 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
3861 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
3862 &enable_cp);
3863 break; 4165 break;
3864 4166
3865 default: 4167 default:
@@ -3873,16 +4175,16 @@ static bool trigger_discovery(struct hci_request *req, u8 *status)
3873static void start_discovery_complete(struct hci_dev *hdev, u8 status, 4175static void start_discovery_complete(struct hci_dev *hdev, u8 status,
3874 u16 opcode) 4176 u16 opcode)
3875{ 4177{
3876 struct pending_cmd *cmd; 4178 struct mgmt_pending_cmd *cmd;
3877 unsigned long timeout; 4179 unsigned long timeout;
3878 4180
3879 BT_DBG("status %d", status); 4181 BT_DBG("status %d", status);
3880 4182
3881 hci_dev_lock(hdev); 4183 hci_dev_lock(hdev);
3882 4184
3883 cmd = mgmt_pending_find(MGMT_OP_START_DISCOVERY, hdev); 4185 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
3884 if (!cmd) 4186 if (!cmd)
3885 cmd = mgmt_pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev); 4187 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
3886 4188
3887 if (cmd) { 4189 if (cmd) {
3888 cmd->cmd_complete(cmd, mgmt_status(status)); 4190 cmd->cmd_complete(cmd, mgmt_status(status));
@@ -3904,7 +4206,18 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status,
3904 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT); 4206 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
3905 break; 4207 break;
3906 case DISCOV_TYPE_INTERLEAVED: 4208 case DISCOV_TYPE_INTERLEAVED:
3907 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout); 4209 /* When running simultaneous discovery, the LE scanning time
4210 * should occupy the whole discovery time sine BR/EDR inquiry
4211 * and LE scanning are scheduled by the controller.
4212 *
4213 * For interleaving discovery in comparison, BR/EDR inquiry
4214 * and LE scanning are done sequentially with separate
4215 * timeouts.
4216 */
4217 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4218 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4219 else
4220 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
3908 break; 4221 break;
3909 case DISCOV_TYPE_BREDR: 4222 case DISCOV_TYPE_BREDR:
3910 timeout = 0; 4223 timeout = 0;
@@ -3923,8 +4236,7 @@ static void start_discovery_complete(struct hci_dev *hdev, u8 status,
3923 */ 4236 */
3924 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, 4237 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
3925 &hdev->quirks) && 4238 &hdev->quirks) &&
3926 (hdev->discovery.uuid_count > 0 || 4239 hdev->discovery.result_filtering) {
3927 hdev->discovery.rssi != HCI_RSSI_INVALID)) {
3928 hdev->discovery.scan_start = jiffies; 4240 hdev->discovery.scan_start = jiffies;
3929 hdev->discovery.scan_duration = timeout; 4241 hdev->discovery.scan_duration = timeout;
3930 } 4242 }
@@ -3941,7 +4253,7 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
3941 void *data, u16 len) 4253 void *data, u16 len)
3942{ 4254{
3943 struct mgmt_cp_start_discovery *cp = data; 4255 struct mgmt_cp_start_discovery *cp = data;
3944 struct pending_cmd *cmd; 4256 struct mgmt_pending_cmd *cmd;
3945 struct hci_request req; 4257 struct hci_request req;
3946 u8 status; 4258 u8 status;
3947 int err; 4259 int err;
@@ -3951,17 +4263,17 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
3951 hci_dev_lock(hdev); 4263 hci_dev_lock(hdev);
3952 4264
3953 if (!hdev_is_powered(hdev)) { 4265 if (!hdev_is_powered(hdev)) {
3954 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, 4266 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3955 MGMT_STATUS_NOT_POWERED, 4267 MGMT_STATUS_NOT_POWERED,
3956 &cp->type, sizeof(cp->type)); 4268 &cp->type, sizeof(cp->type));
3957 goto failed; 4269 goto failed;
3958 } 4270 }
3959 4271
3960 if (hdev->discovery.state != DISCOVERY_STOPPED || 4272 if (hdev->discovery.state != DISCOVERY_STOPPED ||
3961 test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { 4273 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
3962 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, 4274 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3963 MGMT_STATUS_BUSY, &cp->type, 4275 MGMT_STATUS_BUSY, &cp->type,
3964 sizeof(cp->type)); 4276 sizeof(cp->type));
3965 goto failed; 4277 goto failed;
3966 } 4278 }
3967 4279
@@ -3984,8 +4296,8 @@ static int start_discovery(struct sock *sk, struct hci_dev *hdev,
3984 hci_req_init(&req, hdev); 4296 hci_req_init(&req, hdev);
3985 4297
3986 if (!trigger_discovery(&req, &status)) { 4298 if (!trigger_discovery(&req, &status)) {
3987 err = cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY, 4299 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
3988 status, &cp->type, sizeof(cp->type)); 4300 status, &cp->type, sizeof(cp->type));
3989 mgmt_pending_remove(cmd); 4301 mgmt_pending_remove(cmd);
3990 goto failed; 4302 goto failed;
3991 } 4303 }
@@ -4003,17 +4315,18 @@ failed:
4003 return err; 4315 return err;
4004} 4316}
4005 4317
4006static int service_discovery_cmd_complete(struct pending_cmd *cmd, u8 status) 4318static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4319 u8 status)
4007{ 4320{
4008 return cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, 4321 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4009 cmd->param, 1); 4322 cmd->param, 1);
4010} 4323}
4011 4324
4012static int start_service_discovery(struct sock *sk, struct hci_dev *hdev, 4325static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4013 void *data, u16 len) 4326 void *data, u16 len)
4014{ 4327{
4015 struct mgmt_cp_start_service_discovery *cp = data; 4328 struct mgmt_cp_start_service_discovery *cp = data;
4016 struct pending_cmd *cmd; 4329 struct mgmt_pending_cmd *cmd;
4017 struct hci_request req; 4330 struct hci_request req;
4018 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16); 4331 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4019 u16 uuid_count, expected_len; 4332 u16 uuid_count, expected_len;
@@ -4025,19 +4338,19 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4025 hci_dev_lock(hdev); 4338 hci_dev_lock(hdev);
4026 4339
4027 if (!hdev_is_powered(hdev)) { 4340 if (!hdev_is_powered(hdev)) {
4028 err = cmd_complete(sk, hdev->id, 4341 err = mgmt_cmd_complete(sk, hdev->id,
4029 MGMT_OP_START_SERVICE_DISCOVERY, 4342 MGMT_OP_START_SERVICE_DISCOVERY,
4030 MGMT_STATUS_NOT_POWERED, 4343 MGMT_STATUS_NOT_POWERED,
4031 &cp->type, sizeof(cp->type)); 4344 &cp->type, sizeof(cp->type));
4032 goto failed; 4345 goto failed;
4033 } 4346 }
4034 4347
4035 if (hdev->discovery.state != DISCOVERY_STOPPED || 4348 if (hdev->discovery.state != DISCOVERY_STOPPED ||
4036 test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags)) { 4349 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
4037 err = cmd_complete(sk, hdev->id, 4350 err = mgmt_cmd_complete(sk, hdev->id,
4038 MGMT_OP_START_SERVICE_DISCOVERY, 4351 MGMT_OP_START_SERVICE_DISCOVERY,
4039 MGMT_STATUS_BUSY, &cp->type, 4352 MGMT_STATUS_BUSY, &cp->type,
4040 sizeof(cp->type)); 4353 sizeof(cp->type));
4041 goto failed; 4354 goto failed;
4042 } 4355 }
4043 4356
@@ -4045,10 +4358,10 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4045 if (uuid_count > max_uuid_count) { 4358 if (uuid_count > max_uuid_count) {
4046 BT_ERR("service_discovery: too big uuid_count value %u", 4359 BT_ERR("service_discovery: too big uuid_count value %u",
4047 uuid_count); 4360 uuid_count);
4048 err = cmd_complete(sk, hdev->id, 4361 err = mgmt_cmd_complete(sk, hdev->id,
4049 MGMT_OP_START_SERVICE_DISCOVERY, 4362 MGMT_OP_START_SERVICE_DISCOVERY,
4050 MGMT_STATUS_INVALID_PARAMS, &cp->type, 4363 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4051 sizeof(cp->type)); 4364 sizeof(cp->type));
4052 goto failed; 4365 goto failed;
4053 } 4366 }
4054 4367
@@ -4056,10 +4369,10 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4056 if (expected_len != len) { 4369 if (expected_len != len) {
4057 BT_ERR("service_discovery: expected %u bytes, got %u bytes", 4370 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4058 expected_len, len); 4371 expected_len, len);
4059 err = cmd_complete(sk, hdev->id, 4372 err = mgmt_cmd_complete(sk, hdev->id,
4060 MGMT_OP_START_SERVICE_DISCOVERY, 4373 MGMT_OP_START_SERVICE_DISCOVERY,
4061 MGMT_STATUS_INVALID_PARAMS, &cp->type, 4374 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4062 sizeof(cp->type)); 4375 sizeof(cp->type));
4063 goto failed; 4376 goto failed;
4064 } 4377 }
4065 4378
@@ -4077,6 +4390,7 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4077 */ 4390 */
4078 hci_discovery_filter_clear(hdev); 4391 hci_discovery_filter_clear(hdev);
4079 4392
4393 hdev->discovery.result_filtering = true;
4080 hdev->discovery.type = cp->type; 4394 hdev->discovery.type = cp->type;
4081 hdev->discovery.rssi = cp->rssi; 4395 hdev->discovery.rssi = cp->rssi;
4082 hdev->discovery.uuid_count = uuid_count; 4396 hdev->discovery.uuid_count = uuid_count;
@@ -4085,10 +4399,10 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4085 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16, 4399 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4086 GFP_KERNEL); 4400 GFP_KERNEL);
4087 if (!hdev->discovery.uuids) { 4401 if (!hdev->discovery.uuids) {
4088 err = cmd_complete(sk, hdev->id, 4402 err = mgmt_cmd_complete(sk, hdev->id,
4089 MGMT_OP_START_SERVICE_DISCOVERY, 4403 MGMT_OP_START_SERVICE_DISCOVERY,
4090 MGMT_STATUS_FAILED, 4404 MGMT_STATUS_FAILED,
4091 &cp->type, sizeof(cp->type)); 4405 &cp->type, sizeof(cp->type));
4092 mgmt_pending_remove(cmd); 4406 mgmt_pending_remove(cmd);
4093 goto failed; 4407 goto failed;
4094 } 4408 }
@@ -4097,9 +4411,9 @@ static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4097 hci_req_init(&req, hdev); 4411 hci_req_init(&req, hdev);
4098 4412
4099 if (!trigger_discovery(&req, &status)) { 4413 if (!trigger_discovery(&req, &status)) {
4100 err = cmd_complete(sk, hdev->id, 4414 err = mgmt_cmd_complete(sk, hdev->id,
4101 MGMT_OP_START_SERVICE_DISCOVERY, 4415 MGMT_OP_START_SERVICE_DISCOVERY,
4102 status, &cp->type, sizeof(cp->type)); 4416 status, &cp->type, sizeof(cp->type));
4103 mgmt_pending_remove(cmd); 4417 mgmt_pending_remove(cmd);
4104 goto failed; 4418 goto failed;
4105 } 4419 }
@@ -4119,13 +4433,13 @@ failed:
4119 4433
4120static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode) 4434static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4121{ 4435{
4122 struct pending_cmd *cmd; 4436 struct mgmt_pending_cmd *cmd;
4123 4437
4124 BT_DBG("status %d", status); 4438 BT_DBG("status %d", status);
4125 4439
4126 hci_dev_lock(hdev); 4440 hci_dev_lock(hdev);
4127 4441
4128 cmd = mgmt_pending_find(MGMT_OP_STOP_DISCOVERY, hdev); 4442 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
4129 if (cmd) { 4443 if (cmd) {
4130 cmd->cmd_complete(cmd, mgmt_status(status)); 4444 cmd->cmd_complete(cmd, mgmt_status(status));
4131 mgmt_pending_remove(cmd); 4445 mgmt_pending_remove(cmd);
@@ -4141,7 +4455,7 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
4141 u16 len) 4455 u16 len)
4142{ 4456{
4143 struct mgmt_cp_stop_discovery *mgmt_cp = data; 4457 struct mgmt_cp_stop_discovery *mgmt_cp = data;
4144 struct pending_cmd *cmd; 4458 struct mgmt_pending_cmd *cmd;
4145 struct hci_request req; 4459 struct hci_request req;
4146 int err; 4460 int err;
4147 4461
@@ -4150,16 +4464,16 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
4150 hci_dev_lock(hdev); 4464 hci_dev_lock(hdev);
4151 4465
4152 if (!hci_discovery_active(hdev)) { 4466 if (!hci_discovery_active(hdev)) {
4153 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 4467 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4154 MGMT_STATUS_REJECTED, &mgmt_cp->type, 4468 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4155 sizeof(mgmt_cp->type)); 4469 sizeof(mgmt_cp->type));
4156 goto unlock; 4470 goto unlock;
4157 } 4471 }
4158 4472
4159 if (hdev->discovery.type != mgmt_cp->type) { 4473 if (hdev->discovery.type != mgmt_cp->type) {
4160 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 4474 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4161 MGMT_STATUS_INVALID_PARAMS, &mgmt_cp->type, 4475 MGMT_STATUS_INVALID_PARAMS,
4162 sizeof(mgmt_cp->type)); 4476 &mgmt_cp->type, sizeof(mgmt_cp->type));
4163 goto unlock; 4477 goto unlock;
4164 } 4478 }
4165 4479
@@ -4185,8 +4499,8 @@ static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
4185 4499
4186 /* If no HCI commands were sent we're done */ 4500 /* If no HCI commands were sent we're done */
4187 if (err == -ENODATA) { 4501 if (err == -ENODATA) {
4188 err = cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0, 4502 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4189 &mgmt_cp->type, sizeof(mgmt_cp->type)); 4503 &mgmt_cp->type, sizeof(mgmt_cp->type));
4190 hci_discovery_set_state(hdev, DISCOVERY_STOPPED); 4504 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4191 } 4505 }
4192 4506
@@ -4207,17 +4521,17 @@ static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
4207 hci_dev_lock(hdev); 4521 hci_dev_lock(hdev);
4208 4522
4209 if (!hci_discovery_active(hdev)) { 4523 if (!hci_discovery_active(hdev)) {
4210 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 4524 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4211 MGMT_STATUS_FAILED, &cp->addr, 4525 MGMT_STATUS_FAILED, &cp->addr,
4212 sizeof(cp->addr)); 4526 sizeof(cp->addr));
4213 goto failed; 4527 goto failed;
4214 } 4528 }
4215 4529
4216 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr); 4530 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
4217 if (!e) { 4531 if (!e) {
4218 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 4532 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4219 MGMT_STATUS_INVALID_PARAMS, &cp->addr, 4533 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4220 sizeof(cp->addr)); 4534 sizeof(cp->addr));
4221 goto failed; 4535 goto failed;
4222 } 4536 }
4223 4537
@@ -4229,8 +4543,8 @@ static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
4229 hci_inquiry_cache_update_resolve(hdev, e); 4543 hci_inquiry_cache_update_resolve(hdev, e);
4230 } 4544 }
4231 4545
4232 err = cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0, &cp->addr, 4546 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4233 sizeof(cp->addr)); 4547 &cp->addr, sizeof(cp->addr));
4234 4548
4235failed: 4549failed:
4236 hci_dev_unlock(hdev); 4550 hci_dev_unlock(hdev);
@@ -4247,9 +4561,9 @@ static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
4247 BT_DBG("%s", hdev->name); 4561 BT_DBG("%s", hdev->name);
4248 4562
4249 if (!bdaddr_type_is_valid(cp->addr.type)) 4563 if (!bdaddr_type_is_valid(cp->addr.type))
4250 return cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, 4564 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4251 MGMT_STATUS_INVALID_PARAMS, 4565 MGMT_STATUS_INVALID_PARAMS,
4252 &cp->addr, sizeof(cp->addr)); 4566 &cp->addr, sizeof(cp->addr));
4253 4567
4254 hci_dev_lock(hdev); 4568 hci_dev_lock(hdev);
4255 4569
@@ -4265,8 +4579,8 @@ static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
4265 status = MGMT_STATUS_SUCCESS; 4579 status = MGMT_STATUS_SUCCESS;
4266 4580
4267done: 4581done:
4268 err = cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status, 4582 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4269 &cp->addr, sizeof(cp->addr)); 4583 &cp->addr, sizeof(cp->addr));
4270 4584
4271 hci_dev_unlock(hdev); 4585 hci_dev_unlock(hdev);
4272 4586
@@ -4283,9 +4597,9 @@ static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
4283 BT_DBG("%s", hdev->name); 4597 BT_DBG("%s", hdev->name);
4284 4598
4285 if (!bdaddr_type_is_valid(cp->addr.type)) 4599 if (!bdaddr_type_is_valid(cp->addr.type))
4286 return cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, 4600 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4287 MGMT_STATUS_INVALID_PARAMS, 4601 MGMT_STATUS_INVALID_PARAMS,
4288 &cp->addr, sizeof(cp->addr)); 4602 &cp->addr, sizeof(cp->addr));
4289 4603
4290 hci_dev_lock(hdev); 4604 hci_dev_lock(hdev);
4291 4605
@@ -4301,8 +4615,8 @@ static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
4301 status = MGMT_STATUS_SUCCESS; 4615 status = MGMT_STATUS_SUCCESS;
4302 4616
4303done: 4617done:
4304 err = cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status, 4618 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4305 &cp->addr, sizeof(cp->addr)); 4619 &cp->addr, sizeof(cp->addr));
4306 4620
4307 hci_dev_unlock(hdev); 4621 hci_dev_unlock(hdev);
4308 4622
@@ -4322,8 +4636,8 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4322 source = __le16_to_cpu(cp->source); 4636 source = __le16_to_cpu(cp->source);
4323 4637
4324 if (source > 0x0002) 4638 if (source > 0x0002)
4325 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 4639 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4326 MGMT_STATUS_INVALID_PARAMS); 4640 MGMT_STATUS_INVALID_PARAMS);
4327 4641
4328 hci_dev_lock(hdev); 4642 hci_dev_lock(hdev);
4329 4643
@@ -4332,7 +4646,8 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4332 hdev->devid_product = __le16_to_cpu(cp->product); 4646 hdev->devid_product = __le16_to_cpu(cp->product);
4333 hdev->devid_version = __le16_to_cpu(cp->version); 4647 hdev->devid_version = __le16_to_cpu(cp->version);
4334 4648
4335 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0, NULL, 0); 4649 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4650 NULL, 0);
4336 4651
4337 hci_req_init(&req, hdev); 4652 hci_req_init(&req, hdev);
4338 update_eir(&req); 4653 update_eir(&req);
@@ -4343,10 +4658,17 @@ static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4343 return err; 4658 return err;
4344} 4659}
4345 4660
4661static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4662 u16 opcode)
4663{
4664 BT_DBG("status %d", status);
4665}
4666
4346static void set_advertising_complete(struct hci_dev *hdev, u8 status, 4667static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4347 u16 opcode) 4668 u16 opcode)
4348{ 4669{
4349 struct cmd_lookup match = { NULL, hdev }; 4670 struct cmd_lookup match = { NULL, hdev };
4671 struct hci_request req;
4350 4672
4351 hci_dev_lock(hdev); 4673 hci_dev_lock(hdev);
4352 4674
@@ -4358,10 +4680,10 @@ static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4358 goto unlock; 4680 goto unlock;
4359 } 4681 }
4360 4682
4361 if (test_bit(HCI_LE_ADV, &hdev->dev_flags)) 4683 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
4362 set_bit(HCI_ADVERTISING, &hdev->dev_flags); 4684 hci_dev_set_flag(hdev, HCI_ADVERTISING);
4363 else 4685 else
4364 clear_bit(HCI_ADVERTISING, &hdev->dev_flags); 4686 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
4365 4687
4366 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp, 4688 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4367 &match); 4689 &match);
@@ -4371,6 +4693,21 @@ static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4371 if (match.sk) 4693 if (match.sk)
4372 sock_put(match.sk); 4694 sock_put(match.sk);
4373 4695
4696 /* If "Set Advertising" was just disabled and instance advertising was
4697 * set up earlier, then enable the advertising instance.
4698 */
4699 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4700 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4701 goto unlock;
4702
4703 hci_req_init(&req, hdev);
4704
4705 update_adv_data(&req);
4706 enable_advertising(&req);
4707
4708 if (hci_req_run(&req, enable_advertising_instance) < 0)
4709 BT_ERR("Failed to re-configure advertising");
4710
4374unlock: 4711unlock:
4375 hci_dev_unlock(hdev); 4712 hci_dev_unlock(hdev);
4376} 4713}
@@ -4379,41 +4716,48 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4379 u16 len) 4716 u16 len)
4380{ 4717{
4381 struct mgmt_mode *cp = data; 4718 struct mgmt_mode *cp = data;
4382 struct pending_cmd *cmd; 4719 struct mgmt_pending_cmd *cmd;
4383 struct hci_request req; 4720 struct hci_request req;
4384 u8 val, enabled, status; 4721 u8 val, status;
4385 int err; 4722 int err;
4386 4723
4387 BT_DBG("request for %s", hdev->name); 4724 BT_DBG("request for %s", hdev->name);
4388 4725
4389 status = mgmt_le_support(hdev); 4726 status = mgmt_le_support(hdev);
4390 if (status) 4727 if (status)
4391 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 4728 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4392 status); 4729 status);
4393 4730
4394 if (cp->val != 0x00 && cp->val != 0x01) 4731 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
4395 return cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 4732 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4396 MGMT_STATUS_INVALID_PARAMS); 4733 MGMT_STATUS_INVALID_PARAMS);
4397 4734
4398 hci_dev_lock(hdev); 4735 hci_dev_lock(hdev);
4399 4736
4400 val = !!cp->val; 4737 val = !!cp->val;
4401 enabled = test_bit(HCI_ADVERTISING, &hdev->dev_flags);
4402 4738
4403 /* The following conditions are ones which mean that we should 4739 /* The following conditions are ones which mean that we should
4404 * not do any HCI communication but directly send a mgmt 4740 * not do any HCI communication but directly send a mgmt
4405 * response to user space (after toggling the flag if 4741 * response to user space (after toggling the flag if
4406 * necessary). 4742 * necessary).
4407 */ 4743 */
4408 if (!hdev_is_powered(hdev) || val == enabled || 4744 if (!hdev_is_powered(hdev) ||
4745 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4746 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
4409 hci_conn_num(hdev, LE_LINK) > 0 || 4747 hci_conn_num(hdev, LE_LINK) > 0 ||
4410 (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && 4748 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
4411 hdev->le_scan_type == LE_SCAN_ACTIVE)) { 4749 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
4412 bool changed = false; 4750 bool changed;
4413 4751
4414 if (val != test_bit(HCI_ADVERTISING, &hdev->dev_flags)) { 4752 if (cp->val) {
4415 change_bit(HCI_ADVERTISING, &hdev->dev_flags); 4753 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
4416 changed = true; 4754 if (cp->val == 0x02)
4755 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4756 else
4757 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4758 } else {
4759 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
4760 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4417 } 4761 }
4418 4762
4419 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev); 4763 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
@@ -4426,10 +4770,10 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4426 goto unlock; 4770 goto unlock;
4427 } 4771 }
4428 4772
4429 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING, hdev) || 4773 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4430 mgmt_pending_find(MGMT_OP_SET_LE, hdev)) { 4774 pending_find(MGMT_OP_SET_LE, hdev)) {
4431 err = cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING, 4775 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4432 MGMT_STATUS_BUSY); 4776 MGMT_STATUS_BUSY);
4433 goto unlock; 4777 goto unlock;
4434 } 4778 }
4435 4779
@@ -4441,10 +4785,19 @@ static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4441 4785
4442 hci_req_init(&req, hdev); 4786 hci_req_init(&req, hdev);
4443 4787
4444 if (val) 4788 if (cp->val == 0x02)
4445 enable_advertising(&req); 4789 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4446 else 4790 else
4791 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4792
4793 if (val) {
4794 /* Switch to instance "0" for the Set Advertising setting. */
4795 update_adv_data_for_instance(&req, 0);
4796 update_scan_rsp_data_for_instance(&req, 0);
4797 enable_advertising(&req);
4798 } else {
4447 disable_advertising(&req); 4799 disable_advertising(&req);
4800 }
4448 4801
4449 err = hci_req_run(&req, set_advertising_complete); 4802 err = hci_req_run(&req, set_advertising_complete);
4450 if (err < 0) 4803 if (err < 0)
@@ -4464,34 +4817,38 @@ static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4464 BT_DBG("%s", hdev->name); 4817 BT_DBG("%s", hdev->name);
4465 4818
4466 if (!lmp_le_capable(hdev)) 4819 if (!lmp_le_capable(hdev))
4467 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 4820 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4468 MGMT_STATUS_NOT_SUPPORTED); 4821 MGMT_STATUS_NOT_SUPPORTED);
4469 4822
4470 if (hdev_is_powered(hdev)) 4823 if (hdev_is_powered(hdev))
4471 return cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 4824 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4472 MGMT_STATUS_REJECTED); 4825 MGMT_STATUS_REJECTED);
4473 4826
4474 if (bacmp(&cp->bdaddr, BDADDR_ANY)) { 4827 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4475 if (!bacmp(&cp->bdaddr, BDADDR_NONE)) 4828 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
4476 return cmd_status(sk, hdev->id, 4829 return mgmt_cmd_status(sk, hdev->id,
4477 MGMT_OP_SET_STATIC_ADDRESS, 4830 MGMT_OP_SET_STATIC_ADDRESS,
4478 MGMT_STATUS_INVALID_PARAMS); 4831 MGMT_STATUS_INVALID_PARAMS);
4479 4832
4480 /* Two most significant bits shall be set */ 4833 /* Two most significant bits shall be set */
4481 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0) 4834 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
4482 return cmd_status(sk, hdev->id, 4835 return mgmt_cmd_status(sk, hdev->id,
4483 MGMT_OP_SET_STATIC_ADDRESS, 4836 MGMT_OP_SET_STATIC_ADDRESS,
4484 MGMT_STATUS_INVALID_PARAMS); 4837 MGMT_STATUS_INVALID_PARAMS);
4485 } 4838 }
4486 4839
4487 hci_dev_lock(hdev); 4840 hci_dev_lock(hdev);
4488 4841
4489 bacpy(&hdev->static_addr, &cp->bdaddr); 4842 bacpy(&hdev->static_addr, &cp->bdaddr);
4490 4843
4491 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS, 0, NULL, 0); 4844 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4845 if (err < 0)
4846 goto unlock;
4492 4847
4493 hci_dev_unlock(hdev); 4848 err = new_settings(hdev, sk);
4494 4849
4850unlock:
4851 hci_dev_unlock(hdev);
4495 return err; 4852 return err;
4496} 4853}
4497 4854
@@ -4505,36 +4862,37 @@ static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4505 BT_DBG("%s", hdev->name); 4862 BT_DBG("%s", hdev->name);
4506 4863
4507 if (!lmp_le_capable(hdev)) 4864 if (!lmp_le_capable(hdev))
4508 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 4865 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4509 MGMT_STATUS_NOT_SUPPORTED); 4866 MGMT_STATUS_NOT_SUPPORTED);
4510 4867
4511 interval = __le16_to_cpu(cp->interval); 4868 interval = __le16_to_cpu(cp->interval);
4512 4869
4513 if (interval < 0x0004 || interval > 0x4000) 4870 if (interval < 0x0004 || interval > 0x4000)
4514 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 4871 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4515 MGMT_STATUS_INVALID_PARAMS); 4872 MGMT_STATUS_INVALID_PARAMS);
4516 4873
4517 window = __le16_to_cpu(cp->window); 4874 window = __le16_to_cpu(cp->window);
4518 4875
4519 if (window < 0x0004 || window > 0x4000) 4876 if (window < 0x0004 || window > 0x4000)
4520 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 4877 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4521 MGMT_STATUS_INVALID_PARAMS); 4878 MGMT_STATUS_INVALID_PARAMS);
4522 4879
4523 if (window > interval) 4880 if (window > interval)
4524 return cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 4881 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4525 MGMT_STATUS_INVALID_PARAMS); 4882 MGMT_STATUS_INVALID_PARAMS);
4526 4883
4527 hci_dev_lock(hdev); 4884 hci_dev_lock(hdev);
4528 4885
4529 hdev->le_scan_interval = interval; 4886 hdev->le_scan_interval = interval;
4530 hdev->le_scan_window = window; 4887 hdev->le_scan_window = window;
4531 4888
4532 err = cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0, NULL, 0); 4889 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4890 NULL, 0);
4533 4891
4534 /* If background scan is running, restart it so new parameters are 4892 /* If background scan is running, restart it so new parameters are
4535 * loaded. 4893 * loaded.
4536 */ 4894 */
4537 if (test_bit(HCI_LE_SCAN, &hdev->dev_flags) && 4895 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
4538 hdev->discovery.state == DISCOVERY_STOPPED) { 4896 hdev->discovery.state == DISCOVERY_STOPPED) {
4539 struct hci_request req; 4897 struct hci_request req;
4540 4898
@@ -4554,26 +4912,26 @@ static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4554static void fast_connectable_complete(struct hci_dev *hdev, u8 status, 4912static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4555 u16 opcode) 4913 u16 opcode)
4556{ 4914{
4557 struct pending_cmd *cmd; 4915 struct mgmt_pending_cmd *cmd;
4558 4916
4559 BT_DBG("status 0x%02x", status); 4917 BT_DBG("status 0x%02x", status);
4560 4918
4561 hci_dev_lock(hdev); 4919 hci_dev_lock(hdev);
4562 4920
4563 cmd = mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev); 4921 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4564 if (!cmd) 4922 if (!cmd)
4565 goto unlock; 4923 goto unlock;
4566 4924
4567 if (status) { 4925 if (status) {
4568 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 4926 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4569 mgmt_status(status)); 4927 mgmt_status(status));
4570 } else { 4928 } else {
4571 struct mgmt_mode *cp = cmd->param; 4929 struct mgmt_mode *cp = cmd->param;
4572 4930
4573 if (cp->val) 4931 if (cp->val)
4574 set_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); 4932 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
4575 else 4933 else
4576 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); 4934 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4577 4935
4578 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev); 4936 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4579 new_settings(hdev, cmd->sk); 4937 new_settings(hdev, cmd->sk);
@@ -4589,40 +4947,40 @@ static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
4589 void *data, u16 len) 4947 void *data, u16 len)
4590{ 4948{
4591 struct mgmt_mode *cp = data; 4949 struct mgmt_mode *cp = data;
4592 struct pending_cmd *cmd; 4950 struct mgmt_pending_cmd *cmd;
4593 struct hci_request req; 4951 struct hci_request req;
4594 int err; 4952 int err;
4595 4953
4596 BT_DBG("%s", hdev->name); 4954 BT_DBG("%s", hdev->name);
4597 4955
4598 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) || 4956 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
4599 hdev->hci_ver < BLUETOOTH_VER_1_2) 4957 hdev->hci_ver < BLUETOOTH_VER_1_2)
4600 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 4958 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4601 MGMT_STATUS_NOT_SUPPORTED); 4959 MGMT_STATUS_NOT_SUPPORTED);
4602 4960
4603 if (cp->val != 0x00 && cp->val != 0x01) 4961 if (cp->val != 0x00 && cp->val != 0x01)
4604 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 4962 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4605 MGMT_STATUS_INVALID_PARAMS); 4963 MGMT_STATUS_INVALID_PARAMS);
4606
4607 if (!hdev_is_powered(hdev))
4608 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4609 MGMT_STATUS_NOT_POWERED);
4610
4611 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags))
4612 return cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4613 MGMT_STATUS_REJECTED);
4614 4964
4615 hci_dev_lock(hdev); 4965 hci_dev_lock(hdev);
4616 4966
4617 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) { 4967 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
4618 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 4968 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4619 MGMT_STATUS_BUSY); 4969 MGMT_STATUS_BUSY);
4970 goto unlock;
4971 }
4972
4973 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
4974 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4975 hdev);
4620 goto unlock; 4976 goto unlock;
4621 } 4977 }
4622 4978
4623 if (!!cp->val == test_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags)) { 4979 if (!hdev_is_powered(hdev)) {
4980 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
4624 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, 4981 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4625 hdev); 4982 hdev);
4983 new_settings(hdev, sk);
4626 goto unlock; 4984 goto unlock;
4627 } 4985 }
4628 4986
@@ -4639,8 +4997,8 @@ static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
4639 4997
4640 err = hci_req_run(&req, fast_connectable_complete); 4998 err = hci_req_run(&req, fast_connectable_complete);
4641 if (err < 0) { 4999 if (err < 0) {
4642 err = cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE, 5000 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4643 MGMT_STATUS_FAILED); 5001 MGMT_STATUS_FAILED);
4644 mgmt_pending_remove(cmd); 5002 mgmt_pending_remove(cmd);
4645 } 5003 }
4646 5004
@@ -4652,13 +5010,13 @@ unlock:
4652 5010
4653static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode) 5011static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4654{ 5012{
4655 struct pending_cmd *cmd; 5013 struct mgmt_pending_cmd *cmd;
4656 5014
4657 BT_DBG("status 0x%02x", status); 5015 BT_DBG("status 0x%02x", status);
4658 5016
4659 hci_dev_lock(hdev); 5017 hci_dev_lock(hdev);
4660 5018
4661 cmd = mgmt_pending_find(MGMT_OP_SET_BREDR, hdev); 5019 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
4662 if (!cmd) 5020 if (!cmd)
4663 goto unlock; 5021 goto unlock;
4664 5022
@@ -4668,9 +5026,9 @@ static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4668 /* We need to restore the flag if related HCI commands 5026 /* We need to restore the flag if related HCI commands
4669 * failed. 5027 * failed.
4670 */ 5028 */
4671 clear_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 5029 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
4672 5030
4673 cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err); 5031 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
4674 } else { 5032 } else {
4675 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev); 5033 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4676 new_settings(hdev, cmd->sk); 5034 new_settings(hdev, cmd->sk);
@@ -4685,41 +5043,41 @@ unlock:
4685static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len) 5043static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4686{ 5044{
4687 struct mgmt_mode *cp = data; 5045 struct mgmt_mode *cp = data;
4688 struct pending_cmd *cmd; 5046 struct mgmt_pending_cmd *cmd;
4689 struct hci_request req; 5047 struct hci_request req;
4690 int err; 5048 int err;
4691 5049
4692 BT_DBG("request for %s", hdev->name); 5050 BT_DBG("request for %s", hdev->name);
4693 5051
4694 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev)) 5052 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
4695 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5053 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4696 MGMT_STATUS_NOT_SUPPORTED); 5054 MGMT_STATUS_NOT_SUPPORTED);
4697 5055
4698 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 5056 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
4699 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5057 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4700 MGMT_STATUS_REJECTED); 5058 MGMT_STATUS_REJECTED);
4701 5059
4702 if (cp->val != 0x00 && cp->val != 0x01) 5060 if (cp->val != 0x00 && cp->val != 0x01)
4703 return cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5061 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4704 MGMT_STATUS_INVALID_PARAMS); 5062 MGMT_STATUS_INVALID_PARAMS);
4705 5063
4706 hci_dev_lock(hdev); 5064 hci_dev_lock(hdev);
4707 5065
4708 if (cp->val == test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 5066 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4709 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); 5067 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4710 goto unlock; 5068 goto unlock;
4711 } 5069 }
4712 5070
4713 if (!hdev_is_powered(hdev)) { 5071 if (!hdev_is_powered(hdev)) {
4714 if (!cp->val) { 5072 if (!cp->val) {
4715 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 5073 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4716 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 5074 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4717 clear_bit(HCI_LINK_SECURITY, &hdev->dev_flags); 5075 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4718 clear_bit(HCI_FAST_CONNECTABLE, &hdev->dev_flags); 5076 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4719 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 5077 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
4720 } 5078 }
4721 5079
4722 change_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 5080 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
4723 5081
4724 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev); 5082 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4725 if (err < 0) 5083 if (err < 0)
@@ -4731,8 +5089,8 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4731 5089
4732 /* Reject disabling when powered on */ 5090 /* Reject disabling when powered on */
4733 if (!cp->val) { 5091 if (!cp->val) {
4734 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5092 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4735 MGMT_STATUS_REJECTED); 5093 MGMT_STATUS_REJECTED);
4736 goto unlock; 5094 goto unlock;
4737 } else { 5095 } else {
4738 /* When configuring a dual-mode controller to operate 5096 /* When configuring a dual-mode controller to operate
@@ -4749,18 +5107,18 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4749 * switching BR/EDR back on when secure connections has been 5107 * switching BR/EDR back on when secure connections has been
4750 * enabled is not a supported transaction. 5108 * enabled is not a supported transaction.
4751 */ 5109 */
4752 if (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) && 5110 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
4753 (bacmp(&hdev->static_addr, BDADDR_ANY) || 5111 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
4754 test_bit(HCI_SC_ENABLED, &hdev->dev_flags))) { 5112 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
4755 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5113 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4756 MGMT_STATUS_REJECTED); 5114 MGMT_STATUS_REJECTED);
4757 goto unlock; 5115 goto unlock;
4758 } 5116 }
4759 } 5117 }
4760 5118
4761 if (mgmt_pending_find(MGMT_OP_SET_BREDR, hdev)) { 5119 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
4762 err = cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR, 5120 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4763 MGMT_STATUS_BUSY); 5121 MGMT_STATUS_BUSY);
4764 goto unlock; 5122 goto unlock;
4765 } 5123 }
4766 5124
@@ -4773,7 +5131,7 @@ static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4773 /* We need to flip the bit already here so that update_adv_data 5131 /* We need to flip the bit already here so that update_adv_data
4774 * generates the correct flags. 5132 * generates the correct flags.
4775 */ 5133 */
4776 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags); 5134 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
4777 5135
4778 hci_req_init(&req, hdev); 5136 hci_req_init(&req, hdev);
4779 5137
@@ -4796,20 +5154,20 @@ unlock:
4796 5154
4797static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode) 5155static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4798{ 5156{
4799 struct pending_cmd *cmd; 5157 struct mgmt_pending_cmd *cmd;
4800 struct mgmt_mode *cp; 5158 struct mgmt_mode *cp;
4801 5159
4802 BT_DBG("%s status %u", hdev->name, status); 5160 BT_DBG("%s status %u", hdev->name, status);
4803 5161
4804 hci_dev_lock(hdev); 5162 hci_dev_lock(hdev);
4805 5163
4806 cmd = mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev); 5164 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
4807 if (!cmd) 5165 if (!cmd)
4808 goto unlock; 5166 goto unlock;
4809 5167
4810 if (status) { 5168 if (status) {
4811 cmd_status(cmd->sk, cmd->index, cmd->opcode, 5169 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
4812 mgmt_status(status)); 5170 mgmt_status(status));
4813 goto remove; 5171 goto remove;
4814 } 5172 }
4815 5173
@@ -4817,16 +5175,16 @@ static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4817 5175
4818 switch (cp->val) { 5176 switch (cp->val) {
4819 case 0x00: 5177 case 0x00:
4820 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags); 5178 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
4821 clear_bit(HCI_SC_ONLY, &hdev->dev_flags); 5179 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
4822 break; 5180 break;
4823 case 0x01: 5181 case 0x01:
4824 set_bit(HCI_SC_ENABLED, &hdev->dev_flags); 5182 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4825 clear_bit(HCI_SC_ONLY, &hdev->dev_flags); 5183 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
4826 break; 5184 break;
4827 case 0x02: 5185 case 0x02:
4828 set_bit(HCI_SC_ENABLED, &hdev->dev_flags); 5186 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4829 set_bit(HCI_SC_ONLY, &hdev->dev_flags); 5187 hci_dev_set_flag(hdev, HCI_SC_ONLY);
4830 break; 5188 break;
4831 } 5189 }
4832 5190
@@ -4843,7 +5201,7 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4843 void *data, u16 len) 5201 void *data, u16 len)
4844{ 5202{
4845 struct mgmt_mode *cp = data; 5203 struct mgmt_mode *cp = data;
4846 struct pending_cmd *cmd; 5204 struct mgmt_pending_cmd *cmd;
4847 struct hci_request req; 5205 struct hci_request req;
4848 u8 val; 5206 u8 val;
4849 int err; 5207 int err;
@@ -4851,37 +5209,37 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4851 BT_DBG("request for %s", hdev->name); 5209 BT_DBG("request for %s", hdev->name);
4852 5210
4853 if (!lmp_sc_capable(hdev) && 5211 if (!lmp_sc_capable(hdev) &&
4854 !test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 5212 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
4855 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 5213 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4856 MGMT_STATUS_NOT_SUPPORTED); 5214 MGMT_STATUS_NOT_SUPPORTED);
4857 5215
4858 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) && 5216 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
4859 lmp_sc_capable(hdev) && 5217 lmp_sc_capable(hdev) &&
4860 !test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) 5218 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
4861 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 5219 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4862 MGMT_STATUS_REJECTED); 5220 MGMT_STATUS_REJECTED);
4863 5221
4864 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 5222 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
4865 return cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 5223 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4866 MGMT_STATUS_INVALID_PARAMS); 5224 MGMT_STATUS_INVALID_PARAMS);
4867 5225
4868 hci_dev_lock(hdev); 5226 hci_dev_lock(hdev);
4869 5227
4870 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) || 5228 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
4871 !test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 5229 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4872 bool changed; 5230 bool changed;
4873 5231
4874 if (cp->val) { 5232 if (cp->val) {
4875 changed = !test_and_set_bit(HCI_SC_ENABLED, 5233 changed = !hci_dev_test_and_set_flag(hdev,
4876 &hdev->dev_flags); 5234 HCI_SC_ENABLED);
4877 if (cp->val == 0x02) 5235 if (cp->val == 0x02)
4878 set_bit(HCI_SC_ONLY, &hdev->dev_flags); 5236 hci_dev_set_flag(hdev, HCI_SC_ONLY);
4879 else 5237 else
4880 clear_bit(HCI_SC_ONLY, &hdev->dev_flags); 5238 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
4881 } else { 5239 } else {
4882 changed = test_and_clear_bit(HCI_SC_ENABLED, 5240 changed = hci_dev_test_and_clear_flag(hdev,
4883 &hdev->dev_flags); 5241 HCI_SC_ENABLED);
4884 clear_bit(HCI_SC_ONLY, &hdev->dev_flags); 5242 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
4885 } 5243 }
4886 5244
4887 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); 5245 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
@@ -4894,16 +5252,16 @@ static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4894 goto failed; 5252 goto failed;
4895 } 5253 }
4896 5254
4897 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) { 5255 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
4898 err = cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN, 5256 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4899 MGMT_STATUS_BUSY); 5257 MGMT_STATUS_BUSY);
4900 goto failed; 5258 goto failed;
4901 } 5259 }
4902 5260
4903 val = !!cp->val; 5261 val = !!cp->val;
4904 5262
4905 if (val == test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && 5263 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
4906 (cp->val == 0x02) == test_bit(HCI_SC_ONLY, &hdev->dev_flags)) { 5264 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
4907 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev); 5265 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4908 goto failed; 5266 goto failed;
4909 } 5267 }
@@ -4937,27 +5295,26 @@ static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
4937 BT_DBG("request for %s", hdev->name); 5295 BT_DBG("request for %s", hdev->name);
4938 5296
4939 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02) 5297 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
4940 return cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS, 5298 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
4941 MGMT_STATUS_INVALID_PARAMS); 5299 MGMT_STATUS_INVALID_PARAMS);
4942 5300
4943 hci_dev_lock(hdev); 5301 hci_dev_lock(hdev);
4944 5302
4945 if (cp->val) 5303 if (cp->val)
4946 changed = !test_and_set_bit(HCI_KEEP_DEBUG_KEYS, 5304 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
4947 &hdev->dev_flags);
4948 else 5305 else
4949 changed = test_and_clear_bit(HCI_KEEP_DEBUG_KEYS, 5306 changed = hci_dev_test_and_clear_flag(hdev,
4950 &hdev->dev_flags); 5307 HCI_KEEP_DEBUG_KEYS);
4951 5308
4952 if (cp->val == 0x02) 5309 if (cp->val == 0x02)
4953 use_changed = !test_and_set_bit(HCI_USE_DEBUG_KEYS, 5310 use_changed = !hci_dev_test_and_set_flag(hdev,
4954 &hdev->dev_flags); 5311 HCI_USE_DEBUG_KEYS);
4955 else 5312 else
4956 use_changed = test_and_clear_bit(HCI_USE_DEBUG_KEYS, 5313 use_changed = hci_dev_test_and_clear_flag(hdev,
4957 &hdev->dev_flags); 5314 HCI_USE_DEBUG_KEYS);
4958 5315
4959 if (hdev_is_powered(hdev) && use_changed && 5316 if (hdev_is_powered(hdev) && use_changed &&
4960 test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 5317 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
4961 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00; 5318 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
4962 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE, 5319 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
4963 sizeof(mode), &mode); 5320 sizeof(mode), &mode);
@@ -4985,32 +5342,32 @@ static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
4985 BT_DBG("request for %s", hdev->name); 5342 BT_DBG("request for %s", hdev->name);
4986 5343
4987 if (!lmp_le_capable(hdev)) 5344 if (!lmp_le_capable(hdev))
4988 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 5345 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4989 MGMT_STATUS_NOT_SUPPORTED); 5346 MGMT_STATUS_NOT_SUPPORTED);
4990 5347
4991 if (cp->privacy != 0x00 && cp->privacy != 0x01) 5348 if (cp->privacy != 0x00 && cp->privacy != 0x01)
4992 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 5349 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4993 MGMT_STATUS_INVALID_PARAMS); 5350 MGMT_STATUS_INVALID_PARAMS);
4994 5351
4995 if (hdev_is_powered(hdev)) 5352 if (hdev_is_powered(hdev))
4996 return cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY, 5353 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
4997 MGMT_STATUS_REJECTED); 5354 MGMT_STATUS_REJECTED);
4998 5355
4999 hci_dev_lock(hdev); 5356 hci_dev_lock(hdev);
5000 5357
5001 /* If user space supports this command it is also expected to 5358 /* If user space supports this command it is also expected to
5002 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag. 5359 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5003 */ 5360 */
5004 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); 5361 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
5005 5362
5006 if (cp->privacy) { 5363 if (cp->privacy) {
5007 changed = !test_and_set_bit(HCI_PRIVACY, &hdev->dev_flags); 5364 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
5008 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk)); 5365 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
5009 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 5366 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
5010 } else { 5367 } else {
5011 changed = test_and_clear_bit(HCI_PRIVACY, &hdev->dev_flags); 5368 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
5012 memset(hdev->irk, 0, sizeof(hdev->irk)); 5369 memset(hdev->irk, 0, sizeof(hdev->irk));
5013 clear_bit(HCI_RPA_EXPIRED, &hdev->dev_flags); 5370 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
5014 } 5371 }
5015 5372
5016 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev); 5373 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
@@ -5053,22 +5410,22 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5053 BT_DBG("request for %s", hdev->name); 5410 BT_DBG("request for %s", hdev->name);
5054 5411
5055 if (!lmp_le_capable(hdev)) 5412 if (!lmp_le_capable(hdev))
5056 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 5413 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5057 MGMT_STATUS_NOT_SUPPORTED); 5414 MGMT_STATUS_NOT_SUPPORTED);
5058 5415
5059 irk_count = __le16_to_cpu(cp->irk_count); 5416 irk_count = __le16_to_cpu(cp->irk_count);
5060 if (irk_count > max_irk_count) { 5417 if (irk_count > max_irk_count) {
5061 BT_ERR("load_irks: too big irk_count value %u", irk_count); 5418 BT_ERR("load_irks: too big irk_count value %u", irk_count);
5062 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 5419 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5063 MGMT_STATUS_INVALID_PARAMS); 5420 MGMT_STATUS_INVALID_PARAMS);
5064 } 5421 }
5065 5422
5066 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info); 5423 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5067 if (expected_len != len) { 5424 if (expected_len != len) {
5068 BT_ERR("load_irks: expected %u bytes, got %u bytes", 5425 BT_ERR("load_irks: expected %u bytes, got %u bytes",
5069 expected_len, len); 5426 expected_len, len);
5070 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS, 5427 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5071 MGMT_STATUS_INVALID_PARAMS); 5428 MGMT_STATUS_INVALID_PARAMS);
5072 } 5429 }
5073 5430
5074 BT_DBG("%s irk_count %u", hdev->name, irk_count); 5431 BT_DBG("%s irk_count %u", hdev->name, irk_count);
@@ -5077,9 +5434,9 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5077 struct mgmt_irk_info *key = &cp->irks[i]; 5434 struct mgmt_irk_info *key = &cp->irks[i];
5078 5435
5079 if (!irk_is_valid(key)) 5436 if (!irk_is_valid(key))
5080 return cmd_status(sk, hdev->id, 5437 return mgmt_cmd_status(sk, hdev->id,
5081 MGMT_OP_LOAD_IRKS, 5438 MGMT_OP_LOAD_IRKS,
5082 MGMT_STATUS_INVALID_PARAMS); 5439 MGMT_STATUS_INVALID_PARAMS);
5083 } 5440 }
5084 5441
5085 hci_dev_lock(hdev); 5442 hci_dev_lock(hdev);
@@ -5099,9 +5456,9 @@ static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5099 BDADDR_ANY); 5456 BDADDR_ANY);
5100 } 5457 }
5101 5458
5102 set_bit(HCI_RPA_RESOLVING, &hdev->dev_flags); 5459 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
5103 5460
5104 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0); 5461 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
5105 5462
5106 hci_dev_unlock(hdev); 5463 hci_dev_unlock(hdev);
5107 5464
@@ -5139,14 +5496,14 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5139 BT_DBG("request for %s", hdev->name); 5496 BT_DBG("request for %s", hdev->name);
5140 5497
5141 if (!lmp_le_capable(hdev)) 5498 if (!lmp_le_capable(hdev))
5142 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 5499 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5143 MGMT_STATUS_NOT_SUPPORTED); 5500 MGMT_STATUS_NOT_SUPPORTED);
5144 5501
5145 key_count = __le16_to_cpu(cp->key_count); 5502 key_count = __le16_to_cpu(cp->key_count);
5146 if (key_count > max_key_count) { 5503 if (key_count > max_key_count) {
5147 BT_ERR("load_ltks: too big key_count value %u", key_count); 5504 BT_ERR("load_ltks: too big key_count value %u", key_count);
5148 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 5505 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5149 MGMT_STATUS_INVALID_PARAMS); 5506 MGMT_STATUS_INVALID_PARAMS);
5150 } 5507 }
5151 5508
5152 expected_len = sizeof(*cp) + key_count * 5509 expected_len = sizeof(*cp) + key_count *
@@ -5154,8 +5511,8 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5154 if (expected_len != len) { 5511 if (expected_len != len) {
5155 BT_ERR("load_keys: expected %u bytes, got %u bytes", 5512 BT_ERR("load_keys: expected %u bytes, got %u bytes",
5156 expected_len, len); 5513 expected_len, len);
5157 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 5514 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5158 MGMT_STATUS_INVALID_PARAMS); 5515 MGMT_STATUS_INVALID_PARAMS);
5159 } 5516 }
5160 5517
5161 BT_DBG("%s key_count %u", hdev->name, key_count); 5518 BT_DBG("%s key_count %u", hdev->name, key_count);
@@ -5164,9 +5521,9 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5164 struct mgmt_ltk_info *key = &cp->keys[i]; 5521 struct mgmt_ltk_info *key = &cp->keys[i];
5165 5522
5166 if (!ltk_is_valid(key)) 5523 if (!ltk_is_valid(key))
5167 return cmd_status(sk, hdev->id, 5524 return mgmt_cmd_status(sk, hdev->id,
5168 MGMT_OP_LOAD_LONG_TERM_KEYS, 5525 MGMT_OP_LOAD_LONG_TERM_KEYS,
5169 MGMT_STATUS_INVALID_PARAMS); 5526 MGMT_STATUS_INVALID_PARAMS);
5170 } 5527 }
5171 5528
5172 hci_dev_lock(hdev); 5529 hci_dev_lock(hdev);
@@ -5211,7 +5568,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5211 key->rand); 5568 key->rand);
5212 } 5569 }
5213 5570
5214 err = cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0, 5571 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
5215 NULL, 0); 5572 NULL, 0);
5216 5573
5217 hci_dev_unlock(hdev); 5574 hci_dev_unlock(hdev);
@@ -5219,7 +5576,7 @@ static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
5219 return err; 5576 return err;
5220} 5577}
5221 5578
5222static int conn_info_cmd_complete(struct pending_cmd *cmd, u8 status) 5579static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
5223{ 5580{
5224 struct hci_conn *conn = cmd->user_data; 5581 struct hci_conn *conn = cmd->user_data;
5225 struct mgmt_rp_get_conn_info rp; 5582 struct mgmt_rp_get_conn_info rp;
@@ -5237,8 +5594,8 @@ static int conn_info_cmd_complete(struct pending_cmd *cmd, u8 status)
5237 rp.max_tx_power = HCI_TX_POWER_INVALID; 5594 rp.max_tx_power = HCI_TX_POWER_INVALID;
5238 } 5595 }
5239 5596
5240 err = cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, status, 5597 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5241 &rp, sizeof(rp)); 5598 status, &rp, sizeof(rp));
5242 5599
5243 hci_conn_drop(conn); 5600 hci_conn_drop(conn);
5244 hci_conn_put(conn); 5601 hci_conn_put(conn);
@@ -5250,7 +5607,7 @@ static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5250 u16 opcode) 5607 u16 opcode)
5251{ 5608{
5252 struct hci_cp_read_rssi *cp; 5609 struct hci_cp_read_rssi *cp;
5253 struct pending_cmd *cmd; 5610 struct mgmt_pending_cmd *cmd;
5254 struct hci_conn *conn; 5611 struct hci_conn *conn;
5255 u16 handle; 5612 u16 handle;
5256 u8 status; 5613 u8 status;
@@ -5288,7 +5645,7 @@ static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5288 goto unlock; 5645 goto unlock;
5289 } 5646 }
5290 5647
5291 cmd = mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn); 5648 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
5292 if (!cmd) 5649 if (!cmd)
5293 goto unlock; 5650 goto unlock;
5294 5651
@@ -5315,15 +5672,16 @@ static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5315 rp.addr.type = cp->addr.type; 5672 rp.addr.type = cp->addr.type;
5316 5673
5317 if (!bdaddr_type_is_valid(cp->addr.type)) 5674 if (!bdaddr_type_is_valid(cp->addr.type))
5318 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 5675 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5319 MGMT_STATUS_INVALID_PARAMS, 5676 MGMT_STATUS_INVALID_PARAMS,
5320 &rp, sizeof(rp)); 5677 &rp, sizeof(rp));
5321 5678
5322 hci_dev_lock(hdev); 5679 hci_dev_lock(hdev);
5323 5680
5324 if (!hdev_is_powered(hdev)) { 5681 if (!hdev_is_powered(hdev)) {
5325 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 5682 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5326 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); 5683 MGMT_STATUS_NOT_POWERED, &rp,
5684 sizeof(rp));
5327 goto unlock; 5685 goto unlock;
5328 } 5686 }
5329 5687
@@ -5334,14 +5692,15 @@ static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5334 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr); 5692 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5335 5693
5336 if (!conn || conn->state != BT_CONNECTED) { 5694 if (!conn || conn->state != BT_CONNECTED) {
5337 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 5695 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5338 MGMT_STATUS_NOT_CONNECTED, &rp, sizeof(rp)); 5696 MGMT_STATUS_NOT_CONNECTED, &rp,
5697 sizeof(rp));
5339 goto unlock; 5698 goto unlock;
5340 } 5699 }
5341 5700
5342 if (mgmt_pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) { 5701 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
5343 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 5702 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5344 MGMT_STATUS_BUSY, &rp, sizeof(rp)); 5703 MGMT_STATUS_BUSY, &rp, sizeof(rp));
5345 goto unlock; 5704 goto unlock;
5346 } 5705 }
5347 5706
@@ -5361,7 +5720,7 @@ static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5361 struct hci_request req; 5720 struct hci_request req;
5362 struct hci_cp_read_tx_power req_txp_cp; 5721 struct hci_cp_read_tx_power req_txp_cp;
5363 struct hci_cp_read_rssi req_rssi_cp; 5722 struct hci_cp_read_rssi req_rssi_cp;
5364 struct pending_cmd *cmd; 5723 struct mgmt_pending_cmd *cmd;
5365 5724
5366 hci_req_init(&req, hdev); 5725 hci_req_init(&req, hdev);
5367 req_rssi_cp.handle = cpu_to_le16(conn->handle); 5726 req_rssi_cp.handle = cpu_to_le16(conn->handle);
@@ -5409,8 +5768,8 @@ static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5409 rp.tx_power = conn->tx_power; 5768 rp.tx_power = conn->tx_power;
5410 rp.max_tx_power = conn->max_tx_power; 5769 rp.max_tx_power = conn->max_tx_power;
5411 5770
5412 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO, 5771 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5413 MGMT_STATUS_SUCCESS, &rp, sizeof(rp)); 5772 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
5414 } 5773 }
5415 5774
5416unlock: 5775unlock:
@@ -5418,7 +5777,7 @@ unlock:
5418 return err; 5777 return err;
5419} 5778}
5420 5779
5421static int clock_info_cmd_complete(struct pending_cmd *cmd, u8 status) 5780static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
5422{ 5781{
5423 struct hci_conn *conn = cmd->user_data; 5782 struct hci_conn *conn = cmd->user_data;
5424 struct mgmt_rp_get_clock_info rp; 5783 struct mgmt_rp_get_clock_info rp;
@@ -5443,8 +5802,8 @@ static int clock_info_cmd_complete(struct pending_cmd *cmd, u8 status)
5443 } 5802 }
5444 5803
5445complete: 5804complete:
5446 err = cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp, 5805 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5447 sizeof(rp)); 5806 sizeof(rp));
5448 5807
5449 if (conn) { 5808 if (conn) {
5450 hci_conn_drop(conn); 5809 hci_conn_drop(conn);
@@ -5457,7 +5816,7 @@ complete:
5457static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode) 5816static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5458{ 5817{
5459 struct hci_cp_read_clock *hci_cp; 5818 struct hci_cp_read_clock *hci_cp;
5460 struct pending_cmd *cmd; 5819 struct mgmt_pending_cmd *cmd;
5461 struct hci_conn *conn; 5820 struct hci_conn *conn;
5462 5821
5463 BT_DBG("%s status %u", hdev->name, status); 5822 BT_DBG("%s status %u", hdev->name, status);
@@ -5475,7 +5834,7 @@ static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5475 conn = NULL; 5834 conn = NULL;
5476 } 5835 }
5477 5836
5478 cmd = mgmt_pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn); 5837 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
5479 if (!cmd) 5838 if (!cmd)
5480 goto unlock; 5839 goto unlock;
5481 5840
@@ -5492,7 +5851,7 @@ static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5492 struct mgmt_cp_get_clock_info *cp = data; 5851 struct mgmt_cp_get_clock_info *cp = data;
5493 struct mgmt_rp_get_clock_info rp; 5852 struct mgmt_rp_get_clock_info rp;
5494 struct hci_cp_read_clock hci_cp; 5853 struct hci_cp_read_clock hci_cp;
5495 struct pending_cmd *cmd; 5854 struct mgmt_pending_cmd *cmd;
5496 struct hci_request req; 5855 struct hci_request req;
5497 struct hci_conn *conn; 5856 struct hci_conn *conn;
5498 int err; 5857 int err;
@@ -5504,15 +5863,16 @@ static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5504 rp.addr.type = cp->addr.type; 5863 rp.addr.type = cp->addr.type;
5505 5864
5506 if (cp->addr.type != BDADDR_BREDR) 5865 if (cp->addr.type != BDADDR_BREDR)
5507 return cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, 5866 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5508 MGMT_STATUS_INVALID_PARAMS, 5867 MGMT_STATUS_INVALID_PARAMS,
5509 &rp, sizeof(rp)); 5868 &rp, sizeof(rp));
5510 5869
5511 hci_dev_lock(hdev); 5870 hci_dev_lock(hdev);
5512 5871
5513 if (!hdev_is_powered(hdev)) { 5872 if (!hdev_is_powered(hdev)) {
5514 err = cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO, 5873 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5515 MGMT_STATUS_NOT_POWERED, &rp, sizeof(rp)); 5874 MGMT_STATUS_NOT_POWERED, &rp,
5875 sizeof(rp));
5516 goto unlock; 5876 goto unlock;
5517 } 5877 }
5518 5878
@@ -5520,10 +5880,10 @@ static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5520 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, 5880 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5521 &cp->addr.bdaddr); 5881 &cp->addr.bdaddr);
5522 if (!conn || conn->state != BT_CONNECTED) { 5882 if (!conn || conn->state != BT_CONNECTED) {
5523 err = cmd_complete(sk, hdev->id, 5883 err = mgmt_cmd_complete(sk, hdev->id,
5524 MGMT_OP_GET_CLOCK_INFO, 5884 MGMT_OP_GET_CLOCK_INFO,
5525 MGMT_STATUS_NOT_CONNECTED, 5885 MGMT_STATUS_NOT_CONNECTED,
5526 &rp, sizeof(rp)); 5886 &rp, sizeof(rp));
5527 goto unlock; 5887 goto unlock;
5528 } 5888 }
5529 } else { 5889 } else {
@@ -5634,13 +5994,13 @@ static void device_added(struct sock *sk, struct hci_dev *hdev,
5634 5994
5635static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) 5995static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5636{ 5996{
5637 struct pending_cmd *cmd; 5997 struct mgmt_pending_cmd *cmd;
5638 5998
5639 BT_DBG("status 0x%02x", status); 5999 BT_DBG("status 0x%02x", status);
5640 6000
5641 hci_dev_lock(hdev); 6001 hci_dev_lock(hdev);
5642 6002
5643 cmd = mgmt_pending_find(MGMT_OP_ADD_DEVICE, hdev); 6003 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
5644 if (!cmd) 6004 if (!cmd)
5645 goto unlock; 6005 goto unlock;
5646 6006
@@ -5655,7 +6015,7 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
5655 void *data, u16 len) 6015 void *data, u16 len)
5656{ 6016{
5657 struct mgmt_cp_add_device *cp = data; 6017 struct mgmt_cp_add_device *cp = data;
5658 struct pending_cmd *cmd; 6018 struct mgmt_pending_cmd *cmd;
5659 struct hci_request req; 6019 struct hci_request req;
5660 u8 auto_conn, addr_type; 6020 u8 auto_conn, addr_type;
5661 int err; 6021 int err;
@@ -5664,14 +6024,14 @@ static int add_device(struct sock *sk, struct hci_dev *hdev,
5664 6024
5665 if (!bdaddr_type_is_valid(cp->addr.type) || 6025 if (!bdaddr_type_is_valid(cp->addr.type) ||
5666 !bacmp(&cp->addr.bdaddr, BDADDR_ANY)) 6026 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
5667 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 6027 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5668 MGMT_STATUS_INVALID_PARAMS, 6028 MGMT_STATUS_INVALID_PARAMS,
5669 &cp->addr, sizeof(cp->addr)); 6029 &cp->addr, sizeof(cp->addr));
5670 6030
5671 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02) 6031 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
5672 return cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE, 6032 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5673 MGMT_STATUS_INVALID_PARAMS, 6033 MGMT_STATUS_INVALID_PARAMS,
5674 &cp->addr, sizeof(cp->addr)); 6034 &cp->addr, sizeof(cp->addr));
5675 6035
5676 hci_req_init(&req, hdev); 6036 hci_req_init(&req, hdev);
5677 6037
@@ -5757,13 +6117,13 @@ static void device_removed(struct sock *sk, struct hci_dev *hdev,
5757 6117
5758static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode) 6118static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5759{ 6119{
5760 struct pending_cmd *cmd; 6120 struct mgmt_pending_cmd *cmd;
5761 6121
5762 BT_DBG("status 0x%02x", status); 6122 BT_DBG("status 0x%02x", status);
5763 6123
5764 hci_dev_lock(hdev); 6124 hci_dev_lock(hdev);
5765 6125
5766 cmd = mgmt_pending_find(MGMT_OP_REMOVE_DEVICE, hdev); 6126 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
5767 if (!cmd) 6127 if (!cmd)
5768 goto unlock; 6128 goto unlock;
5769 6129
@@ -5778,7 +6138,7 @@ static int remove_device(struct sock *sk, struct hci_dev *hdev,
5778 void *data, u16 len) 6138 void *data, u16 len)
5779{ 6139{
5780 struct mgmt_cp_remove_device *cp = data; 6140 struct mgmt_cp_remove_device *cp = data;
5781 struct pending_cmd *cmd; 6141 struct mgmt_pending_cmd *cmd;
5782 struct hci_request req; 6142 struct hci_request req;
5783 int err; 6143 int err;
5784 6144
@@ -5911,15 +6271,15 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5911 int i; 6271 int i;
5912 6272
5913 if (!lmp_le_capable(hdev)) 6273 if (!lmp_le_capable(hdev))
5914 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 6274 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5915 MGMT_STATUS_NOT_SUPPORTED); 6275 MGMT_STATUS_NOT_SUPPORTED);
5916 6276
5917 param_count = __le16_to_cpu(cp->param_count); 6277 param_count = __le16_to_cpu(cp->param_count);
5918 if (param_count > max_param_count) { 6278 if (param_count > max_param_count) {
5919 BT_ERR("load_conn_param: too big param_count value %u", 6279 BT_ERR("load_conn_param: too big param_count value %u",
5920 param_count); 6280 param_count);
5921 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 6281 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5922 MGMT_STATUS_INVALID_PARAMS); 6282 MGMT_STATUS_INVALID_PARAMS);
5923 } 6283 }
5924 6284
5925 expected_len = sizeof(*cp) + param_count * 6285 expected_len = sizeof(*cp) + param_count *
@@ -5927,8 +6287,8 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5927 if (expected_len != len) { 6287 if (expected_len != len) {
5928 BT_ERR("load_conn_param: expected %u bytes, got %u bytes", 6288 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
5929 expected_len, len); 6289 expected_len, len);
5930 return cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 6290 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
5931 MGMT_STATUS_INVALID_PARAMS); 6291 MGMT_STATUS_INVALID_PARAMS);
5932 } 6292 }
5933 6293
5934 BT_DBG("%s param_count %u", hdev->name, param_count); 6294 BT_DBG("%s param_count %u", hdev->name, param_count);
@@ -5983,7 +6343,8 @@ static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
5983 6343
5984 hci_dev_unlock(hdev); 6344 hci_dev_unlock(hdev);
5985 6345
5986 return cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0, NULL, 0); 6346 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6347 NULL, 0);
5987} 6348}
5988 6349
5989static int set_external_config(struct sock *sk, struct hci_dev *hdev, 6350static int set_external_config(struct sock *sk, struct hci_dev *hdev,
@@ -5996,25 +6357,23 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
5996 BT_DBG("%s", hdev->name); 6357 BT_DBG("%s", hdev->name);
5997 6358
5998 if (hdev_is_powered(hdev)) 6359 if (hdev_is_powered(hdev))
5999 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 6360 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6000 MGMT_STATUS_REJECTED); 6361 MGMT_STATUS_REJECTED);
6001 6362
6002 if (cp->config != 0x00 && cp->config != 0x01) 6363 if (cp->config != 0x00 && cp->config != 0x01)
6003 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 6364 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6004 MGMT_STATUS_INVALID_PARAMS); 6365 MGMT_STATUS_INVALID_PARAMS);
6005 6366
6006 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks)) 6367 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
6007 return cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG, 6368 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6008 MGMT_STATUS_NOT_SUPPORTED); 6369 MGMT_STATUS_NOT_SUPPORTED);
6009 6370
6010 hci_dev_lock(hdev); 6371 hci_dev_lock(hdev);
6011 6372
6012 if (cp->config) 6373 if (cp->config)
6013 changed = !test_and_set_bit(HCI_EXT_CONFIGURED, 6374 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
6014 &hdev->dev_flags);
6015 else 6375 else
6016 changed = test_and_clear_bit(HCI_EXT_CONFIGURED, 6376 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
6017 &hdev->dev_flags);
6018 6377
6019 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev); 6378 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6020 if (err < 0) 6379 if (err < 0)
@@ -6025,12 +6384,12 @@ static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6025 6384
6026 err = new_options(hdev, sk); 6385 err = new_options(hdev, sk);
6027 6386
6028 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) == is_configured(hdev)) { 6387 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
6029 mgmt_index_removed(hdev); 6388 mgmt_index_removed(hdev);
6030 6389
6031 if (test_and_change_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) { 6390 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
6032 set_bit(HCI_CONFIG, &hdev->dev_flags); 6391 hci_dev_set_flag(hdev, HCI_CONFIG);
6033 set_bit(HCI_AUTO_OFF, &hdev->dev_flags); 6392 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
6034 6393
6035 queue_work(hdev->req_workqueue, &hdev->power_on); 6394 queue_work(hdev->req_workqueue, &hdev->power_on);
6036 } else { 6395 } else {
@@ -6054,16 +6413,16 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6054 BT_DBG("%s", hdev->name); 6413 BT_DBG("%s", hdev->name);
6055 6414
6056 if (hdev_is_powered(hdev)) 6415 if (hdev_is_powered(hdev))
6057 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 6416 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6058 MGMT_STATUS_REJECTED); 6417 MGMT_STATUS_REJECTED);
6059 6418
6060 if (!bacmp(&cp->bdaddr, BDADDR_ANY)) 6419 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
6061 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 6420 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6062 MGMT_STATUS_INVALID_PARAMS); 6421 MGMT_STATUS_INVALID_PARAMS);
6063 6422
6064 if (!hdev->set_bdaddr) 6423 if (!hdev->set_bdaddr)
6065 return cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS, 6424 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6066 MGMT_STATUS_NOT_SUPPORTED); 6425 MGMT_STATUS_NOT_SUPPORTED);
6067 6426
6068 hci_dev_lock(hdev); 6427 hci_dev_lock(hdev);
6069 6428
@@ -6077,16 +6436,16 @@ static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6077 if (!changed) 6436 if (!changed)
6078 goto unlock; 6437 goto unlock;
6079 6438
6080 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 6439 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
6081 err = new_options(hdev, sk); 6440 err = new_options(hdev, sk);
6082 6441
6083 if (is_configured(hdev)) { 6442 if (is_configured(hdev)) {
6084 mgmt_index_removed(hdev); 6443 mgmt_index_removed(hdev);
6085 6444
6086 clear_bit(HCI_UNCONFIGURED, &hdev->dev_flags); 6445 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
6087 6446
6088 set_bit(HCI_CONFIG, &hdev->dev_flags); 6447 hci_dev_set_flag(hdev, HCI_CONFIG);
6089 set_bit(HCI_AUTO_OFF, &hdev->dev_flags); 6448 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
6090 6449
6091 queue_work(hdev->req_workqueue, &hdev->power_on); 6450 queue_work(hdev->req_workqueue, &hdev->power_on);
6092 } 6451 }
@@ -6096,213 +6455,852 @@ unlock:
6096 return err; 6455 return err;
6097} 6456}
6098 6457
6099static const struct mgmt_handler { 6458static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6100 int (*func) (struct sock *sk, struct hci_dev *hdev, void *data, 6459 u8 data_len)
6101 u16 data_len); 6460{
6102 bool var_len; 6461 eir[eir_len++] = sizeof(type) + data_len;
6103 size_t data_len; 6462 eir[eir_len++] = type;
6104} mgmt_handlers[] = { 6463 memcpy(&eir[eir_len], data, data_len);
6105 { NULL }, /* 0x0000 (no command) */ 6464 eir_len += data_len;
6106 { read_version, false, MGMT_READ_VERSION_SIZE },
6107 { read_commands, false, MGMT_READ_COMMANDS_SIZE },
6108 { read_index_list, false, MGMT_READ_INDEX_LIST_SIZE },
6109 { read_controller_info, false, MGMT_READ_INFO_SIZE },
6110 { set_powered, false, MGMT_SETTING_SIZE },
6111 { set_discoverable, false, MGMT_SET_DISCOVERABLE_SIZE },
6112 { set_connectable, false, MGMT_SETTING_SIZE },
6113 { set_fast_connectable, false, MGMT_SETTING_SIZE },
6114 { set_bondable, false, MGMT_SETTING_SIZE },
6115 { set_link_security, false, MGMT_SETTING_SIZE },
6116 { set_ssp, false, MGMT_SETTING_SIZE },
6117 { set_hs, false, MGMT_SETTING_SIZE },
6118 { set_le, false, MGMT_SETTING_SIZE },
6119 { set_dev_class, false, MGMT_SET_DEV_CLASS_SIZE },
6120 { set_local_name, false, MGMT_SET_LOCAL_NAME_SIZE },
6121 { add_uuid, false, MGMT_ADD_UUID_SIZE },
6122 { remove_uuid, false, MGMT_REMOVE_UUID_SIZE },
6123 { load_link_keys, true, MGMT_LOAD_LINK_KEYS_SIZE },
6124 { load_long_term_keys, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE },
6125 { disconnect, false, MGMT_DISCONNECT_SIZE },
6126 { get_connections, false, MGMT_GET_CONNECTIONS_SIZE },
6127 { pin_code_reply, false, MGMT_PIN_CODE_REPLY_SIZE },
6128 { pin_code_neg_reply, false, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6129 { set_io_capability, false, MGMT_SET_IO_CAPABILITY_SIZE },
6130 { pair_device, false, MGMT_PAIR_DEVICE_SIZE },
6131 { cancel_pair_device, false, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6132 { unpair_device, false, MGMT_UNPAIR_DEVICE_SIZE },
6133 { user_confirm_reply, false, MGMT_USER_CONFIRM_REPLY_SIZE },
6134 { user_confirm_neg_reply, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6135 { user_passkey_reply, false, MGMT_USER_PASSKEY_REPLY_SIZE },
6136 { user_passkey_neg_reply, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
6137 { read_local_oob_data, false, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6138 { add_remote_oob_data, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE },
6139 { remove_remote_oob_data, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6140 { start_discovery, false, MGMT_START_DISCOVERY_SIZE },
6141 { stop_discovery, false, MGMT_STOP_DISCOVERY_SIZE },
6142 { confirm_name, false, MGMT_CONFIRM_NAME_SIZE },
6143 { block_device, false, MGMT_BLOCK_DEVICE_SIZE },
6144 { unblock_device, false, MGMT_UNBLOCK_DEVICE_SIZE },
6145 { set_device_id, false, MGMT_SET_DEVICE_ID_SIZE },
6146 { set_advertising, false, MGMT_SETTING_SIZE },
6147 { set_bredr, false, MGMT_SETTING_SIZE },
6148 { set_static_address, false, MGMT_SET_STATIC_ADDRESS_SIZE },
6149 { set_scan_params, false, MGMT_SET_SCAN_PARAMS_SIZE },
6150 { set_secure_conn, false, MGMT_SETTING_SIZE },
6151 { set_debug_keys, false, MGMT_SETTING_SIZE },
6152 { set_privacy, false, MGMT_SET_PRIVACY_SIZE },
6153 { load_irks, true, MGMT_LOAD_IRKS_SIZE },
6154 { get_conn_info, false, MGMT_GET_CONN_INFO_SIZE },
6155 { get_clock_info, false, MGMT_GET_CLOCK_INFO_SIZE },
6156 { add_device, false, MGMT_ADD_DEVICE_SIZE },
6157 { remove_device, false, MGMT_REMOVE_DEVICE_SIZE },
6158 { load_conn_param, true, MGMT_LOAD_CONN_PARAM_SIZE },
6159 { read_unconf_index_list, false, MGMT_READ_UNCONF_INDEX_LIST_SIZE },
6160 { read_config_info, false, MGMT_READ_CONFIG_INFO_SIZE },
6161 { set_external_config, false, MGMT_SET_EXTERNAL_CONFIG_SIZE },
6162 { set_public_address, false, MGMT_SET_PUBLIC_ADDRESS_SIZE },
6163 { start_service_discovery,true, MGMT_START_SERVICE_DISCOVERY_SIZE },
6164};
6165 6465
6166int mgmt_control(struct sock *sk, struct msghdr *msg, size_t msglen) 6466 return eir_len;
6467}
6468
6469static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6470 u16 opcode, struct sk_buff *skb)
6167{ 6471{
6168 void *buf; 6472 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
6169 u8 *cp; 6473 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
6170 struct mgmt_hdr *hdr; 6474 u8 *h192, *r192, *h256, *r256;
6171 u16 opcode, index, len; 6475 struct mgmt_pending_cmd *cmd;
6172 struct hci_dev *hdev = NULL; 6476 u16 eir_len;
6173 const struct mgmt_handler *handler;
6174 int err; 6477 int err;
6175 6478
6176 BT_DBG("got %zu bytes", msglen); 6479 BT_DBG("%s status %u", hdev->name, status);
6177 6480
6178 if (msglen < sizeof(*hdr)) 6481 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6179 return -EINVAL; 6482 if (!cmd)
6483 return;
6180 6484
6181 buf = kmalloc(msglen, GFP_KERNEL); 6485 mgmt_cp = cmd->param;
6182 if (!buf) 6486
6183 return -ENOMEM; 6487 if (status) {
6488 status = mgmt_status(status);
6489 eir_len = 0;
6490
6491 h192 = NULL;
6492 r192 = NULL;
6493 h256 = NULL;
6494 r256 = NULL;
6495 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
6496 struct hci_rp_read_local_oob_data *rp;
6497
6498 if (skb->len != sizeof(*rp)) {
6499 status = MGMT_STATUS_FAILED;
6500 eir_len = 0;
6501 } else {
6502 status = MGMT_STATUS_SUCCESS;
6503 rp = (void *)skb->data;
6184 6504
6185 if (memcpy_from_msg(buf, msg, msglen)) { 6505 eir_len = 5 + 18 + 18;
6186 err = -EFAULT; 6506 h192 = rp->hash;
6507 r192 = rp->rand;
6508 h256 = NULL;
6509 r256 = NULL;
6510 }
6511 } else {
6512 struct hci_rp_read_local_oob_ext_data *rp;
6513
6514 if (skb->len != sizeof(*rp)) {
6515 status = MGMT_STATUS_FAILED;
6516 eir_len = 0;
6517 } else {
6518 status = MGMT_STATUS_SUCCESS;
6519 rp = (void *)skb->data;
6520
6521 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6522 eir_len = 5 + 18 + 18;
6523 h192 = NULL;
6524 r192 = NULL;
6525 } else {
6526 eir_len = 5 + 18 + 18 + 18 + 18;
6527 h192 = rp->hash192;
6528 r192 = rp->rand192;
6529 }
6530
6531 h256 = rp->hash256;
6532 r256 = rp->rand256;
6533 }
6534 }
6535
6536 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
6537 if (!mgmt_rp)
6187 goto done; 6538 goto done;
6539
6540 if (status)
6541 goto send_rsp;
6542
6543 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
6544 hdev->dev_class, 3);
6545
6546 if (h192 && r192) {
6547 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6548 EIR_SSP_HASH_C192, h192, 16);
6549 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6550 EIR_SSP_RAND_R192, r192, 16);
6551 }
6552
6553 if (h256 && r256) {
6554 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6555 EIR_SSP_HASH_C256, h256, 16);
6556 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6557 EIR_SSP_RAND_R256, r256, 16);
6188 } 6558 }
6189 6559
6190 hdr = buf; 6560send_rsp:
6191 opcode = __le16_to_cpu(hdr->opcode); 6561 mgmt_rp->type = mgmt_cp->type;
6192 index = __le16_to_cpu(hdr->index); 6562 mgmt_rp->eir_len = cpu_to_le16(eir_len);
6193 len = __le16_to_cpu(hdr->len);
6194 6563
6195 if (len != msglen - sizeof(*hdr)) { 6564 err = mgmt_cmd_complete(cmd->sk, hdev->id,
6196 err = -EINVAL; 6565 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
6566 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
6567 if (err < 0 || status)
6197 goto done; 6568 goto done;
6569
6570 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
6571
6572 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6573 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
6574 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
6575done:
6576 kfree(mgmt_rp);
6577 mgmt_pending_remove(cmd);
6578}
6579
6580static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
6581 struct mgmt_cp_read_local_oob_ext_data *cp)
6582{
6583 struct mgmt_pending_cmd *cmd;
6584 struct hci_request req;
6585 int err;
6586
6587 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
6588 cp, sizeof(*cp));
6589 if (!cmd)
6590 return -ENOMEM;
6591
6592 hci_req_init(&req, hdev);
6593
6594 if (bredr_sc_enabled(hdev))
6595 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
6596 else
6597 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
6598
6599 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
6600 if (err < 0) {
6601 mgmt_pending_remove(cmd);
6602 return err;
6198 } 6603 }
6199 6604
6200 if (index != MGMT_INDEX_NONE) { 6605 return 0;
6201 hdev = hci_dev_get(index); 6606}
6202 if (!hdev) { 6607
6203 err = cmd_status(sk, index, opcode, 6608static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6204 MGMT_STATUS_INVALID_INDEX); 6609 void *data, u16 data_len)
6205 goto done; 6610{
6611 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6612 struct mgmt_rp_read_local_oob_ext_data *rp;
6613 size_t rp_len;
6614 u16 eir_len;
6615 u8 status, flags, role, addr[7], hash[16], rand[16];
6616 int err;
6617
6618 BT_DBG("%s", hdev->name);
6619
6620 if (hdev_is_powered(hdev)) {
6621 switch (cp->type) {
6622 case BIT(BDADDR_BREDR):
6623 status = mgmt_bredr_support(hdev);
6624 if (status)
6625 eir_len = 0;
6626 else
6627 eir_len = 5;
6628 break;
6629 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6630 status = mgmt_le_support(hdev);
6631 if (status)
6632 eir_len = 0;
6633 else
6634 eir_len = 9 + 3 + 18 + 18 + 3;
6635 break;
6636 default:
6637 status = MGMT_STATUS_INVALID_PARAMS;
6638 eir_len = 0;
6639 break;
6206 } 6640 }
6641 } else {
6642 status = MGMT_STATUS_NOT_POWERED;
6643 eir_len = 0;
6644 }
6207 6645
6208 if (test_bit(HCI_SETUP, &hdev->dev_flags) || 6646 rp_len = sizeof(*rp) + eir_len;
6209 test_bit(HCI_CONFIG, &hdev->dev_flags) || 6647 rp = kmalloc(rp_len, GFP_ATOMIC);
6210 test_bit(HCI_USER_CHANNEL, &hdev->dev_flags)) { 6648 if (!rp)
6211 err = cmd_status(sk, index, opcode, 6649 return -ENOMEM;
6212 MGMT_STATUS_INVALID_INDEX); 6650
6213 goto done; 6651 if (status)
6652 goto complete;
6653
6654 hci_dev_lock(hdev);
6655
6656 eir_len = 0;
6657 switch (cp->type) {
6658 case BIT(BDADDR_BREDR):
6659 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6660 err = read_local_ssp_oob_req(hdev, sk, cp);
6661 hci_dev_unlock(hdev);
6662 if (!err)
6663 goto done;
6664
6665 status = MGMT_STATUS_FAILED;
6666 goto complete;
6667 } else {
6668 eir_len = eir_append_data(rp->eir, eir_len,
6669 EIR_CLASS_OF_DEV,
6670 hdev->dev_class, 3);
6671 }
6672 break;
6673 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6674 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6675 smp_generate_oob(hdev, hash, rand) < 0) {
6676 hci_dev_unlock(hdev);
6677 status = MGMT_STATUS_FAILED;
6678 goto complete;
6679 }
6680
6681 /* This should return the active RPA, but since the RPA
6682 * is only programmed on demand, it is really hard to fill
6683 * this in at the moment. For now disallow retrieving
6684 * local out-of-band data when privacy is in use.
6685 *
6686 * Returning the identity address will not help here since
6687 * pairing happens before the identity resolving key is
6688 * known and thus the connection establishment happens
6689 * based on the RPA and not the identity address.
6690 */
6691 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6692 hci_dev_unlock(hdev);
6693 status = MGMT_STATUS_REJECTED;
6694 goto complete;
6214 } 6695 }
6215 6696
6216 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags) && 6697 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6217 opcode != MGMT_OP_READ_CONFIG_INFO && 6698 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6218 opcode != MGMT_OP_SET_EXTERNAL_CONFIG && 6699 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6219 opcode != MGMT_OP_SET_PUBLIC_ADDRESS) { 6700 bacmp(&hdev->static_addr, BDADDR_ANY))) {
6220 err = cmd_status(sk, index, opcode, 6701 memcpy(addr, &hdev->static_addr, 6);
6221 MGMT_STATUS_INVALID_INDEX); 6702 addr[6] = 0x01;
6222 goto done; 6703 } else {
6704 memcpy(addr, &hdev->bdaddr, 6);
6705 addr[6] = 0x00;
6706 }
6707
6708 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6709 addr, sizeof(addr));
6710
6711 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6712 role = 0x02;
6713 else
6714 role = 0x01;
6715
6716 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6717 &role, sizeof(role));
6718
6719 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6720 eir_len = eir_append_data(rp->eir, eir_len,
6721 EIR_LE_SC_CONFIRM,
6722 hash, sizeof(hash));
6723
6724 eir_len = eir_append_data(rp->eir, eir_len,
6725 EIR_LE_SC_RANDOM,
6726 rand, sizeof(rand));
6223 } 6727 }
6728
6729 flags = get_adv_discov_flags(hdev);
6730
6731 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6732 flags |= LE_AD_NO_BREDR;
6733
6734 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6735 &flags, sizeof(flags));
6736 break;
6224 } 6737 }
6225 6738
6226 if (opcode >= ARRAY_SIZE(mgmt_handlers) || 6739 hci_dev_unlock(hdev);
6227 mgmt_handlers[opcode].func == NULL) { 6740
6228 BT_DBG("Unknown op %u", opcode); 6741 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6229 err = cmd_status(sk, index, opcode, 6742
6230 MGMT_STATUS_UNKNOWN_COMMAND); 6743 status = MGMT_STATUS_SUCCESS;
6744
6745complete:
6746 rp->type = cp->type;
6747 rp->eir_len = cpu_to_le16(eir_len);
6748
6749 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6750 status, rp, sizeof(*rp) + eir_len);
6751 if (err < 0 || status)
6231 goto done; 6752 goto done;
6753
6754 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6755 rp, sizeof(*rp) + eir_len,
6756 HCI_MGMT_OOB_DATA_EVENTS, sk);
6757
6758done:
6759 kfree(rp);
6760
6761 return err;
6762}
6763
6764static u32 get_supported_adv_flags(struct hci_dev *hdev)
6765{
6766 u32 flags = 0;
6767
6768 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6769 flags |= MGMT_ADV_FLAG_DISCOV;
6770 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6771 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6772
6773 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6774 flags |= MGMT_ADV_FLAG_TX_POWER;
6775
6776 return flags;
6777}
6778
6779static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6780 void *data, u16 data_len)
6781{
6782 struct mgmt_rp_read_adv_features *rp;
6783 size_t rp_len;
6784 int err;
6785 bool instance;
6786 u32 supported_flags;
6787
6788 BT_DBG("%s", hdev->name);
6789
6790 if (!lmp_le_capable(hdev))
6791 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6792 MGMT_STATUS_REJECTED);
6793
6794 hci_dev_lock(hdev);
6795
6796 rp_len = sizeof(*rp);
6797
6798 /* Currently only one instance is supported, so just add 1 to the
6799 * response length.
6800 */
6801 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6802 if (instance)
6803 rp_len++;
6804
6805 rp = kmalloc(rp_len, GFP_ATOMIC);
6806 if (!rp) {
6807 hci_dev_unlock(hdev);
6808 return -ENOMEM;
6232 } 6809 }
6233 6810
6234 if (hdev && (opcode <= MGMT_OP_READ_INDEX_LIST || 6811 supported_flags = get_supported_adv_flags(hdev);
6235 opcode == MGMT_OP_READ_UNCONF_INDEX_LIST)) { 6812
6236 err = cmd_status(sk, index, opcode, 6813 rp->supported_flags = cpu_to_le32(supported_flags);
6237 MGMT_STATUS_INVALID_INDEX); 6814 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6238 goto done; 6815 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
6816 rp->max_instances = 1;
6817
6818 /* Currently only one instance is supported, so simply return the
6819 * current instance number.
6820 */
6821 if (instance) {
6822 rp->num_instances = 1;
6823 rp->instance[0] = 1;
6824 } else {
6825 rp->num_instances = 0;
6239 } 6826 }
6240 6827
6241 if (!hdev && (opcode > MGMT_OP_READ_INDEX_LIST && 6828 hci_dev_unlock(hdev);
6242 opcode != MGMT_OP_READ_UNCONF_INDEX_LIST)) { 6829
6243 err = cmd_status(sk, index, opcode, 6830 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6244 MGMT_STATUS_INVALID_INDEX); 6831 MGMT_STATUS_SUCCESS, rp, rp_len);
6245 goto done; 6832
6833 kfree(rp);
6834
6835 return err;
6836}
6837
6838static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
6839 u8 len, bool is_adv_data)
6840{
6841 u8 max_len = HCI_MAX_AD_LENGTH;
6842 int i, cur_len;
6843 bool flags_managed = false;
6844 bool tx_power_managed = false;
6845 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
6846 MGMT_ADV_FLAG_MANAGED_FLAGS;
6847
6848 if (is_adv_data && (adv_flags & flags_params)) {
6849 flags_managed = true;
6850 max_len -= 3;
6246 } 6851 }
6247 6852
6248 handler = &mgmt_handlers[opcode]; 6853 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
6854 tx_power_managed = true;
6855 max_len -= 3;
6856 }
6249 6857
6250 if ((handler->var_len && len < handler->data_len) || 6858 if (len > max_len)
6251 (!handler->var_len && len != handler->data_len)) { 6859 return false;
6252 err = cmd_status(sk, index, opcode, 6860
6253 MGMT_STATUS_INVALID_PARAMS); 6861 /* Make sure that the data is correctly formatted. */
6254 goto done; 6862 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
6863 cur_len = data[i];
6864
6865 if (flags_managed && data[i + 1] == EIR_FLAGS)
6866 return false;
6867
6868 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
6869 return false;
6870
6871 /* If the current field length would exceed the total data
6872 * length, then it's invalid.
6873 */
6874 if (i + cur_len >= len)
6875 return false;
6255 } 6876 }
6256 6877
6257 if (hdev) 6878 return true;
6258 mgmt_init_hdev(sk, hdev); 6879}
6880
6881static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6882 u16 opcode)
6883{
6884 struct mgmt_pending_cmd *cmd;
6885 struct mgmt_rp_add_advertising rp;
6886
6887 BT_DBG("status %d", status);
6888
6889 hci_dev_lock(hdev);
6890
6891 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6892
6893 if (status) {
6894 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6895 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6896 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6897 }
6898
6899 if (!cmd)
6900 goto unlock;
6259 6901
6260 cp = buf + sizeof(*hdr); 6902 rp.instance = 0x01;
6903
6904 if (status)
6905 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6906 mgmt_status(status));
6907 else
6908 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6909 mgmt_status(status), &rp, sizeof(rp));
6910
6911 mgmt_pending_remove(cmd);
6912
6913unlock:
6914 hci_dev_unlock(hdev);
6915}
6916
6917static void adv_timeout_expired(struct work_struct *work)
6918{
6919 struct hci_dev *hdev = container_of(work, struct hci_dev,
6920 adv_instance.timeout_exp.work);
6921
6922 hdev->adv_instance.timeout = 0;
6923
6924 hci_dev_lock(hdev);
6925 clear_adv_instance(hdev);
6926 hci_dev_unlock(hdev);
6927}
6928
6929static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6930 void *data, u16 data_len)
6931{
6932 struct mgmt_cp_add_advertising *cp = data;
6933 struct mgmt_rp_add_advertising rp;
6934 u32 flags;
6935 u32 supported_flags;
6936 u8 status;
6937 u16 timeout;
6938 int err;
6939 struct mgmt_pending_cmd *cmd;
6940 struct hci_request req;
6941
6942 BT_DBG("%s", hdev->name);
6943
6944 status = mgmt_le_support(hdev);
6945 if (status)
6946 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6947 status);
6261 6948
6262 err = handler->func(sk, hdev, cp, len); 6949 flags = __le32_to_cpu(cp->flags);
6950 timeout = __le16_to_cpu(cp->timeout);
6951
6952 /* The current implementation only supports adding one instance and only
6953 * a subset of the specified flags.
6954 */
6955 supported_flags = get_supported_adv_flags(hdev);
6956 if (cp->instance != 0x01 || (flags & ~supported_flags))
6957 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6958 MGMT_STATUS_INVALID_PARAMS);
6959
6960 hci_dev_lock(hdev);
6961
6962 if (timeout && !hdev_is_powered(hdev)) {
6963 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6964 MGMT_STATUS_REJECTED);
6965 goto unlock;
6966 }
6967
6968 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6969 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
6970 pending_find(MGMT_OP_SET_LE, hdev)) {
6971 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6972 MGMT_STATUS_BUSY);
6973 goto unlock;
6974 }
6975
6976 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
6977 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
6978 cp->scan_rsp_len, false)) {
6979 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6980 MGMT_STATUS_INVALID_PARAMS);
6981 goto unlock;
6982 }
6983
6984 INIT_DELAYED_WORK(&hdev->adv_instance.timeout_exp, adv_timeout_expired);
6985
6986 hdev->adv_instance.flags = flags;
6987 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6988 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6989
6990 if (cp->adv_data_len)
6991 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6992
6993 if (cp->scan_rsp_len)
6994 memcpy(hdev->adv_instance.scan_rsp_data,
6995 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6996
6997 if (hdev->adv_instance.timeout)
6998 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
6999
7000 hdev->adv_instance.timeout = timeout;
7001
7002 if (timeout)
7003 queue_delayed_work(hdev->workqueue,
7004 &hdev->adv_instance.timeout_exp,
7005 msecs_to_jiffies(timeout * 1000));
7006
7007 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
7008 advertising_added(sk, hdev, 1);
7009
7010 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
7011 * we have no HCI communication to make. Simply return.
7012 */
7013 if (!hdev_is_powered(hdev) ||
7014 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7015 rp.instance = 0x01;
7016 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7017 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7018 goto unlock;
7019 }
7020
7021 /* We're good to go, update advertising data, parameters, and start
7022 * advertising.
7023 */
7024 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7025 data_len);
7026 if (!cmd) {
7027 err = -ENOMEM;
7028 goto unlock;
7029 }
7030
7031 hci_req_init(&req, hdev);
7032
7033 update_adv_data(&req);
7034 update_scan_rsp_data(&req);
7035 enable_advertising(&req);
7036
7037 err = hci_req_run(&req, add_advertising_complete);
6263 if (err < 0) 7038 if (err < 0)
6264 goto done; 7039 mgmt_pending_remove(cmd);
7040
7041unlock:
7042 hci_dev_unlock(hdev);
6265 7043
6266 err = msglen; 7044 return err;
7045}
6267 7046
6268done: 7047static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
6269 if (hdev) 7048 u16 opcode)
6270 hci_dev_put(hdev); 7049{
7050 struct mgmt_pending_cmd *cmd;
7051 struct mgmt_rp_remove_advertising rp;
7052
7053 BT_DBG("status %d", status);
7054
7055 hci_dev_lock(hdev);
7056
7057 /* A failure status here only means that we failed to disable
7058 * advertising. Otherwise, the advertising instance has been removed,
7059 * so report success.
7060 */
7061 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
7062 if (!cmd)
7063 goto unlock;
7064
7065 rp.instance = 1;
7066
7067 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
7068 &rp, sizeof(rp));
7069 mgmt_pending_remove(cmd);
7070
7071unlock:
7072 hci_dev_unlock(hdev);
7073}
7074
7075static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
7076 void *data, u16 data_len)
7077{
7078 struct mgmt_cp_remove_advertising *cp = data;
7079 struct mgmt_rp_remove_advertising rp;
7080 int err;
7081 struct mgmt_pending_cmd *cmd;
7082 struct hci_request req;
7083
7084 BT_DBG("%s", hdev->name);
7085
7086 /* The current implementation only allows modifying instance no 1. A
7087 * value of 0 indicates that all instances should be cleared.
7088 */
7089 if (cp->instance > 1)
7090 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7091 MGMT_STATUS_INVALID_PARAMS);
7092
7093 hci_dev_lock(hdev);
7094
7095 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7096 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7097 pending_find(MGMT_OP_SET_LE, hdev)) {
7098 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7099 MGMT_STATUS_BUSY);
7100 goto unlock;
7101 }
7102
7103 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
7104 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7105 MGMT_STATUS_INVALID_PARAMS);
7106 goto unlock;
7107 }
7108
7109 if (hdev->adv_instance.timeout)
7110 cancel_delayed_work(&hdev->adv_instance.timeout_exp);
7111
7112 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
7113
7114 advertising_removed(sk, hdev, 1);
7115
7116 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
7117
7118 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
7119 * we have no HCI communication to make. Simply return.
7120 */
7121 if (!hdev_is_powered(hdev) ||
7122 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
7123 rp.instance = 1;
7124 err = mgmt_cmd_complete(sk, hdev->id,
7125 MGMT_OP_REMOVE_ADVERTISING,
7126 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7127 goto unlock;
7128 }
7129
7130 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7131 data_len);
7132 if (!cmd) {
7133 err = -ENOMEM;
7134 goto unlock;
7135 }
7136
7137 hci_req_init(&req, hdev);
7138 disable_advertising(&req);
7139
7140 err = hci_req_run(&req, remove_advertising_complete);
7141 if (err < 0)
7142 mgmt_pending_remove(cmd);
7143
7144unlock:
7145 hci_dev_unlock(hdev);
6271 7146
6272 kfree(buf);
6273 return err; 7147 return err;
6274} 7148}
6275 7149
7150static const struct hci_mgmt_handler mgmt_handlers[] = {
7151 { NULL }, /* 0x0000 (no command) */
7152 { read_version, MGMT_READ_VERSION_SIZE,
7153 HCI_MGMT_NO_HDEV |
7154 HCI_MGMT_UNTRUSTED },
7155 { read_commands, MGMT_READ_COMMANDS_SIZE,
7156 HCI_MGMT_NO_HDEV |
7157 HCI_MGMT_UNTRUSTED },
7158 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
7159 HCI_MGMT_NO_HDEV |
7160 HCI_MGMT_UNTRUSTED },
7161 { read_controller_info, MGMT_READ_INFO_SIZE,
7162 HCI_MGMT_UNTRUSTED },
7163 { set_powered, MGMT_SETTING_SIZE },
7164 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
7165 { set_connectable, MGMT_SETTING_SIZE },
7166 { set_fast_connectable, MGMT_SETTING_SIZE },
7167 { set_bondable, MGMT_SETTING_SIZE },
7168 { set_link_security, MGMT_SETTING_SIZE },
7169 { set_ssp, MGMT_SETTING_SIZE },
7170 { set_hs, MGMT_SETTING_SIZE },
7171 { set_le, MGMT_SETTING_SIZE },
7172 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
7173 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
7174 { add_uuid, MGMT_ADD_UUID_SIZE },
7175 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
7176 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
7177 HCI_MGMT_VAR_LEN },
7178 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
7179 HCI_MGMT_VAR_LEN },
7180 { disconnect, MGMT_DISCONNECT_SIZE },
7181 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
7182 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
7183 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
7184 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
7185 { pair_device, MGMT_PAIR_DEVICE_SIZE },
7186 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
7187 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
7188 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
7189 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
7190 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
7191 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
7192 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
7193 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
7194 HCI_MGMT_VAR_LEN },
7195 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
7196 { start_discovery, MGMT_START_DISCOVERY_SIZE },
7197 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
7198 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
7199 { block_device, MGMT_BLOCK_DEVICE_SIZE },
7200 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
7201 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
7202 { set_advertising, MGMT_SETTING_SIZE },
7203 { set_bredr, MGMT_SETTING_SIZE },
7204 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
7205 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
7206 { set_secure_conn, MGMT_SETTING_SIZE },
7207 { set_debug_keys, MGMT_SETTING_SIZE },
7208 { set_privacy, MGMT_SET_PRIVACY_SIZE },
7209 { load_irks, MGMT_LOAD_IRKS_SIZE,
7210 HCI_MGMT_VAR_LEN },
7211 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
7212 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
7213 { add_device, MGMT_ADD_DEVICE_SIZE },
7214 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
7215 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
7216 HCI_MGMT_VAR_LEN },
7217 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
7218 HCI_MGMT_NO_HDEV |
7219 HCI_MGMT_UNTRUSTED },
7220 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
7221 HCI_MGMT_UNCONFIGURED |
7222 HCI_MGMT_UNTRUSTED },
7223 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7224 HCI_MGMT_UNCONFIGURED },
7225 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7226 HCI_MGMT_UNCONFIGURED },
7227 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7228 HCI_MGMT_VAR_LEN },
7229 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
7230 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
7231 HCI_MGMT_NO_HDEV |
7232 HCI_MGMT_UNTRUSTED },
7233 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
7234 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7235 HCI_MGMT_VAR_LEN },
7236 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
7237};
7238
6276void mgmt_index_added(struct hci_dev *hdev) 7239void mgmt_index_added(struct hci_dev *hdev)
6277{ 7240{
6278 if (hdev->dev_type != HCI_BREDR) 7241 struct mgmt_ev_ext_index ev;
6279 return;
6280 7242
6281 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 7243 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6282 return; 7244 return;
6283 7245
6284 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 7246 switch (hdev->dev_type) {
6285 mgmt_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev, NULL, 0, NULL); 7247 case HCI_BREDR:
6286 else 7248 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6287 mgmt_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0, NULL); 7249 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7250 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
7251 ev.type = 0x01;
7252 } else {
7253 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7254 HCI_MGMT_INDEX_EVENTS);
7255 ev.type = 0x00;
7256 }
7257 break;
7258 case HCI_AMP:
7259 ev.type = 0x02;
7260 break;
7261 default:
7262 return;
7263 }
7264
7265 ev.bus = hdev->bus;
7266
7267 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7268 HCI_MGMT_EXT_INDEX_EVENTS);
6288} 7269}
6289 7270
6290void mgmt_index_removed(struct hci_dev *hdev) 7271void mgmt_index_removed(struct hci_dev *hdev)
6291{ 7272{
7273 struct mgmt_ev_ext_index ev;
6292 u8 status = MGMT_STATUS_INVALID_INDEX; 7274 u8 status = MGMT_STATUS_INVALID_INDEX;
6293 7275
6294 if (hdev->dev_type != HCI_BREDR) 7276 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6295 return; 7277 return;
6296 7278
6297 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks)) 7279 switch (hdev->dev_type) {
7280 case HCI_BREDR:
7281 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
7282
7283 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7284 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7285 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
7286 ev.type = 0x01;
7287 } else {
7288 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7289 HCI_MGMT_INDEX_EVENTS);
7290 ev.type = 0x00;
7291 }
7292 break;
7293 case HCI_AMP:
7294 ev.type = 0x02;
7295 break;
7296 default:
6298 return; 7297 return;
7298 }
6299 7299
6300 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); 7300 ev.bus = hdev->bus;
6301 7301
6302 if (test_bit(HCI_UNCONFIGURED, &hdev->dev_flags)) 7302 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
6303 mgmt_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev, NULL, 0, NULL); 7303 HCI_MGMT_EXT_INDEX_EVENTS);
6304 else
6305 mgmt_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0, NULL);
6306} 7304}
6307 7305
6308/* This function requires the caller holds hdev->lock */ 7306/* This function requires the caller holds hdev->lock */
@@ -6367,7 +7365,7 @@ static int powered_update_hci(struct hci_dev *hdev)
6367 7365
6368 hci_req_init(&req, hdev); 7366 hci_req_init(&req, hdev);
6369 7367
6370 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags) && 7368 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
6371 !lmp_host_ssp_capable(hdev)) { 7369 !lmp_host_ssp_capable(hdev)) {
6372 u8 mode = 0x01; 7370 u8 mode = 0x01;
6373 7371
@@ -6381,7 +7379,7 @@ static int powered_update_hci(struct hci_dev *hdev)
6381 } 7379 }
6382 } 7380 }
6383 7381
6384 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags) && 7382 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
6385 lmp_bredr_capable(hdev)) { 7383 lmp_bredr_capable(hdev)) {
6386 struct hci_cp_write_le_host_supported cp; 7384 struct hci_cp_write_le_host_supported cp;
6387 7385
@@ -6402,24 +7400,28 @@ static int powered_update_hci(struct hci_dev *hdev)
6402 * advertising data. This also applies to the case 7400 * advertising data. This also applies to the case
6403 * where BR/EDR was toggled during the AUTO_OFF phase. 7401 * where BR/EDR was toggled during the AUTO_OFF phase.
6404 */ 7402 */
6405 if (test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) { 7403 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
6406 update_adv_data(&req); 7404 update_adv_data(&req);
6407 update_scan_rsp_data(&req); 7405 update_scan_rsp_data(&req);
6408 } 7406 }
6409 7407
6410 if (test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 7408 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7409 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
6411 enable_advertising(&req); 7410 enable_advertising(&req);
6412 7411
6413 restart_le_actions(&req); 7412 restart_le_actions(&req);
6414 } 7413 }
6415 7414
6416 link_sec = test_bit(HCI_LINK_SECURITY, &hdev->dev_flags); 7415 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
6417 if (link_sec != test_bit(HCI_AUTH, &hdev->flags)) 7416 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
6418 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE, 7417 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
6419 sizeof(link_sec), &link_sec); 7418 sizeof(link_sec), &link_sec);
6420 7419
6421 if (lmp_bredr_capable(hdev)) { 7420 if (lmp_bredr_capable(hdev)) {
6422 write_fast_connectable(&req, false); 7421 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
7422 write_fast_connectable(&req, true);
7423 else
7424 write_fast_connectable(&req, false);
6423 __hci_update_page_scan(&req); 7425 __hci_update_page_scan(&req);
6424 update_class(&req); 7426 update_class(&req);
6425 update_name(&req); 7427 update_name(&req);
@@ -6435,7 +7437,7 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
6435 u8 status, zero_cod[] = { 0, 0, 0 }; 7437 u8 status, zero_cod[] = { 0, 0, 0 };
6436 int err; 7438 int err;
6437 7439
6438 if (!test_bit(HCI_MGMT, &hdev->dev_flags)) 7440 if (!hci_dev_test_flag(hdev, HCI_MGMT))
6439 return 0; 7441 return 0;
6440 7442
6441 if (powered) { 7443 if (powered) {
@@ -6456,7 +7458,7 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
6456 * been triggered, potentially causing misleading DISCONNECTED 7458 * been triggered, potentially causing misleading DISCONNECTED
6457 * status responses. 7459 * status responses.
6458 */ 7460 */
6459 if (test_bit(HCI_UNREGISTER, &hdev->dev_flags)) 7461 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
6460 status = MGMT_STATUS_INVALID_INDEX; 7462 status = MGMT_STATUS_INVALID_INDEX;
6461 else 7463 else
6462 status = MGMT_STATUS_NOT_POWERED; 7464 status = MGMT_STATUS_NOT_POWERED;
@@ -6464,8 +7466,8 @@ int mgmt_powered(struct hci_dev *hdev, u8 powered)
6464 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status); 7466 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
6465 7467
6466 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) 7468 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
6467 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, 7469 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
6468 zero_cod, sizeof(zero_cod), NULL); 7470 zero_cod, sizeof(zero_cod), NULL);
6469 7471
6470new_settings: 7472new_settings:
6471 err = new_settings(hdev, match.sk); 7473 err = new_settings(hdev, match.sk);
@@ -6478,10 +7480,10 @@ new_settings:
6478 7480
6479void mgmt_set_powered_failed(struct hci_dev *hdev, int err) 7481void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
6480{ 7482{
6481 struct pending_cmd *cmd; 7483 struct mgmt_pending_cmd *cmd;
6482 u8 status; 7484 u8 status;
6483 7485
6484 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); 7486 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
6485 if (!cmd) 7487 if (!cmd)
6486 return; 7488 return;
6487 7489
@@ -6490,7 +7492,7 @@ void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
6490 else 7492 else
6491 status = MGMT_STATUS_FAILED; 7493 status = MGMT_STATUS_FAILED;
6492 7494
6493 cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status); 7495 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
6494 7496
6495 mgmt_pending_remove(cmd); 7497 mgmt_pending_remove(cmd);
6496} 7498}
@@ -6506,17 +7508,23 @@ void mgmt_discoverable_timeout(struct hci_dev *hdev)
6506 * of a timeout triggered from general discoverable, it is 7508 * of a timeout triggered from general discoverable, it is
6507 * safe to unconditionally clear the flag. 7509 * safe to unconditionally clear the flag.
6508 */ 7510 */
6509 clear_bit(HCI_LIMITED_DISCOVERABLE, &hdev->dev_flags); 7511 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
6510 clear_bit(HCI_DISCOVERABLE, &hdev->dev_flags); 7512 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
6511 7513
6512 hci_req_init(&req, hdev); 7514 hci_req_init(&req, hdev);
6513 if (test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags)) { 7515 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
6514 u8 scan = SCAN_PAGE; 7516 u8 scan = SCAN_PAGE;
6515 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 7517 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
6516 sizeof(scan), &scan); 7518 sizeof(scan), &scan);
6517 } 7519 }
6518 update_class(&req); 7520 update_class(&req);
6519 update_adv_data(&req); 7521
7522 /* Advertising instances don't use the global discoverable setting, so
7523 * only update AD if advertising was enabled using Set Advertising.
7524 */
7525 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7526 update_adv_data(&req);
7527
6520 hci_req_run(&req, NULL); 7528 hci_req_run(&req, NULL);
6521 7529
6522 hdev->discov_timeout = 0; 7530 hdev->discov_timeout = 0;
@@ -6654,7 +7662,7 @@ void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
6654 7662
6655 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr); 7663 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
6656 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type); 7664 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
6657 ev.key.master = csrk->master; 7665 ev.key.type = csrk->type;
6658 memcpy(ev.key.val, csrk->val, sizeof(csrk->val)); 7666 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
6659 7667
6660 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL); 7668 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
@@ -6681,17 +7689,6 @@ void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
6681 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL); 7689 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
6682} 7690}
6683 7691
6684static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6685 u8 data_len)
6686{
6687 eir[eir_len++] = sizeof(type) + data_len;
6688 eir[eir_len++] = type;
6689 memcpy(&eir[eir_len], data, data_len);
6690 eir_len += data_len;
6691
6692 return eir_len;
6693}
6694
6695void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn, 7692void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
6696 u32 flags, u8 *name, u8 name_len) 7693 u32 flags, u8 *name, u8 name_len)
6697{ 7694{
@@ -6729,7 +7726,7 @@ void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
6729 sizeof(*ev) + eir_len, NULL); 7726 sizeof(*ev) + eir_len, NULL);
6730} 7727}
6731 7728
6732static void disconnect_rsp(struct pending_cmd *cmd, void *data) 7729static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
6733{ 7730{
6734 struct sock **sk = data; 7731 struct sock **sk = data;
6735 7732
@@ -6741,7 +7738,7 @@ static void disconnect_rsp(struct pending_cmd *cmd, void *data)
6741 mgmt_pending_remove(cmd); 7738 mgmt_pending_remove(cmd);
6742} 7739}
6743 7740
6744static void unpair_device_rsp(struct pending_cmd *cmd, void *data) 7741static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
6745{ 7742{
6746 struct hci_dev *hdev = data; 7743 struct hci_dev *hdev = data;
6747 struct mgmt_cp_unpair_device *cp = cmd->param; 7744 struct mgmt_cp_unpair_device *cp = cmd->param;
@@ -6754,10 +7751,10 @@ static void unpair_device_rsp(struct pending_cmd *cmd, void *data)
6754 7751
6755bool mgmt_powering_down(struct hci_dev *hdev) 7752bool mgmt_powering_down(struct hci_dev *hdev)
6756{ 7753{
6757 struct pending_cmd *cmd; 7754 struct mgmt_pending_cmd *cmd;
6758 struct mgmt_mode *cp; 7755 struct mgmt_mode *cp;
6759 7756
6760 cmd = mgmt_pending_find(MGMT_OP_SET_POWERED, hdev); 7757 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
6761 if (!cmd) 7758 if (!cmd)
6762 return false; 7759 return false;
6763 7760
@@ -6809,12 +7806,12 @@ void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
6809{ 7806{
6810 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type); 7807 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
6811 struct mgmt_cp_disconnect *cp; 7808 struct mgmt_cp_disconnect *cp;
6812 struct pending_cmd *cmd; 7809 struct mgmt_pending_cmd *cmd;
6813 7810
6814 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp, 7811 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
6815 hdev); 7812 hdev);
6816 7813
6817 cmd = mgmt_pending_find(MGMT_OP_DISCONNECT, hdev); 7814 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
6818 if (!cmd) 7815 if (!cmd)
6819 return; 7816 return;
6820 7817
@@ -6864,9 +7861,9 @@ void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
6864void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 7861void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6865 u8 status) 7862 u8 status)
6866{ 7863{
6867 struct pending_cmd *cmd; 7864 struct mgmt_pending_cmd *cmd;
6868 7865
6869 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY, hdev); 7866 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
6870 if (!cmd) 7867 if (!cmd)
6871 return; 7868 return;
6872 7869
@@ -6877,9 +7874,9 @@ void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6877void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr, 7874void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6878 u8 status) 7875 u8 status)
6879{ 7876{
6880 struct pending_cmd *cmd; 7877 struct mgmt_pending_cmd *cmd;
6881 7878
6882 cmd = mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev); 7879 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
6883 if (!cmd) 7880 if (!cmd)
6884 return; 7881 return;
6885 7882
@@ -6922,9 +7919,9 @@ static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
6922 u8 link_type, u8 addr_type, u8 status, 7919 u8 link_type, u8 addr_type, u8 status,
6923 u8 opcode) 7920 u8 opcode)
6924{ 7921{
6925 struct pending_cmd *cmd; 7922 struct mgmt_pending_cmd *cmd;
6926 7923
6927 cmd = mgmt_pending_find(opcode, hdev); 7924 cmd = pending_find(opcode, hdev);
6928 if (!cmd) 7925 if (!cmd)
6929 return -ENOENT; 7926 return -ENOENT;
6930 7927
@@ -6983,7 +7980,7 @@ int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
6983void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status) 7980void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
6984{ 7981{
6985 struct mgmt_ev_auth_failed ev; 7982 struct mgmt_ev_auth_failed ev;
6986 struct pending_cmd *cmd; 7983 struct mgmt_pending_cmd *cmd;
6987 u8 status = mgmt_status(hci_status); 7984 u8 status = mgmt_status(hci_status);
6988 7985
6989 bacpy(&ev.addr.bdaddr, &conn->dst); 7986 bacpy(&ev.addr.bdaddr, &conn->dst);
@@ -7014,11 +8011,9 @@ void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
7014 } 8011 }
7015 8012
7016 if (test_bit(HCI_AUTH, &hdev->flags)) 8013 if (test_bit(HCI_AUTH, &hdev->flags))
7017 changed = !test_and_set_bit(HCI_LINK_SECURITY, 8014 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
7018 &hdev->dev_flags);
7019 else 8015 else
7020 changed = test_and_clear_bit(HCI_LINK_SECURITY, 8016 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
7021 &hdev->dev_flags);
7022 8017
7023 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp, 8018 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
7024 &match); 8019 &match);
@@ -7054,9 +8049,9 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
7054 if (status) { 8049 if (status) {
7055 u8 mgmt_err = mgmt_status(status); 8050 u8 mgmt_err = mgmt_status(status);
7056 8051
7057 if (enable && test_and_clear_bit(HCI_SSP_ENABLED, 8052 if (enable && hci_dev_test_and_clear_flag(hdev,
7058 &hdev->dev_flags)) { 8053 HCI_SSP_ENABLED)) {
7059 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 8054 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
7060 new_settings(hdev, NULL); 8055 new_settings(hdev, NULL);
7061 } 8056 }
7062 8057
@@ -7066,14 +8061,14 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
7066 } 8061 }
7067 8062
7068 if (enable) { 8063 if (enable) {
7069 changed = !test_and_set_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 8064 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
7070 } else { 8065 } else {
7071 changed = test_and_clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags); 8066 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
7072 if (!changed) 8067 if (!changed)
7073 changed = test_and_clear_bit(HCI_HS_ENABLED, 8068 changed = hci_dev_test_and_clear_flag(hdev,
7074 &hdev->dev_flags); 8069 HCI_HS_ENABLED);
7075 else 8070 else
7076 clear_bit(HCI_HS_ENABLED, &hdev->dev_flags); 8071 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
7077 } 8072 }
7078 8073
7079 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match); 8074 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
@@ -7086,8 +8081,8 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
7086 8081
7087 hci_req_init(&req, hdev); 8082 hci_req_init(&req, hdev);
7088 8083
7089 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 8084 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7090 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) 8085 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
7091 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE, 8086 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7092 sizeof(enable), &enable); 8087 sizeof(enable), &enable);
7093 update_eir(&req); 8088 update_eir(&req);
@@ -7098,7 +8093,7 @@ void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
7098 hci_req_run(&req, NULL); 8093 hci_req_run(&req, NULL);
7099} 8094}
7100 8095
7101static void sk_lookup(struct pending_cmd *cmd, void *data) 8096static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
7102{ 8097{
7103 struct cmd_lookup *match = data; 8098 struct cmd_lookup *match = data;
7104 8099
@@ -7118,8 +8113,8 @@ void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7118 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match); 8113 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
7119 8114
7120 if (!status) 8115 if (!status)
7121 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class, 3, 8116 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7122 NULL); 8117 dev_class, 3, NULL);
7123 8118
7124 if (match.sk) 8119 if (match.sk)
7125 sock_put(match.sk); 8120 sock_put(match.sk);
@@ -7128,7 +8123,7 @@ void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7128void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status) 8123void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
7129{ 8124{
7130 struct mgmt_cp_set_local_name ev; 8125 struct mgmt_cp_set_local_name ev;
7131 struct pending_cmd *cmd; 8126 struct mgmt_pending_cmd *cmd;
7132 8127
7133 if (status) 8128 if (status)
7134 return; 8129 return;
@@ -7137,55 +8132,19 @@ void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
7137 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH); 8132 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
7138 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH); 8133 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
7139 8134
7140 cmd = mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME, hdev); 8135 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
7141 if (!cmd) { 8136 if (!cmd) {
7142 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name)); 8137 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
7143 8138
7144 /* If this is a HCI command related to powering on the 8139 /* If this is a HCI command related to powering on the
7145 * HCI dev don't send any mgmt signals. 8140 * HCI dev don't send any mgmt signals.
7146 */ 8141 */
7147 if (mgmt_pending_find(MGMT_OP_SET_POWERED, hdev)) 8142 if (pending_find(MGMT_OP_SET_POWERED, hdev))
7148 return; 8143 return;
7149 } 8144 }
7150 8145
7151 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev), 8146 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7152 cmd ? cmd->sk : NULL); 8147 cmd ? cmd->sk : NULL);
7153}
7154
7155void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
7156 u8 *rand192, u8 *hash256, u8 *rand256,
7157 u8 status)
7158{
7159 struct pending_cmd *cmd;
7160
7161 BT_DBG("%s status %u", hdev->name, status);
7162
7163 cmd = mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
7164 if (!cmd)
7165 return;
7166
7167 if (status) {
7168 cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
7169 mgmt_status(status));
7170 } else {
7171 struct mgmt_rp_read_local_oob_data rp;
7172 size_t rp_size = sizeof(rp);
7173
7174 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
7175 memcpy(rp.rand192, rand192, sizeof(rp.rand192));
7176
7177 if (bredr_sc_enabled(hdev) && hash256 && rand256) {
7178 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
7179 memcpy(rp.rand256, rand256, sizeof(rp.rand256));
7180 } else {
7181 rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
7182 }
7183
7184 cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, 0,
7185 &rp, rp_size);
7186 }
7187
7188 mgmt_pending_remove(cmd);
7189} 8148}
7190 8149
7191static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16]) 8150static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
@@ -7258,7 +8217,7 @@ static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7258static void restart_le_scan(struct hci_dev *hdev) 8217static void restart_le_scan(struct hci_dev *hdev)
7259{ 8218{
7260 /* If controller is not scanning we are done. */ 8219 /* If controller is not scanning we are done. */
7261 if (!test_bit(HCI_LE_SCAN, &hdev->dev_flags)) 8220 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
7262 return; 8221 return;
7263 8222
7264 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY, 8223 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
@@ -7270,14 +8229,58 @@ static void restart_le_scan(struct hci_dev *hdev)
7270 DISCOV_LE_RESTART_DELAY); 8229 DISCOV_LE_RESTART_DELAY);
7271} 8230}
7272 8231
8232static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8233 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8234{
8235 /* If a RSSI threshold has been specified, and
8236 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8237 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8238 * is set, let it through for further processing, as we might need to
8239 * restart the scan.
8240 *
8241 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8242 * the results are also dropped.
8243 */
8244 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8245 (rssi == HCI_RSSI_INVALID ||
8246 (rssi < hdev->discovery.rssi &&
8247 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
8248 return false;
8249
8250 if (hdev->discovery.uuid_count != 0) {
8251 /* If a list of UUIDs is provided in filter, results with no
8252 * matching UUID should be dropped.
8253 */
8254 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8255 hdev->discovery.uuids) &&
8256 !eir_has_uuids(scan_rsp, scan_rsp_len,
8257 hdev->discovery.uuid_count,
8258 hdev->discovery.uuids))
8259 return false;
8260 }
8261
8262 /* If duplicate filtering does not report RSSI changes, then restart
8263 * scanning to ensure updated result with updated RSSI values.
8264 */
8265 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8266 restart_le_scan(hdev);
8267
8268 /* Validate RSSI value against the RSSI threshold once more. */
8269 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8270 rssi < hdev->discovery.rssi)
8271 return false;
8272 }
8273
8274 return true;
8275}
8276
7273void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type, 8277void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7274 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags, 8278 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
7275 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len) 8279 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
7276{ 8280{
7277 char buf[512]; 8281 char buf[512];
7278 struct mgmt_ev_device_found *ev = (void *) buf; 8282 struct mgmt_ev_device_found *ev = (void *)buf;
7279 size_t ev_size; 8283 size_t ev_size;
7280 bool match;
7281 8284
7282 /* Don't send events for a non-kernel initiated discovery. With 8285 /* Don't send events for a non-kernel initiated discovery. With
7283 * LE one exception is if we have pend_le_reports > 0 in which 8286 * LE one exception is if we have pend_le_reports > 0 in which
@@ -7290,21 +8293,12 @@ void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7290 return; 8293 return;
7291 } 8294 }
7292 8295
7293 /* When using service discovery with a RSSI threshold, then check 8296 if (hdev->discovery.result_filtering) {
7294 * if such a RSSI threshold is specified. If a RSSI threshold has 8297 /* We are using service discovery */
7295 * been specified, and HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, 8298 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
7296 * then all results with a RSSI smaller than the RSSI threshold will be 8299 scan_rsp_len))
7297 * dropped. If the quirk is set, let it through for further processing, 8300 return;
7298 * as we might need to restart the scan. 8301 }
7299 *
7300 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
7301 * the results are also dropped.
7302 */
7303 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7304 (rssi == HCI_RSSI_INVALID ||
7305 (rssi < hdev->discovery.rssi &&
7306 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
7307 return;
7308 8302
7309 /* Make sure that the buffer is big enough. The 5 extra bytes 8303 /* Make sure that the buffer is big enough. The 5 extra bytes
7310 * are for the potential CoD field. 8304 * are for the potential CoD field.
@@ -7331,87 +8325,17 @@ void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7331 ev->rssi = rssi; 8325 ev->rssi = rssi;
7332 ev->flags = cpu_to_le32(flags); 8326 ev->flags = cpu_to_le32(flags);
7333 8327
7334 if (eir_len > 0) { 8328 if (eir_len > 0)
7335 /* When using service discovery and a list of UUID is
7336 * provided, results with no matching UUID should be
7337 * dropped. In case there is a match the result is
7338 * kept and checking possible scan response data
7339 * will be skipped.
7340 */
7341 if (hdev->discovery.uuid_count > 0) {
7342 match = eir_has_uuids(eir, eir_len,
7343 hdev->discovery.uuid_count,
7344 hdev->discovery.uuids);
7345 /* If duplicate filtering does not report RSSI changes,
7346 * then restart scanning to ensure updated result with
7347 * updated RSSI values.
7348 */
7349 if (match && test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
7350 &hdev->quirks))
7351 restart_le_scan(hdev);
7352 } else {
7353 match = true;
7354 }
7355
7356 if (!match && !scan_rsp_len)
7357 return;
7358
7359 /* Copy EIR or advertising data into event */ 8329 /* Copy EIR or advertising data into event */
7360 memcpy(ev->eir, eir, eir_len); 8330 memcpy(ev->eir, eir, eir_len);
7361 } else {
7362 /* When using service discovery and a list of UUID is
7363 * provided, results with empty EIR or advertising data
7364 * should be dropped since they do not match any UUID.
7365 */
7366 if (hdev->discovery.uuid_count > 0 && !scan_rsp_len)
7367 return;
7368
7369 match = false;
7370 }
7371 8331
7372 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV)) 8332 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
7373 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV, 8333 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
7374 dev_class, 3); 8334 dev_class, 3);
7375 8335
7376 if (scan_rsp_len > 0) { 8336 if (scan_rsp_len > 0)
7377 /* When using service discovery and a list of UUID is
7378 * provided, results with no matching UUID should be
7379 * dropped if there is no previous match from the
7380 * advertising data.
7381 */
7382 if (hdev->discovery.uuid_count > 0) {
7383 if (!match && !eir_has_uuids(scan_rsp, scan_rsp_len,
7384 hdev->discovery.uuid_count,
7385 hdev->discovery.uuids))
7386 return;
7387
7388 /* If duplicate filtering does not report RSSI changes,
7389 * then restart scanning to ensure updated result with
7390 * updated RSSI values.
7391 */
7392 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
7393 &hdev->quirks))
7394 restart_le_scan(hdev);
7395 }
7396
7397 /* Append scan response data to event */ 8337 /* Append scan response data to event */
7398 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len); 8338 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
7399 } else {
7400 /* When using service discovery and a list of UUID is
7401 * provided, results with empty scan response and no
7402 * previous matched advertising data should be dropped.
7403 */
7404 if (hdev->discovery.uuid_count > 0 && !match)
7405 return;
7406 }
7407
7408 /* Validate the reported RSSI value against the RSSI threshold once more
7409 * incase HCI_QUIRK_STRICT_DUPLICATE_FILTER forced a restart of LE
7410 * scanning.
7411 */
7412 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7413 rssi < hdev->discovery.rssi)
7414 return;
7415 8339
7416 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len); 8340 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
7417 ev_size = sizeof(*ev) + eir_len + scan_rsp_len; 8341 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
@@ -7464,10 +8388,28 @@ void mgmt_reenable_advertising(struct hci_dev *hdev)
7464{ 8388{
7465 struct hci_request req; 8389 struct hci_request req;
7466 8390
7467 if (!test_bit(HCI_ADVERTISING, &hdev->dev_flags)) 8391 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8392 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
7468 return; 8393 return;
7469 8394
7470 hci_req_init(&req, hdev); 8395 hci_req_init(&req, hdev);
7471 enable_advertising(&req); 8396 enable_advertising(&req);
7472 hci_req_run(&req, adv_enable_complete); 8397 hci_req_run(&req, adv_enable_complete);
7473} 8398}
8399
8400static struct hci_mgmt_chan chan = {
8401 .channel = HCI_CHANNEL_CONTROL,
8402 .handler_count = ARRAY_SIZE(mgmt_handlers),
8403 .handlers = mgmt_handlers,
8404 .hdev_init = mgmt_init_hdev,
8405};
8406
8407int mgmt_init(void)
8408{
8409 return hci_mgmt_chan_register(&chan);
8410}
8411
8412void mgmt_exit(void)
8413{
8414 hci_mgmt_chan_unregister(&chan);
8415}
diff --git a/net/bluetooth/mgmt_util.c b/net/bluetooth/mgmt_util.c
new file mode 100644
index 000000000000..8c30c7eb8bef
--- /dev/null
+++ b/net/bluetooth/mgmt_util.c
@@ -0,0 +1,210 @@
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3
4 Copyright (C) 2015 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 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
22*/
23
24#include <net/bluetooth/bluetooth.h>
25#include <net/bluetooth/hci_core.h>
26#include <net/bluetooth/mgmt.h>
27
28#include "mgmt_util.h"
29
30int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
31 void *data, u16 data_len, int flag, struct sock *skip_sk)
32{
33 struct sk_buff *skb;
34 struct mgmt_hdr *hdr;
35
36 skb = alloc_skb(sizeof(*hdr) + data_len, GFP_KERNEL);
37 if (!skb)
38 return -ENOMEM;
39
40 hdr = (void *) skb_put(skb, sizeof(*hdr));
41 hdr->opcode = cpu_to_le16(event);
42 if (hdev)
43 hdr->index = cpu_to_le16(hdev->id);
44 else
45 hdr->index = cpu_to_le16(MGMT_INDEX_NONE);
46 hdr->len = cpu_to_le16(data_len);
47
48 if (data)
49 memcpy(skb_put(skb, data_len), data, data_len);
50
51 /* Time stamp */
52 __net_timestamp(skb);
53
54 hci_send_to_channel(channel, skb, flag, skip_sk);
55 kfree_skb(skb);
56
57 return 0;
58}
59
60int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status)
61{
62 struct sk_buff *skb;
63 struct mgmt_hdr *hdr;
64 struct mgmt_ev_cmd_status *ev;
65 int err;
66
67 BT_DBG("sock %p, index %u, cmd %u, status %u", sk, index, cmd, status);
68
69 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev), GFP_KERNEL);
70 if (!skb)
71 return -ENOMEM;
72
73 hdr = (void *) skb_put(skb, sizeof(*hdr));
74
75 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_STATUS);
76 hdr->index = cpu_to_le16(index);
77 hdr->len = cpu_to_le16(sizeof(*ev));
78
79 ev = (void *) skb_put(skb, sizeof(*ev));
80 ev->status = status;
81 ev->opcode = cpu_to_le16(cmd);
82
83 err = sock_queue_rcv_skb(sk, skb);
84 if (err < 0)
85 kfree_skb(skb);
86
87 return err;
88}
89
90int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
91 void *rp, size_t rp_len)
92{
93 struct sk_buff *skb;
94 struct mgmt_hdr *hdr;
95 struct mgmt_ev_cmd_complete *ev;
96 int err;
97
98 BT_DBG("sock %p", sk);
99
100 skb = alloc_skb(sizeof(*hdr) + sizeof(*ev) + rp_len, GFP_KERNEL);
101 if (!skb)
102 return -ENOMEM;
103
104 hdr = (void *) skb_put(skb, sizeof(*hdr));
105
106 hdr->opcode = cpu_to_le16(MGMT_EV_CMD_COMPLETE);
107 hdr->index = cpu_to_le16(index);
108 hdr->len = cpu_to_le16(sizeof(*ev) + rp_len);
109
110 ev = (void *) skb_put(skb, sizeof(*ev) + rp_len);
111 ev->opcode = cpu_to_le16(cmd);
112 ev->status = status;
113
114 if (rp)
115 memcpy(ev->data, rp, rp_len);
116
117 err = sock_queue_rcv_skb(sk, skb);
118 if (err < 0)
119 kfree_skb(skb);
120
121 return err;
122}
123
124struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
125 struct hci_dev *hdev)
126{
127 struct mgmt_pending_cmd *cmd;
128
129 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
130 if (hci_sock_get_channel(cmd->sk) != channel)
131 continue;
132 if (cmd->opcode == opcode)
133 return cmd;
134 }
135
136 return NULL;
137}
138
139struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
140 u16 opcode,
141 struct hci_dev *hdev,
142 const void *data)
143{
144 struct mgmt_pending_cmd *cmd;
145
146 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
147 if (cmd->user_data != data)
148 continue;
149 if (cmd->opcode == opcode)
150 return cmd;
151 }
152
153 return NULL;
154}
155
156void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
157 void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
158 void *data)
159{
160 struct mgmt_pending_cmd *cmd, *tmp;
161
162 list_for_each_entry_safe(cmd, tmp, &hdev->mgmt_pending, list) {
163 if (opcode > 0 && cmd->opcode != opcode)
164 continue;
165
166 cb(cmd, data);
167 }
168}
169
170struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
171 struct hci_dev *hdev,
172 void *data, u16 len)
173{
174 struct mgmt_pending_cmd *cmd;
175
176 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
177 if (!cmd)
178 return NULL;
179
180 cmd->opcode = opcode;
181 cmd->index = hdev->id;
182
183 cmd->param = kmemdup(data, len, GFP_KERNEL);
184 if (!cmd->param) {
185 kfree(cmd);
186 return NULL;
187 }
188
189 cmd->param_len = len;
190
191 cmd->sk = sk;
192 sock_hold(sk);
193
194 list_add(&cmd->list, &hdev->mgmt_pending);
195
196 return cmd;
197}
198
199void mgmt_pending_free(struct mgmt_pending_cmd *cmd)
200{
201 sock_put(cmd->sk);
202 kfree(cmd->param);
203 kfree(cmd);
204}
205
206void mgmt_pending_remove(struct mgmt_pending_cmd *cmd)
207{
208 list_del(&cmd->list);
209 mgmt_pending_free(cmd);
210}
diff --git a/net/bluetooth/mgmt_util.h b/net/bluetooth/mgmt_util.h
new file mode 100644
index 000000000000..6559f189213c
--- /dev/null
+++ b/net/bluetooth/mgmt_util.h
@@ -0,0 +1,53 @@
1/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2015 Intel Coropration
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
23struct mgmt_pending_cmd {
24 struct list_head list;
25 u16 opcode;
26 int index;
27 void *param;
28 size_t param_len;
29 struct sock *sk;
30 void *user_data;
31 int (*cmd_complete)(struct mgmt_pending_cmd *cmd, u8 status);
32};
33
34int mgmt_send_event(u16 event, struct hci_dev *hdev, unsigned short channel,
35 void *data, u16 data_len, int flag, struct sock *skip_sk);
36int mgmt_cmd_status(struct sock *sk, u16 index, u16 cmd, u8 status);
37int mgmt_cmd_complete(struct sock *sk, u16 index, u16 cmd, u8 status,
38 void *rp, size_t rp_len);
39
40struct mgmt_pending_cmd *mgmt_pending_find(unsigned short channel, u16 opcode,
41 struct hci_dev *hdev);
42struct mgmt_pending_cmd *mgmt_pending_find_data(unsigned short channel,
43 u16 opcode,
44 struct hci_dev *hdev,
45 const void *data);
46void mgmt_pending_foreach(u16 opcode, struct hci_dev *hdev,
47 void (*cb)(struct mgmt_pending_cmd *cmd, void *data),
48 void *data);
49struct mgmt_pending_cmd *mgmt_pending_add(struct sock *sk, u16 opcode,
50 struct hci_dev *hdev,
51 void *data, u16 len);
52void mgmt_pending_free(struct mgmt_pending_cmd *cmd);
53void mgmt_pending_remove(struct mgmt_pending_cmd *cmd);
diff --git a/net/bluetooth/rfcomm/sock.c b/net/bluetooth/rfcomm/sock.c
index 3c6d2c8ac1a4..825e8fb5114b 100644
--- a/net/bluetooth/rfcomm/sock.c
+++ b/net/bluetooth/rfcomm/sock.c
@@ -549,8 +549,8 @@ static int rfcomm_sock_getname(struct socket *sock, struct sockaddr *addr, int *
549 return 0; 549 return 0;
550} 550}
551 551
552static int rfcomm_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 552static int rfcomm_sock_sendmsg(struct socket *sock, struct msghdr *msg,
553 struct msghdr *msg, size_t len) 553 size_t len)
554{ 554{
555 struct sock *sk = sock->sk; 555 struct sock *sk = sock->sk;
556 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; 556 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
@@ -615,8 +615,8 @@ done:
615 return sent; 615 return sent;
616} 616}
617 617
618static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 618static int rfcomm_sock_recvmsg(struct socket *sock, struct msghdr *msg,
619 struct msghdr *msg, size_t size, int flags) 619 size_t size, int flags)
620{ 620{
621 struct sock *sk = sock->sk; 621 struct sock *sk = sock->sk;
622 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc; 622 struct rfcomm_dlc *d = rfcomm_pi(sk)->dlc;
@@ -627,7 +627,7 @@ static int rfcomm_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
627 return 0; 627 return 0;
628 } 628 }
629 629
630 len = bt_sock_stream_recvmsg(iocb, sock, msg, size, flags); 630 len = bt_sock_stream_recvmsg(sock, msg, size, flags);
631 631
632 lock_sock(sk); 632 lock_sock(sk);
633 if (!(flags & MSG_PEEK) && len > 0) 633 if (!(flags & MSG_PEEK) && len > 0)
diff --git a/net/bluetooth/sco.c b/net/bluetooth/sco.c
index 76321b546e84..4322c833e748 100644
--- a/net/bluetooth/sco.c
+++ b/net/bluetooth/sco.c
@@ -688,8 +688,8 @@ static int sco_sock_getname(struct socket *sock, struct sockaddr *addr, int *len
688 return 0; 688 return 0;
689} 689}
690 690
691static int sco_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 691static int sco_sock_sendmsg(struct socket *sock, struct msghdr *msg,
692 struct msghdr *msg, size_t len) 692 size_t len)
693{ 693{
694 struct sock *sk = sock->sk; 694 struct sock *sk = sock->sk;
695 int err; 695 int err;
@@ -758,8 +758,8 @@ static void sco_conn_defer_accept(struct hci_conn *conn, u16 setting)
758 } 758 }
759} 759}
760 760
761static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 761static int sco_sock_recvmsg(struct socket *sock, struct msghdr *msg,
762 struct msghdr *msg, size_t len, int flags) 762 size_t len, int flags)
763{ 763{
764 struct sock *sk = sock->sk; 764 struct sock *sk = sock->sk;
765 struct sco_pinfo *pi = sco_pi(sk); 765 struct sco_pinfo *pi = sco_pi(sk);
@@ -777,7 +777,7 @@ static int sco_sock_recvmsg(struct kiocb *iocb, struct socket *sock,
777 777
778 release_sock(sk); 778 release_sock(sk);
779 779
780 return bt_sock_recvmsg(iocb, sock, msg, len, flags); 780 return bt_sock_recvmsg(sock, msg, len, flags);
781} 781}
782 782
783static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen) 783static int sco_sock_setsockopt(struct socket *sock, int level, int optname, char __user *optval, unsigned int optlen)
@@ -1083,9 +1083,13 @@ int sco_connect_ind(struct hci_dev *hdev, bdaddr_t *bdaddr, __u8 *flags)
1083 return lm; 1083 return lm;
1084} 1084}
1085 1085
1086void sco_connect_cfm(struct hci_conn *hcon, __u8 status) 1086static void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1087{ 1087{
1088 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1089 return;
1090
1088 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status); 1091 BT_DBG("hcon %p bdaddr %pMR status %d", hcon, &hcon->dst, status);
1092
1089 if (!status) { 1093 if (!status) {
1090 struct sco_conn *conn; 1094 struct sco_conn *conn;
1091 1095
@@ -1096,8 +1100,11 @@ void sco_connect_cfm(struct hci_conn *hcon, __u8 status)
1096 sco_conn_del(hcon, bt_to_errno(status)); 1100 sco_conn_del(hcon, bt_to_errno(status));
1097} 1101}
1098 1102
1099void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason) 1103static void sco_disconn_cfm(struct hci_conn *hcon, __u8 reason)
1100{ 1104{
1105 if (hcon->type != SCO_LINK && hcon->type != ESCO_LINK)
1106 return;
1107
1101 BT_DBG("hcon %p reason %d", hcon, reason); 1108 BT_DBG("hcon %p reason %d", hcon, reason);
1102 1109
1103 sco_conn_del(hcon, bt_to_errno(reason)); 1110 sco_conn_del(hcon, bt_to_errno(reason));
@@ -1122,6 +1129,12 @@ drop:
1122 return 0; 1129 return 0;
1123} 1130}
1124 1131
1132static struct hci_cb sco_cb = {
1133 .name = "SCO",
1134 .connect_cfm = sco_connect_cfm,
1135 .disconn_cfm = sco_disconn_cfm,
1136};
1137
1125static int sco_debugfs_show(struct seq_file *f, void *p) 1138static int sco_debugfs_show(struct seq_file *f, void *p)
1126{ 1139{
1127 struct sock *sk; 1140 struct sock *sk;
@@ -1203,6 +1216,8 @@ int __init sco_init(void)
1203 1216
1204 BT_INFO("SCO socket layer initialized"); 1217 BT_INFO("SCO socket layer initialized");
1205 1218
1219 hci_register_cb(&sco_cb);
1220
1206 if (IS_ERR_OR_NULL(bt_debugfs)) 1221 if (IS_ERR_OR_NULL(bt_debugfs))
1207 return 0; 1222 return 0;
1208 1223
@@ -1216,12 +1231,14 @@ error:
1216 return err; 1231 return err;
1217} 1232}
1218 1233
1219void __exit sco_exit(void) 1234void sco_exit(void)
1220{ 1235{
1221 bt_procfs_cleanup(&init_net, "sco"); 1236 bt_procfs_cleanup(&init_net, "sco");
1222 1237
1223 debugfs_remove(sco_debugfs); 1238 debugfs_remove(sco_debugfs);
1224 1239
1240 hci_unregister_cb(&sco_cb);
1241
1225 bt_sock_unregister(BTPROTO_SCO); 1242 bt_sock_unregister(BTPROTO_SCO);
1226 1243
1227 proto_unregister(&sco_proto); 1244 proto_unregister(&sco_proto);
diff --git a/net/bluetooth/selftest.c b/net/bluetooth/selftest.c
index 378f4064952c..dc688f13e496 100644
--- a/net/bluetooth/selftest.c
+++ b/net/bluetooth/selftest.c
@@ -21,6 +21,8 @@
21 SOFTWARE IS DISCLAIMED. 21 SOFTWARE IS DISCLAIMED.
22*/ 22*/
23 23
24#include <linux/debugfs.h>
25
24#include <net/bluetooth/bluetooth.h> 26#include <net/bluetooth/bluetooth.h>
25#include <net/bluetooth/hci_core.h> 27#include <net/bluetooth/hci_core.h>
26 28
@@ -154,6 +156,21 @@ static int __init test_ecdh_sample(const u8 priv_a[32], const u8 priv_b[32],
154 return 0; 156 return 0;
155} 157}
156 158
159static char test_ecdh_buffer[32];
160
161static ssize_t test_ecdh_read(struct file *file, char __user *user_buf,
162 size_t count, loff_t *ppos)
163{
164 return simple_read_from_buffer(user_buf, count, ppos, test_ecdh_buffer,
165 strlen(test_ecdh_buffer));
166}
167
168static const struct file_operations test_ecdh_fops = {
169 .open = simple_open,
170 .read = test_ecdh_read,
171 .llseek = default_llseek,
172};
173
157static int __init test_ecdh(void) 174static int __init test_ecdh(void)
158{ 175{
159 ktime_t calltime, delta, rettime; 176 ktime_t calltime, delta, rettime;
@@ -165,19 +182,19 @@ static int __init test_ecdh(void)
165 err = test_ecdh_sample(priv_a_1, priv_b_1, pub_a_1, pub_b_1, dhkey_1); 182 err = test_ecdh_sample(priv_a_1, priv_b_1, pub_a_1, pub_b_1, dhkey_1);
166 if (err) { 183 if (err) {
167 BT_ERR("ECDH sample 1 failed"); 184 BT_ERR("ECDH sample 1 failed");
168 return err; 185 goto done;
169 } 186 }
170 187
171 err = test_ecdh_sample(priv_a_2, priv_b_2, pub_a_2, pub_b_2, dhkey_2); 188 err = test_ecdh_sample(priv_a_2, priv_b_2, pub_a_2, pub_b_2, dhkey_2);
172 if (err) { 189 if (err) {
173 BT_ERR("ECDH sample 2 failed"); 190 BT_ERR("ECDH sample 2 failed");
174 return err; 191 goto done;
175 } 192 }
176 193
177 err = test_ecdh_sample(priv_a_3, priv_a_3, pub_a_3, pub_a_3, dhkey_3); 194 err = test_ecdh_sample(priv_a_3, priv_a_3, pub_a_3, pub_a_3, dhkey_3);
178 if (err) { 195 if (err) {
179 BT_ERR("ECDH sample 3 failed"); 196 BT_ERR("ECDH sample 3 failed");
180 return err; 197 goto done;
181 } 198 }
182 199
183 rettime = ktime_get(); 200 rettime = ktime_get();
@@ -186,7 +203,17 @@ static int __init test_ecdh(void)
186 203
187 BT_INFO("ECDH test passed in %llu usecs", duration); 204 BT_INFO("ECDH test passed in %llu usecs", duration);
188 205
189 return 0; 206done:
207 if (!err)
208 snprintf(test_ecdh_buffer, sizeof(test_ecdh_buffer),
209 "PASS (%llu usecs)\n", duration);
210 else
211 snprintf(test_ecdh_buffer, sizeof(test_ecdh_buffer), "FAIL\n");
212
213 debugfs_create_file("selftest_ecdh", 0444, bt_debugfs, NULL,
214 &test_ecdh_fops);
215
216 return err;
190} 217}
191 218
192#else 219#else
diff --git a/net/bluetooth/smp.c b/net/bluetooth/smp.c
index c09a821f381d..1ab3dc9c8f99 100644
--- a/net/bluetooth/smp.c
+++ b/net/bluetooth/smp.c
@@ -52,7 +52,7 @@
52 52
53#define SMP_TIMEOUT msecs_to_jiffies(30000) 53#define SMP_TIMEOUT msecs_to_jiffies(30000)
54 54
55#define AUTH_REQ_MASK(dev) (test_bit(HCI_SC_ENABLED, &(dev)->dev_flags) ? \ 55#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
56 0x1f : 0x07) 56 0x1f : 0x07)
57#define KEY_DIST_MASK 0x07 57#define KEY_DIST_MASK 0x07
58 58
@@ -70,7 +70,19 @@ enum {
70 SMP_FLAG_DEBUG_KEY, 70 SMP_FLAG_DEBUG_KEY,
71 SMP_FLAG_WAIT_USER, 71 SMP_FLAG_WAIT_USER,
72 SMP_FLAG_DHKEY_PENDING, 72 SMP_FLAG_DHKEY_PENDING,
73 SMP_FLAG_OOB, 73 SMP_FLAG_REMOTE_OOB,
74 SMP_FLAG_LOCAL_OOB,
75};
76
77struct smp_dev {
78 /* Secure Connections OOB data */
79 u8 local_pk[64];
80 u8 local_sk[32];
81 u8 local_rand[16];
82 bool debug_key;
83
84 struct crypto_blkcipher *tfm_aes;
85 struct crypto_hash *tfm_cmac;
74}; 86};
75 87
76struct smp_chan { 88struct smp_chan {
@@ -84,7 +96,8 @@ struct smp_chan {
84 u8 rrnd[16]; /* SMP Pairing Random (remote) */ 96 u8 rrnd[16]; /* SMP Pairing Random (remote) */
85 u8 pcnf[16]; /* SMP Pairing Confirm */ 97 u8 pcnf[16]; /* SMP Pairing Confirm */
86 u8 tk[16]; /* SMP Temporary Key */ 98 u8 tk[16]; /* SMP Temporary Key */
87 u8 rr[16]; 99 u8 rr[16]; /* Remote OOB ra/rb value */
100 u8 lr[16]; /* Local OOB ra/rb value */
88 u8 enc_key_size; 101 u8 enc_key_size;
89 u8 remote_key_dist; 102 u8 remote_key_dist;
90 bdaddr_t id_addr; 103 bdaddr_t id_addr;
@@ -478,18 +491,18 @@ bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
478 const bdaddr_t *bdaddr) 491 const bdaddr_t *bdaddr)
479{ 492{
480 struct l2cap_chan *chan = hdev->smp_data; 493 struct l2cap_chan *chan = hdev->smp_data;
481 struct crypto_blkcipher *tfm; 494 struct smp_dev *smp;
482 u8 hash[3]; 495 u8 hash[3];
483 int err; 496 int err;
484 497
485 if (!chan || !chan->data) 498 if (!chan || !chan->data)
486 return false; 499 return false;
487 500
488 tfm = chan->data; 501 smp = chan->data;
489 502
490 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk); 503 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
491 504
492 err = smp_ah(tfm, irk, &bdaddr->b[3], hash); 505 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
493 if (err) 506 if (err)
494 return false; 507 return false;
495 508
@@ -499,20 +512,20 @@ bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
499int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa) 512int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
500{ 513{
501 struct l2cap_chan *chan = hdev->smp_data; 514 struct l2cap_chan *chan = hdev->smp_data;
502 struct crypto_blkcipher *tfm; 515 struct smp_dev *smp;
503 int err; 516 int err;
504 517
505 if (!chan || !chan->data) 518 if (!chan || !chan->data)
506 return -EOPNOTSUPP; 519 return -EOPNOTSUPP;
507 520
508 tfm = chan->data; 521 smp = chan->data;
509 522
510 get_random_bytes(&rpa->b[3], 3); 523 get_random_bytes(&rpa->b[3], 3);
511 524
512 rpa->b[5] &= 0x3f; /* Clear two most significant bits */ 525 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
513 rpa->b[5] |= 0x40; /* Set second most significant bit */ 526 rpa->b[5] |= 0x40; /* Set second most significant bit */
514 527
515 err = smp_ah(tfm, irk, &rpa->b[3], rpa->b); 528 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
516 if (err < 0) 529 if (err < 0)
517 return err; 530 return err;
518 531
@@ -521,6 +534,53 @@ int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
521 return 0; 534 return 0;
522} 535}
523 536
537int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
538{
539 struct l2cap_chan *chan = hdev->smp_data;
540 struct smp_dev *smp;
541 int err;
542
543 if (!chan || !chan->data)
544 return -EOPNOTSUPP;
545
546 smp = chan->data;
547
548 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
549 BT_DBG("Using debug keys");
550 memcpy(smp->local_pk, debug_pk, 64);
551 memcpy(smp->local_sk, debug_sk, 32);
552 smp->debug_key = true;
553 } else {
554 while (true) {
555 /* Generate local key pair for Secure Connections */
556 if (!ecc_make_key(smp->local_pk, smp->local_sk))
557 return -EIO;
558
559 /* This is unlikely, but we need to check that
560 * we didn't accidentially generate a debug key.
561 */
562 if (memcmp(smp->local_sk, debug_sk, 32))
563 break;
564 }
565 smp->debug_key = false;
566 }
567
568 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
569 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
570 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
571
572 get_random_bytes(smp->local_rand, 16);
573
574 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
575 smp->local_rand, 0, hash);
576 if (err < 0)
577 return err;
578
579 memcpy(rand, smp->local_rand, 16);
580
581 return 0;
582}
583
524static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data) 584static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
525{ 585{
526 struct l2cap_chan *chan = conn->smp; 586 struct l2cap_chan *chan = conn->smp;
@@ -589,7 +649,7 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
589 struct hci_dev *hdev = hcon->hdev; 649 struct hci_dev *hdev = hcon->hdev;
590 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT; 650 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
591 651
592 if (test_bit(HCI_BONDABLE, &conn->hcon->hdev->dev_flags)) { 652 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
593 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 653 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
594 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 654 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
595 authreq |= SMP_AUTH_BONDING; 655 authreq |= SMP_AUTH_BONDING;
@@ -597,18 +657,18 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
597 authreq &= ~SMP_AUTH_BONDING; 657 authreq &= ~SMP_AUTH_BONDING;
598 } 658 }
599 659
600 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) 660 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
601 remote_dist |= SMP_DIST_ID_KEY; 661 remote_dist |= SMP_DIST_ID_KEY;
602 662
603 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) 663 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
604 local_dist |= SMP_DIST_ID_KEY; 664 local_dist |= SMP_DIST_ID_KEY;
605 665
606 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags) && 666 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
607 (authreq & SMP_AUTH_SC)) { 667 (authreq & SMP_AUTH_SC)) {
608 struct oob_data *oob_data; 668 struct oob_data *oob_data;
609 u8 bdaddr_type; 669 u8 bdaddr_type;
610 670
611 if (test_bit(HCI_SSP_ENABLED, &hdev->dev_flags)) { 671 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
612 local_dist |= SMP_DIST_LINK_KEY; 672 local_dist |= SMP_DIST_LINK_KEY;
613 remote_dist |= SMP_DIST_LINK_KEY; 673 remote_dist |= SMP_DIST_LINK_KEY;
614 } 674 }
@@ -621,10 +681,12 @@ static void build_pairing_cmd(struct l2cap_conn *conn,
621 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst, 681 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
622 bdaddr_type); 682 bdaddr_type);
623 if (oob_data && oob_data->present) { 683 if (oob_data && oob_data->present) {
624 set_bit(SMP_FLAG_OOB, &smp->flags); 684 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
625 oob_flag = SMP_OOB_PRESENT; 685 oob_flag = SMP_OOB_PRESENT;
626 memcpy(smp->rr, oob_data->rand256, 16); 686 memcpy(smp->rr, oob_data->rand256, 16);
627 memcpy(smp->pcnf, oob_data->hash256, 16); 687 memcpy(smp->pcnf, oob_data->hash256, 16);
688 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
689 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
628 } 690 }
629 691
630 } else { 692 } else {
@@ -681,9 +743,9 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
681 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags); 743 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
682 mgmt_smp_complete(hcon, complete); 744 mgmt_smp_complete(hcon, complete);
683 745
684 kfree(smp->csrk); 746 kzfree(smp->csrk);
685 kfree(smp->slave_csrk); 747 kzfree(smp->slave_csrk);
686 kfree(smp->link_key); 748 kzfree(smp->link_key);
687 749
688 crypto_free_blkcipher(smp->tfm_aes); 750 crypto_free_blkcipher(smp->tfm_aes);
689 crypto_free_hash(smp->tfm_cmac); 751 crypto_free_hash(smp->tfm_cmac);
@@ -692,7 +754,7 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
692 * support hasn't been explicitly enabled. 754 * support hasn't been explicitly enabled.
693 */ 755 */
694 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG && 756 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
695 !test_bit(HCI_KEEP_DEBUG_KEYS, &hcon->hdev->dev_flags)) { 757 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
696 list_del_rcu(&smp->ltk->list); 758 list_del_rcu(&smp->ltk->list);
697 kfree_rcu(smp->ltk, rcu); 759 kfree_rcu(smp->ltk, rcu);
698 smp->ltk = NULL; 760 smp->ltk = NULL;
@@ -717,7 +779,7 @@ static void smp_chan_destroy(struct l2cap_conn *conn)
717 } 779 }
718 780
719 chan->data = NULL; 781 chan->data = NULL;
720 kfree(smp); 782 kzfree(smp);
721 hci_conn_drop(hcon); 783 hci_conn_drop(hcon);
722} 784}
723 785
@@ -818,6 +880,12 @@ static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
818 return 0; 880 return 0;
819 } 881 }
820 882
883 /* If this function is used for SC -> legacy fallback we
884 * can only recover the just-works case.
885 */
886 if (test_bit(SMP_FLAG_SC, &smp->flags))
887 return -EINVAL;
888
821 /* Not Just Works/Confirm results in MITM Authentication */ 889 /* Not Just Works/Confirm results in MITM Authentication */
822 if (smp->method != JUST_CFM) { 890 if (smp->method != JUST_CFM) {
823 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags); 891 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
@@ -1052,7 +1120,7 @@ static void smp_notify_keys(struct l2cap_conn *conn)
1052 /* Don't keep debug keys around if the relevant 1120 /* Don't keep debug keys around if the relevant
1053 * flag is not set. 1121 * flag is not set.
1054 */ 1122 */
1055 if (!test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags) && 1123 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
1056 key->type == HCI_LK_DEBUG_COMBINATION) { 1124 key->type == HCI_LK_DEBUG_COMBINATION) {
1057 list_del_rcu(&key->list); 1125 list_del_rcu(&key->list);
1058 kfree_rcu(key, rcu); 1126 kfree_rcu(key, rcu);
@@ -1097,13 +1165,13 @@ static void sc_generate_link_key(struct smp_chan *smp)
1097 return; 1165 return;
1098 1166
1099 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) { 1167 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1100 kfree(smp->link_key); 1168 kzfree(smp->link_key);
1101 smp->link_key = NULL; 1169 smp->link_key = NULL;
1102 return; 1170 return;
1103 } 1171 }
1104 1172
1105 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) { 1173 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
1106 kfree(smp->link_key); 1174 kzfree(smp->link_key);
1107 smp->link_key = NULL; 1175 smp->link_key = NULL;
1108 return; 1176 return;
1109 } 1177 }
@@ -1252,7 +1320,10 @@ static void smp_distribute_keys(struct smp_chan *smp)
1252 1320
1253 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); 1321 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1254 if (csrk) { 1322 if (csrk) {
1255 csrk->master = 0x00; 1323 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1324 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1325 else
1326 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
1256 memcpy(csrk->val, sign.csrk, sizeof(csrk->val)); 1327 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1257 } 1328 }
1258 smp->slave_csrk = csrk; 1329 smp->slave_csrk = csrk;
@@ -1297,7 +1368,7 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1297 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC); 1368 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1298 if (IS_ERR(smp->tfm_aes)) { 1369 if (IS_ERR(smp->tfm_aes)) {
1299 BT_ERR("Unable to create ECB crypto context"); 1370 BT_ERR("Unable to create ECB crypto context");
1300 kfree(smp); 1371 kzfree(smp);
1301 return NULL; 1372 return NULL;
1302 } 1373 }
1303 1374
@@ -1305,7 +1376,7 @@ static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1305 if (IS_ERR(smp->tfm_cmac)) { 1376 if (IS_ERR(smp->tfm_cmac)) {
1306 BT_ERR("Unable to create CMAC crypto context"); 1377 BT_ERR("Unable to create CMAC crypto context");
1307 crypto_free_blkcipher(smp->tfm_aes); 1378 crypto_free_blkcipher(smp->tfm_aes);
1308 kfree(smp); 1379 kzfree(smp);
1309 return NULL; 1380 return NULL;
1310 } 1381 }
1311 1382
@@ -1601,15 +1672,15 @@ static void build_bredr_pairing_cmd(struct smp_chan *smp,
1601 struct hci_dev *hdev = conn->hcon->hdev; 1672 struct hci_dev *hdev = conn->hcon->hdev;
1602 u8 local_dist = 0, remote_dist = 0; 1673 u8 local_dist = 0, remote_dist = 0;
1603 1674
1604 if (test_bit(HCI_BONDABLE, &hdev->dev_flags)) { 1675 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
1605 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 1676 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1606 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN; 1677 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1607 } 1678 }
1608 1679
1609 if (test_bit(HCI_RPA_RESOLVING, &hdev->dev_flags)) 1680 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
1610 remote_dist |= SMP_DIST_ID_KEY; 1681 remote_dist |= SMP_DIST_ID_KEY;
1611 1682
1612 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) 1683 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
1613 local_dist |= SMP_DIST_ID_KEY; 1684 local_dist |= SMP_DIST_ID_KEY;
1614 1685
1615 if (!rsp) { 1686 if (!rsp) {
@@ -1661,22 +1732,29 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
1661 /* We didn't start the pairing, so match remote */ 1732 /* We didn't start the pairing, so match remote */
1662 auth = req->auth_req & AUTH_REQ_MASK(hdev); 1733 auth = req->auth_req & AUTH_REQ_MASK(hdev);
1663 1734
1664 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) && 1735 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
1665 (auth & SMP_AUTH_BONDING)) 1736 (auth & SMP_AUTH_BONDING))
1666 return SMP_PAIRING_NOTSUPP; 1737 return SMP_PAIRING_NOTSUPP;
1667 1738
1668 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) 1739 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
1669 return SMP_AUTH_REQUIREMENTS; 1740 return SMP_AUTH_REQUIREMENTS;
1670 1741
1671 smp->preq[0] = SMP_CMD_PAIRING_REQ; 1742 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1672 memcpy(&smp->preq[1], req, sizeof(*req)); 1743 memcpy(&smp->preq[1], req, sizeof(*req));
1673 skb_pull(skb, sizeof(*req)); 1744 skb_pull(skb, sizeof(*req));
1674 1745
1746 /* If the remote side's OOB flag is set it means it has
1747 * successfully received our local OOB data - therefore set the
1748 * flag to indicate that local OOB is in use.
1749 */
1750 if (req->oob_flag == SMP_OOB_PRESENT)
1751 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1752
1675 /* SMP over BR/EDR requires special treatment */ 1753 /* SMP over BR/EDR requires special treatment */
1676 if (conn->hcon->type == ACL_LINK) { 1754 if (conn->hcon->type == ACL_LINK) {
1677 /* We must have a BR/EDR SC link */ 1755 /* We must have a BR/EDR SC link */
1678 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) && 1756 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
1679 !test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags)) 1757 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
1680 return SMP_CROSS_TRANSP_NOT_ALLOWED; 1758 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1681 1759
1682 set_bit(SMP_FLAG_SC, &smp->flags); 1760 set_bit(SMP_FLAG_SC, &smp->flags);
@@ -1734,14 +1812,19 @@ static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
1734 1812
1735 clear_bit(SMP_FLAG_INITIATOR, &smp->flags); 1813 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1736 1814
1815 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1816 * SC case, however some implementations incorrectly copy RFU auth
1817 * req bits from our security request, which may create a false
1818 * positive SC enablement.
1819 */
1820 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1821
1737 if (test_bit(SMP_FLAG_SC, &smp->flags)) { 1822 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1738 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY); 1823 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1739 /* Clear bits which are generated but not distributed */ 1824 /* Clear bits which are generated but not distributed */
1740 smp->remote_key_dist &= ~SMP_SC_NO_DIST; 1825 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1741 /* Wait for Public Key from Initiating Device */ 1826 /* Wait for Public Key from Initiating Device */
1742 return 0; 1827 return 0;
1743 } else {
1744 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1745 } 1828 }
1746 1829
1747 /* Request setup of TK */ 1830 /* Request setup of TK */
@@ -1758,7 +1841,26 @@ static u8 sc_send_public_key(struct smp_chan *smp)
1758 1841
1759 BT_DBG(""); 1842 BT_DBG("");
1760 1843
1761 if (test_bit(HCI_USE_DEBUG_KEYS, &hdev->dev_flags)) { 1844 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
1845 struct l2cap_chan *chan = hdev->smp_data;
1846 struct smp_dev *smp_dev;
1847
1848 if (!chan || !chan->data)
1849 return SMP_UNSPECIFIED;
1850
1851 smp_dev = chan->data;
1852
1853 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1854 memcpy(smp->local_sk, smp_dev->local_sk, 32);
1855 memcpy(smp->lr, smp_dev->local_rand, 16);
1856
1857 if (smp_dev->debug_key)
1858 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1859
1860 goto done;
1861 }
1862
1863 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
1762 BT_DBG("Using debug keys"); 1864 BT_DBG("Using debug keys");
1763 memcpy(smp->local_pk, debug_pk, 64); 1865 memcpy(smp->local_pk, debug_pk, 64);
1764 memcpy(smp->local_sk, debug_sk, 32); 1866 memcpy(smp->local_sk, debug_sk, 32);
@@ -1777,8 +1879,9 @@ static u8 sc_send_public_key(struct smp_chan *smp)
1777 } 1879 }
1778 } 1880 }
1779 1881
1882done:
1780 SMP_DBG("Local Public Key X: %32phN", smp->local_pk); 1883 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
1781 SMP_DBG("Local Public Key Y: %32phN", &smp->local_pk[32]); 1884 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
1782 SMP_DBG("Local Private Key: %32phN", smp->local_sk); 1885 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
1783 1886
1784 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk); 1887 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
@@ -1813,9 +1916,16 @@ static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
1813 1916
1814 auth = rsp->auth_req & AUTH_REQ_MASK(hdev); 1917 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
1815 1918
1816 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) 1919 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
1817 return SMP_AUTH_REQUIREMENTS; 1920 return SMP_AUTH_REQUIREMENTS;
1818 1921
1922 /* If the remote side's OOB flag is set it means it has
1923 * successfully received our local OOB data - therefore set the
1924 * flag to indicate that local OOB is in use.
1925 */
1926 if (rsp->oob_flag == SMP_OOB_PRESENT)
1927 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1928
1819 smp->prsp[0] = SMP_CMD_PAIRING_RSP; 1929 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1820 memcpy(&smp->prsp[1], rsp, sizeof(*rsp)); 1930 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1821 1931
@@ -1882,10 +1992,6 @@ static u8 sc_check_confirm(struct smp_chan *smp)
1882 1992
1883 BT_DBG(""); 1993 BT_DBG("");
1884 1994
1885 /* Public Key exchange must happen before any other steps */
1886 if (!test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
1887 return SMP_UNSPECIFIED;
1888
1889 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 1995 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
1890 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM); 1996 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
1891 1997
@@ -1898,6 +2004,47 @@ static u8 sc_check_confirm(struct smp_chan *smp)
1898 return 0; 2004 return 0;
1899} 2005}
1900 2006
2007/* Work-around for some implementations that incorrectly copy RFU bits
2008 * from our security request and thereby create the impression that
2009 * we're doing SC when in fact the remote doesn't support it.
2010 */
2011static int fixup_sc_false_positive(struct smp_chan *smp)
2012{
2013 struct l2cap_conn *conn = smp->conn;
2014 struct hci_conn *hcon = conn->hcon;
2015 struct hci_dev *hdev = hcon->hdev;
2016 struct smp_cmd_pairing *req, *rsp;
2017 u8 auth;
2018
2019 /* The issue is only observed when we're in slave role */
2020 if (hcon->out)
2021 return SMP_UNSPECIFIED;
2022
2023 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2024 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2025 return SMP_UNSPECIFIED;
2026 }
2027
2028 BT_ERR("Trying to fall back to legacy SMP");
2029
2030 req = (void *) &smp->preq[1];
2031 rsp = (void *) &smp->prsp[1];
2032
2033 /* Rebuild key dist flags which may have been cleared for SC */
2034 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2035
2036 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2037
2038 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2039 BT_ERR("Failed to fall back to legacy SMP");
2040 return SMP_UNSPECIFIED;
2041 }
2042
2043 clear_bit(SMP_FLAG_SC, &smp->flags);
2044
2045 return 0;
2046}
2047
1901static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb) 2048static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1902{ 2049{
1903 struct l2cap_chan *chan = conn->smp; 2050 struct l2cap_chan *chan = conn->smp;
@@ -1911,8 +2058,19 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1911 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf)); 2058 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
1912 skb_pull(skb, sizeof(smp->pcnf)); 2059 skb_pull(skb, sizeof(smp->pcnf));
1913 2060
1914 if (test_bit(SMP_FLAG_SC, &smp->flags)) 2061 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1915 return sc_check_confirm(smp); 2062 int ret;
2063
2064 /* Public Key exchange must happen before any other steps */
2065 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2066 return sc_check_confirm(smp);
2067
2068 BT_ERR("Unexpected SMP Pairing Confirm");
2069
2070 ret = fixup_sc_false_positive(smp);
2071 if (ret)
2072 return ret;
2073 }
1916 2074
1917 if (conn->hcon->out) { 2075 if (conn->hcon->out) {
1918 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd), 2076 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
@@ -1923,8 +2081,8 @@ static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
1923 2081
1924 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags)) 2082 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
1925 return smp_confirm(smp); 2083 return smp_confirm(smp);
1926 else 2084
1927 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags); 2085 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1928 2086
1929 return 0; 2087 return 0;
1930} 2088}
@@ -2083,7 +2241,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
2083 2241
2084 auth = rp->auth_req & AUTH_REQ_MASK(hdev); 2242 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
2085 2243
2086 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) && !(auth & SMP_AUTH_SC)) 2244 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
2087 return SMP_AUTH_REQUIREMENTS; 2245 return SMP_AUTH_REQUIREMENTS;
2088 2246
2089 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT) 2247 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
@@ -2104,7 +2262,7 @@ static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
2104 if (!smp) 2262 if (!smp)
2105 return SMP_UNSPECIFIED; 2263 return SMP_UNSPECIFIED;
2106 2264
2107 if (!test_bit(HCI_BONDABLE, &hcon->hdev->dev_flags) && 2265 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
2108 (auth & SMP_AUTH_BONDING)) 2266 (auth & SMP_AUTH_BONDING))
2109 return SMP_PAIRING_NOTSUPP; 2267 return SMP_PAIRING_NOTSUPP;
2110 2268
@@ -2138,7 +2296,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
2138 2296
2139 chan = conn->smp; 2297 chan = conn->smp;
2140 2298
2141 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) 2299 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
2142 return 1; 2300 return 1;
2143 2301
2144 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) 2302 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
@@ -2167,7 +2325,7 @@ int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
2167 2325
2168 authreq = seclevel_to_authreq(sec_level); 2326 authreq = seclevel_to_authreq(sec_level);
2169 2327
2170 if (test_bit(HCI_SC_ENABLED, &hcon->hdev->dev_flags)) 2328 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
2171 authreq |= SMP_AUTH_SC; 2329 authreq |= SMP_AUTH_SC;
2172 2330
2173 /* Require MITM if IO Capability allows or the security level 2331 /* Require MITM if IO Capability allows or the security level
@@ -2352,7 +2510,10 @@ static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2352 2510
2353 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL); 2511 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2354 if (csrk) { 2512 if (csrk) {
2355 csrk->master = 0x01; 2513 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2514 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2515 else
2516 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
2356 memcpy(csrk->val, rp->csrk, sizeof(csrk->val)); 2517 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2357 } 2518 }
2358 smp->csrk = csrk; 2519 smp->csrk = csrk;
@@ -2368,7 +2529,8 @@ static u8 sc_select_method(struct smp_chan *smp)
2368 struct smp_cmd_pairing *local, *remote; 2529 struct smp_cmd_pairing *local, *remote;
2369 u8 local_mitm, remote_mitm, local_io, remote_io, method; 2530 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2370 2531
2371 if (test_bit(SMP_FLAG_OOB, &smp->flags)) 2532 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2533 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
2372 return REQ_OOB; 2534 return REQ_OOB;
2373 2535
2374 /* The preq/prsp contain the raw Pairing Request/Response PDUs 2536 /* The preq/prsp contain the raw Pairing Request/Response PDUs
@@ -2422,6 +2584,16 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2422 2584
2423 memcpy(smp->remote_pk, key, 64); 2585 memcpy(smp->remote_pk, key, 64);
2424 2586
2587 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2588 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2589 smp->rr, 0, cfm.confirm_val);
2590 if (err)
2591 return SMP_UNSPECIFIED;
2592
2593 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2594 return SMP_CONFIRM_FAILED;
2595 }
2596
2425 /* Non-initiating device sends its public key after receiving 2597 /* Non-initiating device sends its public key after receiving
2426 * the key from the initiating device. 2598 * the key from the initiating device.
2427 */ 2599 */
@@ -2432,7 +2604,7 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2432 } 2604 }
2433 2605
2434 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk); 2606 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
2435 SMP_DBG("Remote Public Key Y: %32phN", &smp->remote_pk[32]); 2607 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
2436 2608
2437 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey)) 2609 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2438 return SMP_UNSPECIFIED; 2610 return SMP_UNSPECIFIED;
@@ -2470,14 +2642,6 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2470 } 2642 }
2471 2643
2472 if (smp->method == REQ_OOB) { 2644 if (smp->method == REQ_OOB) {
2473 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2474 smp->rr, 0, cfm.confirm_val);
2475 if (err)
2476 return SMP_UNSPECIFIED;
2477
2478 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2479 return SMP_CONFIRM_FAILED;
2480
2481 if (hcon->out) 2645 if (hcon->out)
2482 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, 2646 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2483 sizeof(smp->prnd), smp->prnd); 2647 sizeof(smp->prnd), smp->prnd);
@@ -2550,6 +2714,8 @@ static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2550 2714
2551 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY) 2715 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2552 put_unaligned_le32(hcon->passkey_notify, r); 2716 put_unaligned_le32(hcon->passkey_notify, r);
2717 else if (smp->method == REQ_OOB)
2718 memcpy(r, smp->lr, 16);
2553 2719
2554 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r, 2720 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2555 io_cap, remote_addr, local_addr, e); 2721 io_cap, remote_addr, local_addr, e);
@@ -2600,7 +2766,7 @@ static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
2600 if (skb->len < 1) 2766 if (skb->len < 1)
2601 return -EILSEQ; 2767 return -EILSEQ;
2602 2768
2603 if (!test_bit(HCI_LE_ENABLED, &hcon->hdev->dev_flags)) { 2769 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
2604 reason = SMP_PAIRING_NOTSUPP; 2770 reason = SMP_PAIRING_NOTSUPP;
2605 goto done; 2771 goto done;
2606 } 2772 }
@@ -2738,16 +2904,16 @@ static void bredr_pairing(struct l2cap_chan *chan)
2738 return; 2904 return;
2739 2905
2740 /* Secure Connections support must be enabled */ 2906 /* Secure Connections support must be enabled */
2741 if (!test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) 2907 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
2742 return; 2908 return;
2743 2909
2744 /* BR/EDR must use Secure Connections for SMP */ 2910 /* BR/EDR must use Secure Connections for SMP */
2745 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) && 2911 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
2746 !test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags)) 2912 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
2747 return; 2913 return;
2748 2914
2749 /* If our LE support is not enabled don't do anything */ 2915 /* If our LE support is not enabled don't do anything */
2750 if (!test_bit(HCI_LE_ENABLED, &hdev->dev_flags)) 2916 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2751 return; 2917 return;
2752 2918
2753 /* Don't bother if remote LE support is not enabled */ 2919 /* Don't bother if remote LE support is not enabled */
@@ -2851,7 +3017,7 @@ static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
2851 return ERR_PTR(-ENOMEM); 3017 return ERR_PTR(-ENOMEM);
2852 3018
2853 skb->priority = HCI_PRIO_MAX; 3019 skb->priority = HCI_PRIO_MAX;
2854 bt_cb(skb)->chan = chan; 3020 bt_cb(skb)->l2cap.chan = chan;
2855 3021
2856 return skb; 3022 return skb;
2857} 3023}
@@ -2924,51 +3090,63 @@ static const struct l2cap_ops smp_root_chan_ops = {
2924static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid) 3090static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
2925{ 3091{
2926 struct l2cap_chan *chan; 3092 struct l2cap_chan *chan;
2927 struct crypto_blkcipher *tfm_aes; 3093 struct smp_dev *smp;
3094 struct crypto_blkcipher *tfm_aes;
3095 struct crypto_hash *tfm_cmac;
2928 3096
2929 if (cid == L2CAP_CID_SMP_BREDR) { 3097 if (cid == L2CAP_CID_SMP_BREDR) {
2930 tfm_aes = NULL; 3098 smp = NULL;
2931 goto create_chan; 3099 goto create_chan;
2932 } 3100 }
2933 3101
2934 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, 0); 3102 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3103 if (!smp)
3104 return ERR_PTR(-ENOMEM);
3105
3106 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
2935 if (IS_ERR(tfm_aes)) { 3107 if (IS_ERR(tfm_aes)) {
2936 BT_ERR("Unable to create crypto context"); 3108 BT_ERR("Unable to create ECB crypto context");
3109 kzfree(smp);
2937 return ERR_CAST(tfm_aes); 3110 return ERR_CAST(tfm_aes);
2938 } 3111 }
2939 3112
3113 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3114 if (IS_ERR(tfm_cmac)) {
3115 BT_ERR("Unable to create CMAC crypto context");
3116 crypto_free_blkcipher(tfm_aes);
3117 kzfree(smp);
3118 return ERR_CAST(tfm_cmac);
3119 }
3120
3121 smp->tfm_aes = tfm_aes;
3122 smp->tfm_cmac = tfm_cmac;
3123
2940create_chan: 3124create_chan:
2941 chan = l2cap_chan_create(); 3125 chan = l2cap_chan_create();
2942 if (!chan) { 3126 if (!chan) {
2943 crypto_free_blkcipher(tfm_aes); 3127 if (smp) {
3128 crypto_free_blkcipher(smp->tfm_aes);
3129 crypto_free_hash(smp->tfm_cmac);
3130 kzfree(smp);
3131 }
2944 return ERR_PTR(-ENOMEM); 3132 return ERR_PTR(-ENOMEM);
2945 } 3133 }
2946 3134
2947 chan->data = tfm_aes; 3135 chan->data = smp;
2948 3136
2949 l2cap_add_scid(chan, cid); 3137 l2cap_add_scid(chan, cid);
2950 3138
2951 l2cap_chan_set_defaults(chan); 3139 l2cap_chan_set_defaults(chan);
2952 3140
2953 if (cid == L2CAP_CID_SMP) { 3141 if (cid == L2CAP_CID_SMP) {
2954 /* If usage of static address is forced or if the devices 3142 u8 bdaddr_type;
2955 * does not have a public address, then listen on the static 3143
2956 * address. 3144 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
2957 * 3145
2958 * In case BR/EDR has been disabled on a dual-mode controller 3146 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
2959 * and a static address has been configued, then listen on
2960 * the static address instead.
2961 */
2962 if (test_bit(HCI_FORCE_STATIC_ADDR, &hdev->dbg_flags) ||
2963 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
2964 (!test_bit(HCI_BREDR_ENABLED, &hdev->dev_flags) &&
2965 bacmp(&hdev->static_addr, BDADDR_ANY))) {
2966 bacpy(&chan->src, &hdev->static_addr);
2967 chan->src_type = BDADDR_LE_RANDOM;
2968 } else {
2969 bacpy(&chan->src, &hdev->bdaddr);
2970 chan->src_type = BDADDR_LE_PUBLIC; 3147 chan->src_type = BDADDR_LE_PUBLIC;
2971 } 3148 else
3149 chan->src_type = BDADDR_LE_RANDOM;
2972 } else { 3150 } else {
2973 bacpy(&chan->src, &hdev->bdaddr); 3151 bacpy(&chan->src, &hdev->bdaddr);
2974 chan->src_type = BDADDR_BREDR; 3152 chan->src_type = BDADDR_BREDR;
@@ -2987,14 +3165,18 @@ create_chan:
2987 3165
2988static void smp_del_chan(struct l2cap_chan *chan) 3166static void smp_del_chan(struct l2cap_chan *chan)
2989{ 3167{
2990 struct crypto_blkcipher *tfm_aes; 3168 struct smp_dev *smp;
2991 3169
2992 BT_DBG("chan %p", chan); 3170 BT_DBG("chan %p", chan);
2993 3171
2994 tfm_aes = chan->data; 3172 smp = chan->data;
2995 if (tfm_aes) { 3173 if (smp) {
2996 chan->data = NULL; 3174 chan->data = NULL;
2997 crypto_free_blkcipher(tfm_aes); 3175 if (smp->tfm_aes)
3176 crypto_free_blkcipher(smp->tfm_aes);
3177 if (smp->tfm_cmac)
3178 crypto_free_hash(smp->tfm_cmac);
3179 kzfree(smp);
2998 } 3180 }
2999 3181
3000 l2cap_chan_put(chan); 3182 l2cap_chan_put(chan);
@@ -3007,7 +3189,7 @@ static ssize_t force_bredr_smp_read(struct file *file,
3007 struct hci_dev *hdev = file->private_data; 3189 struct hci_dev *hdev = file->private_data;
3008 char buf[3]; 3190 char buf[3];
3009 3191
3010 buf[0] = test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags) ? 'Y': 'N'; 3192 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
3011 buf[1] = '\n'; 3193 buf[1] = '\n';
3012 buf[2] = '\0'; 3194 buf[2] = '\0';
3013 return simple_read_from_buffer(user_buf, count, ppos, buf, 2); 3195 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
@@ -3029,7 +3211,7 @@ static ssize_t force_bredr_smp_write(struct file *file,
3029 if (strtobool(buf, &enable)) 3211 if (strtobool(buf, &enable))
3030 return -EINVAL; 3212 return -EINVAL;
3031 3213
3032 if (enable == test_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags)) 3214 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3033 return -EALREADY; 3215 return -EALREADY;
3034 3216
3035 if (enable) { 3217 if (enable) {
@@ -3048,7 +3230,7 @@ static ssize_t force_bredr_smp_write(struct file *file,
3048 smp_del_chan(chan); 3230 smp_del_chan(chan);
3049 } 3231 }
3050 3232
3051 change_bit(HCI_FORCE_BREDR_SMP, &hdev->dbg_flags); 3233 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
3052 3234
3053 return count; 3235 return count;
3054} 3236}
@@ -3367,6 +3549,21 @@ static int __init test_h6(struct crypto_hash *tfm_cmac)
3367 return 0; 3549 return 0;
3368} 3550}
3369 3551
3552static char test_smp_buffer[32];
3553
3554static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3555 size_t count, loff_t *ppos)
3556{
3557 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3558 strlen(test_smp_buffer));
3559}
3560
3561static const struct file_operations test_smp_fops = {
3562 .open = simple_open,
3563 .read = test_smp_read,
3564 .llseek = default_llseek,
3565};
3566
3370static int __init run_selftests(struct crypto_blkcipher *tfm_aes, 3567static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3371 struct crypto_hash *tfm_cmac) 3568 struct crypto_hash *tfm_cmac)
3372{ 3569{
@@ -3379,49 +3576,49 @@ static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3379 err = test_ah(tfm_aes); 3576 err = test_ah(tfm_aes);
3380 if (err) { 3577 if (err) {
3381 BT_ERR("smp_ah test failed"); 3578 BT_ERR("smp_ah test failed");
3382 return err; 3579 goto done;
3383 } 3580 }
3384 3581
3385 err = test_c1(tfm_aes); 3582 err = test_c1(tfm_aes);
3386 if (err) { 3583 if (err) {
3387 BT_ERR("smp_c1 test failed"); 3584 BT_ERR("smp_c1 test failed");
3388 return err; 3585 goto done;
3389 } 3586 }
3390 3587
3391 err = test_s1(tfm_aes); 3588 err = test_s1(tfm_aes);
3392 if (err) { 3589 if (err) {
3393 BT_ERR("smp_s1 test failed"); 3590 BT_ERR("smp_s1 test failed");
3394 return err; 3591 goto done;
3395 } 3592 }
3396 3593
3397 err = test_f4(tfm_cmac); 3594 err = test_f4(tfm_cmac);
3398 if (err) { 3595 if (err) {
3399 BT_ERR("smp_f4 test failed"); 3596 BT_ERR("smp_f4 test failed");
3400 return err; 3597 goto done;
3401 } 3598 }
3402 3599
3403 err = test_f5(tfm_cmac); 3600 err = test_f5(tfm_cmac);
3404 if (err) { 3601 if (err) {
3405 BT_ERR("smp_f5 test failed"); 3602 BT_ERR("smp_f5 test failed");
3406 return err; 3603 goto done;
3407 } 3604 }
3408 3605
3409 err = test_f6(tfm_cmac); 3606 err = test_f6(tfm_cmac);
3410 if (err) { 3607 if (err) {
3411 BT_ERR("smp_f6 test failed"); 3608 BT_ERR("smp_f6 test failed");
3412 return err; 3609 goto done;
3413 } 3610 }
3414 3611
3415 err = test_g2(tfm_cmac); 3612 err = test_g2(tfm_cmac);
3416 if (err) { 3613 if (err) {
3417 BT_ERR("smp_g2 test failed"); 3614 BT_ERR("smp_g2 test failed");
3418 return err; 3615 goto done;
3419 } 3616 }
3420 3617
3421 err = test_h6(tfm_cmac); 3618 err = test_h6(tfm_cmac);
3422 if (err) { 3619 if (err) {
3423 BT_ERR("smp_h6 test failed"); 3620 BT_ERR("smp_h6 test failed");
3424 return err; 3621 goto done;
3425 } 3622 }
3426 3623
3427 rettime = ktime_get(); 3624 rettime = ktime_get();
@@ -3430,7 +3627,17 @@ static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3430 3627
3431 BT_INFO("SMP test passed in %llu usecs", duration); 3628 BT_INFO("SMP test passed in %llu usecs", duration);
3432 3629
3433 return 0; 3630done:
3631 if (!err)
3632 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3633 "PASS (%llu usecs)\n", duration);
3634 else
3635 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3636
3637 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3638 &test_smp_fops);
3639
3640 return err;
3434} 3641}
3435 3642
3436int __init bt_selftest_smp(void) 3643int __init bt_selftest_smp(void)
diff --git a/net/bluetooth/smp.h b/net/bluetooth/smp.h
index 60c5b73fcb4b..6cf872563ea7 100644
--- a/net/bluetooth/smp.h
+++ b/net/bluetooth/smp.h
@@ -188,6 +188,7 @@ int smp_user_confirm_reply(struct hci_conn *conn, u16 mgmt_op, __le32 passkey);
188bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16], 188bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
189 const bdaddr_t *bdaddr); 189 const bdaddr_t *bdaddr);
190int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa); 190int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa);
191int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16]);
191 192
192int smp_register(struct hci_dev *hdev); 193int smp_register(struct hci_dev *hdev);
193void smp_unregister(struct hci_dev *hdev); 194void smp_unregister(struct hci_dev *hdev);
diff --git a/net/bridge/br_device.c b/net/bridge/br_device.c
index ffd379db5938..4ff77a16956c 100644
--- a/net/bridge/br_device.c
+++ b/net/bridge/br_device.c
@@ -25,6 +25,9 @@
25#define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \ 25#define COMMON_FEATURES (NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA | \
26 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM) 26 NETIF_F_GSO_MASK | NETIF_F_HW_CSUM)
27 27
28const struct nf_br_ops __rcu *nf_br_ops __read_mostly;
29EXPORT_SYMBOL_GPL(nf_br_ops);
30
28/* net device transmit always called with BH disabled */ 31/* net device transmit always called with BH disabled */
29netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev) 32netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
30{ 33{
@@ -33,16 +36,15 @@ netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev)
33 struct net_bridge_fdb_entry *dst; 36 struct net_bridge_fdb_entry *dst;
34 struct net_bridge_mdb_entry *mdst; 37 struct net_bridge_mdb_entry *mdst;
35 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats); 38 struct pcpu_sw_netstats *brstats = this_cpu_ptr(br->stats);
39 const struct nf_br_ops *nf_ops;
36 u16 vid = 0; 40 u16 vid = 0;
37 41
38 rcu_read_lock(); 42 rcu_read_lock();
39#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 43 nf_ops = rcu_dereference(nf_br_ops);
40 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) { 44 if (nf_ops && nf_ops->br_dev_xmit_hook(skb)) {
41 br_nf_pre_routing_finish_bridge_slow(skb);
42 rcu_read_unlock(); 45 rcu_read_unlock();
43 return NETDEV_TX_OK; 46 return NETDEV_TX_OK;
44 } 47 }
45#endif
46 48
47 u64_stats_update_begin(&brstats->syncp); 49 u64_stats_update_begin(&brstats->syncp);
48 brstats->tx_packets++; 50 brstats->tx_packets++;
diff --git a/net/bridge/br_forward.c b/net/bridge/br_forward.c
index f96933a823e3..e97572b5d2cc 100644
--- a/net/bridge/br_forward.c
+++ b/net/bridge/br_forward.c
@@ -35,11 +35,9 @@ static inline int should_deliver(const struct net_bridge_port *p,
35 p->state == BR_STATE_FORWARDING; 35 p->state == BR_STATE_FORWARDING;
36} 36}
37 37
38int br_dev_queue_push_xmit(struct sk_buff *skb) 38int br_dev_queue_push_xmit(struct sock *sk, struct sk_buff *skb)
39{ 39{
40 /* ip_fragment doesn't copy the MAC header */ 40 if (!is_skb_forwardable(skb->dev, skb)) {
41 if (nf_bridge_maybe_copy_header(skb) ||
42 !is_skb_forwardable(skb->dev, skb)) {
43 kfree_skb(skb); 41 kfree_skb(skb);
44 } else { 42 } else {
45 skb_push(skb, ETH_HLEN); 43 skb_push(skb, ETH_HLEN);
@@ -51,9 +49,10 @@ int br_dev_queue_push_xmit(struct sk_buff *skb)
51} 49}
52EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit); 50EXPORT_SYMBOL_GPL(br_dev_queue_push_xmit);
53 51
54int br_forward_finish(struct sk_buff *skb) 52int br_forward_finish(struct sock *sk, struct sk_buff *skb)
55{ 53{
56 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, skb, NULL, skb->dev, 54 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_POST_ROUTING, sk, skb,
55 NULL, skb->dev,
57 br_dev_queue_push_xmit); 56 br_dev_queue_push_xmit);
58 57
59} 58}
@@ -77,7 +76,8 @@ static void __br_deliver(const struct net_bridge_port *to, struct sk_buff *skb)
77 return; 76 return;
78 } 77 }
79 78
80 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 79 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
80 NULL, skb->dev,
81 br_forward_finish); 81 br_forward_finish);
82} 82}
83 83
@@ -98,7 +98,8 @@ static void __br_forward(const struct net_bridge_port *to, struct sk_buff *skb)
98 skb->dev = to->dev; 98 skb->dev = to->dev;
99 skb_forward_csum(skb); 99 skb_forward_csum(skb);
100 100
101 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, indev, skb->dev, 101 NF_HOOK(NFPROTO_BRIDGE, NF_BR_FORWARD, NULL, skb,
102 indev, skb->dev,
102 br_forward_finish); 103 br_forward_finish);
103} 104}
104 105
@@ -188,6 +189,9 @@ static void br_flood(struct net_bridge *br, struct sk_buff *skb,
188 /* Do not flood to ports that enable proxy ARP */ 189 /* Do not flood to ports that enable proxy ARP */
189 if (p->flags & BR_PROXYARP) 190 if (p->flags & BR_PROXYARP)
190 continue; 191 continue;
192 if ((p->flags & BR_PROXYARP_WIFI) &&
193 BR_INPUT_SKB_CB(skb)->proxyarp_replied)
194 continue;
191 195
192 prev = maybe_deliver(prev, p, skb, __packet_hook); 196 prev = maybe_deliver(prev, p, skb, __packet_hook);
193 if (IS_ERR(prev)) 197 if (IS_ERR(prev))
diff --git a/net/bridge/br_input.c b/net/bridge/br_input.c
index e2aa7be3a847..f921a5dce22d 100644
--- a/net/bridge/br_input.c
+++ b/net/bridge/br_input.c
@@ -55,12 +55,13 @@ static int br_pass_frame_up(struct sk_buff *skb)
55 if (!skb) 55 if (!skb)
56 return NET_RX_DROP; 56 return NET_RX_DROP;
57 57
58 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, indev, NULL, 58 return NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
59 netif_receive_skb); 59 indev, NULL,
60 netif_receive_skb_sk);
60} 61}
61 62
62static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br, 63static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
63 u16 vid) 64 u16 vid, struct net_bridge_port *p)
64{ 65{
65 struct net_device *dev = br->dev; 66 struct net_device *dev = br->dev;
66 struct neighbour *n; 67 struct neighbour *n;
@@ -68,6 +69,8 @@ static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
68 u8 *arpptr, *sha; 69 u8 *arpptr, *sha;
69 __be32 sip, tip; 70 __be32 sip, tip;
70 71
72 BR_INPUT_SKB_CB(skb)->proxyarp_replied = false;
73
71 if (dev->flags & IFF_NOARP) 74 if (dev->flags & IFF_NOARP)
72 return; 75 return;
73 76
@@ -105,16 +108,19 @@ static void br_do_proxy_arp(struct sk_buff *skb, struct net_bridge *br,
105 } 108 }
106 109
107 f = __br_fdb_get(br, n->ha, vid); 110 f = __br_fdb_get(br, n->ha, vid);
108 if (f) 111 if (f && ((p->flags & BR_PROXYARP) ||
112 (f->dst && (f->dst->flags & BR_PROXYARP_WIFI)))) {
109 arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip, 113 arp_send(ARPOP_REPLY, ETH_P_ARP, sip, skb->dev, tip,
110 sha, n->ha, sha); 114 sha, n->ha, sha);
115 BR_INPUT_SKB_CB(skb)->proxyarp_replied = true;
116 }
111 117
112 neigh_release(n); 118 neigh_release(n);
113 } 119 }
114} 120}
115 121
116/* note: already called with rcu_read_lock */ 122/* note: already called with rcu_read_lock */
117int br_handle_frame_finish(struct sk_buff *skb) 123int br_handle_frame_finish(struct sock *sk, struct sk_buff *skb)
118{ 124{
119 const unsigned char *dest = eth_hdr(skb)->h_dest; 125 const unsigned char *dest = eth_hdr(skb)->h_dest;
120 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 126 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
@@ -153,12 +159,10 @@ int br_handle_frame_finish(struct sk_buff *skb)
153 159
154 dst = NULL; 160 dst = NULL;
155 161
156 if (is_broadcast_ether_addr(dest)) { 162 if (IS_ENABLED(CONFIG_INET) && skb->protocol == htons(ETH_P_ARP))
157 if (IS_ENABLED(CONFIG_INET) && 163 br_do_proxy_arp(skb, br, vid, p);
158 p->flags & BR_PROXYARP &&
159 skb->protocol == htons(ETH_P_ARP))
160 br_do_proxy_arp(skb, br, vid);
161 164
165 if (is_broadcast_ether_addr(dest)) {
162 skb2 = skb; 166 skb2 = skb;
163 unicast = false; 167 unicast = false;
164 } else if (is_multicast_ether_addr(dest)) { 168 } else if (is_multicast_ether_addr(dest)) {
@@ -204,7 +208,7 @@ drop:
204EXPORT_SYMBOL_GPL(br_handle_frame_finish); 208EXPORT_SYMBOL_GPL(br_handle_frame_finish);
205 209
206/* note: already called with rcu_read_lock */ 210/* note: already called with rcu_read_lock */
207static int br_handle_local_finish(struct sk_buff *skb) 211static int br_handle_local_finish(struct sock *sk, struct sk_buff *skb)
208{ 212{
209 struct net_bridge_port *p = br_port_get_rcu(skb->dev); 213 struct net_bridge_port *p = br_port_get_rcu(skb->dev);
210 u16 vid = 0; 214 u16 vid = 0;
@@ -274,8 +278,8 @@ rx_handler_result_t br_handle_frame(struct sk_buff **pskb)
274 } 278 }
275 279
276 /* Deliver packet to local host only */ 280 /* Deliver packet to local host only */
277 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, skb, skb->dev, 281 if (NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_IN, NULL, skb,
278 NULL, br_handle_local_finish)) { 282 skb->dev, NULL, br_handle_local_finish)) {
279 return RX_HANDLER_CONSUMED; /* consumed by filter */ 283 return RX_HANDLER_CONSUMED; /* consumed by filter */
280 } else { 284 } else {
281 *pskb = skb; 285 *pskb = skb;
@@ -299,7 +303,8 @@ forward:
299 if (ether_addr_equal(p->br->dev->dev_addr, dest)) 303 if (ether_addr_equal(p->br->dev->dev_addr, dest))
300 skb->pkt_type = PACKET_HOST; 304 skb->pkt_type = PACKET_HOST;
301 305
302 NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL, 306 NF_HOOK(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, NULL, skb,
307 skb->dev, NULL,
303 br_handle_frame_finish); 308 br_handle_frame_finish);
304 break; 309 break;
305 default: 310 default:
diff --git a/net/bridge/br_multicast.c b/net/bridge/br_multicast.c
index c465876c7861..4b6722f8f179 100644
--- a/net/bridge/br_multicast.c
+++ b/net/bridge/br_multicast.c
@@ -814,7 +814,8 @@ static void __br_multicast_send_query(struct net_bridge *br,
814 814
815 if (port) { 815 if (port) {
816 skb->dev = port->dev; 816 skb->dev = port->dev;
817 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 817 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
818 NULL, skb->dev,
818 br_dev_queue_push_xmit); 819 br_dev_queue_push_xmit);
819 } else { 820 } else {
820 br_multicast_select_own_querier(br, ip, skb); 821 br_multicast_select_own_querier(br, ip, skb);
diff --git a/net/bridge/br_netfilter.c b/net/bridge/br_netfilter.c
index 0ee453fad3de..ab55e2472beb 100644
--- a/net/bridge/br_netfilter.c
+++ b/net/bridge/br_netfilter.c
@@ -37,17 +37,16 @@
37#include <net/route.h> 37#include <net/route.h>
38#include <net/netfilter/br_netfilter.h> 38#include <net/netfilter/br_netfilter.h>
39 39
40#if IS_ENABLED(CONFIG_NF_CONNTRACK)
41#include <net/netfilter/nf_conntrack.h>
42#endif
43
40#include <asm/uaccess.h> 44#include <asm/uaccess.h>
41#include "br_private.h" 45#include "br_private.h"
42#ifdef CONFIG_SYSCTL 46#ifdef CONFIG_SYSCTL
43#include <linux/sysctl.h> 47#include <linux/sysctl.h>
44#endif 48#endif
45 49
46#define skb_origaddr(skb) (((struct bridge_skb_cb *) \
47 (skb->nf_bridge->data))->daddr.ipv4)
48#define store_orig_dstaddr(skb) (skb_origaddr(skb) = ip_hdr(skb)->daddr)
49#define dnat_took_place(skb) (skb_origaddr(skb) != ip_hdr(skb)->daddr)
50
51#ifdef CONFIG_SYSCTL 50#ifdef CONFIG_SYSCTL
52static struct ctl_table_header *brnf_sysctl_header; 51static struct ctl_table_header *brnf_sysctl_header;
53static int brnf_call_iptables __read_mostly = 1; 52static int brnf_call_iptables __read_mostly = 1;
@@ -112,6 +111,24 @@ static inline __be16 pppoe_proto(const struct sk_buff *skb)
112 pppoe_proto(skb) == htons(PPP_IPV6) && \ 111 pppoe_proto(skb) == htons(PPP_IPV6) && \
113 brnf_filter_pppoe_tagged) 112 brnf_filter_pppoe_tagged)
114 113
114/* largest possible L2 header, see br_nf_dev_queue_xmit() */
115#define NF_BRIDGE_MAX_MAC_HEADER_LENGTH (PPPOE_SES_HLEN + ETH_HLEN)
116
117#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
118struct brnf_frag_data {
119 char mac[NF_BRIDGE_MAX_MAC_HEADER_LENGTH];
120 u8 encap_size;
121 u8 size;
122};
123
124static DEFINE_PER_CPU(struct brnf_frag_data, brnf_frag_data_storage);
125#endif
126
127static struct nf_bridge_info *nf_bridge_info_get(const struct sk_buff *skb)
128{
129 return skb->nf_bridge;
130}
131
115static inline struct rtable *bridge_parent_rtable(const struct net_device *dev) 132static inline struct rtable *bridge_parent_rtable(const struct net_device *dev)
116{ 133{
117 struct net_bridge_port *port; 134 struct net_bridge_port *port;
@@ -154,6 +171,18 @@ static inline struct nf_bridge_info *nf_bridge_unshare(struct sk_buff *skb)
154 return nf_bridge; 171 return nf_bridge;
155} 172}
156 173
174static unsigned int nf_bridge_encap_header_len(const struct sk_buff *skb)
175{
176 switch (skb->protocol) {
177 case __cpu_to_be16(ETH_P_8021Q):
178 return VLAN_HLEN;
179 case __cpu_to_be16(ETH_P_PPP_SES):
180 return PPPOE_SES_HLEN;
181 default:
182 return 0;
183 }
184}
185
157static inline void nf_bridge_push_encap_header(struct sk_buff *skb) 186static inline void nf_bridge_push_encap_header(struct sk_buff *skb)
158{ 187{
159 unsigned int len = nf_bridge_encap_header_len(skb); 188 unsigned int len = nf_bridge_encap_header_len(skb);
@@ -178,14 +207,6 @@ static inline void nf_bridge_pull_encap_header_rcsum(struct sk_buff *skb)
178 skb->network_header += len; 207 skb->network_header += len;
179} 208}
180 209
181static inline void nf_bridge_save_header(struct sk_buff *skb)
182{
183 int header_size = ETH_HLEN + nf_bridge_encap_header_len(skb);
184
185 skb_copy_from_linear_data_offset(skb, -header_size,
186 skb->nf_bridge->data, header_size);
187}
188
189/* When handing a packet over to the IP layer 210/* When handing a packet over to the IP layer
190 * check whether we have a skb that is in the 211 * check whether we have a skb that is in the
191 * expected format 212 * expected format
@@ -239,17 +260,31 @@ drop:
239 return -1; 260 return -1;
240} 261}
241 262
263static void nf_bridge_update_protocol(struct sk_buff *skb)
264{
265 switch (skb->nf_bridge->orig_proto) {
266 case BRNF_PROTO_8021Q:
267 skb->protocol = htons(ETH_P_8021Q);
268 break;
269 case BRNF_PROTO_PPPOE:
270 skb->protocol = htons(ETH_P_PPP_SES);
271 break;
272 case BRNF_PROTO_UNCHANGED:
273 break;
274 }
275}
276
242/* PF_BRIDGE/PRE_ROUTING *********************************************/ 277/* PF_BRIDGE/PRE_ROUTING *********************************************/
243/* Undo the changes made for ip6tables PREROUTING and continue the 278/* Undo the changes made for ip6tables PREROUTING and continue the
244 * bridge PRE_ROUTING hook. */ 279 * bridge PRE_ROUTING hook. */
245static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb) 280static int br_nf_pre_routing_finish_ipv6(struct sock *sk, struct sk_buff *skb)
246{ 281{
247 struct nf_bridge_info *nf_bridge = skb->nf_bridge; 282 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
248 struct rtable *rt; 283 struct rtable *rt;
249 284
250 if (nf_bridge->mask & BRNF_PKT_TYPE) { 285 if (nf_bridge->pkt_otherhost) {
251 skb->pkt_type = PACKET_OTHERHOST; 286 skb->pkt_type = PACKET_OTHERHOST;
252 nf_bridge->mask ^= BRNF_PKT_TYPE; 287 nf_bridge->pkt_otherhost = false;
253 } 288 }
254 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING; 289 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
255 290
@@ -263,7 +298,8 @@ static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
263 skb->dev = nf_bridge->physindev; 298 skb->dev = nf_bridge->physindev;
264 nf_bridge_update_protocol(skb); 299 nf_bridge_update_protocol(skb);
265 nf_bridge_push_encap_header(skb); 300 nf_bridge_push_encap_header(skb);
266 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL, 301 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
302 skb->dev, NULL,
267 br_handle_frame_finish, 1); 303 br_handle_frame_finish, 1);
268 304
269 return 0; 305 return 0;
@@ -274,9 +310,8 @@ static int br_nf_pre_routing_finish_ipv6(struct sk_buff *skb)
274 * don't, we use the neighbour framework to find out. In both cases, we make 310 * don't, we use the neighbour framework to find out. In both cases, we make
275 * sure that br_handle_frame_finish() is called afterwards. 311 * sure that br_handle_frame_finish() is called afterwards.
276 */ 312 */
277static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb) 313static int br_nf_pre_routing_finish_bridge(struct sock *sk, struct sk_buff *skb)
278{ 314{
279 struct nf_bridge_info *nf_bridge = skb->nf_bridge;
280 struct neighbour *neigh; 315 struct neighbour *neigh;
281 struct dst_entry *dst; 316 struct dst_entry *dst;
282 317
@@ -286,12 +321,13 @@ static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
286 dst = skb_dst(skb); 321 dst = skb_dst(skb);
287 neigh = dst_neigh_lookup_skb(dst, skb); 322 neigh = dst_neigh_lookup_skb(dst, skb);
288 if (neigh) { 323 if (neigh) {
324 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
289 int ret; 325 int ret;
290 326
291 if (neigh->hh.hh_len) { 327 if (neigh->hh.hh_len) {
292 neigh_hh_bridge(&neigh->hh, skb); 328 neigh_hh_bridge(&neigh->hh, skb);
293 skb->dev = nf_bridge->physindev; 329 skb->dev = nf_bridge->physindev;
294 ret = br_handle_frame_finish(skb); 330 ret = br_handle_frame_finish(sk, skb);
295 } else { 331 } else {
296 /* the neighbour function below overwrites the complete 332 /* the neighbour function below overwrites the complete
297 * MAC header, so we save the Ethernet source address and 333 * MAC header, so we save the Ethernet source address and
@@ -299,7 +335,7 @@ static int br_nf_pre_routing_finish_bridge(struct sk_buff *skb)
299 */ 335 */
300 skb_copy_from_linear_data_offset(skb, 336 skb_copy_from_linear_data_offset(skb,
301 -(ETH_HLEN-ETH_ALEN), 337 -(ETH_HLEN-ETH_ALEN),
302 skb->nf_bridge->data, 338 nf_bridge->neigh_header,
303 ETH_HLEN-ETH_ALEN); 339 ETH_HLEN-ETH_ALEN);
304 /* tell br_dev_xmit to continue with forwarding */ 340 /* tell br_dev_xmit to continue with forwarding */
305 nf_bridge->mask |= BRNF_BRIDGED_DNAT; 341 nf_bridge->mask |= BRNF_BRIDGED_DNAT;
@@ -314,6 +350,22 @@ free_skb:
314 return 0; 350 return 0;
315} 351}
316 352
353static bool dnat_took_place(const struct sk_buff *skb)
354{
355#if IS_ENABLED(CONFIG_NF_CONNTRACK)
356 enum ip_conntrack_info ctinfo;
357 struct nf_conn *ct;
358
359 ct = nf_ct_get(skb, &ctinfo);
360 if (!ct || nf_ct_is_untracked(ct))
361 return false;
362
363 return test_bit(IPS_DST_NAT_BIT, &ct->status);
364#else
365 return false;
366#endif
367}
368
317/* This requires some explaining. If DNAT has taken place, 369/* This requires some explaining. If DNAT has taken place,
318 * we will need to fix up the destination Ethernet address. 370 * we will need to fix up the destination Ethernet address.
319 * 371 *
@@ -352,11 +404,11 @@ free_skb:
352 * device, we proceed as if ip_route_input() succeeded. If it differs from the 404 * device, we proceed as if ip_route_input() succeeded. If it differs from the
353 * logical bridge port or if ip_route_output_key() fails we drop the packet. 405 * logical bridge port or if ip_route_output_key() fails we drop the packet.
354 */ 406 */
355static int br_nf_pre_routing_finish(struct sk_buff *skb) 407static int br_nf_pre_routing_finish(struct sock *sk, struct sk_buff *skb)
356{ 408{
357 struct net_device *dev = skb->dev; 409 struct net_device *dev = skb->dev;
358 struct iphdr *iph = ip_hdr(skb); 410 struct iphdr *iph = ip_hdr(skb);
359 struct nf_bridge_info *nf_bridge = skb->nf_bridge; 411 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
360 struct rtable *rt; 412 struct rtable *rt;
361 int err; 413 int err;
362 int frag_max_size; 414 int frag_max_size;
@@ -364,9 +416,9 @@ static int br_nf_pre_routing_finish(struct sk_buff *skb)
364 frag_max_size = IPCB(skb)->frag_max_size; 416 frag_max_size = IPCB(skb)->frag_max_size;
365 BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size; 417 BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size;
366 418
367 if (nf_bridge->mask & BRNF_PKT_TYPE) { 419 if (nf_bridge->pkt_otherhost) {
368 skb->pkt_type = PACKET_OTHERHOST; 420 skb->pkt_type = PACKET_OTHERHOST;
369 nf_bridge->mask ^= BRNF_PKT_TYPE; 421 nf_bridge->pkt_otherhost = false;
370 } 422 }
371 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING; 423 nf_bridge->mask ^= BRNF_NF_BRIDGE_PREROUTING;
372 if (dnat_took_place(skb)) { 424 if (dnat_took_place(skb)) {
@@ -405,7 +457,7 @@ bridged_dnat:
405 nf_bridge_push_encap_header(skb); 457 nf_bridge_push_encap_header(skb);
406 NF_HOOK_THRESH(NFPROTO_BRIDGE, 458 NF_HOOK_THRESH(NFPROTO_BRIDGE,
407 NF_BR_PRE_ROUTING, 459 NF_BR_PRE_ROUTING,
408 skb, skb->dev, NULL, 460 sk, skb, skb->dev, NULL,
409 br_nf_pre_routing_finish_bridge, 461 br_nf_pre_routing_finish_bridge,
410 1); 462 1);
411 return 0; 463 return 0;
@@ -425,7 +477,8 @@ bridged_dnat:
425 skb->dev = nf_bridge->physindev; 477 skb->dev = nf_bridge->physindev;
426 nf_bridge_update_protocol(skb); 478 nf_bridge_update_protocol(skb);
427 nf_bridge_push_encap_header(skb); 479 nf_bridge_push_encap_header(skb);
428 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, skb, skb->dev, NULL, 480 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_PRE_ROUTING, sk, skb,
481 skb->dev, NULL,
429 br_handle_frame_finish, 1); 482 br_handle_frame_finish, 1);
430 483
431 return 0; 484 return 0;
@@ -448,20 +501,21 @@ static struct net_device *brnf_get_logical_dev(struct sk_buff *skb, const struct
448/* Some common code for IPv4/IPv6 */ 501/* Some common code for IPv4/IPv6 */
449static struct net_device *setup_pre_routing(struct sk_buff *skb) 502static struct net_device *setup_pre_routing(struct sk_buff *skb)
450{ 503{
451 struct nf_bridge_info *nf_bridge = skb->nf_bridge; 504 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
452 505
453 if (skb->pkt_type == PACKET_OTHERHOST) { 506 if (skb->pkt_type == PACKET_OTHERHOST) {
454 skb->pkt_type = PACKET_HOST; 507 skb->pkt_type = PACKET_HOST;
455 nf_bridge->mask |= BRNF_PKT_TYPE; 508 nf_bridge->pkt_otherhost = true;
456 } 509 }
457 510
458 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING; 511 nf_bridge->mask |= BRNF_NF_BRIDGE_PREROUTING;
459 nf_bridge->physindev = skb->dev; 512 nf_bridge->physindev = skb->dev;
460 skb->dev = brnf_get_logical_dev(skb, skb->dev); 513 skb->dev = brnf_get_logical_dev(skb, skb->dev);
514
461 if (skb->protocol == htons(ETH_P_8021Q)) 515 if (skb->protocol == htons(ETH_P_8021Q))
462 nf_bridge->mask |= BRNF_8021Q; 516 nf_bridge->orig_proto = BRNF_PROTO_8021Q;
463 else if (skb->protocol == htons(ETH_P_PPP_SES)) 517 else if (skb->protocol == htons(ETH_P_PPP_SES))
464 nf_bridge->mask |= BRNF_PPPoE; 518 nf_bridge->orig_proto = BRNF_PROTO_PPPOE;
465 519
466 /* Must drop socket now because of tproxy. */ 520 /* Must drop socket now because of tproxy. */
467 skb_orphan(skb); 521 skb_orphan(skb);
@@ -527,9 +581,7 @@ bad:
527 * to ip6tables, which doesn't support NAT, so things are fairly simple. */ 581 * to ip6tables, which doesn't support NAT, so things are fairly simple. */
528static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops, 582static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
529 struct sk_buff *skb, 583 struct sk_buff *skb,
530 const struct net_device *in, 584 const struct nf_hook_state *state)
531 const struct net_device *out,
532 int (*okfn)(struct sk_buff *))
533{ 585{
534 const struct ipv6hdr *hdr; 586 const struct ipv6hdr *hdr;
535 u32 pkt_len; 587 u32 pkt_len;
@@ -563,7 +615,8 @@ static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
563 return NF_DROP; 615 return NF_DROP;
564 616
565 skb->protocol = htons(ETH_P_IPV6); 617 skb->protocol = htons(ETH_P_IPV6);
566 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL, 618 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, state->sk, skb,
619 skb->dev, NULL,
567 br_nf_pre_routing_finish_ipv6); 620 br_nf_pre_routing_finish_ipv6);
568 621
569 return NF_STOLEN; 622 return NF_STOLEN;
@@ -577,9 +630,7 @@ static unsigned int br_nf_pre_routing_ipv6(const struct nf_hook_ops *ops,
577 * address to be able to detect DNAT afterwards. */ 630 * address to be able to detect DNAT afterwards. */
578static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops, 631static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
579 struct sk_buff *skb, 632 struct sk_buff *skb,
580 const struct net_device *in, 633 const struct nf_hook_state *state)
581 const struct net_device *out,
582 int (*okfn)(struct sk_buff *))
583{ 634{
584 struct net_bridge_port *p; 635 struct net_bridge_port *p;
585 struct net_bridge *br; 636 struct net_bridge *br;
@@ -588,7 +639,7 @@ static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
588 if (unlikely(!pskb_may_pull(skb, len))) 639 if (unlikely(!pskb_may_pull(skb, len)))
589 return NF_DROP; 640 return NF_DROP;
590 641
591 p = br_port_get_rcu(in); 642 p = br_port_get_rcu(state->in);
592 if (p == NULL) 643 if (p == NULL)
593 return NF_DROP; 644 return NF_DROP;
594 br = p->br; 645 br = p->br;
@@ -598,7 +649,7 @@ static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
598 return NF_ACCEPT; 649 return NF_ACCEPT;
599 650
600 nf_bridge_pull_encap_header_rcsum(skb); 651 nf_bridge_pull_encap_header_rcsum(skb);
601 return br_nf_pre_routing_ipv6(ops, skb, in, out, okfn); 652 return br_nf_pre_routing_ipv6(ops, skb, state);
602 } 653 }
603 654
604 if (!brnf_call_iptables && !br->nf_call_iptables) 655 if (!brnf_call_iptables && !br->nf_call_iptables)
@@ -617,10 +668,11 @@ static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
617 return NF_DROP; 668 return NF_DROP;
618 if (!setup_pre_routing(skb)) 669 if (!setup_pre_routing(skb))
619 return NF_DROP; 670 return NF_DROP;
620 store_orig_dstaddr(skb); 671
621 skb->protocol = htons(ETH_P_IP); 672 skb->protocol = htons(ETH_P_IP);
622 673
623 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL, 674 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, state->sk, skb,
675 skb->dev, NULL,
624 br_nf_pre_routing_finish); 676 br_nf_pre_routing_finish);
625 677
626 return NF_STOLEN; 678 return NF_STOLEN;
@@ -636,25 +688,30 @@ static unsigned int br_nf_pre_routing(const struct nf_hook_ops *ops,
636 * prevent this from happening. */ 688 * prevent this from happening. */
637static unsigned int br_nf_local_in(const struct nf_hook_ops *ops, 689static unsigned int br_nf_local_in(const struct nf_hook_ops *ops,
638 struct sk_buff *skb, 690 struct sk_buff *skb,
639 const struct net_device *in, 691 const struct nf_hook_state *state)
640 const struct net_device *out,
641 int (*okfn)(struct sk_buff *))
642{ 692{
643 br_drop_fake_rtable(skb); 693 br_drop_fake_rtable(skb);
644 return NF_ACCEPT; 694 return NF_ACCEPT;
645} 695}
646 696
647/* PF_BRIDGE/FORWARD *************************************************/ 697/* PF_BRIDGE/FORWARD *************************************************/
648static int br_nf_forward_finish(struct sk_buff *skb) 698static int br_nf_forward_finish(struct sock *sk, struct sk_buff *skb)
649{ 699{
650 struct nf_bridge_info *nf_bridge = skb->nf_bridge; 700 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
651 struct net_device *in; 701 struct net_device *in;
652 702
653 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) { 703 if (!IS_ARP(skb) && !IS_VLAN_ARP(skb)) {
704 int frag_max_size;
705
706 if (skb->protocol == htons(ETH_P_IP)) {
707 frag_max_size = IPCB(skb)->frag_max_size;
708 BR_INPUT_SKB_CB(skb)->frag_max_size = frag_max_size;
709 }
710
654 in = nf_bridge->physindev; 711 in = nf_bridge->physindev;
655 if (nf_bridge->mask & BRNF_PKT_TYPE) { 712 if (nf_bridge->pkt_otherhost) {
656 skb->pkt_type = PACKET_OTHERHOST; 713 skb->pkt_type = PACKET_OTHERHOST;
657 nf_bridge->mask ^= BRNF_PKT_TYPE; 714 nf_bridge->pkt_otherhost = false;
658 } 715 }
659 nf_bridge_update_protocol(skb); 716 nf_bridge_update_protocol(skb);
660 } else { 717 } else {
@@ -662,8 +719,8 @@ static int br_nf_forward_finish(struct sk_buff *skb)
662 } 719 }
663 nf_bridge_push_encap_header(skb); 720 nf_bridge_push_encap_header(skb);
664 721
665 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, skb, in, 722 NF_HOOK_THRESH(NFPROTO_BRIDGE, NF_BR_FORWARD, sk, skb,
666 skb->dev, br_forward_finish, 1); 723 in, skb->dev, br_forward_finish, 1);
667 return 0; 724 return 0;
668} 725}
669 726
@@ -675,9 +732,7 @@ static int br_nf_forward_finish(struct sk_buff *skb)
675 * bridge ports. */ 732 * bridge ports. */
676static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops, 733static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
677 struct sk_buff *skb, 734 struct sk_buff *skb,
678 const struct net_device *in, 735 const struct nf_hook_state *state)
679 const struct net_device *out,
680 int (*okfn)(struct sk_buff *))
681{ 736{
682 struct nf_bridge_info *nf_bridge; 737 struct nf_bridge_info *nf_bridge;
683 struct net_device *parent; 738 struct net_device *parent;
@@ -691,7 +746,11 @@ static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
691 if (!nf_bridge_unshare(skb)) 746 if (!nf_bridge_unshare(skb))
692 return NF_DROP; 747 return NF_DROP;
693 748
694 parent = bridge_parent(out); 749 nf_bridge = nf_bridge_info_get(skb);
750 if (!nf_bridge)
751 return NF_DROP;
752
753 parent = bridge_parent(state->out);
695 if (!parent) 754 if (!parent)
696 return NF_DROP; 755 return NF_DROP;
697 756
@@ -704,40 +763,42 @@ static unsigned int br_nf_forward_ip(const struct nf_hook_ops *ops,
704 763
705 nf_bridge_pull_encap_header(skb); 764 nf_bridge_pull_encap_header(skb);
706 765
707 nf_bridge = skb->nf_bridge;
708 if (skb->pkt_type == PACKET_OTHERHOST) { 766 if (skb->pkt_type == PACKET_OTHERHOST) {
709 skb->pkt_type = PACKET_HOST; 767 skb->pkt_type = PACKET_HOST;
710 nf_bridge->mask |= BRNF_PKT_TYPE; 768 nf_bridge->pkt_otherhost = true;
711 } 769 }
712 770
713 if (pf == NFPROTO_IPV4 && br_parse_ip_options(skb)) 771 if (pf == NFPROTO_IPV4) {
714 return NF_DROP; 772 int frag_max = BR_INPUT_SKB_CB(skb)->frag_max_size;
773
774 if (br_parse_ip_options(skb))
775 return NF_DROP;
776
777 IPCB(skb)->frag_max_size = frag_max;
778 }
715 779
716 /* The physdev module checks on this */
717 nf_bridge->mask |= BRNF_BRIDGED;
718 nf_bridge->physoutdev = skb->dev; 780 nf_bridge->physoutdev = skb->dev;
719 if (pf == NFPROTO_IPV4) 781 if (pf == NFPROTO_IPV4)
720 skb->protocol = htons(ETH_P_IP); 782 skb->protocol = htons(ETH_P_IP);
721 else 783 else
722 skb->protocol = htons(ETH_P_IPV6); 784 skb->protocol = htons(ETH_P_IPV6);
723 785
724 NF_HOOK(pf, NF_INET_FORWARD, skb, brnf_get_logical_dev(skb, in), parent, 786 NF_HOOK(pf, NF_INET_FORWARD, NULL, skb,
725 br_nf_forward_finish); 787 brnf_get_logical_dev(skb, state->in),
788 parent, br_nf_forward_finish);
726 789
727 return NF_STOLEN; 790 return NF_STOLEN;
728} 791}
729 792
730static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops, 793static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
731 struct sk_buff *skb, 794 struct sk_buff *skb,
732 const struct net_device *in, 795 const struct nf_hook_state *state)
733 const struct net_device *out,
734 int (*okfn)(struct sk_buff *))
735{ 796{
736 struct net_bridge_port *p; 797 struct net_bridge_port *p;
737 struct net_bridge *br; 798 struct net_bridge *br;
738 struct net_device **d = (struct net_device **)(skb->cb); 799 struct net_device **d = (struct net_device **)(skb->cb);
739 800
740 p = br_port_get_rcu(out); 801 p = br_port_get_rcu(state->out);
741 if (p == NULL) 802 if (p == NULL)
742 return NF_ACCEPT; 803 return NF_ACCEPT;
743 br = p->br; 804 br = p->br;
@@ -756,55 +817,93 @@ static unsigned int br_nf_forward_arp(const struct nf_hook_ops *ops,
756 nf_bridge_push_encap_header(skb); 817 nf_bridge_push_encap_header(skb);
757 return NF_ACCEPT; 818 return NF_ACCEPT;
758 } 819 }
759 *d = (struct net_device *)in; 820 *d = state->in;
760 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, skb, (struct net_device *)in, 821 NF_HOOK(NFPROTO_ARP, NF_ARP_FORWARD, state->sk, skb,
761 (struct net_device *)out, br_nf_forward_finish); 822 state->in, state->out, br_nf_forward_finish);
762 823
763 return NF_STOLEN; 824 return NF_STOLEN;
764} 825}
765 826
766#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4) 827#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV4)
767static int br_nf_dev_queue_xmit(struct sk_buff *skb) 828static int br_nf_push_frag_xmit(struct sock *sk, struct sk_buff *skb)
829{
830 struct brnf_frag_data *data;
831 int err;
832
833 data = this_cpu_ptr(&brnf_frag_data_storage);
834 err = skb_cow_head(skb, data->size);
835
836 if (err) {
837 kfree_skb(skb);
838 return 0;
839 }
840
841 skb_copy_to_linear_data_offset(skb, -data->size, data->mac, data->size);
842 __skb_push(skb, data->encap_size);
843
844 return br_dev_queue_push_xmit(sk, skb);
845}
846
847static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
768{ 848{
769 int ret; 849 int ret;
770 int frag_max_size; 850 int frag_max_size;
851 unsigned int mtu_reserved;
852
853 if (skb_is_gso(skb) || skb->protocol != htons(ETH_P_IP))
854 return br_dev_queue_push_xmit(sk, skb);
771 855
856 mtu_reserved = nf_bridge_mtu_reduction(skb);
772 /* This is wrong! We should preserve the original fragment 857 /* This is wrong! We should preserve the original fragment
773 * boundaries by preserving frag_list rather than refragmenting. 858 * boundaries by preserving frag_list rather than refragmenting.
774 */ 859 */
775 if (skb->protocol == htons(ETH_P_IP) && 860 if (skb->len + mtu_reserved > skb->dev->mtu) {
776 skb->len + nf_bridge_mtu_reduction(skb) > skb->dev->mtu && 861 struct brnf_frag_data *data;
777 !skb_is_gso(skb)) { 862
778 frag_max_size = BR_INPUT_SKB_CB(skb)->frag_max_size; 863 frag_max_size = BR_INPUT_SKB_CB(skb)->frag_max_size;
779 if (br_parse_ip_options(skb)) 864 if (br_parse_ip_options(skb))
780 /* Drop invalid packet */ 865 /* Drop invalid packet */
781 return NF_DROP; 866 return NF_DROP;
782 IPCB(skb)->frag_max_size = frag_max_size; 867 IPCB(skb)->frag_max_size = frag_max_size;
783 ret = ip_fragment(skb, br_dev_queue_push_xmit); 868
784 } else 869 nf_bridge_update_protocol(skb);
785 ret = br_dev_queue_push_xmit(skb); 870
871 data = this_cpu_ptr(&brnf_frag_data_storage);
872 data->encap_size = nf_bridge_encap_header_len(skb);
873 data->size = ETH_HLEN + data->encap_size;
874
875 skb_copy_from_linear_data_offset(skb, -data->size, data->mac,
876 data->size);
877
878 ret = ip_fragment(sk, skb, br_nf_push_frag_xmit);
879 } else {
880 ret = br_dev_queue_push_xmit(sk, skb);
881 }
786 882
787 return ret; 883 return ret;
788} 884}
789#else 885#else
790static int br_nf_dev_queue_xmit(struct sk_buff *skb) 886static int br_nf_dev_queue_xmit(struct sock *sk, struct sk_buff *skb)
791{ 887{
792 return br_dev_queue_push_xmit(skb); 888 return br_dev_queue_push_xmit(sk, skb);
793} 889}
794#endif 890#endif
795 891
796/* PF_BRIDGE/POST_ROUTING ********************************************/ 892/* PF_BRIDGE/POST_ROUTING ********************************************/
797static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops, 893static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
798 struct sk_buff *skb, 894 struct sk_buff *skb,
799 const struct net_device *in, 895 const struct nf_hook_state *state)
800 const struct net_device *out,
801 int (*okfn)(struct sk_buff *))
802{ 896{
803 struct nf_bridge_info *nf_bridge = skb->nf_bridge; 897 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
804 struct net_device *realoutdev = bridge_parent(skb->dev); 898 struct net_device *realoutdev = bridge_parent(skb->dev);
805 u_int8_t pf; 899 u_int8_t pf;
806 900
807 if (!nf_bridge || !(nf_bridge->mask & BRNF_BRIDGED)) 901 /* if nf_bridge is set, but ->physoutdev is NULL, this packet came in
902 * on a bridge, but was delivered locally and is now being routed:
903 *
904 * POST_ROUTING was already invoked from the ip stack.
905 */
906 if (!nf_bridge || !nf_bridge->physoutdev)
808 return NF_ACCEPT; 907 return NF_ACCEPT;
809 908
810 if (!realoutdev) 909 if (!realoutdev)
@@ -821,17 +920,17 @@ static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
821 * about the value of skb->pkt_type. */ 920 * about the value of skb->pkt_type. */
822 if (skb->pkt_type == PACKET_OTHERHOST) { 921 if (skb->pkt_type == PACKET_OTHERHOST) {
823 skb->pkt_type = PACKET_HOST; 922 skb->pkt_type = PACKET_HOST;
824 nf_bridge->mask |= BRNF_PKT_TYPE; 923 nf_bridge->pkt_otherhost = true;
825 } 924 }
826 925
827 nf_bridge_pull_encap_header(skb); 926 nf_bridge_pull_encap_header(skb);
828 nf_bridge_save_header(skb);
829 if (pf == NFPROTO_IPV4) 927 if (pf == NFPROTO_IPV4)
830 skb->protocol = htons(ETH_P_IP); 928 skb->protocol = htons(ETH_P_IP);
831 else 929 else
832 skb->protocol = htons(ETH_P_IPV6); 930 skb->protocol = htons(ETH_P_IPV6);
833 931
834 NF_HOOK(pf, NF_INET_POST_ROUTING, skb, NULL, realoutdev, 932 NF_HOOK(pf, NF_INET_POST_ROUTING, state->sk, skb,
933 NULL, realoutdev,
835 br_nf_dev_queue_xmit); 934 br_nf_dev_queue_xmit);
836 935
837 return NF_STOLEN; 936 return NF_STOLEN;
@@ -842,9 +941,7 @@ static unsigned int br_nf_post_routing(const struct nf_hook_ops *ops,
842 * for the second time. */ 941 * for the second time. */
843static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops, 942static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
844 struct sk_buff *skb, 943 struct sk_buff *skb,
845 const struct net_device *in, 944 const struct nf_hook_state *state)
846 const struct net_device *out,
847 int (*okfn)(struct sk_buff *))
848{ 945{
849 if (skb->nf_bridge && 946 if (skb->nf_bridge &&
850 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) { 947 !(skb->nf_bridge->mask & BRNF_NF_BRIDGE_PREROUTING)) {
@@ -854,6 +951,44 @@ static unsigned int ip_sabotage_in(const struct nf_hook_ops *ops,
854 return NF_ACCEPT; 951 return NF_ACCEPT;
855} 952}
856 953
954/* This is called when br_netfilter has called into iptables/netfilter,
955 * and DNAT has taken place on a bridge-forwarded packet.
956 *
957 * neigh->output has created a new MAC header, with local br0 MAC
958 * as saddr.
959 *
960 * This restores the original MAC saddr of the bridged packet
961 * before invoking bridge forward logic to transmit the packet.
962 */
963static void br_nf_pre_routing_finish_bridge_slow(struct sk_buff *skb)
964{
965 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
966
967 skb_pull(skb, ETH_HLEN);
968 nf_bridge->mask &= ~BRNF_BRIDGED_DNAT;
969
970 BUILD_BUG_ON(sizeof(nf_bridge->neigh_header) != (ETH_HLEN - ETH_ALEN));
971
972 skb_copy_to_linear_data_offset(skb, -(ETH_HLEN - ETH_ALEN),
973 nf_bridge->neigh_header,
974 ETH_HLEN - ETH_ALEN);
975 skb->dev = nf_bridge->physindev;
976 br_handle_frame_finish(NULL, skb);
977}
978
979static int br_nf_dev_xmit(struct sk_buff *skb)
980{
981 if (skb->nf_bridge && (skb->nf_bridge->mask & BRNF_BRIDGED_DNAT)) {
982 br_nf_pre_routing_finish_bridge_slow(skb);
983 return 1;
984 }
985 return 0;
986}
987
988static const struct nf_br_ops br_ops = {
989 .br_dev_xmit_hook = br_nf_dev_xmit,
990};
991
857void br_netfilter_enable(void) 992void br_netfilter_enable(void)
858{ 993{
859} 994}
@@ -991,12 +1126,14 @@ static int __init br_netfilter_init(void)
991 return -ENOMEM; 1126 return -ENOMEM;
992 } 1127 }
993#endif 1128#endif
1129 RCU_INIT_POINTER(nf_br_ops, &br_ops);
994 printk(KERN_NOTICE "Bridge firewalling registered\n"); 1130 printk(KERN_NOTICE "Bridge firewalling registered\n");
995 return 0; 1131 return 0;
996} 1132}
997 1133
998static void __exit br_netfilter_fini(void) 1134static void __exit br_netfilter_fini(void)
999{ 1135{
1136 RCU_INIT_POINTER(nf_br_ops, NULL);
1000 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops)); 1137 nf_unregister_hooks(br_nf_ops, ARRAY_SIZE(br_nf_ops));
1001#ifdef CONFIG_SYSCTL 1138#ifdef CONFIG_SYSCTL
1002 unregister_net_sysctl_table(brnf_sysctl_header); 1139 unregister_net_sysctl_table(brnf_sysctl_header);
diff --git a/net/bridge/br_netlink.c b/net/bridge/br_netlink.c
index 4fbcea0e7ecb..0e4ddb81610d 100644
--- a/net/bridge/br_netlink.c
+++ b/net/bridge/br_netlink.c
@@ -22,6 +22,85 @@
22#include "br_private.h" 22#include "br_private.h"
23#include "br_private_stp.h" 23#include "br_private_stp.h"
24 24
25static int br_get_num_vlan_infos(const struct net_port_vlans *pv,
26 u32 filter_mask)
27{
28 u16 vid_range_start = 0, vid_range_end = 0;
29 u16 vid_range_flags = 0;
30 u16 pvid, vid, flags;
31 int num_vlans = 0;
32
33 if (filter_mask & RTEXT_FILTER_BRVLAN)
34 return pv->num_vlans;
35
36 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
37 return 0;
38
39 /* Count number of vlan info's
40 */
41 pvid = br_get_pvid(pv);
42 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
43 flags = 0;
44 if (vid == pvid)
45 flags |= BRIDGE_VLAN_INFO_PVID;
46
47 if (test_bit(vid, pv->untagged_bitmap))
48 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
49
50 if (vid_range_start == 0) {
51 goto initvars;
52 } else if ((vid - vid_range_end) == 1 &&
53 flags == vid_range_flags) {
54 vid_range_end = vid;
55 continue;
56 } else {
57 if ((vid_range_end - vid_range_start) > 0)
58 num_vlans += 2;
59 else
60 num_vlans += 1;
61 }
62initvars:
63 vid_range_start = vid;
64 vid_range_end = vid;
65 vid_range_flags = flags;
66 }
67
68 if (vid_range_start != 0) {
69 if ((vid_range_end - vid_range_start) > 0)
70 num_vlans += 2;
71 else
72 num_vlans += 1;
73 }
74
75 return num_vlans;
76}
77
78static size_t br_get_link_af_size_filtered(const struct net_device *dev,
79 u32 filter_mask)
80{
81 struct net_port_vlans *pv;
82 int num_vlan_infos;
83
84 rcu_read_lock();
85 if (br_port_exists(dev))
86 pv = nbp_get_vlan_info(br_port_get_rcu(dev));
87 else if (dev->priv_flags & IFF_EBRIDGE)
88 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
89 else
90 pv = NULL;
91 if (pv)
92 num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask);
93 else
94 num_vlan_infos = 0;
95 rcu_read_unlock();
96
97 if (!num_vlan_infos)
98 return 0;
99
100 /* Each VLAN is returned in bridge_vlan_info along with flags */
101 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
102}
103
25static inline size_t br_port_info_size(void) 104static inline size_t br_port_info_size(void)
26{ 105{
27 return nla_total_size(1) /* IFLA_BRPORT_STATE */ 106 return nla_total_size(1) /* IFLA_BRPORT_STATE */
@@ -36,7 +115,7 @@ static inline size_t br_port_info_size(void)
36 + 0; 115 + 0;
37} 116}
38 117
39static inline size_t br_nlmsg_size(void) 118static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
40{ 119{
41 return NLMSG_ALIGN(sizeof(struct ifinfomsg)) 120 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
42 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */ 121 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
@@ -45,7 +124,9 @@ static inline size_t br_nlmsg_size(void)
45 + nla_total_size(4) /* IFLA_MTU */ 124 + nla_total_size(4) /* IFLA_MTU */
46 + nla_total_size(4) /* IFLA_LINK */ 125 + nla_total_size(4) /* IFLA_LINK */
47 + nla_total_size(1) /* IFLA_OPERSTATE */ 126 + nla_total_size(1) /* IFLA_OPERSTATE */
48 + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */ 127 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
128 + nla_total_size(br_get_link_af_size_filtered(dev,
129 filter_mask)); /* IFLA_AF_SPEC */
49} 130}
50 131
51static int br_port_fill_attrs(struct sk_buff *skb, 132static int br_port_fill_attrs(struct sk_buff *skb,
@@ -62,7 +143,9 @@ static int br_port_fill_attrs(struct sk_buff *skb,
62 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) || 143 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
63 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) || 144 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
64 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) || 145 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
65 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP))) 146 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
147 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
148 !!(p->flags & BR_PROXYARP_WIFI)))
66 return -EMSGSIZE; 149 return -EMSGSIZE;
67 150
68 return 0; 151 return 0;
@@ -222,8 +305,8 @@ static int br_fill_ifinfo(struct sk_buff *skb,
222 nla_put_u8(skb, IFLA_OPERSTATE, operstate) || 305 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
223 (dev->addr_len && 306 (dev->addr_len &&
224 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 307 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
225 (dev->ifindex != dev->iflink && 308 (dev->ifindex != dev_get_iflink(dev) &&
226 nla_put_u32(skb, IFLA_LINK, dev->iflink))) 309 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
227 goto nla_put_failure; 310 goto nla_put_failure;
228 311
229 if (event == RTM_NEWLINK && port) { 312 if (event == RTM_NEWLINK && port) {
@@ -280,6 +363,7 @@ void br_ifinfo_notify(int event, struct net_bridge_port *port)
280 struct net *net; 363 struct net *net;
281 struct sk_buff *skb; 364 struct sk_buff *skb;
282 int err = -ENOBUFS; 365 int err = -ENOBUFS;
366 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
283 367
284 if (!port) 368 if (!port)
285 return; 369 return;
@@ -288,11 +372,11 @@ void br_ifinfo_notify(int event, struct net_bridge_port *port)
288 br_debug(port->br, "port %u(%s) event %d\n", 372 br_debug(port->br, "port %u(%s) event %d\n",
289 (unsigned int)port->port_no, port->dev->name, event); 373 (unsigned int)port->port_no, port->dev->name, event);
290 374
291 skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC); 375 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
292 if (skb == NULL) 376 if (skb == NULL)
293 goto errout; 377 goto errout;
294 378
295 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, 0, port->dev); 379 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
296 if (err < 0) { 380 if (err < 0) {
297 /* -EMSGSIZE implies BUG in br_nlmsg_size() */ 381 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
298 WARN_ON(err == -EMSGSIZE); 382 WARN_ON(err == -EMSGSIZE);
@@ -471,6 +555,7 @@ static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
471 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING); 555 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
472 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD); 556 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
473 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP); 557 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
558 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
474 559
475 if (tb[IFLA_BRPORT_COST]) { 560 if (tb[IFLA_BRPORT_COST]) {
476 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST])); 561 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
@@ -648,6 +733,9 @@ static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
648 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 }, 733 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
649 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 }, 734 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
650 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 }, 735 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
736 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
737 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
738 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
651}; 739};
652 740
653static int br_changelink(struct net_device *brdev, struct nlattr *tb[], 741static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
@@ -677,6 +765,24 @@ static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
677 return err; 765 return err;
678 } 766 }
679 767
768 if (data[IFLA_BR_AGEING_TIME]) {
769 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
770
771 br->ageing_time = clock_t_to_jiffies(ageing_time);
772 }
773
774 if (data[IFLA_BR_STP_STATE]) {
775 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
776
777 br_stp_set_enabled(br, stp_enabled);
778 }
779
780 if (data[IFLA_BR_PRIORITY]) {
781 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
782
783 br_stp_set_bridge_priority(br, priority);
784 }
785
680 return 0; 786 return 0;
681} 787}
682 788
@@ -685,6 +791,9 @@ static size_t br_get_size(const struct net_device *brdev)
685 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */ 791 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
686 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */ 792 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
687 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */ 793 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
794 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
795 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
796 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
688 0; 797 0;
689} 798}
690 799
@@ -694,10 +803,16 @@ static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
694 u32 forward_delay = jiffies_to_clock_t(br->forward_delay); 803 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
695 u32 hello_time = jiffies_to_clock_t(br->hello_time); 804 u32 hello_time = jiffies_to_clock_t(br->hello_time);
696 u32 age_time = jiffies_to_clock_t(br->max_age); 805 u32 age_time = jiffies_to_clock_t(br->max_age);
806 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
807 u32 stp_enabled = br->stp_enabled;
808 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
697 809
698 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) || 810 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
699 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) || 811 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
700 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time)) 812 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
813 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
814 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
815 nla_put_u16(skb, IFLA_BR_PRIORITY, priority))
701 return -EMSGSIZE; 816 return -EMSGSIZE;
702 817
703 return 0; 818 return 0;
diff --git a/net/bridge/br_nf_core.c b/net/bridge/br_nf_core.c
index 387cb3bd017c..20cbb727df4d 100644
--- a/net/bridge/br_nf_core.c
+++ b/net/bridge/br_nf_core.c
@@ -54,7 +54,6 @@ static unsigned int fake_mtu(const struct dst_entry *dst)
54 54
55static struct dst_ops fake_dst_ops = { 55static struct dst_ops fake_dst_ops = {
56 .family = AF_INET, 56 .family = AF_INET,
57 .protocol = cpu_to_be16(ETH_P_IP),
58 .update_pmtu = fake_update_pmtu, 57 .update_pmtu = fake_update_pmtu,
59 .redirect = fake_redirect, 58 .redirect = fake_redirect,
60 .cow_metrics = fake_cow_metrics, 59 .cow_metrics = fake_cow_metrics,
diff --git a/net/bridge/br_private.h b/net/bridge/br_private.h
index de0919975a25..6ca0251cb478 100644
--- a/net/bridge/br_private.h
+++ b/net/bridge/br_private.h
@@ -305,6 +305,7 @@ struct br_input_skb_cb {
305#endif 305#endif
306 306
307 u16 frag_max_size; 307 u16 frag_max_size;
308 bool proxyarp_replied;
308 309
309#ifdef CONFIG_BRIDGE_VLAN_FILTERING 310#ifdef CONFIG_BRIDGE_VLAN_FILTERING
310 bool vlan_filtered; 311 bool vlan_filtered;
@@ -409,10 +410,10 @@ int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
409 410
410/* br_forward.c */ 411/* br_forward.c */
411void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb); 412void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb);
412int br_dev_queue_push_xmit(struct sk_buff *skb); 413int br_dev_queue_push_xmit(struct sock *sk, struct sk_buff *skb);
413void br_forward(const struct net_bridge_port *to, 414void br_forward(const struct net_bridge_port *to,
414 struct sk_buff *skb, struct sk_buff *skb0); 415 struct sk_buff *skb, struct sk_buff *skb0);
415int br_forward_finish(struct sk_buff *skb); 416int br_forward_finish(struct sock *sk, struct sk_buff *skb);
416void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast); 417void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast);
417void br_flood_forward(struct net_bridge *br, struct sk_buff *skb, 418void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
418 struct sk_buff *skb2, bool unicast); 419 struct sk_buff *skb2, bool unicast);
@@ -430,7 +431,7 @@ void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
430void br_manage_promisc(struct net_bridge *br); 431void br_manage_promisc(struct net_bridge *br);
431 432
432/* br_input.c */ 433/* br_input.c */
433int br_handle_frame_finish(struct sk_buff *skb); 434int br_handle_frame_finish(struct sock *sk, struct sk_buff *skb);
434rx_handler_result_t br_handle_frame(struct sk_buff **pskb); 435rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
435 436
436static inline bool br_rx_handler_check_rcu(const struct net_device *dev) 437static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
@@ -762,6 +763,11 @@ static inline int br_vlan_enabled(struct net_bridge *br)
762} 763}
763#endif 764#endif
764 765
766struct nf_br_ops {
767 int (*br_dev_xmit_hook)(struct sk_buff *skb);
768};
769extern const struct nf_br_ops __rcu *nf_br_ops;
770
765/* br_netfilter.c */ 771/* br_netfilter.c */
766#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 772#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
767int br_nf_core_init(void); 773int br_nf_core_init(void);
diff --git a/net/bridge/br_stp_bpdu.c b/net/bridge/br_stp_bpdu.c
index bdb459d21ad8..534fc4cd263e 100644
--- a/net/bridge/br_stp_bpdu.c
+++ b/net/bridge/br_stp_bpdu.c
@@ -54,8 +54,9 @@ static void br_send_bpdu(struct net_bridge_port *p,
54 54
55 skb_reset_mac_header(skb); 55 skb_reset_mac_header(skb);
56 56
57 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, 57 NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, NULL, skb,
58 dev_queue_xmit); 58 NULL, skb->dev,
59 dev_queue_xmit_sk);
59} 60}
60 61
61static inline void br_set_ticks(unsigned char *dest, int j) 62static inline void br_set_ticks(unsigned char *dest, int j)
diff --git a/net/bridge/br_sysfs_if.c b/net/bridge/br_sysfs_if.c
index 2de5d91199e8..4905845a94e9 100644
--- a/net/bridge/br_sysfs_if.c
+++ b/net/bridge/br_sysfs_if.c
@@ -171,6 +171,7 @@ BRPORT_ATTR_FLAG(root_block, BR_ROOT_BLOCK);
171BRPORT_ATTR_FLAG(learning, BR_LEARNING); 171BRPORT_ATTR_FLAG(learning, BR_LEARNING);
172BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD); 172BRPORT_ATTR_FLAG(unicast_flood, BR_FLOOD);
173BRPORT_ATTR_FLAG(proxyarp, BR_PROXYARP); 173BRPORT_ATTR_FLAG(proxyarp, BR_PROXYARP);
174BRPORT_ATTR_FLAG(proxyarp_wifi, BR_PROXYARP_WIFI);
174 175
175#ifdef CONFIG_BRIDGE_IGMP_SNOOPING 176#ifdef CONFIG_BRIDGE_IGMP_SNOOPING
176static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf) 177static ssize_t show_multicast_router(struct net_bridge_port *p, char *buf)
@@ -215,6 +216,7 @@ static const struct brport_attribute *brport_attrs[] = {
215 &brport_attr_multicast_fast_leave, 216 &brport_attr_multicast_fast_leave,
216#endif 217#endif
217 &brport_attr_proxyarp, 218 &brport_attr_proxyarp,
219 &brport_attr_proxyarp_wifi,
218 NULL 220 NULL
219}; 221};
220 222
diff --git a/net/bridge/netfilter/ebtable_filter.c b/net/bridge/netfilter/ebtable_filter.c
index ce205aabf9c5..8a3f63b2e807 100644
--- a/net/bridge/netfilter/ebtable_filter.c
+++ b/net/bridge/netfilter/ebtable_filter.c
@@ -58,20 +58,18 @@ static const struct ebt_table frame_filter = {
58 58
59static unsigned int 59static unsigned int
60ebt_in_hook(const struct nf_hook_ops *ops, struct sk_buff *skb, 60ebt_in_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
61 const struct net_device *in, const struct net_device *out, 61 const struct nf_hook_state *state)
62 int (*okfn)(struct sk_buff *))
63{ 62{
64 return ebt_do_table(ops->hooknum, skb, in, out, 63 return ebt_do_table(ops->hooknum, skb, state->in, state->out,
65 dev_net(in)->xt.frame_filter); 64 dev_net(state->in)->xt.frame_filter);
66} 65}
67 66
68static unsigned int 67static unsigned int
69ebt_out_hook(const struct nf_hook_ops *ops, struct sk_buff *skb, 68ebt_out_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
70 const struct net_device *in, const struct net_device *out, 69 const struct nf_hook_state *state)
71 int (*okfn)(struct sk_buff *))
72{ 70{
73 return ebt_do_table(ops->hooknum, skb, in, out, 71 return ebt_do_table(ops->hooknum, skb, state->in, state->out,
74 dev_net(out)->xt.frame_filter); 72 dev_net(state->out)->xt.frame_filter);
75} 73}
76 74
77static struct nf_hook_ops ebt_ops_filter[] __read_mostly = { 75static 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 a0ac2984fb6c..c5ef5b1ab678 100644
--- a/net/bridge/netfilter/ebtable_nat.c
+++ b/net/bridge/netfilter/ebtable_nat.c
@@ -58,20 +58,18 @@ static struct ebt_table frame_nat = {
58 58
59static unsigned int 59static unsigned int
60ebt_nat_in(const struct nf_hook_ops *ops, struct sk_buff *skb, 60ebt_nat_in(const struct nf_hook_ops *ops, struct sk_buff *skb,
61 const struct net_device *in, const struct net_device *out, 61 const struct nf_hook_state *state)
62 int (*okfn)(struct sk_buff *))
63{ 62{
64 return ebt_do_table(ops->hooknum, skb, in, out, 63 return ebt_do_table(ops->hooknum, skb, state->in, state->out,
65 dev_net(in)->xt.frame_nat); 64 dev_net(state->in)->xt.frame_nat);
66} 65}
67 66
68static unsigned int 67static unsigned int
69ebt_nat_out(const struct nf_hook_ops *ops, struct sk_buff *skb, 68ebt_nat_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
70 const struct net_device *in, const struct net_device *out, 69 const struct nf_hook_state *state)
71 int (*okfn)(struct sk_buff *))
72{ 70{
73 return ebt_do_table(ops->hooknum, skb, in, out, 71 return ebt_do_table(ops->hooknum, skb, state->in, state->out,
74 dev_net(out)->xt.frame_nat); 72 dev_net(state->out)->xt.frame_nat);
75} 73}
76 74
77static struct nf_hook_ops ebt_ops_nat[] __read_mostly = { 75static 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
index 19473a9371b8..a343e62442b1 100644
--- a/net/bridge/netfilter/nf_tables_bridge.c
+++ b/net/bridge/netfilter/nf_tables_bridge.c
@@ -67,47 +67,43 @@ EXPORT_SYMBOL_GPL(nft_bridge_ip6hdr_validate);
67static inline void nft_bridge_set_pktinfo_ipv4(struct nft_pktinfo *pkt, 67static inline void nft_bridge_set_pktinfo_ipv4(struct nft_pktinfo *pkt,
68 const struct nf_hook_ops *ops, 68 const struct nf_hook_ops *ops,
69 struct sk_buff *skb, 69 struct sk_buff *skb,
70 const struct net_device *in, 70 const struct nf_hook_state *state)
71 const struct net_device *out)
72{ 71{
73 if (nft_bridge_iphdr_validate(skb)) 72 if (nft_bridge_iphdr_validate(skb))
74 nft_set_pktinfo_ipv4(pkt, ops, skb, in, out); 73 nft_set_pktinfo_ipv4(pkt, ops, skb, state);
75 else 74 else
76 nft_set_pktinfo(pkt, ops, skb, in, out); 75 nft_set_pktinfo(pkt, ops, skb, state);
77} 76}
78 77
79static inline void nft_bridge_set_pktinfo_ipv6(struct nft_pktinfo *pkt, 78static inline void nft_bridge_set_pktinfo_ipv6(struct nft_pktinfo *pkt,
80 const struct nf_hook_ops *ops, 79 const struct nf_hook_ops *ops,
81 struct sk_buff *skb, 80 struct sk_buff *skb,
82 const struct net_device *in, 81 const struct nf_hook_state *state)
83 const struct net_device *out)
84{ 82{
85#if IS_ENABLED(CONFIG_IPV6) 83#if IS_ENABLED(CONFIG_IPV6)
86 if (nft_bridge_ip6hdr_validate(skb) && 84 if (nft_bridge_ip6hdr_validate(skb) &&
87 nft_set_pktinfo_ipv6(pkt, ops, skb, in, out) == 0) 85 nft_set_pktinfo_ipv6(pkt, ops, skb, state) == 0)
88 return; 86 return;
89#endif 87#endif
90 nft_set_pktinfo(pkt, ops, skb, in, out); 88 nft_set_pktinfo(pkt, ops, skb, state);
91} 89}
92 90
93static unsigned int 91static unsigned int
94nft_do_chain_bridge(const struct nf_hook_ops *ops, 92nft_do_chain_bridge(const struct nf_hook_ops *ops,
95 struct sk_buff *skb, 93 struct sk_buff *skb,
96 const struct net_device *in, 94 const struct nf_hook_state *state)
97 const struct net_device *out,
98 int (*okfn)(struct sk_buff *))
99{ 95{
100 struct nft_pktinfo pkt; 96 struct nft_pktinfo pkt;
101 97
102 switch (eth_hdr(skb)->h_proto) { 98 switch (eth_hdr(skb)->h_proto) {
103 case htons(ETH_P_IP): 99 case htons(ETH_P_IP):
104 nft_bridge_set_pktinfo_ipv4(&pkt, ops, skb, in, out); 100 nft_bridge_set_pktinfo_ipv4(&pkt, ops, skb, state);
105 break; 101 break;
106 case htons(ETH_P_IPV6): 102 case htons(ETH_P_IPV6):
107 nft_bridge_set_pktinfo_ipv6(&pkt, ops, skb, in, out); 103 nft_bridge_set_pktinfo_ipv6(&pkt, ops, skb, state);
108 break; 104 break;
109 default: 105 default:
110 nft_set_pktinfo(&pkt, ops, skb, in, out); 106 nft_set_pktinfo(&pkt, ops, skb, state);
111 break; 107 break;
112 } 108 }
113 109
diff --git a/net/bridge/netfilter/nft_meta_bridge.c b/net/bridge/netfilter/nft_meta_bridge.c
index 4f02109d708f..a21269b83f16 100644
--- a/net/bridge/netfilter/nft_meta_bridge.c
+++ b/net/bridge/netfilter/nft_meta_bridge.c
@@ -19,12 +19,12 @@
19#include "../br_private.h" 19#include "../br_private.h"
20 20
21static void nft_meta_bridge_get_eval(const struct nft_expr *expr, 21static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
22 struct nft_data data[NFT_REG_MAX + 1], 22 struct nft_regs *regs,
23 const struct nft_pktinfo *pkt) 23 const struct nft_pktinfo *pkt)
24{ 24{
25 const struct nft_meta *priv = nft_expr_priv(expr); 25 const struct nft_meta *priv = nft_expr_priv(expr);
26 const struct net_device *in = pkt->in, *out = pkt->out; 26 const struct net_device *in = pkt->in, *out = pkt->out;
27 struct nft_data *dest = &data[priv->dreg]; 27 u32 *dest = &regs->data[priv->dreg];
28 const struct net_bridge_port *p; 28 const struct net_bridge_port *p;
29 29
30 switch (priv->key) { 30 switch (priv->key) {
@@ -40,12 +40,12 @@ static void nft_meta_bridge_get_eval(const struct nft_expr *expr,
40 goto out; 40 goto out;
41 } 41 }
42 42
43 strncpy((char *)dest->data, p->br->dev->name, sizeof(dest->data)); 43 strncpy((char *)dest, p->br->dev->name, IFNAMSIZ);
44 return; 44 return;
45out: 45out:
46 return nft_meta_get_eval(expr, data, pkt); 46 return nft_meta_get_eval(expr, regs, pkt);
47err: 47err:
48 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 48 regs->verdict.code = NFT_BREAK;
49} 49}
50 50
51static int nft_meta_bridge_get_init(const struct nft_ctx *ctx, 51static int nft_meta_bridge_get_init(const struct nft_ctx *ctx,
@@ -53,27 +53,21 @@ static int nft_meta_bridge_get_init(const struct nft_ctx *ctx,
53 const struct nlattr * const tb[]) 53 const struct nlattr * const tb[])
54{ 54{
55 struct nft_meta *priv = nft_expr_priv(expr); 55 struct nft_meta *priv = nft_expr_priv(expr);
56 int err; 56 unsigned int len;
57 57
58 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); 58 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
59 switch (priv->key) { 59 switch (priv->key) {
60 case NFT_META_BRI_IIFNAME: 60 case NFT_META_BRI_IIFNAME:
61 case NFT_META_BRI_OIFNAME: 61 case NFT_META_BRI_OIFNAME:
62 len = IFNAMSIZ;
62 break; 63 break;
63 default: 64 default:
64 return nft_meta_get_init(ctx, expr, tb); 65 return nft_meta_get_init(ctx, expr, tb);
65 } 66 }
66 67
67 priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG])); 68 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
68 err = nft_validate_output_register(priv->dreg); 69 return nft_validate_register_store(ctx, priv->dreg, NULL,
69 if (err < 0) 70 NFT_DATA_VALUE, len);
70 return err;
71
72 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
73 if (err < 0)
74 return err;
75
76 return 0;
77} 71}
78 72
79static struct nft_expr_type nft_meta_bridge_type; 73static struct nft_expr_type nft_meta_bridge_type;
diff --git a/net/bridge/netfilter/nft_reject_bridge.c b/net/bridge/netfilter/nft_reject_bridge.c
index 3244aead0926..858d848564ee 100644
--- a/net/bridge/netfilter/nft_reject_bridge.c
+++ b/net/bridge/netfilter/nft_reject_bridge.c
@@ -21,6 +21,7 @@
21#include <net/ip.h> 21#include <net/ip.h>
22#include <net/ip6_checksum.h> 22#include <net/ip6_checksum.h>
23#include <linux/netfilter_bridge.h> 23#include <linux/netfilter_bridge.h>
24#include <linux/netfilter_ipv6.h>
24#include "../br_private.h" 25#include "../br_private.h"
25 26
26static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb, 27static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
@@ -36,7 +37,12 @@ static void nft_reject_br_push_etherhdr(struct sk_buff *oldskb,
36 skb_pull(nskb, ETH_HLEN); 37 skb_pull(nskb, ETH_HLEN);
37} 38}
38 39
39static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook) 40/* We cannot use oldskb->dev, it can be either bridge device (NF_BRIDGE INPUT)
41 * or the bridge port (NF_BRIDGE PREROUTING).
42 */
43static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb,
44 const struct net_device *dev,
45 int hook)
40{ 46{
41 struct sk_buff *nskb; 47 struct sk_buff *nskb;
42 struct iphdr *niph; 48 struct iphdr *niph;
@@ -65,11 +71,12 @@ static void nft_reject_br_send_v4_tcp_reset(struct sk_buff *oldskb, int hook)
65 71
66 nft_reject_br_push_etherhdr(oldskb, nskb); 72 nft_reject_br_push_etherhdr(oldskb, nskb);
67 73
68 br_deliver(br_port_get_rcu(oldskb->dev), nskb); 74 br_deliver(br_port_get_rcu(dev), nskb);
69} 75}
70 76
71static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook, 77static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb,
72 u8 code) 78 const struct net_device *dev,
79 int hook, u8 code)
73{ 80{
74 struct sk_buff *nskb; 81 struct sk_buff *nskb;
75 struct iphdr *niph; 82 struct iphdr *niph;
@@ -77,8 +84,9 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
77 unsigned int len; 84 unsigned int len;
78 void *payload; 85 void *payload;
79 __wsum csum; 86 __wsum csum;
87 u8 proto;
80 88
81 if (!nft_bridge_iphdr_validate(oldskb)) 89 if (oldskb->csum_bad || !nft_bridge_iphdr_validate(oldskb))
82 return; 90 return;
83 91
84 /* IP header checks: fragment. */ 92 /* IP header checks: fragment. */
@@ -91,7 +99,17 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
91 if (!pskb_may_pull(oldskb, len)) 99 if (!pskb_may_pull(oldskb, len))
92 return; 100 return;
93 101
94 if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), 0)) 102 if (pskb_trim_rcsum(oldskb, ntohs(ip_hdr(oldskb)->tot_len)))
103 return;
104
105 if (ip_hdr(oldskb)->protocol == IPPROTO_TCP ||
106 ip_hdr(oldskb)->protocol == IPPROTO_UDP)
107 proto = ip_hdr(oldskb)->protocol;
108 else
109 proto = 0;
110
111 if (!skb_csum_unnecessary(oldskb) &&
112 nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), proto))
95 return; 113 return;
96 114
97 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) + 115 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmphdr) +
@@ -120,11 +138,13 @@ static void nft_reject_br_send_v4_unreach(struct sk_buff *oldskb, int hook,
120 138
121 nft_reject_br_push_etherhdr(oldskb, nskb); 139 nft_reject_br_push_etherhdr(oldskb, nskb);
122 140
123 br_deliver(br_port_get_rcu(oldskb->dev), nskb); 141 br_deliver(br_port_get_rcu(dev), nskb);
124} 142}
125 143
126static void nft_reject_br_send_v6_tcp_reset(struct net *net, 144static void nft_reject_br_send_v6_tcp_reset(struct net *net,
127 struct sk_buff *oldskb, int hook) 145 struct sk_buff *oldskb,
146 const struct net_device *dev,
147 int hook)
128{ 148{
129 struct sk_buff *nskb; 149 struct sk_buff *nskb;
130 const struct tcphdr *oth; 150 const struct tcphdr *oth;
@@ -152,12 +172,37 @@ static void nft_reject_br_send_v6_tcp_reset(struct net *net,
152 172
153 nft_reject_br_push_etherhdr(oldskb, nskb); 173 nft_reject_br_push_etherhdr(oldskb, nskb);
154 174
155 br_deliver(br_port_get_rcu(oldskb->dev), nskb); 175 br_deliver(br_port_get_rcu(dev), nskb);
176}
177
178static bool reject6_br_csum_ok(struct sk_buff *skb, int hook)
179{
180 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
181 int thoff;
182 __be16 fo;
183 u8 proto = ip6h->nexthdr;
184
185 if (skb->csum_bad)
186 return false;
187
188 if (skb_csum_unnecessary(skb))
189 return true;
190
191 if (ip6h->payload_len &&
192 pskb_trim_rcsum(skb, ntohs(ip6h->payload_len) + sizeof(*ip6h)))
193 return false;
194
195 thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
196 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
197 return false;
198
199 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
156} 200}
157 201
158static void nft_reject_br_send_v6_unreach(struct net *net, 202static void nft_reject_br_send_v6_unreach(struct net *net,
159 struct sk_buff *oldskb, int hook, 203 struct sk_buff *oldskb,
160 u8 code) 204 const struct net_device *dev,
205 int hook, u8 code)
161{ 206{
162 struct sk_buff *nskb; 207 struct sk_buff *nskb;
163 struct ipv6hdr *nip6h; 208 struct ipv6hdr *nip6h;
@@ -176,6 +221,9 @@ static void nft_reject_br_send_v6_unreach(struct net *net,
176 if (!pskb_may_pull(oldskb, len)) 221 if (!pskb_may_pull(oldskb, len))
177 return; 222 return;
178 223
224 if (!reject6_br_csum_ok(oldskb, hook))
225 return;
226
179 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) + 227 nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct icmp6hdr) +
180 LL_MAX_HEADER + len, GFP_ATOMIC); 228 LL_MAX_HEADER + len, GFP_ATOMIC);
181 if (!nskb) 229 if (!nskb)
@@ -205,12 +253,12 @@ static void nft_reject_br_send_v6_unreach(struct net *net,
205 253
206 nft_reject_br_push_etherhdr(oldskb, nskb); 254 nft_reject_br_push_etherhdr(oldskb, nskb);
207 255
208 br_deliver(br_port_get_rcu(oldskb->dev), nskb); 256 br_deliver(br_port_get_rcu(dev), nskb);
209} 257}
210 258
211static void nft_reject_bridge_eval(const struct nft_expr *expr, 259static void nft_reject_bridge_eval(const struct nft_expr *expr,
212 struct nft_data data[NFT_REG_MAX + 1], 260 struct nft_regs *regs,
213 const struct nft_pktinfo *pkt) 261 const struct nft_pktinfo *pkt)
214{ 262{
215 struct nft_reject *priv = nft_expr_priv(expr); 263 struct nft_reject *priv = nft_expr_priv(expr);
216 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out); 264 struct net *net = dev_net((pkt->in != NULL) ? pkt->in : pkt->out);
@@ -224,16 +272,16 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr,
224 case htons(ETH_P_IP): 272 case htons(ETH_P_IP):
225 switch (priv->type) { 273 switch (priv->type) {
226 case NFT_REJECT_ICMP_UNREACH: 274 case NFT_REJECT_ICMP_UNREACH:
227 nft_reject_br_send_v4_unreach(pkt->skb, 275 nft_reject_br_send_v4_unreach(pkt->skb, pkt->in,
228 pkt->ops->hooknum, 276 pkt->ops->hooknum,
229 priv->icmp_code); 277 priv->icmp_code);
230 break; 278 break;
231 case NFT_REJECT_TCP_RST: 279 case NFT_REJECT_TCP_RST:
232 nft_reject_br_send_v4_tcp_reset(pkt->skb, 280 nft_reject_br_send_v4_tcp_reset(pkt->skb, pkt->in,
233 pkt->ops->hooknum); 281 pkt->ops->hooknum);
234 break; 282 break;
235 case NFT_REJECT_ICMPX_UNREACH: 283 case NFT_REJECT_ICMPX_UNREACH:
236 nft_reject_br_send_v4_unreach(pkt->skb, 284 nft_reject_br_send_v4_unreach(pkt->skb, pkt->in,
237 pkt->ops->hooknum, 285 pkt->ops->hooknum,
238 nft_reject_icmp_code(priv->icmp_code)); 286 nft_reject_icmp_code(priv->icmp_code));
239 break; 287 break;
@@ -242,16 +290,16 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr,
242 case htons(ETH_P_IPV6): 290 case htons(ETH_P_IPV6):
243 switch (priv->type) { 291 switch (priv->type) {
244 case NFT_REJECT_ICMP_UNREACH: 292 case NFT_REJECT_ICMP_UNREACH:
245 nft_reject_br_send_v6_unreach(net, pkt->skb, 293 nft_reject_br_send_v6_unreach(net, pkt->skb, pkt->in,
246 pkt->ops->hooknum, 294 pkt->ops->hooknum,
247 priv->icmp_code); 295 priv->icmp_code);
248 break; 296 break;
249 case NFT_REJECT_TCP_RST: 297 case NFT_REJECT_TCP_RST:
250 nft_reject_br_send_v6_tcp_reset(net, pkt->skb, 298 nft_reject_br_send_v6_tcp_reset(net, pkt->skb, pkt->in,
251 pkt->ops->hooknum); 299 pkt->ops->hooknum);
252 break; 300 break;
253 case NFT_REJECT_ICMPX_UNREACH: 301 case NFT_REJECT_ICMPX_UNREACH:
254 nft_reject_br_send_v6_unreach(net, pkt->skb, 302 nft_reject_br_send_v6_unreach(net, pkt->skb, pkt->in,
255 pkt->ops->hooknum, 303 pkt->ops->hooknum,
256 nft_reject_icmpv6_code(priv->icmp_code)); 304 nft_reject_icmpv6_code(priv->icmp_code));
257 break; 305 break;
@@ -262,7 +310,7 @@ static void nft_reject_bridge_eval(const struct nft_expr *expr,
262 break; 310 break;
263 } 311 }
264out: 312out:
265 data[NFT_REG_VERDICT].verdict = NF_DROP; 313 regs->verdict.code = NF_DROP;
266} 314}
267 315
268static int nft_reject_bridge_validate(const struct nft_ctx *ctx, 316static int nft_reject_bridge_validate(const struct nft_ctx *ctx,
@@ -323,6 +371,8 @@ static int nft_reject_bridge_dump(struct sk_buff *skb,
323 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code)) 371 if (nla_put_u8(skb, NFTA_REJECT_ICMP_CODE, priv->icmp_code))
324 goto nla_put_failure; 372 goto nla_put_failure;
325 break; 373 break;
374 default:
375 break;
326 } 376 }
327 377
328 return 0; 378 return 0;
diff --git a/net/caif/caif_socket.c b/net/caif/caif_socket.c
index a6e2da0bc718..4ec0c803aef1 100644
--- a/net/caif/caif_socket.c
+++ b/net/caif/caif_socket.c
@@ -271,8 +271,8 @@ static void caif_check_flow_release(struct sock *sk)
271 * Copied from unix_dgram_recvmsg, but removed credit checks, 271 * Copied from unix_dgram_recvmsg, but removed credit checks,
272 * changed locking, address handling and added MSG_TRUNC. 272 * changed locking, address handling and added MSG_TRUNC.
273 */ 273 */
274static int caif_seqpkt_recvmsg(struct kiocb *iocb, struct socket *sock, 274static int caif_seqpkt_recvmsg(struct socket *sock, struct msghdr *m,
275 struct msghdr *m, size_t len, int flags) 275 size_t len, int flags)
276 276
277{ 277{
278 struct sock *sk = sock->sk; 278 struct sock *sk = sock->sk;
@@ -343,9 +343,8 @@ static long caif_stream_data_wait(struct sock *sk, long timeo)
343 * Copied from unix_stream_recvmsg, but removed credit checks, 343 * Copied from unix_stream_recvmsg, but removed credit checks,
344 * changed locking calls, changed address handling. 344 * changed locking calls, changed address handling.
345 */ 345 */
346static int caif_stream_recvmsg(struct kiocb *iocb, struct socket *sock, 346static int caif_stream_recvmsg(struct socket *sock, struct msghdr *msg,
347 struct msghdr *msg, size_t size, 347 size_t size, int flags)
348 int flags)
349{ 348{
350 struct sock *sk = sock->sk; 349 struct sock *sk = sock->sk;
351 int copied = 0; 350 int copied = 0;
@@ -511,8 +510,8 @@ static int transmit_skb(struct sk_buff *skb, struct caifsock *cf_sk,
511} 510}
512 511
513/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */ 512/* Copied from af_unix:unix_dgram_sendmsg, and adapted to CAIF */
514static int caif_seqpkt_sendmsg(struct kiocb *kiocb, struct socket *sock, 513static int caif_seqpkt_sendmsg(struct socket *sock, struct msghdr *msg,
515 struct msghdr *msg, size_t len) 514 size_t len)
516{ 515{
517 struct sock *sk = sock->sk; 516 struct sock *sk = sock->sk;
518 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 517 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
@@ -586,8 +585,8 @@ err:
586 * Changed removed permission handling and added waiting for flow on 585 * Changed removed permission handling and added waiting for flow on
587 * and other minor adaptations. 586 * and other minor adaptations.
588 */ 587 */
589static int caif_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, 588static int caif_stream_sendmsg(struct socket *sock, struct msghdr *msg,
590 struct msghdr *msg, size_t len) 589 size_t len)
591{ 590{
592 struct sock *sk = sock->sk; 591 struct sock *sk = sock->sk;
593 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk); 592 struct caifsock *cf_sk = container_of(sk, struct caifsock, sk);
diff --git a/net/can/bcm.c b/net/can/bcm.c
index ee9ffd956552..b523453585be 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -328,7 +328,7 @@ static void bcm_send_to_user(struct bcm_op *op, struct bcm_msg_head *head,
328 * containing the interface index. 328 * containing the interface index.
329 */ 329 */
330 330
331 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); 331 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
332 addr = (struct sockaddr_can *)skb->cb; 332 addr = (struct sockaddr_can *)skb->cb;
333 memset(addr, 0, sizeof(*addr)); 333 memset(addr, 0, sizeof(*addr));
334 addr->can_family = AF_CAN; 334 addr->can_family = AF_CAN;
@@ -1231,8 +1231,7 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1231/* 1231/*
1232 * bcm_sendmsg - process BCM commands (opcodes) from the userspace 1232 * bcm_sendmsg - process BCM commands (opcodes) from the userspace
1233 */ 1233 */
1234static int bcm_sendmsg(struct kiocb *iocb, struct socket *sock, 1234static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1235 struct msghdr *msg, size_t size)
1236{ 1235{
1237 struct sock *sk = sock->sk; 1236 struct sock *sk = sock->sk;
1238 struct bcm_sock *bo = bcm_sk(sk); 1237 struct bcm_sock *bo = bcm_sk(sk);
@@ -1535,8 +1534,8 @@ static int bcm_connect(struct socket *sock, struct sockaddr *uaddr, int len,
1535 return 0; 1534 return 0;
1536} 1535}
1537 1536
1538static int bcm_recvmsg(struct kiocb *iocb, struct socket *sock, 1537static int bcm_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1539 struct msghdr *msg, size_t size, int flags) 1538 int flags)
1540{ 1539{
1541 struct sock *sk = sock->sk; 1540 struct sock *sk = sock->sk;
1542 struct sk_buff *skb; 1541 struct sk_buff *skb;
diff --git a/net/can/raw.c b/net/can/raw.c
index 00c13ef23661..31b9748cbb4e 100644
--- a/net/can/raw.c
+++ b/net/can/raw.c
@@ -74,6 +74,12 @@ MODULE_ALIAS("can-proto-1");
74 * storing the single filter in dfilter, to avoid using dynamic memory. 74 * storing the single filter in dfilter, to avoid using dynamic memory.
75 */ 75 */
76 76
77struct uniqframe {
78 ktime_t tstamp;
79 const struct sk_buff *skb;
80 unsigned int join_rx_count;
81};
82
77struct raw_sock { 83struct raw_sock {
78 struct sock sk; 84 struct sock sk;
79 int bound; 85 int bound;
@@ -82,10 +88,12 @@ struct raw_sock {
82 int loopback; 88 int loopback;
83 int recv_own_msgs; 89 int recv_own_msgs;
84 int fd_frames; 90 int fd_frames;
91 int join_filters;
85 int count; /* number of active filters */ 92 int count; /* number of active filters */
86 struct can_filter dfilter; /* default/single filter */ 93 struct can_filter dfilter; /* default/single filter */
87 struct can_filter *filter; /* pointer to filter(s) */ 94 struct can_filter *filter; /* pointer to filter(s) */
88 can_err_mask_t err_mask; 95 can_err_mask_t err_mask;
96 struct uniqframe __percpu *uniq;
89}; 97};
90 98
91/* 99/*
@@ -95,8 +103,8 @@ struct raw_sock {
95 */ 103 */
96static inline unsigned int *raw_flags(struct sk_buff *skb) 104static inline unsigned int *raw_flags(struct sk_buff *skb)
97{ 105{
98 BUILD_BUG_ON(sizeof(skb->cb) <= (sizeof(struct sockaddr_can) + 106 sock_skb_cb_check_size(sizeof(struct sockaddr_can) +
99 sizeof(unsigned int))); 107 sizeof(unsigned int));
100 108
101 /* return pointer after struct sockaddr_can */ 109 /* return pointer after struct sockaddr_can */
102 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]); 110 return (unsigned int *)(&((struct sockaddr_can *)skb->cb)[1]);
@@ -123,6 +131,26 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
123 if (!ro->fd_frames && oskb->len != CAN_MTU) 131 if (!ro->fd_frames && oskb->len != CAN_MTU)
124 return; 132 return;
125 133
134 /* eliminate multiple filter matches for the same skb */
135 if (this_cpu_ptr(ro->uniq)->skb == oskb &&
136 ktime_equal(this_cpu_ptr(ro->uniq)->tstamp, oskb->tstamp)) {
137 if (ro->join_filters) {
138 this_cpu_inc(ro->uniq->join_rx_count);
139 /* drop frame until all enabled filters matched */
140 if (this_cpu_ptr(ro->uniq)->join_rx_count < ro->count)
141 return;
142 } else {
143 return;
144 }
145 } else {
146 this_cpu_ptr(ro->uniq)->skb = oskb;
147 this_cpu_ptr(ro->uniq)->tstamp = oskb->tstamp;
148 this_cpu_ptr(ro->uniq)->join_rx_count = 1;
149 /* drop first frame to check all enabled filters? */
150 if (ro->join_filters && ro->count > 1)
151 return;
152 }
153
126 /* clone the given skb to be able to enqueue it into the rcv queue */ 154 /* clone the given skb to be able to enqueue it into the rcv queue */
127 skb = skb_clone(oskb, GFP_ATOMIC); 155 skb = skb_clone(oskb, GFP_ATOMIC);
128 if (!skb) 156 if (!skb)
@@ -135,7 +163,7 @@ static void raw_rcv(struct sk_buff *oskb, void *data)
135 * containing the interface index. 163 * containing the interface index.
136 */ 164 */
137 165
138 BUILD_BUG_ON(sizeof(skb->cb) < sizeof(struct sockaddr_can)); 166 sock_skb_cb_check_size(sizeof(struct sockaddr_can));
139 addr = (struct sockaddr_can *)skb->cb; 167 addr = (struct sockaddr_can *)skb->cb;
140 memset(addr, 0, sizeof(*addr)); 168 memset(addr, 0, sizeof(*addr));
141 addr->can_family = AF_CAN; 169 addr->can_family = AF_CAN;
@@ -296,6 +324,12 @@ static int raw_init(struct sock *sk)
296 ro->loopback = 1; 324 ro->loopback = 1;
297 ro->recv_own_msgs = 0; 325 ro->recv_own_msgs = 0;
298 ro->fd_frames = 0; 326 ro->fd_frames = 0;
327 ro->join_filters = 0;
328
329 /* alloc_percpu provides zero'ed memory */
330 ro->uniq = alloc_percpu(struct uniqframe);
331 if (unlikely(!ro->uniq))
332 return -ENOMEM;
299 333
300 /* set notifier */ 334 /* set notifier */
301 ro->notifier.notifier_call = raw_notifier; 335 ro->notifier.notifier_call = raw_notifier;
@@ -339,6 +373,7 @@ static int raw_release(struct socket *sock)
339 ro->ifindex = 0; 373 ro->ifindex = 0;
340 ro->bound = 0; 374 ro->bound = 0;
341 ro->count = 0; 375 ro->count = 0;
376 free_percpu(ro->uniq);
342 377
343 sock_orphan(sk); 378 sock_orphan(sk);
344 sock->sk = NULL; 379 sock->sk = NULL;
@@ -583,6 +618,15 @@ static int raw_setsockopt(struct socket *sock, int level, int optname,
583 618
584 break; 619 break;
585 620
621 case CAN_RAW_JOIN_FILTERS:
622 if (optlen != sizeof(ro->join_filters))
623 return -EINVAL;
624
625 if (copy_from_user(&ro->join_filters, optval, optlen))
626 return -EFAULT;
627
628 break;
629
586 default: 630 default:
587 return -ENOPROTOOPT; 631 return -ENOPROTOOPT;
588 } 632 }
@@ -647,6 +691,12 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
647 val = &ro->fd_frames; 691 val = &ro->fd_frames;
648 break; 692 break;
649 693
694 case CAN_RAW_JOIN_FILTERS:
695 if (len > sizeof(int))
696 len = sizeof(int);
697 val = &ro->join_filters;
698 break;
699
650 default: 700 default:
651 return -ENOPROTOOPT; 701 return -ENOPROTOOPT;
652 } 702 }
@@ -658,8 +708,7 @@ static int raw_getsockopt(struct socket *sock, int level, int optname,
658 return 0; 708 return 0;
659} 709}
660 710
661static int raw_sendmsg(struct kiocb *iocb, struct socket *sock, 711static int raw_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
662 struct msghdr *msg, size_t size)
663{ 712{
664 struct sock *sk = sock->sk; 713 struct sock *sk = sock->sk;
665 struct raw_sock *ro = raw_sk(sk); 714 struct raw_sock *ro = raw_sk(sk);
@@ -728,8 +777,8 @@ send_failed:
728 return err; 777 return err;
729} 778}
730 779
731static int raw_recvmsg(struct kiocb *iocb, struct socket *sock, 780static int raw_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
732 struct msghdr *msg, size_t size, int flags) 781 int flags)
733{ 782{
734 struct sock *sk = sock->sk; 783 struct sock *sk = sock->sk;
735 struct sk_buff *skb; 784 struct sk_buff *skb;
diff --git a/net/ceph/messenger.c b/net/ceph/messenger.c
index 6b3f54ed65ba..a9f4ae45b7fb 100644
--- a/net/ceph/messenger.c
+++ b/net/ceph/messenger.c
@@ -484,7 +484,7 @@ static int ceph_tcp_connect(struct ceph_connection *con)
484 IPPROTO_TCP, &sock); 484 IPPROTO_TCP, &sock);
485 if (ret) 485 if (ret)
486 return ret; 486 return ret;
487 sock->sk->sk_allocation = GFP_NOFS | __GFP_MEMALLOC; 487 sock->sk->sk_allocation = GFP_NOFS;
488 488
489#ifdef CONFIG_LOCKDEP 489#ifdef CONFIG_LOCKDEP
490 lockdep_set_class(&sock->sk->sk_lock, &socket_class); 490 lockdep_set_class(&sock->sk->sk_lock, &socket_class);
@@ -520,8 +520,6 @@ static int ceph_tcp_connect(struct ceph_connection *con)
520 ret); 520 ret);
521 } 521 }
522 522
523 sk_set_memalloc(sock->sk);
524
525 con->sock = sock; 523 con->sock = sock;
526 return 0; 524 return 0;
527} 525}
@@ -2808,11 +2806,8 @@ static void con_work(struct work_struct *work)
2808{ 2806{
2809 struct ceph_connection *con = container_of(work, struct ceph_connection, 2807 struct ceph_connection *con = container_of(work, struct ceph_connection,
2810 work.work); 2808 work.work);
2811 unsigned long pflags = current->flags;
2812 bool fault; 2809 bool fault;
2813 2810
2814 current->flags |= PF_MEMALLOC;
2815
2816 mutex_lock(&con->mutex); 2811 mutex_lock(&con->mutex);
2817 while (true) { 2812 while (true) {
2818 int ret; 2813 int ret;
@@ -2866,8 +2861,6 @@ static void con_work(struct work_struct *work)
2866 con_fault_finish(con); 2861 con_fault_finish(con);
2867 2862
2868 con->ops->put(con); 2863 con->ops->put(con);
2869
2870 tsk_restore_flags(current, pflags, PF_MEMALLOC);
2871} 2864}
2872 2865
2873/* 2866/*
diff --git a/net/compat.c b/net/compat.c
index f7bd286a8280..5cfd26a0006f 100644
--- a/net/compat.c
+++ b/net/compat.c
@@ -31,10 +31,10 @@
31#include <asm/uaccess.h> 31#include <asm/uaccess.h>
32#include <net/compat.h> 32#include <net/compat.h>
33 33
34ssize_t get_compat_msghdr(struct msghdr *kmsg, 34int get_compat_msghdr(struct msghdr *kmsg,
35 struct compat_msghdr __user *umsg, 35 struct compat_msghdr __user *umsg,
36 struct sockaddr __user **save_addr, 36 struct sockaddr __user **save_addr,
37 struct iovec **iov) 37 struct iovec **iov)
38{ 38{
39 compat_uptr_t uaddr, uiov, tmp3; 39 compat_uptr_t uaddr, uiov, tmp3;
40 compat_size_t nr_segs; 40 compat_size_t nr_segs;
@@ -79,13 +79,11 @@ ssize_t get_compat_msghdr(struct msghdr *kmsg,
79 if (nr_segs > UIO_MAXIOV) 79 if (nr_segs > UIO_MAXIOV)
80 return -EMSGSIZE; 80 return -EMSGSIZE;
81 81
82 err = compat_rw_copy_check_uvector(save_addr ? READ : WRITE, 82 kmsg->msg_iocb = NULL;
83 compat_ptr(uiov), nr_segs, 83
84 UIO_FASTIOV, *iov, iov); 84 return compat_import_iovec(save_addr ? READ : WRITE,
85 if (err >= 0) 85 compat_ptr(uiov), nr_segs,
86 iov_iter_init(&kmsg->msg_iter, save_addr ? READ : WRITE, 86 UIO_FASTIOV, iov, &kmsg->msg_iter);
87 *iov, nr_segs, err);
88 return err;
89} 87}
90 88
91/* Bleech... */ 89/* Bleech... */
@@ -515,25 +513,25 @@ COMPAT_SYSCALL_DEFINE5(getsockopt, int, fd, int, level, int, optname,
515struct compat_group_req { 513struct compat_group_req {
516 __u32 gr_interface; 514 __u32 gr_interface;
517 struct __kernel_sockaddr_storage gr_group 515 struct __kernel_sockaddr_storage gr_group
518 __attribute__ ((aligned(4))); 516 __aligned(4);
519} __packed; 517} __packed;
520 518
521struct compat_group_source_req { 519struct compat_group_source_req {
522 __u32 gsr_interface; 520 __u32 gsr_interface;
523 struct __kernel_sockaddr_storage gsr_group 521 struct __kernel_sockaddr_storage gsr_group
524 __attribute__ ((aligned(4))); 522 __aligned(4);
525 struct __kernel_sockaddr_storage gsr_source 523 struct __kernel_sockaddr_storage gsr_source
526 __attribute__ ((aligned(4))); 524 __aligned(4);
527} __packed; 525} __packed;
528 526
529struct compat_group_filter { 527struct compat_group_filter {
530 __u32 gf_interface; 528 __u32 gf_interface;
531 struct __kernel_sockaddr_storage gf_group 529 struct __kernel_sockaddr_storage gf_group
532 __attribute__ ((aligned(4))); 530 __aligned(4);
533 __u32 gf_fmode; 531 __u32 gf_fmode;
534 __u32 gf_numsrc; 532 __u32 gf_numsrc;
535 struct __kernel_sockaddr_storage gf_slist[1] 533 struct __kernel_sockaddr_storage gf_slist[1]
536 __attribute__ ((aligned(4))); 534 __aligned(4);
537} __packed; 535} __packed;
538 536
539#define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \ 537#define __COMPAT_GF0_SIZE (sizeof(struct compat_group_filter) - \
diff --git a/net/core/datagram.c b/net/core/datagram.c
index df493d68330c..b80fb91bb3f7 100644
--- a/net/core/datagram.c
+++ b/net/core/datagram.c
@@ -673,7 +673,7 @@ int skb_copy_and_csum_datagram_msg(struct sk_buff *skb,
673 if (!chunk) 673 if (!chunk)
674 return 0; 674 return 0;
675 675
676 if (iov_iter_count(&msg->msg_iter) < chunk) { 676 if (msg_data_left(msg) < chunk) {
677 if (__skb_checksum_complete(skb)) 677 if (__skb_checksum_complete(skb))
678 goto csum_error; 678 goto csum_error;
679 if (skb_copy_datagram_msg(skb, hlen, msg, chunk)) 679 if (skb_copy_datagram_msg(skb, hlen, msg, chunk))
diff --git a/net/core/dev.c b/net/core/dev.c
index 45109b70664e..1796cef55ab5 100644
--- a/net/core/dev.c
+++ b/net/core/dev.c
@@ -660,6 +660,27 @@ __setup("netdev=", netdev_boot_setup);
660*******************************************************************************/ 660*******************************************************************************/
661 661
662/** 662/**
663 * dev_get_iflink - get 'iflink' value of a interface
664 * @dev: targeted interface
665 *
666 * Indicates the ifindex the interface is linked to.
667 * Physical interfaces have the same 'ifindex' and 'iflink' values.
668 */
669
670int dev_get_iflink(const struct net_device *dev)
671{
672 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
673 return dev->netdev_ops->ndo_get_iflink(dev);
674
675 /* If dev->rtnl_link_ops is set, it's a virtual interface. */
676 if (dev->rtnl_link_ops)
677 return 0;
678
679 return dev->ifindex;
680}
681EXPORT_SYMBOL(dev_get_iflink);
682
683/**
663 * __dev_get_by_name - find a device by its name 684 * __dev_get_by_name - find a device by its name
664 * @net: the applicable net namespace 685 * @net: the applicable net namespace
665 * @name: name to find 686 * @name: name to find
@@ -1385,7 +1406,7 @@ static int __dev_close(struct net_device *dev)
1385 return retval; 1406 return retval;
1386} 1407}
1387 1408
1388static int dev_close_many(struct list_head *head) 1409int dev_close_many(struct list_head *head, bool unlink)
1389{ 1410{
1390 struct net_device *dev, *tmp; 1411 struct net_device *dev, *tmp;
1391 1412
@@ -1399,11 +1420,13 @@ static int dev_close_many(struct list_head *head)
1399 list_for_each_entry_safe(dev, tmp, head, close_list) { 1420 list_for_each_entry_safe(dev, tmp, head, close_list) {
1400 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); 1421 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1401 call_netdevice_notifiers(NETDEV_DOWN, dev); 1422 call_netdevice_notifiers(NETDEV_DOWN, dev);
1402 list_del_init(&dev->close_list); 1423 if (unlink)
1424 list_del_init(&dev->close_list);
1403 } 1425 }
1404 1426
1405 return 0; 1427 return 0;
1406} 1428}
1429EXPORT_SYMBOL(dev_close_many);
1407 1430
1408/** 1431/**
1409 * dev_close - shutdown an interface. 1432 * dev_close - shutdown an interface.
@@ -1420,7 +1443,7 @@ int dev_close(struct net_device *dev)
1420 LIST_HEAD(single); 1443 LIST_HEAD(single);
1421 1444
1422 list_add(&dev->close_list, &single); 1445 list_add(&dev->close_list, &single);
1423 dev_close_many(&single); 1446 dev_close_many(&single, true);
1424 list_del(&single); 1447 list_del(&single);
1425 } 1448 }
1426 return 0; 1449 return 0;
@@ -1607,6 +1630,22 @@ int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1607} 1630}
1608EXPORT_SYMBOL(call_netdevice_notifiers); 1631EXPORT_SYMBOL(call_netdevice_notifiers);
1609 1632
1633#ifdef CONFIG_NET_CLS_ACT
1634static struct static_key ingress_needed __read_mostly;
1635
1636void net_inc_ingress_queue(void)
1637{
1638 static_key_slow_inc(&ingress_needed);
1639}
1640EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1641
1642void net_dec_ingress_queue(void)
1643{
1644 static_key_slow_dec(&ingress_needed);
1645}
1646EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1647#endif
1648
1610static struct static_key netstamp_needed __read_mostly; 1649static struct static_key netstamp_needed __read_mostly;
1611#ifdef HAVE_JUMP_LABEL 1650#ifdef HAVE_JUMP_LABEL
1612/* We are not allowed to call static_key_slow_dec() from irq context 1651/* We are not allowed to call static_key_slow_dec() from irq context
@@ -1694,6 +1733,7 @@ int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1694 } 1733 }
1695 1734
1696 skb_scrub_packet(skb, true); 1735 skb_scrub_packet(skb, true);
1736 skb->priority = 0;
1697 skb->protocol = eth_type_trans(skb, dev); 1737 skb->protocol = eth_type_trans(skb, dev);
1698 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); 1738 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1699 1739
@@ -1737,7 +1777,8 @@ static inline int deliver_skb(struct sk_buff *skb,
1737 1777
1738static inline void deliver_ptype_list_skb(struct sk_buff *skb, 1778static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1739 struct packet_type **pt, 1779 struct packet_type **pt,
1740 struct net_device *dev, __be16 type, 1780 struct net_device *orig_dev,
1781 __be16 type,
1741 struct list_head *ptype_list) 1782 struct list_head *ptype_list)
1742{ 1783{
1743 struct packet_type *ptype, *pt_prev = *pt; 1784 struct packet_type *ptype, *pt_prev = *pt;
@@ -1746,7 +1787,7 @@ static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1746 if (ptype->type != type) 1787 if (ptype->type != type)
1747 continue; 1788 continue;
1748 if (pt_prev) 1789 if (pt_prev)
1749 deliver_skb(skb, pt_prev, dev); 1790 deliver_skb(skb, pt_prev, orig_dev);
1750 pt_prev = ptype; 1791 pt_prev = ptype;
1751 } 1792 }
1752 *pt = pt_prev; 1793 *pt = pt_prev;
@@ -2559,12 +2600,26 @@ static netdev_features_t harmonize_features(struct sk_buff *skb,
2559 return features; 2600 return features;
2560} 2601}
2561 2602
2603netdev_features_t passthru_features_check(struct sk_buff *skb,
2604 struct net_device *dev,
2605 netdev_features_t features)
2606{
2607 return features;
2608}
2609EXPORT_SYMBOL(passthru_features_check);
2610
2611static netdev_features_t dflt_features_check(const struct sk_buff *skb,
2612 struct net_device *dev,
2613 netdev_features_t features)
2614{
2615 return vlan_features_check(skb, features);
2616}
2617
2562netdev_features_t netif_skb_features(struct sk_buff *skb) 2618netdev_features_t netif_skb_features(struct sk_buff *skb)
2563{ 2619{
2564 struct net_device *dev = skb->dev; 2620 struct net_device *dev = skb->dev;
2565 netdev_features_t features = dev->features; 2621 netdev_features_t features = dev->features;
2566 u16 gso_segs = skb_shinfo(skb)->gso_segs; 2622 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2567 __be16 protocol = skb->protocol;
2568 2623
2569 if (gso_segs > dev->gso_max_segs || gso_segs < dev->gso_min_segs) 2624 if (gso_segs > dev->gso_max_segs || gso_segs < dev->gso_min_segs)
2570 features &= ~NETIF_F_GSO_MASK; 2625 features &= ~NETIF_F_GSO_MASK;
@@ -2576,34 +2631,17 @@ netdev_features_t netif_skb_features(struct sk_buff *skb)
2576 if (skb->encapsulation) 2631 if (skb->encapsulation)
2577 features &= dev->hw_enc_features; 2632 features &= dev->hw_enc_features;
2578 2633
2579 if (!skb_vlan_tag_present(skb)) { 2634 if (skb_vlan_tagged(skb))
2580 if (unlikely(protocol == htons(ETH_P_8021Q) ||
2581 protocol == htons(ETH_P_8021AD))) {
2582 struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data;
2583 protocol = veh->h_vlan_encapsulated_proto;
2584 } else {
2585 goto finalize;
2586 }
2587 }
2588
2589 features = netdev_intersect_features(features,
2590 dev->vlan_features |
2591 NETIF_F_HW_VLAN_CTAG_TX |
2592 NETIF_F_HW_VLAN_STAG_TX);
2593
2594 if (protocol == htons(ETH_P_8021Q) || protocol == htons(ETH_P_8021AD))
2595 features = netdev_intersect_features(features, 2635 features = netdev_intersect_features(features,
2596 NETIF_F_SG | 2636 dev->vlan_features |
2597 NETIF_F_HIGHDMA |
2598 NETIF_F_FRAGLIST |
2599 NETIF_F_GEN_CSUM |
2600 NETIF_F_HW_VLAN_CTAG_TX | 2637 NETIF_F_HW_VLAN_CTAG_TX |
2601 NETIF_F_HW_VLAN_STAG_TX); 2638 NETIF_F_HW_VLAN_STAG_TX);
2602 2639
2603finalize:
2604 if (dev->netdev_ops->ndo_features_check) 2640 if (dev->netdev_ops->ndo_features_check)
2605 features &= dev->netdev_ops->ndo_features_check(skb, dev, 2641 features &= dev->netdev_ops->ndo_features_check(skb, dev,
2606 features); 2642 features);
2643 else
2644 features &= dflt_features_check(skb, dev, features);
2607 2645
2608 return harmonize_features(skb, features); 2646 return harmonize_features(skb, features);
2609} 2647}
@@ -2675,7 +2713,7 @@ static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device
2675 if (unlikely(!skb)) 2713 if (unlikely(!skb))
2676 goto out_null; 2714 goto out_null;
2677 2715
2678 if (netif_needs_gso(dev, skb, features)) { 2716 if (netif_needs_gso(skb, features)) {
2679 struct sk_buff *segs; 2717 struct sk_buff *segs;
2680 2718
2681 segs = skb_gso_segment(skb, features); 2719 segs = skb_gso_segment(skb, features);
@@ -2857,7 +2895,7 @@ EXPORT_SYMBOL(xmit_recursion);
2857 * dev_loopback_xmit - loop back @skb 2895 * dev_loopback_xmit - loop back @skb
2858 * @skb: buffer to transmit 2896 * @skb: buffer to transmit
2859 */ 2897 */
2860int dev_loopback_xmit(struct sk_buff *skb) 2898int dev_loopback_xmit(struct sock *sk, struct sk_buff *skb)
2861{ 2899{
2862 skb_reset_mac_header(skb); 2900 skb_reset_mac_header(skb);
2863 __skb_pull(skb, skb_network_offset(skb)); 2901 __skb_pull(skb, skb_network_offset(skb));
@@ -2995,11 +3033,11 @@ out:
2995 return rc; 3033 return rc;
2996} 3034}
2997 3035
2998int dev_queue_xmit(struct sk_buff *skb) 3036int dev_queue_xmit_sk(struct sock *sk, struct sk_buff *skb)
2999{ 3037{
3000 return __dev_queue_xmit(skb, NULL); 3038 return __dev_queue_xmit(skb, NULL);
3001} 3039}
3002EXPORT_SYMBOL(dev_queue_xmit); 3040EXPORT_SYMBOL(dev_queue_xmit_sk);
3003 3041
3004int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv) 3042int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3005{ 3043{
@@ -3525,7 +3563,7 @@ static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3525 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue); 3563 struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue);
3526 3564
3527 if (!rxq || rcu_access_pointer(rxq->qdisc) == &noop_qdisc) 3565 if (!rxq || rcu_access_pointer(rxq->qdisc) == &noop_qdisc)
3528 goto out; 3566 return skb;
3529 3567
3530 if (*pt_prev) { 3568 if (*pt_prev) {
3531 *ret = deliver_skb(skb, *pt_prev, orig_dev); 3569 *ret = deliver_skb(skb, *pt_prev, orig_dev);
@@ -3539,8 +3577,6 @@ static inline struct sk_buff *handle_ing(struct sk_buff *skb,
3539 return NULL; 3577 return NULL;
3540 } 3578 }
3541 3579
3542out:
3543 skb->tc_verd = 0;
3544 return skb; 3580 return skb;
3545} 3581}
3546#endif 3582#endif
@@ -3676,12 +3712,15 @@ another_round:
3676 3712
3677skip_taps: 3713skip_taps:
3678#ifdef CONFIG_NET_CLS_ACT 3714#ifdef CONFIG_NET_CLS_ACT
3679 skb = handle_ing(skb, &pt_prev, &ret, orig_dev); 3715 if (static_key_false(&ingress_needed)) {
3680 if (!skb) 3716 skb = handle_ing(skb, &pt_prev, &ret, orig_dev);
3681 goto unlock; 3717 if (!skb)
3718 goto unlock;
3719 }
3720
3721 skb->tc_verd = 0;
3682ncls: 3722ncls:
3683#endif 3723#endif
3684
3685 if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) 3724 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
3686 goto drop; 3725 goto drop;
3687 3726
@@ -3831,13 +3870,13 @@ static int netif_receive_skb_internal(struct sk_buff *skb)
3831 * NET_RX_SUCCESS: no congestion 3870 * NET_RX_SUCCESS: no congestion
3832 * NET_RX_DROP: packet was dropped 3871 * NET_RX_DROP: packet was dropped
3833 */ 3872 */
3834int netif_receive_skb(struct sk_buff *skb) 3873int netif_receive_skb_sk(struct sock *sk, struct sk_buff *skb)
3835{ 3874{
3836 trace_netif_receive_skb_entry(skb); 3875 trace_netif_receive_skb_entry(skb);
3837 3876
3838 return netif_receive_skb_internal(skb); 3877 return netif_receive_skb_internal(skb);
3839} 3878}
3840EXPORT_SYMBOL(netif_receive_skb); 3879EXPORT_SYMBOL(netif_receive_skb_sk);
3841 3880
3842/* Network device is going away, flush any packets still pending 3881/* Network device is going away, flush any packets still pending
3843 * Called with irqs disabled. 3882 * Called with irqs disabled.
@@ -5914,6 +5953,24 @@ int dev_get_phys_port_id(struct net_device *dev,
5914EXPORT_SYMBOL(dev_get_phys_port_id); 5953EXPORT_SYMBOL(dev_get_phys_port_id);
5915 5954
5916/** 5955/**
5956 * dev_get_phys_port_name - Get device physical port name
5957 * @dev: device
5958 * @name: port name
5959 *
5960 * Get device physical port name
5961 */
5962int dev_get_phys_port_name(struct net_device *dev,
5963 char *name, size_t len)
5964{
5965 const struct net_device_ops *ops = dev->netdev_ops;
5966
5967 if (!ops->ndo_get_phys_port_name)
5968 return -EOPNOTSUPP;
5969 return ops->ndo_get_phys_port_name(dev, name, len);
5970}
5971EXPORT_SYMBOL(dev_get_phys_port_name);
5972
5973/**
5917 * dev_new_index - allocate an ifindex 5974 * dev_new_index - allocate an ifindex
5918 * @net: the applicable net namespace 5975 * @net: the applicable net namespace
5919 * 5976 *
@@ -5970,7 +6027,7 @@ static void rollback_registered_many(struct list_head *head)
5970 /* If device is running, close it first. */ 6027 /* If device is running, close it first. */
5971 list_for_each_entry(dev, head, unreg_list) 6028 list_for_each_entry(dev, head, unreg_list)
5972 list_add_tail(&dev->close_list, &close_head); 6029 list_add_tail(&dev->close_list, &close_head);
5973 dev_close_many(&close_head); 6030 dev_close_many(&close_head, true);
5974 6031
5975 list_for_each_entry(dev, head, unreg_list) { 6032 list_for_each_entry(dev, head, unreg_list) {
5976 /* And unlink it from device chain. */ 6033 /* And unlink it from device chain. */
@@ -6297,8 +6354,6 @@ int register_netdevice(struct net_device *dev)
6297 spin_lock_init(&dev->addr_list_lock); 6354 spin_lock_init(&dev->addr_list_lock);
6298 netdev_set_addr_lockdep_class(dev); 6355 netdev_set_addr_lockdep_class(dev);
6299 6356
6300 dev->iflink = -1;
6301
6302 ret = dev_get_valid_name(net, dev, dev->name); 6357 ret = dev_get_valid_name(net, dev, dev->name);
6303 if (ret < 0) 6358 if (ret < 0)
6304 goto out; 6359 goto out;
@@ -6328,9 +6383,6 @@ int register_netdevice(struct net_device *dev)
6328 else if (__dev_get_by_index(net, dev->ifindex)) 6383 else if (__dev_get_by_index(net, dev->ifindex))
6329 goto err_uninit; 6384 goto err_uninit;
6330 6385
6331 if (dev->iflink == -1)
6332 dev->iflink = dev->ifindex;
6333
6334 /* Transfer changeable features to wanted_features and enable 6386 /* Transfer changeable features to wanted_features and enable
6335 * software offloads (GSO and GRO). 6387 * software offloads (GSO and GRO).
6336 */ 6388 */
@@ -6843,8 +6895,6 @@ void free_netdev(struct net_device *dev)
6843{ 6895{
6844 struct napi_struct *p, *n; 6896 struct napi_struct *p, *n;
6845 6897
6846 release_net(dev_net(dev));
6847
6848 netif_free_tx_queues(dev); 6898 netif_free_tx_queues(dev);
6849#ifdef CONFIG_SYSFS 6899#ifdef CONFIG_SYSFS
6850 kvfree(dev->_rx); 6900 kvfree(dev->_rx);
@@ -7045,12 +7095,8 @@ int dev_change_net_namespace(struct net_device *dev, struct net *net, const char
7045 dev_net_set(dev, net); 7095 dev_net_set(dev, net);
7046 7096
7047 /* If there is an ifindex conflict assign a new one */ 7097 /* If there is an ifindex conflict assign a new one */
7048 if (__dev_get_by_index(net, dev->ifindex)) { 7098 if (__dev_get_by_index(net, dev->ifindex))
7049 int iflink = (dev->iflink == dev->ifindex);
7050 dev->ifindex = dev_new_index(net); 7099 dev->ifindex = dev_new_index(net);
7051 if (iflink)
7052 dev->iflink = dev->ifindex;
7053 }
7054 7100
7055 /* Send a netdev-add uevent to the new namespace */ 7101 /* Send a netdev-add uevent to the new namespace */
7056 kobject_uevent(&dev->dev.kobj, KOBJ_ADD); 7102 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
diff --git a/net/core/ethtool.c b/net/core/ethtool.c
index aa378ecef186..1d00b8922902 100644
--- a/net/core/ethtool.c
+++ b/net/core/ethtool.c
@@ -790,7 +790,7 @@ static noinline_for_stack int ethtool_set_rxfh(struct net_device *dev,
790 if (ops->get_rxfh_indir_size) 790 if (ops->get_rxfh_indir_size)
791 dev_indir_size = ops->get_rxfh_indir_size(dev); 791 dev_indir_size = ops->get_rxfh_indir_size(dev);
792 if (ops->get_rxfh_key_size) 792 if (ops->get_rxfh_key_size)
793 dev_key_size = dev->ethtool_ops->get_rxfh_key_size(dev); 793 dev_key_size = ops->get_rxfh_key_size(dev);
794 794
795 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh))) 795 if (copy_from_user(&rxfh, useraddr, sizeof(rxfh)))
796 return -EFAULT; 796 return -EFAULT;
diff --git a/net/core/fib_rules.c b/net/core/fib_rules.c
index e4fdc9dfb2c7..9a12668f7d62 100644
--- a/net/core/fib_rules.c
+++ b/net/core/fib_rules.c
@@ -31,7 +31,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
31 r->pref = pref; 31 r->pref = pref;
32 r->table = table; 32 r->table = table;
33 r->flags = flags; 33 r->flags = flags;
34 r->fr_net = hold_net(ops->fro_net); 34 r->fr_net = ops->fro_net;
35 35
36 r->suppress_prefixlen = -1; 36 r->suppress_prefixlen = -1;
37 r->suppress_ifgroup = -1; 37 r->suppress_ifgroup = -1;
@@ -116,7 +116,6 @@ static int __fib_rules_register(struct fib_rules_ops *ops)
116 if (ops->family == o->family) 116 if (ops->family == o->family)
117 goto errout; 117 goto errout;
118 118
119 hold_net(net);
120 list_add_tail_rcu(&ops->list, &net->rules_ops); 119 list_add_tail_rcu(&ops->list, &net->rules_ops);
121 err = 0; 120 err = 0;
122errout: 121errout:
@@ -160,15 +159,6 @@ static void fib_rules_cleanup_ops(struct fib_rules_ops *ops)
160 } 159 }
161} 160}
162 161
163static void fib_rules_put_rcu(struct rcu_head *head)
164{
165 struct fib_rules_ops *ops = container_of(head, struct fib_rules_ops, rcu);
166 struct net *net = ops->fro_net;
167
168 release_net(net);
169 kfree(ops);
170}
171
172void fib_rules_unregister(struct fib_rules_ops *ops) 162void fib_rules_unregister(struct fib_rules_ops *ops)
173{ 163{
174 struct net *net = ops->fro_net; 164 struct net *net = ops->fro_net;
@@ -178,7 +168,7 @@ void fib_rules_unregister(struct fib_rules_ops *ops)
178 spin_unlock(&net->rules_mod_lock); 168 spin_unlock(&net->rules_mod_lock);
179 169
180 fib_rules_cleanup_ops(ops); 170 fib_rules_cleanup_ops(ops);
181 call_rcu(&ops->rcu, fib_rules_put_rcu); 171 kfree_rcu(ops, rcu);
182} 172}
183EXPORT_SYMBOL_GPL(fib_rules_unregister); 173EXPORT_SYMBOL_GPL(fib_rules_unregister);
184 174
@@ -303,7 +293,7 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
303 err = -ENOMEM; 293 err = -ENOMEM;
304 goto errout; 294 goto errout;
305 } 295 }
306 rule->fr_net = hold_net(net); 296 rule->fr_net = net;
307 297
308 if (tb[FRA_PRIORITY]) 298 if (tb[FRA_PRIORITY])
309 rule->pref = nla_get_u32(tb[FRA_PRIORITY]); 299 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
@@ -423,7 +413,6 @@ static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh)
423 return 0; 413 return 0;
424 414
425errout_free: 415errout_free:
426 release_net(rule->fr_net);
427 kfree(rule); 416 kfree(rule);
428errout: 417errout:
429 rules_ops_put(ops); 418 rules_ops_put(ops);
@@ -492,6 +481,12 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
492 goto errout; 481 goto errout;
493 } 482 }
494 483
484 if (ops->delete) {
485 err = ops->delete(rule);
486 if (err)
487 goto errout;
488 }
489
495 list_del_rcu(&rule->list); 490 list_del_rcu(&rule->list);
496 491
497 if (rule->action == FR_ACT_GOTO) { 492 if (rule->action == FR_ACT_GOTO) {
@@ -517,8 +512,6 @@ static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh)
517 512
518 notify_rule_change(RTM_DELRULE, rule, ops, nlh, 513 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
519 NETLINK_CB(skb).portid); 514 NETLINK_CB(skb).portid);
520 if (ops->delete)
521 ops->delete(rule);
522 fib_rule_put(rule); 515 fib_rule_put(rule);
523 flush_route_cache(ops); 516 flush_route_cache(ops);
524 rules_ops_put(ops); 517 rules_ops_put(ops);
diff --git a/net/core/filter.c b/net/core/filter.c
index f6bdc2b1ba01..bf831a85c315 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -150,10 +150,62 @@ static u64 __get_random_u32(u64 ctx, u64 a, u64 x, u64 r4, u64 r5)
150 return prandom_u32(); 150 return prandom_u32();
151} 151}
152 152
153static u32 convert_skb_access(int skb_field, int dst_reg, int src_reg,
154 struct bpf_insn *insn_buf)
155{
156 struct bpf_insn *insn = insn_buf;
157
158 switch (skb_field) {
159 case SKF_AD_MARK:
160 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4);
161
162 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
163 offsetof(struct sk_buff, mark));
164 break;
165
166 case SKF_AD_PKTTYPE:
167 *insn++ = BPF_LDX_MEM(BPF_B, dst_reg, src_reg, PKT_TYPE_OFFSET());
168 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, PKT_TYPE_MAX);
169#ifdef __BIG_ENDIAN_BITFIELD
170 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 5);
171#endif
172 break;
173
174 case SKF_AD_QUEUE:
175 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2);
176
177 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
178 offsetof(struct sk_buff, queue_mapping));
179 break;
180
181 case SKF_AD_VLAN_TAG:
182 case SKF_AD_VLAN_TAG_PRESENT:
183 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2);
184 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000);
185
186 /* dst_reg = *(u16 *) (src_reg + offsetof(vlan_tci)) */
187 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
188 offsetof(struct sk_buff, vlan_tci));
189 if (skb_field == SKF_AD_VLAN_TAG) {
190 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg,
191 ~VLAN_TAG_PRESENT);
192 } else {
193 /* dst_reg >>= 12 */
194 *insn++ = BPF_ALU32_IMM(BPF_RSH, dst_reg, 12);
195 /* dst_reg &= 1 */
196 *insn++ = BPF_ALU32_IMM(BPF_AND, dst_reg, 1);
197 }
198 break;
199 }
200
201 return insn - insn_buf;
202}
203
153static bool convert_bpf_extensions(struct sock_filter *fp, 204static bool convert_bpf_extensions(struct sock_filter *fp,
154 struct bpf_insn **insnp) 205 struct bpf_insn **insnp)
155{ 206{
156 struct bpf_insn *insn = *insnp; 207 struct bpf_insn *insn = *insnp;
208 u32 cnt;
157 209
158 switch (fp->k) { 210 switch (fp->k) {
159 case SKF_AD_OFF + SKF_AD_PROTOCOL: 211 case SKF_AD_OFF + SKF_AD_PROTOCOL:
@@ -167,13 +219,8 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
167 break; 219 break;
168 220
169 case SKF_AD_OFF + SKF_AD_PKTTYPE: 221 case SKF_AD_OFF + SKF_AD_PKTTYPE:
170 *insn++ = BPF_LDX_MEM(BPF_B, BPF_REG_A, BPF_REG_CTX, 222 cnt = convert_skb_access(SKF_AD_PKTTYPE, BPF_REG_A, BPF_REG_CTX, insn);
171 PKT_TYPE_OFFSET()); 223 insn += cnt - 1;
172 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, PKT_TYPE_MAX);
173#ifdef __BIG_ENDIAN_BITFIELD
174 insn++;
175 *insn = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 5);
176#endif
177 break; 224 break;
178 225
179 case SKF_AD_OFF + SKF_AD_IFINDEX: 226 case SKF_AD_OFF + SKF_AD_IFINDEX:
@@ -197,10 +244,8 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
197 break; 244 break;
198 245
199 case SKF_AD_OFF + SKF_AD_MARK: 246 case SKF_AD_OFF + SKF_AD_MARK:
200 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, mark) != 4); 247 cnt = convert_skb_access(SKF_AD_MARK, BPF_REG_A, BPF_REG_CTX, insn);
201 248 insn += cnt - 1;
202 *insn = BPF_LDX_MEM(BPF_W, BPF_REG_A, BPF_REG_CTX,
203 offsetof(struct sk_buff, mark));
204 break; 249 break;
205 250
206 case SKF_AD_OFF + SKF_AD_RXHASH: 251 case SKF_AD_OFF + SKF_AD_RXHASH:
@@ -211,29 +256,30 @@ static bool convert_bpf_extensions(struct sock_filter *fp,
211 break; 256 break;
212 257
213 case SKF_AD_OFF + SKF_AD_QUEUE: 258 case SKF_AD_OFF + SKF_AD_QUEUE:
214 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, queue_mapping) != 2); 259 cnt = convert_skb_access(SKF_AD_QUEUE, BPF_REG_A, BPF_REG_CTX, insn);
215 260 insn += cnt - 1;
216 *insn = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
217 offsetof(struct sk_buff, queue_mapping));
218 break; 261 break;
219 262
220 case SKF_AD_OFF + SKF_AD_VLAN_TAG: 263 case SKF_AD_OFF + SKF_AD_VLAN_TAG:
264 cnt = convert_skb_access(SKF_AD_VLAN_TAG,
265 BPF_REG_A, BPF_REG_CTX, insn);
266 insn += cnt - 1;
267 break;
268
221 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT: 269 case SKF_AD_OFF + SKF_AD_VLAN_TAG_PRESENT:
222 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_tci) != 2); 270 cnt = convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
223 BUILD_BUG_ON(VLAN_TAG_PRESENT != 0x1000); 271 BPF_REG_A, BPF_REG_CTX, insn);
272 insn += cnt - 1;
273 break;
224 274
225 /* A = *(u16 *) (CTX + offsetof(vlan_tci)) */ 275 case SKF_AD_OFF + SKF_AD_VLAN_TPID:
276 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
277
278 /* A = *(u16 *) (CTX + offsetof(vlan_proto)) */
226 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX, 279 *insn++ = BPF_LDX_MEM(BPF_H, BPF_REG_A, BPF_REG_CTX,
227 offsetof(struct sk_buff, vlan_tci)); 280 offsetof(struct sk_buff, vlan_proto));
228 if (fp->k == SKF_AD_OFF + SKF_AD_VLAN_TAG) { 281 /* A = ntohs(A) [emitting a nop or swap16] */
229 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 282 *insn = BPF_ENDIAN(BPF_FROM_BE, BPF_REG_A, 16);
230 ~VLAN_TAG_PRESENT);
231 } else {
232 /* A >>= 12 */
233 *insn++ = BPF_ALU32_IMM(BPF_RSH, BPF_REG_A, 12);
234 /* A &= 1 */
235 *insn = BPF_ALU32_IMM(BPF_AND, BPF_REG_A, 1);
236 }
237 break; 283 break;
238 284
239 case SKF_AD_OFF + SKF_AD_PAY_OFFSET: 285 case SKF_AD_OFF + SKF_AD_PAY_OFFSET:
@@ -814,7 +860,7 @@ static void bpf_release_orig_filter(struct bpf_prog *fp)
814 860
815static void __bpf_prog_release(struct bpf_prog *prog) 861static void __bpf_prog_release(struct bpf_prog *prog)
816{ 862{
817 if (prog->aux->prog_type == BPF_PROG_TYPE_SOCKET_FILTER) { 863 if (prog->type == BPF_PROG_TYPE_SOCKET_FILTER) {
818 bpf_prog_put(prog); 864 bpf_prog_put(prog);
819 } else { 865 } else {
820 bpf_release_orig_filter(prog); 866 bpf_release_orig_filter(prog);
@@ -1019,6 +1065,32 @@ void bpf_prog_destroy(struct bpf_prog *fp)
1019} 1065}
1020EXPORT_SYMBOL_GPL(bpf_prog_destroy); 1066EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1021 1067
1068static int __sk_attach_prog(struct bpf_prog *prog, struct sock *sk)
1069{
1070 struct sk_filter *fp, *old_fp;
1071
1072 fp = kmalloc(sizeof(*fp), GFP_KERNEL);
1073 if (!fp)
1074 return -ENOMEM;
1075
1076 fp->prog = prog;
1077 atomic_set(&fp->refcnt, 0);
1078
1079 if (!sk_filter_charge(sk, fp)) {
1080 kfree(fp);
1081 return -ENOMEM;
1082 }
1083
1084 old_fp = rcu_dereference_protected(sk->sk_filter,
1085 sock_owned_by_user(sk));
1086 rcu_assign_pointer(sk->sk_filter, fp);
1087
1088 if (old_fp)
1089 sk_filter_uncharge(sk, old_fp);
1090
1091 return 0;
1092}
1093
1022/** 1094/**
1023 * sk_attach_filter - attach a socket filter 1095 * sk_attach_filter - attach a socket filter
1024 * @fprog: the filter program 1096 * @fprog: the filter program
@@ -1031,7 +1103,6 @@ EXPORT_SYMBOL_GPL(bpf_prog_destroy);
1031 */ 1103 */
1032int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk) 1104int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1033{ 1105{
1034 struct sk_filter *fp, *old_fp;
1035 unsigned int fsize = bpf_classic_proglen(fprog); 1106 unsigned int fsize = bpf_classic_proglen(fprog);
1036 unsigned int bpf_fsize = bpf_prog_size(fprog->len); 1107 unsigned int bpf_fsize = bpf_prog_size(fprog->len);
1037 struct bpf_prog *prog; 1108 struct bpf_prog *prog;
@@ -1068,36 +1139,20 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
1068 if (IS_ERR(prog)) 1139 if (IS_ERR(prog))
1069 return PTR_ERR(prog); 1140 return PTR_ERR(prog);
1070 1141
1071 fp = kmalloc(sizeof(*fp), GFP_KERNEL); 1142 err = __sk_attach_prog(prog, sk);
1072 if (!fp) { 1143 if (err < 0) {
1073 __bpf_prog_release(prog); 1144 __bpf_prog_release(prog);
1074 return -ENOMEM; 1145 return err;
1075 }
1076 fp->prog = prog;
1077
1078 atomic_set(&fp->refcnt, 0);
1079
1080 if (!sk_filter_charge(sk, fp)) {
1081 __sk_filter_release(fp);
1082 return -ENOMEM;
1083 } 1146 }
1084 1147
1085 old_fp = rcu_dereference_protected(sk->sk_filter,
1086 sock_owned_by_user(sk));
1087 rcu_assign_pointer(sk->sk_filter, fp);
1088
1089 if (old_fp)
1090 sk_filter_uncharge(sk, old_fp);
1091
1092 return 0; 1148 return 0;
1093} 1149}
1094EXPORT_SYMBOL_GPL(sk_attach_filter); 1150EXPORT_SYMBOL_GPL(sk_attach_filter);
1095 1151
1096#ifdef CONFIG_BPF_SYSCALL
1097int sk_attach_bpf(u32 ufd, struct sock *sk) 1152int sk_attach_bpf(u32 ufd, struct sock *sk)
1098{ 1153{
1099 struct sk_filter *fp, *old_fp;
1100 struct bpf_prog *prog; 1154 struct bpf_prog *prog;
1155 int err;
1101 1156
1102 if (sock_flag(sk, SOCK_FILTER_LOCKED)) 1157 if (sock_flag(sk, SOCK_FILTER_LOCKED))
1103 return -EPERM; 1158 return -EPERM;
@@ -1106,40 +1161,191 @@ int sk_attach_bpf(u32 ufd, struct sock *sk)
1106 if (IS_ERR(prog)) 1161 if (IS_ERR(prog))
1107 return PTR_ERR(prog); 1162 return PTR_ERR(prog);
1108 1163
1109 if (prog->aux->prog_type != BPF_PROG_TYPE_SOCKET_FILTER) { 1164 if (prog->type != BPF_PROG_TYPE_SOCKET_FILTER) {
1110 /* valid fd, but invalid program type */
1111 bpf_prog_put(prog); 1165 bpf_prog_put(prog);
1112 return -EINVAL; 1166 return -EINVAL;
1113 } 1167 }
1114 1168
1115 fp = kmalloc(sizeof(*fp), GFP_KERNEL); 1169 err = __sk_attach_prog(prog, sk);
1116 if (!fp) { 1170 if (err < 0) {
1117 bpf_prog_put(prog); 1171 bpf_prog_put(prog);
1118 return -ENOMEM; 1172 return err;
1119 } 1173 }
1120 fp->prog = prog;
1121 1174
1122 atomic_set(&fp->refcnt, 0); 1175 return 0;
1176}
1123 1177
1124 if (!sk_filter_charge(sk, fp)) { 1178/**
1125 __sk_filter_release(fp); 1179 * bpf_skb_clone_not_writable - is the header of a clone not writable
1126 return -ENOMEM; 1180 * @skb: buffer to check
1181 * @len: length up to which to write, can be negative
1182 *
1183 * Returns true if modifying the header part of the cloned buffer
1184 * does require the data to be copied. I.e. this version works with
1185 * negative lengths needed for eBPF case!
1186 */
1187static bool bpf_skb_clone_unwritable(const struct sk_buff *skb, int len)
1188{
1189 return skb_header_cloned(skb) ||
1190 (int) skb_headroom(skb) + len > skb->hdr_len;
1191}
1192
1193#define BPF_RECOMPUTE_CSUM(flags) ((flags) & 1)
1194
1195static u64 bpf_skb_store_bytes(u64 r1, u64 r2, u64 r3, u64 r4, u64 flags)
1196{
1197 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1198 int offset = (int) r2;
1199 void *from = (void *) (long) r3;
1200 unsigned int len = (unsigned int) r4;
1201 char buf[16];
1202 void *ptr;
1203
1204 /* bpf verifier guarantees that:
1205 * 'from' pointer points to bpf program stack
1206 * 'len' bytes of it were initialized
1207 * 'len' > 0
1208 * 'skb' is a valid pointer to 'struct sk_buff'
1209 *
1210 * so check for invalid 'offset' and too large 'len'
1211 */
1212 if (unlikely((u32) offset > 0xffff || len > sizeof(buf)))
1213 return -EFAULT;
1214
1215 offset -= skb->data - skb_mac_header(skb);
1216 if (unlikely(skb_cloned(skb) &&
1217 bpf_skb_clone_unwritable(skb, offset + len)))
1218 return -EFAULT;
1219
1220 ptr = skb_header_pointer(skb, offset, len, buf);
1221 if (unlikely(!ptr))
1222 return -EFAULT;
1223
1224 if (BPF_RECOMPUTE_CSUM(flags))
1225 skb_postpull_rcsum(skb, ptr, len);
1226
1227 memcpy(ptr, from, len);
1228
1229 if (ptr == buf)
1230 /* skb_store_bits cannot return -EFAULT here */
1231 skb_store_bits(skb, offset, ptr, len);
1232
1233 if (BPF_RECOMPUTE_CSUM(flags) && skb->ip_summed == CHECKSUM_COMPLETE)
1234 skb->csum = csum_add(skb->csum, csum_partial(ptr, len, 0));
1235 return 0;
1236}
1237
1238const struct bpf_func_proto bpf_skb_store_bytes_proto = {
1239 .func = bpf_skb_store_bytes,
1240 .gpl_only = false,
1241 .ret_type = RET_INTEGER,
1242 .arg1_type = ARG_PTR_TO_CTX,
1243 .arg2_type = ARG_ANYTHING,
1244 .arg3_type = ARG_PTR_TO_STACK,
1245 .arg4_type = ARG_CONST_STACK_SIZE,
1246 .arg5_type = ARG_ANYTHING,
1247};
1248
1249#define BPF_HEADER_FIELD_SIZE(flags) ((flags) & 0x0f)
1250#define BPF_IS_PSEUDO_HEADER(flags) ((flags) & 0x10)
1251
1252static u64 bpf_l3_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1253{
1254 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1255 int offset = (int) r2;
1256 __sum16 sum, *ptr;
1257
1258 if (unlikely((u32) offset > 0xffff))
1259 return -EFAULT;
1260
1261 offset -= skb->data - skb_mac_header(skb);
1262 if (unlikely(skb_cloned(skb) &&
1263 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1264 return -EFAULT;
1265
1266 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1267 if (unlikely(!ptr))
1268 return -EFAULT;
1269
1270 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1271 case 2:
1272 csum_replace2(ptr, from, to);
1273 break;
1274 case 4:
1275 csum_replace4(ptr, from, to);
1276 break;
1277 default:
1278 return -EINVAL;
1127 } 1279 }
1128 1280
1129 old_fp = rcu_dereference_protected(sk->sk_filter, 1281 if (ptr == &sum)
1130 sock_owned_by_user(sk)); 1282 /* skb_store_bits guaranteed to not return -EFAULT here */
1131 rcu_assign_pointer(sk->sk_filter, fp); 1283 skb_store_bits(skb, offset, ptr, sizeof(sum));
1132 1284
1133 if (old_fp) 1285 return 0;
1134 sk_filter_uncharge(sk, old_fp); 1286}
1287
1288const struct bpf_func_proto bpf_l3_csum_replace_proto = {
1289 .func = bpf_l3_csum_replace,
1290 .gpl_only = false,
1291 .ret_type = RET_INTEGER,
1292 .arg1_type = ARG_PTR_TO_CTX,
1293 .arg2_type = ARG_ANYTHING,
1294 .arg3_type = ARG_ANYTHING,
1295 .arg4_type = ARG_ANYTHING,
1296 .arg5_type = ARG_ANYTHING,
1297};
1298
1299static u64 bpf_l4_csum_replace(u64 r1, u64 r2, u64 from, u64 to, u64 flags)
1300{
1301 struct sk_buff *skb = (struct sk_buff *) (long) r1;
1302 u32 is_pseudo = BPF_IS_PSEUDO_HEADER(flags);
1303 int offset = (int) r2;
1304 __sum16 sum, *ptr;
1305
1306 if (unlikely((u32) offset > 0xffff))
1307 return -EFAULT;
1308
1309 offset -= skb->data - skb_mac_header(skb);
1310 if (unlikely(skb_cloned(skb) &&
1311 bpf_skb_clone_unwritable(skb, offset + sizeof(sum))))
1312 return -EFAULT;
1313
1314 ptr = skb_header_pointer(skb, offset, sizeof(sum), &sum);
1315 if (unlikely(!ptr))
1316 return -EFAULT;
1317
1318 switch (BPF_HEADER_FIELD_SIZE(flags)) {
1319 case 2:
1320 inet_proto_csum_replace2(ptr, skb, from, to, is_pseudo);
1321 break;
1322 case 4:
1323 inet_proto_csum_replace4(ptr, skb, from, to, is_pseudo);
1324 break;
1325 default:
1326 return -EINVAL;
1327 }
1328
1329 if (ptr == &sum)
1330 /* skb_store_bits guaranteed to not return -EFAULT here */
1331 skb_store_bits(skb, offset, ptr, sizeof(sum));
1135 1332
1136 return 0; 1333 return 0;
1137} 1334}
1138 1335
1139/* allow socket filters to call 1336const struct bpf_func_proto bpf_l4_csum_replace_proto = {
1140 * bpf_map_lookup_elem(), bpf_map_update_elem(), bpf_map_delete_elem() 1337 .func = bpf_l4_csum_replace,
1141 */ 1338 .gpl_only = false,
1142static const struct bpf_func_proto *sock_filter_func_proto(enum bpf_func_id func_id) 1339 .ret_type = RET_INTEGER,
1340 .arg1_type = ARG_PTR_TO_CTX,
1341 .arg2_type = ARG_ANYTHING,
1342 .arg3_type = ARG_ANYTHING,
1343 .arg4_type = ARG_ANYTHING,
1344 .arg5_type = ARG_ANYTHING,
1345};
1346
1347static const struct bpf_func_proto *
1348sk_filter_func_proto(enum bpf_func_id func_id)
1143{ 1349{
1144 switch (func_id) { 1350 switch (func_id) {
1145 case BPF_FUNC_map_lookup_elem: 1351 case BPF_FUNC_map_lookup_elem:
@@ -1148,39 +1354,144 @@ static const struct bpf_func_proto *sock_filter_func_proto(enum bpf_func_id func
1148 return &bpf_map_update_elem_proto; 1354 return &bpf_map_update_elem_proto;
1149 case BPF_FUNC_map_delete_elem: 1355 case BPF_FUNC_map_delete_elem:
1150 return &bpf_map_delete_elem_proto; 1356 return &bpf_map_delete_elem_proto;
1357 case BPF_FUNC_get_prandom_u32:
1358 return &bpf_get_prandom_u32_proto;
1359 case BPF_FUNC_get_smp_processor_id:
1360 return &bpf_get_smp_processor_id_proto;
1151 default: 1361 default:
1152 return NULL; 1362 return NULL;
1153 } 1363 }
1154} 1364}
1155 1365
1156static bool sock_filter_is_valid_access(int off, int size, enum bpf_access_type type) 1366static const struct bpf_func_proto *
1367tc_cls_act_func_proto(enum bpf_func_id func_id)
1157{ 1368{
1158 /* skb fields cannot be accessed yet */ 1369 switch (func_id) {
1159 return false; 1370 case BPF_FUNC_skb_store_bytes:
1371 return &bpf_skb_store_bytes_proto;
1372 case BPF_FUNC_l3_csum_replace:
1373 return &bpf_l3_csum_replace_proto;
1374 case BPF_FUNC_l4_csum_replace:
1375 return &bpf_l4_csum_replace_proto;
1376 default:
1377 return sk_filter_func_proto(func_id);
1378 }
1160} 1379}
1161 1380
1162static struct bpf_verifier_ops sock_filter_ops = { 1381static bool sk_filter_is_valid_access(int off, int size,
1163 .get_func_proto = sock_filter_func_proto, 1382 enum bpf_access_type type)
1164 .is_valid_access = sock_filter_is_valid_access, 1383{
1384 /* only read is allowed */
1385 if (type != BPF_READ)
1386 return false;
1387
1388 /* check bounds */
1389 if (off < 0 || off >= sizeof(struct __sk_buff))
1390 return false;
1391
1392 /* disallow misaligned access */
1393 if (off % size != 0)
1394 return false;
1395
1396 /* all __sk_buff fields are __u32 */
1397 if (size != 4)
1398 return false;
1399
1400 return true;
1401}
1402
1403static u32 sk_filter_convert_ctx_access(int dst_reg, int src_reg, int ctx_off,
1404 struct bpf_insn *insn_buf)
1405{
1406 struct bpf_insn *insn = insn_buf;
1407
1408 switch (ctx_off) {
1409 case offsetof(struct __sk_buff, len):
1410 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, len) != 4);
1411
1412 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1413 offsetof(struct sk_buff, len));
1414 break;
1415
1416 case offsetof(struct __sk_buff, protocol):
1417 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, protocol) != 2);
1418
1419 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1420 offsetof(struct sk_buff, protocol));
1421 break;
1422
1423 case offsetof(struct __sk_buff, vlan_proto):
1424 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, vlan_proto) != 2);
1425
1426 *insn++ = BPF_LDX_MEM(BPF_H, dst_reg, src_reg,
1427 offsetof(struct sk_buff, vlan_proto));
1428 break;
1429
1430 case offsetof(struct __sk_buff, priority):
1431 BUILD_BUG_ON(FIELD_SIZEOF(struct sk_buff, priority) != 4);
1432
1433 *insn++ = BPF_LDX_MEM(BPF_W, dst_reg, src_reg,
1434 offsetof(struct sk_buff, priority));
1435 break;
1436
1437 case offsetof(struct __sk_buff, mark):
1438 return convert_skb_access(SKF_AD_MARK, dst_reg, src_reg, insn);
1439
1440 case offsetof(struct __sk_buff, pkt_type):
1441 return convert_skb_access(SKF_AD_PKTTYPE, dst_reg, src_reg, insn);
1442
1443 case offsetof(struct __sk_buff, queue_mapping):
1444 return convert_skb_access(SKF_AD_QUEUE, dst_reg, src_reg, insn);
1445
1446 case offsetof(struct __sk_buff, vlan_present):
1447 return convert_skb_access(SKF_AD_VLAN_TAG_PRESENT,
1448 dst_reg, src_reg, insn);
1449
1450 case offsetof(struct __sk_buff, vlan_tci):
1451 return convert_skb_access(SKF_AD_VLAN_TAG,
1452 dst_reg, src_reg, insn);
1453 }
1454
1455 return insn - insn_buf;
1456}
1457
1458static const struct bpf_verifier_ops sk_filter_ops = {
1459 .get_func_proto = sk_filter_func_proto,
1460 .is_valid_access = sk_filter_is_valid_access,
1461 .convert_ctx_access = sk_filter_convert_ctx_access,
1462};
1463
1464static const struct bpf_verifier_ops tc_cls_act_ops = {
1465 .get_func_proto = tc_cls_act_func_proto,
1466 .is_valid_access = sk_filter_is_valid_access,
1467 .convert_ctx_access = sk_filter_convert_ctx_access,
1165}; 1468};
1166 1469
1167static struct bpf_prog_type_list tl = { 1470static struct bpf_prog_type_list sk_filter_type __read_mostly = {
1168 .ops = &sock_filter_ops, 1471 .ops = &sk_filter_ops,
1169 .type = BPF_PROG_TYPE_SOCKET_FILTER, 1472 .type = BPF_PROG_TYPE_SOCKET_FILTER,
1170}; 1473};
1171 1474
1172static int __init register_sock_filter_ops(void) 1475static struct bpf_prog_type_list sched_cls_type __read_mostly = {
1476 .ops = &tc_cls_act_ops,
1477 .type = BPF_PROG_TYPE_SCHED_CLS,
1478};
1479
1480static struct bpf_prog_type_list sched_act_type __read_mostly = {
1481 .ops = &tc_cls_act_ops,
1482 .type = BPF_PROG_TYPE_SCHED_ACT,
1483};
1484
1485static int __init register_sk_filter_ops(void)
1173{ 1486{
1174 bpf_register_prog_type(&tl); 1487 bpf_register_prog_type(&sk_filter_type);
1488 bpf_register_prog_type(&sched_cls_type);
1489 bpf_register_prog_type(&sched_act_type);
1490
1175 return 0; 1491 return 0;
1176} 1492}
1177late_initcall(register_sock_filter_ops); 1493late_initcall(register_sk_filter_ops);
1178#else 1494
1179int sk_attach_bpf(u32 ufd, struct sock *sk)
1180{
1181 return -EOPNOTSUPP;
1182}
1183#endif
1184int sk_detach_filter(struct sock *sk) 1495int sk_detach_filter(struct sock *sk)
1185{ 1496{
1186 int ret = -ENOENT; 1497 int ret = -ENOENT;
diff --git a/net/core/link_watch.c b/net/core/link_watch.c
index 49a9e3e06c08..982861607f88 100644
--- a/net/core/link_watch.c
+++ b/net/core/link_watch.c
@@ -40,7 +40,7 @@ static DEFINE_SPINLOCK(lweventlist_lock);
40static unsigned char default_operstate(const struct net_device *dev) 40static unsigned char default_operstate(const struct net_device *dev)
41{ 41{
42 if (!netif_carrier_ok(dev)) 42 if (!netif_carrier_ok(dev))
43 return (dev->ifindex != dev->iflink ? 43 return (dev->ifindex != dev_get_iflink(dev) ?
44 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN); 44 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
45 45
46 if (netif_dormant(dev)) 46 if (netif_dormant(dev))
@@ -89,7 +89,7 @@ static bool linkwatch_urgent_event(struct net_device *dev)
89 if (!netif_running(dev)) 89 if (!netif_running(dev))
90 return false; 90 return false;
91 91
92 if (dev->ifindex != dev->iflink) 92 if (dev->ifindex != dev_get_iflink(dev))
93 return true; 93 return true;
94 94
95 if (dev->priv_flags & IFF_TEAM_PORT) 95 if (dev->priv_flags & IFF_TEAM_PORT)
diff --git a/net/core/neighbour.c b/net/core/neighbour.c
index 70fe9e10ac86..3de654256028 100644
--- a/net/core/neighbour.c
+++ b/net/core/neighbour.c
@@ -397,25 +397,15 @@ struct neighbour *neigh_lookup(struct neigh_table *tbl, const void *pkey,
397 struct net_device *dev) 397 struct net_device *dev)
398{ 398{
399 struct neighbour *n; 399 struct neighbour *n;
400 int key_len = tbl->key_len;
401 u32 hash_val;
402 struct neigh_hash_table *nht;
403 400
404 NEIGH_CACHE_STAT_INC(tbl, lookups); 401 NEIGH_CACHE_STAT_INC(tbl, lookups);
405 402
406 rcu_read_lock_bh(); 403 rcu_read_lock_bh();
407 nht = rcu_dereference_bh(tbl->nht); 404 n = __neigh_lookup_noref(tbl, pkey, dev);
408 hash_val = tbl->hash(pkey, dev, nht->hash_rnd) >> (32 - nht->hash_shift); 405 if (n) {
409 406 if (!atomic_inc_not_zero(&n->refcnt))
410 for (n = rcu_dereference_bh(nht->hash_buckets[hash_val]); 407 n = NULL;
411 n != NULL; 408 NEIGH_CACHE_STAT_INC(tbl, hits);
412 n = rcu_dereference_bh(n->next)) {
413 if (dev == n->dev && !memcmp(n->primary_key, pkey, key_len)) {
414 if (!atomic_inc_not_zero(&n->refcnt))
415 n = NULL;
416 NEIGH_CACHE_STAT_INC(tbl, hits);
417 break;
418 }
419 } 409 }
420 410
421 rcu_read_unlock_bh(); 411 rcu_read_unlock_bh();
@@ -601,7 +591,7 @@ struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
601 if (!n) 591 if (!n)
602 goto out; 592 goto out;
603 593
604 write_pnet(&n->net, hold_net(net)); 594 write_pnet(&n->net, net);
605 memcpy(n->key, pkey, key_len); 595 memcpy(n->key, pkey, key_len);
606 n->dev = dev; 596 n->dev = dev;
607 if (dev) 597 if (dev)
@@ -610,7 +600,6 @@ struct pneigh_entry * pneigh_lookup(struct neigh_table *tbl,
610 if (tbl->pconstructor && tbl->pconstructor(n)) { 600 if (tbl->pconstructor && tbl->pconstructor(n)) {
611 if (dev) 601 if (dev)
612 dev_put(dev); 602 dev_put(dev);
613 release_net(net);
614 kfree(n); 603 kfree(n);
615 n = NULL; 604 n = NULL;
616 goto out; 605 goto out;
@@ -644,7 +633,6 @@ int pneigh_delete(struct neigh_table *tbl, struct net *net, const void *pkey,
644 tbl->pdestructor(n); 633 tbl->pdestructor(n);
645 if (n->dev) 634 if (n->dev)
646 dev_put(n->dev); 635 dev_put(n->dev);
647 release_net(pneigh_net(n));
648 kfree(n); 636 kfree(n);
649 return 0; 637 return 0;
650 } 638 }
@@ -667,7 +655,6 @@ static int pneigh_ifdown(struct neigh_table *tbl, struct net_device *dev)
667 tbl->pdestructor(n); 655 tbl->pdestructor(n);
668 if (n->dev) 656 if (n->dev)
669 dev_put(n->dev); 657 dev_put(n->dev);
670 release_net(pneigh_net(n));
671 kfree(n); 658 kfree(n);
672 continue; 659 continue;
673 } 660 }
@@ -830,10 +817,9 @@ out:
830static __inline__ int neigh_max_probes(struct neighbour *n) 817static __inline__ int neigh_max_probes(struct neighbour *n)
831{ 818{
832 struct neigh_parms *p = n->parms; 819 struct neigh_parms *p = n->parms;
833 int max_probes = NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES); 820 return NEIGH_VAR(p, UCAST_PROBES) + NEIGH_VAR(p, APP_PROBES) +
834 if (!(n->nud_state & NUD_PROBE)) 821 (n->nud_state & NUD_PROBE ? NEIGH_VAR(p, MCAST_REPROBES) :
835 max_probes += NEIGH_VAR(p, MCAST_PROBES); 822 NEIGH_VAR(p, MCAST_PROBES));
836 return max_probes;
837} 823}
838 824
839static void neigh_invalidate(struct neighbour *neigh) 825static void neigh_invalidate(struct neighbour *neigh)
@@ -1263,10 +1249,10 @@ struct neighbour *neigh_event_ns(struct neigh_table *tbl,
1263EXPORT_SYMBOL(neigh_event_ns); 1249EXPORT_SYMBOL(neigh_event_ns);
1264 1250
1265/* called with read_lock_bh(&n->lock); */ 1251/* called with read_lock_bh(&n->lock); */
1266static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst) 1252static void neigh_hh_init(struct neighbour *n)
1267{ 1253{
1268 struct net_device *dev = dst->dev; 1254 struct net_device *dev = n->dev;
1269 __be16 prot = dst->ops->protocol; 1255 __be16 prot = n->tbl->protocol;
1270 struct hh_cache *hh = &n->hh; 1256 struct hh_cache *hh = &n->hh;
1271 1257
1272 write_lock_bh(&n->lock); 1258 write_lock_bh(&n->lock);
@@ -1280,43 +1266,19 @@ static void neigh_hh_init(struct neighbour *n, struct dst_entry *dst)
1280 write_unlock_bh(&n->lock); 1266 write_unlock_bh(&n->lock);
1281} 1267}
1282 1268
1283/* This function can be used in contexts, where only old dev_queue_xmit
1284 * worked, f.e. if you want to override normal output path (eql, shaper),
1285 * but resolution is not made yet.
1286 */
1287
1288int neigh_compat_output(struct neighbour *neigh, struct sk_buff *skb)
1289{
1290 struct net_device *dev = skb->dev;
1291
1292 __skb_pull(skb, skb_network_offset(skb));
1293
1294 if (dev_hard_header(skb, dev, ntohs(skb->protocol), NULL, NULL,
1295 skb->len) < 0 &&
1296 dev_rebuild_header(skb))
1297 return 0;
1298
1299 return dev_queue_xmit(skb);
1300}
1301EXPORT_SYMBOL(neigh_compat_output);
1302
1303/* Slow and careful. */ 1269/* Slow and careful. */
1304 1270
1305int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb) 1271int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1306{ 1272{
1307 struct dst_entry *dst = skb_dst(skb);
1308 int rc = 0; 1273 int rc = 0;
1309 1274
1310 if (!dst)
1311 goto discard;
1312
1313 if (!neigh_event_send(neigh, skb)) { 1275 if (!neigh_event_send(neigh, skb)) {
1314 int err; 1276 int err;
1315 struct net_device *dev = neigh->dev; 1277 struct net_device *dev = neigh->dev;
1316 unsigned int seq; 1278 unsigned int seq;
1317 1279
1318 if (dev->header_ops->cache && !neigh->hh.hh_len) 1280 if (dev->header_ops->cache && !neigh->hh.hh_len)
1319 neigh_hh_init(neigh, dst); 1281 neigh_hh_init(neigh);
1320 1282
1321 do { 1283 do {
1322 __skb_pull(skb, skb_network_offset(skb)); 1284 __skb_pull(skb, skb_network_offset(skb));
@@ -1332,8 +1294,6 @@ int neigh_resolve_output(struct neighbour *neigh, struct sk_buff *skb)
1332 } 1294 }
1333out: 1295out:
1334 return rc; 1296 return rc;
1335discard:
1336 neigh_dbg(1, "%s: dst=%p neigh=%p\n", __func__, dst, neigh);
1337out_kfree_skb: 1297out_kfree_skb:
1338 rc = -EINVAL; 1298 rc = -EINVAL;
1339 kfree_skb(skb); 1299 kfree_skb(skb);
@@ -1464,11 +1424,10 @@ struct neigh_parms *neigh_parms_alloc(struct net_device *dev,
1464 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME)); 1424 neigh_rand_reach_time(NEIGH_VAR(p, BASE_REACHABLE_TIME));
1465 dev_hold(dev); 1425 dev_hold(dev);
1466 p->dev = dev; 1426 p->dev = dev;
1467 write_pnet(&p->net, hold_net(net)); 1427 write_pnet(&p->net, net);
1468 p->sysctl_table = NULL; 1428 p->sysctl_table = NULL;
1469 1429
1470 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) { 1430 if (ops->ndo_neigh_setup && ops->ndo_neigh_setup(dev, p)) {
1471 release_net(net);
1472 dev_put(dev); 1431 dev_put(dev);
1473 kfree(p); 1432 kfree(p);
1474 return NULL; 1433 return NULL;
@@ -1508,7 +1467,6 @@ EXPORT_SYMBOL(neigh_parms_release);
1508 1467
1509static void neigh_parms_destroy(struct neigh_parms *parms) 1468static void neigh_parms_destroy(struct neigh_parms *parms)
1510{ 1469{
1511 release_net(neigh_parms_net(parms));
1512 kfree(parms); 1470 kfree(parms);
1513} 1471}
1514 1472
@@ -1783,6 +1741,8 @@ static int neightbl_fill_parms(struct sk_buff *skb, struct neigh_parms *parms)
1783 NEIGH_VAR(parms, UCAST_PROBES)) || 1741 NEIGH_VAR(parms, UCAST_PROBES)) ||
1784 nla_put_u32(skb, NDTPA_MCAST_PROBES, 1742 nla_put_u32(skb, NDTPA_MCAST_PROBES,
1785 NEIGH_VAR(parms, MCAST_PROBES)) || 1743 NEIGH_VAR(parms, MCAST_PROBES)) ||
1744 nla_put_u32(skb, NDTPA_MCAST_REPROBES,
1745 NEIGH_VAR(parms, MCAST_REPROBES)) ||
1786 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) || 1746 nla_put_msecs(skb, NDTPA_REACHABLE_TIME, parms->reachable_time) ||
1787 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME, 1747 nla_put_msecs(skb, NDTPA_BASE_REACHABLE_TIME,
1788 NEIGH_VAR(parms, BASE_REACHABLE_TIME)) || 1748 NEIGH_VAR(parms, BASE_REACHABLE_TIME)) ||
@@ -1942,6 +1902,7 @@ static const struct nla_policy nl_ntbl_parm_policy[NDTPA_MAX+1] = {
1942 [NDTPA_APP_PROBES] = { .type = NLA_U32 }, 1902 [NDTPA_APP_PROBES] = { .type = NLA_U32 },
1943 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 }, 1903 [NDTPA_UCAST_PROBES] = { .type = NLA_U32 },
1944 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 }, 1904 [NDTPA_MCAST_PROBES] = { .type = NLA_U32 },
1905 [NDTPA_MCAST_REPROBES] = { .type = NLA_U32 },
1945 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 }, 1906 [NDTPA_BASE_REACHABLE_TIME] = { .type = NLA_U64 },
1946 [NDTPA_GC_STALETIME] = { .type = NLA_U64 }, 1907 [NDTPA_GC_STALETIME] = { .type = NLA_U64 },
1947 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 }, 1908 [NDTPA_DELAY_PROBE_TIME] = { .type = NLA_U64 },
@@ -2042,6 +2003,10 @@ static int neightbl_set(struct sk_buff *skb, struct nlmsghdr *nlh)
2042 NEIGH_VAR_SET(p, MCAST_PROBES, 2003 NEIGH_VAR_SET(p, MCAST_PROBES,
2043 nla_get_u32(tbp[i])); 2004 nla_get_u32(tbp[i]));
2044 break; 2005 break;
2006 case NDTPA_MCAST_REPROBES:
2007 NEIGH_VAR_SET(p, MCAST_REPROBES,
2008 nla_get_u32(tbp[i]));
2009 break;
2045 case NDTPA_BASE_REACHABLE_TIME: 2010 case NDTPA_BASE_REACHABLE_TIME:
2046 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME, 2011 NEIGH_VAR_SET(p, BASE_REACHABLE_TIME,
2047 nla_get_msecs(tbp[i])); 2012 nla_get_msecs(tbp[i]));
@@ -2427,6 +2392,40 @@ void __neigh_for_each_release(struct neigh_table *tbl,
2427} 2392}
2428EXPORT_SYMBOL(__neigh_for_each_release); 2393EXPORT_SYMBOL(__neigh_for_each_release);
2429 2394
2395int neigh_xmit(int index, struct net_device *dev,
2396 const void *addr, struct sk_buff *skb)
2397{
2398 int err = -EAFNOSUPPORT;
2399 if (likely(index < NEIGH_NR_TABLES)) {
2400 struct neigh_table *tbl;
2401 struct neighbour *neigh;
2402
2403 tbl = neigh_tables[index];
2404 if (!tbl)
2405 goto out;
2406 neigh = __neigh_lookup_noref(tbl, addr, dev);
2407 if (!neigh)
2408 neigh = __neigh_create(tbl, addr, dev, false);
2409 err = PTR_ERR(neigh);
2410 if (IS_ERR(neigh))
2411 goto out_kfree_skb;
2412 err = neigh->output(neigh, skb);
2413 }
2414 else if (index == NEIGH_LINK_TABLE) {
2415 err = dev_hard_header(skb, dev, ntohs(skb->protocol),
2416 addr, NULL, skb->len);
2417 if (err < 0)
2418 goto out_kfree_skb;
2419 err = dev_queue_xmit(skb);
2420 }
2421out:
2422 return err;
2423out_kfree_skb:
2424 kfree_skb(skb);
2425 goto out;
2426}
2427EXPORT_SYMBOL(neigh_xmit);
2428
2430#ifdef CONFIG_PROC_FS 2429#ifdef CONFIG_PROC_FS
2431 2430
2432static struct neighbour *neigh_get_first(struct seq_file *seq) 2431static struct neighbour *neigh_get_first(struct seq_file *seq)
@@ -2994,6 +2993,7 @@ static struct neigh_sysctl_table {
2994 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"), 2993 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_PROBES, "mcast_solicit"),
2995 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"), 2994 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(UCAST_PROBES, "ucast_solicit"),
2996 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"), 2995 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(APP_PROBES, "app_solicit"),
2996 NEIGH_SYSCTL_ZERO_INTMAX_ENTRY(MCAST_REPROBES, "mcast_resolicit"),
2997 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"), 2997 NEIGH_SYSCTL_USERHZ_JIFFIES_ENTRY(RETRANS_TIME, "retrans_time"),
2998 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"), 2998 NEIGH_SYSCTL_JIFFIES_ENTRY(BASE_REACHABLE_TIME, "base_reachable_time"),
2999 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"), 2999 NEIGH_SYSCTL_JIFFIES_ENTRY(DELAY_PROBE_TIME, "delay_first_probe_time"),
diff --git a/net/core/net-sysfs.c b/net/core/net-sysfs.c
index f2aa73bfb0e4..4238d6da5c60 100644
--- a/net/core/net-sysfs.c
+++ b/net/core/net-sysfs.c
@@ -23,6 +23,7 @@
23#include <linux/export.h> 23#include <linux/export.h>
24#include <linux/jiffies.h> 24#include <linux/jiffies.h>
25#include <linux/pm_runtime.h> 25#include <linux/pm_runtime.h>
26#include <linux/of.h>
26 27
27#include "net-sysfs.h" 28#include "net-sysfs.h"
28 29
@@ -108,11 +109,19 @@ NETDEVICE_SHOW_RO(dev_id, fmt_hex);
108NETDEVICE_SHOW_RO(dev_port, fmt_dec); 109NETDEVICE_SHOW_RO(dev_port, fmt_dec);
109NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec); 110NETDEVICE_SHOW_RO(addr_assign_type, fmt_dec);
110NETDEVICE_SHOW_RO(addr_len, fmt_dec); 111NETDEVICE_SHOW_RO(addr_len, fmt_dec);
111NETDEVICE_SHOW_RO(iflink, fmt_dec);
112NETDEVICE_SHOW_RO(ifindex, fmt_dec); 112NETDEVICE_SHOW_RO(ifindex, fmt_dec);
113NETDEVICE_SHOW_RO(type, fmt_dec); 113NETDEVICE_SHOW_RO(type, fmt_dec);
114NETDEVICE_SHOW_RO(link_mode, fmt_dec); 114NETDEVICE_SHOW_RO(link_mode, fmt_dec);
115 115
116static ssize_t iflink_show(struct device *dev, struct device_attribute *attr,
117 char *buf)
118{
119 struct net_device *ndev = to_net_dev(dev);
120
121 return sprintf(buf, fmt_dec, dev_get_iflink(ndev));
122}
123static DEVICE_ATTR_RO(iflink);
124
116static ssize_t format_name_assign_type(const struct net_device *dev, char *buf) 125static ssize_t format_name_assign_type(const struct net_device *dev, char *buf)
117{ 126{
118 return sprintf(buf, fmt_dec, dev->name_assign_type); 127 return sprintf(buf, fmt_dec, dev->name_assign_type);
@@ -417,6 +426,28 @@ static ssize_t phys_port_id_show(struct device *dev,
417} 426}
418static DEVICE_ATTR_RO(phys_port_id); 427static DEVICE_ATTR_RO(phys_port_id);
419 428
429static ssize_t phys_port_name_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
432 struct net_device *netdev = to_net_dev(dev);
433 ssize_t ret = -EINVAL;
434
435 if (!rtnl_trylock())
436 return restart_syscall();
437
438 if (dev_isalive(netdev)) {
439 char name[IFNAMSIZ];
440
441 ret = dev_get_phys_port_name(netdev, name, sizeof(name));
442 if (!ret)
443 ret = sprintf(buf, "%s\n", name);
444 }
445 rtnl_unlock();
446
447 return ret;
448}
449static DEVICE_ATTR_RO(phys_port_name);
450
420static ssize_t phys_switch_id_show(struct device *dev, 451static ssize_t phys_switch_id_show(struct device *dev,
421 struct device_attribute *attr, char *buf) 452 struct device_attribute *attr, char *buf)
422{ 453{
@@ -464,6 +495,7 @@ static struct attribute *net_class_attrs[] = {
464 &dev_attr_tx_queue_len.attr, 495 &dev_attr_tx_queue_len.attr,
465 &dev_attr_gro_flush_timeout.attr, 496 &dev_attr_gro_flush_timeout.attr,
466 &dev_attr_phys_port_id.attr, 497 &dev_attr_phys_port_id.attr,
498 &dev_attr_phys_port_name.attr,
467 &dev_attr_phys_switch_id.attr, 499 &dev_attr_phys_switch_id.attr,
468 NULL, 500 NULL,
469}; 501};
@@ -950,6 +982,60 @@ static ssize_t show_trans_timeout(struct netdev_queue *queue,
950 return sprintf(buf, "%lu", trans_timeout); 982 return sprintf(buf, "%lu", trans_timeout);
951} 983}
952 984
985#ifdef CONFIG_XPS
986static inline unsigned int get_netdev_queue_index(struct netdev_queue *queue)
987{
988 struct net_device *dev = queue->dev;
989 int i;
990
991 for (i = 0; i < dev->num_tx_queues; i++)
992 if (queue == &dev->_tx[i])
993 break;
994
995 BUG_ON(i >= dev->num_tx_queues);
996
997 return i;
998}
999
1000static ssize_t show_tx_maxrate(struct netdev_queue *queue,
1001 struct netdev_queue_attribute *attribute,
1002 char *buf)
1003{
1004 return sprintf(buf, "%lu\n", queue->tx_maxrate);
1005}
1006
1007static ssize_t set_tx_maxrate(struct netdev_queue *queue,
1008 struct netdev_queue_attribute *attribute,
1009 const char *buf, size_t len)
1010{
1011 struct net_device *dev = queue->dev;
1012 int err, index = get_netdev_queue_index(queue);
1013 u32 rate = 0;
1014
1015 err = kstrtou32(buf, 10, &rate);
1016 if (err < 0)
1017 return err;
1018
1019 if (!rtnl_trylock())
1020 return restart_syscall();
1021
1022 err = -EOPNOTSUPP;
1023 if (dev->netdev_ops->ndo_set_tx_maxrate)
1024 err = dev->netdev_ops->ndo_set_tx_maxrate(dev, index, rate);
1025
1026 rtnl_unlock();
1027 if (!err) {
1028 queue->tx_maxrate = rate;
1029 return len;
1030 }
1031 return err;
1032}
1033
1034static struct netdev_queue_attribute queue_tx_maxrate =
1035 __ATTR(tx_maxrate, S_IRUGO | S_IWUSR,
1036 show_tx_maxrate, set_tx_maxrate);
1037#endif
1038
953static struct netdev_queue_attribute queue_trans_timeout = 1039static struct netdev_queue_attribute queue_trans_timeout =
954 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL); 1040 __ATTR(tx_timeout, S_IRUGO, show_trans_timeout, NULL);
955 1041
@@ -1064,18 +1150,6 @@ static struct attribute_group dql_group = {
1064#endif /* CONFIG_BQL */ 1150#endif /* CONFIG_BQL */
1065 1151
1066#ifdef CONFIG_XPS 1152#ifdef CONFIG_XPS
1067static unsigned int get_netdev_queue_index(struct netdev_queue *queue)
1068{
1069 struct net_device *dev = queue->dev;
1070 unsigned int i;
1071
1072 i = queue - dev->_tx;
1073 BUG_ON(i >= dev->num_tx_queues);
1074
1075 return i;
1076}
1077
1078
1079static ssize_t show_xps_map(struct netdev_queue *queue, 1153static ssize_t show_xps_map(struct netdev_queue *queue,
1080 struct netdev_queue_attribute *attribute, char *buf) 1154 struct netdev_queue_attribute *attribute, char *buf)
1081{ 1155{
@@ -1152,6 +1226,7 @@ static struct attribute *netdev_queue_default_attrs[] = {
1152 &queue_trans_timeout.attr, 1226 &queue_trans_timeout.attr,
1153#ifdef CONFIG_XPS 1227#ifdef CONFIG_XPS
1154 &xps_cpus_attribute.attr, 1228 &xps_cpus_attribute.attr,
1229 &queue_tx_maxrate.attr,
1155#endif 1230#endif
1156 NULL 1231 NULL
1157}; 1232};
@@ -1374,6 +1449,30 @@ static struct class net_class = {
1374 .namespace = net_namespace, 1449 .namespace = net_namespace,
1375}; 1450};
1376 1451
1452#ifdef CONFIG_OF_NET
1453static int of_dev_node_match(struct device *dev, const void *data)
1454{
1455 int ret = 0;
1456
1457 if (dev->parent)
1458 ret = dev->parent->of_node == data;
1459
1460 return ret == 0 ? dev->of_node == data : ret;
1461}
1462
1463struct net_device *of_find_net_device_by_node(struct device_node *np)
1464{
1465 struct device *dev;
1466
1467 dev = class_find_device(&net_class, NULL, np, of_dev_node_match);
1468 if (!dev)
1469 return NULL;
1470
1471 return to_net_dev(dev);
1472}
1473EXPORT_SYMBOL(of_find_net_device_by_node);
1474#endif
1475
1377/* Delete sysfs entries but hold kobject reference until after all 1476/* Delete sysfs entries but hold kobject reference until after all
1378 * netdev references are gone. 1477 * netdev references are gone.
1379 */ 1478 */
diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c
index 70d3450588b2..78fc04ad36fc 100644
--- a/net/core/net_namespace.c
+++ b/net/core/net_namespace.c
@@ -16,7 +16,6 @@
16#include <linux/export.h> 16#include <linux/export.h>
17#include <linux/user_namespace.h> 17#include <linux/user_namespace.h>
18#include <linux/net_namespace.h> 18#include <linux/net_namespace.h>
19#include <linux/rtnetlink.h>
20#include <net/sock.h> 19#include <net/sock.h>
21#include <net/netlink.h> 20#include <net/netlink.h>
22#include <net/net_namespace.h> 21#include <net/net_namespace.h>
@@ -148,9 +147,11 @@ static void ops_free_list(const struct pernet_operations *ops,
148 } 147 }
149} 148}
150 149
150static void rtnl_net_notifyid(struct net *net, struct net *peer, int cmd,
151 int id);
151static int alloc_netid(struct net *net, struct net *peer, int reqid) 152static int alloc_netid(struct net *net, struct net *peer, int reqid)
152{ 153{
153 int min = 0, max = 0; 154 int min = 0, max = 0, id;
154 155
155 ASSERT_RTNL(); 156 ASSERT_RTNL();
156 157
@@ -159,7 +160,11 @@ static int alloc_netid(struct net *net, struct net *peer, int reqid)
159 max = reqid + 1; 160 max = reqid + 1;
160 } 161 }
161 162
162 return idr_alloc(&net->netns_ids, peer, min, max, GFP_KERNEL); 163 id = idr_alloc(&net->netns_ids, peer, min, max, GFP_KERNEL);
164 if (id >= 0)
165 rtnl_net_notifyid(net, peer, RTM_NEWNSID, id);
166
167 return id;
163} 168}
164 169
165/* This function is used by idr_for_each(). If net is equal to peer, the 170/* This function is used by idr_for_each(). If net is equal to peer, the
@@ -238,10 +243,6 @@ static __net_init int setup_net(struct net *net, struct user_namespace *user_ns)
238 net->user_ns = user_ns; 243 net->user_ns = user_ns;
239 idr_init(&net->netns_ids); 244 idr_init(&net->netns_ids);
240 245
241#ifdef NETNS_REFCNT_DEBUG
242 atomic_set(&net->use_count, 0);
243#endif
244
245 list_for_each_entry(ops, &pernet_list, list) { 246 list_for_each_entry(ops, &pernet_list, list) {
246 error = ops_init(ops, net); 247 error = ops_init(ops, net);
247 if (error < 0) 248 if (error < 0)
@@ -296,13 +297,6 @@ out_free:
296 297
297static void net_free(struct net *net) 298static void net_free(struct net *net)
298{ 299{
299#ifdef NETNS_REFCNT_DEBUG
300 if (unlikely(atomic_read(&net->use_count) != 0)) {
301 pr_emerg("network namespace not free! Usage: %d\n",
302 atomic_read(&net->use_count));
303 return;
304 }
305#endif
306 kfree(rcu_access_pointer(net->gen)); 300 kfree(rcu_access_pointer(net->gen));
307 kmem_cache_free(net_cachep, net); 301 kmem_cache_free(net_cachep, net);
308} 302}
@@ -370,8 +364,10 @@ static void cleanup_net(struct work_struct *work)
370 for_each_net(tmp) { 364 for_each_net(tmp) {
371 int id = __peernet2id(tmp, net, false); 365 int id = __peernet2id(tmp, net, false);
372 366
373 if (id >= 0) 367 if (id >= 0) {
368 rtnl_net_notifyid(tmp, net, RTM_DELNSID, id);
374 idr_remove(&tmp->netns_ids, id); 369 idr_remove(&tmp->netns_ids, id);
370 }
375 } 371 }
376 idr_destroy(&net->netns_ids); 372 idr_destroy(&net->netns_ids);
377 373
@@ -542,7 +538,8 @@ static int rtnl_net_get_size(void)
542} 538}
543 539
544static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags, 540static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
545 int cmd, struct net *net, struct net *peer) 541 int cmd, struct net *net, struct net *peer,
542 int nsid)
546{ 543{
547 struct nlmsghdr *nlh; 544 struct nlmsghdr *nlh;
548 struct rtgenmsg *rth; 545 struct rtgenmsg *rth;
@@ -557,9 +554,13 @@ static int rtnl_net_fill(struct sk_buff *skb, u32 portid, u32 seq, int flags,
557 rth = nlmsg_data(nlh); 554 rth = nlmsg_data(nlh);
558 rth->rtgen_family = AF_UNSPEC; 555 rth->rtgen_family = AF_UNSPEC;
559 556
560 id = __peernet2id(net, peer, false); 557 if (nsid >= 0) {
561 if (id < 0) 558 id = nsid;
562 id = NETNSA_NSID_NOT_ASSIGNED; 559 } else {
560 id = __peernet2id(net, peer, false);
561 if (id < 0)
562 id = NETNSA_NSID_NOT_ASSIGNED;
563 }
563 if (nla_put_s32(skb, NETNSA_NSID, id)) 564 if (nla_put_s32(skb, NETNSA_NSID, id))
564 goto nla_put_failure; 565 goto nla_put_failure;
565 566
@@ -576,8 +577,8 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)
576 struct net *net = sock_net(skb->sk); 577 struct net *net = sock_net(skb->sk);
577 struct nlattr *tb[NETNSA_MAX + 1]; 578 struct nlattr *tb[NETNSA_MAX + 1];
578 struct sk_buff *msg; 579 struct sk_buff *msg;
579 int err = -ENOBUFS;
580 struct net *peer; 580 struct net *peer;
581 int err;
581 582
582 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX, 583 err = nlmsg_parse(nlh, sizeof(struct rtgenmsg), tb, NETNSA_MAX,
583 rtnl_net_policy); 584 rtnl_net_policy);
@@ -600,7 +601,7 @@ static int rtnl_net_getid(struct sk_buff *skb, struct nlmsghdr *nlh)
600 } 601 }
601 602
602 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0, 603 err = rtnl_net_fill(msg, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
603 RTM_GETNSID, net, peer); 604 RTM_GETNSID, net, peer, -1);
604 if (err < 0) 605 if (err < 0)
605 goto err_out; 606 goto err_out;
606 607
@@ -614,6 +615,75 @@ out:
614 return err; 615 return err;
615} 616}
616 617
618struct rtnl_net_dump_cb {
619 struct net *net;
620 struct sk_buff *skb;
621 struct netlink_callback *cb;
622 int idx;
623 int s_idx;
624};
625
626static int rtnl_net_dumpid_one(int id, void *peer, void *data)
627{
628 struct rtnl_net_dump_cb *net_cb = (struct rtnl_net_dump_cb *)data;
629 int ret;
630
631 if (net_cb->idx < net_cb->s_idx)
632 goto cont;
633
634 ret = rtnl_net_fill(net_cb->skb, NETLINK_CB(net_cb->cb->skb).portid,
635 net_cb->cb->nlh->nlmsg_seq, NLM_F_MULTI,
636 RTM_NEWNSID, net_cb->net, peer, id);
637 if (ret < 0)
638 return ret;
639
640cont:
641 net_cb->idx++;
642 return 0;
643}
644
645static int rtnl_net_dumpid(struct sk_buff *skb, struct netlink_callback *cb)
646{
647 struct net *net = sock_net(skb->sk);
648 struct rtnl_net_dump_cb net_cb = {
649 .net = net,
650 .skb = skb,
651 .cb = cb,
652 .idx = 0,
653 .s_idx = cb->args[0],
654 };
655
656 ASSERT_RTNL();
657
658 idr_for_each(&net->netns_ids, rtnl_net_dumpid_one, &net_cb);
659
660 cb->args[0] = net_cb.idx;
661 return skb->len;
662}
663
664static void rtnl_net_notifyid(struct net *net, struct net *peer, int cmd,
665 int id)
666{
667 struct sk_buff *msg;
668 int err = -ENOMEM;
669
670 msg = nlmsg_new(rtnl_net_get_size(), GFP_KERNEL);
671 if (!msg)
672 goto out;
673
674 err = rtnl_net_fill(msg, 0, 0, 0, cmd, net, peer, id);
675 if (err < 0)
676 goto err_out;
677
678 rtnl_notify(msg, net, 0, RTNLGRP_NSID, NULL, 0);
679 return;
680
681err_out:
682 nlmsg_free(msg);
683out:
684 rtnl_set_sk_err(net, RTNLGRP_NSID, err);
685}
686
617static int __init net_ns_init(void) 687static int __init net_ns_init(void)
618{ 688{
619 struct net_generic *ng; 689 struct net_generic *ng;
@@ -648,7 +718,8 @@ static int __init net_ns_init(void)
648 register_pernet_subsys(&net_ns_ops); 718 register_pernet_subsys(&net_ns_ops);
649 719
650 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL, NULL); 720 rtnl_register(PF_UNSPEC, RTM_NEWNSID, rtnl_net_newid, NULL, NULL);
651 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, NULL, NULL); 721 rtnl_register(PF_UNSPEC, RTM_GETNSID, rtnl_net_getid, rtnl_net_dumpid,
722 NULL);
652 723
653 return 0; 724 return 0;
654} 725}
diff --git a/net/core/request_sock.c b/net/core/request_sock.c
index 04db318e6218..87b22c0bc08c 100644
--- a/net/core/request_sock.c
+++ b/net/core/request_sock.c
@@ -58,14 +58,14 @@ int reqsk_queue_alloc(struct request_sock_queue *queue,
58 return -ENOMEM; 58 return -ENOMEM;
59 59
60 get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd)); 60 get_random_bytes(&lopt->hash_rnd, sizeof(lopt->hash_rnd));
61 rwlock_init(&queue->syn_wait_lock); 61 spin_lock_init(&queue->syn_wait_lock);
62 queue->rskq_accept_head = NULL; 62 queue->rskq_accept_head = NULL;
63 lopt->nr_table_entries = nr_table_entries; 63 lopt->nr_table_entries = nr_table_entries;
64 lopt->max_qlen_log = ilog2(nr_table_entries); 64 lopt->max_qlen_log = ilog2(nr_table_entries);
65 65
66 write_lock_bh(&queue->syn_wait_lock); 66 spin_lock_bh(&queue->syn_wait_lock);
67 queue->listen_opt = lopt; 67 queue->listen_opt = lopt;
68 write_unlock_bh(&queue->syn_wait_lock); 68 spin_unlock_bh(&queue->syn_wait_lock);
69 69
70 return 0; 70 return 0;
71} 71}
@@ -81,10 +81,10 @@ static inline struct listen_sock *reqsk_queue_yank_listen_sk(
81{ 81{
82 struct listen_sock *lopt; 82 struct listen_sock *lopt;
83 83
84 write_lock_bh(&queue->syn_wait_lock); 84 spin_lock_bh(&queue->syn_wait_lock);
85 lopt = queue->listen_opt; 85 lopt = queue->listen_opt;
86 queue->listen_opt = NULL; 86 queue->listen_opt = NULL;
87 write_unlock_bh(&queue->syn_wait_lock); 87 spin_unlock_bh(&queue->syn_wait_lock);
88 88
89 return lopt; 89 return lopt;
90} 90}
@@ -94,21 +94,26 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)
94 /* make all the listen_opt local to us */ 94 /* make all the listen_opt local to us */
95 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue); 95 struct listen_sock *lopt = reqsk_queue_yank_listen_sk(queue);
96 96
97 if (lopt->qlen != 0) { 97 if (listen_sock_qlen(lopt) != 0) {
98 unsigned int i; 98 unsigned int i;
99 99
100 for (i = 0; i < lopt->nr_table_entries; i++) { 100 for (i = 0; i < lopt->nr_table_entries; i++) {
101 struct request_sock *req; 101 struct request_sock *req;
102 102
103 spin_lock_bh(&queue->syn_wait_lock);
103 while ((req = lopt->syn_table[i]) != NULL) { 104 while ((req = lopt->syn_table[i]) != NULL) {
104 lopt->syn_table[i] = req->dl_next; 105 lopt->syn_table[i] = req->dl_next;
105 lopt->qlen--; 106 atomic_inc(&lopt->qlen_dec);
106 reqsk_free(req); 107 if (del_timer(&req->rsk_timer))
108 reqsk_put(req);
109 reqsk_put(req);
107 } 110 }
111 spin_unlock_bh(&queue->syn_wait_lock);
108 } 112 }
109 } 113 }
110 114
111 WARN_ON(lopt->qlen != 0); 115 if (WARN_ON(listen_sock_qlen(lopt) != 0))
116 pr_err("qlen %u\n", listen_sock_qlen(lopt));
112 kvfree(lopt); 117 kvfree(lopt);
113} 118}
114 119
@@ -153,24 +158,22 @@ void reqsk_queue_destroy(struct request_sock_queue *queue)
153 * case might also exist in tcp_v4_hnd_req() that will trigger this locking 158 * case might also exist in tcp_v4_hnd_req() that will trigger this locking
154 * order. 159 * order.
155 * 160 *
156 * When a TFO req is created, it needs to sock_hold its listener to prevent 161 * This function also sets "treq->tfo_listener" to false.
157 * the latter data structure from going away. 162 * treq->tfo_listener is used by the listener so it is protected by the
158 *
159 * This function also sets "treq->listener" to NULL and unreference listener
160 * socket. treq->listener is used by the listener so it is protected by the
161 * fastopenq->lock in this function. 163 * fastopenq->lock in this function.
162 */ 164 */
163void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req, 165void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
164 bool reset) 166 bool reset)
165{ 167{
166 struct sock *lsk = tcp_rsk(req)->listener; 168 struct sock *lsk = req->rsk_listener;
167 struct fastopen_queue *fastopenq = 169 struct fastopen_queue *fastopenq;
168 inet_csk(lsk)->icsk_accept_queue.fastopenq; 170
171 fastopenq = inet_csk(lsk)->icsk_accept_queue.fastopenq;
169 172
170 tcp_sk(sk)->fastopen_rsk = NULL; 173 tcp_sk(sk)->fastopen_rsk = NULL;
171 spin_lock_bh(&fastopenq->lock); 174 spin_lock_bh(&fastopenq->lock);
172 fastopenq->qlen--; 175 fastopenq->qlen--;
173 tcp_rsk(req)->listener = NULL; 176 tcp_rsk(req)->tfo_listener = false;
174 if (req->sk) /* the child socket hasn't been accepted yet */ 177 if (req->sk) /* the child socket hasn't been accepted yet */
175 goto out; 178 goto out;
176 179
@@ -179,8 +182,7 @@ void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
179 * special RST handling below. 182 * special RST handling below.
180 */ 183 */
181 spin_unlock_bh(&fastopenq->lock); 184 spin_unlock_bh(&fastopenq->lock);
182 sock_put(lsk); 185 reqsk_put(req);
183 reqsk_free(req);
184 return; 186 return;
185 } 187 }
186 /* Wait for 60secs before removing a req that has triggered RST. 188 /* Wait for 60secs before removing a req that has triggered RST.
@@ -190,7 +192,7 @@ void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
190 * 192 *
191 * For more details see CoNext'11 "TCP Fast Open" paper. 193 * For more details see CoNext'11 "TCP Fast Open" paper.
192 */ 194 */
193 req->expires = jiffies + 60*HZ; 195 req->rsk_timer.expires = jiffies + 60*HZ;
194 if (fastopenq->rskq_rst_head == NULL) 196 if (fastopenq->rskq_rst_head == NULL)
195 fastopenq->rskq_rst_head = req; 197 fastopenq->rskq_rst_head = req;
196 else 198 else
@@ -201,5 +203,4 @@ void reqsk_fastopen_remove(struct sock *sk, struct request_sock *req,
201 fastopenq->qlen++; 203 fastopenq->qlen++;
202out: 204out:
203 spin_unlock_bh(&fastopenq->lock); 205 spin_unlock_bh(&fastopenq->lock);
204 sock_put(lsk);
205} 206}
diff --git a/net/core/rtnetlink.c b/net/core/rtnetlink.c
index 7ebed55b5f7d..358d52a38533 100644
--- a/net/core/rtnetlink.c
+++ b/net/core/rtnetlink.c
@@ -818,7 +818,8 @@ static inline int rtnl_vfinfo_size(const struct net_device *dev,
818 nla_total_size(sizeof(struct ifla_vf_vlan)) + 818 nla_total_size(sizeof(struct ifla_vf_vlan)) +
819 nla_total_size(sizeof(struct ifla_vf_spoofchk)) + 819 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
820 nla_total_size(sizeof(struct ifla_vf_rate)) + 820 nla_total_size(sizeof(struct ifla_vf_rate)) +
821 nla_total_size(sizeof(struct ifla_vf_link_state))); 821 nla_total_size(sizeof(struct ifla_vf_link_state)) +
822 nla_total_size(sizeof(struct ifla_vf_rss_query_en)));
822 return size; 823 return size;
823 } else 824 } else
824 return 0; 825 return 0;
@@ -982,6 +983,24 @@ static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
982 return 0; 983 return 0;
983} 984}
984 985
986static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
987{
988 char name[IFNAMSIZ];
989 int err;
990
991 err = dev_get_phys_port_name(dev, name, sizeof(name));
992 if (err) {
993 if (err == -EOPNOTSUPP)
994 return 0;
995 return err;
996 }
997
998 if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
999 return -EMSGSIZE;
1000
1001 return 0;
1002}
1003
985static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev) 1004static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
986{ 1005{
987 int err; 1006 int err;
@@ -1037,8 +1056,8 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1037#ifdef CONFIG_RPS 1056#ifdef CONFIG_RPS
1038 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) || 1057 nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1039#endif 1058#endif
1040 (dev->ifindex != dev->iflink && 1059 (dev->ifindex != dev_get_iflink(dev) &&
1041 nla_put_u32(skb, IFLA_LINK, dev->iflink)) || 1060 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1042 (upper_dev && 1061 (upper_dev &&
1043 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) || 1062 nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1044 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) || 1063 nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
@@ -1072,6 +1091,9 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1072 if (rtnl_phys_port_id_fill(skb, dev)) 1091 if (rtnl_phys_port_id_fill(skb, dev))
1073 goto nla_put_failure; 1092 goto nla_put_failure;
1074 1093
1094 if (rtnl_phys_port_name_fill(skb, dev))
1095 goto nla_put_failure;
1096
1075 if (rtnl_phys_switch_id_fill(skb, dev)) 1097 if (rtnl_phys_switch_id_fill(skb, dev))
1076 goto nla_put_failure; 1098 goto nla_put_failure;
1077 1099
@@ -1111,14 +1133,16 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1111 struct ifla_vf_tx_rate vf_tx_rate; 1133 struct ifla_vf_tx_rate vf_tx_rate;
1112 struct ifla_vf_spoofchk vf_spoofchk; 1134 struct ifla_vf_spoofchk vf_spoofchk;
1113 struct ifla_vf_link_state vf_linkstate; 1135 struct ifla_vf_link_state vf_linkstate;
1136 struct ifla_vf_rss_query_en vf_rss_query_en;
1114 1137
1115 /* 1138 /*
1116 * Not all SR-IOV capable drivers support the 1139 * Not all SR-IOV capable drivers support the
1117 * spoofcheck query. Preset to -1 so the user 1140 * spoofcheck and "RSS query enable" query. Preset to
1118 * space tool can detect that the driver didn't 1141 * -1 so the user space tool can detect that the driver
1119 * report anything. 1142 * didn't report anything.
1120 */ 1143 */
1121 ivi.spoofchk = -1; 1144 ivi.spoofchk = -1;
1145 ivi.rss_query_en = -1;
1122 memset(ivi.mac, 0, sizeof(ivi.mac)); 1146 memset(ivi.mac, 0, sizeof(ivi.mac));
1123 /* The default value for VF link state is "auto" 1147 /* The default value for VF link state is "auto"
1124 * IFLA_VF_LINK_STATE_AUTO which equals zero 1148 * IFLA_VF_LINK_STATE_AUTO which equals zero
@@ -1131,7 +1155,8 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1131 vf_rate.vf = 1155 vf_rate.vf =
1132 vf_tx_rate.vf = 1156 vf_tx_rate.vf =
1133 vf_spoofchk.vf = 1157 vf_spoofchk.vf =
1134 vf_linkstate.vf = ivi.vf; 1158 vf_linkstate.vf =
1159 vf_rss_query_en.vf = ivi.vf;
1135 1160
1136 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac)); 1161 memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1137 vf_vlan.vlan = ivi.vlan; 1162 vf_vlan.vlan = ivi.vlan;
@@ -1141,6 +1166,7 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1141 vf_rate.max_tx_rate = ivi.max_tx_rate; 1166 vf_rate.max_tx_rate = ivi.max_tx_rate;
1142 vf_spoofchk.setting = ivi.spoofchk; 1167 vf_spoofchk.setting = ivi.spoofchk;
1143 vf_linkstate.link_state = ivi.linkstate; 1168 vf_linkstate.link_state = ivi.linkstate;
1169 vf_rss_query_en.setting = ivi.rss_query_en;
1144 vf = nla_nest_start(skb, IFLA_VF_INFO); 1170 vf = nla_nest_start(skb, IFLA_VF_INFO);
1145 if (!vf) { 1171 if (!vf) {
1146 nla_nest_cancel(skb, vfinfo); 1172 nla_nest_cancel(skb, vfinfo);
@@ -1155,7 +1181,10 @@ static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1155 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk), 1181 nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1156 &vf_spoofchk) || 1182 &vf_spoofchk) ||
1157 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate), 1183 nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1158 &vf_linkstate)) 1184 &vf_linkstate) ||
1185 nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1186 sizeof(vf_rss_query_en),
1187 &vf_rss_query_en))
1159 goto nla_put_failure; 1188 goto nla_put_failure;
1160 nla_nest_end(skb, vf); 1189 nla_nest_end(skb, vf);
1161 } 1190 }
@@ -1269,6 +1298,7 @@ static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1269 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) }, 1298 [IFLA_VF_SPOOFCHK] = { .len = sizeof(struct ifla_vf_spoofchk) },
1270 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) }, 1299 [IFLA_VF_RATE] = { .len = sizeof(struct ifla_vf_rate) },
1271 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) }, 1300 [IFLA_VF_LINK_STATE] = { .len = sizeof(struct ifla_vf_link_state) },
1301 [IFLA_VF_RSS_QUERY_EN] = { .len = sizeof(struct ifla_vf_rss_query_en) },
1272}; 1302};
1273 1303
1274static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = { 1304static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
@@ -1479,6 +1509,17 @@ static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1479 ivl->link_state); 1509 ivl->link_state);
1480 break; 1510 break;
1481 } 1511 }
1512 case IFLA_VF_RSS_QUERY_EN: {
1513 struct ifla_vf_rss_query_en *ivrssq_en;
1514
1515 ivrssq_en = nla_data(vf);
1516 err = -EOPNOTSUPP;
1517 if (ops->ndo_set_vf_rss_query_en)
1518 err = ops->ndo_set_vf_rss_query_en(dev,
1519 ivrssq_en->vf,
1520 ivrssq_en->setting);
1521 break;
1522 }
1482 default: 1523 default:
1483 err = -EINVAL; 1524 err = -EINVAL;
1484 break; 1525 break;
@@ -1815,6 +1856,42 @@ errout:
1815 return err; 1856 return err;
1816} 1857}
1817 1858
1859static int rtnl_group_dellink(const struct net *net, int group)
1860{
1861 struct net_device *dev, *aux;
1862 LIST_HEAD(list_kill);
1863 bool found = false;
1864
1865 if (!group)
1866 return -EPERM;
1867
1868 for_each_netdev(net, dev) {
1869 if (dev->group == group) {
1870 const struct rtnl_link_ops *ops;
1871
1872 found = true;
1873 ops = dev->rtnl_link_ops;
1874 if (!ops || !ops->dellink)
1875 return -EOPNOTSUPP;
1876 }
1877 }
1878
1879 if (!found)
1880 return -ENODEV;
1881
1882 for_each_netdev_safe(net, dev, aux) {
1883 if (dev->group == group) {
1884 const struct rtnl_link_ops *ops;
1885
1886 ops = dev->rtnl_link_ops;
1887 ops->dellink(dev, &list_kill);
1888 }
1889 }
1890 unregister_netdevice_many(&list_kill);
1891
1892 return 0;
1893}
1894
1818static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh) 1895static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
1819{ 1896{
1820 struct net *net = sock_net(skb->sk); 1897 struct net *net = sock_net(skb->sk);
@@ -1838,6 +1915,8 @@ static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
1838 dev = __dev_get_by_index(net, ifm->ifi_index); 1915 dev = __dev_get_by_index(net, ifm->ifi_index);
1839 else if (tb[IFLA_IFNAME]) 1916 else if (tb[IFLA_IFNAME])
1840 dev = __dev_get_by_name(net, ifname); 1917 dev = __dev_get_by_name(net, ifname);
1918 else if (tb[IFLA_GROUP])
1919 return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
1841 else 1920 else
1842 return -EINVAL; 1921 return -EINVAL;
1843 1922
@@ -1873,7 +1952,7 @@ int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1873EXPORT_SYMBOL(rtnl_configure_link); 1952EXPORT_SYMBOL(rtnl_configure_link);
1874 1953
1875struct net_device *rtnl_create_link(struct net *net, 1954struct net_device *rtnl_create_link(struct net *net,
1876 char *ifname, unsigned char name_assign_type, 1955 const char *ifname, unsigned char name_assign_type,
1877 const struct rtnl_link_ops *ops, struct nlattr *tb[]) 1956 const struct rtnl_link_ops *ops, struct nlattr *tb[])
1878{ 1957{
1879 int err; 1958 int err;
@@ -2345,7 +2424,7 @@ EXPORT_SYMBOL(rtmsg_ifinfo);
2345 2424
2346static int nlmsg_populate_fdb_fill(struct sk_buff *skb, 2425static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2347 struct net_device *dev, 2426 struct net_device *dev,
2348 u8 *addr, u32 pid, u32 seq, 2427 u8 *addr, u16 vid, u32 pid, u32 seq,
2349 int type, unsigned int flags, 2428 int type, unsigned int flags,
2350 int nlflags) 2429 int nlflags)
2351{ 2430{
@@ -2367,6 +2446,9 @@ static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2367 2446
2368 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr)) 2447 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2369 goto nla_put_failure; 2448 goto nla_put_failure;
2449 if (vid)
2450 if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2451 goto nla_put_failure;
2370 2452
2371 nlmsg_end(skb, nlh); 2453 nlmsg_end(skb, nlh);
2372 return 0; 2454 return 0;
@@ -2381,7 +2463,7 @@ static inline size_t rtnl_fdb_nlmsg_size(void)
2381 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN); 2463 return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2382} 2464}
2383 2465
2384static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type) 2466static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
2385{ 2467{
2386 struct net *net = dev_net(dev); 2468 struct net *net = dev_net(dev);
2387 struct sk_buff *skb; 2469 struct sk_buff *skb;
@@ -2391,7 +2473,8 @@ static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, int type)
2391 if (!skb) 2473 if (!skb)
2392 goto errout; 2474 goto errout;
2393 2475
2394 err = nlmsg_populate_fdb_fill(skb, dev, addr, 0, 0, type, NTF_SELF, 0); 2476 err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2477 0, 0, type, NTF_SELF, 0);
2395 if (err < 0) { 2478 if (err < 0) {
2396 kfree_skb(skb); 2479 kfree_skb(skb);
2397 goto errout; 2480 goto errout;
@@ -2526,7 +2609,7 @@ static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2526 nlh->nlmsg_flags); 2609 nlh->nlmsg_flags);
2527 2610
2528 if (!err) { 2611 if (!err) {
2529 rtnl_fdb_notify(dev, addr, RTM_NEWNEIGH); 2612 rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
2530 ndm->ndm_flags &= ~NTF_SELF; 2613 ndm->ndm_flags &= ~NTF_SELF;
2531 } 2614 }
2532 } 2615 }
@@ -2627,7 +2710,7 @@ static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2627 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid); 2710 err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2628 2711
2629 if (!err) { 2712 if (!err) {
2630 rtnl_fdb_notify(dev, addr, RTM_DELNEIGH); 2713 rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
2631 ndm->ndm_flags &= ~NTF_SELF; 2714 ndm->ndm_flags &= ~NTF_SELF;
2632 } 2715 }
2633 } 2716 }
@@ -2652,7 +2735,7 @@ static int nlmsg_populate_fdb(struct sk_buff *skb,
2652 if (*idx < cb->args[0]) 2735 if (*idx < cb->args[0])
2653 goto skip; 2736 goto skip;
2654 2737
2655 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 2738 err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2656 portid, seq, 2739 portid, seq,
2657 RTM_NEWNEIGH, NTF_SELF, 2740 RTM_NEWNEIGH, NTF_SELF,
2658 NLM_F_MULTI); 2741 NLM_F_MULTI);
@@ -2695,7 +2778,6 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2695{ 2778{
2696 struct net_device *dev; 2779 struct net_device *dev;
2697 struct nlattr *tb[IFLA_MAX+1]; 2780 struct nlattr *tb[IFLA_MAX+1];
2698 struct net_device *bdev = NULL;
2699 struct net_device *br_dev = NULL; 2781 struct net_device *br_dev = NULL;
2700 const struct net_device_ops *ops = NULL; 2782 const struct net_device_ops *ops = NULL;
2701 const struct net_device_ops *cops = NULL; 2783 const struct net_device_ops *cops = NULL;
@@ -2719,7 +2801,6 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2719 return -ENODEV; 2801 return -ENODEV;
2720 2802
2721 ops = br_dev->netdev_ops; 2803 ops = br_dev->netdev_ops;
2722 bdev = br_dev;
2723 } 2804 }
2724 2805
2725 for_each_netdev(net, dev) { 2806 for_each_netdev(net, dev) {
@@ -2732,7 +2813,6 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2732 cops = br_dev->netdev_ops; 2813 cops = br_dev->netdev_ops;
2733 } 2814 }
2734 2815
2735 bdev = dev;
2736 } else { 2816 } else {
2737 if (dev != br_dev && 2817 if (dev != br_dev &&
2738 !(dev->priv_flags & IFF_BRIDGE_PORT)) 2818 !(dev->priv_flags & IFF_BRIDGE_PORT))
@@ -2742,7 +2822,6 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2742 !(dev->priv_flags & IFF_EBRIDGE)) 2822 !(dev->priv_flags & IFF_EBRIDGE))
2743 continue; 2823 continue;
2744 2824
2745 bdev = br_dev;
2746 cops = ops; 2825 cops = ops;
2747 } 2826 }
2748 2827
@@ -2804,8 +2883,8 @@ int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2804 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) || 2883 nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
2805 (dev->addr_len && 2884 (dev->addr_len &&
2806 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 2885 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
2807 (dev->ifindex != dev->iflink && 2886 (dev->ifindex != dev_get_iflink(dev) &&
2808 nla_put_u32(skb, IFLA_LINK, dev->iflink))) 2887 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
2809 goto nla_put_failure; 2888 goto nla_put_failure;
2810 2889
2811 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC); 2890 br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 8e4ac97c8477..d1967dab9cc6 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -2865,7 +2865,6 @@ static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2865 * @from: search offset 2865 * @from: search offset
2866 * @to: search limit 2866 * @to: search limit
2867 * @config: textsearch configuration 2867 * @config: textsearch configuration
2868 * @state: uninitialized textsearch state variable
2869 * 2868 *
2870 * Finds a pattern in the skb data according to the specified 2869 * Finds a pattern in the skb data according to the specified
2871 * textsearch configuration. Use textsearch_next() to retrieve 2870 * textsearch configuration. Use textsearch_next() to retrieve
@@ -2873,17 +2872,17 @@ static void skb_ts_finish(struct ts_config *conf, struct ts_state *state)
2873 * to the first occurrence or UINT_MAX if no match was found. 2872 * to the first occurrence or UINT_MAX if no match was found.
2874 */ 2873 */
2875unsigned int skb_find_text(struct sk_buff *skb, unsigned int from, 2874unsigned int skb_find_text(struct sk_buff *skb, unsigned int from,
2876 unsigned int to, struct ts_config *config, 2875 unsigned int to, struct ts_config *config)
2877 struct ts_state *state)
2878{ 2876{
2877 struct ts_state state;
2879 unsigned int ret; 2878 unsigned int ret;
2880 2879
2881 config->get_next_block = skb_ts_get_next_block; 2880 config->get_next_block = skb_ts_get_next_block;
2882 config->finish = skb_ts_finish; 2881 config->finish = skb_ts_finish;
2883 2882
2884 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(state)); 2883 skb_prepare_seq_read(skb, from, to, TS_SKB_CB(&state));
2885 2884
2886 ret = textsearch_find(config, state); 2885 ret = textsearch_find(config, &state);
2887 return (ret <= to - from ? ret : UINT_MAX); 2886 return (ret <= to - from ? ret : UINT_MAX);
2888} 2887}
2889EXPORT_SYMBOL(skb_find_text); 2888EXPORT_SYMBOL(skb_find_text);
@@ -3207,10 +3206,9 @@ int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3207 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb); 3206 struct skb_shared_info *pinfo, *skbinfo = skb_shinfo(skb);
3208 unsigned int offset = skb_gro_offset(skb); 3207 unsigned int offset = skb_gro_offset(skb);
3209 unsigned int headlen = skb_headlen(skb); 3208 unsigned int headlen = skb_headlen(skb);
3210 struct sk_buff *nskb, *lp, *p = *head;
3211 unsigned int len = skb_gro_len(skb); 3209 unsigned int len = skb_gro_len(skb);
3210 struct sk_buff *lp, *p = *head;
3212 unsigned int delta_truesize; 3211 unsigned int delta_truesize;
3213 unsigned int headroom;
3214 3212
3215 if (unlikely(p->len + len >= 65536)) 3213 if (unlikely(p->len + len >= 65536))
3216 return -E2BIG; 3214 return -E2BIG;
@@ -3277,48 +3275,6 @@ int skb_gro_receive(struct sk_buff **head, struct sk_buff *skb)
3277 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD; 3275 NAPI_GRO_CB(skb)->free = NAPI_GRO_FREE_STOLEN_HEAD;
3278 goto done; 3276 goto done;
3279 } 3277 }
3280 /* switch back to head shinfo */
3281 pinfo = skb_shinfo(p);
3282
3283 if (pinfo->frag_list)
3284 goto merge;
3285 if (skb_gro_len(p) != pinfo->gso_size)
3286 return -E2BIG;
3287
3288 headroom = skb_headroom(p);
3289 nskb = alloc_skb(headroom + skb_gro_offset(p), GFP_ATOMIC);
3290 if (unlikely(!nskb))
3291 return -ENOMEM;
3292
3293 __copy_skb_header(nskb, p);
3294 nskb->mac_len = p->mac_len;
3295
3296 skb_reserve(nskb, headroom);
3297 __skb_put(nskb, skb_gro_offset(p));
3298
3299 skb_set_mac_header(nskb, skb_mac_header(p) - p->data);
3300 skb_set_network_header(nskb, skb_network_offset(p));
3301 skb_set_transport_header(nskb, skb_transport_offset(p));
3302
3303 __skb_pull(p, skb_gro_offset(p));
3304 memcpy(skb_mac_header(nskb), skb_mac_header(p),
3305 p->data - skb_mac_header(p));
3306
3307 skb_shinfo(nskb)->frag_list = p;
3308 skb_shinfo(nskb)->gso_size = pinfo->gso_size;
3309 pinfo->gso_size = 0;
3310 __skb_header_release(p);
3311 NAPI_GRO_CB(nskb)->last = p;
3312
3313 nskb->data_len += p->len;
3314 nskb->truesize += p->truesize;
3315 nskb->len += p->len;
3316
3317 *head = nskb;
3318 nskb->next = p->next;
3319 p->next = NULL;
3320
3321 p = nskb;
3322 3278
3323merge: 3279merge:
3324 delta_truesize = skb->truesize; 3280 delta_truesize = skb->truesize;
@@ -3796,7 +3752,6 @@ void skb_complete_wifi_ack(struct sk_buff *skb, bool acked)
3796} 3752}
3797EXPORT_SYMBOL_GPL(skb_complete_wifi_ack); 3753EXPORT_SYMBOL_GPL(skb_complete_wifi_ack);
3798 3754
3799
3800/** 3755/**
3801 * skb_partial_csum_set - set up and verify partial csum values for packet 3756 * skb_partial_csum_set - set up and verify partial csum values for packet
3802 * @skb: the skb to set 3757 * @skb: the skb to set
@@ -4169,19 +4124,21 @@ EXPORT_SYMBOL(skb_try_coalesce);
4169 */ 4124 */
4170void skb_scrub_packet(struct sk_buff *skb, bool xnet) 4125void skb_scrub_packet(struct sk_buff *skb, bool xnet)
4171{ 4126{
4172 if (xnet)
4173 skb_orphan(skb);
4174 skb->tstamp.tv64 = 0; 4127 skb->tstamp.tv64 = 0;
4175 skb->pkt_type = PACKET_HOST; 4128 skb->pkt_type = PACKET_HOST;
4176 skb->skb_iif = 0; 4129 skb->skb_iif = 0;
4177 skb->ignore_df = 0; 4130 skb->ignore_df = 0;
4178 skb_dst_drop(skb); 4131 skb_dst_drop(skb);
4179 skb->mark = 0;
4180 skb_sender_cpu_clear(skb); 4132 skb_sender_cpu_clear(skb);
4181 skb_init_secmark(skb);
4182 secpath_reset(skb); 4133 secpath_reset(skb);
4183 nf_reset(skb); 4134 nf_reset(skb);
4184 nf_reset_trace(skb); 4135 nf_reset_trace(skb);
4136
4137 if (!xnet)
4138 return;
4139
4140 skb_orphan(skb);
4141 skb->mark = 0;
4185} 4142}
4186EXPORT_SYMBOL_GPL(skb_scrub_packet); 4143EXPORT_SYMBOL_GPL(skb_scrub_packet);
4187 4144
diff --git a/net/core/sock.c b/net/core/sock.c
index 71e3e5f1eaa0..e891bcf325ca 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -466,7 +466,7 @@ int sock_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
466 skb_dst_force(skb); 466 skb_dst_force(skb);
467 467
468 spin_lock_irqsave(&list->lock, flags); 468 spin_lock_irqsave(&list->lock, flags);
469 skb->dropcount = atomic_read(&sk->sk_drops); 469 sock_skb_set_dropcount(sk, skb);
470 __skb_queue_tail(list, skb); 470 __skb_queue_tail(list, skb);
471 spin_unlock_irqrestore(&list->lock, flags); 471 spin_unlock_irqrestore(&list->lock, flags);
472 472
@@ -947,8 +947,6 @@ set_rcvbuf:
947 sk->sk_mark = val; 947 sk->sk_mark = val;
948 break; 948 break;
949 949
950 /* We implement the SO_SNDLOWAT etc to
951 not be settable (1003.1g 5.3) */
952 case SO_RXQ_OVFL: 950 case SO_RXQ_OVFL:
953 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool); 951 sock_valbool_flag(sk, SOCK_RXQ_OVFL, valbool);
954 break; 952 break;
@@ -1253,6 +1251,9 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
1253 break; 1251 break;
1254 1252
1255 default: 1253 default:
1254 /* We implement the SO_SNDLOWAT etc to not be settable
1255 * (1003.1g 7).
1256 */
1256 return -ENOPROTOOPT; 1257 return -ENOPROTOOPT;
1257 } 1258 }
1258 1259
@@ -1473,9 +1474,8 @@ void sk_release_kernel(struct sock *sk)
1473 return; 1474 return;
1474 1475
1475 sock_hold(sk); 1476 sock_hold(sk);
1476 sock_release(sk->sk_socket);
1477 release_net(sock_net(sk));
1478 sock_net_set(sk, get_net(&init_net)); 1477 sock_net_set(sk, get_net(&init_net));
1478 sock_release(sk->sk_socket);
1479 sock_put(sk); 1479 sock_put(sk);
1480} 1480}
1481EXPORT_SYMBOL(sk_release_kernel); 1481EXPORT_SYMBOL(sk_release_kernel);
@@ -1557,6 +1557,7 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
1557 newsk->sk_err = 0; 1557 newsk->sk_err = 0;
1558 newsk->sk_priority = 0; 1558 newsk->sk_priority = 0;
1559 newsk->sk_incoming_cpu = raw_smp_processor_id(); 1559 newsk->sk_incoming_cpu = raw_smp_processor_id();
1560 atomic64_set(&newsk->sk_cookie, 0);
1560 /* 1561 /*
1561 * Before updating sk_refcnt, we must commit prior changes to memory 1562 * Before updating sk_refcnt, we must commit prior changes to memory
1562 * (Documentation/RCU/rculist_nulls.txt for details) 1563 * (Documentation/RCU/rculist_nulls.txt for details)
@@ -1684,19 +1685,6 @@ void sock_efree(struct sk_buff *skb)
1684} 1685}
1685EXPORT_SYMBOL(sock_efree); 1686EXPORT_SYMBOL(sock_efree);
1686 1687
1687#ifdef CONFIG_INET
1688void sock_edemux(struct sk_buff *skb)
1689{
1690 struct sock *sk = skb->sk;
1691
1692 if (sk->sk_state == TCP_TIME_WAIT)
1693 inet_twsk_put(inet_twsk(sk));
1694 else
1695 sock_put(sk);
1696}
1697EXPORT_SYMBOL(sock_edemux);
1698#endif
1699
1700kuid_t sock_i_uid(struct sock *sk) 1688kuid_t sock_i_uid(struct sock *sk)
1701{ 1689{
1702 kuid_t uid; 1690 kuid_t uid;
@@ -2186,15 +2174,14 @@ int sock_no_getsockopt(struct socket *sock, int level, int optname,
2186} 2174}
2187EXPORT_SYMBOL(sock_no_getsockopt); 2175EXPORT_SYMBOL(sock_no_getsockopt);
2188 2176
2189int sock_no_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 2177int sock_no_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
2190 size_t len)
2191{ 2178{
2192 return -EOPNOTSUPP; 2179 return -EOPNOTSUPP;
2193} 2180}
2194EXPORT_SYMBOL(sock_no_sendmsg); 2181EXPORT_SYMBOL(sock_no_sendmsg);
2195 2182
2196int sock_no_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 2183int sock_no_recvmsg(struct socket *sock, struct msghdr *m, size_t len,
2197 size_t len, int flags) 2184 int flags)
2198{ 2185{
2199 return -EOPNOTSUPP; 2186 return -EOPNOTSUPP;
2200} 2187}
@@ -2566,14 +2553,14 @@ int compat_sock_common_getsockopt(struct socket *sock, int level, int optname,
2566EXPORT_SYMBOL(compat_sock_common_getsockopt); 2553EXPORT_SYMBOL(compat_sock_common_getsockopt);
2567#endif 2554#endif
2568 2555
2569int sock_common_recvmsg(struct kiocb *iocb, struct socket *sock, 2556int sock_common_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
2570 struct msghdr *msg, size_t size, int flags) 2557 int flags)
2571{ 2558{
2572 struct sock *sk = sock->sk; 2559 struct sock *sk = sock->sk;
2573 int addr_len = 0; 2560 int addr_len = 0;
2574 int err; 2561 int err;
2575 2562
2576 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT, 2563 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
2577 flags & ~MSG_DONTWAIT, &addr_len); 2564 flags & ~MSG_DONTWAIT, &addr_len);
2578 if (err >= 0) 2565 if (err >= 0)
2579 msg->msg_namelen = addr_len; 2566 msg->msg_namelen = addr_len;
@@ -2750,6 +2737,42 @@ static inline void release_proto_idx(struct proto *prot)
2750} 2737}
2751#endif 2738#endif
2752 2739
2740static void req_prot_cleanup(struct request_sock_ops *rsk_prot)
2741{
2742 if (!rsk_prot)
2743 return;
2744 kfree(rsk_prot->slab_name);
2745 rsk_prot->slab_name = NULL;
2746 if (rsk_prot->slab) {
2747 kmem_cache_destroy(rsk_prot->slab);
2748 rsk_prot->slab = NULL;
2749 }
2750}
2751
2752static int req_prot_init(const struct proto *prot)
2753{
2754 struct request_sock_ops *rsk_prot = prot->rsk_prot;
2755
2756 if (!rsk_prot)
2757 return 0;
2758
2759 rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s",
2760 prot->name);
2761 if (!rsk_prot->slab_name)
2762 return -ENOMEM;
2763
2764 rsk_prot->slab = kmem_cache_create(rsk_prot->slab_name,
2765 rsk_prot->obj_size, 0,
2766 0, NULL);
2767
2768 if (!rsk_prot->slab) {
2769 pr_crit("%s: Can't create request sock SLAB cache!\n",
2770 prot->name);
2771 return -ENOMEM;
2772 }
2773 return 0;
2774}
2775
2753int proto_register(struct proto *prot, int alloc_slab) 2776int proto_register(struct proto *prot, int alloc_slab)
2754{ 2777{
2755 if (alloc_slab) { 2778 if (alloc_slab) {
@@ -2763,21 +2786,8 @@ int proto_register(struct proto *prot, int alloc_slab)
2763 goto out; 2786 goto out;
2764 } 2787 }
2765 2788
2766 if (prot->rsk_prot != NULL) { 2789 if (req_prot_init(prot))
2767 prot->rsk_prot->slab_name = kasprintf(GFP_KERNEL, "request_sock_%s", prot->name); 2790 goto out_free_request_sock_slab;
2768 if (prot->rsk_prot->slab_name == NULL)
2769 goto out_free_sock_slab;
2770
2771 prot->rsk_prot->slab = kmem_cache_create(prot->rsk_prot->slab_name,
2772 prot->rsk_prot->obj_size, 0,
2773 SLAB_HWCACHE_ALIGN, NULL);
2774
2775 if (prot->rsk_prot->slab == NULL) {
2776 pr_crit("%s: Can't create request sock SLAB cache!\n",
2777 prot->name);
2778 goto out_free_request_sock_slab_name;
2779 }
2780 }
2781 2791
2782 if (prot->twsk_prot != NULL) { 2792 if (prot->twsk_prot != NULL) {
2783 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name); 2793 prot->twsk_prot->twsk_slab_name = kasprintf(GFP_KERNEL, "tw_sock_%s", prot->name);
@@ -2789,8 +2799,7 @@ int proto_register(struct proto *prot, int alloc_slab)
2789 kmem_cache_create(prot->twsk_prot->twsk_slab_name, 2799 kmem_cache_create(prot->twsk_prot->twsk_slab_name,
2790 prot->twsk_prot->twsk_obj_size, 2800 prot->twsk_prot->twsk_obj_size,
2791 0, 2801 0,
2792 SLAB_HWCACHE_ALIGN | 2802 prot->slab_flags,
2793 prot->slab_flags,
2794 NULL); 2803 NULL);
2795 if (prot->twsk_prot->twsk_slab == NULL) 2804 if (prot->twsk_prot->twsk_slab == NULL)
2796 goto out_free_timewait_sock_slab_name; 2805 goto out_free_timewait_sock_slab_name;
@@ -2806,14 +2815,8 @@ int proto_register(struct proto *prot, int alloc_slab)
2806out_free_timewait_sock_slab_name: 2815out_free_timewait_sock_slab_name:
2807 kfree(prot->twsk_prot->twsk_slab_name); 2816 kfree(prot->twsk_prot->twsk_slab_name);
2808out_free_request_sock_slab: 2817out_free_request_sock_slab:
2809 if (prot->rsk_prot && prot->rsk_prot->slab) { 2818 req_prot_cleanup(prot->rsk_prot);
2810 kmem_cache_destroy(prot->rsk_prot->slab); 2819
2811 prot->rsk_prot->slab = NULL;
2812 }
2813out_free_request_sock_slab_name:
2814 if (prot->rsk_prot)
2815 kfree(prot->rsk_prot->slab_name);
2816out_free_sock_slab:
2817 kmem_cache_destroy(prot->slab); 2820 kmem_cache_destroy(prot->slab);
2818 prot->slab = NULL; 2821 prot->slab = NULL;
2819out: 2822out:
@@ -2833,11 +2836,7 @@ void proto_unregister(struct proto *prot)
2833 prot->slab = NULL; 2836 prot->slab = NULL;
2834 } 2837 }
2835 2838
2836 if (prot->rsk_prot != NULL && prot->rsk_prot->slab != NULL) { 2839 req_prot_cleanup(prot->rsk_prot);
2837 kmem_cache_destroy(prot->rsk_prot->slab);
2838 kfree(prot->rsk_prot->slab_name);
2839 prot->rsk_prot->slab = NULL;
2840 }
2841 2840
2842 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) { 2841 if (prot->twsk_prot != NULL && prot->twsk_prot->twsk_slab != NULL) {
2843 kmem_cache_destroy(prot->twsk_prot->twsk_slab); 2842 kmem_cache_destroy(prot->twsk_prot->twsk_slab);
diff --git a/net/core/sock_diag.c b/net/core/sock_diag.c
index ad704c757bb4..74dddf84adcd 100644
--- a/net/core/sock_diag.c
+++ b/net/core/sock_diag.c
@@ -13,22 +13,39 @@ static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
13static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); 13static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
14static DEFINE_MUTEX(sock_diag_table_mutex); 14static DEFINE_MUTEX(sock_diag_table_mutex);
15 15
16int sock_diag_check_cookie(void *sk, __u32 *cookie) 16static u64 sock_gen_cookie(struct sock *sk)
17{ 17{
18 if ((cookie[0] != INET_DIAG_NOCOOKIE || 18 while (1) {
19 cookie[1] != INET_DIAG_NOCOOKIE) && 19 u64 res = atomic64_read(&sk->sk_cookie);
20 ((u32)(unsigned long)sk != cookie[0] || 20
21 (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1])) 21 if (res)
22 return -ESTALE; 22 return res;
23 else 23 res = atomic64_inc_return(&sock_net(sk)->cookie_gen);
24 atomic64_cmpxchg(&sk->sk_cookie, 0, res);
25 }
26}
27
28int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
29{
30 u64 res;
31
32 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
24 return 0; 33 return 0;
34
35 res = sock_gen_cookie(sk);
36 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
37 return -ESTALE;
38
39 return 0;
25} 40}
26EXPORT_SYMBOL_GPL(sock_diag_check_cookie); 41EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
27 42
28void sock_diag_save_cookie(void *sk, __u32 *cookie) 43void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
29{ 44{
30 cookie[0] = (u32)(unsigned long)sk; 45 u64 res = sock_gen_cookie(sk);
31 cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1); 46
47 cookie[0] = (u32)res;
48 cookie[1] = (u32)(res >> 32);
32} 49}
33EXPORT_SYMBOL_GPL(sock_diag_save_cookie); 50EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
34 51
diff --git a/net/core/sysctl_net_core.c b/net/core/sysctl_net_core.c
index 8ce351ffceb1..95b6139d710c 100644
--- a/net/core/sysctl_net_core.c
+++ b/net/core/sysctl_net_core.c
@@ -24,7 +24,6 @@
24 24
25static int zero = 0; 25static int zero = 0;
26static int one = 1; 26static int one = 1;
27static int ushort_max = USHRT_MAX;
28static int min_sndbuf = SOCK_MIN_SNDBUF; 27static int min_sndbuf = SOCK_MIN_SNDBUF;
29static int min_rcvbuf = SOCK_MIN_RCVBUF; 28static int min_rcvbuf = SOCK_MIN_RCVBUF;
30 29
@@ -403,7 +402,6 @@ static struct ctl_table netns_core_table[] = {
403 .maxlen = sizeof(int), 402 .maxlen = sizeof(int),
404 .mode = 0644, 403 .mode = 0644,
405 .extra1 = &zero, 404 .extra1 = &zero,
406 .extra2 = &ushort_max,
407 .proc_handler = proc_dointvec_minmax 405 .proc_handler = proc_dointvec_minmax
408 }, 406 },
409 { } 407 { }
diff --git a/net/dcb/dcbnl.c b/net/dcb/dcbnl.c
index 93ea80196f0e..5b21f6f88e97 100644
--- a/net/dcb/dcbnl.c
+++ b/net/dcb/dcbnl.c
@@ -177,6 +177,8 @@ static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
177 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)}, 177 [DCB_ATTR_IEEE_PFC] = {.len = sizeof(struct ieee_pfc)},
178 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED}, 178 [DCB_ATTR_IEEE_APP_TABLE] = {.type = NLA_NESTED},
179 [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)}, 179 [DCB_ATTR_IEEE_MAXRATE] = {.len = sizeof(struct ieee_maxrate)},
180 [DCB_ATTR_IEEE_QCN] = {.len = sizeof(struct ieee_qcn)},
181 [DCB_ATTR_IEEE_QCN_STATS] = {.len = sizeof(struct ieee_qcn_stats)},
180}; 182};
181 183
182static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = { 184static const struct nla_policy dcbnl_ieee_app[DCB_ATTR_IEEE_APP_MAX + 1] = {
@@ -1030,7 +1032,7 @@ nla_put_failure:
1030 return err; 1032 return err;
1031} 1033}
1032 1034
1033/* Handle IEEE 802.1Qaz GET commands. */ 1035/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
1034static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev) 1036static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
1035{ 1037{
1036 struct nlattr *ieee, *app; 1038 struct nlattr *ieee, *app;
@@ -1067,6 +1069,32 @@ static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
1067 } 1069 }
1068 } 1070 }
1069 1071
1072 if (ops->ieee_getqcn) {
1073 struct ieee_qcn qcn;
1074
1075 memset(&qcn, 0, sizeof(qcn));
1076 err = ops->ieee_getqcn(netdev, &qcn);
1077 if (!err) {
1078 err = nla_put(skb, DCB_ATTR_IEEE_QCN,
1079 sizeof(qcn), &qcn);
1080 if (err)
1081 return -EMSGSIZE;
1082 }
1083 }
1084
1085 if (ops->ieee_getqcnstats) {
1086 struct ieee_qcn_stats qcn_stats;
1087
1088 memset(&qcn_stats, 0, sizeof(qcn_stats));
1089 err = ops->ieee_getqcnstats(netdev, &qcn_stats);
1090 if (!err) {
1091 err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
1092 sizeof(qcn_stats), &qcn_stats);
1093 if (err)
1094 return -EMSGSIZE;
1095 }
1096 }
1097
1070 if (ops->ieee_getpfc) { 1098 if (ops->ieee_getpfc) {
1071 struct ieee_pfc pfc; 1099 struct ieee_pfc pfc;
1072 memset(&pfc, 0, sizeof(pfc)); 1100 memset(&pfc, 0, sizeof(pfc));
@@ -1379,8 +1407,9 @@ int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
1379} 1407}
1380EXPORT_SYMBOL(dcbnl_cee_notify); 1408EXPORT_SYMBOL(dcbnl_cee_notify);
1381 1409
1382/* Handle IEEE 802.1Qaz SET commands. If any requested operation can not 1410/* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
1383 * be completed the entire msg is aborted and error value is returned. 1411 * If any requested operation can not be completed
1412 * the entire msg is aborted and error value is returned.
1384 * No attempt is made to reconcile the case where only part of the 1413 * No attempt is made to reconcile the case where only part of the
1385 * cmd can be completed. 1414 * cmd can be completed.
1386 */ 1415 */
@@ -1417,6 +1446,15 @@ static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
1417 goto err; 1446 goto err;
1418 } 1447 }
1419 1448
1449 if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
1450 struct ieee_qcn *qcn =
1451 nla_data(ieee[DCB_ATTR_IEEE_QCN]);
1452
1453 err = ops->ieee_setqcn(netdev, qcn);
1454 if (err)
1455 goto err;
1456 }
1457
1420 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) { 1458 if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1421 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]); 1459 struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1422 err = ops->ieee_setpfc(netdev, pfc); 1460 err = ops->ieee_setpfc(netdev, pfc);
diff --git a/net/dccp/dccp.h b/net/dccp/dccp.h
index e4c144fa706f..bebc735f5afc 100644
--- a/net/dccp/dccp.h
+++ b/net/dccp/dccp.h
@@ -280,8 +280,7 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
280 struct request_sock *req, 280 struct request_sock *req,
281 struct dst_entry *dst); 281 struct dst_entry *dst);
282struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb, 282struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
283 struct request_sock *req, 283 struct request_sock *req);
284 struct request_sock **prev);
285 284
286int dccp_child_process(struct sock *parent, struct sock *child, 285int dccp_child_process(struct sock *parent, struct sock *child,
287 struct sk_buff *skb); 286 struct sk_buff *skb);
@@ -310,16 +309,15 @@ int compat_dccp_setsockopt(struct sock *sk, int level, int optname,
310 char __user *optval, unsigned int optlen); 309 char __user *optval, unsigned int optlen);
311#endif 310#endif
312int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg); 311int dccp_ioctl(struct sock *sk, int cmd, unsigned long arg);
313int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 312int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size);
314 size_t size); 313int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
315int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, 314 int flags, int *addr_len);
316 struct msghdr *msg, size_t len, int nonblock, int flags,
317 int *addr_len);
318void dccp_shutdown(struct sock *sk, int how); 315void dccp_shutdown(struct sock *sk, int how);
319int inet_dccp_listen(struct socket *sock, int backlog); 316int inet_dccp_listen(struct socket *sock, int backlog);
320unsigned int dccp_poll(struct file *file, struct socket *sock, 317unsigned int dccp_poll(struct file *file, struct socket *sock,
321 poll_table *wait); 318 poll_table *wait);
322int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len); 319int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
320void dccp_req_err(struct sock *sk, u64 seq);
323 321
324struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *skb); 322struct sk_buff *dccp_ctl_make_reset(struct sock *sk, struct sk_buff *skb);
325int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code); 323int dccp_send_reset(struct sock *sk, enum dccp_reset_codes code);
diff --git a/net/dccp/diag.c b/net/dccp/diag.c
index 028fc43aacbd..5a45f8de5d99 100644
--- a/net/dccp/diag.c
+++ b/net/dccp/diag.c
@@ -49,13 +49,14 @@ static void dccp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
49} 49}
50 50
51static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 51static void dccp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
52 struct inet_diag_req_v2 *r, struct nlattr *bc) 52 const struct inet_diag_req_v2 *r, struct nlattr *bc)
53{ 53{
54 inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r, bc); 54 inet_diag_dump_icsk(&dccp_hashinfo, skb, cb, r, bc);
55} 55}
56 56
57static int dccp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, 57static int dccp_diag_dump_one(struct sk_buff *in_skb,
58 struct inet_diag_req_v2 *req) 58 const struct nlmsghdr *nlh,
59 const struct inet_diag_req_v2 *req)
59{ 60{
60 return inet_diag_dump_one_icsk(&dccp_hashinfo, in_skb, nlh, req); 61 return inet_diag_dump_one_icsk(&dccp_hashinfo, in_skb, nlh, req);
61} 62}
diff --git a/net/dccp/ipv4.c b/net/dccp/ipv4.c
index e45b968613a4..2b4f21d34df6 100644
--- a/net/dccp/ipv4.c
+++ b/net/dccp/ipv4.c
@@ -89,10 +89,9 @@ int dccp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
89 89
90 if (inet->inet_saddr == 0) 90 if (inet->inet_saddr == 0)
91 inet->inet_saddr = fl4->saddr; 91 inet->inet_saddr = fl4->saddr;
92 inet->inet_rcv_saddr = inet->inet_saddr; 92 sk_rcv_saddr_set(sk, inet->inet_saddr);
93
94 inet->inet_dport = usin->sin_port; 93 inet->inet_dport = usin->sin_port;
95 inet->inet_daddr = daddr; 94 sk_daddr_set(sk, daddr);
96 95
97 inet_csk(sk)->icsk_ext_hdr_len = 0; 96 inet_csk(sk)->icsk_ext_hdr_len = 0;
98 if (inet_opt) 97 if (inet_opt)
@@ -196,6 +195,32 @@ static void dccp_do_redirect(struct sk_buff *skb, struct sock *sk)
196 dst->ops->redirect(dst, sk, skb); 195 dst->ops->redirect(dst, sk, skb);
197} 196}
198 197
198void dccp_req_err(struct sock *sk, u64 seq)
199 {
200 struct request_sock *req = inet_reqsk(sk);
201 struct net *net = sock_net(sk);
202
203 /*
204 * ICMPs are not backlogged, hence we cannot get an established
205 * socket here.
206 */
207 WARN_ON(req->sk);
208
209 if (!between48(seq, dccp_rsk(req)->dreq_iss, dccp_rsk(req)->dreq_gss)) {
210 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
211 reqsk_put(req);
212 } else {
213 /*
214 * Still in RESPOND, just remove it silently.
215 * There is no good way to pass the error to the newly
216 * created socket, and POSIX does not want network
217 * errors returned from accept().
218 */
219 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
220 }
221}
222EXPORT_SYMBOL(dccp_req_err);
223
199/* 224/*
200 * This routine is called by the ICMP module when it gets some sort of error 225 * This routine is called by the ICMP module when it gets some sort of error
201 * condition. If err < 0 then the socket should be closed and the error 226 * condition. If err < 0 then the socket should be closed and the error
@@ -228,10 +253,11 @@ static void dccp_v4_err(struct sk_buff *skb, u32 info)
228 return; 253 return;
229 } 254 }
230 255
231 sk = inet_lookup(net, &dccp_hashinfo, 256 sk = __inet_lookup_established(net, &dccp_hashinfo,
232 iph->daddr, dh->dccph_dport, 257 iph->daddr, dh->dccph_dport,
233 iph->saddr, dh->dccph_sport, inet_iif(skb)); 258 iph->saddr, ntohs(dh->dccph_sport),
234 if (sk == NULL) { 259 inet_iif(skb));
260 if (!sk) {
235 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 261 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
236 return; 262 return;
237 } 263 }
@@ -240,6 +266,9 @@ static void dccp_v4_err(struct sk_buff *skb, u32 info)
240 inet_twsk_put(inet_twsk(sk)); 266 inet_twsk_put(inet_twsk(sk));
241 return; 267 return;
242 } 268 }
269 seq = dccp_hdr_seq(dh);
270 if (sk->sk_state == DCCP_NEW_SYN_RECV)
271 return dccp_req_err(sk, seq);
243 272
244 bh_lock_sock(sk); 273 bh_lock_sock(sk);
245 /* If too many ICMPs get dropped on busy 274 /* If too many ICMPs get dropped on busy
@@ -252,7 +281,6 @@ static void dccp_v4_err(struct sk_buff *skb, u32 info)
252 goto out; 281 goto out;
253 282
254 dp = dccp_sk(sk); 283 dp = dccp_sk(sk);
255 seq = dccp_hdr_seq(dh);
256 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) && 284 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
257 !between48(seq, dp->dccps_awl, dp->dccps_awh)) { 285 !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
258 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); 286 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
@@ -289,35 +317,6 @@ static void dccp_v4_err(struct sk_buff *skb, u32 info)
289 } 317 }
290 318
291 switch (sk->sk_state) { 319 switch (sk->sk_state) {
292 struct request_sock *req , **prev;
293 case DCCP_LISTEN:
294 if (sock_owned_by_user(sk))
295 goto out;
296 req = inet_csk_search_req(sk, &prev, dh->dccph_dport,
297 iph->daddr, iph->saddr);
298 if (!req)
299 goto out;
300
301 /*
302 * ICMPs are not backlogged, hence we cannot get an established
303 * socket here.
304 */
305 WARN_ON(req->sk);
306
307 if (!between48(seq, dccp_rsk(req)->dreq_iss,
308 dccp_rsk(req)->dreq_gss)) {
309 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
310 goto out;
311 }
312 /*
313 * Still in RESPOND, just remove it silently.
314 * There is no good way to pass the error to the newly
315 * created socket, and POSIX does not want network
316 * errors returned from accept().
317 */
318 inet_csk_reqsk_queue_drop(sk, req, prev);
319 goto out;
320
321 case DCCP_REQUESTING: 320 case DCCP_REQUESTING:
322 case DCCP_RESPOND: 321 case DCCP_RESPOND:
323 if (!sock_owned_by_user(sk)) { 322 if (!sock_owned_by_user(sk)) {
@@ -408,8 +407,8 @@ struct sock *dccp_v4_request_recv_sock(struct sock *sk, struct sk_buff *skb,
408 407
409 newinet = inet_sk(newsk); 408 newinet = inet_sk(newsk);
410 ireq = inet_rsk(req); 409 ireq = inet_rsk(req);
411 newinet->inet_daddr = ireq->ir_rmt_addr; 410 sk_daddr_set(newsk, ireq->ir_rmt_addr);
412 newinet->inet_rcv_saddr = ireq->ir_loc_addr; 411 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
413 newinet->inet_saddr = ireq->ir_loc_addr; 412 newinet->inet_saddr = ireq->ir_loc_addr;
414 newinet->inet_opt = ireq->opt; 413 newinet->inet_opt = ireq->opt;
415 ireq->opt = NULL; 414 ireq->opt = NULL;
@@ -449,14 +448,14 @@ static struct sock *dccp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
449 const struct dccp_hdr *dh = dccp_hdr(skb); 448 const struct dccp_hdr *dh = dccp_hdr(skb);
450 const struct iphdr *iph = ip_hdr(skb); 449 const struct iphdr *iph = ip_hdr(skb);
451 struct sock *nsk; 450 struct sock *nsk;
452 struct request_sock **prev;
453 /* Find possible connection requests. */ 451 /* Find possible connection requests. */
454 struct request_sock *req = inet_csk_search_req(sk, &prev, 452 struct request_sock *req = inet_csk_search_req(sk, dh->dccph_sport,
455 dh->dccph_sport,
456 iph->saddr, iph->daddr); 453 iph->saddr, iph->daddr);
457 if (req != NULL) 454 if (req) {
458 return dccp_check_req(sk, skb, req, prev); 455 nsk = dccp_check_req(sk, skb, req);
459 456 reqsk_put(req);
457 return nsk;
458 }
460 nsk = inet_lookup_established(sock_net(sk), &dccp_hashinfo, 459 nsk = inet_lookup_established(sock_net(sk), &dccp_hashinfo,
461 iph->saddr, dh->dccph_sport, 460 iph->saddr, dh->dccph_sport,
462 iph->daddr, dh->dccph_dport, 461 iph->daddr, dh->dccph_dport,
@@ -575,7 +574,7 @@ static void dccp_v4_reqsk_destructor(struct request_sock *req)
575 kfree(inet_rsk(req)->opt); 574 kfree(inet_rsk(req)->opt);
576} 575}
577 576
578void dccp_syn_ack_timeout(struct sock *sk, struct request_sock *req) 577void dccp_syn_ack_timeout(const struct request_sock *req)
579{ 578{
580} 579}
581EXPORT_SYMBOL(dccp_syn_ack_timeout); 580EXPORT_SYMBOL(dccp_syn_ack_timeout);
@@ -624,7 +623,7 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
624 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) 623 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
625 goto drop; 624 goto drop;
626 625
627 req = inet_reqsk_alloc(&dccp_request_sock_ops); 626 req = inet_reqsk_alloc(&dccp_request_sock_ops, sk);
628 if (req == NULL) 627 if (req == NULL)
629 goto drop; 628 goto drop;
630 629
@@ -639,8 +638,10 @@ int dccp_v4_conn_request(struct sock *sk, struct sk_buff *skb)
639 goto drop_and_free; 638 goto drop_and_free;
640 639
641 ireq = inet_rsk(req); 640 ireq = inet_rsk(req);
642 ireq->ir_loc_addr = ip_hdr(skb)->daddr; 641 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
643 ireq->ir_rmt_addr = ip_hdr(skb)->saddr; 642 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
643 ireq->ireq_family = AF_INET;
644 ireq->ir_iif = sk->sk_bound_dev_if;
644 645
645 /* 646 /*
646 * Step 3: Process LISTEN state 647 * Step 3: Process LISTEN state
diff --git a/net/dccp/ipv6.c b/net/dccp/ipv6.c
index 6bcaa33cd804..9d0551092c6c 100644
--- a/net/dccp/ipv6.c
+++ b/net/dccp/ipv6.c
@@ -40,19 +40,6 @@
40static const struct inet_connection_sock_af_ops dccp_ipv6_mapped; 40static const struct inet_connection_sock_af_ops dccp_ipv6_mapped;
41static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops; 41static const struct inet_connection_sock_af_ops dccp_ipv6_af_ops;
42 42
43static void dccp_v6_hash(struct sock *sk)
44{
45 if (sk->sk_state != DCCP_CLOSED) {
46 if (inet_csk(sk)->icsk_af_ops == &dccp_ipv6_mapped) {
47 inet_hash(sk);
48 return;
49 }
50 local_bh_disable();
51 __inet6_hash(sk, NULL);
52 local_bh_enable();
53 }
54}
55
56/* add pseudo-header to DCCP checksum stored in skb->csum */ 43/* add pseudo-header to DCCP checksum stored in skb->csum */
57static inline __sum16 dccp_v6_csum_finish(struct sk_buff *skb, 44static inline __sum16 dccp_v6_csum_finish(struct sk_buff *skb,
58 const struct in6_addr *saddr, 45 const struct in6_addr *saddr,
@@ -98,11 +85,12 @@ static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
98 return; 85 return;
99 } 86 }
100 87
101 sk = inet6_lookup(net, &dccp_hashinfo, 88 sk = __inet6_lookup_established(net, &dccp_hashinfo,
102 &hdr->daddr, dh->dccph_dport, 89 &hdr->daddr, dh->dccph_dport,
103 &hdr->saddr, dh->dccph_sport, inet6_iif(skb)); 90 &hdr->saddr, ntohs(dh->dccph_sport),
91 inet6_iif(skb));
104 92
105 if (sk == NULL) { 93 if (!sk) {
106 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), 94 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
107 ICMP6_MIB_INERRORS); 95 ICMP6_MIB_INERRORS);
108 return; 96 return;
@@ -112,6 +100,9 @@ static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
112 inet_twsk_put(inet_twsk(sk)); 100 inet_twsk_put(inet_twsk(sk));
113 return; 101 return;
114 } 102 }
103 seq = dccp_hdr_seq(dh);
104 if (sk->sk_state == DCCP_NEW_SYN_RECV)
105 return dccp_req_err(sk, seq);
115 106
116 bh_lock_sock(sk); 107 bh_lock_sock(sk);
117 if (sock_owned_by_user(sk)) 108 if (sock_owned_by_user(sk))
@@ -121,7 +112,6 @@ static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
121 goto out; 112 goto out;
122 113
123 dp = dccp_sk(sk); 114 dp = dccp_sk(sk);
124 seq = dccp_hdr_seq(dh);
125 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) && 115 if ((1 << sk->sk_state) & ~(DCCPF_REQUESTING | DCCPF_LISTEN) &&
126 !between48(seq, dp->dccps_awl, dp->dccps_awh)) { 116 !between48(seq, dp->dccps_awl, dp->dccps_awh)) {
127 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); 117 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
@@ -162,32 +152,6 @@ static void dccp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
162 152
163 /* Might be for an request_sock */ 153 /* Might be for an request_sock */
164 switch (sk->sk_state) { 154 switch (sk->sk_state) {
165 struct request_sock *req, **prev;
166 case DCCP_LISTEN:
167 if (sock_owned_by_user(sk))
168 goto out;
169
170 req = inet6_csk_search_req(sk, &prev, dh->dccph_dport,
171 &hdr->daddr, &hdr->saddr,
172 inet6_iif(skb));
173 if (req == NULL)
174 goto out;
175
176 /*
177 * ICMPs are not backlogged, hence we cannot get an established
178 * socket here.
179 */
180 WARN_ON(req->sk != NULL);
181
182 if (!between48(seq, dccp_rsk(req)->dreq_iss,
183 dccp_rsk(req)->dreq_gss)) {
184 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
185 goto out;
186 }
187
188 inet_csk_reqsk_queue_drop(sk, req, prev);
189 goto out;
190
191 case DCCP_REQUESTING: 155 case DCCP_REQUESTING:
192 case DCCP_RESPOND: /* Cannot happen. 156 case DCCP_RESPOND: /* Cannot happen.
193 It can, it SYNs are crossed. --ANK */ 157 It can, it SYNs are crossed. --ANK */
@@ -330,17 +294,16 @@ static struct sock *dccp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
330{ 294{
331 const struct dccp_hdr *dh = dccp_hdr(skb); 295 const struct dccp_hdr *dh = dccp_hdr(skb);
332 const struct ipv6hdr *iph = ipv6_hdr(skb); 296 const struct ipv6hdr *iph = ipv6_hdr(skb);
297 struct request_sock *req;
333 struct sock *nsk; 298 struct sock *nsk;
334 struct request_sock **prev;
335 /* Find possible connection requests. */
336 struct request_sock *req = inet6_csk_search_req(sk, &prev,
337 dh->dccph_sport,
338 &iph->saddr,
339 &iph->daddr,
340 inet6_iif(skb));
341 if (req != NULL)
342 return dccp_check_req(sk, skb, req, prev);
343 299
300 req = inet6_csk_search_req(sk, dh->dccph_sport, &iph->saddr,
301 &iph->daddr, inet6_iif(skb));
302 if (req) {
303 nsk = dccp_check_req(sk, skb, req);
304 reqsk_put(req);
305 return nsk;
306 }
344 nsk = __inet6_lookup_established(sock_net(sk), &dccp_hashinfo, 307 nsk = __inet6_lookup_established(sock_net(sk), &dccp_hashinfo,
345 &iph->saddr, dh->dccph_sport, 308 &iph->saddr, dh->dccph_sport,
346 &iph->daddr, ntohs(dh->dccph_dport), 309 &iph->daddr, ntohs(dh->dccph_dport),
@@ -386,7 +349,7 @@ static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
386 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) 349 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
387 goto drop; 350 goto drop;
388 351
389 req = inet_reqsk_alloc(&dccp6_request_sock_ops); 352 req = inet_reqsk_alloc(&dccp6_request_sock_ops, sk);
390 if (req == NULL) 353 if (req == NULL)
391 goto drop; 354 goto drop;
392 355
@@ -403,6 +366,7 @@ static int dccp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
403 ireq = inet_rsk(req); 366 ireq = inet_rsk(req);
404 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr; 367 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
405 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr; 368 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
369 ireq->ireq_family = AF_INET6;
406 370
407 if (ipv6_opt_accepted(sk, skb, IP6CB(skb)) || 371 if (ipv6_opt_accepted(sk, skb, IP6CB(skb)) ||
408 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || 372 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
@@ -469,11 +433,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
469 433
470 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 434 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
471 435
472 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr); 436 newnp->saddr = newsk->sk_v6_rcv_saddr;
473
474 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
475
476 newsk->sk_v6_rcv_saddr = newnp->saddr;
477 437
478 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped; 438 inet_csk(newsk)->icsk_af_ops = &dccp_ipv6_mapped;
479 newsk->sk_backlog_rcv = dccp_v4_do_rcv; 439 newsk->sk_backlog_rcv = dccp_v4_do_rcv;
@@ -591,7 +551,7 @@ static struct sock *dccp_v6_request_recv_sock(struct sock *sk,
591 dccp_done(newsk); 551 dccp_done(newsk);
592 goto out; 552 goto out;
593 } 553 }
594 __inet6_hash(newsk, NULL); 554 __inet_hash(newsk, NULL);
595 555
596 return newsk; 556 return newsk;
597 557
@@ -916,9 +876,7 @@ static int dccp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
916 sk->sk_backlog_rcv = dccp_v6_do_rcv; 876 sk->sk_backlog_rcv = dccp_v6_do_rcv;
917 goto failure; 877 goto failure;
918 } 878 }
919 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); 879 np->saddr = sk->sk_v6_rcv_saddr;
920 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, &sk->sk_v6_rcv_saddr);
921
922 return err; 880 return err;
923 } 881 }
924 882
@@ -1061,7 +1019,7 @@ static struct proto dccp_v6_prot = {
1061 .sendmsg = dccp_sendmsg, 1019 .sendmsg = dccp_sendmsg,
1062 .recvmsg = dccp_recvmsg, 1020 .recvmsg = dccp_recvmsg,
1063 .backlog_rcv = dccp_v6_do_rcv, 1021 .backlog_rcv = dccp_v6_do_rcv,
1064 .hash = dccp_v6_hash, 1022 .hash = inet_hash,
1065 .unhash = inet_unhash, 1023 .unhash = inet_unhash,
1066 .accept = inet_csk_accept, 1024 .accept = inet_csk_accept,
1067 .get_port = inet_csk_get_port, 1025 .get_port = inet_csk_get_port,
diff --git a/net/dccp/minisocks.c b/net/dccp/minisocks.c
index b50dc436db1f..5f566663e47f 100644
--- a/net/dccp/minisocks.c
+++ b/net/dccp/minisocks.c
@@ -27,28 +27,16 @@
27 27
28struct inet_timewait_death_row dccp_death_row = { 28struct inet_timewait_death_row dccp_death_row = {
29 .sysctl_max_tw_buckets = NR_FILE * 2, 29 .sysctl_max_tw_buckets = NR_FILE * 2,
30 .period = DCCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
31 .death_lock = __SPIN_LOCK_UNLOCKED(dccp_death_row.death_lock),
32 .hashinfo = &dccp_hashinfo, 30 .hashinfo = &dccp_hashinfo,
33 .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
34 (unsigned long)&dccp_death_row),
35 .twkill_work = __WORK_INITIALIZER(dccp_death_row.twkill_work,
36 inet_twdr_twkill_work),
37/* Short-time timewait calendar */
38
39 .twcal_hand = -1,
40 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
41 (unsigned long)&dccp_death_row),
42}; 31};
43 32
44EXPORT_SYMBOL_GPL(dccp_death_row); 33EXPORT_SYMBOL_GPL(dccp_death_row);
45 34
46void dccp_time_wait(struct sock *sk, int state, int timeo) 35void dccp_time_wait(struct sock *sk, int state, int timeo)
47{ 36{
48 struct inet_timewait_sock *tw = NULL; 37 struct inet_timewait_sock *tw;
49 38
50 if (dccp_death_row.tw_count < dccp_death_row.sysctl_max_tw_buckets) 39 tw = inet_twsk_alloc(sk, &dccp_death_row, state);
51 tw = inet_twsk_alloc(sk, state);
52 40
53 if (tw != NULL) { 41 if (tw != NULL) {
54 const struct inet_connection_sock *icsk = inet_csk(sk); 42 const struct inet_connection_sock *icsk = inet_csk(sk);
@@ -71,8 +59,7 @@ void dccp_time_wait(struct sock *sk, int state, int timeo)
71 if (state == DCCP_TIME_WAIT) 59 if (state == DCCP_TIME_WAIT)
72 timeo = DCCP_TIMEWAIT_LEN; 60 timeo = DCCP_TIMEWAIT_LEN;
73 61
74 inet_twsk_schedule(tw, &dccp_death_row, timeo, 62 inet_twsk_schedule(tw, timeo);
75 DCCP_TIMEWAIT_LEN);
76 inet_twsk_put(tw); 63 inet_twsk_put(tw);
77 } else { 64 } else {
78 /* Sorry, if we're out of memory, just CLOSE this 65 /* Sorry, if we're out of memory, just CLOSE this
@@ -152,8 +139,7 @@ EXPORT_SYMBOL_GPL(dccp_create_openreq_child);
152 * as an request_sock. 139 * as an request_sock.
153 */ 140 */
154struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb, 141struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
155 struct request_sock *req, 142 struct request_sock *req)
156 struct request_sock **prev)
157{ 143{
158 struct sock *child = NULL; 144 struct sock *child = NULL;
159 struct dccp_request_sock *dreq = dccp_rsk(req); 145 struct dccp_request_sock *dreq = dccp_rsk(req);
@@ -200,7 +186,7 @@ struct sock *dccp_check_req(struct sock *sk, struct sk_buff *skb,
200 if (child == NULL) 186 if (child == NULL)
201 goto listen_overflow; 187 goto listen_overflow;
202 188
203 inet_csk_reqsk_queue_unlink(sk, req, prev); 189 inet_csk_reqsk_queue_unlink(sk, req);
204 inet_csk_reqsk_queue_removed(sk, req); 190 inet_csk_reqsk_queue_removed(sk, req);
205 inet_csk_reqsk_queue_add(sk, req, child); 191 inet_csk_reqsk_queue_add(sk, req, child);
206out: 192out:
@@ -212,7 +198,7 @@ drop:
212 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET) 198 if (dccp_hdr(skb)->dccph_type != DCCP_PKT_RESET)
213 req->rsk_ops->send_reset(sk, skb); 199 req->rsk_ops->send_reset(sk, skb);
214 200
215 inet_csk_reqsk_queue_drop(sk, req, prev); 201 inet_csk_reqsk_queue_drop(sk, req);
216 goto out; 202 goto out;
217} 203}
218 204
diff --git a/net/dccp/probe.c b/net/dccp/probe.c
index 595ddf0459db..d8346d0eadeb 100644
--- a/net/dccp/probe.c
+++ b/net/dccp/probe.c
@@ -72,8 +72,7 @@ static void printl(const char *fmt, ...)
72 wake_up(&dccpw.wait); 72 wake_up(&dccpw.wait);
73} 73}
74 74
75static int jdccp_sendmsg(struct kiocb *iocb, struct sock *sk, 75static int jdccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
76 struct msghdr *msg, size_t size)
77{ 76{
78 const struct inet_sock *inet = inet_sk(sk); 77 const struct inet_sock *inet = inet_sk(sk);
79 struct ccid3_hc_tx_sock *hc = NULL; 78 struct ccid3_hc_tx_sock *hc = NULL;
diff --git a/net/dccp/proto.c b/net/dccp/proto.c
index e171b780b499..52a94016526d 100644
--- a/net/dccp/proto.c
+++ b/net/dccp/proto.c
@@ -741,8 +741,7 @@ static int dccp_msghdr_parse(struct msghdr *msg, struct sk_buff *skb)
741 return 0; 741 return 0;
742} 742}
743 743
744int dccp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 744int dccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
745 size_t len)
746{ 745{
747 const struct dccp_sock *dp = dccp_sk(sk); 746 const struct dccp_sock *dp = dccp_sk(sk);
748 const int flags = msg->msg_flags; 747 const int flags = msg->msg_flags;
@@ -806,8 +805,8 @@ out_discard:
806 805
807EXPORT_SYMBOL_GPL(dccp_sendmsg); 806EXPORT_SYMBOL_GPL(dccp_sendmsg);
808 807
809int dccp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 808int dccp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
810 size_t len, int nonblock, int flags, int *addr_len) 809 int flags, int *addr_len)
811{ 810{
812 const struct dccp_hdr *dh; 811 const struct dccp_hdr *dh;
813 long timeo; 812 long timeo;
diff --git a/net/dccp/timer.c b/net/dccp/timer.c
index 1cd46a345cb0..3ef7acef3ce8 100644
--- a/net/dccp/timer.c
+++ b/net/dccp/timer.c
@@ -161,33 +161,11 @@ out:
161 sock_put(sk); 161 sock_put(sk);
162} 162}
163 163
164/*
165 * Timer for listening sockets
166 */
167static void dccp_response_timer(struct sock *sk)
168{
169 inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, DCCP_TIMEOUT_INIT,
170 DCCP_RTO_MAX);
171}
172
173static void dccp_keepalive_timer(unsigned long data) 164static void dccp_keepalive_timer(unsigned long data)
174{ 165{
175 struct sock *sk = (struct sock *)data; 166 struct sock *sk = (struct sock *)data;
176 167
177 /* Only process if socket is not in use. */ 168 pr_err("dccp should not use a keepalive timer !\n");
178 bh_lock_sock(sk);
179 if (sock_owned_by_user(sk)) {
180 /* Try again later. */
181 inet_csk_reset_keepalive_timer(sk, HZ / 20);
182 goto out;
183 }
184
185 if (sk->sk_state == DCCP_LISTEN) {
186 dccp_response_timer(sk);
187 goto out;
188 }
189out:
190 bh_unlock_sock(sk);
191 sock_put(sk); 169 sock_put(sk);
192} 170}
193 171
diff --git a/net/decnet/af_decnet.c b/net/decnet/af_decnet.c
index 810228646de3..754484b3cd0e 100644
--- a/net/decnet/af_decnet.c
+++ b/net/decnet/af_decnet.c
@@ -1669,8 +1669,8 @@ static int dn_data_ready(struct sock *sk, struct sk_buff_head *q, int flags, int
1669} 1669}
1670 1670
1671 1671
1672static int dn_recvmsg(struct kiocb *iocb, struct socket *sock, 1672static int dn_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1673 struct msghdr *msg, size_t size, int flags) 1673 int flags)
1674{ 1674{
1675 struct sock *sk = sock->sk; 1675 struct sock *sk = sock->sk;
1676 struct dn_scp *scp = DN_SK(sk); 1676 struct dn_scp *scp = DN_SK(sk);
@@ -1905,8 +1905,7 @@ static inline struct sk_buff *dn_alloc_send_pskb(struct sock *sk,
1905 return skb; 1905 return skb;
1906} 1906}
1907 1907
1908static int dn_sendmsg(struct kiocb *iocb, struct socket *sock, 1908static int dn_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1909 struct msghdr *msg, size_t size)
1910{ 1909{
1911 struct sock *sk = sock->sk; 1910 struct sock *sk = sock->sk;
1912 struct dn_scp *scp = DN_SK(sk); 1911 struct dn_scp *scp = DN_SK(sk);
diff --git a/net/decnet/dn_neigh.c b/net/decnet/dn_neigh.c
index 7ca7c3143da3..4507b188fc51 100644
--- a/net/decnet/dn_neigh.c
+++ b/net/decnet/dn_neigh.c
@@ -49,41 +49,17 @@
49#include <net/dn_route.h> 49#include <net/dn_route.h>
50 50
51static int dn_neigh_construct(struct neighbour *); 51static int dn_neigh_construct(struct neighbour *);
52static void dn_long_error_report(struct neighbour *, struct sk_buff *); 52static void dn_neigh_error_report(struct neighbour *, struct sk_buff *);
53static void dn_short_error_report(struct neighbour *, struct sk_buff *); 53static int dn_neigh_output(struct neighbour *neigh, struct sk_buff *skb);
54static int dn_long_output(struct neighbour *, struct sk_buff *);
55static int dn_short_output(struct neighbour *, struct sk_buff *);
56static int dn_phase3_output(struct neighbour *, struct sk_buff *);
57
58
59/*
60 * For talking to broadcast devices: Ethernet & PPP
61 */
62static const struct neigh_ops dn_long_ops = {
63 .family = AF_DECnet,
64 .error_report = dn_long_error_report,
65 .output = dn_long_output,
66 .connected_output = dn_long_output,
67};
68 54
69/* 55/*
70 * For talking to pointopoint and multidrop devices: DDCMP and X.25 56 * Operations for adding the link layer header.
71 */ 57 */
72static const struct neigh_ops dn_short_ops = { 58static const struct neigh_ops dn_neigh_ops = {
73 .family = AF_DECnet, 59 .family = AF_DECnet,
74 .error_report = dn_short_error_report, 60 .error_report = dn_neigh_error_report,
75 .output = dn_short_output, 61 .output = dn_neigh_output,
76 .connected_output = dn_short_output, 62 .connected_output = dn_neigh_output,
77};
78
79/*
80 * For talking to DECnet phase III nodes
81 */
82static const struct neigh_ops dn_phase3_ops = {
83 .family = AF_DECnet,
84 .error_report = dn_short_error_report, /* Can use short version here */
85 .output = dn_phase3_output,
86 .connected_output = dn_phase3_output,
87}; 63};
88 64
89static u32 dn_neigh_hash(const void *pkey, 65static u32 dn_neigh_hash(const void *pkey,
@@ -93,11 +69,18 @@ static u32 dn_neigh_hash(const void *pkey,
93 return jhash_2words(*(__u16 *)pkey, 0, hash_rnd[0]); 69 return jhash_2words(*(__u16 *)pkey, 0, hash_rnd[0]);
94} 70}
95 71
72static bool dn_key_eq(const struct neighbour *neigh, const void *pkey)
73{
74 return neigh_key_eq16(neigh, pkey);
75}
76
96struct neigh_table dn_neigh_table = { 77struct neigh_table dn_neigh_table = {
97 .family = PF_DECnet, 78 .family = PF_DECnet,
98 .entry_size = NEIGH_ENTRY_SIZE(sizeof(struct dn_neigh)), 79 .entry_size = NEIGH_ENTRY_SIZE(sizeof(struct dn_neigh)),
99 .key_len = sizeof(__le16), 80 .key_len = sizeof(__le16),
81 .protocol = cpu_to_be16(ETH_P_DNA_RT),
100 .hash = dn_neigh_hash, 82 .hash = dn_neigh_hash,
83 .key_eq = dn_key_eq,
101 .constructor = dn_neigh_construct, 84 .constructor = dn_neigh_construct,
102 .id = "dn_neigh_cache", 85 .id = "dn_neigh_cache",
103 .parms ={ 86 .parms ={
@@ -146,16 +129,9 @@ static int dn_neigh_construct(struct neighbour *neigh)
146 129
147 __neigh_parms_put(neigh->parms); 130 __neigh_parms_put(neigh->parms);
148 neigh->parms = neigh_parms_clone(parms); 131 neigh->parms = neigh_parms_clone(parms);
149
150 if (dn_db->use_long)
151 neigh->ops = &dn_long_ops;
152 else
153 neigh->ops = &dn_short_ops;
154 rcu_read_unlock(); 132 rcu_read_unlock();
155 133
156 if (dn->flags & DN_NDFLAG_P3) 134 neigh->ops = &dn_neigh_ops;
157 neigh->ops = &dn_phase3_ops;
158
159 neigh->nud_state = NUD_NOARP; 135 neigh->nud_state = NUD_NOARP;
160 neigh->output = neigh->ops->connected_output; 136 neigh->output = neigh->ops->connected_output;
161 137
@@ -187,24 +163,16 @@ static int dn_neigh_construct(struct neighbour *neigh)
187 return 0; 163 return 0;
188} 164}
189 165
190static void dn_long_error_report(struct neighbour *neigh, struct sk_buff *skb) 166static void dn_neigh_error_report(struct neighbour *neigh, struct sk_buff *skb)
191{
192 printk(KERN_DEBUG "dn_long_error_report: called\n");
193 kfree_skb(skb);
194}
195
196
197static void dn_short_error_report(struct neighbour *neigh, struct sk_buff *skb)
198{ 167{
199 printk(KERN_DEBUG "dn_short_error_report: called\n"); 168 printk(KERN_DEBUG "dn_neigh_error_report: called\n");
200 kfree_skb(skb); 169 kfree_skb(skb);
201} 170}
202 171
203static int dn_neigh_output_packet(struct sk_buff *skb) 172static int dn_neigh_output(struct neighbour *neigh, struct sk_buff *skb)
204{ 173{
205 struct dst_entry *dst = skb_dst(skb); 174 struct dst_entry *dst = skb_dst(skb);
206 struct dn_route *rt = (struct dn_route *)dst; 175 struct dn_route *rt = (struct dn_route *)dst;
207 struct neighbour *neigh = rt->n;
208 struct net_device *dev = neigh->dev; 176 struct net_device *dev = neigh->dev;
209 char mac_addr[ETH_ALEN]; 177 char mac_addr[ETH_ALEN];
210 unsigned int seq; 178 unsigned int seq;
@@ -226,7 +194,20 @@ static int dn_neigh_output_packet(struct sk_buff *skb)
226 return err; 194 return err;
227} 195}
228 196
229static int dn_long_output(struct neighbour *neigh, struct sk_buff *skb) 197static int dn_neigh_output_packet(struct sock *sk, struct sk_buff *skb)
198{
199 struct dst_entry *dst = skb_dst(skb);
200 struct dn_route *rt = (struct dn_route *)dst;
201 struct neighbour *neigh = rt->n;
202
203 return neigh->output(neigh, skb);
204}
205
206/*
207 * For talking to broadcast devices: Ethernet & PPP
208 */
209static int dn_long_output(struct neighbour *neigh, struct sock *sk,
210 struct sk_buff *skb)
230{ 211{
231 struct net_device *dev = neigh->dev; 212 struct net_device *dev = neigh->dev;
232 int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3; 213 int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
@@ -265,11 +246,15 @@ static int dn_long_output(struct neighbour *neigh, struct sk_buff *skb)
265 246
266 skb_reset_network_header(skb); 247 skb_reset_network_header(skb);
267 248
268 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, skb, NULL, 249 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, sk, skb,
269 neigh->dev, dn_neigh_output_packet); 250 NULL, neigh->dev, dn_neigh_output_packet);
270} 251}
271 252
272static int dn_short_output(struct neighbour *neigh, struct sk_buff *skb) 253/*
254 * For talking to pointopoint and multidrop devices: DDCMP and X.25
255 */
256static int dn_short_output(struct neighbour *neigh, struct sock *sk,
257 struct sk_buff *skb)
273{ 258{
274 struct net_device *dev = neigh->dev; 259 struct net_device *dev = neigh->dev;
275 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2; 260 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
@@ -301,15 +286,17 @@ static int dn_short_output(struct neighbour *neigh, struct sk_buff *skb)
301 286
302 skb_reset_network_header(skb); 287 skb_reset_network_header(skb);
303 288
304 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, skb, NULL, 289 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, sk, skb,
305 neigh->dev, dn_neigh_output_packet); 290 NULL, neigh->dev, dn_neigh_output_packet);
306} 291}
307 292
308/* 293/*
309 * Phase 3 output is the same is short output, execpt that 294 * For talking to DECnet phase III nodes
295 * Phase 3 output is the same as short output, execpt that
310 * it clears the area bits before transmission. 296 * it clears the area bits before transmission.
311 */ 297 */
312static int dn_phase3_output(struct neighbour *neigh, struct sk_buff *skb) 298static int dn_phase3_output(struct neighbour *neigh, struct sock *sk,
299 struct sk_buff *skb)
313{ 300{
314 struct net_device *dev = neigh->dev; 301 struct net_device *dev = neigh->dev;
315 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2; 302 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
@@ -340,8 +327,34 @@ static int dn_phase3_output(struct neighbour *neigh, struct sk_buff *skb)
340 327
341 skb_reset_network_header(skb); 328 skb_reset_network_header(skb);
342 329
343 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, skb, NULL, 330 return NF_HOOK(NFPROTO_DECNET, NF_DN_POST_ROUTING, sk, skb,
344 neigh->dev, dn_neigh_output_packet); 331 NULL, neigh->dev, dn_neigh_output_packet);
332}
333
334int dn_to_neigh_output(struct sock *sk, struct sk_buff *skb)
335{
336 struct dst_entry *dst = skb_dst(skb);
337 struct dn_route *rt = (struct dn_route *) dst;
338 struct neighbour *neigh = rt->n;
339 struct dn_neigh *dn = (struct dn_neigh *)neigh;
340 struct dn_dev *dn_db;
341 bool use_long;
342
343 rcu_read_lock();
344 dn_db = rcu_dereference(neigh->dev->dn_ptr);
345 if (dn_db == NULL) {
346 rcu_read_unlock();
347 return -EINVAL;
348 }
349 use_long = dn_db->use_long;
350 rcu_read_unlock();
351
352 if (dn->flags & DN_NDFLAG_P3)
353 return dn_phase3_output(neigh, sk, skb);
354 if (use_long)
355 return dn_long_output(neigh, sk, skb);
356 else
357 return dn_short_output(neigh, sk, skb);
345} 358}
346 359
347/* 360/*
@@ -362,7 +375,7 @@ void dn_neigh_pointopoint_hello(struct sk_buff *skb)
362/* 375/*
363 * Ethernet router hello message received 376 * Ethernet router hello message received
364 */ 377 */
365int dn_neigh_router_hello(struct sk_buff *skb) 378int dn_neigh_router_hello(struct sock *sk, struct sk_buff *skb)
366{ 379{
367 struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data; 380 struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
368 381
@@ -424,7 +437,7 @@ int dn_neigh_router_hello(struct sk_buff *skb)
424/* 437/*
425 * Endnode hello message received 438 * Endnode hello message received
426 */ 439 */
427int dn_neigh_endnode_hello(struct sk_buff *skb) 440int dn_neigh_endnode_hello(struct sock *sk, struct sk_buff *skb)
428{ 441{
429 struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data; 442 struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
430 struct neighbour *neigh; 443 struct neighbour *neigh;
diff --git a/net/decnet/dn_nsp_in.c b/net/decnet/dn_nsp_in.c
index fe5f01485d33..a321eac9fd0c 100644
--- a/net/decnet/dn_nsp_in.c
+++ b/net/decnet/dn_nsp_in.c
@@ -714,7 +714,7 @@ out:
714 return ret; 714 return ret;
715} 715}
716 716
717static int dn_nsp_rx_packet(struct sk_buff *skb) 717static int dn_nsp_rx_packet(struct sock *sk2, struct sk_buff *skb)
718{ 718{
719 struct dn_skb_cb *cb = DN_SKB_CB(skb); 719 struct dn_skb_cb *cb = DN_SKB_CB(skb);
720 struct sock *sk = NULL; 720 struct sock *sk = NULL;
@@ -814,7 +814,8 @@ free_out:
814 814
815int dn_nsp_rx(struct sk_buff *skb) 815int dn_nsp_rx(struct sk_buff *skb)
816{ 816{
817 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN, skb, skb->dev, NULL, 817 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_IN, NULL, skb,
818 skb->dev, NULL,
818 dn_nsp_rx_packet); 819 dn_nsp_rx_packet);
819} 820}
820 821
diff --git a/net/decnet/dn_route.c b/net/decnet/dn_route.c
index 3b81092771f8..03227ffd19ce 100644
--- a/net/decnet/dn_route.c
+++ b/net/decnet/dn_route.c
@@ -136,7 +136,6 @@ int decnet_dst_gc_interval = 2;
136 136
137static struct dst_ops dn_dst_ops = { 137static struct dst_ops dn_dst_ops = {
138 .family = PF_DECnet, 138 .family = PF_DECnet,
139 .protocol = cpu_to_be16(ETH_P_DNA_RT),
140 .gc_thresh = 128, 139 .gc_thresh = 128,
141 .gc = dn_dst_gc, 140 .gc = dn_dst_gc,
142 .check = dn_dst_check, 141 .check = dn_dst_check,
@@ -513,7 +512,7 @@ static int dn_return_long(struct sk_buff *skb)
513 * 512 *
514 * Returns: result of input function if route is found, error code otherwise 513 * Returns: result of input function if route is found, error code otherwise
515 */ 514 */
516static int dn_route_rx_packet(struct sk_buff *skb) 515static int dn_route_rx_packet(struct sock *sk, struct sk_buff *skb)
517{ 516{
518 struct dn_skb_cb *cb; 517 struct dn_skb_cb *cb;
519 int err; 518 int err;
@@ -574,7 +573,8 @@ static int dn_route_rx_long(struct sk_buff *skb)
574 ptr++; 573 ptr++;
575 cb->hops = *ptr++; /* Visit Count */ 574 cb->hops = *ptr++; /* Visit Count */
576 575
577 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, 576 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, NULL, skb,
577 skb->dev, NULL,
578 dn_route_rx_packet); 578 dn_route_rx_packet);
579 579
580drop_it: 580drop_it:
@@ -601,7 +601,8 @@ static int dn_route_rx_short(struct sk_buff *skb)
601 ptr += 2; 601 ptr += 2;
602 cb->hops = *ptr & 0x3f; 602 cb->hops = *ptr & 0x3f;
603 603
604 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, skb, skb->dev, NULL, 604 return NF_HOOK(NFPROTO_DECNET, NF_DN_PRE_ROUTING, NULL, skb,
605 skb->dev, NULL,
605 dn_route_rx_packet); 606 dn_route_rx_packet);
606 607
607drop_it: 608drop_it:
@@ -609,7 +610,7 @@ drop_it:
609 return NET_RX_DROP; 610 return NET_RX_DROP;
610} 611}
611 612
612static int dn_route_discard(struct sk_buff *skb) 613static int dn_route_discard(struct sock *sk, struct sk_buff *skb)
613{ 614{
614 /* 615 /*
615 * I know we drop the packet here, but thats considered success in 616 * I know we drop the packet here, but thats considered success in
@@ -619,7 +620,7 @@ static int dn_route_discard(struct sk_buff *skb)
619 return NET_RX_SUCCESS; 620 return NET_RX_SUCCESS;
620} 621}
621 622
622static int dn_route_ptp_hello(struct sk_buff *skb) 623static int dn_route_ptp_hello(struct sock *sk, struct sk_buff *skb)
623{ 624{
624 dn_dev_hello(skb); 625 dn_dev_hello(skb);
625 dn_neigh_pointopoint_hello(skb); 626 dn_neigh_pointopoint_hello(skb);
@@ -705,22 +706,22 @@ int dn_route_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type
705 switch (flags & DN_RT_CNTL_MSK) { 706 switch (flags & DN_RT_CNTL_MSK) {
706 case DN_RT_PKT_HELO: 707 case DN_RT_PKT_HELO:
707 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, 708 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
708 skb, skb->dev, NULL, 709 NULL, skb, skb->dev, NULL,
709 dn_route_ptp_hello); 710 dn_route_ptp_hello);
710 711
711 case DN_RT_PKT_L1RT: 712 case DN_RT_PKT_L1RT:
712 case DN_RT_PKT_L2RT: 713 case DN_RT_PKT_L2RT:
713 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE, 714 return NF_HOOK(NFPROTO_DECNET, NF_DN_ROUTE,
714 skb, skb->dev, NULL, 715 NULL, skb, skb->dev, NULL,
715 dn_route_discard); 716 dn_route_discard);
716 case DN_RT_PKT_ERTH: 717 case DN_RT_PKT_ERTH:
717 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, 718 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
718 skb, skb->dev, NULL, 719 NULL, skb, skb->dev, NULL,
719 dn_neigh_router_hello); 720 dn_neigh_router_hello);
720 721
721 case DN_RT_PKT_EEDH: 722 case DN_RT_PKT_EEDH:
722 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO, 723 return NF_HOOK(NFPROTO_DECNET, NF_DN_HELLO,
723 skb, skb->dev, NULL, 724 NULL, skb, skb->dev, NULL,
724 dn_neigh_endnode_hello); 725 dn_neigh_endnode_hello);
725 } 726 }
726 } else { 727 } else {
@@ -743,15 +744,6 @@ out:
743 return NET_RX_DROP; 744 return NET_RX_DROP;
744} 745}
745 746
746static int dn_to_neigh_output(struct sk_buff *skb)
747{
748 struct dst_entry *dst = skb_dst(skb);
749 struct dn_route *rt = (struct dn_route *) dst;
750 struct neighbour *n = rt->n;
751
752 return n->output(n, skb);
753}
754
755static int dn_output(struct sock *sk, struct sk_buff *skb) 747static int dn_output(struct sock *sk, struct sk_buff *skb)
756{ 748{
757 struct dst_entry *dst = skb_dst(skb); 749 struct dst_entry *dst = skb_dst(skb);
@@ -778,7 +770,8 @@ static int dn_output(struct sock *sk, struct sk_buff *skb)
778 cb->rt_flags |= DN_RT_F_IE; 770 cb->rt_flags |= DN_RT_F_IE;
779 cb->hops = 0; 771 cb->hops = 0;
780 772
781 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, skb, NULL, dev, 773 return NF_HOOK(NFPROTO_DECNET, NF_DN_LOCAL_OUT, sk, skb,
774 NULL, dev,
782 dn_to_neigh_output); 775 dn_to_neigh_output);
783 776
784error: 777error:
@@ -826,7 +819,8 @@ static int dn_forward(struct sk_buff *skb)
826 if (rt->rt_flags & RTCF_DOREDIRECT) 819 if (rt->rt_flags & RTCF_DOREDIRECT)
827 cb->rt_flags |= DN_RT_F_IE; 820 cb->rt_flags |= DN_RT_F_IE;
828 821
829 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, skb, dev, skb->dev, 822 return NF_HOOK(NFPROTO_DECNET, NF_DN_FORWARD, NULL, skb,
823 dev, skb->dev,
830 dn_to_neigh_output); 824 dn_to_neigh_output);
831 825
832drop: 826drop:
diff --git a/net/decnet/netfilter/dn_rtmsg.c b/net/decnet/netfilter/dn_rtmsg.c
index e4d9560a910b..af34fc9bdf69 100644
--- a/net/decnet/netfilter/dn_rtmsg.c
+++ b/net/decnet/netfilter/dn_rtmsg.c
@@ -89,9 +89,7 @@ static void dnrmg_send_peer(struct sk_buff *skb)
89 89
90static unsigned int dnrmg_hook(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
93 const struct net_device *out,
94 int (*okfn)(struct sk_buff *))
95{ 93{
96 dnrmg_send_peer(skb); 94 dnrmg_send_peer(skb);
97 return NF_ACCEPT; 95 return NF_ACCEPT;
diff --git a/net/dsa/Kconfig b/net/dsa/Kconfig
index 5f8ac404535b..ff7736f7ff42 100644
--- a/net/dsa/Kconfig
+++ b/net/dsa/Kconfig
@@ -5,9 +5,12 @@ config HAVE_NET_DSA
5# Drivers must select NET_DSA and the appropriate tagging format 5# Drivers must select NET_DSA and the appropriate tagging format
6 6
7config NET_DSA 7config NET_DSA
8 tristate 8 tristate "Distributed Switch Architecture"
9 depends on HAVE_NET_DSA 9 depends on HAVE_NET_DSA && NET_SWITCHDEV
10 select PHYLIB 10 select PHYLIB
11 ---help---
12 Say Y if you want to enable support for the hardware switches supported
13 by the Distributed Switch Architecture.
11 14
12if NET_DSA 15if NET_DSA
13 16
diff --git a/net/dsa/dsa.c b/net/dsa/dsa.c
index 4dea2e0681d1..079a224471e7 100644
--- a/net/dsa/dsa.c
+++ b/net/dsa/dsa.c
@@ -20,6 +20,7 @@
20#include <linux/of.h> 20#include <linux/of.h>
21#include <linux/of_mdio.h> 21#include <linux/of_mdio.h>
22#include <linux/of_platform.h> 22#include <linux/of_platform.h>
23#include <linux/of_net.h>
23#include <linux/sysfs.h> 24#include <linux/sysfs.h>
24#include "dsa_priv.h" 25#include "dsa_priv.h"
25 26
@@ -123,7 +124,7 @@ static ssize_t temp1_max_store(struct device *dev,
123 124
124 return count; 125 return count;
125} 126}
126static DEVICE_ATTR(temp1_max, S_IRUGO, temp1_max_show, temp1_max_store); 127static DEVICE_ATTR_RW(temp1_max);
127 128
128static ssize_t temp1_max_alarm_show(struct device *dev, 129static ssize_t temp1_max_alarm_show(struct device *dev,
129 struct device_attribute *attr, char *buf) 130 struct device_attribute *attr, char *buf)
@@ -158,8 +159,8 @@ static umode_t dsa_hwmon_attrs_visible(struct kobject *kobj,
158 if (index == 1) { 159 if (index == 1) {
159 if (!drv->get_temp_limit) 160 if (!drv->get_temp_limit)
160 mode = 0; 161 mode = 0;
161 else if (drv->set_temp_limit) 162 else if (!drv->set_temp_limit)
162 mode |= S_IWUSR; 163 mode &= ~S_IWUSR;
163 } else if (index == 2 && !drv->get_temp_alarm) { 164 } else if (index == 2 && !drv->get_temp_alarm) {
164 mode = 0; 165 mode = 0;
165 } 166 }
@@ -175,43 +176,14 @@ __ATTRIBUTE_GROUPS(dsa_hwmon);
175#endif /* CONFIG_NET_DSA_HWMON */ 176#endif /* CONFIG_NET_DSA_HWMON */
176 177
177/* basic switch operations **************************************************/ 178/* basic switch operations **************************************************/
178static struct dsa_switch * 179static int dsa_switch_setup_one(struct dsa_switch *ds, struct device *parent)
179dsa_switch_setup(struct dsa_switch_tree *dst, int index,
180 struct device *parent, struct device *host_dev)
181{ 180{
182 struct dsa_chip_data *pd = dst->pd->chip + index; 181 struct dsa_switch_driver *drv = ds->drv;
183 struct dsa_switch_driver *drv; 182 struct dsa_switch_tree *dst = ds->dst;
184 struct dsa_switch *ds; 183 struct dsa_chip_data *pd = ds->pd;
185 int ret;
186 char *name;
187 int i;
188 bool valid_name_found = false; 184 bool valid_name_found = false;
189 185 int index = ds->index;
190 /* 186 int i, ret;
191 * Probe for switch model.
192 */
193 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
194 if (drv == NULL) {
195 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
196 index);
197 return ERR_PTR(-EINVAL);
198 }
199 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
200 index, name);
201
202
203 /*
204 * Allocate and initialise switch state.
205 */
206 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
207 if (ds == NULL)
208 return ERR_PTR(-ENOMEM);
209
210 ds->dst = dst;
211 ds->index = index;
212 ds->pd = dst->pd->chip + index;
213 ds->drv = drv;
214 ds->master_dev = host_dev;
215 187
216 /* 188 /*
217 * Validate supplied switch configuration. 189 * Validate supplied switch configuration.
@@ -256,7 +228,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
256 * switch. 228 * switch.
257 */ 229 */
258 if (dst->cpu_switch == index) { 230 if (dst->cpu_switch == index) {
259 switch (drv->tag_protocol) { 231 switch (ds->tag_protocol) {
260#ifdef CONFIG_NET_DSA_TAG_DSA 232#ifdef CONFIG_NET_DSA_TAG_DSA
261 case DSA_TAG_PROTO_DSA: 233 case DSA_TAG_PROTO_DSA:
262 dst->rcv = dsa_netdev_ops.rcv; 234 dst->rcv = dsa_netdev_ops.rcv;
@@ -284,7 +256,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
284 goto out; 256 goto out;
285 } 257 }
286 258
287 dst->tag_protocol = drv->tag_protocol; 259 dst->tag_protocol = ds->tag_protocol;
288 } 260 }
289 261
290 /* 262 /*
@@ -314,19 +286,15 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
314 * Create network devices for physical switch ports. 286 * Create network devices for physical switch ports.
315 */ 287 */
316 for (i = 0; i < DSA_MAX_PORTS; i++) { 288 for (i = 0; i < DSA_MAX_PORTS; i++) {
317 struct net_device *slave_dev;
318
319 if (!(ds->phys_port_mask & (1 << i))) 289 if (!(ds->phys_port_mask & (1 << i)))
320 continue; 290 continue;
321 291
322 slave_dev = dsa_slave_create(ds, parent, i, pd->port_names[i]); 292 ret = dsa_slave_create(ds, parent, i, pd->port_names[i]);
323 if (slave_dev == NULL) { 293 if (ret < 0) {
324 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n", 294 netdev_err(dst->master_netdev, "[%d]: can't create dsa slave device for port %d(%s)\n",
325 index, i, pd->port_names[i]); 295 index, i, pd->port_names[i]);
326 continue; 296 ret = 0;
327 } 297 }
328
329 ds->ports[i] = slave_dev;
330 } 298 }
331 299
332#ifdef CONFIG_NET_DSA_HWMON 300#ifdef CONFIG_NET_DSA_HWMON
@@ -354,13 +322,57 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
354 } 322 }
355#endif /* CONFIG_NET_DSA_HWMON */ 323#endif /* CONFIG_NET_DSA_HWMON */
356 324
357 return ds; 325 return ret;
358 326
359out_free: 327out_free:
360 mdiobus_free(ds->slave_mii_bus); 328 mdiobus_free(ds->slave_mii_bus);
361out: 329out:
362 kfree(ds); 330 kfree(ds);
363 return ERR_PTR(ret); 331 return ret;
332}
333
334static struct dsa_switch *
335dsa_switch_setup(struct dsa_switch_tree *dst, int index,
336 struct device *parent, struct device *host_dev)
337{
338 struct dsa_chip_data *pd = dst->pd->chip + index;
339 struct dsa_switch_driver *drv;
340 struct dsa_switch *ds;
341 int ret;
342 char *name;
343
344 /*
345 * Probe for switch model.
346 */
347 drv = dsa_switch_probe(host_dev, pd->sw_addr, &name);
348 if (drv == NULL) {
349 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
350 index);
351 return ERR_PTR(-EINVAL);
352 }
353 netdev_info(dst->master_netdev, "[%d]: detected a %s switch\n",
354 index, name);
355
356
357 /*
358 * Allocate and initialise switch state.
359 */
360 ds = kzalloc(sizeof(*ds) + drv->priv_size, GFP_KERNEL);
361 if (ds == NULL)
362 return NULL;
363
364 ds->dst = dst;
365 ds->index = index;
366 ds->pd = pd;
367 ds->drv = drv;
368 ds->tag_protocol = drv->tag_protocol;
369 ds->master_dev = host_dev;
370
371 ret = dsa_switch_setup_one(ds, parent);
372 if (ret)
373 return NULL;
374
375 return ds;
364} 376}
365 377
366static void dsa_switch_destroy(struct dsa_switch *ds) 378static void dsa_switch_destroy(struct dsa_switch *ds)
@@ -378,7 +390,7 @@ static int dsa_switch_suspend(struct dsa_switch *ds)
378 390
379 /* Suspend slave network devices */ 391 /* Suspend slave network devices */
380 for (i = 0; i < DSA_MAX_PORTS; i++) { 392 for (i = 0; i < DSA_MAX_PORTS; i++) {
381 if (!(ds->phys_port_mask & (1 << i))) 393 if (!dsa_is_port_initialized(ds, i))
382 continue; 394 continue;
383 395
384 ret = dsa_slave_suspend(ds->ports[i]); 396 ret = dsa_slave_suspend(ds->ports[i]);
@@ -404,7 +416,7 @@ static int dsa_switch_resume(struct dsa_switch *ds)
404 416
405 /* Resume slave network devices */ 417 /* Resume slave network devices */
406 for (i = 0; i < DSA_MAX_PORTS; i++) { 418 for (i = 0; i < DSA_MAX_PORTS; i++) {
407 if (!(ds->phys_port_mask & (1 << i))) 419 if (!dsa_is_port_initialized(ds, i))
408 continue; 420 continue;
409 421
410 ret = dsa_slave_resume(ds->ports[i]); 422 ret = dsa_slave_resume(ds->ports[i]);
@@ -558,12 +570,12 @@ static void dsa_of_free_platform_data(struct dsa_platform_data *pd)
558 kfree(pd->chip); 570 kfree(pd->chip);
559} 571}
560 572
561static int dsa_of_probe(struct platform_device *pdev) 573static int dsa_of_probe(struct device *dev)
562{ 574{
563 struct device_node *np = pdev->dev.of_node; 575 struct device_node *np = dev->of_node;
564 struct device_node *child, *mdio, *ethernet, *port, *link; 576 struct device_node *child, *mdio, *ethernet, *port, *link;
565 struct mii_bus *mdio_bus; 577 struct mii_bus *mdio_bus;
566 struct platform_device *ethernet_dev; 578 struct net_device *ethernet_dev;
567 struct dsa_platform_data *pd; 579 struct dsa_platform_data *pd;
568 struct dsa_chip_data *cd; 580 struct dsa_chip_data *cd;
569 const char *port_name; 581 const char *port_name;
@@ -578,22 +590,22 @@ static int dsa_of_probe(struct platform_device *pdev)
578 590
579 mdio_bus = of_mdio_find_bus(mdio); 591 mdio_bus = of_mdio_find_bus(mdio);
580 if (!mdio_bus) 592 if (!mdio_bus)
581 return -EINVAL; 593 return -EPROBE_DEFER;
582 594
583 ethernet = of_parse_phandle(np, "dsa,ethernet", 0); 595 ethernet = of_parse_phandle(np, "dsa,ethernet", 0);
584 if (!ethernet) 596 if (!ethernet)
585 return -EINVAL; 597 return -EINVAL;
586 598
587 ethernet_dev = of_find_device_by_node(ethernet); 599 ethernet_dev = of_find_net_device_by_node(ethernet);
588 if (!ethernet_dev) 600 if (!ethernet_dev)
589 return -ENODEV; 601 return -EPROBE_DEFER;
590 602
591 pd = kzalloc(sizeof(*pd), GFP_KERNEL); 603 pd = kzalloc(sizeof(*pd), GFP_KERNEL);
592 if (!pd) 604 if (!pd)
593 return -ENOMEM; 605 return -ENOMEM;
594 606
595 pdev->dev.platform_data = pd; 607 dev->platform_data = pd;
596 pd->netdev = &ethernet_dev->dev; 608 pd->of_netdev = ethernet_dev;
597 pd->nr_chips = of_get_available_child_count(np); 609 pd->nr_chips = of_get_available_child_count(np);
598 if (pd->nr_chips > DSA_MAX_SWITCHES) 610 if (pd->nr_chips > DSA_MAX_SWITCHES)
599 pd->nr_chips = DSA_MAX_SWITCHES; 611 pd->nr_chips = DSA_MAX_SWITCHES;
@@ -665,72 +677,35 @@ out_free_chip:
665 dsa_of_free_platform_data(pd); 677 dsa_of_free_platform_data(pd);
666out_free: 678out_free:
667 kfree(pd); 679 kfree(pd);
668 pdev->dev.platform_data = NULL; 680 dev->platform_data = NULL;
669 return ret; 681 return ret;
670} 682}
671 683
672static void dsa_of_remove(struct platform_device *pdev) 684static void dsa_of_remove(struct device *dev)
673{ 685{
674 struct dsa_platform_data *pd = pdev->dev.platform_data; 686 struct dsa_platform_data *pd = dev->platform_data;
675 687
676 if (!pdev->dev.of_node) 688 if (!dev->of_node)
677 return; 689 return;
678 690
679 dsa_of_free_platform_data(pd); 691 dsa_of_free_platform_data(pd);
680 kfree(pd); 692 kfree(pd);
681} 693}
682#else 694#else
683static inline int dsa_of_probe(struct platform_device *pdev) 695static inline int dsa_of_probe(struct device *dev)
684{ 696{
685 return 0; 697 return 0;
686} 698}
687 699
688static inline void dsa_of_remove(struct platform_device *pdev) 700static inline void dsa_of_remove(struct device *dev)
689{ 701{
690} 702}
691#endif 703#endif
692 704
693static int dsa_probe(struct platform_device *pdev) 705static void dsa_setup_dst(struct dsa_switch_tree *dst, struct net_device *dev,
706 struct device *parent, struct dsa_platform_data *pd)
694{ 707{
695 struct dsa_platform_data *pd = pdev->dev.platform_data; 708 int i;
696 struct net_device *dev;
697 struct dsa_switch_tree *dst;
698 int i, ret;
699
700 pr_notice_once("Distributed Switch Architecture driver version %s\n",
701 dsa_driver_version);
702
703 if (pdev->dev.of_node) {
704 ret = dsa_of_probe(pdev);
705 if (ret)
706 return ret;
707
708 pd = pdev->dev.platform_data;
709 }
710
711 if (pd == NULL || pd->netdev == NULL)
712 return -EINVAL;
713
714 dev = dev_to_net_device(pd->netdev);
715 if (dev == NULL) {
716 ret = -EINVAL;
717 goto out;
718 }
719
720 if (dev->dsa_ptr != NULL) {
721 dev_put(dev);
722 ret = -EEXIST;
723 goto out;
724 }
725
726 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
727 if (dst == NULL) {
728 dev_put(dev);
729 ret = -ENOMEM;
730 goto out;
731 }
732
733 platform_set_drvdata(pdev, dst);
734 709
735 dst->pd = pd; 710 dst->pd = pd;
736 dst->master_netdev = dev; 711 dst->master_netdev = dev;
@@ -740,7 +715,7 @@ static int dsa_probe(struct platform_device *pdev)
740 for (i = 0; i < pd->nr_chips; i++) { 715 for (i = 0; i < pd->nr_chips; i++) {
741 struct dsa_switch *ds; 716 struct dsa_switch *ds;
742 717
743 ds = dsa_switch_setup(dst, i, &pdev->dev, pd->chip[i].host_dev); 718 ds = dsa_switch_setup(dst, i, parent, pd->chip[i].host_dev);
744 if (IS_ERR(ds)) { 719 if (IS_ERR(ds)) {
745 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n", 720 netdev_err(dev, "[%d]: couldn't create dsa switch instance (error %ld)\n",
746 i, PTR_ERR(ds)); 721 i, PTR_ERR(ds));
@@ -768,18 +743,67 @@ static int dsa_probe(struct platform_device *pdev)
768 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ); 743 dst->link_poll_timer.expires = round_jiffies(jiffies + HZ);
769 add_timer(&dst->link_poll_timer); 744 add_timer(&dst->link_poll_timer);
770 } 745 }
746}
747
748static int dsa_probe(struct platform_device *pdev)
749{
750 struct dsa_platform_data *pd = pdev->dev.platform_data;
751 struct net_device *dev;
752 struct dsa_switch_tree *dst;
753 int ret;
754
755 pr_notice_once("Distributed Switch Architecture driver version %s\n",
756 dsa_driver_version);
757
758 if (pdev->dev.of_node) {
759 ret = dsa_of_probe(&pdev->dev);
760 if (ret)
761 return ret;
762
763 pd = pdev->dev.platform_data;
764 }
765
766 if (pd == NULL || (pd->netdev == NULL && pd->of_netdev == NULL))
767 return -EINVAL;
768
769 if (pd->of_netdev) {
770 dev = pd->of_netdev;
771 dev_hold(dev);
772 } else {
773 dev = dev_to_net_device(pd->netdev);
774 }
775 if (dev == NULL) {
776 ret = -EPROBE_DEFER;
777 goto out;
778 }
779
780 if (dev->dsa_ptr != NULL) {
781 dev_put(dev);
782 ret = -EEXIST;
783 goto out;
784 }
785
786 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
787 if (dst == NULL) {
788 dev_put(dev);
789 ret = -ENOMEM;
790 goto out;
791 }
792
793 platform_set_drvdata(pdev, dst);
794
795 dsa_setup_dst(dst, dev, &pdev->dev, pd);
771 796
772 return 0; 797 return 0;
773 798
774out: 799out:
775 dsa_of_remove(pdev); 800 dsa_of_remove(&pdev->dev);
776 801
777 return ret; 802 return ret;
778} 803}
779 804
780static int dsa_remove(struct platform_device *pdev) 805static void dsa_remove_dst(struct dsa_switch_tree *dst)
781{ 806{
782 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
783 int i; 807 int i;
784 808
785 if (dst->link_poll_needed) 809 if (dst->link_poll_needed)
@@ -793,8 +817,14 @@ static int dsa_remove(struct platform_device *pdev)
793 if (ds != NULL) 817 if (ds != NULL)
794 dsa_switch_destroy(ds); 818 dsa_switch_destroy(ds);
795 } 819 }
820}
821
822static int dsa_remove(struct platform_device *pdev)
823{
824 struct dsa_switch_tree *dst = platform_get_drvdata(pdev);
796 825
797 dsa_of_remove(pdev); 826 dsa_remove_dst(dst);
827 dsa_of_remove(&pdev->dev);
798 828
799 return 0; 829 return 0;
800} 830}
@@ -821,6 +851,10 @@ static struct packet_type dsa_pack_type __read_mostly = {
821 .func = dsa_switch_rcv, 851 .func = dsa_switch_rcv,
822}; 852};
823 853
854static struct notifier_block dsa_netdevice_nb __read_mostly = {
855 .notifier_call = dsa_slave_netdevice_event,
856};
857
824#ifdef CONFIG_PM_SLEEP 858#ifdef CONFIG_PM_SLEEP
825static int dsa_suspend(struct device *d) 859static int dsa_suspend(struct device *d)
826{ 860{
@@ -879,6 +913,8 @@ static int __init dsa_init_module(void)
879{ 913{
880 int rc; 914 int rc;
881 915
916 register_netdevice_notifier(&dsa_netdevice_nb);
917
882 rc = platform_driver_register(&dsa_driver); 918 rc = platform_driver_register(&dsa_driver);
883 if (rc) 919 if (rc)
884 return rc; 920 return rc;
@@ -891,6 +927,7 @@ module_init(dsa_init_module);
891 927
892static void __exit dsa_cleanup_module(void) 928static void __exit dsa_cleanup_module(void)
893{ 929{
930 unregister_netdevice_notifier(&dsa_netdevice_nb);
894 dev_remove_pack(&dsa_pack_type); 931 dev_remove_pack(&dsa_pack_type);
895 platform_driver_unregister(&dsa_driver); 932 platform_driver_unregister(&dsa_driver);
896} 933}
diff --git a/net/dsa/dsa_priv.h b/net/dsa/dsa_priv.h
index dc9756d3154c..d5f1f9b862ea 100644
--- a/net/dsa/dsa_priv.h
+++ b/net/dsa/dsa_priv.h
@@ -45,6 +45,8 @@ struct dsa_slave_priv {
45 int old_link; 45 int old_link;
46 int old_pause; 46 int old_pause;
47 int old_duplex; 47 int old_duplex;
48
49 struct net_device *bridge_dev;
48}; 50};
49 51
50/* dsa.c */ 52/* dsa.c */
@@ -53,11 +55,12 @@ extern char dsa_driver_version[];
53/* slave.c */ 55/* slave.c */
54extern const struct dsa_device_ops notag_netdev_ops; 56extern const struct dsa_device_ops notag_netdev_ops;
55void dsa_slave_mii_bus_init(struct dsa_switch *ds); 57void dsa_slave_mii_bus_init(struct dsa_switch *ds);
56struct net_device *dsa_slave_create(struct dsa_switch *ds, 58int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
57 struct device *parent, 59 int port, char *name);
58 int port, char *name);
59int dsa_slave_suspend(struct net_device *slave_dev); 60int dsa_slave_suspend(struct net_device *slave_dev);
60int dsa_slave_resume(struct net_device *slave_dev); 61int dsa_slave_resume(struct net_device *slave_dev);
62int dsa_slave_netdevice_event(struct notifier_block *unused,
63 unsigned long event, void *ptr);
61 64
62/* tag_dsa.c */ 65/* tag_dsa.c */
63extern const struct dsa_device_ops dsa_netdev_ops; 66extern const struct dsa_device_ops dsa_netdev_ops;
diff --git a/net/dsa/slave.c b/net/dsa/slave.c
index f23deadf42a0..827cda560a55 100644
--- a/net/dsa/slave.c
+++ b/net/dsa/slave.c
@@ -10,10 +10,14 @@
10 10
11#include <linux/list.h> 11#include <linux/list.h>
12#include <linux/etherdevice.h> 12#include <linux/etherdevice.h>
13#include <linux/netdevice.h>
13#include <linux/phy.h> 14#include <linux/phy.h>
14#include <linux/phy_fixed.h> 15#include <linux/phy_fixed.h>
15#include <linux/of_net.h> 16#include <linux/of_net.h>
16#include <linux/of_mdio.h> 17#include <linux/of_mdio.h>
18#include <net/rtnetlink.h>
19#include <net/switchdev.h>
20#include <linux/if_bridge.h>
17#include "dsa_priv.h" 21#include "dsa_priv.h"
18 22
19/* slave mii_bus handling ***************************************************/ 23/* slave mii_bus handling ***************************************************/
@@ -51,13 +55,16 @@ void dsa_slave_mii_bus_init(struct dsa_switch *ds)
51 55
52 56
53/* slave device handling ****************************************************/ 57/* slave device handling ****************************************************/
54static int dsa_slave_init(struct net_device *dev) 58static int dsa_slave_get_iflink(const struct net_device *dev)
55{ 59{
56 struct dsa_slave_priv *p = netdev_priv(dev); 60 struct dsa_slave_priv *p = netdev_priv(dev);
57 61
58 dev->iflink = p->parent->dst->master_netdev->ifindex; 62 return p->parent->dst->master_netdev->ifindex;
63}
59 64
60 return 0; 65static inline bool dsa_port_is_bridged(struct dsa_slave_priv *p)
66{
67 return !!p->bridge_dev;
61} 68}
62 69
63static int dsa_slave_open(struct net_device *dev) 70static int dsa_slave_open(struct net_device *dev)
@@ -65,6 +72,8 @@ static int dsa_slave_open(struct net_device *dev)
65 struct dsa_slave_priv *p = netdev_priv(dev); 72 struct dsa_slave_priv *p = netdev_priv(dev);
66 struct net_device *master = p->parent->dst->master_netdev; 73 struct net_device *master = p->parent->dst->master_netdev;
67 struct dsa_switch *ds = p->parent; 74 struct dsa_switch *ds = p->parent;
75 u8 stp_state = dsa_port_is_bridged(p) ?
76 BR_STATE_BLOCKING : BR_STATE_FORWARDING;
68 int err; 77 int err;
69 78
70 if (!(master->flags & IFF_UP)) 79 if (!(master->flags & IFF_UP))
@@ -93,6 +102,9 @@ static int dsa_slave_open(struct net_device *dev)
93 goto clear_promisc; 102 goto clear_promisc;
94 } 103 }
95 104
105 if (ds->drv->port_stp_update)
106 ds->drv->port_stp_update(ds, p->port, stp_state);
107
96 if (p->phy) 108 if (p->phy)
97 phy_start(p->phy); 109 phy_start(p->phy);
98 110
@@ -133,6 +145,9 @@ static int dsa_slave_close(struct net_device *dev)
133 if (ds->drv->port_disable) 145 if (ds->drv->port_disable)
134 ds->drv->port_disable(ds, p->port, p->phy); 146 ds->drv->port_disable(ds, p->port, p->phy);
135 147
148 if (ds->drv->port_stp_update)
149 ds->drv->port_stp_update(ds, p->port, BR_STATE_DISABLED);
150
136 return 0; 151 return 0;
137} 152}
138 153
@@ -184,6 +199,105 @@ out:
184 return 0; 199 return 0;
185} 200}
186 201
202static int dsa_slave_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
203 struct net_device *dev,
204 const unsigned char *addr, u16 vid, u16 nlm_flags)
205{
206 struct dsa_slave_priv *p = netdev_priv(dev);
207 struct dsa_switch *ds = p->parent;
208 int ret = -EOPNOTSUPP;
209
210 if (ds->drv->fdb_add)
211 ret = ds->drv->fdb_add(ds, p->port, addr, vid);
212
213 return ret;
214}
215
216static int dsa_slave_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
217 struct net_device *dev,
218 const unsigned char *addr, u16 vid)
219{
220 struct dsa_slave_priv *p = netdev_priv(dev);
221 struct dsa_switch *ds = p->parent;
222 int ret = -EOPNOTSUPP;
223
224 if (ds->drv->fdb_del)
225 ret = ds->drv->fdb_del(ds, p->port, addr, vid);
226
227 return ret;
228}
229
230static int dsa_slave_fill_info(struct net_device *dev, struct sk_buff *skb,
231 const unsigned char *addr, u16 vid,
232 bool is_static,
233 u32 portid, u32 seq, int type,
234 unsigned int flags)
235{
236 struct nlmsghdr *nlh;
237 struct ndmsg *ndm;
238
239 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
240 if (!nlh)
241 return -EMSGSIZE;
242
243 ndm = nlmsg_data(nlh);
244 ndm->ndm_family = AF_BRIDGE;
245 ndm->ndm_pad1 = 0;
246 ndm->ndm_pad2 = 0;
247 ndm->ndm_flags = NTF_EXT_LEARNED;
248 ndm->ndm_type = 0;
249 ndm->ndm_ifindex = dev->ifindex;
250 ndm->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
251
252 if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
253 goto nla_put_failure;
254
255 if (vid && nla_put_u16(skb, NDA_VLAN, vid))
256 goto nla_put_failure;
257
258 nlmsg_end(skb, nlh);
259 return 0;
260
261nla_put_failure:
262 nlmsg_cancel(skb, nlh);
263 return -EMSGSIZE;
264}
265
266/* Dump information about entries, in response to GETNEIGH */
267static int dsa_slave_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
268 struct net_device *dev,
269 struct net_device *filter_dev, int idx)
270{
271 struct dsa_slave_priv *p = netdev_priv(dev);
272 struct dsa_switch *ds = p->parent;
273 unsigned char addr[ETH_ALEN] = { 0 };
274 int ret;
275
276 if (!ds->drv->fdb_getnext)
277 return -EOPNOTSUPP;
278
279 for (; ; idx++) {
280 bool is_static;
281
282 ret = ds->drv->fdb_getnext(ds, p->port, addr, &is_static);
283 if (ret < 0)
284 break;
285
286 if (idx < cb->args[0])
287 continue;
288
289 ret = dsa_slave_fill_info(dev, skb, addr, 0,
290 is_static,
291 NETLINK_CB(cb->skb).portid,
292 cb->nlh->nlmsg_seq,
293 RTM_NEWNEIGH, NLM_F_MULTI);
294 if (ret < 0)
295 break;
296 }
297
298 return idx;
299}
300
187static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 301static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
188{ 302{
189 struct dsa_slave_priv *p = netdev_priv(dev); 303 struct dsa_slave_priv *p = netdev_priv(dev);
@@ -194,6 +308,92 @@ static int dsa_slave_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
194 return -EOPNOTSUPP; 308 return -EOPNOTSUPP;
195} 309}
196 310
311/* Return a bitmask of all ports being currently bridged within a given bridge
312 * device. Note that on leave, the mask will still return the bitmask of ports
313 * currently bridged, prior to port removal, and this is exactly what we want.
314 */
315static u32 dsa_slave_br_port_mask(struct dsa_switch *ds,
316 struct net_device *bridge)
317{
318 struct dsa_slave_priv *p;
319 unsigned int port;
320 u32 mask = 0;
321
322 for (port = 0; port < DSA_MAX_PORTS; port++) {
323 if (!dsa_is_port_initialized(ds, port))
324 continue;
325
326 p = netdev_priv(ds->ports[port]);
327
328 if (ds->ports[port]->priv_flags & IFF_BRIDGE_PORT &&
329 p->bridge_dev == bridge)
330 mask |= 1 << port;
331 }
332
333 return mask;
334}
335
336static int dsa_slave_stp_update(struct net_device *dev, u8 state)
337{
338 struct dsa_slave_priv *p = netdev_priv(dev);
339 struct dsa_switch *ds = p->parent;
340 int ret = -EOPNOTSUPP;
341
342 if (ds->drv->port_stp_update)
343 ret = ds->drv->port_stp_update(ds, p->port, state);
344
345 return ret;
346}
347
348static int dsa_slave_bridge_port_join(struct net_device *dev,
349 struct net_device *br)
350{
351 struct dsa_slave_priv *p = netdev_priv(dev);
352 struct dsa_switch *ds = p->parent;
353 int ret = -EOPNOTSUPP;
354
355 p->bridge_dev = br;
356
357 if (ds->drv->port_join_bridge)
358 ret = ds->drv->port_join_bridge(ds, p->port,
359 dsa_slave_br_port_mask(ds, br));
360
361 return ret;
362}
363
364static int dsa_slave_bridge_port_leave(struct net_device *dev)
365{
366 struct dsa_slave_priv *p = netdev_priv(dev);
367 struct dsa_switch *ds = p->parent;
368 int ret = -EOPNOTSUPP;
369
370
371 if (ds->drv->port_leave_bridge)
372 ret = ds->drv->port_leave_bridge(ds, p->port,
373 dsa_slave_br_port_mask(ds, p->bridge_dev));
374
375 p->bridge_dev = NULL;
376
377 /* Port left the bridge, put in BR_STATE_DISABLED by the bridge layer,
378 * so allow it to be in BR_STATE_FORWARDING to be kept functional
379 */
380 dsa_slave_stp_update(dev, BR_STATE_FORWARDING);
381
382 return ret;
383}
384
385static int dsa_slave_parent_id_get(struct net_device *dev,
386 struct netdev_phys_item_id *psid)
387{
388 struct dsa_slave_priv *p = netdev_priv(dev);
389 struct dsa_switch *ds = p->parent;
390
391 psid->id_len = sizeof(ds->index);
392 memcpy(&psid->id, &ds->index, psid->id_len);
393
394 return 0;
395}
396
197static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev) 397static netdev_tx_t dsa_slave_xmit(struct sk_buff *skb, struct net_device *dev)
198{ 398{
199 struct dsa_slave_priv *p = netdev_priv(dev); 399 struct dsa_slave_priv *p = netdev_priv(dev);
@@ -462,14 +662,22 @@ static const struct ethtool_ops dsa_slave_ethtool_ops = {
462}; 662};
463 663
464static const struct net_device_ops dsa_slave_netdev_ops = { 664static const struct net_device_ops dsa_slave_netdev_ops = {
465 .ndo_init = dsa_slave_init,
466 .ndo_open = dsa_slave_open, 665 .ndo_open = dsa_slave_open,
467 .ndo_stop = dsa_slave_close, 666 .ndo_stop = dsa_slave_close,
468 .ndo_start_xmit = dsa_slave_xmit, 667 .ndo_start_xmit = dsa_slave_xmit,
469 .ndo_change_rx_flags = dsa_slave_change_rx_flags, 668 .ndo_change_rx_flags = dsa_slave_change_rx_flags,
470 .ndo_set_rx_mode = dsa_slave_set_rx_mode, 669 .ndo_set_rx_mode = dsa_slave_set_rx_mode,
471 .ndo_set_mac_address = dsa_slave_set_mac_address, 670 .ndo_set_mac_address = dsa_slave_set_mac_address,
671 .ndo_fdb_add = dsa_slave_fdb_add,
672 .ndo_fdb_del = dsa_slave_fdb_del,
673 .ndo_fdb_dump = dsa_slave_fdb_dump,
472 .ndo_do_ioctl = dsa_slave_ioctl, 674 .ndo_do_ioctl = dsa_slave_ioctl,
675 .ndo_get_iflink = dsa_slave_get_iflink,
676};
677
678static const struct swdev_ops dsa_slave_swdev_ops = {
679 .swdev_parent_id_get = dsa_slave_parent_id_get,
680 .swdev_port_stp_update = dsa_slave_stp_update,
473}; 681};
474 682
475static void dsa_slave_adjust_link(struct net_device *dev) 683static void dsa_slave_adjust_link(struct net_device *dev)
@@ -513,6 +721,24 @@ static int dsa_slave_fixed_link_update(struct net_device *dev,
513} 721}
514 722
515/* slave device setup *******************************************************/ 723/* slave device setup *******************************************************/
724static int dsa_slave_phy_connect(struct dsa_slave_priv *p,
725 struct net_device *slave_dev,
726 int addr)
727{
728 struct dsa_switch *ds = p->parent;
729
730 p->phy = ds->slave_mii_bus->phy_map[addr];
731 if (!p->phy)
732 return -ENODEV;
733
734 /* Use already configured phy mode */
735 p->phy_interface = p->phy->interface;
736 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
737 p->phy_interface);
738
739 return 0;
740}
741
516static int dsa_slave_phy_setup(struct dsa_slave_priv *p, 742static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
517 struct net_device *slave_dev) 743 struct net_device *slave_dev)
518{ 744{
@@ -546,10 +772,25 @@ static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
546 if (ds->drv->get_phy_flags) 772 if (ds->drv->get_phy_flags)
547 phy_flags = ds->drv->get_phy_flags(ds, p->port); 773 phy_flags = ds->drv->get_phy_flags(ds, p->port);
548 774
549 if (phy_dn) 775 if (phy_dn) {
550 p->phy = of_phy_connect(slave_dev, phy_dn, 776 ret = of_mdio_parse_addr(&slave_dev->dev, phy_dn);
551 dsa_slave_adjust_link, phy_flags, 777 /* If this PHY address is part of phys_mii_mask, which means
552 p->phy_interface); 778 * that we need to divert reads and writes to/from it, then we
779 * want to bind this device using the slave MII bus created by
780 * DSA to make that happen.
781 */
782 if (!phy_is_fixed && ret >= 0 &&
783 (ds->phys_mii_mask & (1 << ret))) {
784 ret = dsa_slave_phy_connect(p, slave_dev, ret);
785 if (ret)
786 return ret;
787 } else {
788 p->phy = of_phy_connect(slave_dev, phy_dn,
789 dsa_slave_adjust_link,
790 phy_flags,
791 p->phy_interface);
792 }
793 }
553 794
554 if (p->phy && phy_is_fixed) 795 if (p->phy && phy_is_fixed)
555 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update); 796 fixed_phy_set_link_update(p->phy, dsa_slave_fixed_link_update);
@@ -558,14 +799,9 @@ static int dsa_slave_phy_setup(struct dsa_slave_priv *p,
558 * MDIO bus instead 799 * MDIO bus instead
559 */ 800 */
560 if (!p->phy) { 801 if (!p->phy) {
561 p->phy = ds->slave_mii_bus->phy_map[p->port]; 802 ret = dsa_slave_phy_connect(p, slave_dev, p->port);
562 if (!p->phy) 803 if (ret)
563 return -ENODEV; 804 return ret;
564
565 /* Use already configured phy mode */
566 p->phy_interface = p->phy->interface;
567 phy_connect_direct(slave_dev, p->phy, dsa_slave_adjust_link,
568 p->phy_interface);
569 } else { 805 } else {
570 netdev_info(slave_dev, "attached PHY at address %d [%s]\n", 806 netdev_info(slave_dev, "attached PHY at address %d [%s]\n",
571 p->phy->addr, p->phy->drv->name); 807 p->phy->addr, p->phy->drv->name);
@@ -605,9 +841,8 @@ int dsa_slave_resume(struct net_device *slave_dev)
605 return 0; 841 return 0;
606} 842}
607 843
608struct net_device * 844int dsa_slave_create(struct dsa_switch *ds, struct device *parent,
609dsa_slave_create(struct dsa_switch *ds, struct device *parent, 845 int port, char *name)
610 int port, char *name)
611{ 846{
612 struct net_device *master = ds->dst->master_netdev; 847 struct net_device *master = ds->dst->master_netdev;
613 struct net_device *slave_dev; 848 struct net_device *slave_dev;
@@ -617,13 +852,14 @@ dsa_slave_create(struct dsa_switch *ds, struct device *parent,
617 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name, 852 slave_dev = alloc_netdev(sizeof(struct dsa_slave_priv), name,
618 NET_NAME_UNKNOWN, ether_setup); 853 NET_NAME_UNKNOWN, ether_setup);
619 if (slave_dev == NULL) 854 if (slave_dev == NULL)
620 return slave_dev; 855 return -ENOMEM;
621 856
622 slave_dev->features = master->vlan_features; 857 slave_dev->features = master->vlan_features;
623 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops; 858 slave_dev->ethtool_ops = &dsa_slave_ethtool_ops;
624 eth_hw_addr_inherit(slave_dev, master); 859 eth_hw_addr_inherit(slave_dev, master);
625 slave_dev->tx_queue_len = 0; 860 slave_dev->tx_queue_len = 0;
626 slave_dev->netdev_ops = &dsa_slave_netdev_ops; 861 slave_dev->netdev_ops = &dsa_slave_netdev_ops;
862 slave_dev->swdev_ops = &dsa_slave_swdev_ops;
627 863
628 SET_NETDEV_DEV(slave_dev, parent); 864 SET_NETDEV_DEV(slave_dev, parent);
629 slave_dev->dev.of_node = ds->pd->port_dn[port]; 865 slave_dev->dev.of_node = ds->pd->port_dn[port];
@@ -667,19 +903,64 @@ dsa_slave_create(struct dsa_switch *ds, struct device *parent,
667 ret = dsa_slave_phy_setup(p, slave_dev); 903 ret = dsa_slave_phy_setup(p, slave_dev);
668 if (ret) { 904 if (ret) {
669 free_netdev(slave_dev); 905 free_netdev(slave_dev);
670 return NULL; 906 return ret;
671 } 907 }
672 908
909 ds->ports[port] = slave_dev;
673 ret = register_netdev(slave_dev); 910 ret = register_netdev(slave_dev);
674 if (ret) { 911 if (ret) {
675 netdev_err(master, "error %d registering interface %s\n", 912 netdev_err(master, "error %d registering interface %s\n",
676 ret, slave_dev->name); 913 ret, slave_dev->name);
677 phy_disconnect(p->phy); 914 phy_disconnect(p->phy);
915 ds->ports[port] = NULL;
678 free_netdev(slave_dev); 916 free_netdev(slave_dev);
679 return NULL; 917 return ret;
680 } 918 }
681 919
682 netif_carrier_off(slave_dev); 920 netif_carrier_off(slave_dev);
683 921
684 return slave_dev; 922 return 0;
923}
924
925static bool dsa_slave_dev_check(struct net_device *dev)
926{
927 return dev->netdev_ops == &dsa_slave_netdev_ops;
928}
929
930static int dsa_slave_master_changed(struct net_device *dev)
931{
932 struct net_device *master = netdev_master_upper_dev_get(dev);
933 struct dsa_slave_priv *p = netdev_priv(dev);
934 int err = 0;
935
936 if (master && master->rtnl_link_ops &&
937 !strcmp(master->rtnl_link_ops->kind, "bridge"))
938 err = dsa_slave_bridge_port_join(dev, master);
939 else if (dsa_port_is_bridged(p))
940 err = dsa_slave_bridge_port_leave(dev);
941
942 return err;
943}
944
945int dsa_slave_netdevice_event(struct notifier_block *unused,
946 unsigned long event, void *ptr)
947{
948 struct net_device *dev;
949 int err = 0;
950
951 switch (event) {
952 case NETDEV_CHANGEUPPER:
953 dev = netdev_notifier_info_to_dev(ptr);
954 if (!dsa_slave_dev_check(dev))
955 goto out;
956
957 err = dsa_slave_master_changed(dev);
958 if (err)
959 netdev_warn(dev, "failed to reflect master change\n");
960
961 break;
962 }
963
964out:
965 return NOTIFY_DONE;
685} 966}
diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
index 238f38d21641..f3bad41d725f 100644
--- a/net/ethernet/eth.c
+++ b/net/ethernet/eth.c
@@ -104,7 +104,7 @@ int eth_header(struct sk_buff *skb, struct net_device *dev,
104 */ 104 */
105 105
106 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { 106 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
107 memset(eth->h_dest, 0, ETH_ALEN); 107 eth_zero_addr(eth->h_dest);
108 return ETH_HLEN; 108 return ETH_HLEN;
109 } 109 }
110 110
@@ -113,39 +113,6 @@ int eth_header(struct sk_buff *skb, struct net_device *dev,
113EXPORT_SYMBOL(eth_header); 113EXPORT_SYMBOL(eth_header);
114 114
115/** 115/**
116 * eth_rebuild_header- rebuild the Ethernet MAC header.
117 * @skb: socket buffer to update
118 *
119 * This is called after an ARP or IPV6 ndisc it's resolution on this
120 * sk_buff. We now let protocol (ARP) fill in the other fields.
121 *
122 * This routine CANNOT use cached dst->neigh!
123 * Really, it is used only when dst->neigh is wrong.
124 */
125int eth_rebuild_header(struct sk_buff *skb)
126{
127 struct ethhdr *eth = (struct ethhdr *)skb->data;
128 struct net_device *dev = skb->dev;
129
130 switch (eth->h_proto) {
131#ifdef CONFIG_INET
132 case htons(ETH_P_IP):
133 return arp_find(eth->h_dest, skb);
134#endif
135 default:
136 netdev_dbg(dev,
137 "%s: unable to resolve type %X addresses.\n",
138 dev->name, ntohs(eth->h_proto));
139
140 memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);
141 break;
142 }
143
144 return 0;
145}
146EXPORT_SYMBOL(eth_rebuild_header);
147
148/**
149 * eth_get_headlen - determine the the length of header for an ethernet frame 116 * eth_get_headlen - determine the the length of header for an ethernet frame
150 * @data: pointer to start of frame 117 * @data: pointer to start of frame
151 * @len: total length of frame 118 * @len: total length of frame
@@ -369,7 +336,6 @@ EXPORT_SYMBOL(eth_validate_addr);
369const struct header_ops eth_header_ops ____cacheline_aligned = { 336const struct header_ops eth_header_ops ____cacheline_aligned = {
370 .create = eth_header, 337 .create = eth_header,
371 .parse = eth_header_parse, 338 .parse = eth_header_parse,
372 .rebuild = eth_rebuild_header,
373 .cache = eth_header_cache, 339 .cache = eth_header_cache,
374 .cache_update = eth_header_cache_update, 340 .cache_update = eth_header_cache_update,
375}; 341};
@@ -391,7 +357,7 @@ void ether_setup(struct net_device *dev)
391 dev->flags = IFF_BROADCAST|IFF_MULTICAST; 357 dev->flags = IFF_BROADCAST|IFF_MULTICAST;
392 dev->priv_flags |= IFF_TX_SKB_SHARING; 358 dev->priv_flags |= IFF_TX_SKB_SHARING;
393 359
394 memset(dev->broadcast, 0xFF, ETH_ALEN); 360 eth_broadcast_addr(dev->broadcast);
395 361
396} 362}
397EXPORT_SYMBOL(ether_setup); 363EXPORT_SYMBOL(ether_setup);
diff --git a/net/ieee802154/6lowpan/core.c b/net/ieee802154/6lowpan/core.c
index 055fbb71ba6f..0ae5822ef944 100644
--- a/net/ieee802154/6lowpan/core.c
+++ b/net/ieee802154/6lowpan/core.c
@@ -113,7 +113,7 @@ static void lowpan_setup(struct net_device *dev)
113{ 113{
114 dev->addr_len = IEEE802154_ADDR_LEN; 114 dev->addr_len = IEEE802154_ADDR_LEN;
115 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); 115 memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN);
116 dev->type = ARPHRD_IEEE802154; 116 dev->type = ARPHRD_6LOWPAN;
117 /* Frame Control + Sequence Number + Address fields + Security Header */ 117 /* Frame Control + Sequence Number + Address fields + Security Header */
118 dev->hard_header_len = 2 + 1 + 20 + 14; 118 dev->hard_header_len = 2 + 1 + 20 + 14;
119 dev->needed_tailroom = 2; /* FCS */ 119 dev->needed_tailroom = 2; /* FCS */
@@ -126,6 +126,7 @@ static void lowpan_setup(struct net_device *dev)
126 dev->header_ops = &lowpan_header_ops; 126 dev->header_ops = &lowpan_header_ops;
127 dev->ml_priv = &lowpan_mlme; 127 dev->ml_priv = &lowpan_mlme;
128 dev->destructor = free_netdev; 128 dev->destructor = free_netdev;
129 dev->features |= NETIF_F_NETNS_LOCAL;
129} 130}
130 131
131static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[]) 132static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[])
@@ -148,10 +149,11 @@ static int lowpan_newlink(struct net *src_net, struct net_device *dev,
148 149
149 pr_debug("adding new link\n"); 150 pr_debug("adding new link\n");
150 151
151 if (!tb[IFLA_LINK]) 152 if (!tb[IFLA_LINK] ||
153 !net_eq(dev_net(dev), &init_net))
152 return -EINVAL; 154 return -EINVAL;
153 /* find and hold real wpan device */ 155 /* find and hold real wpan device */
154 real_dev = dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK])); 156 real_dev = dev_get_by_index(dev_net(dev), nla_get_u32(tb[IFLA_LINK]));
155 if (!real_dev) 157 if (!real_dev)
156 return -ENODEV; 158 return -ENODEV;
157 if (real_dev->type != ARPHRD_IEEE802154) { 159 if (real_dev->type != ARPHRD_IEEE802154) {
diff --git a/net/ieee802154/core.c b/net/ieee802154/core.c
index 18bc7e738507..2ee00e8a0308 100644
--- a/net/ieee802154/core.c
+++ b/net/ieee802154/core.c
@@ -25,6 +25,9 @@
25#include "sysfs.h" 25#include "sysfs.h"
26#include "core.h" 26#include "core.h"
27 27
28/* name for sysfs, %d is appended */
29#define PHY_NAME "phy"
30
28/* RCU-protected (and RTNL for writers) */ 31/* RCU-protected (and RTNL for writers) */
29LIST_HEAD(cfg802154_rdev_list); 32LIST_HEAD(cfg802154_rdev_list);
30int cfg802154_rdev_list_generation; 33int cfg802154_rdev_list_generation;
@@ -122,7 +125,7 @@ wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size)
122 125
123 INIT_LIST_HEAD(&rdev->wpan_dev_list); 126 INIT_LIST_HEAD(&rdev->wpan_dev_list);
124 device_initialize(&rdev->wpan_phy.dev); 127 device_initialize(&rdev->wpan_phy.dev);
125 dev_set_name(&rdev->wpan_phy.dev, "wpan-phy%d", rdev->wpan_phy_idx); 128 dev_set_name(&rdev->wpan_phy.dev, PHY_NAME "%d", rdev->wpan_phy_idx);
126 129
127 rdev->wpan_phy.dev.class = &wpan_phy_class; 130 rdev->wpan_phy.dev.class = &wpan_phy_class;
128 rdev->wpan_phy.dev.platform_data = rdev; 131 rdev->wpan_phy.dev.platform_data = rdev;
@@ -225,6 +228,7 @@ static int cfg802154_netdev_notifier_call(struct notifier_block *nb,
225 switch (state) { 228 switch (state) {
226 /* TODO NETDEV_DEVTYPE */ 229 /* TODO NETDEV_DEVTYPE */
227 case NETDEV_REGISTER: 230 case NETDEV_REGISTER:
231 dev->features |= NETIF_F_NETNS_LOCAL;
228 wpan_dev->identifier = ++rdev->wpan_dev_id; 232 wpan_dev->identifier = ++rdev->wpan_dev_id;
229 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list); 233 list_add_rcu(&wpan_dev->list, &rdev->wpan_dev_list);
230 rdev->devlist_generation++; 234 rdev->devlist_generation++;
diff --git a/net/ieee802154/nl-mac.c b/net/ieee802154/nl-mac.c
index 9105265920fe..2b4955d7aae5 100644
--- a/net/ieee802154/nl-mac.c
+++ b/net/ieee802154/nl-mac.c
@@ -76,7 +76,6 @@ nla_put_failure:
76 nlmsg_free(msg); 76 nlmsg_free(msg);
77 return -ENOBUFS; 77 return -ENOBUFS;
78} 78}
79EXPORT_SYMBOL(ieee802154_nl_start_confirm);
80 79
81static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid, 80static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid,
82 u32 seq, int flags, struct net_device *dev) 81 u32 seq, int flags, struct net_device *dev)
diff --git a/net/ieee802154/socket.c b/net/ieee802154/socket.c
index 2878d8ca6d3b..b60c65f70346 100644
--- a/net/ieee802154/socket.c
+++ b/net/ieee802154/socket.c
@@ -98,12 +98,12 @@ static int ieee802154_sock_release(struct socket *sock)
98 return 0; 98 return 0;
99} 99}
100 100
101static int ieee802154_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 101static int ieee802154_sock_sendmsg(struct socket *sock, struct msghdr *msg,
102 struct msghdr *msg, size_t len) 102 size_t len)
103{ 103{
104 struct sock *sk = sock->sk; 104 struct sock *sk = sock->sk;
105 105
106 return sk->sk_prot->sendmsg(iocb, sk, msg, len); 106 return sk->sk_prot->sendmsg(sk, msg, len);
107} 107}
108 108
109static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr, 109static int ieee802154_sock_bind(struct socket *sock, struct sockaddr *uaddr,
@@ -255,8 +255,7 @@ static int raw_disconnect(struct sock *sk, int flags)
255 return 0; 255 return 0;
256} 256}
257 257
258static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, 258static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
259 struct msghdr *msg, size_t size)
260{ 259{
261 struct net_device *dev; 260 struct net_device *dev;
262 unsigned int mtu; 261 unsigned int mtu;
@@ -327,8 +326,8 @@ out:
327 return err; 326 return err;
328} 327}
329 328
330static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 329static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
331 size_t len, int noblock, int flags, int *addr_len) 330 int noblock, int flags, int *addr_len)
332{ 331{
333 size_t copied = 0; 332 size_t copied = 0;
334 int err = -EOPNOTSUPP; 333 int err = -EOPNOTSUPP;
@@ -615,8 +614,7 @@ static int dgram_disconnect(struct sock *sk, int flags)
615 return 0; 614 return 0;
616} 615}
617 616
618static int dgram_sendmsg(struct kiocb *iocb, struct sock *sk, 617static int dgram_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
619 struct msghdr *msg, size_t size)
620{ 618{
621 struct net_device *dev; 619 struct net_device *dev;
622 unsigned int mtu; 620 unsigned int mtu;
@@ -715,9 +713,8 @@ out:
715 return err; 713 return err;
716} 714}
717 715
718static int dgram_recvmsg(struct kiocb *iocb, struct sock *sk, 716static int dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
719 struct msghdr *msg, size_t len, int noblock, 717 int noblock, int flags, int *addr_len)
720 int flags, int *addr_len)
721{ 718{
722 size_t copied = 0; 719 size_t copied = 0;
723 int err = -EOPNOTSUPP; 720 int err = -EOPNOTSUPP;
diff --git a/net/ieee802154/sysfs.c b/net/ieee802154/sysfs.c
index dff55c2d87f3..133b4280660c 100644
--- a/net/ieee802154/sysfs.c
+++ b/net/ieee802154/sysfs.c
@@ -48,49 +48,6 @@ static ssize_t name_show(struct device *dev,
48} 48}
49static DEVICE_ATTR_RO(name); 49static DEVICE_ATTR_RO(name);
50 50
51#define MASTER_SHOW_COMPLEX(name, format_string, args...) \
52static ssize_t name ## _show(struct device *dev, \
53 struct device_attribute *attr, char *buf) \
54{ \
55 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev); \
56 int ret; \
57 \
58 mutex_lock(&phy->pib_lock); \
59 ret = snprintf(buf, PAGE_SIZE, format_string "\n", args); \
60 mutex_unlock(&phy->pib_lock); \
61 return ret; \
62} \
63static DEVICE_ATTR_RO(name)
64
65#define MASTER_SHOW(field, format_string) \
66 MASTER_SHOW_COMPLEX(field, format_string, phy->field)
67
68MASTER_SHOW(current_channel, "%d");
69MASTER_SHOW(current_page, "%d");
70MASTER_SHOW(transmit_power, "%d +- 1 dB");
71MASTER_SHOW_COMPLEX(cca_mode, "%d", phy->cca.mode);
72
73static ssize_t channels_supported_show(struct device *dev,
74 struct device_attribute *attr,
75 char *buf)
76{
77 struct wpan_phy *phy = container_of(dev, struct wpan_phy, dev);
78 int ret;
79 int i, len = 0;
80
81 mutex_lock(&phy->pib_lock);
82 for (i = 0; i < 32; i++) {
83 ret = snprintf(buf + len, PAGE_SIZE - len,
84 "%#09x\n", phy->channels_supported[i]);
85 if (ret < 0)
86 break;
87 len += ret;
88 }
89 mutex_unlock(&phy->pib_lock);
90 return len;
91}
92static DEVICE_ATTR_RO(channels_supported);
93
94static void wpan_phy_release(struct device *dev) 51static void wpan_phy_release(struct device *dev)
95{ 52{
96 struct cfg802154_registered_device *rdev = dev_to_rdev(dev); 53 struct cfg802154_registered_device *rdev = dev_to_rdev(dev);
@@ -101,12 +58,6 @@ static void wpan_phy_release(struct device *dev)
101static struct attribute *pmib_attrs[] = { 58static struct attribute *pmib_attrs[] = {
102 &dev_attr_index.attr, 59 &dev_attr_index.attr,
103 &dev_attr_name.attr, 60 &dev_attr_name.attr,
104 /* below will be removed soon */
105 &dev_attr_current_channel.attr,
106 &dev_attr_current_page.attr,
107 &dev_attr_channels_supported.attr,
108 &dev_attr_transmit_power.attr,
109 &dev_attr_cca_mode.attr,
110 NULL, 61 NULL,
111}; 62};
112ATTRIBUTE_GROUPS(pmib); 63ATTRIBUTE_GROUPS(pmib);
diff --git a/net/ipv4/af_inet.c b/net/ipv4/af_inet.c
index d2e49baaff63..8b47a4d79d04 100644
--- a/net/ipv4/af_inet.c
+++ b/net/ipv4/af_inet.c
@@ -217,7 +217,7 @@ int inet_listen(struct socket *sock, int backlog)
217 * shutdown() (rather than close()). 217 * shutdown() (rather than close()).
218 */ 218 */
219 if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) != 0 && 219 if ((sysctl_tcp_fastopen & TFO_SERVER_ENABLE) != 0 &&
220 inet_csk(sk)->icsk_accept_queue.fastopenq == NULL) { 220 !inet_csk(sk)->icsk_accept_queue.fastopenq) {
221 if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) != 0) 221 if ((sysctl_tcp_fastopen & TFO_SERVER_WO_SOCKOPT1) != 0)
222 err = fastopen_init_queue(sk, backlog); 222 err = fastopen_init_queue(sk, backlog);
223 else if ((sysctl_tcp_fastopen & 223 else if ((sysctl_tcp_fastopen &
@@ -314,11 +314,11 @@ lookup_protocol:
314 answer_flags = answer->flags; 314 answer_flags = answer->flags;
315 rcu_read_unlock(); 315 rcu_read_unlock();
316 316
317 WARN_ON(answer_prot->slab == NULL); 317 WARN_ON(!answer_prot->slab);
318 318
319 err = -ENOBUFS; 319 err = -ENOBUFS;
320 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot); 320 sk = sk_alloc(net, PF_INET, GFP_KERNEL, answer_prot);
321 if (sk == NULL) 321 if (!sk)
322 goto out; 322 goto out;
323 323
324 err = 0; 324 err = 0;
@@ -716,8 +716,7 @@ int inet_getname(struct socket *sock, struct sockaddr *uaddr,
716} 716}
717EXPORT_SYMBOL(inet_getname); 717EXPORT_SYMBOL(inet_getname);
718 718
719int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 719int inet_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
720 size_t size)
721{ 720{
722 struct sock *sk = sock->sk; 721 struct sock *sk = sock->sk;
723 722
@@ -728,7 +727,7 @@ int inet_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
728 inet_autobind(sk)) 727 inet_autobind(sk))
729 return -EAGAIN; 728 return -EAGAIN;
730 729
731 return sk->sk_prot->sendmsg(iocb, sk, msg, size); 730 return sk->sk_prot->sendmsg(sk, msg, size);
732} 731}
733EXPORT_SYMBOL(inet_sendmsg); 732EXPORT_SYMBOL(inet_sendmsg);
734 733
@@ -750,8 +749,8 @@ ssize_t inet_sendpage(struct socket *sock, struct page *page, int offset,
750} 749}
751EXPORT_SYMBOL(inet_sendpage); 750EXPORT_SYMBOL(inet_sendpage);
752 751
753int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 752int inet_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
754 size_t size, int flags) 753 int flags)
755{ 754{
756 struct sock *sk = sock->sk; 755 struct sock *sk = sock->sk;
757 int addr_len = 0; 756 int addr_len = 0;
@@ -759,7 +758,7 @@ int inet_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg,
759 758
760 sock_rps_record_flow(sk); 759 sock_rps_record_flow(sk);
761 760
762 err = sk->sk_prot->recvmsg(iocb, sk, msg, size, flags & MSG_DONTWAIT, 761 err = sk->sk_prot->recvmsg(sk, msg, size, flags & MSG_DONTWAIT,
763 flags & ~MSG_DONTWAIT, &addr_len); 762 flags & ~MSG_DONTWAIT, &addr_len);
764 if (err >= 0) 763 if (err >= 0)
765 msg->msg_namelen = addr_len; 764 msg->msg_namelen = addr_len;
@@ -1270,7 +1269,7 @@ static struct sk_buff *inet_gso_segment(struct sk_buff *skb,
1270 if (udpfrag) { 1269 if (udpfrag) {
1271 iph->id = htons(id); 1270 iph->id = htons(id);
1272 iph->frag_off = htons(offset >> 3); 1271 iph->frag_off = htons(offset >> 3);
1273 if (skb->next != NULL) 1272 if (skb->next)
1274 iph->frag_off |= htons(IP_MF); 1273 iph->frag_off |= htons(IP_MF);
1275 offset += skb->len - nhoff - ihl; 1274 offset += skb->len - nhoff - ihl;
1276 } else { 1275 } else {
@@ -1675,7 +1674,7 @@ static int __init inet_init(void)
1675 struct list_head *r; 1674 struct list_head *r;
1676 int rc = -EINVAL; 1675 int rc = -EINVAL;
1677 1676
1678 BUILD_BUG_ON(sizeof(struct inet_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb)); 1677 sock_skb_cb_check_size(sizeof(struct inet_skb_parm));
1679 1678
1680 rc = proto_register(&tcp_prot, 1); 1679 rc = proto_register(&tcp_prot, 1);
1681 if (rc) 1680 if (rc)
diff --git a/net/ipv4/arp.c b/net/ipv4/arp.c
index 205e1472aa78..933a92820d26 100644
--- a/net/ipv4/arp.c
+++ b/net/ipv4/arp.c
@@ -122,6 +122,7 @@
122 * Interface to generic neighbour cache. 122 * Interface to generic neighbour cache.
123 */ 123 */
124static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd); 124static u32 arp_hash(const void *pkey, const struct net_device *dev, __u32 *hash_rnd);
125static bool arp_key_eq(const struct neighbour *n, const void *pkey);
125static int arp_constructor(struct neighbour *neigh); 126static int arp_constructor(struct neighbour *neigh);
126static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb); 127static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb);
127static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb); 128static void arp_error_report(struct neighbour *neigh, struct sk_buff *skb);
@@ -149,18 +150,12 @@ static const struct neigh_ops arp_direct_ops = {
149 .connected_output = neigh_direct_output, 150 .connected_output = neigh_direct_output,
150}; 151};
151 152
152static const struct neigh_ops arp_broken_ops = {
153 .family = AF_INET,
154 .solicit = arp_solicit,
155 .error_report = arp_error_report,
156 .output = neigh_compat_output,
157 .connected_output = neigh_compat_output,
158};
159
160struct neigh_table arp_tbl = { 153struct neigh_table arp_tbl = {
161 .family = AF_INET, 154 .family = AF_INET,
162 .key_len = 4, 155 .key_len = 4,
156 .protocol = cpu_to_be16(ETH_P_IP),
163 .hash = arp_hash, 157 .hash = arp_hash,
158 .key_eq = arp_key_eq,
164 .constructor = arp_constructor, 159 .constructor = arp_constructor,
165 .proxy_redo = parp_redo, 160 .proxy_redo = parp_redo,
166 .id = "arp_cache", 161 .id = "arp_cache",
@@ -216,7 +211,12 @@ static u32 arp_hash(const void *pkey,
216 const struct net_device *dev, 211 const struct net_device *dev,
217 __u32 *hash_rnd) 212 __u32 *hash_rnd)
218{ 213{
219 return arp_hashfn(*(u32 *)pkey, dev, *hash_rnd); 214 return arp_hashfn(pkey, dev, hash_rnd);
215}
216
217static bool arp_key_eq(const struct neighbour *neigh, const void *pkey)
218{
219 return neigh_key_eq32(neigh, pkey);
220} 220}
221 221
222static int arp_constructor(struct neighbour *neigh) 222static int arp_constructor(struct neighbour *neigh)
@@ -228,7 +228,7 @@ static int arp_constructor(struct neighbour *neigh)
228 228
229 rcu_read_lock(); 229 rcu_read_lock();
230 in_dev = __in_dev_get_rcu(dev); 230 in_dev = __in_dev_get_rcu(dev);
231 if (in_dev == NULL) { 231 if (!in_dev) {
232 rcu_read_unlock(); 232 rcu_read_unlock();
233 return -EINVAL; 233 return -EINVAL;
234 } 234 }
@@ -260,35 +260,6 @@ static int arp_constructor(struct neighbour *neigh)
260 in old paradigm. 260 in old paradigm.
261 */ 261 */
262 262
263#if 1
264 /* So... these "amateur" devices are hopeless.
265 The only thing, that I can say now:
266 It is very sad that we need to keep ugly obsolete
267 code to make them happy.
268
269 They should be moved to more reasonable state, now
270 they use rebuild_header INSTEAD OF hard_start_xmit!!!
271 Besides that, they are sort of out of date
272 (a lot of redundant clones/copies, useless in 2.1),
273 I wonder why people believe that they work.
274 */
275 switch (dev->type) {
276 default:
277 break;
278 case ARPHRD_ROSE:
279#if IS_ENABLED(CONFIG_AX25)
280 case ARPHRD_AX25:
281#if IS_ENABLED(CONFIG_NETROM)
282 case ARPHRD_NETROM:
283#endif
284 neigh->ops = &arp_broken_ops;
285 neigh->output = neigh->ops->output;
286 return 0;
287#else
288 break;
289#endif
290 }
291#endif
292 if (neigh->type == RTN_MULTICAST) { 263 if (neigh->type == RTN_MULTICAST) {
293 neigh->nud_state = NUD_NOARP; 264 neigh->nud_state = NUD_NOARP;
294 arp_mc_map(addr, neigh->ha, dev, 1); 265 arp_mc_map(addr, neigh->ha, dev, 1);
@@ -433,71 +404,6 @@ static int arp_filter(__be32 sip, __be32 tip, struct net_device *dev)
433 return flag; 404 return flag;
434} 405}
435 406
436/* OBSOLETE FUNCTIONS */
437
438/*
439 * Find an arp mapping in the cache. If not found, post a request.
440 *
441 * It is very UGLY routine: it DOES NOT use skb->dst->neighbour,
442 * even if it exists. It is supposed that skb->dev was mangled
443 * by a virtual device (eql, shaper). Nobody but broken devices
444 * is allowed to use this function, it is scheduled to be removed. --ANK
445 */
446
447static int arp_set_predefined(int addr_hint, unsigned char *haddr,
448 __be32 paddr, struct net_device *dev)
449{
450 switch (addr_hint) {
451 case RTN_LOCAL:
452 pr_debug("arp called for own IP address\n");
453 memcpy(haddr, dev->dev_addr, dev->addr_len);
454 return 1;
455 case RTN_MULTICAST:
456 arp_mc_map(paddr, haddr, dev, 1);
457 return 1;
458 case RTN_BROADCAST:
459 memcpy(haddr, dev->broadcast, dev->addr_len);
460 return 1;
461 }
462 return 0;
463}
464
465
466int arp_find(unsigned char *haddr, struct sk_buff *skb)
467{
468 struct net_device *dev = skb->dev;
469 __be32 paddr;
470 struct neighbour *n;
471
472 if (!skb_dst(skb)) {
473 pr_debug("arp_find is called with dst==NULL\n");
474 kfree_skb(skb);
475 return 1;
476 }
477
478 paddr = rt_nexthop(skb_rtable(skb), ip_hdr(skb)->daddr);
479 if (arp_set_predefined(inet_addr_type(dev_net(dev), paddr), haddr,
480 paddr, dev))
481 return 0;
482
483 n = __neigh_lookup(&arp_tbl, &paddr, dev, 1);
484
485 if (n) {
486 n->used = jiffies;
487 if (n->nud_state & NUD_VALID || neigh_event_send(n, skb) == 0) {
488 neigh_ha_snapshot(haddr, n, dev);
489 neigh_release(n);
490 return 0;
491 }
492 neigh_release(n);
493 } else
494 kfree_skb(skb);
495 return 1;
496}
497EXPORT_SYMBOL(arp_find);
498
499/* END OF OBSOLETE FUNCTIONS */
500
501/* 407/*
502 * Check if we can use proxy ARP for this path 408 * Check if we can use proxy ARP for this path
503 */ 409 */
@@ -569,7 +475,7 @@ static inline int arp_fwd_pvlan(struct in_device *in_dev,
569 */ 475 */
570 476
571/* 477/*
572 * Create an arp packet. If (dest_hw == NULL), we create a broadcast 478 * Create an arp packet. If dest_hw is not set, we create a broadcast
573 * message. 479 * message.
574 */ 480 */
575struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip, 481struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
@@ -589,7 +495,7 @@ struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
589 */ 495 */
590 496
591 skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC); 497 skb = alloc_skb(arp_hdr_len(dev) + hlen + tlen, GFP_ATOMIC);
592 if (skb == NULL) 498 if (!skb)
593 return NULL; 499 return NULL;
594 500
595 skb_reserve(skb, hlen); 501 skb_reserve(skb, hlen);
@@ -597,9 +503,9 @@ struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
597 arp = (struct arphdr *) skb_put(skb, arp_hdr_len(dev)); 503 arp = (struct arphdr *) skb_put(skb, arp_hdr_len(dev));
598 skb->dev = dev; 504 skb->dev = dev;
599 skb->protocol = htons(ETH_P_ARP); 505 skb->protocol = htons(ETH_P_ARP);
600 if (src_hw == NULL) 506 if (!src_hw)
601 src_hw = dev->dev_addr; 507 src_hw = dev->dev_addr;
602 if (dest_hw == NULL) 508 if (!dest_hw)
603 dest_hw = dev->broadcast; 509 dest_hw = dev->broadcast;
604 510
605 /* 511 /*
@@ -663,7 +569,7 @@ struct sk_buff *arp_create(int type, int ptype, __be32 dest_ip,
663 break; 569 break;
664#endif 570#endif
665 default: 571 default:
666 if (target_hw != NULL) 572 if (target_hw)
667 memcpy(arp_ptr, target_hw, dev->addr_len); 573 memcpy(arp_ptr, target_hw, dev->addr_len);
668 else 574 else
669 memset(arp_ptr, 0, dev->addr_len); 575 memset(arp_ptr, 0, dev->addr_len);
@@ -685,7 +591,8 @@ EXPORT_SYMBOL(arp_create);
685void arp_xmit(struct sk_buff *skb) 591void arp_xmit(struct sk_buff *skb)
686{ 592{
687 /* Send it off, maybe filter it using firewalling first. */ 593 /* Send it off, maybe filter it using firewalling first. */
688 NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, skb, NULL, skb->dev, dev_queue_xmit); 594 NF_HOOK(NFPROTO_ARP, NF_ARP_OUT, NULL, skb,
595 NULL, skb->dev, dev_queue_xmit_sk);
689} 596}
690EXPORT_SYMBOL(arp_xmit); 597EXPORT_SYMBOL(arp_xmit);
691 598
@@ -708,7 +615,7 @@ void arp_send(int type, int ptype, __be32 dest_ip,
708 615
709 skb = arp_create(type, ptype, dest_ip, dev, src_ip, 616 skb = arp_create(type, ptype, dest_ip, dev, src_ip,
710 dest_hw, src_hw, target_hw); 617 dest_hw, src_hw, target_hw);
711 if (skb == NULL) 618 if (!skb)
712 return; 619 return;
713 620
714 arp_xmit(skb); 621 arp_xmit(skb);
@@ -719,7 +626,7 @@ EXPORT_SYMBOL(arp_send);
719 * Process an arp request. 626 * Process an arp request.
720 */ 627 */
721 628
722static int arp_process(struct sk_buff *skb) 629static int arp_process(struct sock *sk, struct sk_buff *skb)
723{ 630{
724 struct net_device *dev = skb->dev; 631 struct net_device *dev = skb->dev;
725 struct in_device *in_dev = __in_dev_get_rcu(dev); 632 struct in_device *in_dev = __in_dev_get_rcu(dev);
@@ -738,7 +645,7 @@ static int arp_process(struct sk_buff *skb)
738 * is ARP'able. 645 * is ARP'able.
739 */ 646 */
740 647
741 if (in_dev == NULL) 648 if (!in_dev)
742 goto out; 649 goto out;
743 650
744 arp = arp_hdr(skb); 651 arp = arp_hdr(skb);
@@ -902,7 +809,7 @@ static int arp_process(struct sk_buff *skb)
902 is_garp = arp->ar_op == htons(ARPOP_REQUEST) && tip == sip && 809 is_garp = arp->ar_op == htons(ARPOP_REQUEST) && tip == sip &&
903 inet_addr_type(net, sip) == RTN_UNICAST; 810 inet_addr_type(net, sip) == RTN_UNICAST;
904 811
905 if (n == NULL && 812 if (!n &&
906 ((arp->ar_op == htons(ARPOP_REPLY) && 813 ((arp->ar_op == htons(ARPOP_REPLY) &&
907 inet_addr_type(net, sip) == RTN_UNICAST) || is_garp)) 814 inet_addr_type(net, sip) == RTN_UNICAST) || is_garp))
908 n = __neigh_lookup(&arp_tbl, &sip, dev, 1); 815 n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
@@ -940,7 +847,7 @@ out:
940 847
941static void parp_redo(struct sk_buff *skb) 848static void parp_redo(struct sk_buff *skb)
942{ 849{
943 arp_process(skb); 850 arp_process(NULL, skb);
944} 851}
945 852
946 853
@@ -973,7 +880,8 @@ static int arp_rcv(struct sk_buff *skb, struct net_device *dev,
973 880
974 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb)); 881 memset(NEIGH_CB(skb), 0, sizeof(struct neighbour_cb));
975 882
976 return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, skb, dev, NULL, arp_process); 883 return NF_HOOK(NFPROTO_ARP, NF_ARP_IN, NULL, skb,
884 dev, NULL, arp_process);
977 885
978consumeskb: 886consumeskb:
979 consume_skb(skb); 887 consume_skb(skb);
@@ -994,7 +902,7 @@ out_of_mem:
994 902
995static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on) 903static int arp_req_set_proxy(struct net *net, struct net_device *dev, int on)
996{ 904{
997 if (dev == NULL) { 905 if (!dev) {
998 IPV4_DEVCONF_ALL(net, PROXY_ARP) = on; 906 IPV4_DEVCONF_ALL(net, PROXY_ARP) = on;
999 return 0; 907 return 0;
1000 } 908 }
@@ -1020,7 +928,7 @@ static int arp_req_set_public(struct net *net, struct arpreq *r,
1020 return -ENODEV; 928 return -ENODEV;
1021 } 929 }
1022 if (mask) { 930 if (mask) {
1023 if (pneigh_lookup(&arp_tbl, net, &ip, dev, 1) == NULL) 931 if (!pneigh_lookup(&arp_tbl, net, &ip, dev, 1))
1024 return -ENOBUFS; 932 return -ENOBUFS;
1025 return 0; 933 return 0;
1026 } 934 }
@@ -1041,7 +949,7 @@ static int arp_req_set(struct net *net, struct arpreq *r,
1041 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 949 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1042 if (r->arp_flags & ATF_PERM) 950 if (r->arp_flags & ATF_PERM)
1043 r->arp_flags |= ATF_COM; 951 r->arp_flags |= ATF_COM;
1044 if (dev == NULL) { 952 if (!dev) {
1045 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0); 953 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
1046 954
1047 if (IS_ERR(rt)) 955 if (IS_ERR(rt))
@@ -1161,7 +1069,7 @@ static int arp_req_delete(struct net *net, struct arpreq *r,
1161 return arp_req_delete_public(net, r, dev); 1069 return arp_req_delete_public(net, r, dev);
1162 1070
1163 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr; 1071 ip = ((struct sockaddr_in *)&r->arp_pa)->sin_addr.s_addr;
1164 if (dev == NULL) { 1072 if (!dev) {
1165 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0); 1073 struct rtable *rt = ip_route_output(net, ip, 0, RTO_ONLINK, 0);
1166 if (IS_ERR(rt)) 1074 if (IS_ERR(rt))
1167 return PTR_ERR(rt); 1075 return PTR_ERR(rt);
@@ -1210,7 +1118,7 @@ int arp_ioctl(struct net *net, unsigned int cmd, void __user *arg)
1210 if (r.arp_dev[0]) { 1118 if (r.arp_dev[0]) {
1211 err = -ENODEV; 1119 err = -ENODEV;
1212 dev = __dev_get_by_name(net, r.arp_dev); 1120 dev = __dev_get_by_name(net, r.arp_dev);
1213 if (dev == NULL) 1121 if (!dev)
1214 goto out; 1122 goto out;
1215 1123
1216 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */ 1124 /* Mmmm... It is wrong... ARPHRD_NETROM==0 */
diff --git a/net/ipv4/cipso_ipv4.c b/net/ipv4/cipso_ipv4.c
index e361ea6f3fc8..bdb2a07ec363 100644
--- a/net/ipv4/cipso_ipv4.c
+++ b/net/ipv4/cipso_ipv4.c
@@ -255,7 +255,7 @@ static int __init cipso_v4_cache_init(void)
255 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS, 255 cipso_v4_cache = kcalloc(CIPSO_V4_CACHE_BUCKETS,
256 sizeof(struct cipso_v4_map_cache_bkt), 256 sizeof(struct cipso_v4_map_cache_bkt),
257 GFP_KERNEL); 257 GFP_KERNEL);
258 if (cipso_v4_cache == NULL) 258 if (!cipso_v4_cache)
259 return -ENOMEM; 259 return -ENOMEM;
260 260
261 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) { 261 for (iter = 0; iter < CIPSO_V4_CACHE_BUCKETS; iter++) {
@@ -339,7 +339,7 @@ static int cipso_v4_cache_check(const unsigned char *key,
339 secattr->cache = entry->lsm_data; 339 secattr->cache = entry->lsm_data;
340 secattr->flags |= NETLBL_SECATTR_CACHE; 340 secattr->flags |= NETLBL_SECATTR_CACHE;
341 secattr->type = NETLBL_NLTYPE_CIPSOV4; 341 secattr->type = NETLBL_NLTYPE_CIPSOV4;
342 if (prev_entry == NULL) { 342 if (!prev_entry) {
343 spin_unlock_bh(&cipso_v4_cache[bkt].lock); 343 spin_unlock_bh(&cipso_v4_cache[bkt].lock);
344 return 0; 344 return 0;
345 } 345 }
@@ -393,10 +393,10 @@ int cipso_v4_cache_add(const unsigned char *cipso_ptr,
393 cipso_ptr_len = cipso_ptr[1]; 393 cipso_ptr_len = cipso_ptr[1];
394 394
395 entry = kzalloc(sizeof(*entry), GFP_ATOMIC); 395 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
396 if (entry == NULL) 396 if (!entry)
397 return -ENOMEM; 397 return -ENOMEM;
398 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC); 398 entry->key = kmemdup(cipso_ptr, cipso_ptr_len, GFP_ATOMIC);
399 if (entry->key == NULL) { 399 if (!entry->key) {
400 ret_val = -ENOMEM; 400 ret_val = -ENOMEM;
401 goto cache_add_failure; 401 goto cache_add_failure;
402 } 402 }
@@ -502,7 +502,7 @@ int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
502 atomic_set(&doi_def->refcount, 1); 502 atomic_set(&doi_def->refcount, 1);
503 503
504 spin_lock(&cipso_v4_doi_list_lock); 504 spin_lock(&cipso_v4_doi_list_lock);
505 if (cipso_v4_doi_search(doi_def->doi) != NULL) { 505 if (cipso_v4_doi_search(doi_def->doi)) {
506 spin_unlock(&cipso_v4_doi_list_lock); 506 spin_unlock(&cipso_v4_doi_list_lock);
507 ret_val = -EEXIST; 507 ret_val = -EEXIST;
508 goto doi_add_return; 508 goto doi_add_return;
@@ -513,7 +513,7 @@ int cipso_v4_doi_add(struct cipso_v4_doi *doi_def,
513 513
514doi_add_return: 514doi_add_return:
515 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info); 515 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_ADD, audit_info);
516 if (audit_buf != NULL) { 516 if (audit_buf) {
517 const char *type_str; 517 const char *type_str;
518 switch (doi_type) { 518 switch (doi_type) {
519 case CIPSO_V4_MAP_TRANS: 519 case CIPSO_V4_MAP_TRANS:
@@ -547,7 +547,7 @@ doi_add_return:
547 */ 547 */
548void cipso_v4_doi_free(struct cipso_v4_doi *doi_def) 548void cipso_v4_doi_free(struct cipso_v4_doi *doi_def)
549{ 549{
550 if (doi_def == NULL) 550 if (!doi_def)
551 return; 551 return;
552 552
553 switch (doi_def->type) { 553 switch (doi_def->type) {
@@ -598,7 +598,7 @@ int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
598 598
599 spin_lock(&cipso_v4_doi_list_lock); 599 spin_lock(&cipso_v4_doi_list_lock);
600 doi_def = cipso_v4_doi_search(doi); 600 doi_def = cipso_v4_doi_search(doi);
601 if (doi_def == NULL) { 601 if (!doi_def) {
602 spin_unlock(&cipso_v4_doi_list_lock); 602 spin_unlock(&cipso_v4_doi_list_lock);
603 ret_val = -ENOENT; 603 ret_val = -ENOENT;
604 goto doi_remove_return; 604 goto doi_remove_return;
@@ -617,7 +617,7 @@ int cipso_v4_doi_remove(u32 doi, struct netlbl_audit *audit_info)
617 617
618doi_remove_return: 618doi_remove_return:
619 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info); 619 audit_buf = netlbl_audit_start(AUDIT_MAC_CIPSOV4_DEL, audit_info);
620 if (audit_buf != NULL) { 620 if (audit_buf) {
621 audit_log_format(audit_buf, 621 audit_log_format(audit_buf,
622 " cipso_doi=%u res=%u", 622 " cipso_doi=%u res=%u",
623 doi, ret_val == 0 ? 1 : 0); 623 doi, ret_val == 0 ? 1 : 0);
@@ -644,7 +644,7 @@ struct cipso_v4_doi *cipso_v4_doi_getdef(u32 doi)
644 644
645 rcu_read_lock(); 645 rcu_read_lock();
646 doi_def = cipso_v4_doi_search(doi); 646 doi_def = cipso_v4_doi_search(doi);
647 if (doi_def == NULL) 647 if (!doi_def)
648 goto doi_getdef_return; 648 goto doi_getdef_return;
649 if (!atomic_inc_not_zero(&doi_def->refcount)) 649 if (!atomic_inc_not_zero(&doi_def->refcount))
650 doi_def = NULL; 650 doi_def = NULL;
@@ -664,7 +664,7 @@ doi_getdef_return:
664 */ 664 */
665void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def) 665void cipso_v4_doi_putdef(struct cipso_v4_doi *doi_def)
666{ 666{
667 if (doi_def == NULL) 667 if (!doi_def)
668 return; 668 return;
669 669
670 if (!atomic_dec_and_test(&doi_def->refcount)) 670 if (!atomic_dec_and_test(&doi_def->refcount))
@@ -1642,7 +1642,7 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1642 1642
1643 rcu_read_lock(); 1643 rcu_read_lock();
1644 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2])); 1644 doi_def = cipso_v4_doi_search(get_unaligned_be32(&opt[2]));
1645 if (doi_def == NULL) { 1645 if (!doi_def) {
1646 err_offset = 2; 1646 err_offset = 2;
1647 goto validate_return_locked; 1647 goto validate_return_locked;
1648 } 1648 }
@@ -1736,7 +1736,7 @@ int cipso_v4_validate(const struct sk_buff *skb, unsigned char **option)
1736 * not the loopback device drop the packet. Further, 1736 * not the loopback device drop the packet. Further,
1737 * there is no legitimate reason for setting this from 1737 * there is no legitimate reason for setting this from
1738 * userspace so reject it if skb is NULL. */ 1738 * userspace so reject it if skb is NULL. */
1739 if (skb == NULL || !(skb->dev->flags & IFF_LOOPBACK)) { 1739 if (!skb || !(skb->dev->flags & IFF_LOOPBACK)) {
1740 err_offset = opt_iter; 1740 err_offset = opt_iter;
1741 goto validate_return_locked; 1741 goto validate_return_locked;
1742 } 1742 }
@@ -1897,7 +1897,7 @@ int cipso_v4_sock_setattr(struct sock *sk,
1897 * defined yet but it is not a problem as the only users of these 1897 * defined yet but it is not a problem as the only users of these
1898 * "lite" PF_INET sockets are functions which do an accept() call 1898 * "lite" PF_INET sockets are functions which do an accept() call
1899 * afterwards so we will label the socket as part of the accept(). */ 1899 * afterwards so we will label the socket as part of the accept(). */
1900 if (sk == NULL) 1900 if (!sk)
1901 return 0; 1901 return 0;
1902 1902
1903 /* We allocate the maximum CIPSO option size here so we are probably 1903 /* We allocate the maximum CIPSO option size here so we are probably
@@ -1905,7 +1905,7 @@ int cipso_v4_sock_setattr(struct sock *sk,
1905 * on and after all we are only talking about 40 bytes. */ 1905 * on and after all we are only talking about 40 bytes. */
1906 buf_len = CIPSO_V4_OPT_LEN_MAX; 1906 buf_len = CIPSO_V4_OPT_LEN_MAX;
1907 buf = kmalloc(buf_len, GFP_ATOMIC); 1907 buf = kmalloc(buf_len, GFP_ATOMIC);
1908 if (buf == NULL) { 1908 if (!buf) {
1909 ret_val = -ENOMEM; 1909 ret_val = -ENOMEM;
1910 goto socket_setattr_failure; 1910 goto socket_setattr_failure;
1911 } 1911 }
@@ -1921,7 +1921,7 @@ int cipso_v4_sock_setattr(struct sock *sk,
1921 * set the IPOPT_CIPSO option. */ 1921 * set the IPOPT_CIPSO option. */
1922 opt_len = (buf_len + 3) & ~3; 1922 opt_len = (buf_len + 3) & ~3;
1923 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 1923 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
1924 if (opt == NULL) { 1924 if (!opt) {
1925 ret_val = -ENOMEM; 1925 ret_val = -ENOMEM;
1926 goto socket_setattr_failure; 1926 goto socket_setattr_failure;
1927 } 1927 }
@@ -1981,7 +1981,7 @@ int cipso_v4_req_setattr(struct request_sock *req,
1981 * on and after all we are only talking about 40 bytes. */ 1981 * on and after all we are only talking about 40 bytes. */
1982 buf_len = CIPSO_V4_OPT_LEN_MAX; 1982 buf_len = CIPSO_V4_OPT_LEN_MAX;
1983 buf = kmalloc(buf_len, GFP_ATOMIC); 1983 buf = kmalloc(buf_len, GFP_ATOMIC);
1984 if (buf == NULL) { 1984 if (!buf) {
1985 ret_val = -ENOMEM; 1985 ret_val = -ENOMEM;
1986 goto req_setattr_failure; 1986 goto req_setattr_failure;
1987 } 1987 }
@@ -1997,7 +1997,7 @@ int cipso_v4_req_setattr(struct request_sock *req,
1997 * set the IPOPT_CIPSO option. */ 1997 * set the IPOPT_CIPSO option. */
1998 opt_len = (buf_len + 3) & ~3; 1998 opt_len = (buf_len + 3) & ~3;
1999 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC); 1999 opt = kzalloc(sizeof(*opt) + opt_len, GFP_ATOMIC);
2000 if (opt == NULL) { 2000 if (!opt) {
2001 ret_val = -ENOMEM; 2001 ret_val = -ENOMEM;
2002 goto req_setattr_failure; 2002 goto req_setattr_failure;
2003 } 2003 }
@@ -2102,7 +2102,7 @@ void cipso_v4_sock_delattr(struct sock *sk)
2102 2102
2103 sk_inet = inet_sk(sk); 2103 sk_inet = inet_sk(sk);
2104 opt = rcu_dereference_protected(sk_inet->inet_opt, 1); 2104 opt = rcu_dereference_protected(sk_inet->inet_opt, 1);
2105 if (opt == NULL || opt->opt.cipso == 0) 2105 if (!opt || opt->opt.cipso == 0)
2106 return; 2106 return;
2107 2107
2108 hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt); 2108 hdr_delta = cipso_v4_delopt(&sk_inet->inet_opt);
@@ -2128,7 +2128,7 @@ void cipso_v4_req_delattr(struct request_sock *req)
2128 2128
2129 req_inet = inet_rsk(req); 2129 req_inet = inet_rsk(req);
2130 opt = req_inet->opt; 2130 opt = req_inet->opt;
2131 if (opt == NULL || opt->opt.cipso == 0) 2131 if (!opt || opt->opt.cipso == 0)
2132 return; 2132 return;
2133 2133
2134 cipso_v4_delopt(&req_inet->opt); 2134 cipso_v4_delopt(&req_inet->opt);
@@ -2157,7 +2157,7 @@ int cipso_v4_getattr(const unsigned char *cipso,
2157 doi = get_unaligned_be32(&cipso[2]); 2157 doi = get_unaligned_be32(&cipso[2]);
2158 rcu_read_lock(); 2158 rcu_read_lock();
2159 doi_def = cipso_v4_doi_search(doi); 2159 doi_def = cipso_v4_doi_search(doi);
2160 if (doi_def == NULL) 2160 if (!doi_def)
2161 goto getattr_return; 2161 goto getattr_return;
2162 /* XXX - This code assumes only one tag per CIPSO option which isn't 2162 /* XXX - This code assumes only one tag per CIPSO option which isn't
2163 * really a good assumption to make but since we only support the MAC 2163 * really a good assumption to make but since we only support the MAC
diff --git a/net/ipv4/devinet.c b/net/ipv4/devinet.c
index 3a8985c94581..419d23c53ec7 100644
--- a/net/ipv4/devinet.c
+++ b/net/ipv4/devinet.c
@@ -107,7 +107,7 @@ static const struct nla_policy ifa_ipv4_policy[IFA_MAX+1] = {
107 107
108static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE]; 108static struct hlist_head inet_addr_lst[IN4_ADDR_HSIZE];
109 109
110static u32 inet_addr_hash(struct net *net, __be32 addr) 110static u32 inet_addr_hash(const struct net *net, __be32 addr)
111{ 111{
112 u32 val = (__force u32) addr ^ net_hash_mix(net); 112 u32 val = (__force u32) addr ^ net_hash_mix(net);
113 113
@@ -548,6 +548,26 @@ struct in_ifaddr *inet_ifa_byprefix(struct in_device *in_dev, __be32 prefix,
548 return NULL; 548 return NULL;
549} 549}
550 550
551static int ip_mc_config(struct sock *sk, bool join, const struct in_ifaddr *ifa)
552{
553 struct ip_mreqn mreq = {
554 .imr_multiaddr.s_addr = ifa->ifa_address,
555 .imr_ifindex = ifa->ifa_dev->dev->ifindex,
556 };
557 int ret;
558
559 ASSERT_RTNL();
560
561 lock_sock(sk);
562 if (join)
563 ret = ip_mc_join_group(sk, &mreq);
564 else
565 ret = ip_mc_leave_group(sk, &mreq);
566 release_sock(sk);
567
568 return ret;
569}
570
551static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh) 571static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
552{ 572{
553 struct net *net = sock_net(skb->sk); 573 struct net *net = sock_net(skb->sk);
@@ -565,7 +585,7 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
565 585
566 ifm = nlmsg_data(nlh); 586 ifm = nlmsg_data(nlh);
567 in_dev = inetdev_by_index(net, ifm->ifa_index); 587 in_dev = inetdev_by_index(net, ifm->ifa_index);
568 if (in_dev == NULL) { 588 if (!in_dev) {
569 err = -ENODEV; 589 err = -ENODEV;
570 goto errout; 590 goto errout;
571 } 591 }
@@ -573,7 +593,7 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
573 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL; 593 for (ifap = &in_dev->ifa_list; (ifa = *ifap) != NULL;
574 ifap = &ifa->ifa_next) { 594 ifap = &ifa->ifa_next) {
575 if (tb[IFA_LOCAL] && 595 if (tb[IFA_LOCAL] &&
576 ifa->ifa_local != nla_get_be32(tb[IFA_LOCAL])) 596 ifa->ifa_local != nla_get_in_addr(tb[IFA_LOCAL]))
577 continue; 597 continue;
578 598
579 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label)) 599 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
@@ -581,9 +601,11 @@ static int inet_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
581 601
582 if (tb[IFA_ADDRESS] && 602 if (tb[IFA_ADDRESS] &&
583 (ifm->ifa_prefixlen != ifa->ifa_prefixlen || 603 (ifm->ifa_prefixlen != ifa->ifa_prefixlen ||
584 !inet_ifa_match(nla_get_be32(tb[IFA_ADDRESS]), ifa))) 604 !inet_ifa_match(nla_get_in_addr(tb[IFA_ADDRESS]), ifa)))
585 continue; 605 continue;
586 606
607 if (ipv4_is_multicast(ifa->ifa_address))
608 ip_mc_config(net->ipv4.mc_autojoin_sk, false, ifa);
587 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid); 609 __inet_del_ifa(in_dev, ifap, 1, nlh, NETLINK_CB(skb).portid);
588 return 0; 610 return 0;
589 } 611 }
@@ -733,21 +755,21 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
733 755
734 ifm = nlmsg_data(nlh); 756 ifm = nlmsg_data(nlh);
735 err = -EINVAL; 757 err = -EINVAL;
736 if (ifm->ifa_prefixlen > 32 || tb[IFA_LOCAL] == NULL) 758 if (ifm->ifa_prefixlen > 32 || !tb[IFA_LOCAL])
737 goto errout; 759 goto errout;
738 760
739 dev = __dev_get_by_index(net, ifm->ifa_index); 761 dev = __dev_get_by_index(net, ifm->ifa_index);
740 err = -ENODEV; 762 err = -ENODEV;
741 if (dev == NULL) 763 if (!dev)
742 goto errout; 764 goto errout;
743 765
744 in_dev = __in_dev_get_rtnl(dev); 766 in_dev = __in_dev_get_rtnl(dev);
745 err = -ENOBUFS; 767 err = -ENOBUFS;
746 if (in_dev == NULL) 768 if (!in_dev)
747 goto errout; 769 goto errout;
748 770
749 ifa = inet_alloc_ifa(); 771 ifa = inet_alloc_ifa();
750 if (ifa == NULL) 772 if (!ifa)
751 /* 773 /*
752 * A potential indev allocation can be left alive, it stays 774 * A potential indev allocation can be left alive, it stays
753 * assigned to its device and is destroy with it. 775 * assigned to its device and is destroy with it.
@@ -758,7 +780,7 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
758 neigh_parms_data_state_setall(in_dev->arp_parms); 780 neigh_parms_data_state_setall(in_dev->arp_parms);
759 in_dev_hold(in_dev); 781 in_dev_hold(in_dev);
760 782
761 if (tb[IFA_ADDRESS] == NULL) 783 if (!tb[IFA_ADDRESS])
762 tb[IFA_ADDRESS] = tb[IFA_LOCAL]; 784 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
763 785
764 INIT_HLIST_NODE(&ifa->hash); 786 INIT_HLIST_NODE(&ifa->hash);
@@ -769,11 +791,11 @@ static struct in_ifaddr *rtm_to_ifaddr(struct net *net, struct nlmsghdr *nlh,
769 ifa->ifa_scope = ifm->ifa_scope; 791 ifa->ifa_scope = ifm->ifa_scope;
770 ifa->ifa_dev = in_dev; 792 ifa->ifa_dev = in_dev;
771 793
772 ifa->ifa_local = nla_get_be32(tb[IFA_LOCAL]); 794 ifa->ifa_local = nla_get_in_addr(tb[IFA_LOCAL]);
773 ifa->ifa_address = nla_get_be32(tb[IFA_ADDRESS]); 795 ifa->ifa_address = nla_get_in_addr(tb[IFA_ADDRESS]);
774 796
775 if (tb[IFA_BROADCAST]) 797 if (tb[IFA_BROADCAST])
776 ifa->ifa_broadcast = nla_get_be32(tb[IFA_BROADCAST]); 798 ifa->ifa_broadcast = nla_get_in_addr(tb[IFA_BROADCAST]);
777 799
778 if (tb[IFA_LABEL]) 800 if (tb[IFA_LABEL])
779 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ); 801 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
@@ -838,6 +860,15 @@ static int inet_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
838 * userspace already relies on not having to provide this. 860 * userspace already relies on not having to provide this.
839 */ 861 */
840 set_ifa_lifetime(ifa, valid_lft, prefered_lft); 862 set_ifa_lifetime(ifa, valid_lft, prefered_lft);
863 if (ifa->ifa_flags & IFA_F_MCAUTOJOIN) {
864 int ret = ip_mc_config(net->ipv4.mc_autojoin_sk,
865 true, ifa);
866
867 if (ret < 0) {
868 inet_free_ifa(ifa);
869 return ret;
870 }
871 }
841 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid); 872 return __inet_insert_ifa(ifa, nlh, NETLINK_CB(skb).portid);
842 } else { 873 } else {
843 inet_free_ifa(ifa); 874 inet_free_ifa(ifa);
@@ -1259,7 +1290,7 @@ __be32 inet_confirm_addr(struct net *net, struct in_device *in_dev,
1259 __be32 addr = 0; 1290 __be32 addr = 0;
1260 struct net_device *dev; 1291 struct net_device *dev;
1261 1292
1262 if (in_dev != NULL) 1293 if (in_dev)
1263 return confirm_addr_indev(in_dev, dst, local, scope); 1294 return confirm_addr_indev(in_dev, dst, local, scope);
1264 1295
1265 rcu_read_lock(); 1296 rcu_read_lock();
@@ -1309,7 +1340,7 @@ static void inetdev_changename(struct net_device *dev, struct in_device *in_dev)
1309 if (named++ == 0) 1340 if (named++ == 0)
1310 goto skip; 1341 goto skip;
1311 dot = strchr(old, ':'); 1342 dot = strchr(old, ':');
1312 if (dot == NULL) { 1343 if (!dot) {
1313 sprintf(old, ":%d", named); 1344 sprintf(old, ":%d", named);
1314 dot = old; 1345 dot = old;
1315 } 1346 }
@@ -1478,7 +1509,7 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1478 u32 preferred, valid; 1509 u32 preferred, valid;
1479 1510
1480 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags); 1511 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
1481 if (nlh == NULL) 1512 if (!nlh)
1482 return -EMSGSIZE; 1513 return -EMSGSIZE;
1483 1514
1484 ifm = nlmsg_data(nlh); 1515 ifm = nlmsg_data(nlh);
@@ -1510,11 +1541,11 @@ static int inet_fill_ifaddr(struct sk_buff *skb, struct in_ifaddr *ifa,
1510 valid = INFINITY_LIFE_TIME; 1541 valid = INFINITY_LIFE_TIME;
1511 } 1542 }
1512 if ((ifa->ifa_address && 1543 if ((ifa->ifa_address &&
1513 nla_put_be32(skb, IFA_ADDRESS, ifa->ifa_address)) || 1544 nla_put_in_addr(skb, IFA_ADDRESS, ifa->ifa_address)) ||
1514 (ifa->ifa_local && 1545 (ifa->ifa_local &&
1515 nla_put_be32(skb, IFA_LOCAL, ifa->ifa_local)) || 1546 nla_put_in_addr(skb, IFA_LOCAL, ifa->ifa_local)) ||
1516 (ifa->ifa_broadcast && 1547 (ifa->ifa_broadcast &&
1517 nla_put_be32(skb, IFA_BROADCAST, ifa->ifa_broadcast)) || 1548 nla_put_in_addr(skb, IFA_BROADCAST, ifa->ifa_broadcast)) ||
1518 (ifa->ifa_label[0] && 1549 (ifa->ifa_label[0] &&
1519 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) || 1550 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
1520 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) || 1551 nla_put_u32(skb, IFA_FLAGS, ifa->ifa_flags) ||
@@ -1597,7 +1628,7 @@ static void rtmsg_ifa(int event, struct in_ifaddr *ifa, struct nlmsghdr *nlh,
1597 1628
1598 net = dev_net(ifa->ifa_dev->dev); 1629 net = dev_net(ifa->ifa_dev->dev);
1599 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL); 1630 skb = nlmsg_new(inet_nlmsg_size(), GFP_KERNEL);
1600 if (skb == NULL) 1631 if (!skb)
1601 goto errout; 1632 goto errout;
1602 1633
1603 err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0); 1634 err = inet_fill_ifaddr(skb, ifa, portid, seq, event, 0);
@@ -1634,7 +1665,7 @@ static int inet_fill_link_af(struct sk_buff *skb, const struct net_device *dev)
1634 return -ENODATA; 1665 return -ENODATA;
1635 1666
1636 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4); 1667 nla = nla_reserve(skb, IFLA_INET_CONF, IPV4_DEVCONF_MAX * 4);
1637 if (nla == NULL) 1668 if (!nla)
1638 return -EMSGSIZE; 1669 return -EMSGSIZE;
1639 1670
1640 for (i = 0; i < IPV4_DEVCONF_MAX; i++) 1671 for (i = 0; i < IPV4_DEVCONF_MAX; i++)
@@ -1723,7 +1754,7 @@ static int inet_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
1723 1754
1724 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 1755 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
1725 flags); 1756 flags);
1726 if (nlh == NULL) 1757 if (!nlh)
1727 return -EMSGSIZE; 1758 return -EMSGSIZE;
1728 1759
1729 ncm = nlmsg_data(nlh); 1760 ncm = nlmsg_data(nlh);
@@ -1765,7 +1796,7 @@ void inet_netconf_notify_devconf(struct net *net, int type, int ifindex,
1765 int err = -ENOBUFS; 1796 int err = -ENOBUFS;
1766 1797
1767 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_ATOMIC); 1798 skb = nlmsg_new(inet_netconf_msgsize_devconf(type), GFP_ATOMIC);
1768 if (skb == NULL) 1799 if (!skb)
1769 goto errout; 1800 goto errout;
1770 1801
1771 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 1802 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
@@ -1822,10 +1853,10 @@ static int inet_netconf_get_devconf(struct sk_buff *in_skb,
1822 break; 1853 break;
1823 default: 1854 default:
1824 dev = __dev_get_by_index(net, ifindex); 1855 dev = __dev_get_by_index(net, ifindex);
1825 if (dev == NULL) 1856 if (!dev)
1826 goto errout; 1857 goto errout;
1827 in_dev = __in_dev_get_rtnl(dev); 1858 in_dev = __in_dev_get_rtnl(dev);
1828 if (in_dev == NULL) 1859 if (!in_dev)
1829 goto errout; 1860 goto errout;
1830 devconf = &in_dev->cnf; 1861 devconf = &in_dev->cnf;
1831 break; 1862 break;
@@ -1833,7 +1864,7 @@ static int inet_netconf_get_devconf(struct sk_buff *in_skb,
1833 1864
1834 err = -ENOBUFS; 1865 err = -ENOBUFS;
1835 skb = nlmsg_new(inet_netconf_msgsize_devconf(-1), GFP_ATOMIC); 1866 skb = nlmsg_new(inet_netconf_msgsize_devconf(-1), GFP_ATOMIC);
1836 if (skb == NULL) 1867 if (!skb)
1837 goto errout; 1868 goto errout;
1838 1869
1839 err = inet_netconf_fill_devconf(skb, ifindex, devconf, 1870 err = inet_netconf_fill_devconf(skb, ifindex, devconf,
@@ -2184,7 +2215,7 @@ static void __devinet_sysctl_unregister(struct ipv4_devconf *cnf)
2184{ 2215{
2185 struct devinet_sysctl_table *t = cnf->sysctl; 2216 struct devinet_sysctl_table *t = cnf->sysctl;
2186 2217
2187 if (t == NULL) 2218 if (!t)
2188 return; 2219 return;
2189 2220
2190 cnf->sysctl = NULL; 2221 cnf->sysctl = NULL;
@@ -2245,16 +2276,16 @@ static __net_init int devinet_init_net(struct net *net)
2245 2276
2246 if (!net_eq(net, &init_net)) { 2277 if (!net_eq(net, &init_net)) {
2247 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL); 2278 all = kmemdup(all, sizeof(ipv4_devconf), GFP_KERNEL);
2248 if (all == NULL) 2279 if (!all)
2249 goto err_alloc_all; 2280 goto err_alloc_all;
2250 2281
2251 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL); 2282 dflt = kmemdup(dflt, sizeof(ipv4_devconf_dflt), GFP_KERNEL);
2252 if (dflt == NULL) 2283 if (!dflt)
2253 goto err_alloc_dflt; 2284 goto err_alloc_dflt;
2254 2285
2255#ifdef CONFIG_SYSCTL 2286#ifdef CONFIG_SYSCTL
2256 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL); 2287 tbl = kmemdup(tbl, sizeof(ctl_forward_entry), GFP_KERNEL);
2257 if (tbl == NULL) 2288 if (!tbl)
2258 goto err_alloc_ctl; 2289 goto err_alloc_ctl;
2259 2290
2260 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1]; 2291 tbl[0].data = &all->data[IPV4_DEVCONF_FORWARDING - 1];
@@ -2274,7 +2305,7 @@ static __net_init int devinet_init_net(struct net *net)
2274 2305
2275 err = -ENOMEM; 2306 err = -ENOMEM;
2276 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl); 2307 forw_hdr = register_net_sysctl(net, "net/ipv4", tbl);
2277 if (forw_hdr == NULL) 2308 if (!forw_hdr)
2278 goto err_reg_ctl; 2309 goto err_reg_ctl;
2279 net->ipv4.forw_hdr = forw_hdr; 2310 net->ipv4.forw_hdr = forw_hdr;
2280#endif 2311#endif
diff --git a/net/ipv4/esp4.c b/net/ipv4/esp4.c
index 60173d4d3a0e..421a80b09b62 100644
--- a/net/ipv4/esp4.c
+++ b/net/ipv4/esp4.c
@@ -553,7 +553,7 @@ static int esp_init_authenc(struct xfrm_state *x)
553 int err; 553 int err;
554 554
555 err = -EINVAL; 555 err = -EINVAL;
556 if (x->ealg == NULL) 556 if (!x->ealg)
557 goto error; 557 goto error;
558 558
559 err = -ENAMETOOLONG; 559 err = -ENAMETOOLONG;
diff --git a/net/ipv4/fib_frontend.c b/net/ipv4/fib_frontend.c
index 23b9b3e86f4c..872494e6e6eb 100644
--- a/net/ipv4/fib_frontend.c
+++ b/net/ipv4/fib_frontend.c
@@ -52,12 +52,12 @@ static int __net_init fib4_rules_init(struct net *net)
52{ 52{
53 struct fib_table *local_table, *main_table; 53 struct fib_table *local_table, *main_table;
54 54
55 local_table = fib_trie_table(RT_TABLE_LOCAL); 55 main_table = fib_trie_table(RT_TABLE_MAIN, NULL);
56 if (local_table == NULL) 56 if (!main_table)
57 return -ENOMEM; 57 return -ENOMEM;
58 58
59 main_table = fib_trie_table(RT_TABLE_MAIN); 59 local_table = fib_trie_table(RT_TABLE_LOCAL, main_table);
60 if (main_table == NULL) 60 if (!local_table)
61 goto fail; 61 goto fail;
62 62
63 hlist_add_head_rcu(&local_table->tb_hlist, 63 hlist_add_head_rcu(&local_table->tb_hlist,
@@ -67,14 +67,14 @@ static int __net_init fib4_rules_init(struct net *net)
67 return 0; 67 return 0;
68 68
69fail: 69fail:
70 fib_free_table(local_table); 70 fib_free_table(main_table);
71 return -ENOMEM; 71 return -ENOMEM;
72} 72}
73#else 73#else
74 74
75struct fib_table *fib_new_table(struct net *net, u32 id) 75struct fib_table *fib_new_table(struct net *net, u32 id)
76{ 76{
77 struct fib_table *tb; 77 struct fib_table *tb, *alias = NULL;
78 unsigned int h; 78 unsigned int h;
79 79
80 if (id == 0) 80 if (id == 0)
@@ -83,23 +83,23 @@ struct fib_table *fib_new_table(struct net *net, u32 id)
83 if (tb) 83 if (tb)
84 return tb; 84 return tb;
85 85
86 tb = fib_trie_table(id); 86 if (id == RT_TABLE_LOCAL)
87 alias = fib_new_table(net, RT_TABLE_MAIN);
88
89 tb = fib_trie_table(id, alias);
87 if (!tb) 90 if (!tb)
88 return NULL; 91 return NULL;
89 92
90 switch (id) { 93 switch (id) {
91 case RT_TABLE_LOCAL: 94 case RT_TABLE_LOCAL:
92 net->ipv4.fib_local = tb; 95 rcu_assign_pointer(net->ipv4.fib_local, tb);
93 break; 96 break;
94
95 case RT_TABLE_MAIN: 97 case RT_TABLE_MAIN:
96 net->ipv4.fib_main = tb; 98 rcu_assign_pointer(net->ipv4.fib_main, tb);
97 break; 99 break;
98
99 case RT_TABLE_DEFAULT: 100 case RT_TABLE_DEFAULT:
100 net->ipv4.fib_default = tb; 101 rcu_assign_pointer(net->ipv4.fib_default, tb);
101 break; 102 break;
102
103 default: 103 default:
104 break; 104 break;
105 } 105 }
@@ -129,16 +129,62 @@ struct fib_table *fib_get_table(struct net *net, u32 id)
129} 129}
130#endif /* CONFIG_IP_MULTIPLE_TABLES */ 130#endif /* CONFIG_IP_MULTIPLE_TABLES */
131 131
132static void fib_replace_table(struct net *net, struct fib_table *old,
133 struct fib_table *new)
134{
135#ifdef CONFIG_IP_MULTIPLE_TABLES
136 switch (new->tb_id) {
137 case RT_TABLE_LOCAL:
138 rcu_assign_pointer(net->ipv4.fib_local, new);
139 break;
140 case RT_TABLE_MAIN:
141 rcu_assign_pointer(net->ipv4.fib_main, new);
142 break;
143 case RT_TABLE_DEFAULT:
144 rcu_assign_pointer(net->ipv4.fib_default, new);
145 break;
146 default:
147 break;
148 }
149
150#endif
151 /* replace the old table in the hlist */
152 hlist_replace_rcu(&old->tb_hlist, &new->tb_hlist);
153}
154
155int fib_unmerge(struct net *net)
156{
157 struct fib_table *old, *new;
158
159 /* attempt to fetch local table if it has been allocated */
160 old = fib_get_table(net, RT_TABLE_LOCAL);
161 if (!old)
162 return 0;
163
164 new = fib_trie_unmerge(old);
165 if (!new)
166 return -ENOMEM;
167
168 /* replace merged table with clean table */
169 if (new != old) {
170 fib_replace_table(net, old, new);
171 fib_free_table(old);
172 }
173
174 return 0;
175}
176
132static void fib_flush(struct net *net) 177static void fib_flush(struct net *net)
133{ 178{
134 int flushed = 0; 179 int flushed = 0;
135 struct fib_table *tb;
136 struct hlist_head *head;
137 unsigned int h; 180 unsigned int h;
138 181
139 for (h = 0; h < FIB_TABLE_HASHSZ; h++) { 182 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
140 head = &net->ipv4.fib_table_hash[h]; 183 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
141 hlist_for_each_entry(tb, head, tb_hlist) 184 struct hlist_node *tmp;
185 struct fib_table *tb;
186
187 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist)
142 flushed += fib_table_flush(tb); 188 flushed += fib_table_flush(tb);
143 } 189 }
144 190
@@ -146,6 +192,19 @@ static void fib_flush(struct net *net)
146 rt_cache_flush(net); 192 rt_cache_flush(net);
147} 193}
148 194
195void fib_flush_external(struct net *net)
196{
197 struct fib_table *tb;
198 struct hlist_head *head;
199 unsigned int h;
200
201 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
202 head = &net->ipv4.fib_table_hash[h];
203 hlist_for_each_entry(tb, head, tb_hlist)
204 fib_table_flush_external(tb);
205 }
206}
207
149/* 208/*
150 * Find address type as if only "dev" was present in the system. If 209 * Find address type as if only "dev" was present in the system. If
151 * on_dev is NULL then all interfaces are taken into consideration. 210 * on_dev is NULL then all interfaces are taken into consideration.
@@ -427,7 +486,7 @@ static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
427 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) 486 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next)
428 if (strcmp(ifa->ifa_label, devname) == 0) 487 if (strcmp(ifa->ifa_label, devname) == 0)
429 break; 488 break;
430 if (ifa == NULL) 489 if (!ifa)
431 return -ENODEV; 490 return -ENODEV;
432 cfg->fc_prefsrc = ifa->ifa_local; 491 cfg->fc_prefsrc = ifa->ifa_local;
433 } 492 }
@@ -455,7 +514,7 @@ static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
455 int len = 0; 514 int len = 0;
456 515
457 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL); 516 mx = kzalloc(3 * nla_total_size(4), GFP_KERNEL);
458 if (mx == NULL) 517 if (!mx)
459 return -ENOMEM; 518 return -ENOMEM;
460 519
461 if (rt->rt_flags & RTF_MTU) 520 if (rt->rt_flags & RTF_MTU)
@@ -617,7 +676,7 @@ static int inet_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
617 goto errout; 676 goto errout;
618 677
619 tb = fib_get_table(net, cfg.fc_table); 678 tb = fib_get_table(net, cfg.fc_table);
620 if (tb == NULL) { 679 if (!tb) {
621 err = -ESRCH; 680 err = -ESRCH;
622 goto errout; 681 goto errout;
623 } 682 }
@@ -639,7 +698,7 @@ static int inet_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
639 goto errout; 698 goto errout;
640 699
641 tb = fib_new_table(net, cfg.fc_table); 700 tb = fib_new_table(net, cfg.fc_table);
642 if (tb == NULL) { 701 if (!tb) {
643 err = -ENOBUFS; 702 err = -ENOBUFS;
644 goto errout; 703 goto errout;
645 } 704 }
@@ -665,10 +724,12 @@ static int inet_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
665 s_h = cb->args[0]; 724 s_h = cb->args[0];
666 s_e = cb->args[1]; 725 s_e = cb->args[1];
667 726
727 rcu_read_lock();
728
668 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) { 729 for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
669 e = 0; 730 e = 0;
670 head = &net->ipv4.fib_table_hash[h]; 731 head = &net->ipv4.fib_table_hash[h];
671 hlist_for_each_entry(tb, head, tb_hlist) { 732 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
672 if (e < s_e) 733 if (e < s_e)
673 goto next; 734 goto next;
674 if (dumped) 735 if (dumped)
@@ -682,6 +743,8 @@ next:
682 } 743 }
683 } 744 }
684out: 745out:
746 rcu_read_unlock();
747
685 cb->args[1] = e; 748 cb->args[1] = e;
686 cb->args[0] = h; 749 cb->args[0] = h;
687 750
@@ -716,7 +779,7 @@ static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifad
716 else 779 else
717 tb = fib_new_table(net, RT_TABLE_LOCAL); 780 tb = fib_new_table(net, RT_TABLE_LOCAL);
718 781
719 if (tb == NULL) 782 if (!tb)
720 return; 783 return;
721 784
722 cfg.fc_table = tb->tb_id; 785 cfg.fc_table = tb->tb_id;
@@ -743,7 +806,7 @@ void fib_add_ifaddr(struct in_ifaddr *ifa)
743 806
744 if (ifa->ifa_flags & IFA_F_SECONDARY) { 807 if (ifa->ifa_flags & IFA_F_SECONDARY) {
745 prim = inet_ifa_byprefix(in_dev, prefix, mask); 808 prim = inet_ifa_byprefix(in_dev, prefix, mask);
746 if (prim == NULL) { 809 if (!prim) {
747 pr_warn("%s: bug: prim == NULL\n", __func__); 810 pr_warn("%s: bug: prim == NULL\n", __func__);
748 return; 811 return;
749 } 812 }
@@ -797,7 +860,7 @@ void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
797 860
798 if (ifa->ifa_flags & IFA_F_SECONDARY) { 861 if (ifa->ifa_flags & IFA_F_SECONDARY) {
799 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask); 862 prim = inet_ifa_byprefix(in_dev, any, ifa->ifa_mask);
800 if (prim == NULL) { 863 if (!prim) {
801 pr_warn("%s: bug: prim == NULL\n", __func__); 864 pr_warn("%s: bug: prim == NULL\n", __func__);
802 return; 865 return;
803 } 866 }
@@ -967,7 +1030,7 @@ static void nl_fib_input(struct sk_buff *skb)
967 return; 1030 return;
968 1031
969 skb = netlink_skb_clone(skb, GFP_KERNEL); 1032 skb = netlink_skb_clone(skb, GFP_KERNEL);
970 if (skb == NULL) 1033 if (!skb)
971 return; 1034 return;
972 nlh = nlmsg_hdr(skb); 1035 nlh = nlmsg_hdr(skb);
973 1036
@@ -988,7 +1051,7 @@ static int __net_init nl_fib_lookup_init(struct net *net)
988 }; 1051 };
989 1052
990 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg); 1053 sk = netlink_kernel_create(net, NETLINK_FIB_LOOKUP, &cfg);
991 if (sk == NULL) 1054 if (!sk)
992 return -EAFNOSUPPORT; 1055 return -EAFNOSUPPORT;
993 net->ipv4.fibnl = sk; 1056 net->ipv4.fibnl = sk;
994 return 0; 1057 return 0;
@@ -1026,7 +1089,7 @@ static int fib_inetaddr_event(struct notifier_block *this, unsigned long event,
1026 case NETDEV_DOWN: 1089 case NETDEV_DOWN:
1027 fib_del_ifaddr(ifa, NULL); 1090 fib_del_ifaddr(ifa, NULL);
1028 atomic_inc(&net->ipv4.dev_addr_genid); 1091 atomic_inc(&net->ipv4.dev_addr_genid);
1029 if (ifa->ifa_dev->ifa_list == NULL) { 1092 if (!ifa->ifa_dev->ifa_list) {
1030 /* Last address was deleted from this interface. 1093 /* Last address was deleted from this interface.
1031 * Disable IP. 1094 * Disable IP.
1032 */ 1095 */
@@ -1094,7 +1157,7 @@ static int __net_init ip_fib_net_init(struct net *net)
1094 size = max_t(size_t, size, L1_CACHE_BYTES); 1157 size = max_t(size_t, size, L1_CACHE_BYTES);
1095 1158
1096 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL); 1159 net->ipv4.fib_table_hash = kzalloc(size, GFP_KERNEL);
1097 if (net->ipv4.fib_table_hash == NULL) 1160 if (!net->ipv4.fib_table_hash)
1098 return -ENOMEM; 1161 return -ENOMEM;
1099 1162
1100 err = fib4_rules_init(net); 1163 err = fib4_rules_init(net);
@@ -1113,20 +1176,25 @@ static void ip_fib_net_exit(struct net *net)
1113 1176
1114 rtnl_lock(); 1177 rtnl_lock();
1115#ifdef CONFIG_IP_MULTIPLE_TABLES 1178#ifdef CONFIG_IP_MULTIPLE_TABLES
1116 fib4_rules_exit(net); 1179 RCU_INIT_POINTER(net->ipv4.fib_local, NULL);
1180 RCU_INIT_POINTER(net->ipv4.fib_main, NULL);
1181 RCU_INIT_POINTER(net->ipv4.fib_default, NULL);
1117#endif 1182#endif
1118 for (i = 0; i < FIB_TABLE_HASHSZ; i++) { 1183 for (i = 0; i < FIB_TABLE_HASHSZ; i++) {
1119 struct fib_table *tb; 1184 struct hlist_head *head = &net->ipv4.fib_table_hash[i];
1120 struct hlist_head *head;
1121 struct hlist_node *tmp; 1185 struct hlist_node *tmp;
1186 struct fib_table *tb;
1122 1187
1123 head = &net->ipv4.fib_table_hash[i];
1124 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) { 1188 hlist_for_each_entry_safe(tb, tmp, head, tb_hlist) {
1125 hlist_del(&tb->tb_hlist); 1189 hlist_del(&tb->tb_hlist);
1126 fib_table_flush(tb); 1190 fib_table_flush(tb);
1127 fib_free_table(tb); 1191 fib_free_table(tb);
1128 } 1192 }
1129 } 1193 }
1194
1195#ifdef CONFIG_IP_MULTIPLE_TABLES
1196 fib4_rules_exit(net);
1197#endif
1130 rtnl_unlock(); 1198 rtnl_unlock();
1131 kfree(net->ipv4.fib_table_hash); 1199 kfree(net->ipv4.fib_table_hash);
1132} 1200}
diff --git a/net/ipv4/fib_lookup.h b/net/ipv4/fib_lookup.h
index 825981b1049a..c6211ed60b03 100644
--- a/net/ipv4/fib_lookup.h
+++ b/net/ipv4/fib_lookup.h
@@ -6,11 +6,13 @@
6#include <net/ip_fib.h> 6#include <net/ip_fib.h>
7 7
8struct fib_alias { 8struct fib_alias {
9 struct list_head fa_list; 9 struct hlist_node fa_list;
10 struct fib_info *fa_info; 10 struct fib_info *fa_info;
11 u8 fa_tos; 11 u8 fa_tos;
12 u8 fa_type; 12 u8 fa_type;
13 u8 fa_state; 13 u8 fa_state;
14 u8 fa_slen;
15 u32 tb_id;
14 struct rcu_head rcu; 16 struct rcu_head rcu;
15}; 17};
16 18
diff --git a/net/ipv4/fib_rules.c b/net/ipv4/fib_rules.c
index d3db718be51d..56151982f74e 100644
--- a/net/ipv4/fib_rules.c
+++ b/net/ipv4/fib_rules.c
@@ -153,7 +153,7 @@ static struct fib_table *fib_empty_table(struct net *net)
153 u32 id; 153 u32 id;
154 154
155 for (id = 1; id <= RT_TABLE_MAX; id++) 155 for (id = 1; id <= RT_TABLE_MAX; id++)
156 if (fib_get_table(net, id) == NULL) 156 if (!fib_get_table(net, id))
157 return fib_new_table(net, id); 157 return fib_new_table(net, id);
158 return NULL; 158 return NULL;
159} 159}
@@ -174,12 +174,17 @@ static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
174 if (frh->tos & ~IPTOS_TOS_MASK) 174 if (frh->tos & ~IPTOS_TOS_MASK)
175 goto errout; 175 goto errout;
176 176
177 /* split local/main if they are not already split */
178 err = fib_unmerge(net);
179 if (err)
180 goto errout;
181
177 if (rule->table == RT_TABLE_UNSPEC) { 182 if (rule->table == RT_TABLE_UNSPEC) {
178 if (rule->action == FR_ACT_TO_TBL) { 183 if (rule->action == FR_ACT_TO_TBL) {
179 struct fib_table *table; 184 struct fib_table *table;
180 185
181 table = fib_empty_table(net); 186 table = fib_empty_table(net);
182 if (table == NULL) { 187 if (!table) {
183 err = -ENOBUFS; 188 err = -ENOBUFS;
184 goto errout; 189 goto errout;
185 } 190 }
@@ -189,10 +194,10 @@ static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 } 194 }
190 195
191 if (frh->src_len) 196 if (frh->src_len)
192 rule4->src = nla_get_be32(tb[FRA_SRC]); 197 rule4->src = nla_get_in_addr(tb[FRA_SRC]);
193 198
194 if (frh->dst_len) 199 if (frh->dst_len)
195 rule4->dst = nla_get_be32(tb[FRA_DST]); 200 rule4->dst = nla_get_in_addr(tb[FRA_DST]);
196 201
197#ifdef CONFIG_IP_ROUTE_CLASSID 202#ifdef CONFIG_IP_ROUTE_CLASSID
198 if (tb[FRA_FLOW]) { 203 if (tb[FRA_FLOW]) {
@@ -209,21 +214,31 @@ static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
209 rule4->tos = frh->tos; 214 rule4->tos = frh->tos;
210 215
211 net->ipv4.fib_has_custom_rules = true; 216 net->ipv4.fib_has_custom_rules = true;
217 fib_flush_external(rule->fr_net);
218
212 err = 0; 219 err = 0;
213errout: 220errout:
214 return err; 221 return err;
215} 222}
216 223
217static void fib4_rule_delete(struct fib_rule *rule) 224static int fib4_rule_delete(struct fib_rule *rule)
218{ 225{
219 struct net *net = rule->fr_net; 226 struct net *net = rule->fr_net;
220#ifdef CONFIG_IP_ROUTE_CLASSID 227 int err;
221 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
222 228
223 if (rule4->tclassid) 229 /* split local/main if they are not already split */
230 err = fib_unmerge(net);
231 if (err)
232 goto errout;
233
234#ifdef CONFIG_IP_ROUTE_CLASSID
235 if (((struct fib4_rule *)rule)->tclassid)
224 net->ipv4.fib_num_tclassid_users--; 236 net->ipv4.fib_num_tclassid_users--;
225#endif 237#endif
226 net->ipv4.fib_has_custom_rules = true; 238 net->ipv4.fib_has_custom_rules = true;
239 fib_flush_external(rule->fr_net);
240errout:
241 return err;
227} 242}
228 243
229static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 244static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
@@ -245,10 +260,10 @@ static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
245 return 0; 260 return 0;
246#endif 261#endif
247 262
248 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) 263 if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
249 return 0; 264 return 0;
250 265
251 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) 266 if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
252 return 0; 267 return 0;
253 268
254 return 1; 269 return 1;
@@ -264,9 +279,9 @@ static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
264 frh->tos = rule4->tos; 279 frh->tos = rule4->tos;
265 280
266 if ((rule4->dst_len && 281 if ((rule4->dst_len &&
267 nla_put_be32(skb, FRA_DST, rule4->dst)) || 282 nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
268 (rule4->src_len && 283 (rule4->src_len &&
269 nla_put_be32(skb, FRA_SRC, rule4->src))) 284 nla_put_in_addr(skb, FRA_SRC, rule4->src)))
270 goto nla_put_failure; 285 goto nla_put_failure;
271#ifdef CONFIG_IP_ROUTE_CLASSID 286#ifdef CONFIG_IP_ROUTE_CLASSID
272 if (rule4->tclassid && 287 if (rule4->tclassid &&
diff --git a/net/ipv4/fib_semantics.c b/net/ipv4/fib_semantics.c
index 1e2090ea663e..8d695b6659c7 100644
--- a/net/ipv4/fib_semantics.c
+++ b/net/ipv4/fib_semantics.c
@@ -213,7 +213,6 @@ static void free_fib_info_rcu(struct rcu_head *head)
213 rt_fibinfo_free(&nexthop_nh->nh_rth_input); 213 rt_fibinfo_free(&nexthop_nh->nh_rth_input);
214 } endfor_nexthops(fi); 214 } endfor_nexthops(fi);
215 215
216 release_net(fi->fib_net);
217 if (fi->fib_metrics != (u32 *) dst_default_metrics) 216 if (fi->fib_metrics != (u32 *) dst_default_metrics)
218 kfree(fi->fib_metrics); 217 kfree(fi->fib_metrics);
219 kfree(fi); 218 kfree(fi);
@@ -391,7 +390,7 @@ void rtmsg_fib(int event, __be32 key, struct fib_alias *fa,
391 int err = -ENOBUFS; 390 int err = -ENOBUFS;
392 391
393 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL); 392 skb = nlmsg_new(fib_nlmsg_size(fa->fa_info), GFP_KERNEL);
394 if (skb == NULL) 393 if (!skb)
395 goto errout; 394 goto errout;
396 395
397 err = fib_dump_info(skb, info->portid, seq, event, tb_id, 396 err = fib_dump_info(skb, info->portid, seq, event, tb_id,
@@ -469,7 +468,7 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
469 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 468 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
470 469
471 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 470 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
472 nexthop_nh->nh_gw = nla ? nla_get_be32(nla) : 0; 471 nexthop_nh->nh_gw = nla ? nla_get_in_addr(nla) : 0;
473#ifdef CONFIG_IP_ROUTE_CLASSID 472#ifdef CONFIG_IP_ROUTE_CLASSID
474 nla = nla_find(attrs, attrlen, RTA_FLOW); 473 nla = nla_find(attrs, attrlen, RTA_FLOW);
475 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0; 474 nexthop_nh->nh_tclassid = nla ? nla_get_u32(nla) : 0;
@@ -504,7 +503,7 @@ int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
504 } 503 }
505 504
506#ifdef CONFIG_IP_ROUTE_MULTIPATH 505#ifdef CONFIG_IP_ROUTE_MULTIPATH
507 if (cfg->fc_mp == NULL) 506 if (!cfg->fc_mp)
508 return 0; 507 return 0;
509 508
510 rtnh = cfg->fc_mp; 509 rtnh = cfg->fc_mp;
@@ -524,7 +523,7 @@ int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
524 struct nlattr *nla, *attrs = rtnh_attrs(rtnh); 523 struct nlattr *nla, *attrs = rtnh_attrs(rtnh);
525 524
526 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 525 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
527 if (nla && nla_get_be32(nla) != nh->nh_gw) 526 if (nla && nla_get_in_addr(nla) != nh->nh_gw)
528 return 1; 527 return 1;
529#ifdef CONFIG_IP_ROUTE_CLASSID 528#ifdef CONFIG_IP_ROUTE_CLASSID
530 nla = nla_find(attrs, attrlen, RTA_FLOW); 529 nla = nla_find(attrs, attrlen, RTA_FLOW);
@@ -647,7 +646,7 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
647 rcu_read_lock(); 646 rcu_read_lock();
648 err = -ENODEV; 647 err = -ENODEV;
649 in_dev = inetdev_by_index(net, nh->nh_oif); 648 in_dev = inetdev_by_index(net, nh->nh_oif);
650 if (in_dev == NULL) 649 if (!in_dev)
651 goto out; 650 goto out;
652 err = -ENETDOWN; 651 err = -ENETDOWN;
653 if (!(in_dev->dev->flags & IFF_UP)) 652 if (!(in_dev->dev->flags & IFF_UP))
@@ -804,7 +803,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
804 } 803 }
805 804
806 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL); 805 fi = kzalloc(sizeof(*fi)+nhs*sizeof(struct fib_nh), GFP_KERNEL);
807 if (fi == NULL) 806 if (!fi)
808 goto failure; 807 goto failure;
809 fib_info_cnt++; 808 fib_info_cnt++;
810 if (cfg->fc_mx) { 809 if (cfg->fc_mx) {
@@ -814,7 +813,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
814 } else 813 } else
815 fi->fib_metrics = (u32 *) dst_default_metrics; 814 fi->fib_metrics = (u32 *) dst_default_metrics;
816 815
817 fi->fib_net = hold_net(net); 816 fi->fib_net = net;
818 fi->fib_protocol = cfg->fc_protocol; 817 fi->fib_protocol = cfg->fc_protocol;
819 fi->fib_scope = cfg->fc_scope; 818 fi->fib_scope = cfg->fc_scope;
820 fi->fib_flags = cfg->fc_flags; 819 fi->fib_flags = cfg->fc_flags;
@@ -922,7 +921,7 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
922 nh->nh_scope = RT_SCOPE_NOWHERE; 921 nh->nh_scope = RT_SCOPE_NOWHERE;
923 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif); 922 nh->nh_dev = dev_get_by_index(net, fi->fib_nh->nh_oif);
924 err = -ENODEV; 923 err = -ENODEV;
925 if (nh->nh_dev == NULL) 924 if (!nh->nh_dev)
926 goto failure; 925 goto failure;
927 } else { 926 } else {
928 change_nexthops(fi) { 927 change_nexthops(fi) {
@@ -996,7 +995,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
996 struct rtmsg *rtm; 995 struct rtmsg *rtm;
997 996
998 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags); 997 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
999 if (nlh == NULL) 998 if (!nlh)
1000 return -EMSGSIZE; 999 return -EMSGSIZE;
1001 1000
1002 rtm = nlmsg_data(nlh); 1001 rtm = nlmsg_data(nlh);
@@ -1016,7 +1015,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1016 rtm->rtm_protocol = fi->fib_protocol; 1015 rtm->rtm_protocol = fi->fib_protocol;
1017 1016
1018 if (rtm->rtm_dst_len && 1017 if (rtm->rtm_dst_len &&
1019 nla_put_be32(skb, RTA_DST, dst)) 1018 nla_put_in_addr(skb, RTA_DST, dst))
1020 goto nla_put_failure; 1019 goto nla_put_failure;
1021 if (fi->fib_priority && 1020 if (fi->fib_priority &&
1022 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority)) 1021 nla_put_u32(skb, RTA_PRIORITY, fi->fib_priority))
@@ -1025,11 +1024,11 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1025 goto nla_put_failure; 1024 goto nla_put_failure;
1026 1025
1027 if (fi->fib_prefsrc && 1026 if (fi->fib_prefsrc &&
1028 nla_put_be32(skb, RTA_PREFSRC, fi->fib_prefsrc)) 1027 nla_put_in_addr(skb, RTA_PREFSRC, fi->fib_prefsrc))
1029 goto nla_put_failure; 1028 goto nla_put_failure;
1030 if (fi->fib_nhs == 1) { 1029 if (fi->fib_nhs == 1) {
1031 if (fi->fib_nh->nh_gw && 1030 if (fi->fib_nh->nh_gw &&
1032 nla_put_be32(skb, RTA_GATEWAY, fi->fib_nh->nh_gw)) 1031 nla_put_in_addr(skb, RTA_GATEWAY, fi->fib_nh->nh_gw))
1033 goto nla_put_failure; 1032 goto nla_put_failure;
1034 if (fi->fib_nh->nh_oif && 1033 if (fi->fib_nh->nh_oif &&
1035 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif)) 1034 nla_put_u32(skb, RTA_OIF, fi->fib_nh->nh_oif))
@@ -1046,12 +1045,12 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1046 struct nlattr *mp; 1045 struct nlattr *mp;
1047 1046
1048 mp = nla_nest_start(skb, RTA_MULTIPATH); 1047 mp = nla_nest_start(skb, RTA_MULTIPATH);
1049 if (mp == NULL) 1048 if (!mp)
1050 goto nla_put_failure; 1049 goto nla_put_failure;
1051 1050
1052 for_nexthops(fi) { 1051 for_nexthops(fi) {
1053 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh)); 1052 rtnh = nla_reserve_nohdr(skb, sizeof(*rtnh));
1054 if (rtnh == NULL) 1053 if (!rtnh)
1055 goto nla_put_failure; 1054 goto nla_put_failure;
1056 1055
1057 rtnh->rtnh_flags = nh->nh_flags & 0xFF; 1056 rtnh->rtnh_flags = nh->nh_flags & 0xFF;
@@ -1059,7 +1058,7 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
1059 rtnh->rtnh_ifindex = nh->nh_oif; 1058 rtnh->rtnh_ifindex = nh->nh_oif;
1060 1059
1061 if (nh->nh_gw && 1060 if (nh->nh_gw &&
1062 nla_put_be32(skb, RTA_GATEWAY, nh->nh_gw)) 1061 nla_put_in_addr(skb, RTA_GATEWAY, nh->nh_gw))
1063 goto nla_put_failure; 1062 goto nla_put_failure;
1064#ifdef CONFIG_IP_ROUTE_CLASSID 1063#ifdef CONFIG_IP_ROUTE_CLASSID
1065 if (nh->nh_tclassid && 1064 if (nh->nh_tclassid &&
@@ -1094,7 +1093,7 @@ int fib_sync_down_addr(struct net *net, __be32 local)
1094 struct hlist_head *head = &fib_info_laddrhash[hash]; 1093 struct hlist_head *head = &fib_info_laddrhash[hash];
1095 struct fib_info *fi; 1094 struct fib_info *fi;
1096 1095
1097 if (fib_info_laddrhash == NULL || local == 0) 1096 if (!fib_info_laddrhash || local == 0)
1098 return 0; 1097 return 0;
1099 1098
1100 hlist_for_each_entry(fi, head, fib_lhash) { 1099 hlist_for_each_entry(fi, head, fib_lhash) {
@@ -1163,12 +1162,12 @@ int fib_sync_down_dev(struct net_device *dev, int force)
1163void fib_select_default(struct fib_result *res) 1162void fib_select_default(struct fib_result *res)
1164{ 1163{
1165 struct fib_info *fi = NULL, *last_resort = NULL; 1164 struct fib_info *fi = NULL, *last_resort = NULL;
1166 struct list_head *fa_head = res->fa_head; 1165 struct hlist_head *fa_head = res->fa_head;
1167 struct fib_table *tb = res->table; 1166 struct fib_table *tb = res->table;
1168 int order = -1, last_idx = -1; 1167 int order = -1, last_idx = -1;
1169 struct fib_alias *fa; 1168 struct fib_alias *fa;
1170 1169
1171 list_for_each_entry_rcu(fa, fa_head, fa_list) { 1170 hlist_for_each_entry_rcu(fa, fa_head, fa_list) {
1172 struct fib_info *next_fi = fa->fa_info; 1171 struct fib_info *next_fi = fa->fa_info;
1173 1172
1174 if (next_fi->fib_scope != res->scope || 1173 if (next_fi->fib_scope != res->scope ||
@@ -1183,7 +1182,7 @@ void fib_select_default(struct fib_result *res)
1183 1182
1184 fib_alias_accessed(fa); 1183 fib_alias_accessed(fa);
1185 1184
1186 if (fi == NULL) { 1185 if (!fi) {
1187 if (next_fi != res->fi) 1186 if (next_fi != res->fi)
1188 break; 1187 break;
1189 } else if (!fib_detect_death(fi, order, &last_resort, 1188 } else if (!fib_detect_death(fi, order, &last_resort,
@@ -1196,7 +1195,7 @@ void fib_select_default(struct fib_result *res)
1196 order++; 1195 order++;
1197 } 1196 }
1198 1197
1199 if (order <= 0 || fi == NULL) { 1198 if (order <= 0 || !fi) {
1200 tb->tb_default = -1; 1199 tb->tb_default = -1;
1201 goto out; 1200 goto out;
1202 } 1201 }
@@ -1252,7 +1251,7 @@ int fib_sync_up(struct net_device *dev)
1252 alive++; 1251 alive++;
1253 continue; 1252 continue;
1254 } 1253 }
1255 if (nexthop_nh->nh_dev == NULL || 1254 if (!nexthop_nh->nh_dev ||
1256 !(nexthop_nh->nh_dev->flags & IFF_UP)) 1255 !(nexthop_nh->nh_dev->flags & IFF_UP))
1257 continue; 1256 continue;
1258 if (nexthop_nh->nh_dev != dev || 1257 if (nexthop_nh->nh_dev != dev ||
diff --git a/net/ipv4/fib_trie.c b/net/ipv4/fib_trie.c
index 3daf0224ff2e..e13fcc602da2 100644
--- a/net/ipv4/fib_trie.c
+++ b/net/ipv4/fib_trie.c
@@ -79,6 +79,7 @@
79#include <net/tcp.h> 79#include <net/tcp.h>
80#include <net/sock.h> 80#include <net/sock.h>
81#include <net/ip_fib.h> 81#include <net/ip_fib.h>
82#include <net/switchdev.h>
82#include "fib_lookup.h" 83#include "fib_lookup.h"
83 84
84#define MAX_STAT_DEPTH 32 85#define MAX_STAT_DEPTH 32
@@ -88,38 +89,35 @@
88 89
89typedef unsigned int t_key; 90typedef unsigned int t_key;
90 91
91#define IS_TNODE(n) ((n)->bits) 92#define IS_TRIE(n) ((n)->pos >= KEYLENGTH)
92#define IS_LEAF(n) (!(n)->bits) 93#define IS_TNODE(n) ((n)->bits)
94#define IS_LEAF(n) (!(n)->bits)
93 95
94#define get_index(_key, _kv) (((_key) ^ (_kv)->key) >> (_kv)->pos) 96struct key_vector {
95
96struct tnode {
97 t_key key; 97 t_key key;
98 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
99 unsigned char pos; /* 2log(KEYLENGTH) bits needed */ 98 unsigned char pos; /* 2log(KEYLENGTH) bits needed */
99 unsigned char bits; /* 2log(KEYLENGTH) bits needed */
100 unsigned char slen; 100 unsigned char slen;
101 struct tnode __rcu *parent;
102 struct rcu_head rcu;
103 union { 101 union {
104 /* The fields in this struct are valid if bits > 0 (TNODE) */ 102 /* This list pointer if valid if (pos | bits) == 0 (LEAF) */
105 struct { 103 struct hlist_head leaf;
106 t_key empty_children; /* KEYLENGTH bits needed */ 104 /* This array is valid if (pos | bits) > 0 (TNODE) */
107 t_key full_children; /* KEYLENGTH bits needed */ 105 struct key_vector __rcu *tnode[0];
108 struct tnode __rcu *child[0];
109 };
110 /* This list pointer if valid if bits == 0 (LEAF) */
111 struct hlist_head list;
112 }; 106 };
113}; 107};
114 108
115struct leaf_info { 109struct tnode {
116 struct hlist_node hlist;
117 int plen;
118 u32 mask_plen; /* ntohl(inet_make_mask(plen)) */
119 struct list_head falh;
120 struct rcu_head rcu; 110 struct rcu_head rcu;
111 t_key empty_children; /* KEYLENGTH bits needed */
112 t_key full_children; /* KEYLENGTH bits needed */
113 struct key_vector __rcu *parent;
114 struct key_vector kv[1];
115#define tn_bits kv[0].bits
121}; 116};
122 117
118#define TNODE_SIZE(n) offsetof(struct tnode, kv[0].tnode[n])
119#define LEAF_SIZE TNODE_SIZE(1)
120
123#ifdef CONFIG_IP_FIB_TRIE_STATS 121#ifdef CONFIG_IP_FIB_TRIE_STATS
124struct trie_use_stats { 122struct trie_use_stats {
125 unsigned int gets; 123 unsigned int gets;
@@ -142,13 +140,13 @@ struct trie_stat {
142}; 140};
143 141
144struct trie { 142struct trie {
145 struct tnode __rcu *trie; 143 struct key_vector kv[1];
146#ifdef CONFIG_IP_FIB_TRIE_STATS 144#ifdef CONFIG_IP_FIB_TRIE_STATS
147 struct trie_use_stats __percpu *stats; 145 struct trie_use_stats __percpu *stats;
148#endif 146#endif
149}; 147};
150 148
151static void resize(struct trie *t, struct tnode *tn); 149static struct key_vector *resize(struct trie *t, struct key_vector *tn);
152static size_t tnode_free_size; 150static size_t tnode_free_size;
153 151
154/* 152/*
@@ -161,41 +159,46 @@ static const int sync_pages = 128;
161static struct kmem_cache *fn_alias_kmem __read_mostly; 159static struct kmem_cache *fn_alias_kmem __read_mostly;
162static struct kmem_cache *trie_leaf_kmem __read_mostly; 160static struct kmem_cache *trie_leaf_kmem __read_mostly;
163 161
162static inline struct tnode *tn_info(struct key_vector *kv)
163{
164 return container_of(kv, struct tnode, kv[0]);
165}
166
164/* caller must hold RTNL */ 167/* caller must hold RTNL */
165#define node_parent(n) rtnl_dereference((n)->parent) 168#define node_parent(tn) rtnl_dereference(tn_info(tn)->parent)
169#define get_child(tn, i) rtnl_dereference((tn)->tnode[i])
166 170
167/* caller must hold RCU read lock or RTNL */ 171/* caller must hold RCU read lock or RTNL */
168#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent) 172#define node_parent_rcu(tn) rcu_dereference_rtnl(tn_info(tn)->parent)
173#define get_child_rcu(tn, i) rcu_dereference_rtnl((tn)->tnode[i])
169 174
170/* wrapper for rcu_assign_pointer */ 175/* wrapper for rcu_assign_pointer */
171static inline void node_set_parent(struct tnode *n, struct tnode *tp) 176static inline void node_set_parent(struct key_vector *n, struct key_vector *tp)
172{ 177{
173 if (n) 178 if (n)
174 rcu_assign_pointer(n->parent, tp); 179 rcu_assign_pointer(tn_info(n)->parent, tp);
175} 180}
176 181
177#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p) 182#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER(tn_info(n)->parent, p)
178 183
179/* This provides us with the number of children in this node, in the case of a 184/* This provides us with the number of children in this node, in the case of a
180 * leaf this will return 0 meaning none of the children are accessible. 185 * leaf this will return 0 meaning none of the children are accessible.
181 */ 186 */
182static inline unsigned long tnode_child_length(const struct tnode *tn) 187static inline unsigned long child_length(const struct key_vector *tn)
183{ 188{
184 return (1ul << tn->bits) & ~(1ul); 189 return (1ul << tn->bits) & ~(1ul);
185} 190}
186 191
187/* caller must hold RTNL */ 192#define get_cindex(key, kv) (((key) ^ (kv)->key) >> (kv)->pos)
188static inline struct tnode *tnode_get_child(const struct tnode *tn,
189 unsigned long i)
190{
191 return rtnl_dereference(tn->child[i]);
192}
193 193
194/* caller must hold RCU read lock or RTNL */ 194static inline unsigned long get_index(t_key key, struct key_vector *kv)
195static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
196 unsigned long i)
197{ 195{
198 return rcu_dereference_rtnl(tn->child[i]); 196 unsigned long index = key ^ kv->key;
197
198 if ((BITS_PER_LONG <= KEYLENGTH) && (KEYLENGTH == kv->pos))
199 return 0;
200
201 return index >> kv->pos;
199} 202}
200 203
201/* To understand this stuff, an understanding of keys and all their bits is 204/* To understand this stuff, an understanding of keys and all their bits is
@@ -274,106 +277,104 @@ static inline void alias_free_mem_rcu(struct fib_alias *fa)
274} 277}
275 278
276#define TNODE_KMALLOC_MAX \ 279#define TNODE_KMALLOC_MAX \
277 ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct tnode *)) 280 ilog2((PAGE_SIZE - TNODE_SIZE(0)) / sizeof(struct key_vector *))
281#define TNODE_VMALLOC_MAX \
282 ilog2((SIZE_MAX - TNODE_SIZE(0)) / sizeof(struct key_vector *))
278 283
279static void __node_free_rcu(struct rcu_head *head) 284static void __node_free_rcu(struct rcu_head *head)
280{ 285{
281 struct tnode *n = container_of(head, struct tnode, rcu); 286 struct tnode *n = container_of(head, struct tnode, rcu);
282 287
283 if (IS_LEAF(n)) 288 if (!n->tn_bits)
284 kmem_cache_free(trie_leaf_kmem, n); 289 kmem_cache_free(trie_leaf_kmem, n);
285 else if (n->bits <= TNODE_KMALLOC_MAX) 290 else if (n->tn_bits <= TNODE_KMALLOC_MAX)
286 kfree(n); 291 kfree(n);
287 else 292 else
288 vfree(n); 293 vfree(n);
289} 294}
290 295
291#define node_free(n) call_rcu(&n->rcu, __node_free_rcu) 296#define node_free(n) call_rcu(&tn_info(n)->rcu, __node_free_rcu)
292 297
293static inline void free_leaf_info(struct leaf_info *leaf) 298static struct tnode *tnode_alloc(int bits)
294{ 299{
295 kfree_rcu(leaf, rcu); 300 size_t size;
296} 301
302 /* verify bits is within bounds */
303 if (bits > TNODE_VMALLOC_MAX)
304 return NULL;
305
306 /* determine size and verify it is non-zero and didn't overflow */
307 size = TNODE_SIZE(1ul << bits);
297 308
298static struct tnode *tnode_alloc(size_t size)
299{
300 if (size <= PAGE_SIZE) 309 if (size <= PAGE_SIZE)
301 return kzalloc(size, GFP_KERNEL); 310 return kzalloc(size, GFP_KERNEL);
302 else 311 else
303 return vzalloc(size); 312 return vzalloc(size);
304} 313}
305 314
306static inline void empty_child_inc(struct tnode *n) 315static inline void empty_child_inc(struct key_vector *n)
307{ 316{
308 ++n->empty_children ? : ++n->full_children; 317 ++tn_info(n)->empty_children ? : ++tn_info(n)->full_children;
309} 318}
310 319
311static inline void empty_child_dec(struct tnode *n) 320static inline void empty_child_dec(struct key_vector *n)
312{ 321{
313 n->empty_children-- ? : n->full_children--; 322 tn_info(n)->empty_children-- ? : tn_info(n)->full_children--;
314} 323}
315 324
316static struct tnode *leaf_new(t_key key) 325static struct key_vector *leaf_new(t_key key, struct fib_alias *fa)
317{ 326{
318 struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL); 327 struct tnode *kv = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
319 if (l) { 328 struct key_vector *l = kv->kv;
320 l->parent = NULL;
321 /* set key and pos to reflect full key value
322 * any trailing zeros in the key should be ignored
323 * as the nodes are searched
324 */
325 l->key = key;
326 l->slen = 0;
327 l->pos = 0;
328 /* set bits to 0 indicating we are not a tnode */
329 l->bits = 0;
330 329
331 INIT_HLIST_HEAD(&l->list); 330 if (!kv)
332 } 331 return NULL;
333 return l;
334}
335 332
336static struct leaf_info *leaf_info_new(int plen) 333 /* initialize key vector */
337{ 334 l->key = key;
338 struct leaf_info *li = kmalloc(sizeof(struct leaf_info), GFP_KERNEL); 335 l->pos = 0;
339 if (li) { 336 l->bits = 0;
340 li->plen = plen; 337 l->slen = fa->fa_slen;
341 li->mask_plen = ntohl(inet_make_mask(plen)); 338
342 INIT_LIST_HEAD(&li->falh); 339 /* link leaf to fib alias */
343 } 340 INIT_HLIST_HEAD(&l->leaf);
344 return li; 341 hlist_add_head(&fa->fa_list, &l->leaf);
342
343 return l;
345} 344}
346 345
347static struct tnode *tnode_new(t_key key, int pos, int bits) 346static struct key_vector *tnode_new(t_key key, int pos, int bits)
348{ 347{
349 size_t sz = offsetof(struct tnode, child[1ul << bits]); 348 struct tnode *tnode = tnode_alloc(bits);
350 struct tnode *tn = tnode_alloc(sz);
351 unsigned int shift = pos + bits; 349 unsigned int shift = pos + bits;
350 struct key_vector *tn = tnode->kv;
352 351
353 /* verify bits and pos their msb bits clear and values are valid */ 352 /* verify bits and pos their msb bits clear and values are valid */
354 BUG_ON(!bits || (shift > KEYLENGTH)); 353 BUG_ON(!bits || (shift > KEYLENGTH));
355 354
356 if (tn) { 355 pr_debug("AT %p s=%zu %zu\n", tnode, TNODE_SIZE(0),
357 tn->parent = NULL; 356 sizeof(struct key_vector *) << bits);
358 tn->slen = pos; 357
359 tn->pos = pos; 358 if (!tnode)
360 tn->bits = bits; 359 return NULL;
361 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0; 360
362 if (bits == KEYLENGTH) 361 if (bits == KEYLENGTH)
363 tn->full_children = 1; 362 tnode->full_children = 1;
364 else 363 else
365 tn->empty_children = 1ul << bits; 364 tnode->empty_children = 1ul << bits;
366 } 365
366 tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
367 tn->pos = pos;
368 tn->bits = bits;
369 tn->slen = pos;
367 370
368 pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
369 sizeof(struct tnode *) << bits);
370 return tn; 371 return tn;
371} 372}
372 373
373/* Check whether a tnode 'n' is "full", i.e. it is an internal node 374/* Check whether a tnode 'n' is "full", i.e. it is an internal node
374 * and no bits are skipped. See discussion in dyntree paper p. 6 375 * and no bits are skipped. See discussion in dyntree paper p. 6
375 */ 376 */
376static inline int tnode_full(const struct tnode *tn, const struct tnode *n) 377static inline int tnode_full(struct key_vector *tn, struct key_vector *n)
377{ 378{
378 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n); 379 return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
379} 380}
@@ -381,17 +382,18 @@ static inline int tnode_full(const struct tnode *tn, const struct tnode *n)
381/* Add a child at position i overwriting the old value. 382/* Add a child at position i overwriting the old value.
382 * Update the value of full_children and empty_children. 383 * Update the value of full_children and empty_children.
383 */ 384 */
384static void put_child(struct tnode *tn, unsigned long i, struct tnode *n) 385static void put_child(struct key_vector *tn, unsigned long i,
386 struct key_vector *n)
385{ 387{
386 struct tnode *chi = tnode_get_child(tn, i); 388 struct key_vector *chi = get_child(tn, i);
387 int isfull, wasfull; 389 int isfull, wasfull;
388 390
389 BUG_ON(i >= tnode_child_length(tn)); 391 BUG_ON(i >= child_length(tn));
390 392
391 /* update emptyChildren, overflow into fullChildren */ 393 /* update emptyChildren, overflow into fullChildren */
392 if (n == NULL && chi != NULL) 394 if (!n && chi)
393 empty_child_inc(tn); 395 empty_child_inc(tn);
394 if (n != NULL && chi == NULL) 396 if (n && !chi)
395 empty_child_dec(tn); 397 empty_child_dec(tn);
396 398
397 /* update fullChildren */ 399 /* update fullChildren */
@@ -399,23 +401,23 @@ static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
399 isfull = tnode_full(tn, n); 401 isfull = tnode_full(tn, n);
400 402
401 if (wasfull && !isfull) 403 if (wasfull && !isfull)
402 tn->full_children--; 404 tn_info(tn)->full_children--;
403 else if (!wasfull && isfull) 405 else if (!wasfull && isfull)
404 tn->full_children++; 406 tn_info(tn)->full_children++;
405 407
406 if (n && (tn->slen < n->slen)) 408 if (n && (tn->slen < n->slen))
407 tn->slen = n->slen; 409 tn->slen = n->slen;
408 410
409 rcu_assign_pointer(tn->child[i], n); 411 rcu_assign_pointer(tn->tnode[i], n);
410} 412}
411 413
412static void update_children(struct tnode *tn) 414static void update_children(struct key_vector *tn)
413{ 415{
414 unsigned long i; 416 unsigned long i;
415 417
416 /* update all of the child parent pointers */ 418 /* update all of the child parent pointers */
417 for (i = tnode_child_length(tn); i;) { 419 for (i = child_length(tn); i;) {
418 struct tnode *inode = tnode_get_child(tn, --i); 420 struct key_vector *inode = get_child(tn, --i);
419 421
420 if (!inode) 422 if (!inode)
421 continue; 423 continue;
@@ -431,36 +433,37 @@ static void update_children(struct tnode *tn)
431 } 433 }
432} 434}
433 435
434static inline void put_child_root(struct tnode *tp, struct trie *t, 436static inline void put_child_root(struct key_vector *tp, t_key key,
435 t_key key, struct tnode *n) 437 struct key_vector *n)
436{ 438{
437 if (tp) 439 if (IS_TRIE(tp))
438 put_child(tp, get_index(key, tp), n); 440 rcu_assign_pointer(tp->tnode[0], n);
439 else 441 else
440 rcu_assign_pointer(t->trie, n); 442 put_child(tp, get_index(key, tp), n);
441} 443}
442 444
443static inline void tnode_free_init(struct tnode *tn) 445static inline void tnode_free_init(struct key_vector *tn)
444{ 446{
445 tn->rcu.next = NULL; 447 tn_info(tn)->rcu.next = NULL;
446} 448}
447 449
448static inline void tnode_free_append(struct tnode *tn, struct tnode *n) 450static inline void tnode_free_append(struct key_vector *tn,
451 struct key_vector *n)
449{ 452{
450 n->rcu.next = tn->rcu.next; 453 tn_info(n)->rcu.next = tn_info(tn)->rcu.next;
451 tn->rcu.next = &n->rcu; 454 tn_info(tn)->rcu.next = &tn_info(n)->rcu;
452} 455}
453 456
454static void tnode_free(struct tnode *tn) 457static void tnode_free(struct key_vector *tn)
455{ 458{
456 struct callback_head *head = &tn->rcu; 459 struct callback_head *head = &tn_info(tn)->rcu;
457 460
458 while (head) { 461 while (head) {
459 head = head->next; 462 head = head->next;
460 tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]); 463 tnode_free_size += TNODE_SIZE(1ul << tn->bits);
461 node_free(tn); 464 node_free(tn);
462 465
463 tn = container_of(head, struct tnode, rcu); 466 tn = container_of(head, struct tnode, rcu)->kv;
464 } 467 }
465 468
466 if (tnode_free_size >= PAGE_SIZE * sync_pages) { 469 if (tnode_free_size >= PAGE_SIZE * sync_pages) {
@@ -469,14 +472,16 @@ static void tnode_free(struct tnode *tn)
469 } 472 }
470} 473}
471 474
472static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn) 475static struct key_vector *replace(struct trie *t,
476 struct key_vector *oldtnode,
477 struct key_vector *tn)
473{ 478{
474 struct tnode *tp = node_parent(oldtnode); 479 struct key_vector *tp = node_parent(oldtnode);
475 unsigned long i; 480 unsigned long i;
476 481
477 /* setup the parent pointer out of and back into this node */ 482 /* setup the parent pointer out of and back into this node */
478 NODE_INIT_PARENT(tn, tp); 483 NODE_INIT_PARENT(tn, tp);
479 put_child_root(tp, t, tn->key, tn); 484 put_child_root(tp, tn->key, tn);
480 485
481 /* update all of the child parent pointers */ 486 /* update all of the child parent pointers */
482 update_children(tn); 487 update_children(tn);
@@ -485,18 +490,21 @@ static void replace(struct trie *t, struct tnode *oldtnode, struct tnode *tn)
485 tnode_free(oldtnode); 490 tnode_free(oldtnode);
486 491
487 /* resize children now that oldtnode is freed */ 492 /* resize children now that oldtnode is freed */
488 for (i = tnode_child_length(tn); i;) { 493 for (i = child_length(tn); i;) {
489 struct tnode *inode = tnode_get_child(tn, --i); 494 struct key_vector *inode = get_child(tn, --i);
490 495
491 /* resize child node */ 496 /* resize child node */
492 if (tnode_full(tn, inode)) 497 if (tnode_full(tn, inode))
493 resize(t, inode); 498 tn = resize(t, inode);
494 } 499 }
500
501 return tp;
495} 502}
496 503
497static int inflate(struct trie *t, struct tnode *oldtnode) 504static struct key_vector *inflate(struct trie *t,
505 struct key_vector *oldtnode)
498{ 506{
499 struct tnode *tn; 507 struct key_vector *tn;
500 unsigned long i; 508 unsigned long i;
501 t_key m; 509 t_key m;
502 510
@@ -504,7 +512,7 @@ static int inflate(struct trie *t, struct tnode *oldtnode)
504 512
505 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1); 513 tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
506 if (!tn) 514 if (!tn)
507 return -ENOMEM; 515 goto notnode;
508 516
509 /* prepare oldtnode to be freed */ 517 /* prepare oldtnode to be freed */
510 tnode_free_init(oldtnode); 518 tnode_free_init(oldtnode);
@@ -514,13 +522,13 @@ static int inflate(struct trie *t, struct tnode *oldtnode)
514 * point to existing tnodes and the links between our allocated 522 * point to existing tnodes and the links between our allocated
515 * nodes. 523 * nodes.
516 */ 524 */
517 for (i = tnode_child_length(oldtnode), m = 1u << tn->pos; i;) { 525 for (i = child_length(oldtnode), m = 1u << tn->pos; i;) {
518 struct tnode *inode = tnode_get_child(oldtnode, --i); 526 struct key_vector *inode = get_child(oldtnode, --i);
519 struct tnode *node0, *node1; 527 struct key_vector *node0, *node1;
520 unsigned long j, k; 528 unsigned long j, k;
521 529
522 /* An empty child */ 530 /* An empty child */
523 if (inode == NULL) 531 if (!inode)
524 continue; 532 continue;
525 533
526 /* A leaf or an internal node with skipped bits */ 534 /* A leaf or an internal node with skipped bits */
@@ -534,8 +542,8 @@ static int inflate(struct trie *t, struct tnode *oldtnode)
534 542
535 /* An internal node with two children */ 543 /* An internal node with two children */
536 if (inode->bits == 1) { 544 if (inode->bits == 1) {
537 put_child(tn, 2 * i + 1, tnode_get_child(inode, 1)); 545 put_child(tn, 2 * i + 1, get_child(inode, 1));
538 put_child(tn, 2 * i, tnode_get_child(inode, 0)); 546 put_child(tn, 2 * i, get_child(inode, 0));
539 continue; 547 continue;
540 } 548 }
541 549
@@ -564,11 +572,11 @@ static int inflate(struct trie *t, struct tnode *oldtnode)
564 tnode_free_append(tn, node0); 572 tnode_free_append(tn, node0);
565 573
566 /* populate child pointers in new nodes */ 574 /* populate child pointers in new nodes */
567 for (k = tnode_child_length(inode), j = k / 2; j;) { 575 for (k = child_length(inode), j = k / 2; j;) {
568 put_child(node1, --j, tnode_get_child(inode, --k)); 576 put_child(node1, --j, get_child(inode, --k));
569 put_child(node0, j, tnode_get_child(inode, j)); 577 put_child(node0, j, get_child(inode, j));
570 put_child(node1, --j, tnode_get_child(inode, --k)); 578 put_child(node1, --j, get_child(inode, --k));
571 put_child(node0, j, tnode_get_child(inode, j)); 579 put_child(node0, j, get_child(inode, j));
572 } 580 }
573 581
574 /* link new nodes to parent */ 582 /* link new nodes to parent */
@@ -581,25 +589,25 @@ static int inflate(struct trie *t, struct tnode *oldtnode)
581 } 589 }
582 590
583 /* setup the parent pointers into and out of this node */ 591 /* setup the parent pointers into and out of this node */
584 replace(t, oldtnode, tn); 592 return replace(t, oldtnode, tn);
585
586 return 0;
587nomem: 593nomem:
588 /* all pointers should be clean so we are done */ 594 /* all pointers should be clean so we are done */
589 tnode_free(tn); 595 tnode_free(tn);
590 return -ENOMEM; 596notnode:
597 return NULL;
591} 598}
592 599
593static int halve(struct trie *t, struct tnode *oldtnode) 600static struct key_vector *halve(struct trie *t,
601 struct key_vector *oldtnode)
594{ 602{
595 struct tnode *tn; 603 struct key_vector *tn;
596 unsigned long i; 604 unsigned long i;
597 605
598 pr_debug("In halve\n"); 606 pr_debug("In halve\n");
599 607
600 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1); 608 tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
601 if (!tn) 609 if (!tn)
602 return -ENOMEM; 610 goto notnode;
603 611
604 /* prepare oldtnode to be freed */ 612 /* prepare oldtnode to be freed */
605 tnode_free_init(oldtnode); 613 tnode_free_init(oldtnode);
@@ -609,10 +617,10 @@ static int halve(struct trie *t, struct tnode *oldtnode)
609 * point to existing tnodes and the links between our allocated 617 * point to existing tnodes and the links between our allocated
610 * nodes. 618 * nodes.
611 */ 619 */
612 for (i = tnode_child_length(oldtnode); i;) { 620 for (i = child_length(oldtnode); i;) {
613 struct tnode *node1 = tnode_get_child(oldtnode, --i); 621 struct key_vector *node1 = get_child(oldtnode, --i);
614 struct tnode *node0 = tnode_get_child(oldtnode, --i); 622 struct key_vector *node0 = get_child(oldtnode, --i);
615 struct tnode *inode; 623 struct key_vector *inode;
616 624
617 /* At least one of the children is empty */ 625 /* At least one of the children is empty */
618 if (!node1 || !node0) { 626 if (!node1 || !node0) {
@@ -622,10 +630,8 @@ static int halve(struct trie *t, struct tnode *oldtnode)
622 630
623 /* Two nonempty children */ 631 /* Two nonempty children */
624 inode = tnode_new(node0->key, oldtnode->pos, 1); 632 inode = tnode_new(node0->key, oldtnode->pos, 1);
625 if (!inode) { 633 if (!inode)
626 tnode_free(tn); 634 goto nomem;
627 return -ENOMEM;
628 }
629 tnode_free_append(tn, inode); 635 tnode_free_append(tn, inode);
630 636
631 /* initialize pointers out of node */ 637 /* initialize pointers out of node */
@@ -638,30 +644,36 @@ static int halve(struct trie *t, struct tnode *oldtnode)
638 } 644 }
639 645
640 /* setup the parent pointers into and out of this node */ 646 /* setup the parent pointers into and out of this node */
641 replace(t, oldtnode, tn); 647 return replace(t, oldtnode, tn);
642 648nomem:
643 return 0; 649 /* all pointers should be clean so we are done */
650 tnode_free(tn);
651notnode:
652 return NULL;
644} 653}
645 654
646static void collapse(struct trie *t, struct tnode *oldtnode) 655static struct key_vector *collapse(struct trie *t,
656 struct key_vector *oldtnode)
647{ 657{
648 struct tnode *n, *tp; 658 struct key_vector *n, *tp;
649 unsigned long i; 659 unsigned long i;
650 660
651 /* scan the tnode looking for that one child that might still exist */ 661 /* scan the tnode looking for that one child that might still exist */
652 for (n = NULL, i = tnode_child_length(oldtnode); !n && i;) 662 for (n = NULL, i = child_length(oldtnode); !n && i;)
653 n = tnode_get_child(oldtnode, --i); 663 n = get_child(oldtnode, --i);
654 664
655 /* compress one level */ 665 /* compress one level */
656 tp = node_parent(oldtnode); 666 tp = node_parent(oldtnode);
657 put_child_root(tp, t, oldtnode->key, n); 667 put_child_root(tp, oldtnode->key, n);
658 node_set_parent(n, tp); 668 node_set_parent(n, tp);
659 669
660 /* drop dead node */ 670 /* drop dead node */
661 node_free(oldtnode); 671 node_free(oldtnode);
672
673 return tp;
662} 674}
663 675
664static unsigned char update_suffix(struct tnode *tn) 676static unsigned char update_suffix(struct key_vector *tn)
665{ 677{
666 unsigned char slen = tn->pos; 678 unsigned char slen = tn->pos;
667 unsigned long stride, i; 679 unsigned long stride, i;
@@ -671,8 +683,8 @@ static unsigned char update_suffix(struct tnode *tn)
671 * why we start with a stride of 2 since a stride of 1 would 683 * why we start with a stride of 2 since a stride of 1 would
672 * represent the nodes with suffix length equal to tn->pos 684 * represent the nodes with suffix length equal to tn->pos
673 */ 685 */
674 for (i = 0, stride = 0x2ul ; i < tnode_child_length(tn); i += stride) { 686 for (i = 0, stride = 0x2ul ; i < child_length(tn); i += stride) {
675 struct tnode *n = tnode_get_child(tn, i); 687 struct key_vector *n = get_child(tn, i);
676 688
677 if (!n || (n->slen <= slen)) 689 if (!n || (n->slen <= slen))
678 continue; 690 continue;
@@ -704,12 +716,12 @@ static unsigned char update_suffix(struct tnode *tn)
704 * 716 *
705 * 'high' in this instance is the variable 'inflate_threshold'. It 717 * 'high' in this instance is the variable 'inflate_threshold'. It
706 * is expressed as a percentage, so we multiply it with 718 * is expressed as a percentage, so we multiply it with
707 * tnode_child_length() and instead of multiplying by 2 (since the 719 * child_length() and instead of multiplying by 2 (since the
708 * child array will be doubled by inflate()) and multiplying 720 * child array will be doubled by inflate()) and multiplying
709 * the left-hand side by 100 (to handle the percentage thing) we 721 * the left-hand side by 100 (to handle the percentage thing) we
710 * multiply the left-hand side by 50. 722 * multiply the left-hand side by 50.
711 * 723 *
712 * The left-hand side may look a bit weird: tnode_child_length(tn) 724 * The left-hand side may look a bit weird: child_length(tn)
713 * - tn->empty_children is of course the number of non-null children 725 * - tn->empty_children is of course the number of non-null children
714 * in the current node. tn->full_children is the number of "full" 726 * in the current node. tn->full_children is the number of "full"
715 * children, that is non-null tnodes with a skip value of 0. 727 * children, that is non-null tnodes with a skip value of 0.
@@ -719,10 +731,10 @@ static unsigned char update_suffix(struct tnode *tn)
719 * A clearer way to write this would be: 731 * A clearer way to write this would be:
720 * 732 *
721 * to_be_doubled = tn->full_children; 733 * to_be_doubled = tn->full_children;
722 * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children - 734 * not_to_be_doubled = child_length(tn) - tn->empty_children -
723 * tn->full_children; 735 * tn->full_children;
724 * 736 *
725 * new_child_length = tnode_child_length(tn) * 2; 737 * new_child_length = child_length(tn) * 2;
726 * 738 *
727 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) / 739 * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
728 * new_child_length; 740 * new_child_length;
@@ -739,57 +751,57 @@ static unsigned char update_suffix(struct tnode *tn)
739 * inflate_threshold * new_child_length 751 * inflate_threshold * new_child_length
740 * 752 *
741 * expand not_to_be_doubled and to_be_doubled, and shorten: 753 * expand not_to_be_doubled and to_be_doubled, and shorten:
742 * 100 * (tnode_child_length(tn) - tn->empty_children + 754 * 100 * (child_length(tn) - tn->empty_children +
743 * tn->full_children) >= inflate_threshold * new_child_length 755 * tn->full_children) >= inflate_threshold * new_child_length
744 * 756 *
745 * expand new_child_length: 757 * expand new_child_length:
746 * 100 * (tnode_child_length(tn) - tn->empty_children + 758 * 100 * (child_length(tn) - tn->empty_children +
747 * tn->full_children) >= 759 * tn->full_children) >=
748 * inflate_threshold * tnode_child_length(tn) * 2 760 * inflate_threshold * child_length(tn) * 2
749 * 761 *
750 * shorten again: 762 * shorten again:
751 * 50 * (tn->full_children + tnode_child_length(tn) - 763 * 50 * (tn->full_children + child_length(tn) -
752 * tn->empty_children) >= inflate_threshold * 764 * tn->empty_children) >= inflate_threshold *
753 * tnode_child_length(tn) 765 * child_length(tn)
754 * 766 *
755 */ 767 */
756static bool should_inflate(const struct tnode *tp, const struct tnode *tn) 768static inline bool should_inflate(struct key_vector *tp, struct key_vector *tn)
757{ 769{
758 unsigned long used = tnode_child_length(tn); 770 unsigned long used = child_length(tn);
759 unsigned long threshold = used; 771 unsigned long threshold = used;
760 772
761 /* Keep root node larger */ 773 /* Keep root node larger */
762 threshold *= tp ? inflate_threshold : inflate_threshold_root; 774 threshold *= IS_TRIE(tp) ? inflate_threshold_root : inflate_threshold;
763 used -= tn->empty_children; 775 used -= tn_info(tn)->empty_children;
764 used += tn->full_children; 776 used += tn_info(tn)->full_children;
765 777
766 /* if bits == KEYLENGTH then pos = 0, and will fail below */ 778 /* if bits == KEYLENGTH then pos = 0, and will fail below */
767 779
768 return (used > 1) && tn->pos && ((50 * used) >= threshold); 780 return (used > 1) && tn->pos && ((50 * used) >= threshold);
769} 781}
770 782
771static bool should_halve(const struct tnode *tp, const struct tnode *tn) 783static inline bool should_halve(struct key_vector *tp, struct key_vector *tn)
772{ 784{
773 unsigned long used = tnode_child_length(tn); 785 unsigned long used = child_length(tn);
774 unsigned long threshold = used; 786 unsigned long threshold = used;
775 787
776 /* Keep root node larger */ 788 /* Keep root node larger */
777 threshold *= tp ? halve_threshold : halve_threshold_root; 789 threshold *= IS_TRIE(tp) ? halve_threshold_root : halve_threshold;
778 used -= tn->empty_children; 790 used -= tn_info(tn)->empty_children;
779 791
780 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */ 792 /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
781 793
782 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold); 794 return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
783} 795}
784 796
785static bool should_collapse(const struct tnode *tn) 797static inline bool should_collapse(struct key_vector *tn)
786{ 798{
787 unsigned long used = tnode_child_length(tn); 799 unsigned long used = child_length(tn);
788 800
789 used -= tn->empty_children; 801 used -= tn_info(tn)->empty_children;
790 802
791 /* account for bits == KEYLENGTH case */ 803 /* account for bits == KEYLENGTH case */
792 if ((tn->bits == KEYLENGTH) && tn->full_children) 804 if ((tn->bits == KEYLENGTH) && tn_info(tn)->full_children)
793 used -= KEY_MAX; 805 used -= KEY_MAX;
794 806
795 /* One child or none, time to drop us from the trie */ 807 /* One child or none, time to drop us from the trie */
@@ -797,10 +809,13 @@ static bool should_collapse(const struct tnode *tn)
797} 809}
798 810
799#define MAX_WORK 10 811#define MAX_WORK 10
800static void resize(struct trie *t, struct tnode *tn) 812static struct key_vector *resize(struct trie *t, struct key_vector *tn)
801{ 813{
802 struct tnode *tp = node_parent(tn); 814#ifdef CONFIG_IP_FIB_TRIE_STATS
803 struct tnode __rcu **cptr; 815 struct trie_use_stats __percpu *stats = t->stats;
816#endif
817 struct key_vector *tp = node_parent(tn);
818 unsigned long cindex = get_index(tn->key, tp);
804 int max_work = MAX_WORK; 819 int max_work = MAX_WORK;
805 820
806 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n", 821 pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
@@ -810,183 +825,128 @@ static void resize(struct trie *t, struct tnode *tn)
810 * doing it ourselves. This way we can let RCU fully do its 825 * doing it ourselves. This way we can let RCU fully do its
811 * thing without us interfering 826 * thing without us interfering
812 */ 827 */
813 cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie; 828 BUG_ON(tn != get_child(tp, cindex));
814 BUG_ON(tn != rtnl_dereference(*cptr));
815 829
816 /* Double as long as the resulting node has a number of 830 /* Double as long as the resulting node has a number of
817 * nonempty nodes that are above the threshold. 831 * nonempty nodes that are above the threshold.
818 */ 832 */
819 while (should_inflate(tp, tn) && max_work) { 833 while (should_inflate(tp, tn) && max_work) {
820 if (inflate(t, tn)) { 834 tp = inflate(t, tn);
835 if (!tp) {
821#ifdef CONFIG_IP_FIB_TRIE_STATS 836#ifdef CONFIG_IP_FIB_TRIE_STATS
822 this_cpu_inc(t->stats->resize_node_skipped); 837 this_cpu_inc(stats->resize_node_skipped);
823#endif 838#endif
824 break; 839 break;
825 } 840 }
826 841
827 max_work--; 842 max_work--;
828 tn = rtnl_dereference(*cptr); 843 tn = get_child(tp, cindex);
829 } 844 }
830 845
846 /* update parent in case inflate failed */
847 tp = node_parent(tn);
848
831 /* Return if at least one inflate is run */ 849 /* Return if at least one inflate is run */
832 if (max_work != MAX_WORK) 850 if (max_work != MAX_WORK)
833 return; 851 return tp;
834 852
835 /* Halve as long as the number of empty children in this 853 /* Halve as long as the number of empty children in this
836 * node is above threshold. 854 * node is above threshold.
837 */ 855 */
838 while (should_halve(tp, tn) && max_work) { 856 while (should_halve(tp, tn) && max_work) {
839 if (halve(t, tn)) { 857 tp = halve(t, tn);
858 if (!tp) {
840#ifdef CONFIG_IP_FIB_TRIE_STATS 859#ifdef CONFIG_IP_FIB_TRIE_STATS
841 this_cpu_inc(t->stats->resize_node_skipped); 860 this_cpu_inc(stats->resize_node_skipped);
842#endif 861#endif
843 break; 862 break;
844 } 863 }
845 864
846 max_work--; 865 max_work--;
847 tn = rtnl_dereference(*cptr); 866 tn = get_child(tp, cindex);
848 } 867 }
849 868
850 /* Only one child remains */ 869 /* Only one child remains */
851 if (should_collapse(tn)) { 870 if (should_collapse(tn))
852 collapse(t, tn); 871 return collapse(t, tn);
853 return; 872
854 } 873 /* update parent in case halve failed */
874 tp = node_parent(tn);
855 875
856 /* Return if at least one deflate was run */ 876 /* Return if at least one deflate was run */
857 if (max_work != MAX_WORK) 877 if (max_work != MAX_WORK)
858 return; 878 return tp;
859 879
860 /* push the suffix length to the parent node */ 880 /* push the suffix length to the parent node */
861 if (tn->slen > tn->pos) { 881 if (tn->slen > tn->pos) {
862 unsigned char slen = update_suffix(tn); 882 unsigned char slen = update_suffix(tn);
863 883
864 if (tp && (slen > tp->slen)) 884 if (slen > tp->slen)
865 tp->slen = slen; 885 tp->slen = slen;
866 } 886 }
867}
868
869/* readside must use rcu_read_lock currently dump routines
870 via get_fa_head and dump */
871
872static struct leaf_info *find_leaf_info(struct tnode *l, int plen)
873{
874 struct hlist_head *head = &l->list;
875 struct leaf_info *li;
876
877 hlist_for_each_entry_rcu(li, head, hlist)
878 if (li->plen == plen)
879 return li;
880
881 return NULL;
882}
883
884static inline struct list_head *get_fa_head(struct tnode *l, int plen)
885{
886 struct leaf_info *li = find_leaf_info(l, plen);
887
888 if (!li)
889 return NULL;
890 887
891 return &li->falh; 888 return tp;
892} 889}
893 890
894static void leaf_pull_suffix(struct tnode *l) 891static void leaf_pull_suffix(struct key_vector *tp, struct key_vector *l)
895{ 892{
896 struct tnode *tp = node_parent(l); 893 while ((tp->slen > tp->pos) && (tp->slen > l->slen)) {
897
898 while (tp && (tp->slen > tp->pos) && (tp->slen > l->slen)) {
899 if (update_suffix(tp) > l->slen) 894 if (update_suffix(tp) > l->slen)
900 break; 895 break;
901 tp = node_parent(tp); 896 tp = node_parent(tp);
902 } 897 }
903} 898}
904 899
905static void leaf_push_suffix(struct tnode *l) 900static void leaf_push_suffix(struct key_vector *tn, struct key_vector *l)
906{ 901{
907 struct tnode *tn = node_parent(l);
908
909 /* if this is a new leaf then tn will be NULL and we can sort 902 /* if this is a new leaf then tn will be NULL and we can sort
910 * out parent suffix lengths as a part of trie_rebalance 903 * out parent suffix lengths as a part of trie_rebalance
911 */ 904 */
912 while (tn && (tn->slen < l->slen)) { 905 while (tn->slen < l->slen) {
913 tn->slen = l->slen; 906 tn->slen = l->slen;
914 tn = node_parent(tn); 907 tn = node_parent(tn);
915 } 908 }
916} 909}
917 910
918static void remove_leaf_info(struct tnode *l, struct leaf_info *old) 911/* rcu_read_lock needs to be hold by caller from readside */
919{ 912static struct key_vector *fib_find_node(struct trie *t,
920 /* record the location of the previous list_info entry */ 913 struct key_vector **tp, u32 key)
921 struct hlist_node **pprev = old->hlist.pprev;
922 struct leaf_info *li = hlist_entry(pprev, typeof(*li), hlist.next);
923
924 /* remove the leaf info from the list */
925 hlist_del_rcu(&old->hlist);
926
927 /* only access li if it is pointing at the last valid hlist_node */
928 if (hlist_empty(&l->list) || (*pprev))
929 return;
930
931 /* update the trie with the latest suffix length */
932 l->slen = KEYLENGTH - li->plen;
933 leaf_pull_suffix(l);
934}
935
936static void insert_leaf_info(struct tnode *l, struct leaf_info *new)
937{ 914{
938 struct hlist_head *head = &l->list; 915 struct key_vector *pn, *n = t->kv;
939 struct leaf_info *li = NULL, *last = NULL; 916 unsigned long index = 0;
940 917
941 if (hlist_empty(head)) { 918 do {
942 hlist_add_head_rcu(&new->hlist, head); 919 pn = n;
943 } else { 920 n = get_child_rcu(n, index);
944 hlist_for_each_entry(li, head, hlist) {
945 if (new->plen > li->plen)
946 break;
947
948 last = li;
949 }
950 if (last)
951 hlist_add_behind_rcu(&new->hlist, &last->hlist);
952 else
953 hlist_add_before_rcu(&new->hlist, &li->hlist);
954 }
955
956 /* if we added to the tail node then we need to update slen */
957 if (l->slen < (KEYLENGTH - new->plen)) {
958 l->slen = KEYLENGTH - new->plen;
959 leaf_push_suffix(l);
960 }
961}
962 921
963/* rcu_read_lock needs to be hold by caller from readside */ 922 if (!n)
964static struct tnode *fib_find_node(struct trie *t, u32 key) 923 break;
965{
966 struct tnode *n = rcu_dereference_rtnl(t->trie);
967 924
968 while (n) { 925 index = get_cindex(key, n);
969 unsigned long index = get_index(key, n);
970 926
971 /* This bit of code is a bit tricky but it combines multiple 927 /* This bit of code is a bit tricky but it combines multiple
972 * checks into a single check. The prefix consists of the 928 * checks into a single check. The prefix consists of the
973 * prefix plus zeros for the bits in the cindex. The index 929 * prefix plus zeros for the bits in the cindex. The index
974 * is the difference between the key and this value. From 930 * is the difference between the key and this value. From
975 * this we can actually derive several pieces of data. 931 * this we can actually derive several pieces of data.
976 * if (index & (~0ul << bits)) 932 * if (index >= (1ul << bits))
977 * we have a mismatch in skip bits and failed 933 * we have a mismatch in skip bits and failed
978 * else 934 * else
979 * we know the value is cindex 935 * we know the value is cindex
936 *
937 * This check is safe even if bits == KEYLENGTH due to the
938 * fact that we can only allocate a node with 32 bits if a
939 * long is greater than 32 bits.
980 */ 940 */
981 if (index & (~0ul << n->bits)) 941 if (index >= (1ul << n->bits)) {
982 return NULL; 942 n = NULL;
983
984 /* we have found a leaf. Prefixes have already been compared */
985 if (IS_LEAF(n))
986 break; 943 break;
944 }
987 945
988 n = tnode_get_child_rcu(n, index); 946 /* keep searching until we find a perfect match leaf or NULL */
989 } 947 } while (IS_TNODE(n));
948
949 *tp = pn;
990 950
991 return n; 951 return n;
992} 952}
@@ -994,14 +954,23 @@ static struct tnode *fib_find_node(struct trie *t, u32 key)
994/* Return the first fib alias matching TOS with 954/* Return the first fib alias matching TOS with
995 * priority less than or equal to PRIO. 955 * priority less than or equal to PRIO.
996 */ 956 */
997static struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio) 957static struct fib_alias *fib_find_alias(struct hlist_head *fah, u8 slen,
958 u8 tos, u32 prio, u32 tb_id)
998{ 959{
999 struct fib_alias *fa; 960 struct fib_alias *fa;
1000 961
1001 if (!fah) 962 if (!fah)
1002 return NULL; 963 return NULL;
1003 964
1004 list_for_each_entry(fa, fah, fa_list) { 965 hlist_for_each_entry(fa, fah, fa_list) {
966 if (fa->fa_slen < slen)
967 continue;
968 if (fa->fa_slen != slen)
969 break;
970 if (fa->tb_id > tb_id)
971 continue;
972 if (fa->tb_id != tb_id)
973 break;
1005 if (fa->fa_tos > tos) 974 if (fa->fa_tos > tos)
1006 continue; 975 continue;
1007 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos) 976 if (fa->fa_info->fib_priority >= prio || fa->fa_tos < tos)
@@ -1011,77 +980,23 @@ static struct fib_alias *fib_find_alias(struct list_head *fah, u8 tos, u32 prio)
1011 return NULL; 980 return NULL;
1012} 981}
1013 982
1014static void trie_rebalance(struct trie *t, struct tnode *tn) 983static void trie_rebalance(struct trie *t, struct key_vector *tn)
1015{ 984{
1016 struct tnode *tp; 985 while (!IS_TRIE(tn))
1017 986 tn = resize(t, tn);
1018 while ((tp = node_parent(tn)) != NULL) {
1019 resize(t, tn);
1020 tn = tp;
1021 }
1022
1023 /* Handle last (top) tnode */
1024 if (IS_TNODE(tn))
1025 resize(t, tn);
1026} 987}
1027 988
1028/* only used from updater-side */ 989static int fib_insert_node(struct trie *t, struct key_vector *tp,
1029 990 struct fib_alias *new, t_key key)
1030static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
1031{ 991{
1032 struct list_head *fa_head = NULL; 992 struct key_vector *n, *l;
1033 struct tnode *l, *n, *tp = NULL;
1034 struct leaf_info *li;
1035
1036 li = leaf_info_new(plen);
1037 if (!li)
1038 return NULL;
1039 fa_head = &li->falh;
1040 993
1041 n = rtnl_dereference(t->trie); 994 l = leaf_new(key, new);
1042 995 if (!l)
1043 /* If we point to NULL, stop. Either the tree is empty and we should 996 goto noleaf;
1044 * just put a new leaf in if, or we have reached an empty child slot,
1045 * and we should just put our new leaf in that.
1046 *
1047 * If we hit a node with a key that does't match then we should stop
1048 * and create a new tnode to replace that node and insert ourselves
1049 * and the other node into the new tnode.
1050 */
1051 while (n) {
1052 unsigned long index = get_index(key, n);
1053
1054 /* This bit of code is a bit tricky but it combines multiple
1055 * checks into a single check. The prefix consists of the
1056 * prefix plus zeros for the "bits" in the prefix. The index
1057 * is the difference between the key and this value. From
1058 * this we can actually derive several pieces of data.
1059 * if !(index >> bits)
1060 * we know the value is child index
1061 * else
1062 * we have a mismatch in skip bits and failed
1063 */
1064 if (index >> n->bits)
1065 break;
1066
1067 /* we have found a leaf. Prefixes have already been compared */
1068 if (IS_LEAF(n)) {
1069 /* Case 1: n is a leaf, and prefixes match*/
1070 insert_leaf_info(n, li);
1071 return fa_head;
1072 }
1073
1074 tp = n;
1075 n = tnode_get_child_rcu(n, index);
1076 }
1077
1078 l = leaf_new(key);
1079 if (!l) {
1080 free_leaf_info(li);
1081 return NULL;
1082 }
1083 997
1084 insert_leaf_info(l, li); 998 /* retrieve child from parent node */
999 n = get_child(tp, get_index(key, tp));
1085 1000
1086 /* Case 2: n is a LEAF or a TNODE and the key doesn't match. 1001 /* Case 2: n is a LEAF or a TNODE and the key doesn't match.
1087 * 1002 *
@@ -1090,21 +1005,18 @@ static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
1090 * leaves us in position for handling as case 3 1005 * leaves us in position for handling as case 3
1091 */ 1006 */
1092 if (n) { 1007 if (n) {
1093 struct tnode *tn; 1008 struct key_vector *tn;
1094 1009
1095 tn = tnode_new(key, __fls(key ^ n->key), 1); 1010 tn = tnode_new(key, __fls(key ^ n->key), 1);
1096 if (!tn) { 1011 if (!tn)
1097 free_leaf_info(li); 1012 goto notnode;
1098 node_free(l);
1099 return NULL;
1100 }
1101 1013
1102 /* initialize routes out of node */ 1014 /* initialize routes out of node */
1103 NODE_INIT_PARENT(tn, tp); 1015 NODE_INIT_PARENT(tn, tp);
1104 put_child(tn, get_index(key, tn) ^ 1, n); 1016 put_child(tn, get_index(key, tn) ^ 1, n);
1105 1017
1106 /* start adding routes into the node */ 1018 /* start adding routes into the node */
1107 put_child_root(tp, t, key, tn); 1019 put_child_root(tp, key, tn);
1108 node_set_parent(n, tn); 1020 node_set_parent(n, tn);
1109 1021
1110 /* parent now has a NULL spot where the leaf can go */ 1022 /* parent now has a NULL spot where the leaf can go */
@@ -1112,69 +1024,93 @@ static struct list_head *fib_insert_node(struct trie *t, u32 key, int plen)
1112 } 1024 }
1113 1025
1114 /* Case 3: n is NULL, and will just insert a new leaf */ 1026 /* Case 3: n is NULL, and will just insert a new leaf */
1115 if (tp) { 1027 NODE_INIT_PARENT(l, tp);
1116 NODE_INIT_PARENT(l, tp); 1028 put_child_root(tp, key, l);
1117 put_child(tp, get_index(key, tp), l); 1029 trie_rebalance(t, tp);
1118 trie_rebalance(t, tp); 1030
1031 return 0;
1032notnode:
1033 node_free(l);
1034noleaf:
1035 return -ENOMEM;
1036}
1037
1038static int fib_insert_alias(struct trie *t, struct key_vector *tp,
1039 struct key_vector *l, struct fib_alias *new,
1040 struct fib_alias *fa, t_key key)
1041{
1042 if (!l)
1043 return fib_insert_node(t, tp, new, key);
1044
1045 if (fa) {
1046 hlist_add_before_rcu(&new->fa_list, &fa->fa_list);
1119 } else { 1047 } else {
1120 rcu_assign_pointer(t->trie, l); 1048 struct fib_alias *last;
1049
1050 hlist_for_each_entry(last, &l->leaf, fa_list) {
1051 if (new->fa_slen < last->fa_slen)
1052 break;
1053 if ((new->fa_slen == last->fa_slen) &&
1054 (new->tb_id > last->tb_id))
1055 break;
1056 fa = last;
1057 }
1058
1059 if (fa)
1060 hlist_add_behind_rcu(&new->fa_list, &fa->fa_list);
1061 else
1062 hlist_add_head_rcu(&new->fa_list, &l->leaf);
1121 } 1063 }
1122 1064
1123 return fa_head; 1065 /* if we added to the tail node then we need to update slen */
1066 if (l->slen < new->fa_slen) {
1067 l->slen = new->fa_slen;
1068 leaf_push_suffix(tp, l);
1069 }
1070
1071 return 0;
1124} 1072}
1125 1073
1126/* 1074/* Caller must hold RTNL. */
1127 * Caller must hold RTNL.
1128 */
1129int fib_table_insert(struct fib_table *tb, struct fib_config *cfg) 1075int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
1130{ 1076{
1131 struct trie *t = (struct trie *) tb->tb_data; 1077 struct trie *t = (struct trie *)tb->tb_data;
1132 struct fib_alias *fa, *new_fa; 1078 struct fib_alias *fa, *new_fa;
1133 struct list_head *fa_head = NULL; 1079 struct key_vector *l, *tp;
1134 struct fib_info *fi; 1080 struct fib_info *fi;
1135 int plen = cfg->fc_dst_len; 1081 u8 plen = cfg->fc_dst_len;
1082 u8 slen = KEYLENGTH - plen;
1136 u8 tos = cfg->fc_tos; 1083 u8 tos = cfg->fc_tos;
1137 u32 key, mask; 1084 u32 key;
1138 int err; 1085 int err;
1139 struct tnode *l;
1140 1086
1141 if (plen > 32) 1087 if (plen > KEYLENGTH)
1142 return -EINVAL; 1088 return -EINVAL;
1143 1089
1144 key = ntohl(cfg->fc_dst); 1090 key = ntohl(cfg->fc_dst);
1145 1091
1146 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen); 1092 pr_debug("Insert table=%u %08x/%d\n", tb->tb_id, key, plen);
1147 1093
1148 mask = ntohl(inet_make_mask(plen)); 1094 if ((plen < KEYLENGTH) && (key << plen))
1149
1150 if (key & ~mask)
1151 return -EINVAL; 1095 return -EINVAL;
1152 1096
1153 key = key & mask;
1154
1155 fi = fib_create_info(cfg); 1097 fi = fib_create_info(cfg);
1156 if (IS_ERR(fi)) { 1098 if (IS_ERR(fi)) {
1157 err = PTR_ERR(fi); 1099 err = PTR_ERR(fi);
1158 goto err; 1100 goto err;
1159 } 1101 }
1160 1102
1161 l = fib_find_node(t, key); 1103 l = fib_find_node(t, &tp, key);
1162 fa = NULL; 1104 fa = l ? fib_find_alias(&l->leaf, slen, tos, fi->fib_priority,
1163 1105 tb->tb_id) : NULL;
1164 if (l) {
1165 fa_head = get_fa_head(l, plen);
1166 fa = fib_find_alias(fa_head, tos, fi->fib_priority);
1167 }
1168 1106
1169 /* Now fa, if non-NULL, points to the first fib alias 1107 /* Now fa, if non-NULL, points to the first fib alias
1170 * with the same keys [prefix,tos,priority], if such key already 1108 * with the same keys [prefix,tos,priority], if such key already
1171 * exists or to the node before which we will insert new one. 1109 * exists or to the node before which we will insert new one.
1172 * 1110 *
1173 * If fa is NULL, we will need to allocate a new one and 1111 * If fa is NULL, we will need to allocate a new one and
1174 * insert to the head of f. 1112 * insert to the tail of the section matching the suffix length
1175 * 1113 * of the new alias.
1176 * If f is NULL, no fib node matched the destination key
1177 * and we need to allocate a new one of those as well.
1178 */ 1114 */
1179 1115
1180 if (fa && fa->fa_tos == tos && 1116 if (fa && fa->fa_tos == tos &&
@@ -1192,9 +1128,10 @@ int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
1192 */ 1128 */
1193 fa_match = NULL; 1129 fa_match = NULL;
1194 fa_first = fa; 1130 fa_first = fa;
1195 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); 1131 hlist_for_each_entry_from(fa, fa_list) {
1196 list_for_each_entry_continue(fa, fa_head, fa_list) { 1132 if ((fa->fa_slen != slen) ||
1197 if (fa->fa_tos != tos) 1133 (fa->tb_id != tb->tb_id) ||
1134 (fa->fa_tos != tos))
1198 break; 1135 break;
1199 if (fa->fa_info->fib_priority != fi->fib_priority) 1136 if (fa->fa_info->fib_priority != fi->fib_priority)
1200 break; 1137 break;
@@ -1217,7 +1154,7 @@ int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
1217 } 1154 }
1218 err = -ENOBUFS; 1155 err = -ENOBUFS;
1219 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL); 1156 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1220 if (new_fa == NULL) 1157 if (!new_fa)
1221 goto out; 1158 goto out;
1222 1159
1223 fi_drop = fa->fa_info; 1160 fi_drop = fa->fa_info;
@@ -1226,8 +1163,21 @@ int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
1226 new_fa->fa_type = cfg->fc_type; 1163 new_fa->fa_type = cfg->fc_type;
1227 state = fa->fa_state; 1164 state = fa->fa_state;
1228 new_fa->fa_state = state & ~FA_S_ACCESSED; 1165 new_fa->fa_state = state & ~FA_S_ACCESSED;
1166 new_fa->fa_slen = fa->fa_slen;
1167
1168 err = netdev_switch_fib_ipv4_add(key, plen, fi,
1169 new_fa->fa_tos,
1170 cfg->fc_type,
1171 cfg->fc_nlflags,
1172 tb->tb_id);
1173 if (err) {
1174 netdev_switch_fib_ipv4_abort(fi);
1175 kmem_cache_free(fn_alias_kmem, new_fa);
1176 goto out;
1177 }
1178
1179 hlist_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1229 1180
1230 list_replace_rcu(&fa->fa_list, &new_fa->fa_list);
1231 alias_free_mem_rcu(fa); 1181 alias_free_mem_rcu(fa);
1232 1182
1233 fib_release_info(fi_drop); 1183 fib_release_info(fi_drop);
@@ -1254,37 +1204,42 @@ int fib_table_insert(struct fib_table *tb, struct fib_config *cfg)
1254 1204
1255 err = -ENOBUFS; 1205 err = -ENOBUFS;
1256 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL); 1206 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1257 if (new_fa == NULL) 1207 if (!new_fa)
1258 goto out; 1208 goto out;
1259 1209
1260 new_fa->fa_info = fi; 1210 new_fa->fa_info = fi;
1261 new_fa->fa_tos = tos; 1211 new_fa->fa_tos = tos;
1262 new_fa->fa_type = cfg->fc_type; 1212 new_fa->fa_type = cfg->fc_type;
1263 new_fa->fa_state = 0; 1213 new_fa->fa_state = 0;
1264 /* 1214 new_fa->fa_slen = slen;
1265 * Insert new entry to the list. 1215 new_fa->tb_id = tb->tb_id;
1266 */ 1216
1267 1217 /* (Optionally) offload fib entry to switch hardware. */
1268 if (!fa_head) { 1218 err = netdev_switch_fib_ipv4_add(key, plen, fi, tos,
1269 fa_head = fib_insert_node(t, key, plen); 1219 cfg->fc_type,
1270 if (unlikely(!fa_head)) { 1220 cfg->fc_nlflags,
1271 err = -ENOMEM; 1221 tb->tb_id);
1272 goto out_free_new_fa; 1222 if (err) {
1273 } 1223 netdev_switch_fib_ipv4_abort(fi);
1224 goto out_free_new_fa;
1274 } 1225 }
1275 1226
1227 /* Insert new entry to the list. */
1228 err = fib_insert_alias(t, tp, l, new_fa, fa, key);
1229 if (err)
1230 goto out_sw_fib_del;
1231
1276 if (!plen) 1232 if (!plen)
1277 tb->tb_num_default++; 1233 tb->tb_num_default++;
1278 1234
1279 list_add_tail_rcu(&new_fa->fa_list,
1280 (fa ? &fa->fa_list : fa_head));
1281
1282 rt_cache_flush(cfg->fc_nlinfo.nl_net); 1235 rt_cache_flush(cfg->fc_nlinfo.nl_net);
1283 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, tb->tb_id, 1236 rtmsg_fib(RTM_NEWROUTE, htonl(key), new_fa, plen, new_fa->tb_id,
1284 &cfg->fc_nlinfo, 0); 1237 &cfg->fc_nlinfo, 0);
1285succeeded: 1238succeeded:
1286 return 0; 1239 return 0;
1287 1240
1241out_sw_fib_del:
1242 netdev_switch_fib_ipv4_del(key, plen, fi, tos, cfg->fc_type, tb->tb_id);
1288out_free_new_fa: 1243out_free_new_fa:
1289 kmem_cache_free(fn_alias_kmem, new_fa); 1244 kmem_cache_free(fn_alias_kmem, new_fa);
1290out: 1245out:
@@ -1293,7 +1248,7 @@ err:
1293 return err; 1248 return err;
1294} 1249}
1295 1250
1296static inline t_key prefix_mismatch(t_key key, struct tnode *n) 1251static inline t_key prefix_mismatch(t_key key, struct key_vector *n)
1297{ 1252{
1298 t_key prefix = n->key; 1253 t_key prefix = n->key;
1299 1254
@@ -1304,16 +1259,20 @@ static inline t_key prefix_mismatch(t_key key, struct tnode *n)
1304int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp, 1259int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
1305 struct fib_result *res, int fib_flags) 1260 struct fib_result *res, int fib_flags)
1306{ 1261{
1307 struct trie *t = (struct trie *)tb->tb_data; 1262 struct trie *t = (struct trie *) tb->tb_data;
1308#ifdef CONFIG_IP_FIB_TRIE_STATS 1263#ifdef CONFIG_IP_FIB_TRIE_STATS
1309 struct trie_use_stats __percpu *stats = t->stats; 1264 struct trie_use_stats __percpu *stats = t->stats;
1310#endif 1265#endif
1311 const t_key key = ntohl(flp->daddr); 1266 const t_key key = ntohl(flp->daddr);
1312 struct tnode *n, *pn; 1267 struct key_vector *n, *pn;
1313 struct leaf_info *li; 1268 struct fib_alias *fa;
1269 unsigned long index;
1314 t_key cindex; 1270 t_key cindex;
1315 1271
1316 n = rcu_dereference(t->trie); 1272 pn = t->kv;
1273 cindex = 0;
1274
1275 n = get_child_rcu(pn, cindex);
1317 if (!n) 1276 if (!n)
1318 return -EAGAIN; 1277 return -EAGAIN;
1319 1278
@@ -1321,24 +1280,25 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
1321 this_cpu_inc(stats->gets); 1280 this_cpu_inc(stats->gets);
1322#endif 1281#endif
1323 1282
1324 pn = n;
1325 cindex = 0;
1326
1327 /* Step 1: Travel to the longest prefix match in the trie */ 1283 /* Step 1: Travel to the longest prefix match in the trie */
1328 for (;;) { 1284 for (;;) {
1329 unsigned long index = get_index(key, n); 1285 index = get_cindex(key, n);
1330 1286
1331 /* This bit of code is a bit tricky but it combines multiple 1287 /* This bit of code is a bit tricky but it combines multiple
1332 * checks into a single check. The prefix consists of the 1288 * checks into a single check. The prefix consists of the
1333 * prefix plus zeros for the "bits" in the prefix. The index 1289 * prefix plus zeros for the "bits" in the prefix. The index
1334 * is the difference between the key and this value. From 1290 * is the difference between the key and this value. From
1335 * this we can actually derive several pieces of data. 1291 * this we can actually derive several pieces of data.
1336 * if (index & (~0ul << bits)) 1292 * if (index >= (1ul << bits))
1337 * we have a mismatch in skip bits and failed 1293 * we have a mismatch in skip bits and failed
1338 * else 1294 * else
1339 * we know the value is cindex 1295 * we know the value is cindex
1296 *
1297 * This check is safe even if bits == KEYLENGTH due to the
1298 * fact that we can only allocate a node with 32 bits if a
1299 * long is greater than 32 bits.
1340 */ 1300 */
1341 if (index & (~0ul << n->bits)) 1301 if (index >= (1ul << n->bits))
1342 break; 1302 break;
1343 1303
1344 /* we have found a leaf. Prefixes have already been compared */ 1304 /* we have found a leaf. Prefixes have already been compared */
@@ -1353,7 +1313,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
1353 cindex = index; 1313 cindex = index;
1354 } 1314 }
1355 1315
1356 n = tnode_get_child_rcu(n, index); 1316 n = get_child_rcu(n, index);
1357 if (unlikely(!n)) 1317 if (unlikely(!n))
1358 goto backtrace; 1318 goto backtrace;
1359 } 1319 }
@@ -1361,7 +1321,7 @@ int fib_table_lookup(struct fib_table *tb, const struct flowi4 *flp,
1361 /* Step 2: Sort out leaves and begin backtracing for longest prefix */ 1321 /* Step 2: Sort out leaves and begin backtracing for longest prefix */
1362 for (;;) { 1322 for (;;) {
1363 /* record the pointer where our next node pointer is stored */ 1323 /* record the pointer where our next node pointer is stored */
1364 struct tnode __rcu **cptr = n->child; 1324 struct key_vector __rcu **cptr = n->tnode;
1365 1325
1366 /* This test verifies that none of the bits that differ 1326 /* This test verifies that none of the bits that differ
1367 * between the key and the prefix exist in the region of 1327 * between the key and the prefix exist in the region of
@@ -1393,13 +1353,17 @@ backtrace:
1393 while (!cindex) { 1353 while (!cindex) {
1394 t_key pkey = pn->key; 1354 t_key pkey = pn->key;
1395 1355
1396 pn = node_parent_rcu(pn); 1356 /* If we don't have a parent then there is
1397 if (unlikely(!pn)) 1357 * nothing for us to do as we do not have any
1358 * further nodes to parse.
1359 */
1360 if (IS_TRIE(pn))
1398 return -EAGAIN; 1361 return -EAGAIN;
1399#ifdef CONFIG_IP_FIB_TRIE_STATS 1362#ifdef CONFIG_IP_FIB_TRIE_STATS
1400 this_cpu_inc(stats->backtrack); 1363 this_cpu_inc(stats->backtrack);
1401#endif 1364#endif
1402 /* Get Child's index */ 1365 /* Get Child's index */
1366 pn = node_parent_rcu(pn);
1403 cindex = get_index(pkey, pn); 1367 cindex = get_index(pkey, pn);
1404 } 1368 }
1405 1369
@@ -1407,138 +1371,134 @@ backtrace:
1407 cindex &= cindex - 1; 1371 cindex &= cindex - 1;
1408 1372
1409 /* grab pointer for next child node */ 1373 /* grab pointer for next child node */
1410 cptr = &pn->child[cindex]; 1374 cptr = &pn->tnode[cindex];
1411 } 1375 }
1412 } 1376 }
1413 1377
1414found: 1378found:
1379 /* this line carries forward the xor from earlier in the function */
1380 index = key ^ n->key;
1381
1415 /* Step 3: Process the leaf, if that fails fall back to backtracing */ 1382 /* Step 3: Process the leaf, if that fails fall back to backtracing */
1416 hlist_for_each_entry_rcu(li, &n->list, hlist) { 1383 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
1417 struct fib_alias *fa; 1384 struct fib_info *fi = fa->fa_info;
1385 int nhsel, err;
1418 1386
1419 if ((key ^ n->key) & li->mask_plen) 1387 if ((index >= (1ul << fa->fa_slen)) &&
1388 ((BITS_PER_LONG > KEYLENGTH) || (fa->fa_slen != KEYLENGTH)))
1420 continue; 1389 continue;
1421 1390 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos)
1422 list_for_each_entry_rcu(fa, &li->falh, fa_list) { 1391 continue;
1423 struct fib_info *fi = fa->fa_info; 1392 if (fi->fib_dead)
1424 int nhsel, err; 1393 continue;
1425 1394 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1426 if (fa->fa_tos && fa->fa_tos != flp->flowi4_tos) 1395 continue;
1427 continue; 1396 fib_alias_accessed(fa);
1428 if (fi->fib_dead) 1397 err = fib_props[fa->fa_type].error;
1429 continue; 1398 if (unlikely(err < 0)) {
1430 if (fa->fa_info->fib_scope < flp->flowi4_scope)
1431 continue;
1432 fib_alias_accessed(fa);
1433 err = fib_props[fa->fa_type].error;
1434 if (unlikely(err < 0)) {
1435#ifdef CONFIG_IP_FIB_TRIE_STATS 1399#ifdef CONFIG_IP_FIB_TRIE_STATS
1436 this_cpu_inc(stats->semantic_match_passed); 1400 this_cpu_inc(stats->semantic_match_passed);
1437#endif 1401#endif
1438 return err; 1402 return err;
1439 } 1403 }
1440 if (fi->fib_flags & RTNH_F_DEAD) 1404 if (fi->fib_flags & RTNH_F_DEAD)
1405 continue;
1406 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
1407 const struct fib_nh *nh = &fi->fib_nh[nhsel];
1408
1409 if (nh->nh_flags & RTNH_F_DEAD)
1441 continue; 1410 continue;
1442 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) { 1411 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
1443 const struct fib_nh *nh = &fi->fib_nh[nhsel]; 1412 continue;
1444 1413
1445 if (nh->nh_flags & RTNH_F_DEAD) 1414 if (!(fib_flags & FIB_LOOKUP_NOREF))
1446 continue; 1415 atomic_inc(&fi->fib_clntref);
1447 if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif) 1416
1448 continue; 1417 res->prefixlen = KEYLENGTH - fa->fa_slen;
1449 1418 res->nh_sel = nhsel;
1450 if (!(fib_flags & FIB_LOOKUP_NOREF)) 1419 res->type = fa->fa_type;
1451 atomic_inc(&fi->fib_clntref); 1420 res->scope = fi->fib_scope;
1452 1421 res->fi = fi;
1453 res->prefixlen = li->plen; 1422 res->table = tb;
1454 res->nh_sel = nhsel; 1423 res->fa_head = &n->leaf;
1455 res->type = fa->fa_type;
1456 res->scope = fi->fib_scope;
1457 res->fi = fi;
1458 res->table = tb;
1459 res->fa_head = &li->falh;
1460#ifdef CONFIG_IP_FIB_TRIE_STATS 1424#ifdef CONFIG_IP_FIB_TRIE_STATS
1461 this_cpu_inc(stats->semantic_match_passed); 1425 this_cpu_inc(stats->semantic_match_passed);
1462#endif 1426#endif
1463 return err; 1427 return err;
1464 }
1465 } 1428 }
1466 1429 }
1467#ifdef CONFIG_IP_FIB_TRIE_STATS 1430#ifdef CONFIG_IP_FIB_TRIE_STATS
1468 this_cpu_inc(stats->semantic_match_miss); 1431 this_cpu_inc(stats->semantic_match_miss);
1469#endif 1432#endif
1470 }
1471 goto backtrace; 1433 goto backtrace;
1472} 1434}
1473EXPORT_SYMBOL_GPL(fib_table_lookup); 1435EXPORT_SYMBOL_GPL(fib_table_lookup);
1474 1436
1475/* 1437static void fib_remove_alias(struct trie *t, struct key_vector *tp,
1476 * Remove the leaf and return parent. 1438 struct key_vector *l, struct fib_alias *old)
1477 */
1478static void trie_leaf_remove(struct trie *t, struct tnode *l)
1479{ 1439{
1480 struct tnode *tp = node_parent(l); 1440 /* record the location of the previous list_info entry */
1441 struct hlist_node **pprev = old->fa_list.pprev;
1442 struct fib_alias *fa = hlist_entry(pprev, typeof(*fa), fa_list.next);
1481 1443
1482 pr_debug("entering trie_leaf_remove(%p)\n", l); 1444 /* remove the fib_alias from the list */
1445 hlist_del_rcu(&old->fa_list);
1483 1446
1484 if (tp) { 1447 /* if we emptied the list this leaf will be freed and we can sort
1485 put_child(tp, get_index(l->key, tp), NULL); 1448 * out parent suffix lengths as a part of trie_rebalance
1449 */
1450 if (hlist_empty(&l->leaf)) {
1451 put_child_root(tp, l->key, NULL);
1452 node_free(l);
1486 trie_rebalance(t, tp); 1453 trie_rebalance(t, tp);
1487 } else { 1454 return;
1488 RCU_INIT_POINTER(t->trie, NULL);
1489 } 1455 }
1490 1456
1491 node_free(l); 1457 /* only access fa if it is pointing at the last valid hlist_node */
1458 if (*pprev)
1459 return;
1460
1461 /* update the trie with the latest suffix length */
1462 l->slen = fa->fa_slen;
1463 leaf_pull_suffix(tp, l);
1492} 1464}
1493 1465
1494/* 1466/* Caller must hold RTNL. */
1495 * Caller must hold RTNL.
1496 */
1497int fib_table_delete(struct fib_table *tb, struct fib_config *cfg) 1467int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
1498{ 1468{
1499 struct trie *t = (struct trie *) tb->tb_data; 1469 struct trie *t = (struct trie *) tb->tb_data;
1500 u32 key, mask;
1501 int plen = cfg->fc_dst_len;
1502 u8 tos = cfg->fc_tos;
1503 struct fib_alias *fa, *fa_to_delete; 1470 struct fib_alias *fa, *fa_to_delete;
1504 struct list_head *fa_head; 1471 struct key_vector *l, *tp;
1505 struct tnode *l; 1472 u8 plen = cfg->fc_dst_len;
1506 struct leaf_info *li; 1473 u8 slen = KEYLENGTH - plen;
1474 u8 tos = cfg->fc_tos;
1475 u32 key;
1507 1476
1508 if (plen > 32) 1477 if (plen > KEYLENGTH)
1509 return -EINVAL; 1478 return -EINVAL;
1510 1479
1511 key = ntohl(cfg->fc_dst); 1480 key = ntohl(cfg->fc_dst);
1512 mask = ntohl(inet_make_mask(plen));
1513 1481
1514 if (key & ~mask) 1482 if ((plen < KEYLENGTH) && (key << plen))
1515 return -EINVAL; 1483 return -EINVAL;
1516 1484
1517 key = key & mask; 1485 l = fib_find_node(t, &tp, key);
1518 l = fib_find_node(t, key);
1519
1520 if (!l) 1486 if (!l)
1521 return -ESRCH; 1487 return -ESRCH;
1522 1488
1523 li = find_leaf_info(l, plen); 1489 fa = fib_find_alias(&l->leaf, slen, tos, 0, tb->tb_id);
1524
1525 if (!li)
1526 return -ESRCH;
1527
1528 fa_head = &li->falh;
1529 fa = fib_find_alias(fa_head, tos, 0);
1530
1531 if (!fa) 1490 if (!fa)
1532 return -ESRCH; 1491 return -ESRCH;
1533 1492
1534 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t); 1493 pr_debug("Deleting %08x/%d tos=%d t=%p\n", key, plen, tos, t);
1535 1494
1536 fa_to_delete = NULL; 1495 fa_to_delete = NULL;
1537 fa = list_entry(fa->fa_list.prev, struct fib_alias, fa_list); 1496 hlist_for_each_entry_from(fa, fa_list) {
1538 list_for_each_entry_continue(fa, fa_head, fa_list) {
1539 struct fib_info *fi = fa->fa_info; 1497 struct fib_info *fi = fa->fa_info;
1540 1498
1541 if (fa->fa_tos != tos) 1499 if ((fa->fa_slen != slen) ||
1500 (fa->tb_id != tb->tb_id) ||
1501 (fa->fa_tos != tos))
1542 break; 1502 break;
1543 1503
1544 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) && 1504 if ((!cfg->fc_type || fa->fa_type == cfg->fc_type) &&
@@ -1557,240 +1517,397 @@ int fib_table_delete(struct fib_table *tb, struct fib_config *cfg)
1557 if (!fa_to_delete) 1517 if (!fa_to_delete)
1558 return -ESRCH; 1518 return -ESRCH;
1559 1519
1560 fa = fa_to_delete; 1520 netdev_switch_fib_ipv4_del(key, plen, fa_to_delete->fa_info, tos,
1561 rtmsg_fib(RTM_DELROUTE, htonl(key), fa, plen, tb->tb_id, 1521 cfg->fc_type, tb->tb_id);
1562 &cfg->fc_nlinfo, 0);
1563 1522
1564 list_del_rcu(&fa->fa_list); 1523 rtmsg_fib(RTM_DELROUTE, htonl(key), fa_to_delete, plen, tb->tb_id,
1524 &cfg->fc_nlinfo, 0);
1565 1525
1566 if (!plen) 1526 if (!plen)
1567 tb->tb_num_default--; 1527 tb->tb_num_default--;
1568 1528
1569 if (list_empty(fa_head)) { 1529 fib_remove_alias(t, tp, l, fa_to_delete);
1570 remove_leaf_info(l, li);
1571 free_leaf_info(li);
1572 }
1573 1530
1574 if (hlist_empty(&l->list)) 1531 if (fa_to_delete->fa_state & FA_S_ACCESSED)
1575 trie_leaf_remove(t, l);
1576
1577 if (fa->fa_state & FA_S_ACCESSED)
1578 rt_cache_flush(cfg->fc_nlinfo.nl_net); 1532 rt_cache_flush(cfg->fc_nlinfo.nl_net);
1579 1533
1580 fib_release_info(fa->fa_info); 1534 fib_release_info(fa_to_delete->fa_info);
1581 alias_free_mem_rcu(fa); 1535 alias_free_mem_rcu(fa_to_delete);
1582 return 0; 1536 return 0;
1583} 1537}
1584 1538
1585static int trie_flush_list(struct list_head *head) 1539/* Scan for the next leaf starting at the provided key value */
1540static struct key_vector *leaf_walk_rcu(struct key_vector **tn, t_key key)
1586{ 1541{
1587 struct fib_alias *fa, *fa_node; 1542 struct key_vector *pn, *n = *tn;
1588 int found = 0; 1543 unsigned long cindex;
1589 1544
1590 list_for_each_entry_safe(fa, fa_node, head, fa_list) { 1545 /* this loop is meant to try and find the key in the trie */
1591 struct fib_info *fi = fa->fa_info; 1546 do {
1547 /* record parent and next child index */
1548 pn = n;
1549 cindex = key ? get_index(key, pn) : 0;
1592 1550
1593 if (fi && (fi->fib_flags & RTNH_F_DEAD)) { 1551 if (cindex >> pn->bits)
1594 list_del_rcu(&fa->fa_list); 1552 break;
1595 fib_release_info(fa->fa_info); 1553
1596 alias_free_mem_rcu(fa); 1554 /* descend into the next child */
1597 found++; 1555 n = get_child_rcu(pn, cindex++);
1556 if (!n)
1557 break;
1558
1559 /* guarantee forward progress on the keys */
1560 if (IS_LEAF(n) && (n->key >= key))
1561 goto found;
1562 } while (IS_TNODE(n));
1563
1564 /* this loop will search for the next leaf with a greater key */
1565 while (!IS_TRIE(pn)) {
1566 /* if we exhausted the parent node we will need to climb */
1567 if (cindex >= (1ul << pn->bits)) {
1568 t_key pkey = pn->key;
1569
1570 pn = node_parent_rcu(pn);
1571 cindex = get_index(pkey, pn) + 1;
1572 continue;
1598 } 1573 }
1574
1575 /* grab the next available node */
1576 n = get_child_rcu(pn, cindex++);
1577 if (!n)
1578 continue;
1579
1580 /* no need to compare keys since we bumped the index */
1581 if (IS_LEAF(n))
1582 goto found;
1583
1584 /* Rescan start scanning in new node */
1585 pn = n;
1586 cindex = 0;
1599 } 1587 }
1600 return found; 1588
1589 *tn = pn;
1590 return NULL; /* Root of trie */
1591found:
1592 /* if we are at the limit for keys just return NULL for the tnode */
1593 *tn = pn;
1594 return n;
1601} 1595}
1602 1596
1603static int trie_flush_leaf(struct tnode *l) 1597static void fib_trie_free(struct fib_table *tb)
1604{ 1598{
1605 int found = 0; 1599 struct trie *t = (struct trie *)tb->tb_data;
1606 struct hlist_head *lih = &l->list; 1600 struct key_vector *pn = t->kv;
1601 unsigned long cindex = 1;
1607 struct hlist_node *tmp; 1602 struct hlist_node *tmp;
1608 struct leaf_info *li = NULL; 1603 struct fib_alias *fa;
1609 unsigned char plen = KEYLENGTH; 1604
1605 /* walk trie in reverse order and free everything */
1606 for (;;) {
1607 struct key_vector *n;
1608
1609 if (!(cindex--)) {
1610 t_key pkey = pn->key;
1611
1612 if (IS_TRIE(pn))
1613 break;
1614
1615 n = pn;
1616 pn = node_parent(pn);
1610 1617
1611 hlist_for_each_entry_safe(li, tmp, lih, hlist) { 1618 /* drop emptied tnode */
1612 found += trie_flush_list(&li->falh); 1619 put_child_root(pn, n->key, NULL);
1620 node_free(n);
1621
1622 cindex = get_index(pkey, pn);
1613 1623
1614 if (list_empty(&li->falh)) {
1615 hlist_del_rcu(&li->hlist);
1616 free_leaf_info(li);
1617 continue; 1624 continue;
1618 } 1625 }
1619 1626
1620 plen = li->plen; 1627 /* grab the next available node */
1621 } 1628 n = get_child(pn, cindex);
1629 if (!n)
1630 continue;
1622 1631
1623 l->slen = KEYLENGTH - plen; 1632 if (IS_TNODE(n)) {
1633 /* record pn and cindex for leaf walking */
1634 pn = n;
1635 cindex = 1ul << n->bits;
1624 1636
1625 return found; 1637 continue;
1638 }
1639
1640 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1641 hlist_del_rcu(&fa->fa_list);
1642 alias_free_mem_rcu(fa);
1643 }
1644
1645 put_child_root(pn, n->key, NULL);
1646 node_free(n);
1647 }
1648
1649#ifdef CONFIG_IP_FIB_TRIE_STATS
1650 free_percpu(t->stats);
1651#endif
1652 kfree(tb);
1626} 1653}
1627 1654
1628/* 1655struct fib_table *fib_trie_unmerge(struct fib_table *oldtb)
1629 * Scan for the next right leaf starting at node p->child[idx]
1630 * Since we have back pointer, no recursion necessary.
1631 */
1632static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
1633{ 1656{
1634 do { 1657 struct trie *ot = (struct trie *)oldtb->tb_data;
1635 unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0; 1658 struct key_vector *l, *tp = ot->kv;
1659 struct fib_table *local_tb;
1660 struct fib_alias *fa;
1661 struct trie *lt;
1662 t_key key = 0;
1636 1663
1637 while (idx < tnode_child_length(p)) { 1664 if (oldtb->tb_data == oldtb->__data)
1638 c = tnode_get_child_rcu(p, idx++); 1665 return oldtb;
1639 if (!c) 1666
1667 local_tb = fib_trie_table(RT_TABLE_LOCAL, NULL);
1668 if (!local_tb)
1669 return NULL;
1670
1671 lt = (struct trie *)local_tb->tb_data;
1672
1673 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1674 struct key_vector *local_l = NULL, *local_tp;
1675
1676 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1677 struct fib_alias *new_fa;
1678
1679 if (local_tb->tb_id != fa->tb_id)
1640 continue; 1680 continue;
1641 1681
1642 if (IS_LEAF(c)) 1682 /* clone fa for new local table */
1643 return c; 1683 new_fa = kmem_cache_alloc(fn_alias_kmem, GFP_KERNEL);
1684 if (!new_fa)
1685 goto out;
1686
1687 memcpy(new_fa, fa, sizeof(*fa));
1644 1688
1645 /* Rescan start scanning in new node */ 1689 /* insert clone into table */
1646 p = c; 1690 if (!local_l)
1647 idx = 0; 1691 local_l = fib_find_node(lt, &local_tp, l->key);
1692
1693 if (fib_insert_alias(lt, local_tp, local_l, new_fa,
1694 NULL, l->key))
1695 goto out;
1648 } 1696 }
1649 1697
1650 /* Node empty, walk back up to parent */ 1698 /* stop loop if key wrapped back to 0 */
1651 c = p; 1699 key = l->key + 1;
1652 } while ((p = node_parent_rcu(c)) != NULL); 1700 if (key < l->key)
1701 break;
1702 }
1653 1703
1654 return NULL; /* Root of trie */ 1704 return local_tb;
1705out:
1706 fib_trie_free(local_tb);
1707
1708 return NULL;
1655} 1709}
1656 1710
1657static struct tnode *trie_firstleaf(struct trie *t) 1711/* Caller must hold RTNL */
1712void fib_table_flush_external(struct fib_table *tb)
1658{ 1713{
1659 struct tnode *n = rcu_dereference_rtnl(t->trie); 1714 struct trie *t = (struct trie *)tb->tb_data;
1715 struct key_vector *pn = t->kv;
1716 unsigned long cindex = 1;
1717 struct hlist_node *tmp;
1718 struct fib_alias *fa;
1660 1719
1661 if (!n) 1720 /* walk trie in reverse order */
1662 return NULL; 1721 for (;;) {
1722 unsigned char slen = 0;
1723 struct key_vector *n;
1663 1724
1664 if (IS_LEAF(n)) /* trie is just a leaf */ 1725 if (!(cindex--)) {
1665 return n; 1726 t_key pkey = pn->key;
1666 1727
1667 return leaf_walk_rcu(n, NULL); 1728 /* cannot resize the trie vector */
1668} 1729 if (IS_TRIE(pn))
1730 break;
1669 1731
1670static struct tnode *trie_nextleaf(struct tnode *l) 1732 /* resize completed node */
1671{ 1733 pn = resize(t, pn);
1672 struct tnode *p = node_parent_rcu(l); 1734 cindex = get_index(pkey, pn);
1673 1735
1674 if (!p) 1736 continue;
1675 return NULL; /* trie with just one leaf */ 1737 }
1676 1738
1677 return leaf_walk_rcu(p, l); 1739 /* grab the next available node */
1678} 1740 n = get_child(pn, cindex);
1741 if (!n)
1742 continue;
1679 1743
1680static struct tnode *trie_leafindex(struct trie *t, int index) 1744 if (IS_TNODE(n)) {
1681{ 1745 /* record pn and cindex for leaf walking */
1682 struct tnode *l = trie_firstleaf(t); 1746 pn = n;
1747 cindex = 1ul << n->bits;
1683 1748
1684 while (l && index-- > 0) 1749 continue;
1685 l = trie_nextleaf(l); 1750 }
1686 1751
1687 return l; 1752 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1688} 1753 struct fib_info *fi = fa->fa_info;
1754
1755 /* if alias was cloned to local then we just
1756 * need to remove the local copy from main
1757 */
1758 if (tb->tb_id != fa->tb_id) {
1759 hlist_del_rcu(&fa->fa_list);
1760 alias_free_mem_rcu(fa);
1761 continue;
1762 }
1689 1763
1764 /* record local slen */
1765 slen = fa->fa_slen;
1690 1766
1691/* 1767 if (!fi || !(fi->fib_flags & RTNH_F_EXTERNAL))
1692 * Caller must hold RTNL. 1768 continue;
1693 */ 1769
1770 netdev_switch_fib_ipv4_del(n->key,
1771 KEYLENGTH - fa->fa_slen,
1772 fi, fa->fa_tos,
1773 fa->fa_type, tb->tb_id);
1774 }
1775
1776 /* update leaf slen */
1777 n->slen = slen;
1778
1779 if (hlist_empty(&n->leaf)) {
1780 put_child_root(pn, n->key, NULL);
1781 node_free(n);
1782 } else {
1783 leaf_pull_suffix(pn, n);
1784 }
1785 }
1786}
1787
1788/* Caller must hold RTNL. */
1694int fib_table_flush(struct fib_table *tb) 1789int fib_table_flush(struct fib_table *tb)
1695{ 1790{
1696 struct trie *t = (struct trie *) tb->tb_data; 1791 struct trie *t = (struct trie *)tb->tb_data;
1697 struct tnode *l, *ll = NULL; 1792 struct key_vector *pn = t->kv;
1793 unsigned long cindex = 1;
1794 struct hlist_node *tmp;
1795 struct fib_alias *fa;
1698 int found = 0; 1796 int found = 0;
1699 1797
1700 for (l = trie_firstleaf(t); l; l = trie_nextleaf(l)) { 1798 /* walk trie in reverse order */
1701 found += trie_flush_leaf(l); 1799 for (;;) {
1800 unsigned char slen = 0;
1801 struct key_vector *n;
1802
1803 if (!(cindex--)) {
1804 t_key pkey = pn->key;
1702 1805
1703 if (ll) { 1806 /* cannot resize the trie vector */
1704 if (hlist_empty(&ll->list)) 1807 if (IS_TRIE(pn))
1705 trie_leaf_remove(t, ll); 1808 break;
1706 else 1809
1707 leaf_pull_suffix(ll); 1810 /* resize completed node */
1811 pn = resize(t, pn);
1812 cindex = get_index(pkey, pn);
1813
1814 continue;
1708 } 1815 }
1709 1816
1710 ll = l; 1817 /* grab the next available node */
1711 } 1818 n = get_child(pn, cindex);
1819 if (!n)
1820 continue;
1712 1821
1713 if (ll) { 1822 if (IS_TNODE(n)) {
1714 if (hlist_empty(&ll->list)) 1823 /* record pn and cindex for leaf walking */
1715 trie_leaf_remove(t, ll); 1824 pn = n;
1716 else 1825 cindex = 1ul << n->bits;
1717 leaf_pull_suffix(ll); 1826
1827 continue;
1828 }
1829
1830 hlist_for_each_entry_safe(fa, tmp, &n->leaf, fa_list) {
1831 struct fib_info *fi = fa->fa_info;
1832
1833 if (!fi || !(fi->fib_flags & RTNH_F_DEAD)) {
1834 slen = fa->fa_slen;
1835 continue;
1836 }
1837
1838 netdev_switch_fib_ipv4_del(n->key,
1839 KEYLENGTH - fa->fa_slen,
1840 fi, fa->fa_tos,
1841 fa->fa_type, tb->tb_id);
1842 hlist_del_rcu(&fa->fa_list);
1843 fib_release_info(fa->fa_info);
1844 alias_free_mem_rcu(fa);
1845 found++;
1846 }
1847
1848 /* update leaf slen */
1849 n->slen = slen;
1850
1851 if (hlist_empty(&n->leaf)) {
1852 put_child_root(pn, n->key, NULL);
1853 node_free(n);
1854 } else {
1855 leaf_pull_suffix(pn, n);
1856 }
1718 } 1857 }
1719 1858
1720 pr_debug("trie_flush found=%d\n", found); 1859 pr_debug("trie_flush found=%d\n", found);
1721 return found; 1860 return found;
1722} 1861}
1723 1862
1724void fib_free_table(struct fib_table *tb) 1863static void __trie_free_rcu(struct rcu_head *head)
1725{ 1864{
1865 struct fib_table *tb = container_of(head, struct fib_table, rcu);
1726#ifdef CONFIG_IP_FIB_TRIE_STATS 1866#ifdef CONFIG_IP_FIB_TRIE_STATS
1727 struct trie *t = (struct trie *)tb->tb_data; 1867 struct trie *t = (struct trie *)tb->tb_data;
1728 1868
1729 free_percpu(t->stats); 1869 if (tb->tb_data == tb->__data)
1870 free_percpu(t->stats);
1730#endif /* CONFIG_IP_FIB_TRIE_STATS */ 1871#endif /* CONFIG_IP_FIB_TRIE_STATS */
1731 kfree(tb); 1872 kfree(tb);
1732} 1873}
1733 1874
1734static int fn_trie_dump_fa(t_key key, int plen, struct list_head *fah, 1875void fib_free_table(struct fib_table *tb)
1735 struct fib_table *tb,
1736 struct sk_buff *skb, struct netlink_callback *cb)
1737{ 1876{
1738 int i, s_i; 1877 call_rcu(&tb->rcu, __trie_free_rcu);
1878}
1879
1880static int fn_trie_dump_leaf(struct key_vector *l, struct fib_table *tb,
1881 struct sk_buff *skb, struct netlink_callback *cb)
1882{
1883 __be32 xkey = htonl(l->key);
1739 struct fib_alias *fa; 1884 struct fib_alias *fa;
1740 __be32 xkey = htonl(key); 1885 int i, s_i;
1741 1886
1742 s_i = cb->args[5]; 1887 s_i = cb->args[4];
1743 i = 0; 1888 i = 0;
1744 1889
1745 /* rcu_read_lock is hold by caller */ 1890 /* rcu_read_lock is hold by caller */
1746 1891 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
1747 list_for_each_entry_rcu(fa, fah, fa_list) {
1748 if (i < s_i) { 1892 if (i < s_i) {
1749 i++; 1893 i++;
1750 continue; 1894 continue;
1751 } 1895 }
1752 1896
1897 if (tb->tb_id != fa->tb_id) {
1898 i++;
1899 continue;
1900 }
1901
1753 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid, 1902 if (fib_dump_info(skb, NETLINK_CB(cb->skb).portid,
1754 cb->nlh->nlmsg_seq, 1903 cb->nlh->nlmsg_seq,
1755 RTM_NEWROUTE, 1904 RTM_NEWROUTE,
1756 tb->tb_id, 1905 tb->tb_id,
1757 fa->fa_type, 1906 fa->fa_type,
1758 xkey, 1907 xkey,
1759 plen, 1908 KEYLENGTH - fa->fa_slen,
1760 fa->fa_tos, 1909 fa->fa_tos,
1761 fa->fa_info, NLM_F_MULTI) < 0) { 1910 fa->fa_info, NLM_F_MULTI) < 0) {
1762 cb->args[5] = i;
1763 return -1;
1764 }
1765 i++;
1766 }
1767 cb->args[5] = i;
1768 return skb->len;
1769}
1770
1771static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
1772 struct sk_buff *skb, struct netlink_callback *cb)
1773{
1774 struct leaf_info *li;
1775 int i, s_i;
1776
1777 s_i = cb->args[4];
1778 i = 0;
1779
1780 /* rcu_read_lock is hold by caller */
1781 hlist_for_each_entry_rcu(li, &l->list, hlist) {
1782 if (i < s_i) {
1783 i++;
1784 continue;
1785 }
1786
1787 if (i > s_i)
1788 cb->args[5] = 0;
1789
1790 if (list_empty(&li->falh))
1791 continue;
1792
1793 if (fn_trie_dump_fa(l->key, li->plen, &li->falh, tb, skb, cb) < 0) {
1794 cb->args[4] = i; 1911 cb->args[4] = i;
1795 return -1; 1912 return -1;
1796 } 1913 }
@@ -1801,44 +1918,38 @@ static int fn_trie_dump_leaf(struct tnode *l, struct fib_table *tb,
1801 return skb->len; 1918 return skb->len;
1802} 1919}
1803 1920
1921/* rcu_read_lock needs to be hold by caller from readside */
1804int fib_table_dump(struct fib_table *tb, struct sk_buff *skb, 1922int fib_table_dump(struct fib_table *tb, struct sk_buff *skb,
1805 struct netlink_callback *cb) 1923 struct netlink_callback *cb)
1806{ 1924{
1807 struct tnode *l; 1925 struct trie *t = (struct trie *)tb->tb_data;
1808 struct trie *t = (struct trie *) tb->tb_data; 1926 struct key_vector *l, *tp = t->kv;
1809 t_key key = cb->args[2];
1810 int count = cb->args[3];
1811
1812 rcu_read_lock();
1813 /* Dump starting at last key. 1927 /* Dump starting at last key.
1814 * Note: 0.0.0.0/0 (ie default) is first key. 1928 * Note: 0.0.0.0/0 (ie default) is first key.
1815 */ 1929 */
1816 if (count == 0) 1930 int count = cb->args[2];
1817 l = trie_firstleaf(t); 1931 t_key key = cb->args[3];
1818 else {
1819 /* Normally, continue from last key, but if that is missing
1820 * fallback to using slow rescan
1821 */
1822 l = fib_find_node(t, key);
1823 if (!l)
1824 l = trie_leafindex(t, count);
1825 }
1826 1932
1827 while (l) { 1933 while ((l = leaf_walk_rcu(&tp, key)) != NULL) {
1828 cb->args[2] = l->key;
1829 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) { 1934 if (fn_trie_dump_leaf(l, tb, skb, cb) < 0) {
1830 cb->args[3] = count; 1935 cb->args[3] = key;
1831 rcu_read_unlock(); 1936 cb->args[2] = count;
1832 return -1; 1937 return -1;
1833 } 1938 }
1834 1939
1835 ++count; 1940 ++count;
1836 l = trie_nextleaf(l); 1941 key = l->key + 1;
1942
1837 memset(&cb->args[4], 0, 1943 memset(&cb->args[4], 0,
1838 sizeof(cb->args) - 4*sizeof(cb->args[0])); 1944 sizeof(cb->args) - 4*sizeof(cb->args[0]));
1945
1946 /* stop loop if key wrapped back to 0 */
1947 if (key < l->key)
1948 break;
1839 } 1949 }
1840 cb->args[3] = count; 1950
1841 rcu_read_unlock(); 1951 cb->args[3] = key;
1952 cb->args[2] = count;
1842 1953
1843 return skb->len; 1954 return skb->len;
1844} 1955}
@@ -1850,28 +1961,34 @@ void __init fib_trie_init(void)
1850 0, SLAB_PANIC, NULL); 1961 0, SLAB_PANIC, NULL);
1851 1962
1852 trie_leaf_kmem = kmem_cache_create("ip_fib_trie", 1963 trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
1853 max(sizeof(struct tnode), 1964 LEAF_SIZE,
1854 sizeof(struct leaf_info)),
1855 0, SLAB_PANIC, NULL); 1965 0, SLAB_PANIC, NULL);
1856} 1966}
1857 1967
1858 1968struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
1859struct fib_table *fib_trie_table(u32 id)
1860{ 1969{
1861 struct fib_table *tb; 1970 struct fib_table *tb;
1862 struct trie *t; 1971 struct trie *t;
1972 size_t sz = sizeof(*tb);
1973
1974 if (!alias)
1975 sz += sizeof(struct trie);
1863 1976
1864 tb = kmalloc(sizeof(struct fib_table) + sizeof(struct trie), 1977 tb = kzalloc(sz, GFP_KERNEL);
1865 GFP_KERNEL); 1978 if (!tb)
1866 if (tb == NULL)
1867 return NULL; 1979 return NULL;
1868 1980
1869 tb->tb_id = id; 1981 tb->tb_id = id;
1870 tb->tb_default = -1; 1982 tb->tb_default = -1;
1871 tb->tb_num_default = 0; 1983 tb->tb_num_default = 0;
1984 tb->tb_data = (alias ? alias->__data : tb->__data);
1985
1986 if (alias)
1987 return tb;
1872 1988
1873 t = (struct trie *) tb->tb_data; 1989 t = (struct trie *) tb->tb_data;
1874 RCU_INIT_POINTER(t->trie, NULL); 1990 t->kv[0].pos = KEYLENGTH;
1991 t->kv[0].slen = KEYLENGTH;
1875#ifdef CONFIG_IP_FIB_TRIE_STATS 1992#ifdef CONFIG_IP_FIB_TRIE_STATS
1876 t->stats = alloc_percpu(struct trie_use_stats); 1993 t->stats = alloc_percpu(struct trie_use_stats);
1877 if (!t->stats) { 1994 if (!t->stats) {
@@ -1888,65 +2005,63 @@ struct fib_table *fib_trie_table(u32 id)
1888struct fib_trie_iter { 2005struct fib_trie_iter {
1889 struct seq_net_private p; 2006 struct seq_net_private p;
1890 struct fib_table *tb; 2007 struct fib_table *tb;
1891 struct tnode *tnode; 2008 struct key_vector *tnode;
1892 unsigned int index; 2009 unsigned int index;
1893 unsigned int depth; 2010 unsigned int depth;
1894}; 2011};
1895 2012
1896static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter) 2013static struct key_vector *fib_trie_get_next(struct fib_trie_iter *iter)
1897{ 2014{
1898 unsigned long cindex = iter->index; 2015 unsigned long cindex = iter->index;
1899 struct tnode *tn = iter->tnode; 2016 struct key_vector *pn = iter->tnode;
1900 struct tnode *p; 2017 t_key pkey;
1901
1902 /* A single entry routing table */
1903 if (!tn)
1904 return NULL;
1905 2018
1906 pr_debug("get_next iter={node=%p index=%d depth=%d}\n", 2019 pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
1907 iter->tnode, iter->index, iter->depth); 2020 iter->tnode, iter->index, iter->depth);
1908rescan:
1909 while (cindex < tnode_child_length(tn)) {
1910 struct tnode *n = tnode_get_child_rcu(tn, cindex);
1911 2021
1912 if (n) { 2022 while (!IS_TRIE(pn)) {
2023 while (cindex < child_length(pn)) {
2024 struct key_vector *n = get_child_rcu(pn, cindex++);
2025
2026 if (!n)
2027 continue;
2028
1913 if (IS_LEAF(n)) { 2029 if (IS_LEAF(n)) {
1914 iter->tnode = tn; 2030 iter->tnode = pn;
1915 iter->index = cindex + 1; 2031 iter->index = cindex;
1916 } else { 2032 } else {
1917 /* push down one level */ 2033 /* push down one level */
1918 iter->tnode = n; 2034 iter->tnode = n;
1919 iter->index = 0; 2035 iter->index = 0;
1920 ++iter->depth; 2036 ++iter->depth;
1921 } 2037 }
2038
1922 return n; 2039 return n;
1923 } 2040 }
1924 2041
1925 ++cindex; 2042 /* Current node exhausted, pop back up */
1926 } 2043 pkey = pn->key;
1927 2044 pn = node_parent_rcu(pn);
1928 /* Current node exhausted, pop back up */ 2045 cindex = get_index(pkey, pn) + 1;
1929 p = node_parent_rcu(tn);
1930 if (p) {
1931 cindex = get_index(tn->key, p) + 1;
1932 tn = p;
1933 --iter->depth; 2046 --iter->depth;
1934 goto rescan;
1935 } 2047 }
1936 2048
1937 /* got root? */ 2049 /* record root node so further searches know we are done */
2050 iter->tnode = pn;
2051 iter->index = 0;
2052
1938 return NULL; 2053 return NULL;
1939} 2054}
1940 2055
1941static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter, 2056static struct key_vector *fib_trie_get_first(struct fib_trie_iter *iter,
1942 struct trie *t) 2057 struct trie *t)
1943{ 2058{
1944 struct tnode *n; 2059 struct key_vector *n, *pn = t->kv;
1945 2060
1946 if (!t) 2061 if (!t)
1947 return NULL; 2062 return NULL;
1948 2063
1949 n = rcu_dereference(t->trie); 2064 n = rcu_dereference(pn->tnode[0]);
1950 if (!n) 2065 if (!n)
1951 return NULL; 2066 return NULL;
1952 2067
@@ -1955,7 +2070,7 @@ static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
1955 iter->index = 0; 2070 iter->index = 0;
1956 iter->depth = 1; 2071 iter->depth = 1;
1957 } else { 2072 } else {
1958 iter->tnode = NULL; 2073 iter->tnode = pn;
1959 iter->index = 0; 2074 iter->index = 0;
1960 iter->depth = 0; 2075 iter->depth = 0;
1961 } 2076 }
@@ -1965,7 +2080,7 @@ static struct tnode *fib_trie_get_first(struct fib_trie_iter *iter,
1965 2080
1966static void trie_collect_stats(struct trie *t, struct trie_stat *s) 2081static void trie_collect_stats(struct trie *t, struct trie_stat *s)
1967{ 2082{
1968 struct tnode *n; 2083 struct key_vector *n;
1969 struct fib_trie_iter iter; 2084 struct fib_trie_iter iter;
1970 2085
1971 memset(s, 0, sizeof(*s)); 2086 memset(s, 0, sizeof(*s));
@@ -1973,20 +2088,20 @@ static void trie_collect_stats(struct trie *t, struct trie_stat *s)
1973 rcu_read_lock(); 2088 rcu_read_lock();
1974 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) { 2089 for (n = fib_trie_get_first(&iter, t); n; n = fib_trie_get_next(&iter)) {
1975 if (IS_LEAF(n)) { 2090 if (IS_LEAF(n)) {
1976 struct leaf_info *li; 2091 struct fib_alias *fa;
1977 2092
1978 s->leaves++; 2093 s->leaves++;
1979 s->totdepth += iter.depth; 2094 s->totdepth += iter.depth;
1980 if (iter.depth > s->maxdepth) 2095 if (iter.depth > s->maxdepth)
1981 s->maxdepth = iter.depth; 2096 s->maxdepth = iter.depth;
1982 2097
1983 hlist_for_each_entry_rcu(li, &n->list, hlist) 2098 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list)
1984 ++s->prefixes; 2099 ++s->prefixes;
1985 } else { 2100 } else {
1986 s->tnodes++; 2101 s->tnodes++;
1987 if (n->bits < MAX_STAT_DEPTH) 2102 if (n->bits < MAX_STAT_DEPTH)
1988 s->nodesizes[n->bits]++; 2103 s->nodesizes[n->bits]++;
1989 s->nullpointers += n->empty_children; 2104 s->nullpointers += tn_info(n)->empty_children;
1990 } 2105 }
1991 } 2106 }
1992 rcu_read_unlock(); 2107 rcu_read_unlock();
@@ -2009,13 +2124,13 @@ static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
2009 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth); 2124 seq_printf(seq, "\tMax depth: %u\n", stat->maxdepth);
2010 2125
2011 seq_printf(seq, "\tLeaves: %u\n", stat->leaves); 2126 seq_printf(seq, "\tLeaves: %u\n", stat->leaves);
2012 bytes = sizeof(struct tnode) * stat->leaves; 2127 bytes = LEAF_SIZE * stat->leaves;
2013 2128
2014 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes); 2129 seq_printf(seq, "\tPrefixes: %u\n", stat->prefixes);
2015 bytes += sizeof(struct leaf_info) * stat->prefixes; 2130 bytes += sizeof(struct fib_alias) * stat->prefixes;
2016 2131
2017 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes); 2132 seq_printf(seq, "\tInternal nodes: %u\n\t", stat->tnodes);
2018 bytes += sizeof(struct tnode) * stat->tnodes; 2133 bytes += TNODE_SIZE(0) * stat->tnodes;
2019 2134
2020 max = MAX_STAT_DEPTH; 2135 max = MAX_STAT_DEPTH;
2021 while (max > 0 && stat->nodesizes[max-1] == 0) 2136 while (max > 0 && stat->nodesizes[max-1] == 0)
@@ -2030,7 +2145,7 @@ static void trie_show_stats(struct seq_file *seq, struct trie_stat *stat)
2030 seq_putc(seq, '\n'); 2145 seq_putc(seq, '\n');
2031 seq_printf(seq, "\tPointers: %u\n", pointers); 2146 seq_printf(seq, "\tPointers: %u\n", pointers);
2032 2147
2033 bytes += sizeof(struct tnode *) * pointers; 2148 bytes += sizeof(struct key_vector *) * pointers;
2034 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers); 2149 seq_printf(seq, "Null ptrs: %u\n", stat->nullpointers);
2035 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024); 2150 seq_printf(seq, "Total size: %u kB\n", (bytes + 1023) / 1024);
2036} 2151}
@@ -2084,7 +2199,7 @@ static int fib_triestat_seq_show(struct seq_file *seq, void *v)
2084 seq_printf(seq, 2199 seq_printf(seq,
2085 "Basic info: size of leaf:" 2200 "Basic info: size of leaf:"
2086 " %Zd bytes, size of tnode: %Zd bytes.\n", 2201 " %Zd bytes, size of tnode: %Zd bytes.\n",
2087 sizeof(struct tnode), sizeof(struct tnode)); 2202 LEAF_SIZE, TNODE_SIZE(0));
2088 2203
2089 for (h = 0; h < FIB_TABLE_HASHSZ; h++) { 2204 for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
2090 struct hlist_head *head = &net->ipv4.fib_table_hash[h]; 2205 struct hlist_head *head = &net->ipv4.fib_table_hash[h];
@@ -2123,7 +2238,7 @@ static const struct file_operations fib_triestat_fops = {
2123 .release = single_release_net, 2238 .release = single_release_net,
2124}; 2239};
2125 2240
2126static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos) 2241static struct key_vector *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
2127{ 2242{
2128 struct fib_trie_iter *iter = seq->private; 2243 struct fib_trie_iter *iter = seq->private;
2129 struct net *net = seq_file_net(seq); 2244 struct net *net = seq_file_net(seq);
@@ -2135,7 +2250,7 @@ static struct tnode *fib_trie_get_idx(struct seq_file *seq, loff_t pos)
2135 struct fib_table *tb; 2250 struct fib_table *tb;
2136 2251
2137 hlist_for_each_entry_rcu(tb, head, tb_hlist) { 2252 hlist_for_each_entry_rcu(tb, head, tb_hlist) {
2138 struct tnode *n; 2253 struct key_vector *n;
2139 2254
2140 for (n = fib_trie_get_first(iter, 2255 for (n = fib_trie_get_first(iter,
2141 (struct trie *) tb->tb_data); 2256 (struct trie *) tb->tb_data);
@@ -2164,7 +2279,7 @@ static void *fib_trie_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2164 struct fib_table *tb = iter->tb; 2279 struct fib_table *tb = iter->tb;
2165 struct hlist_node *tb_node; 2280 struct hlist_node *tb_node;
2166 unsigned int h; 2281 unsigned int h;
2167 struct tnode *n; 2282 struct key_vector *n;
2168 2283
2169 ++*pos; 2284 ++*pos;
2170 /* next node in same table */ 2285 /* next node in same table */
@@ -2250,9 +2365,9 @@ static inline const char *rtn_type(char *buf, size_t len, unsigned int t)
2250static int fib_trie_seq_show(struct seq_file *seq, void *v) 2365static int fib_trie_seq_show(struct seq_file *seq, void *v)
2251{ 2366{
2252 const struct fib_trie_iter *iter = seq->private; 2367 const struct fib_trie_iter *iter = seq->private;
2253 struct tnode *n = v; 2368 struct key_vector *n = v;
2254 2369
2255 if (!node_parent_rcu(n)) 2370 if (IS_TRIE(node_parent_rcu(n)))
2256 fib_table_print(seq, iter->tb); 2371 fib_table_print(seq, iter->tb);
2257 2372
2258 if (IS_TNODE(n)) { 2373 if (IS_TNODE(n)) {
@@ -2261,30 +2376,28 @@ static int fib_trie_seq_show(struct seq_file *seq, void *v)
2261 seq_indent(seq, iter->depth-1); 2376 seq_indent(seq, iter->depth-1);
2262 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n", 2377 seq_printf(seq, " +-- %pI4/%zu %u %u %u\n",
2263 &prf, KEYLENGTH - n->pos - n->bits, n->bits, 2378 &prf, KEYLENGTH - n->pos - n->bits, n->bits,
2264 n->full_children, n->empty_children); 2379 tn_info(n)->full_children,
2380 tn_info(n)->empty_children);
2265 } else { 2381 } else {
2266 struct leaf_info *li;
2267 __be32 val = htonl(n->key); 2382 __be32 val = htonl(n->key);
2383 struct fib_alias *fa;
2268 2384
2269 seq_indent(seq, iter->depth); 2385 seq_indent(seq, iter->depth);
2270 seq_printf(seq, " |-- %pI4\n", &val); 2386 seq_printf(seq, " |-- %pI4\n", &val);
2271 2387
2272 hlist_for_each_entry_rcu(li, &n->list, hlist) { 2388 hlist_for_each_entry_rcu(fa, &n->leaf, fa_list) {
2273 struct fib_alias *fa; 2389 char buf1[32], buf2[32];
2274 2390
2275 list_for_each_entry_rcu(fa, &li->falh, fa_list) { 2391 seq_indent(seq, iter->depth + 1);
2276 char buf1[32], buf2[32]; 2392 seq_printf(seq, " /%zu %s %s",
2277 2393 KEYLENGTH - fa->fa_slen,
2278 seq_indent(seq, iter->depth+1); 2394 rtn_scope(buf1, sizeof(buf1),
2279 seq_printf(seq, " /%d %s %s", li->plen, 2395 fa->fa_info->fib_scope),
2280 rtn_scope(buf1, sizeof(buf1), 2396 rtn_type(buf2, sizeof(buf2),
2281 fa->fa_info->fib_scope), 2397 fa->fa_type));
2282 rtn_type(buf2, sizeof(buf2), 2398 if (fa->fa_tos)
2283 fa->fa_type)); 2399 seq_printf(seq, " tos=%d", fa->fa_tos);
2284 if (fa->fa_tos) 2400 seq_putc(seq, '\n');
2285 seq_printf(seq, " tos=%d", fa->fa_tos);
2286 seq_putc(seq, '\n');
2287 }
2288 } 2401 }
2289 } 2402 }
2290 2403
@@ -2314,31 +2427,47 @@ static const struct file_operations fib_trie_fops = {
2314 2427
2315struct fib_route_iter { 2428struct fib_route_iter {
2316 struct seq_net_private p; 2429 struct seq_net_private p;
2317 struct trie *main_trie; 2430 struct fib_table *main_tb;
2431 struct key_vector *tnode;
2318 loff_t pos; 2432 loff_t pos;
2319 t_key key; 2433 t_key key;
2320}; 2434};
2321 2435
2322static struct tnode *fib_route_get_idx(struct fib_route_iter *iter, loff_t pos) 2436static struct key_vector *fib_route_get_idx(struct fib_route_iter *iter,
2437 loff_t pos)
2323{ 2438{
2324 struct tnode *l = NULL; 2439 struct fib_table *tb = iter->main_tb;
2325 struct trie *t = iter->main_trie; 2440 struct key_vector *l, **tp = &iter->tnode;
2441 struct trie *t;
2442 t_key key;
2326 2443
2327 /* use cache location of last found key */ 2444 /* use cache location of next-to-find key */
2328 if (iter->pos > 0 && pos >= iter->pos && (l = fib_find_node(t, iter->key))) 2445 if (iter->pos > 0 && pos >= iter->pos) {
2329 pos -= iter->pos; 2446 pos -= iter->pos;
2330 else { 2447 key = iter->key;
2448 } else {
2449 t = (struct trie *)tb->tb_data;
2450 iter->tnode = t->kv;
2331 iter->pos = 0; 2451 iter->pos = 0;
2332 l = trie_firstleaf(t); 2452 key = 0;
2333 } 2453 }
2334 2454
2335 while (l && pos-- > 0) { 2455 while ((l = leaf_walk_rcu(tp, key)) != NULL) {
2456 key = l->key + 1;
2336 iter->pos++; 2457 iter->pos++;
2337 l = trie_nextleaf(l); 2458
2459 if (pos-- <= 0)
2460 break;
2461
2462 l = NULL;
2463
2464 /* handle unlikely case of a key wrap */
2465 if (!key)
2466 break;
2338 } 2467 }
2339 2468
2340 if (l) 2469 if (l)
2341 iter->key = pos; /* remember it */ 2470 iter->key = key; /* remember it */
2342 else 2471 else
2343 iter->pos = 0; /* forget it */ 2472 iter->pos = 0; /* forget it */
2344 2473
@@ -2350,37 +2479,46 @@ static void *fib_route_seq_start(struct seq_file *seq, loff_t *pos)
2350{ 2479{
2351 struct fib_route_iter *iter = seq->private; 2480 struct fib_route_iter *iter = seq->private;
2352 struct fib_table *tb; 2481 struct fib_table *tb;
2482 struct trie *t;
2353 2483
2354 rcu_read_lock(); 2484 rcu_read_lock();
2485
2355 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN); 2486 tb = fib_get_table(seq_file_net(seq), RT_TABLE_MAIN);
2356 if (!tb) 2487 if (!tb)
2357 return NULL; 2488 return NULL;
2358 2489
2359 iter->main_trie = (struct trie *) tb->tb_data; 2490 iter->main_tb = tb;
2360 if (*pos == 0) 2491
2361 return SEQ_START_TOKEN; 2492 if (*pos != 0)
2362 else 2493 return fib_route_get_idx(iter, *pos);
2363 return fib_route_get_idx(iter, *pos - 1); 2494
2495 t = (struct trie *)tb->tb_data;
2496 iter->tnode = t->kv;
2497 iter->pos = 0;
2498 iter->key = 0;
2499
2500 return SEQ_START_TOKEN;
2364} 2501}
2365 2502
2366static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos) 2503static void *fib_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2367{ 2504{
2368 struct fib_route_iter *iter = seq->private; 2505 struct fib_route_iter *iter = seq->private;
2369 struct tnode *l = v; 2506 struct key_vector *l = NULL;
2507 t_key key = iter->key;
2370 2508
2371 ++*pos; 2509 ++*pos;
2372 if (v == SEQ_START_TOKEN) { 2510
2373 iter->pos = 0; 2511 /* only allow key of 0 for start of sequence */
2374 l = trie_firstleaf(iter->main_trie); 2512 if ((v == SEQ_START_TOKEN) || key)
2375 } else { 2513 l = leaf_walk_rcu(&iter->tnode, key);
2514
2515 if (l) {
2516 iter->key = l->key + 1;
2376 iter->pos++; 2517 iter->pos++;
2377 l = trie_nextleaf(l); 2518 } else {
2519 iter->pos = 0;
2378 } 2520 }
2379 2521
2380 if (l)
2381 iter->key = l->key;
2382 else
2383 iter->pos = 0;
2384 return l; 2522 return l;
2385} 2523}
2386 2524
@@ -2412,8 +2550,11 @@ static unsigned int fib_flag_trans(int type, __be32 mask, const struct fib_info
2412 */ 2550 */
2413static int fib_route_seq_show(struct seq_file *seq, void *v) 2551static int fib_route_seq_show(struct seq_file *seq, void *v)
2414{ 2552{
2415 struct tnode *l = v; 2553 struct fib_route_iter *iter = seq->private;
2416 struct leaf_info *li; 2554 struct fib_table *tb = iter->main_tb;
2555 struct fib_alias *fa;
2556 struct key_vector *l = v;
2557 __be32 prefix;
2417 2558
2418 if (v == SEQ_START_TOKEN) { 2559 if (v == SEQ_START_TOKEN) {
2419 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway " 2560 seq_printf(seq, "%-127s\n", "Iface\tDestination\tGateway "
@@ -2422,45 +2563,43 @@ static int fib_route_seq_show(struct seq_file *seq, void *v)
2422 return 0; 2563 return 0;
2423 } 2564 }
2424 2565
2425 hlist_for_each_entry_rcu(li, &l->list, hlist) { 2566 prefix = htonl(l->key);
2426 struct fib_alias *fa;
2427 __be32 mask, prefix;
2428 2567
2429 mask = inet_make_mask(li->plen); 2568 hlist_for_each_entry_rcu(fa, &l->leaf, fa_list) {
2430 prefix = htonl(l->key); 2569 const struct fib_info *fi = fa->fa_info;
2570 __be32 mask = inet_make_mask(KEYLENGTH - fa->fa_slen);
2571 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi);
2431 2572
2432 list_for_each_entry_rcu(fa, &li->falh, fa_list) { 2573 if ((fa->fa_type == RTN_BROADCAST) ||
2433 const struct fib_info *fi = fa->fa_info; 2574 (fa->fa_type == RTN_MULTICAST))
2434 unsigned int flags = fib_flag_trans(fa->fa_type, mask, fi); 2575 continue;
2435 2576
2436 if (fa->fa_type == RTN_BROADCAST 2577 if (fa->tb_id != tb->tb_id)
2437 || fa->fa_type == RTN_MULTICAST) 2578 continue;
2438 continue;
2439 2579
2440 seq_setwidth(seq, 127); 2580 seq_setwidth(seq, 127);
2441 2581
2442 if (fi) 2582 if (fi)
2443 seq_printf(seq, 2583 seq_printf(seq,
2444 "%s\t%08X\t%08X\t%04X\t%d\t%u\t" 2584 "%s\t%08X\t%08X\t%04X\t%d\t%u\t"
2445 "%d\t%08X\t%d\t%u\t%u", 2585 "%d\t%08X\t%d\t%u\t%u",
2446 fi->fib_dev ? fi->fib_dev->name : "*", 2586 fi->fib_dev ? fi->fib_dev->name : "*",
2447 prefix, 2587 prefix,
2448 fi->fib_nh->nh_gw, flags, 0, 0, 2588 fi->fib_nh->nh_gw, flags, 0, 0,
2449 fi->fib_priority, 2589 fi->fib_priority,
2450 mask, 2590 mask,
2451 (fi->fib_advmss ? 2591 (fi->fib_advmss ?
2452 fi->fib_advmss + 40 : 0), 2592 fi->fib_advmss + 40 : 0),
2453 fi->fib_window, 2593 fi->fib_window,
2454 fi->fib_rtt >> 3); 2594 fi->fib_rtt >> 3);
2455 else 2595 else
2456 seq_printf(seq, 2596 seq_printf(seq,
2457 "*\t%08X\t%08X\t%04X\t%d\t%u\t" 2597 "*\t%08X\t%08X\t%04X\t%d\t%u\t"
2458 "%d\t%08X\t%d\t%u\t%u", 2598 "%d\t%08X\t%d\t%u\t%u",
2459 prefix, 0, flags, 0, 0, 0, 2599 prefix, 0, flags, 0, 0, 0,
2460 mask, 0, 0, 0); 2600 mask, 0, 0, 0);
2461 2601
2462 seq_pad(seq, '\n'); 2602 seq_pad(seq, '\n');
2463 }
2464 } 2603 }
2465 2604
2466 return 0; 2605 return 0;
diff --git a/net/ipv4/fou.c b/net/ipv4/fou.c
index ff069f6597ac..34968cd5c146 100644
--- a/net/ipv4/fou.c
+++ b/net/ipv4/fou.c
@@ -16,14 +16,12 @@
16#include <uapi/linux/fou.h> 16#include <uapi/linux/fou.h>
17#include <uapi/linux/genetlink.h> 17#include <uapi/linux/genetlink.h>
18 18
19static DEFINE_SPINLOCK(fou_lock);
20static LIST_HEAD(fou_list);
21
22struct fou { 19struct fou {
23 struct socket *sock; 20 struct socket *sock;
24 u8 protocol; 21 u8 protocol;
25 u8 flags; 22 u8 flags;
26 u16 port; 23 __be16 port;
24 u16 type;
27 struct udp_offload udp_offloads; 25 struct udp_offload udp_offloads;
28 struct list_head list; 26 struct list_head list;
29}; 27};
@@ -37,6 +35,13 @@ struct fou_cfg {
37 struct udp_port_cfg udp_config; 35 struct udp_port_cfg udp_config;
38}; 36};
39 37
38static unsigned int fou_net_id;
39
40struct fou_net {
41 struct list_head fou_list;
42 struct mutex fou_lock;
43};
44
40static inline struct fou *fou_from_sock(struct sock *sk) 45static inline struct fou *fou_from_sock(struct sock *sk)
41{ 46{
42 return sk->sk_user_data; 47 return sk->sk_user_data;
@@ -387,20 +392,21 @@ out_unlock:
387 return err; 392 return err;
388} 393}
389 394
390static int fou_add_to_port_list(struct fou *fou) 395static int fou_add_to_port_list(struct net *net, struct fou *fou)
391{ 396{
397 struct fou_net *fn = net_generic(net, fou_net_id);
392 struct fou *fout; 398 struct fou *fout;
393 399
394 spin_lock(&fou_lock); 400 mutex_lock(&fn->fou_lock);
395 list_for_each_entry(fout, &fou_list, list) { 401 list_for_each_entry(fout, &fn->fou_list, list) {
396 if (fou->port == fout->port) { 402 if (fou->port == fout->port) {
397 spin_unlock(&fou_lock); 403 mutex_unlock(&fn->fou_lock);
398 return -EALREADY; 404 return -EALREADY;
399 } 405 }
400 } 406 }
401 407
402 list_add(&fou->list, &fou_list); 408 list_add(&fou->list, &fn->fou_list);
403 spin_unlock(&fou_lock); 409 mutex_unlock(&fn->fou_lock);
404 410
405 return 0; 411 return 0;
406} 412}
@@ -410,14 +416,10 @@ static void fou_release(struct fou *fou)
410 struct socket *sock = fou->sock; 416 struct socket *sock = fou->sock;
411 struct sock *sk = sock->sk; 417 struct sock *sk = sock->sk;
412 418
413 udp_del_offload(&fou->udp_offloads); 419 if (sk->sk_family == AF_INET)
414 420 udp_del_offload(&fou->udp_offloads);
415 list_del(&fou->list); 421 list_del(&fou->list);
416 422 udp_tunnel_sock_release(sock);
417 /* Remove hooks into tunnel socket */
418 sk->sk_user_data = NULL;
419
420 sock_release(sock);
421 423
422 kfree(fou); 424 kfree(fou);
423} 425}
@@ -447,10 +449,10 @@ static int gue_encap_init(struct sock *sk, struct fou *fou, struct fou_cfg *cfg)
447static int fou_create(struct net *net, struct fou_cfg *cfg, 449static int fou_create(struct net *net, struct fou_cfg *cfg,
448 struct socket **sockp) 450 struct socket **sockp)
449{ 451{
450 struct fou *fou = NULL;
451 int err;
452 struct socket *sock = NULL; 452 struct socket *sock = NULL;
453 struct fou *fou = NULL;
453 struct sock *sk; 454 struct sock *sk;
455 int err;
454 456
455 /* Open UDP socket */ 457 /* Open UDP socket */
456 err = udp_sock_create(net, &cfg->udp_config, &sock); 458 err = udp_sock_create(net, &cfg->udp_config, &sock);
@@ -486,6 +488,8 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
486 goto error; 488 goto error;
487 } 489 }
488 490
491 fou->type = cfg->type;
492
489 udp_sk(sk)->encap_type = 1; 493 udp_sk(sk)->encap_type = 1;
490 udp_encap_enable(); 494 udp_encap_enable();
491 495
@@ -502,7 +506,7 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
502 goto error; 506 goto error;
503 } 507 }
504 508
505 err = fou_add_to_port_list(fou); 509 err = fou_add_to_port_list(net, fou);
506 if (err) 510 if (err)
507 goto error; 511 goto error;
508 512
@@ -514,27 +518,27 @@ static int fou_create(struct net *net, struct fou_cfg *cfg,
514error: 518error:
515 kfree(fou); 519 kfree(fou);
516 if (sock) 520 if (sock)
517 sock_release(sock); 521 udp_tunnel_sock_release(sock);
518 522
519 return err; 523 return err;
520} 524}
521 525
522static int fou_destroy(struct net *net, struct fou_cfg *cfg) 526static int fou_destroy(struct net *net, struct fou_cfg *cfg)
523{ 527{
524 struct fou *fou; 528 struct fou_net *fn = net_generic(net, fou_net_id);
525 u16 port = cfg->udp_config.local_udp_port; 529 __be16 port = cfg->udp_config.local_udp_port;
526 int err = -EINVAL; 530 int err = -EINVAL;
531 struct fou *fou;
527 532
528 spin_lock(&fou_lock); 533 mutex_lock(&fn->fou_lock);
529 list_for_each_entry(fou, &fou_list, list) { 534 list_for_each_entry(fou, &fn->fou_list, list) {
530 if (fou->port == port) { 535 if (fou->port == port) {
531 udp_del_offload(&fou->udp_offloads);
532 fou_release(fou); 536 fou_release(fou);
533 err = 0; 537 err = 0;
534 break; 538 break;
535 } 539 }
536 } 540 }
537 spin_unlock(&fou_lock); 541 mutex_unlock(&fn->fou_lock);
538 542
539 return err; 543 return err;
540} 544}
@@ -573,7 +577,7 @@ static int parse_nl_config(struct genl_info *info,
573 } 577 }
574 578
575 if (info->attrs[FOU_ATTR_PORT]) { 579 if (info->attrs[FOU_ATTR_PORT]) {
576 u16 port = nla_get_u16(info->attrs[FOU_ATTR_PORT]); 580 __be16 port = nla_get_be16(info->attrs[FOU_ATTR_PORT]);
577 581
578 cfg->udp_config.local_udp_port = port; 582 cfg->udp_config.local_udp_port = port;
579 } 583 }
@@ -592,6 +596,7 @@ static int parse_nl_config(struct genl_info *info,
592 596
593static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info) 597static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
594{ 598{
599 struct net *net = genl_info_net(info);
595 struct fou_cfg cfg; 600 struct fou_cfg cfg;
596 int err; 601 int err;
597 602
@@ -599,16 +604,119 @@ static int fou_nl_cmd_add_port(struct sk_buff *skb, struct genl_info *info)
599 if (err) 604 if (err)
600 return err; 605 return err;
601 606
602 return fou_create(&init_net, &cfg, NULL); 607 return fou_create(net, &cfg, NULL);
603} 608}
604 609
605static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info) 610static int fou_nl_cmd_rm_port(struct sk_buff *skb, struct genl_info *info)
606{ 611{
612 struct net *net = genl_info_net(info);
607 struct fou_cfg cfg; 613 struct fou_cfg cfg;
614 int err;
608 615
609 parse_nl_config(info, &cfg); 616 err = parse_nl_config(info, &cfg);
617 if (err)
618 return err;
610 619
611 return fou_destroy(&init_net, &cfg); 620 return fou_destroy(net, &cfg);
621}
622
623static int fou_fill_info(struct fou *fou, struct sk_buff *msg)
624{
625 if (nla_put_u8(msg, FOU_ATTR_AF, fou->sock->sk->sk_family) ||
626 nla_put_be16(msg, FOU_ATTR_PORT, fou->port) ||
627 nla_put_u8(msg, FOU_ATTR_IPPROTO, fou->protocol) ||
628 nla_put_u8(msg, FOU_ATTR_TYPE, fou->type))
629 return -1;
630
631 if (fou->flags & FOU_F_REMCSUM_NOPARTIAL)
632 if (nla_put_flag(msg, FOU_ATTR_REMCSUM_NOPARTIAL))
633 return -1;
634 return 0;
635}
636
637static int fou_dump_info(struct fou *fou, u32 portid, u32 seq,
638 u32 flags, struct sk_buff *skb, u8 cmd)
639{
640 void *hdr;
641
642 hdr = genlmsg_put(skb, portid, seq, &fou_nl_family, flags, cmd);
643 if (!hdr)
644 return -ENOMEM;
645
646 if (fou_fill_info(fou, skb) < 0)
647 goto nla_put_failure;
648
649 genlmsg_end(skb, hdr);
650 return 0;
651
652nla_put_failure:
653 genlmsg_cancel(skb, hdr);
654 return -EMSGSIZE;
655}
656
657static int fou_nl_cmd_get_port(struct sk_buff *skb, struct genl_info *info)
658{
659 struct net *net = genl_info_net(info);
660 struct fou_net *fn = net_generic(net, fou_net_id);
661 struct sk_buff *msg;
662 struct fou_cfg cfg;
663 struct fou *fout;
664 __be16 port;
665 int ret;
666
667 ret = parse_nl_config(info, &cfg);
668 if (ret)
669 return ret;
670 port = cfg.udp_config.local_udp_port;
671 if (port == 0)
672 return -EINVAL;
673
674 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
675 if (!msg)
676 return -ENOMEM;
677
678 ret = -ESRCH;
679 mutex_lock(&fn->fou_lock);
680 list_for_each_entry(fout, &fn->fou_list, list) {
681 if (port == fout->port) {
682 ret = fou_dump_info(fout, info->snd_portid,
683 info->snd_seq, 0, msg,
684 info->genlhdr->cmd);
685 break;
686 }
687 }
688 mutex_unlock(&fn->fou_lock);
689 if (ret < 0)
690 goto out_free;
691
692 return genlmsg_reply(msg, info);
693
694out_free:
695 nlmsg_free(msg);
696 return ret;
697}
698
699static int fou_nl_dump(struct sk_buff *skb, struct netlink_callback *cb)
700{
701 struct net *net = sock_net(skb->sk);
702 struct fou_net *fn = net_generic(net, fou_net_id);
703 struct fou *fout;
704 int idx = 0, ret;
705
706 mutex_lock(&fn->fou_lock);
707 list_for_each_entry(fout, &fn->fou_list, list) {
708 if (idx++ < cb->args[0])
709 continue;
710 ret = fou_dump_info(fout, NETLINK_CB(cb->skb).portid,
711 cb->nlh->nlmsg_seq, NLM_F_MULTI,
712 skb, FOU_CMD_GET);
713 if (ret)
714 break;
715 }
716 mutex_unlock(&fn->fou_lock);
717
718 cb->args[0] = idx;
719 return skb->len;
612} 720}
613 721
614static const struct genl_ops fou_nl_ops[] = { 722static const struct genl_ops fou_nl_ops[] = {
@@ -624,6 +732,12 @@ static const struct genl_ops fou_nl_ops[] = {
624 .policy = fou_nl_policy, 732 .policy = fou_nl_policy,
625 .flags = GENL_ADMIN_PERM, 733 .flags = GENL_ADMIN_PERM,
626 }, 734 },
735 {
736 .cmd = FOU_CMD_GET,
737 .doit = fou_nl_cmd_get_port,
738 .dumpit = fou_nl_dump,
739 .policy = fou_nl_policy,
740 },
627}; 741};
628 742
629size_t fou_encap_hlen(struct ip_tunnel_encap *e) 743size_t fou_encap_hlen(struct ip_tunnel_encap *e)
@@ -771,12 +885,12 @@ EXPORT_SYMBOL(gue_build_header);
771 885
772#ifdef CONFIG_NET_FOU_IP_TUNNELS 886#ifdef CONFIG_NET_FOU_IP_TUNNELS
773 887
774static const struct ip_tunnel_encap_ops __read_mostly fou_iptun_ops = { 888static const struct ip_tunnel_encap_ops fou_iptun_ops = {
775 .encap_hlen = fou_encap_hlen, 889 .encap_hlen = fou_encap_hlen,
776 .build_header = fou_build_header, 890 .build_header = fou_build_header,
777}; 891};
778 892
779static const struct ip_tunnel_encap_ops __read_mostly gue_iptun_ops = { 893static const struct ip_tunnel_encap_ops gue_iptun_ops = {
780 .encap_hlen = gue_encap_hlen, 894 .encap_hlen = gue_encap_hlen,
781 .build_header = gue_build_header, 895 .build_header = gue_build_header,
782}; 896};
@@ -820,38 +934,63 @@ static void ip_tunnel_encap_del_fou_ops(void)
820 934
821#endif 935#endif
822 936
937static __net_init int fou_init_net(struct net *net)
938{
939 struct fou_net *fn = net_generic(net, fou_net_id);
940
941 INIT_LIST_HEAD(&fn->fou_list);
942 mutex_init(&fn->fou_lock);
943 return 0;
944}
945
946static __net_exit void fou_exit_net(struct net *net)
947{
948 struct fou_net *fn = net_generic(net, fou_net_id);
949 struct fou *fou, *next;
950
951 /* Close all the FOU sockets */
952 mutex_lock(&fn->fou_lock);
953 list_for_each_entry_safe(fou, next, &fn->fou_list, list)
954 fou_release(fou);
955 mutex_unlock(&fn->fou_lock);
956}
957
958static struct pernet_operations fou_net_ops = {
959 .init = fou_init_net,
960 .exit = fou_exit_net,
961 .id = &fou_net_id,
962 .size = sizeof(struct fou_net),
963};
964
823static int __init fou_init(void) 965static int __init fou_init(void)
824{ 966{
825 int ret; 967 int ret;
826 968
969 ret = register_pernet_device(&fou_net_ops);
970 if (ret)
971 goto exit;
972
827 ret = genl_register_family_with_ops(&fou_nl_family, 973 ret = genl_register_family_with_ops(&fou_nl_family,
828 fou_nl_ops); 974 fou_nl_ops);
829
830 if (ret < 0) 975 if (ret < 0)
831 goto exit; 976 goto unregister;
832 977
833 ret = ip_tunnel_encap_add_fou_ops(); 978 ret = ip_tunnel_encap_add_fou_ops();
834 if (ret < 0) 979 if (ret == 0)
835 genl_unregister_family(&fou_nl_family); 980 return 0;
836 981
982 genl_unregister_family(&fou_nl_family);
983unregister:
984 unregister_pernet_device(&fou_net_ops);
837exit: 985exit:
838 return ret; 986 return ret;
839} 987}
840 988
841static void __exit fou_fini(void) 989static void __exit fou_fini(void)
842{ 990{
843 struct fou *fou, *next;
844
845 ip_tunnel_encap_del_fou_ops(); 991 ip_tunnel_encap_del_fou_ops();
846
847 genl_unregister_family(&fou_nl_family); 992 genl_unregister_family(&fou_nl_family);
848 993 unregister_pernet_device(&fou_net_ops);
849 /* Close all the FOU sockets */
850
851 spin_lock(&fou_lock);
852 list_for_each_entry_safe(fou, next, &fou_list, list)
853 fou_release(fou);
854 spin_unlock(&fou_lock);
855} 994}
856 995
857module_init(fou_init); 996module_init(fou_init);
diff --git a/net/ipv4/geneve.c b/net/ipv4/geneve.c
index 5a4828ba05ad..8986e63f3bda 100644
--- a/net/ipv4/geneve.c
+++ b/net/ipv4/geneve.c
@@ -113,10 +113,6 @@ int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
113 int min_headroom; 113 int min_headroom;
114 int err; 114 int err;
115 115
116 skb = udp_tunnel_handle_offloads(skb, csum);
117 if (IS_ERR(skb))
118 return PTR_ERR(skb);
119
120 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len 116 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
121 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr) 117 + GENEVE_BASE_HLEN + opt_len + sizeof(struct iphdr)
122 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0); 118 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
@@ -131,12 +127,16 @@ int geneve_xmit_skb(struct geneve_sock *gs, struct rtable *rt,
131 if (unlikely(!skb)) 127 if (unlikely(!skb))
132 return -ENOMEM; 128 return -ENOMEM;
133 129
130 skb = udp_tunnel_handle_offloads(skb, csum);
131 if (IS_ERR(skb))
132 return PTR_ERR(skb);
133
134 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len); 134 gnvh = (struct genevehdr *)__skb_push(skb, sizeof(*gnvh) + opt_len);
135 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt); 135 geneve_build_header(gnvh, tun_flags, vni, opt_len, opt);
136 136
137 skb_set_inner_protocol(skb, htons(ETH_P_TEB)); 137 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
138 138
139 return udp_tunnel_xmit_skb(rt, skb, src, dst, 139 return udp_tunnel_xmit_skb(rt, gs->sock->sk, skb, src, dst,
140 tos, ttl, df, src_port, dst_port, xnet, 140 tos, ttl, df, src_port, dst_port, xnet,
141 !csum); 141 !csum);
142} 142}
@@ -196,7 +196,7 @@ static struct sk_buff **geneve_gro_receive(struct sk_buff **head,
196 196
197 rcu_read_lock(); 197 rcu_read_lock();
198 ptype = gro_find_receive_by_type(type); 198 ptype = gro_find_receive_by_type(type);
199 if (ptype == NULL) { 199 if (!ptype) {
200 flush = 1; 200 flush = 1;
201 goto out_unlock; 201 goto out_unlock;
202 } 202 }
@@ -230,7 +230,7 @@ static int geneve_gro_complete(struct sk_buff *skb, int nhoff,
230 230
231 rcu_read_lock(); 231 rcu_read_lock();
232 ptype = gro_find_complete_by_type(type); 232 ptype = gro_find_complete_by_type(type);
233 if (ptype != NULL) 233 if (ptype)
234 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len); 234 err = ptype->callbacks.gro_complete(skb, nhoff + gh_len);
235 235
236 rcu_read_unlock(); 236 rcu_read_unlock();
diff --git a/net/ipv4/gre_offload.c b/net/ipv4/gre_offload.c
index 51973ddc05a6..5aa46d4b44ef 100644
--- a/net/ipv4/gre_offload.c
+++ b/net/ipv4/gre_offload.c
@@ -149,7 +149,7 @@ static struct sk_buff **gre_gro_receive(struct sk_buff **head,
149 149
150 rcu_read_lock(); 150 rcu_read_lock();
151 ptype = gro_find_receive_by_type(type); 151 ptype = gro_find_receive_by_type(type);
152 if (ptype == NULL) 152 if (!ptype)
153 goto out_unlock; 153 goto out_unlock;
154 154
155 grehlen = GRE_HEADER_SECTION; 155 grehlen = GRE_HEADER_SECTION;
@@ -243,7 +243,7 @@ static int gre_gro_complete(struct sk_buff *skb, int nhoff)
243 243
244 rcu_read_lock(); 244 rcu_read_lock();
245 ptype = gro_find_complete_by_type(type); 245 ptype = gro_find_complete_by_type(type);
246 if (ptype != NULL) 246 if (ptype)
247 err = ptype->callbacks.gro_complete(skb, nhoff + grehlen); 247 err = ptype->callbacks.gro_complete(skb, nhoff + grehlen);
248 248
249 rcu_read_unlock(); 249 rcu_read_unlock();
diff --git a/net/ipv4/icmp.c b/net/ipv4/icmp.c
index 5e564014a0b7..f5203fba6236 100644
--- a/net/ipv4/icmp.c
+++ b/net/ipv4/icmp.c
@@ -399,7 +399,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
399 return; 399 return;
400 400
401 sk = icmp_xmit_lock(net); 401 sk = icmp_xmit_lock(net);
402 if (sk == NULL) 402 if (!sk)
403 return; 403 return;
404 inet = inet_sk(sk); 404 inet = inet_sk(sk);
405 405
@@ -609,7 +609,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
609 skb_in->data, 609 skb_in->data,
610 sizeof(_inner_type), 610 sizeof(_inner_type),
611 &_inner_type); 611 &_inner_type);
612 if (itp == NULL) 612 if (!itp)
613 goto out; 613 goto out;
614 614
615 /* 615 /*
@@ -627,7 +627,7 @@ void icmp_send(struct sk_buff *skb_in, int type, int code, __be32 info)
627 return; 627 return;
628 628
629 sk = icmp_xmit_lock(net); 629 sk = icmp_xmit_lock(net);
630 if (sk == NULL) 630 if (!sk)
631 goto out_free; 631 goto out_free;
632 632
633 /* 633 /*
diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
index 666cf364df86..a3a697f5ffba 100644
--- a/net/ipv4/igmp.c
+++ b/net/ipv4/igmp.c
@@ -97,6 +97,7 @@
97#include <net/route.h> 97#include <net/route.h>
98#include <net/sock.h> 98#include <net/sock.h>
99#include <net/checksum.h> 99#include <net/checksum.h>
100#include <net/inet_common.h>
100#include <linux/netfilter_ipv4.h> 101#include <linux/netfilter_ipv4.h>
101#ifdef CONFIG_IP_MROUTE 102#ifdef CONFIG_IP_MROUTE
102#include <linux/mroute.h> 103#include <linux/mroute.h>
@@ -369,7 +370,7 @@ static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu)
369 pip->saddr = fl4.saddr; 370 pip->saddr = fl4.saddr;
370 pip->protocol = IPPROTO_IGMP; 371 pip->protocol = IPPROTO_IGMP;
371 pip->tot_len = 0; /* filled in later */ 372 pip->tot_len = 0; /* filled in later */
372 ip_select_ident(skb, NULL); 373 ip_select_ident(net, skb, NULL);
373 ((u8 *)&pip[1])[0] = IPOPT_RA; 374 ((u8 *)&pip[1])[0] = IPOPT_RA;
374 ((u8 *)&pip[1])[1] = 4; 375 ((u8 *)&pip[1])[1] = 4;
375 ((u8 *)&pip[1])[2] = 0; 376 ((u8 *)&pip[1])[2] = 0;
@@ -691,7 +692,7 @@ static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
691 hlen = LL_RESERVED_SPACE(dev); 692 hlen = LL_RESERVED_SPACE(dev);
692 tlen = dev->needed_tailroom; 693 tlen = dev->needed_tailroom;
693 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); 694 skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC);
694 if (skb == NULL) { 695 if (!skb) {
695 ip_rt_put(rt); 696 ip_rt_put(rt);
696 return -1; 697 return -1;
697 } 698 }
@@ -713,7 +714,7 @@ static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
713 iph->daddr = dst; 714 iph->daddr = dst;
714 iph->saddr = fl4.saddr; 715 iph->saddr = fl4.saddr;
715 iph->protocol = IPPROTO_IGMP; 716 iph->protocol = IPPROTO_IGMP;
716 ip_select_ident(skb, NULL); 717 ip_select_ident(net, skb, NULL);
717 ((u8 *)&iph[1])[0] = IPOPT_RA; 718 ((u8 *)&iph[1])[0] = IPOPT_RA;
718 ((u8 *)&iph[1])[1] = 4; 719 ((u8 *)&iph[1])[1] = 4;
719 ((u8 *)&iph[1])[2] = 0; 720 ((u8 *)&iph[1])[2] = 0;
@@ -980,7 +981,7 @@ int igmp_rcv(struct sk_buff *skb)
980 int len = skb->len; 981 int len = skb->len;
981 bool dropped = true; 982 bool dropped = true;
982 983
983 if (in_dev == NULL) 984 if (!in_dev)
984 goto drop; 985 goto drop;
985 986
986 if (!pskb_may_pull(skb, sizeof(struct igmphdr))) 987 if (!pskb_may_pull(skb, sizeof(struct igmphdr)))
@@ -1849,30 +1850,28 @@ static void ip_mc_clear_src(struct ip_mc_list *pmc)
1849 pmc->sfcount[MCAST_EXCLUDE] = 1; 1850 pmc->sfcount[MCAST_EXCLUDE] = 1;
1850} 1851}
1851 1852
1852 1853/* Join a multicast group
1853/*
1854 * Join a multicast group
1855 */ 1854 */
1856int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr) 1855
1856int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr)
1857{ 1857{
1858 int err;
1859 __be32 addr = imr->imr_multiaddr.s_addr; 1858 __be32 addr = imr->imr_multiaddr.s_addr;
1860 struct ip_mc_socklist *iml = NULL, *i; 1859 struct ip_mc_socklist *iml, *i;
1861 struct in_device *in_dev; 1860 struct in_device *in_dev;
1862 struct inet_sock *inet = inet_sk(sk); 1861 struct inet_sock *inet = inet_sk(sk);
1863 struct net *net = sock_net(sk); 1862 struct net *net = sock_net(sk);
1864 int ifindex; 1863 int ifindex;
1865 int count = 0; 1864 int count = 0;
1865 int err;
1866
1867 ASSERT_RTNL();
1866 1868
1867 if (!ipv4_is_multicast(addr)) 1869 if (!ipv4_is_multicast(addr))
1868 return -EINVAL; 1870 return -EINVAL;
1869 1871
1870 rtnl_lock();
1871
1872 in_dev = ip_mc_find_dev(net, imr); 1872 in_dev = ip_mc_find_dev(net, imr);
1873 1873
1874 if (!in_dev) { 1874 if (!in_dev) {
1875 iml = NULL;
1876 err = -ENODEV; 1875 err = -ENODEV;
1877 goto done; 1876 goto done;
1878 } 1877 }
@@ -1889,7 +1888,7 @@ int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1889 if (count >= sysctl_igmp_max_memberships) 1888 if (count >= sysctl_igmp_max_memberships)
1890 goto done; 1889 goto done;
1891 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); 1890 iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1892 if (iml == NULL) 1891 if (!iml)
1893 goto done; 1892 goto done;
1894 1893
1895 memcpy(&iml->multi, imr, sizeof(*imr)); 1894 memcpy(&iml->multi, imr, sizeof(*imr));
@@ -1900,7 +1899,6 @@ int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1900 ip_mc_inc_group(in_dev, addr); 1899 ip_mc_inc_group(in_dev, addr);
1901 err = 0; 1900 err = 0;
1902done: 1901done:
1903 rtnl_unlock();
1904 return err; 1902 return err;
1905} 1903}
1906EXPORT_SYMBOL(ip_mc_join_group); 1904EXPORT_SYMBOL(ip_mc_join_group);
@@ -1911,7 +1909,7 @@ static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1911 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); 1909 struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist);
1912 int err; 1910 int err;
1913 1911
1914 if (psf == NULL) { 1912 if (!psf) {
1915 /* any-source empty exclude case */ 1913 /* any-source empty exclude case */
1916 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, 1914 return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1917 iml->sfmode, 0, NULL, 0); 1915 iml->sfmode, 0, NULL, 0);
@@ -1925,10 +1923,6 @@ static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1925 return err; 1923 return err;
1926} 1924}
1927 1925
1928/*
1929 * Ask a socket to leave a group.
1930 */
1931
1932int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) 1926int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1933{ 1927{
1934 struct inet_sock *inet = inet_sk(sk); 1928 struct inet_sock *inet = inet_sk(sk);
@@ -1940,7 +1934,8 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1940 u32 ifindex; 1934 u32 ifindex;
1941 int ret = -EADDRNOTAVAIL; 1935 int ret = -EADDRNOTAVAIL;
1942 1936
1943 rtnl_lock(); 1937 ASSERT_RTNL();
1938
1944 in_dev = ip_mc_find_dev(net, imr); 1939 in_dev = ip_mc_find_dev(net, imr);
1945 if (!in_dev) { 1940 if (!in_dev) {
1946 ret = -ENODEV; 1941 ret = -ENODEV;
@@ -1964,14 +1959,13 @@ int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1964 *imlp = iml->next_rcu; 1959 *imlp = iml->next_rcu;
1965 1960
1966 ip_mc_dec_group(in_dev, group); 1961 ip_mc_dec_group(in_dev, group);
1967 rtnl_unlock(); 1962
1968 /* decrease mem now to avoid the memleak warning */ 1963 /* decrease mem now to avoid the memleak warning */
1969 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 1964 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
1970 kfree_rcu(iml, rcu); 1965 kfree_rcu(iml, rcu);
1971 return 0; 1966 return 0;
1972 } 1967 }
1973out: 1968out:
1974 rtnl_unlock();
1975 return ret; 1969 return ret;
1976} 1970}
1977EXPORT_SYMBOL(ip_mc_leave_group); 1971EXPORT_SYMBOL(ip_mc_leave_group);
@@ -1993,7 +1987,7 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
1993 if (!ipv4_is_multicast(addr)) 1987 if (!ipv4_is_multicast(addr))
1994 return -EINVAL; 1988 return -EINVAL;
1995 1989
1996 rtnl_lock(); 1990 ASSERT_RTNL();
1997 1991
1998 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; 1992 imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1999 imr.imr_address.s_addr = mreqs->imr_interface; 1993 imr.imr_address.s_addr = mreqs->imr_interface;
@@ -2107,9 +2101,8 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
2107 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 2101 ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1,
2108 &mreqs->imr_sourceaddr, 1); 2102 &mreqs->imr_sourceaddr, 1);
2109done: 2103done:
2110 rtnl_unlock();
2111 if (leavegroup) 2104 if (leavegroup)
2112 return ip_mc_leave_group(sk, &imr); 2105 err = ip_mc_leave_group(sk, &imr);
2113 return err; 2106 return err;
2114} 2107}
2115 2108
@@ -2131,7 +2124,7 @@ int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2131 msf->imsf_fmode != MCAST_EXCLUDE) 2124 msf->imsf_fmode != MCAST_EXCLUDE)
2132 return -EINVAL; 2125 return -EINVAL;
2133 2126
2134 rtnl_lock(); 2127 ASSERT_RTNL();
2135 2128
2136 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; 2129 imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
2137 imr.imr_address.s_addr = msf->imsf_interface; 2130 imr.imr_address.s_addr = msf->imsf_interface;
@@ -2193,7 +2186,6 @@ int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
2193 pmc->sfmode = msf->imsf_fmode; 2186 pmc->sfmode = msf->imsf_fmode;
2194 err = 0; 2187 err = 0;
2195done: 2188done:
2196 rtnl_unlock();
2197 if (leavegroup) 2189 if (leavegroup)
2198 err = ip_mc_leave_group(sk, &imr); 2190 err = ip_mc_leave_group(sk, &imr);
2199 return err; 2191 return err;
@@ -2368,7 +2360,7 @@ void ip_mc_drop_socket(struct sock *sk)
2368 struct ip_mc_socklist *iml; 2360 struct ip_mc_socklist *iml;
2369 struct net *net = sock_net(sk); 2361 struct net *net = sock_net(sk);
2370 2362
2371 if (inet->mc_list == NULL) 2363 if (!inet->mc_list)
2372 return; 2364 return;
2373 2365
2374 rtnl_lock(); 2366 rtnl_lock();
@@ -2378,7 +2370,7 @@ void ip_mc_drop_socket(struct sock *sk)
2378 inet->mc_list = iml->next_rcu; 2370 inet->mc_list = iml->next_rcu;
2379 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); 2371 in_dev = inetdev_by_index(net, iml->multi.imr_ifindex);
2380 (void) ip_mc_leave_src(sk, iml, in_dev); 2372 (void) ip_mc_leave_src(sk, iml, in_dev);
2381 if (in_dev != NULL) 2373 if (in_dev)
2382 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); 2374 ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2383 /* decrease mem now to avoid the memleak warning */ 2375 /* decrease mem now to avoid the memleak warning */
2384 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); 2376 atomic_sub(sizeof(*iml), &sk->sk_omem_alloc);
@@ -2595,13 +2587,13 @@ static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2595 for_each_netdev_rcu(net, state->dev) { 2587 for_each_netdev_rcu(net, state->dev) {
2596 struct in_device *idev; 2588 struct in_device *idev;
2597 idev = __in_dev_get_rcu(state->dev); 2589 idev = __in_dev_get_rcu(state->dev);
2598 if (unlikely(idev == NULL)) 2590 if (unlikely(!idev))
2599 continue; 2591 continue;
2600 im = rcu_dereference(idev->mc_list); 2592 im = rcu_dereference(idev->mc_list);
2601 if (likely(im != NULL)) { 2593 if (likely(im)) {
2602 spin_lock_bh(&im->lock); 2594 spin_lock_bh(&im->lock);
2603 psf = im->sources; 2595 psf = im->sources;
2604 if (likely(psf != NULL)) { 2596 if (likely(psf)) {
2605 state->im = im; 2597 state->im = im;
2606 state->idev = idev; 2598 state->idev = idev;
2607 break; 2599 break;
@@ -2671,7 +2663,7 @@ static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2671 __releases(rcu) 2663 __releases(rcu)
2672{ 2664{
2673 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); 2665 struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2674 if (likely(state->im != NULL)) { 2666 if (likely(state->im)) {
2675 spin_unlock_bh(&state->im->lock); 2667 spin_unlock_bh(&state->im->lock);
2676 state->im = NULL; 2668 state->im = NULL;
2677 } 2669 }
@@ -2724,6 +2716,7 @@ static const struct file_operations igmp_mcf_seq_fops = {
2724static int __net_init igmp_net_init(struct net *net) 2716static int __net_init igmp_net_init(struct net *net)
2725{ 2717{
2726 struct proc_dir_entry *pde; 2718 struct proc_dir_entry *pde;
2719 int err;
2727 2720
2728 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); 2721 pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops);
2729 if (!pde) 2722 if (!pde)
@@ -2732,8 +2725,18 @@ static int __net_init igmp_net_init(struct net *net)
2732 &igmp_mcf_seq_fops); 2725 &igmp_mcf_seq_fops);
2733 if (!pde) 2726 if (!pde)
2734 goto out_mcfilter; 2727 goto out_mcfilter;
2728 err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET,
2729 SOCK_DGRAM, 0, net);
2730 if (err < 0) {
2731 pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n",
2732 err);
2733 goto out_sock;
2734 }
2735
2735 return 0; 2736 return 0;
2736 2737
2738out_sock:
2739 remove_proc_entry("mcfilter", net->proc_net);
2737out_mcfilter: 2740out_mcfilter:
2738 remove_proc_entry("igmp", net->proc_net); 2741 remove_proc_entry("igmp", net->proc_net);
2739out_igmp: 2742out_igmp:
@@ -2744,6 +2747,7 @@ static void __net_exit igmp_net_exit(struct net *net)
2744{ 2747{
2745 remove_proc_entry("mcfilter", net->proc_net); 2748 remove_proc_entry("mcfilter", net->proc_net);
2746 remove_proc_entry("igmp", net->proc_net); 2749 remove_proc_entry("igmp", net->proc_net);
2750 inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk);
2747} 2751}
2748 2752
2749static struct pernet_operations igmp_net_ops = { 2753static struct pernet_operations igmp_net_ops = {
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 3e44b9b0b78e..5c3dd6267ed3 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -23,6 +23,7 @@
23#include <net/route.h> 23#include <net/route.h>
24#include <net/tcp_states.h> 24#include <net/tcp_states.h>
25#include <net/xfrm.h> 25#include <net/xfrm.h>
26#include <net/tcp.h>
26 27
27#ifdef INET_CSK_DEBUG 28#ifdef INET_CSK_DEBUG
28const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n"; 29const char inet_csk_timer_bug_msg[] = "inet_csk BUG: unknown timer value\n";
@@ -294,8 +295,8 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
294{ 295{
295 struct inet_connection_sock *icsk = inet_csk(sk); 296 struct inet_connection_sock *icsk = inet_csk(sk);
296 struct request_sock_queue *queue = &icsk->icsk_accept_queue; 297 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
297 struct sock *newsk;
298 struct request_sock *req; 298 struct request_sock *req;
299 struct sock *newsk;
299 int error; 300 int error;
300 301
301 lock_sock(sk); 302 lock_sock(sk);
@@ -324,9 +325,11 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
324 newsk = req->sk; 325 newsk = req->sk;
325 326
326 sk_acceptq_removed(sk); 327 sk_acceptq_removed(sk);
327 if (sk->sk_protocol == IPPROTO_TCP && queue->fastopenq != NULL) { 328 if (sk->sk_protocol == IPPROTO_TCP &&
329 tcp_rsk(req)->tfo_listener &&
330 queue->fastopenq) {
328 spin_lock_bh(&queue->fastopenq->lock); 331 spin_lock_bh(&queue->fastopenq->lock);
329 if (tcp_rsk(req)->listener) { 332 if (tcp_rsk(req)->tfo_listener) {
330 /* We are still waiting for the final ACK from 3WHS 333 /* We are still waiting for the final ACK from 3WHS
331 * so can't free req now. Instead, we set req->sk to 334 * so can't free req now. Instead, we set req->sk to
332 * NULL to signify that the child socket is taken 335 * NULL to signify that the child socket is taken
@@ -341,7 +344,7 @@ struct sock *inet_csk_accept(struct sock *sk, int flags, int *err)
341out: 344out:
342 release_sock(sk); 345 release_sock(sk);
343 if (req) 346 if (req)
344 __reqsk_free(req); 347 reqsk_put(req);
345 return newsk; 348 return newsk;
346out_err: 349out_err:
347 newsk = NULL; 350 newsk = NULL;
@@ -400,18 +403,17 @@ struct dst_entry *inet_csk_route_req(struct sock *sk,
400 struct flowi4 *fl4, 403 struct flowi4 *fl4,
401 const struct request_sock *req) 404 const struct request_sock *req)
402{ 405{
403 struct rtable *rt;
404 const struct inet_request_sock *ireq = inet_rsk(req); 406 const struct inet_request_sock *ireq = inet_rsk(req);
405 struct ip_options_rcu *opt = inet_rsk(req)->opt; 407 struct net *net = read_pnet(&ireq->ireq_net);
406 struct net *net = sock_net(sk); 408 struct ip_options_rcu *opt = ireq->opt;
407 int flags = inet_sk_flowi_flags(sk); 409 struct rtable *rt;
408 410
409 flowi4_init_output(fl4, sk->sk_bound_dev_if, ireq->ir_mark, 411 flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
410 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 412 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
411 sk->sk_protocol, 413 sk->sk_protocol, inet_sk_flowi_flags(sk),
412 flags,
413 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, 414 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
414 ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport); 415 ireq->ir_loc_addr, ireq->ir_rmt_port,
416 htons(ireq->ir_num));
415 security_req_classify_flow(req, flowi4_to_flowi(fl4)); 417 security_req_classify_flow(req, flowi4_to_flowi(fl4));
416 rt = ip_route_output_flow(net, fl4, sk); 418 rt = ip_route_output_flow(net, fl4, sk);
417 if (IS_ERR(rt)) 419 if (IS_ERR(rt))
@@ -433,9 +435,9 @@ struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
433 const struct request_sock *req) 435 const struct request_sock *req)
434{ 436{
435 const struct inet_request_sock *ireq = inet_rsk(req); 437 const struct inet_request_sock *ireq = inet_rsk(req);
438 struct net *net = read_pnet(&ireq->ireq_net);
436 struct inet_sock *newinet = inet_sk(newsk); 439 struct inet_sock *newinet = inet_sk(newsk);
437 struct ip_options_rcu *opt; 440 struct ip_options_rcu *opt;
438 struct net *net = sock_net(sk);
439 struct flowi4 *fl4; 441 struct flowi4 *fl4;
440 struct rtable *rt; 442 struct rtable *rt;
441 443
@@ -443,11 +445,12 @@ struct dst_entry *inet_csk_route_child_sock(struct sock *sk,
443 445
444 rcu_read_lock(); 446 rcu_read_lock();
445 opt = rcu_dereference(newinet->inet_opt); 447 opt = rcu_dereference(newinet->inet_opt);
446 flowi4_init_output(fl4, sk->sk_bound_dev_if, inet_rsk(req)->ir_mark, 448 flowi4_init_output(fl4, ireq->ir_iif, ireq->ir_mark,
447 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE, 449 RT_CONN_FLAGS(sk), RT_SCOPE_UNIVERSE,
448 sk->sk_protocol, inet_sk_flowi_flags(sk), 450 sk->sk_protocol, inet_sk_flowi_flags(sk),
449 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr, 451 (opt && opt->opt.srr) ? opt->opt.faddr : ireq->ir_rmt_addr,
450 ireq->ir_loc_addr, ireq->ir_rmt_port, inet_sk(sk)->inet_sport); 452 ireq->ir_loc_addr, ireq->ir_rmt_port,
453 htons(ireq->ir_num));
451 security_req_classify_flow(req, flowi4_to_flowi(fl4)); 454 security_req_classify_flow(req, flowi4_to_flowi(fl4));
452 rt = ip_route_output_flow(net, fl4, sk); 455 rt = ip_route_output_flow(net, fl4, sk);
453 if (IS_ERR(rt)) 456 if (IS_ERR(rt))
@@ -475,33 +478,37 @@ static inline u32 inet_synq_hash(const __be32 raddr, const __be16 rport,
475#if IS_ENABLED(CONFIG_IPV6) 478#if IS_ENABLED(CONFIG_IPV6)
476#define AF_INET_FAMILY(fam) ((fam) == AF_INET) 479#define AF_INET_FAMILY(fam) ((fam) == AF_INET)
477#else 480#else
478#define AF_INET_FAMILY(fam) 1 481#define AF_INET_FAMILY(fam) true
479#endif 482#endif
480 483
481struct request_sock *inet_csk_search_req(const struct sock *sk, 484/* Note: this is temporary :
482 struct request_sock ***prevp, 485 * req sock will no longer be in listener hash table
483 const __be16 rport, const __be32 raddr, 486*/
487struct request_sock *inet_csk_search_req(struct sock *sk,
488 const __be16 rport,
489 const __be32 raddr,
484 const __be32 laddr) 490 const __be32 laddr)
485{ 491{
486 const struct inet_connection_sock *icsk = inet_csk(sk); 492 struct inet_connection_sock *icsk = inet_csk(sk);
487 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; 493 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
488 struct request_sock *req, **prev; 494 struct request_sock *req;
495 u32 hash = inet_synq_hash(raddr, rport, lopt->hash_rnd,
496 lopt->nr_table_entries);
489 497
490 for (prev = &lopt->syn_table[inet_synq_hash(raddr, rport, lopt->hash_rnd, 498 spin_lock(&icsk->icsk_accept_queue.syn_wait_lock);
491 lopt->nr_table_entries)]; 499 for (req = lopt->syn_table[hash]; req != NULL; req = req->dl_next) {
492 (req = *prev) != NULL;
493 prev = &req->dl_next) {
494 const struct inet_request_sock *ireq = inet_rsk(req); 500 const struct inet_request_sock *ireq = inet_rsk(req);
495 501
496 if (ireq->ir_rmt_port == rport && 502 if (ireq->ir_rmt_port == rport &&
497 ireq->ir_rmt_addr == raddr && 503 ireq->ir_rmt_addr == raddr &&
498 ireq->ir_loc_addr == laddr && 504 ireq->ir_loc_addr == laddr &&
499 AF_INET_FAMILY(req->rsk_ops->family)) { 505 AF_INET_FAMILY(req->rsk_ops->family)) {
506 atomic_inc(&req->rsk_refcnt);
500 WARN_ON(req->sk); 507 WARN_ON(req->sk);
501 *prevp = prev;
502 break; 508 break;
503 } 509 }
504 } 510 }
511 spin_unlock(&icsk->icsk_accept_queue.syn_wait_lock);
505 512
506 return req; 513 return req;
507} 514}
@@ -557,23 +564,24 @@ int inet_rtx_syn_ack(struct sock *parent, struct request_sock *req)
557} 564}
558EXPORT_SYMBOL(inet_rtx_syn_ack); 565EXPORT_SYMBOL(inet_rtx_syn_ack);
559 566
560void inet_csk_reqsk_queue_prune(struct sock *parent, 567static void reqsk_timer_handler(unsigned long data)
561 const unsigned long interval,
562 const unsigned long timeout,
563 const unsigned long max_rto)
564{ 568{
565 struct inet_connection_sock *icsk = inet_csk(parent); 569 struct request_sock *req = (struct request_sock *)data;
570 struct sock *sk_listener = req->rsk_listener;
571 struct inet_connection_sock *icsk = inet_csk(sk_listener);
566 struct request_sock_queue *queue = &icsk->icsk_accept_queue; 572 struct request_sock_queue *queue = &icsk->icsk_accept_queue;
567 struct listen_sock *lopt = queue->listen_opt; 573 struct listen_sock *lopt = queue->listen_opt;
568 int max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries; 574 int qlen, expire = 0, resend = 0;
569 int thresh = max_retries; 575 int max_retries, thresh;
570 unsigned long now = jiffies; 576 u8 defer_accept;
571 struct request_sock **reqp, *req;
572 int i, budget;
573 577
574 if (lopt == NULL || lopt->qlen == 0) 578 if (sk_listener->sk_state != TCP_LISTEN || !lopt) {
579 reqsk_put(req);
575 return; 580 return;
581 }
576 582
583 max_retries = icsk->icsk_syn_retries ? : sysctl_tcp_synack_retries;
584 thresh = max_retries;
577 /* Normally all the openreqs are young and become mature 585 /* Normally all the openreqs are young and become mature
578 * (i.e. converted to established socket) for first timeout. 586 * (i.e. converted to established socket) for first timeout.
579 * If synack was not acknowledged for 1 second, it means 587 * If synack was not acknowledged for 1 second, it means
@@ -591,67 +599,65 @@ void inet_csk_reqsk_queue_prune(struct sock *parent,
591 * embrions; and abort old ones without pity, if old 599 * embrions; and abort old ones without pity, if old
592 * ones are about to clog our table. 600 * ones are about to clog our table.
593 */ 601 */
594 if (lopt->qlen>>(lopt->max_qlen_log-1)) { 602 qlen = listen_sock_qlen(lopt);
595 int young = (lopt->qlen_young<<1); 603 if (qlen >> (lopt->max_qlen_log - 1)) {
604 int young = listen_sock_young(lopt) << 1;
596 605
597 while (thresh > 2) { 606 while (thresh > 2) {
598 if (lopt->qlen < young) 607 if (qlen < young)
599 break; 608 break;
600 thresh--; 609 thresh--;
601 young <<= 1; 610 young <<= 1;
602 } 611 }
603 } 612 }
613 defer_accept = READ_ONCE(queue->rskq_defer_accept);
614 if (defer_accept)
615 max_retries = defer_accept;
616 syn_ack_recalc(req, thresh, max_retries, defer_accept,
617 &expire, &resend);
618 req->rsk_ops->syn_ack_timeout(req);
619 if (!expire &&
620 (!resend ||
621 !inet_rtx_syn_ack(sk_listener, req) ||
622 inet_rsk(req)->acked)) {
623 unsigned long timeo;
624
625 if (req->num_timeout++ == 0)
626 atomic_inc(&lopt->young_dec);
627 timeo = min(TCP_TIMEOUT_INIT << req->num_timeout, TCP_RTO_MAX);
628 mod_timer_pinned(&req->rsk_timer, jiffies + timeo);
629 return;
630 }
631 inet_csk_reqsk_queue_drop(sk_listener, req);
632 reqsk_put(req);
633}
604 634
605 if (queue->rskq_defer_accept) 635void reqsk_queue_hash_req(struct request_sock_queue *queue,
606 max_retries = queue->rskq_defer_accept; 636 u32 hash, struct request_sock *req,
607 637 unsigned long timeout)
608 budget = 2 * (lopt->nr_table_entries / (timeout / interval)); 638{
609 i = lopt->clock_hand; 639 struct listen_sock *lopt = queue->listen_opt;
610
611 do {
612 reqp=&lopt->syn_table[i];
613 while ((req = *reqp) != NULL) {
614 if (time_after_eq(now, req->expires)) {
615 int expire = 0, resend = 0;
616
617 syn_ack_recalc(req, thresh, max_retries,
618 queue->rskq_defer_accept,
619 &expire, &resend);
620 req->rsk_ops->syn_ack_timeout(parent, req);
621 if (!expire &&
622 (!resend ||
623 !inet_rtx_syn_ack(parent, req) ||
624 inet_rsk(req)->acked)) {
625 unsigned long timeo;
626
627 if (req->num_timeout++ == 0)
628 lopt->qlen_young--;
629 timeo = min(timeout << req->num_timeout,
630 max_rto);
631 req->expires = now + timeo;
632 reqp = &req->dl_next;
633 continue;
634 }
635
636 /* Drop this request */
637 inet_csk_reqsk_queue_unlink(parent, req, reqp);
638 reqsk_queue_removed(queue, req);
639 reqsk_free(req);
640 continue;
641 }
642 reqp = &req->dl_next;
643 }
644 640
645 i = (i + 1) & (lopt->nr_table_entries - 1); 641 req->num_retrans = 0;
642 req->num_timeout = 0;
643 req->sk = NULL;
646 644
647 } while (--budget > 0); 645 /* before letting lookups find us, make sure all req fields
646 * are committed to memory and refcnt initialized.
647 */
648 smp_wmb();
649 atomic_set(&req->rsk_refcnt, 2);
650 setup_timer(&req->rsk_timer, reqsk_timer_handler, (unsigned long)req);
651 req->rsk_hash = hash;
648 652
649 lopt->clock_hand = i; 653 spin_lock(&queue->syn_wait_lock);
654 req->dl_next = lopt->syn_table[hash];
655 lopt->syn_table[hash] = req;
656 spin_unlock(&queue->syn_wait_lock);
650 657
651 if (lopt->qlen) 658 mod_timer_pinned(&req->rsk_timer, jiffies + timeout);
652 inet_csk_reset_keepalive_timer(parent, interval);
653} 659}
654EXPORT_SYMBOL_GPL(inet_csk_reqsk_queue_prune); 660EXPORT_SYMBOL(reqsk_queue_hash_req);
655 661
656/** 662/**
657 * inet_csk_clone_lock - clone an inet socket, and lock its clone 663 * inet_csk_clone_lock - clone an inet socket, and lock its clone
@@ -667,7 +673,7 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
667{ 673{
668 struct sock *newsk = sk_clone_lock(sk, priority); 674 struct sock *newsk = sk_clone_lock(sk, priority);
669 675
670 if (newsk != NULL) { 676 if (newsk) {
671 struct inet_connection_sock *newicsk = inet_csk(newsk); 677 struct inet_connection_sock *newicsk = inet_csk(newsk);
672 678
673 newsk->sk_state = TCP_SYN_RECV; 679 newsk->sk_state = TCP_SYN_RECV;
@@ -679,6 +685,8 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
679 newsk->sk_write_space = sk_stream_write_space; 685 newsk->sk_write_space = sk_stream_write_space;
680 686
681 newsk->sk_mark = inet_rsk(req)->ir_mark; 687 newsk->sk_mark = inet_rsk(req)->ir_mark;
688 atomic64_set(&newsk->sk_cookie,
689 atomic64_read(&inet_rsk(req)->ir_cookie));
682 690
683 newicsk->icsk_retransmits = 0; 691 newicsk->icsk_retransmits = 0;
684 newicsk->icsk_backoff = 0; 692 newicsk->icsk_backoff = 0;
@@ -785,8 +793,6 @@ void inet_csk_listen_stop(struct sock *sk)
785 struct request_sock *acc_req; 793 struct request_sock *acc_req;
786 struct request_sock *req; 794 struct request_sock *req;
787 795
788 inet_csk_delete_keepalive_timer(sk);
789
790 /* make all the listen_opt local to us */ 796 /* make all the listen_opt local to us */
791 acc_req = reqsk_queue_yank_acceptq(queue); 797 acc_req = reqsk_queue_yank_acceptq(queue);
792 798
@@ -816,9 +822,9 @@ void inet_csk_listen_stop(struct sock *sk)
816 822
817 percpu_counter_inc(sk->sk_prot->orphan_count); 823 percpu_counter_inc(sk->sk_prot->orphan_count);
818 824
819 if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->listener) { 825 if (sk->sk_protocol == IPPROTO_TCP && tcp_rsk(req)->tfo_listener) {
820 BUG_ON(tcp_sk(child)->fastopen_rsk != req); 826 BUG_ON(tcp_sk(child)->fastopen_rsk != req);
821 BUG_ON(sk != tcp_rsk(req)->listener); 827 BUG_ON(sk != req->rsk_listener);
822 828
823 /* Paranoid, to prevent race condition if 829 /* Paranoid, to prevent race condition if
824 * an inbound pkt destined for child is 830 * an inbound pkt destined for child is
@@ -827,7 +833,6 @@ void inet_csk_listen_stop(struct sock *sk)
827 * tcp_v4_destroy_sock(). 833 * tcp_v4_destroy_sock().
828 */ 834 */
829 tcp_sk(child)->fastopen_rsk = NULL; 835 tcp_sk(child)->fastopen_rsk = NULL;
830 sock_put(sk);
831 } 836 }
832 inet_csk_destroy_sock(child); 837 inet_csk_destroy_sock(child);
833 838
@@ -836,9 +841,9 @@ void inet_csk_listen_stop(struct sock *sk)
836 sock_put(child); 841 sock_put(child);
837 842
838 sk_acceptq_removed(sk); 843 sk_acceptq_removed(sk);
839 __reqsk_free(req); 844 reqsk_put(req);
840 } 845 }
841 if (queue->fastopenq != NULL) { 846 if (queue->fastopenq) {
842 /* Free all the reqs queued in rskq_rst_head. */ 847 /* Free all the reqs queued in rskq_rst_head. */
843 spin_lock_bh(&queue->fastopenq->lock); 848 spin_lock_bh(&queue->fastopenq->lock);
844 acc_req = queue->fastopenq->rskq_rst_head; 849 acc_req = queue->fastopenq->rskq_rst_head;
@@ -846,7 +851,7 @@ void inet_csk_listen_stop(struct sock *sk)
846 spin_unlock_bh(&queue->fastopenq->lock); 851 spin_unlock_bh(&queue->fastopenq->lock);
847 while ((req = acc_req) != NULL) { 852 while ((req = acc_req) != NULL) {
848 acc_req = req->dl_next; 853 acc_req = req->dl_next;
849 __reqsk_free(req); 854 reqsk_put(req);
850 } 855 }
851 } 856 }
852 WARN_ON(sk->sk_ack_backlog); 857 WARN_ON(sk->sk_ack_backlog);
@@ -870,7 +875,7 @@ int inet_csk_compat_getsockopt(struct sock *sk, int level, int optname,
870{ 875{
871 const struct inet_connection_sock *icsk = inet_csk(sk); 876 const struct inet_connection_sock *icsk = inet_csk(sk);
872 877
873 if (icsk->icsk_af_ops->compat_getsockopt != NULL) 878 if (icsk->icsk_af_ops->compat_getsockopt)
874 return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname, 879 return icsk->icsk_af_ops->compat_getsockopt(sk, level, optname,
875 optval, optlen); 880 optval, optlen);
876 return icsk->icsk_af_ops->getsockopt(sk, level, optname, 881 return icsk->icsk_af_ops->getsockopt(sk, level, optname,
@@ -883,7 +888,7 @@ int inet_csk_compat_setsockopt(struct sock *sk, int level, int optname,
883{ 888{
884 const struct inet_connection_sock *icsk = inet_csk(sk); 889 const struct inet_connection_sock *icsk = inet_csk(sk);
885 890
886 if (icsk->icsk_af_ops->compat_setsockopt != NULL) 891 if (icsk->icsk_af_ops->compat_setsockopt)
887 return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname, 892 return icsk->icsk_af_ops->compat_setsockopt(sk, level, optname,
888 optval, optlen); 893 optval, optlen);
889 return icsk->icsk_af_ops->setsockopt(sk, level, optname, 894 return icsk->icsk_af_ops->setsockopt(sk, level, optname,
diff --git a/net/ipv4/inet_diag.c b/net/ipv4/inet_diag.c
index 592aff37366b..bb77ebdae3b3 100644
--- a/net/ipv4/inet_diag.c
+++ b/net/ipv4/inet_diag.c
@@ -38,16 +38,12 @@
38static const struct inet_diag_handler **inet_diag_table; 38static const struct inet_diag_handler **inet_diag_table;
39 39
40struct inet_diag_entry { 40struct inet_diag_entry {
41 __be32 *saddr; 41 const __be32 *saddr;
42 __be32 *daddr; 42 const __be32 *daddr;
43 u16 sport; 43 u16 sport;
44 u16 dport; 44 u16 dport;
45 u16 family; 45 u16 family;
46 u16 userlocks; 46 u16 userlocks;
47#if IS_ENABLED(CONFIG_IPV6)
48 struct in6_addr saddr_storage; /* for IPv4-mapped-IPv6 addresses */
49 struct in6_addr daddr_storage; /* for IPv4-mapped-IPv6 addresses */
50#endif
51}; 47};
52 48
53static DEFINE_MUTEX(inet_diag_table_mutex); 49static DEFINE_MUTEX(inet_diag_table_mutex);
@@ -65,12 +61,35 @@ static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
65 return inet_diag_table[proto]; 61 return inet_diag_table[proto];
66} 62}
67 63
68static inline void inet_diag_unlock_handler( 64static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
69 const struct inet_diag_handler *handler)
70{ 65{
71 mutex_unlock(&inet_diag_table_mutex); 66 mutex_unlock(&inet_diag_table_mutex);
72} 67}
73 68
69static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
70{
71 r->idiag_family = sk->sk_family;
72
73 r->id.idiag_sport = htons(sk->sk_num);
74 r->id.idiag_dport = sk->sk_dport;
75 r->id.idiag_if = sk->sk_bound_dev_if;
76 sock_diag_save_cookie(sk, r->id.idiag_cookie);
77
78#if IS_ENABLED(CONFIG_IPV6)
79 if (sk->sk_family == AF_INET6) {
80 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
81 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
82 } else
83#endif
84 {
85 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
86 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
87
88 r->id.idiag_src[0] = sk->sk_rcv_saddr;
89 r->id.idiag_dst[0] = sk->sk_daddr;
90 }
91}
92
74static size_t inet_sk_attr_size(void) 93static size_t inet_sk_attr_size(void)
75{ 94{
76 return nla_total_size(sizeof(struct tcp_info)) 95 return nla_total_size(sizeof(struct tcp_info))
@@ -86,21 +105,22 @@ static size_t inet_sk_attr_size(void)
86} 105}
87 106
88int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk, 107int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
89 struct sk_buff *skb, struct inet_diag_req_v2 *req, 108 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
90 struct user_namespace *user_ns, 109 struct user_namespace *user_ns,
91 u32 portid, u32 seq, u16 nlmsg_flags, 110 u32 portid, u32 seq, u16 nlmsg_flags,
92 const struct nlmsghdr *unlh) 111 const struct nlmsghdr *unlh)
93{ 112{
94 const struct inet_sock *inet = inet_sk(sk); 113 const struct inet_sock *inet = inet_sk(sk);
114 const struct tcp_congestion_ops *ca_ops;
115 const struct inet_diag_handler *handler;
116 int ext = req->idiag_ext;
95 struct inet_diag_msg *r; 117 struct inet_diag_msg *r;
96 struct nlmsghdr *nlh; 118 struct nlmsghdr *nlh;
97 struct nlattr *attr; 119 struct nlattr *attr;
98 void *info = NULL; 120 void *info = NULL;
99 const struct inet_diag_handler *handler;
100 int ext = req->idiag_ext;
101 121
102 handler = inet_diag_table[req->sdiag_protocol]; 122 handler = inet_diag_table[req->sdiag_protocol];
103 BUG_ON(handler == NULL); 123 BUG_ON(!handler);
104 124
105 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 125 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
106 nlmsg_flags); 126 nlmsg_flags);
@@ -108,25 +128,13 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
108 return -EMSGSIZE; 128 return -EMSGSIZE;
109 129
110 r = nlmsg_data(nlh); 130 r = nlmsg_data(nlh);
111 BUG_ON(sk->sk_state == TCP_TIME_WAIT); 131 BUG_ON(!sk_fullsock(sk));
112 132
113 r->idiag_family = sk->sk_family; 133 inet_diag_msg_common_fill(r, sk);
114 r->idiag_state = sk->sk_state; 134 r->idiag_state = sk->sk_state;
115 r->idiag_timer = 0; 135 r->idiag_timer = 0;
116 r->idiag_retrans = 0; 136 r->idiag_retrans = 0;
117 137
118 r->id.idiag_if = sk->sk_bound_dev_if;
119 sock_diag_save_cookie(sk, r->id.idiag_cookie);
120
121 r->id.idiag_sport = inet->inet_sport;
122 r->id.idiag_dport = inet->inet_dport;
123
124 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
125 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
126
127 r->id.idiag_src[0] = inet->inet_rcv_saddr;
128 r->id.idiag_dst[0] = inet->inet_daddr;
129
130 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown)) 138 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
131 goto errout; 139 goto errout;
132 140
@@ -139,10 +147,6 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
139 147
140#if IS_ENABLED(CONFIG_IPV6) 148#if IS_ENABLED(CONFIG_IPV6)
141 if (r->idiag_family == AF_INET6) { 149 if (r->idiag_family == AF_INET6) {
142
143 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
144 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
145
146 if (ext & (1 << (INET_DIAG_TCLASS - 1))) 150 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
147 if (nla_put_u8(skb, INET_DIAG_TCLASS, 151 if (nla_put_u8(skb, INET_DIAG_TCLASS,
148 inet6_sk(sk)->tclass) < 0) 152 inet6_sk(sk)->tclass) < 0)
@@ -169,7 +173,7 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
169 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO)) 173 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
170 goto errout; 174 goto errout;
171 175
172 if (icsk == NULL) { 176 if (!icsk) {
173 handler->idiag_get_info(sk, r, NULL); 177 handler->idiag_get_info(sk, r, NULL);
174 goto out; 178 goto out;
175 } 179 }
@@ -205,16 +209,31 @@ int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
205 info = nla_data(attr); 209 info = nla_data(attr);
206 } 210 }
207 211
208 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) 212 if (ext & (1 << (INET_DIAG_CONG - 1))) {
209 if (nla_put_string(skb, INET_DIAG_CONG, 213 int err = 0;
210 icsk->icsk_ca_ops->name) < 0) 214
215 rcu_read_lock();
216 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
217 if (ca_ops)
218 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
219 rcu_read_unlock();
220 if (err < 0)
211 goto errout; 221 goto errout;
222 }
212 223
213 handler->idiag_get_info(sk, r, info); 224 handler->idiag_get_info(sk, r, info);
214 225
215 if (sk->sk_state < TCP_TIME_WAIT && 226 if (sk->sk_state < TCP_TIME_WAIT) {
216 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info) 227 int err = 0;
217 icsk->icsk_ca_ops->get_info(sk, ext, skb); 228
229 rcu_read_lock();
230 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
231 if (ca_ops && ca_ops->get_info)
232 err = ca_ops->get_info(sk, ext, skb);
233 rcu_read_unlock();
234 if (err < 0)
235 goto errout;
236 }
218 237
219out: 238out:
220 nlmsg_end(skb, nlh); 239 nlmsg_end(skb, nlh);
@@ -227,23 +246,25 @@ errout:
227EXPORT_SYMBOL_GPL(inet_sk_diag_fill); 246EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
228 247
229static int inet_csk_diag_fill(struct sock *sk, 248static int inet_csk_diag_fill(struct sock *sk,
230 struct sk_buff *skb, struct inet_diag_req_v2 *req, 249 struct sk_buff *skb,
250 const struct inet_diag_req_v2 *req,
231 struct user_namespace *user_ns, 251 struct user_namespace *user_ns,
232 u32 portid, u32 seq, u16 nlmsg_flags, 252 u32 portid, u32 seq, u16 nlmsg_flags,
233 const struct nlmsghdr *unlh) 253 const struct nlmsghdr *unlh)
234{ 254{
235 return inet_sk_diag_fill(sk, inet_csk(sk), 255 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
236 skb, req, user_ns, portid, seq, nlmsg_flags, unlh); 256 user_ns, portid, seq, nlmsg_flags, unlh);
237} 257}
238 258
239static int inet_twsk_diag_fill(struct inet_timewait_sock *tw, 259static int inet_twsk_diag_fill(struct sock *sk,
240 struct sk_buff *skb, struct inet_diag_req_v2 *req, 260 struct sk_buff *skb,
241 u32 portid, u32 seq, u16 nlmsg_flags, 261 u32 portid, u32 seq, u16 nlmsg_flags,
242 const struct nlmsghdr *unlh) 262 const struct nlmsghdr *unlh)
243{ 263{
244 s32 tmo; 264 struct inet_timewait_sock *tw = inet_twsk(sk);
245 struct inet_diag_msg *r; 265 struct inet_diag_msg *r;
246 struct nlmsghdr *nlh; 266 struct nlmsghdr *nlh;
267 long tmo;
247 268
248 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r), 269 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
249 nlmsg_flags); 270 nlmsg_flags);
@@ -253,25 +274,13 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
253 r = nlmsg_data(nlh); 274 r = nlmsg_data(nlh);
254 BUG_ON(tw->tw_state != TCP_TIME_WAIT); 275 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
255 276
256 tmo = tw->tw_ttd - inet_tw_time_stamp(); 277 tmo = tw->tw_timer.expires - jiffies;
257 if (tmo < 0) 278 if (tmo < 0)
258 tmo = 0; 279 tmo = 0;
259 280
260 r->idiag_family = tw->tw_family; 281 inet_diag_msg_common_fill(r, sk);
261 r->idiag_retrans = 0; 282 r->idiag_retrans = 0;
262 283
263 r->id.idiag_if = tw->tw_bound_dev_if;
264 sock_diag_save_cookie(tw, r->id.idiag_cookie);
265
266 r->id.idiag_sport = tw->tw_sport;
267 r->id.idiag_dport = tw->tw_dport;
268
269 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
270 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
271
272 r->id.idiag_src[0] = tw->tw_rcv_saddr;
273 r->id.idiag_dst[0] = tw->tw_daddr;
274
275 r->idiag_state = tw->tw_substate; 284 r->idiag_state = tw->tw_substate;
276 r->idiag_timer = 3; 285 r->idiag_timer = 3;
277 r->idiag_expires = jiffies_to_msecs(tmo); 286 r->idiag_expires = jiffies_to_msecs(tmo);
@@ -279,61 +288,91 @@ static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
279 r->idiag_wqueue = 0; 288 r->idiag_wqueue = 0;
280 r->idiag_uid = 0; 289 r->idiag_uid = 0;
281 r->idiag_inode = 0; 290 r->idiag_inode = 0;
282#if IS_ENABLED(CONFIG_IPV6) 291
283 if (tw->tw_family == AF_INET6) { 292 nlmsg_end(skb, nlh);
284 *(struct in6_addr *)r->id.idiag_src = tw->tw_v6_rcv_saddr; 293 return 0;
285 *(struct in6_addr *)r->id.idiag_dst = tw->tw_v6_daddr; 294}
286 } 295
287#endif 296static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
297 u32 portid, u32 seq, u16 nlmsg_flags,
298 const struct nlmsghdr *unlh)
299{
300 struct inet_diag_msg *r;
301 struct nlmsghdr *nlh;
302 long tmo;
303
304 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
305 nlmsg_flags);
306 if (!nlh)
307 return -EMSGSIZE;
308
309 r = nlmsg_data(nlh);
310 inet_diag_msg_common_fill(r, sk);
311 r->idiag_state = TCP_SYN_RECV;
312 r->idiag_timer = 1;
313 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
314
315 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
316 offsetof(struct sock, sk_cookie));
317
318 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
319 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
320 r->idiag_rqueue = 0;
321 r->idiag_wqueue = 0;
322 r->idiag_uid = 0;
323 r->idiag_inode = 0;
288 324
289 nlmsg_end(skb, nlh); 325 nlmsg_end(skb, nlh);
290 return 0; 326 return 0;
291} 327}
292 328
293static int sk_diag_fill(struct sock *sk, struct sk_buff *skb, 329static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
294 struct inet_diag_req_v2 *r, 330 const struct inet_diag_req_v2 *r,
295 struct user_namespace *user_ns, 331 struct user_namespace *user_ns,
296 u32 portid, u32 seq, u16 nlmsg_flags, 332 u32 portid, u32 seq, u16 nlmsg_flags,
297 const struct nlmsghdr *unlh) 333 const struct nlmsghdr *unlh)
298{ 334{
299 if (sk->sk_state == TCP_TIME_WAIT) 335 if (sk->sk_state == TCP_TIME_WAIT)
300 return inet_twsk_diag_fill(inet_twsk(sk), skb, r, portid, seq, 336 return inet_twsk_diag_fill(sk, skb, portid, seq,
301 nlmsg_flags, unlh); 337 nlmsg_flags, unlh);
302 338
339 if (sk->sk_state == TCP_NEW_SYN_RECV)
340 return inet_req_diag_fill(sk, skb, portid, seq,
341 nlmsg_flags, unlh);
342
303 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq, 343 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
304 nlmsg_flags, unlh); 344 nlmsg_flags, unlh);
305} 345}
306 346
307int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, 347int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
308 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req) 348 struct sk_buff *in_skb,
349 const struct nlmsghdr *nlh,
350 const struct inet_diag_req_v2 *req)
309{ 351{
310 int err;
311 struct sock *sk;
312 struct sk_buff *rep;
313 struct net *net = sock_net(in_skb->sk); 352 struct net *net = sock_net(in_skb->sk);
353 struct sk_buff *rep;
354 struct sock *sk;
355 int err;
314 356
315 err = -EINVAL; 357 err = -EINVAL;
316 if (req->sdiag_family == AF_INET) { 358 if (req->sdiag_family == AF_INET)
317 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0], 359 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
318 req->id.idiag_dport, req->id.idiag_src[0], 360 req->id.idiag_dport, req->id.idiag_src[0],
319 req->id.idiag_sport, req->id.idiag_if); 361 req->id.idiag_sport, req->id.idiag_if);
320 }
321#if IS_ENABLED(CONFIG_IPV6) 362#if IS_ENABLED(CONFIG_IPV6)
322 else if (req->sdiag_family == AF_INET6) { 363 else if (req->sdiag_family == AF_INET6)
323 sk = inet6_lookup(net, hashinfo, 364 sk = inet6_lookup(net, hashinfo,
324 (struct in6_addr *)req->id.idiag_dst, 365 (struct in6_addr *)req->id.idiag_dst,
325 req->id.idiag_dport, 366 req->id.idiag_dport,
326 (struct in6_addr *)req->id.idiag_src, 367 (struct in6_addr *)req->id.idiag_src,
327 req->id.idiag_sport, 368 req->id.idiag_sport,
328 req->id.idiag_if); 369 req->id.idiag_if);
329 }
330#endif 370#endif
331 else { 371 else
332 goto out_nosk; 372 goto out_nosk;
333 }
334 373
335 err = -ENOENT; 374 err = -ENOENT;
336 if (sk == NULL) 375 if (!sk)
337 goto out_nosk; 376 goto out_nosk;
338 377
339 err = sock_diag_check_cookie(sk, req->id.idiag_cookie); 378 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
@@ -371,7 +410,7 @@ EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
371 410
372static int inet_diag_get_exact(struct sk_buff *in_skb, 411static int inet_diag_get_exact(struct sk_buff *in_skb,
373 const struct nlmsghdr *nlh, 412 const struct nlmsghdr *nlh,
374 struct inet_diag_req_v2 *req) 413 const struct inet_diag_req_v2 *req)
375{ 414{
376 const struct inet_diag_handler *handler; 415 const struct inet_diag_handler *handler;
377 int err; 416 int err;
@@ -412,9 +451,8 @@ static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
412 return 1; 451 return 1;
413} 452}
414 453
415
416static int inet_diag_bc_run(const struct nlattr *_bc, 454static int inet_diag_bc_run(const struct nlattr *_bc,
417 const struct inet_diag_entry *entry) 455 const struct inet_diag_entry *entry)
418{ 456{
419 const void *bc = nla_data(_bc); 457 const void *bc = nla_data(_bc);
420 int len = nla_len(_bc); 458 int len = nla_len(_bc);
@@ -446,10 +484,10 @@ static int inet_diag_bc_run(const struct nlattr *_bc,
446 break; 484 break;
447 case INET_DIAG_BC_S_COND: 485 case INET_DIAG_BC_S_COND:
448 case INET_DIAG_BC_D_COND: { 486 case INET_DIAG_BC_D_COND: {
449 struct inet_diag_hostcond *cond; 487 const struct inet_diag_hostcond *cond;
450 __be32 *addr; 488 const __be32 *addr;
451 489
452 cond = (struct inet_diag_hostcond *)(op + 1); 490 cond = (const struct inet_diag_hostcond *)(op + 1);
453 if (cond->port != -1 && 491 if (cond->port != -1 &&
454 cond->port != (op->code == INET_DIAG_BC_S_COND ? 492 cond->port != (op->code == INET_DIAG_BC_S_COND ?
455 entry->sport : entry->dport)) { 493 entry->sport : entry->dport)) {
@@ -498,29 +536,36 @@ static int inet_diag_bc_run(const struct nlattr *_bc,
498 return len == 0; 536 return len == 0;
499} 537}
500 538
539/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
540 */
541static void entry_fill_addrs(struct inet_diag_entry *entry,
542 const struct sock *sk)
543{
544#if IS_ENABLED(CONFIG_IPV6)
545 if (sk->sk_family == AF_INET6) {
546 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
547 entry->daddr = sk->sk_v6_daddr.s6_addr32;
548 } else
549#endif
550 {
551 entry->saddr = &sk->sk_rcv_saddr;
552 entry->daddr = &sk->sk_daddr;
553 }
554}
555
501int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk) 556int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
502{ 557{
503 struct inet_diag_entry entry;
504 struct inet_sock *inet = inet_sk(sk); 558 struct inet_sock *inet = inet_sk(sk);
559 struct inet_diag_entry entry;
505 560
506 if (bc == NULL) 561 if (!bc)
507 return 1; 562 return 1;
508 563
509 entry.family = sk->sk_family; 564 entry.family = sk->sk_family;
510#if IS_ENABLED(CONFIG_IPV6) 565 entry_fill_addrs(&entry, sk);
511 if (entry.family == AF_INET6) {
512
513 entry.saddr = sk->sk_v6_rcv_saddr.s6_addr32;
514 entry.daddr = sk->sk_v6_daddr.s6_addr32;
515 } else
516#endif
517 {
518 entry.saddr = &inet->inet_rcv_saddr;
519 entry.daddr = &inet->inet_daddr;
520 }
521 entry.sport = inet->inet_num; 566 entry.sport = inet->inet_num;
522 entry.dport = ntohs(inet->inet_dport); 567 entry.dport = ntohs(inet->inet_dport);
523 entry.userlocks = sk->sk_userlocks; 568 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
524 569
525 return inet_diag_bc_run(bc, &entry); 570 return inet_diag_bc_run(bc, &entry);
526} 571}
@@ -547,8 +592,8 @@ static int valid_cc(const void *bc, int len, int cc)
547static bool valid_hostcond(const struct inet_diag_bc_op *op, int len, 592static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
548 int *min_len) 593 int *min_len)
549{ 594{
550 int addr_len;
551 struct inet_diag_hostcond *cond; 595 struct inet_diag_hostcond *cond;
596 int addr_len;
552 597
553 /* Check hostcond space. */ 598 /* Check hostcond space. */
554 *min_len += sizeof(struct inet_diag_hostcond); 599 *min_len += sizeof(struct inet_diag_hostcond);
@@ -582,8 +627,8 @@ static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
582} 627}
583 628
584/* Validate a port comparison operator. */ 629/* Validate a port comparison operator. */
585static inline bool valid_port_comparison(const struct inet_diag_bc_op *op, 630static bool valid_port_comparison(const struct inet_diag_bc_op *op,
586 int len, int *min_len) 631 int len, int *min_len)
587{ 632{
588 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */ 633 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
589 *min_len += sizeof(struct inet_diag_bc_op); 634 *min_len += sizeof(struct inet_diag_bc_op);
@@ -598,10 +643,9 @@ static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
598 int len = bytecode_len; 643 int len = bytecode_len;
599 644
600 while (len > 0) { 645 while (len > 0) {
601 const struct inet_diag_bc_op *op = bc;
602 int min_len = sizeof(struct inet_diag_bc_op); 646 int min_len = sizeof(struct inet_diag_bc_op);
647 const struct inet_diag_bc_op *op = bc;
603 648
604//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
605 switch (op->code) { 649 switch (op->code) {
606 case INET_DIAG_BC_S_COND: 650 case INET_DIAG_BC_S_COND:
607 case INET_DIAG_BC_D_COND: 651 case INET_DIAG_BC_D_COND:
@@ -642,7 +686,7 @@ static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
642static int inet_csk_diag_dump(struct sock *sk, 686static int inet_csk_diag_dump(struct sock *sk,
643 struct sk_buff *skb, 687 struct sk_buff *skb,
644 struct netlink_callback *cb, 688 struct netlink_callback *cb,
645 struct inet_diag_req_v2 *r, 689 const struct inet_diag_req_v2 *r,
646 const struct nlattr *bc) 690 const struct nlattr *bc)
647{ 691{
648 if (!inet_diag_bc_sk(bc, sk)) 692 if (!inet_diag_bc_sk(bc, sk))
@@ -654,139 +698,42 @@ static int inet_csk_diag_dump(struct sock *sk,
654 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh); 698 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
655} 699}
656 700
657static int inet_twsk_diag_dump(struct sock *sk, 701static void twsk_build_assert(void)
658 struct sk_buff *skb,
659 struct netlink_callback *cb,
660 struct inet_diag_req_v2 *r,
661 const struct nlattr *bc)
662{ 702{
663 struct inet_timewait_sock *tw = inet_twsk(sk); 703 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
704 offsetof(struct sock, sk_family));
664 705
665 if (bc != NULL) { 706 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
666 struct inet_diag_entry entry; 707 offsetof(struct inet_sock, inet_num));
667 708
668 entry.family = tw->tw_family; 709 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
669#if IS_ENABLED(CONFIG_IPV6) 710 offsetof(struct inet_sock, inet_dport));
670 if (tw->tw_family == AF_INET6) {
671 entry.saddr = tw->tw_v6_rcv_saddr.s6_addr32;
672 entry.daddr = tw->tw_v6_daddr.s6_addr32;
673 } else
674#endif
675 {
676 entry.saddr = &tw->tw_rcv_saddr;
677 entry.daddr = &tw->tw_daddr;
678 }
679 entry.sport = tw->tw_num;
680 entry.dport = ntohs(tw->tw_dport);
681 entry.userlocks = 0;
682
683 if (!inet_diag_bc_run(bc, &entry))
684 return 0;
685 }
686 711
687 return inet_twsk_diag_fill(tw, skb, r, 712 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
688 NETLINK_CB(cb->skb).portid, 713 offsetof(struct inet_sock, inet_rcv_saddr));
689 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
690}
691 714
692/* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses 715 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
693 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6. 716 offsetof(struct inet_sock, inet_daddr));
694 */
695static inline void inet_diag_req_addrs(const struct sock *sk,
696 const struct request_sock *req,
697 struct inet_diag_entry *entry)
698{
699 struct inet_request_sock *ireq = inet_rsk(req);
700 717
701#if IS_ENABLED(CONFIG_IPV6) 718#if IS_ENABLED(CONFIG_IPV6)
702 if (sk->sk_family == AF_INET6) { 719 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
703 if (req->rsk_ops->family == AF_INET6) { 720 offsetof(struct sock, sk_v6_rcv_saddr));
704 entry->saddr = ireq->ir_v6_loc_addr.s6_addr32;
705 entry->daddr = ireq->ir_v6_rmt_addr.s6_addr32;
706 } else if (req->rsk_ops->family == AF_INET) {
707 ipv6_addr_set_v4mapped(ireq->ir_loc_addr,
708 &entry->saddr_storage);
709 ipv6_addr_set_v4mapped(ireq->ir_rmt_addr,
710 &entry->daddr_storage);
711 entry->saddr = entry->saddr_storage.s6_addr32;
712 entry->daddr = entry->daddr_storage.s6_addr32;
713 }
714 } else
715#endif
716 {
717 entry->saddr = &ireq->ir_loc_addr;
718 entry->daddr = &ireq->ir_rmt_addr;
719 }
720}
721
722static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
723 struct request_sock *req,
724 struct user_namespace *user_ns,
725 u32 portid, u32 seq,
726 const struct nlmsghdr *unlh)
727{
728 const struct inet_request_sock *ireq = inet_rsk(req);
729 struct inet_sock *inet = inet_sk(sk);
730 struct inet_diag_msg *r;
731 struct nlmsghdr *nlh;
732 long tmo;
733
734 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
735 NLM_F_MULTI);
736 if (!nlh)
737 return -EMSGSIZE;
738
739 r = nlmsg_data(nlh);
740 r->idiag_family = sk->sk_family;
741 r->idiag_state = TCP_SYN_RECV;
742 r->idiag_timer = 1;
743 r->idiag_retrans = req->num_retrans;
744
745 r->id.idiag_if = sk->sk_bound_dev_if;
746 sock_diag_save_cookie(req, r->id.idiag_cookie);
747
748 tmo = req->expires - jiffies;
749 if (tmo < 0)
750 tmo = 0;
751
752 r->id.idiag_sport = inet->inet_sport;
753 r->id.idiag_dport = ireq->ir_rmt_port;
754
755 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
756 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
757
758 r->id.idiag_src[0] = ireq->ir_loc_addr;
759 r->id.idiag_dst[0] = ireq->ir_rmt_addr;
760 721
761 r->idiag_expires = jiffies_to_msecs(tmo); 722 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
762 r->idiag_rqueue = 0; 723 offsetof(struct sock, sk_v6_daddr));
763 r->idiag_wqueue = 0;
764 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
765 r->idiag_inode = 0;
766#if IS_ENABLED(CONFIG_IPV6)
767 if (r->idiag_family == AF_INET6) {
768 struct inet_diag_entry entry;
769 inet_diag_req_addrs(sk, req, &entry);
770 memcpy(r->id.idiag_src, entry.saddr, sizeof(struct in6_addr));
771 memcpy(r->id.idiag_dst, entry.daddr, sizeof(struct in6_addr));
772 }
773#endif 724#endif
774
775 nlmsg_end(skb, nlh);
776 return 0;
777} 725}
778 726
779static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk, 727static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
780 struct netlink_callback *cb, 728 struct netlink_callback *cb,
781 struct inet_diag_req_v2 *r, 729 const struct inet_diag_req_v2 *r,
782 const struct nlattr *bc) 730 const struct nlattr *bc)
783{ 731{
784 struct inet_diag_entry entry;
785 struct inet_connection_sock *icsk = inet_csk(sk); 732 struct inet_connection_sock *icsk = inet_csk(sk);
786 struct listen_sock *lopt;
787 struct inet_sock *inet = inet_sk(sk); 733 struct inet_sock *inet = inet_sk(sk);
788 int j, s_j; 734 struct inet_diag_entry entry;
789 int reqnum, s_reqnum; 735 int j, s_j, reqnum, s_reqnum;
736 struct listen_sock *lopt;
790 int err = 0; 737 int err = 0;
791 738
792 s_j = cb->args[3]; 739 s_j = cb->args[3];
@@ -797,13 +744,13 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
797 744
798 entry.family = sk->sk_family; 745 entry.family = sk->sk_family;
799 746
800 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 747 spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
801 748
802 lopt = icsk->icsk_accept_queue.listen_opt; 749 lopt = icsk->icsk_accept_queue.listen_opt;
803 if (!lopt || !lopt->qlen) 750 if (!lopt || !listen_sock_qlen(lopt))
804 goto out; 751 goto out;
805 752
806 if (bc != NULL) { 753 if (bc) {
807 entry.sport = inet->inet_num; 754 entry.sport = inet->inet_num;
808 entry.userlocks = sk->sk_userlocks; 755 entry.userlocks = sk->sk_userlocks;
809 } 756 }
@@ -822,17 +769,18 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
822 continue; 769 continue;
823 770
824 if (bc) { 771 if (bc) {
825 inet_diag_req_addrs(sk, req, &entry); 772 /* Note: entry.sport and entry.userlocks are already set */
773 entry_fill_addrs(&entry, req_to_sk(req));
826 entry.dport = ntohs(ireq->ir_rmt_port); 774 entry.dport = ntohs(ireq->ir_rmt_port);
827 775
828 if (!inet_diag_bc_run(bc, &entry)) 776 if (!inet_diag_bc_run(bc, &entry))
829 continue; 777 continue;
830 } 778 }
831 779
832 err = inet_diag_fill_req(skb, sk, req, 780 err = inet_req_diag_fill(req_to_sk(req), skb,
833 sk_user_ns(NETLINK_CB(cb->skb).sk), 781 NETLINK_CB(cb->skb).portid,
834 NETLINK_CB(cb->skb).portid, 782 cb->nlh->nlmsg_seq,
835 cb->nlh->nlmsg_seq, cb->nlh); 783 NLM_F_MULTI, cb->nlh);
836 if (err < 0) { 784 if (err < 0) {
837 cb->args[3] = j + 1; 785 cb->args[3] = j + 1;
838 cb->args[4] = reqnum; 786 cb->args[4] = reqnum;
@@ -844,17 +792,17 @@ static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
844 } 792 }
845 793
846out: 794out:
847 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 795 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
848 796
849 return err; 797 return err;
850} 798}
851 799
852void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb, 800void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
853 struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc) 801 struct netlink_callback *cb,
802 const struct inet_diag_req_v2 *r, struct nlattr *bc)
854{ 803{
855 int i, num;
856 int s_i, s_num;
857 struct net *net = sock_net(skb->sk); 804 struct net *net = sock_net(skb->sk);
805 int i, num, s_i, s_num;
858 806
859 s_i = cb->args[1]; 807 s_i = cb->args[1];
860 s_num = num = cb->args[2]; 808 s_num = num = cb->args[2];
@@ -864,9 +812,9 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
864 goto skip_listen_ht; 812 goto skip_listen_ht;
865 813
866 for (i = s_i; i < INET_LHTABLE_SIZE; i++) { 814 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
867 struct sock *sk;
868 struct hlist_nulls_node *node;
869 struct inet_listen_hashbucket *ilb; 815 struct inet_listen_hashbucket *ilb;
816 struct hlist_nulls_node *node;
817 struct sock *sk;
870 818
871 num = 0; 819 num = 0;
872 ilb = &hashinfo->listening_hash[i]; 820 ilb = &hashinfo->listening_hash[i];
@@ -883,7 +831,7 @@ void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
883 } 831 }
884 832
885 if (r->sdiag_family != AF_UNSPEC && 833 if (r->sdiag_family != AF_UNSPEC &&
886 sk->sk_family != r->sdiag_family) 834 sk->sk_family != r->sdiag_family)
887 goto next_listen; 835 goto next_listen;
888 836
889 if (r->id.idiag_sport != inet->inet_sport && 837 if (r->id.idiag_sport != inet->inet_sport &&
@@ -931,8 +879,8 @@ skip_listen_ht:
931 for (i = s_i; i <= hashinfo->ehash_mask; i++) { 879 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
932 struct inet_ehash_bucket *head = &hashinfo->ehash[i]; 880 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
933 spinlock_t *lock = inet_ehash_lockp(hashinfo, i); 881 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
934 struct sock *sk;
935 struct hlist_nulls_node *node; 882 struct hlist_nulls_node *node;
883 struct sock *sk;
936 884
937 num = 0; 885 num = 0;
938 886
@@ -944,8 +892,7 @@ skip_listen_ht:
944 892
945 spin_lock_bh(lock); 893 spin_lock_bh(lock);
946 sk_nulls_for_each(sk, node, &head->chain) { 894 sk_nulls_for_each(sk, node, &head->chain) {
947 int res; 895 int state, res;
948 int state;
949 896
950 if (!net_eq(sock_net(sk), net)) 897 if (!net_eq(sock_net(sk), net))
951 continue; 898 continue;
@@ -964,10 +911,16 @@ skip_listen_ht:
964 if (r->id.idiag_dport != sk->sk_dport && 911 if (r->id.idiag_dport != sk->sk_dport &&
965 r->id.idiag_dport) 912 r->id.idiag_dport)
966 goto next_normal; 913 goto next_normal;
967 if (sk->sk_state == TCP_TIME_WAIT) 914 twsk_build_assert();
968 res = inet_twsk_diag_dump(sk, skb, cb, r, bc); 915
969 else 916 if (!inet_diag_bc_sk(bc, sk))
970 res = inet_csk_diag_dump(sk, skb, cb, r, bc); 917 goto next_normal;
918
919 res = sk_diag_fill(sk, skb, r,
920 sk_user_ns(NETLINK_CB(cb->skb).sk),
921 NETLINK_CB(cb->skb).portid,
922 cb->nlh->nlmsg_seq, NLM_F_MULTI,
923 cb->nlh);
971 if (res < 0) { 924 if (res < 0) {
972 spin_unlock_bh(lock); 925 spin_unlock_bh(lock);
973 goto done; 926 goto done;
@@ -988,7 +941,8 @@ out:
988EXPORT_SYMBOL_GPL(inet_diag_dump_icsk); 941EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
989 942
990static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 943static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
991 struct inet_diag_req_v2 *r, struct nlattr *bc) 944 const struct inet_diag_req_v2 *r,
945 struct nlattr *bc)
992{ 946{
993 const struct inet_diag_handler *handler; 947 const struct inet_diag_handler *handler;
994 int err = 0; 948 int err = 0;
@@ -1005,8 +959,8 @@ static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
1005 959
1006static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb) 960static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1007{ 961{
1008 struct nlattr *bc = NULL;
1009 int hdrlen = sizeof(struct inet_diag_req_v2); 962 int hdrlen = sizeof(struct inet_diag_req_v2);
963 struct nlattr *bc = NULL;
1010 964
1011 if (nlmsg_attrlen(cb->nlh, hdrlen)) 965 if (nlmsg_attrlen(cb->nlh, hdrlen))
1012 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE); 966 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
@@ -1014,7 +968,7 @@ static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1014 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc); 968 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
1015} 969}
1016 970
1017static inline int inet_diag_type2proto(int type) 971static int inet_diag_type2proto(int type)
1018{ 972{
1019 switch (type) { 973 switch (type) {
1020 case TCPDIAG_GETSOCK: 974 case TCPDIAG_GETSOCK:
@@ -1026,12 +980,13 @@ static inline int inet_diag_type2proto(int type)
1026 } 980 }
1027} 981}
1028 982
1029static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb) 983static int inet_diag_dump_compat(struct sk_buff *skb,
984 struct netlink_callback *cb)
1030{ 985{
1031 struct inet_diag_req *rc = nlmsg_data(cb->nlh); 986 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
987 int hdrlen = sizeof(struct inet_diag_req);
1032 struct inet_diag_req_v2 req; 988 struct inet_diag_req_v2 req;
1033 struct nlattr *bc = NULL; 989 struct nlattr *bc = NULL;
1034 int hdrlen = sizeof(struct inet_diag_req);
1035 990
1036 req.sdiag_family = AF_UNSPEC; /* compatibility */ 991 req.sdiag_family = AF_UNSPEC; /* compatibility */
1037 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type); 992 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
@@ -1046,7 +1001,7 @@ static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *c
1046} 1001}
1047 1002
1048static int inet_diag_get_exact_compat(struct sk_buff *in_skb, 1003static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
1049 const struct nlmsghdr *nlh) 1004 const struct nlmsghdr *nlh)
1050{ 1005{
1051 struct inet_diag_req *rc = nlmsg_data(nlh); 1006 struct inet_diag_req *rc = nlmsg_data(nlh);
1052 struct inet_diag_req_v2 req; 1007 struct inet_diag_req_v2 req;
@@ -1075,7 +1030,7 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
1075 1030
1076 attr = nlmsg_find_attr(nlh, hdrlen, 1031 attr = nlmsg_find_attr(nlh, hdrlen,
1077 INET_DIAG_REQ_BYTECODE); 1032 INET_DIAG_REQ_BYTECODE);
1078 if (attr == NULL || 1033 if (!attr ||
1079 nla_len(attr) < sizeof(struct inet_diag_bc_op) || 1034 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1080 inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 1035 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1081 return -EINVAL; 1036 return -EINVAL;
@@ -1102,9 +1057,10 @@ static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1102 if (h->nlmsg_flags & NLM_F_DUMP) { 1057 if (h->nlmsg_flags & NLM_F_DUMP) {
1103 if (nlmsg_attrlen(h, hdrlen)) { 1058 if (nlmsg_attrlen(h, hdrlen)) {
1104 struct nlattr *attr; 1059 struct nlattr *attr;
1060
1105 attr = nlmsg_find_attr(h, hdrlen, 1061 attr = nlmsg_find_attr(h, hdrlen,
1106 INET_DIAG_REQ_BYTECODE); 1062 INET_DIAG_REQ_BYTECODE);
1107 if (attr == NULL || 1063 if (!attr ||
1108 nla_len(attr) < sizeof(struct inet_diag_bc_op) || 1064 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1109 inet_diag_bc_audit(nla_data(attr), nla_len(attr))) 1065 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1110 return -EINVAL; 1066 return -EINVAL;
@@ -1140,7 +1096,7 @@ int inet_diag_register(const struct inet_diag_handler *h)
1140 1096
1141 mutex_lock(&inet_diag_table_mutex); 1097 mutex_lock(&inet_diag_table_mutex);
1142 err = -EEXIST; 1098 err = -EEXIST;
1143 if (inet_diag_table[type] == NULL) { 1099 if (!inet_diag_table[type]) {
1144 inet_diag_table[type] = h; 1100 inet_diag_table[type] = h;
1145 err = 0; 1101 err = 0;
1146 } 1102 }
diff --git a/net/ipv4/inet_fragment.c b/net/ipv4/inet_fragment.c
index e7920352646a..5e346a082e5f 100644
--- a/net/ipv4/inet_fragment.c
+++ b/net/ipv4/inet_fragment.c
@@ -385,7 +385,7 @@ static struct inet_frag_queue *inet_frag_alloc(struct netns_frags *nf,
385 } 385 }
386 386
387 q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC); 387 q = kmem_cache_zalloc(f->frags_cachep, GFP_ATOMIC);
388 if (q == NULL) 388 if (!q)
389 return NULL; 389 return NULL;
390 390
391 q->net = nf; 391 q->net = nf;
@@ -406,7 +406,7 @@ static struct inet_frag_queue *inet_frag_create(struct netns_frags *nf,
406 struct inet_frag_queue *q; 406 struct inet_frag_queue *q;
407 407
408 q = inet_frag_alloc(nf, f, arg); 408 q = inet_frag_alloc(nf, f, arg);
409 if (q == NULL) 409 if (!q)
410 return NULL; 410 return NULL;
411 411
412 return inet_frag_intern(nf, q, f, arg); 412 return inet_frag_intern(nf, q, f, arg);
diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
index 9111a4e22155..c6fb80bd5826 100644
--- a/net/ipv4/inet_hashtables.c
+++ b/net/ipv4/inet_hashtables.c
@@ -24,9 +24,9 @@
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, 27static u32 inet_ehashfn(const struct net *net, const __be32 laddr,
28 const __u16 lport, const __be32 faddr, 28 const __u16 lport, const __be32 faddr,
29 const __be16 fport) 29 const __be16 fport)
30{ 30{
31 static u32 inet_ehash_secret __read_mostly; 31 static u32 inet_ehash_secret __read_mostly;
32 32
@@ -36,17 +36,21 @@ static unsigned int inet_ehashfn(struct net *net, const __be32 laddr,
36 inet_ehash_secret + net_hash_mix(net)); 36 inet_ehash_secret + net_hash_mix(net));
37} 37}
38 38
39 39/* This function handles inet_sock, but also timewait and request sockets
40static unsigned int inet_sk_ehashfn(const struct sock *sk) 40 * for IPv4/IPv6.
41 */
42u32 sk_ehashfn(const struct sock *sk)
41{ 43{
42 const struct inet_sock *inet = inet_sk(sk); 44#if IS_ENABLED(CONFIG_IPV6)
43 const __be32 laddr = inet->inet_rcv_saddr; 45 if (sk->sk_family == AF_INET6 &&
44 const __u16 lport = inet->inet_num; 46 !ipv6_addr_v4mapped(&sk->sk_v6_daddr))
45 const __be32 faddr = inet->inet_daddr; 47 return inet6_ehashfn(sock_net(sk),
46 const __be16 fport = inet->inet_dport; 48 &sk->sk_v6_rcv_saddr, sk->sk_num,
47 struct net *net = sock_net(sk); 49 &sk->sk_v6_daddr, sk->sk_dport);
48 50#endif
49 return inet_ehashfn(net, laddr, lport, faddr, fport); 51 return inet_ehashfn(sock_net(sk),
52 sk->sk_rcv_saddr, sk->sk_num,
53 sk->sk_daddr, sk->sk_dport);
50} 54}
51 55
52/* 56/*
@@ -60,8 +64,8 @@ struct inet_bind_bucket *inet_bind_bucket_create(struct kmem_cache *cachep,
60{ 64{
61 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC); 65 struct inet_bind_bucket *tb = kmem_cache_alloc(cachep, GFP_ATOMIC);
62 66
63 if (tb != NULL) { 67 if (tb) {
64 write_pnet(&tb->ib_net, hold_net(net)); 68 write_pnet(&tb->ib_net, net);
65 tb->port = snum; 69 tb->port = snum;
66 tb->fastreuse = 0; 70 tb->fastreuse = 0;
67 tb->fastreuseport = 0; 71 tb->fastreuseport = 0;
@@ -79,7 +83,6 @@ void inet_bind_bucket_destroy(struct kmem_cache *cachep, struct inet_bind_bucket
79{ 83{
80 if (hlist_empty(&tb->owners)) { 84 if (hlist_empty(&tb->owners)) {
81 __hlist_del(&tb->node); 85 __hlist_del(&tb->node);
82 release_net(ib_net(tb));
83 kmem_cache_free(cachep, tb); 86 kmem_cache_free(cachep, tb);
84 } 87 }
85} 88}
@@ -263,11 +266,19 @@ void sock_gen_put(struct sock *sk)
263 266
264 if (sk->sk_state == TCP_TIME_WAIT) 267 if (sk->sk_state == TCP_TIME_WAIT)
265 inet_twsk_free(inet_twsk(sk)); 268 inet_twsk_free(inet_twsk(sk));
269 else if (sk->sk_state == TCP_NEW_SYN_RECV)
270 reqsk_free(inet_reqsk(sk));
266 else 271 else
267 sk_free(sk); 272 sk_free(sk);
268} 273}
269EXPORT_SYMBOL_GPL(sock_gen_put); 274EXPORT_SYMBOL_GPL(sock_gen_put);
270 275
276void sock_edemux(struct sk_buff *skb)
277{
278 sock_gen_put(skb->sk);
279}
280EXPORT_SYMBOL(sock_edemux);
281
271struct sock *__inet_lookup_established(struct net *net, 282struct sock *__inet_lookup_established(struct net *net,
272 struct inet_hashinfo *hashinfo, 283 struct inet_hashinfo *hashinfo,
273 const __be32 saddr, const __be16 sport, 284 const __be32 saddr, const __be16 sport,
@@ -377,7 +388,7 @@ static int __inet_check_established(struct inet_timewait_death_row *death_row,
377 *twp = tw; 388 *twp = tw;
378 } else if (tw) { 389 } else if (tw) {
379 /* Silly. Should hash-dance instead... */ 390 /* Silly. Should hash-dance instead... */
380 inet_twsk_deschedule(tw, death_row); 391 inet_twsk_deschedule(tw);
381 392
382 inet_twsk_put(tw); 393 inet_twsk_put(tw);
383 } 394 }
@@ -400,13 +411,13 @@ int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw)
400{ 411{
401 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; 412 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
402 struct hlist_nulls_head *list; 413 struct hlist_nulls_head *list;
403 spinlock_t *lock;
404 struct inet_ehash_bucket *head; 414 struct inet_ehash_bucket *head;
415 spinlock_t *lock;
405 int twrefcnt = 0; 416 int twrefcnt = 0;
406 417
407 WARN_ON(!sk_unhashed(sk)); 418 WARN_ON(!sk_unhashed(sk));
408 419
409 sk->sk_hash = inet_sk_ehashfn(sk); 420 sk->sk_hash = sk_ehashfn(sk);
410 head = inet_ehash_bucket(hashinfo, sk->sk_hash); 421 head = inet_ehash_bucket(hashinfo, sk->sk_hash);
411 list = &head->chain; 422 list = &head->chain;
412 lock = inet_ehash_lockp(hashinfo, sk->sk_hash); 423 lock = inet_ehash_lockp(hashinfo, sk->sk_hash);
@@ -423,15 +434,13 @@ int __inet_hash_nolisten(struct sock *sk, struct inet_timewait_sock *tw)
423} 434}
424EXPORT_SYMBOL_GPL(__inet_hash_nolisten); 435EXPORT_SYMBOL_GPL(__inet_hash_nolisten);
425 436
426static void __inet_hash(struct sock *sk) 437int __inet_hash(struct sock *sk, struct inet_timewait_sock *tw)
427{ 438{
428 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo; 439 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
429 struct inet_listen_hashbucket *ilb; 440 struct inet_listen_hashbucket *ilb;
430 441
431 if (sk->sk_state != TCP_LISTEN) { 442 if (sk->sk_state != TCP_LISTEN)
432 __inet_hash_nolisten(sk, NULL); 443 return __inet_hash_nolisten(sk, tw);
433 return;
434 }
435 444
436 WARN_ON(!sk_unhashed(sk)); 445 WARN_ON(!sk_unhashed(sk));
437 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)]; 446 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
@@ -440,13 +449,15 @@ static void __inet_hash(struct sock *sk)
440 __sk_nulls_add_node_rcu(sk, &ilb->head); 449 __sk_nulls_add_node_rcu(sk, &ilb->head);
441 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1); 450 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
442 spin_unlock(&ilb->lock); 451 spin_unlock(&ilb->lock);
452 return 0;
443} 453}
454EXPORT_SYMBOL(__inet_hash);
444 455
445void inet_hash(struct sock *sk) 456void inet_hash(struct sock *sk)
446{ 457{
447 if (sk->sk_state != TCP_CLOSE) { 458 if (sk->sk_state != TCP_CLOSE) {
448 local_bh_disable(); 459 local_bh_disable();
449 __inet_hash(sk); 460 __inet_hash(sk, NULL);
450 local_bh_enable(); 461 local_bh_enable();
451 } 462 }
452} 463}
@@ -477,8 +488,7 @@ EXPORT_SYMBOL_GPL(inet_unhash);
477int __inet_hash_connect(struct inet_timewait_death_row *death_row, 488int __inet_hash_connect(struct inet_timewait_death_row *death_row,
478 struct sock *sk, u32 port_offset, 489 struct sock *sk, u32 port_offset,
479 int (*check_established)(struct inet_timewait_death_row *, 490 int (*check_established)(struct inet_timewait_death_row *,
480 struct sock *, __u16, struct inet_timewait_sock **), 491 struct sock *, __u16, struct inet_timewait_sock **))
481 int (*hash)(struct sock *sk, struct inet_timewait_sock *twp))
482{ 492{
483 struct inet_hashinfo *hinfo = death_row->hashinfo; 493 struct inet_hashinfo *hinfo = death_row->hashinfo;
484 const unsigned short snum = inet_sk(sk)->inet_num; 494 const unsigned short snum = inet_sk(sk)->inet_num;
@@ -548,14 +558,14 @@ ok:
548 inet_bind_hash(sk, tb, port); 558 inet_bind_hash(sk, tb, port);
549 if (sk_unhashed(sk)) { 559 if (sk_unhashed(sk)) {
550 inet_sk(sk)->inet_sport = htons(port); 560 inet_sk(sk)->inet_sport = htons(port);
551 twrefcnt += hash(sk, tw); 561 twrefcnt += __inet_hash_nolisten(sk, tw);
552 } 562 }
553 if (tw) 563 if (tw)
554 twrefcnt += inet_twsk_bind_unhash(tw, hinfo); 564 twrefcnt += inet_twsk_bind_unhash(tw, hinfo);
555 spin_unlock(&head->lock); 565 spin_unlock(&head->lock);
556 566
557 if (tw) { 567 if (tw) {
558 inet_twsk_deschedule(tw, death_row); 568 inet_twsk_deschedule(tw);
559 while (twrefcnt) { 569 while (twrefcnt) {
560 twrefcnt--; 570 twrefcnt--;
561 inet_twsk_put(tw); 571 inet_twsk_put(tw);
@@ -570,7 +580,7 @@ ok:
570 tb = inet_csk(sk)->icsk_bind_hash; 580 tb = inet_csk(sk)->icsk_bind_hash;
571 spin_lock_bh(&head->lock); 581 spin_lock_bh(&head->lock);
572 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) { 582 if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
573 hash(sk, NULL); 583 __inet_hash_nolisten(sk, NULL);
574 spin_unlock_bh(&head->lock); 584 spin_unlock_bh(&head->lock);
575 return 0; 585 return 0;
576 } else { 586 } else {
@@ -590,7 +600,7 @@ int inet_hash_connect(struct inet_timewait_death_row *death_row,
590 struct sock *sk) 600 struct sock *sk)
591{ 601{
592 return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk), 602 return __inet_hash_connect(death_row, sk, inet_sk_port_offset(sk),
593 __inet_check_established, __inet_hash_nolisten); 603 __inet_check_established);
594} 604}
595EXPORT_SYMBOL_GPL(inet_hash_connect); 605EXPORT_SYMBOL_GPL(inet_hash_connect);
596 606
diff --git a/net/ipv4/inet_timewait_sock.c b/net/ipv4/inet_timewait_sock.c
index 6d592f8555fb..00ec8d5d7e7e 100644
--- a/net/ipv4/inet_timewait_sock.c
+++ b/net/ipv4/inet_timewait_sock.c
@@ -67,9 +67,9 @@ int inet_twsk_bind_unhash(struct inet_timewait_sock *tw,
67} 67}
68 68
69/* Must be called with locally disabled BHs. */ 69/* Must be called with locally disabled BHs. */
70static void __inet_twsk_kill(struct inet_timewait_sock *tw, 70static void inet_twsk_kill(struct inet_timewait_sock *tw)
71 struct inet_hashinfo *hashinfo)
72{ 71{
72 struct inet_hashinfo *hashinfo = tw->tw_dr->hashinfo;
73 struct inet_bind_hashbucket *bhead; 73 struct inet_bind_hashbucket *bhead;
74 int refcnt; 74 int refcnt;
75 /* Unlink from established hashes. */ 75 /* Unlink from established hashes. */
@@ -89,6 +89,8 @@ static void __inet_twsk_kill(struct inet_timewait_sock *tw,
89 89
90 BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt)); 90 BUG_ON(refcnt >= atomic_read(&tw->tw_refcnt));
91 atomic_sub(refcnt, &tw->tw_refcnt); 91 atomic_sub(refcnt, &tw->tw_refcnt);
92 atomic_dec(&tw->tw_dr->tw_count);
93 inet_twsk_put(tw);
92} 94}
93 95
94void inet_twsk_free(struct inet_timewait_sock *tw) 96void inet_twsk_free(struct inet_timewait_sock *tw)
@@ -98,7 +100,6 @@ void inet_twsk_free(struct inet_timewait_sock *tw)
98#ifdef SOCK_REFCNT_DEBUG 100#ifdef SOCK_REFCNT_DEBUG
99 pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw); 101 pr_debug("%s timewait_sock %p released\n", tw->tw_prot->name, tw);
100#endif 102#endif
101 release_net(twsk_net(tw));
102 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw); 103 kmem_cache_free(tw->tw_prot->twsk_prot->twsk_slab, tw);
103 module_put(owner); 104 module_put(owner);
104} 105}
@@ -169,16 +170,34 @@ void __inet_twsk_hashdance(struct inet_timewait_sock *tw, struct sock *sk,
169} 170}
170EXPORT_SYMBOL_GPL(__inet_twsk_hashdance); 171EXPORT_SYMBOL_GPL(__inet_twsk_hashdance);
171 172
172struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int state) 173void tw_timer_handler(unsigned long data)
173{ 174{
174 struct inet_timewait_sock *tw = 175 struct inet_timewait_sock *tw = (struct inet_timewait_sock *)data;
175 kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab, 176
176 GFP_ATOMIC); 177 if (tw->tw_kill)
177 if (tw != NULL) { 178 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
179 else
180 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
181 inet_twsk_kill(tw);
182}
183
184struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk,
185 struct inet_timewait_death_row *dr,
186 const int state)
187{
188 struct inet_timewait_sock *tw;
189
190 if (atomic_read(&dr->tw_count) >= dr->sysctl_max_tw_buckets)
191 return NULL;
192
193 tw = kmem_cache_alloc(sk->sk_prot_creator->twsk_prot->twsk_slab,
194 GFP_ATOMIC);
195 if (tw) {
178 const struct inet_sock *inet = inet_sk(sk); 196 const struct inet_sock *inet = inet_sk(sk);
179 197
180 kmemcheck_annotate_bitfield(tw, flags); 198 kmemcheck_annotate_bitfield(tw, flags);
181 199
200 tw->tw_dr = dr;
182 /* Give us an identity. */ 201 /* Give us an identity. */
183 tw->tw_daddr = inet->inet_daddr; 202 tw->tw_daddr = inet->inet_daddr;
184 tw->tw_rcv_saddr = inet->inet_rcv_saddr; 203 tw->tw_rcv_saddr = inet->inet_rcv_saddr;
@@ -195,14 +214,16 @@ struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int stat
195 tw->tw_ipv6only = 0; 214 tw->tw_ipv6only = 0;
196 tw->tw_transparent = inet->transparent; 215 tw->tw_transparent = inet->transparent;
197 tw->tw_prot = sk->sk_prot_creator; 216 tw->tw_prot = sk->sk_prot_creator;
198 twsk_net_set(tw, hold_net(sock_net(sk))); 217 atomic64_set(&tw->tw_cookie, atomic64_read(&sk->sk_cookie));
218 twsk_net_set(tw, sock_net(sk));
219 setup_timer(&tw->tw_timer, tw_timer_handler, (unsigned long)tw);
199 /* 220 /*
200 * Because we use RCU lookups, we should not set tw_refcnt 221 * Because we use RCU lookups, we should not set tw_refcnt
201 * to a non null value before everything is setup for this 222 * to a non null value before everything is setup for this
202 * timewait socket. 223 * timewait socket.
203 */ 224 */
204 atomic_set(&tw->tw_refcnt, 0); 225 atomic_set(&tw->tw_refcnt, 0);
205 inet_twsk_dead_node_init(tw); 226
206 __module_get(tw->tw_prot->owner); 227 __module_get(tw->tw_prot->owner);
207 } 228 }
208 229
@@ -210,139 +231,20 @@ struct inet_timewait_sock *inet_twsk_alloc(const struct sock *sk, const int stat
210} 231}
211EXPORT_SYMBOL_GPL(inet_twsk_alloc); 232EXPORT_SYMBOL_GPL(inet_twsk_alloc);
212 233
213/* Returns non-zero if quota exceeded. */
214static int inet_twdr_do_twkill_work(struct inet_timewait_death_row *twdr,
215 const int slot)
216{
217 struct inet_timewait_sock *tw;
218 unsigned int killed;
219 int ret;
220
221 /* NOTE: compare this to previous version where lock
222 * was released after detaching chain. It was racy,
223 * because tw buckets are scheduled in not serialized context
224 * in 2.3 (with netfilter), and with softnet it is common, because
225 * soft irqs are not sequenced.
226 */
227 killed = 0;
228 ret = 0;
229rescan:
230 inet_twsk_for_each_inmate(tw, &twdr->cells[slot]) {
231 __inet_twsk_del_dead_node(tw);
232 spin_unlock(&twdr->death_lock);
233 __inet_twsk_kill(tw, twdr->hashinfo);
234#ifdef CONFIG_NET_NS
235 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITED);
236#endif
237 inet_twsk_put(tw);
238 killed++;
239 spin_lock(&twdr->death_lock);
240 if (killed > INET_TWDR_TWKILL_QUOTA) {
241 ret = 1;
242 break;
243 }
244
245 /* While we dropped twdr->death_lock, another cpu may have
246 * killed off the next TW bucket in the list, therefore
247 * do a fresh re-read of the hlist head node with the
248 * lock reacquired. We still use the hlist traversal
249 * macro in order to get the prefetches.
250 */
251 goto rescan;
252 }
253
254 twdr->tw_count -= killed;
255#ifndef CONFIG_NET_NS
256 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITED, killed);
257#endif
258 return ret;
259}
260
261void inet_twdr_hangman(unsigned long data)
262{
263 struct inet_timewait_death_row *twdr;
264 unsigned int need_timer;
265
266 twdr = (struct inet_timewait_death_row *)data;
267 spin_lock(&twdr->death_lock);
268
269 if (twdr->tw_count == 0)
270 goto out;
271
272 need_timer = 0;
273 if (inet_twdr_do_twkill_work(twdr, twdr->slot)) {
274 twdr->thread_slots |= (1 << twdr->slot);
275 schedule_work(&twdr->twkill_work);
276 need_timer = 1;
277 } else {
278 /* We purged the entire slot, anything left? */
279 if (twdr->tw_count)
280 need_timer = 1;
281 twdr->slot = ((twdr->slot + 1) & (INET_TWDR_TWKILL_SLOTS - 1));
282 }
283 if (need_timer)
284 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
285out:
286 spin_unlock(&twdr->death_lock);
287}
288EXPORT_SYMBOL_GPL(inet_twdr_hangman);
289
290void inet_twdr_twkill_work(struct work_struct *work)
291{
292 struct inet_timewait_death_row *twdr =
293 container_of(work, struct inet_timewait_death_row, twkill_work);
294 int i;
295
296 BUILD_BUG_ON((INET_TWDR_TWKILL_SLOTS - 1) >
297 (sizeof(twdr->thread_slots) * 8));
298
299 while (twdr->thread_slots) {
300 spin_lock_bh(&twdr->death_lock);
301 for (i = 0; i < INET_TWDR_TWKILL_SLOTS; i++) {
302 if (!(twdr->thread_slots & (1 << i)))
303 continue;
304
305 while (inet_twdr_do_twkill_work(twdr, i) != 0) {
306 if (need_resched()) {
307 spin_unlock_bh(&twdr->death_lock);
308 schedule();
309 spin_lock_bh(&twdr->death_lock);
310 }
311 }
312
313 twdr->thread_slots &= ~(1 << i);
314 }
315 spin_unlock_bh(&twdr->death_lock);
316 }
317}
318EXPORT_SYMBOL_GPL(inet_twdr_twkill_work);
319
320/* These are always called from BH context. See callers in 234/* These are always called from BH context. See callers in
321 * tcp_input.c to verify this. 235 * tcp_input.c to verify this.
322 */ 236 */
323 237
324/* This is for handling early-kills of TIME_WAIT sockets. */ 238/* This is for handling early-kills of TIME_WAIT sockets. */
325void inet_twsk_deschedule(struct inet_timewait_sock *tw, 239void inet_twsk_deschedule(struct inet_timewait_sock *tw)
326 struct inet_timewait_death_row *twdr)
327{ 240{
328 spin_lock(&twdr->death_lock); 241 if (del_timer_sync(&tw->tw_timer))
329 if (inet_twsk_del_dead_node(tw)) { 242 inet_twsk_kill(tw);
330 inet_twsk_put(tw);
331 if (--twdr->tw_count == 0)
332 del_timer(&twdr->tw_timer);
333 }
334 spin_unlock(&twdr->death_lock);
335 __inet_twsk_kill(tw, twdr->hashinfo);
336} 243}
337EXPORT_SYMBOL(inet_twsk_deschedule); 244EXPORT_SYMBOL(inet_twsk_deschedule);
338 245
339void inet_twsk_schedule(struct inet_timewait_sock *tw, 246void inet_twsk_schedule(struct inet_timewait_sock *tw, const int timeo)
340 struct inet_timewait_death_row *twdr,
341 const int timeo, const int timewait_len)
342{ 247{
343 struct hlist_head *list;
344 int slot;
345
346 /* timeout := RTO * 3.5 248 /* timeout := RTO * 3.5
347 * 249 *
348 * 3.5 = 1+2+0.5 to wait for two retransmits. 250 * 3.5 = 1+2+0.5 to wait for two retransmits.
@@ -367,115 +269,15 @@ void inet_twsk_schedule(struct inet_timewait_sock *tw,
367 * is greater than TS tick!) and detect old duplicates with help 269 * is greater than TS tick!) and detect old duplicates with help
368 * of PAWS. 270 * of PAWS.
369 */ 271 */
370 slot = (timeo + (1 << INET_TWDR_RECYCLE_TICK) - 1) >> INET_TWDR_RECYCLE_TICK;
371 272
372 spin_lock(&twdr->death_lock); 273 tw->tw_kill = timeo <= 4*HZ;
373 274 if (!mod_timer_pinned(&tw->tw_timer, jiffies + timeo)) {
374 /* Unlink it, if it was scheduled */
375 if (inet_twsk_del_dead_node(tw))
376 twdr->tw_count--;
377 else
378 atomic_inc(&tw->tw_refcnt); 275 atomic_inc(&tw->tw_refcnt);
379 276 atomic_inc(&tw->tw_dr->tw_count);
380 if (slot >= INET_TWDR_RECYCLE_SLOTS) {
381 /* Schedule to slow timer */
382 if (timeo >= timewait_len) {
383 slot = INET_TWDR_TWKILL_SLOTS - 1;
384 } else {
385 slot = DIV_ROUND_UP(timeo, twdr->period);
386 if (slot >= INET_TWDR_TWKILL_SLOTS)
387 slot = INET_TWDR_TWKILL_SLOTS - 1;
388 }
389 tw->tw_ttd = inet_tw_time_stamp() + timeo;
390 slot = (twdr->slot + slot) & (INET_TWDR_TWKILL_SLOTS - 1);
391 list = &twdr->cells[slot];
392 } else {
393 tw->tw_ttd = inet_tw_time_stamp() + (slot << INET_TWDR_RECYCLE_TICK);
394
395 if (twdr->twcal_hand < 0) {
396 twdr->twcal_hand = 0;
397 twdr->twcal_jiffie = jiffies;
398 twdr->twcal_timer.expires = twdr->twcal_jiffie +
399 (slot << INET_TWDR_RECYCLE_TICK);
400 add_timer(&twdr->twcal_timer);
401 } else {
402 if (time_after(twdr->twcal_timer.expires,
403 jiffies + (slot << INET_TWDR_RECYCLE_TICK)))
404 mod_timer(&twdr->twcal_timer,
405 jiffies + (slot << INET_TWDR_RECYCLE_TICK));
406 slot = (twdr->twcal_hand + slot) & (INET_TWDR_RECYCLE_SLOTS - 1);
407 }
408 list = &twdr->twcal_row[slot];
409 } 277 }
410
411 hlist_add_head(&tw->tw_death_node, list);
412
413 if (twdr->tw_count++ == 0)
414 mod_timer(&twdr->tw_timer, jiffies + twdr->period);
415 spin_unlock(&twdr->death_lock);
416} 278}
417EXPORT_SYMBOL_GPL(inet_twsk_schedule); 279EXPORT_SYMBOL_GPL(inet_twsk_schedule);
418 280
419void inet_twdr_twcal_tick(unsigned long data)
420{
421 struct inet_timewait_death_row *twdr;
422 int n, slot;
423 unsigned long j;
424 unsigned long now = jiffies;
425 int killed = 0;
426 int adv = 0;
427
428 twdr = (struct inet_timewait_death_row *)data;
429
430 spin_lock(&twdr->death_lock);
431 if (twdr->twcal_hand < 0)
432 goto out;
433
434 slot = twdr->twcal_hand;
435 j = twdr->twcal_jiffie;
436
437 for (n = 0; n < INET_TWDR_RECYCLE_SLOTS; n++) {
438 if (time_before_eq(j, now)) {
439 struct hlist_node *safe;
440 struct inet_timewait_sock *tw;
441
442 inet_twsk_for_each_inmate_safe(tw, safe,
443 &twdr->twcal_row[slot]) {
444 __inet_twsk_del_dead_node(tw);
445 __inet_twsk_kill(tw, twdr->hashinfo);
446#ifdef CONFIG_NET_NS
447 NET_INC_STATS_BH(twsk_net(tw), LINUX_MIB_TIMEWAITKILLED);
448#endif
449 inet_twsk_put(tw);
450 killed++;
451 }
452 } else {
453 if (!adv) {
454 adv = 1;
455 twdr->twcal_jiffie = j;
456 twdr->twcal_hand = slot;
457 }
458
459 if (!hlist_empty(&twdr->twcal_row[slot])) {
460 mod_timer(&twdr->twcal_timer, j);
461 goto out;
462 }
463 }
464 j += 1 << INET_TWDR_RECYCLE_TICK;
465 slot = (slot + 1) & (INET_TWDR_RECYCLE_SLOTS - 1);
466 }
467 twdr->twcal_hand = -1;
468
469out:
470 if ((twdr->tw_count -= killed) == 0)
471 del_timer(&twdr->tw_timer);
472#ifndef CONFIG_NET_NS
473 NET_ADD_STATS_BH(&init_net, LINUX_MIB_TIMEWAITKILLED, killed);
474#endif
475 spin_unlock(&twdr->death_lock);
476}
477EXPORT_SYMBOL_GPL(inet_twdr_twcal_tick);
478
479void inet_twsk_purge(struct inet_hashinfo *hashinfo, 281void inet_twsk_purge(struct inet_hashinfo *hashinfo,
480 struct inet_timewait_death_row *twdr, int family) 282 struct inet_timewait_death_row *twdr, int family)
481{ 283{
@@ -487,6 +289,7 @@ void inet_twsk_purge(struct inet_hashinfo *hashinfo,
487 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) { 289 for (slot = 0; slot <= hashinfo->ehash_mask; slot++) {
488 struct inet_ehash_bucket *head = &hashinfo->ehash[slot]; 290 struct inet_ehash_bucket *head = &hashinfo->ehash[slot];
489restart_rcu: 291restart_rcu:
292 cond_resched();
490 rcu_read_lock(); 293 rcu_read_lock();
491restart: 294restart:
492 sk_nulls_for_each_rcu(sk, node, &head->chain) { 295 sk_nulls_for_each_rcu(sk, node, &head->chain) {
@@ -508,7 +311,7 @@ restart:
508 311
509 rcu_read_unlock(); 312 rcu_read_unlock();
510 local_bh_disable(); 313 local_bh_disable();
511 inet_twsk_deschedule(tw, twdr); 314 inet_twsk_deschedule(tw);
512 local_bh_enable(); 315 local_bh_enable();
513 inet_twsk_put(tw); 316 inet_twsk_put(tw);
514 goto restart_rcu; 317 goto restart_rcu;
diff --git a/net/ipv4/ip_forward.c b/net/ipv4/ip_forward.c
index d9bc28ac5d1b..939992c456f3 100644
--- a/net/ipv4/ip_forward.c
+++ b/net/ipv4/ip_forward.c
@@ -57,7 +57,7 @@ static bool ip_exceeds_mtu(const struct sk_buff *skb, unsigned int mtu)
57} 57}
58 58
59 59
60static int ip_forward_finish(struct sk_buff *skb) 60static int ip_forward_finish(struct sock *sk, struct sk_buff *skb)
61{ 61{
62 struct ip_options *opt = &(IPCB(skb)->opt); 62 struct ip_options *opt = &(IPCB(skb)->opt);
63 63
@@ -68,7 +68,7 @@ static int ip_forward_finish(struct sk_buff *skb)
68 ip_forward_options(skb); 68 ip_forward_options(skb);
69 69
70 skb_sender_cpu_clear(skb); 70 skb_sender_cpu_clear(skb);
71 return dst_output(skb); 71 return dst_output_sk(sk, skb);
72} 72}
73 73
74int ip_forward(struct sk_buff *skb) 74int ip_forward(struct sk_buff *skb)
@@ -136,8 +136,8 @@ int ip_forward(struct sk_buff *skb)
136 136
137 skb->priority = rt_tos2priority(iph->tos); 137 skb->priority = rt_tos2priority(iph->tos);
138 138
139 return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, 139 return NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, NULL, skb,
140 rt->dst.dev, ip_forward_finish); 140 skb->dev, rt->dst.dev, ip_forward_finish);
141 141
142sr_failed: 142sr_failed:
143 /* 143 /*
diff --git a/net/ipv4/ip_fragment.c b/net/ipv4/ip_fragment.c
index 145a50c4d566..cc1da6d9cb35 100644
--- a/net/ipv4/ip_fragment.c
+++ b/net/ipv4/ip_fragment.c
@@ -372,7 +372,7 @@ static int ip_frag_queue(struct ipq *qp, struct sk_buff *skb)
372 goto err; 372 goto err;
373 373
374 err = -ENOMEM; 374 err = -ENOMEM;
375 if (pskb_pull(skb, ihl) == NULL) 375 if (!pskb_pull(skb, ihl))
376 goto err; 376 goto err;
377 377
378 err = pskb_trim_rcsum(skb, end - offset); 378 err = pskb_trim_rcsum(skb, end - offset);
@@ -537,7 +537,7 @@ static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
537 qp->q.fragments = head; 537 qp->q.fragments = head;
538 } 538 }
539 539
540 WARN_ON(head == NULL); 540 WARN_ON(!head);
541 WARN_ON(FRAG_CB(head)->offset != 0); 541 WARN_ON(FRAG_CB(head)->offset != 0);
542 542
543 /* Allocate a new buffer for the datagram. */ 543 /* Allocate a new buffer for the datagram. */
@@ -559,7 +559,8 @@ static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
559 struct sk_buff *clone; 559 struct sk_buff *clone;
560 int i, plen = 0; 560 int i, plen = 0;
561 561
562 if ((clone = alloc_skb(0, GFP_ATOMIC)) == NULL) 562 clone = alloc_skb(0, GFP_ATOMIC);
563 if (!clone)
563 goto out_nomem; 564 goto out_nomem;
564 clone->next = head->next; 565 clone->next = head->next;
565 head->next = clone; 566 head->next = clone;
@@ -638,7 +639,8 @@ int ip_defrag(struct sk_buff *skb, u32 user)
638 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS); 639 IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
639 640
640 /* Lookup (or create) queue header */ 641 /* Lookup (or create) queue header */
641 if ((qp = ip_find(net, ip_hdr(skb), user)) != NULL) { 642 qp = ip_find(net, ip_hdr(skb), user);
643 if (qp) {
642 int ret; 644 int ret;
643 645
644 spin_lock(&qp->q.lock); 646 spin_lock(&qp->q.lock);
@@ -754,7 +756,7 @@ static int __net_init ip4_frags_ns_ctl_register(struct net *net)
754 table = ip4_frags_ns_ctl_table; 756 table = ip4_frags_ns_ctl_table;
755 if (!net_eq(net, &init_net)) { 757 if (!net_eq(net, &init_net)) {
756 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL); 758 table = kmemdup(table, sizeof(ip4_frags_ns_ctl_table), GFP_KERNEL);
757 if (table == NULL) 759 if (!table)
758 goto err_alloc; 760 goto err_alloc;
759 761
760 table[0].data = &net->ipv4.frags.high_thresh; 762 table[0].data = &net->ipv4.frags.high_thresh;
@@ -770,7 +772,7 @@ static int __net_init ip4_frags_ns_ctl_register(struct net *net)
770 } 772 }
771 773
772 hdr = register_net_sysctl(net, "net/ipv4", table); 774 hdr = register_net_sysctl(net, "net/ipv4", table);
773 if (hdr == NULL) 775 if (!hdr)
774 goto err_reg; 776 goto err_reg;
775 777
776 net->ipv4.frags_hdr = hdr; 778 net->ipv4.frags_hdr = hdr;
diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
index 6207275fc749..5fd706473c73 100644
--- a/net/ipv4/ip_gre.c
+++ b/net/ipv4/ip_gre.c
@@ -182,7 +182,7 @@ static int ipgre_err(struct sk_buff *skb, u32 info,
182 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags, 182 t = ip_tunnel_lookup(itn, skb->dev->ifindex, tpi->flags,
183 iph->daddr, iph->saddr, tpi->key); 183 iph->daddr, iph->saddr, tpi->key);
184 184
185 if (t == NULL) 185 if (!t)
186 return PACKET_REJECT; 186 return PACKET_REJECT;
187 187
188 if (t->parms.iph.daddr == 0 || 188 if (t->parms.iph.daddr == 0 ||
@@ -423,7 +423,7 @@ static int ipgre_open(struct net_device *dev)
423 return -EADDRNOTAVAIL; 423 return -EADDRNOTAVAIL;
424 dev = rt->dst.dev; 424 dev = rt->dst.dev;
425 ip_rt_put(rt); 425 ip_rt_put(rt);
426 if (__in_dev_get_rtnl(dev) == NULL) 426 if (!__in_dev_get_rtnl(dev))
427 return -EADDRNOTAVAIL; 427 return -EADDRNOTAVAIL;
428 t->mlink = dev->ifindex; 428 t->mlink = dev->ifindex;
429 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr); 429 ip_mc_inc_group(__in_dev_get_rtnl(dev), t->parms.iph.daddr);
@@ -456,6 +456,7 @@ static const struct net_device_ops ipgre_netdev_ops = {
456 .ndo_do_ioctl = ipgre_tunnel_ioctl, 456 .ndo_do_ioctl = ipgre_tunnel_ioctl,
457 .ndo_change_mtu = ip_tunnel_change_mtu, 457 .ndo_change_mtu = ip_tunnel_change_mtu,
458 .ndo_get_stats64 = ip_tunnel_get_stats64, 458 .ndo_get_stats64 = ip_tunnel_get_stats64,
459 .ndo_get_iflink = ip_tunnel_get_iflink,
459}; 460};
460 461
461#define GRE_FEATURES (NETIF_F_SG | \ 462#define GRE_FEATURES (NETIF_F_SG | \
@@ -621,10 +622,10 @@ static void ipgre_netlink_parms(struct nlattr *data[], struct nlattr *tb[],
621 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 622 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
622 623
623 if (data[IFLA_GRE_LOCAL]) 624 if (data[IFLA_GRE_LOCAL])
624 parms->iph.saddr = nla_get_be32(data[IFLA_GRE_LOCAL]); 625 parms->iph.saddr = nla_get_in_addr(data[IFLA_GRE_LOCAL]);
625 626
626 if (data[IFLA_GRE_REMOTE]) 627 if (data[IFLA_GRE_REMOTE])
627 parms->iph.daddr = nla_get_be32(data[IFLA_GRE_REMOTE]); 628 parms->iph.daddr = nla_get_in_addr(data[IFLA_GRE_REMOTE]);
628 629
629 if (data[IFLA_GRE_TTL]) 630 if (data[IFLA_GRE_TTL])
630 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]); 631 parms->iph.ttl = nla_get_u8(data[IFLA_GRE_TTL]);
@@ -686,6 +687,7 @@ static const struct net_device_ops gre_tap_netdev_ops = {
686 .ndo_validate_addr = eth_validate_addr, 687 .ndo_validate_addr = eth_validate_addr,
687 .ndo_change_mtu = ip_tunnel_change_mtu, 688 .ndo_change_mtu = ip_tunnel_change_mtu,
688 .ndo_get_stats64 = ip_tunnel_get_stats64, 689 .ndo_get_stats64 = ip_tunnel_get_stats64,
690 .ndo_get_iflink = ip_tunnel_get_iflink,
689}; 691};
690 692
691static void ipgre_tap_setup(struct net_device *dev) 693static void ipgre_tap_setup(struct net_device *dev)
@@ -776,8 +778,8 @@ static int ipgre_fill_info(struct sk_buff *skb, const struct net_device *dev)
776 nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) || 778 nla_put_be16(skb, IFLA_GRE_OFLAGS, tnl_flags_to_gre_flags(p->o_flags)) ||
777 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || 779 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
778 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || 780 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
779 nla_put_be32(skb, IFLA_GRE_LOCAL, p->iph.saddr) || 781 nla_put_in_addr(skb, IFLA_GRE_LOCAL, p->iph.saddr) ||
780 nla_put_be32(skb, IFLA_GRE_REMOTE, p->iph.daddr) || 782 nla_put_in_addr(skb, IFLA_GRE_REMOTE, p->iph.daddr) ||
781 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) || 783 nla_put_u8(skb, IFLA_GRE_TTL, p->iph.ttl) ||
782 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) || 784 nla_put_u8(skb, IFLA_GRE_TOS, p->iph.tos) ||
783 nla_put_u8(skb, IFLA_GRE_PMTUDISC, 785 nla_put_u8(skb, IFLA_GRE_PMTUDISC,
diff --git a/net/ipv4/ip_input.c b/net/ipv4/ip_input.c
index 3d4da2c16b6a..2db4c8773c1b 100644
--- a/net/ipv4/ip_input.c
+++ b/net/ipv4/ip_input.c
@@ -187,7 +187,7 @@ bool ip_call_ra_chain(struct sk_buff *skb)
187 return false; 187 return false;
188} 188}
189 189
190static int ip_local_deliver_finish(struct sk_buff *skb) 190static int ip_local_deliver_finish(struct sock *sk, struct sk_buff *skb)
191{ 191{
192 struct net *net = dev_net(skb->dev); 192 struct net *net = dev_net(skb->dev);
193 193
@@ -203,7 +203,7 @@ static int ip_local_deliver_finish(struct sk_buff *skb)
203 raw = raw_local_deliver(skb, protocol); 203 raw = raw_local_deliver(skb, protocol);
204 204
205 ipprot = rcu_dereference(inet_protos[protocol]); 205 ipprot = rcu_dereference(inet_protos[protocol]);
206 if (ipprot != NULL) { 206 if (ipprot) {
207 int ret; 207 int ret;
208 208
209 if (!ipprot->no_policy) { 209 if (!ipprot->no_policy) {
@@ -253,7 +253,8 @@ int ip_local_deliver(struct sk_buff *skb)
253 return 0; 253 return 0;
254 } 254 }
255 255
256 return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN, skb, skb->dev, NULL, 256 return NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_IN, NULL, skb,
257 skb->dev, NULL,
257 ip_local_deliver_finish); 258 ip_local_deliver_finish);
258} 259}
259 260
@@ -309,12 +310,12 @@ drop:
309int sysctl_ip_early_demux __read_mostly = 1; 310int sysctl_ip_early_demux __read_mostly = 1;
310EXPORT_SYMBOL(sysctl_ip_early_demux); 311EXPORT_SYMBOL(sysctl_ip_early_demux);
311 312
312static int ip_rcv_finish(struct sk_buff *skb) 313static int ip_rcv_finish(struct sock *sk, struct sk_buff *skb)
313{ 314{
314 const struct iphdr *iph = ip_hdr(skb); 315 const struct iphdr *iph = ip_hdr(skb);
315 struct rtable *rt; 316 struct rtable *rt;
316 317
317 if (sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 318 if (sysctl_ip_early_demux && !skb_dst(skb) && !skb->sk) {
318 const struct net_protocol *ipprot; 319 const struct net_protocol *ipprot;
319 int protocol = iph->protocol; 320 int protocol = iph->protocol;
320 321
@@ -387,7 +388,8 @@ int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
387 388
388 IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len); 389 IP_UPD_PO_STATS_BH(dev_net(dev), IPSTATS_MIB_IN, skb->len);
389 390
390 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { 391 skb = skb_share_check(skb, GFP_ATOMIC);
392 if (!skb) {
391 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS); 393 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_INDISCARDS);
392 goto out; 394 goto out;
393 } 395 }
@@ -450,7 +452,8 @@ int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
450 /* Must drop socket now because of tproxy. */ 452 /* Must drop socket now because of tproxy. */
451 skb_orphan(skb); 453 skb_orphan(skb);
452 454
453 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, dev, NULL, 455 return NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, NULL, skb,
456 dev, NULL,
454 ip_rcv_finish); 457 ip_rcv_finish);
455 458
456csum_error: 459csum_error:
diff --git a/net/ipv4/ip_options.c b/net/ipv4/ip_options.c
index 5b3d91be2db0..bd246792360b 100644
--- a/net/ipv4/ip_options.c
+++ b/net/ipv4/ip_options.c
@@ -264,7 +264,7 @@ int ip_options_compile(struct net *net,
264 unsigned char *iph; 264 unsigned char *iph;
265 int optlen, l; 265 int optlen, l;
266 266
267 if (skb != NULL) { 267 if (skb) {
268 rt = skb_rtable(skb); 268 rt = skb_rtable(skb);
269 optptr = (unsigned char *)&(ip_hdr(skb)[1]); 269 optptr = (unsigned char *)&(ip_hdr(skb)[1]);
270 } else 270 } else
diff --git a/net/ipv4/ip_output.c b/net/ipv4/ip_output.c
index a7aea2048a0d..c65b93a7b711 100644
--- a/net/ipv4/ip_output.c
+++ b/net/ipv4/ip_output.c
@@ -91,14 +91,19 @@ void ip_send_check(struct iphdr *iph)
91} 91}
92EXPORT_SYMBOL(ip_send_check); 92EXPORT_SYMBOL(ip_send_check);
93 93
94int __ip_local_out(struct sk_buff *skb) 94int __ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
95{ 95{
96 struct iphdr *iph = ip_hdr(skb); 96 struct iphdr *iph = ip_hdr(skb);
97 97
98 iph->tot_len = htons(skb->len); 98 iph->tot_len = htons(skb->len);
99 ip_send_check(iph); 99 ip_send_check(iph);
100 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL, 100 return nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, sk, skb, NULL,
101 skb_dst(skb)->dev, dst_output); 101 skb_dst(skb)->dev, dst_output_sk);
102}
103
104int __ip_local_out(struct sk_buff *skb)
105{
106 return __ip_local_out_sk(skb->sk, skb);
102} 107}
103 108
104int ip_local_out_sk(struct sock *sk, struct sk_buff *skb) 109int ip_local_out_sk(struct sock *sk, struct sk_buff *skb)
@@ -148,7 +153,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
148 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr); 153 iph->daddr = (opt && opt->opt.srr ? opt->opt.faddr : daddr);
149 iph->saddr = saddr; 154 iph->saddr = saddr;
150 iph->protocol = sk->sk_protocol; 155 iph->protocol = sk->sk_protocol;
151 ip_select_ident(skb, sk); 156 ip_select_ident(sock_net(sk), skb, sk);
152 157
153 if (opt && opt->opt.optlen) { 158 if (opt && opt->opt.optlen) {
154 iph->ihl += opt->opt.optlen>>2; 159 iph->ihl += opt->opt.optlen>>2;
@@ -163,7 +168,7 @@ int ip_build_and_send_pkt(struct sk_buff *skb, struct sock *sk,
163} 168}
164EXPORT_SYMBOL_GPL(ip_build_and_send_pkt); 169EXPORT_SYMBOL_GPL(ip_build_and_send_pkt);
165 170
166static inline int ip_finish_output2(struct sk_buff *skb) 171static inline int ip_finish_output2(struct sock *sk, struct sk_buff *skb)
167{ 172{
168 struct dst_entry *dst = skb_dst(skb); 173 struct dst_entry *dst = skb_dst(skb);
169 struct rtable *rt = (struct rtable *)dst; 174 struct rtable *rt = (struct rtable *)dst;
@@ -182,7 +187,7 @@ static inline int ip_finish_output2(struct sk_buff *skb)
182 struct sk_buff *skb2; 187 struct sk_buff *skb2;
183 188
184 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); 189 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
185 if (skb2 == NULL) { 190 if (!skb2) {
186 kfree_skb(skb); 191 kfree_skb(skb);
187 return -ENOMEM; 192 return -ENOMEM;
188 } 193 }
@@ -211,7 +216,7 @@ static inline int ip_finish_output2(struct sk_buff *skb)
211 return -EINVAL; 216 return -EINVAL;
212} 217}
213 218
214static int ip_finish_output_gso(struct sk_buff *skb) 219static int ip_finish_output_gso(struct sock *sk, struct sk_buff *skb)
215{ 220{
216 netdev_features_t features; 221 netdev_features_t features;
217 struct sk_buff *segs; 222 struct sk_buff *segs;
@@ -220,7 +225,7 @@ static int ip_finish_output_gso(struct sk_buff *skb)
220 /* common case: locally created skb or seglen is <= mtu */ 225 /* common case: locally created skb or seglen is <= mtu */
221 if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) || 226 if (((IPCB(skb)->flags & IPSKB_FORWARDED) == 0) ||
222 skb_gso_network_seglen(skb) <= ip_skb_dst_mtu(skb)) 227 skb_gso_network_seglen(skb) <= ip_skb_dst_mtu(skb))
223 return ip_finish_output2(skb); 228 return ip_finish_output2(sk, skb);
224 229
225 /* Slowpath - GSO segment length is exceeding the dst MTU. 230 /* Slowpath - GSO segment length is exceeding the dst MTU.
226 * 231 *
@@ -243,7 +248,7 @@ static int ip_finish_output_gso(struct sk_buff *skb)
243 int err; 248 int err;
244 249
245 segs->next = NULL; 250 segs->next = NULL;
246 err = ip_fragment(segs, ip_finish_output2); 251 err = ip_fragment(sk, segs, ip_finish_output2);
247 252
248 if (err && ret == 0) 253 if (err && ret == 0)
249 ret = err; 254 ret = err;
@@ -253,22 +258,22 @@ static int ip_finish_output_gso(struct sk_buff *skb)
253 return ret; 258 return ret;
254} 259}
255 260
256static int ip_finish_output(struct sk_buff *skb) 261static int ip_finish_output(struct sock *sk, struct sk_buff *skb)
257{ 262{
258#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM) 263#if defined(CONFIG_NETFILTER) && defined(CONFIG_XFRM)
259 /* Policy lookup after SNAT yielded a new policy */ 264 /* Policy lookup after SNAT yielded a new policy */
260 if (skb_dst(skb)->xfrm != NULL) { 265 if (skb_dst(skb)->xfrm) {
261 IPCB(skb)->flags |= IPSKB_REROUTED; 266 IPCB(skb)->flags |= IPSKB_REROUTED;
262 return dst_output(skb); 267 return dst_output_sk(sk, skb);
263 } 268 }
264#endif 269#endif
265 if (skb_is_gso(skb)) 270 if (skb_is_gso(skb))
266 return ip_finish_output_gso(skb); 271 return ip_finish_output_gso(sk, skb);
267 272
268 if (skb->len > ip_skb_dst_mtu(skb)) 273 if (skb->len > ip_skb_dst_mtu(skb))
269 return ip_fragment(skb, ip_finish_output2); 274 return ip_fragment(sk, skb, ip_finish_output2);
270 275
271 return ip_finish_output2(skb); 276 return ip_finish_output2(sk, skb);
272} 277}
273 278
274int ip_mc_output(struct sock *sk, struct sk_buff *skb) 279int ip_mc_output(struct sock *sk, struct sk_buff *skb)
@@ -307,7 +312,7 @@ int ip_mc_output(struct sock *sk, struct sk_buff *skb)
307 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 312 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
308 if (newskb) 313 if (newskb)
309 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, 314 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING,
310 newskb, NULL, newskb->dev, 315 sk, newskb, NULL, newskb->dev,
311 dev_loopback_xmit); 316 dev_loopback_xmit);
312 } 317 }
313 318
@@ -322,11 +327,11 @@ int ip_mc_output(struct sock *sk, struct sk_buff *skb)
322 if (rt->rt_flags&RTCF_BROADCAST) { 327 if (rt->rt_flags&RTCF_BROADCAST) {
323 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC); 328 struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);
324 if (newskb) 329 if (newskb)
325 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, newskb, 330 NF_HOOK(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, newskb,
326 NULL, newskb->dev, dev_loopback_xmit); 331 NULL, newskb->dev, dev_loopback_xmit);
327 } 332 }
328 333
329 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, 334 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb, NULL,
330 skb->dev, ip_finish_output, 335 skb->dev, ip_finish_output,
331 !(IPCB(skb)->flags & IPSKB_REROUTED)); 336 !(IPCB(skb)->flags & IPSKB_REROUTED));
332} 337}
@@ -340,7 +345,8 @@ int ip_output(struct sock *sk, struct sk_buff *skb)
340 skb->dev = dev; 345 skb->dev = dev;
341 skb->protocol = htons(ETH_P_IP); 346 skb->protocol = htons(ETH_P_IP);
342 347
343 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, NULL, dev, 348 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
349 NULL, dev,
344 ip_finish_output, 350 ip_finish_output,
345 !(IPCB(skb)->flags & IPSKB_REROUTED)); 351 !(IPCB(skb)->flags & IPSKB_REROUTED));
346} 352}
@@ -376,12 +382,12 @@ int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl)
376 inet_opt = rcu_dereference(inet->inet_opt); 382 inet_opt = rcu_dereference(inet->inet_opt);
377 fl4 = &fl->u.ip4; 383 fl4 = &fl->u.ip4;
378 rt = skb_rtable(skb); 384 rt = skb_rtable(skb);
379 if (rt != NULL) 385 if (rt)
380 goto packet_routed; 386 goto packet_routed;
381 387
382 /* Make sure we can route this packet. */ 388 /* Make sure we can route this packet. */
383 rt = (struct rtable *)__sk_dst_check(sk, 0); 389 rt = (struct rtable *)__sk_dst_check(sk, 0);
384 if (rt == NULL) { 390 if (!rt) {
385 __be32 daddr; 391 __be32 daddr;
386 392
387 /* Use correct destination address if we have options. */ 393 /* Use correct destination address if we have options. */
@@ -430,7 +436,8 @@ packet_routed:
430 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0); 436 ip_options_build(skb, &inet_opt->opt, inet->inet_daddr, rt, 0);
431 } 437 }
432 438
433 ip_select_ident_segs(skb, sk, skb_shinfo(skb)->gso_segs ?: 1); 439 ip_select_ident_segs(sock_net(sk), skb, sk,
440 skb_shinfo(skb)->gso_segs ?: 1);
434 441
435 /* TODO : should we use skb->sk here instead of sk ? */ 442 /* TODO : should we use skb->sk here instead of sk ? */
436 skb->priority = sk->sk_priority; 443 skb->priority = sk->sk_priority;
@@ -448,7 +455,6 @@ no_route:
448} 455}
449EXPORT_SYMBOL(ip_queue_xmit); 456EXPORT_SYMBOL(ip_queue_xmit);
450 457
451
452static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from) 458static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
453{ 459{
454 to->pkt_type = from->pkt_type; 460 to->pkt_type = from->pkt_type;
@@ -479,7 +485,8 @@ static void ip_copy_metadata(struct sk_buff *to, struct sk_buff *from)
479 * single device frame, and queue such a frame for sending. 485 * single device frame, and queue such a frame for sending.
480 */ 486 */
481 487
482int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)) 488int ip_fragment(struct sock *sk, struct sk_buff *skb,
489 int (*output)(struct sock *, struct sk_buff *))
483{ 490{
484 struct iphdr *iph; 491 struct iphdr *iph;
485 int ptr; 492 int ptr;
@@ -586,13 +593,13 @@ int ip_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
586 ip_options_fragment(frag); 593 ip_options_fragment(frag);
587 offset += skb->len - hlen; 594 offset += skb->len - hlen;
588 iph->frag_off = htons(offset>>3); 595 iph->frag_off = htons(offset>>3);
589 if (frag->next != NULL) 596 if (frag->next)
590 iph->frag_off |= htons(IP_MF); 597 iph->frag_off |= htons(IP_MF);
591 /* Ready, complete checksum */ 598 /* Ready, complete checksum */
592 ip_send_check(iph); 599 ip_send_check(iph);
593 } 600 }
594 601
595 err = output(skb); 602 err = output(sk, skb);
596 603
597 if (!err) 604 if (!err)
598 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES); 605 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGCREATES);
@@ -636,10 +643,7 @@ slow_path:
636 left = skb->len - hlen; /* Space per frame */ 643 left = skb->len - hlen; /* Space per frame */
637 ptr = hlen; /* Where to start from */ 644 ptr = hlen; /* Where to start from */
638 645
639 /* for bridged IP traffic encapsulated inside f.e. a vlan header, 646 ll_rs = LL_RESERVED_SPACE(rt->dst.dev);
640 * we need to make room for the encapsulating header
641 */
642 ll_rs = LL_RESERVED_SPACE_EXTRA(rt->dst.dev, nf_bridge_pad(skb));
643 647
644 /* 648 /*
645 * Fragment the datagram. 649 * Fragment the datagram.
@@ -732,7 +736,7 @@ slow_path:
732 736
733 ip_send_check(iph); 737 ip_send_check(iph);
734 738
735 err = output(skb2); 739 err = output(sk, skb2);
736 if (err) 740 if (err)
737 goto fail; 741 goto fail;
738 742
@@ -792,12 +796,13 @@ static inline int ip_ufo_append_data(struct sock *sk,
792 * device, so create one single skb packet containing complete 796 * device, so create one single skb packet containing complete
793 * udp datagram 797 * udp datagram
794 */ 798 */
795 if ((skb = skb_peek_tail(queue)) == NULL) { 799 skb = skb_peek_tail(queue);
800 if (!skb) {
796 skb = sock_alloc_send_skb(sk, 801 skb = sock_alloc_send_skb(sk,
797 hh_len + fragheaderlen + transhdrlen + 20, 802 hh_len + fragheaderlen + transhdrlen + 20,
798 (flags & MSG_DONTWAIT), &err); 803 (flags & MSG_DONTWAIT), &err);
799 804
800 if (skb == NULL) 805 if (!skb)
801 return err; 806 return err;
802 807
803 /* reserve space for Hardware header */ 808 /* reserve space for Hardware header */
@@ -814,7 +819,6 @@ static inline int ip_ufo_append_data(struct sock *sk,
814 819
815 skb->csum = 0; 820 skb->csum = 0;
816 821
817
818 __skb_queue_tail(queue, skb); 822 __skb_queue_tail(queue, skb);
819 } else if (skb_is_gso(skb)) { 823 } else if (skb_is_gso(skb)) {
820 goto append; 824 goto append;
@@ -963,10 +967,10 @@ alloc_new_skb:
963 skb = sock_wmalloc(sk, 967 skb = sock_wmalloc(sk,
964 alloclen + hh_len + 15, 1, 968 alloclen + hh_len + 15, 1,
965 sk->sk_allocation); 969 sk->sk_allocation);
966 if (unlikely(skb == NULL)) 970 if (unlikely(!skb))
967 err = -ENOBUFS; 971 err = -ENOBUFS;
968 } 972 }
969 if (skb == NULL) 973 if (!skb)
970 goto error; 974 goto error;
971 975
972 /* 976 /*
@@ -1090,10 +1094,10 @@ static int ip_setup_cork(struct sock *sk, struct inet_cork *cork,
1090 */ 1094 */
1091 opt = ipc->opt; 1095 opt = ipc->opt;
1092 if (opt) { 1096 if (opt) {
1093 if (cork->opt == NULL) { 1097 if (!cork->opt) {
1094 cork->opt = kmalloc(sizeof(struct ip_options) + 40, 1098 cork->opt = kmalloc(sizeof(struct ip_options) + 40,
1095 sk->sk_allocation); 1099 sk->sk_allocation);
1096 if (unlikely(cork->opt == NULL)) 1100 if (unlikely(!cork->opt))
1097 return -ENOBUFS; 1101 return -ENOBUFS;
1098 } 1102 }
1099 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen); 1103 memcpy(cork->opt, &opt->opt, sizeof(struct ip_options) + opt->opt.optlen);
@@ -1200,7 +1204,8 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1200 return -EMSGSIZE; 1204 return -EMSGSIZE;
1201 } 1205 }
1202 1206
1203 if ((skb = skb_peek_tail(&sk->sk_write_queue)) == NULL) 1207 skb = skb_peek_tail(&sk->sk_write_queue);
1208 if (!skb)
1204 return -EINVAL; 1209 return -EINVAL;
1205 1210
1206 cork->length += size; 1211 cork->length += size;
@@ -1211,7 +1216,6 @@ ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
1211 skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 1216 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1212 } 1217 }
1213 1218
1214
1215 while (size > 0) { 1219 while (size > 0) {
1216 int i; 1220 int i;
1217 1221
@@ -1331,7 +1335,8 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1331 __be16 df = 0; 1335 __be16 df = 0;
1332 __u8 ttl; 1336 __u8 ttl;
1333 1337
1334 if ((skb = __skb_dequeue(queue)) == NULL) 1338 skb = __skb_dequeue(queue);
1339 if (!skb)
1335 goto out; 1340 goto out;
1336 tail_skb = &(skb_shinfo(skb)->frag_list); 1341 tail_skb = &(skb_shinfo(skb)->frag_list);
1337 1342
@@ -1382,7 +1387,7 @@ struct sk_buff *__ip_make_skb(struct sock *sk,
1382 iph->ttl = ttl; 1387 iph->ttl = ttl;
1383 iph->protocol = sk->sk_protocol; 1388 iph->protocol = sk->sk_protocol;
1384 ip_copy_addrs(iph, fl4); 1389 ip_copy_addrs(iph, fl4);
1385 ip_select_ident(skb, sk); 1390 ip_select_ident(net, skb, sk);
1386 1391
1387 if (opt) { 1392 if (opt) {
1388 iph->ihl += opt->optlen>>2; 1393 iph->ihl += opt->optlen>>2;
diff --git a/net/ipv4/ip_sockglue.c b/net/ipv4/ip_sockglue.c
index 5cd99271d3a6..7cfb0893f263 100644
--- a/net/ipv4/ip_sockglue.c
+++ b/net/ipv4/ip_sockglue.c
@@ -351,7 +351,7 @@ int ip_ra_control(struct sock *sk, unsigned char on,
351 return 0; 351 return 0;
352 } 352 }
353 } 353 }
354 if (new_ra == NULL) { 354 if (!new_ra) {
355 spin_unlock_bh(&ip_ra_lock); 355 spin_unlock_bh(&ip_ra_lock);
356 return -ENOBUFS; 356 return -ENOBUFS;
357 } 357 }
@@ -387,7 +387,7 @@ void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
387 skb_network_header(skb); 387 skb_network_header(skb);
388 serr->port = port; 388 serr->port = port;
389 389
390 if (skb_pull(skb, payload - skb->data) != NULL) { 390 if (skb_pull(skb, payload - skb->data)) {
391 skb_reset_transport_header(skb); 391 skb_reset_transport_header(skb);
392 if (sock_queue_err_skb(sk, skb) == 0) 392 if (sock_queue_err_skb(sk, skb) == 0)
393 return; 393 return;
@@ -482,7 +482,7 @@ int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
482 482
483 err = -EAGAIN; 483 err = -EAGAIN;
484 skb = sock_dequeue_err_skb(sk); 484 skb = sock_dequeue_err_skb(sk);
485 if (skb == NULL) 485 if (!skb)
486 goto out; 486 goto out;
487 487
488 copied = skb->len; 488 copied = skb->len;
@@ -536,12 +536,34 @@ out:
536 * Socket option code for IP. This is the end of the line after any 536 * Socket option code for IP. This is the end of the line after any
537 * TCP,UDP etc options on an IP socket. 537 * TCP,UDP etc options on an IP socket.
538 */ 538 */
539static bool setsockopt_needs_rtnl(int optname)
540{
541 switch (optname) {
542 case IP_ADD_MEMBERSHIP:
543 case IP_ADD_SOURCE_MEMBERSHIP:
544 case IP_BLOCK_SOURCE:
545 case IP_DROP_MEMBERSHIP:
546 case IP_DROP_SOURCE_MEMBERSHIP:
547 case IP_MSFILTER:
548 case IP_UNBLOCK_SOURCE:
549 case MCAST_BLOCK_SOURCE:
550 case MCAST_MSFILTER:
551 case MCAST_JOIN_GROUP:
552 case MCAST_JOIN_SOURCE_GROUP:
553 case MCAST_LEAVE_GROUP:
554 case MCAST_LEAVE_SOURCE_GROUP:
555 case MCAST_UNBLOCK_SOURCE:
556 return true;
557 }
558 return false;
559}
539 560
540static int do_ip_setsockopt(struct sock *sk, int level, 561static int do_ip_setsockopt(struct sock *sk, int level,
541 int optname, char __user *optval, unsigned int optlen) 562 int optname, char __user *optval, unsigned int optlen)
542{ 563{
543 struct inet_sock *inet = inet_sk(sk); 564 struct inet_sock *inet = inet_sk(sk);
544 int val = 0, err; 565 int val = 0, err;
566 bool needs_rtnl = setsockopt_needs_rtnl(optname);
545 567
546 switch (optname) { 568 switch (optname) {
547 case IP_PKTINFO: 569 case IP_PKTINFO:
@@ -584,6 +606,8 @@ static int do_ip_setsockopt(struct sock *sk, int level,
584 return ip_mroute_setsockopt(sk, optname, optval, optlen); 606 return ip_mroute_setsockopt(sk, optname, optval, optlen);
585 607
586 err = 0; 608 err = 0;
609 if (needs_rtnl)
610 rtnl_lock();
587 lock_sock(sk); 611 lock_sock(sk);
588 612
589 switch (optname) { 613 switch (optname) {
@@ -1118,10 +1142,14 @@ mc_msf_out:
1118 break; 1142 break;
1119 } 1143 }
1120 release_sock(sk); 1144 release_sock(sk);
1145 if (needs_rtnl)
1146 rtnl_unlock();
1121 return err; 1147 return err;
1122 1148
1123e_inval: 1149e_inval:
1124 release_sock(sk); 1150 release_sock(sk);
1151 if (needs_rtnl)
1152 rtnl_unlock();
1125 return -EINVAL; 1153 return -EINVAL;
1126} 1154}
1127 1155
diff --git a/net/ipv4/ip_tunnel.c b/net/ipv4/ip_tunnel.c
index 2cd08280c77b..4c2c3ba4ba65 100644
--- a/net/ipv4/ip_tunnel.c
+++ b/net/ipv4/ip_tunnel.c
@@ -389,7 +389,6 @@ static int ip_tunnel_bind_dev(struct net_device *dev)
389 hlen = tdev->hard_header_len + tdev->needed_headroom; 389 hlen = tdev->hard_header_len + tdev->needed_headroom;
390 mtu = tdev->mtu; 390 mtu = tdev->mtu;
391 } 391 }
392 dev->iflink = tunnel->parms.link;
393 392
394 dev->needed_headroom = t_hlen + hlen; 393 dev->needed_headroom = t_hlen + hlen;
395 mtu -= (dev->hard_header_len + t_hlen); 394 mtu -= (dev->hard_header_len + t_hlen);
@@ -655,7 +654,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
655 if (dst == 0) { 654 if (dst == 0) {
656 /* NBMA tunnel */ 655 /* NBMA tunnel */
657 656
658 if (skb_dst(skb) == NULL) { 657 if (!skb_dst(skb)) {
659 dev->stats.tx_fifo_errors++; 658 dev->stats.tx_fifo_errors++;
660 goto tx_error; 659 goto tx_error;
661 } 660 }
@@ -673,7 +672,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
673 672
674 neigh = dst_neigh_lookup(skb_dst(skb), 673 neigh = dst_neigh_lookup(skb_dst(skb),
675 &ipv6_hdr(skb)->daddr); 674 &ipv6_hdr(skb)->daddr);
676 if (neigh == NULL) 675 if (!neigh)
677 goto tx_error; 676 goto tx_error;
678 677
679 addr6 = (const struct in6_addr *)&neigh->primary_key; 678 addr6 = (const struct in6_addr *)&neigh->primary_key;
@@ -783,7 +782,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
783 return; 782 return;
784 } 783 }
785 784
786 err = iptunnel_xmit(skb->sk, rt, skb, fl4.saddr, fl4.daddr, protocol, 785 err = iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr, protocol,
787 tos, ttl, df, !net_eq(tunnel->net, dev_net(dev))); 786 tos, ttl, df, !net_eq(tunnel->net, dev_net(dev)));
788 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 787 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
789 788
@@ -844,7 +843,7 @@ int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
844 case SIOCGETTUNNEL: 843 case SIOCGETTUNNEL:
845 if (dev == itn->fb_tunnel_dev) { 844 if (dev == itn->fb_tunnel_dev) {
846 t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type); 845 t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
847 if (t == NULL) 846 if (!t)
848 t = netdev_priv(dev); 847 t = netdev_priv(dev);
849 } 848 }
850 memcpy(p, &t->parms, sizeof(*p)); 849 memcpy(p, &t->parms, sizeof(*p));
@@ -877,7 +876,7 @@ int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
877 break; 876 break;
878 } 877 }
879 if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 878 if (dev != itn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
880 if (t != NULL) { 879 if (t) {
881 if (t->dev != dev) { 880 if (t->dev != dev) {
882 err = -EEXIST; 881 err = -EEXIST;
883 break; 882 break;
@@ -915,7 +914,7 @@ int ip_tunnel_ioctl(struct net_device *dev, struct ip_tunnel_parm *p, int cmd)
915 if (dev == itn->fb_tunnel_dev) { 914 if (dev == itn->fb_tunnel_dev) {
916 err = -ENOENT; 915 err = -ENOENT;
917 t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type); 916 t = ip_tunnel_find(itn, p, itn->fb_tunnel_dev->type);
918 if (t == NULL) 917 if (!t)
919 goto done; 918 goto done;
920 err = -EPERM; 919 err = -EPERM;
921 if (t == netdev_priv(itn->fb_tunnel_dev)) 920 if (t == netdev_priv(itn->fb_tunnel_dev))
@@ -980,6 +979,14 @@ struct net *ip_tunnel_get_link_net(const struct net_device *dev)
980} 979}
981EXPORT_SYMBOL(ip_tunnel_get_link_net); 980EXPORT_SYMBOL(ip_tunnel_get_link_net);
982 981
982int ip_tunnel_get_iflink(const struct net_device *dev)
983{
984 struct ip_tunnel *tunnel = netdev_priv(dev);
985
986 return tunnel->parms.link;
987}
988EXPORT_SYMBOL(ip_tunnel_get_iflink);
989
983int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id, 990int ip_tunnel_init_net(struct net *net, int ip_tnl_net_id,
984 struct rtnl_link_ops *ops, char *devname) 991 struct rtnl_link_ops *ops, char *devname)
985{ 992{
diff --git a/net/ipv4/ip_tunnel_core.c b/net/ipv4/ip_tunnel_core.c
index 88c386cf7d85..ce63ab21b6cd 100644
--- a/net/ipv4/ip_tunnel_core.c
+++ b/net/ipv4/ip_tunnel_core.c
@@ -74,7 +74,8 @@ int iptunnel_xmit(struct sock *sk, struct rtable *rt, struct sk_buff *skb,
74 iph->daddr = dst; 74 iph->daddr = dst;
75 iph->saddr = src; 75 iph->saddr = src;
76 iph->ttl = ttl; 76 iph->ttl = ttl;
77 __ip_select_ident(iph, skb_shinfo(skb)->gso_segs ?: 1); 77 __ip_select_ident(dev_net(rt->dst.dev), iph,
78 skb_shinfo(skb)->gso_segs ?: 1);
78 79
79 err = ip_local_out_sk(sk, skb); 80 err = ip_local_out_sk(sk, skb);
80 if (unlikely(net_xmit_eval(err))) 81 if (unlikely(net_xmit_eval(err)))
diff --git a/net/ipv4/ip_vti.c b/net/ipv4/ip_vti.c
index 94efe148181c..9f7269f3c54a 100644
--- a/net/ipv4/ip_vti.c
+++ b/net/ipv4/ip_vti.c
@@ -60,7 +60,7 @@ static int vti_input(struct sk_buff *skb, int nexthdr, __be32 spi,
60 60
61 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY, 61 tunnel = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
62 iph->saddr, iph->daddr, 0); 62 iph->saddr, iph->daddr, 0);
63 if (tunnel != NULL) { 63 if (tunnel) {
64 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb)) 64 if (!xfrm4_policy_check(NULL, XFRM_POLICY_IN, skb))
65 goto drop; 65 goto drop;
66 66
@@ -341,6 +341,7 @@ static const struct net_device_ops vti_netdev_ops = {
341 .ndo_do_ioctl = vti_tunnel_ioctl, 341 .ndo_do_ioctl = vti_tunnel_ioctl,
342 .ndo_change_mtu = ip_tunnel_change_mtu, 342 .ndo_change_mtu = ip_tunnel_change_mtu,
343 .ndo_get_stats64 = ip_tunnel_get_stats64, 343 .ndo_get_stats64 = ip_tunnel_get_stats64,
344 .ndo_get_iflink = ip_tunnel_get_iflink,
344}; 345};
345 346
346static void vti_tunnel_setup(struct net_device *dev) 347static void vti_tunnel_setup(struct net_device *dev)
@@ -361,7 +362,6 @@ static int vti_tunnel_init(struct net_device *dev)
361 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr); 362 dev->hard_header_len = LL_MAX_HEADER + sizeof(struct iphdr);
362 dev->mtu = ETH_DATA_LEN; 363 dev->mtu = ETH_DATA_LEN;
363 dev->flags = IFF_NOARP; 364 dev->flags = IFF_NOARP;
364 dev->iflink = 0;
365 dev->addr_len = 4; 365 dev->addr_len = 4;
366 dev->features |= NETIF_F_LLTX; 366 dev->features |= NETIF_F_LLTX;
367 netif_keep_dst(dev); 367 netif_keep_dst(dev);
@@ -456,10 +456,10 @@ static void vti_netlink_parms(struct nlattr *data[],
456 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]); 456 parms->o_key = nla_get_be32(data[IFLA_VTI_OKEY]);
457 457
458 if (data[IFLA_VTI_LOCAL]) 458 if (data[IFLA_VTI_LOCAL])
459 parms->iph.saddr = nla_get_be32(data[IFLA_VTI_LOCAL]); 459 parms->iph.saddr = nla_get_in_addr(data[IFLA_VTI_LOCAL]);
460 460
461 if (data[IFLA_VTI_REMOTE]) 461 if (data[IFLA_VTI_REMOTE])
462 parms->iph.daddr = nla_get_be32(data[IFLA_VTI_REMOTE]); 462 parms->iph.daddr = nla_get_in_addr(data[IFLA_VTI_REMOTE]);
463 463
464} 464}
465 465
@@ -505,8 +505,8 @@ static int vti_fill_info(struct sk_buff *skb, const struct net_device *dev)
505 nla_put_u32(skb, IFLA_VTI_LINK, p->link); 505 nla_put_u32(skb, IFLA_VTI_LINK, p->link);
506 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key); 506 nla_put_be32(skb, IFLA_VTI_IKEY, p->i_key);
507 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key); 507 nla_put_be32(skb, IFLA_VTI_OKEY, p->o_key);
508 nla_put_be32(skb, IFLA_VTI_LOCAL, p->iph.saddr); 508 nla_put_in_addr(skb, IFLA_VTI_LOCAL, p->iph.saddr);
509 nla_put_be32(skb, IFLA_VTI_REMOTE, p->iph.daddr); 509 nla_put_in_addr(skb, IFLA_VTI_REMOTE, p->iph.daddr);
510 510
511 return 0; 511 return 0;
512} 512}
diff --git a/net/ipv4/ipcomp.c b/net/ipv4/ipcomp.c
index c0855d50a3fa..d97f4f2787f5 100644
--- a/net/ipv4/ipcomp.c
+++ b/net/ipv4/ipcomp.c
@@ -63,7 +63,7 @@ static struct xfrm_state *ipcomp_tunnel_create(struct xfrm_state *x)
63 struct xfrm_state *t; 63 struct xfrm_state *t;
64 64
65 t = xfrm_state_alloc(net); 65 t = xfrm_state_alloc(net);
66 if (t == NULL) 66 if (!t)
67 goto out; 67 goto out;
68 68
69 t->id.proto = IPPROTO_IPIP; 69 t->id.proto = IPPROTO_IPIP;
diff --git a/net/ipv4/ipconfig.c b/net/ipv4/ipconfig.c
index b26376ef87f6..8e7328c6a390 100644
--- a/net/ipv4/ipconfig.c
+++ b/net/ipv4/ipconfig.c
@@ -504,7 +504,8 @@ ic_rarp_recv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt
504 if (!net_eq(dev_net(dev), &init_net)) 504 if (!net_eq(dev_net(dev), &init_net))
505 goto drop; 505 goto drop;
506 506
507 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 507 skb = skb_share_check(skb, GFP_ATOMIC);
508 if (!skb)
508 return NET_RX_DROP; 509 return NET_RX_DROP;
509 510
510 if (!pskb_may_pull(skb, sizeof(struct arphdr))) 511 if (!pskb_may_pull(skb, sizeof(struct arphdr)))
@@ -958,7 +959,8 @@ static int __init ic_bootp_recv(struct sk_buff *skb, struct net_device *dev, str
958 if (skb->pkt_type == PACKET_OTHERHOST) 959 if (skb->pkt_type == PACKET_OTHERHOST)
959 goto drop; 960 goto drop;
960 961
961 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) 962 skb = skb_share_check(skb, GFP_ATOMIC);
963 if (!skb)
962 return NET_RX_DROP; 964 return NET_RX_DROP;
963 965
964 if (!pskb_may_pull(skb, 966 if (!pskb_may_pull(skb,
diff --git a/net/ipv4/ipip.c b/net/ipv4/ipip.c
index 915d215a7d14..ff96396ebec5 100644
--- a/net/ipv4/ipip.c
+++ b/net/ipv4/ipip.c
@@ -144,7 +144,7 @@ static int ipip_err(struct sk_buff *skb, u32 info)
144 err = -ENOENT; 144 err = -ENOENT;
145 t = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY, 145 t = ip_tunnel_lookup(itn, skb->dev->ifindex, TUNNEL_NO_KEY,
146 iph->daddr, iph->saddr, 0); 146 iph->daddr, iph->saddr, 0);
147 if (t == NULL) 147 if (!t)
148 goto out; 148 goto out;
149 149
150 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { 150 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
@@ -272,6 +272,7 @@ static const struct net_device_ops ipip_netdev_ops = {
272 .ndo_do_ioctl = ipip_tunnel_ioctl, 272 .ndo_do_ioctl = ipip_tunnel_ioctl,
273 .ndo_change_mtu = ip_tunnel_change_mtu, 273 .ndo_change_mtu = ip_tunnel_change_mtu,
274 .ndo_get_stats64 = ip_tunnel_get_stats64, 274 .ndo_get_stats64 = ip_tunnel_get_stats64,
275 .ndo_get_iflink = ip_tunnel_get_iflink,
275}; 276};
276 277
277#define IPIP_FEATURES (NETIF_F_SG | \ 278#define IPIP_FEATURES (NETIF_F_SG | \
@@ -286,7 +287,6 @@ static void ipip_tunnel_setup(struct net_device *dev)
286 287
287 dev->type = ARPHRD_TUNNEL; 288 dev->type = ARPHRD_TUNNEL;
288 dev->flags = IFF_NOARP; 289 dev->flags = IFF_NOARP;
289 dev->iflink = 0;
290 dev->addr_len = 4; 290 dev->addr_len = 4;
291 dev->features |= NETIF_F_LLTX; 291 dev->features |= NETIF_F_LLTX;
292 netif_keep_dst(dev); 292 netif_keep_dst(dev);
@@ -325,10 +325,10 @@ static void ipip_netlink_parms(struct nlattr *data[],
325 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 325 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
326 326
327 if (data[IFLA_IPTUN_LOCAL]) 327 if (data[IFLA_IPTUN_LOCAL])
328 parms->iph.saddr = nla_get_be32(data[IFLA_IPTUN_LOCAL]); 328 parms->iph.saddr = nla_get_in_addr(data[IFLA_IPTUN_LOCAL]);
329 329
330 if (data[IFLA_IPTUN_REMOTE]) 330 if (data[IFLA_IPTUN_REMOTE])
331 parms->iph.daddr = nla_get_be32(data[IFLA_IPTUN_REMOTE]); 331 parms->iph.daddr = nla_get_in_addr(data[IFLA_IPTUN_REMOTE]);
332 332
333 if (data[IFLA_IPTUN_TTL]) { 333 if (data[IFLA_IPTUN_TTL]) {
334 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]); 334 parms->iph.ttl = nla_get_u8(data[IFLA_IPTUN_TTL]);
@@ -450,8 +450,8 @@ static int ipip_fill_info(struct sk_buff *skb, const struct net_device *dev)
450 struct ip_tunnel_parm *parm = &tunnel->parms; 450 struct ip_tunnel_parm *parm = &tunnel->parms;
451 451
452 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 452 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
453 nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) || 453 nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
454 nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) || 454 nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
455 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) || 455 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
456 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) || 456 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
457 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC, 457 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
diff --git a/net/ipv4/ipmr.c b/net/ipv4/ipmr.c
index fe54eba6d00d..3a2c0162c3ba 100644
--- a/net/ipv4/ipmr.c
+++ b/net/ipv4/ipmr.c
@@ -73,9 +73,7 @@
73 73
74struct mr_table { 74struct mr_table {
75 struct list_head list; 75 struct list_head list;
76#ifdef CONFIG_NET_NS 76 possible_net_t net;
77 struct net *net;
78#endif
79 u32 id; 77 u32 id;
80 struct sock __rcu *mroute_sk; 78 struct sock __rcu *mroute_sk;
81 struct timer_list ipmr_expire_timer; 79 struct timer_list ipmr_expire_timer;
@@ -191,7 +189,7 @@ static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
191 } 189 }
192 190
193 mrt = ipmr_get_table(rule->fr_net, rule->table); 191 mrt = ipmr_get_table(rule->fr_net, rule->table);
194 if (mrt == NULL) 192 if (!mrt)
195 return -EAGAIN; 193 return -EAGAIN;
196 res->mrt = mrt; 194 res->mrt = mrt;
197 return 0; 195 return 0;
@@ -255,7 +253,7 @@ static int __net_init ipmr_rules_init(struct net *net)
255 INIT_LIST_HEAD(&net->ipv4.mr_tables); 253 INIT_LIST_HEAD(&net->ipv4.mr_tables);
256 254
257 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 255 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
258 if (mrt == NULL) { 256 if (!mrt) {
259 err = -ENOMEM; 257 err = -ENOMEM;
260 goto err1; 258 goto err1;
261 } 259 }
@@ -323,11 +321,11 @@ static struct mr_table *ipmr_new_table(struct net *net, u32 id)
323 unsigned int i; 321 unsigned int i;
324 322
325 mrt = ipmr_get_table(net, id); 323 mrt = ipmr_get_table(net, id);
326 if (mrt != NULL) 324 if (mrt)
327 return mrt; 325 return mrt;
328 326
329 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 327 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
330 if (mrt == NULL) 328 if (!mrt)
331 return NULL; 329 return NULL;
332 write_pnet(&mrt->net, net); 330 write_pnet(&mrt->net, net);
333 mrt->id = id; 331 mrt->id = id;
@@ -429,7 +427,7 @@ struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
429 dev->flags |= IFF_MULTICAST; 427 dev->flags |= IFF_MULTICAST;
430 428
431 in_dev = __in_dev_get_rtnl(dev); 429 in_dev = __in_dev_get_rtnl(dev);
432 if (in_dev == NULL) 430 if (!in_dev)
433 goto failure; 431 goto failure;
434 432
435 ipv4_devconf_setall(in_dev); 433 ipv4_devconf_setall(in_dev);
@@ -480,8 +478,14 @@ static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
480 return NETDEV_TX_OK; 478 return NETDEV_TX_OK;
481} 479}
482 480
481static int reg_vif_get_iflink(const struct net_device *dev)
482{
483 return 0;
484}
485
483static const struct net_device_ops reg_vif_netdev_ops = { 486static const struct net_device_ops reg_vif_netdev_ops = {
484 .ndo_start_xmit = reg_vif_xmit, 487 .ndo_start_xmit = reg_vif_xmit,
488 .ndo_get_iflink = reg_vif_get_iflink,
485}; 489};
486 490
487static void reg_vif_setup(struct net_device *dev) 491static void reg_vif_setup(struct net_device *dev)
@@ -507,7 +511,7 @@ static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
507 511
508 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); 512 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
509 513
510 if (dev == NULL) 514 if (!dev)
511 return NULL; 515 return NULL;
512 516
513 dev_net_set(dev, net); 517 dev_net_set(dev, net);
@@ -516,7 +520,6 @@ static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
516 free_netdev(dev); 520 free_netdev(dev);
517 return NULL; 521 return NULL;
518 } 522 }
519 dev->iflink = 0;
520 523
521 rcu_read_lock(); 524 rcu_read_lock();
522 in_dev = __in_dev_get_rcu(dev); 525 in_dev = __in_dev_get_rcu(dev);
@@ -764,7 +767,7 @@ static int vif_add(struct net *net, struct mr_table *mrt,
764 case 0: 767 case 0:
765 if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 768 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
766 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 769 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
767 if (dev && __in_dev_get_rtnl(dev) == NULL) { 770 if (dev && !__in_dev_get_rtnl(dev)) {
768 dev_put(dev); 771 dev_put(dev);
769 return -EADDRNOTAVAIL; 772 return -EADDRNOTAVAIL;
770 } 773 }
@@ -808,7 +811,7 @@ static int vif_add(struct net *net, struct mr_table *mrt,
808 v->pkt_out = 0; 811 v->pkt_out = 0;
809 v->link = dev->ifindex; 812 v->link = dev->ifindex;
810 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER)) 813 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
811 v->link = dev->iflink; 814 v->link = dev_get_iflink(dev);
812 815
813 /* And finish update writing critical data */ 816 /* And finish update writing critical data */
814 write_lock_bh(&mrt_lock); 817 write_lock_bh(&mrt_lock);
@@ -1010,7 +1013,7 @@ static int ipmr_cache_report(struct mr_table *mrt,
1010 1013
1011 rcu_read_lock(); 1014 rcu_read_lock();
1012 mroute_sk = rcu_dereference(mrt->mroute_sk); 1015 mroute_sk = rcu_dereference(mrt->mroute_sk);
1013 if (mroute_sk == NULL) { 1016 if (!mroute_sk) {
1014 rcu_read_unlock(); 1017 rcu_read_unlock();
1015 kfree_skb(skb); 1018 kfree_skb(skb);
1016 return -EINVAL; 1019 return -EINVAL;
@@ -1163,7 +1166,7 @@ static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1163 return -EINVAL; 1166 return -EINVAL;
1164 1167
1165 c = ipmr_cache_alloc(); 1168 c = ipmr_cache_alloc();
1166 if (c == NULL) 1169 if (!c)
1167 return -ENOMEM; 1170 return -ENOMEM;
1168 1171
1169 c->mfc_origin = mfc->mfcc_origin.s_addr; 1172 c->mfc_origin = mfc->mfcc_origin.s_addr;
@@ -1285,7 +1288,7 @@ int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsi
1285 return -EOPNOTSUPP; 1288 return -EOPNOTSUPP;
1286 1289
1287 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1290 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1288 if (mrt == NULL) 1291 if (!mrt)
1289 return -ENOENT; 1292 return -ENOENT;
1290 1293
1291 if (optname != MRT_INIT) { 1294 if (optname != MRT_INIT) {
@@ -1448,7 +1451,7 @@ int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int
1448 return -EOPNOTSUPP; 1451 return -EOPNOTSUPP;
1449 1452
1450 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1453 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1451 if (mrt == NULL) 1454 if (!mrt)
1452 return -ENOENT; 1455 return -ENOENT;
1453 1456
1454 if (optname != MRT_VERSION && 1457 if (optname != MRT_VERSION &&
@@ -1494,7 +1497,7 @@ int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1494 struct mr_table *mrt; 1497 struct mr_table *mrt;
1495 1498
1496 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1499 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1497 if (mrt == NULL) 1500 if (!mrt)
1498 return -ENOENT; 1501 return -ENOENT;
1499 1502
1500 switch (cmd) { 1503 switch (cmd) {
@@ -1568,7 +1571,7 @@ int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1568 struct mr_table *mrt; 1571 struct mr_table *mrt;
1569 1572
1570 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1573 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1571 if (mrt == NULL) 1574 if (!mrt)
1572 return -ENOENT; 1575 return -ENOENT;
1573 1576
1574 switch (cmd) { 1577 switch (cmd) {
@@ -1649,7 +1652,8 @@ static struct notifier_block ip_mr_notifier = {
1649 * important for multicast video. 1652 * important for multicast video.
1650 */ 1653 */
1651 1654
1652static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) 1655static void ip_encap(struct net *net, struct sk_buff *skb,
1656 __be32 saddr, __be32 daddr)
1653{ 1657{
1654 struct iphdr *iph; 1658 struct iphdr *iph;
1655 const struct iphdr *old_iph = ip_hdr(skb); 1659 const struct iphdr *old_iph = ip_hdr(skb);
@@ -1668,14 +1672,14 @@ static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1668 iph->protocol = IPPROTO_IPIP; 1672 iph->protocol = IPPROTO_IPIP;
1669 iph->ihl = 5; 1673 iph->ihl = 5;
1670 iph->tot_len = htons(skb->len); 1674 iph->tot_len = htons(skb->len);
1671 ip_select_ident(skb, NULL); 1675 ip_select_ident(net, skb, NULL);
1672 ip_send_check(iph); 1676 ip_send_check(iph);
1673 1677
1674 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1678 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1675 nf_reset(skb); 1679 nf_reset(skb);
1676} 1680}
1677 1681
1678static inline int ipmr_forward_finish(struct sk_buff *skb) 1682static inline int ipmr_forward_finish(struct sock *sk, struct sk_buff *skb)
1679{ 1683{
1680 struct ip_options *opt = &(IPCB(skb)->opt); 1684 struct ip_options *opt = &(IPCB(skb)->opt);
1681 1685
@@ -1685,7 +1689,7 @@ static inline int ipmr_forward_finish(struct sk_buff *skb)
1685 if (unlikely(opt->optlen)) 1689 if (unlikely(opt->optlen))
1686 ip_forward_options(skb); 1690 ip_forward_options(skb);
1687 1691
1688 return dst_output(skb); 1692 return dst_output_sk(sk, skb);
1689} 1693}
1690 1694
1691/* 1695/*
@@ -1702,7 +1706,7 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1702 struct flowi4 fl4; 1706 struct flowi4 fl4;
1703 int encap = 0; 1707 int encap = 0;
1704 1708
1705 if (vif->dev == NULL) 1709 if (!vif->dev)
1706 goto out_free; 1710 goto out_free;
1707 1711
1708#ifdef CONFIG_IP_PIMSM 1712#ifdef CONFIG_IP_PIMSM
@@ -1765,7 +1769,7 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1765 * What do we do with netfilter? -- RR 1769 * What do we do with netfilter? -- RR
1766 */ 1770 */
1767 if (vif->flags & VIFF_TUNNEL) { 1771 if (vif->flags & VIFF_TUNNEL) {
1768 ip_encap(skb, vif->local, vif->remote); 1772 ip_encap(net, skb, vif->local, vif->remote);
1769 /* FIXME: extra output firewall step used to be here. --RR */ 1773 /* FIXME: extra output firewall step used to be here. --RR */
1770 vif->dev->stats.tx_packets++; 1774 vif->dev->stats.tx_packets++;
1771 vif->dev->stats.tx_bytes += skb->len; 1775 vif->dev->stats.tx_bytes += skb->len;
@@ -1784,7 +1788,8 @@ static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1784 * not mrouter) cannot join to more than one interface - it will 1788 * not mrouter) cannot join to more than one interface - it will
1785 * result in receiving multiple packets. 1789 * result in receiving multiple packets.
1786 */ 1790 */
1787 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev, 1791 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, NULL, skb,
1792 skb->dev, dev,
1788 ipmr_forward_finish); 1793 ipmr_forward_finish);
1789 return; 1794 return;
1790 1795
@@ -1993,7 +1998,7 @@ int ip_mr_input(struct sk_buff *skb)
1993 1998
1994 /* already under rcu_read_lock() */ 1999 /* already under rcu_read_lock() */
1995 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 2000 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1996 if (cache == NULL) { 2001 if (!cache) {
1997 int vif = ipmr_find_vif(mrt, skb->dev); 2002 int vif = ipmr_find_vif(mrt, skb->dev);
1998 2003
1999 if (vif >= 0) 2004 if (vif >= 0)
@@ -2004,13 +2009,13 @@ int ip_mr_input(struct sk_buff *skb)
2004 /* 2009 /*
2005 * No usable cache entry 2010 * No usable cache entry
2006 */ 2011 */
2007 if (cache == NULL) { 2012 if (!cache) {
2008 int vif; 2013 int vif;
2009 2014
2010 if (local) { 2015 if (local) {
2011 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2016 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2012 ip_local_deliver(skb); 2017 ip_local_deliver(skb);
2013 if (skb2 == NULL) 2018 if (!skb2)
2014 return -ENOBUFS; 2019 return -ENOBUFS;
2015 skb = skb2; 2020 skb = skb2;
2016 } 2021 }
@@ -2069,7 +2074,7 @@ static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2069 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 2074 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
2070 read_unlock(&mrt_lock); 2075 read_unlock(&mrt_lock);
2071 2076
2072 if (reg_dev == NULL) 2077 if (!reg_dev)
2073 return 1; 2078 return 1;
2074 2079
2075 skb->mac_header = skb->network_header; 2080 skb->mac_header = skb->network_header;
@@ -2199,18 +2204,18 @@ int ipmr_get_route(struct net *net, struct sk_buff *skb,
2199 int err; 2204 int err;
2200 2205
2201 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2206 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2202 if (mrt == NULL) 2207 if (!mrt)
2203 return -ENOENT; 2208 return -ENOENT;
2204 2209
2205 rcu_read_lock(); 2210 rcu_read_lock();
2206 cache = ipmr_cache_find(mrt, saddr, daddr); 2211 cache = ipmr_cache_find(mrt, saddr, daddr);
2207 if (cache == NULL && skb->dev) { 2212 if (!cache && skb->dev) {
2208 int vif = ipmr_find_vif(mrt, skb->dev); 2213 int vif = ipmr_find_vif(mrt, skb->dev);
2209 2214
2210 if (vif >= 0) 2215 if (vif >= 0)
2211 cache = ipmr_cache_find_any(mrt, daddr, vif); 2216 cache = ipmr_cache_find_any(mrt, daddr, vif);
2212 } 2217 }
2213 if (cache == NULL) { 2218 if (!cache) {
2214 struct sk_buff *skb2; 2219 struct sk_buff *skb2;
2215 struct iphdr *iph; 2220 struct iphdr *iph;
2216 struct net_device *dev; 2221 struct net_device *dev;
@@ -2268,7 +2273,7 @@ static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2268 int err; 2273 int err;
2269 2274
2270 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 2275 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2271 if (nlh == NULL) 2276 if (!nlh)
2272 return -EMSGSIZE; 2277 return -EMSGSIZE;
2273 2278
2274 rtm = nlmsg_data(nlh); 2279 rtm = nlmsg_data(nlh);
@@ -2287,8 +2292,8 @@ static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2287 rtm->rtm_protocol = RTPROT_MROUTED; 2292 rtm->rtm_protocol = RTPROT_MROUTED;
2288 rtm->rtm_flags = 0; 2293 rtm->rtm_flags = 0;
2289 2294
2290 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) || 2295 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2291 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp)) 2296 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2292 goto nla_put_failure; 2297 goto nla_put_failure;
2293 err = __ipmr_fill_mroute(mrt, skb, c, rtm); 2298 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2294 /* do not break the dump if cache is unresolved */ 2299 /* do not break the dump if cache is unresolved */
@@ -2333,7 +2338,7 @@ static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2333 2338
2334 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif), 2339 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2335 GFP_ATOMIC); 2340 GFP_ATOMIC);
2336 if (skb == NULL) 2341 if (!skb)
2337 goto errout; 2342 goto errout;
2338 2343
2339 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 2344 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
@@ -2448,7 +2453,7 @@ static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2448 struct mr_table *mrt; 2453 struct mr_table *mrt;
2449 2454
2450 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2455 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2451 if (mrt == NULL) 2456 if (!mrt)
2452 return ERR_PTR(-ENOENT); 2457 return ERR_PTR(-ENOENT);
2453 2458
2454 iter->mrt = mrt; 2459 iter->mrt = mrt;
@@ -2567,7 +2572,7 @@ static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2567 struct mr_table *mrt; 2572 struct mr_table *mrt;
2568 2573
2569 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2574 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2570 if (mrt == NULL) 2575 if (!mrt)
2571 return ERR_PTR(-ENOENT); 2576 return ERR_PTR(-ENOENT);
2572 2577
2573 it->mrt = mrt; 2578 it->mrt = mrt;
diff --git a/net/ipv4/netfilter.c b/net/ipv4/netfilter.c
index 7ebd6e37875c..65de0684e22a 100644
--- a/net/ipv4/netfilter.c
+++ b/net/ipv4/netfilter.c
@@ -94,7 +94,7 @@ static void nf_ip_saveroute(const struct sk_buff *skb,
94{ 94{
95 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); 95 struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
96 96
97 if (entry->hook == NF_INET_LOCAL_OUT) { 97 if (entry->state.hook == NF_INET_LOCAL_OUT) {
98 const struct iphdr *iph = ip_hdr(skb); 98 const struct iphdr *iph = ip_hdr(skb);
99 99
100 rt_info->tos = iph->tos; 100 rt_info->tos = iph->tos;
@@ -109,7 +109,7 @@ static int nf_ip_reroute(struct sk_buff *skb,
109{ 109{
110 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry); 110 const struct ip_rt_info *rt_info = nf_queue_entry_reroute(entry);
111 111
112 if (entry->hook == NF_INET_LOCAL_OUT) { 112 if (entry->state.hook == NF_INET_LOCAL_OUT) {
113 const struct iphdr *iph = ip_hdr(skb); 113 const struct iphdr *iph = ip_hdr(skb);
114 114
115 if (!(iph->tos == rt_info->tos && 115 if (!(iph->tos == rt_info->tos &&
diff --git a/net/ipv4/netfilter/Kconfig b/net/ipv4/netfilter/Kconfig
index 59f883d9cadf..fb20f363151f 100644
--- a/net/ipv4/netfilter/Kconfig
+++ b/net/ipv4/netfilter/Kconfig
@@ -36,24 +36,16 @@ config NF_CONNTRACK_PROC_COMPAT
36 36
37 If unsure, say Y. 37 If unsure, say Y.
38 38
39config NF_LOG_ARP 39if NF_TABLES
40 tristate "ARP packet logging"
41 default m if NETFILTER_ADVANCED=n
42 select NF_LOG_COMMON
43
44config NF_LOG_IPV4
45 tristate "IPv4 packet logging"
46 default m if NETFILTER_ADVANCED=n
47 select NF_LOG_COMMON
48 40
49config NF_TABLES_IPV4 41config NF_TABLES_IPV4
50 depends on NF_TABLES
51 tristate "IPv4 nf_tables support" 42 tristate "IPv4 nf_tables support"
52 help 43 help
53 This option enables the IPv4 support for nf_tables. 44 This option enables the IPv4 support for nf_tables.
54 45
46if NF_TABLES_IPV4
47
55config NFT_CHAIN_ROUTE_IPV4 48config NFT_CHAIN_ROUTE_IPV4
56 depends on NF_TABLES_IPV4
57 tristate "IPv4 nf_tables route chain support" 49 tristate "IPv4 nf_tables route chain support"
58 help 50 help
59 This option enables the "route" chain for IPv4 in nf_tables. This 51 This option enables the "route" chain for IPv4 in nf_tables. This
@@ -61,22 +53,34 @@ config NFT_CHAIN_ROUTE_IPV4
61 fields such as the source, destination, type of service and 53 fields such as the source, destination, type of service and
62 the packet mark. 54 the packet mark.
63 55
64config NF_REJECT_IPV4
65 tristate "IPv4 packet rejection"
66 default m if NETFILTER_ADVANCED=n
67
68config NFT_REJECT_IPV4 56config NFT_REJECT_IPV4
69 depends on NF_TABLES_IPV4
70 select NF_REJECT_IPV4 57 select NF_REJECT_IPV4
71 default NFT_REJECT 58 default NFT_REJECT
72 tristate 59 tristate
73 60
61endif # NF_TABLES_IPV4
62
74config NF_TABLES_ARP 63config NF_TABLES_ARP
75 depends on NF_TABLES
76 tristate "ARP nf_tables support" 64 tristate "ARP nf_tables support"
77 help 65 help
78 This option enables the ARP support for nf_tables. 66 This option enables the ARP support for nf_tables.
79 67
68endif # NF_TABLES
69
70config NF_LOG_ARP
71 tristate "ARP packet logging"
72 default m if NETFILTER_ADVANCED=n
73 select NF_LOG_COMMON
74
75config NF_LOG_IPV4
76 tristate "IPv4 packet logging"
77 default m if NETFILTER_ADVANCED=n
78 select NF_LOG_COMMON
79
80config NF_REJECT_IPV4
81 tristate "IPv4 packet rejection"
82 default m if NETFILTER_ADVANCED=n
83
80config NF_NAT_IPV4 84config NF_NAT_IPV4
81 tristate "IPv4 NAT" 85 tristate "IPv4 NAT"
82 depends on NF_CONNTRACK_IPV4 86 depends on NF_CONNTRACK_IPV4
diff --git a/net/ipv4/netfilter/arp_tables.c b/net/ipv4/netfilter/arp_tables.c
index f95b6f93814b..13bfe84bf3ca 100644
--- a/net/ipv4/netfilter/arp_tables.c
+++ b/net/ipv4/netfilter/arp_tables.c
@@ -248,8 +248,7 @@ struct arpt_entry *arpt_next_entry(const struct arpt_entry *entry)
248 248
249unsigned int arpt_do_table(struct sk_buff *skb, 249unsigned int arpt_do_table(struct sk_buff *skb,
250 unsigned int hook, 250 unsigned int hook,
251 const struct net_device *in, 251 const struct nf_hook_state *state,
252 const struct net_device *out,
253 struct xt_table *table) 252 struct xt_table *table)
254{ 253{
255 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); 254 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
@@ -265,8 +264,8 @@ unsigned int arpt_do_table(struct sk_buff *skb,
265 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev))) 264 if (!pskb_may_pull(skb, arp_hdr_len(skb->dev)))
266 return NF_DROP; 265 return NF_DROP;
267 266
268 indev = in ? in->name : nulldevname; 267 indev = state->in ? state->in->name : nulldevname;
269 outdev = out ? out->name : nulldevname; 268 outdev = state->out ? state->out->name : nulldevname;
270 269
271 local_bh_disable(); 270 local_bh_disable();
272 addend = xt_write_recseq_begin(); 271 addend = xt_write_recseq_begin();
@@ -281,8 +280,8 @@ unsigned int arpt_do_table(struct sk_buff *skb,
281 e = get_entry(table_base, private->hook_entry[hook]); 280 e = get_entry(table_base, private->hook_entry[hook]);
282 back = get_entry(table_base, private->underflow[hook]); 281 back = get_entry(table_base, private->underflow[hook]);
283 282
284 acpar.in = in; 283 acpar.in = state->in;
285 acpar.out = out; 284 acpar.out = state->out;
286 acpar.hooknum = hook; 285 acpar.hooknum = hook;
287 acpar.family = NFPROTO_ARP; 286 acpar.family = NFPROTO_ARP;
288 acpar.hotdrop = false; 287 acpar.hotdrop = false;
diff --git a/net/ipv4/netfilter/arptable_filter.c b/net/ipv4/netfilter/arptable_filter.c
index 802ddecb30b8..93876d03120c 100644
--- a/net/ipv4/netfilter/arptable_filter.c
+++ b/net/ipv4/netfilter/arptable_filter.c
@@ -28,12 +28,11 @@ static const struct xt_table packet_filter = {
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(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
32 int (*okfn)(struct sk_buff *))
33{ 32{
34 const struct net *net = dev_net((in != NULL) ? in : out); 33 const struct net *net = dev_net(state->in ? state->in : state->out);
35 34
36 return arpt_do_table(skb, ops->hooknum, in, out, 35 return arpt_do_table(skb, ops->hooknum, state,
37 net->ipv4.arptable_filter); 36 net->ipv4.arptable_filter);
38} 37}
39 38
diff --git a/net/ipv4/netfilter/ip_tables.c b/net/ipv4/netfilter/ip_tables.c
index cf5e82f39d3b..c69db7fa25ee 100644
--- a/net/ipv4/netfilter/ip_tables.c
+++ b/net/ipv4/netfilter/ip_tables.c
@@ -288,8 +288,7 @@ struct ipt_entry *ipt_next_entry(const struct ipt_entry *entry)
288unsigned int 288unsigned int
289ipt_do_table(struct sk_buff *skb, 289ipt_do_table(struct sk_buff *skb,
290 unsigned int hook, 290 unsigned int hook,
291 const struct net_device *in, 291 const struct nf_hook_state *state,
292 const struct net_device *out,
293 struct xt_table *table) 292 struct xt_table *table)
294{ 293{
295 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); 294 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
@@ -306,8 +305,8 @@ ipt_do_table(struct sk_buff *skb,
306 305
307 /* Initialization */ 306 /* Initialization */
308 ip = ip_hdr(skb); 307 ip = ip_hdr(skb);
309 indev = in ? in->name : nulldevname; 308 indev = state->in ? state->in->name : nulldevname;
310 outdev = out ? out->name : nulldevname; 309 outdev = state->out ? state->out->name : nulldevname;
311 /* We handle fragments by dealing with the first fragment as 310 /* We handle fragments by dealing with the first fragment as
312 * if it was a normal packet. All other fragments are treated 311 * if it was a normal packet. All other fragments are treated
313 * normally, except that they will NEVER match rules that ask 312 * normally, except that they will NEVER match rules that ask
@@ -317,8 +316,8 @@ ipt_do_table(struct sk_buff *skb,
317 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET; 316 acpar.fragoff = ntohs(ip->frag_off) & IP_OFFSET;
318 acpar.thoff = ip_hdrlen(skb); 317 acpar.thoff = ip_hdrlen(skb);
319 acpar.hotdrop = false; 318 acpar.hotdrop = false;
320 acpar.in = in; 319 acpar.in = state->in;
321 acpar.out = out; 320 acpar.out = state->out;
322 acpar.family = NFPROTO_IPV4; 321 acpar.family = NFPROTO_IPV4;
323 acpar.hooknum = hook; 322 acpar.hooknum = hook;
324 323
@@ -370,7 +369,7 @@ ipt_do_table(struct sk_buff *skb,
370#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 369#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
371 /* The packet is traced: log it */ 370 /* The packet is traced: log it */
372 if (unlikely(skb->nf_trace)) 371 if (unlikely(skb->nf_trace))
373 trace_packet(skb, hook, in, out, 372 trace_packet(skb, hook, state->in, state->out,
374 table->name, private, e); 373 table->name, private, e);
375#endif 374#endif
376 /* Standard target? */ 375 /* Standard target? */
diff --git a/net/ipv4/netfilter/ipt_CLUSTERIP.c b/net/ipv4/netfilter/ipt_CLUSTERIP.c
index e90f83a3415b..771ab3d01ad3 100644
--- a/net/ipv4/netfilter/ipt_CLUSTERIP.c
+++ b/net/ipv4/netfilter/ipt_CLUSTERIP.c
@@ -418,6 +418,13 @@ static int clusterip_tg_check(const struct xt_tgchk_param *par)
418 if (ret < 0) 418 if (ret < 0)
419 pr_info("cannot load conntrack support for proto=%u\n", 419 pr_info("cannot load conntrack support for proto=%u\n",
420 par->family); 420 par->family);
421
422 if (!par->net->xt.clusterip_deprecated_warning) {
423 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
424 "use xt_cluster instead\n");
425 par->net->xt.clusterip_deprecated_warning = true;
426 }
427
421 return ret; 428 return ret;
422} 429}
423 430
@@ -497,14 +504,12 @@ static void arp_print(struct arp_payload *payload)
497static unsigned int 504static unsigned int
498arp_mangle(const struct nf_hook_ops *ops, 505arp_mangle(const struct nf_hook_ops *ops,
499 struct sk_buff *skb, 506 struct sk_buff *skb,
500 const struct net_device *in, 507 const struct nf_hook_state *state)
501 const struct net_device *out,
502 int (*okfn)(struct sk_buff *))
503{ 508{
504 struct arphdr *arp = arp_hdr(skb); 509 struct arphdr *arp = arp_hdr(skb);
505 struct arp_payload *payload; 510 struct arp_payload *payload;
506 struct clusterip_config *c; 511 struct clusterip_config *c;
507 struct net *net = dev_net(in ? in : out); 512 struct net *net = dev_net(state->in ? state->in : state->out);
508 513
509 /* we don't care about non-ethernet and non-ipv4 ARP */ 514 /* we don't care about non-ethernet and non-ipv4 ARP */
510 if (arp->ar_hrd != htons(ARPHRD_ETHER) || 515 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
@@ -529,10 +534,10 @@ arp_mangle(const struct nf_hook_ops *ops,
529 * addresses on different interfacs. However, in the CLUSTERIP case 534 * addresses on different interfacs. However, in the CLUSTERIP case
530 * this wouldn't work, since we didn't subscribe the mcast group on 535 * this wouldn't work, since we didn't subscribe the mcast group on
531 * other interfaces */ 536 * other interfaces */
532 if (c->dev != out) { 537 if (c->dev != state->out) {
533 pr_debug("not mangling arp reply on different " 538 pr_debug("not mangling arp reply on different "
534 "interface: cip'%s'-skb'%s'\n", 539 "interface: cip'%s'-skb'%s'\n",
535 c->dev->name, out->name); 540 c->dev->name, state->out->name);
536 clusterip_config_put(c); 541 clusterip_config_put(c);
537 return NF_ACCEPT; 542 return NF_ACCEPT;
538 } 543 }
diff --git a/net/ipv4/netfilter/ipt_REJECT.c b/net/ipv4/netfilter/ipt_REJECT.c
index 8f48f5517e33..87907d4bd259 100644
--- a/net/ipv4/netfilter/ipt_REJECT.c
+++ b/net/ipv4/netfilter/ipt_REJECT.c
@@ -34,31 +34,32 @@ static unsigned int
34reject_tg(struct sk_buff *skb, const struct xt_action_param *par) 34reject_tg(struct sk_buff *skb, const struct xt_action_param *par)
35{ 35{
36 const struct ipt_reject_info *reject = par->targinfo; 36 const struct ipt_reject_info *reject = par->targinfo;
37 int hook = par->hooknum;
37 38
38 switch (reject->with) { 39 switch (reject->with) {
39 case IPT_ICMP_NET_UNREACHABLE: 40 case IPT_ICMP_NET_UNREACHABLE:
40 nf_send_unreach(skb, ICMP_NET_UNREACH); 41 nf_send_unreach(skb, ICMP_NET_UNREACH, hook);
41 break; 42 break;
42 case IPT_ICMP_HOST_UNREACHABLE: 43 case IPT_ICMP_HOST_UNREACHABLE:
43 nf_send_unreach(skb, ICMP_HOST_UNREACH); 44 nf_send_unreach(skb, ICMP_HOST_UNREACH, hook);
44 break; 45 break;
45 case IPT_ICMP_PROT_UNREACHABLE: 46 case IPT_ICMP_PROT_UNREACHABLE:
46 nf_send_unreach(skb, ICMP_PROT_UNREACH); 47 nf_send_unreach(skb, ICMP_PROT_UNREACH, hook);
47 break; 48 break;
48 case IPT_ICMP_PORT_UNREACHABLE: 49 case IPT_ICMP_PORT_UNREACHABLE:
49 nf_send_unreach(skb, ICMP_PORT_UNREACH); 50 nf_send_unreach(skb, ICMP_PORT_UNREACH, hook);
50 break; 51 break;
51 case IPT_ICMP_NET_PROHIBITED: 52 case IPT_ICMP_NET_PROHIBITED:
52 nf_send_unreach(skb, ICMP_NET_ANO); 53 nf_send_unreach(skb, ICMP_NET_ANO, hook);
53 break; 54 break;
54 case IPT_ICMP_HOST_PROHIBITED: 55 case IPT_ICMP_HOST_PROHIBITED:
55 nf_send_unreach(skb, ICMP_HOST_ANO); 56 nf_send_unreach(skb, ICMP_HOST_ANO, hook);
56 break; 57 break;
57 case IPT_ICMP_ADMIN_PROHIBITED: 58 case IPT_ICMP_ADMIN_PROHIBITED:
58 nf_send_unreach(skb, ICMP_PKT_FILTERED); 59 nf_send_unreach(skb, ICMP_PKT_FILTERED, hook);
59 break; 60 break;
60 case IPT_TCP_RESET: 61 case IPT_TCP_RESET:
61 nf_send_reset(skb, par->hooknum); 62 nf_send_reset(skb, hook);
62 case IPT_ICMP_ECHOREPLY: 63 case IPT_ICMP_ECHOREPLY:
63 /* Doesn't happen. */ 64 /* Doesn't happen. */
64 break; 65 break;
diff --git a/net/ipv4/netfilter/ipt_SYNPROXY.c b/net/ipv4/netfilter/ipt_SYNPROXY.c
index a313c3fbeb46..e9e67793055f 100644
--- a/net/ipv4/netfilter/ipt_SYNPROXY.c
+++ b/net/ipv4/netfilter/ipt_SYNPROXY.c
@@ -300,11 +300,9 @@ synproxy_tg4(struct sk_buff *skb, const struct xt_action_param *par)
300 300
301static unsigned int ipv4_synproxy_hook(const struct nf_hook_ops *ops, 301static unsigned int ipv4_synproxy_hook(const struct nf_hook_ops *ops,
302 struct sk_buff *skb, 302 struct sk_buff *skb,
303 const struct net_device *in, 303 const struct nf_hook_state *nhs)
304 const struct net_device *out,
305 int (*okfn)(struct sk_buff *))
306{ 304{
307 struct synproxy_net *snet = synproxy_pernet(dev_net(in ? : out)); 305 struct synproxy_net *snet = synproxy_pernet(dev_net(nhs->in ? : nhs->out));
308 enum ip_conntrack_info ctinfo; 306 enum ip_conntrack_info ctinfo;
309 struct nf_conn *ct; 307 struct nf_conn *ct;
310 struct nf_conn_synproxy *synproxy; 308 struct nf_conn_synproxy *synproxy;
diff --git a/net/ipv4/netfilter/iptable_filter.c b/net/ipv4/netfilter/iptable_filter.c
index e08a74a243a8..a0f3beca52d2 100644
--- a/net/ipv4/netfilter/iptable_filter.c
+++ b/net/ipv4/netfilter/iptable_filter.c
@@ -34,8 +34,7 @@ static const struct xt_table packet_filter = {
34 34
35static unsigned int 35static unsigned int
36iptable_filter_hook(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
38 int (*okfn)(struct sk_buff *))
39{ 38{
40 const struct net *net; 39 const struct net *net;
41 40
@@ -45,9 +44,8 @@ iptable_filter_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
45 /* root is playing with raw sockets. */ 44 /* root is playing with raw sockets. */
46 return NF_ACCEPT; 45 return NF_ACCEPT;
47 46
48 net = dev_net((in != NULL) ? in : out); 47 net = dev_net(state->in ? state->in : state->out);
49 return ipt_do_table(skb, ops->hooknum, in, out, 48 return ipt_do_table(skb, ops->hooknum, state, net->ipv4.iptable_filter);
50 net->ipv4.iptable_filter);
51} 49}
52 50
53static struct nf_hook_ops *filter_ops __read_mostly; 51static 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 6a5079c34bb3..62cbb8c5f4a8 100644
--- a/net/ipv4/netfilter/iptable_mangle.c
+++ b/net/ipv4/netfilter/iptable_mangle.c
@@ -37,8 +37,9 @@ static const struct xt_table packet_mangler = {
37}; 37};
38 38
39static unsigned int 39static unsigned int
40ipt_mangle_out(struct sk_buff *skb, const struct net_device *out) 40ipt_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
41{ 41{
42 struct net_device *out = state->out;
42 unsigned int ret; 43 unsigned int ret;
43 const struct iphdr *iph; 44 const struct iphdr *iph;
44 u_int8_t tos; 45 u_int8_t tos;
@@ -58,7 +59,7 @@ ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
58 daddr = iph->daddr; 59 daddr = iph->daddr;
59 tos = iph->tos; 60 tos = iph->tos;
60 61
61 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, NULL, out, 62 ret = ipt_do_table(skb, NF_INET_LOCAL_OUT, state,
62 dev_net(out)->ipv4.iptable_mangle); 63 dev_net(out)->ipv4.iptable_mangle);
63 /* Reroute for ANY change. */ 64 /* Reroute for ANY change. */
64 if (ret != NF_DROP && ret != NF_STOLEN) { 65 if (ret != NF_DROP && ret != NF_STOLEN) {
@@ -81,18 +82,16 @@ ipt_mangle_out(struct sk_buff *skb, const struct net_device *out)
81static unsigned int 82static unsigned int
82iptable_mangle_hook(const struct nf_hook_ops *ops, 83iptable_mangle_hook(const struct nf_hook_ops *ops,
83 struct sk_buff *skb, 84 struct sk_buff *skb,
84 const struct net_device *in, 85 const struct nf_hook_state *state)
85 const struct net_device *out,
86 int (*okfn)(struct sk_buff *))
87{ 86{
88 if (ops->hooknum == NF_INET_LOCAL_OUT) 87 if (ops->hooknum == NF_INET_LOCAL_OUT)
89 return ipt_mangle_out(skb, out); 88 return ipt_mangle_out(skb, state);
90 if (ops->hooknum == NF_INET_POST_ROUTING) 89 if (ops->hooknum == NF_INET_POST_ROUTING)
91 return ipt_do_table(skb, ops->hooknum, in, out, 90 return ipt_do_table(skb, ops->hooknum, state,
92 dev_net(out)->ipv4.iptable_mangle); 91 dev_net(state->out)->ipv4.iptable_mangle);
93 /* PREROUTING/INPUT/FORWARD: */ 92 /* PREROUTING/INPUT/FORWARD: */
94 return ipt_do_table(skb, ops->hooknum, in, out, 93 return ipt_do_table(skb, ops->hooknum, state,
95 dev_net(in)->ipv4.iptable_mangle); 94 dev_net(state->in)->ipv4.iptable_mangle);
96} 95}
97 96
98static struct nf_hook_ops *mangle_ops __read_mostly; 97static struct nf_hook_ops *mangle_ops __read_mostly;
diff --git a/net/ipv4/netfilter/iptable_nat.c b/net/ipv4/netfilter/iptable_nat.c
index 6b67d7e9a75d..0d4d9cdf98a4 100644
--- a/net/ipv4/netfilter/iptable_nat.c
+++ b/net/ipv4/netfilter/iptable_nat.c
@@ -30,49 +30,40 @@ static const struct xt_table nf_nat_ipv4_table = {
30 30
31static unsigned int iptable_nat_do_chain(const struct nf_hook_ops *ops, 31static unsigned int iptable_nat_do_chain(const struct nf_hook_ops *ops,
32 struct sk_buff *skb, 32 struct sk_buff *skb,
33 const struct net_device *in, 33 const struct nf_hook_state *state,
34 const struct net_device *out,
35 struct nf_conn *ct) 34 struct nf_conn *ct)
36{ 35{
37 struct net *net = nf_ct_net(ct); 36 struct net *net = nf_ct_net(ct);
38 37
39 return ipt_do_table(skb, ops->hooknum, in, out, net->ipv4.nat_table); 38 return ipt_do_table(skb, ops->hooknum, state, net->ipv4.nat_table);
40} 39}
41 40
42static unsigned int iptable_nat_ipv4_fn(const struct nf_hook_ops *ops, 41static unsigned int iptable_nat_ipv4_fn(const struct nf_hook_ops *ops,
43 struct sk_buff *skb, 42 struct sk_buff *skb,
44 const struct net_device *in, 43 const struct nf_hook_state *state)
45 const struct net_device *out,
46 int (*okfn)(struct sk_buff *))
47{ 44{
48 return nf_nat_ipv4_fn(ops, skb, in, out, iptable_nat_do_chain); 45 return nf_nat_ipv4_fn(ops, skb, state, iptable_nat_do_chain);
49} 46}
50 47
51static unsigned int iptable_nat_ipv4_in(const struct nf_hook_ops *ops, 48static unsigned int iptable_nat_ipv4_in(const struct nf_hook_ops *ops,
52 struct sk_buff *skb, 49 struct sk_buff *skb,
53 const struct net_device *in, 50 const struct nf_hook_state *state)
54 const struct net_device *out,
55 int (*okfn)(struct sk_buff *))
56{ 51{
57 return nf_nat_ipv4_in(ops, skb, in, out, iptable_nat_do_chain); 52 return nf_nat_ipv4_in(ops, skb, state, iptable_nat_do_chain);
58} 53}
59 54
60static unsigned int iptable_nat_ipv4_out(const struct nf_hook_ops *ops, 55static unsigned int iptable_nat_ipv4_out(const struct nf_hook_ops *ops,
61 struct sk_buff *skb, 56 struct sk_buff *skb,
62 const struct net_device *in, 57 const struct nf_hook_state *state)
63 const struct net_device *out,
64 int (*okfn)(struct sk_buff *))
65{ 58{
66 return nf_nat_ipv4_out(ops, skb, in, out, iptable_nat_do_chain); 59 return nf_nat_ipv4_out(ops, skb, state, iptable_nat_do_chain);
67} 60}
68 61
69static unsigned int iptable_nat_ipv4_local_fn(const struct nf_hook_ops *ops, 62static unsigned int iptable_nat_ipv4_local_fn(const struct nf_hook_ops *ops,
70 struct sk_buff *skb, 63 struct sk_buff *skb,
71 const struct net_device *in, 64 const struct nf_hook_state *state)
72 const struct net_device *out,
73 int (*okfn)(struct sk_buff *))
74{ 65{
75 return nf_nat_ipv4_local_fn(ops, skb, in, out, iptable_nat_do_chain); 66 return nf_nat_ipv4_local_fn(ops, skb, state, iptable_nat_do_chain);
76} 67}
77 68
78static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = { 69static struct nf_hook_ops nf_nat_ipv4_ops[] __read_mostly = {
diff --git a/net/ipv4/netfilter/iptable_raw.c b/net/ipv4/netfilter/iptable_raw.c
index b2f7e8f98316..0356e6da4bb7 100644
--- a/net/ipv4/netfilter/iptable_raw.c
+++ b/net/ipv4/netfilter/iptable_raw.c
@@ -21,8 +21,7 @@ static const struct xt_table packet_raw = {
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(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
25 int (*okfn)(struct sk_buff *))
26{ 25{
27 const struct net *net; 26 const struct net *net;
28 27
@@ -32,8 +31,8 @@ iptable_raw_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
32 /* root is playing with raw sockets. */ 31 /* root is playing with raw sockets. */
33 return NF_ACCEPT; 32 return NF_ACCEPT;
34 33
35 net = dev_net((in != NULL) ? in : out); 34 net = dev_net(state->in ? state->in : state->out);
36 return ipt_do_table(skb, ops->hooknum, in, out, net->ipv4.iptable_raw); 35 return ipt_do_table(skb, ops->hooknum, state, net->ipv4.iptable_raw);
37} 36}
38 37
39static struct nf_hook_ops *rawtable_ops __read_mostly; 38static 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 c86647ed2078..4bce3980ccd9 100644
--- a/net/ipv4/netfilter/iptable_security.c
+++ b/net/ipv4/netfilter/iptable_security.c
@@ -38,9 +38,7 @@ static const struct xt_table security_table = {
38 38
39static unsigned int 39static unsigned int
40iptable_security_hook(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
42 const struct net_device *out,
43 int (*okfn)(struct sk_buff *))
44{ 42{
45 const struct net *net; 43 const struct net *net;
46 44
@@ -50,8 +48,8 @@ iptable_security_hook(const struct nf_hook_ops *ops, struct sk_buff *skb,
50 /* Somebody is playing with raw sockets. */ 48 /* Somebody is playing with raw sockets. */
51 return NF_ACCEPT; 49 return NF_ACCEPT;
52 50
53 net = dev_net((in != NULL) ? in : out); 51 net = dev_net(state->in ? state->in : state->out);
54 return ipt_do_table(skb, ops->hooknum, in, out, 52 return ipt_do_table(skb, ops->hooknum, state,
55 net->ipv4.iptable_security); 53 net->ipv4.iptable_security);
56} 54}
57 55
diff --git a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
index 5c61328b7704..30ad9554b5e9 100644
--- a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
+++ b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4.c
@@ -94,9 +94,7 @@ static int ipv4_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
94 94
95static unsigned int ipv4_helper(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
98 const struct net_device *out,
99 int (*okfn)(struct sk_buff *))
100{ 98{
101 struct nf_conn *ct; 99 struct nf_conn *ct;
102 enum ip_conntrack_info ctinfo; 100 enum ip_conntrack_info ctinfo;
@@ -123,9 +121,7 @@ static unsigned int ipv4_helper(const struct nf_hook_ops *ops,
123 121
124static unsigned int ipv4_confirm(const struct nf_hook_ops *ops, 122static unsigned int ipv4_confirm(const struct nf_hook_ops *ops,
125 struct sk_buff *skb, 123 struct sk_buff *skb,
126 const struct net_device *in, 124 const struct nf_hook_state *state)
127 const struct net_device *out,
128 int (*okfn)(struct sk_buff *))
129{ 125{
130 struct nf_conn *ct; 126 struct nf_conn *ct;
131 enum ip_conntrack_info ctinfo; 127 enum ip_conntrack_info ctinfo;
@@ -149,24 +145,20 @@ out:
149 145
150static unsigned int ipv4_conntrack_in(const struct nf_hook_ops *ops, 146static unsigned int ipv4_conntrack_in(const struct nf_hook_ops *ops,
151 struct sk_buff *skb, 147 struct sk_buff *skb,
152 const struct net_device *in, 148 const struct nf_hook_state *state)
153 const struct net_device *out,
154 int (*okfn)(struct sk_buff *))
155{ 149{
156 return nf_conntrack_in(dev_net(in), PF_INET, ops->hooknum, skb); 150 return nf_conntrack_in(dev_net(state->in), PF_INET, ops->hooknum, skb);
157} 151}
158 152
159static unsigned int ipv4_conntrack_local(const struct nf_hook_ops *ops, 153static unsigned int ipv4_conntrack_local(const struct nf_hook_ops *ops,
160 struct sk_buff *skb, 154 struct sk_buff *skb,
161 const struct net_device *in, 155 const struct nf_hook_state *state)
162 const struct net_device *out,
163 int (*okfn)(struct sk_buff *))
164{ 156{
165 /* root is playing with raw sockets. */ 157 /* root is playing with raw sockets. */
166 if (skb->len < sizeof(struct iphdr) || 158 if (skb->len < sizeof(struct iphdr) ||
167 ip_hdrlen(skb) < sizeof(struct iphdr)) 159 ip_hdrlen(skb) < sizeof(struct iphdr))
168 return NF_ACCEPT; 160 return NF_ACCEPT;
169 return nf_conntrack_in(dev_net(out), PF_INET, ops->hooknum, skb); 161 return nf_conntrack_in(dev_net(state->out), PF_INET, ops->hooknum, skb);
170} 162}
171 163
172/* Connection tracking may drop packets, but never alters them, so 164/* Connection tracking may drop packets, but never alters them, so
@@ -322,8 +314,8 @@ getorigdst(struct sock *sk, int optval, void __user *user, int *len)
322static int ipv4_tuple_to_nlattr(struct sk_buff *skb, 314static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
323 const struct nf_conntrack_tuple *tuple) 315 const struct nf_conntrack_tuple *tuple)
324{ 316{
325 if (nla_put_be32(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) || 317 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
326 nla_put_be32(skb, CTA_IP_V4_DST, tuple->dst.u3.ip)) 318 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
327 goto nla_put_failure; 319 goto nla_put_failure;
328 return 0; 320 return 0;
329 321
@@ -342,8 +334,8 @@ static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
342 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST]) 334 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
343 return -EINVAL; 335 return -EINVAL;
344 336
345 t->src.u3.ip = nla_get_be32(tb[CTA_IP_V4_SRC]); 337 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
346 t->dst.u3.ip = nla_get_be32(tb[CTA_IP_V4_DST]); 338 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
347 339
348 return 0; 340 return 0;
349} 341}
diff --git a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c
index a460a87e14f8..f0dfe92a00d6 100644
--- a/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c
+++ b/net/ipv4/netfilter/nf_conntrack_l3proto_ipv4_compat.c
@@ -300,7 +300,9 @@ static int exp_seq_show(struct seq_file *s, void *v)
300 __nf_ct_l3proto_find(exp->tuple.src.l3num), 300 __nf_ct_l3proto_find(exp->tuple.src.l3num),
301 __nf_ct_l4proto_find(exp->tuple.src.l3num, 301 __nf_ct_l4proto_find(exp->tuple.src.l3num,
302 exp->tuple.dst.protonum)); 302 exp->tuple.dst.protonum));
303 return seq_putc(s, '\n'); 303 seq_putc(s, '\n');
304
305 return 0;
304} 306}
305 307
306static const struct seq_operations exp_seq_ops = { 308static const struct seq_operations exp_seq_ops = {
diff --git a/net/ipv4/netfilter/nf_defrag_ipv4.c b/net/ipv4/netfilter/nf_defrag_ipv4.c
index 7e5ca6f2d0cd..c88b7d434718 100644
--- a/net/ipv4/netfilter/nf_defrag_ipv4.c
+++ b/net/ipv4/netfilter/nf_defrag_ipv4.c
@@ -63,9 +63,7 @@ static enum ip_defrag_users nf_ct_defrag_user(unsigned int hooknum,
63 63
64static unsigned int ipv4_conntrack_defrag(const struct nf_hook_ops *ops, 64static unsigned int ipv4_conntrack_defrag(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 nf_hook_state *state)
67 const struct net_device *out,
68 int (*okfn)(struct sk_buff *))
69{ 67{
70 struct sock *sk = skb->sk; 68 struct sock *sk = skb->sk;
71 struct inet_sock *inet = inet_sk(skb->sk); 69 struct inet_sock *inet = inet_sk(skb->sk);
diff --git a/net/ipv4/netfilter/nf_log_arp.c b/net/ipv4/netfilter/nf_log_arp.c
index d059182c1466..e7ad950cf9ef 100644
--- a/net/ipv4/netfilter/nf_log_arp.c
+++ b/net/ipv4/netfilter/nf_log_arp.c
@@ -10,8 +10,10 @@
10 * it under the terms of the GNU General Public License version 2 as 10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation. 11 * published by the Free Software Foundation.
12 */ 12 */
13
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 15
16#include <linux/kernel.h>
15#include <linux/module.h> 17#include <linux/module.h>
16#include <linux/spinlock.h> 18#include <linux/spinlock.h>
17#include <linux/skbuff.h> 19#include <linux/skbuff.h>
@@ -27,7 +29,7 @@ static struct nf_loginfo default_loginfo = {
27 .type = NF_LOG_TYPE_LOG, 29 .type = NF_LOG_TYPE_LOG,
28 .u = { 30 .u = {
29 .log = { 31 .log = {
30 .level = 5, 32 .level = LOGLEVEL_NOTICE,
31 .logflags = NF_LOG_MASK, 33 .logflags = NF_LOG_MASK,
32 }, 34 },
33 }, 35 },
diff --git a/net/ipv4/netfilter/nf_log_ipv4.c b/net/ipv4/netfilter/nf_log_ipv4.c
index 75101980eeee..076aadda0473 100644
--- a/net/ipv4/netfilter/nf_log_ipv4.c
+++ b/net/ipv4/netfilter/nf_log_ipv4.c
@@ -5,8 +5,10 @@
5 * it under the terms of the GNU General Public License version 2 as 5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation. 6 * published by the Free Software Foundation.
7 */ 7 */
8
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 10
11#include <linux/kernel.h>
10#include <linux/module.h> 12#include <linux/module.h>
11#include <linux/spinlock.h> 13#include <linux/spinlock.h>
12#include <linux/skbuff.h> 14#include <linux/skbuff.h>
@@ -26,7 +28,7 @@ static struct nf_loginfo default_loginfo = {
26 .type = NF_LOG_TYPE_LOG, 28 .type = NF_LOG_TYPE_LOG,
27 .u = { 29 .u = {
28 .log = { 30 .log = {
29 .level = 5, 31 .level = LOGLEVEL_NOTICE,
30 .logflags = NF_LOG_MASK, 32 .logflags = NF_LOG_MASK,
31 }, 33 },
32 }, 34 },
diff --git a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
index fc37711e11f3..e59cc05c09e9 100644
--- a/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
+++ b/net/ipv4/netfilter/nf_nat_l3proto_ipv4.c
@@ -256,11 +256,10 @@ EXPORT_SYMBOL_GPL(nf_nat_icmp_reply_translation);
256 256
257unsigned int 257unsigned int
258nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, 258nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
259 const struct net_device *in, const struct net_device *out, 259 const struct nf_hook_state *state,
260 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 260 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
261 struct sk_buff *skb, 261 struct sk_buff *skb,
262 const struct net_device *in, 262 const struct nf_hook_state *state,
263 const struct net_device *out,
264 struct nf_conn *ct)) 263 struct nf_conn *ct))
265{ 264{
266 struct nf_conn *ct; 265 struct nf_conn *ct;
@@ -309,7 +308,7 @@ nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
309 if (!nf_nat_initialized(ct, maniptype)) { 308 if (!nf_nat_initialized(ct, maniptype)) {
310 unsigned int ret; 309 unsigned int ret;
311 310
312 ret = do_chain(ops, skb, in, out, ct); 311 ret = do_chain(ops, skb, state, ct);
313 if (ret != NF_ACCEPT) 312 if (ret != NF_ACCEPT)
314 return ret; 313 return ret;
315 314
@@ -323,7 +322,8 @@ nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
323 pr_debug("Already setup manip %s for ct %p\n", 322 pr_debug("Already setup manip %s for ct %p\n",
324 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST", 323 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
325 ct); 324 ct);
326 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) 325 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat,
326 state->out))
327 goto oif_changed; 327 goto oif_changed;
328 } 328 }
329 break; 329 break;
@@ -332,7 +332,7 @@ nf_nat_ipv4_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
332 /* ESTABLISHED */ 332 /* ESTABLISHED */
333 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || 333 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
334 ctinfo == IP_CT_ESTABLISHED_REPLY); 334 ctinfo == IP_CT_ESTABLISHED_REPLY);
335 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) 335 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, state->out))
336 goto oif_changed; 336 goto oif_changed;
337 } 337 }
338 338
@@ -346,17 +346,16 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv4_fn);
346 346
347unsigned int 347unsigned int
348nf_nat_ipv4_in(const struct nf_hook_ops *ops, struct sk_buff *skb, 348nf_nat_ipv4_in(const struct nf_hook_ops *ops, struct sk_buff *skb,
349 const struct net_device *in, const struct net_device *out, 349 const struct nf_hook_state *state,
350 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 350 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
351 struct sk_buff *skb, 351 struct sk_buff *skb,
352 const struct net_device *in, 352 const struct nf_hook_state *state,
353 const struct net_device *out,
354 struct nf_conn *ct)) 353 struct nf_conn *ct))
355{ 354{
356 unsigned int ret; 355 unsigned int ret;
357 __be32 daddr = ip_hdr(skb)->daddr; 356 __be32 daddr = ip_hdr(skb)->daddr;
358 357
359 ret = nf_nat_ipv4_fn(ops, skb, in, out, do_chain); 358 ret = nf_nat_ipv4_fn(ops, skb, state, do_chain);
360 if (ret != NF_DROP && ret != NF_STOLEN && 359 if (ret != NF_DROP && ret != NF_STOLEN &&
361 daddr != ip_hdr(skb)->daddr) 360 daddr != ip_hdr(skb)->daddr)
362 skb_dst_drop(skb); 361 skb_dst_drop(skb);
@@ -367,11 +366,10 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv4_in);
367 366
368unsigned int 367unsigned int
369nf_nat_ipv4_out(const struct nf_hook_ops *ops, struct sk_buff *skb, 368nf_nat_ipv4_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
370 const struct net_device *in, const struct net_device *out, 369 const struct nf_hook_state *state,
371 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 370 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
372 struct sk_buff *skb, 371 struct sk_buff *skb,
373 const struct net_device *in, 372 const struct nf_hook_state *state,
374 const struct net_device *out,
375 struct nf_conn *ct)) 373 struct nf_conn *ct))
376{ 374{
377#ifdef CONFIG_XFRM 375#ifdef CONFIG_XFRM
@@ -386,7 +384,7 @@ nf_nat_ipv4_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
386 ip_hdrlen(skb) < sizeof(struct iphdr)) 384 ip_hdrlen(skb) < sizeof(struct iphdr))
387 return NF_ACCEPT; 385 return NF_ACCEPT;
388 386
389 ret = nf_nat_ipv4_fn(ops, skb, in, out, do_chain); 387 ret = nf_nat_ipv4_fn(ops, skb, state, do_chain);
390#ifdef CONFIG_XFRM 388#ifdef CONFIG_XFRM
391 if (ret != NF_DROP && ret != NF_STOLEN && 389 if (ret != NF_DROP && ret != NF_STOLEN &&
392 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) && 390 !(IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) &&
@@ -410,11 +408,10 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv4_out);
410 408
411unsigned int 409unsigned int
412nf_nat_ipv4_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, 410nf_nat_ipv4_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
413 const struct net_device *in, const struct net_device *out, 411 const struct nf_hook_state *state,
414 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 412 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
415 struct sk_buff *skb, 413 struct sk_buff *skb,
416 const struct net_device *in, 414 const struct nf_hook_state *state,
417 const struct net_device *out,
418 struct nf_conn *ct)) 415 struct nf_conn *ct))
419{ 416{
420 const struct nf_conn *ct; 417 const struct nf_conn *ct;
@@ -427,7 +424,7 @@ nf_nat_ipv4_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
427 ip_hdrlen(skb) < sizeof(struct iphdr)) 424 ip_hdrlen(skb) < sizeof(struct iphdr))
428 return NF_ACCEPT; 425 return NF_ACCEPT;
429 426
430 ret = nf_nat_ipv4_fn(ops, skb, in, out, do_chain); 427 ret = nf_nat_ipv4_fn(ops, skb, state, do_chain);
431 if (ret != NF_DROP && ret != NF_STOLEN && 428 if (ret != NF_DROP && ret != NF_STOLEN &&
432 (ct = nf_ct_get(skb, &ctinfo)) != NULL) { 429 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
433 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); 430 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
diff --git a/net/ipv4/netfilter/nf_reject_ipv4.c b/net/ipv4/netfilter/nf_reject_ipv4.c
index 536da7bc598a..3262e41ff76f 100644
--- a/net/ipv4/netfilter/nf_reject_ipv4.c
+++ b/net/ipv4/netfilter/nf_reject_ipv4.c
@@ -13,6 +13,7 @@
13#include <net/dst.h> 13#include <net/dst.h>
14#include <net/netfilter/ipv4/nf_reject.h> 14#include <net/netfilter/ipv4/nf_reject.h>
15#include <linux/netfilter_ipv4.h> 15#include <linux/netfilter_ipv4.h>
16#include <linux/netfilter_bridge.h>
16#include <net/netfilter/ipv4/nf_reject.h> 17#include <net/netfilter/ipv4/nf_reject.h>
17 18
18const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb, 19const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,
@@ -43,7 +44,7 @@ EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get);
43 44
44struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb, 45struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb,
45 const struct sk_buff *oldskb, 46 const struct sk_buff *oldskb,
46 __be16 protocol, int ttl) 47 __u8 protocol, int ttl)
47{ 48{
48 struct iphdr *niph, *oiph = ip_hdr(oldskb); 49 struct iphdr *niph, *oiph = ip_hdr(oldskb);
49 50
@@ -146,7 +147,8 @@ void nf_send_reset(struct sk_buff *oldskb, int hook)
146 */ 147 */
147 if (oldskb->nf_bridge) { 148 if (oldskb->nf_bridge) {
148 struct ethhdr *oeth = eth_hdr(oldskb); 149 struct ethhdr *oeth = eth_hdr(oldskb);
149 nskb->dev = oldskb->nf_bridge->physindev; 150
151 nskb->dev = nf_bridge_get_physindev(oldskb);
150 niph->tot_len = htons(nskb->len); 152 niph->tot_len = htons(nskb->len);
151 ip_send_check(niph); 153 ip_send_check(niph);
152 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol), 154 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
@@ -164,4 +166,27 @@ void nf_send_reset(struct sk_buff *oldskb, int hook)
164} 166}
165EXPORT_SYMBOL_GPL(nf_send_reset); 167EXPORT_SYMBOL_GPL(nf_send_reset);
166 168
169void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)
170{
171 struct iphdr *iph = ip_hdr(skb_in);
172 u8 proto;
173
174 if (skb_in->csum_bad || iph->frag_off & htons(IP_OFFSET))
175 return;
176
177 if (skb_csum_unnecessary(skb_in)) {
178 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
179 return;
180 }
181
182 if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
183 proto = iph->protocol;
184 else
185 proto = 0;
186
187 if (nf_ip_checksum(skb_in, hook, ip_hdrlen(skb_in), proto) == 0)
188 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
189}
190EXPORT_SYMBOL_GPL(nf_send_unreach);
191
167MODULE_LICENSE("GPL"); 192MODULE_LICENSE("GPL");
diff --git a/net/ipv4/netfilter/nf_tables_arp.c b/net/ipv4/netfilter/nf_tables_arp.c
index 19412a4063fb..8412268bbad1 100644
--- a/net/ipv4/netfilter/nf_tables_arp.c
+++ b/net/ipv4/netfilter/nf_tables_arp.c
@@ -17,13 +17,11 @@
17static unsigned int 17static unsigned int
18nft_do_chain_arp(const struct nf_hook_ops *ops, 18nft_do_chain_arp(const struct nf_hook_ops *ops,
19 struct sk_buff *skb, 19 struct sk_buff *skb,
20 const struct net_device *in, 20 const struct nf_hook_state *state)
21 const struct net_device *out,
22 int (*okfn)(struct sk_buff *))
23{ 21{
24 struct nft_pktinfo pkt; 22 struct nft_pktinfo pkt;
25 23
26 nft_set_pktinfo(&pkt, ops, skb, in, out); 24 nft_set_pktinfo(&pkt, ops, skb, state);
27 25
28 return nft_do_chain(&pkt, ops); 26 return nft_do_chain(&pkt, ops);
29} 27}
diff --git a/net/ipv4/netfilter/nf_tables_ipv4.c b/net/ipv4/netfilter/nf_tables_ipv4.c
index 6820c8c40842..aa180d3a69a5 100644
--- a/net/ipv4/netfilter/nf_tables_ipv4.c
+++ b/net/ipv4/netfilter/nf_tables_ipv4.c
@@ -20,22 +20,18 @@
20 20
21static unsigned int nft_do_chain_ipv4(const struct nf_hook_ops *ops, 21static unsigned int nft_do_chain_ipv4(const struct nf_hook_ops *ops,
22 struct sk_buff *skb, 22 struct sk_buff *skb,
23 const struct net_device *in, 23 const struct nf_hook_state *state)
24 const struct net_device *out,
25 int (*okfn)(struct sk_buff *))
26{ 24{
27 struct nft_pktinfo pkt; 25 struct nft_pktinfo pkt;
28 26
29 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out); 27 nft_set_pktinfo_ipv4(&pkt, ops, skb, state);
30 28
31 return nft_do_chain(&pkt, ops); 29 return nft_do_chain(&pkt, ops);
32} 30}
33 31
34static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops, 32static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops,
35 struct sk_buff *skb, 33 struct sk_buff *skb,
36 const struct net_device *in, 34 const struct nf_hook_state *state)
37 const struct net_device *out,
38 int (*okfn)(struct sk_buff *))
39{ 35{
40 if (unlikely(skb->len < sizeof(struct iphdr) || 36 if (unlikely(skb->len < sizeof(struct iphdr) ||
41 ip_hdr(skb)->ihl < sizeof(struct iphdr) / 4)) { 37 ip_hdr(skb)->ihl < sizeof(struct iphdr) / 4)) {
@@ -45,7 +41,7 @@ static unsigned int nft_ipv4_output(const struct nf_hook_ops *ops,
45 return NF_ACCEPT; 41 return NF_ACCEPT;
46 } 42 }
47 43
48 return nft_do_chain_ipv4(ops, skb, in, out, okfn); 44 return nft_do_chain_ipv4(ops, skb, state);
49} 45}
50 46
51struct nft_af_info nft_af_ipv4 __read_mostly = { 47struct nft_af_info nft_af_ipv4 __read_mostly = {
diff --git a/net/ipv4/netfilter/nft_chain_nat_ipv4.c b/net/ipv4/netfilter/nft_chain_nat_ipv4.c
index df547bf50078..bf5c30ae14e4 100644
--- a/net/ipv4/netfilter/nft_chain_nat_ipv4.c
+++ b/net/ipv4/netfilter/nft_chain_nat_ipv4.c
@@ -28,51 +28,42 @@
28 28
29static unsigned int nft_nat_do_chain(const struct nf_hook_ops *ops, 29static unsigned int nft_nat_do_chain(const struct nf_hook_ops *ops,
30 struct sk_buff *skb, 30 struct sk_buff *skb,
31 const struct net_device *in, 31 const struct nf_hook_state *state,
32 const struct net_device *out,
33 struct nf_conn *ct) 32 struct nf_conn *ct)
34{ 33{
35 struct nft_pktinfo pkt; 34 struct nft_pktinfo pkt;
36 35
37 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out); 36 nft_set_pktinfo_ipv4(&pkt, ops, skb, state);
38 37
39 return nft_do_chain(&pkt, ops); 38 return nft_do_chain(&pkt, ops);
40} 39}
41 40
42static unsigned int nft_nat_ipv4_fn(const struct nf_hook_ops *ops, 41static unsigned int nft_nat_ipv4_fn(const struct nf_hook_ops *ops,
43 struct sk_buff *skb, 42 struct sk_buff *skb,
44 const struct net_device *in, 43 const struct nf_hook_state *state)
45 const struct net_device *out,
46 int (*okfn)(struct sk_buff *))
47{ 44{
48 return nf_nat_ipv4_fn(ops, skb, in, out, nft_nat_do_chain); 45 return nf_nat_ipv4_fn(ops, skb, state, nft_nat_do_chain);
49} 46}
50 47
51static unsigned int nft_nat_ipv4_in(const struct nf_hook_ops *ops, 48static unsigned int nft_nat_ipv4_in(const struct nf_hook_ops *ops,
52 struct sk_buff *skb, 49 struct sk_buff *skb,
53 const struct net_device *in, 50 const struct nf_hook_state *state)
54 const struct net_device *out,
55 int (*okfn)(struct sk_buff *))
56{ 51{
57 return nf_nat_ipv4_in(ops, skb, in, out, nft_nat_do_chain); 52 return nf_nat_ipv4_in(ops, skb, state, nft_nat_do_chain);
58} 53}
59 54
60static unsigned int nft_nat_ipv4_out(const struct nf_hook_ops *ops, 55static unsigned int nft_nat_ipv4_out(const struct nf_hook_ops *ops,
61 struct sk_buff *skb, 56 struct sk_buff *skb,
62 const struct net_device *in, 57 const struct nf_hook_state *state)
63 const struct net_device *out,
64 int (*okfn)(struct sk_buff *))
65{ 58{
66 return nf_nat_ipv4_out(ops, skb, in, out, nft_nat_do_chain); 59 return nf_nat_ipv4_out(ops, skb, state, nft_nat_do_chain);
67} 60}
68 61
69static unsigned int nft_nat_ipv4_local_fn(const struct nf_hook_ops *ops, 62static unsigned int nft_nat_ipv4_local_fn(const struct nf_hook_ops *ops,
70 struct sk_buff *skb, 63 struct sk_buff *skb,
71 const struct net_device *in, 64 const struct nf_hook_state *state)
72 const struct net_device *out,
73 int (*okfn)(struct sk_buff *))
74{ 65{
75 return nf_nat_ipv4_local_fn(ops, skb, in, out, nft_nat_do_chain); 66 return nf_nat_ipv4_local_fn(ops, skb, state, nft_nat_do_chain);
76} 67}
77 68
78static const struct nf_chain_type nft_chain_nat_ipv4 = { 69static const struct nf_chain_type nft_chain_nat_ipv4 = {
diff --git a/net/ipv4/netfilter/nft_chain_route_ipv4.c b/net/ipv4/netfilter/nft_chain_route_ipv4.c
index 125b66766c0a..e335b0afdaf3 100644
--- a/net/ipv4/netfilter/nft_chain_route_ipv4.c
+++ b/net/ipv4/netfilter/nft_chain_route_ipv4.c
@@ -23,9 +23,7 @@
23 23
24static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, 24static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
25 struct sk_buff *skb, 25 struct sk_buff *skb,
26 const struct net_device *in, 26 const struct nf_hook_state *state)
27 const struct net_device *out,
28 int (*okfn)(struct sk_buff *))
29{ 27{
30 unsigned int ret; 28 unsigned int ret;
31 struct nft_pktinfo pkt; 29 struct nft_pktinfo pkt;
@@ -39,7 +37,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
39 ip_hdrlen(skb) < sizeof(struct iphdr)) 37 ip_hdrlen(skb) < sizeof(struct iphdr))
40 return NF_ACCEPT; 38 return NF_ACCEPT;
41 39
42 nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out); 40 nft_set_pktinfo_ipv4(&pkt, ops, skb, state);
43 41
44 mark = skb->mark; 42 mark = skb->mark;
45 iph = ip_hdr(skb); 43 iph = ip_hdr(skb);
diff --git a/net/ipv4/netfilter/nft_masq_ipv4.c b/net/ipv4/netfilter/nft_masq_ipv4.c
index 665de06561cd..40e414c4ca56 100644
--- a/net/ipv4/netfilter/nft_masq_ipv4.c
+++ b/net/ipv4/netfilter/nft_masq_ipv4.c
@@ -17,20 +17,17 @@
17#include <net/netfilter/ipv4/nf_nat_masquerade.h> 17#include <net/netfilter/ipv4/nf_nat_masquerade.h>
18 18
19static void nft_masq_ipv4_eval(const struct nft_expr *expr, 19static void nft_masq_ipv4_eval(const struct nft_expr *expr,
20 struct nft_data data[NFT_REG_MAX + 1], 20 struct nft_regs *regs,
21 const struct nft_pktinfo *pkt) 21 const struct nft_pktinfo *pkt)
22{ 22{
23 struct nft_masq *priv = nft_expr_priv(expr); 23 struct nft_masq *priv = nft_expr_priv(expr);
24 struct nf_nat_range range; 24 struct nf_nat_range range;
25 unsigned int verdict;
26 25
27 memset(&range, 0, sizeof(range)); 26 memset(&range, 0, sizeof(range));
28 range.flags = priv->flags; 27 range.flags = priv->flags;
29 28
30 verdict = nf_nat_masquerade_ipv4(pkt->skb, pkt->ops->hooknum, 29 regs->verdict.code = nf_nat_masquerade_ipv4(pkt->skb, pkt->ops->hooknum,
31 &range, pkt->out); 30 &range, pkt->out);
32
33 data[NFT_REG_VERDICT].verdict = verdict;
34} 31}
35 32
36static struct nft_expr_type nft_masq_ipv4_type; 33static struct nft_expr_type nft_masq_ipv4_type;
diff --git a/net/ipv4/netfilter/nft_redir_ipv4.c b/net/ipv4/netfilter/nft_redir_ipv4.c
index 6ecfce63201a..d8d795df9c13 100644
--- a/net/ipv4/netfilter/nft_redir_ipv4.c
+++ b/net/ipv4/netfilter/nft_redir_ipv4.c
@@ -18,26 +18,25 @@
18#include <net/netfilter/nft_redir.h> 18#include <net/netfilter/nft_redir.h>
19 19
20static void nft_redir_ipv4_eval(const struct nft_expr *expr, 20static void nft_redir_ipv4_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt) 22 const struct nft_pktinfo *pkt)
23{ 23{
24 struct nft_redir *priv = nft_expr_priv(expr); 24 struct nft_redir *priv = nft_expr_priv(expr);
25 struct nf_nat_ipv4_multi_range_compat mr; 25 struct nf_nat_ipv4_multi_range_compat mr;
26 unsigned int verdict;
27 26
28 memset(&mr, 0, sizeof(mr)); 27 memset(&mr, 0, sizeof(mr));
29 if (priv->sreg_proto_min) { 28 if (priv->sreg_proto_min) {
30 mr.range[0].min.all = 29 mr.range[0].min.all =
31 *(__be16 *)&data[priv->sreg_proto_min].data[0]; 30 *(__be16 *)&regs->data[priv->sreg_proto_min];
32 mr.range[0].max.all = 31 mr.range[0].max.all =
33 *(__be16 *)&data[priv->sreg_proto_max].data[0]; 32 *(__be16 *)&regs->data[priv->sreg_proto_max];
34 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 33 mr.range[0].flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
35 } 34 }
36 35
37 mr.range[0].flags |= priv->flags; 36 mr.range[0].flags |= priv->flags;
38 37
39 verdict = nf_nat_redirect_ipv4(pkt->skb, &mr, pkt->ops->hooknum); 38 regs->verdict.code = nf_nat_redirect_ipv4(pkt->skb, &mr,
40 data[NFT_REG_VERDICT].verdict = verdict; 39 pkt->ops->hooknum);
41} 40}
42 41
43static struct nft_expr_type nft_redir_ipv4_type; 42static struct nft_expr_type nft_redir_ipv4_type;
diff --git a/net/ipv4/netfilter/nft_reject_ipv4.c b/net/ipv4/netfilter/nft_reject_ipv4.c
index d729542bd1b7..b07e58b51158 100644
--- a/net/ipv4/netfilter/nft_reject_ipv4.c
+++ b/net/ipv4/netfilter/nft_reject_ipv4.c
@@ -20,21 +20,24 @@
20#include <net/netfilter/nft_reject.h> 20#include <net/netfilter/nft_reject.h>
21 21
22static void nft_reject_ipv4_eval(const struct nft_expr *expr, 22static void nft_reject_ipv4_eval(const struct nft_expr *expr,
23 struct nft_data data[NFT_REG_MAX + 1], 23 struct nft_regs *regs,
24 const struct nft_pktinfo *pkt) 24 const struct nft_pktinfo *pkt)
25{ 25{
26 struct nft_reject *priv = nft_expr_priv(expr); 26 struct nft_reject *priv = nft_expr_priv(expr);
27 27
28 switch (priv->type) { 28 switch (priv->type) {
29 case NFT_REJECT_ICMP_UNREACH: 29 case NFT_REJECT_ICMP_UNREACH:
30 nf_send_unreach(pkt->skb, priv->icmp_code); 30 nf_send_unreach(pkt->skb, priv->icmp_code,
31 pkt->ops->hooknum);
31 break; 32 break;
32 case NFT_REJECT_TCP_RST: 33 case NFT_REJECT_TCP_RST:
33 nf_send_reset(pkt->skb, pkt->ops->hooknum); 34 nf_send_reset(pkt->skb, pkt->ops->hooknum);
34 break; 35 break;
36 default:
37 break;
35 } 38 }
36 39
37 data[NFT_REG_VERDICT].verdict = NF_DROP; 40 regs->verdict.code = NF_DROP;
38} 41}
39 42
40static struct nft_expr_type nft_reject_ipv4_type; 43static struct nft_expr_type nft_reject_ipv4_type;
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 208d5439e59b..a93f260cf24c 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -64,11 +64,11 @@ EXPORT_SYMBOL_GPL(pingv6_ops);
64 64
65static u16 ping_port_rover; 65static u16 ping_port_rover;
66 66
67static inline int ping_hashfn(struct net *net, unsigned int num, unsigned int mask) 67static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)
68{ 68{
69 int res = (num + net_hash_mix(net)) & mask; 69 u32 res = (num + net_hash_mix(net)) & mask;
70 70
71 pr_debug("hash(%d) = %d\n", num, res); 71 pr_debug("hash(%u) = %u\n", num, res);
72 return res; 72 return res;
73} 73}
74EXPORT_SYMBOL_GPL(ping_hash); 74EXPORT_SYMBOL_GPL(ping_hash);
@@ -516,7 +516,7 @@ void ping_err(struct sk_buff *skb, int offset, u32 info)
516 ntohs(icmph->un.echo.sequence)); 516 ntohs(icmph->un.echo.sequence));
517 517
518 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); 518 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
519 if (sk == NULL) { 519 if (!sk) {
520 pr_debug("no socket, dropping\n"); 520 pr_debug("no socket, dropping\n");
521 return; /* No socket for error */ 521 return; /* No socket for error */
522 } 522 }
@@ -692,8 +692,7 @@ int ping_common_sendmsg(int family, struct msghdr *msg, size_t len,
692} 692}
693EXPORT_SYMBOL_GPL(ping_common_sendmsg); 693EXPORT_SYMBOL_GPL(ping_common_sendmsg);
694 694
695static int ping_v4_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 695static int ping_v4_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
696 size_t len)
697{ 696{
698 struct net *net = sock_net(sk); 697 struct net *net = sock_net(sk);
699 struct flowi4 fl4; 698 struct flowi4 fl4;
@@ -849,8 +848,8 @@ do_confirm:
849 goto out; 848 goto out;
850} 849}
851 850
852int ping_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 851int ping_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
853 size_t len, int noblock, int flags, int *addr_len) 852 int flags, int *addr_len)
854{ 853{
855 struct inet_sock *isk = inet_sk(sk); 854 struct inet_sock *isk = inet_sk(sk);
856 int family = sk->sk_family; 855 int family = sk->sk_family;
@@ -972,7 +971,7 @@ bool ping_rcv(struct sk_buff *skb)
972 skb_push(skb, skb->data - (u8 *)icmph); 971 skb_push(skb, skb->data - (u8 *)icmph);
973 972
974 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id)); 973 sk = ping_lookup(net, skb, ntohs(icmph->un.echo.id));
975 if (sk != NULL) { 974 if (sk) {
976 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 975 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
977 976
978 pr_debug("rcv on socket %p\n", sk); 977 pr_debug("rcv on socket %p\n", sk);
diff --git a/net/ipv4/proc.c b/net/ipv4/proc.c
index d8953ef0770c..e1f3b911dd1e 100644
--- a/net/ipv4/proc.c
+++ b/net/ipv4/proc.c
@@ -63,7 +63,7 @@ static int sockstat_seq_show(struct seq_file *seq, void *v)
63 socket_seq_show(seq); 63 socket_seq_show(seq);
64 seq_printf(seq, "TCP: inuse %d orphan %d tw %d alloc %d mem %ld\n", 64 seq_printf(seq, "TCP: inuse %d orphan %d tw %d alloc %d mem %ld\n",
65 sock_prot_inuse_get(net, &tcp_prot), orphans, 65 sock_prot_inuse_get(net, &tcp_prot), orphans,
66 tcp_death_row.tw_count, sockets, 66 atomic_read(&tcp_death_row.tw_count), sockets,
67 proto_memory_allocated(&tcp_prot)); 67 proto_memory_allocated(&tcp_prot));
68 seq_printf(seq, "UDP: inuse %d mem %ld\n", 68 seq_printf(seq, "UDP: inuse %d mem %ld\n",
69 sock_prot_inuse_get(net, &udp_prot), 69 sock_prot_inuse_get(net, &udp_prot),
diff --git a/net/ipv4/raw.c b/net/ipv4/raw.c
index f027a708b7e0..561cd4b8fc6e 100644
--- a/net/ipv4/raw.c
+++ b/net/ipv4/raw.c
@@ -46,7 +46,6 @@
46#include <linux/stddef.h> 46#include <linux/stddef.h>
47#include <linux/slab.h> 47#include <linux/slab.h>
48#include <linux/errno.h> 48#include <linux/errno.h>
49#include <linux/aio.h>
50#include <linux/kernel.h> 49#include <linux/kernel.h>
51#include <linux/export.h> 50#include <linux/export.h>
52#include <linux/spinlock.h> 51#include <linux/spinlock.h>
@@ -293,7 +292,7 @@ void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
293 292
294 read_lock(&raw_v4_hashinfo.lock); 293 read_lock(&raw_v4_hashinfo.lock);
295 raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]); 294 raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
296 if (raw_sk != NULL) { 295 if (raw_sk) {
297 iph = (const struct iphdr *)skb->data; 296 iph = (const struct iphdr *)skb->data;
298 net = dev_net(skb->dev); 297 net = dev_net(skb->dev);
299 298
@@ -363,7 +362,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
363 skb = sock_alloc_send_skb(sk, 362 skb = sock_alloc_send_skb(sk,
364 length + hlen + tlen + 15, 363 length + hlen + tlen + 15,
365 flags & MSG_DONTWAIT, &err); 364 flags & MSG_DONTWAIT, &err);
366 if (skb == NULL) 365 if (!skb)
367 goto error; 366 goto error;
368 skb_reserve(skb, hlen); 367 skb_reserve(skb, hlen);
369 368
@@ -404,7 +403,7 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
404 iph->check = 0; 403 iph->check = 0;
405 iph->tot_len = htons(length); 404 iph->tot_len = htons(length);
406 if (!iph->id) 405 if (!iph->id)
407 ip_select_ident(skb, NULL); 406 ip_select_ident(net, skb, NULL);
408 407
409 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl); 408 iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
410 } 409 }
@@ -412,8 +411,8 @@ static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
412 icmp_out_count(net, ((struct icmphdr *) 411 icmp_out_count(net, ((struct icmphdr *)
413 skb_transport_header(skb))->type); 412 skb_transport_header(skb))->type);
414 413
415 err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, skb, NULL, 414 err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT, sk, skb,
416 rt->dst.dev, dst_output); 415 NULL, rt->dst.dev, dst_output_sk);
417 if (err > 0) 416 if (err > 0)
418 err = net_xmit_errno(err); 417 err = net_xmit_errno(err);
419 if (err) 418 if (err)
@@ -481,8 +480,7 @@ static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
481 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 480 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
482} 481}
483 482
484static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 483static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
485 size_t len)
486{ 484{
487 struct inet_sock *inet = inet_sk(sk); 485 struct inet_sock *inet = inet_sk(sk);
488 struct ipcm_cookie ipc; 486 struct ipcm_cookie ipc;
@@ -709,8 +707,8 @@ out: return ret;
709 * we return it, otherwise we block. 707 * we return it, otherwise we block.
710 */ 708 */
711 709
712static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 710static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
713 size_t len, int noblock, int flags, int *addr_len) 711 int noblock, int flags, int *addr_len)
714{ 712{
715 struct inet_sock *inet = inet_sk(sk); 713 struct inet_sock *inet = inet_sk(sk);
716 size_t copied = 0; 714 size_t copied = 0;
@@ -873,7 +871,7 @@ static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
873 871
874 spin_lock_bh(&sk->sk_receive_queue.lock); 872 spin_lock_bh(&sk->sk_receive_queue.lock);
875 skb = skb_peek(&sk->sk_receive_queue); 873 skb = skb_peek(&sk->sk_receive_queue);
876 if (skb != NULL) 874 if (skb)
877 amount = skb->len; 875 amount = skb->len;
878 spin_unlock_bh(&sk->sk_receive_queue.lock); 876 spin_unlock_bh(&sk->sk_receive_queue.lock);
879 return put_user(amount, (int __user *)arg); 877 return put_user(amount, (int __user *)arg);
diff --git a/net/ipv4/route.c b/net/ipv4/route.c
index ad5064362c5c..a78540f28276 100644
--- a/net/ipv4/route.c
+++ b/net/ipv4/route.c
@@ -152,7 +152,6 @@ static struct neighbour *ipv4_neigh_lookup(const struct dst_entry *dst,
152 152
153static struct dst_ops ipv4_dst_ops = { 153static struct dst_ops ipv4_dst_ops = {
154 .family = AF_INET, 154 .family = AF_INET,
155 .protocol = cpu_to_be16(ETH_P_IP),
156 .check = ipv4_dst_check, 155 .check = ipv4_dst_check,
157 .default_advmss = ipv4_default_advmss, 156 .default_advmss = ipv4_default_advmss,
158 .mtu = ipv4_mtu, 157 .mtu = ipv4_mtu,
@@ -483,7 +482,7 @@ u32 ip_idents_reserve(u32 hash, int segs)
483} 482}
484EXPORT_SYMBOL(ip_idents_reserve); 483EXPORT_SYMBOL(ip_idents_reserve);
485 484
486void __ip_select_ident(struct iphdr *iph, int segs) 485void __ip_select_ident(struct net *net, struct iphdr *iph, int segs)
487{ 486{
488 static u32 ip_idents_hashrnd __read_mostly; 487 static u32 ip_idents_hashrnd __read_mostly;
489 u32 hash, id; 488 u32 hash, id;
@@ -492,7 +491,7 @@ void __ip_select_ident(struct iphdr *iph, int segs)
492 491
493 hash = jhash_3words((__force u32)iph->daddr, 492 hash = jhash_3words((__force u32)iph->daddr,
494 (__force u32)iph->saddr, 493 (__force u32)iph->saddr,
495 iph->protocol, 494 iph->protocol ^ net_hash_mix(net),
496 ip_idents_hashrnd); 495 ip_idents_hashrnd);
497 id = ip_idents_reserve(hash, segs); 496 id = ip_idents_reserve(hash, segs);
498 iph->id = htons(id); 497 iph->id = htons(id);
@@ -1057,7 +1056,7 @@ void ipv4_sk_update_pmtu(struct sk_buff *skb, struct sock *sk, u32 mtu)
1057 __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0); 1056 __build_flow_key(&fl4, sk, iph, 0, 0, 0, 0, 0);
1058 1057
1059 rt = (struct rtable *)odst; 1058 rt = (struct rtable *)odst;
1060 if (odst->obsolete && odst->ops->check(odst, 0) == NULL) { 1059 if (odst->obsolete && !odst->ops->check(odst, 0)) {
1061 rt = ip_route_output_flow(sock_net(sk), &fl4, sk); 1060 rt = ip_route_output_flow(sock_net(sk), &fl4, sk);
1062 if (IS_ERR(rt)) 1061 if (IS_ERR(rt))
1063 goto out; 1062 goto out;
@@ -1451,7 +1450,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
1451 1450
1452 /* Primary sanity checks. */ 1451 /* Primary sanity checks. */
1453 1452
1454 if (in_dev == NULL) 1453 if (!in_dev)
1455 return -EINVAL; 1454 return -EINVAL;
1456 1455
1457 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) || 1456 if (ipv4_is_multicast(saddr) || ipv4_is_lbcast(saddr) ||
@@ -1554,7 +1553,7 @@ static int __mkroute_input(struct sk_buff *skb,
1554 1553
1555 /* get a working reference to the output device */ 1554 /* get a working reference to the output device */
1556 out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res)); 1555 out_dev = __in_dev_get_rcu(FIB_RES_DEV(*res));
1557 if (out_dev == NULL) { 1556 if (!out_dev) {
1558 net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n"); 1557 net_crit_ratelimited("Bug in ip_route_input_slow(). Please report.\n");
1559 return -EINVAL; 1558 return -EINVAL;
1560 } 1559 }
@@ -1592,7 +1591,7 @@ static int __mkroute_input(struct sk_buff *skb,
1592 1591
1593 fnhe = find_exception(&FIB_RES_NH(*res), daddr); 1592 fnhe = find_exception(&FIB_RES_NH(*res), daddr);
1594 if (do_cache) { 1593 if (do_cache) {
1595 if (fnhe != NULL) 1594 if (fnhe)
1596 rth = rcu_dereference(fnhe->fnhe_rth_input); 1595 rth = rcu_dereference(fnhe->fnhe_rth_input);
1597 else 1596 else
1598 rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input); 1597 rth = rcu_dereference(FIB_RES_NH(*res).nh_rth_input);
@@ -2055,7 +2054,7 @@ struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
2055 ipv4_is_lbcast(fl4->daddr))) { 2054 ipv4_is_lbcast(fl4->daddr))) {
2056 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */ 2055 /* It is equivalent to inet_addr_type(saddr) == RTN_LOCAL */
2057 dev_out = __ip_dev_find(net, fl4->saddr, false); 2056 dev_out = __ip_dev_find(net, fl4->saddr, false);
2058 if (dev_out == NULL) 2057 if (!dev_out)
2059 goto out; 2058 goto out;
2060 2059
2061 /* Special hack: user can direct multicasts 2060 /* Special hack: user can direct multicasts
@@ -2088,7 +2087,7 @@ struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
2088 if (fl4->flowi4_oif) { 2087 if (fl4->flowi4_oif) {
2089 dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif); 2088 dev_out = dev_get_by_index_rcu(net, fl4->flowi4_oif);
2090 rth = ERR_PTR(-ENODEV); 2089 rth = ERR_PTR(-ENODEV);
2091 if (dev_out == NULL) 2090 if (!dev_out)
2092 goto out; 2091 goto out;
2093 2092
2094 /* RACE: Check return value of inet_select_addr instead. */ 2093 /* RACE: Check return value of inet_select_addr instead. */
@@ -2225,7 +2224,6 @@ static u32 *ipv4_rt_blackhole_cow_metrics(struct dst_entry *dst,
2225 2224
2226static struct dst_ops ipv4_dst_blackhole_ops = { 2225static struct dst_ops ipv4_dst_blackhole_ops = {
2227 .family = AF_INET, 2226 .family = AF_INET,
2228 .protocol = cpu_to_be16(ETH_P_IP),
2229 .check = ipv4_blackhole_dst_check, 2227 .check = ipv4_blackhole_dst_check,
2230 .mtu = ipv4_blackhole_mtu, 2228 .mtu = ipv4_blackhole_mtu,
2231 .default_advmss = ipv4_default_advmss, 2229 .default_advmss = ipv4_default_advmss,
@@ -2301,7 +2299,7 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
2301 u32 metrics[RTAX_MAX]; 2299 u32 metrics[RTAX_MAX];
2302 2300
2303 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags); 2301 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*r), flags);
2304 if (nlh == NULL) 2302 if (!nlh)
2305 return -EMSGSIZE; 2303 return -EMSGSIZE;
2306 2304
2307 r = nlmsg_data(nlh); 2305 r = nlmsg_data(nlh);
@@ -2321,11 +2319,11 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
2321 if (IPCB(skb)->flags & IPSKB_DOREDIRECT) 2319 if (IPCB(skb)->flags & IPSKB_DOREDIRECT)
2322 r->rtm_flags |= RTCF_DOREDIRECT; 2320 r->rtm_flags |= RTCF_DOREDIRECT;
2323 2321
2324 if (nla_put_be32(skb, RTA_DST, dst)) 2322 if (nla_put_in_addr(skb, RTA_DST, dst))
2325 goto nla_put_failure; 2323 goto nla_put_failure;
2326 if (src) { 2324 if (src) {
2327 r->rtm_src_len = 32; 2325 r->rtm_src_len = 32;
2328 if (nla_put_be32(skb, RTA_SRC, src)) 2326 if (nla_put_in_addr(skb, RTA_SRC, src))
2329 goto nla_put_failure; 2327 goto nla_put_failure;
2330 } 2328 }
2331 if (rt->dst.dev && 2329 if (rt->dst.dev &&
@@ -2338,11 +2336,11 @@ static int rt_fill_info(struct net *net, __be32 dst, __be32 src,
2338#endif 2336#endif
2339 if (!rt_is_input_route(rt) && 2337 if (!rt_is_input_route(rt) &&
2340 fl4->saddr != src) { 2338 fl4->saddr != src) {
2341 if (nla_put_be32(skb, RTA_PREFSRC, fl4->saddr)) 2339 if (nla_put_in_addr(skb, RTA_PREFSRC, fl4->saddr))
2342 goto nla_put_failure; 2340 goto nla_put_failure;
2343 } 2341 }
2344 if (rt->rt_uses_gateway && 2342 if (rt->rt_uses_gateway &&
2345 nla_put_be32(skb, RTA_GATEWAY, rt->rt_gateway)) 2343 nla_put_in_addr(skb, RTA_GATEWAY, rt->rt_gateway))
2346 goto nla_put_failure; 2344 goto nla_put_failure;
2347 2345
2348 expires = rt->dst.expires; 2346 expires = rt->dst.expires;
@@ -2423,7 +2421,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
2423 rtm = nlmsg_data(nlh); 2421 rtm = nlmsg_data(nlh);
2424 2422
2425 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); 2423 skb = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
2426 if (skb == NULL) { 2424 if (!skb) {
2427 err = -ENOBUFS; 2425 err = -ENOBUFS;
2428 goto errout; 2426 goto errout;
2429 } 2427 }
@@ -2438,8 +2436,8 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
2438 ip_hdr(skb)->protocol = IPPROTO_ICMP; 2436 ip_hdr(skb)->protocol = IPPROTO_ICMP;
2439 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr)); 2437 skb_reserve(skb, MAX_HEADER + sizeof(struct iphdr));
2440 2438
2441 src = tb[RTA_SRC] ? nla_get_be32(tb[RTA_SRC]) : 0; 2439 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2442 dst = tb[RTA_DST] ? nla_get_be32(tb[RTA_DST]) : 0; 2440 dst = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2443 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0; 2441 iif = tb[RTA_IIF] ? nla_get_u32(tb[RTA_IIF]) : 0;
2444 mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0; 2442 mark = tb[RTA_MARK] ? nla_get_u32(tb[RTA_MARK]) : 0;
2445 2443
@@ -2454,7 +2452,7 @@ static int inet_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh)
2454 struct net_device *dev; 2452 struct net_device *dev;
2455 2453
2456 dev = __dev_get_by_index(net, iif); 2454 dev = __dev_get_by_index(net, iif);
2457 if (dev == NULL) { 2455 if (!dev) {
2458 err = -ENODEV; 2456 err = -ENODEV;
2459 goto errout_free; 2457 goto errout_free;
2460 } 2458 }
@@ -2653,7 +2651,7 @@ static __net_init int sysctl_route_net_init(struct net *net)
2653 tbl = ipv4_route_flush_table; 2651 tbl = ipv4_route_flush_table;
2654 if (!net_eq(net, &init_net)) { 2652 if (!net_eq(net, &init_net)) {
2655 tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL); 2653 tbl = kmemdup(tbl, sizeof(ipv4_route_flush_table), GFP_KERNEL);
2656 if (tbl == NULL) 2654 if (!tbl)
2657 goto err_dup; 2655 goto err_dup;
2658 2656
2659 /* Don't export sysctls to unprivileged users */ 2657 /* Don't export sysctls to unprivileged users */
@@ -2663,7 +2661,7 @@ static __net_init int sysctl_route_net_init(struct net *net)
2663 tbl[0].extra1 = net; 2661 tbl[0].extra1 = net;
2664 2662
2665 net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl); 2663 net->ipv4.route_hdr = register_net_sysctl(net, "net/ipv4/route", tbl);
2666 if (net->ipv4.route_hdr == NULL) 2664 if (!net->ipv4.route_hdr)
2667 goto err_reg; 2665 goto err_reg;
2668 return 0; 2666 return 0;
2669 2667
diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
index 45fe60c5238e..df849e5a10f1 100644
--- a/net/ipv4/syncookies.c
+++ b/net/ipv4/syncookies.c
@@ -219,19 +219,20 @@ int __cookie_v4_check(const struct iphdr *iph, const struct tcphdr *th,
219} 219}
220EXPORT_SYMBOL_GPL(__cookie_v4_check); 220EXPORT_SYMBOL_GPL(__cookie_v4_check);
221 221
222static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb, 222static struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
223 struct request_sock *req, 223 struct request_sock *req,
224 struct dst_entry *dst) 224 struct dst_entry *dst)
225{ 225{
226 struct inet_connection_sock *icsk = inet_csk(sk); 226 struct inet_connection_sock *icsk = inet_csk(sk);
227 struct sock *child; 227 struct sock *child;
228 228
229 child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst); 229 child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
230 if (child) 230 if (child) {
231 atomic_set(&req->rsk_refcnt, 1);
231 inet_csk_reqsk_queue_add(sk, req, child); 232 inet_csk_reqsk_queue_add(sk, req, child);
232 else 233 } else {
233 reqsk_free(req); 234 reqsk_free(req);
234 235 }
235 return child; 236 return child;
236} 237}
237 238
@@ -325,7 +326,7 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
325 goto out; 326 goto out;
326 327
327 ret = NULL; 328 ret = NULL;
328 req = inet_reqsk_alloc(&tcp_request_sock_ops); /* for safety */ 329 req = inet_reqsk_alloc(&tcp_request_sock_ops, sk); /* for safety */
329 if (!req) 330 if (!req)
330 goto out; 331 goto out;
331 332
@@ -336,8 +337,8 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
336 req->mss = mss; 337 req->mss = mss;
337 ireq->ir_num = ntohs(th->dest); 338 ireq->ir_num = ntohs(th->dest);
338 ireq->ir_rmt_port = th->source; 339 ireq->ir_rmt_port = th->source;
339 ireq->ir_loc_addr = ip_hdr(skb)->daddr; 340 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
340 ireq->ir_rmt_addr = ip_hdr(skb)->saddr; 341 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
341 ireq->ir_mark = inet_request_mark(sk, skb); 342 ireq->ir_mark = inet_request_mark(sk, skb);
342 ireq->snd_wscale = tcp_opt.snd_wscale; 343 ireq->snd_wscale = tcp_opt.snd_wscale;
343 ireq->sack_ok = tcp_opt.sack_ok; 344 ireq->sack_ok = tcp_opt.sack_ok;
@@ -345,7 +346,9 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
345 ireq->tstamp_ok = tcp_opt.saw_tstamp; 346 ireq->tstamp_ok = tcp_opt.saw_tstamp;
346 req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0; 347 req->ts_recent = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsval : 0;
347 treq->snt_synack = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsecr : 0; 348 treq->snt_synack = tcp_opt.saw_tstamp ? tcp_opt.rcv_tsecr : 0;
348 treq->listener = NULL; 349 treq->tfo_listener = false;
350
351 ireq->ir_iif = sk->sk_bound_dev_if;
349 352
350 /* We throwed the options of the initial SYN away, so we hope 353 /* We throwed the options of the initial SYN away, so we hope
351 * the ACK carries the same options again (see RFC1122 4.2.3.8) 354 * the ACK carries the same options again (see RFC1122 4.2.3.8)
@@ -357,7 +360,6 @@ struct sock *cookie_v4_check(struct sock *sk, struct sk_buff *skb)
357 goto out; 360 goto out;
358 } 361 }
359 362
360 req->expires = 0UL;
361 req->num_retrans = 0; 363 req->num_retrans = 0;
362 364
363 /* 365 /*
diff --git a/net/ipv4/sysctl_net_ipv4.c b/net/ipv4/sysctl_net_ipv4.c
index d151539da8e6..c3852a7ff3c7 100644
--- a/net/ipv4/sysctl_net_ipv4.c
+++ b/net/ipv4/sysctl_net_ipv4.c
@@ -883,6 +883,20 @@ static struct ctl_table ipv4_net_table[] = {
883 .mode = 0644, 883 .mode = 0644,
884 .proc_handler = proc_dointvec, 884 .proc_handler = proc_dointvec,
885 }, 885 },
886 {
887 .procname = "tcp_probe_threshold",
888 .data = &init_net.ipv4.sysctl_tcp_probe_threshold,
889 .maxlen = sizeof(int),
890 .mode = 0644,
891 .proc_handler = proc_dointvec,
892 },
893 {
894 .procname = "tcp_probe_interval",
895 .data = &init_net.ipv4.sysctl_tcp_probe_interval,
896 .maxlen = sizeof(int),
897 .mode = 0644,
898 .proc_handler = proc_dointvec,
899 },
886 { } 900 { }
887}; 901};
888 902
@@ -895,7 +909,7 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
895 int i; 909 int i;
896 910
897 table = kmemdup(table, sizeof(ipv4_net_table), GFP_KERNEL); 911 table = kmemdup(table, sizeof(ipv4_net_table), GFP_KERNEL);
898 if (table == NULL) 912 if (!table)
899 goto err_alloc; 913 goto err_alloc;
900 914
901 /* Update the variables to point into the current struct net */ 915 /* Update the variables to point into the current struct net */
@@ -904,7 +918,7 @@ static __net_init int ipv4_sysctl_init_net(struct net *net)
904 } 918 }
905 919
906 net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table); 920 net->ipv4.ipv4_hdr = register_net_sysctl(net, "net/ipv4", table);
907 if (net->ipv4.ipv4_hdr == NULL) 921 if (!net->ipv4.ipv4_hdr)
908 goto err_reg; 922 goto err_reg;
909 923
910 net->ipv4.sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL); 924 net->ipv4.sysctl_local_reserved_ports = kzalloc(65536 / 8, GFP_KERNEL);
@@ -942,7 +956,7 @@ static __init int sysctl_ipv4_init(void)
942 struct ctl_table_header *hdr; 956 struct ctl_table_header *hdr;
943 957
944 hdr = register_net_sysctl(&init_net, "net/ipv4", ipv4_table); 958 hdr = register_net_sysctl(&init_net, "net/ipv4", ipv4_table);
945 if (hdr == NULL) 959 if (!hdr)
946 return -ENOMEM; 960 return -ENOMEM;
947 961
948 if (register_pernet_subsys(&ipv4_sysctl_ops)) { 962 if (register_pernet_subsys(&ipv4_sysctl_ops)) {
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 995a2259bcfc..59c8a027721b 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -496,7 +496,7 @@ unsigned int tcp_poll(struct file *file, struct socket *sock, poll_table *wait)
496 496
497 /* Connected or passive Fast Open socket? */ 497 /* Connected or passive Fast Open socket? */
498 if (sk->sk_state != TCP_SYN_SENT && 498 if (sk->sk_state != TCP_SYN_SENT &&
499 (sk->sk_state != TCP_SYN_RECV || tp->fastopen_rsk != NULL)) { 499 (sk->sk_state != TCP_SYN_RECV || tp->fastopen_rsk)) {
500 int target = sock_rcvlowat(sk, 0, INT_MAX); 500 int target = sock_rcvlowat(sk, 0, INT_MAX);
501 501
502 if (tp->urg_seq == tp->copied_seq && 502 if (tp->urg_seq == tp->copied_seq &&
@@ -1028,7 +1028,7 @@ static inline int select_size(const struct sock *sk, bool sg)
1028 1028
1029void tcp_free_fastopen_req(struct tcp_sock *tp) 1029void tcp_free_fastopen_req(struct tcp_sock *tp)
1030{ 1030{
1031 if (tp->fastopen_req != NULL) { 1031 if (tp->fastopen_req) {
1032 kfree(tp->fastopen_req); 1032 kfree(tp->fastopen_req);
1033 tp->fastopen_req = NULL; 1033 tp->fastopen_req = NULL;
1034 } 1034 }
@@ -1042,12 +1042,12 @@ static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1042 1042
1043 if (!(sysctl_tcp_fastopen & TFO_CLIENT_ENABLE)) 1043 if (!(sysctl_tcp_fastopen & TFO_CLIENT_ENABLE))
1044 return -EOPNOTSUPP; 1044 return -EOPNOTSUPP;
1045 if (tp->fastopen_req != NULL) 1045 if (tp->fastopen_req)
1046 return -EALREADY; /* Another Fast Open is in progress */ 1046 return -EALREADY; /* Another Fast Open is in progress */
1047 1047
1048 tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request), 1048 tp->fastopen_req = kzalloc(sizeof(struct tcp_fastopen_request),
1049 sk->sk_allocation); 1049 sk->sk_allocation);
1050 if (unlikely(tp->fastopen_req == NULL)) 1050 if (unlikely(!tp->fastopen_req))
1051 return -ENOBUFS; 1051 return -ENOBUFS;
1052 tp->fastopen_req->data = msg; 1052 tp->fastopen_req->data = msg;
1053 tp->fastopen_req->size = size; 1053 tp->fastopen_req->size = size;
@@ -1060,8 +1060,7 @@ static int tcp_sendmsg_fastopen(struct sock *sk, struct msghdr *msg,
1060 return err; 1060 return err;
1061} 1061}
1062 1062
1063int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 1063int tcp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
1064 size_t size)
1065{ 1064{
1066 struct tcp_sock *tp = tcp_sk(sk); 1065 struct tcp_sock *tp = tcp_sk(sk);
1067 struct sk_buff *skb; 1066 struct sk_buff *skb;
@@ -1120,7 +1119,7 @@ int tcp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1120 1119
1121 sg = !!(sk->sk_route_caps & NETIF_F_SG); 1120 sg = !!(sk->sk_route_caps & NETIF_F_SG);
1122 1121
1123 while (iov_iter_count(&msg->msg_iter)) { 1122 while (msg_data_left(msg)) {
1124 int copy = 0; 1123 int copy = 0;
1125 int max = size_goal; 1124 int max = size_goal;
1126 1125
@@ -1164,8 +1163,8 @@ new_segment:
1164 } 1163 }
1165 1164
1166 /* Try to append data to the end of skb. */ 1165 /* Try to append data to the end of skb. */
1167 if (copy > iov_iter_count(&msg->msg_iter)) 1166 if (copy > msg_data_left(msg))
1168 copy = iov_iter_count(&msg->msg_iter); 1167 copy = msg_data_left(msg);
1169 1168
1170 /* Where to copy to? */ 1169 /* Where to copy to? */
1171 if (skb_availroom(skb) > 0) { 1170 if (skb_availroom(skb) > 0) {
@@ -1222,7 +1221,7 @@ new_segment:
1222 tcp_skb_pcount_set(skb, 0); 1221 tcp_skb_pcount_set(skb, 0);
1223 1222
1224 copied += copy; 1223 copied += copy;
1225 if (!iov_iter_count(&msg->msg_iter)) { 1224 if (!msg_data_left(msg)) {
1226 tcp_tx_timestamp(sk, skb); 1225 tcp_tx_timestamp(sk, skb);
1227 goto out; 1226 goto out;
1228 } 1227 }
@@ -1539,8 +1538,8 @@ EXPORT_SYMBOL(tcp_read_sock);
1539 * Probably, code can be easily improved even more. 1538 * Probably, code can be easily improved even more.
1540 */ 1539 */
1541 1540
1542int tcp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 1541int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
1543 size_t len, int nonblock, int flags, int *addr_len) 1542 int flags, int *addr_len)
1544{ 1543{
1545 struct tcp_sock *tp = tcp_sk(sk); 1544 struct tcp_sock *tp = tcp_sk(sk);
1546 int copied = 0; 1545 int copied = 0;
@@ -1914,18 +1913,19 @@ EXPORT_SYMBOL_GPL(tcp_set_state);
1914 1913
1915static const unsigned char new_state[16] = { 1914static const unsigned char new_state[16] = {
1916 /* current state: new state: action: */ 1915 /* current state: new state: action: */
1917 /* (Invalid) */ TCP_CLOSE, 1916 [0 /* (Invalid) */] = TCP_CLOSE,
1918 /* TCP_ESTABLISHED */ TCP_FIN_WAIT1 | TCP_ACTION_FIN, 1917 [TCP_ESTABLISHED] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1919 /* TCP_SYN_SENT */ TCP_CLOSE, 1918 [TCP_SYN_SENT] = TCP_CLOSE,
1920 /* TCP_SYN_RECV */ TCP_FIN_WAIT1 | TCP_ACTION_FIN, 1919 [TCP_SYN_RECV] = TCP_FIN_WAIT1 | TCP_ACTION_FIN,
1921 /* TCP_FIN_WAIT1 */ TCP_FIN_WAIT1, 1920 [TCP_FIN_WAIT1] = TCP_FIN_WAIT1,
1922 /* TCP_FIN_WAIT2 */ TCP_FIN_WAIT2, 1921 [TCP_FIN_WAIT2] = TCP_FIN_WAIT2,
1923 /* TCP_TIME_WAIT */ TCP_CLOSE, 1922 [TCP_TIME_WAIT] = TCP_CLOSE,
1924 /* TCP_CLOSE */ TCP_CLOSE, 1923 [TCP_CLOSE] = TCP_CLOSE,
1925 /* TCP_CLOSE_WAIT */ TCP_LAST_ACK | TCP_ACTION_FIN, 1924 [TCP_CLOSE_WAIT] = TCP_LAST_ACK | TCP_ACTION_FIN,
1926 /* TCP_LAST_ACK */ TCP_LAST_ACK, 1925 [TCP_LAST_ACK] = TCP_LAST_ACK,
1927 /* TCP_LISTEN */ TCP_CLOSE, 1926 [TCP_LISTEN] = TCP_CLOSE,
1928 /* TCP_CLOSING */ TCP_CLOSING, 1927 [TCP_CLOSING] = TCP_CLOSING,
1928 [TCP_NEW_SYN_RECV] = TCP_CLOSE, /* should not happen ! */
1929}; 1929};
1930 1930
1931static int tcp_close_state(struct sock *sk) 1931static int tcp_close_state(struct sock *sk)
@@ -2138,7 +2138,7 @@ adjudge_to_death:
2138 * aborted (e.g., closed with unread data) before 3WHS 2138 * aborted (e.g., closed with unread data) before 3WHS
2139 * finishes. 2139 * finishes.
2140 */ 2140 */
2141 if (req != NULL) 2141 if (req)
2142 reqsk_fastopen_remove(sk, req, false); 2142 reqsk_fastopen_remove(sk, req, false);
2143 inet_csk_destroy_sock(sk); 2143 inet_csk_destroy_sock(sk);
2144 } 2144 }
@@ -2595,6 +2595,7 @@ void tcp_get_info(const struct sock *sk, struct tcp_info *info)
2595 const struct tcp_sock *tp = tcp_sk(sk); 2595 const struct tcp_sock *tp = tcp_sk(sk);
2596 const struct inet_connection_sock *icsk = inet_csk(sk); 2596 const struct inet_connection_sock *icsk = inet_csk(sk);
2597 u32 now = tcp_time_stamp; 2597 u32 now = tcp_time_stamp;
2598 u32 rate;
2598 2599
2599 memset(info, 0, sizeof(*info)); 2600 memset(info, 0, sizeof(*info));
2600 2601
@@ -2655,10 +2656,11 @@ void tcp_get_info(const struct sock *sk, struct tcp_info *info)
2655 2656
2656 info->tcpi_total_retrans = tp->total_retrans; 2657 info->tcpi_total_retrans = tp->total_retrans;
2657 2658
2658 info->tcpi_pacing_rate = sk->sk_pacing_rate != ~0U ? 2659 rate = READ_ONCE(sk->sk_pacing_rate);
2659 sk->sk_pacing_rate : ~0ULL; 2660 info->tcpi_pacing_rate = rate != ~0U ? rate : ~0ULL;
2660 info->tcpi_max_pacing_rate = sk->sk_max_pacing_rate != ~0U ? 2661
2661 sk->sk_max_pacing_rate : ~0ULL; 2662 rate = READ_ONCE(sk->sk_max_pacing_rate);
2663 info->tcpi_max_pacing_rate = rate != ~0U ? rate : ~0ULL;
2662} 2664}
2663EXPORT_SYMBOL_GPL(tcp_get_info); 2665EXPORT_SYMBOL_GPL(tcp_get_info);
2664 2666
@@ -2776,7 +2778,7 @@ static int do_tcp_getsockopt(struct sock *sk, int level,
2776 break; 2778 break;
2777 2779
2778 case TCP_FASTOPEN: 2780 case TCP_FASTOPEN:
2779 if (icsk->icsk_accept_queue.fastopenq != NULL) 2781 if (icsk->icsk_accept_queue.fastopenq)
2780 val = icsk->icsk_accept_queue.fastopenq->max_qlen; 2782 val = icsk->icsk_accept_queue.fastopenq->max_qlen;
2781 else 2783 else
2782 val = 0; 2784 val = 0;
@@ -2960,7 +2962,7 @@ void tcp_done(struct sock *sk)
2960 2962
2961 tcp_set_state(sk, TCP_CLOSE); 2963 tcp_set_state(sk, TCP_CLOSE);
2962 tcp_clear_xmit_timers(sk); 2964 tcp_clear_xmit_timers(sk);
2963 if (req != NULL) 2965 if (req)
2964 reqsk_fastopen_remove(sk, req, false); 2966 reqsk_fastopen_remove(sk, req, false);
2965 2967
2966 sk->sk_shutdown = SHUTDOWN_MASK; 2968 sk->sk_shutdown = SHUTDOWN_MASK;
@@ -3001,12 +3003,11 @@ static void __init tcp_init_mem(void)
3001 3003
3002void __init tcp_init(void) 3004void __init tcp_init(void)
3003{ 3005{
3004 struct sk_buff *skb = NULL;
3005 unsigned long limit; 3006 unsigned long limit;
3006 int max_rshare, max_wshare, cnt; 3007 int max_rshare, max_wshare, cnt;
3007 unsigned int i; 3008 unsigned int i;
3008 3009
3009 BUILD_BUG_ON(sizeof(struct tcp_skb_cb) > sizeof(skb->cb)); 3010 sock_skb_cb_check_size(sizeof(struct tcp_skb_cb));
3010 3011
3011 percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL); 3012 percpu_counter_init(&tcp_sockets_allocated, 0, GFP_KERNEL);
3012 percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL); 3013 percpu_counter_init(&tcp_orphan_count, 0, GFP_KERNEL);
diff --git a/net/ipv4/tcp_cong.c b/net/ipv4/tcp_cong.c
index 62856e185a93..7a5ae50c80c8 100644
--- a/net/ipv4/tcp_cong.c
+++ b/net/ipv4/tcp_cong.c
@@ -83,7 +83,7 @@ int tcp_register_congestion_control(struct tcp_congestion_ops *ca)
83 ret = -EEXIST; 83 ret = -EEXIST;
84 } else { 84 } else {
85 list_add_tail_rcu(&ca->list, &tcp_cong_list); 85 list_add_tail_rcu(&ca->list, &tcp_cong_list);
86 pr_info("%s registered\n", ca->name); 86 pr_debug("%s registered\n", ca->name);
87 } 87 }
88 spin_unlock(&tcp_cong_list_lock); 88 spin_unlock(&tcp_cong_list_lock);
89 89
diff --git a/net/ipv4/tcp_dctcp.c b/net/ipv4/tcp_dctcp.c
index b504371af742..4376016f7fa5 100644
--- a/net/ipv4/tcp_dctcp.c
+++ b/net/ipv4/tcp_dctcp.c
@@ -277,7 +277,7 @@ static void dctcp_cwnd_event(struct sock *sk, enum tcp_ca_event ev)
277 } 277 }
278} 278}
279 279
280static void dctcp_get_info(struct sock *sk, u32 ext, struct sk_buff *skb) 280static int dctcp_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
281{ 281{
282 const struct dctcp *ca = inet_csk_ca(sk); 282 const struct dctcp *ca = inet_csk_ca(sk);
283 283
@@ -297,8 +297,9 @@ static void dctcp_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
297 info.dctcp_ab_tot = ca->acked_bytes_total; 297 info.dctcp_ab_tot = ca->acked_bytes_total;
298 } 298 }
299 299
300 nla_put(skb, INET_DIAG_DCTCPINFO, sizeof(info), &info); 300 return nla_put(skb, INET_DIAG_DCTCPINFO, sizeof(info), &info);
301 } 301 }
302 return 0;
302} 303}
303 304
304static struct tcp_congestion_ops dctcp __read_mostly = { 305static struct tcp_congestion_ops dctcp __read_mostly = {
diff --git a/net/ipv4/tcp_diag.c b/net/ipv4/tcp_diag.c
index 0d73f9ddb55b..79b34a0f4a4a 100644
--- a/net/ipv4/tcp_diag.c
+++ b/net/ipv4/tcp_diag.c
@@ -29,18 +29,18 @@ static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
29 r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0); 29 r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
30 r->idiag_wqueue = tp->write_seq - tp->snd_una; 30 r->idiag_wqueue = tp->write_seq - tp->snd_una;
31 } 31 }
32 if (info != NULL) 32 if (info)
33 tcp_get_info(sk, info); 33 tcp_get_info(sk, info);
34} 34}
35 35
36static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 36static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
37 struct inet_diag_req_v2 *r, struct nlattr *bc) 37 const struct inet_diag_req_v2 *r, struct nlattr *bc)
38{ 38{
39 inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc); 39 inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
40} 40}
41 41
42static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, 42static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
43 struct inet_diag_req_v2 *req) 43 const struct inet_diag_req_v2 *req)
44{ 44{
45 return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req); 45 return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
46} 46}
diff --git a/net/ipv4/tcp_fastopen.c b/net/ipv4/tcp_fastopen.c
index ea82fd492c1b..e3d87aca6be8 100644
--- a/net/ipv4/tcp_fastopen.c
+++ b/net/ipv4/tcp_fastopen.c
@@ -141,7 +141,7 @@ static bool tcp_fastopen_create_child(struct sock *sk,
141 req->sk = NULL; 141 req->sk = NULL;
142 142
143 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL); 143 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
144 if (child == NULL) 144 if (!child)
145 return false; 145 return false;
146 146
147 spin_lock(&queue->fastopenq->lock); 147 spin_lock(&queue->fastopenq->lock);
@@ -155,12 +155,7 @@ static bool tcp_fastopen_create_child(struct sock *sk,
155 tp = tcp_sk(child); 155 tp = tcp_sk(child);
156 156
157 tp->fastopen_rsk = req; 157 tp->fastopen_rsk = req;
158 /* Do a hold on the listner sk so that if the listener is being 158 tcp_rsk(req)->tfo_listener = true;
159 * closed, the child that has been accepted can live on and still
160 * access listen_lock.
161 */
162 sock_hold(sk);
163 tcp_rsk(req)->listener = sk;
164 159
165 /* RFC1323: The window in SYN & SYN/ACK segments is never 160 /* RFC1323: The window in SYN & SYN/ACK segments is never
166 * scaled. So correct it appropriately. 161 * scaled. So correct it appropriately.
@@ -174,6 +169,7 @@ static bool tcp_fastopen_create_child(struct sock *sk,
174 inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS, 169 inet_csk_reset_xmit_timer(child, ICSK_TIME_RETRANS,
175 TCP_TIMEOUT_INIT, TCP_RTO_MAX); 170 TCP_TIMEOUT_INIT, TCP_RTO_MAX);
176 171
172 atomic_set(&req->rsk_refcnt, 1);
177 /* Add the child socket directly into the accept queue */ 173 /* Add the child socket directly into the accept queue */
178 inet_csk_reqsk_queue_add(sk, req, child); 174 inet_csk_reqsk_queue_add(sk, req, child);
179 175
@@ -218,10 +214,9 @@ static bool tcp_fastopen_create_child(struct sock *sk,
218 sk->sk_data_ready(sk); 214 sk->sk_data_ready(sk);
219 bh_unlock_sock(child); 215 bh_unlock_sock(child);
220 sock_put(child); 216 sock_put(child);
221 WARN_ON(req->sk == NULL); 217 WARN_ON(!req->sk);
222 return true; 218 return true;
223} 219}
224EXPORT_SYMBOL(tcp_fastopen_create_child);
225 220
226static bool tcp_fastopen_queue_check(struct sock *sk) 221static bool tcp_fastopen_queue_check(struct sock *sk)
227{ 222{
@@ -238,14 +233,14 @@ static bool tcp_fastopen_queue_check(struct sock *sk)
238 * temporarily vs a server not supporting Fast Open at all. 233 * temporarily vs a server not supporting Fast Open at all.
239 */ 234 */
240 fastopenq = inet_csk(sk)->icsk_accept_queue.fastopenq; 235 fastopenq = inet_csk(sk)->icsk_accept_queue.fastopenq;
241 if (fastopenq == NULL || fastopenq->max_qlen == 0) 236 if (!fastopenq || fastopenq->max_qlen == 0)
242 return false; 237 return false;
243 238
244 if (fastopenq->qlen >= fastopenq->max_qlen) { 239 if (fastopenq->qlen >= fastopenq->max_qlen) {
245 struct request_sock *req1; 240 struct request_sock *req1;
246 spin_lock(&fastopenq->lock); 241 spin_lock(&fastopenq->lock);
247 req1 = fastopenq->rskq_rst_head; 242 req1 = fastopenq->rskq_rst_head;
248 if ((req1 == NULL) || time_after(req1->expires, jiffies)) { 243 if (!req1 || time_after(req1->rsk_timer.expires, jiffies)) {
249 spin_unlock(&fastopenq->lock); 244 spin_unlock(&fastopenq->lock);
250 NET_INC_STATS_BH(sock_net(sk), 245 NET_INC_STATS_BH(sock_net(sk),
251 LINUX_MIB_TCPFASTOPENLISTENOVERFLOW); 246 LINUX_MIB_TCPFASTOPENLISTENOVERFLOW);
@@ -254,7 +249,7 @@ static bool tcp_fastopen_queue_check(struct sock *sk)
254 fastopenq->rskq_rst_head = req1->dl_next; 249 fastopenq->rskq_rst_head = req1->dl_next;
255 fastopenq->qlen--; 250 fastopenq->qlen--;
256 spin_unlock(&fastopenq->lock); 251 spin_unlock(&fastopenq->lock);
257 reqsk_free(req1); 252 reqsk_put(req1);
258 } 253 }
259 return true; 254 return true;
260} 255}
@@ -308,6 +303,7 @@ fastopen:
308 } else if (foc->len > 0) /* Client presents an invalid cookie */ 303 } else if (foc->len > 0) /* Client presents an invalid cookie */
309 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL); 304 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPFASTOPENPASSIVEFAIL);
310 305
306 valid_foc.exp = foc->exp;
311 *foc = valid_foc; 307 *foc = valid_foc;
312 return false; 308 return false;
313} 309}
diff --git a/net/ipv4/tcp_illinois.c b/net/ipv4/tcp_illinois.c
index 1d5a30a90adf..67476f085e48 100644
--- a/net/ipv4/tcp_illinois.c
+++ b/net/ipv4/tcp_illinois.c
@@ -300,8 +300,7 @@ static u32 tcp_illinois_ssthresh(struct sock *sk)
300} 300}
301 301
302/* Extract info for Tcp socket info provided via netlink. */ 302/* Extract info for Tcp socket info provided via netlink. */
303static void tcp_illinois_info(struct sock *sk, u32 ext, 303static int tcp_illinois_info(struct sock *sk, u32 ext, struct sk_buff *skb)
304 struct sk_buff *skb)
305{ 304{
306 const struct illinois *ca = inet_csk_ca(sk); 305 const struct illinois *ca = inet_csk_ca(sk);
307 306
@@ -318,8 +317,9 @@ static void tcp_illinois_info(struct sock *sk, u32 ext,
318 do_div(t, info.tcpv_rttcnt); 317 do_div(t, info.tcpv_rttcnt);
319 info.tcpv_rtt = t; 318 info.tcpv_rtt = t;
320 } 319 }
321 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info); 320 return nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
322 } 321 }
322 return 0;
323} 323}
324 324
325static struct tcp_congestion_ops tcp_illinois __read_mostly = { 325static struct tcp_congestion_ops tcp_illinois __read_mostly = {
diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
index f501ac048366..a7ef679dd3ea 100644
--- a/net/ipv4/tcp_input.c
+++ b/net/ipv4/tcp_input.c
@@ -866,7 +866,7 @@ static void tcp_update_reordering(struct sock *sk, const int metric,
866/* This must be called before lost_out is incremented */ 866/* This must be called before lost_out is incremented */
867static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb) 867static void tcp_verify_retransmit_hint(struct tcp_sock *tp, struct sk_buff *skb)
868{ 868{
869 if ((tp->retransmit_skb_hint == NULL) || 869 if (!tp->retransmit_skb_hint ||
870 before(TCP_SKB_CB(skb)->seq, 870 before(TCP_SKB_CB(skb)->seq,
871 TCP_SKB_CB(tp->retransmit_skb_hint)->seq)) 871 TCP_SKB_CB(tp->retransmit_skb_hint)->seq))
872 tp->retransmit_skb_hint = skb; 872 tp->retransmit_skb_hint = skb;
@@ -1256,7 +1256,7 @@ static u8 tcp_sacktag_one(struct sock *sk,
1256 fack_count += pcount; 1256 fack_count += pcount;
1257 1257
1258 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */ 1258 /* Lost marker hint past SACKed? Tweak RFC3517 cnt */
1259 if (!tcp_is_fack(tp) && (tp->lost_skb_hint != NULL) && 1259 if (!tcp_is_fack(tp) && tp->lost_skb_hint &&
1260 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq)) 1260 before(start_seq, TCP_SKB_CB(tp->lost_skb_hint)->seq))
1261 tp->lost_cnt_hint += pcount; 1261 tp->lost_cnt_hint += pcount;
1262 1262
@@ -1535,7 +1535,7 @@ static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1535 if (!before(TCP_SKB_CB(skb)->seq, end_seq)) 1535 if (!before(TCP_SKB_CB(skb)->seq, end_seq))
1536 break; 1536 break;
1537 1537
1538 if ((next_dup != NULL) && 1538 if (next_dup &&
1539 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) { 1539 before(TCP_SKB_CB(skb)->seq, next_dup->end_seq)) {
1540 in_sack = tcp_match_skb_to_sack(sk, skb, 1540 in_sack = tcp_match_skb_to_sack(sk, skb,
1541 next_dup->start_seq, 1541 next_dup->start_seq,
@@ -1551,7 +1551,7 @@ static struct sk_buff *tcp_sacktag_walk(struct sk_buff *skb, struct sock *sk,
1551 if (in_sack <= 0) { 1551 if (in_sack <= 0) {
1552 tmp = tcp_shift_skb_data(sk, skb, state, 1552 tmp = tcp_shift_skb_data(sk, skb, state,
1553 start_seq, end_seq, dup_sack); 1553 start_seq, end_seq, dup_sack);
1554 if (tmp != NULL) { 1554 if (tmp) {
1555 if (tmp != skb) { 1555 if (tmp != skb) {
1556 skb = tmp; 1556 skb = tmp;
1557 continue; 1557 continue;
@@ -1614,7 +1614,7 @@ static struct sk_buff *tcp_maybe_skipping_dsack(struct sk_buff *skb,
1614 struct tcp_sacktag_state *state, 1614 struct tcp_sacktag_state *state,
1615 u32 skip_to_seq) 1615 u32 skip_to_seq)
1616{ 1616{
1617 if (next_dup == NULL) 1617 if (!next_dup)
1618 return skb; 1618 return skb;
1619 1619
1620 if (before(next_dup->start_seq, skip_to_seq)) { 1620 if (before(next_dup->start_seq, skip_to_seq)) {
@@ -1783,7 +1783,7 @@ tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
1783 if (tcp_highest_sack_seq(tp) == cache->end_seq) { 1783 if (tcp_highest_sack_seq(tp) == cache->end_seq) {
1784 /* ...but better entrypoint exists! */ 1784 /* ...but better entrypoint exists! */
1785 skb = tcp_highest_sack(sk); 1785 skb = tcp_highest_sack(sk);
1786 if (skb == NULL) 1786 if (!skb)
1787 break; 1787 break;
1788 state.fack_count = tp->fackets_out; 1788 state.fack_count = tp->fackets_out;
1789 cache++; 1789 cache++;
@@ -1798,7 +1798,7 @@ tcp_sacktag_write_queue(struct sock *sk, const struct sk_buff *ack_skb,
1798 1798
1799 if (!before(start_seq, tcp_highest_sack_seq(tp))) { 1799 if (!before(start_seq, tcp_highest_sack_seq(tp))) {
1800 skb = tcp_highest_sack(sk); 1800 skb = tcp_highest_sack(sk);
1801 if (skb == NULL) 1801 if (!skb)
1802 break; 1802 break;
1803 state.fack_count = tp->fackets_out; 1803 state.fack_count = tp->fackets_out;
1804 } 1804 }
@@ -3099,17 +3099,15 @@ static int tcp_clean_rtx_queue(struct sock *sk, int prior_fackets,
3099 if (sacked & TCPCB_SACKED_RETRANS) 3099 if (sacked & TCPCB_SACKED_RETRANS)
3100 tp->retrans_out -= acked_pcount; 3100 tp->retrans_out -= acked_pcount;
3101 flag |= FLAG_RETRANS_DATA_ACKED; 3101 flag |= FLAG_RETRANS_DATA_ACKED;
3102 } else { 3102 } else if (!(sacked & TCPCB_SACKED_ACKED)) {
3103 last_ackt = skb->skb_mstamp; 3103 last_ackt = skb->skb_mstamp;
3104 WARN_ON_ONCE(last_ackt.v64 == 0); 3104 WARN_ON_ONCE(last_ackt.v64 == 0);
3105 if (!first_ackt.v64) 3105 if (!first_ackt.v64)
3106 first_ackt = last_ackt; 3106 first_ackt = last_ackt;
3107 3107
3108 if (!(sacked & TCPCB_SACKED_ACKED)) { 3108 reord = min(pkts_acked, reord);
3109 reord = min(pkts_acked, reord); 3109 if (!after(scb->end_seq, tp->high_seq))
3110 if (!after(scb->end_seq, tp->high_seq)) 3110 flag |= FLAG_ORIG_SACK_ACKED;
3111 flag |= FLAG_ORIG_SACK_ACKED;
3112 }
3113 } 3111 }
3114 3112
3115 if (sacked & TCPCB_SACKED_ACKED) 3113 if (sacked & TCPCB_SACKED_ACKED)
@@ -3322,6 +3320,36 @@ static int tcp_ack_update_window(struct sock *sk, const struct sk_buff *skb, u32
3322 return flag; 3320 return flag;
3323} 3321}
3324 3322
3323/* Return true if we're currently rate-limiting out-of-window ACKs and
3324 * thus shouldn't send a dupack right now. We rate-limit dupacks in
3325 * response to out-of-window SYNs or ACKs to mitigate ACK loops or DoS
3326 * attacks that send repeated SYNs or ACKs for the same connection. To
3327 * do this, we do not send a duplicate SYNACK or ACK if the remote
3328 * endpoint is sending out-of-window SYNs or pure ACKs at a high rate.
3329 */
3330bool tcp_oow_rate_limited(struct net *net, const struct sk_buff *skb,
3331 int mib_idx, u32 *last_oow_ack_time)
3332{
3333 /* Data packets without SYNs are not likely part of an ACK loop. */
3334 if ((TCP_SKB_CB(skb)->seq != TCP_SKB_CB(skb)->end_seq) &&
3335 !tcp_hdr(skb)->syn)
3336 goto not_rate_limited;
3337
3338 if (*last_oow_ack_time) {
3339 s32 elapsed = (s32)(tcp_time_stamp - *last_oow_ack_time);
3340
3341 if (0 <= elapsed && elapsed < sysctl_tcp_invalid_ratelimit) {
3342 NET_INC_STATS_BH(net, mib_idx);
3343 return true; /* rate-limited: don't send yet! */
3344 }
3345 }
3346
3347 *last_oow_ack_time = tcp_time_stamp;
3348
3349not_rate_limited:
3350 return false; /* not rate-limited: go ahead, send dupack now! */
3351}
3352
3325/* RFC 5961 7 [ACK Throttling] */ 3353/* RFC 5961 7 [ACK Throttling] */
3326static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb) 3354static void tcp_send_challenge_ack(struct sock *sk, const struct sk_buff *skb)
3327{ 3355{
@@ -3573,6 +3601,23 @@ old_ack:
3573 return 0; 3601 return 0;
3574} 3602}
3575 3603
3604static void tcp_parse_fastopen_option(int len, const unsigned char *cookie,
3605 bool syn, struct tcp_fastopen_cookie *foc,
3606 bool exp_opt)
3607{
3608 /* Valid only in SYN or SYN-ACK with an even length. */
3609 if (!foc || !syn || len < 0 || (len & 1))
3610 return;
3611
3612 if (len >= TCP_FASTOPEN_COOKIE_MIN &&
3613 len <= TCP_FASTOPEN_COOKIE_MAX)
3614 memcpy(foc->val, cookie, len);
3615 else if (len != 0)
3616 len = -1;
3617 foc->len = len;
3618 foc->exp = exp_opt;
3619}
3620
3576/* Look for tcp options. Normally only called on SYN and SYNACK packets. 3621/* Look for tcp options. Normally only called on SYN and SYNACK packets.
3577 * But, this can also be called on packets in the established flow when 3622 * But, this can also be called on packets in the established flow when
3578 * the fast version below fails. 3623 * the fast version below fails.
@@ -3662,21 +3707,22 @@ void tcp_parse_options(const struct sk_buff *skb,
3662 */ 3707 */
3663 break; 3708 break;
3664#endif 3709#endif
3710 case TCPOPT_FASTOPEN:
3711 tcp_parse_fastopen_option(
3712 opsize - TCPOLEN_FASTOPEN_BASE,
3713 ptr, th->syn, foc, false);
3714 break;
3715
3665 case TCPOPT_EXP: 3716 case TCPOPT_EXP:
3666 /* Fast Open option shares code 254 using a 3717 /* Fast Open option shares code 254 using a
3667 * 16 bits magic number. It's valid only in 3718 * 16 bits magic number.
3668 * SYN or SYN-ACK with an even size.
3669 */ 3719 */
3670 if (opsize < TCPOLEN_EXP_FASTOPEN_BASE || 3720 if (opsize >= TCPOLEN_EXP_FASTOPEN_BASE &&
3671 get_unaligned_be16(ptr) != TCPOPT_FASTOPEN_MAGIC || 3721 get_unaligned_be16(ptr) ==
3672 foc == NULL || !th->syn || (opsize & 1)) 3722 TCPOPT_FASTOPEN_MAGIC)
3673 break; 3723 tcp_parse_fastopen_option(opsize -
3674 foc->len = opsize - TCPOLEN_EXP_FASTOPEN_BASE; 3724 TCPOLEN_EXP_FASTOPEN_BASE,
3675 if (foc->len >= TCP_FASTOPEN_COOKIE_MIN && 3725 ptr + 2, th->syn, foc, true);
3676 foc->len <= TCP_FASTOPEN_COOKIE_MAX)
3677 memcpy(foc->val, ptr + 2, foc->len);
3678 else if (foc->len != 0)
3679 foc->len = -1;
3680 break; 3726 break;
3681 3727
3682 } 3728 }
@@ -4640,7 +4686,7 @@ static void tcp_collapse_ofo_queue(struct sock *sk)
4640 struct sk_buff *head; 4686 struct sk_buff *head;
4641 u32 start, end; 4687 u32 start, end;
4642 4688
4643 if (skb == NULL) 4689 if (!skb)
4644 return; 4690 return;
4645 4691
4646 start = TCP_SKB_CB(skb)->seq; 4692 start = TCP_SKB_CB(skb)->seq;
@@ -5095,7 +5141,7 @@ void tcp_rcv_established(struct sock *sk, struct sk_buff *skb,
5095{ 5141{
5096 struct tcp_sock *tp = tcp_sk(sk); 5142 struct tcp_sock *tp = tcp_sk(sk);
5097 5143
5098 if (unlikely(sk->sk_rx_dst == NULL)) 5144 if (unlikely(!sk->sk_rx_dst))
5099 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb); 5145 inet_csk(sk)->icsk_af_ops->sk_rx_dst_set(sk, skb);
5100 /* 5146 /*
5101 * Header prediction. 5147 * Header prediction.
@@ -5292,7 +5338,7 @@ void tcp_finish_connect(struct sock *sk, struct sk_buff *skb)
5292 5338
5293 tcp_set_state(sk, TCP_ESTABLISHED); 5339 tcp_set_state(sk, TCP_ESTABLISHED);
5294 5340
5295 if (skb != NULL) { 5341 if (skb) {
5296 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb); 5342 icsk->icsk_af_ops->sk_rx_dst_set(sk, skb);
5297 security_inet_conn_established(sk, skb); 5343 security_inet_conn_established(sk, skb);
5298 } 5344 }
@@ -5330,8 +5376,8 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5330{ 5376{
5331 struct tcp_sock *tp = tcp_sk(sk); 5377 struct tcp_sock *tp = tcp_sk(sk);
5332 struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL; 5378 struct sk_buff *data = tp->syn_data ? tcp_write_queue_head(sk) : NULL;
5333 u16 mss = tp->rx_opt.mss_clamp; 5379 u16 mss = tp->rx_opt.mss_clamp, try_exp = 0;
5334 bool syn_drop; 5380 bool syn_drop = false;
5335 5381
5336 if (mss == tp->rx_opt.user_mss) { 5382 if (mss == tp->rx_opt.user_mss) {
5337 struct tcp_options_received opt; 5383 struct tcp_options_received opt;
@@ -5343,16 +5389,25 @@ static bool tcp_rcv_fastopen_synack(struct sock *sk, struct sk_buff *synack,
5343 mss = opt.mss_clamp; 5389 mss = opt.mss_clamp;
5344 } 5390 }
5345 5391
5346 if (!tp->syn_fastopen) /* Ignore an unsolicited cookie */ 5392 if (!tp->syn_fastopen) {
5393 /* Ignore an unsolicited cookie */
5347 cookie->len = -1; 5394 cookie->len = -1;
5395 } else if (tp->total_retrans) {
5396 /* SYN timed out and the SYN-ACK neither has a cookie nor
5397 * acknowledges data. Presumably the remote received only
5398 * the retransmitted (regular) SYNs: either the original
5399 * SYN-data or the corresponding SYN-ACK was dropped.
5400 */
5401 syn_drop = (cookie->len < 0 && data);
5402 } else if (cookie->len < 0 && !tp->syn_data) {
5403 /* We requested a cookie but didn't get it. If we did not use
5404 * the (old) exp opt format then try so next time (try_exp=1).
5405 * Otherwise we go back to use the RFC7413 opt (try_exp=2).
5406 */
5407 try_exp = tp->syn_fastopen_exp ? 2 : 1;
5408 }
5348 5409
5349 /* The SYN-ACK neither has cookie nor acknowledges the data. Presumably 5410 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop, try_exp);
5350 * the remote receives only the retransmitted (regular) SYNs: either
5351 * the original SYN-data or the corresponding SYN-ACK is lost.
5352 */
5353 syn_drop = (cookie->len <= 0 && data && tp->total_retrans);
5354
5355 tcp_fastopen_cache_set(sk, mss, cookie, syn_drop);
5356 5411
5357 if (data) { /* Retransmit unacked data in SYN */ 5412 if (data) { /* Retransmit unacked data in SYN */
5358 tcp_for_write_queue_from(data, sk) { 5413 tcp_for_write_queue_from(data, sk) {
@@ -5661,11 +5716,11 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5661 } 5716 }
5662 5717
5663 req = tp->fastopen_rsk; 5718 req = tp->fastopen_rsk;
5664 if (req != NULL) { 5719 if (req) {
5665 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV && 5720 WARN_ON_ONCE(sk->sk_state != TCP_SYN_RECV &&
5666 sk->sk_state != TCP_FIN_WAIT1); 5721 sk->sk_state != TCP_FIN_WAIT1);
5667 5722
5668 if (tcp_check_req(sk, skb, req, NULL, true) == NULL) 5723 if (!tcp_check_req(sk, skb, req, true))
5669 goto discard; 5724 goto discard;
5670 } 5725 }
5671 5726
@@ -5751,7 +5806,7 @@ int tcp_rcv_state_process(struct sock *sk, struct sk_buff *skb,
5751 * ACK we have received, this would have acknowledged 5806 * ACK we have received, this would have acknowledged
5752 * our SYNACK so stop the SYNACK timer. 5807 * our SYNACK so stop the SYNACK timer.
5753 */ 5808 */
5754 if (req != NULL) { 5809 if (req) {
5755 /* Return RST if ack_seq is invalid. 5810 /* Return RST if ack_seq is invalid.
5756 * Note that RFC793 only says to generate a 5811 * Note that RFC793 only says to generate a
5757 * DUPACK for it but for TCP Fast Open it seems 5812 * DUPACK for it but for TCP Fast Open it seems
@@ -5913,6 +5968,80 @@ static void tcp_ecn_create_request(struct request_sock *req,
5913 inet_rsk(req)->ecn_ok = 1; 5968 inet_rsk(req)->ecn_ok = 1;
5914} 5969}
5915 5970
5971static void tcp_openreq_init(struct request_sock *req,
5972 const struct tcp_options_received *rx_opt,
5973 struct sk_buff *skb, const struct sock *sk)
5974{
5975 struct inet_request_sock *ireq = inet_rsk(req);
5976
5977 req->rcv_wnd = 0; /* So that tcp_send_synack() knows! */
5978 req->cookie_ts = 0;
5979 tcp_rsk(req)->rcv_isn = TCP_SKB_CB(skb)->seq;
5980 tcp_rsk(req)->rcv_nxt = TCP_SKB_CB(skb)->seq + 1;
5981 tcp_rsk(req)->snt_synack = tcp_time_stamp;
5982 tcp_rsk(req)->last_oow_ack_time = 0;
5983 req->mss = rx_opt->mss_clamp;
5984 req->ts_recent = rx_opt->saw_tstamp ? rx_opt->rcv_tsval : 0;
5985 ireq->tstamp_ok = rx_opt->tstamp_ok;
5986 ireq->sack_ok = rx_opt->sack_ok;
5987 ireq->snd_wscale = rx_opt->snd_wscale;
5988 ireq->wscale_ok = rx_opt->wscale_ok;
5989 ireq->acked = 0;
5990 ireq->ecn_ok = 0;
5991 ireq->ir_rmt_port = tcp_hdr(skb)->source;
5992 ireq->ir_num = ntohs(tcp_hdr(skb)->dest);
5993 ireq->ir_mark = inet_request_mark(sk, skb);
5994}
5995
5996struct request_sock *inet_reqsk_alloc(const struct request_sock_ops *ops,
5997 struct sock *sk_listener)
5998{
5999 struct request_sock *req = reqsk_alloc(ops, sk_listener);
6000
6001 if (req) {
6002 struct inet_request_sock *ireq = inet_rsk(req);
6003
6004 kmemcheck_annotate_bitfield(ireq, flags);
6005 ireq->opt = NULL;
6006 atomic64_set(&ireq->ir_cookie, 0);
6007 ireq->ireq_state = TCP_NEW_SYN_RECV;
6008 write_pnet(&ireq->ireq_net, sock_net(sk_listener));
6009 ireq->ireq_family = sk_listener->sk_family;
6010 }
6011
6012 return req;
6013}
6014EXPORT_SYMBOL(inet_reqsk_alloc);
6015
6016/*
6017 * Return true if a syncookie should be sent
6018 */
6019static bool tcp_syn_flood_action(struct sock *sk,
6020 const struct sk_buff *skb,
6021 const char *proto)
6022{
6023 const char *msg = "Dropping request";
6024 bool want_cookie = false;
6025 struct listen_sock *lopt;
6026
6027#ifdef CONFIG_SYN_COOKIES
6028 if (sysctl_tcp_syncookies) {
6029 msg = "Sending cookies";
6030 want_cookie = true;
6031 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
6032 } else
6033#endif
6034 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
6035
6036 lopt = inet_csk(sk)->icsk_accept_queue.listen_opt;
6037 if (!lopt->synflood_warned && sysctl_tcp_syncookies != 2) {
6038 lopt->synflood_warned = 1;
6039 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
6040 proto, ntohs(tcp_hdr(skb)->dest), msg);
6041 }
6042 return want_cookie;
6043}
6044
5916int tcp_conn_request(struct request_sock_ops *rsk_ops, 6045int tcp_conn_request(struct request_sock_ops *rsk_ops,
5917 const struct tcp_request_sock_ops *af_ops, 6046 const struct tcp_request_sock_ops *af_ops,
5918 struct sock *sk, struct sk_buff *skb) 6047 struct sock *sk, struct sk_buff *skb)
@@ -5950,7 +6079,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
5950 goto drop; 6079 goto drop;
5951 } 6080 }
5952 6081
5953 req = inet_reqsk_alloc(rsk_ops); 6082 req = inet_reqsk_alloc(rsk_ops, sk);
5954 if (!req) 6083 if (!req)
5955 goto drop; 6084 goto drop;
5956 6085
@@ -5967,6 +6096,9 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
5967 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; 6096 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
5968 tcp_openreq_init(req, &tmp_opt, skb, sk); 6097 tcp_openreq_init(req, &tmp_opt, skb, sk);
5969 6098
6099 /* Note: tcp_v6_init_req() might override ir_iif for link locals */
6100 inet_rsk(req)->ir_iif = sk->sk_bound_dev_if;
6101
5970 af_ops->init_req(req, sk, skb); 6102 af_ops->init_req(req, sk, skb);
5971 6103
5972 if (security_inet_conn_request(sk, skb, req)) 6104 if (security_inet_conn_request(sk, skb, req))
@@ -6039,7 +6171,7 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
6039 if (err || want_cookie) 6171 if (err || want_cookie)
6040 goto drop_and_free; 6172 goto drop_and_free;
6041 6173
6042 tcp_rsk(req)->listener = NULL; 6174 tcp_rsk(req)->tfo_listener = false;
6043 af_ops->queue_hash_add(sk, req, TCP_TIMEOUT_INIT); 6175 af_ops->queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
6044 } 6176 }
6045 6177
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index f1756ee02207..3571f2be4470 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -122,7 +122,7 @@ int tcp_twsk_unique(struct sock *sk, struct sock *sktw, void *twp)
122 and use initial timestamp retrieved from peer table. 122 and use initial timestamp retrieved from peer table.
123 */ 123 */
124 if (tcptw->tw_ts_recent_stamp && 124 if (tcptw->tw_ts_recent_stamp &&
125 (twp == NULL || (sysctl_tcp_tw_reuse && 125 (!twp || (sysctl_tcp_tw_reuse &&
126 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) { 126 get_seconds() - tcptw->tw_ts_recent_stamp > 1))) {
127 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2; 127 tp->write_seq = tcptw->tw_snd_nxt + 65535 + 2;
128 if (tp->write_seq == 0) 128 if (tp->write_seq == 0)
@@ -189,7 +189,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
189 189
190 if (!inet->inet_saddr) 190 if (!inet->inet_saddr)
191 inet->inet_saddr = fl4->saddr; 191 inet->inet_saddr = fl4->saddr;
192 inet->inet_rcv_saddr = inet->inet_saddr; 192 sk_rcv_saddr_set(sk, inet->inet_saddr);
193 193
194 if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) { 194 if (tp->rx_opt.ts_recent_stamp && inet->inet_daddr != daddr) {
195 /* Reset inherited state */ 195 /* Reset inherited state */
@@ -204,7 +204,7 @@ int tcp_v4_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
204 tcp_fetch_timewait_stamp(sk, &rt->dst); 204 tcp_fetch_timewait_stamp(sk, &rt->dst);
205 205
206 inet->inet_dport = usin->sin_port; 206 inet->inet_dport = usin->sin_port;
207 inet->inet_daddr = daddr; 207 sk_daddr_set(sk, daddr);
208 208
209 inet_csk(sk)->icsk_ext_hdr_len = 0; 209 inet_csk(sk)->icsk_ext_hdr_len = 0;
210 if (inet_opt) 210 if (inet_opt)
@@ -310,6 +310,34 @@ static void do_redirect(struct sk_buff *skb, struct sock *sk)
310 dst->ops->redirect(dst, sk, skb); 310 dst->ops->redirect(dst, sk, skb);
311} 311}
312 312
313
314/* handle ICMP messages on TCP_NEW_SYN_RECV request sockets */
315void tcp_req_err(struct sock *sk, u32 seq)
316{
317 struct request_sock *req = inet_reqsk(sk);
318 struct net *net = sock_net(sk);
319
320 /* ICMPs are not backlogged, hence we cannot get
321 * an established socket here.
322 */
323 WARN_ON(req->sk);
324
325 if (seq != tcp_rsk(req)->snt_isn) {
326 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
327 reqsk_put(req);
328 } else {
329 /*
330 * Still in SYN_RECV, just remove it silently.
331 * There is no good way to pass the error to the newly
332 * created socket, and POSIX does not want network
333 * errors returned from accept().
334 */
335 NET_INC_STATS_BH(net, LINUX_MIB_LISTENDROPS);
336 inet_csk_reqsk_queue_drop(req->rsk_listener, req);
337 }
338}
339EXPORT_SYMBOL(tcp_req_err);
340
313/* 341/*
314 * This routine is called by the ICMP module when it gets some 342 * This routine is called by the ICMP module when it gets some
315 * sort of error condition. If err < 0 then the socket should 343 * sort of error condition. If err < 0 then the socket should
@@ -343,8 +371,9 @@ void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
343 int err; 371 int err;
344 struct net *net = dev_net(icmp_skb->dev); 372 struct net *net = dev_net(icmp_skb->dev);
345 373
346 sk = inet_lookup(net, &tcp_hashinfo, iph->daddr, th->dest, 374 sk = __inet_lookup_established(net, &tcp_hashinfo, iph->daddr,
347 iph->saddr, th->source, inet_iif(icmp_skb)); 375 th->dest, iph->saddr, ntohs(th->source),
376 inet_iif(icmp_skb));
348 if (!sk) { 377 if (!sk) {
349 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 378 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
350 return; 379 return;
@@ -353,6 +382,9 @@ void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
353 inet_twsk_put(inet_twsk(sk)); 382 inet_twsk_put(inet_twsk(sk));
354 return; 383 return;
355 } 384 }
385 seq = ntohl(th->seq);
386 if (sk->sk_state == TCP_NEW_SYN_RECV)
387 return tcp_req_err(sk, seq);
356 388
357 bh_lock_sock(sk); 389 bh_lock_sock(sk);
358 /* If too many ICMPs get dropped on busy 390 /* If too many ICMPs get dropped on busy
@@ -374,7 +406,6 @@ void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
374 406
375 icsk = inet_csk(sk); 407 icsk = inet_csk(sk);
376 tp = tcp_sk(sk); 408 tp = tcp_sk(sk);
377 seq = ntohl(th->seq);
378 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */ 409 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
379 fastopen = tp->fastopen_rsk; 410 fastopen = tp->fastopen_rsk;
380 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una; 411 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
@@ -458,42 +489,12 @@ void tcp_v4_err(struct sk_buff *icmp_skb, u32 info)
458 } 489 }
459 490
460 switch (sk->sk_state) { 491 switch (sk->sk_state) {
461 struct request_sock *req, **prev;
462 case TCP_LISTEN:
463 if (sock_owned_by_user(sk))
464 goto out;
465
466 req = inet_csk_search_req(sk, &prev, th->dest,
467 iph->daddr, iph->saddr);
468 if (!req)
469 goto out;
470
471 /* ICMPs are not backlogged, hence we cannot get
472 an established socket here.
473 */
474 WARN_ON(req->sk);
475
476 if (seq != tcp_rsk(req)->snt_isn) {
477 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
478 goto out;
479 }
480
481 /*
482 * Still in SYN_RECV, just remove it silently.
483 * There is no good way to pass the error to the newly
484 * created socket, and POSIX does not want network
485 * errors returned from accept().
486 */
487 inet_csk_reqsk_queue_drop(sk, req, prev);
488 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
489 goto out;
490
491 case TCP_SYN_SENT: 492 case TCP_SYN_SENT:
492 case TCP_SYN_RECV: 493 case TCP_SYN_RECV:
493 /* Only in fast or simultaneous open. If a fast open socket is 494 /* Only in fast or simultaneous open. If a fast open socket is
494 * is already accepted it is treated as a connected one below. 495 * is already accepted it is treated as a connected one below.
495 */ 496 */
496 if (fastopen && fastopen->sk == NULL) 497 if (fastopen && !fastopen->sk)
497 break; 498 break;
498 499
499 if (!sock_owned_by_user(sk)) { 500 if (!sock_owned_by_user(sk)) {
@@ -647,7 +648,7 @@ static void tcp_v4_send_reset(struct sock *sk, struct sk_buff *skb)
647 if (!key) 648 if (!key)
648 goto release_sk1; 649 goto release_sk1;
649 650
650 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, NULL, skb); 651 genhash = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
651 if (genhash || memcmp(hash_location, newhash, 16) != 0) 652 if (genhash || memcmp(hash_location, newhash, 16) != 0)
652 goto release_sk1; 653 goto release_sk1;
653 } else { 654 } else {
@@ -855,35 +856,6 @@ static void tcp_v4_reqsk_destructor(struct request_sock *req)
855 kfree(inet_rsk(req)->opt); 856 kfree(inet_rsk(req)->opt);
856} 857}
857 858
858/*
859 * Return true if a syncookie should be sent
860 */
861bool tcp_syn_flood_action(struct sock *sk,
862 const struct sk_buff *skb,
863 const char *proto)
864{
865 const char *msg = "Dropping request";
866 bool want_cookie = false;
867 struct listen_sock *lopt;
868
869#ifdef CONFIG_SYN_COOKIES
870 if (sysctl_tcp_syncookies) {
871 msg = "Sending cookies";
872 want_cookie = true;
873 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDOCOOKIES);
874 } else
875#endif
876 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPREQQFULLDROP);
877
878 lopt = inet_csk(sk)->icsk_accept_queue.listen_opt;
879 if (!lopt->synflood_warned && sysctl_tcp_syncookies != 2) {
880 lopt->synflood_warned = 1;
881 pr_info("%s: Possible SYN flooding on port %d. %s. Check SNMP counters.\n",
882 proto, ntohs(tcp_hdr(skb)->dest), msg);
883 }
884 return want_cookie;
885}
886EXPORT_SYMBOL(tcp_syn_flood_action);
887 859
888#ifdef CONFIG_TCP_MD5SIG 860#ifdef CONFIG_TCP_MD5SIG
889/* 861/*
@@ -897,10 +869,10 @@ struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
897 const union tcp_md5_addr *addr, 869 const union tcp_md5_addr *addr,
898 int family) 870 int family)
899{ 871{
900 struct tcp_sock *tp = tcp_sk(sk); 872 const struct tcp_sock *tp = tcp_sk(sk);
901 struct tcp_md5sig_key *key; 873 struct tcp_md5sig_key *key;
902 unsigned int size = sizeof(struct in_addr); 874 unsigned int size = sizeof(struct in_addr);
903 struct tcp_md5sig_info *md5sig; 875 const struct tcp_md5sig_info *md5sig;
904 876
905 /* caller either holds rcu_read_lock() or socket lock */ 877 /* caller either holds rcu_read_lock() or socket lock */
906 md5sig = rcu_dereference_check(tp->md5sig_info, 878 md5sig = rcu_dereference_check(tp->md5sig_info,
@@ -923,24 +895,15 @@ struct tcp_md5sig_key *tcp_md5_do_lookup(struct sock *sk,
923EXPORT_SYMBOL(tcp_md5_do_lookup); 895EXPORT_SYMBOL(tcp_md5_do_lookup);
924 896
925struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk, 897struct tcp_md5sig_key *tcp_v4_md5_lookup(struct sock *sk,
926 struct sock *addr_sk) 898 const struct sock *addr_sk)
927{ 899{
928 union tcp_md5_addr *addr; 900 const union tcp_md5_addr *addr;
929 901
930 addr = (union tcp_md5_addr *)&inet_sk(addr_sk)->inet_daddr; 902 addr = (const union tcp_md5_addr *)&addr_sk->sk_daddr;
931 return tcp_md5_do_lookup(sk, addr, AF_INET); 903 return tcp_md5_do_lookup(sk, addr, AF_INET);
932} 904}
933EXPORT_SYMBOL(tcp_v4_md5_lookup); 905EXPORT_SYMBOL(tcp_v4_md5_lookup);
934 906
935static struct tcp_md5sig_key *tcp_v4_reqsk_md5_lookup(struct sock *sk,
936 struct request_sock *req)
937{
938 union tcp_md5_addr *addr;
939
940 addr = (union tcp_md5_addr *)&inet_rsk(req)->ir_rmt_addr;
941 return tcp_md5_do_lookup(sk, addr, AF_INET);
942}
943
944/* This can be called on a newly created socket, from other files */ 907/* This can be called on a newly created socket, from other files */
945int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr, 908int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
946 int family, const u8 *newkey, u8 newkeylen, gfp_t gfp) 909 int family, const u8 *newkey, u8 newkeylen, gfp_t gfp)
@@ -1101,8 +1064,8 @@ clear_hash_noput:
1101 return 1; 1064 return 1;
1102} 1065}
1103 1066
1104int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key, 1067int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
1105 const struct sock *sk, const struct request_sock *req, 1068 const struct sock *sk,
1106 const struct sk_buff *skb) 1069 const struct sk_buff *skb)
1107{ 1070{
1108 struct tcp_md5sig_pool *hp; 1071 struct tcp_md5sig_pool *hp;
@@ -1110,12 +1073,9 @@ int tcp_v4_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
1110 const struct tcphdr *th = tcp_hdr(skb); 1073 const struct tcphdr *th = tcp_hdr(skb);
1111 __be32 saddr, daddr; 1074 __be32 saddr, daddr;
1112 1075
1113 if (sk) { 1076 if (sk) { /* valid for establish/request sockets */
1114 saddr = inet_sk(sk)->inet_saddr; 1077 saddr = sk->sk_rcv_saddr;
1115 daddr = inet_sk(sk)->inet_daddr; 1078 daddr = sk->sk_daddr;
1116 } else if (req) {
1117 saddr = inet_rsk(req)->ir_loc_addr;
1118 daddr = inet_rsk(req)->ir_rmt_addr;
1119 } else { 1079 } else {
1120 const struct iphdr *iph = ip_hdr(skb); 1080 const struct iphdr *iph = ip_hdr(skb);
1121 saddr = iph->saddr; 1081 saddr = iph->saddr;
@@ -1152,8 +1112,9 @@ clear_hash_noput:
1152} 1112}
1153EXPORT_SYMBOL(tcp_v4_md5_hash_skb); 1113EXPORT_SYMBOL(tcp_v4_md5_hash_skb);
1154 1114
1155static bool __tcp_v4_inbound_md5_hash(struct sock *sk, 1115/* Called with rcu_read_lock() */
1156 const struct sk_buff *skb) 1116static bool tcp_v4_inbound_md5_hash(struct sock *sk,
1117 const struct sk_buff *skb)
1157{ 1118{
1158 /* 1119 /*
1159 * This gets called for each TCP segment that arrives 1120 * This gets called for each TCP segment that arrives
@@ -1193,7 +1154,7 @@ static bool __tcp_v4_inbound_md5_hash(struct sock *sk,
1193 */ 1154 */
1194 genhash = tcp_v4_md5_hash_skb(newhash, 1155 genhash = tcp_v4_md5_hash_skb(newhash,
1195 hash_expected, 1156 hash_expected,
1196 NULL, NULL, skb); 1157 NULL, skb);
1197 1158
1198 if (genhash || memcmp(hash_location, newhash, 16) != 0) { 1159 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
1199 net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n", 1160 net_info_ratelimited("MD5 Hash failed for (%pI4, %d)->(%pI4, %d)%s\n",
@@ -1205,28 +1166,16 @@ static bool __tcp_v4_inbound_md5_hash(struct sock *sk,
1205 } 1166 }
1206 return false; 1167 return false;
1207} 1168}
1208
1209static bool tcp_v4_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
1210{
1211 bool ret;
1212
1213 rcu_read_lock();
1214 ret = __tcp_v4_inbound_md5_hash(sk, skb);
1215 rcu_read_unlock();
1216
1217 return ret;
1218}
1219
1220#endif 1169#endif
1221 1170
1222static void tcp_v4_init_req(struct request_sock *req, struct sock *sk, 1171static void tcp_v4_init_req(struct request_sock *req, struct sock *sk_listener,
1223 struct sk_buff *skb) 1172 struct sk_buff *skb)
1224{ 1173{
1225 struct inet_request_sock *ireq = inet_rsk(req); 1174 struct inet_request_sock *ireq = inet_rsk(req);
1226 1175
1227 ireq->ir_loc_addr = ip_hdr(skb)->daddr; 1176 sk_rcv_saddr_set(req_to_sk(req), ip_hdr(skb)->daddr);
1228 ireq->ir_rmt_addr = ip_hdr(skb)->saddr; 1177 sk_daddr_set(req_to_sk(req), ip_hdr(skb)->saddr);
1229 ireq->no_srccheck = inet_sk(sk)->transparent; 1178 ireq->no_srccheck = inet_sk(sk_listener)->transparent;
1230 ireq->opt = tcp_v4_save_options(skb); 1179 ireq->opt = tcp_v4_save_options(skb);
1231} 1180}
1232 1181
@@ -1259,7 +1208,7 @@ struct request_sock_ops tcp_request_sock_ops __read_mostly = {
1259static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = { 1208static const struct tcp_request_sock_ops tcp_request_sock_ipv4_ops = {
1260 .mss_clamp = TCP_MSS_DEFAULT, 1209 .mss_clamp = TCP_MSS_DEFAULT,
1261#ifdef CONFIG_TCP_MD5SIG 1210#ifdef CONFIG_TCP_MD5SIG
1262 .md5_lookup = tcp_v4_reqsk_md5_lookup, 1211 .req_md5_lookup = tcp_v4_md5_lookup,
1263 .calc_md5_hash = tcp_v4_md5_hash_skb, 1212 .calc_md5_hash = tcp_v4_md5_hash_skb,
1264#endif 1213#endif
1265 .init_req = tcp_v4_init_req, 1214 .init_req = tcp_v4_init_req,
@@ -1318,8 +1267,8 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1318 newtp = tcp_sk(newsk); 1267 newtp = tcp_sk(newsk);
1319 newinet = inet_sk(newsk); 1268 newinet = inet_sk(newsk);
1320 ireq = inet_rsk(req); 1269 ireq = inet_rsk(req);
1321 newinet->inet_daddr = ireq->ir_rmt_addr; 1270 sk_daddr_set(newsk, ireq->ir_rmt_addr);
1322 newinet->inet_rcv_saddr = ireq->ir_loc_addr; 1271 sk_rcv_saddr_set(newsk, ireq->ir_loc_addr);
1323 newinet->inet_saddr = ireq->ir_loc_addr; 1272 newinet->inet_saddr = ireq->ir_loc_addr;
1324 inet_opt = ireq->opt; 1273 inet_opt = ireq->opt;
1325 rcu_assign_pointer(newinet->inet_opt, inet_opt); 1274 rcu_assign_pointer(newinet->inet_opt, inet_opt);
@@ -1356,7 +1305,7 @@ struct sock *tcp_v4_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1356 /* Copy over the MD5 key from the original socket */ 1305 /* Copy over the MD5 key from the original socket */
1357 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr, 1306 key = tcp_md5_do_lookup(sk, (union tcp_md5_addr *)&newinet->inet_daddr,
1358 AF_INET); 1307 AF_INET);
1359 if (key != NULL) { 1308 if (key) {
1360 /* 1309 /*
1361 * We're using one, so create a matching key 1310 * We're using one, so create a matching key
1362 * on the newsk structure. If we fail to get 1311 * on the newsk structure. If we fail to get
@@ -1391,15 +1340,17 @@ EXPORT_SYMBOL(tcp_v4_syn_recv_sock);
1391 1340
1392static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb) 1341static struct sock *tcp_v4_hnd_req(struct sock *sk, struct sk_buff *skb)
1393{ 1342{
1394 struct tcphdr *th = tcp_hdr(skb); 1343 const struct tcphdr *th = tcp_hdr(skb);
1395 const struct iphdr *iph = ip_hdr(skb); 1344 const struct iphdr *iph = ip_hdr(skb);
1345 struct request_sock *req;
1396 struct sock *nsk; 1346 struct sock *nsk;
1397 struct request_sock **prev; 1347
1398 /* Find possible connection requests. */ 1348 req = inet_csk_search_req(sk, th->source, iph->saddr, iph->daddr);
1399 struct request_sock *req = inet_csk_search_req(sk, &prev, th->source, 1349 if (req) {
1400 iph->saddr, iph->daddr); 1350 nsk = tcp_check_req(sk, skb, req, false);
1401 if (req) 1351 reqsk_put(req);
1402 return tcp_check_req(sk, skb, req, prev, false); 1352 return nsk;
1353 }
1403 1354
1404 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr, 1355 nsk = inet_lookup_established(sock_net(sk), &tcp_hashinfo, iph->saddr,
1405 th->source, iph->daddr, th->dest, inet_iif(skb)); 1356 th->source, iph->daddr, th->dest, inet_iif(skb));
@@ -1439,7 +1390,7 @@ int tcp_v4_do_rcv(struct sock *sk, struct sk_buff *skb)
1439 sk_mark_napi_id(sk, skb); 1390 sk_mark_napi_id(sk, skb);
1440 if (dst) { 1391 if (dst) {
1441 if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif || 1392 if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
1442 dst->ops->check(dst, 0) == NULL) { 1393 !dst->ops->check(dst, 0)) {
1443 dst_release(dst); 1394 dst_release(dst);
1444 sk->sk_rx_dst = NULL; 1395 sk->sk_rx_dst = NULL;
1445 } 1396 }
@@ -1517,7 +1468,7 @@ void tcp_v4_early_demux(struct sk_buff *skb)
1517 if (sk) { 1468 if (sk) {
1518 skb->sk = sk; 1469 skb->sk = sk;
1519 skb->destructor = sock_edemux; 1470 skb->destructor = sock_edemux;
1520 if (sk->sk_state != TCP_TIME_WAIT) { 1471 if (sk_fullsock(sk)) {
1521 struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst); 1472 struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst);
1522 1473
1523 if (dst) 1474 if (dst)
@@ -1734,7 +1685,7 @@ do_time_wait:
1734 iph->daddr, th->dest, 1685 iph->daddr, th->dest,
1735 inet_iif(skb)); 1686 inet_iif(skb));
1736 if (sk2) { 1687 if (sk2) {
1737 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row); 1688 inet_twsk_deschedule(inet_twsk(sk));
1738 inet_twsk_put(inet_twsk(sk)); 1689 inet_twsk_put(inet_twsk(sk));
1739 sk = sk2; 1690 sk = sk2;
1740 goto process; 1691 goto process;
@@ -1846,7 +1797,7 @@ void tcp_v4_destroy_sock(struct sock *sk)
1846 if (inet_csk(sk)->icsk_bind_hash) 1797 if (inet_csk(sk)->icsk_bind_hash)
1847 inet_put_port(sk); 1798 inet_put_port(sk);
1848 1799
1849 BUG_ON(tp->fastopen_rsk != NULL); 1800 BUG_ON(tp->fastopen_rsk);
1850 1801
1851 /* If socket is aborted during connect operation */ 1802 /* If socket is aborted during connect operation */
1852 tcp_free_fastopen_req(tp); 1803 tcp_free_fastopen_req(tp);
@@ -1904,13 +1855,13 @@ get_req:
1904 } 1855 }
1905 sk = sk_nulls_next(st->syn_wait_sk); 1856 sk = sk_nulls_next(st->syn_wait_sk);
1906 st->state = TCP_SEQ_STATE_LISTENING; 1857 st->state = TCP_SEQ_STATE_LISTENING;
1907 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 1858 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1908 } else { 1859 } else {
1909 icsk = inet_csk(sk); 1860 icsk = inet_csk(sk);
1910 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 1861 spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1911 if (reqsk_queue_len(&icsk->icsk_accept_queue)) 1862 if (reqsk_queue_len(&icsk->icsk_accept_queue))
1912 goto start_req; 1863 goto start_req;
1913 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 1864 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1914 sk = sk_nulls_next(sk); 1865 sk = sk_nulls_next(sk);
1915 } 1866 }
1916get_sk: 1867get_sk:
@@ -1922,7 +1873,7 @@ get_sk:
1922 goto out; 1873 goto out;
1923 } 1874 }
1924 icsk = inet_csk(sk); 1875 icsk = inet_csk(sk);
1925 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 1876 spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1926 if (reqsk_queue_len(&icsk->icsk_accept_queue)) { 1877 if (reqsk_queue_len(&icsk->icsk_accept_queue)) {
1927start_req: 1878start_req:
1928 st->uid = sock_i_uid(sk); 1879 st->uid = sock_i_uid(sk);
@@ -1931,7 +1882,7 @@ start_req:
1931 st->sbucket = 0; 1882 st->sbucket = 0;
1932 goto get_req; 1883 goto get_req;
1933 } 1884 }
1934 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 1885 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
1935 } 1886 }
1936 spin_unlock_bh(&ilb->lock); 1887 spin_unlock_bh(&ilb->lock);
1937 st->offset = 0; 1888 st->offset = 0;
@@ -2150,7 +2101,7 @@ static void tcp_seq_stop(struct seq_file *seq, void *v)
2150 case TCP_SEQ_STATE_OPENREQ: 2101 case TCP_SEQ_STATE_OPENREQ:
2151 if (v) { 2102 if (v) {
2152 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk); 2103 struct inet_connection_sock *icsk = inet_csk(st->syn_wait_sk);
2153 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock); 2104 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
2154 } 2105 }
2155 case TCP_SEQ_STATE_LISTENING: 2106 case TCP_SEQ_STATE_LISTENING:
2156 if (v != SEQ_START_TOKEN) 2107 if (v != SEQ_START_TOKEN)
@@ -2204,17 +2155,17 @@ void tcp_proc_unregister(struct net *net, struct tcp_seq_afinfo *afinfo)
2204} 2155}
2205EXPORT_SYMBOL(tcp_proc_unregister); 2156EXPORT_SYMBOL(tcp_proc_unregister);
2206 2157
2207static void get_openreq4(const struct sock *sk, const struct request_sock *req, 2158static void get_openreq4(const struct request_sock *req,
2208 struct seq_file *f, int i, kuid_t uid) 2159 struct seq_file *f, int i, kuid_t uid)
2209{ 2160{
2210 const struct inet_request_sock *ireq = inet_rsk(req); 2161 const struct inet_request_sock *ireq = inet_rsk(req);
2211 long delta = req->expires - jiffies; 2162 long delta = req->rsk_timer.expires - jiffies;
2212 2163
2213 seq_printf(f, "%4d: %08X:%04X %08X:%04X" 2164 seq_printf(f, "%4d: %08X:%04X %08X:%04X"
2214 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK", 2165 " %02X %08X:%08X %02X:%08lX %08X %5u %8d %u %d %pK",
2215 i, 2166 i,
2216 ireq->ir_loc_addr, 2167 ireq->ir_loc_addr,
2217 ntohs(inet_sk(sk)->inet_sport), 2168 ireq->ir_num,
2218 ireq->ir_rmt_addr, 2169 ireq->ir_rmt_addr,
2219 ntohs(ireq->ir_rmt_port), 2170 ntohs(ireq->ir_rmt_port),
2220 TCP_SYN_RECV, 2171 TCP_SYN_RECV,
@@ -2225,7 +2176,7 @@ static void get_openreq4(const struct sock *sk, const struct request_sock *req,
2225 from_kuid_munged(seq_user_ns(f), uid), 2176 from_kuid_munged(seq_user_ns(f), uid),
2226 0, /* non standard timer */ 2177 0, /* non standard timer */
2227 0, /* open_requests have no inode */ 2178 0, /* open_requests have no inode */
2228 atomic_read(&sk->sk_refcnt), 2179 0,
2229 req); 2180 req);
2230} 2181}
2231 2182
@@ -2291,9 +2242,9 @@ static void get_tcp4_sock(struct sock *sk, struct seq_file *f, int i)
2291static void get_timewait4_sock(const struct inet_timewait_sock *tw, 2242static void get_timewait4_sock(const struct inet_timewait_sock *tw,
2292 struct seq_file *f, int i) 2243 struct seq_file *f, int i)
2293{ 2244{
2245 long delta = tw->tw_timer.expires - jiffies;
2294 __be32 dest, src; 2246 __be32 dest, src;
2295 __u16 destp, srcp; 2247 __u16 destp, srcp;
2296 s32 delta = tw->tw_ttd - inet_tw_time_stamp();
2297 2248
2298 dest = tw->tw_daddr; 2249 dest = tw->tw_daddr;
2299 src = tw->tw_rcv_saddr; 2250 src = tw->tw_rcv_saddr;
@@ -2332,7 +2283,7 @@ static int tcp4_seq_show(struct seq_file *seq, void *v)
2332 get_tcp4_sock(v, seq, st->num); 2283 get_tcp4_sock(v, seq, st->num);
2333 break; 2284 break;
2334 case TCP_SEQ_STATE_OPENREQ: 2285 case TCP_SEQ_STATE_OPENREQ:
2335 get_openreq4(st->syn_wait_sk, v, seq, st->num, st->uid); 2286 get_openreq4(v, seq, st->num, st->uid);
2336 break; 2287 break;
2337 } 2288 }
2338out: 2289out:
@@ -2460,6 +2411,8 @@ static int __net_init tcp_sk_init(struct net *net)
2460 } 2411 }
2461 net->ipv4.sysctl_tcp_ecn = 2; 2412 net->ipv4.sysctl_tcp_ecn = 2;
2462 net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS; 2413 net->ipv4.sysctl_tcp_base_mss = TCP_BASE_MSS;
2414 net->ipv4.sysctl_tcp_probe_threshold = TCP_PROBE_THRESHOLD;
2415 net->ipv4.sysctl_tcp_probe_interval = TCP_PROBE_INTERVAL;
2463 return 0; 2416 return 0;
2464 2417
2465fail: 2418fail:
diff --git a/net/ipv4/tcp_metrics.c b/net/ipv4/tcp_metrics.c
index e5f41bd5ec1b..a51d63a43e33 100644
--- a/net/ipv4/tcp_metrics.c
+++ b/net/ipv4/tcp_metrics.c
@@ -28,7 +28,8 @@ static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *s
28 28
29struct tcp_fastopen_metrics { 29struct tcp_fastopen_metrics {
30 u16 mss; 30 u16 mss;
31 u16 syn_loss:10; /* Recurring Fast Open SYN losses */ 31 u16 syn_loss:10, /* Recurring Fast Open SYN losses */
32 try_exp:2; /* Request w/ exp. option (once) */
32 unsigned long last_syn_loss; /* Last Fast Open SYN loss */ 33 unsigned long last_syn_loss; /* Last Fast Open SYN loss */
33 struct tcp_fastopen_cookie cookie; 34 struct tcp_fastopen_cookie cookie;
34}; 35};
@@ -40,6 +41,7 @@ struct tcp_fastopen_metrics {
40 41
41struct tcp_metrics_block { 42struct tcp_metrics_block {
42 struct tcp_metrics_block __rcu *tcpm_next; 43 struct tcp_metrics_block __rcu *tcpm_next;
44 possible_net_t tcpm_net;
43 struct inetpeer_addr tcpm_saddr; 45 struct inetpeer_addr tcpm_saddr;
44 struct inetpeer_addr tcpm_daddr; 46 struct inetpeer_addr tcpm_daddr;
45 unsigned long tcpm_stamp; 47 unsigned long tcpm_stamp;
@@ -52,6 +54,11 @@ struct tcp_metrics_block {
52 struct rcu_head rcu_head; 54 struct rcu_head rcu_head;
53}; 55};
54 56
57static inline struct net *tm_net(struct tcp_metrics_block *tm)
58{
59 return read_pnet(&tm->tcpm_net);
60}
61
55static bool tcp_metric_locked(struct tcp_metrics_block *tm, 62static bool tcp_metric_locked(struct tcp_metrics_block *tm,
56 enum tcp_metric_index idx) 63 enum tcp_metric_index idx)
57{ 64{
@@ -74,23 +81,20 @@ static void tcp_metric_set(struct tcp_metrics_block *tm,
74static bool addr_same(const struct inetpeer_addr *a, 81static bool addr_same(const struct inetpeer_addr *a,
75 const struct inetpeer_addr *b) 82 const struct inetpeer_addr *b)
76{ 83{
77 const struct in6_addr *a6, *b6;
78
79 if (a->family != b->family) 84 if (a->family != b->family)
80 return false; 85 return false;
81 if (a->family == AF_INET) 86 if (a->family == AF_INET)
82 return a->addr.a4 == b->addr.a4; 87 return a->addr.a4 == b->addr.a4;
83 88 return ipv6_addr_equal(&a->addr.in6, &b->addr.in6);
84 a6 = (const struct in6_addr *) &a->addr.a6[0];
85 b6 = (const struct in6_addr *) &b->addr.a6[0];
86
87 return ipv6_addr_equal(a6, b6);
88} 89}
89 90
90struct tcpm_hash_bucket { 91struct tcpm_hash_bucket {
91 struct tcp_metrics_block __rcu *chain; 92 struct tcp_metrics_block __rcu *chain;
92}; 93};
93 94
95static struct tcpm_hash_bucket *tcp_metrics_hash __read_mostly;
96static unsigned int tcp_metrics_hash_log __read_mostly;
97
94static DEFINE_SPINLOCK(tcp_metrics_lock); 98static DEFINE_SPINLOCK(tcp_metrics_lock);
95 99
96static void tcpm_suck_dst(struct tcp_metrics_block *tm, 100static void tcpm_suck_dst(struct tcp_metrics_block *tm,
@@ -128,6 +132,8 @@ static void tcpm_suck_dst(struct tcp_metrics_block *tm,
128 if (fastopen_clear) { 132 if (fastopen_clear) {
129 tm->tcpm_fastopen.mss = 0; 133 tm->tcpm_fastopen.mss = 0;
130 tm->tcpm_fastopen.syn_loss = 0; 134 tm->tcpm_fastopen.syn_loss = 0;
135 tm->tcpm_fastopen.try_exp = 0;
136 tm->tcpm_fastopen.cookie.exp = false;
131 tm->tcpm_fastopen.cookie.len = 0; 137 tm->tcpm_fastopen.cookie.len = 0;
132 } 138 }
133} 139}
@@ -143,6 +149,9 @@ static void tcpm_check_stamp(struct tcp_metrics_block *tm, struct dst_entry *dst
143#define TCP_METRICS_RECLAIM_DEPTH 5 149#define TCP_METRICS_RECLAIM_DEPTH 5
144#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL 150#define TCP_METRICS_RECLAIM_PTR (struct tcp_metrics_block *) 0x1UL
145 151
152#define deref_locked(p) \
153 rcu_dereference_protected(p, lockdep_is_held(&tcp_metrics_lock))
154
146static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst, 155static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
147 struct inetpeer_addr *saddr, 156 struct inetpeer_addr *saddr,
148 struct inetpeer_addr *daddr, 157 struct inetpeer_addr *daddr,
@@ -171,9 +180,9 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
171 if (unlikely(reclaim)) { 180 if (unlikely(reclaim)) {
172 struct tcp_metrics_block *oldest; 181 struct tcp_metrics_block *oldest;
173 182
174 oldest = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); 183 oldest = deref_locked(tcp_metrics_hash[hash].chain);
175 for (tm = rcu_dereference(oldest->tcpm_next); tm; 184 for (tm = deref_locked(oldest->tcpm_next); tm;
176 tm = rcu_dereference(tm->tcpm_next)) { 185 tm = deref_locked(tm->tcpm_next)) {
177 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp)) 186 if (time_before(tm->tcpm_stamp, oldest->tcpm_stamp))
178 oldest = tm; 187 oldest = tm;
179 } 188 }
@@ -183,14 +192,15 @@ static struct tcp_metrics_block *tcpm_new(struct dst_entry *dst,
183 if (!tm) 192 if (!tm)
184 goto out_unlock; 193 goto out_unlock;
185 } 194 }
195 write_pnet(&tm->tcpm_net, net);
186 tm->tcpm_saddr = *saddr; 196 tm->tcpm_saddr = *saddr;
187 tm->tcpm_daddr = *daddr; 197 tm->tcpm_daddr = *daddr;
188 198
189 tcpm_suck_dst(tm, dst, true); 199 tcpm_suck_dst(tm, dst, true);
190 200
191 if (likely(!reclaim)) { 201 if (likely(!reclaim)) {
192 tm->tcpm_next = net->ipv4.tcp_metrics_hash[hash].chain; 202 tm->tcpm_next = tcp_metrics_hash[hash].chain;
193 rcu_assign_pointer(net->ipv4.tcp_metrics_hash[hash].chain, tm); 203 rcu_assign_pointer(tcp_metrics_hash[hash].chain, tm);
194 } 204 }
195 205
196out_unlock: 206out_unlock:
@@ -214,10 +224,11 @@ static struct tcp_metrics_block *__tcp_get_metrics(const struct inetpeer_addr *s
214 struct tcp_metrics_block *tm; 224 struct tcp_metrics_block *tm;
215 int depth = 0; 225 int depth = 0;
216 226
217 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm; 227 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
218 tm = rcu_dereference(tm->tcpm_next)) { 228 tm = rcu_dereference(tm->tcpm_next)) {
219 if (addr_same(&tm->tcpm_saddr, saddr) && 229 if (addr_same(&tm->tcpm_saddr, saddr) &&
220 addr_same(&tm->tcpm_daddr, daddr)) 230 addr_same(&tm->tcpm_daddr, daddr) &&
231 net_eq(tm_net(tm), net))
221 break; 232 break;
222 depth++; 233 depth++;
223 } 234 }
@@ -242,8 +253,8 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
242 break; 253 break;
243#if IS_ENABLED(CONFIG_IPV6) 254#if IS_ENABLED(CONFIG_IPV6)
244 case AF_INET6: 255 case AF_INET6:
245 *(struct in6_addr *)saddr.addr.a6 = inet_rsk(req)->ir_v6_loc_addr; 256 saddr.addr.in6 = inet_rsk(req)->ir_v6_loc_addr;
246 *(struct in6_addr *)daddr.addr.a6 = inet_rsk(req)->ir_v6_rmt_addr; 257 daddr.addr.in6 = inet_rsk(req)->ir_v6_rmt_addr;
247 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr); 258 hash = ipv6_addr_hash(&inet_rsk(req)->ir_v6_rmt_addr);
248 break; 259 break;
249#endif 260#endif
@@ -252,12 +263,14 @@ static struct tcp_metrics_block *__tcp_get_metrics_req(struct request_sock *req,
252 } 263 }
253 264
254 net = dev_net(dst->dev); 265 net = dev_net(dst->dev);
255 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 266 hash ^= net_hash_mix(net);
267 hash = hash_32(hash, tcp_metrics_hash_log);
256 268
257 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm; 269 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
258 tm = rcu_dereference(tm->tcpm_next)) { 270 tm = rcu_dereference(tm->tcpm_next)) {
259 if (addr_same(&tm->tcpm_saddr, &saddr) && 271 if (addr_same(&tm->tcpm_saddr, &saddr) &&
260 addr_same(&tm->tcpm_daddr, &daddr)) 272 addr_same(&tm->tcpm_daddr, &daddr) &&
273 net_eq(tm_net(tm), net))
261 break; 274 break;
262 } 275 }
263 tcpm_check_stamp(tm, dst); 276 tcpm_check_stamp(tm, dst);
@@ -288,9 +301,9 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
288 hash = (__force unsigned int) daddr.addr.a4; 301 hash = (__force unsigned int) daddr.addr.a4;
289 } else { 302 } else {
290 saddr.family = AF_INET6; 303 saddr.family = AF_INET6;
291 *(struct in6_addr *)saddr.addr.a6 = tw->tw_v6_rcv_saddr; 304 saddr.addr.in6 = tw->tw_v6_rcv_saddr;
292 daddr.family = AF_INET6; 305 daddr.family = AF_INET6;
293 *(struct in6_addr *)daddr.addr.a6 = tw->tw_v6_daddr; 306 daddr.addr.in6 = tw->tw_v6_daddr;
294 hash = ipv6_addr_hash(&tw->tw_v6_daddr); 307 hash = ipv6_addr_hash(&tw->tw_v6_daddr);
295 } 308 }
296 } 309 }
@@ -299,12 +312,14 @@ static struct tcp_metrics_block *__tcp_get_metrics_tw(struct inet_timewait_sock
299 return NULL; 312 return NULL;
300 313
301 net = twsk_net(tw); 314 net = twsk_net(tw);
302 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 315 hash ^= net_hash_mix(net);
316 hash = hash_32(hash, tcp_metrics_hash_log);
303 317
304 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm; 318 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
305 tm = rcu_dereference(tm->tcpm_next)) { 319 tm = rcu_dereference(tm->tcpm_next)) {
306 if (addr_same(&tm->tcpm_saddr, &saddr) && 320 if (addr_same(&tm->tcpm_saddr, &saddr) &&
307 addr_same(&tm->tcpm_daddr, &daddr)) 321 addr_same(&tm->tcpm_daddr, &daddr) &&
322 net_eq(tm_net(tm), net))
308 break; 323 break;
309 } 324 }
310 return tm; 325 return tm;
@@ -336,9 +351,9 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
336 hash = (__force unsigned int) daddr.addr.a4; 351 hash = (__force unsigned int) daddr.addr.a4;
337 } else { 352 } else {
338 saddr.family = AF_INET6; 353 saddr.family = AF_INET6;
339 *(struct in6_addr *)saddr.addr.a6 = sk->sk_v6_rcv_saddr; 354 saddr.addr.in6 = sk->sk_v6_rcv_saddr;
340 daddr.family = AF_INET6; 355 daddr.family = AF_INET6;
341 *(struct in6_addr *)daddr.addr.a6 = sk->sk_v6_daddr; 356 daddr.addr.in6 = sk->sk_v6_daddr;
342 hash = ipv6_addr_hash(&sk->sk_v6_daddr); 357 hash = ipv6_addr_hash(&sk->sk_v6_daddr);
343 } 358 }
344 } 359 }
@@ -347,7 +362,8 @@ static struct tcp_metrics_block *tcp_get_metrics(struct sock *sk,
347 return NULL; 362 return NULL;
348 363
349 net = dev_net(dst->dev); 364 net = dev_net(dst->dev);
350 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 365 hash ^= net_hash_mix(net);
366 hash = hash_32(hash, tcp_metrics_hash_log);
351 367
352 tm = __tcp_get_metrics(&saddr, &daddr, net, hash); 368 tm = __tcp_get_metrics(&saddr, &daddr, net, hash);
353 if (tm == TCP_METRICS_RECLAIM_PTR) 369 if (tm == TCP_METRICS_RECLAIM_PTR)
@@ -492,7 +508,7 @@ void tcp_init_metrics(struct sock *sk)
492 struct tcp_metrics_block *tm; 508 struct tcp_metrics_block *tm;
493 u32 val, crtt = 0; /* cached RTT scaled by 8 */ 509 u32 val, crtt = 0; /* cached RTT scaled by 8 */
494 510
495 if (dst == NULL) 511 if (!dst)
496 goto reset; 512 goto reset;
497 513
498 dst_confirm(dst); 514 dst_confirm(dst);
@@ -700,6 +716,8 @@ void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
700 if (tfom->mss) 716 if (tfom->mss)
701 *mss = tfom->mss; 717 *mss = tfom->mss;
702 *cookie = tfom->cookie; 718 *cookie = tfom->cookie;
719 if (cookie->len <= 0 && tfom->try_exp == 1)
720 cookie->exp = true;
703 *syn_loss = tfom->syn_loss; 721 *syn_loss = tfom->syn_loss;
704 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0; 722 *last_syn_loss = *syn_loss ? tfom->last_syn_loss : 0;
705 } while (read_seqretry(&fastopen_seqlock, seq)); 723 } while (read_seqretry(&fastopen_seqlock, seq));
@@ -708,7 +726,8 @@ void tcp_fastopen_cache_get(struct sock *sk, u16 *mss,
708} 726}
709 727
710void tcp_fastopen_cache_set(struct sock *sk, u16 mss, 728void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
711 struct tcp_fastopen_cookie *cookie, bool syn_lost) 729 struct tcp_fastopen_cookie *cookie, bool syn_lost,
730 u16 try_exp)
712{ 731{
713 struct dst_entry *dst = __sk_dst_get(sk); 732 struct dst_entry *dst = __sk_dst_get(sk);
714 struct tcp_metrics_block *tm; 733 struct tcp_metrics_block *tm;
@@ -725,6 +744,9 @@ void tcp_fastopen_cache_set(struct sock *sk, u16 mss,
725 tfom->mss = mss; 744 tfom->mss = mss;
726 if (cookie && cookie->len > 0) 745 if (cookie && cookie->len > 0)
727 tfom->cookie = *cookie; 746 tfom->cookie = *cookie;
747 else if (try_exp > tfom->try_exp &&
748 tfom->cookie.len <= 0 && !tfom->cookie.exp)
749 tfom->try_exp = try_exp;
728 if (syn_lost) { 750 if (syn_lost) {
729 ++tfom->syn_loss; 751 ++tfom->syn_loss;
730 tfom->last_syn_loss = jiffies; 752 tfom->last_syn_loss = jiffies;
@@ -773,19 +795,19 @@ static int tcp_metrics_fill_info(struct sk_buff *msg,
773 795
774 switch (tm->tcpm_daddr.family) { 796 switch (tm->tcpm_daddr.family) {
775 case AF_INET: 797 case AF_INET:
776 if (nla_put_be32(msg, TCP_METRICS_ATTR_ADDR_IPV4, 798 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_ADDR_IPV4,
777 tm->tcpm_daddr.addr.a4) < 0) 799 tm->tcpm_daddr.addr.a4) < 0)
778 goto nla_put_failure; 800 goto nla_put_failure;
779 if (nla_put_be32(msg, TCP_METRICS_ATTR_SADDR_IPV4, 801 if (nla_put_in_addr(msg, TCP_METRICS_ATTR_SADDR_IPV4,
780 tm->tcpm_saddr.addr.a4) < 0) 802 tm->tcpm_saddr.addr.a4) < 0)
781 goto nla_put_failure; 803 goto nla_put_failure;
782 break; 804 break;
783 case AF_INET6: 805 case AF_INET6:
784 if (nla_put(msg, TCP_METRICS_ATTR_ADDR_IPV6, 16, 806 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_ADDR_IPV6,
785 tm->tcpm_daddr.addr.a6) < 0) 807 &tm->tcpm_daddr.addr.in6) < 0)
786 goto nla_put_failure; 808 goto nla_put_failure;
787 if (nla_put(msg, TCP_METRICS_ATTR_SADDR_IPV6, 16, 809 if (nla_put_in6_addr(msg, TCP_METRICS_ATTR_SADDR_IPV6,
788 tm->tcpm_saddr.addr.a6) < 0) 810 &tm->tcpm_saddr.addr.in6) < 0)
789 goto nla_put_failure; 811 goto nla_put_failure;
790 break; 812 break;
791 default: 813 default:
@@ -898,17 +920,19 @@ static int tcp_metrics_nl_dump(struct sk_buff *skb,
898 struct netlink_callback *cb) 920 struct netlink_callback *cb)
899{ 921{
900 struct net *net = sock_net(skb->sk); 922 struct net *net = sock_net(skb->sk);
901 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log; 923 unsigned int max_rows = 1U << tcp_metrics_hash_log;
902 unsigned int row, s_row = cb->args[0]; 924 unsigned int row, s_row = cb->args[0];
903 int s_col = cb->args[1], col = s_col; 925 int s_col = cb->args[1], col = s_col;
904 926
905 for (row = s_row; row < max_rows; row++, s_col = 0) { 927 for (row = s_row; row < max_rows; row++, s_col = 0) {
906 struct tcp_metrics_block *tm; 928 struct tcp_metrics_block *tm;
907 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash + row; 929 struct tcpm_hash_bucket *hb = tcp_metrics_hash + row;
908 930
909 rcu_read_lock(); 931 rcu_read_lock();
910 for (col = 0, tm = rcu_dereference(hb->chain); tm; 932 for (col = 0, tm = rcu_dereference(hb->chain); tm;
911 tm = rcu_dereference(tm->tcpm_next), col++) { 933 tm = rcu_dereference(tm->tcpm_next), col++) {
934 if (!net_eq(tm_net(tm), net))
935 continue;
912 if (col < s_col) 936 if (col < s_col)
913 continue; 937 continue;
914 if (tcp_metrics_dump_info(skb, cb, tm) < 0) { 938 if (tcp_metrics_dump_info(skb, cb, tm) < 0) {
@@ -933,7 +957,7 @@ static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
933 a = info->attrs[v4]; 957 a = info->attrs[v4];
934 if (a) { 958 if (a) {
935 addr->family = AF_INET; 959 addr->family = AF_INET;
936 addr->addr.a4 = nla_get_be32(a); 960 addr->addr.a4 = nla_get_in_addr(a);
937 if (hash) 961 if (hash)
938 *hash = (__force unsigned int) addr->addr.a4; 962 *hash = (__force unsigned int) addr->addr.a4;
939 return 0; 963 return 0;
@@ -943,9 +967,9 @@ static int __parse_nl_addr(struct genl_info *info, struct inetpeer_addr *addr,
943 if (nla_len(a) != sizeof(struct in6_addr)) 967 if (nla_len(a) != sizeof(struct in6_addr))
944 return -EINVAL; 968 return -EINVAL;
945 addr->family = AF_INET6; 969 addr->family = AF_INET6;
946 memcpy(addr->addr.a6, nla_data(a), sizeof(addr->addr.a6)); 970 addr->addr.in6 = nla_get_in6_addr(a);
947 if (hash) 971 if (hash)
948 *hash = ipv6_addr_hash((struct in6_addr *) addr->addr.a6); 972 *hash = ipv6_addr_hash(&addr->addr.in6);
949 return 0; 973 return 0;
950 } 974 }
951 return optional ? 1 : -EAFNOSUPPORT; 975 return optional ? 1 : -EAFNOSUPPORT;
@@ -994,13 +1018,15 @@ static int tcp_metrics_nl_cmd_get(struct sk_buff *skb, struct genl_info *info)
994 if (!reply) 1018 if (!reply)
995 goto nla_put_failure; 1019 goto nla_put_failure;
996 1020
997 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 1021 hash ^= net_hash_mix(net);
1022 hash = hash_32(hash, tcp_metrics_hash_log);
998 ret = -ESRCH; 1023 ret = -ESRCH;
999 rcu_read_lock(); 1024 rcu_read_lock();
1000 for (tm = rcu_dereference(net->ipv4.tcp_metrics_hash[hash].chain); tm; 1025 for (tm = rcu_dereference(tcp_metrics_hash[hash].chain); tm;
1001 tm = rcu_dereference(tm->tcpm_next)) { 1026 tm = rcu_dereference(tm->tcpm_next)) {
1002 if (addr_same(&tm->tcpm_daddr, &daddr) && 1027 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1003 (!src || addr_same(&tm->tcpm_saddr, &saddr))) { 1028 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1029 net_eq(tm_net(tm), net)) {
1004 ret = tcp_metrics_fill_info(msg, tm); 1030 ret = tcp_metrics_fill_info(msg, tm);
1005 break; 1031 break;
1006 } 1032 }
@@ -1020,34 +1046,27 @@ out_free:
1020 return ret; 1046 return ret;
1021} 1047}
1022 1048
1023#define deref_locked_genl(p) \ 1049static void tcp_metrics_flush_all(struct net *net)
1024 rcu_dereference_protected(p, lockdep_genl_is_held() && \
1025 lockdep_is_held(&tcp_metrics_lock))
1026
1027#define deref_genl(p) rcu_dereference_protected(p, lockdep_genl_is_held())
1028
1029static int tcp_metrics_flush_all(struct net *net)
1030{ 1050{
1031 unsigned int max_rows = 1U << net->ipv4.tcp_metrics_hash_log; 1051 unsigned int max_rows = 1U << tcp_metrics_hash_log;
1032 struct tcpm_hash_bucket *hb = net->ipv4.tcp_metrics_hash; 1052 struct tcpm_hash_bucket *hb = tcp_metrics_hash;
1033 struct tcp_metrics_block *tm; 1053 struct tcp_metrics_block *tm;
1034 unsigned int row; 1054 unsigned int row;
1035 1055
1036 for (row = 0; row < max_rows; row++, hb++) { 1056 for (row = 0; row < max_rows; row++, hb++) {
1057 struct tcp_metrics_block __rcu **pp;
1037 spin_lock_bh(&tcp_metrics_lock); 1058 spin_lock_bh(&tcp_metrics_lock);
1038 tm = deref_locked_genl(hb->chain); 1059 pp = &hb->chain;
1039 if (tm) 1060 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1040 hb->chain = NULL; 1061 if (net_eq(tm_net(tm), net)) {
1041 spin_unlock_bh(&tcp_metrics_lock); 1062 *pp = tm->tcpm_next;
1042 while (tm) { 1063 kfree_rcu(tm, rcu_head);
1043 struct tcp_metrics_block *next; 1064 } else {
1044 1065 pp = &tm->tcpm_next;
1045 next = deref_genl(tm->tcpm_next); 1066 }
1046 kfree_rcu(tm, rcu_head);
1047 tm = next;
1048 } 1067 }
1068 spin_unlock_bh(&tcp_metrics_lock);
1049 } 1069 }
1050 return 0;
1051} 1070}
1052 1071
1053static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info) 1072static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
@@ -1064,19 +1083,23 @@ static int tcp_metrics_nl_cmd_del(struct sk_buff *skb, struct genl_info *info)
1064 ret = parse_nl_addr(info, &daddr, &hash, 1); 1083 ret = parse_nl_addr(info, &daddr, &hash, 1);
1065 if (ret < 0) 1084 if (ret < 0)
1066 return ret; 1085 return ret;
1067 if (ret > 0) 1086 if (ret > 0) {
1068 return tcp_metrics_flush_all(net); 1087 tcp_metrics_flush_all(net);
1088 return 0;
1089 }
1069 ret = parse_nl_saddr(info, &saddr); 1090 ret = parse_nl_saddr(info, &saddr);
1070 if (ret < 0) 1091 if (ret < 0)
1071 src = false; 1092 src = false;
1072 1093
1073 hash = hash_32(hash, net->ipv4.tcp_metrics_hash_log); 1094 hash ^= net_hash_mix(net);
1074 hb = net->ipv4.tcp_metrics_hash + hash; 1095 hash = hash_32(hash, tcp_metrics_hash_log);
1096 hb = tcp_metrics_hash + hash;
1075 pp = &hb->chain; 1097 pp = &hb->chain;
1076 spin_lock_bh(&tcp_metrics_lock); 1098 spin_lock_bh(&tcp_metrics_lock);
1077 for (tm = deref_locked_genl(*pp); tm; tm = deref_locked_genl(*pp)) { 1099 for (tm = deref_locked(*pp); tm; tm = deref_locked(*pp)) {
1078 if (addr_same(&tm->tcpm_daddr, &daddr) && 1100 if (addr_same(&tm->tcpm_daddr, &daddr) &&
1079 (!src || addr_same(&tm->tcpm_saddr, &saddr))) { 1101 (!src || addr_same(&tm->tcpm_saddr, &saddr)) &&
1102 net_eq(tm_net(tm), net)) {
1080 *pp = tm->tcpm_next; 1103 *pp = tm->tcpm_next;
1081 kfree_rcu(tm, rcu_head); 1104 kfree_rcu(tm, rcu_head);
1082 found = true; 1105 found = true;
@@ -1126,6 +1149,9 @@ static int __net_init tcp_net_metrics_init(struct net *net)
1126 size_t size; 1149 size_t size;
1127 unsigned int slots; 1150 unsigned int slots;
1128 1151
1152 if (!net_eq(net, &init_net))
1153 return 0;
1154
1129 slots = tcpmhash_entries; 1155 slots = tcpmhash_entries;
1130 if (!slots) { 1156 if (!slots) {
1131 if (totalram_pages >= 128 * 1024) 1157 if (totalram_pages >= 128 * 1024)
@@ -1134,14 +1160,14 @@ static int __net_init tcp_net_metrics_init(struct net *net)
1134 slots = 8 * 1024; 1160 slots = 8 * 1024;
1135 } 1161 }
1136 1162
1137 net->ipv4.tcp_metrics_hash_log = order_base_2(slots); 1163 tcp_metrics_hash_log = order_base_2(slots);
1138 size = sizeof(struct tcpm_hash_bucket) << net->ipv4.tcp_metrics_hash_log; 1164 size = sizeof(struct tcpm_hash_bucket) << tcp_metrics_hash_log;
1139 1165
1140 net->ipv4.tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN); 1166 tcp_metrics_hash = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
1141 if (!net->ipv4.tcp_metrics_hash) 1167 if (!tcp_metrics_hash)
1142 net->ipv4.tcp_metrics_hash = vzalloc(size); 1168 tcp_metrics_hash = vzalloc(size);
1143 1169
1144 if (!net->ipv4.tcp_metrics_hash) 1170 if (!tcp_metrics_hash)
1145 return -ENOMEM; 1171 return -ENOMEM;
1146 1172
1147 return 0; 1173 return 0;
@@ -1149,19 +1175,7 @@ static int __net_init tcp_net_metrics_init(struct net *net)
1149 1175
1150static void __net_exit tcp_net_metrics_exit(struct net *net) 1176static void __net_exit tcp_net_metrics_exit(struct net *net)
1151{ 1177{
1152 unsigned int i; 1178 tcp_metrics_flush_all(net);
1153
1154 for (i = 0; i < (1U << net->ipv4.tcp_metrics_hash_log) ; i++) {
1155 struct tcp_metrics_block *tm, *next;
1156
1157 tm = rcu_dereference_protected(net->ipv4.tcp_metrics_hash[i].chain, 1);
1158 while (tm) {
1159 next = rcu_dereference_protected(tm->tcpm_next, 1);
1160 kfree(tm);
1161 tm = next;
1162 }
1163 }
1164 kvfree(net->ipv4.tcp_metrics_hash);
1165} 1179}
1166 1180
1167static __net_initdata struct pernet_operations tcp_net_metrics_ops = { 1181static __net_initdata struct pernet_operations tcp_net_metrics_ops = {
@@ -1175,16 +1189,10 @@ void __init tcp_metrics_init(void)
1175 1189
1176 ret = register_pernet_subsys(&tcp_net_metrics_ops); 1190 ret = register_pernet_subsys(&tcp_net_metrics_ops);
1177 if (ret < 0) 1191 if (ret < 0)
1178 goto cleanup; 1192 panic("Could not allocate the tcp_metrics hash table\n");
1193
1179 ret = genl_register_family_with_ops(&tcp_metrics_nl_family, 1194 ret = genl_register_family_with_ops(&tcp_metrics_nl_family,
1180 tcp_metrics_nl_ops); 1195 tcp_metrics_nl_ops);
1181 if (ret < 0) 1196 if (ret < 0)
1182 goto cleanup_subsys; 1197 panic("Could not register tcp_metrics generic netlink\n");
1183 return;
1184
1185cleanup_subsys:
1186 unregister_pernet_subsys(&tcp_net_metrics_ops);
1187
1188cleanup:
1189 return;
1190} 1198}
diff --git a/net/ipv4/tcp_minisocks.c b/net/ipv4/tcp_minisocks.c
index dd11ac7798c6..63d6311b5365 100644
--- a/net/ipv4/tcp_minisocks.c
+++ b/net/ipv4/tcp_minisocks.c
@@ -34,18 +34,7 @@ int sysctl_tcp_abort_on_overflow __read_mostly;
34 34
35struct inet_timewait_death_row tcp_death_row = { 35struct inet_timewait_death_row tcp_death_row = {
36 .sysctl_max_tw_buckets = NR_FILE * 2, 36 .sysctl_max_tw_buckets = NR_FILE * 2,
37 .period = TCP_TIMEWAIT_LEN / INET_TWDR_TWKILL_SLOTS,
38 .death_lock = __SPIN_LOCK_UNLOCKED(tcp_death_row.death_lock),
39 .hashinfo = &tcp_hashinfo, 37 .hashinfo = &tcp_hashinfo,
40 .tw_timer = TIMER_INITIALIZER(inet_twdr_hangman, 0,
41 (unsigned long)&tcp_death_row),
42 .twkill_work = __WORK_INITIALIZER(tcp_death_row.twkill_work,
43 inet_twdr_twkill_work),
44/* Short-time timewait calendar */
45
46 .twcal_hand = -1,
47 .twcal_timer = TIMER_INITIALIZER(inet_twdr_twcal_tick, 0,
48 (unsigned long)&tcp_death_row),
49}; 38};
50EXPORT_SYMBOL_GPL(tcp_death_row); 39EXPORT_SYMBOL_GPL(tcp_death_row);
51 40
@@ -158,7 +147,7 @@ tcp_timewait_state_process(struct inet_timewait_sock *tw, struct sk_buff *skb,
158 if (!th->fin || 147 if (!th->fin ||
159 TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) { 148 TCP_SKB_CB(skb)->end_seq != tcptw->tw_rcv_nxt + 1) {
160kill_with_rst: 149kill_with_rst:
161 inet_twsk_deschedule(tw, &tcp_death_row); 150 inet_twsk_deschedule(tw);
162 inet_twsk_put(tw); 151 inet_twsk_put(tw);
163 return TCP_TW_RST; 152 return TCP_TW_RST;
164 } 153 }
@@ -174,11 +163,9 @@ kill_with_rst:
174 if (tcp_death_row.sysctl_tw_recycle && 163 if (tcp_death_row.sysctl_tw_recycle &&
175 tcptw->tw_ts_recent_stamp && 164 tcptw->tw_ts_recent_stamp &&
176 tcp_tw_remember_stamp(tw)) 165 tcp_tw_remember_stamp(tw))
177 inet_twsk_schedule(tw, &tcp_death_row, tw->tw_timeout, 166 inet_twsk_schedule(tw, tw->tw_timeout);
178 TCP_TIMEWAIT_LEN);
179 else 167 else
180 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 168 inet_twsk_schedule(tw, TCP_TIMEWAIT_LEN);
181 TCP_TIMEWAIT_LEN);
182 return TCP_TW_ACK; 169 return TCP_TW_ACK;
183 } 170 }
184 171
@@ -211,13 +198,12 @@ kill_with_rst:
211 */ 198 */
212 if (sysctl_tcp_rfc1337 == 0) { 199 if (sysctl_tcp_rfc1337 == 0) {
213kill: 200kill:
214 inet_twsk_deschedule(tw, &tcp_death_row); 201 inet_twsk_deschedule(tw);
215 inet_twsk_put(tw); 202 inet_twsk_put(tw);
216 return TCP_TW_SUCCESS; 203 return TCP_TW_SUCCESS;
217 } 204 }
218 } 205 }
219 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 206 inet_twsk_schedule(tw, TCP_TIMEWAIT_LEN);
220 TCP_TIMEWAIT_LEN);
221 207
222 if (tmp_opt.saw_tstamp) { 208 if (tmp_opt.saw_tstamp) {
223 tcptw->tw_ts_recent = tmp_opt.rcv_tsval; 209 tcptw->tw_ts_recent = tmp_opt.rcv_tsval;
@@ -267,8 +253,7 @@ kill:
267 * Do not reschedule in the last case. 253 * Do not reschedule in the last case.
268 */ 254 */
269 if (paws_reject || th->ack) 255 if (paws_reject || th->ack)
270 inet_twsk_schedule(tw, &tcp_death_row, TCP_TIMEWAIT_LEN, 256 inet_twsk_schedule(tw, TCP_TIMEWAIT_LEN);
271 TCP_TIMEWAIT_LEN);
272 257
273 return tcp_timewait_check_oow_rate_limit( 258 return tcp_timewait_check_oow_rate_limit(
274 tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT); 259 tw, skb, LINUX_MIB_TCPACKSKIPPEDTIMEWAIT);
@@ -283,18 +268,17 @@ EXPORT_SYMBOL(tcp_timewait_state_process);
283 */ 268 */
284void tcp_time_wait(struct sock *sk, int state, int timeo) 269void tcp_time_wait(struct sock *sk, int state, int timeo)
285{ 270{
286 struct inet_timewait_sock *tw = NULL;
287 const struct inet_connection_sock *icsk = inet_csk(sk); 271 const struct inet_connection_sock *icsk = inet_csk(sk);
288 const struct tcp_sock *tp = tcp_sk(sk); 272 const struct tcp_sock *tp = tcp_sk(sk);
273 struct inet_timewait_sock *tw;
289 bool recycle_ok = false; 274 bool recycle_ok = false;
290 275
291 if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp) 276 if (tcp_death_row.sysctl_tw_recycle && tp->rx_opt.ts_recent_stamp)
292 recycle_ok = tcp_remember_stamp(sk); 277 recycle_ok = tcp_remember_stamp(sk);
293 278
294 if (tcp_death_row.tw_count < tcp_death_row.sysctl_max_tw_buckets) 279 tw = inet_twsk_alloc(sk, &tcp_death_row, state);
295 tw = inet_twsk_alloc(sk, state);
296 280
297 if (tw != NULL) { 281 if (tw) {
298 struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw); 282 struct tcp_timewait_sock *tcptw = tcp_twsk((struct sock *)tw);
299 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1); 283 const int rto = (icsk->icsk_rto << 2) - (icsk->icsk_rto >> 1);
300 struct inet_sock *inet = inet_sk(sk); 284 struct inet_sock *inet = inet_sk(sk);
@@ -332,7 +316,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)
332 struct tcp_md5sig_key *key; 316 struct tcp_md5sig_key *key;
333 tcptw->tw_md5_key = NULL; 317 tcptw->tw_md5_key = NULL;
334 key = tp->af_specific->md5_lookup(sk, sk); 318 key = tp->af_specific->md5_lookup(sk, sk);
335 if (key != NULL) { 319 if (key) {
336 tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC); 320 tcptw->tw_md5_key = kmemdup(key, sizeof(*key), GFP_ATOMIC);
337 if (tcptw->tw_md5_key && !tcp_alloc_md5sig_pool()) 321 if (tcptw->tw_md5_key && !tcp_alloc_md5sig_pool())
338 BUG(); 322 BUG();
@@ -355,8 +339,7 @@ void tcp_time_wait(struct sock *sk, int state, int timeo)
355 timeo = TCP_TIMEWAIT_LEN; 339 timeo = TCP_TIMEWAIT_LEN;
356 } 340 }
357 341
358 inet_twsk_schedule(tw, &tcp_death_row, timeo, 342 inet_twsk_schedule(tw, timeo);
359 TCP_TIMEWAIT_LEN);
360 inet_twsk_put(tw); 343 inet_twsk_put(tw);
361 } else { 344 } else {
362 /* Sorry, if we're out of memory, just CLOSE this 345 /* Sorry, if we're out of memory, just CLOSE this
@@ -454,7 +437,7 @@ struct sock *tcp_create_openreq_child(struct sock *sk, struct request_sock *req,
454{ 437{
455 struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC); 438 struct sock *newsk = inet_csk_clone_lock(sk, req, GFP_ATOMIC);
456 439
457 if (newsk != NULL) { 440 if (newsk) {
458 const struct inet_request_sock *ireq = inet_rsk(req); 441 const struct inet_request_sock *ireq = inet_rsk(req);
459 struct tcp_request_sock *treq = tcp_rsk(req); 442 struct tcp_request_sock *treq = tcp_rsk(req);
460 struct inet_connection_sock *newicsk = inet_csk(newsk); 443 struct inet_connection_sock *newicsk = inet_csk(newsk);
@@ -572,7 +555,6 @@ EXPORT_SYMBOL(tcp_create_openreq_child);
572 555
573struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb, 556struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
574 struct request_sock *req, 557 struct request_sock *req,
575 struct request_sock **prev,
576 bool fastopen) 558 bool fastopen)
577{ 559{
578 struct tcp_options_received tmp_opt; 560 struct tcp_options_received tmp_opt;
@@ -629,9 +611,16 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
629 LINUX_MIB_TCPACKSKIPPEDSYNRECV, 611 LINUX_MIB_TCPACKSKIPPEDSYNRECV,
630 &tcp_rsk(req)->last_oow_ack_time) && 612 &tcp_rsk(req)->last_oow_ack_time) &&
631 613
632 !inet_rtx_syn_ack(sk, req)) 614 !inet_rtx_syn_ack(sk, req)) {
633 req->expires = min(TCP_TIMEOUT_INIT << req->num_timeout, 615 unsigned long expires = jiffies;
634 TCP_RTO_MAX) + jiffies; 616
617 expires += min(TCP_TIMEOUT_INIT << req->num_timeout,
618 TCP_RTO_MAX);
619 if (!fastopen)
620 mod_timer_pending(&req->rsk_timer, expires);
621 else
622 req->rsk_timer.expires = expires;
623 }
635 return NULL; 624 return NULL;
636 } 625 }
637 626
@@ -763,10 +752,10 @@ struct sock *tcp_check_req(struct sock *sk, struct sk_buff *skb,
763 * socket is created, wait for troubles. 752 * socket is created, wait for troubles.
764 */ 753 */
765 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL); 754 child = inet_csk(sk)->icsk_af_ops->syn_recv_sock(sk, skb, req, NULL);
766 if (child == NULL) 755 if (!child)
767 goto listen_overflow; 756 goto listen_overflow;
768 757
769 inet_csk_reqsk_queue_unlink(sk, req, prev); 758 inet_csk_reqsk_queue_unlink(sk, req);
770 inet_csk_reqsk_queue_removed(sk, req); 759 inet_csk_reqsk_queue_removed(sk, req);
771 760
772 inet_csk_reqsk_queue_add(sk, req, child); 761 inet_csk_reqsk_queue_add(sk, req, child);
@@ -791,7 +780,7 @@ embryonic_reset:
791 tcp_reset(sk); 780 tcp_reset(sk);
792 } 781 }
793 if (!fastopen) { 782 if (!fastopen) {
794 inet_csk_reqsk_queue_drop(sk, req, prev); 783 inet_csk_reqsk_queue_drop(sk, req);
795 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_EMBRYONICRSTS); 784 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_EMBRYONICRSTS);
796 } 785 }
797 return NULL; 786 return NULL;
diff --git a/net/ipv4/tcp_offload.c b/net/ipv4/tcp_offload.c
index 9d7930ba8e0f..3f7c2fca5431 100644
--- a/net/ipv4/tcp_offload.c
+++ b/net/ipv4/tcp_offload.c
@@ -29,8 +29,8 @@ static void tcp_gso_tstamp(struct sk_buff *skb, unsigned int ts_seq,
29 } 29 }
30} 30}
31 31
32struct sk_buff *tcp4_gso_segment(struct sk_buff *skb, 32static struct sk_buff *tcp4_gso_segment(struct sk_buff *skb,
33 netdev_features_t features) 33 netdev_features_t features)
34{ 34{
35 if (!pskb_may_pull(skb, sizeof(struct tcphdr))) 35 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
36 return ERR_PTR(-EINVAL); 36 return ERR_PTR(-EINVAL);
diff --git a/net/ipv4/tcp_output.c b/net/ipv4/tcp_output.c
index 1db253e36045..8c8d7e06b72f 100644
--- a/net/ipv4/tcp_output.c
+++ b/net/ipv4/tcp_output.c
@@ -518,17 +518,26 @@ static void tcp_options_write(__be32 *ptr, struct tcp_sock *tp,
518 518
519 if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) { 519 if (unlikely(OPTION_FAST_OPEN_COOKIE & options)) {
520 struct tcp_fastopen_cookie *foc = opts->fastopen_cookie; 520 struct tcp_fastopen_cookie *foc = opts->fastopen_cookie;
521 u8 *p = (u8 *)ptr;
522 u32 len; /* Fast Open option length */
523
524 if (foc->exp) {
525 len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
526 *ptr = htonl((TCPOPT_EXP << 24) | (len << 16) |
527 TCPOPT_FASTOPEN_MAGIC);
528 p += TCPOLEN_EXP_FASTOPEN_BASE;
529 } else {
530 len = TCPOLEN_FASTOPEN_BASE + foc->len;
531 *p++ = TCPOPT_FASTOPEN;
532 *p++ = len;
533 }
521 534
522 *ptr++ = htonl((TCPOPT_EXP << 24) | 535 memcpy(p, foc->val, foc->len);
523 ((TCPOLEN_EXP_FASTOPEN_BASE + foc->len) << 16) | 536 if ((len & 3) == 2) {
524 TCPOPT_FASTOPEN_MAGIC); 537 p[foc->len] = TCPOPT_NOP;
525 538 p[foc->len + 1] = TCPOPT_NOP;
526 memcpy(ptr, foc->val, foc->len);
527 if ((foc->len & 3) == 2) {
528 u8 *align = ((u8 *)ptr) + foc->len;
529 align[0] = align[1] = TCPOPT_NOP;
530 } 539 }
531 ptr += (foc->len + 3) >> 2; 540 ptr += (len + 3) >> 2;
532 } 541 }
533} 542}
534 543
@@ -565,7 +574,7 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
565 opts->mss = tcp_advertise_mss(sk); 574 opts->mss = tcp_advertise_mss(sk);
566 remaining -= TCPOLEN_MSS_ALIGNED; 575 remaining -= TCPOLEN_MSS_ALIGNED;
567 576
568 if (likely(sysctl_tcp_timestamps && *md5 == NULL)) { 577 if (likely(sysctl_tcp_timestamps && !*md5)) {
569 opts->options |= OPTION_TS; 578 opts->options |= OPTION_TS;
570 opts->tsval = tcp_skb_timestamp(skb) + tp->tsoffset; 579 opts->tsval = tcp_skb_timestamp(skb) + tp->tsoffset;
571 opts->tsecr = tp->rx_opt.ts_recent; 580 opts->tsecr = tp->rx_opt.ts_recent;
@@ -583,13 +592,17 @@ static unsigned int tcp_syn_options(struct sock *sk, struct sk_buff *skb,
583 } 592 }
584 593
585 if (fastopen && fastopen->cookie.len >= 0) { 594 if (fastopen && fastopen->cookie.len >= 0) {
586 u32 need = TCPOLEN_EXP_FASTOPEN_BASE + fastopen->cookie.len; 595 u32 need = fastopen->cookie.len;
596
597 need += fastopen->cookie.exp ? TCPOLEN_EXP_FASTOPEN_BASE :
598 TCPOLEN_FASTOPEN_BASE;
587 need = (need + 3) & ~3U; /* Align to 32 bits */ 599 need = (need + 3) & ~3U; /* Align to 32 bits */
588 if (remaining >= need) { 600 if (remaining >= need) {
589 opts->options |= OPTION_FAST_OPEN_COOKIE; 601 opts->options |= OPTION_FAST_OPEN_COOKIE;
590 opts->fastopen_cookie = &fastopen->cookie; 602 opts->fastopen_cookie = &fastopen->cookie;
591 remaining -= need; 603 remaining -= need;
592 tp->syn_fastopen = 1; 604 tp->syn_fastopen = 1;
605 tp->syn_fastopen_exp = fastopen->cookie.exp ? 1 : 0;
593 } 606 }
594 } 607 }
595 608
@@ -601,15 +614,14 @@ static unsigned int tcp_synack_options(struct sock *sk,
601 struct request_sock *req, 614 struct request_sock *req,
602 unsigned int mss, struct sk_buff *skb, 615 unsigned int mss, struct sk_buff *skb,
603 struct tcp_out_options *opts, 616 struct tcp_out_options *opts,
604 struct tcp_md5sig_key **md5, 617 const struct tcp_md5sig_key *md5,
605 struct tcp_fastopen_cookie *foc) 618 struct tcp_fastopen_cookie *foc)
606{ 619{
607 struct inet_request_sock *ireq = inet_rsk(req); 620 struct inet_request_sock *ireq = inet_rsk(req);
608 unsigned int remaining = MAX_TCP_OPTION_SPACE; 621 unsigned int remaining = MAX_TCP_OPTION_SPACE;
609 622
610#ifdef CONFIG_TCP_MD5SIG 623#ifdef CONFIG_TCP_MD5SIG
611 *md5 = tcp_rsk(req)->af_specific->md5_lookup(sk, req); 624 if (md5) {
612 if (*md5) {
613 opts->options |= OPTION_MD5; 625 opts->options |= OPTION_MD5;
614 remaining -= TCPOLEN_MD5SIG_ALIGNED; 626 remaining -= TCPOLEN_MD5SIG_ALIGNED;
615 627
@@ -620,8 +632,6 @@ static unsigned int tcp_synack_options(struct sock *sk,
620 */ 632 */
621 ireq->tstamp_ok &= !ireq->sack_ok; 633 ireq->tstamp_ok &= !ireq->sack_ok;
622 } 634 }
623#else
624 *md5 = NULL;
625#endif 635#endif
626 636
627 /* We always send an MSS option. */ 637 /* We always send an MSS option. */
@@ -645,7 +655,10 @@ static unsigned int tcp_synack_options(struct sock *sk,
645 remaining -= TCPOLEN_SACKPERM_ALIGNED; 655 remaining -= TCPOLEN_SACKPERM_ALIGNED;
646 } 656 }
647 if (foc != NULL && foc->len >= 0) { 657 if (foc != NULL && foc->len >= 0) {
648 u32 need = TCPOLEN_EXP_FASTOPEN_BASE + foc->len; 658 u32 need = foc->len;
659
660 need += foc->exp ? TCPOLEN_EXP_FASTOPEN_BASE :
661 TCPOLEN_FASTOPEN_BASE;
649 need = (need + 3) & ~3U; /* Align to 32 bits */ 662 need = (need + 3) & ~3U; /* Align to 32 bits */
650 if (remaining >= need) { 663 if (remaining >= need) {
651 opts->options |= OPTION_FAST_OPEN_COOKIE; 664 opts->options |= OPTION_FAST_OPEN_COOKIE;
@@ -989,7 +1002,7 @@ static int tcp_transmit_skb(struct sock *sk, struct sk_buff *skb, int clone_it,
989 if (md5) { 1002 if (md5) {
990 sk_nocaps_add(sk, NETIF_F_GSO_MASK); 1003 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
991 tp->af_specific->calc_md5_hash(opts.hash_location, 1004 tp->af_specific->calc_md5_hash(opts.hash_location,
992 md5, sk, NULL, skb); 1005 md5, sk, skb);
993 } 1006 }
994#endif 1007#endif
995 1008
@@ -1151,7 +1164,7 @@ int tcp_fragment(struct sock *sk, struct sk_buff *skb, u32 len,
1151 1164
1152 /* Get a new skb... force flag on. */ 1165 /* Get a new skb... force flag on. */
1153 buff = sk_stream_alloc_skb(sk, nsize, gfp); 1166 buff = sk_stream_alloc_skb(sk, nsize, gfp);
1154 if (buff == NULL) 1167 if (!buff)
1155 return -ENOMEM; /* We'll just try again later. */ 1168 return -ENOMEM; /* We'll just try again later. */
1156 1169
1157 sk->sk_wmem_queued += buff->truesize; 1170 sk->sk_wmem_queued += buff->truesize;
@@ -1354,6 +1367,8 @@ void tcp_mtup_init(struct sock *sk)
1354 icsk->icsk_af_ops->net_header_len; 1367 icsk->icsk_af_ops->net_header_len;
1355 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, net->ipv4.sysctl_tcp_base_mss); 1368 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, net->ipv4.sysctl_tcp_base_mss);
1356 icsk->icsk_mtup.probe_size = 0; 1369 icsk->icsk_mtup.probe_size = 0;
1370 if (icsk->icsk_mtup.enabled)
1371 icsk->icsk_mtup.probe_timestamp = tcp_time_stamp;
1357} 1372}
1358EXPORT_SYMBOL(tcp_mtup_init); 1373EXPORT_SYMBOL(tcp_mtup_init);
1359 1374
@@ -1708,7 +1723,7 @@ static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
1708 return tcp_fragment(sk, skb, len, mss_now, gfp); 1723 return tcp_fragment(sk, skb, len, mss_now, gfp);
1709 1724
1710 buff = sk_stream_alloc_skb(sk, 0, gfp); 1725 buff = sk_stream_alloc_skb(sk, 0, gfp);
1711 if (unlikely(buff == NULL)) 1726 if (unlikely(!buff))
1712 return -ENOMEM; 1727 return -ENOMEM;
1713 1728
1714 sk->sk_wmem_queued += buff->truesize; 1729 sk->sk_wmem_queued += buff->truesize;
@@ -1752,20 +1767,23 @@ static int tso_fragment(struct sock *sk, struct sk_buff *skb, unsigned int len,
1752static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb, 1767static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb,
1753 bool *is_cwnd_limited, u32 max_segs) 1768 bool *is_cwnd_limited, u32 max_segs)
1754{ 1769{
1755 struct tcp_sock *tp = tcp_sk(sk);
1756 const struct inet_connection_sock *icsk = inet_csk(sk); 1770 const struct inet_connection_sock *icsk = inet_csk(sk);
1757 u32 send_win, cong_win, limit, in_flight; 1771 u32 age, send_win, cong_win, limit, in_flight;
1772 struct tcp_sock *tp = tcp_sk(sk);
1773 struct skb_mstamp now;
1774 struct sk_buff *head;
1758 int win_divisor; 1775 int win_divisor;
1759 1776
1760 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN) 1777 if (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)
1761 goto send_now; 1778 goto send_now;
1762 1779
1763 if (icsk->icsk_ca_state != TCP_CA_Open) 1780 if (!((1 << icsk->icsk_ca_state) & (TCPF_CA_Open | TCPF_CA_CWR)))
1764 goto send_now; 1781 goto send_now;
1765 1782
1766 /* Defer for less than two clock ticks. */ 1783 /* Avoid bursty behavior by allowing defer
1767 if (tp->tso_deferred && 1784 * only if the last write was recent.
1768 (((u32)jiffies << 1) >> 1) - (tp->tso_deferred >> 1) > 1) 1785 */
1786 if ((s32)(tcp_time_stamp - tp->lsndtime) > 0)
1769 goto send_now; 1787 goto send_now;
1770 1788
1771 in_flight = tcp_packets_in_flight(tp); 1789 in_flight = tcp_packets_in_flight(tp);
@@ -1807,11 +1825,14 @@ static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb,
1807 goto send_now; 1825 goto send_now;
1808 } 1826 }
1809 1827
1810 /* Ok, it looks like it is advisable to defer. 1828 head = tcp_write_queue_head(sk);
1811 * Do not rearm the timer if already set to not break TCP ACK clocking. 1829 skb_mstamp_get(&now);
1812 */ 1830 age = skb_mstamp_us_delta(&now, &head->skb_mstamp);
1813 if (!tp->tso_deferred) 1831 /* If next ACK is likely to come too late (half srtt), do not defer */
1814 tp->tso_deferred = 1 | (jiffies << 1); 1832 if (age < (tp->srtt_us >> 4))
1833 goto send_now;
1834
1835 /* Ok, it looks like it is advisable to defer. */
1815 1836
1816 if (cong_win < send_win && cong_win < skb->len) 1837 if (cong_win < send_win && cong_win < skb->len)
1817 *is_cwnd_limited = true; 1838 *is_cwnd_limited = true;
@@ -1819,10 +1840,34 @@ static bool tcp_tso_should_defer(struct sock *sk, struct sk_buff *skb,
1819 return true; 1840 return true;
1820 1841
1821send_now: 1842send_now:
1822 tp->tso_deferred = 0;
1823 return false; 1843 return false;
1824} 1844}
1825 1845
1846static inline void tcp_mtu_check_reprobe(struct sock *sk)
1847{
1848 struct inet_connection_sock *icsk = inet_csk(sk);
1849 struct tcp_sock *tp = tcp_sk(sk);
1850 struct net *net = sock_net(sk);
1851 u32 interval;
1852 s32 delta;
1853
1854 interval = net->ipv4.sysctl_tcp_probe_interval;
1855 delta = tcp_time_stamp - icsk->icsk_mtup.probe_timestamp;
1856 if (unlikely(delta >= interval * HZ)) {
1857 int mss = tcp_current_mss(sk);
1858
1859 /* Update current search range */
1860 icsk->icsk_mtup.probe_size = 0;
1861 icsk->icsk_mtup.search_high = tp->rx_opt.mss_clamp +
1862 sizeof(struct tcphdr) +
1863 icsk->icsk_af_ops->net_header_len;
1864 icsk->icsk_mtup.search_low = tcp_mss_to_mtu(sk, mss);
1865
1866 /* Update probe time stamp */
1867 icsk->icsk_mtup.probe_timestamp = tcp_time_stamp;
1868 }
1869}
1870
1826/* Create a new MTU probe if we are ready. 1871/* Create a new MTU probe if we are ready.
1827 * MTU probe is regularly attempting to increase the path MTU by 1872 * MTU probe is regularly attempting to increase the path MTU by
1828 * deliberately sending larger packets. This discovers routing 1873 * deliberately sending larger packets. This discovers routing
@@ -1837,11 +1882,13 @@ static int tcp_mtu_probe(struct sock *sk)
1837 struct tcp_sock *tp = tcp_sk(sk); 1882 struct tcp_sock *tp = tcp_sk(sk);
1838 struct inet_connection_sock *icsk = inet_csk(sk); 1883 struct inet_connection_sock *icsk = inet_csk(sk);
1839 struct sk_buff *skb, *nskb, *next; 1884 struct sk_buff *skb, *nskb, *next;
1885 struct net *net = sock_net(sk);
1840 int len; 1886 int len;
1841 int probe_size; 1887 int probe_size;
1842 int size_needed; 1888 int size_needed;
1843 int copy; 1889 int copy;
1844 int mss_now; 1890 int mss_now;
1891 int interval;
1845 1892
1846 /* Not currently probing/verifying, 1893 /* Not currently probing/verifying,
1847 * not in recovery, 1894 * not in recovery,
@@ -1854,12 +1901,25 @@ static int tcp_mtu_probe(struct sock *sk)
1854 tp->rx_opt.num_sacks || tp->rx_opt.dsack) 1901 tp->rx_opt.num_sacks || tp->rx_opt.dsack)
1855 return -1; 1902 return -1;
1856 1903
1857 /* Very simple search strategy: just double the MSS. */ 1904 /* Use binary search for probe_size between tcp_mss_base,
1905 * and current mss_clamp. if (search_high - search_low)
1906 * smaller than a threshold, backoff from probing.
1907 */
1858 mss_now = tcp_current_mss(sk); 1908 mss_now = tcp_current_mss(sk);
1859 probe_size = 2 * tp->mss_cache; 1909 probe_size = tcp_mtu_to_mss(sk, (icsk->icsk_mtup.search_high +
1910 icsk->icsk_mtup.search_low) >> 1);
1860 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache; 1911 size_needed = probe_size + (tp->reordering + 1) * tp->mss_cache;
1861 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high)) { 1912 interval = icsk->icsk_mtup.search_high - icsk->icsk_mtup.search_low;
1862 /* TODO: set timer for probe_converge_event */ 1913 /* When misfortune happens, we are reprobing actively,
1914 * and then reprobe timer has expired. We stick with current
1915 * probing process by not resetting search range to its orignal.
1916 */
1917 if (probe_size > tcp_mtu_to_mss(sk, icsk->icsk_mtup.search_high) ||
1918 interval < net->ipv4.sysctl_tcp_probe_threshold) {
1919 /* Check whether enough time has elaplased for
1920 * another round of probing.
1921 */
1922 tcp_mtu_check_reprobe(sk);
1863 return -1; 1923 return -1;
1864 } 1924 }
1865 1925
@@ -1881,7 +1941,8 @@ static int tcp_mtu_probe(struct sock *sk)
1881 } 1941 }
1882 1942
1883 /* We're allowed to probe. Build it now. */ 1943 /* We're allowed to probe. Build it now. */
1884 if ((nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC)) == NULL) 1944 nskb = sk_stream_alloc_skb(sk, probe_size, GFP_ATOMIC);
1945 if (!nskb)
1885 return -1; 1946 return -1;
1886 sk->sk_wmem_queued += nskb->truesize; 1947 sk->sk_wmem_queued += nskb->truesize;
1887 sk_mem_charge(sk, nskb->truesize); 1948 sk_mem_charge(sk, nskb->truesize);
@@ -2179,7 +2240,7 @@ void tcp_send_loss_probe(struct sock *sk)
2179 int mss = tcp_current_mss(sk); 2240 int mss = tcp_current_mss(sk);
2180 int err = -1; 2241 int err = -1;
2181 2242
2182 if (tcp_send_head(sk) != NULL) { 2243 if (tcp_send_head(sk)) {
2183 err = tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC); 2244 err = tcp_write_xmit(sk, mss, TCP_NAGLE_OFF, 2, GFP_ATOMIC);
2184 goto rearm_timer; 2245 goto rearm_timer;
2185 } 2246 }
@@ -2689,7 +2750,7 @@ void tcp_xmit_retransmit_queue(struct sock *sk)
2689 if (skb == tcp_send_head(sk)) 2750 if (skb == tcp_send_head(sk))
2690 break; 2751 break;
2691 /* we could do better than to assign each time */ 2752 /* we could do better than to assign each time */
2692 if (hole == NULL) 2753 if (!hole)
2693 tp->retransmit_skb_hint = skb; 2754 tp->retransmit_skb_hint = skb;
2694 2755
2695 /* Assume this retransmit will generate 2756 /* Assume this retransmit will generate
@@ -2713,7 +2774,7 @@ begin_fwd:
2713 if (!tcp_can_forward_retransmit(sk)) 2774 if (!tcp_can_forward_retransmit(sk))
2714 break; 2775 break;
2715 /* Backtrack if necessary to non-L'ed skb */ 2776 /* Backtrack if necessary to non-L'ed skb */
2716 if (hole != NULL) { 2777 if (hole) {
2717 skb = hole; 2778 skb = hole;
2718 hole = NULL; 2779 hole = NULL;
2719 } 2780 }
@@ -2721,7 +2782,7 @@ begin_fwd:
2721 goto begin_fwd; 2782 goto begin_fwd;
2722 2783
2723 } else if (!(sacked & TCPCB_LOST)) { 2784 } else if (!(sacked & TCPCB_LOST)) {
2724 if (hole == NULL && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED))) 2785 if (!hole && !(sacked & (TCPCB_SACKED_RETRANS|TCPCB_SACKED_ACKED)))
2725 hole = skb; 2786 hole = skb;
2726 continue; 2787 continue;
2727 2788
@@ -2766,7 +2827,7 @@ void tcp_send_fin(struct sock *sk)
2766 */ 2827 */
2767 mss_now = tcp_current_mss(sk); 2828 mss_now = tcp_current_mss(sk);
2768 2829
2769 if (tcp_send_head(sk) != NULL) { 2830 if (tcp_send_head(sk)) {
2770 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_FIN; 2831 TCP_SKB_CB(skb)->tcp_flags |= TCPHDR_FIN;
2771 TCP_SKB_CB(skb)->end_seq++; 2832 TCP_SKB_CB(skb)->end_seq++;
2772 tp->write_seq++; 2833 tp->write_seq++;
@@ -2824,14 +2885,14 @@ int tcp_send_synack(struct sock *sk)
2824 struct sk_buff *skb; 2885 struct sk_buff *skb;
2825 2886
2826 skb = tcp_write_queue_head(sk); 2887 skb = tcp_write_queue_head(sk);
2827 if (skb == NULL || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) { 2888 if (!skb || !(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_SYN)) {
2828 pr_debug("%s: wrong queue state\n", __func__); 2889 pr_debug("%s: wrong queue state\n", __func__);
2829 return -EFAULT; 2890 return -EFAULT;
2830 } 2891 }
2831 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) { 2892 if (!(TCP_SKB_CB(skb)->tcp_flags & TCPHDR_ACK)) {
2832 if (skb_cloned(skb)) { 2893 if (skb_cloned(skb)) {
2833 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC); 2894 struct sk_buff *nskb = skb_copy(skb, GFP_ATOMIC);
2834 if (nskb == NULL) 2895 if (!nskb)
2835 return -ENOMEM; 2896 return -ENOMEM;
2836 tcp_unlink_write_queue(skb, sk); 2897 tcp_unlink_write_queue(skb, sk);
2837 __skb_header_release(nskb); 2898 __skb_header_release(nskb);
@@ -2866,7 +2927,7 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2866 struct tcp_sock *tp = tcp_sk(sk); 2927 struct tcp_sock *tp = tcp_sk(sk);
2867 struct tcphdr *th; 2928 struct tcphdr *th;
2868 struct sk_buff *skb; 2929 struct sk_buff *skb;
2869 struct tcp_md5sig_key *md5; 2930 struct tcp_md5sig_key *md5 = NULL;
2870 int tcp_header_size; 2931 int tcp_header_size;
2871 int mss; 2932 int mss;
2872 2933
@@ -2879,7 +2940,6 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2879 skb_reserve(skb, MAX_TCP_HEADER); 2940 skb_reserve(skb, MAX_TCP_HEADER);
2880 2941
2881 skb_dst_set(skb, dst); 2942 skb_dst_set(skb, dst);
2882 security_skb_owned_by(skb, sk);
2883 2943
2884 mss = dst_metric_advmss(dst); 2944 mss = dst_metric_advmss(dst);
2885 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss) 2945 if (tp->rx_opt.user_mss && tp->rx_opt.user_mss < mss)
@@ -2892,7 +2952,12 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2892 else 2952 else
2893#endif 2953#endif
2894 skb_mstamp_get(&skb->skb_mstamp); 2954 skb_mstamp_get(&skb->skb_mstamp);
2895 tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, &md5, 2955
2956#ifdef CONFIG_TCP_MD5SIG
2957 rcu_read_lock();
2958 md5 = tcp_rsk(req)->af_specific->req_md5_lookup(sk, req_to_sk(req));
2959#endif
2960 tcp_header_size = tcp_synack_options(sk, req, mss, skb, &opts, md5,
2896 foc) + sizeof(*th); 2961 foc) + sizeof(*th);
2897 2962
2898 skb_push(skb, tcp_header_size); 2963 skb_push(skb, tcp_header_size);
@@ -2923,12 +2988,14 @@ struct sk_buff *tcp_make_synack(struct sock *sk, struct dst_entry *dst,
2923 2988
2924#ifdef CONFIG_TCP_MD5SIG 2989#ifdef CONFIG_TCP_MD5SIG
2925 /* Okay, we have all we need - do the md5 hash if needed */ 2990 /* Okay, we have all we need - do the md5 hash if needed */
2926 if (md5) { 2991 if (md5)
2927 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location, 2992 tcp_rsk(req)->af_specific->calc_md5_hash(opts.hash_location,
2928 md5, NULL, req, skb); 2993 md5, req_to_sk(req), skb);
2929 } 2994 rcu_read_unlock();
2930#endif 2995#endif
2931 2996
2997 /* Do not fool tcpdump (if any), clean our debris */
2998 skb->tstamp.tv64 = 0;
2932 return skb; 2999 return skb;
2933} 3000}
2934EXPORT_SYMBOL(tcp_make_synack); 3001EXPORT_SYMBOL(tcp_make_synack);
@@ -2966,7 +3033,7 @@ static void tcp_connect_init(struct sock *sk)
2966 (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0); 3033 (sysctl_tcp_timestamps ? TCPOLEN_TSTAMP_ALIGNED : 0);
2967 3034
2968#ifdef CONFIG_TCP_MD5SIG 3035#ifdef CONFIG_TCP_MD5SIG
2969 if (tp->af_specific->md5_lookup(sk, sk) != NULL) 3036 if (tp->af_specific->md5_lookup(sk, sk))
2970 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED; 3037 tp->tcp_header_len += TCPOLEN_MD5SIG_ALIGNED;
2971#endif 3038#endif
2972 3039
@@ -3252,7 +3319,7 @@ void tcp_send_ack(struct sock *sk)
3252 * sock. 3319 * sock.
3253 */ 3320 */
3254 buff = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC)); 3321 buff = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
3255 if (buff == NULL) { 3322 if (!buff) {
3256 inet_csk_schedule_ack(sk); 3323 inet_csk_schedule_ack(sk);
3257 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN; 3324 inet_csk(sk)->icsk_ack.ato = TCP_ATO_MIN;
3258 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK, 3325 inet_csk_reset_xmit_timer(sk, ICSK_TIME_DACK,
@@ -3296,7 +3363,7 @@ static int tcp_xmit_probe_skb(struct sock *sk, int urgent)
3296 3363
3297 /* We don't queue it, tcp_transmit_skb() sets ownership. */ 3364 /* We don't queue it, tcp_transmit_skb() sets ownership. */
3298 skb = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC)); 3365 skb = alloc_skb(MAX_TCP_HEADER, sk_gfp_atomic(sk, GFP_ATOMIC));
3299 if (skb == NULL) 3366 if (!skb)
3300 return -1; 3367 return -1;
3301 3368
3302 /* Reserve space for headers and set control bits. */ 3369 /* Reserve space for headers and set control bits. */
@@ -3327,8 +3394,8 @@ int tcp_write_wakeup(struct sock *sk)
3327 if (sk->sk_state == TCP_CLOSE) 3394 if (sk->sk_state == TCP_CLOSE)
3328 return -1; 3395 return -1;
3329 3396
3330 if ((skb = tcp_send_head(sk)) != NULL && 3397 skb = tcp_send_head(sk);
3331 before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) { 3398 if (skb && before(TCP_SKB_CB(skb)->seq, tcp_wnd_end(tp))) {
3332 int err; 3399 int err;
3333 unsigned int mss = tcp_current_mss(sk); 3400 unsigned int mss = tcp_current_mss(sk);
3334 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq; 3401 unsigned int seg_size = tcp_wnd_end(tp) - TCP_SKB_CB(skb)->seq;
diff --git a/net/ipv4/tcp_timer.c b/net/ipv4/tcp_timer.c
index 0732b787904e..8c65dc147d8b 100644
--- a/net/ipv4/tcp_timer.c
+++ b/net/ipv4/tcp_timer.c
@@ -107,6 +107,7 @@ static void tcp_mtu_probing(struct inet_connection_sock *icsk, struct sock *sk)
107 if (net->ipv4.sysctl_tcp_mtu_probing) { 107 if (net->ipv4.sysctl_tcp_mtu_probing) {
108 if (!icsk->icsk_mtup.enabled) { 108 if (!icsk->icsk_mtup.enabled) {
109 icsk->icsk_mtup.enabled = 1; 109 icsk->icsk_mtup.enabled = 1;
110 icsk->icsk_mtup.probe_timestamp = tcp_time_stamp;
110 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie); 111 tcp_sync_mss(sk, icsk->icsk_pmtu_cookie);
111 } else { 112 } else {
112 struct net *net = sock_net(sk); 113 struct net *net = sock_net(sk);
@@ -166,7 +167,7 @@ static int tcp_write_timeout(struct sock *sk)
166 if (icsk->icsk_retransmits) { 167 if (icsk->icsk_retransmits) {
167 dst_negative_advice(sk); 168 dst_negative_advice(sk);
168 if (tp->syn_fastopen || tp->syn_data) 169 if (tp->syn_fastopen || tp->syn_data)
169 tcp_fastopen_cache_set(sk, 0, NULL, true); 170 tcp_fastopen_cache_set(sk, 0, NULL, true, 0);
170 if (tp->syn_data) 171 if (tp->syn_data)
171 NET_INC_STATS_BH(sock_net(sk), 172 NET_INC_STATS_BH(sock_net(sk),
172 LINUX_MIB_TCPFASTOPENACTIVEFAIL); 173 LINUX_MIB_TCPFASTOPENACTIVEFAIL);
@@ -326,7 +327,7 @@ static void tcp_fastopen_synack_timer(struct sock *sk)
326 struct request_sock *req; 327 struct request_sock *req;
327 328
328 req = tcp_sk(sk)->fastopen_rsk; 329 req = tcp_sk(sk)->fastopen_rsk;
329 req->rsk_ops->syn_ack_timeout(sk, req); 330 req->rsk_ops->syn_ack_timeout(req);
330 331
331 if (req->num_timeout >= max_retries) { 332 if (req->num_timeout >= max_retries) {
332 tcp_write_err(sk); 333 tcp_write_err(sk);
@@ -538,19 +539,11 @@ static void tcp_write_timer(unsigned long data)
538 sock_put(sk); 539 sock_put(sk);
539} 540}
540 541
541/* 542void tcp_syn_ack_timeout(const struct request_sock *req)
542 * Timer for listening sockets
543 */
544
545static void tcp_synack_timer(struct sock *sk)
546{ 543{
547 inet_csk_reqsk_queue_prune(sk, TCP_SYNQ_INTERVAL, 544 struct net *net = read_pnet(&inet_rsk(req)->ireq_net);
548 TCP_TIMEOUT_INIT, TCP_RTO_MAX);
549}
550 545
551void tcp_syn_ack_timeout(struct sock *sk, struct request_sock *req) 546 NET_INC_STATS_BH(net, LINUX_MIB_TCPTIMEOUTS);
552{
553 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPTIMEOUTS);
554} 547}
555EXPORT_SYMBOL(tcp_syn_ack_timeout); 548EXPORT_SYMBOL(tcp_syn_ack_timeout);
556 549
@@ -582,7 +575,7 @@ static void tcp_keepalive_timer (unsigned long data)
582 } 575 }
583 576
584 if (sk->sk_state == TCP_LISTEN) { 577 if (sk->sk_state == TCP_LISTEN) {
585 tcp_synack_timer(sk); 578 pr_err("Hmm... keepalive on a LISTEN ???\n");
586 goto out; 579 goto out;
587 } 580 }
588 581
diff --git a/net/ipv4/tcp_vegas.c b/net/ipv4/tcp_vegas.c
index a6afde666ab1..c71a1b8f7bde 100644
--- a/net/ipv4/tcp_vegas.c
+++ b/net/ipv4/tcp_vegas.c
@@ -286,7 +286,7 @@ static void tcp_vegas_cong_avoid(struct sock *sk, u32 ack, u32 acked)
286} 286}
287 287
288/* Extract info for Tcp socket info provided via netlink. */ 288/* Extract info for Tcp socket info provided via netlink. */
289void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb) 289int tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
290{ 290{
291 const struct vegas *ca = inet_csk_ca(sk); 291 const struct vegas *ca = inet_csk_ca(sk);
292 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) { 292 if (ext & (1 << (INET_DIAG_VEGASINFO - 1))) {
@@ -297,8 +297,9 @@ void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb)
297 .tcpv_minrtt = ca->minRTT, 297 .tcpv_minrtt = ca->minRTT,
298 }; 298 };
299 299
300 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info); 300 return nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
301 } 301 }
302 return 0;
302} 303}
303EXPORT_SYMBOL_GPL(tcp_vegas_get_info); 304EXPORT_SYMBOL_GPL(tcp_vegas_get_info);
304 305
diff --git a/net/ipv4/tcp_vegas.h b/net/ipv4/tcp_vegas.h
index 0531b99d8637..e8a6b33cc61d 100644
--- a/net/ipv4/tcp_vegas.h
+++ b/net/ipv4/tcp_vegas.h
@@ -19,6 +19,6 @@ void tcp_vegas_init(struct sock *sk);
19void tcp_vegas_state(struct sock *sk, u8 ca_state); 19void tcp_vegas_state(struct sock *sk, u8 ca_state);
20void 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);
21void 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);
22void tcp_vegas_get_info(struct sock *sk, u32 ext, struct sk_buff *skb); 22int 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_westwood.c b/net/ipv4/tcp_westwood.c
index bb63fba47d47..b3c57cceb990 100644
--- a/net/ipv4/tcp_westwood.c
+++ b/net/ipv4/tcp_westwood.c
@@ -256,8 +256,7 @@ static void tcp_westwood_event(struct sock *sk, enum tcp_ca_event event)
256} 256}
257 257
258/* Extract info for Tcp socket info provided via netlink. */ 258/* Extract info for Tcp socket info provided via netlink. */
259static void tcp_westwood_info(struct sock *sk, u32 ext, 259static int tcp_westwood_info(struct sock *sk, u32 ext, struct sk_buff *skb)
260 struct sk_buff *skb)
261{ 260{
262 const struct westwood *ca = inet_csk_ca(sk); 261 const struct westwood *ca = inet_csk_ca(sk);
263 262
@@ -268,8 +267,9 @@ static void tcp_westwood_info(struct sock *sk, u32 ext,
268 .tcpv_minrtt = jiffies_to_usecs(ca->rtt_min), 267 .tcpv_minrtt = jiffies_to_usecs(ca->rtt_min),
269 }; 268 };
270 269
271 nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info); 270 return nla_put(skb, INET_DIAG_VEGASINFO, sizeof(info), &info);
272 } 271 }
272 return 0;
273} 273}
274 274
275static struct tcp_congestion_ops tcp_westwood __read_mostly = { 275static struct tcp_congestion_ops tcp_westwood __read_mostly = {
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 97ef1f8b7be8..d10b7e0112eb 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -318,8 +318,8 @@ static int ipv4_rcv_saddr_equal(const struct sock *sk1, const struct sock *sk2)
318 inet1->inet_rcv_saddr == inet2->inet_rcv_saddr)); 318 inet1->inet_rcv_saddr == inet2->inet_rcv_saddr));
319} 319}
320 320
321static unsigned int udp4_portaddr_hash(struct net *net, __be32 saddr, 321static u32 udp4_portaddr_hash(const struct net *net, __be32 saddr,
322 unsigned int port) 322 unsigned int port)
323{ 323{
324 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port; 324 return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
325} 325}
@@ -421,9 +421,9 @@ static inline int compute_score2(struct sock *sk, struct net *net,
421 return score; 421 return score;
422} 422}
423 423
424static unsigned int udp_ehashfn(struct net *net, const __be32 laddr, 424static u32 udp_ehashfn(const struct net *net, const __be32 laddr,
425 const __u16 lport, const __be32 faddr, 425 const __u16 lport, const __be32 faddr,
426 const __be16 fport) 426 const __be16 fport)
427{ 427{
428 static u32 udp_ehash_secret __read_mostly; 428 static u32 udp_ehash_secret __read_mostly;
429 429
@@ -433,7 +433,6 @@ static unsigned int udp_ehashfn(struct net *net, const __be32 laddr,
433 udp_ehash_secret + net_hash_mix(net)); 433 udp_ehash_secret + net_hash_mix(net));
434} 434}
435 435
436
437/* called with read_rcu_lock() */ 436/* called with read_rcu_lock() */
438static struct sock *udp4_lib_lookup2(struct net *net, 437static struct sock *udp4_lib_lookup2(struct net *net,
439 __be32 saddr, __be16 sport, 438 __be32 saddr, __be16 sport,
@@ -633,7 +632,7 @@ void __udp4_lib_err(struct sk_buff *skb, u32 info, struct udp_table *udptable)
633 632
634 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest, 633 sk = __udp4_lib_lookup(net, iph->daddr, uh->dest,
635 iph->saddr, uh->source, skb->dev->ifindex, udptable); 634 iph->saddr, uh->source, skb->dev->ifindex, udptable);
636 if (sk == NULL) { 635 if (!sk) {
637 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS); 636 ICMP_INC_STATS_BH(net, ICMP_MIB_INERRORS);
638 return; /* No socket for error */ 637 return; /* No socket for error */
639 } 638 }
@@ -873,8 +872,7 @@ out:
873} 872}
874EXPORT_SYMBOL(udp_push_pending_frames); 873EXPORT_SYMBOL(udp_push_pending_frames);
875 874
876int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 875int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
877 size_t len)
878{ 876{
879 struct inet_sock *inet = inet_sk(sk); 877 struct inet_sock *inet = inet_sk(sk);
880 struct udp_sock *up = udp_sk(sk); 878 struct udp_sock *up = udp_sk(sk);
@@ -1012,7 +1010,7 @@ int udp_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
1012 if (connected) 1010 if (connected)
1013 rt = (struct rtable *)sk_dst_check(sk, 0); 1011 rt = (struct rtable *)sk_dst_check(sk, 0);
1014 1012
1015 if (rt == NULL) { 1013 if (!rt) {
1016 struct net *net = sock_net(sk); 1014 struct net *net = sock_net(sk);
1017 1015
1018 fl4 = &fl4_stack; 1016 fl4 = &fl4_stack;
@@ -1136,7 +1134,7 @@ int udp_sendpage(struct sock *sk, struct page *page, int offset,
1136 * sendpage interface can't pass. 1134 * sendpage interface can't pass.
1137 * This will succeed only when the socket is connected. 1135 * This will succeed only when the socket is connected.
1138 */ 1136 */
1139 ret = udp_sendmsg(NULL, sk, &msg, 0); 1137 ret = udp_sendmsg(sk, &msg, 0);
1140 if (ret < 0) 1138 if (ret < 0)
1141 return ret; 1139 return ret;
1142 } 1140 }
@@ -1172,7 +1170,6 @@ out:
1172 return ret; 1170 return ret;
1173} 1171}
1174 1172
1175
1176/** 1173/**
1177 * first_packet_length - return length of first packet in receive queue 1174 * first_packet_length - return length of first packet in receive queue
1178 * @sk: socket 1175 * @sk: socket
@@ -1254,8 +1251,8 @@ EXPORT_SYMBOL(udp_ioctl);
1254 * return it, otherwise we block. 1251 * return it, otherwise we block.
1255 */ 1252 */
1256 1253
1257int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 1254int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
1258 size_t len, int noblock, int flags, int *addr_len) 1255 int flags, int *addr_len)
1259{ 1256{
1260 struct inet_sock *inet = inet_sk(sk); 1257 struct inet_sock *inet = inet_sk(sk);
1261 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); 1258 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
@@ -1356,7 +1353,6 @@ csum_copy_err:
1356 goto try_again; 1353 goto try_again;
1357} 1354}
1358 1355
1359
1360int udp_disconnect(struct sock *sk, int flags) 1356int udp_disconnect(struct sock *sk, int flags)
1361{ 1357{
1362 struct inet_sock *inet = inet_sk(sk); 1358 struct inet_sock *inet = inet_sk(sk);
@@ -1523,7 +1519,7 @@ int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1523 1519
1524 /* if we're overly short, let UDP handle it */ 1520 /* if we're overly short, let UDP handle it */
1525 encap_rcv = ACCESS_ONCE(up->encap_rcv); 1521 encap_rcv = ACCESS_ONCE(up->encap_rcv);
1526 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) { 1522 if (skb->len > sizeof(struct udphdr) && encap_rcv) {
1527 int ret; 1523 int ret;
1528 1524
1529 /* Verify checksum before giving to encap */ 1525 /* Verify checksum before giving to encap */
@@ -1580,7 +1576,6 @@ int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1580 udp_lib_checksum_complete(skb)) 1576 udp_lib_checksum_complete(skb))
1581 goto csum_error; 1577 goto csum_error;
1582 1578
1583
1584 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) { 1579 if (sk_rcvqueues_full(sk, sk->sk_rcvbuf)) {
1585 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS, 1580 UDP_INC_STATS_BH(sock_net(sk), UDP_MIB_RCVBUFERRORS,
1586 is_udplite); 1581 is_udplite);
@@ -1610,7 +1605,6 @@ drop:
1610 return -1; 1605 return -1;
1611} 1606}
1612 1607
1613
1614static void flush_stack(struct sock **stack, unsigned int count, 1608static void flush_stack(struct sock **stack, unsigned int count,
1615 struct sk_buff *skb, unsigned int final) 1609 struct sk_buff *skb, unsigned int final)
1616{ 1610{
@@ -1620,7 +1614,7 @@ static void flush_stack(struct sock **stack, unsigned int count,
1620 1614
1621 for (i = 0; i < count; i++) { 1615 for (i = 0; i < count; i++) {
1622 sk = stack[i]; 1616 sk = stack[i];
1623 if (likely(skb1 == NULL)) 1617 if (likely(!skb1))
1624 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC); 1618 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
1625 1619
1626 if (!skb1) { 1620 if (!skb1) {
@@ -1803,7 +1797,7 @@ int __udp4_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
1803 saddr, daddr, udptable, proto); 1797 saddr, daddr, udptable, proto);
1804 1798
1805 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 1799 sk = __udp4_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
1806 if (sk != NULL) { 1800 if (sk) {
1807 int ret; 1801 int ret;
1808 1802
1809 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk)) 1803 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
@@ -2525,6 +2519,16 @@ void __init udp_table_init(struct udp_table *table, const char *name)
2525 } 2519 }
2526} 2520}
2527 2521
2522u32 udp_flow_hashrnd(void)
2523{
2524 static u32 hashrnd __read_mostly;
2525
2526 net_get_random_once(&hashrnd, sizeof(hashrnd));
2527
2528 return hashrnd;
2529}
2530EXPORT_SYMBOL(udp_flow_hashrnd);
2531
2528void __init udp_init(void) 2532void __init udp_init(void)
2529{ 2533{
2530 unsigned long limit; 2534 unsigned long limit;
diff --git a/net/ipv4/udp_diag.c b/net/ipv4/udp_diag.c
index 4a000f1dd757..b763c39ae1d7 100644
--- a/net/ipv4/udp_diag.c
+++ b/net/ipv4/udp_diag.c
@@ -18,8 +18,9 @@
18#include <linux/sock_diag.h> 18#include <linux/sock_diag.h>
19 19
20static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, 20static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
21 struct netlink_callback *cb, struct inet_diag_req_v2 *req, 21 struct netlink_callback *cb,
22 struct nlattr *bc) 22 const struct inet_diag_req_v2 *req,
23 struct nlattr *bc)
23{ 24{
24 if (!inet_diag_bc_sk(bc, sk)) 25 if (!inet_diag_bc_sk(bc, sk))
25 return 0; 26 return 0;
@@ -31,7 +32,8 @@ static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
31} 32}
32 33
33static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb, 34static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
34 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req) 35 const struct nlmsghdr *nlh,
36 const struct inet_diag_req_v2 *req)
35{ 37{
36 int err = -EINVAL; 38 int err = -EINVAL;
37 struct sock *sk; 39 struct sock *sk;
@@ -56,7 +58,7 @@ static int udp_dump_one(struct udp_table *tbl, struct sk_buff *in_skb,
56 goto out_nosk; 58 goto out_nosk;
57 59
58 err = -ENOENT; 60 err = -ENOENT;
59 if (sk == NULL) 61 if (!sk)
60 goto out_nosk; 62 goto out_nosk;
61 63
62 err = sock_diag_check_cookie(sk, req->id.idiag_cookie); 64 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
@@ -90,8 +92,9 @@ out_nosk:
90 return err; 92 return err;
91} 93}
92 94
93static void udp_dump(struct udp_table *table, struct sk_buff *skb, struct netlink_callback *cb, 95static void udp_dump(struct udp_table *table, struct sk_buff *skb,
94 struct inet_diag_req_v2 *r, struct nlattr *bc) 96 struct netlink_callback *cb,
97 const struct inet_diag_req_v2 *r, struct nlattr *bc)
95{ 98{
96 int num, s_num, slot, s_slot; 99 int num, s_num, slot, s_slot;
97 struct net *net = sock_net(skb->sk); 100 struct net *net = sock_net(skb->sk);
@@ -144,13 +147,13 @@ done:
144} 147}
145 148
146static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 149static void udp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
147 struct inet_diag_req_v2 *r, struct nlattr *bc) 150 const struct inet_diag_req_v2 *r, struct nlattr *bc)
148{ 151{
149 udp_dump(&udp_table, skb, cb, r, bc); 152 udp_dump(&udp_table, skb, cb, r, bc);
150} 153}
151 154
152static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, 155static int udp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
153 struct inet_diag_req_v2 *req) 156 const struct inet_diag_req_v2 *req)
154{ 157{
155 return udp_dump_one(&udp_table, in_skb, nlh, req); 158 return udp_dump_one(&udp_table, in_skb, nlh, req);
156} 159}
@@ -170,13 +173,14 @@ static const struct inet_diag_handler udp_diag_handler = {
170}; 173};
171 174
172static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 175static void udplite_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
173 struct inet_diag_req_v2 *r, struct nlattr *bc) 176 const struct inet_diag_req_v2 *r,
177 struct nlattr *bc)
174{ 178{
175 udp_dump(&udplite_table, skb, cb, r, bc); 179 udp_dump(&udplite_table, skb, cb, r, bc);
176} 180}
177 181
178static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, 182static int udplite_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
179 struct inet_diag_req_v2 *req) 183 const struct inet_diag_req_v2 *req)
180{ 184{
181 return udp_dump_one(&udplite_table, in_skb, nlh, req); 185 return udp_dump_one(&udplite_table, in_skb, nlh, req);
182} 186}
diff --git a/net/ipv4/udp_impl.h b/net/ipv4/udp_impl.h
index f3c27899f62b..7e0fe4bdd967 100644
--- a/net/ipv4/udp_impl.h
+++ b/net/ipv4/udp_impl.h
@@ -21,8 +21,8 @@ int compat_udp_setsockopt(struct sock *sk, int level, int optname,
21int 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
24int udp_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 24int udp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
25 size_t len, int noblock, int flags, int *addr_len); 25 int flags, int *addr_len);
26int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size, 26int udp_sendpage(struct sock *sk, struct page *page, int offset, size_t size,
27 int flags); 27 int flags);
28int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 28int udp_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
diff --git a/net/ipv4/udp_offload.c b/net/ipv4/udp_offload.c
index 4915d8284a86..f9386160cbee 100644
--- a/net/ipv4/udp_offload.c
+++ b/net/ipv4/udp_offload.c
@@ -285,7 +285,7 @@ void udp_del_offload(struct udp_offload *uo)
285 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port)); 285 pr_warn("udp_del_offload: didn't find offload for port %d\n", ntohs(uo->port));
286unlock: 286unlock:
287 spin_unlock(&udp_offload_lock); 287 spin_unlock(&udp_offload_lock);
288 if (uo_priv != NULL) 288 if (uo_priv)
289 call_rcu(&uo_priv->rcu, udp_offload_free_routine); 289 call_rcu(&uo_priv->rcu, udp_offload_free_routine);
290} 290}
291EXPORT_SYMBOL(udp_del_offload); 291EXPORT_SYMBOL(udp_del_offload);
@@ -394,7 +394,7 @@ int udp_gro_complete(struct sk_buff *skb, int nhoff)
394 break; 394 break;
395 } 395 }
396 396
397 if (uo_priv != NULL) { 397 if (uo_priv) {
398 NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto; 398 NAPI_GRO_CB(skb)->proto = uo_priv->offload->ipproto;
399 err = uo_priv->offload->callbacks.gro_complete(skb, 399 err = uo_priv->offload->callbacks.gro_complete(skb,
400 nhoff + sizeof(struct udphdr), 400 nhoff + sizeof(struct udphdr),
diff --git a/net/ipv4/udp_tunnel.c b/net/ipv4/udp_tunnel.c
index c83b35485056..6bb98cc193c9 100644
--- a/net/ipv4/udp_tunnel.c
+++ b/net/ipv4/udp_tunnel.c
@@ -75,7 +75,7 @@ void setup_udp_tunnel_sock(struct net *net, struct socket *sock,
75} 75}
76EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock); 76EXPORT_SYMBOL_GPL(setup_udp_tunnel_sock);
77 77
78int udp_tunnel_xmit_skb(struct rtable *rt, struct sk_buff *skb, 78int udp_tunnel_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
79 __be32 src, __be32 dst, __u8 tos, __u8 ttl, 79 __be32 src, __be32 dst, __u8 tos, __u8 ttl,
80 __be16 df, __be16 src_port, __be16 dst_port, 80 __be16 df, __be16 src_port, __be16 dst_port,
81 bool xnet, bool nocheck) 81 bool xnet, bool nocheck)
@@ -92,7 +92,7 @@ int udp_tunnel_xmit_skb(struct rtable *rt, struct sk_buff *skb,
92 92
93 udp_set_csum(nocheck, skb, src, dst, skb->len); 93 udp_set_csum(nocheck, skb, src, dst, skb->len);
94 94
95 return iptunnel_xmit(skb->sk, rt, skb, src, dst, IPPROTO_UDP, 95 return iptunnel_xmit(sk, rt, skb, src, dst, IPPROTO_UDP,
96 tos, ttl, df, xnet); 96 tos, ttl, df, xnet);
97} 97}
98EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb); 98EXPORT_SYMBOL_GPL(udp_tunnel_xmit_skb);
diff --git a/net/ipv4/xfrm4_input.c b/net/ipv4/xfrm4_input.c
index aac6197b7a71..60b032f58ccc 100644
--- a/net/ipv4/xfrm4_input.c
+++ b/net/ipv4/xfrm4_input.c
@@ -22,9 +22,9 @@ int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb)
22 return xfrm4_extract_header(skb); 22 return xfrm4_extract_header(skb);
23} 23}
24 24
25static inline int xfrm4_rcv_encap_finish(struct sk_buff *skb) 25static inline int xfrm4_rcv_encap_finish(struct sock *sk, struct sk_buff *skb)
26{ 26{
27 if (skb_dst(skb) == NULL) { 27 if (!skb_dst(skb)) {
28 const struct iphdr *iph = ip_hdr(skb); 28 const struct iphdr *iph = ip_hdr(skb);
29 29
30 if (ip_route_input_noref(skb, iph->daddr, iph->saddr, 30 if (ip_route_input_noref(skb, iph->daddr, iph->saddr,
@@ -52,7 +52,8 @@ int xfrm4_transport_finish(struct sk_buff *skb, int async)
52 iph->tot_len = htons(skb->len); 52 iph->tot_len = htons(skb->len);
53 ip_send_check(iph); 53 ip_send_check(iph);
54 54
55 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, skb->dev, NULL, 55 NF_HOOK(NFPROTO_IPV4, NF_INET_PRE_ROUTING, NULL, skb,
56 skb->dev, NULL,
56 xfrm4_rcv_encap_finish); 57 xfrm4_rcv_encap_finish);
57 return 0; 58 return 0;
58} 59}
diff --git a/net/ipv4/xfrm4_mode_tunnel.c b/net/ipv4/xfrm4_mode_tunnel.c
index 91771a7c802f..35feda676464 100644
--- a/net/ipv4/xfrm4_mode_tunnel.c
+++ b/net/ipv4/xfrm4_mode_tunnel.c
@@ -63,7 +63,7 @@ static int xfrm4_mode_tunnel_output(struct xfrm_state *x, struct sk_buff *skb)
63 63
64 top_iph->saddr = x->props.saddr.a4; 64 top_iph->saddr = x->props.saddr.a4;
65 top_iph->daddr = x->id.daddr.a4; 65 top_iph->daddr = x->id.daddr.a4;
66 ip_select_ident(skb, NULL); 66 ip_select_ident(dev_net(dst->dev), skb, NULL);
67 67
68 return 0; 68 return 0;
69} 69}
diff --git a/net/ipv4/xfrm4_output.c b/net/ipv4/xfrm4_output.c
index dab73813cb92..2878dbfffeb7 100644
--- a/net/ipv4/xfrm4_output.c
+++ b/net/ipv4/xfrm4_output.c
@@ -69,7 +69,7 @@ int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
69} 69}
70EXPORT_SYMBOL(xfrm4_prepare_output); 70EXPORT_SYMBOL(xfrm4_prepare_output);
71 71
72int xfrm4_output_finish(struct sk_buff *skb) 72int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb)
73{ 73{
74 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 74 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
75 75
@@ -77,26 +77,26 @@ int xfrm4_output_finish(struct sk_buff *skb)
77 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED; 77 IPCB(skb)->flags |= IPSKB_XFRM_TRANSFORMED;
78#endif 78#endif
79 79
80 return xfrm_output(skb); 80 return xfrm_output(sk, skb);
81} 81}
82 82
83static int __xfrm4_output(struct sk_buff *skb) 83static int __xfrm4_output(struct sock *sk, struct sk_buff *skb)
84{ 84{
85 struct xfrm_state *x = skb_dst(skb)->xfrm; 85 struct xfrm_state *x = skb_dst(skb)->xfrm;
86 86
87#ifdef CONFIG_NETFILTER 87#ifdef CONFIG_NETFILTER
88 if (!x) { 88 if (!x) {
89 IPCB(skb)->flags |= IPSKB_REROUTED; 89 IPCB(skb)->flags |= IPSKB_REROUTED;
90 return dst_output(skb); 90 return dst_output_sk(sk, skb);
91 } 91 }
92#endif 92#endif
93 93
94 return x->outer_mode->afinfo->output_finish(skb); 94 return x->outer_mode->afinfo->output_finish(sk, skb);
95} 95}
96 96
97int xfrm4_output(struct sock *sk, struct sk_buff *skb) 97int xfrm4_output(struct sock *sk, struct sk_buff *skb)
98{ 98{
99 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, skb, 99 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
100 NULL, skb_dst(skb)->dev, __xfrm4_output, 100 NULL, skb_dst(skb)->dev, __xfrm4_output,
101 !(IPCB(skb)->flags & IPSKB_REROUTED)); 101 !(IPCB(skb)->flags & IPSKB_REROUTED));
102} 102}
diff --git a/net/ipv4/xfrm4_policy.c b/net/ipv4/xfrm4_policy.c
index 6156f68a1e90..bff69746e05f 100644
--- a/net/ipv4/xfrm4_policy.c
+++ b/net/ipv4/xfrm4_policy.c
@@ -232,7 +232,6 @@ static void xfrm4_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
232 232
233static struct dst_ops xfrm4_dst_ops = { 233static struct dst_ops xfrm4_dst_ops = {
234 .family = AF_INET, 234 .family = AF_INET,
235 .protocol = cpu_to_be16(ETH_P_IP),
236 .gc = xfrm4_garbage_collect, 235 .gc = xfrm4_garbage_collect,
237 .update_pmtu = xfrm4_update_pmtu, 236 .update_pmtu = xfrm4_update_pmtu,
238 .redirect = xfrm4_redirect, 237 .redirect = xfrm4_redirect,
@@ -299,7 +298,7 @@ static void __net_exit xfrm4_net_exit(struct net *net)
299{ 298{
300 struct ctl_table *table; 299 struct ctl_table *table;
301 300
302 if (net->ipv4.xfrm4_hdr == NULL) 301 if (!net->ipv4.xfrm4_hdr)
303 return; 302 return;
304 303
305 table = net->ipv4.xfrm4_hdr->ctl_table_arg; 304 table = net->ipv4.xfrm4_hdr->ctl_table_arg;
diff --git a/net/ipv6/addrconf.c b/net/ipv6/addrconf.c
index b6030025f411..37b70e82bff8 100644
--- a/net/ipv6/addrconf.c
+++ b/net/ipv6/addrconf.c
@@ -46,6 +46,7 @@
46#include <linux/socket.h> 46#include <linux/socket.h>
47#include <linux/sockios.h> 47#include <linux/sockios.h>
48#include <linux/net.h> 48#include <linux/net.h>
49#include <linux/inet.h>
49#include <linux/in6.h> 50#include <linux/in6.h>
50#include <linux/netdevice.h> 51#include <linux/netdevice.h>
51#include <linux/if_addr.h> 52#include <linux/if_addr.h>
@@ -102,6 +103,9 @@
102 103
103#define INFINITY_LIFE_TIME 0xFFFFFFFF 104#define INFINITY_LIFE_TIME 0xFFFFFFFF
104 105
106#define IPV6_MAX_STRLEN \
107 sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")
108
105static inline u32 cstamp_delta(unsigned long cstamp) 109static inline u32 cstamp_delta(unsigned long cstamp)
106{ 110{
107 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ; 111 return (cstamp - INITIAL_JIFFIES) * 100UL / HZ;
@@ -127,6 +131,9 @@ static void ipv6_regen_rndid(unsigned long data);
127 131
128static int ipv6_generate_eui64(u8 *eui, struct net_device *dev); 132static int ipv6_generate_eui64(u8 *eui, struct net_device *dev);
129static int ipv6_count_addresses(struct inet6_dev *idev); 133static int ipv6_count_addresses(struct inet6_dev *idev);
134static int ipv6_generate_stable_address(struct in6_addr *addr,
135 u8 dad_count,
136 const struct inet6_dev *idev);
130 137
131/* 138/*
132 * Configured unicast address hash table 139 * Configured unicast address hash table
@@ -202,6 +209,9 @@ static struct ipv6_devconf ipv6_devconf __read_mostly = {
202 .accept_dad = 1, 209 .accept_dad = 1,
203 .suppress_frag_ndisc = 1, 210 .suppress_frag_ndisc = 1,
204 .accept_ra_mtu = 1, 211 .accept_ra_mtu = 1,
212 .stable_secret = {
213 .initialized = false,
214 }
205}; 215};
206 216
207static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = { 217static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
@@ -240,6 +250,9 @@ static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
240 .accept_dad = 1, 250 .accept_dad = 1,
241 .suppress_frag_ndisc = 1, 251 .suppress_frag_ndisc = 1,
242 .accept_ra_mtu = 1, 252 .accept_ra_mtu = 1,
253 .stable_secret = {
254 .initialized = false,
255 },
243}; 256};
244 257
245/* Check if a valid qdisc is available */ 258/* Check if a valid qdisc is available */
@@ -321,7 +334,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
321 return ERR_PTR(-EINVAL); 334 return ERR_PTR(-EINVAL);
322 335
323 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL); 336 ndev = kzalloc(sizeof(struct inet6_dev), GFP_KERNEL);
324 if (ndev == NULL) 337 if (!ndev)
325 return ERR_PTR(err); 338 return ERR_PTR(err);
326 339
327 rwlock_init(&ndev->lock); 340 rwlock_init(&ndev->lock);
@@ -333,7 +346,7 @@ static struct inet6_dev *ipv6_add_dev(struct net_device *dev)
333 ndev->cnf.mtu6 = dev->mtu; 346 ndev->cnf.mtu6 = dev->mtu;
334 ndev->cnf.sysctl = NULL; 347 ndev->cnf.sysctl = NULL;
335 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); 348 ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
336 if (ndev->nd_parms == NULL) { 349 if (!ndev->nd_parms) {
337 kfree(ndev); 350 kfree(ndev);
338 return ERR_PTR(err); 351 return ERR_PTR(err);
339 } 352 }
@@ -468,7 +481,7 @@ static int inet6_netconf_fill_devconf(struct sk_buff *skb, int ifindex,
468 481
469 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg), 482 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct netconfmsg),
470 flags); 483 flags);
471 if (nlh == NULL) 484 if (!nlh)
472 return -EMSGSIZE; 485 return -EMSGSIZE;
473 486
474 ncm = nlmsg_data(nlh); 487 ncm = nlmsg_data(nlh);
@@ -506,7 +519,7 @@ void inet6_netconf_notify_devconf(struct net *net, int type, int ifindex,
506 int err = -ENOBUFS; 519 int err = -ENOBUFS;
507 520
508 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC); 521 skb = nlmsg_new(inet6_netconf_msgsize_devconf(type), GFP_ATOMIC);
509 if (skb == NULL) 522 if (!skb)
510 goto errout; 523 goto errout;
511 524
512 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0, 525 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 0, 0,
@@ -561,10 +574,10 @@ static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
561 break; 574 break;
562 default: 575 default:
563 dev = __dev_get_by_index(net, ifindex); 576 dev = __dev_get_by_index(net, ifindex);
564 if (dev == NULL) 577 if (!dev)
565 goto errout; 578 goto errout;
566 in6_dev = __in6_dev_get(dev); 579 in6_dev = __in6_dev_get(dev);
567 if (in6_dev == NULL) 580 if (!in6_dev)
568 goto errout; 581 goto errout;
569 devconf = &in6_dev->cnf; 582 devconf = &in6_dev->cnf;
570 break; 583 break;
@@ -572,7 +585,7 @@ static int inet6_netconf_get_devconf(struct sk_buff *in_skb,
572 585
573 err = -ENOBUFS; 586 err = -ENOBUFS;
574 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC); 587 skb = nlmsg_new(inet6_netconf_msgsize_devconf(-1), GFP_ATOMIC);
575 if (skb == NULL) 588 if (!skb)
576 goto errout; 589 goto errout;
577 590
578 err = inet6_netconf_fill_devconf(skb, ifindex, devconf, 591 err = inet6_netconf_fill_devconf(skb, ifindex, devconf,
@@ -841,7 +854,7 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
841 854
842 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); 855 ifa = kzalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
843 856
844 if (ifa == NULL) { 857 if (!ifa) {
845 ADBG("ipv6_add_addr: malloc failed\n"); 858 ADBG("ipv6_add_addr: malloc failed\n");
846 err = -ENOBUFS; 859 err = -ENOBUFS;
847 goto out; 860 goto out;
@@ -860,7 +873,6 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr,
860 ifa->peer_addr = *peer_addr; 873 ifa->peer_addr = *peer_addr;
861 874
862 spin_lock_init(&ifa->lock); 875 spin_lock_init(&ifa->lock);
863 spin_lock_init(&ifa->state_lock);
864 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work); 876 INIT_DELAYED_WORK(&ifa->dad_work, addrconf_dad_work);
865 INIT_HLIST_NODE(&ifa->addr_lst); 877 INIT_HLIST_NODE(&ifa->addr_lst);
866 ifa->scope = scope; 878 ifa->scope = scope;
@@ -1003,10 +1015,10 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
1003 1015
1004 ASSERT_RTNL(); 1016 ASSERT_RTNL();
1005 1017
1006 spin_lock_bh(&ifp->state_lock); 1018 spin_lock_bh(&ifp->lock);
1007 state = ifp->state; 1019 state = ifp->state;
1008 ifp->state = INET6_IFADDR_STATE_DEAD; 1020 ifp->state = INET6_IFADDR_STATE_DEAD;
1009 spin_unlock_bh(&ifp->state_lock); 1021 spin_unlock_bh(&ifp->lock);
1010 1022
1011 if (state == INET6_IFADDR_STATE_DEAD) 1023 if (state == INET6_IFADDR_STATE_DEAD)
1012 goto out; 1024 goto out;
@@ -1546,7 +1558,7 @@ int ipv6_chk_addr_and_flags(struct net *net, const struct in6_addr *addr,
1546 : ifp->flags; 1558 : ifp->flags;
1547 if (ipv6_addr_equal(&ifp->addr, addr) && 1559 if (ipv6_addr_equal(&ifp->addr, addr) &&
1548 !(ifp_flags&banned_flags) && 1560 !(ifp_flags&banned_flags) &&
1549 (dev == NULL || ifp->idev->dev == dev || 1561 (!dev || ifp->idev->dev == dev ||
1550 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) { 1562 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))) {
1551 rcu_read_unlock_bh(); 1563 rcu_read_unlock_bh();
1552 return 1; 1564 return 1;
@@ -1568,7 +1580,7 @@ static bool ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
1568 if (!net_eq(dev_net(ifp->idev->dev), net)) 1580 if (!net_eq(dev_net(ifp->idev->dev), net))
1569 continue; 1581 continue;
1570 if (ipv6_addr_equal(&ifp->addr, addr)) { 1582 if (ipv6_addr_equal(&ifp->addr, addr)) {
1571 if (dev == NULL || ifp->idev->dev == dev) 1583 if (!dev || ifp->idev->dev == dev)
1572 return true; 1584 return true;
1573 } 1585 }
1574 } 1586 }
@@ -1637,7 +1649,7 @@ struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, const struct in6_addr *add
1637 if (!net_eq(dev_net(ifp->idev->dev), net)) 1649 if (!net_eq(dev_net(ifp->idev->dev), net))
1638 continue; 1650 continue;
1639 if (ipv6_addr_equal(&ifp->addr, addr)) { 1651 if (ipv6_addr_equal(&ifp->addr, addr)) {
1640 if (dev == NULL || ifp->idev->dev == dev || 1652 if (!dev || ifp->idev->dev == dev ||
1641 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) { 1653 !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
1642 result = ifp; 1654 result = ifp;
1643 in6_ifa_hold(ifp); 1655 in6_ifa_hold(ifp);
@@ -1686,19 +1698,21 @@ static int addrconf_dad_end(struct inet6_ifaddr *ifp)
1686{ 1698{
1687 int err = -ENOENT; 1699 int err = -ENOENT;
1688 1700
1689 spin_lock_bh(&ifp->state_lock); 1701 spin_lock_bh(&ifp->lock);
1690 if (ifp->state == INET6_IFADDR_STATE_DAD) { 1702 if (ifp->state == INET6_IFADDR_STATE_DAD) {
1691 ifp->state = INET6_IFADDR_STATE_POSTDAD; 1703 ifp->state = INET6_IFADDR_STATE_POSTDAD;
1692 err = 0; 1704 err = 0;
1693 } 1705 }
1694 spin_unlock_bh(&ifp->state_lock); 1706 spin_unlock_bh(&ifp->lock);
1695 1707
1696 return err; 1708 return err;
1697} 1709}
1698 1710
1699void addrconf_dad_failure(struct inet6_ifaddr *ifp) 1711void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1700{ 1712{
1713 struct in6_addr addr;
1701 struct inet6_dev *idev = ifp->idev; 1714 struct inet6_dev *idev = ifp->idev;
1715 struct net *net = dev_net(ifp->idev->dev);
1702 1716
1703 if (addrconf_dad_end(ifp)) { 1717 if (addrconf_dad_end(ifp)) {
1704 in6_ifa_put(ifp); 1718 in6_ifa_put(ifp);
@@ -1708,9 +1722,57 @@ void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1708 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n", 1722 net_info_ratelimited("%s: IPv6 duplicate address %pI6c detected!\n",
1709 ifp->idev->dev->name, &ifp->addr); 1723 ifp->idev->dev->name, &ifp->addr);
1710 1724
1711 if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) { 1725 spin_lock_bh(&ifp->lock);
1712 struct in6_addr addr; 1726
1727 if (ifp->flags & IFA_F_STABLE_PRIVACY) {
1728 int scope = ifp->scope;
1729 u32 flags = ifp->flags;
1730 struct in6_addr new_addr;
1731 struct inet6_ifaddr *ifp2;
1732 u32 valid_lft, preferred_lft;
1733 int pfxlen = ifp->prefix_len;
1734 int retries = ifp->stable_privacy_retry + 1;
1735
1736 if (retries > net->ipv6.sysctl.idgen_retries) {
1737 net_info_ratelimited("%s: privacy stable address generation failed because of DAD conflicts!\n",
1738 ifp->idev->dev->name);
1739 goto errdad;
1740 }
1741
1742 new_addr = ifp->addr;
1743 if (ipv6_generate_stable_address(&new_addr, retries,
1744 idev))
1745 goto errdad;
1746
1747 valid_lft = ifp->valid_lft;
1748 preferred_lft = ifp->prefered_lft;
1749
1750 spin_unlock_bh(&ifp->lock);
1751
1752 if (idev->cnf.max_addresses &&
1753 ipv6_count_addresses(idev) >=
1754 idev->cnf.max_addresses)
1755 goto lock_errdad;
1756
1757 net_info_ratelimited("%s: generating new stable privacy address because of DAD conflict\n",
1758 ifp->idev->dev->name);
1759
1760 ifp2 = ipv6_add_addr(idev, &new_addr, NULL, pfxlen,
1761 scope, flags, valid_lft,
1762 preferred_lft);
1763 if (IS_ERR(ifp2))
1764 goto lock_errdad;
1765
1766 spin_lock_bh(&ifp2->lock);
1767 ifp2->stable_privacy_retry = retries;
1768 ifp2->state = INET6_IFADDR_STATE_PREDAD;
1769 spin_unlock_bh(&ifp2->lock);
1713 1770
1771 addrconf_mod_dad_work(ifp2, net->ipv6.sysctl.idgen_delay);
1772 in6_ifa_put(ifp2);
1773lock_errdad:
1774 spin_lock_bh(&ifp->lock);
1775 } else if (idev->cnf.accept_dad > 1 && !idev->cnf.disable_ipv6) {
1714 addr.s6_addr32[0] = htonl(0xfe800000); 1776 addr.s6_addr32[0] = htonl(0xfe800000);
1715 addr.s6_addr32[1] = 0; 1777 addr.s6_addr32[1] = 0;
1716 1778
@@ -1724,10 +1786,10 @@ void addrconf_dad_failure(struct inet6_ifaddr *ifp)
1724 } 1786 }
1725 } 1787 }
1726 1788
1727 spin_lock_bh(&ifp->state_lock); 1789errdad:
1728 /* transition from _POSTDAD to _ERRDAD */ 1790 /* transition from _POSTDAD to _ERRDAD */
1729 ifp->state = INET6_IFADDR_STATE_ERRDAD; 1791 ifp->state = INET6_IFADDR_STATE_ERRDAD;
1730 spin_unlock_bh(&ifp->state_lock); 1792 spin_unlock_bh(&ifp->lock);
1731 1793
1732 addrconf_mod_dad_work(ifp, 0); 1794 addrconf_mod_dad_work(ifp, 0);
1733} 1795}
@@ -2052,7 +2114,7 @@ static struct rt6_info *addrconf_get_prefix_route(const struct in6_addr *pfx,
2052 struct fib6_table *table; 2114 struct fib6_table *table;
2053 2115
2054 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX); 2116 table = fib6_get_table(dev_net(dev), RT6_TABLE_PREFIX);
2055 if (table == NULL) 2117 if (!table)
2056 return NULL; 2118 return NULL;
2057 2119
2058 read_lock_bh(&table->tb6_lock); 2120 read_lock_bh(&table->tb6_lock);
@@ -2186,6 +2248,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2186 __u32 valid_lft; 2248 __u32 valid_lft;
2187 __u32 prefered_lft; 2249 __u32 prefered_lft;
2188 int addr_type; 2250 int addr_type;
2251 u32 addr_flags = 0;
2189 struct inet6_dev *in6_dev; 2252 struct inet6_dev *in6_dev;
2190 struct net *net = dev_net(dev); 2253 struct net *net = dev_net(dev);
2191 2254
@@ -2215,7 +2278,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2215 2278
2216 in6_dev = in6_dev_get(dev); 2279 in6_dev = in6_dev_get(dev);
2217 2280
2218 if (in6_dev == NULL) { 2281 if (!in6_dev) {
2219 net_dbg_ratelimited("addrconf: device %s not configured\n", 2282 net_dbg_ratelimited("addrconf: device %s not configured\n",
2220 dev->name); 2283 dev->name);
2221 return; 2284 return;
@@ -2292,6 +2355,12 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len, bool sllao)
2292 in6_dev->token.s6_addr + 8, 8); 2355 in6_dev->token.s6_addr + 8, 8);
2293 read_unlock_bh(&in6_dev->lock); 2356 read_unlock_bh(&in6_dev->lock);
2294 tokenized = true; 2357 tokenized = true;
2358 } else if (in6_dev->addr_gen_mode ==
2359 IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
2360 !ipv6_generate_stable_address(&addr, 0,
2361 in6_dev)) {
2362 addr_flags |= IFA_F_STABLE_PRIVACY;
2363 goto ok;
2295 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) && 2364 } else if (ipv6_generate_eui64(addr.s6_addr + 8, dev) &&
2296 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) { 2365 ipv6_inherit_eui64(addr.s6_addr + 8, in6_dev)) {
2297 in6_dev_put(in6_dev); 2366 in6_dev_put(in6_dev);
@@ -2308,9 +2377,8 @@ ok:
2308 2377
2309 ifp = ipv6_get_ifaddr(net, &addr, dev, 1); 2378 ifp = ipv6_get_ifaddr(net, &addr, dev, 1);
2310 2379
2311 if (ifp == NULL && valid_lft) { 2380 if (!ifp && valid_lft) {
2312 int max_addresses = in6_dev->cnf.max_addresses; 2381 int max_addresses = in6_dev->cnf.max_addresses;
2313 u32 addr_flags = 0;
2314 2382
2315#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2383#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2316 if (in6_dev->cnf.optimistic_dad && 2384 if (in6_dev->cnf.optimistic_dad &&
@@ -2350,7 +2418,7 @@ ok:
2350 u32 stored_lft; 2418 u32 stored_lft;
2351 2419
2352 /* update lifetime (RFC2462 5.5.3 e) */ 2420 /* update lifetime (RFC2462 5.5.3 e) */
2353 spin_lock(&ifp->lock); 2421 spin_lock_bh(&ifp->lock);
2354 now = jiffies; 2422 now = jiffies;
2355 if (ifp->valid_lft > (now - ifp->tstamp) / HZ) 2423 if (ifp->valid_lft > (now - ifp->tstamp) / HZ)
2356 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ; 2424 stored_lft = ifp->valid_lft - (now - ifp->tstamp) / HZ;
@@ -2380,12 +2448,12 @@ ok:
2380 ifp->tstamp = now; 2448 ifp->tstamp = now;
2381 flags = ifp->flags; 2449 flags = ifp->flags;
2382 ifp->flags &= ~IFA_F_DEPRECATED; 2450 ifp->flags &= ~IFA_F_DEPRECATED;
2383 spin_unlock(&ifp->lock); 2451 spin_unlock_bh(&ifp->lock);
2384 2452
2385 if (!(flags&IFA_F_TENTATIVE)) 2453 if (!(flags&IFA_F_TENTATIVE))
2386 ipv6_ifa_notify(0, ifp); 2454 ipv6_ifa_notify(0, ifp);
2387 } else 2455 } else
2388 spin_unlock(&ifp->lock); 2456 spin_unlock_bh(&ifp->lock);
2389 2457
2390 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft, 2458 manage_tempaddrs(in6_dev, ifp, valid_lft, prefered_lft,
2391 create, now); 2459 create, now);
@@ -2418,7 +2486,7 @@ int addrconf_set_dstaddr(struct net *net, void __user *arg)
2418 dev = __dev_get_by_index(net, ireq.ifr6_ifindex); 2486 dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
2419 2487
2420 err = -ENODEV; 2488 err = -ENODEV;
2421 if (dev == NULL) 2489 if (!dev)
2422 goto err_exit; 2490 goto err_exit;
2423 2491
2424#if IS_ENABLED(CONFIG_IPV6_SIT) 2492#if IS_ENABLED(CONFIG_IPV6_SIT)
@@ -2464,6 +2532,23 @@ err_exit:
2464 return err; 2532 return err;
2465} 2533}
2466 2534
2535static int ipv6_mc_config(struct sock *sk, bool join,
2536 const struct in6_addr *addr, int ifindex)
2537{
2538 int ret;
2539
2540 ASSERT_RTNL();
2541
2542 lock_sock(sk);
2543 if (join)
2544 ret = ipv6_sock_mc_join(sk, ifindex, addr);
2545 else
2546 ret = ipv6_sock_mc_drop(sk, ifindex, addr);
2547 release_sock(sk);
2548
2549 return ret;
2550}
2551
2467/* 2552/*
2468 * Manual configuration of address on an interface 2553 * Manual configuration of address on an interface
2469 */ 2554 */
@@ -2476,10 +2561,10 @@ static int inet6_addr_add(struct net *net, int ifindex,
2476 struct inet6_ifaddr *ifp; 2561 struct inet6_ifaddr *ifp;
2477 struct inet6_dev *idev; 2562 struct inet6_dev *idev;
2478 struct net_device *dev; 2563 struct net_device *dev;
2564 unsigned long timeout;
2565 clock_t expires;
2479 int scope; 2566 int scope;
2480 u32 flags; 2567 u32 flags;
2481 clock_t expires;
2482 unsigned long timeout;
2483 2568
2484 ASSERT_RTNL(); 2569 ASSERT_RTNL();
2485 2570
@@ -2501,6 +2586,14 @@ static int inet6_addr_add(struct net *net, int ifindex,
2501 if (IS_ERR(idev)) 2586 if (IS_ERR(idev))
2502 return PTR_ERR(idev); 2587 return PTR_ERR(idev);
2503 2588
2589 if (ifa_flags & IFA_F_MCAUTOJOIN) {
2590 int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2591 true, pfx, ifindex);
2592
2593 if (ret < 0)
2594 return ret;
2595 }
2596
2504 scope = ipv6_addr_scope(pfx); 2597 scope = ipv6_addr_scope(pfx);
2505 2598
2506 timeout = addrconf_timeout_fixup(valid_lft, HZ); 2599 timeout = addrconf_timeout_fixup(valid_lft, HZ);
@@ -2542,6 +2635,9 @@ static int inet6_addr_add(struct net *net, int ifindex,
2542 in6_ifa_put(ifp); 2635 in6_ifa_put(ifp);
2543 addrconf_verify_rtnl(); 2636 addrconf_verify_rtnl();
2544 return 0; 2637 return 0;
2638 } else if (ifa_flags & IFA_F_MCAUTOJOIN) {
2639 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2640 false, pfx, ifindex);
2545 } 2641 }
2546 2642
2547 return PTR_ERR(ifp); 2643 return PTR_ERR(ifp);
@@ -2562,7 +2658,7 @@ static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2562 return -ENODEV; 2658 return -ENODEV;
2563 2659
2564 idev = __in6_dev_get(dev); 2660 idev = __in6_dev_get(dev);
2565 if (idev == NULL) 2661 if (!idev)
2566 return -ENXIO; 2662 return -ENXIO;
2567 2663
2568 read_lock_bh(&idev->lock); 2664 read_lock_bh(&idev->lock);
@@ -2578,6 +2674,10 @@ static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
2578 jiffies); 2674 jiffies);
2579 ipv6_del_addr(ifp); 2675 ipv6_del_addr(ifp);
2580 addrconf_verify_rtnl(); 2676 addrconf_verify_rtnl();
2677 if (ipv6_addr_is_multicast(pfx)) {
2678 ipv6_mc_config(net->ipv6.mc_autojoin_sk,
2679 false, pfx, dev->ifindex);
2680 }
2581 return 0; 2681 return 0;
2582 } 2682 }
2583 } 2683 }
@@ -2710,7 +2810,7 @@ static void init_loopback(struct net_device *dev)
2710 ASSERT_RTNL(); 2810 ASSERT_RTNL();
2711 2811
2712 idev = ipv6_find_idev(dev); 2812 idev = ipv6_find_idev(dev);
2713 if (idev == NULL) { 2813 if (!idev) {
2714 pr_debug("%s: add_dev failed\n", __func__); 2814 pr_debug("%s: add_dev failed\n", __func__);
2715 return; 2815 return;
2716 } 2816 }
@@ -2757,10 +2857,11 @@ static void init_loopback(struct net_device *dev)
2757 } 2857 }
2758} 2858}
2759 2859
2760static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr *addr) 2860static void addrconf_add_linklocal(struct inet6_dev *idev,
2861 const struct in6_addr *addr, u32 flags)
2761{ 2862{
2762 struct inet6_ifaddr *ifp; 2863 struct inet6_ifaddr *ifp;
2763 u32 addr_flags = IFA_F_PERMANENT; 2864 u32 addr_flags = flags | IFA_F_PERMANENT;
2764 2865
2765#ifdef CONFIG_IPV6_OPTIMISTIC_DAD 2866#ifdef CONFIG_IPV6_OPTIMISTIC_DAD
2766 if (idev->cnf.optimistic_dad && 2867 if (idev->cnf.optimistic_dad &&
@@ -2768,7 +2869,6 @@ static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr
2768 addr_flags |= IFA_F_OPTIMISTIC; 2869 addr_flags |= IFA_F_OPTIMISTIC;
2769#endif 2870#endif
2770 2871
2771
2772 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags, 2872 ifp = ipv6_add_addr(idev, addr, NULL, 64, IFA_LINK, addr_flags,
2773 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME); 2873 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
2774 if (!IS_ERR(ifp)) { 2874 if (!IS_ERR(ifp)) {
@@ -2778,18 +2878,103 @@ static void addrconf_add_linklocal(struct inet6_dev *idev, const struct in6_addr
2778 } 2878 }
2779} 2879}
2780 2880
2881static bool ipv6_reserved_interfaceid(struct in6_addr address)
2882{
2883 if ((address.s6_addr32[2] | address.s6_addr32[3]) == 0)
2884 return true;
2885
2886 if (address.s6_addr32[2] == htonl(0x02005eff) &&
2887 ((address.s6_addr32[3] & htonl(0xfe000000)) == htonl(0xfe000000)))
2888 return true;
2889
2890 if (address.s6_addr32[2] == htonl(0xfdffffff) &&
2891 ((address.s6_addr32[3] & htonl(0xffffff80)) == htonl(0xffffff80)))
2892 return true;
2893
2894 return false;
2895}
2896
2897static int ipv6_generate_stable_address(struct in6_addr *address,
2898 u8 dad_count,
2899 const struct inet6_dev *idev)
2900{
2901 static DEFINE_SPINLOCK(lock);
2902 static __u32 digest[SHA_DIGEST_WORDS];
2903 static __u32 workspace[SHA_WORKSPACE_WORDS];
2904
2905 static union {
2906 char __data[SHA_MESSAGE_BYTES];
2907 struct {
2908 struct in6_addr secret;
2909 __be32 prefix[2];
2910 unsigned char hwaddr[MAX_ADDR_LEN];
2911 u8 dad_count;
2912 } __packed;
2913 } data;
2914
2915 struct in6_addr secret;
2916 struct in6_addr temp;
2917 struct net *net = dev_net(idev->dev);
2918
2919 BUILD_BUG_ON(sizeof(data.__data) != sizeof(data));
2920
2921 if (idev->cnf.stable_secret.initialized)
2922 secret = idev->cnf.stable_secret.secret;
2923 else if (net->ipv6.devconf_dflt->stable_secret.initialized)
2924 secret = net->ipv6.devconf_dflt->stable_secret.secret;
2925 else
2926 return -1;
2927
2928retry:
2929 spin_lock_bh(&lock);
2930
2931 sha_init(digest);
2932 memset(&data, 0, sizeof(data));
2933 memset(workspace, 0, sizeof(workspace));
2934 memcpy(data.hwaddr, idev->dev->perm_addr, idev->dev->addr_len);
2935 data.prefix[0] = address->s6_addr32[0];
2936 data.prefix[1] = address->s6_addr32[1];
2937 data.secret = secret;
2938 data.dad_count = dad_count;
2939
2940 sha_transform(digest, data.__data, workspace);
2941
2942 temp = *address;
2943 temp.s6_addr32[2] = (__force __be32)digest[0];
2944 temp.s6_addr32[3] = (__force __be32)digest[1];
2945
2946 spin_unlock_bh(&lock);
2947
2948 if (ipv6_reserved_interfaceid(temp)) {
2949 dad_count++;
2950 if (dad_count > dev_net(idev->dev)->ipv6.sysctl.idgen_retries)
2951 return -1;
2952 goto retry;
2953 }
2954
2955 *address = temp;
2956 return 0;
2957}
2958
2781static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route) 2959static void addrconf_addr_gen(struct inet6_dev *idev, bool prefix_route)
2782{ 2960{
2783 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) { 2961 struct in6_addr addr;
2784 struct in6_addr addr; 2962
2963 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0);
2785 2964
2786 ipv6_addr_set(&addr, htonl(0xFE800000), 0, 0, 0); 2965 if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY) {
2966 if (!ipv6_generate_stable_address(&addr, 0, idev))
2967 addrconf_add_linklocal(idev, &addr,
2968 IFA_F_STABLE_PRIVACY);
2969 else if (prefix_route)
2970 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2971 } else if (idev->addr_gen_mode == IN6_ADDR_GEN_MODE_EUI64) {
2787 /* addrconf_add_linklocal also adds a prefix_route and we 2972 /* addrconf_add_linklocal also adds a prefix_route and we
2788 * only need to care about prefix routes if ipv6_generate_eui64 2973 * only need to care about prefix routes if ipv6_generate_eui64
2789 * couldn't generate one. 2974 * couldn't generate one.
2790 */ 2975 */
2791 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0) 2976 if (ipv6_generate_eui64(addr.s6_addr + 8, idev->dev) == 0)
2792 addrconf_add_linklocal(idev, &addr); 2977 addrconf_add_linklocal(idev, &addr, 0);
2793 else if (prefix_route) 2978 else if (prefix_route)
2794 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0); 2979 addrconf_prefix_route(&addr, 64, idev->dev, 0, 0);
2795 } 2980 }
@@ -2834,7 +3019,7 @@ static void addrconf_sit_config(struct net_device *dev)
2834 */ 3019 */
2835 3020
2836 idev = ipv6_find_idev(dev); 3021 idev = ipv6_find_idev(dev);
2837 if (idev == NULL) { 3022 if (!idev) {
2838 pr_debug("%s: add_dev failed\n", __func__); 3023 pr_debug("%s: add_dev failed\n", __func__);
2839 return; 3024 return;
2840 } 3025 }
@@ -2859,7 +3044,7 @@ static void addrconf_gre_config(struct net_device *dev)
2859 ASSERT_RTNL(); 3044 ASSERT_RTNL();
2860 3045
2861 idev = ipv6_find_idev(dev); 3046 idev = ipv6_find_idev(dev);
2862 if (idev == NULL) { 3047 if (!idev) {
2863 pr_debug("%s: add_dev failed\n", __func__); 3048 pr_debug("%s: add_dev failed\n", __func__);
2864 return; 3049 return;
2865 } 3050 }
@@ -3056,7 +3241,7 @@ static int addrconf_ifdown(struct net_device *dev, int how)
3056 neigh_ifdown(&nd_tbl, dev); 3241 neigh_ifdown(&nd_tbl, dev);
3057 3242
3058 idev = __in6_dev_get(dev); 3243 idev = __in6_dev_get(dev);
3059 if (idev == NULL) 3244 if (!idev)
3060 return -ENODEV; 3245 return -ENODEV;
3061 3246
3062 /* 3247 /*
@@ -3127,10 +3312,10 @@ restart:
3127 3312
3128 write_unlock_bh(&idev->lock); 3313 write_unlock_bh(&idev->lock);
3129 3314
3130 spin_lock_bh(&ifa->state_lock); 3315 spin_lock_bh(&ifa->lock);
3131 state = ifa->state; 3316 state = ifa->state;
3132 ifa->state = INET6_IFADDR_STATE_DEAD; 3317 ifa->state = INET6_IFADDR_STATE_DEAD;
3133 spin_unlock_bh(&ifa->state_lock); 3318 spin_unlock_bh(&ifa->lock);
3134 3319
3135 if (state != INET6_IFADDR_STATE_DEAD) { 3320 if (state != INET6_IFADDR_STATE_DEAD) {
3136 __ipv6_ifa_notify(RTM_DELADDR, ifa); 3321 __ipv6_ifa_notify(RTM_DELADDR, ifa);
@@ -3288,12 +3473,12 @@ static void addrconf_dad_start(struct inet6_ifaddr *ifp)
3288{ 3473{
3289 bool begin_dad = false; 3474 bool begin_dad = false;
3290 3475
3291 spin_lock_bh(&ifp->state_lock); 3476 spin_lock_bh(&ifp->lock);
3292 if (ifp->state != INET6_IFADDR_STATE_DEAD) { 3477 if (ifp->state != INET6_IFADDR_STATE_DEAD) {
3293 ifp->state = INET6_IFADDR_STATE_PREDAD; 3478 ifp->state = INET6_IFADDR_STATE_PREDAD;
3294 begin_dad = true; 3479 begin_dad = true;
3295 } 3480 }
3296 spin_unlock_bh(&ifp->state_lock); 3481 spin_unlock_bh(&ifp->lock);
3297 3482
3298 if (begin_dad) 3483 if (begin_dad)
3299 addrconf_mod_dad_work(ifp, 0); 3484 addrconf_mod_dad_work(ifp, 0);
@@ -3315,7 +3500,7 @@ static void addrconf_dad_work(struct work_struct *w)
3315 3500
3316 rtnl_lock(); 3501 rtnl_lock();
3317 3502
3318 spin_lock_bh(&ifp->state_lock); 3503 spin_lock_bh(&ifp->lock);
3319 if (ifp->state == INET6_IFADDR_STATE_PREDAD) { 3504 if (ifp->state == INET6_IFADDR_STATE_PREDAD) {
3320 action = DAD_BEGIN; 3505 action = DAD_BEGIN;
3321 ifp->state = INET6_IFADDR_STATE_DAD; 3506 ifp->state = INET6_IFADDR_STATE_DAD;
@@ -3323,7 +3508,7 @@ static void addrconf_dad_work(struct work_struct *w)
3323 action = DAD_ABORT; 3508 action = DAD_ABORT;
3324 ifp->state = INET6_IFADDR_STATE_POSTDAD; 3509 ifp->state = INET6_IFADDR_STATE_POSTDAD;
3325 } 3510 }
3326 spin_unlock_bh(&ifp->state_lock); 3511 spin_unlock_bh(&ifp->lock);
3327 3512
3328 if (action == DAD_BEGIN) { 3513 if (action == DAD_BEGIN) {
3329 addrconf_dad_begin(ifp); 3514 addrconf_dad_begin(ifp);
@@ -3811,7 +3996,7 @@ inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
3811 3996
3812 ifm = nlmsg_data(nlh); 3997 ifm = nlmsg_data(nlh);
3813 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 3998 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3814 if (pfx == NULL) 3999 if (!pfx)
3815 return -EINVAL; 4000 return -EINVAL;
3816 4001
3817 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4002 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
@@ -3923,7 +4108,7 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3923 4108
3924 ifm = nlmsg_data(nlh); 4109 ifm = nlmsg_data(nlh);
3925 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx); 4110 pfx = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer_pfx);
3926 if (pfx == NULL) 4111 if (!pfx)
3927 return -EINVAL; 4112 return -EINVAL;
3928 4113
3929 if (tb[IFA_CACHEINFO]) { 4114 if (tb[IFA_CACHEINFO]) {
@@ -3938,17 +4123,17 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
3938 } 4123 }
3939 4124
3940 dev = __dev_get_by_index(net, ifm->ifa_index); 4125 dev = __dev_get_by_index(net, ifm->ifa_index);
3941 if (dev == NULL) 4126 if (!dev)
3942 return -ENODEV; 4127 return -ENODEV;
3943 4128
3944 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags; 4129 ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) : ifm->ifa_flags;
3945 4130
3946 /* We ignore other flags so far. */ 4131 /* We ignore other flags so far. */
3947 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR | 4132 ifa_flags &= IFA_F_NODAD | IFA_F_HOMEADDRESS | IFA_F_MANAGETEMPADDR |
3948 IFA_F_NOPREFIXROUTE; 4133 IFA_F_NOPREFIXROUTE | IFA_F_MCAUTOJOIN;
3949 4134
3950 ifa = ipv6_get_ifaddr(net, pfx, dev, 1); 4135 ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
3951 if (ifa == NULL) { 4136 if (!ifa) {
3952 /* 4137 /*
3953 * It would be best to check for !NLM_F_CREATE here but 4138 * It would be best to check for !NLM_F_CREATE here but
3954 * userspace already relies on not having to provide this. 4139 * userspace already relies on not having to provide this.
@@ -4023,7 +4208,7 @@ static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4023 u32 preferred, valid; 4208 u32 preferred, valid;
4024 4209
4025 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4210 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4026 if (nlh == NULL) 4211 if (!nlh)
4027 return -EMSGSIZE; 4212 return -EMSGSIZE;
4028 4213
4029 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope), 4214 put_ifaddrmsg(nlh, ifa->prefix_len, ifa->flags, rt_scope(ifa->scope),
@@ -4052,11 +4237,11 @@ static int inet6_fill_ifaddr(struct sk_buff *skb, struct inet6_ifaddr *ifa,
4052 } 4237 }
4053 4238
4054 if (!ipv6_addr_any(&ifa->peer_addr)) { 4239 if (!ipv6_addr_any(&ifa->peer_addr)) {
4055 if (nla_put(skb, IFA_LOCAL, 16, &ifa->addr) < 0 || 4240 if (nla_put_in6_addr(skb, IFA_LOCAL, &ifa->addr) < 0 ||
4056 nla_put(skb, IFA_ADDRESS, 16, &ifa->peer_addr) < 0) 4241 nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->peer_addr) < 0)
4057 goto error; 4242 goto error;
4058 } else 4243 } else
4059 if (nla_put(skb, IFA_ADDRESS, 16, &ifa->addr) < 0) 4244 if (nla_put_in6_addr(skb, IFA_ADDRESS, &ifa->addr) < 0)
4060 goto error; 4245 goto error;
4061 4246
4062 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0) 4247 if (put_cacheinfo(skb, ifa->cstamp, ifa->tstamp, preferred, valid) < 0)
@@ -4084,11 +4269,11 @@ static int inet6_fill_ifmcaddr(struct sk_buff *skb, struct ifmcaddr6 *ifmca,
4084 scope = RT_SCOPE_SITE; 4269 scope = RT_SCOPE_SITE;
4085 4270
4086 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4271 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4087 if (nlh == NULL) 4272 if (!nlh)
4088 return -EMSGSIZE; 4273 return -EMSGSIZE;
4089 4274
4090 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4275 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4091 if (nla_put(skb, IFA_MULTICAST, 16, &ifmca->mca_addr) < 0 || 4276 if (nla_put_in6_addr(skb, IFA_MULTICAST, &ifmca->mca_addr) < 0 ||
4092 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp, 4277 put_cacheinfo(skb, ifmca->mca_cstamp, ifmca->mca_tstamp,
4093 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 4278 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4094 nlmsg_cancel(skb, nlh); 4279 nlmsg_cancel(skb, nlh);
@@ -4110,11 +4295,11 @@ static int inet6_fill_ifacaddr(struct sk_buff *skb, struct ifacaddr6 *ifaca,
4110 scope = RT_SCOPE_SITE; 4295 scope = RT_SCOPE_SITE;
4111 4296
4112 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags); 4297 nlh = nlmsg_put(skb, portid, seq, event, sizeof(struct ifaddrmsg), flags);
4113 if (nlh == NULL) 4298 if (!nlh)
4114 return -EMSGSIZE; 4299 return -EMSGSIZE;
4115 4300
4116 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex); 4301 put_ifaddrmsg(nlh, 128, IFA_F_PERMANENT, scope, ifindex);
4117 if (nla_put(skb, IFA_ANYCAST, 16, &ifaca->aca_addr) < 0 || 4302 if (nla_put_in6_addr(skb, IFA_ANYCAST, &ifaca->aca_addr) < 0 ||
4118 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp, 4303 put_cacheinfo(skb, ifaca->aca_cstamp, ifaca->aca_tstamp,
4119 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) { 4304 INFINITY_LIFE_TIME, INFINITY_LIFE_TIME) < 0) {
4120 nlmsg_cancel(skb, nlh); 4305 nlmsg_cancel(skb, nlh);
@@ -4283,7 +4468,7 @@ static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr *nlh)
4283 goto errout; 4468 goto errout;
4284 4469
4285 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer); 4470 addr = extract_addr(tb[IFA_ADDRESS], tb[IFA_LOCAL], &peer);
4286 if (addr == NULL) { 4471 if (!addr) {
4287 err = -EINVAL; 4472 err = -EINVAL;
4288 goto errout; 4473 goto errout;
4289 } 4474 }
@@ -4326,7 +4511,7 @@ static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
4326 int err = -ENOBUFS; 4511 int err = -ENOBUFS;
4327 4512
4328 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC); 4513 skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
4329 if (skb == NULL) 4514 if (!skb)
4330 goto errout; 4515 goto errout;
4331 4516
4332 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0); 4517 err = inet6_fill_ifaddr(skb, ifa, 0, 0, event, 0);
@@ -4398,6 +4583,7 @@ static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
4398 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc; 4583 array[DEVCONF_SUPPRESS_FRAG_NDISC] = cnf->suppress_frag_ndisc;
4399 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local; 4584 array[DEVCONF_ACCEPT_RA_FROM_LOCAL] = cnf->accept_ra_from_local;
4400 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu; 4585 array[DEVCONF_ACCEPT_RA_MTU] = cnf->accept_ra_mtu;
4586 /* we omit DEVCONF_STABLE_SECRET for now */
4401} 4587}
4402 4588
4403static inline size_t inet6_ifla6_size(void) 4589static inline size_t inet6_ifla6_size(void)
@@ -4478,24 +4664,24 @@ static int inet6_fill_ifla6_attrs(struct sk_buff *skb, struct inet6_dev *idev)
4478 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci)) 4664 if (nla_put(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci))
4479 goto nla_put_failure; 4665 goto nla_put_failure;
4480 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32)); 4666 nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
4481 if (nla == NULL) 4667 if (!nla)
4482 goto nla_put_failure; 4668 goto nla_put_failure;
4483 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla)); 4669 ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
4484 4670
4485 /* XXX - MC not implemented */ 4671 /* XXX - MC not implemented */
4486 4672
4487 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64)); 4673 nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
4488 if (nla == NULL) 4674 if (!nla)
4489 goto nla_put_failure; 4675 goto nla_put_failure;
4490 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla)); 4676 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
4491 4677
4492 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64)); 4678 nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
4493 if (nla == NULL) 4679 if (!nla)
4494 goto nla_put_failure; 4680 goto nla_put_failure;
4495 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla)); 4681 snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
4496 4682
4497 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr)); 4683 nla = nla_reserve(skb, IFLA_INET6_TOKEN, sizeof(struct in6_addr));
4498 if (nla == NULL) 4684 if (!nla)
4499 goto nla_put_failure; 4685 goto nla_put_failure;
4500 4686
4501 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode)) 4687 if (nla_put_u8(skb, IFLA_INET6_ADDR_GEN_MODE, idev->addr_gen_mode))
@@ -4541,7 +4727,7 @@ static int inet6_set_iftoken(struct inet6_dev *idev, struct in6_addr *token)
4541 4727
4542 ASSERT_RTNL(); 4728 ASSERT_RTNL();
4543 4729
4544 if (token == NULL) 4730 if (!token)
4545 return -EINVAL; 4731 return -EINVAL;
4546 if (ipv6_addr_any(token)) 4732 if (ipv6_addr_any(token))
4547 return -EINVAL; 4733 return -EINVAL;
@@ -4632,8 +4818,15 @@ static int inet6_set_link_af(struct net_device *dev, const struct nlattr *nla)
4632 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]); 4818 u8 mode = nla_get_u8(tb[IFLA_INET6_ADDR_GEN_MODE]);
4633 4819
4634 if (mode != IN6_ADDR_GEN_MODE_EUI64 && 4820 if (mode != IN6_ADDR_GEN_MODE_EUI64 &&
4635 mode != IN6_ADDR_GEN_MODE_NONE) 4821 mode != IN6_ADDR_GEN_MODE_NONE &&
4822 mode != IN6_ADDR_GEN_MODE_STABLE_PRIVACY)
4636 return -EINVAL; 4823 return -EINVAL;
4824
4825 if (mode == IN6_ADDR_GEN_MODE_STABLE_PRIVACY &&
4826 !idev->cnf.stable_secret.initialized &&
4827 !dev_net(dev)->ipv6.devconf_dflt->stable_secret.initialized)
4828 return -EINVAL;
4829
4637 idev->addr_gen_mode = mode; 4830 idev->addr_gen_mode = mode;
4638 err = 0; 4831 err = 0;
4639 } 4832 }
@@ -4650,7 +4843,7 @@ static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4650 void *protoinfo; 4843 void *protoinfo;
4651 4844
4652 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags); 4845 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*hdr), flags);
4653 if (nlh == NULL) 4846 if (!nlh)
4654 return -EMSGSIZE; 4847 return -EMSGSIZE;
4655 4848
4656 hdr = nlmsg_data(nlh); 4849 hdr = nlmsg_data(nlh);
@@ -4665,11 +4858,11 @@ static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
4665 (dev->addr_len && 4858 (dev->addr_len &&
4666 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) || 4859 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4667 nla_put_u32(skb, IFLA_MTU, dev->mtu) || 4860 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4668 (dev->ifindex != dev->iflink && 4861 (dev->ifindex != dev_get_iflink(dev) &&
4669 nla_put_u32(skb, IFLA_LINK, dev->iflink))) 4862 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4670 goto nla_put_failure; 4863 goto nla_put_failure;
4671 protoinfo = nla_nest_start(skb, IFLA_PROTINFO); 4864 protoinfo = nla_nest_start(skb, IFLA_PROTINFO);
4672 if (protoinfo == NULL) 4865 if (!protoinfo)
4673 goto nla_put_failure; 4866 goto nla_put_failure;
4674 4867
4675 if (inet6_fill_ifla6_attrs(skb, idev) < 0) 4868 if (inet6_fill_ifla6_attrs(skb, idev) < 0)
@@ -4730,7 +4923,7 @@ void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
4730 int err = -ENOBUFS; 4923 int err = -ENOBUFS;
4731 4924
4732 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC); 4925 skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
4733 if (skb == NULL) 4926 if (!skb)
4734 goto errout; 4927 goto errout;
4735 4928
4736 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0); 4929 err = inet6_fill_ifinfo(skb, idev, 0, 0, event, 0);
@@ -4763,7 +4956,7 @@ static int inet6_fill_prefix(struct sk_buff *skb, struct inet6_dev *idev,
4763 struct prefix_cacheinfo ci; 4956 struct prefix_cacheinfo ci;
4764 4957
4765 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags); 4958 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*pmsg), flags);
4766 if (nlh == NULL) 4959 if (!nlh)
4767 return -EMSGSIZE; 4960 return -EMSGSIZE;
4768 4961
4769 pmsg = nlmsg_data(nlh); 4962 pmsg = nlmsg_data(nlh);
@@ -4802,7 +4995,7 @@ static void inet6_prefix_notify(int event, struct inet6_dev *idev,
4802 int err = -ENOBUFS; 4995 int err = -ENOBUFS;
4803 4996
4804 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC); 4997 skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
4805 if (skb == NULL) 4998 if (!skb)
4806 goto errout; 4999 goto errout;
4807 5000
4808 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0); 5001 err = inet6_fill_prefix(skb, idev, pinfo, 0, 0, event, 0);
@@ -5042,6 +5235,74 @@ int addrconf_sysctl_proxy_ndp(struct ctl_table *ctl, int write,
5042 return ret; 5235 return ret;
5043} 5236}
5044 5237
5238static int addrconf_sysctl_stable_secret(struct ctl_table *ctl, int write,
5239 void __user *buffer, size_t *lenp,
5240 loff_t *ppos)
5241{
5242 int err;
5243 struct in6_addr addr;
5244 char str[IPV6_MAX_STRLEN];
5245 struct ctl_table lctl = *ctl;
5246 struct net *net = ctl->extra2;
5247 struct ipv6_stable_secret *secret = ctl->data;
5248
5249 if (&net->ipv6.devconf_all->stable_secret == ctl->data)
5250 return -EIO;
5251
5252 lctl.maxlen = IPV6_MAX_STRLEN;
5253 lctl.data = str;
5254
5255 if (!rtnl_trylock())
5256 return restart_syscall();
5257
5258 if (!write && !secret->initialized) {
5259 err = -EIO;
5260 goto out;
5261 }
5262
5263 if (!write) {
5264 err = snprintf(str, sizeof(str), "%pI6",
5265 &secret->secret);
5266 if (err >= sizeof(str)) {
5267 err = -EIO;
5268 goto out;
5269 }
5270 }
5271
5272 err = proc_dostring(&lctl, write, buffer, lenp, ppos);
5273 if (err || !write)
5274 goto out;
5275
5276 if (in6_pton(str, -1, addr.in6_u.u6_addr8, -1, NULL) != 1) {
5277 err = -EIO;
5278 goto out;
5279 }
5280
5281 secret->initialized = true;
5282 secret->secret = addr;
5283
5284 if (&net->ipv6.devconf_dflt->stable_secret == ctl->data) {
5285 struct net_device *dev;
5286
5287 for_each_netdev(net, dev) {
5288 struct inet6_dev *idev = __in6_dev_get(dev);
5289
5290 if (idev) {
5291 idev->addr_gen_mode =
5292 IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5293 }
5294 }
5295 } else {
5296 struct inet6_dev *idev = ctl->extra1;
5297
5298 idev->addr_gen_mode = IN6_ADDR_GEN_MODE_STABLE_PRIVACY;
5299 }
5300
5301out:
5302 rtnl_unlock();
5303
5304 return err;
5305}
5045 5306
5046static struct addrconf_sysctl_table 5307static struct addrconf_sysctl_table
5047{ 5308{
@@ -5315,6 +5576,13 @@ static struct addrconf_sysctl_table
5315 .proc_handler = proc_dointvec, 5576 .proc_handler = proc_dointvec,
5316 }, 5577 },
5317 { 5578 {
5579 .procname = "stable_secret",
5580 .data = &ipv6_devconf.stable_secret,
5581 .maxlen = IPV6_MAX_STRLEN,
5582 .mode = 0600,
5583 .proc_handler = addrconf_sysctl_stable_secret,
5584 },
5585 {
5318 /* sentinel */ 5586 /* sentinel */
5319 } 5587 }
5320 }, 5588 },
@@ -5328,7 +5596,7 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5328 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ]; 5596 char path[sizeof("net/ipv6/conf/") + IFNAMSIZ];
5329 5597
5330 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL); 5598 t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
5331 if (t == NULL) 5599 if (!t)
5332 goto out; 5600 goto out;
5333 5601
5334 for (i = 0; t->addrconf_vars[i].data; i++) { 5602 for (i = 0; t->addrconf_vars[i].data; i++) {
@@ -5340,7 +5608,7 @@ static int __addrconf_sysctl_register(struct net *net, char *dev_name,
5340 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name); 5608 snprintf(path, sizeof(path), "net/ipv6/conf/%s", dev_name);
5341 5609
5342 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars); 5610 t->sysctl_header = register_net_sysctl(net, path, t->addrconf_vars);
5343 if (t->sysctl_header == NULL) 5611 if (!t->sysctl_header)
5344 goto free; 5612 goto free;
5345 5613
5346 p->sysctl = t; 5614 p->sysctl = t;
@@ -5356,7 +5624,7 @@ static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
5356{ 5624{
5357 struct addrconf_sysctl_table *t; 5625 struct addrconf_sysctl_table *t;
5358 5626
5359 if (p->sysctl == NULL) 5627 if (!p->sysctl)
5360 return; 5628 return;
5361 5629
5362 t = p->sysctl; 5630 t = p->sysctl;
@@ -5399,17 +5667,20 @@ static int __net_init addrconf_init_net(struct net *net)
5399 struct ipv6_devconf *all, *dflt; 5667 struct ipv6_devconf *all, *dflt;
5400 5668
5401 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL); 5669 all = kmemdup(&ipv6_devconf, sizeof(ipv6_devconf), GFP_KERNEL);
5402 if (all == NULL) 5670 if (!all)
5403 goto err_alloc_all; 5671 goto err_alloc_all;
5404 5672
5405 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL); 5673 dflt = kmemdup(&ipv6_devconf_dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
5406 if (dflt == NULL) 5674 if (!dflt)
5407 goto err_alloc_dflt; 5675 goto err_alloc_dflt;
5408 5676
5409 /* these will be inherited by all namespaces */ 5677 /* these will be inherited by all namespaces */
5410 dflt->autoconf = ipv6_defaults.autoconf; 5678 dflt->autoconf = ipv6_defaults.autoconf;
5411 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6; 5679 dflt->disable_ipv6 = ipv6_defaults.disable_ipv6;
5412 5680
5681 dflt->stable_secret.initialized = false;
5682 all->stable_secret.initialized = false;
5683
5413 net->ipv6.devconf_all = all; 5684 net->ipv6.devconf_all = all;
5414 net->ipv6.devconf_dflt = dflt; 5685 net->ipv6.devconf_dflt = dflt;
5415 5686
diff --git a/net/ipv6/addrconf_core.c b/net/ipv6/addrconf_core.c
index 98cc4cd570e2..d873ceea86e6 100644
--- a/net/ipv6/addrconf_core.c
+++ b/net/ipv6/addrconf_core.c
@@ -140,7 +140,7 @@ void in6_dev_finish_destroy(struct inet6_dev *idev)
140 struct net_device *dev = idev->dev; 140 struct net_device *dev = idev->dev;
141 141
142 WARN_ON(!list_empty(&idev->addr_list)); 142 WARN_ON(!list_empty(&idev->addr_list));
143 WARN_ON(idev->mc_list != NULL); 143 WARN_ON(idev->mc_list);
144 WARN_ON(timer_pending(&idev->rs_timer)); 144 WARN_ON(timer_pending(&idev->rs_timer));
145 145
146#ifdef NET_REFCNT_DEBUG 146#ifdef NET_REFCNT_DEBUG
diff --git a/net/ipv6/addrlabel.c b/net/ipv6/addrlabel.c
index e43e79d0a612..882124ebb438 100644
--- a/net/ipv6/addrlabel.c
+++ b/net/ipv6/addrlabel.c
@@ -29,9 +29,7 @@
29 * Policy Table 29 * Policy Table
30 */ 30 */
31struct ip6addrlbl_entry { 31struct ip6addrlbl_entry {
32#ifdef CONFIG_NET_NS 32 possible_net_t lbl_net;
33 struct net *lbl_net;
34#endif
35 struct in6_addr prefix; 33 struct in6_addr prefix;
36 int prefixlen; 34 int prefixlen;
37 int ifindex; 35 int ifindex;
@@ -129,9 +127,6 @@ static const __net_initconst struct ip6addrlbl_init_table
129/* Object management */ 127/* Object management */
130static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p) 128static inline void ip6addrlbl_free(struct ip6addrlbl_entry *p)
131{ 129{
132#ifdef CONFIG_NET_NS
133 release_net(p->lbl_net);
134#endif
135 kfree(p); 130 kfree(p);
136} 131}
137 132
@@ -240,9 +235,7 @@ static struct ip6addrlbl_entry *ip6addrlbl_alloc(struct net *net,
240 newp->addrtype = addrtype; 235 newp->addrtype = addrtype;
241 newp->label = label; 236 newp->label = label;
242 INIT_HLIST_NODE(&newp->list); 237 INIT_HLIST_NODE(&newp->list);
243#ifdef CONFIG_NET_NS 238 write_pnet(&newp->lbl_net, net);
244 newp->lbl_net = hold_net(net);
245#endif
246 atomic_set(&newp->refcnt, 1); 239 atomic_set(&newp->refcnt, 1);
247 return newp; 240 return newp;
248} 241}
@@ -484,7 +477,7 @@ static int ip6addrlbl_fill(struct sk_buff *skb,
484 477
485 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq); 478 ip6addrlbl_putmsg(nlh, p->prefixlen, p->ifindex, lseq);
486 479
487 if (nla_put(skb, IFAL_ADDRESS, 16, &p->prefix) < 0 || 480 if (nla_put_in6_addr(skb, IFAL_ADDRESS, &p->prefix) < 0 ||
488 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) { 481 nla_put_u32(skb, IFAL_LABEL, p->label) < 0) {
489 nlmsg_cancel(skb, nlh); 482 nlmsg_cancel(skb, nlh);
490 return -EMSGSIZE; 483 return -EMSGSIZE;
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index e8c4400f23e9..eef63b394c5a 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -164,11 +164,11 @@ lookup_protocol:
164 answer_flags = answer->flags; 164 answer_flags = answer->flags;
165 rcu_read_unlock(); 165 rcu_read_unlock();
166 166
167 WARN_ON(answer_prot->slab == NULL); 167 WARN_ON(!answer_prot->slab);
168 168
169 err = -ENOBUFS; 169 err = -ENOBUFS;
170 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot); 170 sk = sk_alloc(net, PF_INET6, GFP_KERNEL, answer_prot);
171 if (sk == NULL) 171 if (!sk)
172 goto out; 172 goto out;
173 173
174 sock_init_data(sock, sk); 174 sock_init_data(sock, sk);
@@ -391,7 +391,7 @@ int inet6_release(struct socket *sock)
391{ 391{
392 struct sock *sk = sock->sk; 392 struct sock *sk = sock->sk;
393 393
394 if (sk == NULL) 394 if (!sk)
395 return -EINVAL; 395 return -EINVAL;
396 396
397 /* Free mc lists */ 397 /* Free mc lists */
@@ -413,11 +413,11 @@ void inet6_destroy_sock(struct sock *sk)
413 /* Release rx options */ 413 /* Release rx options */
414 414
415 skb = xchg(&np->pktoptions, NULL); 415 skb = xchg(&np->pktoptions, NULL);
416 if (skb != NULL) 416 if (skb)
417 kfree_skb(skb); 417 kfree_skb(skb);
418 418
419 skb = xchg(&np->rxpmtu, NULL); 419 skb = xchg(&np->rxpmtu, NULL);
420 if (skb != NULL) 420 if (skb)
421 kfree_skb(skb); 421 kfree_skb(skb);
422 422
423 /* Free flowlabels */ 423 /* Free flowlabels */
@@ -426,7 +426,7 @@ void inet6_destroy_sock(struct sock *sk)
426 /* Free tx options */ 426 /* Free tx options */
427 427
428 opt = xchg(&np->opt, NULL); 428 opt = xchg(&np->opt, NULL);
429 if (opt != NULL) 429 if (opt)
430 sock_kfree_s(sk, opt, opt->tot_len); 430 sock_kfree_s(sk, opt, opt->tot_len);
431} 431}
432EXPORT_SYMBOL_GPL(inet6_destroy_sock); 432EXPORT_SYMBOL_GPL(inet6_destroy_sock);
@@ -640,7 +640,7 @@ int inet6_sk_rebuild_header(struct sock *sk)
640 640
641 dst = __sk_dst_check(sk, np->dst_cookie); 641 dst = __sk_dst_check(sk, np->dst_cookie);
642 642
643 if (dst == NULL) { 643 if (!dst) {
644 struct inet_sock *inet = inet_sk(sk); 644 struct inet_sock *inet = inet_sk(sk);
645 struct in6_addr *final_p, final; 645 struct in6_addr *final_p, final;
646 struct flowi6 fl6; 646 struct flowi6 fl6;
@@ -766,6 +766,8 @@ static int __net_init inet6_net_init(struct net *net)
766 net->ipv6.sysctl.icmpv6_time = 1*HZ; 766 net->ipv6.sysctl.icmpv6_time = 1*HZ;
767 net->ipv6.sysctl.flowlabel_consistency = 1; 767 net->ipv6.sysctl.flowlabel_consistency = 1;
768 net->ipv6.sysctl.auto_flowlabels = 0; 768 net->ipv6.sysctl.auto_flowlabels = 0;
769 net->ipv6.sysctl.idgen_retries = 3;
770 net->ipv6.sysctl.idgen_delay = 1 * HZ;
769 atomic_set(&net->ipv6.fib6_sernum, 1); 771 atomic_set(&net->ipv6.fib6_sernum, 1);
770 772
771 err = ipv6_init_mibs(net); 773 err = ipv6_init_mibs(net);
@@ -824,7 +826,7 @@ static int __init inet6_init(void)
824 struct list_head *r; 826 struct list_head *r;
825 int err = 0; 827 int err = 0;
826 828
827 BUILD_BUG_ON(sizeof(struct inet6_skb_parm) > FIELD_SIZEOF(struct sk_buff, cb)); 829 sock_skb_cb_check_size(sizeof(struct inet6_skb_parm));
828 830
829 /* Register the socket-side information for inet6_create. */ 831 /* Register the socket-side information for inet6_create. */
830 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r) 832 for (r = &inetsw6[0]; r < &inetsw6[SOCK_MAX]; ++r)
diff --git a/net/ipv6/ah6.c b/net/ipv6/ah6.c
index a6727add2624..ed7d4e3f9c10 100644
--- a/net/ipv6/ah6.c
+++ b/net/ipv6/ah6.c
@@ -681,7 +681,7 @@ static int ah6_init_state(struct xfrm_state *x)
681 goto error; 681 goto error;
682 682
683 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL); 683 ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
684 if (ahp == NULL) 684 if (!ahp)
685 return -ENOMEM; 685 return -ENOMEM;
686 686
687 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0); 687 ahash = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
diff --git a/net/ipv6/anycast.c b/net/ipv6/anycast.c
index baf2742d1ec4..514ac259f543 100644
--- a/net/ipv6/anycast.c
+++ b/net/ipv6/anycast.c
@@ -60,6 +60,8 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
60 int ishost = !net->ipv6.devconf_all->forwarding; 60 int ishost = !net->ipv6.devconf_all->forwarding;
61 int err = 0; 61 int err = 0;
62 62
63 ASSERT_RTNL();
64
63 if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) 65 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
64 return -EPERM; 66 return -EPERM;
65 if (ipv6_addr_is_multicast(addr)) 67 if (ipv6_addr_is_multicast(addr))
@@ -68,12 +70,11 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
68 return -EINVAL; 70 return -EINVAL;
69 71
70 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL); 72 pac = sock_kmalloc(sk, sizeof(struct ipv6_ac_socklist), GFP_KERNEL);
71 if (pac == NULL) 73 if (!pac)
72 return -ENOMEM; 74 return -ENOMEM;
73 pac->acl_next = NULL; 75 pac->acl_next = NULL;
74 pac->acl_addr = *addr; 76 pac->acl_addr = *addr;
75 77
76 rtnl_lock();
77 if (ifindex == 0) { 78 if (ifindex == 0) {
78 struct rt6_info *rt; 79 struct rt6_info *rt;
79 80
@@ -92,7 +93,7 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
92 } else 93 } else
93 dev = __dev_get_by_index(net, ifindex); 94 dev = __dev_get_by_index(net, ifindex);
94 95
95 if (dev == NULL) { 96 if (!dev) {
96 err = -ENODEV; 97 err = -ENODEV;
97 goto error; 98 goto error;
98 } 99 }
@@ -130,7 +131,6 @@ int ipv6_sock_ac_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
130 } 131 }
131 132
132error: 133error:
133 rtnl_unlock();
134 if (pac) 134 if (pac)
135 sock_kfree_s(sk, pac, sizeof(*pac)); 135 sock_kfree_s(sk, pac, sizeof(*pac));
136 return err; 136 return err;
@@ -146,7 +146,8 @@ int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
146 struct ipv6_ac_socklist *pac, *prev_pac; 146 struct ipv6_ac_socklist *pac, *prev_pac;
147 struct net *net = sock_net(sk); 147 struct net *net = sock_net(sk);
148 148
149 rtnl_lock(); 149 ASSERT_RTNL();
150
150 prev_pac = NULL; 151 prev_pac = NULL;
151 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) { 152 for (pac = np->ipv6_ac_list; pac; pac = pac->acl_next) {
152 if ((ifindex == 0 || pac->acl_ifindex == ifindex) && 153 if ((ifindex == 0 || pac->acl_ifindex == ifindex) &&
@@ -154,10 +155,8 @@ int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
154 break; 155 break;
155 prev_pac = pac; 156 prev_pac = pac;
156 } 157 }
157 if (!pac) { 158 if (!pac)
158 rtnl_unlock();
159 return -ENOENT; 159 return -ENOENT;
160 }
161 if (prev_pac) 160 if (prev_pac)
162 prev_pac->acl_next = pac->acl_next; 161 prev_pac->acl_next = pac->acl_next;
163 else 162 else
@@ -166,7 +165,6 @@ int ipv6_sock_ac_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
166 dev = __dev_get_by_index(net, pac->acl_ifindex); 165 dev = __dev_get_by_index(net, pac->acl_ifindex);
167 if (dev) 166 if (dev)
168 ipv6_dev_ac_dec(dev, &pac->acl_addr); 167 ipv6_dev_ac_dec(dev, &pac->acl_addr);
169 rtnl_unlock();
170 168
171 sock_kfree_s(sk, pac, sizeof(*pac)); 169 sock_kfree_s(sk, pac, sizeof(*pac));
172 return 0; 170 return 0;
@@ -224,7 +222,7 @@ static struct ifacaddr6 *aca_alloc(struct rt6_info *rt,
224 struct ifacaddr6 *aca; 222 struct ifacaddr6 *aca;
225 223
226 aca = kzalloc(sizeof(*aca), GFP_ATOMIC); 224 aca = kzalloc(sizeof(*aca), GFP_ATOMIC);
227 if (aca == NULL) 225 if (!aca)
228 return NULL; 226 return NULL;
229 227
230 aca->aca_addr = *addr; 228 aca->aca_addr = *addr;
@@ -270,7 +268,7 @@ int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
270 goto out; 268 goto out;
271 } 269 }
272 aca = aca_alloc(rt, addr); 270 aca = aca_alloc(rt, addr);
273 if (aca == NULL) { 271 if (!aca) {
274 ip6_rt_put(rt); 272 ip6_rt_put(rt);
275 err = -ENOMEM; 273 err = -ENOMEM;
276 goto out; 274 goto out;
@@ -339,7 +337,7 @@ static int ipv6_dev_ac_dec(struct net_device *dev, const struct in6_addr *addr)
339{ 337{
340 struct inet6_dev *idev = __in6_dev_get(dev); 338 struct inet6_dev *idev = __in6_dev_get(dev);
341 339
342 if (idev == NULL) 340 if (!idev)
343 return -ENODEV; 341 return -ENODEV;
344 return __ipv6_dev_ac_dec(idev, addr); 342 return __ipv6_dev_ac_dec(idev, addr);
345} 343}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ace8daca5c83..762a58c772b8 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -71,7 +71,7 @@ int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
71 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK; 71 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
72 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 72 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
73 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 73 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
74 if (flowlabel == NULL) 74 if (!flowlabel)
75 return -EINVAL; 75 return -EINVAL;
76 } 76 }
77 } 77 }
@@ -373,7 +373,7 @@ int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
373 373
374 err = -EAGAIN; 374 err = -EAGAIN;
375 skb = sock_dequeue_err_skb(sk); 375 skb = sock_dequeue_err_skb(sk);
376 if (skb == NULL) 376 if (!skb)
377 goto out; 377 goto out;
378 378
379 copied = skb->len; 379 copied = skb->len;
@@ -463,7 +463,7 @@ int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
463 463
464 err = -EAGAIN; 464 err = -EAGAIN;
465 skb = xchg(&np->rxpmtu, NULL); 465 skb = xchg(&np->rxpmtu, NULL);
466 if (skb == NULL) 466 if (!skb)
467 goto out; 467 goto out;
468 468
469 copied = skb->len; 469 copied = skb->len;
diff --git a/net/ipv6/esp6.c b/net/ipv6/esp6.c
index e48f2c7c5c59..31f1b5d5e2ef 100644
--- a/net/ipv6/esp6.c
+++ b/net/ipv6/esp6.c
@@ -495,7 +495,7 @@ static int esp_init_authenc(struct xfrm_state *x)
495 int err; 495 int err;
496 496
497 err = -EINVAL; 497 err = -EINVAL;
498 if (x->ealg == NULL) 498 if (!x->ealg)
499 goto error; 499 goto error;
500 500
501 err = -ENAMETOOLONG; 501 err = -ENAMETOOLONG;
diff --git a/net/ipv6/exthdrs_core.c b/net/ipv6/exthdrs_core.c
index 8af3eb57f438..5c5d23e59da5 100644
--- a/net/ipv6/exthdrs_core.c
+++ b/net/ipv6/exthdrs_core.c
@@ -82,7 +82,7 @@ int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
82 if (nexthdr == NEXTHDR_NONE) 82 if (nexthdr == NEXTHDR_NONE)
83 return -1; 83 return -1;
84 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); 84 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
85 if (hp == NULL) 85 if (!hp)
86 return -1; 86 return -1;
87 if (nexthdr == NEXTHDR_FRAGMENT) { 87 if (nexthdr == NEXTHDR_FRAGMENT) {
88 __be16 _frag_off, *fp; 88 __be16 _frag_off, *fp;
@@ -91,7 +91,7 @@ int ipv6_skip_exthdr(const struct sk_buff *skb, int start, u8 *nexthdrp,
91 frag_off), 91 frag_off),
92 sizeof(_frag_off), 92 sizeof(_frag_off),
93 &_frag_off); 93 &_frag_off);
94 if (fp == NULL) 94 if (!fp)
95 return -1; 95 return -1;
96 96
97 *frag_offp = *fp; 97 *frag_offp = *fp;
@@ -218,7 +218,7 @@ int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
218 } 218 }
219 219
220 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr); 220 hp = skb_header_pointer(skb, start, sizeof(_hdr), &_hdr);
221 if (hp == NULL) 221 if (!hp)
222 return -EBADMSG; 222 return -EBADMSG;
223 223
224 if (nexthdr == NEXTHDR_ROUTING) { 224 if (nexthdr == NEXTHDR_ROUTING) {
@@ -226,7 +226,7 @@ int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
226 226
227 rh = skb_header_pointer(skb, start, sizeof(_rh), 227 rh = skb_header_pointer(skb, start, sizeof(_rh),
228 &_rh); 228 &_rh);
229 if (rh == NULL) 229 if (!rh)
230 return -EBADMSG; 230 return -EBADMSG;
231 231
232 if (flags && (*flags & IP6_FH_F_SKIP_RH) && 232 if (flags && (*flags & IP6_FH_F_SKIP_RH) &&
@@ -245,7 +245,7 @@ int ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
245 frag_off), 245 frag_off),
246 sizeof(_frag_off), 246 sizeof(_frag_off),
247 &_frag_off); 247 &_frag_off);
248 if (fp == NULL) 248 if (!fp)
249 return -EBADMSG; 249 return -EBADMSG;
250 250
251 _frag_off = ntohs(*fp) & ~0x7; 251 _frag_off = ntohs(*fp) & ~0x7;
diff --git a/net/ipv6/fib6_rules.c b/net/ipv6/fib6_rules.c
index 70bc6abc0639..2367a16eae58 100644
--- a/net/ipv6/fib6_rules.c
+++ b/net/ipv6/fib6_rules.c
@@ -199,12 +199,10 @@ static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199 } 199 }
200 200
201 if (frh->src_len) 201 if (frh->src_len)
202 nla_memcpy(&rule6->src.addr, tb[FRA_SRC], 202 rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
203 sizeof(struct in6_addr));
204 203
205 if (frh->dst_len) 204 if (frh->dst_len)
206 nla_memcpy(&rule6->dst.addr, tb[FRA_DST], 205 rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
207 sizeof(struct in6_addr));
208 206
209 rule6->src.plen = frh->src_len; 207 rule6->src.plen = frh->src_len;
210 rule6->dst.plen = frh->dst_len; 208 rule6->dst.plen = frh->dst_len;
@@ -250,11 +248,9 @@ static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
250 frh->tos = rule6->tclass; 248 frh->tos = rule6->tclass;
251 249
252 if ((rule6->dst.plen && 250 if ((rule6->dst.plen &&
253 nla_put(skb, FRA_DST, sizeof(struct in6_addr), 251 nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
254 &rule6->dst.addr)) ||
255 (rule6->src.plen && 252 (rule6->src.plen &&
256 nla_put(skb, FRA_SRC, sizeof(struct in6_addr), 253 nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
257 &rule6->src.addr)))
258 goto nla_put_failure; 254 goto nla_put_failure;
259 return 0; 255 return 0;
260 256
@@ -299,19 +295,16 @@ static int __net_init fib6_rules_net_init(struct net *net)
299 ops = fib_rules_register(&fib6_rules_ops_template, net); 295 ops = fib_rules_register(&fib6_rules_ops_template, net);
300 if (IS_ERR(ops)) 296 if (IS_ERR(ops))
301 return PTR_ERR(ops); 297 return PTR_ERR(ops);
302 net->ipv6.fib6_rules_ops = ops;
303
304 298
305 err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 0, 299 err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
306 RT6_TABLE_LOCAL, 0);
307 if (err) 300 if (err)
308 goto out_fib6_rules_ops; 301 goto out_fib6_rules_ops;
309 302
310 err = fib_default_rule_add(net->ipv6.fib6_rules_ops, 303 err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
311 0x7FFE, RT6_TABLE_MAIN, 0);
312 if (err) 304 if (err)
313 goto out_fib6_rules_ops; 305 goto out_fib6_rules_ops;
314 306
307 net->ipv6.fib6_rules_ops = ops;
315out: 308out:
316 return err; 309 return err;
317 310
diff --git a/net/ipv6/icmp.c b/net/ipv6/icmp.c
index a5e95199585e..2c2b5d51f15c 100644
--- a/net/ipv6/icmp.c
+++ b/net/ipv6/icmp.c
@@ -160,8 +160,7 @@ static bool is_ineligible(const struct sk_buff *skb)
160 tp = skb_header_pointer(skb, 160 tp = skb_header_pointer(skb,
161 ptr+offsetof(struct icmp6hdr, icmp6_type), 161 ptr+offsetof(struct icmp6hdr, icmp6_type),
162 sizeof(_type), &_type); 162 sizeof(_type), &_type);
163 if (tp == NULL || 163 if (!tp || !(*tp & ICMPV6_INFOMSG_MASK))
164 !(*tp & ICMPV6_INFOMSG_MASK))
165 return true; 164 return true;
166 } 165 }
167 return false; 166 return false;
@@ -231,7 +230,7 @@ static bool opt_unrec(struct sk_buff *skb, __u32 offset)
231 230
232 offset += skb_network_offset(skb); 231 offset += skb_network_offset(skb);
233 op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval); 232 op = skb_header_pointer(skb, offset, sizeof(_optval), &_optval);
234 if (op == NULL) 233 if (!op)
235 return true; 234 return true;
236 return (*op & 0xC0) == 0x80; 235 return (*op & 0xC0) == 0x80;
237} 236}
@@ -244,7 +243,7 @@ int icmpv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
244 int err = 0; 243 int err = 0;
245 244
246 skb = skb_peek(&sk->sk_write_queue); 245 skb = skb_peek(&sk->sk_write_queue);
247 if (skb == NULL) 246 if (!skb)
248 goto out; 247 goto out;
249 248
250 icmp6h = icmp6_hdr(skb); 249 icmp6h = icmp6_hdr(skb);
@@ -479,7 +478,7 @@ static void icmp6_send(struct sk_buff *skb, u8 type, u8 code, __u32 info)
479 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 478 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
480 479
481 sk = icmpv6_xmit_lock(net); 480 sk = icmpv6_xmit_lock(net);
482 if (sk == NULL) 481 if (!sk)
483 return; 482 return;
484 sk->sk_mark = mark; 483 sk->sk_mark = mark;
485 np = inet6_sk(sk); 484 np = inet6_sk(sk);
@@ -582,7 +581,7 @@ static void icmpv6_echo_reply(struct sk_buff *skb)
582 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); 581 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
583 582
584 sk = icmpv6_xmit_lock(net); 583 sk = icmpv6_xmit_lock(net);
585 if (sk == NULL) 584 if (!sk)
586 return; 585 return;
587 sk->sk_mark = mark; 586 sk->sk_mark = mark;
588 np = inet6_sk(sk); 587 np = inet6_sk(sk);
@@ -839,7 +838,7 @@ static int __net_init icmpv6_sk_init(struct net *net)
839 838
840 net->ipv6.icmp_sk = 839 net->ipv6.icmp_sk =
841 kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL); 840 kzalloc(nr_cpu_ids * sizeof(struct sock *), GFP_KERNEL);
842 if (net->ipv6.icmp_sk == NULL) 841 if (!net->ipv6.icmp_sk)
843 return -ENOMEM; 842 return -ENOMEM;
844 843
845 for_each_possible_cpu(i) { 844 for_each_possible_cpu(i) {
diff --git a/net/ipv6/inet6_connection_sock.c b/net/ipv6/inet6_connection_sock.c
index 29b32206e494..6927f3fb5597 100644
--- a/net/ipv6/inet6_connection_sock.c
+++ b/net/ipv6/inet6_connection_sock.c
@@ -112,22 +112,20 @@ static u32 inet6_synq_hash(const struct in6_addr *raddr, const __be16 rport,
112 return c & (synq_hsize - 1); 112 return c & (synq_hsize - 1);
113} 113}
114 114
115struct request_sock *inet6_csk_search_req(const struct sock *sk, 115struct request_sock *inet6_csk_search_req(struct sock *sk,
116 struct request_sock ***prevp,
117 const __be16 rport, 116 const __be16 rport,
118 const struct in6_addr *raddr, 117 const struct in6_addr *raddr,
119 const struct in6_addr *laddr, 118 const struct in6_addr *laddr,
120 const int iif) 119 const int iif)
121{ 120{
122 const struct inet_connection_sock *icsk = inet_csk(sk); 121 struct inet_connection_sock *icsk = inet_csk(sk);
123 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt; 122 struct listen_sock *lopt = icsk->icsk_accept_queue.listen_opt;
124 struct request_sock *req, **prev; 123 struct request_sock *req;
124 u32 hash = inet6_synq_hash(raddr, rport, lopt->hash_rnd,
125 lopt->nr_table_entries);
125 126
126 for (prev = &lopt->syn_table[inet6_synq_hash(raddr, rport, 127 spin_lock(&icsk->icsk_accept_queue.syn_wait_lock);
127 lopt->hash_rnd, 128 for (req = lopt->syn_table[hash]; req != NULL; req = req->dl_next) {
128 lopt->nr_table_entries)];
129 (req = *prev) != NULL;
130 prev = &req->dl_next) {
131 const struct inet_request_sock *ireq = inet_rsk(req); 129 const struct inet_request_sock *ireq = inet_rsk(req);
132 130
133 if (ireq->ir_rmt_port == rport && 131 if (ireq->ir_rmt_port == rport &&
@@ -135,13 +133,14 @@ struct request_sock *inet6_csk_search_req(const struct sock *sk,
135 ipv6_addr_equal(&ireq->ir_v6_rmt_addr, raddr) && 133 ipv6_addr_equal(&ireq->ir_v6_rmt_addr, raddr) &&
136 ipv6_addr_equal(&ireq->ir_v6_loc_addr, laddr) && 134 ipv6_addr_equal(&ireq->ir_v6_loc_addr, laddr) &&
137 (!ireq->ir_iif || ireq->ir_iif == iif)) { 135 (!ireq->ir_iif || ireq->ir_iif == iif)) {
136 atomic_inc(&req->rsk_refcnt);
138 WARN_ON(req->sk != NULL); 137 WARN_ON(req->sk != NULL);
139 *prevp = prev; 138 break;
140 return req;
141 } 139 }
142 } 140 }
141 spin_unlock(&icsk->icsk_accept_queue.syn_wait_lock);
143 142
144 return NULL; 143 return req;
145} 144}
146EXPORT_SYMBOL_GPL(inet6_csk_search_req); 145EXPORT_SYMBOL_GPL(inet6_csk_search_req);
147 146
diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
index 051dffb49c90..871641bc1ed4 100644
--- a/net/ipv6/inet6_hashtables.c
+++ b/net/ipv6/inet6_hashtables.c
@@ -23,11 +23,9 @@
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, 26u32 inet6_ehashfn(const struct net *net,
27 const struct in6_addr *laddr, 27 const struct in6_addr *laddr, const u16 lport,
28 const u16 lport, 28 const struct in6_addr *faddr, const __be16 fport)
29 const struct in6_addr *faddr,
30 const __be16 fport)
31{ 29{
32 static u32 inet6_ehash_secret __read_mostly; 30 static u32 inet6_ehash_secret __read_mostly;
33 static u32 ipv6_hash_secret __read_mostly; 31 static u32 ipv6_hash_secret __read_mostly;
@@ -44,54 +42,6 @@ static unsigned int inet6_ehashfn(struct net *net,
44 inet6_ehash_secret + net_hash_mix(net)); 42 inet6_ehash_secret + net_hash_mix(net));
45} 43}
46 44
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
59int __inet6_hash(struct sock *sk, struct inet_timewait_sock *tw)
60{
61 struct inet_hashinfo *hashinfo = sk->sk_prot->h.hashinfo;
62 int twrefcnt = 0;
63
64 WARN_ON(!sk_unhashed(sk));
65
66 if (sk->sk_state == TCP_LISTEN) {
67 struct inet_listen_hashbucket *ilb;
68
69 ilb = &hashinfo->listening_hash[inet_sk_listen_hashfn(sk)];
70 spin_lock(&ilb->lock);
71 __sk_nulls_add_node_rcu(sk, &ilb->head);
72 spin_unlock(&ilb->lock);
73 } else {
74 unsigned int hash;
75 struct hlist_nulls_head *list;
76 spinlock_t *lock;
77
78 sk->sk_hash = hash = inet6_sk_ehashfn(sk);
79 list = &inet_ehash_bucket(hashinfo, hash)->chain;
80 lock = inet_ehash_lockp(hashinfo, hash);
81 spin_lock(lock);
82 __sk_nulls_add_node_rcu(sk, list);
83 if (tw) {
84 WARN_ON(sk->sk_hash != tw->tw_hash);
85 twrefcnt = inet_twsk_unhash(tw);
86 }
87 spin_unlock(lock);
88 }
89
90 sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
91 return twrefcnt;
92}
93EXPORT_SYMBOL(__inet6_hash);
94
95/* 45/*
96 * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so 46 * Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
97 * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM 47 * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
@@ -296,7 +246,7 @@ static int __inet6_check_established(struct inet_timewait_death_row *death_row,
296 *twp = tw; 246 *twp = tw;
297 } else if (tw) { 247 } else if (tw) {
298 /* Silly. Should hash-dance instead... */ 248 /* Silly. Should hash-dance instead... */
299 inet_twsk_deschedule(tw, death_row); 249 inet_twsk_deschedule(tw);
300 250
301 inet_twsk_put(tw); 251 inet_twsk_put(tw);
302 } 252 }
@@ -320,6 +270,6 @@ int inet6_hash_connect(struct inet_timewait_death_row *death_row,
320 struct sock *sk) 270 struct sock *sk)
321{ 271{
322 return __inet_hash_connect(death_row, sk, inet6_sk_port_offset(sk), 272 return __inet_hash_connect(death_row, sk, inet6_sk_port_offset(sk),
323 __inet6_check_established, __inet6_hash); 273 __inet6_check_established);
324} 274}
325EXPORT_SYMBOL_GPL(inet6_hash_connect); 275EXPORT_SYMBOL_GPL(inet6_hash_connect);
diff --git a/net/ipv6/ip6_fib.c b/net/ipv6/ip6_fib.c
index 263ef4143bff..96dbffff5a24 100644
--- a/net/ipv6/ip6_fib.c
+++ b/net/ipv6/ip6_fib.c
@@ -1206,7 +1206,7 @@ static struct fib6_node *fib6_repair_tree(struct net *net,
1206 1206
1207 WARN_ON(fn->fn_flags & RTN_RTINFO); 1207 WARN_ON(fn->fn_flags & RTN_RTINFO);
1208 WARN_ON(fn->fn_flags & RTN_TL_ROOT); 1208 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1209 WARN_ON(fn->leaf != NULL); 1209 WARN_ON(fn->leaf);
1210 1210
1211 children = 0; 1211 children = 0;
1212 child = NULL; 1212 child = NULL;
@@ -1361,7 +1361,7 @@ int fib6_del(struct rt6_info *rt, struct nl_info *info)
1361 1361
1362#if RT6_DEBUG >= 2 1362#if RT6_DEBUG >= 2
1363 if (rt->dst.obsolete > 0) { 1363 if (rt->dst.obsolete > 0) {
1364 WARN_ON(fn != NULL); 1364 WARN_ON(fn);
1365 return -ENOENT; 1365 return -ENOENT;
1366 } 1366 }
1367#endif 1367#endif
diff --git a/net/ipv6/ip6_flowlabel.c b/net/ipv6/ip6_flowlabel.c
index f45d6db50a45..d491125011c4 100644
--- a/net/ipv6/ip6_flowlabel.c
+++ b/net/ipv6/ip6_flowlabel.c
@@ -100,7 +100,6 @@ static void fl_free(struct ip6_flowlabel *fl)
100 if (fl) { 100 if (fl) {
101 if (fl->share == IPV6_FL_S_PROCESS) 101 if (fl->share == IPV6_FL_S_PROCESS)
102 put_pid(fl->owner.pid); 102 put_pid(fl->owner.pid);
103 release_net(fl->fl_net);
104 kfree(fl->opt); 103 kfree(fl->opt);
105 kfree_rcu(fl, rcu); 104 kfree_rcu(fl, rcu);
106 } 105 }
@@ -206,7 +205,7 @@ static struct ip6_flowlabel *fl_intern(struct net *net,
206 fl->label = htonl(prandom_u32())&IPV6_FLOWLABEL_MASK; 205 fl->label = htonl(prandom_u32())&IPV6_FLOWLABEL_MASK;
207 if (fl->label) { 206 if (fl->label) {
208 lfl = __fl_lookup(net, fl->label); 207 lfl = __fl_lookup(net, fl->label);
209 if (lfl == NULL) 208 if (!lfl)
210 break; 209 break;
211 } 210 }
212 } 211 }
@@ -220,7 +219,7 @@ static struct ip6_flowlabel *fl_intern(struct net *net,
220 * with the same label can only appear on another sock 219 * with the same label can only appear on another sock
221 */ 220 */
222 lfl = __fl_lookup(net, fl->label); 221 lfl = __fl_lookup(net, fl->label);
223 if (lfl != NULL) { 222 if (lfl) {
224 atomic_inc(&lfl->users); 223 atomic_inc(&lfl->users);
225 spin_unlock_bh(&ip6_fl_lock); 224 spin_unlock_bh(&ip6_fl_lock);
226 return lfl; 225 return lfl;
@@ -298,10 +297,10 @@ struct ipv6_txoptions *fl6_merge_options(struct ipv6_txoptions *opt_space,
298{ 297{
299 struct ipv6_txoptions *fl_opt = fl->opt; 298 struct ipv6_txoptions *fl_opt = fl->opt;
300 299
301 if (fopt == NULL || fopt->opt_flen == 0) 300 if (!fopt || fopt->opt_flen == 0)
302 return fl_opt; 301 return fl_opt;
303 302
304 if (fl_opt != NULL) { 303 if (fl_opt) {
305 opt_space->hopopt = fl_opt->hopopt; 304 opt_space->hopopt = fl_opt->hopopt;
306 opt_space->dst0opt = fl_opt->dst0opt; 305 opt_space->dst0opt = fl_opt->dst0opt;
307 opt_space->srcrt = fl_opt->srcrt; 306 opt_space->srcrt = fl_opt->srcrt;
@@ -367,7 +366,7 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
367 366
368 err = -ENOMEM; 367 err = -ENOMEM;
369 fl = kzalloc(sizeof(*fl), GFP_KERNEL); 368 fl = kzalloc(sizeof(*fl), GFP_KERNEL);
370 if (fl == NULL) 369 if (!fl)
371 goto done; 370 goto done;
372 371
373 if (olen > 0) { 372 if (olen > 0) {
@@ -377,7 +376,7 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
377 376
378 err = -ENOMEM; 377 err = -ENOMEM;
379 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL); 378 fl->opt = kmalloc(sizeof(*fl->opt) + olen, GFP_KERNEL);
380 if (fl->opt == NULL) 379 if (!fl->opt)
381 goto done; 380 goto done;
382 381
383 memset(fl->opt, 0, sizeof(*fl->opt)); 382 memset(fl->opt, 0, sizeof(*fl->opt));
@@ -403,7 +402,7 @@ fl_create(struct net *net, struct sock *sk, struct in6_flowlabel_req *freq,
403 } 402 }
404 } 403 }
405 404
406 fl->fl_net = hold_net(net); 405 fl->fl_net = net;
407 fl->expires = jiffies; 406 fl->expires = jiffies;
408 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires); 407 err = fl6_renew(fl, freq->flr_linger, freq->flr_expires);
409 if (err) 408 if (err)
@@ -597,7 +596,7 @@ int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
597 return -EINVAL; 596 return -EINVAL;
598 597
599 fl = fl_create(net, sk, &freq, optval, optlen, &err); 598 fl = fl_create(net, sk, &freq, optval, optlen, &err);
600 if (fl == NULL) 599 if (!fl)
601 return err; 600 return err;
602 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL); 601 sfl1 = kmalloc(sizeof(*sfl1), GFP_KERNEL);
603 602
@@ -617,7 +616,7 @@ int ipv6_flowlabel_opt(struct sock *sk, char __user *optval, int optlen)
617 } 616 }
618 rcu_read_unlock_bh(); 617 rcu_read_unlock_bh();
619 618
620 if (fl1 == NULL) 619 if (!fl1)
621 fl1 = fl_lookup(net, freq.flr_label); 620 fl1 = fl_lookup(net, freq.flr_label);
622 if (fl1) { 621 if (fl1) {
623recheck: 622recheck:
@@ -634,7 +633,7 @@ recheck:
634 goto release; 633 goto release;
635 634
636 err = -ENOMEM; 635 err = -ENOMEM;
637 if (sfl1 == NULL) 636 if (!sfl1)
638 goto release; 637 goto release;
639 if (fl->linger > fl1->linger) 638 if (fl->linger > fl1->linger)
640 fl1->linger = fl->linger; 639 fl1->linger = fl->linger;
@@ -654,7 +653,7 @@ release:
654 goto done; 653 goto done;
655 654
656 err = -ENOMEM; 655 err = -ENOMEM;
657 if (sfl1 == NULL) 656 if (!sfl1)
658 goto done; 657 goto done;
659 658
660 err = mem_check(sk); 659 err = mem_check(sk);
@@ -662,7 +661,7 @@ release:
662 goto done; 661 goto done;
663 662
664 fl1 = fl_intern(net, fl, freq.flr_label); 663 fl1 = fl_intern(net, fl, freq.flr_label);
665 if (fl1 != NULL) 664 if (fl1)
666 goto recheck; 665 goto recheck;
667 666
668 if (!freq.flr_label) { 667 if (!freq.flr_label) {
diff --git a/net/ipv6/ip6_gre.c b/net/ipv6/ip6_gre.c
index bc28b7d42a6d..b5e6cc1d4a73 100644
--- a/net/ipv6/ip6_gre.c
+++ b/net/ipv6/ip6_gre.c
@@ -223,7 +223,7 @@ static struct ip6_tnl *ip6gre_tunnel_lookup(struct net_device *dev,
223 } 223 }
224 } 224 }
225 225
226 if (cand != NULL) 226 if (cand)
227 return cand; 227 return cand;
228 228
229 dev = ign->fb_tunnel_dev; 229 dev = ign->fb_tunnel_dev;
@@ -395,7 +395,7 @@ static void ip6gre_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
395 flags & GRE_KEY ? 395 flags & GRE_KEY ?
396 *(((__be32 *)p) + (grehlen / 4) - 1) : 0, 396 *(((__be32 *)p) + (grehlen / 4) - 1) : 0,
397 p[1]); 397 p[1]);
398 if (t == NULL) 398 if (!t)
399 return; 399 return;
400 400
401 switch (type) { 401 switch (type) {
@@ -760,7 +760,7 @@ static netdev_tx_t ip6gre_xmit2(struct sk_buff *skb,
760 760
761 skb_set_inner_protocol(skb, protocol); 761 skb_set_inner_protocol(skb, protocol);
762 762
763 ip6tunnel_xmit(skb, dev); 763 ip6tunnel_xmit(NULL, skb, dev);
764 if (ndst) 764 if (ndst)
765 ip6_tnl_dst_store(tunnel, ndst); 765 ip6_tnl_dst_store(tunnel, ndst);
766 return 0; 766 return 0;
@@ -980,7 +980,7 @@ static void ip6gre_tnl_link_config(struct ip6_tnl *t, int set_mtu)
980 &p->raddr, &p->laddr, 980 &p->raddr, &p->laddr,
981 p->link, strict); 981 p->link, strict);
982 982
983 if (rt == NULL) 983 if (!rt)
984 return; 984 return;
985 985
986 if (rt->dst.dev) { 986 if (rt->dst.dev) {
@@ -1073,7 +1073,7 @@ static int ip6gre_tunnel_ioctl(struct net_device *dev,
1073 } 1073 }
1074 ip6gre_tnl_parm_from_user(&p1, &p); 1074 ip6gre_tnl_parm_from_user(&p1, &p);
1075 t = ip6gre_tunnel_locate(net, &p1, 0); 1075 t = ip6gre_tunnel_locate(net, &p1, 0);
1076 if (t == NULL) 1076 if (!t)
1077 t = netdev_priv(dev); 1077 t = netdev_priv(dev);
1078 } 1078 }
1079 memset(&p, 0, sizeof(p)); 1079 memset(&p, 0, sizeof(p));
@@ -1105,7 +1105,7 @@ static int ip6gre_tunnel_ioctl(struct net_device *dev,
1105 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL); 1105 t = ip6gre_tunnel_locate(net, &p1, cmd == SIOCADDTUNNEL);
1106 1106
1107 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 1107 if (dev != ign->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1108 if (t != NULL) { 1108 if (t) {
1109 if (t->dev != dev) { 1109 if (t->dev != dev) {
1110 err = -EEXIST; 1110 err = -EEXIST;
1111 break; 1111 break;
@@ -1144,7 +1144,7 @@ static int ip6gre_tunnel_ioctl(struct net_device *dev,
1144 err = -ENOENT; 1144 err = -ENOENT;
1145 ip6gre_tnl_parm_from_user(&p1, &p); 1145 ip6gre_tnl_parm_from_user(&p1, &p);
1146 t = ip6gre_tunnel_locate(net, &p1, 0); 1146 t = ip6gre_tunnel_locate(net, &p1, 0);
1147 if (t == NULL) 1147 if (!t)
1148 goto done; 1148 goto done;
1149 err = -EPERM; 1149 err = -EPERM;
1150 if (t == netdev_priv(ign->fb_tunnel_dev)) 1150 if (t == netdev_priv(ign->fb_tunnel_dev))
@@ -1216,6 +1216,7 @@ static const struct net_device_ops ip6gre_netdev_ops = {
1216 .ndo_do_ioctl = ip6gre_tunnel_ioctl, 1216 .ndo_do_ioctl = ip6gre_tunnel_ioctl,
1217 .ndo_change_mtu = ip6gre_tunnel_change_mtu, 1217 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1218 .ndo_get_stats64 = ip_tunnel_get_stats64, 1218 .ndo_get_stats64 = ip_tunnel_get_stats64,
1219 .ndo_get_iflink = ip6_tnl_get_iflink,
1219}; 1220};
1220 1221
1221static void ip6gre_dev_free(struct net_device *dev) 1222static void ip6gre_dev_free(struct net_device *dev)
@@ -1238,7 +1239,6 @@ static void ip6gre_tunnel_setup(struct net_device *dev)
1238 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT)) 1239 if (!(t->parms.flags & IP6_TNL_F_IGN_ENCAP_LIMIT))
1239 dev->mtu -= 8; 1240 dev->mtu -= 8;
1240 dev->flags |= IFF_NOARP; 1241 dev->flags |= IFF_NOARP;
1241 dev->iflink = 0;
1242 dev->addr_len = sizeof(struct in6_addr); 1242 dev->addr_len = sizeof(struct in6_addr);
1243 netif_keep_dst(dev); 1243 netif_keep_dst(dev);
1244} 1244}
@@ -1270,8 +1270,6 @@ static int ip6gre_tunnel_init(struct net_device *dev)
1270 u64_stats_init(&ip6gre_tunnel_stats->syncp); 1270 u64_stats_init(&ip6gre_tunnel_stats->syncp);
1271 } 1271 }
1272 1272
1273 dev->iflink = tunnel->parms.link;
1274
1275 return 0; 1273 return 0;
1276} 1274}
1277 1275
@@ -1313,7 +1311,7 @@ static void ip6gre_destroy_tunnels(struct net *net, struct list_head *head)
1313 1311
1314 t = rtnl_dereference(ign->tunnels[prio][h]); 1312 t = rtnl_dereference(ign->tunnels[prio][h]);
1315 1313
1316 while (t != NULL) { 1314 while (t) {
1317 /* If dev is in the same netns, it has already 1315 /* If dev is in the same netns, it has already
1318 * been added to the list by the previous loop. 1316 * been added to the list by the previous loop.
1319 */ 1317 */
@@ -1412,7 +1410,7 @@ static int ip6gre_tap_validate(struct nlattr *tb[], struct nlattr *data[])
1412 goto out; 1410 goto out;
1413 1411
1414 if (data[IFLA_GRE_REMOTE]) { 1412 if (data[IFLA_GRE_REMOTE]) {
1415 nla_memcpy(&daddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); 1413 daddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1416 if (ipv6_addr_any(&daddr)) 1414 if (ipv6_addr_any(&daddr))
1417 return -EINVAL; 1415 return -EINVAL;
1418 } 1416 }
@@ -1446,10 +1444,10 @@ static void ip6gre_netlink_parms(struct nlattr *data[],
1446 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]); 1444 parms->o_key = nla_get_be32(data[IFLA_GRE_OKEY]);
1447 1445
1448 if (data[IFLA_GRE_LOCAL]) 1446 if (data[IFLA_GRE_LOCAL])
1449 nla_memcpy(&parms->laddr, data[IFLA_GRE_LOCAL], sizeof(struct in6_addr)); 1447 parms->laddr = nla_get_in6_addr(data[IFLA_GRE_LOCAL]);
1450 1448
1451 if (data[IFLA_GRE_REMOTE]) 1449 if (data[IFLA_GRE_REMOTE])
1452 nla_memcpy(&parms->raddr, data[IFLA_GRE_REMOTE], sizeof(struct in6_addr)); 1450 parms->raddr = nla_get_in6_addr(data[IFLA_GRE_REMOTE]);
1453 1451
1454 if (data[IFLA_GRE_TTL]) 1452 if (data[IFLA_GRE_TTL])
1455 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]); 1453 parms->hop_limit = nla_get_u8(data[IFLA_GRE_TTL]);
@@ -1480,8 +1478,6 @@ static int ip6gre_tap_init(struct net_device *dev)
1480 if (!dev->tstats) 1478 if (!dev->tstats)
1481 return -ENOMEM; 1479 return -ENOMEM;
1482 1480
1483 dev->iflink = tunnel->parms.link;
1484
1485 return 0; 1481 return 0;
1486} 1482}
1487 1483
@@ -1493,6 +1489,7 @@ static const struct net_device_ops ip6gre_tap_netdev_ops = {
1493 .ndo_validate_addr = eth_validate_addr, 1489 .ndo_validate_addr = eth_validate_addr,
1494 .ndo_change_mtu = ip6gre_tunnel_change_mtu, 1490 .ndo_change_mtu = ip6gre_tunnel_change_mtu,
1495 .ndo_get_stats64 = ip_tunnel_get_stats64, 1491 .ndo_get_stats64 = ip_tunnel_get_stats64,
1492 .ndo_get_iflink = ip6_tnl_get_iflink,
1496}; 1493};
1497 1494
1498static void ip6gre_tap_setup(struct net_device *dev) 1495static void ip6gre_tap_setup(struct net_device *dev)
@@ -1503,7 +1500,6 @@ static void ip6gre_tap_setup(struct net_device *dev)
1503 dev->netdev_ops = &ip6gre_tap_netdev_ops; 1500 dev->netdev_ops = &ip6gre_tap_netdev_ops;
1504 dev->destructor = ip6gre_dev_free; 1501 dev->destructor = ip6gre_dev_free;
1505 1502
1506 dev->iflink = 0;
1507 dev->features |= NETIF_F_NETNS_LOCAL; 1503 dev->features |= NETIF_F_NETNS_LOCAL;
1508} 1504}
1509 1505
@@ -1622,8 +1618,8 @@ static int ip6gre_fill_info(struct sk_buff *skb, const struct net_device *dev)
1622 nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) || 1618 nla_put_be16(skb, IFLA_GRE_OFLAGS, p->o_flags) ||
1623 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) || 1619 nla_put_be32(skb, IFLA_GRE_IKEY, p->i_key) ||
1624 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) || 1620 nla_put_be32(skb, IFLA_GRE_OKEY, p->o_key) ||
1625 nla_put(skb, IFLA_GRE_LOCAL, sizeof(struct in6_addr), &p->laddr) || 1621 nla_put_in6_addr(skb, IFLA_GRE_LOCAL, &p->laddr) ||
1626 nla_put(skb, IFLA_GRE_REMOTE, sizeof(struct in6_addr), &p->raddr) || 1622 nla_put_in6_addr(skb, IFLA_GRE_REMOTE, &p->raddr) ||
1627 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) || 1623 nla_put_u8(skb, IFLA_GRE_TTL, p->hop_limit) ||
1628 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/ 1624 /*nla_put_u8(skb, IFLA_GRE_TOS, t->priority) ||*/
1629 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) || 1625 nla_put_u8(skb, IFLA_GRE_ENCAP_LIMIT, p->encap_limit) ||
diff --git a/net/ipv6/ip6_input.c b/net/ipv6/ip6_input.c
index aacdcb4dc762..f2e464eba5ef 100644
--- a/net/ipv6/ip6_input.c
+++ b/net/ipv6/ip6_input.c
@@ -46,8 +46,7 @@
46#include <net/xfrm.h> 46#include <net/xfrm.h>
47#include <net/inet_ecn.h> 47#include <net/inet_ecn.h>
48 48
49 49int ip6_rcv_finish(struct sock *sk, struct sk_buff *skb)
50int ip6_rcv_finish(struct sk_buff *skb)
51{ 50{
52 if (sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) { 51 if (sysctl_ip_early_demux && !skb_dst(skb) && skb->sk == NULL) {
53 const struct inet6_protocol *ipprot; 52 const struct inet6_protocol *ipprot;
@@ -183,7 +182,8 @@ int ipv6_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt
183 /* Must drop socket now because of tproxy. */ 182 /* Must drop socket now because of tproxy. */
184 skb_orphan(skb); 183 skb_orphan(skb);
185 184
186 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, dev, NULL, 185 return NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, NULL, skb,
186 dev, NULL,
187 ip6_rcv_finish); 187 ip6_rcv_finish);
188err: 188err:
189 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS); 189 IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_INHDRERRORS);
@@ -198,7 +198,7 @@ drop:
198 */ 198 */
199 199
200 200
201static int ip6_input_finish(struct sk_buff *skb) 201static int ip6_input_finish(struct sock *sk, struct sk_buff *skb)
202{ 202{
203 struct net *net = dev_net(skb_dst(skb)->dev); 203 struct net *net = dev_net(skb_dst(skb)->dev);
204 const struct inet6_protocol *ipprot; 204 const struct inet6_protocol *ipprot;
@@ -221,7 +221,7 @@ resubmit:
221 221
222 raw = raw6_local_deliver(skb, nexthdr); 222 raw = raw6_local_deliver(skb, nexthdr);
223 ipprot = rcu_dereference(inet6_protos[nexthdr]); 223 ipprot = rcu_dereference(inet6_protos[nexthdr]);
224 if (ipprot != NULL) { 224 if (ipprot) {
225 int ret; 225 int ret;
226 226
227 if (ipprot->flags & INET6_PROTO_FINAL) { 227 if (ipprot->flags & INET6_PROTO_FINAL) {
@@ -277,7 +277,8 @@ discard:
277 277
278int ip6_input(struct sk_buff *skb) 278int ip6_input(struct sk_buff *skb)
279{ 279{
280 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, skb, skb->dev, NULL, 280 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_IN, NULL, skb,
281 skb->dev, NULL,
281 ip6_input_finish); 282 ip6_input_finish);
282} 283}
283 284
diff --git a/net/ipv6/ip6_offload.c b/net/ipv6/ip6_offload.c
index 46d452a56d3e..e893cd18612f 100644
--- a/net/ipv6/ip6_offload.c
+++ b/net/ipv6/ip6_offload.c
@@ -124,7 +124,7 @@ static struct sk_buff *ipv6_gso_segment(struct sk_buff *skb,
124 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr); 124 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
125 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen); 125 fptr = (struct frag_hdr *)((u8 *)ipv6h + unfrag_ip6hlen);
126 fptr->frag_off = htons(offset); 126 fptr->frag_off = htons(offset);
127 if (skb->next != NULL) 127 if (skb->next)
128 fptr->frag_off |= htons(IP6_MF); 128 fptr->frag_off |= htons(IP6_MF);
129 offset += (ntohs(ipv6h->payload_len) - 129 offset += (ntohs(ipv6h->payload_len) -
130 sizeof(struct frag_hdr)); 130 sizeof(struct frag_hdr));
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 36cf0ab685a0..7fde1f265c90 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -56,7 +56,7 @@
56#include <net/checksum.h> 56#include <net/checksum.h>
57#include <linux/mroute6.h> 57#include <linux/mroute6.h>
58 58
59static int ip6_finish_output2(struct sk_buff *skb) 59static int ip6_finish_output2(struct sock *sk, struct sk_buff *skb)
60{ 60{
61 struct dst_entry *dst = skb_dst(skb); 61 struct dst_entry *dst = skb_dst(skb);
62 struct net_device *dev = dst->dev; 62 struct net_device *dev = dst->dev;
@@ -70,7 +70,7 @@ static int ip6_finish_output2(struct sk_buff *skb)
70 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) { 70 if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
71 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); 71 struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
72 72
73 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) && 73 if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(sk) &&
74 ((mroute6_socket(dev_net(dev), skb) && 74 ((mroute6_socket(dev_net(dev), skb) &&
75 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) || 75 !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
76 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr, 76 ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
@@ -82,7 +82,7 @@ static int ip6_finish_output2(struct sk_buff *skb)
82 */ 82 */
83 if (newskb) 83 if (newskb)
84 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING, 84 NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
85 newskb, NULL, newskb->dev, 85 sk, newskb, NULL, newskb->dev,
86 dev_loopback_xmit); 86 dev_loopback_xmit);
87 87
88 if (ipv6_hdr(skb)->hop_limit == 0) { 88 if (ipv6_hdr(skb)->hop_limit == 0) {
@@ -122,14 +122,14 @@ static int ip6_finish_output2(struct sk_buff *skb)
122 return -EINVAL; 122 return -EINVAL;
123} 123}
124 124
125static int ip6_finish_output(struct sk_buff *skb) 125static int ip6_finish_output(struct sock *sk, 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 (IP6CB(skb)->frag_max_size && skb->len > IP6CB(skb)->frag_max_size))
130 return ip6_fragment(skb, ip6_finish_output2); 130 return ip6_fragment(sk, skb, ip6_finish_output2);
131 else 131 else
132 return ip6_finish_output2(skb); 132 return ip6_finish_output2(sk, skb);
133} 133}
134 134
135int ip6_output(struct sock *sk, struct sk_buff *skb) 135int ip6_output(struct sock *sk, struct sk_buff *skb)
@@ -143,7 +143,8 @@ int ip6_output(struct sock *sk, struct sk_buff *skb)
143 return 0; 143 return 0;
144 } 144 }
145 145
146 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev, 146 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, sk, skb,
147 NULL, dev,
147 ip6_finish_output, 148 ip6_finish_output,
148 !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 149 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
149} 150}
@@ -177,7 +178,7 @@ int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
177 178
178 if (skb_headroom(skb) < head_room) { 179 if (skb_headroom(skb) < head_room) {
179 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room); 180 struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
180 if (skb2 == NULL) { 181 if (!skb2) {
181 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), 182 IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
182 IPSTATS_MIB_OUTDISCARDS); 183 IPSTATS_MIB_OUTDISCARDS);
183 kfree_skb(skb); 184 kfree_skb(skb);
@@ -223,8 +224,8 @@ int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
223 if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) { 224 if ((skb->len <= mtu) || skb->ignore_df || skb_is_gso(skb)) {
224 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)), 225 IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
225 IPSTATS_MIB_OUT, skb->len); 226 IPSTATS_MIB_OUT, skb->len);
226 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, 227 return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
227 dst->dev, dst_output); 228 NULL, dst->dev, dst_output_sk);
228 } 229 }
229 230
230 skb->dev = dst->dev; 231 skb->dev = dst->dev;
@@ -316,10 +317,10 @@ static int ip6_forward_proxy_check(struct sk_buff *skb)
316 return 0; 317 return 0;
317} 318}
318 319
319static inline int ip6_forward_finish(struct sk_buff *skb) 320static inline int ip6_forward_finish(struct sock *sk, struct sk_buff *skb)
320{ 321{
321 skb_sender_cpu_clear(skb); 322 skb_sender_cpu_clear(skb);
322 return dst_output(skb); 323 return dst_output_sk(sk, skb);
323} 324}
324 325
325static unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst) 326static unsigned int ip6_dst_mtu_forward(const struct dst_entry *dst)
@@ -511,7 +512,8 @@ int ip6_forward(struct sk_buff *skb)
511 512
512 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS); 513 IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
513 IP6_ADD_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len); 514 IP6_ADD_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTOCTETS, skb->len);
514 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev, 515 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, NULL, skb,
516 skb->dev, dst->dev,
515 ip6_forward_finish); 517 ip6_forward_finish);
516 518
517error: 519error:
@@ -538,7 +540,8 @@ static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
538 skb_copy_secmark(to, from); 540 skb_copy_secmark(to, from);
539} 541}
540 542
541int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *)) 543int ip6_fragment(struct sock *sk, struct sk_buff *skb,
544 int (*output)(struct sock *, struct sk_buff *))
542{ 545{
543 struct sk_buff *frag; 546 struct sk_buff *frag;
544 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 547 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
@@ -629,7 +632,7 @@ int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
629 skb_reset_network_header(skb); 632 skb_reset_network_header(skb);
630 memcpy(skb_network_header(skb), tmp_hdr, hlen); 633 memcpy(skb_network_header(skb), tmp_hdr, hlen);
631 634
632 ipv6_select_ident(fh, rt); 635 ipv6_select_ident(net, fh, rt);
633 fh->nexthdr = nexthdr; 636 fh->nexthdr = nexthdr;
634 fh->reserved = 0; 637 fh->reserved = 0;
635 fh->frag_off = htons(IP6_MF); 638 fh->frag_off = htons(IP6_MF);
@@ -658,7 +661,7 @@ int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
658 fh->nexthdr = nexthdr; 661 fh->nexthdr = nexthdr;
659 fh->reserved = 0; 662 fh->reserved = 0;
660 fh->frag_off = htons(offset); 663 fh->frag_off = htons(offset);
661 if (frag->next != NULL) 664 if (frag->next)
662 fh->frag_off |= htons(IP6_MF); 665 fh->frag_off |= htons(IP6_MF);
663 fh->identification = frag_id; 666 fh->identification = frag_id;
664 ipv6_hdr(frag)->payload_len = 667 ipv6_hdr(frag)->payload_len =
@@ -667,7 +670,7 @@ int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
667 ip6_copy_metadata(frag, skb); 670 ip6_copy_metadata(frag, skb);
668 } 671 }
669 672
670 err = output(skb); 673 err = output(sk, skb);
671 if (!err) 674 if (!err)
672 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst), 675 IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
673 IPSTATS_MIB_FRAGCREATES); 676 IPSTATS_MIB_FRAGCREATES);
@@ -776,7 +779,7 @@ slow_path:
776 fh->nexthdr = nexthdr; 779 fh->nexthdr = nexthdr;
777 fh->reserved = 0; 780 fh->reserved = 0;
778 if (!frag_id) { 781 if (!frag_id) {
779 ipv6_select_ident(fh, rt); 782 ipv6_select_ident(net, fh, rt);
780 frag_id = fh->identification; 783 frag_id = fh->identification;
781 } else 784 } else
782 fh->identification = frag_id; 785 fh->identification = frag_id;
@@ -800,7 +803,7 @@ slow_path:
800 /* 803 /*
801 * Put this fragment into the sending queue. 804 * Put this fragment into the sending queue.
802 */ 805 */
803 err = output(frag); 806 err = output(sk, frag);
804 if (err) 807 if (err)
805 goto fail; 808 goto fail;
806 809
@@ -824,7 +827,7 @@ static inline int ip6_rt_check(const struct rt6key *rt_key,
824 const struct in6_addr *addr_cache) 827 const struct in6_addr *addr_cache)
825{ 828{
826 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) && 829 return (rt_key->plen != 128 || !ipv6_addr_equal(fl_addr, &rt_key->addr)) &&
827 (addr_cache == NULL || !ipv6_addr_equal(fl_addr, addr_cache)); 830 (!addr_cache || !ipv6_addr_equal(fl_addr, addr_cache));
828} 831}
829 832
830static struct dst_entry *ip6_sk_dst_check(struct sock *sk, 833static struct dst_entry *ip6_sk_dst_check(struct sock *sk,
@@ -883,7 +886,7 @@ static int ip6_dst_lookup_tail(struct sock *sk,
883#endif 886#endif
884 int err; 887 int err;
885 888
886 if (*dst == NULL) 889 if (!*dst)
887 *dst = ip6_route_output(net, sk, fl6); 890 *dst = ip6_route_output(net, sk, fl6);
888 891
889 err = (*dst)->error; 892 err = (*dst)->error;
@@ -1046,11 +1049,11 @@ static inline int ip6_ufo_append_data(struct sock *sk,
1046 * udp datagram 1049 * udp datagram
1047 */ 1050 */
1048 skb = skb_peek_tail(queue); 1051 skb = skb_peek_tail(queue);
1049 if (skb == NULL) { 1052 if (!skb) {
1050 skb = sock_alloc_send_skb(sk, 1053 skb = sock_alloc_send_skb(sk,
1051 hh_len + fragheaderlen + transhdrlen + 20, 1054 hh_len + fragheaderlen + transhdrlen + 20,
1052 (flags & MSG_DONTWAIT), &err); 1055 (flags & MSG_DONTWAIT), &err);
1053 if (skb == NULL) 1056 if (!skb)
1054 return err; 1057 return err;
1055 1058
1056 /* reserve space for Hardware header */ 1059 /* reserve space for Hardware header */
@@ -1080,7 +1083,7 @@ static inline int ip6_ufo_append_data(struct sock *sk,
1080 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen - 1083 skb_shinfo(skb)->gso_size = (mtu - fragheaderlen -
1081 sizeof(struct frag_hdr)) & ~7; 1084 sizeof(struct frag_hdr)) & ~7;
1082 skb_shinfo(skb)->gso_type = SKB_GSO_UDP; 1085 skb_shinfo(skb)->gso_type = SKB_GSO_UDP;
1083 ipv6_select_ident(&fhdr, rt); 1086 ipv6_select_ident(sock_net(sk), &fhdr, rt);
1084 skb_shinfo(skb)->ip6_frag_id = fhdr.identification; 1087 skb_shinfo(skb)->ip6_frag_id = fhdr.identification;
1085 1088
1086append: 1089append:
@@ -1108,7 +1111,7 @@ static void ip6_append_data_mtu(unsigned int *mtu,
1108 unsigned int orig_mtu) 1111 unsigned int orig_mtu)
1109{ 1112{
1110 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) { 1113 if (!(rt->dst.flags & DST_XFRM_TUNNEL)) {
1111 if (skb == NULL) { 1114 if (!skb) {
1112 /* first fragment, reserve header_len */ 1115 /* first fragment, reserve header_len */
1113 *mtu = orig_mtu - rt->dst.header_len; 1116 *mtu = orig_mtu - rt->dst.header_len;
1114 1117
@@ -1140,7 +1143,7 @@ static int ip6_setup_cork(struct sock *sk, struct inet_cork_full *cork,
1140 return -EINVAL; 1143 return -EINVAL;
1141 1144
1142 v6_cork->opt = kzalloc(opt->tot_len, sk->sk_allocation); 1145 v6_cork->opt = kzalloc(opt->tot_len, sk->sk_allocation);
1143 if (unlikely(v6_cork->opt == NULL)) 1146 if (unlikely(!v6_cork->opt))
1144 return -ENOBUFS; 1147 return -ENOBUFS;
1145 1148
1146 v6_cork->opt->tot_len = opt->tot_len; 1149 v6_cork->opt->tot_len = opt->tot_len;
@@ -1332,7 +1335,7 @@ alloc_new_skb:
1332 else 1335 else
1333 fraggap = 0; 1336 fraggap = 0;
1334 /* update mtu and maxfraglen if necessary */ 1337 /* update mtu and maxfraglen if necessary */
1335 if (skb == NULL || skb_prev == NULL) 1338 if (!skb || !skb_prev)
1336 ip6_append_data_mtu(&mtu, &maxfraglen, 1339 ip6_append_data_mtu(&mtu, &maxfraglen,
1337 fragheaderlen, skb, rt, 1340 fragheaderlen, skb, rt,
1338 orig_mtu); 1341 orig_mtu);
@@ -1384,10 +1387,10 @@ alloc_new_skb:
1384 skb = sock_wmalloc(sk, 1387 skb = sock_wmalloc(sk,
1385 alloclen + hh_len, 1, 1388 alloclen + hh_len, 1,
1386 sk->sk_allocation); 1389 sk->sk_allocation);
1387 if (unlikely(skb == NULL)) 1390 if (unlikely(!skb))
1388 err = -ENOBUFS; 1391 err = -ENOBUFS;
1389 } 1392 }
1390 if (skb == NULL) 1393 if (!skb)
1391 goto error; 1394 goto error;
1392 /* 1395 /*
1393 * Fill in the control structures 1396 * Fill in the control structures
@@ -1579,7 +1582,7 @@ struct sk_buff *__ip6_make_skb(struct sock *sk,
1579 unsigned char proto = fl6->flowi6_proto; 1582 unsigned char proto = fl6->flowi6_proto;
1580 1583
1581 skb = __skb_dequeue(queue); 1584 skb = __skb_dequeue(queue);
1582 if (skb == NULL) 1585 if (!skb)
1583 goto out; 1586 goto out;
1584 tail_skb = &(skb_shinfo(skb)->frag_list); 1587 tail_skb = &(skb_shinfo(skb)->frag_list);
1585 1588
diff --git a/net/ipv6/ip6_tunnel.c b/net/ipv6/ip6_tunnel.c
index ddd94eca19b3..5cafd92c2312 100644
--- a/net/ipv6/ip6_tunnel.c
+++ b/net/ipv6/ip6_tunnel.c
@@ -64,12 +64,6 @@ MODULE_LICENSE("GPL");
64MODULE_ALIAS_RTNL_LINK("ip6tnl"); 64MODULE_ALIAS_RTNL_LINK("ip6tnl");
65MODULE_ALIAS_NETDEV("ip6tnl0"); 65MODULE_ALIAS_NETDEV("ip6tnl0");
66 66
67#ifdef IP6_TNL_DEBUG
68#define IP6_TNL_TRACE(x...) pr_debug("%s:" x "\n", __func__)
69#else
70#define IP6_TNL_TRACE(x...) do {;} while(0)
71#endif
72
73#define HASH_SIZE_SHIFT 5 67#define HASH_SIZE_SHIFT 5
74#define HASH_SIZE (1 << HASH_SIZE_SHIFT) 68#define HASH_SIZE (1 << HASH_SIZE_SHIFT)
75 69
@@ -137,7 +131,7 @@ struct dst_entry *ip6_tnl_dst_check(struct ip6_tnl *t)
137 struct dst_entry *dst = t->dst_cache; 131 struct dst_entry *dst = t->dst_cache;
138 132
139 if (dst && dst->obsolete && 133 if (dst && dst->obsolete &&
140 dst->ops->check(dst, t->dst_cookie) == NULL) { 134 !dst->ops->check(dst, t->dst_cookie)) {
141 t->dst_cache = NULL; 135 t->dst_cache = NULL;
142 dst_release(dst); 136 dst_release(dst);
143 return NULL; 137 return NULL;
@@ -331,7 +325,7 @@ static struct ip6_tnl *ip6_tnl_create(struct net *net, struct __ip6_tnl_parm *p)
331 325
332 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, 326 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
333 ip6_tnl_dev_setup); 327 ip6_tnl_dev_setup);
334 if (dev == NULL) 328 if (!dev)
335 goto failed; 329 goto failed;
336 330
337 dev_net_set(dev, net); 331 dev_net_set(dev, net);
@@ -502,7 +496,7 @@ ip6_tnl_err(struct sk_buff *skb, __u8 ipproto, struct inet6_skb_parm *opt,
502 496
503 rcu_read_lock(); 497 rcu_read_lock();
504 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr); 498 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->daddr, &ipv6h->saddr);
505 if (t == NULL) 499 if (!t)
506 goto out; 500 goto out;
507 501
508 tproto = ACCESS_ONCE(t->parms.proto); 502 tproto = ACCESS_ONCE(t->parms.proto);
@@ -813,7 +807,7 @@ static int ip6_tnl_rcv(struct sk_buff *skb, __u16 protocol,
813 807
814 rcu_read_lock(); 808 rcu_read_lock();
815 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr); 809 t = ip6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
816 if (t != NULL) { 810 if (t) {
817 struct pcpu_sw_netstats *tstats; 811 struct pcpu_sw_netstats *tstats;
818 812
819 tproto = ACCESS_ONCE(t->parms.proto); 813 tproto = ACCESS_ONCE(t->parms.proto);
@@ -1106,7 +1100,7 @@ static int ip6_tnl_xmit2(struct sk_buff *skb,
1106 ipv6h->nexthdr = proto; 1100 ipv6h->nexthdr = proto;
1107 ipv6h->saddr = fl6->saddr; 1101 ipv6h->saddr = fl6->saddr;
1108 ipv6h->daddr = fl6->daddr; 1102 ipv6h->daddr = fl6->daddr;
1109 ip6tunnel_xmit(skb, dev); 1103 ip6tunnel_xmit(NULL, skb, dev);
1110 if (ndst) 1104 if (ndst)
1111 ip6_tnl_dst_store(t, ndst); 1105 ip6_tnl_dst_store(t, ndst);
1112 return 0; 1106 return 0;
@@ -1270,8 +1264,6 @@ static void ip6_tnl_link_config(struct ip6_tnl *t)
1270 else 1264 else
1271 dev->flags &= ~IFF_POINTOPOINT; 1265 dev->flags &= ~IFF_POINTOPOINT;
1272 1266
1273 dev->iflink = p->link;
1274
1275 if (p->flags & IP6_TNL_F_CAP_XMIT) { 1267 if (p->flags & IP6_TNL_F_CAP_XMIT) {
1276 int strict = (ipv6_addr_type(&p->raddr) & 1268 int strict = (ipv6_addr_type(&p->raddr) &
1277 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL)); 1269 (IPV6_ADDR_MULTICAST|IPV6_ADDR_LINKLOCAL));
@@ -1280,7 +1272,7 @@ static void ip6_tnl_link_config(struct ip6_tnl *t)
1280 &p->raddr, &p->laddr, 1272 &p->raddr, &p->laddr,
1281 p->link, strict); 1273 p->link, strict);
1282 1274
1283 if (rt == NULL) 1275 if (!rt)
1284 return; 1276 return;
1285 1277
1286 if (rt->dst.dev) { 1278 if (rt->dst.dev) {
@@ -1523,6 +1515,13 @@ ip6_tnl_change_mtu(struct net_device *dev, int new_mtu)
1523 return 0; 1515 return 0;
1524} 1516}
1525 1517
1518int ip6_tnl_get_iflink(const struct net_device *dev)
1519{
1520 struct ip6_tnl *t = netdev_priv(dev);
1521
1522 return t->parms.link;
1523}
1524EXPORT_SYMBOL(ip6_tnl_get_iflink);
1526 1525
1527static const struct net_device_ops ip6_tnl_netdev_ops = { 1526static const struct net_device_ops ip6_tnl_netdev_ops = {
1528 .ndo_init = ip6_tnl_dev_init, 1527 .ndo_init = ip6_tnl_dev_init,
@@ -1531,6 +1530,7 @@ static const struct net_device_ops ip6_tnl_netdev_ops = {
1531 .ndo_do_ioctl = ip6_tnl_ioctl, 1530 .ndo_do_ioctl = ip6_tnl_ioctl,
1532 .ndo_change_mtu = ip6_tnl_change_mtu, 1531 .ndo_change_mtu = ip6_tnl_change_mtu,
1533 .ndo_get_stats = ip6_get_stats, 1532 .ndo_get_stats = ip6_get_stats,
1533 .ndo_get_iflink = ip6_tnl_get_iflink,
1534}; 1534};
1535 1535
1536 1536
@@ -1646,12 +1646,10 @@ static void ip6_tnl_netlink_parms(struct nlattr *data[],
1646 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]); 1646 parms->link = nla_get_u32(data[IFLA_IPTUN_LINK]);
1647 1647
1648 if (data[IFLA_IPTUN_LOCAL]) 1648 if (data[IFLA_IPTUN_LOCAL])
1649 nla_memcpy(&parms->laddr, data[IFLA_IPTUN_LOCAL], 1649 parms->laddr = nla_get_in6_addr(data[IFLA_IPTUN_LOCAL]);
1650 sizeof(struct in6_addr));
1651 1650
1652 if (data[IFLA_IPTUN_REMOTE]) 1651 if (data[IFLA_IPTUN_REMOTE])
1653 nla_memcpy(&parms->raddr, data[IFLA_IPTUN_REMOTE], 1652 parms->raddr = nla_get_in6_addr(data[IFLA_IPTUN_REMOTE]);
1654 sizeof(struct in6_addr));
1655 1653
1656 if (data[IFLA_IPTUN_TTL]) 1654 if (data[IFLA_IPTUN_TTL])
1657 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]); 1655 parms->hop_limit = nla_get_u8(data[IFLA_IPTUN_TTL]);
@@ -1745,10 +1743,8 @@ static int ip6_tnl_fill_info(struct sk_buff *skb, const struct net_device *dev)
1745 struct __ip6_tnl_parm *parm = &tunnel->parms; 1743 struct __ip6_tnl_parm *parm = &tunnel->parms;
1746 1744
1747 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 1745 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1748 nla_put(skb, IFLA_IPTUN_LOCAL, sizeof(struct in6_addr), 1746 nla_put_in6_addr(skb, IFLA_IPTUN_LOCAL, &parm->laddr) ||
1749 &parm->laddr) || 1747 nla_put_in6_addr(skb, IFLA_IPTUN_REMOTE, &parm->raddr) ||
1750 nla_put(skb, IFLA_IPTUN_REMOTE, sizeof(struct in6_addr),
1751 &parm->raddr) ||
1752 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) || 1748 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->hop_limit) ||
1753 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) || 1749 nla_put_u8(skb, IFLA_IPTUN_ENCAP_LIMIT, parm->encap_limit) ||
1754 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) || 1750 nla_put_be32(skb, IFLA_IPTUN_FLOWINFO, parm->flowinfo) ||
@@ -1821,7 +1817,7 @@ static void __net_exit ip6_tnl_destroy_tunnels(struct net *net)
1821 1817
1822 for (h = 0; h < HASH_SIZE; h++) { 1818 for (h = 0; h < HASH_SIZE; h++) {
1823 t = rtnl_dereference(ip6n->tnls_r_l[h]); 1819 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1824 while (t != NULL) { 1820 while (t) {
1825 /* If dev is in the same netns, it has already 1821 /* If dev is in the same netns, it has already
1826 * been added to the list by the previous loop. 1822 * been added to the list by the previous loop.
1827 */ 1823 */
diff --git a/net/ipv6/ip6_udp_tunnel.c b/net/ipv6/ip6_udp_tunnel.c
index 32d9b268e7d8..bba8903e871f 100644
--- a/net/ipv6/ip6_udp_tunnel.c
+++ b/net/ipv6/ip6_udp_tunnel.c
@@ -62,7 +62,8 @@ error:
62} 62}
63EXPORT_SYMBOL_GPL(udp_sock_create6); 63EXPORT_SYMBOL_GPL(udp_sock_create6);
64 64
65int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb, 65int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sock *sk,
66 struct sk_buff *skb,
66 struct net_device *dev, struct in6_addr *saddr, 67 struct net_device *dev, struct in6_addr *saddr,
67 struct in6_addr *daddr, 68 struct in6_addr *daddr,
68 __u8 prio, __u8 ttl, __be16 src_port, 69 __u8 prio, __u8 ttl, __be16 src_port,
@@ -97,7 +98,7 @@ int udp_tunnel6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
97 ip6h->daddr = *daddr; 98 ip6h->daddr = *daddr;
98 ip6h->saddr = *saddr; 99 ip6h->saddr = *saddr;
99 100
100 ip6tunnel_xmit(skb, dev); 101 ip6tunnel_xmit(sk, skb, dev);
101 return 0; 102 return 0;
102} 103}
103EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb); 104EXPORT_SYMBOL_GPL(udp_tunnel6_xmit_skb);
diff --git a/net/ipv6/ip6_vti.c b/net/ipv6/ip6_vti.c
index 5fb9e212eca8..ed9d681207fa 100644
--- a/net/ipv6/ip6_vti.c
+++ b/net/ipv6/ip6_vti.c
@@ -218,7 +218,7 @@ static struct ip6_tnl *vti6_tnl_create(struct net *net, struct __ip6_tnl_parm *p
218 sprintf(name, "ip6_vti%%d"); 218 sprintf(name, "ip6_vti%%d");
219 219
220 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup); 220 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, vti6_dev_setup);
221 if (dev == NULL) 221 if (!dev)
222 goto failed; 222 goto failed;
223 223
224 dev_net_set(dev, net); 224 dev_net_set(dev, net);
@@ -288,8 +288,7 @@ static struct ip6_tnl *vti6_locate(struct net *net, struct __ip6_tnl_parm *p,
288static void vti6_dev_uninit(struct net_device *dev) 288static void vti6_dev_uninit(struct net_device *dev)
289{ 289{
290 struct ip6_tnl *t = netdev_priv(dev); 290 struct ip6_tnl *t = netdev_priv(dev);
291 struct net *net = dev_net(dev); 291 struct vti6_net *ip6n = net_generic(t->net, vti6_net_id);
292 struct vti6_net *ip6n = net_generic(net, vti6_net_id);
293 292
294 if (dev == ip6n->fb_tnl_dev) 293 if (dev == ip6n->fb_tnl_dev)
295 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL); 294 RCU_INIT_POINTER(ip6n->tnls_wc[0], NULL);
@@ -305,7 +304,7 @@ static int vti6_rcv(struct sk_buff *skb)
305 304
306 rcu_read_lock(); 305 rcu_read_lock();
307 t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr); 306 t = vti6_tnl_lookup(dev_net(skb->dev), &ipv6h->saddr, &ipv6h->daddr);
308 if (t != NULL) { 307 if (t) {
309 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) { 308 if (t->parms.proto != IPPROTO_IPV6 && t->parms.proto != 0) {
310 rcu_read_unlock(); 309 rcu_read_unlock();
311 goto discard; 310 goto discard;
@@ -601,8 +600,6 @@ static void vti6_link_config(struct ip6_tnl *t)
601 dev->flags |= IFF_POINTOPOINT; 600 dev->flags |= IFF_POINTOPOINT;
602 else 601 else
603 dev->flags &= ~IFF_POINTOPOINT; 602 dev->flags &= ~IFF_POINTOPOINT;
604
605 dev->iflink = p->link;
606} 603}
607 604
608/** 605/**
@@ -716,7 +713,7 @@ vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
716 } else { 713 } else {
717 memset(&p, 0, sizeof(p)); 714 memset(&p, 0, sizeof(p));
718 } 715 }
719 if (t == NULL) 716 if (!t)
720 t = netdev_priv(dev); 717 t = netdev_priv(dev);
721 vti6_parm_to_user(&p, &t->parms); 718 vti6_parm_to_user(&p, &t->parms);
722 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p))) 719 if (copy_to_user(ifr->ifr_ifru.ifru_data, &p, sizeof(p)))
@@ -736,7 +733,7 @@ vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
736 vti6_parm_from_user(&p1, &p); 733 vti6_parm_from_user(&p1, &p);
737 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL); 734 t = vti6_locate(net, &p1, cmd == SIOCADDTUNNEL);
738 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) { 735 if (dev != ip6n->fb_tnl_dev && cmd == SIOCCHGTUNNEL) {
739 if (t != NULL) { 736 if (t) {
740 if (t->dev != dev) { 737 if (t->dev != dev) {
741 err = -EEXIST; 738 err = -EEXIST;
742 break; 739 break;
@@ -767,7 +764,7 @@ vti6_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
767 err = -ENOENT; 764 err = -ENOENT;
768 vti6_parm_from_user(&p1, &p); 765 vti6_parm_from_user(&p1, &p);
769 t = vti6_locate(net, &p1, 0); 766 t = vti6_locate(net, &p1, 0);
770 if (t == NULL) 767 if (!t)
771 break; 768 break;
772 err = -EPERM; 769 err = -EPERM;
773 if (t->dev == ip6n->fb_tnl_dev) 770 if (t->dev == ip6n->fb_tnl_dev)
@@ -808,6 +805,7 @@ static const struct net_device_ops vti6_netdev_ops = {
808 .ndo_do_ioctl = vti6_ioctl, 805 .ndo_do_ioctl = vti6_ioctl,
809 .ndo_change_mtu = vti6_change_mtu, 806 .ndo_change_mtu = vti6_change_mtu,
810 .ndo_get_stats64 = ip_tunnel_get_stats64, 807 .ndo_get_stats64 = ip_tunnel_get_stats64,
808 .ndo_get_iflink = ip6_tnl_get_iflink,
811}; 809};
812 810
813/** 811/**
@@ -897,12 +895,10 @@ static void vti6_netlink_parms(struct nlattr *data[],
897 parms->link = nla_get_u32(data[IFLA_VTI_LINK]); 895 parms->link = nla_get_u32(data[IFLA_VTI_LINK]);
898 896
899 if (data[IFLA_VTI_LOCAL]) 897 if (data[IFLA_VTI_LOCAL])
900 nla_memcpy(&parms->laddr, data[IFLA_VTI_LOCAL], 898 parms->laddr = nla_get_in6_addr(data[IFLA_VTI_LOCAL]);
901 sizeof(struct in6_addr));
902 899
903 if (data[IFLA_VTI_REMOTE]) 900 if (data[IFLA_VTI_REMOTE])
904 nla_memcpy(&parms->raddr, data[IFLA_VTI_REMOTE], 901 parms->raddr = nla_get_in6_addr(data[IFLA_VTI_REMOTE]);
905 sizeof(struct in6_addr));
906 902
907 if (data[IFLA_VTI_IKEY]) 903 if (data[IFLA_VTI_IKEY])
908 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]); 904 parms->i_key = nla_get_be32(data[IFLA_VTI_IKEY]);
@@ -983,10 +979,8 @@ static int vti6_fill_info(struct sk_buff *skb, const struct net_device *dev)
983 struct __ip6_tnl_parm *parm = &tunnel->parms; 979 struct __ip6_tnl_parm *parm = &tunnel->parms;
984 980
985 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) || 981 if (nla_put_u32(skb, IFLA_VTI_LINK, parm->link) ||
986 nla_put(skb, IFLA_VTI_LOCAL, sizeof(struct in6_addr), 982 nla_put_in6_addr(skb, IFLA_VTI_LOCAL, &parm->laddr) ||
987 &parm->laddr) || 983 nla_put_in6_addr(skb, IFLA_VTI_REMOTE, &parm->raddr) ||
988 nla_put(skb, IFLA_VTI_REMOTE, sizeof(struct in6_addr),
989 &parm->raddr) ||
990 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) || 984 nla_put_be32(skb, IFLA_VTI_IKEY, parm->i_key) ||
991 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key)) 985 nla_put_be32(skb, IFLA_VTI_OKEY, parm->o_key))
992 goto nla_put_failure; 986 goto nla_put_failure;
@@ -1027,7 +1021,7 @@ static void __net_exit vti6_destroy_tunnels(struct vti6_net *ip6n)
1027 1021
1028 for (h = 0; h < HASH_SIZE; h++) { 1022 for (h = 0; h < HASH_SIZE; h++) {
1029 t = rtnl_dereference(ip6n->tnls_r_l[h]); 1023 t = rtnl_dereference(ip6n->tnls_r_l[h]);
1030 while (t != NULL) { 1024 while (t) {
1031 unregister_netdevice_queue(t->dev, &list); 1025 unregister_netdevice_queue(t->dev, &list);
1032 t = rtnl_dereference(t->next); 1026 t = rtnl_dereference(t->next);
1033 } 1027 }
diff --git a/net/ipv6/ip6mr.c b/net/ipv6/ip6mr.c
index 312e0ff47339..74ceb73c1c9a 100644
--- a/net/ipv6/ip6mr.c
+++ b/net/ipv6/ip6mr.c
@@ -56,9 +56,7 @@
56 56
57struct mr6_table { 57struct mr6_table {
58 struct list_head list; 58 struct list_head list;
59#ifdef CONFIG_NET_NS 59 possible_net_t net;
60 struct net *net;
61#endif
62 u32 id; 60 u32 id;
63 struct sock *mroute6_sk; 61 struct sock *mroute6_sk;
64 struct timer_list ipmr_expire_timer; 62 struct timer_list ipmr_expire_timer;
@@ -175,7 +173,7 @@ static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
175 } 173 }
176 174
177 mrt = ip6mr_get_table(rule->fr_net, rule->table); 175 mrt = ip6mr_get_table(rule->fr_net, rule->table);
178 if (mrt == NULL) 176 if (!mrt)
179 return -EAGAIN; 177 return -EAGAIN;
180 res->mrt = mrt; 178 res->mrt = mrt;
181 return 0; 179 return 0;
@@ -239,7 +237,7 @@ static int __net_init ip6mr_rules_init(struct net *net)
239 INIT_LIST_HEAD(&net->ipv6.mr6_tables); 237 INIT_LIST_HEAD(&net->ipv6.mr6_tables);
240 238
241 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT); 239 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
242 if (mrt == NULL) { 240 if (!mrt) {
243 err = -ENOMEM; 241 err = -ENOMEM;
244 goto err1; 242 goto err1;
245 } 243 }
@@ -307,11 +305,11 @@ static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
307 unsigned int i; 305 unsigned int i;
308 306
309 mrt = ip6mr_get_table(net, id); 307 mrt = ip6mr_get_table(net, id);
310 if (mrt != NULL) 308 if (mrt)
311 return mrt; 309 return mrt;
312 310
313 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); 311 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
314 if (mrt == NULL) 312 if (!mrt)
315 return NULL; 313 return NULL;
316 mrt->id = id; 314 mrt->id = id;
317 write_pnet(&mrt->net, net); 315 write_pnet(&mrt->net, net);
@@ -410,7 +408,7 @@ static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
410 struct mr6_table *mrt; 408 struct mr6_table *mrt;
411 409
412 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); 410 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
413 if (mrt == NULL) 411 if (!mrt)
414 return ERR_PTR(-ENOENT); 412 return ERR_PTR(-ENOENT);
415 413
416 iter->mrt = mrt; 414 iter->mrt = mrt;
@@ -494,7 +492,7 @@ static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
494 struct mr6_table *mrt; 492 struct mr6_table *mrt;
495 493
496 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); 494 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
497 if (mrt == NULL) 495 if (!mrt)
498 return ERR_PTR(-ENOENT); 496 return ERR_PTR(-ENOENT);
499 497
500 it->mrt = mrt; 498 it->mrt = mrt;
@@ -667,7 +665,7 @@ static int pim6_rcv(struct sk_buff *skb)
667 dev_hold(reg_dev); 665 dev_hold(reg_dev);
668 read_unlock(&mrt_lock); 666 read_unlock(&mrt_lock);
669 667
670 if (reg_dev == NULL) 668 if (!reg_dev)
671 goto drop; 669 goto drop;
672 670
673 skb->mac_header = skb->network_header; 671 skb->mac_header = skb->network_header;
@@ -720,8 +718,14 @@ static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
720 return NETDEV_TX_OK; 718 return NETDEV_TX_OK;
721} 719}
722 720
721static int reg_vif_get_iflink(const struct net_device *dev)
722{
723 return 0;
724}
725
723static const struct net_device_ops reg_vif_netdev_ops = { 726static const struct net_device_ops reg_vif_netdev_ops = {
724 .ndo_start_xmit = reg_vif_xmit, 727 .ndo_start_xmit = reg_vif_xmit,
728 .ndo_get_iflink = reg_vif_get_iflink,
725}; 729};
726 730
727static void reg_vif_setup(struct net_device *dev) 731static void reg_vif_setup(struct net_device *dev)
@@ -745,7 +749,7 @@ static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
745 sprintf(name, "pim6reg%u", mrt->id); 749 sprintf(name, "pim6reg%u", mrt->id);
746 750
747 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); 751 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
748 if (dev == NULL) 752 if (!dev)
749 return NULL; 753 return NULL;
750 754
751 dev_net_set(dev, net); 755 dev_net_set(dev, net);
@@ -754,7 +758,6 @@ static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
754 free_netdev(dev); 758 free_netdev(dev);
755 return NULL; 759 return NULL;
756 } 760 }
757 dev->iflink = 0;
758 761
759 if (dev_open(dev)) 762 if (dev_open(dev))
760 goto failure; 763 goto failure;
@@ -994,7 +997,7 @@ static int mif6_add(struct net *net, struct mr6_table *mrt,
994 v->pkt_out = 0; 997 v->pkt_out = 0;
995 v->link = dev->ifindex; 998 v->link = dev->ifindex;
996 if (v->flags & MIFF_REGISTER) 999 if (v->flags & MIFF_REGISTER)
997 v->link = dev->iflink; 1000 v->link = dev_get_iflink(dev);
998 1001
999 /* And finish update writing critical data */ 1002 /* And finish update writing critical data */
1000 write_lock_bh(&mrt_lock); 1003 write_lock_bh(&mrt_lock);
@@ -1074,7 +1077,7 @@ skip:
1074static struct mfc6_cache *ip6mr_cache_alloc(void) 1077static struct mfc6_cache *ip6mr_cache_alloc(void)
1075{ 1078{
1076 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 1079 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
1077 if (c == NULL) 1080 if (!c)
1078 return NULL; 1081 return NULL;
1079 c->mfc_un.res.minvif = MAXMIFS; 1082 c->mfc_un.res.minvif = MAXMIFS;
1080 return c; 1083 return c;
@@ -1083,7 +1086,7 @@ static struct mfc6_cache *ip6mr_cache_alloc(void)
1083static struct mfc6_cache *ip6mr_cache_alloc_unres(void) 1086static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
1084{ 1087{
1085 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 1088 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1086 if (c == NULL) 1089 if (!c)
1087 return NULL; 1090 return NULL;
1088 skb_queue_head_init(&c->mfc_un.unres.unresolved); 1091 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1089 c->mfc_un.unres.expires = jiffies + 10 * HZ; 1092 c->mfc_un.unres.expires = jiffies + 10 * HZ;
@@ -1200,7 +1203,7 @@ static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
1200 skb->ip_summed = CHECKSUM_UNNECESSARY; 1203 skb->ip_summed = CHECKSUM_UNNECESSARY;
1201 } 1204 }
1202 1205
1203 if (mrt->mroute6_sk == NULL) { 1206 if (!mrt->mroute6_sk) {
1204 kfree_skb(skb); 1207 kfree_skb(skb);
1205 return -EINVAL; 1208 return -EINVAL;
1206 } 1209 }
@@ -1495,7 +1498,7 @@ static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
1495 return -EINVAL; 1498 return -EINVAL;
1496 1499
1497 c = ip6mr_cache_alloc(); 1500 c = ip6mr_cache_alloc();
1498 if (c == NULL) 1501 if (!c)
1499 return -ENOMEM; 1502 return -ENOMEM;
1500 1503
1501 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr; 1504 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
@@ -1665,7 +1668,7 @@ int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, uns
1665 struct mr6_table *mrt; 1668 struct mr6_table *mrt;
1666 1669
1667 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); 1670 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1668 if (mrt == NULL) 1671 if (!mrt)
1669 return -ENOENT; 1672 return -ENOENT;
1670 1673
1671 if (optname != MRT6_INIT) { 1674 if (optname != MRT6_INIT) {
@@ -1814,7 +1817,7 @@ int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1814 struct mr6_table *mrt; 1817 struct mr6_table *mrt;
1815 1818
1816 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); 1819 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1817 if (mrt == NULL) 1820 if (!mrt)
1818 return -ENOENT; 1821 return -ENOENT;
1819 1822
1820 switch (optname) { 1823 switch (optname) {
@@ -1861,7 +1864,7 @@ int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1861 struct mr6_table *mrt; 1864 struct mr6_table *mrt;
1862 1865
1863 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); 1866 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1864 if (mrt == NULL) 1867 if (!mrt)
1865 return -ENOENT; 1868 return -ENOENT;
1866 1869
1867 switch (cmd) { 1870 switch (cmd) {
@@ -1935,7 +1938,7 @@ int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1935 struct mr6_table *mrt; 1938 struct mr6_table *mrt;
1936 1939
1937 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); 1940 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1938 if (mrt == NULL) 1941 if (!mrt)
1939 return -ENOENT; 1942 return -ENOENT;
1940 1943
1941 switch (cmd) { 1944 switch (cmd) {
@@ -1983,13 +1986,13 @@ int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1983} 1986}
1984#endif 1987#endif
1985 1988
1986static inline int ip6mr_forward2_finish(struct sk_buff *skb) 1989static inline int ip6mr_forward2_finish(struct sock *sk, struct sk_buff *skb)
1987{ 1990{
1988 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)), 1991 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1989 IPSTATS_MIB_OUTFORWDATAGRAMS); 1992 IPSTATS_MIB_OUTFORWDATAGRAMS);
1990 IP6_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)), 1993 IP6_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1991 IPSTATS_MIB_OUTOCTETS, skb->len); 1994 IPSTATS_MIB_OUTOCTETS, skb->len);
1992 return dst_output(skb); 1995 return dst_output_sk(sk, skb);
1993} 1996}
1994 1997
1995/* 1998/*
@@ -2005,7 +2008,7 @@ static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
2005 struct dst_entry *dst; 2008 struct dst_entry *dst;
2006 struct flowi6 fl6; 2009 struct flowi6 fl6;
2007 2010
2008 if (vif->dev == NULL) 2011 if (!vif->dev)
2009 goto out_free; 2012 goto out_free;
2010 2013
2011#ifdef CONFIG_IPV6_PIMSM_V2 2014#ifdef CONFIG_IPV6_PIMSM_V2
@@ -2061,7 +2064,8 @@ static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
2061 2064
2062 IP6CB(skb)->flags |= IP6SKB_FORWARDED; 2065 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
2063 2066
2064 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev, 2067 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, NULL, skb,
2068 skb->dev, dev,
2065 ip6mr_forward2_finish); 2069 ip6mr_forward2_finish);
2066 2070
2067out_free: 2071out_free:
@@ -2194,7 +2198,7 @@ int ip6_mr_input(struct sk_buff *skb)
2194 read_lock(&mrt_lock); 2198 read_lock(&mrt_lock);
2195 cache = ip6mr_cache_find(mrt, 2199 cache = ip6mr_cache_find(mrt,
2196 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr); 2200 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
2197 if (cache == NULL) { 2201 if (!cache) {
2198 int vif = ip6mr_find_vif(mrt, skb->dev); 2202 int vif = ip6mr_find_vif(mrt, skb->dev);
2199 2203
2200 if (vif >= 0) 2204 if (vif >= 0)
@@ -2206,7 +2210,7 @@ int ip6_mr_input(struct sk_buff *skb)
2206 /* 2210 /*
2207 * No usable cache entry 2211 * No usable cache entry
2208 */ 2212 */
2209 if (cache == NULL) { 2213 if (!cache) {
2210 int vif; 2214 int vif;
2211 2215
2212 vif = ip6mr_find_vif(mrt, skb->dev); 2216 vif = ip6mr_find_vif(mrt, skb->dev);
@@ -2245,13 +2249,13 @@ static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2245 nla_put_u32(skb, RTA_IIF, mrt->vif6_table[c->mf6c_parent].dev->ifindex) < 0) 2249 nla_put_u32(skb, RTA_IIF, mrt->vif6_table[c->mf6c_parent].dev->ifindex) < 0)
2246 return -EMSGSIZE; 2250 return -EMSGSIZE;
2247 mp_attr = nla_nest_start(skb, RTA_MULTIPATH); 2251 mp_attr = nla_nest_start(skb, RTA_MULTIPATH);
2248 if (mp_attr == NULL) 2252 if (!mp_attr)
2249 return -EMSGSIZE; 2253 return -EMSGSIZE;
2250 2254
2251 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { 2255 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2252 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { 2256 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2253 nhp = nla_reserve_nohdr(skb, sizeof(*nhp)); 2257 nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
2254 if (nhp == NULL) { 2258 if (!nhp) {
2255 nla_nest_cancel(skb, mp_attr); 2259 nla_nest_cancel(skb, mp_attr);
2256 return -EMSGSIZE; 2260 return -EMSGSIZE;
2257 } 2261 }
@@ -2284,7 +2288,7 @@ int ip6mr_get_route(struct net *net,
2284 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); 2288 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
2285 2289
2286 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); 2290 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2287 if (mrt == NULL) 2291 if (!mrt)
2288 return -ENOENT; 2292 return -ENOENT;
2289 2293
2290 read_lock(&mrt_lock); 2294 read_lock(&mrt_lock);
@@ -2309,7 +2313,7 @@ int ip6mr_get_route(struct net *net,
2309 } 2313 }
2310 2314
2311 dev = skb->dev; 2315 dev = skb->dev;
2312 if (dev == NULL || (vif = ip6mr_find_vif(mrt, dev)) < 0) { 2316 if (!dev || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
2313 read_unlock(&mrt_lock); 2317 read_unlock(&mrt_lock);
2314 return -ENODEV; 2318 return -ENODEV;
2315 } 2319 }
@@ -2361,7 +2365,7 @@ static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2361 int err; 2365 int err;
2362 2366
2363 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 2367 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2364 if (nlh == NULL) 2368 if (!nlh)
2365 return -EMSGSIZE; 2369 return -EMSGSIZE;
2366 2370
2367 rtm = nlmsg_data(nlh); 2371 rtm = nlmsg_data(nlh);
@@ -2380,8 +2384,8 @@ static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2380 rtm->rtm_protocol = RTPROT_MROUTED; 2384 rtm->rtm_protocol = RTPROT_MROUTED;
2381 rtm->rtm_flags = 0; 2385 rtm->rtm_flags = 0;
2382 2386
2383 if (nla_put(skb, RTA_SRC, 16, &c->mf6c_origin) || 2387 if (nla_put_in6_addr(skb, RTA_SRC, &c->mf6c_origin) ||
2384 nla_put(skb, RTA_DST, 16, &c->mf6c_mcastgrp)) 2388 nla_put_in6_addr(skb, RTA_DST, &c->mf6c_mcastgrp))
2385 goto nla_put_failure; 2389 goto nla_put_failure;
2386 err = __ip6mr_fill_mroute(mrt, skb, c, rtm); 2390 err = __ip6mr_fill_mroute(mrt, skb, c, rtm);
2387 /* do not break the dump if cache is unresolved */ 2391 /* do not break the dump if cache is unresolved */
@@ -2426,7 +2430,7 @@ static void mr6_netlink_event(struct mr6_table *mrt, struct mfc6_cache *mfc,
2426 2430
2427 skb = nlmsg_new(mr6_msgsize(mfc->mf6c_parent >= MAXMIFS, mrt->maxvif), 2431 skb = nlmsg_new(mr6_msgsize(mfc->mf6c_parent >= MAXMIFS, mrt->maxvif),
2428 GFP_ATOMIC); 2432 GFP_ATOMIC);
2429 if (skb == NULL) 2433 if (!skb)
2430 goto errout; 2434 goto errout;
2431 2435
2432 err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 2436 err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
diff --git a/net/ipv6/ipv6_sockglue.c b/net/ipv6/ipv6_sockglue.c
index 8d766d9100cb..63e6956917c9 100644
--- a/net/ipv6/ipv6_sockglue.c
+++ b/net/ipv6/ipv6_sockglue.c
@@ -85,7 +85,7 @@ int ip6_ra_control(struct sock *sk, int sel)
85 return 0; 85 return 0;
86 } 86 }
87 } 87 }
88 if (new_ra == NULL) { 88 if (!new_ra) {
89 write_unlock_bh(&ip6_ra_lock); 89 write_unlock_bh(&ip6_ra_lock);
90 return -ENOBUFS; 90 return -ENOBUFS;
91 } 91 }
@@ -117,6 +117,25 @@ struct ipv6_txoptions *ipv6_update_options(struct sock *sk,
117 return opt; 117 return opt;
118} 118}
119 119
120static bool setsockopt_needs_rtnl(int optname)
121{
122 switch (optname) {
123 case IPV6_ADD_MEMBERSHIP:
124 case IPV6_DROP_MEMBERSHIP:
125 case IPV6_JOIN_ANYCAST:
126 case IPV6_LEAVE_ANYCAST:
127 case MCAST_JOIN_GROUP:
128 case MCAST_LEAVE_GROUP:
129 case MCAST_JOIN_SOURCE_GROUP:
130 case MCAST_LEAVE_SOURCE_GROUP:
131 case MCAST_BLOCK_SOURCE:
132 case MCAST_UNBLOCK_SOURCE:
133 case MCAST_MSFILTER:
134 return true;
135 }
136 return false;
137}
138
120static int do_ipv6_setsockopt(struct sock *sk, int level, int optname, 139static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
121 char __user *optval, unsigned int optlen) 140 char __user *optval, unsigned int optlen)
122{ 141{
@@ -124,8 +143,9 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
124 struct net *net = sock_net(sk); 143 struct net *net = sock_net(sk);
125 int val, valbool; 144 int val, valbool;
126 int retv = -ENOPROTOOPT; 145 int retv = -ENOPROTOOPT;
146 bool needs_rtnl = setsockopt_needs_rtnl(optname);
127 147
128 if (optval == NULL) 148 if (!optval)
129 val = 0; 149 val = 0;
130 else { 150 else {
131 if (optlen >= sizeof(int)) { 151 if (optlen >= sizeof(int)) {
@@ -140,6 +160,8 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
140 if (ip6_mroute_opt(optname)) 160 if (ip6_mroute_opt(optname))
141 return ip6_mroute_setsockopt(sk, optname, optval, optlen); 161 return ip6_mroute_setsockopt(sk, optname, optval, optlen);
142 162
163 if (needs_rtnl)
164 rtnl_lock();
143 lock_sock(sk); 165 lock_sock(sk);
144 166
145 switch (optname) { 167 switch (optname) {
@@ -370,7 +392,7 @@ static int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
370 */ 392 */
371 if (optlen == 0) 393 if (optlen == 0)
372 optval = NULL; 394 optval = NULL;
373 else if (optval == NULL) 395 else if (!optval)
374 goto e_inval; 396 goto e_inval;
375 else if (optlen < sizeof(struct ipv6_opt_hdr) || 397 else if (optlen < sizeof(struct ipv6_opt_hdr) ||
376 optlen & 0x7 || optlen > 8 * 255) 398 optlen & 0x7 || optlen > 8 * 255)
@@ -421,7 +443,7 @@ sticky_done:
421 443
422 if (optlen == 0) 444 if (optlen == 0)
423 goto e_inval; 445 goto e_inval;
424 else if (optlen < sizeof(struct in6_pktinfo) || optval == NULL) 446 else if (optlen < sizeof(struct in6_pktinfo) || !optval)
425 goto e_inval; 447 goto e_inval;
426 448
427 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) { 449 if (copy_from_user(&pkt, optval, sizeof(struct in6_pktinfo))) {
@@ -460,7 +482,7 @@ sticky_done:
460 482
461 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL); 483 opt = sock_kmalloc(sk, sizeof(*opt) + optlen, GFP_KERNEL);
462 retv = -ENOBUFS; 484 retv = -ENOBUFS;
463 if (opt == NULL) 485 if (!opt)
464 break; 486 break;
465 487
466 memset(opt, 0, sizeof(*opt)); 488 memset(opt, 0, sizeof(*opt));
@@ -624,10 +646,10 @@ done:
624 psin6 = (struct sockaddr_in6 *)&greq.gr_group; 646 psin6 = (struct sockaddr_in6 *)&greq.gr_group;
625 if (optname == MCAST_JOIN_GROUP) 647 if (optname == MCAST_JOIN_GROUP)
626 retv = ipv6_sock_mc_join(sk, greq.gr_interface, 648 retv = ipv6_sock_mc_join(sk, greq.gr_interface,
627 &psin6->sin6_addr); 649 &psin6->sin6_addr);
628 else 650 else
629 retv = ipv6_sock_mc_drop(sk, greq.gr_interface, 651 retv = ipv6_sock_mc_drop(sk, greq.gr_interface,
630 &psin6->sin6_addr); 652 &psin6->sin6_addr);
631 break; 653 break;
632 } 654 }
633 case MCAST_JOIN_SOURCE_GROUP: 655 case MCAST_JOIN_SOURCE_GROUP:
@@ -660,7 +682,7 @@ done:
660 682
661 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group; 683 psin6 = (struct sockaddr_in6 *)&greqs.gsr_group;
662 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface, 684 retv = ipv6_sock_mc_join(sk, greqs.gsr_interface,
663 &psin6->sin6_addr); 685 &psin6->sin6_addr);
664 /* prior join w/ different source is ok */ 686 /* prior join w/ different source is ok */
665 if (retv && retv != -EADDRINUSE) 687 if (retv && retv != -EADDRINUSE)
666 break; 688 break;
@@ -837,11 +859,15 @@ pref_skip_coa:
837 } 859 }
838 860
839 release_sock(sk); 861 release_sock(sk);
862 if (needs_rtnl)
863 rtnl_unlock();
840 864
841 return retv; 865 return retv;
842 866
843e_inval: 867e_inval:
844 release_sock(sk); 868 release_sock(sk);
869 if (needs_rtnl)
870 rtnl_unlock();
845 return -EINVAL; 871 return -EINVAL;
846} 872}
847 873
diff --git a/net/ipv6/mcast.c b/net/ipv6/mcast.c
index 5ce107c8aab3..083b2927fc67 100644
--- a/net/ipv6/mcast.c
+++ b/net/ipv6/mcast.c
@@ -140,6 +140,8 @@ int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
140 struct net *net = sock_net(sk); 140 struct net *net = sock_net(sk);
141 int err; 141 int err;
142 142
143 ASSERT_RTNL();
144
143 if (!ipv6_addr_is_multicast(addr)) 145 if (!ipv6_addr_is_multicast(addr))
144 return -EINVAL; 146 return -EINVAL;
145 147
@@ -155,13 +157,12 @@ int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
155 157
156 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL); 158 mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL);
157 159
158 if (mc_lst == NULL) 160 if (!mc_lst)
159 return -ENOMEM; 161 return -ENOMEM;
160 162
161 mc_lst->next = NULL; 163 mc_lst->next = NULL;
162 mc_lst->addr = *addr; 164 mc_lst->addr = *addr;
163 165
164 rtnl_lock();
165 if (ifindex == 0) { 166 if (ifindex == 0) {
166 struct rt6_info *rt; 167 struct rt6_info *rt;
167 rt = rt6_lookup(net, addr, NULL, 0, 0); 168 rt = rt6_lookup(net, addr, NULL, 0, 0);
@@ -172,8 +173,7 @@ int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
172 } else 173 } else
173 dev = __dev_get_by_index(net, ifindex); 174 dev = __dev_get_by_index(net, ifindex);
174 175
175 if (dev == NULL) { 176 if (!dev) {
176 rtnl_unlock();
177 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 177 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
178 return -ENODEV; 178 return -ENODEV;
179 } 179 }
@@ -190,7 +190,6 @@ int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
190 err = ipv6_dev_mc_inc(dev, addr); 190 err = ipv6_dev_mc_inc(dev, addr);
191 191
192 if (err) { 192 if (err) {
193 rtnl_unlock();
194 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); 193 sock_kfree_s(sk, mc_lst, sizeof(*mc_lst));
195 return err; 194 return err;
196 } 195 }
@@ -198,10 +197,9 @@ int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr)
198 mc_lst->next = np->ipv6_mc_list; 197 mc_lst->next = np->ipv6_mc_list;
199 rcu_assign_pointer(np->ipv6_mc_list, mc_lst); 198 rcu_assign_pointer(np->ipv6_mc_list, mc_lst);
200 199
201 rtnl_unlock();
202
203 return 0; 200 return 0;
204} 201}
202EXPORT_SYMBOL(ipv6_sock_mc_join);
205 203
206/* 204/*
207 * socket leave on multicast group 205 * socket leave on multicast group
@@ -213,10 +211,11 @@ int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
213 struct ipv6_mc_socklist __rcu **lnk; 211 struct ipv6_mc_socklist __rcu **lnk;
214 struct net *net = sock_net(sk); 212 struct net *net = sock_net(sk);
215 213
214 ASSERT_RTNL();
215
216 if (!ipv6_addr_is_multicast(addr)) 216 if (!ipv6_addr_is_multicast(addr))
217 return -EINVAL; 217 return -EINVAL;
218 218
219 rtnl_lock();
220 for (lnk = &np->ipv6_mc_list; 219 for (lnk = &np->ipv6_mc_list;
221 (mc_lst = rtnl_dereference(*lnk)) != NULL; 220 (mc_lst = rtnl_dereference(*lnk)) != NULL;
222 lnk = &mc_lst->next) { 221 lnk = &mc_lst->next) {
@@ -227,7 +226,7 @@ int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
227 *lnk = mc_lst->next; 226 *lnk = mc_lst->next;
228 227
229 dev = __dev_get_by_index(net, mc_lst->ifindex); 228 dev = __dev_get_by_index(net, mc_lst->ifindex);
230 if (dev != NULL) { 229 if (dev) {
231 struct inet6_dev *idev = __in6_dev_get(dev); 230 struct inet6_dev *idev = __in6_dev_get(dev);
232 231
233 (void) ip6_mc_leave_src(sk, mc_lst, idev); 232 (void) ip6_mc_leave_src(sk, mc_lst, idev);
@@ -235,17 +234,16 @@ int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr)
235 __ipv6_dev_mc_dec(idev, &mc_lst->addr); 234 __ipv6_dev_mc_dec(idev, &mc_lst->addr);
236 } else 235 } else
237 (void) ip6_mc_leave_src(sk, mc_lst, NULL); 236 (void) ip6_mc_leave_src(sk, mc_lst, NULL);
238 rtnl_unlock();
239 237
240 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc); 238 atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc);
241 kfree_rcu(mc_lst, rcu); 239 kfree_rcu(mc_lst, rcu);
242 return 0; 240 return 0;
243 } 241 }
244 } 242 }
245 rtnl_unlock();
246 243
247 return -EADDRNOTAVAIL; 244 return -EADDRNOTAVAIL;
248} 245}
246EXPORT_SYMBOL(ipv6_sock_mc_drop);
249 247
250/* called with rcu_read_lock() */ 248/* called with rcu_read_lock() */
251static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net, 249static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net,
@@ -438,7 +436,7 @@ done:
438 read_unlock_bh(&idev->lock); 436 read_unlock_bh(&idev->lock);
439 rcu_read_unlock(); 437 rcu_read_unlock();
440 if (leavegroup) 438 if (leavegroup)
441 return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group); 439 err = ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group);
442 return err; 440 return err;
443} 441}
444 442
@@ -825,7 +823,7 @@ static struct ifmcaddr6 *mca_alloc(struct inet6_dev *idev,
825 struct ifmcaddr6 *mc; 823 struct ifmcaddr6 *mc;
826 824
827 mc = kzalloc(sizeof(*mc), GFP_ATOMIC); 825 mc = kzalloc(sizeof(*mc), GFP_ATOMIC);
828 if (mc == NULL) 826 if (!mc)
829 return NULL; 827 return NULL;
830 828
831 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc); 829 setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc);
@@ -862,7 +860,7 @@ int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr)
862 /* we need to take a reference on idev */ 860 /* we need to take a reference on idev */
863 idev = in6_dev_get(dev); 861 idev = in6_dev_get(dev);
864 862
865 if (idev == NULL) 863 if (!idev)
866 return -EINVAL; 864 return -EINVAL;
867 865
868 write_lock_bh(&idev->lock); 866 write_lock_bh(&idev->lock);
@@ -1330,7 +1328,7 @@ int igmp6_event_query(struct sk_buff *skb)
1330 return -EINVAL; 1328 return -EINVAL;
1331 1329
1332 idev = __in6_dev_get(skb->dev); 1330 idev = __in6_dev_get(skb->dev);
1333 if (idev == NULL) 1331 if (!idev)
1334 return 0; 1332 return 0;
1335 1333
1336 mld = (struct mld_msg *)icmp6_hdr(skb); 1334 mld = (struct mld_msg *)icmp6_hdr(skb);
@@ -1445,7 +1443,7 @@ int igmp6_event_report(struct sk_buff *skb)
1445 return -EINVAL; 1443 return -EINVAL;
1446 1444
1447 idev = __in6_dev_get(skb->dev); 1445 idev = __in6_dev_get(skb->dev);
1448 if (idev == NULL) 1446 if (!idev)
1449 return -ENODEV; 1447 return -ENODEV;
1450 1448
1451 /* 1449 /*
@@ -1646,8 +1644,9 @@ static void mld_sendpack(struct sk_buff *skb)
1646 1644
1647 payload_len = skb->len; 1645 payload_len = skb->len;
1648 1646
1649 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev, 1647 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT,
1650 dst_output); 1648 net->ipv6.igmp_sk, skb, NULL, skb->dev,
1649 dst_output_sk);
1651out: 1650out:
1652 if (!err) { 1651 if (!err) {
1653 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT); 1652 ICMP6MSGOUT_INC_STATS(net, idev, ICMPV6_MLD2_REPORT);
@@ -1964,7 +1963,7 @@ static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
1964 1963
1965 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err); 1964 skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err);
1966 1965
1967 if (skb == NULL) { 1966 if (!skb) {
1968 rcu_read_lock(); 1967 rcu_read_lock();
1969 IP6_INC_STATS(net, __in6_dev_get(dev), 1968 IP6_INC_STATS(net, __in6_dev_get(dev),
1970 IPSTATS_MIB_OUTDISCARDS); 1969 IPSTATS_MIB_OUTDISCARDS);
@@ -2009,8 +2008,8 @@ static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type)
2009 } 2008 }
2010 2009
2011 skb_dst_set(skb, dst); 2010 skb_dst_set(skb, dst);
2012 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev, 2011 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
2013 dst_output); 2012 NULL, skb->dev, dst_output_sk);
2014out: 2013out:
2015 if (!err) { 2014 if (!err) {
2016 ICMP6MSGOUT_INC_STATS(net, idev, type); 2015 ICMP6MSGOUT_INC_STATS(net, idev, type);
@@ -2613,7 +2612,7 @@ static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr
2613 2612
2614 im = im->next; 2613 im = im->next;
2615 while (!im) { 2614 while (!im) {
2616 if (likely(state->idev != NULL)) 2615 if (likely(state->idev))
2617 read_unlock_bh(&state->idev->lock); 2616 read_unlock_bh(&state->idev->lock);
2618 2617
2619 state->dev = next_net_device_rcu(state->dev); 2618 state->dev = next_net_device_rcu(state->dev);
@@ -2659,7 +2658,7 @@ static void igmp6_mc_seq_stop(struct seq_file *seq, void *v)
2659{ 2658{
2660 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq); 2659 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2661 2660
2662 if (likely(state->idev != NULL)) { 2661 if (likely(state->idev)) {
2663 read_unlock_bh(&state->idev->lock); 2662 read_unlock_bh(&state->idev->lock);
2664 state->idev = NULL; 2663 state->idev = NULL;
2665 } 2664 }
@@ -2728,10 +2727,10 @@ static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq)
2728 continue; 2727 continue;
2729 read_lock_bh(&idev->lock); 2728 read_lock_bh(&idev->lock);
2730 im = idev->mc_list; 2729 im = idev->mc_list;
2731 if (likely(im != NULL)) { 2730 if (likely(im)) {
2732 spin_lock_bh(&im->mca_lock); 2731 spin_lock_bh(&im->mca_lock);
2733 psf = im->mca_sources; 2732 psf = im->mca_sources;
2734 if (likely(psf != NULL)) { 2733 if (likely(psf)) {
2735 state->im = im; 2734 state->im = im;
2736 state->idev = idev; 2735 state->idev = idev;
2737 break; 2736 break;
@@ -2752,7 +2751,7 @@ static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_s
2752 spin_unlock_bh(&state->im->mca_lock); 2751 spin_unlock_bh(&state->im->mca_lock);
2753 state->im = state->im->next; 2752 state->im = state->im->next;
2754 while (!state->im) { 2753 while (!state->im) {
2755 if (likely(state->idev != NULL)) 2754 if (likely(state->idev))
2756 read_unlock_bh(&state->idev->lock); 2755 read_unlock_bh(&state->idev->lock);
2757 2756
2758 state->dev = next_net_device_rcu(state->dev); 2757 state->dev = next_net_device_rcu(state->dev);
@@ -2806,11 +2805,11 @@ static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v)
2806 __releases(RCU) 2805 __releases(RCU)
2807{ 2806{
2808 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq); 2807 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2809 if (likely(state->im != NULL)) { 2808 if (likely(state->im)) {
2810 spin_unlock_bh(&state->im->mca_lock); 2809 spin_unlock_bh(&state->im->mca_lock);
2811 state->im = NULL; 2810 state->im = NULL;
2812 } 2811 }
2813 if (likely(state->idev != NULL)) { 2812 if (likely(state->idev)) {
2814 read_unlock_bh(&state->idev->lock); 2813 read_unlock_bh(&state->idev->lock);
2815 state->idev = NULL; 2814 state->idev = NULL;
2816 } 2815 }
@@ -2907,20 +2906,32 @@ static int __net_init igmp6_net_init(struct net *net)
2907 2906
2908 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1; 2907 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2909 2908
2909 err = inet_ctl_sock_create(&net->ipv6.mc_autojoin_sk, PF_INET6,
2910 SOCK_RAW, IPPROTO_ICMPV6, net);
2911 if (err < 0) {
2912 pr_err("Failed to initialize the IGMP6 autojoin socket (err %d)\n",
2913 err);
2914 goto out_sock_create;
2915 }
2916
2910 err = igmp6_proc_init(net); 2917 err = igmp6_proc_init(net);
2911 if (err) 2918 if (err)
2912 goto out_sock_create; 2919 goto out_sock_create_autojoin;
2913out: 2920
2914 return err; 2921 return 0;
2915 2922
2923out_sock_create_autojoin:
2924 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2916out_sock_create: 2925out_sock_create:
2917 inet_ctl_sock_destroy(net->ipv6.igmp_sk); 2926 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2918 goto out; 2927out:
2928 return err;
2919} 2929}
2920 2930
2921static void __net_exit igmp6_net_exit(struct net *net) 2931static void __net_exit igmp6_net_exit(struct net *net)
2922{ 2932{
2923 inet_ctl_sock_destroy(net->ipv6.igmp_sk); 2933 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2934 inet_ctl_sock_destroy(net->ipv6.mc_autojoin_sk);
2924 igmp6_proc_exit(net); 2935 igmp6_proc_exit(net);
2925} 2936}
2926 2937
diff --git a/net/ipv6/ndisc.c b/net/ipv6/ndisc.c
index 14ecdaf06bf7..96f153c0846b 100644
--- a/net/ipv6/ndisc.c
+++ b/net/ipv6/ndisc.c
@@ -84,6 +84,7 @@ do { \
84static u32 ndisc_hash(const void *pkey, 84static u32 ndisc_hash(const void *pkey,
85 const struct net_device *dev, 85 const struct net_device *dev,
86 __u32 *hash_rnd); 86 __u32 *hash_rnd);
87static bool ndisc_key_eq(const struct neighbour *neigh, const void *pkey);
87static int ndisc_constructor(struct neighbour *neigh); 88static int ndisc_constructor(struct neighbour *neigh);
88static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb); 89static void ndisc_solicit(struct neighbour *neigh, struct sk_buff *skb);
89static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb); 90static void ndisc_error_report(struct neighbour *neigh, struct sk_buff *skb);
@@ -117,7 +118,9 @@ static const struct neigh_ops ndisc_direct_ops = {
117struct neigh_table nd_tbl = { 118struct neigh_table nd_tbl = {
118 .family = AF_INET6, 119 .family = AF_INET6,
119 .key_len = sizeof(struct in6_addr), 120 .key_len = sizeof(struct in6_addr),
121 .protocol = cpu_to_be16(ETH_P_IPV6),
120 .hash = ndisc_hash, 122 .hash = ndisc_hash,
123 .key_eq = ndisc_key_eq,
121 .constructor = ndisc_constructor, 124 .constructor = ndisc_constructor,
122 .pconstructor = pndisc_constructor, 125 .pconstructor = pndisc_constructor,
123 .pdestructor = pndisc_destructor, 126 .pdestructor = pndisc_destructor,
@@ -294,6 +297,11 @@ static u32 ndisc_hash(const void *pkey,
294 return ndisc_hashfn(pkey, dev, hash_rnd); 297 return ndisc_hashfn(pkey, dev, hash_rnd);
295} 298}
296 299
300static bool ndisc_key_eq(const struct neighbour *n, const void *pkey)
301{
302 return neigh_key_eq128(n, pkey);
303}
304
297static int ndisc_constructor(struct neighbour *neigh) 305static int ndisc_constructor(struct neighbour *neigh)
298{ 306{
299 struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key; 307 struct in6_addr *addr = (struct in6_addr *)&neigh->primary_key;
@@ -303,7 +311,7 @@ static int ndisc_constructor(struct neighbour *neigh)
303 bool is_multicast = ipv6_addr_is_multicast(addr); 311 bool is_multicast = ipv6_addr_is_multicast(addr);
304 312
305 in6_dev = in6_dev_get(dev); 313 in6_dev = in6_dev_get(dev);
306 if (in6_dev == NULL) { 314 if (!in6_dev) {
307 return -EINVAL; 315 return -EINVAL;
308 } 316 }
309 317
@@ -348,7 +356,7 @@ static int pndisc_constructor(struct pneigh_entry *n)
348 struct in6_addr maddr; 356 struct in6_addr maddr;
349 struct net_device *dev = n->dev; 357 struct net_device *dev = n->dev;
350 358
351 if (dev == NULL || __in6_dev_get(dev) == NULL) 359 if (!dev || !__in6_dev_get(dev))
352 return -EINVAL; 360 return -EINVAL;
353 addrconf_addr_solict_mult(addr, &maddr); 361 addrconf_addr_solict_mult(addr, &maddr);
354 ipv6_dev_mc_inc(dev, &maddr); 362 ipv6_dev_mc_inc(dev, &maddr);
@@ -361,7 +369,7 @@ static void pndisc_destructor(struct pneigh_entry *n)
361 struct in6_addr maddr; 369 struct in6_addr maddr;
362 struct net_device *dev = n->dev; 370 struct net_device *dev = n->dev;
363 371
364 if (dev == NULL || __in6_dev_get(dev) == NULL) 372 if (!dev || !__in6_dev_get(dev))
365 return; 373 return;
366 addrconf_addr_solict_mult(addr, &maddr); 374 addrconf_addr_solict_mult(addr, &maddr);
367 ipv6_dev_mc_dec(dev, &maddr); 375 ipv6_dev_mc_dec(dev, &maddr);
@@ -455,8 +463,9 @@ static void ndisc_send_skb(struct sk_buff *skb,
455 idev = __in6_dev_get(dst->dev); 463 idev = __in6_dev_get(dst->dev);
456 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len); 464 IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len);
457 465
458 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, dst->dev, 466 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
459 dst_output); 467 NULL, dst->dev,
468 dst_output_sk);
460 if (!err) { 469 if (!err) {
461 ICMP6MSGOUT_INC_STATS(net, idev, type); 470 ICMP6MSGOUT_INC_STATS(net, idev, type);
462 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); 471 ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS);
@@ -552,7 +561,7 @@ void ndisc_send_ns(struct net_device *dev, struct neighbour *neigh,
552 int optlen = 0; 561 int optlen = 0;
553 struct nd_msg *msg; 562 struct nd_msg *msg;
554 563
555 if (saddr == NULL) { 564 if (!saddr) {
556 if (ipv6_get_lladdr(dev, &addr_buf, 565 if (ipv6_get_lladdr(dev, &addr_buf,
557 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC))) 566 (IFA_F_TENTATIVE|IFA_F_OPTIMISTIC)))
558 return; 567 return;
@@ -1022,13 +1031,13 @@ static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1022 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr)); 1031 size_t msg_size = base_size + nla_total_size(sizeof(struct in6_addr));
1023 1032
1024 skb = nlmsg_new(msg_size, GFP_ATOMIC); 1033 skb = nlmsg_new(msg_size, GFP_ATOMIC);
1025 if (skb == NULL) { 1034 if (!skb) {
1026 err = -ENOBUFS; 1035 err = -ENOBUFS;
1027 goto errout; 1036 goto errout;
1028 } 1037 }
1029 1038
1030 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0); 1039 nlh = nlmsg_put(skb, 0, 0, RTM_NEWNDUSEROPT, base_size, 0);
1031 if (nlh == NULL) { 1040 if (!nlh) {
1032 goto nla_put_failure; 1041 goto nla_put_failure;
1033 } 1042 }
1034 1043
@@ -1041,8 +1050,7 @@ static void ndisc_ra_useropt(struct sk_buff *ra, struct nd_opt_hdr *opt)
1041 1050
1042 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3); 1051 memcpy(ndmsg + 1, opt, opt->nd_opt_len << 3);
1043 1052
1044 if (nla_put(skb, NDUSEROPT_SRCADDR, sizeof(struct in6_addr), 1053 if (nla_put_in6_addr(skb, NDUSEROPT_SRCADDR, &ipv6_hdr(ra)->saddr))
1045 &ipv6_hdr(ra)->saddr))
1046 goto nla_put_failure; 1054 goto nla_put_failure;
1047 nlmsg_end(skb, nlh); 1055 nlmsg_end(skb, nlh);
1048 1056
@@ -1096,7 +1104,7 @@ static void ndisc_router_discovery(struct sk_buff *skb)
1096 */ 1104 */
1097 1105
1098 in6_dev = __in6_dev_get(skb->dev); 1106 in6_dev = __in6_dev_get(skb->dev);
1099 if (in6_dev == NULL) { 1107 if (!in6_dev) {
1100 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n", 1108 ND_PRINTK(0, err, "RA: can't find inet6 device for %s\n",
1101 skb->dev->name); 1109 skb->dev->name);
1102 return; 1110 return;
@@ -1191,11 +1199,11 @@ static void ndisc_router_discovery(struct sk_buff *skb)
1191 1199
1192 ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n", 1200 ND_PRINTK(3, info, "RA: rt: %p lifetime: %d, for dev: %s\n",
1193 rt, lifetime, skb->dev->name); 1201 rt, lifetime, skb->dev->name);
1194 if (rt == NULL && lifetime) { 1202 if (!rt && lifetime) {
1195 ND_PRINTK(3, info, "RA: adding default router\n"); 1203 ND_PRINTK(3, info, "RA: adding default router\n");
1196 1204
1197 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref); 1205 rt = rt6_add_dflt_router(&ipv6_hdr(skb)->saddr, skb->dev, pref);
1198 if (rt == NULL) { 1206 if (!rt) {
1199 ND_PRINTK(0, err, 1207 ND_PRINTK(0, err,
1200 "RA: %s failed to add default route\n", 1208 "RA: %s failed to add default route\n",
1201 __func__); 1209 __func__);
@@ -1203,7 +1211,7 @@ static void ndisc_router_discovery(struct sk_buff *skb)
1203 } 1211 }
1204 1212
1205 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr); 1213 neigh = dst_neigh_lookup(&rt->dst, &ipv6_hdr(skb)->saddr);
1206 if (neigh == NULL) { 1214 if (!neigh) {
1207 ND_PRINTK(0, err, 1215 ND_PRINTK(0, err,
1208 "RA: %s got default router without neighbour\n", 1216 "RA: %s got default router without neighbour\n",
1209 __func__); 1217 __func__);
diff --git a/net/ipv6/netfilter.c b/net/ipv6/netfilter.c
index 398377a9d018..d958718b5031 100644
--- a/net/ipv6/netfilter.c
+++ b/net/ipv6/netfilter.c
@@ -84,7 +84,7 @@ static void nf_ip6_saveroute(const struct sk_buff *skb,
84{ 84{
85 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry); 85 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
86 86
87 if (entry->hook == NF_INET_LOCAL_OUT) { 87 if (entry->state.hook == NF_INET_LOCAL_OUT) {
88 const struct ipv6hdr *iph = ipv6_hdr(skb); 88 const struct ipv6hdr *iph = ipv6_hdr(skb);
89 89
90 rt_info->daddr = iph->daddr; 90 rt_info->daddr = iph->daddr;
@@ -98,7 +98,7 @@ static int nf_ip6_reroute(struct sk_buff *skb,
98{ 98{
99 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry); 99 struct ip6_rt_info *rt_info = nf_queue_entry_reroute(entry);
100 100
101 if (entry->hook == NF_INET_LOCAL_OUT) { 101 if (entry->state.hook == NF_INET_LOCAL_OUT) {
102 const struct ipv6hdr *iph = ipv6_hdr(skb); 102 const struct ipv6hdr *iph = ipv6_hdr(skb);
103 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) || 103 if (!ipv6_addr_equal(&iph->daddr, &rt_info->daddr) ||
104 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) || 104 !ipv6_addr_equal(&iph->saddr, &rt_info->saddr) ||
diff --git a/net/ipv6/netfilter/Kconfig b/net/ipv6/netfilter/Kconfig
index a069822936e6..ca6998345b42 100644
--- a/net/ipv6/netfilter/Kconfig
+++ b/net/ipv6/netfilter/Kconfig
@@ -25,14 +25,16 @@ 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
28if NF_TABLES
29
28config NF_TABLES_IPV6 30config NF_TABLES_IPV6
29 depends on NF_TABLES
30 tristate "IPv6 nf_tables support" 31 tristate "IPv6 nf_tables support"
31 help 32 help
32 This option enables the IPv6 support for nf_tables. 33 This option enables the IPv6 support for nf_tables.
33 34
35if NF_TABLES_IPV6
36
34config NFT_CHAIN_ROUTE_IPV6 37config NFT_CHAIN_ROUTE_IPV6
35 depends on NF_TABLES_IPV6
36 tristate "IPv6 nf_tables route chain support" 38 tristate "IPv6 nf_tables route chain support"
37 help 39 help
38 This option enables the "route" chain for IPv6 in nf_tables. This 40 This option enables the "route" chain for IPv6 in nf_tables. This
@@ -40,16 +42,18 @@ config NFT_CHAIN_ROUTE_IPV6
40 fields such as the source, destination, flowlabel, hop-limit and 42 fields such as the source, destination, flowlabel, hop-limit and
41 the packet mark. 43 the packet mark.
42 44
43config NF_REJECT_IPV6
44 tristate "IPv6 packet rejection"
45 default m if NETFILTER_ADVANCED=n
46
47config NFT_REJECT_IPV6 45config NFT_REJECT_IPV6
48 depends on NF_TABLES_IPV6
49 select NF_REJECT_IPV6 46 select NF_REJECT_IPV6
50 default NFT_REJECT 47 default NFT_REJECT
51 tristate 48 tristate
52 49
50endif # NF_TABLES_IPV6
51endif # NF_TABLES
52
53config NF_REJECT_IPV6
54 tristate "IPv6 packet rejection"
55 default m if NETFILTER_ADVANCED=n
56
53config NF_LOG_IPV6 57config NF_LOG_IPV6
54 tristate "IPv6 packet logging" 58 tristate "IPv6 packet logging"
55 default m if NETFILTER_ADVANCED=n 59 default m if NETFILTER_ADVANCED=n
diff --git a/net/ipv6/netfilter/ip6_tables.c b/net/ipv6/netfilter/ip6_tables.c
index bb00c6f2a885..1a732a1d3c8e 100644
--- a/net/ipv6/netfilter/ip6_tables.c
+++ b/net/ipv6/netfilter/ip6_tables.c
@@ -9,7 +9,10 @@
9 * it under the terms of the GNU General Public License version 2 as 9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation. 10 * published by the Free Software Foundation.
11 */ 11 */
12
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15#include <linux/kernel.h>
13#include <linux/capability.h> 16#include <linux/capability.h>
14#include <linux/in.h> 17#include <linux/in.h>
15#include <linux/skbuff.h> 18#include <linux/skbuff.h>
@@ -234,7 +237,7 @@ static struct nf_loginfo trace_loginfo = {
234 .type = NF_LOG_TYPE_LOG, 237 .type = NF_LOG_TYPE_LOG,
235 .u = { 238 .u = {
236 .log = { 239 .log = {
237 .level = 4, 240 .level = LOGLEVEL_WARNING,
238 .logflags = NF_LOG_MASK, 241 .logflags = NF_LOG_MASK,
239 }, 242 },
240 }, 243 },
@@ -314,8 +317,7 @@ ip6t_next_entry(const struct ip6t_entry *entry)
314unsigned int 317unsigned int
315ip6t_do_table(struct sk_buff *skb, 318ip6t_do_table(struct sk_buff *skb,
316 unsigned int hook, 319 unsigned int hook,
317 const struct net_device *in, 320 const struct nf_hook_state *state,
318 const struct net_device *out,
319 struct xt_table *table) 321 struct xt_table *table)
320{ 322{
321 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long)))); 323 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
@@ -330,8 +332,8 @@ ip6t_do_table(struct sk_buff *skb,
330 unsigned int addend; 332 unsigned int addend;
331 333
332 /* Initialization */ 334 /* Initialization */
333 indev = in ? in->name : nulldevname; 335 indev = state->in ? state->in->name : nulldevname;
334 outdev = out ? out->name : nulldevname; 336 outdev = state->out ? state->out->name : nulldevname;
335 /* We handle fragments by dealing with the first fragment as 337 /* We handle fragments by dealing with the first fragment as
336 * if it was a normal packet. All other fragments are treated 338 * if it was a normal packet. All other fragments are treated
337 * normally, except that they will NEVER match rules that ask 339 * normally, except that they will NEVER match rules that ask
@@ -339,8 +341,8 @@ ip6t_do_table(struct sk_buff *skb,
339 * rule is also a fragment-specific rule, non-fragments won't 341 * rule is also a fragment-specific rule, non-fragments won't
340 * match it. */ 342 * match it. */
341 acpar.hotdrop = false; 343 acpar.hotdrop = false;
342 acpar.in = in; 344 acpar.in = state->in;
343 acpar.out = out; 345 acpar.out = state->out;
344 acpar.family = NFPROTO_IPV6; 346 acpar.family = NFPROTO_IPV6;
345 acpar.hooknum = hook; 347 acpar.hooknum = hook;
346 348
@@ -390,7 +392,7 @@ ip6t_do_table(struct sk_buff *skb,
390#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE) 392#if IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TRACE)
391 /* The packet is traced: log it */ 393 /* The packet is traced: log it */
392 if (unlikely(skb->nf_trace)) 394 if (unlikely(skb->nf_trace))
393 trace_packet(skb, hook, in, out, 395 trace_packet(skb, hook, state->in, state->out,
394 table->name, private, e); 396 table->name, private, e);
395#endif 397#endif
396 /* Standard target? */ 398 /* Standard target? */
diff --git a/net/ipv6/netfilter/ip6t_REJECT.c b/net/ipv6/netfilter/ip6t_REJECT.c
index 544b0a9da1b5..12331efd49cf 100644
--- a/net/ipv6/netfilter/ip6t_REJECT.c
+++ b/net/ipv6/netfilter/ip6t_REJECT.c
@@ -83,7 +83,8 @@ static int reject_tg6_check(const struct xt_tgchk_param *par)
83 return -EINVAL; 83 return -EINVAL;
84 } else if (rejinfo->with == IP6T_TCP_RESET) { 84 } else if (rejinfo->with == IP6T_TCP_RESET) {
85 /* Must specify that it's a TCP packet */ 85 /* Must specify that it's a TCP packet */
86 if (e->ipv6.proto != IPPROTO_TCP || 86 if (!(e->ipv6.flags & IP6T_F_PROTO) ||
87 e->ipv6.proto != IPPROTO_TCP ||
87 (e->ipv6.invflags & XT_INV_PROTO)) { 88 (e->ipv6.invflags & XT_INV_PROTO)) {
88 pr_info("TCP_RESET illegal for non-tcp\n"); 89 pr_info("TCP_RESET illegal for non-tcp\n");
89 return -EINVAL; 90 return -EINVAL;
diff --git a/net/ipv6/netfilter/ip6t_SYNPROXY.c b/net/ipv6/netfilter/ip6t_SYNPROXY.c
index a0d17270117c..6edb7b106de7 100644
--- a/net/ipv6/netfilter/ip6t_SYNPROXY.c
+++ b/net/ipv6/netfilter/ip6t_SYNPROXY.c
@@ -315,11 +315,9 @@ synproxy_tg6(struct sk_buff *skb, const struct xt_action_param *par)
315 315
316static unsigned int ipv6_synproxy_hook(const struct nf_hook_ops *ops, 316static unsigned int ipv6_synproxy_hook(const struct nf_hook_ops *ops,
317 struct sk_buff *skb, 317 struct sk_buff *skb,
318 const struct net_device *in, 318 const struct nf_hook_state *nhs)
319 const struct net_device *out,
320 int (*okfn)(struct sk_buff *))
321{ 319{
322 struct synproxy_net *snet = synproxy_pernet(dev_net(in ? : out)); 320 struct synproxy_net *snet = synproxy_pernet(dev_net(nhs->in ? : nhs->out));
323 enum ip_conntrack_info ctinfo; 321 enum ip_conntrack_info ctinfo;
324 struct nf_conn *ct; 322 struct nf_conn *ct;
325 struct nf_conn_synproxy *synproxy; 323 struct nf_conn_synproxy *synproxy;
diff --git a/net/ipv6/netfilter/ip6table_filter.c b/net/ipv6/netfilter/ip6table_filter.c
index ca7f6c128086..5c33d8abc077 100644
--- a/net/ipv6/netfilter/ip6table_filter.c
+++ b/net/ipv6/netfilter/ip6table_filter.c
@@ -33,13 +33,11 @@ static const struct xt_table packet_filter = {
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(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
37 int (*okfn)(struct sk_buff *))
38{ 37{
39 const struct net *net = dev_net((in != NULL) ? in : out); 38 const struct net *net = dev_net(state->in ? state->in : state->out);
40 39
41 return ip6t_do_table(skb, ops->hooknum, in, out, 40 return ip6t_do_table(skb, ops->hooknum, state, net->ipv6.ip6table_filter);
42 net->ipv6.ip6table_filter);
43} 41}
44 42
45static struct nf_hook_ops *filter_ops __read_mostly; 43static 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 307bbb782d14..b551f5b79fe2 100644
--- a/net/ipv6/netfilter/ip6table_mangle.c
+++ b/net/ipv6/netfilter/ip6table_mangle.c
@@ -32,7 +32,7 @@ static const struct xt_table packet_mangler = {
32}; 32};
33 33
34static unsigned int 34static unsigned int
35ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out) 35ip6t_mangle_out(struct sk_buff *skb, const struct nf_hook_state *state)
36{ 36{
37 unsigned int ret; 37 unsigned int ret;
38 struct in6_addr saddr, daddr; 38 struct in6_addr saddr, daddr;
@@ -57,8 +57,8 @@ ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
57 /* flowlabel and prio (includes version, which shouldn't change either */ 57 /* flowlabel and prio (includes version, which shouldn't change either */
58 flowlabel = *((u_int32_t *)ipv6_hdr(skb)); 58 flowlabel = *((u_int32_t *)ipv6_hdr(skb));
59 59
60 ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, NULL, out, 60 ret = ip6t_do_table(skb, NF_INET_LOCAL_OUT, state,
61 dev_net(out)->ipv6.ip6table_mangle); 61 dev_net(state->out)->ipv6.ip6table_mangle);
62 62
63 if (ret != NF_DROP && ret != NF_STOLEN && 63 if (ret != NF_DROP && ret != NF_STOLEN &&
64 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) || 64 (!ipv6_addr_equal(&ipv6_hdr(skb)->saddr, &saddr) ||
@@ -77,17 +77,16 @@ ip6t_mangle_out(struct sk_buff *skb, const struct net_device *out)
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(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
81 int (*okfn)(struct sk_buff *))
82{ 81{
83 if (ops->hooknum == NF_INET_LOCAL_OUT) 82 if (ops->hooknum == NF_INET_LOCAL_OUT)
84 return ip6t_mangle_out(skb, out); 83 return ip6t_mangle_out(skb, state);
85 if (ops->hooknum == NF_INET_POST_ROUTING) 84 if (ops->hooknum == NF_INET_POST_ROUTING)
86 return ip6t_do_table(skb, ops->hooknum, in, out, 85 return ip6t_do_table(skb, ops->hooknum, state,
87 dev_net(out)->ipv6.ip6table_mangle); 86 dev_net(state->out)->ipv6.ip6table_mangle);
88 /* INPUT/FORWARD */ 87 /* INPUT/FORWARD */
89 return ip6t_do_table(skb, ops->hooknum, in, out, 88 return ip6t_do_table(skb, ops->hooknum, state,
90 dev_net(in)->ipv6.ip6table_mangle); 89 dev_net(state->in)->ipv6.ip6table_mangle);
91} 90}
92 91
93static struct nf_hook_ops *mangle_ops __read_mostly; 92static struct nf_hook_ops *mangle_ops __read_mostly;
diff --git a/net/ipv6/netfilter/ip6table_nat.c b/net/ipv6/netfilter/ip6table_nat.c
index b0634ac996b7..c3a7f7af0ed4 100644
--- a/net/ipv6/netfilter/ip6table_nat.c
+++ b/net/ipv6/netfilter/ip6table_nat.c
@@ -32,49 +32,40 @@ static const struct xt_table nf_nat_ipv6_table = {
32 32
33static unsigned int ip6table_nat_do_chain(const struct nf_hook_ops *ops, 33static unsigned int ip6table_nat_do_chain(const struct nf_hook_ops *ops,
34 struct sk_buff *skb, 34 struct sk_buff *skb,
35 const struct net_device *in, 35 const struct nf_hook_state *state,
36 const struct net_device *out,
37 struct nf_conn *ct) 36 struct nf_conn *ct)
38{ 37{
39 struct net *net = nf_ct_net(ct); 38 struct net *net = nf_ct_net(ct);
40 39
41 return ip6t_do_table(skb, ops->hooknum, in, out, net->ipv6.ip6table_nat); 40 return ip6t_do_table(skb, ops->hooknum, state, net->ipv6.ip6table_nat);
42} 41}
43 42
44static unsigned int ip6table_nat_fn(const struct nf_hook_ops *ops, 43static unsigned int ip6table_nat_fn(const struct nf_hook_ops *ops,
45 struct sk_buff *skb, 44 struct sk_buff *skb,
46 const struct net_device *in, 45 const struct nf_hook_state *state)
47 const struct net_device *out,
48 int (*okfn)(struct sk_buff *))
49{ 46{
50 return nf_nat_ipv6_fn(ops, skb, in, out, ip6table_nat_do_chain); 47 return nf_nat_ipv6_fn(ops, skb, state, ip6table_nat_do_chain);
51} 48}
52 49
53static unsigned int ip6table_nat_in(const struct nf_hook_ops *ops, 50static unsigned int ip6table_nat_in(const struct nf_hook_ops *ops,
54 struct sk_buff *skb, 51 struct sk_buff *skb,
55 const struct net_device *in, 52 const struct nf_hook_state *state)
56 const struct net_device *out,
57 int (*okfn)(struct sk_buff *))
58{ 53{
59 return nf_nat_ipv6_in(ops, skb, in, out, ip6table_nat_do_chain); 54 return nf_nat_ipv6_in(ops, skb, state, ip6table_nat_do_chain);
60} 55}
61 56
62static unsigned int ip6table_nat_out(const struct nf_hook_ops *ops, 57static unsigned int ip6table_nat_out(const struct nf_hook_ops *ops,
63 struct sk_buff *skb, 58 struct sk_buff *skb,
64 const struct net_device *in, 59 const struct nf_hook_state *state)
65 const struct net_device *out,
66 int (*okfn)(struct sk_buff *))
67{ 60{
68 return nf_nat_ipv6_out(ops, skb, in, out, ip6table_nat_do_chain); 61 return nf_nat_ipv6_out(ops, skb, state, ip6table_nat_do_chain);
69} 62}
70 63
71static unsigned int ip6table_nat_local_fn(const struct nf_hook_ops *ops, 64static unsigned int ip6table_nat_local_fn(const struct nf_hook_ops *ops,
72 struct sk_buff *skb, 65 struct sk_buff *skb,
73 const struct net_device *in, 66 const struct nf_hook_state *state)
74 const struct net_device *out,
75 int (*okfn)(struct sk_buff *))
76{ 67{
77 return nf_nat_ipv6_local_fn(ops, skb, in, out, ip6table_nat_do_chain); 68 return nf_nat_ipv6_local_fn(ops, skb, state, ip6table_nat_do_chain);
78} 69}
79 70
80static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = { 71static struct nf_hook_ops nf_nat_ipv6_ops[] __read_mostly = {
diff --git a/net/ipv6/netfilter/ip6table_raw.c b/net/ipv6/netfilter/ip6table_raw.c
index 5274740acecc..0b33caad2b69 100644
--- a/net/ipv6/netfilter/ip6table_raw.c
+++ b/net/ipv6/netfilter/ip6table_raw.c
@@ -20,13 +20,11 @@ static const struct xt_table packet_raw = {
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(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
24 int (*okfn)(struct sk_buff *))
25{ 24{
26 const struct net *net = dev_net((in != NULL) ? in : out); 25 const struct net *net = dev_net(state->in ? state->in : state->out);
27 26
28 return ip6t_do_table(skb, ops->hooknum, in, out, 27 return ip6t_do_table(skb, ops->hooknum, state, net->ipv6.ip6table_raw);
29 net->ipv6.ip6table_raw);
30} 28}
31 29
32static struct nf_hook_ops *rawtable_ops __read_mostly; 30static 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 ab3b0219ecfa..fcef83c25f7b 100644
--- a/net/ipv6/netfilter/ip6table_security.c
+++ b/net/ipv6/netfilter/ip6table_security.c
@@ -37,13 +37,11 @@ static const struct xt_table security_table = {
37 37
38static unsigned int 38static unsigned int
39ip6table_security_hook(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
41 const struct net_device *out,
42 int (*okfn)(struct sk_buff *))
43{ 41{
44 const struct net *net = dev_net((in != NULL) ? in : out); 42 const struct net *net = dev_net(state->in ? state->in : state->out);
45 43
46 return ip6t_do_table(skb, ops->hooknum, in, out, 44 return ip6t_do_table(skb, ops->hooknum, state,
47 net->ipv6.ip6table_security); 45 net->ipv6.ip6table_security);
48} 46}
49 47
diff --git a/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c b/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
index b68d0e59c1f8..4ba0c34c627b 100644
--- a/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
+++ b/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
@@ -97,9 +97,7 @@ static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
97 97
98static unsigned int ipv6_helper(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
101 const struct net_device *out,
102 int (*okfn)(struct sk_buff *))
103{ 101{
104 struct nf_conn *ct; 102 struct nf_conn *ct;
105 const struct nf_conn_help *help; 103 const struct nf_conn_help *help;
@@ -135,9 +133,7 @@ static unsigned int ipv6_helper(const struct nf_hook_ops *ops,
135 133
136static unsigned int ipv6_confirm(const struct nf_hook_ops *ops, 134static unsigned int ipv6_confirm(const struct nf_hook_ops *ops,
137 struct sk_buff *skb, 135 struct sk_buff *skb,
138 const struct net_device *in, 136 const struct nf_hook_state *state)
139 const struct net_device *out,
140 int (*okfn)(struct sk_buff *))
141{ 137{
142 struct nf_conn *ct; 138 struct nf_conn *ct;
143 enum ip_conntrack_info ctinfo; 139 enum ip_conntrack_info ctinfo;
@@ -171,25 +167,21 @@ out:
171 167
172static unsigned int ipv6_conntrack_in(const struct nf_hook_ops *ops, 168static unsigned int ipv6_conntrack_in(const struct nf_hook_ops *ops,
173 struct sk_buff *skb, 169 struct sk_buff *skb,
174 const struct net_device *in, 170 const struct nf_hook_state *state)
175 const struct net_device *out,
176 int (*okfn)(struct sk_buff *))
177{ 171{
178 return nf_conntrack_in(dev_net(in), PF_INET6, ops->hooknum, skb); 172 return nf_conntrack_in(dev_net(state->in), PF_INET6, ops->hooknum, skb);
179} 173}
180 174
181static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops, 175static unsigned int ipv6_conntrack_local(const struct nf_hook_ops *ops,
182 struct sk_buff *skb, 176 struct sk_buff *skb,
183 const struct net_device *in, 177 const struct nf_hook_state *state)
184 const struct net_device *out,
185 int (*okfn)(struct sk_buff *))
186{ 178{
187 /* root is playing with raw sockets. */ 179 /* root is playing with raw sockets. */
188 if (skb->len < sizeof(struct ipv6hdr)) { 180 if (skb->len < sizeof(struct ipv6hdr)) {
189 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n"); 181 net_notice_ratelimited("ipv6_conntrack_local: packet too short\n");
190 return NF_ACCEPT; 182 return NF_ACCEPT;
191 } 183 }
192 return nf_conntrack_in(dev_net(out), PF_INET6, ops->hooknum, skb); 184 return nf_conntrack_in(dev_net(state->out), PF_INET6, ops->hooknum, skb);
193} 185}
194 186
195static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = { 187static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
@@ -290,10 +282,8 @@ ipv6_getorigdst(struct sock *sk, int optval, void __user *user, int *len)
290static int ipv6_tuple_to_nlattr(struct sk_buff *skb, 282static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
291 const struct nf_conntrack_tuple *tuple) 283 const struct nf_conntrack_tuple *tuple)
292{ 284{
293 if (nla_put(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4, 285 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
294 &tuple->src.u3.ip6) || 286 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
295 nla_put(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
296 &tuple->dst.u3.ip6))
297 goto nla_put_failure; 287 goto nla_put_failure;
298 return 0; 288 return 0;
299 289
@@ -312,10 +302,8 @@ static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
312 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST]) 302 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
313 return -EINVAL; 303 return -EINVAL;
314 304
315 memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]), 305 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
316 sizeof(u_int32_t) * 4); 306 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
317 memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
318 sizeof(u_int32_t) * 4);
319 307
320 return 0; 308 return 0;
321} 309}
diff --git a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
index e70382e4dfb5..a45db0b4785c 100644
--- a/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
+++ b/net/ipv6/netfilter/nf_defrag_ipv6_hooks.c
@@ -54,9 +54,7 @@ static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
54 54
55static unsigned int ipv6_defrag(const struct nf_hook_ops *ops, 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 nf_hook_state *state)
58 const struct net_device *out,
59 int (*okfn)(struct sk_buff *))
60{ 58{
61 struct sk_buff *reasm; 59 struct sk_buff *reasm;
62 60
@@ -77,9 +75,9 @@ static unsigned int ipv6_defrag(const struct nf_hook_ops *ops,
77 75
78 nf_ct_frag6_consume_orig(reasm); 76 nf_ct_frag6_consume_orig(reasm);
79 77
80 NF_HOOK_THRESH(NFPROTO_IPV6, ops->hooknum, reasm, 78 NF_HOOK_THRESH(NFPROTO_IPV6, ops->hooknum, state->sk, reasm,
81 (struct net_device *) in, (struct net_device *) out, 79 state->in, state->out,
82 okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1); 80 state->okfn, NF_IP6_PRI_CONNTRACK_DEFRAG + 1);
83 81
84 return NF_STOLEN; 82 return NF_STOLEN;
85} 83}
diff --git a/net/ipv6/netfilter/nf_log_ipv6.c b/net/ipv6/netfilter/nf_log_ipv6.c
index ddf07e6f59d7..8dd869642f45 100644
--- a/net/ipv6/netfilter/nf_log_ipv6.c
+++ b/net/ipv6/netfilter/nf_log_ipv6.c
@@ -5,8 +5,10 @@
5 * it under the terms of the GNU General Public License version 2 as 5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation. 6 * published by the Free Software Foundation.
7 */ 7 */
8
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 10
11#include <linux/kernel.h>
10#include <linux/module.h> 12#include <linux/module.h>
11#include <linux/spinlock.h> 13#include <linux/spinlock.h>
12#include <linux/skbuff.h> 14#include <linux/skbuff.h>
@@ -27,7 +29,7 @@ static struct nf_loginfo default_loginfo = {
27 .type = NF_LOG_TYPE_LOG, 29 .type = NF_LOG_TYPE_LOG,
28 .u = { 30 .u = {
29 .log = { 31 .log = {
30 .level = 5, 32 .level = LOGLEVEL_NOTICE,
31 .logflags = NF_LOG_MASK, 33 .logflags = NF_LOG_MASK,
32 }, 34 },
33 }, 35 },
diff --git a/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c b/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
index c5812e1c1ffb..e76900e0aa92 100644
--- a/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
+++ b/net/ipv6/netfilter/nf_nat_l3proto_ipv6.c
@@ -263,11 +263,10 @@ EXPORT_SYMBOL_GPL(nf_nat_icmpv6_reply_translation);
263 263
264unsigned int 264unsigned int
265nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, 265nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
266 const struct net_device *in, const struct net_device *out, 266 const struct nf_hook_state *state,
267 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 267 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
268 struct sk_buff *skb, 268 struct sk_buff *skb,
269 const struct net_device *in, 269 const struct nf_hook_state *state,
270 const struct net_device *out,
271 struct nf_conn *ct)) 270 struct nf_conn *ct))
272{ 271{
273 struct nf_conn *ct; 272 struct nf_conn *ct;
@@ -318,7 +317,7 @@ nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
318 if (!nf_nat_initialized(ct, maniptype)) { 317 if (!nf_nat_initialized(ct, maniptype)) {
319 unsigned int ret; 318 unsigned int ret;
320 319
321 ret = do_chain(ops, skb, in, out, ct); 320 ret = do_chain(ops, skb, state, ct);
322 if (ret != NF_ACCEPT) 321 if (ret != NF_ACCEPT)
323 return ret; 322 return ret;
324 323
@@ -332,7 +331,7 @@ nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
332 pr_debug("Already setup manip %s for ct %p\n", 331 pr_debug("Already setup manip %s for ct %p\n",
333 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST", 332 maniptype == NF_NAT_MANIP_SRC ? "SRC" : "DST",
334 ct); 333 ct);
335 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) 334 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, state->out))
336 goto oif_changed; 335 goto oif_changed;
337 } 336 }
338 break; 337 break;
@@ -341,7 +340,7 @@ nf_nat_ipv6_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
341 /* ESTABLISHED */ 340 /* ESTABLISHED */
342 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED || 341 NF_CT_ASSERT(ctinfo == IP_CT_ESTABLISHED ||
343 ctinfo == IP_CT_ESTABLISHED_REPLY); 342 ctinfo == IP_CT_ESTABLISHED_REPLY);
344 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, out)) 343 if (nf_nat_oif_changed(ops->hooknum, ctinfo, nat, state->out))
345 goto oif_changed; 344 goto oif_changed;
346 } 345 }
347 346
@@ -355,17 +354,16 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv6_fn);
355 354
356unsigned int 355unsigned int
357nf_nat_ipv6_in(const struct nf_hook_ops *ops, struct sk_buff *skb, 356nf_nat_ipv6_in(const struct nf_hook_ops *ops, struct sk_buff *skb,
358 const struct net_device *in, const struct net_device *out, 357 const struct nf_hook_state *state,
359 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 358 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
360 struct sk_buff *skb, 359 struct sk_buff *skb,
361 const struct net_device *in, 360 const struct nf_hook_state *state,
362 const struct net_device *out,
363 struct nf_conn *ct)) 361 struct nf_conn *ct))
364{ 362{
365 unsigned int ret; 363 unsigned int ret;
366 struct in6_addr daddr = ipv6_hdr(skb)->daddr; 364 struct in6_addr daddr = ipv6_hdr(skb)->daddr;
367 365
368 ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); 366 ret = nf_nat_ipv6_fn(ops, skb, state, do_chain);
369 if (ret != NF_DROP && ret != NF_STOLEN && 367 if (ret != NF_DROP && ret != NF_STOLEN &&
370 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr)) 368 ipv6_addr_cmp(&daddr, &ipv6_hdr(skb)->daddr))
371 skb_dst_drop(skb); 369 skb_dst_drop(skb);
@@ -376,11 +374,10 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv6_in);
376 374
377unsigned int 375unsigned int
378nf_nat_ipv6_out(const struct nf_hook_ops *ops, struct sk_buff *skb, 376nf_nat_ipv6_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
379 const struct net_device *in, const struct net_device *out, 377 const struct nf_hook_state *state,
380 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 378 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
381 struct sk_buff *skb, 379 struct sk_buff *skb,
382 const struct net_device *in, 380 const struct nf_hook_state *state,
383 const struct net_device *out,
384 struct nf_conn *ct)) 381 struct nf_conn *ct))
385{ 382{
386#ifdef CONFIG_XFRM 383#ifdef CONFIG_XFRM
@@ -394,7 +391,7 @@ nf_nat_ipv6_out(const struct nf_hook_ops *ops, struct sk_buff *skb,
394 if (skb->len < sizeof(struct ipv6hdr)) 391 if (skb->len < sizeof(struct ipv6hdr))
395 return NF_ACCEPT; 392 return NF_ACCEPT;
396 393
397 ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); 394 ret = nf_nat_ipv6_fn(ops, skb, state, do_chain);
398#ifdef CONFIG_XFRM 395#ifdef CONFIG_XFRM
399 if (ret != NF_DROP && ret != NF_STOLEN && 396 if (ret != NF_DROP && ret != NF_STOLEN &&
400 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) && 397 !(IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) &&
@@ -418,11 +415,10 @@ EXPORT_SYMBOL_GPL(nf_nat_ipv6_out);
418 415
419unsigned int 416unsigned int
420nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb, 417nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
421 const struct net_device *in, const struct net_device *out, 418 const struct nf_hook_state *state,
422 unsigned int (*do_chain)(const struct nf_hook_ops *ops, 419 unsigned int (*do_chain)(const struct nf_hook_ops *ops,
423 struct sk_buff *skb, 420 struct sk_buff *skb,
424 const struct net_device *in, 421 const struct nf_hook_state *state,
425 const struct net_device *out,
426 struct nf_conn *ct)) 422 struct nf_conn *ct))
427{ 423{
428 const struct nf_conn *ct; 424 const struct nf_conn *ct;
@@ -434,7 +430,7 @@ nf_nat_ipv6_local_fn(const struct nf_hook_ops *ops, struct sk_buff *skb,
434 if (skb->len < sizeof(struct ipv6hdr)) 430 if (skb->len < sizeof(struct ipv6hdr))
435 return NF_ACCEPT; 431 return NF_ACCEPT;
436 432
437 ret = nf_nat_ipv6_fn(ops, skb, in, out, do_chain); 433 ret = nf_nat_ipv6_fn(ops, skb, state, do_chain);
438 if (ret != NF_DROP && ret != NF_STOLEN && 434 if (ret != NF_DROP && ret != NF_STOLEN &&
439 (ct = nf_ct_get(skb, &ctinfo)) != NULL) { 435 (ct = nf_ct_get(skb, &ctinfo)) != NULL) {
440 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); 436 enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
diff --git a/net/ipv6/netfilter/nf_reject_ipv6.c b/net/ipv6/netfilter/nf_reject_ipv6.c
index d05b36440e8b..94b4c6dfb400 100644
--- a/net/ipv6/netfilter/nf_reject_ipv6.c
+++ b/net/ipv6/netfilter/nf_reject_ipv6.c
@@ -13,6 +13,7 @@
13#include <net/ip6_checksum.h> 13#include <net/ip6_checksum.h>
14#include <net/netfilter/ipv6/nf_reject.h> 14#include <net/netfilter/ipv6/nf_reject.h>
15#include <linux/netfilter_ipv6.h> 15#include <linux/netfilter_ipv6.h>
16#include <linux/netfilter_bridge.h>
16#include <net/netfilter/ipv6/nf_reject.h> 17#include <net/netfilter/ipv6/nf_reject.h>
17 18
18const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb, 19const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
@@ -65,7 +66,7 @@ EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
65 66
66struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb, 67struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
67 const struct sk_buff *oldskb, 68 const struct sk_buff *oldskb,
68 __be16 protocol, int hoplimit) 69 __u8 protocol, int hoplimit)
69{ 70{
70 struct ipv6hdr *ip6h; 71 struct ipv6hdr *ip6h;
71 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb); 72 const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
@@ -195,7 +196,8 @@ void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
195 */ 196 */
196 if (oldskb->nf_bridge) { 197 if (oldskb->nf_bridge) {
197 struct ethhdr *oeth = eth_hdr(oldskb); 198 struct ethhdr *oeth = eth_hdr(oldskb);
198 nskb->dev = oldskb->nf_bridge->physindev; 199
200 nskb->dev = nf_bridge_get_physindev(oldskb);
199 nskb->protocol = htons(ETH_P_IPV6); 201 nskb->protocol = htons(ETH_P_IPV6);
200 ip6h->payload_len = htons(sizeof(struct tcphdr)); 202 ip6h->payload_len = htons(sizeof(struct tcphdr));
201 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol), 203 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
@@ -208,4 +210,39 @@ void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
208} 210}
209EXPORT_SYMBOL_GPL(nf_send_reset6); 211EXPORT_SYMBOL_GPL(nf_send_reset6);
210 212
213static bool reject6_csum_ok(struct sk_buff *skb, int hook)
214{
215 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
216 int thoff;
217 __be16 fo;
218 u8 proto;
219
220 if (skb->csum_bad)
221 return false;
222
223 if (skb_csum_unnecessary(skb))
224 return true;
225
226 proto = ip6h->nexthdr;
227 thoff = ipv6_skip_exthdr(skb, ((u8*)(ip6h+1) - skb->data), &proto, &fo);
228
229 if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
230 return false;
231
232 return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
233}
234
235void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
236 unsigned char code, unsigned int hooknum)
237{
238 if (!reject6_csum_ok(skb_in, hooknum))
239 return;
240
241 if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
242 skb_in->dev = net->loopback_dev;
243
244 icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
245}
246EXPORT_SYMBOL_GPL(nf_send_unreach6);
247
211MODULE_LICENSE("GPL"); 248MODULE_LICENSE("GPL");
diff --git a/net/ipv6/netfilter/nf_tables_ipv6.c b/net/ipv6/netfilter/nf_tables_ipv6.c
index 0d812b31277d..c8148ba76d1a 100644
--- a/net/ipv6/netfilter/nf_tables_ipv6.c
+++ b/net/ipv6/netfilter/nf_tables_ipv6.c
@@ -18,14 +18,12 @@
18 18
19static unsigned int nft_do_chain_ipv6(const struct nf_hook_ops *ops, 19static unsigned int nft_do_chain_ipv6(const struct nf_hook_ops *ops,
20 struct sk_buff *skb, 20 struct sk_buff *skb,
21 const struct net_device *in, 21 const struct nf_hook_state *state)
22 const struct net_device *out,
23 int (*okfn)(struct sk_buff *))
24{ 22{
25 struct nft_pktinfo pkt; 23 struct nft_pktinfo pkt;
26 24
27 /* malformed packet, drop it */ 25 /* malformed packet, drop it */
28 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0) 26 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, state) < 0)
29 return NF_DROP; 27 return NF_DROP;
30 28
31 return nft_do_chain(&pkt, ops); 29 return nft_do_chain(&pkt, ops);
@@ -33,9 +31,7 @@ static unsigned int nft_do_chain_ipv6(const struct nf_hook_ops *ops,
33 31
34static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops, 32static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops,
35 struct sk_buff *skb, 33 struct sk_buff *skb,
36 const struct net_device *in, 34 const struct nf_hook_state *state)
37 const struct net_device *out,
38 int (*okfn)(struct sk_buff *))
39{ 35{
40 if (unlikely(skb->len < sizeof(struct ipv6hdr))) { 36 if (unlikely(skb->len < sizeof(struct ipv6hdr))) {
41 if (net_ratelimit()) 37 if (net_ratelimit())
@@ -44,7 +40,7 @@ static unsigned int nft_ipv6_output(const struct nf_hook_ops *ops,
44 return NF_ACCEPT; 40 return NF_ACCEPT;
45 } 41 }
46 42
47 return nft_do_chain_ipv6(ops, skb, in, out, okfn); 43 return nft_do_chain_ipv6(ops, skb, state);
48} 44}
49 45
50struct nft_af_info nft_af_ipv6 __read_mostly = { 46struct nft_af_info nft_af_ipv6 __read_mostly = {
diff --git a/net/ipv6/netfilter/nft_chain_nat_ipv6.c b/net/ipv6/netfilter/nft_chain_nat_ipv6.c
index 1c4b75dd425b..951bb458b7bd 100644
--- a/net/ipv6/netfilter/nft_chain_nat_ipv6.c
+++ b/net/ipv6/netfilter/nft_chain_nat_ipv6.c
@@ -26,51 +26,42 @@
26 26
27static unsigned int nft_nat_do_chain(const struct nf_hook_ops *ops, 27static unsigned int nft_nat_do_chain(const struct nf_hook_ops *ops,
28 struct sk_buff *skb, 28 struct sk_buff *skb,
29 const struct net_device *in, 29 const struct nf_hook_state *state,
30 const struct net_device *out,
31 struct nf_conn *ct) 30 struct nf_conn *ct)
32{ 31{
33 struct nft_pktinfo pkt; 32 struct nft_pktinfo pkt;
34 33
35 nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out); 34 nft_set_pktinfo_ipv6(&pkt, ops, skb, state);
36 35
37 return nft_do_chain(&pkt, ops); 36 return nft_do_chain(&pkt, ops);
38} 37}
39 38
40static unsigned int nft_nat_ipv6_fn(const struct nf_hook_ops *ops, 39static unsigned int nft_nat_ipv6_fn(const struct nf_hook_ops *ops,
41 struct sk_buff *skb, 40 struct sk_buff *skb,
42 const struct net_device *in, 41 const struct nf_hook_state *state)
43 const struct net_device *out,
44 int (*okfn)(struct sk_buff *))
45{ 42{
46 return nf_nat_ipv6_fn(ops, skb, in, out, nft_nat_do_chain); 43 return nf_nat_ipv6_fn(ops, skb, state, nft_nat_do_chain);
47} 44}
48 45
49static unsigned int nft_nat_ipv6_in(const struct nf_hook_ops *ops, 46static unsigned int nft_nat_ipv6_in(const struct nf_hook_ops *ops,
50 struct sk_buff *skb, 47 struct sk_buff *skb,
51 const struct net_device *in, 48 const struct nf_hook_state *state)
52 const struct net_device *out,
53 int (*okfn)(struct sk_buff *))
54{ 49{
55 return nf_nat_ipv6_in(ops, skb, in, out, nft_nat_do_chain); 50 return nf_nat_ipv6_in(ops, skb, state, nft_nat_do_chain);
56} 51}
57 52
58static unsigned int nft_nat_ipv6_out(const struct nf_hook_ops *ops, 53static unsigned int nft_nat_ipv6_out(const struct nf_hook_ops *ops,
59 struct sk_buff *skb, 54 struct sk_buff *skb,
60 const struct net_device *in, 55 const struct nf_hook_state *state)
61 const struct net_device *out,
62 int (*okfn)(struct sk_buff *))
63{ 56{
64 return nf_nat_ipv6_out(ops, skb, in, out, nft_nat_do_chain); 57 return nf_nat_ipv6_out(ops, skb, state, nft_nat_do_chain);
65} 58}
66 59
67static unsigned int nft_nat_ipv6_local_fn(const struct nf_hook_ops *ops, 60static unsigned int nft_nat_ipv6_local_fn(const struct nf_hook_ops *ops,
68 struct sk_buff *skb, 61 struct sk_buff *skb,
69 const struct net_device *in, 62 const struct nf_hook_state *state)
70 const struct net_device *out,
71 int (*okfn)(struct sk_buff *))
72{ 63{
73 return nf_nat_ipv6_local_fn(ops, skb, in, out, nft_nat_do_chain); 64 return nf_nat_ipv6_local_fn(ops, skb, state, nft_nat_do_chain);
74} 65}
75 66
76static const struct nf_chain_type nft_chain_nat_ipv6 = { 67static const struct nf_chain_type nft_chain_nat_ipv6 = {
diff --git a/net/ipv6/netfilter/nft_chain_route_ipv6.c b/net/ipv6/netfilter/nft_chain_route_ipv6.c
index 42031299585e..0dafdaac5e17 100644
--- a/net/ipv6/netfilter/nft_chain_route_ipv6.c
+++ b/net/ipv6/netfilter/nft_chain_route_ipv6.c
@@ -24,9 +24,7 @@
24 24
25static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, 25static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
26 struct sk_buff *skb, 26 struct sk_buff *skb,
27 const struct net_device *in, 27 const struct nf_hook_state *state)
28 const struct net_device *out,
29 int (*okfn)(struct sk_buff *))
30{ 28{
31 unsigned int ret; 29 unsigned int ret;
32 struct nft_pktinfo pkt; 30 struct nft_pktinfo pkt;
@@ -35,7 +33,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,
35 u32 mark, flowlabel; 33 u32 mark, flowlabel;
36 34
37 /* malformed packet, drop it */ 35 /* malformed packet, drop it */
38 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, in, out) < 0) 36 if (nft_set_pktinfo_ipv6(&pkt, ops, skb, state) < 0)
39 return NF_DROP; 37 return NF_DROP;
40 38
41 /* save source/dest address, mark, hoplimit, flowlabel, priority */ 39 /* save source/dest address, mark, hoplimit, flowlabel, priority */
diff --git a/net/ipv6/netfilter/nft_masq_ipv6.c b/net/ipv6/netfilter/nft_masq_ipv6.c
index 529c119cbb14..cd1ac1637a05 100644
--- a/net/ipv6/netfilter/nft_masq_ipv6.c
+++ b/net/ipv6/netfilter/nft_masq_ipv6.c
@@ -18,19 +18,16 @@
18#include <net/netfilter/ipv6/nf_nat_masquerade.h> 18#include <net/netfilter/ipv6/nf_nat_masquerade.h>
19 19
20static void nft_masq_ipv6_eval(const struct nft_expr *expr, 20static void nft_masq_ipv6_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt) 22 const struct nft_pktinfo *pkt)
23{ 23{
24 struct nft_masq *priv = nft_expr_priv(expr); 24 struct nft_masq *priv = nft_expr_priv(expr);
25 struct nf_nat_range range; 25 struct nf_nat_range range;
26 unsigned int verdict;
27 26
28 memset(&range, 0, sizeof(range)); 27 memset(&range, 0, sizeof(range));
29 range.flags = priv->flags; 28 range.flags = priv->flags;
30 29
31 verdict = nf_nat_masquerade_ipv6(pkt->skb, &range, pkt->out); 30 regs->verdict.code = nf_nat_masquerade_ipv6(pkt->skb, &range, pkt->out);
32
33 data[NFT_REG_VERDICT].verdict = verdict;
34} 31}
35 32
36static struct nft_expr_type nft_masq_ipv6_type; 33static struct nft_expr_type nft_masq_ipv6_type;
diff --git a/net/ipv6/netfilter/nft_redir_ipv6.c b/net/ipv6/netfilter/nft_redir_ipv6.c
index 11820b6b3613..effd393bd517 100644
--- a/net/ipv6/netfilter/nft_redir_ipv6.c
+++ b/net/ipv6/netfilter/nft_redir_ipv6.c
@@ -18,26 +18,25 @@
18#include <net/netfilter/nf_nat_redirect.h> 18#include <net/netfilter/nf_nat_redirect.h>
19 19
20static void nft_redir_ipv6_eval(const struct nft_expr *expr, 20static void nft_redir_ipv6_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt) 22 const struct nft_pktinfo *pkt)
23{ 23{
24 struct nft_redir *priv = nft_expr_priv(expr); 24 struct nft_redir *priv = nft_expr_priv(expr);
25 struct nf_nat_range range; 25 struct nf_nat_range range;
26 unsigned int verdict;
27 26
28 memset(&range, 0, sizeof(range)); 27 memset(&range, 0, sizeof(range));
29 if (priv->sreg_proto_min) { 28 if (priv->sreg_proto_min) {
30 range.min_proto.all = 29 range.min_proto.all =
31 *(__be16 *)&data[priv->sreg_proto_min].data[0]; 30 *(__be16 *)&regs->data[priv->sreg_proto_min],
32 range.max_proto.all = 31 range.max_proto.all =
33 *(__be16 *)&data[priv->sreg_proto_max].data[0]; 32 *(__be16 *)&regs->data[priv->sreg_proto_max],
34 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 33 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
35 } 34 }
36 35
37 range.flags |= priv->flags; 36 range.flags |= priv->flags;
38 37
39 verdict = nf_nat_redirect_ipv6(pkt->skb, &range, pkt->ops->hooknum); 38 regs->verdict.code = nf_nat_redirect_ipv6(pkt->skb, &range,
40 data[NFT_REG_VERDICT].verdict = verdict; 39 pkt->ops->hooknum);
41} 40}
42 41
43static struct nft_expr_type nft_redir_ipv6_type; 42static struct nft_expr_type nft_redir_ipv6_type;
diff --git a/net/ipv6/netfilter/nft_reject_ipv6.c b/net/ipv6/netfilter/nft_reject_ipv6.c
index f73285924144..d0d1540ecf87 100644
--- a/net/ipv6/netfilter/nft_reject_ipv6.c
+++ b/net/ipv6/netfilter/nft_reject_ipv6.c
@@ -20,7 +20,7 @@
20#include <net/netfilter/ipv6/nf_reject.h> 20#include <net/netfilter/ipv6/nf_reject.h>
21 21
22static void nft_reject_ipv6_eval(const struct nft_expr *expr, 22static void nft_reject_ipv6_eval(const struct nft_expr *expr,
23 struct nft_data data[NFT_REG_MAX + 1], 23 struct nft_regs *regs,
24 const struct nft_pktinfo *pkt) 24 const struct nft_pktinfo *pkt)
25{ 25{
26 struct nft_reject *priv = nft_expr_priv(expr); 26 struct nft_reject *priv = nft_expr_priv(expr);
@@ -34,9 +34,11 @@ static void nft_reject_ipv6_eval(const struct nft_expr *expr,
34 case NFT_REJECT_TCP_RST: 34 case NFT_REJECT_TCP_RST:
35 nf_send_reset6(net, pkt->skb, pkt->ops->hooknum); 35 nf_send_reset6(net, pkt->skb, pkt->ops->hooknum);
36 break; 36 break;
37 default:
38 break;
37 } 39 }
38 40
39 data[NFT_REG_VERDICT].verdict = NF_DROP; 41 regs->verdict.code = NF_DROP;
40} 42}
41 43
42static struct nft_expr_type nft_reject_ipv6_type; 44static struct nft_expr_type nft_reject_ipv6_type;
diff --git a/net/ipv6/output_core.c b/net/ipv6/output_core.c
index 74581f706c4d..85892af57364 100644
--- a/net/ipv6/output_core.c
+++ b/net/ipv6/output_core.c
@@ -9,13 +9,14 @@
9#include <net/addrconf.h> 9#include <net/addrconf.h>
10#include <net/secure_seq.h> 10#include <net/secure_seq.h>
11 11
12static u32 __ipv6_select_ident(u32 hashrnd, struct in6_addr *dst, 12static u32 __ipv6_select_ident(struct net *net, u32 hashrnd,
13 struct in6_addr *src) 13 struct in6_addr *dst, struct in6_addr *src)
14{ 14{
15 u32 hash, id; 15 u32 hash, id;
16 16
17 hash = __ipv6_addr_jhash(dst, hashrnd); 17 hash = __ipv6_addr_jhash(dst, hashrnd);
18 hash = __ipv6_addr_jhash(src, hash); 18 hash = __ipv6_addr_jhash(src, hash);
19 hash ^= net_hash_mix(net);
19 20
20 /* Treat id of 0 as unset and if we get 0 back from ip_idents_reserve, 21 /* Treat id of 0 as unset and if we get 0 back from ip_idents_reserve,
21 * set the hight order instead thus minimizing possible future 22 * set the hight order instead thus minimizing possible future
@@ -36,7 +37,7 @@ static u32 __ipv6_select_ident(u32 hashrnd, struct in6_addr *dst,
36 * 37 *
37 * The network header must be set before calling this. 38 * The network header must be set before calling this.
38 */ 39 */
39void ipv6_proxy_select_ident(struct sk_buff *skb) 40void ipv6_proxy_select_ident(struct net *net, struct sk_buff *skb)
40{ 41{
41 static u32 ip6_proxy_idents_hashrnd __read_mostly; 42 static u32 ip6_proxy_idents_hashrnd __read_mostly;
42 struct in6_addr buf[2]; 43 struct in6_addr buf[2];
@@ -53,20 +54,21 @@ void ipv6_proxy_select_ident(struct sk_buff *skb)
53 net_get_random_once(&ip6_proxy_idents_hashrnd, 54 net_get_random_once(&ip6_proxy_idents_hashrnd,
54 sizeof(ip6_proxy_idents_hashrnd)); 55 sizeof(ip6_proxy_idents_hashrnd));
55 56
56 id = __ipv6_select_ident(ip6_proxy_idents_hashrnd, 57 id = __ipv6_select_ident(net, ip6_proxy_idents_hashrnd,
57 &addrs[1], &addrs[0]); 58 &addrs[1], &addrs[0]);
58 skb_shinfo(skb)->ip6_frag_id = htonl(id); 59 skb_shinfo(skb)->ip6_frag_id = htonl(id);
59} 60}
60EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident); 61EXPORT_SYMBOL_GPL(ipv6_proxy_select_ident);
61 62
62void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt) 63void ipv6_select_ident(struct net *net, struct frag_hdr *fhdr,
64 struct rt6_info *rt)
63{ 65{
64 static u32 ip6_idents_hashrnd __read_mostly; 66 static u32 ip6_idents_hashrnd __read_mostly;
65 u32 id; 67 u32 id;
66 68
67 net_get_random_once(&ip6_idents_hashrnd, sizeof(ip6_idents_hashrnd)); 69 net_get_random_once(&ip6_idents_hashrnd, sizeof(ip6_idents_hashrnd));
68 70
69 id = __ipv6_select_ident(ip6_idents_hashrnd, &rt->rt6i_dst.addr, 71 id = __ipv6_select_ident(net, ip6_idents_hashrnd, &rt->rt6i_dst.addr,
70 &rt->rt6i_src.addr); 72 &rt->rt6i_src.addr);
71 fhdr->identification = htonl(id); 73 fhdr->identification = htonl(id);
72} 74}
@@ -134,7 +136,7 @@ int ip6_dst_hoplimit(struct dst_entry *dst)
134EXPORT_SYMBOL(ip6_dst_hoplimit); 136EXPORT_SYMBOL(ip6_dst_hoplimit);
135#endif 137#endif
136 138
137int __ip6_local_out(struct sk_buff *skb) 139static int __ip6_local_out_sk(struct sock *sk, struct sk_buff *skb)
138{ 140{
139 int len; 141 int len;
140 142
@@ -144,19 +146,30 @@ int __ip6_local_out(struct sk_buff *skb)
144 ipv6_hdr(skb)->payload_len = htons(len); 146 ipv6_hdr(skb)->payload_len = htons(len);
145 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr); 147 IP6CB(skb)->nhoff = offsetof(struct ipv6hdr, nexthdr);
146 148
147 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, 149 return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
148 skb_dst(skb)->dev, dst_output); 150 NULL, skb_dst(skb)->dev, dst_output_sk);
151}
152
153int __ip6_local_out(struct sk_buff *skb)
154{
155 return __ip6_local_out_sk(skb->sk, skb);
149} 156}
150EXPORT_SYMBOL_GPL(__ip6_local_out); 157EXPORT_SYMBOL_GPL(__ip6_local_out);
151 158
152int ip6_local_out(struct sk_buff *skb) 159int ip6_local_out_sk(struct sock *sk, struct sk_buff *skb)
153{ 160{
154 int err; 161 int err;
155 162
156 err = __ip6_local_out(skb); 163 err = __ip6_local_out_sk(sk, skb);
157 if (likely(err == 1)) 164 if (likely(err == 1))
158 err = dst_output(skb); 165 err = dst_output_sk(sk, skb);
159 166
160 return err; 167 return err;
161} 168}
169EXPORT_SYMBOL_GPL(ip6_local_out_sk);
170
171int ip6_local_out(struct sk_buff *skb)
172{
173 return ip6_local_out_sk(skb->sk, skb);
174}
162EXPORT_SYMBOL_GPL(ip6_local_out); 175EXPORT_SYMBOL_GPL(ip6_local_out);
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index a2dfff6ff227..263a5164a6f5 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -77,8 +77,7 @@ static int dummy_ipv6_chk_addr(struct net *net, const struct in6_addr *addr,
77 return 0; 77 return 0;
78} 78}
79 79
80int ping_v6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 80int ping_v6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
81 size_t len)
82{ 81{
83 struct inet_sock *inet = inet_sk(sk); 82 struct inet_sock *inet = inet_sk(sk);
84 struct ipv6_pinfo *np = inet6_sk(sk); 83 struct ipv6_pinfo *np = inet6_sk(sk);
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index dae7f1a1e464..8072bd4139b7 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -32,7 +32,7 @@
32#include <linux/netfilter_ipv6.h> 32#include <linux/netfilter_ipv6.h>
33#include <linux/skbuff.h> 33#include <linux/skbuff.h>
34#include <linux/compat.h> 34#include <linux/compat.h>
35#include <asm/uaccess.h> 35#include <linux/uaccess.h>
36#include <asm/ioctls.h> 36#include <asm/ioctls.h>
37 37
38#include <net/net_namespace.h> 38#include <net/net_namespace.h>
@@ -172,7 +172,7 @@ static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
172 read_lock(&raw_v6_hashinfo.lock); 172 read_lock(&raw_v6_hashinfo.lock);
173 sk = sk_head(&raw_v6_hashinfo.ht[hash]); 173 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
174 174
175 if (sk == NULL) 175 if (!sk)
176 goto out; 176 goto out;
177 177
178 net = dev_net(skb->dev); 178 net = dev_net(skb->dev);
@@ -367,7 +367,7 @@ void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
367 367
368 read_lock(&raw_v6_hashinfo.lock); 368 read_lock(&raw_v6_hashinfo.lock);
369 sk = sk_head(&raw_v6_hashinfo.ht[hash]); 369 sk = sk_head(&raw_v6_hashinfo.ht[hash]);
370 if (sk != NULL) { 370 if (sk) {
371 /* Note: ipv6_hdr(skb) != skb->data */ 371 /* Note: ipv6_hdr(skb) != skb->data */
372 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data; 372 const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
373 saddr = &ip6h->saddr; 373 saddr = &ip6h->saddr;
@@ -456,9 +456,8 @@ int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
456 * we return it, otherwise we block. 456 * we return it, otherwise we block.
457 */ 457 */
458 458
459static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk, 459static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
460 struct msghdr *msg, size_t len, 460 int noblock, int flags, int *addr_len)
461 int noblock, int flags, int *addr_len)
462{ 461{
463 struct ipv6_pinfo *np = inet6_sk(sk); 462 struct ipv6_pinfo *np = inet6_sk(sk);
464 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 463 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
@@ -631,7 +630,7 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
631 skb = sock_alloc_send_skb(sk, 630 skb = sock_alloc_send_skb(sk,
632 length + hlen + tlen + 15, 631 length + hlen + tlen + 15,
633 flags & MSG_DONTWAIT, &err); 632 flags & MSG_DONTWAIT, &err);
634 if (skb == NULL) 633 if (!skb)
635 goto error; 634 goto error;
636 skb_reserve(skb, hlen); 635 skb_reserve(skb, hlen);
637 636
@@ -653,8 +652,8 @@ static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
653 goto error_fault; 652 goto error_fault;
654 653
655 IP6_UPD_PO_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len); 654 IP6_UPD_PO_STATS(sock_net(sk), rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
656 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, 655 err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, sk, skb,
657 rt->dst.dev, dst_output); 656 NULL, rt->dst.dev, dst_output_sk);
658 if (err > 0) 657 if (err > 0)
659 err = net_xmit_errno(err); 658 err = net_xmit_errno(err);
660 if (err) 659 if (err)
@@ -730,8 +729,7 @@ static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
730 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb); 729 return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
731} 730}
732 731
733static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk, 732static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
734 struct msghdr *msg, size_t len)
735{ 733{
736 struct ipv6_txoptions opt_space; 734 struct ipv6_txoptions opt_space;
737 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name); 735 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
@@ -791,7 +789,7 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
791 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 789 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
792 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 790 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
793 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 791 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
794 if (flowlabel == NULL) 792 if (!flowlabel)
795 return -EINVAL; 793 return -EINVAL;
796 } 794 }
797 } 795 }
@@ -833,13 +831,13 @@ static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
833 } 831 }
834 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 832 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
835 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 833 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
836 if (flowlabel == NULL) 834 if (!flowlabel)
837 return -EINVAL; 835 return -EINVAL;
838 } 836 }
839 if (!(opt->opt_nflen|opt->opt_flen)) 837 if (!(opt->opt_nflen|opt->opt_flen))
840 opt = NULL; 838 opt = NULL;
841 } 839 }
842 if (opt == NULL) 840 if (!opt)
843 opt = np->opt; 841 opt = np->opt;
844 if (flowlabel) 842 if (flowlabel)
845 opt = fl6_merge_options(&opt_space, flowlabel, opt); 843 opt = fl6_merge_options(&opt_space, flowlabel, opt);
@@ -1132,7 +1130,7 @@ static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
1132 1130
1133 spin_lock_bh(&sk->sk_receive_queue.lock); 1131 spin_lock_bh(&sk->sk_receive_queue.lock);
1134 skb = skb_peek(&sk->sk_receive_queue); 1132 skb = skb_peek(&sk->sk_receive_queue);
1135 if (skb != NULL) 1133 if (skb)
1136 amount = skb_tail_pointer(skb) - 1134 amount = skb_tail_pointer(skb) -
1137 skb_transport_header(skb); 1135 skb_transport_header(skb);
1138 spin_unlock_bh(&sk->sk_receive_queue.lock); 1136 spin_unlock_bh(&sk->sk_receive_queue.lock);
diff --git a/net/ipv6/reassembly.c b/net/ipv6/reassembly.c
index d7d70e69973b..8ffa2c8cce77 100644
--- a/net/ipv6/reassembly.c
+++ b/net/ipv6/reassembly.c
@@ -430,7 +430,7 @@ static int ip6_frag_reasm(struct frag_queue *fq, struct sk_buff *prev,
430 int i, plen = 0; 430 int i, plen = 0;
431 431
432 clone = alloc_skb(0, GFP_ATOMIC); 432 clone = alloc_skb(0, GFP_ATOMIC);
433 if (clone == NULL) 433 if (!clone)
434 goto out_oom; 434 goto out_oom;
435 clone->next = head->next; 435 clone->next = head->next;
436 head->next = clone; 436 head->next = clone;
@@ -552,7 +552,7 @@ static int ipv6_frag_rcv(struct sk_buff *skb)
552 552
553 fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr, 553 fq = fq_find(net, fhdr->identification, &hdr->saddr, &hdr->daddr,
554 ip6_frag_ecn(hdr)); 554 ip6_frag_ecn(hdr));
555 if (fq != NULL) { 555 if (fq) {
556 int ret; 556 int ret;
557 557
558 spin_lock(&fq->q.lock); 558 spin_lock(&fq->q.lock);
@@ -632,7 +632,7 @@ static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
632 table = ip6_frags_ns_ctl_table; 632 table = ip6_frags_ns_ctl_table;
633 if (!net_eq(net, &init_net)) { 633 if (!net_eq(net, &init_net)) {
634 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL); 634 table = kmemdup(table, sizeof(ip6_frags_ns_ctl_table), GFP_KERNEL);
635 if (table == NULL) 635 if (!table)
636 goto err_alloc; 636 goto err_alloc;
637 637
638 table[0].data = &net->ipv6.frags.high_thresh; 638 table[0].data = &net->ipv6.frags.high_thresh;
@@ -648,7 +648,7 @@ static int __net_init ip6_frags_ns_sysctl_register(struct net *net)
648 } 648 }
649 649
650 hdr = register_net_sysctl(net, "net/ipv6", table); 650 hdr = register_net_sysctl(net, "net/ipv6", table);
651 if (hdr == NULL) 651 if (!hdr)
652 goto err_reg; 652 goto err_reg;
653 653
654 net->ipv6.sysctl.frags_hdr = hdr; 654 net->ipv6.sysctl.frags_hdr = hdr;
diff --git a/net/ipv6/route.c b/net/ipv6/route.c
index 4688bd4d7f59..5c48293ff062 100644
--- a/net/ipv6/route.c
+++ b/net/ipv6/route.c
@@ -194,7 +194,6 @@ static struct neighbour *ip6_neigh_lookup(const struct dst_entry *dst,
194 194
195static struct dst_ops ip6_dst_ops_template = { 195static struct dst_ops ip6_dst_ops_template = {
196 .family = AF_INET6, 196 .family = AF_INET6,
197 .protocol = cpu_to_be16(ETH_P_IPV6),
198 .gc = ip6_dst_gc, 197 .gc = ip6_dst_gc,
199 .gc_thresh = 1024, 198 .gc_thresh = 1024,
200 .check = ip6_dst_check, 199 .check = ip6_dst_check,
@@ -236,7 +235,6 @@ static u32 *ip6_rt_blackhole_cow_metrics(struct dst_entry *dst,
236 235
237static struct dst_ops ip6_dst_blackhole_ops = { 236static struct dst_ops ip6_dst_blackhole_ops = {
238 .family = AF_INET6, 237 .family = AF_INET6,
239 .protocol = cpu_to_be16(ETH_P_IPV6),
240 .destroy = ip6_dst_destroy, 238 .destroy = ip6_dst_destroy,
241 .check = ip6_dst_check, 239 .check = ip6_dst_check,
242 .mtu = ip6_blackhole_mtu, 240 .mtu = ip6_blackhole_mtu,
@@ -1478,7 +1476,7 @@ static int ip6_convert_metrics(struct mx6_config *mxc,
1478 int remaining; 1476 int remaining;
1479 u32 *mp; 1477 u32 *mp;
1480 1478
1481 if (cfg->fc_mx == NULL) 1479 if (!cfg->fc_mx)
1482 return 0; 1480 return 0;
1483 1481
1484 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL); 1482 mp = kzalloc(sizeof(u32) * RTAX_MAX, GFP_KERNEL);
@@ -2400,6 +2398,7 @@ static const struct nla_policy rtm_ipv6_policy[RTA_MAX+1] = {
2400 [RTA_PRIORITY] = { .type = NLA_U32 }, 2398 [RTA_PRIORITY] = { .type = NLA_U32 },
2401 [RTA_METRICS] = { .type = NLA_NESTED }, 2399 [RTA_METRICS] = { .type = NLA_NESTED },
2402 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2400 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2401 [RTA_PREF] = { .type = NLA_U8 },
2403}; 2402};
2404 2403
2405static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh, 2404static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
@@ -2407,6 +2406,7 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2407{ 2406{
2408 struct rtmsg *rtm; 2407 struct rtmsg *rtm;
2409 struct nlattr *tb[RTA_MAX+1]; 2408 struct nlattr *tb[RTA_MAX+1];
2409 unsigned int pref;
2410 int err; 2410 int err;
2411 2411
2412 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy); 2412 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_ipv6_policy);
@@ -2438,7 +2438,7 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2438 cfg->fc_nlinfo.nl_net = sock_net(skb->sk); 2438 cfg->fc_nlinfo.nl_net = sock_net(skb->sk);
2439 2439
2440 if (tb[RTA_GATEWAY]) { 2440 if (tb[RTA_GATEWAY]) {
2441 nla_memcpy(&cfg->fc_gateway, tb[RTA_GATEWAY], 16); 2441 cfg->fc_gateway = nla_get_in6_addr(tb[RTA_GATEWAY]);
2442 cfg->fc_flags |= RTF_GATEWAY; 2442 cfg->fc_flags |= RTF_GATEWAY;
2443 } 2443 }
2444 2444
@@ -2461,7 +2461,7 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2461 } 2461 }
2462 2462
2463 if (tb[RTA_PREFSRC]) 2463 if (tb[RTA_PREFSRC])
2464 nla_memcpy(&cfg->fc_prefsrc, tb[RTA_PREFSRC], 16); 2464 cfg->fc_prefsrc = nla_get_in6_addr(tb[RTA_PREFSRC]);
2465 2465
2466 if (tb[RTA_OIF]) 2466 if (tb[RTA_OIF])
2467 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]); 2467 cfg->fc_ifindex = nla_get_u32(tb[RTA_OIF]);
@@ -2482,6 +2482,14 @@ static int rtm_to_fib6_config(struct sk_buff *skb, struct nlmsghdr *nlh,
2482 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]); 2482 cfg->fc_mp_len = nla_len(tb[RTA_MULTIPATH]);
2483 } 2483 }
2484 2484
2485 if (tb[RTA_PREF]) {
2486 pref = nla_get_u8(tb[RTA_PREF]);
2487 if (pref != ICMPV6_ROUTER_PREF_LOW &&
2488 pref != ICMPV6_ROUTER_PREF_HIGH)
2489 pref = ICMPV6_ROUTER_PREF_MEDIUM;
2490 cfg->fc_flags |= RTF_PREF(pref);
2491 }
2492
2485 err = 0; 2493 err = 0;
2486errout: 2494errout:
2487 return err; 2495 return err;
@@ -2511,7 +2519,7 @@ beginning:
2511 2519
2512 nla = nla_find(attrs, attrlen, RTA_GATEWAY); 2520 nla = nla_find(attrs, attrlen, RTA_GATEWAY);
2513 if (nla) { 2521 if (nla) {
2514 nla_memcpy(&r_cfg.fc_gateway, nla, 16); 2522 r_cfg.fc_gateway = nla_get_in6_addr(nla);
2515 r_cfg.fc_flags |= RTF_GATEWAY; 2523 r_cfg.fc_flags |= RTF_GATEWAY;
2516 } 2524 }
2517 } 2525 }
@@ -2585,7 +2593,8 @@ static inline size_t rt6_nlmsg_size(void)
2585 + nla_total_size(4) /* RTA_PRIORITY */ 2593 + nla_total_size(4) /* RTA_PRIORITY */
2586 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */ 2594 + RTAX_MAX * nla_total_size(4) /* RTA_METRICS */
2587 + nla_total_size(sizeof(struct rta_cacheinfo)) 2595 + nla_total_size(sizeof(struct rta_cacheinfo))
2588 + nla_total_size(TCP_CA_NAME_MAX); /* RTAX_CC_ALGO */ 2596 + nla_total_size(TCP_CA_NAME_MAX) /* RTAX_CC_ALGO */
2597 + nla_total_size(1); /* RTA_PREF */
2589} 2598}
2590 2599
2591static int rt6_fill_node(struct net *net, 2600static int rt6_fill_node(struct net *net,
@@ -2660,19 +2669,19 @@ static int rt6_fill_node(struct net *net,
2660 rtm->rtm_flags |= RTM_F_CLONED; 2669 rtm->rtm_flags |= RTM_F_CLONED;
2661 2670
2662 if (dst) { 2671 if (dst) {
2663 if (nla_put(skb, RTA_DST, 16, dst)) 2672 if (nla_put_in6_addr(skb, RTA_DST, dst))
2664 goto nla_put_failure; 2673 goto nla_put_failure;
2665 rtm->rtm_dst_len = 128; 2674 rtm->rtm_dst_len = 128;
2666 } else if (rtm->rtm_dst_len) 2675 } else if (rtm->rtm_dst_len)
2667 if (nla_put(skb, RTA_DST, 16, &rt->rt6i_dst.addr)) 2676 if (nla_put_in6_addr(skb, RTA_DST, &rt->rt6i_dst.addr))
2668 goto nla_put_failure; 2677 goto nla_put_failure;
2669#ifdef CONFIG_IPV6_SUBTREES 2678#ifdef CONFIG_IPV6_SUBTREES
2670 if (src) { 2679 if (src) {
2671 if (nla_put(skb, RTA_SRC, 16, src)) 2680 if (nla_put_in6_addr(skb, RTA_SRC, src))
2672 goto nla_put_failure; 2681 goto nla_put_failure;
2673 rtm->rtm_src_len = 128; 2682 rtm->rtm_src_len = 128;
2674 } else if (rtm->rtm_src_len && 2683 } else if (rtm->rtm_src_len &&
2675 nla_put(skb, RTA_SRC, 16, &rt->rt6i_src.addr)) 2684 nla_put_in6_addr(skb, RTA_SRC, &rt->rt6i_src.addr))
2676 goto nla_put_failure; 2685 goto nla_put_failure;
2677#endif 2686#endif
2678 if (iif) { 2687 if (iif) {
@@ -2696,14 +2705,14 @@ static int rt6_fill_node(struct net *net,
2696 } else if (dst) { 2705 } else if (dst) {
2697 struct in6_addr saddr_buf; 2706 struct in6_addr saddr_buf;
2698 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 && 2707 if (ip6_route_get_saddr(net, rt, dst, 0, &saddr_buf) == 0 &&
2699 nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2708 nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2700 goto nla_put_failure; 2709 goto nla_put_failure;
2701 } 2710 }
2702 2711
2703 if (rt->rt6i_prefsrc.plen) { 2712 if (rt->rt6i_prefsrc.plen) {
2704 struct in6_addr saddr_buf; 2713 struct in6_addr saddr_buf;
2705 saddr_buf = rt->rt6i_prefsrc.addr; 2714 saddr_buf = rt->rt6i_prefsrc.addr;
2706 if (nla_put(skb, RTA_PREFSRC, 16, &saddr_buf)) 2715 if (nla_put_in6_addr(skb, RTA_PREFSRC, &saddr_buf))
2707 goto nla_put_failure; 2716 goto nla_put_failure;
2708 } 2717 }
2709 2718
@@ -2711,7 +2720,7 @@ static int rt6_fill_node(struct net *net,
2711 goto nla_put_failure; 2720 goto nla_put_failure;
2712 2721
2713 if (rt->rt6i_flags & RTF_GATEWAY) { 2722 if (rt->rt6i_flags & RTF_GATEWAY) {
2714 if (nla_put(skb, RTA_GATEWAY, 16, &rt->rt6i_gateway) < 0) 2723 if (nla_put_in6_addr(skb, RTA_GATEWAY, &rt->rt6i_gateway) < 0)
2715 goto nla_put_failure; 2724 goto nla_put_failure;
2716 } 2725 }
2717 2726
@@ -2726,6 +2735,9 @@ static int rt6_fill_node(struct net *net,
2726 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0) 2735 if (rtnl_put_cacheinfo(skb, &rt->dst, 0, expires, rt->dst.error) < 0)
2727 goto nla_put_failure; 2736 goto nla_put_failure;
2728 2737
2738 if (nla_put_u8(skb, RTA_PREF, IPV6_EXTRACT_PREF(rt->rt6i_flags)))
2739 goto nla_put_failure;
2740
2729 nlmsg_end(skb, nlh); 2741 nlmsg_end(skb, nlh);
2730 return 0; 2742 return 0;
2731 2743
diff --git a/net/ipv6/sit.c b/net/ipv6/sit.c
index e4cbd5798eba..ac35a28599be 100644
--- a/net/ipv6/sit.c
+++ b/net/ipv6/sit.c
@@ -118,7 +118,7 @@ static struct ip_tunnel *ipip6_tunnel_lookup(struct net *net,
118 return t; 118 return t;
119 } 119 }
120 t = rcu_dereference(sitn->tunnels_wc[0]); 120 t = rcu_dereference(sitn->tunnels_wc[0]);
121 if ((t != NULL) && (t->dev->flags & IFF_UP)) 121 if (t && (t->dev->flags & IFF_UP))
122 return t; 122 return t;
123 return NULL; 123 return NULL;
124} 124}
@@ -251,7 +251,7 @@ static struct ip_tunnel *ipip6_tunnel_locate(struct net *net,
251 251
252 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN, 252 dev = alloc_netdev(sizeof(*t), name, NET_NAME_UNKNOWN,
253 ipip6_tunnel_setup); 253 ipip6_tunnel_setup);
254 if (dev == NULL) 254 if (!dev)
255 return NULL; 255 return NULL;
256 256
257 dev_net_set(dev, net); 257 dev_net_set(dev, net);
@@ -555,7 +555,7 @@ static int ipip6_err(struct sk_buff *skb, u32 info)
555 skb->dev, 555 skb->dev,
556 iph->daddr, 556 iph->daddr,
557 iph->saddr); 557 iph->saddr);
558 if (t == NULL) 558 if (!t)
559 goto out; 559 goto out;
560 560
561 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) { 561 if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED) {
@@ -671,7 +671,7 @@ static int ipip6_rcv(struct sk_buff *skb)
671 671
672 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, 672 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
673 iph->saddr, iph->daddr); 673 iph->saddr, iph->daddr);
674 if (tunnel != NULL) { 674 if (tunnel) {
675 struct pcpu_sw_netstats *tstats; 675 struct pcpu_sw_netstats *tstats;
676 676
677 if (tunnel->parms.iph.protocol != IPPROTO_IPV6 && 677 if (tunnel->parms.iph.protocol != IPPROTO_IPV6 &&
@@ -733,7 +733,7 @@ static int ipip_rcv(struct sk_buff *skb)
733 iph = ip_hdr(skb); 733 iph = ip_hdr(skb);
734 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev, 734 tunnel = ipip6_tunnel_lookup(dev_net(skb->dev), skb->dev,
735 iph->saddr, iph->daddr); 735 iph->saddr, iph->daddr);
736 if (tunnel != NULL) { 736 if (tunnel) {
737 if (tunnel->parms.iph.protocol != IPPROTO_IPIP && 737 if (tunnel->parms.iph.protocol != IPPROTO_IPIP &&
738 tunnel->parms.iph.protocol != 0) 738 tunnel->parms.iph.protocol != 0)
739 goto drop; 739 goto drop;
@@ -838,7 +838,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
838 if (skb_dst(skb)) 838 if (skb_dst(skb))
839 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr); 839 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
840 840
841 if (neigh == NULL) { 841 if (!neigh) {
842 net_dbg_ratelimited("nexthop == NULL\n"); 842 net_dbg_ratelimited("nexthop == NULL\n");
843 goto tx_error; 843 goto tx_error;
844 } 844 }
@@ -867,7 +867,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
867 if (skb_dst(skb)) 867 if (skb_dst(skb))
868 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr); 868 neigh = dst_neigh_lookup(skb_dst(skb), &iph6->daddr);
869 869
870 if (neigh == NULL) { 870 if (!neigh) {
871 net_dbg_ratelimited("nexthop == NULL\n"); 871 net_dbg_ratelimited("nexthop == NULL\n");
872 goto tx_error; 872 goto tx_error;
873 } 873 }
@@ -983,7 +983,7 @@ static netdev_tx_t ipip6_tunnel_xmit(struct sk_buff *skb,
983 983
984 skb_set_inner_ipproto(skb, IPPROTO_IPV6); 984 skb_set_inner_ipproto(skb, IPPROTO_IPV6);
985 985
986 err = iptunnel_xmit(skb->sk, rt, skb, fl4.saddr, fl4.daddr, 986 err = iptunnel_xmit(NULL, rt, skb, fl4.saddr, fl4.daddr,
987 protocol, tos, ttl, df, 987 protocol, tos, ttl, df,
988 !net_eq(tunnel->net, dev_net(dev))); 988 !net_eq(tunnel->net, dev_net(dev)));
989 iptunnel_xmit_stats(err, &dev->stats, dev->tstats); 989 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
@@ -1076,7 +1076,6 @@ static void ipip6_tunnel_bind_dev(struct net_device *dev)
1076 if (dev->mtu < IPV6_MIN_MTU) 1076 if (dev->mtu < IPV6_MIN_MTU)
1077 dev->mtu = IPV6_MIN_MTU; 1077 dev->mtu = IPV6_MIN_MTU;
1078 } 1078 }
1079 dev->iflink = tunnel->parms.link;
1080} 1079}
1081 1080
1082static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p) 1081static void ipip6_tunnel_update(struct ip_tunnel *t, struct ip_tunnel_parm *p)
@@ -1158,7 +1157,7 @@ ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1158 break; 1157 break;
1159 } 1158 }
1160 t = ipip6_tunnel_locate(net, &p, 0); 1159 t = ipip6_tunnel_locate(net, &p, 0);
1161 if (t == NULL) 1160 if (!t)
1162 t = netdev_priv(dev); 1161 t = netdev_priv(dev);
1163 } 1162 }
1164 1163
@@ -1206,7 +1205,7 @@ ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1206 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL); 1205 t = ipip6_tunnel_locate(net, &p, cmd == SIOCADDTUNNEL);
1207 1206
1208 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) { 1207 if (dev != sitn->fb_tunnel_dev && cmd == SIOCCHGTUNNEL) {
1209 if (t != NULL) { 1208 if (t) {
1210 if (t->dev != dev) { 1209 if (t->dev != dev) {
1211 err = -EEXIST; 1210 err = -EEXIST;
1212 break; 1211 break;
@@ -1242,7 +1241,7 @@ ipip6_tunnel_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1242 goto done; 1241 goto done;
1243 err = -ENOENT; 1242 err = -ENOENT;
1244 t = ipip6_tunnel_locate(net, &p, 0); 1243 t = ipip6_tunnel_locate(net, &p, 0);
1245 if (t == NULL) 1244 if (!t)
1246 goto done; 1245 goto done;
1247 err = -EPERM; 1246 err = -EPERM;
1248 if (t == netdev_priv(sitn->fb_tunnel_dev)) 1247 if (t == netdev_priv(sitn->fb_tunnel_dev))
@@ -1336,6 +1335,7 @@ static const struct net_device_ops ipip6_netdev_ops = {
1336 .ndo_do_ioctl = ipip6_tunnel_ioctl, 1335 .ndo_do_ioctl = ipip6_tunnel_ioctl,
1337 .ndo_change_mtu = ipip6_tunnel_change_mtu, 1336 .ndo_change_mtu = ipip6_tunnel_change_mtu,
1338 .ndo_get_stats64 = ip_tunnel_get_stats64, 1337 .ndo_get_stats64 = ip_tunnel_get_stats64,
1338 .ndo_get_iflink = ip_tunnel_get_iflink,
1339}; 1339};
1340 1340
1341static void ipip6_dev_free(struct net_device *dev) 1341static void ipip6_dev_free(struct net_device *dev)
@@ -1366,7 +1366,6 @@ static void ipip6_tunnel_setup(struct net_device *dev)
1366 dev->mtu = ETH_DATA_LEN - t_hlen; 1366 dev->mtu = ETH_DATA_LEN - t_hlen;
1367 dev->flags = IFF_NOARP; 1367 dev->flags = IFF_NOARP;
1368 netif_keep_dst(dev); 1368 netif_keep_dst(dev);
1369 dev->iflink = 0;
1370 dev->addr_len = 4; 1369 dev->addr_len = 4;
1371 dev->features |= NETIF_F_LLTX; 1370 dev->features |= NETIF_F_LLTX;
1372 dev->features |= SIT_FEATURES; 1371 dev->features |= SIT_FEATURES;
@@ -1530,8 +1529,7 @@ static bool ipip6_netlink_6rd_parms(struct nlattr *data[],
1530 1529
1531 if (data[IFLA_IPTUN_6RD_PREFIX]) { 1530 if (data[IFLA_IPTUN_6RD_PREFIX]) {
1532 ret = true; 1531 ret = true;
1533 nla_memcpy(&ip6rd->prefix, data[IFLA_IPTUN_6RD_PREFIX], 1532 ip6rd->prefix = nla_get_in6_addr(data[IFLA_IPTUN_6RD_PREFIX]);
1534 sizeof(struct in6_addr));
1535 } 1533 }
1536 1534
1537 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) { 1535 if (data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
@@ -1683,8 +1681,8 @@ static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1683 struct ip_tunnel_parm *parm = &tunnel->parms; 1681 struct ip_tunnel_parm *parm = &tunnel->parms;
1684 1682
1685 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) || 1683 if (nla_put_u32(skb, IFLA_IPTUN_LINK, parm->link) ||
1686 nla_put_be32(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) || 1684 nla_put_in_addr(skb, IFLA_IPTUN_LOCAL, parm->iph.saddr) ||
1687 nla_put_be32(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) || 1685 nla_put_in_addr(skb, IFLA_IPTUN_REMOTE, parm->iph.daddr) ||
1688 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) || 1686 nla_put_u8(skb, IFLA_IPTUN_TTL, parm->iph.ttl) ||
1689 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) || 1687 nla_put_u8(skb, IFLA_IPTUN_TOS, parm->iph.tos) ||
1690 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC, 1688 nla_put_u8(skb, IFLA_IPTUN_PMTUDISC,
@@ -1694,10 +1692,10 @@ static int ipip6_fill_info(struct sk_buff *skb, const struct net_device *dev)
1694 goto nla_put_failure; 1692 goto nla_put_failure;
1695 1693
1696#ifdef CONFIG_IPV6_SIT_6RD 1694#ifdef CONFIG_IPV6_SIT_6RD
1697 if (nla_put(skb, IFLA_IPTUN_6RD_PREFIX, sizeof(struct in6_addr), 1695 if (nla_put_in6_addr(skb, IFLA_IPTUN_6RD_PREFIX,
1698 &tunnel->ip6rd.prefix) || 1696 &tunnel->ip6rd.prefix) ||
1699 nla_put_be32(skb, IFLA_IPTUN_6RD_RELAY_PREFIX, 1697 nla_put_in_addr(skb, IFLA_IPTUN_6RD_RELAY_PREFIX,
1700 tunnel->ip6rd.relay_prefix) || 1698 tunnel->ip6rd.relay_prefix) ||
1701 nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN, 1699 nla_put_u16(skb, IFLA_IPTUN_6RD_PREFIXLEN,
1702 tunnel->ip6rd.prefixlen) || 1700 tunnel->ip6rd.prefixlen) ||
1703 nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN, 1701 nla_put_u16(skb, IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
@@ -1795,7 +1793,7 @@ static void __net_exit sit_destroy_tunnels(struct net *net,
1795 struct ip_tunnel *t; 1793 struct ip_tunnel *t;
1796 1794
1797 t = rtnl_dereference(sitn->tunnels[prio][h]); 1795 t = rtnl_dereference(sitn->tunnels[prio][h]);
1798 while (t != NULL) { 1796 while (t) {
1799 /* If dev is in the same netns, it has already 1797 /* If dev is in the same netns, it has already
1800 * been added to the list by the previous loop. 1798 * been added to the list by the previous loop.
1801 */ 1799 */
diff --git a/net/ipv6/syncookies.c b/net/ipv6/syncookies.c
index 7337fc7947e2..21bc2eb53c57 100644
--- a/net/ipv6/syncookies.c
+++ b/net/ipv6/syncookies.c
@@ -49,11 +49,12 @@ static inline struct sock *get_cookie_sock(struct sock *sk, struct sk_buff *skb,
49 struct sock *child; 49 struct sock *child;
50 50
51 child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst); 51 child = icsk->icsk_af_ops->syn_recv_sock(sk, skb, req, dst);
52 if (child) 52 if (child) {
53 atomic_set(&req->rsk_refcnt, 1);
53 inet_csk_reqsk_queue_add(sk, req, child); 54 inet_csk_reqsk_queue_add(sk, req, child);
54 else 55 } else {
55 reqsk_free(req); 56 reqsk_free(req);
56 57 }
57 return child; 58 return child;
58} 59}
59 60
@@ -189,13 +190,13 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
189 goto out; 190 goto out;
190 191
191 ret = NULL; 192 ret = NULL;
192 req = inet_reqsk_alloc(&tcp6_request_sock_ops); 193 req = inet_reqsk_alloc(&tcp6_request_sock_ops, sk);
193 if (!req) 194 if (!req)
194 goto out; 195 goto out;
195 196
196 ireq = inet_rsk(req); 197 ireq = inet_rsk(req);
197 treq = tcp_rsk(req); 198 treq = tcp_rsk(req);
198 treq->listener = NULL; 199 treq->tfo_listener = false;
199 200
200 if (security_inet_conn_request(sk, skb, req)) 201 if (security_inet_conn_request(sk, skb, req))
201 goto out_free; 202 goto out_free;
@@ -220,7 +221,6 @@ struct sock *cookie_v6_check(struct sock *sk, struct sk_buff *skb)
220 221
221 ireq->ir_mark = inet_request_mark(sk, skb); 222 ireq->ir_mark = inet_request_mark(sk, skb);
222 223
223 req->expires = 0UL;
224 req->num_retrans = 0; 224 req->num_retrans = 0;
225 ireq->snd_wscale = tcp_opt.snd_wscale; 225 ireq->snd_wscale = tcp_opt.snd_wscale;
226 ireq->sack_ok = tcp_opt.sack_ok; 226 ireq->sack_ok = tcp_opt.sack_ok;
diff --git a/net/ipv6/sysctl_net_ipv6.c b/net/ipv6/sysctl_net_ipv6.c
index c5c10fafcfe2..abcc79f649b3 100644
--- a/net/ipv6/sysctl_net_ipv6.c
+++ b/net/ipv6/sysctl_net_ipv6.c
@@ -54,6 +54,20 @@ static struct ctl_table ipv6_table_template[] = {
54 .mode = 0644, 54 .mode = 0644,
55 .proc_handler = proc_dointvec 55 .proc_handler = proc_dointvec
56 }, 56 },
57 {
58 .procname = "idgen_retries",
59 .data = &init_net.ipv6.sysctl.idgen_retries,
60 .maxlen = sizeof(int),
61 .mode = 0644,
62 .proc_handler = proc_dointvec,
63 },
64 {
65 .procname = "idgen_delay",
66 .data = &init_net.ipv6.sysctl.idgen_delay,
67 .maxlen = sizeof(int),
68 .mode = 0644,
69 .proc_handler = proc_dointvec_jiffies,
70 },
57 { } 71 { }
58}; 72};
59 73
@@ -93,6 +107,8 @@ static int __net_init ipv6_sysctl_net_init(struct net *net)
93 ipv6_table[2].data = &net->ipv6.sysctl.flowlabel_consistency; 107 ipv6_table[2].data = &net->ipv6.sysctl.flowlabel_consistency;
94 ipv6_table[3].data = &net->ipv6.sysctl.auto_flowlabels; 108 ipv6_table[3].data = &net->ipv6.sysctl.auto_flowlabels;
95 ipv6_table[4].data = &net->ipv6.sysctl.fwmark_reflect; 109 ipv6_table[4].data = &net->ipv6.sysctl.fwmark_reflect;
110 ipv6_table[5].data = &net->ipv6.sysctl.idgen_retries;
111 ipv6_table[6].data = &net->ipv6.sysctl.idgen_delay;
96 112
97 ipv6_route_table = ipv6_route_sysctl_init(net); 113 ipv6_route_table = ipv6_route_sysctl_init(net);
98 if (!ipv6_route_table) 114 if (!ipv6_route_table)
@@ -163,7 +179,7 @@ int ipv6_sysctl_register(void)
163 int err = -ENOMEM; 179 int err = -ENOMEM;
164 180
165 ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable); 181 ip6_header = register_net_sysctl(&init_net, "net/ipv6", ipv6_rotable);
166 if (ip6_header == NULL) 182 if (!ip6_header)
167 goto out; 183 goto out;
168 184
169 err = register_pernet_subsys(&ipv6_sysctl_net_ops); 185 err = register_pernet_subsys(&ipv6_sysctl_net_ops);
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 1f5e62229aaa..ad51df85aa00 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -104,19 +104,6 @@ static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
104 } 104 }
105} 105}
106 106
107static void tcp_v6_hash(struct sock *sk)
108{
109 if (sk->sk_state != TCP_CLOSE) {
110 if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
111 tcp_prot.hash(sk);
112 return;
113 }
114 local_bh_disable();
115 __inet6_hash(sk, NULL);
116 local_bh_enable();
117 }
118}
119
120static __u32 tcp_v6_init_sequence(const struct sk_buff *skb) 107static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
121{ 108{
122 return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32, 109 return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
@@ -154,7 +141,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
154 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 141 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
155 struct ip6_flowlabel *flowlabel; 142 struct ip6_flowlabel *flowlabel;
156 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 143 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
157 if (flowlabel == NULL) 144 if (!flowlabel)
158 return -EINVAL; 145 return -EINVAL;
159 fl6_sock_release(flowlabel); 146 fl6_sock_release(flowlabel);
160 } 147 }
@@ -233,11 +220,8 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
233 tp->af_specific = &tcp_sock_ipv6_specific; 220 tp->af_specific = &tcp_sock_ipv6_specific;
234#endif 221#endif
235 goto failure; 222 goto failure;
236 } else {
237 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
238 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
239 &sk->sk_v6_rcv_saddr);
240 } 223 }
224 np->saddr = sk->sk_v6_rcv_saddr;
241 225
242 return err; 226 return err;
243 } 227 }
@@ -263,7 +247,7 @@ static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
263 goto failure; 247 goto failure;
264 } 248 }
265 249
266 if (saddr == NULL) { 250 if (!saddr) {
267 saddr = &fl6.saddr; 251 saddr = &fl6.saddr;
268 sk->sk_v6_rcv_saddr = *saddr; 252 sk->sk_v6_rcv_saddr = *saddr;
269 } 253 }
@@ -340,18 +324,20 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
340{ 324{
341 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data; 325 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
342 const struct tcphdr *th = (struct tcphdr *)(skb->data+offset); 326 const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
327 struct net *net = dev_net(skb->dev);
328 struct request_sock *fastopen;
343 struct ipv6_pinfo *np; 329 struct ipv6_pinfo *np;
344 struct sock *sk;
345 int err;
346 struct tcp_sock *tp; 330 struct tcp_sock *tp;
347 struct request_sock *fastopen;
348 __u32 seq, snd_una; 331 __u32 seq, snd_una;
349 struct net *net = dev_net(skb->dev); 332 struct sock *sk;
333 int err;
350 334
351 sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr, 335 sk = __inet6_lookup_established(net, &tcp_hashinfo,
352 th->dest, &hdr->saddr, th->source, skb->dev->ifindex); 336 &hdr->daddr, th->dest,
337 &hdr->saddr, ntohs(th->source),
338 skb->dev->ifindex);
353 339
354 if (sk == NULL) { 340 if (!sk) {
355 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), 341 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
356 ICMP6_MIB_INERRORS); 342 ICMP6_MIB_INERRORS);
357 return; 343 return;
@@ -361,6 +347,9 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
361 inet_twsk_put(inet_twsk(sk)); 347 inet_twsk_put(inet_twsk(sk));
362 return; 348 return;
363 } 349 }
350 seq = ntohl(th->seq);
351 if (sk->sk_state == TCP_NEW_SYN_RECV)
352 return tcp_req_err(sk, seq);
364 353
365 bh_lock_sock(sk); 354 bh_lock_sock(sk);
366 if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG) 355 if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)
@@ -375,7 +364,6 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
375 } 364 }
376 365
377 tp = tcp_sk(sk); 366 tp = tcp_sk(sk);
378 seq = ntohl(th->seq);
379 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */ 367 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
380 fastopen = tp->fastopen_rsk; 368 fastopen = tp->fastopen_rsk;
381 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una; 369 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
@@ -419,37 +407,12 @@ static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
419 407
420 /* Might be for an request_sock */ 408 /* Might be for an request_sock */
421 switch (sk->sk_state) { 409 switch (sk->sk_state) {
422 struct request_sock *req, **prev;
423 case TCP_LISTEN:
424 if (sock_owned_by_user(sk))
425 goto out;
426
427 /* Note : We use inet6_iif() here, not tcp_v6_iif() */
428 req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
429 &hdr->saddr, inet6_iif(skb));
430 if (!req)
431 goto out;
432
433 /* ICMPs are not backlogged, hence we cannot get
434 * an established socket here.
435 */
436 WARN_ON(req->sk != NULL);
437
438 if (seq != tcp_rsk(req)->snt_isn) {
439 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
440 goto out;
441 }
442
443 inet_csk_reqsk_queue_drop(sk, req, prev);
444 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
445 goto out;
446
447 case TCP_SYN_SENT: 410 case TCP_SYN_SENT:
448 case TCP_SYN_RECV: 411 case TCP_SYN_RECV:
449 /* Only in fast or simultaneous open. If a fast open socket is 412 /* Only in fast or simultaneous open. If a fast open socket is
450 * is already accepted it is treated as a connected one below. 413 * is already accepted it is treated as a connected one below.
451 */ 414 */
452 if (fastopen && fastopen->sk == NULL) 415 if (fastopen && !fastopen->sk)
453 break; 416 break;
454 417
455 if (!sock_owned_by_user(sk)) { 418 if (!sock_owned_by_user(sk)) {
@@ -497,7 +460,7 @@ static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
497 &ireq->ir_v6_rmt_addr); 460 &ireq->ir_v6_rmt_addr);
498 461
499 fl6->daddr = ireq->ir_v6_rmt_addr; 462 fl6->daddr = ireq->ir_v6_rmt_addr;
500 if (np->repflow && (ireq->pktopts != NULL)) 463 if (np->repflow && ireq->pktopts)
501 fl6->flowlabel = ip6_flowlabel(ipv6_hdr(ireq->pktopts)); 464 fl6->flowlabel = ip6_flowlabel(ipv6_hdr(ireq->pktopts));
502 465
503 skb_set_queue_mapping(skb, queue_mapping); 466 skb_set_queue_mapping(skb, queue_mapping);
@@ -523,17 +486,11 @@ static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
523} 486}
524 487
525static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk, 488static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
526 struct sock *addr_sk) 489 const struct sock *addr_sk)
527{ 490{
528 return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr); 491 return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr);
529} 492}
530 493
531static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
532 struct request_sock *req)
533{
534 return tcp_v6_md5_do_lookup(sk, &inet_rsk(req)->ir_v6_rmt_addr);
535}
536
537static int tcp_v6_parse_md5_keys(struct sock *sk, char __user *optval, 494static int tcp_v6_parse_md5_keys(struct sock *sk, char __user *optval,
538 int optlen) 495 int optlen)
539{ 496{
@@ -619,9 +576,9 @@ clear_hash_noput:
619 return 1; 576 return 1;
620} 577}
621 578
622static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key, 579static int tcp_v6_md5_hash_skb(char *md5_hash,
580 const struct tcp_md5sig_key *key,
623 const struct sock *sk, 581 const struct sock *sk,
624 const struct request_sock *req,
625 const struct sk_buff *skb) 582 const struct sk_buff *skb)
626{ 583{
627 const struct in6_addr *saddr, *daddr; 584 const struct in6_addr *saddr, *daddr;
@@ -629,12 +586,9 @@ static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
629 struct hash_desc *desc; 586 struct hash_desc *desc;
630 const struct tcphdr *th = tcp_hdr(skb); 587 const struct tcphdr *th = tcp_hdr(skb);
631 588
632 if (sk) { 589 if (sk) { /* valid for establish/request sockets */
633 saddr = &inet6_sk(sk)->saddr; 590 saddr = &sk->sk_v6_rcv_saddr;
634 daddr = &sk->sk_v6_daddr; 591 daddr = &sk->sk_v6_daddr;
635 } else if (req) {
636 saddr = &inet_rsk(req)->ir_v6_loc_addr;
637 daddr = &inet_rsk(req)->ir_v6_rmt_addr;
638 } else { 592 } else {
639 const struct ipv6hdr *ip6h = ipv6_hdr(skb); 593 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
640 saddr = &ip6h->saddr; 594 saddr = &ip6h->saddr;
@@ -670,8 +624,7 @@ clear_hash_noput:
670 return 1; 624 return 1;
671} 625}
672 626
673static int __tcp_v6_inbound_md5_hash(struct sock *sk, 627static bool tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
674 const struct sk_buff *skb)
675{ 628{
676 const __u8 *hash_location = NULL; 629 const __u8 *hash_location = NULL;
677 struct tcp_md5sig_key *hash_expected; 630 struct tcp_md5sig_key *hash_expected;
@@ -685,44 +638,32 @@ static int __tcp_v6_inbound_md5_hash(struct sock *sk,
685 638
686 /* We've parsed the options - do we have a hash? */ 639 /* We've parsed the options - do we have a hash? */
687 if (!hash_expected && !hash_location) 640 if (!hash_expected && !hash_location)
688 return 0; 641 return false;
689 642
690 if (hash_expected && !hash_location) { 643 if (hash_expected && !hash_location) {
691 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); 644 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
692 return 1; 645 return true;
693 } 646 }
694 647
695 if (!hash_expected && hash_location) { 648 if (!hash_expected && hash_location) {
696 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED); 649 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
697 return 1; 650 return true;
698 } 651 }
699 652
700 /* check the signature */ 653 /* check the signature */
701 genhash = tcp_v6_md5_hash_skb(newhash, 654 genhash = tcp_v6_md5_hash_skb(newhash,
702 hash_expected, 655 hash_expected,
703 NULL, NULL, skb); 656 NULL, skb);
704 657
705 if (genhash || memcmp(hash_location, newhash, 16) != 0) { 658 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
706 net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n", 659 net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
707 genhash ? "failed" : "mismatch", 660 genhash ? "failed" : "mismatch",
708 &ip6h->saddr, ntohs(th->source), 661 &ip6h->saddr, ntohs(th->source),
709 &ip6h->daddr, ntohs(th->dest)); 662 &ip6h->daddr, ntohs(th->dest));
710 return 1; 663 return true;
711 } 664 }
712 return 0; 665 return false;
713}
714
715static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
716{
717 int ret;
718
719 rcu_read_lock();
720 ret = __tcp_v6_inbound_md5_hash(sk, skb);
721 rcu_read_unlock();
722
723 return ret;
724} 666}
725
726#endif 667#endif
727 668
728static void tcp_v6_init_req(struct request_sock *req, struct sock *sk, 669static void tcp_v6_init_req(struct request_sock *req, struct sock *sk,
@@ -734,8 +675,6 @@ static void tcp_v6_init_req(struct request_sock *req, struct sock *sk,
734 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr; 675 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
735 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr; 676 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
736 677
737 ireq->ir_iif = sk->sk_bound_dev_if;
738
739 /* So that link locals have meaning */ 678 /* So that link locals have meaning */
740 if (!sk->sk_bound_dev_if && 679 if (!sk->sk_bound_dev_if &&
741 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL) 680 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
@@ -774,7 +713,7 @@ static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
774 .mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - 713 .mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
775 sizeof(struct ipv6hdr), 714 sizeof(struct ipv6hdr),
776#ifdef CONFIG_TCP_MD5SIG 715#ifdef CONFIG_TCP_MD5SIG
777 .md5_lookup = tcp_v6_reqsk_md5_lookup, 716 .req_md5_lookup = tcp_v6_md5_lookup,
778 .calc_md5_hash = tcp_v6_md5_hash_skb, 717 .calc_md5_hash = tcp_v6_md5_hash_skb,
779#endif 718#endif
780 .init_req = tcp_v6_init_req, 719 .init_req = tcp_v6_init_req,
@@ -811,7 +750,7 @@ static void tcp_v6_send_response(struct sock *sk, struct sk_buff *skb, u32 seq,
811 750
812 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len, 751 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
813 GFP_ATOMIC); 752 GFP_ATOMIC);
814 if (buff == NULL) 753 if (!buff)
815 return; 754 return;
816 755
817 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len); 756 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
@@ -931,7 +870,7 @@ static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
931 if (!key) 870 if (!key)
932 goto release_sk1; 871 goto release_sk1;
933 872
934 genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, NULL, skb); 873 genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, skb);
935 if (genhash || memcmp(hash_location, newhash, 16) != 0) 874 if (genhash || memcmp(hash_location, newhash, 16) != 0)
936 goto release_sk1; 875 goto release_sk1;
937 } else { 876 } else {
@@ -997,17 +936,19 @@ static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
997 936
998static struct sock *tcp_v6_hnd_req(struct sock *sk, struct sk_buff *skb) 937static struct sock *tcp_v6_hnd_req(struct sock *sk, struct sk_buff *skb)
999{ 938{
1000 struct request_sock *req, **prev;
1001 const struct tcphdr *th = tcp_hdr(skb); 939 const struct tcphdr *th = tcp_hdr(skb);
940 struct request_sock *req;
1002 struct sock *nsk; 941 struct sock *nsk;
1003 942
1004 /* Find possible connection requests. */ 943 /* Find possible connection requests. */
1005 req = inet6_csk_search_req(sk, &prev, th->source, 944 req = inet6_csk_search_req(sk, th->source,
1006 &ipv6_hdr(skb)->saddr, 945 &ipv6_hdr(skb)->saddr,
1007 &ipv6_hdr(skb)->daddr, tcp_v6_iif(skb)); 946 &ipv6_hdr(skb)->daddr, tcp_v6_iif(skb));
1008 if (req) 947 if (req) {
1009 return tcp_check_req(sk, skb, req, prev, false); 948 nsk = tcp_check_req(sk, skb, req, false);
1010 949 reqsk_put(req);
950 return nsk;
951 }
1011 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo, 952 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
1012 &ipv6_hdr(skb)->saddr, th->source, 953 &ipv6_hdr(skb)->saddr, th->source,
1013 &ipv6_hdr(skb)->daddr, ntohs(th->dest), 954 &ipv6_hdr(skb)->daddr, ntohs(th->dest),
@@ -1067,7 +1008,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1067 1008
1068 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst); 1009 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1069 1010
1070 if (newsk == NULL) 1011 if (!newsk)
1071 return NULL; 1012 return NULL;
1072 1013
1073 newtcp6sk = (struct tcp6_sock *)newsk; 1014 newtcp6sk = (struct tcp6_sock *)newsk;
@@ -1079,11 +1020,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1079 1020
1080 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 1021 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1081 1022
1082 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr); 1023 newnp->saddr = newsk->sk_v6_rcv_saddr;
1083
1084 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1085
1086 newsk->sk_v6_rcv_saddr = newnp->saddr;
1087 1024
1088 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; 1025 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1089 newsk->sk_backlog_rcv = tcp_v4_do_rcv; 1026 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
@@ -1128,7 +1065,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1128 } 1065 }
1129 1066
1130 newsk = tcp_create_openreq_child(sk, req, skb); 1067 newsk = tcp_create_openreq_child(sk, req, skb);
1131 if (newsk == NULL) 1068 if (!newsk)
1132 goto out_nonewsk; 1069 goto out_nonewsk;
1133 1070
1134 /* 1071 /*
@@ -1170,7 +1107,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1170 1107
1171 /* Clone pktoptions received with SYN */ 1108 /* Clone pktoptions received with SYN */
1172 newnp->pktoptions = NULL; 1109 newnp->pktoptions = NULL;
1173 if (ireq->pktopts != NULL) { 1110 if (ireq->pktopts) {
1174 newnp->pktoptions = skb_clone(ireq->pktopts, 1111 newnp->pktoptions = skb_clone(ireq->pktopts,
1175 sk_gfp_atomic(sk, GFP_ATOMIC)); 1112 sk_gfp_atomic(sk, GFP_ATOMIC));
1176 consume_skb(ireq->pktopts); 1113 consume_skb(ireq->pktopts);
@@ -1215,7 +1152,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1215#ifdef CONFIG_TCP_MD5SIG 1152#ifdef CONFIG_TCP_MD5SIG
1216 /* Copy over the MD5 key from the original socket */ 1153 /* Copy over the MD5 key from the original socket */
1217 key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr); 1154 key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr);
1218 if (key != NULL) { 1155 if (key) {
1219 /* We're using one, so create a matching key 1156 /* We're using one, so create a matching key
1220 * on the newsk structure. If we fail to get 1157 * on the newsk structure. If we fail to get
1221 * memory, then we end up not copying the key 1158 * memory, then we end up not copying the key
@@ -1232,7 +1169,7 @@ static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1232 tcp_done(newsk); 1169 tcp_done(newsk);
1233 goto out; 1170 goto out;
1234 } 1171 }
1235 __inet6_hash(newsk, NULL); 1172 __inet_hash(newsk, NULL);
1236 1173
1237 return newsk; 1174 return newsk;
1238 1175
@@ -1547,9 +1484,9 @@ do_time_wait:
1547 &ipv6_hdr(skb)->saddr, th->source, 1484 &ipv6_hdr(skb)->saddr, th->source,
1548 &ipv6_hdr(skb)->daddr, 1485 &ipv6_hdr(skb)->daddr,
1549 ntohs(th->dest), tcp_v6_iif(skb)); 1486 ntohs(th->dest), tcp_v6_iif(skb));
1550 if (sk2 != NULL) { 1487 if (sk2) {
1551 struct inet_timewait_sock *tw = inet_twsk(sk); 1488 struct inet_timewait_sock *tw = inet_twsk(sk);
1552 inet_twsk_deschedule(tw, &tcp_death_row); 1489 inet_twsk_deschedule(tw);
1553 inet_twsk_put(tw); 1490 inet_twsk_put(tw);
1554 sk = sk2; 1491 sk = sk2;
1555 tcp_v6_restore_cb(skb); 1492 tcp_v6_restore_cb(skb);
@@ -1595,7 +1532,7 @@ static void tcp_v6_early_demux(struct sk_buff *skb)
1595 if (sk) { 1532 if (sk) {
1596 skb->sk = sk; 1533 skb->sk = sk;
1597 skb->destructor = sock_edemux; 1534 skb->destructor = sock_edemux;
1598 if (sk->sk_state != TCP_TIME_WAIT) { 1535 if (sk_fullsock(sk)) {
1599 struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst); 1536 struct dst_entry *dst = READ_ONCE(sk->sk_rx_dst);
1600 1537
1601 if (dst) 1538 if (dst)
@@ -1700,9 +1637,9 @@ static void tcp_v6_destroy_sock(struct sock *sk)
1700#ifdef CONFIG_PROC_FS 1637#ifdef CONFIG_PROC_FS
1701/* Proc filesystem TCPv6 sock list dumping. */ 1638/* Proc filesystem TCPv6 sock list dumping. */
1702static void get_openreq6(struct seq_file *seq, 1639static void get_openreq6(struct seq_file *seq,
1703 const struct sock *sk, struct request_sock *req, int i, kuid_t uid) 1640 struct request_sock *req, int i, kuid_t uid)
1704{ 1641{
1705 int ttd = req->expires - jiffies; 1642 long ttd = req->rsk_timer.expires - jiffies;
1706 const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr; 1643 const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
1707 const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr; 1644 const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
1708 1645
@@ -1791,9 +1728,9 @@ static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1791static void get_timewait6_sock(struct seq_file *seq, 1728static void get_timewait6_sock(struct seq_file *seq,
1792 struct inet_timewait_sock *tw, int i) 1729 struct inet_timewait_sock *tw, int i)
1793{ 1730{
1731 long delta = tw->tw_timer.expires - jiffies;
1794 const struct in6_addr *dest, *src; 1732 const struct in6_addr *dest, *src;
1795 __u16 destp, srcp; 1733 __u16 destp, srcp;
1796 s32 delta = tw->tw_ttd - inet_tw_time_stamp();
1797 1734
1798 dest = &tw->tw_v6_daddr; 1735 dest = &tw->tw_v6_daddr;
1799 src = &tw->tw_v6_rcv_saddr; 1736 src = &tw->tw_v6_rcv_saddr;
@@ -1838,7 +1775,7 @@ static int tcp6_seq_show(struct seq_file *seq, void *v)
1838 get_tcp6_sock(seq, v, st->num); 1775 get_tcp6_sock(seq, v, st->num);
1839 break; 1776 break;
1840 case TCP_SEQ_STATE_OPENREQ: 1777 case TCP_SEQ_STATE_OPENREQ:
1841 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid); 1778 get_openreq6(seq, v, st->num, st->uid);
1842 break; 1779 break;
1843 } 1780 }
1844out: 1781out:
@@ -1902,7 +1839,7 @@ struct proto tcpv6_prot = {
1902 .sendpage = tcp_sendpage, 1839 .sendpage = tcp_sendpage,
1903 .backlog_rcv = tcp_v6_do_rcv, 1840 .backlog_rcv = tcp_v6_do_rcv,
1904 .release_cb = tcp_release_cb, 1841 .release_cb = tcp_release_cb,
1905 .hash = tcp_v6_hash, 1842 .hash = inet_hash,
1906 .unhash = inet_unhash, 1843 .unhash = inet_unhash,
1907 .get_port = inet_csk_get_port, 1844 .get_port = inet_csk_get_port,
1908 .enter_memory_pressure = tcp_enter_memory_pressure, 1845 .enter_memory_pressure = tcp_enter_memory_pressure,
diff --git a/net/ipv6/tcpv6_offload.c b/net/ipv6/tcpv6_offload.c
index c1ab77105b4c..d883c9204c01 100644
--- a/net/ipv6/tcpv6_offload.c
+++ b/net/ipv6/tcpv6_offload.c
@@ -41,8 +41,8 @@ static int tcp6_gro_complete(struct sk_buff *skb, int thoff)
41 return tcp_gro_complete(skb); 41 return tcp_gro_complete(skb);
42} 42}
43 43
44struct sk_buff *tcp6_gso_segment(struct sk_buff *skb, 44static struct sk_buff *tcp6_gso_segment(struct sk_buff *skb,
45 netdev_features_t features) 45 netdev_features_t features)
46{ 46{
47 struct tcphdr *th; 47 struct tcphdr *th;
48 48
diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c
index d048d46779fc..3477c919fcc8 100644
--- a/net/ipv6/udp.c
+++ b/net/ipv6/udp.c
@@ -53,11 +53,11 @@
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, 56static u32 udp6_ehashfn(const struct net *net,
57 const struct in6_addr *laddr, 57 const struct in6_addr *laddr,
58 const u16 lport, 58 const u16 lport,
59 const struct in6_addr *faddr, 59 const struct in6_addr *faddr,
60 const __be16 fport) 60 const __be16 fport)
61{ 61{
62 static u32 udp6_ehash_secret __read_mostly; 62 static u32 udp6_ehash_secret __read_mostly;
63 static u32 udp_ipv6_hash_secret __read_mostly; 63 static u32 udp_ipv6_hash_secret __read_mostly;
@@ -104,9 +104,9 @@ int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
104 return 0; 104 return 0;
105} 105}
106 106
107static unsigned int udp6_portaddr_hash(struct net *net, 107static u32 udp6_portaddr_hash(const struct net *net,
108 const struct in6_addr *addr6, 108 const struct in6_addr *addr6,
109 unsigned int port) 109 unsigned int port)
110{ 110{
111 unsigned int hash, mix = net_hash_mix(net); 111 unsigned int hash, mix = net_hash_mix(net);
112 112
@@ -120,7 +120,6 @@ static unsigned int udp6_portaddr_hash(struct net *net,
120 return hash ^ port; 120 return hash ^ port;
121} 121}
122 122
123
124int udp_v6_get_port(struct sock *sk, unsigned short snum) 123int udp_v6_get_port(struct sock *sk, unsigned short snum)
125{ 124{
126 unsigned int hash2_nulladdr = 125 unsigned int hash2_nulladdr =
@@ -385,14 +384,12 @@ struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be
385} 384}
386EXPORT_SYMBOL_GPL(udp6_lib_lookup); 385EXPORT_SYMBOL_GPL(udp6_lib_lookup);
387 386
388
389/* 387/*
390 * This should be easy, if there is something there we 388 * This should be easy, if there is something there we
391 * return it, otherwise we block. 389 * return it, otherwise we block.
392 */ 390 */
393 391
394int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, 392int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
395 struct msghdr *msg, size_t len,
396 int noblock, int flags, int *addr_len) 393 int noblock, int flags, int *addr_len)
397{ 394{
398 struct ipv6_pinfo *np = inet6_sk(sk); 395 struct ipv6_pinfo *np = inet6_sk(sk);
@@ -551,7 +548,7 @@ void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
551 548
552 sk = __udp6_lib_lookup(net, daddr, uh->dest, 549 sk = __udp6_lib_lookup(net, daddr, uh->dest,
553 saddr, uh->source, inet6_iif(skb), udptable); 550 saddr, uh->source, inet6_iif(skb), udptable);
554 if (sk == NULL) { 551 if (!sk) {
555 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), 552 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
556 ICMP6_MIB_INERRORS); 553 ICMP6_MIB_INERRORS);
557 return; 554 return;
@@ -649,7 +646,7 @@ int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
649 646
650 /* if we're overly short, let UDP handle it */ 647 /* if we're overly short, let UDP handle it */
651 encap_rcv = ACCESS_ONCE(up->encap_rcv); 648 encap_rcv = ACCESS_ONCE(up->encap_rcv);
652 if (skb->len > sizeof(struct udphdr) && encap_rcv != NULL) { 649 if (skb->len > sizeof(struct udphdr) && encap_rcv) {
653 int ret; 650 int ret;
654 651
655 /* Verify checksum before giving to encap */ 652 /* Verify checksum before giving to encap */
@@ -750,7 +747,7 @@ static void flush_stack(struct sock **stack, unsigned int count,
750 747
751 for (i = 0; i < count; i++) { 748 for (i = 0; i < count; i++) {
752 sk = stack[i]; 749 sk = stack[i];
753 if (likely(skb1 == NULL)) 750 if (likely(!skb1))
754 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC); 751 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
755 if (!skb1) { 752 if (!skb1) {
756 atomic_inc(&sk->sk_drops); 753 atomic_inc(&sk->sk_drops);
@@ -900,7 +897,7 @@ int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
900 * for sock caches... i'll skip this for now. 897 * for sock caches... i'll skip this for now.
901 */ 898 */
902 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable); 899 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
903 if (sk != NULL) { 900 if (sk) {
904 int ret; 901 int ret;
905 902
906 if (!uh->check && !udp_sk(sk)->no_check6_rx) { 903 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
@@ -1101,8 +1098,7 @@ out:
1101 return err; 1098 return err;
1102} 1099}
1103 1100
1104int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, 1101int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1105 struct msghdr *msg, size_t len)
1106{ 1102{
1107 struct ipv6_txoptions opt_space; 1103 struct ipv6_txoptions opt_space;
1108 struct udp_sock *up = udp_sk(sk); 1104 struct udp_sock *up = udp_sk(sk);
@@ -1164,12 +1160,12 @@ int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
1164do_udp_sendmsg: 1160do_udp_sendmsg:
1165 if (__ipv6_only_sock(sk)) 1161 if (__ipv6_only_sock(sk))
1166 return -ENETUNREACH; 1162 return -ENETUNREACH;
1167 return udp_sendmsg(iocb, sk, msg, len); 1163 return udp_sendmsg(sk, msg, len);
1168 } 1164 }
1169 } 1165 }
1170 1166
1171 if (up->pending == AF_INET) 1167 if (up->pending == AF_INET)
1172 return udp_sendmsg(iocb, sk, msg, len); 1168 return udp_sendmsg(sk, msg, len);
1173 1169
1174 /* Rough check on arithmetic overflow, 1170 /* Rough check on arithmetic overflow,
1175 better check is made in ip6_append_data(). 1171 better check is made in ip6_append_data().
@@ -1209,7 +1205,7 @@ do_udp_sendmsg:
1209 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK; 1205 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1210 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { 1206 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1211 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1207 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1212 if (flowlabel == NULL) 1208 if (!flowlabel)
1213 return -EINVAL; 1209 return -EINVAL;
1214 } 1210 }
1215 } 1211 }
@@ -1257,14 +1253,14 @@ do_udp_sendmsg:
1257 } 1253 }
1258 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) { 1254 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1259 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); 1255 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1260 if (flowlabel == NULL) 1256 if (!flowlabel)
1261 return -EINVAL; 1257 return -EINVAL;
1262 } 1258 }
1263 if (!(opt->opt_nflen|opt->opt_flen)) 1259 if (!(opt->opt_nflen|opt->opt_flen))
1264 opt = NULL; 1260 opt = NULL;
1265 connected = 0; 1261 connected = 0;
1266 } 1262 }
1267 if (opt == NULL) 1263 if (!opt)
1268 opt = np->opt; 1264 opt = np->opt;
1269 if (flowlabel) 1265 if (flowlabel)
1270 opt = fl6_merge_options(&opt_space, flowlabel, opt); 1266 opt = fl6_merge_options(&opt_space, flowlabel, opt);
@@ -1557,7 +1553,6 @@ static struct inet_protosw udpv6_protosw = {
1557 .flags = INET_PROTOSW_PERMANENT, 1553 .flags = INET_PROTOSW_PERMANENT,
1558}; 1554};
1559 1555
1560
1561int __init udpv6_init(void) 1556int __init udpv6_init(void)
1562{ 1557{
1563 int ret; 1558 int ret;
diff --git a/net/ipv6/udp_impl.h b/net/ipv6/udp_impl.h
index c779c3c90b9d..0682c031ccdc 100644
--- a/net/ipv6/udp_impl.h
+++ b/net/ipv6/udp_impl.h
@@ -23,10 +23,9 @@ int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
23int 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
26int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 26int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len);
27 size_t len); 27int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int noblock,
28int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 28 int flags, int *addr_len);
29 size_t len, int noblock, int flags, int *addr_len);
30int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb); 29int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb);
31void udpv6_destroy_sock(struct sock *sk); 30void udpv6_destroy_sock(struct sock *sk);
32 31
diff --git a/net/ipv6/udp_offload.c b/net/ipv6/udp_offload.c
index be2c0ba82c85..7441e1e63893 100644
--- a/net/ipv6/udp_offload.c
+++ b/net/ipv6/udp_offload.c
@@ -54,7 +54,7 @@ static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
54 54
55 /* Set the IPv6 fragment id if not set yet */ 55 /* Set the IPv6 fragment id if not set yet */
56 if (!skb_shinfo(skb)->ip6_frag_id) 56 if (!skb_shinfo(skb)->ip6_frag_id)
57 ipv6_proxy_select_ident(skb); 57 ipv6_proxy_select_ident(dev_net(skb->dev), skb);
58 58
59 segs = NULL; 59 segs = NULL;
60 goto out; 60 goto out;
@@ -113,7 +113,7 @@ static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
113 fptr->nexthdr = nexthdr; 113 fptr->nexthdr = nexthdr;
114 fptr->reserved = 0; 114 fptr->reserved = 0;
115 if (!skb_shinfo(skb)->ip6_frag_id) 115 if (!skb_shinfo(skb)->ip6_frag_id)
116 ipv6_proxy_select_ident(skb); 116 ipv6_proxy_select_ident(dev_net(skb->dev), skb);
117 fptr->identification = skb_shinfo(skb)->ip6_frag_id; 117 fptr->identification = skb_shinfo(skb)->ip6_frag_id;
118 118
119 /* Fragment the skb. ipv6 header and the remaining fields of the 119 /* Fragment the skb. ipv6 header and the remaining fields of the
diff --git a/net/ipv6/xfrm6_input.c b/net/ipv6/xfrm6_input.c
index f48fbe4d16f5..74bd17882a2f 100644
--- a/net/ipv6/xfrm6_input.c
+++ b/net/ipv6/xfrm6_input.c
@@ -42,7 +42,8 @@ int xfrm6_transport_finish(struct sk_buff *skb, int async)
42 ipv6_hdr(skb)->payload_len = htons(skb->len); 42 ipv6_hdr(skb)->payload_len = htons(skb->len);
43 __skb_push(skb, skb->data - skb_network_header(skb)); 43 __skb_push(skb, skb->data - skb_network_header(skb));
44 44
45 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, skb->dev, NULL, 45 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, NULL, skb,
46 skb->dev, NULL,
46 ip6_rcv_finish); 47 ip6_rcv_finish);
47 return -1; 48 return -1;
48} 49}
diff --git a/net/ipv6/xfrm6_mode_beet.c b/net/ipv6/xfrm6_mode_beet.c
index 9949a356d62c..1e205c3253ac 100644
--- a/net/ipv6/xfrm6_mode_beet.c
+++ b/net/ipv6/xfrm6_mode_beet.c
@@ -95,8 +95,8 @@ static int xfrm6_beet_input(struct xfrm_state *x, struct sk_buff *skb)
95 95
96 ip6h = ipv6_hdr(skb); 96 ip6h = ipv6_hdr(skb);
97 ip6h->payload_len = htons(skb->len - size); 97 ip6h->payload_len = htons(skb->len - size);
98 ip6h->daddr = *(struct in6_addr *)&x->sel.daddr.a6; 98 ip6h->daddr = x->sel.daddr.in6;
99 ip6h->saddr = *(struct in6_addr *)&x->sel.saddr.a6; 99 ip6h->saddr = x->sel.saddr.in6;
100 err = 0; 100 err = 0;
101out: 101out:
102 return err; 102 return err;
diff --git a/net/ipv6/xfrm6_output.c b/net/ipv6/xfrm6_output.c
index 010f8bd2d577..09c76a7b474d 100644
--- a/net/ipv6/xfrm6_output.c
+++ b/net/ipv6/xfrm6_output.c
@@ -120,7 +120,7 @@ int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
120} 120}
121EXPORT_SYMBOL(xfrm6_prepare_output); 121EXPORT_SYMBOL(xfrm6_prepare_output);
122 122
123int xfrm6_output_finish(struct sk_buff *skb) 123int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb)
124{ 124{
125 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); 125 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
126 126
@@ -128,10 +128,10 @@ int xfrm6_output_finish(struct sk_buff *skb)
128 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED; 128 IP6CB(skb)->flags |= IP6SKB_XFRM_TRANSFORMED;
129#endif 129#endif
130 130
131 return xfrm_output(skb); 131 return xfrm_output(sk, skb);
132} 132}
133 133
134static int __xfrm6_output(struct sk_buff *skb) 134static int __xfrm6_output(struct sock *sk, struct sk_buff *skb)
135{ 135{
136 struct dst_entry *dst = skb_dst(skb); 136 struct dst_entry *dst = skb_dst(skb);
137 struct xfrm_state *x = dst->xfrm; 137 struct xfrm_state *x = dst->xfrm;
@@ -140,7 +140,7 @@ static int __xfrm6_output(struct sk_buff *skb)
140#ifdef CONFIG_NETFILTER 140#ifdef CONFIG_NETFILTER
141 if (!x) { 141 if (!x) {
142 IP6CB(skb)->flags |= IP6SKB_REROUTED; 142 IP6CB(skb)->flags |= IP6SKB_REROUTED;
143 return dst_output(skb); 143 return dst_output_sk(sk, skb);
144 } 144 }
145#endif 145#endif
146 146
@@ -160,14 +160,15 @@ static int __xfrm6_output(struct sk_buff *skb)
160 if (x->props.mode == XFRM_MODE_TUNNEL && 160 if (x->props.mode == XFRM_MODE_TUNNEL &&
161 ((skb->len > mtu && !skb_is_gso(skb)) || 161 ((skb->len > mtu && !skb_is_gso(skb)) ||
162 dst_allfrag(skb_dst(skb)))) { 162 dst_allfrag(skb_dst(skb)))) {
163 return ip6_fragment(skb, x->outer_mode->afinfo->output_finish); 163 return ip6_fragment(sk, skb,
164 x->outer_mode->afinfo->output_finish);
164 } 165 }
165 return x->outer_mode->afinfo->output_finish(skb); 166 return x->outer_mode->afinfo->output_finish(sk, skb);
166} 167}
167 168
168int xfrm6_output(struct sock *sk, struct sk_buff *skb) 169int xfrm6_output(struct sock *sk, struct sk_buff *skb)
169{ 170{
170 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, 171 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, sk, skb,
171 NULL, skb_dst(skb)->dev, __xfrm6_output, 172 NULL, skb_dst(skb)->dev, __xfrm6_output,
172 !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 173 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
173} 174}
diff --git a/net/ipv6/xfrm6_policy.c b/net/ipv6/xfrm6_policy.c
index 8d2d01b4800a..f337a908a76a 100644
--- a/net/ipv6/xfrm6_policy.c
+++ b/net/ipv6/xfrm6_policy.c
@@ -61,9 +61,7 @@ static int xfrm6_get_saddr(struct net *net,
61 return -EHOSTUNREACH; 61 return -EHOSTUNREACH;
62 62
63 dev = ip6_dst_idev(dst)->dev; 63 dev = ip6_dst_idev(dst)->dev;
64 ipv6_dev_get_saddr(dev_net(dev), dev, 64 ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6);
65 (struct in6_addr *)&daddr->a6, 0,
66 (struct in6_addr *)&saddr->a6);
67 dst_release(dst); 65 dst_release(dst);
68 return 0; 66 return 0;
69} 67}
@@ -293,7 +291,6 @@ static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev,
293 291
294static struct dst_ops xfrm6_dst_ops = { 292static struct dst_ops xfrm6_dst_ops = {
295 .family = AF_INET6, 293 .family = AF_INET6,
296 .protocol = cpu_to_be16(ETH_P_IPV6),
297 .gc = xfrm6_garbage_collect, 294 .gc = xfrm6_garbage_collect,
298 .update_pmtu = xfrm6_update_pmtu, 295 .update_pmtu = xfrm6_update_pmtu,
299 .redirect = xfrm6_redirect, 296 .redirect = xfrm6_redirect,
@@ -371,7 +368,7 @@ static void __net_exit xfrm6_net_exit(struct net *net)
371{ 368{
372 struct ctl_table *table; 369 struct ctl_table *table;
373 370
374 if (net->ipv6.sysctl.xfrm6_hdr == NULL) 371 if (!net->ipv6.sysctl.xfrm6_hdr)
375 return; 372 return;
376 373
377 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg; 374 table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg;
diff --git a/net/ipx/af_ipx.c b/net/ipx/af_ipx.c
index f11ad1d95e0e..4ea5d7497b5f 100644
--- a/net/ipx/af_ipx.c
+++ b/net/ipx/af_ipx.c
@@ -1688,8 +1688,7 @@ out:
1688 return rc; 1688 return rc;
1689} 1689}
1690 1690
1691static int ipx_sendmsg(struct kiocb *iocb, struct socket *sock, 1691static int ipx_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1692 struct msghdr *msg, size_t len)
1693{ 1692{
1694 struct sock *sk = sock->sk; 1693 struct sock *sk = sock->sk;
1695 struct ipx_sock *ipxs = ipx_sk(sk); 1694 struct ipx_sock *ipxs = ipx_sk(sk);
@@ -1754,8 +1753,8 @@ out:
1754} 1753}
1755 1754
1756 1755
1757static int ipx_recvmsg(struct kiocb *iocb, struct socket *sock, 1756static int ipx_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1758 struct msghdr *msg, size_t size, int flags) 1757 int flags)
1759{ 1758{
1760 struct sock *sk = sock->sk; 1759 struct sock *sk = sock->sk;
1761 struct ipx_sock *ipxs = ipx_sk(sk); 1760 struct ipx_sock *ipxs = ipx_sk(sk);
diff --git a/net/irda/af_irda.c b/net/irda/af_irda.c
index 568edc72d737..ee0ea25c8e7a 100644
--- a/net/irda/af_irda.c
+++ b/net/irda/af_irda.c
@@ -1256,14 +1256,13 @@ static int irda_release(struct socket *sock)
1256} 1256}
1257 1257
1258/* 1258/*
1259 * Function irda_sendmsg (iocb, sock, msg, len) 1259 * Function irda_sendmsg (sock, msg, len)
1260 * 1260 *
1261 * Send message down to TinyTP. This function is used for both STREAM and 1261 * Send message down to TinyTP. This function is used for both STREAM and
1262 * SEQPACK services. This is possible since it forces the client to 1262 * SEQPACK services. This is possible since it forces the client to
1263 * fragment the message if necessary 1263 * fragment the message if necessary
1264 */ 1264 */
1265static int irda_sendmsg(struct kiocb *iocb, struct socket *sock, 1265static int irda_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1266 struct msghdr *msg, size_t len)
1267{ 1266{
1268 struct sock *sk = sock->sk; 1267 struct sock *sk = sock->sk;
1269 struct irda_sock *self; 1268 struct irda_sock *self;
@@ -1348,13 +1347,13 @@ out:
1348} 1347}
1349 1348
1350/* 1349/*
1351 * Function irda_recvmsg_dgram (iocb, sock, msg, size, flags) 1350 * Function irda_recvmsg_dgram (sock, msg, size, flags)
1352 * 1351 *
1353 * Try to receive message and copy it to user. The frame is discarded 1352 * Try to receive message and copy it to user. The frame is discarded
1354 * after being read, regardless of how much the user actually read 1353 * after being read, regardless of how much the user actually read
1355 */ 1354 */
1356static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock, 1355static int irda_recvmsg_dgram(struct socket *sock, struct msghdr *msg,
1357 struct msghdr *msg, size_t size, int flags) 1356 size_t size, int flags)
1358{ 1357{
1359 struct sock *sk = sock->sk; 1358 struct sock *sk = sock->sk;
1360 struct irda_sock *self = irda_sk(sk); 1359 struct irda_sock *self = irda_sk(sk);
@@ -1398,10 +1397,10 @@ static int irda_recvmsg_dgram(struct kiocb *iocb, struct socket *sock,
1398} 1397}
1399 1398
1400/* 1399/*
1401 * Function irda_recvmsg_stream (iocb, sock, msg, size, flags) 1400 * Function irda_recvmsg_stream (sock, msg, size, flags)
1402 */ 1401 */
1403static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock, 1402static int irda_recvmsg_stream(struct socket *sock, struct msghdr *msg,
1404 struct msghdr *msg, size_t size, int flags) 1403 size_t size, int flags)
1405{ 1404{
1406 struct sock *sk = sock->sk; 1405 struct sock *sk = sock->sk;
1407 struct irda_sock *self = irda_sk(sk); 1406 struct irda_sock *self = irda_sk(sk);
@@ -1515,14 +1514,14 @@ static int irda_recvmsg_stream(struct kiocb *iocb, struct socket *sock,
1515} 1514}
1516 1515
1517/* 1516/*
1518 * Function irda_sendmsg_dgram (iocb, sock, msg, len) 1517 * Function irda_sendmsg_dgram (sock, msg, len)
1519 * 1518 *
1520 * Send message down to TinyTP for the unreliable sequenced 1519 * Send message down to TinyTP for the unreliable sequenced
1521 * packet service... 1520 * packet service...
1522 * 1521 *
1523 */ 1522 */
1524static int irda_sendmsg_dgram(struct kiocb *iocb, struct socket *sock, 1523static int irda_sendmsg_dgram(struct socket *sock, struct msghdr *msg,
1525 struct msghdr *msg, size_t len) 1524 size_t len)
1526{ 1525{
1527 struct sock *sk = sock->sk; 1526 struct sock *sk = sock->sk;
1528 struct irda_sock *self; 1527 struct irda_sock *self;
@@ -1594,14 +1593,14 @@ out:
1594} 1593}
1595 1594
1596/* 1595/*
1597 * Function irda_sendmsg_ultra (iocb, sock, msg, len) 1596 * Function irda_sendmsg_ultra (sock, msg, len)
1598 * 1597 *
1599 * Send message down to IrLMP for the unreliable Ultra 1598 * Send message down to IrLMP for the unreliable Ultra
1600 * packet service... 1599 * packet service...
1601 */ 1600 */
1602#ifdef CONFIG_IRDA_ULTRA 1601#ifdef CONFIG_IRDA_ULTRA
1603static int irda_sendmsg_ultra(struct kiocb *iocb, struct socket *sock, 1602static int irda_sendmsg_ultra(struct socket *sock, struct msghdr *msg,
1604 struct msghdr *msg, size_t len) 1603 size_t len)
1605{ 1604{
1606 struct sock *sk = sock->sk; 1605 struct sock *sk = sock->sk;
1607 struct irda_sock *self; 1606 struct irda_sock *self;
diff --git a/net/iucv/af_iucv.c b/net/iucv/af_iucv.c
index 53d931172088..6daa52a18d40 100644
--- a/net/iucv/af_iucv.c
+++ b/net/iucv/af_iucv.c
@@ -1026,8 +1026,8 @@ static int iucv_send_iprm(struct iucv_path *path, struct iucv_message *msg,
1026 (void *) prmdata, 8); 1026 (void *) prmdata, 8);
1027} 1027}
1028 1028
1029static int iucv_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 1029static int iucv_sock_sendmsg(struct socket *sock, struct msghdr *msg,
1030 struct msghdr *msg, size_t len) 1030 size_t len)
1031{ 1031{
1032 struct sock *sk = sock->sk; 1032 struct sock *sk = sock->sk;
1033 struct iucv_sock *iucv = iucv_sk(sk); 1033 struct iucv_sock *iucv = iucv_sk(sk);
@@ -1315,8 +1315,8 @@ static void iucv_process_message_q(struct sock *sk)
1315 } 1315 }
1316} 1316}
1317 1317
1318static int iucv_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 1318static int iucv_sock_recvmsg(struct socket *sock, struct msghdr *msg,
1319 struct msghdr *msg, size_t len, int flags) 1319 size_t len, int flags)
1320{ 1320{
1321 int noblock = flags & MSG_DONTWAIT; 1321 int noblock = flags & MSG_DONTWAIT;
1322 struct sock *sk = sock->sk; 1322 struct sock *sk = sock->sk;
diff --git a/net/key/af_key.c b/net/key/af_key.c
index f8ac939d52b4..f0d52d721b3a 100644
--- a/net/key/af_key.c
+++ b/net/key/af_key.c
@@ -709,7 +709,7 @@ static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port
709 sin6->sin6_family = AF_INET6; 709 sin6->sin6_family = AF_INET6;
710 sin6->sin6_port = port; 710 sin6->sin6_port = port;
711 sin6->sin6_flowinfo = 0; 711 sin6->sin6_flowinfo = 0;
712 sin6->sin6_addr = *(struct in6_addr *)xaddr->a6; 712 sin6->sin6_addr = xaddr->in6;
713 sin6->sin6_scope_id = 0; 713 sin6->sin6_scope_id = 0;
714 return 128; 714 return 128;
715 } 715 }
@@ -3588,8 +3588,7 @@ static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
3588} 3588}
3589#endif 3589#endif
3590 3590
3591static int pfkey_sendmsg(struct kiocb *kiocb, 3591static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
3592 struct socket *sock, struct msghdr *msg, size_t len)
3593{ 3592{
3594 struct sock *sk = sock->sk; 3593 struct sock *sk = sock->sk;
3595 struct sk_buff *skb = NULL; 3594 struct sk_buff *skb = NULL;
@@ -3630,8 +3629,7 @@ out:
3630 return err ? : len; 3629 return err ? : len;
3631} 3630}
3632 3631
3633static int pfkey_recvmsg(struct kiocb *kiocb, 3632static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
3634 struct socket *sock, struct msghdr *msg, size_t len,
3635 int flags) 3633 int flags)
3636{ 3634{
3637 struct sock *sk = sock->sk; 3635 struct sock *sk = sock->sk;
diff --git a/net/l2tp/l2tp_eth.c b/net/l2tp/l2tp_eth.c
index 781b3a226ba7..4b552873b556 100644
--- a/net/l2tp/l2tp_eth.c
+++ b/net/l2tp/l2tp_eth.c
@@ -74,7 +74,7 @@ static int l2tp_eth_dev_init(struct net_device *dev)
74 74
75 priv->dev = dev; 75 priv->dev = dev;
76 eth_hw_addr_random(dev); 76 eth_hw_addr_random(dev);
77 memset(&dev->broadcast[0], 0xff, 6); 77 eth_broadcast_addr(dev->broadcast);
78 dev->qdisc_tx_busylock = &l2tp_eth_tx_busylock; 78 dev->qdisc_tx_busylock = &l2tp_eth_tx_busylock;
79 return 0; 79 return 0;
80} 80}
diff --git a/net/l2tp/l2tp_ip.c b/net/l2tp/l2tp_ip.c
index 05dfc8aa36af..79649937ec71 100644
--- a/net/l2tp/l2tp_ip.c
+++ b/net/l2tp/l2tp_ip.c
@@ -385,7 +385,7 @@ drop:
385/* Userspace will call sendmsg() on the tunnel socket to send L2TP 385/* Userspace will call sendmsg() on the tunnel socket to send L2TP
386 * control frames. 386 * control frames.
387 */ 387 */
388static int l2tp_ip_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, size_t len) 388static int l2tp_ip_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
389{ 389{
390 struct sk_buff *skb; 390 struct sk_buff *skb;
391 int rc; 391 int rc;
@@ -506,7 +506,7 @@ no_route:
506 goto out; 506 goto out;
507} 507}
508 508
509static int l2tp_ip_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg, 509static int l2tp_ip_recvmsg(struct sock *sk, struct msghdr *msg,
510 size_t len, int noblock, int flags, int *addr_len) 510 size_t len, int noblock, int flags, int *addr_len)
511{ 511{
512 struct inet_sock *inet = inet_sk(sk); 512 struct inet_sock *inet = inet_sk(sk);
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index 8611f1b63141..d1ded3777815 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -480,8 +480,7 @@ out:
480/* Userspace will call sendmsg() on the tunnel socket to send L2TP 480/* Userspace will call sendmsg() on the tunnel socket to send L2TP
481 * control frames. 481 * control frames.
482 */ 482 */
483static int l2tp_ip6_sendmsg(struct kiocb *iocb, struct sock *sk, 483static int l2tp_ip6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
484 struct msghdr *msg, size_t len)
485{ 484{
486 struct ipv6_txoptions opt_space; 485 struct ipv6_txoptions opt_space;
487 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name); 486 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
@@ -643,9 +642,8 @@ do_confirm:
643 goto done; 642 goto done;
644} 643}
645 644
646static int l2tp_ip6_recvmsg(struct kiocb *iocb, struct sock *sk, 645static int l2tp_ip6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
647 struct msghdr *msg, size_t len, int noblock, 646 int noblock, int flags, int *addr_len)
648 int flags, int *addr_len)
649{ 647{
650 struct ipv6_pinfo *np = inet6_sk(sk); 648 struct ipv6_pinfo *np = inet6_sk(sk);
651 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name); 649 DECLARE_SOCKADDR(struct sockaddr_l2tpip6 *, lsa, msg->msg_name);
diff --git a/net/l2tp/l2tp_netlink.c b/net/l2tp/l2tp_netlink.c
index b4e923f77954..9e13c2ff8789 100644
--- a/net/l2tp/l2tp_netlink.c
+++ b/net/l2tp/l2tp_netlink.c
@@ -205,9 +205,9 @@ static int l2tp_nl_cmd_tunnel_create(struct sk_buff *skb, struct genl_info *info
205#endif 205#endif
206 if (info->attrs[L2TP_ATTR_IP_SADDR] && 206 if (info->attrs[L2TP_ATTR_IP_SADDR] &&
207 info->attrs[L2TP_ATTR_IP_DADDR]) { 207 info->attrs[L2TP_ATTR_IP_DADDR]) {
208 cfg.local_ip.s_addr = nla_get_be32( 208 cfg.local_ip.s_addr = nla_get_in_addr(
209 info->attrs[L2TP_ATTR_IP_SADDR]); 209 info->attrs[L2TP_ATTR_IP_SADDR]);
210 cfg.peer_ip.s_addr = nla_get_be32( 210 cfg.peer_ip.s_addr = nla_get_in_addr(
211 info->attrs[L2TP_ATTR_IP_DADDR]); 211 info->attrs[L2TP_ATTR_IP_DADDR]);
212 } else { 212 } else {
213 ret = -EINVAL; 213 ret = -EINVAL;
@@ -376,15 +376,17 @@ static int l2tp_nl_tunnel_send(struct sk_buff *skb, u32 portid, u32 seq, int fla
376 case L2TP_ENCAPTYPE_IP: 376 case L2TP_ENCAPTYPE_IP:
377#if IS_ENABLED(CONFIG_IPV6) 377#if IS_ENABLED(CONFIG_IPV6)
378 if (np) { 378 if (np) {
379 if (nla_put(skb, L2TP_ATTR_IP6_SADDR, sizeof(np->saddr), 379 if (nla_put_in6_addr(skb, L2TP_ATTR_IP6_SADDR,
380 &np->saddr) || 380 &np->saddr) ||
381 nla_put(skb, L2TP_ATTR_IP6_DADDR, sizeof(sk->sk_v6_daddr), 381 nla_put_in6_addr(skb, L2TP_ATTR_IP6_DADDR,
382 &sk->sk_v6_daddr)) 382 &sk->sk_v6_daddr))
383 goto nla_put_failure; 383 goto nla_put_failure;
384 } else 384 } else
385#endif 385#endif
386 if (nla_put_be32(skb, L2TP_ATTR_IP_SADDR, inet->inet_saddr) || 386 if (nla_put_in_addr(skb, L2TP_ATTR_IP_SADDR,
387 nla_put_be32(skb, L2TP_ATTR_IP_DADDR, inet->inet_daddr)) 387 inet->inet_saddr) ||
388 nla_put_in_addr(skb, L2TP_ATTR_IP_DADDR,
389 inet->inet_daddr))
388 goto nla_put_failure; 390 goto nla_put_failure;
389 break; 391 break;
390 } 392 }
diff --git a/net/l2tp/l2tp_ppp.c b/net/l2tp/l2tp_ppp.c
index cc7a828fc914..e9b0dec56b8e 100644
--- a/net/l2tp/l2tp_ppp.c
+++ b/net/l2tp/l2tp_ppp.c
@@ -185,9 +185,8 @@ static int pppol2tp_recv_payload_hook(struct sk_buff *skb)
185 185
186/* Receive message. This is the recvmsg for the PPPoL2TP socket. 186/* Receive message. This is the recvmsg for the PPPoL2TP socket.
187 */ 187 */
188static int pppol2tp_recvmsg(struct kiocb *iocb, struct socket *sock, 188static int pppol2tp_recvmsg(struct socket *sock, struct msghdr *msg,
189 struct msghdr *msg, size_t len, 189 size_t len, int flags)
190 int flags)
191{ 190{
192 int err; 191 int err;
193 struct sk_buff *skb; 192 struct sk_buff *skb;
@@ -295,7 +294,7 @@ static void pppol2tp_session_sock_put(struct l2tp_session *session)
295 * when a user application does a sendmsg() on the session socket. L2TP and 294 * when a user application does a sendmsg() on the session socket. L2TP and
296 * PPP headers must be inserted into the user's data. 295 * PPP headers must be inserted into the user's data.
297 */ 296 */
298static int pppol2tp_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 297static int pppol2tp_sendmsg(struct socket *sock, struct msghdr *m,
299 size_t total_len) 298 size_t total_len)
300{ 299{
301 static const unsigned char ppph[2] = { 0xff, 0x03 }; 300 static const unsigned char ppph[2] = { 0xff, 0x03 };
diff --git a/net/llc/af_llc.c b/net/llc/af_llc.c
index 2c0b83ce43bd..17a8dff06090 100644
--- a/net/llc/af_llc.c
+++ b/net/llc/af_llc.c
@@ -704,8 +704,8 @@ out:
704 * Copy received data to the socket user. 704 * Copy received data to the socket user.
705 * Returns non-negative upon success, negative otherwise. 705 * Returns non-negative upon success, negative otherwise.
706 */ 706 */
707static int llc_ui_recvmsg(struct kiocb *iocb, struct socket *sock, 707static int llc_ui_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
708 struct msghdr *msg, size_t len, int flags) 708 int flags)
709{ 709{
710 DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name); 710 DECLARE_SOCKADDR(struct sockaddr_llc *, uaddr, msg->msg_name);
711 const int nonblock = flags & MSG_DONTWAIT; 711 const int nonblock = flags & MSG_DONTWAIT;
@@ -878,8 +878,7 @@ copy_uaddr:
878 * Transmit data provided by the socket user. 878 * Transmit data provided by the socket user.
879 * Returns non-negative upon success, negative otherwise. 879 * Returns non-negative upon success, negative otherwise.
880 */ 880 */
881static int llc_ui_sendmsg(struct kiocb *iocb, struct socket *sock, 881static int llc_ui_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
882 struct msghdr *msg, size_t len)
883{ 882{
884 struct sock *sk = sock->sk; 883 struct sock *sk = sock->sk;
885 struct llc_sock *llc = llc_sk(sk); 884 struct llc_sock *llc = llc_sk(sk);
diff --git a/net/mac80211/aes_ccm.c b/net/mac80211/aes_ccm.c
index 7869bb40acaa..208df7c0b6ea 100644
--- a/net/mac80211/aes_ccm.c
+++ b/net/mac80211/aes_ccm.c
@@ -85,11 +85,15 @@ struct crypto_aead *ieee80211_aes_key_setup_encrypt(const u8 key[],
85 return tfm; 85 return tfm;
86 86
87 err = crypto_aead_setkey(tfm, key, key_len); 87 err = crypto_aead_setkey(tfm, key, key_len);
88 if (!err) 88 if (err)
89 err = crypto_aead_setauthsize(tfm, mic_len); 89 goto free_aead;
90 if (!err) 90 err = crypto_aead_setauthsize(tfm, mic_len);
91 return tfm; 91 if (err)
92 goto free_aead;
93
94 return tfm;
92 95
96free_aead:
93 crypto_free_aead(tfm); 97 crypto_free_aead(tfm);
94 return ERR_PTR(err); 98 return ERR_PTR(err);
95} 99}
diff --git a/net/mac80211/aes_gcm.c b/net/mac80211/aes_gcm.c
index c2bf6698d738..fd278bbe1b0d 100644
--- a/net/mac80211/aes_gcm.c
+++ b/net/mac80211/aes_gcm.c
@@ -80,11 +80,15 @@ struct crypto_aead *ieee80211_aes_gcm_key_setup_encrypt(const u8 key[],
80 return tfm; 80 return tfm;
81 81
82 err = crypto_aead_setkey(tfm, key, key_len); 82 err = crypto_aead_setkey(tfm, key, key_len);
83 if (!err) 83 if (err)
84 err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN); 84 goto free_aead;
85 if (!err) 85 err = crypto_aead_setauthsize(tfm, IEEE80211_GCMP_MIC_LEN);
86 return tfm; 86 if (err)
87 goto free_aead;
88
89 return tfm;
87 90
91free_aead:
88 crypto_free_aead(tfm); 92 crypto_free_aead(tfm);
89 return ERR_PTR(err); 93 return ERR_PTR(err);
90} 94}
diff --git a/net/mac80211/aes_gmac.c b/net/mac80211/aes_gmac.c
index 1c72edcb0083..f1321b7d6506 100644
--- a/net/mac80211/aes_gmac.c
+++ b/net/mac80211/aes_gmac.c
@@ -70,9 +70,9 @@ struct crypto_aead *ieee80211_aes_gmac_key_setup(const u8 key[],
70 70
71 err = crypto_aead_setkey(tfm, key, key_len); 71 err = crypto_aead_setkey(tfm, key, key_len);
72 if (!err) 72 if (!err)
73 return tfm;
74 if (!err)
75 err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN); 73 err = crypto_aead_setauthsize(tfm, GMAC_MIC_LEN);
74 if (!err)
75 return tfm;
76 76
77 crypto_free_aead(tfm); 77 crypto_free_aead(tfm);
78 return ERR_PTR(err); 78 return ERR_PTR(err);
diff --git a/net/mac80211/agg-rx.c b/net/mac80211/agg-rx.c
index 7702978a4c99..5c564a68fb50 100644
--- a/net/mac80211/agg-rx.c
+++ b/net/mac80211/agg-rx.c
@@ -238,6 +238,14 @@ void __ieee80211_start_rx_ba_session(struct sta_info *sta,
238 int i, ret = -EOPNOTSUPP; 238 int i, ret = -EOPNOTSUPP;
239 u16 status = WLAN_STATUS_REQUEST_DECLINED; 239 u16 status = WLAN_STATUS_REQUEST_DECLINED;
240 240
241 if (!sta->sta.ht_cap.ht_supported) {
242 ht_dbg(sta->sdata,
243 "STA %pM erroneously requests BA session on tid %d w/o QoS\n",
244 sta->sta.addr, tid);
245 /* send a response anyway, it's an error case if we get here */
246 goto end_no_lock;
247 }
248
241 if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) { 249 if (test_sta_flag(sta, WLAN_STA_BLOCK_BA)) {
242 ht_dbg(sta->sdata, 250 ht_dbg(sta->sdata,
243 "Suspend in progress - Denying ADDBA request (%pM tid %d)\n", 251 "Suspend in progress - Denying ADDBA request (%pM tid %d)\n",
diff --git a/net/mac80211/agg-tx.c b/net/mac80211/agg-tx.c
index a360c15cc978..cce9d425c718 100644
--- a/net/mac80211/agg-tx.c
+++ b/net/mac80211/agg-tx.c
@@ -188,6 +188,43 @@ ieee80211_wake_queue_agg(struct ieee80211_sub_if_data *sdata, int tid)
188 __release(agg_queue); 188 __release(agg_queue);
189} 189}
190 190
191static void
192ieee80211_agg_stop_txq(struct sta_info *sta, int tid)
193{
194 struct ieee80211_txq *txq = sta->sta.txq[tid];
195 struct txq_info *txqi;
196
197 if (!txq)
198 return;
199
200 txqi = to_txq_info(txq);
201
202 /* Lock here to protect against further seqno updates on dequeue */
203 spin_lock_bh(&txqi->queue.lock);
204 set_bit(IEEE80211_TXQ_STOP, &txqi->flags);
205 spin_unlock_bh(&txqi->queue.lock);
206}
207
208static void
209ieee80211_agg_start_txq(struct sta_info *sta, int tid, bool enable)
210{
211 struct ieee80211_txq *txq = sta->sta.txq[tid];
212 struct txq_info *txqi;
213
214 if (!txq)
215 return;
216
217 txqi = to_txq_info(txq);
218
219 if (enable)
220 set_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
221 else
222 clear_bit(IEEE80211_TXQ_AMPDU, &txqi->flags);
223
224 clear_bit(IEEE80211_TXQ_STOP, &txqi->flags);
225 drv_wake_tx_queue(sta->sdata->local, txqi);
226}
227
191/* 228/*
192 * splice packets from the STA's pending to the local pending, 229 * splice packets from the STA's pending to the local pending,
193 * requires a call to ieee80211_agg_splice_finish later 230 * requires a call to ieee80211_agg_splice_finish later
@@ -247,6 +284,7 @@ static void ieee80211_remove_tid_tx(struct sta_info *sta, int tid)
247 ieee80211_assign_tid_tx(sta, tid, NULL); 284 ieee80211_assign_tid_tx(sta, tid, NULL);
248 285
249 ieee80211_agg_splice_finish(sta->sdata, tid); 286 ieee80211_agg_splice_finish(sta->sdata, tid);
287 ieee80211_agg_start_txq(sta, tid, false);
250 288
251 kfree_rcu(tid_tx, rcu_head); 289 kfree_rcu(tid_tx, rcu_head);
252} 290}
@@ -418,6 +456,8 @@ void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
418 */ 456 */
419 clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state); 457 clear_bit(HT_AGG_STATE_WANT_START, &tid_tx->state);
420 458
459 ieee80211_agg_stop_txq(sta, tid);
460
421 /* 461 /*
422 * Make sure no packets are being processed. This ensures that 462 * Make sure no packets are being processed. This ensures that
423 * we have a valid starting sequence number and that in-flight 463 * we have a valid starting sequence number and that in-flight
@@ -440,6 +480,8 @@ void ieee80211_tx_ba_session_handle_start(struct sta_info *sta, int tid)
440 ieee80211_agg_splice_finish(sdata, tid); 480 ieee80211_agg_splice_finish(sdata, tid);
441 spin_unlock_bh(&sta->lock); 481 spin_unlock_bh(&sta->lock);
442 482
483 ieee80211_agg_start_txq(sta, tid, false);
484
443 kfree_rcu(tid_tx, rcu_head); 485 kfree_rcu(tid_tx, rcu_head);
444 return; 486 return;
445 } 487 }
@@ -509,11 +551,14 @@ int ieee80211_start_tx_ba_session(struct ieee80211_sta *pubsta, u16 tid,
509 struct tid_ampdu_tx *tid_tx; 551 struct tid_ampdu_tx *tid_tx;
510 int ret = 0; 552 int ret = 0;
511 553
554 trace_api_start_tx_ba_session(pubsta, tid);
555
512 if (WARN(sta->reserved_tid == tid, 556 if (WARN(sta->reserved_tid == tid,
513 "Requested to start BA session on reserved tid=%d", tid)) 557 "Requested to start BA session on reserved tid=%d", tid))
514 return -EINVAL; 558 return -EINVAL;
515 559
516 trace_api_start_tx_ba_session(pubsta, tid); 560 if (!pubsta->ht_cap.ht_supported)
561 return -EINVAL;
517 562
518 if (WARN_ON_ONCE(!local->ops->ampdu_action)) 563 if (WARN_ON_ONCE(!local->ops->ampdu_action))
519 return -EINVAL; 564 return -EINVAL;
@@ -666,6 +711,8 @@ static void ieee80211_agg_tx_operational(struct ieee80211_local *local,
666 ieee80211_agg_splice_finish(sta->sdata, tid); 711 ieee80211_agg_splice_finish(sta->sdata, tid);
667 712
668 spin_unlock_bh(&sta->lock); 713 spin_unlock_bh(&sta->lock);
714
715 ieee80211_agg_start_txq(sta, tid, true);
669} 716}
670 717
671void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid) 718void ieee80211_start_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u16 tid)
@@ -793,6 +840,7 @@ void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
793 struct ieee80211_local *local = sdata->local; 840 struct ieee80211_local *local = sdata->local;
794 struct sta_info *sta; 841 struct sta_info *sta;
795 struct tid_ampdu_tx *tid_tx; 842 struct tid_ampdu_tx *tid_tx;
843 bool send_delba = false;
796 844
797 trace_api_stop_tx_ba_cb(sdata, ra, tid); 845 trace_api_stop_tx_ba_cb(sdata, ra, tid);
798 846
@@ -824,13 +872,17 @@ void ieee80211_stop_tx_ba_cb(struct ieee80211_vif *vif, u8 *ra, u8 tid)
824 } 872 }
825 873
826 if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop) 874 if (tid_tx->stop_initiator == WLAN_BACK_INITIATOR && tid_tx->tx_stop)
827 ieee80211_send_delba(sta->sdata, ra, tid, 875 send_delba = true;
828 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
829 876
830 ieee80211_remove_tid_tx(sta, tid); 877 ieee80211_remove_tid_tx(sta, tid);
831 878
832 unlock_sta: 879 unlock_sta:
833 spin_unlock_bh(&sta->lock); 880 spin_unlock_bh(&sta->lock);
881
882 if (send_delba)
883 ieee80211_send_delba(sdata, ra, tid,
884 WLAN_BACK_INITIATOR, WLAN_REASON_QSTA_NOT_USE);
885
834 mutex_unlock(&sta->ampdu_mlme.mtx); 886 mutex_unlock(&sta->ampdu_mlme.mtx);
835 unlock: 887 unlock:
836 mutex_unlock(&local->sta_mtx); 888 mutex_unlock(&local->sta_mtx);
diff --git a/net/mac80211/cfg.c b/net/mac80211/cfg.c
index dd4ff36c557a..265e42721a66 100644
--- a/net/mac80211/cfg.c
+++ b/net/mac80211/cfg.c
@@ -24,6 +24,7 @@
24 24
25static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy, 25static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
26 const char *name, 26 const char *name,
27 unsigned char name_assign_type,
27 enum nl80211_iftype type, 28 enum nl80211_iftype type,
28 u32 *flags, 29 u32 *flags,
29 struct vif_params *params) 30 struct vif_params *params)
@@ -33,7 +34,7 @@ static struct wireless_dev *ieee80211_add_iface(struct wiphy *wiphy,
33 struct ieee80211_sub_if_data *sdata; 34 struct ieee80211_sub_if_data *sdata;
34 int err; 35 int err;
35 36
36 err = ieee80211_if_add(local, name, &wdev, type, params); 37 err = ieee80211_if_add(local, name, name_assign_type, &wdev, type, params);
37 if (err) 38 if (err)
38 return ERR_PTR(err); 39 return ERR_PTR(err);
39 40
@@ -977,6 +978,14 @@ static int sta_apply_auth_flags(struct ieee80211_local *local,
977 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) && 978 if (mask & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
978 set & BIT(NL80211_STA_FLAG_ASSOCIATED) && 979 set & BIT(NL80211_STA_FLAG_ASSOCIATED) &&
979 !test_sta_flag(sta, WLAN_STA_ASSOC)) { 980 !test_sta_flag(sta, WLAN_STA_ASSOC)) {
981 /*
982 * When peer becomes associated, init rate control as
983 * well. Some drivers require rate control initialized
984 * before drv_sta_state() is called.
985 */
986 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
987 rate_control_rate_init(sta);
988
980 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC); 989 ret = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
981 if (ret) 990 if (ret)
982 return ret; 991 return ret;
@@ -1050,6 +1059,10 @@ static int sta_apply_parameters(struct ieee80211_local *local,
1050 } 1059 }
1051 } 1060 }
1052 1061
1062 if (mask & BIT(NL80211_STA_FLAG_WME) &&
1063 local->hw.queues >= IEEE80211_NUM_ACS)
1064 sta->sta.wme = set & BIT(NL80211_STA_FLAG_WME);
1065
1053 /* auth flags will be set later for TDLS stations */ 1066 /* auth flags will be set later for TDLS stations */
1054 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) { 1067 if (!test_sta_flag(sta, WLAN_STA_TDLS_PEER)) {
1055 ret = sta_apply_auth_flags(local, sta, mask, set); 1068 ret = sta_apply_auth_flags(local, sta, mask, set);
@@ -1064,10 +1077,8 @@ static int sta_apply_parameters(struct ieee80211_local *local,
1064 clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE); 1077 clear_sta_flag(sta, WLAN_STA_SHORT_PREAMBLE);
1065 } 1078 }
1066 1079
1067 if (mask & BIT(NL80211_STA_FLAG_WME))
1068 sta->sta.wme = set & BIT(NL80211_STA_FLAG_WME);
1069
1070 if (mask & BIT(NL80211_STA_FLAG_MFP)) { 1080 if (mask & BIT(NL80211_STA_FLAG_MFP)) {
1081 sta->sta.mfp = !!(set & BIT(NL80211_STA_FLAG_MFP));
1071 if (set & BIT(NL80211_STA_FLAG_MFP)) 1082 if (set & BIT(NL80211_STA_FLAG_MFP))
1072 set_sta_flag(sta, WLAN_STA_MFP); 1083 set_sta_flag(sta, WLAN_STA_MFP);
1073 else 1084 else
@@ -1377,11 +1388,6 @@ static int ieee80211_change_station(struct wiphy *wiphy,
1377 if (err) 1388 if (err)
1378 goto out_err; 1389 goto out_err;
1379 1390
1380 /* When peer becomes authorized, init rate control as well */
1381 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) &&
1382 test_sta_flag(sta, WLAN_STA_AUTHORIZED))
1383 rate_control_rate_init(sta);
1384
1385 mutex_unlock(&local->sta_mtx); 1391 mutex_unlock(&local->sta_mtx);
1386 1392
1387 if ((sdata->vif.type == NL80211_IFTYPE_AP || 1393 if ((sdata->vif.type == NL80211_IFTYPE_AP ||
@@ -1488,7 +1494,7 @@ static void mpath_set_pinfo(struct mesh_path *mpath, u8 *next_hop,
1488 if (next_hop_sta) 1494 if (next_hop_sta)
1489 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN); 1495 memcpy(next_hop, next_hop_sta->sta.addr, ETH_ALEN);
1490 else 1496 else
1491 memset(next_hop, 0, ETH_ALEN); 1497 eth_zero_addr(next_hop);
1492 1498
1493 memset(pinfo, 0, sizeof(*pinfo)); 1499 memset(pinfo, 0, sizeof(*pinfo));
1494 1500
@@ -2273,7 +2279,6 @@ int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
2273{ 2279{
2274 struct sta_info *sta; 2280 struct sta_info *sta;
2275 enum ieee80211_smps_mode old_req; 2281 enum ieee80211_smps_mode old_req;
2276 int i;
2277 2282
2278 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP)) 2283 if (WARN_ON_ONCE(sdata->vif.type != NL80211_IFTYPE_AP))
2279 return -EINVAL; 2284 return -EINVAL;
@@ -2297,52 +2302,44 @@ int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
2297 } 2302 }
2298 2303
2299 ht_dbg(sdata, 2304 ht_dbg(sdata,
2300 "SMSP %d requested in AP mode, sending Action frame to %d stations\n", 2305 "SMPS %d requested in AP mode, sending Action frame to %d stations\n",
2301 smps_mode, atomic_read(&sdata->u.ap.num_mcast_sta)); 2306 smps_mode, atomic_read(&sdata->u.ap.num_mcast_sta));
2302 2307
2303 mutex_lock(&sdata->local->sta_mtx); 2308 mutex_lock(&sdata->local->sta_mtx);
2304 for (i = 0; i < STA_HASH_SIZE; i++) { 2309 list_for_each_entry(sta, &sdata->local->sta_list, list) {
2305 for (sta = rcu_dereference_protected(sdata->local->sta_hash[i], 2310 /*
2306 lockdep_is_held(&sdata->local->sta_mtx)); 2311 * Only stations associated to our AP and
2307 sta; 2312 * associated VLANs
2308 sta = rcu_dereference_protected(sta->hnext, 2313 */
2309 lockdep_is_held(&sdata->local->sta_mtx))) { 2314 if (sta->sdata->bss != &sdata->u.ap)
2310 /* 2315 continue;
2311 * Only stations associated to our AP and
2312 * associated VLANs
2313 */
2314 if (sta->sdata->bss != &sdata->u.ap)
2315 continue;
2316 2316
2317 /* This station doesn't support MIMO - skip it */ 2317 /* This station doesn't support MIMO - skip it */
2318 if (sta_info_tx_streams(sta) == 1) 2318 if (sta_info_tx_streams(sta) == 1)
2319 continue; 2319 continue;
2320 2320
2321 /* 2321 /*
2322 * Don't wake up a STA just to send the action frame 2322 * Don't wake up a STA just to send the action frame
2323 * unless we are getting more restrictive. 2323 * unless we are getting more restrictive.
2324 */ 2324 */
2325 if (test_sta_flag(sta, WLAN_STA_PS_STA) && 2325 if (test_sta_flag(sta, WLAN_STA_PS_STA) &&
2326 !ieee80211_smps_is_restrictive(sta->known_smps_mode, 2326 !ieee80211_smps_is_restrictive(sta->known_smps_mode,
2327 smps_mode)) { 2327 smps_mode)) {
2328 ht_dbg(sdata, 2328 ht_dbg(sdata, "Won't send SMPS to sleeping STA %pM\n",
2329 "Won't send SMPS to sleeping STA %pM\n", 2329 sta->sta.addr);
2330 sta->sta.addr); 2330 continue;
2331 continue; 2331 }
2332 }
2333 2332
2334 /* 2333 /*
2335 * If the STA is not authorized, wait until it gets 2334 * If the STA is not authorized, wait until it gets
2336 * authorized and the action frame will be sent then. 2335 * authorized and the action frame will be sent then.
2337 */ 2336 */
2338 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED)) 2337 if (!test_sta_flag(sta, WLAN_STA_AUTHORIZED))
2339 continue; 2338 continue;
2340 2339
2341 ht_dbg(sdata, "Sending SMPS to %pM\n", sta->sta.addr); 2340 ht_dbg(sdata, "Sending SMPS to %pM\n", sta->sta.addr);
2342 ieee80211_send_smps_action(sdata, smps_mode, 2341 ieee80211_send_smps_action(sdata, smps_mode, sta->sta.addr,
2343 sta->sta.addr, 2342 sdata->vif.bss_conf.bssid);
2344 sdata->vif.bss_conf.bssid);
2345 }
2346 } 2343 }
2347 mutex_unlock(&sdata->local->sta_mtx); 2344 mutex_unlock(&sdata->local->sta_mtx);
2348 2345
@@ -3581,7 +3578,7 @@ static int ieee80211_probe_client(struct wiphy *wiphy, struct net_device *dev,
3581 nullfunc->qos_ctrl = cpu_to_le16(7); 3578 nullfunc->qos_ctrl = cpu_to_le16(7);
3582 3579
3583 local_bh_disable(); 3580 local_bh_disable();
3584 ieee80211_xmit(sdata, skb); 3581 ieee80211_xmit(sdata, sta, skb);
3585 local_bh_enable(); 3582 local_bh_enable();
3586 rcu_read_unlock(); 3583 rcu_read_unlock();
3587 3584
diff --git a/net/mac80211/debugfs.c b/net/mac80211/debugfs.c
index eeb0bbd69d98..23813ebb349c 100644
--- a/net/mac80211/debugfs.c
+++ b/net/mac80211/debugfs.c
@@ -18,172 +18,6 @@
18 18
19#define DEBUGFS_FORMAT_BUFFER_SIZE 100 19#define DEBUGFS_FORMAT_BUFFER_SIZE 100
20 20
21#define TX_LATENCY_BIN_DELIMTER_C ','
22#define TX_LATENCY_BIN_DELIMTER_S ","
23#define TX_LATENCY_BINS_DISABLED "enable(bins disabled)\n"
24#define TX_LATENCY_DISABLED "disable\n"
25
26
27/*
28 * Display if Tx latency statistics & bins are enabled/disabled
29 */
30static ssize_t sta_tx_latency_stat_read(struct file *file,
31 char __user *userbuf,
32 size_t count, loff_t *ppos)
33{
34 struct ieee80211_local *local = file->private_data;
35 struct ieee80211_tx_latency_bin_ranges *tx_latency;
36 char *buf;
37 int bufsz, i, ret;
38 int pos = 0;
39
40 rcu_read_lock();
41
42 tx_latency = rcu_dereference(local->tx_latency);
43
44 if (tx_latency && tx_latency->n_ranges) {
45 bufsz = tx_latency->n_ranges * 15;
46 buf = kzalloc(bufsz, GFP_ATOMIC);
47 if (!buf)
48 goto err;
49
50 for (i = 0; i < tx_latency->n_ranges; i++)
51 pos += scnprintf(buf + pos, bufsz - pos, "%d,",
52 tx_latency->ranges[i]);
53 pos += scnprintf(buf + pos, bufsz - pos, "\n");
54 } else if (tx_latency) {
55 bufsz = sizeof(TX_LATENCY_BINS_DISABLED) + 1;
56 buf = kzalloc(bufsz, GFP_ATOMIC);
57 if (!buf)
58 goto err;
59
60 pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
61 TX_LATENCY_BINS_DISABLED);
62 } else {
63 bufsz = sizeof(TX_LATENCY_DISABLED) + 1;
64 buf = kzalloc(bufsz, GFP_ATOMIC);
65 if (!buf)
66 goto err;
67
68 pos += scnprintf(buf + pos, bufsz - pos, "%s\n",
69 TX_LATENCY_DISABLED);
70 }
71
72 rcu_read_unlock();
73
74 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
75 kfree(buf);
76
77 return ret;
78err:
79 rcu_read_unlock();
80 return -ENOMEM;
81}
82
83/*
84 * Receive input from user regarding Tx latency statistics
85 * The input should indicate if Tx latency statistics and bins are
86 * enabled/disabled.
87 * If bins are enabled input should indicate the amount of different bins and
88 * their ranges. Each bin will count how many Tx frames transmitted within the
89 * appropriate latency.
90 * Legal input is:
91 * a) "enable(bins disabled)" - to enable only general statistics
92 * b) "a,b,c,d,...z" - to enable general statistics and bins, where all are
93 * numbers and a < b < c < d.. < z
94 * c) "disable" - disable all statistics
95 * NOTE: must configure Tx latency statistics bins before stations connected.
96 */
97
98static ssize_t sta_tx_latency_stat_write(struct file *file,
99 const char __user *userbuf,
100 size_t count, loff_t *ppos)
101{
102 struct ieee80211_local *local = file->private_data;
103 char buf[128] = {};
104 char *bins = buf;
105 char *token;
106 int buf_size, i, alloc_size;
107 int prev_bin = 0;
108 int n_ranges = 0;
109 int ret = count;
110 struct ieee80211_tx_latency_bin_ranges *tx_latency;
111
112 if (sizeof(buf) <= count)
113 return -EINVAL;
114 buf_size = count;
115 if (copy_from_user(buf, userbuf, buf_size))
116 return -EFAULT;
117
118 mutex_lock(&local->sta_mtx);
119
120 /* cannot change config once we have stations */
121 if (local->num_sta)
122 goto unlock;
123
124 tx_latency =
125 rcu_dereference_protected(local->tx_latency,
126 lockdep_is_held(&local->sta_mtx));
127
128 /* disable Tx statistics */
129 if (!strcmp(buf, TX_LATENCY_DISABLED)) {
130 if (!tx_latency)
131 goto unlock;
132 RCU_INIT_POINTER(local->tx_latency, NULL);
133 synchronize_rcu();
134 kfree(tx_latency);
135 goto unlock;
136 }
137
138 /* Tx latency already enabled */
139 if (tx_latency)
140 goto unlock;
141
142 if (strcmp(TX_LATENCY_BINS_DISABLED, buf)) {
143 /* check how many bins and between what ranges user requested */
144 token = buf;
145 while (*token != '\0') {
146 if (*token == TX_LATENCY_BIN_DELIMTER_C)
147 n_ranges++;
148 token++;
149 }
150 n_ranges++;
151 }
152
153 alloc_size = sizeof(struct ieee80211_tx_latency_bin_ranges) +
154 n_ranges * sizeof(u32);
155 tx_latency = kzalloc(alloc_size, GFP_ATOMIC);
156 if (!tx_latency) {
157 ret = -ENOMEM;
158 goto unlock;
159 }
160 tx_latency->n_ranges = n_ranges;
161 for (i = 0; i < n_ranges; i++) { /* setting bin ranges */
162 token = strsep(&bins, TX_LATENCY_BIN_DELIMTER_S);
163 sscanf(token, "%d", &tx_latency->ranges[i]);
164 /* bins values should be in ascending order */
165 if (prev_bin >= tx_latency->ranges[i]) {
166 ret = -EINVAL;
167 kfree(tx_latency);
168 goto unlock;
169 }
170 prev_bin = tx_latency->ranges[i];
171 }
172 rcu_assign_pointer(local->tx_latency, tx_latency);
173
174unlock:
175 mutex_unlock(&local->sta_mtx);
176
177 return ret;
178}
179
180static const struct file_operations stats_tx_latency_ops = {
181 .write = sta_tx_latency_stat_write,
182 .read = sta_tx_latency_stat_read,
183 .open = simple_open,
184 .llseek = generic_file_llseek,
185};
186
187int mac80211_format_buffer(char __user *userbuf, size_t count, 21int mac80211_format_buffer(char __user *userbuf, size_t count,
188 loff_t *ppos, char *fmt, ...) 22 loff_t *ppos, char *fmt, ...)
189{ 23{
@@ -440,8 +274,6 @@ void debugfs_hw_add(struct ieee80211_local *local)
440#ifdef CONFIG_MAC80211_DEBUG_COUNTERS 274#ifdef CONFIG_MAC80211_DEBUG_COUNTERS
441 DEBUGFS_STATS_ADD(tx_handlers_drop, local->tx_handlers_drop); 275 DEBUGFS_STATS_ADD(tx_handlers_drop, local->tx_handlers_drop);
442 DEBUGFS_STATS_ADD(tx_handlers_queued, local->tx_handlers_queued); 276 DEBUGFS_STATS_ADD(tx_handlers_queued, local->tx_handlers_queued);
443 DEBUGFS_STATS_ADD(tx_handlers_drop_unencrypted,
444 local->tx_handlers_drop_unencrypted);
445 DEBUGFS_STATS_ADD(tx_handlers_drop_fragment, 277 DEBUGFS_STATS_ADD(tx_handlers_drop_fragment,
446 local->tx_handlers_drop_fragment); 278 local->tx_handlers_drop_fragment);
447 DEBUGFS_STATS_ADD(tx_handlers_drop_wep, 279 DEBUGFS_STATS_ADD(tx_handlers_drop_wep,
@@ -475,6 +307,4 @@ void debugfs_hw_add(struct ieee80211_local *local)
475 DEBUGFS_DEVSTATS_ADD(dot11RTSFailureCount); 307 DEBUGFS_DEVSTATS_ADD(dot11RTSFailureCount);
476 DEBUGFS_DEVSTATS_ADD(dot11FCSErrorCount); 308 DEBUGFS_DEVSTATS_ADD(dot11FCSErrorCount);
477 DEBUGFS_DEVSTATS_ADD(dot11RTSSuccessCount); 309 DEBUGFS_DEVSTATS_ADD(dot11RTSSuccessCount);
478
479 DEBUGFS_DEVSTATS_ADD(tx_latency);
480} 310}
diff --git a/net/mac80211/debugfs_netdev.c b/net/mac80211/debugfs_netdev.c
index c68896adfa96..29236e832e44 100644
--- a/net/mac80211/debugfs_netdev.c
+++ b/net/mac80211/debugfs_netdev.c
@@ -177,7 +177,6 @@ static ssize_t ieee80211_if_write_##name(struct file *file, \
177 IEEE80211_IF_FILE_R(name) 177 IEEE80211_IF_FILE_R(name)
178 178
179/* common attributes */ 179/* common attributes */
180IEEE80211_IF_FILE(drop_unencrypted, drop_unencrypted, DEC);
181IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[IEEE80211_BAND_2GHZ], 180IEEE80211_IF_FILE(rc_rateidx_mask_2ghz, rc_rateidx_mask[IEEE80211_BAND_2GHZ],
182 HEX); 181 HEX);
183IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[IEEE80211_BAND_5GHZ], 182IEEE80211_IF_FILE(rc_rateidx_mask_5ghz, rc_rateidx_mask[IEEE80211_BAND_5GHZ],
@@ -562,7 +561,6 @@ IEEE80211_IF_FILE(dot11MeshAwakeWindowDuration,
562 561
563static void add_common_files(struct ieee80211_sub_if_data *sdata) 562static void add_common_files(struct ieee80211_sub_if_data *sdata)
564{ 563{
565 DEBUGFS_ADD(drop_unencrypted);
566 DEBUGFS_ADD(rc_rateidx_mask_2ghz); 564 DEBUGFS_ADD(rc_rateidx_mask_2ghz);
567 DEBUGFS_ADD(rc_rateidx_mask_5ghz); 565 DEBUGFS_ADD(rc_rateidx_mask_5ghz);
568 DEBUGFS_ADD(rc_rateidx_mcs_mask_2ghz); 566 DEBUGFS_ADD(rc_rateidx_mcs_mask_2ghz);
diff --git a/net/mac80211/debugfs_sta.c b/net/mac80211/debugfs_sta.c
index 94c70091bbd7..252859e90e8a 100644
--- a/net/mac80211/debugfs_sta.c
+++ b/net/mac80211/debugfs_sta.c
@@ -39,13 +39,6 @@ static const struct file_operations sta_ ##name## _ops = { \
39 .llseek = generic_file_llseek, \ 39 .llseek = generic_file_llseek, \
40} 40}
41 41
42#define STA_OPS_W(name) \
43static const struct file_operations sta_ ##name## _ops = { \
44 .write = sta_##name##_write, \
45 .open = simple_open, \
46 .llseek = generic_file_llseek, \
47}
48
49#define STA_OPS_RW(name) \ 42#define STA_OPS_RW(name) \
50static const struct file_operations sta_ ##name## _ops = { \ 43static const struct file_operations sta_ ##name## _ops = { \
51 .read = sta_##name##_read, \ 44 .read = sta_##name##_read, \
@@ -398,131 +391,6 @@ static ssize_t sta_last_rx_rate_read(struct file *file, char __user *userbuf,
398} 391}
399STA_OPS(last_rx_rate); 392STA_OPS(last_rx_rate);
400 393
401static int
402sta_tx_latency_stat_header(struct ieee80211_tx_latency_bin_ranges *tx_latency,
403 char *buf, int pos, int bufsz)
404{
405 int i;
406 int range_count = tx_latency->n_ranges;
407 u32 *bin_ranges = tx_latency->ranges;
408
409 pos += scnprintf(buf + pos, bufsz - pos,
410 "Station\t\t\tTID\tMax\tAvg");
411 if (range_count) {
412 pos += scnprintf(buf + pos, bufsz - pos,
413 "\t<=%d", bin_ranges[0]);
414 for (i = 0; i < range_count - 1; i++)
415 pos += scnprintf(buf + pos, bufsz - pos, "\t%d-%d",
416 bin_ranges[i], bin_ranges[i+1]);
417 pos += scnprintf(buf + pos, bufsz - pos,
418 "\t%d<", bin_ranges[range_count - 1]);
419 }
420
421 pos += scnprintf(buf + pos, bufsz - pos, "\n");
422
423 return pos;
424}
425
426static int
427sta_tx_latency_stat_table(struct ieee80211_tx_latency_bin_ranges *tx_lat_range,
428 struct ieee80211_tx_latency_stat *tx_lat,
429 char *buf, int pos, int bufsz, int tid)
430{
431 u32 avg = 0;
432 int j;
433 int bin_count = tx_lat->bin_count;
434
435 pos += scnprintf(buf + pos, bufsz - pos, "\t\t\t%d", tid);
436 /* make sure you don't divide in 0 */
437 if (tx_lat->counter)
438 avg = tx_lat->sum / tx_lat->counter;
439
440 pos += scnprintf(buf + pos, bufsz - pos, "\t%d\t%d",
441 tx_lat->max, avg);
442
443 if (tx_lat_range->n_ranges && tx_lat->bins)
444 for (j = 0; j < bin_count; j++)
445 pos += scnprintf(buf + pos, bufsz - pos,
446 "\t%d", tx_lat->bins[j]);
447 pos += scnprintf(buf + pos, bufsz - pos, "\n");
448
449 return pos;
450}
451
452/*
453 * Output Tx latency statistics station && restart all statistics information
454 */
455static ssize_t sta_tx_latency_stat_read(struct file *file,
456 char __user *userbuf,
457 size_t count, loff_t *ppos)
458{
459 struct sta_info *sta = file->private_data;
460 struct ieee80211_local *local = sta->local;
461 struct ieee80211_tx_latency_bin_ranges *tx_latency;
462 char *buf;
463 int bufsz, ret, i;
464 int pos = 0;
465
466 bufsz = 20 * IEEE80211_NUM_TIDS *
467 sizeof(struct ieee80211_tx_latency_stat);
468 buf = kzalloc(bufsz, GFP_KERNEL);
469 if (!buf)
470 return -ENOMEM;
471
472 rcu_read_lock();
473
474 tx_latency = rcu_dereference(local->tx_latency);
475
476 if (!sta->tx_lat) {
477 pos += scnprintf(buf + pos, bufsz - pos,
478 "Tx latency statistics are not enabled\n");
479 goto unlock;
480 }
481
482 pos = sta_tx_latency_stat_header(tx_latency, buf, pos, bufsz);
483
484 pos += scnprintf(buf + pos, bufsz - pos, "%pM\n", sta->sta.addr);
485 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
486 pos = sta_tx_latency_stat_table(tx_latency, &sta->tx_lat[i],
487 buf, pos, bufsz, i);
488unlock:
489 rcu_read_unlock();
490
491 ret = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
492 kfree(buf);
493
494 return ret;
495}
496STA_OPS(tx_latency_stat);
497
498static ssize_t sta_tx_latency_stat_reset_write(struct file *file,
499 const char __user *userbuf,
500 size_t count, loff_t *ppos)
501{
502 u32 *bins;
503 int bin_count;
504 struct sta_info *sta = file->private_data;
505 int i;
506
507 if (!sta->tx_lat)
508 return -EINVAL;
509
510 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
511 bins = sta->tx_lat[i].bins;
512 bin_count = sta->tx_lat[i].bin_count;
513
514 sta->tx_lat[i].max = 0;
515 sta->tx_lat[i].sum = 0;
516 sta->tx_lat[i].counter = 0;
517
518 if (bin_count)
519 memset(bins, 0, bin_count * sizeof(u32));
520 }
521
522 return count;
523}
524STA_OPS_W(tx_latency_stat_reset);
525
526#define DEBUGFS_ADD(name) \ 394#define DEBUGFS_ADD(name) \
527 debugfs_create_file(#name, 0400, \ 395 debugfs_create_file(#name, 0400, \
528 sta->debugfs.dir, sta, &sta_ ##name## _ops); 396 sta->debugfs.dir, sta, &sta_ ##name## _ops);
@@ -576,8 +444,6 @@ void ieee80211_sta_debugfs_add(struct sta_info *sta)
576 DEBUGFS_ADD(last_ack_signal); 444 DEBUGFS_ADD(last_ack_signal);
577 DEBUGFS_ADD(current_tx_rate); 445 DEBUGFS_ADD(current_tx_rate);
578 DEBUGFS_ADD(last_rx_rate); 446 DEBUGFS_ADD(last_rx_rate);
579 DEBUGFS_ADD(tx_latency_stat);
580 DEBUGFS_ADD(tx_latency_stat_reset);
581 447
582 DEBUGFS_ADD_COUNTER(rx_packets, rx_packets); 448 DEBUGFS_ADD_COUNTER(rx_packets, rx_packets);
583 DEBUGFS_ADD_COUNTER(tx_packets, tx_packets); 449 DEBUGFS_ADD_COUNTER(tx_packets, tx_packets);
diff --git a/net/mac80211/driver-ops.h b/net/mac80211/driver-ops.h
index fdeda17b8dd2..26e1ca8a474a 100644
--- a/net/mac80211/driver-ops.h
+++ b/net/mac80211/driver-ops.h
@@ -941,13 +941,13 @@ static inline void drv_set_rekey_data(struct ieee80211_local *local,
941 trace_drv_return_void(local); 941 trace_drv_return_void(local);
942} 942}
943 943
944static inline void drv_rssi_callback(struct ieee80211_local *local, 944static inline void drv_event_callback(struct ieee80211_local *local,
945 struct ieee80211_sub_if_data *sdata, 945 struct ieee80211_sub_if_data *sdata,
946 const enum ieee80211_rssi_event event) 946 const struct ieee80211_event *event)
947{ 947{
948 trace_drv_rssi_callback(local, sdata, event); 948 trace_drv_event_callback(local, sdata, event);
949 if (local->ops->rssi_callback) 949 if (local->ops->event_callback)
950 local->ops->rssi_callback(&local->hw, &sdata->vif, event); 950 local->ops->event_callback(&local->hw, &sdata->vif, event);
951 trace_drv_return_void(local); 951 trace_drv_return_void(local);
952} 952}
953 953
@@ -1367,4 +1367,16 @@ drv_tdls_recv_channel_switch(struct ieee80211_local *local,
1367 trace_drv_return_void(local); 1367 trace_drv_return_void(local);
1368} 1368}
1369 1369
1370static inline void drv_wake_tx_queue(struct ieee80211_local *local,
1371 struct txq_info *txq)
1372{
1373 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->txq.vif);
1374
1375 if (!check_sdata_in_driver(sdata))
1376 return;
1377
1378 trace_drv_wake_tx_queue(local, sdata, txq);
1379 local->ops->wake_tx_queue(&local->hw, &txq->txq);
1380}
1381
1370#endif /* __MAC80211_DRIVER_OPS */ 1382#endif /* __MAC80211_DRIVER_OPS */
diff --git a/net/mac80211/ht.c b/net/mac80211/ht.c
index ff630be2ca75..7a76ce639d58 100644
--- a/net/mac80211/ht.c
+++ b/net/mac80211/ht.c
@@ -252,8 +252,6 @@ bool ieee80211_ht_cap_ie_to_sta_ht_cap(struct ieee80211_sub_if_data *sdata,
252 break; 252 break;
253 } 253 }
254 254
255 if (bw != sta->sta.bandwidth)
256 changed = true;
257 sta->sta.bandwidth = bw; 255 sta->sta.bandwidth = bw;
258 256
259 sta->cur_max_bandwidth = 257 sta->cur_max_bandwidth =
diff --git a/net/mac80211/ibss.c b/net/mac80211/ibss.c
index b606b53a49a7..bfef1b215050 100644
--- a/net/mac80211/ibss.c
+++ b/net/mac80211/ibss.c
@@ -188,6 +188,16 @@ ieee80211_ibss_build_presp(struct ieee80211_sub_if_data *sdata,
188 */ 188 */
189 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap, 189 pos = ieee80211_ie_build_ht_oper(pos, &sband->ht_cap,
190 chandef, 0); 190 chandef, 0);
191
192 /* add VHT capability and information IEs */
193 if (chandef->width != NL80211_CHAN_WIDTH_20 &&
194 chandef->width != NL80211_CHAN_WIDTH_40 &&
195 sband->vht_cap.vht_supported) {
196 pos = ieee80211_ie_build_vht_cap(pos, &sband->vht_cap,
197 sband->vht_cap.cap);
198 pos = ieee80211_ie_build_vht_oper(pos, &sband->vht_cap,
199 chandef);
200 }
191 } 201 }
192 202
193 if (local->hw.queues >= IEEE80211_NUM_ACS) 203 if (local->hw.queues >= IEEE80211_NUM_ACS)
@@ -249,8 +259,6 @@ static void __ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
249 if (presp) 259 if (presp)
250 kfree_rcu(presp, rcu_head); 260 kfree_rcu(presp, rcu_head);
251 261
252 sdata->drop_unencrypted = capability & WLAN_CAPABILITY_PRIVACY ? 1 : 0;
253
254 /* make a copy of the chandef, it could be modified below. */ 262 /* make a copy of the chandef, it could be modified below. */
255 chandef = *req_chandef; 263 chandef = *req_chandef;
256 chan = chandef.chan; 264 chan = chandef.chan;
@@ -417,6 +425,11 @@ static void ieee80211_sta_join_ibss(struct ieee80211_sub_if_data *sdata,
417 NL80211_CHAN_WIDTH_20_NOHT); 425 NL80211_CHAN_WIDTH_20_NOHT);
418 chandef.width = sdata->u.ibss.chandef.width; 426 chandef.width = sdata->u.ibss.chandef.width;
419 break; 427 break;
428 case NL80211_CHAN_WIDTH_80:
429 case NL80211_CHAN_WIDTH_160:
430 chandef = sdata->u.ibss.chandef;
431 chandef.chan = cbss->channel;
432 break;
420 default: 433 default:
421 /* fall back to 20 MHz for unsupported modes */ 434 /* fall back to 20 MHz for unsupported modes */
422 cfg80211_chandef_create(&chandef, cbss->channel, 435 cfg80211_chandef_create(&chandef, cbss->channel,
@@ -470,22 +483,19 @@ int ieee80211_ibss_csa_beacon(struct ieee80211_sub_if_data *sdata,
470 struct beacon_data *presp, *old_presp; 483 struct beacon_data *presp, *old_presp;
471 struct cfg80211_bss *cbss; 484 struct cfg80211_bss *cbss;
472 const struct cfg80211_bss_ies *ies; 485 const struct cfg80211_bss_ies *ies;
473 u16 capability; 486 u16 capability = 0;
474 u64 tsf; 487 u64 tsf;
475 int ret = 0; 488 int ret = 0;
476 489
477 sdata_assert_lock(sdata); 490 sdata_assert_lock(sdata);
478 491
479 capability = WLAN_CAPABILITY_IBSS;
480
481 if (ifibss->privacy) 492 if (ifibss->privacy)
482 capability |= WLAN_CAPABILITY_PRIVACY; 493 capability = WLAN_CAPABILITY_PRIVACY;
483 494
484 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan, 495 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, ifibss->chandef.chan,
485 ifibss->bssid, ifibss->ssid, 496 ifibss->bssid, ifibss->ssid,
486 ifibss->ssid_len, WLAN_CAPABILITY_IBSS | 497 ifibss->ssid_len, IEEE80211_BSS_TYPE_IBSS,
487 WLAN_CAPABILITY_PRIVACY, 498 IEEE80211_PRIVACY(ifibss->privacy));
488 capability);
489 499
490 if (WARN_ON(!cbss)) { 500 if (WARN_ON(!cbss)) {
491 ret = -EINVAL; 501 ret = -EINVAL;
@@ -525,23 +535,17 @@ int ieee80211_ibss_finish_csa(struct ieee80211_sub_if_data *sdata)
525 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss; 535 struct ieee80211_if_ibss *ifibss = &sdata->u.ibss;
526 struct cfg80211_bss *cbss; 536 struct cfg80211_bss *cbss;
527 int err, changed = 0; 537 int err, changed = 0;
528 u16 capability;
529 538
530 sdata_assert_lock(sdata); 539 sdata_assert_lock(sdata);
531 540
532 /* update cfg80211 bss information with the new channel */ 541 /* update cfg80211 bss information with the new channel */
533 if (!is_zero_ether_addr(ifibss->bssid)) { 542 if (!is_zero_ether_addr(ifibss->bssid)) {
534 capability = WLAN_CAPABILITY_IBSS;
535
536 if (ifibss->privacy)
537 capability |= WLAN_CAPABILITY_PRIVACY;
538
539 cbss = cfg80211_get_bss(sdata->local->hw.wiphy, 543 cbss = cfg80211_get_bss(sdata->local->hw.wiphy,
540 ifibss->chandef.chan, 544 ifibss->chandef.chan,
541 ifibss->bssid, ifibss->ssid, 545 ifibss->bssid, ifibss->ssid,
542 ifibss->ssid_len, WLAN_CAPABILITY_IBSS | 546 ifibss->ssid_len,
543 WLAN_CAPABILITY_PRIVACY, 547 IEEE80211_BSS_TYPE_IBSS,
544 capability); 548 IEEE80211_PRIVACY(ifibss->privacy));
545 /* XXX: should not really modify cfg80211 data */ 549 /* XXX: should not really modify cfg80211 data */
546 if (cbss) { 550 if (cbss) {
547 cbss->channel = sdata->csa_chandef.chan; 551 cbss->channel = sdata->csa_chandef.chan;
@@ -682,19 +686,13 @@ static void ieee80211_ibss_disconnect(struct ieee80211_sub_if_data *sdata)
682 struct cfg80211_bss *cbss; 686 struct cfg80211_bss *cbss;
683 struct beacon_data *presp; 687 struct beacon_data *presp;
684 struct sta_info *sta; 688 struct sta_info *sta;
685 u16 capability;
686 689
687 if (!is_zero_ether_addr(ifibss->bssid)) { 690 if (!is_zero_ether_addr(ifibss->bssid)) {
688 capability = WLAN_CAPABILITY_IBSS;
689
690 if (ifibss->privacy)
691 capability |= WLAN_CAPABILITY_PRIVACY;
692
693 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan, 691 cbss = cfg80211_get_bss(local->hw.wiphy, ifibss->chandef.chan,
694 ifibss->bssid, ifibss->ssid, 692 ifibss->bssid, ifibss->ssid,
695 ifibss->ssid_len, WLAN_CAPABILITY_IBSS | 693 ifibss->ssid_len,
696 WLAN_CAPABILITY_PRIVACY, 694 IEEE80211_BSS_TYPE_IBSS,
697 capability); 695 IEEE80211_PRIVACY(ifibss->privacy));
698 696
699 if (cbss) { 697 if (cbss) {
700 cfg80211_unlink_bss(local->hw.wiphy, cbss); 698 cfg80211_unlink_bss(local->hw.wiphy, cbss);
@@ -980,110 +978,140 @@ static void ieee80211_rx_mgmt_auth_ibss(struct ieee80211_sub_if_data *sdata,
980 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0); 978 mgmt->sa, sdata->u.ibss.bssid, NULL, 0, 0, 0);
981} 979}
982 980
983static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata, 981static void ieee80211_update_sta_info(struct ieee80211_sub_if_data *sdata,
984 struct ieee80211_mgmt *mgmt, size_t len, 982 struct ieee80211_mgmt *mgmt, size_t len,
985 struct ieee80211_rx_status *rx_status, 983 struct ieee80211_rx_status *rx_status,
986 struct ieee802_11_elems *elems) 984 struct ieee802_11_elems *elems,
985 struct ieee80211_channel *channel)
987{ 986{
988 struct ieee80211_local *local = sdata->local;
989 struct cfg80211_bss *cbss;
990 struct ieee80211_bss *bss;
991 struct sta_info *sta; 987 struct sta_info *sta;
992 struct ieee80211_channel *channel;
993 u64 beacon_timestamp, rx_timestamp;
994 u32 supp_rates = 0;
995 enum ieee80211_band band = rx_status->band; 988 enum ieee80211_band band = rx_status->band;
996 enum nl80211_bss_scan_width scan_width; 989 enum nl80211_bss_scan_width scan_width;
990 struct ieee80211_local *local = sdata->local;
997 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band]; 991 struct ieee80211_supported_band *sband = local->hw.wiphy->bands[band];
998 bool rates_updated = false; 992 bool rates_updated = false;
993 u32 supp_rates = 0;
999 994
1000 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq); 995 if (sdata->vif.type != NL80211_IFTYPE_ADHOC)
1001 if (!channel)
1002 return; 996 return;
1003 997
1004 if (sdata->vif.type == NL80211_IFTYPE_ADHOC && 998 if (!ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid))
1005 ether_addr_equal(mgmt->bssid, sdata->u.ibss.bssid)) { 999 return;
1006 1000
1007 rcu_read_lock(); 1001 rcu_read_lock();
1008 sta = sta_info_get(sdata, mgmt->sa); 1002 sta = sta_info_get(sdata, mgmt->sa);
1009 1003
1010 if (elems->supp_rates) { 1004 if (elems->supp_rates) {
1011 supp_rates = ieee80211_sta_get_rates(sdata, elems, 1005 supp_rates = ieee80211_sta_get_rates(sdata, elems,
1012 band, NULL); 1006 band, NULL);
1013 if (sta) { 1007 if (sta) {
1014 u32 prev_rates; 1008 u32 prev_rates;
1015 1009
1016 prev_rates = sta->sta.supp_rates[band]; 1010 prev_rates = sta->sta.supp_rates[band];
1017 /* make sure mandatory rates are always added */ 1011 /* make sure mandatory rates are always added */
1018 scan_width = NL80211_BSS_CHAN_WIDTH_20; 1012 scan_width = NL80211_BSS_CHAN_WIDTH_20;
1019 if (rx_status->flag & RX_FLAG_5MHZ) 1013 if (rx_status->flag & RX_FLAG_5MHZ)
1020 scan_width = NL80211_BSS_CHAN_WIDTH_5; 1014 scan_width = NL80211_BSS_CHAN_WIDTH_5;
1021 if (rx_status->flag & RX_FLAG_10MHZ) 1015 if (rx_status->flag & RX_FLAG_10MHZ)
1022 scan_width = NL80211_BSS_CHAN_WIDTH_10; 1016 scan_width = NL80211_BSS_CHAN_WIDTH_10;
1023 1017
1024 sta->sta.supp_rates[band] = supp_rates | 1018 sta->sta.supp_rates[band] = supp_rates |
1025 ieee80211_mandatory_rates(sband, 1019 ieee80211_mandatory_rates(sband, scan_width);
1026 scan_width); 1020 if (sta->sta.supp_rates[band] != prev_rates) {
1027 if (sta->sta.supp_rates[band] != prev_rates) { 1021 ibss_dbg(sdata,
1028 ibss_dbg(sdata, 1022 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n",
1029 "updated supp_rates set for %pM based on beacon/probe_resp (0x%x -> 0x%x)\n", 1023 sta->sta.addr, prev_rates,
1030 sta->sta.addr, prev_rates, 1024 sta->sta.supp_rates[band]);
1031 sta->sta.supp_rates[band]); 1025 rates_updated = true;
1032 rates_updated = true;
1033 }
1034 } else {
1035 rcu_read_unlock();
1036 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1037 mgmt->sa, supp_rates);
1038 } 1026 }
1027 } else {
1028 rcu_read_unlock();
1029 sta = ieee80211_ibss_add_sta(sdata, mgmt->bssid,
1030 mgmt->sa, supp_rates);
1039 } 1031 }
1032 }
1040 1033
1041 if (sta && elems->wmm_info) 1034 if (sta && elems->wmm_info && local->hw.queues >= IEEE80211_NUM_ACS)
1042 sta->sta.wme = true; 1035 sta->sta.wme = true;
1043 1036
1044 if (sta && elems->ht_operation && elems->ht_cap_elem && 1037 if (sta && elems->ht_operation && elems->ht_cap_elem &&
1045 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT && 1038 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20_NOHT &&
1046 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 && 1039 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_5 &&
1047 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) { 1040 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_10) {
1048 /* we both use HT */ 1041 /* we both use HT */
1049 struct ieee80211_ht_cap htcap_ie; 1042 struct ieee80211_ht_cap htcap_ie;
1050 struct cfg80211_chan_def chandef; 1043 struct cfg80211_chan_def chandef;
1051 1044 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
1052 ieee80211_ht_oper_to_chandef(channel, 1045
1053 elems->ht_operation, 1046 ieee80211_ht_oper_to_chandef(channel,
1054 &chandef); 1047 elems->ht_operation,
1055 1048 &chandef);
1056 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie)); 1049
1057 1050 memcpy(&htcap_ie, elems->ht_cap_elem, sizeof(htcap_ie));
1058 /* 1051 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap(sdata, sband,
1059 * fall back to HT20 if we don't use or use 1052 &htcap_ie,
1060 * the other extension channel 1053 sta);
1061 */ 1054
1062 if (chandef.center_freq1 != 1055 if (elems->vht_operation && elems->vht_cap_elem &&
1063 sdata->u.ibss.chandef.center_freq1) 1056 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_20 &&
1064 htcap_ie.cap_info &= 1057 sdata->u.ibss.chandef.width != NL80211_CHAN_WIDTH_40) {
1065 cpu_to_le16(~IEEE80211_HT_CAP_SUP_WIDTH_20_40); 1058 /* we both use VHT */
1066 1059 struct ieee80211_vht_cap cap_ie;
1067 rates_updated |= ieee80211_ht_cap_ie_to_sta_ht_cap( 1060 struct ieee80211_sta_vht_cap cap = sta->sta.vht_cap;
1068 sdata, sband, &htcap_ie, sta); 1061
1062 ieee80211_vht_oper_to_chandef(channel,
1063 elems->vht_operation,
1064 &chandef);
1065 memcpy(&cap_ie, elems->vht_cap_elem, sizeof(cap_ie));
1066 ieee80211_vht_cap_ie_to_sta_vht_cap(sdata, sband,
1067 &cap_ie, sta);
1068 if (memcmp(&cap, &sta->sta.vht_cap, sizeof(cap)))
1069 rates_updated |= true;
1069 } 1070 }
1070 1071
1071 if (sta && rates_updated) { 1072 if (bw != sta->sta.bandwidth)
1072 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED; 1073 rates_updated |= true;
1073 u8 rx_nss = sta->sta.rx_nss;
1074 1074
1075 /* Force rx_nss recalculation */ 1075 if (!cfg80211_chandef_compatible(&sdata->u.ibss.chandef,
1076 sta->sta.rx_nss = 0; 1076 &chandef))
1077 rate_control_rate_init(sta); 1077 WARN_ON_ONCE(1);
1078 if (sta->sta.rx_nss != rx_nss) 1078 }
1079 changed |= IEEE80211_RC_NSS_CHANGED;
1080 1079
1081 drv_sta_rc_update(local, sdata, &sta->sta, changed); 1080 if (sta && rates_updated) {
1082 } 1081 u32 changed = IEEE80211_RC_SUPP_RATES_CHANGED;
1082 u8 rx_nss = sta->sta.rx_nss;
1083 1083
1084 rcu_read_unlock(); 1084 /* Force rx_nss recalculation */
1085 sta->sta.rx_nss = 0;
1086 rate_control_rate_init(sta);
1087 if (sta->sta.rx_nss != rx_nss)
1088 changed |= IEEE80211_RC_NSS_CHANGED;
1089
1090 drv_sta_rc_update(local, sdata, &sta->sta, changed);
1085 } 1091 }
1086 1092
1093 rcu_read_unlock();
1094}
1095
1096static void ieee80211_rx_bss_info(struct ieee80211_sub_if_data *sdata,
1097 struct ieee80211_mgmt *mgmt, size_t len,
1098 struct ieee80211_rx_status *rx_status,
1099 struct ieee802_11_elems *elems)
1100{
1101 struct ieee80211_local *local = sdata->local;
1102 struct cfg80211_bss *cbss;
1103 struct ieee80211_bss *bss;
1104 struct ieee80211_channel *channel;
1105 u64 beacon_timestamp, rx_timestamp;
1106 u32 supp_rates = 0;
1107 enum ieee80211_band band = rx_status->band;
1108
1109 channel = ieee80211_get_channel(local->hw.wiphy, rx_status->freq);
1110 if (!channel)
1111 return;
1112
1113 ieee80211_update_sta_info(sdata, mgmt, len, rx_status, elems, channel);
1114
1087 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems, 1115 bss = ieee80211_bss_info_update(local, rx_status, mgmt, len, elems,
1088 channel); 1116 channel);
1089 if (!bss) 1117 if (!bss)
@@ -1273,7 +1301,7 @@ static void ieee80211_sta_merge_ibss(struct ieee80211_sub_if_data *sdata)
1273 1301
1274 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef); 1302 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
1275 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len, 1303 ieee80211_request_ibss_scan(sdata, ifibss->ssid, ifibss->ssid_len,
1276 NULL, scan_width); 1304 NULL, 0, scan_width);
1277} 1305}
1278 1306
1279static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata) 1307static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
@@ -1304,14 +1332,82 @@ static void ieee80211_sta_create_ibss(struct ieee80211_sub_if_data *sdata)
1304 1332
1305 if (ifibss->privacy) 1333 if (ifibss->privacy)
1306 capability |= WLAN_CAPABILITY_PRIVACY; 1334 capability |= WLAN_CAPABILITY_PRIVACY;
1307 else
1308 sdata->drop_unencrypted = 0;
1309 1335
1310 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int, 1336 __ieee80211_sta_join_ibss(sdata, bssid, sdata->vif.bss_conf.beacon_int,
1311 &ifibss->chandef, ifibss->basic_rates, 1337 &ifibss->chandef, ifibss->basic_rates,
1312 capability, 0, true); 1338 capability, 0, true);
1313} 1339}
1314 1340
1341static unsigned ibss_setup_channels(struct wiphy *wiphy,
1342 struct ieee80211_channel **channels,
1343 unsigned int channels_max,
1344 u32 center_freq, u32 width)
1345{
1346 struct ieee80211_channel *chan = NULL;
1347 unsigned int n_chan = 0;
1348 u32 start_freq, end_freq, freq;
1349
1350 if (width <= 20) {
1351 start_freq = center_freq;
1352 end_freq = center_freq;
1353 } else {
1354 start_freq = center_freq - width / 2 + 10;
1355 end_freq = center_freq + width / 2 - 10;
1356 }
1357
1358 for (freq = start_freq; freq <= end_freq; freq += 20) {
1359 chan = ieee80211_get_channel(wiphy, freq);
1360 if (!chan)
1361 continue;
1362 if (n_chan >= channels_max)
1363 return n_chan;
1364
1365 channels[n_chan] = chan;
1366 n_chan++;
1367 }
1368
1369 return n_chan;
1370}
1371
1372static unsigned int
1373ieee80211_ibss_setup_scan_channels(struct wiphy *wiphy,
1374 const struct cfg80211_chan_def *chandef,
1375 struct ieee80211_channel **channels,
1376 unsigned int channels_max)
1377{
1378 unsigned int n_chan = 0;
1379 u32 width, cf1, cf2 = 0;
1380
1381 switch (chandef->width) {
1382 case NL80211_CHAN_WIDTH_40:
1383 width = 40;
1384 break;
1385 case NL80211_CHAN_WIDTH_80P80:
1386 cf2 = chandef->center_freq2;
1387 /* fall through */
1388 case NL80211_CHAN_WIDTH_80:
1389 width = 80;
1390 break;
1391 case NL80211_CHAN_WIDTH_160:
1392 width = 160;
1393 break;
1394 default:
1395 width = 20;
1396 break;
1397 }
1398
1399 cf1 = chandef->center_freq1;
1400
1401 n_chan = ibss_setup_channels(wiphy, channels, channels_max, cf1, width);
1402
1403 if (cf2)
1404 n_chan += ibss_setup_channels(wiphy, &channels[n_chan],
1405 channels_max - n_chan, cf2,
1406 width);
1407
1408 return n_chan;
1409}
1410
1315/* 1411/*
1316 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH 1412 * This function is called with state == IEEE80211_IBSS_MLME_SEARCH
1317 */ 1413 */
@@ -1325,7 +1421,6 @@ static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1325 const u8 *bssid = NULL; 1421 const u8 *bssid = NULL;
1326 enum nl80211_bss_scan_width scan_width; 1422 enum nl80211_bss_scan_width scan_width;
1327 int active_ibss; 1423 int active_ibss;
1328 u16 capability;
1329 1424
1330 sdata_assert_lock(sdata); 1425 sdata_assert_lock(sdata);
1331 1426
@@ -1335,9 +1430,6 @@ static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1335 if (active_ibss) 1430 if (active_ibss)
1336 return; 1431 return;
1337 1432
1338 capability = WLAN_CAPABILITY_IBSS;
1339 if (ifibss->privacy)
1340 capability |= WLAN_CAPABILITY_PRIVACY;
1341 if (ifibss->fixed_bssid) 1433 if (ifibss->fixed_bssid)
1342 bssid = ifibss->bssid; 1434 bssid = ifibss->bssid;
1343 if (ifibss->fixed_channel) 1435 if (ifibss->fixed_channel)
@@ -1346,8 +1438,8 @@ static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1346 bssid = ifibss->bssid; 1438 bssid = ifibss->bssid;
1347 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid, 1439 cbss = cfg80211_get_bss(local->hw.wiphy, chan, bssid,
1348 ifibss->ssid, ifibss->ssid_len, 1440 ifibss->ssid, ifibss->ssid_len,
1349 WLAN_CAPABILITY_IBSS | WLAN_CAPABILITY_PRIVACY, 1441 IEEE80211_BSS_TYPE_IBSS,
1350 capability); 1442 IEEE80211_PRIVACY(ifibss->privacy));
1351 1443
1352 if (cbss) { 1444 if (cbss) {
1353 struct ieee80211_bss *bss; 1445 struct ieee80211_bss *bss;
@@ -1381,11 +1473,18 @@ static void ieee80211_sta_find_ibss(struct ieee80211_sub_if_data *sdata)
1381 /* Selected IBSS not found in current scan results - try to scan */ 1473 /* Selected IBSS not found in current scan results - try to scan */
1382 if (time_after(jiffies, ifibss->last_scan_completed + 1474 if (time_after(jiffies, ifibss->last_scan_completed +
1383 IEEE80211_SCAN_INTERVAL)) { 1475 IEEE80211_SCAN_INTERVAL)) {
1476 struct ieee80211_channel *channels[8];
1477 unsigned int num;
1478
1384 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n"); 1479 sdata_info(sdata, "Trigger new scan to find an IBSS to join\n");
1385 1480
1481 num = ieee80211_ibss_setup_scan_channels(local->hw.wiphy,
1482 &ifibss->chandef,
1483 channels,
1484 ARRAY_SIZE(channels));
1386 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef); 1485 scan_width = cfg80211_chandef_to_scan_width(&ifibss->chandef);
1387 ieee80211_request_ibss_scan(sdata, ifibss->ssid, 1486 ieee80211_request_ibss_scan(sdata, ifibss->ssid,
1388 ifibss->ssid_len, chan, 1487 ifibss->ssid_len, channels, num,
1389 scan_width); 1488 scan_width);
1390 } else { 1489 } else {
1391 int interval = IEEE80211_SCAN_INTERVAL; 1490 int interval = IEEE80211_SCAN_INTERVAL;
@@ -1742,7 +1841,7 @@ int ieee80211_ibss_leave(struct ieee80211_sub_if_data *sdata)
1742 1841
1743 ieee80211_ibss_disconnect(sdata); 1842 ieee80211_ibss_disconnect(sdata);
1744 ifibss->ssid_len = 0; 1843 ifibss->ssid_len = 0;
1745 memset(ifibss->bssid, 0, ETH_ALEN); 1844 eth_zero_addr(ifibss->bssid);
1746 1845
1747 /* remove beacon */ 1846 /* remove beacon */
1748 kfree(sdata->u.ibss.ie); 1847 kfree(sdata->u.ibss.ie);
diff --git a/net/mac80211/ieee80211_i.h b/net/mac80211/ieee80211_i.h
index 8d53d65bd2ab..ab46ab4a7249 100644
--- a/net/mac80211/ieee80211_i.h
+++ b/net/mac80211/ieee80211_i.h
@@ -26,6 +26,7 @@
26#include <linux/etherdevice.h> 26#include <linux/etherdevice.h>
27#include <linux/leds.h> 27#include <linux/leds.h>
28#include <linux/idr.h> 28#include <linux/idr.h>
29#include <linux/rhashtable.h>
29#include <net/ieee80211_radiotap.h> 30#include <net/ieee80211_radiotap.h>
30#include <net/cfg80211.h> 31#include <net/cfg80211.h>
31#include <net/mac80211.h> 32#include <net/mac80211.h>
@@ -810,6 +811,19 @@ struct mac80211_qos_map {
810 struct rcu_head rcu_head; 811 struct rcu_head rcu_head;
811}; 812};
812 813
814enum txq_info_flags {
815 IEEE80211_TXQ_STOP,
816 IEEE80211_TXQ_AMPDU,
817};
818
819struct txq_info {
820 struct sk_buff_head queue;
821 unsigned long flags;
822
823 /* keep last! */
824 struct ieee80211_txq txq;
825};
826
813struct ieee80211_sub_if_data { 827struct ieee80211_sub_if_data {
814 struct list_head list; 828 struct list_head list;
815 829
@@ -830,8 +844,6 @@ struct ieee80211_sub_if_data {
830 844
831 unsigned long state; 845 unsigned long state;
832 846
833 int drop_unencrypted;
834
835 char name[IFNAMSIZ]; 847 char name[IFNAMSIZ];
836 848
837 /* Fragment table for host-based reassembly */ 849 /* Fragment table for host-based reassembly */
@@ -854,6 +866,7 @@ struct ieee80211_sub_if_data {
854 bool control_port_no_encrypt; 866 bool control_port_no_encrypt;
855 int encrypt_headroom; 867 int encrypt_headroom;
856 868
869 atomic_t txqs_len[IEEE80211_NUM_ACS];
857 struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS]; 870 struct ieee80211_tx_queue_params tx_conf[IEEE80211_NUM_ACS];
858 struct mac80211_qos_map __rcu *qos_map; 871 struct mac80211_qos_map __rcu *qos_map;
859 872
@@ -1042,24 +1055,6 @@ struct tpt_led_trigger {
1042}; 1055};
1043#endif 1056#endif
1044 1057
1045/*
1046 * struct ieee80211_tx_latency_bin_ranges - Tx latency statistics bins ranges
1047 *
1048 * Measuring Tx latency statistics. Counts how many Tx frames transmitted in a
1049 * certain latency range (in Milliseconds). Each station that uses these
1050 * ranges will have bins to count the amount of frames received in that range.
1051 * The user can configure the ranges via debugfs.
1052 * If ranges is NULL then Tx latency statistics bins are disabled for all
1053 * stations.
1054 *
1055 * @n_ranges: number of ranges that are taken in account
1056 * @ranges: the ranges that the user requested or NULL if disabled.
1057 */
1058struct ieee80211_tx_latency_bin_ranges {
1059 int n_ranges;
1060 u32 ranges[];
1061};
1062
1063/** 1058/**
1064 * mac80211 scan flags - currently active scan mode 1059 * mac80211 scan flags - currently active scan mode
1065 * 1060 *
@@ -1207,16 +1202,10 @@ struct ieee80211_local {
1207 spinlock_t tim_lock; 1202 spinlock_t tim_lock;
1208 unsigned long num_sta; 1203 unsigned long num_sta;
1209 struct list_head sta_list; 1204 struct list_head sta_list;
1210 struct sta_info __rcu *sta_hash[STA_HASH_SIZE]; 1205 struct rhashtable sta_hash;
1211 struct timer_list sta_cleanup; 1206 struct timer_list sta_cleanup;
1212 int sta_generation; 1207 int sta_generation;
1213 1208
1214 /*
1215 * Tx latency statistics parameters for all stations.
1216 * Can enable via debugfs (NULL when disabled).
1217 */
1218 struct ieee80211_tx_latency_bin_ranges __rcu *tx_latency;
1219
1220 struct sk_buff_head pending[IEEE80211_MAX_QUEUES]; 1209 struct sk_buff_head pending[IEEE80211_MAX_QUEUES];
1221 struct tasklet_struct tx_pending_tasklet; 1210 struct tasklet_struct tx_pending_tasklet;
1222 1211
@@ -1298,7 +1287,6 @@ struct ieee80211_local {
1298 /* TX/RX handler statistics */ 1287 /* TX/RX handler statistics */
1299 unsigned int tx_handlers_drop; 1288 unsigned int tx_handlers_drop;
1300 unsigned int tx_handlers_queued; 1289 unsigned int tx_handlers_queued;
1301 unsigned int tx_handlers_drop_unencrypted;
1302 unsigned int tx_handlers_drop_fragment; 1290 unsigned int tx_handlers_drop_fragment;
1303 unsigned int tx_handlers_drop_wep; 1291 unsigned int tx_handlers_drop_wep;
1304 unsigned int tx_handlers_drop_not_assoc; 1292 unsigned int tx_handlers_drop_not_assoc;
@@ -1476,6 +1464,10 @@ static inline struct ieee80211_local *hw_to_local(
1476 return container_of(hw, struct ieee80211_local, hw); 1464 return container_of(hw, struct ieee80211_local, hw);
1477} 1465}
1478 1466
1467static inline struct txq_info *to_txq_info(struct ieee80211_txq *txq)
1468{
1469 return container_of(txq, struct txq_info, txq);
1470}
1479 1471
1480static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr) 1472static inline int ieee80211_bssid_match(const u8 *raddr, const u8 *addr)
1481{ 1473{
@@ -1568,7 +1560,8 @@ int ieee80211_mesh_finish_csa(struct ieee80211_sub_if_data *sdata);
1568void ieee80211_scan_work(struct work_struct *work); 1560void ieee80211_scan_work(struct work_struct *work);
1569int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 1561int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
1570 const u8 *ssid, u8 ssid_len, 1562 const u8 *ssid, u8 ssid_len,
1571 struct ieee80211_channel *chan, 1563 struct ieee80211_channel **channels,
1564 unsigned int n_channels,
1572 enum nl80211_bss_scan_width scan_width); 1565 enum nl80211_bss_scan_width scan_width);
1573int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata, 1566int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
1574 struct cfg80211_scan_request *req); 1567 struct cfg80211_scan_request *req);
@@ -1617,6 +1610,7 @@ int ieee80211_channel_switch(struct wiphy *wiphy, struct net_device *dev,
1617int ieee80211_iface_init(void); 1610int ieee80211_iface_init(void);
1618void ieee80211_iface_exit(void); 1611void ieee80211_iface_exit(void);
1619int ieee80211_if_add(struct ieee80211_local *local, const char *name, 1612int ieee80211_if_add(struct ieee80211_local *local, const char *name,
1613 unsigned char name_assign_type,
1620 struct wireless_dev **new_wdev, enum nl80211_iftype type, 1614 struct wireless_dev **new_wdev, enum nl80211_iftype type,
1621 struct vif_params *params); 1615 struct vif_params *params);
1622int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata, 1616int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
@@ -1784,7 +1778,8 @@ void mac80211_ev_michael_mic_failure(struct ieee80211_sub_if_data *sdata, int ke
1784 gfp_t gfp); 1778 gfp_t gfp);
1785void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata, 1779void ieee80211_set_wmm_default(struct ieee80211_sub_if_data *sdata,
1786 bool bss_notify); 1780 bool bss_notify);
1787void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb); 1781void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1782 struct sta_info *sta, struct sk_buff *skb);
1788 1783
1789void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata, 1784void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
1790 struct sk_buff *skb, int tid, 1785 struct sk_buff *skb, int tid,
@@ -1929,6 +1924,9 @@ static inline bool ieee80211_can_run_worker(struct ieee80211_local *local)
1929 return true; 1924 return true;
1930} 1925}
1931 1926
1927void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
1928 struct sta_info *sta,
1929 struct txq_info *txq, int tid);
1932void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata, 1930void ieee80211_send_auth(struct ieee80211_sub_if_data *sdata,
1933 u16 transaction, u16 auth_alg, u16 status, 1931 u16 transaction, u16 auth_alg, u16 status,
1934 const u8 *extra, size_t extra_len, const u8 *bssid, 1932 const u8 *extra, size_t extra_len, const u8 *bssid,
@@ -1967,10 +1965,6 @@ int __ieee80211_request_smps_ap(struct ieee80211_sub_if_data *sdata,
1967void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata); 1965void ieee80211_recalc_smps(struct ieee80211_sub_if_data *sdata);
1968void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata); 1966void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata);
1969 1967
1970size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1971 const u8 *ids, int n_ids,
1972 const u8 *after_ric, int n_after_ric,
1973 size_t offset);
1974size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset); 1968size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset);
1975u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap, 1969u8 *ieee80211_ie_build_ht_cap(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
1976 u16 cap); 1970 u16 cap);
@@ -1979,6 +1973,8 @@ u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
1979 u16 prot_mode); 1973 u16 prot_mode);
1980u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap, 1974u8 *ieee80211_ie_build_vht_cap(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
1981 u32 cap); 1975 u32 cap);
1976u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
1977 const struct cfg80211_chan_def *chandef);
1982int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef, 1978int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
1983 const struct ieee80211_supported_band *sband, 1979 const struct ieee80211_supported_band *sband,
1984 const u8 *srates, int srates_len, u32 *rates); 1980 const u8 *srates, int srates_len, u32 *rates);
@@ -1994,6 +1990,9 @@ u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo);
1994void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan, 1990void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
1995 const struct ieee80211_ht_operation *ht_oper, 1991 const struct ieee80211_ht_operation *ht_oper,
1996 struct cfg80211_chan_def *chandef); 1992 struct cfg80211_chan_def *chandef);
1993void ieee80211_vht_oper_to_chandef(struct ieee80211_channel *control_chan,
1994 const struct ieee80211_vht_operation *oper,
1995 struct cfg80211_chan_def *chandef);
1997u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c); 1996u32 ieee80211_chandef_downgrade(struct cfg80211_chan_def *c);
1998 1997
1999int __must_check 1998int __must_check
diff --git a/net/mac80211/iface.c b/net/mac80211/iface.c
index 81a27516813e..b4ac596a7cb7 100644
--- a/net/mac80211/iface.c
+++ b/net/mac80211/iface.c
@@ -969,6 +969,13 @@ static void ieee80211_do_stop(struct ieee80211_sub_if_data *sdata,
969 } 969 }
970 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 970 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
971 971
972 if (sdata->vif.txq) {
973 struct txq_info *txqi = to_txq_info(sdata->vif.txq);
974
975 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
976 atomic_set(&sdata->txqs_len[txqi->txq.ac], 0);
977 }
978
972 if (local->open_count == 0) 979 if (local->open_count == 0)
973 ieee80211_clear_tx_pending(local); 980 ieee80211_clear_tx_pending(local);
974 981
@@ -1508,7 +1515,6 @@ int ieee80211_if_change_type(struct ieee80211_sub_if_data *sdata,
1508 } 1515 }
1509 1516
1510 /* reset some values that shouldn't be kept across type changes */ 1517 /* reset some values that shouldn't be kept across type changes */
1511 sdata->drop_unencrypted = 0;
1512 if (type == NL80211_IFTYPE_STATION) 1518 if (type == NL80211_IFTYPE_STATION)
1513 sdata->u.mgd.use_4addr = false; 1519 sdata->u.mgd.use_4addr = false;
1514 1520
@@ -1649,11 +1655,13 @@ static void ieee80211_assign_perm_addr(struct ieee80211_local *local,
1649} 1655}
1650 1656
1651int ieee80211_if_add(struct ieee80211_local *local, const char *name, 1657int ieee80211_if_add(struct ieee80211_local *local, const char *name,
1658 unsigned char name_assign_type,
1652 struct wireless_dev **new_wdev, enum nl80211_iftype type, 1659 struct wireless_dev **new_wdev, enum nl80211_iftype type,
1653 struct vif_params *params) 1660 struct vif_params *params)
1654{ 1661{
1655 struct net_device *ndev = NULL; 1662 struct net_device *ndev = NULL;
1656 struct ieee80211_sub_if_data *sdata = NULL; 1663 struct ieee80211_sub_if_data *sdata = NULL;
1664 struct txq_info *txqi;
1657 int ret, i; 1665 int ret, i;
1658 int txqs = 1; 1666 int txqs = 1;
1659 1667
@@ -1673,11 +1681,19 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
1673 ieee80211_assign_perm_addr(local, wdev->address, type); 1681 ieee80211_assign_perm_addr(local, wdev->address, type);
1674 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN); 1682 memcpy(sdata->vif.addr, wdev->address, ETH_ALEN);
1675 } else { 1683 } else {
1684 int size = ALIGN(sizeof(*sdata) + local->hw.vif_data_size,
1685 sizeof(void *));
1686 int txq_size = 0;
1687
1688 if (local->ops->wake_tx_queue)
1689 txq_size += sizeof(struct txq_info) +
1690 local->hw.txq_data_size;
1691
1676 if (local->hw.queues >= IEEE80211_NUM_ACS) 1692 if (local->hw.queues >= IEEE80211_NUM_ACS)
1677 txqs = IEEE80211_NUM_ACS; 1693 txqs = IEEE80211_NUM_ACS;
1678 1694
1679 ndev = alloc_netdev_mqs(sizeof(*sdata) + local->hw.vif_data_size, 1695 ndev = alloc_netdev_mqs(size + txq_size,
1680 name, NET_NAME_UNKNOWN, 1696 name, name_assign_type,
1681 ieee80211_if_setup, txqs, 1); 1697 ieee80211_if_setup, txqs, 1);
1682 if (!ndev) 1698 if (!ndev)
1683 return -ENOMEM; 1699 return -ENOMEM;
@@ -1711,6 +1727,11 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name,
1711 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN); 1727 memcpy(sdata->vif.addr, ndev->dev_addr, ETH_ALEN);
1712 memcpy(sdata->name, ndev->name, IFNAMSIZ); 1728 memcpy(sdata->name, ndev->name, IFNAMSIZ);
1713 1729
1730 if (txq_size) {
1731 txqi = netdev_priv(ndev) + size;
1732 ieee80211_init_tx_queue(sdata, NULL, txqi, 0);
1733 }
1734
1714 sdata->dev = ndev; 1735 sdata->dev = ndev;
1715 } 1736 }
1716 1737
diff --git a/net/mac80211/key.c b/net/mac80211/key.c
index 0825d76edcfc..2291cd730091 100644
--- a/net/mac80211/key.c
+++ b/net/mac80211/key.c
@@ -492,6 +492,7 @@ ieee80211_key_alloc(u32 cipher, int idx, size_t key_len,
492 for (j = 0; j < len; j++) 492 for (j = 0; j < len; j++)
493 key->u.gen.rx_pn[i][j] = 493 key->u.gen.rx_pn[i][j] =
494 seq[len - j - 1]; 494 seq[len - j - 1];
495 key->flags |= KEY_FLAG_CIPHER_SCHEME;
495 } 496 }
496 } 497 }
497 memcpy(key->conf.key, key_data, key_len); 498 memcpy(key->conf.key, key_data, key_len);
diff --git a/net/mac80211/key.h b/net/mac80211/key.h
index d57a9915494f..c5a31835be0e 100644
--- a/net/mac80211/key.h
+++ b/net/mac80211/key.h
@@ -30,10 +30,12 @@ struct sta_info;
30 * @KEY_FLAG_UPLOADED_TO_HARDWARE: Indicates that this key is present 30 * @KEY_FLAG_UPLOADED_TO_HARDWARE: Indicates that this key is present
31 * in the hardware for TX crypto hardware acceleration. 31 * in the hardware for TX crypto hardware acceleration.
32 * @KEY_FLAG_TAINTED: Key is tainted and packets should be dropped. 32 * @KEY_FLAG_TAINTED: Key is tainted and packets should be dropped.
33 * @KEY_FLAG_CIPHER_SCHEME: This key is for a hardware cipher scheme
33 */ 34 */
34enum ieee80211_internal_key_flags { 35enum ieee80211_internal_key_flags {
35 KEY_FLAG_UPLOADED_TO_HARDWARE = BIT(0), 36 KEY_FLAG_UPLOADED_TO_HARDWARE = BIT(0),
36 KEY_FLAG_TAINTED = BIT(1), 37 KEY_FLAG_TAINTED = BIT(1),
38 KEY_FLAG_CIPHER_SCHEME = BIT(2),
37}; 39};
38 40
39enum ieee80211_internal_tkip_state { 41enum ieee80211_internal_tkip_state {
diff --git a/net/mac80211/main.c b/net/mac80211/main.c
index 5e09d354c5a5..df3051d96aff 100644
--- a/net/mac80211/main.c
+++ b/net/mac80211/main.c
@@ -557,6 +557,9 @@ struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
557 557
558 local = wiphy_priv(wiphy); 558 local = wiphy_priv(wiphy);
559 559
560 if (sta_info_init(local))
561 goto err_free;
562
560 local->hw.wiphy = wiphy; 563 local->hw.wiphy = wiphy;
561 564
562 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN); 565 local->hw.priv = (char *)local + ALIGN(sizeof(*local), NETDEV_ALIGN);
@@ -629,8 +632,6 @@ struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
629 spin_lock_init(&local->ack_status_lock); 632 spin_lock_init(&local->ack_status_lock);
630 idr_init(&local->ack_status_frames); 633 idr_init(&local->ack_status_frames);
631 634
632 sta_info_init(local);
633
634 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) { 635 for (i = 0; i < IEEE80211_MAX_QUEUES; i++) {
635 skb_queue_head_init(&local->pending[i]); 636 skb_queue_head_init(&local->pending[i]);
636 atomic_set(&local->agg_queue_stop[i], 0); 637 atomic_set(&local->agg_queue_stop[i], 0);
@@ -650,6 +651,9 @@ struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,
650 ieee80211_roc_setup(local); 651 ieee80211_roc_setup(local);
651 652
652 return &local->hw; 653 return &local->hw;
654 err_free:
655 wiphy_free(wiphy);
656 return NULL;
653} 657}
654EXPORT_SYMBOL(ieee80211_alloc_hw_nm); 658EXPORT_SYMBOL(ieee80211_alloc_hw_nm);
655 659
@@ -1035,6 +1039,9 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
1035 1039
1036 local->dynamic_ps_forced_timeout = -1; 1040 local->dynamic_ps_forced_timeout = -1;
1037 1041
1042 if (!local->hw.txq_ac_max_pending)
1043 local->hw.txq_ac_max_pending = 64;
1044
1038 result = ieee80211_wep_init(local); 1045 result = ieee80211_wep_init(local);
1039 if (result < 0) 1046 if (result < 0)
1040 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n", 1047 wiphy_debug(local->hw.wiphy, "Failed to initialize wep: %d\n",
@@ -1057,7 +1064,7 @@ int ieee80211_register_hw(struct ieee80211_hw *hw)
1057 /* add one default STA interface if supported */ 1064 /* add one default STA interface if supported */
1058 if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) && 1065 if (local->hw.wiphy->interface_modes & BIT(NL80211_IFTYPE_STATION) &&
1059 !(hw->flags & IEEE80211_HW_NO_AUTO_VIF)) { 1066 !(hw->flags & IEEE80211_HW_NO_AUTO_VIF)) {
1060 result = ieee80211_if_add(local, "wlan%d", NULL, 1067 result = ieee80211_if_add(local, "wlan%d", NET_NAME_ENUM, NULL,
1061 NL80211_IFTYPE_STATION, NULL); 1068 NL80211_IFTYPE_STATION, NULL);
1062 if (result) 1069 if (result)
1063 wiphy_warn(local->hw.wiphy, 1070 wiphy_warn(local->hw.wiphy,
@@ -1173,7 +1180,6 @@ void ieee80211_unregister_hw(struct ieee80211_hw *hw)
1173 1180
1174 destroy_workqueue(local->workqueue); 1181 destroy_workqueue(local->workqueue);
1175 wiphy_unregister(local->hw.wiphy); 1182 wiphy_unregister(local->hw.wiphy);
1176 sta_info_stop(local);
1177 ieee80211_wep_free(local); 1183 ieee80211_wep_free(local);
1178 ieee80211_led_exit(local); 1184 ieee80211_led_exit(local);
1179 kfree(local->int_scan_req); 1185 kfree(local->int_scan_req);
@@ -1201,8 +1207,6 @@ void ieee80211_free_hw(struct ieee80211_hw *hw)
1201 ieee80211_free_ack_frame, NULL); 1207 ieee80211_free_ack_frame, NULL);
1202 idr_destroy(&local->ack_status_frames); 1208 idr_destroy(&local->ack_status_frames);
1203 1209
1204 kfree(rcu_access_pointer(local->tx_latency));
1205
1206 sta_info_stop(local); 1210 sta_info_stop(local);
1207 1211
1208 wiphy_free(local->hw.wiphy); 1212 wiphy_free(local->hw.wiphy);
diff --git a/net/mac80211/mesh.c b/net/mac80211/mesh.c
index 0c8b2a77d312..d4684242e78b 100644
--- a/net/mac80211/mesh.c
+++ b/net/mac80211/mesh.c
@@ -520,7 +520,7 @@ int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
520 } else { 520 } else {
521 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS); 521 *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
522 /* RA TA DA SA */ 522 /* RA TA DA SA */
523 memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */ 523 eth_zero_addr(hdr->addr1); /* RA is resolved later */
524 memcpy(hdr->addr2, meshsa, ETH_ALEN); 524 memcpy(hdr->addr2, meshsa, ETH_ALEN);
525 memcpy(hdr->addr3, meshda, ETH_ALEN); 525 memcpy(hdr->addr3, meshda, ETH_ALEN);
526 memcpy(hdr->addr4, meshsa, ETH_ALEN); 526 memcpy(hdr->addr4, meshsa, ETH_ALEN);
@@ -574,7 +574,8 @@ static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata)
574 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh; 574 struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
575 u32 changed; 575 u32 changed;
576 576
577 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ); 577 if (ifmsh->mshcfg.plink_timeout > 0)
578 ieee80211_sta_expire(sdata, ifmsh->mshcfg.plink_timeout * HZ);
578 mesh_path_expire(sdata); 579 mesh_path_expire(sdata);
579 580
580 changed = mesh_accept_plinks_update(sdata); 581 changed = mesh_accept_plinks_update(sdata);
diff --git a/net/mac80211/mesh_plink.c b/net/mac80211/mesh_plink.c
index b488e1859b18..60d737f144e3 100644
--- a/net/mac80211/mesh_plink.c
+++ b/net/mac80211/mesh_plink.c
@@ -17,7 +17,7 @@
17#define PLINK_GET_PLID(p) (p + 4) 17#define PLINK_GET_PLID(p) (p + 4)
18 18
19#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \ 19#define mod_plink_timer(s, t) (mod_timer(&s->plink_timer, \
20 jiffies + HZ * t / 1000)) 20 jiffies + msecs_to_jiffies(t)))
21 21
22enum plink_event { 22enum plink_event {
23 PLINK_UNDEFINED, 23 PLINK_UNDEFINED,
@@ -382,6 +382,7 @@ static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
382 enum ieee80211_band band = ieee80211_get_sdata_band(sdata); 382 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
383 struct ieee80211_supported_band *sband; 383 struct ieee80211_supported_band *sband;
384 u32 rates, basic_rates = 0, changed = 0; 384 u32 rates, basic_rates = 0, changed = 0;
385 enum ieee80211_sta_rx_bandwidth bw = sta->sta.bandwidth;
385 386
386 sband = local->hw.wiphy->bands[band]; 387 sband = local->hw.wiphy->bands[band];
387 rates = ieee80211_sta_get_rates(sdata, elems, band, &basic_rates); 388 rates = ieee80211_sta_get_rates(sdata, elems, band, &basic_rates);
@@ -401,6 +402,9 @@ static void mesh_sta_info_init(struct ieee80211_sub_if_data *sdata,
401 elems->ht_cap_elem, sta)) 402 elems->ht_cap_elem, sta))
402 changed |= IEEE80211_RC_BW_CHANGED; 403 changed |= IEEE80211_RC_BW_CHANGED;
403 404
405 if (bw != sta->sta.bandwidth)
406 changed |= IEEE80211_RC_BW_CHANGED;
407
404 /* HT peer is operating 20MHz-only */ 408 /* HT peer is operating 20MHz-only */
405 if (elems->ht_operation && 409 if (elems->ht_operation &&
406 !(elems->ht_operation->ht_param & 410 !(elems->ht_operation->ht_param &
@@ -621,9 +625,9 @@ static void mesh_plink_timer(unsigned long data)
621 sta->llid, sta->plid, reason); 625 sta->llid, sta->plid, reason);
622} 626}
623 627
624static inline void mesh_plink_timer_set(struct sta_info *sta, int timeout) 628static inline void mesh_plink_timer_set(struct sta_info *sta, u32 timeout)
625{ 629{
626 sta->plink_timer.expires = jiffies + (HZ * timeout / 1000); 630 sta->plink_timer.expires = jiffies + msecs_to_jiffies(timeout);
627 sta->plink_timer.data = (unsigned long) sta; 631 sta->plink_timer.data = (unsigned long) sta;
628 sta->plink_timer.function = mesh_plink_timer; 632 sta->plink_timer.function = mesh_plink_timer;
629 sta->plink_timeout = timeout; 633 sta->plink_timeout = timeout;
diff --git a/net/mac80211/mlme.c b/net/mac80211/mlme.c
index 142f66aece18..26053bf2faa8 100644
--- a/net/mac80211/mlme.c
+++ b/net/mac80211/mlme.c
@@ -1168,11 +1168,7 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1168 if (!conf) { 1168 if (!conf) {
1169 sdata_info(sdata, 1169 sdata_info(sdata,
1170 "no channel context assigned to vif?, disconnecting\n"); 1170 "no channel context assigned to vif?, disconnecting\n");
1171 ieee80211_queue_work(&local->hw, 1171 goto drop_connection;
1172 &ifmgd->csa_connection_drop_work);
1173 mutex_unlock(&local->chanctx_mtx);
1174 mutex_unlock(&local->mtx);
1175 return;
1176 } 1172 }
1177 1173
1178 chanctx = container_of(conf, struct ieee80211_chanctx, conf); 1174 chanctx = container_of(conf, struct ieee80211_chanctx, conf);
@@ -1181,11 +1177,7 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1181 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) { 1177 !(local->hw.flags & IEEE80211_HW_CHANCTX_STA_CSA)) {
1182 sdata_info(sdata, 1178 sdata_info(sdata,
1183 "driver doesn't support chan-switch with channel contexts\n"); 1179 "driver doesn't support chan-switch with channel contexts\n");
1184 ieee80211_queue_work(&local->hw, 1180 goto drop_connection;
1185 &ifmgd->csa_connection_drop_work);
1186 mutex_unlock(&local->chanctx_mtx);
1187 mutex_unlock(&local->mtx);
1188 return;
1189 } 1181 }
1190 1182
1191 ch_switch.timestamp = timestamp; 1183 ch_switch.timestamp = timestamp;
@@ -1197,11 +1189,7 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1197 if (drv_pre_channel_switch(sdata, &ch_switch)) { 1189 if (drv_pre_channel_switch(sdata, &ch_switch)) {
1198 sdata_info(sdata, 1190 sdata_info(sdata,
1199 "preparing for channel switch failed, disconnecting\n"); 1191 "preparing for channel switch failed, disconnecting\n");
1200 ieee80211_queue_work(&local->hw, 1192 goto drop_connection;
1201 &ifmgd->csa_connection_drop_work);
1202 mutex_unlock(&local->chanctx_mtx);
1203 mutex_unlock(&local->mtx);
1204 return;
1205 } 1193 }
1206 1194
1207 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef, 1195 res = ieee80211_vif_reserve_chanctx(sdata, &csa_ie.chandef,
@@ -1210,11 +1198,7 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1210 sdata_info(sdata, 1198 sdata_info(sdata,
1211 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n", 1199 "failed to reserve channel context for channel switch, disconnecting (err=%d)\n",
1212 res); 1200 res);
1213 ieee80211_queue_work(&local->hw, 1201 goto drop_connection;
1214 &ifmgd->csa_connection_drop_work);
1215 mutex_unlock(&local->chanctx_mtx);
1216 mutex_unlock(&local->mtx);
1217 return;
1218 } 1202 }
1219 mutex_unlock(&local->chanctx_mtx); 1203 mutex_unlock(&local->chanctx_mtx);
1220 1204
@@ -1244,6 +1228,11 @@ ieee80211_sta_process_chanswitch(struct ieee80211_sub_if_data *sdata,
1244 mod_timer(&ifmgd->chswitch_timer, 1228 mod_timer(&ifmgd->chswitch_timer,
1245 TU_TO_EXP_TIME((csa_ie.count - 1) * 1229 TU_TO_EXP_TIME((csa_ie.count - 1) *
1246 cbss->beacon_interval)); 1230 cbss->beacon_interval));
1231 return;
1232 drop_connection:
1233 ieee80211_queue_work(&local->hw, &ifmgd->csa_connection_drop_work);
1234 mutex_unlock(&local->chanctx_mtx);
1235 mutex_unlock(&local->mtx);
1247} 1236}
1248 1237
1249static bool 1238static bool
@@ -1359,15 +1348,15 @@ static u32 ieee80211_handle_pwr_constr(struct ieee80211_sub_if_data *sdata,
1359 */ 1348 */
1360 if (has_80211h_pwr && 1349 if (has_80211h_pwr &&
1361 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) { 1350 (!has_cisco_pwr || pwr_level_80211h <= pwr_level_cisco)) {
1362 sdata_info(sdata, 1351 sdata_dbg(sdata,
1363 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n", 1352 "Limiting TX power to %d (%d - %d) dBm as advertised by %pM\n",
1364 pwr_level_80211h, chan_pwr, pwr_reduction_80211h, 1353 pwr_level_80211h, chan_pwr, pwr_reduction_80211h,
1365 sdata->u.mgd.bssid); 1354 sdata->u.mgd.bssid);
1366 new_ap_level = pwr_level_80211h; 1355 new_ap_level = pwr_level_80211h;
1367 } else { /* has_cisco_pwr is always true here. */ 1356 } else { /* has_cisco_pwr is always true here. */
1368 sdata_info(sdata, 1357 sdata_dbg(sdata,
1369 "Limiting TX power to %d dBm as advertised by %pM\n", 1358 "Limiting TX power to %d dBm as advertised by %pM\n",
1370 pwr_level_cisco, sdata->u.mgd.bssid); 1359 pwr_level_cisco, sdata->u.mgd.bssid);
1371 new_ap_level = pwr_level_cisco; 1360 new_ap_level = pwr_level_cisco;
1372 } 1361 }
1373 1362
@@ -1633,9 +1622,6 @@ void ieee80211_dynamic_ps_timer(unsigned long data)
1633{ 1622{
1634 struct ieee80211_local *local = (void *) data; 1623 struct ieee80211_local *local = (void *) data;
1635 1624
1636 if (local->quiescing || local->suspended)
1637 return;
1638
1639 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work); 1625 ieee80211_queue_work(&local->hw, &local->dynamic_ps_enable_work);
1640} 1626}
1641 1627
@@ -2045,7 +2031,7 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
2045 ieee80211_flush_queues(local, sdata, false); 2031 ieee80211_flush_queues(local, sdata, false);
2046 2032
2047 /* clear bssid only after building the needed mgmt frames */ 2033 /* clear bssid only after building the needed mgmt frames */
2048 memset(ifmgd->bssid, 0, ETH_ALEN); 2034 eth_zero_addr(ifmgd->bssid);
2049 2035
2050 /* remove AP and TDLS peers */ 2036 /* remove AP and TDLS peers */
2051 sta_info_flush(sdata); 2037 sta_info_flush(sdata);
@@ -2260,7 +2246,7 @@ static void ieee80211_mgd_probe_ap_send(struct ieee80211_sub_if_data *sdata)
2260 else 2246 else
2261 ssid_len = ssid[1]; 2247 ssid_len = ssid[1];
2262 2248
2263 ieee80211_send_probe_req(sdata, sdata->vif.addr, NULL, 2249 ieee80211_send_probe_req(sdata, sdata->vif.addr, dst,
2264 ssid + 2, ssid_len, NULL, 2250 ssid + 2, ssid_len, NULL,
2265 0, (u32) -1, true, 0, 2251 0, (u32) -1, true, 0,
2266 ifmgd->associated->channel, false); 2252 ifmgd->associated->channel, false);
@@ -2372,6 +2358,24 @@ struct sk_buff *ieee80211_ap_probereq_get(struct ieee80211_hw *hw,
2372} 2358}
2373EXPORT_SYMBOL(ieee80211_ap_probereq_get); 2359EXPORT_SYMBOL(ieee80211_ap_probereq_get);
2374 2360
2361static void ieee80211_report_disconnect(struct ieee80211_sub_if_data *sdata,
2362 const u8 *buf, size_t len, bool tx,
2363 u16 reason)
2364{
2365 struct ieee80211_event event = {
2366 .type = MLME_EVENT,
2367 .u.mlme.data = tx ? DEAUTH_TX_EVENT : DEAUTH_RX_EVENT,
2368 .u.mlme.reason = reason,
2369 };
2370
2371 if (tx)
2372 cfg80211_tx_mlme_mgmt(sdata->dev, buf, len);
2373 else
2374 cfg80211_rx_mlme_mgmt(sdata->dev, buf, len);
2375
2376 drv_event_callback(sdata->local, sdata, &event);
2377}
2378
2375static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata) 2379static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2376{ 2380{
2377 struct ieee80211_local *local = sdata->local; 2381 struct ieee80211_local *local = sdata->local;
@@ -2397,8 +2401,9 @@ static void __ieee80211_disconnect(struct ieee80211_sub_if_data *sdata)
2397 } 2401 }
2398 mutex_unlock(&local->mtx); 2402 mutex_unlock(&local->mtx);
2399 2403
2400 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 2404 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
2401 IEEE80211_DEAUTH_FRAME_LEN); 2405 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
2406
2402 sdata_unlock(sdata); 2407 sdata_unlock(sdata);
2403} 2408}
2404 2409
@@ -2477,7 +2482,7 @@ static void ieee80211_destroy_auth_data(struct ieee80211_sub_if_data *sdata,
2477 del_timer_sync(&sdata->u.mgd.timer); 2482 del_timer_sync(&sdata->u.mgd.timer);
2478 sta_info_destroy_addr(sdata, auth_data->bss->bssid); 2483 sta_info_destroy_addr(sdata, auth_data->bss->bssid);
2479 2484
2480 memset(sdata->u.mgd.bssid, 0, ETH_ALEN); 2485 eth_zero_addr(sdata->u.mgd.bssid);
2481 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID); 2486 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2482 sdata->u.mgd.flags = 0; 2487 sdata->u.mgd.flags = 0;
2483 mutex_lock(&sdata->local->mtx); 2488 mutex_lock(&sdata->local->mtx);
@@ -2522,6 +2527,10 @@ static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2522 u8 bssid[ETH_ALEN]; 2527 u8 bssid[ETH_ALEN];
2523 u16 auth_alg, auth_transaction, status_code; 2528 u16 auth_alg, auth_transaction, status_code;
2524 struct sta_info *sta; 2529 struct sta_info *sta;
2530 struct ieee80211_event event = {
2531 .type = MLME_EVENT,
2532 .u.mlme.data = AUTH_EVENT,
2533 };
2525 2534
2526 sdata_assert_lock(sdata); 2535 sdata_assert_lock(sdata);
2527 2536
@@ -2554,6 +2563,9 @@ static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2554 mgmt->sa, status_code); 2563 mgmt->sa, status_code);
2555 ieee80211_destroy_auth_data(sdata, false); 2564 ieee80211_destroy_auth_data(sdata, false);
2556 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 2565 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len);
2566 event.u.mlme.status = MLME_DENIED;
2567 event.u.mlme.reason = status_code;
2568 drv_event_callback(sdata->local, sdata, &event);
2557 return; 2569 return;
2558 } 2570 }
2559 2571
@@ -2576,6 +2588,8 @@ static void ieee80211_rx_mgmt_auth(struct ieee80211_sub_if_data *sdata,
2576 return; 2588 return;
2577 } 2589 }
2578 2590
2591 event.u.mlme.status = MLME_SUCCESS;
2592 drv_event_callback(sdata->local, sdata, &event);
2579 sdata_info(sdata, "authenticated\n"); 2593 sdata_info(sdata, "authenticated\n");
2580 ifmgd->auth_data->done = true; 2594 ifmgd->auth_data->done = true;
2581 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC; 2595 ifmgd->auth_data->timeout = jiffies + IEEE80211_AUTH_WAIT_ASSOC;
@@ -2694,7 +2708,7 @@ static void ieee80211_rx_mgmt_deauth(struct ieee80211_sub_if_data *sdata,
2694 2708
2695 ieee80211_set_disassoc(sdata, 0, 0, false, NULL); 2709 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2696 2710
2697 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 2711 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
2698} 2712}
2699 2713
2700 2714
@@ -2720,7 +2734,7 @@ static void ieee80211_rx_mgmt_disassoc(struct ieee80211_sub_if_data *sdata,
2720 2734
2721 ieee80211_set_disassoc(sdata, 0, 0, false, NULL); 2735 ieee80211_set_disassoc(sdata, 0, 0, false, NULL);
2722 2736
2723 cfg80211_rx_mlme_mgmt(sdata->dev, (u8 *)mgmt, len); 2737 ieee80211_report_disconnect(sdata, (u8 *)mgmt, len, false, reason_code);
2724} 2738}
2725 2739
2726static void ieee80211_get_rates(struct ieee80211_supported_band *sband, 2740static void ieee80211_get_rates(struct ieee80211_supported_band *sband,
@@ -2790,7 +2804,7 @@ static void ieee80211_destroy_assoc_data(struct ieee80211_sub_if_data *sdata,
2790 del_timer_sync(&sdata->u.mgd.timer); 2804 del_timer_sync(&sdata->u.mgd.timer);
2791 sta_info_destroy_addr(sdata, assoc_data->bss->bssid); 2805 sta_info_destroy_addr(sdata, assoc_data->bss->bssid);
2792 2806
2793 memset(sdata->u.mgd.bssid, 0, ETH_ALEN); 2807 eth_zero_addr(sdata->u.mgd.bssid);
2794 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID); 2808 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
2795 sdata->u.mgd.flags = 0; 2809 sdata->u.mgd.flags = 0;
2796 mutex_lock(&sdata->local->mtx); 2810 mutex_lock(&sdata->local->mtx);
@@ -2982,10 +2996,14 @@ static bool ieee80211_assoc_success(struct ieee80211_sub_if_data *sdata,
2982 2996
2983 rate_control_rate_init(sta); 2997 rate_control_rate_init(sta);
2984 2998
2985 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) 2999 if (ifmgd->flags & IEEE80211_STA_MFP_ENABLED) {
2986 set_sta_flag(sta, WLAN_STA_MFP); 3000 set_sta_flag(sta, WLAN_STA_MFP);
3001 sta->sta.mfp = true;
3002 } else {
3003 sta->sta.mfp = false;
3004 }
2987 3005
2988 sta->sta.wme = elems.wmm_param; 3006 sta->sta.wme = elems.wmm_param && local->hw.queues >= IEEE80211_NUM_ACS;
2989 3007
2990 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC); 3008 err = sta_info_move_state(sta, IEEE80211_STA_ASSOC);
2991 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT)) 3009 if (!err && !(ifmgd->flags & IEEE80211_STA_CONTROL_PORT))
@@ -3055,6 +3073,10 @@ static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3055 u8 *pos; 3073 u8 *pos;
3056 bool reassoc; 3074 bool reassoc;
3057 struct cfg80211_bss *bss; 3075 struct cfg80211_bss *bss;
3076 struct ieee80211_event event = {
3077 .type = MLME_EVENT,
3078 .u.mlme.data = ASSOC_EVENT,
3079 };
3058 3080
3059 sdata_assert_lock(sdata); 3081 sdata_assert_lock(sdata);
3060 3082
@@ -3106,6 +3128,9 @@ static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3106 sdata_info(sdata, "%pM denied association (code=%d)\n", 3128 sdata_info(sdata, "%pM denied association (code=%d)\n",
3107 mgmt->sa, status_code); 3129 mgmt->sa, status_code);
3108 ieee80211_destroy_assoc_data(sdata, false); 3130 ieee80211_destroy_assoc_data(sdata, false);
3131 event.u.mlme.status = MLME_DENIED;
3132 event.u.mlme.reason = status_code;
3133 drv_event_callback(sdata->local, sdata, &event);
3109 } else { 3134 } else {
3110 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) { 3135 if (!ieee80211_assoc_success(sdata, bss, mgmt, len)) {
3111 /* oops -- internal error -- send timeout for now */ 3136 /* oops -- internal error -- send timeout for now */
@@ -3113,6 +3138,8 @@ static void ieee80211_rx_mgmt_assoc_resp(struct ieee80211_sub_if_data *sdata,
3113 cfg80211_assoc_timeout(sdata->dev, bss); 3138 cfg80211_assoc_timeout(sdata->dev, bss);
3114 return; 3139 return;
3115 } 3140 }
3141 event.u.mlme.status = MLME_SUCCESS;
3142 drv_event_callback(sdata->local, sdata, &event);
3116 sdata_info(sdata, "associated\n"); 3143 sdata_info(sdata, "associated\n");
3117 3144
3118 /* 3145 /*
@@ -3315,6 +3342,9 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3315 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) { 3342 ifmgd->count_beacon_signal >= IEEE80211_SIGNAL_AVE_MIN_COUNT) {
3316 int sig = ifmgd->ave_beacon_signal; 3343 int sig = ifmgd->ave_beacon_signal;
3317 int last_sig = ifmgd->last_ave_beacon_signal; 3344 int last_sig = ifmgd->last_ave_beacon_signal;
3345 struct ieee80211_event event = {
3346 .type = RSSI_EVENT,
3347 };
3318 3348
3319 /* 3349 /*
3320 * if signal crosses either of the boundaries, invoke callback 3350 * if signal crosses either of the boundaries, invoke callback
@@ -3323,12 +3353,14 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3323 if (sig > ifmgd->rssi_max_thold && 3353 if (sig > ifmgd->rssi_max_thold &&
3324 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) { 3354 (last_sig <= ifmgd->rssi_min_thold || last_sig == 0)) {
3325 ifmgd->last_ave_beacon_signal = sig; 3355 ifmgd->last_ave_beacon_signal = sig;
3326 drv_rssi_callback(local, sdata, RSSI_EVENT_HIGH); 3356 event.u.rssi.data = RSSI_EVENT_HIGH;
3357 drv_event_callback(local, sdata, &event);
3327 } else if (sig < ifmgd->rssi_min_thold && 3358 } else if (sig < ifmgd->rssi_min_thold &&
3328 (last_sig >= ifmgd->rssi_max_thold || 3359 (last_sig >= ifmgd->rssi_max_thold ||
3329 last_sig == 0)) { 3360 last_sig == 0)) {
3330 ifmgd->last_ave_beacon_signal = sig; 3361 ifmgd->last_ave_beacon_signal = sig;
3331 drv_rssi_callback(local, sdata, RSSI_EVENT_LOW); 3362 event.u.rssi.data = RSSI_EVENT_LOW;
3363 drv_event_callback(local, sdata, &event);
3332 } 3364 }
3333 } 3365 }
3334 3366
@@ -3433,6 +3465,26 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3433 if (ifmgd->csa_waiting_bcn) 3465 if (ifmgd->csa_waiting_bcn)
3434 ieee80211_chswitch_post_beacon(sdata); 3466 ieee80211_chswitch_post_beacon(sdata);
3435 3467
3468 /*
3469 * Update beacon timing and dtim count on every beacon appearance. This
3470 * will allow the driver to use the most updated values. Do it before
3471 * comparing this one with last received beacon.
3472 * IMPORTANT: These parameters would possibly be out of sync by the time
3473 * the driver will use them. The synchronized view is currently
3474 * guaranteed only in certain callbacks.
3475 */
3476 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3477 sdata->vif.bss_conf.sync_tsf =
3478 le64_to_cpu(mgmt->u.beacon.timestamp);
3479 sdata->vif.bss_conf.sync_device_ts =
3480 rx_status->device_timestamp;
3481 if (elems.tim)
3482 sdata->vif.bss_conf.sync_dtim_count =
3483 elems.tim->dtim_count;
3484 else
3485 sdata->vif.bss_conf.sync_dtim_count = 0;
3486 }
3487
3436 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid) 3488 if (ncrc == ifmgd->beacon_crc && ifmgd->beacon_crc_valid)
3437 return; 3489 return;
3438 ifmgd->beacon_crc = ncrc; 3490 ifmgd->beacon_crc = ncrc;
@@ -3460,18 +3512,6 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3460 else 3512 else
3461 bss_conf->dtim_period = 1; 3513 bss_conf->dtim_period = 1;
3462 3514
3463 if (local->hw.flags & IEEE80211_HW_TIMING_BEACON_ONLY) {
3464 sdata->vif.bss_conf.sync_tsf =
3465 le64_to_cpu(mgmt->u.beacon.timestamp);
3466 sdata->vif.bss_conf.sync_device_ts =
3467 rx_status->device_timestamp;
3468 if (elems.tim)
3469 sdata->vif.bss_conf.sync_dtim_count =
3470 elems.tim->dtim_count;
3471 else
3472 sdata->vif.bss_conf.sync_dtim_count = 0;
3473 }
3474
3475 changed |= BSS_CHANGED_BEACON_INFO; 3515 changed |= BSS_CHANGED_BEACON_INFO;
3476 ifmgd->have_beacon = true; 3516 ifmgd->have_beacon = true;
3477 3517
@@ -3502,8 +3542,9 @@ static void ieee80211_rx_mgmt_beacon(struct ieee80211_sub_if_data *sdata,
3502 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 3542 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
3503 WLAN_REASON_DEAUTH_LEAVING, 3543 WLAN_REASON_DEAUTH_LEAVING,
3504 true, deauth_buf); 3544 true, deauth_buf);
3505 cfg80211_tx_mlme_mgmt(sdata->dev, deauth_buf, 3545 ieee80211_report_disconnect(sdata, deauth_buf,
3506 sizeof(deauth_buf)); 3546 sizeof(deauth_buf), true,
3547 WLAN_REASON_DEAUTH_LEAVING);
3507 return; 3548 return;
3508 } 3549 }
3509 3550
@@ -3621,8 +3662,8 @@ static void ieee80211_sta_connection_lost(struct ieee80211_sub_if_data *sdata,
3621 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason, 3662 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, reason,
3622 tx, frame_buf); 3663 tx, frame_buf);
3623 3664
3624 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 3665 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
3625 IEEE80211_DEAUTH_FRAME_LEN); 3666 reason);
3626} 3667}
3627 3668
3628static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata) 3669static int ieee80211_probe_auth(struct ieee80211_sub_if_data *sdata)
@@ -3816,12 +3857,18 @@ void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3816 ieee80211_destroy_auth_data(sdata, false); 3857 ieee80211_destroy_auth_data(sdata, false);
3817 } else if (ieee80211_probe_auth(sdata)) { 3858 } else if (ieee80211_probe_auth(sdata)) {
3818 u8 bssid[ETH_ALEN]; 3859 u8 bssid[ETH_ALEN];
3860 struct ieee80211_event event = {
3861 .type = MLME_EVENT,
3862 .u.mlme.data = AUTH_EVENT,
3863 .u.mlme.status = MLME_TIMEOUT,
3864 };
3819 3865
3820 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN); 3866 memcpy(bssid, ifmgd->auth_data->bss->bssid, ETH_ALEN);
3821 3867
3822 ieee80211_destroy_auth_data(sdata, false); 3868 ieee80211_destroy_auth_data(sdata, false);
3823 3869
3824 cfg80211_auth_timeout(sdata->dev, bssid); 3870 cfg80211_auth_timeout(sdata->dev, bssid);
3871 drv_event_callback(sdata->local, sdata, &event);
3825 } 3872 }
3826 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started) 3873 } else if (ifmgd->auth_data && ifmgd->auth_data->timeout_started)
3827 run_again(sdata, ifmgd->auth_data->timeout); 3874 run_again(sdata, ifmgd->auth_data->timeout);
@@ -3831,9 +3878,15 @@ void ieee80211_sta_work(struct ieee80211_sub_if_data *sdata)
3831 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) || 3878 if ((ifmgd->assoc_data->need_beacon && !ifmgd->have_beacon) ||
3832 ieee80211_do_assoc(sdata)) { 3879 ieee80211_do_assoc(sdata)) {
3833 struct cfg80211_bss *bss = ifmgd->assoc_data->bss; 3880 struct cfg80211_bss *bss = ifmgd->assoc_data->bss;
3881 struct ieee80211_event event = {
3882 .type = MLME_EVENT,
3883 .u.mlme.data = ASSOC_EVENT,
3884 .u.mlme.status = MLME_TIMEOUT,
3885 };
3834 3886
3835 ieee80211_destroy_assoc_data(sdata, false); 3887 ieee80211_destroy_assoc_data(sdata, false);
3836 cfg80211_assoc_timeout(sdata->dev, bss); 3888 cfg80211_assoc_timeout(sdata->dev, bss);
3889 drv_event_callback(sdata->local, sdata, &event);
3837 } 3890 }
3838 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started) 3891 } else if (ifmgd->assoc_data && ifmgd->assoc_data->timeout_started)
3839 run_again(sdata, ifmgd->assoc_data->timeout); 3892 run_again(sdata, ifmgd->assoc_data->timeout);
@@ -3905,12 +3958,8 @@ static void ieee80211_sta_bcn_mon_timer(unsigned long data)
3905{ 3958{
3906 struct ieee80211_sub_if_data *sdata = 3959 struct ieee80211_sub_if_data *sdata =
3907 (struct ieee80211_sub_if_data *) data; 3960 (struct ieee80211_sub_if_data *) data;
3908 struct ieee80211_local *local = sdata->local;
3909 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3961 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3910 3962
3911 if (local->quiescing)
3912 return;
3913
3914 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) 3963 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
3915 return; 3964 return;
3916 3965
@@ -3926,9 +3975,6 @@ static void ieee80211_sta_conn_mon_timer(unsigned long data)
3926 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 3975 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
3927 struct ieee80211_local *local = sdata->local; 3976 struct ieee80211_local *local = sdata->local;
3928 3977
3929 if (local->quiescing)
3930 return;
3931
3932 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn) 3978 if (sdata->vif.csa_active && !ifmgd->csa_waiting_bcn)
3933 return; 3979 return;
3934 3980
@@ -3991,6 +4037,34 @@ void ieee80211_mgd_quiesce(struct ieee80211_sub_if_data *sdata)
3991 IEEE80211_DEAUTH_FRAME_LEN); 4037 IEEE80211_DEAUTH_FRAME_LEN);
3992 } 4038 }
3993 4039
4040 /* This is a bit of a hack - we should find a better and more generic
4041 * solution to this. Normally when suspending, cfg80211 will in fact
4042 * deauthenticate. However, it doesn't (and cannot) stop an ongoing
4043 * auth (not so important) or assoc (this is the problem) process.
4044 *
4045 * As a consequence, it can happen that we are in the process of both
4046 * associating and suspending, and receive an association response
4047 * after cfg80211 has checked if it needs to disconnect, but before
4048 * we actually set the flag to drop incoming frames. This will then
4049 * cause the workqueue flush to process the association response in
4050 * the suspend, resulting in a successful association just before it
4051 * tries to remove the interface from the driver, which now though
4052 * has a channel context assigned ... this results in issues.
4053 *
4054 * To work around this (for now) simply deauth here again if we're
4055 * now connected.
4056 */
4057 if (ifmgd->associated && !sdata->local->wowlan) {
4058 u8 bssid[ETH_ALEN];
4059 struct cfg80211_deauth_request req = {
4060 .reason_code = WLAN_REASON_DEAUTH_LEAVING,
4061 .bssid = bssid,
4062 };
4063
4064 memcpy(bssid, ifmgd->associated->bssid, ETH_ALEN);
4065 ieee80211_mgd_deauth(sdata, &req);
4066 }
4067
3994 sdata_unlock(sdata); 4068 sdata_unlock(sdata);
3995} 4069}
3996 4070
@@ -4379,6 +4453,10 @@ static int ieee80211_prep_connection(struct ieee80211_sub_if_data *sdata,
4379 } else 4453 } else
4380 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid)); 4454 WARN_ON_ONCE(!ether_addr_equal(ifmgd->bssid, cbss->bssid));
4381 4455
4456 /* Cancel scan to ensure that nothing interferes with connection */
4457 if (local->scanning)
4458 ieee80211_scan_cancel(local);
4459
4382 return 0; 4460 return 0;
4383} 4461}
4384 4462
@@ -4467,8 +4545,9 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4467 WLAN_REASON_UNSPECIFIED, 4545 WLAN_REASON_UNSPECIFIED,
4468 false, frame_buf); 4546 false, frame_buf);
4469 4547
4470 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 4548 ieee80211_report_disconnect(sdata, frame_buf,
4471 sizeof(frame_buf)); 4549 sizeof(frame_buf), true,
4550 WLAN_REASON_UNSPECIFIED);
4472 } 4551 }
4473 4552
4474 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid); 4553 sdata_info(sdata, "authenticate with %pM\n", req->bss->bssid);
@@ -4488,7 +4567,7 @@ int ieee80211_mgd_auth(struct ieee80211_sub_if_data *sdata,
4488 return 0; 4567 return 0;
4489 4568
4490 err_clear: 4569 err_clear:
4491 memset(ifmgd->bssid, 0, ETH_ALEN); 4570 eth_zero_addr(ifmgd->bssid);
4492 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID); 4571 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4493 ifmgd->auth_data = NULL; 4572 ifmgd->auth_data = NULL;
4494 err_free: 4573 err_free:
@@ -4568,8 +4647,9 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4568 WLAN_REASON_UNSPECIFIED, 4647 WLAN_REASON_UNSPECIFIED,
4569 false, frame_buf); 4648 false, frame_buf);
4570 4649
4571 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 4650 ieee80211_report_disconnect(sdata, frame_buf,
4572 sizeof(frame_buf)); 4651 sizeof(frame_buf), true,
4652 WLAN_REASON_UNSPECIFIED);
4573 } 4653 }
4574 4654
4575 if (ifmgd->auth_data && !ifmgd->auth_data->done) { 4655 if (ifmgd->auth_data && !ifmgd->auth_data->done) {
@@ -4831,7 +4911,7 @@ int ieee80211_mgd_assoc(struct ieee80211_sub_if_data *sdata,
4831 4911
4832 return 0; 4912 return 0;
4833 err_clear: 4913 err_clear:
4834 memset(ifmgd->bssid, 0, ETH_ALEN); 4914 eth_zero_addr(ifmgd->bssid);
4835 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID); 4915 ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BSSID);
4836 ifmgd->assoc_data = NULL; 4916 ifmgd->assoc_data = NULL;
4837 err_free: 4917 err_free:
@@ -4859,8 +4939,9 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4859 req->reason_code, tx, 4939 req->reason_code, tx,
4860 frame_buf); 4940 frame_buf);
4861 ieee80211_destroy_auth_data(sdata, false); 4941 ieee80211_destroy_auth_data(sdata, false);
4862 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 4942 ieee80211_report_disconnect(sdata, frame_buf,
4863 IEEE80211_DEAUTH_FRAME_LEN); 4943 sizeof(frame_buf), true,
4944 req->reason_code);
4864 4945
4865 return 0; 4946 return 0;
4866 } 4947 }
@@ -4874,8 +4955,9 @@ int ieee80211_mgd_deauth(struct ieee80211_sub_if_data *sdata,
4874 4955
4875 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH, 4956 ieee80211_set_disassoc(sdata, IEEE80211_STYPE_DEAUTH,
4876 req->reason_code, tx, frame_buf); 4957 req->reason_code, tx, frame_buf);
4877 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 4958 ieee80211_report_disconnect(sdata, frame_buf,
4878 IEEE80211_DEAUTH_FRAME_LEN); 4959 sizeof(frame_buf), true,
4960 req->reason_code);
4879 return 0; 4961 return 0;
4880 } 4962 }
4881 4963
@@ -4907,8 +4989,8 @@ int ieee80211_mgd_disassoc(struct ieee80211_sub_if_data *sdata,
4907 req->reason_code, !req->local_state_change, 4989 req->reason_code, !req->local_state_change,
4908 frame_buf); 4990 frame_buf);
4909 4991
4910 cfg80211_tx_mlme_mgmt(sdata->dev, frame_buf, 4992 ieee80211_report_disconnect(sdata, frame_buf, sizeof(frame_buf), true,
4911 IEEE80211_DEAUTH_FRAME_LEN); 4993 req->reason_code);
4912 4994
4913 return 0; 4995 return 0;
4914} 4996}
diff --git a/net/mac80211/pm.c b/net/mac80211/pm.c
index ca405b6b686d..ac6ad6238e3a 100644
--- a/net/mac80211/pm.c
+++ b/net/mac80211/pm.c
@@ -59,9 +59,26 @@ int __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
59 cancel_work_sync(&local->dynamic_ps_enable_work); 59 cancel_work_sync(&local->dynamic_ps_enable_work);
60 del_timer_sync(&local->dynamic_ps_timer); 60 del_timer_sync(&local->dynamic_ps_timer);
61 61
62 local->wowlan = wowlan && local->open_count; 62 local->wowlan = wowlan;
63 if (local->wowlan) { 63 if (local->wowlan) {
64 int err = drv_suspend(local, wowlan); 64 int err;
65
66 /* Drivers don't expect to suspend while some operations like
67 * authenticating or associating are in progress. It doesn't
68 * make sense anyway to accept that, since the authentication
69 * or association would never finish since the driver can't do
70 * that on its own.
71 * Thus, clean up in-progress auth/assoc first.
72 */
73 list_for_each_entry(sdata, &local->interfaces, list) {
74 if (!ieee80211_sdata_running(sdata))
75 continue;
76 if (sdata->vif.type != NL80211_IFTYPE_STATION)
77 continue;
78 ieee80211_mgd_quiesce(sdata);
79 }
80
81 err = drv_suspend(local, wowlan);
65 if (err < 0) { 82 if (err < 0) {
66 local->quiescing = false; 83 local->quiescing = false;
67 local->wowlan = false; 84 local->wowlan = false;
@@ -80,6 +97,13 @@ int __ieee80211_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
80 return err; 97 return err;
81 } else if (err > 0) { 98 } else if (err > 0) {
82 WARN_ON(err != 1); 99 WARN_ON(err != 1);
100 /* cfg80211 will call back into mac80211 to disconnect
101 * all interfaces, allow that to proceed properly
102 */
103 ieee80211_wake_queues_by_reason(hw,
104 IEEE80211_MAX_QUEUE_MAP,
105 IEEE80211_QUEUE_STOP_REASON_SUSPEND,
106 false);
83 return err; 107 return err;
84 } else { 108 } else {
85 goto suspend; 109 goto suspend;
diff --git a/net/mac80211/rc80211_minstrel.c b/net/mac80211/rc80211_minstrel.c
index ef6e8a6c4253..247552a7f6c2 100644
--- a/net/mac80211/rc80211_minstrel.c
+++ b/net/mac80211/rc80211_minstrel.c
@@ -69,14 +69,39 @@ rix_to_ndx(struct minstrel_sta_info *mi, int rix)
69 return i; 69 return i;
70} 70}
71 71
72/* return current EMWA throughput */
73int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma)
74{
75 int usecs;
76
77 usecs = mr->perfect_tx_time;
78 if (!usecs)
79 usecs = 1000000;
80
81 /* reset thr. below 10% success */
82 if (mr->stats.prob_ewma < MINSTREL_FRAC(10, 100))
83 return 0;
84
85 if (prob_ewma > MINSTREL_FRAC(90, 100))
86 return MINSTREL_TRUNC(100000 * (MINSTREL_FRAC(90, 100) / usecs));
87 else
88 return MINSTREL_TRUNC(100000 * (prob_ewma / usecs));
89}
90
72/* find & sort topmost throughput rates */ 91/* find & sort topmost throughput rates */
73static inline void 92static inline void
74minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list) 93minstrel_sort_best_tp_rates(struct minstrel_sta_info *mi, int i, u8 *tp_list)
75{ 94{
76 int j = MAX_THR_RATES; 95 int j = MAX_THR_RATES;
96 struct minstrel_rate_stats *tmp_mrs = &mi->r[j - 1].stats;
97 struct minstrel_rate_stats *cur_mrs = &mi->r[i].stats;
77 98
78 while (j > 0 && mi->r[i].stats.cur_tp > mi->r[tp_list[j - 1]].stats.cur_tp) 99 while (j > 0 && (minstrel_get_tp_avg(&mi->r[i], cur_mrs->prob_ewma) >
100 minstrel_get_tp_avg(&mi->r[tp_list[j - 1]], tmp_mrs->prob_ewma))) {
79 j--; 101 j--;
102 tmp_mrs = &mi->r[tp_list[j - 1]].stats;
103 }
104
80 if (j < MAX_THR_RATES - 1) 105 if (j < MAX_THR_RATES - 1)
81 memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1)); 106 memmove(&tp_list[j + 1], &tp_list[j], MAX_THR_RATES - (j + 1));
82 if (j < MAX_THR_RATES) 107 if (j < MAX_THR_RATES)
@@ -127,13 +152,47 @@ minstrel_update_rates(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
127 rate_control_set_rates(mp->hw, mi->sta, ratetbl); 152 rate_control_set_rates(mp->hw, mi->sta, ratetbl);
128} 153}
129 154
155/*
156* Recalculate statistics and counters of a given rate
157*/
158void
159minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs)
160{
161 if (unlikely(mrs->attempts > 0)) {
162 mrs->sample_skipped = 0;
163 mrs->cur_prob = MINSTREL_FRAC(mrs->success, mrs->attempts);
164 if (unlikely(!mrs->att_hist)) {
165 mrs->prob_ewma = mrs->cur_prob;
166 } else {
167 /* update exponential weighted moving variance */
168 mrs->prob_ewmsd = minstrel_ewmsd(mrs->prob_ewmsd,
169 mrs->cur_prob,
170 mrs->prob_ewma,
171 EWMA_LEVEL);
172
173 /*update exponential weighted moving avarage */
174 mrs->prob_ewma = minstrel_ewma(mrs->prob_ewma,
175 mrs->cur_prob,
176 EWMA_LEVEL);
177 }
178 mrs->att_hist += mrs->attempts;
179 mrs->succ_hist += mrs->success;
180 } else {
181 mrs->sample_skipped++;
182 }
183
184 mrs->last_success = mrs->success;
185 mrs->last_attempts = mrs->attempts;
186 mrs->success = 0;
187 mrs->attempts = 0;
188}
189
130static void 190static void
131minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi) 191minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
132{ 192{
133 u8 tmp_tp_rate[MAX_THR_RATES]; 193 u8 tmp_tp_rate[MAX_THR_RATES];
134 u8 tmp_prob_rate = 0; 194 u8 tmp_prob_rate = 0;
135 u32 usecs; 195 int i, tmp_cur_tp, tmp_prob_tp;
136 int i;
137 196
138 for (i = 0; i < MAX_THR_RATES; i++) 197 for (i = 0; i < MAX_THR_RATES; i++)
139 tmp_tp_rate[i] = 0; 198 tmp_tp_rate[i] = 0;
@@ -141,38 +200,15 @@ minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
141 for (i = 0; i < mi->n_rates; i++) { 200 for (i = 0; i < mi->n_rates; i++) {
142 struct minstrel_rate *mr = &mi->r[i]; 201 struct minstrel_rate *mr = &mi->r[i];
143 struct minstrel_rate_stats *mrs = &mi->r[i].stats; 202 struct minstrel_rate_stats *mrs = &mi->r[i].stats;
203 struct minstrel_rate_stats *tmp_mrs = &mi->r[tmp_prob_rate].stats;
144 204
145 usecs = mr->perfect_tx_time; 205 /* Update statistics of success probability per rate */
146 if (!usecs) 206 minstrel_calc_rate_stats(mrs);
147 usecs = 1000000;
148
149 if (unlikely(mrs->attempts > 0)) {
150 mrs->sample_skipped = 0;
151 mrs->cur_prob = MINSTREL_FRAC(mrs->success,
152 mrs->attempts);
153 mrs->succ_hist += mrs->success;
154 mrs->att_hist += mrs->attempts;
155 mrs->probability = minstrel_ewma(mrs->probability,
156 mrs->cur_prob,
157 EWMA_LEVEL);
158 } else
159 mrs->sample_skipped++;
160
161 mrs->last_success = mrs->success;
162 mrs->last_attempts = mrs->attempts;
163 mrs->success = 0;
164 mrs->attempts = 0;
165
166 /* Update throughput per rate, reset thr. below 10% success */
167 if (mrs->probability < MINSTREL_FRAC(10, 100))
168 mrs->cur_tp = 0;
169 else
170 mrs->cur_tp = mrs->probability * (1000000 / usecs);
171 207
172 /* Sample less often below the 10% chance of success. 208 /* Sample less often below the 10% chance of success.
173 * Sample less often above the 95% chance of success. */ 209 * Sample less often above the 95% chance of success. */
174 if (mrs->probability > MINSTREL_FRAC(95, 100) || 210 if (mrs->prob_ewma > MINSTREL_FRAC(95, 100) ||
175 mrs->probability < MINSTREL_FRAC(10, 100)) { 211 mrs->prob_ewma < MINSTREL_FRAC(10, 100)) {
176 mr->adjusted_retry_count = mrs->retry_count >> 1; 212 mr->adjusted_retry_count = mrs->retry_count >> 1;
177 if (mr->adjusted_retry_count > 2) 213 if (mr->adjusted_retry_count > 2)
178 mr->adjusted_retry_count = 2; 214 mr->adjusted_retry_count = 2;
@@ -192,11 +228,14 @@ minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
192 * choose the maximum throughput rate as max_prob_rate 228 * choose the maximum throughput rate as max_prob_rate
193 * (2) if all success probabilities < 95%, the rate with 229 * (2) if all success probabilities < 95%, the rate with
194 * highest success probability is chosen as max_prob_rate */ 230 * highest success probability is chosen as max_prob_rate */
195 if (mrs->probability >= MINSTREL_FRAC(95, 100)) { 231 if (mrs->prob_ewma >= MINSTREL_FRAC(95, 100)) {
196 if (mrs->cur_tp >= mi->r[tmp_prob_rate].stats.cur_tp) 232 tmp_cur_tp = minstrel_get_tp_avg(mr, mrs->prob_ewma);
233 tmp_prob_tp = minstrel_get_tp_avg(&mi->r[tmp_prob_rate],
234 tmp_mrs->prob_ewma);
235 if (tmp_cur_tp >= tmp_prob_tp)
197 tmp_prob_rate = i; 236 tmp_prob_rate = i;
198 } else { 237 } else {
199 if (mrs->probability >= mi->r[tmp_prob_rate].stats.probability) 238 if (mrs->prob_ewma >= tmp_mrs->prob_ewma)
200 tmp_prob_rate = i; 239 tmp_prob_rate = i;
201 } 240 }
202 } 241 }
@@ -215,7 +254,7 @@ minstrel_update_stats(struct minstrel_priv *mp, struct minstrel_sta_info *mi)
215#endif 254#endif
216 255
217 /* Reset update timer */ 256 /* Reset update timer */
218 mi->stats_update = jiffies; 257 mi->last_stats_update = jiffies;
219 258
220 minstrel_update_rates(mp, mi); 259 minstrel_update_rates(mp, mi);
221} 260}
@@ -253,7 +292,7 @@ minstrel_tx_status(void *priv, struct ieee80211_supported_band *sband,
253 if (mi->sample_deferred > 0) 292 if (mi->sample_deferred > 0)
254 mi->sample_deferred--; 293 mi->sample_deferred--;
255 294
256 if (time_after(jiffies, mi->stats_update + 295 if (time_after(jiffies, mi->last_stats_update +
257 (mp->update_interval * HZ) / 1000)) 296 (mp->update_interval * HZ) / 1000))
258 minstrel_update_stats(mp, mi); 297 minstrel_update_stats(mp, mi);
259} 298}
@@ -385,7 +424,7 @@ minstrel_get_rate(void *priv, struct ieee80211_sta *sta,
385 * has a probability of >95%, we shouldn't be attempting 424 * has a probability of >95%, we shouldn't be attempting
386 * to use it, as this only wastes precious airtime */ 425 * to use it, as this only wastes precious airtime */
387 if (!mrr_capable && 426 if (!mrr_capable &&
388 (mi->r[ndx].stats.probability > MINSTREL_FRAC(95, 100))) 427 (mi->r[ndx].stats.prob_ewma > MINSTREL_FRAC(95, 100)))
389 return; 428 return;
390 429
391 mi->prev_sample = true; 430 mi->prev_sample = true;
@@ -519,7 +558,7 @@ minstrel_rate_init(void *priv, struct ieee80211_supported_band *sband,
519 } 558 }
520 559
521 mi->n_rates = n; 560 mi->n_rates = n;
522 mi->stats_update = jiffies; 561 mi->last_stats_update = jiffies;
523 562
524 init_sample_table(mi); 563 init_sample_table(mi);
525 minstrel_update_rates(mp, mi); 564 minstrel_update_rates(mp, mi);
@@ -553,7 +592,7 @@ minstrel_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
553 if (!mi->sample_table) 592 if (!mi->sample_table)
554 goto error1; 593 goto error1;
555 594
556 mi->stats_update = jiffies; 595 mi->last_stats_update = jiffies;
557 return mi; 596 return mi;
558 597
559error1: 598error1:
@@ -663,12 +702,18 @@ minstrel_free(void *priv)
663static u32 minstrel_get_expected_throughput(void *priv_sta) 702static u32 minstrel_get_expected_throughput(void *priv_sta)
664{ 703{
665 struct minstrel_sta_info *mi = priv_sta; 704 struct minstrel_sta_info *mi = priv_sta;
705 struct minstrel_rate_stats *tmp_mrs;
666 int idx = mi->max_tp_rate[0]; 706 int idx = mi->max_tp_rate[0];
707 int tmp_cur_tp;
667 708
668 /* convert pkt per sec in kbps (1200 is the average pkt size used for 709 /* convert pkt per sec in kbps (1200 is the average pkt size used for
669 * computing cur_tp 710 * computing cur_tp
670 */ 711 */
671 return MINSTREL_TRUNC(mi->r[idx].stats.cur_tp) * 1200 * 8 / 1024; 712 tmp_mrs = &mi->r[idx].stats;
713 tmp_cur_tp = minstrel_get_tp_avg(&mi->r[idx], tmp_mrs->prob_ewma);
714 tmp_cur_tp = tmp_cur_tp * 1200 * 8 / 1024;
715
716 return tmp_cur_tp;
672} 717}
673 718
674const struct rate_control_ops mac80211_minstrel = { 719const struct rate_control_ops mac80211_minstrel = {
diff --git a/net/mac80211/rc80211_minstrel.h b/net/mac80211/rc80211_minstrel.h
index 410efe620c57..c230bbe93262 100644
--- a/net/mac80211/rc80211_minstrel.h
+++ b/net/mac80211/rc80211_minstrel.h
@@ -13,7 +13,6 @@
13#define EWMA_DIV 128 13#define EWMA_DIV 128
14#define SAMPLE_COLUMNS 10 /* number of columns in sample table */ 14#define SAMPLE_COLUMNS 10 /* number of columns in sample table */
15 15
16
17/* scaled fraction values */ 16/* scaled fraction values */
18#define MINSTREL_SCALE 16 17#define MINSTREL_SCALE 16
19#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div) 18#define MINSTREL_FRAC(val, div) (((val) << MINSTREL_SCALE) / div)
@@ -24,11 +23,34 @@
24 23
25/* 24/*
26 * Perform EWMA (Exponentially Weighted Moving Average) calculation 25 * Perform EWMA (Exponentially Weighted Moving Average) calculation
27 */ 26 */
28static inline int 27static inline int
29minstrel_ewma(int old, int new, int weight) 28minstrel_ewma(int old, int new, int weight)
30{ 29{
31 return (new * (EWMA_DIV - weight) + old * weight) / EWMA_DIV; 30 int diff, incr;
31
32 diff = new - old;
33 incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
34
35 return old + incr;
36}
37
38/*
39 * Perform EWMSD (Exponentially Weighted Moving Standard Deviation) calculation
40 */
41static inline int
42minstrel_ewmsd(int old_ewmsd, int cur_prob, int prob_ewma, int weight)
43{
44 int diff, incr, tmp_var;
45
46 /* calculate exponential weighted moving variance */
47 diff = MINSTREL_TRUNC((cur_prob - prob_ewma) * 1000000);
48 incr = (EWMA_DIV - weight) * diff / EWMA_DIV;
49 tmp_var = old_ewmsd * old_ewmsd;
50 tmp_var = weight * (tmp_var + diff * incr / 1000000) / EWMA_DIV;
51
52 /* return standard deviation */
53 return (u16) int_sqrt(tmp_var);
32} 54}
33 55
34struct minstrel_rate_stats { 56struct minstrel_rate_stats {
@@ -39,11 +61,13 @@ struct minstrel_rate_stats {
39 /* total attempts/success counters */ 61 /* total attempts/success counters */
40 u64 att_hist, succ_hist; 62 u64 att_hist, succ_hist;
41 63
42 /* current throughput */ 64 /* statistis of packet delivery probability
43 unsigned int cur_tp; 65 * cur_prob - current prob within last update intervall
44 66 * prob_ewma - exponential weighted moving average of prob
45 /* packet delivery probabilities */ 67 * prob_ewmsd - exp. weighted moving standard deviation of prob */
46 unsigned int cur_prob, probability; 68 unsigned int cur_prob;
69 unsigned int prob_ewma;
70 u16 prob_ewmsd;
47 71
48 /* maximum retry counts */ 72 /* maximum retry counts */
49 u8 retry_count; 73 u8 retry_count;
@@ -71,7 +95,7 @@ struct minstrel_rate {
71struct minstrel_sta_info { 95struct minstrel_sta_info {
72 struct ieee80211_sta *sta; 96 struct ieee80211_sta *sta;
73 97
74 unsigned long stats_update; 98 unsigned long last_stats_update;
75 unsigned int sp_ack_dur; 99 unsigned int sp_ack_dur;
76 unsigned int rate_avg; 100 unsigned int rate_avg;
77 101
@@ -95,6 +119,7 @@ struct minstrel_sta_info {
95 119
96#ifdef CONFIG_MAC80211_DEBUGFS 120#ifdef CONFIG_MAC80211_DEBUGFS
97 struct dentry *dbg_stats; 121 struct dentry *dbg_stats;
122 struct dentry *dbg_stats_csv;
98#endif 123#endif
99}; 124};
100 125
@@ -121,7 +146,6 @@ struct minstrel_priv {
121 u32 fixed_rate_idx; 146 u32 fixed_rate_idx;
122 struct dentry *dbg_fixed_rate; 147 struct dentry *dbg_fixed_rate;
123#endif 148#endif
124
125}; 149};
126 150
127struct minstrel_debugfs_info { 151struct minstrel_debugfs_info {
@@ -133,8 +157,13 @@ extern const struct rate_control_ops mac80211_minstrel;
133void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir); 157void minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
134void minstrel_remove_sta_debugfs(void *priv, void *priv_sta); 158void minstrel_remove_sta_debugfs(void *priv, void *priv_sta);
135 159
160/* Recalculate success probabilities and counters for a given rate using EWMA */
161void minstrel_calc_rate_stats(struct minstrel_rate_stats *mrs);
162int minstrel_get_tp_avg(struct minstrel_rate *mr, int prob_ewma);
163
136/* debugfs */ 164/* debugfs */
137int minstrel_stats_open(struct inode *inode, struct file *file); 165int minstrel_stats_open(struct inode *inode, struct file *file);
166int minstrel_stats_csv_open(struct inode *inode, struct file *file);
138ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos); 167ssize_t minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos);
139int minstrel_stats_release(struct inode *inode, struct file *file); 168int minstrel_stats_release(struct inode *inode, struct file *file);
140 169
diff --git a/net/mac80211/rc80211_minstrel_debugfs.c b/net/mac80211/rc80211_minstrel_debugfs.c
index 2acab1bcaa4b..1db5f7c3318a 100644
--- a/net/mac80211/rc80211_minstrel_debugfs.c
+++ b/net/mac80211/rc80211_minstrel_debugfs.c
@@ -54,12 +54,28 @@
54#include <net/mac80211.h> 54#include <net/mac80211.h>
55#include "rc80211_minstrel.h" 55#include "rc80211_minstrel.h"
56 56
57ssize_t
58minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos)
59{
60 struct minstrel_debugfs_info *ms;
61
62 ms = file->private_data;
63 return simple_read_from_buffer(buf, len, ppos, ms->buf, ms->len);
64}
65
66int
67minstrel_stats_release(struct inode *inode, struct file *file)
68{
69 kfree(file->private_data);
70 return 0;
71}
72
57int 73int
58minstrel_stats_open(struct inode *inode, struct file *file) 74minstrel_stats_open(struct inode *inode, struct file *file)
59{ 75{
60 struct minstrel_sta_info *mi = inode->i_private; 76 struct minstrel_sta_info *mi = inode->i_private;
61 struct minstrel_debugfs_info *ms; 77 struct minstrel_debugfs_info *ms;
62 unsigned int i, tp, prob, eprob; 78 unsigned int i, tp_max, tp_avg, prob, eprob;
63 char *p; 79 char *p;
64 80
65 ms = kmalloc(2048, GFP_KERNEL); 81 ms = kmalloc(2048, GFP_KERNEL);
@@ -68,8 +84,14 @@ minstrel_stats_open(struct inode *inode, struct file *file)
68 84
69 file->private_data = ms; 85 file->private_data = ms;
70 p = ms->buf; 86 p = ms->buf;
71 p += sprintf(p, "rate tpt eprob *prob" 87 p += sprintf(p, "\n");
72 " *ok(*cum) ok( cum)\n"); 88 p += sprintf(p, "best __________rate_________ ______"
89 "statistics______ ________last_______ "
90 "______sum-of________\n");
91 p += sprintf(p, "rate [name idx airtime max_tp] [ ø(tp) ø(prob) "
92 "sd(prob)] [prob.|retry|suc|att] "
93 "[#success | #attempts]\n");
94
73 for (i = 0; i < mi->n_rates; i++) { 95 for (i = 0; i < mi->n_rates; i++) {
74 struct minstrel_rate *mr = &mi->r[i]; 96 struct minstrel_rate *mr = &mi->r[i];
75 struct minstrel_rate_stats *mrs = &mi->r[i].stats; 97 struct minstrel_rate_stats *mrs = &mi->r[i].stats;
@@ -79,18 +101,26 @@ minstrel_stats_open(struct inode *inode, struct file *file)
79 *(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' '; 101 *(p++) = (i == mi->max_tp_rate[2]) ? 'C' : ' ';
80 *(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' '; 102 *(p++) = (i == mi->max_tp_rate[3]) ? 'D' : ' ';
81 *(p++) = (i == mi->max_prob_rate) ? 'P' : ' '; 103 *(p++) = (i == mi->max_prob_rate) ? 'P' : ' ';
82 p += sprintf(p, "%3u%s", mr->bitrate / 2, 104
105 p += sprintf(p, " %3u%s ", mr->bitrate / 2,
83 (mr->bitrate & 1 ? ".5" : " ")); 106 (mr->bitrate & 1 ? ".5" : " "));
107 p += sprintf(p, "%3u ", i);
108 p += sprintf(p, "%6u ", mr->perfect_tx_time);
84 109
85 tp = MINSTREL_TRUNC(mrs->cur_tp / 10); 110 tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
111 tp_avg = minstrel_get_tp_avg(mr, mrs->prob_ewma);
86 prob = MINSTREL_TRUNC(mrs->cur_prob * 1000); 112 prob = MINSTREL_TRUNC(mrs->cur_prob * 1000);
87 eprob = MINSTREL_TRUNC(mrs->probability * 1000); 113 eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
88 114
89 p += sprintf(p, " %4u.%1u %3u.%1u %3u.%1u" 115 p += sprintf(p, "%4u.%1u %4u.%1u %3u.%1u %3u.%1u"
90 " %4u(%4u) %9llu(%9llu)\n", 116 " %3u.%1u %3u %3u %-3u "
91 tp / 10, tp % 10, 117 "%9llu %-9llu\n",
118 tp_max / 10, tp_max % 10,
119 tp_avg / 10, tp_avg % 10,
92 eprob / 10, eprob % 10, 120 eprob / 10, eprob % 10,
121 mrs->prob_ewmsd / 10, mrs->prob_ewmsd % 10,
93 prob / 10, prob % 10, 122 prob / 10, prob % 10,
123 mrs->retry_count,
94 mrs->last_success, 124 mrs->last_success,
95 mrs->last_attempts, 125 mrs->last_attempts,
96 (unsigned long long)mrs->succ_hist, 126 (unsigned long long)mrs->succ_hist,
@@ -107,25 +137,75 @@ minstrel_stats_open(struct inode *inode, struct file *file)
107 return 0; 137 return 0;
108} 138}
109 139
110ssize_t 140static const struct file_operations minstrel_stat_fops = {
111minstrel_stats_read(struct file *file, char __user *buf, size_t len, loff_t *ppos) 141 .owner = THIS_MODULE,
142 .open = minstrel_stats_open,
143 .read = minstrel_stats_read,
144 .release = minstrel_stats_release,
145 .llseek = default_llseek,
146};
147
148int
149minstrel_stats_csv_open(struct inode *inode, struct file *file)
112{ 150{
151 struct minstrel_sta_info *mi = inode->i_private;
113 struct minstrel_debugfs_info *ms; 152 struct minstrel_debugfs_info *ms;
153 unsigned int i, tp_max, tp_avg, prob, eprob;
154 char *p;
114 155
115 ms = file->private_data; 156 ms = kmalloc(2048, GFP_KERNEL);
116 return simple_read_from_buffer(buf, len, ppos, ms->buf, ms->len); 157 if (!ms)
117} 158 return -ENOMEM;
159
160 file->private_data = ms;
161 p = ms->buf;
162
163 for (i = 0; i < mi->n_rates; i++) {
164 struct minstrel_rate *mr = &mi->r[i];
165 struct minstrel_rate_stats *mrs = &mi->r[i].stats;
166
167 p += sprintf(p, "%s" ,((i == mi->max_tp_rate[0]) ? "A" : ""));
168 p += sprintf(p, "%s" ,((i == mi->max_tp_rate[1]) ? "B" : ""));
169 p += sprintf(p, "%s" ,((i == mi->max_tp_rate[2]) ? "C" : ""));
170 p += sprintf(p, "%s" ,((i == mi->max_tp_rate[3]) ? "D" : ""));
171 p += sprintf(p, "%s" ,((i == mi->max_prob_rate) ? "P" : ""));
172
173 p += sprintf(p, ",%u%s", mr->bitrate / 2,
174 (mr->bitrate & 1 ? ".5," : ","));
175 p += sprintf(p, "%u,", i);
176 p += sprintf(p, "%u,",mr->perfect_tx_time);
177
178 tp_max = minstrel_get_tp_avg(mr, MINSTREL_FRAC(100,100));
179 tp_avg = minstrel_get_tp_avg(mr, mrs->prob_ewma);
180 prob = MINSTREL_TRUNC(mrs->cur_prob * 1000);
181 eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
182
183 p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u.%u,%u.%u,%u,%u,%u,"
184 "%llu,%llu,%d,%d\n",
185 tp_max / 10, tp_max % 10,
186 tp_avg / 10, tp_avg % 10,
187 eprob / 10, eprob % 10,
188 mrs->prob_ewmsd / 10, mrs->prob_ewmsd % 10,
189 prob / 10, prob % 10,
190 mrs->retry_count,
191 mrs->last_success,
192 mrs->last_attempts,
193 (unsigned long long)mrs->succ_hist,
194 (unsigned long long)mrs->att_hist,
195 mi->total_packets - mi->sample_packets,
196 mi->sample_packets);
197
198 }
199 ms->len = p - ms->buf;
200
201 WARN_ON(ms->len + sizeof(*ms) > 2048);
118 202
119int
120minstrel_stats_release(struct inode *inode, struct file *file)
121{
122 kfree(file->private_data);
123 return 0; 203 return 0;
124} 204}
125 205
126static const struct file_operations minstrel_stat_fops = { 206static const struct file_operations minstrel_stat_csv_fops = {
127 .owner = THIS_MODULE, 207 .owner = THIS_MODULE,
128 .open = minstrel_stats_open, 208 .open = minstrel_stats_csv_open,
129 .read = minstrel_stats_read, 209 .read = minstrel_stats_read,
130 .release = minstrel_stats_release, 210 .release = minstrel_stats_release,
131 .llseek = default_llseek, 211 .llseek = default_llseek,
@@ -138,6 +218,9 @@ minstrel_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
138 218
139 mi->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, mi, 219 mi->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, mi,
140 &minstrel_stat_fops); 220 &minstrel_stat_fops);
221
222 mi->dbg_stats_csv = debugfs_create_file("rc_stats_csv", S_IRUGO, dir,
223 mi, &minstrel_stat_csv_fops);
141} 224}
142 225
143void 226void
@@ -146,4 +229,6 @@ minstrel_remove_sta_debugfs(void *priv, void *priv_sta)
146 struct minstrel_sta_info *mi = priv_sta; 229 struct minstrel_sta_info *mi = priv_sta;
147 230
148 debugfs_remove(mi->dbg_stats); 231 debugfs_remove(mi->dbg_stats);
232
233 debugfs_remove(mi->dbg_stats_csv);
149} 234}
diff --git a/net/mac80211/rc80211_minstrel_ht.c b/net/mac80211/rc80211_minstrel_ht.c
index 80452cfd2dc5..7430a1df2ab1 100644
--- a/net/mac80211/rc80211_minstrel_ht.c
+++ b/net/mac80211/rc80211_minstrel_ht.c
@@ -17,10 +17,11 @@
17#include "rc80211_minstrel.h" 17#include "rc80211_minstrel.h"
18#include "rc80211_minstrel_ht.h" 18#include "rc80211_minstrel_ht.h"
19 19
20#define AVG_AMPDU_SIZE 16
20#define AVG_PKT_SIZE 1200 21#define AVG_PKT_SIZE 1200
21 22
22/* Number of bits for an average sized packet */ 23/* Number of bits for an average sized packet */
23#define MCS_NBITS (AVG_PKT_SIZE << 3) 24#define MCS_NBITS ((AVG_PKT_SIZE * AVG_AMPDU_SIZE) << 3)
24 25
25/* Number of symbols for a packet with (bps) bits per symbol */ 26/* Number of symbols for a packet with (bps) bits per symbol */
26#define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps)) 27#define MCS_NSYMS(bps) DIV_ROUND_UP(MCS_NBITS, (bps))
@@ -33,7 +34,8 @@
33 ) 34 )
34 35
35/* Transmit duration for the raw data part of an average sized packet */ 36/* Transmit duration for the raw data part of an average sized packet */
36#define MCS_DURATION(streams, sgi, bps) MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) 37#define MCS_DURATION(streams, sgi, bps) \
38 (MCS_SYMBOL_TIME(sgi, MCS_NSYMS((streams) * (bps))) / AVG_AMPDU_SIZE)
37 39
38#define BW_20 0 40#define BW_20 0
39#define BW_40 1 41#define BW_40 1
@@ -311,67 +313,35 @@ minstrel_get_ratestats(struct minstrel_ht_sta *mi, int index)
311 return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES]; 313 return &mi->groups[index / MCS_GROUP_RATES].rates[index % MCS_GROUP_RATES];
312} 314}
313 315
314
315/* 316/*
316 * Recalculate success probabilities and counters for a rate using EWMA 317 * Return current throughput based on the average A-MPDU length, taking into
318 * account the expected number of retransmissions and their expected length
317 */ 319 */
318static void 320int
319minstrel_calc_rate_ewma(struct minstrel_rate_stats *mr) 321minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
322 int prob_ewma)
320{ 323{
321 if (unlikely(mr->attempts > 0)) {
322 mr->sample_skipped = 0;
323 mr->cur_prob = MINSTREL_FRAC(mr->success, mr->attempts);
324 if (!mr->att_hist)
325 mr->probability = mr->cur_prob;
326 else
327 mr->probability = minstrel_ewma(mr->probability,
328 mr->cur_prob, EWMA_LEVEL);
329 mr->att_hist += mr->attempts;
330 mr->succ_hist += mr->success;
331 } else {
332 mr->sample_skipped++;
333 }
334 mr->last_success = mr->success;
335 mr->last_attempts = mr->attempts;
336 mr->success = 0;
337 mr->attempts = 0;
338}
339
340/*
341 * Calculate throughput based on the average A-MPDU length, taking into account
342 * the expected number of retransmissions and their expected length
343 */
344static void
345minstrel_ht_calc_tp(struct minstrel_ht_sta *mi, int group, int rate)
346{
347 struct minstrel_rate_stats *mr;
348 unsigned int nsecs = 0; 324 unsigned int nsecs = 0;
349 unsigned int tp;
350 unsigned int prob;
351 325
352 mr = &mi->groups[group].rates[rate]; 326 /* do not account throughput if sucess prob is below 10% */
353 prob = mr->probability; 327 if (prob_ewma < MINSTREL_FRAC(10, 100))
354 328 return 0;
355 if (prob < MINSTREL_FRAC(1, 10)) {
356 mr->cur_tp = 0;
357 return;
358 }
359
360 /*
361 * For the throughput calculation, limit the probability value to 90% to
362 * account for collision related packet error rate fluctuation
363 */
364 if (prob > MINSTREL_FRAC(9, 10))
365 prob = MINSTREL_FRAC(9, 10);
366 329
367 if (group != MINSTREL_CCK_GROUP) 330 if (group != MINSTREL_CCK_GROUP)
368 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len); 331 nsecs = 1000 * mi->overhead / MINSTREL_TRUNC(mi->avg_ampdu_len);
369 332
370 nsecs += minstrel_mcs_groups[group].duration[rate]; 333 nsecs += minstrel_mcs_groups[group].duration[rate];
371 334
372 /* prob is scaled - see MINSTREL_FRAC above */ 335 /*
373 tp = 1000000 * ((prob * 1000) / nsecs); 336 * For the throughput calculation, limit the probability value to 90% to
374 mr->cur_tp = MINSTREL_TRUNC(tp); 337 * account for collision related packet error rate fluctuation
338 * (prob is scaled - see MINSTREL_FRAC above)
339 */
340 if (prob_ewma > MINSTREL_FRAC(90, 100))
341 return MINSTREL_TRUNC(100000 * ((MINSTREL_FRAC(90, 100) * 1000)
342 / nsecs));
343 else
344 return MINSTREL_TRUNC(100000 * ((prob_ewma * 1000) / nsecs));
375} 345}
376 346
377/* 347/*
@@ -385,22 +355,23 @@ static void
385minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index, 355minstrel_ht_sort_best_tp_rates(struct minstrel_ht_sta *mi, u16 index,
386 u16 *tp_list) 356 u16 *tp_list)
387{ 357{
388 int cur_group, cur_idx, cur_thr, cur_prob; 358 int cur_group, cur_idx, cur_tp_avg, cur_prob;
389 int tmp_group, tmp_idx, tmp_thr, tmp_prob; 359 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
390 int j = MAX_THR_RATES; 360 int j = MAX_THR_RATES;
391 361
392 cur_group = index / MCS_GROUP_RATES; 362 cur_group = index / MCS_GROUP_RATES;
393 cur_idx = index % MCS_GROUP_RATES; 363 cur_idx = index % MCS_GROUP_RATES;
394 cur_thr = mi->groups[cur_group].rates[cur_idx].cur_tp; 364 cur_prob = mi->groups[cur_group].rates[cur_idx].prob_ewma;
395 cur_prob = mi->groups[cur_group].rates[cur_idx].probability; 365 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx, cur_prob);
396 366
397 do { 367 do {
398 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES; 368 tmp_group = tp_list[j - 1] / MCS_GROUP_RATES;
399 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES; 369 tmp_idx = tp_list[j - 1] % MCS_GROUP_RATES;
400 tmp_thr = mi->groups[tmp_group].rates[tmp_idx].cur_tp; 370 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
401 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability; 371 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx,
402 if (cur_thr < tmp_thr || 372 tmp_prob);
403 (cur_thr == tmp_thr && cur_prob <= tmp_prob)) 373 if (cur_tp_avg < tmp_tp_avg ||
374 (cur_tp_avg == tmp_tp_avg && cur_prob <= tmp_prob))
404 break; 375 break;
405 j--; 376 j--;
406 } while (j > 0); 377 } while (j > 0);
@@ -420,16 +391,21 @@ static void
420minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index) 391minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
421{ 392{
422 struct minstrel_mcs_group_data *mg; 393 struct minstrel_mcs_group_data *mg;
423 struct minstrel_rate_stats *mr; 394 struct minstrel_rate_stats *mrs;
424 int tmp_group, tmp_idx, tmp_tp, tmp_prob, max_tp_group; 395 int tmp_group, tmp_idx, tmp_tp_avg, tmp_prob;
396 int max_tp_group, cur_tp_avg, cur_group, cur_idx;
397 int max_gpr_group, max_gpr_idx;
398 int max_gpr_tp_avg, max_gpr_prob;
425 399
400 cur_group = index / MCS_GROUP_RATES;
401 cur_idx = index % MCS_GROUP_RATES;
426 mg = &mi->groups[index / MCS_GROUP_RATES]; 402 mg = &mi->groups[index / MCS_GROUP_RATES];
427 mr = &mg->rates[index % MCS_GROUP_RATES]; 403 mrs = &mg->rates[index % MCS_GROUP_RATES];
428 404
429 tmp_group = mi->max_prob_rate / MCS_GROUP_RATES; 405 tmp_group = mi->max_prob_rate / MCS_GROUP_RATES;
430 tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES; 406 tmp_idx = mi->max_prob_rate % MCS_GROUP_RATES;
431 tmp_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; 407 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
432 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].probability; 408 tmp_tp_avg = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
433 409
434 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from 410 /* if max_tp_rate[0] is from MCS_GROUP max_prob_rate get selected from
435 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */ 411 * MCS_GROUP as well as CCK_GROUP rates do not allow aggregation */
@@ -438,15 +414,24 @@ minstrel_ht_set_best_prob_rate(struct minstrel_ht_sta *mi, u16 index)
438 (max_tp_group != MINSTREL_CCK_GROUP)) 414 (max_tp_group != MINSTREL_CCK_GROUP))
439 return; 415 return;
440 416
441 if (mr->probability > MINSTREL_FRAC(75, 100)) { 417 if (mrs->prob_ewma > MINSTREL_FRAC(75, 100)) {
442 if (mr->cur_tp > tmp_tp) 418 cur_tp_avg = minstrel_ht_get_tp_avg(mi, cur_group, cur_idx,
419 mrs->prob_ewma);
420 if (cur_tp_avg > tmp_tp_avg)
443 mi->max_prob_rate = index; 421 mi->max_prob_rate = index;
444 if (mr->cur_tp > mg->rates[mg->max_group_prob_rate].cur_tp) 422
423 max_gpr_group = mg->max_group_prob_rate / MCS_GROUP_RATES;
424 max_gpr_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
425 max_gpr_prob = mi->groups[max_gpr_group].rates[max_gpr_idx].prob_ewma;
426 max_gpr_tp_avg = minstrel_ht_get_tp_avg(mi, max_gpr_group,
427 max_gpr_idx,
428 max_gpr_prob);
429 if (cur_tp_avg > max_gpr_tp_avg)
445 mg->max_group_prob_rate = index; 430 mg->max_group_prob_rate = index;
446 } else { 431 } else {
447 if (mr->probability > tmp_prob) 432 if (mrs->prob_ewma > tmp_prob)
448 mi->max_prob_rate = index; 433 mi->max_prob_rate = index;
449 if (mr->probability > mg->rates[mg->max_group_prob_rate].probability) 434 if (mrs->prob_ewma > mg->rates[mg->max_group_prob_rate].prob_ewma)
450 mg->max_group_prob_rate = index; 435 mg->max_group_prob_rate = index;
451 } 436 }
452} 437}
@@ -463,16 +448,18 @@ minstrel_ht_assign_best_tp_rates(struct minstrel_ht_sta *mi,
463 u16 tmp_mcs_tp_rate[MAX_THR_RATES], 448 u16 tmp_mcs_tp_rate[MAX_THR_RATES],
464 u16 tmp_cck_tp_rate[MAX_THR_RATES]) 449 u16 tmp_cck_tp_rate[MAX_THR_RATES])
465{ 450{
466 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp; 451 unsigned int tmp_group, tmp_idx, tmp_cck_tp, tmp_mcs_tp, tmp_prob;
467 int i; 452 int i;
468 453
469 tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES; 454 tmp_group = tmp_cck_tp_rate[0] / MCS_GROUP_RATES;
470 tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES; 455 tmp_idx = tmp_cck_tp_rate[0] % MCS_GROUP_RATES;
471 tmp_cck_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; 456 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
457 tmp_cck_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
472 458
473 tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES; 459 tmp_group = tmp_mcs_tp_rate[0] / MCS_GROUP_RATES;
474 tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES; 460 tmp_idx = tmp_mcs_tp_rate[0] % MCS_GROUP_RATES;
475 tmp_mcs_tp = mi->groups[tmp_group].rates[tmp_idx].cur_tp; 461 tmp_prob = mi->groups[tmp_group].rates[tmp_idx].prob_ewma;
462 tmp_mcs_tp = minstrel_ht_get_tp_avg(mi, tmp_group, tmp_idx, tmp_prob);
476 463
477 if (tmp_cck_tp > tmp_mcs_tp) { 464 if (tmp_cck_tp > tmp_mcs_tp) {
478 for(i = 0; i < MAX_THR_RATES; i++) { 465 for(i = 0; i < MAX_THR_RATES; i++) {
@@ -491,8 +478,7 @@ static inline void
491minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi) 478minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
492{ 479{
493 struct minstrel_mcs_group_data *mg; 480 struct minstrel_mcs_group_data *mg;
494 struct minstrel_rate_stats *mr; 481 int tmp_max_streams, group, tmp_idx, tmp_prob;
495 int tmp_max_streams, group;
496 int tmp_tp = 0; 482 int tmp_tp = 0;
497 483
498 tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] / 484 tmp_max_streams = minstrel_mcs_groups[mi->max_tp_rate[0] /
@@ -501,11 +487,16 @@ minstrel_ht_prob_rate_reduce_streams(struct minstrel_ht_sta *mi)
501 mg = &mi->groups[group]; 487 mg = &mi->groups[group];
502 if (!mg->supported || group == MINSTREL_CCK_GROUP) 488 if (!mg->supported || group == MINSTREL_CCK_GROUP)
503 continue; 489 continue;
504 mr = minstrel_get_ratestats(mi, mg->max_group_prob_rate); 490
505 if (tmp_tp < mr->cur_tp && 491 tmp_idx = mg->max_group_prob_rate % MCS_GROUP_RATES;
492 tmp_prob = mi->groups[group].rates[tmp_idx].prob_ewma;
493
494 if (tmp_tp < minstrel_ht_get_tp_avg(mi, group, tmp_idx, tmp_prob) &&
506 (minstrel_mcs_groups[group].streams < tmp_max_streams)) { 495 (minstrel_mcs_groups[group].streams < tmp_max_streams)) {
507 mi->max_prob_rate = mg->max_group_prob_rate; 496 mi->max_prob_rate = mg->max_group_prob_rate;
508 tmp_tp = mr->cur_tp; 497 tmp_tp = minstrel_ht_get_tp_avg(mi, group,
498 tmp_idx,
499 tmp_prob);
509 } 500 }
510 } 501 }
511} 502}
@@ -523,8 +514,8 @@ static void
523minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) 514minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
524{ 515{
525 struct minstrel_mcs_group_data *mg; 516 struct minstrel_mcs_group_data *mg;
526 struct minstrel_rate_stats *mr; 517 struct minstrel_rate_stats *mrs;
527 int group, i, j; 518 int group, i, j, cur_prob;
528 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES]; 519 u16 tmp_mcs_tp_rate[MAX_THR_RATES], tmp_group_tp_rate[MAX_THR_RATES];
529 u16 tmp_cck_tp_rate[MAX_THR_RATES], index; 520 u16 tmp_cck_tp_rate[MAX_THR_RATES], index;
530 521
@@ -563,12 +554,12 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
563 554
564 index = MCS_GROUP_RATES * group + i; 555 index = MCS_GROUP_RATES * group + i;
565 556
566 mr = &mg->rates[i]; 557 mrs = &mg->rates[i];
567 mr->retry_updated = false; 558 mrs->retry_updated = false;
568 minstrel_calc_rate_ewma(mr); 559 minstrel_calc_rate_stats(mrs);
569 minstrel_ht_calc_tp(mi, group, i); 560 cur_prob = mrs->prob_ewma;
570 561
571 if (!mr->cur_tp) 562 if (minstrel_ht_get_tp_avg(mi, group, i, cur_prob) == 0)
572 continue; 563 continue;
573 564
574 /* Find max throughput rate set */ 565 /* Find max throughput rate set */
@@ -612,7 +603,7 @@ minstrel_ht_update_stats(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
612#endif 603#endif
613 604
614 /* Reset update timer */ 605 /* Reset update timer */
615 mi->stats_update = jiffies; 606 mi->last_stats_update = jiffies;
616} 607}
617 608
618static bool 609static bool
@@ -635,7 +626,7 @@ minstrel_ht_txstat_valid(struct minstrel_priv *mp, struct ieee80211_tx_rate *rat
635} 626}
636 627
637static void 628static void
638minstrel_next_sample_idx(struct minstrel_ht_sta *mi) 629minstrel_set_next_sample_idx(struct minstrel_ht_sta *mi)
639{ 630{
640 struct minstrel_mcs_group_data *mg; 631 struct minstrel_mcs_group_data *mg;
641 632
@@ -776,7 +767,8 @@ minstrel_ht_tx_status(void *priv, struct ieee80211_supported_band *sband,
776 update = true; 767 update = true;
777 } 768 }
778 769
779 if (time_after(jiffies, mi->stats_update + (mp->update_interval / 2 * HZ) / 1000)) { 770 if (time_after(jiffies, mi->last_stats_update +
771 (mp->update_interval / 2 * HZ) / 1000)) {
780 update = true; 772 update = true;
781 minstrel_ht_update_stats(mp, mi); 773 minstrel_ht_update_stats(mp, mi);
782 } 774 }
@@ -789,7 +781,7 @@ static void
789minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi, 781minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
790 int index) 782 int index)
791{ 783{
792 struct minstrel_rate_stats *mr; 784 struct minstrel_rate_stats *mrs;
793 const struct mcs_group *group; 785 const struct mcs_group *group;
794 unsigned int tx_time, tx_time_rtscts, tx_time_data; 786 unsigned int tx_time, tx_time_rtscts, tx_time_data;
795 unsigned int cw = mp->cw_min; 787 unsigned int cw = mp->cw_min;
@@ -798,16 +790,16 @@ minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
798 unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len); 790 unsigned int ampdu_len = MINSTREL_TRUNC(mi->avg_ampdu_len);
799 unsigned int overhead = 0, overhead_rtscts = 0; 791 unsigned int overhead = 0, overhead_rtscts = 0;
800 792
801 mr = minstrel_get_ratestats(mi, index); 793 mrs = minstrel_get_ratestats(mi, index);
802 if (mr->probability < MINSTREL_FRAC(1, 10)) { 794 if (mrs->prob_ewma < MINSTREL_FRAC(1, 10)) {
803 mr->retry_count = 1; 795 mrs->retry_count = 1;
804 mr->retry_count_rtscts = 1; 796 mrs->retry_count_rtscts = 1;
805 return; 797 return;
806 } 798 }
807 799
808 mr->retry_count = 2; 800 mrs->retry_count = 2;
809 mr->retry_count_rtscts = 2; 801 mrs->retry_count_rtscts = 2;
810 mr->retry_updated = true; 802 mrs->retry_updated = true;
811 803
812 group = &minstrel_mcs_groups[index / MCS_GROUP_RATES]; 804 group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
813 tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000; 805 tx_time_data = group->duration[index % MCS_GROUP_RATES] * ampdu_len / 1000;
@@ -838,9 +830,9 @@ minstrel_calc_retransmit(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
838 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data; 830 tx_time_rtscts += ctime + overhead_rtscts + tx_time_data;
839 831
840 if (tx_time_rtscts < mp->segment_size) 832 if (tx_time_rtscts < mp->segment_size)
841 mr->retry_count_rtscts++; 833 mrs->retry_count_rtscts++;
842 } while ((tx_time < mp->segment_size) && 834 } while ((tx_time < mp->segment_size) &&
843 (++mr->retry_count < mp->max_retry)); 835 (++mrs->retry_count < mp->max_retry));
844} 836}
845 837
846 838
@@ -849,22 +841,22 @@ minstrel_ht_set_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi,
849 struct ieee80211_sta_rates *ratetbl, int offset, int index) 841 struct ieee80211_sta_rates *ratetbl, int offset, int index)
850{ 842{
851 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES]; 843 const struct mcs_group *group = &minstrel_mcs_groups[index / MCS_GROUP_RATES];
852 struct minstrel_rate_stats *mr; 844 struct minstrel_rate_stats *mrs;
853 u8 idx; 845 u8 idx;
854 u16 flags = group->flags; 846 u16 flags = group->flags;
855 847
856 mr = minstrel_get_ratestats(mi, index); 848 mrs = minstrel_get_ratestats(mi, index);
857 if (!mr->retry_updated) 849 if (!mrs->retry_updated)
858 minstrel_calc_retransmit(mp, mi, index); 850 minstrel_calc_retransmit(mp, mi, index);
859 851
860 if (mr->probability < MINSTREL_FRAC(20, 100) || !mr->retry_count) { 852 if (mrs->prob_ewma < MINSTREL_FRAC(20, 100) || !mrs->retry_count) {
861 ratetbl->rate[offset].count = 2; 853 ratetbl->rate[offset].count = 2;
862 ratetbl->rate[offset].count_rts = 2; 854 ratetbl->rate[offset].count_rts = 2;
863 ratetbl->rate[offset].count_cts = 2; 855 ratetbl->rate[offset].count_cts = 2;
864 } else { 856 } else {
865 ratetbl->rate[offset].count = mr->retry_count; 857 ratetbl->rate[offset].count = mrs->retry_count;
866 ratetbl->rate[offset].count_cts = mr->retry_count; 858 ratetbl->rate[offset].count_cts = mrs->retry_count;
867 ratetbl->rate[offset].count_rts = mr->retry_count_rtscts; 859 ratetbl->rate[offset].count_rts = mrs->retry_count_rtscts;
868 } 860 }
869 861
870 if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP) 862 if (index / MCS_GROUP_RATES == MINSTREL_CCK_GROUP)
@@ -922,7 +914,7 @@ minstrel_get_duration(int index)
922static int 914static int
923minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi) 915minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
924{ 916{
925 struct minstrel_rate_stats *mr; 917 struct minstrel_rate_stats *mrs;
926 struct minstrel_mcs_group_data *mg; 918 struct minstrel_mcs_group_data *mg;
927 unsigned int sample_dur, sample_group, cur_max_tp_streams; 919 unsigned int sample_dur, sample_group, cur_max_tp_streams;
928 int sample_idx = 0; 920 int sample_idx = 0;
@@ -938,12 +930,12 @@ minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
938 sample_group = mi->sample_group; 930 sample_group = mi->sample_group;
939 mg = &mi->groups[sample_group]; 931 mg = &mi->groups[sample_group];
940 sample_idx = sample_table[mg->column][mg->index]; 932 sample_idx = sample_table[mg->column][mg->index];
941 minstrel_next_sample_idx(mi); 933 minstrel_set_next_sample_idx(mi);
942 934
943 if (!(mg->supported & BIT(sample_idx))) 935 if (!(mg->supported & BIT(sample_idx)))
944 return -1; 936 return -1;
945 937
946 mr = &mg->rates[sample_idx]; 938 mrs = &mg->rates[sample_idx];
947 sample_idx += sample_group * MCS_GROUP_RATES; 939 sample_idx += sample_group * MCS_GROUP_RATES;
948 940
949 /* 941 /*
@@ -960,7 +952,7 @@ minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
960 * Do not sample if the probability is already higher than 95% 952 * Do not sample if the probability is already higher than 95%
961 * to avoid wasting airtime. 953 * to avoid wasting airtime.
962 */ 954 */
963 if (mr->probability > MINSTREL_FRAC(95, 100)) 955 if (mrs->prob_ewma > MINSTREL_FRAC(95, 100))
964 return -1; 956 return -1;
965 957
966 /* 958 /*
@@ -975,7 +967,7 @@ minstrel_get_sample_rate(struct minstrel_priv *mp, struct minstrel_ht_sta *mi)
975 (cur_max_tp_streams - 1 < 967 (cur_max_tp_streams - 1 <
976 minstrel_mcs_groups[sample_group].streams || 968 minstrel_mcs_groups[sample_group].streams ||
977 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) { 969 sample_dur >= minstrel_get_duration(mi->max_prob_rate))) {
978 if (mr->sample_skipped < 20) 970 if (mrs->sample_skipped < 20)
979 return -1; 971 return -1;
980 972
981 if (mi->sample_slow++ > 2) 973 if (mi->sample_slow++ > 2)
@@ -1129,7 +1121,7 @@ minstrel_ht_update_caps(void *priv, struct ieee80211_supported_band *sband,
1129 memset(mi, 0, sizeof(*mi)); 1121 memset(mi, 0, sizeof(*mi));
1130 1122
1131 mi->sta = sta; 1123 mi->sta = sta;
1132 mi->stats_update = jiffies; 1124 mi->last_stats_update = jiffies;
1133 1125
1134 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0); 1126 ack_dur = ieee80211_frame_duration(sband->band, 10, 60, 1, 1, 0);
1135 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0); 1127 mi->overhead = ieee80211_frame_duration(sband->band, 0, 60, 1, 1, 0);
@@ -1326,16 +1318,19 @@ static u32 minstrel_ht_get_expected_throughput(void *priv_sta)
1326{ 1318{
1327 struct minstrel_ht_sta_priv *msp = priv_sta; 1319 struct minstrel_ht_sta_priv *msp = priv_sta;
1328 struct minstrel_ht_sta *mi = &msp->ht; 1320 struct minstrel_ht_sta *mi = &msp->ht;
1329 int i, j; 1321 int i, j, prob, tp_avg;
1330 1322
1331 if (!msp->is_ht) 1323 if (!msp->is_ht)
1332 return mac80211_minstrel.get_expected_throughput(priv_sta); 1324 return mac80211_minstrel.get_expected_throughput(priv_sta);
1333 1325
1334 i = mi->max_tp_rate[0] / MCS_GROUP_RATES; 1326 i = mi->max_tp_rate[0] / MCS_GROUP_RATES;
1335 j = mi->max_tp_rate[0] % MCS_GROUP_RATES; 1327 j = mi->max_tp_rate[0] % MCS_GROUP_RATES;
1328 prob = mi->groups[i].rates[j].prob_ewma;
1329
1330 /* convert tp_avg from pkt per second in kbps */
1331 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, prob) * AVG_PKT_SIZE * 8 / 1024;
1336 1332
1337 /* convert cur_tp from pkt per second in kbps */ 1333 return tp_avg;
1338 return mi->groups[i].rates[j].cur_tp * AVG_PKT_SIZE * 8 / 1024;
1339} 1334}
1340 1335
1341static const struct rate_control_ops mac80211_minstrel_ht = { 1336static const struct rate_control_ops mac80211_minstrel_ht = {
diff --git a/net/mac80211/rc80211_minstrel_ht.h b/net/mac80211/rc80211_minstrel_ht.h
index f2217d6aa0c2..e8b52a94d24b 100644
--- a/net/mac80211/rc80211_minstrel_ht.h
+++ b/net/mac80211/rc80211_minstrel_ht.h
@@ -78,7 +78,7 @@ struct minstrel_ht_sta {
78 u16 max_prob_rate; 78 u16 max_prob_rate;
79 79
80 /* time of last status update */ 80 /* time of last status update */
81 unsigned long stats_update; 81 unsigned long last_stats_update;
82 82
83 /* overhead time in usec for each frame */ 83 /* overhead time in usec for each frame */
84 unsigned int overhead; 84 unsigned int overhead;
@@ -112,6 +112,7 @@ struct minstrel_ht_sta_priv {
112 }; 112 };
113#ifdef CONFIG_MAC80211_DEBUGFS 113#ifdef CONFIG_MAC80211_DEBUGFS
114 struct dentry *dbg_stats; 114 struct dentry *dbg_stats;
115 struct dentry *dbg_stats_csv;
115#endif 116#endif
116 void *ratelist; 117 void *ratelist;
117 void *sample_table; 118 void *sample_table;
@@ -120,5 +121,7 @@ struct minstrel_ht_sta_priv {
120 121
121void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir); 122void minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir);
122void minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta); 123void minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta);
124int minstrel_ht_get_tp_avg(struct minstrel_ht_sta *mi, int group, int rate,
125 int prob_ewma);
123 126
124#endif 127#endif
diff --git a/net/mac80211/rc80211_minstrel_ht_debugfs.c b/net/mac80211/rc80211_minstrel_ht_debugfs.c
index 20c676b8e5b6..6822ce0f95e5 100644
--- a/net/mac80211/rc80211_minstrel_ht_debugfs.c
+++ b/net/mac80211/rc80211_minstrel_ht_debugfs.c
@@ -19,7 +19,7 @@ static char *
19minstrel_ht_stats_dump(struct minstrel_ht_sta *mi, int i, char *p) 19minstrel_ht_stats_dump(struct minstrel_ht_sta *mi, int i, char *p)
20{ 20{
21 const struct mcs_group *mg; 21 const struct mcs_group *mg;
22 unsigned int j, tp, prob, eprob; 22 unsigned int j, tp_max, tp_avg, prob, eprob, tx_time;
23 char htmode = '2'; 23 char htmode = '2';
24 char gimode = 'L'; 24 char gimode = 'L';
25 u32 gflags; 25 u32 gflags;
@@ -38,19 +38,26 @@ minstrel_ht_stats_dump(struct minstrel_ht_sta *mi, int i, char *p)
38 gimode = 'S'; 38 gimode = 'S';
39 39
40 for (j = 0; j < MCS_GROUP_RATES; j++) { 40 for (j = 0; j < MCS_GROUP_RATES; j++) {
41 struct minstrel_rate_stats *mr = &mi->groups[i].rates[j]; 41 struct minstrel_rate_stats *mrs = &mi->groups[i].rates[j];
42 static const int bitrates[4] = { 10, 20, 55, 110 }; 42 static const int bitrates[4] = { 10, 20, 55, 110 };
43 int idx = i * MCS_GROUP_RATES + j; 43 int idx = i * MCS_GROUP_RATES + j;
44 44
45 if (!(mi->groups[i].supported & BIT(j))) 45 if (!(mi->groups[i].supported & BIT(j)))
46 continue; 46 continue;
47 47
48 if (gflags & IEEE80211_TX_RC_MCS) 48 if (gflags & IEEE80211_TX_RC_MCS) {
49 p += sprintf(p, " HT%c0/%cGI ", htmode, gimode); 49 p += sprintf(p, "HT%c0 ", htmode);
50 else if (gflags & IEEE80211_TX_RC_VHT_MCS) 50 p += sprintf(p, "%cGI ", gimode);
51 p += sprintf(p, "VHT%c0/%cGI ", htmode, gimode); 51 p += sprintf(p, "%d ", mg->streams);
52 else 52 } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
53 p += sprintf(p, " CCK/%cP ", j < 4 ? 'L' : 'S'); 53 p += sprintf(p, "VHT%c0 ", htmode);
54 p += sprintf(p, "%cGI ", gimode);
55 p += sprintf(p, "%d ", mg->streams);
56 } else {
57 p += sprintf(p, "CCK ");
58 p += sprintf(p, "%cP ", j < 4 ? 'L' : 'S');
59 p += sprintf(p, "1 ");
60 }
54 61
55 *(p++) = (idx == mi->max_tp_rate[0]) ? 'A' : ' '; 62 *(p++) = (idx == mi->max_tp_rate[0]) ? 'A' : ' ';
56 *(p++) = (idx == mi->max_tp_rate[1]) ? 'B' : ' '; 63 *(p++) = (idx == mi->max_tp_rate[1]) ? 'B' : ' ';
@@ -59,29 +66,39 @@ minstrel_ht_stats_dump(struct minstrel_ht_sta *mi, int i, char *p)
59 *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' '; 66 *(p++) = (idx == mi->max_prob_rate) ? 'P' : ' ';
60 67
61 if (gflags & IEEE80211_TX_RC_MCS) { 68 if (gflags & IEEE80211_TX_RC_MCS) {
62 p += sprintf(p, " MCS%-2u ", (mg->streams - 1) * 8 + j); 69 p += sprintf(p, " MCS%-2u", (mg->streams - 1) * 8 + j);
63 } else if (gflags & IEEE80211_TX_RC_VHT_MCS) { 70 } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
64 p += sprintf(p, " MCS%-1u/%1u", j, mg->streams); 71 p += sprintf(p, " MCS%-1u/%1u", j, mg->streams);
65 } else { 72 } else {
66 int r = bitrates[j % 4]; 73 int r = bitrates[j % 4];
67 74
68 p += sprintf(p, " %2u.%1uM ", r / 10, r % 10); 75 p += sprintf(p, " %2u.%1uM", r / 10, r % 10);
69 } 76 }
70 77
71 tp = mr->cur_tp / 10; 78 p += sprintf(p, " %3u ", idx);
72 prob = MINSTREL_TRUNC(mr->cur_prob * 1000);
73 eprob = MINSTREL_TRUNC(mr->probability * 1000);
74 79
75 p += sprintf(p, " %4u.%1u %3u.%1u %3u.%1u " 80 /* tx_time[rate(i)] in usec */
76 "%3u %4u(%4u) %9llu(%9llu)\n", 81 tx_time = DIV_ROUND_CLOSEST(mg->duration[j], 1000);
77 tp / 10, tp % 10, 82 p += sprintf(p, "%6u ", tx_time);
83
84 tp_max = minstrel_ht_get_tp_avg(mi, i, j, MINSTREL_FRAC(100, 100));
85 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, mrs->prob_ewma);
86 prob = MINSTREL_TRUNC(mrs->cur_prob * 1000);
87 eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
88
89 p += sprintf(p, "%4u.%1u %4u.%1u %3u.%1u %3u.%1u"
90 " %3u.%1u %3u %3u %-3u "
91 "%9llu %-9llu\n",
92 tp_max / 10, tp_max % 10,
93 tp_avg / 10, tp_avg % 10,
78 eprob / 10, eprob % 10, 94 eprob / 10, eprob % 10,
95 mrs->prob_ewmsd / 10, mrs->prob_ewmsd % 10,
79 prob / 10, prob % 10, 96 prob / 10, prob % 10,
80 mr->retry_count, 97 mrs->retry_count,
81 mr->last_success, 98 mrs->last_success,
82 mr->last_attempts, 99 mrs->last_attempts,
83 (unsigned long long)mr->succ_hist, 100 (unsigned long long)mrs->succ_hist,
84 (unsigned long long)mr->att_hist); 101 (unsigned long long)mrs->att_hist);
85 } 102 }
86 103
87 return p; 104 return p;
@@ -94,8 +111,8 @@ minstrel_ht_stats_open(struct inode *inode, struct file *file)
94 struct minstrel_ht_sta *mi = &msp->ht; 111 struct minstrel_ht_sta *mi = &msp->ht;
95 struct minstrel_debugfs_info *ms; 112 struct minstrel_debugfs_info *ms;
96 unsigned int i; 113 unsigned int i;
97 char *p;
98 int ret; 114 int ret;
115 char *p;
99 116
100 if (!msp->is_ht) { 117 if (!msp->is_ht) {
101 inode->i_private = &msp->legacy; 118 inode->i_private = &msp->legacy;
@@ -110,8 +127,14 @@ minstrel_ht_stats_open(struct inode *inode, struct file *file)
110 127
111 file->private_data = ms; 128 file->private_data = ms;
112 p = ms->buf; 129 p = ms->buf;
113 p += sprintf(p, " type rate tpt eprob *prob " 130
114 "ret *ok(*cum) ok( cum)\n"); 131 p += sprintf(p, "\n");
132 p += sprintf(p, " best ____________rate__________ "
133 "______statistics______ ________last_______ "
134 "______sum-of________\n");
135 p += sprintf(p, "mode guard # rate [name idx airtime max_tp] "
136 "[ ø(tp) ø(prob) sd(prob)] [prob.|retry|suc|att] [#success | "
137 "#attempts]\n");
115 138
116 p = minstrel_ht_stats_dump(mi, MINSTREL_CCK_GROUP, p); 139 p = minstrel_ht_stats_dump(mi, MINSTREL_CCK_GROUP, p);
117 for (i = 0; i < MINSTREL_CCK_GROUP; i++) 140 for (i = 0; i < MINSTREL_CCK_GROUP; i++)
@@ -123,11 +146,10 @@ minstrel_ht_stats_open(struct inode *inode, struct file *file)
123 "lookaround %d\n", 146 "lookaround %d\n",
124 max(0, (int) mi->total_packets - (int) mi->sample_packets), 147 max(0, (int) mi->total_packets - (int) mi->sample_packets),
125 mi->sample_packets); 148 mi->sample_packets);
126 p += sprintf(p, "Average A-MPDU length: %d.%d\n", 149 p += sprintf(p, "Average # of aggregated frames per A-MPDU: %d.%d\n",
127 MINSTREL_TRUNC(mi->avg_ampdu_len), 150 MINSTREL_TRUNC(mi->avg_ampdu_len),
128 MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10); 151 MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
129 ms->len = p - ms->buf; 152 ms->len = p - ms->buf;
130
131 WARN_ON(ms->len + sizeof(*ms) > 32768); 153 WARN_ON(ms->len + sizeof(*ms) > 32768);
132 154
133 return nonseekable_open(inode, file); 155 return nonseekable_open(inode, file);
@@ -141,6 +163,143 @@ static const struct file_operations minstrel_ht_stat_fops = {
141 .llseek = no_llseek, 163 .llseek = no_llseek,
142}; 164};
143 165
166static char *
167minstrel_ht_stats_csv_dump(struct minstrel_ht_sta *mi, int i, char *p)
168{
169 const struct mcs_group *mg;
170 unsigned int j, tp_max, tp_avg, prob, eprob, tx_time;
171 char htmode = '2';
172 char gimode = 'L';
173 u32 gflags;
174
175 if (!mi->groups[i].supported)
176 return p;
177
178 mg = &minstrel_mcs_groups[i];
179 gflags = mg->flags;
180
181 if (gflags & IEEE80211_TX_RC_40_MHZ_WIDTH)
182 htmode = '4';
183 else if (gflags & IEEE80211_TX_RC_80_MHZ_WIDTH)
184 htmode = '8';
185 if (gflags & IEEE80211_TX_RC_SHORT_GI)
186 gimode = 'S';
187
188 for (j = 0; j < MCS_GROUP_RATES; j++) {
189 struct minstrel_rate_stats *mrs = &mi->groups[i].rates[j];
190 static const int bitrates[4] = { 10, 20, 55, 110 };
191 int idx = i * MCS_GROUP_RATES + j;
192
193 if (!(mi->groups[i].supported & BIT(j)))
194 continue;
195
196 if (gflags & IEEE80211_TX_RC_MCS) {
197 p += sprintf(p, "HT%c0,", htmode);
198 p += sprintf(p, "%cGI,", gimode);
199 p += sprintf(p, "%d,", mg->streams);
200 } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
201 p += sprintf(p, "VHT%c0,", htmode);
202 p += sprintf(p, "%cGI,", gimode);
203 p += sprintf(p, "%d,", mg->streams);
204 } else {
205 p += sprintf(p, "CCK,");
206 p += sprintf(p, "%cP,", j < 4 ? 'L' : 'S');
207 p += sprintf(p, "1,");
208 }
209
210 p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[0]) ? "A" : ""));
211 p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[1]) ? "B" : ""));
212 p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[2]) ? "C" : ""));
213 p += sprintf(p, "%s" ,((idx == mi->max_tp_rate[3]) ? "D" : ""));
214 p += sprintf(p, "%s" ,((idx == mi->max_prob_rate) ? "P" : ""));
215
216 if (gflags & IEEE80211_TX_RC_MCS) {
217 p += sprintf(p, ",MCS%-2u,", (mg->streams - 1) * 8 + j);
218 } else if (gflags & IEEE80211_TX_RC_VHT_MCS) {
219 p += sprintf(p, ",MCS%-1u/%1u,", j, mg->streams);
220 } else {
221 int r = bitrates[j % 4];
222 p += sprintf(p, ",%2u.%1uM,", r / 10, r % 10);
223 }
224
225 p += sprintf(p, "%u,", idx);
226 tx_time = DIV_ROUND_CLOSEST(mg->duration[j], 1000);
227 p += sprintf(p, "%u,", tx_time);
228
229 tp_max = minstrel_ht_get_tp_avg(mi, i, j, MINSTREL_FRAC(100, 100));
230 tp_avg = minstrel_ht_get_tp_avg(mi, i, j, mrs->prob_ewma);
231 prob = MINSTREL_TRUNC(mrs->cur_prob * 1000);
232 eprob = MINSTREL_TRUNC(mrs->prob_ewma * 1000);
233
234 p += sprintf(p, "%u.%u,%u.%u,%u.%u,%u.%u,%u.%u,%u,%u,"
235 "%u,%llu,%llu,",
236 tp_max / 10, tp_max % 10,
237 tp_avg / 10, tp_avg % 10,
238 eprob / 10, eprob % 10,
239 mrs->prob_ewmsd / 10, mrs->prob_ewmsd % 10,
240 prob / 10, prob % 10,
241 mrs->retry_count,
242 mrs->last_success,
243 mrs->last_attempts,
244 (unsigned long long)mrs->succ_hist,
245 (unsigned long long)mrs->att_hist);
246 p += sprintf(p, "%d,%d,%d.%d\n",
247 max(0, (int) mi->total_packets -
248 (int) mi->sample_packets),
249 mi->sample_packets,
250 MINSTREL_TRUNC(mi->avg_ampdu_len),
251 MINSTREL_TRUNC(mi->avg_ampdu_len * 10) % 10);
252 }
253
254 return p;
255}
256
257static int
258minstrel_ht_stats_csv_open(struct inode *inode, struct file *file)
259{
260 struct minstrel_ht_sta_priv *msp = inode->i_private;
261 struct minstrel_ht_sta *mi = &msp->ht;
262 struct minstrel_debugfs_info *ms;
263 unsigned int i;
264 int ret;
265 char *p;
266
267 if (!msp->is_ht) {
268 inode->i_private = &msp->legacy;
269 ret = minstrel_stats_csv_open(inode, file);
270 inode->i_private = msp;
271 return ret;
272 }
273
274 ms = kmalloc(32768, GFP_KERNEL);
275
276 if (!ms)
277 return -ENOMEM;
278
279 file->private_data = ms;
280
281 p = ms->buf;
282
283 p = minstrel_ht_stats_csv_dump(mi, MINSTREL_CCK_GROUP, p);
284 for (i = 0; i < MINSTREL_CCK_GROUP; i++)
285 p = minstrel_ht_stats_csv_dump(mi, i, p);
286 for (i++; i < ARRAY_SIZE(mi->groups); i++)
287 p = minstrel_ht_stats_csv_dump(mi, i, p);
288
289 ms->len = p - ms->buf;
290 WARN_ON(ms->len + sizeof(*ms) > 32768);
291
292 return nonseekable_open(inode, file);
293}
294
295static const struct file_operations minstrel_ht_stat_csv_fops = {
296 .owner = THIS_MODULE,
297 .open = minstrel_ht_stats_csv_open,
298 .read = minstrel_stats_read,
299 .release = minstrel_stats_release,
300 .llseek = no_llseek,
301};
302
144void 303void
145minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir) 304minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
146{ 305{
@@ -148,6 +307,8 @@ minstrel_ht_add_sta_debugfs(void *priv, void *priv_sta, struct dentry *dir)
148 307
149 msp->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, msp, 308 msp->dbg_stats = debugfs_create_file("rc_stats", S_IRUGO, dir, msp,
150 &minstrel_ht_stat_fops); 309 &minstrel_ht_stat_fops);
310 msp->dbg_stats_csv = debugfs_create_file("rc_stats_csv", S_IRUGO,
311 dir, msp, &minstrel_ht_stat_csv_fops);
151} 312}
152 313
153void 314void
@@ -156,4 +317,5 @@ minstrel_ht_remove_sta_debugfs(void *priv, void *priv_sta)
156 struct minstrel_ht_sta_priv *msp = priv_sta; 317 struct minstrel_ht_sta_priv *msp = priv_sta;
157 318
158 debugfs_remove(msp->dbg_stats); 319 debugfs_remove(msp->dbg_stats);
320 debugfs_remove(msp->dbg_stats_csv);
159} 321}
diff --git a/net/mac80211/rx.c b/net/mac80211/rx.c
index 1eb730bf8752..260eed45b6d2 100644
--- a/net/mac80211/rx.c
+++ b/net/mac80211/rx.c
@@ -1185,6 +1185,7 @@ static void sta_ps_start(struct sta_info *sta)
1185 struct ieee80211_sub_if_data *sdata = sta->sdata; 1185 struct ieee80211_sub_if_data *sdata = sta->sdata;
1186 struct ieee80211_local *local = sdata->local; 1186 struct ieee80211_local *local = sdata->local;
1187 struct ps_data *ps; 1187 struct ps_data *ps;
1188 int tid;
1188 1189
1189 if (sta->sdata->vif.type == NL80211_IFTYPE_AP || 1190 if (sta->sdata->vif.type == NL80211_IFTYPE_AP ||
1190 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN) 1191 sta->sdata->vif.type == NL80211_IFTYPE_AP_VLAN)
@@ -1198,6 +1199,18 @@ static void sta_ps_start(struct sta_info *sta)
1198 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta); 1199 drv_sta_notify(local, sdata, STA_NOTIFY_SLEEP, &sta->sta);
1199 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n", 1200 ps_dbg(sdata, "STA %pM aid %d enters power save mode\n",
1200 sta->sta.addr, sta->sta.aid); 1201 sta->sta.addr, sta->sta.aid);
1202
1203 if (!sta->sta.txq[0])
1204 return;
1205
1206 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1207 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1208
1209 if (!skb_queue_len(&txqi->queue))
1210 set_bit(tid, &sta->txq_buffered_tids);
1211 else
1212 clear_bit(tid, &sta->txq_buffered_tids);
1213 }
1201} 1214}
1202 1215
1203static void sta_ps_end(struct sta_info *sta) 1216static void sta_ps_end(struct sta_info *sta)
@@ -1913,8 +1926,7 @@ static int ieee80211_drop_unencrypted(struct ieee80211_rx_data *rx, __le16 fc)
1913 /* Drop unencrypted frames if key is set. */ 1926 /* Drop unencrypted frames if key is set. */
1914 if (unlikely(!ieee80211_has_protected(fc) && 1927 if (unlikely(!ieee80211_has_protected(fc) &&
1915 !ieee80211_is_nullfunc(fc) && 1928 !ieee80211_is_nullfunc(fc) &&
1916 ieee80211_is_data(fc) && 1929 ieee80211_is_data(fc) && rx->key))
1917 (rx->key || rx->sdata->drop_unencrypted)))
1918 return -EACCES; 1930 return -EACCES;
1919 1931
1920 return 0; 1932 return 0;
@@ -2044,6 +2056,9 @@ ieee80211_deliver_skb(struct ieee80211_rx_data *rx)
2044 struct sta_info *dsta; 2056 struct sta_info *dsta;
2045 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb); 2057 struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
2046 2058
2059 dev->stats.rx_packets++;
2060 dev->stats.rx_bytes += rx->skb->len;
2061
2047 skb = rx->skb; 2062 skb = rx->skb;
2048 xmit_skb = NULL; 2063 xmit_skb = NULL;
2049 2064
@@ -2174,8 +2189,6 @@ ieee80211_rx_h_amsdu(struct ieee80211_rx_data *rx)
2174 dev_kfree_skb(rx->skb); 2189 dev_kfree_skb(rx->skb);
2175 continue; 2190 continue;
2176 } 2191 }
2177 dev->stats.rx_packets++;
2178 dev->stats.rx_bytes += rx->skb->len;
2179 2192
2180 ieee80211_deliver_skb(rx); 2193 ieee80211_deliver_skb(rx);
2181 } 2194 }
@@ -2401,9 +2414,6 @@ ieee80211_rx_h_data(struct ieee80211_rx_data *rx)
2401 2414
2402 rx->skb->dev = dev; 2415 rx->skb->dev = dev;
2403 2416
2404 dev->stats.rx_packets++;
2405 dev->stats.rx_bytes += rx->skb->len;
2406
2407 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 && 2417 if (local->ps_sdata && local->hw.conf.dynamic_ps_timeout > 0 &&
2408 !is_multicast_ether_addr( 2418 !is_multicast_ether_addr(
2409 ((struct ethhdr *)rx->skb->data)->h_dest) && 2419 ((struct ethhdr *)rx->skb->data)->h_dest) &&
@@ -3129,6 +3139,12 @@ static void ieee80211_rx_handlers(struct ieee80211_rx_data *rx,
3129 goto rxh_next; \ 3139 goto rxh_next; \
3130 } while (0); 3140 } while (0);
3131 3141
3142 /* Lock here to avoid hitting all of the data used in the RX
3143 * path (e.g. key data, station data, ...) concurrently when
3144 * a frame is released from the reorder buffer due to timeout
3145 * from the timer, potentially concurrently with RX from the
3146 * driver.
3147 */
3132 spin_lock_bh(&rx->local->rx_path_lock); 3148 spin_lock_bh(&rx->local->rx_path_lock);
3133 3149
3134 while ((skb = __skb_dequeue(frames))) { 3150 while ((skb = __skb_dequeue(frames))) {
@@ -3421,7 +3437,8 @@ static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
3421 __le16 fc; 3437 __le16 fc;
3422 struct ieee80211_rx_data rx; 3438 struct ieee80211_rx_data rx;
3423 struct ieee80211_sub_if_data *prev; 3439 struct ieee80211_sub_if_data *prev;
3424 struct sta_info *sta, *tmp, *prev_sta; 3440 struct sta_info *sta, *prev_sta;
3441 struct rhash_head *tmp;
3425 int err = 0; 3442 int err = 0;
3426 3443
3427 fc = ((struct ieee80211_hdr *)skb->data)->frame_control; 3444 fc = ((struct ieee80211_hdr *)skb->data)->frame_control;
@@ -3456,9 +3473,13 @@ static void __ieee80211_rx_handle_packet(struct ieee80211_hw *hw,
3456 ieee80211_scan_rx(local, skb); 3473 ieee80211_scan_rx(local, skb);
3457 3474
3458 if (ieee80211_is_data(fc)) { 3475 if (ieee80211_is_data(fc)) {
3476 const struct bucket_table *tbl;
3477
3459 prev_sta = NULL; 3478 prev_sta = NULL;
3460 3479
3461 for_each_sta_info(local, hdr->addr2, sta, tmp) { 3480 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
3481
3482 for_each_sta_info(local, tbl, hdr->addr2, sta, tmp) {
3462 if (!prev_sta) { 3483 if (!prev_sta) {
3463 prev_sta = sta; 3484 prev_sta = sta;
3464 continue; 3485 continue;
diff --git a/net/mac80211/scan.c b/net/mac80211/scan.c
index 05f0d711b6d8..7bb6a9383f58 100644
--- a/net/mac80211/scan.c
+++ b/net/mac80211/scan.c
@@ -928,11 +928,12 @@ int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
928 928
929int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata, 929int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
930 const u8 *ssid, u8 ssid_len, 930 const u8 *ssid, u8 ssid_len,
931 struct ieee80211_channel *chan, 931 struct ieee80211_channel **channels,
932 unsigned int n_channels,
932 enum nl80211_bss_scan_width scan_width) 933 enum nl80211_bss_scan_width scan_width)
933{ 934{
934 struct ieee80211_local *local = sdata->local; 935 struct ieee80211_local *local = sdata->local;
935 int ret = -EBUSY; 936 int ret = -EBUSY, i, n_ch = 0;
936 enum ieee80211_band band; 937 enum ieee80211_band band;
937 938
938 mutex_lock(&local->mtx); 939 mutex_lock(&local->mtx);
@@ -942,9 +943,8 @@ int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
942 goto unlock; 943 goto unlock;
943 944
944 /* fill internal scan request */ 945 /* fill internal scan request */
945 if (!chan) { 946 if (!channels) {
946 int i, max_n; 947 int max_n;
947 int n_ch = 0;
948 948
949 for (band = 0; band < IEEE80211_NUM_BANDS; band++) { 949 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
950 if (!local->hw.wiphy->bands[band]) 950 if (!local->hw.wiphy->bands[band])
@@ -969,12 +969,19 @@ int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
969 969
970 local->int_scan_req->n_channels = n_ch; 970 local->int_scan_req->n_channels = n_ch;
971 } else { 971 } else {
972 if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IR | 972 for (i = 0; i < n_channels; i++) {
973 IEEE80211_CHAN_DISABLED))) 973 if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
974 IEEE80211_CHAN_DISABLED))
975 continue;
976
977 local->int_scan_req->channels[n_ch] = channels[i];
978 n_ch++;
979 }
980
981 if (WARN_ON_ONCE(n_ch == 0))
974 goto unlock; 982 goto unlock;
975 983
976 local->int_scan_req->channels[0] = chan; 984 local->int_scan_req->n_channels = n_ch;
977 local->int_scan_req->n_channels = 1;
978 } 985 }
979 986
980 local->int_scan_req->ssids = &local->scan_ssid; 987 local->int_scan_req->ssids = &local->scan_ssid;
diff --git a/net/mac80211/sta_info.c b/net/mac80211/sta_info.c
index 00ca8dcc2bcf..12971b71d0fa 100644
--- a/net/mac80211/sta_info.c
+++ b/net/mac80211/sta_info.c
@@ -64,32 +64,20 @@
64 * freed before they are done using it. 64 * freed before they are done using it.
65 */ 65 */
66 66
67static const struct rhashtable_params sta_rht_params = {
68 .nelem_hint = 3, /* start small */
69 .head_offset = offsetof(struct sta_info, hash_node),
70 .key_offset = offsetof(struct sta_info, sta.addr),
71 .key_len = ETH_ALEN,
72 .hashfn = sta_addr_hash,
73};
74
67/* Caller must hold local->sta_mtx */ 75/* Caller must hold local->sta_mtx */
68static int sta_info_hash_del(struct ieee80211_local *local, 76static int sta_info_hash_del(struct ieee80211_local *local,
69 struct sta_info *sta) 77 struct sta_info *sta)
70{ 78{
71 struct sta_info *s; 79 return rhashtable_remove_fast(&local->sta_hash, &sta->hash_node,
72 80 sta_rht_params);
73 s = rcu_dereference_protected(local->sta_hash[STA_HASH(sta->sta.addr)],
74 lockdep_is_held(&local->sta_mtx));
75 if (!s)
76 return -ENOENT;
77 if (s == sta) {
78 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)],
79 s->hnext);
80 return 0;
81 }
82
83 while (rcu_access_pointer(s->hnext) &&
84 rcu_access_pointer(s->hnext) != sta)
85 s = rcu_dereference_protected(s->hnext,
86 lockdep_is_held(&local->sta_mtx));
87 if (rcu_access_pointer(s->hnext)) {
88 rcu_assign_pointer(s->hnext, sta->hnext);
89 return 0;
90 }
91
92 return -ENOENT;
93} 81}
94 82
95static void __cleanup_single_sta(struct sta_info *sta) 83static void __cleanup_single_sta(struct sta_info *sta)
@@ -118,6 +106,16 @@ static void __cleanup_single_sta(struct sta_info *sta)
118 atomic_dec(&ps->num_sta_ps); 106 atomic_dec(&ps->num_sta_ps);
119 } 107 }
120 108
109 if (sta->sta.txq[0]) {
110 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
111 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
112 int n = skb_queue_len(&txqi->queue);
113
114 ieee80211_purge_tx_queue(&local->hw, &txqi->queue);
115 atomic_sub(n, &sdata->txqs_len[txqi->txq.ac]);
116 }
117 }
118
121 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) { 119 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
122 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]); 120 local->total_ps_buffered -= skb_queue_len(&sta->ps_tx_buf[ac]);
123 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]); 121 ieee80211_purge_tx_queue(&local->hw, &sta->ps_tx_buf[ac]);
@@ -159,18 +157,8 @@ struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
159 const u8 *addr) 157 const u8 *addr)
160{ 158{
161 struct ieee80211_local *local = sdata->local; 159 struct ieee80211_local *local = sdata->local;
162 struct sta_info *sta;
163 160
164 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 161 return rhashtable_lookup_fast(&local->sta_hash, addr, sta_rht_params);
165 lockdep_is_held(&local->sta_mtx));
166 while (sta) {
167 if (sta->sdata == sdata &&
168 ether_addr_equal(sta->sta.addr, addr))
169 break;
170 sta = rcu_dereference_check(sta->hnext,
171 lockdep_is_held(&local->sta_mtx));
172 }
173 return sta;
174} 162}
175 163
176/* 164/*
@@ -182,18 +170,24 @@ struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
182{ 170{
183 struct ieee80211_local *local = sdata->local; 171 struct ieee80211_local *local = sdata->local;
184 struct sta_info *sta; 172 struct sta_info *sta;
173 struct rhash_head *tmp;
174 const struct bucket_table *tbl;
185 175
186 sta = rcu_dereference_check(local->sta_hash[STA_HASH(addr)], 176 rcu_read_lock();
187 lockdep_is_held(&local->sta_mtx)); 177 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
188 while (sta) { 178
189 if ((sta->sdata == sdata || 179 for_each_sta_info(local, tbl, addr, sta, tmp) {
190 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) && 180 if (sta->sdata == sdata ||
191 ether_addr_equal(sta->sta.addr, addr)) 181 (sta->sdata->bss && sta->sdata->bss == sdata->bss)) {
192 break; 182 rcu_read_unlock();
193 sta = rcu_dereference_check(sta->hnext, 183 /* this is safe as the caller must already hold
194 lockdep_is_held(&local->sta_mtx)); 184 * another rcu read section or the mutex
185 */
186 return sta;
187 }
195 } 188 }
196 return sta; 189 rcu_read_unlock();
190 return NULL;
197} 191}
198 192
199struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata, 193struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
@@ -229,19 +223,13 @@ struct sta_info *sta_info_get_by_idx(struct ieee80211_sub_if_data *sdata,
229 */ 223 */
230void sta_info_free(struct ieee80211_local *local, struct sta_info *sta) 224void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
231{ 225{
232 int i;
233
234 if (sta->rate_ctrl) 226 if (sta->rate_ctrl)
235 rate_control_free_sta(sta); 227 rate_control_free_sta(sta);
236 228
237 if (sta->tx_lat) {
238 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
239 kfree(sta->tx_lat[i].bins);
240 kfree(sta->tx_lat);
241 }
242
243 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr); 229 sta_dbg(sta->sdata, "Destroyed STA %pM\n", sta->sta.addr);
244 230
231 if (sta->sta.txq[0])
232 kfree(to_txq_info(sta->sta.txq[0]));
245 kfree(rcu_dereference_raw(sta->sta.rates)); 233 kfree(rcu_dereference_raw(sta->sta.rates));
246 kfree(sta); 234 kfree(sta);
247} 235}
@@ -250,9 +238,8 @@ void sta_info_free(struct ieee80211_local *local, struct sta_info *sta)
250static void sta_info_hash_add(struct ieee80211_local *local, 238static void sta_info_hash_add(struct ieee80211_local *local,
251 struct sta_info *sta) 239 struct sta_info *sta)
252{ 240{
253 lockdep_assert_held(&local->sta_mtx); 241 rhashtable_insert_fast(&local->sta_hash, &sta->hash_node,
254 sta->hnext = local->sta_hash[STA_HASH(sta->sta.addr)]; 242 sta_rht_params);
255 rcu_assign_pointer(local->sta_hash[STA_HASH(sta->sta.addr)], sta);
256} 243}
257 244
258static void sta_deliver_ps_frames(struct work_struct *wk) 245static void sta_deliver_ps_frames(struct work_struct *wk)
@@ -293,44 +280,15 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
293 const u8 *addr, gfp_t gfp) 280 const u8 *addr, gfp_t gfp)
294{ 281{
295 struct ieee80211_local *local = sdata->local; 282 struct ieee80211_local *local = sdata->local;
283 struct ieee80211_hw *hw = &local->hw;
296 struct sta_info *sta; 284 struct sta_info *sta;
297 struct timespec uptime; 285 struct timespec uptime;
298 struct ieee80211_tx_latency_bin_ranges *tx_latency;
299 int i; 286 int i;
300 287
301 sta = kzalloc(sizeof(*sta) + local->hw.sta_data_size, gfp); 288 sta = kzalloc(sizeof(*sta) + hw->sta_data_size, gfp);
302 if (!sta) 289 if (!sta)
303 return NULL; 290 return NULL;
304 291
305 rcu_read_lock();
306 tx_latency = rcu_dereference(local->tx_latency);
307 /* init stations Tx latency statistics && TID bins */
308 if (tx_latency) {
309 sta->tx_lat = kzalloc(IEEE80211_NUM_TIDS *
310 sizeof(struct ieee80211_tx_latency_stat),
311 GFP_ATOMIC);
312 if (!sta->tx_lat) {
313 rcu_read_unlock();
314 goto free;
315 }
316
317 if (tx_latency->n_ranges) {
318 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
319 /* size of bins is size of the ranges +1 */
320 sta->tx_lat[i].bin_count =
321 tx_latency->n_ranges + 1;
322 sta->tx_lat[i].bins =
323 kcalloc(sta->tx_lat[i].bin_count,
324 sizeof(u32), GFP_ATOMIC);
325 if (!sta->tx_lat[i].bins) {
326 rcu_read_unlock();
327 goto free;
328 }
329 }
330 }
331 }
332 rcu_read_unlock();
333
334 spin_lock_init(&sta->lock); 292 spin_lock_init(&sta->lock);
335 spin_lock_init(&sta->ps_lock); 293 spin_lock_init(&sta->ps_lock);
336 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames); 294 INIT_WORK(&sta->drv_deliver_wk, sta_deliver_ps_frames);
@@ -359,8 +317,24 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
359 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++) 317 for (i = 0; i < ARRAY_SIZE(sta->chain_signal_avg); i++)
360 ewma_init(&sta->chain_signal_avg[i], 1024, 8); 318 ewma_init(&sta->chain_signal_avg[i], 1024, 8);
361 319
320 if (local->ops->wake_tx_queue) {
321 void *txq_data;
322 int size = sizeof(struct txq_info) +
323 ALIGN(hw->txq_data_size, sizeof(void *));
324
325 txq_data = kcalloc(ARRAY_SIZE(sta->sta.txq), size, gfp);
326 if (!txq_data)
327 goto free;
328
329 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
330 struct txq_info *txq = txq_data + i * size;
331
332 ieee80211_init_tx_queue(sdata, sta, txq, i);
333 }
334 }
335
362 if (sta_prepare_rate_control(local, sta, gfp)) 336 if (sta_prepare_rate_control(local, sta, gfp))
363 goto free; 337 goto free_txq;
364 338
365 for (i = 0; i < IEEE80211_NUM_TIDS; i++) { 339 for (i = 0; i < IEEE80211_NUM_TIDS; i++) {
366 /* 340 /*
@@ -382,7 +356,7 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
382 if (sdata->vif.type == NL80211_IFTYPE_AP || 356 if (sdata->vif.type == NL80211_IFTYPE_AP ||
383 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 357 sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
384 struct ieee80211_supported_band *sband = 358 struct ieee80211_supported_band *sband =
385 local->hw.wiphy->bands[ieee80211_get_sdata_band(sdata)]; 359 hw->wiphy->bands[ieee80211_get_sdata_band(sdata)];
386 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 360 u8 smps = (sband->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >>
387 IEEE80211_HT_CAP_SM_PS_SHIFT; 361 IEEE80211_HT_CAP_SM_PS_SHIFT;
388 /* 362 /*
@@ -405,14 +379,13 @@ struct sta_info *sta_info_alloc(struct ieee80211_sub_if_data *sdata,
405 } 379 }
406 380
407 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr); 381 sta_dbg(sdata, "Allocated STA %pM\n", sta->sta.addr);
382
408 return sta; 383 return sta;
409 384
385free_txq:
386 if (sta->sta.txq[0])
387 kfree(to_txq_info(sta->sta.txq[0]));
410free: 388free:
411 if (sta->tx_lat) {
412 for (i = 0; i < IEEE80211_NUM_TIDS; i++)
413 kfree(sta->tx_lat[i].bins);
414 kfree(sta->tx_lat);
415 }
416 kfree(sta); 389 kfree(sta);
417 return NULL; 390 return NULL;
418} 391}
@@ -684,6 +657,8 @@ static void __sta_info_recalc_tim(struct sta_info *sta, bool ignore_pending)
684 657
685 indicate_tim |= 658 indicate_tim |=
686 sta->driver_buffered_tids & tids; 659 sta->driver_buffered_tids & tids;
660 indicate_tim |=
661 sta->txq_buffered_tids & tids;
687 } 662 }
688 663
689 done: 664 done:
@@ -992,19 +967,32 @@ static void sta_info_cleanup(unsigned long data)
992 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL)); 967 round_jiffies(jiffies + STA_INFO_CLEANUP_INTERVAL));
993} 968}
994 969
995void sta_info_init(struct ieee80211_local *local) 970u32 sta_addr_hash(const void *key, u32 length, u32 seed)
971{
972 return jhash(key, ETH_ALEN, seed);
973}
974
975int sta_info_init(struct ieee80211_local *local)
996{ 976{
977 int err;
978
979 err = rhashtable_init(&local->sta_hash, &sta_rht_params);
980 if (err)
981 return err;
982
997 spin_lock_init(&local->tim_lock); 983 spin_lock_init(&local->tim_lock);
998 mutex_init(&local->sta_mtx); 984 mutex_init(&local->sta_mtx);
999 INIT_LIST_HEAD(&local->sta_list); 985 INIT_LIST_HEAD(&local->sta_list);
1000 986
1001 setup_timer(&local->sta_cleanup, sta_info_cleanup, 987 setup_timer(&local->sta_cleanup, sta_info_cleanup,
1002 (unsigned long)local); 988 (unsigned long)local);
989 return 0;
1003} 990}
1004 991
1005void sta_info_stop(struct ieee80211_local *local) 992void sta_info_stop(struct ieee80211_local *local)
1006{ 993{
1007 del_timer_sync(&local->sta_cleanup); 994 del_timer_sync(&local->sta_cleanup);
995 rhashtable_destroy(&local->sta_hash);
1008} 996}
1009 997
1010 998
@@ -1068,16 +1056,21 @@ void ieee80211_sta_expire(struct ieee80211_sub_if_data *sdata,
1068} 1056}
1069 1057
1070struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw, 1058struct ieee80211_sta *ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
1071 const u8 *addr, 1059 const u8 *addr,
1072 const u8 *localaddr) 1060 const u8 *localaddr)
1073{ 1061{
1074 struct sta_info *sta, *nxt; 1062 struct ieee80211_local *local = hw_to_local(hw);
1063 struct sta_info *sta;
1064 struct rhash_head *tmp;
1065 const struct bucket_table *tbl;
1066
1067 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
1075 1068
1076 /* 1069 /*
1077 * Just return a random station if localaddr is NULL 1070 * Just return a random station if localaddr is NULL
1078 * ... first in list. 1071 * ... first in list.
1079 */ 1072 */
1080 for_each_sta_info(hw_to_local(hw), addr, sta, nxt) { 1073 for_each_sta_info(local, tbl, addr, sta, tmp) {
1081 if (localaddr && 1074 if (localaddr &&
1082 !ether_addr_equal(sta->sdata->vif.addr, localaddr)) 1075 !ether_addr_equal(sta->sdata->vif.addr, localaddr))
1083 continue; 1076 continue;
@@ -1115,7 +1108,7 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1115 struct ieee80211_sub_if_data *sdata = sta->sdata; 1108 struct ieee80211_sub_if_data *sdata = sta->sdata;
1116 struct ieee80211_local *local = sdata->local; 1109 struct ieee80211_local *local = sdata->local;
1117 struct sk_buff_head pending; 1110 struct sk_buff_head pending;
1118 int filtered = 0, buffered = 0, ac; 1111 int filtered = 0, buffered = 0, ac, i;
1119 unsigned long flags; 1112 unsigned long flags;
1120 struct ps_data *ps; 1113 struct ps_data *ps;
1121 1114
@@ -1134,10 +1127,22 @@ void ieee80211_sta_ps_deliver_wakeup(struct sta_info *sta)
1134 1127
1135 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1); 1128 BUILD_BUG_ON(BITS_TO_LONGS(IEEE80211_NUM_TIDS) > 1);
1136 sta->driver_buffered_tids = 0; 1129 sta->driver_buffered_tids = 0;
1130 sta->txq_buffered_tids = 0;
1137 1131
1138 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS)) 1132 if (!(local->hw.flags & IEEE80211_HW_AP_LINK_PS))
1139 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta); 1133 drv_sta_notify(local, sdata, STA_NOTIFY_AWAKE, &sta->sta);
1140 1134
1135 if (sta->sta.txq[0]) {
1136 for (i = 0; i < ARRAY_SIZE(sta->sta.txq); i++) {
1137 struct txq_info *txqi = to_txq_info(sta->sta.txq[i]);
1138
1139 if (!skb_queue_len(&txqi->queue))
1140 continue;
1141
1142 drv_wake_tx_queue(local, txqi);
1143 }
1144 }
1145
1141 skb_queue_head_init(&pending); 1146 skb_queue_head_init(&pending);
1142 1147
1143 /* sync with ieee80211_tx_h_unicast_ps_buf */ 1148 /* sync with ieee80211_tx_h_unicast_ps_buf */
@@ -1275,7 +1280,7 @@ static void ieee80211_send_null_response(struct ieee80211_sub_if_data *sdata,
1275 } 1280 }
1276 1281
1277 info->band = chanctx_conf->def.chan->band; 1282 info->band = chanctx_conf->def.chan->band;
1278 ieee80211_xmit(sdata, skb); 1283 ieee80211_xmit(sdata, sta, skb);
1279 rcu_read_unlock(); 1284 rcu_read_unlock();
1280} 1285}
1281 1286
@@ -1319,8 +1324,10 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1319 /* if we already have frames from software, then we can't also 1324 /* if we already have frames from software, then we can't also
1320 * release from hardware queues 1325 * release from hardware queues
1321 */ 1326 */
1322 if (skb_queue_empty(&frames)) 1327 if (skb_queue_empty(&frames)) {
1323 driver_release_tids |= sta->driver_buffered_tids & tids; 1328 driver_release_tids |= sta->driver_buffered_tids & tids;
1329 driver_release_tids |= sta->txq_buffered_tids & tids;
1330 }
1324 1331
1325 if (driver_release_tids) { 1332 if (driver_release_tids) {
1326 /* If the driver has data on more than one TID then 1333 /* If the driver has data on more than one TID then
@@ -1491,6 +1498,9 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1491 1498
1492 sta_info_recalc_tim(sta); 1499 sta_info_recalc_tim(sta);
1493 } else { 1500 } else {
1501 unsigned long tids = sta->txq_buffered_tids & driver_release_tids;
1502 int tid;
1503
1494 /* 1504 /*
1495 * We need to release a frame that is buffered somewhere in the 1505 * We need to release a frame that is buffered somewhere in the
1496 * driver ... it'll have to handle that. 1506 * driver ... it'll have to handle that.
@@ -1510,8 +1520,22 @@ ieee80211_sta_ps_deliver_response(struct sta_info *sta,
1510 * that the TID(s) became empty before returning here from the 1520 * that the TID(s) became empty before returning here from the
1511 * release function. 1521 * release function.
1512 * Either way, however, when the driver tells us that the TID(s) 1522 * Either way, however, when the driver tells us that the TID(s)
1513 * became empty we'll do the TIM recalculation. 1523 * became empty or we find that a txq became empty, we'll do the
1524 * TIM recalculation.
1514 */ 1525 */
1526
1527 if (!sta->sta.txq[0])
1528 return;
1529
1530 for (tid = 0; tid < ARRAY_SIZE(sta->sta.txq); tid++) {
1531 struct txq_info *txqi = to_txq_info(sta->sta.txq[tid]);
1532
1533 if (!(tids & BIT(tid)) || skb_queue_len(&txqi->queue))
1534 continue;
1535
1536 sta_info_recalc_tim(sta);
1537 break;
1538 }
1515 } 1539 }
1516} 1540}
1517 1541
diff --git a/net/mac80211/sta_info.h b/net/mac80211/sta_info.h
index fb0fc1302a58..5c164fb3f6c5 100644
--- a/net/mac80211/sta_info.h
+++ b/net/mac80211/sta_info.h
@@ -16,6 +16,7 @@
16#include <linux/workqueue.h> 16#include <linux/workqueue.h>
17#include <linux/average.h> 17#include <linux/average.h>
18#include <linux/etherdevice.h> 18#include <linux/etherdevice.h>
19#include <linux/rhashtable.h>
19#include "key.h" 20#include "key.h"
20 21
21/** 22/**
@@ -236,25 +237,6 @@ struct sta_ampdu_mlme {
236 u8 dialog_token_allocator; 237 u8 dialog_token_allocator;
237}; 238};
238 239
239/*
240 * struct ieee80211_tx_latency_stat - Tx latency statistics
241 *
242 * Measures TX latency and jitter for a station per TID.
243 *
244 * @max: worst case latency
245 * @sum: sum of all latencies
246 * @counter: amount of Tx frames sent from interface
247 * @bins: each bin counts how many frames transmitted within a certain
248 * latency range. when disabled it is NULL.
249 * @bin_count: amount of bins.
250 */
251struct ieee80211_tx_latency_stat {
252 u32 max;
253 u32 sum;
254 u32 counter;
255 u32 *bins;
256 u32 bin_count;
257};
258 240
259/* Value to indicate no TID reservation */ 241/* Value to indicate no TID reservation */
260#define IEEE80211_TID_UNRESERVED 0xff 242#define IEEE80211_TID_UNRESERVED 0xff
@@ -267,7 +249,7 @@ struct ieee80211_tx_latency_stat {
267 * 249 *
268 * @list: global linked list entry 250 * @list: global linked list entry
269 * @free_list: list entry for keeping track of stations to free 251 * @free_list: list entry for keeping track of stations to free
270 * @hnext: hash table linked list pointer 252 * @hash_node: hash node for rhashtable
271 * @local: pointer to the global information 253 * @local: pointer to the global information
272 * @sdata: virtual interface this station belongs to 254 * @sdata: virtual interface this station belongs to
273 * @ptk: peer keys negotiated with this station, if any 255 * @ptk: peer keys negotiated with this station, if any
@@ -295,6 +277,7 @@ struct ieee80211_tx_latency_stat {
295 * entered power saving state, these are also delivered to 277 * entered power saving state, these are also delivered to
296 * the station when it leaves powersave or polls for frames 278 * the station when it leaves powersave or polls for frames
297 * @driver_buffered_tids: bitmap of TIDs the driver has data buffered on 279 * @driver_buffered_tids: bitmap of TIDs the driver has data buffered on
280 * @txq_buffered_tids: bitmap of TIDs that mac80211 has txq data buffered on
298 * @rx_packets: Number of MSDUs received from this STA 281 * @rx_packets: Number of MSDUs received from this STA
299 * @rx_bytes: Number of bytes received from this STA 282 * @rx_bytes: Number of bytes received from this STA
300 * @last_rx: time (in jiffies) when last frame was received from this STA 283 * @last_rx: time (in jiffies) when last frame was received from this STA
@@ -316,7 +299,6 @@ struct ieee80211_tx_latency_stat {
316 * @tid_seq: per-TID sequence numbers for sending to this STA 299 * @tid_seq: per-TID sequence numbers for sending to this STA
317 * @ampdu_mlme: A-MPDU state machine state 300 * @ampdu_mlme: A-MPDU state machine state
318 * @timer_to_tid: identity mapping to ID timers 301 * @timer_to_tid: identity mapping to ID timers
319 * @tx_lat: Tx latency statistics
320 * @llid: Local link ID 302 * @llid: Local link ID
321 * @plid: Peer link ID 303 * @plid: Peer link ID
322 * @reason: Cancel reason on PLINK_HOLDING state 304 * @reason: Cancel reason on PLINK_HOLDING state
@@ -361,7 +343,7 @@ struct sta_info {
361 /* General information, mostly static */ 343 /* General information, mostly static */
362 struct list_head list, free_list; 344 struct list_head list, free_list;
363 struct rcu_head rcu_head; 345 struct rcu_head rcu_head;
364 struct sta_info __rcu *hnext; 346 struct rhash_head hash_node;
365 struct ieee80211_local *local; 347 struct ieee80211_local *local;
366 struct ieee80211_sub_if_data *sdata; 348 struct ieee80211_sub_if_data *sdata;
367 struct ieee80211_key __rcu *gtk[NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS]; 349 struct ieee80211_key __rcu *gtk[NUM_DEFAULT_KEYS + NUM_DEFAULT_MGMT_KEYS];
@@ -390,6 +372,7 @@ struct sta_info {
390 struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS]; 372 struct sk_buff_head ps_tx_buf[IEEE80211_NUM_ACS];
391 struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS]; 373 struct sk_buff_head tx_filtered[IEEE80211_NUM_ACS];
392 unsigned long driver_buffered_tids; 374 unsigned long driver_buffered_tids;
375 unsigned long txq_buffered_tids;
393 376
394 /* Updated from RX path only, no locking requirements */ 377 /* Updated from RX path only, no locking requirements */
395 unsigned long rx_packets; 378 unsigned long rx_packets;
@@ -437,8 +420,6 @@ struct sta_info {
437 struct sta_ampdu_mlme ampdu_mlme; 420 struct sta_ampdu_mlme ampdu_mlme;
438 u8 timer_to_tid[IEEE80211_NUM_TIDS]; 421 u8 timer_to_tid[IEEE80211_NUM_TIDS];
439 422
440 struct ieee80211_tx_latency_stat *tx_lat;
441
442#ifdef CONFIG_MAC80211_MESH 423#ifdef CONFIG_MAC80211_MESH
443 /* 424 /*
444 * Mesh peer link attributes 425 * Mesh peer link attributes
@@ -559,10 +540,6 @@ rcu_dereference_protected_tid_tx(struct sta_info *sta, int tid)
559 lockdep_is_held(&sta->ampdu_mlme.mtx)); 540 lockdep_is_held(&sta->ampdu_mlme.mtx));
560} 541}
561 542
562#define STA_HASH_SIZE 256
563#define STA_HASH(sta) (sta[5])
564
565
566/* Maximum number of frames to buffer per power saving station per AC */ 543/* Maximum number of frames to buffer per power saving station per AC */
567#define STA_MAX_TX_BUFFER 64 544#define STA_MAX_TX_BUFFER 64
568 545
@@ -583,26 +560,15 @@ struct sta_info *sta_info_get(struct ieee80211_sub_if_data *sdata,
583struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata, 560struct sta_info *sta_info_get_bss(struct ieee80211_sub_if_data *sdata,
584 const u8 *addr); 561 const u8 *addr);
585 562
586static inline 563u32 sta_addr_hash(const void *key, u32 length, u32 seed);
587void for_each_sta_info_type_check(struct ieee80211_local *local, 564
588 const u8 *addr, 565#define _sta_bucket_idx(_tbl, _a) \
589 struct sta_info *sta, 566 rht_bucket_index(_tbl, sta_addr_hash(_a, ETH_ALEN, (_tbl)->hash_rnd))
590 struct sta_info *nxt)
591{
592}
593 567
594#define for_each_sta_info(local, _addr, _sta, nxt) \ 568#define for_each_sta_info(local, tbl, _addr, _sta, _tmp) \
595 for ( /* initialise loop */ \ 569 rht_for_each_entry_rcu(_sta, _tmp, tbl, \
596 _sta = rcu_dereference(local->sta_hash[STA_HASH(_addr)]),\ 570 _sta_bucket_idx(tbl, _addr), \
597 nxt = _sta ? rcu_dereference(_sta->hnext) : NULL; \ 571 hash_node) \
598 /* typecheck */ \
599 for_each_sta_info_type_check(local, (_addr), _sta, nxt),\
600 /* continue condition */ \
601 _sta; \
602 /* advance loop */ \
603 _sta = nxt, \
604 nxt = _sta ? rcu_dereference(_sta->hnext) : NULL \
605 ) \
606 /* compare address and run code only if it matches */ \ 572 /* compare address and run code only if it matches */ \
607 if (ether_addr_equal(_sta->sta.addr, (_addr))) 573 if (ether_addr_equal(_sta->sta.addr, (_addr)))
608 574
@@ -639,7 +605,7 @@ int sta_info_destroy_addr_bss(struct ieee80211_sub_if_data *sdata,
639 605
640void sta_info_recalc_tim(struct sta_info *sta); 606void sta_info_recalc_tim(struct sta_info *sta);
641 607
642void sta_info_init(struct ieee80211_local *local); 608int sta_info_init(struct ieee80211_local *local);
643void sta_info_stop(struct ieee80211_local *local); 609void sta_info_stop(struct ieee80211_local *local);
644 610
645/** 611/**
diff --git a/net/mac80211/status.c b/net/mac80211/status.c
index e679b7c9b160..005fdbe39a8b 100644
--- a/net/mac80211/status.c
+++ b/net/mac80211/status.c
@@ -12,7 +12,6 @@
12 12
13#include <linux/export.h> 13#include <linux/export.h>
14#include <linux/etherdevice.h> 14#include <linux/etherdevice.h>
15#include <linux/time.h>
16#include <net/mac80211.h> 15#include <net/mac80211.h>
17#include <asm/unaligned.h> 16#include <asm/unaligned.h>
18#include "ieee80211_i.h" 17#include "ieee80211_i.h"
@@ -515,73 +514,6 @@ static void ieee80211_report_used_skb(struct ieee80211_local *local,
515} 514}
516 515
517/* 516/*
518 * Measure Tx frame completion and removal time for Tx latency statistics
519 * calculation. A single Tx frame latency should be measured from when it
520 * is entering the Kernel until we receive Tx complete confirmation indication
521 * and remove the skb.
522 */
523static void ieee80211_tx_latency_end_msrmnt(struct ieee80211_local *local,
524 struct sk_buff *skb,
525 struct sta_info *sta,
526 struct ieee80211_hdr *hdr)
527{
528 u32 msrmnt;
529 u16 tid;
530 u8 *qc;
531 int i, bin_range_count;
532 u32 *bin_ranges;
533 __le16 fc;
534 struct ieee80211_tx_latency_stat *tx_lat;
535 struct ieee80211_tx_latency_bin_ranges *tx_latency;
536 ktime_t skb_arv = skb->tstamp;
537
538 tx_latency = rcu_dereference(local->tx_latency);
539
540 /* assert Tx latency stats are enabled & frame arrived when enabled */
541 if (!tx_latency || !ktime_to_ns(skb_arv))
542 return;
543
544 fc = hdr->frame_control;
545
546 if (!ieee80211_is_data(fc)) /* make sure it is a data frame */
547 return;
548
549 /* get frame tid */
550 if (ieee80211_is_data_qos(hdr->frame_control)) {
551 qc = ieee80211_get_qos_ctl(hdr);
552 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
553 } else {
554 tid = 0;
555 }
556
557 tx_lat = &sta->tx_lat[tid];
558
559 /* Calculate the latency */
560 msrmnt = ktime_to_ms(ktime_sub(ktime_get(), skb_arv));
561
562 if (tx_lat->max < msrmnt) /* update stats */
563 tx_lat->max = msrmnt;
564 tx_lat->counter++;
565 tx_lat->sum += msrmnt;
566
567 if (!tx_lat->bins) /* bins not activated */
568 return;
569
570 /* count how many Tx frames transmitted with the appropriate latency */
571 bin_range_count = tx_latency->n_ranges;
572 bin_ranges = tx_latency->ranges;
573
574 for (i = 0; i < bin_range_count; i++) {
575 if (msrmnt <= bin_ranges[i]) {
576 tx_lat->bins[i]++;
577 break;
578 }
579 }
580 if (i == bin_range_count) /* msrmnt is bigger than the biggest range */
581 tx_lat->bins[i]++;
582}
583
584/*
585 * Use a static threshold for now, best value to be determined 517 * Use a static threshold for now, best value to be determined
586 * by testing ... 518 * by testing ...
587 * Should it depend on: 519 * Should it depend on:
@@ -722,7 +654,8 @@ void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
722 struct ieee80211_supported_band *sband; 654 struct ieee80211_supported_band *sband;
723 struct ieee80211_sub_if_data *sdata; 655 struct ieee80211_sub_if_data *sdata;
724 struct net_device *prev_dev = NULL; 656 struct net_device *prev_dev = NULL;
725 struct sta_info *sta, *tmp; 657 struct sta_info *sta;
658 struct rhash_head *tmp;
726 int retry_count; 659 int retry_count;
727 int rates_idx; 660 int rates_idx;
728 bool send_to_cooked; 661 bool send_to_cooked;
@@ -731,6 +664,7 @@ void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
731 int rtap_len; 664 int rtap_len;
732 int shift = 0; 665 int shift = 0;
733 int tid = IEEE80211_NUM_TIDS; 666 int tid = IEEE80211_NUM_TIDS;
667 const struct bucket_table *tbl;
734 668
735 rates_idx = ieee80211_tx_get_rates(hw, info, &retry_count); 669 rates_idx = ieee80211_tx_get_rates(hw, info, &retry_count);
736 670
@@ -739,7 +673,9 @@ void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
739 sband = local->hw.wiphy->bands[info->band]; 673 sband = local->hw.wiphy->bands[info->band];
740 fc = hdr->frame_control; 674 fc = hdr->frame_control;
741 675
742 for_each_sta_info(local, hdr->addr1, sta, tmp) { 676 tbl = rht_dereference_rcu(local->sta_hash.tbl, &local->sta_hash);
677
678 for_each_sta_info(local, tbl, hdr->addr1, sta, tmp) {
743 /* skip wrong virtual interface */ 679 /* skip wrong virtual interface */
744 if (!ether_addr_equal(hdr->addr2, sta->sdata->vif.addr)) 680 if (!ether_addr_equal(hdr->addr2, sta->sdata->vif.addr))
745 continue; 681 continue;
@@ -853,12 +789,6 @@ void ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
853 789
854 if (acked) 790 if (acked)
855 sta->last_ack_signal = info->status.ack_signal; 791 sta->last_ack_signal = info->status.ack_signal;
856
857 /*
858 * Measure frame removal for tx latency
859 * statistics calculation
860 */
861 ieee80211_tx_latency_end_msrmnt(local, skb, sta, hdr);
862 } 792 }
863 793
864 rcu_read_unlock(); 794 rcu_read_unlock();
diff --git a/net/mac80211/tdls.c b/net/mac80211/tdls.c
index c9f9752217ac..fff0d864adfa 100644
--- a/net/mac80211/tdls.c
+++ b/net/mac80211/tdls.c
@@ -136,6 +136,24 @@ ieee80211_tdls_add_supp_channels(struct ieee80211_sub_if_data *sdata,
136 *pos = 2 * subband_cnt; 136 *pos = 2 * subband_cnt;
137} 137}
138 138
139static void ieee80211_tdls_add_oper_classes(struct ieee80211_sub_if_data *sdata,
140 struct sk_buff *skb)
141{
142 u8 *pos;
143 u8 op_class;
144
145 if (!ieee80211_chandef_to_operating_class(&sdata->vif.bss_conf.chandef,
146 &op_class))
147 return;
148
149 pos = skb_put(skb, 4);
150 *pos++ = WLAN_EID_SUPPORTED_REGULATORY_CLASSES;
151 *pos++ = 2; /* len */
152
153 *pos++ = op_class;
154 *pos++ = op_class; /* give current operating class as alternate too */
155}
156
139static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb) 157static void ieee80211_tdls_add_bss_coex_ie(struct sk_buff *skb)
140{ 158{
141 u8 *pos = (void *)skb_put(skb, 3); 159 u8 *pos = (void *)skb_put(skb, 3);
@@ -193,6 +211,17 @@ static void ieee80211_tdls_add_link_ie(struct ieee80211_sub_if_data *sdata,
193 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN); 211 memcpy(lnkid->resp_sta, rsp_addr, ETH_ALEN);
194} 212}
195 213
214static void
215ieee80211_tdls_add_aid(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
216{
217 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
218 u8 *pos = (void *)skb_put(skb, 4);
219
220 *pos++ = WLAN_EID_AID;
221 *pos++ = 2; /* len */
222 put_unaligned_le16(ifmgd->aid, pos);
223}
224
196/* translate numbering in the WMM parameter IE to the mac80211 notation */ 225/* translate numbering in the WMM parameter IE to the mac80211 notation */
197static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac) 226static enum ieee80211_ac_numbers ieee80211_ac_from_wmm(int ac)
198{ 227{
@@ -271,21 +300,11 @@ ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
271 struct ieee80211_local *local = sdata->local; 300 struct ieee80211_local *local = sdata->local;
272 struct ieee80211_supported_band *sband; 301 struct ieee80211_supported_band *sband;
273 struct ieee80211_sta_ht_cap ht_cap; 302 struct ieee80211_sta_ht_cap ht_cap;
303 struct ieee80211_sta_vht_cap vht_cap;
274 struct sta_info *sta = NULL; 304 struct sta_info *sta = NULL;
275 size_t offset = 0, noffset; 305 size_t offset = 0, noffset;
276 u8 *pos; 306 u8 *pos;
277 307
278 rcu_read_lock();
279
280 /* we should have the peer STA if we're already responding */
281 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
282 sta = sta_info_get(sdata, peer);
283 if (WARN_ON_ONCE(!sta)) {
284 rcu_read_unlock();
285 return;
286 }
287 }
288
289 ieee80211_add_srates_ie(sdata, skb, false, band); 308 ieee80211_add_srates_ie(sdata, skb, false, band);
290 ieee80211_add_ext_srates_ie(sdata, skb, false, band); 309 ieee80211_add_ext_srates_ie(sdata, skb, false, band);
291 ieee80211_tdls_add_supp_channels(sdata, skb); 310 ieee80211_tdls_add_supp_channels(sdata, skb);
@@ -338,6 +357,19 @@ ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
338 offset = noffset; 357 offset = noffset;
339 } 358 }
340 359
360 rcu_read_lock();
361
362 /* we should have the peer STA if we're already responding */
363 if (action_code == WLAN_TDLS_SETUP_RESPONSE) {
364 sta = sta_info_get(sdata, peer);
365 if (WARN_ON_ONCE(!sta)) {
366 rcu_read_unlock();
367 return;
368 }
369 }
370
371 ieee80211_tdls_add_oper_classes(sdata, skb);
372
341 /* 373 /*
342 * with TDLS we can switch channels, and HT-caps are not necessarily 374 * with TDLS we can switch channels, and HT-caps are not necessarily
343 * the same on all bands. The specification limits the setup to a 375 * the same on all bands. The specification limits the setup to a
@@ -346,7 +378,9 @@ ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
346 sband = local->hw.wiphy->bands[band]; 378 sband = local->hw.wiphy->bands[band];
347 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap)); 379 memcpy(&ht_cap, &sband->ht_cap, sizeof(ht_cap));
348 380
349 if (action_code == WLAN_TDLS_SETUP_REQUEST && ht_cap.ht_supported) { 381 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
382 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
383 ht_cap.ht_supported) {
350 ieee80211_apply_htcap_overrides(sdata, &ht_cap); 384 ieee80211_apply_htcap_overrides(sdata, &ht_cap);
351 385
352 /* disable SMPS in TDLS initiator */ 386 /* disable SMPS in TDLS initiator */
@@ -368,12 +402,63 @@ ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
368 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap); 402 ieee80211_ie_build_ht_cap(pos, &ht_cap, ht_cap.cap);
369 } 403 }
370 404
371 rcu_read_unlock();
372
373 if (ht_cap.ht_supported && 405 if (ht_cap.ht_supported &&
374 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)) 406 (ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40))
375 ieee80211_tdls_add_bss_coex_ie(skb); 407 ieee80211_tdls_add_bss_coex_ie(skb);
376 408
409 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
410
411 /* add any custom IEs that go before VHT capabilities */
412 if (extra_ies_len) {
413 static const u8 before_vht_cap[] = {
414 WLAN_EID_SUPP_RATES,
415 WLAN_EID_COUNTRY,
416 WLAN_EID_EXT_SUPP_RATES,
417 WLAN_EID_SUPPORTED_CHANNELS,
418 WLAN_EID_RSN,
419 WLAN_EID_EXT_CAPABILITY,
420 WLAN_EID_QOS_CAPA,
421 WLAN_EID_FAST_BSS_TRANSITION,
422 WLAN_EID_TIMEOUT_INTERVAL,
423 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
424 WLAN_EID_MULTI_BAND,
425 };
426 noffset = ieee80211_ie_split(extra_ies, extra_ies_len,
427 before_vht_cap,
428 ARRAY_SIZE(before_vht_cap),
429 offset);
430 pos = skb_put(skb, noffset - offset);
431 memcpy(pos, extra_ies + offset, noffset - offset);
432 offset = noffset;
433 }
434
435 /* build the VHT-cap similarly to the HT-cap */
436 memcpy(&vht_cap, &sband->vht_cap, sizeof(vht_cap));
437 if ((action_code == WLAN_TDLS_SETUP_REQUEST ||
438 action_code == WLAN_PUB_ACTION_TDLS_DISCOVER_RES) &&
439 vht_cap.vht_supported) {
440 ieee80211_apply_vhtcap_overrides(sdata, &vht_cap);
441
442 /* the AID is present only when VHT is implemented */
443 if (action_code == WLAN_TDLS_SETUP_REQUEST)
444 ieee80211_tdls_add_aid(sdata, skb);
445
446 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
447 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
448 } else if (action_code == WLAN_TDLS_SETUP_RESPONSE &&
449 vht_cap.vht_supported && sta->sta.vht_cap.vht_supported) {
450 /* the peer caps are already intersected with our own */
451 memcpy(&vht_cap, &sta->sta.vht_cap, sizeof(vht_cap));
452
453 /* the AID is present only when VHT is implemented */
454 ieee80211_tdls_add_aid(sdata, skb);
455
456 pos = skb_put(skb, sizeof(struct ieee80211_vht_cap) + 2);
457 ieee80211_ie_build_vht_cap(pos, &vht_cap, vht_cap.cap);
458 }
459
460 rcu_read_unlock();
461
377 /* add any remaining IEs */ 462 /* add any remaining IEs */
378 if (extra_ies_len) { 463 if (extra_ies_len) {
379 noffset = extra_ies_len; 464 noffset = extra_ies_len;
@@ -381,7 +466,6 @@ ieee80211_tdls_add_setup_start_ies(struct ieee80211_sub_if_data *sdata,
381 memcpy(pos, extra_ies + offset, noffset - offset); 466 memcpy(pos, extra_ies + offset, noffset - offset);
382 } 467 }
383 468
384 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
385} 469}
386 470
387static void 471static void
@@ -394,6 +478,7 @@ ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
394 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd; 478 struct ieee80211_if_managed *ifmgd = &sdata->u.mgd;
395 size_t offset = 0, noffset; 479 size_t offset = 0, noffset;
396 struct sta_info *sta, *ap_sta; 480 struct sta_info *sta, *ap_sta;
481 enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
397 u8 *pos; 482 u8 *pos;
398 483
399 rcu_read_lock(); 484 rcu_read_lock();
@@ -453,6 +538,21 @@ ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
453 } 538 }
454 } 539 }
455 540
541 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
542
543 /* only include VHT-operation if not on the 2.4GHz band */
544 if (band != IEEE80211_BAND_2GHZ && !ap_sta->sta.vht_cap.vht_supported &&
545 sta->sta.vht_cap.vht_supported) {
546 struct ieee80211_chanctx_conf *chanctx_conf =
547 rcu_dereference(sdata->vif.chanctx_conf);
548 if (!WARN_ON(!chanctx_conf)) {
549 pos = skb_put(skb, 2 +
550 sizeof(struct ieee80211_vht_operation));
551 ieee80211_ie_build_vht_oper(pos, &sta->sta.vht_cap,
552 &chanctx_conf->def);
553 }
554 }
555
456 rcu_read_unlock(); 556 rcu_read_unlock();
457 557
458 /* add any remaining IEs */ 558 /* add any remaining IEs */
@@ -461,8 +561,6 @@ ieee80211_tdls_add_setup_cfm_ies(struct ieee80211_sub_if_data *sdata,
461 pos = skb_put(skb, noffset - offset); 561 pos = skb_put(skb, noffset - offset);
462 memcpy(pos, extra_ies + offset, noffset - offset); 562 memcpy(pos, extra_ies + offset, noffset - offset);
463 } 563 }
464
465 ieee80211_tdls_add_link_ie(sdata, skb, peer, initiator);
466} 564}
467 565
468static void 566static void
@@ -708,8 +806,12 @@ ieee80211_tdls_build_mgmt_packet_data(struct ieee80211_sub_if_data *sdata,
708 26 + /* max(WMM-info, WMM-param) */ 806 26 + /* max(WMM-info, WMM-param) */
709 2 + max(sizeof(struct ieee80211_ht_cap), 807 2 + max(sizeof(struct ieee80211_ht_cap),
710 sizeof(struct ieee80211_ht_operation)) + 808 sizeof(struct ieee80211_ht_operation)) +
809 2 + max(sizeof(struct ieee80211_vht_cap),
810 sizeof(struct ieee80211_vht_operation)) +
711 50 + /* supported channels */ 811 50 + /* supported channels */
712 3 + /* 40/20 BSS coex */ 812 3 + /* 40/20 BSS coex */
813 4 + /* AID */
814 4 + /* oper classes */
713 extra_ies_len + 815 extra_ies_len +
714 sizeof(struct ieee80211_tdls_lnkie)); 816 sizeof(struct ieee80211_tdls_lnkie));
715 if (!skb) 817 if (!skb)
@@ -907,7 +1009,7 @@ ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
907 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) && 1009 if (!is_zero_ether_addr(sdata->u.mgd.tdls_peer) &&
908 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) { 1010 !ether_addr_equal(sdata->u.mgd.tdls_peer, peer)) {
909 ret = -EBUSY; 1011 ret = -EBUSY;
910 goto exit; 1012 goto out_unlock;
911 } 1013 }
912 1014
913 /* 1015 /*
@@ -922,27 +1024,34 @@ ieee80211_tdls_mgmt_setup(struct wiphy *wiphy, struct net_device *dev,
922 if (!sta_info_get(sdata, peer)) { 1024 if (!sta_info_get(sdata, peer)) {
923 rcu_read_unlock(); 1025 rcu_read_unlock();
924 ret = -ENOLINK; 1026 ret = -ENOLINK;
925 goto exit; 1027 goto out_unlock;
926 } 1028 }
927 rcu_read_unlock(); 1029 rcu_read_unlock();
928 } 1030 }
929 1031
930 ieee80211_flush_queues(local, sdata, false); 1032 ieee80211_flush_queues(local, sdata, false);
1033 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
1034 mutex_unlock(&local->mtx);
931 1035
1036 /* we cannot take the mutex while preparing the setup packet */
932 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code, 1037 ret = ieee80211_tdls_prep_mgmt_packet(wiphy, dev, peer, action_code,
933 dialog_token, status_code, 1038 dialog_token, status_code,
934 peer_capability, initiator, 1039 peer_capability, initiator,
935 extra_ies, extra_ies_len, 0, 1040 extra_ies, extra_ies_len, 0,
936 NULL); 1041 NULL);
937 if (ret < 0) 1042 if (ret < 0) {
938 goto exit; 1043 mutex_lock(&local->mtx);
1044 eth_zero_addr(sdata->u.mgd.tdls_peer);
1045 mutex_unlock(&local->mtx);
1046 return ret;
1047 }
939 1048
940 memcpy(sdata->u.mgd.tdls_peer, peer, ETH_ALEN);
941 ieee80211_queue_delayed_work(&sdata->local->hw, 1049 ieee80211_queue_delayed_work(&sdata->local->hw,
942 &sdata->u.mgd.tdls_peer_del_work, 1050 &sdata->u.mgd.tdls_peer_del_work,
943 TDLS_PEER_SETUP_TIMEOUT); 1051 TDLS_PEER_SETUP_TIMEOUT);
1052 return 0;
944 1053
945exit: 1054out_unlock:
946 mutex_unlock(&local->mtx); 1055 mutex_unlock(&local->mtx);
947 return ret; 1056 return ret;
948} 1057}
diff --git a/net/mac80211/trace.c b/net/mac80211/trace.c
index 386e45d8a958..edfe0c170a1c 100644
--- a/net/mac80211/trace.c
+++ b/net/mac80211/trace.c
@@ -8,6 +8,7 @@
8#include "debug.h" 8#include "debug.h"
9#define CREATE_TRACE_POINTS 9#define CREATE_TRACE_POINTS
10#include "trace.h" 10#include "trace.h"
11#include "trace_msg.h"
11 12
12#ifdef CONFIG_MAC80211_MESSAGE_TRACING 13#ifdef CONFIG_MAC80211_MESSAGE_TRACING
13void __sdata_info(const char *fmt, ...) 14void __sdata_info(const char *fmt, ...)
diff --git a/net/mac80211/trace.h b/net/mac80211/trace.h
index 263a9561eb26..4c2e7690226a 100644
--- a/net/mac80211/trace.h
+++ b/net/mac80211/trace.h
@@ -1256,28 +1256,28 @@ TRACE_EVENT(drv_set_rekey_data,
1256 LOCAL_PR_ARG, VIF_PR_ARG) 1256 LOCAL_PR_ARG, VIF_PR_ARG)
1257); 1257);
1258 1258
1259TRACE_EVENT(drv_rssi_callback, 1259TRACE_EVENT(drv_event_callback,
1260 TP_PROTO(struct ieee80211_local *local, 1260 TP_PROTO(struct ieee80211_local *local,
1261 struct ieee80211_sub_if_data *sdata, 1261 struct ieee80211_sub_if_data *sdata,
1262 enum ieee80211_rssi_event rssi_event), 1262 const struct ieee80211_event *_event),
1263 1263
1264 TP_ARGS(local, sdata, rssi_event), 1264 TP_ARGS(local, sdata, _event),
1265 1265
1266 TP_STRUCT__entry( 1266 TP_STRUCT__entry(
1267 LOCAL_ENTRY 1267 LOCAL_ENTRY
1268 VIF_ENTRY 1268 VIF_ENTRY
1269 __field(u32, rssi_event) 1269 __field(u32, type)
1270 ), 1270 ),
1271 1271
1272 TP_fast_assign( 1272 TP_fast_assign(
1273 LOCAL_ASSIGN; 1273 LOCAL_ASSIGN;
1274 VIF_ASSIGN; 1274 VIF_ASSIGN;
1275 __entry->rssi_event = rssi_event; 1275 __entry->type = _event->type;
1276 ), 1276 ),
1277 1277
1278 TP_printk( 1278 TP_printk(
1279 LOCAL_PR_FMT VIF_PR_FMT " rssi_event:%d", 1279 LOCAL_PR_FMT VIF_PR_FMT " event:%d",
1280 LOCAL_PR_ARG, VIF_PR_ARG, __entry->rssi_event 1280 LOCAL_PR_ARG, VIF_PR_ARG, __entry->type
1281 ) 1281 )
1282); 1282);
1283 1283
@@ -2312,43 +2312,36 @@ TRACE_EVENT(drv_tdls_recv_channel_switch,
2312 ) 2312 )
2313); 2313);
2314 2314
2315#ifdef CONFIG_MAC80211_MESSAGE_TRACING 2315TRACE_EVENT(drv_wake_tx_queue,
2316#undef TRACE_SYSTEM 2316 TP_PROTO(struct ieee80211_local *local,
2317#define TRACE_SYSTEM mac80211_msg 2317 struct ieee80211_sub_if_data *sdata,
2318 2318 struct txq_info *txq),
2319#define MAX_MSG_LEN 100
2320
2321DECLARE_EVENT_CLASS(mac80211_msg_event,
2322 TP_PROTO(struct va_format *vaf),
2323 2319
2324 TP_ARGS(vaf), 2320 TP_ARGS(local, sdata, txq),
2325 2321
2326 TP_STRUCT__entry( 2322 TP_STRUCT__entry(
2327 __dynamic_array(char, msg, MAX_MSG_LEN) 2323 LOCAL_ENTRY
2324 VIF_ENTRY
2325 STA_ENTRY
2326 __field(u8, ac)
2327 __field(u8, tid)
2328 ), 2328 ),
2329 2329
2330 TP_fast_assign( 2330 TP_fast_assign(
2331 WARN_ON_ONCE(vsnprintf(__get_dynamic_array(msg), 2331 struct ieee80211_sta *sta = txq->txq.sta;
2332 MAX_MSG_LEN, vaf->fmt,
2333 *vaf->va) >= MAX_MSG_LEN);
2334 ),
2335 2332
2336 TP_printk("%s", __get_str(msg)) 2333 LOCAL_ASSIGN;
2337); 2334 VIF_ASSIGN;
2335 STA_ASSIGN;
2336 __entry->ac = txq->txq.ac;
2337 __entry->tid = txq->txq.tid;
2338 ),
2338 2339
2339DEFINE_EVENT(mac80211_msg_event, mac80211_info, 2340 TP_printk(
2340 TP_PROTO(struct va_format *vaf), 2341 LOCAL_PR_FMT VIF_PR_FMT STA_PR_FMT " ac:%d tid:%d",
2341 TP_ARGS(vaf) 2342 LOCAL_PR_ARG, VIF_PR_ARG, STA_PR_ARG, __entry->ac, __entry->tid
2342); 2343 )
2343DEFINE_EVENT(mac80211_msg_event, mac80211_dbg,
2344 TP_PROTO(struct va_format *vaf),
2345 TP_ARGS(vaf)
2346);
2347DEFINE_EVENT(mac80211_msg_event, mac80211_err,
2348 TP_PROTO(struct va_format *vaf),
2349 TP_ARGS(vaf)
2350); 2344);
2351#endif
2352 2345
2353#endif /* !__MAC80211_DRIVER_TRACE || TRACE_HEADER_MULTI_READ */ 2346#endif /* !__MAC80211_DRIVER_TRACE || TRACE_HEADER_MULTI_READ */
2354 2347
diff --git a/net/mac80211/trace_msg.h b/net/mac80211/trace_msg.h
new file mode 100644
index 000000000000..768f7c22a190
--- /dev/null
+++ b/net/mac80211/trace_msg.h
@@ -0,0 +1,53 @@
1#ifdef CONFIG_MAC80211_MESSAGE_TRACING
2
3#if !defined(__MAC80211_MSG_DRIVER_TRACE) || defined(TRACE_HEADER_MULTI_READ)
4#define __MAC80211_MSG_DRIVER_TRACE
5
6#include <linux/tracepoint.h>
7#include <net/mac80211.h>
8#include "ieee80211_i.h"
9
10#undef TRACE_SYSTEM
11#define TRACE_SYSTEM mac80211_msg
12
13#define MAX_MSG_LEN 100
14
15DECLARE_EVENT_CLASS(mac80211_msg_event,
16 TP_PROTO(struct va_format *vaf),
17
18 TP_ARGS(vaf),
19
20 TP_STRUCT__entry(
21 __dynamic_array(char, msg, MAX_MSG_LEN)
22 ),
23
24 TP_fast_assign(
25 WARN_ON_ONCE(vsnprintf(__get_dynamic_array(msg),
26 MAX_MSG_LEN, vaf->fmt,
27 *vaf->va) >= MAX_MSG_LEN);
28 ),
29
30 TP_printk("%s", __get_str(msg))
31);
32
33DEFINE_EVENT(mac80211_msg_event, mac80211_info,
34 TP_PROTO(struct va_format *vaf),
35 TP_ARGS(vaf)
36);
37DEFINE_EVENT(mac80211_msg_event, mac80211_dbg,
38 TP_PROTO(struct va_format *vaf),
39 TP_ARGS(vaf)
40);
41DEFINE_EVENT(mac80211_msg_event, mac80211_err,
42 TP_PROTO(struct va_format *vaf),
43 TP_ARGS(vaf)
44);
45#endif /* !__MAC80211_MSG_DRIVER_TRACE || TRACE_HEADER_MULTI_READ */
46
47#undef TRACE_INCLUDE_PATH
48#define TRACE_INCLUDE_PATH .
49#undef TRACE_INCLUDE_FILE
50#define TRACE_INCLUDE_FILE trace_msg
51#include <trace/define_trace.h>
52
53#endif
diff --git a/net/mac80211/tx.c b/net/mac80211/tx.c
index 07bd8db00af8..667111ee6a20 100644
--- a/net/mac80211/tx.c
+++ b/net/mac80211/tx.c
@@ -20,7 +20,6 @@
20#include <linux/bitmap.h> 20#include <linux/bitmap.h>
21#include <linux/rcupdate.h> 21#include <linux/rcupdate.h>
22#include <linux/export.h> 22#include <linux/export.h>
23#include <linux/time.h>
24#include <net/net_namespace.h> 23#include <net/net_namespace.h>
25#include <net/ieee80211_radiotap.h> 24#include <net/ieee80211_radiotap.h>
26#include <net/cfg80211.h> 25#include <net/cfg80211.h>
@@ -595,23 +594,8 @@ ieee80211_tx_h_select_key(struct ieee80211_tx_data *tx)
595 else if (!is_multicast_ether_addr(hdr->addr1) && 594 else if (!is_multicast_ether_addr(hdr->addr1) &&
596 (key = rcu_dereference(tx->sdata->default_unicast_key))) 595 (key = rcu_dereference(tx->sdata->default_unicast_key)))
597 tx->key = key; 596 tx->key = key;
598 else if (info->flags & IEEE80211_TX_CTL_INJECTED) 597 else
599 tx->key = NULL;
600 else if (!tx->sdata->drop_unencrypted)
601 tx->key = NULL;
602 else if (tx->skb->protocol == tx->sdata->control_port_protocol)
603 tx->key = NULL;
604 else if (ieee80211_is_robust_mgmt_frame(tx->skb) &&
605 !(ieee80211_is_action(hdr->frame_control) &&
606 tx->sta && test_sta_flag(tx->sta, WLAN_STA_MFP)))
607 tx->key = NULL;
608 else if (ieee80211_is_mgmt(hdr->frame_control) &&
609 !ieee80211_is_robust_mgmt_frame(tx->skb))
610 tx->key = NULL; 598 tx->key = NULL;
611 else {
612 I802_DEBUG_INC(tx->local->tx_handlers_drop_unencrypted);
613 return TX_DROP;
614 }
615 599
616 if (tx->key) { 600 if (tx->key) {
617 bool skip_hw = false; 601 bool skip_hw = false;
@@ -783,12 +767,22 @@ ieee80211_tx_h_rate_ctrl(struct ieee80211_tx_data *tx)
783 return TX_CONTINUE; 767 return TX_CONTINUE;
784} 768}
785 769
770static __le16 ieee80211_tx_next_seq(struct sta_info *sta, int tid)
771{
772 u16 *seq = &sta->tid_seq[tid];
773 __le16 ret = cpu_to_le16(*seq);
774
775 /* Increase the sequence number. */
776 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
777
778 return ret;
779}
780
786static ieee80211_tx_result debug_noinline 781static ieee80211_tx_result debug_noinline
787ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx) 782ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
788{ 783{
789 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb); 784 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx->skb);
790 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data; 785 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx->skb->data;
791 u16 *seq;
792 u8 *qc; 786 u8 *qc;
793 int tid; 787 int tid;
794 788
@@ -839,13 +833,10 @@ ieee80211_tx_h_sequence(struct ieee80211_tx_data *tx)
839 833
840 qc = ieee80211_get_qos_ctl(hdr); 834 qc = ieee80211_get_qos_ctl(hdr);
841 tid = *qc & IEEE80211_QOS_CTL_TID_MASK; 835 tid = *qc & IEEE80211_QOS_CTL_TID_MASK;
842 seq = &tx->sta->tid_seq[tid];
843 tx->sta->tx_msdu[tid]++; 836 tx->sta->tx_msdu[tid]++;
844 837
845 hdr->seq_ctrl = cpu_to_le16(*seq); 838 if (!tx->sta->sta.txq[0])
846 839 hdr->seq_ctrl = ieee80211_tx_next_seq(tx->sta, tid);
847 /* Increase the sequence number. */
848 *seq = (*seq + 0x10) & IEEE80211_SCTL_SEQ;
849 840
850 return TX_CONTINUE; 841 return TX_CONTINUE;
851} 842}
@@ -1086,7 +1077,7 @@ static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1086 * nothing -- this aggregation session is being started 1077 * nothing -- this aggregation session is being started
1087 * but that might still fail with the driver 1078 * but that might still fail with the driver
1088 */ 1079 */
1089 } else { 1080 } else if (!tx->sta->sta.txq[tid]) {
1090 spin_lock(&tx->sta->lock); 1081 spin_lock(&tx->sta->lock);
1091 /* 1082 /*
1092 * Need to re-check now, because we may get here 1083 * Need to re-check now, because we may get here
@@ -1137,11 +1128,13 @@ static bool ieee80211_tx_prep_agg(struct ieee80211_tx_data *tx,
1137 1128
1138/* 1129/*
1139 * initialises @tx 1130 * initialises @tx
1131 * pass %NULL for the station if unknown, a valid pointer if known
1132 * or an ERR_PTR() if the station is known not to exist
1140 */ 1133 */
1141static ieee80211_tx_result 1134static ieee80211_tx_result
1142ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata, 1135ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1143 struct ieee80211_tx_data *tx, 1136 struct ieee80211_tx_data *tx,
1144 struct sk_buff *skb) 1137 struct sta_info *sta, struct sk_buff *skb)
1145{ 1138{
1146 struct ieee80211_local *local = sdata->local; 1139 struct ieee80211_local *local = sdata->local;
1147 struct ieee80211_hdr *hdr; 1140 struct ieee80211_hdr *hdr;
@@ -1164,17 +1157,22 @@ ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1164 1157
1165 hdr = (struct ieee80211_hdr *) skb->data; 1158 hdr = (struct ieee80211_hdr *) skb->data;
1166 1159
1167 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) { 1160 if (likely(sta)) {
1168 tx->sta = rcu_dereference(sdata->u.vlan.sta); 1161 if (!IS_ERR(sta))
1169 if (!tx->sta && sdata->dev->ieee80211_ptr->use_4addr) 1162 tx->sta = sta;
1170 return TX_DROP; 1163 } else {
1171 } else if (info->flags & (IEEE80211_TX_CTL_INJECTED | 1164 if (sdata->vif.type == NL80211_IFTYPE_AP_VLAN) {
1172 IEEE80211_TX_INTFL_NL80211_FRAME_TX) || 1165 tx->sta = rcu_dereference(sdata->u.vlan.sta);
1173 tx->sdata->control_port_protocol == tx->skb->protocol) { 1166 if (!tx->sta && sdata->wdev.use_4addr)
1174 tx->sta = sta_info_get_bss(sdata, hdr->addr1); 1167 return TX_DROP;
1168 } else if (info->flags & (IEEE80211_TX_INTFL_NL80211_FRAME_TX |
1169 IEEE80211_TX_CTL_INJECTED) ||
1170 tx->sdata->control_port_protocol == tx->skb->protocol) {
1171 tx->sta = sta_info_get_bss(sdata, hdr->addr1);
1172 }
1173 if (!tx->sta && !is_multicast_ether_addr(hdr->addr1))
1174 tx->sta = sta_info_get(sdata, hdr->addr1);
1175 } 1175 }
1176 if (!tx->sta)
1177 tx->sta = sta_info_get(sdata, hdr->addr1);
1178 1176
1179 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) && 1177 if (tx->sta && ieee80211_is_data_qos(hdr->frame_control) &&
1180 !ieee80211_is_qos_nullfunc(hdr->frame_control) && 1178 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
@@ -1220,13 +1218,102 @@ ieee80211_tx_prepare(struct ieee80211_sub_if_data *sdata,
1220 return TX_CONTINUE; 1218 return TX_CONTINUE;
1221} 1219}
1222 1220
1221static void ieee80211_drv_tx(struct ieee80211_local *local,
1222 struct ieee80211_vif *vif,
1223 struct ieee80211_sta *pubsta,
1224 struct sk_buff *skb)
1225{
1226 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
1227 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1228 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1229 struct ieee80211_tx_control control = {
1230 .sta = pubsta,
1231 };
1232 struct ieee80211_txq *txq = NULL;
1233 struct txq_info *txqi;
1234 u8 ac;
1235
1236 if (info->control.flags & IEEE80211_TX_CTRL_PS_RESPONSE)
1237 goto tx_normal;
1238
1239 if (!ieee80211_is_data(hdr->frame_control))
1240 goto tx_normal;
1241
1242 if (pubsta) {
1243 u8 tid = skb->priority & IEEE80211_QOS_CTL_TID_MASK;
1244
1245 txq = pubsta->txq[tid];
1246 } else if (vif) {
1247 txq = vif->txq;
1248 }
1249
1250 if (!txq)
1251 goto tx_normal;
1252
1253 ac = txq->ac;
1254 txqi = to_txq_info(txq);
1255 atomic_inc(&sdata->txqs_len[ac]);
1256 if (atomic_read(&sdata->txqs_len[ac]) >= local->hw.txq_ac_max_pending)
1257 netif_stop_subqueue(sdata->dev, ac);
1258
1259 skb_queue_tail(&txqi->queue, skb);
1260 drv_wake_tx_queue(local, txqi);
1261
1262 return;
1263
1264tx_normal:
1265 drv_tx(local, &control, skb);
1266}
1267
1268struct sk_buff *ieee80211_tx_dequeue(struct ieee80211_hw *hw,
1269 struct ieee80211_txq *txq)
1270{
1271 struct ieee80211_local *local = hw_to_local(hw);
1272 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
1273 struct txq_info *txqi = container_of(txq, struct txq_info, txq);
1274 struct ieee80211_hdr *hdr;
1275 struct sk_buff *skb = NULL;
1276 u8 ac = txq->ac;
1277
1278 spin_lock_bh(&txqi->queue.lock);
1279
1280 if (test_bit(IEEE80211_TXQ_STOP, &txqi->flags))
1281 goto out;
1282
1283 skb = __skb_dequeue(&txqi->queue);
1284 if (!skb)
1285 goto out;
1286
1287 atomic_dec(&sdata->txqs_len[ac]);
1288 if (__netif_subqueue_stopped(sdata->dev, ac))
1289 ieee80211_propagate_queue_wake(local, sdata->vif.hw_queue[ac]);
1290
1291 hdr = (struct ieee80211_hdr *)skb->data;
1292 if (txq->sta && ieee80211_is_data_qos(hdr->frame_control)) {
1293 struct sta_info *sta = container_of(txq->sta, struct sta_info,
1294 sta);
1295 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1296
1297 hdr->seq_ctrl = ieee80211_tx_next_seq(sta, txq->tid);
1298 if (test_bit(IEEE80211_TXQ_AMPDU, &txqi->flags))
1299 info->flags |= IEEE80211_TX_CTL_AMPDU;
1300 else
1301 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1302 }
1303
1304out:
1305 spin_unlock_bh(&txqi->queue.lock);
1306
1307 return skb;
1308}
1309EXPORT_SYMBOL(ieee80211_tx_dequeue);
1310
1223static bool ieee80211_tx_frags(struct ieee80211_local *local, 1311static bool ieee80211_tx_frags(struct ieee80211_local *local,
1224 struct ieee80211_vif *vif, 1312 struct ieee80211_vif *vif,
1225 struct ieee80211_sta *sta, 1313 struct ieee80211_sta *sta,
1226 struct sk_buff_head *skbs, 1314 struct sk_buff_head *skbs,
1227 bool txpending) 1315 bool txpending)
1228{ 1316{
1229 struct ieee80211_tx_control control;
1230 struct sk_buff *skb, *tmp; 1317 struct sk_buff *skb, *tmp;
1231 unsigned long flags; 1318 unsigned long flags;
1232 1319
@@ -1284,10 +1371,9 @@ static bool ieee80211_tx_frags(struct ieee80211_local *local,
1284 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags); 1371 spin_unlock_irqrestore(&local->queue_stop_reason_lock, flags);
1285 1372
1286 info->control.vif = vif; 1373 info->control.vif = vif;
1287 control.sta = sta;
1288 1374
1289 __skb_unlink(skb, skbs); 1375 __skb_unlink(skb, skbs);
1290 drv_tx(local, &control, skb); 1376 ieee80211_drv_tx(local, vif, sta, skb);
1291 } 1377 }
1292 1378
1293 return true; 1379 return true;
@@ -1422,8 +1508,9 @@ bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1422 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif); 1508 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
1423 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1509 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1424 struct ieee80211_tx_data tx; 1510 struct ieee80211_tx_data tx;
1511 struct sk_buff *skb2;
1425 1512
1426 if (ieee80211_tx_prepare(sdata, &tx, skb) == TX_DROP) 1513 if (ieee80211_tx_prepare(sdata, &tx, NULL, skb) == TX_DROP)
1427 return false; 1514 return false;
1428 1515
1429 info->band = band; 1516 info->band = band;
@@ -1440,6 +1527,14 @@ bool ieee80211_tx_prepare_skb(struct ieee80211_hw *hw,
1440 *sta = NULL; 1527 *sta = NULL;
1441 } 1528 }
1442 1529
1530 /* this function isn't suitable for fragmented data frames */
1531 skb2 = __skb_dequeue(&tx.skbs);
1532 if (WARN_ON(skb2 != skb || !skb_queue_empty(&tx.skbs))) {
1533 ieee80211_free_txskb(hw, skb2);
1534 ieee80211_purge_tx_queue(hw, &tx.skbs);
1535 return false;
1536 }
1537
1443 return true; 1538 return true;
1444} 1539}
1445EXPORT_SYMBOL(ieee80211_tx_prepare_skb); 1540EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
@@ -1448,7 +1543,8 @@ EXPORT_SYMBOL(ieee80211_tx_prepare_skb);
1448 * Returns false if the frame couldn't be transmitted but was queued instead. 1543 * Returns false if the frame couldn't be transmitted but was queued instead.
1449 */ 1544 */
1450static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata, 1545static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1451 struct sk_buff *skb, bool txpending) 1546 struct sta_info *sta, struct sk_buff *skb,
1547 bool txpending)
1452{ 1548{
1453 struct ieee80211_local *local = sdata->local; 1549 struct ieee80211_local *local = sdata->local;
1454 struct ieee80211_tx_data tx; 1550 struct ieee80211_tx_data tx;
@@ -1464,7 +1560,7 @@ static bool ieee80211_tx(struct ieee80211_sub_if_data *sdata,
1464 1560
1465 /* initialises tx */ 1561 /* initialises tx */
1466 led_len = skb->len; 1562 led_len = skb->len;
1467 res_prepare = ieee80211_tx_prepare(sdata, &tx, skb); 1563 res_prepare = ieee80211_tx_prepare(sdata, &tx, sta, skb);
1468 1564
1469 if (unlikely(res_prepare == TX_DROP)) { 1565 if (unlikely(res_prepare == TX_DROP)) {
1470 ieee80211_free_txskb(&local->hw, skb); 1566 ieee80211_free_txskb(&local->hw, skb);
@@ -1520,7 +1616,8 @@ static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
1520 return 0; 1616 return 0;
1521} 1617}
1522 1618
1523void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb) 1619void ieee80211_xmit(struct ieee80211_sub_if_data *sdata,
1620 struct sta_info *sta, struct sk_buff *skb)
1524{ 1621{
1525 struct ieee80211_local *local = sdata->local; 1622 struct ieee80211_local *local = sdata->local;
1526 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 1623 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
@@ -1555,7 +1652,7 @@ void ieee80211_xmit(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
1555 } 1652 }
1556 1653
1557 ieee80211_set_qos_hdr(sdata, skb); 1654 ieee80211_set_qos_hdr(sdata, skb);
1558 ieee80211_tx(sdata, skb, false); 1655 ieee80211_tx(sdata, sta, skb, false);
1559} 1656}
1560 1657
1561static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb) 1658static bool ieee80211_parse_tx_radiotap(struct sk_buff *skb)
@@ -1776,7 +1873,7 @@ netdev_tx_t ieee80211_monitor_start_xmit(struct sk_buff *skb,
1776 goto fail_rcu; 1873 goto fail_rcu;
1777 1874
1778 info->band = chandef->chan->band; 1875 info->band = chandef->chan->band;
1779 ieee80211_xmit(sdata, skb); 1876 ieee80211_xmit(sdata, NULL, skb);
1780 rcu_read_unlock(); 1877 rcu_read_unlock();
1781 1878
1782 return NETDEV_TX_OK; 1879 return NETDEV_TX_OK;
@@ -1788,21 +1885,89 @@ fail:
1788 return NETDEV_TX_OK; /* meaning, we dealt with the skb */ 1885 return NETDEV_TX_OK; /* meaning, we dealt with the skb */
1789} 1886}
1790 1887
1791/* 1888static inline bool ieee80211_is_tdls_setup(struct sk_buff *skb)
1792 * Measure Tx frame arrival time for Tx latency statistics calculation
1793 * A single Tx frame latency should be measured from when it is entering the
1794 * Kernel until we receive Tx complete confirmation indication and the skb is
1795 * freed.
1796 */
1797static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
1798 struct sk_buff *skb)
1799{ 1889{
1800 struct ieee80211_tx_latency_bin_ranges *tx_latency; 1890 u16 ethertype = (skb->data[12] << 8) | skb->data[13];
1801 1891
1802 tx_latency = rcu_dereference(local->tx_latency); 1892 return ethertype == ETH_P_TDLS &&
1803 if (!tx_latency) 1893 skb->len > 14 &&
1804 return; 1894 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
1805 skb->tstamp = ktime_get(); 1895}
1896
1897static int ieee80211_lookup_ra_sta(struct ieee80211_sub_if_data *sdata,
1898 struct sk_buff *skb,
1899 struct sta_info **sta_out)
1900{
1901 struct sta_info *sta;
1902
1903 switch (sdata->vif.type) {
1904 case NL80211_IFTYPE_AP_VLAN:
1905 sta = rcu_dereference(sdata->u.vlan.sta);
1906 if (sta) {
1907 *sta_out = sta;
1908 return 0;
1909 } else if (sdata->wdev.use_4addr) {
1910 return -ENOLINK;
1911 }
1912 /* fall through */
1913 case NL80211_IFTYPE_AP:
1914 case NL80211_IFTYPE_OCB:
1915 case NL80211_IFTYPE_ADHOC:
1916 if (is_multicast_ether_addr(skb->data)) {
1917 *sta_out = ERR_PTR(-ENOENT);
1918 return 0;
1919 }
1920 sta = sta_info_get_bss(sdata, skb->data);
1921 break;
1922 case NL80211_IFTYPE_WDS:
1923 sta = sta_info_get(sdata, sdata->u.wds.remote_addr);
1924 break;
1925#ifdef CONFIG_MAC80211_MESH
1926 case NL80211_IFTYPE_MESH_POINT:
1927 /* determined much later */
1928 *sta_out = NULL;
1929 return 0;
1930#endif
1931 case NL80211_IFTYPE_STATION:
1932 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) {
1933 sta = sta_info_get(sdata, skb->data);
1934 if (sta) {
1935 bool tdls_peer, tdls_auth;
1936
1937 tdls_peer = test_sta_flag(sta,
1938 WLAN_STA_TDLS_PEER);
1939 tdls_auth = test_sta_flag(sta,
1940 WLAN_STA_TDLS_PEER_AUTH);
1941
1942 if (tdls_peer && tdls_auth) {
1943 *sta_out = sta;
1944 return 0;
1945 }
1946
1947 /*
1948 * TDLS link during setup - throw out frames to
1949 * peer. Allow TDLS-setup frames to unauthorized
1950 * peers for the special case of a link teardown
1951 * after a TDLS sta is removed due to being
1952 * unreachable.
1953 */
1954 if (tdls_peer && !tdls_auth &&
1955 !ieee80211_is_tdls_setup(skb))
1956 return -EINVAL;
1957 }
1958
1959 }
1960
1961 sta = sta_info_get(sdata, sdata->u.mgd.bssid);
1962 if (!sta)
1963 return -ENOLINK;
1964 break;
1965 default:
1966 return -EINVAL;
1967 }
1968
1969 *sta_out = sta ?: ERR_PTR(-ENOENT);
1970 return 0;
1806} 1971}
1807 1972
1808/** 1973/**
@@ -1824,7 +1989,8 @@ static void ieee80211_tx_latency_start_msrmnt(struct ieee80211_local *local,
1824 * Returns: the (possibly reallocated) skb or an ERR_PTR() code 1989 * Returns: the (possibly reallocated) skb or an ERR_PTR() code
1825 */ 1990 */
1826static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata, 1991static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1827 struct sk_buff *skb, u32 info_flags) 1992 struct sk_buff *skb, u32 info_flags,
1993 struct sta_info *sta)
1828{ 1994{
1829 struct ieee80211_local *local = sdata->local; 1995 struct ieee80211_local *local = sdata->local;
1830 struct ieee80211_tx_info *info; 1996 struct ieee80211_tx_info *info;
@@ -1837,9 +2003,8 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1837 const u8 *encaps_data; 2003 const u8 *encaps_data;
1838 int encaps_len, skip_header_bytes; 2004 int encaps_len, skip_header_bytes;
1839 int nh_pos, h_pos; 2005 int nh_pos, h_pos;
1840 struct sta_info *sta = NULL; 2006 bool wme_sta = false, authorized = false;
1841 bool wme_sta = false, authorized = false, tdls_auth = false; 2007 bool tdls_peer;
1842 bool tdls_peer = false, tdls_setup_frame = false;
1843 bool multicast; 2008 bool multicast;
1844 u16 info_id = 0; 2009 u16 info_id = 0;
1845 struct ieee80211_chanctx_conf *chanctx_conf; 2010 struct ieee80211_chanctx_conf *chanctx_conf;
@@ -1847,6 +2012,9 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1847 enum ieee80211_band band; 2012 enum ieee80211_band band;
1848 int ret; 2013 int ret;
1849 2014
2015 if (IS_ERR(sta))
2016 sta = NULL;
2017
1850 /* convert Ethernet header to proper 802.11 header (based on 2018 /* convert Ethernet header to proper 802.11 header (based on
1851 * operation mode) */ 2019 * operation mode) */
1852 ethertype = (skb->data[12] << 8) | skb->data[13]; 2020 ethertype = (skb->data[12] << 8) | skb->data[13];
@@ -1854,8 +2022,7 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1854 2022
1855 switch (sdata->vif.type) { 2023 switch (sdata->vif.type) {
1856 case NL80211_IFTYPE_AP_VLAN: 2024 case NL80211_IFTYPE_AP_VLAN:
1857 sta = rcu_dereference(sdata->u.vlan.sta); 2025 if (sdata->wdev.use_4addr) {
1858 if (sta) {
1859 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS); 2026 fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
1860 /* RA TA DA SA */ 2027 /* RA TA DA SA */
1861 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN); 2028 memcpy(hdr.addr1, sta->sta.addr, ETH_ALEN);
@@ -1874,7 +2041,7 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1874 goto free; 2041 goto free;
1875 } 2042 }
1876 band = chanctx_conf->def.chan->band; 2043 band = chanctx_conf->def.chan->band;
1877 if (sta) 2044 if (sdata->wdev.use_4addr)
1878 break; 2045 break;
1879 /* fall through */ 2046 /* fall through */
1880 case NL80211_IFTYPE_AP: 2047 case NL80211_IFTYPE_AP:
@@ -1978,38 +2145,10 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
1978 break; 2145 break;
1979#endif 2146#endif
1980 case NL80211_IFTYPE_STATION: 2147 case NL80211_IFTYPE_STATION:
1981 if (sdata->wdev.wiphy->flags & WIPHY_FLAG_SUPPORTS_TDLS) { 2148 /* we already did checks when looking up the RA STA */
1982 sta = sta_info_get(sdata, skb->data); 2149 tdls_peer = test_sta_flag(sta, WLAN_STA_TDLS_PEER);
1983 if (sta) {
1984 authorized = test_sta_flag(sta,
1985 WLAN_STA_AUTHORIZED);
1986 wme_sta = sta->sta.wme;
1987 tdls_peer = test_sta_flag(sta,
1988 WLAN_STA_TDLS_PEER);
1989 tdls_auth = test_sta_flag(sta,
1990 WLAN_STA_TDLS_PEER_AUTH);
1991 }
1992
1993 if (tdls_peer)
1994 tdls_setup_frame =
1995 ethertype == ETH_P_TDLS &&
1996 skb->len > 14 &&
1997 skb->data[14] == WLAN_TDLS_SNAP_RFTYPE;
1998 }
1999
2000 /*
2001 * TDLS link during setup - throw out frames to peer. We allow
2002 * TDLS-setup frames to unauthorized peers for the special case
2003 * of a link teardown after a TDLS sta is removed due to being
2004 * unreachable.
2005 */
2006 if (tdls_peer && !tdls_auth && !tdls_setup_frame) {
2007 ret = -EINVAL;
2008 goto free;
2009 }
2010 2150
2011 /* send direct packets to authorized TDLS peers */ 2151 if (tdls_peer) {
2012 if (tdls_peer && tdls_auth) {
2013 /* DA SA BSSID */ 2152 /* DA SA BSSID */
2014 memcpy(hdr.addr1, skb->data, ETH_ALEN); 2153 memcpy(hdr.addr1, skb->data, ETH_ALEN);
2015 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN); 2154 memcpy(hdr.addr2, skb->data + ETH_ALEN, ETH_ALEN);
@@ -2071,26 +2210,19 @@ static struct sk_buff *ieee80211_build_hdr(struct ieee80211_sub_if_data *sdata,
2071 goto free; 2210 goto free;
2072 } 2211 }
2073 2212
2074 /*
2075 * There's no need to try to look up the destination
2076 * if it is a multicast address (which can only happen
2077 * in AP mode)
2078 */
2079 multicast = is_multicast_ether_addr(hdr.addr1); 2213 multicast = is_multicast_ether_addr(hdr.addr1);
2080 if (!multicast) {
2081 sta = sta_info_get(sdata, hdr.addr1);
2082 if (sta) {
2083 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2084 wme_sta = sta->sta.wme;
2085 }
2086 }
2087 2214
2088 /* For mesh, the use of the QoS header is mandatory */ 2215 /* sta is always NULL for mesh */
2089 if (ieee80211_vif_is_mesh(&sdata->vif)) 2216 if (sta) {
2217 authorized = test_sta_flag(sta, WLAN_STA_AUTHORIZED);
2218 wme_sta = sta->sta.wme;
2219 } else if (ieee80211_vif_is_mesh(&sdata->vif)) {
2220 /* For mesh, the use of the QoS header is mandatory */
2090 wme_sta = true; 2221 wme_sta = true;
2222 }
2091 2223
2092 /* receiver and we are QoS enabled, use a QoS type frame */ 2224 /* receiver does QoS (which also means we do) use it */
2093 if (wme_sta && local->hw.queues >= IEEE80211_NUM_ACS) { 2225 if (wme_sta) {
2094 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA); 2226 fc |= cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2095 hdrlen += 2; 2227 hdrlen += 2;
2096 } 2228 }
@@ -2260,7 +2392,7 @@ void __ieee80211_subif_start_xmit(struct sk_buff *skb,
2260 u32 info_flags) 2392 u32 info_flags)
2261{ 2393{
2262 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); 2394 struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
2263 struct ieee80211_local *local = sdata->local; 2395 struct sta_info *sta;
2264 2396
2265 if (unlikely(skb->len < ETH_HLEN)) { 2397 if (unlikely(skb->len < ETH_HLEN)) {
2266 kfree_skb(skb); 2398 kfree_skb(skb);
@@ -2269,10 +2401,12 @@ void __ieee80211_subif_start_xmit(struct sk_buff *skb,
2269 2401
2270 rcu_read_lock(); 2402 rcu_read_lock();
2271 2403
2272 /* Measure frame arrival for Tx latency statistics calculation */ 2404 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
2273 ieee80211_tx_latency_start_msrmnt(local, skb); 2405 kfree_skb(skb);
2406 goto out;
2407 }
2274 2408
2275 skb = ieee80211_build_hdr(sdata, skb, info_flags); 2409 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
2276 if (IS_ERR(skb)) 2410 if (IS_ERR(skb))
2277 goto out; 2411 goto out;
2278 2412
@@ -2280,7 +2414,7 @@ void __ieee80211_subif_start_xmit(struct sk_buff *skb,
2280 dev->stats.tx_bytes += skb->len; 2414 dev->stats.tx_bytes += skb->len;
2281 dev->trans_start = jiffies; 2415 dev->trans_start = jiffies;
2282 2416
2283 ieee80211_xmit(sdata, skb); 2417 ieee80211_xmit(sdata, sta, skb);
2284 out: 2418 out:
2285 rcu_read_unlock(); 2419 rcu_read_unlock();
2286} 2420}
@@ -2308,10 +2442,17 @@ ieee80211_build_data_template(struct ieee80211_sub_if_data *sdata,
2308 .local = sdata->local, 2442 .local = sdata->local,
2309 .sdata = sdata, 2443 .sdata = sdata,
2310 }; 2444 };
2445 struct sta_info *sta;
2311 2446
2312 rcu_read_lock(); 2447 rcu_read_lock();
2313 2448
2314 skb = ieee80211_build_hdr(sdata, skb, info_flags); 2449 if (ieee80211_lookup_ra_sta(sdata, skb, &sta)) {
2450 kfree_skb(skb);
2451 skb = ERR_PTR(-EINVAL);
2452 goto out;
2453 }
2454
2455 skb = ieee80211_build_hdr(sdata, skb, info_flags, sta);
2315 if (IS_ERR(skb)) 2456 if (IS_ERR(skb))
2316 goto out; 2457 goto out;
2317 2458
@@ -2369,7 +2510,7 @@ static bool ieee80211_tx_pending_skb(struct ieee80211_local *local,
2369 return true; 2510 return true;
2370 } 2511 }
2371 info->band = chanctx_conf->def.chan->band; 2512 info->band = chanctx_conf->def.chan->band;
2372 result = ieee80211_tx(sdata, skb, true); 2513 result = ieee80211_tx(sdata, NULL, skb, true);
2373 } else { 2514 } else {
2374 struct sk_buff_head skbs; 2515 struct sk_buff_head skbs;
2375 2516
@@ -3107,7 +3248,7 @@ ieee80211_get_buffered_bc(struct ieee80211_hw *hw,
3107 3248
3108 if (sdata->vif.type == NL80211_IFTYPE_AP) 3249 if (sdata->vif.type == NL80211_IFTYPE_AP)
3109 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev); 3250 sdata = IEEE80211_DEV_TO_SUB_IF(skb->dev);
3110 if (!ieee80211_tx_prepare(sdata, &tx, skb)) 3251 if (!ieee80211_tx_prepare(sdata, &tx, NULL, skb))
3111 break; 3252 break;
3112 dev_kfree_skb_any(skb); 3253 dev_kfree_skb_any(skb);
3113 } 3254 }
@@ -3239,6 +3380,6 @@ void __ieee80211_tx_skb_tid_band(struct ieee80211_sub_if_data *sdata,
3239 */ 3380 */
3240 local_bh_disable(); 3381 local_bh_disable();
3241 IEEE80211_SKB_CB(skb)->band = band; 3382 IEEE80211_SKB_CB(skb)->band = band;
3242 ieee80211_xmit(sdata, skb); 3383 ieee80211_xmit(sdata, NULL, skb);
3243 local_bh_enable(); 3384 local_bh_enable();
3244} 3385}
diff --git a/net/mac80211/util.c b/net/mac80211/util.c
index 747bdcf72e92..79412f16b61d 100644
--- a/net/mac80211/util.c
+++ b/net/mac80211/util.c
@@ -308,6 +308,11 @@ void ieee80211_propagate_queue_wake(struct ieee80211_local *local, int queue)
308 for (ac = 0; ac < n_acs; ac++) { 308 for (ac = 0; ac < n_acs; ac++) {
309 int ac_queue = sdata->vif.hw_queue[ac]; 309 int ac_queue = sdata->vif.hw_queue[ac];
310 310
311 if (local->ops->wake_tx_queue &&
312 (atomic_read(&sdata->txqs_len[ac]) >
313 local->hw.txq_ac_max_pending))
314 continue;
315
311 if (ac_queue == queue || 316 if (ac_queue == queue ||
312 (sdata->vif.cab_queue == queue && 317 (sdata->vif.cab_queue == queue &&
313 local->queue_stop_reasons[ac_queue] == 0 && 318 local->queue_stop_reasons[ac_queue] == 0 &&
@@ -625,13 +630,14 @@ void ieee80211_wake_vif_queues(struct ieee80211_local *local,
625 reason, true); 630 reason, true);
626} 631}
627 632
628static void __iterate_active_interfaces(struct ieee80211_local *local, 633static void __iterate_interfaces(struct ieee80211_local *local,
629 u32 iter_flags, 634 u32 iter_flags,
630 void (*iterator)(void *data, u8 *mac, 635 void (*iterator)(void *data, u8 *mac,
631 struct ieee80211_vif *vif), 636 struct ieee80211_vif *vif),
632 void *data) 637 void *data)
633{ 638{
634 struct ieee80211_sub_if_data *sdata; 639 struct ieee80211_sub_if_data *sdata;
640 bool active_only = iter_flags & IEEE80211_IFACE_ITER_ACTIVE;
635 641
636 list_for_each_entry_rcu(sdata, &local->interfaces, list) { 642 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
637 switch (sdata->vif.type) { 643 switch (sdata->vif.type) {
@@ -645,9 +651,9 @@ static void __iterate_active_interfaces(struct ieee80211_local *local,
645 break; 651 break;
646 } 652 }
647 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) && 653 if (!(iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL) &&
648 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 654 active_only && !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
649 continue; 655 continue;
650 if (ieee80211_sdata_running(sdata)) 656 if (ieee80211_sdata_running(sdata) || !active_only)
651 iterator(data, sdata->vif.addr, 657 iterator(data, sdata->vif.addr,
652 &sdata->vif); 658 &sdata->vif);
653 } 659 }
@@ -656,12 +662,12 @@ static void __iterate_active_interfaces(struct ieee80211_local *local,
656 lockdep_is_held(&local->iflist_mtx) || 662 lockdep_is_held(&local->iflist_mtx) ||
657 lockdep_rtnl_is_held()); 663 lockdep_rtnl_is_held());
658 if (sdata && 664 if (sdata &&
659 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || 665 (iter_flags & IEEE80211_IFACE_ITER_RESUME_ALL || !active_only ||
660 sdata->flags & IEEE80211_SDATA_IN_DRIVER)) 666 sdata->flags & IEEE80211_SDATA_IN_DRIVER))
661 iterator(data, sdata->vif.addr, &sdata->vif); 667 iterator(data, sdata->vif.addr, &sdata->vif);
662} 668}
663 669
664void ieee80211_iterate_active_interfaces( 670void ieee80211_iterate_interfaces(
665 struct ieee80211_hw *hw, u32 iter_flags, 671 struct ieee80211_hw *hw, u32 iter_flags,
666 void (*iterator)(void *data, u8 *mac, 672 void (*iterator)(void *data, u8 *mac,
667 struct ieee80211_vif *vif), 673 struct ieee80211_vif *vif),
@@ -670,10 +676,10 @@ void ieee80211_iterate_active_interfaces(
670 struct ieee80211_local *local = hw_to_local(hw); 676 struct ieee80211_local *local = hw_to_local(hw);
671 677
672 mutex_lock(&local->iflist_mtx); 678 mutex_lock(&local->iflist_mtx);
673 __iterate_active_interfaces(local, iter_flags, iterator, data); 679 __iterate_interfaces(local, iter_flags, iterator, data);
674 mutex_unlock(&local->iflist_mtx); 680 mutex_unlock(&local->iflist_mtx);
675} 681}
676EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces); 682EXPORT_SYMBOL_GPL(ieee80211_iterate_interfaces);
677 683
678void ieee80211_iterate_active_interfaces_atomic( 684void ieee80211_iterate_active_interfaces_atomic(
679 struct ieee80211_hw *hw, u32 iter_flags, 685 struct ieee80211_hw *hw, u32 iter_flags,
@@ -684,7 +690,8 @@ void ieee80211_iterate_active_interfaces_atomic(
684 struct ieee80211_local *local = hw_to_local(hw); 690 struct ieee80211_local *local = hw_to_local(hw);
685 691
686 rcu_read_lock(); 692 rcu_read_lock();
687 __iterate_active_interfaces(local, iter_flags, iterator, data); 693 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
694 iterator, data);
688 rcu_read_unlock(); 695 rcu_read_unlock();
689} 696}
690EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic); 697EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_atomic);
@@ -699,7 +706,8 @@ void ieee80211_iterate_active_interfaces_rtnl(
699 706
700 ASSERT_RTNL(); 707 ASSERT_RTNL();
701 708
702 __iterate_active_interfaces(local, iter_flags, iterator, data); 709 __iterate_interfaces(local, iter_flags | IEEE80211_IFACE_ITER_ACTIVE,
710 iterator, data);
703} 711}
704EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl); 712EXPORT_SYMBOL_GPL(ieee80211_iterate_active_interfaces_rtnl);
705 713
@@ -742,6 +750,18 @@ struct ieee80211_vif *wdev_to_ieee80211_vif(struct wireless_dev *wdev)
742} 750}
743EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif); 751EXPORT_SYMBOL_GPL(wdev_to_ieee80211_vif);
744 752
753struct wireless_dev *ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
754{
755 struct ieee80211_sub_if_data *sdata = vif_to_sdata(vif);
756
757 if (!ieee80211_sdata_running(sdata) ||
758 !(sdata->flags & IEEE80211_SDATA_IN_DRIVER))
759 return NULL;
760
761 return &sdata->wdev;
762}
763EXPORT_SYMBOL_GPL(ieee80211_vif_to_wdev);
764
745/* 765/*
746 * Nothing should have been stuffed into the workqueue during 766 * Nothing should have been stuffed into the workqueue during
747 * the suspend->resume cycle. Since we can't check each caller 767 * the suspend->resume cycle. Since we can't check each caller
@@ -1811,8 +1831,25 @@ int ieee80211_reconfig(struct ieee80211_local *local)
1811 list_for_each_entry(sdata, &local->interfaces, list) { 1831 list_for_each_entry(sdata, &local->interfaces, list) {
1812 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN && 1832 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1813 sdata->vif.type != NL80211_IFTYPE_MONITOR && 1833 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1814 ieee80211_sdata_running(sdata)) 1834 ieee80211_sdata_running(sdata)) {
1815 res = drv_add_interface(local, sdata); 1835 res = drv_add_interface(local, sdata);
1836 if (WARN_ON(res))
1837 break;
1838 }
1839 }
1840
1841 /* If adding any of the interfaces failed above, roll back and
1842 * report failure.
1843 */
1844 if (res) {
1845 list_for_each_entry_continue_reverse(sdata, &local->interfaces,
1846 list)
1847 if (sdata->vif.type != NL80211_IFTYPE_AP_VLAN &&
1848 sdata->vif.type != NL80211_IFTYPE_MONITOR &&
1849 ieee80211_sdata_running(sdata))
1850 drv_remove_interface(local, sdata);
1851 ieee80211_handle_reconfig_failure(local);
1852 return res;
1816 } 1853 }
1817 1854
1818 /* add channel contexts */ 1855 /* add channel contexts */
@@ -2157,46 +2194,6 @@ void ieee80211_recalc_min_chandef(struct ieee80211_sub_if_data *sdata)
2157 mutex_unlock(&local->chanctx_mtx); 2194 mutex_unlock(&local->chanctx_mtx);
2158} 2195}
2159 2196
2160static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
2161{
2162 int i;
2163
2164 for (i = 0; i < n_ids; i++)
2165 if (ids[i] == id)
2166 return true;
2167 return false;
2168}
2169
2170size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
2171 const u8 *ids, int n_ids,
2172 const u8 *after_ric, int n_after_ric,
2173 size_t offset)
2174{
2175 size_t pos = offset;
2176
2177 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
2178 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
2179 pos += 2 + ies[pos + 1];
2180
2181 while (pos < ielen &&
2182 !ieee80211_id_in_list(after_ric, n_after_ric,
2183 ies[pos]))
2184 pos += 2 + ies[pos + 1];
2185 } else {
2186 pos += 2 + ies[pos + 1];
2187 }
2188 }
2189
2190 return pos;
2191}
2192
2193size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
2194 const u8 *ids, int n_ids, size_t offset)
2195{
2196 return ieee80211_ie_split_ric(ies, ielen, ids, n_ids, NULL, 0, offset);
2197}
2198EXPORT_SYMBOL(ieee80211_ie_split);
2199
2200size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset) 2197size_t ieee80211_ie_split_vendor(const u8 *ies, size_t ielen, size_t offset)
2201{ 2198{
2202 size_t pos = offset; 2199 size_t pos = offset;
@@ -2344,6 +2341,41 @@ u8 *ieee80211_ie_build_ht_oper(u8 *pos, struct ieee80211_sta_ht_cap *ht_cap,
2344 return pos + sizeof(struct ieee80211_ht_operation); 2341 return pos + sizeof(struct ieee80211_ht_operation);
2345} 2342}
2346 2343
2344u8 *ieee80211_ie_build_vht_oper(u8 *pos, struct ieee80211_sta_vht_cap *vht_cap,
2345 const struct cfg80211_chan_def *chandef)
2346{
2347 struct ieee80211_vht_operation *vht_oper;
2348
2349 *pos++ = WLAN_EID_VHT_OPERATION;
2350 *pos++ = sizeof(struct ieee80211_vht_operation);
2351 vht_oper = (struct ieee80211_vht_operation *)pos;
2352 vht_oper->center_freq_seg1_idx = ieee80211_frequency_to_channel(
2353 chandef->center_freq1);
2354 if (chandef->center_freq2)
2355 vht_oper->center_freq_seg2_idx =
2356 ieee80211_frequency_to_channel(chandef->center_freq2);
2357
2358 switch (chandef->width) {
2359 case NL80211_CHAN_WIDTH_160:
2360 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_160MHZ;
2361 break;
2362 case NL80211_CHAN_WIDTH_80P80:
2363 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80P80MHZ;
2364 break;
2365 case NL80211_CHAN_WIDTH_80:
2366 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_80MHZ;
2367 break;
2368 default:
2369 vht_oper->chan_width = IEEE80211_VHT_CHANWIDTH_USE_HT;
2370 break;
2371 }
2372
2373 /* don't require special VHT peer rates */
2374 vht_oper->basic_mcs_set = cpu_to_le16(0xffff);
2375
2376 return pos + sizeof(struct ieee80211_vht_operation);
2377}
2378
2347void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan, 2379void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
2348 const struct ieee80211_ht_operation *ht_oper, 2380 const struct ieee80211_ht_operation *ht_oper,
2349 struct cfg80211_chan_def *chandef) 2381 struct cfg80211_chan_def *chandef)
@@ -2373,6 +2405,39 @@ void ieee80211_ht_oper_to_chandef(struct ieee80211_channel *control_chan,
2373 cfg80211_chandef_create(chandef, control_chan, channel_type); 2405 cfg80211_chandef_create(chandef, control_chan, channel_type);
2374} 2406}
2375 2407
2408void ieee80211_vht_oper_to_chandef(struct ieee80211_channel *control_chan,
2409 const struct ieee80211_vht_operation *oper,
2410 struct cfg80211_chan_def *chandef)
2411{
2412 if (!oper)
2413 return;
2414
2415 chandef->chan = control_chan;
2416
2417 switch (oper->chan_width) {
2418 case IEEE80211_VHT_CHANWIDTH_USE_HT:
2419 break;
2420 case IEEE80211_VHT_CHANWIDTH_80MHZ:
2421 chandef->width = NL80211_CHAN_WIDTH_80;
2422 break;
2423 case IEEE80211_VHT_CHANWIDTH_160MHZ:
2424 chandef->width = NL80211_CHAN_WIDTH_160;
2425 break;
2426 case IEEE80211_VHT_CHANWIDTH_80P80MHZ:
2427 chandef->width = NL80211_CHAN_WIDTH_80P80;
2428 break;
2429 default:
2430 break;
2431 }
2432
2433 chandef->center_freq1 =
2434 ieee80211_channel_to_frequency(oper->center_freq_seg1_idx,
2435 control_chan->band);
2436 chandef->center_freq2 =
2437 ieee80211_channel_to_frequency(oper->center_freq_seg2_idx,
2438 control_chan->band);
2439}
2440
2376int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef, 2441int ieee80211_parse_bitrates(struct cfg80211_chan_def *chandef,
2377 const struct ieee80211_supported_band *sband, 2442 const struct ieee80211_supported_band *sband,
2378 const u8 *srates, int srates_len, u32 *rates) 2443 const u8 *srates, int srates_len, u32 *rates)
@@ -3252,3 +3317,20 @@ u8 *ieee80211_add_wmm_info_ie(u8 *buf, u8 qosinfo)
3252 3317
3253 return buf; 3318 return buf;
3254} 3319}
3320
3321void ieee80211_init_tx_queue(struct ieee80211_sub_if_data *sdata,
3322 struct sta_info *sta,
3323 struct txq_info *txqi, int tid)
3324{
3325 skb_queue_head_init(&txqi->queue);
3326 txqi->txq.vif = &sdata->vif;
3327
3328 if (sta) {
3329 txqi->txq.sta = &sta->sta;
3330 sta->sta.txq[tid] = &txqi->txq;
3331 txqi->txq.ac = ieee802_1d_to_ac[tid & 7];
3332 } else {
3333 sdata->vif.txq = &txqi->txq;
3334 txqi->txq.ac = IEEE80211_AC_BE;
3335 }
3336}
diff --git a/net/mac80211/vht.c b/net/mac80211/vht.c
index 85f9596da07b..80694d55db74 100644
--- a/net/mac80211/vht.c
+++ b/net/mac80211/vht.c
@@ -129,10 +129,6 @@ ieee80211_vht_cap_ie_to_sta_vht_cap(struct ieee80211_sub_if_data *sdata,
129 if (!vht_cap_ie || !sband->vht_cap.vht_supported) 129 if (!vht_cap_ie || !sband->vht_cap.vht_supported)
130 return; 130 return;
131 131
132 /* don't support VHT for TDLS peers for now */
133 if (test_sta_flag(sta, WLAN_STA_TDLS_PEER))
134 return;
135
136 /* 132 /*
137 * A VHT STA must support 40 MHz, but if we verify that here 133 * A VHT STA must support 40 MHz, but if we verify that here
138 * then we break a few things - some APs (e.g. Netgear R6300v2 134 * then we break a few things - some APs (e.g. Netgear R6300v2
diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c
index 75de6fac40d1..9d63d93c836e 100644
--- a/net/mac80211/wpa.c
+++ b/net/mac80211/wpa.c
@@ -780,9 +780,8 @@ ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
780 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 780 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
781 struct ieee80211_key *key = tx->key; 781 struct ieee80211_key *key = tx->key;
782 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 782 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
783 const struct ieee80211_cipher_scheme *cs = key->sta->cipher_scheme;
784 int hdrlen; 783 int hdrlen;
785 u8 *pos; 784 u8 *pos, iv_len = key->conf.iv_len;
786 785
787 if (info->control.hw_key && 786 if (info->control.hw_key &&
788 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) { 787 !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
@@ -790,14 +789,14 @@ ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
790 return TX_CONTINUE; 789 return TX_CONTINUE;
791 } 790 }
792 791
793 if (unlikely(skb_headroom(skb) < cs->hdr_len && 792 if (unlikely(skb_headroom(skb) < iv_len &&
794 pskb_expand_head(skb, cs->hdr_len, 0, GFP_ATOMIC))) 793 pskb_expand_head(skb, iv_len, 0, GFP_ATOMIC)))
795 return TX_DROP; 794 return TX_DROP;
796 795
797 hdrlen = ieee80211_hdrlen(hdr->frame_control); 796 hdrlen = ieee80211_hdrlen(hdr->frame_control);
798 797
799 pos = skb_push(skb, cs->hdr_len); 798 pos = skb_push(skb, iv_len);
800 memmove(pos, pos + cs->hdr_len, hdrlen); 799 memmove(pos, pos + iv_len, hdrlen);
801 800
802 return TX_CONTINUE; 801 return TX_CONTINUE;
803} 802}
@@ -1217,7 +1216,7 @@ ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
1217 if (!info->control.hw_key) 1216 if (!info->control.hw_key)
1218 return TX_DROP; 1217 return TX_DROP;
1219 1218
1220 if (tx->key->sta->cipher_scheme) { 1219 if (tx->key->flags & KEY_FLAG_CIPHER_SCHEME) {
1221 res = ieee80211_crypto_cs_encrypt(tx, skb); 1220 res = ieee80211_crypto_cs_encrypt(tx, skb);
1222 if (res != TX_CONTINUE) 1221 if (res != TX_CONTINUE)
1223 return res; 1222 return res;
diff --git a/net/mac802154/driver-ops.h b/net/mac802154/driver-ops.h
index 98180a9fff4a..a0533357b9ea 100644
--- a/net/mac802154/driver-ops.h
+++ b/net/mac802154/driver-ops.h
@@ -1,4 +1,4 @@
1#ifndef __MAC802154_DRVIER_OPS 1#ifndef __MAC802154_DRIVER_OPS
2#define __MAC802154_DRIVER_OPS 2#define __MAC802154_DRIVER_OPS
3 3
4#include <linux/types.h> 4#include <linux/types.h>
@@ -220,4 +220,4 @@ drv_set_promiscuous_mode(struct ieee802154_local *local, bool on)
220 return local->ops->set_promiscuous_mode(&local->hw, on); 220 return local->ops->set_promiscuous_mode(&local->hw, on);
221} 221}
222 222
223#endif /* __MAC802154_DRVIER_OPS */ 223#endif /* __MAC802154_DRIVER_OPS */
diff --git a/net/mac802154/iface.c b/net/mac802154/iface.c
index 6fb6bdf9868c..38b56f9d9386 100644
--- a/net/mac802154/iface.c
+++ b/net/mac802154/iface.c
@@ -174,24 +174,16 @@ ieee802154_check_mac_settings(struct ieee802154_local *local,
174 } 174 }
175 175
176 if (local->hw.flags & IEEE802154_HW_AFILT) { 176 if (local->hw.flags & IEEE802154_HW_AFILT) {
177 if (wpan_dev->pan_id != nwpan_dev->pan_id) 177 if (wpan_dev->pan_id != nwpan_dev->pan_id ||
178 return -EBUSY; 178 wpan_dev->short_addr != nwpan_dev->short_addr ||
179 179 wpan_dev->extended_addr != nwpan_dev->extended_addr)
180 if (wpan_dev->short_addr != nwpan_dev->short_addr)
181 return -EBUSY;
182
183 if (wpan_dev->extended_addr != nwpan_dev->extended_addr)
184 return -EBUSY; 180 return -EBUSY;
185 } 181 }
186 182
187 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { 183 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) {
188 if (wpan_dev->min_be != nwpan_dev->min_be) 184 if (wpan_dev->min_be != nwpan_dev->min_be ||
189 return -EBUSY; 185 wpan_dev->max_be != nwpan_dev->max_be ||
190 186 wpan_dev->csma_retries != nwpan_dev->csma_retries)
191 if (wpan_dev->max_be != nwpan_dev->max_be)
192 return -EBUSY;
193
194 if (wpan_dev->csma_retries != nwpan_dev->csma_retries)
195 return -EBUSY; 187 return -EBUSY;
196 } 188 }
197 189
diff --git a/net/mac802154/util.c b/net/mac802154/util.c
index 5fc979027919..150bf807e572 100644
--- a/net/mac802154/util.c
+++ b/net/mac802154/util.c
@@ -65,8 +65,19 @@ void ieee802154_xmit_complete(struct ieee802154_hw *hw, struct sk_buff *skb,
65{ 65{
66 if (ifs_handling) { 66 if (ifs_handling) {
67 struct ieee802154_local *local = hw_to_local(hw); 67 struct ieee802154_local *local = hw_to_local(hw);
68 u8 max_sifs_size;
68 69
69 if (skb->len > 18) 70 /* If transceiver sets CRC on his own we need to use lifs
71 * threshold len above 16 otherwise 18, because it's not
72 * part of skb->len.
73 */
74 if (hw->flags & IEEE802154_HW_TX_OMIT_CKSUM)
75 max_sifs_size = IEEE802154_MAX_SIFS_FRAME_SIZE -
76 IEEE802154_FCS_LEN;
77 else
78 max_sifs_size = IEEE802154_MAX_SIFS_FRAME_SIZE;
79
80 if (skb->len > max_sifs_size)
70 hrtimer_start(&local->ifs_timer, 81 hrtimer_start(&local->ifs_timer,
71 ktime_set(0, hw->phy->lifs_period * NSEC_PER_USEC), 82 ktime_set(0, hw->phy->lifs_period * NSEC_PER_USEC),
72 HRTIMER_MODE_REL); 83 HRTIMER_MODE_REL);
diff --git a/net/mpls/Kconfig b/net/mpls/Kconfig
index 37421db88965..17bde799c854 100644
--- a/net/mpls/Kconfig
+++ b/net/mpls/Kconfig
@@ -1,9 +1,30 @@
1# 1#
2# MPLS configuration 2# MPLS configuration
3# 3#
4
5menuconfig MPLS
6 bool "MultiProtocol Label Switching"
7 default n
8 ---help---
9 MultiProtocol Label Switching routes packets through logical
10 circuits. Originally conceived as a way of routing packets at
11 hardware speeds (before hardware was capable of routing ipv4 packets),
12 MPLS remains a simple way of making tunnels.
13
14 If you have not heard of MPLS you probably want to say N here.
15
16if MPLS
17
4config NET_MPLS_GSO 18config NET_MPLS_GSO
5 tristate "MPLS: GSO support" 19 tristate "MPLS: GSO support"
6 help 20 help
7 This is helper module to allow segmentation of non-MPLS GSO packets 21 This is helper module to allow segmentation of non-MPLS GSO packets
8 that have had MPLS stack entries pushed onto them and thus 22 that have had MPLS stack entries pushed onto them and thus
9 become MPLS GSO packets. 23 become MPLS GSO packets.
24
25config MPLS_ROUTING
26 tristate "MPLS: routing support"
27 help
28 Add support for forwarding of mpls packets.
29
30endif # MPLS
diff --git a/net/mpls/Makefile b/net/mpls/Makefile
index 6dec088c2d0f..65bbe68c72e6 100644
--- a/net/mpls/Makefile
+++ b/net/mpls/Makefile
@@ -2,3 +2,6 @@
2# Makefile for MPLS. 2# Makefile for MPLS.
3# 3#
4obj-$(CONFIG_NET_MPLS_GSO) += mpls_gso.o 4obj-$(CONFIG_NET_MPLS_GSO) += mpls_gso.o
5obj-$(CONFIG_MPLS_ROUTING) += mpls_router.o
6
7mpls_router-y := af_mpls.o
diff --git a/net/mpls/af_mpls.c b/net/mpls/af_mpls.c
new file mode 100644
index 000000000000..db8a2ea6d4de
--- /dev/null
+++ b/net/mpls/af_mpls.c
@@ -0,0 +1,1023 @@
1#include <linux/types.h>
2#include <linux/skbuff.h>
3#include <linux/socket.h>
4#include <linux/sysctl.h>
5#include <linux/net.h>
6#include <linux/module.h>
7#include <linux/if_arp.h>
8#include <linux/ipv6.h>
9#include <linux/mpls.h>
10#include <linux/vmalloc.h>
11#include <net/ip.h>
12#include <net/dst.h>
13#include <net/sock.h>
14#include <net/arp.h>
15#include <net/ip_fib.h>
16#include <net/netevent.h>
17#include <net/netns/generic.h>
18#include "internal.h"
19
20#define LABEL_NOT_SPECIFIED (1<<20)
21#define MAX_NEW_LABELS 2
22
23/* This maximum ha length copied from the definition of struct neighbour */
24#define MAX_VIA_ALEN (ALIGN(MAX_ADDR_LEN, sizeof(unsigned long)))
25
26struct mpls_route { /* next hop label forwarding entry */
27 struct net_device __rcu *rt_dev;
28 struct rcu_head rt_rcu;
29 u32 rt_label[MAX_NEW_LABELS];
30 u8 rt_protocol; /* routing protocol that set this entry */
31 u8 rt_labels;
32 u8 rt_via_alen;
33 u8 rt_via_table;
34 u8 rt_via[0];
35};
36
37static int zero = 0;
38static int label_limit = (1 << 20) - 1;
39
40static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
41 struct nlmsghdr *nlh, struct net *net, u32 portid,
42 unsigned int nlm_flags);
43
44static struct mpls_route *mpls_route_input_rcu(struct net *net, unsigned index)
45{
46 struct mpls_route *rt = NULL;
47
48 if (index < net->mpls.platform_labels) {
49 struct mpls_route __rcu **platform_label =
50 rcu_dereference(net->mpls.platform_label);
51 rt = rcu_dereference(platform_label[index]);
52 }
53 return rt;
54}
55
56static bool mpls_output_possible(const struct net_device *dev)
57{
58 return dev && (dev->flags & IFF_UP) && netif_carrier_ok(dev);
59}
60
61static unsigned int mpls_rt_header_size(const struct mpls_route *rt)
62{
63 /* The size of the layer 2.5 labels to be added for this route */
64 return rt->rt_labels * sizeof(struct mpls_shim_hdr);
65}
66
67static unsigned int mpls_dev_mtu(const struct net_device *dev)
68{
69 /* The amount of data the layer 2 frame can hold */
70 return dev->mtu;
71}
72
73static bool mpls_pkt_too_big(const struct sk_buff *skb, unsigned int mtu)
74{
75 if (skb->len <= mtu)
76 return false;
77
78 if (skb_is_gso(skb) && skb_gso_network_seglen(skb) <= mtu)
79 return false;
80
81 return true;
82}
83
84static bool mpls_egress(struct mpls_route *rt, struct sk_buff *skb,
85 struct mpls_entry_decoded dec)
86{
87 /* RFC4385 and RFC5586 encode other packets in mpls such that
88 * they don't conflict with the ip version number, making
89 * decoding by examining the ip version correct in everything
90 * except for the strangest cases.
91 *
92 * The strange cases if we choose to support them will require
93 * manual configuration.
94 */
95 struct iphdr *hdr4;
96 bool success = true;
97
98 /* The IPv4 code below accesses through the IPv4 header
99 * checksum, which is 12 bytes into the packet.
100 * The IPv6 code below accesses through the IPv6 hop limit
101 * which is 8 bytes into the packet.
102 *
103 * For all supported cases there should always be at least 12
104 * bytes of packet data present. The IPv4 header is 20 bytes
105 * without options and the IPv6 header is always 40 bytes
106 * long.
107 */
108 if (!pskb_may_pull(skb, 12))
109 return false;
110
111 /* Use ip_hdr to find the ip protocol version */
112 hdr4 = ip_hdr(skb);
113 if (hdr4->version == 4) {
114 skb->protocol = htons(ETH_P_IP);
115 csum_replace2(&hdr4->check,
116 htons(hdr4->ttl << 8),
117 htons(dec.ttl << 8));
118 hdr4->ttl = dec.ttl;
119 }
120 else if (hdr4->version == 6) {
121 struct ipv6hdr *hdr6 = ipv6_hdr(skb);
122 skb->protocol = htons(ETH_P_IPV6);
123 hdr6->hop_limit = dec.ttl;
124 }
125 else
126 /* version 0 and version 1 are used by pseudo wires */
127 success = false;
128 return success;
129}
130
131static int mpls_forward(struct sk_buff *skb, struct net_device *dev,
132 struct packet_type *pt, struct net_device *orig_dev)
133{
134 struct net *net = dev_net(dev);
135 struct mpls_shim_hdr *hdr;
136 struct mpls_route *rt;
137 struct mpls_entry_decoded dec;
138 struct net_device *out_dev;
139 unsigned int hh_len;
140 unsigned int new_header_size;
141 unsigned int mtu;
142 int err;
143
144 /* Careful this entire function runs inside of an rcu critical section */
145
146 if (skb->pkt_type != PACKET_HOST)
147 goto drop;
148
149 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL)
150 goto drop;
151
152 if (!pskb_may_pull(skb, sizeof(*hdr)))
153 goto drop;
154
155 /* Read and decode the label */
156 hdr = mpls_hdr(skb);
157 dec = mpls_entry_decode(hdr);
158
159 /* Pop the label */
160 skb_pull(skb, sizeof(*hdr));
161 skb_reset_network_header(skb);
162
163 skb_orphan(skb);
164
165 rt = mpls_route_input_rcu(net, dec.label);
166 if (!rt)
167 goto drop;
168
169 /* Find the output device */
170 out_dev = rcu_dereference(rt->rt_dev);
171 if (!mpls_output_possible(out_dev))
172 goto drop;
173
174 if (skb_warn_if_lro(skb))
175 goto drop;
176
177 skb_forward_csum(skb);
178
179 /* Verify ttl is valid */
180 if (dec.ttl <= 1)
181 goto drop;
182 dec.ttl -= 1;
183
184 /* Verify the destination can hold the packet */
185 new_header_size = mpls_rt_header_size(rt);
186 mtu = mpls_dev_mtu(out_dev);
187 if (mpls_pkt_too_big(skb, mtu - new_header_size))
188 goto drop;
189
190 hh_len = LL_RESERVED_SPACE(out_dev);
191 if (!out_dev->header_ops)
192 hh_len = 0;
193
194 /* Ensure there is enough space for the headers in the skb */
195 if (skb_cow(skb, hh_len + new_header_size))
196 goto drop;
197
198 skb->dev = out_dev;
199 skb->protocol = htons(ETH_P_MPLS_UC);
200
201 if (unlikely(!new_header_size && dec.bos)) {
202 /* Penultimate hop popping */
203 if (!mpls_egress(rt, skb, dec))
204 goto drop;
205 } else {
206 bool bos;
207 int i;
208 skb_push(skb, new_header_size);
209 skb_reset_network_header(skb);
210 /* Push the new labels */
211 hdr = mpls_hdr(skb);
212 bos = dec.bos;
213 for (i = rt->rt_labels - 1; i >= 0; i--) {
214 hdr[i] = mpls_entry_encode(rt->rt_label[i], dec.ttl, 0, bos);
215 bos = false;
216 }
217 }
218
219 err = neigh_xmit(rt->rt_via_table, out_dev, rt->rt_via, skb);
220 if (err)
221 net_dbg_ratelimited("%s: packet transmission failed: %d\n",
222 __func__, err);
223 return 0;
224
225drop:
226 kfree_skb(skb);
227 return NET_RX_DROP;
228}
229
230static struct packet_type mpls_packet_type __read_mostly = {
231 .type = cpu_to_be16(ETH_P_MPLS_UC),
232 .func = mpls_forward,
233};
234
235static const struct nla_policy rtm_mpls_policy[RTA_MAX+1] = {
236 [RTA_DST] = { .type = NLA_U32 },
237 [RTA_OIF] = { .type = NLA_U32 },
238};
239
240struct mpls_route_config {
241 u32 rc_protocol;
242 u32 rc_ifindex;
243 u16 rc_via_table;
244 u16 rc_via_alen;
245 u8 rc_via[MAX_VIA_ALEN];
246 u32 rc_label;
247 u32 rc_output_labels;
248 u32 rc_output_label[MAX_NEW_LABELS];
249 u32 rc_nlflags;
250 struct nl_info rc_nlinfo;
251};
252
253static struct mpls_route *mpls_rt_alloc(size_t alen)
254{
255 struct mpls_route *rt;
256
257 rt = kzalloc(sizeof(*rt) + alen, GFP_KERNEL);
258 if (rt)
259 rt->rt_via_alen = alen;
260 return rt;
261}
262
263static void mpls_rt_free(struct mpls_route *rt)
264{
265 if (rt)
266 kfree_rcu(rt, rt_rcu);
267}
268
269static void mpls_notify_route(struct net *net, unsigned index,
270 struct mpls_route *old, struct mpls_route *new,
271 const struct nl_info *info)
272{
273 struct nlmsghdr *nlh = info ? info->nlh : NULL;
274 unsigned portid = info ? info->portid : 0;
275 int event = new ? RTM_NEWROUTE : RTM_DELROUTE;
276 struct mpls_route *rt = new ? new : old;
277 unsigned nlm_flags = (old && new) ? NLM_F_REPLACE : 0;
278 /* Ignore reserved labels for now */
279 if (rt && (index >= 16))
280 rtmsg_lfib(event, index, rt, nlh, net, portid, nlm_flags);
281}
282
283static void mpls_route_update(struct net *net, unsigned index,
284 struct net_device *dev, struct mpls_route *new,
285 const struct nl_info *info)
286{
287 struct mpls_route __rcu **platform_label;
288 struct mpls_route *rt, *old = NULL;
289
290 ASSERT_RTNL();
291
292 platform_label = rtnl_dereference(net->mpls.platform_label);
293 rt = rtnl_dereference(platform_label[index]);
294 if (!dev || (rt && (rtnl_dereference(rt->rt_dev) == dev))) {
295 rcu_assign_pointer(platform_label[index], new);
296 old = rt;
297 }
298
299 mpls_notify_route(net, index, old, new, info);
300
301 /* If we removed a route free it now */
302 mpls_rt_free(old);
303}
304
305static unsigned find_free_label(struct net *net)
306{
307 struct mpls_route __rcu **platform_label;
308 size_t platform_labels;
309 unsigned index;
310
311 platform_label = rtnl_dereference(net->mpls.platform_label);
312 platform_labels = net->mpls.platform_labels;
313 for (index = 16; index < platform_labels; index++) {
314 if (!rtnl_dereference(platform_label[index]))
315 return index;
316 }
317 return LABEL_NOT_SPECIFIED;
318}
319
320static int mpls_route_add(struct mpls_route_config *cfg)
321{
322 struct mpls_route __rcu **platform_label;
323 struct net *net = cfg->rc_nlinfo.nl_net;
324 struct net_device *dev = NULL;
325 struct mpls_route *rt, *old;
326 unsigned index;
327 int i;
328 int err = -EINVAL;
329
330 index = cfg->rc_label;
331
332 /* If a label was not specified during insert pick one */
333 if ((index == LABEL_NOT_SPECIFIED) &&
334 (cfg->rc_nlflags & NLM_F_CREATE)) {
335 index = find_free_label(net);
336 }
337
338 /* The first 16 labels are reserved, and may not be set */
339 if (index < 16)
340 goto errout;
341
342 /* The full 20 bit range may not be supported. */
343 if (index >= net->mpls.platform_labels)
344 goto errout;
345
346 /* Ensure only a supported number of labels are present */
347 if (cfg->rc_output_labels > MAX_NEW_LABELS)
348 goto errout;
349
350 err = -ENODEV;
351 dev = dev_get_by_index(net, cfg->rc_ifindex);
352 if (!dev)
353 goto errout;
354
355 /* For now just support ethernet devices */
356 err = -EINVAL;
357 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
358 goto errout;
359
360 err = -EINVAL;
361 if ((cfg->rc_via_table == NEIGH_LINK_TABLE) &&
362 (dev->addr_len != cfg->rc_via_alen))
363 goto errout;
364
365 /* Append makes no sense with mpls */
366 err = -EOPNOTSUPP;
367 if (cfg->rc_nlflags & NLM_F_APPEND)
368 goto errout;
369
370 err = -EEXIST;
371 platform_label = rtnl_dereference(net->mpls.platform_label);
372 old = rtnl_dereference(platform_label[index]);
373 if ((cfg->rc_nlflags & NLM_F_EXCL) && old)
374 goto errout;
375
376 err = -EEXIST;
377 if (!(cfg->rc_nlflags & NLM_F_REPLACE) && old)
378 goto errout;
379
380 err = -ENOENT;
381 if (!(cfg->rc_nlflags & NLM_F_CREATE) && !old)
382 goto errout;
383
384 err = -ENOMEM;
385 rt = mpls_rt_alloc(cfg->rc_via_alen);
386 if (!rt)
387 goto errout;
388
389 rt->rt_labels = cfg->rc_output_labels;
390 for (i = 0; i < rt->rt_labels; i++)
391 rt->rt_label[i] = cfg->rc_output_label[i];
392 rt->rt_protocol = cfg->rc_protocol;
393 RCU_INIT_POINTER(rt->rt_dev, dev);
394 rt->rt_via_table = cfg->rc_via_table;
395 memcpy(rt->rt_via, cfg->rc_via, cfg->rc_via_alen);
396
397 mpls_route_update(net, index, NULL, rt, &cfg->rc_nlinfo);
398
399 dev_put(dev);
400 return 0;
401
402errout:
403 if (dev)
404 dev_put(dev);
405 return err;
406}
407
408static int mpls_route_del(struct mpls_route_config *cfg)
409{
410 struct net *net = cfg->rc_nlinfo.nl_net;
411 unsigned index;
412 int err = -EINVAL;
413
414 index = cfg->rc_label;
415
416 /* The first 16 labels are reserved, and may not be removed */
417 if (index < 16)
418 goto errout;
419
420 /* The full 20 bit range may not be supported */
421 if (index >= net->mpls.platform_labels)
422 goto errout;
423
424 mpls_route_update(net, index, NULL, NULL, &cfg->rc_nlinfo);
425
426 err = 0;
427errout:
428 return err;
429}
430
431static void mpls_ifdown(struct net_device *dev)
432{
433 struct mpls_route __rcu **platform_label;
434 struct net *net = dev_net(dev);
435 unsigned index;
436
437 platform_label = rtnl_dereference(net->mpls.platform_label);
438 for (index = 0; index < net->mpls.platform_labels; index++) {
439 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
440 if (!rt)
441 continue;
442 if (rtnl_dereference(rt->rt_dev) != dev)
443 continue;
444 rt->rt_dev = NULL;
445 }
446}
447
448static int mpls_dev_notify(struct notifier_block *this, unsigned long event,
449 void *ptr)
450{
451 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
452
453 switch(event) {
454 case NETDEV_UNREGISTER:
455 mpls_ifdown(dev);
456 break;
457 }
458 return NOTIFY_OK;
459}
460
461static struct notifier_block mpls_dev_notifier = {
462 .notifier_call = mpls_dev_notify,
463};
464
465static int nla_put_via(struct sk_buff *skb,
466 u8 table, const void *addr, int alen)
467{
468 static const int table_to_family[NEIGH_NR_TABLES + 1] = {
469 AF_INET, AF_INET6, AF_DECnet, AF_PACKET,
470 };
471 struct nlattr *nla;
472 struct rtvia *via;
473 int family = AF_UNSPEC;
474
475 nla = nla_reserve(skb, RTA_VIA, alen + 2);
476 if (!nla)
477 return -EMSGSIZE;
478
479 if (table <= NEIGH_NR_TABLES)
480 family = table_to_family[table];
481
482 via = nla_data(nla);
483 via->rtvia_family = family;
484 memcpy(via->rtvia_addr, addr, alen);
485 return 0;
486}
487
488int nla_put_labels(struct sk_buff *skb, int attrtype,
489 u8 labels, const u32 label[])
490{
491 struct nlattr *nla;
492 struct mpls_shim_hdr *nla_label;
493 bool bos;
494 int i;
495 nla = nla_reserve(skb, attrtype, labels*4);
496 if (!nla)
497 return -EMSGSIZE;
498
499 nla_label = nla_data(nla);
500 bos = true;
501 for (i = labels - 1; i >= 0; i--) {
502 nla_label[i] = mpls_entry_encode(label[i], 0, 0, bos);
503 bos = false;
504 }
505
506 return 0;
507}
508
509int nla_get_labels(const struct nlattr *nla,
510 u32 max_labels, u32 *labels, u32 label[])
511{
512 unsigned len = nla_len(nla);
513 unsigned nla_labels;
514 struct mpls_shim_hdr *nla_label;
515 bool bos;
516 int i;
517
518 /* len needs to be an even multiple of 4 (the label size) */
519 if (len & 3)
520 return -EINVAL;
521
522 /* Limit the number of new labels allowed */
523 nla_labels = len/4;
524 if (nla_labels > max_labels)
525 return -EINVAL;
526
527 nla_label = nla_data(nla);
528 bos = true;
529 for (i = nla_labels - 1; i >= 0; i--, bos = false) {
530 struct mpls_entry_decoded dec;
531 dec = mpls_entry_decode(nla_label + i);
532
533 /* Ensure the bottom of stack flag is properly set
534 * and ttl and tc are both clear.
535 */
536 if ((dec.bos != bos) || dec.ttl || dec.tc)
537 return -EINVAL;
538
539 label[i] = dec.label;
540 }
541 *labels = nla_labels;
542 return 0;
543}
544
545static int rtm_to_route_config(struct sk_buff *skb, struct nlmsghdr *nlh,
546 struct mpls_route_config *cfg)
547{
548 struct rtmsg *rtm;
549 struct nlattr *tb[RTA_MAX+1];
550 int index;
551 int err;
552
553 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_mpls_policy);
554 if (err < 0)
555 goto errout;
556
557 err = -EINVAL;
558 rtm = nlmsg_data(nlh);
559 memset(cfg, 0, sizeof(*cfg));
560
561 if (rtm->rtm_family != AF_MPLS)
562 goto errout;
563 if (rtm->rtm_dst_len != 20)
564 goto errout;
565 if (rtm->rtm_src_len != 0)
566 goto errout;
567 if (rtm->rtm_tos != 0)
568 goto errout;
569 if (rtm->rtm_table != RT_TABLE_MAIN)
570 goto errout;
571 /* Any value is acceptable for rtm_protocol */
572
573 /* As mpls uses destination specific addresses
574 * (or source specific address in the case of multicast)
575 * all addresses have universal scope.
576 */
577 if (rtm->rtm_scope != RT_SCOPE_UNIVERSE)
578 goto errout;
579 if (rtm->rtm_type != RTN_UNICAST)
580 goto errout;
581 if (rtm->rtm_flags != 0)
582 goto errout;
583
584 cfg->rc_label = LABEL_NOT_SPECIFIED;
585 cfg->rc_protocol = rtm->rtm_protocol;
586 cfg->rc_nlflags = nlh->nlmsg_flags;
587 cfg->rc_nlinfo.portid = NETLINK_CB(skb).portid;
588 cfg->rc_nlinfo.nlh = nlh;
589 cfg->rc_nlinfo.nl_net = sock_net(skb->sk);
590
591 for (index = 0; index <= RTA_MAX; index++) {
592 struct nlattr *nla = tb[index];
593 if (!nla)
594 continue;
595
596 switch(index) {
597 case RTA_OIF:
598 cfg->rc_ifindex = nla_get_u32(nla);
599 break;
600 case RTA_NEWDST:
601 if (nla_get_labels(nla, MAX_NEW_LABELS,
602 &cfg->rc_output_labels,
603 cfg->rc_output_label))
604 goto errout;
605 break;
606 case RTA_DST:
607 {
608 u32 label_count;
609 if (nla_get_labels(nla, 1, &label_count,
610 &cfg->rc_label))
611 goto errout;
612
613 /* The first 16 labels are reserved, and may not be set */
614 if (cfg->rc_label < 16)
615 goto errout;
616
617 break;
618 }
619 case RTA_VIA:
620 {
621 struct rtvia *via = nla_data(nla);
622 if (nla_len(nla) < offsetof(struct rtvia, rtvia_addr))
623 goto errout;
624 cfg->rc_via_alen = nla_len(nla) -
625 offsetof(struct rtvia, rtvia_addr);
626 if (cfg->rc_via_alen > MAX_VIA_ALEN)
627 goto errout;
628
629 /* Validate the address family */
630 switch(via->rtvia_family) {
631 case AF_PACKET:
632 cfg->rc_via_table = NEIGH_LINK_TABLE;
633 break;
634 case AF_INET:
635 cfg->rc_via_table = NEIGH_ARP_TABLE;
636 if (cfg->rc_via_alen != 4)
637 goto errout;
638 break;
639 case AF_INET6:
640 cfg->rc_via_table = NEIGH_ND_TABLE;
641 if (cfg->rc_via_alen != 16)
642 goto errout;
643 break;
644 default:
645 /* Unsupported address family */
646 goto errout;
647 }
648
649 memcpy(cfg->rc_via, via->rtvia_addr, cfg->rc_via_alen);
650 break;
651 }
652 default:
653 /* Unsupported attribute */
654 goto errout;
655 }
656 }
657
658 err = 0;
659errout:
660 return err;
661}
662
663static int mpls_rtm_delroute(struct sk_buff *skb, struct nlmsghdr *nlh)
664{
665 struct mpls_route_config cfg;
666 int err;
667
668 err = rtm_to_route_config(skb, nlh, &cfg);
669 if (err < 0)
670 return err;
671
672 return mpls_route_del(&cfg);
673}
674
675
676static int mpls_rtm_newroute(struct sk_buff *skb, struct nlmsghdr *nlh)
677{
678 struct mpls_route_config cfg;
679 int err;
680
681 err = rtm_to_route_config(skb, nlh, &cfg);
682 if (err < 0)
683 return err;
684
685 return mpls_route_add(&cfg);
686}
687
688static int mpls_dump_route(struct sk_buff *skb, u32 portid, u32 seq, int event,
689 u32 label, struct mpls_route *rt, int flags)
690{
691 struct net_device *dev;
692 struct nlmsghdr *nlh;
693 struct rtmsg *rtm;
694
695 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*rtm), flags);
696 if (nlh == NULL)
697 return -EMSGSIZE;
698
699 rtm = nlmsg_data(nlh);
700 rtm->rtm_family = AF_MPLS;
701 rtm->rtm_dst_len = 20;
702 rtm->rtm_src_len = 0;
703 rtm->rtm_tos = 0;
704 rtm->rtm_table = RT_TABLE_MAIN;
705 rtm->rtm_protocol = rt->rt_protocol;
706 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
707 rtm->rtm_type = RTN_UNICAST;
708 rtm->rtm_flags = 0;
709
710 if (rt->rt_labels &&
711 nla_put_labels(skb, RTA_NEWDST, rt->rt_labels, rt->rt_label))
712 goto nla_put_failure;
713 if (nla_put_via(skb, rt->rt_via_table, rt->rt_via, rt->rt_via_alen))
714 goto nla_put_failure;
715 dev = rtnl_dereference(rt->rt_dev);
716 if (dev && nla_put_u32(skb, RTA_OIF, dev->ifindex))
717 goto nla_put_failure;
718 if (nla_put_labels(skb, RTA_DST, 1, &label))
719 goto nla_put_failure;
720
721 nlmsg_end(skb, nlh);
722 return 0;
723
724nla_put_failure:
725 nlmsg_cancel(skb, nlh);
726 return -EMSGSIZE;
727}
728
729static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
730{
731 struct net *net = sock_net(skb->sk);
732 struct mpls_route __rcu **platform_label;
733 size_t platform_labels;
734 unsigned int index;
735
736 ASSERT_RTNL();
737
738 index = cb->args[0];
739 if (index < 16)
740 index = 16;
741
742 platform_label = rtnl_dereference(net->mpls.platform_label);
743 platform_labels = net->mpls.platform_labels;
744 for (; index < platform_labels; index++) {
745 struct mpls_route *rt;
746 rt = rtnl_dereference(platform_label[index]);
747 if (!rt)
748 continue;
749
750 if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
751 cb->nlh->nlmsg_seq, RTM_NEWROUTE,
752 index, rt, NLM_F_MULTI) < 0)
753 break;
754 }
755 cb->args[0] = index;
756
757 return skb->len;
758}
759
760static inline size_t lfib_nlmsg_size(struct mpls_route *rt)
761{
762 size_t payload =
763 NLMSG_ALIGN(sizeof(struct rtmsg))
764 + nla_total_size(2 + rt->rt_via_alen) /* RTA_VIA */
765 + nla_total_size(4); /* RTA_DST */
766 if (rt->rt_labels) /* RTA_NEWDST */
767 payload += nla_total_size(rt->rt_labels * 4);
768 if (rt->rt_dev) /* RTA_OIF */
769 payload += nla_total_size(4);
770 return payload;
771}
772
773static void rtmsg_lfib(int event, u32 label, struct mpls_route *rt,
774 struct nlmsghdr *nlh, struct net *net, u32 portid,
775 unsigned int nlm_flags)
776{
777 struct sk_buff *skb;
778 u32 seq = nlh ? nlh->nlmsg_seq : 0;
779 int err = -ENOBUFS;
780
781 skb = nlmsg_new(lfib_nlmsg_size(rt), GFP_KERNEL);
782 if (skb == NULL)
783 goto errout;
784
785 err = mpls_dump_route(skb, portid, seq, event, label, rt, nlm_flags);
786 if (err < 0) {
787 /* -EMSGSIZE implies BUG in lfib_nlmsg_size */
788 WARN_ON(err == -EMSGSIZE);
789 kfree_skb(skb);
790 goto errout;
791 }
792 rtnl_notify(skb, net, portid, RTNLGRP_MPLS_ROUTE, nlh, GFP_KERNEL);
793
794 return;
795errout:
796 if (err < 0)
797 rtnl_set_sk_err(net, RTNLGRP_MPLS_ROUTE, err);
798}
799
800static int resize_platform_label_table(struct net *net, size_t limit)
801{
802 size_t size = sizeof(struct mpls_route *) * limit;
803 size_t old_limit;
804 size_t cp_size;
805 struct mpls_route __rcu **labels = NULL, **old;
806 struct mpls_route *rt0 = NULL, *rt2 = NULL;
807 unsigned index;
808
809 if (size) {
810 labels = kzalloc(size, GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY);
811 if (!labels)
812 labels = vzalloc(size);
813
814 if (!labels)
815 goto nolabels;
816 }
817
818 /* In case the predefined labels need to be populated */
819 if (limit > LABEL_IPV4_EXPLICIT_NULL) {
820 struct net_device *lo = net->loopback_dev;
821 rt0 = mpls_rt_alloc(lo->addr_len);
822 if (!rt0)
823 goto nort0;
824 RCU_INIT_POINTER(rt0->rt_dev, lo);
825 rt0->rt_protocol = RTPROT_KERNEL;
826 rt0->rt_via_table = NEIGH_LINK_TABLE;
827 memcpy(rt0->rt_via, lo->dev_addr, lo->addr_len);
828 }
829 if (limit > LABEL_IPV6_EXPLICIT_NULL) {
830 struct net_device *lo = net->loopback_dev;
831 rt2 = mpls_rt_alloc(lo->addr_len);
832 if (!rt2)
833 goto nort2;
834 RCU_INIT_POINTER(rt2->rt_dev, lo);
835 rt2->rt_protocol = RTPROT_KERNEL;
836 rt2->rt_via_table = NEIGH_LINK_TABLE;
837 memcpy(rt2->rt_via, lo->dev_addr, lo->addr_len);
838 }
839
840 rtnl_lock();
841 /* Remember the original table */
842 old = rtnl_dereference(net->mpls.platform_label);
843 old_limit = net->mpls.platform_labels;
844
845 /* Free any labels beyond the new table */
846 for (index = limit; index < old_limit; index++)
847 mpls_route_update(net, index, NULL, NULL, NULL);
848
849 /* Copy over the old labels */
850 cp_size = size;
851 if (old_limit < limit)
852 cp_size = old_limit * sizeof(struct mpls_route *);
853
854 memcpy(labels, old, cp_size);
855
856 /* If needed set the predefined labels */
857 if ((old_limit <= LABEL_IPV6_EXPLICIT_NULL) &&
858 (limit > LABEL_IPV6_EXPLICIT_NULL)) {
859 RCU_INIT_POINTER(labels[LABEL_IPV6_EXPLICIT_NULL], rt2);
860 rt2 = NULL;
861 }
862
863 if ((old_limit <= LABEL_IPV4_EXPLICIT_NULL) &&
864 (limit > LABEL_IPV4_EXPLICIT_NULL)) {
865 RCU_INIT_POINTER(labels[LABEL_IPV4_EXPLICIT_NULL], rt0);
866 rt0 = NULL;
867 }
868
869 /* Update the global pointers */
870 net->mpls.platform_labels = limit;
871 rcu_assign_pointer(net->mpls.platform_label, labels);
872
873 rtnl_unlock();
874
875 mpls_rt_free(rt2);
876 mpls_rt_free(rt0);
877
878 if (old) {
879 synchronize_rcu();
880 kvfree(old);
881 }
882 return 0;
883
884nort2:
885 mpls_rt_free(rt0);
886nort0:
887 kvfree(labels);
888nolabels:
889 return -ENOMEM;
890}
891
892static int mpls_platform_labels(struct ctl_table *table, int write,
893 void __user *buffer, size_t *lenp, loff_t *ppos)
894{
895 struct net *net = table->data;
896 int platform_labels = net->mpls.platform_labels;
897 int ret;
898 struct ctl_table tmp = {
899 .procname = table->procname,
900 .data = &platform_labels,
901 .maxlen = sizeof(int),
902 .mode = table->mode,
903 .extra1 = &zero,
904 .extra2 = &label_limit,
905 };
906
907 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
908
909 if (write && ret == 0)
910 ret = resize_platform_label_table(net, platform_labels);
911
912 return ret;
913}
914
915static struct ctl_table mpls_table[] = {
916 {
917 .procname = "platform_labels",
918 .data = NULL,
919 .maxlen = sizeof(int),
920 .mode = 0644,
921 .proc_handler = mpls_platform_labels,
922 },
923 { }
924};
925
926static int mpls_net_init(struct net *net)
927{
928 struct ctl_table *table;
929
930 net->mpls.platform_labels = 0;
931 net->mpls.platform_label = NULL;
932
933 table = kmemdup(mpls_table, sizeof(mpls_table), GFP_KERNEL);
934 if (table == NULL)
935 return -ENOMEM;
936
937 table[0].data = net;
938 net->mpls.ctl = register_net_sysctl(net, "net/mpls", table);
939 if (net->mpls.ctl == NULL)
940 return -ENOMEM;
941
942 return 0;
943}
944
945static void mpls_net_exit(struct net *net)
946{
947 struct mpls_route __rcu **platform_label;
948 size_t platform_labels;
949 struct ctl_table *table;
950 unsigned int index;
951
952 table = net->mpls.ctl->ctl_table_arg;
953 unregister_net_sysctl_table(net->mpls.ctl);
954 kfree(table);
955
956 /* An rcu grace period has passed since there was a device in
957 * the network namespace (and thus the last in flight packet)
958 * left this network namespace. This is because
959 * unregister_netdevice_many and netdev_run_todo has completed
960 * for each network device that was in this network namespace.
961 *
962 * As such no additional rcu synchronization is necessary when
963 * freeing the platform_label table.
964 */
965 rtnl_lock();
966 platform_label = rtnl_dereference(net->mpls.platform_label);
967 platform_labels = net->mpls.platform_labels;
968 for (index = 0; index < platform_labels; index++) {
969 struct mpls_route *rt = rtnl_dereference(platform_label[index]);
970 RCU_INIT_POINTER(platform_label[index], NULL);
971 mpls_rt_free(rt);
972 }
973 rtnl_unlock();
974
975 kvfree(platform_label);
976}
977
978static struct pernet_operations mpls_net_ops = {
979 .init = mpls_net_init,
980 .exit = mpls_net_exit,
981};
982
983static int __init mpls_init(void)
984{
985 int err;
986
987 BUILD_BUG_ON(sizeof(struct mpls_shim_hdr) != 4);
988
989 err = register_pernet_subsys(&mpls_net_ops);
990 if (err)
991 goto out;
992
993 err = register_netdevice_notifier(&mpls_dev_notifier);
994 if (err)
995 goto out_unregister_pernet;
996
997 dev_add_pack(&mpls_packet_type);
998
999 rtnl_register(PF_MPLS, RTM_NEWROUTE, mpls_rtm_newroute, NULL, NULL);
1000 rtnl_register(PF_MPLS, RTM_DELROUTE, mpls_rtm_delroute, NULL, NULL);
1001 rtnl_register(PF_MPLS, RTM_GETROUTE, NULL, mpls_dump_routes, NULL);
1002 err = 0;
1003out:
1004 return err;
1005
1006out_unregister_pernet:
1007 unregister_pernet_subsys(&mpls_net_ops);
1008 goto out;
1009}
1010module_init(mpls_init);
1011
1012static void __exit mpls_exit(void)
1013{
1014 rtnl_unregister_all(PF_MPLS);
1015 dev_remove_pack(&mpls_packet_type);
1016 unregister_netdevice_notifier(&mpls_dev_notifier);
1017 unregister_pernet_subsys(&mpls_net_ops);
1018}
1019module_exit(mpls_exit);
1020
1021MODULE_DESCRIPTION("MultiProtocol Label Switching");
1022MODULE_LICENSE("GPL v2");
1023MODULE_ALIAS_NETPROTO(PF_MPLS);
diff --git a/net/mpls/internal.h b/net/mpls/internal.h
new file mode 100644
index 000000000000..fb6de92052c4
--- /dev/null
+++ b/net/mpls/internal.h
@@ -0,0 +1,59 @@
1#ifndef MPLS_INTERNAL_H
2#define MPLS_INTERNAL_H
3
4#define LABEL_IPV4_EXPLICIT_NULL 0 /* RFC3032 */
5#define LABEL_ROUTER_ALERT_LABEL 1 /* RFC3032 */
6#define LABEL_IPV6_EXPLICIT_NULL 2 /* RFC3032 */
7#define LABEL_IMPLICIT_NULL 3 /* RFC3032 */
8#define LABEL_ENTROPY_INDICATOR 7 /* RFC6790 */
9#define LABEL_GAL 13 /* RFC5586 */
10#define LABEL_OAM_ALERT 14 /* RFC3429 */
11#define LABEL_EXTENSION 15 /* RFC7274 */
12
13
14struct mpls_shim_hdr {
15 __be32 label_stack_entry;
16};
17
18struct mpls_entry_decoded {
19 u32 label;
20 u8 ttl;
21 u8 tc;
22 u8 bos;
23};
24
25struct sk_buff;
26
27static inline struct mpls_shim_hdr *mpls_hdr(const struct sk_buff *skb)
28{
29 return (struct mpls_shim_hdr *)skb_network_header(skb);
30}
31
32static inline struct mpls_shim_hdr mpls_entry_encode(u32 label, unsigned ttl, unsigned tc, bool bos)
33{
34 struct mpls_shim_hdr result;
35 result.label_stack_entry =
36 cpu_to_be32((label << MPLS_LS_LABEL_SHIFT) |
37 (tc << MPLS_LS_TC_SHIFT) |
38 (bos ? (1 << MPLS_LS_S_SHIFT) : 0) |
39 (ttl << MPLS_LS_TTL_SHIFT));
40 return result;
41}
42
43static inline struct mpls_entry_decoded mpls_entry_decode(struct mpls_shim_hdr *hdr)
44{
45 struct mpls_entry_decoded result;
46 unsigned entry = be32_to_cpu(hdr->label_stack_entry);
47
48 result.label = (entry & MPLS_LS_LABEL_MASK) >> MPLS_LS_LABEL_SHIFT;
49 result.ttl = (entry & MPLS_LS_TTL_MASK) >> MPLS_LS_TTL_SHIFT;
50 result.tc = (entry & MPLS_LS_TC_MASK) >> MPLS_LS_TC_SHIFT;
51 result.bos = (entry & MPLS_LS_S_MASK) >> MPLS_LS_S_SHIFT;
52
53 return result;
54}
55
56int nla_put_labels(struct sk_buff *skb, int attrtype, u8 labels, const u32 label[]);
57int nla_get_labels(const struct nlattr *nla, u32 max_labels, u32 *labels, u32 label[]);
58
59#endif /* MPLS_INTERNAL_H */
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index b02660fa9eb0..f70e34a68f70 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -438,8 +438,10 @@ config NF_TABLES
438 438
439 To compile it as a module, choose M here. 439 To compile it as a module, choose M here.
440 440
441if NF_TABLES
442
441config NF_TABLES_INET 443config NF_TABLES_INET
442 depends on NF_TABLES && IPV6 444 depends on IPV6
443 select NF_TABLES_IPV4 445 select NF_TABLES_IPV4
444 select NF_TABLES_IPV6 446 select NF_TABLES_IPV6
445 tristate "Netfilter nf_tables mixed IPv4/IPv6 tables support" 447 tristate "Netfilter nf_tables mixed IPv4/IPv6 tables support"
@@ -447,21 +449,18 @@ config NF_TABLES_INET
447 This option enables support for a mixed IPv4/IPv6 "inet" table. 449 This option enables support for a mixed IPv4/IPv6 "inet" table.
448 450
449config NFT_EXTHDR 451config NFT_EXTHDR
450 depends on NF_TABLES
451 tristate "Netfilter nf_tables IPv6 exthdr module" 452 tristate "Netfilter nf_tables IPv6 exthdr module"
452 help 453 help
453 This option adds the "exthdr" expression that you can use to match 454 This option adds the "exthdr" expression that you can use to match
454 IPv6 extension headers. 455 IPv6 extension headers.
455 456
456config NFT_META 457config NFT_META
457 depends on NF_TABLES
458 tristate "Netfilter nf_tables meta module" 458 tristate "Netfilter nf_tables meta module"
459 help 459 help
460 This option adds the "meta" expression that you can use to match and 460 This option adds the "meta" expression that you can use to match and
461 to set packet metainformation such as the packet mark. 461 to set packet metainformation such as the packet mark.
462 462
463config NFT_CT 463config NFT_CT
464 depends on NF_TABLES
465 depends on NF_CONNTRACK 464 depends on NF_CONNTRACK
466 tristate "Netfilter nf_tables conntrack module" 465 tristate "Netfilter nf_tables conntrack module"
467 help 466 help
@@ -469,42 +468,36 @@ config NFT_CT
469 connection tracking information such as the flow state. 468 connection tracking information such as the flow state.
470 469
471config NFT_RBTREE 470config NFT_RBTREE
472 depends on NF_TABLES
473 tristate "Netfilter nf_tables rbtree set module" 471 tristate "Netfilter nf_tables rbtree set module"
474 help 472 help
475 This option adds the "rbtree" set type (Red Black tree) that is used 473 This option adds the "rbtree" set type (Red Black tree) that is used
476 to build interval-based sets. 474 to build interval-based sets.
477 475
478config NFT_HASH 476config NFT_HASH
479 depends on NF_TABLES
480 tristate "Netfilter nf_tables hash set module" 477 tristate "Netfilter nf_tables hash set module"
481 help 478 help
482 This option adds the "hash" set type that is used to build one-way 479 This option adds the "hash" set type that is used to build one-way
483 mappings between matchings and actions. 480 mappings between matchings and actions.
484 481
485config NFT_COUNTER 482config NFT_COUNTER
486 depends on NF_TABLES
487 tristate "Netfilter nf_tables counter module" 483 tristate "Netfilter nf_tables counter module"
488 help 484 help
489 This option adds the "counter" expression that you can use to 485 This option adds the "counter" expression that you can use to
490 include packet and byte counters in a rule. 486 include packet and byte counters in a rule.
491 487
492config NFT_LOG 488config NFT_LOG
493 depends on NF_TABLES
494 tristate "Netfilter nf_tables log module" 489 tristate "Netfilter nf_tables log module"
495 help 490 help
496 This option adds the "log" expression that you can use to log 491 This option adds the "log" expression that you can use to log
497 packets matching some criteria. 492 packets matching some criteria.
498 493
499config NFT_LIMIT 494config NFT_LIMIT
500 depends on NF_TABLES
501 tristate "Netfilter nf_tables limit module" 495 tristate "Netfilter nf_tables limit module"
502 help 496 help
503 This option adds the "limit" expression that you can use to 497 This option adds the "limit" expression that you can use to
504 ratelimit rule matchings. 498 ratelimit rule matchings.
505 499
506config NFT_MASQ 500config NFT_MASQ
507 depends on NF_TABLES
508 depends on NF_CONNTRACK 501 depends on NF_CONNTRACK
509 depends on NF_NAT 502 depends on NF_NAT
510 tristate "Netfilter nf_tables masquerade support" 503 tristate "Netfilter nf_tables masquerade support"
@@ -513,7 +506,6 @@ config NFT_MASQ
513 to perform NAT in the masquerade flavour. 506 to perform NAT in the masquerade flavour.
514 507
515config NFT_REDIR 508config NFT_REDIR
516 depends on NF_TABLES
517 depends on NF_CONNTRACK 509 depends on NF_CONNTRACK
518 depends on NF_NAT 510 depends on NF_NAT
519 tristate "Netfilter nf_tables redirect support" 511 tristate "Netfilter nf_tables redirect support"
@@ -522,7 +514,6 @@ config NFT_REDIR
522 to perform NAT in the redirect flavour. 514 to perform NAT in the redirect flavour.
523 515
524config NFT_NAT 516config NFT_NAT
525 depends on NF_TABLES
526 depends on NF_CONNTRACK 517 depends on NF_CONNTRACK
527 select NF_NAT 518 select NF_NAT
528 tristate "Netfilter nf_tables nat module" 519 tristate "Netfilter nf_tables nat module"
@@ -531,8 +522,6 @@ config NFT_NAT
531 typical Network Address Translation (NAT) packet transformations. 522 typical Network Address Translation (NAT) packet transformations.
532 523
533config NFT_QUEUE 524config NFT_QUEUE
534 depends on NF_TABLES
535 depends on NETFILTER_XTABLES
536 depends on NETFILTER_NETLINK_QUEUE 525 depends on NETFILTER_NETLINK_QUEUE
537 tristate "Netfilter nf_tables queue module" 526 tristate "Netfilter nf_tables queue module"
538 help 527 help
@@ -540,7 +529,6 @@ config NFT_QUEUE
540 infrastructure (also known as NFQUEUE) from nftables. 529 infrastructure (also known as NFQUEUE) from nftables.
541 530
542config NFT_REJECT 531config NFT_REJECT
543 depends on NF_TABLES
544 default m if NETFILTER_ADVANCED=n 532 default m if NETFILTER_ADVANCED=n
545 tristate "Netfilter nf_tables reject support" 533 tristate "Netfilter nf_tables reject support"
546 help 534 help
@@ -554,7 +542,6 @@ config NFT_REJECT_INET
554 tristate 542 tristate
555 543
556config NFT_COMPAT 544config NFT_COMPAT
557 depends on NF_TABLES
558 depends on NETFILTER_XTABLES 545 depends on NETFILTER_XTABLES
559 tristate "Netfilter x_tables over nf_tables module" 546 tristate "Netfilter x_tables over nf_tables module"
560 help 547 help
@@ -562,6 +549,8 @@ config NFT_COMPAT
562 x_tables match/target extensions over the nf_tables 549 x_tables match/target extensions over the nf_tables
563 framework. 550 framework.
564 551
552endif # NF_TABLES
553
565config NETFILTER_XTABLES 554config NETFILTER_XTABLES
566 tristate "Netfilter Xtables support (required for ip_tables)" 555 tristate "Netfilter Xtables support (required for ip_tables)"
567 default m if NETFILTER_ADVANCED=n 556 default m if NETFILTER_ADVANCED=n
@@ -951,7 +940,7 @@ comment "Xtables matches"
951 940
952config NETFILTER_XT_MATCH_ADDRTYPE 941config NETFILTER_XT_MATCH_ADDRTYPE
953 tristate '"addrtype" address type match support' 942 tristate '"addrtype" address type match support'
954 depends on NETFILTER_ADVANCED 943 default m if NETFILTER_ADVANCED=n
955 ---help--- 944 ---help---
956 This option allows you to match what routing thinks of an address, 945 This option allows you to match what routing thinks of an address,
957 eg. UNICAST, LOCAL, BROADCAST, ... 946 eg. UNICAST, LOCAL, BROADCAST, ...
diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
index 89f73a9e9874..a87d8b8ec730 100644
--- a/net/netfilter/Makefile
+++ b/net/netfilter/Makefile
@@ -70,7 +70,7 @@ obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o
70 70
71# nf_tables 71# nf_tables
72nf_tables-objs += nf_tables_core.o nf_tables_api.o 72nf_tables-objs += nf_tables_core.o nf_tables_api.o
73nf_tables-objs += nft_immediate.o nft_cmp.o nft_lookup.o 73nf_tables-objs += nft_immediate.o nft_cmp.o nft_lookup.o nft_dynset.o
74nf_tables-objs += nft_bitwise.o nft_byteorder.o nft_payload.o 74nf_tables-objs += nft_bitwise.o nft_byteorder.o nft_payload.o
75 75
76obj-$(CONFIG_NF_TABLES) += nf_tables.o 76obj-$(CONFIG_NF_TABLES) += nf_tables.o
diff --git a/net/netfilter/core.c b/net/netfilter/core.c
index fea9ef566427..e6163017c42d 100644
--- a/net/netfilter/core.c
+++ b/net/netfilter/core.c
@@ -120,12 +120,8 @@ EXPORT_SYMBOL(nf_unregister_hooks);
120 120
121unsigned int nf_iterate(struct list_head *head, 121unsigned int nf_iterate(struct list_head *head,
122 struct sk_buff *skb, 122 struct sk_buff *skb,
123 unsigned int hook, 123 struct nf_hook_state *state,
124 const struct net_device *indev, 124 struct nf_hook_ops **elemp)
125 const struct net_device *outdev,
126 struct nf_hook_ops **elemp,
127 int (*okfn)(struct sk_buff *),
128 int hook_thresh)
129{ 125{
130 unsigned int verdict; 126 unsigned int verdict;
131 127
@@ -134,19 +130,19 @@ unsigned int nf_iterate(struct list_head *head,
134 * function because of risk of continuing from deleted element. 130 * function because of risk of continuing from deleted element.
135 */ 131 */
136 list_for_each_entry_continue_rcu((*elemp), head, list) { 132 list_for_each_entry_continue_rcu((*elemp), head, list) {
137 if (hook_thresh > (*elemp)->priority) 133 if (state->thresh > (*elemp)->priority)
138 continue; 134 continue;
139 135
140 /* Optimization: we don't need to hold module 136 /* Optimization: we don't need to hold module
141 reference here, since function can't sleep. --RR */ 137 reference here, since function can't sleep. --RR */
142repeat: 138repeat:
143 verdict = (*elemp)->hook(*elemp, skb, indev, outdev, okfn); 139 verdict = (*elemp)->hook(*elemp, skb, state);
144 if (verdict != NF_ACCEPT) { 140 if (verdict != NF_ACCEPT) {
145#ifdef CONFIG_NETFILTER_DEBUG 141#ifdef CONFIG_NETFILTER_DEBUG
146 if (unlikely((verdict & NF_VERDICT_MASK) 142 if (unlikely((verdict & NF_VERDICT_MASK)
147 > NF_MAX_VERDICT)) { 143 > NF_MAX_VERDICT)) {
148 NFDEBUG("Evil return from %p(%u).\n", 144 NFDEBUG("Evil return from %p(%u).\n",
149 (*elemp)->hook, hook); 145 (*elemp)->hook, state->hook);
150 continue; 146 continue;
151 } 147 }
152#endif 148#endif
@@ -161,11 +157,7 @@ repeat:
161 157
162/* Returns 1 if okfn() needs to be executed by the caller, 158/* Returns 1 if okfn() needs to be executed by the caller,
163 * -EPERM for NF_DROP, 0 otherwise. */ 159 * -EPERM for NF_DROP, 0 otherwise. */
164int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb, 160int nf_hook_slow(struct sk_buff *skb, struct nf_hook_state *state)
165 struct net_device *indev,
166 struct net_device *outdev,
167 int (*okfn)(struct sk_buff *),
168 int hook_thresh)
169{ 161{
170 struct nf_hook_ops *elem; 162 struct nf_hook_ops *elem;
171 unsigned int verdict; 163 unsigned int verdict;
@@ -174,10 +166,11 @@ int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
174 /* We may already have this, but read-locks nest anyway */ 166 /* We may already have this, but read-locks nest anyway */
175 rcu_read_lock(); 167 rcu_read_lock();
176 168
177 elem = list_entry_rcu(&nf_hooks[pf][hook], struct nf_hook_ops, list); 169 elem = list_entry_rcu(&nf_hooks[state->pf][state->hook],
170 struct nf_hook_ops, list);
178next_hook: 171next_hook:
179 verdict = nf_iterate(&nf_hooks[pf][hook], skb, hook, indev, 172 verdict = nf_iterate(&nf_hooks[state->pf][state->hook], skb, state,
180 outdev, &elem, okfn, hook_thresh); 173 &elem);
181 if (verdict == NF_ACCEPT || verdict == NF_STOP) { 174 if (verdict == NF_ACCEPT || verdict == NF_STOP) {
182 ret = 1; 175 ret = 1;
183 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) { 176 } else if ((verdict & NF_VERDICT_MASK) == NF_DROP) {
@@ -186,8 +179,8 @@ next_hook:
186 if (ret == 0) 179 if (ret == 0)
187 ret = -EPERM; 180 ret = -EPERM;
188 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { 181 } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) {
189 int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn, 182 int err = nf_queue(skb, elem, state,
190 verdict >> NF_VERDICT_QBITS); 183 verdict >> NF_VERDICT_QBITS);
191 if (err < 0) { 184 if (err < 0) {
192 if (err == -ECANCELED) 185 if (err == -ECANCELED)
193 goto next_hook; 186 goto next_hook;
diff --git a/net/netfilter/ipset/ip_set_hash_netiface.c b/net/netfilter/ipset/ip_set_hash_netiface.c
index 758b002130d9..380ef5148ea1 100644
--- a/net/netfilter/ipset/ip_set_hash_netiface.c
+++ b/net/netfilter/ipset/ip_set_hash_netiface.c
@@ -19,6 +19,7 @@
19#include <net/netlink.h> 19#include <net/netlink.h>
20 20
21#include <linux/netfilter.h> 21#include <linux/netfilter.h>
22#include <linux/netfilter_bridge.h>
22#include <linux/netfilter/ipset/pfxlen.h> 23#include <linux/netfilter/ipset/pfxlen.h>
23#include <linux/netfilter/ipset/ip_set.h> 24#include <linux/netfilter/ipset/ip_set.h>
24#include <linux/netfilter/ipset/ip_set_hash.h> 25#include <linux/netfilter/ipset/ip_set_hash.h>
@@ -211,6 +212,22 @@ hash_netiface4_data_next(struct hash_netiface4_elem *next,
211#define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed) 212#define HKEY_DATALEN sizeof(struct hash_netiface4_elem_hashed)
212#include "ip_set_hash_gen.h" 213#include "ip_set_hash_gen.h"
213 214
215#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
216static const char *get_physindev_name(const struct sk_buff *skb)
217{
218 struct net_device *dev = nf_bridge_get_physindev(skb);
219
220 return dev ? dev->name : NULL;
221}
222
223static const char *get_phyoutdev_name(const struct sk_buff *skb)
224{
225 struct net_device *dev = nf_bridge_get_physoutdev(skb);
226
227 return dev ? dev->name : NULL;
228}
229#endif
230
214static int 231static int
215hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb, 232hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
216 const struct xt_action_param *par, 233 const struct xt_action_param *par,
@@ -234,16 +251,15 @@ hash_netiface4_kadt(struct ip_set *set, const struct sk_buff *skb,
234 e.ip &= ip_set_netmask(e.cidr); 251 e.ip &= ip_set_netmask(e.cidr);
235 252
236#define IFACE(dir) (par->dir ? par->dir->name : NULL) 253#define IFACE(dir) (par->dir ? par->dir->name : NULL)
237#define PHYSDEV(dir) (nf_bridge->dir ? nf_bridge->dir->name : NULL)
238#define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC) 254#define SRCDIR (opt->flags & IPSET_DIM_TWO_SRC)
239 255
240 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) { 256 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
241#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 257#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
242 const struct nf_bridge_info *nf_bridge = skb->nf_bridge; 258 e.iface = SRCDIR ? get_physindev_name(skb) :
259 get_phyoutdev_name(skb);
243 260
244 if (!nf_bridge) 261 if (!e.iface)
245 return -EINVAL; 262 return -EINVAL;
246 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev);
247 e.physdev = 1; 263 e.physdev = 1;
248#else 264#else
249 e.iface = NULL; 265 e.iface = NULL;
@@ -476,11 +492,11 @@ hash_netiface6_kadt(struct ip_set *set, const struct sk_buff *skb,
476 492
477 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) { 493 if (opt->cmdflags & IPSET_FLAG_PHYSDEV) {
478#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 494#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
479 const struct nf_bridge_info *nf_bridge = skb->nf_bridge; 495 e.iface = SRCDIR ? get_physindev_name(skb) :
480 496 get_phyoutdev_name(skb);
481 if (!nf_bridge) 497 if (!e.iface)
482 return -EINVAL; 498 return -EINVAL;
483 e.iface = SRCDIR ? PHYSDEV(physindev) : PHYSDEV(physoutdev); 499
484 e.physdev = 1; 500 e.physdev = 1;
485#else 501#else
486 e.iface = NULL; 502 e.iface = NULL;
diff --git a/net/netfilter/ipvs/ip_vs_core.c b/net/netfilter/ipvs/ip_vs_core.c
index b87ca32efa0b..5d2b806a862e 100644
--- a/net/netfilter/ipvs/ip_vs_core.c
+++ b/net/netfilter/ipvs/ip_vs_core.c
@@ -119,24 +119,24 @@ ip_vs_in_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
119 struct ip_vs_service *svc; 119 struct ip_vs_service *svc;
120 120
121 s = this_cpu_ptr(dest->stats.cpustats); 121 s = this_cpu_ptr(dest->stats.cpustats);
122 s->ustats.inpkts++;
123 u64_stats_update_begin(&s->syncp); 122 u64_stats_update_begin(&s->syncp);
124 s->ustats.inbytes += skb->len; 123 s->cnt.inpkts++;
124 s->cnt.inbytes += skb->len;
125 u64_stats_update_end(&s->syncp); 125 u64_stats_update_end(&s->syncp);
126 126
127 rcu_read_lock(); 127 rcu_read_lock();
128 svc = rcu_dereference(dest->svc); 128 svc = rcu_dereference(dest->svc);
129 s = this_cpu_ptr(svc->stats.cpustats); 129 s = this_cpu_ptr(svc->stats.cpustats);
130 s->ustats.inpkts++;
131 u64_stats_update_begin(&s->syncp); 130 u64_stats_update_begin(&s->syncp);
132 s->ustats.inbytes += skb->len; 131 s->cnt.inpkts++;
132 s->cnt.inbytes += skb->len;
133 u64_stats_update_end(&s->syncp); 133 u64_stats_update_end(&s->syncp);
134 rcu_read_unlock(); 134 rcu_read_unlock();
135 135
136 s = this_cpu_ptr(ipvs->tot_stats.cpustats); 136 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
137 s->ustats.inpkts++;
138 u64_stats_update_begin(&s->syncp); 137 u64_stats_update_begin(&s->syncp);
139 s->ustats.inbytes += skb->len; 138 s->cnt.inpkts++;
139 s->cnt.inbytes += skb->len;
140 u64_stats_update_end(&s->syncp); 140 u64_stats_update_end(&s->syncp);
141 } 141 }
142} 142}
@@ -153,24 +153,24 @@ ip_vs_out_stats(struct ip_vs_conn *cp, struct sk_buff *skb)
153 struct ip_vs_service *svc; 153 struct ip_vs_service *svc;
154 154
155 s = this_cpu_ptr(dest->stats.cpustats); 155 s = this_cpu_ptr(dest->stats.cpustats);
156 s->ustats.outpkts++;
157 u64_stats_update_begin(&s->syncp); 156 u64_stats_update_begin(&s->syncp);
158 s->ustats.outbytes += skb->len; 157 s->cnt.outpkts++;
158 s->cnt.outbytes += skb->len;
159 u64_stats_update_end(&s->syncp); 159 u64_stats_update_end(&s->syncp);
160 160
161 rcu_read_lock(); 161 rcu_read_lock();
162 svc = rcu_dereference(dest->svc); 162 svc = rcu_dereference(dest->svc);
163 s = this_cpu_ptr(svc->stats.cpustats); 163 s = this_cpu_ptr(svc->stats.cpustats);
164 s->ustats.outpkts++;
165 u64_stats_update_begin(&s->syncp); 164 u64_stats_update_begin(&s->syncp);
166 s->ustats.outbytes += skb->len; 165 s->cnt.outpkts++;
166 s->cnt.outbytes += skb->len;
167 u64_stats_update_end(&s->syncp); 167 u64_stats_update_end(&s->syncp);
168 rcu_read_unlock(); 168 rcu_read_unlock();
169 169
170 s = this_cpu_ptr(ipvs->tot_stats.cpustats); 170 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
171 s->ustats.outpkts++;
172 u64_stats_update_begin(&s->syncp); 171 u64_stats_update_begin(&s->syncp);
173 s->ustats.outbytes += skb->len; 172 s->cnt.outpkts++;
173 s->cnt.outbytes += skb->len;
174 u64_stats_update_end(&s->syncp); 174 u64_stats_update_end(&s->syncp);
175 } 175 }
176} 176}
@@ -183,13 +183,19 @@ ip_vs_conn_stats(struct ip_vs_conn *cp, struct ip_vs_service *svc)
183 struct ip_vs_cpu_stats *s; 183 struct ip_vs_cpu_stats *s;
184 184
185 s = this_cpu_ptr(cp->dest->stats.cpustats); 185 s = this_cpu_ptr(cp->dest->stats.cpustats);
186 s->ustats.conns++; 186 u64_stats_update_begin(&s->syncp);
187 s->cnt.conns++;
188 u64_stats_update_end(&s->syncp);
187 189
188 s = this_cpu_ptr(svc->stats.cpustats); 190 s = this_cpu_ptr(svc->stats.cpustats);
189 s->ustats.conns++; 191 u64_stats_update_begin(&s->syncp);
192 s->cnt.conns++;
193 u64_stats_update_end(&s->syncp);
190 194
191 s = this_cpu_ptr(ipvs->tot_stats.cpustats); 195 s = this_cpu_ptr(ipvs->tot_stats.cpustats);
192 s->ustats.conns++; 196 u64_stats_update_begin(&s->syncp);
197 s->cnt.conns++;
198 u64_stats_update_end(&s->syncp);
193} 199}
194 200
195 201
@@ -1046,6 +1052,26 @@ static inline bool is_new_conn(const struct sk_buff *skb,
1046 } 1052 }
1047} 1053}
1048 1054
1055static inline bool is_new_conn_expected(const struct ip_vs_conn *cp,
1056 int conn_reuse_mode)
1057{
1058 /* Controlled (FTP DATA or persistence)? */
1059 if (cp->control)
1060 return false;
1061
1062 switch (cp->protocol) {
1063 case IPPROTO_TCP:
1064 return (cp->state == IP_VS_TCP_S_TIME_WAIT) ||
1065 ((conn_reuse_mode & 2) &&
1066 (cp->state == IP_VS_TCP_S_FIN_WAIT) &&
1067 (cp->flags & IP_VS_CONN_F_NOOUTPUT));
1068 case IPPROTO_SCTP:
1069 return cp->state == IP_VS_SCTP_S_CLOSED;
1070 default:
1071 return false;
1072 }
1073}
1074
1049/* Handle response packets: rewrite addresses and send away... 1075/* Handle response packets: rewrite addresses and send away...
1050 */ 1076 */
1051static unsigned int 1077static unsigned int
@@ -1246,8 +1272,7 @@ ip_vs_out(unsigned int hooknum, struct sk_buff *skb, int af)
1246 */ 1272 */
1247static unsigned int 1273static unsigned int
1248ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb, 1274ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1249 const struct net_device *in, const struct net_device *out, 1275 const struct nf_hook_state *state)
1250 int (*okfn)(struct sk_buff *))
1251{ 1276{
1252 return ip_vs_out(ops->hooknum, skb, AF_INET); 1277 return ip_vs_out(ops->hooknum, skb, AF_INET);
1253} 1278}
@@ -1258,8 +1283,7 @@ ip_vs_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1258 */ 1283 */
1259static unsigned int 1284static unsigned int
1260ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb, 1285ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1261 const struct net_device *in, const struct net_device *out, 1286 const struct nf_hook_state *state)
1262 int (*okfn)(struct sk_buff *))
1263{ 1287{
1264 return ip_vs_out(ops->hooknum, skb, AF_INET); 1288 return ip_vs_out(ops->hooknum, skb, AF_INET);
1265} 1289}
@@ -1273,8 +1297,7 @@ ip_vs_local_reply4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1273 */ 1297 */
1274static unsigned int 1298static unsigned int
1275ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb, 1299ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1276 const struct net_device *in, const struct net_device *out, 1300 const struct nf_hook_state *state)
1277 int (*okfn)(struct sk_buff *))
1278{ 1301{
1279 return ip_vs_out(ops->hooknum, skb, AF_INET6); 1302 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1280} 1303}
@@ -1285,8 +1308,7 @@ ip_vs_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1285 */ 1308 */
1286static unsigned int 1309static unsigned int
1287ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb, 1310ip_vs_local_reply6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1288 const struct net_device *in, const struct net_device *out, 1311 const struct nf_hook_state *state)
1289 int (*okfn)(struct sk_buff *))
1290{ 1312{
1291 return ip_vs_out(ops->hooknum, skb, AF_INET6); 1313 return ip_vs_out(ops->hooknum, skb, AF_INET6);
1292} 1314}
@@ -1585,6 +1607,7 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1585 struct ip_vs_conn *cp; 1607 struct ip_vs_conn *cp;
1586 int ret, pkts; 1608 int ret, pkts;
1587 struct netns_ipvs *ipvs; 1609 struct netns_ipvs *ipvs;
1610 int conn_reuse_mode;
1588 1611
1589 /* Already marked as IPVS request or reply? */ 1612 /* Already marked as IPVS request or reply? */
1590 if (skb->ipvs_property) 1613 if (skb->ipvs_property)
@@ -1653,10 +1676,14 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1653 */ 1676 */
1654 cp = pp->conn_in_get(af, skb, &iph, 0); 1677 cp = pp->conn_in_get(af, skb, &iph, 0);
1655 1678
1656 if (unlikely(sysctl_expire_nodest_conn(ipvs)) && cp && cp->dest && 1679 conn_reuse_mode = sysctl_conn_reuse_mode(ipvs);
1657 unlikely(!atomic_read(&cp->dest->weight)) && !iph.fragoffs && 1680 if (conn_reuse_mode && !iph.fragoffs &&
1658 is_new_conn(skb, &iph)) { 1681 is_new_conn(skb, &iph) && cp &&
1659 ip_vs_conn_expire_now(cp); 1682 ((unlikely(sysctl_expire_nodest_conn(ipvs)) && cp->dest &&
1683 unlikely(!atomic_read(&cp->dest->weight))) ||
1684 unlikely(is_new_conn_expected(cp, conn_reuse_mode)))) {
1685 if (!atomic_read(&cp->n_control))
1686 ip_vs_conn_expire_now(cp);
1660 __ip_vs_conn_put(cp); 1687 __ip_vs_conn_put(cp);
1661 cp = NULL; 1688 cp = NULL;
1662 } 1689 }
@@ -1738,9 +1765,7 @@ ip_vs_in(unsigned int hooknum, struct sk_buff *skb, int af)
1738 */ 1765 */
1739static unsigned int 1766static unsigned int
1740ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb, 1767ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1741 const struct net_device *in, 1768 const struct nf_hook_state *state)
1742 const struct net_device *out,
1743 int (*okfn)(struct sk_buff *))
1744{ 1769{
1745 return ip_vs_in(ops->hooknum, skb, AF_INET); 1770 return ip_vs_in(ops->hooknum, skb, AF_INET);
1746} 1771}
@@ -1751,8 +1776,7 @@ ip_vs_remote_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1751 */ 1776 */
1752static unsigned int 1777static unsigned int
1753ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb, 1778ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1754 const struct net_device *in, const struct net_device *out, 1779 const struct nf_hook_state *state)
1755 int (*okfn)(struct sk_buff *))
1756{ 1780{
1757 return ip_vs_in(ops->hooknum, skb, AF_INET); 1781 return ip_vs_in(ops->hooknum, skb, AF_INET);
1758} 1782}
@@ -1765,9 +1789,7 @@ ip_vs_local_request4(const struct nf_hook_ops *ops, struct sk_buff *skb,
1765 */ 1789 */
1766static unsigned int 1790static unsigned int
1767ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb, 1791ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1768 const struct net_device *in, 1792 const struct nf_hook_state *state)
1769 const struct net_device *out,
1770 int (*okfn)(struct sk_buff *))
1771{ 1793{
1772 return ip_vs_in(ops->hooknum, skb, AF_INET6); 1794 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1773} 1795}
@@ -1778,8 +1800,7 @@ ip_vs_remote_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1778 */ 1800 */
1779static unsigned int 1801static unsigned int
1780ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb, 1802ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1781 const struct net_device *in, const struct net_device *out, 1803 const struct nf_hook_state *state)
1782 int (*okfn)(struct sk_buff *))
1783{ 1804{
1784 return ip_vs_in(ops->hooknum, skb, AF_INET6); 1805 return ip_vs_in(ops->hooknum, skb, AF_INET6);
1785} 1806}
@@ -1798,8 +1819,7 @@ ip_vs_local_request6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1798 */ 1819 */
1799static unsigned int 1820static unsigned int
1800ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb, 1821ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1801 const struct net_device *in, const struct net_device *out, 1822 const struct nf_hook_state *state)
1802 int (*okfn)(struct sk_buff *))
1803{ 1823{
1804 int r; 1824 int r;
1805 struct net *net; 1825 struct net *net;
@@ -1820,8 +1840,7 @@ ip_vs_forward_icmp(const struct nf_hook_ops *ops, struct sk_buff *skb,
1820#ifdef CONFIG_IP_VS_IPV6 1840#ifdef CONFIG_IP_VS_IPV6
1821static unsigned int 1841static unsigned int
1822ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb, 1842ip_vs_forward_icmp_v6(const struct nf_hook_ops *ops, struct sk_buff *skb,
1823 const struct net_device *in, const struct net_device *out, 1843 const struct nf_hook_state *state)
1824 int (*okfn)(struct sk_buff *))
1825{ 1844{
1826 int r; 1845 int r;
1827 struct net *net; 1846 struct net *net;
diff --git a/net/netfilter/ipvs/ip_vs_ctl.c b/net/netfilter/ipvs/ip_vs_ctl.c
index ed99448671c3..49532672f66d 100644
--- a/net/netfilter/ipvs/ip_vs_ctl.c
+++ b/net/netfilter/ipvs/ip_vs_ctl.c
@@ -729,9 +729,9 @@ static void ip_vs_trash_cleanup(struct net *net)
729} 729}
730 730
731static void 731static void
732ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src) 732ip_vs_copy_stats(struct ip_vs_kstats *dst, struct ip_vs_stats *src)
733{ 733{
734#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->ustats.c - src->ustats0.c 734#define IP_VS_SHOW_STATS_COUNTER(c) dst->c = src->kstats.c - src->kstats0.c
735 735
736 spin_lock_bh(&src->lock); 736 spin_lock_bh(&src->lock);
737 737
@@ -747,13 +747,28 @@ ip_vs_copy_stats(struct ip_vs_stats_user *dst, struct ip_vs_stats *src)
747} 747}
748 748
749static void 749static void
750ip_vs_export_stats_user(struct ip_vs_stats_user *dst, struct ip_vs_kstats *src)
751{
752 dst->conns = (u32)src->conns;
753 dst->inpkts = (u32)src->inpkts;
754 dst->outpkts = (u32)src->outpkts;
755 dst->inbytes = src->inbytes;
756 dst->outbytes = src->outbytes;
757 dst->cps = (u32)src->cps;
758 dst->inpps = (u32)src->inpps;
759 dst->outpps = (u32)src->outpps;
760 dst->inbps = (u32)src->inbps;
761 dst->outbps = (u32)src->outbps;
762}
763
764static void
750ip_vs_zero_stats(struct ip_vs_stats *stats) 765ip_vs_zero_stats(struct ip_vs_stats *stats)
751{ 766{
752 spin_lock_bh(&stats->lock); 767 spin_lock_bh(&stats->lock);
753 768
754 /* get current counters as zero point, rates are zeroed */ 769 /* get current counters as zero point, rates are zeroed */
755 770
756#define IP_VS_ZERO_STATS_COUNTER(c) stats->ustats0.c = stats->ustats.c 771#define IP_VS_ZERO_STATS_COUNTER(c) stats->kstats0.c = stats->kstats.c
757 772
758 IP_VS_ZERO_STATS_COUNTER(conns); 773 IP_VS_ZERO_STATS_COUNTER(conns);
759 IP_VS_ZERO_STATS_COUNTER(inpkts); 774 IP_VS_ZERO_STATS_COUNTER(inpkts);
@@ -1808,6 +1823,12 @@ static struct ctl_table vs_vars[] = {
1808 .mode = 0644, 1823 .mode = 0644,
1809 .proc_handler = proc_dointvec, 1824 .proc_handler = proc_dointvec,
1810 }, 1825 },
1826 {
1827 .procname = "conn_reuse_mode",
1828 .maxlen = sizeof(int),
1829 .mode = 0644,
1830 .proc_handler = proc_dointvec,
1831 },
1811#ifdef CONFIG_IP_VS_DEBUG 1832#ifdef CONFIG_IP_VS_DEBUG
1812 { 1833 {
1813 .procname = "debug_level", 1834 .procname = "debug_level",
@@ -2044,7 +2065,7 @@ static const struct file_operations ip_vs_info_fops = {
2044static int ip_vs_stats_show(struct seq_file *seq, void *v) 2065static int ip_vs_stats_show(struct seq_file *seq, void *v)
2045{ 2066{
2046 struct net *net = seq_file_single_net(seq); 2067 struct net *net = seq_file_single_net(seq);
2047 struct ip_vs_stats_user show; 2068 struct ip_vs_kstats show;
2048 2069
2049/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ 2070/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2050 seq_puts(seq, 2071 seq_puts(seq,
@@ -2053,17 +2074,22 @@ static int ip_vs_stats_show(struct seq_file *seq, void *v)
2053 " Conns Packets Packets Bytes Bytes\n"); 2074 " Conns Packets Packets Bytes Bytes\n");
2054 2075
2055 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats); 2076 ip_vs_copy_stats(&show, &net_ipvs(net)->tot_stats);
2056 seq_printf(seq, "%8X %8X %8X %16LX %16LX\n\n", show.conns, 2077 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n\n",
2057 show.inpkts, show.outpkts, 2078 (unsigned long long)show.conns,
2058 (unsigned long long) show.inbytes, 2079 (unsigned long long)show.inpkts,
2059 (unsigned long long) show.outbytes); 2080 (unsigned long long)show.outpkts,
2060 2081 (unsigned long long)show.inbytes,
2061/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ 2082 (unsigned long long)show.outbytes);
2083
2084/* 01234567 01234567 01234567 0123456701234567 0123456701234567*/
2062 seq_puts(seq, 2085 seq_puts(seq,
2063 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n"); 2086 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2064 seq_printf(seq, "%8X %8X %8X %16X %16X\n", 2087 seq_printf(seq, "%8LX %8LX %8LX %16LX %16LX\n",
2065 show.cps, show.inpps, show.outpps, 2088 (unsigned long long)show.cps,
2066 show.inbps, show.outbps); 2089 (unsigned long long)show.inpps,
2090 (unsigned long long)show.outpps,
2091 (unsigned long long)show.inbps,
2092 (unsigned long long)show.outbps);
2067 2093
2068 return 0; 2094 return 0;
2069} 2095}
@@ -2086,7 +2112,7 @@ static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2086 struct net *net = seq_file_single_net(seq); 2112 struct net *net = seq_file_single_net(seq);
2087 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats; 2113 struct ip_vs_stats *tot_stats = &net_ipvs(net)->tot_stats;
2088 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats; 2114 struct ip_vs_cpu_stats __percpu *cpustats = tot_stats->cpustats;
2089 struct ip_vs_stats_user rates; 2115 struct ip_vs_kstats kstats;
2090 int i; 2116 int i;
2091 2117
2092/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ 2118/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */
@@ -2098,41 +2124,41 @@ static int ip_vs_stats_percpu_show(struct seq_file *seq, void *v)
2098 for_each_possible_cpu(i) { 2124 for_each_possible_cpu(i) {
2099 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i); 2125 struct ip_vs_cpu_stats *u = per_cpu_ptr(cpustats, i);
2100 unsigned int start; 2126 unsigned int start;
2101 __u64 inbytes, outbytes; 2127 u64 conns, inpkts, outpkts, inbytes, outbytes;
2102 2128
2103 do { 2129 do {
2104 start = u64_stats_fetch_begin_irq(&u->syncp); 2130 start = u64_stats_fetch_begin_irq(&u->syncp);
2105 inbytes = u->ustats.inbytes; 2131 conns = u->cnt.conns;
2106 outbytes = u->ustats.outbytes; 2132 inpkts = u->cnt.inpkts;
2133 outpkts = u->cnt.outpkts;
2134 inbytes = u->cnt.inbytes;
2135 outbytes = u->cnt.outbytes;
2107 } while (u64_stats_fetch_retry_irq(&u->syncp, start)); 2136 } while (u64_stats_fetch_retry_irq(&u->syncp, start));
2108 2137
2109 seq_printf(seq, "%3X %8X %8X %8X %16LX %16LX\n", 2138 seq_printf(seq, "%3X %8LX %8LX %8LX %16LX %16LX\n",
2110 i, u->ustats.conns, u->ustats.inpkts, 2139 i, (u64)conns, (u64)inpkts,
2111 u->ustats.outpkts, (__u64)inbytes, 2140 (u64)outpkts, (u64)inbytes,
2112 (__u64)outbytes); 2141 (u64)outbytes);
2113 } 2142 }
2114 2143
2115 spin_lock_bh(&tot_stats->lock); 2144 ip_vs_copy_stats(&kstats, tot_stats);
2116
2117 seq_printf(seq, " ~ %8X %8X %8X %16LX %16LX\n\n",
2118 tot_stats->ustats.conns, tot_stats->ustats.inpkts,
2119 tot_stats->ustats.outpkts,
2120 (unsigned long long) tot_stats->ustats.inbytes,
2121 (unsigned long long) tot_stats->ustats.outbytes);
2122
2123 ip_vs_read_estimator(&rates, tot_stats);
2124 2145
2125 spin_unlock_bh(&tot_stats->lock); 2146 seq_printf(seq, " ~ %8LX %8LX %8LX %16LX %16LX\n\n",
2147 (unsigned long long)kstats.conns,
2148 (unsigned long long)kstats.inpkts,
2149 (unsigned long long)kstats.outpkts,
2150 (unsigned long long)kstats.inbytes,
2151 (unsigned long long)kstats.outbytes);
2126 2152
2127/* 01234567 01234567 01234567 0123456701234567 0123456701234567 */ 2153/* ... 01234567 01234567 01234567 0123456701234567 0123456701234567 */
2128 seq_puts(seq, 2154 seq_puts(seq,
2129 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n"); 2155 " Conns/s Pkts/s Pkts/s Bytes/s Bytes/s\n");
2130 seq_printf(seq, " %8X %8X %8X %16X %16X\n", 2156 seq_printf(seq, " %8LX %8LX %8LX %16LX %16LX\n",
2131 rates.cps, 2157 kstats.cps,
2132 rates.inpps, 2158 kstats.inpps,
2133 rates.outpps, 2159 kstats.outpps,
2134 rates.inbps, 2160 kstats.inbps,
2135 rates.outbps); 2161 kstats.outbps);
2136 2162
2137 return 0; 2163 return 0;
2138} 2164}
@@ -2400,6 +2426,7 @@ static void
2400ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src) 2426ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2401{ 2427{
2402 struct ip_vs_scheduler *sched; 2428 struct ip_vs_scheduler *sched;
2429 struct ip_vs_kstats kstats;
2403 2430
2404 sched = rcu_dereference_protected(src->scheduler, 1); 2431 sched = rcu_dereference_protected(src->scheduler, 1);
2405 dst->protocol = src->protocol; 2432 dst->protocol = src->protocol;
@@ -2411,7 +2438,8 @@ ip_vs_copy_service(struct ip_vs_service_entry *dst, struct ip_vs_service *src)
2411 dst->timeout = src->timeout / HZ; 2438 dst->timeout = src->timeout / HZ;
2412 dst->netmask = src->netmask; 2439 dst->netmask = src->netmask;
2413 dst->num_dests = src->num_dests; 2440 dst->num_dests = src->num_dests;
2414 ip_vs_copy_stats(&dst->stats, &src->stats); 2441 ip_vs_copy_stats(&kstats, &src->stats);
2442 ip_vs_export_stats_user(&dst->stats, &kstats);
2415} 2443}
2416 2444
2417static inline int 2445static inline int
@@ -2485,6 +2513,7 @@ __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2485 int count = 0; 2513 int count = 0;
2486 struct ip_vs_dest *dest; 2514 struct ip_vs_dest *dest;
2487 struct ip_vs_dest_entry entry; 2515 struct ip_vs_dest_entry entry;
2516 struct ip_vs_kstats kstats;
2488 2517
2489 memset(&entry, 0, sizeof(entry)); 2518 memset(&entry, 0, sizeof(entry));
2490 list_for_each_entry(dest, &svc->destinations, n_list) { 2519 list_for_each_entry(dest, &svc->destinations, n_list) {
@@ -2506,7 +2535,8 @@ __ip_vs_get_dest_entries(struct net *net, const struct ip_vs_get_dests *get,
2506 entry.activeconns = atomic_read(&dest->activeconns); 2535 entry.activeconns = atomic_read(&dest->activeconns);
2507 entry.inactconns = atomic_read(&dest->inactconns); 2536 entry.inactconns = atomic_read(&dest->inactconns);
2508 entry.persistconns = atomic_read(&dest->persistconns); 2537 entry.persistconns = atomic_read(&dest->persistconns);
2509 ip_vs_copy_stats(&entry.stats, &dest->stats); 2538 ip_vs_copy_stats(&kstats, &dest->stats);
2539 ip_vs_export_stats_user(&entry.stats, &kstats);
2510 if (copy_to_user(&uptr->entrytable[count], 2540 if (copy_to_user(&uptr->entrytable[count],
2511 &entry, sizeof(entry))) { 2541 &entry, sizeof(entry))) {
2512 ret = -EFAULT; 2542 ret = -EFAULT;
@@ -2798,25 +2828,51 @@ static const struct nla_policy ip_vs_dest_policy[IPVS_DEST_ATTR_MAX + 1] = {
2798}; 2828};
2799 2829
2800static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type, 2830static int ip_vs_genl_fill_stats(struct sk_buff *skb, int container_type,
2801 struct ip_vs_stats *stats) 2831 struct ip_vs_kstats *kstats)
2802{ 2832{
2803 struct ip_vs_stats_user ustats;
2804 struct nlattr *nl_stats = nla_nest_start(skb, container_type); 2833 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2834
2805 if (!nl_stats) 2835 if (!nl_stats)
2806 return -EMSGSIZE; 2836 return -EMSGSIZE;
2807 2837
2808 ip_vs_copy_stats(&ustats, stats); 2838 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, (u32)kstats->conns) ||
2809 2839 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, (u32)kstats->inpkts) ||
2810 if (nla_put_u32(skb, IPVS_STATS_ATTR_CONNS, ustats.conns) || 2840 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, (u32)kstats->outpkts) ||
2811 nla_put_u32(skb, IPVS_STATS_ATTR_INPKTS, ustats.inpkts) || 2841 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2812 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPKTS, ustats.outpkts) || 2842 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2813 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, ustats.inbytes) || 2843 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, (u32)kstats->cps) ||
2814 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, ustats.outbytes) || 2844 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, (u32)kstats->inpps) ||
2815 nla_put_u32(skb, IPVS_STATS_ATTR_CPS, ustats.cps) || 2845 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, (u32)kstats->outpps) ||
2816 nla_put_u32(skb, IPVS_STATS_ATTR_INPPS, ustats.inpps) || 2846 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, (u32)kstats->inbps) ||
2817 nla_put_u32(skb, IPVS_STATS_ATTR_OUTPPS, ustats.outpps) || 2847 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, (u32)kstats->outbps))
2818 nla_put_u32(skb, IPVS_STATS_ATTR_INBPS, ustats.inbps) || 2848 goto nla_put_failure;
2819 nla_put_u32(skb, IPVS_STATS_ATTR_OUTBPS, ustats.outbps)) 2849 nla_nest_end(skb, nl_stats);
2850
2851 return 0;
2852
2853nla_put_failure:
2854 nla_nest_cancel(skb, nl_stats);
2855 return -EMSGSIZE;
2856}
2857
2858static int ip_vs_genl_fill_stats64(struct sk_buff *skb, int container_type,
2859 struct ip_vs_kstats *kstats)
2860{
2861 struct nlattr *nl_stats = nla_nest_start(skb, container_type);
2862
2863 if (!nl_stats)
2864 return -EMSGSIZE;
2865
2866 if (nla_put_u64(skb, IPVS_STATS_ATTR_CONNS, kstats->conns) ||
2867 nla_put_u64(skb, IPVS_STATS_ATTR_INPKTS, kstats->inpkts) ||
2868 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPKTS, kstats->outpkts) ||
2869 nla_put_u64(skb, IPVS_STATS_ATTR_INBYTES, kstats->inbytes) ||
2870 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBYTES, kstats->outbytes) ||
2871 nla_put_u64(skb, IPVS_STATS_ATTR_CPS, kstats->cps) ||
2872 nla_put_u64(skb, IPVS_STATS_ATTR_INPPS, kstats->inpps) ||
2873 nla_put_u64(skb, IPVS_STATS_ATTR_OUTPPS, kstats->outpps) ||
2874 nla_put_u64(skb, IPVS_STATS_ATTR_INBPS, kstats->inbps) ||
2875 nla_put_u64(skb, IPVS_STATS_ATTR_OUTBPS, kstats->outbps))
2820 goto nla_put_failure; 2876 goto nla_put_failure;
2821 nla_nest_end(skb, nl_stats); 2877 nla_nest_end(skb, nl_stats);
2822 2878
@@ -2835,6 +2891,7 @@ static int ip_vs_genl_fill_service(struct sk_buff *skb,
2835 struct nlattr *nl_service; 2891 struct nlattr *nl_service;
2836 struct ip_vs_flags flags = { .flags = svc->flags, 2892 struct ip_vs_flags flags = { .flags = svc->flags,
2837 .mask = ~0 }; 2893 .mask = ~0 };
2894 struct ip_vs_kstats kstats;
2838 2895
2839 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE); 2896 nl_service = nla_nest_start(skb, IPVS_CMD_ATTR_SERVICE);
2840 if (!nl_service) 2897 if (!nl_service)
@@ -2860,7 +2917,10 @@ static int ip_vs_genl_fill_service(struct sk_buff *skb,
2860 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) || 2917 nla_put_u32(skb, IPVS_SVC_ATTR_TIMEOUT, svc->timeout / HZ) ||
2861 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask)) 2918 nla_put_be32(skb, IPVS_SVC_ATTR_NETMASK, svc->netmask))
2862 goto nla_put_failure; 2919 goto nla_put_failure;
2863 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &svc->stats)) 2920 ip_vs_copy_stats(&kstats, &svc->stats);
2921 if (ip_vs_genl_fill_stats(skb, IPVS_SVC_ATTR_STATS, &kstats))
2922 goto nla_put_failure;
2923 if (ip_vs_genl_fill_stats64(skb, IPVS_SVC_ATTR_STATS64, &kstats))
2864 goto nla_put_failure; 2924 goto nla_put_failure;
2865 2925
2866 nla_nest_end(skb, nl_service); 2926 nla_nest_end(skb, nl_service);
@@ -3032,6 +3092,7 @@ static struct ip_vs_service *ip_vs_genl_find_service(struct net *net,
3032static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest) 3092static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3033{ 3093{
3034 struct nlattr *nl_dest; 3094 struct nlattr *nl_dest;
3095 struct ip_vs_kstats kstats;
3035 3096
3036 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST); 3097 nl_dest = nla_nest_start(skb, IPVS_CMD_ATTR_DEST);
3037 if (!nl_dest) 3098 if (!nl_dest)
@@ -3054,7 +3115,10 @@ static int ip_vs_genl_fill_dest(struct sk_buff *skb, struct ip_vs_dest *dest)
3054 atomic_read(&dest->persistconns)) || 3115 atomic_read(&dest->persistconns)) ||
3055 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af)) 3116 nla_put_u16(skb, IPVS_DEST_ATTR_ADDR_FAMILY, dest->af))
3056 goto nla_put_failure; 3117 goto nla_put_failure;
3057 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &dest->stats)) 3118 ip_vs_copy_stats(&kstats, &dest->stats);
3119 if (ip_vs_genl_fill_stats(skb, IPVS_DEST_ATTR_STATS, &kstats))
3120 goto nla_put_failure;
3121 if (ip_vs_genl_fill_stats64(skb, IPVS_DEST_ATTR_STATS64, &kstats))
3058 goto nla_put_failure; 3122 goto nla_put_failure;
3059 3123
3060 nla_nest_end(skb, nl_dest); 3124 nla_nest_end(skb, nl_dest);
@@ -3732,6 +3796,8 @@ static int __net_init ip_vs_control_net_init_sysctl(struct net *net)
3732 ipvs->sysctl_pmtu_disc = 1; 3796 ipvs->sysctl_pmtu_disc = 1;
3733 tbl[idx++].data = &ipvs->sysctl_pmtu_disc; 3797 tbl[idx++].data = &ipvs->sysctl_pmtu_disc;
3734 tbl[idx++].data = &ipvs->sysctl_backup_only; 3798 tbl[idx++].data = &ipvs->sysctl_backup_only;
3799 ipvs->sysctl_conn_reuse_mode = 1;
3800 tbl[idx++].data = &ipvs->sysctl_conn_reuse_mode;
3735 3801
3736 3802
3737 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl); 3803 ipvs->sysctl_hdr = register_net_sysctl(net, "net/ipv4/vs", tbl);
diff --git a/net/netfilter/ipvs/ip_vs_est.c b/net/netfilter/ipvs/ip_vs_est.c
index 1425e9a924c4..ef0eb0a8d552 100644
--- a/net/netfilter/ipvs/ip_vs_est.c
+++ b/net/netfilter/ipvs/ip_vs_est.c
@@ -45,17 +45,19 @@
45 45
46 NOTES. 46 NOTES.
47 47
48 * The stored value for average bps is scaled by 2^5, so that maximal 48 * Average bps is scaled by 2^5, while average pps and cps are scaled by 2^10.
49 rate is ~2.15Gbits/s, average pps and cps are scaled by 2^10.
50 49
51 * A lot code is taken from net/sched/estimator.c 50 * Netlink users can see 64-bit values but sockopt users are restricted
51 to 32-bit values for conns, packets, bps, cps and pps.
52
53 * A lot of code is taken from net/core/gen_estimator.c
52 */ 54 */
53 55
54 56
55/* 57/*
56 * Make a summary from each cpu 58 * Make a summary from each cpu
57 */ 59 */
58static void ip_vs_read_cpu_stats(struct ip_vs_stats_user *sum, 60static void ip_vs_read_cpu_stats(struct ip_vs_kstats *sum,
59 struct ip_vs_cpu_stats __percpu *stats) 61 struct ip_vs_cpu_stats __percpu *stats)
60{ 62{
61 int i; 63 int i;
@@ -64,27 +66,31 @@ static void ip_vs_read_cpu_stats(struct ip_vs_stats_user *sum,
64 for_each_possible_cpu(i) { 66 for_each_possible_cpu(i) {
65 struct ip_vs_cpu_stats *s = per_cpu_ptr(stats, i); 67 struct ip_vs_cpu_stats *s = per_cpu_ptr(stats, i);
66 unsigned int start; 68 unsigned int start;
67 __u64 inbytes, outbytes; 69 u64 conns, inpkts, outpkts, inbytes, outbytes;
70
68 if (add) { 71 if (add) {
69 sum->conns += s->ustats.conns;
70 sum->inpkts += s->ustats.inpkts;
71 sum->outpkts += s->ustats.outpkts;
72 do { 72 do {
73 start = u64_stats_fetch_begin(&s->syncp); 73 start = u64_stats_fetch_begin(&s->syncp);
74 inbytes = s->ustats.inbytes; 74 conns = s->cnt.conns;
75 outbytes = s->ustats.outbytes; 75 inpkts = s->cnt.inpkts;
76 outpkts = s->cnt.outpkts;
77 inbytes = s->cnt.inbytes;
78 outbytes = s->cnt.outbytes;
76 } while (u64_stats_fetch_retry(&s->syncp, start)); 79 } while (u64_stats_fetch_retry(&s->syncp, start));
80 sum->conns += conns;
81 sum->inpkts += inpkts;
82 sum->outpkts += outpkts;
77 sum->inbytes += inbytes; 83 sum->inbytes += inbytes;
78 sum->outbytes += outbytes; 84 sum->outbytes += outbytes;
79 } else { 85 } else {
80 add = true; 86 add = true;
81 sum->conns = s->ustats.conns;
82 sum->inpkts = s->ustats.inpkts;
83 sum->outpkts = s->ustats.outpkts;
84 do { 87 do {
85 start = u64_stats_fetch_begin(&s->syncp); 88 start = u64_stats_fetch_begin(&s->syncp);
86 sum->inbytes = s->ustats.inbytes; 89 sum->conns = s->cnt.conns;
87 sum->outbytes = s->ustats.outbytes; 90 sum->inpkts = s->cnt.inpkts;
91 sum->outpkts = s->cnt.outpkts;
92 sum->inbytes = s->cnt.inbytes;
93 sum->outbytes = s->cnt.outbytes;
88 } while (u64_stats_fetch_retry(&s->syncp, start)); 94 } while (u64_stats_fetch_retry(&s->syncp, start));
89 } 95 }
90 } 96 }
@@ -95,10 +101,7 @@ static void estimation_timer(unsigned long arg)
95{ 101{
96 struct ip_vs_estimator *e; 102 struct ip_vs_estimator *e;
97 struct ip_vs_stats *s; 103 struct ip_vs_stats *s;
98 u32 n_conns; 104 u64 rate;
99 u32 n_inpkts, n_outpkts;
100 u64 n_inbytes, n_outbytes;
101 u32 rate;
102 struct net *net = (struct net *)arg; 105 struct net *net = (struct net *)arg;
103 struct netns_ipvs *ipvs; 106 struct netns_ipvs *ipvs;
104 107
@@ -108,33 +111,29 @@ static void estimation_timer(unsigned long arg)
108 s = container_of(e, struct ip_vs_stats, est); 111 s = container_of(e, struct ip_vs_stats, est);
109 112
110 spin_lock(&s->lock); 113 spin_lock(&s->lock);
111 ip_vs_read_cpu_stats(&s->ustats, s->cpustats); 114 ip_vs_read_cpu_stats(&s->kstats, s->cpustats);
112 n_conns = s->ustats.conns;
113 n_inpkts = s->ustats.inpkts;
114 n_outpkts = s->ustats.outpkts;
115 n_inbytes = s->ustats.inbytes;
116 n_outbytes = s->ustats.outbytes;
117 115
118 /* scaled by 2^10, but divided 2 seconds */ 116 /* scaled by 2^10, but divided 2 seconds */
119 rate = (n_conns - e->last_conns) << 9; 117 rate = (s->kstats.conns - e->last_conns) << 9;
120 e->last_conns = n_conns; 118 e->last_conns = s->kstats.conns;
121 e->cps += ((long)rate - (long)e->cps) >> 2; 119 e->cps += ((s64)rate - (s64)e->cps) >> 2;
122 120
123 rate = (n_inpkts - e->last_inpkts) << 9; 121 rate = (s->kstats.inpkts - e->last_inpkts) << 9;
124 e->last_inpkts = n_inpkts; 122 e->last_inpkts = s->kstats.inpkts;
125 e->inpps += ((long)rate - (long)e->inpps) >> 2; 123 e->inpps += ((s64)rate - (s64)e->inpps) >> 2;
126 124
127 rate = (n_outpkts - e->last_outpkts) << 9; 125 rate = (s->kstats.outpkts - e->last_outpkts) << 9;
128 e->last_outpkts = n_outpkts; 126 e->last_outpkts = s->kstats.outpkts;
129 e->outpps += ((long)rate - (long)e->outpps) >> 2; 127 e->outpps += ((s64)rate - (s64)e->outpps) >> 2;
130 128
131 rate = (n_inbytes - e->last_inbytes) << 4; 129 /* scaled by 2^5, but divided 2 seconds */
132 e->last_inbytes = n_inbytes; 130 rate = (s->kstats.inbytes - e->last_inbytes) << 4;
133 e->inbps += ((long)rate - (long)e->inbps) >> 2; 131 e->last_inbytes = s->kstats.inbytes;
134 132 e->inbps += ((s64)rate - (s64)e->inbps) >> 2;
135 rate = (n_outbytes - e->last_outbytes) << 4; 133
136 e->last_outbytes = n_outbytes; 134 rate = (s->kstats.outbytes - e->last_outbytes) << 4;
137 e->outbps += ((long)rate - (long)e->outbps) >> 2; 135 e->last_outbytes = s->kstats.outbytes;
136 e->outbps += ((s64)rate - (s64)e->outbps) >> 2;
138 spin_unlock(&s->lock); 137 spin_unlock(&s->lock);
139 } 138 }
140 spin_unlock(&ipvs->est_lock); 139 spin_unlock(&ipvs->est_lock);
@@ -166,14 +165,14 @@ void ip_vs_stop_estimator(struct net *net, struct ip_vs_stats *stats)
166void ip_vs_zero_estimator(struct ip_vs_stats *stats) 165void ip_vs_zero_estimator(struct ip_vs_stats *stats)
167{ 166{
168 struct ip_vs_estimator *est = &stats->est; 167 struct ip_vs_estimator *est = &stats->est;
169 struct ip_vs_stats_user *u = &stats->ustats; 168 struct ip_vs_kstats *k = &stats->kstats;
170 169
171 /* reset counters, caller must hold the stats->lock lock */ 170 /* reset counters, caller must hold the stats->lock lock */
172 est->last_inbytes = u->inbytes; 171 est->last_inbytes = k->inbytes;
173 est->last_outbytes = u->outbytes; 172 est->last_outbytes = k->outbytes;
174 est->last_conns = u->conns; 173 est->last_conns = k->conns;
175 est->last_inpkts = u->inpkts; 174 est->last_inpkts = k->inpkts;
176 est->last_outpkts = u->outpkts; 175 est->last_outpkts = k->outpkts;
177 est->cps = 0; 176 est->cps = 0;
178 est->inpps = 0; 177 est->inpps = 0;
179 est->outpps = 0; 178 est->outpps = 0;
@@ -182,8 +181,7 @@ void ip_vs_zero_estimator(struct ip_vs_stats *stats)
182} 181}
183 182
184/* Get decoded rates */ 183/* Get decoded rates */
185void ip_vs_read_estimator(struct ip_vs_stats_user *dst, 184void ip_vs_read_estimator(struct ip_vs_kstats *dst, struct ip_vs_stats *stats)
186 struct ip_vs_stats *stats)
187{ 185{
188 struct ip_vs_estimator *e = &stats->est; 186 struct ip_vs_estimator *e = &stats->est;
189 187
diff --git a/net/netfilter/ipvs/ip_vs_sync.c b/net/netfilter/ipvs/ip_vs_sync.c
index d93ceeb3ef04..19b9cce6c210 100644
--- a/net/netfilter/ipvs/ip_vs_sync.c
+++ b/net/netfilter/ipvs/ip_vs_sync.c
@@ -845,10 +845,27 @@ static void ip_vs_proc_conn(struct net *net, struct ip_vs_conn_param *param,
845 struct ip_vs_conn *cp; 845 struct ip_vs_conn *cp;
846 struct netns_ipvs *ipvs = net_ipvs(net); 846 struct netns_ipvs *ipvs = net_ipvs(net);
847 847
848 if (!(flags & IP_VS_CONN_F_TEMPLATE)) 848 if (!(flags & IP_VS_CONN_F_TEMPLATE)) {
849 cp = ip_vs_conn_in_get(param); 849 cp = ip_vs_conn_in_get(param);
850 else 850 if (cp && ((cp->dport != dport) ||
851 !ip_vs_addr_equal(cp->daf, &cp->daddr, daddr))) {
852 if (!(flags & IP_VS_CONN_F_INACTIVE)) {
853 ip_vs_conn_expire_now(cp);
854 __ip_vs_conn_put(cp);
855 cp = NULL;
856 } else {
857 /* This is the expiration message for the
858 * connection that was already replaced, so we
859 * just ignore it.
860 */
861 __ip_vs_conn_put(cp);
862 kfree(param->pe_data);
863 return;
864 }
865 }
866 } else {
851 cp = ip_vs_ct_in_get(param); 867 cp = ip_vs_ct_in_get(param);
868 }
852 869
853 if (cp) { 870 if (cp) {
854 /* Free pe_data */ 871 /* Free pe_data */
@@ -1388,9 +1405,11 @@ join_mcast_group(struct sock *sk, struct in_addr *addr, char *ifname)
1388 1405
1389 mreq.imr_ifindex = dev->ifindex; 1406 mreq.imr_ifindex = dev->ifindex;
1390 1407
1408 rtnl_lock();
1391 lock_sock(sk); 1409 lock_sock(sk);
1392 ret = ip_mc_join_group(sk, &mreq); 1410 ret = ip_mc_join_group(sk, &mreq);
1393 release_sock(sk); 1411 release_sock(sk);
1412 rtnl_unlock();
1394 1413
1395 return ret; 1414 return ret;
1396} 1415}
diff --git a/net/netfilter/ipvs/ip_vs_xmit.c b/net/netfilter/ipvs/ip_vs_xmit.c
index 3aedbda7658a..19986ec5f21a 100644
--- a/net/netfilter/ipvs/ip_vs_xmit.c
+++ b/net/netfilter/ipvs/ip_vs_xmit.c
@@ -209,7 +209,7 @@ static inline void maybe_update_pmtu(int skb_af, struct sk_buff *skb, int mtu)
209 struct sock *sk = skb->sk; 209 struct sock *sk = skb->sk;
210 struct rtable *ort = skb_rtable(skb); 210 struct rtable *ort = skb_rtable(skb);
211 211
212 if (!skb->dev && sk && sk->sk_state != TCP_TIME_WAIT) 212 if (!skb->dev && sk && sk_fullsock(sk))
213 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu); 213 ort->dst.ops->update_pmtu(&ort->dst, sk, NULL, mtu);
214} 214}
215 215
@@ -536,8 +536,8 @@ static inline int ip_vs_nat_send_or_cont(int pf, struct sk_buff *skb,
536 ip_vs_update_conntrack(skb, cp, 1); 536 ip_vs_update_conntrack(skb, cp, 1);
537 if (!local) { 537 if (!local) {
538 skb_forward_csum(skb); 538 skb_forward_csum(skb);
539 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev, 539 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
540 dst_output); 540 NULL, skb_dst(skb)->dev, dst_output_sk);
541 } else 541 } else
542 ret = NF_ACCEPT; 542 ret = NF_ACCEPT;
543 return ret; 543 return ret;
@@ -554,8 +554,8 @@ static inline int ip_vs_send_or_cont(int pf, struct sk_buff *skb,
554 ip_vs_notrack(skb); 554 ip_vs_notrack(skb);
555 if (!local) { 555 if (!local) {
556 skb_forward_csum(skb); 556 skb_forward_csum(skb);
557 NF_HOOK(pf, NF_INET_LOCAL_OUT, skb, NULL, skb_dst(skb)->dev, 557 NF_HOOK(pf, NF_INET_LOCAL_OUT, NULL, skb,
558 dst_output); 558 NULL, skb_dst(skb)->dev, dst_output_sk);
559 } else 559 } else
560 ret = NF_ACCEPT; 560 ret = NF_ACCEPT;
561 return ret; 561 return ret;
@@ -924,7 +924,8 @@ int
924ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp, 924ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
925 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh) 925 struct ip_vs_protocol *pp, struct ip_vs_iphdr *ipvsh)
926{ 926{
927 struct netns_ipvs *ipvs = net_ipvs(skb_net(skb)); 927 struct net *net = skb_net(skb);
928 struct netns_ipvs *ipvs = net_ipvs(net);
928 struct rtable *rt; /* Route to the other host */ 929 struct rtable *rt; /* Route to the other host */
929 __be32 saddr; /* Source for tunnel */ 930 __be32 saddr; /* Source for tunnel */
930 struct net_device *tdev; /* Device to other host */ 931 struct net_device *tdev; /* Device to other host */
@@ -991,7 +992,7 @@ ip_vs_tunnel_xmit(struct sk_buff *skb, struct ip_vs_conn *cp,
991 iph->daddr = cp->daddr.ip; 992 iph->daddr = cp->daddr.ip;
992 iph->saddr = saddr; 993 iph->saddr = saddr;
993 iph->ttl = ttl; 994 iph->ttl = ttl;
994 ip_select_ident(skb, NULL); 995 ip_select_ident(net, skb, NULL);
995 996
996 /* Another hack: avoid icmp_send in ip_fragment */ 997 /* Another hack: avoid icmp_send in ip_fragment */
997 skb->ignore_df = 1; 998 skb->ignore_df = 1;
diff --git a/net/netfilter/nf_conntrack_acct.c b/net/netfilter/nf_conntrack_acct.c
index a4b5e2a435ac..45da11afa785 100644
--- a/net/netfilter/nf_conntrack_acct.c
+++ b/net/netfilter/nf_conntrack_acct.c
@@ -47,9 +47,11 @@ seq_print_acct(struct seq_file *s, const struct nf_conn *ct, int dir)
47 return 0; 47 return 0;
48 48
49 counter = acct->counter; 49 counter = acct->counter;
50 return seq_printf(s, "packets=%llu bytes=%llu ", 50 seq_printf(s, "packets=%llu bytes=%llu ",
51 (unsigned long long)atomic64_read(&counter[dir].packets), 51 (unsigned long long)atomic64_read(&counter[dir].packets),
52 (unsigned long long)atomic64_read(&counter[dir].bytes)); 52 (unsigned long long)atomic64_read(&counter[dir].bytes));
53
54 return 0;
53}; 55};
54EXPORT_SYMBOL_GPL(seq_print_acct); 56EXPORT_SYMBOL_GPL(seq_print_acct);
55 57
diff --git a/net/netfilter/nf_conntrack_amanda.c b/net/netfilter/nf_conntrack_amanda.c
index b8b95f4027ca..57a26cc90c9f 100644
--- a/net/netfilter/nf_conntrack_amanda.c
+++ b/net/netfilter/nf_conntrack_amanda.c
@@ -88,7 +88,6 @@ static int amanda_help(struct sk_buff *skb,
88 struct nf_conn *ct, 88 struct nf_conn *ct,
89 enum ip_conntrack_info ctinfo) 89 enum ip_conntrack_info ctinfo)
90{ 90{
91 struct ts_state ts;
92 struct nf_conntrack_expect *exp; 91 struct nf_conntrack_expect *exp;
93 struct nf_conntrack_tuple *tuple; 92 struct nf_conntrack_tuple *tuple;
94 unsigned int dataoff, start, stop, off, i; 93 unsigned int dataoff, start, stop, off, i;
@@ -113,23 +112,20 @@ static int amanda_help(struct sk_buff *skb,
113 return NF_ACCEPT; 112 return NF_ACCEPT;
114 } 113 }
115 114
116 memset(&ts, 0, sizeof(ts));
117 start = skb_find_text(skb, dataoff, skb->len, 115 start = skb_find_text(skb, dataoff, skb->len,
118 search[SEARCH_CONNECT].ts, &ts); 116 search[SEARCH_CONNECT].ts);
119 if (start == UINT_MAX) 117 if (start == UINT_MAX)
120 goto out; 118 goto out;
121 start += dataoff + search[SEARCH_CONNECT].len; 119 start += dataoff + search[SEARCH_CONNECT].len;
122 120
123 memset(&ts, 0, sizeof(ts));
124 stop = skb_find_text(skb, start, skb->len, 121 stop = skb_find_text(skb, start, skb->len,
125 search[SEARCH_NEWLINE].ts, &ts); 122 search[SEARCH_NEWLINE].ts);
126 if (stop == UINT_MAX) 123 if (stop == UINT_MAX)
127 goto out; 124 goto out;
128 stop += start; 125 stop += start;
129 126
130 for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) { 127 for (i = SEARCH_DATA; i <= SEARCH_INDEX; i++) {
131 memset(&ts, 0, sizeof(ts)); 128 off = skb_find_text(skb, start, stop, search[i].ts);
132 off = skb_find_text(skb, start, stop, search[i].ts, &ts);
133 if (off == UINT_MAX) 129 if (off == UINT_MAX)
134 continue; 130 continue;
135 off += start + search[i].len; 131 off += start + search[i].len;
diff --git a/net/netfilter/nf_conntrack_expect.c b/net/netfilter/nf_conntrack_expect.c
index 91a1837acd0e..7a17070c5dab 100644
--- a/net/netfilter/nf_conntrack_expect.c
+++ b/net/netfilter/nf_conntrack_expect.c
@@ -561,7 +561,9 @@ static int exp_seq_show(struct seq_file *s, void *v)
561 helper->expect_policy[expect->class].name); 561 helper->expect_policy[expect->class].name);
562 } 562 }
563 563
564 return seq_putc(s, '\n'); 564 seq_putc(s, '\n');
565
566 return 0;
565} 567}
566 568
567static const struct seq_operations exp_seq_ops = { 569static const struct seq_operations exp_seq_ops = {
diff --git a/net/netfilter/nf_internals.h b/net/netfilter/nf_internals.h
index 61a3c927e63c..ea7f36784b3d 100644
--- a/net/netfilter/nf_internals.h
+++ b/net/netfilter/nf_internals.h
@@ -14,16 +14,11 @@
14 14
15/* core.c */ 15/* core.c */
16unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb, 16unsigned int nf_iterate(struct list_head *head, struct sk_buff *skb,
17 unsigned int hook, const struct net_device *indev, 17 struct nf_hook_state *state, struct nf_hook_ops **elemp);
18 const struct net_device *outdev,
19 struct nf_hook_ops **elemp,
20 int (*okfn)(struct sk_buff *), int hook_thresh);
21 18
22/* nf_queue.c */ 19/* nf_queue.c */
23int nf_queue(struct sk_buff *skb, struct nf_hook_ops *elem, u_int8_t pf, 20int nf_queue(struct sk_buff *skb, struct nf_hook_ops *elem,
24 unsigned int hook, struct net_device *indev, 21 struct nf_hook_state *state, unsigned int queuenum);
25 struct net_device *outdev, int (*okfn)(struct sk_buff *),
26 unsigned int queuenum);
27int __init netfilter_queue_init(void); 22int __init netfilter_queue_init(void);
28 23
29/* nf_log.c */ 24/* nf_log.c */
diff --git a/net/netfilter/nf_log_common.c b/net/netfilter/nf_log_common.c
index a2233e77cf39..a5aa5967b8e1 100644
--- a/net/netfilter/nf_log_common.c
+++ b/net/netfilter/nf_log_common.c
@@ -17,6 +17,7 @@
17#include <net/route.h> 17#include <net/route.h>
18 18
19#include <linux/netfilter.h> 19#include <linux/netfilter.h>
20#include <linux/netfilter_bridge.h>
20#include <linux/netfilter/xt_LOG.h> 21#include <linux/netfilter/xt_LOG.h>
21#include <net/netfilter/nf_log.h> 22#include <net/netfilter/nf_log.h>
22 23
@@ -133,7 +134,7 @@ EXPORT_SYMBOL_GPL(nf_log_dump_tcp_header);
133 134
134void nf_log_dump_sk_uid_gid(struct nf_log_buf *m, struct sock *sk) 135void nf_log_dump_sk_uid_gid(struct nf_log_buf *m, struct sock *sk)
135{ 136{
136 if (!sk || sk->sk_state == TCP_TIME_WAIT) 137 if (!sk || !sk_fullsock(sk))
137 return; 138 return;
138 139
139 read_lock_bh(&sk->sk_callback_lock); 140 read_lock_bh(&sk->sk_callback_lock);
@@ -163,10 +164,10 @@ nf_log_dump_packet_common(struct nf_log_buf *m, u_int8_t pf,
163 const struct net_device *physindev; 164 const struct net_device *physindev;
164 const struct net_device *physoutdev; 165 const struct net_device *physoutdev;
165 166
166 physindev = skb->nf_bridge->physindev; 167 physindev = nf_bridge_get_physindev(skb);
167 if (physindev && in != physindev) 168 if (physindev && in != physindev)
168 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name); 169 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
169 physoutdev = skb->nf_bridge->physoutdev; 170 physoutdev = nf_bridge_get_physoutdev(skb);
170 if (physoutdev && out != physoutdev) 171 if (physoutdev && out != physoutdev)
171 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name); 172 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
172 } 173 }
diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c
index 4c8b68e5fa16..2e88032cd5ad 100644
--- a/net/netfilter/nf_queue.c
+++ b/net/netfilter/nf_queue.c
@@ -10,6 +10,7 @@
10#include <linux/proc_fs.h> 10#include <linux/proc_fs.h>
11#include <linux/skbuff.h> 11#include <linux/skbuff.h>
12#include <linux/netfilter.h> 12#include <linux/netfilter.h>
13#include <linux/netfilter_bridge.h>
13#include <linux/seq_file.h> 14#include <linux/seq_file.h>
14#include <linux/rcupdate.h> 15#include <linux/rcupdate.h>
15#include <net/protocol.h> 16#include <net/protocol.h>
@@ -47,19 +48,25 @@ EXPORT_SYMBOL(nf_unregister_queue_handler);
47 48
48void nf_queue_entry_release_refs(struct nf_queue_entry *entry) 49void nf_queue_entry_release_refs(struct nf_queue_entry *entry)
49{ 50{
51 struct nf_hook_state *state = &entry->state;
52
50 /* Release those devices we held, or Alexey will kill me. */ 53 /* Release those devices we held, or Alexey will kill me. */
51 if (entry->indev) 54 if (state->in)
52 dev_put(entry->indev); 55 dev_put(state->in);
53 if (entry->outdev) 56 if (state->out)
54 dev_put(entry->outdev); 57 dev_put(state->out);
58 if (state->sk)
59 sock_put(state->sk);
55#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 60#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
56 if (entry->skb->nf_bridge) { 61 if (entry->skb->nf_bridge) {
57 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge; 62 struct net_device *physdev;
58 63
59 if (nf_bridge->physindev) 64 physdev = nf_bridge_get_physindev(entry->skb);
60 dev_put(nf_bridge->physindev); 65 if (physdev)
61 if (nf_bridge->physoutdev) 66 dev_put(physdev);
62 dev_put(nf_bridge->physoutdev); 67 physdev = nf_bridge_get_physoutdev(entry->skb);
68 if (physdev)
69 dev_put(physdev);
63 } 70 }
64#endif 71#endif
65 /* Drop reference to owner of hook which queued us. */ 72 /* Drop reference to owner of hook which queued us. */
@@ -70,22 +77,25 @@ EXPORT_SYMBOL_GPL(nf_queue_entry_release_refs);
70/* Bump dev refs so they don't vanish while packet is out */ 77/* Bump dev refs so they don't vanish while packet is out */
71bool nf_queue_entry_get_refs(struct nf_queue_entry *entry) 78bool nf_queue_entry_get_refs(struct nf_queue_entry *entry)
72{ 79{
80 struct nf_hook_state *state = &entry->state;
81
73 if (!try_module_get(entry->elem->owner)) 82 if (!try_module_get(entry->elem->owner))
74 return false; 83 return false;
75 84
76 if (entry->indev) 85 if (state->in)
77 dev_hold(entry->indev); 86 dev_hold(state->in);
78 if (entry->outdev) 87 if (state->out)
79 dev_hold(entry->outdev); 88 dev_hold(state->out);
89 if (state->sk)
90 sock_hold(state->sk);
80#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 91#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
81 if (entry->skb->nf_bridge) { 92 if (entry->skb->nf_bridge) {
82 struct nf_bridge_info *nf_bridge = entry->skb->nf_bridge;
83 struct net_device *physdev; 93 struct net_device *physdev;
84 94
85 physdev = nf_bridge->physindev; 95 physdev = nf_bridge_get_physindev(entry->skb);
86 if (physdev) 96 if (physdev)
87 dev_hold(physdev); 97 dev_hold(physdev);
88 physdev = nf_bridge->physoutdev; 98 physdev = nf_bridge_get_physoutdev(entry->skb);
89 if (physdev) 99 if (physdev)
90 dev_hold(physdev); 100 dev_hold(physdev);
91 } 101 }
@@ -100,12 +110,9 @@ EXPORT_SYMBOL_GPL(nf_queue_entry_get_refs);
100 * through nf_reinject(). 110 * through nf_reinject().
101 */ 111 */
102int nf_queue(struct sk_buff *skb, 112int nf_queue(struct sk_buff *skb,
103 struct nf_hook_ops *elem, 113 struct nf_hook_ops *elem,
104 u_int8_t pf, unsigned int hook, 114 struct nf_hook_state *state,
105 struct net_device *indev, 115 unsigned int queuenum)
106 struct net_device *outdev,
107 int (*okfn)(struct sk_buff *),
108 unsigned int queuenum)
109{ 116{
110 int status = -ENOENT; 117 int status = -ENOENT;
111 struct nf_queue_entry *entry = NULL; 118 struct nf_queue_entry *entry = NULL;
@@ -121,7 +128,7 @@ int nf_queue(struct sk_buff *skb,
121 goto err_unlock; 128 goto err_unlock;
122 } 129 }
123 130
124 afinfo = nf_get_afinfo(pf); 131 afinfo = nf_get_afinfo(state->pf);
125 if (!afinfo) 132 if (!afinfo)
126 goto err_unlock; 133 goto err_unlock;
127 134
@@ -134,11 +141,7 @@ int nf_queue(struct sk_buff *skb,
134 *entry = (struct nf_queue_entry) { 141 *entry = (struct nf_queue_entry) {
135 .skb = skb, 142 .skb = skb,
136 .elem = elem, 143 .elem = elem,
137 .pf = pf, 144 .state = *state,
138 .hook = hook,
139 .indev = indev,
140 .outdev = outdev,
141 .okfn = okfn,
142 .size = sizeof(*entry) + afinfo->route_key_size, 145 .size = sizeof(*entry) + afinfo->route_key_size,
143 }; 146 };
144 147
@@ -184,30 +187,29 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict)
184 } 187 }
185 188
186 if (verdict == NF_ACCEPT) { 189 if (verdict == NF_ACCEPT) {
187 afinfo = nf_get_afinfo(entry->pf); 190 afinfo = nf_get_afinfo(entry->state.pf);
188 if (!afinfo || afinfo->reroute(skb, entry) < 0) 191 if (!afinfo || afinfo->reroute(skb, entry) < 0)
189 verdict = NF_DROP; 192 verdict = NF_DROP;
190 } 193 }
191 194
195 entry->state.thresh = INT_MIN;
196
192 if (verdict == NF_ACCEPT) { 197 if (verdict == NF_ACCEPT) {
193 next_hook: 198 next_hook:
194 verdict = nf_iterate(&nf_hooks[entry->pf][entry->hook], 199 verdict = nf_iterate(&nf_hooks[entry->state.pf][entry->state.hook],
195 skb, entry->hook, 200 skb, &entry->state, &elem);
196 entry->indev, entry->outdev, &elem,
197 entry->okfn, INT_MIN);
198 } 201 }
199 202
200 switch (verdict & NF_VERDICT_MASK) { 203 switch (verdict & NF_VERDICT_MASK) {
201 case NF_ACCEPT: 204 case NF_ACCEPT:
202 case NF_STOP: 205 case NF_STOP:
203 local_bh_disable(); 206 local_bh_disable();
204 entry->okfn(skb); 207 entry->state.okfn(entry->state.sk, skb);
205 local_bh_enable(); 208 local_bh_enable();
206 break; 209 break;
207 case NF_QUEUE: 210 case NF_QUEUE:
208 err = nf_queue(skb, elem, entry->pf, entry->hook, 211 err = nf_queue(skb, elem, &entry->state,
209 entry->indev, entry->outdev, entry->okfn, 212 verdict >> NF_VERDICT_QBITS);
210 verdict >> NF_VERDICT_QBITS);
211 if (err < 0) { 213 if (err < 0) {
212 if (err == -ECANCELED) 214 if (err == -ECANCELED)
213 goto next_hook; 215 goto next_hook;
diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c
index ac1a9528dbf2..78af83bc9c8e 100644
--- a/net/netfilter/nf_tables_api.c
+++ b/net/netfilter/nf_tables_api.c
@@ -198,36 +198,31 @@ static int nft_delchain(struct nft_ctx *ctx)
198static inline bool 198static inline bool
199nft_rule_is_active(struct net *net, const struct nft_rule *rule) 199nft_rule_is_active(struct net *net, const struct nft_rule *rule)
200{ 200{
201 return (rule->genmask & (1 << net->nft.gencursor)) == 0; 201 return (rule->genmask & nft_genmask_cur(net)) == 0;
202}
203
204static inline int gencursor_next(struct net *net)
205{
206 return net->nft.gencursor+1 == 1 ? 1 : 0;
207} 202}
208 203
209static inline int 204static inline int
210nft_rule_is_active_next(struct net *net, const struct nft_rule *rule) 205nft_rule_is_active_next(struct net *net, const struct nft_rule *rule)
211{ 206{
212 return (rule->genmask & (1 << gencursor_next(net))) == 0; 207 return (rule->genmask & nft_genmask_next(net)) == 0;
213} 208}
214 209
215static inline void 210static inline void
216nft_rule_activate_next(struct net *net, struct nft_rule *rule) 211nft_rule_activate_next(struct net *net, struct nft_rule *rule)
217{ 212{
218 /* Now inactive, will be active in the future */ 213 /* Now inactive, will be active in the future */
219 rule->genmask = (1 << net->nft.gencursor); 214 rule->genmask = nft_genmask_cur(net);
220} 215}
221 216
222static inline void 217static inline void
223nft_rule_deactivate_next(struct net *net, struct nft_rule *rule) 218nft_rule_deactivate_next(struct net *net, struct nft_rule *rule)
224{ 219{
225 rule->genmask = (1 << gencursor_next(net)); 220 rule->genmask = nft_genmask_next(net);
226} 221}
227 222
228static inline void nft_rule_clear(struct net *net, struct nft_rule *rule) 223static inline void nft_rule_clear(struct net *net, struct nft_rule *rule)
229{ 224{
230 rule->genmask &= ~(1 << gencursor_next(net)); 225 rule->genmask &= ~nft_genmask_next(net);
231} 226}
232 227
233static int 228static int
@@ -401,7 +396,8 @@ nf_tables_chain_type_lookup(const struct nft_af_info *afi,
401} 396}
402 397
403static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = { 398static const struct nla_policy nft_table_policy[NFTA_TABLE_MAX + 1] = {
404 [NFTA_TABLE_NAME] = { .type = NLA_STRING }, 399 [NFTA_TABLE_NAME] = { .type = NLA_STRING,
400 .len = NFT_TABLE_MAXNAMELEN - 1 },
405 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 }, 401 [NFTA_TABLE_FLAGS] = { .type = NLA_U32 },
406}; 402};
407 403
@@ -686,26 +682,28 @@ static int nf_tables_newtable(struct sock *nlsk, struct sk_buff *skb,
686 if (!try_module_get(afi->owner)) 682 if (!try_module_get(afi->owner))
687 return -EAFNOSUPPORT; 683 return -EAFNOSUPPORT;
688 684
689 table = kzalloc(sizeof(*table) + nla_len(name), GFP_KERNEL); 685 err = -ENOMEM;
690 if (table == NULL) { 686 table = kzalloc(sizeof(*table), GFP_KERNEL);
691 module_put(afi->owner); 687 if (table == NULL)
692 return -ENOMEM; 688 goto err1;
693 }
694 689
695 nla_strlcpy(table->name, name, nla_len(name)); 690 nla_strlcpy(table->name, name, NFT_TABLE_MAXNAMELEN);
696 INIT_LIST_HEAD(&table->chains); 691 INIT_LIST_HEAD(&table->chains);
697 INIT_LIST_HEAD(&table->sets); 692 INIT_LIST_HEAD(&table->sets);
698 table->flags = flags; 693 table->flags = flags;
699 694
700 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla); 695 nft_ctx_init(&ctx, skb, nlh, afi, table, NULL, nla);
701 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE); 696 err = nft_trans_table_add(&ctx, NFT_MSG_NEWTABLE);
702 if (err < 0) { 697 if (err < 0)
703 kfree(table); 698 goto err2;
704 module_put(afi->owner); 699
705 return err;
706 }
707 list_add_tail_rcu(&table->list, &afi->tables); 700 list_add_tail_rcu(&table->list, &afi->tables);
708 return 0; 701 return 0;
702err2:
703 kfree(table);
704err1:
705 module_put(afi->owner);
706 return err;
709} 707}
710 708
711static int nft_flush_table(struct nft_ctx *ctx) 709static int nft_flush_table(struct nft_ctx *ctx)
@@ -1351,6 +1349,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1351 rcu_assign_pointer(basechain->stats, stats); 1349 rcu_assign_pointer(basechain->stats, stats);
1352 } 1350 }
1353 1351
1352 write_pnet(&basechain->pnet, net);
1354 basechain->type = type; 1353 basechain->type = type;
1355 chain = &basechain->chain; 1354 chain = &basechain->chain;
1356 1355
@@ -1378,7 +1377,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,
1378 1377
1379 INIT_LIST_HEAD(&chain->rules); 1378 INIT_LIST_HEAD(&chain->rules);
1380 chain->handle = nf_tables_alloc_handle(table); 1379 chain->handle = nf_tables_alloc_handle(table);
1381 chain->net = net;
1382 chain->table = table; 1380 chain->table = table;
1383 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN); 1381 nla_strlcpy(chain->name, name, NFT_CHAIN_MAXNAMELEN);
1384 1382
@@ -1547,6 +1545,23 @@ nla_put_failure:
1547 return -1; 1545 return -1;
1548}; 1546};
1549 1547
1548int nft_expr_dump(struct sk_buff *skb, unsigned int attr,
1549 const struct nft_expr *expr)
1550{
1551 struct nlattr *nest;
1552
1553 nest = nla_nest_start(skb, attr);
1554 if (!nest)
1555 goto nla_put_failure;
1556 if (nf_tables_fill_expr_info(skb, expr) < 0)
1557 goto nla_put_failure;
1558 nla_nest_end(skb, nest);
1559 return 0;
1560
1561nla_put_failure:
1562 return -1;
1563}
1564
1550struct nft_expr_info { 1565struct nft_expr_info {
1551 const struct nft_expr_ops *ops; 1566 const struct nft_expr_ops *ops;
1552 struct nlattr *tb[NFT_EXPR_MAXATTR + 1]; 1567 struct nlattr *tb[NFT_EXPR_MAXATTR + 1];
@@ -1624,6 +1639,39 @@ static void nf_tables_expr_destroy(const struct nft_ctx *ctx,
1624 module_put(expr->ops->type->owner); 1639 module_put(expr->ops->type->owner);
1625} 1640}
1626 1641
1642struct nft_expr *nft_expr_init(const struct nft_ctx *ctx,
1643 const struct nlattr *nla)
1644{
1645 struct nft_expr_info info;
1646 struct nft_expr *expr;
1647 int err;
1648
1649 err = nf_tables_expr_parse(ctx, nla, &info);
1650 if (err < 0)
1651 goto err1;
1652
1653 err = -ENOMEM;
1654 expr = kzalloc(info.ops->size, GFP_KERNEL);
1655 if (expr == NULL)
1656 goto err2;
1657
1658 err = nf_tables_newexpr(ctx, &info, expr);
1659 if (err < 0)
1660 goto err2;
1661
1662 return expr;
1663err2:
1664 module_put(info.ops->type->owner);
1665err1:
1666 return ERR_PTR(err);
1667}
1668
1669void nft_expr_destroy(const struct nft_ctx *ctx, struct nft_expr *expr)
1670{
1671 nf_tables_expr_destroy(ctx, expr);
1672 kfree(expr);
1673}
1674
1627/* 1675/*
1628 * Rules 1676 * Rules
1629 */ 1677 */
@@ -1705,12 +1753,8 @@ static int nf_tables_fill_rule_info(struct sk_buff *skb, struct net *net,
1705 if (list == NULL) 1753 if (list == NULL)
1706 goto nla_put_failure; 1754 goto nla_put_failure;
1707 nft_rule_for_each_expr(expr, next, rule) { 1755 nft_rule_for_each_expr(expr, next, rule) {
1708 struct nlattr *elem = nla_nest_start(skb, NFTA_LIST_ELEM); 1756 if (nft_expr_dump(skb, NFTA_LIST_ELEM, expr) < 0)
1709 if (elem == NULL)
1710 goto nla_put_failure; 1757 goto nla_put_failure;
1711 if (nf_tables_fill_expr_info(skb, expr) < 0)
1712 goto nla_put_failure;
1713 nla_nest_end(skb, elem);
1714 } 1758 }
1715 nla_nest_end(skb, list); 1759 nla_nest_end(skb, list);
1716 1760
@@ -2161,7 +2205,7 @@ nft_select_set_ops(const struct nlattr * const nla[],
2161 features = 0; 2205 features = 0;
2162 if (nla[NFTA_SET_FLAGS] != NULL) { 2206 if (nla[NFTA_SET_FLAGS] != NULL) {
2163 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); 2207 features = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2164 features &= NFT_SET_INTERVAL | NFT_SET_MAP; 2208 features &= NFT_SET_INTERVAL | NFT_SET_MAP | NFT_SET_TIMEOUT;
2165 } 2209 }
2166 2210
2167 bops = NULL; 2211 bops = NULL;
@@ -2218,6 +2262,8 @@ static const struct nla_policy nft_set_policy[NFTA_SET_MAX + 1] = {
2218 [NFTA_SET_POLICY] = { .type = NLA_U32 }, 2262 [NFTA_SET_POLICY] = { .type = NLA_U32 },
2219 [NFTA_SET_DESC] = { .type = NLA_NESTED }, 2263 [NFTA_SET_DESC] = { .type = NLA_NESTED },
2220 [NFTA_SET_ID] = { .type = NLA_U32 }, 2264 [NFTA_SET_ID] = { .type = NLA_U32 },
2265 [NFTA_SET_TIMEOUT] = { .type = NLA_U64 },
2266 [NFTA_SET_GC_INTERVAL] = { .type = NLA_U32 },
2221}; 2267};
2222 2268
2223static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = { 2269static const struct nla_policy nft_set_desc_policy[NFTA_SET_DESC_MAX + 1] = {
@@ -2368,6 +2414,13 @@ static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,
2368 goto nla_put_failure; 2414 goto nla_put_failure;
2369 } 2415 }
2370 2416
2417 if (set->timeout &&
2418 nla_put_be64(skb, NFTA_SET_TIMEOUT, cpu_to_be64(set->timeout)))
2419 goto nla_put_failure;
2420 if (set->gc_int &&
2421 nla_put_be32(skb, NFTA_SET_GC_INTERVAL, htonl(set->gc_int)))
2422 goto nla_put_failure;
2423
2371 if (set->policy != NFT_SET_POL_PERFORMANCE) { 2424 if (set->policy != NFT_SET_POL_PERFORMANCE) {
2372 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy))) 2425 if (nla_put_be32(skb, NFTA_SET_POLICY, htonl(set->policy)))
2373 goto nla_put_failure; 2426 goto nla_put_failure;
@@ -2580,7 +2633,8 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2580 char name[IFNAMSIZ]; 2633 char name[IFNAMSIZ];
2581 unsigned int size; 2634 unsigned int size;
2582 bool create; 2635 bool create;
2583 u32 ktype, dtype, flags, policy; 2636 u64 timeout;
2637 u32 ktype, dtype, flags, policy, gc_int;
2584 struct nft_set_desc desc; 2638 struct nft_set_desc desc;
2585 int err; 2639 int err;
2586 2640
@@ -2600,15 +2654,20 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2600 } 2654 }
2601 2655
2602 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN])); 2656 desc.klen = ntohl(nla_get_be32(nla[NFTA_SET_KEY_LEN]));
2603 if (desc.klen == 0 || desc.klen > FIELD_SIZEOF(struct nft_data, data)) 2657 if (desc.klen == 0 || desc.klen > NFT_DATA_VALUE_MAXLEN)
2604 return -EINVAL; 2658 return -EINVAL;
2605 2659
2606 flags = 0; 2660 flags = 0;
2607 if (nla[NFTA_SET_FLAGS] != NULL) { 2661 if (nla[NFTA_SET_FLAGS] != NULL) {
2608 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS])); 2662 flags = ntohl(nla_get_be32(nla[NFTA_SET_FLAGS]));
2609 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT | 2663 if (flags & ~(NFT_SET_ANONYMOUS | NFT_SET_CONSTANT |
2610 NFT_SET_INTERVAL | NFT_SET_MAP)) 2664 NFT_SET_INTERVAL | NFT_SET_TIMEOUT |
2665 NFT_SET_MAP | NFT_SET_EVAL))
2611 return -EINVAL; 2666 return -EINVAL;
2667 /* Only one of both operations is supported */
2668 if ((flags & (NFT_SET_MAP | NFT_SET_EVAL)) ==
2669 (NFT_SET_MAP | NFT_SET_EVAL))
2670 return -EOPNOTSUPP;
2612 } 2671 }
2613 2672
2614 dtype = 0; 2673 dtype = 0;
@@ -2625,14 +2684,26 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2625 if (nla[NFTA_SET_DATA_LEN] == NULL) 2684 if (nla[NFTA_SET_DATA_LEN] == NULL)
2626 return -EINVAL; 2685 return -EINVAL;
2627 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN])); 2686 desc.dlen = ntohl(nla_get_be32(nla[NFTA_SET_DATA_LEN]));
2628 if (desc.dlen == 0 || 2687 if (desc.dlen == 0 || desc.dlen > NFT_DATA_VALUE_MAXLEN)
2629 desc.dlen > FIELD_SIZEOF(struct nft_data, data))
2630 return -EINVAL; 2688 return -EINVAL;
2631 } else 2689 } else
2632 desc.dlen = sizeof(struct nft_data); 2690 desc.dlen = sizeof(struct nft_verdict);
2633 } else if (flags & NFT_SET_MAP) 2691 } else if (flags & NFT_SET_MAP)
2634 return -EINVAL; 2692 return -EINVAL;
2635 2693
2694 timeout = 0;
2695 if (nla[NFTA_SET_TIMEOUT] != NULL) {
2696 if (!(flags & NFT_SET_TIMEOUT))
2697 return -EINVAL;
2698 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_TIMEOUT]));
2699 }
2700 gc_int = 0;
2701 if (nla[NFTA_SET_GC_INTERVAL] != NULL) {
2702 if (!(flags & NFT_SET_TIMEOUT))
2703 return -EINVAL;
2704 gc_int = ntohl(nla_get_be32(nla[NFTA_SET_GC_INTERVAL]));
2705 }
2706
2636 policy = NFT_SET_POL_PERFORMANCE; 2707 policy = NFT_SET_POL_PERFORMANCE;
2637 if (nla[NFTA_SET_POLICY] != NULL) 2708 if (nla[NFTA_SET_POLICY] != NULL)
2638 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY])); 2709 policy = ntohl(nla_get_be32(nla[NFTA_SET_POLICY]));
@@ -2692,6 +2763,7 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2692 goto err2; 2763 goto err2;
2693 2764
2694 INIT_LIST_HEAD(&set->bindings); 2765 INIT_LIST_HEAD(&set->bindings);
2766 write_pnet(&set->pnet, net);
2695 set->ops = ops; 2767 set->ops = ops;
2696 set->ktype = ktype; 2768 set->ktype = ktype;
2697 set->klen = desc.klen; 2769 set->klen = desc.klen;
@@ -2700,6 +2772,8 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,
2700 set->flags = flags; 2772 set->flags = flags;
2701 set->size = desc.size; 2773 set->size = desc.size;
2702 set->policy = policy; 2774 set->policy = policy;
2775 set->timeout = timeout;
2776 set->gc_int = gc_int;
2703 2777
2704 err = ops->init(set, &desc, nla); 2778 err = ops->init(set, &desc, nla);
2705 if (err < 0) 2779 if (err < 0)
@@ -2768,12 +2842,14 @@ static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx,
2768 const struct nft_set_iter *iter, 2842 const struct nft_set_iter *iter,
2769 const struct nft_set_elem *elem) 2843 const struct nft_set_elem *elem)
2770{ 2844{
2845 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
2771 enum nft_registers dreg; 2846 enum nft_registers dreg;
2772 2847
2773 dreg = nft_type_to_reg(set->dtype); 2848 dreg = nft_type_to_reg(set->dtype);
2774 return nft_validate_data_load(ctx, dreg, &elem->data, 2849 return nft_validate_register_store(ctx, dreg, nft_set_ext_data(ext),
2775 set->dtype == NFT_DATA_VERDICT ? 2850 set->dtype == NFT_DATA_VERDICT ?
2776 NFT_DATA_VERDICT : NFT_DATA_VALUE); 2851 NFT_DATA_VERDICT : NFT_DATA_VALUE,
2852 set->dlen);
2777} 2853}
2778 2854
2779int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set, 2855int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
@@ -2785,12 +2861,13 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
2785 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS) 2861 if (!list_empty(&set->bindings) && set->flags & NFT_SET_ANONYMOUS)
2786 return -EBUSY; 2862 return -EBUSY;
2787 2863
2788 if (set->flags & NFT_SET_MAP) { 2864 if (binding->flags & NFT_SET_MAP) {
2789 /* If the set is already bound to the same chain all 2865 /* If the set is already bound to the same chain all
2790 * jumps are already validated for that chain. 2866 * jumps are already validated for that chain.
2791 */ 2867 */
2792 list_for_each_entry(i, &set->bindings, list) { 2868 list_for_each_entry(i, &set->bindings, list) {
2793 if (i->chain == binding->chain) 2869 if (binding->flags & NFT_SET_MAP &&
2870 i->chain == binding->chain)
2794 goto bind; 2871 goto bind;
2795 } 2872 }
2796 2873
@@ -2824,6 +2901,35 @@ void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
2824 nf_tables_set_destroy(ctx, set); 2901 nf_tables_set_destroy(ctx, set);
2825} 2902}
2826 2903
2904const struct nft_set_ext_type nft_set_ext_types[] = {
2905 [NFT_SET_EXT_KEY] = {
2906 .align = __alignof__(u32),
2907 },
2908 [NFT_SET_EXT_DATA] = {
2909 .align = __alignof__(u32),
2910 },
2911 [NFT_SET_EXT_EXPR] = {
2912 .align = __alignof__(struct nft_expr),
2913 },
2914 [NFT_SET_EXT_FLAGS] = {
2915 .len = sizeof(u8),
2916 .align = __alignof__(u8),
2917 },
2918 [NFT_SET_EXT_TIMEOUT] = {
2919 .len = sizeof(u64),
2920 .align = __alignof__(u64),
2921 },
2922 [NFT_SET_EXT_EXPIRATION] = {
2923 .len = sizeof(unsigned long),
2924 .align = __alignof__(unsigned long),
2925 },
2926 [NFT_SET_EXT_USERDATA] = {
2927 .len = sizeof(struct nft_userdata),
2928 .align = __alignof__(struct nft_userdata),
2929 },
2930};
2931EXPORT_SYMBOL_GPL(nft_set_ext_types);
2932
2827/* 2933/*
2828 * Set elements 2934 * Set elements
2829 */ 2935 */
@@ -2832,6 +2938,9 @@ static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
2832 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED }, 2938 [NFTA_SET_ELEM_KEY] = { .type = NLA_NESTED },
2833 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED }, 2939 [NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
2834 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 }, 2940 [NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
2941 [NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
2942 [NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
2943 .len = NFT_USERDATA_MAXLEN },
2835}; 2944};
2836 2945
2837static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = { 2946static const struct nla_policy nft_set_elem_list_policy[NFTA_SET_ELEM_LIST_MAX + 1] = {
@@ -2870,6 +2979,7 @@ static int nf_tables_fill_setelem(struct sk_buff *skb,
2870 const struct nft_set *set, 2979 const struct nft_set *set,
2871 const struct nft_set_elem *elem) 2980 const struct nft_set_elem *elem)
2872{ 2981{
2982 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
2873 unsigned char *b = skb_tail_pointer(skb); 2983 unsigned char *b = skb_tail_pointer(skb);
2874 struct nlattr *nest; 2984 struct nlattr *nest;
2875 2985
@@ -2877,20 +2987,52 @@ static int nf_tables_fill_setelem(struct sk_buff *skb,
2877 if (nest == NULL) 2987 if (nest == NULL)
2878 goto nla_put_failure; 2988 goto nla_put_failure;
2879 2989
2880 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, &elem->key, NFT_DATA_VALUE, 2990 if (nft_data_dump(skb, NFTA_SET_ELEM_KEY, nft_set_ext_key(ext),
2881 set->klen) < 0) 2991 NFT_DATA_VALUE, set->klen) < 0)
2882 goto nla_put_failure; 2992 goto nla_put_failure;
2883 2993
2884 if (set->flags & NFT_SET_MAP && 2994 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA) &&
2885 !(elem->flags & NFT_SET_ELEM_INTERVAL_END) && 2995 nft_data_dump(skb, NFTA_SET_ELEM_DATA, nft_set_ext_data(ext),
2886 nft_data_dump(skb, NFTA_SET_ELEM_DATA, &elem->data,
2887 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE, 2996 set->dtype == NFT_DATA_VERDICT ? NFT_DATA_VERDICT : NFT_DATA_VALUE,
2888 set->dlen) < 0) 2997 set->dlen) < 0)
2889 goto nla_put_failure; 2998 goto nla_put_failure;
2890 2999
2891 if (elem->flags != 0) 3000 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR) &&
2892 if (nla_put_be32(skb, NFTA_SET_ELEM_FLAGS, htonl(elem->flags))) 3001 nft_expr_dump(skb, NFTA_SET_ELEM_EXPR, nft_set_ext_expr(ext)) < 0)
3002 goto nla_put_failure;
3003
3004 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
3005 nla_put_be32(skb, NFTA_SET_ELEM_FLAGS,
3006 htonl(*nft_set_ext_flags(ext))))
3007 goto nla_put_failure;
3008
3009 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT) &&
3010 nla_put_be64(skb, NFTA_SET_ELEM_TIMEOUT,
3011 cpu_to_be64(*nft_set_ext_timeout(ext))))
3012 goto nla_put_failure;
3013
3014 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
3015 unsigned long expires, now = jiffies;
3016
3017 expires = *nft_set_ext_expiration(ext);
3018 if (time_before(now, expires))
3019 expires -= now;
3020 else
3021 expires = 0;
3022
3023 if (nla_put_be64(skb, NFTA_SET_ELEM_EXPIRATION,
3024 cpu_to_be64(jiffies_to_msecs(expires))))
2893 goto nla_put_failure; 3025 goto nla_put_failure;
3026 }
3027
3028 if (nft_set_ext_exists(ext, NFT_SET_EXT_USERDATA)) {
3029 struct nft_userdata *udata;
3030
3031 udata = nft_set_ext_userdata(ext);
3032 if (nla_put(skb, NFTA_SET_ELEM_USERDATA,
3033 udata->len + 1, udata->data))
3034 goto nla_put_failure;
3035 }
2894 3036
2895 nla_nest_end(skb, nest); 3037 nla_nest_end(skb, nest);
2896 return 0; 3038 return 0;
@@ -3111,20 +3253,65 @@ static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
3111 return trans; 3253 return trans;
3112} 3254}
3113 3255
3256void *nft_set_elem_init(const struct nft_set *set,
3257 const struct nft_set_ext_tmpl *tmpl,
3258 const u32 *key, const u32 *data,
3259 u64 timeout, gfp_t gfp)
3260{
3261 struct nft_set_ext *ext;
3262 void *elem;
3263
3264 elem = kzalloc(set->ops->elemsize + tmpl->len, gfp);
3265 if (elem == NULL)
3266 return NULL;
3267
3268 ext = nft_set_elem_ext(set, elem);
3269 nft_set_ext_init(ext, tmpl);
3270
3271 memcpy(nft_set_ext_key(ext), key, set->klen);
3272 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3273 memcpy(nft_set_ext_data(ext), data, set->dlen);
3274 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
3275 *nft_set_ext_expiration(ext) =
3276 jiffies + msecs_to_jiffies(timeout);
3277 if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
3278 *nft_set_ext_timeout(ext) = timeout;
3279
3280 return elem;
3281}
3282
3283void nft_set_elem_destroy(const struct nft_set *set, void *elem)
3284{
3285 struct nft_set_ext *ext = nft_set_elem_ext(set, elem);
3286
3287 nft_data_uninit(nft_set_ext_key(ext), NFT_DATA_VALUE);
3288 if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
3289 nft_data_uninit(nft_set_ext_data(ext), set->dtype);
3290 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
3291 nf_tables_expr_destroy(NULL, nft_set_ext_expr(ext));
3292
3293 kfree(elem);
3294}
3295EXPORT_SYMBOL_GPL(nft_set_elem_destroy);
3296
3114static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, 3297static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3115 const struct nlattr *attr) 3298 const struct nlattr *attr)
3116{ 3299{
3117 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1]; 3300 struct nlattr *nla[NFTA_SET_ELEM_MAX + 1];
3118 struct nft_data_desc d1, d2; 3301 struct nft_data_desc d1, d2;
3302 struct nft_set_ext_tmpl tmpl;
3303 struct nft_set_ext *ext;
3119 struct nft_set_elem elem; 3304 struct nft_set_elem elem;
3120 struct nft_set_binding *binding; 3305 struct nft_set_binding *binding;
3306 struct nft_userdata *udata;
3307 struct nft_data data;
3121 enum nft_registers dreg; 3308 enum nft_registers dreg;
3122 struct nft_trans *trans; 3309 struct nft_trans *trans;
3310 u64 timeout;
3311 u32 flags;
3312 u8 ulen;
3123 int err; 3313 int err;
3124 3314
3125 if (set->size && set->nelems == set->size)
3126 return -ENFILE;
3127
3128 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr, 3315 err = nla_parse_nested(nla, NFTA_SET_ELEM_MAX, attr,
3129 nft_set_elem_policy); 3316 nft_set_elem_policy);
3130 if (err < 0) 3317 if (err < 0)
@@ -3133,38 +3320,59 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3133 if (nla[NFTA_SET_ELEM_KEY] == NULL) 3320 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3134 return -EINVAL; 3321 return -EINVAL;
3135 3322
3136 elem.flags = 0; 3323 nft_set_ext_prepare(&tmpl);
3324
3325 flags = 0;
3137 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) { 3326 if (nla[NFTA_SET_ELEM_FLAGS] != NULL) {
3138 elem.flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS])); 3327 flags = ntohl(nla_get_be32(nla[NFTA_SET_ELEM_FLAGS]));
3139 if (elem.flags & ~NFT_SET_ELEM_INTERVAL_END) 3328 if (flags & ~NFT_SET_ELEM_INTERVAL_END)
3329 return -EINVAL;
3330 if (!(set->flags & NFT_SET_INTERVAL) &&
3331 flags & NFT_SET_ELEM_INTERVAL_END)
3140 return -EINVAL; 3332 return -EINVAL;
3333 if (flags != 0)
3334 nft_set_ext_add(&tmpl, NFT_SET_EXT_FLAGS);
3141 } 3335 }
3142 3336
3143 if (set->flags & NFT_SET_MAP) { 3337 if (set->flags & NFT_SET_MAP) {
3144 if (nla[NFTA_SET_ELEM_DATA] == NULL && 3338 if (nla[NFTA_SET_ELEM_DATA] == NULL &&
3145 !(elem.flags & NFT_SET_ELEM_INTERVAL_END)) 3339 !(flags & NFT_SET_ELEM_INTERVAL_END))
3146 return -EINVAL; 3340 return -EINVAL;
3147 if (nla[NFTA_SET_ELEM_DATA] != NULL && 3341 if (nla[NFTA_SET_ELEM_DATA] != NULL &&
3148 elem.flags & NFT_SET_ELEM_INTERVAL_END) 3342 flags & NFT_SET_ELEM_INTERVAL_END)
3149 return -EINVAL; 3343 return -EINVAL;
3150 } else { 3344 } else {
3151 if (nla[NFTA_SET_ELEM_DATA] != NULL) 3345 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3152 return -EINVAL; 3346 return -EINVAL;
3153 } 3347 }
3154 3348
3155 err = nft_data_init(ctx, &elem.key, &d1, nla[NFTA_SET_ELEM_KEY]); 3349 timeout = 0;
3350 if (nla[NFTA_SET_ELEM_TIMEOUT] != NULL) {
3351 if (!(set->flags & NFT_SET_TIMEOUT))
3352 return -EINVAL;
3353 timeout = be64_to_cpu(nla_get_be64(nla[NFTA_SET_ELEM_TIMEOUT]));
3354 } else if (set->flags & NFT_SET_TIMEOUT) {
3355 timeout = set->timeout;
3356 }
3357
3358 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
3359 nla[NFTA_SET_ELEM_KEY]);
3156 if (err < 0) 3360 if (err < 0)
3157 goto err1; 3361 goto err1;
3158 err = -EINVAL; 3362 err = -EINVAL;
3159 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen) 3363 if (d1.type != NFT_DATA_VALUE || d1.len != set->klen)
3160 goto err2; 3364 goto err2;
3161 3365
3162 err = -EEXIST; 3366 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_KEY, d1.len);
3163 if (set->ops->get(set, &elem) == 0) 3367 if (timeout > 0) {
3164 goto err2; 3368 nft_set_ext_add(&tmpl, NFT_SET_EXT_EXPIRATION);
3369 if (timeout != set->timeout)
3370 nft_set_ext_add(&tmpl, NFT_SET_EXT_TIMEOUT);
3371 }
3165 3372
3166 if (nla[NFTA_SET_ELEM_DATA] != NULL) { 3373 if (nla[NFTA_SET_ELEM_DATA] != NULL) {
3167 err = nft_data_init(ctx, &elem.data, &d2, nla[NFTA_SET_ELEM_DATA]); 3374 err = nft_data_init(ctx, &data, sizeof(data), &d2,
3375 nla[NFTA_SET_ELEM_DATA]);
3168 if (err < 0) 3376 if (err < 0)
3169 goto err2; 3377 goto err2;
3170 3378
@@ -3180,32 +3388,68 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
3180 .chain = (struct nft_chain *)binding->chain, 3388 .chain = (struct nft_chain *)binding->chain,
3181 }; 3389 };
3182 3390
3183 err = nft_validate_data_load(&bind_ctx, dreg, 3391 if (!(binding->flags & NFT_SET_MAP))
3184 &elem.data, d2.type); 3392 continue;
3393
3394 err = nft_validate_register_store(&bind_ctx, dreg,
3395 &data,
3396 d2.type, d2.len);
3185 if (err < 0) 3397 if (err < 0)
3186 goto err3; 3398 goto err3;
3187 } 3399 }
3400
3401 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_DATA, d2.len);
3402 }
3403
3404 /* The full maximum length of userdata can exceed the maximum
3405 * offset value (U8_MAX) for following extensions, therefor it
3406 * must be the last extension added.
3407 */
3408 ulen = 0;
3409 if (nla[NFTA_SET_ELEM_USERDATA] != NULL) {
3410 ulen = nla_len(nla[NFTA_SET_ELEM_USERDATA]);
3411 if (ulen > 0)
3412 nft_set_ext_add_length(&tmpl, NFT_SET_EXT_USERDATA,
3413 ulen);
3414 }
3415
3416 err = -ENOMEM;
3417 elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
3418 timeout, GFP_KERNEL);
3419 if (elem.priv == NULL)
3420 goto err3;
3421
3422 ext = nft_set_elem_ext(set, elem.priv);
3423 if (flags)
3424 *nft_set_ext_flags(ext) = flags;
3425 if (ulen > 0) {
3426 udata = nft_set_ext_userdata(ext);
3427 udata->len = ulen - 1;
3428 nla_memcpy(&udata->data, nla[NFTA_SET_ELEM_USERDATA], ulen);
3188 } 3429 }
3189 3430
3190 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set); 3431 trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
3191 if (trans == NULL) 3432 if (trans == NULL)
3192 goto err3; 3433 goto err4;
3193 3434
3435 ext->genmask = nft_genmask_cur(ctx->net) | NFT_SET_ELEM_BUSY_MASK;
3194 err = set->ops->insert(set, &elem); 3436 err = set->ops->insert(set, &elem);
3195 if (err < 0) 3437 if (err < 0)
3196 goto err4; 3438 goto err5;
3197 3439
3198 nft_trans_elem(trans) = elem; 3440 nft_trans_elem(trans) = elem;
3199 list_add_tail(&trans->list, &ctx->net->nft.commit_list); 3441 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3200 return 0; 3442 return 0;
3201 3443
3202err4: 3444err5:
3203 kfree(trans); 3445 kfree(trans);
3446err4:
3447 kfree(elem.priv);
3204err3: 3448err3:
3205 if (nla[NFTA_SET_ELEM_DATA] != NULL) 3449 if (nla[NFTA_SET_ELEM_DATA] != NULL)
3206 nft_data_uninit(&elem.data, d2.type); 3450 nft_data_uninit(&data, d2.type);
3207err2: 3451err2:
3208 nft_data_uninit(&elem.key, d1.type); 3452 nft_data_uninit(&elem.key.val, d1.type);
3209err1: 3453err1:
3210 return err; 3454 return err;
3211} 3455}
@@ -3241,11 +3485,15 @@ static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,
3241 return -EBUSY; 3485 return -EBUSY;
3242 3486
3243 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) { 3487 nla_for_each_nested(attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS], rem) {
3488 if (set->size &&
3489 !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact))
3490 return -ENFILE;
3491
3244 err = nft_add_set_elem(&ctx, set, attr); 3492 err = nft_add_set_elem(&ctx, set, attr);
3245 if (err < 0) 3493 if (err < 0) {
3494 atomic_dec(&set->nelems);
3246 break; 3495 break;
3247 3496 }
3248 set->nelems++;
3249 } 3497 }
3250 return err; 3498 return err;
3251} 3499}
@@ -3268,7 +3516,8 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3268 if (nla[NFTA_SET_ELEM_KEY] == NULL) 3516 if (nla[NFTA_SET_ELEM_KEY] == NULL)
3269 goto err1; 3517 goto err1;
3270 3518
3271 err = nft_data_init(ctx, &elem.key, &desc, nla[NFTA_SET_ELEM_KEY]); 3519 err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &desc,
3520 nla[NFTA_SET_ELEM_KEY]);
3272 if (err < 0) 3521 if (err < 0)
3273 goto err1; 3522 goto err1;
3274 3523
@@ -3276,21 +3525,26 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
3276 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen) 3525 if (desc.type != NFT_DATA_VALUE || desc.len != set->klen)
3277 goto err2; 3526 goto err2;
3278 3527
3279 err = set->ops->get(set, &elem);
3280 if (err < 0)
3281 goto err2;
3282
3283 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set); 3528 trans = nft_trans_elem_alloc(ctx, NFT_MSG_DELSETELEM, set);
3284 if (trans == NULL) { 3529 if (trans == NULL) {
3285 err = -ENOMEM; 3530 err = -ENOMEM;
3286 goto err2; 3531 goto err2;
3287 } 3532 }
3288 3533
3534 elem.priv = set->ops->deactivate(set, &elem);
3535 if (elem.priv == NULL) {
3536 err = -ENOENT;
3537 goto err3;
3538 }
3539
3289 nft_trans_elem(trans) = elem; 3540 nft_trans_elem(trans) = elem;
3290 list_add_tail(&trans->list, &ctx->net->nft.commit_list); 3541 list_add_tail(&trans->list, &ctx->net->nft.commit_list);
3291 return 0; 3542 return 0;
3543
3544err3:
3545 kfree(trans);
3292err2: 3546err2:
3293 nft_data_uninit(&elem.key, desc.type); 3547 nft_data_uninit(&elem.key.val, desc.type);
3294err1: 3548err1:
3295 return err; 3549 return err;
3296} 3550}
@@ -3322,11 +3576,36 @@ static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,
3322 if (err < 0) 3576 if (err < 0)
3323 break; 3577 break;
3324 3578
3325 set->nelems--; 3579 set->ndeact++;
3326 } 3580 }
3327 return err; 3581 return err;
3328} 3582}
3329 3583
3584void nft_set_gc_batch_release(struct rcu_head *rcu)
3585{
3586 struct nft_set_gc_batch *gcb;
3587 unsigned int i;
3588
3589 gcb = container_of(rcu, struct nft_set_gc_batch, head.rcu);
3590 for (i = 0; i < gcb->head.cnt; i++)
3591 nft_set_elem_destroy(gcb->head.set, gcb->elems[i]);
3592 kfree(gcb);
3593}
3594EXPORT_SYMBOL_GPL(nft_set_gc_batch_release);
3595
3596struct nft_set_gc_batch *nft_set_gc_batch_alloc(const struct nft_set *set,
3597 gfp_t gfp)
3598{
3599 struct nft_set_gc_batch *gcb;
3600
3601 gcb = kzalloc(sizeof(*gcb), gfp);
3602 if (gcb == NULL)
3603 return gcb;
3604 gcb->head.set = set;
3605 return gcb;
3606}
3607EXPORT_SYMBOL_GPL(nft_set_gc_batch_alloc);
3608
3330static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net, 3609static int nf_tables_fill_gen_info(struct sk_buff *skb, struct net *net,
3331 u32 portid, u32 seq) 3610 u32 portid, u32 seq)
3332{ 3611{
@@ -3526,6 +3805,10 @@ static void nf_tables_commit_release(struct nft_trans *trans)
3526 case NFT_MSG_DELSET: 3805 case NFT_MSG_DELSET:
3527 nft_set_destroy(nft_trans_set(trans)); 3806 nft_set_destroy(nft_trans_set(trans));
3528 break; 3807 break;
3808 case NFT_MSG_DELSETELEM:
3809 nft_set_elem_destroy(nft_trans_elem_set(trans),
3810 nft_trans_elem(trans).priv);
3811 break;
3529 } 3812 }
3530 kfree(trans); 3813 kfree(trans);
3531} 3814}
@@ -3540,7 +3823,7 @@ static int nf_tables_commit(struct sk_buff *skb)
3540 while (++net->nft.base_seq == 0); 3823 while (++net->nft.base_seq == 0);
3541 3824
3542 /* A new generation has just started */ 3825 /* A new generation has just started */
3543 net->nft.gencursor = gencursor_next(net); 3826 net->nft.gencursor = nft_gencursor_next(net);
3544 3827
3545 /* Make sure all packets have left the previous generation before 3828 /* Make sure all packets have left the previous generation before
3546 * purging old rules. 3829 * purging old rules.
@@ -3611,24 +3894,23 @@ static int nf_tables_commit(struct sk_buff *skb)
3611 NFT_MSG_DELSET, GFP_KERNEL); 3894 NFT_MSG_DELSET, GFP_KERNEL);
3612 break; 3895 break;
3613 case NFT_MSG_NEWSETELEM: 3896 case NFT_MSG_NEWSETELEM:
3614 nf_tables_setelem_notify(&trans->ctx, 3897 te = (struct nft_trans_elem *)trans->data;
3615 nft_trans_elem_set(trans), 3898
3616 &nft_trans_elem(trans), 3899 te->set->ops->activate(te->set, &te->elem);
3900 nf_tables_setelem_notify(&trans->ctx, te->set,
3901 &te->elem,
3617 NFT_MSG_NEWSETELEM, 0); 3902 NFT_MSG_NEWSETELEM, 0);
3618 nft_trans_destroy(trans); 3903 nft_trans_destroy(trans);
3619 break; 3904 break;
3620 case NFT_MSG_DELSETELEM: 3905 case NFT_MSG_DELSETELEM:
3621 te = (struct nft_trans_elem *)trans->data; 3906 te = (struct nft_trans_elem *)trans->data;
3907
3622 nf_tables_setelem_notify(&trans->ctx, te->set, 3908 nf_tables_setelem_notify(&trans->ctx, te->set,
3623 &te->elem, 3909 &te->elem,
3624 NFT_MSG_DELSETELEM, 0); 3910 NFT_MSG_DELSETELEM, 0);
3625 te->set->ops->get(te->set, &te->elem);
3626 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3627 if (te->set->flags & NFT_SET_MAP &&
3628 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3629 nft_data_uninit(&te->elem.data, te->set->dtype);
3630 te->set->ops->remove(te->set, &te->elem); 3911 te->set->ops->remove(te->set, &te->elem);
3631 nft_trans_destroy(trans); 3912 atomic_dec(&te->set->nelems);
3913 te->set->ndeact--;
3632 break; 3914 break;
3633 } 3915 }
3634 } 3916 }
@@ -3660,6 +3942,10 @@ static void nf_tables_abort_release(struct nft_trans *trans)
3660 case NFT_MSG_NEWSET: 3942 case NFT_MSG_NEWSET:
3661 nft_set_destroy(nft_trans_set(trans)); 3943 nft_set_destroy(nft_trans_set(trans));
3662 break; 3944 break;
3945 case NFT_MSG_NEWSETELEM:
3946 nft_set_elem_destroy(nft_trans_elem_set(trans),
3947 nft_trans_elem(trans).priv);
3948 break;
3663 } 3949 }
3664 kfree(trans); 3950 kfree(trans);
3665} 3951}
@@ -3728,18 +4014,17 @@ static int nf_tables_abort(struct sk_buff *skb)
3728 nft_trans_destroy(trans); 4014 nft_trans_destroy(trans);
3729 break; 4015 break;
3730 case NFT_MSG_NEWSETELEM: 4016 case NFT_MSG_NEWSETELEM:
3731 nft_trans_elem_set(trans)->nelems--;
3732 te = (struct nft_trans_elem *)trans->data; 4017 te = (struct nft_trans_elem *)trans->data;
3733 te->set->ops->get(te->set, &te->elem); 4018
3734 nft_data_uninit(&te->elem.key, NFT_DATA_VALUE);
3735 if (te->set->flags & NFT_SET_MAP &&
3736 !(te->elem.flags & NFT_SET_ELEM_INTERVAL_END))
3737 nft_data_uninit(&te->elem.data, te->set->dtype);
3738 te->set->ops->remove(te->set, &te->elem); 4019 te->set->ops->remove(te->set, &te->elem);
3739 nft_trans_destroy(trans); 4020 atomic_dec(&te->set->nelems);
3740 break; 4021 break;
3741 case NFT_MSG_DELSETELEM: 4022 case NFT_MSG_DELSETELEM:
3742 nft_trans_elem_set(trans)->nelems++; 4023 te = (struct nft_trans_elem *)trans->data;
4024
4025 te->set->ops->activate(te->set, &te->elem);
4026 te->set->ndeact--;
4027
3743 nft_trans_destroy(trans); 4028 nft_trans_destroy(trans);
3744 break; 4029 break;
3745 } 4030 }
@@ -3814,13 +4099,18 @@ static int nf_tables_loop_check_setelem(const struct nft_ctx *ctx,
3814 const struct nft_set_iter *iter, 4099 const struct nft_set_iter *iter,
3815 const struct nft_set_elem *elem) 4100 const struct nft_set_elem *elem)
3816{ 4101{
3817 if (elem->flags & NFT_SET_ELEM_INTERVAL_END) 4102 const struct nft_set_ext *ext = nft_set_elem_ext(set, elem->priv);
4103 const struct nft_data *data;
4104
4105 if (nft_set_ext_exists(ext, NFT_SET_EXT_FLAGS) &&
4106 *nft_set_ext_flags(ext) & NFT_SET_ELEM_INTERVAL_END)
3818 return 0; 4107 return 0;
3819 4108
3820 switch (elem->data.verdict) { 4109 data = nft_set_ext_data(ext);
4110 switch (data->verdict.code) {
3821 case NFT_JUMP: 4111 case NFT_JUMP:
3822 case NFT_GOTO: 4112 case NFT_GOTO:
3823 return nf_tables_check_loops(ctx, elem->data.chain); 4113 return nf_tables_check_loops(ctx, data->verdict.chain);
3824 default: 4114 default:
3825 return 0; 4115 return 0;
3826 } 4116 }
@@ -3853,10 +4143,11 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx,
3853 if (data == NULL) 4143 if (data == NULL)
3854 continue; 4144 continue;
3855 4145
3856 switch (data->verdict) { 4146 switch (data->verdict.code) {
3857 case NFT_JUMP: 4147 case NFT_JUMP:
3858 case NFT_GOTO: 4148 case NFT_GOTO:
3859 err = nf_tables_check_loops(ctx, data->chain); 4149 err = nf_tables_check_loops(ctx,
4150 data->verdict.chain);
3860 if (err < 0) 4151 if (err < 0)
3861 return err; 4152 return err;
3862 default: 4153 default:
@@ -3871,7 +4162,8 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx,
3871 continue; 4162 continue;
3872 4163
3873 list_for_each_entry(binding, &set->bindings, list) { 4164 list_for_each_entry(binding, &set->bindings, list) {
3874 if (binding->chain != chain) 4165 if (!(binding->flags & NFT_SET_MAP) ||
4166 binding->chain != chain)
3875 continue; 4167 continue;
3876 4168
3877 iter.skip = 0; 4169 iter.skip = 0;
@@ -3889,85 +4181,129 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx,
3889} 4181}
3890 4182
3891/** 4183/**
3892 * nft_validate_input_register - validate an expressions' input register 4184 * nft_parse_register - parse a register value from a netlink attribute
3893 * 4185 *
3894 * @reg: the register number 4186 * @attr: netlink attribute
3895 * 4187 *
3896 * Validate that the input register is one of the general purpose 4188 * Parse and translate a register value from a netlink attribute.
3897 * registers. 4189 * Registers used to be 128 bit wide, these register numbers will be
4190 * mapped to the corresponding 32 bit register numbers.
3898 */ 4191 */
3899int nft_validate_input_register(enum nft_registers reg) 4192unsigned int nft_parse_register(const struct nlattr *attr)
3900{ 4193{
3901 if (reg <= NFT_REG_VERDICT) 4194 unsigned int reg;
3902 return -EINVAL; 4195
3903 if (reg > NFT_REG_MAX) 4196 reg = ntohl(nla_get_be32(attr));
3904 return -ERANGE; 4197 switch (reg) {
3905 return 0; 4198 case NFT_REG_VERDICT...NFT_REG_4:
4199 return reg * NFT_REG_SIZE / NFT_REG32_SIZE;
4200 default:
4201 return reg + NFT_REG_SIZE / NFT_REG32_SIZE - NFT_REG32_00;
4202 }
3906} 4203}
3907EXPORT_SYMBOL_GPL(nft_validate_input_register); 4204EXPORT_SYMBOL_GPL(nft_parse_register);
3908 4205
3909/** 4206/**
3910 * nft_validate_output_register - validate an expressions' output register 4207 * nft_dump_register - dump a register value to a netlink attribute
4208 *
4209 * @skb: socket buffer
4210 * @attr: attribute number
4211 * @reg: register number
4212 *
4213 * Construct a netlink attribute containing the register number. For
4214 * compatibility reasons, register numbers being a multiple of 4 are
4215 * translated to the corresponding 128 bit register numbers.
4216 */
4217int nft_dump_register(struct sk_buff *skb, unsigned int attr, unsigned int reg)
4218{
4219 if (reg % (NFT_REG_SIZE / NFT_REG32_SIZE) == 0)
4220 reg = reg / (NFT_REG_SIZE / NFT_REG32_SIZE);
4221 else
4222 reg = reg - NFT_REG_SIZE / NFT_REG32_SIZE + NFT_REG32_00;
4223
4224 return nla_put_be32(skb, attr, htonl(reg));
4225}
4226EXPORT_SYMBOL_GPL(nft_dump_register);
4227
4228/**
4229 * nft_validate_register_load - validate a load from a register
3911 * 4230 *
3912 * @reg: the register number 4231 * @reg: the register number
4232 * @len: the length of the data
3913 * 4233 *
3914 * Validate that the output register is one of the general purpose 4234 * Validate that the input register is one of the general purpose
3915 * registers or the verdict register. 4235 * registers and that the length of the load is within the bounds.
3916 */ 4236 */
3917int nft_validate_output_register(enum nft_registers reg) 4237int nft_validate_register_load(enum nft_registers reg, unsigned int len)
3918{ 4238{
3919 if (reg < NFT_REG_VERDICT) 4239 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
3920 return -EINVAL; 4240 return -EINVAL;
3921 if (reg > NFT_REG_MAX) 4241 if (len == 0)
4242 return -EINVAL;
4243 if (reg * NFT_REG32_SIZE + len > FIELD_SIZEOF(struct nft_regs, data))
3922 return -ERANGE; 4244 return -ERANGE;
4245
3923 return 0; 4246 return 0;
3924} 4247}
3925EXPORT_SYMBOL_GPL(nft_validate_output_register); 4248EXPORT_SYMBOL_GPL(nft_validate_register_load);
3926 4249
3927/** 4250/**
3928 * nft_validate_data_load - validate an expressions' data load 4251 * nft_validate_register_store - validate an expressions' register store
3929 * 4252 *
3930 * @ctx: context of the expression performing the load 4253 * @ctx: context of the expression performing the load
3931 * @reg: the destination register number 4254 * @reg: the destination register number
3932 * @data: the data to load 4255 * @data: the data to load
3933 * @type: the data type 4256 * @type: the data type
4257 * @len: the length of the data
3934 * 4258 *
3935 * Validate that a data load uses the appropriate data type for 4259 * Validate that a data load uses the appropriate data type for
3936 * the destination register. A value of NULL for the data means 4260 * the destination register and the length is within the bounds.
3937 * that its runtime gathered data, which is always of type 4261 * A value of NULL for the data means that its runtime gathered
3938 * NFT_DATA_VALUE. 4262 * data.
3939 */ 4263 */
3940int nft_validate_data_load(const struct nft_ctx *ctx, enum nft_registers reg, 4264int nft_validate_register_store(const struct nft_ctx *ctx,
3941 const struct nft_data *data, 4265 enum nft_registers reg,
3942 enum nft_data_types type) 4266 const struct nft_data *data,
4267 enum nft_data_types type, unsigned int len)
3943{ 4268{
3944 int err; 4269 int err;
3945 4270
3946 switch (reg) { 4271 switch (reg) {
3947 case NFT_REG_VERDICT: 4272 case NFT_REG_VERDICT:
3948 if (data == NULL || type != NFT_DATA_VERDICT) 4273 if (type != NFT_DATA_VERDICT)
3949 return -EINVAL; 4274 return -EINVAL;
3950 4275
3951 if (data->verdict == NFT_GOTO || data->verdict == NFT_JUMP) { 4276 if (data != NULL &&
3952 err = nf_tables_check_loops(ctx, data->chain); 4277 (data->verdict.code == NFT_GOTO ||
4278 data->verdict.code == NFT_JUMP)) {
4279 err = nf_tables_check_loops(ctx, data->verdict.chain);
3953 if (err < 0) 4280 if (err < 0)
3954 return err; 4281 return err;
3955 4282
3956 if (ctx->chain->level + 1 > data->chain->level) { 4283 if (ctx->chain->level + 1 >
4284 data->verdict.chain->level) {
3957 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE) 4285 if (ctx->chain->level + 1 == NFT_JUMP_STACK_SIZE)
3958 return -EMLINK; 4286 return -EMLINK;
3959 data->chain->level = ctx->chain->level + 1; 4287 data->verdict.chain->level = ctx->chain->level + 1;
3960 } 4288 }
3961 } 4289 }
3962 4290
3963 return 0; 4291 return 0;
3964 default: 4292 default:
4293 if (reg < NFT_REG_1 * NFT_REG_SIZE / NFT_REG32_SIZE)
4294 return -EINVAL;
4295 if (len == 0)
4296 return -EINVAL;
4297 if (reg * NFT_REG32_SIZE + len >
4298 FIELD_SIZEOF(struct nft_regs, data))
4299 return -ERANGE;
4300
3965 if (data != NULL && type != NFT_DATA_VALUE) 4301 if (data != NULL && type != NFT_DATA_VALUE)
3966 return -EINVAL; 4302 return -EINVAL;
3967 return 0; 4303 return 0;
3968 } 4304 }
3969} 4305}
3970EXPORT_SYMBOL_GPL(nft_validate_data_load); 4306EXPORT_SYMBOL_GPL(nft_validate_register_store);
3971 4307
3972static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = { 4308static const struct nla_policy nft_verdict_policy[NFTA_VERDICT_MAX + 1] = {
3973 [NFTA_VERDICT_CODE] = { .type = NLA_U32 }, 4309 [NFTA_VERDICT_CODE] = { .type = NLA_U32 },
@@ -3988,11 +4324,11 @@ static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
3988 4324
3989 if (!tb[NFTA_VERDICT_CODE]) 4325 if (!tb[NFTA_VERDICT_CODE])
3990 return -EINVAL; 4326 return -EINVAL;
3991 data->verdict = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE])); 4327 data->verdict.code = ntohl(nla_get_be32(tb[NFTA_VERDICT_CODE]));
3992 4328
3993 switch (data->verdict) { 4329 switch (data->verdict.code) {
3994 default: 4330 default:
3995 switch (data->verdict & NF_VERDICT_MASK) { 4331 switch (data->verdict.code & NF_VERDICT_MASK) {
3996 case NF_ACCEPT: 4332 case NF_ACCEPT:
3997 case NF_DROP: 4333 case NF_DROP:
3998 case NF_QUEUE: 4334 case NF_QUEUE:
@@ -4018,7 +4354,7 @@ static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4018 return -EOPNOTSUPP; 4354 return -EOPNOTSUPP;
4019 4355
4020 chain->use++; 4356 chain->use++;
4021 data->chain = chain; 4357 data->verdict.chain = chain;
4022 desc->len = sizeof(data); 4358 desc->len = sizeof(data);
4023 break; 4359 break;
4024 } 4360 }
@@ -4029,10 +4365,10 @@ static int nft_verdict_init(const struct nft_ctx *ctx, struct nft_data *data,
4029 4365
4030static void nft_verdict_uninit(const struct nft_data *data) 4366static void nft_verdict_uninit(const struct nft_data *data)
4031{ 4367{
4032 switch (data->verdict) { 4368 switch (data->verdict.code) {
4033 case NFT_JUMP: 4369 case NFT_JUMP:
4034 case NFT_GOTO: 4370 case NFT_GOTO:
4035 data->chain->use--; 4371 data->verdict.chain->use--;
4036 break; 4372 break;
4037 } 4373 }
4038} 4374}
@@ -4045,13 +4381,14 @@ static int nft_verdict_dump(struct sk_buff *skb, const struct nft_data *data)
4045 if (!nest) 4381 if (!nest)
4046 goto nla_put_failure; 4382 goto nla_put_failure;
4047 4383
4048 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict))) 4384 if (nla_put_be32(skb, NFTA_VERDICT_CODE, htonl(data->verdict.code)))
4049 goto nla_put_failure; 4385 goto nla_put_failure;
4050 4386
4051 switch (data->verdict) { 4387 switch (data->verdict.code) {
4052 case NFT_JUMP: 4388 case NFT_JUMP:
4053 case NFT_GOTO: 4389 case NFT_GOTO:
4054 if (nla_put_string(skb, NFTA_VERDICT_CHAIN, data->chain->name)) 4390 if (nla_put_string(skb, NFTA_VERDICT_CHAIN,
4391 data->verdict.chain->name))
4055 goto nla_put_failure; 4392 goto nla_put_failure;
4056 } 4393 }
4057 nla_nest_end(skb, nest); 4394 nla_nest_end(skb, nest);
@@ -4061,7 +4398,8 @@ nla_put_failure:
4061 return -1; 4398 return -1;
4062} 4399}
4063 4400
4064static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data, 4401static int nft_value_init(const struct nft_ctx *ctx,
4402 struct nft_data *data, unsigned int size,
4065 struct nft_data_desc *desc, const struct nlattr *nla) 4403 struct nft_data_desc *desc, const struct nlattr *nla)
4066{ 4404{
4067 unsigned int len; 4405 unsigned int len;
@@ -4069,10 +4407,10 @@ static int nft_value_init(const struct nft_ctx *ctx, struct nft_data *data,
4069 len = nla_len(nla); 4407 len = nla_len(nla);
4070 if (len == 0) 4408 if (len == 0)
4071 return -EINVAL; 4409 return -EINVAL;
4072 if (len > sizeof(data->data)) 4410 if (len > size)
4073 return -EOVERFLOW; 4411 return -EOVERFLOW;
4074 4412
4075 nla_memcpy(data->data, nla, sizeof(data->data)); 4413 nla_memcpy(data->data, nla, len);
4076 desc->type = NFT_DATA_VALUE; 4414 desc->type = NFT_DATA_VALUE;
4077 desc->len = len; 4415 desc->len = len;
4078 return 0; 4416 return 0;
@@ -4085,8 +4423,7 @@ static int nft_value_dump(struct sk_buff *skb, const struct nft_data *data,
4085} 4423}
4086 4424
4087static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = { 4425static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4088 [NFTA_DATA_VALUE] = { .type = NLA_BINARY, 4426 [NFTA_DATA_VALUE] = { .type = NLA_BINARY },
4089 .len = FIELD_SIZEOF(struct nft_data, data) },
4090 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED }, 4427 [NFTA_DATA_VERDICT] = { .type = NLA_NESTED },
4091}; 4428};
4092 4429
@@ -4095,6 +4432,7 @@ static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4095 * 4432 *
4096 * @ctx: context of the expression using the data 4433 * @ctx: context of the expression using the data
4097 * @data: destination struct nft_data 4434 * @data: destination struct nft_data
4435 * @size: maximum data length
4098 * @desc: data description 4436 * @desc: data description
4099 * @nla: netlink attribute containing data 4437 * @nla: netlink attribute containing data
4100 * 4438 *
@@ -4104,7 +4442,8 @@ static const struct nla_policy nft_data_policy[NFTA_DATA_MAX + 1] = {
4104 * The caller can indicate that it only wants to accept data of type 4442 * The caller can indicate that it only wants to accept data of type
4105 * NFT_DATA_VALUE by passing NULL for the ctx argument. 4443 * NFT_DATA_VALUE by passing NULL for the ctx argument.
4106 */ 4444 */
4107int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data, 4445int nft_data_init(const struct nft_ctx *ctx,
4446 struct nft_data *data, unsigned int size,
4108 struct nft_data_desc *desc, const struct nlattr *nla) 4447 struct nft_data_desc *desc, const struct nlattr *nla)
4109{ 4448{
4110 struct nlattr *tb[NFTA_DATA_MAX + 1]; 4449 struct nlattr *tb[NFTA_DATA_MAX + 1];
@@ -4115,7 +4454,8 @@ int nft_data_init(const struct nft_ctx *ctx, struct nft_data *data,
4115 return err; 4454 return err;
4116 4455
4117 if (tb[NFTA_DATA_VALUE]) 4456 if (tb[NFTA_DATA_VALUE])
4118 return nft_value_init(ctx, data, desc, tb[NFTA_DATA_VALUE]); 4457 return nft_value_init(ctx, data, size, desc,
4458 tb[NFTA_DATA_VALUE]);
4119 if (tb[NFTA_DATA_VERDICT] && ctx != NULL) 4459 if (tb[NFTA_DATA_VERDICT] && ctx != NULL)
4120 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]); 4460 return nft_verdict_init(ctx, data, desc, tb[NFTA_DATA_VERDICT]);
4121 return -EINVAL; 4461 return -EINVAL;
diff --git a/net/netfilter/nf_tables_core.c b/net/netfilter/nf_tables_core.c
index 2d298dccb6dd..f153b07073af 100644
--- a/net/netfilter/nf_tables_core.c
+++ b/net/netfilter/nf_tables_core.c
@@ -8,6 +8,7 @@
8 * Development of this code funded by Astaro AG (http://www.astaro.com/) 8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */ 9 */
10 10
11#include <linux/kernel.h>
11#include <linux/module.h> 12#include <linux/module.h>
12#include <linux/init.h> 13#include <linux/init.h>
13#include <linux/list.h> 14#include <linux/list.h>
@@ -21,24 +22,66 @@
21#include <net/netfilter/nf_tables.h> 22#include <net/netfilter/nf_tables.h>
22#include <net/netfilter/nf_log.h> 23#include <net/netfilter/nf_log.h>
23 24
25enum nft_trace {
26 NFT_TRACE_RULE,
27 NFT_TRACE_RETURN,
28 NFT_TRACE_POLICY,
29};
30
31static const char *const comments[] = {
32 [NFT_TRACE_RULE] = "rule",
33 [NFT_TRACE_RETURN] = "return",
34 [NFT_TRACE_POLICY] = "policy",
35};
36
37static struct nf_loginfo trace_loginfo = {
38 .type = NF_LOG_TYPE_LOG,
39 .u = {
40 .log = {
41 .level = LOGLEVEL_WARNING,
42 .logflags = NF_LOG_MASK,
43 },
44 },
45};
46
47static void __nft_trace_packet(const struct nft_pktinfo *pkt,
48 const struct nft_chain *chain,
49 int rulenum, enum nft_trace type)
50{
51 struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
52
53 nf_log_trace(net, pkt->xt.family, pkt->ops->hooknum, pkt->skb, pkt->in,
54 pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ",
55 chain->table->name, chain->name, comments[type],
56 rulenum);
57}
58
59static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
60 const struct nft_chain *chain,
61 int rulenum, enum nft_trace type)
62{
63 if (unlikely(pkt->skb->nf_trace))
64 __nft_trace_packet(pkt, chain, rulenum, type);
65}
66
24static void nft_cmp_fast_eval(const struct nft_expr *expr, 67static void nft_cmp_fast_eval(const struct nft_expr *expr,
25 struct nft_data data[NFT_REG_MAX + 1]) 68 struct nft_regs *regs)
26{ 69{
27 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); 70 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
28 u32 mask = nft_cmp_fast_mask(priv->len); 71 u32 mask = nft_cmp_fast_mask(priv->len);
29 72
30 if ((data[priv->sreg].data[0] & mask) == priv->data) 73 if ((regs->data[priv->sreg] & mask) == priv->data)
31 return; 74 return;
32 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 75 regs->verdict.code = NFT_BREAK;
33} 76}
34 77
35static bool nft_payload_fast_eval(const struct nft_expr *expr, 78static bool nft_payload_fast_eval(const struct nft_expr *expr,
36 struct nft_data data[NFT_REG_MAX + 1], 79 struct nft_regs *regs,
37 const struct nft_pktinfo *pkt) 80 const struct nft_pktinfo *pkt)
38{ 81{
39 const struct nft_payload *priv = nft_expr_priv(expr); 82 const struct nft_payload *priv = nft_expr_priv(expr);
40 const struct sk_buff *skb = pkt->skb; 83 const struct sk_buff *skb = pkt->skb;
41 struct nft_data *dest = &data[priv->dreg]; 84 u32 *dest = &regs->data[priv->dreg];
42 unsigned char *ptr; 85 unsigned char *ptr;
43 86
44 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER) 87 if (priv->base == NFT_PAYLOAD_NETWORK_HEADER)
@@ -51,12 +94,13 @@ static bool nft_payload_fast_eval(const struct nft_expr *expr,
51 if (unlikely(ptr + priv->len >= skb_tail_pointer(skb))) 94 if (unlikely(ptr + priv->len >= skb_tail_pointer(skb)))
52 return false; 95 return false;
53 96
97 *dest = 0;
54 if (priv->len == 2) 98 if (priv->len == 2)
55 *(u16 *)dest->data = *(u16 *)ptr; 99 *(u16 *)dest = *(u16 *)ptr;
56 else if (priv->len == 4) 100 else if (priv->len == 4)
57 *(u32 *)dest->data = *(u32 *)ptr; 101 *(u32 *)dest = *(u32 *)ptr;
58 else 102 else
59 *(u8 *)dest->data = *(u8 *)ptr; 103 *(u8 *)dest = *(u8 *)ptr;
60 return true; 104 return true;
61} 105}
62 106
@@ -66,62 +110,25 @@ struct nft_jumpstack {
66 int rulenum; 110 int rulenum;
67}; 111};
68 112
69enum nft_trace {
70 NFT_TRACE_RULE,
71 NFT_TRACE_RETURN,
72 NFT_TRACE_POLICY,
73};
74
75static const char *const comments[] = {
76 [NFT_TRACE_RULE] = "rule",
77 [NFT_TRACE_RETURN] = "return",
78 [NFT_TRACE_POLICY] = "policy",
79};
80
81static struct nf_loginfo trace_loginfo = {
82 .type = NF_LOG_TYPE_LOG,
83 .u = {
84 .log = {
85 .level = 4,
86 .logflags = NF_LOG_MASK,
87 },
88 },
89};
90
91static void nft_trace_packet(const struct nft_pktinfo *pkt,
92 const struct nft_chain *chain,
93 int rulenum, enum nft_trace type)
94{
95 struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
96
97 nf_log_trace(net, pkt->xt.family, pkt->ops->hooknum, pkt->skb, pkt->in,
98 pkt->out, &trace_loginfo, "TRACE: %s:%s:%s:%u ",
99 chain->table->name, chain->name, comments[type],
100 rulenum);
101}
102
103unsigned int 113unsigned int
104nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops) 114nft_do_chain(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)
105{ 115{
106 const struct nft_chain *chain = ops->priv, *basechain = chain; 116 const struct nft_chain *chain = ops->priv, *basechain = chain;
117 const struct net *net = read_pnet(&nft_base_chain(basechain)->pnet);
107 const struct nft_rule *rule; 118 const struct nft_rule *rule;
108 const struct nft_expr *expr, *last; 119 const struct nft_expr *expr, *last;
109 struct nft_data data[NFT_REG_MAX + 1]; 120 struct nft_regs regs;
110 unsigned int stackptr = 0; 121 unsigned int stackptr = 0;
111 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE]; 122 struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE];
112 struct nft_stats *stats; 123 struct nft_stats *stats;
113 int rulenum; 124 int rulenum;
114 /* 125 unsigned int gencursor = nft_genmask_cur(net);
115 * Cache cursor to avoid problems in case that the cursor is updated
116 * while traversing the ruleset.
117 */
118 unsigned int gencursor = ACCESS_ONCE(chain->net->nft.gencursor);
119 126
120do_chain: 127do_chain:
121 rulenum = 0; 128 rulenum = 0;
122 rule = list_entry(&chain->rules, struct nft_rule, list); 129 rule = list_entry(&chain->rules, struct nft_rule, list);
123next_rule: 130next_rule:
124 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; 131 regs.verdict.code = NFT_CONTINUE;
125 list_for_each_entry_continue_rcu(rule, &chain->rules, list) { 132 list_for_each_entry_continue_rcu(rule, &chain->rules, list) {
126 133
127 /* This rule is not active, skip. */ 134 /* This rule is not active, skip. */
@@ -132,62 +139,52 @@ next_rule:
132 139
133 nft_rule_for_each_expr(expr, last, rule) { 140 nft_rule_for_each_expr(expr, last, rule) {
134 if (expr->ops == &nft_cmp_fast_ops) 141 if (expr->ops == &nft_cmp_fast_ops)
135 nft_cmp_fast_eval(expr, data); 142 nft_cmp_fast_eval(expr, &regs);
136 else if (expr->ops != &nft_payload_fast_ops || 143 else if (expr->ops != &nft_payload_fast_ops ||
137 !nft_payload_fast_eval(expr, data, pkt)) 144 !nft_payload_fast_eval(expr, &regs, pkt))
138 expr->ops->eval(expr, data, pkt); 145 expr->ops->eval(expr, &regs, pkt);
139 146
140 if (data[NFT_REG_VERDICT].verdict != NFT_CONTINUE) 147 if (regs.verdict.code != NFT_CONTINUE)
141 break; 148 break;
142 } 149 }
143 150
144 switch (data[NFT_REG_VERDICT].verdict) { 151 switch (regs.verdict.code) {
145 case NFT_BREAK: 152 case NFT_BREAK:
146 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; 153 regs.verdict.code = NFT_CONTINUE;
147 continue; 154 continue;
148 case NFT_CONTINUE: 155 case NFT_CONTINUE:
149 if (unlikely(pkt->skb->nf_trace)) 156 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
150 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
151 continue; 157 continue;
152 } 158 }
153 break; 159 break;
154 } 160 }
155 161
156 switch (data[NFT_REG_VERDICT].verdict & NF_VERDICT_MASK) { 162 switch (regs.verdict.code & NF_VERDICT_MASK) {
157 case NF_ACCEPT: 163 case NF_ACCEPT:
158 case NF_DROP: 164 case NF_DROP:
159 case NF_QUEUE: 165 case NF_QUEUE:
160 if (unlikely(pkt->skb->nf_trace)) 166 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
161 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE); 167 return regs.verdict.code;
162
163 return data[NFT_REG_VERDICT].verdict;
164 } 168 }
165 169
166 switch (data[NFT_REG_VERDICT].verdict) { 170 switch (regs.verdict.code) {
167 case NFT_JUMP: 171 case NFT_JUMP:
168 if (unlikely(pkt->skb->nf_trace))
169 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
170
171 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE); 172 BUG_ON(stackptr >= NFT_JUMP_STACK_SIZE);
172 jumpstack[stackptr].chain = chain; 173 jumpstack[stackptr].chain = chain;
173 jumpstack[stackptr].rule = rule; 174 jumpstack[stackptr].rule = rule;
174 jumpstack[stackptr].rulenum = rulenum; 175 jumpstack[stackptr].rulenum = rulenum;
175 stackptr++; 176 stackptr++;
176 chain = data[NFT_REG_VERDICT].chain; 177 /* fall through */
177 goto do_chain;
178 case NFT_GOTO: 178 case NFT_GOTO:
179 if (unlikely(pkt->skb->nf_trace)) 179 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
180 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RULE);
181 180
182 chain = data[NFT_REG_VERDICT].chain; 181 chain = regs.verdict.chain;
183 goto do_chain; 182 goto do_chain;
184 case NFT_RETURN:
185 if (unlikely(pkt->skb->nf_trace))
186 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
187 break;
188 case NFT_CONTINUE: 183 case NFT_CONTINUE:
189 if (unlikely(pkt->skb->nf_trace && !(chain->flags & NFT_BASE_CHAIN))) 184 rulenum++;
190 nft_trace_packet(pkt, chain, ++rulenum, NFT_TRACE_RETURN); 185 /* fall through */
186 case NFT_RETURN:
187 nft_trace_packet(pkt, chain, rulenum, NFT_TRACE_RETURN);
191 break; 188 break;
192 default: 189 default:
193 WARN_ON(1); 190 WARN_ON(1);
@@ -201,8 +198,7 @@ next_rule:
201 goto next_rule; 198 goto next_rule;
202 } 199 }
203 200
204 if (unlikely(pkt->skb->nf_trace)) 201 nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY);
205 nft_trace_packet(pkt, basechain, -1, NFT_TRACE_POLICY);
206 202
207 rcu_read_lock_bh(); 203 rcu_read_lock_bh();
208 stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats)); 204 stats = this_cpu_ptr(rcu_dereference(nft_base_chain(basechain)->stats));
@@ -244,8 +240,14 @@ int __init nf_tables_core_module_init(void)
244 if (err < 0) 240 if (err < 0)
245 goto err6; 241 goto err6;
246 242
243 err = nft_dynset_module_init();
244 if (err < 0)
245 goto err7;
246
247 return 0; 247 return 0;
248 248
249err7:
250 nft_payload_module_exit();
249err6: 251err6:
250 nft_byteorder_module_exit(); 252 nft_byteorder_module_exit();
251err5: 253err5:
@@ -262,6 +264,7 @@ err1:
262 264
263void nf_tables_core_module_exit(void) 265void nf_tables_core_module_exit(void)
264{ 266{
267 nft_dynset_module_exit();
265 nft_payload_module_exit(); 268 nft_payload_module_exit();
266 nft_byteorder_module_exit(); 269 nft_byteorder_module_exit();
267 nft_bitwise_module_exit(); 270 nft_bitwise_module_exit();
diff --git a/net/netfilter/nfnetlink_log.c b/net/netfilter/nfnetlink_log.c
index 11d85b3813f2..3ad91266c821 100644
--- a/net/netfilter/nfnetlink_log.c
+++ b/net/netfilter/nfnetlink_log.c
@@ -23,6 +23,7 @@
23#include <linux/ipv6.h> 23#include <linux/ipv6.h>
24#include <linux/netdevice.h> 24#include <linux/netdevice.h>
25#include <linux/netfilter.h> 25#include <linux/netfilter.h>
26#include <linux/netfilter_bridge.h>
26#include <net/netlink.h> 27#include <net/netlink.h>
27#include <linux/netfilter/nfnetlink.h> 28#include <linux/netfilter/nfnetlink.h>
28#include <linux/netfilter/nfnetlink_log.h> 29#include <linux/netfilter/nfnetlink_log.h>
@@ -62,7 +63,7 @@ struct nfulnl_instance {
62 struct timer_list timer; 63 struct timer_list timer;
63 struct net *net; 64 struct net *net;
64 struct user_namespace *peer_user_ns; /* User namespace of the peer process */ 65 struct user_namespace *peer_user_ns; /* User namespace of the peer process */
65 int peer_portid; /* PORTID of the peer process */ 66 u32 peer_portid; /* PORTID of the peer process */
66 67
67 /* configurable parameters */ 68 /* configurable parameters */
68 unsigned int flushtimeout; /* timeout until queue flush */ 69 unsigned int flushtimeout; /* timeout until queue flush */
@@ -151,7 +152,7 @@ static void nfulnl_timer(unsigned long data);
151 152
152static struct nfulnl_instance * 153static struct nfulnl_instance *
153instance_create(struct net *net, u_int16_t group_num, 154instance_create(struct net *net, u_int16_t group_num,
154 int portid, struct user_namespace *user_ns) 155 u32 portid, struct user_namespace *user_ns)
155{ 156{
156 struct nfulnl_instance *inst; 157 struct nfulnl_instance *inst;
157 struct nfnl_log_net *log = nfnl_log_pernet(net); 158 struct nfnl_log_net *log = nfnl_log_pernet(net);
@@ -448,14 +449,18 @@ __build_packet_message(struct nfnl_log_net *log,
448 htonl(br_port_get_rcu(indev)->br->dev->ifindex))) 449 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
449 goto nla_put_failure; 450 goto nla_put_failure;
450 } else { 451 } else {
452 struct net_device *physindev;
453
451 /* Case 2: indev is bridge group, we need to look for 454 /* Case 2: indev is bridge group, we need to look for
452 * physical device (when called from ipv4) */ 455 * physical device (when called from ipv4) */
453 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV, 456 if (nla_put_be32(inst->skb, NFULA_IFINDEX_INDEV,
454 htonl(indev->ifindex))) 457 htonl(indev->ifindex)))
455 goto nla_put_failure; 458 goto nla_put_failure;
456 if (skb->nf_bridge && skb->nf_bridge->physindev && 459
460 physindev = nf_bridge_get_physindev(skb);
461 if (physindev &&
457 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV, 462 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
458 htonl(skb->nf_bridge->physindev->ifindex))) 463 htonl(physindev->ifindex)))
459 goto nla_put_failure; 464 goto nla_put_failure;
460 } 465 }
461#endif 466#endif
@@ -479,14 +484,18 @@ __build_packet_message(struct nfnl_log_net *log,
479 htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) 484 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
480 goto nla_put_failure; 485 goto nla_put_failure;
481 } else { 486 } else {
487 struct net_device *physoutdev;
488
482 /* Case 2: indev is a bridge group, we need to look 489 /* Case 2: indev is a bridge group, we need to look
483 * for physical device (when called from ipv4) */ 490 * for physical device (when called from ipv4) */
484 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV, 491 if (nla_put_be32(inst->skb, NFULA_IFINDEX_OUTDEV,
485 htonl(outdev->ifindex))) 492 htonl(outdev->ifindex)))
486 goto nla_put_failure; 493 goto nla_put_failure;
487 if (skb->nf_bridge && skb->nf_bridge->physoutdev && 494
495 physoutdev = nf_bridge_get_physoutdev(skb);
496 if (physoutdev &&
488 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV, 497 nla_put_be32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
489 htonl(skb->nf_bridge->physoutdev->ifindex))) 498 htonl(physoutdev->ifindex)))
490 goto nla_put_failure; 499 goto nla_put_failure;
491 } 500 }
492#endif 501#endif
@@ -539,7 +548,7 @@ __build_packet_message(struct nfnl_log_net *log,
539 548
540 /* UID */ 549 /* UID */
541 sk = skb->sk; 550 sk = skb->sk;
542 if (sk && sk->sk_state != TCP_TIME_WAIT) { 551 if (sk && sk_fullsock(sk)) {
543 read_lock_bh(&sk->sk_callback_lock); 552 read_lock_bh(&sk->sk_callback_lock);
544 if (sk->sk_socket && sk->sk_socket->file) { 553 if (sk->sk_socket && sk->sk_socket->file) {
545 struct file *file = sk->sk_socket->file; 554 struct file *file = sk->sk_socket->file;
@@ -998,11 +1007,13 @@ static int seq_show(struct seq_file *s, void *v)
998{ 1007{
999 const struct nfulnl_instance *inst = v; 1008 const struct nfulnl_instance *inst = v;
1000 1009
1001 return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n", 1010 seq_printf(s, "%5u %6u %5u %1u %5u %6u %2u\n",
1002 inst->group_num, 1011 inst->group_num,
1003 inst->peer_portid, inst->qlen, 1012 inst->peer_portid, inst->qlen,
1004 inst->copy_mode, inst->copy_range, 1013 inst->copy_mode, inst->copy_range,
1005 inst->flushtimeout, atomic_read(&inst->use)); 1014 inst->flushtimeout, atomic_read(&inst->use));
1015
1016 return 0;
1006} 1017}
1007 1018
1008static const struct seq_operations nful_seq_ops = { 1019static const struct seq_operations nful_seq_ops = {
diff --git a/net/netfilter/nfnetlink_queue_core.c b/net/netfilter/nfnetlink_queue_core.c
index 0db8515e76da..0b98c7420239 100644
--- a/net/netfilter/nfnetlink_queue_core.c
+++ b/net/netfilter/nfnetlink_queue_core.c
@@ -25,6 +25,7 @@
25#include <linux/proc_fs.h> 25#include <linux/proc_fs.h>
26#include <linux/netfilter_ipv4.h> 26#include <linux/netfilter_ipv4.h>
27#include <linux/netfilter_ipv6.h> 27#include <linux/netfilter_ipv6.h>
28#include <linux/netfilter_bridge.h>
28#include <linux/netfilter/nfnetlink.h> 29#include <linux/netfilter/nfnetlink.h>
29#include <linux/netfilter/nfnetlink_queue.h> 30#include <linux/netfilter/nfnetlink_queue.h>
30#include <linux/list.h> 31#include <linux/list.h>
@@ -54,7 +55,7 @@ struct nfqnl_instance {
54 struct hlist_node hlist; /* global list of queues */ 55 struct hlist_node hlist; /* global list of queues */
55 struct rcu_head rcu; 56 struct rcu_head rcu;
56 57
57 int peer_portid; 58 u32 peer_portid;
58 unsigned int queue_maxlen; 59 unsigned int queue_maxlen;
59 unsigned int copy_range; 60 unsigned int copy_range;
60 unsigned int queue_dropped; 61 unsigned int queue_dropped;
@@ -109,8 +110,7 @@ instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
109} 110}
110 111
111static struct nfqnl_instance * 112static struct nfqnl_instance *
112instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, 113instance_create(struct nfnl_queue_net *q, u_int16_t queue_num, u32 portid)
113 int portid)
114{ 114{
115 struct nfqnl_instance *inst; 115 struct nfqnl_instance *inst;
116 unsigned int h; 116 unsigned int h;
@@ -257,7 +257,7 @@ static int nfqnl_put_sk_uidgid(struct sk_buff *skb, struct sock *sk)
257{ 257{
258 const struct cred *cred; 258 const struct cred *cred;
259 259
260 if (sk->sk_state == TCP_TIME_WAIT) 260 if (!sk_fullsock(sk))
261 return 0; 261 return 0;
262 262
263 read_lock_bh(&sk->sk_callback_lock); 263 read_lock_bh(&sk->sk_callback_lock);
@@ -314,13 +314,13 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
314 if (entskb->tstamp.tv64) 314 if (entskb->tstamp.tv64)
315 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp)); 315 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
316 316
317 if (entry->hook <= NF_INET_FORWARD || 317 if (entry->state.hook <= NF_INET_FORWARD ||
318 (entry->hook == NF_INET_POST_ROUTING && entskb->sk == NULL)) 318 (entry->state.hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
319 csum_verify = !skb_csum_unnecessary(entskb); 319 csum_verify = !skb_csum_unnecessary(entskb);
320 else 320 else
321 csum_verify = false; 321 csum_verify = false;
322 322
323 outdev = entry->outdev; 323 outdev = entry->state.out;
324 324
325 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) { 325 switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
326 case NFQNL_COPY_META: 326 case NFQNL_COPY_META:
@@ -368,23 +368,23 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
368 return NULL; 368 return NULL;
369 } 369 }
370 nfmsg = nlmsg_data(nlh); 370 nfmsg = nlmsg_data(nlh);
371 nfmsg->nfgen_family = entry->pf; 371 nfmsg->nfgen_family = entry->state.pf;
372 nfmsg->version = NFNETLINK_V0; 372 nfmsg->version = NFNETLINK_V0;
373 nfmsg->res_id = htons(queue->queue_num); 373 nfmsg->res_id = htons(queue->queue_num);
374 374
375 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg)); 375 nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
376 pmsg = nla_data(nla); 376 pmsg = nla_data(nla);
377 pmsg->hw_protocol = entskb->protocol; 377 pmsg->hw_protocol = entskb->protocol;
378 pmsg->hook = entry->hook; 378 pmsg->hook = entry->state.hook;
379 *packet_id_ptr = &pmsg->packet_id; 379 *packet_id_ptr = &pmsg->packet_id;
380 380
381 indev = entry->indev; 381 indev = entry->state.in;
382 if (indev) { 382 if (indev) {
383#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 383#if !IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
384 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex))) 384 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
385 goto nla_put_failure; 385 goto nla_put_failure;
386#else 386#else
387 if (entry->pf == PF_BRIDGE) { 387 if (entry->state.pf == PF_BRIDGE) {
388 /* Case 1: indev is physical input device, we need to 388 /* Case 1: indev is physical input device, we need to
389 * look for bridge group (when called from 389 * look for bridge group (when called from
390 * netfilter_bridge) */ 390 * netfilter_bridge) */
@@ -396,14 +396,18 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
396 htonl(br_port_get_rcu(indev)->br->dev->ifindex))) 396 htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
397 goto nla_put_failure; 397 goto nla_put_failure;
398 } else { 398 } else {
399 int physinif;
400
399 /* Case 2: indev is bridge group, we need to look for 401 /* Case 2: indev is bridge group, we need to look for
400 * physical device (when called from ipv4) */ 402 * physical device (when called from ipv4) */
401 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, 403 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
402 htonl(indev->ifindex))) 404 htonl(indev->ifindex)))
403 goto nla_put_failure; 405 goto nla_put_failure;
404 if (entskb->nf_bridge && entskb->nf_bridge->physindev && 406
407 physinif = nf_bridge_get_physinif(entskb);
408 if (physinif &&
405 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV, 409 nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
406 htonl(entskb->nf_bridge->physindev->ifindex))) 410 htonl(physinif)))
407 goto nla_put_failure; 411 goto nla_put_failure;
408 } 412 }
409#endif 413#endif
@@ -414,7 +418,7 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
414 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex))) 418 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
415 goto nla_put_failure; 419 goto nla_put_failure;
416#else 420#else
417 if (entry->pf == PF_BRIDGE) { 421 if (entry->state.pf == PF_BRIDGE) {
418 /* Case 1: outdev is physical output device, we need to 422 /* Case 1: outdev is physical output device, we need to
419 * look for bridge group (when called from 423 * look for bridge group (when called from
420 * netfilter_bridge) */ 424 * netfilter_bridge) */
@@ -426,14 +430,18 @@ nfqnl_build_packet_message(struct net *net, struct nfqnl_instance *queue,
426 htonl(br_port_get_rcu(outdev)->br->dev->ifindex))) 430 htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
427 goto nla_put_failure; 431 goto nla_put_failure;
428 } else { 432 } else {
433 int physoutif;
434
429 /* Case 2: outdev is bridge group, we need to look for 435 /* Case 2: outdev is bridge group, we need to look for
430 * physical output device (when called from ipv4) */ 436 * physical output device (when called from ipv4) */
431 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, 437 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
432 htonl(outdev->ifindex))) 438 htonl(outdev->ifindex)))
433 goto nla_put_failure; 439 goto nla_put_failure;
434 if (entskb->nf_bridge && entskb->nf_bridge->physoutdev && 440
441 physoutif = nf_bridge_get_physoutif(entskb);
442 if (physoutif &&
435 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV, 443 nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
436 htonl(entskb->nf_bridge->physoutdev->ifindex))) 444 htonl(physoutif)))
437 goto nla_put_failure; 445 goto nla_put_failure;
438 } 446 }
439#endif 447#endif
@@ -633,8 +641,8 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
633 struct nfqnl_instance *queue; 641 struct nfqnl_instance *queue;
634 struct sk_buff *skb, *segs; 642 struct sk_buff *skb, *segs;
635 int err = -ENOBUFS; 643 int err = -ENOBUFS;
636 struct net *net = dev_net(entry->indev ? 644 struct net *net = dev_net(entry->state.in ?
637 entry->indev : entry->outdev); 645 entry->state.in : entry->state.out);
638 struct nfnl_queue_net *q = nfnl_queue_pernet(net); 646 struct nfnl_queue_net *q = nfnl_queue_pernet(net);
639 647
640 /* rcu_read_lock()ed by nf_hook_slow() */ 648 /* rcu_read_lock()ed by nf_hook_slow() */
@@ -647,7 +655,7 @@ nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
647 655
648 skb = entry->skb; 656 skb = entry->skb;
649 657
650 switch (entry->pf) { 658 switch (entry->state.pf) {
651 case NFPROTO_IPV4: 659 case NFPROTO_IPV4:
652 skb->protocol = htons(ETH_P_IP); 660 skb->protocol = htons(ETH_P_IP);
653 break; 661 break;
@@ -757,19 +765,20 @@ nfqnl_set_mode(struct nfqnl_instance *queue,
757static int 765static int
758dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex) 766dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
759{ 767{
760 if (entry->indev) 768 if (entry->state.in)
761 if (entry->indev->ifindex == ifindex) 769 if (entry->state.in->ifindex == ifindex)
762 return 1; 770 return 1;
763 if (entry->outdev) 771 if (entry->state.out)
764 if (entry->outdev->ifindex == ifindex) 772 if (entry->state.out->ifindex == ifindex)
765 return 1; 773 return 1;
766#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER) 774#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
767 if (entry->skb->nf_bridge) { 775 if (entry->skb->nf_bridge) {
768 if (entry->skb->nf_bridge->physindev && 776 int physinif, physoutif;
769 entry->skb->nf_bridge->physindev->ifindex == ifindex) 777
770 return 1; 778 physinif = nf_bridge_get_physinif(entry->skb);
771 if (entry->skb->nf_bridge->physoutdev && 779 physoutif = nf_bridge_get_physoutif(entry->skb);
772 entry->skb->nf_bridge->physoutdev->ifindex == ifindex) 780
781 if (physinif == ifindex || physoutif == ifindex)
773 return 1; 782 return 1;
774 } 783 }
775#endif 784#endif
@@ -860,7 +869,7 @@ static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
860}; 869};
861 870
862static struct nfqnl_instance * 871static struct nfqnl_instance *
863verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid) 872verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, u32 nlportid)
864{ 873{
865 struct nfqnl_instance *queue; 874 struct nfqnl_instance *queue;
866 875
@@ -1242,7 +1251,7 @@ static int seq_show(struct seq_file *s, void *v)
1242{ 1251{
1243 const struct nfqnl_instance *inst = v; 1252 const struct nfqnl_instance *inst = v;
1244 1253
1245 seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n", 1254 seq_printf(s, "%5u %6u %5u %1u %5u %5u %5u %8u %2d\n",
1246 inst->queue_num, 1255 inst->queue_num,
1247 inst->peer_portid, inst->queue_total, 1256 inst->peer_portid, inst->queue_total,
1248 inst->copy_mode, inst->copy_range, 1257 inst->copy_mode, inst->copy_range,
diff --git a/net/netfilter/nft_bitwise.c b/net/netfilter/nft_bitwise.c
index 4fb6ee2c1106..d71cc18fa35d 100644
--- a/net/netfilter/nft_bitwise.c
+++ b/net/netfilter/nft_bitwise.c
@@ -26,18 +26,16 @@ struct nft_bitwise {
26}; 26};
27 27
28static void nft_bitwise_eval(const struct nft_expr *expr, 28static void nft_bitwise_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1], 29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt) 30 const struct nft_pktinfo *pkt)
31{ 31{
32 const struct nft_bitwise *priv = nft_expr_priv(expr); 32 const struct nft_bitwise *priv = nft_expr_priv(expr);
33 const struct nft_data *src = &data[priv->sreg]; 33 const u32 *src = &regs->data[priv->sreg];
34 struct nft_data *dst = &data[priv->dreg]; 34 u32 *dst = &regs->data[priv->dreg];
35 unsigned int i; 35 unsigned int i;
36 36
37 for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++) { 37 for (i = 0; i < DIV_ROUND_UP(priv->len, 4); i++)
38 dst->data[i] = (src->data[i] & priv->mask.data[i]) ^ 38 dst[i] = (src[i] & priv->mask.data[i]) ^ priv->xor.data[i];
39 priv->xor.data[i];
40 }
41} 39}
42 40
43static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = { 41static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = {
@@ -63,28 +61,27 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,
63 tb[NFTA_BITWISE_XOR] == NULL) 61 tb[NFTA_BITWISE_XOR] == NULL)
64 return -EINVAL; 62 return -EINVAL;
65 63
66 priv->sreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_SREG])); 64 priv->len = ntohl(nla_get_be32(tb[NFTA_BITWISE_LEN]));
67 err = nft_validate_input_register(priv->sreg); 65 priv->sreg = nft_parse_register(tb[NFTA_BITWISE_SREG]);
66 err = nft_validate_register_load(priv->sreg, priv->len);
68 if (err < 0) 67 if (err < 0)
69 return err; 68 return err;
70 69
71 priv->dreg = ntohl(nla_get_be32(tb[NFTA_BITWISE_DREG])); 70 priv->dreg = nft_parse_register(tb[NFTA_BITWISE_DREG]);
72 err = nft_validate_output_register(priv->dreg); 71 err = nft_validate_register_store(ctx, priv->dreg, NULL,
72 NFT_DATA_VALUE, priv->len);
73 if (err < 0) 73 if (err < 0)
74 return err; 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 75
81 err = nft_data_init(NULL, &priv->mask, &d1, tb[NFTA_BITWISE_MASK]); 76 err = nft_data_init(NULL, &priv->mask, sizeof(priv->mask), &d1,
77 tb[NFTA_BITWISE_MASK]);
82 if (err < 0) 78 if (err < 0)
83 return err; 79 return err;
84 if (d1.len != priv->len) 80 if (d1.len != priv->len)
85 return -EINVAL; 81 return -EINVAL;
86 82
87 err = nft_data_init(NULL, &priv->xor, &d2, tb[NFTA_BITWISE_XOR]); 83 err = nft_data_init(NULL, &priv->xor, sizeof(priv->xor), &d2,
84 tb[NFTA_BITWISE_XOR]);
88 if (err < 0) 85 if (err < 0)
89 return err; 86 return err;
90 if (d2.len != priv->len) 87 if (d2.len != priv->len)
@@ -97,9 +94,9 @@ static int nft_bitwise_dump(struct sk_buff *skb, const struct nft_expr *expr)
97{ 94{
98 const struct nft_bitwise *priv = nft_expr_priv(expr); 95 const struct nft_bitwise *priv = nft_expr_priv(expr);
99 96
100 if (nla_put_be32(skb, NFTA_BITWISE_SREG, htonl(priv->sreg))) 97 if (nft_dump_register(skb, NFTA_BITWISE_SREG, priv->sreg))
101 goto nla_put_failure; 98 goto nla_put_failure;
102 if (nla_put_be32(skb, NFTA_BITWISE_DREG, htonl(priv->dreg))) 99 if (nft_dump_register(skb, NFTA_BITWISE_DREG, priv->dreg))
103 goto nla_put_failure; 100 goto nla_put_failure;
104 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len))) 101 if (nla_put_be32(skb, NFTA_BITWISE_LEN, htonl(priv->len)))
105 goto nla_put_failure; 102 goto nla_put_failure;
diff --git a/net/netfilter/nft_byteorder.c b/net/netfilter/nft_byteorder.c
index c39ed8d29df1..fde5145f2e36 100644
--- a/net/netfilter/nft_byteorder.c
+++ b/net/netfilter/nft_byteorder.c
@@ -26,16 +26,17 @@ struct nft_byteorder {
26}; 26};
27 27
28static void nft_byteorder_eval(const struct nft_expr *expr, 28static void nft_byteorder_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1], 29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt) 30 const struct nft_pktinfo *pkt)
31{ 31{
32 const struct nft_byteorder *priv = nft_expr_priv(expr); 32 const struct nft_byteorder *priv = nft_expr_priv(expr);
33 struct nft_data *src = &data[priv->sreg], *dst = &data[priv->dreg]; 33 u32 *src = &regs->data[priv->sreg];
34 u32 *dst = &regs->data[priv->dreg];
34 union { u32 u32; u16 u16; } *s, *d; 35 union { u32 u32; u16 u16; } *s, *d;
35 unsigned int i; 36 unsigned int i;
36 37
37 s = (void *)src->data; 38 s = (void *)src;
38 d = (void *)dst->data; 39 d = (void *)dst;
39 40
40 switch (priv->size) { 41 switch (priv->size) {
41 case 4: 42 case 4:
@@ -87,19 +88,6 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
87 tb[NFTA_BYTEORDER_OP] == NULL) 88 tb[NFTA_BYTEORDER_OP] == NULL)
88 return -EINVAL; 89 return -EINVAL;
89 90
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])); 91 priv->op = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_OP]));
104 switch (priv->op) { 92 switch (priv->op) {
105 case NFT_BYTEORDER_NTOH: 93 case NFT_BYTEORDER_NTOH:
@@ -109,10 +97,6 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
109 return -EINVAL; 97 return -EINVAL;
110 } 98 }
111 99
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])); 100 priv->size = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SIZE]));
117 switch (priv->size) { 101 switch (priv->size) {
118 case 2: 102 case 2:
@@ -122,16 +106,24 @@ static int nft_byteorder_init(const struct nft_ctx *ctx,
122 return -EINVAL; 106 return -EINVAL;
123 } 107 }
124 108
125 return 0; 109 priv->sreg = nft_parse_register(tb[NFTA_BYTEORDER_SREG]);
110 priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
111 err = nft_validate_register_load(priv->sreg, priv->len);
112 if (err < 0)
113 return err;
114
115 priv->dreg = nft_parse_register(tb[NFTA_BYTEORDER_DREG]);
116 return nft_validate_register_store(ctx, priv->dreg, NULL,
117 NFT_DATA_VALUE, priv->len);
126} 118}
127 119
128static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr) 120static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr)
129{ 121{
130 const struct nft_byteorder *priv = nft_expr_priv(expr); 122 const struct nft_byteorder *priv = nft_expr_priv(expr);
131 123
132 if (nla_put_be32(skb, NFTA_BYTEORDER_SREG, htonl(priv->sreg))) 124 if (nft_dump_register(skb, NFTA_BYTEORDER_SREG, priv->sreg))
133 goto nla_put_failure; 125 goto nla_put_failure;
134 if (nla_put_be32(skb, NFTA_BYTEORDER_DREG, htonl(priv->dreg))) 126 if (nft_dump_register(skb, NFTA_BYTEORDER_DREG, priv->dreg))
135 goto nla_put_failure; 127 goto nla_put_failure;
136 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op))) 128 if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
137 goto nla_put_failure; 129 goto nla_put_failure;
diff --git a/net/netfilter/nft_cmp.c b/net/netfilter/nft_cmp.c
index e2b3f51c81f1..e25b35d70e4d 100644
--- a/net/netfilter/nft_cmp.c
+++ b/net/netfilter/nft_cmp.c
@@ -25,13 +25,13 @@ struct nft_cmp_expr {
25}; 25};
26 26
27static void nft_cmp_eval(const struct nft_expr *expr, 27static void nft_cmp_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1], 28 struct nft_regs *regs,
29 const struct nft_pktinfo *pkt) 29 const struct nft_pktinfo *pkt)
30{ 30{
31 const struct nft_cmp_expr *priv = nft_expr_priv(expr); 31 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
32 int d; 32 int d;
33 33
34 d = nft_data_cmp(&data[priv->sreg], &priv->data, priv->len); 34 d = memcmp(&regs->data[priv->sreg], &priv->data, priv->len);
35 switch (priv->op) { 35 switch (priv->op) {
36 case NFT_CMP_EQ: 36 case NFT_CMP_EQ:
37 if (d != 0) 37 if (d != 0)
@@ -59,7 +59,7 @@ static void nft_cmp_eval(const struct nft_expr *expr,
59 return; 59 return;
60 60
61mismatch: 61mismatch:
62 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 62 regs->verdict.code = NFT_BREAK;
63} 63}
64 64
65static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = { 65static const struct nla_policy nft_cmp_policy[NFTA_CMP_MAX + 1] = {
@@ -75,12 +75,16 @@ static int nft_cmp_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
75 struct nft_data_desc desc; 75 struct nft_data_desc desc;
76 int err; 76 int err;
77 77
78 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG])); 78 err = nft_data_init(NULL, &priv->data, sizeof(priv->data), &desc,
79 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP])); 79 tb[NFTA_CMP_DATA]);
80
81 err = nft_data_init(NULL, &priv->data, &desc, tb[NFTA_CMP_DATA]);
82 BUG_ON(err < 0); 80 BUG_ON(err < 0);
83 81
82 priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
83 err = nft_validate_register_load(priv->sreg, desc.len);
84 if (err < 0)
85 return err;
86
87 priv->op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
84 priv->len = desc.len; 88 priv->len = desc.len;
85 return 0; 89 return 0;
86} 90}
@@ -89,7 +93,7 @@ static int nft_cmp_dump(struct sk_buff *skb, const struct nft_expr *expr)
89{ 93{
90 const struct nft_cmp_expr *priv = nft_expr_priv(expr); 94 const struct nft_cmp_expr *priv = nft_expr_priv(expr);
91 95
92 if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg))) 96 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
93 goto nla_put_failure; 97 goto nla_put_failure;
94 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op))) 98 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(priv->op)))
95 goto nla_put_failure; 99 goto nla_put_failure;
@@ -122,13 +126,18 @@ static int nft_cmp_fast_init(const struct nft_ctx *ctx,
122 u32 mask; 126 u32 mask;
123 int err; 127 int err;
124 128
125 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CMP_SREG])); 129 err = nft_data_init(NULL, &data, sizeof(data), &desc,
126 130 tb[NFTA_CMP_DATA]);
127 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]);
128 BUG_ON(err < 0); 131 BUG_ON(err < 0);
129 desc.len *= BITS_PER_BYTE;
130 132
133 priv->sreg = nft_parse_register(tb[NFTA_CMP_SREG]);
134 err = nft_validate_register_load(priv->sreg, desc.len);
135 if (err < 0)
136 return err;
137
138 desc.len *= BITS_PER_BYTE;
131 mask = nft_cmp_fast_mask(desc.len); 139 mask = nft_cmp_fast_mask(desc.len);
140
132 priv->data = data.data[0] & mask; 141 priv->data = data.data[0] & mask;
133 priv->len = desc.len; 142 priv->len = desc.len;
134 return 0; 143 return 0;
@@ -139,7 +148,7 @@ static int nft_cmp_fast_dump(struct sk_buff *skb, const struct nft_expr *expr)
139 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr); 148 const struct nft_cmp_fast_expr *priv = nft_expr_priv(expr);
140 struct nft_data data; 149 struct nft_data data;
141 150
142 if (nla_put_be32(skb, NFTA_CMP_SREG, htonl(priv->sreg))) 151 if (nft_dump_register(skb, NFTA_CMP_SREG, priv->sreg))
143 goto nla_put_failure; 152 goto nla_put_failure;
144 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ))) 153 if (nla_put_be32(skb, NFTA_CMP_OP, htonl(NFT_CMP_EQ)))
145 goto nla_put_failure; 154 goto nla_put_failure;
@@ -167,7 +176,6 @@ nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
167{ 176{
168 struct nft_data_desc desc; 177 struct nft_data_desc desc;
169 struct nft_data data; 178 struct nft_data data;
170 enum nft_registers sreg;
171 enum nft_cmp_ops op; 179 enum nft_cmp_ops op;
172 int err; 180 int err;
173 181
@@ -176,11 +184,6 @@ nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
176 tb[NFTA_CMP_DATA] == NULL) 184 tb[NFTA_CMP_DATA] == NULL)
177 return ERR_PTR(-EINVAL); 185 return ERR_PTR(-EINVAL);
178 186
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])); 187 op = ntohl(nla_get_be32(tb[NFTA_CMP_OP]));
185 switch (op) { 188 switch (op) {
186 case NFT_CMP_EQ: 189 case NFT_CMP_EQ:
@@ -194,7 +197,8 @@ nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[])
194 return ERR_PTR(-EINVAL); 197 return ERR_PTR(-EINVAL);
195 } 198 }
196 199
197 err = nft_data_init(NULL, &data, &desc, tb[NFTA_CMP_DATA]); 200 err = nft_data_init(NULL, &data, sizeof(data), &desc,
201 tb[NFTA_CMP_DATA]);
198 if (err < 0) 202 if (err < 0)
199 return ERR_PTR(err); 203 return ERR_PTR(err);
200 204
diff --git a/net/netfilter/nft_compat.c b/net/netfilter/nft_compat.c
index 65f3e2b6be44..7f29cfc76349 100644
--- a/net/netfilter/nft_compat.c
+++ b/net/netfilter/nft_compat.c
@@ -20,6 +20,7 @@
20#include <linux/netfilter_ipv4/ip_tables.h> 20#include <linux/netfilter_ipv4/ip_tables.h>
21#include <linux/netfilter_ipv6/ip6_tables.h> 21#include <linux/netfilter_ipv6/ip6_tables.h>
22#include <linux/netfilter_bridge/ebtables.h> 22#include <linux/netfilter_bridge/ebtables.h>
23#include <linux/netfilter_arp/arp_tables.h>
23#include <net/netfilter/nf_tables.h> 24#include <net/netfilter/nf_tables.h>
24 25
25static int nft_compat_chain_validate_dependency(const char *tablename, 26static int nft_compat_chain_validate_dependency(const char *tablename,
@@ -42,6 +43,7 @@ union nft_entry {
42 struct ipt_entry e4; 43 struct ipt_entry e4;
43 struct ip6t_entry e6; 44 struct ip6t_entry e6;
44 struct ebt_entry ebt; 45 struct ebt_entry ebt;
46 struct arpt_entry arp;
45}; 47};
46 48
47static inline void 49static inline void
@@ -53,7 +55,7 @@ nft_compat_set_par(struct xt_action_param *par, void *xt, const void *xt_info)
53} 55}
54 56
55static void nft_target_eval_xt(const struct nft_expr *expr, 57static void nft_target_eval_xt(const struct nft_expr *expr,
56 struct nft_data data[NFT_REG_MAX + 1], 58 struct nft_regs *regs,
57 const struct nft_pktinfo *pkt) 59 const struct nft_pktinfo *pkt)
58{ 60{
59 void *info = nft_expr_priv(expr); 61 void *info = nft_expr_priv(expr);
@@ -70,16 +72,16 @@ static void nft_target_eval_xt(const struct nft_expr *expr,
70 72
71 switch (ret) { 73 switch (ret) {
72 case XT_CONTINUE: 74 case XT_CONTINUE:
73 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; 75 regs->verdict.code = NFT_CONTINUE;
74 break; 76 break;
75 default: 77 default:
76 data[NFT_REG_VERDICT].verdict = ret; 78 regs->verdict.code = ret;
77 break; 79 break;
78 } 80 }
79} 81}
80 82
81static void nft_target_eval_bridge(const struct nft_expr *expr, 83static void nft_target_eval_bridge(const struct nft_expr *expr,
82 struct nft_data data[NFT_REG_MAX + 1], 84 struct nft_regs *regs,
83 const struct nft_pktinfo *pkt) 85 const struct nft_pktinfo *pkt)
84{ 86{
85 void *info = nft_expr_priv(expr); 87 void *info = nft_expr_priv(expr);
@@ -96,19 +98,19 @@ static void nft_target_eval_bridge(const struct nft_expr *expr,
96 98
97 switch (ret) { 99 switch (ret) {
98 case EBT_ACCEPT: 100 case EBT_ACCEPT:
99 data[NFT_REG_VERDICT].verdict = NF_ACCEPT; 101 regs->verdict.code = NF_ACCEPT;
100 break; 102 break;
101 case EBT_DROP: 103 case EBT_DROP:
102 data[NFT_REG_VERDICT].verdict = NF_DROP; 104 regs->verdict.code = NF_DROP;
103 break; 105 break;
104 case EBT_CONTINUE: 106 case EBT_CONTINUE:
105 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; 107 regs->verdict.code = NFT_CONTINUE;
106 break; 108 break;
107 case EBT_RETURN: 109 case EBT_RETURN:
108 data[NFT_REG_VERDICT].verdict = NFT_RETURN; 110 regs->verdict.code = NFT_RETURN;
109 break; 111 break;
110 default: 112 default:
111 data[NFT_REG_VERDICT].verdict = ret; 113 regs->verdict.code = ret;
112 break; 114 break;
113 } 115 }
114} 116}
@@ -143,6 +145,8 @@ nft_target_set_tgchk_param(struct xt_tgchk_param *par,
143 entry->ebt.ethproto = (__force __be16)proto; 145 entry->ebt.ethproto = (__force __be16)proto;
144 entry->ebt.invflags = inv ? EBT_IPROTO : 0; 146 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
145 break; 147 break;
148 case NFPROTO_ARP:
149 break;
146 } 150 }
147 par->entryinfo = entry; 151 par->entryinfo = entry;
148 par->target = target; 152 par->target = target;
@@ -300,7 +304,7 @@ static int nft_target_validate(const struct nft_ctx *ctx,
300} 304}
301 305
302static void nft_match_eval(const struct nft_expr *expr, 306static void nft_match_eval(const struct nft_expr *expr,
303 struct nft_data data[NFT_REG_MAX + 1], 307 struct nft_regs *regs,
304 const struct nft_pktinfo *pkt) 308 const struct nft_pktinfo *pkt)
305{ 309{
306 void *info = nft_expr_priv(expr); 310 void *info = nft_expr_priv(expr);
@@ -313,16 +317,16 @@ static void nft_match_eval(const struct nft_expr *expr,
313 ret = match->match(skb, (struct xt_action_param *)&pkt->xt); 317 ret = match->match(skb, (struct xt_action_param *)&pkt->xt);
314 318
315 if (pkt->xt.hotdrop) { 319 if (pkt->xt.hotdrop) {
316 data[NFT_REG_VERDICT].verdict = NF_DROP; 320 regs->verdict.code = NF_DROP;
317 return; 321 return;
318 } 322 }
319 323
320 switch(ret) { 324 switch (ret ? 1 : 0) {
321 case true: 325 case 1:
322 data[NFT_REG_VERDICT].verdict = NFT_CONTINUE; 326 regs->verdict.code = NFT_CONTINUE;
323 break; 327 break;
324 case false: 328 case 0:
325 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 329 regs->verdict.code = NFT_BREAK;
326 break; 330 break;
327 } 331 }
328} 332}
@@ -357,6 +361,8 @@ nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,
357 entry->ebt.ethproto = (__force __be16)proto; 361 entry->ebt.ethproto = (__force __be16)proto;
358 entry->ebt.invflags = inv ? EBT_IPROTO : 0; 362 entry->ebt.invflags = inv ? EBT_IPROTO : 0;
359 break; 363 break;
364 case NFPROTO_ARP:
365 break;
360 } 366 }
361 par->entryinfo = entry; 367 par->entryinfo = entry;
362 par->match = match; 368 par->match = match;
@@ -543,6 +549,9 @@ nfnl_compat_get(struct sock *nfnl, struct sk_buff *skb,
543 case NFPROTO_BRIDGE: 549 case NFPROTO_BRIDGE:
544 fmt = "ebt_%s"; 550 fmt = "ebt_%s";
545 break; 551 break;
552 case NFPROTO_ARP:
553 fmt = "arpt_%s";
554 break;
546 default: 555 default:
547 pr_err("nft_compat: unsupported protocol %d\n", 556 pr_err("nft_compat: unsupported protocol %d\n",
548 nfmsg->nfgen_family); 557 nfmsg->nfgen_family);
diff --git a/net/netfilter/nft_counter.c b/net/netfilter/nft_counter.c
index c89ee486ce54..17591239229f 100644
--- a/net/netfilter/nft_counter.c
+++ b/net/netfilter/nft_counter.c
@@ -24,7 +24,7 @@ struct nft_counter {
24}; 24};
25 25
26static void nft_counter_eval(const struct nft_expr *expr, 26static void nft_counter_eval(const struct nft_expr *expr,
27 struct nft_data data[NFT_REG_MAX + 1], 27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt) 28 const struct nft_pktinfo *pkt)
29{ 29{
30 struct nft_counter *priv = nft_expr_priv(expr); 30 struct nft_counter *priv = nft_expr_priv(expr);
@@ -92,6 +92,7 @@ static struct nft_expr_type nft_counter_type __read_mostly = {
92 .ops = &nft_counter_ops, 92 .ops = &nft_counter_ops,
93 .policy = nft_counter_policy, 93 .policy = nft_counter_policy,
94 .maxattr = NFTA_COUNTER_MAX, 94 .maxattr = NFTA_COUNTER_MAX,
95 .flags = NFT_EXPR_STATEFUL,
95 .owner = THIS_MODULE, 96 .owner = THIS_MODULE,
96}; 97};
97 98
diff --git a/net/netfilter/nft_ct.c b/net/netfilter/nft_ct.c
index cc5603016242..8cbca3432f90 100644
--- a/net/netfilter/nft_ct.c
+++ b/net/netfilter/nft_ct.c
@@ -31,11 +31,11 @@ struct nft_ct {
31}; 31};
32 32
33static void nft_ct_get_eval(const struct nft_expr *expr, 33static void nft_ct_get_eval(const struct nft_expr *expr,
34 struct nft_data data[NFT_REG_MAX + 1], 34 struct nft_regs *regs,
35 const struct nft_pktinfo *pkt) 35 const struct nft_pktinfo *pkt)
36{ 36{
37 const struct nft_ct *priv = nft_expr_priv(expr); 37 const struct nft_ct *priv = nft_expr_priv(expr);
38 struct nft_data *dest = &data[priv->dreg]; 38 u32 *dest = &regs->data[priv->dreg];
39 enum ip_conntrack_info ctinfo; 39 enum ip_conntrack_info ctinfo;
40 const struct nf_conn *ct; 40 const struct nf_conn *ct;
41 const struct nf_conn_help *help; 41 const struct nf_conn_help *help;
@@ -54,8 +54,10 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
54 state = NF_CT_STATE_UNTRACKED_BIT; 54 state = NF_CT_STATE_UNTRACKED_BIT;
55 else 55 else
56 state = NF_CT_STATE_BIT(ctinfo); 56 state = NF_CT_STATE_BIT(ctinfo);
57 dest->data[0] = state; 57 *dest = state;
58 return; 58 return;
59 default:
60 break;
59 } 61 }
60 62
61 if (ct == NULL) 63 if (ct == NULL)
@@ -63,26 +65,26 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
63 65
64 switch (priv->key) { 66 switch (priv->key) {
65 case NFT_CT_DIRECTION: 67 case NFT_CT_DIRECTION:
66 dest->data[0] = CTINFO2DIR(ctinfo); 68 *dest = CTINFO2DIR(ctinfo);
67 return; 69 return;
68 case NFT_CT_STATUS: 70 case NFT_CT_STATUS:
69 dest->data[0] = ct->status; 71 *dest = ct->status;
70 return; 72 return;
71#ifdef CONFIG_NF_CONNTRACK_MARK 73#ifdef CONFIG_NF_CONNTRACK_MARK
72 case NFT_CT_MARK: 74 case NFT_CT_MARK:
73 dest->data[0] = ct->mark; 75 *dest = ct->mark;
74 return; 76 return;
75#endif 77#endif
76#ifdef CONFIG_NF_CONNTRACK_SECMARK 78#ifdef CONFIG_NF_CONNTRACK_SECMARK
77 case NFT_CT_SECMARK: 79 case NFT_CT_SECMARK:
78 dest->data[0] = ct->secmark; 80 *dest = ct->secmark;
79 return; 81 return;
80#endif 82#endif
81 case NFT_CT_EXPIRATION: 83 case NFT_CT_EXPIRATION:
82 diff = (long)jiffies - (long)ct->timeout.expires; 84 diff = (long)jiffies - (long)ct->timeout.expires;
83 if (diff < 0) 85 if (diff < 0)
84 diff = 0; 86 diff = 0;
85 dest->data[0] = jiffies_to_msecs(diff); 87 *dest = jiffies_to_msecs(diff);
86 return; 88 return;
87 case NFT_CT_HELPER: 89 case NFT_CT_HELPER:
88 if (ct->master == NULL) 90 if (ct->master == NULL)
@@ -93,9 +95,7 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
93 helper = rcu_dereference(help->helper); 95 helper = rcu_dereference(help->helper);
94 if (helper == NULL) 96 if (helper == NULL)
95 goto err; 97 goto err;
96 if (strlen(helper->name) >= sizeof(dest->data)) 98 strncpy((char *)dest, helper->name, NF_CT_HELPER_NAME_LEN);
97 goto err;
98 strncpy((char *)dest->data, helper->name, sizeof(dest->data));
99 return; 99 return;
100#ifdef CONFIG_NF_CONNTRACK_LABELS 100#ifdef CONFIG_NF_CONNTRACK_LABELS
101 case NFT_CT_LABELS: { 101 case NFT_CT_LABELS: {
@@ -103,58 +103,60 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
103 unsigned int size; 103 unsigned int size;
104 104
105 if (!labels) { 105 if (!labels) {
106 memset(dest->data, 0, sizeof(dest->data)); 106 memset(dest, 0, NF_CT_LABELS_MAX_SIZE);
107 return; 107 return;
108 } 108 }
109 109
110 BUILD_BUG_ON(NF_CT_LABELS_MAX_SIZE > sizeof(dest->data));
111 size = labels->words * sizeof(long); 110 size = labels->words * sizeof(long);
112 111 memcpy(dest, labels->bits, size);
113 memcpy(dest->data, labels->bits, size); 112 if (size < NF_CT_LABELS_MAX_SIZE)
114 if (size < sizeof(dest->data)) 113 memset(((char *) dest) + size, 0,
115 memset(((char *) dest->data) + size, 0, 114 NF_CT_LABELS_MAX_SIZE - size);
116 sizeof(dest->data) - size);
117 return; 115 return;
118 } 116 }
119#endif 117#endif
118 default:
119 break;
120 } 120 }
121 121
122 tuple = &ct->tuplehash[priv->dir].tuple; 122 tuple = &ct->tuplehash[priv->dir].tuple;
123 switch (priv->key) { 123 switch (priv->key) {
124 case NFT_CT_L3PROTOCOL: 124 case NFT_CT_L3PROTOCOL:
125 dest->data[0] = nf_ct_l3num(ct); 125 *dest = nf_ct_l3num(ct);
126 return; 126 return;
127 case NFT_CT_SRC: 127 case NFT_CT_SRC:
128 memcpy(dest->data, tuple->src.u3.all, 128 memcpy(dest, tuple->src.u3.all,
129 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); 129 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
130 return; 130 return;
131 case NFT_CT_DST: 131 case NFT_CT_DST:
132 memcpy(dest->data, tuple->dst.u3.all, 132 memcpy(dest, tuple->dst.u3.all,
133 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16); 133 nf_ct_l3num(ct) == NFPROTO_IPV4 ? 4 : 16);
134 return; 134 return;
135 case NFT_CT_PROTOCOL: 135 case NFT_CT_PROTOCOL:
136 dest->data[0] = nf_ct_protonum(ct); 136 *dest = nf_ct_protonum(ct);
137 return; 137 return;
138 case NFT_CT_PROTO_SRC: 138 case NFT_CT_PROTO_SRC:
139 dest->data[0] = (__force __u16)tuple->src.u.all; 139 *dest = (__force __u16)tuple->src.u.all;
140 return; 140 return;
141 case NFT_CT_PROTO_DST: 141 case NFT_CT_PROTO_DST:
142 dest->data[0] = (__force __u16)tuple->dst.u.all; 142 *dest = (__force __u16)tuple->dst.u.all;
143 return; 143 return;
144 default:
145 break;
144 } 146 }
145 return; 147 return;
146err: 148err:
147 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 149 regs->verdict.code = NFT_BREAK;
148} 150}
149 151
150static void nft_ct_set_eval(const struct nft_expr *expr, 152static void nft_ct_set_eval(const struct nft_expr *expr,
151 struct nft_data data[NFT_REG_MAX + 1], 153 struct nft_regs *regs,
152 const struct nft_pktinfo *pkt) 154 const struct nft_pktinfo *pkt)
153{ 155{
154 const struct nft_ct *priv = nft_expr_priv(expr); 156 const struct nft_ct *priv = nft_expr_priv(expr);
155 struct sk_buff *skb = pkt->skb; 157 struct sk_buff *skb = pkt->skb;
156#ifdef CONFIG_NF_CONNTRACK_MARK 158#ifdef CONFIG_NF_CONNTRACK_MARK
157 u32 value = data[priv->sreg].data[0]; 159 u32 value = regs->data[priv->sreg];
158#endif 160#endif
159 enum ip_conntrack_info ctinfo; 161 enum ip_conntrack_info ctinfo;
160 struct nf_conn *ct; 162 struct nf_conn *ct;
@@ -172,6 +174,8 @@ static void nft_ct_set_eval(const struct nft_expr *expr,
172 } 174 }
173 break; 175 break;
174#endif 176#endif
177 default:
178 break;
175 } 179 }
176} 180}
177 181
@@ -220,12 +224,17 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,
220 const struct nlattr * const tb[]) 224 const struct nlattr * const tb[])
221{ 225{
222 struct nft_ct *priv = nft_expr_priv(expr); 226 struct nft_ct *priv = nft_expr_priv(expr);
227 unsigned int len;
223 int err; 228 int err;
224 229
225 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); 230 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
226 switch (priv->key) { 231 switch (priv->key) {
227 case NFT_CT_STATE:
228 case NFT_CT_DIRECTION: 232 case NFT_CT_DIRECTION:
233 if (tb[NFTA_CT_DIRECTION] != NULL)
234 return -EINVAL;
235 len = sizeof(u8);
236 break;
237 case NFT_CT_STATE:
229 case NFT_CT_STATUS: 238 case NFT_CT_STATUS:
230#ifdef CONFIG_NF_CONNTRACK_MARK 239#ifdef CONFIG_NF_CONNTRACK_MARK
231 case NFT_CT_MARK: 240 case NFT_CT_MARK:
@@ -233,22 +242,54 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,
233#ifdef CONFIG_NF_CONNTRACK_SECMARK 242#ifdef CONFIG_NF_CONNTRACK_SECMARK
234 case NFT_CT_SECMARK: 243 case NFT_CT_SECMARK:
235#endif 244#endif
245 case NFT_CT_EXPIRATION:
246 if (tb[NFTA_CT_DIRECTION] != NULL)
247 return -EINVAL;
248 len = sizeof(u32);
249 break;
236#ifdef CONFIG_NF_CONNTRACK_LABELS 250#ifdef CONFIG_NF_CONNTRACK_LABELS
237 case NFT_CT_LABELS: 251 case NFT_CT_LABELS:
252 if (tb[NFTA_CT_DIRECTION] != NULL)
253 return -EINVAL;
254 len = NF_CT_LABELS_MAX_SIZE;
255 break;
238#endif 256#endif
239 case NFT_CT_EXPIRATION:
240 case NFT_CT_HELPER: 257 case NFT_CT_HELPER:
241 if (tb[NFTA_CT_DIRECTION] != NULL) 258 if (tb[NFTA_CT_DIRECTION] != NULL)
242 return -EINVAL; 259 return -EINVAL;
260 len = NF_CT_HELPER_NAME_LEN;
243 break; 261 break;
262
244 case NFT_CT_L3PROTOCOL: 263 case NFT_CT_L3PROTOCOL:
245 case NFT_CT_PROTOCOL: 264 case NFT_CT_PROTOCOL:
265 if (tb[NFTA_CT_DIRECTION] == NULL)
266 return -EINVAL;
267 len = sizeof(u8);
268 break;
246 case NFT_CT_SRC: 269 case NFT_CT_SRC:
247 case NFT_CT_DST: 270 case NFT_CT_DST:
271 if (tb[NFTA_CT_DIRECTION] == NULL)
272 return -EINVAL;
273
274 switch (ctx->afi->family) {
275 case NFPROTO_IPV4:
276 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
277 src.u3.ip);
278 break;
279 case NFPROTO_IPV6:
280 case NFPROTO_INET:
281 len = FIELD_SIZEOF(struct nf_conntrack_tuple,
282 src.u3.ip6);
283 break;
284 default:
285 return -EAFNOSUPPORT;
286 }
287 break;
248 case NFT_CT_PROTO_SRC: 288 case NFT_CT_PROTO_SRC:
249 case NFT_CT_PROTO_DST: 289 case NFT_CT_PROTO_DST:
250 if (tb[NFTA_CT_DIRECTION] == NULL) 290 if (tb[NFTA_CT_DIRECTION] == NULL)
251 return -EINVAL; 291 return -EINVAL;
292 len = FIELD_SIZEOF(struct nf_conntrack_tuple, src.u.all);
252 break; 293 break;
253 default: 294 default:
254 return -EOPNOTSUPP; 295 return -EOPNOTSUPP;
@@ -265,12 +306,9 @@ static int nft_ct_get_init(const struct nft_ctx *ctx,
265 } 306 }
266 } 307 }
267 308
268 priv->dreg = ntohl(nla_get_be32(tb[NFTA_CT_DREG])); 309 priv->dreg = nft_parse_register(tb[NFTA_CT_DREG]);
269 err = nft_validate_output_register(priv->dreg); 310 err = nft_validate_register_store(ctx, priv->dreg, NULL,
270 if (err < 0) 311 NFT_DATA_VALUE, len);
271 return err;
272
273 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
274 if (err < 0) 312 if (err < 0)
275 return err; 313 return err;
276 314
@@ -286,20 +324,22 @@ static int nft_ct_set_init(const struct nft_ctx *ctx,
286 const struct nlattr * const tb[]) 324 const struct nlattr * const tb[])
287{ 325{
288 struct nft_ct *priv = nft_expr_priv(expr); 326 struct nft_ct *priv = nft_expr_priv(expr);
327 unsigned int len;
289 int err; 328 int err;
290 329
291 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY])); 330 priv->key = ntohl(nla_get_be32(tb[NFTA_CT_KEY]));
292 switch (priv->key) { 331 switch (priv->key) {
293#ifdef CONFIG_NF_CONNTRACK_MARK 332#ifdef CONFIG_NF_CONNTRACK_MARK
294 case NFT_CT_MARK: 333 case NFT_CT_MARK:
334 len = FIELD_SIZEOF(struct nf_conn, mark);
295 break; 335 break;
296#endif 336#endif
297 default: 337 default:
298 return -EOPNOTSUPP; 338 return -EOPNOTSUPP;
299 } 339 }
300 340
301 priv->sreg = ntohl(nla_get_be32(tb[NFTA_CT_SREG])); 341 priv->sreg = nft_parse_register(tb[NFTA_CT_SREG]);
302 err = nft_validate_input_register(priv->sreg); 342 err = nft_validate_register_load(priv->sreg, len);
303 if (err < 0) 343 if (err < 0)
304 return err; 344 return err;
305 345
@@ -320,7 +360,7 @@ static int nft_ct_get_dump(struct sk_buff *skb, const struct nft_expr *expr)
320{ 360{
321 const struct nft_ct *priv = nft_expr_priv(expr); 361 const struct nft_ct *priv = nft_expr_priv(expr);
322 362
323 if (nla_put_be32(skb, NFTA_CT_DREG, htonl(priv->dreg))) 363 if (nft_dump_register(skb, NFTA_CT_DREG, priv->dreg))
324 goto nla_put_failure; 364 goto nla_put_failure;
325 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) 365 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
326 goto nla_put_failure; 366 goto nla_put_failure;
@@ -347,7 +387,7 @@ static int nft_ct_set_dump(struct sk_buff *skb, const struct nft_expr *expr)
347{ 387{
348 const struct nft_ct *priv = nft_expr_priv(expr); 388 const struct nft_ct *priv = nft_expr_priv(expr);
349 389
350 if (nla_put_be32(skb, NFTA_CT_SREG, htonl(priv->sreg))) 390 if (nft_dump_register(skb, NFTA_CT_SREG, priv->sreg))
351 goto nla_put_failure; 391 goto nla_put_failure;
352 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key))) 392 if (nla_put_be32(skb, NFTA_CT_KEY, htonl(priv->key)))
353 goto nla_put_failure; 393 goto nla_put_failure;
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
new file mode 100644
index 000000000000..513a8ef60a59
--- /dev/null
+++ b/net/netfilter/nft_dynset.c
@@ -0,0 +1,265 @@
1/*
2 * Copyright (c) 2015 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 */
9
10#include <linux/kernel.h>
11#include <linux/module.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#include <net/netfilter/nf_tables_core.h>
18
19struct nft_dynset {
20 struct nft_set *set;
21 struct nft_set_ext_tmpl tmpl;
22 enum nft_dynset_ops op:8;
23 enum nft_registers sreg_key:8;
24 enum nft_registers sreg_data:8;
25 u64 timeout;
26 struct nft_expr *expr;
27 struct nft_set_binding binding;
28};
29
30static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
31 struct nft_regs *regs)
32{
33 const struct nft_dynset *priv = nft_expr_priv(expr);
34 struct nft_set_ext *ext;
35 u64 timeout;
36 void *elem;
37
38 if (set->size && !atomic_add_unless(&set->nelems, 1, set->size))
39 return NULL;
40
41 timeout = priv->timeout ? : set->timeout;
42 elem = nft_set_elem_init(set, &priv->tmpl,
43 &regs->data[priv->sreg_key],
44 &regs->data[priv->sreg_data],
45 timeout, GFP_ATOMIC);
46 if (elem == NULL) {
47 if (set->size)
48 atomic_dec(&set->nelems);
49 return NULL;
50 }
51
52 ext = nft_set_elem_ext(set, elem);
53 if (priv->expr != NULL)
54 nft_expr_clone(nft_set_ext_expr(ext), priv->expr);
55
56 return elem;
57}
58
59static void nft_dynset_eval(const struct nft_expr *expr,
60 struct nft_regs *regs,
61 const struct nft_pktinfo *pkt)
62{
63 const struct nft_dynset *priv = nft_expr_priv(expr);
64 struct nft_set *set = priv->set;
65 const struct nft_set_ext *ext;
66 const struct nft_expr *sexpr;
67 u64 timeout;
68
69 if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
70 expr, regs, &ext)) {
71 sexpr = NULL;
72 if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
73 sexpr = nft_set_ext_expr(ext);
74
75 if (priv->op == NFT_DYNSET_OP_UPDATE &&
76 nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
77 timeout = priv->timeout ? : set->timeout;
78 *nft_set_ext_expiration(ext) = jiffies + timeout;
79 } else if (sexpr == NULL)
80 goto out;
81
82 if (sexpr != NULL)
83 sexpr->ops->eval(sexpr, regs, pkt);
84 return;
85 }
86out:
87 regs->verdict.code = NFT_BREAK;
88}
89
90static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
91 [NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING },
92 [NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
93 [NFTA_DYNSET_OP] = { .type = NLA_U32 },
94 [NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
95 [NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
96 [NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
97 [NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
98};
99
100static int nft_dynset_init(const struct nft_ctx *ctx,
101 const struct nft_expr *expr,
102 const struct nlattr * const tb[])
103{
104 struct nft_dynset *priv = nft_expr_priv(expr);
105 struct nft_set *set;
106 u64 timeout;
107 int err;
108
109 if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
110 tb[NFTA_DYNSET_OP] == NULL ||
111 tb[NFTA_DYNSET_SREG_KEY] == NULL)
112 return -EINVAL;
113
114 set = nf_tables_set_lookup(ctx->table, tb[NFTA_DYNSET_SET_NAME]);
115 if (IS_ERR(set)) {
116 if (tb[NFTA_DYNSET_SET_ID])
117 set = nf_tables_set_lookup_byid(ctx->net,
118 tb[NFTA_DYNSET_SET_ID]);
119 if (IS_ERR(set))
120 return PTR_ERR(set);
121 }
122
123 if (set->flags & NFT_SET_CONSTANT)
124 return -EBUSY;
125
126 priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
127 switch (priv->op) {
128 case NFT_DYNSET_OP_ADD:
129 break;
130 case NFT_DYNSET_OP_UPDATE:
131 if (!(set->flags & NFT_SET_TIMEOUT))
132 return -EOPNOTSUPP;
133 break;
134 default:
135 return -EOPNOTSUPP;
136 }
137
138 timeout = 0;
139 if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
140 if (!(set->flags & NFT_SET_TIMEOUT))
141 return -EINVAL;
142 timeout = be64_to_cpu(nla_get_be64(tb[NFTA_DYNSET_TIMEOUT]));
143 }
144
145 priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
146 err = nft_validate_register_load(priv->sreg_key, set->klen);;
147 if (err < 0)
148 return err;
149
150 if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
151 if (!(set->flags & NFT_SET_MAP))
152 return -EINVAL;
153 if (set->dtype == NFT_DATA_VERDICT)
154 return -EOPNOTSUPP;
155
156 priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
157 err = nft_validate_register_load(priv->sreg_data, set->dlen);
158 if (err < 0)
159 return err;
160 } else if (set->flags & NFT_SET_MAP)
161 return -EINVAL;
162
163 if (tb[NFTA_DYNSET_EXPR] != NULL) {
164 if (!(set->flags & NFT_SET_EVAL))
165 return -EINVAL;
166 if (!(set->flags & NFT_SET_ANONYMOUS))
167 return -EOPNOTSUPP;
168
169 priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
170 if (IS_ERR(priv->expr))
171 return PTR_ERR(priv->expr);
172
173 err = -EOPNOTSUPP;
174 if (!(priv->expr->ops->type->flags & NFT_EXPR_STATEFUL))
175 goto err1;
176 } else if (set->flags & NFT_SET_EVAL)
177 return -EINVAL;
178
179 nft_set_ext_prepare(&priv->tmpl);
180 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
181 if (set->flags & NFT_SET_MAP)
182 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
183 if (priv->expr != NULL)
184 nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
185 priv->expr->ops->size);
186 if (set->flags & NFT_SET_TIMEOUT) {
187 if (timeout || set->timeout)
188 nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
189 }
190
191 priv->timeout = timeout;
192
193 err = nf_tables_bind_set(ctx, set, &priv->binding);
194 if (err < 0)
195 goto err1;
196
197 priv->set = set;
198 return 0;
199
200err1:
201 if (priv->expr != NULL)
202 nft_expr_destroy(ctx, priv->expr);
203 return err;
204}
205
206static void nft_dynset_destroy(const struct nft_ctx *ctx,
207 const struct nft_expr *expr)
208{
209 struct nft_dynset *priv = nft_expr_priv(expr);
210
211 nf_tables_unbind_set(ctx, priv->set, &priv->binding);
212 if (priv->expr != NULL)
213 nft_expr_destroy(ctx, priv->expr);
214}
215
216static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
217{
218 const struct nft_dynset *priv = nft_expr_priv(expr);
219
220 if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
221 goto nla_put_failure;
222 if (priv->set->flags & NFT_SET_MAP &&
223 nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
224 goto nla_put_failure;
225 if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
226 goto nla_put_failure;
227 if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
228 goto nla_put_failure;
229 if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT, cpu_to_be64(priv->timeout)))
230 goto nla_put_failure;
231 if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
232 goto nla_put_failure;
233 return 0;
234
235nla_put_failure:
236 return -1;
237}
238
239static struct nft_expr_type nft_dynset_type;
240static const struct nft_expr_ops nft_dynset_ops = {
241 .type = &nft_dynset_type,
242 .size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
243 .eval = nft_dynset_eval,
244 .init = nft_dynset_init,
245 .destroy = nft_dynset_destroy,
246 .dump = nft_dynset_dump,
247};
248
249static struct nft_expr_type nft_dynset_type __read_mostly = {
250 .name = "dynset",
251 .ops = &nft_dynset_ops,
252 .policy = nft_dynset_policy,
253 .maxattr = NFTA_DYNSET_MAX,
254 .owner = THIS_MODULE,
255};
256
257int __init nft_dynset_module_init(void)
258{
259 return nft_register_expr(&nft_dynset_type);
260}
261
262void nft_dynset_module_exit(void)
263{
264 nft_unregister_expr(&nft_dynset_type);
265}
diff --git a/net/netfilter/nft_expr_template.c b/net/netfilter/nft_expr_template.c
deleted file mode 100644
index b6eed4d5a096..000000000000
--- a/net/netfilter/nft_expr_template.c
+++ /dev/null
@@ -1,94 +0,0 @@
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
index 55c939f5371f..ba7aed13e174 100644
--- a/net/netfilter/nft_exthdr.c
+++ b/net/netfilter/nft_exthdr.c
@@ -26,11 +26,11 @@ struct nft_exthdr {
26}; 26};
27 27
28static void nft_exthdr_eval(const struct nft_expr *expr, 28static void nft_exthdr_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1], 29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt) 30 const struct nft_pktinfo *pkt)
31{ 31{
32 struct nft_exthdr *priv = nft_expr_priv(expr); 32 struct nft_exthdr *priv = nft_expr_priv(expr);
33 struct nft_data *dest = &data[priv->dreg]; 33 u32 *dest = &regs->data[priv->dreg];
34 unsigned int offset = 0; 34 unsigned int offset = 0;
35 int err; 35 int err;
36 36
@@ -39,11 +39,12 @@ static void nft_exthdr_eval(const struct nft_expr *expr,
39 goto err; 39 goto err;
40 offset += priv->offset; 40 offset += priv->offset;
41 41
42 if (skb_copy_bits(pkt->skb, offset, dest->data, priv->len) < 0) 42 dest[priv->len / NFT_REG32_SIZE] = 0;
43 if (skb_copy_bits(pkt->skb, offset, dest, priv->len) < 0)
43 goto err; 44 goto err;
44 return; 45 return;
45err: 46err:
46 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 47 regs->verdict.code = NFT_BREAK;
47} 48}
48 49
49static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = { 50static const struct nla_policy nft_exthdr_policy[NFTA_EXTHDR_MAX + 1] = {
@@ -58,7 +59,6 @@ static int nft_exthdr_init(const struct nft_ctx *ctx,
58 const struct nlattr * const tb[]) 59 const struct nlattr * const tb[])
59{ 60{
60 struct nft_exthdr *priv = nft_expr_priv(expr); 61 struct nft_exthdr *priv = nft_expr_priv(expr);
61 int err;
62 62
63 if (tb[NFTA_EXTHDR_DREG] == NULL || 63 if (tb[NFTA_EXTHDR_DREG] == NULL ||
64 tb[NFTA_EXTHDR_TYPE] == NULL || 64 tb[NFTA_EXTHDR_TYPE] == NULL ||
@@ -69,22 +69,17 @@ static int nft_exthdr_init(const struct nft_ctx *ctx,
69 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]); 69 priv->type = nla_get_u8(tb[NFTA_EXTHDR_TYPE]);
70 priv->offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET])); 70 priv->offset = ntohl(nla_get_be32(tb[NFTA_EXTHDR_OFFSET]));
71 priv->len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN])); 71 priv->len = ntohl(nla_get_be32(tb[NFTA_EXTHDR_LEN]));
72 if (priv->len == 0 || 72 priv->dreg = nft_parse_register(tb[NFTA_EXTHDR_DREG]);
73 priv->len > FIELD_SIZEOF(struct nft_data, data))
74 return -EINVAL;
75 73
76 priv->dreg = ntohl(nla_get_be32(tb[NFTA_EXTHDR_DREG])); 74 return nft_validate_register_store(ctx, priv->dreg, NULL,
77 err = nft_validate_output_register(priv->dreg); 75 NFT_DATA_VALUE, priv->len);
78 if (err < 0)
79 return err;
80 return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
81} 76}
82 77
83static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr) 78static int nft_exthdr_dump(struct sk_buff *skb, const struct nft_expr *expr)
84{ 79{
85 const struct nft_exthdr *priv = nft_expr_priv(expr); 80 const struct nft_exthdr *priv = nft_expr_priv(expr);
86 81
87 if (nla_put_be32(skb, NFTA_EXTHDR_DREG, htonl(priv->dreg))) 82 if (nft_dump_register(skb, NFTA_EXTHDR_DREG, priv->dreg))
88 goto nla_put_failure; 83 goto nla_put_failure;
89 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type)) 84 if (nla_put_u8(skb, NFTA_EXTHDR_TYPE, priv->type))
90 goto nla_put_failure; 85 goto nla_put_failure;
diff --git a/net/netfilter/nft_hash.c b/net/netfilter/nft_hash.c
index 37c15e674884..3f9d45d3d9b7 100644
--- a/net/netfilter/nft_hash.c
+++ b/net/netfilter/nft_hash.c
@@ -15,6 +15,7 @@
15#include <linux/log2.h> 15#include <linux/log2.h>
16#include <linux/jhash.h> 16#include <linux/jhash.h>
17#include <linux/netlink.h> 17#include <linux/netlink.h>
18#include <linux/workqueue.h>
18#include <linux/rhashtable.h> 19#include <linux/rhashtable.h>
19#include <linux/netfilter.h> 20#include <linux/netfilter.h>
20#include <linux/netfilter/nf_tables.h> 21#include <linux/netfilter/nf_tables.h>
@@ -23,119 +24,175 @@
23/* We target a hash table size of 4, element hint is 75% of final size */ 24/* We target a hash table size of 4, element hint is 75% of final size */
24#define NFT_HASH_ELEMENT_HINT 3 25#define NFT_HASH_ELEMENT_HINT 3
25 26
27struct nft_hash {
28 struct rhashtable ht;
29 struct delayed_work gc_work;
30};
31
26struct nft_hash_elem { 32struct nft_hash_elem {
27 struct rhash_head node; 33 struct rhash_head node;
28 struct nft_data key; 34 struct nft_set_ext ext;
29 struct nft_data data[]; 35};
36
37struct nft_hash_cmp_arg {
38 const struct nft_set *set;
39 const u32 *key;
40 u8 genmask;
30}; 41};
31 42
32static bool nft_hash_lookup(const struct nft_set *set, 43static const struct rhashtable_params nft_hash_params;
33 const struct nft_data *key, 44
34 struct nft_data *data) 45static inline u32 nft_hash_key(const void *data, u32 len, u32 seed)
46{
47 const struct nft_hash_cmp_arg *arg = data;
48
49 return jhash(arg->key, len, seed);
50}
51
52static inline u32 nft_hash_obj(const void *data, u32 len, u32 seed)
35{ 53{
36 struct rhashtable *priv = nft_set_priv(set); 54 const struct nft_hash_elem *he = data;
55
56 return jhash(nft_set_ext_key(&he->ext), len, seed);
57}
58
59static inline int nft_hash_cmp(struct rhashtable_compare_arg *arg,
60 const void *ptr)
61{
62 const struct nft_hash_cmp_arg *x = arg->key;
63 const struct nft_hash_elem *he = ptr;
64
65 if (memcmp(nft_set_ext_key(&he->ext), x->key, x->set->klen))
66 return 1;
67 if (nft_set_elem_expired(&he->ext))
68 return 1;
69 if (!nft_set_elem_active(&he->ext, x->genmask))
70 return 1;
71 return 0;
72}
73
74static bool nft_hash_lookup(const struct nft_set *set, const u32 *key,
75 const struct nft_set_ext **ext)
76{
77 struct nft_hash *priv = nft_set_priv(set);
37 const struct nft_hash_elem *he; 78 const struct nft_hash_elem *he;
79 struct nft_hash_cmp_arg arg = {
80 .genmask = nft_genmask_cur(read_pnet(&set->pnet)),
81 .set = set,
82 .key = key,
83 };
38 84
39 he = rhashtable_lookup(priv, key); 85 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
40 if (he && set->flags & NFT_SET_MAP) 86 if (he != NULL)
41 nft_data_copy(data, he->data); 87 *ext = &he->ext;
42 88
43 return !!he; 89 return !!he;
44} 90}
45 91
46static int nft_hash_insert(const struct nft_set *set, 92static bool nft_hash_update(struct nft_set *set, const u32 *key,
47 const struct nft_set_elem *elem) 93 void *(*new)(struct nft_set *,
94 const struct nft_expr *,
95 struct nft_regs *regs),
96 const struct nft_expr *expr,
97 struct nft_regs *regs,
98 const struct nft_set_ext **ext)
48{ 99{
49 struct rhashtable *priv = nft_set_priv(set); 100 struct nft_hash *priv = nft_set_priv(set);
50 struct nft_hash_elem *he; 101 struct nft_hash_elem *he;
51 unsigned int size; 102 struct nft_hash_cmp_arg arg = {
52 103 .genmask = NFT_GENMASK_ANY,
53 if (elem->flags != 0) 104 .set = set,
54 return -EINVAL; 105 .key = key,
106 };
55 107
56 size = sizeof(*he); 108 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
57 if (set->flags & NFT_SET_MAP) 109 if (he != NULL)
58 size += sizeof(he->data[0]); 110 goto out;
59 111
60 he = kzalloc(size, GFP_KERNEL); 112 he = new(set, expr, regs);
61 if (he == NULL) 113 if (he == NULL)
62 return -ENOMEM; 114 goto err1;
63 115 if (rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
64 nft_data_copy(&he->key, &elem->key); 116 nft_hash_params))
65 if (set->flags & NFT_SET_MAP) 117 goto err2;
66 nft_data_copy(he->data, &elem->data); 118out:
67 119 *ext = &he->ext;
68 rhashtable_insert(priv, &he->node); 120 return true;
69 121
70 return 0; 122err2:
123 nft_set_elem_destroy(set, he);
124err1:
125 return false;
71} 126}
72 127
73static void nft_hash_elem_destroy(const struct nft_set *set, 128static int nft_hash_insert(const struct nft_set *set,
74 struct nft_hash_elem *he) 129 const struct nft_set_elem *elem)
75{ 130{
76 nft_data_uninit(&he->key, NFT_DATA_VALUE); 131 struct nft_hash *priv = nft_set_priv(set);
77 if (set->flags & NFT_SET_MAP) 132 struct nft_hash_elem *he = elem->priv;
78 nft_data_uninit(he->data, set->dtype); 133 struct nft_hash_cmp_arg arg = {
79 kfree(he); 134 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
135 .set = set,
136 .key = elem->key.val.data,
137 };
138
139 return rhashtable_lookup_insert_key(&priv->ht, &arg, &he->node,
140 nft_hash_params);
80} 141}
81 142
82static void nft_hash_remove(const struct nft_set *set, 143static void nft_hash_activate(const struct nft_set *set,
83 const struct nft_set_elem *elem) 144 const struct nft_set_elem *elem)
84{ 145{
85 struct rhashtable *priv = nft_set_priv(set); 146 struct nft_hash_elem *he = elem->priv;
86 147
87 rhashtable_remove(priv, elem->cookie); 148 nft_set_elem_change_active(set, &he->ext);
88 synchronize_rcu(); 149 nft_set_elem_clear_busy(&he->ext);
89 kfree(elem->cookie);
90} 150}
91 151
92struct nft_compare_arg { 152static void *nft_hash_deactivate(const struct nft_set *set,
93 const struct nft_set *set; 153 const struct nft_set_elem *elem)
94 struct nft_set_elem *elem;
95};
96
97static bool nft_hash_compare(void *ptr, void *arg)
98{ 154{
99 struct nft_hash_elem *he = ptr; 155 struct nft_hash *priv = nft_set_priv(set);
100 struct nft_compare_arg *x = arg; 156 struct nft_hash_elem *he;
101 157 struct nft_hash_cmp_arg arg = {
102 if (!nft_data_cmp(&he->key, &x->elem->key, x->set->klen)) { 158 .genmask = nft_genmask_next(read_pnet(&set->pnet)),
103 x->elem->cookie = he; 159 .set = set,
104 x->elem->flags = 0; 160 .key = elem->key.val.data,
105 if (x->set->flags & NFT_SET_MAP) 161 };
106 nft_data_copy(&x->elem->data, he->data);
107 162
108 return true; 163 rcu_read_lock();
164 he = rhashtable_lookup_fast(&priv->ht, &arg, nft_hash_params);
165 if (he != NULL) {
166 if (!nft_set_elem_mark_busy(&he->ext))
167 nft_set_elem_change_active(set, &he->ext);
168 else
169 he = NULL;
109 } 170 }
171 rcu_read_unlock();
110 172
111 return false; 173 return he;
112} 174}
113 175
114static int nft_hash_get(const struct nft_set *set, struct nft_set_elem *elem) 176static void nft_hash_remove(const struct nft_set *set,
177 const struct nft_set_elem *elem)
115{ 178{
116 struct rhashtable *priv = nft_set_priv(set); 179 struct nft_hash *priv = nft_set_priv(set);
117 struct nft_compare_arg arg = { 180 struct nft_hash_elem *he = elem->priv;
118 .set = set,
119 .elem = elem,
120 };
121 181
122 if (rhashtable_lookup_compare(priv, &elem->key, 182 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
123 &nft_hash_compare, &arg))
124 return 0;
125
126 return -ENOENT;
127} 183}
128 184
129static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set, 185static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
130 struct nft_set_iter *iter) 186 struct nft_set_iter *iter)
131{ 187{
132 struct rhashtable *priv = nft_set_priv(set); 188 struct nft_hash *priv = nft_set_priv(set);
133 const struct nft_hash_elem *he; 189 struct nft_hash_elem *he;
134 struct rhashtable_iter hti; 190 struct rhashtable_iter hti;
135 struct nft_set_elem elem; 191 struct nft_set_elem elem;
192 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
136 int err; 193 int err;
137 194
138 err = rhashtable_walk_init(priv, &hti); 195 err = rhashtable_walk_init(&priv->ht, &hti);
139 iter->err = err; 196 iter->err = err;
140 if (err) 197 if (err)
141 return; 198 return;
@@ -159,11 +216,12 @@ static void nft_hash_walk(const struct nft_ctx *ctx, const struct nft_set *set,
159 216
160 if (iter->count < iter->skip) 217 if (iter->count < iter->skip)
161 goto cont; 218 goto cont;
219 if (nft_set_elem_expired(&he->ext))
220 goto cont;
221 if (!nft_set_elem_active(&he->ext, genmask))
222 goto cont;
162 223
163 memcpy(&elem.key, &he->key, sizeof(elem.key)); 224 elem.priv = he;
164 if (set->flags & NFT_SET_MAP)
165 memcpy(&elem.data, he->data, sizeof(elem.data));
166 elem.flags = 0;
167 225
168 iter->err = iter->fn(ctx, set, iter, &elem); 226 iter->err = iter->fn(ctx, set, iter, &elem);
169 if (iter->err < 0) 227 if (iter->err < 0)
@@ -178,47 +236,102 @@ out:
178 rhashtable_walk_exit(&hti); 236 rhashtable_walk_exit(&hti);
179} 237}
180 238
239static void nft_hash_gc(struct work_struct *work)
240{
241 struct nft_set *set;
242 struct nft_hash_elem *he;
243 struct nft_hash *priv;
244 struct nft_set_gc_batch *gcb = NULL;
245 struct rhashtable_iter hti;
246 int err;
247
248 priv = container_of(work, struct nft_hash, gc_work.work);
249 set = nft_set_container_of(priv);
250
251 err = rhashtable_walk_init(&priv->ht, &hti);
252 if (err)
253 goto schedule;
254
255 err = rhashtable_walk_start(&hti);
256 if (err && err != -EAGAIN)
257 goto out;
258
259 while ((he = rhashtable_walk_next(&hti))) {
260 if (IS_ERR(he)) {
261 if (PTR_ERR(he) != -EAGAIN)
262 goto out;
263 continue;
264 }
265
266 if (!nft_set_elem_expired(&he->ext))
267 continue;
268 if (nft_set_elem_mark_busy(&he->ext))
269 continue;
270
271 gcb = nft_set_gc_batch_check(set, gcb, GFP_ATOMIC);
272 if (gcb == NULL)
273 goto out;
274 rhashtable_remove_fast(&priv->ht, &he->node, nft_hash_params);
275 atomic_dec(&set->nelems);
276 nft_set_gc_batch_add(gcb, he);
277 }
278out:
279 rhashtable_walk_stop(&hti);
280 rhashtable_walk_exit(&hti);
281
282 nft_set_gc_batch_complete(gcb);
283schedule:
284 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
285 nft_set_gc_interval(set));
286}
287
181static unsigned int nft_hash_privsize(const struct nlattr * const nla[]) 288static unsigned int nft_hash_privsize(const struct nlattr * const nla[])
182{ 289{
183 return sizeof(struct rhashtable); 290 return sizeof(struct nft_hash);
184} 291}
185 292
293static const struct rhashtable_params nft_hash_params = {
294 .head_offset = offsetof(struct nft_hash_elem, node),
295 .hashfn = nft_hash_key,
296 .obj_hashfn = nft_hash_obj,
297 .obj_cmpfn = nft_hash_cmp,
298 .automatic_shrinking = true,
299};
300
186static int nft_hash_init(const struct nft_set *set, 301static int nft_hash_init(const struct nft_set *set,
187 const struct nft_set_desc *desc, 302 const struct nft_set_desc *desc,
188 const struct nlattr * const tb[]) 303 const struct nlattr * const tb[])
189{ 304{
190 struct rhashtable *priv = nft_set_priv(set); 305 struct nft_hash *priv = nft_set_priv(set);
191 struct rhashtable_params params = { 306 struct rhashtable_params params = nft_hash_params;
192 .nelem_hint = desc->size ? : NFT_HASH_ELEMENT_HINT, 307 int err;
193 .head_offset = offsetof(struct nft_hash_elem, node),
194 .key_offset = offsetof(struct nft_hash_elem, key),
195 .key_len = set->klen,
196 .hashfn = jhash,
197 };
198 308
199 return rhashtable_init(priv, &params); 309 params.nelem_hint = desc->size ?: NFT_HASH_ELEMENT_HINT;
310 params.key_len = set->klen;
311
312 err = rhashtable_init(&priv->ht, &params);
313 if (err < 0)
314 return err;
315
316 INIT_DEFERRABLE_WORK(&priv->gc_work, nft_hash_gc);
317 if (set->flags & NFT_SET_TIMEOUT)
318 queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
319 nft_set_gc_interval(set));
320 return 0;
200} 321}
201 322
202static void nft_hash_destroy(const struct nft_set *set) 323static void nft_hash_elem_destroy(void *ptr, void *arg)
203{ 324{
204 struct rhashtable *priv = nft_set_priv(set); 325 nft_set_elem_destroy((const struct nft_set *)arg, ptr);
205 const struct bucket_table *tbl; 326}
206 struct nft_hash_elem *he;
207 struct rhash_head *pos, *next;
208 unsigned int i;
209
210 /* Stop an eventual async resizing */
211 priv->being_destroyed = true;
212 mutex_lock(&priv->mutex);
213 327
214 tbl = rht_dereference(priv->tbl, priv); 328static void nft_hash_destroy(const struct nft_set *set)
215 for (i = 0; i < tbl->size; i++) { 329{
216 rht_for_each_entry_safe(he, pos, next, tbl, i, node) 330 struct nft_hash *priv = nft_set_priv(set);
217 nft_hash_elem_destroy(set, he);
218 }
219 mutex_unlock(&priv->mutex);
220 331
221 rhashtable_destroy(priv); 332 cancel_delayed_work_sync(&priv->gc_work);
333 rhashtable_free_and_destroy(&priv->ht, nft_hash_elem_destroy,
334 (void *)set);
222} 335}
223 336
224static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features, 337static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
@@ -227,11 +340,8 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
227 unsigned int esize; 340 unsigned int esize;
228 341
229 esize = sizeof(struct nft_hash_elem); 342 esize = sizeof(struct nft_hash_elem);
230 if (features & NFT_SET_MAP)
231 esize += FIELD_SIZEOF(struct nft_hash_elem, data[0]);
232
233 if (desc->size) { 343 if (desc->size) {
234 est->size = sizeof(struct rhashtable) + 344 est->size = sizeof(struct nft_hash) +
235 roundup_pow_of_two(desc->size * 4 / 3) * 345 roundup_pow_of_two(desc->size * 4 / 3) *
236 sizeof(struct nft_hash_elem *) + 346 sizeof(struct nft_hash_elem *) +
237 desc->size * esize; 347 desc->size * esize;
@@ -251,15 +361,18 @@ static bool nft_hash_estimate(const struct nft_set_desc *desc, u32 features,
251 361
252static struct nft_set_ops nft_hash_ops __read_mostly = { 362static struct nft_set_ops nft_hash_ops __read_mostly = {
253 .privsize = nft_hash_privsize, 363 .privsize = nft_hash_privsize,
364 .elemsize = offsetof(struct nft_hash_elem, ext),
254 .estimate = nft_hash_estimate, 365 .estimate = nft_hash_estimate,
255 .init = nft_hash_init, 366 .init = nft_hash_init,
256 .destroy = nft_hash_destroy, 367 .destroy = nft_hash_destroy,
257 .get = nft_hash_get,
258 .insert = nft_hash_insert, 368 .insert = nft_hash_insert,
369 .activate = nft_hash_activate,
370 .deactivate = nft_hash_deactivate,
259 .remove = nft_hash_remove, 371 .remove = nft_hash_remove,
260 .lookup = nft_hash_lookup, 372 .lookup = nft_hash_lookup,
373 .update = nft_hash_update,
261 .walk = nft_hash_walk, 374 .walk = nft_hash_walk,
262 .features = NFT_SET_MAP, 375 .features = NFT_SET_MAP | NFT_SET_TIMEOUT,
263 .owner = THIS_MODULE, 376 .owner = THIS_MODULE,
264}; 377};
265 378
diff --git a/net/netfilter/nft_immediate.c b/net/netfilter/nft_immediate.c
index 810385eb7249..db3b746858e3 100644
--- a/net/netfilter/nft_immediate.c
+++ b/net/netfilter/nft_immediate.c
@@ -24,12 +24,12 @@ struct nft_immediate_expr {
24}; 24};
25 25
26static void nft_immediate_eval(const struct nft_expr *expr, 26static void nft_immediate_eval(const struct nft_expr *expr,
27 struct nft_data data[NFT_REG_MAX + 1], 27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt) 28 const struct nft_pktinfo *pkt)
29{ 29{
30 const struct nft_immediate_expr *priv = nft_expr_priv(expr); 30 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
31 31
32 nft_data_copy(&data[priv->dreg], &priv->data); 32 nft_data_copy(&regs->data[priv->dreg], &priv->data, priv->dlen);
33} 33}
34 34
35static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = { 35static const struct nla_policy nft_immediate_policy[NFTA_IMMEDIATE_MAX + 1] = {
@@ -49,17 +49,15 @@ static int nft_immediate_init(const struct nft_ctx *ctx,
49 tb[NFTA_IMMEDIATE_DATA] == NULL) 49 tb[NFTA_IMMEDIATE_DATA] == NULL)
50 return -EINVAL; 50 return -EINVAL;
51 51
52 priv->dreg = ntohl(nla_get_be32(tb[NFTA_IMMEDIATE_DREG])); 52 err = nft_data_init(ctx, &priv->data, sizeof(priv->data), &desc,
53 err = nft_validate_output_register(priv->dreg); 53 tb[NFTA_IMMEDIATE_DATA]);
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) 54 if (err < 0)
59 return err; 55 return err;
60 priv->dlen = desc.len; 56 priv->dlen = desc.len;
61 57
62 err = nft_validate_data_load(ctx, priv->dreg, &priv->data, desc.type); 58 priv->dreg = nft_parse_register(tb[NFTA_IMMEDIATE_DREG]);
59 err = nft_validate_register_store(ctx, priv->dreg, &priv->data,
60 desc.type, desc.len);
63 if (err < 0) 61 if (err < 0)
64 goto err1; 62 goto err1;
65 63
@@ -81,7 +79,7 @@ static int nft_immediate_dump(struct sk_buff *skb, const struct nft_expr *expr)
81{ 79{
82 const struct nft_immediate_expr *priv = nft_expr_priv(expr); 80 const struct nft_immediate_expr *priv = nft_expr_priv(expr);
83 81
84 if (nla_put_be32(skb, NFTA_IMMEDIATE_DREG, htonl(priv->dreg))) 82 if (nft_dump_register(skb, NFTA_IMMEDIATE_DREG, priv->dreg))
85 goto nla_put_failure; 83 goto nla_put_failure;
86 84
87 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data, 85 return nft_data_dump(skb, NFTA_IMMEDIATE_DATA, &priv->data,
diff --git a/net/netfilter/nft_limit.c b/net/netfilter/nft_limit.c
index 85da5bd02f64..435c1ccd6c0e 100644
--- a/net/netfilter/nft_limit.c
+++ b/net/netfilter/nft_limit.c
@@ -27,7 +27,7 @@ struct nft_limit {
27}; 27};
28 28
29static void nft_limit_eval(const struct nft_expr *expr, 29static void nft_limit_eval(const struct nft_expr *expr,
30 struct nft_data data[NFT_REG_MAX + 1], 30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt) 31 const struct nft_pktinfo *pkt)
32{ 32{
33 struct nft_limit *priv = nft_expr_priv(expr); 33 struct nft_limit *priv = nft_expr_priv(expr);
@@ -45,7 +45,7 @@ static void nft_limit_eval(const struct nft_expr *expr,
45 } 45 }
46 spin_unlock_bh(&limit_lock); 46 spin_unlock_bh(&limit_lock);
47 47
48 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 48 regs->verdict.code = NFT_BREAK;
49} 49}
50 50
51static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = { 51static const struct nla_policy nft_limit_policy[NFTA_LIMIT_MAX + 1] = {
@@ -98,6 +98,7 @@ static struct nft_expr_type nft_limit_type __read_mostly = {
98 .ops = &nft_limit_ops, 98 .ops = &nft_limit_ops,
99 .policy = nft_limit_policy, 99 .policy = nft_limit_policy,
100 .maxattr = NFTA_LIMIT_MAX, 100 .maxattr = NFTA_LIMIT_MAX,
101 .flags = NFT_EXPR_STATEFUL,
101 .owner = THIS_MODULE, 102 .owner = THIS_MODULE,
102}; 103};
103 104
diff --git a/net/netfilter/nft_log.c b/net/netfilter/nft_log.c
index bde05f28cf14..a13d6a386d63 100644
--- a/net/netfilter/nft_log.c
+++ b/net/netfilter/nft_log.c
@@ -27,7 +27,7 @@ struct nft_log {
27}; 27};
28 28
29static void nft_log_eval(const struct nft_expr *expr, 29static void nft_log_eval(const struct nft_expr *expr,
30 struct nft_data data[NFT_REG_MAX + 1], 30 struct nft_regs *regs,
31 const struct nft_pktinfo *pkt) 31 const struct nft_pktinfo *pkt)
32{ 32{
33 const struct nft_log *priv = nft_expr_priv(expr); 33 const struct nft_log *priv = nft_expr_priv(expr);
@@ -78,7 +78,7 @@ static int nft_log_init(const struct nft_ctx *ctx,
78 li->u.log.level = 78 li->u.log.level =
79 ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL])); 79 ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
80 } else { 80 } else {
81 li->u.log.level = 4; 81 li->u.log.level = LOGLEVEL_WARNING;
82 } 82 }
83 if (tb[NFTA_LOG_FLAGS] != NULL) { 83 if (tb[NFTA_LOG_FLAGS] != NULL) {
84 li->u.log.logflags = 84 li->u.log.logflags =
diff --git a/net/netfilter/nft_lookup.c b/net/netfilter/nft_lookup.c
index 9615b8b9fb37..b3c31ef8015d 100644
--- a/net/netfilter/nft_lookup.c
+++ b/net/netfilter/nft_lookup.c
@@ -26,15 +26,20 @@ struct nft_lookup {
26}; 26};
27 27
28static void nft_lookup_eval(const struct nft_expr *expr, 28static void nft_lookup_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1], 29 struct nft_regs *regs,
30 const struct nft_pktinfo *pkt) 30 const struct nft_pktinfo *pkt)
31{ 31{
32 const struct nft_lookup *priv = nft_expr_priv(expr); 32 const struct nft_lookup *priv = nft_expr_priv(expr);
33 const struct nft_set *set = priv->set; 33 const struct nft_set *set = priv->set;
34 const struct nft_set_ext *ext;
34 35
35 if (set->ops->lookup(set, &data[priv->sreg], &data[priv->dreg])) 36 if (set->ops->lookup(set, &regs->data[priv->sreg], &ext)) {
37 if (set->flags & NFT_SET_MAP)
38 nft_data_copy(&regs->data[priv->dreg],
39 nft_set_ext_data(ext), set->dlen);
36 return; 40 return;
37 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 41 }
42 regs->verdict.code = NFT_BREAK;
38} 43}
39 44
40static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = { 45static const struct nla_policy nft_lookup_policy[NFTA_LOOKUP_MAX + 1] = {
@@ -66,8 +71,11 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
66 return PTR_ERR(set); 71 return PTR_ERR(set);
67 } 72 }
68 73
69 priv->sreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_SREG])); 74 if (set->flags & NFT_SET_EVAL)
70 err = nft_validate_input_register(priv->sreg); 75 return -EOPNOTSUPP;
76
77 priv->sreg = nft_parse_register(tb[NFTA_LOOKUP_SREG]);
78 err = nft_validate_register_load(priv->sreg, set->klen);
71 if (err < 0) 79 if (err < 0)
72 return err; 80 return err;
73 81
@@ -75,19 +83,16 @@ static int nft_lookup_init(const struct nft_ctx *ctx,
75 if (!(set->flags & NFT_SET_MAP)) 83 if (!(set->flags & NFT_SET_MAP))
76 return -EINVAL; 84 return -EINVAL;
77 85
78 priv->dreg = ntohl(nla_get_be32(tb[NFTA_LOOKUP_DREG])); 86 priv->dreg = nft_parse_register(tb[NFTA_LOOKUP_DREG]);
79 err = nft_validate_output_register(priv->dreg); 87 err = nft_validate_register_store(ctx, priv->dreg, NULL,
88 set->dtype, set->dlen);
80 if (err < 0) 89 if (err < 0)
81 return err; 90 return err;
82
83 if (priv->dreg == NFT_REG_VERDICT) {
84 if (set->dtype != NFT_DATA_VERDICT)
85 return -EINVAL;
86 } else if (set->dtype == NFT_DATA_VERDICT)
87 return -EINVAL;
88 } else if (set->flags & NFT_SET_MAP) 91 } else if (set->flags & NFT_SET_MAP)
89 return -EINVAL; 92 return -EINVAL;
90 93
94 priv->binding.flags = set->flags & NFT_SET_MAP;
95
91 err = nf_tables_bind_set(ctx, set, &priv->binding); 96 err = nf_tables_bind_set(ctx, set, &priv->binding);
92 if (err < 0) 97 if (err < 0)
93 return err; 98 return err;
@@ -110,10 +115,10 @@ static int nft_lookup_dump(struct sk_buff *skb, const struct nft_expr *expr)
110 115
111 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name)) 116 if (nla_put_string(skb, NFTA_LOOKUP_SET, priv->set->name))
112 goto nla_put_failure; 117 goto nla_put_failure;
113 if (nla_put_be32(skb, NFTA_LOOKUP_SREG, htonl(priv->sreg))) 118 if (nft_dump_register(skb, NFTA_LOOKUP_SREG, priv->sreg))
114 goto nla_put_failure; 119 goto nla_put_failure;
115 if (priv->set->flags & NFT_SET_MAP) 120 if (priv->set->flags & NFT_SET_MAP)
116 if (nla_put_be32(skb, NFTA_LOOKUP_DREG, htonl(priv->dreg))) 121 if (nft_dump_register(skb, NFTA_LOOKUP_DREG, priv->dreg))
117 goto nla_put_failure; 122 goto nla_put_failure;
118 return 0; 123 return 0;
119 124
diff --git a/net/netfilter/nft_meta.c b/net/netfilter/nft_meta.c
index e99911eda915..52561e1c31e2 100644
--- a/net/netfilter/nft_meta.c
+++ b/net/netfilter/nft_meta.c
@@ -25,65 +25,68 @@
25#include <net/netfilter/nft_meta.h> 25#include <net/netfilter/nft_meta.h>
26 26
27void nft_meta_get_eval(const struct nft_expr *expr, 27void nft_meta_get_eval(const struct nft_expr *expr,
28 struct nft_data data[NFT_REG_MAX + 1], 28 struct nft_regs *regs,
29 const struct nft_pktinfo *pkt) 29 const struct nft_pktinfo *pkt)
30{ 30{
31 const struct nft_meta *priv = nft_expr_priv(expr); 31 const struct nft_meta *priv = nft_expr_priv(expr);
32 const struct sk_buff *skb = pkt->skb; 32 const struct sk_buff *skb = pkt->skb;
33 const struct net_device *in = pkt->in, *out = pkt->out; 33 const struct net_device *in = pkt->in, *out = pkt->out;
34 struct nft_data *dest = &data[priv->dreg]; 34 u32 *dest = &regs->data[priv->dreg];
35 35
36 switch (priv->key) { 36 switch (priv->key) {
37 case NFT_META_LEN: 37 case NFT_META_LEN:
38 dest->data[0] = skb->len; 38 *dest = skb->len;
39 break; 39 break;
40 case NFT_META_PROTOCOL: 40 case NFT_META_PROTOCOL:
41 *(__be16 *)dest->data = skb->protocol; 41 *dest = 0;
42 *(__be16 *)dest = skb->protocol;
42 break; 43 break;
43 case NFT_META_NFPROTO: 44 case NFT_META_NFPROTO:
44 dest->data[0] = pkt->ops->pf; 45 *dest = pkt->ops->pf;
45 break; 46 break;
46 case NFT_META_L4PROTO: 47 case NFT_META_L4PROTO:
47 dest->data[0] = pkt->tprot; 48 *dest = pkt->tprot;
48 break; 49 break;
49 case NFT_META_PRIORITY: 50 case NFT_META_PRIORITY:
50 dest->data[0] = skb->priority; 51 *dest = skb->priority;
51 break; 52 break;
52 case NFT_META_MARK: 53 case NFT_META_MARK:
53 dest->data[0] = skb->mark; 54 *dest = skb->mark;
54 break; 55 break;
55 case NFT_META_IIF: 56 case NFT_META_IIF:
56 if (in == NULL) 57 if (in == NULL)
57 goto err; 58 goto err;
58 dest->data[0] = in->ifindex; 59 *dest = in->ifindex;
59 break; 60 break;
60 case NFT_META_OIF: 61 case NFT_META_OIF:
61 if (out == NULL) 62 if (out == NULL)
62 goto err; 63 goto err;
63 dest->data[0] = out->ifindex; 64 *dest = out->ifindex;
64 break; 65 break;
65 case NFT_META_IIFNAME: 66 case NFT_META_IIFNAME:
66 if (in == NULL) 67 if (in == NULL)
67 goto err; 68 goto err;
68 strncpy((char *)dest->data, in->name, sizeof(dest->data)); 69 strncpy((char *)dest, in->name, IFNAMSIZ);
69 break; 70 break;
70 case NFT_META_OIFNAME: 71 case NFT_META_OIFNAME:
71 if (out == NULL) 72 if (out == NULL)
72 goto err; 73 goto err;
73 strncpy((char *)dest->data, out->name, sizeof(dest->data)); 74 strncpy((char *)dest, out->name, IFNAMSIZ);
74 break; 75 break;
75 case NFT_META_IIFTYPE: 76 case NFT_META_IIFTYPE:
76 if (in == NULL) 77 if (in == NULL)
77 goto err; 78 goto err;
78 *(u16 *)dest->data = in->type; 79 *dest = 0;
80 *(u16 *)dest = in->type;
79 break; 81 break;
80 case NFT_META_OIFTYPE: 82 case NFT_META_OIFTYPE:
81 if (out == NULL) 83 if (out == NULL)
82 goto err; 84 goto err;
83 *(u16 *)dest->data = out->type; 85 *dest = 0;
86 *(u16 *)dest = out->type;
84 break; 87 break;
85 case NFT_META_SKUID: 88 case NFT_META_SKUID:
86 if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT) 89 if (skb->sk == NULL || !sk_fullsock(skb->sk))
87 goto err; 90 goto err;
88 91
89 read_lock_bh(&skb->sk->sk_callback_lock); 92 read_lock_bh(&skb->sk->sk_callback_lock);
@@ -93,13 +96,12 @@ void nft_meta_get_eval(const struct nft_expr *expr,
93 goto err; 96 goto err;
94 } 97 }
95 98
96 dest->data[0] = 99 *dest = from_kuid_munged(&init_user_ns,
97 from_kuid_munged(&init_user_ns,
98 skb->sk->sk_socket->file->f_cred->fsuid); 100 skb->sk->sk_socket->file->f_cred->fsuid);
99 read_unlock_bh(&skb->sk->sk_callback_lock); 101 read_unlock_bh(&skb->sk->sk_callback_lock);
100 break; 102 break;
101 case NFT_META_SKGID: 103 case NFT_META_SKGID:
102 if (skb->sk == NULL || skb->sk->sk_state == TCP_TIME_WAIT) 104 if (skb->sk == NULL || !sk_fullsock(skb->sk))
103 goto err; 105 goto err;
104 106
105 read_lock_bh(&skb->sk->sk_callback_lock); 107 read_lock_bh(&skb->sk->sk_callback_lock);
@@ -108,8 +110,7 @@ void nft_meta_get_eval(const struct nft_expr *expr,
108 read_unlock_bh(&skb->sk->sk_callback_lock); 110 read_unlock_bh(&skb->sk->sk_callback_lock);
109 goto err; 111 goto err;
110 } 112 }
111 dest->data[0] = 113 *dest = from_kgid_munged(&init_user_ns,
112 from_kgid_munged(&init_user_ns,
113 skb->sk->sk_socket->file->f_cred->fsgid); 114 skb->sk->sk_socket->file->f_cred->fsgid);
114 read_unlock_bh(&skb->sk->sk_callback_lock); 115 read_unlock_bh(&skb->sk->sk_callback_lock);
115 break; 116 break;
@@ -119,33 +120,33 @@ void nft_meta_get_eval(const struct nft_expr *expr,
119 120
120 if (dst == NULL) 121 if (dst == NULL)
121 goto err; 122 goto err;
122 dest->data[0] = dst->tclassid; 123 *dest = dst->tclassid;
123 break; 124 break;
124 } 125 }
125#endif 126#endif
126#ifdef CONFIG_NETWORK_SECMARK 127#ifdef CONFIG_NETWORK_SECMARK
127 case NFT_META_SECMARK: 128 case NFT_META_SECMARK:
128 dest->data[0] = skb->secmark; 129 *dest = skb->secmark;
129 break; 130 break;
130#endif 131#endif
131 case NFT_META_PKTTYPE: 132 case NFT_META_PKTTYPE:
132 if (skb->pkt_type != PACKET_LOOPBACK) { 133 if (skb->pkt_type != PACKET_LOOPBACK) {
133 dest->data[0] = skb->pkt_type; 134 *dest = skb->pkt_type;
134 break; 135 break;
135 } 136 }
136 137
137 switch (pkt->ops->pf) { 138 switch (pkt->ops->pf) {
138 case NFPROTO_IPV4: 139 case NFPROTO_IPV4:
139 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) 140 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
140 dest->data[0] = PACKET_MULTICAST; 141 *dest = PACKET_MULTICAST;
141 else 142 else
142 dest->data[0] = PACKET_BROADCAST; 143 *dest = PACKET_BROADCAST;
143 break; 144 break;
144 case NFPROTO_IPV6: 145 case NFPROTO_IPV6:
145 if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF) 146 if (ipv6_hdr(skb)->daddr.s6_addr[0] == 0xFF)
146 dest->data[0] = PACKET_MULTICAST; 147 *dest = PACKET_MULTICAST;
147 else 148 else
148 dest->data[0] = PACKET_BROADCAST; 149 *dest = PACKET_BROADCAST;
149 break; 150 break;
150 default: 151 default:
151 WARN_ON(1); 152 WARN_ON(1);
@@ -153,23 +154,22 @@ void nft_meta_get_eval(const struct nft_expr *expr,
153 } 154 }
154 break; 155 break;
155 case NFT_META_CPU: 156 case NFT_META_CPU:
156 dest->data[0] = smp_processor_id(); 157 *dest = raw_smp_processor_id();
157 break; 158 break;
158 case NFT_META_IIFGROUP: 159 case NFT_META_IIFGROUP:
159 if (in == NULL) 160 if (in == NULL)
160 goto err; 161 goto err;
161 dest->data[0] = in->group; 162 *dest = in->group;
162 break; 163 break;
163 case NFT_META_OIFGROUP: 164 case NFT_META_OIFGROUP:
164 if (out == NULL) 165 if (out == NULL)
165 goto err; 166 goto err;
166 dest->data[0] = out->group; 167 *dest = out->group;
167 break; 168 break;
168 case NFT_META_CGROUP: 169 case NFT_META_CGROUP:
169 if (skb->sk == NULL) 170 if (skb->sk == NULL || !sk_fullsock(skb->sk))
170 break; 171 goto err;
171 172 *dest = skb->sk->sk_classid;
172 dest->data[0] = skb->sk->sk_classid;
173 break; 173 break;
174 default: 174 default:
175 WARN_ON(1); 175 WARN_ON(1);
@@ -178,17 +178,17 @@ void nft_meta_get_eval(const struct nft_expr *expr,
178 return; 178 return;
179 179
180err: 180err:
181 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 181 regs->verdict.code = NFT_BREAK;
182} 182}
183EXPORT_SYMBOL_GPL(nft_meta_get_eval); 183EXPORT_SYMBOL_GPL(nft_meta_get_eval);
184 184
185void nft_meta_set_eval(const struct nft_expr *expr, 185void nft_meta_set_eval(const struct nft_expr *expr,
186 struct nft_data data[NFT_REG_MAX + 1], 186 struct nft_regs *regs,
187 const struct nft_pktinfo *pkt) 187 const struct nft_pktinfo *pkt)
188{ 188{
189 const struct nft_meta *meta = nft_expr_priv(expr); 189 const struct nft_meta *meta = nft_expr_priv(expr);
190 struct sk_buff *skb = pkt->skb; 190 struct sk_buff *skb = pkt->skb;
191 u32 value = data[meta->sreg].data[0]; 191 u32 value = regs->data[meta->sreg];
192 192
193 switch (meta->key) { 193 switch (meta->key) {
194 case NFT_META_MARK: 194 case NFT_META_MARK:
@@ -218,22 +218,22 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
218 const struct nlattr * const tb[]) 218 const struct nlattr * const tb[])
219{ 219{
220 struct nft_meta *priv = nft_expr_priv(expr); 220 struct nft_meta *priv = nft_expr_priv(expr);
221 int err; 221 unsigned int len;
222 222
223 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); 223 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
224 switch (priv->key) { 224 switch (priv->key) {
225 case NFT_META_LEN:
226 case NFT_META_PROTOCOL: 225 case NFT_META_PROTOCOL:
226 case NFT_META_IIFTYPE:
227 case NFT_META_OIFTYPE:
228 len = sizeof(u16);
229 break;
227 case NFT_META_NFPROTO: 230 case NFT_META_NFPROTO:
228 case NFT_META_L4PROTO: 231 case NFT_META_L4PROTO:
232 case NFT_META_LEN:
229 case NFT_META_PRIORITY: 233 case NFT_META_PRIORITY:
230 case NFT_META_MARK: 234 case NFT_META_MARK:
231 case NFT_META_IIF: 235 case NFT_META_IIF:
232 case NFT_META_OIF: 236 case NFT_META_OIF:
233 case NFT_META_IIFNAME:
234 case NFT_META_OIFNAME:
235 case NFT_META_IIFTYPE:
236 case NFT_META_OIFTYPE:
237 case NFT_META_SKUID: 237 case NFT_META_SKUID:
238 case NFT_META_SKGID: 238 case NFT_META_SKGID:
239#ifdef CONFIG_IP_ROUTE_CLASSID 239#ifdef CONFIG_IP_ROUTE_CLASSID
@@ -247,21 +247,19 @@ int nft_meta_get_init(const struct nft_ctx *ctx,
247 case NFT_META_IIFGROUP: 247 case NFT_META_IIFGROUP:
248 case NFT_META_OIFGROUP: 248 case NFT_META_OIFGROUP:
249 case NFT_META_CGROUP: 249 case NFT_META_CGROUP:
250 len = sizeof(u32);
251 break;
252 case NFT_META_IIFNAME:
253 case NFT_META_OIFNAME:
254 len = IFNAMSIZ;
250 break; 255 break;
251 default: 256 default:
252 return -EOPNOTSUPP; 257 return -EOPNOTSUPP;
253 } 258 }
254 259
255 priv->dreg = ntohl(nla_get_be32(tb[NFTA_META_DREG])); 260 priv->dreg = nft_parse_register(tb[NFTA_META_DREG]);
256 err = nft_validate_output_register(priv->dreg); 261 return nft_validate_register_store(ctx, priv->dreg, NULL,
257 if (err < 0) 262 NFT_DATA_VALUE, len);
258 return err;
259
260 err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
261 if (err < 0)
262 return err;
263
264 return 0;
265} 263}
266EXPORT_SYMBOL_GPL(nft_meta_get_init); 264EXPORT_SYMBOL_GPL(nft_meta_get_init);
267 265
@@ -270,20 +268,24 @@ int nft_meta_set_init(const struct nft_ctx *ctx,
270 const struct nlattr * const tb[]) 268 const struct nlattr * const tb[])
271{ 269{
272 struct nft_meta *priv = nft_expr_priv(expr); 270 struct nft_meta *priv = nft_expr_priv(expr);
271 unsigned int len;
273 int err; 272 int err;
274 273
275 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY])); 274 priv->key = ntohl(nla_get_be32(tb[NFTA_META_KEY]));
276 switch (priv->key) { 275 switch (priv->key) {
277 case NFT_META_MARK: 276 case NFT_META_MARK:
278 case NFT_META_PRIORITY: 277 case NFT_META_PRIORITY:
278 len = sizeof(u32);
279 break;
279 case NFT_META_NFTRACE: 280 case NFT_META_NFTRACE:
281 len = sizeof(u8);
280 break; 282 break;
281 default: 283 default:
282 return -EOPNOTSUPP; 284 return -EOPNOTSUPP;
283 } 285 }
284 286
285 priv->sreg = ntohl(nla_get_be32(tb[NFTA_META_SREG])); 287 priv->sreg = nft_parse_register(tb[NFTA_META_SREG]);
286 err = nft_validate_input_register(priv->sreg); 288 err = nft_validate_register_load(priv->sreg, len);
287 if (err < 0) 289 if (err < 0)
288 return err; 290 return err;
289 291
@@ -298,7 +300,7 @@ int nft_meta_get_dump(struct sk_buff *skb,
298 300
299 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) 301 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
300 goto nla_put_failure; 302 goto nla_put_failure;
301 if (nla_put_be32(skb, NFTA_META_DREG, htonl(priv->dreg))) 303 if (nft_dump_register(skb, NFTA_META_DREG, priv->dreg))
302 goto nla_put_failure; 304 goto nla_put_failure;
303 return 0; 305 return 0;
304 306
@@ -314,7 +316,7 @@ int nft_meta_set_dump(struct sk_buff *skb,
314 316
315 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key))) 317 if (nla_put_be32(skb, NFTA_META_KEY, htonl(priv->key)))
316 goto nla_put_failure; 318 goto nla_put_failure;
317 if (nla_put_be32(skb, NFTA_META_SREG, htonl(priv->sreg))) 319 if (nft_dump_register(skb, NFTA_META_SREG, priv->sreg))
318 goto nla_put_failure; 320 goto nla_put_failure;
319 321
320 return 0; 322 return 0;
diff --git a/net/netfilter/nft_nat.c b/net/netfilter/nft_nat.c
index a0837c6c9283..ee2d71753746 100644
--- a/net/netfilter/nft_nat.c
+++ b/net/netfilter/nft_nat.c
@@ -37,7 +37,7 @@ struct nft_nat {
37}; 37};
38 38
39static void nft_nat_eval(const struct nft_expr *expr, 39static void nft_nat_eval(const struct nft_expr *expr,
40 struct nft_data data[NFT_REG_MAX + 1], 40 struct nft_regs *regs,
41 const struct nft_pktinfo *pkt) 41 const struct nft_pktinfo *pkt)
42{ 42{
43 const struct nft_nat *priv = nft_expr_priv(expr); 43 const struct nft_nat *priv = nft_expr_priv(expr);
@@ -49,33 +49,32 @@ static void nft_nat_eval(const struct nft_expr *expr,
49 if (priv->sreg_addr_min) { 49 if (priv->sreg_addr_min) {
50 if (priv->family == AF_INET) { 50 if (priv->family == AF_INET) {
51 range.min_addr.ip = (__force __be32) 51 range.min_addr.ip = (__force __be32)
52 data[priv->sreg_addr_min].data[0]; 52 regs->data[priv->sreg_addr_min];
53 range.max_addr.ip = (__force __be32) 53 range.max_addr.ip = (__force __be32)
54 data[priv->sreg_addr_max].data[0]; 54 regs->data[priv->sreg_addr_max];
55 55
56 } else { 56 } else {
57 memcpy(range.min_addr.ip6, 57 memcpy(range.min_addr.ip6,
58 data[priv->sreg_addr_min].data, 58 &regs->data[priv->sreg_addr_min],
59 sizeof(struct nft_data)); 59 sizeof(range.min_addr.ip6));
60 memcpy(range.max_addr.ip6, 60 memcpy(range.max_addr.ip6,
61 data[priv->sreg_addr_max].data, 61 &regs->data[priv->sreg_addr_max],
62 sizeof(struct nft_data)); 62 sizeof(range.max_addr.ip6));
63 } 63 }
64 range.flags |= NF_NAT_RANGE_MAP_IPS; 64 range.flags |= NF_NAT_RANGE_MAP_IPS;
65 } 65 }
66 66
67 if (priv->sreg_proto_min) { 67 if (priv->sreg_proto_min) {
68 range.min_proto.all = 68 range.min_proto.all =
69 *(__be16 *)&data[priv->sreg_proto_min].data[0]; 69 *(__be16 *)&regs->data[priv->sreg_proto_min];
70 range.max_proto.all = 70 range.max_proto.all =
71 *(__be16 *)&data[priv->sreg_proto_max].data[0]; 71 *(__be16 *)&regs->data[priv->sreg_proto_max];
72 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED; 72 range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
73 } 73 }
74 74
75 range.flags |= priv->flags; 75 range.flags |= priv->flags;
76 76
77 data[NFT_REG_VERDICT].verdict = 77 regs->verdict.code = nf_nat_setup_info(ct, &range, priv->type);
78 nf_nat_setup_info(ct, &range, priv->type);
79} 78}
80 79
81static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = { 80static const struct nla_policy nft_nat_policy[NFTA_NAT_MAX + 1] = {
@@ -119,6 +118,7 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
119 const struct nlattr * const tb[]) 118 const struct nlattr * const tb[])
120{ 119{
121 struct nft_nat *priv = nft_expr_priv(expr); 120 struct nft_nat *priv = nft_expr_priv(expr);
121 unsigned int alen, plen;
122 u32 family; 122 u32 family;
123 int err; 123 int err;
124 124
@@ -146,25 +146,34 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
146 return -EINVAL; 146 return -EINVAL;
147 147
148 family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY])); 148 family = ntohl(nla_get_be32(tb[NFTA_NAT_FAMILY]));
149 if (family != AF_INET && family != AF_INET6)
150 return -EAFNOSUPPORT;
151 if (family != ctx->afi->family) 149 if (family != ctx->afi->family)
152 return -EOPNOTSUPP; 150 return -EOPNOTSUPP;
151
152 switch (family) {
153 case NFPROTO_IPV4:
154 alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip);
155 break;
156 case NFPROTO_IPV6:
157 alen = FIELD_SIZEOF(struct nf_nat_range, min_addr.ip6);
158 break;
159 default:
160 return -EAFNOSUPPORT;
161 }
153 priv->family = family; 162 priv->family = family;
154 163
155 if (tb[NFTA_NAT_REG_ADDR_MIN]) { 164 if (tb[NFTA_NAT_REG_ADDR_MIN]) {
156 priv->sreg_addr_min = 165 priv->sreg_addr_min =
157 ntohl(nla_get_be32(tb[NFTA_NAT_REG_ADDR_MIN])); 166 nft_parse_register(tb[NFTA_NAT_REG_ADDR_MIN]);
158 167 err = nft_validate_register_load(priv->sreg_addr_min, alen);
159 err = nft_validate_input_register(priv->sreg_addr_min);
160 if (err < 0) 168 if (err < 0)
161 return err; 169 return err;
162 170
163 if (tb[NFTA_NAT_REG_ADDR_MAX]) { 171 if (tb[NFTA_NAT_REG_ADDR_MAX]) {
164 priv->sreg_addr_max = 172 priv->sreg_addr_max =
165 ntohl(nla_get_be32(tb[NFTA_NAT_REG_ADDR_MAX])); 173 nft_parse_register(tb[NFTA_NAT_REG_ADDR_MAX]);
166 174
167 err = nft_validate_input_register(priv->sreg_addr_max); 175 err = nft_validate_register_load(priv->sreg_addr_max,
176 alen);
168 if (err < 0) 177 if (err < 0)
169 return err; 178 return err;
170 } else { 179 } else {
@@ -172,19 +181,21 @@ static int nft_nat_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
172 } 181 }
173 } 182 }
174 183
184 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
175 if (tb[NFTA_NAT_REG_PROTO_MIN]) { 185 if (tb[NFTA_NAT_REG_PROTO_MIN]) {
176 priv->sreg_proto_min = 186 priv->sreg_proto_min =
177 ntohl(nla_get_be32(tb[NFTA_NAT_REG_PROTO_MIN])); 187 nft_parse_register(tb[NFTA_NAT_REG_PROTO_MIN]);
178 188
179 err = nft_validate_input_register(priv->sreg_proto_min); 189 err = nft_validate_register_load(priv->sreg_proto_min, plen);
180 if (err < 0) 190 if (err < 0)
181 return err; 191 return err;
182 192
183 if (tb[NFTA_NAT_REG_PROTO_MAX]) { 193 if (tb[NFTA_NAT_REG_PROTO_MAX]) {
184 priv->sreg_proto_max = 194 priv->sreg_proto_max =
185 ntohl(nla_get_be32(tb[NFTA_NAT_REG_PROTO_MAX])); 195 nft_parse_register(tb[NFTA_NAT_REG_PROTO_MAX]);
186 196
187 err = nft_validate_input_register(priv->sreg_proto_max); 197 err = nft_validate_register_load(priv->sreg_proto_max,
198 plen);
188 if (err < 0) 199 if (err < 0)
189 return err; 200 return err;
190 } else { 201 } else {
@@ -220,18 +231,18 @@ static int nft_nat_dump(struct sk_buff *skb, const struct nft_expr *expr)
220 goto nla_put_failure; 231 goto nla_put_failure;
221 232
222 if (priv->sreg_addr_min) { 233 if (priv->sreg_addr_min) {
223 if (nla_put_be32(skb, NFTA_NAT_REG_ADDR_MIN, 234 if (nft_dump_register(skb, NFTA_NAT_REG_ADDR_MIN,
224 htonl(priv->sreg_addr_min)) || 235 priv->sreg_addr_min) ||
225 nla_put_be32(skb, NFTA_NAT_REG_ADDR_MAX, 236 nft_dump_register(skb, NFTA_NAT_REG_ADDR_MAX,
226 htonl(priv->sreg_addr_max))) 237 priv->sreg_addr_max))
227 goto nla_put_failure; 238 goto nla_put_failure;
228 } 239 }
229 240
230 if (priv->sreg_proto_min) { 241 if (priv->sreg_proto_min) {
231 if (nla_put_be32(skb, NFTA_NAT_REG_PROTO_MIN, 242 if (nft_dump_register(skb, NFTA_NAT_REG_PROTO_MIN,
232 htonl(priv->sreg_proto_min)) || 243 priv->sreg_proto_min) ||
233 nla_put_be32(skb, NFTA_NAT_REG_PROTO_MAX, 244 nft_dump_register(skb, NFTA_NAT_REG_PROTO_MAX,
234 htonl(priv->sreg_proto_max))) 245 priv->sreg_proto_max))
235 goto nla_put_failure; 246 goto nla_put_failure;
236 } 247 }
237 248
diff --git a/net/netfilter/nft_payload.c b/net/netfilter/nft_payload.c
index 85daa84bfdfe..94fb3b27a2c5 100644
--- a/net/netfilter/nft_payload.c
+++ b/net/netfilter/nft_payload.c
@@ -18,12 +18,12 @@
18#include <net/netfilter/nf_tables.h> 18#include <net/netfilter/nf_tables.h>
19 19
20static void nft_payload_eval(const struct nft_expr *expr, 20static void nft_payload_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt) 22 const struct nft_pktinfo *pkt)
23{ 23{
24 const struct nft_payload *priv = nft_expr_priv(expr); 24 const struct nft_payload *priv = nft_expr_priv(expr);
25 const struct sk_buff *skb = pkt->skb; 25 const struct sk_buff *skb = pkt->skb;
26 struct nft_data *dest = &data[priv->dreg]; 26 u32 *dest = &regs->data[priv->dreg];
27 int offset; 27 int offset;
28 28
29 switch (priv->base) { 29 switch (priv->base) {
@@ -43,11 +43,12 @@ static void nft_payload_eval(const struct nft_expr *expr,
43 } 43 }
44 offset += priv->offset; 44 offset += priv->offset;
45 45
46 if (skb_copy_bits(skb, offset, dest->data, priv->len) < 0) 46 dest[priv->len / NFT_REG32_SIZE] = 0;
47 if (skb_copy_bits(skb, offset, dest, priv->len) < 0)
47 goto err; 48 goto err;
48 return; 49 return;
49err: 50err:
50 data[NFT_REG_VERDICT].verdict = NFT_BREAK; 51 regs->verdict.code = NFT_BREAK;
51} 52}
52 53
53static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = { 54static const struct nla_policy nft_payload_policy[NFTA_PAYLOAD_MAX + 1] = {
@@ -62,24 +63,21 @@ static int nft_payload_init(const struct nft_ctx *ctx,
62 const struct nlattr * const tb[]) 63 const struct nlattr * const tb[])
63{ 64{
64 struct nft_payload *priv = nft_expr_priv(expr); 65 struct nft_payload *priv = nft_expr_priv(expr);
65 int err;
66 66
67 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE])); 67 priv->base = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_BASE]));
68 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); 68 priv->offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
69 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); 69 priv->len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN]));
70 priv->dreg = nft_parse_register(tb[NFTA_PAYLOAD_DREG]);
70 71
71 priv->dreg = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_DREG])); 72 return nft_validate_register_store(ctx, priv->dreg, NULL,
72 err = nft_validate_output_register(priv->dreg); 73 NFT_DATA_VALUE, priv->len);
73 if (err < 0)
74 return err;
75 return nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
76} 74}
77 75
78static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr) 76static int nft_payload_dump(struct sk_buff *skb, const struct nft_expr *expr)
79{ 77{
80 const struct nft_payload *priv = nft_expr_priv(expr); 78 const struct nft_payload *priv = nft_expr_priv(expr);
81 79
82 if (nla_put_be32(skb, NFTA_PAYLOAD_DREG, htonl(priv->dreg)) || 80 if (nft_dump_register(skb, NFTA_PAYLOAD_DREG, priv->dreg) ||
83 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) || 81 nla_put_be32(skb, NFTA_PAYLOAD_BASE, htonl(priv->base)) ||
84 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) || 82 nla_put_be32(skb, NFTA_PAYLOAD_OFFSET, htonl(priv->offset)) ||
85 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len))) 83 nla_put_be32(skb, NFTA_PAYLOAD_LEN, htonl(priv->len)))
@@ -131,9 +129,7 @@ nft_payload_select_ops(const struct nft_ctx *ctx,
131 } 129 }
132 130
133 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET])); 131 offset = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_OFFSET]));
134 len = ntohl(nla_get_be32(tb[NFTA_PAYLOAD_LEN])); 132 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 133
138 if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) && 134 if (len <= 4 && is_power_of_2(len) && IS_ALIGNED(offset, len) &&
139 base != NFT_PAYLOAD_LL_HEADER) 135 base != NFT_PAYLOAD_LL_HEADER)
diff --git a/net/netfilter/nft_queue.c b/net/netfilter/nft_queue.c
index e8ae2f6bf232..96805d21d618 100644
--- a/net/netfilter/nft_queue.c
+++ b/net/netfilter/nft_queue.c
@@ -28,7 +28,7 @@ struct nft_queue {
28}; 28};
29 29
30static void nft_queue_eval(const struct nft_expr *expr, 30static void nft_queue_eval(const struct nft_expr *expr,
31 struct nft_data data[NFT_REG_MAX + 1], 31 struct nft_regs *regs,
32 const struct nft_pktinfo *pkt) 32 const struct nft_pktinfo *pkt)
33{ 33{
34 struct nft_queue *priv = nft_expr_priv(expr); 34 struct nft_queue *priv = nft_expr_priv(expr);
@@ -51,7 +51,7 @@ static void nft_queue_eval(const struct nft_expr *expr,
51 if (priv->flags & NFT_QUEUE_FLAG_BYPASS) 51 if (priv->flags & NFT_QUEUE_FLAG_BYPASS)
52 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS; 52 ret |= NF_VERDICT_FLAG_QUEUE_BYPASS;
53 53
54 data[NFT_REG_VERDICT].verdict = ret; 54 regs->verdict.code = ret;
55} 55}
56 56
57static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = { 57static const struct nla_policy nft_queue_policy[NFTA_QUEUE_MAX + 1] = {
diff --git a/net/netfilter/nft_rbtree.c b/net/netfilter/nft_rbtree.c
index 46214f245665..1c30f41cff5b 100644
--- a/net/netfilter/nft_rbtree.c
+++ b/net/netfilter/nft_rbtree.c
@@ -26,25 +26,25 @@ struct nft_rbtree {
26 26
27struct nft_rbtree_elem { 27struct nft_rbtree_elem {
28 struct rb_node node; 28 struct rb_node node;
29 u16 flags; 29 struct nft_set_ext ext;
30 struct nft_data key;
31 struct nft_data data[];
32}; 30};
33 31
34static bool nft_rbtree_lookup(const struct nft_set *set, 32
35 const struct nft_data *key, 33static bool nft_rbtree_lookup(const struct nft_set *set, const u32 *key,
36 struct nft_data *data) 34 const struct nft_set_ext **ext)
37{ 35{
38 const struct nft_rbtree *priv = nft_set_priv(set); 36 const struct nft_rbtree *priv = nft_set_priv(set);
39 const struct nft_rbtree_elem *rbe, *interval = NULL; 37 const struct nft_rbtree_elem *rbe, *interval = NULL;
40 const struct rb_node *parent = priv->root.rb_node; 38 const struct rb_node *parent;
39 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
41 int d; 40 int d;
42 41
43 spin_lock_bh(&nft_rbtree_lock); 42 spin_lock_bh(&nft_rbtree_lock);
43 parent = priv->root.rb_node;
44 while (parent != NULL) { 44 while (parent != NULL) {
45 rbe = rb_entry(parent, struct nft_rbtree_elem, node); 45 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
46 46
47 d = nft_data_cmp(&rbe->key, key, set->klen); 47 d = memcmp(nft_set_ext_key(&rbe->ext), key, set->klen);
48 if (d < 0) { 48 if (d < 0) {
49 parent = parent->rb_left; 49 parent = parent->rb_left;
50 interval = rbe; 50 interval = rbe;
@@ -52,12 +52,17 @@ static bool nft_rbtree_lookup(const struct nft_set *set,
52 parent = parent->rb_right; 52 parent = parent->rb_right;
53 else { 53 else {
54found: 54found:
55 if (rbe->flags & NFT_SET_ELEM_INTERVAL_END) 55 if (!nft_set_elem_active(&rbe->ext, genmask)) {
56 parent = parent->rb_left;
57 continue;
58 }
59 if (nft_set_ext_exists(&rbe->ext, NFT_SET_EXT_FLAGS) &&
60 *nft_set_ext_flags(&rbe->ext) &
61 NFT_SET_ELEM_INTERVAL_END)
56 goto out; 62 goto out;
57 if (set->flags & NFT_SET_MAP)
58 nft_data_copy(data, rbe->data);
59
60 spin_unlock_bh(&nft_rbtree_lock); 63 spin_unlock_bh(&nft_rbtree_lock);
64
65 *ext = &rbe->ext;
61 return true; 66 return true;
62 } 67 }
63 } 68 }
@@ -71,23 +76,13 @@ out:
71 return false; 76 return false;
72} 77}
73 78
74static void nft_rbtree_elem_destroy(const struct nft_set *set,
75 struct nft_rbtree_elem *rbe)
76{
77 nft_data_uninit(&rbe->key, NFT_DATA_VALUE);
78 if (set->flags & NFT_SET_MAP &&
79 !(rbe->flags & NFT_SET_ELEM_INTERVAL_END))
80 nft_data_uninit(rbe->data, set->dtype);
81
82 kfree(rbe);
83}
84
85static int __nft_rbtree_insert(const struct nft_set *set, 79static int __nft_rbtree_insert(const struct nft_set *set,
86 struct nft_rbtree_elem *new) 80 struct nft_rbtree_elem *new)
87{ 81{
88 struct nft_rbtree *priv = nft_set_priv(set); 82 struct nft_rbtree *priv = nft_set_priv(set);
89 struct nft_rbtree_elem *rbe; 83 struct nft_rbtree_elem *rbe;
90 struct rb_node *parent, **p; 84 struct rb_node *parent, **p;
85 u8 genmask = nft_genmask_next(read_pnet(&set->pnet));
91 int d; 86 int d;
92 87
93 parent = NULL; 88 parent = NULL;
@@ -95,13 +90,18 @@ static int __nft_rbtree_insert(const struct nft_set *set,
95 while (*p != NULL) { 90 while (*p != NULL) {
96 parent = *p; 91 parent = *p;
97 rbe = rb_entry(parent, struct nft_rbtree_elem, node); 92 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
98 d = nft_data_cmp(&rbe->key, &new->key, set->klen); 93 d = memcmp(nft_set_ext_key(&rbe->ext),
94 nft_set_ext_key(&new->ext),
95 set->klen);
99 if (d < 0) 96 if (d < 0)
100 p = &parent->rb_left; 97 p = &parent->rb_left;
101 else if (d > 0) 98 else if (d > 0)
102 p = &parent->rb_right; 99 p = &parent->rb_right;
103 else 100 else {
104 return -EEXIST; 101 if (nft_set_elem_active(&rbe->ext, genmask))
102 return -EEXIST;
103 p = &parent->rb_left;
104 }
105 } 105 }
106 rb_link_node(&new->node, parent, p); 106 rb_link_node(&new->node, parent, p);
107 rb_insert_color(&new->node, &priv->root); 107 rb_insert_color(&new->node, &priv->root);
@@ -111,31 +111,13 @@ static int __nft_rbtree_insert(const struct nft_set *set,
111static int nft_rbtree_insert(const struct nft_set *set, 111static int nft_rbtree_insert(const struct nft_set *set,
112 const struct nft_set_elem *elem) 112 const struct nft_set_elem *elem)
113{ 113{
114 struct nft_rbtree_elem *rbe; 114 struct nft_rbtree_elem *rbe = elem->priv;
115 unsigned int size;
116 int err; 115 int err;
117 116
118 size = sizeof(*rbe);
119 if (set->flags & NFT_SET_MAP &&
120 !(elem->flags & NFT_SET_ELEM_INTERVAL_END))
121 size += sizeof(rbe->data[0]);
122
123 rbe = kzalloc(size, GFP_KERNEL);
124 if (rbe == NULL)
125 return -ENOMEM;
126
127 rbe->flags = elem->flags;
128 nft_data_copy(&rbe->key, &elem->key);
129 if (set->flags & NFT_SET_MAP &&
130 !(rbe->flags & NFT_SET_ELEM_INTERVAL_END))
131 nft_data_copy(rbe->data, &elem->data);
132
133 spin_lock_bh(&nft_rbtree_lock); 117 spin_lock_bh(&nft_rbtree_lock);
134 err = __nft_rbtree_insert(set, rbe); 118 err = __nft_rbtree_insert(set, rbe);
135 if (err < 0)
136 kfree(rbe);
137
138 spin_unlock_bh(&nft_rbtree_lock); 119 spin_unlock_bh(&nft_rbtree_lock);
120
139 return err; 121 return err;
140} 122}
141 123
@@ -143,42 +125,49 @@ static void nft_rbtree_remove(const struct nft_set *set,
143 const struct nft_set_elem *elem) 125 const struct nft_set_elem *elem)
144{ 126{
145 struct nft_rbtree *priv = nft_set_priv(set); 127 struct nft_rbtree *priv = nft_set_priv(set);
146 struct nft_rbtree_elem *rbe = elem->cookie; 128 struct nft_rbtree_elem *rbe = elem->priv;
147 129
148 spin_lock_bh(&nft_rbtree_lock); 130 spin_lock_bh(&nft_rbtree_lock);
149 rb_erase(&rbe->node, &priv->root); 131 rb_erase(&rbe->node, &priv->root);
150 spin_unlock_bh(&nft_rbtree_lock); 132 spin_unlock_bh(&nft_rbtree_lock);
151 kfree(rbe);
152} 133}
153 134
154static int nft_rbtree_get(const struct nft_set *set, struct nft_set_elem *elem) 135static void nft_rbtree_activate(const struct nft_set *set,
136 const struct nft_set_elem *elem)
137{
138 struct nft_rbtree_elem *rbe = elem->priv;
139
140 nft_set_elem_change_active(set, &rbe->ext);
141}
142
143static void *nft_rbtree_deactivate(const struct nft_set *set,
144 const struct nft_set_elem *elem)
155{ 145{
156 const struct nft_rbtree *priv = nft_set_priv(set); 146 const struct nft_rbtree *priv = nft_set_priv(set);
157 const struct rb_node *parent = priv->root.rb_node; 147 const struct rb_node *parent = priv->root.rb_node;
158 struct nft_rbtree_elem *rbe; 148 struct nft_rbtree_elem *rbe;
149 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
159 int d; 150 int d;
160 151
161 spin_lock_bh(&nft_rbtree_lock);
162 while (parent != NULL) { 152 while (parent != NULL) {
163 rbe = rb_entry(parent, struct nft_rbtree_elem, node); 153 rbe = rb_entry(parent, struct nft_rbtree_elem, node);
164 154
165 d = nft_data_cmp(&rbe->key, &elem->key, set->klen); 155 d = memcmp(nft_set_ext_key(&rbe->ext), &elem->key.val,
156 set->klen);
166 if (d < 0) 157 if (d < 0)
167 parent = parent->rb_left; 158 parent = parent->rb_left;
168 else if (d > 0) 159 else if (d > 0)
169 parent = parent->rb_right; 160 parent = parent->rb_right;
170 else { 161 else {
171 elem->cookie = rbe; 162 if (!nft_set_elem_active(&rbe->ext, genmask)) {
172 if (set->flags & NFT_SET_MAP && 163 parent = parent->rb_left;
173 !(rbe->flags & NFT_SET_ELEM_INTERVAL_END)) 164 continue;
174 nft_data_copy(&elem->data, rbe->data); 165 }
175 elem->flags = rbe->flags; 166 nft_set_elem_change_active(set, &rbe->ext);
176 spin_unlock_bh(&nft_rbtree_lock); 167 return rbe;
177 return 0;
178 } 168 }
179 } 169 }
180 spin_unlock_bh(&nft_rbtree_lock); 170 return NULL;
181 return -ENOENT;
182} 171}
183 172
184static void nft_rbtree_walk(const struct nft_ctx *ctx, 173static void nft_rbtree_walk(const struct nft_ctx *ctx,
@@ -186,21 +175,21 @@ static void nft_rbtree_walk(const struct nft_ctx *ctx,
186 struct nft_set_iter *iter) 175 struct nft_set_iter *iter)
187{ 176{
188 const struct nft_rbtree *priv = nft_set_priv(set); 177 const struct nft_rbtree *priv = nft_set_priv(set);
189 const struct nft_rbtree_elem *rbe; 178 struct nft_rbtree_elem *rbe;
190 struct nft_set_elem elem; 179 struct nft_set_elem elem;
191 struct rb_node *node; 180 struct rb_node *node;
181 u8 genmask = nft_genmask_cur(read_pnet(&set->pnet));
192 182
193 spin_lock_bh(&nft_rbtree_lock); 183 spin_lock_bh(&nft_rbtree_lock);
194 for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) { 184 for (node = rb_first(&priv->root); node != NULL; node = rb_next(node)) {
185 rbe = rb_entry(node, struct nft_rbtree_elem, node);
186
195 if (iter->count < iter->skip) 187 if (iter->count < iter->skip)
196 goto cont; 188 goto cont;
189 if (!nft_set_elem_active(&rbe->ext, genmask))
190 goto cont;
197 191
198 rbe = rb_entry(node, struct nft_rbtree_elem, node); 192 elem.priv = rbe;
199 nft_data_copy(&elem.key, &rbe->key);
200 if (set->flags & NFT_SET_MAP &&
201 !(rbe->flags & NFT_SET_ELEM_INTERVAL_END))
202 nft_data_copy(&elem.data, rbe->data);
203 elem.flags = rbe->flags;
204 193
205 iter->err = iter->fn(ctx, set, iter, &elem); 194 iter->err = iter->fn(ctx, set, iter, &elem);
206 if (iter->err < 0) { 195 if (iter->err < 0) {
@@ -237,7 +226,7 @@ static void nft_rbtree_destroy(const struct nft_set *set)
237 while ((node = priv->root.rb_node) != NULL) { 226 while ((node = priv->root.rb_node) != NULL) {
238 rb_erase(node, &priv->root); 227 rb_erase(node, &priv->root);
239 rbe = rb_entry(node, struct nft_rbtree_elem, node); 228 rbe = rb_entry(node, struct nft_rbtree_elem, node);
240 nft_rbtree_elem_destroy(set, rbe); 229 nft_set_elem_destroy(set, rbe);
241 } 230 }
242} 231}
243 232
@@ -247,9 +236,6 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
247 unsigned int nsize; 236 unsigned int nsize;
248 237
249 nsize = sizeof(struct nft_rbtree_elem); 238 nsize = sizeof(struct nft_rbtree_elem);
250 if (features & NFT_SET_MAP)
251 nsize += FIELD_SIZEOF(struct nft_rbtree_elem, data[0]);
252
253 if (desc->size) 239 if (desc->size)
254 est->size = sizeof(struct nft_rbtree) + desc->size * nsize; 240 est->size = sizeof(struct nft_rbtree) + desc->size * nsize;
255 else 241 else
@@ -262,12 +248,14 @@ static bool nft_rbtree_estimate(const struct nft_set_desc *desc, u32 features,
262 248
263static struct nft_set_ops nft_rbtree_ops __read_mostly = { 249static struct nft_set_ops nft_rbtree_ops __read_mostly = {
264 .privsize = nft_rbtree_privsize, 250 .privsize = nft_rbtree_privsize,
251 .elemsize = offsetof(struct nft_rbtree_elem, ext),
265 .estimate = nft_rbtree_estimate, 252 .estimate = nft_rbtree_estimate,
266 .init = nft_rbtree_init, 253 .init = nft_rbtree_init,
267 .destroy = nft_rbtree_destroy, 254 .destroy = nft_rbtree_destroy,
268 .insert = nft_rbtree_insert, 255 .insert = nft_rbtree_insert,
269 .remove = nft_rbtree_remove, 256 .remove = nft_rbtree_remove,
270 .get = nft_rbtree_get, 257 .deactivate = nft_rbtree_deactivate,
258 .activate = nft_rbtree_activate,
271 .lookup = nft_rbtree_lookup, 259 .lookup = nft_rbtree_lookup,
272 .walk = nft_rbtree_walk, 260 .walk = nft_rbtree_walk,
273 .features = NFT_SET_INTERVAL | NFT_SET_MAP, 261 .features = NFT_SET_INTERVAL | NFT_SET_MAP,
diff --git a/net/netfilter/nft_redir.c b/net/netfilter/nft_redir.c
index d7e9e93a4e90..03f7bf40ae75 100644
--- a/net/netfilter/nft_redir.c
+++ b/net/netfilter/nft_redir.c
@@ -44,25 +44,28 @@ int nft_redir_init(const struct nft_ctx *ctx,
44 const struct nlattr * const tb[]) 44 const struct nlattr * const tb[])
45{ 45{
46 struct nft_redir *priv = nft_expr_priv(expr); 46 struct nft_redir *priv = nft_expr_priv(expr);
47 unsigned int plen;
47 int err; 48 int err;
48 49
49 err = nft_redir_validate(ctx, expr, NULL); 50 err = nft_redir_validate(ctx, expr, NULL);
50 if (err < 0) 51 if (err < 0)
51 return err; 52 return err;
52 53
54 plen = FIELD_SIZEOF(struct nf_nat_range, min_addr.all);
53 if (tb[NFTA_REDIR_REG_PROTO_MIN]) { 55 if (tb[NFTA_REDIR_REG_PROTO_MIN]) {
54 priv->sreg_proto_min = 56 priv->sreg_proto_min =
55 ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MIN])); 57 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MIN]);
56 58
57 err = nft_validate_input_register(priv->sreg_proto_min); 59 err = nft_validate_register_load(priv->sreg_proto_min, plen);
58 if (err < 0) 60 if (err < 0)
59 return err; 61 return err;
60 62
61 if (tb[NFTA_REDIR_REG_PROTO_MAX]) { 63 if (tb[NFTA_REDIR_REG_PROTO_MAX]) {
62 priv->sreg_proto_max = 64 priv->sreg_proto_max =
63 ntohl(nla_get_be32(tb[NFTA_REDIR_REG_PROTO_MAX])); 65 nft_parse_register(tb[NFTA_REDIR_REG_PROTO_MAX]);
64 66
65 err = nft_validate_input_register(priv->sreg_proto_max); 67 err = nft_validate_register_load(priv->sreg_proto_max,
68 plen);
66 if (err < 0) 69 if (err < 0)
67 return err; 70 return err;
68 } else { 71 } else {
@@ -85,11 +88,11 @@ int nft_redir_dump(struct sk_buff *skb, const struct nft_expr *expr)
85 const struct nft_redir *priv = nft_expr_priv(expr); 88 const struct nft_redir *priv = nft_expr_priv(expr);
86 89
87 if (priv->sreg_proto_min) { 90 if (priv->sreg_proto_min) {
88 if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MIN, 91 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MIN,
89 htonl(priv->sreg_proto_min))) 92 priv->sreg_proto_min))
90 goto nla_put_failure; 93 goto nla_put_failure;
91 if (nla_put_be32(skb, NFTA_REDIR_REG_PROTO_MAX, 94 if (nft_dump_register(skb, NFTA_REDIR_REG_PROTO_MAX,
92 htonl(priv->sreg_proto_max))) 95 priv->sreg_proto_max))
93 goto nla_put_failure; 96 goto nla_put_failure;
94 } 97 }
95 98
diff --git a/net/netfilter/nft_reject_inet.c b/net/netfilter/nft_reject_inet.c
index 7b5f9d58680a..62cabee42fbe 100644
--- a/net/netfilter/nft_reject_inet.c
+++ b/net/netfilter/nft_reject_inet.c
@@ -18,7 +18,7 @@
18#include <net/netfilter/ipv6/nf_reject.h> 18#include <net/netfilter/ipv6/nf_reject.h>
19 19
20static void nft_reject_inet_eval(const struct nft_expr *expr, 20static void nft_reject_inet_eval(const struct nft_expr *expr,
21 struct nft_data data[NFT_REG_MAX + 1], 21 struct nft_regs *regs,
22 const struct nft_pktinfo *pkt) 22 const struct nft_pktinfo *pkt)
23{ 23{
24 struct nft_reject *priv = nft_expr_priv(expr); 24 struct nft_reject *priv = nft_expr_priv(expr);
@@ -28,14 +28,16 @@ static void nft_reject_inet_eval(const struct nft_expr *expr,
28 case NFPROTO_IPV4: 28 case NFPROTO_IPV4:
29 switch (priv->type) { 29 switch (priv->type) {
30 case NFT_REJECT_ICMP_UNREACH: 30 case NFT_REJECT_ICMP_UNREACH:
31 nf_send_unreach(pkt->skb, priv->icmp_code); 31 nf_send_unreach(pkt->skb, priv->icmp_code,
32 pkt->ops->hooknum);
32 break; 33 break;
33 case NFT_REJECT_TCP_RST: 34 case NFT_REJECT_TCP_RST:
34 nf_send_reset(pkt->skb, pkt->ops->hooknum); 35 nf_send_reset(pkt->skb, pkt->ops->hooknum);
35 break; 36 break;
36 case NFT_REJECT_ICMPX_UNREACH: 37 case NFT_REJECT_ICMPX_UNREACH:
37 nf_send_unreach(pkt->skb, 38 nf_send_unreach(pkt->skb,
38 nft_reject_icmp_code(priv->icmp_code)); 39 nft_reject_icmp_code(priv->icmp_code),
40 pkt->ops->hooknum);
39 break; 41 break;
40 } 42 }
41 break; 43 break;
@@ -56,7 +58,8 @@ static void nft_reject_inet_eval(const struct nft_expr *expr,
56 } 58 }
57 break; 59 break;
58 } 60 }
59 data[NFT_REG_VERDICT].verdict = NF_DROP; 61
62 regs->verdict.code = NF_DROP;
60} 63}
61 64
62static int nft_reject_inet_init(const struct nft_ctx *ctx, 65static int nft_reject_inet_init(const struct nft_ctx *ctx,
diff --git a/net/netfilter/xt_TPROXY.c b/net/netfilter/xt_TPROXY.c
index 50e1e5aaf4ce..cca96cec1b68 100644
--- a/net/netfilter/xt_TPROXY.c
+++ b/net/netfilter/xt_TPROXY.c
@@ -42,15 +42,21 @@ enum nf_tproxy_lookup_t {
42 42
43static bool tproxy_sk_is_transparent(struct sock *sk) 43static bool tproxy_sk_is_transparent(struct sock *sk)
44{ 44{
45 if (sk->sk_state != TCP_TIME_WAIT) { 45 switch (sk->sk_state) {
46 if (inet_sk(sk)->transparent) 46 case TCP_TIME_WAIT:
47 return true;
48 sock_put(sk);
49 } else {
50 if (inet_twsk(sk)->tw_transparent) 47 if (inet_twsk(sk)->tw_transparent)
51 return true; 48 return true;
52 inet_twsk_put(inet_twsk(sk)); 49 break;
50 case TCP_NEW_SYN_RECV:
51 if (inet_rsk(inet_reqsk(sk))->no_srccheck)
52 return true;
53 break;
54 default:
55 if (inet_sk(sk)->transparent)
56 return true;
53 } 57 }
58
59 sock_gen_put(sk);
54 return false; 60 return false;
55} 61}
56 62
@@ -266,7 +272,7 @@ tproxy_handle_time_wait4(struct sk_buff *skb, __be32 laddr, __be16 lport,
266 hp->source, lport ? lport : hp->dest, 272 hp->source, lport ? lport : hp->dest,
267 skb->dev, NFT_LOOKUP_LISTENER); 273 skb->dev, NFT_LOOKUP_LISTENER);
268 if (sk2) { 274 if (sk2) {
269 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row); 275 inet_twsk_deschedule(inet_twsk(sk));
270 inet_twsk_put(inet_twsk(sk)); 276 inet_twsk_put(inet_twsk(sk));
271 sk = sk2; 277 sk = sk2;
272 } 278 }
@@ -431,7 +437,7 @@ tproxy_handle_time_wait6(struct sk_buff *skb, int tproto, int thoff,
431 tgi->lport ? tgi->lport : hp->dest, 437 tgi->lport ? tgi->lport : hp->dest,
432 skb->dev, NFT_LOOKUP_LISTENER); 438 skb->dev, NFT_LOOKUP_LISTENER);
433 if (sk2) { 439 if (sk2) {
434 inet_twsk_deschedule(inet_twsk(sk), &tcp_death_row); 440 inet_twsk_deschedule(inet_twsk(sk));
435 inet_twsk_put(inet_twsk(sk)); 441 inet_twsk_put(inet_twsk(sk));
436 sk = sk2; 442 sk = sk2;
437 } 443 }
diff --git a/net/netfilter/xt_cgroup.c b/net/netfilter/xt_cgroup.c
index 7198d660b4de..a1d126f29463 100644
--- a/net/netfilter/xt_cgroup.c
+++ b/net/netfilter/xt_cgroup.c
@@ -39,7 +39,7 @@ cgroup_mt(const struct sk_buff *skb, struct xt_action_param *par)
39{ 39{
40 const struct xt_cgroup_info *info = par->matchinfo; 40 const struct xt_cgroup_info *info = par->matchinfo;
41 41
42 if (skb->sk == NULL) 42 if (skb->sk == NULL || !sk_fullsock(skb->sk))
43 return false; 43 return false;
44 44
45 return (info->id == skb->sk->sk_classid) ^ info->invert; 45 return (info->id == skb->sk->sk_classid) ^ info->invert;
diff --git a/net/netfilter/xt_physdev.c b/net/netfilter/xt_physdev.c
index f440f57a452f..1caaccbc306c 100644
--- a/net/netfilter/xt_physdev.c
+++ b/net/netfilter/xt_physdev.c
@@ -25,16 +25,15 @@ MODULE_ALIAS("ip6t_physdev");
25static bool 25static bool
26physdev_mt(const struct sk_buff *skb, struct xt_action_param *par) 26physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
27{ 27{
28 static const char nulldevname[IFNAMSIZ] __attribute__((aligned(sizeof(long))));
29 const struct xt_physdev_info *info = par->matchinfo; 28 const struct xt_physdev_info *info = par->matchinfo;
29 const struct net_device *physdev;
30 unsigned long ret; 30 unsigned long ret;
31 const char *indev, *outdev; 31 const char *indev, *outdev;
32 const struct nf_bridge_info *nf_bridge;
33 32
34 /* Not a bridged IP packet or no info available yet: 33 /* Not a bridged IP packet or no info available yet:
35 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if 34 * LOCAL_OUT/mangle and LOCAL_OUT/nat don't know if
36 * the destination device will be a bridge. */ 35 * the destination device will be a bridge. */
37 if (!(nf_bridge = skb->nf_bridge)) { 36 if (!skb->nf_bridge) {
38 /* Return MATCH if the invert flags of the used options are on */ 37 /* Return MATCH if the invert flags of the used options are on */
39 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && 38 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
40 !(info->invert & XT_PHYSDEV_OP_BRIDGED)) 39 !(info->invert & XT_PHYSDEV_OP_BRIDGED))
@@ -54,31 +53,41 @@ physdev_mt(const struct sk_buff *skb, struct xt_action_param *par)
54 return true; 53 return true;
55 } 54 }
56 55
56 physdev = nf_bridge_get_physoutdev(skb);
57 outdev = physdev ? physdev->name : NULL;
58
57 /* This only makes sense in the FORWARD and POSTROUTING chains */ 59 /* This only makes sense in the FORWARD and POSTROUTING chains */
58 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) && 60 if ((info->bitmask & XT_PHYSDEV_OP_BRIDGED) &&
59 (!!(nf_bridge->mask & BRNF_BRIDGED) ^ 61 (!!outdev ^ !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
60 !(info->invert & XT_PHYSDEV_OP_BRIDGED)))
61 return false; 62 return false;
62 63
64 physdev = nf_bridge_get_physindev(skb);
65 indev = physdev ? physdev->name : NULL;
66
63 if ((info->bitmask & XT_PHYSDEV_OP_ISIN && 67 if ((info->bitmask & XT_PHYSDEV_OP_ISIN &&
64 (!nf_bridge->physindev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) || 68 (!indev ^ !!(info->invert & XT_PHYSDEV_OP_ISIN))) ||
65 (info->bitmask & XT_PHYSDEV_OP_ISOUT && 69 (info->bitmask & XT_PHYSDEV_OP_ISOUT &&
66 (!nf_bridge->physoutdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT)))) 70 (!outdev ^ !!(info->invert & XT_PHYSDEV_OP_ISOUT))))
67 return false; 71 return false;
68 72
69 if (!(info->bitmask & XT_PHYSDEV_OP_IN)) 73 if (!(info->bitmask & XT_PHYSDEV_OP_IN))
70 goto match_outdev; 74 goto match_outdev;
71 indev = nf_bridge->physindev ? nf_bridge->physindev->name : nulldevname;
72 ret = ifname_compare_aligned(indev, info->physindev, info->in_mask);
73 75
74 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN)) 76 if (indev) {
75 return false; 77 ret = ifname_compare_aligned(indev, info->physindev,
78 info->in_mask);
79
80 if (!ret ^ !(info->invert & XT_PHYSDEV_OP_IN))
81 return false;
82 }
76 83
77match_outdev: 84match_outdev:
78 if (!(info->bitmask & XT_PHYSDEV_OP_OUT)) 85 if (!(info->bitmask & XT_PHYSDEV_OP_OUT))
79 return true; 86 return true;
80 outdev = nf_bridge->physoutdev ? 87
81 nf_bridge->physoutdev->name : nulldevname; 88 if (!outdev)
89 return false;
90
82 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask); 91 ret = ifname_compare_aligned(outdev, info->physoutdev, info->out_mask);
83 92
84 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT)); 93 return (!!ret ^ !(info->invert & XT_PHYSDEV_OP_OUT));
diff --git a/net/netfilter/xt_set.c b/net/netfilter/xt_set.c
index 0d47afea9682..89045982ec94 100644
--- a/net/netfilter/xt_set.c
+++ b/net/netfilter/xt_set.c
@@ -193,7 +193,7 @@ set_match_v3(const struct sk_buff *skb, struct xt_action_param *par)
193 return ret; 193 return ret;
194 194
195 if (!match_counter0(opt.ext.packets, &info->packets)) 195 if (!match_counter0(opt.ext.packets, &info->packets))
196 return 0; 196 return false;
197 return match_counter0(opt.ext.bytes, &info->bytes); 197 return match_counter0(opt.ext.bytes, &info->bytes);
198} 198}
199 199
@@ -239,7 +239,7 @@ set_match_v4(const struct sk_buff *skb, struct xt_action_param *par)
239 return ret; 239 return ret;
240 240
241 if (!match_counter(opt.ext.packets, &info->packets)) 241 if (!match_counter(opt.ext.packets, &info->packets))
242 return 0; 242 return false;
243 return match_counter(opt.ext.bytes, &info->bytes); 243 return match_counter(opt.ext.bytes, &info->bytes);
244} 244}
245 245
diff --git a/net/netfilter/xt_socket.c b/net/netfilter/xt_socket.c
index 13332dbf291d..e092cb046326 100644
--- a/net/netfilter/xt_socket.c
+++ b/net/netfilter/xt_socket.c
@@ -129,13 +129,24 @@ xt_socket_get_sock_v4(struct net *net, const u8 protocol,
129 return NULL; 129 return NULL;
130} 130}
131 131
132static bool 132static bool xt_socket_sk_is_transparent(struct sock *sk)
133socket_match(const struct sk_buff *skb, struct xt_action_param *par, 133{
134 const struct xt_socket_mtinfo1 *info) 134 switch (sk->sk_state) {
135 case TCP_TIME_WAIT:
136 return inet_twsk(sk)->tw_transparent;
137
138 case TCP_NEW_SYN_RECV:
139 return inet_rsk(inet_reqsk(sk))->no_srccheck;
140
141 default:
142 return inet_sk(sk)->transparent;
143 }
144}
145
146static struct sock *xt_socket_lookup_slow_v4(const struct sk_buff *skb,
147 const struct net_device *indev)
135{ 148{
136 const struct iphdr *iph = ip_hdr(skb); 149 const struct iphdr *iph = ip_hdr(skb);
137 struct udphdr _hdr, *hp = NULL;
138 struct sock *sk = skb->sk;
139 __be32 uninitialized_var(daddr), uninitialized_var(saddr); 150 __be32 uninitialized_var(daddr), uninitialized_var(saddr);
140 __be16 uninitialized_var(dport), uninitialized_var(sport); 151 __be16 uninitialized_var(dport), uninitialized_var(sport);
141 u8 uninitialized_var(protocol); 152 u8 uninitialized_var(protocol);
@@ -145,10 +156,12 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
145#endif 156#endif
146 157
147 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) { 158 if (iph->protocol == IPPROTO_UDP || iph->protocol == IPPROTO_TCP) {
159 struct udphdr _hdr, *hp;
160
148 hp = skb_header_pointer(skb, ip_hdrlen(skb), 161 hp = skb_header_pointer(skb, ip_hdrlen(skb),
149 sizeof(_hdr), &_hdr); 162 sizeof(_hdr), &_hdr);
150 if (hp == NULL) 163 if (hp == NULL)
151 return false; 164 return NULL;
152 165
153 protocol = iph->protocol; 166 protocol = iph->protocol;
154 saddr = iph->saddr; 167 saddr = iph->saddr;
@@ -158,16 +171,17 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
158 171
159 } else if (iph->protocol == IPPROTO_ICMP) { 172 } else if (iph->protocol == IPPROTO_ICMP) {
160 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr, 173 if (extract_icmp4_fields(skb, &protocol, &saddr, &daddr,
161 &sport, &dport)) 174 &sport, &dport))
162 return false; 175 return NULL;
163 } else { 176 } else {
164 return false; 177 return NULL;
165 } 178 }
166 179
167#ifdef XT_SOCKET_HAVE_CONNTRACK 180#ifdef XT_SOCKET_HAVE_CONNTRACK
168 /* Do the lookup with the original socket address in case this is a 181 /* Do the lookup with the original socket address in
169 * reply packet of an established SNAT-ted connection. */ 182 * case this is a reply packet of an established
170 183 * SNAT-ted connection.
184 */
171 ct = nf_ct_get(skb, &ctinfo); 185 ct = nf_ct_get(skb, &ctinfo);
172 if (ct && !nf_ct_is_untracked(ct) && 186 if (ct && !nf_ct_is_untracked(ct) &&
173 ((iph->protocol != IPPROTO_ICMP && 187 ((iph->protocol != IPPROTO_ICMP &&
@@ -183,10 +197,18 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
183 } 197 }
184#endif 198#endif
185 199
200 return xt_socket_get_sock_v4(dev_net(skb->dev), protocol, saddr, daddr,
201 sport, dport, indev);
202}
203
204static bool
205socket_match(const struct sk_buff *skb, struct xt_action_param *par,
206 const struct xt_socket_mtinfo1 *info)
207{
208 struct sock *sk = skb->sk;
209
186 if (!sk) 210 if (!sk)
187 sk = xt_socket_get_sock_v4(dev_net(skb->dev), protocol, 211 sk = xt_socket_lookup_slow_v4(skb, par->in);
188 saddr, daddr, sport, dport,
189 par->in);
190 if (sk) { 212 if (sk) {
191 bool wildcard; 213 bool wildcard;
192 bool transparent = true; 214 bool transparent = true;
@@ -195,16 +217,14 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
195 * unless XT_SOCKET_NOWILDCARD is set 217 * unless XT_SOCKET_NOWILDCARD is set
196 */ 218 */
197 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) && 219 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
198 sk->sk_state != TCP_TIME_WAIT && 220 sk_fullsock(sk) &&
199 inet_sk(sk)->inet_rcv_saddr == 0); 221 inet_sk(sk)->inet_rcv_saddr == 0);
200 222
201 /* Ignore non-transparent sockets, 223 /* Ignore non-transparent sockets,
202 if XT_SOCKET_TRANSPARENT is used */ 224 * if XT_SOCKET_TRANSPARENT is used
225 */
203 if (info->flags & XT_SOCKET_TRANSPARENT) 226 if (info->flags & XT_SOCKET_TRANSPARENT)
204 transparent = ((sk->sk_state != TCP_TIME_WAIT && 227 transparent = xt_socket_sk_is_transparent(sk);
205 inet_sk(sk)->transparent) ||
206 (sk->sk_state == TCP_TIME_WAIT &&
207 inet_twsk(sk)->tw_transparent));
208 228
209 if (sk != skb->sk) 229 if (sk != skb->sk)
210 sock_gen_put(sk); 230 sock_gen_put(sk);
@@ -213,12 +233,7 @@ socket_match(const struct sk_buff *skb, struct xt_action_param *par,
213 sk = NULL; 233 sk = NULL;
214 } 234 }
215 235
216 pr_debug("proto %hhu %pI4:%hu -> %pI4:%hu (orig %pI4:%hu) sock %p\n", 236 return sk != NULL;
217 protocol, &saddr, ntohs(sport),
218 &daddr, ntohs(dport),
219 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
220
221 return (sk != NULL);
222} 237}
223 238
224static bool 239static bool
@@ -315,28 +330,26 @@ xt_socket_get_sock_v6(struct net *net, const u8 protocol,
315 return NULL; 330 return NULL;
316} 331}
317 332
318static bool 333static struct sock *xt_socket_lookup_slow_v6(const struct sk_buff *skb,
319socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par) 334 const struct net_device *indev)
320{ 335{
321 struct ipv6hdr ipv6_var, *iph = ipv6_hdr(skb);
322 struct udphdr _hdr, *hp = NULL;
323 struct sock *sk = skb->sk;
324 const struct in6_addr *daddr = NULL, *saddr = NULL;
325 __be16 uninitialized_var(dport), uninitialized_var(sport); 336 __be16 uninitialized_var(dport), uninitialized_var(sport);
326 int thoff = 0, uninitialized_var(tproto); 337 const struct in6_addr *daddr = NULL, *saddr = NULL;
327 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo; 338 struct ipv6hdr *iph = ipv6_hdr(skb);
339 int thoff = 0, tproto;
328 340
329 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL); 341 tproto = ipv6_find_hdr(skb, &thoff, -1, NULL, NULL);
330 if (tproto < 0) { 342 if (tproto < 0) {
331 pr_debug("unable to find transport header in IPv6 packet, dropping\n"); 343 pr_debug("unable to find transport header in IPv6 packet, dropping\n");
332 return NF_DROP; 344 return NULL;
333 } 345 }
334 346
335 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) { 347 if (tproto == IPPROTO_UDP || tproto == IPPROTO_TCP) {
336 hp = skb_header_pointer(skb, thoff, 348 struct udphdr _hdr, *hp;
337 sizeof(_hdr), &_hdr); 349
350 hp = skb_header_pointer(skb, thoff, sizeof(_hdr), &_hdr);
338 if (hp == NULL) 351 if (hp == NULL)
339 return false; 352 return NULL;
340 353
341 saddr = &iph->saddr; 354 saddr = &iph->saddr;
342 sport = hp->source; 355 sport = hp->source;
@@ -344,17 +357,27 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
344 dport = hp->dest; 357 dport = hp->dest;
345 358
346 } else if (tproto == IPPROTO_ICMPV6) { 359 } else if (tproto == IPPROTO_ICMPV6) {
360 struct ipv6hdr ipv6_var;
361
347 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr, 362 if (extract_icmp6_fields(skb, thoff, &tproto, &saddr, &daddr,
348 &sport, &dport, &ipv6_var)) 363 &sport, &dport, &ipv6_var))
349 return false; 364 return NULL;
350 } else { 365 } else {
351 return false; 366 return NULL;
352 } 367 }
353 368
369 return xt_socket_get_sock_v6(dev_net(skb->dev), tproto, saddr, daddr,
370 sport, dport, indev);
371}
372
373static bool
374socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
375{
376 const struct xt_socket_mtinfo1 *info = (struct xt_socket_mtinfo1 *) par->matchinfo;
377 struct sock *sk = skb->sk;
378
354 if (!sk) 379 if (!sk)
355 sk = xt_socket_get_sock_v6(dev_net(skb->dev), tproto, 380 sk = xt_socket_lookup_slow_v6(skb, par->in);
356 saddr, daddr, sport, dport,
357 par->in);
358 if (sk) { 381 if (sk) {
359 bool wildcard; 382 bool wildcard;
360 bool transparent = true; 383 bool transparent = true;
@@ -363,16 +386,14 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
363 * unless XT_SOCKET_NOWILDCARD is set 386 * unless XT_SOCKET_NOWILDCARD is set
364 */ 387 */
365 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) && 388 wildcard = (!(info->flags & XT_SOCKET_NOWILDCARD) &&
366 sk->sk_state != TCP_TIME_WAIT && 389 sk_fullsock(sk) &&
367 ipv6_addr_any(&sk->sk_v6_rcv_saddr)); 390 ipv6_addr_any(&sk->sk_v6_rcv_saddr));
368 391
369 /* Ignore non-transparent sockets, 392 /* Ignore non-transparent sockets,
370 if XT_SOCKET_TRANSPARENT is used */ 393 * if XT_SOCKET_TRANSPARENT is used
394 */
371 if (info->flags & XT_SOCKET_TRANSPARENT) 395 if (info->flags & XT_SOCKET_TRANSPARENT)
372 transparent = ((sk->sk_state != TCP_TIME_WAIT && 396 transparent = xt_socket_sk_is_transparent(sk);
373 inet_sk(sk)->transparent) ||
374 (sk->sk_state == TCP_TIME_WAIT &&
375 inet_twsk(sk)->tw_transparent));
376 397
377 if (sk != skb->sk) 398 if (sk != skb->sk)
378 sock_gen_put(sk); 399 sock_gen_put(sk);
@@ -381,13 +402,7 @@ socket_mt6_v1_v2(const struct sk_buff *skb, struct xt_action_param *par)
381 sk = NULL; 402 sk = NULL;
382 } 403 }
383 404
384 pr_debug("proto %hhd %pI6:%hu -> %pI6:%hu " 405 return sk != NULL;
385 "(orig %pI6:%hu) sock %p\n",
386 tproto, saddr, ntohs(sport),
387 daddr, ntohs(dport),
388 &iph->daddr, hp ? ntohs(hp->dest) : 0, sk);
389
390 return (sk != NULL);
391} 406}
392#endif 407#endif
393 408
diff --git a/net/netfilter/xt_string.c b/net/netfilter/xt_string.c
index 5699adb97652..0bc3460319c8 100644
--- a/net/netfilter/xt_string.c
+++ b/net/netfilter/xt_string.c
@@ -26,13 +26,12 @@ static bool
26string_mt(const struct sk_buff *skb, struct xt_action_param *par) 26string_mt(const struct sk_buff *skb, struct xt_action_param *par)
27{ 27{
28 const struct xt_string_info *conf = par->matchinfo; 28 const struct xt_string_info *conf = par->matchinfo;
29 struct ts_state state;
30 bool invert; 29 bool invert;
31 30
32 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT; 31 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
33 32
34 return (skb_find_text((struct sk_buff *)skb, conf->from_offset, 33 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
35 conf->to_offset, conf->config, &state) 34 conf->to_offset, conf->config)
36 != UINT_MAX) ^ invert; 35 != UINT_MAX) ^ invert;
37} 36}
38 37
diff --git a/net/netlabel/netlabel_mgmt.c b/net/netlabel/netlabel_mgmt.c
index 70440748fe5c..13f777f20995 100644
--- a/net/netlabel/netlabel_mgmt.c
+++ b/net/netlabel/netlabel_mgmt.c
@@ -293,15 +293,13 @@ static int netlbl_mgmt_listentry(struct sk_buff *skb,
293 return -ENOMEM; 293 return -ENOMEM;
294 294
295 addr_struct.s_addr = iter4->addr; 295 addr_struct.s_addr = iter4->addr;
296 ret_val = nla_put(skb, NLBL_MGMT_A_IPV4ADDR, 296 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4ADDR,
297 sizeof(struct in_addr), 297 addr_struct.s_addr);
298 &addr_struct);
299 if (ret_val != 0) 298 if (ret_val != 0)
300 return ret_val; 299 return ret_val;
301 addr_struct.s_addr = iter4->mask; 300 addr_struct.s_addr = iter4->mask;
302 ret_val = nla_put(skb, NLBL_MGMT_A_IPV4MASK, 301 ret_val = nla_put_in_addr(skb, NLBL_MGMT_A_IPV4MASK,
303 sizeof(struct in_addr), 302 addr_struct.s_addr);
304 &addr_struct);
305 if (ret_val != 0) 303 if (ret_val != 0)
306 return ret_val; 304 return ret_val;
307 map4 = netlbl_domhsh_addr4_entry(iter4); 305 map4 = netlbl_domhsh_addr4_entry(iter4);
@@ -328,14 +326,12 @@ static int netlbl_mgmt_listentry(struct sk_buff *skb,
328 if (nla_b == NULL) 326 if (nla_b == NULL)
329 return -ENOMEM; 327 return -ENOMEM;
330 328
331 ret_val = nla_put(skb, NLBL_MGMT_A_IPV6ADDR, 329 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6ADDR,
332 sizeof(struct in6_addr), 330 &iter6->addr);
333 &iter6->addr);
334 if (ret_val != 0) 331 if (ret_val != 0)
335 return ret_val; 332 return ret_val;
336 ret_val = nla_put(skb, NLBL_MGMT_A_IPV6MASK, 333 ret_val = nla_put_in6_addr(skb, NLBL_MGMT_A_IPV6MASK,
337 sizeof(struct in6_addr), 334 &iter6->mask);
338 &iter6->mask);
339 if (ret_val != 0) 335 if (ret_val != 0)
340 return ret_val; 336 return ret_val;
341 map6 = netlbl_domhsh_addr6_entry(iter6); 337 map6 = netlbl_domhsh_addr6_entry(iter6);
diff --git a/net/netlabel/netlabel_unlabeled.c b/net/netlabel/netlabel_unlabeled.c
index aec7994f78cf..b0380927f05f 100644
--- a/net/netlabel/netlabel_unlabeled.c
+++ b/net/netlabel/netlabel_unlabeled.c
@@ -1117,34 +1117,30 @@ static int netlbl_unlabel_staticlist_gen(u32 cmd,
1117 struct in_addr addr_struct; 1117 struct in_addr addr_struct;
1118 1118
1119 addr_struct.s_addr = addr4->list.addr; 1119 addr_struct.s_addr = addr4->list.addr;
1120 ret_val = nla_put(cb_arg->skb, 1120 ret_val = nla_put_in_addr(cb_arg->skb,
1121 NLBL_UNLABEL_A_IPV4ADDR, 1121 NLBL_UNLABEL_A_IPV4ADDR,
1122 sizeof(struct in_addr), 1122 addr_struct.s_addr);
1123 &addr_struct);
1124 if (ret_val != 0) 1123 if (ret_val != 0)
1125 goto list_cb_failure; 1124 goto list_cb_failure;
1126 1125
1127 addr_struct.s_addr = addr4->list.mask; 1126 addr_struct.s_addr = addr4->list.mask;
1128 ret_val = nla_put(cb_arg->skb, 1127 ret_val = nla_put_in_addr(cb_arg->skb,
1129 NLBL_UNLABEL_A_IPV4MASK, 1128 NLBL_UNLABEL_A_IPV4MASK,
1130 sizeof(struct in_addr), 1129 addr_struct.s_addr);
1131 &addr_struct);
1132 if (ret_val != 0) 1130 if (ret_val != 0)
1133 goto list_cb_failure; 1131 goto list_cb_failure;
1134 1132
1135 secid = addr4->secid; 1133 secid = addr4->secid;
1136 } else { 1134 } else {
1137 ret_val = nla_put(cb_arg->skb, 1135 ret_val = nla_put_in6_addr(cb_arg->skb,
1138 NLBL_UNLABEL_A_IPV6ADDR, 1136 NLBL_UNLABEL_A_IPV6ADDR,
1139 sizeof(struct in6_addr), 1137 &addr6->list.addr);
1140 &addr6->list.addr);
1141 if (ret_val != 0) 1138 if (ret_val != 0)
1142 goto list_cb_failure; 1139 goto list_cb_failure;
1143 1140
1144 ret_val = nla_put(cb_arg->skb, 1141 ret_val = nla_put_in6_addr(cb_arg->skb,
1145 NLBL_UNLABEL_A_IPV6MASK, 1142 NLBL_UNLABEL_A_IPV6MASK,
1146 sizeof(struct in6_addr), 1143 &addr6->list.mask);
1147 &addr6->list.mask);
1148 if (ret_val != 0) 1144 if (ret_val != 0)
1149 goto list_cb_failure; 1145 goto list_cb_failure;
1150 1146
diff --git a/net/netlink/af_netlink.c b/net/netlink/af_netlink.c
index 05919bf3f670..19909d0786a2 100644
--- a/net/netlink/af_netlink.c
+++ b/net/netlink/af_netlink.c
@@ -116,6 +116,8 @@ static ATOMIC_NOTIFIER_HEAD(netlink_chain);
116static DEFINE_SPINLOCK(netlink_tap_lock); 116static DEFINE_SPINLOCK(netlink_tap_lock);
117static struct list_head netlink_tap_all __read_mostly; 117static struct list_head netlink_tap_all __read_mostly;
118 118
119static const struct rhashtable_params netlink_rhashtable_params;
120
119static inline u32 netlink_group_mask(u32 group) 121static inline u32 netlink_group_mask(u32 group)
120{ 122{
121 return group ? 1 << (group - 1) : 0; 123 return group ? 1 << (group - 1) : 0;
@@ -970,41 +972,50 @@ netlink_unlock_table(void)
970 972
971struct netlink_compare_arg 973struct netlink_compare_arg
972{ 974{
973 struct net *net; 975 possible_net_t pnet;
974 u32 portid; 976 u32 portid;
975}; 977};
976 978
977static bool netlink_compare(void *ptr, void *arg) 979/* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
980#define netlink_compare_arg_len \
981 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
982
983static inline int netlink_compare(struct rhashtable_compare_arg *arg,
984 const void *ptr)
978{ 985{
979 struct netlink_compare_arg *x = arg; 986 const struct netlink_compare_arg *x = arg->key;
980 struct sock *sk = ptr; 987 const struct netlink_sock *nlk = ptr;
981 988
982 return nlk_sk(sk)->portid == x->portid && 989 return nlk->portid != x->portid ||
983 net_eq(sock_net(sk), x->net); 990 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
991}
992
993static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
994 struct net *net, u32 portid)
995{
996 memset(arg, 0, sizeof(*arg));
997 write_pnet(&arg->pnet, net);
998 arg->portid = portid;
984} 999}
985 1000
986static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid, 1001static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
987 struct net *net) 1002 struct net *net)
988{ 1003{
989 struct netlink_compare_arg arg = { 1004 struct netlink_compare_arg arg;
990 .net = net,
991 .portid = portid,
992 };
993 1005
994 return rhashtable_lookup_compare(&table->hash, &portid, 1006 netlink_compare_arg_init(&arg, net, portid);
995 &netlink_compare, &arg); 1007 return rhashtable_lookup_fast(&table->hash, &arg,
1008 netlink_rhashtable_params);
996} 1009}
997 1010
998static bool __netlink_insert(struct netlink_table *table, struct sock *sk) 1011static int __netlink_insert(struct netlink_table *table, struct sock *sk)
999{ 1012{
1000 struct netlink_compare_arg arg = { 1013 struct netlink_compare_arg arg;
1001 .net = sock_net(sk),
1002 .portid = nlk_sk(sk)->portid,
1003 };
1004 1014
1005 return rhashtable_lookup_compare_insert(&table->hash, 1015 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
1006 &nlk_sk(sk)->node, 1016 return rhashtable_lookup_insert_key(&table->hash, &arg,
1007 &netlink_compare, &arg); 1017 &nlk_sk(sk)->node,
1018 netlink_rhashtable_params);
1008} 1019}
1009 1020
1010static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid) 1021static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
@@ -1066,9 +1077,10 @@ static int netlink_insert(struct sock *sk, u32 portid)
1066 nlk_sk(sk)->portid = portid; 1077 nlk_sk(sk)->portid = portid;
1067 sock_hold(sk); 1078 sock_hold(sk);
1068 1079
1069 err = 0; 1080 err = __netlink_insert(table, sk);
1070 if (!__netlink_insert(table, sk)) { 1081 if (err) {
1071 err = -EADDRINUSE; 1082 if (err == -EEXIST)
1083 err = -EADDRINUSE;
1072 sock_put(sk); 1084 sock_put(sk);
1073 } 1085 }
1074 1086
@@ -1082,7 +1094,8 @@ static void netlink_remove(struct sock *sk)
1082 struct netlink_table *table; 1094 struct netlink_table *table;
1083 1095
1084 table = &nl_table[sk->sk_protocol]; 1096 table = &nl_table[sk->sk_protocol];
1085 if (rhashtable_remove(&table->hash, &nlk_sk(sk)->node)) { 1097 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
1098 netlink_rhashtable_params)) {
1086 WARN_ON(atomic_read(&sk->sk_refcnt) == 1); 1099 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
1087 __sock_put(sk); 1100 __sock_put(sk);
1088 } 1101 }
@@ -2256,8 +2269,7 @@ static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2256 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); 2269 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2257} 2270}
2258 2271
2259static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, 2272static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2260 struct msghdr *msg, size_t len)
2261{ 2273{
2262 struct sock *sk = sock->sk; 2274 struct sock *sk = sock->sk;
2263 struct netlink_sock *nlk = nlk_sk(sk); 2275 struct netlink_sock *nlk = nlk_sk(sk);
@@ -2346,8 +2358,7 @@ out:
2346 return err; 2358 return err;
2347} 2359}
2348 2360
2349static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, 2361static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2350 struct msghdr *msg, size_t len,
2351 int flags) 2362 int flags)
2352{ 2363{
2353 struct scm_cookie scm; 2364 struct scm_cookie scm;
@@ -3116,17 +3127,28 @@ static struct pernet_operations __net_initdata netlink_net_ops = {
3116 .exit = netlink_net_exit, 3127 .exit = netlink_net_exit,
3117}; 3128};
3118 3129
3130static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
3131{
3132 const struct netlink_sock *nlk = data;
3133 struct netlink_compare_arg arg;
3134
3135 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
3136 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
3137}
3138
3139static const struct rhashtable_params netlink_rhashtable_params = {
3140 .head_offset = offsetof(struct netlink_sock, node),
3141 .key_len = netlink_compare_arg_len,
3142 .obj_hashfn = netlink_hash,
3143 .obj_cmpfn = netlink_compare,
3144 .max_size = 65536,
3145 .automatic_shrinking = true,
3146};
3147
3119static int __init netlink_proto_init(void) 3148static int __init netlink_proto_init(void)
3120{ 3149{
3121 int i; 3150 int i;
3122 int err = proto_register(&netlink_proto, 0); 3151 int err = proto_register(&netlink_proto, 0);
3123 struct rhashtable_params ht_params = {
3124 .head_offset = offsetof(struct netlink_sock, node),
3125 .key_offset = offsetof(struct netlink_sock, portid),
3126 .key_len = sizeof(u32), /* portid */
3127 .hashfn = jhash,
3128 .max_shift = 16, /* 64K */
3129 };
3130 3152
3131 if (err != 0) 3153 if (err != 0)
3132 goto out; 3154 goto out;
@@ -3138,7 +3160,8 @@ static int __init netlink_proto_init(void)
3138 goto panic; 3160 goto panic;
3139 3161
3140 for (i = 0; i < MAX_LINKS; i++) { 3162 for (i = 0; i < MAX_LINKS; i++) {
3141 if (rhashtable_init(&nl_table[i].hash, &ht_params) < 0) { 3163 if (rhashtable_init(&nl_table[i].hash,
3164 &netlink_rhashtable_params) < 0) {
3142 while (--i > 0) 3165 while (--i > 0)
3143 rhashtable_destroy(&nl_table[i].hash); 3166 rhashtable_destroy(&nl_table[i].hash);
3144 kfree(nl_table); 3167 kfree(nl_table);
diff --git a/net/netrom/af_netrom.c b/net/netrom/af_netrom.c
index 69f1d5e9959f..b987fd56c3c5 100644
--- a/net/netrom/af_netrom.c
+++ b/net/netrom/af_netrom.c
@@ -1023,8 +1023,7 @@ int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
1023 return 1; 1023 return 1;
1024} 1024}
1025 1025
1026static int nr_sendmsg(struct kiocb *iocb, struct socket *sock, 1026static int nr_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1027 struct msghdr *msg, size_t len)
1028{ 1027{
1029 struct sock *sk = sock->sk; 1028 struct sock *sk = sock->sk;
1030 struct nr_sock *nr = nr_sk(sk); 1029 struct nr_sock *nr = nr_sk(sk);
@@ -1133,8 +1132,8 @@ out:
1133 return err; 1132 return err;
1134} 1133}
1135 1134
1136static int nr_recvmsg(struct kiocb *iocb, struct socket *sock, 1135static int nr_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1137 struct msghdr *msg, size_t size, int flags) 1136 int flags)
1138{ 1137{
1139 struct sock *sk = sock->sk; 1138 struct sock *sk = sock->sk;
1140 DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name); 1139 DECLARE_SOCKADDR(struct sockaddr_ax25 *, sax, msg->msg_name);
diff --git a/net/netrom/nr_dev.c b/net/netrom/nr_dev.c
index 6ae063cebf7d..988f542481a8 100644
--- a/net/netrom/nr_dev.c
+++ b/net/netrom/nr_dev.c
@@ -65,36 +65,6 @@ int nr_rx_ip(struct sk_buff *skb, struct net_device *dev)
65 return 1; 65 return 1;
66} 66}
67 67
68#ifdef CONFIG_INET
69
70static int nr_rebuild_header(struct sk_buff *skb)
71{
72 unsigned char *bp = skb->data;
73
74 if (arp_find(bp + 7, skb))
75 return 1;
76
77 bp[6] &= ~AX25_CBIT;
78 bp[6] &= ~AX25_EBIT;
79 bp[6] |= AX25_SSSID_SPARE;
80 bp += AX25_ADDR_LEN;
81
82 bp[6] &= ~AX25_CBIT;
83 bp[6] |= AX25_EBIT;
84 bp[6] |= AX25_SSSID_SPARE;
85
86 return 0;
87}
88
89#else
90
91static int nr_rebuild_header(struct sk_buff *skb)
92{
93 return 1;
94}
95
96#endif
97
98static int nr_header(struct sk_buff *skb, struct net_device *dev, 68static int nr_header(struct sk_buff *skb, struct net_device *dev,
99 unsigned short type, 69 unsigned short type,
100 const void *daddr, const void *saddr, unsigned int len) 70 const void *daddr, const void *saddr, unsigned int len)
@@ -188,7 +158,6 @@ static netdev_tx_t nr_xmit(struct sk_buff *skb, struct net_device *dev)
188 158
189static const struct header_ops nr_header_ops = { 159static const struct header_ops nr_header_ops = {
190 .create = nr_header, 160 .create = nr_header,
191 .rebuild= nr_rebuild_header,
192}; 161};
193 162
194static const struct net_device_ops nr_netdev_ops = { 163static const struct net_device_ops nr_netdev_ops = {
diff --git a/net/nfc/llcp_sock.c b/net/nfc/llcp_sock.c
index e181e290427c..9578bd6a4f3e 100644
--- a/net/nfc/llcp_sock.c
+++ b/net/nfc/llcp_sock.c
@@ -750,8 +750,8 @@ error:
750 return ret; 750 return ret;
751} 751}
752 752
753static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock, 753static int llcp_sock_sendmsg(struct socket *sock, struct msghdr *msg,
754 struct msghdr *msg, size_t len) 754 size_t len)
755{ 755{
756 struct sock *sk = sock->sk; 756 struct sock *sk = sock->sk;
757 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk); 757 struct nfc_llcp_sock *llcp_sock = nfc_llcp_sock(sk);
@@ -793,8 +793,8 @@ static int llcp_sock_sendmsg(struct kiocb *iocb, struct socket *sock,
793 return nfc_llcp_send_i_frame(llcp_sock, msg, len); 793 return nfc_llcp_send_i_frame(llcp_sock, msg, len);
794} 794}
795 795
796static int llcp_sock_recvmsg(struct kiocb *iocb, struct socket *sock, 796static int llcp_sock_recvmsg(struct socket *sock, struct msghdr *msg,
797 struct msghdr *msg, size_t len, int flags) 797 size_t len, int flags)
798{ 798{
799 int noblock = flags & MSG_DONTWAIT; 799 int noblock = flags & MSG_DONTWAIT;
800 struct sock *sk = sock->sk; 800 struct sock *sk = sock->sk;
diff --git a/net/nfc/nci/core.c b/net/nfc/nci/core.c
index 9575a1892607..49ff32106080 100644
--- a/net/nfc/nci/core.c
+++ b/net/nfc/nci/core.c
@@ -907,6 +907,16 @@ static int nci_se_io(struct nfc_dev *nfc_dev, u32 se_idx,
907 return 0; 907 return 0;
908} 908}
909 909
910static int nci_fw_download(struct nfc_dev *nfc_dev, const char *firmware_name)
911{
912 struct nci_dev *ndev = nfc_get_drvdata(nfc_dev);
913
914 if (!ndev->ops->fw_download)
915 return -ENOTSUPP;
916
917 return ndev->ops->fw_download(ndev, firmware_name);
918}
919
910static struct nfc_ops nci_nfc_ops = { 920static struct nfc_ops nci_nfc_ops = {
911 .dev_up = nci_dev_up, 921 .dev_up = nci_dev_up,
912 .dev_down = nci_dev_down, 922 .dev_down = nci_dev_down,
@@ -922,6 +932,7 @@ static struct nfc_ops nci_nfc_ops = {
922 .disable_se = nci_disable_se, 932 .disable_se = nci_disable_se,
923 .discover_se = nci_discover_se, 933 .discover_se = nci_discover_se,
924 .se_io = nci_se_io, 934 .se_io = nci_se_io,
935 .fw_download = nci_fw_download,
925}; 936};
926 937
927/* ---- Interface to NCI drivers ---- */ 938/* ---- Interface to NCI drivers ---- */
diff --git a/net/nfc/netlink.c b/net/nfc/netlink.c
index 14a2d11581da..3763036710ae 100644
--- a/net/nfc/netlink.c
+++ b/net/nfc/netlink.c
@@ -1584,7 +1584,7 @@ static const struct genl_ops nfc_genl_ops[] = {
1584 1584
1585struct urelease_work { 1585struct urelease_work {
1586 struct work_struct w; 1586 struct work_struct w;
1587 int portid; 1587 u32 portid;
1588}; 1588};
1589 1589
1590static void nfc_urelease_event_work(struct work_struct *work) 1590static void nfc_urelease_event_work(struct work_struct *work)
diff --git a/net/nfc/rawsock.c b/net/nfc/rawsock.c
index 373e138c0ab6..82b4e8024778 100644
--- a/net/nfc/rawsock.c
+++ b/net/nfc/rawsock.c
@@ -211,8 +211,7 @@ static void rawsock_tx_work(struct work_struct *work)
211 } 211 }
212} 212}
213 213
214static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock, 214static int rawsock_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
215 struct msghdr *msg, size_t len)
216{ 215{
217 struct sock *sk = sock->sk; 216 struct sock *sk = sock->sk;
218 struct nfc_dev *dev = nfc_rawsock(sk)->dev; 217 struct nfc_dev *dev = nfc_rawsock(sk)->dev;
@@ -248,8 +247,8 @@ static int rawsock_sendmsg(struct kiocb *iocb, struct socket *sock,
248 return len; 247 return len;
249} 248}
250 249
251static int rawsock_recvmsg(struct kiocb *iocb, struct socket *sock, 250static int rawsock_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
252 struct msghdr *msg, size_t len, int flags) 251 int flags)
253{ 252{
254 int noblock = flags & MSG_DONTWAIT; 253 int noblock = flags & MSG_DONTWAIT;
255 struct sock *sk = sock->sk; 254 struct sock *sk = sock->sk;
diff --git a/net/openvswitch/Kconfig b/net/openvswitch/Kconfig
index b7d818c59423..ed6b0f8dd1bb 100644
--- a/net/openvswitch/Kconfig
+++ b/net/openvswitch/Kconfig
@@ -6,6 +6,7 @@ config OPENVSWITCH
6 tristate "Open vSwitch" 6 tristate "Open vSwitch"
7 depends on INET 7 depends on INET
8 select LIBCRC32C 8 select LIBCRC32C
9 select MPLS
9 select NET_MPLS_GSO 10 select NET_MPLS_GSO
10 ---help--- 11 ---help---
11 Open vSwitch is a multilayer Ethernet switch targeted at virtualized 12 Open vSwitch is a multilayer Ethernet switch targeted at virtualized
diff --git a/net/openvswitch/datapath.c b/net/openvswitch/datapath.c
index 5bae7243c577..096c6276e6b9 100644
--- a/net/openvswitch/datapath.c
+++ b/net/openvswitch/datapath.c
@@ -203,7 +203,6 @@ static void destroy_dp_rcu(struct rcu_head *rcu)
203 203
204 ovs_flow_tbl_destroy(&dp->table); 204 ovs_flow_tbl_destroy(&dp->table);
205 free_percpu(dp->stats_percpu); 205 free_percpu(dp->stats_percpu);
206 release_net(ovs_dp_get_net(dp));
207 kfree(dp->ports); 206 kfree(dp->ports);
208 kfree(dp); 207 kfree(dp);
209} 208}
@@ -1501,7 +1500,7 @@ static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1501 if (dp == NULL) 1500 if (dp == NULL)
1502 goto err_free_reply; 1501 goto err_free_reply;
1503 1502
1504 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); 1503 ovs_dp_set_net(dp, sock_net(skb->sk));
1505 1504
1506 /* Allocate table. */ 1505 /* Allocate table. */
1507 err = ovs_flow_tbl_init(&dp->table); 1506 err = ovs_flow_tbl_init(&dp->table);
@@ -1575,7 +1574,6 @@ err_destroy_percpu:
1575err_destroy_table: 1574err_destroy_table:
1576 ovs_flow_tbl_destroy(&dp->table); 1575 ovs_flow_tbl_destroy(&dp->table);
1577err_free_dp: 1576err_free_dp:
1578 release_net(ovs_dp_get_net(dp));
1579 kfree(dp); 1577 kfree(dp);
1580err_free_reply: 1578err_free_reply:
1581 kfree_skb(reply); 1579 kfree_skb(reply);
diff --git a/net/openvswitch/datapath.h b/net/openvswitch/datapath.h
index 3ece94563079..4ec4a480b147 100644
--- a/net/openvswitch/datapath.h
+++ b/net/openvswitch/datapath.h
@@ -84,10 +84,8 @@ struct datapath {
84 /* Stats. */ 84 /* Stats. */
85 struct dp_stats_percpu __percpu *stats_percpu; 85 struct dp_stats_percpu __percpu *stats_percpu;
86 86
87#ifdef CONFIG_NET_NS
88 /* Network namespace ref. */ 87 /* Network namespace ref. */
89 struct net *net; 88 possible_net_t net;
90#endif
91 89
92 u32 user_features; 90 u32 user_features;
93}; 91};
diff --git a/net/openvswitch/flow.c b/net/openvswitch/flow.c
index 50ec42f170a0..2dacc7b5af23 100644
--- a/net/openvswitch/flow.c
+++ b/net/openvswitch/flow.c
@@ -100,7 +100,9 @@ void ovs_flow_stats_update(struct sw_flow *flow, __be16 tcp_flags,
100 100
101 new_stats = 101 new_stats =
102 kmem_cache_alloc_node(flow_stats_cache, 102 kmem_cache_alloc_node(flow_stats_cache,
103 GFP_THISNODE | 103 GFP_NOWAIT |
104 __GFP_THISNODE |
105 __GFP_NOWARN |
104 __GFP_NOMEMALLOC, 106 __GFP_NOMEMALLOC,
105 node); 107 node);
106 if (likely(new_stats)) { 108 if (likely(new_stats)) {
diff --git a/net/openvswitch/flow_netlink.c b/net/openvswitch/flow_netlink.c
index 22b18c145c92..c691b1a1eee0 100644
--- a/net/openvswitch/flow_netlink.c
+++ b/net/openvswitch/flow_netlink.c
@@ -535,11 +535,11 @@ static int ipv4_tun_from_nlattr(const struct nlattr *attr,
535 break; 535 break;
536 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC: 536 case OVS_TUNNEL_KEY_ATTR_IPV4_SRC:
537 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src, 537 SW_FLOW_KEY_PUT(match, tun_key.ipv4_src,
538 nla_get_be32(a), is_mask); 538 nla_get_in_addr(a), is_mask);
539 break; 539 break;
540 case OVS_TUNNEL_KEY_ATTR_IPV4_DST: 540 case OVS_TUNNEL_KEY_ATTR_IPV4_DST:
541 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst, 541 SW_FLOW_KEY_PUT(match, tun_key.ipv4_dst,
542 nla_get_be32(a), is_mask); 542 nla_get_in_addr(a), is_mask);
543 break; 543 break;
544 case OVS_TUNNEL_KEY_ATTR_TOS: 544 case OVS_TUNNEL_KEY_ATTR_TOS:
545 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos, 545 SW_FLOW_KEY_PUT(match, tun_key.ipv4_tos,
@@ -648,10 +648,12 @@ static int __ipv4_tun_to_nlattr(struct sk_buff *skb,
648 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id)) 648 nla_put_be64(skb, OVS_TUNNEL_KEY_ATTR_ID, output->tun_id))
649 return -EMSGSIZE; 649 return -EMSGSIZE;
650 if (output->ipv4_src && 650 if (output->ipv4_src &&
651 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC, output->ipv4_src)) 651 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_SRC,
652 output->ipv4_src))
652 return -EMSGSIZE; 653 return -EMSGSIZE;
653 if (output->ipv4_dst && 654 if (output->ipv4_dst &&
654 nla_put_be32(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST, output->ipv4_dst)) 655 nla_put_in_addr(skb, OVS_TUNNEL_KEY_ATTR_IPV4_DST,
656 output->ipv4_dst))
655 return -EMSGSIZE; 657 return -EMSGSIZE;
656 if (output->ipv4_tos && 658 if (output->ipv4_tos &&
657 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos)) 659 nla_put_u8(skb, OVS_TUNNEL_KEY_ATTR_TOS, output->ipv4_tos))
diff --git a/net/openvswitch/vport-vxlan.c b/net/openvswitch/vport-vxlan.c
index 3277a7520e31..6d39766e7828 100644
--- a/net/openvswitch/vport-vxlan.c
+++ b/net/openvswitch/vport-vxlan.c
@@ -222,7 +222,8 @@ static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
222{ 222{
223 struct net *net = ovs_dp_get_net(vport->dp); 223 struct net *net = ovs_dp_get_net(vport->dp);
224 struct vxlan_port *vxlan_port = vxlan_vport(vport); 224 struct vxlan_port *vxlan_port = vxlan_vport(vport);
225 __be16 dst_port = inet_sk(vxlan_port->vs->sock->sk)->inet_sport; 225 struct sock *sk = vxlan_port->vs->sock->sk;
226 __be16 dst_port = inet_sk(sk)->inet_sport;
226 const struct ovs_key_ipv4_tunnel *tun_key; 227 const struct ovs_key_ipv4_tunnel *tun_key;
227 struct vxlan_metadata md = {0}; 228 struct vxlan_metadata md = {0};
228 struct rtable *rt; 229 struct rtable *rt;
@@ -255,7 +256,7 @@ static int vxlan_tnl_send(struct vport *vport, struct sk_buff *skb)
255 vxflags = vxlan_port->exts | 256 vxflags = vxlan_port->exts |
256 (tun_key->tun_flags & TUNNEL_CSUM ? VXLAN_F_UDP_CSUM : 0); 257 (tun_key->tun_flags & TUNNEL_CSUM ? VXLAN_F_UDP_CSUM : 0);
257 258
258 err = vxlan_xmit_skb(rt, skb, fl.saddr, tun_key->ipv4_dst, 259 err = vxlan_xmit_skb(rt, sk, skb, fl.saddr, tun_key->ipv4_dst,
259 tun_key->ipv4_tos, tun_key->ipv4_ttl, df, 260 tun_key->ipv4_tos, tun_key->ipv4_ttl, df,
260 src_port, dst_port, 261 src_port, dst_port,
261 &md, false, vxflags); 262 &md, false, vxflags);
diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index f8db7064d81c..5102c3cc4eec 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -216,10 +216,16 @@ static void prb_fill_vlan_info(struct tpacket_kbdq_core *,
216static void packet_flush_mclist(struct sock *sk); 216static void packet_flush_mclist(struct sock *sk);
217 217
218struct packet_skb_cb { 218struct packet_skb_cb {
219 unsigned int origlen;
220 union { 219 union {
221 struct sockaddr_pkt pkt; 220 struct sockaddr_pkt pkt;
222 struct sockaddr_ll ll; 221 union {
222 /* Trick: alias skb original length with
223 * ll.sll_family and ll.protocol in order
224 * to save room.
225 */
226 unsigned int origlen;
227 struct sockaddr_ll ll;
228 };
223 } sa; 229 } sa;
224}; 230};
225 231
@@ -1608,8 +1614,8 @@ oom:
1608 * protocol layers and you must therefore supply it with a complete frame 1614 * protocol layers and you must therefore supply it with a complete frame
1609 */ 1615 */
1610 1616
1611static int packet_sendmsg_spkt(struct kiocb *iocb, struct socket *sock, 1617static int packet_sendmsg_spkt(struct socket *sock, struct msghdr *msg,
1612 struct msghdr *msg, size_t len) 1618 size_t len)
1613{ 1619{
1614 struct sock *sk = sock->sk; 1620 struct sock *sk = sock->sk;
1615 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name); 1621 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name);
@@ -1818,13 +1824,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1818 skb = nskb; 1824 skb = nskb;
1819 } 1825 }
1820 1826
1821 BUILD_BUG_ON(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8 > 1827 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8);
1822 sizeof(skb->cb));
1823 1828
1824 sll = &PACKET_SKB_CB(skb)->sa.ll; 1829 sll = &PACKET_SKB_CB(skb)->sa.ll;
1825 sll->sll_family = AF_PACKET;
1826 sll->sll_hatype = dev->type; 1830 sll->sll_hatype = dev->type;
1827 sll->sll_protocol = skb->protocol;
1828 sll->sll_pkttype = skb->pkt_type; 1831 sll->sll_pkttype = skb->pkt_type;
1829 if (unlikely(po->origdev)) 1832 if (unlikely(po->origdev))
1830 sll->sll_ifindex = orig_dev->ifindex; 1833 sll->sll_ifindex = orig_dev->ifindex;
@@ -1833,7 +1836,10 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1833 1836
1834 sll->sll_halen = dev_parse_header(skb, sll->sll_addr); 1837 sll->sll_halen = dev_parse_header(skb, sll->sll_addr);
1835 1838
1836 PACKET_SKB_CB(skb)->origlen = skb->len; 1839 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg().
1840 * Use their space for storing the original skb length.
1841 */
1842 PACKET_SKB_CB(skb)->sa.origlen = skb->len;
1837 1843
1838 if (pskb_trim(skb, snaplen)) 1844 if (pskb_trim(skb, snaplen))
1839 goto drop_n_acct; 1845 goto drop_n_acct;
@@ -1847,7 +1853,7 @@ static int packet_rcv(struct sk_buff *skb, struct net_device *dev,
1847 1853
1848 spin_lock(&sk->sk_receive_queue.lock); 1854 spin_lock(&sk->sk_receive_queue.lock);
1849 po->stats.stats1.tp_packets++; 1855 po->stats.stats1.tp_packets++;
1850 skb->dropcount = atomic_read(&sk->sk_drops); 1856 sock_skb_set_dropcount(sk, skb);
1851 __skb_queue_tail(&sk->sk_receive_queue, skb); 1857 __skb_queue_tail(&sk->sk_receive_queue, skb);
1852 spin_unlock(&sk->sk_receive_queue.lock); 1858 spin_unlock(&sk->sk_receive_queue.lock);
1853 sk->sk_data_ready(sk); 1859 sk->sk_data_ready(sk);
@@ -1910,14 +1916,19 @@ static int tpacket_rcv(struct sk_buff *skb, struct net_device *dev,
1910 } 1916 }
1911 } 1917 }
1912 1918
1913 if (skb->ip_summed == CHECKSUM_PARTIAL)
1914 status |= TP_STATUS_CSUMNOTREADY;
1915
1916 snaplen = skb->len; 1919 snaplen = skb->len;
1917 1920
1918 res = run_filter(skb, sk, snaplen); 1921 res = run_filter(skb, sk, snaplen);
1919 if (!res) 1922 if (!res)
1920 goto drop_n_restore; 1923 goto drop_n_restore;
1924
1925 if (skb->ip_summed == CHECKSUM_PARTIAL)
1926 status |= TP_STATUS_CSUMNOTREADY;
1927 else if (skb->pkt_type != PACKET_OUTGOING &&
1928 (skb->ip_summed == CHECKSUM_COMPLETE ||
1929 skb_csum_unnecessary(skb)))
1930 status |= TP_STATUS_CSUM_VALID;
1931
1921 if (snaplen > res) 1932 if (snaplen > res)
1922 snaplen = res; 1933 snaplen = res;
1923 1934
@@ -2603,8 +2614,7 @@ out:
2603 return err; 2614 return err;
2604} 2615}
2605 2616
2606static int packet_sendmsg(struct kiocb *iocb, struct socket *sock, 2617static int packet_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
2607 struct msghdr *msg, size_t len)
2608{ 2618{
2609 struct sock *sk = sock->sk; 2619 struct sock *sk = sock->sk;
2610 struct packet_sock *po = pkt_sk(sk); 2620 struct packet_sock *po = pkt_sk(sk);
@@ -2884,13 +2894,14 @@ out:
2884 * If necessary we block. 2894 * If necessary we block.
2885 */ 2895 */
2886 2896
2887static int packet_recvmsg(struct kiocb *iocb, struct socket *sock, 2897static int packet_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
2888 struct msghdr *msg, size_t len, int flags) 2898 int flags)
2889{ 2899{
2890 struct sock *sk = sock->sk; 2900 struct sock *sk = sock->sk;
2891 struct sk_buff *skb; 2901 struct sk_buff *skb;
2892 int copied, err; 2902 int copied, err;
2893 int vnet_hdr_len = 0; 2903 int vnet_hdr_len = 0;
2904 unsigned int origlen = 0;
2894 2905
2895 err = -EINVAL; 2906 err = -EINVAL;
2896 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE)) 2907 if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT|MSG_ERRQUEUE))
@@ -2990,6 +3001,15 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
2990 if (err) 3001 if (err)
2991 goto out_free; 3002 goto out_free;
2992 3003
3004 if (sock->type != SOCK_PACKET) {
3005 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3006
3007 /* Original length was stored in sockaddr_ll fields */
3008 origlen = PACKET_SKB_CB(skb)->sa.origlen;
3009 sll->sll_family = AF_PACKET;
3010 sll->sll_protocol = skb->protocol;
3011 }
3012
2993 sock_recv_ts_and_drops(msg, sk, skb); 3013 sock_recv_ts_and_drops(msg, sk, skb);
2994 3014
2995 if (msg->msg_name) { 3015 if (msg->msg_name) {
@@ -3001,6 +3021,7 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
3001 msg->msg_namelen = sizeof(struct sockaddr_pkt); 3021 msg->msg_namelen = sizeof(struct sockaddr_pkt);
3002 } else { 3022 } else {
3003 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll; 3023 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll;
3024
3004 msg->msg_namelen = sll->sll_halen + 3025 msg->msg_namelen = sll->sll_halen +
3005 offsetof(struct sockaddr_ll, sll_addr); 3026 offsetof(struct sockaddr_ll, sll_addr);
3006 } 3027 }
@@ -3014,7 +3035,12 @@ static int packet_recvmsg(struct kiocb *iocb, struct socket *sock,
3014 aux.tp_status = TP_STATUS_USER; 3035 aux.tp_status = TP_STATUS_USER;
3015 if (skb->ip_summed == CHECKSUM_PARTIAL) 3036 if (skb->ip_summed == CHECKSUM_PARTIAL)
3016 aux.tp_status |= TP_STATUS_CSUMNOTREADY; 3037 aux.tp_status |= TP_STATUS_CSUMNOTREADY;
3017 aux.tp_len = PACKET_SKB_CB(skb)->origlen; 3038 else if (skb->pkt_type != PACKET_OUTGOING &&
3039 (skb->ip_summed == CHECKSUM_COMPLETE ||
3040 skb_csum_unnecessary(skb)))
3041 aux.tp_status |= TP_STATUS_CSUM_VALID;
3042
3043 aux.tp_len = origlen;
3018 aux.tp_snaplen = skb->len; 3044 aux.tp_snaplen = skb->len;
3019 aux.tp_mac = 0; 3045 aux.tp_mac = 0;
3020 aux.tp_net = skb_network_offset(skb); 3046 aux.tp_net = skb_network_offset(skb);
diff --git a/net/packet/internal.h b/net/packet/internal.h
index cdddf6a30399..fe6e20caea1d 100644
--- a/net/packet/internal.h
+++ b/net/packet/internal.h
@@ -74,9 +74,7 @@ extern struct mutex fanout_mutex;
74#define PACKET_FANOUT_MAX 256 74#define PACKET_FANOUT_MAX 256
75 75
76struct packet_fanout { 76struct packet_fanout {
77#ifdef CONFIG_NET_NS 77 possible_net_t net;
78 struct net *net;
79#endif
80 unsigned int num_members; 78 unsigned int num_members;
81 u16 id; 79 u16 id;
82 u8 type; 80 u8 type;
diff --git a/net/phonet/datagram.c b/net/phonet/datagram.c
index 26054b4b467c..5e710435ffa9 100644
--- a/net/phonet/datagram.c
+++ b/net/phonet/datagram.c
@@ -83,8 +83,7 @@ static int pn_init(struct sock *sk)
83 return 0; 83 return 0;
84} 84}
85 85
86static int pn_sendmsg(struct kiocb *iocb, struct sock *sk, 86static int pn_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
87 struct msghdr *msg, size_t len)
88{ 87{
89 DECLARE_SOCKADDR(struct sockaddr_pn *, target, msg->msg_name); 88 DECLARE_SOCKADDR(struct sockaddr_pn *, target, msg->msg_name);
90 struct sk_buff *skb; 89 struct sk_buff *skb;
@@ -125,9 +124,8 @@ static int pn_sendmsg(struct kiocb *iocb, struct sock *sk,
125 return (err >= 0) ? len : err; 124 return (err >= 0) ? len : err;
126} 125}
127 126
128static int pn_recvmsg(struct kiocb *iocb, struct sock *sk, 127static int pn_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
129 struct msghdr *msg, size_t len, int noblock, 128 int noblock, int flags, int *addr_len)
130 int flags, int *addr_len)
131{ 129{
132 struct sk_buff *skb = NULL; 130 struct sk_buff *skb = NULL;
133 struct sockaddr_pn sa; 131 struct sockaddr_pn sa;
diff --git a/net/phonet/pep.c b/net/phonet/pep.c
index 5d3f2b7507d4..6de2aeb98a1f 100644
--- a/net/phonet/pep.c
+++ b/net/phonet/pep.c
@@ -1118,8 +1118,7 @@ static int pipe_skb_send(struct sock *sk, struct sk_buff *skb)
1118 1118
1119} 1119}
1120 1120
1121static int pep_sendmsg(struct kiocb *iocb, struct sock *sk, 1121static int pep_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1122 struct msghdr *msg, size_t len)
1123{ 1122{
1124 struct pep_sock *pn = pep_sk(sk); 1123 struct pep_sock *pn = pep_sk(sk);
1125 struct sk_buff *skb; 1124 struct sk_buff *skb;
@@ -1246,9 +1245,8 @@ struct sk_buff *pep_read(struct sock *sk)
1246 return skb; 1245 return skb;
1247} 1246}
1248 1247
1249static int pep_recvmsg(struct kiocb *iocb, struct sock *sk, 1248static int pep_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1250 struct msghdr *msg, size_t len, int noblock, 1249 int noblock, int flags, int *addr_len)
1251 int flags, int *addr_len)
1252{ 1250{
1253 struct sk_buff *skb; 1251 struct sk_buff *skb;
1254 int err; 1252 int err;
diff --git a/net/phonet/socket.c b/net/phonet/socket.c
index 008214a3d5eb..d575ef4e9aa6 100644
--- a/net/phonet/socket.c
+++ b/net/phonet/socket.c
@@ -425,15 +425,15 @@ out:
425 return err; 425 return err;
426} 426}
427 427
428static int pn_socket_sendmsg(struct kiocb *iocb, struct socket *sock, 428static int pn_socket_sendmsg(struct socket *sock, struct msghdr *m,
429 struct msghdr *m, size_t total_len) 429 size_t total_len)
430{ 430{
431 struct sock *sk = sock->sk; 431 struct sock *sk = sock->sk;
432 432
433 if (pn_socket_autobind(sock)) 433 if (pn_socket_autobind(sock))
434 return -EAGAIN; 434 return -EAGAIN;
435 435
436 return sk->sk_prot->sendmsg(iocb, sk, m, total_len); 436 return sk->sk_prot->sendmsg(sk, m, total_len);
437} 437}
438 438
439const struct proto_ops phonet_dgram_ops = { 439const struct proto_ops phonet_dgram_ops = {
diff --git a/net/rds/connection.c b/net/rds/connection.c
index 378c3a6acf84..14f041398ca1 100644
--- a/net/rds/connection.c
+++ b/net/rds/connection.c
@@ -130,7 +130,7 @@ static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
130 rcu_read_lock(); 130 rcu_read_lock();
131 conn = rds_conn_lookup(head, laddr, faddr, trans); 131 conn = rds_conn_lookup(head, laddr, faddr, trans);
132 if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport && 132 if (conn && conn->c_loopback && conn->c_trans != &rds_loop_transport &&
133 !is_outgoing) { 133 laddr == faddr && !is_outgoing) {
134 /* This is a looped back IB connection, and we're 134 /* This is a looped back IB connection, and we're
135 * called by the code handling the incoming connect. 135 * called by the code handling the incoming connect.
136 * We need a second connection object into which we 136 * We need a second connection object into which we
@@ -193,6 +193,7 @@ static struct rds_connection *__rds_conn_create(__be32 laddr, __be32 faddr,
193 } 193 }
194 194
195 atomic_set(&conn->c_state, RDS_CONN_DOWN); 195 atomic_set(&conn->c_state, RDS_CONN_DOWN);
196 conn->c_send_gen = 0;
196 conn->c_reconnect_jiffies = 0; 197 conn->c_reconnect_jiffies = 0;
197 INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker); 198 INIT_DELAYED_WORK(&conn->c_send_w, rds_send_worker);
198 INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker); 199 INIT_DELAYED_WORK(&conn->c_recv_w, rds_recv_worker);
diff --git a/net/rds/rds.h b/net/rds/rds.h
index c2a5eef41343..0d41155a2258 100644
--- a/net/rds/rds.h
+++ b/net/rds/rds.h
@@ -110,6 +110,7 @@ struct rds_connection {
110 void *c_transport_data; 110 void *c_transport_data;
111 111
112 atomic_t c_state; 112 atomic_t c_state;
113 unsigned long c_send_gen;
113 unsigned long c_flags; 114 unsigned long c_flags;
114 unsigned long c_reconnect_jiffies; 115 unsigned long c_reconnect_jiffies;
115 struct delayed_work c_send_w; 116 struct delayed_work c_send_w;
@@ -702,8 +703,8 @@ void rds_inc_init(struct rds_incoming *inc, struct rds_connection *conn,
702void rds_inc_put(struct rds_incoming *inc); 703void rds_inc_put(struct rds_incoming *inc);
703void rds_recv_incoming(struct rds_connection *conn, __be32 saddr, __be32 daddr, 704void rds_recv_incoming(struct rds_connection *conn, __be32 saddr, __be32 daddr,
704 struct rds_incoming *inc, gfp_t gfp); 705 struct rds_incoming *inc, gfp_t gfp);
705int rds_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 706int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
706 size_t size, int msg_flags); 707 int msg_flags);
707void rds_clear_recv_queue(struct rds_sock *rs); 708void rds_clear_recv_queue(struct rds_sock *rs);
708int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msg); 709int rds_notify_queue_get(struct rds_sock *rs, struct msghdr *msg);
709void rds_inc_info_copy(struct rds_incoming *inc, 710void rds_inc_info_copy(struct rds_incoming *inc,
@@ -711,8 +712,7 @@ void rds_inc_info_copy(struct rds_incoming *inc,
711 __be32 saddr, __be32 daddr, int flip); 712 __be32 saddr, __be32 daddr, int flip);
712 713
713/* send.c */ 714/* send.c */
714int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 715int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len);
715 size_t payload_len);
716void rds_send_reset(struct rds_connection *conn); 716void rds_send_reset(struct rds_connection *conn);
717int rds_send_xmit(struct rds_connection *conn); 717int rds_send_xmit(struct rds_connection *conn);
718struct sockaddr_in; 718struct sockaddr_in;
diff --git a/net/rds/recv.c b/net/rds/recv.c
index f9ec1acd801c..a00462b0d01d 100644
--- a/net/rds/recv.c
+++ b/net/rds/recv.c
@@ -395,8 +395,8 @@ static int rds_cmsg_recv(struct rds_incoming *inc, struct msghdr *msg)
395 return 0; 395 return 0;
396} 396}
397 397
398int rds_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 398int rds_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
399 size_t size, int msg_flags) 399 int msg_flags)
400{ 400{
401 struct sock *sk = sock->sk; 401 struct sock *sk = sock->sk;
402 struct rds_sock *rs = rds_sk_to_rs(sk); 402 struct rds_sock *rs = rds_sk_to_rs(sk);
diff --git a/net/rds/send.c b/net/rds/send.c
index 42f65d4305c8..e9430f537f9c 100644
--- a/net/rds/send.c
+++ b/net/rds/send.c
@@ -140,8 +140,11 @@ int rds_send_xmit(struct rds_connection *conn)
140 struct scatterlist *sg; 140 struct scatterlist *sg;
141 int ret = 0; 141 int ret = 0;
142 LIST_HEAD(to_be_dropped); 142 LIST_HEAD(to_be_dropped);
143 int batch_count;
144 unsigned long send_gen = 0;
143 145
144restart: 146restart:
147 batch_count = 0;
145 148
146 /* 149 /*
147 * sendmsg calls here after having queued its message on the send 150 * sendmsg calls here after having queued its message on the send
@@ -157,6 +160,17 @@ restart:
157 } 160 }
158 161
159 /* 162 /*
163 * we record the send generation after doing the xmit acquire.
164 * if someone else manages to jump in and do some work, we'll use
165 * this to avoid a goto restart farther down.
166 *
167 * The acquire_in_xmit() check above ensures that only one
168 * caller can increment c_send_gen at any time.
169 */
170 conn->c_send_gen++;
171 send_gen = conn->c_send_gen;
172
173 /*
160 * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT, 174 * rds_conn_shutdown() sets the conn state and then tests RDS_IN_XMIT,
161 * we do the opposite to avoid races. 175 * we do the opposite to avoid races.
162 */ 176 */
@@ -202,6 +216,16 @@ restart:
202 if (!rm) { 216 if (!rm) {
203 unsigned int len; 217 unsigned int len;
204 218
219 batch_count++;
220
221 /* we want to process as big a batch as we can, but
222 * we also want to avoid softlockups. If we've been
223 * through a lot of messages, lets back off and see
224 * if anyone else jumps in
225 */
226 if (batch_count >= 1024)
227 goto over_batch;
228
205 spin_lock_irqsave(&conn->c_lock, flags); 229 spin_lock_irqsave(&conn->c_lock, flags);
206 230
207 if (!list_empty(&conn->c_send_queue)) { 231 if (!list_empty(&conn->c_send_queue)) {
@@ -357,9 +381,9 @@ restart:
357 } 381 }
358 } 382 }
359 383
384over_batch:
360 if (conn->c_trans->xmit_complete) 385 if (conn->c_trans->xmit_complete)
361 conn->c_trans->xmit_complete(conn); 386 conn->c_trans->xmit_complete(conn);
362
363 release_in_xmit(conn); 387 release_in_xmit(conn);
364 388
365 /* Nuke any messages we decided not to retransmit. */ 389 /* Nuke any messages we decided not to retransmit. */
@@ -380,10 +404,15 @@ restart:
380 * If the transport cannot continue (i.e ret != 0), then it must 404 * If the transport cannot continue (i.e ret != 0), then it must
381 * call us when more room is available, such as from the tx 405 * call us when more room is available, such as from the tx
382 * completion handler. 406 * completion handler.
407 *
408 * We have an extra generation check here so that if someone manages
409 * to jump in after our release_in_xmit, we'll see that they have done
410 * some work and we will skip our goto
383 */ 411 */
384 if (ret == 0) { 412 if (ret == 0) {
385 smp_mb(); 413 smp_mb();
386 if (!list_empty(&conn->c_send_queue)) { 414 if (!list_empty(&conn->c_send_queue) &&
415 send_gen == conn->c_send_gen) {
387 rds_stats_inc(s_send_lock_queue_raced); 416 rds_stats_inc(s_send_lock_queue_raced);
388 goto restart; 417 goto restart;
389 } 418 }
@@ -920,8 +949,7 @@ static int rds_cmsg_send(struct rds_sock *rs, struct rds_message *rm,
920 return ret; 949 return ret;
921} 950}
922 951
923int rds_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 952int rds_sendmsg(struct socket *sock, struct msghdr *msg, size_t payload_len)
924 size_t payload_len)
925{ 953{
926 struct sock *sk = sock->sk; 954 struct sock *sk = sock->sk;
927 struct rds_sock *rs = rds_sk_to_rs(sk); 955 struct rds_sock *rs = rds_sk_to_rs(sk);
diff --git a/net/rose/af_rose.c b/net/rose/af_rose.c
index 43bac7c4dd9e..8ae603069a1a 100644
--- a/net/rose/af_rose.c
+++ b/net/rose/af_rose.c
@@ -1046,8 +1046,7 @@ int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct ros
1046 return 1; 1046 return 1;
1047} 1047}
1048 1048
1049static int rose_sendmsg(struct kiocb *iocb, struct socket *sock, 1049static int rose_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1050 struct msghdr *msg, size_t len)
1051{ 1050{
1052 struct sock *sk = sock->sk; 1051 struct sock *sk = sock->sk;
1053 struct rose_sock *rose = rose_sk(sk); 1052 struct rose_sock *rose = rose_sk(sk);
@@ -1211,8 +1210,8 @@ static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
1211} 1210}
1212 1211
1213 1212
1214static int rose_recvmsg(struct kiocb *iocb, struct socket *sock, 1213static int rose_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1215 struct msghdr *msg, size_t size, int flags) 1214 int flags)
1216{ 1215{
1217 struct sock *sk = sock->sk; 1216 struct sock *sk = sock->sk;
1218 struct rose_sock *rose = rose_sk(sk); 1217 struct rose_sock *rose = rose_sk(sk);
diff --git a/net/rose/rose_dev.c b/net/rose/rose_dev.c
index 50005888be57..369ca81a8c5d 100644
--- a/net/rose/rose_dev.c
+++ b/net/rose/rose_dev.c
@@ -41,6 +41,9 @@ static int rose_header(struct sk_buff *skb, struct net_device *dev,
41{ 41{
42 unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2); 42 unsigned char *buff = skb_push(skb, ROSE_MIN_LEN + 2);
43 43
44 if (daddr)
45 memcpy(buff + 7, daddr, dev->addr_len);
46
44 *buff++ = ROSE_GFI | ROSE_Q_BIT; 47 *buff++ = ROSE_GFI | ROSE_Q_BIT;
45 *buff++ = 0x00; 48 *buff++ = 0x00;
46 *buff++ = ROSE_DATA; 49 *buff++ = ROSE_DATA;
@@ -53,43 +56,6 @@ static int rose_header(struct sk_buff *skb, struct net_device *dev,
53 return -37; 56 return -37;
54} 57}
55 58
56static int rose_rebuild_header(struct sk_buff *skb)
57{
58#ifdef CONFIG_INET
59 struct net_device *dev = skb->dev;
60 struct net_device_stats *stats = &dev->stats;
61 unsigned char *bp = (unsigned char *)skb->data;
62 struct sk_buff *skbn;
63 unsigned int len;
64
65 if (arp_find(bp + 7, skb)) {
66 return 1;
67 }
68
69 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
70 kfree_skb(skb);
71 return 1;
72 }
73
74 if (skb->sk != NULL)
75 skb_set_owner_w(skbn, skb->sk);
76
77 kfree_skb(skb);
78
79 len = skbn->len;
80
81 if (!rose_route_frame(skbn, NULL)) {
82 kfree_skb(skbn);
83 stats->tx_errors++;
84 return 1;
85 }
86
87 stats->tx_packets++;
88 stats->tx_bytes += len;
89#endif
90 return 1;
91}
92
93static int rose_set_mac_address(struct net_device *dev, void *addr) 59static int rose_set_mac_address(struct net_device *dev, void *addr)
94{ 60{
95 struct sockaddr *sa = addr; 61 struct sockaddr *sa = addr;
@@ -134,19 +100,26 @@ static int rose_close(struct net_device *dev)
134static netdev_tx_t rose_xmit(struct sk_buff *skb, struct net_device *dev) 100static netdev_tx_t rose_xmit(struct sk_buff *skb, struct net_device *dev)
135{ 101{
136 struct net_device_stats *stats = &dev->stats; 102 struct net_device_stats *stats = &dev->stats;
103 unsigned int len = skb->len;
137 104
138 if (!netif_running(dev)) { 105 if (!netif_running(dev)) {
139 printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n"); 106 printk(KERN_ERR "ROSE: rose_xmit - called when iface is down\n");
140 return NETDEV_TX_BUSY; 107 return NETDEV_TX_BUSY;
141 } 108 }
142 dev_kfree_skb(skb); 109
143 stats->tx_errors++; 110 if (!rose_route_frame(skb, NULL)) {
111 dev_kfree_skb(skb);
112 stats->tx_errors++;
113 return NETDEV_TX_OK;
114 }
115
116 stats->tx_packets++;
117 stats->tx_bytes += len;
144 return NETDEV_TX_OK; 118 return NETDEV_TX_OK;
145} 119}
146 120
147static const struct header_ops rose_header_ops = { 121static const struct header_ops rose_header_ops = {
148 .create = rose_header, 122 .create = rose_header,
149 .rebuild = rose_rebuild_header,
150}; 123};
151 124
152static const struct net_device_ops rose_netdev_ops = { 125static const struct net_device_ops rose_netdev_ops = {
diff --git a/net/rxrpc/af_rxrpc.c b/net/rxrpc/af_rxrpc.c
index 7b1670489638..0095b9a0b779 100644
--- a/net/rxrpc/af_rxrpc.c
+++ b/net/rxrpc/af_rxrpc.c
@@ -441,8 +441,7 @@ static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
441 * - sends a call data packet 441 * - sends a call data packet
442 * - may send an abort (abort code in control data) 442 * - may send an abort (abort code in control data)
443 */ 443 */
444static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock, 444static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
445 struct msghdr *m, size_t len)
446{ 445{
447 struct rxrpc_transport *trans; 446 struct rxrpc_transport *trans;
448 struct rxrpc_sock *rx = rxrpc_sk(sock->sk); 447 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
@@ -482,7 +481,7 @@ static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
482 switch (rx->sk.sk_state) { 481 switch (rx->sk.sk_state) {
483 case RXRPC_SERVER_LISTENING: 482 case RXRPC_SERVER_LISTENING:
484 if (!m->msg_name) { 483 if (!m->msg_name) {
485 ret = rxrpc_server_sendmsg(iocb, rx, m, len); 484 ret = rxrpc_server_sendmsg(rx, m, len);
486 break; 485 break;
487 } 486 }
488 case RXRPC_SERVER_BOUND: 487 case RXRPC_SERVER_BOUND:
@@ -492,7 +491,7 @@ static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock,
492 break; 491 break;
493 } 492 }
494 case RXRPC_CLIENT_CONNECTED: 493 case RXRPC_CLIENT_CONNECTED:
495 ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len); 494 ret = rxrpc_client_sendmsg(rx, trans, m, len);
496 break; 495 break;
497 default: 496 default:
498 ret = -ENOTCONN; 497 ret = -ENOTCONN;
diff --git a/net/rxrpc/ar-input.c b/net/rxrpc/ar-input.c
index 481f89f93789..4505a691d88c 100644
--- a/net/rxrpc/ar-input.c
+++ b/net/rxrpc/ar-input.c
@@ -28,7 +28,7 @@
28const char *rxrpc_pkts[] = { 28const char *rxrpc_pkts[] = {
29 "?00", 29 "?00",
30 "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG", 30 "DATA", "ACK", "BUSY", "ABORT", "ACKALL", "CHALL", "RESP", "DEBUG",
31 "?09", "?10", "?11", "?12", "?13", "?14", "?15" 31 "?09", "?10", "?11", "?12", "VERSION", "?14", "?15"
32}; 32};
33 33
34/* 34/*
@@ -593,6 +593,20 @@ static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
593 rxrpc_queue_conn(conn); 593 rxrpc_queue_conn(conn);
594} 594}
595 595
596/*
597 * post endpoint-level events to the local endpoint
598 * - this includes debug and version messages
599 */
600static void rxrpc_post_packet_to_local(struct rxrpc_local *local,
601 struct sk_buff *skb)
602{
603 _enter("%p,%p", local, skb);
604
605 atomic_inc(&local->usage);
606 skb_queue_tail(&local->event_queue, skb);
607 rxrpc_queue_work(&local->event_processor);
608}
609
596static struct rxrpc_connection *rxrpc_conn_from_local(struct rxrpc_local *local, 610static struct rxrpc_connection *rxrpc_conn_from_local(struct rxrpc_local *local,
597 struct sk_buff *skb, 611 struct sk_buff *skb,
598 struct rxrpc_skb_priv *sp) 612 struct rxrpc_skb_priv *sp)
@@ -699,6 +713,11 @@ void rxrpc_data_ready(struct sock *sk)
699 goto bad_message; 713 goto bad_message;
700 } 714 }
701 715
716 if (sp->hdr.type == RXRPC_PACKET_TYPE_VERSION) {
717 rxrpc_post_packet_to_local(local, skb);
718 goto out;
719 }
720
702 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA && 721 if (sp->hdr.type == RXRPC_PACKET_TYPE_DATA &&
703 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0)) 722 (sp->hdr.callNumber == 0 || sp->hdr.seq == 0))
704 goto bad_message; 723 goto bad_message;
@@ -731,6 +750,8 @@ void rxrpc_data_ready(struct sock *sk)
731 else 750 else
732 goto cant_route_call; 751 goto cant_route_call;
733 } 752 }
753
754out:
734 rxrpc_put_local(local); 755 rxrpc_put_local(local);
735 return; 756 return;
736 757
diff --git a/net/rxrpc/ar-internal.h b/net/rxrpc/ar-internal.h
index ba9fd36d3f15..aef1bd294e17 100644
--- a/net/rxrpc/ar-internal.h
+++ b/net/rxrpc/ar-internal.h
@@ -152,11 +152,13 @@ struct rxrpc_local {
152 struct work_struct destroyer; /* endpoint destroyer */ 152 struct work_struct destroyer; /* endpoint destroyer */
153 struct work_struct acceptor; /* incoming call processor */ 153 struct work_struct acceptor; /* incoming call processor */
154 struct work_struct rejecter; /* packet reject writer */ 154 struct work_struct rejecter; /* packet reject writer */
155 struct work_struct event_processor; /* endpoint event processor */
155 struct list_head services; /* services listening on this endpoint */ 156 struct list_head services; /* services listening on this endpoint */
156 struct list_head link; /* link in endpoint list */ 157 struct list_head link; /* link in endpoint list */
157 struct rw_semaphore defrag_sem; /* control re-enablement of IP DF bit */ 158 struct rw_semaphore defrag_sem; /* control re-enablement of IP DF bit */
158 struct sk_buff_head accept_queue; /* incoming calls awaiting acceptance */ 159 struct sk_buff_head accept_queue; /* incoming calls awaiting acceptance */
159 struct sk_buff_head reject_queue; /* packets awaiting rejection */ 160 struct sk_buff_head reject_queue; /* packets awaiting rejection */
161 struct sk_buff_head event_queue; /* endpoint event packets awaiting processing */
160 spinlock_t lock; /* access lock */ 162 spinlock_t lock; /* access lock */
161 rwlock_t services_lock; /* lock for services list */ 163 rwlock_t services_lock; /* lock for services list */
162 atomic_t usage; 164 atomic_t usage;
@@ -548,10 +550,9 @@ int rxrpc_get_server_data_key(struct rxrpc_connection *, const void *, time_t,
548extern unsigned rxrpc_resend_timeout; 550extern unsigned rxrpc_resend_timeout;
549 551
550int rxrpc_send_packet(struct rxrpc_transport *, struct sk_buff *); 552int rxrpc_send_packet(struct rxrpc_transport *, struct sk_buff *);
551int rxrpc_client_sendmsg(struct kiocb *, struct rxrpc_sock *, 553int rxrpc_client_sendmsg(struct rxrpc_sock *, struct rxrpc_transport *,
552 struct rxrpc_transport *, struct msghdr *, size_t); 554 struct msghdr *, size_t);
553int rxrpc_server_sendmsg(struct kiocb *, struct rxrpc_sock *, struct msghdr *, 555int rxrpc_server_sendmsg(struct rxrpc_sock *, struct msghdr *, size_t);
554 size_t);
555 556
556/* 557/*
557 * ar-peer.c 558 * ar-peer.c
@@ -572,8 +573,7 @@ extern const struct file_operations rxrpc_connection_seq_fops;
572 * ar-recvmsg.c 573 * ar-recvmsg.c
573 */ 574 */
574void rxrpc_remove_user_ID(struct rxrpc_sock *, struct rxrpc_call *); 575void rxrpc_remove_user_ID(struct rxrpc_sock *, struct rxrpc_call *);
575int rxrpc_recvmsg(struct kiocb *, struct socket *, struct msghdr *, size_t, 576int rxrpc_recvmsg(struct socket *, struct msghdr *, size_t, int);
576 int);
577 577
578/* 578/*
579 * ar-security.c 579 * ar-security.c
diff --git a/net/rxrpc/ar-local.c b/net/rxrpc/ar-local.c
index 87f7135d238b..ca904ed5400a 100644
--- a/net/rxrpc/ar-local.c
+++ b/net/rxrpc/ar-local.c
@@ -13,16 +13,22 @@
13#include <linux/net.h> 13#include <linux/net.h>
14#include <linux/skbuff.h> 14#include <linux/skbuff.h>
15#include <linux/slab.h> 15#include <linux/slab.h>
16#include <linux/udp.h>
17#include <linux/ip.h>
16#include <net/sock.h> 18#include <net/sock.h>
17#include <net/af_rxrpc.h> 19#include <net/af_rxrpc.h>
20#include <generated/utsrelease.h>
18#include "ar-internal.h" 21#include "ar-internal.h"
19 22
23static const char rxrpc_version_string[65] = "linux-" UTS_RELEASE " AF_RXRPC";
24
20static LIST_HEAD(rxrpc_locals); 25static LIST_HEAD(rxrpc_locals);
21DEFINE_RWLOCK(rxrpc_local_lock); 26DEFINE_RWLOCK(rxrpc_local_lock);
22static DECLARE_RWSEM(rxrpc_local_sem); 27static DECLARE_RWSEM(rxrpc_local_sem);
23static DECLARE_WAIT_QUEUE_HEAD(rxrpc_local_wq); 28static DECLARE_WAIT_QUEUE_HEAD(rxrpc_local_wq);
24 29
25static void rxrpc_destroy_local(struct work_struct *work); 30static void rxrpc_destroy_local(struct work_struct *work);
31static void rxrpc_process_local_events(struct work_struct *work);
26 32
27/* 33/*
28 * allocate a new local 34 * allocate a new local
@@ -37,11 +43,13 @@ struct rxrpc_local *rxrpc_alloc_local(struct sockaddr_rxrpc *srx)
37 INIT_WORK(&local->destroyer, &rxrpc_destroy_local); 43 INIT_WORK(&local->destroyer, &rxrpc_destroy_local);
38 INIT_WORK(&local->acceptor, &rxrpc_accept_incoming_calls); 44 INIT_WORK(&local->acceptor, &rxrpc_accept_incoming_calls);
39 INIT_WORK(&local->rejecter, &rxrpc_reject_packets); 45 INIT_WORK(&local->rejecter, &rxrpc_reject_packets);
46 INIT_WORK(&local->event_processor, &rxrpc_process_local_events);
40 INIT_LIST_HEAD(&local->services); 47 INIT_LIST_HEAD(&local->services);
41 INIT_LIST_HEAD(&local->link); 48 INIT_LIST_HEAD(&local->link);
42 init_rwsem(&local->defrag_sem); 49 init_rwsem(&local->defrag_sem);
43 skb_queue_head_init(&local->accept_queue); 50 skb_queue_head_init(&local->accept_queue);
44 skb_queue_head_init(&local->reject_queue); 51 skb_queue_head_init(&local->reject_queue);
52 skb_queue_head_init(&local->event_queue);
45 spin_lock_init(&local->lock); 53 spin_lock_init(&local->lock);
46 rwlock_init(&local->services_lock); 54 rwlock_init(&local->services_lock);
47 atomic_set(&local->usage, 1); 55 atomic_set(&local->usage, 1);
@@ -264,10 +272,12 @@ static void rxrpc_destroy_local(struct work_struct *work)
264 ASSERT(list_empty(&local->services)); 272 ASSERT(list_empty(&local->services));
265 ASSERT(!work_pending(&local->acceptor)); 273 ASSERT(!work_pending(&local->acceptor));
266 ASSERT(!work_pending(&local->rejecter)); 274 ASSERT(!work_pending(&local->rejecter));
275 ASSERT(!work_pending(&local->event_processor));
267 276
268 /* finish cleaning up the local descriptor */ 277 /* finish cleaning up the local descriptor */
269 rxrpc_purge_queue(&local->accept_queue); 278 rxrpc_purge_queue(&local->accept_queue);
270 rxrpc_purge_queue(&local->reject_queue); 279 rxrpc_purge_queue(&local->reject_queue);
280 rxrpc_purge_queue(&local->event_queue);
271 kernel_sock_shutdown(local->socket, SHUT_RDWR); 281 kernel_sock_shutdown(local->socket, SHUT_RDWR);
272 sock_release(local->socket); 282 sock_release(local->socket);
273 283
@@ -308,3 +318,91 @@ void __exit rxrpc_destroy_all_locals(void)
308 318
309 _leave(""); 319 _leave("");
310} 320}
321
322/*
323 * Reply to a version request
324 */
325static void rxrpc_send_version_request(struct rxrpc_local *local,
326 struct rxrpc_header *hdr,
327 struct sk_buff *skb)
328{
329 struct sockaddr_in sin;
330 struct msghdr msg;
331 struct kvec iov[2];
332 size_t len;
333 int ret;
334
335 _enter("");
336
337 sin.sin_family = AF_INET;
338 sin.sin_port = udp_hdr(skb)->source;
339 sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
340
341 msg.msg_name = &sin;
342 msg.msg_namelen = sizeof(sin);
343 msg.msg_control = NULL;
344 msg.msg_controllen = 0;
345 msg.msg_flags = 0;
346
347 hdr->seq = 0;
348 hdr->serial = 0;
349 hdr->type = RXRPC_PACKET_TYPE_VERSION;
350 hdr->flags = RXRPC_LAST_PACKET | (~hdr->flags & RXRPC_CLIENT_INITIATED);
351 hdr->userStatus = 0;
352 hdr->_rsvd = 0;
353
354 iov[0].iov_base = hdr;
355 iov[0].iov_len = sizeof(*hdr);
356 iov[1].iov_base = (char *)rxrpc_version_string;
357 iov[1].iov_len = sizeof(rxrpc_version_string);
358
359 len = iov[0].iov_len + iov[1].iov_len;
360
361 _proto("Tx VERSION (reply)");
362
363 ret = kernel_sendmsg(local->socket, &msg, iov, 2, len);
364 if (ret < 0)
365 _debug("sendmsg failed: %d", ret);
366
367 _leave("");
368}
369
370/*
371 * Process event packets targetted at a local endpoint.
372 */
373static void rxrpc_process_local_events(struct work_struct *work)
374{
375 struct rxrpc_local *local = container_of(work, struct rxrpc_local, event_processor);
376 struct sk_buff *skb;
377 char v;
378
379 _enter("");
380
381 atomic_inc(&local->usage);
382
383 while ((skb = skb_dequeue(&local->event_queue))) {
384 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
385
386 kdebug("{%d},{%u}", local->debug_id, sp->hdr.type);
387
388 switch (sp->hdr.type) {
389 case RXRPC_PACKET_TYPE_VERSION:
390 if (skb_copy_bits(skb, 0, &v, 1) < 0)
391 return;
392 _proto("Rx VERSION { %02x }", v);
393 if (v == 0)
394 rxrpc_send_version_request(local, &sp->hdr, skb);
395 break;
396
397 default:
398 /* Just ignore anything we don't understand */
399 break;
400 }
401
402 rxrpc_put_local(local);
403 rxrpc_free_skb(skb);
404 }
405
406 rxrpc_put_local(local);
407 _leave("");
408}
diff --git a/net/rxrpc/ar-output.c b/net/rxrpc/ar-output.c
index 8331c95e1522..c0042807bfc6 100644
--- a/net/rxrpc/ar-output.c
+++ b/net/rxrpc/ar-output.c
@@ -23,8 +23,7 @@
23 */ 23 */
24unsigned rxrpc_resend_timeout = 4 * HZ; 24unsigned rxrpc_resend_timeout = 4 * HZ;
25 25
26static int rxrpc_send_data(struct kiocb *iocb, 26static int rxrpc_send_data(struct rxrpc_sock *rx,
27 struct rxrpc_sock *rx,
28 struct rxrpc_call *call, 27 struct rxrpc_call *call,
29 struct msghdr *msg, size_t len); 28 struct msghdr *msg, size_t len);
30 29
@@ -129,9 +128,8 @@ static void rxrpc_send_abort(struct rxrpc_call *call, u32 abort_code)
129 * - caller holds the socket locked 128 * - caller holds the socket locked
130 * - the socket may be either a client socket or a server socket 129 * - the socket may be either a client socket or a server socket
131 */ 130 */
132int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx, 131int rxrpc_client_sendmsg(struct rxrpc_sock *rx, struct rxrpc_transport *trans,
133 struct rxrpc_transport *trans, struct msghdr *msg, 132 struct msghdr *msg, size_t len)
134 size_t len)
135{ 133{
136 struct rxrpc_conn_bundle *bundle; 134 struct rxrpc_conn_bundle *bundle;
137 enum rxrpc_command cmd; 135 enum rxrpc_command cmd;
@@ -191,7 +189,7 @@ int rxrpc_client_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
191 /* request phase complete for this client call */ 189 /* request phase complete for this client call */
192 ret = -EPROTO; 190 ret = -EPROTO;
193 } else { 191 } else {
194 ret = rxrpc_send_data(iocb, rx, call, msg, len); 192 ret = rxrpc_send_data(rx, call, msg, len);
195 } 193 }
196 194
197 rxrpc_put_call(call); 195 rxrpc_put_call(call);
@@ -232,7 +230,7 @@ int rxrpc_kernel_send_data(struct rxrpc_call *call, struct msghdr *msg,
232 call->state != RXRPC_CALL_SERVER_SEND_REPLY) { 230 call->state != RXRPC_CALL_SERVER_SEND_REPLY) {
233 ret = -EPROTO; /* request phase complete for this client call */ 231 ret = -EPROTO; /* request phase complete for this client call */
234 } else { 232 } else {
235 ret = rxrpc_send_data(NULL, call->socket, call, msg, len); 233 ret = rxrpc_send_data(call->socket, call, msg, len);
236 } 234 }
237 235
238 release_sock(&call->socket->sk); 236 release_sock(&call->socket->sk);
@@ -271,8 +269,7 @@ EXPORT_SYMBOL(rxrpc_kernel_abort_call);
271 * send a message through a server socket 269 * send a message through a server socket
272 * - caller holds the socket locked 270 * - caller holds the socket locked
273 */ 271 */
274int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx, 272int rxrpc_server_sendmsg(struct rxrpc_sock *rx, struct msghdr *msg, size_t len)
275 struct msghdr *msg, size_t len)
276{ 273{
277 enum rxrpc_command cmd; 274 enum rxrpc_command cmd;
278 struct rxrpc_call *call; 275 struct rxrpc_call *call;
@@ -313,7 +310,7 @@ int rxrpc_server_sendmsg(struct kiocb *iocb, struct rxrpc_sock *rx,
313 break; 310 break;
314 } 311 }
315 312
316 ret = rxrpc_send_data(iocb, rx, call, msg, len); 313 ret = rxrpc_send_data(rx, call, msg, len);
317 break; 314 break;
318 315
319 case RXRPC_CMD_SEND_ABORT: 316 case RXRPC_CMD_SEND_ABORT:
@@ -520,8 +517,7 @@ static void rxrpc_queue_packet(struct rxrpc_call *call, struct sk_buff *skb,
520 * - must be called in process context 517 * - must be called in process context
521 * - caller holds the socket locked 518 * - caller holds the socket locked
522 */ 519 */
523static int rxrpc_send_data(struct kiocb *iocb, 520static int rxrpc_send_data(struct rxrpc_sock *rx,
524 struct rxrpc_sock *rx,
525 struct rxrpc_call *call, 521 struct rxrpc_call *call,
526 struct msghdr *msg, size_t len) 522 struct msghdr *msg, size_t len)
527{ 523{
@@ -546,11 +542,7 @@ static int rxrpc_send_data(struct kiocb *iocb,
546 call->tx_pending = NULL; 542 call->tx_pending = NULL;
547 543
548 copied = 0; 544 copied = 0;
549 if (len > iov_iter_count(&msg->msg_iter)) 545 do {
550 len = iov_iter_count(&msg->msg_iter);
551 while (len) {
552 int copy;
553
554 if (!skb) { 546 if (!skb) {
555 size_t size, chunk, max, space; 547 size_t size, chunk, max, space;
556 548
@@ -572,8 +564,8 @@ static int rxrpc_send_data(struct kiocb *iocb,
572 max &= ~(call->conn->size_align - 1UL); 564 max &= ~(call->conn->size_align - 1UL);
573 565
574 chunk = max; 566 chunk = max;
575 if (chunk > len && !more) 567 if (chunk > msg_data_left(msg) && !more)
576 chunk = len; 568 chunk = msg_data_left(msg);
577 569
578 space = chunk + call->conn->size_align; 570 space = chunk + call->conn->size_align;
579 space &= ~(call->conn->size_align - 1UL); 571 space &= ~(call->conn->size_align - 1UL);
@@ -616,23 +608,23 @@ static int rxrpc_send_data(struct kiocb *iocb,
616 sp = rxrpc_skb(skb); 608 sp = rxrpc_skb(skb);
617 609
618 /* append next segment of data to the current buffer */ 610 /* append next segment of data to the current buffer */
619 copy = skb_tailroom(skb); 611 if (msg_data_left(msg) > 0) {
620 ASSERTCMP(copy, >, 0); 612 int copy = skb_tailroom(skb);
621 if (copy > len) 613 ASSERTCMP(copy, >, 0);
622 copy = len; 614 if (copy > msg_data_left(msg))
623 if (copy > sp->remain) 615 copy = msg_data_left(msg);
624 copy = sp->remain; 616 if (copy > sp->remain)
625 617 copy = sp->remain;
626 _debug("add"); 618
627 ret = skb_add_data(skb, &msg->msg_iter, copy); 619 _debug("add");
628 _debug("added"); 620 ret = skb_add_data(skb, &msg->msg_iter, copy);
629 if (ret < 0) 621 _debug("added");
630 goto efault; 622 if (ret < 0)
631 sp->remain -= copy; 623 goto efault;
632 skb->mark += copy; 624 sp->remain -= copy;
633 copied += copy; 625 skb->mark += copy;
634 626 copied += copy;
635 len -= copy; 627 }
636 628
637 /* check for the far side aborting the call or a network error 629 /* check for the far side aborting the call or a network error
638 * occurring */ 630 * occurring */
@@ -640,7 +632,8 @@ static int rxrpc_send_data(struct kiocb *iocb,
640 goto call_aborted; 632 goto call_aborted;
641 633
642 /* add the packet to the send queue if it's now full */ 634 /* add the packet to the send queue if it's now full */
643 if (sp->remain <= 0 || (!len && !more)) { 635 if (sp->remain <= 0 ||
636 (msg_data_left(msg) == 0 && !more)) {
644 struct rxrpc_connection *conn = call->conn; 637 struct rxrpc_connection *conn = call->conn;
645 uint32_t seq; 638 uint32_t seq;
646 size_t pad; 639 size_t pad;
@@ -670,7 +663,7 @@ static int rxrpc_send_data(struct kiocb *iocb,
670 sp->hdr.serviceId = conn->service_id; 663 sp->hdr.serviceId = conn->service_id;
671 664
672 sp->hdr.flags = conn->out_clientflag; 665 sp->hdr.flags = conn->out_clientflag;
673 if (len == 0 && !more) 666 if (msg_data_left(msg) == 0 && !more)
674 sp->hdr.flags |= RXRPC_LAST_PACKET; 667 sp->hdr.flags |= RXRPC_LAST_PACKET;
675 else if (CIRC_SPACE(call->acks_head, call->acks_tail, 668 else if (CIRC_SPACE(call->acks_head, call->acks_tail,
676 call->acks_winsz) > 1) 669 call->acks_winsz) > 1)
@@ -686,10 +679,10 @@ static int rxrpc_send_data(struct kiocb *iocb,
686 679
687 memcpy(skb->head, &sp->hdr, 680 memcpy(skb->head, &sp->hdr,
688 sizeof(struct rxrpc_header)); 681 sizeof(struct rxrpc_header));
689 rxrpc_queue_packet(call, skb, !iov_iter_count(&msg->msg_iter) && !more); 682 rxrpc_queue_packet(call, skb, !msg_data_left(msg) && !more);
690 skb = NULL; 683 skb = NULL;
691 } 684 }
692 } 685 } while (msg_data_left(msg) > 0);
693 686
694success: 687success:
695 ret = copied; 688 ret = copied;
diff --git a/net/rxrpc/ar-recvmsg.c b/net/rxrpc/ar-recvmsg.c
index 19a560626dc4..b92beded7459 100644
--- a/net/rxrpc/ar-recvmsg.c
+++ b/net/rxrpc/ar-recvmsg.c
@@ -43,8 +43,8 @@ void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
43 * - we need to be careful about two or more threads calling recvmsg 43 * - we need to be careful about two or more threads calling recvmsg
44 * simultaneously 44 * simultaneously
45 */ 45 */
46int rxrpc_recvmsg(struct kiocb *iocb, struct socket *sock, 46int rxrpc_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
47 struct msghdr *msg, size_t len, int flags) 47 int flags)
48{ 48{
49 struct rxrpc_skb_priv *sp; 49 struct rxrpc_skb_priv *sp;
50 struct rxrpc_call *call = NULL, *continue_call = NULL; 50 struct rxrpc_call *call = NULL, *continue_call = NULL;
@@ -150,7 +150,7 @@ int rxrpc_recvmsg(struct kiocb *iocb, struct socket *sock,
150 &call->conn->trans->peer->srx, len); 150 &call->conn->trans->peer->srx, len);
151 msg->msg_namelen = len; 151 msg->msg_namelen = len;
152 } 152 }
153 sock_recv_ts_and_drops(msg, &rx->sk, skb); 153 sock_recv_timestamp(msg, &rx->sk, skb);
154 } 154 }
155 155
156 /* receive the message */ 156 /* receive the message */
diff --git a/net/sched/act_bpf.c b/net/sched/act_bpf.c
index 5f6288fa3f12..dc6a2d324bd8 100644
--- a/net/sched/act_bpf.c
+++ b/net/sched/act_bpf.c
@@ -13,26 +13,43 @@
13#include <linux/skbuff.h> 13#include <linux/skbuff.h>
14#include <linux/rtnetlink.h> 14#include <linux/rtnetlink.h>
15#include <linux/filter.h> 15#include <linux/filter.h>
16#include <linux/bpf.h>
17
16#include <net/netlink.h> 18#include <net/netlink.h>
17#include <net/pkt_sched.h> 19#include <net/pkt_sched.h>
18 20
19#include <linux/tc_act/tc_bpf.h> 21#include <linux/tc_act/tc_bpf.h>
20#include <net/tc_act/tc_bpf.h> 22#include <net/tc_act/tc_bpf.h>
21 23
22#define BPF_TAB_MASK 15 24#define BPF_TAB_MASK 15
25#define ACT_BPF_NAME_LEN 256
26
27struct tcf_bpf_cfg {
28 struct bpf_prog *filter;
29 struct sock_filter *bpf_ops;
30 char *bpf_name;
31 u32 bpf_fd;
32 u16 bpf_num_ops;
33};
23 34
24static int tcf_bpf(struct sk_buff *skb, const struct tc_action *a, 35static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
25 struct tcf_result *res) 36 struct tcf_result *res)
26{ 37{
27 struct tcf_bpf *b = a->priv; 38 struct tcf_bpf *prog = act->priv;
28 int action, filter_res; 39 int action, filter_res;
29 40
30 spin_lock(&b->tcf_lock); 41 if (unlikely(!skb_mac_header_was_set(skb)))
42 return TC_ACT_UNSPEC;
43
44 spin_lock(&prog->tcf_lock);
31 45
32 b->tcf_tm.lastuse = jiffies; 46 prog->tcf_tm.lastuse = jiffies;
33 bstats_update(&b->tcf_bstats, skb); 47 bstats_update(&prog->tcf_bstats, skb);
34 48
35 filter_res = BPF_PROG_RUN(b->filter, skb); 49 /* Needed here for accessing maps. */
50 rcu_read_lock();
51 filter_res = BPF_PROG_RUN(prog->filter, skb);
52 rcu_read_unlock();
36 53
37 /* A BPF program may overwrite the default action opcode. 54 /* A BPF program may overwrite the default action opcode.
38 * Similarly as in cls_bpf, if filter_res == -1 we use the 55 * Similarly as in cls_bpf, if filter_res == -1 we use the
@@ -52,52 +69,87 @@ static int tcf_bpf(struct sk_buff *skb, const struct tc_action *a,
52 break; 69 break;
53 case TC_ACT_SHOT: 70 case TC_ACT_SHOT:
54 action = filter_res; 71 action = filter_res;
55 b->tcf_qstats.drops++; 72 prog->tcf_qstats.drops++;
56 break; 73 break;
57 case TC_ACT_UNSPEC: 74 case TC_ACT_UNSPEC:
58 action = b->tcf_action; 75 action = prog->tcf_action;
59 break; 76 break;
60 default: 77 default:
61 action = TC_ACT_UNSPEC; 78 action = TC_ACT_UNSPEC;
62 break; 79 break;
63 } 80 }
64 81
65 spin_unlock(&b->tcf_lock); 82 spin_unlock(&prog->tcf_lock);
66 return action; 83 return action;
67} 84}
68 85
69static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *a, 86static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
87{
88 return !prog->bpf_ops;
89}
90
91static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
92 struct sk_buff *skb)
93{
94 struct nlattr *nla;
95
96 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
97 return -EMSGSIZE;
98
99 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
100 sizeof(struct sock_filter));
101 if (nla == NULL)
102 return -EMSGSIZE;
103
104 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
105
106 return 0;
107}
108
109static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
110 struct sk_buff *skb)
111{
112 if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
113 return -EMSGSIZE;
114
115 if (prog->bpf_name &&
116 nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
117 return -EMSGSIZE;
118
119 return 0;
120}
121
122static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
70 int bind, int ref) 123 int bind, int ref)
71{ 124{
72 unsigned char *tp = skb_tail_pointer(skb); 125 unsigned char *tp = skb_tail_pointer(skb);
73 struct tcf_bpf *b = a->priv; 126 struct tcf_bpf *prog = act->priv;
74 struct tc_act_bpf opt = { 127 struct tc_act_bpf opt = {
75 .index = b->tcf_index, 128 .index = prog->tcf_index,
76 .refcnt = b->tcf_refcnt - ref, 129 .refcnt = prog->tcf_refcnt - ref,
77 .bindcnt = b->tcf_bindcnt - bind, 130 .bindcnt = prog->tcf_bindcnt - bind,
78 .action = b->tcf_action, 131 .action = prog->tcf_action,
79 }; 132 };
80 struct tcf_t t; 133 struct tcf_t tm;
81 struct nlattr *nla; 134 int ret;
82 135
83 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt)) 136 if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
84 goto nla_put_failure; 137 goto nla_put_failure;
85 138
86 if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, b->bpf_num_ops)) 139 if (tcf_bpf_is_ebpf(prog))
87 goto nla_put_failure; 140 ret = tcf_bpf_dump_ebpf_info(prog, skb);
88 141 else
89 nla = nla_reserve(skb, TCA_ACT_BPF_OPS, b->bpf_num_ops * 142 ret = tcf_bpf_dump_bpf_info(prog, skb);
90 sizeof(struct sock_filter)); 143 if (ret)
91 if (!nla)
92 goto nla_put_failure; 144 goto nla_put_failure;
93 145
94 memcpy(nla_data(nla), b->bpf_ops, nla_len(nla)); 146 tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
147 tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
148 tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
95 149
96 t.install = jiffies_to_clock_t(jiffies - b->tcf_tm.install); 150 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
97 t.lastuse = jiffies_to_clock_t(jiffies - b->tcf_tm.lastuse);
98 t.expires = jiffies_to_clock_t(b->tcf_tm.expires);
99 if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(t), &t))
100 goto nla_put_failure; 151 goto nla_put_failure;
152
101 return skb->len; 153 return skb->len;
102 154
103nla_put_failure: 155nla_put_failure:
@@ -107,36 +159,21 @@ nla_put_failure:
107 159
108static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = { 160static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
109 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) }, 161 [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
162 [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
163 [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
110 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 }, 164 [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
111 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY, 165 [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
112 .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, 166 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
113}; 167};
114 168
115static int tcf_bpf_init(struct net *net, struct nlattr *nla, 169static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
116 struct nlattr *est, struct tc_action *a,
117 int ovr, int bind)
118{ 170{
119 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
120 struct tc_act_bpf *parm;
121 struct tcf_bpf *b;
122 u16 bpf_size, bpf_num_ops;
123 struct sock_filter *bpf_ops; 171 struct sock_filter *bpf_ops;
124 struct sock_fprog_kern tmp; 172 struct sock_fprog_kern fprog_tmp;
125 struct bpf_prog *fp; 173 struct bpf_prog *fp;
174 u16 bpf_size, bpf_num_ops;
126 int ret; 175 int ret;
127 176
128 if (!nla)
129 return -EINVAL;
130
131 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
132 if (ret < 0)
133 return ret;
134
135 if (!tb[TCA_ACT_BPF_PARMS] ||
136 !tb[TCA_ACT_BPF_OPS_LEN] || !tb[TCA_ACT_BPF_OPS])
137 return -EINVAL;
138 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
139
140 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]); 177 bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
141 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) 178 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
142 return -EINVAL; 179 return -EINVAL;
@@ -146,68 +183,165 @@ static int tcf_bpf_init(struct net *net, struct nlattr *nla,
146 return -EINVAL; 183 return -EINVAL;
147 184
148 bpf_ops = kzalloc(bpf_size, GFP_KERNEL); 185 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
149 if (!bpf_ops) 186 if (bpf_ops == NULL)
150 return -ENOMEM; 187 return -ENOMEM;
151 188
152 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size); 189 memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
153 190
154 tmp.len = bpf_num_ops; 191 fprog_tmp.len = bpf_num_ops;
155 tmp.filter = bpf_ops; 192 fprog_tmp.filter = bpf_ops;
156 193
157 ret = bpf_prog_create(&fp, &tmp); 194 ret = bpf_prog_create(&fp, &fprog_tmp);
158 if (ret) 195 if (ret < 0) {
159 goto free_bpf_ops; 196 kfree(bpf_ops);
197 return ret;
198 }
160 199
161 if (!tcf_hash_check(parm->index, a, bind)) { 200 cfg->bpf_ops = bpf_ops;
162 ret = tcf_hash_create(parm->index, est, a, sizeof(*b), bind); 201 cfg->bpf_num_ops = bpf_num_ops;
163 if (ret) 202 cfg->filter = fp;
203
204 return 0;
205}
206
207static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
208{
209 struct bpf_prog *fp;
210 char *name = NULL;
211 u32 bpf_fd;
212
213 bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
214
215 fp = bpf_prog_get(bpf_fd);
216 if (IS_ERR(fp))
217 return PTR_ERR(fp);
218
219 if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
220 bpf_prog_put(fp);
221 return -EINVAL;
222 }
223
224 if (tb[TCA_ACT_BPF_NAME]) {
225 name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
226 nla_len(tb[TCA_ACT_BPF_NAME]),
227 GFP_KERNEL);
228 if (!name) {
229 bpf_prog_put(fp);
230 return -ENOMEM;
231 }
232 }
233
234 cfg->bpf_fd = bpf_fd;
235 cfg->bpf_name = name;
236 cfg->filter = fp;
237
238 return 0;
239}
240
241static int tcf_bpf_init(struct net *net, struct nlattr *nla,
242 struct nlattr *est, struct tc_action *act,
243 int replace, int bind)
244{
245 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
246 struct tc_act_bpf *parm;
247 struct tcf_bpf *prog;
248 struct tcf_bpf_cfg cfg;
249 bool is_bpf, is_ebpf;
250 int ret;
251
252 if (!nla)
253 return -EINVAL;
254
255 ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
256 if (ret < 0)
257 return ret;
258
259 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
260 is_ebpf = tb[TCA_ACT_BPF_FD];
261
262 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
263 !tb[TCA_ACT_BPF_PARMS])
264 return -EINVAL;
265
266 parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
267
268 memset(&cfg, 0, sizeof(cfg));
269
270 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
271 tcf_bpf_init_from_efd(tb, &cfg);
272 if (ret < 0)
273 return ret;
274
275 if (!tcf_hash_check(parm->index, act, bind)) {
276 ret = tcf_hash_create(parm->index, est, act,
277 sizeof(*prog), bind);
278 if (ret < 0)
164 goto destroy_fp; 279 goto destroy_fp;
165 280
166 ret = ACT_P_CREATED; 281 ret = ACT_P_CREATED;
167 } else { 282 } else {
283 /* Don't override defaults. */
168 if (bind) 284 if (bind)
169 goto destroy_fp; 285 goto destroy_fp;
170 tcf_hash_release(a, bind); 286
171 if (!ovr) { 287 tcf_hash_release(act, bind);
288 if (!replace) {
172 ret = -EEXIST; 289 ret = -EEXIST;
173 goto destroy_fp; 290 goto destroy_fp;
174 } 291 }
175 } 292 }
176 293
177 b = to_bpf(a); 294 prog = to_bpf(act);
178 spin_lock_bh(&b->tcf_lock); 295 spin_lock_bh(&prog->tcf_lock);
179 b->tcf_action = parm->action; 296
180 b->bpf_num_ops = bpf_num_ops; 297 prog->bpf_ops = cfg.bpf_ops;
181 b->bpf_ops = bpf_ops; 298 prog->bpf_name = cfg.bpf_name;
182 b->filter = fp; 299
183 spin_unlock_bh(&b->tcf_lock); 300 if (cfg.bpf_num_ops)
301 prog->bpf_num_ops = cfg.bpf_num_ops;
302 if (cfg.bpf_fd)
303 prog->bpf_fd = cfg.bpf_fd;
304
305 prog->tcf_action = parm->action;
306 prog->filter = cfg.filter;
307
308 spin_unlock_bh(&prog->tcf_lock);
184 309
185 if (ret == ACT_P_CREATED) 310 if (ret == ACT_P_CREATED)
186 tcf_hash_insert(a); 311 tcf_hash_insert(act);
312
187 return ret; 313 return ret;
188 314
189destroy_fp: 315destroy_fp:
190 bpf_prog_destroy(fp); 316 if (is_ebpf)
191free_bpf_ops: 317 bpf_prog_put(cfg.filter);
192 kfree(bpf_ops); 318 else
319 bpf_prog_destroy(cfg.filter);
320
321 kfree(cfg.bpf_ops);
322 kfree(cfg.bpf_name);
323
193 return ret; 324 return ret;
194} 325}
195 326
196static void tcf_bpf_cleanup(struct tc_action *a, int bind) 327static void tcf_bpf_cleanup(struct tc_action *act, int bind)
197{ 328{
198 struct tcf_bpf *b = a->priv; 329 const struct tcf_bpf *prog = act->priv;
199 330
200 bpf_prog_destroy(b->filter); 331 if (tcf_bpf_is_ebpf(prog))
332 bpf_prog_put(prog->filter);
333 else
334 bpf_prog_destroy(prog->filter);
201} 335}
202 336
203static struct tc_action_ops act_bpf_ops = { 337static struct tc_action_ops act_bpf_ops __read_mostly = {
204 .kind = "bpf", 338 .kind = "bpf",
205 .type = TCA_ACT_BPF, 339 .type = TCA_ACT_BPF,
206 .owner = THIS_MODULE, 340 .owner = THIS_MODULE,
207 .act = tcf_bpf, 341 .act = tcf_bpf,
208 .dump = tcf_bpf_dump, 342 .dump = tcf_bpf_dump,
209 .cleanup = tcf_bpf_cleanup, 343 .cleanup = tcf_bpf_cleanup,
210 .init = tcf_bpf_init, 344 .init = tcf_bpf_init,
211}; 345};
212 346
213static int __init bpf_init_module(void) 347static int __init bpf_init_module(void)
diff --git a/net/sched/act_mirred.c b/net/sched/act_mirred.c
index 5953517ec059..3f63ceac8e01 100644
--- a/net/sched/act_mirred.c
+++ b/net/sched/act_mirred.c
@@ -157,7 +157,7 @@ static int tcf_mirred(struct sk_buff *skb, const struct tc_action *a,
157 157
158 if (!(at & AT_EGRESS)) { 158 if (!(at & AT_EGRESS)) {
159 if (m->tcfm_ok_push) 159 if (m->tcfm_ok_push)
160 skb_push(skb2, skb2->dev->hard_header_len); 160 skb_push(skb2, skb->mac_len);
161 } 161 }
162 162
163 /* mirror is always swallowed */ 163 /* mirror is always swallowed */
diff --git a/net/sched/cls_api.c b/net/sched/cls_api.c
index baef987fe2c0..8b0470e418dc 100644
--- a/net/sched/cls_api.c
+++ b/net/sched/cls_api.c
@@ -286,7 +286,7 @@ replay:
286 RCU_INIT_POINTER(*back, next); 286 RCU_INIT_POINTER(*back, next);
287 287
288 tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER); 288 tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
289 tcf_destroy(tp); 289 tcf_destroy(tp, true);
290 err = 0; 290 err = 0;
291 goto errout; 291 goto errout;
292 } 292 }
@@ -301,14 +301,20 @@ replay:
301 err = -EEXIST; 301 err = -EEXIST;
302 if (n->nlmsg_flags & NLM_F_EXCL) { 302 if (n->nlmsg_flags & NLM_F_EXCL) {
303 if (tp_created) 303 if (tp_created)
304 tcf_destroy(tp); 304 tcf_destroy(tp, true);
305 goto errout; 305 goto errout;
306 } 306 }
307 break; 307 break;
308 case RTM_DELTFILTER: 308 case RTM_DELTFILTER:
309 err = tp->ops->delete(tp, fh); 309 err = tp->ops->delete(tp, fh);
310 if (err == 0) 310 if (err == 0) {
311 tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER); 311 tfilter_notify(net, skb, n, tp, fh, RTM_DELTFILTER);
312 if (tcf_destroy(tp, false)) {
313 struct tcf_proto *next = rtnl_dereference(tp->next);
314
315 RCU_INIT_POINTER(*back, next);
316 }
317 }
312 goto errout; 318 goto errout;
313 case RTM_GETTFILTER: 319 case RTM_GETTFILTER:
314 err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER); 320 err = tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
@@ -329,7 +335,7 @@ replay:
329 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER); 335 tfilter_notify(net, skb, n, tp, fh, RTM_NEWTFILTER);
330 } else { 336 } else {
331 if (tp_created) 337 if (tp_created)
332 tcf_destroy(tp); 338 tcf_destroy(tp, true);
333 } 339 }
334 340
335errout: 341errout:
diff --git a/net/sched/cls_basic.c b/net/sched/cls_basic.c
index fc399db86f11..0b8c3ace671f 100644
--- a/net/sched/cls_basic.c
+++ b/net/sched/cls_basic.c
@@ -96,11 +96,14 @@ static void basic_delete_filter(struct rcu_head *head)
96 kfree(f); 96 kfree(f);
97} 97}
98 98
99static void basic_destroy(struct tcf_proto *tp) 99static bool basic_destroy(struct tcf_proto *tp, bool force)
100{ 100{
101 struct basic_head *head = rtnl_dereference(tp->root); 101 struct basic_head *head = rtnl_dereference(tp->root);
102 struct basic_filter *f, *n; 102 struct basic_filter *f, *n;
103 103
104 if (!force && !list_empty(&head->flist))
105 return false;
106
104 list_for_each_entry_safe(f, n, &head->flist, link) { 107 list_for_each_entry_safe(f, n, &head->flist, link) {
105 list_del_rcu(&f->link); 108 list_del_rcu(&f->link);
106 tcf_unbind_filter(tp, &f->res); 109 tcf_unbind_filter(tp, &f->res);
@@ -108,6 +111,7 @@ static void basic_destroy(struct tcf_proto *tp)
108 } 111 }
109 RCU_INIT_POINTER(tp->root, NULL); 112 RCU_INIT_POINTER(tp->root, NULL);
110 kfree_rcu(head, rcu); 113 kfree_rcu(head, rcu);
114 return true;
111} 115}
112 116
113static int basic_delete(struct tcf_proto *tp, unsigned long arg) 117static int basic_delete(struct tcf_proto *tp, unsigned long arg)
diff --git a/net/sched/cls_bpf.c b/net/sched/cls_bpf.c
index 5f3ee9e4b5bf..91bd9c19471d 100644
--- a/net/sched/cls_bpf.c
+++ b/net/sched/cls_bpf.c
@@ -16,6 +16,8 @@
16#include <linux/types.h> 16#include <linux/types.h>
17#include <linux/skbuff.h> 17#include <linux/skbuff.h>
18#include <linux/filter.h> 18#include <linux/filter.h>
19#include <linux/bpf.h>
20
19#include <net/rtnetlink.h> 21#include <net/rtnetlink.h>
20#include <net/pkt_cls.h> 22#include <net/pkt_cls.h>
21#include <net/sock.h> 23#include <net/sock.h>
@@ -24,6 +26,8 @@ MODULE_LICENSE("GPL");
24MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); 26MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
25MODULE_DESCRIPTION("TC BPF based classifier"); 27MODULE_DESCRIPTION("TC BPF based classifier");
26 28
29#define CLS_BPF_NAME_LEN 256
30
27struct cls_bpf_head { 31struct cls_bpf_head {
28 struct list_head plist; 32 struct list_head plist;
29 u32 hgen; 33 u32 hgen;
@@ -32,18 +36,24 @@ struct cls_bpf_head {
32 36
33struct cls_bpf_prog { 37struct cls_bpf_prog {
34 struct bpf_prog *filter; 38 struct bpf_prog *filter;
35 struct sock_filter *bpf_ops;
36 struct tcf_exts exts;
37 struct tcf_result res;
38 struct list_head link; 39 struct list_head link;
40 struct tcf_result res;
41 struct tcf_exts exts;
39 u32 handle; 42 u32 handle;
40 u16 bpf_num_ops; 43 union {
44 u32 bpf_fd;
45 u16 bpf_num_ops;
46 };
47 struct sock_filter *bpf_ops;
48 const char *bpf_name;
41 struct tcf_proto *tp; 49 struct tcf_proto *tp;
42 struct rcu_head rcu; 50 struct rcu_head rcu;
43}; 51};
44 52
45static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { 53static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
46 [TCA_BPF_CLASSID] = { .type = NLA_U32 }, 54 [TCA_BPF_CLASSID] = { .type = NLA_U32 },
55 [TCA_BPF_FD] = { .type = NLA_U32 },
56 [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN },
47 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, 57 [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
48 [TCA_BPF_OPS] = { .type = NLA_BINARY, 58 [TCA_BPF_OPS] = { .type = NLA_BINARY,
49 .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, 59 .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
@@ -54,8 +64,13 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
54{ 64{
55 struct cls_bpf_head *head = rcu_dereference_bh(tp->root); 65 struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
56 struct cls_bpf_prog *prog; 66 struct cls_bpf_prog *prog;
57 int ret; 67 int ret = -1;
68
69 if (unlikely(!skb_mac_header_was_set(skb)))
70 return -1;
58 71
72 /* Needed here for accessing maps. */
73 rcu_read_lock();
59 list_for_each_entry_rcu(prog, &head->plist, link) { 74 list_for_each_entry_rcu(prog, &head->plist, link) {
60 int filter_res = BPF_PROG_RUN(prog->filter, skb); 75 int filter_res = BPF_PROG_RUN(prog->filter, skb);
61 76
@@ -70,10 +85,16 @@ static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
70 if (ret < 0) 85 if (ret < 0)
71 continue; 86 continue;
72 87
73 return ret; 88 break;
74 } 89 }
90 rcu_read_unlock();
75 91
76 return -1; 92 return ret;
93}
94
95static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
96{
97 return !prog->bpf_ops;
77} 98}
78 99
79static int cls_bpf_init(struct tcf_proto *tp) 100static int cls_bpf_init(struct tcf_proto *tp)
@@ -94,8 +115,12 @@ static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
94{ 115{
95 tcf_exts_destroy(&prog->exts); 116 tcf_exts_destroy(&prog->exts);
96 117
97 bpf_prog_destroy(prog->filter); 118 if (cls_bpf_is_ebpf(prog))
119 bpf_prog_put(prog->filter);
120 else
121 bpf_prog_destroy(prog->filter);
98 122
123 kfree(prog->bpf_name);
99 kfree(prog->bpf_ops); 124 kfree(prog->bpf_ops);
100 kfree(prog); 125 kfree(prog);
101} 126}
@@ -114,14 +139,18 @@ static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
114 list_del_rcu(&prog->link); 139 list_del_rcu(&prog->link);
115 tcf_unbind_filter(tp, &prog->res); 140 tcf_unbind_filter(tp, &prog->res);
116 call_rcu(&prog->rcu, __cls_bpf_delete_prog); 141 call_rcu(&prog->rcu, __cls_bpf_delete_prog);
142
117 return 0; 143 return 0;
118} 144}
119 145
120static void cls_bpf_destroy(struct tcf_proto *tp) 146static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
121{ 147{
122 struct cls_bpf_head *head = rtnl_dereference(tp->root); 148 struct cls_bpf_head *head = rtnl_dereference(tp->root);
123 struct cls_bpf_prog *prog, *tmp; 149 struct cls_bpf_prog *prog, *tmp;
124 150
151 if (!force && !list_empty(&head->plist))
152 return false;
153
125 list_for_each_entry_safe(prog, tmp, &head->plist, link) { 154 list_for_each_entry_safe(prog, tmp, &head->plist, link) {
126 list_del_rcu(&prog->link); 155 list_del_rcu(&prog->link);
127 tcf_unbind_filter(tp, &prog->res); 156 tcf_unbind_filter(tp, &prog->res);
@@ -130,6 +159,7 @@ static void cls_bpf_destroy(struct tcf_proto *tp)
130 159
131 RCU_INIT_POINTER(tp->root, NULL); 160 RCU_INIT_POINTER(tp->root, NULL);
132 kfree_rcu(head, rcu); 161 kfree_rcu(head, rcu);
162 return true;
133} 163}
134 164
135static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) 165static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
@@ -151,69 +181,121 @@ static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
151 return ret; 181 return ret;
152} 182}
153 183
154static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, 184static int cls_bpf_prog_from_ops(struct nlattr **tb,
155 struct cls_bpf_prog *prog, 185 struct cls_bpf_prog *prog, u32 classid)
156 unsigned long base, struct nlattr **tb,
157 struct nlattr *est, bool ovr)
158{ 186{
159 struct sock_filter *bpf_ops; 187 struct sock_filter *bpf_ops;
160 struct tcf_exts exts; 188 struct sock_fprog_kern fprog_tmp;
161 struct sock_fprog_kern tmp;
162 struct bpf_prog *fp; 189 struct bpf_prog *fp;
163 u16 bpf_size, bpf_num_ops; 190 u16 bpf_size, bpf_num_ops;
164 u32 classid;
165 int ret; 191 int ret;
166 192
167 if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
168 return -EINVAL;
169
170 tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
171 ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
172 if (ret < 0)
173 return ret;
174
175 classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
176 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); 193 bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
177 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) { 194 if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
178 ret = -EINVAL; 195 return -EINVAL;
179 goto errout;
180 }
181 196
182 bpf_size = bpf_num_ops * sizeof(*bpf_ops); 197 bpf_size = bpf_num_ops * sizeof(*bpf_ops);
183 if (bpf_size != nla_len(tb[TCA_BPF_OPS])) { 198 if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
184 ret = -EINVAL; 199 return -EINVAL;
185 goto errout;
186 }
187 200
188 bpf_ops = kzalloc(bpf_size, GFP_KERNEL); 201 bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
189 if (bpf_ops == NULL) { 202 if (bpf_ops == NULL)
190 ret = -ENOMEM; 203 return -ENOMEM;
191 goto errout;
192 }
193 204
194 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); 205 memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
195 206
196 tmp.len = bpf_num_ops; 207 fprog_tmp.len = bpf_num_ops;
197 tmp.filter = bpf_ops; 208 fprog_tmp.filter = bpf_ops;
198 209
199 ret = bpf_prog_create(&fp, &tmp); 210 ret = bpf_prog_create(&fp, &fprog_tmp);
200 if (ret) 211 if (ret < 0) {
201 goto errout_free; 212 kfree(bpf_ops);
213 return ret;
214 }
202 215
203 prog->bpf_num_ops = bpf_num_ops;
204 prog->bpf_ops = bpf_ops; 216 prog->bpf_ops = bpf_ops;
217 prog->bpf_num_ops = bpf_num_ops;
218 prog->bpf_name = NULL;
219
205 prog->filter = fp; 220 prog->filter = fp;
206 prog->res.classid = classid; 221 prog->res.classid = classid;
207 222
223 return 0;
224}
225
226static int cls_bpf_prog_from_efd(struct nlattr **tb,
227 struct cls_bpf_prog *prog, u32 classid)
228{
229 struct bpf_prog *fp;
230 char *name = NULL;
231 u32 bpf_fd;
232
233 bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
234
235 fp = bpf_prog_get(bpf_fd);
236 if (IS_ERR(fp))
237 return PTR_ERR(fp);
238
239 if (fp->type != BPF_PROG_TYPE_SCHED_CLS) {
240 bpf_prog_put(fp);
241 return -EINVAL;
242 }
243
244 if (tb[TCA_BPF_NAME]) {
245 name = kmemdup(nla_data(tb[TCA_BPF_NAME]),
246 nla_len(tb[TCA_BPF_NAME]),
247 GFP_KERNEL);
248 if (!name) {
249 bpf_prog_put(fp);
250 return -ENOMEM;
251 }
252 }
253
254 prog->bpf_ops = NULL;
255 prog->bpf_fd = bpf_fd;
256 prog->bpf_name = name;
257
258 prog->filter = fp;
259 prog->res.classid = classid;
260
261 return 0;
262}
263
264static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
265 struct cls_bpf_prog *prog,
266 unsigned long base, struct nlattr **tb,
267 struct nlattr *est, bool ovr)
268{
269 struct tcf_exts exts;
270 bool is_bpf, is_ebpf;
271 u32 classid;
272 int ret;
273
274 is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
275 is_ebpf = tb[TCA_BPF_FD];
276
277 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
278 !tb[TCA_BPF_CLASSID])
279 return -EINVAL;
280
281 tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
282 ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
283 if (ret < 0)
284 return ret;
285
286 classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
287
288 ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog, classid) :
289 cls_bpf_prog_from_efd(tb, prog, classid);
290 if (ret < 0) {
291 tcf_exts_destroy(&exts);
292 return ret;
293 }
294
208 tcf_bind_filter(tp, &prog->res, base); 295 tcf_bind_filter(tp, &prog->res, base);
209 tcf_exts_change(tp, &prog->exts, &exts); 296 tcf_exts_change(tp, &prog->exts, &exts);
210 297
211 return 0; 298 return 0;
212errout_free:
213 kfree(bpf_ops);
214errout:
215 tcf_exts_destroy(&exts);
216 return ret;
217} 299}
218 300
219static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, 301static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
@@ -297,11 +379,43 @@ errout:
297 return ret; 379 return ret;
298} 380}
299 381
382static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
383 struct sk_buff *skb)
384{
385 struct nlattr *nla;
386
387 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
388 return -EMSGSIZE;
389
390 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
391 sizeof(struct sock_filter));
392 if (nla == NULL)
393 return -EMSGSIZE;
394
395 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
396
397 return 0;
398}
399
400static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
401 struct sk_buff *skb)
402{
403 if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd))
404 return -EMSGSIZE;
405
406 if (prog->bpf_name &&
407 nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
408 return -EMSGSIZE;
409
410 return 0;
411}
412
300static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, 413static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
301 struct sk_buff *skb, struct tcmsg *tm) 414 struct sk_buff *skb, struct tcmsg *tm)
302{ 415{
303 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; 416 struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
304 struct nlattr *nest, *nla; 417 struct nlattr *nest;
418 int ret;
305 419
306 if (prog == NULL) 420 if (prog == NULL)
307 return skb->len; 421 return skb->len;
@@ -314,16 +428,14 @@ static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
314 428
315 if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) 429 if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
316 goto nla_put_failure; 430 goto nla_put_failure;
317 if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
318 goto nla_put_failure;
319 431
320 nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * 432 if (cls_bpf_is_ebpf(prog))
321 sizeof(struct sock_filter)); 433 ret = cls_bpf_dump_ebpf_info(prog, skb);
322 if (nla == NULL) 434 else
435 ret = cls_bpf_dump_bpf_info(prog, skb);
436 if (ret)
323 goto nla_put_failure; 437 goto nla_put_failure;
324 438
325 memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
326
327 if (tcf_exts_dump(skb, &prog->exts) < 0) 439 if (tcf_exts_dump(skb, &prog->exts) < 0)
328 goto nla_put_failure; 440 goto nla_put_failure;
329 441
diff --git a/net/sched/cls_cgroup.c b/net/sched/cls_cgroup.c
index 221697ab0247..ea611b216412 100644
--- a/net/sched/cls_cgroup.c
+++ b/net/sched/cls_cgroup.c
@@ -143,14 +143,18 @@ errout:
143 return err; 143 return err;
144} 144}
145 145
146static void cls_cgroup_destroy(struct tcf_proto *tp) 146static bool cls_cgroup_destroy(struct tcf_proto *tp, bool force)
147{ 147{
148 struct cls_cgroup_head *head = rtnl_dereference(tp->root); 148 struct cls_cgroup_head *head = rtnl_dereference(tp->root);
149 149
150 if (!force)
151 return false;
152
150 if (head) { 153 if (head) {
151 RCU_INIT_POINTER(tp->root, NULL); 154 RCU_INIT_POINTER(tp->root, NULL);
152 call_rcu(&head->rcu, cls_cgroup_destroy_rcu); 155 call_rcu(&head->rcu, cls_cgroup_destroy_rcu);
153 } 156 }
157 return true;
154} 158}
155 159
156static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg) 160static int cls_cgroup_delete(struct tcf_proto *tp, unsigned long arg)
diff --git a/net/sched/cls_flow.c b/net/sched/cls_flow.c
index 461410394d08..a620c4e288a5 100644
--- a/net/sched/cls_flow.c
+++ b/net/sched/cls_flow.c
@@ -557,17 +557,21 @@ static int flow_init(struct tcf_proto *tp)
557 return 0; 557 return 0;
558} 558}
559 559
560static void flow_destroy(struct tcf_proto *tp) 560static bool flow_destroy(struct tcf_proto *tp, bool force)
561{ 561{
562 struct flow_head *head = rtnl_dereference(tp->root); 562 struct flow_head *head = rtnl_dereference(tp->root);
563 struct flow_filter *f, *next; 563 struct flow_filter *f, *next;
564 564
565 if (!force && !list_empty(&head->filters))
566 return false;
567
565 list_for_each_entry_safe(f, next, &head->filters, list) { 568 list_for_each_entry_safe(f, next, &head->filters, list) {
566 list_del_rcu(&f->list); 569 list_del_rcu(&f->list);
567 call_rcu(&f->rcu, flow_destroy_filter); 570 call_rcu(&f->rcu, flow_destroy_filter);
568 } 571 }
569 RCU_INIT_POINTER(tp->root, NULL); 572 RCU_INIT_POINTER(tp->root, NULL);
570 kfree_rcu(head, rcu); 573 kfree_rcu(head, rcu);
574 return true;
571} 575}
572 576
573static unsigned long flow_get(struct tcf_proto *tp, u32 handle) 577static unsigned long flow_get(struct tcf_proto *tp, u32 handle)
diff --git a/net/sched/cls_fw.c b/net/sched/cls_fw.c
index a5269f76004c..715e01e5910a 100644
--- a/net/sched/cls_fw.c
+++ b/net/sched/cls_fw.c
@@ -33,6 +33,7 @@
33 33
34struct fw_head { 34struct fw_head {
35 u32 mask; 35 u32 mask;
36 bool mask_set;
36 struct fw_filter __rcu *ht[HTSIZE]; 37 struct fw_filter __rcu *ht[HTSIZE];
37 struct rcu_head rcu; 38 struct rcu_head rcu;
38}; 39};
@@ -113,6 +114,14 @@ static unsigned long fw_get(struct tcf_proto *tp, u32 handle)
113 114
114static int fw_init(struct tcf_proto *tp) 115static int fw_init(struct tcf_proto *tp)
115{ 116{
117 struct fw_head *head;
118
119 head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
120 if (head == NULL)
121 return -ENOBUFS;
122
123 head->mask_set = false;
124 rcu_assign_pointer(tp->root, head);
116 return 0; 125 return 0;
117} 126}
118 127
@@ -124,14 +133,20 @@ static void fw_delete_filter(struct rcu_head *head)
124 kfree(f); 133 kfree(f);
125} 134}
126 135
127static void fw_destroy(struct tcf_proto *tp) 136static bool fw_destroy(struct tcf_proto *tp, bool force)
128{ 137{
129 struct fw_head *head = rtnl_dereference(tp->root); 138 struct fw_head *head = rtnl_dereference(tp->root);
130 struct fw_filter *f; 139 struct fw_filter *f;
131 int h; 140 int h;
132 141
133 if (head == NULL) 142 if (head == NULL)
134 return; 143 return true;
144
145 if (!force) {
146 for (h = 0; h < HTSIZE; h++)
147 if (rcu_access_pointer(head->ht[h]))
148 return false;
149 }
135 150
136 for (h = 0; h < HTSIZE; h++) { 151 for (h = 0; h < HTSIZE; h++) {
137 while ((f = rtnl_dereference(head->ht[h])) != NULL) { 152 while ((f = rtnl_dereference(head->ht[h])) != NULL) {
@@ -143,6 +158,7 @@ static void fw_destroy(struct tcf_proto *tp)
143 } 158 }
144 RCU_INIT_POINTER(tp->root, NULL); 159 RCU_INIT_POINTER(tp->root, NULL);
145 kfree_rcu(head, rcu); 160 kfree_rcu(head, rcu);
161 return true;
146} 162}
147 163
148static int fw_delete(struct tcf_proto *tp, unsigned long arg) 164static int fw_delete(struct tcf_proto *tp, unsigned long arg)
@@ -286,17 +302,11 @@ static int fw_change(struct net *net, struct sk_buff *in_skb,
286 if (!handle) 302 if (!handle)
287 return -EINVAL; 303 return -EINVAL;
288 304
289 if (head == NULL) { 305 if (!head->mask_set) {
290 u32 mask = 0xFFFFFFFF; 306 head->mask = 0xFFFFFFFF;
291 if (tb[TCA_FW_MASK]) 307 if (tb[TCA_FW_MASK])
292 mask = nla_get_u32(tb[TCA_FW_MASK]); 308 head->mask = nla_get_u32(tb[TCA_FW_MASK]);
293 309 head->mask_set = true;
294 head = kzalloc(sizeof(struct fw_head), GFP_KERNEL);
295 if (head == NULL)
296 return -ENOBUFS;
297 head->mask = mask;
298
299 rcu_assign_pointer(tp->root, head);
300 } 310 }
301 311
302 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL); 312 f = kzalloc(sizeof(struct fw_filter), GFP_KERNEL);
diff --git a/net/sched/cls_route.c b/net/sched/cls_route.c
index 2ecd24688554..08a3b0a6f5ab 100644
--- a/net/sched/cls_route.c
+++ b/net/sched/cls_route.c
@@ -258,6 +258,13 @@ static unsigned long route4_get(struct tcf_proto *tp, u32 handle)
258 258
259static int route4_init(struct tcf_proto *tp) 259static int route4_init(struct tcf_proto *tp)
260{ 260{
261 struct route4_head *head;
262
263 head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
264 if (head == NULL)
265 return -ENOBUFS;
266
267 rcu_assign_pointer(tp->root, head);
261 return 0; 268 return 0;
262} 269}
263 270
@@ -270,13 +277,20 @@ route4_delete_filter(struct rcu_head *head)
270 kfree(f); 277 kfree(f);
271} 278}
272 279
273static void route4_destroy(struct tcf_proto *tp) 280static bool route4_destroy(struct tcf_proto *tp, bool force)
274{ 281{
275 struct route4_head *head = rtnl_dereference(tp->root); 282 struct route4_head *head = rtnl_dereference(tp->root);
276 int h1, h2; 283 int h1, h2;
277 284
278 if (head == NULL) 285 if (head == NULL)
279 return; 286 return true;
287
288 if (!force) {
289 for (h1 = 0; h1 <= 256; h1++) {
290 if (rcu_access_pointer(head->table[h1]))
291 return false;
292 }
293 }
280 294
281 for (h1 = 0; h1 <= 256; h1++) { 295 for (h1 = 0; h1 <= 256; h1++) {
282 struct route4_bucket *b; 296 struct route4_bucket *b;
@@ -301,6 +315,7 @@ static void route4_destroy(struct tcf_proto *tp)
301 } 315 }
302 RCU_INIT_POINTER(tp->root, NULL); 316 RCU_INIT_POINTER(tp->root, NULL);
303 kfree_rcu(head, rcu); 317 kfree_rcu(head, rcu);
318 return true;
304} 319}
305 320
306static int route4_delete(struct tcf_proto *tp, unsigned long arg) 321static int route4_delete(struct tcf_proto *tp, unsigned long arg)
@@ -484,13 +499,6 @@ static int route4_change(struct net *net, struct sk_buff *in_skb,
484 return -EINVAL; 499 return -EINVAL;
485 500
486 err = -ENOBUFS; 501 err = -ENOBUFS;
487 if (head == NULL) {
488 head = kzalloc(sizeof(struct route4_head), GFP_KERNEL);
489 if (head == NULL)
490 goto errout;
491 rcu_assign_pointer(tp->root, head);
492 }
493
494 f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL); 502 f = kzalloc(sizeof(struct route4_filter), GFP_KERNEL);
495 if (!f) 503 if (!f)
496 goto errout; 504 goto errout;
diff --git a/net/sched/cls_rsvp.h b/net/sched/cls_rsvp.h
index edd8ade3fbc1..02fa82792dab 100644
--- a/net/sched/cls_rsvp.h
+++ b/net/sched/cls_rsvp.h
@@ -291,13 +291,20 @@ rsvp_delete_filter(struct tcf_proto *tp, struct rsvp_filter *f)
291 kfree_rcu(f, rcu); 291 kfree_rcu(f, rcu);
292} 292}
293 293
294static void rsvp_destroy(struct tcf_proto *tp) 294static bool rsvp_destroy(struct tcf_proto *tp, bool force)
295{ 295{
296 struct rsvp_head *data = rtnl_dereference(tp->root); 296 struct rsvp_head *data = rtnl_dereference(tp->root);
297 int h1, h2; 297 int h1, h2;
298 298
299 if (data == NULL) 299 if (data == NULL)
300 return; 300 return true;
301
302 if (!force) {
303 for (h1 = 0; h1 < 256; h1++) {
304 if (rcu_access_pointer(data->ht[h1]))
305 return false;
306 }
307 }
301 308
302 RCU_INIT_POINTER(tp->root, NULL); 309 RCU_INIT_POINTER(tp->root, NULL);
303 310
@@ -319,6 +326,7 @@ static void rsvp_destroy(struct tcf_proto *tp)
319 } 326 }
320 } 327 }
321 kfree_rcu(data, rcu); 328 kfree_rcu(data, rcu);
329 return true;
322} 330}
323 331
324static int rsvp_delete(struct tcf_proto *tp, unsigned long arg) 332static int rsvp_delete(struct tcf_proto *tp, unsigned long arg)
diff --git a/net/sched/cls_tcindex.c b/net/sched/cls_tcindex.c
index bd49bf547a47..a557dbaf5afe 100644
--- a/net/sched/cls_tcindex.c
+++ b/net/sched/cls_tcindex.c
@@ -468,11 +468,14 @@ static void tcindex_walk(struct tcf_proto *tp, struct tcf_walker *walker)
468 } 468 }
469} 469}
470 470
471static void tcindex_destroy(struct tcf_proto *tp) 471static bool tcindex_destroy(struct tcf_proto *tp, bool force)
472{ 472{
473 struct tcindex_data *p = rtnl_dereference(tp->root); 473 struct tcindex_data *p = rtnl_dereference(tp->root);
474 struct tcf_walker walker; 474 struct tcf_walker walker;
475 475
476 if (!force)
477 return false;
478
476 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p); 479 pr_debug("tcindex_destroy(tp %p),p %p\n", tp, p);
477 walker.count = 0; 480 walker.count = 0;
478 walker.skip = 0; 481 walker.skip = 0;
@@ -481,6 +484,7 @@ static void tcindex_destroy(struct tcf_proto *tp)
481 484
482 RCU_INIT_POINTER(tp->root, NULL); 485 RCU_INIT_POINTER(tp->root, NULL);
483 call_rcu(&p->rcu, __tcindex_destroy); 486 call_rcu(&p->rcu, __tcindex_destroy);
487 return true;
484} 488}
485 489
486 490
diff --git a/net/sched/cls_u32.c b/net/sched/cls_u32.c
index 95fdf4e40051..cab9e9b43967 100644
--- a/net/sched/cls_u32.c
+++ b/net/sched/cls_u32.c
@@ -463,13 +463,35 @@ static int u32_destroy_hnode(struct tcf_proto *tp, struct tc_u_hnode *ht)
463 return -ENOENT; 463 return -ENOENT;
464} 464}
465 465
466static void u32_destroy(struct tcf_proto *tp) 466static bool ht_empty(struct tc_u_hnode *ht)
467{
468 unsigned int h;
469
470 for (h = 0; h <= ht->divisor; h++)
471 if (rcu_access_pointer(ht->ht[h]))
472 return false;
473
474 return true;
475}
476
477static bool u32_destroy(struct tcf_proto *tp, bool force)
467{ 478{
468 struct tc_u_common *tp_c = tp->data; 479 struct tc_u_common *tp_c = tp->data;
469 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root); 480 struct tc_u_hnode *root_ht = rtnl_dereference(tp->root);
470 481
471 WARN_ON(root_ht == NULL); 482 WARN_ON(root_ht == NULL);
472 483
484 if (!force) {
485 if (root_ht) {
486 if (root_ht->refcnt > 1)
487 return false;
488 if (root_ht->refcnt == 1) {
489 if (!ht_empty(root_ht))
490 return false;
491 }
492 }
493 }
494
473 if (root_ht && --root_ht->refcnt == 0) 495 if (root_ht && --root_ht->refcnt == 0)
474 u32_destroy_hnode(tp, root_ht); 496 u32_destroy_hnode(tp, root_ht);
475 497
@@ -494,6 +516,7 @@ static void u32_destroy(struct tcf_proto *tp)
494 } 516 }
495 517
496 tp->data = NULL; 518 tp->data = NULL;
519 return true;
497} 520}
498 521
499static int u32_delete(struct tcf_proto *tp, unsigned long arg) 522static int u32_delete(struct tcf_proto *tp, unsigned long arg)
diff --git a/net/sched/em_text.c b/net/sched/em_text.c
index f03c3de16c27..73e2ed576ceb 100644
--- a/net/sched/em_text.c
+++ b/net/sched/em_text.c
@@ -34,7 +34,6 @@ static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
34{ 34{
35 struct text_match *tm = EM_TEXT_PRIV(m); 35 struct text_match *tm = EM_TEXT_PRIV(m);
36 int from, to; 36 int from, to;
37 struct ts_state state;
38 37
39 from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data; 38 from = tcf_get_base_ptr(skb, tm->from_layer) - skb->data;
40 from += tm->from_offset; 39 from += tm->from_offset;
@@ -42,7 +41,7 @@ static int em_text_match(struct sk_buff *skb, struct tcf_ematch *m,
42 to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data; 41 to = tcf_get_base_ptr(skb, tm->to_layer) - skb->data;
43 to += tm->to_offset; 42 to += tm->to_offset;
44 43
45 return skb_find_text(skb, from, to, tm->config, &state) != UINT_MAX; 44 return skb_find_text(skb, from, to, tm->config) != UINT_MAX;
46} 45}
47 46
48static int em_text_change(struct net *net, void *data, int len, 47static int em_text_change(struct net *net, void *data, int len,
diff --git a/net/sched/sch_api.c b/net/sched/sch_api.c
index 243b7d169d61..ad9eed70bc8f 100644
--- a/net/sched/sch_api.c
+++ b/net/sched/sch_api.c
@@ -1858,11 +1858,15 @@ reclassify:
1858} 1858}
1859EXPORT_SYMBOL(tc_classify); 1859EXPORT_SYMBOL(tc_classify);
1860 1860
1861void tcf_destroy(struct tcf_proto *tp) 1861bool tcf_destroy(struct tcf_proto *tp, bool force)
1862{ 1862{
1863 tp->ops->destroy(tp); 1863 if (tp->ops->destroy(tp, force)) {
1864 module_put(tp->ops->owner); 1864 module_put(tp->ops->owner);
1865 kfree_rcu(tp, rcu); 1865 kfree_rcu(tp, rcu);
1866 return true;
1867 }
1868
1869 return false;
1866} 1870}
1867 1871
1868void tcf_destroy_chain(struct tcf_proto __rcu **fl) 1872void tcf_destroy_chain(struct tcf_proto __rcu **fl)
@@ -1871,7 +1875,7 @@ void tcf_destroy_chain(struct tcf_proto __rcu **fl)
1871 1875
1872 while ((tp = rtnl_dereference(*fl)) != NULL) { 1876 while ((tp = rtnl_dereference(*fl)) != NULL) {
1873 RCU_INIT_POINTER(*fl, tp->next); 1877 RCU_INIT_POINTER(*fl, tp->next);
1874 tcf_destroy(tp); 1878 tcf_destroy(tp, true);
1875 } 1879 }
1876} 1880}
1877EXPORT_SYMBOL(tcf_destroy_chain); 1881EXPORT_SYMBOL(tcf_destroy_chain);
diff --git a/net/sched/sch_fq.c b/net/sched/sch_fq.c
index dfcea20e3171..f377702d4b91 100644
--- a/net/sched/sch_fq.c
+++ b/net/sched/sch_fq.c
@@ -8,7 +8,7 @@
8 * as published by the Free Software Foundation; either version 8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version. 9 * 2 of the License, or (at your option) any later version.
10 * 10 *
11 * Meant to be mostly used for localy generated traffic : 11 * Meant to be mostly used for locally generated traffic :
12 * Fast classification depends on skb->sk being set before reaching us. 12 * Fast classification depends on skb->sk being set before reaching us.
13 * If not, (router workload), we use rxhash as fallback, with 32 bits wide hash. 13 * If not, (router workload), we use rxhash as fallback, with 32 bits wide hash.
14 * All packets belonging to a socket are considered as a 'flow'. 14 * All packets belonging to a socket are considered as a 'flow'.
@@ -63,7 +63,7 @@ struct fq_flow {
63 struct sk_buff *tail; /* last skb in the list */ 63 struct sk_buff *tail; /* last skb in the list */
64 unsigned long age; /* jiffies when flow was emptied, for gc */ 64 unsigned long age; /* jiffies when flow was emptied, for gc */
65 }; 65 };
66 struct rb_node fq_node; /* anchor in fq_root[] trees */ 66 struct rb_node fq_node; /* anchor in fq_root[] trees */
67 struct sock *sk; 67 struct sock *sk;
68 int qlen; /* number of packets in flow queue */ 68 int qlen; /* number of packets in flow queue */
69 int credit; 69 int credit;
diff --git a/net/sched/sch_ingress.c b/net/sched/sch_ingress.c
index eb5b8445fef9..4cdbfb85686a 100644
--- a/net/sched/sch_ingress.c
+++ b/net/sched/sch_ingress.c
@@ -88,11 +88,19 @@ static int ingress_enqueue(struct sk_buff *skb, struct Qdisc *sch)
88 88
89/* ------------------------------------------------------------- */ 89/* ------------------------------------------------------------- */
90 90
91static int ingress_init(struct Qdisc *sch, struct nlattr *opt)
92{
93 net_inc_ingress_queue();
94
95 return 0;
96}
97
91static void ingress_destroy(struct Qdisc *sch) 98static void ingress_destroy(struct Qdisc *sch)
92{ 99{
93 struct ingress_qdisc_data *p = qdisc_priv(sch); 100 struct ingress_qdisc_data *p = qdisc_priv(sch);
94 101
95 tcf_destroy_chain(&p->filter_list); 102 tcf_destroy_chain(&p->filter_list);
103 net_dec_ingress_queue();
96} 104}
97 105
98static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb) 106static int ingress_dump(struct Qdisc *sch, struct sk_buff *skb)
@@ -124,6 +132,7 @@ static struct Qdisc_ops ingress_qdisc_ops __read_mostly = {
124 .id = "ingress", 132 .id = "ingress",
125 .priv_size = sizeof(struct ingress_qdisc_data), 133 .priv_size = sizeof(struct ingress_qdisc_data),
126 .enqueue = ingress_enqueue, 134 .enqueue = ingress_enqueue,
135 .init = ingress_init,
127 .destroy = ingress_destroy, 136 .destroy = ingress_destroy,
128 .dump = ingress_dump, 137 .dump = ingress_dump,
129 .owner = THIS_MODULE, 138 .owner = THIS_MODULE,
diff --git a/net/sched/sch_netem.c b/net/sched/sch_netem.c
index 179f1c8c0d8b..956ead2cab9a 100644
--- a/net/sched/sch_netem.c
+++ b/net/sched/sch_netem.c
@@ -560,8 +560,8 @@ static struct sk_buff *netem_dequeue(struct Qdisc *sch)
560tfifo_dequeue: 560tfifo_dequeue:
561 skb = __skb_dequeue(&sch->q); 561 skb = __skb_dequeue(&sch->q);
562 if (skb) { 562 if (skb) {
563deliver:
564 qdisc_qstats_backlog_dec(sch, skb); 563 qdisc_qstats_backlog_dec(sch, skb);
564deliver:
565 qdisc_unthrottled(sch); 565 qdisc_unthrottled(sch);
566 qdisc_bstats_update(sch, skb); 566 qdisc_bstats_update(sch, skb);
567 return skb; 567 return skb;
@@ -578,6 +578,7 @@ deliver:
578 rb_erase(p, &q->t_root); 578 rb_erase(p, &q->t_root);
579 579
580 sch->q.qlen--; 580 sch->q.qlen--;
581 qdisc_qstats_backlog_dec(sch, skb);
581 skb->next = NULL; 582 skb->next = NULL;
582 skb->prev = NULL; 583 skb->prev = NULL;
583 skb->tstamp = netem_skb_cb(skb)->tstamp_save; 584 skb->tstamp = netem_skb_cb(skb)->tstamp_save;
diff --git a/net/sctp/protocol.c b/net/sctp/protocol.c
index 8f34b27d5775..53b7acde9aa3 100644
--- a/net/sctp/protocol.c
+++ b/net/sctp/protocol.c
@@ -1322,8 +1322,7 @@ static __init int sctp_init(void)
1322 int max_share; 1322 int max_share;
1323 int order; 1323 int order;
1324 1324
1325 BUILD_BUG_ON(sizeof(struct sctp_ulpevent) > 1325 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1326 sizeof(((struct sk_buff *) 0)->cb));
1327 1326
1328 /* Allocate bind_bucket and chunk caches. */ 1327 /* Allocate bind_bucket and chunk caches. */
1329 status = -ENOBUFS; 1328 status = -ENOBUFS;
diff --git a/net/sctp/socket.c b/net/sctp/socket.c
index aafe94bf292e..f09de7fac2e6 100644
--- a/net/sctp/socket.c
+++ b/net/sctp/socket.c
@@ -102,11 +102,6 @@ static int sctp_autobind(struct sock *sk);
102static void sctp_sock_migrate(struct sock *, struct sock *, 102static void sctp_sock_migrate(struct sock *, struct sock *,
103 struct sctp_association *, sctp_socket_type_t); 103 struct sctp_association *, sctp_socket_type_t);
104 104
105extern struct kmem_cache *sctp_bucket_cachep;
106extern long sysctl_sctp_mem[3];
107extern int sysctl_sctp_rmem[3];
108extern int sysctl_sctp_wmem[3];
109
110static int sctp_memory_pressure; 105static int sctp_memory_pressure;
111static atomic_long_t sctp_memory_allocated; 106static atomic_long_t sctp_memory_allocated;
112struct percpu_counter sctp_sockets_allocated; 107struct percpu_counter sctp_sockets_allocated;
@@ -1586,8 +1581,7 @@ static int sctp_error(struct sock *sk, int flags, int err)
1586 1581
1587static int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *); 1582static int sctp_msghdr_parse(const struct msghdr *, sctp_cmsgs_t *);
1588 1583
1589static int sctp_sendmsg(struct kiocb *iocb, struct sock *sk, 1584static int sctp_sendmsg(struct sock *sk, struct msghdr *msg, size_t msg_len)
1590 struct msghdr *msg, size_t msg_len)
1591{ 1585{
1592 struct net *net = sock_net(sk); 1586 struct net *net = sock_net(sk);
1593 struct sctp_sock *sp; 1587 struct sctp_sock *sp;
@@ -2066,9 +2060,8 @@ static int sctp_skb_pull(struct sk_buff *skb, int len)
2066 * flags - flags sent or received with the user message, see Section 2060 * flags - flags sent or received with the user message, see Section
2067 * 5 for complete description of the flags. 2061 * 5 for complete description of the flags.
2068 */ 2062 */
2069static int sctp_recvmsg(struct kiocb *iocb, struct sock *sk, 2063static int sctp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
2070 struct msghdr *msg, size_t len, int noblock, 2064 int noblock, int flags, int *addr_len)
2071 int flags, int *addr_len)
2072{ 2065{
2073 struct sctp_ulpevent *event = NULL; 2066 struct sctp_ulpevent *event = NULL;
2074 struct sctp_sock *sp = sctp_sk(sk); 2067 struct sctp_sock *sp = sctp_sk(sk);
diff --git a/net/sctp/sysctl.c b/net/sctp/sysctl.c
index 2e9ada10fd84..26d50c565f54 100644
--- a/net/sctp/sysctl.c
+++ b/net/sctp/sysctl.c
@@ -58,10 +58,6 @@ static unsigned long max_autoclose_max =
58 (MAX_SCHEDULE_TIMEOUT / HZ > UINT_MAX) 58 (MAX_SCHEDULE_TIMEOUT / HZ > UINT_MAX)
59 ? UINT_MAX : MAX_SCHEDULE_TIMEOUT / HZ; 59 ? UINT_MAX : MAX_SCHEDULE_TIMEOUT / HZ;
60 60
61extern long sysctl_sctp_mem[3];
62extern int sysctl_sctp_rmem[3];
63extern int sysctl_sctp_wmem[3];
64
65static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write, 61static int proc_sctp_do_hmac_alg(struct ctl_table *ctl, int write,
66 void __user *buffer, size_t *lenp, 62 void __user *buffer, size_t *lenp,
67 loff_t *ppos); 63 loff_t *ppos);
diff --git a/net/socket.c b/net/socket.c
index 245330ca0015..3e33959f3ce5 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -140,8 +140,6 @@ static ssize_t sock_splice_read(struct file *file, loff_t *ppos,
140static const struct file_operations socket_file_ops = { 140static const struct file_operations socket_file_ops = {
141 .owner = THIS_MODULE, 141 .owner = THIS_MODULE,
142 .llseek = no_llseek, 142 .llseek = no_llseek,
143 .read = new_sync_read,
144 .write = new_sync_write,
145 .read_iter = sock_read_iter, 143 .read_iter = sock_read_iter,
146 .write_iter = sock_write_iter, 144 .write_iter = sock_write_iter,
147 .poll = sock_poll, 145 .poll = sock_poll,
@@ -610,60 +608,27 @@ void __sock_tx_timestamp(const struct sock *sk, __u8 *tx_flags)
610} 608}
611EXPORT_SYMBOL(__sock_tx_timestamp); 609EXPORT_SYMBOL(__sock_tx_timestamp);
612 610
613static inline int __sock_sendmsg_nosec(struct kiocb *iocb, struct socket *sock, 611static inline int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg)
614 struct msghdr *msg, size_t size)
615{ 612{
616 return sock->ops->sendmsg(iocb, sock, msg, size); 613 int ret = sock->ops->sendmsg(sock, msg, msg_data_left(msg));
617} 614 BUG_ON(ret == -EIOCBQUEUED);
618
619static inline int __sock_sendmsg(struct kiocb *iocb, struct socket *sock,
620 struct msghdr *msg, size_t size)
621{
622 int err = security_socket_sendmsg(sock, msg, size);
623
624 return err ?: __sock_sendmsg_nosec(iocb, sock, msg, size);
625}
626
627static int do_sock_sendmsg(struct socket *sock, struct msghdr *msg,
628 size_t size, bool nosec)
629{
630 struct kiocb iocb;
631 int ret;
632
633 init_sync_kiocb(&iocb, NULL);
634 ret = nosec ? __sock_sendmsg_nosec(&iocb, sock, msg, size) :
635 __sock_sendmsg(&iocb, sock, msg, size);
636 if (-EIOCBQUEUED == ret)
637 ret = wait_on_sync_kiocb(&iocb);
638 return ret; 615 return ret;
639} 616}
640 617
641int sock_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) 618int sock_sendmsg(struct socket *sock, struct msghdr *msg)
642{ 619{
643 return do_sock_sendmsg(sock, msg, size, false); 620 int err = security_socket_sendmsg(sock, msg,
644} 621 msg_data_left(msg));
645EXPORT_SYMBOL(sock_sendmsg);
646 622
647static int sock_sendmsg_nosec(struct socket *sock, struct msghdr *msg, size_t size) 623 return err ?: sock_sendmsg_nosec(sock, msg);
648{
649 return do_sock_sendmsg(sock, msg, size, true);
650} 624}
625EXPORT_SYMBOL(sock_sendmsg);
651 626
652int kernel_sendmsg(struct socket *sock, struct msghdr *msg, 627int kernel_sendmsg(struct socket *sock, struct msghdr *msg,
653 struct kvec *vec, size_t num, size_t size) 628 struct kvec *vec, size_t num, size_t size)
654{ 629{
655 mm_segment_t oldfs = get_fs(); 630 iov_iter_kvec(&msg->msg_iter, WRITE | ITER_KVEC, vec, num, size);
656 int result; 631 return sock_sendmsg(sock, msg);
657
658 set_fs(KERNEL_DS);
659 /*
660 * the following is safe, since for compiler definitions of kvec and
661 * iovec are identical, yielding the same in-core layout and alignment
662 */
663 iov_iter_init(&msg->msg_iter, WRITE, (struct iovec *)vec, num, size);
664 result = sock_sendmsg(sock, msg, size);
665 set_fs(oldfs);
666 return result;
667} 632}
668EXPORT_SYMBOL(kernel_sendmsg); 633EXPORT_SYMBOL(kernel_sendmsg);
669 634
@@ -731,9 +696,9 @@ EXPORT_SYMBOL_GPL(__sock_recv_wifi_status);
731static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk, 696static inline void sock_recv_drops(struct msghdr *msg, struct sock *sk,
732 struct sk_buff *skb) 697 struct sk_buff *skb)
733{ 698{
734 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && skb->dropcount) 699 if (sock_flag(sk, SOCK_RXQ_OVFL) && skb && SOCK_SKB_CB(skb)->dropcount)
735 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL, 700 put_cmsg(msg, SOL_SOCKET, SO_RXQ_OVFL,
736 sizeof(__u32), &skb->dropcount); 701 sizeof(__u32), &SOCK_SKB_CB(skb)->dropcount);
737} 702}
738 703
739void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk, 704void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
@@ -744,47 +709,21 @@ void __sock_recv_ts_and_drops(struct msghdr *msg, struct sock *sk,
744} 709}
745EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops); 710EXPORT_SYMBOL_GPL(__sock_recv_ts_and_drops);
746 711
747static inline int __sock_recvmsg_nosec(struct kiocb *iocb, struct socket *sock, 712static inline int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
748 struct msghdr *msg, size_t size, int flags) 713 size_t size, int flags)
749{ 714{
750 return sock->ops->recvmsg(iocb, sock, msg, size, flags); 715 return sock->ops->recvmsg(sock, msg, size, flags);
751} 716}
752 717
753static inline int __sock_recvmsg(struct kiocb *iocb, struct socket *sock, 718int sock_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
754 struct msghdr *msg, size_t size, int flags) 719 int flags)
755{ 720{
756 int err = security_socket_recvmsg(sock, msg, size, flags); 721 int err = security_socket_recvmsg(sock, msg, size, flags);
757 722
758 return err ?: __sock_recvmsg_nosec(iocb, sock, msg, size, flags); 723 return err ?: sock_recvmsg_nosec(sock, msg, size, flags);
759}
760
761int sock_recvmsg(struct socket *sock, struct msghdr *msg,
762 size_t size, int flags)
763{
764 struct kiocb iocb;
765 int ret;
766
767 init_sync_kiocb(&iocb, NULL);
768 ret = __sock_recvmsg(&iocb, sock, msg, size, flags);
769 if (-EIOCBQUEUED == ret)
770 ret = wait_on_sync_kiocb(&iocb);
771 return ret;
772} 724}
773EXPORT_SYMBOL(sock_recvmsg); 725EXPORT_SYMBOL(sock_recvmsg);
774 726
775static int sock_recvmsg_nosec(struct socket *sock, struct msghdr *msg,
776 size_t size, int flags)
777{
778 struct kiocb iocb;
779 int ret;
780
781 init_sync_kiocb(&iocb, NULL);
782 ret = __sock_recvmsg_nosec(&iocb, sock, msg, size, flags);
783 if (-EIOCBQUEUED == ret)
784 ret = wait_on_sync_kiocb(&iocb);
785 return ret;
786}
787
788/** 727/**
789 * kernel_recvmsg - Receive a message from a socket (kernel space) 728 * kernel_recvmsg - Receive a message from a socket (kernel space)
790 * @sock: The socket to receive the message from 729 * @sock: The socket to receive the message from
@@ -806,12 +745,8 @@ int kernel_recvmsg(struct socket *sock, struct msghdr *msg,
806 mm_segment_t oldfs = get_fs(); 745 mm_segment_t oldfs = get_fs();
807 int result; 746 int result;
808 747
748 iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, vec, num, size);
809 set_fs(KERNEL_DS); 749 set_fs(KERNEL_DS);
810 /*
811 * the following is safe, since for compiler definitions of kvec and
812 * iovec are identical, yielding the same in-core layout and alignment
813 */
814 iov_iter_init(&msg->msg_iter, READ, (struct iovec *)vec, num, size);
815 result = sock_recvmsg(sock, msg, size, flags); 750 result = sock_recvmsg(sock, msg, size, flags);
816 set_fs(oldfs); 751 set_fs(oldfs);
817 return result; 752 return result;
@@ -849,7 +784,8 @@ static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
849{ 784{
850 struct file *file = iocb->ki_filp; 785 struct file *file = iocb->ki_filp;
851 struct socket *sock = file->private_data; 786 struct socket *sock = file->private_data;
852 struct msghdr msg = {.msg_iter = *to}; 787 struct msghdr msg = {.msg_iter = *to,
788 .msg_iocb = iocb};
853 ssize_t res; 789 ssize_t res;
854 790
855 if (file->f_flags & O_NONBLOCK) 791 if (file->f_flags & O_NONBLOCK)
@@ -858,11 +794,10 @@ static ssize_t sock_read_iter(struct kiocb *iocb, struct iov_iter *to)
858 if (iocb->ki_pos != 0) 794 if (iocb->ki_pos != 0)
859 return -ESPIPE; 795 return -ESPIPE;
860 796
861 if (iocb->ki_nbytes == 0) /* Match SYS5 behaviour */ 797 if (!iov_iter_count(to)) /* Match SYS5 behaviour */
862 return 0; 798 return 0;
863 799
864 res = __sock_recvmsg(iocb, sock, &msg, 800 res = sock_recvmsg(sock, &msg, iov_iter_count(to), msg.msg_flags);
865 iocb->ki_nbytes, msg.msg_flags);
866 *to = msg.msg_iter; 801 *to = msg.msg_iter;
867 return res; 802 return res;
868} 803}
@@ -871,7 +806,8 @@ static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
871{ 806{
872 struct file *file = iocb->ki_filp; 807 struct file *file = iocb->ki_filp;
873 struct socket *sock = file->private_data; 808 struct socket *sock = file->private_data;
874 struct msghdr msg = {.msg_iter = *from}; 809 struct msghdr msg = {.msg_iter = *from,
810 .msg_iocb = iocb};
875 ssize_t res; 811 ssize_t res;
876 812
877 if (iocb->ki_pos != 0) 813 if (iocb->ki_pos != 0)
@@ -883,7 +819,7 @@ static ssize_t sock_write_iter(struct kiocb *iocb, struct iov_iter *from)
883 if (sock->type == SOCK_SEQPACKET) 819 if (sock->type == SOCK_SEQPACKET)
884 msg.msg_flags |= MSG_EOR; 820 msg.msg_flags |= MSG_EOR;
885 821
886 res = __sock_sendmsg(iocb, sock, &msg, iocb->ki_nbytes); 822 res = sock_sendmsg(sock, &msg);
887 *from = msg.msg_iter; 823 *from = msg.msg_iter;
888 return res; 824 return res;
889} 825}
@@ -1700,18 +1636,14 @@ SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1700 struct iovec iov; 1636 struct iovec iov;
1701 int fput_needed; 1637 int fput_needed;
1702 1638
1703 if (len > INT_MAX) 1639 err = import_single_range(WRITE, buff, len, &iov, &msg.msg_iter);
1704 len = INT_MAX; 1640 if (unlikely(err))
1705 if (unlikely(!access_ok(VERIFY_READ, buff, len))) 1641 return err;
1706 return -EFAULT;
1707 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1642 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1708 if (!sock) 1643 if (!sock)
1709 goto out; 1644 goto out;
1710 1645
1711 iov.iov_base = buff;
1712 iov.iov_len = len;
1713 msg.msg_name = NULL; 1646 msg.msg_name = NULL;
1714 iov_iter_init(&msg.msg_iter, WRITE, &iov, 1, len);
1715 msg.msg_control = NULL; 1647 msg.msg_control = NULL;
1716 msg.msg_controllen = 0; 1648 msg.msg_controllen = 0;
1717 msg.msg_namelen = 0; 1649 msg.msg_namelen = 0;
@@ -1725,7 +1657,7 @@ SYSCALL_DEFINE6(sendto, int, fd, void __user *, buff, size_t, len,
1725 if (sock->file->f_flags & O_NONBLOCK) 1657 if (sock->file->f_flags & O_NONBLOCK)
1726 flags |= MSG_DONTWAIT; 1658 flags |= MSG_DONTWAIT;
1727 msg.msg_flags = flags; 1659 msg.msg_flags = flags;
1728 err = sock_sendmsg(sock, &msg, len); 1660 err = sock_sendmsg(sock, &msg);
1729 1661
1730out_put: 1662out_put:
1731 fput_light(sock->file, fput_needed); 1663 fput_light(sock->file, fput_needed);
@@ -1760,26 +1692,22 @@ SYSCALL_DEFINE6(recvfrom, int, fd, void __user *, ubuf, size_t, size,
1760 int err, err2; 1692 int err, err2;
1761 int fput_needed; 1693 int fput_needed;
1762 1694
1763 if (size > INT_MAX) 1695 err = import_single_range(READ, ubuf, size, &iov, &msg.msg_iter);
1764 size = INT_MAX; 1696 if (unlikely(err))
1765 if (unlikely(!access_ok(VERIFY_WRITE, ubuf, size))) 1697 return err;
1766 return -EFAULT;
1767 sock = sockfd_lookup_light(fd, &err, &fput_needed); 1698 sock = sockfd_lookup_light(fd, &err, &fput_needed);
1768 if (!sock) 1699 if (!sock)
1769 goto out; 1700 goto out;
1770 1701
1771 msg.msg_control = NULL; 1702 msg.msg_control = NULL;
1772 msg.msg_controllen = 0; 1703 msg.msg_controllen = 0;
1773 iov.iov_len = size;
1774 iov.iov_base = ubuf;
1775 iov_iter_init(&msg.msg_iter, READ, &iov, 1, size);
1776 /* Save some cycles and don't copy the address if not needed */ 1704 /* Save some cycles and don't copy the address if not needed */
1777 msg.msg_name = addr ? (struct sockaddr *)&address : NULL; 1705 msg.msg_name = addr ? (struct sockaddr *)&address : NULL;
1778 /* We assume all kernel code knows the size of sockaddr_storage */ 1706 /* We assume all kernel code knows the size of sockaddr_storage */
1779 msg.msg_namelen = 0; 1707 msg.msg_namelen = 0;
1780 if (sock->file->f_flags & O_NONBLOCK) 1708 if (sock->file->f_flags & O_NONBLOCK)
1781 flags |= MSG_DONTWAIT; 1709 flags |= MSG_DONTWAIT;
1782 err = sock_recvmsg(sock, &msg, size, flags); 1710 err = sock_recvmsg(sock, &msg, iov_iter_count(&msg.msg_iter), flags);
1783 1711
1784 if (err >= 0 && addr != NULL) { 1712 if (err >= 0 && addr != NULL) {
1785 err2 = move_addr_to_user(&address, 1713 err2 = move_addr_to_user(&address,
@@ -1899,10 +1827,10 @@ struct used_address {
1899 unsigned int name_len; 1827 unsigned int name_len;
1900}; 1828};
1901 1829
1902static ssize_t copy_msghdr_from_user(struct msghdr *kmsg, 1830static int copy_msghdr_from_user(struct msghdr *kmsg,
1903 struct user_msghdr __user *umsg, 1831 struct user_msghdr __user *umsg,
1904 struct sockaddr __user **save_addr, 1832 struct sockaddr __user **save_addr,
1905 struct iovec **iov) 1833 struct iovec **iov)
1906{ 1834{
1907 struct sockaddr __user *uaddr; 1835 struct sockaddr __user *uaddr;
1908 struct iovec __user *uiov; 1836 struct iovec __user *uiov;
@@ -1946,13 +1874,10 @@ static ssize_t copy_msghdr_from_user(struct msghdr *kmsg,
1946 if (nr_segs > UIO_MAXIOV) 1874 if (nr_segs > UIO_MAXIOV)
1947 return -EMSGSIZE; 1875 return -EMSGSIZE;
1948 1876
1949 err = rw_copy_check_uvector(save_addr ? READ : WRITE, 1877 kmsg->msg_iocb = NULL;
1950 uiov, nr_segs, 1878
1951 UIO_FASTIOV, *iov, iov); 1879 return import_iovec(save_addr ? READ : WRITE, uiov, nr_segs,
1952 if (err >= 0) 1880 UIO_FASTIOV, iov, &kmsg->msg_iter);
1953 iov_iter_init(&kmsg->msg_iter, save_addr ? READ : WRITE,
1954 *iov, nr_segs, err);
1955 return err;
1956} 1881}
1957 1882
1958static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg, 1883static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
@@ -1967,7 +1892,7 @@ static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
1967 __attribute__ ((aligned(sizeof(__kernel_size_t)))); 1892 __attribute__ ((aligned(sizeof(__kernel_size_t))));
1968 /* 20 is size of ipv6_pktinfo */ 1893 /* 20 is size of ipv6_pktinfo */
1969 unsigned char *ctl_buf = ctl; 1894 unsigned char *ctl_buf = ctl;
1970 int ctl_len, total_len; 1895 int ctl_len;
1971 ssize_t err; 1896 ssize_t err;
1972 1897
1973 msg_sys->msg_name = &address; 1898 msg_sys->msg_name = &address;
@@ -1977,8 +1902,7 @@ static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
1977 else 1902 else
1978 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov); 1903 err = copy_msghdr_from_user(msg_sys, msg, NULL, &iov);
1979 if (err < 0) 1904 if (err < 0)
1980 goto out_freeiov; 1905 return err;
1981 total_len = err;
1982 1906
1983 err = -ENOBUFS; 1907 err = -ENOBUFS;
1984 1908
@@ -2025,10 +1949,10 @@ static int ___sys_sendmsg(struct socket *sock, struct user_msghdr __user *msg,
2025 used_address->name_len == msg_sys->msg_namelen && 1949 used_address->name_len == msg_sys->msg_namelen &&
2026 !memcmp(&used_address->name, msg_sys->msg_name, 1950 !memcmp(&used_address->name, msg_sys->msg_name,
2027 used_address->name_len)) { 1951 used_address->name_len)) {
2028 err = sock_sendmsg_nosec(sock, msg_sys, total_len); 1952 err = sock_sendmsg_nosec(sock, msg_sys);
2029 goto out_freectl; 1953 goto out_freectl;
2030 } 1954 }
2031 err = sock_sendmsg(sock, msg_sys, total_len); 1955 err = sock_sendmsg(sock, msg_sys);
2032 /* 1956 /*
2033 * If this is sendmmsg() and sending to current destination address was 1957 * If this is sendmmsg() and sending to current destination address was
2034 * successful, remember it. 1958 * successful, remember it.
@@ -2044,8 +1968,7 @@ out_freectl:
2044 if (ctl_buf != ctl) 1968 if (ctl_buf != ctl)
2045 sock_kfree_s(sock->sk, ctl_buf, ctl_len); 1969 sock_kfree_s(sock->sk, ctl_buf, ctl_len);
2046out_freeiov: 1970out_freeiov:
2047 if (iov != iovstack) 1971 kfree(iov);
2048 kfree(iov);
2049 return err; 1972 return err;
2050} 1973}
2051 1974
@@ -2170,8 +2093,8 @@ static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2170 else 2093 else
2171 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov); 2094 err = copy_msghdr_from_user(msg_sys, msg, &uaddr, &iov);
2172 if (err < 0) 2095 if (err < 0)
2173 goto out_freeiov; 2096 return err;
2174 total_len = err; 2097 total_len = iov_iter_count(&msg_sys->msg_iter);
2175 2098
2176 cmsg_ptr = (unsigned long)msg_sys->msg_control; 2099 cmsg_ptr = (unsigned long)msg_sys->msg_control;
2177 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT); 2100 msg_sys->msg_flags = flags & (MSG_CMSG_CLOEXEC|MSG_CMSG_COMPAT);
@@ -2209,8 +2132,7 @@ static int ___sys_recvmsg(struct socket *sock, struct user_msghdr __user *msg,
2209 err = len; 2132 err = len;
2210 2133
2211out_freeiov: 2134out_freeiov:
2212 if (iov != iovstack) 2135 kfree(iov);
2213 kfree(iov);
2214 return err; 2136 return err;
2215} 2137}
2216 2138
diff --git a/net/sunrpc/Kconfig b/net/sunrpc/Kconfig
index fb78117b896c..9068e72aa73c 100644
--- a/net/sunrpc/Kconfig
+++ b/net/sunrpc/Kconfig
@@ -1,9 +1,11 @@
1config SUNRPC 1config SUNRPC
2 tristate 2 tristate
3 depends on MULTIUSER
3 4
4config SUNRPC_GSS 5config SUNRPC_GSS
5 tristate 6 tristate
6 select OID_REGISTRY 7 select OID_REGISTRY
8 depends on MULTIUSER
7 9
8config SUNRPC_BACKCHANNEL 10config SUNRPC_BACKCHANNEL
9 bool 11 bool
diff --git a/net/sunrpc/cache.c b/net/sunrpc/cache.c
index 5199bb1a017e..2928afffbb81 100644
--- a/net/sunrpc/cache.c
+++ b/net/sunrpc/cache.c
@@ -1072,10 +1072,12 @@ void qword_add(char **bpp, int *lp, char *str)
1072 1072
1073 if (len < 0) return; 1073 if (len < 0) return;
1074 1074
1075 ret = string_escape_str(str, &bp, len, ESCAPE_OCTAL, "\\ \n\t"); 1075 ret = string_escape_str(str, bp, len, ESCAPE_OCTAL, "\\ \n\t");
1076 if (ret < 0 || ret == len) 1076 if (ret >= len) {
1077 bp += len;
1077 len = -1; 1078 len = -1;
1078 else { 1079 } else {
1080 bp += ret;
1079 len -= ret; 1081 len -= ret;
1080 *bp++ = ' '; 1082 *bp++ = ' ';
1081 len--; 1083 len--;
diff --git a/net/sunrpc/svcsock.c b/net/sunrpc/svcsock.c
index cc331b6cf573..0c8120229a03 100644
--- a/net/sunrpc/svcsock.c
+++ b/net/sunrpc/svcsock.c
@@ -257,7 +257,7 @@ static int svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr)
257 257
258 svc_set_cmsg_data(rqstp, cmh); 258 svc_set_cmsg_data(rqstp, cmh);
259 259
260 if (sock_sendmsg(sock, &msg, 0) < 0) 260 if (sock_sendmsg(sock, &msg) < 0)
261 goto out; 261 goto out;
262 } 262 }
263 263
diff --git a/net/sunrpc/xprtrdma/verbs.c b/net/sunrpc/xprtrdma/verbs.c
index 124676c13780..e28909fddd30 100644
--- a/net/sunrpc/xprtrdma/verbs.c
+++ b/net/sunrpc/xprtrdma/verbs.c
@@ -1136,7 +1136,7 @@ rpcrdma_init_fmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
1136 int i, rc; 1136 int i, rc;
1137 1137
1138 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; 1138 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
1139 dprintk("RPC: %s: initalizing %d FMRs\n", __func__, i); 1139 dprintk("RPC: %s: initializing %d FMRs\n", __func__, i);
1140 1140
1141 while (i--) { 1141 while (i--) {
1142 r = kzalloc(sizeof(*r), GFP_KERNEL); 1142 r = kzalloc(sizeof(*r), GFP_KERNEL);
@@ -1169,7 +1169,7 @@ rpcrdma_init_frmrs(struct rpcrdma_ia *ia, struct rpcrdma_buffer *buf)
1169 int i, rc; 1169 int i, rc;
1170 1170
1171 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS; 1171 i = (buf->rb_max_requests + 1) * RPCRDMA_MAX_SEGS;
1172 dprintk("RPC: %s: initalizing %d FRMRs\n", __func__, i); 1172 dprintk("RPC: %s: initializing %d FRMRs\n", __func__, i);
1173 1173
1174 while (i--) { 1174 while (i--) {
1175 r = kzalloc(sizeof(*r), GFP_KERNEL); 1175 r = kzalloc(sizeof(*r), GFP_KERNEL);
diff --git a/net/switchdev/switchdev.c b/net/switchdev/switchdev.c
index 8c1e558db118..46568b85c333 100644
--- a/net/switchdev/switchdev.c
+++ b/net/switchdev/switchdev.c
@@ -1,6 +1,7 @@
1/* 1/*
2 * net/switchdev/switchdev.c - Switch device API 2 * net/switchdev/switchdev.c - Switch device API
3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us> 3 * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
4 * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
4 * 5 *
5 * This program is free software; you can redistribute it and/or modify 6 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
@@ -14,6 +15,7 @@
14#include <linux/mutex.h> 15#include <linux/mutex.h>
15#include <linux/notifier.h> 16#include <linux/notifier.h>
16#include <linux/netdevice.h> 17#include <linux/netdevice.h>
18#include <net/ip_fib.h>
17#include <net/switchdev.h> 19#include <net/switchdev.h>
18 20
19/** 21/**
@@ -26,13 +28,13 @@
26int netdev_switch_parent_id_get(struct net_device *dev, 28int netdev_switch_parent_id_get(struct net_device *dev,
27 struct netdev_phys_item_id *psid) 29 struct netdev_phys_item_id *psid)
28{ 30{
29 const struct net_device_ops *ops = dev->netdev_ops; 31 const struct swdev_ops *ops = dev->swdev_ops;
30 32
31 if (!ops->ndo_switch_parent_id_get) 33 if (!ops || !ops->swdev_parent_id_get)
32 return -EOPNOTSUPP; 34 return -EOPNOTSUPP;
33 return ops->ndo_switch_parent_id_get(dev, psid); 35 return ops->swdev_parent_id_get(dev, psid);
34} 36}
35EXPORT_SYMBOL(netdev_switch_parent_id_get); 37EXPORT_SYMBOL_GPL(netdev_switch_parent_id_get);
36 38
37/** 39/**
38 * netdev_switch_port_stp_update - Notify switch device port of STP 40 * netdev_switch_port_stp_update - Notify switch device port of STP
@@ -44,20 +46,29 @@ EXPORT_SYMBOL(netdev_switch_parent_id_get);
44 */ 46 */
45int netdev_switch_port_stp_update(struct net_device *dev, u8 state) 47int netdev_switch_port_stp_update(struct net_device *dev, u8 state)
46{ 48{
47 const struct net_device_ops *ops = dev->netdev_ops; 49 const struct swdev_ops *ops = dev->swdev_ops;
50 struct net_device *lower_dev;
51 struct list_head *iter;
52 int err = -EOPNOTSUPP;
48 53
49 if (!ops->ndo_switch_port_stp_update) 54 if (ops && ops->swdev_port_stp_update)
50 return -EOPNOTSUPP; 55 return ops->swdev_port_stp_update(dev, state);
51 WARN_ON(!ops->ndo_switch_parent_id_get); 56
52 return ops->ndo_switch_port_stp_update(dev, state); 57 netdev_for_each_lower_dev(dev, lower_dev, iter) {
58 err = netdev_switch_port_stp_update(lower_dev, state);
59 if (err && err != -EOPNOTSUPP)
60 return err;
61 }
62
63 return err;
53} 64}
54EXPORT_SYMBOL(netdev_switch_port_stp_update); 65EXPORT_SYMBOL_GPL(netdev_switch_port_stp_update);
55 66
56static DEFINE_MUTEX(netdev_switch_mutex); 67static DEFINE_MUTEX(netdev_switch_mutex);
57static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain); 68static RAW_NOTIFIER_HEAD(netdev_switch_notif_chain);
58 69
59/** 70/**
60 * register_netdev_switch_notifier - Register nofifier 71 * register_netdev_switch_notifier - Register notifier
61 * @nb: notifier_block 72 * @nb: notifier_block
62 * 73 *
63 * Register switch device notifier. This should be used by code 74 * Register switch device notifier. This should be used by code
@@ -73,10 +84,10 @@ int register_netdev_switch_notifier(struct notifier_block *nb)
73 mutex_unlock(&netdev_switch_mutex); 84 mutex_unlock(&netdev_switch_mutex);
74 return err; 85 return err;
75} 86}
76EXPORT_SYMBOL(register_netdev_switch_notifier); 87EXPORT_SYMBOL_GPL(register_netdev_switch_notifier);
77 88
78/** 89/**
79 * unregister_netdev_switch_notifier - Unregister nofifier 90 * unregister_netdev_switch_notifier - Unregister notifier
80 * @nb: notifier_block 91 * @nb: notifier_block
81 * 92 *
82 * Unregister switch device notifier. 93 * Unregister switch device notifier.
@@ -91,10 +102,10 @@ int unregister_netdev_switch_notifier(struct notifier_block *nb)
91 mutex_unlock(&netdev_switch_mutex); 102 mutex_unlock(&netdev_switch_mutex);
92 return err; 103 return err;
93} 104}
94EXPORT_SYMBOL(unregister_netdev_switch_notifier); 105EXPORT_SYMBOL_GPL(unregister_netdev_switch_notifier);
95 106
96/** 107/**
97 * call_netdev_switch_notifiers - Call nofifiers 108 * call_netdev_switch_notifiers - Call notifiers
98 * @val: value passed unmodified to notifier function 109 * @val: value passed unmodified to notifier function
99 * @dev: port device 110 * @dev: port device
100 * @info: notifier information data 111 * @info: notifier information data
@@ -114,7 +125,7 @@ int call_netdev_switch_notifiers(unsigned long val, struct net_device *dev,
114 mutex_unlock(&netdev_switch_mutex); 125 mutex_unlock(&netdev_switch_mutex);
115 return err; 126 return err;
116} 127}
117EXPORT_SYMBOL(call_netdev_switch_notifiers); 128EXPORT_SYMBOL_GPL(call_netdev_switch_notifiers);
118 129
119/** 130/**
120 * netdev_switch_port_bridge_setlink - Notify switch device port of bridge 131 * netdev_switch_port_bridge_setlink - Notify switch device port of bridge
@@ -139,7 +150,7 @@ int netdev_switch_port_bridge_setlink(struct net_device *dev,
139 150
140 return ops->ndo_bridge_setlink(dev, nlh, flags); 151 return ops->ndo_bridge_setlink(dev, nlh, flags);
141} 152}
142EXPORT_SYMBOL(netdev_switch_port_bridge_setlink); 153EXPORT_SYMBOL_GPL(netdev_switch_port_bridge_setlink);
143 154
144/** 155/**
145 * netdev_switch_port_bridge_dellink - Notify switch device port of bridge 156 * netdev_switch_port_bridge_dellink - Notify switch device port of bridge
@@ -164,7 +175,7 @@ int netdev_switch_port_bridge_dellink(struct net_device *dev,
164 175
165 return ops->ndo_bridge_dellink(dev, nlh, flags); 176 return ops->ndo_bridge_dellink(dev, nlh, flags);
166} 177}
167EXPORT_SYMBOL(netdev_switch_port_bridge_dellink); 178EXPORT_SYMBOL_GPL(netdev_switch_port_bridge_dellink);
168 179
169/** 180/**
170 * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink 181 * ndo_dflt_netdev_switch_port_bridge_setlink - default ndo bridge setlink
@@ -194,7 +205,7 @@ int ndo_dflt_netdev_switch_port_bridge_setlink(struct net_device *dev,
194 205
195 return ret; 206 return ret;
196} 207}
197EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_setlink); 208EXPORT_SYMBOL_GPL(ndo_dflt_netdev_switch_port_bridge_setlink);
198 209
199/** 210/**
200 * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink 211 * ndo_dflt_netdev_switch_port_bridge_dellink - default ndo bridge dellink
@@ -224,4 +235,170 @@ int ndo_dflt_netdev_switch_port_bridge_dellink(struct net_device *dev,
224 235
225 return ret; 236 return ret;
226} 237}
227EXPORT_SYMBOL(ndo_dflt_netdev_switch_port_bridge_dellink); 238EXPORT_SYMBOL_GPL(ndo_dflt_netdev_switch_port_bridge_dellink);
239
240static struct net_device *netdev_switch_get_lowest_dev(struct net_device *dev)
241{
242 const struct swdev_ops *ops = dev->swdev_ops;
243 struct net_device *lower_dev;
244 struct net_device *port_dev;
245 struct list_head *iter;
246
247 /* Recusively search down until we find a sw port dev.
248 * (A sw port dev supports swdev_parent_id_get).
249 */
250
251 if (dev->features & NETIF_F_HW_SWITCH_OFFLOAD &&
252 ops && ops->swdev_parent_id_get)
253 return dev;
254
255 netdev_for_each_lower_dev(dev, lower_dev, iter) {
256 port_dev = netdev_switch_get_lowest_dev(lower_dev);
257 if (port_dev)
258 return port_dev;
259 }
260
261 return NULL;
262}
263
264static struct net_device *netdev_switch_get_dev_by_nhs(struct fib_info *fi)
265{
266 struct netdev_phys_item_id psid;
267 struct netdev_phys_item_id prev_psid;
268 struct net_device *dev = NULL;
269 int nhsel;
270
271 /* For this route, all nexthop devs must be on the same switch. */
272
273 for (nhsel = 0; nhsel < fi->fib_nhs; nhsel++) {
274 const struct fib_nh *nh = &fi->fib_nh[nhsel];
275
276 if (!nh->nh_dev)
277 return NULL;
278
279 dev = netdev_switch_get_lowest_dev(nh->nh_dev);
280 if (!dev)
281 return NULL;
282
283 if (netdev_switch_parent_id_get(dev, &psid))
284 return NULL;
285
286 if (nhsel > 0) {
287 if (prev_psid.id_len != psid.id_len)
288 return NULL;
289 if (memcmp(prev_psid.id, psid.id, psid.id_len))
290 return NULL;
291 }
292
293 prev_psid = psid;
294 }
295
296 return dev;
297}
298
299/**
300 * netdev_switch_fib_ipv4_add - Add IPv4 route entry to switch
301 *
302 * @dst: route's IPv4 destination address
303 * @dst_len: destination address length (prefix length)
304 * @fi: route FIB info structure
305 * @tos: route TOS
306 * @type: route type
307 * @nlflags: netlink flags passed in (NLM_F_*)
308 * @tb_id: route table ID
309 *
310 * Add IPv4 route entry to switch device.
311 */
312int netdev_switch_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
313 u8 tos, u8 type, u32 nlflags, u32 tb_id)
314{
315 struct net_device *dev;
316 const struct swdev_ops *ops;
317 int err = 0;
318
319 /* Don't offload route if using custom ip rules or if
320 * IPv4 FIB offloading has been disabled completely.
321 */
322
323#ifdef CONFIG_IP_MULTIPLE_TABLES
324 if (fi->fib_net->ipv4.fib_has_custom_rules)
325 return 0;
326#endif
327
328 if (fi->fib_net->ipv4.fib_offload_disabled)
329 return 0;
330
331 dev = netdev_switch_get_dev_by_nhs(fi);
332 if (!dev)
333 return 0;
334 ops = dev->swdev_ops;
335
336 if (ops->swdev_fib_ipv4_add) {
337 err = ops->swdev_fib_ipv4_add(dev, htonl(dst), dst_len,
338 fi, tos, type, nlflags,
339 tb_id);
340 if (!err)
341 fi->fib_flags |= RTNH_F_EXTERNAL;
342 }
343
344 return err;
345}
346EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_add);
347
348/**
349 * netdev_switch_fib_ipv4_del - Delete IPv4 route entry from switch
350 *
351 * @dst: route's IPv4 destination address
352 * @dst_len: destination address length (prefix length)
353 * @fi: route FIB info structure
354 * @tos: route TOS
355 * @type: route type
356 * @tb_id: route table ID
357 *
358 * Delete IPv4 route entry from switch device.
359 */
360int netdev_switch_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
361 u8 tos, u8 type, u32 tb_id)
362{
363 struct net_device *dev;
364 const struct swdev_ops *ops;
365 int err = 0;
366
367 if (!(fi->fib_flags & RTNH_F_EXTERNAL))
368 return 0;
369
370 dev = netdev_switch_get_dev_by_nhs(fi);
371 if (!dev)
372 return 0;
373 ops = dev->swdev_ops;
374
375 if (ops->swdev_fib_ipv4_del) {
376 err = ops->swdev_fib_ipv4_del(dev, htonl(dst), dst_len,
377 fi, tos, type, tb_id);
378 if (!err)
379 fi->fib_flags &= ~RTNH_F_EXTERNAL;
380 }
381
382 return err;
383}
384EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_del);
385
386/**
387 * netdev_switch_fib_ipv4_abort - Abort an IPv4 FIB operation
388 *
389 * @fi: route FIB info structure
390 */
391void netdev_switch_fib_ipv4_abort(struct fib_info *fi)
392{
393 /* There was a problem installing this route to the offload
394 * device. For now, until we come up with more refined
395 * policy handling, abruptly end IPv4 fib offloading for
396 * for entire net by flushing offload device(s) of all
397 * IPv4 routes, and mark IPv4 fib offloading broken from
398 * this point forward.
399 */
400
401 fib_flush_external(fi->fib_net);
402 fi->fib_net->ipv4.fib_offload_disabled = true;
403}
404EXPORT_SYMBOL_GPL(netdev_switch_fib_ipv4_abort);
diff --git a/net/tipc/Kconfig b/net/tipc/Kconfig
index 91c8a8e031db..c25a3a149dc4 100644
--- a/net/tipc/Kconfig
+++ b/net/tipc/Kconfig
@@ -26,3 +26,11 @@ config TIPC_MEDIA_IB
26 help 26 help
27 Saying Y here will enable support for running TIPC on 27 Saying Y here will enable support for running TIPC on
28 IP-over-InfiniBand devices. 28 IP-over-InfiniBand devices.
29config TIPC_MEDIA_UDP
30 bool "IP/UDP media type support"
31 depends on TIPC
32 select NET_UDP_TUNNEL
33 help
34 Saying Y here will enable support for running TIPC over IP/UDP
35 bool
36 default y
diff --git a/net/tipc/Makefile b/net/tipc/Makefile
index 599b1a540d2b..57e460be4692 100644
--- a/net/tipc/Makefile
+++ b/net/tipc/Makefile
@@ -10,5 +10,6 @@ tipc-y += addr.o bcast.o bearer.o \
10 netlink.o netlink_compat.o node.o socket.o eth_media.o \ 10 netlink.o netlink_compat.o node.o socket.o eth_media.o \
11 server.o socket.o 11 server.o socket.o
12 12
13tipc-$(CONFIG_TIPC_MEDIA_UDP) += udp_media.o
13tipc-$(CONFIG_TIPC_MEDIA_IB) += ib_media.o 14tipc-$(CONFIG_TIPC_MEDIA_IB) += ib_media.o
14tipc-$(CONFIG_SYSCTL) += sysctl.o 15tipc-$(CONFIG_SYSCTL) += sysctl.o
diff --git a/net/tipc/addr.c b/net/tipc/addr.c
index 48fd3b5a73fb..ba7daa864d44 100644
--- a/net/tipc/addr.c
+++ b/net/tipc/addr.c
@@ -38,6 +38,13 @@
38#include "addr.h" 38#include "addr.h"
39#include "core.h" 39#include "core.h"
40 40
41u32 tipc_own_addr(struct net *net)
42{
43 struct tipc_net *tn = net_generic(net, tipc_net_id);
44
45 return tn->own_addr;
46}
47
41/** 48/**
42 * in_own_cluster - test for cluster inclusion; <0.0.0> always matches 49 * in_own_cluster - test for cluster inclusion; <0.0.0> always matches
43 */ 50 */
diff --git a/net/tipc/addr.h b/net/tipc/addr.h
index c700c2d28e09..7ba6d5c8ae40 100644
--- a/net/tipc/addr.h
+++ b/net/tipc/addr.h
@@ -55,6 +55,7 @@ static inline u32 tipc_cluster_mask(u32 addr)
55 return addr & TIPC_CLUSTER_MASK; 55 return addr & TIPC_CLUSTER_MASK;
56} 56}
57 57
58u32 tipc_own_addr(struct net *net);
58int in_own_cluster(struct net *net, u32 addr); 59int in_own_cluster(struct net *net, u32 addr);
59int in_own_cluster_exact(struct net *net, u32 addr); 60int in_own_cluster_exact(struct net *net, u32 addr);
60int in_own_node(struct net *net, u32 addr); 61int in_own_node(struct net *net, u32 addr);
diff --git a/net/tipc/bcast.c b/net/tipc/bcast.c
index 3e41704832de..c5cbdcb1f0b5 100644
--- a/net/tipc/bcast.c
+++ b/net/tipc/bcast.c
@@ -62,21 +62,8 @@ static void tipc_bclink_lock(struct net *net)
62static void tipc_bclink_unlock(struct net *net) 62static void tipc_bclink_unlock(struct net *net)
63{ 63{
64 struct tipc_net *tn = net_generic(net, tipc_net_id); 64 struct tipc_net *tn = net_generic(net, tipc_net_id);
65 struct tipc_node *node = NULL;
66 65
67 if (likely(!tn->bclink->flags)) {
68 spin_unlock_bh(&tn->bclink->lock);
69 return;
70 }
71
72 if (tn->bclink->flags & TIPC_BCLINK_RESET) {
73 tn->bclink->flags &= ~TIPC_BCLINK_RESET;
74 node = tipc_bclink_retransmit_to(net);
75 }
76 spin_unlock_bh(&tn->bclink->lock); 66 spin_unlock_bh(&tn->bclink->lock);
77
78 if (node)
79 tipc_link_reset_all(node);
80} 67}
81 68
82void tipc_bclink_input(struct net *net) 69void tipc_bclink_input(struct net *net)
@@ -91,13 +78,6 @@ uint tipc_bclink_get_mtu(void)
91 return MAX_PKT_DEFAULT_MCAST; 78 return MAX_PKT_DEFAULT_MCAST;
92} 79}
93 80
94void tipc_bclink_set_flags(struct net *net, unsigned int flags)
95{
96 struct tipc_net *tn = net_generic(net, tipc_net_id);
97
98 tn->bclink->flags |= flags;
99}
100
101static u32 bcbuf_acks(struct sk_buff *buf) 81static u32 bcbuf_acks(struct sk_buff *buf)
102{ 82{
103 return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle; 83 return (u32)(unsigned long)TIPC_SKB_CB(buf)->handle;
@@ -135,9 +115,10 @@ static void bclink_set_last_sent(struct net *net)
135{ 115{
136 struct tipc_net *tn = net_generic(net, tipc_net_id); 116 struct tipc_net *tn = net_generic(net, tipc_net_id);
137 struct tipc_link *bcl = tn->bcl; 117 struct tipc_link *bcl = tn->bcl;
118 struct sk_buff *skb = skb_peek(&bcl->backlogq);
138 119
139 if (bcl->next_out) 120 if (skb)
140 bcl->fsm_msg_cnt = mod(buf_seqno(bcl->next_out) - 1); 121 bcl->fsm_msg_cnt = mod(buf_seqno(skb) - 1);
141 else 122 else
142 bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1); 123 bcl->fsm_msg_cnt = mod(bcl->next_out_no - 1);
143} 124}
@@ -155,7 +136,6 @@ static void bclink_update_last_sent(struct tipc_node *node, u32 seqno)
155 seqno : node->bclink.last_sent; 136 seqno : node->bclink.last_sent;
156} 137}
157 138
158
159/** 139/**
160 * tipc_bclink_retransmit_to - get most recent node to request retransmission 140 * tipc_bclink_retransmit_to - get most recent node to request retransmission
161 * 141 *
@@ -180,7 +160,7 @@ static void bclink_retransmit_pkt(struct tipc_net *tn, u32 after, u32 to)
180 struct sk_buff *skb; 160 struct sk_buff *skb;
181 struct tipc_link *bcl = tn->bcl; 161 struct tipc_link *bcl = tn->bcl;
182 162
183 skb_queue_walk(&bcl->outqueue, skb) { 163 skb_queue_walk(&bcl->transmq, skb) {
184 if (more(buf_seqno(skb), after)) { 164 if (more(buf_seqno(skb), after)) {
185 tipc_link_retransmit(bcl, skb, mod(to - after)); 165 tipc_link_retransmit(bcl, skb, mod(to - after));
186 break; 166 break;
@@ -210,14 +190,17 @@ void tipc_bclink_wakeup_users(struct net *net)
210void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked) 190void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
211{ 191{
212 struct sk_buff *skb, *tmp; 192 struct sk_buff *skb, *tmp;
213 struct sk_buff *next;
214 unsigned int released = 0; 193 unsigned int released = 0;
215 struct net *net = n_ptr->net; 194 struct net *net = n_ptr->net;
216 struct tipc_net *tn = net_generic(net, tipc_net_id); 195 struct tipc_net *tn = net_generic(net, tipc_net_id);
217 196
197 if (unlikely(!n_ptr->bclink.recv_permitted))
198 return;
199
218 tipc_bclink_lock(net); 200 tipc_bclink_lock(net);
201
219 /* Bail out if tx queue is empty (no clean up is required) */ 202 /* Bail out if tx queue is empty (no clean up is required) */
220 skb = skb_peek(&tn->bcl->outqueue); 203 skb = skb_peek(&tn->bcl->transmq);
221 if (!skb) 204 if (!skb)
222 goto exit; 205 goto exit;
223 206
@@ -244,27 +227,19 @@ void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
244 } 227 }
245 228
246 /* Skip over packets that node has previously acknowledged */ 229 /* Skip over packets that node has previously acknowledged */
247 skb_queue_walk(&tn->bcl->outqueue, skb) { 230 skb_queue_walk(&tn->bcl->transmq, skb) {
248 if (more(buf_seqno(skb), n_ptr->bclink.acked)) 231 if (more(buf_seqno(skb), n_ptr->bclink.acked))
249 break; 232 break;
250 } 233 }
251 234
252 /* Update packets that node is now acknowledging */ 235 /* Update packets that node is now acknowledging */
253 skb_queue_walk_from_safe(&tn->bcl->outqueue, skb, tmp) { 236 skb_queue_walk_from_safe(&tn->bcl->transmq, skb, tmp) {
254 if (more(buf_seqno(skb), acked)) 237 if (more(buf_seqno(skb), acked))
255 break; 238 break;
256 239 bcbuf_decr_acks(skb);
257 next = tipc_skb_queue_next(&tn->bcl->outqueue, skb); 240 bclink_set_last_sent(net);
258 if (skb != tn->bcl->next_out) {
259 bcbuf_decr_acks(skb);
260 } else {
261 bcbuf_set_acks(skb, 0);
262 tn->bcl->next_out = next;
263 bclink_set_last_sent(net);
264 }
265
266 if (bcbuf_acks(skb) == 0) { 241 if (bcbuf_acks(skb) == 0) {
267 __skb_unlink(skb, &tn->bcl->outqueue); 242 __skb_unlink(skb, &tn->bcl->transmq);
268 kfree_skb(skb); 243 kfree_skb(skb);
269 released = 1; 244 released = 1;
270 } 245 }
@@ -272,7 +247,7 @@ void tipc_bclink_acknowledge(struct tipc_node *n_ptr, u32 acked)
272 n_ptr->bclink.acked = acked; 247 n_ptr->bclink.acked = acked;
273 248
274 /* Try resolving broadcast link congestion, if necessary */ 249 /* Try resolving broadcast link congestion, if necessary */
275 if (unlikely(tn->bcl->next_out)) { 250 if (unlikely(skb_peek(&tn->bcl->backlogq))) {
276 tipc_link_push_packets(tn->bcl); 251 tipc_link_push_packets(tn->bcl);
277 bclink_set_last_sent(net); 252 bclink_set_last_sent(net);
278 } 253 }
@@ -319,7 +294,7 @@ void tipc_bclink_update_link_state(struct tipc_node *n_ptr,
319 buf = tipc_buf_acquire(INT_H_SIZE); 294 buf = tipc_buf_acquire(INT_H_SIZE);
320 if (buf) { 295 if (buf) {
321 struct tipc_msg *msg = buf_msg(buf); 296 struct tipc_msg *msg = buf_msg(buf);
322 struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferred_queue); 297 struct sk_buff *skb = skb_peek(&n_ptr->bclink.deferdq);
323 u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent; 298 u32 to = skb ? buf_seqno(skb) - 1 : n_ptr->bclink.last_sent;
324 299
325 tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG, 300 tipc_msg_init(tn->own_addr, msg, BCAST_PROTOCOL, STATE_MSG,
@@ -354,13 +329,12 @@ static void bclink_peek_nack(struct net *net, struct tipc_msg *msg)
354 return; 329 return;
355 330
356 tipc_node_lock(n_ptr); 331 tipc_node_lock(n_ptr);
357
358 if (n_ptr->bclink.recv_permitted && 332 if (n_ptr->bclink.recv_permitted &&
359 (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) && 333 (n_ptr->bclink.last_in != n_ptr->bclink.last_sent) &&
360 (n_ptr->bclink.last_in == msg_bcgap_after(msg))) 334 (n_ptr->bclink.last_in == msg_bcgap_after(msg)))
361 n_ptr->bclink.oos_state = 2; 335 n_ptr->bclink.oos_state = 2;
362
363 tipc_node_unlock(n_ptr); 336 tipc_node_unlock(n_ptr);
337 tipc_node_put(n_ptr);
364} 338}
365 339
366/* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster 340/* tipc_bclink_xmit - deliver buffer chain to all nodes in cluster
@@ -387,14 +361,13 @@ int tipc_bclink_xmit(struct net *net, struct sk_buff_head *list)
387 __skb_queue_purge(list); 361 __skb_queue_purge(list);
388 return -EHOSTUNREACH; 362 return -EHOSTUNREACH;
389 } 363 }
390
391 /* Broadcast to all nodes */ 364 /* Broadcast to all nodes */
392 if (likely(bclink)) { 365 if (likely(bclink)) {
393 tipc_bclink_lock(net); 366 tipc_bclink_lock(net);
394 if (likely(bclink->bcast_nodes.count)) { 367 if (likely(bclink->bcast_nodes.count)) {
395 rc = __tipc_link_xmit(net, bcl, list); 368 rc = __tipc_link_xmit(net, bcl, list);
396 if (likely(!rc)) { 369 if (likely(!rc)) {
397 u32 len = skb_queue_len(&bcl->outqueue); 370 u32 len = skb_queue_len(&bcl->transmq);
398 371
399 bclink_set_last_sent(net); 372 bclink_set_last_sent(net);
400 bcl->stats.queue_sz_counts++; 373 bcl->stats.queue_sz_counts++;
@@ -440,7 +413,7 @@ static void bclink_accept_pkt(struct tipc_node *node, u32 seqno)
440 */ 413 */
441 if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) { 414 if (((seqno - tn->own_addr) % TIPC_MIN_LINK_WIN) == 0) {
442 tipc_link_proto_xmit(node->active_links[node->addr & 1], 415 tipc_link_proto_xmit(node->active_links[node->addr & 1],
443 STATE_MSG, 0, 0, 0, 0, 0); 416 STATE_MSG, 0, 0, 0, 0);
444 tn->bcl->stats.sent_acks++; 417 tn->bcl->stats.sent_acks++;
445 } 418 }
446} 419}
@@ -481,17 +454,18 @@ void tipc_bclink_rcv(struct net *net, struct sk_buff *buf)
481 goto unlock; 454 goto unlock;
482 if (msg_destnode(msg) == tn->own_addr) { 455 if (msg_destnode(msg) == tn->own_addr) {
483 tipc_bclink_acknowledge(node, msg_bcast_ack(msg)); 456 tipc_bclink_acknowledge(node, msg_bcast_ack(msg));
484 tipc_node_unlock(node);
485 tipc_bclink_lock(net); 457 tipc_bclink_lock(net);
486 bcl->stats.recv_nacks++; 458 bcl->stats.recv_nacks++;
487 tn->bclink->retransmit_to = node; 459 tn->bclink->retransmit_to = node;
488 bclink_retransmit_pkt(tn, msg_bcgap_after(msg), 460 bclink_retransmit_pkt(tn, msg_bcgap_after(msg),
489 msg_bcgap_to(msg)); 461 msg_bcgap_to(msg));
490 tipc_bclink_unlock(net); 462 tipc_bclink_unlock(net);
463 tipc_node_unlock(node);
491 } else { 464 } else {
492 tipc_node_unlock(node); 465 tipc_node_unlock(node);
493 bclink_peek_nack(net, msg); 466 bclink_peek_nack(net, msg);
494 } 467 }
468 tipc_node_put(node);
495 goto exit; 469 goto exit;
496 } 470 }
497 471
@@ -528,11 +502,13 @@ receive:
528 tipc_bclink_unlock(net); 502 tipc_bclink_unlock(net);
529 tipc_node_unlock(node); 503 tipc_node_unlock(node);
530 } else if (msg_user(msg) == MSG_FRAGMENTER) { 504 } else if (msg_user(msg) == MSG_FRAGMENTER) {
531 tipc_buf_append(&node->bclink.reasm_buf, &buf);
532 if (unlikely(!buf && !node->bclink.reasm_buf))
533 goto unlock;
534 tipc_bclink_lock(net); 505 tipc_bclink_lock(net);
535 bclink_accept_pkt(node, seqno); 506 bclink_accept_pkt(node, seqno);
507 tipc_buf_append(&node->bclink.reasm_buf, &buf);
508 if (unlikely(!buf && !node->bclink.reasm_buf)) {
509 tipc_bclink_unlock(net);
510 goto unlock;
511 }
536 bcl->stats.recv_fragments++; 512 bcl->stats.recv_fragments++;
537 if (buf) { 513 if (buf) {
538 bcl->stats.recv_fragmented++; 514 bcl->stats.recv_fragmented++;
@@ -559,25 +535,25 @@ receive:
559 if (node->bclink.last_in == node->bclink.last_sent) 535 if (node->bclink.last_in == node->bclink.last_sent)
560 goto unlock; 536 goto unlock;
561 537
562 if (skb_queue_empty(&node->bclink.deferred_queue)) { 538 if (skb_queue_empty(&node->bclink.deferdq)) {
563 node->bclink.oos_state = 1; 539 node->bclink.oos_state = 1;
564 goto unlock; 540 goto unlock;
565 } 541 }
566 542
567 msg = buf_msg(skb_peek(&node->bclink.deferred_queue)); 543 msg = buf_msg(skb_peek(&node->bclink.deferdq));
568 seqno = msg_seqno(msg); 544 seqno = msg_seqno(msg);
569 next_in = mod(next_in + 1); 545 next_in = mod(next_in + 1);
570 if (seqno != next_in) 546 if (seqno != next_in)
571 goto unlock; 547 goto unlock;
572 548
573 /* Take in-sequence message from deferred queue & deliver it */ 549 /* Take in-sequence message from deferred queue & deliver it */
574 buf = __skb_dequeue(&node->bclink.deferred_queue); 550 buf = __skb_dequeue(&node->bclink.deferdq);
575 goto receive; 551 goto receive;
576 } 552 }
577 553
578 /* Handle out-of-sequence broadcast message */ 554 /* Handle out-of-sequence broadcast message */
579 if (less(next_in, seqno)) { 555 if (less(next_in, seqno)) {
580 deferred = tipc_link_defer_pkt(&node->bclink.deferred_queue, 556 deferred = tipc_link_defer_pkt(&node->bclink.deferdq,
581 buf); 557 buf);
582 bclink_update_last_sent(node, seqno); 558 bclink_update_last_sent(node, seqno);
583 buf = NULL; 559 buf = NULL;
@@ -594,6 +570,7 @@ receive:
594 570
595unlock: 571unlock:
596 tipc_node_unlock(node); 572 tipc_node_unlock(node);
573 tipc_node_put(node);
597exit: 574exit:
598 kfree_skb(buf); 575 kfree_skb(buf);
599} 576}
@@ -634,7 +611,6 @@ static int tipc_bcbearer_send(struct net *net, struct sk_buff *buf,
634 msg_set_non_seq(msg, 1); 611 msg_set_non_seq(msg, 1);
635 msg_set_mc_netid(msg, tn->net_id); 612 msg_set_mc_netid(msg, tn->net_id);
636 tn->bcl->stats.sent_info++; 613 tn->bcl->stats.sent_info++;
637
638 if (WARN_ON(!bclink->bcast_nodes.count)) { 614 if (WARN_ON(!bclink->bcast_nodes.count)) {
639 dump_stack(); 615 dump_stack();
640 return 0; 616 return 0;
@@ -835,7 +811,7 @@ int tipc_nl_add_bc_link(struct net *net, struct tipc_nl_msg *msg)
835 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP); 811 prop = nla_nest_start(msg->skb, TIPC_NLA_LINK_PROP);
836 if (!prop) 812 if (!prop)
837 goto attr_msg_full; 813 goto attr_msg_full;
838 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->queue_limit[0])) 814 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, bcl->window))
839 goto prop_msg_full; 815 goto prop_msg_full;
840 nla_nest_end(msg->skb, prop); 816 nla_nest_end(msg->skb, prop);
841 817
@@ -913,8 +889,9 @@ int tipc_bclink_init(struct net *net)
913 sprintf(bcbearer->media.name, "tipc-broadcast"); 889 sprintf(bcbearer->media.name, "tipc-broadcast");
914 890
915 spin_lock_init(&bclink->lock); 891 spin_lock_init(&bclink->lock);
916 __skb_queue_head_init(&bcl->outqueue); 892 __skb_queue_head_init(&bcl->transmq);
917 __skb_queue_head_init(&bcl->deferred_queue); 893 __skb_queue_head_init(&bcl->backlogq);
894 __skb_queue_head_init(&bcl->deferdq);
918 skb_queue_head_init(&bcl->wakeupq); 895 skb_queue_head_init(&bcl->wakeupq);
919 bcl->next_out_no = 1; 896 bcl->next_out_no = 1;
920 spin_lock_init(&bclink->node.lock); 897 spin_lock_init(&bclink->node.lock);
@@ -922,7 +899,7 @@ int tipc_bclink_init(struct net *net)
922 skb_queue_head_init(&bclink->inputq); 899 skb_queue_head_init(&bclink->inputq);
923 bcl->owner = &bclink->node; 900 bcl->owner = &bclink->node;
924 bcl->owner->net = net; 901 bcl->owner->net = net;
925 bcl->max_pkt = MAX_PKT_DEFAULT_MCAST; 902 bcl->mtu = MAX_PKT_DEFAULT_MCAST;
926 tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT); 903 tipc_link_set_queue_limits(bcl, BCLINK_WIN_DEFAULT);
927 bcl->bearer_id = MAX_BEARERS; 904 bcl->bearer_id = MAX_BEARERS;
928 rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer); 905 rcu_assign_pointer(tn->bearer_list[MAX_BEARERS], &bcbearer->bearer);
diff --git a/net/tipc/bcast.h b/net/tipc/bcast.h
index 43f397fbac55..4bdc12277d33 100644
--- a/net/tipc/bcast.h
+++ b/net/tipc/bcast.h
@@ -55,7 +55,6 @@ struct tipc_bcbearer_pair {
55 struct tipc_bearer *secondary; 55 struct tipc_bearer *secondary;
56}; 56};
57 57
58#define TIPC_BCLINK_RESET 1
59#define BCBEARER MAX_BEARERS 58#define BCBEARER MAX_BEARERS
60 59
61/** 60/**
@@ -86,7 +85,6 @@ struct tipc_bcbearer {
86 * @lock: spinlock governing access to structure 85 * @lock: spinlock governing access to structure
87 * @link: (non-standard) broadcast link structure 86 * @link: (non-standard) broadcast link structure
88 * @node: (non-standard) node structure representing b'cast link's peer node 87 * @node: (non-standard) node structure representing b'cast link's peer node
89 * @flags: represent bclink states
90 * @bcast_nodes: map of broadcast-capable nodes 88 * @bcast_nodes: map of broadcast-capable nodes
91 * @retransmit_to: node that most recently requested a retransmit 89 * @retransmit_to: node that most recently requested a retransmit
92 * 90 *
@@ -96,7 +94,6 @@ struct tipc_bclink {
96 spinlock_t lock; 94 spinlock_t lock;
97 struct tipc_link link; 95 struct tipc_link link;
98 struct tipc_node node; 96 struct tipc_node node;
99 unsigned int flags;
100 struct sk_buff_head arrvq; 97 struct sk_buff_head arrvq;
101 struct sk_buff_head inputq; 98 struct sk_buff_head inputq;
102 struct tipc_node_map bcast_nodes; 99 struct tipc_node_map bcast_nodes;
@@ -117,7 +114,6 @@ static inline int tipc_nmap_equal(struct tipc_node_map *nm_a,
117 114
118int tipc_bclink_init(struct net *net); 115int tipc_bclink_init(struct net *net);
119void tipc_bclink_stop(struct net *net); 116void tipc_bclink_stop(struct net *net);
120void tipc_bclink_set_flags(struct net *tn, unsigned int flags);
121void tipc_bclink_add_node(struct net *net, u32 addr); 117void tipc_bclink_add_node(struct net *net, u32 addr);
122void tipc_bclink_remove_node(struct net *net, u32 addr); 118void tipc_bclink_remove_node(struct net *net, u32 addr);
123struct tipc_node *tipc_bclink_retransmit_to(struct net *tn); 119struct tipc_node *tipc_bclink_retransmit_to(struct net *tn);
diff --git a/net/tipc/bearer.c b/net/tipc/bearer.c
index 48852c2dcc03..3613e72e858e 100644
--- a/net/tipc/bearer.c
+++ b/net/tipc/bearer.c
@@ -48,6 +48,9 @@ static struct tipc_media * const media_info_array[] = {
48#ifdef CONFIG_TIPC_MEDIA_IB 48#ifdef CONFIG_TIPC_MEDIA_IB
49 &ib_media_info, 49 &ib_media_info,
50#endif 50#endif
51#ifdef CONFIG_TIPC_MEDIA_UDP
52 &udp_media_info,
53#endif
51 NULL 54 NULL
52}; 55};
53 56
@@ -216,7 +219,8 @@ void tipc_bearer_remove_dest(struct net *net, u32 bearer_id, u32 dest)
216 * tipc_enable_bearer - enable bearer with the given name 219 * tipc_enable_bearer - enable bearer with the given name
217 */ 220 */
218static int tipc_enable_bearer(struct net *net, const char *name, 221static int tipc_enable_bearer(struct net *net, const char *name,
219 u32 disc_domain, u32 priority) 222 u32 disc_domain, u32 priority,
223 struct nlattr *attr[])
220{ 224{
221 struct tipc_net *tn = net_generic(net, tipc_net_id); 225 struct tipc_net *tn = net_generic(net, tipc_net_id);
222 struct tipc_bearer *b_ptr; 226 struct tipc_bearer *b_ptr;
@@ -304,7 +308,7 @@ restart:
304 308
305 strcpy(b_ptr->name, name); 309 strcpy(b_ptr->name, name);
306 b_ptr->media = m_ptr; 310 b_ptr->media = m_ptr;
307 res = m_ptr->enable_media(net, b_ptr); 311 res = m_ptr->enable_media(net, b_ptr, attr);
308 if (res) { 312 if (res) {
309 pr_warn("Bearer <%s> rejected, enable failure (%d)\n", 313 pr_warn("Bearer <%s> rejected, enable failure (%d)\n",
310 name, -res); 314 name, -res);
@@ -372,7 +376,8 @@ static void bearer_disable(struct net *net, struct tipc_bearer *b_ptr,
372 kfree_rcu(b_ptr, rcu); 376 kfree_rcu(b_ptr, rcu);
373} 377}
374 378
375int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b) 379int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
380 struct nlattr *attr[])
376{ 381{
377 struct net_device *dev; 382 struct net_device *dev;
378 char *driver_name = strchr((const char *)b->name, ':') + 1; 383 char *driver_name = strchr((const char *)b->name, ':') + 1;
@@ -791,7 +796,7 @@ int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info)
791 } 796 }
792 797
793 rtnl_lock(); 798 rtnl_lock();
794 err = tipc_enable_bearer(net, bearer, domain, prio); 799 err = tipc_enable_bearer(net, bearer, domain, prio, attrs);
795 if (err) { 800 if (err) {
796 rtnl_unlock(); 801 rtnl_unlock();
797 return err; 802 return err;
diff --git a/net/tipc/bearer.h b/net/tipc/bearer.h
index 6b17795ff8bc..5cad243ee8fc 100644
--- a/net/tipc/bearer.h
+++ b/net/tipc/bearer.h
@@ -41,7 +41,7 @@
41#include <net/genetlink.h> 41#include <net/genetlink.h>
42 42
43#define MAX_BEARERS 2 43#define MAX_BEARERS 2
44#define MAX_MEDIA 2 44#define MAX_MEDIA 3
45#define MAX_NODES 4096 45#define MAX_NODES 4096
46#define WSIZE 32 46#define WSIZE 32
47 47
@@ -50,14 +50,16 @@
50 * - the field's actual content and length is defined per media 50 * - the field's actual content and length is defined per media
51 * - remaining unused bytes in the field are set to zero 51 * - remaining unused bytes in the field are set to zero
52 */ 52 */
53#define TIPC_MEDIA_ADDR_SIZE 32 53#define TIPC_MEDIA_INFO_SIZE 32
54#define TIPC_MEDIA_TYPE_OFFSET 3 54#define TIPC_MEDIA_TYPE_OFFSET 3
55#define TIPC_MEDIA_ADDR_OFFSET 4
55 56
56/* 57/*
57 * Identifiers of supported TIPC media types 58 * Identifiers of supported TIPC media types
58 */ 59 */
59#define TIPC_MEDIA_TYPE_ETH 1 60#define TIPC_MEDIA_TYPE_ETH 1
60#define TIPC_MEDIA_TYPE_IB 2 61#define TIPC_MEDIA_TYPE_IB 2
62#define TIPC_MEDIA_TYPE_UDP 3
61 63
62/** 64/**
63 * struct tipc_node_map - set of node identifiers 65 * struct tipc_node_map - set of node identifiers
@@ -76,7 +78,7 @@ struct tipc_node_map {
76 * @broadcast: non-zero if address is a broadcast address 78 * @broadcast: non-zero if address is a broadcast address
77 */ 79 */
78struct tipc_media_addr { 80struct tipc_media_addr {
79 u8 value[TIPC_MEDIA_ADDR_SIZE]; 81 u8 value[TIPC_MEDIA_INFO_SIZE];
80 u8 media_id; 82 u8 media_id;
81 u8 broadcast; 83 u8 broadcast;
82}; 84};
@@ -103,7 +105,8 @@ struct tipc_media {
103 int (*send_msg)(struct net *net, struct sk_buff *buf, 105 int (*send_msg)(struct net *net, struct sk_buff *buf,
104 struct tipc_bearer *b_ptr, 106 struct tipc_bearer *b_ptr,
105 struct tipc_media_addr *dest); 107 struct tipc_media_addr *dest);
106 int (*enable_media)(struct net *net, struct tipc_bearer *b_ptr); 108 int (*enable_media)(struct net *net, struct tipc_bearer *b_ptr,
109 struct nlattr *attr[]);
107 void (*disable_media)(struct tipc_bearer *b_ptr); 110 void (*disable_media)(struct tipc_bearer *b_ptr);
108 int (*addr2str)(struct tipc_media_addr *addr, 111 int (*addr2str)(struct tipc_media_addr *addr,
109 char *strbuf, 112 char *strbuf,
@@ -182,6 +185,9 @@ extern struct tipc_media eth_media_info;
182#ifdef CONFIG_TIPC_MEDIA_IB 185#ifdef CONFIG_TIPC_MEDIA_IB
183extern struct tipc_media ib_media_info; 186extern struct tipc_media ib_media_info;
184#endif 187#endif
188#ifdef CONFIG_TIPC_MEDIA_UDP
189extern struct tipc_media udp_media_info;
190#endif
185 191
186int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info); 192int tipc_nl_bearer_disable(struct sk_buff *skb, struct genl_info *info);
187int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info); 193int tipc_nl_bearer_enable(struct sk_buff *skb, struct genl_info *info);
@@ -196,7 +202,8 @@ int tipc_nl_media_set(struct sk_buff *skb, struct genl_info *info);
196int tipc_media_set_priority(const char *name, u32 new_value); 202int tipc_media_set_priority(const char *name, u32 new_value);
197int tipc_media_set_window(const char *name, u32 new_value); 203int tipc_media_set_window(const char *name, u32 new_value);
198void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a); 204void tipc_media_addr_printf(char *buf, int len, struct tipc_media_addr *a);
199int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b); 205int tipc_enable_l2_media(struct net *net, struct tipc_bearer *b,
206 struct nlattr *attrs[]);
200void tipc_disable_l2_media(struct tipc_bearer *b); 207void tipc_disable_l2_media(struct tipc_bearer *b);
201int tipc_l2_send_msg(struct net *net, struct sk_buff *buf, 208int tipc_l2_send_msg(struct net *net, struct sk_buff *buf,
202 struct tipc_bearer *b, struct tipc_media_addr *dest); 209 struct tipc_bearer *b, struct tipc_media_addr *dest);
diff --git a/net/tipc/discover.c b/net/tipc/discover.c
index feef3753615d..967e292f53c8 100644
--- a/net/tipc/discover.c
+++ b/net/tipc/discover.c
@@ -86,9 +86,10 @@ static void tipc_disc_init_msg(struct net *net, struct sk_buff *buf, u32 type,
86 86
87 msg = buf_msg(buf); 87 msg = buf_msg(buf);
88 tipc_msg_init(tn->own_addr, msg, LINK_CONFIG, type, 88 tipc_msg_init(tn->own_addr, msg, LINK_CONFIG, type,
89 INT_H_SIZE, dest_domain); 89 MAX_H_SIZE, dest_domain);
90 msg_set_non_seq(msg, 1); 90 msg_set_non_seq(msg, 1);
91 msg_set_node_sig(msg, tn->random); 91 msg_set_node_sig(msg, tn->random);
92 msg_set_node_capabilities(msg, 0);
92 msg_set_dest_domain(msg, dest_domain); 93 msg_set_dest_domain(msg, dest_domain);
93 msg_set_bc_netid(msg, tn->net_id); 94 msg_set_bc_netid(msg, tn->net_id);
94 b_ptr->media->addr2msg(msg_media_addr(msg), &b_ptr->addr); 95 b_ptr->media->addr2msg(msg_media_addr(msg), &b_ptr->addr);
@@ -133,6 +134,7 @@ void tipc_disc_rcv(struct net *net, struct sk_buff *buf,
133 u32 net_id = msg_bc_netid(msg); 134 u32 net_id = msg_bc_netid(msg);
134 u32 mtyp = msg_type(msg); 135 u32 mtyp = msg_type(msg);
135 u32 signature = msg_node_sig(msg); 136 u32 signature = msg_node_sig(msg);
137 u16 caps = msg_node_capabilities(msg);
136 bool addr_match = false; 138 bool addr_match = false;
137 bool sign_match = false; 139 bool sign_match = false;
138 bool link_up = false; 140 bool link_up = false;
@@ -167,6 +169,7 @@ void tipc_disc_rcv(struct net *net, struct sk_buff *buf,
167 if (!node) 169 if (!node)
168 return; 170 return;
169 tipc_node_lock(node); 171 tipc_node_lock(node);
172 node->capabilities = caps;
170 link = node->links[bearer->identity]; 173 link = node->links[bearer->identity];
171 174
172 /* Prepare to validate requesting node's signature and media address */ 175 /* Prepare to validate requesting node's signature and media address */
@@ -249,7 +252,7 @@ void tipc_disc_rcv(struct net *net, struct sk_buff *buf,
249 252
250 /* Send response, if necessary */ 253 /* Send response, if necessary */
251 if (respond && (mtyp == DSC_REQ_MSG)) { 254 if (respond && (mtyp == DSC_REQ_MSG)) {
252 rbuf = tipc_buf_acquire(INT_H_SIZE); 255 rbuf = tipc_buf_acquire(MAX_H_SIZE);
253 if (rbuf) { 256 if (rbuf) {
254 tipc_disc_init_msg(net, rbuf, DSC_RESP_MSG, bearer); 257 tipc_disc_init_msg(net, rbuf, DSC_RESP_MSG, bearer);
255 tipc_bearer_send(net, bearer->identity, rbuf, &maddr); 258 tipc_bearer_send(net, bearer->identity, rbuf, &maddr);
@@ -257,6 +260,7 @@ void tipc_disc_rcv(struct net *net, struct sk_buff *buf,
257 } 260 }
258 } 261 }
259 tipc_node_unlock(node); 262 tipc_node_unlock(node);
263 tipc_node_put(node);
260} 264}
261 265
262/** 266/**
@@ -359,8 +363,7 @@ int tipc_disc_create(struct net *net, struct tipc_bearer *b_ptr,
359 req = kmalloc(sizeof(*req), GFP_ATOMIC); 363 req = kmalloc(sizeof(*req), GFP_ATOMIC);
360 if (!req) 364 if (!req)
361 return -ENOMEM; 365 return -ENOMEM;
362 366 req->buf = tipc_buf_acquire(MAX_H_SIZE);
363 req->buf = tipc_buf_acquire(INT_H_SIZE);
364 if (!req->buf) { 367 if (!req->buf) {
365 kfree(req); 368 kfree(req);
366 return -ENOMEM; 369 return -ENOMEM;
diff --git a/net/tipc/eth_media.c b/net/tipc/eth_media.c
index 5e1426f1751f..f69a2fde9f4a 100644
--- a/net/tipc/eth_media.c
+++ b/net/tipc/eth_media.c
@@ -37,8 +37,6 @@
37#include "core.h" 37#include "core.h"
38#include "bearer.h" 38#include "bearer.h"
39 39
40#define ETH_ADDR_OFFSET 4 /* MAC addr position inside address field */
41
42/* Convert Ethernet address (media address format) to string */ 40/* Convert Ethernet address (media address format) to string */
43static int tipc_eth_addr2str(struct tipc_media_addr *addr, 41static int tipc_eth_addr2str(struct tipc_media_addr *addr,
44 char *strbuf, int bufsz) 42 char *strbuf, int bufsz)
@@ -53,9 +51,9 @@ static int tipc_eth_addr2str(struct tipc_media_addr *addr,
53/* Convert from media address format to discovery message addr format */ 51/* Convert from media address format to discovery message addr format */
54static int tipc_eth_addr2msg(char *msg, struct tipc_media_addr *addr) 52static int tipc_eth_addr2msg(char *msg, struct tipc_media_addr *addr)
55{ 53{
56 memset(msg, 0, TIPC_MEDIA_ADDR_SIZE); 54 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
57 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_ETH; 55 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_ETH;
58 memcpy(msg + ETH_ADDR_OFFSET, addr->value, ETH_ALEN); 56 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, addr->value, ETH_ALEN);
59 return 0; 57 return 0;
60} 58}
61 59
@@ -79,7 +77,7 @@ static int tipc_eth_msg2addr(struct tipc_bearer *b,
79 char *msg) 77 char *msg)
80{ 78{
81 /* Skip past preamble: */ 79 /* Skip past preamble: */
82 msg += ETH_ADDR_OFFSET; 80 msg += TIPC_MEDIA_ADDR_OFFSET;
83 return tipc_eth_raw2addr(b, addr, msg); 81 return tipc_eth_raw2addr(b, addr, msg);
84} 82}
85 83
diff --git a/net/tipc/ib_media.c b/net/tipc/ib_media.c
index 8522eef9c136..e8c16718e3fa 100644
--- a/net/tipc/ib_media.c
+++ b/net/tipc/ib_media.c
@@ -57,7 +57,7 @@ static int tipc_ib_addr2str(struct tipc_media_addr *a, char *str_buf,
57/* Convert from media address format to discovery message addr format */ 57/* Convert from media address format to discovery message addr format */
58static int tipc_ib_addr2msg(char *msg, struct tipc_media_addr *addr) 58static int tipc_ib_addr2msg(char *msg, struct tipc_media_addr *addr)
59{ 59{
60 memset(msg, 0, TIPC_MEDIA_ADDR_SIZE); 60 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
61 memcpy(msg, addr->value, INFINIBAND_ALEN); 61 memcpy(msg, addr->value, INFINIBAND_ALEN);
62 return 0; 62 return 0;
63} 63}
diff --git a/net/tipc/link.c b/net/tipc/link.c
index 14f09b3cb87c..a6b30df6ec02 100644
--- a/net/tipc/link.c
+++ b/net/tipc/link.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * net/tipc/link.c: TIPC link code 2 * net/tipc/link.c: TIPC link code
3 * 3 *
4 * Copyright (c) 1996-2007, 2012-2014, Ericsson AB 4 * Copyright (c) 1996-2007, 2012-2015, Ericsson AB
5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems 5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
@@ -35,6 +35,7 @@
35 */ 35 */
36 36
37#include "core.h" 37#include "core.h"
38#include "subscr.h"
38#include "link.h" 39#include "link.h"
39#include "bcast.h" 40#include "bcast.h"
40#include "socket.h" 41#include "socket.h"
@@ -88,24 +89,14 @@ static const struct nla_policy tipc_nl_prop_policy[TIPC_NLA_PROP_MAX + 1] = {
88#define TIMEOUT_EVT 560817u /* link timer expired */ 89#define TIMEOUT_EVT 560817u /* link timer expired */
89 90
90/* 91/*
91 * The following two 'message types' is really just implementation 92 * State value stored in 'failover_pkts'
92 * data conveniently stored in the message header.
93 * They must not be considered part of the protocol
94 */ 93 */
95#define OPEN_MSG 0 94#define FIRST_FAILOVER 0xffffu
96#define CLOSED_MSG 1
97
98/*
99 * State value stored in 'exp_msg_count'
100 */
101#define START_CHANGEOVER 100000u
102 95
103static void link_handle_out_of_seq_msg(struct tipc_link *link, 96static void link_handle_out_of_seq_msg(struct tipc_link *link,
104 struct sk_buff *skb); 97 struct sk_buff *skb);
105static void tipc_link_proto_rcv(struct tipc_link *link, 98static void tipc_link_proto_rcv(struct tipc_link *link,
106 struct sk_buff *skb); 99 struct sk_buff *skb);
107static int tipc_link_tunnel_rcv(struct tipc_node *node,
108 struct sk_buff **skb);
109static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol); 100static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol);
110static void link_state_event(struct tipc_link *l_ptr, u32 event); 101static void link_state_event(struct tipc_link *l_ptr, u32 event);
111static void link_reset_statistics(struct tipc_link *l_ptr); 102static void link_reset_statistics(struct tipc_link *l_ptr);
@@ -114,7 +105,7 @@ static void tipc_link_sync_xmit(struct tipc_link *l);
114static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf); 105static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf);
115static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb); 106static void tipc_link_input(struct tipc_link *l, struct sk_buff *skb);
116static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb); 107static bool tipc_data_input(struct tipc_link *l, struct sk_buff *skb);
117 108static bool tipc_link_failover_rcv(struct tipc_link *l, struct sk_buff **skb);
118/* 109/*
119 * Simple link routines 110 * Simple link routines
120 */ 111 */
@@ -138,32 +129,11 @@ static void tipc_link_put(struct tipc_link *l_ptr)
138 kref_put(&l_ptr->ref, tipc_link_release); 129 kref_put(&l_ptr->ref, tipc_link_release);
139} 130}
140 131
141static void link_init_max_pkt(struct tipc_link *l_ptr) 132static struct tipc_link *tipc_parallel_link(struct tipc_link *l)
142{ 133{
143 struct tipc_node *node = l_ptr->owner; 134 if (l->owner->active_links[0] != l)
144 struct tipc_net *tn = net_generic(node->net, tipc_net_id); 135 return l->owner->active_links[0];
145 struct tipc_bearer *b_ptr; 136 return l->owner->active_links[1];
146 u32 max_pkt;
147
148 rcu_read_lock();
149 b_ptr = rcu_dereference_rtnl(tn->bearer_list[l_ptr->bearer_id]);
150 if (!b_ptr) {
151 rcu_read_unlock();
152 return;
153 }
154 max_pkt = (b_ptr->mtu & ~3);
155 rcu_read_unlock();
156
157 if (max_pkt > MAX_MSG_SIZE)
158 max_pkt = MAX_MSG_SIZE;
159
160 l_ptr->max_pkt_target = max_pkt;
161 if (l_ptr->max_pkt_target < MAX_PKT_DEFAULT)
162 l_ptr->max_pkt = l_ptr->max_pkt_target;
163 else
164 l_ptr->max_pkt = MAX_PKT_DEFAULT;
165
166 l_ptr->max_pkt_probes = 0;
167} 137}
168 138
169/* 139/*
@@ -194,10 +164,10 @@ static void link_timeout(unsigned long data)
194 tipc_node_lock(l_ptr->owner); 164 tipc_node_lock(l_ptr->owner);
195 165
196 /* update counters used in statistical profiling of send traffic */ 166 /* update counters used in statistical profiling of send traffic */
197 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->outqueue); 167 l_ptr->stats.accu_queue_sz += skb_queue_len(&l_ptr->transmq);
198 l_ptr->stats.queue_sz_counts++; 168 l_ptr->stats.queue_sz_counts++;
199 169
200 skb = skb_peek(&l_ptr->outqueue); 170 skb = skb_peek(&l_ptr->transmq);
201 if (skb) { 171 if (skb) {
202 struct tipc_msg *msg = buf_msg(skb); 172 struct tipc_msg *msg = buf_msg(skb);
203 u32 length = msg_size(msg); 173 u32 length = msg_size(msg);
@@ -229,7 +199,7 @@ static void link_timeout(unsigned long data)
229 /* do all other link processing performed on a periodic basis */ 199 /* do all other link processing performed on a periodic basis */
230 link_state_event(l_ptr, TIMEOUT_EVT); 200 link_state_event(l_ptr, TIMEOUT_EVT);
231 201
232 if (l_ptr->next_out) 202 if (skb_queue_len(&l_ptr->backlogq))
233 tipc_link_push_packets(l_ptr); 203 tipc_link_push_packets(l_ptr);
234 204
235 tipc_node_unlock(l_ptr->owner); 205 tipc_node_unlock(l_ptr->owner);
@@ -305,16 +275,15 @@ struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
305 msg_set_session(msg, (tn->random & 0xffff)); 275 msg_set_session(msg, (tn->random & 0xffff));
306 msg_set_bearer_id(msg, b_ptr->identity); 276 msg_set_bearer_id(msg, b_ptr->identity);
307 strcpy((char *)msg_data(msg), if_name); 277 strcpy((char *)msg_data(msg), if_name);
308 278 l_ptr->net_plane = b_ptr->net_plane;
279 l_ptr->advertised_mtu = b_ptr->mtu;
280 l_ptr->mtu = l_ptr->advertised_mtu;
309 l_ptr->priority = b_ptr->priority; 281 l_ptr->priority = b_ptr->priority;
310 tipc_link_set_queue_limits(l_ptr, b_ptr->window); 282 tipc_link_set_queue_limits(l_ptr, b_ptr->window);
311
312 l_ptr->net_plane = b_ptr->net_plane;
313 link_init_max_pkt(l_ptr);
314
315 l_ptr->next_out_no = 1; 283 l_ptr->next_out_no = 1;
316 __skb_queue_head_init(&l_ptr->outqueue); 284 __skb_queue_head_init(&l_ptr->transmq);
317 __skb_queue_head_init(&l_ptr->deferred_queue); 285 __skb_queue_head_init(&l_ptr->backlogq);
286 __skb_queue_head_init(&l_ptr->deferdq);
318 skb_queue_head_init(&l_ptr->wakeupq); 287 skb_queue_head_init(&l_ptr->wakeupq);
319 skb_queue_head_init(&l_ptr->inputq); 288 skb_queue_head_init(&l_ptr->inputq);
320 skb_queue_head_init(&l_ptr->namedq); 289 skb_queue_head_init(&l_ptr->namedq);
@@ -327,15 +296,19 @@ struct tipc_link *tipc_link_create(struct tipc_node *n_ptr,
327} 296}
328 297
329/** 298/**
330 * link_delete - Conditional deletion of link. 299 * tipc_link_delete - Delete a link
331 * If timer still running, real delete is done when it expires 300 * @l: link to be deleted
332 * @link: link to be deleted
333 */ 301 */
334void tipc_link_delete(struct tipc_link *link) 302void tipc_link_delete(struct tipc_link *l)
335{ 303{
336 tipc_link_reset_fragments(link); 304 tipc_link_reset(l);
337 tipc_node_detach_link(link->owner, link); 305 if (del_timer(&l->timer))
338 tipc_link_put(link); 306 tipc_link_put(l);
307 l->flags |= LINK_STOPPED;
308 /* Delete link now, or when timer is finished: */
309 tipc_link_reset_fragments(l);
310 tipc_node_detach_link(l->owner, l);
311 tipc_link_put(l);
339} 312}
340 313
341void tipc_link_delete_list(struct net *net, unsigned int bearer_id, 314void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
@@ -349,16 +322,7 @@ void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
349 list_for_each_entry_rcu(node, &tn->node_list, list) { 322 list_for_each_entry_rcu(node, &tn->node_list, list) {
350 tipc_node_lock(node); 323 tipc_node_lock(node);
351 link = node->links[bearer_id]; 324 link = node->links[bearer_id];
352 if (!link) { 325 if (link)
353 tipc_node_unlock(node);
354 continue;
355 }
356 tipc_link_reset(link);
357 if (del_timer(&link->timer))
358 tipc_link_put(link);
359 link->flags |= LINK_STOPPED;
360 /* Delete link now, or when failover is finished: */
361 if (shutting_down || !tipc_node_is_up(node))
362 tipc_link_delete(link); 326 tipc_link_delete(link);
363 tipc_node_unlock(node); 327 tipc_node_unlock(node);
364 } 328 }
@@ -366,28 +330,43 @@ void tipc_link_delete_list(struct net *net, unsigned int bearer_id,
366} 330}
367 331
368/** 332/**
369 * link_schedule_user - schedule user for wakeup after congestion 333 * link_schedule_user - schedule a message sender for wakeup after congestion
370 * @link: congested link 334 * @link: congested link
371 * @oport: sending port 335 * @list: message that was attempted sent
372 * @chain_sz: size of buffer chain that was attempted sent
373 * @imp: importance of message attempted sent
374 * Create pseudo msg to send back to user when congestion abates 336 * Create pseudo msg to send back to user when congestion abates
337 * Only consumes message if there is an error
375 */ 338 */
376static bool link_schedule_user(struct tipc_link *link, u32 oport, 339static int link_schedule_user(struct tipc_link *link, struct sk_buff_head *list)
377 uint chain_sz, uint imp)
378{ 340{
379 struct sk_buff *buf; 341 struct tipc_msg *msg = buf_msg(skb_peek(list));
342 int imp = msg_importance(msg);
343 u32 oport = msg_origport(msg);
344 u32 addr = link_own_addr(link);
345 struct sk_buff *skb;
380 346
381 buf = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0, 347 /* This really cannot happen... */
382 link_own_addr(link), link_own_addr(link), 348 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
383 oport, 0, 0); 349 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
384 if (!buf) 350 tipc_link_reset(link);
385 return false; 351 goto err;
386 TIPC_SKB_CB(buf)->chain_sz = chain_sz; 352 }
387 TIPC_SKB_CB(buf)->chain_imp = imp; 353 /* Non-blocking sender: */
388 skb_queue_tail(&link->wakeupq, buf); 354 if (TIPC_SKB_CB(skb_peek(list))->wakeup_pending)
355 return -ELINKCONG;
356
357 /* Create and schedule wakeup pseudo message */
358 skb = tipc_msg_create(SOCK_WAKEUP, 0, INT_H_SIZE, 0,
359 addr, addr, oport, 0, 0);
360 if (!skb)
361 goto err;
362 TIPC_SKB_CB(skb)->chain_sz = skb_queue_len(list);
363 TIPC_SKB_CB(skb)->chain_imp = imp;
364 skb_queue_tail(&link->wakeupq, skb);
389 link->stats.link_congs++; 365 link->stats.link_congs++;
390 return true; 366 return -ELINKCONG;
367err:
368 __skb_queue_purge(list);
369 return -ENOBUFS;
391} 370}
392 371
393/** 372/**
@@ -396,19 +375,22 @@ static bool link_schedule_user(struct tipc_link *link, u32 oport,
396 * Move a number of waiting users, as permitted by available space in 375 * Move a number of waiting users, as permitted by available space in
397 * the send queue, from link wait queue to node wait queue for wakeup 376 * the send queue, from link wait queue to node wait queue for wakeup
398 */ 377 */
399void link_prepare_wakeup(struct tipc_link *link) 378void link_prepare_wakeup(struct tipc_link *l)
400{ 379{
401 uint pend_qsz = skb_queue_len(&link->outqueue); 380 int pnd[TIPC_SYSTEM_IMPORTANCE + 1] = {0,};
381 int imp, lim;
402 struct sk_buff *skb, *tmp; 382 struct sk_buff *skb, *tmp;
403 383
404 skb_queue_walk_safe(&link->wakeupq, skb, tmp) { 384 skb_queue_walk_safe(&l->wakeupq, skb, tmp) {
405 if (pend_qsz >= link->queue_limit[TIPC_SKB_CB(skb)->chain_imp]) 385 imp = TIPC_SKB_CB(skb)->chain_imp;
386 lim = l->window + l->backlog[imp].limit;
387 pnd[imp] += TIPC_SKB_CB(skb)->chain_sz;
388 if ((pnd[imp] + l->backlog[imp].len) >= lim)
406 break; 389 break;
407 pend_qsz += TIPC_SKB_CB(skb)->chain_sz; 390 skb_unlink(skb, &l->wakeupq);
408 skb_unlink(skb, &link->wakeupq); 391 skb_queue_tail(&l->inputq, skb);
409 skb_queue_tail(&link->inputq, skb); 392 l->owner->inputq = &l->inputq;
410 link->owner->inputq = &link->inputq; 393 l->owner->action_flags |= TIPC_MSG_EVT;
411 link->owner->action_flags |= TIPC_MSG_EVT;
412 } 394 }
413} 395}
414 396
@@ -422,31 +404,42 @@ void tipc_link_reset_fragments(struct tipc_link *l_ptr)
422 l_ptr->reasm_buf = NULL; 404 l_ptr->reasm_buf = NULL;
423} 405}
424 406
407static void tipc_link_purge_backlog(struct tipc_link *l)
408{
409 __skb_queue_purge(&l->backlogq);
410 l->backlog[TIPC_LOW_IMPORTANCE].len = 0;
411 l->backlog[TIPC_MEDIUM_IMPORTANCE].len = 0;
412 l->backlog[TIPC_HIGH_IMPORTANCE].len = 0;
413 l->backlog[TIPC_CRITICAL_IMPORTANCE].len = 0;
414 l->backlog[TIPC_SYSTEM_IMPORTANCE].len = 0;
415}
416
425/** 417/**
426 * tipc_link_purge_queues - purge all pkt queues associated with link 418 * tipc_link_purge_queues - purge all pkt queues associated with link
427 * @l_ptr: pointer to link 419 * @l_ptr: pointer to link
428 */ 420 */
429void tipc_link_purge_queues(struct tipc_link *l_ptr) 421void tipc_link_purge_queues(struct tipc_link *l_ptr)
430{ 422{
431 __skb_queue_purge(&l_ptr->deferred_queue); 423 __skb_queue_purge(&l_ptr->deferdq);
432 __skb_queue_purge(&l_ptr->outqueue); 424 __skb_queue_purge(&l_ptr->transmq);
425 tipc_link_purge_backlog(l_ptr);
433 tipc_link_reset_fragments(l_ptr); 426 tipc_link_reset_fragments(l_ptr);
434} 427}
435 428
436void tipc_link_reset(struct tipc_link *l_ptr) 429void tipc_link_reset(struct tipc_link *l_ptr)
437{ 430{
438 u32 prev_state = l_ptr->state; 431 u32 prev_state = l_ptr->state;
439 u32 checkpoint = l_ptr->next_in_no;
440 int was_active_link = tipc_link_is_active(l_ptr); 432 int was_active_link = tipc_link_is_active(l_ptr);
441 struct tipc_node *owner = l_ptr->owner; 433 struct tipc_node *owner = l_ptr->owner;
434 struct tipc_link *pl = tipc_parallel_link(l_ptr);
442 435
443 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff)); 436 msg_set_session(l_ptr->pmsg, ((msg_session(l_ptr->pmsg) + 1) & 0xffff));
444 437
445 /* Link is down, accept any session */ 438 /* Link is down, accept any session */
446 l_ptr->peer_session = INVALID_SESSION; 439 l_ptr->peer_session = INVALID_SESSION;
447 440
448 /* Prepare for max packet size negotiation */ 441 /* Prepare for renewed mtu size negotiation */
449 link_init_max_pkt(l_ptr); 442 l_ptr->mtu = l_ptr->advertised_mtu;
450 443
451 l_ptr->state = RESET_UNKNOWN; 444 l_ptr->state = RESET_UNKNOWN;
452 445
@@ -456,21 +449,26 @@ void tipc_link_reset(struct tipc_link *l_ptr)
456 tipc_node_link_down(l_ptr->owner, l_ptr); 449 tipc_node_link_down(l_ptr->owner, l_ptr);
457 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr); 450 tipc_bearer_remove_dest(owner->net, l_ptr->bearer_id, l_ptr->addr);
458 451
459 if (was_active_link && tipc_node_active_links(l_ptr->owner)) { 452 if (was_active_link && tipc_node_is_up(l_ptr->owner) && (pl != l_ptr)) {
460 l_ptr->reset_checkpoint = checkpoint; 453 l_ptr->flags |= LINK_FAILINGOVER;
461 l_ptr->exp_msg_count = START_CHANGEOVER; 454 l_ptr->failover_checkpt = l_ptr->next_in_no;
455 pl->failover_pkts = FIRST_FAILOVER;
456 pl->failover_checkpt = l_ptr->next_in_no;
457 pl->failover_skb = l_ptr->reasm_buf;
458 } else {
459 kfree_skb(l_ptr->reasm_buf);
462 } 460 }
463
464 /* Clean up all queues, except inputq: */ 461 /* Clean up all queues, except inputq: */
465 __skb_queue_purge(&l_ptr->outqueue); 462 __skb_queue_purge(&l_ptr->transmq);
466 __skb_queue_purge(&l_ptr->deferred_queue); 463 __skb_queue_purge(&l_ptr->deferdq);
467 if (!owner->inputq) 464 if (!owner->inputq)
468 owner->inputq = &l_ptr->inputq; 465 owner->inputq = &l_ptr->inputq;
469 skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq); 466 skb_queue_splice_init(&l_ptr->wakeupq, owner->inputq);
470 if (!skb_queue_empty(owner->inputq)) 467 if (!skb_queue_empty(owner->inputq))
471 owner->action_flags |= TIPC_MSG_EVT; 468 owner->action_flags |= TIPC_MSG_EVT;
472 l_ptr->next_out = NULL; 469 tipc_link_purge_backlog(l_ptr);
473 l_ptr->unacked_window = 0; 470 l_ptr->reasm_buf = NULL;
471 l_ptr->rcv_unacked = 0;
474 l_ptr->checkpoint = 1; 472 l_ptr->checkpoint = 1;
475 l_ptr->next_out_no = 1; 473 l_ptr->next_out_no = 1;
476 l_ptr->fsm_msg_cnt = 0; 474 l_ptr->fsm_msg_cnt = 0;
@@ -521,8 +519,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
521 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT)) 519 if (!(l_ptr->flags & LINK_STARTED) && (event != STARTING_EVT))
522 return; /* Not yet. */ 520 return; /* Not yet. */
523 521
524 /* Check whether changeover is going on */ 522 if (l_ptr->flags & LINK_FAILINGOVER) {
525 if (l_ptr->exp_msg_count) {
526 if (event == TIMEOUT_EVT) 523 if (event == TIMEOUT_EVT)
527 link_set_timer(l_ptr, cont_intv); 524 link_set_timer(l_ptr, cont_intv);
528 return; 525 return;
@@ -539,11 +536,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
539 l_ptr->checkpoint = l_ptr->next_in_no; 536 l_ptr->checkpoint = l_ptr->next_in_no;
540 if (tipc_bclink_acks_missing(l_ptr->owner)) { 537 if (tipc_bclink_acks_missing(l_ptr->owner)) {
541 tipc_link_proto_xmit(l_ptr, STATE_MSG, 538 tipc_link_proto_xmit(l_ptr, STATE_MSG,
542 0, 0, 0, 0, 0); 539 0, 0, 0, 0);
543 l_ptr->fsm_msg_cnt++;
544 } else if (l_ptr->max_pkt < l_ptr->max_pkt_target) {
545 tipc_link_proto_xmit(l_ptr, STATE_MSG,
546 1, 0, 0, 0, 0);
547 l_ptr->fsm_msg_cnt++; 540 l_ptr->fsm_msg_cnt++;
548 } 541 }
549 link_set_timer(l_ptr, cont_intv); 542 link_set_timer(l_ptr, cont_intv);
@@ -551,7 +544,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
551 } 544 }
552 l_ptr->state = WORKING_UNKNOWN; 545 l_ptr->state = WORKING_UNKNOWN;
553 l_ptr->fsm_msg_cnt = 0; 546 l_ptr->fsm_msg_cnt = 0;
554 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); 547 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
555 l_ptr->fsm_msg_cnt++; 548 l_ptr->fsm_msg_cnt++;
556 link_set_timer(l_ptr, cont_intv / 4); 549 link_set_timer(l_ptr, cont_intv / 4);
557 break; 550 break;
@@ -562,7 +555,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
562 l_ptr->state = RESET_RESET; 555 l_ptr->state = RESET_RESET;
563 l_ptr->fsm_msg_cnt = 0; 556 l_ptr->fsm_msg_cnt = 0;
564 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG, 557 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
565 0, 0, 0, 0, 0); 558 0, 0, 0, 0);
566 l_ptr->fsm_msg_cnt++; 559 l_ptr->fsm_msg_cnt++;
567 link_set_timer(l_ptr, cont_intv); 560 link_set_timer(l_ptr, cont_intv);
568 break; 561 break;
@@ -585,7 +578,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
585 l_ptr->state = RESET_RESET; 578 l_ptr->state = RESET_RESET;
586 l_ptr->fsm_msg_cnt = 0; 579 l_ptr->fsm_msg_cnt = 0;
587 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG, 580 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
588 0, 0, 0, 0, 0); 581 0, 0, 0, 0);
589 l_ptr->fsm_msg_cnt++; 582 l_ptr->fsm_msg_cnt++;
590 link_set_timer(l_ptr, cont_intv); 583 link_set_timer(l_ptr, cont_intv);
591 break; 584 break;
@@ -596,13 +589,13 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
596 l_ptr->checkpoint = l_ptr->next_in_no; 589 l_ptr->checkpoint = l_ptr->next_in_no;
597 if (tipc_bclink_acks_missing(l_ptr->owner)) { 590 if (tipc_bclink_acks_missing(l_ptr->owner)) {
598 tipc_link_proto_xmit(l_ptr, STATE_MSG, 591 tipc_link_proto_xmit(l_ptr, STATE_MSG,
599 0, 0, 0, 0, 0); 592 0, 0, 0, 0);
600 l_ptr->fsm_msg_cnt++; 593 l_ptr->fsm_msg_cnt++;
601 } 594 }
602 link_set_timer(l_ptr, cont_intv); 595 link_set_timer(l_ptr, cont_intv);
603 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) { 596 } else if (l_ptr->fsm_msg_cnt < l_ptr->abort_limit) {
604 tipc_link_proto_xmit(l_ptr, STATE_MSG, 597 tipc_link_proto_xmit(l_ptr, STATE_MSG,
605 1, 0, 0, 0, 0); 598 1, 0, 0, 0);
606 l_ptr->fsm_msg_cnt++; 599 l_ptr->fsm_msg_cnt++;
607 link_set_timer(l_ptr, cont_intv / 4); 600 link_set_timer(l_ptr, cont_intv / 4);
608 } else { /* Link has failed */ 601 } else { /* Link has failed */
@@ -612,7 +605,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
612 l_ptr->state = RESET_UNKNOWN; 605 l_ptr->state = RESET_UNKNOWN;
613 l_ptr->fsm_msg_cnt = 0; 606 l_ptr->fsm_msg_cnt = 0;
614 tipc_link_proto_xmit(l_ptr, RESET_MSG, 607 tipc_link_proto_xmit(l_ptr, RESET_MSG,
615 0, 0, 0, 0, 0); 608 0, 0, 0, 0);
616 l_ptr->fsm_msg_cnt++; 609 l_ptr->fsm_msg_cnt++;
617 link_set_timer(l_ptr, cont_intv); 610 link_set_timer(l_ptr, cont_intv);
618 } 611 }
@@ -632,7 +625,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
632 l_ptr->state = WORKING_WORKING; 625 l_ptr->state = WORKING_WORKING;
633 l_ptr->fsm_msg_cnt = 0; 626 l_ptr->fsm_msg_cnt = 0;
634 link_activate(l_ptr); 627 link_activate(l_ptr);
635 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); 628 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
636 l_ptr->fsm_msg_cnt++; 629 l_ptr->fsm_msg_cnt++;
637 if (l_ptr->owner->working_links == 1) 630 if (l_ptr->owner->working_links == 1)
638 tipc_link_sync_xmit(l_ptr); 631 tipc_link_sync_xmit(l_ptr);
@@ -642,7 +635,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
642 l_ptr->state = RESET_RESET; 635 l_ptr->state = RESET_RESET;
643 l_ptr->fsm_msg_cnt = 0; 636 l_ptr->fsm_msg_cnt = 0;
644 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG, 637 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
645 1, 0, 0, 0, 0); 638 1, 0, 0, 0);
646 l_ptr->fsm_msg_cnt++; 639 l_ptr->fsm_msg_cnt++;
647 link_set_timer(l_ptr, cont_intv); 640 link_set_timer(l_ptr, cont_intv);
648 break; 641 break;
@@ -652,7 +645,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
652 link_set_timer(l_ptr, cont_intv); 645 link_set_timer(l_ptr, cont_intv);
653 break; 646 break;
654 case TIMEOUT_EVT: 647 case TIMEOUT_EVT:
655 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0, 0); 648 tipc_link_proto_xmit(l_ptr, RESET_MSG, 0, 0, 0, 0);
656 l_ptr->fsm_msg_cnt++; 649 l_ptr->fsm_msg_cnt++;
657 link_set_timer(l_ptr, cont_intv); 650 link_set_timer(l_ptr, cont_intv);
658 break; 651 break;
@@ -670,7 +663,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
670 l_ptr->state = WORKING_WORKING; 663 l_ptr->state = WORKING_WORKING;
671 l_ptr->fsm_msg_cnt = 0; 664 l_ptr->fsm_msg_cnt = 0;
672 link_activate(l_ptr); 665 link_activate(l_ptr);
673 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0, 0); 666 tipc_link_proto_xmit(l_ptr, STATE_MSG, 1, 0, 0, 0);
674 l_ptr->fsm_msg_cnt++; 667 l_ptr->fsm_msg_cnt++;
675 if (l_ptr->owner->working_links == 1) 668 if (l_ptr->owner->working_links == 1)
676 tipc_link_sync_xmit(l_ptr); 669 tipc_link_sync_xmit(l_ptr);
@@ -680,7 +673,7 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
680 break; 673 break;
681 case TIMEOUT_EVT: 674 case TIMEOUT_EVT:
682 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG, 675 tipc_link_proto_xmit(l_ptr, ACTIVATE_MSG,
683 0, 0, 0, 0, 0); 676 0, 0, 0, 0);
684 l_ptr->fsm_msg_cnt++; 677 l_ptr->fsm_msg_cnt++;
685 link_set_timer(l_ptr, cont_intv); 678 link_set_timer(l_ptr, cont_intv);
686 break; 679 break;
@@ -693,101 +686,65 @@ static void link_state_event(struct tipc_link *l_ptr, unsigned int event)
693 } 686 }
694} 687}
695 688
696/* tipc_link_cong: determine return value and how to treat the
697 * sent buffer during link congestion.
698 * - For plain, errorless user data messages we keep the buffer and
699 * return -ELINKONG.
700 * - For all other messages we discard the buffer and return -EHOSTUNREACH
701 * - For TIPC internal messages we also reset the link
702 */
703static int tipc_link_cong(struct tipc_link *link, struct sk_buff_head *list)
704{
705 struct sk_buff *skb = skb_peek(list);
706 struct tipc_msg *msg = buf_msg(skb);
707 uint imp = tipc_msg_tot_importance(msg);
708 u32 oport = msg_tot_origport(msg);
709
710 if (unlikely(imp > TIPC_CRITICAL_IMPORTANCE)) {
711 pr_warn("%s<%s>, send queue full", link_rst_msg, link->name);
712 tipc_link_reset(link);
713 goto drop;
714 }
715 if (unlikely(msg_errcode(msg)))
716 goto drop;
717 if (unlikely(msg_reroute_cnt(msg)))
718 goto drop;
719 if (TIPC_SKB_CB(skb)->wakeup_pending)
720 return -ELINKCONG;
721 if (link_schedule_user(link, oport, skb_queue_len(list), imp))
722 return -ELINKCONG;
723drop:
724 __skb_queue_purge(list);
725 return -EHOSTUNREACH;
726}
727
728/** 689/**
729 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked 690 * __tipc_link_xmit(): same as tipc_link_xmit, but destlink is known & locked
730 * @link: link to use 691 * @link: link to use
731 * @list: chain of buffers containing message 692 * @list: chain of buffers containing message
732 * 693 *
733 * Consumes the buffer chain, except when returning -ELINKCONG 694 * Consumes the buffer chain, except when returning -ELINKCONG,
734 * Returns 0 if success, otherwise errno: -ELINKCONG, -EMSGSIZE (plain socket 695 * since the caller then may want to make more send attempts.
735 * user data messages) or -EHOSTUNREACH (all other messages/senders) 696 * Returns 0 if success, or errno: -ELINKCONG, -EMSGSIZE or -ENOBUFS
736 * Only the socket functions tipc_send_stream() and tipc_send_packet() need 697 * Messages at TIPC_SYSTEM_IMPORTANCE are always accepted
737 * to act on the return value, since they may need to do more send attempts.
738 */ 698 */
739int __tipc_link_xmit(struct net *net, struct tipc_link *link, 699int __tipc_link_xmit(struct net *net, struct tipc_link *link,
740 struct sk_buff_head *list) 700 struct sk_buff_head *list)
741{ 701{
742 struct tipc_msg *msg = buf_msg(skb_peek(list)); 702 struct tipc_msg *msg = buf_msg(skb_peek(list));
743 uint psz = msg_size(msg); 703 unsigned int maxwin = link->window;
744 uint sndlim = link->queue_limit[0]; 704 unsigned int imp = msg_importance(msg);
745 uint imp = tipc_msg_tot_importance(msg); 705 uint mtu = link->mtu;
746 uint mtu = link->max_pkt;
747 uint ack = mod(link->next_in_no - 1); 706 uint ack = mod(link->next_in_no - 1);
748 uint seqno = link->next_out_no; 707 uint seqno = link->next_out_no;
749 uint bc_last_in = link->owner->bclink.last_in; 708 uint bc_last_in = link->owner->bclink.last_in;
750 struct tipc_media_addr *addr = &link->media_addr; 709 struct tipc_media_addr *addr = &link->media_addr;
751 struct sk_buff_head *outqueue = &link->outqueue; 710 struct sk_buff_head *transmq = &link->transmq;
711 struct sk_buff_head *backlogq = &link->backlogq;
752 struct sk_buff *skb, *tmp; 712 struct sk_buff *skb, *tmp;
753 713
754 /* Match queue limits against msg importance: */ 714 /* Match backlog limit against msg importance: */
755 if (unlikely(skb_queue_len(outqueue) >= link->queue_limit[imp])) 715 if (unlikely(link->backlog[imp].len >= link->backlog[imp].limit))
756 return tipc_link_cong(link, list); 716 return link_schedule_user(link, list);
757 717
758 /* Has valid packet limit been used ? */ 718 if (unlikely(msg_size(msg) > mtu)) {
759 if (unlikely(psz > mtu)) {
760 __skb_queue_purge(list); 719 __skb_queue_purge(list);
761 return -EMSGSIZE; 720 return -EMSGSIZE;
762 } 721 }
763 722 /* Prepare each packet for sending, and add to relevant queue: */
764 /* Prepare each packet for sending, and add to outqueue: */
765 skb_queue_walk_safe(list, skb, tmp) { 723 skb_queue_walk_safe(list, skb, tmp) {
766 __skb_unlink(skb, list); 724 __skb_unlink(skb, list);
767 msg = buf_msg(skb); 725 msg = buf_msg(skb);
768 msg_set_word(msg, 2, ((ack << 16) | mod(seqno))); 726 msg_set_seqno(msg, seqno);
727 msg_set_ack(msg, ack);
769 msg_set_bcast_ack(msg, bc_last_in); 728 msg_set_bcast_ack(msg, bc_last_in);
770 729
771 if (skb_queue_len(outqueue) < sndlim) { 730 if (likely(skb_queue_len(transmq) < maxwin)) {
772 __skb_queue_tail(outqueue, skb); 731 __skb_queue_tail(transmq, skb);
773 tipc_bearer_send(net, link->bearer_id, 732 tipc_bearer_send(net, link->bearer_id, skb, addr);
774 skb, addr); 733 link->rcv_unacked = 0;
775 link->next_out = NULL; 734 seqno++;
776 link->unacked_window = 0; 735 continue;
777 } else if (tipc_msg_bundle(outqueue, skb, mtu)) { 736 }
737 if (tipc_msg_bundle(skb_peek_tail(backlogq), skb, mtu)) {
778 link->stats.sent_bundled++; 738 link->stats.sent_bundled++;
779 continue; 739 continue;
780 } else if (tipc_msg_make_bundle(outqueue, skb, mtu, 740 }
781 link->addr)) { 741 if (tipc_msg_make_bundle(&skb, mtu, link->addr)) {
782 link->stats.sent_bundled++; 742 link->stats.sent_bundled++;
783 link->stats.sent_bundles++; 743 link->stats.sent_bundles++;
784 if (!link->next_out) 744 imp = msg_importance(buf_msg(skb));
785 link->next_out = skb_peek_tail(outqueue);
786 } else {
787 __skb_queue_tail(outqueue, skb);
788 if (!link->next_out)
789 link->next_out = skb;
790 } 745 }
746 __skb_queue_tail(backlogq, skb);
747 link->backlog[imp].len++;
791 seqno++; 748 seqno++;
792 } 749 }
793 link->next_out_no = seqno; 750 link->next_out_no = seqno;
@@ -808,13 +765,25 @@ static int __tipc_link_xmit_skb(struct tipc_link *link, struct sk_buff *skb)
808 return __tipc_link_xmit(link->owner->net, link, &head); 765 return __tipc_link_xmit(link->owner->net, link, &head);
809} 766}
810 767
768/* tipc_link_xmit_skb(): send single buffer to destination
769 * Buffers sent via this functon are generally TIPC_SYSTEM_IMPORTANCE
770 * messages, which will not be rejected
771 * The only exception is datagram messages rerouted after secondary
772 * lookup, which are rare and safe to dispose of anyway.
773 * TODO: Return real return value, and let callers use
774 * tipc_wait_for_sendpkt() where applicable
775 */
811int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode, 776int tipc_link_xmit_skb(struct net *net, struct sk_buff *skb, u32 dnode,
812 u32 selector) 777 u32 selector)
813{ 778{
814 struct sk_buff_head head; 779 struct sk_buff_head head;
780 int rc;
815 781
816 skb2list(skb, &head); 782 skb2list(skb, &head);
817 return tipc_link_xmit(net, &head, dnode, selector); 783 rc = tipc_link_xmit(net, &head, dnode, selector);
784 if (rc == -ELINKCONG)
785 kfree_skb(skb);
786 return 0;
818} 787}
819 788
820/** 789/**
@@ -841,12 +810,15 @@ int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dnode,
841 if (link) 810 if (link)
842 rc = __tipc_link_xmit(net, link, list); 811 rc = __tipc_link_xmit(net, link, list);
843 tipc_node_unlock(node); 812 tipc_node_unlock(node);
813 tipc_node_put(node);
844 } 814 }
845 if (link) 815 if (link)
846 return rc; 816 return rc;
847 817
848 if (likely(in_own_node(net, dnode))) 818 if (likely(in_own_node(net, dnode))) {
849 return tipc_sk_rcv(net, list); 819 tipc_sk_rcv(net, list);
820 return 0;
821 }
850 822
851 __skb_queue_purge(list); 823 __skb_queue_purge(list);
852 return rc; 824 return rc;
@@ -893,14 +865,6 @@ static void tipc_link_sync_rcv(struct tipc_node *n, struct sk_buff *buf)
893 kfree_skb(buf); 865 kfree_skb(buf);
894} 866}
895 867
896struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
897 const struct sk_buff *skb)
898{
899 if (skb_queue_is_last(list, skb))
900 return NULL;
901 return skb->next;
902}
903
904/* 868/*
905 * tipc_link_push_packets - push unsent packets to bearer 869 * tipc_link_push_packets - push unsent packets to bearer
906 * 870 *
@@ -909,30 +873,24 @@ struct sk_buff *tipc_skb_queue_next(const struct sk_buff_head *list,
909 * 873 *
910 * Called with node locked 874 * Called with node locked
911 */ 875 */
912void tipc_link_push_packets(struct tipc_link *l_ptr) 876void tipc_link_push_packets(struct tipc_link *link)
913{ 877{
914 struct sk_buff_head *outqueue = &l_ptr->outqueue; 878 struct sk_buff *skb;
915 struct sk_buff *skb = l_ptr->next_out;
916 struct tipc_msg *msg; 879 struct tipc_msg *msg;
917 u32 next, first; 880 unsigned int ack = mod(link->next_in_no - 1);
918 881
919 skb_queue_walk_from(outqueue, skb) { 882 while (skb_queue_len(&link->transmq) < link->window) {
920 msg = buf_msg(skb); 883 skb = __skb_dequeue(&link->backlogq);
921 next = msg_seqno(msg); 884 if (!skb)
922 first = buf_seqno(skb_peek(outqueue));
923
924 if (mod(next - first) < l_ptr->queue_limit[0]) {
925 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
926 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in);
927 if (msg_user(msg) == MSG_BUNDLER)
928 TIPC_SKB_CB(skb)->bundling = false;
929 tipc_bearer_send(l_ptr->owner->net,
930 l_ptr->bearer_id, skb,
931 &l_ptr->media_addr);
932 l_ptr->next_out = tipc_skb_queue_next(outqueue, skb);
933 } else {
934 break; 885 break;
935 } 886 msg = buf_msg(skb);
887 link->backlog[msg_importance(msg)].len--;
888 msg_set_ack(msg, ack);
889 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
890 link->rcv_unacked = 0;
891 __skb_queue_tail(&link->transmq, skb);
892 tipc_bearer_send(link->owner->net, link->bearer_id,
893 skb, &link->media_addr);
936 } 894 }
937} 895}
938 896
@@ -979,7 +937,6 @@ static void link_retransmit_failure(struct tipc_link *l_ptr,
979 (unsigned long) TIPC_SKB_CB(buf)->handle); 937 (unsigned long) TIPC_SKB_CB(buf)->handle);
980 938
981 n_ptr = tipc_bclink_retransmit_to(net); 939 n_ptr = tipc_bclink_retransmit_to(net);
982 tipc_node_lock(n_ptr);
983 940
984 tipc_addr_string_fill(addr_string, n_ptr->addr); 941 tipc_addr_string_fill(addr_string, n_ptr->addr);
985 pr_info("Broadcast link info for %s\n", addr_string); 942 pr_info("Broadcast link info for %s\n", addr_string);
@@ -991,9 +948,7 @@ static void link_retransmit_failure(struct tipc_link *l_ptr,
991 n_ptr->bclink.oos_state, 948 n_ptr->bclink.oos_state,
992 n_ptr->bclink.last_sent); 949 n_ptr->bclink.last_sent);
993 950
994 tipc_node_unlock(n_ptr); 951 n_ptr->action_flags |= TIPC_BCAST_RESET;
995
996 tipc_bclink_set_flags(net, TIPC_BCLINK_RESET);
997 l_ptr->stale_count = 0; 952 l_ptr->stale_count = 0;
998 } 953 }
999} 954}
@@ -1019,8 +974,8 @@ void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
1019 l_ptr->stale_count = 1; 974 l_ptr->stale_count = 1;
1020 } 975 }
1021 976
1022 skb_queue_walk_from(&l_ptr->outqueue, skb) { 977 skb_queue_walk_from(&l_ptr->transmq, skb) {
1023 if (!retransmits || skb == l_ptr->next_out) 978 if (!retransmits)
1024 break; 979 break;
1025 msg = buf_msg(skb); 980 msg = buf_msg(skb);
1026 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); 981 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
@@ -1032,72 +987,43 @@ void tipc_link_retransmit(struct tipc_link *l_ptr, struct sk_buff *skb,
1032 } 987 }
1033} 988}
1034 989
1035static void link_retrieve_defq(struct tipc_link *link, 990/* link_synch(): check if all packets arrived before the synch
1036 struct sk_buff_head *list) 991 * point have been consumed
1037{ 992 * Returns true if the parallel links are synched, otherwise false
1038 u32 seq_no;
1039
1040 if (skb_queue_empty(&link->deferred_queue))
1041 return;
1042
1043 seq_no = buf_seqno(skb_peek(&link->deferred_queue));
1044 if (seq_no == mod(link->next_in_no))
1045 skb_queue_splice_tail_init(&link->deferred_queue, list);
1046}
1047
1048/**
1049 * link_recv_buf_validate - validate basic format of received message
1050 *
1051 * This routine ensures a TIPC message has an acceptable header, and at least
1052 * as much data as the header indicates it should. The routine also ensures
1053 * that the entire message header is stored in the main fragment of the message
1054 * buffer, to simplify future access to message header fields.
1055 *
1056 * Note: Having extra info present in the message header or data areas is OK.
1057 * TIPC will ignore the excess, under the assumption that it is optional info
1058 * introduced by a later release of the protocol.
1059 */ 993 */
1060static int link_recv_buf_validate(struct sk_buff *buf) 994static bool link_synch(struct tipc_link *l)
1061{ 995{
1062 static u32 min_data_hdr_size[8] = { 996 unsigned int post_synch;
1063 SHORT_H_SIZE, MCAST_H_SIZE, NAMED_H_SIZE, BASIC_H_SIZE, 997 struct tipc_link *pl;
1064 MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE, MAX_H_SIZE
1065 };
1066 998
1067 struct tipc_msg *msg; 999 pl = tipc_parallel_link(l);
1068 u32 tipc_hdr[2]; 1000 if (pl == l)
1069 u32 size; 1001 goto synched;
1070 u32 hdr_size;
1071 u32 min_hdr_size;
1072 1002
1073 /* If this packet comes from the defer queue, the skb has already 1003 /* Was last pre-synch packet added to input queue ? */
1074 * been validated 1004 if (less_eq(pl->next_in_no, l->synch_point))
1075 */ 1005 return false;
1076 if (unlikely(TIPC_SKB_CB(buf)->deferred))
1077 return 1;
1078
1079 if (unlikely(buf->len < MIN_H_SIZE))
1080 return 0;
1081
1082 msg = skb_header_pointer(buf, 0, sizeof(tipc_hdr), tipc_hdr);
1083 if (msg == NULL)
1084 return 0;
1085 1006
1086 if (unlikely(msg_version(msg) != TIPC_VERSION)) 1007 /* Is it still in the input queue ? */
1087 return 0; 1008 post_synch = mod(pl->next_in_no - l->synch_point) - 1;
1009 if (skb_queue_len(&pl->inputq) > post_synch)
1010 return false;
1011synched:
1012 l->flags &= ~LINK_SYNCHING;
1013 return true;
1014}
1088 1015
1089 size = msg_size(msg); 1016static void link_retrieve_defq(struct tipc_link *link,
1090 hdr_size = msg_hdr_sz(msg); 1017 struct sk_buff_head *list)
1091 min_hdr_size = msg_isdata(msg) ? 1018{
1092 min_data_hdr_size[msg_type(msg)] : INT_H_SIZE; 1019 u32 seq_no;
1093 1020
1094 if (unlikely((hdr_size < min_hdr_size) || 1021 if (skb_queue_empty(&link->deferdq))
1095 (size < hdr_size) || 1022 return;
1096 (buf->len < size) ||
1097 (size - hdr_size > TIPC_MAX_USER_MSG_SIZE)))
1098 return 0;
1099 1023
1100 return pskb_may_pull(buf, hdr_size); 1024 seq_no = buf_seqno(skb_peek(&link->deferdq));
1025 if (seq_no == mod(link->next_in_no))
1026 skb_queue_splice_tail_init(&link->deferdq, list);
1101} 1027}
1102 1028
1103/** 1029/**
@@ -1125,16 +1051,11 @@ void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
1125 1051
1126 while ((skb = __skb_dequeue(&head))) { 1052 while ((skb = __skb_dequeue(&head))) {
1127 /* Ensure message is well-formed */ 1053 /* Ensure message is well-formed */
1128 if (unlikely(!link_recv_buf_validate(skb))) 1054 if (unlikely(!tipc_msg_validate(skb)))
1129 goto discard;
1130
1131 /* Ensure message data is a single contiguous unit */
1132 if (unlikely(skb_linearize(skb)))
1133 goto discard; 1055 goto discard;
1134 1056
1135 /* Handle arrival of a non-unicast link message */ 1057 /* Handle arrival of a non-unicast link message */
1136 msg = buf_msg(skb); 1058 msg = buf_msg(skb);
1137
1138 if (unlikely(msg_non_seq(msg))) { 1059 if (unlikely(msg_non_seq(msg))) {
1139 if (msg_user(msg) == LINK_CONFIG) 1060 if (msg_user(msg) == LINK_CONFIG)
1140 tipc_disc_rcv(net, skb, b_ptr); 1061 tipc_disc_rcv(net, skb, b_ptr);
@@ -1152,8 +1073,8 @@ void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
1152 n_ptr = tipc_node_find(net, msg_prevnode(msg)); 1073 n_ptr = tipc_node_find(net, msg_prevnode(msg));
1153 if (unlikely(!n_ptr)) 1074 if (unlikely(!n_ptr))
1154 goto discard; 1075 goto discard;
1155 tipc_node_lock(n_ptr);
1156 1076
1077 tipc_node_lock(n_ptr);
1157 /* Locate unicast link endpoint that should handle message */ 1078 /* Locate unicast link endpoint that should handle message */
1158 l_ptr = n_ptr->links[b_ptr->identity]; 1079 l_ptr = n_ptr->links[b_ptr->identity];
1159 if (unlikely(!l_ptr)) 1080 if (unlikely(!l_ptr))
@@ -1175,21 +1096,20 @@ void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
1175 ackd = msg_ack(msg); 1096 ackd = msg_ack(msg);
1176 1097
1177 /* Release acked messages */ 1098 /* Release acked messages */
1178 if (n_ptr->bclink.recv_permitted) 1099 if (unlikely(n_ptr->bclink.acked != msg_bcast_ack(msg)))
1179 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg)); 1100 tipc_bclink_acknowledge(n_ptr, msg_bcast_ack(msg));
1180 1101
1181 released = 0; 1102 released = 0;
1182 skb_queue_walk_safe(&l_ptr->outqueue, skb1, tmp) { 1103 skb_queue_walk_safe(&l_ptr->transmq, skb1, tmp) {
1183 if (skb1 == l_ptr->next_out || 1104 if (more(buf_seqno(skb1), ackd))
1184 more(buf_seqno(skb1), ackd))
1185 break; 1105 break;
1186 __skb_unlink(skb1, &l_ptr->outqueue); 1106 __skb_unlink(skb1, &l_ptr->transmq);
1187 kfree_skb(skb1); 1107 kfree_skb(skb1);
1188 released = 1; 1108 released = 1;
1189 } 1109 }
1190 1110
1191 /* Try sending any messages link endpoint has pending */ 1111 /* Try sending any messages link endpoint has pending */
1192 if (unlikely(l_ptr->next_out)) 1112 if (unlikely(skb_queue_len(&l_ptr->backlogq)))
1193 tipc_link_push_packets(l_ptr); 1113 tipc_link_push_packets(l_ptr);
1194 1114
1195 if (released && !skb_queue_empty(&l_ptr->wakeupq)) 1115 if (released && !skb_queue_empty(&l_ptr->wakeupq))
@@ -1223,18 +1143,26 @@ void tipc_rcv(struct net *net, struct sk_buff *skb, struct tipc_bearer *b_ptr)
1223 skb = NULL; 1143 skb = NULL;
1224 goto unlock; 1144 goto unlock;
1225 } 1145 }
1146 /* Synchronize with parallel link if applicable */
1147 if (unlikely((l_ptr->flags & LINK_SYNCHING) && !msg_dup(msg))) {
1148 link_handle_out_of_seq_msg(l_ptr, skb);
1149 if (link_synch(l_ptr))
1150 link_retrieve_defq(l_ptr, &head);
1151 skb = NULL;
1152 goto unlock;
1153 }
1226 l_ptr->next_in_no++; 1154 l_ptr->next_in_no++;
1227 if (unlikely(!skb_queue_empty(&l_ptr->deferred_queue))) 1155 if (unlikely(!skb_queue_empty(&l_ptr->deferdq)))
1228 link_retrieve_defq(l_ptr, &head); 1156 link_retrieve_defq(l_ptr, &head);
1229 1157 if (unlikely(++l_ptr->rcv_unacked >= TIPC_MIN_LINK_WIN)) {
1230 if (unlikely(++l_ptr->unacked_window >= TIPC_MIN_LINK_WIN)) {
1231 l_ptr->stats.sent_acks++; 1158 l_ptr->stats.sent_acks++;
1232 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0); 1159 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0);
1233 } 1160 }
1234 tipc_link_input(l_ptr, skb); 1161 tipc_link_input(l_ptr, skb);
1235 skb = NULL; 1162 skb = NULL;
1236unlock: 1163unlock:
1237 tipc_node_unlock(n_ptr); 1164 tipc_node_unlock(n_ptr);
1165 tipc_node_put(n_ptr);
1238discard: 1166discard:
1239 if (unlikely(skb)) 1167 if (unlikely(skb))
1240 kfree_skb(skb); 1168 kfree_skb(skb);
@@ -1271,7 +1199,7 @@ static bool tipc_data_input(struct tipc_link *link, struct sk_buff *skb)
1271 node->action_flags |= TIPC_NAMED_MSG_EVT; 1199 node->action_flags |= TIPC_NAMED_MSG_EVT;
1272 return true; 1200 return true;
1273 case MSG_BUNDLER: 1201 case MSG_BUNDLER:
1274 case CHANGEOVER_PROTOCOL: 1202 case TUNNEL_PROTOCOL:
1275 case MSG_FRAGMENTER: 1203 case MSG_FRAGMENTER:
1276 case BCAST_PROTOCOL: 1204 case BCAST_PROTOCOL:
1277 return false; 1205 return false;
@@ -1298,8 +1226,14 @@ static void tipc_link_input(struct tipc_link *link, struct sk_buff *skb)
1298 return; 1226 return;
1299 1227
1300 switch (msg_user(msg)) { 1228 switch (msg_user(msg)) {
1301 case CHANGEOVER_PROTOCOL: 1229 case TUNNEL_PROTOCOL:
1302 if (!tipc_link_tunnel_rcv(node, &skb)) 1230 if (msg_dup(msg)) {
1231 link->flags |= LINK_SYNCHING;
1232 link->synch_point = msg_seqno(msg_get_wrapped(msg));
1233 kfree_skb(skb);
1234 break;
1235 }
1236 if (!tipc_link_failover_rcv(link, &skb))
1303 break; 1237 break;
1304 if (msg_user(buf_msg(skb)) != MSG_BUNDLER) { 1238 if (msg_user(buf_msg(skb)) != MSG_BUNDLER) {
1305 tipc_data_input(link, skb); 1239 tipc_data_input(link, skb);
@@ -1394,11 +1328,10 @@ static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
1394 return; 1328 return;
1395 } 1329 }
1396 1330
1397 if (tipc_link_defer_pkt(&l_ptr->deferred_queue, buf)) { 1331 if (tipc_link_defer_pkt(&l_ptr->deferdq, buf)) {
1398 l_ptr->stats.deferred_recv++; 1332 l_ptr->stats.deferred_recv++;
1399 TIPC_SKB_CB(buf)->deferred = true; 1333 if ((skb_queue_len(&l_ptr->deferdq) % TIPC_MIN_LINK_WIN) == 1)
1400 if ((skb_queue_len(&l_ptr->deferred_queue) % 16) == 1) 1334 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0);
1401 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, 0, 0, 0, 0);
1402 } else { 1335 } else {
1403 l_ptr->stats.duplicates++; 1336 l_ptr->stats.duplicates++;
1404 } 1337 }
@@ -1408,15 +1341,15 @@ static void link_handle_out_of_seq_msg(struct tipc_link *l_ptr,
1408 * Send protocol message to the other endpoint. 1341 * Send protocol message to the other endpoint.
1409 */ 1342 */
1410void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg, 1343void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1411 u32 gap, u32 tolerance, u32 priority, u32 ack_mtu) 1344 u32 gap, u32 tolerance, u32 priority)
1412{ 1345{
1413 struct sk_buff *buf = NULL; 1346 struct sk_buff *buf = NULL;
1414 struct tipc_msg *msg = l_ptr->pmsg; 1347 struct tipc_msg *msg = l_ptr->pmsg;
1415 u32 msg_size = sizeof(l_ptr->proto_msg); 1348 u32 msg_size = sizeof(l_ptr->proto_msg);
1416 int r_flag; 1349 int r_flag;
1417 1350
1418 /* Don't send protocol message during link changeover */ 1351 /* Don't send protocol message during link failover */
1419 if (l_ptr->exp_msg_count) 1352 if (l_ptr->flags & LINK_FAILINGOVER)
1420 return; 1353 return;
1421 1354
1422 /* Abort non-RESET send if communication with node is prohibited */ 1355 /* Abort non-RESET send if communication with node is prohibited */
@@ -1434,11 +1367,11 @@ void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1434 1367
1435 if (!tipc_link_is_up(l_ptr)) 1368 if (!tipc_link_is_up(l_ptr))
1436 return; 1369 return;
1437 if (l_ptr->next_out) 1370 if (skb_queue_len(&l_ptr->backlogq))
1438 next_sent = buf_seqno(l_ptr->next_out); 1371 next_sent = buf_seqno(skb_peek(&l_ptr->backlogq));
1439 msg_set_next_sent(msg, next_sent); 1372 msg_set_next_sent(msg, next_sent);
1440 if (!skb_queue_empty(&l_ptr->deferred_queue)) { 1373 if (!skb_queue_empty(&l_ptr->deferdq)) {
1441 u32 rec = buf_seqno(skb_peek(&l_ptr->deferred_queue)); 1374 u32 rec = buf_seqno(skb_peek(&l_ptr->deferdq));
1442 gap = mod(rec - mod(l_ptr->next_in_no)); 1375 gap = mod(rec - mod(l_ptr->next_in_no));
1443 } 1376 }
1444 msg_set_seq_gap(msg, gap); 1377 msg_set_seq_gap(msg, gap);
@@ -1446,35 +1379,20 @@ void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1446 l_ptr->stats.sent_nacks++; 1379 l_ptr->stats.sent_nacks++;
1447 msg_set_link_tolerance(msg, tolerance); 1380 msg_set_link_tolerance(msg, tolerance);
1448 msg_set_linkprio(msg, priority); 1381 msg_set_linkprio(msg, priority);
1449 msg_set_max_pkt(msg, ack_mtu); 1382 msg_set_max_pkt(msg, l_ptr->mtu);
1450 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); 1383 msg_set_ack(msg, mod(l_ptr->next_in_no - 1));
1451 msg_set_probe(msg, probe_msg != 0); 1384 msg_set_probe(msg, probe_msg != 0);
1452 if (probe_msg) { 1385 if (probe_msg)
1453 u32 mtu = l_ptr->max_pkt;
1454
1455 if ((mtu < l_ptr->max_pkt_target) &&
1456 link_working_working(l_ptr) &&
1457 l_ptr->fsm_msg_cnt) {
1458 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1459 if (l_ptr->max_pkt_probes == 10) {
1460 l_ptr->max_pkt_target = (msg_size - 4);
1461 l_ptr->max_pkt_probes = 0;
1462 msg_size = (mtu + (l_ptr->max_pkt_target - mtu)/2 + 2) & ~3;
1463 }
1464 l_ptr->max_pkt_probes++;
1465 }
1466
1467 l_ptr->stats.sent_probes++; 1386 l_ptr->stats.sent_probes++;
1468 }
1469 l_ptr->stats.sent_states++; 1387 l_ptr->stats.sent_states++;
1470 } else { /* RESET_MSG or ACTIVATE_MSG */ 1388 } else { /* RESET_MSG or ACTIVATE_MSG */
1471 msg_set_ack(msg, mod(l_ptr->reset_checkpoint - 1)); 1389 msg_set_ack(msg, mod(l_ptr->failover_checkpt - 1));
1472 msg_set_seq_gap(msg, 0); 1390 msg_set_seq_gap(msg, 0);
1473 msg_set_next_sent(msg, 1); 1391 msg_set_next_sent(msg, 1);
1474 msg_set_probe(msg, 0); 1392 msg_set_probe(msg, 0);
1475 msg_set_link_tolerance(msg, l_ptr->tolerance); 1393 msg_set_link_tolerance(msg, l_ptr->tolerance);
1476 msg_set_linkprio(msg, l_ptr->priority); 1394 msg_set_linkprio(msg, l_ptr->priority);
1477 msg_set_max_pkt(msg, l_ptr->max_pkt_target); 1395 msg_set_max_pkt(msg, l_ptr->advertised_mtu);
1478 } 1396 }
1479 1397
1480 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr)); 1398 r_flag = (l_ptr->owner->working_links > tipc_link_is_up(l_ptr));
@@ -1490,10 +1408,9 @@ void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int probe_msg,
1490 1408
1491 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg)); 1409 skb_copy_to_linear_data(buf, msg, sizeof(l_ptr->proto_msg));
1492 buf->priority = TC_PRIO_CONTROL; 1410 buf->priority = TC_PRIO_CONTROL;
1493
1494 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf, 1411 tipc_bearer_send(l_ptr->owner->net, l_ptr->bearer_id, buf,
1495 &l_ptr->media_addr); 1412 &l_ptr->media_addr);
1496 l_ptr->unacked_window = 0; 1413 l_ptr->rcv_unacked = 0;
1497 kfree_skb(buf); 1414 kfree_skb(buf);
1498} 1415}
1499 1416
@@ -1506,13 +1423,10 @@ static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
1506 struct sk_buff *buf) 1423 struct sk_buff *buf)
1507{ 1424{
1508 u32 rec_gap = 0; 1425 u32 rec_gap = 0;
1509 u32 max_pkt_info;
1510 u32 max_pkt_ack;
1511 u32 msg_tol; 1426 u32 msg_tol;
1512 struct tipc_msg *msg = buf_msg(buf); 1427 struct tipc_msg *msg = buf_msg(buf);
1513 1428
1514 /* Discard protocol message during link changeover */ 1429 if (l_ptr->flags & LINK_FAILINGOVER)
1515 if (l_ptr->exp_msg_count)
1516 goto exit; 1430 goto exit;
1517 1431
1518 if (l_ptr->net_plane != msg_net_plane(msg)) 1432 if (l_ptr->net_plane != msg_net_plane(msg))
@@ -1551,15 +1465,8 @@ static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
1551 if (msg_linkprio(msg) > l_ptr->priority) 1465 if (msg_linkprio(msg) > l_ptr->priority)
1552 l_ptr->priority = msg_linkprio(msg); 1466 l_ptr->priority = msg_linkprio(msg);
1553 1467
1554 max_pkt_info = msg_max_pkt(msg); 1468 if (l_ptr->mtu > msg_max_pkt(msg))
1555 if (max_pkt_info) { 1469 l_ptr->mtu = msg_max_pkt(msg);
1556 if (max_pkt_info < l_ptr->max_pkt_target)
1557 l_ptr->max_pkt_target = max_pkt_info;
1558 if (l_ptr->max_pkt > l_ptr->max_pkt_target)
1559 l_ptr->max_pkt = l_ptr->max_pkt_target;
1560 } else {
1561 l_ptr->max_pkt = l_ptr->max_pkt_target;
1562 }
1563 1470
1564 /* Synchronize broadcast link info, if not done previously */ 1471 /* Synchronize broadcast link info, if not done previously */
1565 if (!tipc_node_is_up(l_ptr->owner)) { 1472 if (!tipc_node_is_up(l_ptr->owner)) {
@@ -1604,18 +1511,8 @@ static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
1604 mod(l_ptr->next_in_no)); 1511 mod(l_ptr->next_in_no));
1605 } 1512 }
1606 1513
1607 max_pkt_ack = msg_max_pkt(msg); 1514 if (msg_probe(msg))
1608 if (max_pkt_ack > l_ptr->max_pkt) {
1609 l_ptr->max_pkt = max_pkt_ack;
1610 l_ptr->max_pkt_probes = 0;
1611 }
1612
1613 max_pkt_ack = 0;
1614 if (msg_probe(msg)) {
1615 l_ptr->stats.recv_probes++; 1515 l_ptr->stats.recv_probes++;
1616 if (msg_size(msg) > sizeof(l_ptr->proto_msg))
1617 max_pkt_ack = msg_size(msg);
1618 }
1619 1516
1620 /* Protocol message before retransmits, reduce loss risk */ 1517 /* Protocol message before retransmits, reduce loss risk */
1621 if (l_ptr->owner->bclink.recv_permitted) 1518 if (l_ptr->owner->bclink.recv_permitted)
@@ -1623,12 +1520,12 @@ static void tipc_link_proto_rcv(struct tipc_link *l_ptr,
1623 msg_last_bcast(msg)); 1520 msg_last_bcast(msg));
1624 1521
1625 if (rec_gap || (msg_probe(msg))) { 1522 if (rec_gap || (msg_probe(msg))) {
1626 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0, rec_gap, 0, 1523 tipc_link_proto_xmit(l_ptr, STATE_MSG, 0,
1627 0, max_pkt_ack); 1524 rec_gap, 0, 0);
1628 } 1525 }
1629 if (msg_seq_gap(msg)) { 1526 if (msg_seq_gap(msg)) {
1630 l_ptr->stats.recv_nacks++; 1527 l_ptr->stats.recv_nacks++;
1631 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->outqueue), 1528 tipc_link_retransmit(l_ptr, skb_peek(&l_ptr->transmq),
1632 msg_seq_gap(msg)); 1529 msg_seq_gap(msg));
1633 } 1530 }
1634 break; 1531 break;
@@ -1675,7 +1572,7 @@ static void tipc_link_tunnel_xmit(struct tipc_link *l_ptr,
1675 */ 1572 */
1676void tipc_link_failover_send_queue(struct tipc_link *l_ptr) 1573void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1677{ 1574{
1678 u32 msgcount = skb_queue_len(&l_ptr->outqueue); 1575 int msgcount;
1679 struct tipc_link *tunnel = l_ptr->owner->active_links[0]; 1576 struct tipc_link *tunnel = l_ptr->owner->active_links[0];
1680 struct tipc_msg tunnel_hdr; 1577 struct tipc_msg tunnel_hdr;
1681 struct sk_buff *skb; 1578 struct sk_buff *skb;
@@ -1684,12 +1581,15 @@ void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1684 if (!tunnel) 1581 if (!tunnel)
1685 return; 1582 return;
1686 1583
1687 tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, CHANGEOVER_PROTOCOL, 1584 tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, TUNNEL_PROTOCOL,
1688 ORIGINAL_MSG, INT_H_SIZE, l_ptr->addr); 1585 FAILOVER_MSG, INT_H_SIZE, l_ptr->addr);
1586 skb_queue_splice_tail_init(&l_ptr->backlogq, &l_ptr->transmq);
1587 tipc_link_purge_backlog(l_ptr);
1588 msgcount = skb_queue_len(&l_ptr->transmq);
1689 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id); 1589 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id);
1690 msg_set_msgcnt(&tunnel_hdr, msgcount); 1590 msg_set_msgcnt(&tunnel_hdr, msgcount);
1691 1591
1692 if (skb_queue_empty(&l_ptr->outqueue)) { 1592 if (skb_queue_empty(&l_ptr->transmq)) {
1693 skb = tipc_buf_acquire(INT_H_SIZE); 1593 skb = tipc_buf_acquire(INT_H_SIZE);
1694 if (skb) { 1594 if (skb) {
1695 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE); 1595 skb_copy_to_linear_data(skb, &tunnel_hdr, INT_H_SIZE);
@@ -1705,7 +1605,7 @@ void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1705 split_bundles = (l_ptr->owner->active_links[0] != 1605 split_bundles = (l_ptr->owner->active_links[0] !=
1706 l_ptr->owner->active_links[1]); 1606 l_ptr->owner->active_links[1]);
1707 1607
1708 skb_queue_walk(&l_ptr->outqueue, skb) { 1608 skb_queue_walk(&l_ptr->transmq, skb) {
1709 struct tipc_msg *msg = buf_msg(skb); 1609 struct tipc_msg *msg = buf_msg(skb);
1710 1610
1711 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) { 1611 if ((msg_user(msg) == MSG_BUNDLER) && split_bundles) {
@@ -1736,157 +1636,105 @@ void tipc_link_failover_send_queue(struct tipc_link *l_ptr)
1736 * and sequence order is preserved per sender/receiver socket pair. 1636 * and sequence order is preserved per sender/receiver socket pair.
1737 * Owner node is locked. 1637 * Owner node is locked.
1738 */ 1638 */
1739void tipc_link_dup_queue_xmit(struct tipc_link *l_ptr, 1639void tipc_link_dup_queue_xmit(struct tipc_link *link,
1740 struct tipc_link *tunnel) 1640 struct tipc_link *tnl)
1741{ 1641{
1742 struct sk_buff *skb; 1642 struct sk_buff *skb;
1743 struct tipc_msg tunnel_hdr; 1643 struct tipc_msg tnl_hdr;
1744 1644 struct sk_buff_head *queue = &link->transmq;
1745 tipc_msg_init(link_own_addr(l_ptr), &tunnel_hdr, CHANGEOVER_PROTOCOL, 1645 int mcnt;
1746 DUPLICATE_MSG, INT_H_SIZE, l_ptr->addr); 1646
1747 msg_set_msgcnt(&tunnel_hdr, skb_queue_len(&l_ptr->outqueue)); 1647 tipc_msg_init(link_own_addr(link), &tnl_hdr, TUNNEL_PROTOCOL,
1748 msg_set_bearer_id(&tunnel_hdr, l_ptr->peer_bearer_id); 1648 SYNCH_MSG, INT_H_SIZE, link->addr);
1749 skb_queue_walk(&l_ptr->outqueue, skb) { 1649 mcnt = skb_queue_len(&link->transmq) + skb_queue_len(&link->backlogq);
1650 msg_set_msgcnt(&tnl_hdr, mcnt);
1651 msg_set_bearer_id(&tnl_hdr, link->peer_bearer_id);
1652
1653tunnel_queue:
1654 skb_queue_walk(queue, skb) {
1750 struct sk_buff *outskb; 1655 struct sk_buff *outskb;
1751 struct tipc_msg *msg = buf_msg(skb); 1656 struct tipc_msg *msg = buf_msg(skb);
1752 u32 length = msg_size(msg); 1657 u32 len = msg_size(msg);
1753 1658
1754 if (msg_user(msg) == MSG_BUNDLER) 1659 msg_set_ack(msg, mod(link->next_in_no - 1));
1755 msg_set_type(msg, CLOSED_MSG); 1660 msg_set_bcast_ack(msg, link->owner->bclink.last_in);
1756 msg_set_ack(msg, mod(l_ptr->next_in_no - 1)); /* Update */ 1661 msg_set_size(&tnl_hdr, len + INT_H_SIZE);
1757 msg_set_bcast_ack(msg, l_ptr->owner->bclink.last_in); 1662 outskb = tipc_buf_acquire(len + INT_H_SIZE);
1758 msg_set_size(&tunnel_hdr, length + INT_H_SIZE);
1759 outskb = tipc_buf_acquire(length + INT_H_SIZE);
1760 if (outskb == NULL) { 1663 if (outskb == NULL) {
1761 pr_warn("%sunable to send duplicate msg\n", 1664 pr_warn("%sunable to send duplicate msg\n",
1762 link_co_err); 1665 link_co_err);
1763 return; 1666 return;
1764 } 1667 }
1765 skb_copy_to_linear_data(outskb, &tunnel_hdr, INT_H_SIZE); 1668 skb_copy_to_linear_data(outskb, &tnl_hdr, INT_H_SIZE);
1766 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE, skb->data, 1669 skb_copy_to_linear_data_offset(outskb, INT_H_SIZE,
1767 length); 1670 skb->data, len);
1768 __tipc_link_xmit_skb(tunnel, outskb); 1671 __tipc_link_xmit_skb(tnl, outskb);
1769 if (!tipc_link_is_up(l_ptr)) 1672 if (!tipc_link_is_up(link))
1770 return; 1673 return;
1771 } 1674 }
1772} 1675 if (queue == &link->backlogq)
1773
1774/**
1775 * buf_extract - extracts embedded TIPC message from another message
1776 * @skb: encapsulating message buffer
1777 * @from_pos: offset to extract from
1778 *
1779 * Returns a new message buffer containing an embedded message. The
1780 * encapsulating buffer is left unchanged.
1781 */
1782static struct sk_buff *buf_extract(struct sk_buff *skb, u32 from_pos)
1783{
1784 struct tipc_msg *msg = (struct tipc_msg *)(skb->data + from_pos);
1785 u32 size = msg_size(msg);
1786 struct sk_buff *eb;
1787
1788 eb = tipc_buf_acquire(size);
1789 if (eb)
1790 skb_copy_to_linear_data(eb, msg, size);
1791 return eb;
1792}
1793
1794/* tipc_link_dup_rcv(): Receive a tunnelled DUPLICATE_MSG packet.
1795 * Owner node is locked.
1796 */
1797static void tipc_link_dup_rcv(struct tipc_link *l_ptr,
1798 struct sk_buff *t_buf)
1799{
1800 struct sk_buff *buf;
1801
1802 if (!tipc_link_is_up(l_ptr))
1803 return; 1676 return;
1804 1677 queue = &link->backlogq;
1805 buf = buf_extract(t_buf, INT_H_SIZE); 1678 goto tunnel_queue;
1806 if (buf == NULL) {
1807 pr_warn("%sfailed to extract inner dup pkt\n", link_co_err);
1808 return;
1809 }
1810
1811 /* Add buffer to deferred queue, if applicable: */
1812 link_handle_out_of_seq_msg(l_ptr, buf);
1813} 1679}
1814 1680
1815/* tipc_link_failover_rcv(): Receive a tunnelled ORIGINAL_MSG packet 1681/* tipc_link_failover_rcv(): Receive a tunnelled FAILOVER_MSG packet
1816 * Owner node is locked. 1682 * Owner node is locked.
1817 */ 1683 */
1818static struct sk_buff *tipc_link_failover_rcv(struct tipc_link *l_ptr, 1684static bool tipc_link_failover_rcv(struct tipc_link *link,
1819 struct sk_buff *t_buf) 1685 struct sk_buff **skb)
1820{ 1686{
1821 struct tipc_msg *t_msg = buf_msg(t_buf); 1687 struct tipc_msg *msg = buf_msg(*skb);
1822 struct sk_buff *buf = NULL; 1688 struct sk_buff *iskb = NULL;
1823 struct tipc_msg *msg; 1689 struct tipc_link *pl = NULL;
1824 1690 int bearer_id = msg_bearer_id(msg);
1825 if (tipc_link_is_up(l_ptr)) 1691 int pos = 0;
1826 tipc_link_reset(l_ptr);
1827
1828 /* First failover packet? */
1829 if (l_ptr->exp_msg_count == START_CHANGEOVER)
1830 l_ptr->exp_msg_count = msg_msgcnt(t_msg);
1831
1832 /* Should there be an inner packet? */
1833 if (l_ptr->exp_msg_count) {
1834 l_ptr->exp_msg_count--;
1835 buf = buf_extract(t_buf, INT_H_SIZE);
1836 if (buf == NULL) {
1837 pr_warn("%sno inner failover pkt\n", link_co_err);
1838 goto exit;
1839 }
1840 msg = buf_msg(buf);
1841 1692
1842 if (less(msg_seqno(msg), l_ptr->reset_checkpoint)) { 1693 if (msg_type(msg) != FAILOVER_MSG) {
1843 kfree_skb(buf); 1694 pr_warn("%sunknown tunnel pkt received\n", link_co_err);
1844 buf = NULL; 1695 goto exit;
1845 goto exit;
1846 }
1847 if (msg_user(msg) == MSG_FRAGMENTER) {
1848 l_ptr->stats.recv_fragments++;
1849 tipc_buf_append(&l_ptr->reasm_buf, &buf);
1850 }
1851 } 1696 }
1852exit: 1697 if (bearer_id >= MAX_BEARERS)
1853 if ((!l_ptr->exp_msg_count) && (l_ptr->flags & LINK_STOPPED)) 1698 goto exit;
1854 tipc_link_delete(l_ptr);
1855 return buf;
1856}
1857 1699
1858/* tipc_link_tunnel_rcv(): Receive a tunnelled packet, sent 1700 if (bearer_id == link->bearer_id)
1859 * via other link as result of a failover (ORIGINAL_MSG) or 1701 goto exit;
1860 * a new active link (DUPLICATE_MSG). Failover packets are
1861 * returned to the active link for delivery upwards.
1862 * Owner node is locked.
1863 */
1864static int tipc_link_tunnel_rcv(struct tipc_node *n_ptr,
1865 struct sk_buff **buf)
1866{
1867 struct sk_buff *t_buf = *buf;
1868 struct tipc_link *l_ptr;
1869 struct tipc_msg *t_msg = buf_msg(t_buf);
1870 u32 bearer_id = msg_bearer_id(t_msg);
1871 1702
1872 *buf = NULL; 1703 pl = link->owner->links[bearer_id];
1704 if (pl && tipc_link_is_up(pl))
1705 tipc_link_reset(pl);
1873 1706
1874 if (bearer_id >= MAX_BEARERS) 1707 if (link->failover_pkts == FIRST_FAILOVER)
1708 link->failover_pkts = msg_msgcnt(msg);
1709
1710 /* Should we expect an inner packet? */
1711 if (!link->failover_pkts)
1875 goto exit; 1712 goto exit;
1876 1713
1877 l_ptr = n_ptr->links[bearer_id]; 1714 if (!tipc_msg_extract(*skb, &iskb, &pos)) {
1878 if (!l_ptr) 1715 pr_warn("%sno inner failover pkt\n", link_co_err);
1716 *skb = NULL;
1879 goto exit; 1717 goto exit;
1718 }
1719 link->failover_pkts--;
1720 *skb = NULL;
1880 1721
1881 if (msg_type(t_msg) == DUPLICATE_MSG) 1722 /* Was this packet already delivered? */
1882 tipc_link_dup_rcv(l_ptr, t_buf); 1723 if (less(buf_seqno(iskb), link->failover_checkpt)) {
1883 else if (msg_type(t_msg) == ORIGINAL_MSG) 1724 kfree_skb(iskb);
1884 *buf = tipc_link_failover_rcv(l_ptr, t_buf); 1725 iskb = NULL;
1885 else 1726 goto exit;
1886 pr_warn("%sunknown tunnel pkt received\n", link_co_err); 1727 }
1728 if (msg_user(buf_msg(iskb)) == MSG_FRAGMENTER) {
1729 link->stats.recv_fragments++;
1730 tipc_buf_append(&link->failover_skb, &iskb);
1731 }
1887exit: 1732exit:
1888 kfree_skb(t_buf); 1733 if (!link->failover_pkts && pl)
1889 return *buf != NULL; 1734 pl->flags &= ~LINK_FAILINGOVER;
1735 kfree_skb(*skb);
1736 *skb = iskb;
1737 return *skb;
1890} 1738}
1891 1739
1892static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol) 1740static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
@@ -1901,23 +1749,16 @@ static void link_set_supervision_props(struct tipc_link *l_ptr, u32 tol)
1901 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4); 1749 l_ptr->abort_limit = tol / (jiffies_to_msecs(l_ptr->cont_intv) / 4);
1902} 1750}
1903 1751
1904void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window) 1752void tipc_link_set_queue_limits(struct tipc_link *l, u32 win)
1905{ 1753{
1906 /* Data messages from this node, inclusive FIRST_FRAGM */ 1754 int max_bulk = TIPC_MAX_PUBLICATIONS / (l->mtu / ITEM_SIZE);
1907 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE] = window; 1755
1908 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE] = (window / 3) * 4; 1756 l->window = win;
1909 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE] = (window / 3) * 5; 1757 l->backlog[TIPC_LOW_IMPORTANCE].limit = win / 2;
1910 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE] = (window / 3) * 6; 1758 l->backlog[TIPC_MEDIUM_IMPORTANCE].limit = win;
1911 /* Transiting data messages,inclusive FIRST_FRAGM */ 1759 l->backlog[TIPC_HIGH_IMPORTANCE].limit = win / 2 * 3;
1912 l_ptr->queue_limit[TIPC_LOW_IMPORTANCE + 4] = 300; 1760 l->backlog[TIPC_CRITICAL_IMPORTANCE].limit = win * 2;
1913 l_ptr->queue_limit[TIPC_MEDIUM_IMPORTANCE + 4] = 600; 1761 l->backlog[TIPC_SYSTEM_IMPORTANCE].limit = max_bulk;
1914 l_ptr->queue_limit[TIPC_HIGH_IMPORTANCE + 4] = 900;
1915 l_ptr->queue_limit[TIPC_CRITICAL_IMPORTANCE + 4] = 1200;
1916 l_ptr->queue_limit[CONN_MANAGER] = 1200;
1917 l_ptr->queue_limit[CHANGEOVER_PROTOCOL] = 2500;
1918 l_ptr->queue_limit[NAME_DISTRIBUTOR] = 3000;
1919 /* FRAGMENT and LAST_FRAGMENT packets */
1920 l_ptr->queue_limit[MSG_FRAGMENTER] = 4000;
1921} 1762}
1922 1763
1923/* tipc_link_find_owner - locate owner node of link by link's name 1764/* tipc_link_find_owner - locate owner node of link by link's name
@@ -2082,14 +1923,14 @@ int tipc_nl_link_set(struct sk_buff *skb, struct genl_info *info)
2082 1923
2083 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]); 1924 tol = nla_get_u32(props[TIPC_NLA_PROP_TOL]);
2084 link_set_supervision_props(link, tol); 1925 link_set_supervision_props(link, tol);
2085 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0, 0); 1926 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, tol, 0);
2086 } 1927 }
2087 if (props[TIPC_NLA_PROP_PRIO]) { 1928 if (props[TIPC_NLA_PROP_PRIO]) {
2088 u32 prio; 1929 u32 prio;
2089 1930
2090 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]); 1931 prio = nla_get_u32(props[TIPC_NLA_PROP_PRIO]);
2091 link->priority = prio; 1932 link->priority = prio;
2092 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio, 0); 1933 tipc_link_proto_xmit(link, STATE_MSG, 0, 0, 0, prio);
2093 } 1934 }
2094 if (props[TIPC_NLA_PROP_WIN]) { 1935 if (props[TIPC_NLA_PROP_WIN]) {
2095 u32 win; 1936 u32 win;
@@ -2194,7 +2035,7 @@ static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2194 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST, 2035 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_DEST,
2195 tipc_cluster_mask(tn->own_addr))) 2036 tipc_cluster_mask(tn->own_addr)))
2196 goto attr_msg_full; 2037 goto attr_msg_full;
2197 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->max_pkt)) 2038 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_MTU, link->mtu))
2198 goto attr_msg_full; 2039 goto attr_msg_full;
2199 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no)) 2040 if (nla_put_u32(msg->skb, TIPC_NLA_LINK_RX, link->next_in_no))
2200 goto attr_msg_full; 2041 goto attr_msg_full;
@@ -2216,7 +2057,7 @@ static int __tipc_nl_add_link(struct net *net, struct tipc_nl_msg *msg,
2216 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance)) 2057 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_TOL, link->tolerance))
2217 goto prop_msg_full; 2058 goto prop_msg_full;
2218 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN, 2059 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_WIN,
2219 link->queue_limit[TIPC_LOW_IMPORTANCE])) 2060 link->window))
2220 goto prop_msg_full; 2061 goto prop_msg_full;
2221 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority)) 2062 if (nla_put_u32(msg->skb, TIPC_NLA_PROP_PRIO, link->priority))
2222 goto prop_msg_full; 2063 goto prop_msg_full;
@@ -2282,7 +2123,6 @@ int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2282 msg.seq = cb->nlh->nlmsg_seq; 2123 msg.seq = cb->nlh->nlmsg_seq;
2283 2124
2284 rcu_read_lock(); 2125 rcu_read_lock();
2285
2286 if (prev_node) { 2126 if (prev_node) {
2287 node = tipc_node_find(net, prev_node); 2127 node = tipc_node_find(net, prev_node);
2288 if (!node) { 2128 if (!node) {
@@ -2295,6 +2135,7 @@ int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2295 cb->prev_seq = 1; 2135 cb->prev_seq = 1;
2296 goto out; 2136 goto out;
2297 } 2137 }
2138 tipc_node_put(node);
2298 2139
2299 list_for_each_entry_continue_rcu(node, &tn->node_list, 2140 list_for_each_entry_continue_rcu(node, &tn->node_list,
2300 list) { 2141 list) {
@@ -2302,6 +2143,7 @@ int tipc_nl_link_dump(struct sk_buff *skb, struct netlink_callback *cb)
2302 err = __tipc_nl_add_node_links(net, &msg, node, 2143 err = __tipc_nl_add_node_links(net, &msg, node,
2303 &prev_link); 2144 &prev_link);
2304 tipc_node_unlock(node); 2145 tipc_node_unlock(node);
2146 tipc_node_put(node);
2305 if (err) 2147 if (err)
2306 goto out; 2148 goto out;
2307 2149
diff --git a/net/tipc/link.h b/net/tipc/link.h
index 7aeb52092bf3..b5b4e3554d4e 100644
--- a/net/tipc/link.h
+++ b/net/tipc/link.h
@@ -58,8 +58,10 @@
58 58
59/* Link endpoint execution states 59/* Link endpoint execution states
60 */ 60 */
61#define LINK_STARTED 0x0001 61#define LINK_STARTED 0x0001
62#define LINK_STOPPED 0x0002 62#define LINK_STOPPED 0x0002
63#define LINK_SYNCHING 0x0004
64#define LINK_FAILINGOVER 0x0008
63 65
64/* Starting value for maximum packet size negotiation on unicast links 66/* Starting value for maximum packet size negotiation on unicast links
65 * (unless bearer MTU is less) 67 * (unless bearer MTU is less)
@@ -118,13 +120,13 @@ struct tipc_stats {
118 * @pmsg: convenience pointer to "proto_msg" field 120 * @pmsg: convenience pointer to "proto_msg" field
119 * @priority: current link priority 121 * @priority: current link priority
120 * @net_plane: current link network plane ('A' through 'H') 122 * @net_plane: current link network plane ('A' through 'H')
121 * @queue_limit: outbound message queue congestion thresholds (indexed by user) 123 * @backlog_limit: backlog queue congestion thresholds (indexed by importance)
122 * @exp_msg_count: # of tunnelled messages expected during link changeover 124 * @exp_msg_count: # of tunnelled messages expected during link changeover
123 * @reset_checkpoint: seq # of last acknowledged message at time of link reset 125 * @reset_checkpoint: seq # of last acknowledged message at time of link reset
124 * @max_pkt: current maximum packet size for this link 126 * @mtu: current maximum packet size for this link
125 * @max_pkt_target: desired maximum packet size for this link 127 * @advertised_mtu: advertised own mtu when link is being established
126 * @max_pkt_probes: # of probes based on current (max_pkt, max_pkt_target) 128 * @transmitq: queue for sent, non-acked messages
127 * @outqueue: outbound message queue 129 * @backlogq: queue for messages waiting to be sent
128 * @next_out_no: next sequence number to use for outbound messages 130 * @next_out_no: next sequence number to use for outbound messages
129 * @last_retransmitted: sequence number of most recently retransmitted message 131 * @last_retransmitted: sequence number of most recently retransmitted message
130 * @stale_count: # of identical retransmit requests made by peer 132 * @stale_count: # of identical retransmit requests made by peer
@@ -165,36 +167,40 @@ struct tipc_link {
165 struct tipc_msg *pmsg; 167 struct tipc_msg *pmsg;
166 u32 priority; 168 u32 priority;
167 char net_plane; 169 char net_plane;
168 u32 queue_limit[15]; /* queue_limit[0]==window limit */ 170 u16 synch_point;
169 171
170 /* Changeover */ 172 /* Failover */
171 u32 exp_msg_count; 173 u16 failover_pkts;
172 u32 reset_checkpoint; 174 u16 failover_checkpt;
175 struct sk_buff *failover_skb;
173 176
174 /* Max packet negotiation */ 177 /* Max packet negotiation */
175 u32 max_pkt; 178 u16 mtu;
176 u32 max_pkt_target; 179 u16 advertised_mtu;
177 u32 max_pkt_probes;
178 180
179 /* Sending */ 181 /* Sending */
180 struct sk_buff_head outqueue; 182 struct sk_buff_head transmq;
183 struct sk_buff_head backlogq;
184 struct {
185 u16 len;
186 u16 limit;
187 } backlog[5];
181 u32 next_out_no; 188 u32 next_out_no;
189 u32 window;
182 u32 last_retransmitted; 190 u32 last_retransmitted;
183 u32 stale_count; 191 u32 stale_count;
184 192
185 /* Reception */ 193 /* Reception */
186 u32 next_in_no; 194 u32 next_in_no;
187 struct sk_buff_head deferred_queue; 195 u32 rcv_unacked;
188 u32 unacked_window; 196 struct sk_buff_head deferdq;
189 struct sk_buff_head inputq; 197 struct sk_buff_head inputq;
190 struct sk_buff_head namedq; 198 struct sk_buff_head namedq;
191 199
192 /* Congestion handling */ 200 /* Congestion handling */
193 struct sk_buff *next_out;
194 struct sk_buff_head wakeupq; 201 struct sk_buff_head wakeupq;
195 202
196 /* Fragmentation/reassembly */ 203 /* Fragmentation/reassembly */
197 u32 long_msg_seq_no;
198 struct sk_buff *reasm_buf; 204 struct sk_buff *reasm_buf;
199 205
200 /* Statistics */ 206 /* Statistics */
@@ -225,7 +231,7 @@ int tipc_link_xmit(struct net *net, struct sk_buff_head *list, u32 dest,
225int __tipc_link_xmit(struct net *net, struct tipc_link *link, 231int __tipc_link_xmit(struct net *net, struct tipc_link *link,
226 struct sk_buff_head *list); 232 struct sk_buff_head *list);
227void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int prob, 233void tipc_link_proto_xmit(struct tipc_link *l_ptr, u32 msg_typ, int prob,
228 u32 gap, u32 tolerance, u32 priority, u32 acked_mtu); 234 u32 gap, u32 tolerance, u32 priority);
229void tipc_link_push_packets(struct tipc_link *l_ptr); 235void tipc_link_push_packets(struct tipc_link *l_ptr);
230u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *buf); 236u32 tipc_link_defer_pkt(struct sk_buff_head *list, struct sk_buff *buf);
231void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window); 237void tipc_link_set_queue_limits(struct tipc_link *l_ptr, u32 window);
@@ -302,9 +308,4 @@ static inline int link_reset_reset(struct tipc_link *l_ptr)
302 return l_ptr->state == RESET_RESET; 308 return l_ptr->state == RESET_RESET;
303} 309}
304 310
305static inline int link_congested(struct tipc_link *l_ptr)
306{
307 return skb_queue_len(&l_ptr->outqueue) >= l_ptr->queue_limit[0];
308}
309
310#endif 311#endif
diff --git a/net/tipc/msg.c b/net/tipc/msg.c
index b6eb90cd3ef7..c3e96e815418 100644
--- a/net/tipc/msg.c
+++ b/net/tipc/msg.c
@@ -1,7 +1,7 @@
1/* 1/*
2 * net/tipc/msg.c: TIPC message header routines 2 * net/tipc/msg.c: TIPC message header routines
3 * 3 *
4 * Copyright (c) 2000-2006, 2014, Ericsson AB 4 * Copyright (c) 2000-2006, 2014-2015, Ericsson AB
5 * Copyright (c) 2005, 2010-2011, Wind River Systems 5 * Copyright (c) 2005, 2010-2011, Wind River Systems
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
@@ -165,6 +165,9 @@ int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
165 } 165 }
166 166
167 if (fragid == LAST_FRAGMENT) { 167 if (fragid == LAST_FRAGMENT) {
168 TIPC_SKB_CB(head)->validated = false;
169 if (unlikely(!tipc_msg_validate(head)))
170 goto err;
168 *buf = head; 171 *buf = head;
169 TIPC_SKB_CB(head)->tail = NULL; 172 TIPC_SKB_CB(head)->tail = NULL;
170 *headbuf = NULL; 173 *headbuf = NULL;
@@ -172,7 +175,6 @@ int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf)
172 } 175 }
173 *buf = NULL; 176 *buf = NULL;
174 return 0; 177 return 0;
175
176err: 178err:
177 pr_warn_ratelimited("Unable to build fragment list\n"); 179 pr_warn_ratelimited("Unable to build fragment list\n");
178 kfree_skb(*buf); 180 kfree_skb(*buf);
@@ -181,6 +183,48 @@ err:
181 return 0; 183 return 0;
182} 184}
183 185
186/* tipc_msg_validate - validate basic format of received message
187 *
188 * This routine ensures a TIPC message has an acceptable header, and at least
189 * as much data as the header indicates it should. The routine also ensures
190 * that the entire message header is stored in the main fragment of the message
191 * buffer, to simplify future access to message header fields.
192 *
193 * Note: Having extra info present in the message header or data areas is OK.
194 * TIPC will ignore the excess, under the assumption that it is optional info
195 * introduced by a later release of the protocol.
196 */
197bool tipc_msg_validate(struct sk_buff *skb)
198{
199 struct tipc_msg *msg;
200 int msz, hsz;
201
202 if (unlikely(TIPC_SKB_CB(skb)->validated))
203 return true;
204 if (unlikely(!pskb_may_pull(skb, MIN_H_SIZE)))
205 return false;
206
207 hsz = msg_hdr_sz(buf_msg(skb));
208 if (unlikely(hsz < MIN_H_SIZE) || (hsz > MAX_H_SIZE))
209 return false;
210 if (unlikely(!pskb_may_pull(skb, hsz)))
211 return false;
212
213 msg = buf_msg(skb);
214 if (unlikely(msg_version(msg) != TIPC_VERSION))
215 return false;
216
217 msz = msg_size(msg);
218 if (unlikely(msz < hsz))
219 return false;
220 if (unlikely((msz - hsz) > TIPC_MAX_USER_MSG_SIZE))
221 return false;
222 if (unlikely(skb->len < msz))
223 return false;
224
225 TIPC_SKB_CB(skb)->validated = true;
226 return true;
227}
184 228
185/** 229/**
186 * tipc_msg_build - create buffer chain containing specified header and data 230 * tipc_msg_build - create buffer chain containing specified header and data
@@ -228,6 +272,7 @@ int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m,
228 FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr)); 272 FIRST_FRAGMENT, INT_H_SIZE, msg_destnode(mhdr));
229 msg_set_size(&pkthdr, pktmax); 273 msg_set_size(&pkthdr, pktmax);
230 msg_set_fragm_no(&pkthdr, pktno); 274 msg_set_fragm_no(&pkthdr, pktno);
275 msg_set_importance(&pkthdr, msg_importance(mhdr));
231 276
232 /* Prepare first fragment */ 277 /* Prepare first fragment */
233 skb = tipc_buf_acquire(pktmax); 278 skb = tipc_buf_acquire(pktmax);
@@ -286,33 +331,36 @@ error:
286 331
287/** 332/**
288 * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one 333 * tipc_msg_bundle(): Append contents of a buffer to tail of an existing one
289 * @list: the buffer chain of the existing buffer ("bundle") 334 * @bskb: the buffer to append to ("bundle")
290 * @skb: buffer to be appended 335 * @skb: buffer to be appended
291 * @mtu: max allowable size for the bundle buffer 336 * @mtu: max allowable size for the bundle buffer
292 * Consumes buffer if successful 337 * Consumes buffer if successful
293 * Returns true if bundling could be performed, otherwise false 338 * Returns true if bundling could be performed, otherwise false
294 */ 339 */
295bool tipc_msg_bundle(struct sk_buff_head *list, struct sk_buff *skb, u32 mtu) 340bool tipc_msg_bundle(struct sk_buff *bskb, struct sk_buff *skb, u32 mtu)
296{ 341{
297 struct sk_buff *bskb = skb_peek_tail(list); 342 struct tipc_msg *bmsg;
298 struct tipc_msg *bmsg = buf_msg(bskb);
299 struct tipc_msg *msg = buf_msg(skb); 343 struct tipc_msg *msg = buf_msg(skb);
300 unsigned int bsz = msg_size(bmsg); 344 unsigned int bsz;
301 unsigned int msz = msg_size(msg); 345 unsigned int msz = msg_size(msg);
302 u32 start = align(bsz); 346 u32 start, pad;
303 u32 max = mtu - INT_H_SIZE; 347 u32 max = mtu - INT_H_SIZE;
304 u32 pad = start - bsz;
305 348
306 if (likely(msg_user(msg) == MSG_FRAGMENTER)) 349 if (likely(msg_user(msg) == MSG_FRAGMENTER))
307 return false; 350 return false;
308 if (unlikely(msg_user(msg) == CHANGEOVER_PROTOCOL)) 351 if (!bskb)
352 return false;
353 bmsg = buf_msg(bskb);
354 bsz = msg_size(bmsg);
355 start = align(bsz);
356 pad = start - bsz;
357
358 if (unlikely(msg_user(msg) == TUNNEL_PROTOCOL))
309 return false; 359 return false;
310 if (unlikely(msg_user(msg) == BCAST_PROTOCOL)) 360 if (unlikely(msg_user(msg) == BCAST_PROTOCOL))
311 return false; 361 return false;
312 if (likely(msg_user(bmsg) != MSG_BUNDLER)) 362 if (likely(msg_user(bmsg) != MSG_BUNDLER))
313 return false; 363 return false;
314 if (likely(!TIPC_SKB_CB(bskb)->bundling))
315 return false;
316 if (unlikely(skb_tailroom(bskb) < (pad + msz))) 364 if (unlikely(skb_tailroom(bskb) < (pad + msz)))
317 return false; 365 return false;
318 if (unlikely(max < (start + msz))) 366 if (unlikely(max < (start + msz)))
@@ -328,34 +376,40 @@ bool tipc_msg_bundle(struct sk_buff_head *list, struct sk_buff *skb, u32 mtu)
328 376
329/** 377/**
330 * tipc_msg_extract(): extract bundled inner packet from buffer 378 * tipc_msg_extract(): extract bundled inner packet from buffer
331 * @skb: linear outer buffer, to be extracted from. 379 * @skb: buffer to be extracted from.
332 * @iskb: extracted inner buffer, to be returned 380 * @iskb: extracted inner buffer, to be returned
333 * @pos: position of msg to be extracted. Returns with pointer of next msg 381 * @pos: position in outer message of msg to be extracted.
382 * Returns position of next msg
334 * Consumes outer buffer when last packet extracted 383 * Consumes outer buffer when last packet extracted
335 * Returns true when when there is an extracted buffer, otherwise false 384 * Returns true when when there is an extracted buffer, otherwise false
336 */ 385 */
337bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos) 386bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos)
338{ 387{
339 struct tipc_msg *msg = buf_msg(skb); 388 struct tipc_msg *msg;
340 int imsz; 389 int imsz, offset;
341 struct tipc_msg *imsg = (struct tipc_msg *)(msg_data(msg) + *pos);
342 390
343 /* Is there space left for shortest possible message? */ 391 *iskb = NULL;
344 if (*pos > (msg_data_sz(msg) - SHORT_H_SIZE)) 392 if (unlikely(skb_linearize(skb)))
393 goto none;
394
395 msg = buf_msg(skb);
396 offset = msg_hdr_sz(msg) + *pos;
397 if (unlikely(offset > (msg_size(msg) - MIN_H_SIZE)))
345 goto none; 398 goto none;
346 imsz = msg_size(imsg);
347 399
348 /* Is there space left for current message ? */ 400 *iskb = skb_clone(skb, GFP_ATOMIC);
349 if ((*pos + imsz) > msg_data_sz(msg)) 401 if (unlikely(!*iskb))
350 goto none; 402 goto none;
351 *iskb = tipc_buf_acquire(imsz); 403 skb_pull(*iskb, offset);
352 if (!*iskb) 404 imsz = msg_size(buf_msg(*iskb));
405 skb_trim(*iskb, imsz);
406 if (unlikely(!tipc_msg_validate(*iskb)))
353 goto none; 407 goto none;
354 skb_copy_to_linear_data(*iskb, imsg, imsz);
355 *pos += align(imsz); 408 *pos += align(imsz);
356 return true; 409 return true;
357none: 410none:
358 kfree_skb(skb); 411 kfree_skb(skb);
412 kfree_skb(*iskb);
359 *iskb = NULL; 413 *iskb = NULL;
360 return false; 414 return false;
361} 415}
@@ -369,18 +423,17 @@ none:
369 * Replaces buffer if successful 423 * Replaces buffer if successful
370 * Returns true if success, otherwise false 424 * Returns true if success, otherwise false
371 */ 425 */
372bool tipc_msg_make_bundle(struct sk_buff_head *list, 426bool tipc_msg_make_bundle(struct sk_buff **skb, u32 mtu, u32 dnode)
373 struct sk_buff *skb, u32 mtu, u32 dnode)
374{ 427{
375 struct sk_buff *bskb; 428 struct sk_buff *bskb;
376 struct tipc_msg *bmsg; 429 struct tipc_msg *bmsg;
377 struct tipc_msg *msg = buf_msg(skb); 430 struct tipc_msg *msg = buf_msg(*skb);
378 u32 msz = msg_size(msg); 431 u32 msz = msg_size(msg);
379 u32 max = mtu - INT_H_SIZE; 432 u32 max = mtu - INT_H_SIZE;
380 433
381 if (msg_user(msg) == MSG_FRAGMENTER) 434 if (msg_user(msg) == MSG_FRAGMENTER)
382 return false; 435 return false;
383 if (msg_user(msg) == CHANGEOVER_PROTOCOL) 436 if (msg_user(msg) == TUNNEL_PROTOCOL)
384 return false; 437 return false;
385 if (msg_user(msg) == BCAST_PROTOCOL) 438 if (msg_user(msg) == BCAST_PROTOCOL)
386 return false; 439 return false;
@@ -398,9 +451,9 @@ bool tipc_msg_make_bundle(struct sk_buff_head *list,
398 msg_set_seqno(bmsg, msg_seqno(msg)); 451 msg_set_seqno(bmsg, msg_seqno(msg));
399 msg_set_ack(bmsg, msg_ack(msg)); 452 msg_set_ack(bmsg, msg_ack(msg));
400 msg_set_bcast_ack(bmsg, msg_bcast_ack(msg)); 453 msg_set_bcast_ack(bmsg, msg_bcast_ack(msg));
401 TIPC_SKB_CB(bskb)->bundling = true; 454 tipc_msg_bundle(bskb, *skb, mtu);
402 __skb_queue_tail(list, bskb); 455 *skb = bskb;
403 return tipc_msg_bundle(list, skb, mtu); 456 return true;
404} 457}
405 458
406/** 459/**
@@ -415,21 +468,17 @@ bool tipc_msg_reverse(u32 own_addr, struct sk_buff *buf, u32 *dnode,
415 int err) 468 int err)
416{ 469{
417 struct tipc_msg *msg = buf_msg(buf); 470 struct tipc_msg *msg = buf_msg(buf);
418 uint imp = msg_importance(msg);
419 struct tipc_msg ohdr; 471 struct tipc_msg ohdr;
420 uint rdsz = min_t(uint, msg_data_sz(msg), MAX_FORWARD_SIZE); 472 uint rdsz = min_t(uint, msg_data_sz(msg), MAX_FORWARD_SIZE);
421 473
422 if (skb_linearize(buf)) 474 if (skb_linearize(buf))
423 goto exit; 475 goto exit;
476 msg = buf_msg(buf);
424 if (msg_dest_droppable(msg)) 477 if (msg_dest_droppable(msg))
425 goto exit; 478 goto exit;
426 if (msg_errcode(msg)) 479 if (msg_errcode(msg))
427 goto exit; 480 goto exit;
428
429 memcpy(&ohdr, msg, msg_hdr_sz(msg)); 481 memcpy(&ohdr, msg, msg_hdr_sz(msg));
430 imp = min_t(uint, imp + 1, TIPC_CRITICAL_IMPORTANCE);
431 if (msg_isdata(msg))
432 msg_set_importance(msg, imp);
433 msg_set_errcode(msg, err); 482 msg_set_errcode(msg, err);
434 msg_set_origport(msg, msg_destport(&ohdr)); 483 msg_set_origport(msg, msg_destport(&ohdr));
435 msg_set_destport(msg, msg_origport(&ohdr)); 484 msg_set_destport(msg, msg_origport(&ohdr));
@@ -462,15 +511,18 @@ bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb,
462{ 511{
463 struct tipc_msg *msg = buf_msg(skb); 512 struct tipc_msg *msg = buf_msg(skb);
464 u32 dport; 513 u32 dport;
514 u32 own_addr = tipc_own_addr(net);
465 515
466 if (!msg_isdata(msg)) 516 if (!msg_isdata(msg))
467 return false; 517 return false;
468 if (!msg_named(msg)) 518 if (!msg_named(msg))
469 return false; 519 return false;
520 if (msg_errcode(msg))
521 return false;
470 *err = -TIPC_ERR_NO_NAME; 522 *err = -TIPC_ERR_NO_NAME;
471 if (skb_linearize(skb)) 523 if (skb_linearize(skb))
472 return false; 524 return false;
473 if (msg_reroute_cnt(msg) > 0) 525 if (msg_reroute_cnt(msg))
474 return false; 526 return false;
475 *dnode = addr_domain(net, msg_lookup_scope(msg)); 527 *dnode = addr_domain(net, msg_lookup_scope(msg));
476 dport = tipc_nametbl_translate(net, msg_nametype(msg), 528 dport = tipc_nametbl_translate(net, msg_nametype(msg),
@@ -478,6 +530,8 @@ bool tipc_msg_lookup_dest(struct net *net, struct sk_buff *skb,
478 if (!dport) 530 if (!dport)
479 return false; 531 return false;
480 msg_incr_reroute_cnt(msg); 532 msg_incr_reroute_cnt(msg);
533 if (*dnode != own_addr)
534 msg_set_prevnode(msg, own_addr);
481 msg_set_destnode(msg, *dnode); 535 msg_set_destnode(msg, *dnode);
482 msg_set_destport(msg, dport); 536 msg_set_destport(msg, dport);
483 *err = TIPC_OK; 537 *err = TIPC_OK;
diff --git a/net/tipc/msg.h b/net/tipc/msg.h
index 9ace47f44a69..e1d3595e2ee9 100644
--- a/net/tipc/msg.h
+++ b/net/tipc/msg.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * net/tipc/msg.h: Include file for TIPC message header routines 2 * net/tipc/msg.h: Include file for TIPC message header routines
3 * 3 *
4 * Copyright (c) 2000-2007, 2014, Ericsson AB 4 * Copyright (c) 2000-2007, 2014-2015 Ericsson AB
5 * Copyright (c) 2005-2008, 2010-2011, Wind River Systems 5 * Copyright (c) 2005-2008, 2010-2011, Wind River Systems
6 * All rights reserved. 6 * All rights reserved.
7 * 7 *
@@ -54,6 +54,8 @@ struct plist;
54 * - TIPC_HIGH_IMPORTANCE 54 * - TIPC_HIGH_IMPORTANCE
55 * - TIPC_CRITICAL_IMPORTANCE 55 * - TIPC_CRITICAL_IMPORTANCE
56 */ 56 */
57#define TIPC_SYSTEM_IMPORTANCE 4
58
57 59
58/* 60/*
59 * Payload message types 61 * Payload message types
@@ -64,6 +66,19 @@ struct plist;
64#define TIPC_DIRECT_MSG 3 66#define TIPC_DIRECT_MSG 3
65 67
66/* 68/*
69 * Internal message users
70 */
71#define BCAST_PROTOCOL 5
72#define MSG_BUNDLER 6
73#define LINK_PROTOCOL 7
74#define CONN_MANAGER 8
75#define TUNNEL_PROTOCOL 10
76#define NAME_DISTRIBUTOR 11
77#define MSG_FRAGMENTER 12
78#define LINK_CONFIG 13
79#define SOCK_WAKEUP 14 /* pseudo user */
80
81/*
67 * Message header sizes 82 * Message header sizes
68 */ 83 */
69#define SHORT_H_SIZE 24 /* In-cluster basic payload message */ 84#define SHORT_H_SIZE 24 /* In-cluster basic payload message */
@@ -76,7 +91,7 @@ struct plist;
76 91
77#define MAX_MSG_SIZE (MAX_H_SIZE + TIPC_MAX_USER_MSG_SIZE) 92#define MAX_MSG_SIZE (MAX_H_SIZE + TIPC_MAX_USER_MSG_SIZE)
78 93
79#define TIPC_MEDIA_ADDR_OFFSET 5 94#define TIPC_MEDIA_INFO_OFFSET 5
80 95
81/** 96/**
82 * TIPC message buffer code 97 * TIPC message buffer code
@@ -87,12 +102,12 @@ struct plist;
87 * Note: Headroom should be a multiple of 4 to ensure the TIPC header fields 102 * Note: Headroom should be a multiple of 4 to ensure the TIPC header fields
88 * are word aligned for quicker access 103 * are word aligned for quicker access
89 */ 104 */
90#define BUF_HEADROOM LL_MAX_HEADER 105#define BUF_HEADROOM (LL_MAX_HEADER + 48)
91 106
92struct tipc_skb_cb { 107struct tipc_skb_cb {
93 void *handle; 108 void *handle;
94 struct sk_buff *tail; 109 struct sk_buff *tail;
95 bool deferred; 110 bool validated;
96 bool wakeup_pending; 111 bool wakeup_pending;
97 bool bundling; 112 bool bundling;
98 u16 chain_sz; 113 u16 chain_sz;
@@ -170,16 +185,6 @@ static inline void msg_set_user(struct tipc_msg *m, u32 n)
170 msg_set_bits(m, 0, 25, 0xf, n); 185 msg_set_bits(m, 0, 25, 0xf, n);
171} 186}
172 187
173static inline u32 msg_importance(struct tipc_msg *m)
174{
175 return msg_bits(m, 0, 25, 0xf);
176}
177
178static inline void msg_set_importance(struct tipc_msg *m, u32 i)
179{
180 msg_set_user(m, i);
181}
182
183static inline u32 msg_hdr_sz(struct tipc_msg *m) 188static inline u32 msg_hdr_sz(struct tipc_msg *m)
184{ 189{
185 return msg_bits(m, 0, 21, 0xf) << 2; 190 return msg_bits(m, 0, 21, 0xf) << 2;
@@ -235,6 +240,15 @@ static inline void msg_set_size(struct tipc_msg *m, u32 sz)
235 m->hdr[0] = htonl((msg_word(m, 0) & ~0x1ffff) | sz); 240 m->hdr[0] = htonl((msg_word(m, 0) & ~0x1ffff) | sz);
236} 241}
237 242
243static inline unchar *msg_data(struct tipc_msg *m)
244{
245 return ((unchar *)m) + msg_hdr_sz(m);
246}
247
248static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
249{
250 return (struct tipc_msg *)msg_data(m);
251}
238 252
239/* 253/*
240 * Word 1 254 * Word 1
@@ -336,6 +350,25 @@ static inline void msg_set_seqno(struct tipc_msg *m, u32 n)
336/* 350/*
337 * Words 3-10 351 * Words 3-10
338 */ 352 */
353static inline u32 msg_importance(struct tipc_msg *m)
354{
355 if (unlikely(msg_user(m) == MSG_FRAGMENTER))
356 return msg_bits(m, 5, 13, 0x7);
357 if (likely(msg_isdata(m) && !msg_errcode(m)))
358 return msg_user(m);
359 return TIPC_SYSTEM_IMPORTANCE;
360}
361
362static inline void msg_set_importance(struct tipc_msg *m, u32 i)
363{
364 if (unlikely(msg_user(m) == MSG_FRAGMENTER))
365 msg_set_bits(m, 5, 13, 0x7, i);
366 else if (likely(i < TIPC_SYSTEM_IMPORTANCE))
367 msg_set_user(m, i);
368 else
369 pr_warn("Trying to set illegal importance in message\n");
370}
371
339static inline u32 msg_prevnode(struct tipc_msg *m) 372static inline u32 msg_prevnode(struct tipc_msg *m)
340{ 373{
341 return msg_word(m, 3); 374 return msg_word(m, 3);
@@ -348,6 +381,8 @@ static inline void msg_set_prevnode(struct tipc_msg *m, u32 a)
348 381
349static inline u32 msg_origport(struct tipc_msg *m) 382static inline u32 msg_origport(struct tipc_msg *m)
350{ 383{
384 if (msg_user(m) == MSG_FRAGMENTER)
385 m = msg_get_wrapped(m);
351 return msg_word(m, 4); 386 return msg_word(m, 4);
352} 387}
353 388
@@ -443,35 +478,11 @@ static inline void msg_set_nameupper(struct tipc_msg *m, u32 n)
443 msg_set_word(m, 10, n); 478 msg_set_word(m, 10, n);
444} 479}
445 480
446static inline unchar *msg_data(struct tipc_msg *m)
447{
448 return ((unchar *)m) + msg_hdr_sz(m);
449}
450
451static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
452{
453 return (struct tipc_msg *)msg_data(m);
454}
455
456/* 481/*
457 * Constants and routines used to read and write TIPC internal message headers 482 * Constants and routines used to read and write TIPC internal message headers
458 */ 483 */
459 484
460/* 485/*
461 * Internal message users
462 */
463#define BCAST_PROTOCOL 5
464#define MSG_BUNDLER 6
465#define LINK_PROTOCOL 7
466#define CONN_MANAGER 8
467#define ROUTE_DISTRIBUTOR 9 /* obsoleted */
468#define CHANGEOVER_PROTOCOL 10
469#define NAME_DISTRIBUTOR 11
470#define MSG_FRAGMENTER 12
471#define LINK_CONFIG 13
472#define SOCK_WAKEUP 14 /* pseudo user */
473
474/*
475 * Connection management protocol message types 486 * Connection management protocol message types
476 */ 487 */
477#define CONN_PROBE 0 488#define CONN_PROBE 0
@@ -501,8 +512,8 @@ static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
501/* 512/*
502 * Changeover tunnel message types 513 * Changeover tunnel message types
503 */ 514 */
504#define DUPLICATE_MSG 0 515#define SYNCH_MSG 0
505#define ORIGINAL_MSG 1 516#define FAILOVER_MSG 1
506 517
507/* 518/*
508 * Config protocol message types 519 * Config protocol message types
@@ -510,7 +521,6 @@ static inline struct tipc_msg *msg_get_wrapped(struct tipc_msg *m)
510#define DSC_REQ_MSG 0 521#define DSC_REQ_MSG 0
511#define DSC_RESP_MSG 1 522#define DSC_RESP_MSG 1
512 523
513
514/* 524/*
515 * Word 1 525 * Word 1
516 */ 526 */
@@ -534,6 +544,24 @@ static inline void msg_set_node_sig(struct tipc_msg *m, u32 n)
534 msg_set_bits(m, 1, 0, 0xffff, n); 544 msg_set_bits(m, 1, 0, 0xffff, n);
535} 545}
536 546
547static inline u32 msg_node_capabilities(struct tipc_msg *m)
548{
549 return msg_bits(m, 1, 15, 0x1fff);
550}
551
552static inline void msg_set_node_capabilities(struct tipc_msg *m, u32 n)
553{
554 msg_set_bits(m, 1, 15, 0x1fff, n);
555}
556
557static inline bool msg_dup(struct tipc_msg *m)
558{
559 if (likely(msg_user(m) != TUNNEL_PROTOCOL))
560 return false;
561 if (msg_type(m) != SYNCH_MSG)
562 return false;
563 return true;
564}
537 565
538/* 566/*
539 * Word 2 567 * Word 2
@@ -688,7 +716,7 @@ static inline void msg_set_redundant_link(struct tipc_msg *m, u32 r)
688 716
689static inline char *msg_media_addr(struct tipc_msg *m) 717static inline char *msg_media_addr(struct tipc_msg *m)
690{ 718{
691 return (char *)&m->hdr[TIPC_MEDIA_ADDR_OFFSET]; 719 return (char *)&m->hdr[TIPC_MEDIA_INFO_OFFSET];
692} 720}
693 721
694/* 722/*
@@ -734,21 +762,8 @@ static inline void msg_set_link_tolerance(struct tipc_msg *m, u32 n)
734 msg_set_bits(m, 9, 0, 0xffff, n); 762 msg_set_bits(m, 9, 0, 0xffff, n);
735} 763}
736 764
737static inline u32 tipc_msg_tot_importance(struct tipc_msg *m)
738{
739 if ((msg_user(m) == MSG_FRAGMENTER) && (msg_type(m) == FIRST_FRAGMENT))
740 return msg_importance(msg_get_wrapped(m));
741 return msg_importance(m);
742}
743
744static inline u32 msg_tot_origport(struct tipc_msg *m)
745{
746 if ((msg_user(m) == MSG_FRAGMENTER) && (msg_type(m) == FIRST_FRAGMENT))
747 return msg_origport(msg_get_wrapped(m));
748 return msg_origport(m);
749}
750
751struct sk_buff *tipc_buf_acquire(u32 size); 765struct sk_buff *tipc_buf_acquire(u32 size);
766bool tipc_msg_validate(struct sk_buff *skb);
752bool tipc_msg_reverse(u32 own_addr, struct sk_buff *buf, u32 *dnode, 767bool tipc_msg_reverse(u32 own_addr, struct sk_buff *buf, u32 *dnode,
753 int err); 768 int err);
754void tipc_msg_init(u32 own_addr, struct tipc_msg *m, u32 user, u32 type, 769void tipc_msg_init(u32 own_addr, struct tipc_msg *m, u32 user, u32 type,
@@ -757,9 +772,9 @@ struct sk_buff *tipc_msg_create(uint user, uint type, uint hdr_sz,
757 uint data_sz, u32 dnode, u32 onode, 772 uint data_sz, u32 dnode, u32 onode,
758 u32 dport, u32 oport, int errcode); 773 u32 dport, u32 oport, int errcode);
759int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf); 774int tipc_buf_append(struct sk_buff **headbuf, struct sk_buff **buf);
760bool tipc_msg_bundle(struct sk_buff_head *list, struct sk_buff *skb, u32 mtu); 775bool tipc_msg_bundle(struct sk_buff *bskb, struct sk_buff *skb, u32 mtu);
761bool tipc_msg_make_bundle(struct sk_buff_head *list, 776
762 struct sk_buff *skb, u32 mtu, u32 dnode); 777bool tipc_msg_make_bundle(struct sk_buff **skb, u32 mtu, u32 dnode);
763bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos); 778bool tipc_msg_extract(struct sk_buff *skb, struct sk_buff **iskb, int *pos);
764int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m, 779int tipc_msg_build(struct tipc_msg *mhdr, struct msghdr *m,
765 int offset, int dsz, int mtu, struct sk_buff_head *list); 780 int offset, int dsz, int mtu, struct sk_buff_head *list);
diff --git a/net/tipc/name_distr.c b/net/tipc/name_distr.c
index fcb07915aaac..41e7b7e4dda0 100644
--- a/net/tipc/name_distr.c
+++ b/net/tipc/name_distr.c
@@ -98,7 +98,7 @@ void named_cluster_distribute(struct net *net, struct sk_buff *skb)
98 continue; 98 continue;
99 if (!tipc_node_active_links(node)) 99 if (!tipc_node_active_links(node))
100 continue; 100 continue;
101 oskb = skb_copy(skb, GFP_ATOMIC); 101 oskb = pskb_copy(skb, GFP_ATOMIC);
102 if (!oskb) 102 if (!oskb)
103 break; 103 break;
104 msg_set_destnode(buf_msg(oskb), dnode); 104 msg_set_destnode(buf_msg(oskb), dnode);
@@ -244,6 +244,7 @@ static void tipc_publ_subscribe(struct net *net, struct publication *publ,
244 tipc_node_lock(node); 244 tipc_node_lock(node);
245 list_add_tail(&publ->nodesub_list, &node->publ_list); 245 list_add_tail(&publ->nodesub_list, &node->publ_list);
246 tipc_node_unlock(node); 246 tipc_node_unlock(node);
247 tipc_node_put(node);
247} 248}
248 249
249static void tipc_publ_unsubscribe(struct net *net, struct publication *publ, 250static void tipc_publ_unsubscribe(struct net *net, struct publication *publ,
@@ -258,6 +259,7 @@ static void tipc_publ_unsubscribe(struct net *net, struct publication *publ,
258 tipc_node_lock(node); 259 tipc_node_lock(node);
259 list_del_init(&publ->nodesub_list); 260 list_del_init(&publ->nodesub_list);
260 tipc_node_unlock(node); 261 tipc_node_unlock(node);
262 tipc_node_put(node);
261} 263}
262 264
263/** 265/**
diff --git a/net/tipc/name_table.c b/net/tipc/name_table.c
index 105ba7adf06f..ab0ac62a1287 100644
--- a/net/tipc/name_table.c
+++ b/net/tipc/name_table.c
@@ -811,8 +811,8 @@ static void tipc_purge_publications(struct net *net, struct name_seq *seq)
811 sseq = seq->sseqs; 811 sseq = seq->sseqs;
812 info = sseq->info; 812 info = sseq->info;
813 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) { 813 list_for_each_entry_safe(publ, safe, &info->zone_list, zone_list) {
814 tipc_nametbl_remove_publ(net, publ->type, publ->lower, 814 tipc_nameseq_remove_publ(net, seq, publ->lower, publ->node,
815 publ->node, publ->ref, publ->key); 815 publ->ref, publ->key);
816 kfree_rcu(publ, rcu); 816 kfree_rcu(publ, rcu);
817 } 817 }
818 hlist_del_init_rcu(&seq->ns_list); 818 hlist_del_init_rcu(&seq->ns_list);
diff --git a/net/tipc/node.c b/net/tipc/node.c
index 86152de8248d..22c059ad2999 100644
--- a/net/tipc/node.c
+++ b/net/tipc/node.c
@@ -42,6 +42,7 @@
42 42
43static void node_lost_contact(struct tipc_node *n_ptr); 43static void node_lost_contact(struct tipc_node *n_ptr);
44static void node_established_contact(struct tipc_node *n_ptr); 44static void node_established_contact(struct tipc_node *n_ptr);
45static void tipc_node_delete(struct tipc_node *node);
45 46
46struct tipc_sock_conn { 47struct tipc_sock_conn {
47 u32 port; 48 u32 port;
@@ -67,6 +68,23 @@ static unsigned int tipc_hashfn(u32 addr)
67 return addr & (NODE_HTABLE_SIZE - 1); 68 return addr & (NODE_HTABLE_SIZE - 1);
68} 69}
69 70
71static void tipc_node_kref_release(struct kref *kref)
72{
73 struct tipc_node *node = container_of(kref, struct tipc_node, kref);
74
75 tipc_node_delete(node);
76}
77
78void tipc_node_put(struct tipc_node *node)
79{
80 kref_put(&node->kref, tipc_node_kref_release);
81}
82
83static void tipc_node_get(struct tipc_node *node)
84{
85 kref_get(&node->kref);
86}
87
70/* 88/*
71 * tipc_node_find - locate specified node object, if it exists 89 * tipc_node_find - locate specified node object, if it exists
72 */ 90 */
@@ -82,6 +100,7 @@ struct tipc_node *tipc_node_find(struct net *net, u32 addr)
82 hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)], 100 hlist_for_each_entry_rcu(node, &tn->node_htable[tipc_hashfn(addr)],
83 hash) { 101 hash) {
84 if (node->addr == addr) { 102 if (node->addr == addr) {
103 tipc_node_get(node);
85 rcu_read_unlock(); 104 rcu_read_unlock();
86 return node; 105 return node;
87 } 106 }
@@ -106,12 +125,13 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr)
106 } 125 }
107 n_ptr->addr = addr; 126 n_ptr->addr = addr;
108 n_ptr->net = net; 127 n_ptr->net = net;
128 kref_init(&n_ptr->kref);
109 spin_lock_init(&n_ptr->lock); 129 spin_lock_init(&n_ptr->lock);
110 INIT_HLIST_NODE(&n_ptr->hash); 130 INIT_HLIST_NODE(&n_ptr->hash);
111 INIT_LIST_HEAD(&n_ptr->list); 131 INIT_LIST_HEAD(&n_ptr->list);
112 INIT_LIST_HEAD(&n_ptr->publ_list); 132 INIT_LIST_HEAD(&n_ptr->publ_list);
113 INIT_LIST_HEAD(&n_ptr->conn_sks); 133 INIT_LIST_HEAD(&n_ptr->conn_sks);
114 __skb_queue_head_init(&n_ptr->bclink.deferred_queue); 134 __skb_queue_head_init(&n_ptr->bclink.deferdq);
115 hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]); 135 hlist_add_head_rcu(&n_ptr->hash, &tn->node_htable[tipc_hashfn(addr)]);
116 list_for_each_entry_rcu(temp_node, &tn->node_list, list) { 136 list_for_each_entry_rcu(temp_node, &tn->node_list, list) {
117 if (n_ptr->addr < temp_node->addr) 137 if (n_ptr->addr < temp_node->addr)
@@ -120,16 +140,17 @@ struct tipc_node *tipc_node_create(struct net *net, u32 addr)
120 list_add_tail_rcu(&n_ptr->list, &temp_node->list); 140 list_add_tail_rcu(&n_ptr->list, &temp_node->list);
121 n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN; 141 n_ptr->action_flags = TIPC_WAIT_PEER_LINKS_DOWN;
122 n_ptr->signature = INVALID_NODE_SIG; 142 n_ptr->signature = INVALID_NODE_SIG;
143 tipc_node_get(n_ptr);
123exit: 144exit:
124 spin_unlock_bh(&tn->node_list_lock); 145 spin_unlock_bh(&tn->node_list_lock);
125 return n_ptr; 146 return n_ptr;
126} 147}
127 148
128static void tipc_node_delete(struct tipc_net *tn, struct tipc_node *n_ptr) 149static void tipc_node_delete(struct tipc_node *node)
129{ 150{
130 list_del_rcu(&n_ptr->list); 151 list_del_rcu(&node->list);
131 hlist_del_rcu(&n_ptr->hash); 152 hlist_del_rcu(&node->hash);
132 kfree_rcu(n_ptr, rcu); 153 kfree_rcu(node, rcu);
133} 154}
134 155
135void tipc_node_stop(struct net *net) 156void tipc_node_stop(struct net *net)
@@ -139,7 +160,7 @@ void tipc_node_stop(struct net *net)
139 160
140 spin_lock_bh(&tn->node_list_lock); 161 spin_lock_bh(&tn->node_list_lock);
141 list_for_each_entry_safe(node, t_node, &tn->node_list, list) 162 list_for_each_entry_safe(node, t_node, &tn->node_list, list)
142 tipc_node_delete(tn, node); 163 tipc_node_put(node);
143 spin_unlock_bh(&tn->node_list_lock); 164 spin_unlock_bh(&tn->node_list_lock);
144} 165}
145 166
@@ -147,6 +168,7 @@ int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
147{ 168{
148 struct tipc_node *node; 169 struct tipc_node *node;
149 struct tipc_sock_conn *conn; 170 struct tipc_sock_conn *conn;
171 int err = 0;
150 172
151 if (in_own_node(net, dnode)) 173 if (in_own_node(net, dnode))
152 return 0; 174 return 0;
@@ -157,8 +179,10 @@ int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
157 return -EHOSTUNREACH; 179 return -EHOSTUNREACH;
158 } 180 }
159 conn = kmalloc(sizeof(*conn), GFP_ATOMIC); 181 conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
160 if (!conn) 182 if (!conn) {
161 return -EHOSTUNREACH; 183 err = -EHOSTUNREACH;
184 goto exit;
185 }
162 conn->peer_node = dnode; 186 conn->peer_node = dnode;
163 conn->port = port; 187 conn->port = port;
164 conn->peer_port = peer_port; 188 conn->peer_port = peer_port;
@@ -166,7 +190,9 @@ int tipc_node_add_conn(struct net *net, u32 dnode, u32 port, u32 peer_port)
166 tipc_node_lock(node); 190 tipc_node_lock(node);
167 list_add_tail(&conn->list, &node->conn_sks); 191 list_add_tail(&conn->list, &node->conn_sks);
168 tipc_node_unlock(node); 192 tipc_node_unlock(node);
169 return 0; 193exit:
194 tipc_node_put(node);
195 return err;
170} 196}
171 197
172void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port) 198void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
@@ -189,6 +215,7 @@ void tipc_node_remove_conn(struct net *net, u32 dnode, u32 port)
189 kfree(conn); 215 kfree(conn);
190 } 216 }
191 tipc_node_unlock(node); 217 tipc_node_unlock(node);
218 tipc_node_put(node);
192} 219}
193 220
194/** 221/**
@@ -227,8 +254,8 @@ void tipc_node_link_up(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
227 active[0] = active[1] = l_ptr; 254 active[0] = active[1] = l_ptr;
228exit: 255exit:
229 /* Leave room for changeover header when returning 'mtu' to users: */ 256 /* Leave room for changeover header when returning 'mtu' to users: */
230 n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; 257 n_ptr->act_mtus[0] = active[0]->mtu - INT_H_SIZE;
231 n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; 258 n_ptr->act_mtus[1] = active[1]->mtu - INT_H_SIZE;
232} 259}
233 260
234/** 261/**
@@ -292,11 +319,10 @@ void tipc_node_link_down(struct tipc_node *n_ptr, struct tipc_link *l_ptr)
292 319
293 /* Leave room for changeover header when returning 'mtu' to users: */ 320 /* Leave room for changeover header when returning 'mtu' to users: */
294 if (active[0]) { 321 if (active[0]) {
295 n_ptr->act_mtus[0] = active[0]->max_pkt - INT_H_SIZE; 322 n_ptr->act_mtus[0] = active[0]->mtu - INT_H_SIZE;
296 n_ptr->act_mtus[1] = active[1]->max_pkt - INT_H_SIZE; 323 n_ptr->act_mtus[1] = active[1]->mtu - INT_H_SIZE;
297 return; 324 return;
298 } 325 }
299
300 /* Loopback link went down? No fragmentation needed from now on. */ 326 /* Loopback link went down? No fragmentation needed from now on. */
301 if (n_ptr->addr == tn->own_addr) { 327 if (n_ptr->addr == tn->own_addr) {
302 n_ptr->act_mtus[0] = MAX_MSG_SIZE; 328 n_ptr->act_mtus[0] = MAX_MSG_SIZE;
@@ -354,7 +380,7 @@ static void node_lost_contact(struct tipc_node *n_ptr)
354 380
355 /* Flush broadcast link info associated with lost node */ 381 /* Flush broadcast link info associated with lost node */
356 if (n_ptr->bclink.recv_permitted) { 382 if (n_ptr->bclink.recv_permitted) {
357 __skb_queue_purge(&n_ptr->bclink.deferred_queue); 383 __skb_queue_purge(&n_ptr->bclink.deferdq);
358 384
359 if (n_ptr->bclink.reasm_buf) { 385 if (n_ptr->bclink.reasm_buf) {
360 kfree_skb(n_ptr->bclink.reasm_buf); 386 kfree_skb(n_ptr->bclink.reasm_buf);
@@ -367,18 +393,17 @@ static void node_lost_contact(struct tipc_node *n_ptr)
367 n_ptr->bclink.recv_permitted = false; 393 n_ptr->bclink.recv_permitted = false;
368 } 394 }
369 395
370 /* Abort link changeover */ 396 /* Abort any ongoing link failover */
371 for (i = 0; i < MAX_BEARERS; i++) { 397 for (i = 0; i < MAX_BEARERS; i++) {
372 struct tipc_link *l_ptr = n_ptr->links[i]; 398 struct tipc_link *l_ptr = n_ptr->links[i];
373 if (!l_ptr) 399 if (!l_ptr)
374 continue; 400 continue;
375 l_ptr->reset_checkpoint = l_ptr->next_in_no; 401 l_ptr->flags &= ~LINK_FAILINGOVER;
376 l_ptr->exp_msg_count = 0; 402 l_ptr->failover_checkpt = 0;
403 l_ptr->failover_pkts = 0;
404 kfree_skb(l_ptr->failover_skb);
405 l_ptr->failover_skb = NULL;
377 tipc_link_reset_fragments(l_ptr); 406 tipc_link_reset_fragments(l_ptr);
378
379 /* Link marked for deletion after failover? => do it now */
380 if (l_ptr->flags & LINK_STOPPED)
381 tipc_link_delete(l_ptr);
382 } 407 }
383 408
384 n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN; 409 n_ptr->action_flags &= ~TIPC_WAIT_OWN_LINKS_DOWN;
@@ -417,19 +442,25 @@ int tipc_node_get_linkname(struct net *net, u32 bearer_id, u32 addr,
417 char *linkname, size_t len) 442 char *linkname, size_t len)
418{ 443{
419 struct tipc_link *link; 444 struct tipc_link *link;
445 int err = -EINVAL;
420 struct tipc_node *node = tipc_node_find(net, addr); 446 struct tipc_node *node = tipc_node_find(net, addr);
421 447
422 if ((bearer_id >= MAX_BEARERS) || !node) 448 if (!node)
423 return -EINVAL; 449 return err;
450
451 if (bearer_id >= MAX_BEARERS)
452 goto exit;
453
424 tipc_node_lock(node); 454 tipc_node_lock(node);
425 link = node->links[bearer_id]; 455 link = node->links[bearer_id];
426 if (link) { 456 if (link) {
427 strncpy(linkname, link->name, len); 457 strncpy(linkname, link->name, len);
428 tipc_node_unlock(node); 458 err = 0;
429 return 0;
430 } 459 }
460exit:
431 tipc_node_unlock(node); 461 tipc_node_unlock(node);
432 return -EINVAL; 462 tipc_node_put(node);
463 return err;
433} 464}
434 465
435void tipc_node_unlock(struct tipc_node *node) 466void tipc_node_unlock(struct tipc_node *node)
@@ -459,7 +490,7 @@ void tipc_node_unlock(struct tipc_node *node)
459 TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP | 490 TIPC_NOTIFY_NODE_DOWN | TIPC_NOTIFY_NODE_UP |
460 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP | 491 TIPC_NOTIFY_LINK_DOWN | TIPC_NOTIFY_LINK_UP |
461 TIPC_WAKEUP_BCAST_USERS | TIPC_BCAST_MSG_EVT | 492 TIPC_WAKEUP_BCAST_USERS | TIPC_BCAST_MSG_EVT |
462 TIPC_NAMED_MSG_EVT); 493 TIPC_NAMED_MSG_EVT | TIPC_BCAST_RESET);
463 494
464 spin_unlock_bh(&node->lock); 495 spin_unlock_bh(&node->lock);
465 496
@@ -488,6 +519,9 @@ void tipc_node_unlock(struct tipc_node *node)
488 519
489 if (flags & TIPC_BCAST_MSG_EVT) 520 if (flags & TIPC_BCAST_MSG_EVT)
490 tipc_bclink_input(net); 521 tipc_bclink_input(net);
522
523 if (flags & TIPC_BCAST_RESET)
524 tipc_link_reset_all(node);
491} 525}
492 526
493/* Caller should hold node lock for the passed node */ 527/* Caller should hold node lock for the passed node */
@@ -542,17 +576,21 @@ int tipc_nl_node_dump(struct sk_buff *skb, struct netlink_callback *cb)
542 msg.seq = cb->nlh->nlmsg_seq; 576 msg.seq = cb->nlh->nlmsg_seq;
543 577
544 rcu_read_lock(); 578 rcu_read_lock();
545 579 if (last_addr) {
546 if (last_addr && !tipc_node_find(net, last_addr)) { 580 node = tipc_node_find(net, last_addr);
547 rcu_read_unlock(); 581 if (!node) {
548 /* We never set seq or call nl_dump_check_consistent() this 582 rcu_read_unlock();
549 * means that setting prev_seq here will cause the consistence 583 /* We never set seq or call nl_dump_check_consistent()
550 * check to fail in the netlink callback handler. Resulting in 584 * this means that setting prev_seq here will cause the
551 * the NLMSG_DONE message having the NLM_F_DUMP_INTR flag set if 585 * consistence check to fail in the netlink callback
552 * the node state changed while we released the lock. 586 * handler. Resulting in the NLMSG_DONE message having
553 */ 587 * the NLM_F_DUMP_INTR flag set if the node state
554 cb->prev_seq = 1; 588 * changed while we released the lock.
555 return -EPIPE; 589 */
590 cb->prev_seq = 1;
591 return -EPIPE;
592 }
593 tipc_node_put(node);
556 } 594 }
557 595
558 list_for_each_entry_rcu(node, &tn->node_list, list) { 596 list_for_each_entry_rcu(node, &tn->node_list, list) {
diff --git a/net/tipc/node.h b/net/tipc/node.h
index 3d18c66b7f78..02d5c20dc551 100644
--- a/net/tipc/node.h
+++ b/net/tipc/node.h
@@ -64,7 +64,8 @@ enum {
64 TIPC_NOTIFY_LINK_UP = (1 << 6), 64 TIPC_NOTIFY_LINK_UP = (1 << 6),
65 TIPC_NOTIFY_LINK_DOWN = (1 << 7), 65 TIPC_NOTIFY_LINK_DOWN = (1 << 7),
66 TIPC_NAMED_MSG_EVT = (1 << 8), 66 TIPC_NAMED_MSG_EVT = (1 << 8),
67 TIPC_BCAST_MSG_EVT = (1 << 9) 67 TIPC_BCAST_MSG_EVT = (1 << 9),
68 TIPC_BCAST_RESET = (1 << 10)
68}; 69};
69 70
70/** 71/**
@@ -84,7 +85,7 @@ struct tipc_node_bclink {
84 u32 last_sent; 85 u32 last_sent;
85 u32 oos_state; 86 u32 oos_state;
86 u32 deferred_size; 87 u32 deferred_size;
87 struct sk_buff_head deferred_queue; 88 struct sk_buff_head deferdq;
88 struct sk_buff *reasm_buf; 89 struct sk_buff *reasm_buf;
89 int inputq_map; 90 int inputq_map;
90 bool recv_permitted; 91 bool recv_permitted;
@@ -93,6 +94,7 @@ struct tipc_node_bclink {
93/** 94/**
94 * struct tipc_node - TIPC node structure 95 * struct tipc_node - TIPC node structure
95 * @addr: network address of node 96 * @addr: network address of node
97 * @ref: reference counter to node object
96 * @lock: spinlock governing access to structure 98 * @lock: spinlock governing access to structure
97 * @net: the applicable net namespace 99 * @net: the applicable net namespace
98 * @hash: links to adjacent nodes in unsorted hash chain 100 * @hash: links to adjacent nodes in unsorted hash chain
@@ -106,6 +108,7 @@ struct tipc_node_bclink {
106 * @list: links to adjacent nodes in sorted list of cluster's nodes 108 * @list: links to adjacent nodes in sorted list of cluster's nodes
107 * @working_links: number of working links to node (both active and standby) 109 * @working_links: number of working links to node (both active and standby)
108 * @link_cnt: number of links to node 110 * @link_cnt: number of links to node
111 * @capabilities: bitmap, indicating peer node's functional capabilities
109 * @signature: node instance identifier 112 * @signature: node instance identifier
110 * @link_id: local and remote bearer ids of changing link, if any 113 * @link_id: local and remote bearer ids of changing link, if any
111 * @publ_list: list of publications 114 * @publ_list: list of publications
@@ -113,6 +116,7 @@ struct tipc_node_bclink {
113 */ 116 */
114struct tipc_node { 117struct tipc_node {
115 u32 addr; 118 u32 addr;
119 struct kref kref;
116 spinlock_t lock; 120 spinlock_t lock;
117 struct net *net; 121 struct net *net;
118 struct hlist_node hash; 122 struct hlist_node hash;
@@ -125,7 +129,8 @@ struct tipc_node {
125 struct tipc_node_bclink bclink; 129 struct tipc_node_bclink bclink;
126 struct list_head list; 130 struct list_head list;
127 int link_cnt; 131 int link_cnt;
128 int working_links; 132 u16 working_links;
133 u16 capabilities;
129 u32 signature; 134 u32 signature;
130 u32 link_id; 135 u32 link_id;
131 struct list_head publ_list; 136 struct list_head publ_list;
@@ -134,6 +139,7 @@ struct tipc_node {
134}; 139};
135 140
136struct tipc_node *tipc_node_find(struct net *net, u32 addr); 141struct tipc_node *tipc_node_find(struct net *net, u32 addr);
142void tipc_node_put(struct tipc_node *node);
137struct tipc_node *tipc_node_create(struct net *net, u32 addr); 143struct tipc_node *tipc_node_create(struct net *net, u32 addr);
138void tipc_node_stop(struct net *net); 144void tipc_node_stop(struct net *net);
139void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr); 145void tipc_node_attach_link(struct tipc_node *n_ptr, struct tipc_link *l_ptr);
@@ -168,10 +174,12 @@ static inline uint tipc_node_get_mtu(struct net *net, u32 addr, u32 selector)
168 174
169 node = tipc_node_find(net, addr); 175 node = tipc_node_find(net, addr);
170 176
171 if (likely(node)) 177 if (likely(node)) {
172 mtu = node->act_mtus[selector & 1]; 178 mtu = node->act_mtus[selector & 1];
173 else 179 tipc_node_put(node);
180 } else {
174 mtu = MAX_MSG_SIZE; 181 mtu = MAX_MSG_SIZE;
182 }
175 183
176 return mtu; 184 return mtu;
177} 185}
diff --git a/net/tipc/server.c b/net/tipc/server.c
index eadd4ed45905..ab6183cdb121 100644
--- a/net/tipc/server.c
+++ b/net/tipc/server.c
@@ -37,11 +37,13 @@
37#include "core.h" 37#include "core.h"
38#include "socket.h" 38#include "socket.h"
39#include <net/sock.h> 39#include <net/sock.h>
40#include <linux/module.h>
40 41
41/* Number of messages to send before rescheduling */ 42/* Number of messages to send before rescheduling */
42#define MAX_SEND_MSG_COUNT 25 43#define MAX_SEND_MSG_COUNT 25
43#define MAX_RECV_MSG_COUNT 25 44#define MAX_RECV_MSG_COUNT 25
44#define CF_CONNECTED 1 45#define CF_CONNECTED 1
46#define CF_SERVER 2
45 47
46#define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data) 48#define sock2con(x) ((struct tipc_conn *)(x)->sk_user_data)
47 49
@@ -88,9 +90,19 @@ static void tipc_clean_outqueues(struct tipc_conn *con);
88static void tipc_conn_kref_release(struct kref *kref) 90static void tipc_conn_kref_release(struct kref *kref)
89{ 91{
90 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref); 92 struct tipc_conn *con = container_of(kref, struct tipc_conn, kref);
93 struct sockaddr_tipc *saddr = con->server->saddr;
94 struct socket *sock = con->sock;
95 struct sock *sk;
91 96
92 if (con->sock) { 97 if (sock) {
93 tipc_sock_release_local(con->sock); 98 sk = sock->sk;
99 if (test_bit(CF_SERVER, &con->flags)) {
100 __module_get(sock->ops->owner);
101 __module_get(sk->sk_prot_creator->owner);
102 }
103 saddr->scope = -TIPC_NODE_SCOPE;
104 kernel_bind(sock, (struct sockaddr *)saddr, sizeof(*saddr));
105 sk_release_kernel(sk);
94 con->sock = NULL; 106 con->sock = NULL;
95 } 107 }
96 108
@@ -281,7 +293,7 @@ static int tipc_accept_from_sock(struct tipc_conn *con)
281 struct tipc_conn *newcon; 293 struct tipc_conn *newcon;
282 int ret; 294 int ret;
283 295
284 ret = tipc_sock_accept_local(sock, &newsock, O_NONBLOCK); 296 ret = kernel_accept(sock, &newsock, O_NONBLOCK);
285 if (ret < 0) 297 if (ret < 0)
286 return ret; 298 return ret;
287 299
@@ -309,9 +321,12 @@ static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
309 struct socket *sock = NULL; 321 struct socket *sock = NULL;
310 int ret; 322 int ret;
311 323
312 ret = tipc_sock_create_local(s->net, s->type, &sock); 324 ret = sock_create_kern(AF_TIPC, SOCK_SEQPACKET, 0, &sock);
313 if (ret < 0) 325 if (ret < 0)
314 return NULL; 326 return NULL;
327
328 sk_change_net(sock->sk, s->net);
329
315 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE, 330 ret = kernel_setsockopt(sock, SOL_TIPC, TIPC_IMPORTANCE,
316 (char *)&s->imp, sizeof(s->imp)); 331 (char *)&s->imp, sizeof(s->imp));
317 if (ret < 0) 332 if (ret < 0)
@@ -337,11 +352,31 @@ static struct socket *tipc_create_listen_sock(struct tipc_conn *con)
337 pr_err("Unknown socket type %d\n", s->type); 352 pr_err("Unknown socket type %d\n", s->type);
338 goto create_err; 353 goto create_err;
339 } 354 }
355
356 /* As server's listening socket owner and creator is the same module,
357 * we have to decrease TIPC module reference count to guarantee that
358 * it remains zero after the server socket is created, otherwise,
359 * executing "rmmod" command is unable to make TIPC module deleted
360 * after TIPC module is inserted successfully.
361 *
362 * However, the reference count is ever increased twice in
363 * sock_create_kern(): one is to increase the reference count of owner
364 * of TIPC socket's proto_ops struct; another is to increment the
365 * reference count of owner of TIPC proto struct. Therefore, we must
366 * decrement the module reference count twice to ensure that it keeps
367 * zero after server's listening socket is created. Of course, we
368 * must bump the module reference count twice as well before the socket
369 * is closed.
370 */
371 module_put(sock->ops->owner);
372 module_put(sock->sk->sk_prot_creator->owner);
373 set_bit(CF_SERVER, &con->flags);
374
340 return sock; 375 return sock;
341 376
342create_err: 377create_err:
343 sock_release(sock); 378 kernel_sock_shutdown(sock, SHUT_RDWR);
344 con->sock = NULL; 379 sk_release_kernel(sock->sk);
345 return NULL; 380 return NULL;
346} 381}
347 382
diff --git a/net/tipc/socket.c b/net/tipc/socket.c
index b4d4467d0bb0..ee90d74d7516 100644
--- a/net/tipc/socket.c
+++ b/net/tipc/socket.c
@@ -35,7 +35,6 @@
35 */ 35 */
36 36
37#include <linux/rhashtable.h> 37#include <linux/rhashtable.h>
38#include <linux/jhash.h>
39#include "core.h" 38#include "core.h"
40#include "name_table.h" 39#include "name_table.h"
41#include "node.h" 40#include "node.h"
@@ -74,6 +73,7 @@
74 * @link_cong: non-zero if owner must sleep because of link congestion 73 * @link_cong: non-zero if owner must sleep because of link congestion
75 * @sent_unacked: # messages sent by socket, and not yet acked by peer 74 * @sent_unacked: # messages sent by socket, and not yet acked by peer
76 * @rcv_unacked: # messages read by user, but not yet acked back to peer 75 * @rcv_unacked: # messages read by user, but not yet acked back to peer
76 * @remote: 'connected' peer for dgram/rdm
77 * @node: hash table node 77 * @node: hash table node
78 * @rcu: rcu struct for tipc_sock 78 * @rcu: rcu struct for tipc_sock
79 */ 79 */
@@ -96,6 +96,7 @@ struct tipc_sock {
96 bool link_cong; 96 bool link_cong;
97 uint sent_unacked; 97 uint sent_unacked;
98 uint rcv_unacked; 98 uint rcv_unacked;
99 struct sockaddr_tipc remote;
99 struct rhash_head node; 100 struct rhash_head node;
100 struct rcu_head rcu; 101 struct rcu_head rcu;
101}; 102};
@@ -114,13 +115,14 @@ static int tipc_sk_withdraw(struct tipc_sock *tsk, uint scope,
114static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid); 115static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid);
115static int tipc_sk_insert(struct tipc_sock *tsk); 116static int tipc_sk_insert(struct tipc_sock *tsk);
116static void tipc_sk_remove(struct tipc_sock *tsk); 117static void tipc_sk_remove(struct tipc_sock *tsk);
118static int __tipc_send_stream(struct socket *sock, struct msghdr *m,
119 size_t dsz);
120static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
117 121
118static const struct proto_ops packet_ops; 122static const struct proto_ops packet_ops;
119static const struct proto_ops stream_ops; 123static const struct proto_ops stream_ops;
120static const struct proto_ops msg_ops; 124static const struct proto_ops msg_ops;
121
122static struct proto tipc_proto; 125static struct proto tipc_proto;
123static struct proto tipc_proto_kern;
124 126
125static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = { 127static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
126 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC }, 128 [TIPC_NLA_SOCK_UNSPEC] = { .type = NLA_UNSPEC },
@@ -130,6 +132,8 @@ static const struct nla_policy tipc_nl_sock_policy[TIPC_NLA_SOCK_MAX + 1] = {
130 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG } 132 [TIPC_NLA_SOCK_HAS_PUBL] = { .type = NLA_FLAG }
131}; 133};
132 134
135static const struct rhashtable_params tsk_rht_params;
136
133/* 137/*
134 * Revised TIPC socket locking policy: 138 * Revised TIPC socket locking policy:
135 * 139 *
@@ -338,11 +342,7 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
338 } 342 }
339 343
340 /* Allocate socket's protocol area */ 344 /* Allocate socket's protocol area */
341 if (!kern) 345 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
342 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto);
343 else
344 sk = sk_alloc(net, AF_TIPC, GFP_KERNEL, &tipc_proto_kern);
345
346 if (sk == NULL) 346 if (sk == NULL)
347 return -ENOMEM; 347 return -ENOMEM;
348 348
@@ -380,75 +380,6 @@ static int tipc_sk_create(struct net *net, struct socket *sock,
380 return 0; 380 return 0;
381} 381}
382 382
383/**
384 * tipc_sock_create_local - create TIPC socket from inside TIPC module
385 * @type: socket type - SOCK_RDM or SOCK_SEQPACKET
386 *
387 * We cannot use sock_creat_kern here because it bumps module user count.
388 * Since socket owner and creator is the same module we must make sure
389 * that module count remains zero for module local sockets, otherwise
390 * we cannot do rmmod.
391 *
392 * Returns 0 on success, errno otherwise
393 */
394int tipc_sock_create_local(struct net *net, int type, struct socket **res)
395{
396 int rc;
397
398 rc = sock_create_lite(AF_TIPC, type, 0, res);
399 if (rc < 0) {
400 pr_err("Failed to create kernel socket\n");
401 return rc;
402 }
403 tipc_sk_create(net, *res, 0, 1);
404
405 return 0;
406}
407
408/**
409 * tipc_sock_release_local - release socket created by tipc_sock_create_local
410 * @sock: the socket to be released.
411 *
412 * Module reference count is not incremented when such sockets are created,
413 * so we must keep it from being decremented when they are released.
414 */
415void tipc_sock_release_local(struct socket *sock)
416{
417 tipc_release(sock);
418 sock->ops = NULL;
419 sock_release(sock);
420}
421
422/**
423 * tipc_sock_accept_local - accept a connection on a socket created
424 * with tipc_sock_create_local. Use this function to avoid that
425 * module reference count is inadvertently incremented.
426 *
427 * @sock: the accepting socket
428 * @newsock: reference to the new socket to be created
429 * @flags: socket flags
430 */
431
432int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
433 int flags)
434{
435 struct sock *sk = sock->sk;
436 int ret;
437
438 ret = sock_create_lite(sk->sk_family, sk->sk_type,
439 sk->sk_protocol, newsock);
440 if (ret < 0)
441 return ret;
442
443 ret = tipc_accept(sock, *newsock, flags);
444 if (ret < 0) {
445 sock_release(*newsock);
446 return ret;
447 }
448 (*newsock)->ops = sock->ops;
449 return ret;
450}
451
452static void tipc_sk_callback(struct rcu_head *head) 383static void tipc_sk_callback(struct rcu_head *head)
453{ 384{
454 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu); 385 struct tipc_sock *tsk = container_of(head, struct tipc_sock, rcu);
@@ -892,7 +823,6 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
892 823
893/** 824/**
894 * tipc_sendmsg - send message in connectionless manner 825 * tipc_sendmsg - send message in connectionless manner
895 * @iocb: if NULL, indicates that socket lock is already held
896 * @sock: socket structure 826 * @sock: socket structure
897 * @m: message to send 827 * @m: message to send
898 * @dsz: amount of user data to be sent 828 * @dsz: amount of user data to be sent
@@ -904,9 +834,21 @@ static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
904 * 834 *
905 * Returns the number of bytes sent on success, or errno otherwise 835 * Returns the number of bytes sent on success, or errno otherwise
906 */ 836 */
907static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock, 837static int tipc_sendmsg(struct socket *sock,
908 struct msghdr *m, size_t dsz) 838 struct msghdr *m, size_t dsz)
909{ 839{
840 struct sock *sk = sock->sk;
841 int ret;
842
843 lock_sock(sk);
844 ret = __tipc_sendmsg(sock, m, dsz);
845 release_sock(sk);
846
847 return ret;
848}
849
850static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz)
851{
910 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name); 852 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
911 struct sock *sk = sock->sk; 853 struct sock *sk = sock->sk;
912 struct tipc_sock *tsk = tipc_sk(sk); 854 struct tipc_sock *tsk = tipc_sk(sk);
@@ -915,49 +857,40 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
915 u32 dnode, dport; 857 u32 dnode, dport;
916 struct sk_buff_head *pktchain = &sk->sk_write_queue; 858 struct sk_buff_head *pktchain = &sk->sk_write_queue;
917 struct sk_buff *skb; 859 struct sk_buff *skb;
918 struct tipc_name_seq *seq = &dest->addr.nameseq; 860 struct tipc_name_seq *seq;
919 struct iov_iter save; 861 struct iov_iter save;
920 u32 mtu; 862 u32 mtu;
921 long timeo; 863 long timeo;
922 int rc; 864 int rc;
923 865
924 if (unlikely(!dest))
925 return -EDESTADDRREQ;
926
927 if (unlikely((m->msg_namelen < sizeof(*dest)) ||
928 (dest->family != AF_TIPC)))
929 return -EINVAL;
930
931 if (dsz > TIPC_MAX_USER_MSG_SIZE) 866 if (dsz > TIPC_MAX_USER_MSG_SIZE)
932 return -EMSGSIZE; 867 return -EMSGSIZE;
933 868 if (unlikely(!dest)) {
934 if (iocb) 869 if (tsk->connected && sock->state == SS_READY)
935 lock_sock(sk); 870 dest = &tsk->remote;
936 871 else
872 return -EDESTADDRREQ;
873 } else if (unlikely(m->msg_namelen < sizeof(*dest)) ||
874 dest->family != AF_TIPC) {
875 return -EINVAL;
876 }
937 if (unlikely(sock->state != SS_READY)) { 877 if (unlikely(sock->state != SS_READY)) {
938 if (sock->state == SS_LISTENING) { 878 if (sock->state == SS_LISTENING)
939 rc = -EPIPE; 879 return -EPIPE;
940 goto exit; 880 if (sock->state != SS_UNCONNECTED)
941 } 881 return -EISCONN;
942 if (sock->state != SS_UNCONNECTED) { 882 if (tsk->published)
943 rc = -EISCONN; 883 return -EOPNOTSUPP;
944 goto exit;
945 }
946 if (tsk->published) {
947 rc = -EOPNOTSUPP;
948 goto exit;
949 }
950 if (dest->addrtype == TIPC_ADDR_NAME) { 884 if (dest->addrtype == TIPC_ADDR_NAME) {
951 tsk->conn_type = dest->addr.name.name.type; 885 tsk->conn_type = dest->addr.name.name.type;
952 tsk->conn_instance = dest->addr.name.name.instance; 886 tsk->conn_instance = dest->addr.name.name.instance;
953 } 887 }
954 } 888 }
955 889 seq = &dest->addr.nameseq;
956 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 890 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
957 891
958 if (dest->addrtype == TIPC_ADDR_MCAST) { 892 if (dest->addrtype == TIPC_ADDR_MCAST) {
959 rc = tipc_sendmcast(sock, seq, m, dsz, timeo); 893 return tipc_sendmcast(sock, seq, m, dsz, timeo);
960 goto exit;
961 } else if (dest->addrtype == TIPC_ADDR_NAME) { 894 } else if (dest->addrtype == TIPC_ADDR_NAME) {
962 u32 type = dest->addr.name.name.type; 895 u32 type = dest->addr.name.name.type;
963 u32 inst = dest->addr.name.name.instance; 896 u32 inst = dest->addr.name.name.instance;
@@ -972,10 +905,8 @@ static int tipc_sendmsg(struct kiocb *iocb, struct socket *sock,
972 dport = tipc_nametbl_translate(net, type, inst, &dnode); 905 dport = tipc_nametbl_translate(net, type, inst, &dnode);
973 msg_set_destnode(mhdr, dnode); 906 msg_set_destnode(mhdr, dnode);
974 msg_set_destport(mhdr, dport); 907 msg_set_destport(mhdr, dport);
975 if (unlikely(!dport && !dnode)) { 908 if (unlikely(!dport && !dnode))
976 rc = -EHOSTUNREACH; 909 return -EHOSTUNREACH;
977 goto exit;
978 }
979 } else if (dest->addrtype == TIPC_ADDR_ID) { 910 } else if (dest->addrtype == TIPC_ADDR_ID) {
980 dnode = dest->addr.id.node; 911 dnode = dest->addr.id.node;
981 msg_set_type(mhdr, TIPC_DIRECT_MSG); 912 msg_set_type(mhdr, TIPC_DIRECT_MSG);
@@ -990,7 +921,7 @@ new_mtu:
990 mtu = tipc_node_get_mtu(net, dnode, tsk->portid); 921 mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
991 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain); 922 rc = tipc_msg_build(mhdr, m, 0, dsz, mtu, pktchain);
992 if (rc < 0) 923 if (rc < 0)
993 goto exit; 924 return rc;
994 925
995 do { 926 do {
996 skb = skb_peek(pktchain); 927 skb = skb_peek(pktchain);
@@ -1013,9 +944,6 @@ new_mtu:
1013 if (rc) 944 if (rc)
1014 __skb_queue_purge(pktchain); 945 __skb_queue_purge(pktchain);
1015 } while (!rc); 946 } while (!rc);
1016exit:
1017 if (iocb)
1018 release_sock(sk);
1019 947
1020 return rc; 948 return rc;
1021} 949}
@@ -1052,7 +980,6 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1052 980
1053/** 981/**
1054 * tipc_send_stream - send stream-oriented data 982 * tipc_send_stream - send stream-oriented data
1055 * @iocb: (unused)
1056 * @sock: socket structure 983 * @sock: socket structure
1057 * @m: data to send 984 * @m: data to send
1058 * @dsz: total length of data to be transmitted 985 * @dsz: total length of data to be transmitted
@@ -1062,8 +989,19 @@ static int tipc_wait_for_sndpkt(struct socket *sock, long *timeo_p)
1062 * Returns the number of bytes sent on success (or partial success), 989 * Returns the number of bytes sent on success (or partial success),
1063 * or errno if no data sent 990 * or errno if no data sent
1064 */ 991 */
1065static int tipc_send_stream(struct kiocb *iocb, struct socket *sock, 992static int tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1066 struct msghdr *m, size_t dsz) 993{
994 struct sock *sk = sock->sk;
995 int ret;
996
997 lock_sock(sk);
998 ret = __tipc_send_stream(sock, m, dsz);
999 release_sock(sk);
1000
1001 return ret;
1002}
1003
1004static int __tipc_send_stream(struct socket *sock, struct msghdr *m, size_t dsz)
1067{ 1005{
1068 struct sock *sk = sock->sk; 1006 struct sock *sk = sock->sk;
1069 struct net *net = sock_net(sk); 1007 struct net *net = sock_net(sk);
@@ -1080,7 +1018,7 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
1080 1018
1081 /* Handle implied connection establishment */ 1019 /* Handle implied connection establishment */
1082 if (unlikely(dest)) { 1020 if (unlikely(dest)) {
1083 rc = tipc_sendmsg(iocb, sock, m, dsz); 1021 rc = __tipc_sendmsg(sock, m, dsz);
1084 if (dsz && (dsz == rc)) 1022 if (dsz && (dsz == rc))
1085 tsk->sent_unacked = 1; 1023 tsk->sent_unacked = 1;
1086 return rc; 1024 return rc;
@@ -1088,15 +1026,11 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
1088 if (dsz > (uint)INT_MAX) 1026 if (dsz > (uint)INT_MAX)
1089 return -EMSGSIZE; 1027 return -EMSGSIZE;
1090 1028
1091 if (iocb)
1092 lock_sock(sk);
1093
1094 if (unlikely(sock->state != SS_CONNECTED)) { 1029 if (unlikely(sock->state != SS_CONNECTED)) {
1095 if (sock->state == SS_DISCONNECTING) 1030 if (sock->state == SS_DISCONNECTING)
1096 rc = -EPIPE; 1031 return -EPIPE;
1097 else 1032 else
1098 rc = -ENOTCONN; 1033 return -ENOTCONN;
1099 goto exit;
1100 } 1034 }
1101 1035
1102 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT); 1036 timeo = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
@@ -1108,7 +1042,7 @@ next:
1108 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE); 1042 send = min_t(uint, dsz - sent, TIPC_MAX_USER_MSG_SIZE);
1109 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain); 1043 rc = tipc_msg_build(mhdr, m, sent, send, mtu, pktchain);
1110 if (unlikely(rc < 0)) 1044 if (unlikely(rc < 0))
1111 goto exit; 1045 return rc;
1112 do { 1046 do {
1113 if (likely(!tsk_conn_cong(tsk))) { 1047 if (likely(!tsk_conn_cong(tsk))) {
1114 rc = tipc_link_xmit(net, pktchain, dnode, portid); 1048 rc = tipc_link_xmit(net, pktchain, dnode, portid);
@@ -1133,15 +1067,12 @@ next:
1133 if (rc) 1067 if (rc)
1134 __skb_queue_purge(pktchain); 1068 __skb_queue_purge(pktchain);
1135 } while (!rc); 1069 } while (!rc);
1136exit: 1070
1137 if (iocb)
1138 release_sock(sk);
1139 return sent ? sent : rc; 1071 return sent ? sent : rc;
1140} 1072}
1141 1073
1142/** 1074/**
1143 * tipc_send_packet - send a connection-oriented message 1075 * tipc_send_packet - send a connection-oriented message
1144 * @iocb: if NULL, indicates that socket lock is already held
1145 * @sock: socket structure 1076 * @sock: socket structure
1146 * @m: message to send 1077 * @m: message to send
1147 * @dsz: length of data to be transmitted 1078 * @dsz: length of data to be transmitted
@@ -1150,13 +1081,12 @@ exit:
1150 * 1081 *
1151 * Returns the number of bytes sent on success, or errno otherwise 1082 * Returns the number of bytes sent on success, or errno otherwise
1152 */ 1083 */
1153static int tipc_send_packet(struct kiocb *iocb, struct socket *sock, 1084static int tipc_send_packet(struct socket *sock, struct msghdr *m, size_t dsz)
1154 struct msghdr *m, size_t dsz)
1155{ 1085{
1156 if (dsz > TIPC_MAX_USER_MSG_SIZE) 1086 if (dsz > TIPC_MAX_USER_MSG_SIZE)
1157 return -EMSGSIZE; 1087 return -EMSGSIZE;
1158 1088
1159 return tipc_send_stream(iocb, sock, m, dsz); 1089 return tipc_send_stream(sock, m, dsz);
1160} 1090}
1161 1091
1162/* tipc_sk_finish_conn - complete the setup of a connection 1092/* tipc_sk_finish_conn - complete the setup of a connection
@@ -1317,12 +1247,12 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1317 err = 0; 1247 err = 0;
1318 if (!skb_queue_empty(&sk->sk_receive_queue)) 1248 if (!skb_queue_empty(&sk->sk_receive_queue))
1319 break; 1249 break;
1320 err = sock_intr_errno(timeo);
1321 if (signal_pending(current))
1322 break;
1323 err = -EAGAIN; 1250 err = -EAGAIN;
1324 if (!timeo) 1251 if (!timeo)
1325 break; 1252 break;
1253 err = sock_intr_errno(timeo);
1254 if (signal_pending(current))
1255 break;
1326 } 1256 }
1327 finish_wait(sk_sleep(sk), &wait); 1257 finish_wait(sk_sleep(sk), &wait);
1328 *timeop = timeo; 1258 *timeop = timeo;
@@ -1331,7 +1261,6 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1331 1261
1332/** 1262/**
1333 * tipc_recvmsg - receive packet-oriented message 1263 * tipc_recvmsg - receive packet-oriented message
1334 * @iocb: (unused)
1335 * @m: descriptor for message info 1264 * @m: descriptor for message info
1336 * @buf_len: total size of user buffer area 1265 * @buf_len: total size of user buffer area
1337 * @flags: receive flags 1266 * @flags: receive flags
@@ -1341,8 +1270,8 @@ static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
1341 * 1270 *
1342 * Returns size of returned message data, errno otherwise 1271 * Returns size of returned message data, errno otherwise
1343 */ 1272 */
1344static int tipc_recvmsg(struct kiocb *iocb, struct socket *sock, 1273static int tipc_recvmsg(struct socket *sock, struct msghdr *m, size_t buf_len,
1345 struct msghdr *m, size_t buf_len, int flags) 1274 int flags)
1346{ 1275{
1347 struct sock *sk = sock->sk; 1276 struct sock *sk = sock->sk;
1348 struct tipc_sock *tsk = tipc_sk(sk); 1277 struct tipc_sock *tsk = tipc_sk(sk);
@@ -1426,7 +1355,6 @@ exit:
1426 1355
1427/** 1356/**
1428 * tipc_recv_stream - receive stream-oriented data 1357 * tipc_recv_stream - receive stream-oriented data
1429 * @iocb: (unused)
1430 * @m: descriptor for message info 1358 * @m: descriptor for message info
1431 * @buf_len: total size of user buffer area 1359 * @buf_len: total size of user buffer area
1432 * @flags: receive flags 1360 * @flags: receive flags
@@ -1436,8 +1364,8 @@ exit:
1436 * 1364 *
1437 * Returns size of returned message data, errno otherwise 1365 * Returns size of returned message data, errno otherwise
1438 */ 1366 */
1439static int tipc_recv_stream(struct kiocb *iocb, struct socket *sock, 1367static int tipc_recv_stream(struct socket *sock, struct msghdr *m,
1440 struct msghdr *m, size_t buf_len, int flags) 1368 size_t buf_len, int flags)
1441{ 1369{
1442 struct sock *sk = sock->sk; 1370 struct sock *sk = sock->sk;
1443 struct tipc_sock *tsk = tipc_sk(sk); 1371 struct tipc_sock *tsk = tipc_sk(sk);
@@ -1909,17 +1837,26 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1909 int destlen, int flags) 1837 int destlen, int flags)
1910{ 1838{
1911 struct sock *sk = sock->sk; 1839 struct sock *sk = sock->sk;
1840 struct tipc_sock *tsk = tipc_sk(sk);
1912 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest; 1841 struct sockaddr_tipc *dst = (struct sockaddr_tipc *)dest;
1913 struct msghdr m = {NULL,}; 1842 struct msghdr m = {NULL,};
1914 long timeout = (flags & O_NONBLOCK) ? 0 : tipc_sk(sk)->conn_timeout; 1843 long timeout = (flags & O_NONBLOCK) ? 0 : tsk->conn_timeout;
1915 socket_state previous; 1844 socket_state previous;
1916 int res; 1845 int res = 0;
1917 1846
1918 lock_sock(sk); 1847 lock_sock(sk);
1919 1848
1920 /* For now, TIPC does not allow use of connect() with DGRAM/RDM types */ 1849 /* DGRAM/RDM connect(), just save the destaddr */
1921 if (sock->state == SS_READY) { 1850 if (sock->state == SS_READY) {
1922 res = -EOPNOTSUPP; 1851 if (dst->family == AF_UNSPEC) {
1852 memset(&tsk->remote, 0, sizeof(struct sockaddr_tipc));
1853 tsk->connected = 0;
1854 } else if (destlen != sizeof(struct sockaddr_tipc)) {
1855 res = -EINVAL;
1856 } else {
1857 memcpy(&tsk->remote, dest, destlen);
1858 tsk->connected = 1;
1859 }
1923 goto exit; 1860 goto exit;
1924 } 1861 }
1925 1862
@@ -1947,7 +1884,7 @@ static int tipc_connect(struct socket *sock, struct sockaddr *dest,
1947 if (!timeout) 1884 if (!timeout)
1948 m.msg_flags = MSG_DONTWAIT; 1885 m.msg_flags = MSG_DONTWAIT;
1949 1886
1950 res = tipc_sendmsg(NULL, sock, &m, 0); 1887 res = __tipc_sendmsg(sock, &m, 0);
1951 if ((res < 0) && (res != -EWOULDBLOCK)) 1888 if ((res < 0) && (res != -EWOULDBLOCK))
1952 goto exit; 1889 goto exit;
1953 1890
@@ -2027,12 +1964,12 @@ static int tipc_wait_for_accept(struct socket *sock, long timeo)
2027 err = -EINVAL; 1964 err = -EINVAL;
2028 if (sock->state != SS_LISTENING) 1965 if (sock->state != SS_LISTENING)
2029 break; 1966 break;
2030 err = sock_intr_errno(timeo);
2031 if (signal_pending(current))
2032 break;
2033 err = -EAGAIN; 1967 err = -EAGAIN;
2034 if (!timeo) 1968 if (!timeo)
2035 break; 1969 break;
1970 err = sock_intr_errno(timeo);
1971 if (signal_pending(current))
1972 break;
2036 } 1973 }
2037 finish_wait(sk_sleep(sk), &wait); 1974 finish_wait(sk_sleep(sk), &wait);
2038 return err; 1975 return err;
@@ -2103,7 +2040,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
2103 struct msghdr m = {NULL,}; 2040 struct msghdr m = {NULL,};
2104 2041
2105 tsk_advance_rx_queue(sk); 2042 tsk_advance_rx_queue(sk);
2106 tipc_send_packet(NULL, new_sock, &m, 0); 2043 __tipc_send_stream(new_sock, &m, 0);
2107 } else { 2044 } else {
2108 __skb_dequeue(&sk->sk_receive_queue); 2045 __skb_dequeue(&sk->sk_receive_queue);
2109 __skb_queue_head(&new_sk->sk_receive_queue, buf); 2046 __skb_queue_head(&new_sk->sk_receive_queue, buf);
@@ -2154,7 +2091,6 @@ restart:
2154 TIPC_CONN_SHUTDOWN)) 2091 TIPC_CONN_SHUTDOWN))
2155 tipc_link_xmit_skb(net, skb, dnode, 2092 tipc_link_xmit_skb(net, skb, dnode,
2156 tsk->portid); 2093 tsk->portid);
2157 tipc_node_remove_conn(net, dnode, tsk->portid);
2158 } else { 2094 } else {
2159 dnode = tsk_peer_node(tsk); 2095 dnode = tsk_peer_node(tsk);
2160 2096
@@ -2312,7 +2248,7 @@ static struct tipc_sock *tipc_sk_lookup(struct net *net, u32 portid)
2312 struct tipc_sock *tsk; 2248 struct tipc_sock *tsk;
2313 2249
2314 rcu_read_lock(); 2250 rcu_read_lock();
2315 tsk = rhashtable_lookup(&tn->sk_rht, &portid); 2251 tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2316 if (tsk) 2252 if (tsk)
2317 sock_hold(&tsk->sk); 2253 sock_hold(&tsk->sk);
2318 rcu_read_unlock(); 2254 rcu_read_unlock();
@@ -2334,7 +2270,8 @@ static int tipc_sk_insert(struct tipc_sock *tsk)
2334 portid = TIPC_MIN_PORT; 2270 portid = TIPC_MIN_PORT;
2335 tsk->portid = portid; 2271 tsk->portid = portid;
2336 sock_hold(&tsk->sk); 2272 sock_hold(&tsk->sk);
2337 if (rhashtable_lookup_insert(&tn->sk_rht, &tsk->node)) 2273 if (!rhashtable_lookup_insert_fast(&tn->sk_rht, &tsk->node,
2274 tsk_rht_params))
2338 return 0; 2275 return 0;
2339 sock_put(&tsk->sk); 2276 sock_put(&tsk->sk);
2340 } 2277 }
@@ -2347,26 +2284,27 @@ static void tipc_sk_remove(struct tipc_sock *tsk)
2347 struct sock *sk = &tsk->sk; 2284 struct sock *sk = &tsk->sk;
2348 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id); 2285 struct tipc_net *tn = net_generic(sock_net(sk), tipc_net_id);
2349 2286
2350 if (rhashtable_remove(&tn->sk_rht, &tsk->node)) { 2287 if (!rhashtable_remove_fast(&tn->sk_rht, &tsk->node, tsk_rht_params)) {
2351 WARN_ON(atomic_read(&sk->sk_refcnt) == 1); 2288 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
2352 __sock_put(sk); 2289 __sock_put(sk);
2353 } 2290 }
2354} 2291}
2355 2292
2293static const struct rhashtable_params tsk_rht_params = {
2294 .nelem_hint = 192,
2295 .head_offset = offsetof(struct tipc_sock, node),
2296 .key_offset = offsetof(struct tipc_sock, portid),
2297 .key_len = sizeof(u32), /* portid */
2298 .max_size = 1048576,
2299 .min_size = 256,
2300 .automatic_shrinking = true,
2301};
2302
2356int tipc_sk_rht_init(struct net *net) 2303int tipc_sk_rht_init(struct net *net)
2357{ 2304{
2358 struct tipc_net *tn = net_generic(net, tipc_net_id); 2305 struct tipc_net *tn = net_generic(net, tipc_net_id);
2359 struct rhashtable_params rht_params = {
2360 .nelem_hint = 192,
2361 .head_offset = offsetof(struct tipc_sock, node),
2362 .key_offset = offsetof(struct tipc_sock, portid),
2363 .key_len = sizeof(u32), /* portid */
2364 .hashfn = jhash,
2365 .max_shift = 20, /* 1M */
2366 .min_shift = 8, /* 256 */
2367 };
2368 2306
2369 return rhashtable_init(&tn->sk_rht, &rht_params); 2307 return rhashtable_init(&tn->sk_rht, &tsk_rht_params);
2370} 2308}
2371 2309
2372void tipc_sk_rht_destroy(struct net *net) 2310void tipc_sk_rht_destroy(struct net *net)
@@ -2609,12 +2547,6 @@ static struct proto tipc_proto = {
2609 .sysctl_rmem = sysctl_tipc_rmem 2547 .sysctl_rmem = sysctl_tipc_rmem
2610}; 2548};
2611 2549
2612static struct proto tipc_proto_kern = {
2613 .name = "TIPC",
2614 .obj_size = sizeof(struct tipc_sock),
2615 .sysctl_rmem = sysctl_tipc_rmem
2616};
2617
2618/** 2550/**
2619 * tipc_socket_init - initialize TIPC socket interface 2551 * tipc_socket_init - initialize TIPC socket interface
2620 * 2552 *
diff --git a/net/tipc/socket.h b/net/tipc/socket.h
index 238f1b7bd9bd..bf6551389522 100644
--- a/net/tipc/socket.h
+++ b/net/tipc/socket.h
@@ -44,10 +44,6 @@
44 SKB_TRUESIZE(TIPC_MAX_USER_MSG_SIZE)) 44 SKB_TRUESIZE(TIPC_MAX_USER_MSG_SIZE))
45int tipc_socket_init(void); 45int tipc_socket_init(void);
46void tipc_socket_stop(void); 46void tipc_socket_stop(void);
47int tipc_sock_create_local(struct net *net, int type, struct socket **res);
48void tipc_sock_release_local(struct socket *sock);
49int tipc_sock_accept_local(struct socket *sock, struct socket **newsock,
50 int flags);
51int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq); 47int tipc_sk_rcv(struct net *net, struct sk_buff_head *inputq);
52void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq, 48void tipc_sk_mcast_rcv(struct net *net, struct sk_buff_head *arrvq,
53 struct sk_buff_head *inputq); 49 struct sk_buff_head *inputq);
diff --git a/net/tipc/subscr.c b/net/tipc/subscr.c
index 72c339e432aa..1c147c869c2e 100644
--- a/net/tipc/subscr.c
+++ b/net/tipc/subscr.c
@@ -162,19 +162,6 @@ static void subscr_del(struct tipc_subscription *sub)
162 atomic_dec(&tn->subscription_count); 162 atomic_dec(&tn->subscription_count);
163} 163}
164 164
165/**
166 * subscr_terminate - terminate communication with a subscriber
167 *
168 * Note: Must call it in process context since it might sleep.
169 */
170static void subscr_terminate(struct tipc_subscription *sub)
171{
172 struct tipc_subscriber *subscriber = sub->subscriber;
173 struct tipc_net *tn = net_generic(sub->net, tipc_net_id);
174
175 tipc_conn_terminate(tn->topsrv, subscriber->conid);
176}
177
178static void subscr_release(struct tipc_subscriber *subscriber) 165static void subscr_release(struct tipc_subscriber *subscriber)
179{ 166{
180 struct tipc_subscription *sub; 167 struct tipc_subscription *sub;
@@ -312,16 +299,14 @@ static void subscr_conn_msg_event(struct net *net, int conid,
312{ 299{
313 struct tipc_subscriber *subscriber = usr_data; 300 struct tipc_subscriber *subscriber = usr_data;
314 struct tipc_subscription *sub = NULL; 301 struct tipc_subscription *sub = NULL;
302 struct tipc_net *tn = net_generic(net, tipc_net_id);
315 303
316 spin_lock_bh(&subscriber->lock); 304 spin_lock_bh(&subscriber->lock);
317 if (subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber, 305 subscr_subscribe(net, (struct tipc_subscr *)buf, subscriber, &sub);
318 &sub) < 0) {
319 spin_unlock_bh(&subscriber->lock);
320 subscr_terminate(sub);
321 return;
322 }
323 if (sub) 306 if (sub)
324 tipc_nametbl_subscribe(sub); 307 tipc_nametbl_subscribe(sub);
308 else
309 tipc_conn_terminate(tn->topsrv, subscriber->conid);
325 spin_unlock_bh(&subscriber->lock); 310 spin_unlock_bh(&subscriber->lock);
326} 311}
327 312
diff --git a/net/tipc/udp_media.c b/net/tipc/udp_media.c
new file mode 100644
index 000000000000..66deebc66aa1
--- /dev/null
+++ b/net/tipc/udp_media.c
@@ -0,0 +1,448 @@
1/* net/tipc/udp_media.c: IP bearer support for TIPC
2 *
3 * Copyright (c) 2015, Ericsson AB
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the names of the copyright holders nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * Alternatively, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2 as published by the Free
20 * Software Foundation.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <linux/socket.h>
36#include <linux/ip.h>
37#include <linux/udp.h>
38#include <linux/inet.h>
39#include <linux/inetdevice.h>
40#include <linux/igmp.h>
41#include <linux/kernel.h>
42#include <linux/workqueue.h>
43#include <linux/list.h>
44#include <net/sock.h>
45#include <net/ip.h>
46#include <net/udp_tunnel.h>
47#include <net/addrconf.h>
48#include <linux/tipc_netlink.h>
49#include "core.h"
50#include "bearer.h"
51
52/* IANA assigned UDP port */
53#define UDP_PORT_DEFAULT 6118
54
55static const struct nla_policy tipc_nl_udp_policy[TIPC_NLA_UDP_MAX + 1] = {
56 [TIPC_NLA_UDP_UNSPEC] = {.type = NLA_UNSPEC},
57 [TIPC_NLA_UDP_LOCAL] = {.type = NLA_BINARY,
58 .len = sizeof(struct sockaddr_storage)},
59 [TIPC_NLA_UDP_REMOTE] = {.type = NLA_BINARY,
60 .len = sizeof(struct sockaddr_storage)},
61};
62
63/**
64 * struct udp_media_addr - IP/UDP addressing information
65 *
66 * This is the bearer level originating address used in neighbor discovery
67 * messages, and all fields should be in network byte order
68 */
69struct udp_media_addr {
70 __be16 proto;
71 __be16 udp_port;
72 union {
73 struct in_addr ipv4;
74 struct in6_addr ipv6;
75 };
76};
77
78/**
79 * struct udp_bearer - ip/udp bearer data structure
80 * @bearer: associated generic tipc bearer
81 * @ubsock: bearer associated socket
82 * @ifindex: local address scope
83 * @work: used to schedule deferred work on a bearer
84 */
85struct udp_bearer {
86 struct tipc_bearer __rcu *bearer;
87 struct socket *ubsock;
88 u32 ifindex;
89 struct work_struct work;
90};
91
92/* udp_media_addr_set - convert a ip/udp address to a TIPC media address */
93static void tipc_udp_media_addr_set(struct tipc_media_addr *addr,
94 struct udp_media_addr *ua)
95{
96 memset(addr, 0, sizeof(struct tipc_media_addr));
97 addr->media_id = TIPC_MEDIA_TYPE_UDP;
98 memcpy(addr->value, ua, sizeof(struct udp_media_addr));
99 if (ntohs(ua->proto) == ETH_P_IP) {
100 if (ipv4_is_multicast(ua->ipv4.s_addr))
101 addr->broadcast = 1;
102 } else if (ntohs(ua->proto) == ETH_P_IPV6) {
103 if (ipv6_addr_type(&ua->ipv6) & IPV6_ADDR_MULTICAST)
104 addr->broadcast = 1;
105 } else {
106 pr_err("Invalid UDP media address\n");
107 }
108}
109
110/* tipc_udp_addr2str - convert ip/udp address to string */
111static int tipc_udp_addr2str(struct tipc_media_addr *a, char *buf, int size)
112{
113 struct udp_media_addr *ua = (struct udp_media_addr *)&a->value;
114
115 if (ntohs(ua->proto) == ETH_P_IP)
116 snprintf(buf, size, "%pI4:%u", &ua->ipv4, ntohs(ua->udp_port));
117 else if (ntohs(ua->proto) == ETH_P_IPV6)
118 snprintf(buf, size, "%pI6:%u", &ua->ipv6, ntohs(ua->udp_port));
119 else
120 pr_err("Invalid UDP media address\n");
121 return 0;
122}
123
124/* tipc_udp_msg2addr - extract an ip/udp address from a TIPC ndisc message */
125static int tipc_udp_msg2addr(struct tipc_bearer *b, struct tipc_media_addr *a,
126 char *msg)
127{
128 struct udp_media_addr *ua;
129
130 ua = (struct udp_media_addr *) (msg + TIPC_MEDIA_ADDR_OFFSET);
131 if (msg[TIPC_MEDIA_TYPE_OFFSET] != TIPC_MEDIA_TYPE_UDP)
132 return -EINVAL;
133 tipc_udp_media_addr_set(a, ua);
134 return 0;
135}
136
137/* tipc_udp_addr2msg - write an ip/udp address to a TIPC ndisc message */
138static int tipc_udp_addr2msg(char *msg, struct tipc_media_addr *a)
139{
140 memset(msg, 0, TIPC_MEDIA_INFO_SIZE);
141 msg[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_UDP;
142 memcpy(msg + TIPC_MEDIA_ADDR_OFFSET, a->value,
143 sizeof(struct udp_media_addr));
144 return 0;
145}
146
147/* tipc_send_msg - enqueue a send request */
148static int tipc_udp_send_msg(struct net *net, struct sk_buff *skb,
149 struct tipc_bearer *b,
150 struct tipc_media_addr *dest)
151{
152 int ttl, err = 0;
153 struct udp_bearer *ub;
154 struct udp_media_addr *dst = (struct udp_media_addr *)&dest->value;
155 struct udp_media_addr *src = (struct udp_media_addr *)&b->addr.value;
156 struct sk_buff *clone;
157 struct rtable *rt;
158
159 clone = skb_clone(skb, GFP_ATOMIC);
160 skb_set_inner_protocol(clone, htons(ETH_P_TIPC));
161 ub = rcu_dereference_rtnl(b->media_ptr);
162 if (!ub) {
163 err = -ENODEV;
164 goto tx_error;
165 }
166 if (dst->proto == htons(ETH_P_IP)) {
167 struct flowi4 fl = {
168 .daddr = dst->ipv4.s_addr,
169 .saddr = src->ipv4.s_addr,
170 .flowi4_mark = clone->mark,
171 .flowi4_proto = IPPROTO_UDP
172 };
173 rt = ip_route_output_key(net, &fl);
174 if (IS_ERR(rt)) {
175 err = PTR_ERR(rt);
176 goto tx_error;
177 }
178 ttl = ip4_dst_hoplimit(&rt->dst);
179 err = udp_tunnel_xmit_skb(rt, ub->ubsock->sk, clone,
180 src->ipv4.s_addr,
181 dst->ipv4.s_addr, 0, ttl, 0,
182 src->udp_port, dst->udp_port,
183 false, true);
184 if (err < 0) {
185 ip_rt_put(rt);
186 goto tx_error;
187 }
188#if IS_ENABLED(CONFIG_IPV6)
189 } else {
190 struct dst_entry *ndst;
191 struct flowi6 fl6 = {
192 .flowi6_oif = ub->ifindex,
193 .daddr = dst->ipv6,
194 .saddr = src->ipv6,
195 .flowi6_proto = IPPROTO_UDP
196 };
197 err = ipv6_stub->ipv6_dst_lookup(ub->ubsock->sk, &ndst, &fl6);
198 if (err)
199 goto tx_error;
200 ttl = ip6_dst_hoplimit(ndst);
201 err = udp_tunnel6_xmit_skb(ndst, ub->ubsock->sk, clone,
202 ndst->dev, &src->ipv6,
203 &dst->ipv6, 0, ttl, src->udp_port,
204 dst->udp_port, false);
205#endif
206 }
207 return err;
208
209tx_error:
210 kfree_skb(clone);
211 return err;
212}
213
214/* tipc_udp_recv - read data from bearer socket */
215static int tipc_udp_recv(struct sock *sk, struct sk_buff *skb)
216{
217 struct udp_bearer *ub;
218 struct tipc_bearer *b;
219
220 ub = rcu_dereference_sk_user_data(sk);
221 if (!ub) {
222 pr_err_ratelimited("Failed to get UDP bearer reference");
223 kfree_skb(skb);
224 return 0;
225 }
226
227 skb_pull(skb, sizeof(struct udphdr));
228 rcu_read_lock();
229 b = rcu_dereference_rtnl(ub->bearer);
230
231 if (b) {
232 tipc_rcv(sock_net(sk), skb, b);
233 rcu_read_unlock();
234 return 0;
235 }
236 rcu_read_unlock();
237 kfree_skb(skb);
238 return 0;
239}
240
241static int enable_mcast(struct udp_bearer *ub, struct udp_media_addr *remote)
242{
243 int err = 0;
244 struct ip_mreqn mreqn;
245 struct sock *sk = ub->ubsock->sk;
246
247 if (ntohs(remote->proto) == ETH_P_IP) {
248 if (!ipv4_is_multicast(remote->ipv4.s_addr))
249 return 0;
250 mreqn.imr_multiaddr = remote->ipv4;
251 mreqn.imr_ifindex = ub->ifindex;
252 err = ip_mc_join_group(sk, &mreqn);
253#if IS_ENABLED(CONFIG_IPV6)
254 } else {
255 if (!ipv6_addr_is_multicast(&remote->ipv6))
256 return 0;
257 err = ipv6_stub->ipv6_sock_mc_join(sk, ub->ifindex,
258 &remote->ipv6);
259#endif
260 }
261 return err;
262}
263
264/**
265 * parse_options - build local/remote addresses from configuration
266 * @attrs: netlink config data
267 * @ub: UDP bearer instance
268 * @local: local bearer IP address/port
269 * @remote: peer or multicast IP/port
270 */
271static int parse_options(struct nlattr *attrs[], struct udp_bearer *ub,
272 struct udp_media_addr *local,
273 struct udp_media_addr *remote)
274{
275 struct nlattr *opts[TIPC_NLA_UDP_MAX + 1];
276 struct sockaddr_storage *sa_local, *sa_remote;
277
278 if (!attrs[TIPC_NLA_BEARER_UDP_OPTS])
279 goto err;
280 if (nla_parse_nested(opts, TIPC_NLA_UDP_MAX,
281 attrs[TIPC_NLA_BEARER_UDP_OPTS],
282 tipc_nl_udp_policy))
283 goto err;
284 if (opts[TIPC_NLA_UDP_LOCAL] && opts[TIPC_NLA_UDP_REMOTE]) {
285 sa_local = nla_data(opts[TIPC_NLA_UDP_LOCAL]);
286 sa_remote = nla_data(opts[TIPC_NLA_UDP_REMOTE]);
287 } else {
288err:
289 pr_err("Invalid UDP bearer configuration");
290 return -EINVAL;
291 }
292 if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET) {
293 struct sockaddr_in *ip4;
294
295 ip4 = (struct sockaddr_in *)sa_local;
296 local->proto = htons(ETH_P_IP);
297 local->udp_port = ip4->sin_port;
298 local->ipv4.s_addr = ip4->sin_addr.s_addr;
299
300 ip4 = (struct sockaddr_in *)sa_remote;
301 remote->proto = htons(ETH_P_IP);
302 remote->udp_port = ip4->sin_port;
303 remote->ipv4.s_addr = ip4->sin_addr.s_addr;
304 return 0;
305
306#if IS_ENABLED(CONFIG_IPV6)
307 } else if ((sa_local->ss_family & sa_remote->ss_family) == AF_INET6) {
308 struct sockaddr_in6 *ip6;
309
310 ip6 = (struct sockaddr_in6 *)sa_local;
311 local->proto = htons(ETH_P_IPV6);
312 local->udp_port = ip6->sin6_port;
313 local->ipv6 = ip6->sin6_addr;
314 ub->ifindex = ip6->sin6_scope_id;
315
316 ip6 = (struct sockaddr_in6 *)sa_remote;
317 remote->proto = htons(ETH_P_IPV6);
318 remote->udp_port = ip6->sin6_port;
319 remote->ipv6 = ip6->sin6_addr;
320 return 0;
321#endif
322 }
323 return -EADDRNOTAVAIL;
324}
325
326/**
327 * tipc_udp_enable - callback to create a new udp bearer instance
328 * @net: network namespace
329 * @b: pointer to generic tipc_bearer
330 * @attrs: netlink bearer configuration
331 *
332 * validate the bearer parameters and initialize the udp bearer
333 * rtnl_lock should be held
334 */
335static int tipc_udp_enable(struct net *net, struct tipc_bearer *b,
336 struct nlattr *attrs[])
337{
338 int err = -EINVAL;
339 struct udp_bearer *ub;
340 struct udp_media_addr *remote;
341 struct udp_media_addr local = {0};
342 struct udp_port_cfg udp_conf = {0};
343 struct udp_tunnel_sock_cfg tuncfg = {NULL};
344
345 ub = kzalloc(sizeof(*ub), GFP_ATOMIC);
346 if (!ub)
347 return -ENOMEM;
348
349 remote = (struct udp_media_addr *)&b->bcast_addr.value;
350 memset(remote, 0, sizeof(struct udp_media_addr));
351 err = parse_options(attrs, ub, &local, remote);
352 if (err)
353 goto err;
354
355 b->bcast_addr.media_id = TIPC_MEDIA_TYPE_UDP;
356 b->bcast_addr.broadcast = 1;
357 rcu_assign_pointer(b->media_ptr, ub);
358 rcu_assign_pointer(ub->bearer, b);
359 tipc_udp_media_addr_set(&b->addr, &local);
360 if (local.proto == htons(ETH_P_IP)) {
361 struct net_device *dev;
362
363 dev = __ip_dev_find(net, local.ipv4.s_addr, false);
364 if (!dev) {
365 err = -ENODEV;
366 goto err;
367 }
368 udp_conf.family = AF_INET;
369 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
370 udp_conf.use_udp_checksums = false;
371 ub->ifindex = dev->ifindex;
372 b->mtu = dev->mtu - sizeof(struct iphdr)
373 - sizeof(struct udphdr);
374#if IS_ENABLED(CONFIG_IPV6)
375 } else if (local.proto == htons(ETH_P_IPV6)) {
376 udp_conf.family = AF_INET6;
377 udp_conf.use_udp6_tx_checksums = true;
378 udp_conf.use_udp6_rx_checksums = true;
379 udp_conf.local_ip6 = in6addr_any;
380 b->mtu = 1280;
381#endif
382 } else {
383 err = -EAFNOSUPPORT;
384 goto err;
385 }
386 udp_conf.local_udp_port = local.udp_port;
387 err = udp_sock_create(net, &udp_conf, &ub->ubsock);
388 if (err)
389 goto err;
390 tuncfg.sk_user_data = ub;
391 tuncfg.encap_type = 1;
392 tuncfg.encap_rcv = tipc_udp_recv;
393 tuncfg.encap_destroy = NULL;
394 setup_udp_tunnel_sock(net, ub->ubsock, &tuncfg);
395
396 if (enable_mcast(ub, remote))
397 goto err;
398 return 0;
399err:
400 kfree(ub);
401 return err;
402}
403
404/* cleanup_bearer - break the socket/bearer association */
405static void cleanup_bearer(struct work_struct *work)
406{
407 struct udp_bearer *ub = container_of(work, struct udp_bearer, work);
408
409 if (ub->ubsock)
410 udp_tunnel_sock_release(ub->ubsock);
411 synchronize_net();
412 kfree(ub);
413}
414
415/* tipc_udp_disable - detach bearer from socket */
416static void tipc_udp_disable(struct tipc_bearer *b)
417{
418 struct udp_bearer *ub;
419
420 ub = rcu_dereference_rtnl(b->media_ptr);
421 if (!ub) {
422 pr_err("UDP bearer instance not found\n");
423 return;
424 }
425 if (ub->ubsock)
426 sock_set_flag(ub->ubsock->sk, SOCK_DEAD);
427 RCU_INIT_POINTER(b->media_ptr, NULL);
428 RCU_INIT_POINTER(ub->bearer, NULL);
429
430 /* sock_release need to be done outside of rtnl lock */
431 INIT_WORK(&ub->work, cleanup_bearer);
432 schedule_work(&ub->work);
433}
434
435struct tipc_media udp_media_info = {
436 .send_msg = tipc_udp_send_msg,
437 .enable_media = tipc_udp_enable,
438 .disable_media = tipc_udp_disable,
439 .addr2str = tipc_udp_addr2str,
440 .addr2msg = tipc_udp_addr2msg,
441 .msg2addr = tipc_udp_msg2addr,
442 .priority = TIPC_DEF_LINK_PRI,
443 .tolerance = TIPC_DEF_LINK_TOL,
444 .window = TIPC_DEF_LINK_WIN,
445 .type_id = TIPC_MEDIA_TYPE_UDP,
446 .hwaddr_len = 0,
447 .name = "udp"
448};
diff --git a/net/unix/af_unix.c b/net/unix/af_unix.c
index 526b6edab018..433f287ee548 100644
--- a/net/unix/af_unix.c
+++ b/net/unix/af_unix.c
@@ -516,20 +516,15 @@ static unsigned int unix_dgram_poll(struct file *, struct socket *,
516 poll_table *); 516 poll_table *);
517static int unix_ioctl(struct socket *, unsigned int, unsigned long); 517static int unix_ioctl(struct socket *, unsigned int, unsigned long);
518static int unix_shutdown(struct socket *, int); 518static int unix_shutdown(struct socket *, int);
519static int unix_stream_sendmsg(struct kiocb *, struct socket *, 519static int unix_stream_sendmsg(struct socket *, struct msghdr *, size_t);
520 struct msghdr *, size_t); 520static int unix_stream_recvmsg(struct socket *, struct msghdr *, size_t, int);
521static int unix_stream_recvmsg(struct kiocb *, struct socket *, 521static int unix_dgram_sendmsg(struct socket *, struct msghdr *, size_t);
522 struct msghdr *, size_t, int); 522static int unix_dgram_recvmsg(struct socket *, struct msghdr *, size_t, int);
523static int unix_dgram_sendmsg(struct kiocb *, struct socket *,
524 struct msghdr *, size_t);
525static int unix_dgram_recvmsg(struct kiocb *, struct socket *,
526 struct msghdr *, size_t, int);
527static int unix_dgram_connect(struct socket *, struct sockaddr *, 523static int unix_dgram_connect(struct socket *, struct sockaddr *,
528 int, int); 524 int, int);
529static int unix_seqpacket_sendmsg(struct kiocb *, struct socket *, 525static int unix_seqpacket_sendmsg(struct socket *, struct msghdr *, size_t);
530 struct msghdr *, size_t); 526static int unix_seqpacket_recvmsg(struct socket *, struct msghdr *, size_t,
531static int unix_seqpacket_recvmsg(struct kiocb *, struct socket *, 527 int);
532 struct msghdr *, size_t, int);
533 528
534static int unix_set_peek_off(struct sock *sk, int val) 529static int unix_set_peek_off(struct sock *sk, int val)
535{ 530{
@@ -1442,8 +1437,8 @@ static void maybe_add_creds(struct sk_buff *skb, const struct socket *sock,
1442 * Send AF_UNIX data. 1437 * Send AF_UNIX data.
1443 */ 1438 */
1444 1439
1445static int unix_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, 1440static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
1446 struct msghdr *msg, size_t len) 1441 size_t len)
1447{ 1442{
1448 struct sock *sk = sock->sk; 1443 struct sock *sk = sock->sk;
1449 struct net *net = sock_net(sk); 1444 struct net *net = sock_net(sk);
@@ -1622,8 +1617,8 @@ out:
1622 */ 1617 */
1623#define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768)) 1618#define UNIX_SKB_FRAGS_SZ (PAGE_SIZE << get_order(32768))
1624 1619
1625static int unix_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, 1620static int unix_stream_sendmsg(struct socket *sock, struct msghdr *msg,
1626 struct msghdr *msg, size_t len) 1621 size_t len)
1627{ 1622{
1628 struct sock *sk = sock->sk; 1623 struct sock *sk = sock->sk;
1629 struct sock *other = NULL; 1624 struct sock *other = NULL;
@@ -1725,8 +1720,8 @@ out_err:
1725 return sent ? : err; 1720 return sent ? : err;
1726} 1721}
1727 1722
1728static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock, 1723static int unix_seqpacket_sendmsg(struct socket *sock, struct msghdr *msg,
1729 struct msghdr *msg, size_t len) 1724 size_t len)
1730{ 1725{
1731 int err; 1726 int err;
1732 struct sock *sk = sock->sk; 1727 struct sock *sk = sock->sk;
@@ -1741,19 +1736,18 @@ static int unix_seqpacket_sendmsg(struct kiocb *kiocb, struct socket *sock,
1741 if (msg->msg_namelen) 1736 if (msg->msg_namelen)
1742 msg->msg_namelen = 0; 1737 msg->msg_namelen = 0;
1743 1738
1744 return unix_dgram_sendmsg(kiocb, sock, msg, len); 1739 return unix_dgram_sendmsg(sock, msg, len);
1745} 1740}
1746 1741
1747static int unix_seqpacket_recvmsg(struct kiocb *iocb, struct socket *sock, 1742static int unix_seqpacket_recvmsg(struct socket *sock, struct msghdr *msg,
1748 struct msghdr *msg, size_t size, 1743 size_t size, int flags)
1749 int flags)
1750{ 1744{
1751 struct sock *sk = sock->sk; 1745 struct sock *sk = sock->sk;
1752 1746
1753 if (sk->sk_state != TCP_ESTABLISHED) 1747 if (sk->sk_state != TCP_ESTABLISHED)
1754 return -ENOTCONN; 1748 return -ENOTCONN;
1755 1749
1756 return unix_dgram_recvmsg(iocb, sock, msg, size, flags); 1750 return unix_dgram_recvmsg(sock, msg, size, flags);
1757} 1751}
1758 1752
1759static void unix_copy_addr(struct msghdr *msg, struct sock *sk) 1753static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
@@ -1766,9 +1760,8 @@ static void unix_copy_addr(struct msghdr *msg, struct sock *sk)
1766 } 1760 }
1767} 1761}
1768 1762
1769static int unix_dgram_recvmsg(struct kiocb *iocb, struct socket *sock, 1763static int unix_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
1770 struct msghdr *msg, size_t size, 1764 size_t size, int flags)
1771 int flags)
1772{ 1765{
1773 struct scm_cookie scm; 1766 struct scm_cookie scm;
1774 struct sock *sk = sock->sk; 1767 struct sock *sk = sock->sk;
@@ -1900,9 +1893,8 @@ static unsigned int unix_skb_len(const struct sk_buff *skb)
1900 return skb->len - UNIXCB(skb).consumed; 1893 return skb->len - UNIXCB(skb).consumed;
1901} 1894}
1902 1895
1903static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock, 1896static int unix_stream_recvmsg(struct socket *sock, struct msghdr *msg,
1904 struct msghdr *msg, size_t size, 1897 size_t size, int flags)
1905 int flags)
1906{ 1898{
1907 struct scm_cookie scm; 1899 struct scm_cookie scm;
1908 struct sock *sk = sock->sk; 1900 struct sock *sk = sock->sk;
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 1d0e39c9a3e2..2ec86e652a19 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -949,8 +949,8 @@ static unsigned int vsock_poll(struct file *file, struct socket *sock,
949 return mask; 949 return mask;
950} 950}
951 951
952static int vsock_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock, 952static int vsock_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
953 struct msghdr *msg, size_t len) 953 size_t len)
954{ 954{
955 int err; 955 int err;
956 struct sock *sk; 956 struct sock *sk;
@@ -1062,11 +1062,10 @@ out:
1062 return err; 1062 return err;
1063} 1063}
1064 1064
1065static int vsock_dgram_recvmsg(struct kiocb *kiocb, struct socket *sock, 1065static int vsock_dgram_recvmsg(struct socket *sock, struct msghdr *msg,
1066 struct msghdr *msg, size_t len, int flags) 1066 size_t len, int flags)
1067{ 1067{
1068 return transport->dgram_dequeue(kiocb, vsock_sk(sock->sk), msg, len, 1068 return transport->dgram_dequeue(vsock_sk(sock->sk), msg, len, flags);
1069 flags);
1070} 1069}
1071 1070
1072static const struct proto_ops vsock_dgram_ops = { 1071static const struct proto_ops vsock_dgram_ops = {
@@ -1505,8 +1504,8 @@ static int vsock_stream_getsockopt(struct socket *sock,
1505 return 0; 1504 return 0;
1506} 1505}
1507 1506
1508static int vsock_stream_sendmsg(struct kiocb *kiocb, struct socket *sock, 1507static int vsock_stream_sendmsg(struct socket *sock, struct msghdr *msg,
1509 struct msghdr *msg, size_t len) 1508 size_t len)
1510{ 1509{
1511 struct sock *sk; 1510 struct sock *sk;
1512 struct vsock_sock *vsk; 1511 struct vsock_sock *vsk;
@@ -1644,9 +1643,8 @@ out:
1644 1643
1645 1644
1646static int 1645static int
1647vsock_stream_recvmsg(struct kiocb *kiocb, 1646vsock_stream_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
1648 struct socket *sock, 1647 int flags)
1649 struct msghdr *msg, size_t len, int flags)
1650{ 1648{
1651 struct sock *sk; 1649 struct sock *sk;
1652 struct vsock_sock *vsk; 1650 struct vsock_sock *vsk;
diff --git a/net/vmw_vsock/vmci_transport.c b/net/vmw_vsock/vmci_transport.c
index 7f3255084a6c..c294da095461 100644
--- a/net/vmw_vsock/vmci_transport.c
+++ b/net/vmw_vsock/vmci_transport.c
@@ -1730,8 +1730,7 @@ static int vmci_transport_dgram_enqueue(
1730 return err - sizeof(*dg); 1730 return err - sizeof(*dg);
1731} 1731}
1732 1732
1733static int vmci_transport_dgram_dequeue(struct kiocb *kiocb, 1733static int vmci_transport_dgram_dequeue(struct vsock_sock *vsk,
1734 struct vsock_sock *vsk,
1735 struct msghdr *msg, size_t len, 1734 struct msghdr *msg, size_t len,
1736 int flags) 1735 int flags)
1737{ 1736{
diff --git a/net/wireless/Kconfig b/net/wireless/Kconfig
index 29c8675f9a11..4f5543dd2524 100644
--- a/net/wireless/Kconfig
+++ b/net/wireless/Kconfig
@@ -175,13 +175,21 @@ config CFG80211_INTERNAL_REGDB
175 Most distributions have a CRDA package. So if unsure, say N. 175 Most distributions have a CRDA package. So if unsure, say N.
176 176
177config CFG80211_WEXT 177config CFG80211_WEXT
178 bool "cfg80211 wireless extensions compatibility" 178 bool "cfg80211 wireless extensions compatibility" if !CFG80211_WEXT_EXPORT
179 depends on CFG80211 179 depends on CFG80211
180 select WEXT_CORE 180 select WEXT_CORE
181 default y if CFG80211_WEXT_EXPORT
181 help 182 help
182 Enable this option if you need old userspace for wireless 183 Enable this option if you need old userspace for wireless
183 extensions with cfg80211-based drivers. 184 extensions with cfg80211-based drivers.
184 185
186config CFG80211_WEXT_EXPORT
187 bool
188 depends on CFG80211
189 help
190 Drivers should select this option if they require cfg80211's
191 wext compatibility symbols to be exported.
192
185config LIB80211 193config LIB80211
186 tristate 194 tristate
187 default n 195 default n
diff --git a/net/wireless/ibss.c b/net/wireless/ibss.c
index e24fc585c883..4c55fab9b4e4 100644
--- a/net/wireless/ibss.c
+++ b/net/wireless/ibss.c
@@ -30,7 +30,7 @@ void __cfg80211_ibss_joined(struct net_device *dev, const u8 *bssid,
30 return; 30 return;
31 31
32 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0, 32 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, NULL, 0,
33 WLAN_CAPABILITY_IBSS, WLAN_CAPABILITY_IBSS); 33 IEEE80211_BSS_TYPE_IBSS, IEEE80211_PRIVACY_ANY);
34 34
35 if (WARN_ON(!bss)) 35 if (WARN_ON(!bss))
36 return; 36 return;
@@ -533,7 +533,7 @@ int cfg80211_ibss_wext_giwap(struct net_device *dev,
533 else if (wdev->wext.ibss.bssid) 533 else if (wdev->wext.ibss.bssid)
534 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN); 534 memcpy(ap_addr->sa_data, wdev->wext.ibss.bssid, ETH_ALEN);
535 else 535 else
536 memset(ap_addr->sa_data, 0, ETH_ALEN); 536 eth_zero_addr(ap_addr->sa_data);
537 537
538 wdev_unlock(wdev); 538 wdev_unlock(wdev);
539 539
diff --git a/net/wireless/mlme.c b/net/wireless/mlme.c
index 2c52b59e43f3..7aae329e2b4e 100644
--- a/net/wireless/mlme.c
+++ b/net/wireless/mlme.c
@@ -229,7 +229,8 @@ int cfg80211_mlme_auth(struct cfg80211_registered_device *rdev,
229 return -EALREADY; 229 return -EALREADY;
230 230
231 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len, 231 req.bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
232 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS); 232 IEEE80211_BSS_TYPE_ESS,
233 IEEE80211_PRIVACY_ANY);
233 if (!req.bss) 234 if (!req.bss)
234 return -ENOENT; 235 return -ENOENT;
235 236
@@ -296,7 +297,8 @@ int cfg80211_mlme_assoc(struct cfg80211_registered_device *rdev,
296 rdev->wiphy.vht_capa_mod_mask); 297 rdev->wiphy.vht_capa_mod_mask);
297 298
298 req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len, 299 req->bss = cfg80211_get_bss(&rdev->wiphy, chan, bssid, ssid, ssid_len,
299 WLAN_CAPABILITY_ESS, WLAN_CAPABILITY_ESS); 300 IEEE80211_BSS_TYPE_ESS,
301 IEEE80211_PRIVACY_ANY);
300 if (!req->bss) 302 if (!req->bss)
301 return -ENOENT; 303 return -ENOENT;
302 304
diff --git a/net/wireless/nl80211.c b/net/wireless/nl80211.c
index b6f84f6a2a09..dd78445c7d50 100644
--- a/net/wireless/nl80211.c
+++ b/net/wireless/nl80211.c
@@ -399,6 +399,7 @@ static const struct nla_policy nl80211_policy[NUM_NL80211_ATTR] = {
399 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG }, 399 [NL80211_ATTR_WIPHY_SELF_MANAGED_REG] = { .type = NLA_FLAG },
400 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 }, 400 [NL80211_ATTR_NETNS_FD] = { .type = NLA_U32 },
401 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 }, 401 [NL80211_ATTR_SCHED_SCAN_DELAY] = { .type = NLA_U32 },
402 [NL80211_ATTR_REG_INDOOR] = { .type = NLA_FLAG },
402}; 403};
403 404
404/* policy for the key attributes */ 405/* policy for the key attributes */
@@ -1098,8 +1099,6 @@ static int nl80211_send_wowlan(struct sk_buff *msg,
1098 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg)) 1099 if (large && nl80211_send_wowlan_tcp_caps(rdev, msg))
1099 return -ENOBUFS; 1100 return -ENOBUFS;
1100 1101
1101 /* TODO: send wowlan net detect */
1102
1103 nla_nest_end(msg, nl_wowlan); 1102 nla_nest_end(msg, nl_wowlan);
1104 1103
1105 return 0; 1104 return 0;
@@ -2668,7 +2667,8 @@ static int nl80211_new_interface(struct sk_buff *skb, struct genl_info *info)
2668 2667
2669 wdev = rdev_add_virtual_intf(rdev, 2668 wdev = rdev_add_virtual_intf(rdev,
2670 nla_data(info->attrs[NL80211_ATTR_IFNAME]), 2669 nla_data(info->attrs[NL80211_ATTR_IFNAME]),
2671 type, err ? NULL : &flags, &params); 2670 NET_NAME_USER, type, err ? NULL : &flags,
2671 &params);
2672 if (WARN_ON(!wdev)) { 2672 if (WARN_ON(!wdev)) {
2673 nlmsg_free(msg); 2673 nlmsg_free(msg);
2674 return -EPROTO; 2674 return -EPROTO;
@@ -4968,7 +4968,10 @@ static int parse_reg_rule(struct nlattr *tb[],
4968static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info) 4968static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4969{ 4969{
4970 char *data = NULL; 4970 char *data = NULL;
4971 bool is_indoor;
4971 enum nl80211_user_reg_hint_type user_reg_hint_type; 4972 enum nl80211_user_reg_hint_type user_reg_hint_type;
4973 u32 owner_nlportid;
4974
4972 4975
4973 /* 4976 /*
4974 * You should only get this when cfg80211 hasn't yet initialized 4977 * You should only get this when cfg80211 hasn't yet initialized
@@ -4994,7 +4997,15 @@ static int nl80211_req_set_reg(struct sk_buff *skb, struct genl_info *info)
4994 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]); 4997 data = nla_data(info->attrs[NL80211_ATTR_REG_ALPHA2]);
4995 return regulatory_hint_user(data, user_reg_hint_type); 4998 return regulatory_hint_user(data, user_reg_hint_type);
4996 case NL80211_USER_REG_HINT_INDOOR: 4999 case NL80211_USER_REG_HINT_INDOOR:
4997 return regulatory_hint_indoor_user(); 5000 if (info->attrs[NL80211_ATTR_SOCKET_OWNER]) {
5001 owner_nlportid = info->snd_portid;
5002 is_indoor = !!info->attrs[NL80211_ATTR_REG_INDOOR];
5003 } else {
5004 owner_nlportid = 0;
5005 is_indoor = true;
5006 }
5007
5008 return regulatory_hint_indoor(is_indoor, owner_nlportid);
4998 default: 5009 default:
4999 return -EINVAL; 5010 return -EINVAL;
5000 } 5011 }
@@ -5275,7 +5286,7 @@ do { \
5275 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration, 5286 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, dot11MeshAwakeWindowDuration,
5276 0, 65535, mask, 5287 0, 65535, mask,
5277 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16); 5288 NL80211_MESHCONF_AWAKE_WINDOW, nla_get_u16);
5278 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 1, 0xffffffff, 5289 FILL_IN_MESH_PARAM_IF_SET(tb, cfg, plink_timeout, 0, 0xffffffff,
5279 mask, NL80211_MESHCONF_PLINK_TIMEOUT, 5290 mask, NL80211_MESHCONF_PLINK_TIMEOUT,
5280 nla_get_u32); 5291 nla_get_u32);
5281 if (mask_out) 5292 if (mask_out)
@@ -5653,7 +5664,7 @@ static int nl80211_set_reg(struct sk_buff *skb, struct genl_info *info)
5653 } 5664 }
5654 } 5665 }
5655 5666
5656 r = set_regdom(rd); 5667 r = set_regdom(rd, REGD_SOURCE_CRDA);
5657 /* set_regdom took ownership */ 5668 /* set_regdom took ownership */
5658 rd = NULL; 5669 rd = NULL;
5659 5670
@@ -5693,8 +5704,8 @@ static int nl80211_parse_random_mac(struct nlattr **attrs,
5693 int i; 5704 int i;
5694 5705
5695 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) { 5706 if (!attrs[NL80211_ATTR_MAC] && !attrs[NL80211_ATTR_MAC_MASK]) {
5696 memset(mac_addr, 0, ETH_ALEN); 5707 eth_zero_addr(mac_addr);
5697 memset(mac_addr_mask, 0, ETH_ALEN); 5708 eth_zero_addr(mac_addr_mask);
5698 mac_addr[0] = 0x2; 5709 mac_addr[0] = 0x2;
5699 mac_addr_mask[0] = 0x3; 5710 mac_addr_mask[0] = 0x3;
5700 5711
@@ -7275,8 +7286,18 @@ static int nl80211_join_ibss(struct sk_buff *skb, struct genl_info *info)
7275 break; 7286 break;
7276 case NL80211_CHAN_WIDTH_20: 7287 case NL80211_CHAN_WIDTH_20:
7277 case NL80211_CHAN_WIDTH_40: 7288 case NL80211_CHAN_WIDTH_40:
7278 if (rdev->wiphy.features & NL80211_FEATURE_HT_IBSS) 7289 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7279 break; 7290 return -EINVAL;
7291 break;
7292 case NL80211_CHAN_WIDTH_80:
7293 case NL80211_CHAN_WIDTH_80P80:
7294 case NL80211_CHAN_WIDTH_160:
7295 if (!(rdev->wiphy.features & NL80211_FEATURE_HT_IBSS))
7296 return -EINVAL;
7297 if (!wiphy_ext_feature_isset(&rdev->wiphy,
7298 NL80211_EXT_FEATURE_VHT_IBSS))
7299 return -EINVAL;
7300 break;
7280 default: 7301 default:
7281 return -EINVAL; 7302 return -EINVAL;
7282 } 7303 }
@@ -7389,8 +7410,8 @@ static int nl80211_set_mcast_rate(struct sk_buff *skb, struct genl_info *info)
7389 7410
7390static struct sk_buff * 7411static struct sk_buff *
7391__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev, 7412__cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7392 int approxlen, u32 portid, u32 seq, 7413 struct wireless_dev *wdev, int approxlen,
7393 enum nl80211_commands cmd, 7414 u32 portid, u32 seq, enum nl80211_commands cmd,
7394 enum nl80211_attrs attr, 7415 enum nl80211_attrs attr,
7395 const struct nl80211_vendor_cmd_info *info, 7416 const struct nl80211_vendor_cmd_info *info,
7396 gfp_t gfp) 7417 gfp_t gfp)
@@ -7421,6 +7442,16 @@ __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7421 goto nla_put_failure; 7442 goto nla_put_failure;
7422 } 7443 }
7423 7444
7445 if (wdev) {
7446 if (nla_put_u64(skb, NL80211_ATTR_WDEV,
7447 wdev_id(wdev)))
7448 goto nla_put_failure;
7449 if (wdev->netdev &&
7450 nla_put_u32(skb, NL80211_ATTR_IFINDEX,
7451 wdev->netdev->ifindex))
7452 goto nla_put_failure;
7453 }
7454
7424 data = nla_nest_start(skb, attr); 7455 data = nla_nest_start(skb, attr);
7425 7456
7426 ((void **)skb->cb)[0] = rdev; 7457 ((void **)skb->cb)[0] = rdev;
@@ -7435,6 +7466,7 @@ __cfg80211_alloc_vendor_skb(struct cfg80211_registered_device *rdev,
7435} 7466}
7436 7467
7437struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy, 7468struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7469 struct wireless_dev *wdev,
7438 enum nl80211_commands cmd, 7470 enum nl80211_commands cmd,
7439 enum nl80211_attrs attr, 7471 enum nl80211_attrs attr,
7440 int vendor_event_idx, 7472 int vendor_event_idx,
@@ -7460,7 +7492,7 @@ struct sk_buff *__cfg80211_alloc_event_skb(struct wiphy *wiphy,
7460 return NULL; 7492 return NULL;
7461 } 7493 }
7462 7494
7463 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 0, 0, 7495 return __cfg80211_alloc_vendor_skb(rdev, wdev, approxlen, 0, 0,
7464 cmd, attr, info, gfp); 7496 cmd, attr, info, gfp);
7465} 7497}
7466EXPORT_SYMBOL(__cfg80211_alloc_event_skb); 7498EXPORT_SYMBOL(__cfg80211_alloc_event_skb);
@@ -8761,8 +8793,8 @@ static int nl80211_send_wowlan_tcp(struct sk_buff *msg,
8761 if (!nl_tcp) 8793 if (!nl_tcp)
8762 return -ENOBUFS; 8794 return -ENOBUFS;
8763 8795
8764 if (nla_put_be32(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) || 8796 if (nla_put_in_addr(msg, NL80211_WOWLAN_TCP_SRC_IPV4, tcp->src) ||
8765 nla_put_be32(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) || 8797 nla_put_in_addr(msg, NL80211_WOWLAN_TCP_DST_IPV4, tcp->dst) ||
8766 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) || 8798 nla_put(msg, NL80211_WOWLAN_TCP_DST_MAC, ETH_ALEN, tcp->dst_mac) ||
8767 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) || 8799 nla_put_u16(msg, NL80211_WOWLAN_TCP_SRC_PORT, tcp->src_port) ||
8768 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) || 8800 nla_put_u16(msg, NL80211_WOWLAN_TCP_DST_PORT, tcp->dst_port) ||
@@ -8808,6 +8840,9 @@ static int nl80211_send_wowlan_nd(struct sk_buff *msg,
8808 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, req->interval)) 8840 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_INTERVAL, req->interval))
8809 return -ENOBUFS; 8841 return -ENOBUFS;
8810 8842
8843 if (nla_put_u32(msg, NL80211_ATTR_SCHED_SCAN_DELAY, req->delay))
8844 return -ENOBUFS;
8845
8811 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES); 8846 freqs = nla_nest_start(msg, NL80211_ATTR_SCAN_FREQUENCIES);
8812 if (!freqs) 8847 if (!freqs)
8813 return -ENOBUFS; 8848 return -ENOBUFS;
@@ -8993,8 +9028,8 @@ static int nl80211_parse_wowlan_tcp(struct cfg80211_registered_device *rdev,
8993 cfg = kzalloc(size, GFP_KERNEL); 9028 cfg = kzalloc(size, GFP_KERNEL);
8994 if (!cfg) 9029 if (!cfg)
8995 return -ENOMEM; 9030 return -ENOMEM;
8996 cfg->src = nla_get_be32(tb[NL80211_WOWLAN_TCP_SRC_IPV4]); 9031 cfg->src = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_SRC_IPV4]);
8997 cfg->dst = nla_get_be32(tb[NL80211_WOWLAN_TCP_DST_IPV4]); 9032 cfg->dst = nla_get_in_addr(tb[NL80211_WOWLAN_TCP_DST_IPV4]);
8998 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]), 9033 memcpy(cfg->dst_mac, nla_data(tb[NL80211_WOWLAN_TCP_DST_MAC]),
8999 ETH_ALEN); 9034 ETH_ALEN);
9000 if (tb[NL80211_WOWLAN_TCP_SRC_PORT]) 9035 if (tb[NL80211_WOWLAN_TCP_SRC_PORT])
@@ -9094,6 +9129,7 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9094 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan; 9129 const struct wiphy_wowlan_support *wowlan = rdev->wiphy.wowlan;
9095 int err, i; 9130 int err, i;
9096 bool prev_enabled = rdev->wiphy.wowlan_config; 9131 bool prev_enabled = rdev->wiphy.wowlan_config;
9132 bool regular = false;
9097 9133
9098 if (!wowlan) 9134 if (!wowlan)
9099 return -EOPNOTSUPP; 9135 return -EOPNOTSUPP;
@@ -9121,12 +9157,14 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9121 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT)) 9157 if (!(wowlan->flags & WIPHY_WOWLAN_DISCONNECT))
9122 return -EINVAL; 9158 return -EINVAL;
9123 new_triggers.disconnect = true; 9159 new_triggers.disconnect = true;
9160 regular = true;
9124 } 9161 }
9125 9162
9126 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) { 9163 if (tb[NL80211_WOWLAN_TRIG_MAGIC_PKT]) {
9127 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT)) 9164 if (!(wowlan->flags & WIPHY_WOWLAN_MAGIC_PKT))
9128 return -EINVAL; 9165 return -EINVAL;
9129 new_triggers.magic_pkt = true; 9166 new_triggers.magic_pkt = true;
9167 regular = true;
9130 } 9168 }
9131 9169
9132 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED]) 9170 if (tb[NL80211_WOWLAN_TRIG_GTK_REKEY_SUPPORTED])
@@ -9136,24 +9174,28 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9136 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE)) 9174 if (!(wowlan->flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE))
9137 return -EINVAL; 9175 return -EINVAL;
9138 new_triggers.gtk_rekey_failure = true; 9176 new_triggers.gtk_rekey_failure = true;
9177 regular = true;
9139 } 9178 }
9140 9179
9141 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) { 9180 if (tb[NL80211_WOWLAN_TRIG_EAP_IDENT_REQUEST]) {
9142 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ)) 9181 if (!(wowlan->flags & WIPHY_WOWLAN_EAP_IDENTITY_REQ))
9143 return -EINVAL; 9182 return -EINVAL;
9144 new_triggers.eap_identity_req = true; 9183 new_triggers.eap_identity_req = true;
9184 regular = true;
9145 } 9185 }
9146 9186
9147 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) { 9187 if (tb[NL80211_WOWLAN_TRIG_4WAY_HANDSHAKE]) {
9148 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE)) 9188 if (!(wowlan->flags & WIPHY_WOWLAN_4WAY_HANDSHAKE))
9149 return -EINVAL; 9189 return -EINVAL;
9150 new_triggers.four_way_handshake = true; 9190 new_triggers.four_way_handshake = true;
9191 regular = true;
9151 } 9192 }
9152 9193
9153 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) { 9194 if (tb[NL80211_WOWLAN_TRIG_RFKILL_RELEASE]) {
9154 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE)) 9195 if (!(wowlan->flags & WIPHY_WOWLAN_RFKILL_RELEASE))
9155 return -EINVAL; 9196 return -EINVAL;
9156 new_triggers.rfkill_release = true; 9197 new_triggers.rfkill_release = true;
9198 regular = true;
9157 } 9199 }
9158 9200
9159 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) { 9201 if (tb[NL80211_WOWLAN_TRIG_PKT_PATTERN]) {
@@ -9162,6 +9204,8 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9162 int rem, pat_len, mask_len, pkt_offset; 9204 int rem, pat_len, mask_len, pkt_offset;
9163 struct nlattr *pat_tb[NUM_NL80211_PKTPAT]; 9205 struct nlattr *pat_tb[NUM_NL80211_PKTPAT];
9164 9206
9207 regular = true;
9208
9165 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN], 9209 nla_for_each_nested(pat, tb[NL80211_WOWLAN_TRIG_PKT_PATTERN],
9166 rem) 9210 rem)
9167 n_patterns++; 9211 n_patterns++;
@@ -9223,6 +9267,7 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9223 } 9267 }
9224 9268
9225 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) { 9269 if (tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION]) {
9270 regular = true;
9226 err = nl80211_parse_wowlan_tcp( 9271 err = nl80211_parse_wowlan_tcp(
9227 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION], 9272 rdev, tb[NL80211_WOWLAN_TRIG_TCP_CONNECTION],
9228 &new_triggers); 9273 &new_triggers);
@@ -9231,6 +9276,7 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9231 } 9276 }
9232 9277
9233 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) { 9278 if (tb[NL80211_WOWLAN_TRIG_NET_DETECT]) {
9279 regular = true;
9234 err = nl80211_parse_wowlan_nd( 9280 err = nl80211_parse_wowlan_nd(
9235 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT], 9281 rdev, wowlan, tb[NL80211_WOWLAN_TRIG_NET_DETECT],
9236 &new_triggers); 9282 &new_triggers);
@@ -9238,6 +9284,17 @@ static int nl80211_set_wowlan(struct sk_buff *skb, struct genl_info *info)
9238 goto error; 9284 goto error;
9239 } 9285 }
9240 9286
9287 /* The 'any' trigger means the device continues operating more or less
9288 * as in its normal operation mode and wakes up the host on most of the
9289 * normal interrupts (like packet RX, ...)
9290 * It therefore makes little sense to combine with the more constrained
9291 * wakeup trigger modes.
9292 */
9293 if (new_triggers.any && regular) {
9294 err = -EINVAL;
9295 goto error;
9296 }
9297
9241 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL); 9298 ntrig = kmemdup(&new_triggers, sizeof(new_triggers), GFP_KERNEL);
9242 if (!ntrig) { 9299 if (!ntrig) {
9243 err = -ENOMEM; 9300 err = -ENOMEM;
@@ -9906,7 +9963,7 @@ struct sk_buff *__cfg80211_alloc_reply_skb(struct wiphy *wiphy,
9906 if (WARN_ON(!rdev->cur_cmd_info)) 9963 if (WARN_ON(!rdev->cur_cmd_info))
9907 return NULL; 9964 return NULL;
9908 9965
9909 return __cfg80211_alloc_vendor_skb(rdev, approxlen, 9966 return __cfg80211_alloc_vendor_skb(rdev, NULL, approxlen,
9910 rdev->cur_cmd_info->snd_portid, 9967 rdev->cur_cmd_info->snd_portid,
9911 rdev->cur_cmd_info->snd_seq, 9968 rdev->cur_cmd_info->snd_seq,
9912 cmd, attr, NULL, GFP_KERNEL); 9969 cmd, attr, NULL, GFP_KERNEL);
@@ -12775,6 +12832,11 @@ static int nl80211_netlink_notify(struct notifier_block * nb,
12775 12832
12776 rcu_read_unlock(); 12833 rcu_read_unlock();
12777 12834
12835 /*
12836 * It is possible that the user space process that is controlling the
12837 * indoor setting disappeared, so notify the regulatory core.
12838 */
12839 regulatory_netlink_notify(notify->portid);
12778 return NOTIFY_OK; 12840 return NOTIFY_OK;
12779} 12841}
12780 12842
diff --git a/net/wireless/rdev-ops.h b/net/wireless/rdev-ops.h
index 35cfb7134bdb..c6e83a7468c0 100644
--- a/net/wireless/rdev-ops.h
+++ b/net/wireless/rdev-ops.h
@@ -35,13 +35,14 @@ static inline void rdev_set_wakeup(struct cfg80211_registered_device *rdev,
35 35
36static inline struct wireless_dev 36static inline struct wireless_dev
37*rdev_add_virtual_intf(struct cfg80211_registered_device *rdev, char *name, 37*rdev_add_virtual_intf(struct cfg80211_registered_device *rdev, char *name,
38 unsigned char name_assign_type,
38 enum nl80211_iftype type, u32 *flags, 39 enum nl80211_iftype type, u32 *flags,
39 struct vif_params *params) 40 struct vif_params *params)
40{ 41{
41 struct wireless_dev *ret; 42 struct wireless_dev *ret;
42 trace_rdev_add_virtual_intf(&rdev->wiphy, name, type); 43 trace_rdev_add_virtual_intf(&rdev->wiphy, name, type);
43 ret = rdev->ops->add_virtual_intf(&rdev->wiphy, name, type, flags, 44 ret = rdev->ops->add_virtual_intf(&rdev->wiphy, name, name_assign_type,
44 params); 45 type, flags, params);
45 trace_rdev_return_wdev(&rdev->wiphy, ret); 46 trace_rdev_return_wdev(&rdev->wiphy, ret);
46 return ret; 47 return ret;
47} 48}
diff --git a/net/wireless/reg.c b/net/wireless/reg.c
index 48dfc7b4e981..0e347f888fe9 100644
--- a/net/wireless/reg.c
+++ b/net/wireless/reg.c
@@ -82,17 +82,12 @@
82 * be intersected with the current one. 82 * be intersected with the current one.
83 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current 83 * @REG_REQ_ALREADY_SET: the regulatory request will not change the current
84 * regulatory settings, and no further processing is required. 84 * regulatory settings, and no further processing is required.
85 * @REG_REQ_USER_HINT_HANDLED: a non alpha2 user hint was handled and no
86 * further processing is required, i.e., not need to update last_request
87 * etc. This should be used for user hints that do not provide an alpha2
88 * but some other type of regulatory hint, i.e., indoor operation.
89 */ 85 */
90enum reg_request_treatment { 86enum reg_request_treatment {
91 REG_REQ_OK, 87 REG_REQ_OK,
92 REG_REQ_IGNORE, 88 REG_REQ_IGNORE,
93 REG_REQ_INTERSECT, 89 REG_REQ_INTERSECT,
94 REG_REQ_ALREADY_SET, 90 REG_REQ_ALREADY_SET,
95 REG_REQ_USER_HINT_HANDLED,
96}; 91};
97 92
98static struct regulatory_request core_request_world = { 93static struct regulatory_request core_request_world = {
@@ -133,9 +128,17 @@ static int reg_num_devs_support_basehint;
133 * State variable indicating if the platform on which the devices 128 * State variable indicating if the platform on which the devices
134 * are attached is operating in an indoor environment. The state variable 129 * are attached is operating in an indoor environment. The state variable
135 * is relevant for all registered devices. 130 * is relevant for all registered devices.
136 * (protected by RTNL)
137 */ 131 */
138static bool reg_is_indoor; 132static bool reg_is_indoor;
133static spinlock_t reg_indoor_lock;
134
135/* Used to track the userspace process controlling the indoor setting */
136static u32 reg_is_indoor_portid;
137
138/* Max number of consecutive attempts to communicate with CRDA */
139#define REG_MAX_CRDA_TIMEOUTS 10
140
141static u32 reg_crda_timeouts;
139 142
140static const struct ieee80211_regdomain *get_cfg80211_regdom(void) 143static const struct ieee80211_regdomain *get_cfg80211_regdom(void)
141{ 144{
@@ -487,7 +490,7 @@ static void reg_regdb_search(struct work_struct *work)
487 mutex_unlock(&reg_regdb_search_mutex); 490 mutex_unlock(&reg_regdb_search_mutex);
488 491
489 if (!IS_ERR_OR_NULL(regdom)) 492 if (!IS_ERR_OR_NULL(regdom))
490 set_regdom(regdom); 493 set_regdom(regdom, REGD_SOURCE_INTERNAL_DB);
491 494
492 rtnl_unlock(); 495 rtnl_unlock();
493} 496}
@@ -537,15 +540,20 @@ static int call_crda(const char *alpha2)
537 snprintf(country, sizeof(country), "COUNTRY=%c%c", 540 snprintf(country, sizeof(country), "COUNTRY=%c%c",
538 alpha2[0], alpha2[1]); 541 alpha2[0], alpha2[1]);
539 542
543 /* query internal regulatory database (if it exists) */
544 reg_regdb_query(alpha2);
545
546 if (reg_crda_timeouts > REG_MAX_CRDA_TIMEOUTS) {
547 pr_info("Exceeded CRDA call max attempts. Not calling CRDA\n");
548 return -EINVAL;
549 }
550
540 if (!is_world_regdom((char *) alpha2)) 551 if (!is_world_regdom((char *) alpha2))
541 pr_info("Calling CRDA for country: %c%c\n", 552 pr_info("Calling CRDA for country: %c%c\n",
542 alpha2[0], alpha2[1]); 553 alpha2[0], alpha2[1]);
543 else 554 else
544 pr_info("Calling CRDA to update world regulatory domain\n"); 555 pr_info("Calling CRDA to update world regulatory domain\n");
545 556
546 /* query internal regulatory database (if it exists) */
547 reg_regdb_query(alpha2);
548
549 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env); 557 return kobject_uevent_env(&reg_pdev->dev.kobj, KOBJ_CHANGE, env);
550} 558}
551 559
@@ -554,6 +562,9 @@ reg_call_crda(struct regulatory_request *request)
554{ 562{
555 if (call_crda(request->alpha2)) 563 if (call_crda(request->alpha2))
556 return REG_REQ_IGNORE; 564 return REG_REQ_IGNORE;
565
566 queue_delayed_work(system_power_efficient_wq,
567 &reg_timeout, msecs_to_jiffies(3142));
557 return REG_REQ_OK; 568 return REG_REQ_OK;
558} 569}
559 570
@@ -1248,13 +1259,6 @@ static bool reg_request_cell_base(struct regulatory_request *request)
1248 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE; 1259 return request->user_reg_hint_type == NL80211_USER_REG_HINT_CELL_BASE;
1249} 1260}
1250 1261
1251static bool reg_request_indoor(struct regulatory_request *request)
1252{
1253 if (request->initiator != NL80211_REGDOM_SET_BY_USER)
1254 return false;
1255 return request->user_reg_hint_type == NL80211_USER_REG_HINT_INDOOR;
1256}
1257
1258bool reg_last_request_cell_base(void) 1262bool reg_last_request_cell_base(void)
1259{ 1263{
1260 return reg_request_cell_base(get_last_request()); 1264 return reg_request_cell_base(get_last_request());
@@ -1800,8 +1804,7 @@ static void reg_set_request_processed(void)
1800 need_more_processing = true; 1804 need_more_processing = true;
1801 spin_unlock(&reg_requests_lock); 1805 spin_unlock(&reg_requests_lock);
1802 1806
1803 if (lr->initiator == NL80211_REGDOM_SET_BY_USER) 1807 cancel_delayed_work(&reg_timeout);
1804 cancel_delayed_work(&reg_timeout);
1805 1808
1806 if (need_more_processing) 1809 if (need_more_processing)
1807 schedule_work(&reg_work); 1810 schedule_work(&reg_work);
@@ -1833,11 +1836,6 @@ __reg_process_hint_user(struct regulatory_request *user_request)
1833{ 1836{
1834 struct regulatory_request *lr = get_last_request(); 1837 struct regulatory_request *lr = get_last_request();
1835 1838
1836 if (reg_request_indoor(user_request)) {
1837 reg_is_indoor = true;
1838 return REG_REQ_USER_HINT_HANDLED;
1839 }
1840
1841 if (reg_request_cell_base(user_request)) 1839 if (reg_request_cell_base(user_request))
1842 return reg_ignore_cell_hint(user_request); 1840 return reg_ignore_cell_hint(user_request);
1843 1841
@@ -1885,8 +1883,7 @@ reg_process_hint_user(struct regulatory_request *user_request)
1885 1883
1886 treatment = __reg_process_hint_user(user_request); 1884 treatment = __reg_process_hint_user(user_request);
1887 if (treatment == REG_REQ_IGNORE || 1885 if (treatment == REG_REQ_IGNORE ||
1888 treatment == REG_REQ_ALREADY_SET || 1886 treatment == REG_REQ_ALREADY_SET) {
1889 treatment == REG_REQ_USER_HINT_HANDLED) {
1890 reg_free_request(user_request); 1887 reg_free_request(user_request);
1891 return treatment; 1888 return treatment;
1892 } 1889 }
@@ -1947,7 +1944,6 @@ reg_process_hint_driver(struct wiphy *wiphy,
1947 case REG_REQ_OK: 1944 case REG_REQ_OK:
1948 break; 1945 break;
1949 case REG_REQ_IGNORE: 1946 case REG_REQ_IGNORE:
1950 case REG_REQ_USER_HINT_HANDLED:
1951 reg_free_request(driver_request); 1947 reg_free_request(driver_request);
1952 return treatment; 1948 return treatment;
1953 case REG_REQ_INTERSECT: 1949 case REG_REQ_INTERSECT:
@@ -2047,7 +2043,6 @@ reg_process_hint_country_ie(struct wiphy *wiphy,
2047 case REG_REQ_OK: 2043 case REG_REQ_OK:
2048 break; 2044 break;
2049 case REG_REQ_IGNORE: 2045 case REG_REQ_IGNORE:
2050 case REG_REQ_USER_HINT_HANDLED:
2051 /* fall through */ 2046 /* fall through */
2052 case REG_REQ_ALREADY_SET: 2047 case REG_REQ_ALREADY_SET:
2053 reg_free_request(country_ie_request); 2048 reg_free_request(country_ie_request);
@@ -2086,11 +2081,8 @@ static void reg_process_hint(struct regulatory_request *reg_request)
2086 case NL80211_REGDOM_SET_BY_USER: 2081 case NL80211_REGDOM_SET_BY_USER:
2087 treatment = reg_process_hint_user(reg_request); 2082 treatment = reg_process_hint_user(reg_request);
2088 if (treatment == REG_REQ_IGNORE || 2083 if (treatment == REG_REQ_IGNORE ||
2089 treatment == REG_REQ_ALREADY_SET || 2084 treatment == REG_REQ_ALREADY_SET)
2090 treatment == REG_REQ_USER_HINT_HANDLED)
2091 return; 2085 return;
2092 queue_delayed_work(system_power_efficient_wq,
2093 &reg_timeout, msecs_to_jiffies(3142));
2094 return; 2086 return;
2095 case NL80211_REGDOM_SET_BY_DRIVER: 2087 case NL80211_REGDOM_SET_BY_DRIVER:
2096 if (!wiphy) 2088 if (!wiphy)
@@ -2177,6 +2169,13 @@ static void reg_process_pending_hints(void)
2177 } 2169 }
2178 2170
2179 reg_process_hint(reg_request); 2171 reg_process_hint(reg_request);
2172
2173 lr = get_last_request();
2174
2175 spin_lock(&reg_requests_lock);
2176 if (!list_empty(&reg_requests_list) && lr && lr->processed)
2177 schedule_work(&reg_work);
2178 spin_unlock(&reg_requests_lock);
2180} 2179}
2181 2180
2182/* Processes beacon hints -- this has nothing to do with country IEs */ 2181/* Processes beacon hints -- this has nothing to do with country IEs */
@@ -2304,27 +2303,58 @@ int regulatory_hint_user(const char *alpha2,
2304 request->initiator = NL80211_REGDOM_SET_BY_USER; 2303 request->initiator = NL80211_REGDOM_SET_BY_USER;
2305 request->user_reg_hint_type = user_reg_hint_type; 2304 request->user_reg_hint_type = user_reg_hint_type;
2306 2305
2306 /* Allow calling CRDA again */
2307 reg_crda_timeouts = 0;
2308
2307 queue_regulatory_request(request); 2309 queue_regulatory_request(request);
2308 2310
2309 return 0; 2311 return 0;
2310} 2312}
2311 2313
2312int regulatory_hint_indoor_user(void) 2314int regulatory_hint_indoor(bool is_indoor, u32 portid)
2313{ 2315{
2314 struct regulatory_request *request; 2316 spin_lock(&reg_indoor_lock);
2315 2317
2316 request = kzalloc(sizeof(struct regulatory_request), GFP_KERNEL); 2318 /* It is possible that more than one user space process is trying to
2317 if (!request) 2319 * configure the indoor setting. To handle such cases, clear the indoor
2318 return -ENOMEM; 2320 * setting in case that some process does not think that the device
2321 * is operating in an indoor environment. In addition, if a user space
2322 * process indicates that it is controlling the indoor setting, save its
2323 * portid, i.e., make it the owner.
2324 */
2325 reg_is_indoor = is_indoor;
2326 if (reg_is_indoor) {
2327 if (!reg_is_indoor_portid)
2328 reg_is_indoor_portid = portid;
2329 } else {
2330 reg_is_indoor_portid = 0;
2331 }
2319 2332
2320 request->wiphy_idx = WIPHY_IDX_INVALID; 2333 spin_unlock(&reg_indoor_lock);
2321 request->initiator = NL80211_REGDOM_SET_BY_USER; 2334
2322 request->user_reg_hint_type = NL80211_USER_REG_HINT_INDOOR; 2335 if (!is_indoor)
2323 queue_regulatory_request(request); 2336 reg_check_channels();
2324 2337
2325 return 0; 2338 return 0;
2326} 2339}
2327 2340
2341void regulatory_netlink_notify(u32 portid)
2342{
2343 spin_lock(&reg_indoor_lock);
2344
2345 if (reg_is_indoor_portid != portid) {
2346 spin_unlock(&reg_indoor_lock);
2347 return;
2348 }
2349
2350 reg_is_indoor = false;
2351 reg_is_indoor_portid = 0;
2352
2353 spin_unlock(&reg_indoor_lock);
2354
2355 reg_check_channels();
2356}
2357
2328/* Driver hints */ 2358/* Driver hints */
2329int regulatory_hint(struct wiphy *wiphy, const char *alpha2) 2359int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2330{ 2360{
@@ -2345,6 +2375,9 @@ int regulatory_hint(struct wiphy *wiphy, const char *alpha2)
2345 request->alpha2[1] = alpha2[1]; 2375 request->alpha2[1] = alpha2[1];
2346 request->initiator = NL80211_REGDOM_SET_BY_DRIVER; 2376 request->initiator = NL80211_REGDOM_SET_BY_DRIVER;
2347 2377
2378 /* Allow calling CRDA again */
2379 reg_crda_timeouts = 0;
2380
2348 queue_regulatory_request(request); 2381 queue_regulatory_request(request);
2349 2382
2350 return 0; 2383 return 0;
@@ -2398,6 +2431,9 @@ void regulatory_hint_country_ie(struct wiphy *wiphy, enum ieee80211_band band,
2398 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE; 2431 request->initiator = NL80211_REGDOM_SET_BY_COUNTRY_IE;
2399 request->country_ie_env = env; 2432 request->country_ie_env = env;
2400 2433
2434 /* Allow calling CRDA again */
2435 reg_crda_timeouts = 0;
2436
2401 queue_regulatory_request(request); 2437 queue_regulatory_request(request);
2402 request = NULL; 2438 request = NULL;
2403out: 2439out:
@@ -2486,13 +2522,22 @@ static void restore_regulatory_settings(bool reset_user)
2486 char alpha2[2]; 2522 char alpha2[2];
2487 char world_alpha2[2]; 2523 char world_alpha2[2];
2488 struct reg_beacon *reg_beacon, *btmp; 2524 struct reg_beacon *reg_beacon, *btmp;
2489 struct regulatory_request *reg_request, *tmp;
2490 LIST_HEAD(tmp_reg_req_list); 2525 LIST_HEAD(tmp_reg_req_list);
2491 struct cfg80211_registered_device *rdev; 2526 struct cfg80211_registered_device *rdev;
2492 2527
2493 ASSERT_RTNL(); 2528 ASSERT_RTNL();
2494 2529
2495 reg_is_indoor = false; 2530 /*
2531 * Clear the indoor setting in case that it is not controlled by user
2532 * space, as otherwise there is no guarantee that the device is still
2533 * operating in an indoor environment.
2534 */
2535 spin_lock(&reg_indoor_lock);
2536 if (reg_is_indoor && !reg_is_indoor_portid) {
2537 reg_is_indoor = false;
2538 reg_check_channels();
2539 }
2540 spin_unlock(&reg_indoor_lock);
2496 2541
2497 reset_regdomains(true, &world_regdom); 2542 reset_regdomains(true, &world_regdom);
2498 restore_alpha2(alpha2, reset_user); 2543 restore_alpha2(alpha2, reset_user);
@@ -2504,11 +2549,7 @@ static void restore_regulatory_settings(bool reset_user)
2504 * settings. 2549 * settings.
2505 */ 2550 */
2506 spin_lock(&reg_requests_lock); 2551 spin_lock(&reg_requests_lock);
2507 list_for_each_entry_safe(reg_request, tmp, &reg_requests_list, list) { 2552 list_splice_tail_init(&reg_requests_list, &tmp_reg_req_list);
2508 if (reg_request->initiator != NL80211_REGDOM_SET_BY_USER)
2509 continue;
2510 list_move_tail(&reg_request->list, &tmp_reg_req_list);
2511 }
2512 spin_unlock(&reg_requests_lock); 2553 spin_unlock(&reg_requests_lock);
2513 2554
2514 /* Clear beacon hints */ 2555 /* Clear beacon hints */
@@ -2871,7 +2912,8 @@ static int reg_set_rd_country_ie(const struct ieee80211_regdomain *rd,
2871 * multiple drivers can be ironed out later. Caller must've already 2912 * multiple drivers can be ironed out later. Caller must've already
2872 * kmalloc'd the rd structure. 2913 * kmalloc'd the rd structure.
2873 */ 2914 */
2874int set_regdom(const struct ieee80211_regdomain *rd) 2915int set_regdom(const struct ieee80211_regdomain *rd,
2916 enum ieee80211_regd_source regd_src)
2875{ 2917{
2876 struct regulatory_request *lr; 2918 struct regulatory_request *lr;
2877 bool user_reset = false; 2919 bool user_reset = false;
@@ -2882,6 +2924,9 @@ int set_regdom(const struct ieee80211_regdomain *rd)
2882 return -EINVAL; 2924 return -EINVAL;
2883 } 2925 }
2884 2926
2927 if (regd_src == REGD_SOURCE_CRDA)
2928 reg_crda_timeouts = 0;
2929
2885 lr = get_last_request(); 2930 lr = get_last_request();
2886 2931
2887 /* Note that this doesn't update the wiphys, this is done below */ 2932 /* Note that this doesn't update the wiphys, this is done below */
@@ -3041,6 +3086,7 @@ static void reg_timeout_work(struct work_struct *work)
3041{ 3086{
3042 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n"); 3087 REG_DBG_PRINT("Timeout while waiting for CRDA to reply, restoring regulatory settings\n");
3043 rtnl_lock(); 3088 rtnl_lock();
3089 reg_crda_timeouts++;
3044 restore_regulatory_settings(true); 3090 restore_regulatory_settings(true);
3045 rtnl_unlock(); 3091 rtnl_unlock();
3046} 3092}
@@ -3089,6 +3135,7 @@ int __init regulatory_init(void)
3089 3135
3090 spin_lock_init(&reg_requests_lock); 3136 spin_lock_init(&reg_requests_lock);
3091 spin_lock_init(&reg_pending_beacons_lock); 3137 spin_lock_init(&reg_pending_beacons_lock);
3138 spin_lock_init(&reg_indoor_lock);
3092 3139
3093 reg_regdb_size_check(); 3140 reg_regdb_size_check();
3094 3141
diff --git a/net/wireless/reg.h b/net/wireless/reg.h
index 4b45d6e61d24..9f495d76eca0 100644
--- a/net/wireless/reg.h
+++ b/net/wireless/reg.h
@@ -16,6 +16,11 @@
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19enum ieee80211_regd_source {
20 REGD_SOURCE_INTERNAL_DB,
21 REGD_SOURCE_CRDA,
22};
23
19extern const struct ieee80211_regdomain __rcu *cfg80211_regdomain; 24extern const struct ieee80211_regdomain __rcu *cfg80211_regdomain;
20 25
21bool reg_is_valid_request(const char *alpha2); 26bool reg_is_valid_request(const char *alpha2);
@@ -25,7 +30,20 @@ enum nl80211_dfs_regions reg_get_dfs_region(struct wiphy *wiphy);
25 30
26int regulatory_hint_user(const char *alpha2, 31int regulatory_hint_user(const char *alpha2,
27 enum nl80211_user_reg_hint_type user_reg_hint_type); 32 enum nl80211_user_reg_hint_type user_reg_hint_type);
28int regulatory_hint_indoor_user(void); 33
34/**
35 * regulatory_hint_indoor - hint operation in indoor env. or not
36 * @is_indoor: if true indicates that user space thinks that the
37 * device is operating in an indoor environment.
38 * @portid: the netlink port ID on which the hint was given.
39 */
40int regulatory_hint_indoor(bool is_indoor, u32 portid);
41
42/**
43 * regulatory_netlink_notify - notify on released netlink socket
44 * @portid: the netlink socket port ID
45 */
46void regulatory_netlink_notify(u32 portid);
29 47
30void wiphy_regulatory_register(struct wiphy *wiphy); 48void wiphy_regulatory_register(struct wiphy *wiphy);
31void wiphy_regulatory_deregister(struct wiphy *wiphy); 49void wiphy_regulatory_deregister(struct wiphy *wiphy);
@@ -33,7 +51,9 @@ void wiphy_regulatory_deregister(struct wiphy *wiphy);
33int __init regulatory_init(void); 51int __init regulatory_init(void);
34void regulatory_exit(void); 52void regulatory_exit(void);
35 53
36int set_regdom(const struct ieee80211_regdomain *rd); 54int set_regdom(const struct ieee80211_regdomain *rd,
55 enum ieee80211_regd_source regd_src);
56
37unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd, 57unsigned int reg_get_max_bandwidth(const struct ieee80211_regdomain *rd,
38 const struct ieee80211_reg_rule *rule); 58 const struct ieee80211_reg_rule *rule);
39 59
diff --git a/net/wireless/scan.c b/net/wireless/scan.c
index c705c3e2b751..3a50aa2553bf 100644
--- a/net/wireless/scan.c
+++ b/net/wireless/scan.c
@@ -531,24 +531,78 @@ static int cmp_bss(struct cfg80211_bss *a,
531 } 531 }
532} 532}
533 533
534static bool cfg80211_bss_type_match(u16 capability,
535 enum ieee80211_band band,
536 enum ieee80211_bss_type bss_type)
537{
538 bool ret = true;
539 u16 mask, val;
540
541 if (bss_type == IEEE80211_BSS_TYPE_ANY)
542 return ret;
543
544 if (band == IEEE80211_BAND_60GHZ) {
545 mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
546 switch (bss_type) {
547 case IEEE80211_BSS_TYPE_ESS:
548 val = WLAN_CAPABILITY_DMG_TYPE_AP;
549 break;
550 case IEEE80211_BSS_TYPE_PBSS:
551 val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
552 break;
553 case IEEE80211_BSS_TYPE_IBSS:
554 val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
555 break;
556 default:
557 return false;
558 }
559 } else {
560 mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
561 switch (bss_type) {
562 case IEEE80211_BSS_TYPE_ESS:
563 val = WLAN_CAPABILITY_ESS;
564 break;
565 case IEEE80211_BSS_TYPE_IBSS:
566 val = WLAN_CAPABILITY_IBSS;
567 break;
568 case IEEE80211_BSS_TYPE_MBSS:
569 val = 0;
570 break;
571 default:
572 return false;
573 }
574 }
575
576 ret = ((capability & mask) == val);
577 return ret;
578}
579
534/* Returned bss is reference counted and must be cleaned up appropriately. */ 580/* Returned bss is reference counted and must be cleaned up appropriately. */
535struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy, 581struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
536 struct ieee80211_channel *channel, 582 struct ieee80211_channel *channel,
537 const u8 *bssid, 583 const u8 *bssid,
538 const u8 *ssid, size_t ssid_len, 584 const u8 *ssid, size_t ssid_len,
539 u16 capa_mask, u16 capa_val) 585 enum ieee80211_bss_type bss_type,
586 enum ieee80211_privacy privacy)
540{ 587{
541 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); 588 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
542 struct cfg80211_internal_bss *bss, *res = NULL; 589 struct cfg80211_internal_bss *bss, *res = NULL;
543 unsigned long now = jiffies; 590 unsigned long now = jiffies;
591 int bss_privacy;
544 592
545 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask, 593 trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
546 capa_val); 594 privacy);
547 595
548 spin_lock_bh(&rdev->bss_lock); 596 spin_lock_bh(&rdev->bss_lock);
549 597
550 list_for_each_entry(bss, &rdev->bss_list, list) { 598 list_for_each_entry(bss, &rdev->bss_list, list) {
551 if ((bss->pub.capability & capa_mask) != capa_val) 599 if (!cfg80211_bss_type_match(bss->pub.capability,
600 bss->pub.channel->band, bss_type))
601 continue;
602
603 bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
604 if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
605 (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
552 continue; 606 continue;
553 if (channel && bss->pub.channel != channel) 607 if (channel && bss->pub.channel != channel)
554 continue; 608 continue;
@@ -896,6 +950,7 @@ cfg80211_inform_bss_width(struct wiphy *wiphy,
896 struct cfg80211_bss_ies *ies; 950 struct cfg80211_bss_ies *ies;
897 struct ieee80211_channel *channel; 951 struct ieee80211_channel *channel;
898 struct cfg80211_internal_bss tmp = {}, *res; 952 struct cfg80211_internal_bss tmp = {}, *res;
953 int bss_type;
899 bool signal_valid; 954 bool signal_valid;
900 955
901 if (WARN_ON(!wiphy)) 956 if (WARN_ON(!wiphy))
@@ -950,8 +1005,15 @@ cfg80211_inform_bss_width(struct wiphy *wiphy,
950 if (!res) 1005 if (!res)
951 return NULL; 1006 return NULL;
952 1007
953 if (res->pub.capability & WLAN_CAPABILITY_ESS) 1008 if (channel->band == IEEE80211_BAND_60GHZ) {
954 regulatory_hint_found_beacon(wiphy, channel, gfp); 1009 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1010 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1011 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1012 regulatory_hint_found_beacon(wiphy, channel, gfp);
1013 } else {
1014 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1015 regulatory_hint_found_beacon(wiphy, channel, gfp);
1016 }
955 1017
956 trace_cfg80211_return_bss(&res->pub); 1018 trace_cfg80211_return_bss(&res->pub);
957 /* cfg80211_bss_update gives us a referenced result */ 1019 /* cfg80211_bss_update gives us a referenced result */
@@ -973,6 +1035,7 @@ cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
973 bool signal_valid; 1035 bool signal_valid;
974 size_t ielen = len - offsetof(struct ieee80211_mgmt, 1036 size_t ielen = len - offsetof(struct ieee80211_mgmt,
975 u.probe_resp.variable); 1037 u.probe_resp.variable);
1038 int bss_type;
976 1039
977 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) != 1040 BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
978 offsetof(struct ieee80211_mgmt, u.beacon.variable)); 1041 offsetof(struct ieee80211_mgmt, u.beacon.variable));
@@ -1025,8 +1088,15 @@ cfg80211_inform_bss_width_frame(struct wiphy *wiphy,
1025 if (!res) 1088 if (!res)
1026 return NULL; 1089 return NULL;
1027 1090
1028 if (res->pub.capability & WLAN_CAPABILITY_ESS) 1091 if (channel->band == IEEE80211_BAND_60GHZ) {
1029 regulatory_hint_found_beacon(wiphy, channel, gfp); 1092 bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
1093 if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
1094 bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
1095 regulatory_hint_found_beacon(wiphy, channel, gfp);
1096 } else {
1097 if (res->pub.capability & WLAN_CAPABILITY_ESS)
1098 regulatory_hint_found_beacon(wiphy, channel, gfp);
1099 }
1030 1100
1031 trace_cfg80211_return_bss(&res->pub); 1101 trace_cfg80211_return_bss(&res->pub);
1032 /* cfg80211_bss_update gives us a referenced result */ 1102 /* cfg80211_bss_update gives us a referenced result */
@@ -1237,17 +1307,17 @@ int cfg80211_wext_siwscan(struct net_device *dev,
1237 kfree(creq); 1307 kfree(creq);
1238 return err; 1308 return err;
1239} 1309}
1240EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan); 1310EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
1241 1311
1242static void ieee80211_scan_add_ies(struct iw_request_info *info, 1312static char *ieee80211_scan_add_ies(struct iw_request_info *info,
1243 const struct cfg80211_bss_ies *ies, 1313 const struct cfg80211_bss_ies *ies,
1244 char **current_ev, char *end_buf) 1314 char *current_ev, char *end_buf)
1245{ 1315{
1246 const u8 *pos, *end, *next; 1316 const u8 *pos, *end, *next;
1247 struct iw_event iwe; 1317 struct iw_event iwe;
1248 1318
1249 if (!ies) 1319 if (!ies)
1250 return; 1320 return current_ev;
1251 1321
1252 /* 1322 /*
1253 * If needed, fragment the IEs buffer (at IE boundaries) into short 1323 * If needed, fragment the IEs buffer (at IE boundaries) into short
@@ -1264,10 +1334,11 @@ static void ieee80211_scan_add_ies(struct iw_request_info *info,
1264 memset(&iwe, 0, sizeof(iwe)); 1334 memset(&iwe, 0, sizeof(iwe));
1265 iwe.cmd = IWEVGENIE; 1335 iwe.cmd = IWEVGENIE;
1266 iwe.u.data.length = next - pos; 1336 iwe.u.data.length = next - pos;
1267 *current_ev = iwe_stream_add_point(info, *current_ev, 1337 current_ev = iwe_stream_add_point_check(info, current_ev,
1268 end_buf, &iwe, 1338 end_buf, &iwe,
1269 (void *)pos); 1339 (void *)pos);
1270 1340 if (IS_ERR(current_ev))
1341 return current_ev;
1271 pos = next; 1342 pos = next;
1272 } 1343 }
1273 1344
@@ -1275,10 +1346,14 @@ static void ieee80211_scan_add_ies(struct iw_request_info *info,
1275 memset(&iwe, 0, sizeof(iwe)); 1346 memset(&iwe, 0, sizeof(iwe));
1276 iwe.cmd = IWEVGENIE; 1347 iwe.cmd = IWEVGENIE;
1277 iwe.u.data.length = end - pos; 1348 iwe.u.data.length = end - pos;
1278 *current_ev = iwe_stream_add_point(info, *current_ev, 1349 current_ev = iwe_stream_add_point_check(info, current_ev,
1279 end_buf, &iwe, 1350 end_buf, &iwe,
1280 (void *)pos); 1351 (void *)pos);
1352 if (IS_ERR(current_ev))
1353 return current_ev;
1281 } 1354 }
1355
1356 return current_ev;
1282} 1357}
1283 1358
1284static char * 1359static char *
@@ -1289,7 +1364,8 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1289 const struct cfg80211_bss_ies *ies; 1364 const struct cfg80211_bss_ies *ies;
1290 struct iw_event iwe; 1365 struct iw_event iwe;
1291 const u8 *ie; 1366 const u8 *ie;
1292 u8 *buf, *cfg, *p; 1367 u8 buf[50];
1368 u8 *cfg, *p, *tmp;
1293 int rem, i, sig; 1369 int rem, i, sig;
1294 bool ismesh = false; 1370 bool ismesh = false;
1295 1371
@@ -1297,22 +1373,28 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1297 iwe.cmd = SIOCGIWAP; 1373 iwe.cmd = SIOCGIWAP;
1298 iwe.u.ap_addr.sa_family = ARPHRD_ETHER; 1374 iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
1299 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN); 1375 memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
1300 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, 1376 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1301 IW_EV_ADDR_LEN); 1377 IW_EV_ADDR_LEN);
1378 if (IS_ERR(current_ev))
1379 return current_ev;
1302 1380
1303 memset(&iwe, 0, sizeof(iwe)); 1381 memset(&iwe, 0, sizeof(iwe));
1304 iwe.cmd = SIOCGIWFREQ; 1382 iwe.cmd = SIOCGIWFREQ;
1305 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq); 1383 iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
1306 iwe.u.freq.e = 0; 1384 iwe.u.freq.e = 0;
1307 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, 1385 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1308 IW_EV_FREQ_LEN); 1386 IW_EV_FREQ_LEN);
1387 if (IS_ERR(current_ev))
1388 return current_ev;
1309 1389
1310 memset(&iwe, 0, sizeof(iwe)); 1390 memset(&iwe, 0, sizeof(iwe));
1311 iwe.cmd = SIOCGIWFREQ; 1391 iwe.cmd = SIOCGIWFREQ;
1312 iwe.u.freq.m = bss->pub.channel->center_freq; 1392 iwe.u.freq.m = bss->pub.channel->center_freq;
1313 iwe.u.freq.e = 6; 1393 iwe.u.freq.e = 6;
1314 current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe, 1394 current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
1315 IW_EV_FREQ_LEN); 1395 IW_EV_FREQ_LEN);
1396 if (IS_ERR(current_ev))
1397 return current_ev;
1316 1398
1317 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) { 1399 if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
1318 memset(&iwe, 0, sizeof(iwe)); 1400 memset(&iwe, 0, sizeof(iwe));
@@ -1341,8 +1423,11 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1341 /* not reached */ 1423 /* not reached */
1342 break; 1424 break;
1343 } 1425 }
1344 current_ev = iwe_stream_add_event(info, current_ev, end_buf, 1426 current_ev = iwe_stream_add_event_check(info, current_ev,
1345 &iwe, IW_EV_QUAL_LEN); 1427 end_buf, &iwe,
1428 IW_EV_QUAL_LEN);
1429 if (IS_ERR(current_ev))
1430 return current_ev;
1346 } 1431 }
1347 1432
1348 memset(&iwe, 0, sizeof(iwe)); 1433 memset(&iwe, 0, sizeof(iwe));
@@ -1352,8 +1437,10 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1352 else 1437 else
1353 iwe.u.data.flags = IW_ENCODE_DISABLED; 1438 iwe.u.data.flags = IW_ENCODE_DISABLED;
1354 iwe.u.data.length = 0; 1439 iwe.u.data.length = 0;
1355 current_ev = iwe_stream_add_point(info, current_ev, end_buf, 1440 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
1356 &iwe, ""); 1441 &iwe, "");
1442 if (IS_ERR(current_ev))
1443 return current_ev;
1357 1444
1358 rcu_read_lock(); 1445 rcu_read_lock();
1359 ies = rcu_dereference(bss->pub.ies); 1446 ies = rcu_dereference(bss->pub.ies);
@@ -1371,66 +1458,91 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1371 iwe.cmd = SIOCGIWESSID; 1458 iwe.cmd = SIOCGIWESSID;
1372 iwe.u.data.length = ie[1]; 1459 iwe.u.data.length = ie[1];
1373 iwe.u.data.flags = 1; 1460 iwe.u.data.flags = 1;
1374 current_ev = iwe_stream_add_point(info, current_ev, end_buf, 1461 current_ev = iwe_stream_add_point_check(info,
1375 &iwe, (u8 *)ie + 2); 1462 current_ev,
1463 end_buf, &iwe,
1464 (u8 *)ie + 2);
1465 if (IS_ERR(current_ev))
1466 goto unlock;
1376 break; 1467 break;
1377 case WLAN_EID_MESH_ID: 1468 case WLAN_EID_MESH_ID:
1378 memset(&iwe, 0, sizeof(iwe)); 1469 memset(&iwe, 0, sizeof(iwe));
1379 iwe.cmd = SIOCGIWESSID; 1470 iwe.cmd = SIOCGIWESSID;
1380 iwe.u.data.length = ie[1]; 1471 iwe.u.data.length = ie[1];
1381 iwe.u.data.flags = 1; 1472 iwe.u.data.flags = 1;
1382 current_ev = iwe_stream_add_point(info, current_ev, end_buf, 1473 current_ev = iwe_stream_add_point_check(info,
1383 &iwe, (u8 *)ie + 2); 1474 current_ev,
1475 end_buf, &iwe,
1476 (u8 *)ie + 2);
1477 if (IS_ERR(current_ev))
1478 goto unlock;
1384 break; 1479 break;
1385 case WLAN_EID_MESH_CONFIG: 1480 case WLAN_EID_MESH_CONFIG:
1386 ismesh = true; 1481 ismesh = true;
1387 if (ie[1] != sizeof(struct ieee80211_meshconf_ie)) 1482 if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
1388 break; 1483 break;
1389 buf = kmalloc(50, GFP_ATOMIC);
1390 if (!buf)
1391 break;
1392 cfg = (u8 *)ie + 2; 1484 cfg = (u8 *)ie + 2;
1393 memset(&iwe, 0, sizeof(iwe)); 1485 memset(&iwe, 0, sizeof(iwe));
1394 iwe.cmd = IWEVCUSTOM; 1486 iwe.cmd = IWEVCUSTOM;
1395 sprintf(buf, "Mesh Network Path Selection Protocol ID: " 1487 sprintf(buf, "Mesh Network Path Selection Protocol ID: "
1396 "0x%02X", cfg[0]); 1488 "0x%02X", cfg[0]);
1397 iwe.u.data.length = strlen(buf); 1489 iwe.u.data.length = strlen(buf);
1398 current_ev = iwe_stream_add_point(info, current_ev, 1490 current_ev = iwe_stream_add_point_check(info,
1399 end_buf, 1491 current_ev,
1400 &iwe, buf); 1492 end_buf,
1493 &iwe, buf);
1494 if (IS_ERR(current_ev))
1495 goto unlock;
1401 sprintf(buf, "Path Selection Metric ID: 0x%02X", 1496 sprintf(buf, "Path Selection Metric ID: 0x%02X",
1402 cfg[1]); 1497 cfg[1]);
1403 iwe.u.data.length = strlen(buf); 1498 iwe.u.data.length = strlen(buf);
1404 current_ev = iwe_stream_add_point(info, current_ev, 1499 current_ev = iwe_stream_add_point_check(info,
1405 end_buf, 1500 current_ev,
1406 &iwe, buf); 1501 end_buf,
1502 &iwe, buf);
1503 if (IS_ERR(current_ev))
1504 goto unlock;
1407 sprintf(buf, "Congestion Control Mode ID: 0x%02X", 1505 sprintf(buf, "Congestion Control Mode ID: 0x%02X",
1408 cfg[2]); 1506 cfg[2]);
1409 iwe.u.data.length = strlen(buf); 1507 iwe.u.data.length = strlen(buf);
1410 current_ev = iwe_stream_add_point(info, current_ev, 1508 current_ev = iwe_stream_add_point_check(info,
1411 end_buf, 1509 current_ev,
1412 &iwe, buf); 1510 end_buf,
1511 &iwe, buf);
1512 if (IS_ERR(current_ev))
1513 goto unlock;
1413 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]); 1514 sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
1414 iwe.u.data.length = strlen(buf); 1515 iwe.u.data.length = strlen(buf);
1415 current_ev = iwe_stream_add_point(info, current_ev, 1516 current_ev = iwe_stream_add_point_check(info,
1416 end_buf, 1517 current_ev,
1417 &iwe, buf); 1518 end_buf,
1519 &iwe, buf);
1520 if (IS_ERR(current_ev))
1521 goto unlock;
1418 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]); 1522 sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
1419 iwe.u.data.length = strlen(buf); 1523 iwe.u.data.length = strlen(buf);
1420 current_ev = iwe_stream_add_point(info, current_ev, 1524 current_ev = iwe_stream_add_point_check(info,
1421 end_buf, 1525 current_ev,
1422 &iwe, buf); 1526 end_buf,
1527 &iwe, buf);
1528 if (IS_ERR(current_ev))
1529 goto unlock;
1423 sprintf(buf, "Formation Info: 0x%02X", cfg[5]); 1530 sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
1424 iwe.u.data.length = strlen(buf); 1531 iwe.u.data.length = strlen(buf);
1425 current_ev = iwe_stream_add_point(info, current_ev, 1532 current_ev = iwe_stream_add_point_check(info,
1426 end_buf, 1533 current_ev,
1427 &iwe, buf); 1534 end_buf,
1535 &iwe, buf);
1536 if (IS_ERR(current_ev))
1537 goto unlock;
1428 sprintf(buf, "Capabilities: 0x%02X", cfg[6]); 1538 sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
1429 iwe.u.data.length = strlen(buf); 1539 iwe.u.data.length = strlen(buf);
1430 current_ev = iwe_stream_add_point(info, current_ev, 1540 current_ev = iwe_stream_add_point_check(info,
1431 end_buf, 1541 current_ev,
1432 &iwe, buf); 1542 end_buf,
1433 kfree(buf); 1543 &iwe, buf);
1544 if (IS_ERR(current_ev))
1545 goto unlock;
1434 break; 1546 break;
1435 case WLAN_EID_SUPP_RATES: 1547 case WLAN_EID_SUPP_RATES:
1436 case WLAN_EID_EXT_SUPP_RATES: 1548 case WLAN_EID_EXT_SUPP_RATES:
@@ -1445,8 +1557,14 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1445 for (i = 0; i < ie[1]; i++) { 1557 for (i = 0; i < ie[1]; i++) {
1446 iwe.u.bitrate.value = 1558 iwe.u.bitrate.value =
1447 ((ie[i + 2] & 0x7f) * 500000); 1559 ((ie[i + 2] & 0x7f) * 500000);
1560 tmp = p;
1448 p = iwe_stream_add_value(info, current_ev, p, 1561 p = iwe_stream_add_value(info, current_ev, p,
1449 end_buf, &iwe, IW_EV_PARAM_LEN); 1562 end_buf, &iwe,
1563 IW_EV_PARAM_LEN);
1564 if (p == tmp) {
1565 current_ev = ERR_PTR(-E2BIG);
1566 goto unlock;
1567 }
1450 } 1568 }
1451 current_ev = p; 1569 current_ev = p;
1452 break; 1570 break;
@@ -1465,31 +1583,35 @@ ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
1465 iwe.u.mode = IW_MODE_MASTER; 1583 iwe.u.mode = IW_MODE_MASTER;
1466 else 1584 else
1467 iwe.u.mode = IW_MODE_ADHOC; 1585 iwe.u.mode = IW_MODE_ADHOC;
1468 current_ev = iwe_stream_add_event(info, current_ev, end_buf, 1586 current_ev = iwe_stream_add_event_check(info, current_ev,
1469 &iwe, IW_EV_UINT_LEN); 1587 end_buf, &iwe,
1470 } 1588 IW_EV_UINT_LEN);
1471 1589 if (IS_ERR(current_ev))
1472 buf = kmalloc(31, GFP_ATOMIC); 1590 goto unlock;
1473 if (buf) {
1474 memset(&iwe, 0, sizeof(iwe));
1475 iwe.cmd = IWEVCUSTOM;
1476 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
1477 iwe.u.data.length = strlen(buf);
1478 current_ev = iwe_stream_add_point(info, current_ev, end_buf,
1479 &iwe, buf);
1480 memset(&iwe, 0, sizeof(iwe));
1481 iwe.cmd = IWEVCUSTOM;
1482 sprintf(buf, " Last beacon: %ums ago",
1483 elapsed_jiffies_msecs(bss->ts));
1484 iwe.u.data.length = strlen(buf);
1485 current_ev = iwe_stream_add_point(info, current_ev,
1486 end_buf, &iwe, buf);
1487 kfree(buf);
1488 } 1591 }
1489 1592
1490 ieee80211_scan_add_ies(info, ies, &current_ev, end_buf); 1593 memset(&iwe, 0, sizeof(iwe));
1594 iwe.cmd = IWEVCUSTOM;
1595 sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
1596 iwe.u.data.length = strlen(buf);
1597 current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
1598 &iwe, buf);
1599 if (IS_ERR(current_ev))
1600 goto unlock;
1601 memset(&iwe, 0, sizeof(iwe));
1602 iwe.cmd = IWEVCUSTOM;
1603 sprintf(buf, " Last beacon: %ums ago",
1604 elapsed_jiffies_msecs(bss->ts));
1605 iwe.u.data.length = strlen(buf);
1606 current_ev = iwe_stream_add_point_check(info, current_ev,
1607 end_buf, &iwe, buf);
1608 if (IS_ERR(current_ev))
1609 goto unlock;
1610
1611 current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
1612
1613 unlock:
1491 rcu_read_unlock(); 1614 rcu_read_unlock();
1492
1493 return current_ev; 1615 return current_ev;
1494} 1616}
1495 1617
@@ -1501,19 +1623,27 @@ static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
1501 char *current_ev = buf; 1623 char *current_ev = buf;
1502 char *end_buf = buf + len; 1624 char *end_buf = buf + len;
1503 struct cfg80211_internal_bss *bss; 1625 struct cfg80211_internal_bss *bss;
1626 int err = 0;
1504 1627
1505 spin_lock_bh(&rdev->bss_lock); 1628 spin_lock_bh(&rdev->bss_lock);
1506 cfg80211_bss_expire(rdev); 1629 cfg80211_bss_expire(rdev);
1507 1630
1508 list_for_each_entry(bss, &rdev->bss_list, list) { 1631 list_for_each_entry(bss, &rdev->bss_list, list) {
1509 if (buf + len - current_ev <= IW_EV_ADDR_LEN) { 1632 if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
1510 spin_unlock_bh(&rdev->bss_lock); 1633 err = -E2BIG;
1511 return -E2BIG; 1634 break;
1512 } 1635 }
1513 current_ev = ieee80211_bss(&rdev->wiphy, info, bss, 1636 current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
1514 current_ev, end_buf); 1637 current_ev, end_buf);
1638 if (IS_ERR(current_ev)) {
1639 err = PTR_ERR(current_ev);
1640 break;
1641 }
1515 } 1642 }
1516 spin_unlock_bh(&rdev->bss_lock); 1643 spin_unlock_bh(&rdev->bss_lock);
1644
1645 if (err)
1646 return err;
1517 return current_ev - buf; 1647 return current_ev - buf;
1518} 1648}
1519 1649
@@ -1545,5 +1675,5 @@ int cfg80211_wext_giwscan(struct net_device *dev,
1545 1675
1546 return res; 1676 return res;
1547} 1677}
1548EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan); 1678EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
1549#endif 1679#endif
diff --git a/net/wireless/sme.c b/net/wireless/sme.c
index 0ab3711c79a0..d11454f87bac 100644
--- a/net/wireless/sme.c
+++ b/net/wireless/sme.c
@@ -42,7 +42,7 @@ struct cfg80211_conn {
42 CFG80211_CONN_CONNECTED, 42 CFG80211_CONN_CONNECTED,
43 } state; 43 } state;
44 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN]; 44 u8 bssid[ETH_ALEN], prev_bssid[ETH_ALEN];
45 u8 *ie; 45 const u8 *ie;
46 size_t ie_len; 46 size_t ie_len;
47 bool auto_auth, prev_bssid_valid; 47 bool auto_auth, prev_bssid_valid;
48}; 48};
@@ -257,19 +257,15 @@ static struct cfg80211_bss *cfg80211_get_conn_bss(struct wireless_dev *wdev)
257{ 257{
258 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy); 258 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
259 struct cfg80211_bss *bss; 259 struct cfg80211_bss *bss;
260 u16 capa = WLAN_CAPABILITY_ESS;
261 260
262 ASSERT_WDEV_LOCK(wdev); 261 ASSERT_WDEV_LOCK(wdev);
263 262
264 if (wdev->conn->params.privacy)
265 capa |= WLAN_CAPABILITY_PRIVACY;
266
267 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel, 263 bss = cfg80211_get_bss(wdev->wiphy, wdev->conn->params.channel,
268 wdev->conn->params.bssid, 264 wdev->conn->params.bssid,
269 wdev->conn->params.ssid, 265 wdev->conn->params.ssid,
270 wdev->conn->params.ssid_len, 266 wdev->conn->params.ssid_len,
271 WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_PRIVACY, 267 IEEE80211_BSS_TYPE_ESS,
272 capa); 268 IEEE80211_PRIVACY(wdev->conn->params.privacy));
273 if (!bss) 269 if (!bss)
274 return NULL; 270 return NULL;
275 271
@@ -427,6 +423,62 @@ void cfg80211_sme_assoc_timeout(struct wireless_dev *wdev)
427 schedule_work(&rdev->conn_work); 423 schedule_work(&rdev->conn_work);
428} 424}
429 425
426static int cfg80211_sme_get_conn_ies(struct wireless_dev *wdev,
427 const u8 *ies, size_t ies_len,
428 const u8 **out_ies, size_t *out_ies_len)
429{
430 struct cfg80211_registered_device *rdev = wiphy_to_rdev(wdev->wiphy);
431 u8 *buf;
432 size_t offs;
433
434 if (!rdev->wiphy.extended_capabilities_len ||
435 (ies && cfg80211_find_ie(WLAN_EID_EXT_CAPABILITY, ies, ies_len))) {
436 *out_ies = kmemdup(ies, ies_len, GFP_KERNEL);
437 if (!*out_ies)
438 return -ENOMEM;
439 *out_ies_len = ies_len;
440 return 0;
441 }
442
443 buf = kmalloc(ies_len + rdev->wiphy.extended_capabilities_len + 2,
444 GFP_KERNEL);
445 if (!buf)
446 return -ENOMEM;
447
448 if (ies_len) {
449 static const u8 before_extcapa[] = {
450 /* not listing IEs expected to be created by driver */
451 WLAN_EID_RSN,
452 WLAN_EID_QOS_CAPA,
453 WLAN_EID_RRM_ENABLED_CAPABILITIES,
454 WLAN_EID_MOBILITY_DOMAIN,
455 WLAN_EID_SUPPORTED_REGULATORY_CLASSES,
456 WLAN_EID_BSS_COEX_2040,
457 };
458
459 offs = ieee80211_ie_split(ies, ies_len, before_extcapa,
460 ARRAY_SIZE(before_extcapa), 0);
461 memcpy(buf, ies, offs);
462 /* leave a whole for extended capabilities IE */
463 memcpy(buf + offs + rdev->wiphy.extended_capabilities_len + 2,
464 ies + offs, ies_len - offs);
465 } else {
466 offs = 0;
467 }
468
469 /* place extended capabilities IE (with only driver capabilities) */
470 buf[offs] = WLAN_EID_EXT_CAPABILITY;
471 buf[offs + 1] = rdev->wiphy.extended_capabilities_len;
472 memcpy(buf + offs + 2,
473 rdev->wiphy.extended_capabilities,
474 rdev->wiphy.extended_capabilities_len);
475
476 *out_ies = buf;
477 *out_ies_len = ies_len + rdev->wiphy.extended_capabilities_len + 2;
478
479 return 0;
480}
481
430static int cfg80211_sme_connect(struct wireless_dev *wdev, 482static int cfg80211_sme_connect(struct wireless_dev *wdev,
431 struct cfg80211_connect_params *connect, 483 struct cfg80211_connect_params *connect,
432 const u8 *prev_bssid) 484 const u8 *prev_bssid)
@@ -457,16 +509,14 @@ static int cfg80211_sme_connect(struct wireless_dev *wdev,
457 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN); 509 memcpy(wdev->conn->bssid, connect->bssid, ETH_ALEN);
458 } 510 }
459 511
460 if (connect->ie) { 512 if (cfg80211_sme_get_conn_ies(wdev, connect->ie, connect->ie_len,
461 wdev->conn->ie = kmemdup(connect->ie, connect->ie_len, 513 &wdev->conn->ie,
462 GFP_KERNEL); 514 &wdev->conn->params.ie_len)) {
463 wdev->conn->params.ie = wdev->conn->ie; 515 kfree(wdev->conn);
464 if (!wdev->conn->ie) { 516 wdev->conn = NULL;
465 kfree(wdev->conn); 517 return -ENOMEM;
466 wdev->conn = NULL;
467 return -ENOMEM;
468 }
469 } 518 }
519 wdev->conn->params.ie = wdev->conn->ie;
470 520
471 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) { 521 if (connect->auth_type == NL80211_AUTHTYPE_AUTOMATIC) {
472 wdev->conn->auto_auth = true; 522 wdev->conn->auto_auth = true;
@@ -637,8 +687,8 @@ void __cfg80211_connect_result(struct net_device *dev, const u8 *bssid,
637 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect); 687 WARN_ON_ONCE(!wiphy_to_rdev(wdev->wiphy)->ops->connect);
638 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid, 688 bss = cfg80211_get_bss(wdev->wiphy, NULL, bssid,
639 wdev->ssid, wdev->ssid_len, 689 wdev->ssid, wdev->ssid_len,
640 WLAN_CAPABILITY_ESS, 690 IEEE80211_BSS_TYPE_ESS,
641 WLAN_CAPABILITY_ESS); 691 IEEE80211_PRIVACY_ANY);
642 if (bss) 692 if (bss)
643 cfg80211_hold_bss(bss_from_pub(bss)); 693 cfg80211_hold_bss(bss_from_pub(bss));
644 } 694 }
@@ -795,8 +845,8 @@ void cfg80211_roamed(struct net_device *dev,
795 struct cfg80211_bss *bss; 845 struct cfg80211_bss *bss;
796 846
797 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid, 847 bss = cfg80211_get_bss(wdev->wiphy, channel, bssid, wdev->ssid,
798 wdev->ssid_len, WLAN_CAPABILITY_ESS, 848 wdev->ssid_len,
799 WLAN_CAPABILITY_ESS); 849 IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ANY);
800 if (WARN_ON(!bss)) 850 if (WARN_ON(!bss))
801 return; 851 return;
802 852
diff --git a/net/wireless/trace.h b/net/wireless/trace.h
index b17b3692f8c2..af3617c9879e 100644
--- a/net/wireless/trace.h
+++ b/net/wireless/trace.h
@@ -7,6 +7,7 @@
7#include <linux/tracepoint.h> 7#include <linux/tracepoint.h>
8 8
9#include <linux/rtnetlink.h> 9#include <linux/rtnetlink.h>
10#include <linux/etherdevice.h>
10#include <net/cfg80211.h> 11#include <net/cfg80211.h>
11#include "core.h" 12#include "core.h"
12 13
@@ -15,7 +16,7 @@
15 if (given_mac) \ 16 if (given_mac) \
16 memcpy(__entry->entry_mac, given_mac, ETH_ALEN); \ 17 memcpy(__entry->entry_mac, given_mac, ETH_ALEN); \
17 else \ 18 else \
18 memset(__entry->entry_mac, 0, ETH_ALEN); \ 19 eth_zero_addr(__entry->entry_mac); \
19 } while (0) 20 } while (0)
20#define MAC_PR_FMT "%pM" 21#define MAC_PR_FMT "%pM"
21#define MAC_PR_ARG(entry_mac) (__entry->entry_mac) 22#define MAC_PR_ARG(entry_mac) (__entry->entry_mac)
@@ -627,6 +628,7 @@ DECLARE_EVENT_CLASS(station_add_change,
627 __field(u8, plink_state) 628 __field(u8, plink_state)
628 __field(u8, uapsd_queues) 629 __field(u8, uapsd_queues)
629 __array(u8, ht_capa, (int)sizeof(struct ieee80211_ht_cap)) 630 __array(u8, ht_capa, (int)sizeof(struct ieee80211_ht_cap))
631 __array(char, vlan, IFNAMSIZ)
630 ), 632 ),
631 TP_fast_assign( 633 TP_fast_assign(
632 WIPHY_ASSIGN; 634 WIPHY_ASSIGN;
@@ -644,16 +646,19 @@ DECLARE_EVENT_CLASS(station_add_change,
644 if (params->ht_capa) 646 if (params->ht_capa)
645 memcpy(__entry->ht_capa, params->ht_capa, 647 memcpy(__entry->ht_capa, params->ht_capa,
646 sizeof(struct ieee80211_ht_cap)); 648 sizeof(struct ieee80211_ht_cap));
649 memset(__entry->vlan, 0, sizeof(__entry->vlan));
650 if (params->vlan)
651 memcpy(__entry->vlan, params->vlan->name, IFNAMSIZ);
647 ), 652 ),
648 TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", station mac: " MAC_PR_FMT 653 TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", station mac: " MAC_PR_FMT
649 ", station flags mask: %u, station flags set: %u, " 654 ", station flags mask: %u, station flags set: %u, "
650 "station modify mask: %u, listen interval: %d, aid: %u, " 655 "station modify mask: %u, listen interval: %d, aid: %u, "
651 "plink action: %u, plink state: %u, uapsd queues: %u", 656 "plink action: %u, plink state: %u, uapsd queues: %u, vlan:%s",
652 WIPHY_PR_ARG, NETDEV_PR_ARG, MAC_PR_ARG(sta_mac), 657 WIPHY_PR_ARG, NETDEV_PR_ARG, MAC_PR_ARG(sta_mac),
653 __entry->sta_flags_mask, __entry->sta_flags_set, 658 __entry->sta_flags_mask, __entry->sta_flags_set,
654 __entry->sta_modify_mask, __entry->listen_interval, 659 __entry->sta_modify_mask, __entry->listen_interval,
655 __entry->aid, __entry->plink_action, __entry->plink_state, 660 __entry->aid, __entry->plink_action, __entry->plink_state,
656 __entry->uapsd_queues) 661 __entry->uapsd_queues, __entry->vlan)
657); 662);
658 663
659DEFINE_EVENT(station_add_change, rdev_add_station, 664DEFINE_EVENT(station_add_change, rdev_add_station,
@@ -1077,7 +1082,7 @@ TRACE_EVENT(rdev_auth,
1077 if (req->bss) 1082 if (req->bss)
1078 MAC_ASSIGN(bssid, req->bss->bssid); 1083 MAC_ASSIGN(bssid, req->bss->bssid);
1079 else 1084 else
1080 memset(__entry->bssid, 0, ETH_ALEN); 1085 eth_zero_addr(__entry->bssid);
1081 __entry->auth_type = req->auth_type; 1086 __entry->auth_type = req->auth_type;
1082 ), 1087 ),
1083 TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", auth type: %d, bssid: " MAC_PR_FMT, 1088 TP_printk(WIPHY_PR_FMT ", " NETDEV_PR_FMT ", auth type: %d, bssid: " MAC_PR_FMT,
@@ -1103,7 +1108,7 @@ TRACE_EVENT(rdev_assoc,
1103 if (req->bss) 1108 if (req->bss)
1104 MAC_ASSIGN(bssid, req->bss->bssid); 1109 MAC_ASSIGN(bssid, req->bss->bssid);
1105 else 1110 else
1106 memset(__entry->bssid, 0, ETH_ALEN); 1111 eth_zero_addr(__entry->bssid);
1107 MAC_ASSIGN(prev_bssid, req->prev_bssid); 1112 MAC_ASSIGN(prev_bssid, req->prev_bssid);
1108 __entry->use_mfp = req->use_mfp; 1113 __entry->use_mfp = req->use_mfp;
1109 __entry->flags = req->flags; 1114 __entry->flags = req->flags;
@@ -1153,7 +1158,7 @@ TRACE_EVENT(rdev_disassoc,
1153 if (req->bss) 1158 if (req->bss)
1154 MAC_ASSIGN(bssid, req->bss->bssid); 1159 MAC_ASSIGN(bssid, req->bss->bssid);
1155 else 1160 else
1156 memset(__entry->bssid, 0, ETH_ALEN); 1161 eth_zero_addr(__entry->bssid);
1157 __entry->reason_code = req->reason_code; 1162 __entry->reason_code = req->reason_code;
1158 __entry->local_state_change = req->local_state_change; 1163 __entry->local_state_change = req->local_state_change;
1159 ), 1164 ),
@@ -2636,28 +2641,30 @@ DEFINE_EVENT(wiphy_only_evt, cfg80211_sched_scan_stopped,
2636TRACE_EVENT(cfg80211_get_bss, 2641TRACE_EVENT(cfg80211_get_bss,
2637 TP_PROTO(struct wiphy *wiphy, struct ieee80211_channel *channel, 2642 TP_PROTO(struct wiphy *wiphy, struct ieee80211_channel *channel,
2638 const u8 *bssid, const u8 *ssid, size_t ssid_len, 2643 const u8 *bssid, const u8 *ssid, size_t ssid_len,
2639 u16 capa_mask, u16 capa_val), 2644 enum ieee80211_bss_type bss_type,
2640 TP_ARGS(wiphy, channel, bssid, ssid, ssid_len, capa_mask, capa_val), 2645 enum ieee80211_privacy privacy),
2646 TP_ARGS(wiphy, channel, bssid, ssid, ssid_len, bss_type, privacy),
2641 TP_STRUCT__entry( 2647 TP_STRUCT__entry(
2642 WIPHY_ENTRY 2648 WIPHY_ENTRY
2643 CHAN_ENTRY 2649 CHAN_ENTRY
2644 MAC_ENTRY(bssid) 2650 MAC_ENTRY(bssid)
2645 __dynamic_array(u8, ssid, ssid_len) 2651 __dynamic_array(u8, ssid, ssid_len)
2646 __field(u16, capa_mask) 2652 __field(enum ieee80211_bss_type, bss_type)
2647 __field(u16, capa_val) 2653 __field(enum ieee80211_privacy, privacy)
2648 ), 2654 ),
2649 TP_fast_assign( 2655 TP_fast_assign(
2650 WIPHY_ASSIGN; 2656 WIPHY_ASSIGN;
2651 CHAN_ASSIGN(channel); 2657 CHAN_ASSIGN(channel);
2652 MAC_ASSIGN(bssid, bssid); 2658 MAC_ASSIGN(bssid, bssid);
2653 memcpy(__get_dynamic_array(ssid), ssid, ssid_len); 2659 memcpy(__get_dynamic_array(ssid), ssid, ssid_len);
2654 __entry->capa_mask = capa_mask; 2660 __entry->bss_type = bss_type;
2655 __entry->capa_val = capa_val; 2661 __entry->privacy = privacy;
2656 ), 2662 ),
2657 TP_printk(WIPHY_PR_FMT ", " CHAN_PR_FMT ", " MAC_PR_FMT ", buf: %#.2x, " 2663 TP_printk(WIPHY_PR_FMT ", " CHAN_PR_FMT ", " MAC_PR_FMT
2658 "capa_mask: %d, capa_val: %u", WIPHY_PR_ARG, CHAN_PR_ARG, 2664 ", buf: %#.2x, bss_type: %d, privacy: %d",
2659 MAC_PR_ARG(bssid), ((u8 *)__get_dynamic_array(ssid))[0], 2665 WIPHY_PR_ARG, CHAN_PR_ARG, MAC_PR_ARG(bssid),
2660 __entry->capa_mask, __entry->capa_val) 2666 ((u8 *)__get_dynamic_array(ssid))[0], __entry->bss_type,
2667 __entry->privacy)
2661); 2668);
2662 2669
2663TRACE_EVENT(cfg80211_inform_bss_width_frame, 2670TRACE_EVENT(cfg80211_inform_bss_width_frame,
diff --git a/net/wireless/util.c b/net/wireless/util.c
index 6903dbdcb8c1..70051ab52f4f 100644
--- a/net/wireless/util.c
+++ b/net/wireless/util.c
@@ -1290,12 +1290,54 @@ int cfg80211_get_p2p_attr(const u8 *ies, unsigned int len,
1290} 1290}
1291EXPORT_SYMBOL(cfg80211_get_p2p_attr); 1291EXPORT_SYMBOL(cfg80211_get_p2p_attr);
1292 1292
1293static bool ieee80211_id_in_list(const u8 *ids, int n_ids, u8 id)
1294{
1295 int i;
1296
1297 for (i = 0; i < n_ids; i++)
1298 if (ids[i] == id)
1299 return true;
1300 return false;
1301}
1302
1303size_t ieee80211_ie_split_ric(const u8 *ies, size_t ielen,
1304 const u8 *ids, int n_ids,
1305 const u8 *after_ric, int n_after_ric,
1306 size_t offset)
1307{
1308 size_t pos = offset;
1309
1310 while (pos < ielen && ieee80211_id_in_list(ids, n_ids, ies[pos])) {
1311 if (ies[pos] == WLAN_EID_RIC_DATA && n_after_ric) {
1312 pos += 2 + ies[pos + 1];
1313
1314 while (pos < ielen &&
1315 !ieee80211_id_in_list(after_ric, n_after_ric,
1316 ies[pos]))
1317 pos += 2 + ies[pos + 1];
1318 } else {
1319 pos += 2 + ies[pos + 1];
1320 }
1321 }
1322
1323 return pos;
1324}
1325EXPORT_SYMBOL(ieee80211_ie_split_ric);
1326
1327size_t ieee80211_ie_split(const u8 *ies, size_t ielen,
1328 const u8 *ids, int n_ids, size_t offset)
1329{
1330 return ieee80211_ie_split_ric(ies, ielen, ids, n_ids, NULL, 0, offset);
1331}
1332EXPORT_SYMBOL(ieee80211_ie_split);
1333
1293bool ieee80211_operating_class_to_band(u8 operating_class, 1334bool ieee80211_operating_class_to_band(u8 operating_class,
1294 enum ieee80211_band *band) 1335 enum ieee80211_band *band)
1295{ 1336{
1296 switch (operating_class) { 1337 switch (operating_class) {
1297 case 112: 1338 case 112:
1298 case 115 ... 127: 1339 case 115 ... 127:
1340 case 128 ... 130:
1299 *band = IEEE80211_BAND_5GHZ; 1341 *band = IEEE80211_BAND_5GHZ;
1300 return true; 1342 return true;
1301 case 81: 1343 case 81:
@@ -1313,6 +1355,135 @@ bool ieee80211_operating_class_to_band(u8 operating_class,
1313} 1355}
1314EXPORT_SYMBOL(ieee80211_operating_class_to_band); 1356EXPORT_SYMBOL(ieee80211_operating_class_to_band);
1315 1357
1358bool ieee80211_chandef_to_operating_class(struct cfg80211_chan_def *chandef,
1359 u8 *op_class)
1360{
1361 u8 vht_opclass;
1362 u16 freq = chandef->center_freq1;
1363
1364 if (freq >= 2412 && freq <= 2472) {
1365 if (chandef->width > NL80211_CHAN_WIDTH_40)
1366 return false;
1367
1368 /* 2.407 GHz, channels 1..13 */
1369 if (chandef->width == NL80211_CHAN_WIDTH_40) {
1370 if (freq > chandef->chan->center_freq)
1371 *op_class = 83; /* HT40+ */
1372 else
1373 *op_class = 84; /* HT40- */
1374 } else {
1375 *op_class = 81;
1376 }
1377
1378 return true;
1379 }
1380
1381 if (freq == 2484) {
1382 if (chandef->width > NL80211_CHAN_WIDTH_40)
1383 return false;
1384
1385 *op_class = 82; /* channel 14 */
1386 return true;
1387 }
1388
1389 switch (chandef->width) {
1390 case NL80211_CHAN_WIDTH_80:
1391 vht_opclass = 128;
1392 break;
1393 case NL80211_CHAN_WIDTH_160:
1394 vht_opclass = 129;
1395 break;
1396 case NL80211_CHAN_WIDTH_80P80:
1397 vht_opclass = 130;
1398 break;
1399 case NL80211_CHAN_WIDTH_10:
1400 case NL80211_CHAN_WIDTH_5:
1401 return false; /* unsupported for now */
1402 default:
1403 vht_opclass = 0;
1404 break;
1405 }
1406
1407 /* 5 GHz, channels 36..48 */
1408 if (freq >= 5180 && freq <= 5240) {
1409 if (vht_opclass) {
1410 *op_class = vht_opclass;
1411 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1412 if (freq > chandef->chan->center_freq)
1413 *op_class = 116;
1414 else
1415 *op_class = 117;
1416 } else {
1417 *op_class = 115;
1418 }
1419
1420 return true;
1421 }
1422
1423 /* 5 GHz, channels 52..64 */
1424 if (freq >= 5260 && freq <= 5320) {
1425 if (vht_opclass) {
1426 *op_class = vht_opclass;
1427 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1428 if (freq > chandef->chan->center_freq)
1429 *op_class = 119;
1430 else
1431 *op_class = 120;
1432 } else {
1433 *op_class = 118;
1434 }
1435
1436 return true;
1437 }
1438
1439 /* 5 GHz, channels 100..144 */
1440 if (freq >= 5500 && freq <= 5720) {
1441 if (vht_opclass) {
1442 *op_class = vht_opclass;
1443 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1444 if (freq > chandef->chan->center_freq)
1445 *op_class = 122;
1446 else
1447 *op_class = 123;
1448 } else {
1449 *op_class = 121;
1450 }
1451
1452 return true;
1453 }
1454
1455 /* 5 GHz, channels 149..169 */
1456 if (freq >= 5745 && freq <= 5845) {
1457 if (vht_opclass) {
1458 *op_class = vht_opclass;
1459 } else if (chandef->width == NL80211_CHAN_WIDTH_40) {
1460 if (freq > chandef->chan->center_freq)
1461 *op_class = 126;
1462 else
1463 *op_class = 127;
1464 } else if (freq <= 5805) {
1465 *op_class = 124;
1466 } else {
1467 *op_class = 125;
1468 }
1469
1470 return true;
1471 }
1472
1473 /* 56.16 GHz, channel 1..4 */
1474 if (freq >= 56160 + 2160 * 1 && freq <= 56160 + 2160 * 4) {
1475 if (chandef->width >= NL80211_CHAN_WIDTH_40)
1476 return false;
1477
1478 *op_class = 180;
1479 return true;
1480 }
1481
1482 /* not supported yet */
1483 return false;
1484}
1485EXPORT_SYMBOL(ieee80211_chandef_to_operating_class);
1486
1316int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev, 1487int cfg80211_validate_beacon_int(struct cfg80211_registered_device *rdev,
1317 u32 beacon_int) 1488 u32 beacon_int)
1318{ 1489{
diff --git a/net/wireless/wext-compat.c b/net/wireless/wext-compat.c
index 5b24d39d7903..fff1bef6ed6d 100644
--- a/net/wireless/wext-compat.c
+++ b/net/wireless/wext-compat.c
@@ -63,7 +63,7 @@ int cfg80211_wext_giwname(struct net_device *dev,
63 63
64 return 0; 64 return 0;
65} 65}
66EXPORT_SYMBOL_GPL(cfg80211_wext_giwname); 66EXPORT_WEXT_HANDLER(cfg80211_wext_giwname);
67 67
68int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info, 68int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
69 u32 *mode, char *extra) 69 u32 *mode, char *extra)
@@ -99,7 +99,7 @@ int cfg80211_wext_siwmode(struct net_device *dev, struct iw_request_info *info,
99 99
100 return cfg80211_change_iface(rdev, dev, type, NULL, &vifparams); 100 return cfg80211_change_iface(rdev, dev, type, NULL, &vifparams);
101} 101}
102EXPORT_SYMBOL_GPL(cfg80211_wext_siwmode); 102EXPORT_WEXT_HANDLER(cfg80211_wext_siwmode);
103 103
104int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info, 104int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
105 u32 *mode, char *extra) 105 u32 *mode, char *extra)
@@ -134,7 +134,7 @@ int cfg80211_wext_giwmode(struct net_device *dev, struct iw_request_info *info,
134 } 134 }
135 return 0; 135 return 0;
136} 136}
137EXPORT_SYMBOL_GPL(cfg80211_wext_giwmode); 137EXPORT_WEXT_HANDLER(cfg80211_wext_giwmode);
138 138
139 139
140int cfg80211_wext_giwrange(struct net_device *dev, 140int cfg80211_wext_giwrange(struct net_device *dev,
@@ -248,7 +248,7 @@ int cfg80211_wext_giwrange(struct net_device *dev,
248 248
249 return 0; 249 return 0;
250} 250}
251EXPORT_SYMBOL_GPL(cfg80211_wext_giwrange); 251EXPORT_WEXT_HANDLER(cfg80211_wext_giwrange);
252 252
253 253
254/** 254/**
@@ -303,7 +303,7 @@ int cfg80211_wext_siwrts(struct net_device *dev,
303 303
304 return err; 304 return err;
305} 305}
306EXPORT_SYMBOL_GPL(cfg80211_wext_siwrts); 306EXPORT_WEXT_HANDLER(cfg80211_wext_siwrts);
307 307
308int cfg80211_wext_giwrts(struct net_device *dev, 308int cfg80211_wext_giwrts(struct net_device *dev,
309 struct iw_request_info *info, 309 struct iw_request_info *info,
@@ -317,7 +317,7 @@ int cfg80211_wext_giwrts(struct net_device *dev,
317 317
318 return 0; 318 return 0;
319} 319}
320EXPORT_SYMBOL_GPL(cfg80211_wext_giwrts); 320EXPORT_WEXT_HANDLER(cfg80211_wext_giwrts);
321 321
322int cfg80211_wext_siwfrag(struct net_device *dev, 322int cfg80211_wext_siwfrag(struct net_device *dev,
323 struct iw_request_info *info, 323 struct iw_request_info *info,
@@ -343,7 +343,7 @@ int cfg80211_wext_siwfrag(struct net_device *dev,
343 343
344 return err; 344 return err;
345} 345}
346EXPORT_SYMBOL_GPL(cfg80211_wext_siwfrag); 346EXPORT_WEXT_HANDLER(cfg80211_wext_siwfrag);
347 347
348int cfg80211_wext_giwfrag(struct net_device *dev, 348int cfg80211_wext_giwfrag(struct net_device *dev,
349 struct iw_request_info *info, 349 struct iw_request_info *info,
@@ -357,7 +357,7 @@ int cfg80211_wext_giwfrag(struct net_device *dev,
357 357
358 return 0; 358 return 0;
359} 359}
360EXPORT_SYMBOL_GPL(cfg80211_wext_giwfrag); 360EXPORT_WEXT_HANDLER(cfg80211_wext_giwfrag);
361 361
362static int cfg80211_wext_siwretry(struct net_device *dev, 362static int cfg80211_wext_siwretry(struct net_device *dev,
363 struct iw_request_info *info, 363 struct iw_request_info *info,
@@ -427,7 +427,7 @@ int cfg80211_wext_giwretry(struct net_device *dev,
427 427
428 return 0; 428 return 0;
429} 429}
430EXPORT_SYMBOL_GPL(cfg80211_wext_giwretry); 430EXPORT_WEXT_HANDLER(cfg80211_wext_giwretry);
431 431
432static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev, 432static int __cfg80211_set_encryption(struct cfg80211_registered_device *rdev,
433 struct net_device *dev, bool pairwise, 433 struct net_device *dev, bool pairwise,
diff --git a/net/wireless/wext-compat.h b/net/wireless/wext-compat.h
index ebcacca2f731..94c7405a5413 100644
--- a/net/wireless/wext-compat.h
+++ b/net/wireless/wext-compat.h
@@ -4,6 +4,12 @@
4#include <net/iw_handler.h> 4#include <net/iw_handler.h>
5#include <linux/wireless.h> 5#include <linux/wireless.h>
6 6
7#ifdef CONFIG_CFG80211_WEXT_EXPORT
8#define EXPORT_WEXT_HANDLER(h) EXPORT_SYMBOL_GPL(h)
9#else
10#define EXPORT_WEXT_HANDLER(h)
11#endif /* CONFIG_CFG80211_WEXT_EXPORT */
12
7int cfg80211_ibss_wext_siwfreq(struct net_device *dev, 13int cfg80211_ibss_wext_siwfreq(struct net_device *dev,
8 struct iw_request_info *info, 14 struct iw_request_info *info,
9 struct iw_freq *freq, char *extra); 15 struct iw_freq *freq, char *extra);
diff --git a/net/wireless/wext-sme.c b/net/wireless/wext-sme.c
index 368611c05739..a4e8af3321d2 100644
--- a/net/wireless/wext-sme.c
+++ b/net/wireless/wext-sme.c
@@ -322,7 +322,7 @@ int cfg80211_mgd_wext_giwap(struct net_device *dev,
322 if (wdev->current_bss) 322 if (wdev->current_bss)
323 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN); 323 memcpy(ap_addr->sa_data, wdev->current_bss->pub.bssid, ETH_ALEN);
324 else 324 else
325 memset(ap_addr->sa_data, 0, ETH_ALEN); 325 eth_zero_addr(ap_addr->sa_data);
326 wdev_unlock(wdev); 326 wdev_unlock(wdev);
327 327
328 return 0; 328 return 0;
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
index d9149b68b9bc..c3ab230e4493 100644
--- a/net/x25/af_x25.c
+++ b/net/x25/af_x25.c
@@ -1077,8 +1077,7 @@ out_clear_request:
1077 goto out; 1077 goto out;
1078} 1078}
1079 1079
1080static int x25_sendmsg(struct kiocb *iocb, struct socket *sock, 1080static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
1081 struct msghdr *msg, size_t len)
1082{ 1081{
1083 struct sock *sk = sock->sk; 1082 struct sock *sk = sock->sk;
1084 struct x25_sock *x25 = x25_sk(sk); 1083 struct x25_sock *x25 = x25_sk(sk);
@@ -1252,8 +1251,7 @@ out_kfree_skb:
1252} 1251}
1253 1252
1254 1253
1255static int x25_recvmsg(struct kiocb *iocb, struct socket *sock, 1254static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
1256 struct msghdr *msg, size_t size,
1257 int flags) 1255 int flags)
1258{ 1256{
1259 struct sock *sk = sock->sk; 1257 struct sock *sk = sock->sk;
diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
index 85d1d4764612..526c4feb3b50 100644
--- a/net/xfrm/xfrm_input.c
+++ b/net/xfrm/xfrm_input.c
@@ -238,11 +238,6 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
238 238
239 skb->sp->xvec[skb->sp->len++] = x; 239 skb->sp->xvec[skb->sp->len++] = x;
240 240
241 if (xfrm_tunnel_check(skb, x, family)) {
242 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
243 goto drop;
244 }
245
246 spin_lock(&x->lock); 241 spin_lock(&x->lock);
247 if (unlikely(x->km.state == XFRM_STATE_ACQ)) { 242 if (unlikely(x->km.state == XFRM_STATE_ACQ)) {
248 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR); 243 XFRM_INC_STATS(net, LINUX_MIB_XFRMACQUIREERROR);
@@ -271,6 +266,11 @@ int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type)
271 266
272 spin_unlock(&x->lock); 267 spin_unlock(&x->lock);
273 268
269 if (xfrm_tunnel_check(skb, x, family)) {
270 XFRM_INC_STATS(net, LINUX_MIB_XFRMINSTATEMODEERROR);
271 goto drop;
272 }
273
274 seq_hi = htonl(xfrm_replay_seqhi(x, seq)); 274 seq_hi = htonl(xfrm_replay_seqhi(x, seq));
275 275
276 XFRM_SKB_CB(skb)->seq.input.low = seq; 276 XFRM_SKB_CB(skb)->seq.input.low = seq;
diff --git a/net/xfrm/xfrm_output.c b/net/xfrm/xfrm_output.c
index 7c532856b398..fbcedbe33190 100644
--- a/net/xfrm/xfrm_output.c
+++ b/net/xfrm/xfrm_output.c
@@ -19,7 +19,7 @@
19#include <net/dst.h> 19#include <net/dst.h>
20#include <net/xfrm.h> 20#include <net/xfrm.h>
21 21
22static int xfrm_output2(struct sk_buff *skb); 22static int xfrm_output2(struct sock *sk, struct sk_buff *skb);
23 23
24static int xfrm_skb_check_space(struct sk_buff *skb) 24static int xfrm_skb_check_space(struct sk_buff *skb)
25{ 25{
@@ -130,7 +130,7 @@ int xfrm_output_resume(struct sk_buff *skb, int err)
130 return dst_output(skb); 130 return dst_output(skb);
131 131
132 err = nf_hook(skb_dst(skb)->ops->family, 132 err = nf_hook(skb_dst(skb)->ops->family,
133 NF_INET_POST_ROUTING, skb, 133 NF_INET_POST_ROUTING, skb->sk, skb,
134 NULL, skb_dst(skb)->dev, xfrm_output2); 134 NULL, skb_dst(skb)->dev, xfrm_output2);
135 if (unlikely(err != 1)) 135 if (unlikely(err != 1))
136 goto out; 136 goto out;
@@ -144,12 +144,12 @@ out:
144} 144}
145EXPORT_SYMBOL_GPL(xfrm_output_resume); 145EXPORT_SYMBOL_GPL(xfrm_output_resume);
146 146
147static int xfrm_output2(struct sk_buff *skb) 147static int xfrm_output2(struct sock *sk, struct sk_buff *skb)
148{ 148{
149 return xfrm_output_resume(skb, 1); 149 return xfrm_output_resume(skb, 1);
150} 150}
151 151
152static int xfrm_output_gso(struct sk_buff *skb) 152static int xfrm_output_gso(struct sock *sk, struct sk_buff *skb)
153{ 153{
154 struct sk_buff *segs; 154 struct sk_buff *segs;
155 155
@@ -165,7 +165,7 @@ static int xfrm_output_gso(struct sk_buff *skb)
165 int err; 165 int err;
166 166
167 segs->next = NULL; 167 segs->next = NULL;
168 err = xfrm_output2(segs); 168 err = xfrm_output2(sk, segs);
169 169
170 if (unlikely(err)) { 170 if (unlikely(err)) {
171 kfree_skb_list(nskb); 171 kfree_skb_list(nskb);
@@ -178,13 +178,13 @@ static int xfrm_output_gso(struct sk_buff *skb)
178 return 0; 178 return 0;
179} 179}
180 180
181int xfrm_output(struct sk_buff *skb) 181int xfrm_output(struct sock *sk, struct sk_buff *skb)
182{ 182{
183 struct net *net = dev_net(skb_dst(skb)->dev); 183 struct net *net = dev_net(skb_dst(skb)->dev);
184 int err; 184 int err;
185 185
186 if (skb_is_gso(skb)) 186 if (skb_is_gso(skb))
187 return xfrm_output_gso(skb); 187 return xfrm_output_gso(sk, skb);
188 188
189 if (skb->ip_summed == CHECKSUM_PARTIAL) { 189 if (skb->ip_summed == CHECKSUM_PARTIAL) {
190 err = skb_checksum_help(skb); 190 err = skb_checksum_help(skb);
@@ -195,7 +195,7 @@ int xfrm_output(struct sk_buff *skb)
195 } 195 }
196 } 196 }
197 197
198 return xfrm_output2(skb); 198 return xfrm_output2(sk, skb);
199} 199}
200EXPORT_SYMBOL_GPL(xfrm_output); 200EXPORT_SYMBOL_GPL(xfrm_output);
201 201
diff --git a/net/xfrm/xfrm_state.c b/net/xfrm/xfrm_state.c
index de971b6d38c5..f5e39e35d73a 100644
--- a/net/xfrm/xfrm_state.c
+++ b/net/xfrm/xfrm_state.c
@@ -1043,12 +1043,12 @@ static struct xfrm_state *__find_acq_core(struct net *net,
1043 break; 1043 break;
1044 1044
1045 case AF_INET6: 1045 case AF_INET6:
1046 *(struct in6_addr *)x->sel.daddr.a6 = *(struct in6_addr *)daddr; 1046 x->sel.daddr.in6 = daddr->in6;
1047 *(struct in6_addr *)x->sel.saddr.a6 = *(struct in6_addr *)saddr; 1047 x->sel.saddr.in6 = saddr->in6;
1048 x->sel.prefixlen_d = 128; 1048 x->sel.prefixlen_d = 128;
1049 x->sel.prefixlen_s = 128; 1049 x->sel.prefixlen_s = 128;
1050 *(struct in6_addr *)x->props.saddr.a6 = *(struct in6_addr *)saddr; 1050 x->props.saddr.in6 = saddr->in6;
1051 *(struct in6_addr *)x->id.daddr.a6 = *(struct in6_addr *)daddr; 1051 x->id.daddr.in6 = daddr->in6;
1052 break; 1052 break;
1053 } 1053 }
1054 1054
diff --git a/net/xfrm/xfrm_user.c b/net/xfrm/xfrm_user.c
index 7de2ed9ec46d..2091664295ba 100644
--- a/net/xfrm/xfrm_user.c
+++ b/net/xfrm/xfrm_user.c
@@ -2423,6 +2423,11 @@ static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2423 const struct xfrm_link *link; 2423 const struct xfrm_link *link;
2424 int type, err; 2424 int type, err;
2425 2425
2426#ifdef CONFIG_COMPAT
2427 if (is_compat_task())
2428 return -ENOTSUPP;
2429#endif
2430
2426 type = nlh->nlmsg_type; 2431 type = nlh->nlmsg_type;
2427 if (type > XFRM_MSG_MAX) 2432 if (type > XFRM_MSG_MAX)
2428 return -EINVAL; 2433 return -EINVAL;