blob: 5b78a71c1b02c45a0885b773fa09144dbeaa3248 [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
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "routing.h"
22#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "soft-interface.h"
24#include "hard-interface.h"
25#include "icmp_socket.h"
26#include "translation-table.h"
27#include "originator.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010028#include "bridge_loop_avoidance.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020029#include "distributed-arp-table.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010030#include "network-coding.h"
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +020031#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032
Antonio Quartullic018ad32013-06-04 12:11:39 +020033#include <linux/if_vlan.h>
34
Sven Eckelmann63b01032012-05-16 20:23:13 +020035static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +020036 struct batadv_hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010037
Sven Eckelmann56303d32012-06-05 22:31:31 +020038static void _batadv_update_route(struct batadv_priv *bat_priv,
39 struct batadv_orig_node *orig_node,
40 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041{
Sven Eckelmann56303d32012-06-05 22:31:31 +020042 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000043
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020044 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000045
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000047 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020048 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
49 "Deleting route towards: %pM\n", orig_node->orig);
Antonio Quartulli95fb1302013-08-07 18:28:55 +020050 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +020051 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000053 /* route added */
54 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020055 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020056 "Adding route towards: %pM (via %pM)\n",
57 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000058 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020059 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020060 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020061 "Changing route towards: %pM (now via %pM - was via %pM)\n",
62 orig_node->orig, neigh_node->addr,
63 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064 }
65
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000066 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020067 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000068
69 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000070 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
71 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000072
73 spin_lock_bh(&orig_node->neigh_list_lock);
74 rcu_assign_pointer(orig_node->router, neigh_node);
75 spin_unlock_bh(&orig_node->neigh_list_lock);
76
77 /* decrease refcount of previous best neighbor */
78 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020079 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080}
81
Sven Eckelmann56303d32012-06-05 22:31:31 +020082void batadv_update_route(struct batadv_priv *bat_priv,
83 struct batadv_orig_node *orig_node,
84 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085{
Sven Eckelmann56303d32012-06-05 22:31:31 +020086 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
88 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000089 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000090
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020091 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000092
93 if (router != neigh_node)
Sven Eckelmann63b01032012-05-16 20:23:13 +020094 _batadv_update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000095
96out:
97 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020098 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099}
100
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000101/* caller must hold the neigh_list_lock */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200102void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
103 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000104{
105 /* this neighbor is not part of our candidate list */
106 if (list_empty(&neigh_node->bonding_list))
107 goto out;
108
109 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000110 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200111 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000112 atomic_dec(&orig_node->bond_candidates);
113
114out:
115 return;
116}
117
Sven Eckelmann56303d32012-06-05 22:31:31 +0200118void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
119 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000120{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200121 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
Antonio Quartulli0538f752013-09-02 12:15:01 +0200122 uint8_t interference_candidate = 0, tq;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000123
124 spin_lock_bh(&orig_node->neigh_list_lock);
125
126 /* only consider if it has the same primary address ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200127 if (!batadv_compare_eth(orig_node->orig,
128 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000129 goto candidate_del;
130
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200131 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000132 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000133 goto candidate_del;
134
Antonio Quartulli0538f752013-09-02 12:15:01 +0200135
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000136 /* ... and is good enough to be considered */
Antonio Quartulli0538f752013-09-02 12:15:01 +0200137 tq = router->bat_iv.tq_avg - BATADV_BONDING_TQ_THRESHOLD;
138 if (neigh_node->bat_iv.tq_avg < tq)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000139 goto candidate_del;
140
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200141 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000142 * interface. If we do, we won't select this candidate because of
143 * possible interference.
144 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800145 hlist_for_each_entry_rcu(tmp_neigh_node,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000146 &orig_node->neigh_list, list) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000147 if (tmp_neigh_node == neigh_node)
148 continue;
149
150 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200151 * considered as candidate.
152 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000153 if (list_empty(&tmp_neigh_node->bonding_list))
154 continue;
155
156 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200157 (batadv_compare_eth(neigh_node->addr,
158 tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000159 interference_candidate = 1;
160 break;
161 }
162 }
163
164 /* don't care further if it is an interference candidate */
165 if (interference_candidate)
166 goto candidate_del;
167
168 /* this neighbor already is part of our candidate list */
169 if (!list_empty(&neigh_node->bonding_list))
170 goto out;
171
Marek Lindner44524fc2011-02-10 14:33:53 +0000172 if (!atomic_inc_not_zero(&neigh_node->refcount))
173 goto out;
174
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000175 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000176 atomic_inc(&orig_node->bond_candidates);
177 goto out;
178
179candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200180 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000181
182out:
183 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000184
185 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200186 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000187}
188
189/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200190void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200191batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
192 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200193 const struct batadv_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000194{
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200195 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000196 return;
197
198 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
199}
200
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201/* checks whether the host restarted and is in the protection time.
202 * returns:
203 * 0 if the packet is to be accepted
204 * 1 if the packet is to be ignored.
205 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200206int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200207 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200209 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
210 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
211 if (!batadv_has_timed_out(*last_reset,
212 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800214
215 *last_reset = jiffies;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200216 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200217 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800219
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 return 0;
221}
222
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200223bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200225 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 struct ethhdr *ethhdr;
228
229 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800230 if (unlikely(!pskb_may_pull(skb, header_len)))
231 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200233 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
235 /* packet with broadcast indication but unicast recipient */
236 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800237 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000238
239 /* packet with broadcast sender address */
240 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800241 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242
243 /* create a copy of the skb, if needed, to modify it. */
244 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800245 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246
247 /* keep skb linear */
248 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800249 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250
Marek Lindnerc3e29312012-03-04 16:56:25 +0800251 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252}
253
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200255 struct sk_buff *skb, size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200257 struct batadv_hard_iface *primary_if = NULL;
258 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200259 struct batadv_icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000260 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261
Sven Eckelmann96412692012-06-05 22:31:30 +0200262 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
264 /* add data to device queue */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200265 if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200266 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000267 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 }
269
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200270 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200271 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000272 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
274 /* answer echo request (ping) */
275 /* get routing information */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200276 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000277 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000278 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000279
Marek Lindner44524fc2011-02-10 14:33:53 +0000280 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100281 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000282 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283
Sven Eckelmann96412692012-06-05 22:31:30 +0200284 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200286 memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
287 memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
288 ETH_ALEN);
289 icmp_packet->icmph.msg_type = BATADV_ECHO_REPLY;
290 icmp_packet->icmph.header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200292 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200293 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294
Marek Lindner44524fc2011-02-10 14:33:53 +0000295out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200296 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200297 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000298 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200299 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 return ret;
301}
302
Sven Eckelmann56303d32012-06-05 22:31:31 +0200303static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200304 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200306 struct batadv_hard_iface *primary_if = NULL;
307 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200308 struct batadv_icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000309 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310
Sven Eckelmann96412692012-06-05 22:31:30 +0200311 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
313 /* send TTL exceeded if packet is an echo request (traceroute) */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200314 if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100315 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200316 icmp_packet->icmph.orig, icmp_packet->icmph.dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000317 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318 }
319
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200320 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200321 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000322 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323
324 /* get routing information */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200325 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000326 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000327 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000328
Marek Lindner44524fc2011-02-10 14:33:53 +0000329 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100330 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000331 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000332
Sven Eckelmann96412692012-06-05 22:31:30 +0200333 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200335 memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
336 memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
337 ETH_ALEN);
338 icmp_packet->icmph.msg_type = BATADV_TTL_EXCEEDED;
339 icmp_packet->icmph.header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200341 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200342 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343
Marek Lindner44524fc2011-02-10 14:33:53 +0000344out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200345 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200346 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000347 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200348 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 return ret;
350}
351
352
Sven Eckelmann56303d32012-06-05 22:31:31 +0200353int batadv_recv_icmp_packet(struct sk_buff *skb,
354 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200356 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200357 struct batadv_icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200359 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200360 int hdr_size = sizeof(struct batadv_icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000361 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000362
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200363 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmann96412692012-06-05 22:31:30 +0200364 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
365 hdr_size = sizeof(struct batadv_icmp_packet_rr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366
367 /* drop packet if it has not necessary minimum size */
368 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000369 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200371 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372
373 /* packet with unicast indication but broadcast recipient */
374 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000375 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376
377 /* packet with broadcast sender address */
378 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000379 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380
381 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200382 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000383 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384
Sven Eckelmann96412692012-06-05 22:31:30 +0200385 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
387 /* add record route information if not full */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200388 if ((icmp_packet->icmph.msg_type == BATADV_ECHO_REPLY ||
389 icmp_packet->icmph.msg_type == BATADV_ECHO_REQUEST) &&
Simon Wunderlich97dbc032013-04-25 10:37:26 +0200390 (hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200391 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100393 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394 icmp_packet->rr_cur++;
395 }
396
397 /* packet for me */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200398 if (batadv_is_my_mac(bat_priv, icmp_packet->icmph.dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200399 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
401 /* TTL exceeded */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200402 if (icmp_packet->icmph.header.ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200403 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405 /* get routing information */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200406 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000407 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000408 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000409
Marek Lindner44524fc2011-02-10 14:33:53 +0000410 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100411 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000412 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Sven Eckelmann96412692012-06-05 22:31:30 +0200414 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindner44524fc2011-02-10 14:33:53 +0000416 /* decrement ttl */
Antonio Quartulli0bf84c12013-05-18 14:56:57 +0200417 icmp_packet->icmph.header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418
Marek Lindner44524fc2011-02-10 14:33:53 +0000419 /* route it */
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200420 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200421 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
Marek Lindner44524fc2011-02-10 14:33:53 +0000423out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000424 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200425 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 return ret;
427}
428
Linus Lüssing55158622011-03-14 22:43:27 +0000429/* In the bonding case, send the packets in a round
430 * robin fashion over the remaining interfaces.
431 *
432 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200433 * returned router's refcount.
434 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200435static struct batadv_neigh_node *
436batadv_find_bond_router(struct batadv_orig_node *primary_orig,
437 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000438{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439 struct batadv_neigh_node *tmp_neigh_node;
440 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000441
442 rcu_read_lock();
443 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
444 bonding_list) {
445 if (!first_candidate)
446 first_candidate = tmp_neigh_node;
447
448 /* recv_if == NULL on the first node. */
449 if (tmp_neigh_node->if_incoming == recv_if)
450 continue;
451
452 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
453 continue;
454
455 router = tmp_neigh_node;
456 break;
457 }
458
459 /* use the first candidate if nothing was found. */
460 if (!router && first_candidate &&
461 atomic_inc_not_zero(&first_candidate->refcount))
462 router = first_candidate;
463
464 if (!router)
465 goto out;
466
467 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200468 * after the current router
469 */
Linus Lüssing55158622011-03-14 22:43:27 +0000470 spin_lock_bh(&primary_orig->neigh_list_lock);
471 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200472 * does not exist as rcu version
473 */
Linus Lüssing55158622011-03-14 22:43:27 +0000474 list_del_rcu(&primary_orig->bond_list);
475 list_add_rcu(&primary_orig->bond_list,
476 &router->bonding_list);
477 spin_unlock_bh(&primary_orig->neigh_list_lock);
478
479out:
480 rcu_read_unlock();
481 return router;
482}
483
484/* Interface Alternating: Use the best of the
485 * remaining candidates which are not using
486 * this interface.
487 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200488 * Increases the returned router's refcount
489 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200490static struct batadv_neigh_node *
491batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
492 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000493{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200494 struct batadv_neigh_node *tmp_neigh_node;
495 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000496
497 rcu_read_lock();
498 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
499 bonding_list) {
500 if (!first_candidate)
501 first_candidate = tmp_neigh_node;
502
503 /* recv_if == NULL on the first node. */
504 if (tmp_neigh_node->if_incoming == recv_if)
505 continue;
506
Antonio Quartulli0538f752013-09-02 12:15:01 +0200507 if (router &&
508 tmp_neigh_node->bat_iv.tq_avg <= router->bat_iv.tq_avg)
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200509 continue;
510
Linus Lüssing55158622011-03-14 22:43:27 +0000511 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
512 continue;
513
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200514 /* decrement refcount of previously selected router */
515 if (router)
516 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000517
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200518 /* we found a better router (or at least one valid router) */
519 router = tmp_neigh_node;
Linus Lüssing55158622011-03-14 22:43:27 +0000520 }
521
522 /* use the first candidate if nothing was found. */
523 if (!router && first_candidate &&
524 atomic_inc_not_zero(&first_candidate->refcount))
525 router = first_candidate;
526
527 rcu_read_unlock();
528 return router;
529}
530
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100531/**
532 * batadv_check_unicast_packet - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400533 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100534 * @skb: packet to check
535 * @hdr_size: size of header to pull
536 *
537 * Check for short header and bad addresses in given packet. Returns negative
538 * value when check fails and 0 otherwise. The negative value depends on the
539 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
540 * and -EREMOTE for non-local (other host) destination.
541 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200542static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
543 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200544{
545 struct ethhdr *ethhdr;
546
547 /* drop packet if it has not necessary minimum size */
548 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100549 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200550
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200551 ethhdr = eth_hdr(skb);
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200552
553 /* packet with unicast indication but broadcast recipient */
554 if (is_broadcast_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100555 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200556
557 /* packet with broadcast sender address */
558 if (is_broadcast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100559 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200560
561 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200562 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100563 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200564
565 return 0;
566}
567
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000569 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200570 * refcount.
571 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200572struct batadv_neigh_node *
573batadv_find_router(struct batadv_priv *bat_priv,
574 struct batadv_orig_node *orig_node,
575 const struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200577 struct batadv_orig_node *primary_orig_node;
578 struct batadv_orig_node *router_orig;
579 struct batadv_neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
581 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200582 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583
584 if (!orig_node)
585 return NULL;
586
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200587 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000588 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200589 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590
591 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200592 * always choose the default router.
593 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594 bonding_enabled = atomic_read(&bat_priv->bonding);
595
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000596 rcu_read_lock();
597 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000598 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200599 if (!router_orig)
600 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000601
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000602 if ((!recv_if) && (!bonding_enabled))
603 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604
Sven Eckelmannda641192012-05-12 13:48:56 +0200605 primary_addr = router_orig->primary_addr;
606
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200608 * for a potential bonding candidate.
609 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200610 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000611 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612
613 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200614 * even be the same as our router_orig in many cases
615 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200616 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 primary_orig_node = router_orig;
618 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200619 primary_orig_node = batadv_orig_hash_find(bat_priv,
620 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000622 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000623
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200624 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 }
626
627 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200628 * bonding and prefer the original router.
629 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000630 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
631 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 /* all nodes between should choose a candidate which
634 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200635 * in.
636 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200637 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000638
Linus Lüssing55158622011-03-14 22:43:27 +0000639 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200640 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000641 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200642 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000643
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000644return_router:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200645 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200646 goto err_unlock;
647
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000648 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200650err_unlock:
651 rcu_read_unlock();
652err:
653 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200654 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200655 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656}
657
Sven Eckelmann63b01032012-05-16 20:23:13 +0200658static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200659 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200661 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
662 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200663 struct batadv_unicast_packet *unicast_packet;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200664 struct ethhdr *ethhdr = eth_hdr(skb);
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200665 int res, hdr_len, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666
Sven Eckelmann96412692012-06-05 22:31:30 +0200667 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668
669 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100670 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100671 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
672 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000673 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000674 }
675
676 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200677 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000678
Marek Lindner44524fc2011-02-10 14:33:53 +0000679 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200680 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100683 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000684 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686 /* decrement ttl */
Martin Hundebøllf097e252013-05-23 16:53:01 +0200687 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100688 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200690 switch (unicast_packet->header.packet_type) {
691 case BATADV_UNICAST_4ADDR:
692 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
693 break;
694 case BATADV_UNICAST:
695 hdr_len = sizeof(struct batadv_unicast_packet);
696 break;
697 default:
698 /* other packet types not supported - yet */
699 hdr_len = -1;
700 break;
701 }
702
703 if (hdr_len > 0)
704 batadv_skb_set_priority(skb, hdr_len);
705
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200706 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Martin Hundebøll95332472013-01-25 11:12:40 +0100707
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200708 /* translate transmit result into receive result */
709 if (res == NET_XMIT_SUCCESS) {
710 /* skb was transmitted and consumed */
Martin Hundebøll95332472013-01-25 11:12:40 +0100711 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
712 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
713 skb->len + ETH_HLEN);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200714
715 ret = NET_RX_SUCCESS;
716 } else if (res == NET_XMIT_POLICED) {
717 /* skb was buffered and consumed */
718 ret = NET_RX_SUCCESS;
Martin Hundebøll95332472013-01-25 11:12:40 +0100719 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720
Marek Lindner44524fc2011-02-10 14:33:53 +0000721out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000722 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200723 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000724 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725}
726
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200727/**
728 * batadv_reroute_unicast_packet - update the unicast header for re-routing
729 * @bat_priv: the bat priv with all the soft interface information
730 * @unicast_packet: the unicast header to be updated
731 * @dst_addr: the payload destination
Antonio Quartullic018ad32013-06-04 12:11:39 +0200732 * @vid: VLAN identifier
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200733 *
734 * Search the translation table for dst_addr and update the unicast header with
735 * the new corresponding information (originator address where the destination
736 * client currently is and its known TTVN)
737 *
738 * Returns true if the packet header has been updated, false otherwise
739 */
740static bool
741batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
742 struct batadv_unicast_packet *unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200743 uint8_t *dst_addr, unsigned short vid)
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200744{
745 struct batadv_orig_node *orig_node = NULL;
746 struct batadv_hard_iface *primary_if = NULL;
747 bool ret = false;
748 uint8_t *orig_addr, orig_ttvn;
749
Antonio Quartullic018ad32013-06-04 12:11:39 +0200750 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200751 primary_if = batadv_primary_if_get_selected(bat_priv);
752 if (!primary_if)
753 goto out;
754 orig_addr = primary_if->net_dev->dev_addr;
755 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
756 } else {
Antonio Quartullic018ad32013-06-04 12:11:39 +0200757 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
758 vid);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200759 if (!orig_node)
760 goto out;
761
762 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
763 goto out;
764
765 orig_addr = orig_node->orig;
766 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
767 }
768
769 /* update the packet header */
770 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
771 unicast_packet->ttvn = orig_ttvn;
772
773 ret = true;
774out:
775 if (primary_if)
776 batadv_hardif_free_ref(primary_if);
777 if (orig_node)
778 batadv_orig_node_free_ref(orig_node);
779
780 return ret;
781}
782
Sven Eckelmann56303d32012-06-05 22:31:31 +0200783static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200784 struct sk_buff *skb, int hdr_len) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200785 struct batadv_unicast_packet *unicast_packet;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200786 struct batadv_hard_iface *primary_if;
787 struct batadv_orig_node *orig_node;
788 uint8_t curr_ttvn, old_ttvn;
789 struct ethhdr *ethhdr;
790 unsigned short vid;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200791 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200792
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200793 /* check if there is enough data before accessing it */
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200794 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200795 return 0;
796
797 /* create a copy of the skb (in case of for re-routing) to modify it. */
798 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200799 return 0;
800
Sven Eckelmann96412692012-06-05 22:31:30 +0200801 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullic018ad32013-06-04 12:11:39 +0200802 vid = batadv_get_vid(skb, hdr_len);
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200803 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200804
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200805 /* check if the destination client was served by this node and it is now
806 * roaming. In this case, it means that the node has got a ROAM_ADV
807 * message and that it knows the new destination in the mesh to re-route
808 * the packet to
809 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200810 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200811 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200812 ethhdr->h_dest, vid))
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200813 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
814 bat_priv,
815 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
816 unicast_packet->dest,
817 ethhdr->h_dest);
818 /* at this point the mesh destination should have been
819 * substituted with the originator address found in the global
820 * table. If not, let the packet go untouched anyway because
821 * there is nothing the node can do
822 */
823 return 1;
824 }
825
826 /* retrieve the TTVN known by this node for the packet destination. This
827 * value is used later to check if the node which sent (or re-routed
828 * last time) the packet had an updated information or not
829 */
830 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200831 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200832 orig_node = batadv_orig_hash_find(bat_priv,
833 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200834 /* if it is not possible to find the orig_node representing the
835 * destination, the packet can immediately be dropped as it will
836 * not be possible to deliver it
837 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200838 if (!orig_node)
839 return 0;
840
841 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200842 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200843 }
844
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200845 /* check if the TTVN contained in the packet is fresher than what the
846 * node knows
847 */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200848 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200849 if (!is_old_ttvn)
850 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200851
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200852 old_ttvn = unicast_packet->ttvn;
853 /* the packet was forged based on outdated network information. Its
854 * destination can possibly be updated and forwarded towards the new
855 * target host
856 */
857 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
Antonio Quartullic018ad32013-06-04 12:11:39 +0200858 ethhdr->h_dest, vid)) {
Antonio Quartulli60c39c72012-08-26 01:05:56 +0200859 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200860 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
861 unicast_packet->dest, ethhdr->h_dest,
862 old_ttvn, curr_ttvn);
863 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200864 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200865
866 /* the packet has not been re-routed: either the destination is
867 * currently served by this node or there is no destination at all and
868 * it is possible to drop the packet
869 */
Antonio Quartullic018ad32013-06-04 12:11:39 +0200870 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200871 return 0;
872
873 /* update the header in order to let the packet be delivered to this
874 * node's soft interface
875 */
876 primary_if = batadv_primary_if_get_selected(bat_priv);
877 if (!primary_if)
878 return 0;
879
880 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
881
882 batadv_hardif_free_ref(primary_if);
883
884 unicast_packet->ttvn = curr_ttvn;
885
Antonio Quartullia73105b2011-04-27 14:27:44 +0200886 return 1;
887}
888
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200889/**
890 * batadv_recv_unhandled_unicast_packet - receive and process packets which
891 * are in the unicast number space but not yet known to the implementation
892 * @skb: unicast tvlv packet to process
893 * @recv_if: pointer to interface this packet was received on
894 *
895 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
896 * otherwise.
897 */
898int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
899 struct batadv_hard_iface *recv_if)
900{
901 struct batadv_unicast_packet *unicast_packet;
902 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
903 int check, hdr_size = sizeof(*unicast_packet);
904
905 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
906 if (check < 0)
907 return NET_RX_DROP;
908
909 /* we don't know about this type, drop it. */
910 unicast_packet = (struct batadv_unicast_packet *)skb->data;
911 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
912 return NET_RX_DROP;
913
914 return batadv_route_unicast_packet(skb, recv_if);
915}
916
Sven Eckelmann56303d32012-06-05 22:31:31 +0200917int batadv_recv_unicast_packet(struct sk_buff *skb,
918 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200920 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200921 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200922 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Antonio Quartulli9affec62012-10-14 17:19:19 +0200923 uint8_t *orig_addr;
924 struct batadv_orig_node *orig_node = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100925 int check, hdr_size = sizeof(*unicast_packet);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200926 bool is4addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000927
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200928 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200929 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200930
Antonio Quartullic384ea32011-06-26 03:37:18 +0200931 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200932 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200933 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +0200934 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200935
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100936 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -0400937 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100938
939 /* Even though the packet is not for us, we might save it to use for
940 * decoding a later received coded packet
941 */
942 if (check == -EREMOTE)
943 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
944
945 if (check < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000946 return NET_RX_DROP;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200947 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200948 return NET_RX_DROP;
949
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200951 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Antonio Quartulli9affec62012-10-14 17:19:19 +0200952 if (is4addr) {
Martin Hundebøll4046b242012-04-20 17:02:45 +0200953 batadv_dat_inc_counter(bat_priv,
954 unicast_4addr_packet->subtype);
Antonio Quartulli9affec62012-10-14 17:19:19 +0200955 orig_addr = unicast_4addr_packet->src;
956 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
957 }
Martin Hundebøll4046b242012-04-20 17:02:45 +0200958
Antonio Quartullic384ea32011-06-26 03:37:18 +0200959 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
960 hdr_size))
961 goto rx_success;
962 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
963 hdr_size))
964 goto rx_success;
965
Antonio Quartulli37135172012-07-05 23:38:30 +0200966 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +0200967 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +0200968
Antonio Quartullic384ea32011-06-26 03:37:18 +0200969rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +0200970 if (orig_node)
971 batadv_orig_node_free_ref(orig_node);
972
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000973 return NET_RX_SUCCESS;
974 }
975
Sven Eckelmann63b01032012-05-16 20:23:13 +0200976 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977}
978
Marek Lindneref261572013-04-23 21:39:57 +0800979/**
980 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
981 * @skb: unicast tvlv packet to process
982 * @recv_if: pointer to interface this packet was received on
983 * @dst_addr: the payload destination
984 *
985 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
986 * otherwise.
987 */
988int batadv_recv_unicast_tvlv(struct sk_buff *skb,
989 struct batadv_hard_iface *recv_if)
990{
991 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
992 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
993 unsigned char *tvlv_buff;
994 uint16_t tvlv_buff_len;
995 int hdr_size = sizeof(*unicast_tvlv_packet);
996 int ret = NET_RX_DROP;
997
998 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
999 return NET_RX_DROP;
1000
1001 /* the header is likely to be modified while forwarding */
1002 if (skb_cow(skb, hdr_size) < 0)
1003 return NET_RX_DROP;
1004
1005 /* packet needs to be linearized to access the tvlv content */
1006 if (skb_linearize(skb) < 0)
1007 return NET_RX_DROP;
1008
1009 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1010
1011 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1012 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1013
1014 if (tvlv_buff_len > skb->len - hdr_size)
1015 return NET_RX_DROP;
1016
1017 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1018 unicast_tvlv_packet->src,
1019 unicast_tvlv_packet->dst,
1020 tvlv_buff, tvlv_buff_len);
1021
1022 if (ret != NET_RX_SUCCESS)
1023 ret = batadv_route_unicast_packet(skb, recv_if);
1024
1025 return ret;
1026}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001027
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001028/**
1029 * batadv_recv_frag_packet - process received fragment
1030 * @skb: the received fragment
1031 * @recv_if: interface that the skb is received on
1032 *
1033 * This function does one of the three following things: 1) Forward fragment, if
1034 * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
1035 * lack further fragments; 3) Merge fragments, if we have all needed parts.
1036 *
1037 * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
1038 */
1039int batadv_recv_frag_packet(struct sk_buff *skb,
1040 struct batadv_hard_iface *recv_if)
1041{
1042 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1043 struct batadv_orig_node *orig_node_src = NULL;
1044 struct batadv_frag_packet *frag_packet;
1045 int ret = NET_RX_DROP;
1046
1047 if (batadv_check_unicast_packet(bat_priv, skb,
1048 sizeof(*frag_packet)) < 0)
1049 goto out;
1050
1051 frag_packet = (struct batadv_frag_packet *)skb->data;
1052 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1053 if (!orig_node_src)
1054 goto out;
1055
1056 /* Route the fragment if it is not for us and too big to be merged. */
1057 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1058 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1059 ret = NET_RX_SUCCESS;
1060 goto out;
1061 }
1062
1063 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1064 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1065
1066 /* Add fragment to buffer and merge if possible. */
1067 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1068 goto out;
1069
1070 /* Deliver merged packet to the appropriate handler, if it was
1071 * merged
1072 */
1073 if (skb)
1074 batadv_batman_skb_recv(skb, recv_if->net_dev,
1075 &recv_if->batman_adv_ptype, NULL);
1076
1077 ret = NET_RX_SUCCESS;
1078
1079out:
1080 if (orig_node_src)
1081 batadv_orig_node_free_ref(orig_node_src);
1082
1083 return ret;
1084}
1085
Sven Eckelmann56303d32012-06-05 22:31:31 +02001086int batadv_recv_bcast_packet(struct sk_buff *skb,
1087 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001088{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001089 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1090 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001091 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001092 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001093 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001094 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001095 int32_t seq_diff;
1096
1097 /* drop packet if it has not necessary minimum size */
1098 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001099 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001100
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001101 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102
1103 /* packet with broadcast indication but unicast recipient */
1104 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001105 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001106
1107 /* packet with broadcast sender address */
1108 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001109 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001110
1111 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001112 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001113 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001114
Sven Eckelmann96412692012-06-05 22:31:30 +02001115 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001116
1117 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001118 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001119 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001120
Sven Eckelmann76543d12011-11-20 15:47:38 +01001121 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001122 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001123
Sven Eckelmannda641192012-05-12 13:48:56 +02001124 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001125
1126 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001127 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128
Marek Lindnerf3e00082011-01-25 21:52:11 +00001129 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130
1131 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001132 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1133 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001134 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001135
1136 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1137
1138 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001139 if (batadv_window_protected(bat_priv, seq_diff,
1140 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001141 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001142
1143 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001144 * if required.
1145 */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001146 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1148
Marek Lindnerf3e00082011-01-25 21:52:11 +00001149 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001150
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001151 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001152 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001153 goto out;
1154
Simon Wunderlichc54f38c2013-07-29 17:56:44 +02001155 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1156
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001157 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001158 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001159
Simon Wunderlich23721382012-01-22 20:00:19 +01001160 /* don't hand the broadcast up if it is from an originator
1161 * from the same backbone.
1162 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001163 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001164 goto out;
1165
Antonio Quartullic384ea32011-06-26 03:37:18 +02001166 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1167 goto rx_success;
1168 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1169 goto rx_success;
1170
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001171 /* broadcast for me */
Antonio Quartulli37135172012-07-05 23:38:30 +02001172 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1173 orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001174
1175rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +00001176 ret = NET_RX_SUCCESS;
1177 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178
Marek Lindnerf3e00082011-01-25 21:52:11 +00001179spin_unlock:
1180 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001181out:
1182 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001183 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001184 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001185}