blob: 40f5cffde6a3faf41a91138842741c692d3ce733 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "routing.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#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>
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +010017#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020018#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>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010025#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026
27#include "bitarray.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include "bridge_loop_avoidance.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020029#include "distributed-arp-table.h"
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +020030#include "fragmentation.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020032#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include "network-coding.h"
34#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include "send.h"
36#include "soft-interface.h"
Antonio Quartulli33a3bb42016-05-05 13:09:43 +020037#include "tp_meter.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020038#include "translation-table.h"
Markus Pargmann1f8dce42016-05-15 11:07:43 +020039#include "tvlv.h"
Antonio Quartullic018ad32013-06-04 12:11:39 +020040
Sven Eckelmann63b01032012-05-16 20:23:13 +020041static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +020042 struct batadv_hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010043
Simon Wunderlich7351a4822013-11-13 19:14:47 +010044/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010045 * _batadv_update_route() - set the router for this originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +010046 * @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 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020053static void _batadv_update_route(struct batadv_priv *bat_priv,
54 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +010055 struct batadv_hard_iface *recv_if,
Sven Eckelmann56303d32012-06-05 22:31:31 +020056 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057{
Simon Wunderlich7351a4822013-11-13 19:14:47 +010058 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann56303d32012-06-05 22:31:31 +020059 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000060
Simon Wunderlich7351a4822013-11-13 19:14:47 +010061 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
62 if (!orig_ifinfo)
63 return;
64
Sven Eckelmannb5dcbad2016-06-29 23:45:57 +020065 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 */
Sven Eckelmannb5dcbad2016-06-29 23:45:57 +020073
74 /* increase refcount of new best neighbor */
75 if (neigh_node)
76 kref_get(&neigh_node->refcount);
77
Antonio Quartullicf78bb02020-05-20 10:41:40 +020078 curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
79 true);
Sven Eckelmannb5dcbad2016-06-29 23:45:57 +020080 spin_unlock_bh(&orig_node->neigh_list_lock);
81 batadv_orig_ifinfo_put(orig_ifinfo);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000082
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083 /* route deleted */
Sven Eckelmann825ffe12017-08-23 21:52:13 +020084 if (curr_router && !neigh_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020085 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
86 "Deleting route towards: %pM\n", orig_node->orig);
Antonio Quartulli95fb1302013-08-07 18:28:55 +020087 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +020088 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000089
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000090 /* route added */
Sven Eckelmann825ffe12017-08-23 21:52:13 +020091 } else if (!curr_router && neigh_node) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020092 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020093 "Adding route towards: %pM (via %pM)\n",
94 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000095 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020096 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020097 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020098 "Changing route towards: %pM (now via %pM - was via %pM)\n",
99 orig_node->orig, neigh_node->addr,
100 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101 }
102
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000103 /* decrease refcount of previous best neighbor */
104 if (curr_router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100105 batadv_neigh_node_put(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106}
107
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100108/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100109 * batadv_update_route() - set the router for this originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100110 * @bat_priv: the bat priv with all the soft interface information
111 * @orig_node: orig node which is to be configured
112 * @recv_if: the receive interface for which this route is set
113 * @neigh_node: neighbor which should be the next router
114 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115void batadv_update_route(struct batadv_priv *bat_priv,
116 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100117 struct batadv_hard_iface *recv_if,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200118 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000119{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200120 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121
122 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000123 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100125 router = batadv_orig_router_get(orig_node, recv_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000126
127 if (router != neigh_node)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100128 _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000129
130out:
131 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100132 batadv_neigh_node_put(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133}
134
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200135/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100136 * batadv_window_protected() - checks whether the host restarted and is in the
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200137 * protection time.
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +0100138 * @bat_priv: the bat priv with all the soft interface information
139 * @seq_num_diff: difference between the current/received sequence number and
140 * the last sequence number
Simon Wunderlich81f02682015-11-23 19:57:22 +0100141 * @seq_old_max_diff: maximum age of sequence number not considered as restart
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +0100142 * @last_reset: jiffies timestamp of the last reset, will be updated when reset
143 * is detected
Simon Wunderlich81f02682015-11-23 19:57:22 +0100144 * @protection_started: is set to true if the protection window was started,
145 * doesn't change otherwise.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200146 *
147 * Return:
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100148 * false if the packet is to be accepted.
149 * true if the packet is to be ignored.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100151bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
152 s32 seq_old_max_diff, unsigned long *last_reset,
153 bool *protection_started)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154{
Simon Wunderlich81f02682015-11-23 19:57:22 +0100155 if (seq_num_diff <= -seq_old_max_diff ||
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200156 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
157 if (!batadv_has_timed_out(*last_reset,
158 BATADV_RESET_PROTECTION_MS))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100159 return true;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800160
161 *last_reset = jiffies;
Simon Wunderlich81f02682015-11-23 19:57:22 +0100162 if (protection_started)
163 *protection_started = true;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200164 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200165 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800167
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100168 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169}
170
Sven Eckelmannff15c272017-12-02 19:51:53 +0100171/**
172 * batadv_check_management_packet() - Check preconditions for management packets
173 * @skb: incoming packet buffer
174 * @hard_iface: incoming hard interface
175 * @header_len: minimal header length of packet type
176 *
177 * Return: true when management preconditions are met, false otherwise
178 */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200179bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200180 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200181 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000182{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183 struct ethhdr *ethhdr;
184
185 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800186 if (unlikely(!pskb_may_pull(skb, header_len)))
187 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200189 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190
191 /* packet with broadcast indication but unicast recipient */
192 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800193 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Sven Eckelmann92eef522016-08-06 17:04:22 +0200195 /* packet with invalid sender address */
196 if (!is_valid_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800197 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198
199 /* create a copy of the skb, if needed, to modify it. */
200 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800201 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000202
203 /* keep skb linear */
204 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800205 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Marek Lindnerc3e29312012-03-04 16:56:25 +0800207 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208}
209
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200210/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100211 * batadv_recv_my_icmp_packet() - receive an icmp packet locally
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200212 * @bat_priv: the bat priv with all the soft interface information
213 * @skb: icmp packet to process
214 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200215 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200216 * otherwise.
217 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200218static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200219 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221 struct batadv_hard_iface *primary_if = NULL;
222 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200223 struct batadv_icmp_header *icmph;
224 int res, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200226 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200228 switch (icmph->msg_type) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200229 case BATADV_ECHO_REQUEST:
230 /* answer echo request (ping) */
231 primary_if = batadv_primary_if_get_selected(bat_priv);
232 if (!primary_if)
233 goto out;
234
235 /* get routing information */
236 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
237 if (!orig_node)
238 goto out;
239
240 /* create a copy of the skb, if needed, to modify it. */
241 if (skb_cow(skb, ETH_HLEN) < 0)
242 goto out;
243
244 icmph = (struct batadv_icmp_header *)skb->data;
245
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100246 ether_addr_copy(icmph->dst, icmph->orig);
247 ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200248 icmph->msg_type = BATADV_ECHO_REPLY;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100249 icmph->ttl = BATADV_TTL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200250
251 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200252 if (res == NET_XMIT_SUCCESS)
253 ret = NET_RX_SUCCESS;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200254
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200255 /* skb was consumed */
256 skb = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200257 break;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200258 case BATADV_TP:
259 if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
260 goto out;
261
262 batadv_tp_meter_recv(bat_priv, skb);
263 ret = NET_RX_SUCCESS;
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200264 /* skb was consumed */
265 skb = NULL;
Antonio Quartulli33a3bb42016-05-05 13:09:43 +0200266 goto out;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200267 default:
268 /* drop unknown type */
Marek Lindner44524fc2011-02-10 14:33:53 +0000269 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 }
Marek Lindner44524fc2011-02-10 14:33:53 +0000271out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200272 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100273 batadv_hardif_put(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000274 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100275 batadv_orig_node_put(orig_node);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200276
277 kfree_skb(skb);
278
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 return ret;
280}
281
Sven Eckelmann56303d32012-06-05 22:31:31 +0200282static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200283 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200285 struct batadv_hard_iface *primary_if = NULL;
286 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200287 struct batadv_icmp_packet *icmp_packet;
Antonio Quartullif50ca952016-05-18 11:38:48 +0200288 int res, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
Sven Eckelmann96412692012-06-05 22:31:30 +0200290 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291
292 /* send TTL exceeded if packet is an echo request (traceroute) */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100293 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100294 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100295 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000296 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 }
298
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200299 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200300 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000301 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302
303 /* get routing information */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100304 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000305 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000306 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000307
Marek Lindner44524fc2011-02-10 14:33:53 +0000308 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100309 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000310 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Sven Eckelmann96412692012-06-05 22:31:30 +0200312 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100314 ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
315 ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100316 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
317 icmp_packet->ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
Antonio Quartullif50ca952016-05-18 11:38:48 +0200319 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200320 if (res == NET_RX_SUCCESS)
321 ret = NET_XMIT_SUCCESS;
322
323 /* skb was consumed */
324 skb = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Marek Lindner44524fc2011-02-10 14:33:53 +0000326out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200327 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100328 batadv_hardif_put(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000329 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100330 batadv_orig_node_put(orig_node);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200331
332 kfree_skb(skb);
333
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334 return ret;
335}
336
Sven Eckelmannff15c272017-12-02 19:51:53 +0100337/**
338 * batadv_recv_icmp_packet() - Process incoming icmp packet
339 * @skb: incoming packet buffer
340 * @recv_if: incoming hard interface
341 *
342 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
343 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200344int batadv_recv_icmp_packet(struct sk_buff *skb,
345 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200348 struct batadv_icmp_header *icmph;
349 struct batadv_icmp_packet_rr *icmp_packet_rr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200351 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200352 int hdr_size = sizeof(struct batadv_icmp_header);
Antonio Quartullif50ca952016-05-18 11:38:48 +0200353 int res, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000354
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 /* drop packet if it has not necessary minimum size */
356 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200357 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200359 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
Sven Eckelmann93bbaab2016-08-06 17:04:23 +0200361 /* packet with unicast indication but non-unicast recipient */
362 if (!is_valid_ether_addr(ethhdr->h_dest))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200363 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Sven Eckelmann9f75c8e2016-08-06 17:04:21 +0200365 /* packet with broadcast/multicast sender address */
366 if (is_multicast_ether_addr(ethhdr->h_source))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200367 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368
369 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200370 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200371 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200373 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374
375 /* add record route information if not full */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200376 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
377 icmph->msg_type == BATADV_ECHO_REQUEST) &&
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200378 skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200379 if (skb_linearize(skb) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200380 goto free_skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200381
382 /* create a copy of the skb, if needed, to modify it. */
383 if (skb_cow(skb, ETH_HLEN) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200384 goto free_skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200385
Sven Eckelmann3b55e442016-06-26 11:16:12 +0200386 ethhdr = eth_hdr(skb);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200387 icmph = (struct batadv_icmp_header *)skb->data;
388 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
389 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200390 goto free_skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200391
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100392 ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
393 ethhdr->h_dest);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200394 icmp_packet_rr->rr_cur++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395 }
396
397 /* packet for me */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200398 if (batadv_is_my_mac(bat_priv, icmph->dst))
399 return batadv_recv_my_icmp_packet(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
401 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100402 if (icmph->ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200403 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 /* get routing information */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200406 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000407 if (!orig_node)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200408 goto free_skb;
Marek Lindner44524fc2011-02-10 14:33:53 +0000409
Marek Lindner44524fc2011-02-10 14:33:53 +0000410 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100411 if (skb_cow(skb, ETH_HLEN) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200412 goto put_orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200414 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindner44524fc2011-02-10 14:33:53 +0000416 /* decrement ttl */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100417 icmph->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Marek Lindner44524fc2011-02-10 14:33:53 +0000419 /* route it */
Antonio Quartullif50ca952016-05-18 11:38:48 +0200420 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200421 if (res == NET_XMIT_SUCCESS)
422 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200424 /* skb was consumed */
425 skb = NULL;
426
427put_orig_node:
Marek Lindner44524fc2011-02-10 14:33:53 +0000428 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100429 batadv_orig_node_put(orig_node);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200430free_skb:
431 kfree_skb(skb);
432
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 return ret;
434}
435
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100436/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100437 * batadv_check_unicast_packet() - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400438 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100439 * @skb: packet to check
440 * @hdr_size: size of header to pull
441 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200442 * Checks for short header and bad addresses in the given packet.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200443 *
444 * Return: negative value when check fails and 0 otherwise. The negative value
445 * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
446 * destination or source, and -EREMOTE for non-local (other host) destination.
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100447 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200448static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
449 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200450{
451 struct ethhdr *ethhdr;
452
453 /* drop packet if it has not necessary minimum size */
454 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100455 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200456
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200457 ethhdr = eth_hdr(skb);
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200458
Sven Eckelmann93bbaab2016-08-06 17:04:23 +0200459 /* packet with unicast indication but non-unicast recipient */
460 if (!is_valid_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100461 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200462
Sven Eckelmann9f75c8e2016-08-06 17:04:21 +0200463 /* packet with broadcast/multicast sender address */
464 if (is_multicast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100465 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200466
467 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200468 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100469 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200470
471 return 0;
472}
473
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100474/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100475 * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
Sven Eckelmann93652342016-08-06 15:50:52 +0200476 * @orig_node: originator node whose last bonding candidate should be retrieved
477 *
478 * Return: last bonding candidate of router or NULL if not found
479 *
480 * The object is returned with refcounter increased by 1.
481 */
482static struct batadv_orig_ifinfo *
483batadv_last_bonding_get(struct batadv_orig_node *orig_node)
484{
485 struct batadv_orig_ifinfo *last_bonding_candidate;
486
487 spin_lock_bh(&orig_node->neigh_list_lock);
488 last_bonding_candidate = orig_node->last_bonding_candidate;
489
490 if (last_bonding_candidate)
491 kref_get(&last_bonding_candidate->refcount);
492 spin_unlock_bh(&orig_node->neigh_list_lock);
493
494 return last_bonding_candidate;
495}
496
497/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100498 * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200499 * @orig_node: originator node whose bonding candidates should be replaced
500 * @new_candidate: new bonding candidate or NULL
501 */
502static void
503batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
504 struct batadv_orig_ifinfo *new_candidate)
505{
506 struct batadv_orig_ifinfo *old_candidate;
507
508 spin_lock_bh(&orig_node->neigh_list_lock);
509 old_candidate = orig_node->last_bonding_candidate;
510
511 if (new_candidate)
512 kref_get(&new_candidate->refcount);
513 orig_node->last_bonding_candidate = new_candidate;
514 spin_unlock_bh(&orig_node->neigh_list_lock);
515
516 if (old_candidate)
517 batadv_orig_ifinfo_put(old_candidate);
518}
519
520/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100521 * batadv_find_router() - find a suitable router for this originator
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100522 * @bat_priv: the bat priv with all the soft interface information
523 * @orig_node: the destination node
524 * @recv_if: pointer to interface this packet was received on
525 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200526 * Return: the router which should be used for this orig_node on
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100527 * this interface, or NULL if not available.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200528 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200529struct batadv_neigh_node *
530batadv_find_router(struct batadv_priv *bat_priv,
531 struct batadv_orig_node *orig_node,
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100532 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533{
Antonio Quartulli29824a52016-05-25 23:27:31 +0800534 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100535 struct batadv_neigh_node *first_candidate_router = NULL;
536 struct batadv_neigh_node *next_candidate_router = NULL;
537 struct batadv_neigh_node *router, *cand_router = NULL;
538 struct batadv_neigh_node *last_cand_router = NULL;
539 struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
540 struct batadv_orig_ifinfo *next_candidate = NULL;
541 struct batadv_orig_ifinfo *last_candidate;
542 bool last_candidate_found = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543
544 if (!orig_node)
545 return NULL;
546
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100547 router = batadv_orig_router_get(orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548
Simon Wunderlich329887a2014-08-13 14:26:56 +0200549 if (!router)
550 return router;
551
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100552 /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
553 * and if activated.
554 */
Simon Wunderlich329887a2014-08-13 14:26:56 +0200555 if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100556 return router;
557
558 /* bonding: loop through the list of possible routers found
559 * for the various outgoing interfaces and find a candidate after
560 * the last chosen bonding candidate (next_candidate). If no such
561 * router is found, use the first candidate found (the previously
562 * chosen bonding candidate might have been the last one in the list).
Antonio Quartulli3f687852014-11-02 11:29:56 +0100563 * If this can't be found either, return the previously chosen
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100564 * router - obviously there are no other candidates.
565 */
566 rcu_read_lock();
Sven Eckelmann93652342016-08-06 15:50:52 +0200567 last_candidate = batadv_last_bonding_get(orig_node);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100568 if (last_candidate)
569 last_cand_router = rcu_dereference(last_candidate->router);
570
571 hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
572 /* acquire some structures and references ... */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100573 if (!kref_get_unless_zero(&cand->refcount))
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100574 continue;
575
576 cand_router = rcu_dereference(cand->router);
577 if (!cand_router)
578 goto next;
579
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100580 if (!kref_get_unless_zero(&cand_router->refcount)) {
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100581 cand_router = NULL;
582 goto next;
583 }
584
585 /* alternative candidate should be good enough to be
586 * considered
587 */
Antonio Quartulli29824a52016-05-25 23:27:31 +0800588 if (!bao->neigh.is_similar_or_better(cand_router,
589 cand->if_outgoing, router,
590 recv_if))
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100591 goto next;
592
593 /* don't use the same router twice */
594 if (last_cand_router == cand_router)
595 goto next;
596
597 /* mark the first possible candidate */
598 if (!first_candidate) {
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100599 kref_get(&cand_router->refcount);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100600 kref_get(&cand->refcount);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100601 first_candidate = cand;
602 first_candidate_router = cand_router;
603 }
604
605 /* check if the loop has already passed the previously selected
606 * candidate ... this function should select the next candidate
607 * AFTER the previously used bonding candidate.
608 */
609 if (!last_candidate || last_candidate_found) {
610 next_candidate = cand;
611 next_candidate_router = cand_router;
612 break;
613 }
614
615 if (last_candidate == cand)
616 last_candidate_found = true;
617next:
618 /* free references */
619 if (cand_router) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100620 batadv_neigh_node_put(cand_router);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100621 cand_router = NULL;
622 }
Sven Eckelmann35f94772016-01-17 11:01:13 +0100623 batadv_orig_ifinfo_put(cand);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100624 }
625 rcu_read_unlock();
626
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100627 /* After finding candidates, handle the three cases:
628 * 1) there is a next candidate, use that
629 * 2) there is no next candidate, use the first of the list
630 * 3) there is no candidate at all, return the default router
631 */
632 if (next_candidate) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100633 batadv_neigh_node_put(router);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100634
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200635 kref_get(&next_candidate_router->refcount);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100636 router = next_candidate_router;
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200637 batadv_last_bonding_replace(orig_node, next_candidate);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100638 } else if (first_candidate) {
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100639 batadv_neigh_node_put(router);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100640
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200641 kref_get(&first_candidate_router->refcount);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100642 router = first_candidate_router;
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200643 batadv_last_bonding_replace(orig_node, first_candidate);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100644 } else {
Sven Eckelmann15c2ed752016-06-30 20:11:34 +0200645 batadv_last_bonding_replace(orig_node, NULL);
646 }
647
648 /* cleanup of candidates */
649 if (first_candidate) {
650 batadv_neigh_node_put(first_candidate_router);
651 batadv_orig_ifinfo_put(first_candidate);
652 }
653
654 if (next_candidate) {
655 batadv_neigh_node_put(next_candidate_router);
656 batadv_orig_ifinfo_put(next_candidate);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100657 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Sven Eckelmann93652342016-08-06 15:50:52 +0200659 if (last_candidate)
660 batadv_orig_ifinfo_put(last_candidate);
661
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 return router;
663}
664
Sven Eckelmann63b01032012-05-16 20:23:13 +0200665static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200666 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200668 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
669 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200670 struct batadv_unicast_packet *unicast_packet;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200671 struct ethhdr *ethhdr = eth_hdr(skb);
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200672 int res, hdr_len, ret = NET_RX_DROP;
Florian Westphal63d443e2016-05-10 23:17:59 +0200673 unsigned int len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000674
Sven Eckelmann96412692012-06-05 22:31:30 +0200675 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
677 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100678 if (unicast_packet->ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100679 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
680 ethhdr->h_source, unicast_packet->dest);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200681 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682 }
683
684 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200685 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000686
Marek Lindner44524fc2011-02-10 14:33:53 +0000687 if (!orig_node)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200688 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100691 if (skb_cow(skb, ETH_HLEN) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200692 goto put_orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000693
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694 /* decrement ttl */
Martin Hundebøllf097e252013-05-23 16:53:01 +0200695 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100696 unicast_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100698 switch (unicast_packet->packet_type) {
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200699 case BATADV_UNICAST_4ADDR:
700 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
701 break;
702 case BATADV_UNICAST:
703 hdr_len = sizeof(struct batadv_unicast_packet);
704 break;
705 default:
706 /* other packet types not supported - yet */
707 hdr_len = -1;
708 break;
709 }
710
711 if (hdr_len > 0)
712 batadv_skb_set_priority(skb, hdr_len);
713
Florian Westphal63d443e2016-05-10 23:17:59 +0200714 len = skb->len;
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200715 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Martin Hundebøll95332472013-01-25 11:12:40 +0100716
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200717 /* translate transmit result into receive result */
718 if (res == NET_XMIT_SUCCESS) {
Gao Feng0843f192016-11-21 23:01:09 +0800719 ret = NET_RX_SUCCESS;
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200720 /* skb was transmitted and consumed */
Martin Hundebøll95332472013-01-25 11:12:40 +0100721 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
722 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
Florian Westphal63d443e2016-05-10 23:17:59 +0200723 len + ETH_HLEN);
Martin Hundebøll95332472013-01-25 11:12:40 +0100724 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725
Gao Feng0843f192016-11-21 23:01:09 +0800726 /* skb was consumed */
727 skb = NULL;
728
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200729put_orig_node:
730 batadv_orig_node_put(orig_node);
731free_skb:
732 kfree_skb(skb);
Antonio Quartullif50ca952016-05-18 11:38:48 +0200733
Marek Lindner44524fc2011-02-10 14:33:53 +0000734 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735}
736
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200737/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100738 * batadv_reroute_unicast_packet() - update the unicast header for re-routing
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200739 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100740 * @skb: unicast packet to process
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200741 * @unicast_packet: the unicast header to be updated
742 * @dst_addr: the payload destination
Antonio Quartullic018ad32013-06-04 12:11:39 +0200743 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200744 *
745 * Search the translation table for dst_addr and update the unicast header with
746 * the new corresponding information (originator address where the destination
747 * client currently is and its known TTVN)
748 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200749 * Return: true if the packet header has been updated, false otherwise
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200750 */
751static bool
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100752batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200753 struct batadv_unicast_packet *unicast_packet,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200754 u8 *dst_addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200755{
756 struct batadv_orig_node *orig_node = NULL;
757 struct batadv_hard_iface *primary_if = NULL;
758 bool ret = false;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200759 u8 *orig_addr, orig_ttvn;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200760
Antonio Quartullic018ad32013-06-04 12:11:39 +0200761 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200762 primary_if = batadv_primary_if_get_selected(bat_priv);
763 if (!primary_if)
764 goto out;
765 orig_addr = primary_if->net_dev->dev_addr;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200766 orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200767 } else {
Antonio Quartullic018ad32013-06-04 12:11:39 +0200768 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
769 vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200770 if (!orig_node)
771 goto out;
772
773 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
774 goto out;
775
776 orig_addr = orig_node->orig;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200777 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200778 }
779
780 /* update the packet header */
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100781 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100782 ether_addr_copy(unicast_packet->dest, orig_addr);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200783 unicast_packet->ttvn = orig_ttvn;
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100784 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200785
786 ret = true;
787out:
788 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100789 batadv_hardif_put(primary_if);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200790 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100791 batadv_orig_node_put(orig_node);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200792
793 return ret;
794}
795
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100796static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
797 struct sk_buff *skb, int hdr_len)
798{
Sven Eckelmann96412692012-06-05 22:31:30 +0200799 struct batadv_unicast_packet *unicast_packet;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200800 struct batadv_hard_iface *primary_if;
801 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200802 u8 curr_ttvn, old_ttvn;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200803 struct ethhdr *ethhdr;
804 unsigned short vid;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200805 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200806
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200807 /* check if there is enough data before accessing it */
Antonio Quartullif1791422014-01-30 00:12:24 +0100808 if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100809 return false;
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200810
811 /* create a copy of the skb (in case of for re-routing) to modify it. */
812 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100813 return false;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200814
Sven Eckelmann96412692012-06-05 22:31:30 +0200815 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200816 vid = batadv_get_vid(skb, hdr_len);
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200817 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200818
Linus Lüssing7dda5b32020-09-04 20:28:00 +0200819 /* do not reroute multicast frames in a unicast header */
820 if (is_multicast_ether_addr(ethhdr->h_dest))
821 return true;
822
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200823 /* check if the destination client was served by this node and it is now
824 * roaming. In this case, it means that the node has got a ROAM_ADV
825 * message and that it knows the new destination in the mesh to re-route
826 * the packet to
827 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200828 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100829 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200830 ethhdr->h_dest, vid))
André Gaul23c4ec12014-06-10 17:50:31 +0200831 batadv_dbg_ratelimited(BATADV_DBG_TT,
832 bat_priv,
833 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
834 unicast_packet->dest,
835 ethhdr->h_dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200836 /* at this point the mesh destination should have been
837 * substituted with the originator address found in the global
838 * table. If not, let the packet go untouched anyway because
839 * there is nothing the node can do
840 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100841 return true;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200842 }
843
844 /* retrieve the TTVN known by this node for the packet destination. This
845 * value is used later to check if the node which sent (or re-routed
846 * last time) the packet had an updated information or not
847 */
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200848 curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200849 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200850 orig_node = batadv_orig_hash_find(bat_priv,
851 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200852 /* if it is not possible to find the orig_node representing the
853 * destination, the packet can immediately be dropped as it will
854 * not be possible to deliver it
855 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200856 if (!orig_node)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100857 return false;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200858
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200859 curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100860 batadv_orig_node_put(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200861 }
862
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200863 /* check if the TTVN contained in the packet is fresher than what the
864 * node knows
865 */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200866 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200867 if (!is_old_ttvn)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100868 return true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200869
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200870 old_ttvn = unicast_packet->ttvn;
871 /* the packet was forged based on outdated network information. Its
872 * destination can possibly be updated and forwarded towards the new
873 * target host
874 */
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100875 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200876 ethhdr->h_dest, vid)) {
André Gaul23c4ec12014-06-10 17:50:31 +0200877 batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
878 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
879 unicast_packet->dest, ethhdr->h_dest,
880 old_ttvn, curr_ttvn);
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100881 return true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200882 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200883
884 /* the packet has not been re-routed: either the destination is
885 * currently served by this node or there is no destination at all and
886 * it is possible to drop the packet
887 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200888 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100889 return false;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200890
891 /* update the header in order to let the packet be delivered to this
892 * node's soft interface
893 */
894 primary_if = batadv_primary_if_get_selected(bat_priv);
895 if (!primary_if)
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100896 return false;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200897
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100898 /* update the packet header */
899 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100900 ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
Sven Eckelmannfc04fdb2018-03-16 21:14:32 +0100901 unicast_packet->ttvn = curr_ttvn;
902 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200903
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100904 batadv_hardif_put(primary_if);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200905
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100906 return true;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907}
908
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200909/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100910 * batadv_recv_unhandled_unicast_packet() - receive and process packets which
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200911 * are in the unicast number space but not yet known to the implementation
912 * @skb: unicast tvlv packet to process
913 * @recv_if: pointer to interface this packet was received on
914 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200915 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200916 * otherwise.
917 */
918int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
919 struct batadv_hard_iface *recv_if)
920{
921 struct batadv_unicast_packet *unicast_packet;
922 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
923 int check, hdr_size = sizeof(*unicast_packet);
924
925 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
926 if (check < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200927 goto free_skb;
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200928
929 /* we don't know about this type, drop it. */
930 unicast_packet = (struct batadv_unicast_packet *)skb->data;
931 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200932 goto free_skb;
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200933
934 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200935
936free_skb:
937 kfree_skb(skb);
938 return NET_RX_DROP;
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200939}
940
Sven Eckelmannff15c272017-12-02 19:51:53 +0100941/**
942 * batadv_recv_unicast_packet() - Process incoming unicast packet
943 * @skb: incoming packet buffer
944 * @recv_if: incoming hard interface
945 *
946 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
947 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200948int batadv_recv_unicast_packet(struct sk_buff *skb,
949 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200951 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200952 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200953 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Andreas Pape9e794b62016-09-05 13:20:27 +0200954 u8 *orig_addr, *orig_addr_gw;
955 struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100956 int check, hdr_size = sizeof(*unicast_packet);
Simon Wunderlich437bb0e2015-09-02 20:09:54 +0200957 enum batadv_subtype subtype;
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200958 int ret = NET_RX_DROP;
Andreas Pape9e794b62016-09-05 13:20:27 +0200959 bool is4addr, is_gw;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000960
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200961 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100962 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200963 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200964 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +0200965 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200966
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100967 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -0400968 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100969
970 /* Even though the packet is not for us, we might save it to use for
971 * decoding a later received coded packet
972 */
973 if (check == -EREMOTE)
974 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
975
976 if (check < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200977 goto free_skb;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200978 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
Sven Eckelmannb91a2542016-07-17 21:04:04 +0200979 goto free_skb;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200980
Matthias Schifferbc44b782018-03-16 11:29:09 +0100981 unicast_packet = (struct batadv_unicast_packet *)skb->data;
982
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000983 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200984 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Andreas Pape9e794b62016-09-05 13:20:27 +0200985 /* If this is a unicast packet from another backgone gw,
986 * drop it.
987 */
Matthias Schifferbc44b782018-03-16 11:29:09 +0100988 orig_addr_gw = eth_hdr(skb)->h_source;
Andreas Pape9e794b62016-09-05 13:20:27 +0200989 orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
990 if (orig_node_gw) {
991 is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
992 hdr_size);
993 batadv_orig_node_put(orig_node_gw);
994 if (is_gw) {
995 batadv_dbg(BATADV_DBG_BLA, bat_priv,
Sven Eckelmann22f05022017-05-19 13:02:00 +0200996 "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
997 __func__, orig_addr_gw);
Andreas Papea1a745e2017-05-19 10:01:42 +0200998 goto free_skb;
Andreas Pape9e794b62016-09-05 13:20:27 +0200999 }
1000 }
1001
Antonio Quartulli9affec62012-10-14 17:19:19 +02001002 if (is4addr) {
Matthias Schifferbc44b782018-03-16 11:29:09 +01001003 unicast_4addr_packet =
1004 (struct batadv_unicast_4addr_packet *)skb->data;
Simon Wunderlich437bb0e2015-09-02 20:09:54 +02001005 subtype = unicast_4addr_packet->subtype;
1006 batadv_dat_inc_counter(bat_priv, subtype);
1007
1008 /* Only payload data should be considered for speedy
1009 * join. For example, DAT also uses unicast 4addr
1010 * types, but those packets should not be considered
1011 * for speedy join, since the clients do not actually
1012 * reside at the sending originator.
1013 */
1014 if (subtype == BATADV_P_DATA) {
1015 orig_addr = unicast_4addr_packet->src;
1016 orig_node = batadv_orig_hash_find(bat_priv,
1017 orig_addr);
1018 }
Antonio Quartulli9affec62012-10-14 17:19:19 +02001019 }
Martin Hundebøll4046b242012-04-20 17:02:45 +02001020
Antonio Quartullic384ea32011-06-26 03:37:18 +02001021 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1022 hdr_size))
1023 goto rx_success;
1024 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1025 hdr_size))
1026 goto rx_success;
1027
Linus Lüssingb61ec312018-12-30 16:52:53 +01001028 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1029
Sven Eckelmann6535db52016-02-28 11:38:51 +01001030 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +02001031 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +02001032
Antonio Quartullic384ea32011-06-26 03:37:18 +02001033rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +02001034 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01001035 batadv_orig_node_put(orig_node);
Antonio Quartulli9affec62012-10-14 17:19:19 +02001036
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037 return NET_RX_SUCCESS;
1038 }
1039
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001040 ret = batadv_route_unicast_packet(skb, recv_if);
1041 /* skb was consumed */
1042 skb = NULL;
1043
1044free_skb:
1045 kfree_skb(skb);
1046
1047 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001048}
1049
Marek Lindneref261572013-04-23 21:39:57 +08001050/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001051 * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
Marek Lindneref261572013-04-23 21:39:57 +08001052 * @skb: unicast tvlv packet to process
1053 * @recv_if: pointer to interface this packet was received on
Marek Lindneref261572013-04-23 21:39:57 +08001054 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001055 * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
Marek Lindneref261572013-04-23 21:39:57 +08001056 * otherwise.
1057 */
1058int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1059 struct batadv_hard_iface *recv_if)
1060{
1061 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1062 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1063 unsigned char *tvlv_buff;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001064 u16 tvlv_buff_len;
Marek Lindneref261572013-04-23 21:39:57 +08001065 int hdr_size = sizeof(*unicast_tvlv_packet);
1066 int ret = NET_RX_DROP;
1067
1068 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001069 goto free_skb;
Marek Lindneref261572013-04-23 21:39:57 +08001070
1071 /* the header is likely to be modified while forwarding */
1072 if (skb_cow(skb, hdr_size) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001073 goto free_skb;
Marek Lindneref261572013-04-23 21:39:57 +08001074
1075 /* packet needs to be linearized to access the tvlv content */
1076 if (skb_linearize(skb) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001077 goto free_skb;
Marek Lindneref261572013-04-23 21:39:57 +08001078
1079 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1080
1081 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1082 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1083
1084 if (tvlv_buff_len > skb->len - hdr_size)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001085 goto free_skb;
Marek Lindneref261572013-04-23 21:39:57 +08001086
1087 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1088 unicast_tvlv_packet->src,
1089 unicast_tvlv_packet->dst,
1090 tvlv_buff, tvlv_buff_len);
1091
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001092 if (ret != NET_RX_SUCCESS) {
Marek Lindneref261572013-04-23 21:39:57 +08001093 ret = batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001094 /* skb was consumed */
1095 skb = NULL;
1096 }
1097
1098free_skb:
1099 kfree_skb(skb);
Marek Lindneref261572013-04-23 21:39:57 +08001100
1101 return ret;
1102}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001103
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001104/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001105 * batadv_recv_frag_packet() - process received fragment
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001106 * @skb: the received fragment
1107 * @recv_if: interface that the skb is received on
1108 *
1109 * This function does one of the three following things: 1) Forward fragment, if
Sven Eckelmannbccb48c2020-06-01 20:13:21 +02001110 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we still
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001111 * lack further fragments; 3) Merge fragments, if we have all needed parts.
1112 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001113 * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001114 */
1115int batadv_recv_frag_packet(struct sk_buff *skb,
1116 struct batadv_hard_iface *recv_if)
1117{
1118 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1119 struct batadv_orig_node *orig_node_src = NULL;
1120 struct batadv_frag_packet *frag_packet;
1121 int ret = NET_RX_DROP;
1122
1123 if (batadv_check_unicast_packet(bat_priv, skb,
1124 sizeof(*frag_packet)) < 0)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001125 goto free_skb;
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001126
1127 frag_packet = (struct batadv_frag_packet *)skb->data;
1128 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1129 if (!orig_node_src)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001130 goto free_skb;
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001131
Andrew Lunnc0f25c802016-05-09 20:03:36 +02001132 skb->priority = frag_packet->priority + 256;
1133
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001134 /* Route the fragment if it is not for us and too big to be merged. */
1135 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1136 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001137 /* skb was consumed */
1138 skb = NULL;
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001139 ret = NET_RX_SUCCESS;
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001140 goto put_orig_node;
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001141 }
1142
1143 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1144 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1145
1146 /* Add fragment to buffer and merge if possible. */
1147 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001148 goto put_orig_node;
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001149
1150 /* Deliver merged packet to the appropriate handler, if it was
1151 * merged
1152 */
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001153 if (skb) {
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001154 batadv_batman_skb_recv(skb, recv_if->net_dev,
1155 &recv_if->batman_adv_ptype, NULL);
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001156 /* skb was consumed */
1157 skb = NULL;
1158 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001159
1160 ret = NET_RX_SUCCESS;
1161
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001162put_orig_node:
1163 batadv_orig_node_put(orig_node_src);
1164free_skb:
1165 kfree_skb(skb);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001166
1167 return ret;
1168}
1169
Sven Eckelmannff15c272017-12-02 19:51:53 +01001170/**
1171 * batadv_recv_bcast_packet() - Process incoming broadcast packet
1172 * @skb: incoming packet buffer
1173 * @recv_if: incoming hard interface
1174 *
1175 * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
1176 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001177int batadv_recv_bcast_packet(struct sk_buff *skb,
1178 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001179{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001180 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1181 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001182 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001184 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001185 int ret = NET_RX_DROP;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001186 s32 seq_diff;
1187 u32 seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001188
1189 /* drop packet if it has not necessary minimum size */
1190 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001191 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001192
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001193 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001194
1195 /* packet with broadcast indication but unicast recipient */
1196 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001197 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001198
Sven Eckelmann9f75c8e2016-08-06 17:04:21 +02001199 /* packet with broadcast/multicast sender address */
1200 if (is_multicast_ether_addr(ethhdr->h_source))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001201 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001202
1203 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001204 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001205 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001206
Sven Eckelmann96412692012-06-05 22:31:30 +02001207 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001208
1209 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001210 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001211 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001212
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001213 if (bcast_packet->ttl < 2)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001214 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215
Sven Eckelmannda641192012-05-12 13:48:56 +02001216 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001217
1218 if (!orig_node)
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001219 goto free_skb;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001220
Marek Lindnerf3e00082011-01-25 21:52:11 +00001221 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001222
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001223 seqno = ntohl(bcast_packet->seqno);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001224 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001225 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001226 seqno))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001227 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001228
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001229 seq_diff = seqno - orig_node->last_bcast_seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001230
1231 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001232 if (batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich81f02682015-11-23 19:57:22 +01001233 BATADV_BCAST_MAX_AGE,
1234 &orig_node->bcast_seqno_reset, NULL))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001235 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001236
1237 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001238 * if required.
1239 */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001240 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001241 orig_node->last_bcast_seqno = seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242
Marek Lindnerf3e00082011-01-25 21:52:11 +00001243 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001244
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001245 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001246 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001247 goto free_skb;
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001248
Simon Wunderlichc54f38c2013-07-29 17:56:44 +02001249 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1250
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001251 /* rebroadcast packet */
Linus Lüssing3111bee2016-08-07 12:34:19 +02001252 batadv_add_bcast_packet_to_list(bat_priv, skb, 1, false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001253
Simon Wunderlich23721382012-01-22 20:00:19 +01001254 /* don't hand the broadcast up if it is from an originator
1255 * from the same backbone.
1256 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001257 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001258 goto free_skb;
Simon Wunderlich23721382012-01-22 20:00:19 +01001259
Antonio Quartullic384ea32011-06-26 03:37:18 +02001260 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1261 goto rx_success;
1262 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1263 goto rx_success;
1264
Linus Lüssingb61ec312018-12-30 16:52:53 +01001265 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1266
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001267 /* broadcast for me */
Sven Eckelmann6535db52016-02-28 11:38:51 +01001268 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001269
1270rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +00001271 ret = NET_RX_SUCCESS;
1272 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001273
Marek Lindnerf3e00082011-01-25 21:52:11 +00001274spin_unlock:
1275 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannb91a2542016-07-17 21:04:04 +02001276free_skb:
1277 kfree_skb(skb);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001278out:
1279 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +01001280 batadv_orig_node_put(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001281 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001282}