blob: 6648f321864d8f7b33d258f9ee2c93e339738a93 [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
18#include "main.h"
19#include "routing.h"
20#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000021#include "soft-interface.h"
22#include "hard-interface.h"
23#include "icmp_socket.h"
24#include "translation-table.h"
25#include "originator.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010026#include "bridge_loop_avoidance.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020027#include "distributed-arp-table.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010028#include "network-coding.h"
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +020029#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullic018ad32013-06-04 12:11:39 +020031#include <linux/if_vlan.h>
32
Sven Eckelmann63b01032012-05-16 20:23:13 +020033static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +020034 struct batadv_hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010035
Simon Wunderlich7351a4822013-11-13 19:14:47 +010036/**
37 * _batadv_update_route - set the router for this originator
38 * @bat_priv: the bat priv with all the soft interface information
39 * @orig_node: orig node which is to be configured
40 * @recv_if: the receive interface for which this route is set
41 * @neigh_node: neighbor which should be the next router
42 *
43 * This function does not perform any error checks
44 */
Sven Eckelmann56303d32012-06-05 22:31:31 +020045static void _batadv_update_route(struct batadv_priv *bat_priv,
46 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +010047 struct batadv_hard_iface *recv_if,
Sven Eckelmann56303d32012-06-05 22:31:31 +020048 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049{
Simon Wunderlich7351a4822013-11-13 19:14:47 +010050 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann56303d32012-06-05 22:31:31 +020051 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000052
Simon Wunderlich7351a4822013-11-13 19:14:47 +010053 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
54 if (!orig_ifinfo)
55 return;
56
57 rcu_read_lock();
58 curr_router = rcu_dereference(orig_ifinfo->router);
59 if (curr_router && !atomic_inc_not_zero(&curr_router->refcount))
60 curr_router = NULL;
61 rcu_read_unlock();
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000062
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000064 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020065 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
66 "Deleting route towards: %pM\n", orig_node->orig);
Antonio Quartulli95fb1302013-08-07 18:28:55 +020067 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +020068 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000070 /* route added */
71 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020072 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020073 "Adding route towards: %pM (via %pM)\n",
74 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000075 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020076 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020077 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020078 "Changing route towards: %pM (now via %pM - was via %pM)\n",
79 orig_node->orig, neigh_node->addr,
80 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081 }
82
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000083 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020084 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000085
86 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000087 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
88 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000089
90 spin_lock_bh(&orig_node->neigh_list_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +010091 rcu_assign_pointer(orig_ifinfo->router, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000092 spin_unlock_bh(&orig_node->neigh_list_lock);
Simon Wunderlich7351a4822013-11-13 19:14:47 +010093 batadv_orig_ifinfo_free_ref(orig_ifinfo);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000094
95 /* decrease refcount of previous best neighbor */
96 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020097 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098}
99
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100100/**
101 * batadv_update_route - set the router for this originator
102 * @bat_priv: the bat priv with all the soft interface information
103 * @orig_node: orig node which is to be configured
104 * @recv_if: the receive interface for which this route is set
105 * @neigh_node: neighbor which should be the next router
106 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200107void batadv_update_route(struct batadv_priv *bat_priv,
108 struct batadv_orig_node *orig_node,
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100109 struct batadv_hard_iface *recv_if,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200110 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200112 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113
114 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000115 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100117 router = batadv_orig_router_get(orig_node, recv_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000118
119 if (router != neigh_node)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100120 _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000121
122out:
123 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200124 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125}
126
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127/* checks whether the host restarted and is in the protection time.
128 * returns:
129 * 0 if the packet is to be accepted
130 * 1 if the packet is to be ignored.
131 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200132int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200133 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200135 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
136 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
137 if (!batadv_has_timed_out(*last_reset,
138 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800140
141 *last_reset = jiffies;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200142 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200143 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800145
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146 return 0;
147}
148
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200149bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200150 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200151 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 struct ethhdr *ethhdr;
154
155 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800156 if (unlikely(!pskb_may_pull(skb, header_len)))
157 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200159 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
161 /* packet with broadcast indication but unicast recipient */
162 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800163 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164
165 /* packet with broadcast sender address */
166 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800167 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
169 /* create a copy of the skb, if needed, to modify it. */
170 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800171 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172
173 /* keep skb linear */
174 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800175 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176
Marek Lindnerc3e29312012-03-04 16:56:25 +0800177 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178}
179
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200180/**
181 * batadv_recv_my_icmp_packet - receive an icmp packet locally
182 * @bat_priv: the bat priv with all the soft interface information
183 * @skb: icmp packet to process
184 *
185 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
186 * otherwise.
187 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200188static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200189 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200191 struct batadv_hard_iface *primary_if = NULL;
192 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200193 struct batadv_icmp_header *icmph;
194 int res, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200196 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200198 switch (icmph->msg_type) {
199 case BATADV_ECHO_REPLY:
200 case BATADV_DESTINATION_UNREACHABLE:
201 case BATADV_TTL_EXCEEDED:
202 /* receive the packet */
203 if (skb_linearize(skb) < 0)
204 break;
205
206 batadv_socket_receive_packet(icmph, skb->len);
207 break;
208 case BATADV_ECHO_REQUEST:
209 /* answer echo request (ping) */
210 primary_if = batadv_primary_if_get_selected(bat_priv);
211 if (!primary_if)
212 goto out;
213
214 /* get routing information */
215 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
216 if (!orig_node)
217 goto out;
218
219 /* create a copy of the skb, if needed, to modify it. */
220 if (skb_cow(skb, ETH_HLEN) < 0)
221 goto out;
222
223 icmph = (struct batadv_icmp_header *)skb->data;
224
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100225 ether_addr_copy(icmph->dst, icmph->orig);
226 ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200227 icmph->msg_type = BATADV_ECHO_REPLY;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100228 icmph->ttl = BATADV_TTL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200229
230 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
231 if (res != NET_XMIT_DROP)
232 ret = NET_RX_SUCCESS;
233
234 break;
235 default:
236 /* drop unknown type */
Marek Lindner44524fc2011-02-10 14:33:53 +0000237 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238 }
Marek Lindner44524fc2011-02-10 14:33:53 +0000239out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200240 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200241 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000242 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200243 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 return ret;
245}
246
Sven Eckelmann56303d32012-06-05 22:31:31 +0200247static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200248 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200250 struct batadv_hard_iface *primary_if = NULL;
251 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200252 struct batadv_icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000253 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Sven Eckelmann96412692012-06-05 22:31:30 +0200255 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
257 /* send TTL exceeded if packet is an echo request (traceroute) */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100258 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100259 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100260 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000261 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 }
263
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200264 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200265 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000266 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
268 /* get routing information */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100269 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000270 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000271 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000272
Marek Lindner44524fc2011-02-10 14:33:53 +0000273 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100274 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000275 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Sven Eckelmann96412692012-06-05 22:31:30 +0200277 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100279 ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
280 ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100281 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
282 icmp_packet->ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200284 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200285 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286
Marek Lindner44524fc2011-02-10 14:33:53 +0000287out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200288 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200289 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000290 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200291 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 return ret;
293}
294
295
Sven Eckelmann56303d32012-06-05 22:31:31 +0200296int batadv_recv_icmp_packet(struct sk_buff *skb,
297 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200299 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200300 struct batadv_icmp_header *icmph;
301 struct batadv_icmp_packet_rr *icmp_packet_rr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200303 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200304 int hdr_size = sizeof(struct batadv_icmp_header);
Marek Lindner44524fc2011-02-10 14:33:53 +0000305 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307 /* drop packet if it has not necessary minimum size */
308 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000309 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200311 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
313 /* packet with unicast indication but broadcast recipient */
314 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000315 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
317 /* packet with broadcast sender address */
318 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000319 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320
321 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200322 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000323 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200325 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
327 /* add record route information if not full */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200328 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
329 icmph->msg_type == BATADV_ECHO_REQUEST) &&
330 (skb->len >= sizeof(struct batadv_icmp_packet_rr))) {
331 if (skb_linearize(skb) < 0)
332 goto out;
333
334 /* create a copy of the skb, if needed, to modify it. */
335 if (skb_cow(skb, ETH_HLEN) < 0)
336 goto out;
337
338 icmph = (struct batadv_icmp_header *)skb->data;
339 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
340 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
341 goto out;
342
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100343 ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
344 ethhdr->h_dest);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200345 icmp_packet_rr->rr_cur++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346 }
347
348 /* packet for me */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200349 if (batadv_is_my_mac(bat_priv, icmph->dst))
350 return batadv_recv_my_icmp_packet(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351
352 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100353 if (icmph->ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200354 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 /* get routing information */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200357 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000358 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000359 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000360
Marek Lindner44524fc2011-02-10 14:33:53 +0000361 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100362 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000363 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200365 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
Marek Lindner44524fc2011-02-10 14:33:53 +0000367 /* decrement ttl */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100368 icmph->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369
Marek Lindner44524fc2011-02-10 14:33:53 +0000370 /* route it */
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200371 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200372 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
Marek Lindner44524fc2011-02-10 14:33:53 +0000374out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000375 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200376 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377 return ret;
378}
379
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100380/**
381 * batadv_check_unicast_packet - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400382 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100383 * @skb: packet to check
384 * @hdr_size: size of header to pull
385 *
386 * Check for short header and bad addresses in given packet. Returns negative
387 * value when check fails and 0 otherwise. The negative value depends on the
388 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
389 * and -EREMOTE for non-local (other host) destination.
390 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200391static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
392 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200393{
394 struct ethhdr *ethhdr;
395
396 /* drop packet if it has not necessary minimum size */
397 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100398 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200399
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200400 ethhdr = eth_hdr(skb);
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200401
402 /* packet with unicast indication but broadcast recipient */
403 if (is_broadcast_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100404 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200405
406 /* packet with broadcast sender address */
407 if (is_broadcast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100408 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200409
410 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200411 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100412 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200413
414 return 0;
415}
416
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100417/**
418 * batadv_find_router - find a suitable router for this originator
419 * @bat_priv: the bat priv with all the soft interface information
420 * @orig_node: the destination node
421 * @recv_if: pointer to interface this packet was received on
422 *
423 * Returns the router which should be used for this orig_node on
424 * this interface, or NULL if not available.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200425 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200426struct batadv_neigh_node *
427batadv_find_router(struct batadv_priv *bat_priv,
428 struct batadv_orig_node *orig_node,
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100429 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000430{
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100431 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
432 struct batadv_neigh_node *first_candidate_router = NULL;
433 struct batadv_neigh_node *next_candidate_router = NULL;
434 struct batadv_neigh_node *router, *cand_router = NULL;
435 struct batadv_neigh_node *last_cand_router = NULL;
436 struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
437 struct batadv_orig_ifinfo *next_candidate = NULL;
438 struct batadv_orig_ifinfo *last_candidate;
439 bool last_candidate_found = false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
441 if (!orig_node)
442 return NULL;
443
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100444 router = batadv_orig_router_get(orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
Simon Wunderlich329887a2014-08-13 14:26:56 +0200446 if (!router)
447 return router;
448
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100449 /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
450 * and if activated.
451 */
Simon Wunderlich329887a2014-08-13 14:26:56 +0200452 if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
Simon Wunderlichf3b3d902013-11-13 19:14:50 +0100453 return router;
454
455 /* bonding: loop through the list of possible routers found
456 * for the various outgoing interfaces and find a candidate after
457 * the last chosen bonding candidate (next_candidate). If no such
458 * router is found, use the first candidate found (the previously
459 * chosen bonding candidate might have been the last one in the list).
460 * If this can't be found either, return the previously choosen
461 * router - obviously there are no other candidates.
462 */
463 rcu_read_lock();
464 last_candidate = orig_node->last_bonding_candidate;
465 if (last_candidate)
466 last_cand_router = rcu_dereference(last_candidate->router);
467
468 hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
469 /* acquire some structures and references ... */
470 if (!atomic_inc_not_zero(&cand->refcount))
471 continue;
472
473 cand_router = rcu_dereference(cand->router);
474 if (!cand_router)
475 goto next;
476
477 if (!atomic_inc_not_zero(&cand_router->refcount)) {
478 cand_router = NULL;
479 goto next;
480 }
481
482 /* alternative candidate should be good enough to be
483 * considered
484 */
485 if (!bao->bat_neigh_is_equiv_or_better(cand_router,
486 cand->if_outgoing,
487 router, recv_if))
488 goto next;
489
490 /* don't use the same router twice */
491 if (last_cand_router == cand_router)
492 goto next;
493
494 /* mark the first possible candidate */
495 if (!first_candidate) {
496 atomic_inc(&cand_router->refcount);
497 atomic_inc(&cand->refcount);
498 first_candidate = cand;
499 first_candidate_router = cand_router;
500 }
501
502 /* check if the loop has already passed the previously selected
503 * candidate ... this function should select the next candidate
504 * AFTER the previously used bonding candidate.
505 */
506 if (!last_candidate || last_candidate_found) {
507 next_candidate = cand;
508 next_candidate_router = cand_router;
509 break;
510 }
511
512 if (last_candidate == cand)
513 last_candidate_found = true;
514next:
515 /* free references */
516 if (cand_router) {
517 batadv_neigh_node_free_ref(cand_router);
518 cand_router = NULL;
519 }
520 batadv_orig_ifinfo_free_ref(cand);
521 }
522 rcu_read_unlock();
523
524 /* last_bonding_candidate is reset below, remove the old reference. */
525 if (orig_node->last_bonding_candidate)
526 batadv_orig_ifinfo_free_ref(orig_node->last_bonding_candidate);
527
528 /* After finding candidates, handle the three cases:
529 * 1) there is a next candidate, use that
530 * 2) there is no next candidate, use the first of the list
531 * 3) there is no candidate at all, return the default router
532 */
533 if (next_candidate) {
534 batadv_neigh_node_free_ref(router);
535
536 /* remove references to first candidate, we don't need it. */
537 if (first_candidate) {
538 batadv_neigh_node_free_ref(first_candidate_router);
539 batadv_orig_ifinfo_free_ref(first_candidate);
540 }
541 router = next_candidate_router;
542 orig_node->last_bonding_candidate = next_candidate;
543 } else if (first_candidate) {
544 batadv_neigh_node_free_ref(router);
545
546 /* refcounting has already been done in the loop above. */
547 router = first_candidate_router;
548 orig_node->last_bonding_candidate = first_candidate;
549 } else {
550 orig_node->last_bonding_candidate = NULL;
551 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000552
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553 return router;
554}
555
Sven Eckelmann63b01032012-05-16 20:23:13 +0200556static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200557 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200559 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
560 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200561 struct batadv_unicast_packet *unicast_packet;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200562 struct ethhdr *ethhdr = eth_hdr(skb);
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200563 int res, hdr_len, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564
Sven Eckelmann96412692012-06-05 22:31:30 +0200565 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000566
567 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100568 if (unicast_packet->ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100569 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
570 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000571 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 }
573
574 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200575 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000576
Marek Lindner44524fc2011-02-10 14:33:53 +0000577 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200578 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100581 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000582 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000584 /* decrement ttl */
Martin Hundebøllf097e252013-05-23 16:53:01 +0200585 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100586 unicast_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100588 switch (unicast_packet->packet_type) {
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200589 case BATADV_UNICAST_4ADDR:
590 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
591 break;
592 case BATADV_UNICAST:
593 hdr_len = sizeof(struct batadv_unicast_packet);
594 break;
595 default:
596 /* other packet types not supported - yet */
597 hdr_len = -1;
598 break;
599 }
600
601 if (hdr_len > 0)
602 batadv_skb_set_priority(skb, hdr_len);
603
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200604 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Martin Hundebøll95332472013-01-25 11:12:40 +0100605
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200606 /* translate transmit result into receive result */
607 if (res == NET_XMIT_SUCCESS) {
608 /* skb was transmitted and consumed */
Martin Hundebøll95332472013-01-25 11:12:40 +0100609 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
610 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
611 skb->len + ETH_HLEN);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200612
613 ret = NET_RX_SUCCESS;
614 } else if (res == NET_XMIT_POLICED) {
615 /* skb was buffered and consumed */
616 ret = NET_RX_SUCCESS;
Martin Hundebøll95332472013-01-25 11:12:40 +0100617 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Marek Lindner44524fc2011-02-10 14:33:53 +0000619out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000620 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200621 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000622 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623}
624
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200625/**
626 * batadv_reroute_unicast_packet - update the unicast header for re-routing
627 * @bat_priv: the bat priv with all the soft interface information
628 * @unicast_packet: the unicast header to be updated
629 * @dst_addr: the payload destination
Antonio Quartullic018ad32013-06-04 12:11:39 +0200630 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200631 *
632 * Search the translation table for dst_addr and update the unicast header with
633 * the new corresponding information (originator address where the destination
634 * client currently is and its known TTVN)
635 *
636 * Returns true if the packet header has been updated, false otherwise
637 */
638static bool
639batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
640 struct batadv_unicast_packet *unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200641 uint8_t *dst_addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200642{
643 struct batadv_orig_node *orig_node = NULL;
644 struct batadv_hard_iface *primary_if = NULL;
645 bool ret = false;
646 uint8_t *orig_addr, orig_ttvn;
647
Antonio Quartullic018ad32013-06-04 12:11:39 +0200648 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200649 primary_if = batadv_primary_if_get_selected(bat_priv);
650 if (!primary_if)
651 goto out;
652 orig_addr = primary_if->net_dev->dev_addr;
653 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
654 } else {
Antonio Quartullic018ad32013-06-04 12:11:39 +0200655 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
656 vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200657 if (!orig_node)
658 goto out;
659
660 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
661 goto out;
662
663 orig_addr = orig_node->orig;
664 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
665 }
666
667 /* update the packet header */
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100668 ether_addr_copy(unicast_packet->dest, orig_addr);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200669 unicast_packet->ttvn = orig_ttvn;
670
671 ret = true;
672out:
673 if (primary_if)
674 batadv_hardif_free_ref(primary_if);
675 if (orig_node)
676 batadv_orig_node_free_ref(orig_node);
677
678 return ret;
679}
680
Sven Eckelmann56303d32012-06-05 22:31:31 +0200681static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200682 struct sk_buff *skb, int hdr_len) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200683 struct batadv_unicast_packet *unicast_packet;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200684 struct batadv_hard_iface *primary_if;
685 struct batadv_orig_node *orig_node;
686 uint8_t curr_ttvn, old_ttvn;
687 struct ethhdr *ethhdr;
688 unsigned short vid;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200689 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200690
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200691 /* check if there is enough data before accessing it */
Antonio Quartullif1791422014-01-30 00:12:24 +0100692 if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200693 return 0;
694
695 /* create a copy of the skb (in case of for re-routing) to modify it. */
696 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200697 return 0;
698
Sven Eckelmann96412692012-06-05 22:31:30 +0200699 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200700 vid = batadv_get_vid(skb, hdr_len);
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200701 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200702
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200703 /* check if the destination client was served by this node and it is now
704 * roaming. In this case, it means that the node has got a ROAM_ADV
705 * message and that it knows the new destination in the mesh to re-route
706 * the packet to
707 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200708 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200709 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200710 ethhdr->h_dest, vid))
André Gaul23c4ec12014-06-10 17:50:31 +0200711 batadv_dbg_ratelimited(BATADV_DBG_TT,
712 bat_priv,
713 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
714 unicast_packet->dest,
715 ethhdr->h_dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200716 /* at this point the mesh destination should have been
717 * substituted with the originator address found in the global
718 * table. If not, let the packet go untouched anyway because
719 * there is nothing the node can do
720 */
721 return 1;
722 }
723
724 /* retrieve the TTVN known by this node for the packet destination. This
725 * value is used later to check if the node which sent (or re-routed
726 * last time) the packet had an updated information or not
727 */
728 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200729 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200730 orig_node = batadv_orig_hash_find(bat_priv,
731 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200732 /* if it is not possible to find the orig_node representing the
733 * destination, the packet can immediately be dropped as it will
734 * not be possible to deliver it
735 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200736 if (!orig_node)
737 return 0;
738
739 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200740 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200741 }
742
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200743 /* check if the TTVN contained in the packet is fresher than what the
744 * node knows
745 */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200746 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200747 if (!is_old_ttvn)
748 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200749
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200750 old_ttvn = unicast_packet->ttvn;
751 /* the packet was forged based on outdated network information. Its
752 * destination can possibly be updated and forwarded towards the new
753 * target host
754 */
755 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200756 ethhdr->h_dest, vid)) {
André Gaul23c4ec12014-06-10 17:50:31 +0200757 batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
758 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
759 unicast_packet->dest, ethhdr->h_dest,
760 old_ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200761 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200762 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200763
764 /* the packet has not been re-routed: either the destination is
765 * currently served by this node or there is no destination at all and
766 * it is possible to drop the packet
767 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200768 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200769 return 0;
770
771 /* update the header in order to let the packet be delivered to this
772 * node's soft interface
773 */
774 primary_if = batadv_primary_if_get_selected(bat_priv);
775 if (!primary_if)
776 return 0;
777
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100778 ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200779
780 batadv_hardif_free_ref(primary_if);
781
782 unicast_packet->ttvn = curr_ttvn;
783
Antonio Quartullia73105b2011-04-27 14:27:44 +0200784 return 1;
785}
786
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200787/**
788 * batadv_recv_unhandled_unicast_packet - receive and process packets which
789 * are in the unicast number space but not yet known to the implementation
790 * @skb: unicast tvlv packet to process
791 * @recv_if: pointer to interface this packet was received on
792 *
793 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
794 * otherwise.
795 */
796int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
797 struct batadv_hard_iface *recv_if)
798{
799 struct batadv_unicast_packet *unicast_packet;
800 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
801 int check, hdr_size = sizeof(*unicast_packet);
802
803 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
804 if (check < 0)
805 return NET_RX_DROP;
806
807 /* we don't know about this type, drop it. */
808 unicast_packet = (struct batadv_unicast_packet *)skb->data;
809 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
810 return NET_RX_DROP;
811
812 return batadv_route_unicast_packet(skb, recv_if);
813}
814
Sven Eckelmann56303d32012-06-05 22:31:31 +0200815int batadv_recv_unicast_packet(struct sk_buff *skb,
816 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200818 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200819 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200820 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Antonio Quartulli9affec62012-10-14 17:19:19 +0200821 uint8_t *orig_addr;
822 struct batadv_orig_node *orig_node = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100823 int check, hdr_size = sizeof(*unicast_packet);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200824 bool is4addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000825
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200826 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200827 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200828
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100829 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200830 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200831 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +0200832 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200833
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100834 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -0400835 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100836
837 /* Even though the packet is not for us, we might save it to use for
838 * decoding a later received coded packet
839 */
840 if (check == -EREMOTE)
841 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
842
843 if (check < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000844 return NET_RX_DROP;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200845 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200846 return NET_RX_DROP;
847
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000848 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200849 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Antonio Quartulli9affec62012-10-14 17:19:19 +0200850 if (is4addr) {
Martin Hundebøll4046b242012-04-20 17:02:45 +0200851 batadv_dat_inc_counter(bat_priv,
852 unicast_4addr_packet->subtype);
Antonio Quartulli9affec62012-10-14 17:19:19 +0200853 orig_addr = unicast_4addr_packet->src;
854 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
855 }
Martin Hundebøll4046b242012-04-20 17:02:45 +0200856
Antonio Quartullic384ea32011-06-26 03:37:18 +0200857 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
858 hdr_size))
859 goto rx_success;
860 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
861 hdr_size))
862 goto rx_success;
863
Antonio Quartulli37135172012-07-05 23:38:30 +0200864 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +0200865 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +0200866
Antonio Quartullic384ea32011-06-26 03:37:18 +0200867rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +0200868 if (orig_node)
869 batadv_orig_node_free_ref(orig_node);
870
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871 return NET_RX_SUCCESS;
872 }
873
Sven Eckelmann63b01032012-05-16 20:23:13 +0200874 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875}
876
Marek Lindneref261572013-04-23 21:39:57 +0800877/**
878 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
879 * @skb: unicast tvlv packet to process
880 * @recv_if: pointer to interface this packet was received on
881 * @dst_addr: the payload destination
882 *
883 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
884 * otherwise.
885 */
886int batadv_recv_unicast_tvlv(struct sk_buff *skb,
887 struct batadv_hard_iface *recv_if)
888{
889 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
890 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
891 unsigned char *tvlv_buff;
892 uint16_t tvlv_buff_len;
893 int hdr_size = sizeof(*unicast_tvlv_packet);
894 int ret = NET_RX_DROP;
895
896 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
897 return NET_RX_DROP;
898
899 /* the header is likely to be modified while forwarding */
900 if (skb_cow(skb, hdr_size) < 0)
901 return NET_RX_DROP;
902
903 /* packet needs to be linearized to access the tvlv content */
904 if (skb_linearize(skb) < 0)
905 return NET_RX_DROP;
906
907 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
908
909 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
910 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
911
912 if (tvlv_buff_len > skb->len - hdr_size)
913 return NET_RX_DROP;
914
915 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
916 unicast_tvlv_packet->src,
917 unicast_tvlv_packet->dst,
918 tvlv_buff, tvlv_buff_len);
919
920 if (ret != NET_RX_SUCCESS)
921 ret = batadv_route_unicast_packet(skb, recv_if);
Antonio Quartulli05c3c8a2014-02-11 17:05:07 +0100922 else
923 consume_skb(skb);
Marek Lindneref261572013-04-23 21:39:57 +0800924
925 return ret;
926}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200928/**
929 * batadv_recv_frag_packet - process received fragment
930 * @skb: the received fragment
931 * @recv_if: interface that the skb is received on
932 *
933 * This function does one of the three following things: 1) Forward fragment, if
934 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
935 * lack further fragments; 3) Merge fragments, if we have all needed parts.
936 *
937 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
938 */
939int batadv_recv_frag_packet(struct sk_buff *skb,
940 struct batadv_hard_iface *recv_if)
941{
942 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
943 struct batadv_orig_node *orig_node_src = NULL;
944 struct batadv_frag_packet *frag_packet;
945 int ret = NET_RX_DROP;
946
947 if (batadv_check_unicast_packet(bat_priv, skb,
948 sizeof(*frag_packet)) < 0)
949 goto out;
950
951 frag_packet = (struct batadv_frag_packet *)skb->data;
952 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
953 if (!orig_node_src)
954 goto out;
955
956 /* Route the fragment if it is not for us and too big to be merged. */
957 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
958 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
959 ret = NET_RX_SUCCESS;
960 goto out;
961 }
962
963 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
964 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
965
966 /* Add fragment to buffer and merge if possible. */
967 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
968 goto out;
969
970 /* Deliver merged packet to the appropriate handler, if it was
971 * merged
972 */
973 if (skb)
974 batadv_batman_skb_recv(skb, recv_if->net_dev,
975 &recv_if->batman_adv_ptype, NULL);
976
977 ret = NET_RX_SUCCESS;
978
979out:
980 if (orig_node_src)
981 batadv_orig_node_free_ref(orig_node_src);
982
983 return ret;
984}
985
Sven Eckelmann56303d32012-06-05 22:31:31 +0200986int batadv_recv_bcast_packet(struct sk_buff *skb,
987 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000988{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200989 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
990 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200991 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000992 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200993 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000994 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995 int32_t seq_diff;
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200996 uint32_t seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000997
998 /* drop packet if it has not necessary minimum size */
999 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001000 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001002 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001003
1004 /* packet with broadcast indication but unicast recipient */
1005 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001006 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001007
1008 /* packet with broadcast sender address */
1009 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001010 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011
1012 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001013 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001014 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001015
Sven Eckelmann96412692012-06-05 22:31:30 +02001016 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
1018 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001019 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001020 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021
Simon Wunderlicha40d9b02013-12-02 20:38:31 +01001022 if (bcast_packet->ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001023 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001024
Sven Eckelmannda641192012-05-12 13:48:56 +02001025 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001026
1027 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001028 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
Marek Lindnerf3e00082011-01-25 21:52:11 +00001030 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001032 seqno = ntohl(bcast_packet->seqno);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001034 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001035 seqno))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001036 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001038 seq_diff = seqno - orig_node->last_bcast_seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039
1040 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001041 if (batadv_window_protected(bat_priv, seq_diff,
1042 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001043 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044
1045 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001046 * if required.
1047 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001048 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Antonio Quartulli3fba7322013-10-13 02:50:17 +02001049 orig_node->last_bcast_seqno = seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
Marek Lindnerf3e00082011-01-25 21:52:11 +00001051 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001052
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001053 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001054 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001055 goto out;
1056
Simon Wunderlichc54f38c2013-07-29 17:56:44 +02001057 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1058
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001059 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001060 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061
Simon Wunderlich23721382012-01-22 20:00:19 +01001062 /* don't hand the broadcast up if it is from an originator
1063 * from the same backbone.
1064 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001065 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001066 goto out;
1067
Antonio Quartullic384ea32011-06-26 03:37:18 +02001068 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1069 goto rx_success;
1070 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1071 goto rx_success;
1072
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073 /* broadcast for me */
Antonio Quartulli37135172012-07-05 23:38:30 +02001074 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1075 orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001076
1077rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +00001078 ret = NET_RX_SUCCESS;
1079 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001080
Marek Lindnerf3e00082011-01-25 21:52:11 +00001081spin_unlock:
1082 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001083out:
1084 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001085 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001086 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087}