1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
9 #include <linux/byteorder/generic.h>
10 #include <linux/container_of.h>
11 #include <linux/etherdevice.h>
12 #include <linux/gfp.h>
13 #include <linux/if_ether.h>
14 #include <linux/kref.h>
15 #include <linux/list.h>
16 #include <linux/lockdep.h>
17 #include <linux/netdevice.h>
18 #include <linux/pkt_sched.h>
19 #include <linux/rculist.h>
20 #include <linux/rcupdate.h>
21 #include <linux/skbuff.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/stddef.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <uapi/linux/batadv_packet.h>
29 #include "originator.h"
34 * batadv_tvlv_handler_release() - release tvlv handler from lists and queue for
35 * free after rcu grace period
36 * @ref: kref pointer of the tvlv
38 static void batadv_tvlv_handler_release(struct kref
*ref
)
40 struct batadv_tvlv_handler
*tvlv_handler
;
42 tvlv_handler
= container_of(ref
, struct batadv_tvlv_handler
, refcount
);
43 kfree_rcu(tvlv_handler
, rcu
);
47 * batadv_tvlv_handler_put() - decrement the tvlv container refcounter and
49 * @tvlv_handler: the tvlv handler to free
51 static void batadv_tvlv_handler_put(struct batadv_tvlv_handler
*tvlv_handler
)
56 kref_put(&tvlv_handler
->refcount
, batadv_tvlv_handler_release
);
60 * batadv_tvlv_handler_get() - retrieve tvlv handler from the tvlv handler list
61 * based on the provided type and version (both need to match)
62 * @bat_priv: the bat priv with all the soft interface information
63 * @type: tvlv handler type to look for
64 * @version: tvlv handler version to look for
66 * Return: tvlv handler if found or NULL otherwise.
68 static struct batadv_tvlv_handler
*
69 batadv_tvlv_handler_get(struct batadv_priv
*bat_priv
, u8 type
, u8 version
)
71 struct batadv_tvlv_handler
*tvlv_handler_tmp
, *tvlv_handler
= NULL
;
74 hlist_for_each_entry_rcu(tvlv_handler_tmp
,
75 &bat_priv
->tvlv
.handler_list
, list
) {
76 if (tvlv_handler_tmp
->type
!= type
)
79 if (tvlv_handler_tmp
->version
!= version
)
82 if (!kref_get_unless_zero(&tvlv_handler_tmp
->refcount
))
85 tvlv_handler
= tvlv_handler_tmp
;
94 * batadv_tvlv_container_release() - release tvlv from lists and free
95 * @ref: kref pointer of the tvlv
97 static void batadv_tvlv_container_release(struct kref
*ref
)
99 struct batadv_tvlv_container
*tvlv
;
101 tvlv
= container_of(ref
, struct batadv_tvlv_container
, refcount
);
106 * batadv_tvlv_container_put() - decrement the tvlv container refcounter and
107 * possibly release it
108 * @tvlv: the tvlv container to free
110 static void batadv_tvlv_container_put(struct batadv_tvlv_container
*tvlv
)
115 kref_put(&tvlv
->refcount
, batadv_tvlv_container_release
);
119 * batadv_tvlv_container_get() - retrieve tvlv container from the tvlv container
120 * list based on the provided type and version (both need to match)
121 * @bat_priv: the bat priv with all the soft interface information
122 * @type: tvlv container type to look for
123 * @version: tvlv container version to look for
125 * Has to be called with the appropriate locks being acquired
126 * (tvlv.container_list_lock).
128 * Return: tvlv container if found or NULL otherwise.
130 static struct batadv_tvlv_container
*
131 batadv_tvlv_container_get(struct batadv_priv
*bat_priv
, u8 type
, u8 version
)
133 struct batadv_tvlv_container
*tvlv_tmp
, *tvlv
= NULL
;
135 lockdep_assert_held(&bat_priv
->tvlv
.container_list_lock
);
137 hlist_for_each_entry(tvlv_tmp
, &bat_priv
->tvlv
.container_list
, list
) {
138 if (tvlv_tmp
->tvlv_hdr
.type
!= type
)
141 if (tvlv_tmp
->tvlv_hdr
.version
!= version
)
144 kref_get(&tvlv_tmp
->refcount
);
153 * batadv_tvlv_container_list_size() - calculate the size of the tvlv container
155 * @bat_priv: the bat priv with all the soft interface information
157 * Has to be called with the appropriate locks being acquired
158 * (tvlv.container_list_lock).
160 * Return: size of all currently registered tvlv containers in bytes.
162 static u16
batadv_tvlv_container_list_size(struct batadv_priv
*bat_priv
)
164 struct batadv_tvlv_container
*tvlv
;
167 lockdep_assert_held(&bat_priv
->tvlv
.container_list_lock
);
169 hlist_for_each_entry(tvlv
, &bat_priv
->tvlv
.container_list
, list
) {
170 tvlv_len
+= sizeof(struct batadv_tvlv_hdr
);
171 tvlv_len
+= ntohs(tvlv
->tvlv_hdr
.len
);
178 * batadv_tvlv_container_remove() - remove tvlv container from the tvlv
180 * @bat_priv: the bat priv with all the soft interface information
181 * @tvlv: the to be removed tvlv container
183 * Has to be called with the appropriate locks being acquired
184 * (tvlv.container_list_lock).
186 static void batadv_tvlv_container_remove(struct batadv_priv
*bat_priv
,
187 struct batadv_tvlv_container
*tvlv
)
189 lockdep_assert_held(&bat_priv
->tvlv
.container_list_lock
);
194 hlist_del(&tvlv
->list
);
196 /* first call to decrement the counter, second call to free */
197 batadv_tvlv_container_put(tvlv
);
198 batadv_tvlv_container_put(tvlv
);
202 * batadv_tvlv_container_unregister() - unregister tvlv container based on the
203 * provided type and version (both need to match)
204 * @bat_priv: the bat priv with all the soft interface information
205 * @type: tvlv container type to unregister
206 * @version: tvlv container type to unregister
208 void batadv_tvlv_container_unregister(struct batadv_priv
*bat_priv
,
211 struct batadv_tvlv_container
*tvlv
;
213 spin_lock_bh(&bat_priv
->tvlv
.container_list_lock
);
214 tvlv
= batadv_tvlv_container_get(bat_priv
, type
, version
);
215 batadv_tvlv_container_remove(bat_priv
, tvlv
);
216 spin_unlock_bh(&bat_priv
->tvlv
.container_list_lock
);
220 * batadv_tvlv_container_register() - register tvlv type, version and content
221 * to be propagated with each (primary interface) OGM
222 * @bat_priv: the bat priv with all the soft interface information
223 * @type: tvlv container type
224 * @version: tvlv container version
225 * @tvlv_value: tvlv container content
226 * @tvlv_value_len: tvlv container content length
228 * If a container of the same type and version was already registered the new
229 * content is going to replace the old one.
231 void batadv_tvlv_container_register(struct batadv_priv
*bat_priv
,
233 void *tvlv_value
, u16 tvlv_value_len
)
235 struct batadv_tvlv_container
*tvlv_old
, *tvlv_new
;
240 tvlv_new
= kzalloc(sizeof(*tvlv_new
) + tvlv_value_len
, GFP_ATOMIC
);
244 tvlv_new
->tvlv_hdr
.version
= version
;
245 tvlv_new
->tvlv_hdr
.type
= type
;
246 tvlv_new
->tvlv_hdr
.len
= htons(tvlv_value_len
);
248 memcpy(tvlv_new
+ 1, tvlv_value
, ntohs(tvlv_new
->tvlv_hdr
.len
));
249 INIT_HLIST_NODE(&tvlv_new
->list
);
250 kref_init(&tvlv_new
->refcount
);
252 spin_lock_bh(&bat_priv
->tvlv
.container_list_lock
);
253 tvlv_old
= batadv_tvlv_container_get(bat_priv
, type
, version
);
254 batadv_tvlv_container_remove(bat_priv
, tvlv_old
);
256 kref_get(&tvlv_new
->refcount
);
257 hlist_add_head(&tvlv_new
->list
, &bat_priv
->tvlv
.container_list
);
258 spin_unlock_bh(&bat_priv
->tvlv
.container_list_lock
);
260 /* don't return reference to new tvlv_container */
261 batadv_tvlv_container_put(tvlv_new
);
265 * batadv_tvlv_realloc_packet_buff() - reallocate packet buffer to accommodate
266 * requested packet size
267 * @packet_buff: packet buffer
268 * @packet_buff_len: packet buffer size
269 * @min_packet_len: requested packet minimum size
270 * @additional_packet_len: requested additional packet size on top of minimum
273 * Return: true of the packet buffer could be changed to the requested size,
276 static bool batadv_tvlv_realloc_packet_buff(unsigned char **packet_buff
,
277 int *packet_buff_len
,
279 int additional_packet_len
)
281 unsigned char *new_buff
;
283 new_buff
= kmalloc(min_packet_len
+ additional_packet_len
, GFP_ATOMIC
);
285 /* keep old buffer if kmalloc should fail */
289 memcpy(new_buff
, *packet_buff
, min_packet_len
);
291 *packet_buff
= new_buff
;
292 *packet_buff_len
= min_packet_len
+ additional_packet_len
;
298 * batadv_tvlv_container_ogm_append() - append tvlv container content to given
300 * @bat_priv: the bat priv with all the soft interface information
301 * @packet_buff: ogm packet buffer
302 * @packet_buff_len: ogm packet buffer size including ogm header and tvlv
304 * @packet_min_len: ogm header size to be preserved for the OGM itself
306 * The ogm packet might be enlarged or shrunk depending on the current size
307 * and the size of the to-be-appended tvlv containers.
309 * Return: size of all appended tvlv containers in bytes.
311 u16
batadv_tvlv_container_ogm_append(struct batadv_priv
*bat_priv
,
312 unsigned char **packet_buff
,
313 int *packet_buff_len
, int packet_min_len
)
315 struct batadv_tvlv_container
*tvlv
;
316 struct batadv_tvlv_hdr
*tvlv_hdr
;
321 spin_lock_bh(&bat_priv
->tvlv
.container_list_lock
);
322 tvlv_value_len
= batadv_tvlv_container_list_size(bat_priv
);
324 ret
= batadv_tvlv_realloc_packet_buff(packet_buff
, packet_buff_len
,
325 packet_min_len
, tvlv_value_len
);
333 tvlv_value
= (*packet_buff
) + packet_min_len
;
335 hlist_for_each_entry(tvlv
, &bat_priv
->tvlv
.container_list
, list
) {
336 tvlv_hdr
= tvlv_value
;
337 tvlv_hdr
->type
= tvlv
->tvlv_hdr
.type
;
338 tvlv_hdr
->version
= tvlv
->tvlv_hdr
.version
;
339 tvlv_hdr
->len
= tvlv
->tvlv_hdr
.len
;
340 tvlv_value
= tvlv_hdr
+ 1;
341 memcpy(tvlv_value
, tvlv
+ 1, ntohs(tvlv
->tvlv_hdr
.len
));
342 tvlv_value
= (u8
*)tvlv_value
+ ntohs(tvlv
->tvlv_hdr
.len
);
346 spin_unlock_bh(&bat_priv
->tvlv
.container_list_lock
);
347 return tvlv_value_len
;
351 * batadv_tvlv_call_handler() - parse the given tvlv buffer to call the
352 * appropriate handlers
353 * @bat_priv: the bat priv with all the soft interface information
354 * @tvlv_handler: tvlv callback function handling the tvlv content
355 * @packet_type: indicates for which packet type the TVLV handler is called
356 * @orig_node: orig node emitting the ogm packet
357 * @skb: the skb the TVLV handler is called for
358 * @tvlv_value: tvlv content
359 * @tvlv_value_len: tvlv content length
361 * Return: success if the handler was not found or the return value of the
364 static int batadv_tvlv_call_handler(struct batadv_priv
*bat_priv
,
365 struct batadv_tvlv_handler
*tvlv_handler
,
367 struct batadv_orig_node
*orig_node
,
368 struct sk_buff
*skb
, void *tvlv_value
,
371 unsigned int tvlv_offset
;
375 return NET_RX_SUCCESS
;
377 switch (packet_type
) {
380 if (!tvlv_handler
->ogm_handler
)
381 return NET_RX_SUCCESS
;
384 return NET_RX_SUCCESS
;
386 tvlv_handler
->ogm_handler(bat_priv
, orig_node
,
388 tvlv_value
, tvlv_value_len
);
389 tvlv_handler
->flags
|= BATADV_TVLV_HANDLER_OGM_CALLED
;
391 case BATADV_UNICAST_TVLV
:
393 return NET_RX_SUCCESS
;
395 if (!tvlv_handler
->unicast_handler
)
396 return NET_RX_SUCCESS
;
398 src
= ((struct batadv_unicast_tvlv_packet
*)skb
->data
)->src
;
399 dst
= ((struct batadv_unicast_tvlv_packet
*)skb
->data
)->dst
;
401 return tvlv_handler
->unicast_handler(bat_priv
, src
,
406 return NET_RX_SUCCESS
;
408 if (!tvlv_handler
->mcast_handler
)
409 return NET_RX_SUCCESS
;
411 tvlv_offset
= (unsigned char *)tvlv_value
- skb
->data
;
412 skb_set_network_header(skb
, tvlv_offset
);
413 skb_set_transport_header(skb
, tvlv_offset
+ tvlv_value_len
);
415 return tvlv_handler
->mcast_handler(bat_priv
, skb
);
418 return NET_RX_SUCCESS
;
422 * batadv_tvlv_containers_process() - parse the given tvlv buffer to call the
423 * appropriate handlers
424 * @bat_priv: the bat priv with all the soft interface information
425 * @packet_type: indicates for which packet type the TVLV handler is called
426 * @orig_node: orig node emitting the ogm packet
427 * @skb: the skb the TVLV handler is called for
428 * @tvlv_value: tvlv content
429 * @tvlv_value_len: tvlv content length
431 * Return: success when processing an OGM or the return value of all called
434 int batadv_tvlv_containers_process(struct batadv_priv
*bat_priv
,
436 struct batadv_orig_node
*orig_node
,
437 struct sk_buff
*skb
, void *tvlv_value
,
440 struct batadv_tvlv_handler
*tvlv_handler
;
441 struct batadv_tvlv_hdr
*tvlv_hdr
;
442 u16 tvlv_value_cont_len
;
443 u8 cifnotfound
= BATADV_TVLV_HANDLER_OGM_CIFNOTFND
;
444 int ret
= NET_RX_SUCCESS
;
446 while (tvlv_value_len
>= sizeof(*tvlv_hdr
)) {
447 tvlv_hdr
= tvlv_value
;
448 tvlv_value_cont_len
= ntohs(tvlv_hdr
->len
);
449 tvlv_value
= tvlv_hdr
+ 1;
450 tvlv_value_len
-= sizeof(*tvlv_hdr
);
452 if (tvlv_value_cont_len
> tvlv_value_len
)
455 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
,
459 ret
|= batadv_tvlv_call_handler(bat_priv
, tvlv_handler
,
460 packet_type
, orig_node
, skb
,
462 tvlv_value_cont_len
);
463 batadv_tvlv_handler_put(tvlv_handler
);
464 tvlv_value
= (u8
*)tvlv_value
+ tvlv_value_cont_len
;
465 tvlv_value_len
-= tvlv_value_cont_len
;
468 if (packet_type
!= BATADV_IV_OGM
&&
469 packet_type
!= BATADV_OGM2
)
473 hlist_for_each_entry_rcu(tvlv_handler
,
474 &bat_priv
->tvlv
.handler_list
, list
) {
475 if (!tvlv_handler
->ogm_handler
)
478 if ((tvlv_handler
->flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
) &&
479 !(tvlv_handler
->flags
& BATADV_TVLV_HANDLER_OGM_CALLED
))
480 tvlv_handler
->ogm_handler(bat_priv
, orig_node
,
481 cifnotfound
, NULL
, 0);
483 tvlv_handler
->flags
&= ~BATADV_TVLV_HANDLER_OGM_CALLED
;
487 return NET_RX_SUCCESS
;
491 * batadv_tvlv_ogm_receive() - process an incoming ogm and call the appropriate
493 * @bat_priv: the bat priv with all the soft interface information
494 * @batadv_ogm_packet: ogm packet containing the tvlv containers
495 * @orig_node: orig node emitting the ogm packet
497 void batadv_tvlv_ogm_receive(struct batadv_priv
*bat_priv
,
498 struct batadv_ogm_packet
*batadv_ogm_packet
,
499 struct batadv_orig_node
*orig_node
)
504 if (!batadv_ogm_packet
)
507 tvlv_value_len
= ntohs(batadv_ogm_packet
->tvlv_len
);
511 tvlv_value
= batadv_ogm_packet
+ 1;
513 batadv_tvlv_containers_process(bat_priv
, BATADV_IV_OGM
, orig_node
, NULL
,
514 tvlv_value
, tvlv_value_len
);
518 * batadv_tvlv_handler_register() - register tvlv handler based on the provided
519 * type and version (both need to match) for ogm tvlv payload and/or unicast
521 * @bat_priv: the bat priv with all the soft interface information
522 * @optr: ogm tvlv handler callback function. This function receives the orig
523 * node, flags and the tvlv content as argument to process.
524 * @uptr: unicast tvlv handler callback function. This function receives the
525 * source & destination of the unicast packet as well as the tvlv content
527 * @mptr: multicast packet tvlv handler callback function. This function
528 * receives the full skb to process, with the skb network header pointing
529 * to the current tvlv and the skb transport header pointing to the first
530 * byte after the current tvlv.
531 * @type: tvlv handler type to be registered
532 * @version: tvlv handler version to be registered
533 * @flags: flags to enable or disable TVLV API behavior
535 void batadv_tvlv_handler_register(struct batadv_priv
*bat_priv
,
536 void (*optr
)(struct batadv_priv
*bat_priv
,
537 struct batadv_orig_node
*orig
,
541 int (*uptr
)(struct batadv_priv
*bat_priv
,
545 int (*mptr
)(struct batadv_priv
*bat_priv
,
546 struct sk_buff
*skb
),
547 u8 type
, u8 version
, u8 flags
)
549 struct batadv_tvlv_handler
*tvlv_handler
;
551 spin_lock_bh(&bat_priv
->tvlv
.handler_list_lock
);
553 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
, type
, version
);
555 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
556 batadv_tvlv_handler_put(tvlv_handler
);
560 tvlv_handler
= kzalloc(sizeof(*tvlv_handler
), GFP_ATOMIC
);
562 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
566 tvlv_handler
->ogm_handler
= optr
;
567 tvlv_handler
->unicast_handler
= uptr
;
568 tvlv_handler
->mcast_handler
= mptr
;
569 tvlv_handler
->type
= type
;
570 tvlv_handler
->version
= version
;
571 tvlv_handler
->flags
= flags
;
572 kref_init(&tvlv_handler
->refcount
);
573 INIT_HLIST_NODE(&tvlv_handler
->list
);
575 kref_get(&tvlv_handler
->refcount
);
576 hlist_add_head_rcu(&tvlv_handler
->list
, &bat_priv
->tvlv
.handler_list
);
577 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
579 /* don't return reference to new tvlv_handler */
580 batadv_tvlv_handler_put(tvlv_handler
);
584 * batadv_tvlv_handler_unregister() - unregister tvlv handler based on the
585 * provided type and version (both need to match)
586 * @bat_priv: the bat priv with all the soft interface information
587 * @type: tvlv handler type to be unregistered
588 * @version: tvlv handler version to be unregistered
590 void batadv_tvlv_handler_unregister(struct batadv_priv
*bat_priv
,
593 struct batadv_tvlv_handler
*tvlv_handler
;
595 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
, type
, version
);
599 batadv_tvlv_handler_put(tvlv_handler
);
600 spin_lock_bh(&bat_priv
->tvlv
.handler_list_lock
);
601 hlist_del_rcu(&tvlv_handler
->list
);
602 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
603 batadv_tvlv_handler_put(tvlv_handler
);
607 * batadv_tvlv_unicast_send() - send a unicast packet with tvlv payload to the
609 * @bat_priv: the bat priv with all the soft interface information
610 * @src: source mac address of the unicast packet
611 * @dst: destination mac address of the unicast packet
613 * @version: tvlv version
614 * @tvlv_value: tvlv content
615 * @tvlv_value_len: tvlv content length
617 void batadv_tvlv_unicast_send(struct batadv_priv
*bat_priv
, const u8
*src
,
618 const u8
*dst
, u8 type
, u8 version
,
619 void *tvlv_value
, u16 tvlv_value_len
)
621 struct batadv_unicast_tvlv_packet
*unicast_tvlv_packet
;
622 struct batadv_tvlv_hdr
*tvlv_hdr
;
623 struct batadv_orig_node
*orig_node
;
625 unsigned char *tvlv_buff
;
626 unsigned int tvlv_len
;
627 ssize_t hdr_len
= sizeof(*unicast_tvlv_packet
);
629 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
633 tvlv_len
= sizeof(*tvlv_hdr
) + tvlv_value_len
;
635 skb
= netdev_alloc_skb_ip_align(NULL
, ETH_HLEN
+ hdr_len
+ tvlv_len
);
639 skb
->priority
= TC_PRIO_CONTROL
;
640 skb_reserve(skb
, ETH_HLEN
);
641 tvlv_buff
= skb_put(skb
, sizeof(*unicast_tvlv_packet
) + tvlv_len
);
642 unicast_tvlv_packet
= (struct batadv_unicast_tvlv_packet
*)tvlv_buff
;
643 unicast_tvlv_packet
->packet_type
= BATADV_UNICAST_TVLV
;
644 unicast_tvlv_packet
->version
= BATADV_COMPAT_VERSION
;
645 unicast_tvlv_packet
->ttl
= BATADV_TTL
;
646 unicast_tvlv_packet
->reserved
= 0;
647 unicast_tvlv_packet
->tvlv_len
= htons(tvlv_len
);
648 unicast_tvlv_packet
->align
= 0;
649 ether_addr_copy(unicast_tvlv_packet
->src
, src
);
650 ether_addr_copy(unicast_tvlv_packet
->dst
, dst
);
652 tvlv_buff
= (unsigned char *)(unicast_tvlv_packet
+ 1);
653 tvlv_hdr
= (struct batadv_tvlv_hdr
*)tvlv_buff
;
654 tvlv_hdr
->version
= version
;
655 tvlv_hdr
->type
= type
;
656 tvlv_hdr
->len
= htons(tvlv_value_len
);
657 tvlv_buff
+= sizeof(*tvlv_hdr
);
658 memcpy(tvlv_buff
, tvlv_value
, tvlv_value_len
);
660 batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
662 batadv_orig_node_put(orig_node
);