]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/staging/batman-adv/hard-interface.c
Staging: batman-adv: move /proc interface handling to /sys
[mirror_ubuntu-bionic-kernel.git] / drivers / staging / batman-adv / hard-interface.c
1 /*
2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22 #include "main.h"
23 #include "hard-interface.h"
24 #include "soft-interface.h"
25 #include "send.h"
26 #include "translation-table.h"
27 #include "routing.h"
28 #include "bat_sysfs.h"
29 #include "originator.h"
30 #include "hash.h"
31
32 #include <linux/if_arp.h>
33
34 #define MIN(x, y) ((x) < (y) ? (x) : (y))
35
36 struct batman_if *get_batman_if_by_netdev(struct net_device *net_dev)
37 {
38 struct batman_if *batman_if;
39
40 rcu_read_lock();
41 list_for_each_entry_rcu(batman_if, &if_list, list) {
42 if (batman_if->net_dev == net_dev)
43 goto out;
44 }
45
46 batman_if = NULL;
47
48 out:
49 rcu_read_unlock();
50 return batman_if;
51 }
52
53 static int is_valid_iface(struct net_device *net_dev)
54 {
55 if (net_dev->flags & IFF_LOOPBACK)
56 return 0;
57
58 if (net_dev->type != ARPHRD_ETHER)
59 return 0;
60
61 if (net_dev->addr_len != ETH_ALEN)
62 return 0;
63
64 /* no batman over batman */
65 #ifdef HAVE_NET_DEVICE_OPS
66 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
67 return 0;
68 #else
69 if (net_dev->hard_start_xmit == interface_tx)
70 return 0;
71 #endif
72
73 /* Device is being bridged */
74 /* if (net_dev->br_port != NULL)
75 return 0; */
76
77 return 1;
78 }
79
80 static struct batman_if *get_active_batman_if(void)
81 {
82 struct batman_if *batman_if;
83
84 /* TODO: should check interfaces belonging to bat_priv */
85 rcu_read_lock();
86 list_for_each_entry_rcu(batman_if, &if_list, list) {
87 if (batman_if->if_status == IF_ACTIVE)
88 goto out;
89 }
90
91 batman_if = NULL;
92
93 out:
94 rcu_read_unlock();
95 return batman_if;
96 }
97
98 static void set_primary_if(struct bat_priv *bat_priv,
99 struct batman_if *batman_if)
100 {
101 struct batman_packet *batman_packet;
102
103 bat_priv->primary_if = batman_if;
104
105 if (!bat_priv->primary_if)
106 return;
107
108 set_main_if_addr(batman_if->net_dev->dev_addr);
109
110 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
111 batman_packet->flags = 0;
112 batman_packet->ttl = TTL;
113
114 /***
115 * hacky trick to make sure that we send the HNA information via
116 * our new primary interface
117 */
118 atomic_set(&hna_local_changed, 1);
119 }
120
121 static bool hardif_is_iface_up(struct batman_if *batman_if)
122 {
123 if (batman_if->net_dev->flags & IFF_UP)
124 return true;
125
126 return false;
127 }
128
129 static void update_mac_addresses(struct batman_if *batman_if)
130 {
131 addr_to_string(batman_if->addr_str, batman_if->net_dev->dev_addr);
132
133 memcpy(((struct batman_packet *)(batman_if->packet_buff))->orig,
134 batman_if->net_dev->dev_addr, ETH_ALEN);
135 memcpy(((struct batman_packet *)(batman_if->packet_buff))->prev_sender,
136 batman_if->net_dev->dev_addr, ETH_ALEN);
137 }
138
139 static void check_known_mac_addr(uint8_t *addr)
140 {
141 struct batman_if *batman_if;
142
143 rcu_read_lock();
144 list_for_each_entry_rcu(batman_if, &if_list, list) {
145 if ((batman_if->if_status != IF_ACTIVE) &&
146 (batman_if->if_status != IF_TO_BE_ACTIVATED))
147 continue;
148
149 if (!compare_orig(batman_if->net_dev->dev_addr, addr))
150 continue;
151
152 printk(KERN_WARNING "batman-adv:The newly added mac address (%pM) already exists on: %s\n",
153 addr, batman_if->dev);
154 printk(KERN_WARNING "batman-adv:It is strongly recommended to keep mac addresses unique to avoid problems!\n");
155 }
156 rcu_read_unlock();
157 }
158
159 int hardif_min_mtu(void)
160 {
161 struct batman_if *batman_if;
162 /* allow big frames if all devices are capable to do so
163 * (have MTU > 1500 + BAT_HEADER_LEN) */
164 int min_mtu = ETH_DATA_LEN;
165
166 rcu_read_lock();
167 list_for_each_entry_rcu(batman_if, &if_list, list) {
168 if ((batman_if->if_status == IF_ACTIVE) ||
169 (batman_if->if_status == IF_TO_BE_ACTIVATED))
170 min_mtu = MIN(batman_if->net_dev->mtu - BAT_HEADER_LEN,
171 min_mtu);
172 }
173 rcu_read_unlock();
174
175 return min_mtu;
176 }
177
178 /* adjusts the MTU if a new interface with a smaller MTU appeared. */
179 void update_min_mtu(void)
180 {
181 int min_mtu;
182
183 min_mtu = hardif_min_mtu();
184 if (soft_device->mtu != min_mtu)
185 soft_device->mtu = min_mtu;
186 }
187
188 static void hardif_activate_interface(struct bat_priv *bat_priv,
189 struct batman_if *batman_if)
190 {
191 if (batman_if->if_status != IF_INACTIVE)
192 return;
193
194 dev_hold(batman_if->net_dev);
195
196 update_mac_addresses(batman_if);
197 batman_if->if_status = IF_TO_BE_ACTIVATED;
198
199 /**
200 * the first active interface becomes our primary interface or
201 * the next active interface after the old primay interface was removed
202 */
203 if (!bat_priv->primary_if)
204 set_primary_if(bat_priv, batman_if);
205
206 printk(KERN_INFO "batman-adv:Interface activated: %s\n",
207 batman_if->dev);
208
209 if (atomic_read(&module_state) == MODULE_INACTIVE)
210 activate_module();
211
212 update_min_mtu();
213 return;
214 }
215
216 static void hardif_deactivate_interface(struct batman_if *batman_if)
217 {
218 if ((batman_if->if_status != IF_ACTIVE) &&
219 (batman_if->if_status != IF_TO_BE_ACTIVATED))
220 return;
221
222 dev_put(batman_if->net_dev);
223
224 batman_if->if_status = IF_INACTIVE;
225
226 printk(KERN_INFO "batman-adv:Interface deactivated: %s\n",
227 batman_if->dev);
228
229 update_min_mtu();
230 }
231
232 int hardif_enable_interface(struct batman_if *batman_if)
233 {
234 /* FIXME: each batman_if will be attached to a softif */
235 struct bat_priv *bat_priv = netdev_priv(soft_device);
236 struct batman_packet *batman_packet;
237
238 if (batman_if->if_status != IF_NOT_IN_USE)
239 goto out;
240
241 batman_if->packet_len = BAT_PACKET_LEN;
242 batman_if->packet_buff = kmalloc(batman_if->packet_len, GFP_ATOMIC);
243
244 if (!batman_if->packet_buff) {
245 printk(KERN_ERR "batman-adv:Can't add interface packet (%s): out of memory\n",
246 batman_if->dev);
247 goto err;
248 }
249
250 batman_packet = (struct batman_packet *)(batman_if->packet_buff);
251 batman_packet->packet_type = BAT_PACKET;
252 batman_packet->version = COMPAT_VERSION;
253 batman_packet->flags = 0;
254 batman_packet->ttl = 2;
255 batman_packet->tq = TQ_MAX_VALUE;
256 batman_packet->num_hna = 0;
257
258 batman_if->if_num = bat_priv->num_ifaces;
259 bat_priv->num_ifaces++;
260 batman_if->if_status = IF_INACTIVE;
261 orig_hash_add_if(batman_if, bat_priv->num_ifaces);
262
263 atomic_set(&batman_if->seqno, 1);
264 printk(KERN_INFO "batman-adv:Adding interface: %s\n", batman_if->dev);
265
266 if (hardif_is_iface_up(batman_if))
267 hardif_activate_interface(bat_priv, batman_if);
268 else
269 printk(KERN_ERR "batman-adv:Not using interface %s (retrying later): interface not active\n", batman_if->dev);
270
271 /* begin scheduling originator messages on that interface */
272 schedule_own_packet(batman_if);
273
274 out:
275 return 0;
276
277 err:
278 return -ENOMEM;
279 }
280
281 void hardif_disable_interface(struct batman_if *batman_if)
282 {
283 /* FIXME: each batman_if will be attached to a softif */
284 struct bat_priv *bat_priv = netdev_priv(soft_device);
285
286 if (batman_if->if_status == IF_ACTIVE)
287 hardif_deactivate_interface(batman_if);
288
289 if (batman_if->if_status != IF_INACTIVE)
290 return;
291
292 printk(KERN_INFO "batman-adv:Removing interface: %s\n", batman_if->dev);
293 bat_priv->num_ifaces--;
294 orig_hash_del_if(batman_if, bat_priv->num_ifaces);
295
296 if (batman_if == bat_priv->primary_if)
297 set_primary_if(bat_priv, get_active_batman_if());
298
299 kfree(batman_if->packet_buff);
300 batman_if->packet_buff = NULL;
301 batman_if->if_status = IF_NOT_IN_USE;
302
303 if ((atomic_read(&module_state) == MODULE_ACTIVE) &&
304 (bat_priv->num_ifaces == 0))
305 deactivate_module();
306 }
307
308 static struct batman_if *hardif_add_interface(struct net_device *net_dev)
309 {
310 struct batman_if *batman_if;
311 int ret;
312
313 ret = is_valid_iface(net_dev);
314 if (ret != 1)
315 goto out;
316
317 batman_if = kmalloc(sizeof(struct batman_if), GFP_ATOMIC);
318 if (!batman_if) {
319 printk(KERN_ERR "batman-adv:Can't add interface (%s): out of memory\n",
320 net_dev->name);
321 goto out;
322 }
323
324 batman_if->dev = kstrdup(net_dev->name, GFP_ATOMIC);
325 if (!batman_if->dev)
326 goto free_if;
327
328 ret = sysfs_add_hardif(&batman_if->hardif_obj, net_dev);
329 if (ret)
330 goto free_dev;
331
332 batman_if->if_num = -1;
333 batman_if->net_dev = net_dev;
334 batman_if->if_status = IF_NOT_IN_USE;
335 INIT_RCU_HEAD(&batman_if->rcu);
336 INIT_LIST_HEAD(&batman_if->list);
337
338 check_known_mac_addr(batman_if->net_dev->dev_addr);
339 list_add_tail_rcu(&batman_if->list, &if_list);
340 return batman_if;
341
342 free_dev:
343 kfree(batman_if->dev);
344 free_if:
345 kfree(batman_if);
346 out:
347 return NULL;
348 }
349
350 static void hardif_free_interface(struct rcu_head *rcu)
351 {
352 struct batman_if *batman_if = container_of(rcu, struct batman_if, rcu);
353
354 /* delete all references to this batman_if */
355 purge_orig(NULL);
356 purge_outstanding_packets(batman_if);
357
358 kfree(batman_if->dev);
359 kfree(batman_if);
360 }
361
362 static void hardif_remove_interface(struct batman_if *batman_if)
363 {
364 /* first deactivate interface */
365 if (batman_if->if_status != IF_NOT_IN_USE)
366 hardif_disable_interface(batman_if);
367
368 if (batman_if->if_status != IF_NOT_IN_USE)
369 return;
370
371 batman_if->if_status = IF_TO_BE_REMOVED;
372 list_del_rcu(&batman_if->list);
373 sysfs_del_hardif(&batman_if->hardif_obj);
374 call_rcu(&batman_if->rcu, hardif_free_interface);
375 }
376
377 void hardif_remove_interfaces(void)
378 {
379 struct batman_if *batman_if, *batman_if_tmp;
380
381 list_for_each_entry_safe(batman_if, batman_if_tmp, &if_list, list)
382 hardif_remove_interface(batman_if);
383 }
384
385 static int hard_if_event(struct notifier_block *this,
386 unsigned long event, void *ptr)
387 {
388 struct net_device *net_dev = (struct net_device *)ptr;
389 struct batman_if *batman_if = get_batman_if_by_netdev(net_dev);
390 /* FIXME: each batman_if will be attached to a softif */
391 struct bat_priv *bat_priv = netdev_priv(soft_device);
392
393 if (!batman_if)
394 batman_if = hardif_add_interface(net_dev);
395
396 if (!batman_if)
397 goto out;
398
399 switch (event) {
400 case NETDEV_REGISTER:
401 break;
402 case NETDEV_UP:
403 hardif_activate_interface(bat_priv, batman_if);
404 break;
405 case NETDEV_GOING_DOWN:
406 case NETDEV_DOWN:
407 hardif_deactivate_interface(batman_if);
408 break;
409 case NETDEV_UNREGISTER:
410 hardif_remove_interface(batman_if);
411 break;
412 case NETDEV_CHANGENAME:
413 break;
414 case NETDEV_CHANGEADDR:
415 check_known_mac_addr(batman_if->net_dev->dev_addr);
416 update_mac_addresses(batman_if);
417 if (batman_if == bat_priv->primary_if)
418 set_primary_if(bat_priv, batman_if);
419 break;
420 default:
421 break;
422 };
423
424 out:
425 return NOTIFY_DONE;
426 }
427
428 /* receive a packet with the batman ethertype coming on a hard
429 * interface */
430 int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
431 struct packet_type *ptype, struct net_device *orig_dev)
432 {
433 struct batman_packet *batman_packet;
434 struct batman_if *batman_if;
435 struct net_device_stats *stats;
436 int ret;
437
438 skb = skb_share_check(skb, GFP_ATOMIC);
439
440 /* skb was released by skb_share_check() */
441 if (!skb)
442 goto err_out;
443
444 if (atomic_read(&module_state) != MODULE_ACTIVE)
445 goto err_free;
446
447 /* packet should hold at least type and version */
448 if (unlikely(skb_headlen(skb) < 2))
449 goto err_free;
450
451 /* expect a valid ethernet header here. */
452 if (unlikely(skb->mac_len != sizeof(struct ethhdr)
453 || !skb_mac_header(skb)))
454 goto err_free;
455
456 batman_if = get_batman_if_by_netdev(skb->dev);
457 if (!batman_if)
458 goto err_free;
459
460 /* discard frames on not active interfaces */
461 if (batman_if->if_status != IF_ACTIVE)
462 goto err_free;
463
464 stats = (struct net_device_stats *)dev_get_stats(skb->dev);
465 if (stats) {
466 stats->rx_packets++;
467 stats->rx_bytes += skb->len;
468 }
469
470 batman_packet = (struct batman_packet *)skb->data;
471
472 if (batman_packet->version != COMPAT_VERSION) {
473 bat_dbg(DBG_BATMAN,
474 "Drop packet: incompatible batman version (%i)\n",
475 batman_packet->version);
476 goto err_free;
477 }
478
479 /* all receive handlers return whether they received or reused
480 * the supplied skb. if not, we have to free the skb. */
481
482 switch (batman_packet->packet_type) {
483 /* batman originator packet */
484 case BAT_PACKET:
485 ret = recv_bat_packet(skb, batman_if);
486 break;
487
488 /* batman icmp packet */
489 case BAT_ICMP:
490 ret = recv_icmp_packet(skb);
491 break;
492
493 /* unicast packet */
494 case BAT_UNICAST:
495 ret = recv_unicast_packet(skb);
496 break;
497
498 /* broadcast packet */
499 case BAT_BCAST:
500 ret = recv_bcast_packet(skb);
501 break;
502
503 /* vis packet */
504 case BAT_VIS:
505 ret = recv_vis_packet(skb);
506 break;
507 default:
508 ret = NET_RX_DROP;
509 }
510
511 if (ret == NET_RX_DROP)
512 kfree_skb(skb);
513
514 /* return NET_RX_SUCCESS in any case as we
515 * most probably dropped the packet for
516 * routing-logical reasons. */
517
518 return NET_RX_SUCCESS;
519
520 err_free:
521 kfree_skb(skb);
522 err_out:
523 return NET_RX_DROP;
524 }
525
526
527 struct notifier_block hard_if_notifier = {
528 .notifier_call = hard_if_event,
529 };