batman-adv: Add get_ethtool_stats() support for DAT
[deliverable/linux.git] / net / batman-adv / routing.c
1 /* Copyright (C) 2007-2012 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 "routing.h"
22 #include "send.h"
23 #include "soft-interface.h"
24 #include "hard-interface.h"
25 #include "icmp_socket.h"
26 #include "translation-table.h"
27 #include "originator.h"
28 #include "vis.h"
29 #include "unicast.h"
30 #include "bridge_loop_avoidance.h"
31 #include "distributed-arp-table.h"
32
33 static int batadv_route_unicast_packet(struct sk_buff *skb,
34 struct batadv_hard_iface *recv_if);
35
36 void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
37 {
38 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
39 struct batadv_hashtable *hash = bat_priv->orig_hash;
40 struct hlist_node *node;
41 struct hlist_head *head;
42 struct batadv_orig_node *orig_node;
43 unsigned long *word;
44 uint32_t i;
45 size_t word_index;
46 uint8_t *w;
47
48 for (i = 0; i < hash->size; i++) {
49 head = &hash->table[i];
50
51 rcu_read_lock();
52 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
53 spin_lock_bh(&orig_node->ogm_cnt_lock);
54 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
55 word = &(orig_node->bcast_own[word_index]);
56
57 batadv_bit_get_packet(bat_priv, word, 1, 0);
58 w = &orig_node->bcast_own_sum[hard_iface->if_num];
59 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
60 spin_unlock_bh(&orig_node->ogm_cnt_lock);
61 }
62 rcu_read_unlock();
63 }
64 }
65
66 static void _batadv_update_route(struct batadv_priv *bat_priv,
67 struct batadv_orig_node *orig_node,
68 struct batadv_neigh_node *neigh_node)
69 {
70 struct batadv_neigh_node *curr_router;
71
72 curr_router = batadv_orig_node_get_router(orig_node);
73
74 /* route deleted */
75 if ((curr_router) && (!neigh_node)) {
76 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
77 "Deleting route towards: %pM\n", orig_node->orig);
78 batadv_tt_global_del_orig(bat_priv, orig_node,
79 "Deleted route towards originator");
80
81 /* route added */
82 } else if ((!curr_router) && (neigh_node)) {
83
84 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
85 "Adding route towards: %pM (via %pM)\n",
86 orig_node->orig, neigh_node->addr);
87 /* route changed */
88 } else if (neigh_node && curr_router) {
89 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
90 "Changing route towards: %pM (now via %pM - was via %pM)\n",
91 orig_node->orig, neigh_node->addr,
92 curr_router->addr);
93 }
94
95 if (curr_router)
96 batadv_neigh_node_free_ref(curr_router);
97
98 /* increase refcount of new best neighbor */
99 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
100 neigh_node = NULL;
101
102 spin_lock_bh(&orig_node->neigh_list_lock);
103 rcu_assign_pointer(orig_node->router, neigh_node);
104 spin_unlock_bh(&orig_node->neigh_list_lock);
105
106 /* decrease refcount of previous best neighbor */
107 if (curr_router)
108 batadv_neigh_node_free_ref(curr_router);
109 }
110
111 void batadv_update_route(struct batadv_priv *bat_priv,
112 struct batadv_orig_node *orig_node,
113 struct batadv_neigh_node *neigh_node)
114 {
115 struct batadv_neigh_node *router = NULL;
116
117 if (!orig_node)
118 goto out;
119
120 router = batadv_orig_node_get_router(orig_node);
121
122 if (router != neigh_node)
123 _batadv_update_route(bat_priv, orig_node, neigh_node);
124
125 out:
126 if (router)
127 batadv_neigh_node_free_ref(router);
128 }
129
130 /* caller must hold the neigh_list_lock */
131 void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
132 struct batadv_neigh_node *neigh_node)
133 {
134 /* this neighbor is not part of our candidate list */
135 if (list_empty(&neigh_node->bonding_list))
136 goto out;
137
138 list_del_rcu(&neigh_node->bonding_list);
139 INIT_LIST_HEAD(&neigh_node->bonding_list);
140 batadv_neigh_node_free_ref(neigh_node);
141 atomic_dec(&orig_node->bond_candidates);
142
143 out:
144 return;
145 }
146
147 void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
148 struct batadv_neigh_node *neigh_node)
149 {
150 struct hlist_node *node;
151 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
152 uint8_t interference_candidate = 0;
153
154 spin_lock_bh(&orig_node->neigh_list_lock);
155
156 /* only consider if it has the same primary address ... */
157 if (!batadv_compare_eth(orig_node->orig,
158 neigh_node->orig_node->primary_addr))
159 goto candidate_del;
160
161 router = batadv_orig_node_get_router(orig_node);
162 if (!router)
163 goto candidate_del;
164
165 /* ... and is good enough to be considered */
166 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
167 goto candidate_del;
168
169 /* check if we have another candidate with the same mac address or
170 * interface. If we do, we won't select this candidate because of
171 * possible interference.
172 */
173 hlist_for_each_entry_rcu(tmp_neigh_node, node,
174 &orig_node->neigh_list, list) {
175
176 if (tmp_neigh_node == neigh_node)
177 continue;
178
179 /* we only care if the other candidate is even
180 * considered as candidate.
181 */
182 if (list_empty(&tmp_neigh_node->bonding_list))
183 continue;
184
185 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
186 (batadv_compare_eth(neigh_node->addr,
187 tmp_neigh_node->addr))) {
188 interference_candidate = 1;
189 break;
190 }
191 }
192
193 /* don't care further if it is an interference candidate */
194 if (interference_candidate)
195 goto candidate_del;
196
197 /* this neighbor already is part of our candidate list */
198 if (!list_empty(&neigh_node->bonding_list))
199 goto out;
200
201 if (!atomic_inc_not_zero(&neigh_node->refcount))
202 goto out;
203
204 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
205 atomic_inc(&orig_node->bond_candidates);
206 goto out;
207
208 candidate_del:
209 batadv_bonding_candidate_del(orig_node, neigh_node);
210
211 out:
212 spin_unlock_bh(&orig_node->neigh_list_lock);
213
214 if (router)
215 batadv_neigh_node_free_ref(router);
216 }
217
218 /* copy primary address for bonding */
219 void
220 batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
221 struct batadv_orig_node *orig_neigh_node,
222 const struct batadv_ogm_packet *batman_ogm_packet)
223 {
224 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
225 return;
226
227 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
228 }
229
230 /* checks whether the host restarted and is in the protection time.
231 * returns:
232 * 0 if the packet is to be accepted
233 * 1 if the packet is to be ignored.
234 */
235 int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
236 unsigned long *last_reset)
237 {
238 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
239 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
240 if (!batadv_has_timed_out(*last_reset,
241 BATADV_RESET_PROTECTION_MS))
242 return 1;
243
244 *last_reset = jiffies;
245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
246 "old packet received, start protection\n");
247 }
248
249 return 0;
250 }
251
252 bool batadv_check_management_packet(struct sk_buff *skb,
253 struct batadv_hard_iface *hard_iface,
254 int header_len)
255 {
256 struct ethhdr *ethhdr;
257
258 /* drop packet if it has not necessary minimum size */
259 if (unlikely(!pskb_may_pull(skb, header_len)))
260 return false;
261
262 ethhdr = (struct ethhdr *)skb_mac_header(skb);
263
264 /* packet with broadcast indication but unicast recipient */
265 if (!is_broadcast_ether_addr(ethhdr->h_dest))
266 return false;
267
268 /* packet with broadcast sender address */
269 if (is_broadcast_ether_addr(ethhdr->h_source))
270 return false;
271
272 /* create a copy of the skb, if needed, to modify it. */
273 if (skb_cow(skb, 0) < 0)
274 return false;
275
276 /* keep skb linear */
277 if (skb_linearize(skb) < 0)
278 return false;
279
280 return true;
281 }
282
283 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
284 struct sk_buff *skb, size_t icmp_len)
285 {
286 struct batadv_hard_iface *primary_if = NULL;
287 struct batadv_orig_node *orig_node = NULL;
288 struct batadv_neigh_node *router = NULL;
289 struct batadv_icmp_packet_rr *icmp_packet;
290 int ret = NET_RX_DROP;
291
292 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
293
294 /* add data to device queue */
295 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
296 batadv_socket_receive_packet(icmp_packet, icmp_len);
297 goto out;
298 }
299
300 primary_if = batadv_primary_if_get_selected(bat_priv);
301 if (!primary_if)
302 goto out;
303
304 /* answer echo request (ping) */
305 /* get routing information */
306 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
307 if (!orig_node)
308 goto out;
309
310 router = batadv_orig_node_get_router(orig_node);
311 if (!router)
312 goto out;
313
314 /* create a copy of the skb, if needed, to modify it. */
315 if (skb_cow(skb, ETH_HLEN) < 0)
316 goto out;
317
318 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
319
320 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
321 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
322 icmp_packet->msg_type = BATADV_ECHO_REPLY;
323 icmp_packet->header.ttl = BATADV_TTL;
324
325 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
326 ret = NET_RX_SUCCESS;
327
328 out:
329 if (primary_if)
330 batadv_hardif_free_ref(primary_if);
331 if (router)
332 batadv_neigh_node_free_ref(router);
333 if (orig_node)
334 batadv_orig_node_free_ref(orig_node);
335 return ret;
336 }
337
338 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
339 struct sk_buff *skb)
340 {
341 struct batadv_hard_iface *primary_if = NULL;
342 struct batadv_orig_node *orig_node = NULL;
343 struct batadv_neigh_node *router = NULL;
344 struct batadv_icmp_packet *icmp_packet;
345 int ret = NET_RX_DROP;
346
347 icmp_packet = (struct batadv_icmp_packet *)skb->data;
348
349 /* send TTL exceeded if packet is an echo request (traceroute) */
350 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
351 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
352 icmp_packet->orig, icmp_packet->dst);
353 goto out;
354 }
355
356 primary_if = batadv_primary_if_get_selected(bat_priv);
357 if (!primary_if)
358 goto out;
359
360 /* get routing information */
361 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
362 if (!orig_node)
363 goto out;
364
365 router = batadv_orig_node_get_router(orig_node);
366 if (!router)
367 goto out;
368
369 /* create a copy of the skb, if needed, to modify it. */
370 if (skb_cow(skb, ETH_HLEN) < 0)
371 goto out;
372
373 icmp_packet = (struct batadv_icmp_packet *)skb->data;
374
375 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
376 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
377 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
378 icmp_packet->header.ttl = BATADV_TTL;
379
380 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
381 ret = NET_RX_SUCCESS;
382
383 out:
384 if (primary_if)
385 batadv_hardif_free_ref(primary_if);
386 if (router)
387 batadv_neigh_node_free_ref(router);
388 if (orig_node)
389 batadv_orig_node_free_ref(orig_node);
390 return ret;
391 }
392
393
394 int batadv_recv_icmp_packet(struct sk_buff *skb,
395 struct batadv_hard_iface *recv_if)
396 {
397 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
398 struct batadv_icmp_packet_rr *icmp_packet;
399 struct ethhdr *ethhdr;
400 struct batadv_orig_node *orig_node = NULL;
401 struct batadv_neigh_node *router = NULL;
402 int hdr_size = sizeof(struct batadv_icmp_packet);
403 int ret = NET_RX_DROP;
404
405 /* we truncate all incoming icmp packets if they don't match our size */
406 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
407 hdr_size = sizeof(struct batadv_icmp_packet_rr);
408
409 /* drop packet if it has not necessary minimum size */
410 if (unlikely(!pskb_may_pull(skb, hdr_size)))
411 goto out;
412
413 ethhdr = (struct ethhdr *)skb_mac_header(skb);
414
415 /* packet with unicast indication but broadcast recipient */
416 if (is_broadcast_ether_addr(ethhdr->h_dest))
417 goto out;
418
419 /* packet with broadcast sender address */
420 if (is_broadcast_ether_addr(ethhdr->h_source))
421 goto out;
422
423 /* not for me */
424 if (!batadv_is_my_mac(ethhdr->h_dest))
425 goto out;
426
427 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
428
429 /* add record route information if not full */
430 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
431 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
432 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
433 ethhdr->h_dest, ETH_ALEN);
434 icmp_packet->rr_cur++;
435 }
436
437 /* packet for me */
438 if (batadv_is_my_mac(icmp_packet->dst))
439 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
440
441 /* TTL exceeded */
442 if (icmp_packet->header.ttl < 2)
443 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
444
445 /* get routing information */
446 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
447 if (!orig_node)
448 goto out;
449
450 router = batadv_orig_node_get_router(orig_node);
451 if (!router)
452 goto out;
453
454 /* create a copy of the skb, if needed, to modify it. */
455 if (skb_cow(skb, ETH_HLEN) < 0)
456 goto out;
457
458 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
459
460 /* decrement ttl */
461 icmp_packet->header.ttl--;
462
463 /* route it */
464 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
465 ret = NET_RX_SUCCESS;
466
467 out:
468 if (router)
469 batadv_neigh_node_free_ref(router);
470 if (orig_node)
471 batadv_orig_node_free_ref(orig_node);
472 return ret;
473 }
474
475 /* In the bonding case, send the packets in a round
476 * robin fashion over the remaining interfaces.
477 *
478 * This method rotates the bonding list and increases the
479 * returned router's refcount.
480 */
481 static struct batadv_neigh_node *
482 batadv_find_bond_router(struct batadv_orig_node *primary_orig,
483 const struct batadv_hard_iface *recv_if)
484 {
485 struct batadv_neigh_node *tmp_neigh_node;
486 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
487
488 rcu_read_lock();
489 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
490 bonding_list) {
491 if (!first_candidate)
492 first_candidate = tmp_neigh_node;
493
494 /* recv_if == NULL on the first node. */
495 if (tmp_neigh_node->if_incoming == recv_if)
496 continue;
497
498 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
499 continue;
500
501 router = tmp_neigh_node;
502 break;
503 }
504
505 /* use the first candidate if nothing was found. */
506 if (!router && first_candidate &&
507 atomic_inc_not_zero(&first_candidate->refcount))
508 router = first_candidate;
509
510 if (!router)
511 goto out;
512
513 /* selected should point to the next element
514 * after the current router
515 */
516 spin_lock_bh(&primary_orig->neigh_list_lock);
517 /* this is a list_move(), which unfortunately
518 * does not exist as rcu version
519 */
520 list_del_rcu(&primary_orig->bond_list);
521 list_add_rcu(&primary_orig->bond_list,
522 &router->bonding_list);
523 spin_unlock_bh(&primary_orig->neigh_list_lock);
524
525 out:
526 rcu_read_unlock();
527 return router;
528 }
529
530 /* Interface Alternating: Use the best of the
531 * remaining candidates which are not using
532 * this interface.
533 *
534 * Increases the returned router's refcount
535 */
536 static struct batadv_neigh_node *
537 batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
538 const struct batadv_hard_iface *recv_if)
539 {
540 struct batadv_neigh_node *tmp_neigh_node;
541 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
542
543 rcu_read_lock();
544 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
545 bonding_list) {
546 if (!first_candidate)
547 first_candidate = tmp_neigh_node;
548
549 /* recv_if == NULL on the first node. */
550 if (tmp_neigh_node->if_incoming == recv_if)
551 continue;
552
553 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
554 continue;
555
556 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
557 continue;
558
559 /* decrement refcount of previously selected router */
560 if (router)
561 batadv_neigh_node_free_ref(router);
562
563 /* we found a better router (or at least one valid router) */
564 router = tmp_neigh_node;
565 }
566
567 /* use the first candidate if nothing was found. */
568 if (!router && first_candidate &&
569 atomic_inc_not_zero(&first_candidate->refcount))
570 router = first_candidate;
571
572 rcu_read_unlock();
573 return router;
574 }
575
576 static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
577 {
578 struct ethhdr *ethhdr;
579
580 /* drop packet if it has not necessary minimum size */
581 if (unlikely(!pskb_may_pull(skb, hdr_size)))
582 return -1;
583
584 ethhdr = (struct ethhdr *)skb_mac_header(skb);
585
586 /* packet with unicast indication but broadcast recipient */
587 if (is_broadcast_ether_addr(ethhdr->h_dest))
588 return -1;
589
590 /* packet with broadcast sender address */
591 if (is_broadcast_ether_addr(ethhdr->h_source))
592 return -1;
593
594 /* not for me */
595 if (!batadv_is_my_mac(ethhdr->h_dest))
596 return -1;
597
598 return 0;
599 }
600
601 int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
602 {
603 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
604 struct batadv_tt_query_packet *tt_query;
605 uint16_t tt_size;
606 int hdr_size = sizeof(*tt_query);
607 char tt_flag;
608 size_t packet_size;
609
610 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
611 return NET_RX_DROP;
612
613 /* I could need to modify it */
614 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
615 goto out;
616
617 tt_query = (struct batadv_tt_query_packet *)skb->data;
618
619 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
620 case BATADV_TT_REQUEST:
621 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
622
623 /* If we cannot provide an answer the tt_request is
624 * forwarded
625 */
626 if (!batadv_send_tt_response(bat_priv, tt_query)) {
627 if (tt_query->flags & BATADV_TT_FULL_TABLE)
628 tt_flag = 'F';
629 else
630 tt_flag = '.';
631
632 batadv_dbg(BATADV_DBG_TT, bat_priv,
633 "Routing TT_REQUEST to %pM [%c]\n",
634 tt_query->dst,
635 tt_flag);
636 return batadv_route_unicast_packet(skb, recv_if);
637 }
638 break;
639 case BATADV_TT_RESPONSE:
640 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
641
642 if (batadv_is_my_mac(tt_query->dst)) {
643 /* packet needs to be linearized to access the TT
644 * changes
645 */
646 if (skb_linearize(skb) < 0)
647 goto out;
648 /* skb_linearize() possibly changed skb->data */
649 tt_query = (struct batadv_tt_query_packet *)skb->data;
650
651 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
652
653 /* Ensure we have all the claimed data */
654 packet_size = sizeof(struct batadv_tt_query_packet);
655 packet_size += tt_size;
656 if (unlikely(skb_headlen(skb) < packet_size))
657 goto out;
658
659 batadv_handle_tt_response(bat_priv, tt_query);
660 } else {
661 if (tt_query->flags & BATADV_TT_FULL_TABLE)
662 tt_flag = 'F';
663 else
664 tt_flag = '.';
665 batadv_dbg(BATADV_DBG_TT, bat_priv,
666 "Routing TT_RESPONSE to %pM [%c]\n",
667 tt_query->dst,
668 tt_flag);
669 return batadv_route_unicast_packet(skb, recv_if);
670 }
671 break;
672 }
673
674 out:
675 /* returning NET_RX_DROP will make the caller function kfree the skb */
676 return NET_RX_DROP;
677 }
678
679 int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
680 {
681 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
682 struct batadv_roam_adv_packet *roam_adv_packet;
683 struct batadv_orig_node *orig_node;
684
685 if (batadv_check_unicast_packet(skb, sizeof(*roam_adv_packet)) < 0)
686 goto out;
687
688 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
689
690 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
691
692 if (!batadv_is_my_mac(roam_adv_packet->dst))
693 return batadv_route_unicast_packet(skb, recv_if);
694
695 /* check if it is a backbone gateway. we don't accept
696 * roaming advertisement from it, as it has the same
697 * entries as we have.
698 */
699 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
700 goto out;
701
702 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
703 if (!orig_node)
704 goto out;
705
706 batadv_dbg(BATADV_DBG_TT, bat_priv,
707 "Received ROAMING_ADV from %pM (client %pM)\n",
708 roam_adv_packet->src, roam_adv_packet->client);
709
710 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
711 BATADV_TT_CLIENT_ROAM,
712 atomic_read(&orig_node->last_ttvn) + 1);
713
714 /* Roaming phase starts: I have new information but the ttvn has not
715 * been incremented yet. This flag will make me check all the incoming
716 * packets for the correct destination.
717 */
718 bat_priv->tt.poss_change = true;
719
720 batadv_orig_node_free_ref(orig_node);
721 out:
722 /* returning NET_RX_DROP will make the caller function kfree the skb */
723 return NET_RX_DROP;
724 }
725
726 /* find a suitable router for this originator, and use
727 * bonding if possible. increases the found neighbors
728 * refcount.
729 */
730 struct batadv_neigh_node *
731 batadv_find_router(struct batadv_priv *bat_priv,
732 struct batadv_orig_node *orig_node,
733 const struct batadv_hard_iface *recv_if)
734 {
735 struct batadv_orig_node *primary_orig_node;
736 struct batadv_orig_node *router_orig;
737 struct batadv_neigh_node *router;
738 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
739 int bonding_enabled;
740 uint8_t *primary_addr;
741
742 if (!orig_node)
743 return NULL;
744
745 router = batadv_orig_node_get_router(orig_node);
746 if (!router)
747 goto err;
748
749 /* without bonding, the first node should
750 * always choose the default router.
751 */
752 bonding_enabled = atomic_read(&bat_priv->bonding);
753
754 rcu_read_lock();
755 /* select default router to output */
756 router_orig = router->orig_node;
757 if (!router_orig)
758 goto err_unlock;
759
760 if ((!recv_if) && (!bonding_enabled))
761 goto return_router;
762
763 primary_addr = router_orig->primary_addr;
764
765 /* if we have something in the primary_addr, we can search
766 * for a potential bonding candidate.
767 */
768 if (batadv_compare_eth(primary_addr, zero_mac))
769 goto return_router;
770
771 /* find the orig_node which has the primary interface. might
772 * even be the same as our router_orig in many cases
773 */
774 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
775 primary_orig_node = router_orig;
776 } else {
777 primary_orig_node = batadv_orig_hash_find(bat_priv,
778 primary_addr);
779 if (!primary_orig_node)
780 goto return_router;
781
782 batadv_orig_node_free_ref(primary_orig_node);
783 }
784
785 /* with less than 2 candidates, we can't do any
786 * bonding and prefer the original router.
787 */
788 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
789 goto return_router;
790
791 /* all nodes between should choose a candidate which
792 * is is not on the interface where the packet came
793 * in.
794 */
795 batadv_neigh_node_free_ref(router);
796
797 if (bonding_enabled)
798 router = batadv_find_bond_router(primary_orig_node, recv_if);
799 else
800 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
801
802 return_router:
803 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
804 goto err_unlock;
805
806 rcu_read_unlock();
807 return router;
808 err_unlock:
809 rcu_read_unlock();
810 err:
811 if (router)
812 batadv_neigh_node_free_ref(router);
813 return NULL;
814 }
815
816 static int batadv_route_unicast_packet(struct sk_buff *skb,
817 struct batadv_hard_iface *recv_if)
818 {
819 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
820 struct batadv_orig_node *orig_node = NULL;
821 struct batadv_neigh_node *neigh_node = NULL;
822 struct batadv_unicast_packet *unicast_packet;
823 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
824 int ret = NET_RX_DROP;
825 struct sk_buff *new_skb;
826
827 unicast_packet = (struct batadv_unicast_packet *)skb->data;
828
829 /* TTL exceeded */
830 if (unicast_packet->header.ttl < 2) {
831 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
832 ethhdr->h_source, unicast_packet->dest);
833 goto out;
834 }
835
836 /* get routing information */
837 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
838
839 if (!orig_node)
840 goto out;
841
842 /* find_router() increases neigh_nodes refcount if found. */
843 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
844
845 if (!neigh_node)
846 goto out;
847
848 /* create a copy of the skb, if needed, to modify it. */
849 if (skb_cow(skb, ETH_HLEN) < 0)
850 goto out;
851
852 unicast_packet = (struct batadv_unicast_packet *)skb->data;
853
854 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
855 atomic_read(&bat_priv->fragmentation) &&
856 skb->len > neigh_node->if_incoming->net_dev->mtu) {
857 ret = batadv_frag_send_skb(skb, bat_priv,
858 neigh_node->if_incoming,
859 neigh_node->addr);
860 goto out;
861 }
862
863 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
864 batadv_frag_can_reassemble(skb,
865 neigh_node->if_incoming->net_dev->mtu)) {
866
867 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
868
869 if (ret == NET_RX_DROP)
870 goto out;
871
872 /* packet was buffered for late merge */
873 if (!new_skb) {
874 ret = NET_RX_SUCCESS;
875 goto out;
876 }
877
878 skb = new_skb;
879 unicast_packet = (struct batadv_unicast_packet *)skb->data;
880 }
881
882 /* decrement ttl */
883 unicast_packet->header.ttl--;
884
885 /* Update stats counter */
886 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
887 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
888 skb->len + ETH_HLEN);
889
890 /* route it */
891 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
892 ret = NET_RX_SUCCESS;
893
894 out:
895 if (neigh_node)
896 batadv_neigh_node_free_ref(neigh_node);
897 if (orig_node)
898 batadv_orig_node_free_ref(orig_node);
899 return ret;
900 }
901
902 static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
903 struct sk_buff *skb) {
904 uint8_t curr_ttvn;
905 struct batadv_orig_node *orig_node;
906 struct ethhdr *ethhdr;
907 struct batadv_hard_iface *primary_if;
908 struct batadv_unicast_packet *unicast_packet;
909 bool tt_poss_change;
910 int is_old_ttvn;
911
912 /* check if there is enough data before accessing it */
913 if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0)
914 return 0;
915
916 /* create a copy of the skb (in case of for re-routing) to modify it. */
917 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
918 return 0;
919
920 unicast_packet = (struct batadv_unicast_packet *)skb->data;
921
922 if (batadv_is_my_mac(unicast_packet->dest)) {
923 tt_poss_change = bat_priv->tt.poss_change;
924 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
925 } else {
926 orig_node = batadv_orig_hash_find(bat_priv,
927 unicast_packet->dest);
928
929 if (!orig_node)
930 return 0;
931
932 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
933 tt_poss_change = orig_node->tt_poss_change;
934 batadv_orig_node_free_ref(orig_node);
935 }
936
937 /* Check whether I have to reroute the packet */
938 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
939 if (is_old_ttvn || tt_poss_change) {
940 /* check if there is enough data before accessing it */
941 if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
942 ETH_HLEN) < 0)
943 return 0;
944
945 ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
946
947 /* we don't have an updated route for this client, so we should
948 * not try to reroute the packet!!
949 */
950 if (batadv_tt_global_client_is_roaming(bat_priv,
951 ethhdr->h_dest))
952 return 1;
953
954 orig_node = batadv_transtable_search(bat_priv, NULL,
955 ethhdr->h_dest);
956
957 if (!orig_node) {
958 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
959 return 0;
960 primary_if = batadv_primary_if_get_selected(bat_priv);
961 if (!primary_if)
962 return 0;
963 memcpy(unicast_packet->dest,
964 primary_if->net_dev->dev_addr, ETH_ALEN);
965 batadv_hardif_free_ref(primary_if);
966 } else {
967 memcpy(unicast_packet->dest, orig_node->orig,
968 ETH_ALEN);
969 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
970 batadv_orig_node_free_ref(orig_node);
971 }
972
973 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
974 "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
975 unicast_packet->ttvn, curr_ttvn,
976 ethhdr->h_dest, unicast_packet->dest);
977
978 unicast_packet->ttvn = curr_ttvn;
979 }
980 return 1;
981 }
982
983 int batadv_recv_unicast_packet(struct sk_buff *skb,
984 struct batadv_hard_iface *recv_if)
985 {
986 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
987 struct batadv_unicast_packet *unicast_packet;
988 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
989 int hdr_size = sizeof(*unicast_packet);
990 bool is4addr;
991
992 unicast_packet = (struct batadv_unicast_packet *)skb->data;
993 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
994
995 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
996 /* the caller function should have already pulled 2 bytes */
997 if (is4addr)
998 hdr_size = sizeof(*unicast_4addr_packet);
999
1000 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1001 return NET_RX_DROP;
1002
1003 if (!batadv_check_unicast_ttvn(bat_priv, skb))
1004 return NET_RX_DROP;
1005
1006 /* packet for me */
1007 if (batadv_is_my_mac(unicast_packet->dest)) {
1008 if (is4addr)
1009 batadv_dat_inc_counter(bat_priv,
1010 unicast_4addr_packet->subtype);
1011
1012 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1013 hdr_size))
1014 goto rx_success;
1015 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1016 hdr_size))
1017 goto rx_success;
1018
1019 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1020 NULL);
1021
1022 rx_success:
1023 return NET_RX_SUCCESS;
1024 }
1025
1026 return batadv_route_unicast_packet(skb, recv_if);
1027 }
1028
1029 int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
1030 struct batadv_hard_iface *recv_if)
1031 {
1032 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1033 struct batadv_unicast_frag_packet *unicast_packet;
1034 int hdr_size = sizeof(*unicast_packet);
1035 struct sk_buff *new_skb = NULL;
1036 int ret;
1037
1038 if (batadv_check_unicast_packet(skb, hdr_size) < 0)
1039 return NET_RX_DROP;
1040
1041 if (!batadv_check_unicast_ttvn(bat_priv, skb))
1042 return NET_RX_DROP;
1043
1044 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
1045
1046 /* packet for me */
1047 if (batadv_is_my_mac(unicast_packet->dest)) {
1048
1049 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
1050
1051 if (ret == NET_RX_DROP)
1052 return NET_RX_DROP;
1053
1054 /* packet was buffered for late merge */
1055 if (!new_skb)
1056 return NET_RX_SUCCESS;
1057
1058 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
1059 hdr_size))
1060 goto rx_success;
1061 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
1062 hdr_size))
1063 goto rx_success;
1064
1065 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
1066 sizeof(struct batadv_unicast_packet), NULL);
1067
1068 rx_success:
1069 return NET_RX_SUCCESS;
1070 }
1071
1072 return batadv_route_unicast_packet(skb, recv_if);
1073 }
1074
1075
1076 int batadv_recv_bcast_packet(struct sk_buff *skb,
1077 struct batadv_hard_iface *recv_if)
1078 {
1079 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1080 struct batadv_orig_node *orig_node = NULL;
1081 struct batadv_bcast_packet *bcast_packet;
1082 struct ethhdr *ethhdr;
1083 int hdr_size = sizeof(*bcast_packet);
1084 int ret = NET_RX_DROP;
1085 int32_t seq_diff;
1086
1087 /* drop packet if it has not necessary minimum size */
1088 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1089 goto out;
1090
1091 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1092
1093 /* packet with broadcast indication but unicast recipient */
1094 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1095 goto out;
1096
1097 /* packet with broadcast sender address */
1098 if (is_broadcast_ether_addr(ethhdr->h_source))
1099 goto out;
1100
1101 /* ignore broadcasts sent by myself */
1102 if (batadv_is_my_mac(ethhdr->h_source))
1103 goto out;
1104
1105 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1106
1107 /* ignore broadcasts originated by myself */
1108 if (batadv_is_my_mac(bcast_packet->orig))
1109 goto out;
1110
1111 if (bcast_packet->header.ttl < 2)
1112 goto out;
1113
1114 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1115
1116 if (!orig_node)
1117 goto out;
1118
1119 spin_lock_bh(&orig_node->bcast_seqno_lock);
1120
1121 /* check whether the packet is a duplicate */
1122 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1123 ntohl(bcast_packet->seqno)))
1124 goto spin_unlock;
1125
1126 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1127
1128 /* check whether the packet is old and the host just restarted. */
1129 if (batadv_window_protected(bat_priv, seq_diff,
1130 &orig_node->bcast_seqno_reset))
1131 goto spin_unlock;
1132
1133 /* mark broadcast in flood history, update window position
1134 * if required.
1135 */
1136 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1137 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1138
1139 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1140
1141 /* keep skb linear for crc calculation */
1142 if (skb_linearize(skb) < 0)
1143 goto out;
1144
1145 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1146
1147 /* check whether this has been sent by another originator before */
1148 if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, skb->len))
1149 goto out;
1150
1151 /* rebroadcast packet */
1152 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
1153
1154 /* don't hand the broadcast up if it is from an originator
1155 * from the same backbone.
1156 */
1157 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1158 goto out;
1159
1160 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1161 goto rx_success;
1162 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1163 goto rx_success;
1164
1165 /* broadcast for me */
1166 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1167 orig_node);
1168
1169 rx_success:
1170 ret = NET_RX_SUCCESS;
1171 goto out;
1172
1173 spin_unlock:
1174 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1175 out:
1176 if (orig_node)
1177 batadv_orig_node_free_ref(orig_node);
1178 return ret;
1179 }
1180
1181 int batadv_recv_vis_packet(struct sk_buff *skb,
1182 struct batadv_hard_iface *recv_if)
1183 {
1184 struct batadv_vis_packet *vis_packet;
1185 struct ethhdr *ethhdr;
1186 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1187 int hdr_size = sizeof(*vis_packet);
1188
1189 /* keep skb linear */
1190 if (skb_linearize(skb) < 0)
1191 return NET_RX_DROP;
1192
1193 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1194 return NET_RX_DROP;
1195
1196 vis_packet = (struct batadv_vis_packet *)skb->data;
1197 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1198
1199 /* not for me */
1200 if (!batadv_is_my_mac(ethhdr->h_dest))
1201 return NET_RX_DROP;
1202
1203 /* ignore own packets */
1204 if (batadv_is_my_mac(vis_packet->vis_orig))
1205 return NET_RX_DROP;
1206
1207 if (batadv_is_my_mac(vis_packet->sender_orig))
1208 return NET_RX_DROP;
1209
1210 switch (vis_packet->vis_type) {
1211 case BATADV_VIS_TYPE_SERVER_SYNC:
1212 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1213 skb_headlen(skb));
1214 break;
1215
1216 case BATADV_VIS_TYPE_CLIENT_UPDATE:
1217 batadv_receive_client_update_packet(bat_priv, vis_packet,
1218 skb_headlen(skb));
1219 break;
1220
1221 default: /* ignore unknown packet */
1222 break;
1223 }
1224
1225 /* We take a copy of the data in the packet, so we should
1226 * always free the skbuf.
1227 */
1228 return NET_RX_DROP;
1229 }
This page took 0.079605 seconds and 5 git commands to generate.