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