File Index
Files and directories
Sign in
FILE

routing.c

File preview with syntax highlighting.

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