blob: 4fd2687b88c2ceb352bf3e812a1757830d5b71e0 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 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
Sven Eckelmann56303d32012-06-05 22:31:31 +020036static void _batadv_update_route(struct batadv_priv *bat_priv,
37 struct batadv_orig_node *orig_node,
38 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039{
Sven Eckelmann56303d32012-06-05 22:31:31 +020040 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000041
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020042 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000043
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000045 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020046 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
47 "Deleting route towards: %pM\n", orig_node->orig);
Antonio Quartulli95fb1302013-08-07 18:28:55 +020048 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +020049 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000051 /* route added */
52 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020053 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020054 "Adding route towards: %pM (via %pM)\n",
55 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000056 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020057 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020058 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020059 "Changing route towards: %pM (now via %pM - was via %pM)\n",
60 orig_node->orig, neigh_node->addr,
61 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 }
63
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000064 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020065 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000066
67 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000068 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
69 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000070
71 spin_lock_bh(&orig_node->neigh_list_lock);
72 rcu_assign_pointer(orig_node->router, neigh_node);
73 spin_unlock_bh(&orig_node->neigh_list_lock);
74
75 /* decrease refcount of previous best neighbor */
76 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020077 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078}
79
Sven Eckelmann56303d32012-06-05 22:31:31 +020080void batadv_update_route(struct batadv_priv *bat_priv,
81 struct batadv_orig_node *orig_node,
82 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083{
Sven Eckelmann56303d32012-06-05 22:31:31 +020084 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085
86 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000087 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000088
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020089 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000090
91 if (router != neigh_node)
Sven Eckelmann63b01032012-05-16 20:23:13 +020092 _batadv_update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000093
94out:
95 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020096 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097}
98
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099/* checks whether the host restarted and is in the protection time.
100 * returns:
101 * 0 if the packet is to be accepted
102 * 1 if the packet is to be ignored.
103 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200104int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200105 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200107 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
108 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
109 if (!batadv_has_timed_out(*last_reset,
110 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800112
113 *last_reset = jiffies;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200114 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200115 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800117
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 return 0;
119}
120
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200121bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200122 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200123 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000124{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 struct ethhdr *ethhdr;
126
127 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800128 if (unlikely(!pskb_may_pull(skb, header_len)))
129 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000130
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200131 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132
133 /* packet with broadcast indication but unicast recipient */
134 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800135 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136
137 /* packet with broadcast sender address */
138 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800139 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140
141 /* create a copy of the skb, if needed, to modify it. */
142 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800143 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144
145 /* keep skb linear */
146 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800147 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148
Marek Lindnerc3e29312012-03-04 16:56:25 +0800149 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000150}
151
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200152/**
153 * batadv_recv_my_icmp_packet - receive an icmp packet locally
154 * @bat_priv: the bat priv with all the soft interface information
155 * @skb: icmp packet to process
156 *
157 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
158 * otherwise.
159 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200161 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000162{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200163 struct batadv_hard_iface *primary_if = NULL;
164 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200165 struct batadv_icmp_header *icmph;
166 int res, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200168 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200170 switch (icmph->msg_type) {
171 case BATADV_ECHO_REPLY:
172 case BATADV_DESTINATION_UNREACHABLE:
173 case BATADV_TTL_EXCEEDED:
174 /* receive the packet */
175 if (skb_linearize(skb) < 0)
176 break;
177
178 batadv_socket_receive_packet(icmph, skb->len);
179 break;
180 case BATADV_ECHO_REQUEST:
181 /* answer echo request (ping) */
182 primary_if = batadv_primary_if_get_selected(bat_priv);
183 if (!primary_if)
184 goto out;
185
186 /* get routing information */
187 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
188 if (!orig_node)
189 goto out;
190
191 /* create a copy of the skb, if needed, to modify it. */
192 if (skb_cow(skb, ETH_HLEN) < 0)
193 goto out;
194
195 icmph = (struct batadv_icmp_header *)skb->data;
196
197 memcpy(icmph->dst, icmph->orig, ETH_ALEN);
198 memcpy(icmph->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
199 icmph->msg_type = BATADV_ECHO_REPLY;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100200 icmph->ttl = BATADV_TTL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200201
202 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
203 if (res != NET_XMIT_DROP)
204 ret = NET_RX_SUCCESS;
205
206 break;
207 default:
208 /* drop unknown type */
Marek Lindner44524fc2011-02-10 14:33:53 +0000209 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 }
Marek Lindner44524fc2011-02-10 14:33:53 +0000211out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200212 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200213 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000214 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200215 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216 return ret;
217}
218
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200220 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200222 struct batadv_hard_iface *primary_if = NULL;
223 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200224 struct batadv_icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000225 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
Sven Eckelmann96412692012-06-05 22:31:30 +0200227 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000228
229 /* send TTL exceeded if packet is an echo request (traceroute) */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100230 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100231 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100232 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000233 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234 }
235
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200236 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200237 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000238 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239
240 /* get routing information */
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100241 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000242 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000243 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000244
Marek Lindner44524fc2011-02-10 14:33:53 +0000245 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100246 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000247 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248
Sven Eckelmann96412692012-06-05 22:31:30 +0200249 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100251 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
252 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr,
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200253 ETH_ALEN);
Antonio Quartulli27a417e2013-12-05 15:33:00 +0100254 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
255 icmp_packet->ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200257 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200258 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Marek Lindner44524fc2011-02-10 14:33:53 +0000260out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200261 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200262 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000263 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200264 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265 return ret;
266}
267
268
Sven Eckelmann56303d32012-06-05 22:31:31 +0200269int batadv_recv_icmp_packet(struct sk_buff *skb,
270 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200272 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200273 struct batadv_icmp_header *icmph;
274 struct batadv_icmp_packet_rr *icmp_packet_rr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200276 struct batadv_orig_node *orig_node = NULL;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200277 int hdr_size = sizeof(struct batadv_icmp_header);
Marek Lindner44524fc2011-02-10 14:33:53 +0000278 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 /* drop packet if it has not necessary minimum size */
281 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000282 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200284 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
286 /* packet with unicast indication but broadcast recipient */
287 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000288 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
290 /* packet with broadcast sender address */
291 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000292 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293
294 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200295 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000296 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200298 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299
300 /* add record route information if not full */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200301 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
302 icmph->msg_type == BATADV_ECHO_REQUEST) &&
303 (skb->len >= sizeof(struct batadv_icmp_packet_rr))) {
304 if (skb_linearize(skb) < 0)
305 goto out;
306
307 /* create a copy of the skb, if needed, to modify it. */
308 if (skb_cow(skb, ETH_HLEN) < 0)
309 goto out;
310
311 icmph = (struct batadv_icmp_header *)skb->data;
312 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
313 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
314 goto out;
315
316 memcpy(&(icmp_packet_rr->rr[icmp_packet_rr->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100317 ethhdr->h_dest, ETH_ALEN);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200318 icmp_packet_rr->rr_cur++;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 }
320
321 /* packet for me */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200322 if (batadv_is_my_mac(bat_priv, icmph->dst))
323 return batadv_recv_my_icmp_packet(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324
325 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100326 if (icmph->ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200327 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 /* get routing information */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200330 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000331 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000332 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000333
Marek Lindner44524fc2011-02-10 14:33:53 +0000334 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100335 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000336 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200338 icmph = (struct batadv_icmp_header *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
Marek Lindner44524fc2011-02-10 14:33:53 +0000340 /* decrement ttl */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100341 icmph->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342
Marek Lindner44524fc2011-02-10 14:33:53 +0000343 /* route it */
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200344 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200345 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346
Marek Lindner44524fc2011-02-10 14:33:53 +0000347out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000348 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200349 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 return ret;
351}
352
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100353/**
354 * batadv_check_unicast_packet - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400355 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100356 * @skb: packet to check
357 * @hdr_size: size of header to pull
358 *
359 * Check for short header and bad addresses in given packet. Returns negative
360 * value when check fails and 0 otherwise. The negative value depends on the
361 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
362 * and -EREMOTE for non-local (other host) destination.
363 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200364static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
365 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200366{
367 struct ethhdr *ethhdr;
368
369 /* drop packet if it has not necessary minimum size */
370 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100371 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200372
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200373 ethhdr = eth_hdr(skb);
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200374
375 /* packet with unicast indication but broadcast recipient */
376 if (is_broadcast_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100377 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200378
379 /* packet with broadcast sender address */
380 if (is_broadcast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100381 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200382
383 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200384 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100385 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200386
387 return 0;
388}
389
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100390/**
391 * batadv_find_router - find a suitable router for this originator
392 * @bat_priv: the bat priv with all the soft interface information
393 * @orig_node: the destination node
394 * @recv_if: pointer to interface this packet was received on
395 *
396 * Returns the router which should be used for this orig_node on
397 * this interface, or NULL if not available.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200398 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200399struct batadv_neigh_node *
400batadv_find_router(struct batadv_priv *bat_priv,
401 struct batadv_orig_node *orig_node,
402 const struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200404 struct batadv_neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
406 if (!orig_node)
407 return NULL;
408
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200409 router = batadv_orig_node_get_router(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
Simon Wunderlichf6c8b712013-11-13 19:14:45 +0100411 /* TODO: fill this later with new bonding mechanism */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 return router;
414}
415
Sven Eckelmann63b01032012-05-16 20:23:13 +0200416static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200417 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200419 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
420 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200421 struct batadv_unicast_packet *unicast_packet;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200422 struct ethhdr *ethhdr = eth_hdr(skb);
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200423 int res, hdr_len, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
Sven Eckelmann96412692012-06-05 22:31:30 +0200425 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
427 /* TTL exceeded */
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100428 if (unicast_packet->ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100429 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
430 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000431 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 }
433
434 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200435 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000436
Marek Lindner44524fc2011-02-10 14:33:53 +0000437 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200438 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100441 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000442 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 /* decrement ttl */
Martin Hundebøllf097e252013-05-23 16:53:01 +0200445 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100446 unicast_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000447
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100448 switch (unicast_packet->packet_type) {
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200449 case BATADV_UNICAST_4ADDR:
450 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
451 break;
452 case BATADV_UNICAST:
453 hdr_len = sizeof(struct batadv_unicast_packet);
454 break;
455 default:
456 /* other packet types not supported - yet */
457 hdr_len = -1;
458 break;
459 }
460
461 if (hdr_len > 0)
462 batadv_skb_set_priority(skb, hdr_len);
463
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200464 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Martin Hundebøll95332472013-01-25 11:12:40 +0100465
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200466 /* translate transmit result into receive result */
467 if (res == NET_XMIT_SUCCESS) {
468 /* skb was transmitted and consumed */
Martin Hundebøll95332472013-01-25 11:12:40 +0100469 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
470 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
471 skb->len + ETH_HLEN);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200472
473 ret = NET_RX_SUCCESS;
474 } else if (res == NET_XMIT_POLICED) {
475 /* skb was buffered and consumed */
476 ret = NET_RX_SUCCESS;
Martin Hundebøll95332472013-01-25 11:12:40 +0100477 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478
Marek Lindner44524fc2011-02-10 14:33:53 +0000479out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000480 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200481 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000482 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483}
484
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200485/**
486 * batadv_reroute_unicast_packet - update the unicast header for re-routing
487 * @bat_priv: the bat priv with all the soft interface information
488 * @unicast_packet: the unicast header to be updated
489 * @dst_addr: the payload destination
Antonio Quartullic018ad32013-06-04 12:11:39 +0200490 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200491 *
492 * Search the translation table for dst_addr and update the unicast header with
493 * the new corresponding information (originator address where the destination
494 * client currently is and its known TTVN)
495 *
496 * Returns true if the packet header has been updated, false otherwise
497 */
498static bool
499batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
500 struct batadv_unicast_packet *unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200501 uint8_t *dst_addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200502{
503 struct batadv_orig_node *orig_node = NULL;
504 struct batadv_hard_iface *primary_if = NULL;
505 bool ret = false;
506 uint8_t *orig_addr, orig_ttvn;
507
Antonio Quartullic018ad32013-06-04 12:11:39 +0200508 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200509 primary_if = batadv_primary_if_get_selected(bat_priv);
510 if (!primary_if)
511 goto out;
512 orig_addr = primary_if->net_dev->dev_addr;
513 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
514 } else {
Antonio Quartullic018ad32013-06-04 12:11:39 +0200515 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
516 vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200517 if (!orig_node)
518 goto out;
519
520 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
521 goto out;
522
523 orig_addr = orig_node->orig;
524 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
525 }
526
527 /* update the packet header */
528 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
529 unicast_packet->ttvn = orig_ttvn;
530
531 ret = true;
532out:
533 if (primary_if)
534 batadv_hardif_free_ref(primary_if);
535 if (orig_node)
536 batadv_orig_node_free_ref(orig_node);
537
538 return ret;
539}
540
Sven Eckelmann56303d32012-06-05 22:31:31 +0200541static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200542 struct sk_buff *skb, int hdr_len) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200543 struct batadv_unicast_packet *unicast_packet;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200544 struct batadv_hard_iface *primary_if;
545 struct batadv_orig_node *orig_node;
546 uint8_t curr_ttvn, old_ttvn;
547 struct ethhdr *ethhdr;
548 unsigned short vid;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200549 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200550
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200551 /* check if there is enough data before accessing it */
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200552 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200553 return 0;
554
555 /* create a copy of the skb (in case of for re-routing) to modify it. */
556 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200557 return 0;
558
Sven Eckelmann96412692012-06-05 22:31:30 +0200559 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200560 vid = batadv_get_vid(skb, hdr_len);
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200561 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200562
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200563 /* check if the destination client was served by this node and it is now
564 * roaming. In this case, it means that the node has got a ROAM_ADV
565 * message and that it knows the new destination in the mesh to re-route
566 * the packet to
567 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200568 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200569 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200570 ethhdr->h_dest, vid))
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200571 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
572 bat_priv,
573 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
574 unicast_packet->dest,
575 ethhdr->h_dest);
576 /* at this point the mesh destination should have been
577 * substituted with the originator address found in the global
578 * table. If not, let the packet go untouched anyway because
579 * there is nothing the node can do
580 */
581 return 1;
582 }
583
584 /* retrieve the TTVN known by this node for the packet destination. This
585 * value is used later to check if the node which sent (or re-routed
586 * last time) the packet had an updated information or not
587 */
588 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200589 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200590 orig_node = batadv_orig_hash_find(bat_priv,
591 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200592 /* if it is not possible to find the orig_node representing the
593 * destination, the packet can immediately be dropped as it will
594 * not be possible to deliver it
595 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200596 if (!orig_node)
597 return 0;
598
599 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200600 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200601 }
602
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200603 /* check if the TTVN contained in the packet is fresher than what the
604 * node knows
605 */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200606 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200607 if (!is_old_ttvn)
608 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200609
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200610 old_ttvn = unicast_packet->ttvn;
611 /* the packet was forged based on outdated network information. Its
612 * destination can possibly be updated and forwarded towards the new
613 * target host
614 */
615 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200616 ethhdr->h_dest, vid)) {
Antonio Quartulli60c39c72012-08-26 01:05:56 +0200617 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200618 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
619 unicast_packet->dest, ethhdr->h_dest,
620 old_ttvn, curr_ttvn);
621 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200622 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200623
624 /* the packet has not been re-routed: either the destination is
625 * currently served by this node or there is no destination at all and
626 * it is possible to drop the packet
627 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200628 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200629 return 0;
630
631 /* update the header in order to let the packet be delivered to this
632 * node's soft interface
633 */
634 primary_if = batadv_primary_if_get_selected(bat_priv);
635 if (!primary_if)
636 return 0;
637
638 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
639
640 batadv_hardif_free_ref(primary_if);
641
642 unicast_packet->ttvn = curr_ttvn;
643
Antonio Quartullia73105b2011-04-27 14:27:44 +0200644 return 1;
645}
646
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200647/**
648 * batadv_recv_unhandled_unicast_packet - receive and process packets which
649 * are in the unicast number space but not yet known to the implementation
650 * @skb: unicast tvlv packet to process
651 * @recv_if: pointer to interface this packet was received on
652 *
653 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
654 * otherwise.
655 */
656int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
657 struct batadv_hard_iface *recv_if)
658{
659 struct batadv_unicast_packet *unicast_packet;
660 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
661 int check, hdr_size = sizeof(*unicast_packet);
662
663 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
664 if (check < 0)
665 return NET_RX_DROP;
666
667 /* we don't know about this type, drop it. */
668 unicast_packet = (struct batadv_unicast_packet *)skb->data;
669 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
670 return NET_RX_DROP;
671
672 return batadv_route_unicast_packet(skb, recv_if);
673}
674
Sven Eckelmann56303d32012-06-05 22:31:31 +0200675int batadv_recv_unicast_packet(struct sk_buff *skb,
676 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200678 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200679 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200680 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Antonio Quartulli9affec62012-10-14 17:19:19 +0200681 uint8_t *orig_addr;
682 struct batadv_orig_node *orig_node = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100683 int check, hdr_size = sizeof(*unicast_packet);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200684 bool is4addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200686 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200687 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200688
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100689 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200690 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200691 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +0200692 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200693
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100694 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -0400695 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100696
697 /* Even though the packet is not for us, we might save it to use for
698 * decoding a later received coded packet
699 */
700 if (check == -EREMOTE)
701 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
702
703 if (check < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704 return NET_RX_DROP;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200705 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200706 return NET_RX_DROP;
707
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200709 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Antonio Quartulli9affec62012-10-14 17:19:19 +0200710 if (is4addr) {
Martin Hundebøll4046b242012-04-20 17:02:45 +0200711 batadv_dat_inc_counter(bat_priv,
712 unicast_4addr_packet->subtype);
Antonio Quartulli9affec62012-10-14 17:19:19 +0200713 orig_addr = unicast_4addr_packet->src;
714 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
715 }
Martin Hundebøll4046b242012-04-20 17:02:45 +0200716
Antonio Quartullic384ea32011-06-26 03:37:18 +0200717 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
718 hdr_size))
719 goto rx_success;
720 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
721 hdr_size))
722 goto rx_success;
723
Antonio Quartulli37135172012-07-05 23:38:30 +0200724 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +0200725 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +0200726
Antonio Quartullic384ea32011-06-26 03:37:18 +0200727rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +0200728 if (orig_node)
729 batadv_orig_node_free_ref(orig_node);
730
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731 return NET_RX_SUCCESS;
732 }
733
Sven Eckelmann63b01032012-05-16 20:23:13 +0200734 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735}
736
Marek Lindneref261572013-04-23 21:39:57 +0800737/**
738 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
739 * @skb: unicast tvlv packet to process
740 * @recv_if: pointer to interface this packet was received on
741 * @dst_addr: the payload destination
742 *
743 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
744 * otherwise.
745 */
746int batadv_recv_unicast_tvlv(struct sk_buff *skb,
747 struct batadv_hard_iface *recv_if)
748{
749 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
750 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
751 unsigned char *tvlv_buff;
752 uint16_t tvlv_buff_len;
753 int hdr_size = sizeof(*unicast_tvlv_packet);
754 int ret = NET_RX_DROP;
755
756 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
757 return NET_RX_DROP;
758
759 /* the header is likely to be modified while forwarding */
760 if (skb_cow(skb, hdr_size) < 0)
761 return NET_RX_DROP;
762
763 /* packet needs to be linearized to access the tvlv content */
764 if (skb_linearize(skb) < 0)
765 return NET_RX_DROP;
766
767 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
768
769 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
770 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
771
772 if (tvlv_buff_len > skb->len - hdr_size)
773 return NET_RX_DROP;
774
775 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
776 unicast_tvlv_packet->src,
777 unicast_tvlv_packet->dst,
778 tvlv_buff, tvlv_buff_len);
779
780 if (ret != NET_RX_SUCCESS)
781 ret = batadv_route_unicast_packet(skb, recv_if);
782
783 return ret;
784}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200786/**
787 * batadv_recv_frag_packet - process received fragment
788 * @skb: the received fragment
789 * @recv_if: interface that the skb is received on
790 *
791 * This function does one of the three following things: 1) Forward fragment, if
792 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
793 * lack further fragments; 3) Merge fragments, if we have all needed parts.
794 *
795 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
796 */
797int batadv_recv_frag_packet(struct sk_buff *skb,
798 struct batadv_hard_iface *recv_if)
799{
800 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
801 struct batadv_orig_node *orig_node_src = NULL;
802 struct batadv_frag_packet *frag_packet;
803 int ret = NET_RX_DROP;
804
805 if (batadv_check_unicast_packet(bat_priv, skb,
806 sizeof(*frag_packet)) < 0)
807 goto out;
808
809 frag_packet = (struct batadv_frag_packet *)skb->data;
810 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
811 if (!orig_node_src)
812 goto out;
813
814 /* Route the fragment if it is not for us and too big to be merged. */
815 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
816 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
817 ret = NET_RX_SUCCESS;
818 goto out;
819 }
820
821 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
822 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
823
824 /* Add fragment to buffer and merge if possible. */
825 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
826 goto out;
827
828 /* Deliver merged packet to the appropriate handler, if it was
829 * merged
830 */
831 if (skb)
832 batadv_batman_skb_recv(skb, recv_if->net_dev,
833 &recv_if->batman_adv_ptype, NULL);
834
835 ret = NET_RX_SUCCESS;
836
837out:
838 if (orig_node_src)
839 batadv_orig_node_free_ref(orig_node_src);
840
841 return ret;
842}
843
Sven Eckelmann56303d32012-06-05 22:31:31 +0200844int batadv_recv_bcast_packet(struct sk_buff *skb,
845 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200847 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
848 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200849 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200851 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000852 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853 int32_t seq_diff;
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200854 uint32_t seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000855
856 /* drop packet if it has not necessary minimum size */
857 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000858 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200860 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
862 /* packet with broadcast indication but unicast recipient */
863 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000864 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865
866 /* packet with broadcast sender address */
867 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000868 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869
870 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200871 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000872 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
Sven Eckelmann96412692012-06-05 22:31:30 +0200874 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
876 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200877 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000878 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100880 if (bcast_packet->ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +0000881 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882
Sven Eckelmannda641192012-05-12 13:48:56 +0200883 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000884
885 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200886 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000887
Marek Lindnerf3e00082011-01-25 21:52:11 +0000888 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200890 seqno = ntohl(bcast_packet->seqno);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +0200892 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200893 seqno))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000894 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200896 seq_diff = seqno - orig_node->last_bcast_seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000897
898 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200899 if (batadv_window_protected(bat_priv, seq_diff,
900 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +0000901 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902
903 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200904 * if required.
905 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +0200906 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Antonio Quartulli3fba7322013-10-13 02:50:17 +0200907 orig_node->last_bcast_seqno = seqno;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
Marek Lindnerf3e00082011-01-25 21:52:11 +0000909 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000910
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +0100911 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +0200912 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +0100913 goto out;
914
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200915 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
916
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200918 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919
Simon Wunderlich23721382012-01-22 20:00:19 +0100920 /* don't hand the broadcast up if it is from an originator
921 * from the same backbone.
922 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200923 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +0100924 goto out;
925
Antonio Quartullic384ea32011-06-26 03:37:18 +0200926 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
927 goto rx_success;
928 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
929 goto rx_success;
930
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931 /* broadcast for me */
Antonio Quartulli37135172012-07-05 23:38:30 +0200932 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
933 orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200934
935rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +0000936 ret = NET_RX_SUCCESS;
937 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000938
Marek Lindnerf3e00082011-01-25 21:52:11 +0000939spin_unlock:
940 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000941out:
942 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200943 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000944 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945}