ath: Add regulatory mapping for APL13_WORLD
[linux/fpc-iii.git] / net / batman-adv / tvlv.c
blob77654f055f24b088a77c9e23e420171619cf88c3
1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "main.h"
20 #include <linux/byteorder/generic.h>
21 #include <linux/etherdevice.h>
22 #include <linux/fs.h>
23 #include <linux/if_ether.h>
24 #include <linux/kernel.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/netdevice.h>
29 #include <linux/pkt_sched.h>
30 #include <linux/rculist.h>
31 #include <linux/rcupdate.h>
32 #include <linux/skbuff.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/stddef.h>
36 #include <linux/string.h>
37 #include <linux/types.h>
39 #include "originator.h"
40 #include "packet.h"
41 #include "send.h"
42 #include "tvlv.h"
44 /**
45 * batadv_tvlv_handler_release - release tvlv handler from lists and queue for
46 * free after rcu grace period
47 * @ref: kref pointer of the tvlv
49 static void batadv_tvlv_handler_release(struct kref *ref)
51 struct batadv_tvlv_handler *tvlv_handler;
53 tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
54 kfree_rcu(tvlv_handler, rcu);
57 /**
58 * batadv_tvlv_handler_put - decrement the tvlv container refcounter and
59 * possibly release it
60 * @tvlv_handler: the tvlv handler to free
62 static void batadv_tvlv_handler_put(struct batadv_tvlv_handler *tvlv_handler)
64 kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
67 /**
68 * batadv_tvlv_handler_get - retrieve tvlv handler from the tvlv handler list
69 * based on the provided type and version (both need to match)
70 * @bat_priv: the bat priv with all the soft interface information
71 * @type: tvlv handler type to look for
72 * @version: tvlv handler version to look for
74 * Return: tvlv handler if found or NULL otherwise.
76 static struct batadv_tvlv_handler *
77 batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
79 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
81 rcu_read_lock();
82 hlist_for_each_entry_rcu(tvlv_handler_tmp,
83 &bat_priv->tvlv.handler_list, list) {
84 if (tvlv_handler_tmp->type != type)
85 continue;
87 if (tvlv_handler_tmp->version != version)
88 continue;
90 if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
91 continue;
93 tvlv_handler = tvlv_handler_tmp;
94 break;
96 rcu_read_unlock();
98 return tvlv_handler;
102 * batadv_tvlv_container_release - release tvlv from lists and free
103 * @ref: kref pointer of the tvlv
105 static void batadv_tvlv_container_release(struct kref *ref)
107 struct batadv_tvlv_container *tvlv;
109 tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
110 kfree(tvlv);
114 * batadv_tvlv_container_put - decrement the tvlv container refcounter and
115 * possibly release it
116 * @tvlv: the tvlv container to free
118 static void batadv_tvlv_container_put(struct batadv_tvlv_container *tvlv)
120 kref_put(&tvlv->refcount, batadv_tvlv_container_release);
124 * batadv_tvlv_container_get - retrieve tvlv container from the tvlv container
125 * list based on the provided type and version (both need to match)
126 * @bat_priv: the bat priv with all the soft interface information
127 * @type: tvlv container type to look for
128 * @version: tvlv container version to look for
130 * Has to be called with the appropriate locks being acquired
131 * (tvlv.container_list_lock).
133 * Return: tvlv container if found or NULL otherwise.
135 static struct batadv_tvlv_container *
136 batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
138 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
140 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
142 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
143 if (tvlv_tmp->tvlv_hdr.type != type)
144 continue;
146 if (tvlv_tmp->tvlv_hdr.version != version)
147 continue;
149 kref_get(&tvlv_tmp->refcount);
150 tvlv = tvlv_tmp;
151 break;
154 return tvlv;
158 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
159 * list entries
160 * @bat_priv: the bat priv with all the soft interface information
162 * Has to be called with the appropriate locks being acquired
163 * (tvlv.container_list_lock).
165 * Return: size of all currently registered tvlv containers in bytes.
167 static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
169 struct batadv_tvlv_container *tvlv;
170 u16 tvlv_len = 0;
172 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
174 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
175 tvlv_len += sizeof(struct batadv_tvlv_hdr);
176 tvlv_len += ntohs(tvlv->tvlv_hdr.len);
179 return tvlv_len;
183 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
184 * list
185 * @bat_priv: the bat priv with all the soft interface information
186 * @tvlv: the to be removed tvlv container
188 * Has to be called with the appropriate locks being acquired
189 * (tvlv.container_list_lock).
191 static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
192 struct batadv_tvlv_container *tvlv)
194 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
196 if (!tvlv)
197 return;
199 hlist_del(&tvlv->list);
201 /* first call to decrement the counter, second call to free */
202 batadv_tvlv_container_put(tvlv);
203 batadv_tvlv_container_put(tvlv);
207 * batadv_tvlv_container_unregister - unregister tvlv container based on the
208 * provided type and version (both need to match)
209 * @bat_priv: the bat priv with all the soft interface information
210 * @type: tvlv container type to unregister
211 * @version: tvlv container type to unregister
213 void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
214 u8 type, u8 version)
216 struct batadv_tvlv_container *tvlv;
218 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
219 tvlv = batadv_tvlv_container_get(bat_priv, type, version);
220 batadv_tvlv_container_remove(bat_priv, tvlv);
221 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
225 * batadv_tvlv_container_register - register tvlv type, version and content
226 * to be propagated with each (primary interface) OGM
227 * @bat_priv: the bat priv with all the soft interface information
228 * @type: tvlv container type
229 * @version: tvlv container version
230 * @tvlv_value: tvlv container content
231 * @tvlv_value_len: tvlv container content length
233 * If a container of the same type and version was already registered the new
234 * content is going to replace the old one.
236 void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
237 u8 type, u8 version,
238 void *tvlv_value, u16 tvlv_value_len)
240 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
242 if (!tvlv_value)
243 tvlv_value_len = 0;
245 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
246 if (!tvlv_new)
247 return;
249 tvlv_new->tvlv_hdr.version = version;
250 tvlv_new->tvlv_hdr.type = type;
251 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
253 memcpy(tvlv_new + 1, tvlv_value, ntohs(tvlv_new->tvlv_hdr.len));
254 INIT_HLIST_NODE(&tvlv_new->list);
255 kref_init(&tvlv_new->refcount);
257 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
258 tvlv_old = batadv_tvlv_container_get(bat_priv, type, version);
259 batadv_tvlv_container_remove(bat_priv, tvlv_old);
261 kref_get(&tvlv_new->refcount);
262 hlist_add_head(&tvlv_new->list, &bat_priv->tvlv.container_list);
263 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
265 /* don't return reference to new tvlv_container */
266 batadv_tvlv_container_put(tvlv_new);
270 * batadv_tvlv_realloc_packet_buff - reallocate packet buffer to accommodate
271 * requested packet size
272 * @packet_buff: packet buffer
273 * @packet_buff_len: packet buffer size
274 * @min_packet_len: requested packet minimum size
275 * @additional_packet_len: requested additional packet size on top of minimum
276 * size
278 * Return: true of the packet buffer could be changed to the requested size,
279 * false otherwise.
281 static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
282 int *packet_buff_len,
283 int min_packet_len,
284 int additional_packet_len)
286 unsigned char *new_buff;
288 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
290 /* keep old buffer if kmalloc should fail */
291 if (!new_buff)
292 return false;
294 memcpy(new_buff, *packet_buff, min_packet_len);
295 kfree(*packet_buff);
296 *packet_buff = new_buff;
297 *packet_buff_len = min_packet_len + additional_packet_len;
299 return true;
303 * batadv_tvlv_container_ogm_append - append tvlv container content to given
304 * OGM packet buffer
305 * @bat_priv: the bat priv with all the soft interface information
306 * @packet_buff: ogm packet buffer
307 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
308 * content
309 * @packet_min_len: ogm header size to be preserved for the OGM itself
311 * The ogm packet might be enlarged or shrunk depending on the current size
312 * and the size of the to-be-appended tvlv containers.
314 * Return: size of all appended tvlv containers in bytes.
316 u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
317 unsigned char **packet_buff,
318 int *packet_buff_len, int packet_min_len)
320 struct batadv_tvlv_container *tvlv;
321 struct batadv_tvlv_hdr *tvlv_hdr;
322 u16 tvlv_value_len;
323 void *tvlv_value;
324 bool ret;
326 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
327 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
329 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
330 packet_min_len, tvlv_value_len);
332 if (!ret)
333 goto end;
335 if (!tvlv_value_len)
336 goto end;
338 tvlv_value = (*packet_buff) + packet_min_len;
340 hlist_for_each_entry(tvlv, &bat_priv->tvlv.container_list, list) {
341 tvlv_hdr = tvlv_value;
342 tvlv_hdr->type = tvlv->tvlv_hdr.type;
343 tvlv_hdr->version = tvlv->tvlv_hdr.version;
344 tvlv_hdr->len = tvlv->tvlv_hdr.len;
345 tvlv_value = tvlv_hdr + 1;
346 memcpy(tvlv_value, tvlv + 1, ntohs(tvlv->tvlv_hdr.len));
347 tvlv_value = (u8 *)tvlv_value + ntohs(tvlv->tvlv_hdr.len);
350 end:
351 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
352 return tvlv_value_len;
356 * batadv_tvlv_call_handler - parse the given tvlv buffer to call the
357 * appropriate handlers
358 * @bat_priv: the bat priv with all the soft interface information
359 * @tvlv_handler: tvlv callback function handling the tvlv content
360 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
361 * @orig_node: orig node emitting the ogm packet
362 * @src: source mac address of the unicast packet
363 * @dst: destination mac address of the unicast packet
364 * @tvlv_value: tvlv content
365 * @tvlv_value_len: tvlv content length
367 * Return: success if handler was not found or the return value of the handler
368 * callback.
370 static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
371 struct batadv_tvlv_handler *tvlv_handler,
372 bool ogm_source,
373 struct batadv_orig_node *orig_node,
374 u8 *src, u8 *dst,
375 void *tvlv_value, u16 tvlv_value_len)
377 if (!tvlv_handler)
378 return NET_RX_SUCCESS;
380 if (ogm_source) {
381 if (!tvlv_handler->ogm_handler)
382 return NET_RX_SUCCESS;
384 if (!orig_node)
385 return NET_RX_SUCCESS;
387 tvlv_handler->ogm_handler(bat_priv, orig_node,
388 BATADV_NO_FLAGS,
389 tvlv_value, tvlv_value_len);
390 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
391 } else {
392 if (!src)
393 return NET_RX_SUCCESS;
395 if (!dst)
396 return NET_RX_SUCCESS;
398 if (!tvlv_handler->unicast_handler)
399 return NET_RX_SUCCESS;
401 return tvlv_handler->unicast_handler(bat_priv, src,
402 dst, tvlv_value,
403 tvlv_value_len);
406 return NET_RX_SUCCESS;
410 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
411 * appropriate handlers
412 * @bat_priv: the bat priv with all the soft interface information
413 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
414 * @orig_node: orig node emitting the ogm packet
415 * @src: source mac address of the unicast packet
416 * @dst: destination mac address of the unicast packet
417 * @tvlv_value: tvlv content
418 * @tvlv_value_len: tvlv content length
420 * Return: success when processing an OGM or the return value of all called
421 * handler callbacks.
423 int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
424 bool ogm_source,
425 struct batadv_orig_node *orig_node,
426 u8 *src, u8 *dst,
427 void *tvlv_value, u16 tvlv_value_len)
429 struct batadv_tvlv_handler *tvlv_handler;
430 struct batadv_tvlv_hdr *tvlv_hdr;
431 u16 tvlv_value_cont_len;
432 u8 cifnotfound = BATADV_TVLV_HANDLER_OGM_CIFNOTFND;
433 int ret = NET_RX_SUCCESS;
435 while (tvlv_value_len >= sizeof(*tvlv_hdr)) {
436 tvlv_hdr = tvlv_value;
437 tvlv_value_cont_len = ntohs(tvlv_hdr->len);
438 tvlv_value = tvlv_hdr + 1;
439 tvlv_value_len -= sizeof(*tvlv_hdr);
441 if (tvlv_value_cont_len > tvlv_value_len)
442 break;
444 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
445 tvlv_hdr->type,
446 tvlv_hdr->version);
448 ret |= batadv_tvlv_call_handler(bat_priv, tvlv_handler,
449 ogm_source, orig_node,
450 src, dst, tvlv_value,
451 tvlv_value_cont_len);
452 if (tvlv_handler)
453 batadv_tvlv_handler_put(tvlv_handler);
454 tvlv_value = (u8 *)tvlv_value + tvlv_value_cont_len;
455 tvlv_value_len -= tvlv_value_cont_len;
458 if (!ogm_source)
459 return ret;
461 rcu_read_lock();
462 hlist_for_each_entry_rcu(tvlv_handler,
463 &bat_priv->tvlv.handler_list, list) {
464 if ((tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND) &&
465 !(tvlv_handler->flags & BATADV_TVLV_HANDLER_OGM_CALLED))
466 tvlv_handler->ogm_handler(bat_priv, orig_node,
467 cifnotfound, NULL, 0);
469 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
471 rcu_read_unlock();
473 return NET_RX_SUCCESS;
477 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
478 * handlers
479 * @bat_priv: the bat priv with all the soft interface information
480 * @batadv_ogm_packet: ogm packet containing the tvlv containers
481 * @orig_node: orig node emitting the ogm packet
483 void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
484 struct batadv_ogm_packet *batadv_ogm_packet,
485 struct batadv_orig_node *orig_node)
487 void *tvlv_value;
488 u16 tvlv_value_len;
490 if (!batadv_ogm_packet)
491 return;
493 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
494 if (!tvlv_value_len)
495 return;
497 tvlv_value = batadv_ogm_packet + 1;
499 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
500 tvlv_value, tvlv_value_len);
504 * batadv_tvlv_handler_register - register tvlv handler based on the provided
505 * type and version (both need to match) for ogm tvlv payload and/or unicast
506 * payload
507 * @bat_priv: the bat priv with all the soft interface information
508 * @optr: ogm tvlv handler callback function. This function receives the orig
509 * node, flags and the tvlv content as argument to process.
510 * @uptr: unicast tvlv handler callback function. This function receives the
511 * source & destination of the unicast packet as well as the tvlv content
512 * to process.
513 * @type: tvlv handler type to be registered
514 * @version: tvlv handler version to be registered
515 * @flags: flags to enable or disable TVLV API behavior
517 void batadv_tvlv_handler_register(struct batadv_priv *bat_priv,
518 void (*optr)(struct batadv_priv *bat_priv,
519 struct batadv_orig_node *orig,
520 u8 flags,
521 void *tvlv_value,
522 u16 tvlv_value_len),
523 int (*uptr)(struct batadv_priv *bat_priv,
524 u8 *src, u8 *dst,
525 void *tvlv_value,
526 u16 tvlv_value_len),
527 u8 type, u8 version, u8 flags)
529 struct batadv_tvlv_handler *tvlv_handler;
531 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
532 if (tvlv_handler) {
533 batadv_tvlv_handler_put(tvlv_handler);
534 return;
537 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
538 if (!tvlv_handler)
539 return;
541 tvlv_handler->ogm_handler = optr;
542 tvlv_handler->unicast_handler = uptr;
543 tvlv_handler->type = type;
544 tvlv_handler->version = version;
545 tvlv_handler->flags = flags;
546 kref_init(&tvlv_handler->refcount);
547 INIT_HLIST_NODE(&tvlv_handler->list);
549 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
550 kref_get(&tvlv_handler->refcount);
551 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
552 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
554 /* don't return reference to new tvlv_handler */
555 batadv_tvlv_handler_put(tvlv_handler);
559 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
560 * 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 be unregistered
563 * @version: tvlv handler version to be unregistered
565 void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
566 u8 type, u8 version)
568 struct batadv_tvlv_handler *tvlv_handler;
570 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
571 if (!tvlv_handler)
572 return;
574 batadv_tvlv_handler_put(tvlv_handler);
575 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
576 hlist_del_rcu(&tvlv_handler->list);
577 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
578 batadv_tvlv_handler_put(tvlv_handler);
582 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
583 * specified host
584 * @bat_priv: the bat priv with all the soft interface information
585 * @src: source mac address of the unicast packet
586 * @dst: destination mac address of the unicast packet
587 * @type: tvlv type
588 * @version: tvlv version
589 * @tvlv_value: tvlv content
590 * @tvlv_value_len: tvlv content length
592 void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
593 u8 *dst, u8 type, u8 version,
594 void *tvlv_value, u16 tvlv_value_len)
596 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
597 struct batadv_tvlv_hdr *tvlv_hdr;
598 struct batadv_orig_node *orig_node;
599 struct sk_buff *skb;
600 unsigned char *tvlv_buff;
601 unsigned int tvlv_len;
602 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
603 int res;
605 orig_node = batadv_orig_hash_find(bat_priv, dst);
606 if (!orig_node)
607 return;
609 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
611 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
612 if (!skb)
613 goto out;
615 skb->priority = TC_PRIO_CONTROL;
616 skb_reserve(skb, ETH_HLEN);
617 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
618 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
619 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
620 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
621 unicast_tvlv_packet->ttl = BATADV_TTL;
622 unicast_tvlv_packet->reserved = 0;
623 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
624 unicast_tvlv_packet->align = 0;
625 ether_addr_copy(unicast_tvlv_packet->src, src);
626 ether_addr_copy(unicast_tvlv_packet->dst, dst);
628 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
629 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
630 tvlv_hdr->version = version;
631 tvlv_hdr->type = type;
632 tvlv_hdr->len = htons(tvlv_value_len);
633 tvlv_buff += sizeof(*tvlv_hdr);
634 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
636 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
637 if (res == -1)
638 kfree_skb(skb);
639 out:
640 batadv_orig_node_put(orig_node);