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