93
static struct batman_if *get_active_batman_if(struct net_device *soft_iface)
89
static struct hard_iface *hardif_get_active(struct net_device *soft_iface)
95
struct batman_if *batman_if;
91
struct hard_iface *hard_iface;
98
list_for_each_entry_rcu(batman_if, &if_list, list) {
99
if (batman_if->soft_iface != soft_iface)
94
list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
95
if (hard_iface->soft_iface != soft_iface)
102
if (batman_if->if_status == IF_ACTIVE)
98
if (hard_iface->if_status == IF_ACTIVE &&
99
atomic_inc_not_zero(&hard_iface->refcount))
110
kref_get(&batman_if->refcount);
112
106
rcu_read_unlock();
116
static void update_primary_addr(struct bat_priv *bat_priv)
110
static void primary_if_update_addr(struct bat_priv *bat_priv)
118
112
struct vis_packet *vis_packet;
113
struct hard_iface *primary_if;
115
primary_if = primary_if_get_selected(bat_priv);
120
119
vis_packet = (struct vis_packet *)
121
120
bat_priv->my_vis_info->skb_packet->data;
122
memcpy(vis_packet->vis_orig,
123
bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
121
memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
124
122
memcpy(vis_packet->sender_orig,
125
bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
123
primary_if->net_dev->dev_addr, ETH_ALEN);
127
hardif_free_ref(primary_if);
128
static void set_primary_if(struct bat_priv *bat_priv,
129
struct batman_if *batman_if)
130
static void primary_if_select(struct bat_priv *bat_priv,
131
struct hard_iface *new_hard_iface)
133
struct hard_iface *curr_hard_iface;
131
134
struct batman_packet *batman_packet;
132
struct batman_if *old_if;
135
kref_get(&batman_if->refcount);
137
old_if = bat_priv->primary_if;
138
bat_priv->primary_if = batman_if;
141
kref_put(&old_if->refcount, hardif_free_ref);
143
if (!bat_priv->primary_if)
138
if (new_hard_iface && !atomic_inc_not_zero(&new_hard_iface->refcount))
139
new_hard_iface = NULL;
141
curr_hard_iface = bat_priv->primary_if;
142
rcu_assign_pointer(bat_priv->primary_if, new_hard_iface);
145
hardif_free_ref(curr_hard_iface);
146
batman_packet = (struct batman_packet *)(batman_if->packet_buff);
150
batman_packet = (struct batman_packet *)(new_hard_iface->packet_buff);
147
151
batman_packet->flags = PRIMARIES_FIRST_HOP;
148
152
batman_packet->ttl = TTL;
150
update_primary_addr(bat_priv);
154
primary_if_update_addr(bat_priv);
153
* hacky trick to make sure that we send the HNA information via
157
* hacky trick to make sure that we send the TT information via
154
158
* our new primary interface
156
atomic_set(&bat_priv->hna_local_changed, 1);
160
atomic_set(&bat_priv->tt_local_changed, 1);
159
static bool hardif_is_iface_up(struct batman_if *batman_if)
163
static bool hardif_is_iface_up(struct hard_iface *hard_iface)
161
if (batman_if->net_dev->flags & IFF_UP)
165
if (hard_iface->net_dev->flags & IFF_UP)
167
static void update_mac_addresses(struct batman_if *batman_if)
171
static void update_mac_addresses(struct hard_iface *hard_iface)
169
memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
170
batman_if->net_dev->dev_addr, ETH_ALEN);
171
memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
172
batman_if->net_dev->dev_addr, ETH_ALEN);
173
memcpy(((struct batman_packet *)(hard_iface->packet_buff))->orig,
174
hard_iface->net_dev->dev_addr, ETH_ALEN);
175
memcpy(((struct batman_packet *)(hard_iface->packet_buff))->prev_sender,
176
hard_iface->net_dev->dev_addr, ETH_ALEN);
175
179
static void check_known_mac_addr(struct net_device *net_dev)
177
struct batman_if *batman_if;
181
struct hard_iface *hard_iface;
180
list_for_each_entry_rcu(batman_if, &if_list, list) {
181
if ((batman_if->if_status != IF_ACTIVE) &&
182
(batman_if->if_status != IF_TO_BE_ACTIVATED))
185
if (batman_if->net_dev == net_dev)
188
if (!compare_orig(batman_if->net_dev->dev_addr,
184
list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
185
if ((hard_iface->if_status != IF_ACTIVE) &&
186
(hard_iface->if_status != IF_TO_BE_ACTIVATED))
189
if (hard_iface->net_dev == net_dev)
192
if (!compare_eth(hard_iface->net_dev->dev_addr,
192
196
pr_warning("The newly added mac address (%pM) already exists "
193
197
"on: %s\n", net_dev->dev_addr,
194
batman_if->net_dev->name);
198
hard_iface->net_dev->name);
195
199
pr_warning("It is strongly recommended to keep mac addresses "
196
200
"unique to avoid problems!\n");
236
240
soft_iface->mtu = min_mtu;
239
static void hardif_activate_interface(struct batman_if *batman_if)
243
static void hardif_activate_interface(struct hard_iface *hard_iface)
241
245
struct bat_priv *bat_priv;
243
if (batman_if->if_status != IF_INACTIVE)
246
bat_priv = netdev_priv(batman_if->soft_iface);
248
update_mac_addresses(batman_if);
249
batman_if->if_status = IF_TO_BE_ACTIVATED;
246
struct hard_iface *primary_if = NULL;
248
if (hard_iface->if_status != IF_INACTIVE)
251
bat_priv = netdev_priv(hard_iface->soft_iface);
253
update_mac_addresses(hard_iface);
254
hard_iface->if_status = IF_TO_BE_ACTIVATED;
252
257
* the first active interface becomes our primary interface or
253
258
* the next active interface after the old primay interface was removed
255
if (!bat_priv->primary_if)
256
set_primary_if(bat_priv, batman_if);
258
bat_info(batman_if->soft_iface, "Interface activated: %s\n",
259
batman_if->net_dev->name);
261
update_min_mtu(batman_if->soft_iface);
260
primary_if = primary_if_get_selected(bat_priv);
262
primary_if_select(bat_priv, hard_iface);
264
bat_info(hard_iface->soft_iface, "Interface activated: %s\n",
265
hard_iface->net_dev->name);
267
update_min_mtu(hard_iface->soft_iface);
271
hardif_free_ref(primary_if);
265
static void hardif_deactivate_interface(struct batman_if *batman_if)
274
static void hardif_deactivate_interface(struct hard_iface *hard_iface)
267
if ((batman_if->if_status != IF_ACTIVE) &&
268
(batman_if->if_status != IF_TO_BE_ACTIVATED))
276
if ((hard_iface->if_status != IF_ACTIVE) &&
277
(hard_iface->if_status != IF_TO_BE_ACTIVATED))
271
batman_if->if_status = IF_INACTIVE;
273
bat_info(batman_if->soft_iface, "Interface deactivated: %s\n",
274
batman_if->net_dev->name);
276
update_min_mtu(batman_if->soft_iface);
280
hard_iface->if_status = IF_INACTIVE;
282
bat_info(hard_iface->soft_iface, "Interface deactivated: %s\n",
283
hard_iface->net_dev->name);
285
update_min_mtu(hard_iface->soft_iface);
279
int hardif_enable_interface(struct batman_if *batman_if, char *iface_name)
288
int hardif_enable_interface(struct hard_iface *hard_iface, char *iface_name)
281
290
struct bat_priv *bat_priv;
282
291
struct batman_packet *batman_packet;
284
if (batman_if->if_status != IF_NOT_IN_USE)
287
batman_if->soft_iface = dev_get_by_name(&init_net, iface_name);
289
if (!batman_if->soft_iface) {
290
batman_if->soft_iface = softif_create(iface_name);
292
if (!batman_if->soft_iface)
292
struct net_device *soft_iface;
295
if (hard_iface->if_status != IF_NOT_IN_USE)
298
if (!atomic_inc_not_zero(&hard_iface->refcount))
301
soft_iface = dev_get_by_name(&init_net, iface_name);
304
soft_iface = softif_create(iface_name);
295
311
/* dev_get_by_name() increases the reference counter for us */
296
dev_hold(batman_if->soft_iface);
299
bat_priv = netdev_priv(batman_if->soft_iface);
300
batman_if->packet_len = BAT_PACKET_LEN;
301
batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
303
if (!batman_if->packet_buff) {
304
bat_err(batman_if->soft_iface, "Can't add interface packet "
305
"(%s): out of memory\n", batman_if->net_dev->name);
309
batman_packet = (struct batman_packet *)(batman_if->packet_buff);
312
dev_hold(soft_iface);
315
if (!softif_is_valid(soft_iface)) {
316
pr_err("Can't create batman mesh interface %s: "
317
"already exists as regular interface\n",
324
hard_iface->soft_iface = soft_iface;
325
bat_priv = netdev_priv(hard_iface->soft_iface);
326
hard_iface->packet_len = BAT_PACKET_LEN;
327
hard_iface->packet_buff = kmalloc(hard_iface->packet_len, GFP_ATOMIC);
329
if (!hard_iface->packet_buff) {
330
bat_err(hard_iface->soft_iface, "Can't add interface packet "
331
"(%s): out of memory\n", hard_iface->net_dev->name);
336
batman_packet = (struct batman_packet *)(hard_iface->packet_buff);
310
337
batman_packet->packet_type = BAT_PACKET;
311
338
batman_packet->version = COMPAT_VERSION;
312
339
batman_packet->flags = 0;
313
340
batman_packet->ttl = 2;
314
341
batman_packet->tq = TQ_MAX_VALUE;
315
batman_packet->num_hna = 0;
342
batman_packet->num_tt = 0;
317
batman_if->if_num = bat_priv->num_ifaces;
344
hard_iface->if_num = bat_priv->num_ifaces;
318
345
bat_priv->num_ifaces++;
319
batman_if->if_status = IF_INACTIVE;
320
orig_hash_add_if(batman_if, bat_priv->num_ifaces);
322
batman_if->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
323
batman_if->batman_adv_ptype.func = batman_skb_recv;
324
batman_if->batman_adv_ptype.dev = batman_if->net_dev;
325
kref_get(&batman_if->refcount);
326
dev_add_pack(&batman_if->batman_adv_ptype);
328
atomic_set(&batman_if->seqno, 1);
329
atomic_set(&batman_if->frag_seqno, 1);
330
bat_info(batman_if->soft_iface, "Adding interface: %s\n",
331
batman_if->net_dev->name);
333
if (atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
346
hard_iface->if_status = IF_INACTIVE;
347
orig_hash_add_if(hard_iface, bat_priv->num_ifaces);
349
hard_iface->batman_adv_ptype.type = __constant_htons(ETH_P_BATMAN);
350
hard_iface->batman_adv_ptype.func = batman_skb_recv;
351
hard_iface->batman_adv_ptype.dev = hard_iface->net_dev;
352
dev_add_pack(&hard_iface->batman_adv_ptype);
354
atomic_set(&hard_iface->seqno, 1);
355
atomic_set(&hard_iface->frag_seqno, 1);
356
bat_info(hard_iface->soft_iface, "Adding interface: %s\n",
357
hard_iface->net_dev->name);
359
if (atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
334
360
ETH_DATA_LEN + BAT_HEADER_LEN)
335
bat_info(batman_if->soft_iface,
361
bat_info(hard_iface->soft_iface,
336
362
"The MTU of interface %s is too small (%i) to handle "
337
363
"the transport of batman-adv packets. Packets going "
338
364
"over this interface will be fragmented on layer2 "
339
365
"which could impact the performance. Setting the MTU "
340
366
"to %zi would solve the problem.\n",
341
batman_if->net_dev->name, batman_if->net_dev->mtu,
367
hard_iface->net_dev->name, hard_iface->net_dev->mtu,
342
368
ETH_DATA_LEN + BAT_HEADER_LEN);
344
if (!atomic_read(&bat_priv->fragmentation) && batman_if->net_dev->mtu <
370
if (!atomic_read(&bat_priv->fragmentation) && hard_iface->net_dev->mtu <
345
371
ETH_DATA_LEN + BAT_HEADER_LEN)
346
bat_info(batman_if->soft_iface,
372
bat_info(hard_iface->soft_iface,
347
373
"The MTU of interface %s is too small (%i) to handle "
348
374
"the transport of batman-adv packets. If you experience"
349
375
" problems getting traffic through try increasing the "
351
batman_if->net_dev->name, batman_if->net_dev->mtu,
377
hard_iface->net_dev->name, hard_iface->net_dev->mtu,
352
378
ETH_DATA_LEN + BAT_HEADER_LEN);
354
if (hardif_is_iface_up(batman_if))
355
hardif_activate_interface(batman_if);
380
if (hardif_is_iface_up(hard_iface))
381
hardif_activate_interface(hard_iface);
357
bat_err(batman_if->soft_iface, "Not using interface %s "
383
bat_err(hard_iface->soft_iface, "Not using interface %s "
358
384
"(retrying later): interface not active\n",
359
batman_if->net_dev->name);
385
hard_iface->net_dev->name);
361
387
/* begin scheduling originator messages on that interface */
362
schedule_own_packet(batman_if);
388
schedule_own_packet(hard_iface);
394
hardif_free_ref(hard_iface);
371
void hardif_disable_interface(struct batman_if *batman_if)
398
void hardif_disable_interface(struct hard_iface *hard_iface)
373
struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
375
if (batman_if->if_status == IF_ACTIVE)
376
hardif_deactivate_interface(batman_if);
378
if (batman_if->if_status != IF_INACTIVE)
381
bat_info(batman_if->soft_iface, "Removing interface: %s\n",
382
batman_if->net_dev->name);
383
dev_remove_pack(&batman_if->batman_adv_ptype);
384
kref_put(&batman_if->refcount, hardif_free_ref);
400
struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
401
struct hard_iface *primary_if = NULL;
403
if (hard_iface->if_status == IF_ACTIVE)
404
hardif_deactivate_interface(hard_iface);
406
if (hard_iface->if_status != IF_INACTIVE)
409
bat_info(hard_iface->soft_iface, "Removing interface: %s\n",
410
hard_iface->net_dev->name);
411
dev_remove_pack(&hard_iface->batman_adv_ptype);
386
413
bat_priv->num_ifaces--;
387
orig_hash_del_if(batman_if, bat_priv->num_ifaces);
389
if (batman_if == bat_priv->primary_if) {
390
struct batman_if *new_if;
392
new_if = get_active_batman_if(batman_if->soft_iface);
393
set_primary_if(bat_priv, new_if);
414
orig_hash_del_if(hard_iface, bat_priv->num_ifaces);
416
primary_if = primary_if_get_selected(bat_priv);
417
if (hard_iface == primary_if) {
418
struct hard_iface *new_if;
420
new_if = hardif_get_active(hard_iface->soft_iface);
421
primary_if_select(bat_priv, new_if);
396
kref_put(&new_if->refcount, hardif_free_ref);
424
hardif_free_ref(new_if);
399
kfree(batman_if->packet_buff);
400
batman_if->packet_buff = NULL;
401
batman_if->if_status = IF_NOT_IN_USE;
427
kfree(hard_iface->packet_buff);
428
hard_iface->packet_buff = NULL;
429
hard_iface->if_status = IF_NOT_IN_USE;
403
/* delete all references to this batman_if */
431
/* delete all references to this hard_iface */
404
432
purge_orig_ref(bat_priv);
405
purge_outstanding_packets(bat_priv, batman_if);
406
dev_put(batman_if->soft_iface);
433
purge_outstanding_packets(bat_priv, hard_iface);
434
dev_put(hard_iface->soft_iface);
408
436
/* nobody uses this interface anymore */
409
437
if (!bat_priv->num_ifaces)
410
softif_destroy(batman_if->soft_iface);
412
batman_if->soft_iface = NULL;
438
softif_destroy(hard_iface->soft_iface);
440
hard_iface->soft_iface = NULL;
441
hardif_free_ref(hard_iface);
445
hardif_free_ref(primary_if);
415
static struct batman_if *hardif_add_interface(struct net_device *net_dev)
448
static struct hard_iface *hardif_add_interface(struct net_device *net_dev)
417
struct batman_if *batman_if;
450
struct hard_iface *hard_iface;
420
455
ret = is_valid_iface(net_dev);
424
459
dev_hold(net_dev);
426
batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
461
hard_iface = kmalloc(sizeof(struct hard_iface), GFP_ATOMIC);
428
463
pr_err("Can't add interface (%s): out of memory\n",
430
465
goto release_dev;
433
ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
468
ret = sysfs_add_hardif(&hard_iface->hardif_obj, net_dev);
437
batman_if->if_num = -1;
438
batman_if->net_dev = net_dev;
439
batman_if->soft_iface = NULL;
440
batman_if->if_status = IF_NOT_IN_USE;
441
INIT_LIST_HEAD(&batman_if->list);
442
kref_init(&batman_if->refcount);
444
check_known_mac_addr(batman_if->net_dev);
446
spin_lock(&if_list_lock);
447
list_add_tail_rcu(&batman_if->list, &if_list);
448
spin_unlock(&if_list_lock);
472
hard_iface->if_num = -1;
473
hard_iface->net_dev = net_dev;
474
hard_iface->soft_iface = NULL;
475
hard_iface->if_status = IF_NOT_IN_USE;
476
INIT_LIST_HEAD(&hard_iface->list);
450
477
/* extra reference for return */
451
kref_get(&batman_if->refcount);
478
atomic_set(&hard_iface->refcount, 2);
480
check_known_mac_addr(hard_iface->net_dev);
481
list_add_tail_rcu(&hard_iface->list, &hardif_list);
457
488
dev_put(net_dev);
462
static void hardif_remove_interface(struct batman_if *batman_if)
493
static void hardif_remove_interface(struct hard_iface *hard_iface)
464
497
/* first deactivate interface */
465
if (batman_if->if_status != IF_NOT_IN_USE)
466
hardif_disable_interface(batman_if);
498
if (hard_iface->if_status != IF_NOT_IN_USE)
499
hardif_disable_interface(hard_iface);
468
if (batman_if->if_status != IF_NOT_IN_USE)
501
if (hard_iface->if_status != IF_NOT_IN_USE)
471
batman_if->if_status = IF_TO_BE_REMOVED;
472
sysfs_del_hardif(&batman_if->hardif_obj);
473
call_rcu(&batman_if->rcu, hardif_free_rcu);
504
hard_iface->if_status = IF_TO_BE_REMOVED;
505
sysfs_del_hardif(&hard_iface->hardif_obj);
506
hardif_free_ref(hard_iface);
476
509
void hardif_remove_interfaces(void)
478
struct batman_if *batman_if, *batman_if_tmp;
479
struct list_head if_queue;
481
INIT_LIST_HEAD(&if_queue);
483
spin_lock(&if_list_lock);
484
list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list) {
485
list_del_rcu(&batman_if->list);
486
list_add_tail(&batman_if->list, &if_queue);
488
spin_unlock(&if_list_lock);
511
struct hard_iface *hard_iface, *hard_iface_tmp;
491
list_for_each_entry_safe(batman_if, batman_if_tmp, &if_queue, list) {
492
hardif_remove_interface(batman_if);
514
list_for_each_entry_safe(hard_iface, hard_iface_tmp,
515
&hardif_list, list) {
516
list_del_rcu(&hard_iface->list);
517
hardif_remove_interface(hard_iface);
498
523
unsigned long event, void *ptr)
500
525
struct net_device *net_dev = (struct net_device *)ptr;
501
struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
526
struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
527
struct hard_iface *primary_if = NULL;
502
528
struct bat_priv *bat_priv;
504
if (!batman_if && event == NETDEV_REGISTER)
505
batman_if = hardif_add_interface(net_dev);
530
if (!hard_iface && event == NETDEV_REGISTER)
531
hard_iface = hardif_add_interface(net_dev);
512
hardif_activate_interface(batman_if);
538
hardif_activate_interface(hard_iface);
514
540
case NETDEV_GOING_DOWN:
515
541
case NETDEV_DOWN:
516
hardif_deactivate_interface(batman_if);
542
hardif_deactivate_interface(hard_iface);
518
544
case NETDEV_UNREGISTER:
519
spin_lock(&if_list_lock);
520
list_del_rcu(&batman_if->list);
521
spin_unlock(&if_list_lock);
545
list_del_rcu(&hard_iface->list);
523
hardif_remove_interface(batman_if);
547
hardif_remove_interface(hard_iface);
525
549
case NETDEV_CHANGEMTU:
526
if (batman_if->soft_iface)
527
update_min_mtu(batman_if->soft_iface);
550
if (hard_iface->soft_iface)
551
update_min_mtu(hard_iface->soft_iface);
529
553
case NETDEV_CHANGEADDR:
530
if (batman_if->if_status == IF_NOT_IN_USE)
533
check_known_mac_addr(batman_if->net_dev);
534
update_mac_addresses(batman_if);
536
bat_priv = netdev_priv(batman_if->soft_iface);
537
if (batman_if == bat_priv->primary_if)
538
update_primary_addr(bat_priv);
554
if (hard_iface->if_status == IF_NOT_IN_USE)
557
check_known_mac_addr(hard_iface->net_dev);
558
update_mac_addresses(hard_iface);
560
bat_priv = netdev_priv(hard_iface->soft_iface);
561
primary_if = primary_if_get_selected(bat_priv);
565
if (hard_iface == primary_if)
566
primary_if_update_addr(bat_priv);
545
kref_put(&batman_if->refcount, hardif_free_ref);
573
hardif_free_ref(hard_iface);
576
hardif_free_ref(primary_if);
547
577
return NOTIFY_DONE;
550
580
/* receive a packet with the batman ethertype coming on a hard
552
int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
553
struct packet_type *ptype, struct net_device *orig_dev)
582
static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
583
struct packet_type *ptype,
584
struct net_device *orig_dev)
555
586
struct bat_priv *bat_priv;
556
587
struct batman_packet *batman_packet;
557
struct batman_if *batman_if;
588
struct hard_iface *hard_iface;
560
batman_if = container_of(ptype, struct batman_if, batman_adv_ptype);
591
hard_iface = container_of(ptype, struct hard_iface, batman_adv_ptype);
561
592
skb = skb_share_check(skb, GFP_ATOMIC);
563
594
/* skb was released by skb_share_check() */