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