@@ -110,47 +110,60 @@ static struct hard_iface *hardif_get_active(struct net_device *soft_iface)
110110 return hard_iface ;
111111}
112112
113- static void update_primary_addr (struct bat_priv * bat_priv )
113+ static void primary_if_update_addr (struct bat_priv * bat_priv )
114114{
115115 struct vis_packet * vis_packet ;
116+ struct hard_iface * primary_if ;
117+
118+ primary_if = primary_if_get_selected (bat_priv );
119+ if (!primary_if )
120+ goto out ;
116121
117122 vis_packet = (struct vis_packet * )
118123 bat_priv -> my_vis_info -> skb_packet -> data ;
119- memcpy (vis_packet -> vis_orig ,
120- bat_priv -> primary_if -> net_dev -> dev_addr , ETH_ALEN );
124+ memcpy (vis_packet -> vis_orig , primary_if -> net_dev -> dev_addr , ETH_ALEN );
121125 memcpy (vis_packet -> sender_orig ,
122- bat_priv -> primary_if -> net_dev -> dev_addr , ETH_ALEN );
126+ primary_if -> net_dev -> dev_addr , ETH_ALEN );
127+
128+ out :
129+ if (primary_if )
130+ hardif_free_ref (primary_if );
123131}
124132
125- static void set_primary_if (struct bat_priv * bat_priv ,
126- struct hard_iface * hard_iface )
133+ static void primary_if_select (struct bat_priv * bat_priv ,
134+ struct hard_iface * new_hard_iface )
127135{
136+ struct hard_iface * curr_hard_iface ;
128137 struct batman_packet * batman_packet ;
129- struct hard_iface * old_if ;
130138
131- if (hard_iface && !atomic_inc_not_zero (& hard_iface -> refcount ))
132- hard_iface = NULL ;
139+ spin_lock_bh (& hardif_list_lock );
133140
134- old_if = bat_priv -> primary_if ;
135- bat_priv -> primary_if = hard_iface ;
141+ if ( new_hard_iface && ! atomic_inc_not_zero ( & new_hard_iface -> refcount ))
142+ new_hard_iface = NULL ;
136143
137- if ( old_if )
138- hardif_free_ref ( old_if );
144+ curr_hard_iface = bat_priv -> primary_if ;
145+ rcu_assign_pointer ( bat_priv -> primary_if , new_hard_iface );
139146
140- if (! bat_priv -> primary_if )
141- return ;
147+ if (curr_hard_iface )
148+ hardif_free_ref ( curr_hard_iface ) ;
142149
143- batman_packet = (struct batman_packet * )(hard_iface -> packet_buff );
150+ if (!new_hard_iface )
151+ goto out ;
152+
153+ batman_packet = (struct batman_packet * )(new_hard_iface -> packet_buff );
144154 batman_packet -> flags = PRIMARIES_FIRST_HOP ;
145155 batman_packet -> ttl = TTL ;
146156
147- update_primary_addr (bat_priv );
157+ primary_if_update_addr (bat_priv );
148158
149159 /***
150160 * hacky trick to make sure that we send the HNA information via
151161 * our new primary interface
152162 */
153163 atomic_set (& bat_priv -> hna_local_changed , 1 );
164+
165+ out :
166+ spin_unlock_bh (& hardif_list_lock );
154167}
155168
156169static bool hardif_is_iface_up (struct hard_iface * hard_iface )
@@ -236,9 +249,10 @@ void update_min_mtu(struct net_device *soft_iface)
236249static void hardif_activate_interface (struct hard_iface * hard_iface )
237250{
238251 struct bat_priv * bat_priv ;
252+ struct hard_iface * primary_if = NULL ;
239253
240254 if (hard_iface -> if_status != IF_INACTIVE )
241- return ;
255+ goto out ;
242256
243257 bat_priv = netdev_priv (hard_iface -> soft_iface );
244258
@@ -249,14 +263,18 @@ static void hardif_activate_interface(struct hard_iface *hard_iface)
249263 * the first active interface becomes our primary interface or
250264 * the next active interface after the old primay interface was removed
251265 */
252- if (!bat_priv -> primary_if )
253- set_primary_if (bat_priv , hard_iface );
266+ primary_if = primary_if_get_selected (bat_priv );
267+ if (!primary_if )
268+ primary_if_select (bat_priv , hard_iface );
254269
255270 bat_info (hard_iface -> soft_iface , "Interface activated: %s\n" ,
256271 hard_iface -> net_dev -> name );
257272
258273 update_min_mtu (hard_iface -> soft_iface );
259- return ;
274+
275+ out :
276+ if (primary_if )
277+ hardif_free_ref (primary_if );
260278}
261279
262280static void hardif_deactivate_interface (struct hard_iface * hard_iface )
@@ -386,12 +404,13 @@ int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
386404void hardif_disable_interface (struct hard_iface * hard_iface )
387405{
388406 struct bat_priv * bat_priv = netdev_priv (hard_iface -> soft_iface );
407+ struct hard_iface * primary_if = NULL ;
389408
390409 if (hard_iface -> if_status == IF_ACTIVE )
391410 hardif_deactivate_interface (hard_iface );
392411
393412 if (hard_iface -> if_status != IF_INACTIVE )
394- return ;
413+ goto out ;
395414
396415 bat_info (hard_iface -> soft_iface , "Removing interface: %s\n" ,
397416 hard_iface -> net_dev -> name );
@@ -400,11 +419,12 @@ void hardif_disable_interface(struct hard_iface *hard_iface)
400419 bat_priv -> num_ifaces -- ;
401420 orig_hash_del_if (hard_iface , bat_priv -> num_ifaces );
402421
403- if (hard_iface == bat_priv -> primary_if ) {
422+ primary_if = primary_if_get_selected (bat_priv );
423+ if (hard_iface == primary_if ) {
404424 struct hard_iface * new_if ;
405425
406426 new_if = hardif_get_active (hard_iface -> soft_iface );
407- set_primary_if (bat_priv , new_if );
427+ primary_if_select (bat_priv , new_if );
408428
409429 if (new_if )
410430 hardif_free_ref (new_if );
@@ -425,6 +445,10 @@ void hardif_disable_interface(struct hard_iface *hard_iface)
425445
426446 hard_iface -> soft_iface = NULL ;
427447 hardif_free_ref (hard_iface );
448+
449+ out :
450+ if (primary_if )
451+ hardif_free_ref (primary_if );
428452}
429453
430454static struct hard_iface * hardif_add_interface (struct net_device * net_dev )
@@ -514,6 +538,7 @@ static int hard_if_event(struct notifier_block *this,
514538{
515539 struct net_device * net_dev = (struct net_device * )ptr ;
516540 struct hard_iface * hard_iface = hardif_get_by_netdev (net_dev );
541+ struct hard_iface * primary_if = NULL ;
517542 struct bat_priv * bat_priv ;
518543
519544 if (!hard_iface && event == NETDEV_REGISTER )
@@ -549,8 +574,12 @@ static int hard_if_event(struct notifier_block *this,
549574 update_mac_addresses (hard_iface );
550575
551576 bat_priv = netdev_priv (hard_iface -> soft_iface );
552- if (hard_iface == bat_priv -> primary_if )
553- update_primary_addr (bat_priv );
577+ primary_if = primary_if_get_selected (bat_priv );
578+ if (!primary_if )
579+ goto hardif_put ;
580+
581+ if (hard_iface == primary_if )
582+ primary_if_update_addr (bat_priv );
554583 break ;
555584 default :
556585 break ;
@@ -559,6 +588,8 @@ static int hard_if_event(struct notifier_block *this,
559588hardif_put :
560589 hardif_free_ref (hard_iface );
561590out :
591+ if (primary_if )
592+ hardif_free_ref (primary_if );
562593 return NOTIFY_DONE ;
563594}
564595
0 commit comments