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/>.
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
);
260 hlist_add_head(&tvlv_new
->list
, &bat_priv
->tvlv
.container_list
);
261 spin_unlock_bh(&bat_priv
->tvlv
.container_list_lock
);
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 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
356 * @orig_node: orig node emitting the ogm packet
357 * @src: source mac address of the unicast packet
358 * @dst: destination mac address of the unicast packet
359 * @tvlv_value: tvlv content
360 * @tvlv_value_len: tvlv content length
362 * Return: success if handler was not found or the return value of the handler
365 static int batadv_tvlv_call_handler(struct batadv_priv
*bat_priv
,
366 struct batadv_tvlv_handler
*tvlv_handler
,
368 struct batadv_orig_node
*orig_node
,
370 void *tvlv_value
, u16 tvlv_value_len
)
373 return NET_RX_SUCCESS
;
376 if (!tvlv_handler
->ogm_handler
)
377 return NET_RX_SUCCESS
;
380 return NET_RX_SUCCESS
;
382 tvlv_handler
->ogm_handler(bat_priv
, orig_node
,
384 tvlv_value
, tvlv_value_len
);
385 tvlv_handler
->flags
|= BATADV_TVLV_HANDLER_OGM_CALLED
;
388 return NET_RX_SUCCESS
;
391 return NET_RX_SUCCESS
;
393 if (!tvlv_handler
->unicast_handler
)
394 return NET_RX_SUCCESS
;
396 return tvlv_handler
->unicast_handler(bat_priv
, src
,
401 return NET_RX_SUCCESS
;
405 * batadv_tvlv_containers_process - parse the given tvlv buffer to call the
406 * appropriate handlers
407 * @bat_priv: the bat priv with all the soft interface information
408 * @ogm_source: flag indicating whether the tvlv is an ogm or a unicast packet
409 * @orig_node: orig node emitting the ogm packet
410 * @src: source mac address of the unicast packet
411 * @dst: destination mac address of the unicast packet
412 * @tvlv_value: tvlv content
413 * @tvlv_value_len: tvlv content length
415 * Return: success when processing an OGM or the return value of all called
418 int batadv_tvlv_containers_process(struct batadv_priv
*bat_priv
,
420 struct batadv_orig_node
*orig_node
,
422 void *tvlv_value
, u16 tvlv_value_len
)
424 struct batadv_tvlv_handler
*tvlv_handler
;
425 struct batadv_tvlv_hdr
*tvlv_hdr
;
426 u16 tvlv_value_cont_len
;
427 u8 cifnotfound
= BATADV_TVLV_HANDLER_OGM_CIFNOTFND
;
428 int ret
= NET_RX_SUCCESS
;
430 while (tvlv_value_len
>= sizeof(*tvlv_hdr
)) {
431 tvlv_hdr
= tvlv_value
;
432 tvlv_value_cont_len
= ntohs(tvlv_hdr
->len
);
433 tvlv_value
= tvlv_hdr
+ 1;
434 tvlv_value_len
-= sizeof(*tvlv_hdr
);
436 if (tvlv_value_cont_len
> tvlv_value_len
)
439 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
,
443 ret
|= batadv_tvlv_call_handler(bat_priv
, tvlv_handler
,
444 ogm_source
, orig_node
,
445 src
, dst
, tvlv_value
,
446 tvlv_value_cont_len
);
448 batadv_tvlv_handler_put(tvlv_handler
);
449 tvlv_value
= (u8
*)tvlv_value
+ tvlv_value_cont_len
;
450 tvlv_value_len
-= tvlv_value_cont_len
;
457 hlist_for_each_entry_rcu(tvlv_handler
,
458 &bat_priv
->tvlv
.handler_list
, list
) {
459 if ((tvlv_handler
->flags
& BATADV_TVLV_HANDLER_OGM_CIFNOTFND
) &&
460 !(tvlv_handler
->flags
& BATADV_TVLV_HANDLER_OGM_CALLED
))
461 tvlv_handler
->ogm_handler(bat_priv
, orig_node
,
462 cifnotfound
, NULL
, 0);
464 tvlv_handler
->flags
&= ~BATADV_TVLV_HANDLER_OGM_CALLED
;
468 return NET_RX_SUCCESS
;
472 * batadv_tvlv_ogm_receive - process an incoming ogm and call the appropriate
474 * @bat_priv: the bat priv with all the soft interface information
475 * @batadv_ogm_packet: ogm packet containing the tvlv containers
476 * @orig_node: orig node emitting the ogm packet
478 void batadv_tvlv_ogm_receive(struct batadv_priv
*bat_priv
,
479 struct batadv_ogm_packet
*batadv_ogm_packet
,
480 struct batadv_orig_node
*orig_node
)
485 if (!batadv_ogm_packet
)
488 tvlv_value_len
= ntohs(batadv_ogm_packet
->tvlv_len
);
492 tvlv_value
= batadv_ogm_packet
+ 1;
494 batadv_tvlv_containers_process(bat_priv
, true, orig_node
, NULL
, NULL
,
495 tvlv_value
, tvlv_value_len
);
499 * batadv_tvlv_handler_register - register tvlv handler based on the provided
500 * type and version (both need to match) for ogm tvlv payload and/or unicast
502 * @bat_priv: the bat priv with all the soft interface information
503 * @optr: ogm tvlv handler callback function. This function receives the orig
504 * node, flags and the tvlv content as argument to process.
505 * @uptr: unicast tvlv handler callback function. This function receives the
506 * source & destination of the unicast packet as well as the tvlv content
508 * @type: tvlv handler type to be registered
509 * @version: tvlv handler version to be registered
510 * @flags: flags to enable or disable TVLV API behavior
512 void batadv_tvlv_handler_register(struct batadv_priv
*bat_priv
,
513 void (*optr
)(struct batadv_priv
*bat_priv
,
514 struct batadv_orig_node
*orig
,
518 int (*uptr
)(struct batadv_priv
*bat_priv
,
522 u8 type
, u8 version
, u8 flags
)
524 struct batadv_tvlv_handler
*tvlv_handler
;
526 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
, type
, version
);
528 batadv_tvlv_handler_put(tvlv_handler
);
532 tvlv_handler
= kzalloc(sizeof(*tvlv_handler
), GFP_ATOMIC
);
536 tvlv_handler
->ogm_handler
= optr
;
537 tvlv_handler
->unicast_handler
= uptr
;
538 tvlv_handler
->type
= type
;
539 tvlv_handler
->version
= version
;
540 tvlv_handler
->flags
= flags
;
541 kref_init(&tvlv_handler
->refcount
);
542 INIT_HLIST_NODE(&tvlv_handler
->list
);
544 spin_lock_bh(&bat_priv
->tvlv
.handler_list_lock
);
545 hlist_add_head_rcu(&tvlv_handler
->list
, &bat_priv
->tvlv
.handler_list
);
546 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
550 * batadv_tvlv_handler_unregister - unregister tvlv handler based on the
551 * provided type and version (both need to match)
552 * @bat_priv: the bat priv with all the soft interface information
553 * @type: tvlv handler type to be unregistered
554 * @version: tvlv handler version to be unregistered
556 void batadv_tvlv_handler_unregister(struct batadv_priv
*bat_priv
,
559 struct batadv_tvlv_handler
*tvlv_handler
;
561 tvlv_handler
= batadv_tvlv_handler_get(bat_priv
, type
, version
);
565 batadv_tvlv_handler_put(tvlv_handler
);
566 spin_lock_bh(&bat_priv
->tvlv
.handler_list_lock
);
567 hlist_del_rcu(&tvlv_handler
->list
);
568 spin_unlock_bh(&bat_priv
->tvlv
.handler_list_lock
);
569 batadv_tvlv_handler_put(tvlv_handler
);
573 * batadv_tvlv_unicast_send - send a unicast packet with tvlv payload to the
575 * @bat_priv: the bat priv with all the soft interface information
576 * @src: source mac address of the unicast packet
577 * @dst: destination mac address of the unicast packet
579 * @version: tvlv version
580 * @tvlv_value: tvlv content
581 * @tvlv_value_len: tvlv content length
583 void batadv_tvlv_unicast_send(struct batadv_priv
*bat_priv
, u8
*src
,
584 u8
*dst
, u8 type
, u8 version
,
585 void *tvlv_value
, u16 tvlv_value_len
)
587 struct batadv_unicast_tvlv_packet
*unicast_tvlv_packet
;
588 struct batadv_tvlv_hdr
*tvlv_hdr
;
589 struct batadv_orig_node
*orig_node
;
591 unsigned char *tvlv_buff
;
592 unsigned int tvlv_len
;
593 ssize_t hdr_len
= sizeof(*unicast_tvlv_packet
);
596 orig_node
= batadv_orig_hash_find(bat_priv
, dst
);
600 tvlv_len
= sizeof(*tvlv_hdr
) + tvlv_value_len
;
602 skb
= netdev_alloc_skb_ip_align(NULL
, ETH_HLEN
+ hdr_len
+ tvlv_len
);
606 skb
->priority
= TC_PRIO_CONTROL
;
607 skb_reserve(skb
, ETH_HLEN
);
608 tvlv_buff
= skb_put(skb
, sizeof(*unicast_tvlv_packet
) + tvlv_len
);
609 unicast_tvlv_packet
= (struct batadv_unicast_tvlv_packet
*)tvlv_buff
;
610 unicast_tvlv_packet
->packet_type
= BATADV_UNICAST_TVLV
;
611 unicast_tvlv_packet
->version
= BATADV_COMPAT_VERSION
;
612 unicast_tvlv_packet
->ttl
= BATADV_TTL
;
613 unicast_tvlv_packet
->reserved
= 0;
614 unicast_tvlv_packet
->tvlv_len
= htons(tvlv_len
);
615 unicast_tvlv_packet
->align
= 0;
616 ether_addr_copy(unicast_tvlv_packet
->src
, src
);
617 ether_addr_copy(unicast_tvlv_packet
->dst
, dst
);
619 tvlv_buff
= (unsigned char *)(unicast_tvlv_packet
+ 1);
620 tvlv_hdr
= (struct batadv_tvlv_hdr
*)tvlv_buff
;
621 tvlv_hdr
->version
= version
;
622 tvlv_hdr
->type
= type
;
623 tvlv_hdr
->len
= htons(tvlv_value_len
);
624 tvlv_buff
+= sizeof(*tvlv_hdr
);
625 memcpy(tvlv_buff
, tvlv_value
, tvlv_value_len
);
627 res
= batadv_send_skb_to_orig(skb
, orig_node
, NULL
);
631 batadv_orig_node_put(orig_node
);