@@ -570,10 +570,18 @@ static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
570570
571571static inline struct list_head * ptype_head (const struct packet_type * pt )
572572{
573- if (pt -> type == htons (ETH_P_ALL ))
574- return pt -> dev ? & pt -> dev -> ptype_all : & net_hotdata .ptype_all ;
575- else
576- return pt -> dev ? & pt -> dev -> ptype_specific :
573+ if (pt -> type == htons (ETH_P_ALL )) {
574+ if (!pt -> af_packet_net && !pt -> dev )
575+ return NULL ;
576+
577+ return pt -> dev ? & pt -> dev -> ptype_all :
578+ & pt -> af_packet_net -> ptype_all ;
579+ }
580+
581+ if (pt -> dev )
582+ return & pt -> dev -> ptype_specific ;
583+
584+ return pt -> af_packet_net ? & pt -> af_packet_net -> ptype_specific :
577585 & ptype_base [ntohs (pt -> type ) & PTYPE_HASH_MASK ];
578586}
579587
@@ -594,6 +602,9 @@ void dev_add_pack(struct packet_type *pt)
594602{
595603 struct list_head * head = ptype_head (pt );
596604
605+ if (WARN_ON_ONCE (!head ))
606+ return ;
607+
597608 spin_lock (& ptype_lock );
598609 list_add_rcu (& pt -> list , head );
599610 spin_unlock (& ptype_lock );
@@ -618,6 +629,9 @@ void __dev_remove_pack(struct packet_type *pt)
618629 struct list_head * head = ptype_head (pt );
619630 struct packet_type * pt1 ;
620631
632+ if (!head )
633+ return ;
634+
621635 spin_lock (& ptype_lock );
622636
623637 list_for_each_entry (pt1 , head , list ) {
@@ -2332,16 +2346,21 @@ static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
23322346}
23332347
23342348/**
2335- * dev_nit_active - return true if any network interface taps are in use
2349+ * dev_nit_active_rcu - return true if any network interface taps are in use
2350+ *
2351+ * The caller must hold the RCU lock
23362352 *
23372353 * @dev: network device to check for the presence of taps
23382354 */
2339- bool dev_nit_active ( struct net_device * dev )
2355+ bool dev_nit_active_rcu ( const struct net_device * dev )
23402356{
2341- return !list_empty (& net_hotdata .ptype_all ) ||
2357+ /* Callers may hold either RCU or RCU BH lock */
2358+ WARN_ON_ONCE (!rcu_read_lock_held () && !rcu_read_lock_bh_held ());
2359+
2360+ return !list_empty (& dev_net (dev )-> ptype_all ) ||
23422361 !list_empty (& dev -> ptype_all );
23432362}
2344- EXPORT_SYMBOL_GPL (dev_nit_active );
2363+ EXPORT_SYMBOL_GPL (dev_nit_active_rcu );
23452364
23462365/*
23472366 * Support routine. Sends outgoing frames to any network
@@ -2350,11 +2369,12 @@ EXPORT_SYMBOL_GPL(dev_nit_active);
23502369
23512370void dev_queue_xmit_nit (struct sk_buff * skb , struct net_device * dev )
23522371{
2353- struct list_head * ptype_list = & net_hotdata .ptype_all ;
23542372 struct packet_type * ptype , * pt_prev = NULL ;
2373+ struct list_head * ptype_list ;
23552374 struct sk_buff * skb2 = NULL ;
23562375
23572376 rcu_read_lock ();
2377+ ptype_list = & dev_net_rcu (dev )-> ptype_all ;
23582378again :
23592379 list_for_each_entry_rcu (ptype , ptype_list , list ) {
23602380 if (READ_ONCE (ptype -> ignore_outgoing ))
@@ -2398,7 +2418,7 @@ void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
23982418 pt_prev = ptype ;
23992419 }
24002420
2401- if (ptype_list == & net_hotdata . ptype_all ) {
2421+ if (ptype_list != & dev -> ptype_all ) {
24022422 ptype_list = & dev -> ptype_all ;
24032423 goto again ;
24042424 }
@@ -3641,7 +3661,7 @@ static int xmit_one(struct sk_buff *skb, struct net_device *dev,
36413661 unsigned int len ;
36423662 int rc ;
36433663
3644- if (dev_nit_active (dev ))
3664+ if (dev_nit_active_rcu (dev ))
36453665 dev_queue_xmit_nit (skb , dev );
36463666
36473667 len = skb -> len ;
@@ -5578,7 +5598,8 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
55785598 if (pfmemalloc )
55795599 goto skip_taps ;
55805600
5581- list_for_each_entry_rcu (ptype , & net_hotdata .ptype_all , list ) {
5601+ list_for_each_entry_rcu (ptype , & dev_net_rcu (skb -> dev )-> ptype_all ,
5602+ list ) {
55825603 if (pt_prev )
55835604 ret = deliver_skb (skb , pt_prev , orig_dev );
55845605 pt_prev = ptype ;
@@ -5690,6 +5711,14 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
56905711 deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
56915712 & ptype_base [ntohs (type ) &
56925713 PTYPE_HASH_MASK ]);
5714+
5715+ /* orig_dev and skb->dev could belong to different netns;
5716+ * Even in such case we need to traverse only the list
5717+ * coming from skb->dev, as the ptype owner (packet socket)
5718+ * will use dev_net(skb->dev) to do namespace filtering.
5719+ */
5720+ deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
5721+ & dev_net_rcu (skb -> dev )-> ptype_specific );
56935722 }
56945723
56955724 deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
0 commit comments