batman-adv: make struct batadv_orig_node algorithm agnostic
[deliverable/linux.git] / net / batman-adv / bat_iv_ogm.c
1 /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
18 */
19
20 #include "main.h"
21 #include "translation-table.h"
22 #include "originator.h"
23 #include "routing.h"
24 #include "gateway_common.h"
25 #include "gateway_client.h"
26 #include "hard-interface.h"
27 #include "send.h"
28 #include "bat_algo.h"
29 #include "network-coding.h"
30
31
32 /**
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36 * neighbor)
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
39 */
40 enum batadv_dup_status {
41 BATADV_NO_DUP = 0,
42 BATADV_ORIG_DUP,
43 BATADV_NEIGH_DUP,
44 BATADV_PROTECTED,
45 };
46
47 /**
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
52 */
53 static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54 uint8_t value)
55 {
56 lq_recv[*lq_index] = value;
57 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
58 }
59
60 /**
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
64 *
65 * Returns computed average value.
66 */
67 static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
68 {
69 const uint8_t *ptr;
70 uint16_t count = 0, i = 0, sum = 0;
71
72 ptr = lq_recv;
73
74 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75 if (*ptr != 0) {
76 count++;
77 sum += *ptr;
78 }
79
80 i++;
81 ptr++;
82 }
83
84 if (count == 0)
85 return 0;
86
87 return (uint8_t)(sum / count);
88 }
89
90 /**
91 * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
92 * @bat_priv: the bat priv with all the soft interface information
93 * @addr: mac address of the originator
94 *
95 * Returns the originator object corresponding to the passed mac address or NULL
96 * on failure.
97 * If the object does not exists it is created an initialised.
98 */
99 static struct batadv_orig_node *
100 batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
101 {
102 struct batadv_orig_node *orig_node;
103 int size, hash_added;
104
105 orig_node = batadv_orig_hash_find(bat_priv, addr);
106 if (orig_node)
107 return orig_node;
108
109 orig_node = batadv_orig_node_new(bat_priv, addr);
110 if (!orig_node)
111 return NULL;
112
113 spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
114
115 size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
116 orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
117 if (!orig_node->bat_iv.bcast_own)
118 goto free_orig_node;
119
120 size = bat_priv->num_ifaces * sizeof(uint8_t);
121 orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
122 if (!orig_node->bat_iv.bcast_own_sum)
123 goto free_bcast_own;
124
125 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
126 batadv_choose_orig, orig_node,
127 &orig_node->hash_entry);
128 if (hash_added != 0)
129 goto free_bcast_own;
130
131 return orig_node;
132
133 free_bcast_own:
134 kfree(orig_node->bat_iv.bcast_own);
135 free_orig_node:
136 batadv_orig_node_free_ref(orig_node);
137
138 return NULL;
139 }
140
141 static struct batadv_neigh_node *
142 batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
143 const uint8_t *neigh_addr,
144 struct batadv_orig_node *orig_node,
145 struct batadv_orig_node *orig_neigh)
146 {
147 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
148 struct batadv_neigh_node *neigh_node;
149
150 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
151 if (!neigh_node)
152 goto out;
153
154 spin_lock_init(&neigh_node->bat_iv.lq_update_lock);
155
156 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
157 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
158 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
159
160 spin_lock_bh(&orig_node->neigh_list_lock);
161 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
162 spin_unlock_bh(&orig_node->neigh_list_lock);
163
164 out:
165 return neigh_node;
166 }
167
168 static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
169 {
170 struct batadv_ogm_packet *batadv_ogm_packet;
171 unsigned char *ogm_buff;
172 uint32_t random_seqno;
173 int res = -ENOMEM;
174
175 /* randomize initial seqno to avoid collision */
176 get_random_bytes(&random_seqno, sizeof(random_seqno));
177 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
178
179 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
180 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
181 if (!ogm_buff)
182 goto out;
183
184 hard_iface->bat_iv.ogm_buff = ogm_buff;
185
186 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
187 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
188 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
189 batadv_ogm_packet->header.ttl = 2;
190 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
191 batadv_ogm_packet->reserved = 0;
192 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
193
194 res = 0;
195
196 out:
197 return res;
198 }
199
200 static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
201 {
202 kfree(hard_iface->bat_iv.ogm_buff);
203 hard_iface->bat_iv.ogm_buff = NULL;
204 }
205
206 static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
207 {
208 struct batadv_ogm_packet *batadv_ogm_packet;
209 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
210
211 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
212 memcpy(batadv_ogm_packet->orig,
213 hard_iface->net_dev->dev_addr, ETH_ALEN);
214 memcpy(batadv_ogm_packet->prev_sender,
215 hard_iface->net_dev->dev_addr, ETH_ALEN);
216 }
217
218 static void
219 batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
220 {
221 struct batadv_ogm_packet *batadv_ogm_packet;
222 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
223
224 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
225 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
226 batadv_ogm_packet->header.ttl = BATADV_TTL;
227 }
228
229 /* when do we schedule our own ogm to be sent */
230 static unsigned long
231 batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
232 {
233 unsigned int msecs;
234
235 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
236 msecs += prandom_u32() % (2 * BATADV_JITTER);
237
238 return jiffies + msecs_to_jiffies(msecs);
239 }
240
241 /* when do we schedule a ogm packet to be sent */
242 static unsigned long batadv_iv_ogm_fwd_send_time(void)
243 {
244 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
245 }
246
247 /* apply hop penalty for a normal link */
248 static uint8_t batadv_hop_penalty(uint8_t tq,
249 const struct batadv_priv *bat_priv)
250 {
251 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
252 int new_tq;
253
254 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
255 new_tq /= BATADV_TQ_MAX_VALUE;
256
257 return new_tq;
258 }
259
260 /* is there another aggregated packet here? */
261 static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
262 __be16 tvlv_len)
263 {
264 int next_buff_pos = 0;
265
266 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
267 next_buff_pos += ntohs(tvlv_len);
268
269 return (next_buff_pos <= packet_len) &&
270 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
271 }
272
273 /* send a batman ogm to a given interface */
274 static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
275 struct batadv_hard_iface *hard_iface)
276 {
277 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
278 char *fwd_str;
279 uint8_t packet_num;
280 int16_t buff_pos;
281 struct batadv_ogm_packet *batadv_ogm_packet;
282 struct sk_buff *skb;
283 uint8_t *packet_pos;
284
285 if (hard_iface->if_status != BATADV_IF_ACTIVE)
286 return;
287
288 packet_num = 0;
289 buff_pos = 0;
290 packet_pos = forw_packet->skb->data;
291 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
292
293 /* adjust all flags and log packets */
294 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
295 batadv_ogm_packet->tvlv_len)) {
296 /* we might have aggregated direct link packets with an
297 * ordinary base packet
298 */
299 if (forw_packet->direct_link_flags & BIT(packet_num) &&
300 forw_packet->if_incoming == hard_iface)
301 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
302 else
303 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
304
305 if (packet_num > 0 || !forw_packet->own)
306 fwd_str = "Forwarding";
307 else
308 fwd_str = "Sending own";
309
310 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
311 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
312 fwd_str, (packet_num > 0 ? "aggregated " : ""),
313 batadv_ogm_packet->orig,
314 ntohl(batadv_ogm_packet->seqno),
315 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
316 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
317 "on" : "off"),
318 hard_iface->net_dev->name,
319 hard_iface->net_dev->dev_addr);
320
321 buff_pos += BATADV_OGM_HLEN;
322 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
323 packet_num++;
324 packet_pos = forw_packet->skb->data + buff_pos;
325 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
326 }
327
328 /* create clone because function is called more than once */
329 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
330 if (skb) {
331 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
332 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
333 skb->len + ETH_HLEN);
334 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
335 }
336 }
337
338 /* send a batman ogm packet */
339 static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
340 {
341 struct batadv_hard_iface *hard_iface;
342 struct net_device *soft_iface;
343 struct batadv_priv *bat_priv;
344 struct batadv_hard_iface *primary_if = NULL;
345 struct batadv_ogm_packet *batadv_ogm_packet;
346 unsigned char directlink;
347 uint8_t *packet_pos;
348
349 packet_pos = forw_packet->skb->data;
350 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
351 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
352
353 if (!forw_packet->if_incoming) {
354 pr_err("Error - can't forward packet: incoming iface not specified\n");
355 goto out;
356 }
357
358 soft_iface = forw_packet->if_incoming->soft_iface;
359 bat_priv = netdev_priv(soft_iface);
360
361 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
362 goto out;
363
364 primary_if = batadv_primary_if_get_selected(bat_priv);
365 if (!primary_if)
366 goto out;
367
368 /* multihomed peer assumed
369 * non-primary OGMs are only broadcasted on their interface
370 */
371 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
372 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
373 /* FIXME: what about aggregated packets ? */
374 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
375 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
376 (forw_packet->own ? "Sending own" : "Forwarding"),
377 batadv_ogm_packet->orig,
378 ntohl(batadv_ogm_packet->seqno),
379 batadv_ogm_packet->header.ttl,
380 forw_packet->if_incoming->net_dev->name,
381 forw_packet->if_incoming->net_dev->dev_addr);
382
383 /* skb is only used once and than forw_packet is free'd */
384 batadv_send_skb_packet(forw_packet->skb,
385 forw_packet->if_incoming,
386 batadv_broadcast_addr);
387 forw_packet->skb = NULL;
388
389 goto out;
390 }
391
392 /* broadcast on every interface */
393 rcu_read_lock();
394 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
395 if (hard_iface->soft_iface != soft_iface)
396 continue;
397
398 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
399 }
400 rcu_read_unlock();
401
402 out:
403 if (primary_if)
404 batadv_hardif_free_ref(primary_if);
405 }
406
407 /* return true if new_packet can be aggregated with forw_packet */
408 static bool
409 batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
410 struct batadv_priv *bat_priv,
411 int packet_len, unsigned long send_time,
412 bool directlink,
413 const struct batadv_hard_iface *if_incoming,
414 const struct batadv_forw_packet *forw_packet)
415 {
416 struct batadv_ogm_packet *batadv_ogm_packet;
417 int aggregated_bytes = forw_packet->packet_len + packet_len;
418 struct batadv_hard_iface *primary_if = NULL;
419 bool res = false;
420 unsigned long aggregation_end_time;
421
422 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
423 aggregation_end_time = send_time;
424 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
425
426 /* we can aggregate the current packet to this aggregated packet
427 * if:
428 *
429 * - the send time is within our MAX_AGGREGATION_MS time
430 * - the resulting packet wont be bigger than
431 * MAX_AGGREGATION_BYTES
432 */
433 if (time_before(send_time, forw_packet->send_time) &&
434 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
435 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
436 /* check aggregation compatibility
437 * -> direct link packets are broadcasted on
438 * their interface only
439 * -> aggregate packet if the current packet is
440 * a "global" packet as well as the base
441 * packet
442 */
443 primary_if = batadv_primary_if_get_selected(bat_priv);
444 if (!primary_if)
445 goto out;
446
447 /* packets without direct link flag and high TTL
448 * are flooded through the net
449 */
450 if ((!directlink) &&
451 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
452 (batadv_ogm_packet->header.ttl != 1) &&
453
454 /* own packets originating non-primary
455 * interfaces leave only that interface
456 */
457 ((!forw_packet->own) ||
458 (forw_packet->if_incoming == primary_if))) {
459 res = true;
460 goto out;
461 }
462
463 /* if the incoming packet is sent via this one
464 * interface only - we still can aggregate
465 */
466 if ((directlink) &&
467 (new_bat_ogm_packet->header.ttl == 1) &&
468 (forw_packet->if_incoming == if_incoming) &&
469
470 /* packets from direct neighbors or
471 * own secondary interface packets
472 * (= secondary interface packets in general)
473 */
474 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
475 (forw_packet->own &&
476 forw_packet->if_incoming != primary_if))) {
477 res = true;
478 goto out;
479 }
480 }
481
482 out:
483 if (primary_if)
484 batadv_hardif_free_ref(primary_if);
485 return res;
486 }
487
488 /* create a new aggregated packet and add this packet to it */
489 static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
490 int packet_len, unsigned long send_time,
491 bool direct_link,
492 struct batadv_hard_iface *if_incoming,
493 int own_packet)
494 {
495 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
496 struct batadv_forw_packet *forw_packet_aggr;
497 unsigned char *skb_buff;
498 unsigned int skb_size;
499
500 if (!atomic_inc_not_zero(&if_incoming->refcount))
501 return;
502
503 /* own packet should always be scheduled */
504 if (!own_packet) {
505 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
506 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
507 "batman packet queue full\n");
508 goto out;
509 }
510 }
511
512 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
513 if (!forw_packet_aggr) {
514 if (!own_packet)
515 atomic_inc(&bat_priv->batman_queue_left);
516 goto out;
517 }
518
519 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
520 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
521 skb_size = BATADV_MAX_AGGREGATION_BYTES;
522 else
523 skb_size = packet_len;
524
525 skb_size += ETH_HLEN;
526
527 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
528 if (!forw_packet_aggr->skb) {
529 if (!own_packet)
530 atomic_inc(&bat_priv->batman_queue_left);
531 kfree(forw_packet_aggr);
532 goto out;
533 }
534 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
535 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
536
537 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
538 forw_packet_aggr->packet_len = packet_len;
539 memcpy(skb_buff, packet_buff, packet_len);
540
541 forw_packet_aggr->own = own_packet;
542 forw_packet_aggr->if_incoming = if_incoming;
543 forw_packet_aggr->num_packets = 0;
544 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
545 forw_packet_aggr->send_time = send_time;
546
547 /* save packet direct link flag status */
548 if (direct_link)
549 forw_packet_aggr->direct_link_flags |= 1;
550
551 /* add new packet to packet list */
552 spin_lock_bh(&bat_priv->forw_bat_list_lock);
553 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
554 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
555
556 /* start timer for this packet */
557 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
558 batadv_send_outstanding_bat_ogm_packet);
559 queue_delayed_work(batadv_event_workqueue,
560 &forw_packet_aggr->delayed_work,
561 send_time - jiffies);
562
563 return;
564 out:
565 batadv_hardif_free_ref(if_incoming);
566 }
567
568 /* aggregate a new packet into the existing ogm packet */
569 static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
570 const unsigned char *packet_buff,
571 int packet_len, bool direct_link)
572 {
573 unsigned char *skb_buff;
574 unsigned long new_direct_link_flag;
575
576 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
577 memcpy(skb_buff, packet_buff, packet_len);
578 forw_packet_aggr->packet_len += packet_len;
579 forw_packet_aggr->num_packets++;
580
581 /* save packet direct link flag status */
582 if (direct_link) {
583 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
584 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
585 }
586 }
587
588 static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
589 unsigned char *packet_buff,
590 int packet_len,
591 struct batadv_hard_iface *if_incoming,
592 int own_packet, unsigned long send_time)
593 {
594 /* _aggr -> pointer to the packet we want to aggregate with
595 * _pos -> pointer to the position in the queue
596 */
597 struct batadv_forw_packet *forw_packet_aggr = NULL;
598 struct batadv_forw_packet *forw_packet_pos = NULL;
599 struct batadv_ogm_packet *batadv_ogm_packet;
600 bool direct_link;
601 unsigned long max_aggregation_jiffies;
602
603 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
604 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
605 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
606
607 /* find position for the packet in the forward queue */
608 spin_lock_bh(&bat_priv->forw_bat_list_lock);
609 /* own packets are not to be aggregated */
610 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
611 hlist_for_each_entry(forw_packet_pos,
612 &bat_priv->forw_bat_list, list) {
613 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
614 bat_priv, packet_len,
615 send_time, direct_link,
616 if_incoming,
617 forw_packet_pos)) {
618 forw_packet_aggr = forw_packet_pos;
619 break;
620 }
621 }
622 }
623
624 /* nothing to aggregate with - either aggregation disabled or no
625 * suitable aggregation packet found
626 */
627 if (!forw_packet_aggr) {
628 /* the following section can run without the lock */
629 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
630
631 /* if we could not aggregate this packet with one of the others
632 * we hold it back for a while, so that it might be aggregated
633 * later on
634 */
635 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
636 send_time += max_aggregation_jiffies;
637
638 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
639 send_time, direct_link,
640 if_incoming, own_packet);
641 } else {
642 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
643 packet_len, direct_link);
644 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
645 }
646 }
647
648 static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
649 const struct ethhdr *ethhdr,
650 struct batadv_ogm_packet *batadv_ogm_packet,
651 bool is_single_hop_neigh,
652 bool is_from_best_next_hop,
653 struct batadv_hard_iface *if_incoming)
654 {
655 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
656 uint16_t tvlv_len;
657
658 if (batadv_ogm_packet->header.ttl <= 1) {
659 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
660 return;
661 }
662
663 if (!is_from_best_next_hop) {
664 /* Mark the forwarded packet when it is not coming from our
665 * best next hop. We still need to forward the packet for our
666 * neighbor link quality detection to work in case the packet
667 * originated from a single hop neighbor. Otherwise we can
668 * simply drop the ogm.
669 */
670 if (is_single_hop_neigh)
671 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
672 else
673 return;
674 }
675
676 tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
677
678 batadv_ogm_packet->header.ttl--;
679 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
680
681 /* apply hop penalty */
682 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
683 bat_priv);
684
685 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
686 "Forwarding packet: tq: %i, ttl: %i\n",
687 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
688
689 /* switch of primaries first hop flag when forwarding */
690 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
691 if (is_single_hop_neigh)
692 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
693 else
694 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
695
696 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
697 BATADV_OGM_HLEN + tvlv_len,
698 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
699 }
700
701 /**
702 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
703 * the given interface
704 * @hard_iface: the interface for which the windows have to be shifted
705 */
706 static void
707 batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
708 {
709 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
710 struct batadv_hashtable *hash = bat_priv->orig_hash;
711 struct hlist_head *head;
712 struct batadv_orig_node *orig_node;
713 unsigned long *word;
714 uint32_t i;
715 size_t word_index;
716 uint8_t *w;
717 int if_num;
718
719 for (i = 0; i < hash->size; i++) {
720 head = &hash->table[i];
721
722 rcu_read_lock();
723 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
724 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
725 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
726 word = &(orig_node->bat_iv.bcast_own[word_index]);
727
728 batadv_bit_get_packet(bat_priv, word, 1, 0);
729 if_num = hard_iface->if_num;
730 w = &orig_node->bat_iv.bcast_own_sum[if_num];
731 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
732 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
733 }
734 rcu_read_unlock();
735 }
736 }
737
738 static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
739 {
740 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
741 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
742 struct batadv_ogm_packet *batadv_ogm_packet;
743 struct batadv_hard_iface *primary_if;
744 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
745 uint32_t seqno;
746 uint16_t tvlv_len = 0;
747
748 primary_if = batadv_primary_if_get_selected(bat_priv);
749
750 if (hard_iface == primary_if) {
751 /* tt changes have to be committed before the tvlv data is
752 * appended as it may alter the tt tvlv container
753 */
754 batadv_tt_local_commit_changes(bat_priv);
755 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
756 ogm_buff_len,
757 BATADV_OGM_HLEN);
758 }
759
760 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
761 batadv_ogm_packet->tvlv_len = htons(tvlv_len);
762
763 /* change sequence number to network order */
764 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
765 batadv_ogm_packet->seqno = htonl(seqno);
766 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
767
768 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
769 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
770 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
771 batadv_iv_ogm_emit_send_time(bat_priv));
772
773 if (primary_if)
774 batadv_hardif_free_ref(primary_if);
775 }
776
777 static void
778 batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
779 struct batadv_orig_node *orig_node,
780 const struct ethhdr *ethhdr,
781 const struct batadv_ogm_packet *batadv_ogm_packet,
782 struct batadv_hard_iface *if_incoming,
783 const unsigned char *tt_buff,
784 enum batadv_dup_status dup_status)
785 {
786 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
787 struct batadv_neigh_node *router = NULL;
788 struct batadv_orig_node *orig_node_tmp;
789 int if_num;
790 uint8_t sum_orig, sum_neigh;
791 uint8_t *neigh_addr;
792 uint8_t tq_avg;
793
794 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
795 "update_originator(): Searching and updating originator entry of received packet\n");
796
797 rcu_read_lock();
798 hlist_for_each_entry_rcu(tmp_neigh_node,
799 &orig_node->neigh_list, list) {
800 neigh_addr = tmp_neigh_node->addr;
801 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
802 tmp_neigh_node->if_incoming == if_incoming &&
803 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
804 if (WARN(neigh_node, "too many matching neigh_nodes"))
805 batadv_neigh_node_free_ref(neigh_node);
806 neigh_node = tmp_neigh_node;
807 continue;
808 }
809
810 if (dup_status != BATADV_NO_DUP)
811 continue;
812
813 spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
814 batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv,
815 &tmp_neigh_node->bat_iv.tq_index, 0);
816 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv);
817 tmp_neigh_node->bat_iv.tq_avg = tq_avg;
818 spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock);
819 }
820
821 if (!neigh_node) {
822 struct batadv_orig_node *orig_tmp;
823
824 orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
825 if (!orig_tmp)
826 goto unlock;
827
828 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
829 ethhdr->h_source,
830 orig_node, orig_tmp);
831
832 batadv_orig_node_free_ref(orig_tmp);
833 if (!neigh_node)
834 goto unlock;
835 } else
836 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
837 "Updating existing last-hop neighbor of originator\n");
838
839 rcu_read_unlock();
840
841 neigh_node->last_seen = jiffies;
842
843 spin_lock_bh(&neigh_node->bat_iv.lq_update_lock);
844 batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv,
845 &neigh_node->bat_iv.tq_index,
846 batadv_ogm_packet->tq);
847 tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv);
848 neigh_node->bat_iv.tq_avg = tq_avg;
849 spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock);
850
851 if (dup_status == BATADV_NO_DUP) {
852 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
853 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
854 }
855
856 batadv_bonding_candidate_add(orig_node, neigh_node);
857
858 /* if this neighbor already is our next hop there is nothing
859 * to change
860 */
861 router = batadv_orig_node_get_router(orig_node);
862 if (router == neigh_node)
863 goto out;
864
865 /* if this neighbor does not offer a better TQ we won't consider it */
866 if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg))
867 goto out;
868
869 /* if the TQ is the same and the link not more symmetric we
870 * won't consider it either
871 */
872 if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) {
873 orig_node_tmp = router->orig_node;
874 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
875 if_num = router->if_incoming->if_num;
876 sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
877 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
878
879 orig_node_tmp = neigh_node->orig_node;
880 spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
881 if_num = neigh_node->if_incoming->if_num;
882 sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
883 spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
884
885 if (sum_orig >= sum_neigh)
886 goto out;
887 }
888
889 batadv_update_route(bat_priv, orig_node, neigh_node);
890 goto out;
891
892 unlock:
893 rcu_read_unlock();
894 out:
895 if (neigh_node)
896 batadv_neigh_node_free_ref(neigh_node);
897 if (router)
898 batadv_neigh_node_free_ref(router);
899 }
900
901 static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
902 struct batadv_orig_node *orig_neigh_node,
903 struct batadv_ogm_packet *batadv_ogm_packet,
904 struct batadv_hard_iface *if_incoming)
905 {
906 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
907 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
908 uint8_t total_count;
909 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
910 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
911 int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
912 unsigned int combined_tq;
913
914 /* find corresponding one hop neighbor */
915 rcu_read_lock();
916 hlist_for_each_entry_rcu(tmp_neigh_node,
917 &orig_neigh_node->neigh_list, list) {
918 if (!batadv_compare_eth(tmp_neigh_node->addr,
919 orig_neigh_node->orig))
920 continue;
921
922 if (tmp_neigh_node->if_incoming != if_incoming)
923 continue;
924
925 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
926 continue;
927
928 neigh_node = tmp_neigh_node;
929 break;
930 }
931 rcu_read_unlock();
932
933 if (!neigh_node)
934 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
935 orig_neigh_node->orig,
936 orig_neigh_node,
937 orig_neigh_node);
938
939 if (!neigh_node)
940 goto out;
941
942 /* if orig_node is direct neighbor update neigh_node last_seen */
943 if (orig_node == orig_neigh_node)
944 neigh_node->last_seen = jiffies;
945
946 orig_node->last_seen = jiffies;
947
948 /* find packet count of corresponding one hop neighbor */
949 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
950 if_num = if_incoming->if_num;
951 orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
952 neigh_rq_count = neigh_node->bat_iv.real_packet_count;
953 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
954
955 /* pay attention to not get a value bigger than 100 % */
956 if (orig_eq_count > neigh_rq_count)
957 total_count = neigh_rq_count;
958 else
959 total_count = orig_eq_count;
960
961 /* if we have too few packets (too less data) we set tq_own to zero
962 * if we receive too few packets it is not considered bidirectional
963 */
964 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
965 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
966 tq_own = 0;
967 else
968 /* neigh_node->real_packet_count is never zero as we
969 * only purge old information when getting new
970 * information
971 */
972 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
973
974 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
975 * affect the nearly-symmetric links only a little, but
976 * punishes asymmetric links more. This will give a value
977 * between 0 and TQ_MAX_VALUE
978 */
979 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
980 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
981 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
982 BATADV_TQ_LOCAL_WINDOW_SIZE *
983 BATADV_TQ_LOCAL_WINDOW_SIZE;
984 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
985 inv_asym_penalty /= neigh_rq_max_cube;
986 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
987
988 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
989 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
990 batadv_ogm_packet->tq = combined_tq;
991
992 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
993 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
994 orig_node->orig, orig_neigh_node->orig, total_count,
995 neigh_rq_count, tq_own,
996 tq_asym_penalty, batadv_ogm_packet->tq);
997
998 /* if link has the minimum required transmission quality
999 * consider it bidirectional
1000 */
1001 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
1002 ret = 1;
1003
1004 out:
1005 if (neigh_node)
1006 batadv_neigh_node_free_ref(neigh_node);
1007 return ret;
1008 }
1009
1010 /**
1011 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
1012 * adjust the sequence number and find out whether it is a duplicate
1013 * @ethhdr: ethernet header of the packet
1014 * @batadv_ogm_packet: OGM packet to be considered
1015 * @if_incoming: interface on which the OGM packet was received
1016 *
1017 * Returns duplicate status as enum batadv_dup_status
1018 */
1019 static enum batadv_dup_status
1020 batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
1021 const struct batadv_ogm_packet *batadv_ogm_packet,
1022 const struct batadv_hard_iface *if_incoming)
1023 {
1024 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1025 struct batadv_orig_node *orig_node;
1026 struct batadv_neigh_node *tmp_neigh_node;
1027 int is_dup;
1028 int32_t seq_diff;
1029 int need_update = 0;
1030 int set_mark;
1031 enum batadv_dup_status ret = BATADV_NO_DUP;
1032 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
1033 uint8_t *neigh_addr;
1034 uint8_t packet_count;
1035 unsigned long *bitmap;
1036
1037 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1038 if (!orig_node)
1039 return BATADV_NO_DUP;
1040
1041 spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1042 seq_diff = seqno - orig_node->last_real_seqno;
1043
1044 /* signalize caller that the packet is to be dropped. */
1045 if (!hlist_empty(&orig_node->neigh_list) &&
1046 batadv_window_protected(bat_priv, seq_diff,
1047 &orig_node->batman_seqno_reset)) {
1048 ret = BATADV_PROTECTED;
1049 goto out;
1050 }
1051
1052 rcu_read_lock();
1053 hlist_for_each_entry_rcu(tmp_neigh_node,
1054 &orig_node->neigh_list, list) {
1055 neigh_addr = tmp_neigh_node->addr;
1056 is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits,
1057 orig_node->last_real_seqno,
1058 seqno);
1059
1060 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
1061 tmp_neigh_node->if_incoming == if_incoming) {
1062 set_mark = 1;
1063 if (is_dup)
1064 ret = BATADV_NEIGH_DUP;
1065 } else {
1066 set_mark = 0;
1067 if (is_dup && (ret != BATADV_NEIGH_DUP))
1068 ret = BATADV_ORIG_DUP;
1069 }
1070
1071 /* if the window moved, set the update flag. */
1072 bitmap = tmp_neigh_node->bat_iv.real_bits;
1073 need_update |= batadv_bit_get_packet(bat_priv, bitmap,
1074 seq_diff, set_mark);
1075
1076 packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits,
1077 BATADV_TQ_LOCAL_WINDOW_SIZE);
1078 tmp_neigh_node->bat_iv.real_packet_count = packet_count;
1079 }
1080 rcu_read_unlock();
1081
1082 if (need_update) {
1083 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1084 "updating last_seqno: old %u, new %u\n",
1085 orig_node->last_real_seqno, seqno);
1086 orig_node->last_real_seqno = seqno;
1087 }
1088
1089 out:
1090 spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
1091 batadv_orig_node_free_ref(orig_node);
1092 return ret;
1093 }
1094
1095 static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
1096 struct batadv_ogm_packet *batadv_ogm_packet,
1097 const unsigned char *tt_buff,
1098 struct batadv_hard_iface *if_incoming)
1099 {
1100 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1101 struct batadv_hard_iface *hard_iface;
1102 struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp;
1103 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1104 struct batadv_neigh_node *orig_neigh_router = NULL;
1105 int has_directlink_flag;
1106 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
1107 int is_bidirect;
1108 bool is_single_hop_neigh = false;
1109 bool is_from_best_next_hop = false;
1110 int sameseq, similar_ttl;
1111 enum batadv_dup_status dup_status;
1112 uint32_t if_incoming_seqno;
1113 uint8_t *prev_sender;
1114
1115 /* Silently drop when the batman packet is actually not a
1116 * correct packet.
1117 *
1118 * This might happen if a packet is padded (e.g. Ethernet has a
1119 * minimum frame length of 64 byte) and the aggregation interprets
1120 * it as an additional length.
1121 *
1122 * TODO: A more sane solution would be to have a bit in the
1123 * batadv_ogm_packet to detect whether the packet is the last
1124 * packet in an aggregation. Here we expect that the padding
1125 * is always zero (or not 0x01)
1126 */
1127 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
1128 return;
1129
1130 /* could be changed by schedule_own_packet() */
1131 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
1132
1133 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
1134 has_directlink_flag = 1;
1135 else
1136 has_directlink_flag = 0;
1137
1138 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
1139 is_single_hop_neigh = true;
1140
1141 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1142 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
1143 ethhdr->h_source, if_incoming->net_dev->name,
1144 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1145 batadv_ogm_packet->prev_sender,
1146 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq,
1147 batadv_ogm_packet->header.ttl,
1148 batadv_ogm_packet->header.version, has_directlink_flag);
1149
1150 rcu_read_lock();
1151 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1152 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1153 continue;
1154
1155 if (hard_iface->soft_iface != if_incoming->soft_iface)
1156 continue;
1157
1158 if (batadv_compare_eth(ethhdr->h_source,
1159 hard_iface->net_dev->dev_addr))
1160 is_my_addr = 1;
1161
1162 if (batadv_compare_eth(batadv_ogm_packet->orig,
1163 hard_iface->net_dev->dev_addr))
1164 is_my_orig = 1;
1165
1166 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
1167 hard_iface->net_dev->dev_addr))
1168 is_my_oldorig = 1;
1169 }
1170 rcu_read_unlock();
1171
1172 if (is_my_addr) {
1173 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1174 "Drop packet: received my own broadcast (sender: %pM)\n",
1175 ethhdr->h_source);
1176 return;
1177 }
1178
1179 if (is_my_orig) {
1180 unsigned long *word;
1181 int offset;
1182 int32_t bit_pos;
1183 int16_t if_num;
1184 uint8_t *weight;
1185
1186 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1187 ethhdr->h_source);
1188 if (!orig_neigh_node)
1189 return;
1190
1191 /* neighbor has to indicate direct link and it has to
1192 * come via the corresponding interface
1193 * save packet seqno for bidirectional check
1194 */
1195 if (has_directlink_flag &&
1196 batadv_compare_eth(if_incoming->net_dev->dev_addr,
1197 batadv_ogm_packet->orig)) {
1198 if_num = if_incoming->if_num;
1199 offset = if_num * BATADV_NUM_WORDS;
1200
1201 spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1202 word = &(orig_neigh_node->bat_iv.bcast_own[offset]);
1203 bit_pos = if_incoming_seqno - 2;
1204 bit_pos -= ntohl(batadv_ogm_packet->seqno);
1205 batadv_set_bit(word, bit_pos);
1206 weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
1207 *weight = bitmap_weight(word,
1208 BATADV_TQ_LOCAL_WINDOW_SIZE);
1209 spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
1210 }
1211
1212 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1213 "Drop packet: originator packet from myself (via neighbor)\n");
1214 batadv_orig_node_free_ref(orig_neigh_node);
1215 return;
1216 }
1217
1218 if (is_my_oldorig) {
1219 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1220 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1221 ethhdr->h_source);
1222 return;
1223 }
1224
1225 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
1226 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1227 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1228 ethhdr->h_source);
1229 return;
1230 }
1231
1232 orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
1233 if (!orig_node)
1234 return;
1235
1236 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1237 if_incoming);
1238
1239 if (dup_status == BATADV_PROTECTED) {
1240 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1241 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1242 ethhdr->h_source);
1243 goto out;
1244 }
1245
1246 if (batadv_ogm_packet->tq == 0) {
1247 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1248 "Drop packet: originator packet with tq equal 0\n");
1249 goto out;
1250 }
1251
1252 router = batadv_orig_node_get_router(orig_node);
1253 if (router) {
1254 orig_node_tmp = router->orig_node;
1255 router_router = batadv_orig_node_get_router(orig_node_tmp);
1256 }
1257
1258 if ((router && router->bat_iv.tq_avg != 0) &&
1259 (batadv_compare_eth(router->addr, ethhdr->h_source)))
1260 is_from_best_next_hop = true;
1261
1262 prev_sender = batadv_ogm_packet->prev_sender;
1263 /* avoid temporary routing loops */
1264 if (router && router_router &&
1265 (batadv_compare_eth(router->addr, prev_sender)) &&
1266 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
1267 (batadv_compare_eth(router->addr, router_router->addr))) {
1268 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1269 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1270 ethhdr->h_source);
1271 goto out;
1272 }
1273
1274 batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node);
1275
1276 /* if sender is a direct neighbor the sender mac equals
1277 * originator mac
1278 */
1279 if (is_single_hop_neigh)
1280 orig_neigh_node = orig_node;
1281 else
1282 orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
1283 ethhdr->h_source);
1284
1285 if (!orig_neigh_node)
1286 goto out;
1287
1288 /* Update nc_nodes of the originator */
1289 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1290 batadv_ogm_packet, is_single_hop_neigh);
1291
1292 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
1293
1294 /* drop packet if sender is not a direct neighbor and if we
1295 * don't route towards it
1296 */
1297 if (!is_single_hop_neigh && (!orig_neigh_router)) {
1298 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1299 "Drop packet: OGM via unknown neighbor!\n");
1300 goto out_neigh;
1301 }
1302
1303 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
1304 batadv_ogm_packet, if_incoming);
1305
1306 batadv_bonding_save_primary(orig_node, orig_neigh_node,
1307 batadv_ogm_packet);
1308
1309 /* update ranking if it is not a duplicate or has the same
1310 * seqno and similar ttl as the non-duplicate
1311 */
1312 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
1313 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1314 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1315 (sameseq && similar_ttl)))
1316 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
1317 batadv_ogm_packet, if_incoming,
1318 tt_buff, dup_status);
1319
1320 /* is single hop (direct) neighbor */
1321 if (is_single_hop_neigh) {
1322 /* mark direct link on incoming interface */
1323 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1324 is_single_hop_neigh,
1325 is_from_best_next_hop, if_incoming);
1326
1327 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1328 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
1329 goto out_neigh;
1330 }
1331
1332 /* multihop originator */
1333 if (!is_bidirect) {
1334 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1335 "Drop packet: not received via bidirectional link\n");
1336 goto out_neigh;
1337 }
1338
1339 if (dup_status == BATADV_NEIGH_DUP) {
1340 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1341 "Drop packet: duplicate packet received\n");
1342 goto out_neigh;
1343 }
1344
1345 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1346 "Forwarding packet: rebroadcast originator packet\n");
1347 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
1348 is_single_hop_neigh, is_from_best_next_hop,
1349 if_incoming);
1350
1351 out_neigh:
1352 if ((orig_neigh_node) && (!is_single_hop_neigh))
1353 batadv_orig_node_free_ref(orig_neigh_node);
1354 out:
1355 if (router)
1356 batadv_neigh_node_free_ref(router);
1357 if (router_router)
1358 batadv_neigh_node_free_ref(router_router);
1359 if (orig_neigh_router)
1360 batadv_neigh_node_free_ref(orig_neigh_router);
1361
1362 batadv_orig_node_free_ref(orig_node);
1363 }
1364
1365 static int batadv_iv_ogm_receive(struct sk_buff *skb,
1366 struct batadv_hard_iface *if_incoming)
1367 {
1368 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1369 struct batadv_ogm_packet *batadv_ogm_packet;
1370 struct ethhdr *ethhdr;
1371 int buff_pos = 0, packet_len;
1372 unsigned char *tvlv_buff, *packet_buff;
1373 uint8_t *packet_pos;
1374 bool ret;
1375
1376 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
1377 if (!ret)
1378 return NET_RX_DROP;
1379
1380 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1381 * that does not have B.A.T.M.A.N. IV enabled ?
1382 */
1383 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
1384 return NET_RX_DROP;
1385
1386 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1387 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
1388 skb->len + ETH_HLEN);
1389
1390 packet_len = skb_headlen(skb);
1391 ethhdr = eth_hdr(skb);
1392 packet_buff = skb->data;
1393 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
1394
1395 /* unpack the aggregated packets and process them one by one */
1396 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1397 batadv_ogm_packet->tvlv_len)) {
1398 tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
1399
1400 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet,
1401 tvlv_buff, if_incoming);
1402
1403 buff_pos += BATADV_OGM_HLEN;
1404 buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
1405
1406 packet_pos = packet_buff + buff_pos;
1407 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
1408 }
1409
1410 kfree_skb(skb);
1411 return NET_RX_SUCCESS;
1412 }
1413
1414 static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
1415 .name = "BATMAN_IV",
1416 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1417 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1418 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1419 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1420 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1421 .bat_ogm_emit = batadv_iv_ogm_emit,
1422 };
1423
1424 int __init batadv_iv_init(void)
1425 {
1426 int ret;
1427
1428 /* batman originator packet */
1429 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1430 batadv_iv_ogm_receive);
1431 if (ret < 0)
1432 goto out;
1433
1434 ret = batadv_algo_register(&batadv_batman_iv);
1435 if (ret < 0)
1436 goto handler_unregister;
1437
1438 goto out;
1439
1440 handler_unregister:
1441 batadv_recv_handler_unregister(BATADV_IV_OGM);
1442 out:
1443 return ret;
1444 }
This page took 0.106928 seconds and 5 git commands to generate.