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