1 /*
2  * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21 
22 #include "main.h"
23 #include "routing.h"
24 #include "send.h"
25 #include "soft-interface.h"
26 #include "hard-interface.h"
27 #include "icmp_socket.h"
28 #include "translation-table.h"
29 #include "originator.h"
30 #include "vis.h"
31 #include "unicast.h"
32 
slide_own_bcast_window(struct hard_iface * hard_iface)33 void slide_own_bcast_window(struct hard_iface *hard_iface)
34 {
35 	struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
36 	struct hashtable_t *hash = bat_priv->orig_hash;
37 	struct hlist_node *node;
38 	struct hlist_head *head;
39 	struct orig_node *orig_node;
40 	unsigned long *word;
41 	uint32_t i;
42 	size_t word_index;
43 
44 	for (i = 0; i < hash->size; i++) {
45 		head = &hash->table[i];
46 
47 		rcu_read_lock();
48 		hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
49 			spin_lock_bh(&orig_node->ogm_cnt_lock);
50 			word_index = hard_iface->if_num * NUM_WORDS;
51 			word = &(orig_node->bcast_own[word_index]);
52 
53 			bit_get_packet(bat_priv, word, 1, 0);
54 			orig_node->bcast_own_sum[hard_iface->if_num] =
55 				bit_packet_count(word);
56 			spin_unlock_bh(&orig_node->ogm_cnt_lock);
57 		}
58 		rcu_read_unlock();
59 	}
60 }
61 
_update_route(struct bat_priv * bat_priv,struct orig_node * orig_node,struct neigh_node * neigh_node)62 static void _update_route(struct bat_priv *bat_priv,
63 			  struct orig_node *orig_node,
64 			  struct neigh_node *neigh_node)
65 {
66 	struct neigh_node *curr_router;
67 
68 	curr_router = orig_node_get_router(orig_node);
69 
70 	/* route deleted */
71 	if ((curr_router) && (!neigh_node)) {
72 		bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
73 			orig_node->orig);
74 		tt_global_del_orig(bat_priv, orig_node,
75 				   "Deleted route towards originator");
76 
77 	/* route added */
78 	} else if ((!curr_router) && (neigh_node)) {
79 
80 		bat_dbg(DBG_ROUTES, bat_priv,
81 			"Adding route towards: %pM (via %pM)\n",
82 			orig_node->orig, neigh_node->addr);
83 	/* route changed */
84 	} else if (neigh_node && curr_router) {
85 		bat_dbg(DBG_ROUTES, bat_priv,
86 			"Changing route towards: %pM (now via %pM - was via %pM)\n",
87 			orig_node->orig, neigh_node->addr,
88 			curr_router->addr);
89 	}
90 
91 	if (curr_router)
92 		neigh_node_free_ref(curr_router);
93 
94 	/* increase refcount of new best neighbor */
95 	if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
96 		neigh_node = NULL;
97 
98 	spin_lock_bh(&orig_node->neigh_list_lock);
99 	rcu_assign_pointer(orig_node->router, neigh_node);
100 	spin_unlock_bh(&orig_node->neigh_list_lock);
101 
102 	/* decrease refcount of previous best neighbor */
103 	if (curr_router)
104 		neigh_node_free_ref(curr_router);
105 }
106 
update_route(struct bat_priv * bat_priv,struct orig_node * orig_node,struct neigh_node * neigh_node)107 void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
108 		  struct neigh_node *neigh_node)
109 {
110 	struct neigh_node *router = NULL;
111 
112 	if (!orig_node)
113 		goto out;
114 
115 	router = orig_node_get_router(orig_node);
116 
117 	if (router != neigh_node)
118 		_update_route(bat_priv, orig_node, neigh_node);
119 
120 out:
121 	if (router)
122 		neigh_node_free_ref(router);
123 }
124 
125 /* caller must hold the neigh_list_lock */
bonding_candidate_del(struct orig_node * orig_node,struct neigh_node * neigh_node)126 void bonding_candidate_del(struct orig_node *orig_node,
127 			   struct neigh_node *neigh_node)
128 {
129 	/* this neighbor is not part of our candidate list */
130 	if (list_empty(&neigh_node->bonding_list))
131 		goto out;
132 
133 	list_del_rcu(&neigh_node->bonding_list);
134 	INIT_LIST_HEAD(&neigh_node->bonding_list);
135 	neigh_node_free_ref(neigh_node);
136 	atomic_dec(&orig_node->bond_candidates);
137 
138 out:
139 	return;
140 }
141 
bonding_candidate_add(struct orig_node * orig_node,struct neigh_node * neigh_node)142 void bonding_candidate_add(struct orig_node *orig_node,
143 			   struct neigh_node *neigh_node)
144 {
145 	struct hlist_node *node;
146 	struct neigh_node *tmp_neigh_node, *router = NULL;
147 	uint8_t interference_candidate = 0;
148 
149 	spin_lock_bh(&orig_node->neigh_list_lock);
150 
151 	/* only consider if it has the same primary address ...  */
152 	if (!compare_eth(orig_node->orig,
153 			 neigh_node->orig_node->primary_addr))
154 		goto candidate_del;
155 
156 	router = orig_node_get_router(orig_node);
157 	if (!router)
158 		goto candidate_del;
159 
160 	/* ... and is good enough to be considered */
161 	if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
162 		goto candidate_del;
163 
164 	/**
165 	 * check if we have another candidate with the same mac address or
166 	 * interface. If we do, we won't select this candidate because of
167 	 * possible interference.
168 	 */
169 	hlist_for_each_entry_rcu(tmp_neigh_node, node,
170 				 &orig_node->neigh_list, list) {
171 
172 		if (tmp_neigh_node == neigh_node)
173 			continue;
174 
175 		/* we only care if the other candidate is even
176 		* considered as candidate. */
177 		if (list_empty(&tmp_neigh_node->bonding_list))
178 			continue;
179 
180 		if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
181 		    (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
182 			interference_candidate = 1;
183 			break;
184 		}
185 	}
186 
187 	/* don't care further if it is an interference candidate */
188 	if (interference_candidate)
189 		goto candidate_del;
190 
191 	/* this neighbor already is part of our candidate list */
192 	if (!list_empty(&neigh_node->bonding_list))
193 		goto out;
194 
195 	if (!atomic_inc_not_zero(&neigh_node->refcount))
196 		goto out;
197 
198 	list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
199 	atomic_inc(&orig_node->bond_candidates);
200 	goto out;
201 
202 candidate_del:
203 	bonding_candidate_del(orig_node, neigh_node);
204 
205 out:
206 	spin_unlock_bh(&orig_node->neigh_list_lock);
207 
208 	if (router)
209 		neigh_node_free_ref(router);
210 }
211 
212 /* copy primary address for bonding */
bonding_save_primary(const struct orig_node * orig_node,struct orig_node * orig_neigh_node,const struct batman_ogm_packet * batman_ogm_packet)213 void bonding_save_primary(const struct orig_node *orig_node,
214 			  struct orig_node *orig_neigh_node,
215 			  const struct batman_ogm_packet *batman_ogm_packet)
216 {
217 	if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
218 		return;
219 
220 	memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
221 }
222 
223 /* checks whether the host restarted and is in the protection time.
224  * returns:
225  *  0 if the packet is to be accepted
226  *  1 if the packet is to be ignored.
227  */
window_protected(struct bat_priv * bat_priv,int32_t seq_num_diff,unsigned long * last_reset)228 int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
229 		     unsigned long *last_reset)
230 {
231 	if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
232 	    (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
233 		if (has_timed_out(*last_reset, RESET_PROTECTION_MS)) {
234 
235 			*last_reset = jiffies;
236 			bat_dbg(DBG_BATMAN, bat_priv,
237 				"old packet received, start protection\n");
238 
239 			return 0;
240 		} else {
241 			return 1;
242 		}
243 	}
244 	return 0;
245 }
246 
recv_bat_ogm_packet(struct sk_buff * skb,struct hard_iface * hard_iface)247 int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
248 {
249 	struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
250 	struct ethhdr *ethhdr;
251 
252 	/* drop packet if it has not necessary minimum size */
253 	if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN)))
254 		return NET_RX_DROP;
255 
256 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
257 
258 	/* packet with broadcast indication but unicast recipient */
259 	if (!is_broadcast_ether_addr(ethhdr->h_dest))
260 		return NET_RX_DROP;
261 
262 	/* packet with broadcast sender address */
263 	if (is_broadcast_ether_addr(ethhdr->h_source))
264 		return NET_RX_DROP;
265 
266 	/* create a copy of the skb, if needed, to modify it. */
267 	if (skb_cow(skb, 0) < 0)
268 		return NET_RX_DROP;
269 
270 	/* keep skb linear */
271 	if (skb_linearize(skb) < 0)
272 		return NET_RX_DROP;
273 
274 	bat_priv->bat_algo_ops->bat_ogm_receive(hard_iface, skb);
275 
276 	kfree_skb(skb);
277 	return NET_RX_SUCCESS;
278 }
279 
recv_my_icmp_packet(struct bat_priv * bat_priv,struct sk_buff * skb,size_t icmp_len)280 static int recv_my_icmp_packet(struct bat_priv *bat_priv,
281 			       struct sk_buff *skb, size_t icmp_len)
282 {
283 	struct hard_iface *primary_if = NULL;
284 	struct orig_node *orig_node = NULL;
285 	struct neigh_node *router = NULL;
286 	struct icmp_packet_rr *icmp_packet;
287 	int ret = NET_RX_DROP;
288 
289 	icmp_packet = (struct icmp_packet_rr *)skb->data;
290 
291 	/* add data to device queue */
292 	if (icmp_packet->msg_type != ECHO_REQUEST) {
293 		bat_socket_receive_packet(icmp_packet, icmp_len);
294 		goto out;
295 	}
296 
297 	primary_if = primary_if_get_selected(bat_priv);
298 	if (!primary_if)
299 		goto out;
300 
301 	/* answer echo request (ping) */
302 	/* get routing information */
303 	orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
304 	if (!orig_node)
305 		goto out;
306 
307 	router = orig_node_get_router(orig_node);
308 	if (!router)
309 		goto out;
310 
311 	/* create a copy of the skb, if needed, to modify it. */
312 	if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
313 		goto out;
314 
315 	icmp_packet = (struct icmp_packet_rr *)skb->data;
316 
317 	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
318 	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
319 	icmp_packet->msg_type = ECHO_REPLY;
320 	icmp_packet->header.ttl = TTL;
321 
322 	send_skb_packet(skb, router->if_incoming, router->addr);
323 	ret = NET_RX_SUCCESS;
324 
325 out:
326 	if (primary_if)
327 		hardif_free_ref(primary_if);
328 	if (router)
329 		neigh_node_free_ref(router);
330 	if (orig_node)
331 		orig_node_free_ref(orig_node);
332 	return ret;
333 }
334 
recv_icmp_ttl_exceeded(struct bat_priv * bat_priv,struct sk_buff * skb)335 static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
336 				  struct sk_buff *skb)
337 {
338 	struct hard_iface *primary_if = NULL;
339 	struct orig_node *orig_node = NULL;
340 	struct neigh_node *router = NULL;
341 	struct icmp_packet *icmp_packet;
342 	int ret = NET_RX_DROP;
343 
344 	icmp_packet = (struct icmp_packet *)skb->data;
345 
346 	/* send TTL exceeded if packet is an echo request (traceroute) */
347 	if (icmp_packet->msg_type != ECHO_REQUEST) {
348 		pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
349 			 icmp_packet->orig, icmp_packet->dst);
350 		goto out;
351 	}
352 
353 	primary_if = primary_if_get_selected(bat_priv);
354 	if (!primary_if)
355 		goto out;
356 
357 	/* get routing information */
358 	orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
359 	if (!orig_node)
360 		goto out;
361 
362 	router = orig_node_get_router(orig_node);
363 	if (!router)
364 		goto out;
365 
366 	/* create a copy of the skb, if needed, to modify it. */
367 	if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
368 		goto out;
369 
370 	icmp_packet = (struct icmp_packet *)skb->data;
371 
372 	memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
373 	memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
374 	icmp_packet->msg_type = TTL_EXCEEDED;
375 	icmp_packet->header.ttl = TTL;
376 
377 	send_skb_packet(skb, router->if_incoming, router->addr);
378 	ret = NET_RX_SUCCESS;
379 
380 out:
381 	if (primary_if)
382 		hardif_free_ref(primary_if);
383 	if (router)
384 		neigh_node_free_ref(router);
385 	if (orig_node)
386 		orig_node_free_ref(orig_node);
387 	return ret;
388 }
389 
390 
recv_icmp_packet(struct sk_buff * skb,struct hard_iface * recv_if)391 int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
392 {
393 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
394 	struct icmp_packet_rr *icmp_packet;
395 	struct ethhdr *ethhdr;
396 	struct orig_node *orig_node = NULL;
397 	struct neigh_node *router = NULL;
398 	int hdr_size = sizeof(struct icmp_packet);
399 	int ret = NET_RX_DROP;
400 
401 	/**
402 	 * we truncate all incoming icmp packets if they don't match our size
403 	 */
404 	if (skb->len >= sizeof(struct icmp_packet_rr))
405 		hdr_size = sizeof(struct icmp_packet_rr);
406 
407 	/* drop packet if it has not necessary minimum size */
408 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
409 		goto out;
410 
411 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
412 
413 	/* packet with unicast indication but broadcast recipient */
414 	if (is_broadcast_ether_addr(ethhdr->h_dest))
415 		goto out;
416 
417 	/* packet with broadcast sender address */
418 	if (is_broadcast_ether_addr(ethhdr->h_source))
419 		goto out;
420 
421 	/* not for me */
422 	if (!is_my_mac(ethhdr->h_dest))
423 		goto out;
424 
425 	icmp_packet = (struct icmp_packet_rr *)skb->data;
426 
427 	/* add record route information if not full */
428 	if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
429 	    (icmp_packet->rr_cur < BAT_RR_LEN)) {
430 		memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
431 		       ethhdr->h_dest, ETH_ALEN);
432 		icmp_packet->rr_cur++;
433 	}
434 
435 	/* packet for me */
436 	if (is_my_mac(icmp_packet->dst))
437 		return recv_my_icmp_packet(bat_priv, skb, hdr_size);
438 
439 	/* TTL exceeded */
440 	if (icmp_packet->header.ttl < 2)
441 		return recv_icmp_ttl_exceeded(bat_priv, skb);
442 
443 	/* get routing information */
444 	orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
445 	if (!orig_node)
446 		goto out;
447 
448 	router = orig_node_get_router(orig_node);
449 	if (!router)
450 		goto out;
451 
452 	/* create a copy of the skb, if needed, to modify it. */
453 	if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
454 		goto out;
455 
456 	icmp_packet = (struct icmp_packet_rr *)skb->data;
457 
458 	/* decrement ttl */
459 	icmp_packet->header.ttl--;
460 
461 	/* route it */
462 	send_skb_packet(skb, router->if_incoming, router->addr);
463 	ret = NET_RX_SUCCESS;
464 
465 out:
466 	if (router)
467 		neigh_node_free_ref(router);
468 	if (orig_node)
469 		orig_node_free_ref(orig_node);
470 	return ret;
471 }
472 
473 /* In the bonding case, send the packets in a round
474  * robin fashion over the remaining interfaces.
475  *
476  * This method rotates the bonding list and increases the
477  * returned router's refcount. */
find_bond_router(struct orig_node * primary_orig,const struct hard_iface * recv_if)478 static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
479 					   const struct hard_iface *recv_if)
480 {
481 	struct neigh_node *tmp_neigh_node;
482 	struct neigh_node *router = NULL, *first_candidate = NULL;
483 
484 	rcu_read_lock();
485 	list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
486 				bonding_list) {
487 		if (!first_candidate)
488 			first_candidate = tmp_neigh_node;
489 
490 		/* recv_if == NULL on the first node. */
491 		if (tmp_neigh_node->if_incoming == recv_if)
492 			continue;
493 
494 		if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
495 			continue;
496 
497 		router = tmp_neigh_node;
498 		break;
499 	}
500 
501 	/* use the first candidate if nothing was found. */
502 	if (!router && first_candidate &&
503 	    atomic_inc_not_zero(&first_candidate->refcount))
504 		router = first_candidate;
505 
506 	if (!router)
507 		goto out;
508 
509 	/* selected should point to the next element
510 	 * after the current router */
511 	spin_lock_bh(&primary_orig->neigh_list_lock);
512 	/* this is a list_move(), which unfortunately
513 	 * does not exist as rcu version */
514 	list_del_rcu(&primary_orig->bond_list);
515 	list_add_rcu(&primary_orig->bond_list,
516 		     &router->bonding_list);
517 	spin_unlock_bh(&primary_orig->neigh_list_lock);
518 
519 out:
520 	rcu_read_unlock();
521 	return router;
522 }
523 
524 /* Interface Alternating: Use the best of the
525  * remaining candidates which are not using
526  * this interface.
527  *
528  * Increases the returned router's refcount */
find_ifalter_router(struct orig_node * primary_orig,const struct hard_iface * recv_if)529 static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
530 					      const struct hard_iface *recv_if)
531 {
532 	struct neigh_node *tmp_neigh_node;
533 	struct neigh_node *router = NULL, *first_candidate = NULL;
534 
535 	rcu_read_lock();
536 	list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
537 				bonding_list) {
538 		if (!first_candidate)
539 			first_candidate = tmp_neigh_node;
540 
541 		/* recv_if == NULL on the first node. */
542 		if (tmp_neigh_node->if_incoming == recv_if)
543 			continue;
544 
545 		if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
546 			continue;
547 
548 		/* if we don't have a router yet
549 		 * or this one is better, choose it. */
550 		if ((!router) ||
551 		    (tmp_neigh_node->tq_avg > router->tq_avg)) {
552 			/* decrement refcount of
553 			 * previously selected router */
554 			if (router)
555 				neigh_node_free_ref(router);
556 
557 			router = tmp_neigh_node;
558 			atomic_inc_not_zero(&router->refcount);
559 		}
560 
561 		neigh_node_free_ref(tmp_neigh_node);
562 	}
563 
564 	/* use the first candidate if nothing was found. */
565 	if (!router && first_candidate &&
566 	    atomic_inc_not_zero(&first_candidate->refcount))
567 		router = first_candidate;
568 
569 	rcu_read_unlock();
570 	return router;
571 }
572 
recv_tt_query(struct sk_buff * skb,struct hard_iface * recv_if)573 int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
574 {
575 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
576 	struct tt_query_packet *tt_query;
577 	uint16_t tt_len;
578 	struct ethhdr *ethhdr;
579 
580 	/* drop packet if it has not necessary minimum size */
581 	if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
582 		goto out;
583 
584 	/* I could need to modify it */
585 	if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
586 		goto out;
587 
588 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
589 
590 	/* packet with unicast indication but broadcast recipient */
591 	if (is_broadcast_ether_addr(ethhdr->h_dest))
592 		goto out;
593 
594 	/* packet with broadcast sender address */
595 	if (is_broadcast_ether_addr(ethhdr->h_source))
596 		goto out;
597 
598 	tt_query = (struct tt_query_packet *)skb->data;
599 
600 	tt_query->tt_data = ntohs(tt_query->tt_data);
601 
602 	switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
603 	case TT_REQUEST:
604 		/* If we cannot provide an answer the tt_request is
605 		 * forwarded */
606 		if (!send_tt_response(bat_priv, tt_query)) {
607 			bat_dbg(DBG_TT, bat_priv,
608 				"Routing TT_REQUEST to %pM [%c]\n",
609 				tt_query->dst,
610 				(tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
611 			tt_query->tt_data = htons(tt_query->tt_data);
612 			return route_unicast_packet(skb, recv_if);
613 		}
614 		break;
615 	case TT_RESPONSE:
616 		if (is_my_mac(tt_query->dst)) {
617 			/* packet needs to be linearized to access the TT
618 			 * changes */
619 			if (skb_linearize(skb) < 0)
620 				goto out;
621 			/* skb_linearize() possibly changed skb->data */
622 			tt_query = (struct tt_query_packet *)skb->data;
623 
624 			tt_len = tt_query->tt_data * sizeof(struct tt_change);
625 
626 			/* Ensure we have all the claimed data */
627 			if (unlikely(skb_headlen(skb) <
628 				     sizeof(struct tt_query_packet) + tt_len))
629 				goto out;
630 
631 			handle_tt_response(bat_priv, tt_query);
632 		} else {
633 			bat_dbg(DBG_TT, bat_priv,
634 				"Routing TT_RESPONSE to %pM [%c]\n",
635 				tt_query->dst,
636 				(tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
637 			tt_query->tt_data = htons(tt_query->tt_data);
638 			return route_unicast_packet(skb, recv_if);
639 		}
640 		break;
641 	}
642 
643 out:
644 	/* returning NET_RX_DROP will make the caller function kfree the skb */
645 	return NET_RX_DROP;
646 }
647 
recv_roam_adv(struct sk_buff * skb,struct hard_iface * recv_if)648 int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
649 {
650 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
651 	struct roam_adv_packet *roam_adv_packet;
652 	struct orig_node *orig_node;
653 	struct ethhdr *ethhdr;
654 
655 	/* drop packet if it has not necessary minimum size */
656 	if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
657 		goto out;
658 
659 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
660 
661 	/* packet with unicast indication but broadcast recipient */
662 	if (is_broadcast_ether_addr(ethhdr->h_dest))
663 		goto out;
664 
665 	/* packet with broadcast sender address */
666 	if (is_broadcast_ether_addr(ethhdr->h_source))
667 		goto out;
668 
669 	roam_adv_packet = (struct roam_adv_packet *)skb->data;
670 
671 	if (!is_my_mac(roam_adv_packet->dst))
672 		return route_unicast_packet(skb, recv_if);
673 
674 	orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
675 	if (!orig_node)
676 		goto out;
677 
678 	bat_dbg(DBG_TT, bat_priv,
679 		"Received ROAMING_ADV from %pM (client %pM)\n",
680 		roam_adv_packet->src, roam_adv_packet->client);
681 
682 	tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
683 		      atomic_read(&orig_node->last_ttvn) + 1, true, false);
684 
685 	/* Roaming phase starts: I have new information but the ttvn has not
686 	 * been incremented yet. This flag will make me check all the incoming
687 	 * packets for the correct destination. */
688 	bat_priv->tt_poss_change = true;
689 
690 	orig_node_free_ref(orig_node);
691 out:
692 	/* returning NET_RX_DROP will make the caller function kfree the skb */
693 	return NET_RX_DROP;
694 }
695 
696 /* find a suitable router for this originator, and use
697  * bonding if possible. increases the found neighbors
698  * refcount.*/
find_router(struct bat_priv * bat_priv,struct orig_node * orig_node,const struct hard_iface * recv_if)699 struct neigh_node *find_router(struct bat_priv *bat_priv,
700 			       struct orig_node *orig_node,
701 			       const struct hard_iface *recv_if)
702 {
703 	struct orig_node *primary_orig_node;
704 	struct orig_node *router_orig;
705 	struct neigh_node *router;
706 	static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
707 	int bonding_enabled;
708 
709 	if (!orig_node)
710 		return NULL;
711 
712 	router = orig_node_get_router(orig_node);
713 	if (!router)
714 		goto err;
715 
716 	/* without bonding, the first node should
717 	 * always choose the default router. */
718 	bonding_enabled = atomic_read(&bat_priv->bonding);
719 
720 	rcu_read_lock();
721 	/* select default router to output */
722 	router_orig = router->orig_node;
723 	if (!router_orig)
724 		goto err_unlock;
725 
726 	if ((!recv_if) && (!bonding_enabled))
727 		goto return_router;
728 
729 	/* if we have something in the primary_addr, we can search
730 	 * for a potential bonding candidate. */
731 	if (compare_eth(router_orig->primary_addr, zero_mac))
732 		goto return_router;
733 
734 	/* find the orig_node which has the primary interface. might
735 	 * even be the same as our router_orig in many cases */
736 
737 	if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
738 		primary_orig_node = router_orig;
739 	} else {
740 		primary_orig_node = orig_hash_find(bat_priv,
741 						   router_orig->primary_addr);
742 		if (!primary_orig_node)
743 			goto return_router;
744 
745 		orig_node_free_ref(primary_orig_node);
746 	}
747 
748 	/* with less than 2 candidates, we can't do any
749 	 * bonding and prefer the original router. */
750 	if (atomic_read(&primary_orig_node->bond_candidates) < 2)
751 		goto return_router;
752 
753 	/* all nodes between should choose a candidate which
754 	 * is is not on the interface where the packet came
755 	 * in. */
756 
757 	neigh_node_free_ref(router);
758 
759 	if (bonding_enabled)
760 		router = find_bond_router(primary_orig_node, recv_if);
761 	else
762 		router = find_ifalter_router(primary_orig_node, recv_if);
763 
764 return_router:
765 	if (router && router->if_incoming->if_status != IF_ACTIVE)
766 		goto err_unlock;
767 
768 	rcu_read_unlock();
769 	return router;
770 err_unlock:
771 	rcu_read_unlock();
772 err:
773 	if (router)
774 		neigh_node_free_ref(router);
775 	return NULL;
776 }
777 
check_unicast_packet(struct sk_buff * skb,int hdr_size)778 static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
779 {
780 	struct ethhdr *ethhdr;
781 
782 	/* drop packet if it has not necessary minimum size */
783 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
784 		return -1;
785 
786 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
787 
788 	/* packet with unicast indication but broadcast recipient */
789 	if (is_broadcast_ether_addr(ethhdr->h_dest))
790 		return -1;
791 
792 	/* packet with broadcast sender address */
793 	if (is_broadcast_ether_addr(ethhdr->h_source))
794 		return -1;
795 
796 	/* not for me */
797 	if (!is_my_mac(ethhdr->h_dest))
798 		return -1;
799 
800 	return 0;
801 }
802 
route_unicast_packet(struct sk_buff * skb,struct hard_iface * recv_if)803 int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
804 {
805 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
806 	struct orig_node *orig_node = NULL;
807 	struct neigh_node *neigh_node = NULL;
808 	struct unicast_packet *unicast_packet;
809 	struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
810 	int ret = NET_RX_DROP;
811 	struct sk_buff *new_skb;
812 
813 	unicast_packet = (struct unicast_packet *)skb->data;
814 
815 	/* TTL exceeded */
816 	if (unicast_packet->header.ttl < 2) {
817 		pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
818 			 ethhdr->h_source, unicast_packet->dest);
819 		goto out;
820 	}
821 
822 	/* get routing information */
823 	orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
824 
825 	if (!orig_node)
826 		goto out;
827 
828 	/* find_router() increases neigh_nodes refcount if found. */
829 	neigh_node = find_router(bat_priv, orig_node, recv_if);
830 
831 	if (!neigh_node)
832 		goto out;
833 
834 	/* create a copy of the skb, if needed, to modify it. */
835 	if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
836 		goto out;
837 
838 	unicast_packet = (struct unicast_packet *)skb->data;
839 
840 	if (unicast_packet->header.packet_type == BAT_UNICAST &&
841 	    atomic_read(&bat_priv->fragmentation) &&
842 	    skb->len > neigh_node->if_incoming->net_dev->mtu) {
843 		ret = frag_send_skb(skb, bat_priv,
844 				    neigh_node->if_incoming, neigh_node->addr);
845 		goto out;
846 	}
847 
848 	if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
849 	    frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
850 
851 		ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
852 
853 		if (ret == NET_RX_DROP)
854 			goto out;
855 
856 		/* packet was buffered for late merge */
857 		if (!new_skb) {
858 			ret = NET_RX_SUCCESS;
859 			goto out;
860 		}
861 
862 		skb = new_skb;
863 		unicast_packet = (struct unicast_packet *)skb->data;
864 	}
865 
866 	/* decrement ttl */
867 	unicast_packet->header.ttl--;
868 
869 	/* route it */
870 	send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
871 	ret = NET_RX_SUCCESS;
872 
873 out:
874 	if (neigh_node)
875 		neigh_node_free_ref(neigh_node);
876 	if (orig_node)
877 		orig_node_free_ref(orig_node);
878 	return ret;
879 }
880 
check_unicast_ttvn(struct bat_priv * bat_priv,struct sk_buff * skb)881 static int check_unicast_ttvn(struct bat_priv *bat_priv,
882 			       struct sk_buff *skb) {
883 	uint8_t curr_ttvn;
884 	struct orig_node *orig_node;
885 	struct ethhdr *ethhdr;
886 	struct hard_iface *primary_if;
887 	struct unicast_packet *unicast_packet;
888 	bool tt_poss_change;
889 
890 	/* I could need to modify it */
891 	if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
892 		return 0;
893 
894 	unicast_packet = (struct unicast_packet *)skb->data;
895 
896 	if (is_my_mac(unicast_packet->dest)) {
897 		tt_poss_change = bat_priv->tt_poss_change;
898 		curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
899 	} else {
900 		orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
901 
902 		if (!orig_node)
903 			return 0;
904 
905 		curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
906 		tt_poss_change = orig_node->tt_poss_change;
907 		orig_node_free_ref(orig_node);
908 	}
909 
910 	/* Check whether I have to reroute the packet */
911 	if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
912 		/* Linearize the skb before accessing it */
913 		if (skb_linearize(skb) < 0)
914 			return 0;
915 
916 		ethhdr = (struct ethhdr *)(skb->data +
917 			sizeof(struct unicast_packet));
918 		orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
919 
920 		if (!orig_node) {
921 			if (!is_my_client(bat_priv, ethhdr->h_dest))
922 				return 0;
923 			primary_if = primary_if_get_selected(bat_priv);
924 			if (!primary_if)
925 				return 0;
926 			memcpy(unicast_packet->dest,
927 			       primary_if->net_dev->dev_addr, ETH_ALEN);
928 			hardif_free_ref(primary_if);
929 		} else {
930 			memcpy(unicast_packet->dest, orig_node->orig,
931 			       ETH_ALEN);
932 			curr_ttvn = (uint8_t)
933 				atomic_read(&orig_node->last_ttvn);
934 			orig_node_free_ref(orig_node);
935 		}
936 
937 		bat_dbg(DBG_ROUTES, bat_priv,
938 			"TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
939 			unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
940 			unicast_packet->dest);
941 
942 		unicast_packet->ttvn = curr_ttvn;
943 	}
944 	return 1;
945 }
946 
recv_unicast_packet(struct sk_buff * skb,struct hard_iface * recv_if)947 int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
948 {
949 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
950 	struct unicast_packet *unicast_packet;
951 	int hdr_size = sizeof(*unicast_packet);
952 
953 	if (check_unicast_packet(skb, hdr_size) < 0)
954 		return NET_RX_DROP;
955 
956 	if (!check_unicast_ttvn(bat_priv, skb))
957 		return NET_RX_DROP;
958 
959 	unicast_packet = (struct unicast_packet *)skb->data;
960 
961 	/* packet for me */
962 	if (is_my_mac(unicast_packet->dest)) {
963 		interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
964 		return NET_RX_SUCCESS;
965 	}
966 
967 	return route_unicast_packet(skb, recv_if);
968 }
969 
recv_ucast_frag_packet(struct sk_buff * skb,struct hard_iface * recv_if)970 int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
971 {
972 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
973 	struct unicast_frag_packet *unicast_packet;
974 	int hdr_size = sizeof(*unicast_packet);
975 	struct sk_buff *new_skb = NULL;
976 	int ret;
977 
978 	if (check_unicast_packet(skb, hdr_size) < 0)
979 		return NET_RX_DROP;
980 
981 	if (!check_unicast_ttvn(bat_priv, skb))
982 		return NET_RX_DROP;
983 
984 	unicast_packet = (struct unicast_frag_packet *)skb->data;
985 
986 	/* packet for me */
987 	if (is_my_mac(unicast_packet->dest)) {
988 
989 		ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
990 
991 		if (ret == NET_RX_DROP)
992 			return NET_RX_DROP;
993 
994 		/* packet was buffered for late merge */
995 		if (!new_skb)
996 			return NET_RX_SUCCESS;
997 
998 		interface_rx(recv_if->soft_iface, new_skb, recv_if,
999 			     sizeof(struct unicast_packet));
1000 		return NET_RX_SUCCESS;
1001 	}
1002 
1003 	return route_unicast_packet(skb, recv_if);
1004 }
1005 
1006 
recv_bcast_packet(struct sk_buff * skb,struct hard_iface * recv_if)1007 int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1008 {
1009 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1010 	struct orig_node *orig_node = NULL;
1011 	struct bcast_packet *bcast_packet;
1012 	struct ethhdr *ethhdr;
1013 	int hdr_size = sizeof(*bcast_packet);
1014 	int ret = NET_RX_DROP;
1015 	int32_t seq_diff;
1016 
1017 	/* drop packet if it has not necessary minimum size */
1018 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1019 		goto out;
1020 
1021 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
1022 
1023 	/* packet with broadcast indication but unicast recipient */
1024 	if (!is_broadcast_ether_addr(ethhdr->h_dest))
1025 		goto out;
1026 
1027 	/* packet with broadcast sender address */
1028 	if (is_broadcast_ether_addr(ethhdr->h_source))
1029 		goto out;
1030 
1031 	/* ignore broadcasts sent by myself */
1032 	if (is_my_mac(ethhdr->h_source))
1033 		goto out;
1034 
1035 	bcast_packet = (struct bcast_packet *)skb->data;
1036 
1037 	/* ignore broadcasts originated by myself */
1038 	if (is_my_mac(bcast_packet->orig))
1039 		goto out;
1040 
1041 	if (bcast_packet->header.ttl < 2)
1042 		goto out;
1043 
1044 	orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
1045 
1046 	if (!orig_node)
1047 		goto out;
1048 
1049 	spin_lock_bh(&orig_node->bcast_seqno_lock);
1050 
1051 	/* check whether the packet is a duplicate */
1052 	if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1053 			   ntohl(bcast_packet->seqno)))
1054 		goto spin_unlock;
1055 
1056 	seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1057 
1058 	/* check whether the packet is old and the host just restarted. */
1059 	if (window_protected(bat_priv, seq_diff,
1060 			     &orig_node->bcast_seqno_reset))
1061 		goto spin_unlock;
1062 
1063 	/* mark broadcast in flood history, update window position
1064 	 * if required. */
1065 	if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1066 		orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1067 
1068 	spin_unlock_bh(&orig_node->bcast_seqno_lock);
1069 
1070 	/* rebroadcast packet */
1071 	add_bcast_packet_to_list(bat_priv, skb, 1);
1072 
1073 	/* broadcast for me */
1074 	interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
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 		orig_node_free_ref(orig_node);
1083 	return ret;
1084 }
1085 
recv_vis_packet(struct sk_buff * skb,struct hard_iface * recv_if)1086 int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
1087 {
1088 	struct vis_packet *vis_packet;
1089 	struct ethhdr *ethhdr;
1090 	struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1091 	int hdr_size = sizeof(*vis_packet);
1092 
1093 	/* keep skb linear */
1094 	if (skb_linearize(skb) < 0)
1095 		return NET_RX_DROP;
1096 
1097 	if (unlikely(!pskb_may_pull(skb, hdr_size)))
1098 		return NET_RX_DROP;
1099 
1100 	vis_packet = (struct vis_packet *)skb->data;
1101 	ethhdr = (struct ethhdr *)skb_mac_header(skb);
1102 
1103 	/* not for me */
1104 	if (!is_my_mac(ethhdr->h_dest))
1105 		return NET_RX_DROP;
1106 
1107 	/* ignore own packets */
1108 	if (is_my_mac(vis_packet->vis_orig))
1109 		return NET_RX_DROP;
1110 
1111 	if (is_my_mac(vis_packet->sender_orig))
1112 		return NET_RX_DROP;
1113 
1114 	switch (vis_packet->vis_type) {
1115 	case VIS_TYPE_SERVER_SYNC:
1116 		receive_server_sync_packet(bat_priv, vis_packet,
1117 					   skb_headlen(skb));
1118 		break;
1119 
1120 	case VIS_TYPE_CLIENT_UPDATE:
1121 		receive_client_update_packet(bat_priv, vis_packet,
1122 					     skb_headlen(skb));
1123 		break;
1124 
1125 	default:	/* ignore unknown packet */
1126 		break;
1127 	}
1128 
1129 	/* We take a copy of the data in the packet, so we should
1130 	   always free the skbuf. */
1131 	return NET_RX_DROP;
1132 }
1133