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