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