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