blob: fd2cdbc3ea85cbd2e8200f492c3cc0cb47949aae [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"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031
Sven Eckelmann63b01032012-05-16 20:23:13 +020032static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +020033 struct batadv_hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010034
Sven Eckelmann56303d32012-06-05 22:31:31 +020035static void _batadv_update_route(struct batadv_priv *bat_priv,
36 struct batadv_orig_node *orig_node,
37 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000040
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020041 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000042
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000044 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020045 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
46 "Deleting route towards: %pM\n", orig_node->orig);
Sven Eckelmann08c36d32012-05-12 02:09:39 +020047 batadv_tt_global_del_orig(bat_priv, orig_node,
48 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000050 /* route added */
51 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020052 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020053 "Adding route towards: %pM (via %pM)\n",
54 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000055 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020056 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020057 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020058 "Changing route towards: %pM (now via %pM - was via %pM)\n",
59 orig_node->orig, neigh_node->addr,
60 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 }
62
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000063 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020064 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000065
66 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000067 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
68 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000069
70 spin_lock_bh(&orig_node->neigh_list_lock);
71 rcu_assign_pointer(orig_node->router, neigh_node);
72 spin_unlock_bh(&orig_node->neigh_list_lock);
73
74 /* decrease refcount of previous best neighbor */
75 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020076 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077}
78
Sven Eckelmann56303d32012-06-05 22:31:31 +020079void batadv_update_route(struct batadv_priv *bat_priv,
80 struct batadv_orig_node *orig_node,
81 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
Sven Eckelmann56303d32012-06-05 22:31:31 +020083 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084
85 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000086 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020088 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000089
90 if (router != neigh_node)
Sven Eckelmann63b01032012-05-16 20:23:13 +020091 _batadv_update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000092
93out:
94 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020095 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096}
97
Simon Wunderlicha4c135c2011-01-19 20:01:43 +000098/* caller must hold the neigh_list_lock */
Sven Eckelmann56303d32012-06-05 22:31:31 +020099void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
100 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000101{
102 /* this neighbor is not part of our candidate list */
103 if (list_empty(&neigh_node->bonding_list))
104 goto out;
105
106 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000107 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200108 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000109 atomic_dec(&orig_node->bond_candidates);
110
111out:
112 return;
113}
114
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
116 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000117{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200118 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000119 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000120
121 spin_lock_bh(&orig_node->neigh_list_lock);
122
123 /* only consider if it has the same primary address ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200124 if (!batadv_compare_eth(orig_node->orig,
125 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000126 goto candidate_del;
127
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200128 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000129 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000130 goto candidate_del;
131
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000132 /* ... and is good enough to be considered */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200133 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000134 goto candidate_del;
135
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200136 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000137 * interface. If we do, we won't select this candidate because of
138 * possible interference.
139 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800140 hlist_for_each_entry_rcu(tmp_neigh_node,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000141 &orig_node->neigh_list, list) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000142 if (tmp_neigh_node == neigh_node)
143 continue;
144
145 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200146 * considered as candidate.
147 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000148 if (list_empty(&tmp_neigh_node->bonding_list))
149 continue;
150
151 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200152 (batadv_compare_eth(neigh_node->addr,
153 tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000154 interference_candidate = 1;
155 break;
156 }
157 }
158
159 /* don't care further if it is an interference candidate */
160 if (interference_candidate)
161 goto candidate_del;
162
163 /* this neighbor already is part of our candidate list */
164 if (!list_empty(&neigh_node->bonding_list))
165 goto out;
166
Marek Lindner44524fc2011-02-10 14:33:53 +0000167 if (!atomic_inc_not_zero(&neigh_node->refcount))
168 goto out;
169
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000170 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000171 atomic_inc(&orig_node->bond_candidates);
172 goto out;
173
174candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200175 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000176
177out:
178 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000179
180 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200181 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000182}
183
184/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200185void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200186batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
187 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200188 const struct batadv_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000189{
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200190 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000191 return;
192
193 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
194}
195
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196/* checks whether the host restarted and is in the protection time.
197 * returns:
198 * 0 if the packet is to be accepted
199 * 1 if the packet is to be ignored.
200 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200201int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200202 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200204 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
205 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
206 if (!batadv_has_timed_out(*last_reset,
207 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800209
210 *last_reset = jiffies;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200211 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200212 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000213 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800214
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215 return 0;
216}
217
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200218bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200219 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200220 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 struct ethhdr *ethhdr;
223
224 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800225 if (unlikely(!pskb_may_pull(skb, header_len)))
226 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200228 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
230 /* packet with broadcast indication but unicast recipient */
231 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800232 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
234 /* packet with broadcast sender address */
235 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800236 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
238 /* create a copy of the skb, if needed, to modify it. */
239 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800240 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
242 /* keep skb linear */
243 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800244 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
Marek Lindnerc3e29312012-03-04 16:56:25 +0800246 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000247}
248
Sven Eckelmann56303d32012-06-05 22:31:31 +0200249static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200250 struct sk_buff *skb, size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200252 struct batadv_hard_iface *primary_if = NULL;
253 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200254 struct batadv_icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000255 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
Sven Eckelmann96412692012-06-05 22:31:30 +0200257 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
259 /* add data to device queue */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200260 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200261 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000262 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 }
264
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200265 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200266 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000267 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
269 /* answer echo request (ping) */
270 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200271 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000272 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000273 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000274
Marek Lindner44524fc2011-02-10 14:33:53 +0000275 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100276 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000277 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278
Sven Eckelmann96412692012-06-05 22:31:30 +0200279 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280
Marek Lindner44524fc2011-02-10 14:33:53 +0000281 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200282 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200283 icmp_packet->msg_type = BATADV_ECHO_REPLY;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200284 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200286 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200287 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288
Marek Lindner44524fc2011-02-10 14:33:53 +0000289out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200290 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200291 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000292 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200293 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 return ret;
295}
296
Sven Eckelmann56303d32012-06-05 22:31:31 +0200297static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200298 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200300 struct batadv_hard_iface *primary_if = NULL;
301 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200302 struct batadv_icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000303 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
Sven Eckelmann96412692012-06-05 22:31:30 +0200305 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306
307 /* send TTL exceeded if packet is an echo request (traceroute) */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200308 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100309 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
310 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000311 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312 }
313
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200314 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200315 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000316 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317
318 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200319 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000320 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000321 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000322
Marek Lindner44524fc2011-02-10 14:33:53 +0000323 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100324 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000325 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
Sven Eckelmann96412692012-06-05 22:31:30 +0200327 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
Marek Lindner44524fc2011-02-10 14:33:53 +0000329 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200330 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200331 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200332 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200334 if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200335 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336
Marek Lindner44524fc2011-02-10 14:33:53 +0000337out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200338 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200339 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000340 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200341 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 return ret;
343}
344
345
Sven Eckelmann56303d32012-06-05 22:31:31 +0200346int batadv_recv_icmp_packet(struct sk_buff *skb,
347 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200349 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200350 struct batadv_icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200352 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200353 int hdr_size = sizeof(struct batadv_icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000354 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200356 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmann96412692012-06-05 22:31:30 +0200357 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
358 hdr_size = sizeof(struct batadv_icmp_packet_rr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
360 /* drop packet if it has not necessary minimum size */
361 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000362 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200364 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365
366 /* packet with unicast indication but broadcast recipient */
367 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000368 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369
370 /* packet with broadcast sender address */
371 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000372 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
374 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200375 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000376 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
Sven Eckelmann96412692012-06-05 22:31:30 +0200378 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
380 /* add record route information if not full */
Simon Wunderlich97dbc032013-04-25 10:37:26 +0200381 if ((icmp_packet->msg_type == BATADV_ECHO_REPLY ||
382 icmp_packet->msg_type == BATADV_ECHO_REQUEST) &&
383 (hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200384 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100386 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 icmp_packet->rr_cur++;
388 }
389
390 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200391 if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200392 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393
394 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100395 if (icmp_packet->header.ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200396 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200399 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000400 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000401 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000402
Marek Lindner44524fc2011-02-10 14:33:53 +0000403 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100404 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000405 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406
Sven Eckelmann96412692012-06-05 22:31:30 +0200407 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Marek Lindner44524fc2011-02-10 14:33:53 +0000409 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100410 icmp_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411
Marek Lindner44524fc2011-02-10 14:33:53 +0000412 /* route it */
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200413 if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200414 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
Marek Lindner44524fc2011-02-10 14:33:53 +0000416out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000417 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200418 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419 return ret;
420}
421
Linus Lüssing55158622011-03-14 22:43:27 +0000422/* In the bonding case, send the packets in a round
423 * robin fashion over the remaining interfaces.
424 *
425 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200426 * returned router's refcount.
427 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200428static struct batadv_neigh_node *
429batadv_find_bond_router(struct batadv_orig_node *primary_orig,
430 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000431{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200432 struct batadv_neigh_node *tmp_neigh_node;
433 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000434
435 rcu_read_lock();
436 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
437 bonding_list) {
438 if (!first_candidate)
439 first_candidate = tmp_neigh_node;
440
441 /* recv_if == NULL on the first node. */
442 if (tmp_neigh_node->if_incoming == recv_if)
443 continue;
444
445 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
446 continue;
447
448 router = tmp_neigh_node;
449 break;
450 }
451
452 /* use the first candidate if nothing was found. */
453 if (!router && first_candidate &&
454 atomic_inc_not_zero(&first_candidate->refcount))
455 router = first_candidate;
456
457 if (!router)
458 goto out;
459
460 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200461 * after the current router
462 */
Linus Lüssing55158622011-03-14 22:43:27 +0000463 spin_lock_bh(&primary_orig->neigh_list_lock);
464 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200465 * does not exist as rcu version
466 */
Linus Lüssing55158622011-03-14 22:43:27 +0000467 list_del_rcu(&primary_orig->bond_list);
468 list_add_rcu(&primary_orig->bond_list,
469 &router->bonding_list);
470 spin_unlock_bh(&primary_orig->neigh_list_lock);
471
472out:
473 rcu_read_unlock();
474 return router;
475}
476
477/* Interface Alternating: Use the best of the
478 * remaining candidates which are not using
479 * this interface.
480 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200481 * Increases the returned router's refcount
482 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200483static struct batadv_neigh_node *
484batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
485 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000486{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200487 struct batadv_neigh_node *tmp_neigh_node;
488 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000489
490 rcu_read_lock();
491 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
492 bonding_list) {
493 if (!first_candidate)
494 first_candidate = tmp_neigh_node;
495
496 /* recv_if == NULL on the first node. */
497 if (tmp_neigh_node->if_incoming == recv_if)
498 continue;
499
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200500 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
501 continue;
502
Linus Lüssing55158622011-03-14 22:43:27 +0000503 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
504 continue;
505
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200506 /* decrement refcount of previously selected router */
507 if (router)
508 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000509
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200510 /* we found a better router (or at least one valid router) */
511 router = tmp_neigh_node;
Linus Lüssing55158622011-03-14 22:43:27 +0000512 }
513
514 /* use the first candidate if nothing was found. */
515 if (!router && first_candidate &&
516 atomic_inc_not_zero(&first_candidate->refcount))
517 router = first_candidate;
518
519 rcu_read_unlock();
520 return router;
521}
522
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100523/**
524 * batadv_check_unicast_packet - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400525 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100526 * @skb: packet to check
527 * @hdr_size: size of header to pull
528 *
529 * Check for short header and bad addresses in given packet. Returns negative
530 * value when check fails and 0 otherwise. The negative value depends on the
531 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
532 * and -EREMOTE for non-local (other host) destination.
533 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200534static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
535 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200536{
537 struct ethhdr *ethhdr;
538
539 /* drop packet if it has not necessary minimum size */
540 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100541 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200542
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200543 ethhdr = eth_hdr(skb);
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200544
545 /* packet with unicast indication but broadcast recipient */
546 if (is_broadcast_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100547 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200548
549 /* packet with broadcast sender address */
550 if (is_broadcast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100551 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200552
553 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200554 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100555 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200556
557 return 0;
558}
559
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000561 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200562 * refcount.
563 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200564struct batadv_neigh_node *
565batadv_find_router(struct batadv_priv *bat_priv,
566 struct batadv_orig_node *orig_node,
567 const struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000568{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200569 struct batadv_orig_node *primary_orig_node;
570 struct batadv_orig_node *router_orig;
571 struct batadv_neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
573 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200574 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575
576 if (!orig_node)
577 return NULL;
578
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200579 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000580 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200581 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582
583 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200584 * always choose the default router.
585 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586 bonding_enabled = atomic_read(&bat_priv->bonding);
587
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000588 rcu_read_lock();
589 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000590 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200591 if (!router_orig)
592 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000593
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000594 if ((!recv_if) && (!bonding_enabled))
595 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
Sven Eckelmannda641192012-05-12 13:48:56 +0200597 primary_addr = router_orig->primary_addr;
598
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200600 * for a potential bonding candidate.
601 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200602 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000603 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604
605 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200606 * even be the same as our router_orig in many cases
607 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200608 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609 primary_orig_node = router_orig;
610 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200611 primary_orig_node = batadv_orig_hash_find(bat_priv,
612 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000614 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000615
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200616 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 }
618
619 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200620 * bonding and prefer the original router.
621 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000622 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
623 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 /* all nodes between should choose a candidate which
626 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200627 * in.
628 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200629 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630
Linus Lüssing55158622011-03-14 22:43:27 +0000631 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200632 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000633 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200634 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000635
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000636return_router:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200637 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200638 goto err_unlock;
639
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000640 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200642err_unlock:
643 rcu_read_unlock();
644err:
645 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200646 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200647 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648}
649
Sven Eckelmann63b01032012-05-16 20:23:13 +0200650static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200651 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200653 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
654 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200655 struct batadv_unicast_packet *unicast_packet;
Antonio Quartulli7ed4be92013-04-08 15:08:18 +0200656 struct ethhdr *ethhdr = eth_hdr(skb);
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200657 int res, hdr_len, ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Sven Eckelmann96412692012-06-05 22:31:30 +0200659 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660
661 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100662 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100663 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
664 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000665 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666 }
667
668 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200669 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000670
Marek Lindner44524fc2011-02-10 14:33:53 +0000671 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200672 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000674 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100675 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000676 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678 /* decrement ttl */
Martin Hundebøllf097e252013-05-23 16:53:01 +0200679 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100680 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200682 switch (unicast_packet->header.packet_type) {
683 case BATADV_UNICAST_4ADDR:
684 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
685 break;
686 case BATADV_UNICAST:
687 hdr_len = sizeof(struct batadv_unicast_packet);
688 break;
689 default:
690 /* other packet types not supported - yet */
691 hdr_len = -1;
692 break;
693 }
694
695 if (hdr_len > 0)
696 batadv_skb_set_priority(skb, hdr_len);
697
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200698 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
Martin Hundebøll95332472013-01-25 11:12:40 +0100699
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200700 /* translate transmit result into receive result */
701 if (res == NET_XMIT_SUCCESS) {
702 /* skb was transmitted and consumed */
Martin Hundebøll95332472013-01-25 11:12:40 +0100703 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
704 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
705 skb->len + ETH_HLEN);
Martin Hundebølle91ecfc2013-04-20 13:54:39 +0200706
707 ret = NET_RX_SUCCESS;
708 } else if (res == NET_XMIT_POLICED) {
709 /* skb was buffered and consumed */
710 ret = NET_RX_SUCCESS;
Martin Hundebøll95332472013-01-25 11:12:40 +0100711 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000712
Marek Lindner44524fc2011-02-10 14:33:53 +0000713out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000714 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200715 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000716 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717}
718
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200719/**
720 * batadv_reroute_unicast_packet - update the unicast header for re-routing
721 * @bat_priv: the bat priv with all the soft interface information
722 * @unicast_packet: the unicast header to be updated
723 * @dst_addr: the payload destination
724 *
725 * Search the translation table for dst_addr and update the unicast header with
726 * the new corresponding information (originator address where the destination
727 * client currently is and its known TTVN)
728 *
729 * Returns true if the packet header has been updated, false otherwise
730 */
731static bool
732batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
733 struct batadv_unicast_packet *unicast_packet,
734 uint8_t *dst_addr)
735{
736 struct batadv_orig_node *orig_node = NULL;
737 struct batadv_hard_iface *primary_if = NULL;
738 bool ret = false;
739 uint8_t *orig_addr, orig_ttvn;
740
741 if (batadv_is_my_client(bat_priv, dst_addr)) {
742 primary_if = batadv_primary_if_get_selected(bat_priv);
743 if (!primary_if)
744 goto out;
745 orig_addr = primary_if->net_dev->dev_addr;
746 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
747 } else {
748 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
749 if (!orig_node)
750 goto out;
751
752 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
753 goto out;
754
755 orig_addr = orig_node->orig;
756 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
757 }
758
759 /* update the packet header */
760 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
761 unicast_packet->ttvn = orig_ttvn;
762
763 ret = true;
764out:
765 if (primary_if)
766 batadv_hardif_free_ref(primary_if);
767 if (orig_node)
768 batadv_orig_node_free_ref(orig_node);
769
770 return ret;
771}
772
Sven Eckelmann56303d32012-06-05 22:31:31 +0200773static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200774 struct sk_buff *skb, int hdr_len) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200775 uint8_t curr_ttvn, old_ttvn;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200776 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200777 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200778 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200779 struct batadv_unicast_packet *unicast_packet;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200780 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200781
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200782 /* check if there is enough data before accessing it */
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200783 if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200784 return 0;
785
786 /* create a copy of the skb (in case of for re-routing) to modify it. */
787 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200788 return 0;
789
Sven Eckelmann96412692012-06-05 22:31:30 +0200790 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200791 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200792
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200793 /* check if the destination client was served by this node and it is now
794 * roaming. In this case, it means that the node has got a ROAM_ADV
795 * message and that it knows the new destination in the mesh to re-route
796 * the packet to
797 */
798 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
799 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
800 ethhdr->h_dest))
801 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
802 bat_priv,
803 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
804 unicast_packet->dest,
805 ethhdr->h_dest);
806 /* at this point the mesh destination should have been
807 * substituted with the originator address found in the global
808 * table. If not, let the packet go untouched anyway because
809 * there is nothing the node can do
810 */
811 return 1;
812 }
813
814 /* retrieve the TTVN known by this node for the packet destination. This
815 * value is used later to check if the node which sent (or re-routed
816 * last time) the packet had an updated information or not
817 */
818 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200819 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200820 orig_node = batadv_orig_hash_find(bat_priv,
821 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200822 /* if it is not possible to find the orig_node representing the
823 * destination, the packet can immediately be dropped as it will
824 * not be possible to deliver it
825 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200826 if (!orig_node)
827 return 0;
828
829 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200830 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200831 }
832
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200833 /* check if the TTVN contained in the packet is fresher than what the
834 * node knows
835 */
Sven Eckelmann3e348192012-05-16 20:23:22 +0200836 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200837 if (!is_old_ttvn)
838 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200839
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200840 old_ttvn = unicast_packet->ttvn;
841 /* the packet was forged based on outdated network information. Its
842 * destination can possibly be updated and forwarded towards the new
843 * target host
844 */
845 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
846 ethhdr->h_dest)) {
Antonio Quartulli60c39c72012-08-26 01:05:56 +0200847 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200848 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
849 unicast_packet->dest, ethhdr->h_dest,
850 old_ttvn, curr_ttvn);
851 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200852 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200853
854 /* the packet has not been re-routed: either the destination is
855 * currently served by this node or there is no destination at all and
856 * it is possible to drop the packet
857 */
858 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
859 return 0;
860
861 /* update the header in order to let the packet be delivered to this
862 * node's soft interface
863 */
864 primary_if = batadv_primary_if_get_selected(bat_priv);
865 if (!primary_if)
866 return 0;
867
868 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
869
870 batadv_hardif_free_ref(primary_if);
871
872 unicast_packet->ttvn = curr_ttvn;
873
Antonio Quartullia73105b2011-04-27 14:27:44 +0200874 return 1;
875}
876
Simon Wunderlicha1f1ac52013-04-25 10:37:23 +0200877/**
878 * batadv_recv_unhandled_unicast_packet - receive and process packets which
879 * are in the unicast number space but not yet known to the implementation
880 * @skb: unicast tvlv packet to process
881 * @recv_if: pointer to interface this packet was received on
882 *
883 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
884 * otherwise.
885 */
886int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
887 struct batadv_hard_iface *recv_if)
888{
889 struct batadv_unicast_packet *unicast_packet;
890 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
891 int check, hdr_size = sizeof(*unicast_packet);
892
893 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
894 if (check < 0)
895 return NET_RX_DROP;
896
897 /* we don't know about this type, drop it. */
898 unicast_packet = (struct batadv_unicast_packet *)skb->data;
899 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
900 return NET_RX_DROP;
901
902 return batadv_route_unicast_packet(skb, recv_if);
903}
904
Sven Eckelmann56303d32012-06-05 22:31:31 +0200905int batadv_recv_unicast_packet(struct sk_buff *skb,
906 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000907{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200908 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200909 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200910 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Antonio Quartulli9affec62012-10-14 17:19:19 +0200911 uint8_t *orig_addr;
912 struct batadv_orig_node *orig_node = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100913 int check, hdr_size = sizeof(*unicast_packet);
Antonio Quartullic384ea32011-06-26 03:37:18 +0200914 bool is4addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200916 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Martin Hundebøll4046b242012-04-20 17:02:45 +0200917 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200918
Antonio Quartullic384ea32011-06-26 03:37:18 +0200919 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200920 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +0200921 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +0200922 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +0200923
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100924 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -0400925 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +0100926
927 /* Even though the packet is not for us, we might save it to use for
928 * decoding a later received coded packet
929 */
930 if (check == -EREMOTE)
931 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
932
933 if (check < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934 return NET_RX_DROP;
Antonio Quartullidd981ab2013-04-03 10:14:20 +0200935 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
Antonio Quartullia73105b2011-04-27 14:27:44 +0200936 return NET_RX_DROP;
937
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000938 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200939 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Antonio Quartulli9affec62012-10-14 17:19:19 +0200940 if (is4addr) {
Martin Hundebøll4046b242012-04-20 17:02:45 +0200941 batadv_dat_inc_counter(bat_priv,
942 unicast_4addr_packet->subtype);
Antonio Quartulli9affec62012-10-14 17:19:19 +0200943 orig_addr = unicast_4addr_packet->src;
944 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
945 }
Martin Hundebøll4046b242012-04-20 17:02:45 +0200946
Antonio Quartullic384ea32011-06-26 03:37:18 +0200947 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
948 hdr_size))
949 goto rx_success;
950 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
951 hdr_size))
952 goto rx_success;
953
Antonio Quartulli37135172012-07-05 23:38:30 +0200954 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +0200955 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +0200956
Antonio Quartullic384ea32011-06-26 03:37:18 +0200957rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +0200958 if (orig_node)
959 batadv_orig_node_free_ref(orig_node);
960
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961 return NET_RX_SUCCESS;
962 }
963
Sven Eckelmann63b01032012-05-16 20:23:13 +0200964 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000965}
966
Marek Lindneref261572013-04-23 21:39:57 +0800967/**
968 * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
969 * @skb: unicast tvlv packet to process
970 * @recv_if: pointer to interface this packet was received on
971 * @dst_addr: the payload destination
972 *
973 * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
974 * otherwise.
975 */
976int batadv_recv_unicast_tvlv(struct sk_buff *skb,
977 struct batadv_hard_iface *recv_if)
978{
979 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
980 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
981 unsigned char *tvlv_buff;
982 uint16_t tvlv_buff_len;
983 int hdr_size = sizeof(*unicast_tvlv_packet);
984 int ret = NET_RX_DROP;
985
986 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
987 return NET_RX_DROP;
988
989 /* the header is likely to be modified while forwarding */
990 if (skb_cow(skb, hdr_size) < 0)
991 return NET_RX_DROP;
992
993 /* packet needs to be linearized to access the tvlv content */
994 if (skb_linearize(skb) < 0)
995 return NET_RX_DROP;
996
997 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
998
999 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1000 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1001
1002 if (tvlv_buff_len > skb->len - hdr_size)
1003 return NET_RX_DROP;
1004
1005 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1006 unicast_tvlv_packet->src,
1007 unicast_tvlv_packet->dst,
1008 tvlv_buff, tvlv_buff_len);
1009
1010 if (ret != NET_RX_SUCCESS)
1011 ret = batadv_route_unicast_packet(skb, recv_if);
1012
1013 return ret;
1014}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001015
Sven Eckelmann56303d32012-06-05 22:31:31 +02001016int batadv_recv_bcast_packet(struct sk_buff *skb,
1017 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001018{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001019 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1020 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001021 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001023 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001024 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025 int32_t seq_diff;
1026
1027 /* drop packet if it has not necessary minimum size */
1028 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001029 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001031 ethhdr = eth_hdr(skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032
1033 /* packet with broadcast indication but unicast recipient */
1034 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001035 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001036
1037 /* packet with broadcast sender address */
1038 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001039 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040
1041 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001042 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001043 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001044
Sven Eckelmann96412692012-06-05 22:31:30 +02001045 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046
1047 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001048 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001049 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
Sven Eckelmann76543d12011-11-20 15:47:38 +01001051 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001052 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001053
Sven Eckelmannda641192012-05-12 13:48:56 +02001054 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001055
1056 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001057 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001058
Marek Lindnerf3e00082011-01-25 21:52:11 +00001059 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001060
1061 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001062 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1063 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001064 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001065
1066 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1067
1068 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001069 if (batadv_window_protected(bat_priv, seq_diff,
1070 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001071 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072
1073 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001074 * if required.
1075 */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001076 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001077 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1078
Marek Lindnerf3e00082011-01-25 21:52:11 +00001079 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001080
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001081 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001082 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001083 goto out;
1084
Simon Wunderlichc54f38c2013-07-29 17:56:44 +02001085 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1086
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001088 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Simon Wunderlich23721382012-01-22 20:00:19 +01001090 /* don't hand the broadcast up if it is from an originator
1091 * from the same backbone.
1092 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001093 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001094 goto out;
1095
Antonio Quartullic384ea32011-06-26 03:37:18 +02001096 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1097 goto rx_success;
1098 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1099 goto rx_success;
1100
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001101 /* broadcast for me */
Antonio Quartulli37135172012-07-05 23:38:30 +02001102 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1103 orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001104
1105rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +00001106 ret = NET_RX_SUCCESS;
1107 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001108
Marek Lindnerf3e00082011-01-25 21:52:11 +00001109spin_unlock:
1110 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001111out:
1112 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001113 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001114 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001115}