164164
165165static DEFINE_SPINLOCK (ptype_lock );
166166struct list_head ptype_base [PTYPE_HASH_SIZE ] __read_mostly ;
167- struct list_head ptype_all __read_mostly ; /* Taps */
168167
169168static int netif_rx_internal (struct sk_buff * skb );
170169static int call_netdevice_notifiers_extack (unsigned long val ,
@@ -569,10 +568,18 @@ static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
569568
570569static inline struct list_head * ptype_head (const struct packet_type * pt )
571570{
572- if (pt -> type == htons (ETH_P_ALL ))
573- return pt -> dev ? & pt -> dev -> ptype_all : & ptype_all ;
574- else
575- return pt -> dev ? & pt -> dev -> ptype_specific :
571+ if (pt -> type == htons (ETH_P_ALL )) {
572+ if (!pt -> af_packet_net && !pt -> dev )
573+ return NULL ;
574+
575+ return pt -> dev ? & pt -> dev -> ptype_all :
576+ & pt -> af_packet_net -> ptype_all ;
577+ }
578+
579+ if (pt -> dev )
580+ return & pt -> dev -> ptype_specific ;
581+
582+ return pt -> af_packet_net ? & pt -> af_packet_net -> ptype_specific :
576583 & ptype_base [ntohs (pt -> type ) & PTYPE_HASH_MASK ];
577584}
578585
@@ -593,6 +600,9 @@ void dev_add_pack(struct packet_type *pt)
593600{
594601 struct list_head * head = ptype_head (pt );
595602
603+ if (WARN_ON_ONCE (!head ))
604+ return ;
605+
596606 spin_lock (& ptype_lock );
597607 list_add_rcu (& pt -> list , head );
598608 spin_unlock (& ptype_lock );
@@ -617,6 +627,9 @@ void __dev_remove_pack(struct packet_type *pt)
617627 struct list_head * head = ptype_head (pt );
618628 struct packet_type * pt1 ;
619629
630+ if (!head )
631+ return ;
632+
620633 spin_lock (& ptype_lock );
621634
622635 list_for_each_entry (pt1 , head , list ) {
@@ -2360,15 +2373,21 @@ static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
23602373}
23612374
23622375/**
2363- * dev_nit_active - return true if any network interface taps are in use
2376+ * dev_nit_active_rcu - return true if any network interface taps are in use
2377+ *
2378+ * The caller must hold the RCU lock
23642379 *
23652380 * @dev: network device to check for the presence of taps
23662381 */
2367- bool dev_nit_active ( struct net_device * dev )
2382+ bool dev_nit_active_rcu ( const struct net_device * dev )
23682383{
2369- return !list_empty (& ptype_all ) || !list_empty (& dev -> ptype_all );
2384+ /* Callers may hold either RCU or RCU BH lock */
2385+ WARN_ON_ONCE (!rcu_read_lock_held () && !rcu_read_lock_bh_held ());
2386+
2387+ return !list_empty (& dev_net (dev )-> ptype_all ) ||
2388+ !list_empty (& dev -> ptype_all );
23702389}
2371- EXPORT_SYMBOL_GPL (dev_nit_active );
2390+ EXPORT_SYMBOL_GPL (dev_nit_active_rcu );
23722391
23732392/*
23742393 * Support routine. Sends outgoing frames to any network
@@ -2380,9 +2399,10 @@ void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
23802399 struct packet_type * ptype ;
23812400 struct sk_buff * skb2 = NULL ;
23822401 struct packet_type * pt_prev = NULL ;
2383- struct list_head * ptype_list = & ptype_all ;
2402+ struct list_head * ptype_list ;
23842403
23852404 rcu_read_lock ();
2405+ ptype_list = & dev_net_rcu (dev )-> ptype_all ;
23862406again :
23872407 list_for_each_entry_rcu (ptype , ptype_list , list ) {
23882408 if (READ_ONCE (ptype -> ignore_outgoing ))
@@ -2426,7 +2446,7 @@ void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
24262446 pt_prev = ptype ;
24272447 }
24282448
2429- if (ptype_list == & ptype_all ) {
2449+ if (ptype_list != & dev -> ptype_all ) {
24302450 ptype_list = & dev -> ptype_all ;
24312451 goto again ;
24322452 }
@@ -3640,7 +3660,7 @@ static int xmit_one(struct sk_buff *skb, struct net_device *dev,
36403660 unsigned int len ;
36413661 int rc ;
36423662
3643- if (dev_nit_active (dev ))
3663+ if (dev_nit_active_rcu (dev ))
36443664 dev_queue_xmit_nit (skb , dev );
36453665
36463666 len = skb -> len ;
@@ -5504,7 +5524,8 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
55045524 if (pfmemalloc )
55055525 goto skip_taps ;
55065526
5507- list_for_each_entry_rcu (ptype , & ptype_all , list ) {
5527+ list_for_each_entry_rcu (ptype , & dev_net_rcu (skb -> dev )-> ptype_all ,
5528+ list ) {
55085529 if (pt_prev )
55095530 ret = deliver_skb (skb , pt_prev , orig_dev );
55105531 pt_prev = ptype ;
@@ -5616,6 +5637,14 @@ static int __netif_receive_skb_core(struct sk_buff **pskb, bool pfmemalloc,
56165637 deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
56175638 & ptype_base [ntohs (type ) &
56185639 PTYPE_HASH_MASK ]);
5640+
5641+ /* orig_dev and skb->dev could belong to different netns;
5642+ * Even in such case we need to traverse only the list
5643+ * coming from skb->dev, as the ptype owner (packet socket)
5644+ * will use dev_net(skb->dev) to do namespace filtering.
5645+ */
5646+ deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
5647+ & dev_net_rcu (skb -> dev )-> ptype_specific );
56195648 }
56205649
56215650 deliver_ptype_list_skb (skb , & pt_prev , orig_dev , type ,
@@ -12042,7 +12071,6 @@ static int __init net_dev_init(void)
1204212071 if (netdev_kobject_init ())
1204312072 goto out ;
1204412073
12045- INIT_LIST_HEAD (& ptype_all );
1204612074 for (i = 0 ; i < PTYPE_HASH_SIZE ; i ++ )
1204712075 INIT_LIST_HEAD (& ptype_base [i ]);
1204812076
0 commit comments