blob: 2f1f88923df8ca0208222b559bcffbf23870603d [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"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028#include "vis.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "unicast.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010030#include "bridge_loop_avoidance.h"
Antonio Quartullic384ea32011-06-26 03:37:18 +020031#include "distributed-arp-table.h"
Martin Hundebøll95332472013-01-25 11:12:40 +010032#include "network-coding.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000033
Sven Eckelmann63b01032012-05-16 20:23:13 +020034static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +020035 struct batadv_hard_iface *recv_if);
Simon Wunderlicha7f6ee92012-01-22 20:00:18 +010036
Sven Eckelmann56303d32012-06-05 22:31:31 +020037void batadv_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038{
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +020040 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +020042 struct batadv_orig_node *orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043 unsigned long *word;
Antonio Quartullic90681b2011-10-05 17:05:25 +020044 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045 size_t word_index;
Sven Eckelmann42d0b042012-06-03 22:19:17 +020046 uint8_t *w;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048 for (i = 0; i < hash->size; i++) {
49 head = &hash->table[i];
50
Marek Lindnerfb778ea2011-01-19 20:01:40 +000051 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -080052 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000053 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmann42d0b042012-06-03 22:19:17 +020054 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055 word = &(orig_node->bcast_own[word_index]);
56
Sven Eckelmann0f5f9322012-05-12 02:09:25 +020057 batadv_bit_get_packet(bat_priv, word, 1, 0);
Sven Eckelmann42d0b042012-06-03 22:19:17 +020058 w = &orig_node->bcast_own_sum[hard_iface->if_num];
59 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000060 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000062 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064}
65
Sven Eckelmann56303d32012-06-05 22:31:31 +020066static void _batadv_update_route(struct batadv_priv *bat_priv,
67 struct batadv_orig_node *orig_node,
68 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
Sven Eckelmann56303d32012-06-05 22:31:31 +020070 struct batadv_neigh_node *curr_router;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000071
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020072 curr_router = batadv_orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000073
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000075 if ((curr_router) && (!neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020076 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
77 "Deleting route towards: %pM\n", orig_node->orig);
Sven Eckelmann08c36d32012-05-12 02:09:39 +020078 batadv_tt_global_del_orig(bat_priv, orig_node,
79 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000081 /* route added */
82 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020083 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020084 "Adding route towards: %pM (via %pM)\n",
85 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000086 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +020087 } else if (neigh_node && curr_router) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +020088 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020089 "Changing route towards: %pM (now via %pM - was via %pM)\n",
90 orig_node->orig, neigh_node->addr,
91 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092 }
93
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000094 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +020095 batadv_neigh_node_free_ref(curr_router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +000096
97 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +000098 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
99 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000100
101 spin_lock_bh(&orig_node->neigh_list_lock);
102 rcu_assign_pointer(orig_node->router, neigh_node);
103 spin_unlock_bh(&orig_node->neigh_list_lock);
104
105 /* decrease refcount of previous best neighbor */
106 if (curr_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200107 batadv_neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108}
109
Sven Eckelmann56303d32012-06-05 22:31:31 +0200110void batadv_update_route(struct batadv_priv *bat_priv,
111 struct batadv_orig_node *orig_node,
112 struct batadv_neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114 struct batadv_neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115
116 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000117 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200119 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000120
121 if (router != neigh_node)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200122 _batadv_update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000123
124out:
125 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200126 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000127}
128
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000129/* caller must hold the neigh_list_lock */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200130void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
131 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000132{
133 /* this neighbor is not part of our candidate list */
134 if (list_empty(&neigh_node->bonding_list))
135 goto out;
136
137 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000138 INIT_LIST_HEAD(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200139 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000140 atomic_dec(&orig_node->bond_candidates);
141
142out:
143 return;
144}
145
Sven Eckelmann56303d32012-06-05 22:31:31 +0200146void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
147 struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000148{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200149 struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000150 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000151
152 spin_lock_bh(&orig_node->neigh_list_lock);
153
154 /* only consider if it has the same primary address ... */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200155 if (!batadv_compare_eth(orig_node->orig,
156 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000157 goto candidate_del;
158
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200159 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000160 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000161 goto candidate_del;
162
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000163 /* ... and is good enough to be considered */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200164 if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000165 goto candidate_del;
166
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200167 /* check if we have another candidate with the same mac address or
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000168 * interface. If we do, we won't select this candidate because of
169 * possible interference.
170 */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800171 hlist_for_each_entry_rcu(tmp_neigh_node,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000172 &orig_node->neigh_list, list) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000173 if (tmp_neigh_node == neigh_node)
174 continue;
175
176 /* we only care if the other candidate is even
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200177 * considered as candidate.
178 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000179 if (list_empty(&tmp_neigh_node->bonding_list))
180 continue;
181
182 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200183 (batadv_compare_eth(neigh_node->addr,
184 tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000185 interference_candidate = 1;
186 break;
187 }
188 }
189
190 /* don't care further if it is an interference candidate */
191 if (interference_candidate)
192 goto candidate_del;
193
194 /* this neighbor already is part of our candidate list */
195 if (!list_empty(&neigh_node->bonding_list))
196 goto out;
197
Marek Lindner44524fc2011-02-10 14:33:53 +0000198 if (!atomic_inc_not_zero(&neigh_node->refcount))
199 goto out;
200
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000201 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000202 atomic_inc(&orig_node->bond_candidates);
203 goto out;
204
205candidate_del:
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200206 batadv_bonding_candidate_del(orig_node, neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000207
208out:
209 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000210
211 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200212 batadv_neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000213}
214
215/* copy primary address for bonding */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200216void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200217batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
218 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200219 const struct batadv_ogm_packet *batman_ogm_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000220{
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200221 if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000222 return;
223
224 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
225}
226
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227/* checks whether the host restarted and is in the protection time.
228 * returns:
229 * 0 if the packet is to be accepted
230 * 1 if the packet is to be ignored.
231 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200232int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200233 unsigned long *last_reset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200235 if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
236 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
237 if (!batadv_has_timed_out(*last_reset,
238 BATADV_RESET_PROTECTION_MS))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 return 1;
Marek Lindner8c7bf242012-03-17 15:28:33 +0800240
241 *last_reset = jiffies;
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200242 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200243 "old packet received, start protection\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244 }
Marek Lindner8c7bf242012-03-17 15:28:33 +0800245
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 return 0;
247}
248
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200249bool batadv_check_management_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200250 struct batadv_hard_iface *hard_iface,
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200251 int header_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000253 struct ethhdr *ethhdr;
254
255 /* drop packet if it has not necessary minimum size */
Marek Lindnerc3e29312012-03-04 16:56:25 +0800256 if (unlikely(!pskb_may_pull(skb, header_len)))
257 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258
259 ethhdr = (struct ethhdr *)skb_mac_header(skb);
260
261 /* packet with broadcast indication but unicast recipient */
262 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800263 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264
265 /* packet with broadcast sender address */
266 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerc3e29312012-03-04 16:56:25 +0800267 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268
269 /* create a copy of the skb, if needed, to modify it. */
270 if (skb_cow(skb, 0) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800271 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272
273 /* keep skb linear */
274 if (skb_linearize(skb) < 0)
Marek Lindnerc3e29312012-03-04 16:56:25 +0800275 return false;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
Marek Lindnerc3e29312012-03-04 16:56:25 +0800277 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278}
279
Sven Eckelmann56303d32012-06-05 22:31:31 +0200280static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200281 struct sk_buff *skb, size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200283 struct batadv_hard_iface *primary_if = NULL;
284 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200285 struct batadv_icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000286 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
Sven Eckelmann96412692012-06-05 22:31:30 +0200288 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289
290 /* add data to device queue */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200291 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200292 batadv_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000293 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 }
295
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200296 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200297 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000298 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299
300 /* answer echo request (ping) */
301 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200302 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000303 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000304 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000305
Marek Lindner44524fc2011-02-10 14:33:53 +0000306 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100307 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000308 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309
Sven Eckelmann96412692012-06-05 22:31:30 +0200310 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311
Marek Lindner44524fc2011-02-10 14:33:53 +0000312 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200313 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200314 icmp_packet->msg_type = BATADV_ECHO_REPLY;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200315 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200317 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
318 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Marek Lindner44524fc2011-02-10 14:33:53 +0000320out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200321 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200322 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000323 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200324 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 return ret;
326}
327
Sven Eckelmann56303d32012-06-05 22:31:31 +0200328static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200329 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331 struct batadv_hard_iface *primary_if = NULL;
332 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200333 struct batadv_icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000334 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335
Sven Eckelmann96412692012-06-05 22:31:30 +0200336 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
338 /* send TTL exceeded if packet is an echo request (traceroute) */
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200339 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100340 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
341 icmp_packet->orig, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000342 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 }
344
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200345 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200346 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000347 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
349 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200350 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000351 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000352 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000353
Marek Lindner44524fc2011-02-10 14:33:53 +0000354 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100355 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000356 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357
Sven Eckelmann96412692012-06-05 22:31:30 +0200358 icmp_packet = (struct batadv_icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Marek Lindner44524fc2011-02-10 14:33:53 +0000360 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200361 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200362 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200363 icmp_packet->header.ttl = BATADV_TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200365 if (batadv_send_skb_to_orig(skb, orig_node, NULL))
366 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367
Marek Lindner44524fc2011-02-10 14:33:53 +0000368out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200369 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200370 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000371 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200372 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 return ret;
374}
375
376
Sven Eckelmann56303d32012-06-05 22:31:31 +0200377int batadv_recv_icmp_packet(struct sk_buff *skb,
378 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200380 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200381 struct batadv_icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200383 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200384 int hdr_size = sizeof(struct batadv_icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +0000385 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200387 /* we truncate all incoming icmp packets if they don't match our size */
Sven Eckelmann96412692012-06-05 22:31:30 +0200388 if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
389 hdr_size = sizeof(struct batadv_icmp_packet_rr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
391 /* drop packet if it has not necessary minimum size */
392 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +0000393 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394
395 ethhdr = (struct ethhdr *)skb_mac_header(skb);
396
397 /* packet with unicast indication but broadcast recipient */
398 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000399 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400
401 /* packet with broadcast sender address */
402 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +0000403 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000404
405 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200406 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +0000407 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000408
Sven Eckelmann96412692012-06-05 22:31:30 +0200409 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410
411 /* add record route information if not full */
Sven Eckelmann96412692012-06-05 22:31:30 +0200412 if ((hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200413 (icmp_packet->rr_cur < BATADV_RR_LEN)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100415 ethhdr->h_dest, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 icmp_packet->rr_cur++;
417 }
418
419 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200420 if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200421 return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422
423 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100424 if (icmp_packet->header.ttl < 2)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200425 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200428 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000429 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000430 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000431
Marek Lindner44524fc2011-02-10 14:33:53 +0000432 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100433 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000434 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
Sven Eckelmann96412692012-06-05 22:31:30 +0200436 icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437
Marek Lindner44524fc2011-02-10 14:33:53 +0000438 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100439 icmp_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440
Marek Lindner44524fc2011-02-10 14:33:53 +0000441 /* route it */
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200442 if (batadv_send_skb_to_orig(skb, orig_node, recv_if))
443 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
Marek Lindner44524fc2011-02-10 14:33:53 +0000445out:
Marek Lindner44524fc2011-02-10 14:33:53 +0000446 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200447 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 return ret;
449}
450
Linus Lüssing55158622011-03-14 22:43:27 +0000451/* In the bonding case, send the packets in a round
452 * robin fashion over the remaining interfaces.
453 *
454 * This method rotates the bonding list and increases the
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200455 * returned router's refcount.
456 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200457static struct batadv_neigh_node *
458batadv_find_bond_router(struct batadv_orig_node *primary_orig,
459 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000460{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200461 struct batadv_neigh_node *tmp_neigh_node;
462 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000463
464 rcu_read_lock();
465 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
466 bonding_list) {
467 if (!first_candidate)
468 first_candidate = tmp_neigh_node;
469
470 /* recv_if == NULL on the first node. */
471 if (tmp_neigh_node->if_incoming == recv_if)
472 continue;
473
474 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
475 continue;
476
477 router = tmp_neigh_node;
478 break;
479 }
480
481 /* use the first candidate if nothing was found. */
482 if (!router && first_candidate &&
483 atomic_inc_not_zero(&first_candidate->refcount))
484 router = first_candidate;
485
486 if (!router)
487 goto out;
488
489 /* selected should point to the next element
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200490 * after the current router
491 */
Linus Lüssing55158622011-03-14 22:43:27 +0000492 spin_lock_bh(&primary_orig->neigh_list_lock);
493 /* this is a list_move(), which unfortunately
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200494 * does not exist as rcu version
495 */
Linus Lüssing55158622011-03-14 22:43:27 +0000496 list_del_rcu(&primary_orig->bond_list);
497 list_add_rcu(&primary_orig->bond_list,
498 &router->bonding_list);
499 spin_unlock_bh(&primary_orig->neigh_list_lock);
500
501out:
502 rcu_read_unlock();
503 return router;
504}
505
506/* Interface Alternating: Use the best of the
507 * remaining candidates which are not using
508 * this interface.
509 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200510 * Increases the returned router's refcount
511 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200512static struct batadv_neigh_node *
513batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
514 const struct batadv_hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +0000515{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516 struct batadv_neigh_node *tmp_neigh_node;
517 struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
Linus Lüssing55158622011-03-14 22:43:27 +0000518
519 rcu_read_lock();
520 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
521 bonding_list) {
522 if (!first_candidate)
523 first_candidate = tmp_neigh_node;
524
525 /* recv_if == NULL on the first node. */
526 if (tmp_neigh_node->if_incoming == recv_if)
527 continue;
528
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200529 if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
530 continue;
531
Linus Lüssing55158622011-03-14 22:43:27 +0000532 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
533 continue;
534
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200535 /* decrement refcount of previously selected router */
536 if (router)
537 batadv_neigh_node_free_ref(router);
Linus Lüssing55158622011-03-14 22:43:27 +0000538
Sven Eckelmannfe3f4cf2012-08-20 10:26:49 +0200539 /* we found a better router (or at least one valid router) */
540 router = tmp_neigh_node;
Linus Lüssing55158622011-03-14 22:43:27 +0000541 }
542
543 /* use the first candidate if nothing was found. */
544 if (!router && first_candidate &&
545 atomic_inc_not_zero(&first_candidate->refcount))
546 router = first_candidate;
547
548 rcu_read_unlock();
549 return router;
550}
551
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100552/**
553 * batadv_check_unicast_packet - Check for malformed unicast packets
David S. Miller6e0895c2013-04-22 20:32:51 -0400554 * @bat_priv: the bat priv with all the soft interface information
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100555 * @skb: packet to check
556 * @hdr_size: size of header to pull
557 *
558 * Check for short header and bad addresses in given packet. Returns negative
559 * value when check fails and 0 otherwise. The negative value depends on the
560 * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
561 * and -EREMOTE for non-local (other host) destination.
562 */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200563static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
564 struct sk_buff *skb, int hdr_size)
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200565{
566 struct ethhdr *ethhdr;
567
568 /* drop packet if it has not necessary minimum size */
569 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100570 return -ENODATA;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200571
572 ethhdr = (struct ethhdr *)skb_mac_header(skb);
573
574 /* packet with unicast indication but broadcast recipient */
575 if (is_broadcast_ether_addr(ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100576 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200577
578 /* packet with broadcast sender address */
579 if (is_broadcast_ether_addr(ethhdr->h_source))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100580 return -EBADR;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200581
582 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200583 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Martin Hundebøllf86ce0a2013-01-14 00:20:32 +0100584 return -EREMOTE;
Martin Hundebøllff51fd72012-07-05 11:34:27 +0200585
586 return 0;
587}
588
Sven Eckelmann56303d32012-06-05 22:31:31 +0200589int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200590{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200591 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200592 struct batadv_tt_query_packet *tt_query;
Al Virof25bd582012-04-22 07:44:27 +0100593 uint16_t tt_size;
Martin Hundebøll74ee3632012-07-05 11:34:28 +0200594 int hdr_size = sizeof(*tt_query);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200595 char tt_flag;
Sven Eckelmann96412692012-06-05 22:31:30 +0200596 size_t packet_size;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200597
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200598 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
Martin Hundebøll74ee3632012-07-05 11:34:28 +0200599 return NET_RX_DROP;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200600
601 /* I could need to modify it */
Sven Eckelmann96412692012-06-05 22:31:30 +0200602 if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200603 goto out;
604
Sven Eckelmann96412692012-06-05 22:31:30 +0200605 tt_query = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200606
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200607 switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200608 case BATADV_TT_REQUEST:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200609 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200610
Antonio Quartullia73105b2011-04-27 14:27:44 +0200611 /* If we cannot provide an answer the tt_request is
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200612 * forwarded
613 */
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200614 if (!batadv_send_tt_response(bat_priv, tt_query)) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200615 if (tt_query->flags & BATADV_TT_FULL_TABLE)
616 tt_flag = 'F';
617 else
618 tt_flag = '.';
619
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200620 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200621 "Routing TT_REQUEST to %pM [%c]\n",
622 tt_query->dst,
623 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200624 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200625 }
626 break;
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200627 case BATADV_TT_RESPONSE:
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200629
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200630 if (batadv_is_my_mac(bat_priv, tt_query->dst)) {
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200631 /* packet needs to be linearized to access the TT
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200632 * changes
633 */
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200634 if (skb_linearize(skb) < 0)
635 goto out;
Antonio Quartullid2b6cc82012-06-14 22:21:28 +0200636 /* skb_linearize() possibly changed skb->data */
Sven Eckelmann96412692012-06-05 22:31:30 +0200637 tt_query = (struct batadv_tt_query_packet *)skb->data;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200638
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200639 tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200640
641 /* Ensure we have all the claimed data */
Sven Eckelmann96412692012-06-05 22:31:30 +0200642 packet_size = sizeof(struct batadv_tt_query_packet);
643 packet_size += tt_size;
644 if (unlikely(skb_headlen(skb) < packet_size))
Antonio Quartulli8b7342d2011-10-16 20:32:03 +0200645 goto out;
646
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200647 batadv_handle_tt_response(bat_priv, tt_query);
Antonio Quartullidc58fe32011-10-16 20:32:02 +0200648 } else {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200649 if (tt_query->flags & BATADV_TT_FULL_TABLE)
650 tt_flag = 'F';
651 else
652 tt_flag = '.';
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200653 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200654 "Routing TT_RESPONSE to %pM [%c]\n",
655 tt_query->dst,
656 tt_flag);
Sven Eckelmann63b01032012-05-16 20:23:13 +0200657 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200658 }
659 break;
660 }
661
662out:
663 /* returning NET_RX_DROP will make the caller function kfree the skb */
664 return NET_RX_DROP;
665}
666
Sven Eckelmann56303d32012-06-05 22:31:31 +0200667int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200668{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200669 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +0200670 struct batadv_roam_adv_packet *roam_adv_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200671 struct batadv_orig_node *orig_node;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200672
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200673 if (batadv_check_unicast_packet(bat_priv, skb,
674 sizeof(*roam_adv_packet)) < 0)
Antonio Quartullicc47f662011-04-27 14:27:57 +0200675 goto out;
676
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200677 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200678
Sven Eckelmann96412692012-06-05 22:31:30 +0200679 roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
Antonio Quartullicc47f662011-04-27 14:27:57 +0200680
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200681 if (!batadv_is_my_mac(bat_priv, roam_adv_packet->dst))
Sven Eckelmann63b01032012-05-16 20:23:13 +0200682 return batadv_route_unicast_packet(skb, recv_if);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200683
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100684 /* check if it is a backbone gateway. we don't accept
685 * roaming advertisement from it, as it has the same
686 * entries as we have.
687 */
Sven Eckelmann08adf152012-05-12 13:38:47 +0200688 if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
Simon Wunderlich20ff9d52012-01-22 20:00:23 +0100689 goto out;
690
Sven Eckelmannda641192012-05-12 13:48:56 +0200691 orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200692 if (!orig_node)
693 goto out;
694
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200695 batadv_dbg(BATADV_DBG_TT, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200696 "Received ROAMING_ADV from %pM (client %pM)\n",
697 roam_adv_packet->src, roam_adv_packet->client);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200698
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200699 batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200700 BATADV_TT_CLIENT_ROAM,
Antonio Quartullid4f44692012-05-25 00:00:54 +0200701 atomic_read(&orig_node->last_ttvn) + 1);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200702
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200703 batadv_orig_node_free_ref(orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +0200704out:
705 /* returning NET_RX_DROP will make the caller function kfree the skb */
706 return NET_RX_DROP;
707}
708
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000710 * bonding if possible. increases the found neighbors
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200711 * refcount.
712 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200713struct batadv_neigh_node *
714batadv_find_router(struct batadv_priv *bat_priv,
715 struct batadv_orig_node *orig_node,
716 const struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200718 struct batadv_orig_node *primary_orig_node;
719 struct batadv_orig_node *router_orig;
720 struct batadv_neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
722 int bonding_enabled;
Sven Eckelmannda641192012-05-12 13:48:56 +0200723 uint8_t *primary_addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724
725 if (!orig_node)
726 return NULL;
727
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200728 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000729 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +0200730 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000731
732 /* without bonding, the first node should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200733 * always choose the default router.
734 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735 bonding_enabled = atomic_read(&bat_priv->bonding);
736
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000737 rcu_read_lock();
738 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000739 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +0200740 if (!router_orig)
741 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000742
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000743 if ((!recv_if) && (!bonding_enabled))
744 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745
Sven Eckelmannda641192012-05-12 13:48:56 +0200746 primary_addr = router_orig->primary_addr;
747
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000748 /* if we have something in the primary_addr, we can search
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200749 * for a potential bonding candidate.
750 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200751 if (batadv_compare_eth(primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000752 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753
754 /* find the orig_node which has the primary interface. might
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200755 * even be the same as our router_orig in many cases
756 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200757 if (batadv_compare_eth(primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758 primary_orig_node = router_orig;
759 } else {
Sven Eckelmannda641192012-05-12 13:48:56 +0200760 primary_orig_node = batadv_orig_hash_find(bat_priv,
761 primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000763 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000764
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200765 batadv_orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 }
767
768 /* with less than 2 candidates, we can't do any
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200769 * bonding and prefer the original router.
770 */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000771 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
772 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 /* all nodes between should choose a candidate which
775 * is is not on the interface where the packet came
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200776 * in.
777 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200778 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779
Linus Lüssing55158622011-03-14 22:43:27 +0000780 if (bonding_enabled)
Sven Eckelmann63b01032012-05-16 20:23:13 +0200781 router = batadv_find_bond_router(primary_orig_node, recv_if);
Linus Lüssing55158622011-03-14 22:43:27 +0000782 else
Sven Eckelmann63b01032012-05-16 20:23:13 +0200783 router = batadv_find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000784
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000785return_router:
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200786 if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
Antonio Quartullie2cbc112011-05-08 20:52:57 +0200787 goto err_unlock;
788
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000789 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +0200791err_unlock:
792 rcu_read_unlock();
793err:
794 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200795 batadv_neigh_node_free_ref(router);
Marek Lindner01df2b62011-05-05 14:14:46 +0200796 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797}
798
Sven Eckelmann63b01032012-05-16 20:23:13 +0200799static int batadv_route_unicast_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200800 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200802 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
803 struct batadv_orig_node *orig_node = NULL;
804 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200805 struct batadv_unicast_packet *unicast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +0000807 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 struct sk_buff *new_skb;
809
Sven Eckelmann96412692012-06-05 22:31:30 +0200810 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811
812 /* TTL exceeded */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100813 if (unicast_packet->header.ttl < 2) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100814 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
815 ethhdr->h_source, unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +0000816 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817 }
818
819 /* get routing information */
Sven Eckelmannda641192012-05-12 13:48:56 +0200820 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
Marek Lindner7aadf882011-02-18 12:28:09 +0000821
Marek Lindner44524fc2011-02-10 14:33:53 +0000822 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +0200823 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000825 /* find_router() increases neigh_nodes refcount if found. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200826 neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827
Marek Lindnerd0072602011-01-19 20:01:44 +0000828 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000829 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830
831 /* create a copy of the skb, if needed, to modify it. */
Antonio Quartulli0d125072012-02-18 11:27:34 +0100832 if (skb_cow(skb, ETH_HLEN) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +0000833 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834
Sven Eckelmann96412692012-06-05 22:31:30 +0200835 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200837 if (unicast_packet->header.packet_type == BATADV_UNICAST &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000838 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +0000839 skb->len > neigh_node->if_incoming->net_dev->mtu) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200840 ret = batadv_frag_send_skb(skb, bat_priv,
841 neigh_node->if_incoming,
842 neigh_node->addr);
Marek Lindnerd0072602011-01-19 20:01:44 +0000843 goto out;
844 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200846 if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
Sven Eckelmannf0530ee2012-05-12 13:48:57 +0200847 batadv_frag_can_reassemble(skb,
848 neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +0200849 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850
851 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +0000852 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853
854 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +0000855 if (!new_skb) {
856 ret = NET_RX_SUCCESS;
857 goto out;
858 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
860 skb = new_skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200861 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862 }
863
864 /* decrement ttl */
Sven Eckelmann76543d12011-11-20 15:47:38 +0100865 unicast_packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866
Martin Hundebøll95332472013-01-25 11:12:40 +0100867 /* network code packet if possible */
868 if (batadv_nc_skb_forward(skb, neigh_node, ethhdr)) {
Martin Hundebøllbb351ba2012-10-16 16:13:48 +0200869 ret = NET_RX_SUCCESS;
Martin Hundebøll95332472013-01-25 11:12:40 +0100870 } else if (batadv_send_skb_to_orig(skb, orig_node, recv_if)) {
871 ret = NET_RX_SUCCESS;
872
873 /* Update stats counter */
874 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
875 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
876 skb->len + ETH_HLEN);
877 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
Marek Lindner44524fc2011-02-10 14:33:53 +0000879out:
880 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200881 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000882 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200883 batadv_orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000884 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000885}
886
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200887/**
888 * batadv_reroute_unicast_packet - update the unicast header for re-routing
889 * @bat_priv: the bat priv with all the soft interface information
890 * @unicast_packet: the unicast header to be updated
891 * @dst_addr: the payload destination
892 *
893 * Search the translation table for dst_addr and update the unicast header with
894 * the new corresponding information (originator address where the destination
895 * client currently is and its known TTVN)
896 *
897 * Returns true if the packet header has been updated, false otherwise
898 */
899static bool
900batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
901 struct batadv_unicast_packet *unicast_packet,
902 uint8_t *dst_addr)
903{
904 struct batadv_orig_node *orig_node = NULL;
905 struct batadv_hard_iface *primary_if = NULL;
906 bool ret = false;
907 uint8_t *orig_addr, orig_ttvn;
908
909 if (batadv_is_my_client(bat_priv, dst_addr)) {
910 primary_if = batadv_primary_if_get_selected(bat_priv);
911 if (!primary_if)
912 goto out;
913 orig_addr = primary_if->net_dev->dev_addr;
914 orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
915 } else {
916 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
917 if (!orig_node)
918 goto out;
919
920 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
921 goto out;
922
923 orig_addr = orig_node->orig;
924 orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
925 }
926
927 /* update the packet header */
928 memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
929 unicast_packet->ttvn = orig_ttvn;
930
931 ret = true;
932out:
933 if (primary_if)
934 batadv_hardif_free_ref(primary_if);
935 if (orig_node)
936 batadv_orig_node_free_ref(orig_node);
937
938 return ret;
939}
940
Sven Eckelmann56303d32012-06-05 22:31:31 +0200941static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
Sven Eckelmann63b01032012-05-16 20:23:13 +0200942 struct sk_buff *skb) {
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200943 uint8_t curr_ttvn, old_ttvn;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200944 struct batadv_orig_node *orig_node;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200945 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200946 struct batadv_hard_iface *primary_if;
Sven Eckelmann96412692012-06-05 22:31:30 +0200947 struct batadv_unicast_packet *unicast_packet;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200948 int is_old_ttvn;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200949
Antonio Quartullib8fcfa42012-08-26 23:25:59 +0200950 /* check if there is enough data before accessing it */
951 if (pskb_may_pull(skb, sizeof(*unicast_packet) + ETH_HLEN) < 0)
952 return 0;
953
954 /* create a copy of the skb (in case of for re-routing) to modify it. */
955 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200956 return 0;
957
Sven Eckelmann96412692012-06-05 22:31:30 +0200958 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200959 ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
Antonio Quartullia73105b2011-04-27 14:27:44 +0200960
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200961 /* check if the destination client was served by this node and it is now
962 * roaming. In this case, it means that the node has got a ROAM_ADV
963 * message and that it knows the new destination in the mesh to re-route
964 * the packet to
965 */
966 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
967 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
968 ethhdr->h_dest))
969 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
970 bat_priv,
971 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
972 unicast_packet->dest,
973 ethhdr->h_dest);
974 /* at this point the mesh destination should have been
975 * substituted with the originator address found in the global
976 * table. If not, let the packet go untouched anyway because
977 * there is nothing the node can do
978 */
979 return 1;
980 }
981
982 /* retrieve the TTVN known by this node for the packet destination. This
983 * value is used later to check if the node which sent (or re-routed
984 * last time) the packet had an updated information or not
985 */
986 curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
Antonio Quartullife8a93b2013-04-03 19:10:26 +0200987 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmannda641192012-05-12 13:48:56 +0200988 orig_node = batadv_orig_hash_find(bat_priv,
989 unicast_packet->dest);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +0200990 /* if it is not possible to find the orig_node representing the
991 * destination, the packet can immediately be dropped as it will
992 * not be possible to deliver it
993 */
Antonio Quartullia73105b2011-04-27 14:27:44 +0200994 if (!orig_node)
995 return 0;
996
997 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200998 batadv_orig_node_free_ref(orig_node);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200999 }
1000
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001001 /* check if the TTVN contained in the packet is fresher than what the
1002 * node knows
1003 */
Sven Eckelmann3e348192012-05-16 20:23:22 +02001004 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001005 if (!is_old_ttvn)
1006 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001007
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001008 old_ttvn = unicast_packet->ttvn;
1009 /* the packet was forged based on outdated network information. Its
1010 * destination can possibly be updated and forwarded towards the new
1011 * target host
1012 */
1013 if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
1014 ethhdr->h_dest)) {
Antonio Quartulli60c39c72012-08-26 01:05:56 +02001015 net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001016 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
1017 unicast_packet->dest, ethhdr->h_dest,
1018 old_ttvn, curr_ttvn);
1019 return 1;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001020 }
Antonio Quartulli7c1fd912012-09-23 22:38:34 +02001021
1022 /* the packet has not been re-routed: either the destination is
1023 * currently served by this node or there is no destination at all and
1024 * it is possible to drop the packet
1025 */
1026 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
1027 return 0;
1028
1029 /* update the header in order to let the packet be delivered to this
1030 * node's soft interface
1031 */
1032 primary_if = batadv_primary_if_get_selected(bat_priv);
1033 if (!primary_if)
1034 return 0;
1035
1036 memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
1037
1038 batadv_hardif_free_ref(primary_if);
1039
1040 unicast_packet->ttvn = curr_ttvn;
1041
Antonio Quartullia73105b2011-04-27 14:27:44 +02001042 return 1;
1043}
1044
Sven Eckelmann56303d32012-06-05 22:31:31 +02001045int batadv_recv_unicast_packet(struct sk_buff *skb,
1046 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001048 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001049 struct batadv_unicast_packet *unicast_packet;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001050 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
Antonio Quartulli9affec62012-10-14 17:19:19 +02001051 uint8_t *orig_addr;
1052 struct batadv_orig_node *orig_node = NULL;
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001053 int check, hdr_size = sizeof(*unicast_packet);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001054 bool is4addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001055
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +02001056 unicast_packet = (struct batadv_unicast_packet *)skb->data;
Martin Hundebøll4046b242012-04-20 17:02:45 +02001057 unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +02001058
Antonio Quartullic384ea32011-06-26 03:37:18 +02001059 is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +02001060 /* the caller function should have already pulled 2 bytes */
Antonio Quartullic384ea32011-06-26 03:37:18 +02001061 if (is4addr)
Martin Hundebøll4046b242012-04-20 17:02:45 +02001062 hdr_size = sizeof(*unicast_4addr_packet);
Antonio Quartulli7cdcf6d2012-10-01 09:57:35 +02001063
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001064 /* function returns -EREMOTE for promiscuous packets */
David S. Miller6e0895c2013-04-22 20:32:51 -04001065 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
Martin Hundebøll612d2b42013-01-25 11:12:42 +01001066
1067 /* Even though the packet is not for us, we might save it to use for
1068 * decoding a later received coded packet
1069 */
1070 if (check == -EREMOTE)
1071 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
1072
1073 if (check < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001074 return NET_RX_DROP;
1075
Sven Eckelmann63b01032012-05-16 20:23:13 +02001076 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001077 return NET_RX_DROP;
1078
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001080 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Antonio Quartulli9affec62012-10-14 17:19:19 +02001081 if (is4addr) {
Martin Hundebøll4046b242012-04-20 17:02:45 +02001082 batadv_dat_inc_counter(bat_priv,
1083 unicast_4addr_packet->subtype);
Antonio Quartulli9affec62012-10-14 17:19:19 +02001084 orig_addr = unicast_4addr_packet->src;
1085 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
1086 }
Martin Hundebøll4046b242012-04-20 17:02:45 +02001087
Antonio Quartullic384ea32011-06-26 03:37:18 +02001088 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1089 hdr_size))
1090 goto rx_success;
1091 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1092 hdr_size))
1093 goto rx_success;
1094
Antonio Quartulli37135172012-07-05 23:38:30 +02001095 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
Antonio Quartulli9affec62012-10-14 17:19:19 +02001096 orig_node);
Antonio Quartulli37135172012-07-05 23:38:30 +02001097
Antonio Quartullic384ea32011-06-26 03:37:18 +02001098rx_success:
Antonio Quartulli9affec62012-10-14 17:19:19 +02001099 if (orig_node)
1100 batadv_orig_node_free_ref(orig_node);
1101
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102 return NET_RX_SUCCESS;
1103 }
1104
Sven Eckelmann63b01032012-05-16 20:23:13 +02001105 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001106}
1107
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001108int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001109 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001110{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001111 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001112 struct batadv_unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001113 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001114 struct sk_buff *new_skb = NULL;
1115 int ret;
1116
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001117 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118 return NET_RX_DROP;
1119
Sven Eckelmann63b01032012-05-16 20:23:13 +02001120 if (!batadv_check_unicast_ttvn(bat_priv, skb))
Antonio Quartullia73105b2011-04-27 14:27:44 +02001121 return NET_RX_DROP;
1122
Sven Eckelmann96412692012-06-05 22:31:30 +02001123 unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001124
1125 /* packet for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001126 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
Sven Eckelmann88ed1e772012-05-12 02:09:40 +02001127 ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001128
1129 if (ret == NET_RX_DROP)
1130 return NET_RX_DROP;
1131
1132 /* packet was buffered for late merge */
1133 if (!new_skb)
1134 return NET_RX_SUCCESS;
1135
Antonio Quartullic384ea32011-06-26 03:37:18 +02001136 if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
1137 hdr_size))
1138 goto rx_success;
1139 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
1140 hdr_size))
1141 goto rx_success;
1142
Sven Eckelmann04b482a2012-05-12 02:09:38 +02001143 batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
Antonio Quartulli37135172012-07-05 23:38:30 +02001144 sizeof(struct batadv_unicast_packet), NULL);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001145
1146rx_success:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001147 return NET_RX_SUCCESS;
1148 }
1149
Sven Eckelmann63b01032012-05-16 20:23:13 +02001150 return batadv_route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151}
1152
1153
Sven Eckelmann56303d32012-06-05 22:31:31 +02001154int batadv_recv_bcast_packet(struct sk_buff *skb,
1155 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001156{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001157 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1158 struct batadv_orig_node *orig_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +02001159 struct batadv_bcast_packet *bcast_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001160 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001161 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001162 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001163 int32_t seq_diff;
1164
1165 /* drop packet if it has not necessary minimum size */
1166 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001167 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168
1169 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1170
1171 /* packet with broadcast indication but unicast recipient */
1172 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001173 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001174
1175 /* packet with broadcast sender address */
1176 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001177 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001178
1179 /* ignore broadcasts sent by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001180 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001181 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001182
Sven Eckelmann96412692012-06-05 22:31:30 +02001183 bcast_packet = (struct batadv_bcast_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184
1185 /* ignore broadcasts originated by myself */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001186 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001187 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001188
Sven Eckelmann76543d12011-11-20 15:47:38 +01001189 if (bcast_packet->header.ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001190 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001191
Sven Eckelmannda641192012-05-12 13:48:56 +02001192 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001193
1194 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001195 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196
Marek Lindnerf3e00082011-01-25 21:52:11 +00001197 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001198
1199 /* check whether the packet is a duplicate */
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001200 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1201 ntohl(bcast_packet->seqno)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001202 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001203
1204 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1205
1206 /* check whether the packet is old and the host just restarted. */
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001207 if (batadv_window_protected(bat_priv, seq_diff,
1208 &orig_node->bcast_seqno_reset))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001209 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001210
1211 /* mark broadcast in flood history, update window position
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001212 * if required.
1213 */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001214 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001215 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1216
Marek Lindnerf3e00082011-01-25 21:52:11 +00001217 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001218
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001219 /* check whether this has been sent by another originator before */
Simon Wunderlich004e86f2012-10-18 13:47:42 +02001220 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
Simon Wunderlichfe2da6f2012-01-22 20:00:24 +01001221 goto out;
1222
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001223 /* rebroadcast packet */
Sven Eckelmann9455e342012-05-12 02:09:37 +02001224 batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001225
Simon Wunderlich23721382012-01-22 20:00:19 +01001226 /* don't hand the broadcast up if it is from an originator
1227 * from the same backbone.
1228 */
Sven Eckelmann08adf152012-05-12 13:38:47 +02001229 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
Simon Wunderlich23721382012-01-22 20:00:19 +01001230 goto out;
1231
Antonio Quartullic384ea32011-06-26 03:37:18 +02001232 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1233 goto rx_success;
1234 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1235 goto rx_success;
1236
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001237 /* broadcast for me */
Antonio Quartulli37135172012-07-05 23:38:30 +02001238 batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
1239 orig_node);
Antonio Quartullic384ea32011-06-26 03:37:18 +02001240
1241rx_success:
Marek Lindnerf3e00082011-01-25 21:52:11 +00001242 ret = NET_RX_SUCCESS;
1243 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244
Marek Lindnerf3e00082011-01-25 21:52:11 +00001245spin_unlock:
1246 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001247out:
1248 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001249 batadv_orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001250 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001251}
1252
Sven Eckelmann56303d32012-06-05 22:31:31 +02001253int batadv_recv_vis_packet(struct sk_buff *skb,
1254 struct batadv_hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001255{
Sven Eckelmann96412692012-06-05 22:31:30 +02001256 struct batadv_vis_packet *vis_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001257 struct ethhdr *ethhdr;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001258 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001259 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001260
1261 /* keep skb linear */
1262 if (skb_linearize(skb) < 0)
1263 return NET_RX_DROP;
1264
1265 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1266 return NET_RX_DROP;
1267
Sven Eckelmann96412692012-06-05 22:31:30 +02001268 vis_packet = (struct batadv_vis_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001269 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1270
1271 /* not for me */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001272 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001273 return NET_RX_DROP;
1274
1275 /* ignore own packets */
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001276 if (batadv_is_my_mac(bat_priv, vis_packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001277 return NET_RX_DROP;
1278
Antonio Quartullife8a93b2013-04-03 19:10:26 +02001279 if (batadv_is_my_mac(bat_priv, vis_packet->sender_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001280 return NET_RX_DROP;
1281
1282 switch (vis_packet->vis_type) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001283 case BATADV_VIS_TYPE_SERVER_SYNC:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001284 batadv_receive_server_sync_packet(bat_priv, vis_packet,
1285 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001286 break;
1287
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001288 case BATADV_VIS_TYPE_CLIENT_UPDATE:
Sven Eckelmannd0f714f2012-05-12 02:09:41 +02001289 batadv_receive_client_update_packet(bat_priv, vis_packet,
1290 skb_headlen(skb));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001291 break;
1292
1293 default: /* ignore unknown packet */
1294 break;
1295 }
1296
1297 /* We take a copy of the data in the packet, so we should
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001298 * always free the skbuf.
1299 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001300 return NET_RX_DROP;
1301}