1 /* Copyright (C) 2007-2017 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/>.
20 #include <linux/byteorder/generic.h>
21 #include <linux/etherdevice.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"
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);
58 * batadv_tvlv_handler_put - decrement the tvlv container refcounter and
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);
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;
82 hlist_for_each_entry_rcu(tvlv_handler_tmp,
83 &bat_priv->tvlv.handler_list, list) {
84 if (tvlv_handler_tmp->type != type)
87 if (tvlv_handler_tmp->version != version)
90 if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
93 tvlv_handler = tvlv_handler_tmp;
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);
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)
146 if (tvlv_tmp->tvlv_hdr.version != version)
149 kref_get(&tvlv_tmp->refcount);
158 * batadv_tvlv_container_list_size - calculate the size of the tvlv container
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;
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);
183 * batadv_tvlv_container_remove - remove tvlv container from the tvlv container
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);
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,
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,
238 void *tvlv_value, u16 tvlv_value_len)
240 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
245 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
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
278 * Return: true of the packet buffer could be changed to the requested size,
281 static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff,
282 int *packet_buff_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 */
294 memcpy(new_buff, *packet_buff, min_packet_len);
296 *packet_buff = new_buff;
297 *packet_buff_len = min_packet_len + additional_packet_len;
303 * batadv_tvlv_container_ogm_append - append tvlv container content to given
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
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;
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);
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);
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
370 static int batadv_tvlv_call_handler(struct batadv_priv *bat_priv,
371 struct batadv_tvlv_handler *tvlv_handler,
373 struct batadv_orig_node *orig_node,
375 void *tvlv_value, u16 tvlv_value_len)
378 return NET_RX_SUCCESS;
381 if (!tvlv_handler->ogm_handler)
382 return NET_RX_SUCCESS;
385 return NET_RX_SUCCESS;
387 tvlv_handler->ogm_handler(bat_priv, orig_node,
389 tvlv_value, tvlv_value_len);
390 tvlv_handler->flags |= BATADV_TVLV_HANDLER_OGM_CALLED;
393 return NET_RX_SUCCESS;
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,
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
423 int batadv_tvlv_containers_process(struct batadv_priv *bat_priv,
425 struct batadv_orig_node *orig_node,
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)
444 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
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);
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;
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;
473 return NET_RX_SUCCESS;
477 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
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)
490 if (!batadv_ogm_packet)
493 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
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
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
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,
523 int (*uptr)(struct batadv_priv *bat_priv,
527 u8 type, u8 version, u8 flags)
529 struct batadv_tvlv_handler *tvlv_handler;
531 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
533 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
535 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
536 batadv_tvlv_handler_put(tvlv_handler);
540 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
542 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
546 tvlv_handler->ogm_handler = optr;
547 tvlv_handler->unicast_handler = uptr;
548 tvlv_handler->type = type;
549 tvlv_handler->version = version;
550 tvlv_handler->flags = flags;
551 kref_init(&tvlv_handler->refcount);
552 INIT_HLIST_NODE(&tvlv_handler->list);
554 kref_get(&tvlv_handler->refcount);
555 hlist_add_head_rcu(&tvlv_handler->list, &bat_priv->tvlv.handler_list);
556 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
558 /* don't return reference to new tvlv_handler */
559 batadv_tvlv_handler_put(tvlv_handler);
563 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
564 * provided type and version (both need to match)
565 * @bat_priv: the bat priv with all the soft interface information
566 * @type: tvlv handler type to be unregistered
567 * @version: tvlv handler version to be unregistered
569 void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
572 struct batadv_tvlv_handler *tvlv_handler;
574 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
578 batadv_tvlv_handler_put(tvlv_handler);
579 spin_lock_bh(&bat_priv->tvlv.handler_list_lock);
580 hlist_del_rcu(&tvlv_handler->list);
581 spin_unlock_bh(&bat_priv->tvlv.handler_list_lock);
582 batadv_tvlv_handler_put(tvlv_handler);
586 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
588 * @bat_priv: the bat priv with all the soft interface information
589 * @src: source mac address of the unicast packet
590 * @dst: destination mac address of the unicast packet
592 * @version: tvlv version
593 * @tvlv_value: tvlv content
594 * @tvlv_value_len: tvlv content length
596 void batadv_tvlv_unicast_send(struct batadv_priv *bat_priv, u8 *src,
597 u8 *dst, u8 type, u8 version,
598 void *tvlv_value, u16 tvlv_value_len)
600 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
601 struct batadv_tvlv_hdr *tvlv_hdr;
602 struct batadv_orig_node *orig_node;
604 unsigned char *tvlv_buff;
605 unsigned int tvlv_len;
606 ssize_t hdr_len = sizeof(*unicast_tvlv_packet);
608 orig_node = batadv_orig_hash_find(bat_priv, dst);
612 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
614 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
618 skb->priority = TC_PRIO_CONTROL;
619 skb_reserve(skb, ETH_HLEN);
620 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
621 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
622 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
623 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
624 unicast_tvlv_packet->ttl = BATADV_TTL;
625 unicast_tvlv_packet->reserved = 0;
626 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
627 unicast_tvlv_packet->align = 0;
628 ether_addr_copy(unicast_tvlv_packet->src, src);
629 ether_addr_copy(unicast_tvlv_packet->dst, dst);
631 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
632 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
633 tvlv_hdr->version = version;
634 tvlv_hdr->type = type;
635 tvlv_hdr->len = htons(tvlv_value_len);
636 tvlv_buff += sizeof(*tvlv_hdr);
637 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
639 batadv_send_skb_to_orig(skb, orig_node, NULL);
641 batadv_orig_node_put(orig_node);