xref: /linux/net/batman-adv/routing.c (revision 87320be9f0d24fce67631b7eef919f0b79c3e45c)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  */
6 
7 #include "routing.h"
8 #include "main.h"
9 
10 #include <linux/atomic.h>
11 #include <linux/build_bug.h>
12 #include <linux/byteorder/generic.h>
13 #include <linux/compiler.h>
14 #include <linux/errno.h>
15 #include <linux/etherdevice.h>
16 #include <linux/if_ether.h>
17 #include <linux/jiffies.h>
18 #include <linux/kref.h>
19 #include <linux/netdevice.h>
20 #include <linux/printk.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/skbuff.h>
24 #include <linux/spinlock.h>
25 #include <linux/stddef.h>
26 #include <uapi/linux/batadv_packet.h>
27 
28 #include "bitarray.h"
29 #include "bridge_loop_avoidance.h"
30 #include "distributed-arp-table.h"
31 #include "fragmentation.h"
32 #include "hard-interface.h"
33 #include "log.h"
34 #include "mesh-interface.h"
35 #include "originator.h"
36 #include "send.h"
37 #include "tp_meter.h"
38 #include "translation-table.h"
39 #include "tvlv.h"
40 
41 static int batadv_route_unicast_packet(struct sk_buff *skb,
42 				       struct batadv_hard_iface *recv_if);
43 
44 /**
45  * _batadv_update_route() - set the router for this originator
46  * @bat_priv: the bat priv with all the mesh interface information
47  * @orig_node: orig node which is to be configured
48  * @recv_if: the receive interface for which this route is set
49  * @neigh_node: neighbor which should be the next router
50  *
51  * This function does not perform any error checks
52  */
_batadv_update_route(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_hard_iface * recv_if,struct batadv_neigh_node * neigh_node)53 static void _batadv_update_route(struct batadv_priv *bat_priv,
54 				 struct batadv_orig_node *orig_node,
55 				 struct batadv_hard_iface *recv_if,
56 				 struct batadv_neigh_node *neigh_node)
57 {
58 	struct batadv_orig_ifinfo *orig_ifinfo;
59 	struct batadv_neigh_node *curr_router;
60 
61 	orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
62 	if (!orig_ifinfo)
63 		return;
64 
65 	spin_lock_bh(&orig_node->neigh_list_lock);
66 	/* curr_router used earlier may not be the current orig_ifinfo->router
67 	 * anymore because it was dereferenced outside of the neigh_list_lock
68 	 * protected region. After the new best neighbor has replace the current
69 	 * best neighbor the reference counter needs to decrease. Consequently,
70 	 * the code needs to ensure the curr_router variable contains a pointer
71 	 * to the replaced best neighbor.
72 	 */
73 
74 	/* increase refcount of new best neighbor */
75 	if (neigh_node)
76 		kref_get(&neigh_node->refcount);
77 
78 	curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
79 					  true);
80 	spin_unlock_bh(&orig_node->neigh_list_lock);
81 	batadv_orig_ifinfo_put(orig_ifinfo);
82 
83 	/* route deleted */
84 	if (curr_router && !neigh_node) {
85 		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
86 			   "Deleting route towards: %pM\n", orig_node->orig);
87 		batadv_tt_global_del_orig(bat_priv, orig_node, -1,
88 					  "Deleted route towards originator");
89 
90 	/* route added */
91 	} else if (!curr_router && neigh_node) {
92 		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
93 			   "Adding route towards: %pM (via %pM)\n",
94 			   orig_node->orig, neigh_node->addr);
95 	/* route changed */
96 	} else if (neigh_node && curr_router) {
97 		batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
98 			   "Changing route towards: %pM (now via %pM - was via %pM)\n",
99 			   orig_node->orig, neigh_node->addr,
100 			   curr_router->addr);
101 	}
102 
103 	/* decrease refcount of previous best neighbor */
104 	batadv_neigh_node_put(curr_router);
105 }
106 
107 /**
108  * batadv_update_route() - set the router for this originator
109  * @bat_priv: the bat priv with all the mesh interface information
110  * @orig_node: orig node which is to be configured
111  * @recv_if: the receive interface for which this route is set
112  * @neigh_node: neighbor which should be the next router
113  */
batadv_update_route(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_hard_iface * recv_if,struct batadv_neigh_node * neigh_node)114 void batadv_update_route(struct batadv_priv *bat_priv,
115 			 struct batadv_orig_node *orig_node,
116 			 struct batadv_hard_iface *recv_if,
117 			 struct batadv_neigh_node *neigh_node)
118 {
119 	struct batadv_neigh_node *router = NULL;
120 
121 	if (!orig_node)
122 		goto out;
123 
124 	router = batadv_orig_router_get(orig_node, recv_if);
125 
126 	if (router != neigh_node)
127 		_batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
128 
129 out:
130 	batadv_neigh_node_put(router);
131 }
132 
133 /**
134  * batadv_window_protected() - checks whether the host restarted and is in the
135  *  protection time.
136  * @bat_priv: the bat priv with all the mesh interface information
137  * @seq_num_diff: difference between the current/received sequence number and
138  *  the last sequence number
139  * @seq_old_max_diff: maximum age of sequence number not considered as restart
140  * @last_reset: jiffies timestamp of the last reset, will be updated when reset
141  *  is detected
142  * @protection_started: is set to true if the protection window was started,
143  *   doesn't change otherwise.
144  *
145  * Return:
146  *  false if the packet is to be accepted.
147  *  true if the packet is to be ignored.
148  */
batadv_window_protected(struct batadv_priv * bat_priv,s32 seq_num_diff,s32 seq_old_max_diff,unsigned long * last_reset,bool * protection_started)149 bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
150 			     s32 seq_old_max_diff, unsigned long *last_reset,
151 			     bool *protection_started)
152 {
153 	if (seq_num_diff <= -seq_old_max_diff ||
154 	    seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
155 		if (!batadv_has_timed_out(*last_reset,
156 					  BATADV_RESET_PROTECTION_MS))
157 			return true;
158 
159 		*last_reset = jiffies;
160 		if (protection_started)
161 			*protection_started = true;
162 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
163 			   "old packet received, start protection\n");
164 	}
165 
166 	return false;
167 }
168 
169 /**
170  * batadv_check_management_packet() - Check preconditions for management packets
171  * @skb: incoming packet buffer
172  * @hard_iface: incoming hard interface
173  * @header_len: minimal header length of packet type
174  *
175  * Return: true when management preconditions are met, false otherwise
176  */
batadv_check_management_packet(struct sk_buff * skb,struct batadv_hard_iface * hard_iface,int header_len)177 bool batadv_check_management_packet(struct sk_buff *skb,
178 				    struct batadv_hard_iface *hard_iface,
179 				    int header_len)
180 {
181 	struct ethhdr *ethhdr;
182 
183 	/* drop packet if it has not necessary minimum size */
184 	if (unlikely(!pskb_may_pull(skb, header_len)))
185 		return false;
186 
187 	ethhdr = eth_hdr(skb);
188 
189 	/* packet with broadcast indication but unicast recipient */
190 	if (!is_broadcast_ether_addr(ethhdr->h_dest))
191 		return false;
192 
193 	/* packet with invalid sender address */
194 	if (!is_valid_ether_addr(ethhdr->h_source))
195 		return false;
196 
197 	/* create a copy of the skb, if needed, to modify it. */
198 	if (skb_cow(skb, 0) < 0)
199 		return false;
200 
201 	/* keep skb linear */
202 	if (skb_linearize(skb) < 0)
203 		return false;
204 
205 	return true;
206 }
207 
208 /**
209  * batadv_skb_decrement_ttl() - decrement ttl in a batman-adv header, csum-safe
210  * @skb: the received packet with @skb->data pointing to the batman-adv header
211  *
212  * Supports the following packet types, all of which carry the TTL at offset 2:
213  *
214  * - batadv_ogm_packet
215  * - batadv_ogm2_packet
216  * - batadv_icmp_header
217  * - batadv_icmp_packet
218  * - batadv_icmp_tp_packet
219  * - batadv_icmp_packet_rr
220  * - batadv_unicast_packet
221  * - batadv_frag_packet
222  * - batadv_bcast_packet
223  * - batadv_mcast_packet
224  * - batadv_coded_packet
225  * - batadv_unicast_tvlv_packet
226  *
227  * Return: true if the packet may be forwarded (ttl decremented),
228  *  false if it must be dropped (ttl would expire)
229  */
batadv_skb_decrement_ttl(struct sk_buff * skb)230 static bool batadv_skb_decrement_ttl(struct sk_buff *skb)
231 {
232 	static const size_t ttl_offset = 2;
233 	u8 *ttl_pos;
234 
235 	BUILD_BUG_ON(offsetof(struct batadv_ogm_packet, ttl) != ttl_offset);
236 	BUILD_BUG_ON(offsetof(struct batadv_ogm2_packet, ttl) != ttl_offset);
237 	BUILD_BUG_ON(offsetof(struct batadv_icmp_header, ttl) != ttl_offset);
238 	BUILD_BUG_ON(offsetof(struct batadv_icmp_packet, ttl) != ttl_offset);
239 	BUILD_BUG_ON(offsetof(struct batadv_icmp_tp_packet, ttl) != ttl_offset);
240 	BUILD_BUG_ON(offsetof(struct batadv_icmp_packet_rr, ttl) != ttl_offset);
241 	BUILD_BUG_ON(offsetof(struct batadv_unicast_packet, ttl) != ttl_offset);
242 	BUILD_BUG_ON(offsetof(struct batadv_frag_packet, ttl) != ttl_offset);
243 	BUILD_BUG_ON(offsetof(struct batadv_bcast_packet, ttl) != ttl_offset);
244 	BUILD_BUG_ON(offsetof(struct batadv_mcast_packet, ttl) != ttl_offset);
245 	BUILD_BUG_ON(offsetof(struct batadv_coded_packet, ttl) != ttl_offset);
246 	BUILD_BUG_ON(offsetof(struct batadv_unicast_tvlv_packet, ttl) != ttl_offset);
247 
248 	ttl_pos = skb->data + ttl_offset;
249 
250 	/* would expire on this hop -> drop, leave header + csum untouched */
251 	if (*ttl_pos < 2)
252 		return false;
253 
254 	skb_postpull_rcsum(skb, ttl_pos, 1);
255 	(*ttl_pos)--;
256 	skb_postpush_rcsum(skb, ttl_pos, 1);
257 
258 	return true;
259 }
260 
261 /**
262  * batadv_recv_my_icmp_packet() - receive an icmp packet locally
263  * @bat_priv: the bat priv with all the mesh interface information
264  * @skb: icmp packet to process
265  *
266  * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
267  * otherwise.
268  */
batadv_recv_my_icmp_packet(struct batadv_priv * bat_priv,struct sk_buff * skb)269 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
270 				      struct sk_buff *skb)
271 {
272 	struct batadv_hard_iface *primary_if = NULL;
273 	struct batadv_orig_node *orig_node = NULL;
274 	struct batadv_icmp_header *icmph;
275 	int res, ret = NET_RX_DROP;
276 
277 	icmph = (struct batadv_icmp_header *)skb->data;
278 
279 	switch (icmph->msg_type) {
280 	case BATADV_ECHO_REQUEST:
281 		/* answer echo request (ping) */
282 		primary_if = batadv_primary_if_get_selected(bat_priv);
283 		if (!primary_if)
284 			goto out;
285 
286 		/* get routing information */
287 		orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
288 		if (!orig_node)
289 			goto out;
290 
291 		/* create a copy of the skb, if needed, to modify it. */
292 		if (skb_cow(skb, ETH_HLEN) < 0)
293 			goto out;
294 
295 		icmph = (struct batadv_icmp_header *)skb->data;
296 
297 		ether_addr_copy(icmph->dst, icmph->orig);
298 		ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
299 		icmph->msg_type = BATADV_ECHO_REPLY;
300 		icmph->ttl = BATADV_TTL;
301 
302 		res = batadv_send_skb_to_orig(skb, orig_node, NULL);
303 		if (res == NET_XMIT_SUCCESS)
304 			ret = NET_RX_SUCCESS;
305 
306 		/* skb was consumed */
307 		skb = NULL;
308 		break;
309 	case BATADV_TP:
310 		if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
311 			goto out;
312 
313 		batadv_tp_meter_recv(bat_priv, skb);
314 		ret = NET_RX_SUCCESS;
315 		/* skb was consumed */
316 		skb = NULL;
317 		goto out;
318 	default:
319 		/* drop unknown type */
320 		goto out;
321 	}
322 out:
323 	batadv_hardif_put(primary_if);
324 	batadv_orig_node_put(orig_node);
325 
326 	kfree_skb(skb);
327 
328 	return ret;
329 }
330 
batadv_recv_icmp_ttl_exceeded(struct batadv_priv * bat_priv,struct sk_buff * skb)331 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
332 					 struct sk_buff *skb)
333 {
334 	struct batadv_hard_iface *primary_if = NULL;
335 	struct batadv_orig_node *orig_node = NULL;
336 	struct batadv_icmp_packet *icmp_packet;
337 	int res, ret = NET_RX_DROP;
338 
339 	icmp_packet = (struct batadv_icmp_packet *)skb->data;
340 
341 	/* send TTL exceeded if packet is an echo request (traceroute) */
342 	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
343 		pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
344 			 icmp_packet->orig, icmp_packet->dst);
345 		goto out;
346 	}
347 
348 	primary_if = batadv_primary_if_get_selected(bat_priv);
349 	if (!primary_if)
350 		goto out;
351 
352 	/* get routing information */
353 	orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
354 	if (!orig_node)
355 		goto out;
356 
357 	/* create a copy of the skb, if needed, to modify it. */
358 	if (skb_cow(skb, ETH_HLEN) < 0)
359 		goto out;
360 
361 	icmp_packet = (struct batadv_icmp_packet *)skb->data;
362 
363 	ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
364 	ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
365 	icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
366 	icmp_packet->ttl = BATADV_TTL;
367 
368 	res = batadv_send_skb_to_orig(skb, orig_node, NULL);
369 	if (res == NET_RX_SUCCESS)
370 		ret = NET_XMIT_SUCCESS;
371 
372 	/* skb was consumed */
373 	skb = NULL;
374 
375 out:
376 	batadv_hardif_put(primary_if);
377 	batadv_orig_node_put(orig_node);
378 
379 	kfree_skb(skb);
380 
381 	return ret;
382 }
383 
384 /**
385  * batadv_recv_icmp_packet() - Process incoming icmp packet
386  * @skb: incoming packet buffer
387  * @recv_if: incoming hard interface
388  *
389  * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
390  */
batadv_recv_icmp_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)391 int batadv_recv_icmp_packet(struct sk_buff *skb,
392 			    struct batadv_hard_iface *recv_if)
393 {
394 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
395 	struct batadv_icmp_header *icmph;
396 	struct batadv_icmp_packet_rr *icmp_packet_rr;
397 	struct ethhdr *ethhdr;
398 	struct batadv_orig_node *orig_node = NULL;
399 	int hdr_size = sizeof(struct batadv_icmp_header);
400 	int res, ret = NET_RX_DROP;
401 
402 	/* drop packet if it has not necessary minimum size */
403 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
404 		goto free_skb;
405 
406 	ethhdr = eth_hdr(skb);
407 
408 	/* packet with unicast indication but non-unicast recipient */
409 	if (!is_valid_ether_addr(ethhdr->h_dest))
410 		goto free_skb;
411 
412 	/* packet with broadcast/multicast sender address */
413 	if (is_multicast_ether_addr(ethhdr->h_source))
414 		goto free_skb;
415 
416 	/* not for me */
417 	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
418 		goto free_skb;
419 
420 	icmph = (struct batadv_icmp_header *)skb->data;
421 
422 	/* add record route information if not full */
423 	if ((icmph->msg_type == BATADV_ECHO_REPLY ||
424 	     icmph->msg_type == BATADV_ECHO_REQUEST) &&
425 	    skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
426 		if (skb_linearize(skb) < 0)
427 			goto free_skb;
428 
429 		/* create a copy of the skb, if needed, to modify it. */
430 		if (skb_cow(skb, ETH_HLEN) < 0)
431 			goto free_skb;
432 
433 		ethhdr = eth_hdr(skb);
434 		icmph = (struct batadv_icmp_header *)skb->data;
435 		icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
436 		if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
437 			goto free_skb;
438 
439 		ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
440 				ethhdr->h_dest);
441 		icmp_packet_rr->rr_cur++;
442 	}
443 
444 	/* packet for me */
445 	if (batadv_is_my_mac(bat_priv, icmph->dst))
446 		return batadv_recv_my_icmp_packet(bat_priv, skb);
447 
448 	/* TTL exceeded */
449 	if (icmph->ttl < 2)
450 		return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
451 
452 	/* get routing information */
453 	orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
454 	if (!orig_node)
455 		goto free_skb;
456 
457 	/* create a copy of the skb, if needed, to modify it. */
458 	if (skb_cow(skb, ETH_HLEN) < 0)
459 		goto put_orig_node;
460 
461 	icmph = (struct batadv_icmp_header *)skb->data;
462 
463 	/* decrement ttl */
464 	icmph->ttl--;
465 
466 	/* route it */
467 	res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
468 	if (res == NET_XMIT_SUCCESS)
469 		ret = NET_RX_SUCCESS;
470 
471 	/* skb was consumed */
472 	skb = NULL;
473 
474 put_orig_node:
475 	batadv_orig_node_put(orig_node);
476 free_skb:
477 	kfree_skb(skb);
478 
479 	return ret;
480 }
481 
482 /**
483  * batadv_check_unicast_packet() - Check for malformed unicast packets
484  * @bat_priv: the bat priv with all the mesh interface information
485  * @skb: packet to check
486  * @hdr_size: size of header to pull
487  *
488  * Checks for short header and bad addresses in the given packet.
489  *
490  * Return: negative value when check fails and 0 otherwise. The negative value
491  * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
492  * destination or source, and -EREMOTE for non-local (other host) destination.
493  */
batadv_check_unicast_packet(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_size)494 static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
495 				       struct sk_buff *skb, int hdr_size)
496 {
497 	struct ethhdr *ethhdr;
498 
499 	/* drop packet if it has not necessary minimum size */
500 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
501 		return -ENODATA;
502 
503 	ethhdr = eth_hdr(skb);
504 
505 	/* packet with unicast indication but non-unicast recipient */
506 	if (!is_valid_ether_addr(ethhdr->h_dest))
507 		return -EBADR;
508 
509 	/* packet with broadcast/multicast sender address */
510 	if (is_multicast_ether_addr(ethhdr->h_source))
511 		return -EBADR;
512 
513 	/* not for me */
514 	if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
515 		return -EREMOTE;
516 
517 	return 0;
518 }
519 
520 /**
521  * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
522  * @orig_node: originator node whose last bonding candidate should be retrieved
523  *
524  * Return: last bonding candidate of router or NULL if not found
525  *
526  * The object is returned with refcounter increased by 1.
527  */
528 static struct batadv_orig_ifinfo *
batadv_last_bonding_get(struct batadv_orig_node * orig_node)529 batadv_last_bonding_get(struct batadv_orig_node *orig_node)
530 {
531 	struct batadv_orig_ifinfo *last_bonding_candidate;
532 
533 	spin_lock_bh(&orig_node->neigh_list_lock);
534 	last_bonding_candidate = orig_node->last_bonding_candidate;
535 
536 	if (last_bonding_candidate)
537 		kref_get(&last_bonding_candidate->refcount);
538 	spin_unlock_bh(&orig_node->neigh_list_lock);
539 
540 	return last_bonding_candidate;
541 }
542 
543 /**
544  * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
545  * @orig_node: originator node whose bonding candidates should be replaced
546  * @new_candidate: new bonding candidate or NULL
547  */
548 static void
batadv_last_bonding_replace(struct batadv_orig_node * orig_node,struct batadv_orig_ifinfo * new_candidate)549 batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
550 			    struct batadv_orig_ifinfo *new_candidate)
551 {
552 	struct batadv_orig_ifinfo *old_candidate;
553 
554 	spin_lock_bh(&orig_node->neigh_list_lock);
555 	old_candidate = orig_node->last_bonding_candidate;
556 
557 	if (new_candidate)
558 		kref_get(&new_candidate->refcount);
559 	orig_node->last_bonding_candidate = new_candidate;
560 	spin_unlock_bh(&orig_node->neigh_list_lock);
561 
562 	batadv_orig_ifinfo_put(old_candidate);
563 }
564 
565 /**
566  * batadv_find_router() - find a suitable router for this originator
567  * @bat_priv: the bat priv with all the mesh interface information
568  * @orig_node: the destination node
569  * @recv_if: pointer to interface this packet was received on
570  *
571  * Return: the router which should be used for this orig_node on
572  * this interface, or NULL if not available.
573  */
574 struct batadv_neigh_node *
batadv_find_router(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_hard_iface * recv_if)575 batadv_find_router(struct batadv_priv *bat_priv,
576 		   struct batadv_orig_node *orig_node,
577 		   struct batadv_hard_iface *recv_if)
578 {
579 	struct batadv_algo_ops *bao = bat_priv->algo_ops;
580 	struct batadv_neigh_node *first_candidate_router = NULL;
581 	struct batadv_neigh_node *next_candidate_router = NULL;
582 	struct batadv_neigh_node *router, *cand_router = NULL;
583 	struct batadv_neigh_node *last_cand_router = NULL;
584 	struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
585 	struct batadv_orig_ifinfo *next_candidate = NULL;
586 	struct batadv_orig_ifinfo *last_candidate;
587 	bool last_candidate_found = false;
588 
589 	if (!orig_node)
590 		return NULL;
591 
592 	router = batadv_orig_router_get(orig_node, recv_if);
593 
594 	if (!router)
595 		return router;
596 
597 	/* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
598 	 * and if activated.
599 	 */
600 	if (!(recv_if == BATADV_IF_DEFAULT && READ_ONCE(bat_priv->bonding)))
601 		return router;
602 
603 	/* bonding: loop through the list of possible routers found
604 	 * for the various outgoing interfaces and find a candidate after
605 	 * the last chosen bonding candidate (next_candidate). If no such
606 	 * router is found, use the first candidate found (the previously
607 	 * chosen bonding candidate might have been the last one in the list).
608 	 * If this can't be found either, return the previously chosen
609 	 * router - obviously there are no other candidates.
610 	 */
611 	rcu_read_lock();
612 	last_candidate = batadv_last_bonding_get(orig_node);
613 	if (last_candidate)
614 		last_cand_router = rcu_dereference(last_candidate->router);
615 
616 	hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
617 		/* acquire some structures and references ... */
618 		if (!kref_get_unless_zero(&cand->refcount))
619 			continue;
620 
621 		cand_router = rcu_dereference(cand->router);
622 		if (!cand_router)
623 			goto next;
624 
625 		if (!kref_get_unless_zero(&cand_router->refcount)) {
626 			cand_router = NULL;
627 			goto next;
628 		}
629 
630 		/* alternative candidate should be good enough to be
631 		 * considered
632 		 */
633 		if (!bao->neigh.is_similar_or_better(cand_router,
634 						     cand->if_outgoing, router,
635 						     recv_if))
636 			goto next;
637 
638 		/* don't use the same router twice */
639 		if (last_cand_router == cand_router)
640 			goto next;
641 
642 		/* mark the first possible candidate */
643 		if (!first_candidate) {
644 			kref_get(&cand_router->refcount);
645 			kref_get(&cand->refcount);
646 			first_candidate = cand;
647 			first_candidate_router = cand_router;
648 		}
649 
650 		/* check if the loop has already passed the previously selected
651 		 * candidate ... this function should select the next candidate
652 		 * AFTER the previously used bonding candidate.
653 		 */
654 		if (!last_candidate || last_candidate_found) {
655 			next_candidate = cand;
656 			next_candidate_router = cand_router;
657 			break;
658 		}
659 
660 		if (last_candidate == cand)
661 			last_candidate_found = true;
662 next:
663 		/* free references */
664 		if (cand_router) {
665 			batadv_neigh_node_put(cand_router);
666 			cand_router = NULL;
667 		}
668 		batadv_orig_ifinfo_put(cand);
669 	}
670 	rcu_read_unlock();
671 
672 	/* After finding candidates, handle the three cases:
673 	 * 1) there is a next candidate, use that
674 	 * 2) there is no next candidate, use the first of the list
675 	 * 3) there is no candidate at all, return the default router
676 	 */
677 	if (next_candidate) {
678 		batadv_neigh_node_put(router);
679 
680 		kref_get(&next_candidate_router->refcount);
681 		router = next_candidate_router;
682 		batadv_last_bonding_replace(orig_node, next_candidate);
683 	} else if (first_candidate) {
684 		batadv_neigh_node_put(router);
685 
686 		kref_get(&first_candidate_router->refcount);
687 		router = first_candidate_router;
688 		batadv_last_bonding_replace(orig_node, first_candidate);
689 	} else {
690 		batadv_last_bonding_replace(orig_node, NULL);
691 	}
692 
693 	/* cleanup of candidates */
694 	if (first_candidate) {
695 		batadv_neigh_node_put(first_candidate_router);
696 		batadv_orig_ifinfo_put(first_candidate);
697 	}
698 
699 	if (next_candidate) {
700 		batadv_neigh_node_put(next_candidate_router);
701 		batadv_orig_ifinfo_put(next_candidate);
702 	}
703 
704 	batadv_orig_ifinfo_put(last_candidate);
705 
706 	return router;
707 }
708 
batadv_route_unicast_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)709 static int batadv_route_unicast_packet(struct sk_buff *skb,
710 				       struct batadv_hard_iface *recv_if)
711 {
712 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
713 	struct batadv_orig_node *orig_node = NULL;
714 	struct batadv_unicast_packet *unicast_packet;
715 	struct ethhdr *ethhdr = eth_hdr(skb);
716 	int res, hdr_len, ret = NET_RX_DROP;
717 	unsigned int len;
718 
719 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
720 
721 	/* TTL exceeded */
722 	if (unicast_packet->ttl < 2) {
723 		pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
724 			 ethhdr->h_source, unicast_packet->dest);
725 		goto free_skb;
726 	}
727 
728 	/* get routing information */
729 	orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
730 
731 	if (!orig_node)
732 		goto free_skb;
733 
734 	/* create a copy of the skb, if needed, to modify it. */
735 	if (skb_cow(skb, ETH_HLEN) < 0)
736 		goto put_orig_node;
737 
738 	/* decrement ttl */
739 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
740 	unicast_packet->ttl--;
741 
742 	switch (unicast_packet->packet_type) {
743 	case BATADV_UNICAST_4ADDR:
744 		hdr_len = sizeof(struct batadv_unicast_4addr_packet);
745 		break;
746 	case BATADV_UNICAST:
747 		hdr_len = sizeof(struct batadv_unicast_packet);
748 		break;
749 	default:
750 		/* other packet types not supported - yet */
751 		hdr_len = -1;
752 		break;
753 	}
754 
755 	if (hdr_len > 0)
756 		batadv_skb_set_priority(skb, hdr_len);
757 
758 	len = skb->len;
759 	res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
760 
761 	/* translate transmit result into receive result */
762 	if (res == NET_XMIT_SUCCESS) {
763 		ret = NET_RX_SUCCESS;
764 		/* skb was transmitted and consumed */
765 		batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
766 		batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
767 				   len + ETH_HLEN);
768 	}
769 
770 	/* skb was consumed */
771 	skb = NULL;
772 
773 put_orig_node:
774 	batadv_orig_node_put(orig_node);
775 free_skb:
776 	kfree_skb(skb);
777 
778 	return ret;
779 }
780 
781 /**
782  * batadv_reroute_unicast_packet() - update the unicast header for re-routing
783  * @bat_priv: the bat priv with all the mesh interface information
784  * @skb: unicast packet to process
785  * @unicast_packet: the unicast header to be updated
786  * @dst_addr: the payload destination
787  * @vid: VLAN identifier
788  *
789  * Search the translation table for dst_addr and update the unicast header with
790  * the new corresponding information (originator address where the destination
791  * client currently is and its known TTVN)
792  *
793  * Return: true if the packet header has been updated, false otherwise
794  */
795 static bool
batadv_reroute_unicast_packet(struct batadv_priv * bat_priv,struct sk_buff * skb,struct batadv_unicast_packet * unicast_packet,u8 * dst_addr,unsigned short vid)796 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
797 			      struct batadv_unicast_packet *unicast_packet,
798 			      u8 *dst_addr, unsigned short vid)
799 {
800 	struct batadv_orig_node *orig_node = NULL;
801 	struct batadv_hard_iface *primary_if = NULL;
802 	bool ret = false;
803 	const u8 *orig_addr;
804 	u8 orig_ttvn;
805 
806 	if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
807 		primary_if = batadv_primary_if_get_selected(bat_priv);
808 		if (!primary_if)
809 			goto out;
810 		orig_addr = primary_if->net_dev->dev_addr;
811 		orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
812 	} else {
813 		orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
814 						     vid);
815 		if (!orig_node)
816 			goto out;
817 
818 		if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
819 			goto out;
820 
821 		orig_addr = orig_node->orig;
822 		orig_ttvn = READ_ONCE(orig_node->last_ttvn);
823 	}
824 
825 	/* update the packet header */
826 	skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
827 	ether_addr_copy(unicast_packet->dest, orig_addr);
828 	unicast_packet->ttvn = orig_ttvn;
829 	skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
830 
831 	ret = true;
832 out:
833 	batadv_hardif_put(primary_if);
834 	batadv_orig_node_put(orig_node);
835 
836 	return ret;
837 }
838 
batadv_check_unicast_ttvn(struct batadv_priv * bat_priv,struct sk_buff * skb,int hdr_len)839 static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
840 				      struct sk_buff *skb, int hdr_len)
841 {
842 	struct batadv_unicast_packet *unicast_packet;
843 	struct batadv_hard_iface *primary_if;
844 	struct batadv_orig_node *orig_node;
845 	u8 curr_ttvn, old_ttvn;
846 	struct ethhdr *ethhdr;
847 	unsigned short vid;
848 	int is_old_ttvn;
849 
850 	/* check if there is enough data before accessing it */
851 	if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
852 		return false;
853 
854 	/* create a copy of the skb (in case of for re-routing) to modify it. */
855 	if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
856 		return false;
857 
858 	vid = batadv_get_vid(skb, hdr_len);
859 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
860 	ethhdr = (struct ethhdr *)(skb->data + hdr_len);
861 
862 	/* do not reroute multicast frames in a unicast header */
863 	if (is_multicast_ether_addr(ethhdr->h_dest))
864 		return true;
865 
866 	/* check if the destination client was served by this node and it is now
867 	 * roaming. In this case, it means that the node has got a ROAM_ADV
868 	 * message and that it knows the new destination in the mesh to re-route
869 	 * the packet to
870 	 */
871 	if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
872 		if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
873 						  ethhdr->h_dest, vid))
874 			batadv_dbg_ratelimited(BATADV_DBG_TT,
875 					       bat_priv,
876 					       "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
877 					       unicast_packet->dest,
878 					       ethhdr->h_dest);
879 		/* at this point the mesh destination should have been
880 		 * substituted with the originator address found in the global
881 		 * table. If not, let the packet go untouched anyway because
882 		 * there is nothing the node can do
883 		 */
884 		return true;
885 	}
886 
887 	/* retrieve the TTVN known by this node for the packet destination. This
888 	 * value is used later to check if the node which sent (or re-routed
889 	 * last time) the packet had an updated information or not
890 	 */
891 	curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
892 	if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
893 		orig_node = batadv_orig_hash_find(bat_priv,
894 						  unicast_packet->dest);
895 		/* if it is not possible to find the orig_node representing the
896 		 * destination, the packet can immediately be dropped as it will
897 		 * not be possible to deliver it
898 		 */
899 		if (!orig_node)
900 			return false;
901 
902 		curr_ttvn = READ_ONCE(orig_node->last_ttvn);
903 		batadv_orig_node_put(orig_node);
904 	}
905 
906 	/* check if the TTVN contained in the packet is fresher than what the
907 	 * node knows
908 	 */
909 	is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
910 	if (!is_old_ttvn)
911 		return true;
912 
913 	old_ttvn = unicast_packet->ttvn;
914 	/* the packet was forged based on outdated network information. Its
915 	 * destination can possibly be updated and forwarded towards the new
916 	 * target host
917 	 */
918 	if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
919 					  ethhdr->h_dest, vid)) {
920 		batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
921 				       "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
922 				       unicast_packet->dest, ethhdr->h_dest,
923 				       old_ttvn, curr_ttvn);
924 		return true;
925 	}
926 
927 	/* the packet has not been re-routed: either the destination is
928 	 * currently served by this node or there is no destination at all and
929 	 * it is possible to drop the packet
930 	 */
931 	if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
932 		return false;
933 
934 	/* update the header in order to let the packet be delivered to this
935 	 * node's mesh interface
936 	 */
937 	primary_if = batadv_primary_if_get_selected(bat_priv);
938 	if (!primary_if)
939 		return false;
940 
941 	/* update the packet header */
942 	skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
943 	ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
944 	unicast_packet->ttvn = curr_ttvn;
945 	skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
946 
947 	batadv_hardif_put(primary_if);
948 
949 	return true;
950 }
951 
952 /**
953  * batadv_recv_unhandled_unicast_packet() - receive and process packets which
954  *	are in the unicast number space but not yet known to the implementation
955  * @skb: unicast tvlv packet to process
956  * @recv_if: pointer to interface this packet was received on
957  *
958  * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
959  * otherwise.
960  */
batadv_recv_unhandled_unicast_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)961 int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
962 					 struct batadv_hard_iface *recv_if)
963 {
964 	struct batadv_unicast_packet *unicast_packet;
965 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
966 	int check, hdr_size = sizeof(*unicast_packet);
967 
968 	check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
969 	if (check < 0)
970 		goto free_skb;
971 
972 	/* we don't know about this type, drop it. */
973 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
974 	if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
975 		goto free_skb;
976 
977 	return batadv_route_unicast_packet(skb, recv_if);
978 
979 free_skb:
980 	kfree_skb(skb);
981 	return NET_RX_DROP;
982 }
983 
984 /**
985  * batadv_recv_unicast_packet() - Process incoming unicast packet
986  * @skb: incoming packet buffer
987  * @recv_if: incoming hard interface
988  *
989  * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
990  */
batadv_recv_unicast_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)991 int batadv_recv_unicast_packet(struct sk_buff *skb,
992 			       struct batadv_hard_iface *recv_if)
993 {
994 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
995 	struct batadv_unicast_packet *unicast_packet;
996 	struct batadv_unicast_4addr_packet *unicast_4addr_packet;
997 	u8 *orig_addr, *orig_addr_gw;
998 	struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
999 	int check, hdr_size = sizeof(*unicast_packet);
1000 	enum batadv_subtype subtype;
1001 	int ret = NET_RX_DROP;
1002 	bool is4addr, is_gw;
1003 
1004 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
1005 	is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
1006 	/* the caller function should have already pulled 2 bytes */
1007 	if (is4addr)
1008 		hdr_size = sizeof(*unicast_4addr_packet);
1009 
1010 	/* function returns -EREMOTE for promiscuous packets */
1011 	check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
1012 	if (check < 0)
1013 		goto free_skb;
1014 
1015 	if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
1016 		goto free_skb;
1017 
1018 	unicast_packet = (struct batadv_unicast_packet *)skb->data;
1019 
1020 	/* packet for me */
1021 	if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
1022 		/* If this is a unicast packet from another backgone gw,
1023 		 * drop it.
1024 		 */
1025 		orig_addr_gw = eth_hdr(skb)->h_source;
1026 		orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
1027 		if (orig_node_gw) {
1028 			is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
1029 							  hdr_size);
1030 			batadv_orig_node_put(orig_node_gw);
1031 			if (is_gw) {
1032 				orig_addr_gw = eth_hdr(skb)->h_source;
1033 				batadv_dbg(BATADV_DBG_BLA, bat_priv,
1034 					   "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
1035 					   __func__, orig_addr_gw);
1036 				goto free_skb;
1037 			}
1038 		}
1039 
1040 		if (is4addr) {
1041 			unicast_4addr_packet =
1042 				(struct batadv_unicast_4addr_packet *)skb->data;
1043 			subtype = unicast_4addr_packet->subtype;
1044 			batadv_dat_inc_counter(bat_priv, subtype);
1045 
1046 			/* Only payload data should be considered for speedy
1047 			 * join. For example, DAT also uses unicast 4addr
1048 			 * types, but those packets should not be considered
1049 			 * for speedy join, since the clients do not actually
1050 			 * reside at the sending originator.
1051 			 */
1052 			if (subtype == BATADV_P_DATA) {
1053 				orig_addr = unicast_4addr_packet->src;
1054 				orig_node = batadv_orig_hash_find(bat_priv,
1055 								  orig_addr);
1056 			}
1057 		}
1058 
1059 		if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1060 							  hdr_size))
1061 			goto rx_success;
1062 		if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1063 							hdr_size))
1064 			goto rx_success;
1065 
1066 		batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1067 
1068 		batadv_interface_rx(recv_if->mesh_iface, skb, hdr_size,
1069 				    orig_node);
1070 
1071 rx_success:
1072 		batadv_orig_node_put(orig_node);
1073 
1074 		return NET_RX_SUCCESS;
1075 	}
1076 
1077 	ret = batadv_route_unicast_packet(skb, recv_if);
1078 	/* skb was consumed */
1079 	skb = NULL;
1080 
1081 free_skb:
1082 	kfree_skb(skb);
1083 
1084 	return ret;
1085 }
1086 
1087 /**
1088  * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
1089  * @skb: unicast tvlv packet to process
1090  * @recv_if: pointer to interface this packet was received on
1091  *
1092  * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
1093  * otherwise.
1094  */
batadv_recv_unicast_tvlv(struct sk_buff * skb,struct batadv_hard_iface * recv_if)1095 int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1096 			     struct batadv_hard_iface *recv_if)
1097 {
1098 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
1099 	struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1100 	unsigned char *tvlv_buff;
1101 	u16 tvlv_buff_len;
1102 	int hdr_size = sizeof(*unicast_tvlv_packet);
1103 	int ret = NET_RX_DROP;
1104 
1105 	if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1106 		goto free_skb;
1107 
1108 	/* the header is likely to be modified while forwarding */
1109 	if (skb_cow(skb, hdr_size) < 0)
1110 		goto free_skb;
1111 
1112 	/* packet needs to be linearized to access the tvlv content */
1113 	if (skb_linearize(skb) < 0)
1114 		goto free_skb;
1115 
1116 	unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1117 
1118 	tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1119 	tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1120 
1121 	if (tvlv_buff_len > skb->len - hdr_size)
1122 		goto free_skb;
1123 
1124 	ret = batadv_tvlv_containers_process(bat_priv, BATADV_UNICAST_TVLV,
1125 					     NULL, skb, tvlv_buff,
1126 					     tvlv_buff_len);
1127 
1128 	if (ret != NET_RX_SUCCESS) {
1129 		ret = batadv_route_unicast_packet(skb, recv_if);
1130 		/* skb was consumed */
1131 		skb = NULL;
1132 	}
1133 
1134 free_skb:
1135 	kfree_skb(skb);
1136 
1137 	return ret;
1138 }
1139 
1140 /**
1141  * batadv_recv_frag_packet() - process received fragment
1142  * @skb: the received fragment
1143  * @recv_if: interface that the skb is received on
1144  *
1145  * This function does one of the three following things: 1) Forward fragment, if
1146  * the assembled packet will exceed our MTU; 2) Buffer fragment, if we still
1147  * lack further fragments; 3) Merge fragments, if we have all needed parts.
1148  *
1149  * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1150  */
batadv_recv_frag_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)1151 int batadv_recv_frag_packet(struct sk_buff *skb,
1152 			    struct batadv_hard_iface *recv_if)
1153 {
1154 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
1155 	struct batadv_orig_node *orig_node_src = NULL;
1156 	struct batadv_frag_packet *frag_packet;
1157 	int ret = NET_RX_DROP;
1158 
1159 	if (batadv_check_unicast_packet(bat_priv, skb,
1160 					sizeof(*frag_packet)) < 0)
1161 		goto free_skb;
1162 
1163 	frag_packet = (struct batadv_frag_packet *)skb->data;
1164 	orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1165 	if (!orig_node_src)
1166 		goto free_skb;
1167 
1168 	skb->priority = frag_packet->priority + 256;
1169 
1170 	/* Route the fragment if it is not for us and too big to be merged. */
1171 	if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1172 	    batadv_frag_skb_fwd(skb, recv_if, orig_node_src, &ret)) {
1173 		/* skb was consumed */
1174 		skb = NULL;
1175 		goto put_orig_node;
1176 	}
1177 
1178 	batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1179 	batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1180 
1181 	/* Add fragment to buffer and merge if possible. */
1182 	if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1183 		goto put_orig_node;
1184 
1185 	/* Deliver merged packet to the appropriate handler, if it was
1186 	 * merged
1187 	 */
1188 	if (skb) {
1189 		batadv_batman_skb_recv(skb, recv_if->net_dev,
1190 				       &recv_if->batman_adv_ptype, NULL);
1191 		/* skb was consumed */
1192 		skb = NULL;
1193 	}
1194 
1195 	ret = NET_RX_SUCCESS;
1196 
1197 put_orig_node:
1198 	batadv_orig_node_put(orig_node_src);
1199 free_skb:
1200 	kfree_skb(skb);
1201 
1202 	return ret;
1203 }
1204 
1205 /**
1206  * batadv_recv_bcast_packet() - Process incoming broadcast packet
1207  * @skb: incoming packet buffer
1208  * @recv_if: incoming hard interface
1209  *
1210  * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
1211  */
batadv_recv_bcast_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)1212 int batadv_recv_bcast_packet(struct sk_buff *skb,
1213 			     struct batadv_hard_iface *recv_if)
1214 {
1215 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
1216 	struct batadv_orig_node *orig_node = NULL;
1217 	struct batadv_bcast_packet *bcast_packet;
1218 	struct ethhdr *ethhdr;
1219 	int hdr_size = sizeof(*bcast_packet);
1220 	s32 seq_diff;
1221 	u32 seqno;
1222 	int ret;
1223 
1224 	/* drop packet if it has not necessary minimum size */
1225 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1226 		goto free_skb;
1227 
1228 	ethhdr = eth_hdr(skb);
1229 
1230 	/* packet with broadcast indication but unicast recipient */
1231 	if (!is_broadcast_ether_addr(ethhdr->h_dest))
1232 		goto free_skb;
1233 
1234 	/* packet with broadcast/multicast sender address */
1235 	if (is_multicast_ether_addr(ethhdr->h_source))
1236 		goto free_skb;
1237 
1238 	/* ignore broadcasts sent by myself */
1239 	if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1240 		goto free_skb;
1241 
1242 	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1243 
1244 	/* ignore broadcasts originated by myself */
1245 	if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1246 		goto free_skb;
1247 
1248 	/* create a copy of the skb, if needed, to modify it. */
1249 	if (skb_cow(skb, ETH_HLEN) < 0)
1250 		goto free_skb;
1251 
1252 	bcast_packet = (struct batadv_bcast_packet *)skb->data;
1253 
1254 	if (!batadv_skb_decrement_ttl(skb))
1255 		goto free_skb;
1256 
1257 	orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1258 
1259 	if (!orig_node)
1260 		goto free_skb;
1261 
1262 	spin_lock_bh(&orig_node->bcast_seqno_lock);
1263 
1264 	seqno = ntohl(bcast_packet->seqno);
1265 	/* check whether the packet is a duplicate */
1266 	if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1267 			    seqno))
1268 		goto spin_unlock;
1269 
1270 	seq_diff = seqno - orig_node->last_bcast_seqno;
1271 
1272 	/* check whether the packet is old and the host just restarted. */
1273 	if (batadv_window_protected(bat_priv, seq_diff,
1274 				    BATADV_BCAST_MAX_AGE,
1275 				    &orig_node->bcast_seqno_reset, NULL))
1276 		goto spin_unlock;
1277 
1278 	/* mark broadcast in flood history, update window position
1279 	 * if required.
1280 	 */
1281 	if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1282 		orig_node->last_bcast_seqno = seqno;
1283 
1284 	spin_unlock_bh(&orig_node->bcast_seqno_lock);
1285 
1286 	/* check whether this has been sent by another originator before */
1287 	if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1288 		goto free_skb;
1289 
1290 	batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1291 
1292 	/* rebroadcast packet */
1293 	ret = batadv_forw_bcast_packet(bat_priv, skb, 0, false);
1294 	if (ret == NETDEV_TX_BUSY)
1295 		goto free_skb;
1296 
1297 	/* don't hand the broadcast up if it is from an originator
1298 	 * from the same backbone.
1299 	 */
1300 	if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1301 		goto free_skb;
1302 
1303 	if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1304 		goto rx_success;
1305 	if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1306 		goto rx_success;
1307 
1308 	batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1309 
1310 	/* broadcast for me */
1311 	batadv_interface_rx(recv_if->mesh_iface, skb, hdr_size, orig_node);
1312 
1313 rx_success:
1314 	ret = NET_RX_SUCCESS;
1315 	goto out;
1316 
1317 spin_unlock:
1318 	spin_unlock_bh(&orig_node->bcast_seqno_lock);
1319 free_skb:
1320 	kfree_skb(skb);
1321 	ret = NET_RX_DROP;
1322 out:
1323 	batadv_orig_node_put(orig_node);
1324 	return ret;
1325 }
1326 
1327 #ifdef CONFIG_BATMAN_ADV_MCAST
1328 /**
1329  * batadv_recv_mcast_packet() - process received batman-adv multicast packet
1330  * @skb: the received batman-adv multicast packet
1331  * @recv_if: interface that the skb is received on
1332  *
1333  * Parses the given, received batman-adv multicast packet. Depending on the
1334  * contents of its TVLV forwards it and/or decapsulates it to hand it to the
1335  * mesh interface.
1336  *
1337  * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1338  */
batadv_recv_mcast_packet(struct sk_buff * skb,struct batadv_hard_iface * recv_if)1339 int batadv_recv_mcast_packet(struct sk_buff *skb,
1340 			     struct batadv_hard_iface *recv_if)
1341 {
1342 	struct batadv_priv *bat_priv = netdev_priv(recv_if->mesh_iface);
1343 	struct batadv_mcast_packet *mcast_packet;
1344 	int hdr_size = sizeof(*mcast_packet);
1345 	unsigned char *tvlv_buff;
1346 	int ret = NET_RX_DROP;
1347 	u16 tvlv_buff_len;
1348 
1349 	if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1350 		goto free_skb;
1351 
1352 	/* create a copy of the skb, if needed, to modify it. */
1353 	if (skb_cow(skb, ETH_HLEN) < 0)
1354 		goto free_skb;
1355 
1356 	/* packet needs to be linearized to access the tvlv content */
1357 	if (skb_linearize(skb) < 0)
1358 		goto free_skb;
1359 
1360 	mcast_packet = (struct batadv_mcast_packet *)skb->data;
1361 	if (!batadv_skb_decrement_ttl(skb))
1362 		goto free_skb;
1363 
1364 	tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1365 	tvlv_buff_len = ntohs(mcast_packet->tvlv_len);
1366 
1367 	if (tvlv_buff_len > skb->len - hdr_size)
1368 		goto free_skb;
1369 
1370 	/* the fields of an multicast payload are accessed assuming (at least)
1371 	 * 2-byte alignment, so a following packet must start at an even offset.
1372 	 */
1373 	if (tvlv_buff_len & 1)
1374 		goto free_skb;
1375 
1376 	ret = batadv_tvlv_containers_process(bat_priv, BATADV_MCAST, NULL, skb,
1377 					     tvlv_buff, tvlv_buff_len);
1378 	if (ret >= 0) {
1379 		batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_RX);
1380 		batadv_add_counter(bat_priv, BATADV_CNT_MCAST_RX_BYTES,
1381 				   skb->len + ETH_HLEN);
1382 	}
1383 
1384 	hdr_size += tvlv_buff_len;
1385 
1386 	if (ret == NET_RX_SUCCESS && (skb->len - hdr_size >= ETH_HLEN)) {
1387 		batadv_inc_counter(bat_priv, BATADV_CNT_MCAST_RX_LOCAL);
1388 		batadv_add_counter(bat_priv, BATADV_CNT_MCAST_RX_LOCAL_BYTES,
1389 				   skb->len - hdr_size);
1390 
1391 		batadv_interface_rx(bat_priv->mesh_iface, skb, hdr_size, NULL);
1392 		/* skb was consumed */
1393 		skb = NULL;
1394 	}
1395 
1396 free_skb:
1397 	kfree_skb(skb);
1398 
1399 	return ret;
1400 }
1401 #endif /* CONFIG_BATMAN_ADV_MCAST */
1402