1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "main.h"
19
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>
38
39#include "originator.h"
40#include "packet.h"
41#include "send.h"
42#include "tvlv.h"
43
44
45
46
47
48
49static void batadv_tvlv_handler_release(struct kref *ref)
50{
51 struct batadv_tvlv_handler *tvlv_handler;
52
53 tvlv_handler = container_of(ref, struct batadv_tvlv_handler, refcount);
54 kfree_rcu(tvlv_handler, rcu);
55}
56
57
58
59
60
61
62static void batadv_tvlv_handler_put(struct batadv_tvlv_handler *tvlv_handler)
63{
64 kref_put(&tvlv_handler->refcount, batadv_tvlv_handler_release);
65}
66
67
68
69
70
71
72
73
74
75
76static struct batadv_tvlv_handler *
77batadv_tvlv_handler_get(struct batadv_priv *bat_priv, u8 type, u8 version)
78{
79 struct batadv_tvlv_handler *tvlv_handler_tmp, *tvlv_handler = NULL;
80
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;
86
87 if (tvlv_handler_tmp->version != version)
88 continue;
89
90 if (!kref_get_unless_zero(&tvlv_handler_tmp->refcount))
91 continue;
92
93 tvlv_handler = tvlv_handler_tmp;
94 break;
95 }
96 rcu_read_unlock();
97
98 return tvlv_handler;
99}
100
101
102
103
104
105static void batadv_tvlv_container_release(struct kref *ref)
106{
107 struct batadv_tvlv_container *tvlv;
108
109 tvlv = container_of(ref, struct batadv_tvlv_container, refcount);
110 kfree(tvlv);
111}
112
113
114
115
116
117
118static void batadv_tvlv_container_put(struct batadv_tvlv_container *tvlv)
119{
120 kref_put(&tvlv->refcount, batadv_tvlv_container_release);
121}
122
123
124
125
126
127
128
129
130
131
132
133
134
135static struct batadv_tvlv_container *
136batadv_tvlv_container_get(struct batadv_priv *bat_priv, u8 type, u8 version)
137{
138 struct batadv_tvlv_container *tvlv_tmp, *tvlv = NULL;
139
140 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
141
142 hlist_for_each_entry(tvlv_tmp, &bat_priv->tvlv.container_list, list) {
143 if (tvlv_tmp->tvlv_hdr.type != type)
144 continue;
145
146 if (tvlv_tmp->tvlv_hdr.version != version)
147 continue;
148
149 kref_get(&tvlv_tmp->refcount);
150 tvlv = tvlv_tmp;
151 break;
152 }
153
154 return tvlv;
155}
156
157
158
159
160
161
162
163
164
165
166
167static u16 batadv_tvlv_container_list_size(struct batadv_priv *bat_priv)
168{
169 struct batadv_tvlv_container *tvlv;
170 u16 tvlv_len = 0;
171
172 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
173
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);
177 }
178
179 return tvlv_len;
180}
181
182
183
184
185
186
187
188
189
190
191static void batadv_tvlv_container_remove(struct batadv_priv *bat_priv,
192 struct batadv_tvlv_container *tvlv)
193{
194 lockdep_assert_held(&bat_priv->tvlv.container_list_lock);
195
196 if (!tvlv)
197 return;
198
199 hlist_del(&tvlv->list);
200
201
202 batadv_tvlv_container_put(tvlv);
203 batadv_tvlv_container_put(tvlv);
204}
205
206
207
208
209
210
211
212
213void batadv_tvlv_container_unregister(struct batadv_priv *bat_priv,
214 u8 type, u8 version)
215{
216 struct batadv_tvlv_container *tvlv;
217
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);
222}
223
224
225
226
227
228
229
230
231
232
233
234
235
236void batadv_tvlv_container_register(struct batadv_priv *bat_priv,
237 u8 type, u8 version,
238 void *tvlv_value, u16 tvlv_value_len)
239{
240 struct batadv_tvlv_container *tvlv_old, *tvlv_new;
241
242 if (!tvlv_value)
243 tvlv_value_len = 0;
244
245 tvlv_new = kzalloc(sizeof(*tvlv_new) + tvlv_value_len, GFP_ATOMIC);
246 if (!tvlv_new)
247 return;
248
249 tvlv_new->tvlv_hdr.version = version;
250 tvlv_new->tvlv_hdr.type = type;
251 tvlv_new->tvlv_hdr.len = htons(tvlv_value_len);
252
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);
256
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
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);
264
265
266 batadv_tvlv_container_put(tvlv_new);
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281static 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)
285{
286 unsigned char *new_buff;
287
288 new_buff = kmalloc(min_packet_len + additional_packet_len, GFP_ATOMIC);
289
290
291 if (!new_buff)
292 return false;
293
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;
298
299 return true;
300}
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316u16 batadv_tvlv_container_ogm_append(struct batadv_priv *bat_priv,
317 unsigned char **packet_buff,
318 int *packet_buff_len, int packet_min_len)
319{
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;
325
326 spin_lock_bh(&bat_priv->tvlv.container_list_lock);
327 tvlv_value_len = batadv_tvlv_container_list_size(bat_priv);
328
329 ret = batadv_tvlv_realloc_packet_buff(packet_buff, packet_buff_len,
330 packet_min_len, tvlv_value_len);
331
332 if (!ret)
333 goto end;
334
335 if (!tvlv_value_len)
336 goto end;
337
338 tvlv_value = (*packet_buff) + packet_min_len;
339
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);
348 }
349
350end:
351 spin_unlock_bh(&bat_priv->tvlv.container_list_lock);
352 return tvlv_value_len;
353}
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370static 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)
376{
377 if (!tvlv_handler)
378 return NET_RX_SUCCESS;
379
380 if (ogm_source) {
381 if (!tvlv_handler->ogm_handler)
382 return NET_RX_SUCCESS;
383
384 if (!orig_node)
385 return NET_RX_SUCCESS;
386
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;
394
395 if (!dst)
396 return NET_RX_SUCCESS;
397
398 if (!tvlv_handler->unicast_handler)
399 return NET_RX_SUCCESS;
400
401 return tvlv_handler->unicast_handler(bat_priv, src,
402 dst, tvlv_value,
403 tvlv_value_len);
404 }
405
406 return NET_RX_SUCCESS;
407}
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423int 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)
428{
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;
434
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);
440
441 if (tvlv_value_cont_len > tvlv_value_len)
442 break;
443
444 tvlv_handler = batadv_tvlv_handler_get(bat_priv,
445 tvlv_hdr->type,
446 tvlv_hdr->version);
447
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;
456 }
457
458 if (!ogm_source)
459 return ret;
460
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);
468
469 tvlv_handler->flags &= ~BATADV_TVLV_HANDLER_OGM_CALLED;
470 }
471 rcu_read_unlock();
472
473 return NET_RX_SUCCESS;
474}
475
476
477
478
479
480
481
482
483void batadv_tvlv_ogm_receive(struct batadv_priv *bat_priv,
484 struct batadv_ogm_packet *batadv_ogm_packet,
485 struct batadv_orig_node *orig_node)
486{
487 void *tvlv_value;
488 u16 tvlv_value_len;
489
490 if (!batadv_ogm_packet)
491 return;
492
493 tvlv_value_len = ntohs(batadv_ogm_packet->tvlv_len);
494 if (!tvlv_value_len)
495 return;
496
497 tvlv_value = batadv_ogm_packet + 1;
498
499 batadv_tvlv_containers_process(bat_priv, true, orig_node, NULL, NULL,
500 tvlv_value, tvlv_value_len);
501}
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517void 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)
528{
529 struct batadv_tvlv_handler *tvlv_handler;
530
531 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
532 if (tvlv_handler) {
533 batadv_tvlv_handler_put(tvlv_handler);
534 return;
535 }
536
537 tvlv_handler = kzalloc(sizeof(*tvlv_handler), GFP_ATOMIC);
538 if (!tvlv_handler)
539 return;
540
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);
548
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);
553
554
555 batadv_tvlv_handler_put(tvlv_handler);
556}
557
558
559
560
561
562
563
564
565void batadv_tvlv_handler_unregister(struct batadv_priv *bat_priv,
566 u8 type, u8 version)
567{
568 struct batadv_tvlv_handler *tvlv_handler;
569
570 tvlv_handler = batadv_tvlv_handler_get(bat_priv, type, version);
571 if (!tvlv_handler)
572 return;
573
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);
579}
580
581
582
583
584
585
586
587
588
589
590
591
592void 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)
595{
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
604 orig_node = batadv_orig_hash_find(bat_priv, dst);
605 if (!orig_node)
606 return;
607
608 tvlv_len = sizeof(*tvlv_hdr) + tvlv_value_len;
609
610 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + hdr_len + tvlv_len);
611 if (!skb)
612 goto out;
613
614 skb->priority = TC_PRIO_CONTROL;
615 skb_reserve(skb, ETH_HLEN);
616 tvlv_buff = skb_put(skb, sizeof(*unicast_tvlv_packet) + tvlv_len);
617 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)tvlv_buff;
618 unicast_tvlv_packet->packet_type = BATADV_UNICAST_TVLV;
619 unicast_tvlv_packet->version = BATADV_COMPAT_VERSION;
620 unicast_tvlv_packet->ttl = BATADV_TTL;
621 unicast_tvlv_packet->reserved = 0;
622 unicast_tvlv_packet->tvlv_len = htons(tvlv_len);
623 unicast_tvlv_packet->align = 0;
624 ether_addr_copy(unicast_tvlv_packet->src, src);
625 ether_addr_copy(unicast_tvlv_packet->dst, dst);
626
627 tvlv_buff = (unsigned char *)(unicast_tvlv_packet + 1);
628 tvlv_hdr = (struct batadv_tvlv_hdr *)tvlv_buff;
629 tvlv_hdr->version = version;
630 tvlv_hdr->type = type;
631 tvlv_hdr->len = htons(tvlv_value_len);
632 tvlv_buff += sizeof(*tvlv_hdr);
633 memcpy(tvlv_buff, tvlv_value, tvlv_value_len);
634
635 batadv_send_skb_to_orig(skb, orig_node, NULL);
636out:
637 batadv_orig_node_put(orig_node);
638}
639