2222
2323static int __hw_addr_create_ex (struct netdev_hw_addr_list * list ,
2424 const unsigned char * addr , int addr_len ,
25- unsigned char addr_type , bool global )
25+ unsigned char addr_type , bool global ,
26+ bool sync )
2627{
2728 struct netdev_hw_addr * ha ;
2829 int alloc_size ;
@@ -37,7 +38,7 @@ static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
3738 ha -> type = addr_type ;
3839 ha -> refcount = 1 ;
3940 ha -> global_use = global ;
40- ha -> synced = 0 ;
41+ ha -> synced = sync ;
4142 list_add_tail_rcu (& ha -> list , & list -> list );
4243 list -> count ++ ;
4344
@@ -46,7 +47,7 @@ static int __hw_addr_create_ex(struct netdev_hw_addr_list *list,
4647
4748static int __hw_addr_add_ex (struct netdev_hw_addr_list * list ,
4849 const unsigned char * addr , int addr_len ,
49- unsigned char addr_type , bool global )
50+ unsigned char addr_type , bool global , bool sync )
5051{
5152 struct netdev_hw_addr * ha ;
5253
@@ -63,43 +64,62 @@ static int __hw_addr_add_ex(struct netdev_hw_addr_list *list,
6364 else
6465 ha -> global_use = true;
6566 }
67+ if (sync ) {
68+ if (ha -> synced )
69+ return 0 ;
70+ else
71+ ha -> synced = true;
72+ }
6673 ha -> refcount ++ ;
6774 return 0 ;
6875 }
6976 }
7077
71- return __hw_addr_create_ex (list , addr , addr_len , addr_type , global );
78+ return __hw_addr_create_ex (list , addr , addr_len , addr_type , global ,
79+ sync );
7280}
7381
7482static int __hw_addr_add (struct netdev_hw_addr_list * list ,
7583 const unsigned char * addr , int addr_len ,
7684 unsigned char addr_type )
7785{
78- return __hw_addr_add_ex (list , addr , addr_len , addr_type , false);
86+ return __hw_addr_add_ex (list , addr , addr_len , addr_type , false, false);
87+ }
88+
89+ static int __hw_addr_del_entry (struct netdev_hw_addr_list * list ,
90+ struct netdev_hw_addr * ha , bool global ,
91+ bool sync )
92+ {
93+ if (global && !ha -> global_use )
94+ return - ENOENT ;
95+
96+ if (sync && !ha -> synced )
97+ return - ENOENT ;
98+
99+ if (global )
100+ ha -> global_use = false;
101+
102+ if (sync )
103+ ha -> synced = false;
104+
105+ if (-- ha -> refcount )
106+ return 0 ;
107+ list_del_rcu (& ha -> list );
108+ kfree_rcu (ha , rcu_head );
109+ list -> count -- ;
110+ return 0 ;
79111}
80112
81113static int __hw_addr_del_ex (struct netdev_hw_addr_list * list ,
82114 const unsigned char * addr , int addr_len ,
83- unsigned char addr_type , bool global )
115+ unsigned char addr_type , bool global , bool sync )
84116{
85117 struct netdev_hw_addr * ha ;
86118
87119 list_for_each_entry (ha , & list -> list , list ) {
88120 if (!memcmp (ha -> addr , addr , addr_len ) &&
89- (ha -> type == addr_type || !addr_type )) {
90- if (global ) {
91- if (!ha -> global_use )
92- break ;
93- else
94- ha -> global_use = false;
95- }
96- if (-- ha -> refcount )
97- return 0 ;
98- list_del_rcu (& ha -> list );
99- kfree_rcu (ha , rcu_head );
100- list -> count -- ;
101- return 0 ;
102- }
121+ (ha -> type == addr_type || !addr_type ))
122+ return __hw_addr_del_entry (list , ha , global , sync );
103123 }
104124 return - ENOENT ;
105125}
@@ -108,7 +128,57 @@ static int __hw_addr_del(struct netdev_hw_addr_list *list,
108128 const unsigned char * addr , int addr_len ,
109129 unsigned char addr_type )
110130{
111- return __hw_addr_del_ex (list , addr , addr_len , addr_type , false);
131+ return __hw_addr_del_ex (list , addr , addr_len , addr_type , false, false);
132+ }
133+
134+ static int __hw_addr_sync_one (struct netdev_hw_addr_list * to_list ,
135+ struct netdev_hw_addr * ha ,
136+ int addr_len )
137+ {
138+ int err ;
139+
140+ err = __hw_addr_add_ex (to_list , ha -> addr , addr_len , ha -> type ,
141+ false, true);
142+ if (err )
143+ return err ;
144+ ha -> sync_cnt ++ ;
145+ ha -> refcount ++ ;
146+
147+ return 0 ;
148+ }
149+
150+ static void __hw_addr_unsync_one (struct netdev_hw_addr_list * to_list ,
151+ struct netdev_hw_addr_list * from_list ,
152+ struct netdev_hw_addr * ha ,
153+ int addr_len )
154+ {
155+ int err ;
156+
157+ err = __hw_addr_del_ex (to_list , ha -> addr , addr_len , ha -> type ,
158+ false, true);
159+ if (err )
160+ return ;
161+ ha -> sync_cnt -- ;
162+ __hw_addr_del_entry (from_list , ha , false, true);
163+ }
164+
165+ static int __hw_addr_sync_multiple (struct netdev_hw_addr_list * to_list ,
166+ struct netdev_hw_addr_list * from_list ,
167+ int addr_len )
168+ {
169+ int err = 0 ;
170+ struct netdev_hw_addr * ha , * tmp ;
171+
172+ list_for_each_entry_safe (ha , tmp , & from_list -> list , list ) {
173+ if (ha -> sync_cnt == ha -> refcount ) {
174+ __hw_addr_unsync_one (to_list , from_list , ha , addr_len );
175+ } else {
176+ err = __hw_addr_sync_one (to_list , ha , addr_len );
177+ if (err )
178+ break ;
179+ }
180+ }
181+ return err ;
112182}
113183
114184int __hw_addr_add_multiple (struct netdev_hw_addr_list * to_list ,
@@ -152,6 +222,11 @@ void __hw_addr_del_multiple(struct netdev_hw_addr_list *to_list,
152222}
153223EXPORT_SYMBOL (__hw_addr_del_multiple );
154224
225+ /* This function only works where there is a strict 1-1 relationship
226+ * between source and destionation of they synch. If you ever need to
227+ * sync addresses to more then 1 destination, you need to use
228+ * __hw_addr_sync_multiple().
229+ */
155230int __hw_addr_sync (struct netdev_hw_addr_list * to_list ,
156231 struct netdev_hw_addr_list * from_list ,
157232 int addr_len )
@@ -160,17 +235,12 @@ int __hw_addr_sync(struct netdev_hw_addr_list *to_list,
160235 struct netdev_hw_addr * ha , * tmp ;
161236
162237 list_for_each_entry_safe (ha , tmp , & from_list -> list , list ) {
163- if (!ha -> synced ) {
164- err = __hw_addr_add (to_list , ha -> addr ,
165- addr_len , ha -> type );
238+ if (!ha -> sync_cnt ) {
239+ err = __hw_addr_sync_one (to_list , ha , addr_len );
166240 if (err )
167241 break ;
168- ha -> synced ++ ;
169- ha -> refcount ++ ;
170- } else if (ha -> refcount == 1 ) {
171- __hw_addr_del (to_list , ha -> addr , addr_len , ha -> type );
172- __hw_addr_del (from_list , ha -> addr , addr_len , ha -> type );
173- }
242+ } else if (ha -> refcount == 1 )
243+ __hw_addr_unsync_one (to_list , from_list , ha , addr_len );
174244 }
175245 return err ;
176246}
@@ -183,13 +253,8 @@ void __hw_addr_unsync(struct netdev_hw_addr_list *to_list,
183253 struct netdev_hw_addr * ha , * tmp ;
184254
185255 list_for_each_entry_safe (ha , tmp , & from_list -> list , list ) {
186- if (ha -> synced ) {
187- __hw_addr_del (to_list , ha -> addr ,
188- addr_len , ha -> type );
189- ha -> synced -- ;
190- __hw_addr_del (from_list , ha -> addr ,
191- addr_len , ha -> type );
192- }
256+ if (ha -> sync_cnt )
257+ __hw_addr_unsync_one (to_list , from_list , ha , addr_len );
193258 }
194259}
195260EXPORT_SYMBOL (__hw_addr_unsync );
@@ -406,7 +471,7 @@ int dev_uc_add_excl(struct net_device *dev, const unsigned char *addr)
406471 }
407472 }
408473 err = __hw_addr_create_ex (& dev -> uc , addr , dev -> addr_len ,
409- NETDEV_HW_ADDR_T_UNICAST , true);
474+ NETDEV_HW_ADDR_T_UNICAST , true, false );
410475 if (!err )
411476 __dev_set_rx_mode (dev );
412477out :
@@ -469,7 +534,8 @@ EXPORT_SYMBOL(dev_uc_del);
469534 * locked by netif_addr_lock_bh.
470535 *
471536 * This function is intended to be called from the dev->set_rx_mode
472- * function of layered software devices.
537+ * function of layered software devices. This function assumes that
538+ * addresses will only ever be synced to the @to devices and no other.
473539 */
474540int dev_uc_sync (struct net_device * to , struct net_device * from )
475541{
@@ -487,6 +553,36 @@ int dev_uc_sync(struct net_device *to, struct net_device *from)
487553}
488554EXPORT_SYMBOL (dev_uc_sync );
489555
556+ /**
557+ * dev_uc_sync_multiple - Synchronize device's unicast list to another
558+ * device, but allow for multiple calls to sync to multiple devices.
559+ * @to: destination device
560+ * @from: source device
561+ *
562+ * Add newly added addresses to the destination device and release
563+ * addresses that have been deleted from the source. The source device
564+ * must be locked by netif_addr_lock_bh.
565+ *
566+ * This function is intended to be called from the dev->set_rx_mode
567+ * function of layered software devices. It allows for a single source
568+ * device to be synced to multiple destination devices.
569+ */
570+ int dev_uc_sync_multiple (struct net_device * to , struct net_device * from )
571+ {
572+ int err = 0 ;
573+
574+ if (to -> addr_len != from -> addr_len )
575+ return - EINVAL ;
576+
577+ netif_addr_lock_nested (to );
578+ err = __hw_addr_sync_multiple (& to -> uc , & from -> uc , to -> addr_len );
579+ if (!err )
580+ __dev_set_rx_mode (to );
581+ netif_addr_unlock (to );
582+ return err ;
583+ }
584+ EXPORT_SYMBOL (dev_uc_sync_multiple );
585+
490586/**
491587 * dev_uc_unsync - Remove synchronized addresses from the destination device
492588 * @to: destination device
@@ -559,7 +655,7 @@ int dev_mc_add_excl(struct net_device *dev, const unsigned char *addr)
559655 }
560656 }
561657 err = __hw_addr_create_ex (& dev -> mc , addr , dev -> addr_len ,
562- NETDEV_HW_ADDR_T_MULTICAST , true);
658+ NETDEV_HW_ADDR_T_MULTICAST , true, false );
563659 if (!err )
564660 __dev_set_rx_mode (dev );
565661out :
@@ -575,7 +671,7 @@ static int __dev_mc_add(struct net_device *dev, const unsigned char *addr,
575671
576672 netif_addr_lock_bh (dev );
577673 err = __hw_addr_add_ex (& dev -> mc , addr , dev -> addr_len ,
578- NETDEV_HW_ADDR_T_MULTICAST , global );
674+ NETDEV_HW_ADDR_T_MULTICAST , global , false );
579675 if (!err )
580676 __dev_set_rx_mode (dev );
581677 netif_addr_unlock_bh (dev );
@@ -615,7 +711,7 @@ static int __dev_mc_del(struct net_device *dev, const unsigned char *addr,
615711
616712 netif_addr_lock_bh (dev );
617713 err = __hw_addr_del_ex (& dev -> mc , addr , dev -> addr_len ,
618- NETDEV_HW_ADDR_T_MULTICAST , global );
714+ NETDEV_HW_ADDR_T_MULTICAST , global , false );
619715 if (!err )
620716 __dev_set_rx_mode (dev );
621717 netif_addr_unlock_bh (dev );
@@ -678,6 +774,36 @@ int dev_mc_sync(struct net_device *to, struct net_device *from)
678774}
679775EXPORT_SYMBOL (dev_mc_sync );
680776
777+ /**
778+ * dev_mc_sync_multiple - Synchronize device's unicast list to another
779+ * device, but allow for multiple calls to sync to multiple devices.
780+ * @to: destination device
781+ * @from: source device
782+ *
783+ * Add newly added addresses to the destination device and release
784+ * addresses that have no users left. The source device must be
785+ * locked by netif_addr_lock_bh.
786+ *
787+ * This function is intended to be called from the ndo_set_rx_mode
788+ * function of layered software devices. It allows for a single
789+ * source device to be synced to multiple destination devices.
790+ */
791+ int dev_mc_sync_multiple (struct net_device * to , struct net_device * from )
792+ {
793+ int err = 0 ;
794+
795+ if (to -> addr_len != from -> addr_len )
796+ return - EINVAL ;
797+
798+ netif_addr_lock_nested (to );
799+ err = __hw_addr_sync (& to -> mc , & from -> mc , to -> addr_len );
800+ if (!err )
801+ __dev_set_rx_mode (to );
802+ netif_addr_unlock (to );
803+ return err ;
804+ }
805+ EXPORT_SYMBOL (dev_mc_sync_multiple );
806+
681807/**
682808 * dev_mc_unsync - Remove synchronized addresses from the destination device
683809 * @to: destination device
0 commit comments