batman-adv: remove vis functionality
[linux-block.git] / net / batman-adv / main.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
95a066d8
SE
20#include <linux/crc32c.h>
21#include <linux/highmem.h>
c54f38c9
SW
22#include <linux/if_vlan.h>
23#include <net/ip.h>
24#include <net/ipv6.h>
25#include <net/dsfield.h>
c6c8fea2 26#include "main.h"
b706b13b
SE
27#include "sysfs.h"
28#include "debugfs.h"
c6c8fea2
SE
29#include "routing.h"
30#include "send.h"
31#include "originator.h"
32#include "soft-interface.h"
33#include "icmp_socket.h"
34#include "translation-table.h"
35#include "hard-interface.h"
36#include "gateway_client.h"
23721387 37#include "bridge_loop_avoidance.h"
2f1dfbe1 38#include "distributed-arp-table.h"
ef261577 39#include "unicast.h"
414254e3 40#include "gateway_common.h"
c6c8fea2 41#include "hash.h"
1c280471 42#include "bat_algo.h"
d353d8d4 43#include "network-coding.h"
c6c8fea2 44
c3caf519
SE
45
46/* List manipulations on hardif_list have to be rtnl_lock()'ed,
9cfc7bd6
SE
47 * list traversals just rcu-locked
48 */
3193e8fd 49struct list_head batadv_hardif_list;
ee11ad61 50static int (*batadv_rx_handler[256])(struct sk_buff *,
56303d34 51 struct batadv_hard_iface *);
3193e8fd 52char batadv_routing_algo[20] = "BATMAN_IV";
ee11ad61 53static struct hlist_head batadv_algo_list;
c6c8fea2 54
3193e8fd 55unsigned char batadv_broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
c6c8fea2 56
3193e8fd 57struct workqueue_struct *batadv_event_workqueue;
c6c8fea2 58
ee11ad61 59static void batadv_recv_handler_init(void);
ffa995e0 60
ee11ad61 61static int __init batadv_init(void)
c6c8fea2 62{
3193e8fd 63 INIT_LIST_HEAD(&batadv_hardif_list);
ee11ad61 64 INIT_HLIST_HEAD(&batadv_algo_list);
1c280471 65
ee11ad61 66 batadv_recv_handler_init();
ffa995e0 67
81c524f7 68 batadv_iv_init();
6c519bad 69 batadv_nc_init();
c6c8fea2 70
3193e8fd 71 batadv_event_workqueue = create_singlethread_workqueue("bat_events");
c6c8fea2 72
3193e8fd 73 if (!batadv_event_workqueue)
c6c8fea2
SE
74 return -ENOMEM;
75
9039dc7e 76 batadv_socket_init();
40a072d7 77 batadv_debugfs_init();
c6c8fea2 78
9563877e 79 register_netdevice_notifier(&batadv_hard_if_notifier);
a4ac28c0 80 rtnl_link_register(&batadv_link_ops);
c6c8fea2 81
86ceb360 82 pr_info("B.A.T.M.A.N. advanced %s (compatibility version %i) loaded\n",
42d0b044 83 BATADV_SOURCE_VERSION, BATADV_COMPAT_VERSION);
c6c8fea2
SE
84
85 return 0;
86}
87
ee11ad61 88static void __exit batadv_exit(void)
c6c8fea2 89{
40a072d7 90 batadv_debugfs_destroy();
a4ac28c0 91 rtnl_link_unregister(&batadv_link_ops);
9563877e
SE
92 unregister_netdevice_notifier(&batadv_hard_if_notifier);
93 batadv_hardif_remove_interfaces();
c6c8fea2 94
3193e8fd
SE
95 flush_workqueue(batadv_event_workqueue);
96 destroy_workqueue(batadv_event_workqueue);
97 batadv_event_workqueue = NULL;
c6c8fea2
SE
98
99 rcu_barrier();
100}
101
3193e8fd 102int batadv_mesh_init(struct net_device *soft_iface)
c6c8fea2 103{
56303d34 104 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
5346c35e 105 int ret;
c6c8fea2 106
c6c8fea2
SE
107 spin_lock_init(&bat_priv->forw_bat_list_lock);
108 spin_lock_init(&bat_priv->forw_bcast_list_lock);
807736f6
SE
109 spin_lock_init(&bat_priv->tt.changes_list_lock);
110 spin_lock_init(&bat_priv->tt.req_list_lock);
111 spin_lock_init(&bat_priv->tt.roam_list_lock);
112 spin_lock_init(&bat_priv->tt.last_changeset_lock);
113 spin_lock_init(&bat_priv->gw.list_lock);
ef261577
ML
114 spin_lock_init(&bat_priv->tvlv.container_list_lock);
115 spin_lock_init(&bat_priv->tvlv.handler_list_lock);
c6c8fea2
SE
116
117 INIT_HLIST_HEAD(&bat_priv->forw_bat_list);
118 INIT_HLIST_HEAD(&bat_priv->forw_bcast_list);
807736f6
SE
119 INIT_HLIST_HEAD(&bat_priv->gw.list);
120 INIT_LIST_HEAD(&bat_priv->tt.changes_list);
121 INIT_LIST_HEAD(&bat_priv->tt.req_list);
122 INIT_LIST_HEAD(&bat_priv->tt.roam_list);
ef261577
ML
123 INIT_HLIST_HEAD(&bat_priv->tvlv.container_list);
124 INIT_HLIST_HEAD(&bat_priv->tvlv.handler_list);
c6c8fea2 125
7d211efc 126 ret = batadv_originator_init(bat_priv);
5346c35e 127 if (ret < 0)
c6c8fea2
SE
128 goto err;
129
08c36d3e 130 ret = batadv_tt_init(bat_priv);
5346c35e 131 if (ret < 0)
c6c8fea2
SE
132 goto err;
133
42d0b044
SE
134 batadv_tt_local_add(soft_iface, soft_iface->dev_addr,
135 BATADV_NULL_IFINDEX);
c6c8fea2 136
08adf151 137 ret = batadv_bla_init(bat_priv);
5346c35e 138 if (ret < 0)
23721387
SW
139 goto err;
140
2f1dfbe1
AQ
141 ret = batadv_dat_init(bat_priv);
142 if (ret < 0)
143 goto err;
144
6c519bad 145 ret = batadv_nc_mesh_init(bat_priv);
d353d8d4
MH
146 if (ret < 0)
147 goto err;
148
414254e3
ML
149 batadv_gw_init(bat_priv);
150
807736f6 151 atomic_set(&bat_priv->gw.reselect, 0);
39c75a51 152 atomic_set(&bat_priv->mesh_state, BATADV_MESH_ACTIVE);
5346c35e
SE
153
154 return 0;
c6c8fea2
SE
155
156err:
3193e8fd 157 batadv_mesh_free(soft_iface);
5346c35e 158 return ret;
c6c8fea2
SE
159}
160
3193e8fd 161void batadv_mesh_free(struct net_device *soft_iface)
c6c8fea2 162{
56303d34 163 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
c6c8fea2 164
39c75a51 165 atomic_set(&bat_priv->mesh_state, BATADV_MESH_DEACTIVATING);
c6c8fea2 166
9455e34c 167 batadv_purge_outstanding_packets(bat_priv, NULL);
c6c8fea2 168
7cf06bc6 169 batadv_gw_node_purge(bat_priv);
6c519bad 170 batadv_nc_mesh_free(bat_priv);
a4361860
AQ
171 batadv_dat_free(bat_priv);
172 batadv_bla_free(bat_priv);
c6c8fea2 173
a4361860
AQ
174 /* Free the TT and the originator tables only after having terminated
175 * all the other depending components which may use these structures for
176 * their purposes.
177 */
08c36d3e 178 batadv_tt_free(bat_priv);
c6c8fea2 179
a4361860
AQ
180 /* Since the originator table clean up routine is accessing the TT
181 * tables as well, it has to be invoked after the TT tables have been
182 * freed and marked as empty. This ensures that no cleanup RCU callbacks
183 * accessing the TT data are scheduled for later execution.
184 */
185 batadv_originator_free(bat_priv);
2f1dfbe1 186
414254e3
ML
187 batadv_gw_free(bat_priv);
188
f8214865 189 free_percpu(bat_priv->bat_counters);
f69ae770 190 bat_priv->bat_counters = NULL;
f8214865 191
39c75a51 192 atomic_set(&bat_priv->mesh_state, BATADV_MESH_INACTIVE);
c6c8fea2
SE
193}
194
6e0895c2
DM
195/**
196 * batadv_is_my_mac - check if the given mac address belongs to any of the real
197 * interfaces in the current mesh
198 * @bat_priv: the bat priv with all the soft interface information
199 * @addr: the address to check
200 */
fe8a93b9 201int batadv_is_my_mac(struct batadv_priv *bat_priv, const uint8_t *addr)
c6c8fea2 202{
56303d34 203 const struct batadv_hard_iface *hard_iface;
c6c8fea2
SE
204
205 rcu_read_lock();
3193e8fd 206 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
e9a4f295 207 if (hard_iface->if_status != BATADV_IF_ACTIVE)
c6c8fea2
SE
208 continue;
209
fe8a93b9
AQ
210 if (hard_iface->soft_iface != bat_priv->soft_iface)
211 continue;
212
1eda58bf 213 if (batadv_compare_eth(hard_iface->net_dev->dev_addr, addr)) {
c6c8fea2
SE
214 rcu_read_unlock();
215 return 1;
216 }
217 }
218 rcu_read_unlock();
219 return 0;
c6c8fea2
SE
220}
221
30da63a6
ML
222/**
223 * batadv_seq_print_text_primary_if_get - called from debugfs table printing
224 * function that requires the primary interface
225 * @seq: debugfs table seq_file struct
226 *
227 * Returns primary interface if found or NULL otherwise.
228 */
229struct batadv_hard_iface *
230batadv_seq_print_text_primary_if_get(struct seq_file *seq)
231{
232 struct net_device *net_dev = (struct net_device *)seq->private;
233 struct batadv_priv *bat_priv = netdev_priv(net_dev);
234 struct batadv_hard_iface *primary_if;
235
236 primary_if = batadv_primary_if_get_selected(bat_priv);
237
238 if (!primary_if) {
239 seq_printf(seq,
240 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
241 net_dev->name);
242 goto out;
243 }
244
245 if (primary_if->if_status == BATADV_IF_ACTIVE)
246 goto out;
247
248 seq_printf(seq,
249 "BATMAN mesh %s disabled - primary interface not active\n",
250 net_dev->name);
251 batadv_hardif_free_ref(primary_if);
252 primary_if = NULL;
253
254out:
255 return primary_if;
256}
257
c54f38c9
SW
258/**
259 * batadv_skb_set_priority - sets skb priority according to packet content
260 * @skb: the packet to be sent
261 * @offset: offset to the packet content
262 *
263 * This function sets a value between 256 and 263 (802.1d priority), which
264 * can be interpreted by the cfg80211 or other drivers.
265 */
266void batadv_skb_set_priority(struct sk_buff *skb, int offset)
267{
268 struct iphdr ip_hdr_tmp, *ip_hdr;
269 struct ipv6hdr ip6_hdr_tmp, *ip6_hdr;
270 struct ethhdr ethhdr_tmp, *ethhdr;
271 struct vlan_ethhdr *vhdr, vhdr_tmp;
272 u32 prio;
273
274 /* already set, do nothing */
275 if (skb->priority >= 256 && skb->priority <= 263)
276 return;
277
278 ethhdr = skb_header_pointer(skb, offset, sizeof(*ethhdr), &ethhdr_tmp);
279 if (!ethhdr)
280 return;
281
282 switch (ethhdr->h_proto) {
283 case htons(ETH_P_8021Q):
284 vhdr = skb_header_pointer(skb, offset + sizeof(*vhdr),
285 sizeof(*vhdr), &vhdr_tmp);
286 if (!vhdr)
287 return;
288 prio = ntohs(vhdr->h_vlan_TCI) & VLAN_PRIO_MASK;
289 prio = prio >> VLAN_PRIO_SHIFT;
290 break;
291 case htons(ETH_P_IP):
292 ip_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
293 sizeof(*ip_hdr), &ip_hdr_tmp);
294 if (!ip_hdr)
295 return;
296 prio = (ipv4_get_dsfield(ip_hdr) & 0xfc) >> 5;
297 break;
298 case htons(ETH_P_IPV6):
299 ip6_hdr = skb_header_pointer(skb, offset + sizeof(*ethhdr),
300 sizeof(*ip6_hdr), &ip6_hdr_tmp);
301 if (!ip6_hdr)
302 return;
303 prio = (ipv6_get_dsfield(ip6_hdr) & 0xfc) >> 5;
304 break;
305 default:
306 return;
307 }
308
309 skb->priority = prio + 256;
310}
311
ee11ad61 312static int batadv_recv_unhandled_packet(struct sk_buff *skb,
56303d34 313 struct batadv_hard_iface *recv_if)
ffa995e0
ML
314{
315 return NET_RX_DROP;
316}
317
318/* incoming packets with the batman ethertype received on any active hard
319 * interface
320 */
3193e8fd
SE
321int batadv_batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
322 struct packet_type *ptype,
323 struct net_device *orig_dev)
ffa995e0 324{
56303d34 325 struct batadv_priv *bat_priv;
96412690 326 struct batadv_ogm_packet *batadv_ogm_packet;
56303d34 327 struct batadv_hard_iface *hard_iface;
ffa995e0
ML
328 uint8_t idx;
329 int ret;
330
56303d34
SE
331 hard_iface = container_of(ptype, struct batadv_hard_iface,
332 batman_adv_ptype);
ffa995e0
ML
333 skb = skb_share_check(skb, GFP_ATOMIC);
334
335 /* skb was released by skb_share_check() */
336 if (!skb)
337 goto err_out;
338
339 /* packet should hold at least type and version */
340 if (unlikely(!pskb_may_pull(skb, 2)))
341 goto err_free;
342
343 /* expect a valid ethernet header here. */
344 if (unlikely(skb->mac_len != ETH_HLEN || !skb_mac_header(skb)))
345 goto err_free;
346
347 if (!hard_iface->soft_iface)
348 goto err_free;
349
350 bat_priv = netdev_priv(hard_iface->soft_iface);
351
39c75a51 352 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
ffa995e0
ML
353 goto err_free;
354
355 /* discard frames on not active interfaces */
e9a4f295 356 if (hard_iface->if_status != BATADV_IF_ACTIVE)
ffa995e0
ML
357 goto err_free;
358
96412690 359 batadv_ogm_packet = (struct batadv_ogm_packet *)skb->data;
ffa995e0 360
96412690 361 if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
39c75a51 362 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf 363 "Drop packet: incompatible batman version (%i)\n",
96412690 364 batadv_ogm_packet->header.version);
ffa995e0
ML
365 goto err_free;
366 }
367
368 /* all receive handlers return whether they received or reused
369 * the supplied skb. if not, we have to free the skb.
370 */
96412690 371 idx = batadv_ogm_packet->header.packet_type;
ee11ad61 372 ret = (*batadv_rx_handler[idx])(skb, hard_iface);
ffa995e0
ML
373
374 if (ret == NET_RX_DROP)
375 kfree_skb(skb);
376
377 /* return NET_RX_SUCCESS in any case as we
378 * most probably dropped the packet for
379 * routing-logical reasons.
380 */
381 return NET_RX_SUCCESS;
382
383err_free:
384 kfree_skb(skb);
385err_out:
386 return NET_RX_DROP;
387}
388
ee11ad61 389static void batadv_recv_handler_init(void)
ffa995e0
ML
390{
391 int i;
392
ee11ad61
SE
393 for (i = 0; i < ARRAY_SIZE(batadv_rx_handler); i++)
394 batadv_rx_handler[i] = batadv_recv_unhandled_packet;
ffa995e0 395
ffa995e0 396 /* batman icmp packet */
acd34afa 397 batadv_rx_handler[BATADV_ICMP] = batadv_recv_icmp_packet;
7cdcf6dd
AQ
398 /* unicast with 4 addresses packet */
399 batadv_rx_handler[BATADV_UNICAST_4ADDR] = batadv_recv_unicast_packet;
ffa995e0 400 /* unicast packet */
acd34afa 401 batadv_rx_handler[BATADV_UNICAST] = batadv_recv_unicast_packet;
ffa995e0 402 /* fragmented unicast packet */
acd34afa 403 batadv_rx_handler[BATADV_UNICAST_FRAG] = batadv_recv_ucast_frag_packet;
ffa995e0 404 /* broadcast packet */
acd34afa 405 batadv_rx_handler[BATADV_BCAST] = batadv_recv_bcast_packet;
ef261577
ML
406 /* unicast tvlv packet */
407 batadv_rx_handler[BATADV_UNICAST_TVLV] = batadv_recv_unicast_tvlv;
ffa995e0
ML
408}
409
56303d34
SE
410int
411batadv_recv_handler_register(uint8_t packet_type,
412 int (*recv_handler)(struct sk_buff *,
413 struct batadv_hard_iface *))
ffa995e0 414{
ee11ad61 415 if (batadv_rx_handler[packet_type] != &batadv_recv_unhandled_packet)
ffa995e0
ML
416 return -EBUSY;
417
ee11ad61 418 batadv_rx_handler[packet_type] = recv_handler;
ffa995e0
ML
419 return 0;
420}
421
3193e8fd 422void batadv_recv_handler_unregister(uint8_t packet_type)
ffa995e0 423{
ee11ad61 424 batadv_rx_handler[packet_type] = batadv_recv_unhandled_packet;
ffa995e0
ML
425}
426
56303d34 427static struct batadv_algo_ops *batadv_algo_get(char *name)
1c280471 428{
56303d34 429 struct batadv_algo_ops *bat_algo_ops = NULL, *bat_algo_ops_tmp;
1c280471 430
b67bfe0d 431 hlist_for_each_entry(bat_algo_ops_tmp, &batadv_algo_list, list) {
1c280471
ML
432 if (strcmp(bat_algo_ops_tmp->name, name) != 0)
433 continue;
434
435 bat_algo_ops = bat_algo_ops_tmp;
436 break;
437 }
438
439 return bat_algo_ops;
440}
441
56303d34 442int batadv_algo_register(struct batadv_algo_ops *bat_algo_ops)
1c280471 443{
56303d34 444 struct batadv_algo_ops *bat_algo_ops_tmp;
5346c35e 445 int ret;
1c280471 446
ee11ad61 447 bat_algo_ops_tmp = batadv_algo_get(bat_algo_ops->name);
1c280471 448 if (bat_algo_ops_tmp) {
86ceb360
SE
449 pr_info("Trying to register already registered routing algorithm: %s\n",
450 bat_algo_ops->name);
5346c35e 451 ret = -EEXIST;
1c280471
ML
452 goto out;
453 }
454
01c4224b 455 /* all algorithms must implement all ops (for now) */
c2aca022 456 if (!bat_algo_ops->bat_iface_enable ||
00a50076 457 !bat_algo_ops->bat_iface_disable ||
c3229398 458 !bat_algo_ops->bat_iface_update_mac ||
cd8b78e7 459 !bat_algo_ops->bat_primary_iface_set ||
01c4224b 460 !bat_algo_ops->bat_ogm_schedule ||
c3e29312 461 !bat_algo_ops->bat_ogm_emit) {
01c4224b
ML
462 pr_info("Routing algo '%s' does not implement required ops\n",
463 bat_algo_ops->name);
5346c35e 464 ret = -EINVAL;
01c4224b
ML
465 goto out;
466 }
467
1c280471 468 INIT_HLIST_NODE(&bat_algo_ops->list);
ee11ad61 469 hlist_add_head(&bat_algo_ops->list, &batadv_algo_list);
1c280471
ML
470 ret = 0;
471
472out:
473 return ret;
474}
475
56303d34 476int batadv_algo_select(struct batadv_priv *bat_priv, char *name)
1c280471 477{
56303d34 478 struct batadv_algo_ops *bat_algo_ops;
5346c35e 479 int ret = -EINVAL;
1c280471 480
ee11ad61 481 bat_algo_ops = batadv_algo_get(name);
1c280471
ML
482 if (!bat_algo_ops)
483 goto out;
484
485 bat_priv->bat_algo_ops = bat_algo_ops;
486 ret = 0;
487
488out:
489 return ret;
490}
491
3193e8fd 492int batadv_algo_seq_print_text(struct seq_file *seq, void *offset)
1c280471 493{
56303d34 494 struct batadv_algo_ops *bat_algo_ops;
1c280471 495
0c814653 496 seq_puts(seq, "Available routing algorithms:\n");
1c280471 497
b67bfe0d 498 hlist_for_each_entry(bat_algo_ops, &batadv_algo_list, list) {
1c280471
ML
499 seq_printf(seq, "%s\n", bat_algo_ops->name);
500 }
501
502 return 0;
503}
504
95a066d8
SE
505/**
506 * batadv_skb_crc32 - calculate CRC32 of the whole packet and skip bytes in
507 * the header
508 * @skb: skb pointing to fragmented socket buffers
509 * @payload_ptr: Pointer to position inside the head buffer of the skb
510 * marking the start of the data to be CRC'ed
511 *
512 * payload_ptr must always point to an address in the skb head buffer and not to
513 * a fragment.
514 */
515__be32 batadv_skb_crc32(struct sk_buff *skb, u8 *payload_ptr)
516{
517 u32 crc = 0;
518 unsigned int from;
519 unsigned int to = skb->len;
520 struct skb_seq_state st;
521 const u8 *data;
522 unsigned int len;
523 unsigned int consumed = 0;
524
525 from = (unsigned int)(payload_ptr - skb->data);
526
527 skb_prepare_seq_read(skb, from, to, &st);
528 while ((len = skb_seq_read(consumed, &data, &st)) != 0) {
529 crc = crc32c(crc, data, len);
530 consumed += len;
531 }
95a066d8
SE
532
533 return htonl(crc);
534}
535
ef261577
ML
536/**
537 * batadv_tvlv_handler_free_ref - decrement the tvlv handler refcounter and
538 * possibly free it
539 * @tvlv_handler: the tvlv handler to free
540 */
541static void
542batadv_tvlv_handler_free_ref(struct batadv_tvlv_handler *tvlv_handler)
543{
544 if (atomic_dec_and_test(&tvlv_handler->refcount))
545 kfree_rcu(tvlv_handler, rcu);
546}
547
548/**
549 * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
550 * based on the provided type and version (both need to match)
551 * @bat_priv: the bat priv with all the soft interface information
552 * @type: tvlv handler type to look for
553 * @version: tvlv handler version to look for
554 *
555 * Returns tvlv handler if found or NULL otherwise.
556 */
557static struct batadv_tvlv_handler
558*batadv_tvlv_handler_get(struct batadv_priv *bat_priv,
559 uint8_t type, uint8_t version)
560{
561 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
562
563 rcu_read_lock();
564 hlist_for_each_entry_rcu(tvlv_handler_tmp,
565 &bat_priv->tvlv.handler_list, list) {
566 if (tvlv_handler_tmp->type != type)
567 continue;
568
569 if (tvlv_handler_tmp->version != version)
570 continue;
571
572 if (!atomic_inc_not_zero(&tvlv_handler_tmp->refcount))
573 continue;
574
575 tvlv_handler = tvlv_handler_tmp;
576 break;
577 }
578 rcu_read_unlock();
579
580 return tvlv_handler;
581}
582
583/**
584 * batadv_tvlv_container_free_ref - decrement the tvlv container refcounter and
585 * possibly free it
586 * @tvlv_handler: the tvlv container to free
587 */
588static void batadv_tvlv_container_free_ref(struct batadv_tvlv_container *tvlv)
589{
590 if (atomic_dec_and_test(&tvlv->refcount))
591 kfree(tvlv);
592}
593
594/**
595 * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
596 * list based on the provided type and version (both need to match)
597 * @bat_priv: the bat priv with all the soft interface information
598 * @type: tvlv container type to look for
599 * @version: tvlv container version to look for
600 *
601 * Has to be called with the appropriate locks being acquired
602 * (tvlv.container_list_lock).
603 *
604 * Returns tvlv container if found or NULL otherwise.
605 */
606static struct batadv_tvlv_container
607*batadv_tvlv_container_get(struct batadv_priv *bat_priv,
608 uint8_t type, uint8_t version)
609{
610 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
611
612 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
613 if (tvlv_tmp->tvlv_hdr.type != type)
614 continue;
615
616 if (tvlv_tmp->tvlv_hdr.version != version)
617 continue;
618
619 if (!atomic_inc_not_zero(&tvlv_tmp->refcount))
620 continue;
621
622 tvlv = tvlv_tmp;
623 break;
624 }
625
626 return tvlv;
627}
628
629/**
630 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
631 * list entries
632 * @bat_priv: the bat priv with all the soft interface information
633 *
634 * Has to be called with the appropriate locks being acquired
635 * (tvlv.container_list_lock).
636 *
637 * Returns size of all currently registered tvlv containers in bytes.
638 */
639static uint16_t batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
640{
641 struct batadv_tvlv_container *tvlv;
642 uint16_t tvlv_len = 0;
643
644 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
645 tvlv_len += sizeof(struct batadv_tvlv_hdr);
646 tvlv_len += ntohs(tvlv->tvlv_hdr.len);
647 }
648
649 return tvlv_len;
650}
651
652/**
653 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
654 * list
655 * @tvlv: the to be removed tvlv container
656 *
657 * Has to be called with the appropriate locks being acquired
658 * (tvlv.container_list_lock).
659 */
660static void batadv_tvlv_container_remove(struct batadv_tvlv_container *tvlv)
661{
662 if (!tvlv)
663 return;
664
665 hlist_del(&tvlv->list);
666
667 /* first call to decrement the counter, second call to free */
668 batadv_tvlv_container_free_ref(tvlv);
669 batadv_tvlv_container_free_ref(tvlv);
670}
671
672/**
673 * batadv_tvlv_container_unregister - unregister tvlv container based on the
674 * provided type and version (both need to match)
675 * @bat_priv: the bat priv with all the soft interface information
676 * @type: tvlv container type to unregister
677 * @version: tvlv container type to unregister
678 */
679void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
680 uint8_t type, uint8_t version)
681{
682 struct batadv_tvlv_container *tvlv;
683
684 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
685 tvlv = batadv_tvlv_container_get(bat_priv, type, version);
686 batadv_tvlv_container_remove(tvlv);
687 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
688}
689
690/**
691 * batadv_tvlv_container_register - register tvlv type, version and content
692 * to be propagated with each (primary interface) OGM
693 * @bat_priv: the bat priv with all the soft interface information
694 * @type: tvlv container type
695 * @version: tvlv container version
696 * @tvlv_value: tvlv container content
697 * @tvlv_value_len: tvlv container content length
698 *
699 * If a container of the same type and version was already registered the new
700 * content is going to replace the old one.
701 */
702void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
703 uint8_t type, uint8_t version,
704 void *tvlv_value, uint16_t tvlv_value_len)
705{
706 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
707
708 if (!tvlv_value)
709 tvlv_value_len = 0;
710
711 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
712 if (!tvlv_new)
713 return;
714
715 tvlv_new->tvlv_hdr.version = version;
716 tvlv_new->tvlv_hdr.type = type;
717 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
718
719 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
720 INIT_HLIST_NODE(&tvlv_new->list);
721 atomic_set(&tvlv_new->refcount, 1);
722
723 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
724 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
725 batadv_tvlv_container_remove(tvlv_old);
726 hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
727 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
728}
729
730/**
731 * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accomodate
732 * requested packet size
733 * @packet_buff: packet buffer
734 * @packet_buff_len: packet buffer size
735 * @packet_min_len: requested packet minimum size
736 * @additional_packet_len: requested additional packet size on top of minimum
737 * size
738 *
739 * Returns true of the packet buffer could be changed to the requested size,
740 * false otherwise.
741 */
742static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
743 int *packet_buff_len,
744 int min_packet_len,
745 int additional_packet_len)
746{
747 unsigned char *new_buff;
748
749 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
750
751 /* keep old buffer if kmalloc should fail */
752 if (new_buff) {
753 memcpy(new_buff, *packet_buff, min_packet_len);
754 kfree(*packet_buff);
755 *packet_buff = new_buff;
756 *packet_buff_len = min_packet_len + additional_packet_len;
757 return true;
758 }
759
760 return false;
761}
762
763/**
764 * batadv_tvlv_container_ogm_append - append tvlv container content to given
765 * OGM packet buffer
766 * @bat_priv: the bat priv with all the soft interface information
767 * @packet_buff: ogm packet buffer
768 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
769 * content
770 * @packet_min_len: ogm header size to be preserved for the OGM itself
771 *
772 * The ogm packet might be enlarged or shrunk depending on the current size
773 * and the size of the to-be-appended tvlv containers.
774 *
775 * Returns size of all appended tvlv containers in bytes.
776 */
777uint16_t batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
778 unsigned char **packet_buff,
779 int *packet_buff_len,
780 int packet_min_len)
781{
782 struct batadv_tvlv_container *tvlv;
783 struct batadv_tvlv_hdr *tvlv_hdr;
784 uint16_t tvlv_value_len;
785 void *tvlv_value;
786 bool ret;
787
788 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
789 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
790
791 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
792 packet_min_len, tvlv_value_len);
793
794 if (!ret)
795 goto end;
796
797 if (!tvlv_value_len)
798 goto end;
799
800 tvlv_value = (*packet_buff) + packet_min_len;
801
802 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
803 tvlv_hdr = tvlv_value;
804 tvlv_hdr->type = tvlv->tvlv_hdr.type;
805 tvlv_hdr->version = tvlv->tvlv_hdr.version;
806 tvlv_hdr->len = tvlv->tvlv_hdr.len;
807 tvlv_value = tvlv_hdr + 1;
808 memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
809 tvlv_value = (uint8_t *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
810 }
811
812end:
813 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
814 return tvlv_value_len;
815}
816
817/**
818 * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
819 * appropriate handlers
820 * @bat_priv: the bat priv with all the soft interface information
821 * @tvlv_handler: tvlv callback function handling the tvlv content
822 * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
823 * @orig_node: orig node emitting the ogm packet
824 * @src: source mac address of the unicast packet
825 * @dst: destination mac address of the unicast packet
826 * @tvlv_value: tvlv content
827 * @tvlv_value_len: tvlv content length
828 *
829 * Returns success if handler was not found or the return value of the handler
830 * callback.
831 */
832static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
833 struct batadv_tvlv_handler *tvlv_handler,
834 bool ogm_source,
835 struct batadv_orig_node *orig_node,
836 uint8_t *src, uint8_t *dst,
837 void *tvlv_value, uint16_t tvlv_value_len)
838{
839 if (!tvlv_handler)
840 return NET_RX_SUCCESS;
841
842 if (ogm_source) {
843 if (!tvlv_handler->ogm_handler)
844 return NET_RX_SUCCESS;
845
846 if (!orig_node)
847 return NET_RX_SUCCESS;
848
849 tvlv_handler->ogm_handler(bat_priv, orig_node,
850 BATADV_NO_FLAGS,
851 tvlv_value, tvlv_value_len);
852 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
853 } else {
854 if (!src)
855 return NET_RX_SUCCESS;
856
857 if (!dst)
858 return NET_RX_SUCCESS;
859
860 if (!tvlv_handler->unicast_handler)
861 return NET_RX_SUCCESS;
862
863 return tvlv_handler->unicast_handler(bat_priv, src,
864 dst, tvlv_value,
865 tvlv_value_len);
866 }
867
868 return NET_RX_SUCCESS;
869}
870
871/**
872 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
873 * appropriate handlers
874 * @bat_priv: the bat priv with all the soft interface information
875 * @ogm_source: flag indicating wether the tvlv is an ogm or a unicast packet
876 * @orig_node: orig node emitting the ogm packet
877 * @src: source mac address of the unicast packet
878 * @dst: destination mac address of the unicast packet
879 * @tvlv_value: tvlv content
880 * @tvlv_value_len: tvlv content length
881 *
882 * Returns success when processing an OGM or the return value of all called
883 * handler callbacks.
884 */
885int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
886 bool ogm_source,
887 struct batadv_orig_node *orig_node,
888 uint8_t *src, uint8_t *dst,
889 void *tvlv_value, uint16_t tvlv_value_len)
890{
891 struct batadv_tvlv_handler *tvlv_handler;
892 struct batadv_tvlv_hdr *tvlv_hdr;
893 uint16_t tvlv_value_cont_len;
894 uint8_t cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
895 int ret = NET_RX_SUCCESS;
896
897 while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
898 tvlv_hdr = tvlv_value;
899 tvlv_value_cont_len = ntohs(tvlv_hdr->len);
900 tvlv_value = tvlv_hdr + 1;
901 tvlv_value_len -= sizeof(*tvlv_hdr);
902
903 if (tvlv_value_cont_len > tvlv_value_len)
904 break;
905
906 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
907 tvlv_hdr->type,
908 tvlv_hdr->version);
909
910 ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
911 ogm_source, orig_node,
912 src, dst, tvlv_value,
913 tvlv_value_cont_len);
914 if (tvlv_handler)
915 batadv_tvlv_handler_free_ref(tvlv_handler);
916 tvlv_value = (uint8_t *)tvlv_value + tvlv_value_cont_len;
917 tvlv_value_len -= tvlv_value_cont_len;
918 }
919
920 if (!ogm_source)
921 return ret;
922
923 rcu_read_lock();
924 hlist_for_each_entry_rcu(tvlv_handler,
925 &bat_priv->tvlv.handler_list, list) {
926 if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
927 !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
928 tvlv_handler->ogm_handler(bat_priv, orig_node,
929 cifnotfound, NULL, 0);
930
931 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
932 }
933 rcu_read_unlock();
934
935 return NET_RX_SUCCESS;
936}
937
938/**
939 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
940 * handlers
941 * @bat_priv: the bat priv with all the soft interface information
942 * @batadv_ogm_packet: ogm packet containing the tvlv containers
943 * @orig_node: orig node emitting the ogm packet
944 */
945void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
946 struct batadv_ogm_packet *batadv_ogm_packet,
947 struct batadv_orig_node *orig_node)
948{
949 void *tvlv_value;
950 uint16_t tvlv_value_len;
951
952 if (!batadv_ogm_packet)
953 return;
954
955 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
956 if (!tvlv_value_len)
957 return;
958
959 tvlv_value = batadv_ogm_packet + 1;
960
961 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
962 tvlv_value, tvlv_value_len);
963}
964
965/**
966 * batadv_tvlv_handler_register - register tvlv handler based on the provided
967 * type and version (both need to match) for ogm tvlv payload and/or unicast
968 * payload
969 * @bat_priv: the bat priv with all the soft interface information
970 * @optr: ogm tvlv handler callback function. This function receives the orig
971 * node, flags and the tvlv content as argument to process.
972 * @uptr: unicast tvlv handler callback function. This function receives the
973 * source & destination of the unicast packet as well as the tvlv content
974 * to process.
975 * @type: tvlv handler type to be registered
976 * @version: tvlv handler version to be registered
977 * @flags: flags to enable or disable TVLV API behavior
978 */
979void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
980 void (*optr)(struct batadv_priv *bat_priv,
981 struct batadv_orig_node *orig,
982 uint8_t flags,
983 void *tvlv_value,
984 uint16_t tvlv_value_len),
985 int (*uptr)(struct batadv_priv *bat_priv,
986 uint8_t *src, uint8_t *dst,
987 void *tvlv_value,
988 uint16_t tvlv_value_len),
989 uint8_t type, uint8_t version, uint8_t flags)
990{
991 struct batadv_tvlv_handler *tvlv_handler;
992
993 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
994 if (tvlv_handler) {
995 batadv_tvlv_handler_free_ref(tvlv_handler);
996 return;
997 }
998
999 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
1000 if (!tvlv_handler)
1001 return;
1002
1003 tvlv_handler->ogm_handler = optr;
1004 tvlv_handler->unicast_handler = uptr;
1005 tvlv_handler->type = type;
1006 tvlv_handler->version = version;
1007 tvlv_handler->flags = flags;
1008 atomic_set(&tvlv_handler->refcount, 1);
1009 INIT_HLIST_NODE(&tvlv_handler->list);
1010
1011 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1012 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
1013 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1014}
1015
1016/**
1017 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
1018 * provided type and version (both need to match)
1019 * @bat_priv: the bat priv with all the soft interface information
1020 * @type: tvlv handler type to be unregistered
1021 * @version: tvlv handler version to be unregistered
1022 */
1023void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
1024 uint8_t type, uint8_t version)
1025{
1026 struct batadv_tvlv_handler *tvlv_handler;
1027
1028 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
1029 if (!tvlv_handler)
1030 return;
1031
1032 batadv_tvlv_handler_free_ref(tvlv_handler);
1033 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
1034 hlist_del_rcu(&tvlv_handler->list);
1035 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
1036 batadv_tvlv_handler_free_ref(tvlv_handler);
1037}
1038
1039/**
1040 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
1041 * specified host
1042 * @bat_priv: the bat priv with all the soft interface information
1043 * @src: source mac address of the unicast packet
1044 * @dst: destination mac address of the unicast packet
1045 * @type: tvlv type
1046 * @version: tvlv version
1047 * @tvlv_value: tvlv content
1048 * @tvlv_value_len: tvlv content length
1049 */
1050void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, uint8_t *src,
1051 uint8_t *dst, uint8_t type, uint8_t version,
1052 void *tvlv_value, uint16_t tvlv_value_len)
1053{
1054 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1055 struct batadv_tvlv_hdr *tvlv_hdr;
1056 struct batadv_orig_node *orig_node;
1057 struct sk_buff *skb = NULL;
1058 unsigned char *tvlv_buff;
1059 unsigned int tvlv_len;
1060 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
1061 bool ret = false;
1062
1063 orig_node = batadv_orig_hash_find(bat_priv, dst);
1064 if (!orig_node)
1065 goto out;
1066
1067 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
1068
1069 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
1070 if (!skb)
1071 goto out;
1072
1073 skb->priority = TC_PRIO_CONTROL;
1074 skb_reserve(skb, ETH_HLEN);
1075 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
1076 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
1077 unicast_tvlv_packet->header.packet_type = BATADV_UNICAST_TVLV;
1078 unicast_tvlv_packet->header.version = BATADV_COMPAT_VERSION;
1079 unicast_tvlv_packet->header.ttl = BATADV_TTL;
1080 unicast_tvlv_packet->reserved = 0;
1081 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
1082 unicast_tvlv_packet->align = 0;
1083 memcpy(unicast_tvlv_packet->src, src, ETH_ALEN);
1084 memcpy(unicast_tvlv_packet->dst, dst, ETH_ALEN);
1085
1086 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
1087 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
1088 tvlv_hdr->version = version;
1089 tvlv_hdr->type = type;
1090 tvlv_hdr->len = htons(tvlv_value_len);
1091 tvlv_buff += sizeof(*tvlv_hdr);
1092 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
1093
1094 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
1095 ret = true;
1096
1097out:
1098 if (skb && !ret)
1099 kfree_skb(skb);
1100 if (orig_node)
1101 batadv_orig_node_free_ref(orig_node);
1102}
1103
ee11ad61 1104static int batadv_param_set_ra(const char *val, const struct kernel_param *kp)
d419be1f 1105{
56303d34 1106 struct batadv_algo_ops *bat_algo_ops;
d8cb5486
ML
1107 char *algo_name = (char *)val;
1108 size_t name_len = strlen(algo_name);
d419be1f 1109
293c9c1c 1110 if (name_len > 0 && algo_name[name_len - 1] == '\n')
d8cb5486
ML
1111 algo_name[name_len - 1] = '\0';
1112
ee11ad61 1113 bat_algo_ops = batadv_algo_get(algo_name);
d419be1f 1114 if (!bat_algo_ops) {
d8cb5486 1115 pr_err("Routing algorithm '%s' is not supported\n", algo_name);
d419be1f
ML
1116 return -EINVAL;
1117 }
1118
d8cb5486 1119 return param_set_copystring(algo_name, kp);
d419be1f
ML
1120}
1121
ee11ad61
SE
1122static const struct kernel_param_ops batadv_param_ops_ra = {
1123 .set = batadv_param_set_ra,
d419be1f
ML
1124 .get = param_get_string,
1125};
1126
ee11ad61 1127static struct kparam_string batadv_param_string_ra = {
3193e8fd
SE
1128 .maxlen = sizeof(batadv_routing_algo),
1129 .string = batadv_routing_algo,
d419be1f
ML
1130};
1131
ee11ad61
SE
1132module_param_cb(routing_algo, &batadv_param_ops_ra, &batadv_param_string_ra,
1133 0644);
1134module_init(batadv_init);
1135module_exit(batadv_exit);
c6c8fea2
SE
1136
1137MODULE_LICENSE("GPL");
1138
42d0b044
SE
1139MODULE_AUTHOR(BATADV_DRIVER_AUTHOR);
1140MODULE_DESCRIPTION(BATADV_DRIVER_DESC);
1141MODULE_SUPPORTED_DEVICE(BATADV_DRIVER_DEVICE);
1142MODULE_VERSION(BATADV_SOURCE_VERSION);