]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/batman-adv/routing.c
batman-adv: consider outgoing interface in OGM sending
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / routing.c
CommitLineData
0b873931 1/* Copyright (C) 2007-2013 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
225 memcpy(icmph->dst, icmph->orig, ETH_ALEN);
226 memcpy(icmph->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
227 icmph->msg_type = BATADV_ECHO_REPLY;
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
27a417e6
AQ
279 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
280 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr,
0bf84c16 281 ETH_ALEN);
27a417e6
AQ
282 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
283 icmp_packet->ttl = BATADV_TTL;
44524fcd 284
e91ecfc6 285 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
bb351ba0 286 ret = NET_RX_SUCCESS;
c6c8fea2 287
44524fcd 288out:
32ae9b22 289 if (primary_if)
e5d89254 290 batadv_hardif_free_ref(primary_if);
44524fcd 291 if (orig_node)
7d211efc 292 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
293 return ret;
294}
295
296
56303d34
SE
297int batadv_recv_icmp_packet(struct sk_buff *skb,
298 struct batadv_hard_iface *recv_if)
c6c8fea2 299{
56303d34 300 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
da6b8c20
SW
301 struct batadv_icmp_header *icmph;
302 struct batadv_icmp_packet_rr *icmp_packet_rr;
c6c8fea2 303 struct ethhdr *ethhdr;
56303d34 304 struct batadv_orig_node *orig_node = NULL;
da6b8c20 305 int hdr_size = sizeof(struct batadv_icmp_header);
44524fcd 306 int ret = NET_RX_DROP;
c6c8fea2 307
c6c8fea2
SE
308 /* drop packet if it has not necessary minimum size */
309 if (unlikely(!pskb_may_pull(skb, hdr_size)))
44524fcd 310 goto out;
c6c8fea2 311
7ed4be95 312 ethhdr = eth_hdr(skb);
c6c8fea2
SE
313
314 /* packet with unicast indication but broadcast recipient */
315 if (is_broadcast_ether_addr(ethhdr->h_dest))
44524fcd 316 goto out;
c6c8fea2
SE
317
318 /* packet with broadcast sender address */
319 if (is_broadcast_ether_addr(ethhdr->h_source))
44524fcd 320 goto out;
c6c8fea2
SE
321
322 /* not for me */
fe8a93b9 323 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
44524fcd 324 goto out;
c6c8fea2 325
da6b8c20 326 icmph = (struct batadv_icmp_header *)skb->data;
c6c8fea2
SE
327
328 /* add record route information if not full */
da6b8c20
SW
329 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
330 icmph->msg_type == BATADV_ECHO_REQUEST) &&
331 (skb->len >= sizeof(struct batadv_icmp_packet_rr))) {
332 if (skb_linearize(skb) < 0)
333 goto out;
334
335 /* create a copy of the skb, if needed, to modify it. */
336 if (skb_cow(skb, ETH_HLEN) < 0)
337 goto out;
338
339 icmph = (struct batadv_icmp_header *)skb->data;
340 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
341 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
342 goto out;
343
344 memcpy(&(icmp_packet_rr->rr[icmp_packet_rr->rr_cur]),
7c64fd98 345 ethhdr->h_dest, ETH_ALEN);
da6b8c20 346 icmp_packet_rr->rr_cur++;
c6c8fea2
SE
347 }
348
349 /* packet for me */
da6b8c20
SW
350 if (batadv_is_my_mac(bat_priv, icmph->dst))
351 return batadv_recv_my_icmp_packet(bat_priv, skb);
c6c8fea2
SE
352
353 /* TTL exceeded */
a40d9b07 354 if (icmph->ttl < 2)
63b01037 355 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
c6c8fea2 356
c6c8fea2 357 /* get routing information */
da6b8c20 358 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
44524fcd 359 if (!orig_node)
e1a5382f 360 goto out;
c6c8fea2 361
44524fcd 362 /* create a copy of the skb, if needed, to modify it. */
0d125074 363 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 364 goto out;
c6c8fea2 365
da6b8c20 366 icmph = (struct batadv_icmp_header *)skb->data;
c6c8fea2 367
44524fcd 368 /* decrement ttl */
a40d9b07 369 icmph->ttl--;
44524fcd
ML
370
371 /* route it */
e91ecfc6 372 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
bb351ba0 373 ret = NET_RX_SUCCESS;
c6c8fea2 374
44524fcd 375out:
44524fcd 376 if (orig_node)
7d211efc 377 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
378 return ret;
379}
380
f86ce0ad
MH
381/**
382 * batadv_check_unicast_packet - Check for malformed unicast packets
6e0895c2 383 * @bat_priv: the bat priv with all the soft interface information
f86ce0ad
MH
384 * @skb: packet to check
385 * @hdr_size: size of header to pull
386 *
387 * Check for short header and bad addresses in given packet. Returns negative
388 * value when check fails and 0 otherwise. The negative value depends on the
389 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
390 * and -EREMOTE for non-local (other host) destination.
391 */
fe8a93b9
AQ
392static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
393 struct sk_buff *skb, int hdr_size)
ff51fd70
MH
394{
395 struct ethhdr *ethhdr;
396
397 /* drop packet if it has not necessary minimum size */
398 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f86ce0ad 399 return -ENODATA;
ff51fd70 400
7ed4be95 401 ethhdr = eth_hdr(skb);
ff51fd70
MH
402
403 /* packet with unicast indication but broadcast recipient */
404 if (is_broadcast_ether_addr(ethhdr->h_dest))
f86ce0ad 405 return -EBADR;
ff51fd70
MH
406
407 /* packet with broadcast sender address */
408 if (is_broadcast_ether_addr(ethhdr->h_source))
f86ce0ad 409 return -EBADR;
ff51fd70
MH
410
411 /* not for me */
fe8a93b9 412 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
f86ce0ad 413 return -EREMOTE;
ff51fd70
MH
414
415 return 0;
416}
417
f6c8b711
SW
418/**
419 * batadv_find_router - find a suitable router for this originator
420 * @bat_priv: the bat priv with all the soft interface information
421 * @orig_node: the destination node
422 * @recv_if: pointer to interface this packet was received on
423 *
424 * Returns the router which should be used for this orig_node on
425 * this interface, or NULL if not available.
9cfc7bd6 426 */
56303d34
SE
427struct batadv_neigh_node *
428batadv_find_router(struct batadv_priv *bat_priv,
429 struct batadv_orig_node *orig_node,
430 const struct batadv_hard_iface *recv_if)
c6c8fea2 431{
56303d34 432 struct batadv_neigh_node *router;
c6c8fea2
SE
433
434 if (!orig_node)
435 return NULL;
436
7351a482 437 router = batadv_orig_router_get(orig_node, recv_if);
c6c8fea2 438
f6c8b711
SW
439 /* TODO: fill this later with new bonding mechanism */
440
c6c8fea2
SE
441 return router;
442}
443
63b01037 444static int batadv_route_unicast_packet(struct sk_buff *skb,
56303d34 445 struct batadv_hard_iface *recv_if)
c6c8fea2 446{
56303d34
SE
447 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
448 struct batadv_orig_node *orig_node = NULL;
96412690 449 struct batadv_unicast_packet *unicast_packet;
7ed4be95 450 struct ethhdr *ethhdr = eth_hdr(skb);
c54f38c9 451 int res, hdr_len, ret = NET_RX_DROP;
c6c8fea2 452
96412690 453 unicast_packet = (struct batadv_unicast_packet *)skb->data;
c6c8fea2
SE
454
455 /* TTL exceeded */
a40d9b07 456 if (unicast_packet->ttl < 2) {
86ceb360
SE
457 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
458 ethhdr->h_source, unicast_packet->dest);
44524fcd 459 goto out;
c6c8fea2
SE
460 }
461
462 /* get routing information */
da641193 463 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
7aadf889 464
44524fcd 465 if (!orig_node)
b5a6f69c 466 goto out;
c6c8fea2 467
c6c8fea2 468 /* create a copy of the skb, if needed, to modify it. */
0d125074 469 if (skb_cow(skb, ETH_HLEN) < 0)
44524fcd 470 goto out;
c6c8fea2 471
c6c8fea2 472 /* decrement ttl */
f097e25d 473 unicast_packet = (struct batadv_unicast_packet *)skb->data;
a40d9b07 474 unicast_packet->ttl--;
c6c8fea2 475
a40d9b07 476 switch (unicast_packet->packet_type) {
c54f38c9
SW
477 case BATADV_UNICAST_4ADDR:
478 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
479 break;
480 case BATADV_UNICAST:
481 hdr_len = sizeof(struct batadv_unicast_packet);
482 break;
483 default:
484 /* other packet types not supported - yet */
485 hdr_len = -1;
486 break;
487 }
488
489 if (hdr_len > 0)
490 batadv_skb_set_priority(skb, hdr_len);
491
e91ecfc6 492 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
95332477 493
e91ecfc6
MH
494 /* translate transmit result into receive result */
495 if (res == NET_XMIT_SUCCESS) {
496 /* skb was transmitted and consumed */
95332477
MH
497 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
498 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
499 skb->len + ETH_HLEN);
e91ecfc6
MH
500
501 ret = NET_RX_SUCCESS;
502 } else if (res == NET_XMIT_POLICED) {
503 /* skb was buffered and consumed */
504 ret = NET_RX_SUCCESS;
95332477 505 }
c6c8fea2 506
44524fcd 507out:
44524fcd 508 if (orig_node)
7d211efc 509 batadv_orig_node_free_ref(orig_node);
44524fcd 510 return ret;
c6c8fea2
SE
511}
512
7c1fd91d
AQ
513/**
514 * batadv_reroute_unicast_packet - update the unicast header for re-routing
515 * @bat_priv: the bat priv with all the soft interface information
516 * @unicast_packet: the unicast header to be updated
517 * @dst_addr: the payload destination
c018ad3d 518 * @vid: VLAN identifier
7c1fd91d
AQ
519 *
520 * Search the translation table for dst_addr and update the unicast header with
521 * the new corresponding information (originator address where the destination
522 * client currently is and its known TTVN)
523 *
524 * Returns true if the packet header has been updated, false otherwise
525 */
526static bool
527batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
528 struct batadv_unicast_packet *unicast_packet,
c018ad3d 529 uint8_t *dst_addr, unsigned short vid)
7c1fd91d
AQ
530{
531 struct batadv_orig_node *orig_node = NULL;
532 struct batadv_hard_iface *primary_if = NULL;
533 bool ret = false;
534 uint8_t *orig_addr, orig_ttvn;
535
c018ad3d 536 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
7c1fd91d
AQ
537 primary_if = batadv_primary_if_get_selected(bat_priv);
538 if (!primary_if)
539 goto out;
540 orig_addr = primary_if->net_dev->dev_addr;
541 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
542 } else {
c018ad3d
AQ
543 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
544 vid);
7c1fd91d
AQ
545 if (!orig_node)
546 goto out;
547
548 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
549 goto out;
550
551 orig_addr = orig_node->orig;
552 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
553 }
554
555 /* update the packet header */
556 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
557 unicast_packet->ttvn = orig_ttvn;
558
559 ret = true;
560out:
561 if (primary_if)
562 batadv_hardif_free_ref(primary_if);
563 if (orig_node)
564 batadv_orig_node_free_ref(orig_node);
565
566 return ret;
567}
568
56303d34 569static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
dd981ab0 570 struct sk_buff *skb, int hdr_len) {
c018ad3d
AQ
571 struct batadv_unicast_packet *unicast_packet;
572 struct batadv_hard_iface *primary_if;
56303d34 573 struct batadv_orig_node *orig_node;
c018ad3d 574 uint8_t curr_ttvn, old_ttvn;
a73105b8 575 struct ethhdr *ethhdr;
c018ad3d 576 unsigned short vid;
3e34819e 577 int is_old_ttvn;
a73105b8 578
b8fcfa42 579 /* check if there is enough data before accessing it */
dd981ab0 580 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
b8fcfa42
AQ
581 return 0;
582
583 /* create a copy of the skb (in case of for re-routing) to modify it. */
584 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
a73105b8
AQ
585 return 0;
586
96412690 587 unicast_packet = (struct batadv_unicast_packet *)skb->data;
c018ad3d 588 vid = batadv_get_vid(skb, hdr_len);
dd981ab0 589 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
a73105b8 590
7c1fd91d
AQ
591 /* check if the destination client was served by this node and it is now
592 * roaming. In this case, it means that the node has got a ROAM_ADV
593 * message and that it knows the new destination in the mesh to re-route
594 * the packet to
595 */
c018ad3d 596 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
7c1fd91d 597 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
c018ad3d 598 ethhdr->h_dest, vid))
7c1fd91d
AQ
599 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
600 bat_priv,
601 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
602 unicast_packet->dest,
603 ethhdr->h_dest);
604 /* at this point the mesh destination should have been
605 * substituted with the originator address found in the global
606 * table. If not, let the packet go untouched anyway because
607 * there is nothing the node can do
608 */
609 return 1;
610 }
611
612 /* retrieve the TTVN known by this node for the packet destination. This
613 * value is used later to check if the node which sent (or re-routed
614 * last time) the packet had an updated information or not
615 */
616 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
fe8a93b9 617 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
da641193
SE
618 orig_node = batadv_orig_hash_find(bat_priv,
619 unicast_packet->dest);
7c1fd91d
AQ
620 /* if it is not possible to find the orig_node representing the
621 * destination, the packet can immediately be dropped as it will
622 * not be possible to deliver it
623 */
a73105b8
AQ
624 if (!orig_node)
625 return 0;
626
627 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
7d211efc 628 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
629 }
630
7c1fd91d
AQ
631 /* check if the TTVN contained in the packet is fresher than what the
632 * node knows
633 */
3e34819e 634 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
7c1fd91d
AQ
635 if (!is_old_ttvn)
636 return 1;
a73105b8 637
7c1fd91d
AQ
638 old_ttvn = unicast_packet->ttvn;
639 /* the packet was forged based on outdated network information. Its
640 * destination can possibly be updated and forwarded towards the new
641 * target host
642 */
643 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
c018ad3d 644 ethhdr->h_dest, vid)) {
7c1fd91d
AQ
645 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
646 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
647 unicast_packet->dest, ethhdr->h_dest,
648 old_ttvn, curr_ttvn);
649 return 1;
650 }
3275e7cc 651
7c1fd91d
AQ
652 /* the packet has not been re-routed: either the destination is
653 * currently served by this node or there is no destination at all and
654 * it is possible to drop the packet
655 */
c018ad3d 656 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
7c1fd91d 657 return 0;
3275e7cc 658
7c1fd91d
AQ
659 /* update the header in order to let the packet be delivered to this
660 * node's soft interface
661 */
662 primary_if = batadv_primary_if_get_selected(bat_priv);
663 if (!primary_if)
664 return 0;
a73105b8 665
7c1fd91d
AQ
666 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
667
668 batadv_hardif_free_ref(primary_if);
669
670 unicast_packet->ttvn = curr_ttvn;
a73105b8 671
a73105b8
AQ
672 return 1;
673}
674
a1f1ac5c
SW
675/**
676 * batadv_recv_unhandled_unicast_packet - receive and process packets which
677 * are in the unicast number space but not yet known to the implementation
678 * @skb: unicast tvlv packet to process
679 * @recv_if: pointer to interface this packet was received on
680 *
681 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
682 * otherwise.
683 */
684int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
685 struct batadv_hard_iface *recv_if)
686{
687 struct batadv_unicast_packet *unicast_packet;
688 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
689 int check, hdr_size = sizeof(*unicast_packet);
690
691 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
692 if (check < 0)
693 return NET_RX_DROP;
694
695 /* we don't know about this type, drop it. */
696 unicast_packet = (struct batadv_unicast_packet *)skb->data;
697 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
698 return NET_RX_DROP;
699
700 return batadv_route_unicast_packet(skb, recv_if);
701}
702
56303d34
SE
703int batadv_recv_unicast_packet(struct sk_buff *skb,
704 struct batadv_hard_iface *recv_if)
c6c8fea2 705{
56303d34 706 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
96412690 707 struct batadv_unicast_packet *unicast_packet;
4046b24a 708 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
9affec6b
AQ
709 uint8_t *orig_addr;
710 struct batadv_orig_node *orig_node = NULL;
612d2b4f 711 int check, hdr_size = sizeof(*unicast_packet);
c384ea3e 712 bool is4addr;
c6c8fea2 713
7cdcf6dd 714 unicast_packet = (struct batadv_unicast_packet *)skb->data;
4046b24a 715 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
7cdcf6dd 716
a40d9b07 717 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
7cdcf6dd 718 /* the caller function should have already pulled 2 bytes */
c384ea3e 719 if (is4addr)
4046b24a 720 hdr_size = sizeof(*unicast_4addr_packet);
7cdcf6dd 721
612d2b4f 722 /* function returns -EREMOTE for promiscuous packets */
6e0895c2 723 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
612d2b4f
MH
724
725 /* Even though the packet is not for us, we might save it to use for
726 * decoding a later received coded packet
727 */
728 if (check == -EREMOTE)
729 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
730
731 if (check < 0)
c6c8fea2 732 return NET_RX_DROP;
dd981ab0 733 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
a73105b8
AQ
734 return NET_RX_DROP;
735
c6c8fea2 736 /* packet for me */
fe8a93b9 737 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
9affec6b 738 if (is4addr) {
4046b24a
MH
739 batadv_dat_inc_counter(bat_priv,
740 unicast_4addr_packet->subtype);
9affec6b
AQ
741 orig_addr = unicast_4addr_packet->src;
742 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
743 }
4046b24a 744
c384ea3e
AQ
745 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
746 hdr_size))
747 goto rx_success;
748 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
749 hdr_size))
750 goto rx_success;
751
37135173 752 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
9affec6b 753 orig_node);
37135173 754
c384ea3e 755rx_success:
9affec6b
AQ
756 if (orig_node)
757 batadv_orig_node_free_ref(orig_node);
758
c6c8fea2
SE
759 return NET_RX_SUCCESS;
760 }
761
63b01037 762 return batadv_route_unicast_packet(skb, recv_if);
c6c8fea2
SE
763}
764
ef261577
ML
765/**
766 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
767 * @skb: unicast tvlv packet to process
768 * @recv_if: pointer to interface this packet was received on
769 * @dst_addr: the payload destination
770 *
771 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
772 * otherwise.
773 */
774int batadv_recv_unicast_tvlv(struct sk_buff *skb,
775 struct batadv_hard_iface *recv_if)
776{
777 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
778 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
779 unsigned char *tvlv_buff;
780 uint16_t tvlv_buff_len;
781 int hdr_size = sizeof(*unicast_tvlv_packet);
782 int ret = NET_RX_DROP;
783
784 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
785 return NET_RX_DROP;
786
787 /* the header is likely to be modified while forwarding */
788 if (skb_cow(skb, hdr_size) < 0)
789 return NET_RX_DROP;
790
791 /* packet needs to be linearized to access the tvlv content */
792 if (skb_linearize(skb) < 0)
793 return NET_RX_DROP;
794
795 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
796
797 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
798 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
799
800 if (tvlv_buff_len > skb->len - hdr_size)
801 return NET_RX_DROP;
802
803 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
804 unicast_tvlv_packet->src,
805 unicast_tvlv_packet->dst,
806 tvlv_buff, tvlv_buff_len);
807
808 if (ret != NET_RX_SUCCESS)
809 ret = batadv_route_unicast_packet(skb, recv_if);
810
811 return ret;
812}
c6c8fea2 813
610bfc6b
MH
814/**
815 * batadv_recv_frag_packet - process received fragment
816 * @skb: the received fragment
817 * @recv_if: interface that the skb is received on
818 *
819 * This function does one of the three following things: 1) Forward fragment, if
820 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
821 * lack further fragments; 3) Merge fragments, if we have all needed parts.
822 *
823 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
824 */
825int batadv_recv_frag_packet(struct sk_buff *skb,
826 struct batadv_hard_iface *recv_if)
827{
828 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
829 struct batadv_orig_node *orig_node_src = NULL;
830 struct batadv_frag_packet *frag_packet;
831 int ret = NET_RX_DROP;
832
833 if (batadv_check_unicast_packet(bat_priv, skb,
834 sizeof(*frag_packet)) < 0)
835 goto out;
836
837 frag_packet = (struct batadv_frag_packet *)skb->data;
838 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
839 if (!orig_node_src)
840 goto out;
841
842 /* Route the fragment if it is not for us and too big to be merged. */
843 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
844 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
845 ret = NET_RX_SUCCESS;
846 goto out;
847 }
848
849 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
850 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
851
852 /* Add fragment to buffer and merge if possible. */
853 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
854 goto out;
855
856 /* Deliver merged packet to the appropriate handler, if it was
857 * merged
858 */
859 if (skb)
860 batadv_batman_skb_recv(skb, recv_if->net_dev,
861 &recv_if->batman_adv_ptype, NULL);
862
863 ret = NET_RX_SUCCESS;
864
865out:
866 if (orig_node_src)
867 batadv_orig_node_free_ref(orig_node_src);
868
869 return ret;
870}
871
56303d34
SE
872int batadv_recv_bcast_packet(struct sk_buff *skb,
873 struct batadv_hard_iface *recv_if)
c6c8fea2 874{
56303d34
SE
875 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
876 struct batadv_orig_node *orig_node = NULL;
96412690 877 struct batadv_bcast_packet *bcast_packet;
c6c8fea2 878 struct ethhdr *ethhdr;
704509b8 879 int hdr_size = sizeof(*bcast_packet);
f3e0008f 880 int ret = NET_RX_DROP;
c6c8fea2 881 int32_t seq_diff;
3fba7325 882 uint32_t seqno;
c6c8fea2
SE
883
884 /* drop packet if it has not necessary minimum size */
885 if (unlikely(!pskb_may_pull(skb, hdr_size)))
f3e0008f 886 goto out;
c6c8fea2 887
7ed4be95 888 ethhdr = eth_hdr(skb);
c6c8fea2
SE
889
890 /* packet with broadcast indication but unicast recipient */
891 if (!is_broadcast_ether_addr(ethhdr->h_dest))
f3e0008f 892 goto out;
c6c8fea2
SE
893
894 /* packet with broadcast sender address */
895 if (is_broadcast_ether_addr(ethhdr->h_source))
f3e0008f 896 goto out;
c6c8fea2
SE
897
898 /* ignore broadcasts sent by myself */
fe8a93b9 899 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
f3e0008f 900 goto out;
c6c8fea2 901
96412690 902 bcast_packet = (struct batadv_bcast_packet *)skb->data;
c6c8fea2
SE
903
904 /* ignore broadcasts originated by myself */
fe8a93b9 905 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
f3e0008f 906 goto out;
c6c8fea2 907
a40d9b07 908 if (bcast_packet->ttl < 2)
f3e0008f 909 goto out;
c6c8fea2 910
da641193 911 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
f3e0008f
ML
912
913 if (!orig_node)
b5a6f69c 914 goto out;
c6c8fea2 915
f3e0008f 916 spin_lock_bh(&orig_node->bcast_seqno_lock);
c6c8fea2 917
3fba7325 918 seqno = ntohl(bcast_packet->seqno);
c6c8fea2 919 /* check whether the packet is a duplicate */
9b4a1159 920 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
3fba7325 921 seqno))
f3e0008f 922 goto spin_unlock;
c6c8fea2 923
3fba7325 924 seq_diff = seqno - orig_node->last_bcast_seqno;
c6c8fea2
SE
925
926 /* check whether the packet is old and the host just restarted. */
30d3c511
SE
927 if (batadv_window_protected(bat_priv, seq_diff,
928 &orig_node->bcast_seqno_reset))
f3e0008f 929 goto spin_unlock;
c6c8fea2
SE
930
931 /* mark broadcast in flood history, update window position
9cfc7bd6
SE
932 * if required.
933 */
0f5f9322 934 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
3fba7325 935 orig_node->last_bcast_seqno = seqno;
c6c8fea2 936
f3e0008f 937 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f 938
fe2da6ff 939 /* check whether this has been sent by another originator before */
004e86fc 940 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
fe2da6ff
SW
941 goto out;
942
c54f38c9
SW
943 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
944
c6c8fea2 945 /* rebroadcast packet */
9455e34c 946 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
c6c8fea2 947
23721387
SW
948 /* don't hand the broadcast up if it is from an originator
949 * from the same backbone.
950 */
08adf151 951 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
23721387
SW
952 goto out;
953
c384ea3e
AQ
954 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
955 goto rx_success;
956 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
957 goto rx_success;
958
c6c8fea2 959 /* broadcast for me */
37135173
AQ
960 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
961 orig_node);
c384ea3e
AQ
962
963rx_success:
f3e0008f
ML
964 ret = NET_RX_SUCCESS;
965 goto out;
c6c8fea2 966
f3e0008f
ML
967spin_unlock:
968 spin_unlock_bh(&orig_node->bcast_seqno_lock);
f3e0008f
ML
969out:
970 if (orig_node)
7d211efc 971 batadv_orig_node_free_ref(orig_node);
f3e0008f 972 return ret;
c6c8fea2 973}