blob: 9863c03a21373ff001b441df05ae1301dcaf99fb [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "routing.h"
24#include "send.h"
25#include "hash.h"
26#include "soft-interface.h"
27#include "hard-interface.h"
28#include "icmp_socket.h"
29#include "translation-table.h"
30#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031#include "ring_buffer.h"
32#include "vis.h"
33#include "aggregation.h"
34#include "gateway_common.h"
35#include "gateway_client.h"
36#include "unicast.h"
37
38void slide_own_bcast_window(struct batman_if *batman_if)
39{
40 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
41 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000042 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 struct orig_node *orig_node;
45 unsigned long *word;
46 int i;
47 size_t word_index;
48
49 spin_lock_bh(&bat_priv->orig_hash_lock);
50
51 for (i = 0; i < hash->size; i++) {
52 head = &hash->table[i];
53
Marek Lindnerfb778ea2011-01-19 20:01:40 +000054 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000055 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000056 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057 word_index = batman_if->if_num * NUM_WORDS;
58 word = &(orig_node->bcast_own[word_index]);
59
60 bit_get_packet(bat_priv, word, 1, 0);
61 orig_node->bcast_own_sum[batman_if->if_num] =
62 bit_packet_count(word);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000063 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000065 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066 }
67
68 spin_unlock_bh(&bat_priv->orig_hash_lock);
69}
70
71static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
72 unsigned char *hna_buff, int hna_buff_len)
73{
74 if ((hna_buff_len != orig_node->hna_buff_len) ||
75 ((hna_buff_len > 0) &&
76 (orig_node->hna_buff_len > 0) &&
77 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
78
79 if (orig_node->hna_buff_len > 0)
80 hna_global_del_orig(bat_priv, orig_node,
81 "originator changed hna");
82
83 if ((hna_buff_len > 0) && (hna_buff))
84 hna_global_add_orig(bat_priv, orig_node,
85 hna_buff, hna_buff_len);
86 }
87}
88
89static void update_route(struct bat_priv *bat_priv,
90 struct orig_node *orig_node,
91 struct neigh_node *neigh_node,
92 unsigned char *hna_buff, int hna_buff_len)
93{
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000094 struct neigh_node *neigh_node_tmp;
95
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096 /* route deleted */
97 if ((orig_node->router) && (!neigh_node)) {
98
99 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
100 orig_node->orig);
101 hna_global_del_orig(bat_priv, orig_node,
102 "originator timed out");
103
104 /* route added */
105 } else if ((!orig_node->router) && (neigh_node)) {
106
107 bat_dbg(DBG_ROUTES, bat_priv,
108 "Adding route towards: %pM (via %pM)\n",
109 orig_node->orig, neigh_node->addr);
110 hna_global_add_orig(bat_priv, orig_node,
111 hna_buff, hna_buff_len);
112
113 /* route changed */
114 } else {
115 bat_dbg(DBG_ROUTES, bat_priv,
116 "Changing route towards: %pM "
117 "(now via %pM - was via %pM)\n",
118 orig_node->orig, neigh_node->addr,
119 orig_node->router->addr);
120 }
121
Marek Lindner44524fc2011-02-10 14:33:53 +0000122 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
123 neigh_node = NULL;
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000124 neigh_node_tmp = orig_node->router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 orig_node->router = neigh_node;
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000126 if (neigh_node_tmp)
Marek Lindner44524fc2011-02-10 14:33:53 +0000127 neigh_node_free_ref(neigh_node_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128}
129
130
131void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
132 struct neigh_node *neigh_node, unsigned char *hna_buff,
133 int hna_buff_len)
134{
135
136 if (!orig_node)
137 return;
138
139 if (orig_node->router != neigh_node)
140 update_route(bat_priv, orig_node, neigh_node,
141 hna_buff, hna_buff_len);
142 /* may be just HNA changed */
143 else
144 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
145}
146
147static int is_bidirectional_neigh(struct orig_node *orig_node,
148 struct orig_node *orig_neigh_node,
149 struct batman_packet *batman_packet,
150 struct batman_if *if_incoming)
151{
152 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
153 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Marek Lindner9591a792010-12-12 21:57:11 +0000154 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 unsigned char total_count;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000156 uint8_t orig_eq_count, neigh_rq_count, tq_own;
157 int tq_asym_penalty, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158
159 if (orig_node == orig_neigh_node) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000160 rcu_read_lock();
161 hlist_for_each_entry_rcu(tmp_neigh_node, node,
162 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163
Marek Lindner39901e72011-02-18 12:28:08 +0000164 if (compare_eth(tmp_neigh_node->addr,
165 orig_neigh_node->orig) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166 (tmp_neigh_node->if_incoming == if_incoming))
167 neigh_node = tmp_neigh_node;
168 }
169
170 if (!neigh_node)
171 neigh_node = create_neighbor(orig_node,
172 orig_neigh_node,
173 orig_neigh_node->orig,
174 if_incoming);
175 /* create_neighbor failed, return 0 */
176 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000177 goto unlock;
178
Marek Lindner44524fc2011-02-10 14:33:53 +0000179 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
180 neigh_node = NULL;
181 goto unlock;
182 }
183
Marek Lindnera775eb82011-01-19 20:01:39 +0000184 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185
186 neigh_node->last_valid = jiffies;
187 } else {
188 /* find packet count of corresponding one hop neighbor */
Marek Lindnerf987ed62010-12-12 21:57:12 +0000189 rcu_read_lock();
190 hlist_for_each_entry_rcu(tmp_neigh_node, node,
191 &orig_neigh_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Marek Lindner39901e72011-02-18 12:28:08 +0000193 if (compare_eth(tmp_neigh_node->addr,
194 orig_neigh_node->orig) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 (tmp_neigh_node->if_incoming == if_incoming))
196 neigh_node = tmp_neigh_node;
197 }
198
199 if (!neigh_node)
200 neigh_node = create_neighbor(orig_neigh_node,
201 orig_neigh_node,
202 orig_neigh_node->orig,
203 if_incoming);
204 /* create_neighbor failed, return 0 */
205 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000206 goto unlock;
207
Marek Lindner44524fc2011-02-10 14:33:53 +0000208 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
209 neigh_node = NULL;
210 goto unlock;
211 }
212
Marek Lindnera775eb82011-01-19 20:01:39 +0000213 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214 }
215
216 orig_node->last_valid = jiffies;
217
Marek Lindner0ede9f42011-01-25 21:52:10 +0000218 spin_lock_bh(&orig_node->ogm_cnt_lock);
219 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
220 neigh_rq_count = neigh_node->real_packet_count;
221 spin_unlock_bh(&orig_node->ogm_cnt_lock);
222
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223 /* pay attention to not get a value bigger than 100 % */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000224 total_count = (orig_eq_count > neigh_rq_count ?
225 neigh_rq_count : orig_eq_count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226
227 /* if we have too few packets (too less data) we set tq_own to zero */
228 /* if we receive too few packets it is not considered bidirectional */
229 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
Marek Lindner0ede9f42011-01-25 21:52:10 +0000230 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
231 tq_own = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232 else
233 /* neigh_node->real_packet_count is never zero as we
234 * only purge old information when getting new
235 * information */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000236 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
238 /*
239 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
240 * affect the nearly-symmetric links only a little, but
241 * punishes asymmetric links more. This will give a value
242 * between 0 and TQ_MAX_VALUE
243 */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000244 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
245 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
246 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
247 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
248 (TQ_LOCAL_WINDOW_SIZE *
249 TQ_LOCAL_WINDOW_SIZE *
250 TQ_LOCAL_WINDOW_SIZE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251
Marek Lindner0ede9f42011-01-25 21:52:10 +0000252 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
253 (TQ_MAX_VALUE * TQ_MAX_VALUE));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
255 bat_dbg(DBG_BATMAN, bat_priv,
256 "bidirectional: "
257 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
258 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
259 "total tq: %3i\n",
260 orig_node->orig, orig_neigh_node->orig, total_count,
Marek Lindner0ede9f42011-01-25 21:52:10 +0000261 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
263 /* if link has the minimum required transmission quality
264 * consider it bidirectional */
265 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnera775eb82011-01-19 20:01:39 +0000266 ret = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000267
Marek Lindnera775eb82011-01-19 20:01:39 +0000268 goto out;
269
270unlock:
271 rcu_read_unlock();
272out:
273 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000274 neigh_node_free_ref(neigh_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000275 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276}
277
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000278/* caller must hold the neigh_list_lock */
279void bonding_candidate_del(struct orig_node *orig_node,
280 struct neigh_node *neigh_node)
281{
282 /* this neighbor is not part of our candidate list */
283 if (list_empty(&neigh_node->bonding_list))
284 goto out;
285
286 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000287 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000288 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000289 atomic_dec(&orig_node->bond_candidates);
290
291out:
292 return;
293}
294
295static void bonding_candidate_add(struct orig_node *orig_node,
296 struct neigh_node *neigh_node)
297{
298 struct hlist_node *node;
299 struct neigh_node *tmp_neigh_node;
300 uint8_t best_tq, interference_candidate = 0;
301
302 spin_lock_bh(&orig_node->neigh_list_lock);
303
304 /* only consider if it has the same primary address ... */
Marek Lindner39901e72011-02-18 12:28:08 +0000305 if (!compare_eth(orig_node->orig,
306 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000307 goto candidate_del;
308
309 if (!orig_node->router)
310 goto candidate_del;
311
312 best_tq = orig_node->router->tq_avg;
313
314 /* ... and is good enough to be considered */
315 if (neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
316 goto candidate_del;
317
318 /**
319 * check if we have another candidate with the same mac address or
320 * interface. If we do, we won't select this candidate because of
321 * possible interference.
322 */
323 hlist_for_each_entry_rcu(tmp_neigh_node, node,
324 &orig_node->neigh_list, list) {
325
326 if (tmp_neigh_node == neigh_node)
327 continue;
328
329 /* we only care if the other candidate is even
330 * considered as candidate. */
331 if (list_empty(&tmp_neigh_node->bonding_list))
332 continue;
333
334 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Marek Lindner39901e72011-02-18 12:28:08 +0000335 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000336 interference_candidate = 1;
337 break;
338 }
339 }
340
341 /* don't care further if it is an interference candidate */
342 if (interference_candidate)
343 goto candidate_del;
344
345 /* this neighbor already is part of our candidate list */
346 if (!list_empty(&neigh_node->bonding_list))
347 goto out;
348
Marek Lindner44524fc2011-02-10 14:33:53 +0000349 if (!atomic_inc_not_zero(&neigh_node->refcount))
350 goto out;
351
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000352 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000353 atomic_inc(&orig_node->bond_candidates);
354 goto out;
355
356candidate_del:
357 bonding_candidate_del(orig_node, neigh_node);
358
359out:
360 spin_unlock_bh(&orig_node->neigh_list_lock);
361 return;
362}
363
364/* copy primary address for bonding */
365static void bonding_save_primary(struct orig_node *orig_node,
366 struct orig_node *orig_neigh_node,
367 struct batman_packet *batman_packet)
368{
369 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
370 return;
371
372 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
373}
374
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375static void update_orig(struct bat_priv *bat_priv,
376 struct orig_node *orig_node,
377 struct ethhdr *ethhdr,
378 struct batman_packet *batman_packet,
379 struct batman_if *if_incoming,
380 unsigned char *hna_buff, int hna_buff_len,
381 char is_duplicate)
382{
383 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000384 struct orig_node *orig_node_tmp;
Marek Lindner9591a792010-12-12 21:57:11 +0000385 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000386 int tmp_hna_buff_len;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000387 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
389 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
390 "Searching and updating originator entry of received packet\n");
391
Marek Lindnerf987ed62010-12-12 21:57:12 +0000392 rcu_read_lock();
393 hlist_for_each_entry_rcu(tmp_neigh_node, node,
394 &orig_node->neigh_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000395 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Marek Lindner44524fc2011-02-10 14:33:53 +0000396 (tmp_neigh_node->if_incoming == if_incoming) &&
397 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
398 if (neigh_node)
399 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000400 neigh_node = tmp_neigh_node;
401 continue;
402 }
403
404 if (is_duplicate)
405 continue;
406
407 ring_buffer_set(tmp_neigh_node->tq_recv,
408 &tmp_neigh_node->tq_index, 0);
409 tmp_neigh_node->tq_avg =
410 ring_buffer_avg(tmp_neigh_node->tq_recv);
411 }
412
413 if (!neigh_node) {
414 struct orig_node *orig_tmp;
415
416 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
417 if (!orig_tmp)
Marek Lindnera775eb82011-01-19 20:01:39 +0000418 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419
420 neigh_node = create_neighbor(orig_node, orig_tmp,
421 ethhdr->h_source, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000422
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000423 orig_node_free_ref(orig_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000425 goto unlock;
Marek Lindner44524fc2011-02-10 14:33:53 +0000426
427 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
428 neigh_node = NULL;
429 goto unlock;
430 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 } else
432 bat_dbg(DBG_BATMAN, bat_priv,
433 "Updating existing last-hop neighbor of originator\n");
434
Marek Lindnera775eb82011-01-19 20:01:39 +0000435 rcu_read_unlock();
436
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 orig_node->flags = batman_packet->flags;
438 neigh_node->last_valid = jiffies;
439
440 ring_buffer_set(neigh_node->tq_recv,
441 &neigh_node->tq_index,
442 batman_packet->tq);
443 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
444
445 if (!is_duplicate) {
446 orig_node->last_ttl = batman_packet->ttl;
447 neigh_node->last_ttl = batman_packet->ttl;
448 }
449
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000450 bonding_candidate_add(orig_node, neigh_node);
451
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
453 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
454
455 /* if this neighbor already is our next hop there is nothing
456 * to change */
457 if (orig_node->router == neigh_node)
458 goto update_hna;
459
460 /* if this neighbor does not offer a better TQ we won't consider it */
461 if ((orig_node->router) &&
462 (orig_node->router->tq_avg > neigh_node->tq_avg))
463 goto update_hna;
464
465 /* if the TQ is the same and the link not more symetric we
466 * won't consider it either */
467 if ((orig_node->router) &&
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000468 (neigh_node->tq_avg == orig_node->router->tq_avg)) {
469 orig_node_tmp = orig_node->router->orig_node;
470 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
471 bcast_own_sum_orig =
472 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
473 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
474
475 orig_node_tmp = neigh_node->orig_node;
476 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
477 bcast_own_sum_neigh =
478 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
479 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
480
481 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
482 goto update_hna;
483 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
485 update_routes(bat_priv, orig_node, neigh_node,
486 hna_buff, tmp_hna_buff_len);
487 goto update_gw;
488
489update_hna:
490 update_routes(bat_priv, orig_node, orig_node->router,
491 hna_buff, tmp_hna_buff_len);
492
493update_gw:
494 if (orig_node->gw_flags != batman_packet->gw_flags)
495 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
496
497 orig_node->gw_flags = batman_packet->gw_flags;
498
499 /* restart gateway selection if fast or late switching was enabled */
500 if ((orig_node->gw_flags) &&
501 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
502 (atomic_read(&bat_priv->gw_sel_class) > 2))
503 gw_check_election(bat_priv, orig_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000504
505 goto out;
506
507unlock:
508 rcu_read_unlock();
509out:
510 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000511 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000512}
513
514/* checks whether the host restarted and is in the protection time.
515 * returns:
516 * 0 if the packet is to be accepted
517 * 1 if the packet is to be ignored.
518 */
519static int window_protected(struct bat_priv *bat_priv,
520 int32_t seq_num_diff,
521 unsigned long *last_reset)
522{
523 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
524 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
525 if (time_after(jiffies, *last_reset +
526 msecs_to_jiffies(RESET_PROTECTION_MS))) {
527
528 *last_reset = jiffies;
529 bat_dbg(DBG_BATMAN, bat_priv,
530 "old packet received, start protection\n");
531
532 return 0;
533 } else
534 return 1;
535 }
536 return 0;
537}
538
539/* processes a batman packet for all interfaces, adjusts the sequence number and
540 * finds out whether it is a duplicate.
541 * returns:
542 * 1 the packet is a duplicate
543 * 0 the packet has not yet been received
544 * -1 the packet is old and has been received while the seqno window
545 * was protected. Caller should drop it.
546 */
547static char count_real_packets(struct ethhdr *ethhdr,
548 struct batman_packet *batman_packet,
549 struct batman_if *if_incoming)
550{
551 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
552 struct orig_node *orig_node;
553 struct neigh_node *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000554 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555 char is_duplicate = 0;
556 int32_t seq_diff;
557 int need_update = 0;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000558 int set_mark, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000559
560 orig_node = get_orig_node(bat_priv, batman_packet->orig);
561 if (!orig_node)
562 return 0;
563
Marek Lindner0ede9f42011-01-25 21:52:10 +0000564 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
566
567 /* signalize caller that the packet is to be dropped. */
568 if (window_protected(bat_priv, seq_diff,
569 &orig_node->batman_seqno_reset))
Marek Lindner0ede9f42011-01-25 21:52:10 +0000570 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571
Marek Lindnerf987ed62010-12-12 21:57:12 +0000572 rcu_read_lock();
573 hlist_for_each_entry_rcu(tmp_neigh_node, node,
574 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575
576 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
577 orig_node->last_real_seqno,
578 batman_packet->seqno);
579
Marek Lindner39901e72011-02-18 12:28:08 +0000580 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 (tmp_neigh_node->if_incoming == if_incoming))
582 set_mark = 1;
583 else
584 set_mark = 0;
585
586 /* if the window moved, set the update flag. */
587 need_update |= bit_get_packet(bat_priv,
588 tmp_neigh_node->real_bits,
589 seq_diff, set_mark);
590
591 tmp_neigh_node->real_packet_count =
592 bit_packet_count(tmp_neigh_node->real_bits);
593 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000594 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595
596 if (need_update) {
597 bat_dbg(DBG_BATMAN, bat_priv,
598 "updating last_seqno: old %d, new %d\n",
599 orig_node->last_real_seqno, batman_packet->seqno);
600 orig_node->last_real_seqno = batman_packet->seqno;
601 }
602
Marek Lindner0ede9f42011-01-25 21:52:10 +0000603 ret = is_duplicate;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000604
Marek Lindner0ede9f42011-01-25 21:52:10 +0000605out:
606 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000607 orig_node_free_ref(orig_node);
Marek Lindner0ede9f42011-01-25 21:52:10 +0000608 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609}
610
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611void receive_bat_packet(struct ethhdr *ethhdr,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000612 struct batman_packet *batman_packet,
613 unsigned char *hna_buff, int hna_buff_len,
614 struct batman_if *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615{
616 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
617 struct batman_if *batman_if;
618 struct orig_node *orig_neigh_node, *orig_node;
619 char has_directlink_flag;
620 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
621 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
622 char is_duplicate;
623 uint32_t if_incoming_seqno;
624
625 /* Silently drop when the batman packet is actually not a
626 * correct packet.
627 *
628 * This might happen if a packet is padded (e.g. Ethernet has a
629 * minimum frame length of 64 byte) and the aggregation interprets
630 * it as an additional length.
631 *
632 * TODO: A more sane solution would be to have a bit in the
633 * batman_packet to detect whether the packet is the last
634 * packet in an aggregation. Here we expect that the padding
635 * is always zero (or not 0x01)
636 */
637 if (batman_packet->packet_type != BAT_PACKET)
638 return;
639
640 /* could be changed by schedule_own_packet() */
641 if_incoming_seqno = atomic_read(&if_incoming->seqno);
642
643 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
644
Marek Lindner39901e72011-02-18 12:28:08 +0000645 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
646 batman_packet->orig) ? 1 : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647
648 bat_dbg(DBG_BATMAN, bat_priv,
649 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
650 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
651 "TTL %d, V %d, IDF %d)\n",
652 ethhdr->h_source, if_incoming->net_dev->name,
653 if_incoming->net_dev->dev_addr, batman_packet->orig,
654 batman_packet->prev_sender, batman_packet->seqno,
655 batman_packet->tq, batman_packet->ttl, batman_packet->version,
656 has_directlink_flag);
657
658 rcu_read_lock();
659 list_for_each_entry_rcu(batman_if, &if_list, list) {
660 if (batman_if->if_status != IF_ACTIVE)
661 continue;
662
663 if (batman_if->soft_iface != if_incoming->soft_iface)
664 continue;
665
Marek Lindner39901e72011-02-18 12:28:08 +0000666 if (compare_eth(ethhdr->h_source,
667 batman_if->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 is_my_addr = 1;
669
Marek Lindner39901e72011-02-18 12:28:08 +0000670 if (compare_eth(batman_packet->orig,
671 batman_if->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672 is_my_orig = 1;
673
Marek Lindner39901e72011-02-18 12:28:08 +0000674 if (compare_eth(batman_packet->prev_sender,
675 batman_if->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 is_my_oldorig = 1;
677
Marek Lindner39901e72011-02-18 12:28:08 +0000678 if (compare_eth(ethhdr->h_source, broadcast_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 is_broadcast = 1;
680 }
681 rcu_read_unlock();
682
683 if (batman_packet->version != COMPAT_VERSION) {
684 bat_dbg(DBG_BATMAN, bat_priv,
685 "Drop packet: incompatible batman version (%i)\n",
686 batman_packet->version);
687 return;
688 }
689
690 if (is_my_addr) {
691 bat_dbg(DBG_BATMAN, bat_priv,
692 "Drop packet: received my own broadcast (sender: %pM"
693 ")\n",
694 ethhdr->h_source);
695 return;
696 }
697
698 if (is_broadcast) {
699 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
700 "ignoring all packets with broadcast source addr (sender: %pM"
701 ")\n", ethhdr->h_source);
702 return;
703 }
704
705 if (is_my_orig) {
706 unsigned long *word;
707 int offset;
708
709 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710 if (!orig_neigh_node)
711 return;
712
713 /* neighbor has to indicate direct link and it has to
714 * come via the corresponding interface */
715 /* if received seqno equals last send seqno save new
716 * seqno for bidirectional check */
717 if (has_directlink_flag &&
Marek Lindner39901e72011-02-18 12:28:08 +0000718 compare_eth(if_incoming->net_dev->dev_addr,
719 batman_packet->orig) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
721 offset = if_incoming->if_num * NUM_WORDS;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000722
723 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 word = &(orig_neigh_node->bcast_own[offset]);
725 bit_mark(word, 0);
726 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
727 bit_packet_count(word);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000728 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 }
730
731 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
732 "originator packet from myself (via neighbor)\n");
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000733 orig_node_free_ref(orig_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734 return;
735 }
736
737 if (is_my_oldorig) {
738 bat_dbg(DBG_BATMAN, bat_priv,
739 "Drop packet: ignoring all rebroadcast echos (sender: "
740 "%pM)\n", ethhdr->h_source);
741 return;
742 }
743
744 orig_node = get_orig_node(bat_priv, batman_packet->orig);
745 if (!orig_node)
746 return;
747
748 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
749
750 if (is_duplicate == -1) {
751 bat_dbg(DBG_BATMAN, bat_priv,
752 "Drop packet: packet within seqno protection time "
753 "(sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000754 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 }
756
757 if (batman_packet->tq == 0) {
758 bat_dbg(DBG_BATMAN, bat_priv,
759 "Drop packet: originator packet with tq equal 0\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000760 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761 }
762
763 /* avoid temporary routing loops */
764 if ((orig_node->router) &&
765 (orig_node->router->orig_node->router) &&
Marek Lindner39901e72011-02-18 12:28:08 +0000766 (compare_eth(orig_node->router->addr,
767 batman_packet->prev_sender)) &&
768 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
769 (compare_eth(orig_node->router->addr,
770 orig_node->router->orig_node->router->addr))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771 bat_dbg(DBG_BATMAN, bat_priv,
772 "Drop packet: ignoring all rebroadcast packets that "
773 "may make me loop (sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000774 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 }
776
777 /* if sender is a direct neighbor the sender mac equals
778 * originator mac */
779 orig_neigh_node = (is_single_hop_neigh ?
780 orig_node :
781 get_orig_node(bat_priv, ethhdr->h_source));
782 if (!orig_neigh_node)
Marek Lindner16b1aba2011-01-19 20:01:42 +0000783 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
785 /* drop packet if sender is not a direct neighbor and if we
786 * don't route towards it */
787 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
788 bat_dbg(DBG_BATMAN, bat_priv,
789 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000790 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 }
792
793 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
794 batman_packet, if_incoming);
795
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000796 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
797
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798 /* update ranking if it is not a duplicate or has the same
799 * seqno and similar ttl as the non-duplicate */
800 if (is_bidirectional &&
801 (!is_duplicate ||
802 ((orig_node->last_real_seqno == batman_packet->seqno) &&
803 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
804 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
805 if_incoming, hna_buff, hna_buff_len, is_duplicate);
806
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807 /* is single hop (direct) neighbor */
808 if (is_single_hop_neigh) {
809
810 /* mark direct link on incoming interface */
811 schedule_forward_packet(orig_node, ethhdr, batman_packet,
812 1, hna_buff_len, if_incoming);
813
814 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
815 "rebroadcast neighbor packet with direct link flag\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000816 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817 }
818
819 /* multihop originator */
820 if (!is_bidirectional) {
821 bat_dbg(DBG_BATMAN, bat_priv,
822 "Drop packet: not received via bidirectional link\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000823 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824 }
825
826 if (is_duplicate) {
827 bat_dbg(DBG_BATMAN, bat_priv,
828 "Drop packet: duplicate packet received\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000829 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830 }
831
832 bat_dbg(DBG_BATMAN, bat_priv,
833 "Forwarding packet: rebroadcast originator packet\n");
834 schedule_forward_packet(orig_node, ethhdr, batman_packet,
835 0, hna_buff_len, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000836
837out_neigh:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000838 if ((orig_neigh_node) && (!is_single_hop_neigh))
839 orig_node_free_ref(orig_neigh_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000840out:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000841 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842}
843
844int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
845{
846 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
847 struct ethhdr *ethhdr;
848
849 /* drop packet if it has not necessary minimum size */
850 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
851 return NET_RX_DROP;
852
853 ethhdr = (struct ethhdr *)skb_mac_header(skb);
854
855 /* packet with broadcast indication but unicast recipient */
856 if (!is_broadcast_ether_addr(ethhdr->h_dest))
857 return NET_RX_DROP;
858
859 /* packet with broadcast sender address */
860 if (is_broadcast_ether_addr(ethhdr->h_source))
861 return NET_RX_DROP;
862
863 /* create a copy of the skb, if needed, to modify it. */
864 if (skb_cow(skb, 0) < 0)
865 return NET_RX_DROP;
866
867 /* keep skb linear */
868 if (skb_linearize(skb) < 0)
869 return NET_RX_DROP;
870
871 ethhdr = (struct ethhdr *)skb_mac_header(skb);
872
873 spin_lock_bh(&bat_priv->orig_hash_lock);
874 receive_aggr_bat_packet(ethhdr,
875 skb->data,
876 skb_headlen(skb),
877 batman_if);
878 spin_unlock_bh(&bat_priv->orig_hash_lock);
879
880 kfree_skb(skb);
881 return NET_RX_SUCCESS;
882}
883
884static int recv_my_icmp_packet(struct bat_priv *bat_priv,
885 struct sk_buff *skb, size_t icmp_len)
886{
Marek Lindner44524fc2011-02-10 14:33:53 +0000887 struct orig_node *orig_node = NULL;
888 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889 struct icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000890 struct batman_if *batman_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891 uint8_t dstaddr[ETH_ALEN];
Marek Lindner44524fc2011-02-10 14:33:53 +0000892 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893
894 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000895
896 /* add data to device queue */
897 if (icmp_packet->msg_type != ECHO_REQUEST) {
898 bat_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000899 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900 }
901
902 if (!bat_priv->primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000903 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905 /* answer echo request (ping) */
906 /* get routing information */
907 spin_lock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000908 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000909 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000910
911 if (!orig_node)
912 goto unlock;
913
Marek Lindner44524fc2011-02-10 14:33:53 +0000914 neigh_node = orig_node->router;
915
916 if (!neigh_node)
917 goto unlock;
918
919 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
920 neigh_node = NULL;
921 goto unlock;
922 }
923
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000924 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000925
Marek Lindner44524fc2011-02-10 14:33:53 +0000926 /* don't lock while sending the packets ... we therefore
927 * copy the required data before sending */
928 batman_if = orig_node->router->if_incoming;
929 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
930 spin_unlock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931
Marek Lindner44524fc2011-02-10 14:33:53 +0000932 /* create a copy of the skb, if needed, to modify it. */
933 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
934 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000935
Marek Lindner44524fc2011-02-10 14:33:53 +0000936 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000937
Marek Lindner44524fc2011-02-10 14:33:53 +0000938 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
939 memcpy(icmp_packet->orig,
940 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
941 icmp_packet->msg_type = ECHO_REPLY;
942 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943
Marek Lindner44524fc2011-02-10 14:33:53 +0000944 send_skb_packet(skb, batman_if, dstaddr);
945 ret = NET_RX_SUCCESS;
946 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947
Marek Lindner44524fc2011-02-10 14:33:53 +0000948unlock:
949 rcu_read_unlock();
950 spin_unlock_bh(&bat_priv->orig_hash_lock);
951out:
952 if (neigh_node)
953 neigh_node_free_ref(neigh_node);
954 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000955 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000956 return ret;
957}
958
959static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000960 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961{
Marek Lindner44524fc2011-02-10 14:33:53 +0000962 struct orig_node *orig_node = NULL;
963 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000964 struct icmp_packet *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000965 struct batman_if *batman_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966 uint8_t dstaddr[ETH_ALEN];
Marek Lindner44524fc2011-02-10 14:33:53 +0000967 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000968
969 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970
971 /* send TTL exceeded if packet is an echo request (traceroute) */
972 if (icmp_packet->msg_type != ECHO_REQUEST) {
973 pr_debug("Warning - can't forward icmp packet from %pM to "
974 "%pM: ttl exceeded\n", icmp_packet->orig,
975 icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000976 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977 }
978
979 if (!bat_priv->primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000980 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000981
982 /* get routing information */
983 spin_lock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000984 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000985 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000986
987 if (!orig_node)
988 goto unlock;
989
Marek Lindner44524fc2011-02-10 14:33:53 +0000990 neigh_node = orig_node->router;
991
992 if (!neigh_node)
993 goto unlock;
994
995 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
996 neigh_node = NULL;
997 goto unlock;
998 }
999
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001000 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001
Marek Lindner44524fc2011-02-10 14:33:53 +00001002 /* don't lock while sending the packets ... we therefore
1003 * copy the required data before sending */
1004 batman_if = orig_node->router->if_incoming;
1005 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1006 spin_unlock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001007
Marek Lindner44524fc2011-02-10 14:33:53 +00001008 /* create a copy of the skb, if needed, to modify it. */
1009 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1010 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011
Marek Lindner44524fc2011-02-10 14:33:53 +00001012 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001013
Marek Lindner44524fc2011-02-10 14:33:53 +00001014 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
1015 memcpy(icmp_packet->orig,
1016 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
1017 icmp_packet->msg_type = TTL_EXCEEDED;
1018 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019
Marek Lindner44524fc2011-02-10 14:33:53 +00001020 send_skb_packet(skb, batman_if, dstaddr);
1021 ret = NET_RX_SUCCESS;
1022 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023
Marek Lindner44524fc2011-02-10 14:33:53 +00001024unlock:
1025 rcu_read_unlock();
1026 spin_unlock_bh(&bat_priv->orig_hash_lock);
1027out:
1028 if (neigh_node)
1029 neigh_node_free_ref(neigh_node);
1030 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001031 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032 return ret;
1033}
1034
1035
1036int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
1037{
1038 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1039 struct icmp_packet_rr *icmp_packet;
1040 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +00001041 struct orig_node *orig_node = NULL;
1042 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043 struct batman_if *batman_if;
1044 int hdr_size = sizeof(struct icmp_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001045 uint8_t dstaddr[ETH_ALEN];
Marek Lindner44524fc2011-02-10 14:33:53 +00001046 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001047
1048 /**
1049 * we truncate all incoming icmp packets if they don't match our size
1050 */
1051 if (skb->len >= sizeof(struct icmp_packet_rr))
1052 hdr_size = sizeof(struct icmp_packet_rr);
1053
1054 /* drop packet if it has not necessary minimum size */
1055 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +00001056 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001057
1058 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1059
1060 /* packet with unicast indication but broadcast recipient */
1061 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001062 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001063
1064 /* packet with broadcast sender address */
1065 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +00001066 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067
1068 /* not for me */
1069 if (!is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001070 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001071
1072 icmp_packet = (struct icmp_packet_rr *)skb->data;
1073
1074 /* add record route information if not full */
1075 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1076 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1077 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1078 ethhdr->h_dest, ETH_ALEN);
1079 icmp_packet->rr_cur++;
1080 }
1081
1082 /* packet for me */
1083 if (is_my_mac(icmp_packet->dst))
1084 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1085
1086 /* TTL exceeded */
1087 if (icmp_packet->ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +00001088 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090 /* get routing information */
1091 spin_lock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001092 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +00001093 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
1094
Marek Lindner44524fc2011-02-10 14:33:53 +00001095 if (!orig_node)
1096 goto unlock;
1097
Marek Lindner44524fc2011-02-10 14:33:53 +00001098 neigh_node = orig_node->router;
1099
1100 if (!neigh_node)
1101 goto unlock;
1102
1103 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
1104 neigh_node = NULL;
1105 goto unlock;
1106 }
1107
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001108 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001109
Marek Lindner44524fc2011-02-10 14:33:53 +00001110 /* don't lock while sending the packets ... we therefore
1111 * copy the required data before sending */
1112 batman_if = orig_node->router->if_incoming;
1113 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1114 spin_unlock_bh(&bat_priv->orig_hash_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001115
Marek Lindner44524fc2011-02-10 14:33:53 +00001116 /* create a copy of the skb, if needed, to modify it. */
1117 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1118 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001119
Marek Lindner44524fc2011-02-10 14:33:53 +00001120 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001121
Marek Lindner44524fc2011-02-10 14:33:53 +00001122 /* decrement ttl */
1123 icmp_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001124
Marek Lindner44524fc2011-02-10 14:33:53 +00001125 /* route it */
1126 send_skb_packet(skb, batman_if, dstaddr);
1127 ret = NET_RX_SUCCESS;
1128 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129
Marek Lindner44524fc2011-02-10 14:33:53 +00001130unlock:
1131 rcu_read_unlock();
1132 spin_unlock_bh(&bat_priv->orig_hash_lock);
1133out:
1134 if (neigh_node)
1135 neigh_node_free_ref(neigh_node);
1136 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001137 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001138 return ret;
1139}
1140
1141/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001142 * bonding if possible. increases the found neighbors
1143 * refcount.*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144struct neigh_node *find_router(struct bat_priv *bat_priv,
1145 struct orig_node *orig_node,
1146 struct batman_if *recv_if)
1147{
1148 struct orig_node *primary_orig_node;
1149 struct orig_node *router_orig;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001150 struct neigh_node *router, *first_candidate, *tmp_neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001151 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1152 int bonding_enabled;
1153
1154 if (!orig_node)
1155 return NULL;
1156
1157 if (!orig_node->router)
1158 return NULL;
1159
1160 /* without bonding, the first node should
1161 * always choose the default router. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001162 bonding_enabled = atomic_read(&bat_priv->bonding);
1163
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001164 rcu_read_lock();
1165 /* select default router to output */
1166 router = orig_node->router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001167 router_orig = orig_node->router->orig_node;
Marek Lindner44524fc2011-02-10 14:33:53 +00001168 if (!router_orig || !atomic_inc_not_zero(&router->refcount)) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001169 rcu_read_unlock();
1170 return NULL;
1171 }
1172
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001173 if ((!recv_if) && (!bonding_enabled))
1174 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001175
1176 /* if we have something in the primary_addr, we can search
1177 * for a potential bonding candidate. */
Marek Lindner39901e72011-02-18 12:28:08 +00001178 if (compare_eth(router_orig->primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001179 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001180
1181 /* find the orig_node which has the primary interface. might
1182 * even be the same as our router_orig in many cases */
1183
Marek Lindner39901e72011-02-18 12:28:08 +00001184 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001185 primary_orig_node = router_orig;
1186 } else {
Marek Lindner7aadf882011-02-18 12:28:09 +00001187 primary_orig_node = orig_hash_find(bat_priv,
1188 router_orig->primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001189 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001190 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +00001191
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001192 orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001193 }
1194
1195 /* with less than 2 candidates, we can't do any
1196 * bonding and prefer the original router. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001197 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1198 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001199
1200
1201 /* all nodes between should choose a candidate which
1202 * is is not on the interface where the packet came
1203 * in. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001204
Marek Lindner44524fc2011-02-10 14:33:53 +00001205 neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001206 first_candidate = NULL;
1207 router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001208
1209 if (bonding_enabled) {
1210 /* in the bonding case, send the packets in a round
1211 * robin fashion over the remaining interfaces. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001212
1213 list_for_each_entry_rcu(tmp_neigh_node,
1214 &primary_orig_node->bond_list, bonding_list) {
1215 if (!first_candidate)
1216 first_candidate = tmp_neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001217 /* recv_if == NULL on the first node. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001218 if (tmp_neigh_node->if_incoming != recv_if &&
1219 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001220 router = tmp_neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001221 break;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001222 }
1223 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001224
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001225 /* use the first candidate if nothing was found. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001226 if (!router && first_candidate &&
1227 atomic_inc_not_zero(&first_candidate->refcount))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001228 router = first_candidate;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229
Marek Lindner44524fc2011-02-10 14:33:53 +00001230 if (!router) {
1231 rcu_read_unlock();
1232 return NULL;
1233 }
1234
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001235 /* selected should point to the next element
1236 * after the current router */
1237 spin_lock_bh(&primary_orig_node->neigh_list_lock);
1238 /* this is a list_move(), which unfortunately
1239 * does not exist as rcu version */
1240 list_del_rcu(&primary_orig_node->bond_list);
1241 list_add_rcu(&primary_orig_node->bond_list,
1242 &router->bonding_list);
1243 spin_unlock_bh(&primary_orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001244
1245 } else {
1246 /* if bonding is disabled, use the best of the
1247 * remaining candidates which are not using
1248 * this interface. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001249 list_for_each_entry_rcu(tmp_neigh_node,
1250 &primary_orig_node->bond_list, bonding_list) {
1251 if (!first_candidate)
1252 first_candidate = tmp_neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001253
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001254 /* recv_if == NULL on the first node. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001255 if (tmp_neigh_node->if_incoming == recv_if)
1256 continue;
1257
1258 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1259 continue;
1260
1261 /* if we don't have a router yet
1262 * or this one is better, choose it. */
1263 if ((!router) ||
1264 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1265 /* decrement refcount of
1266 * previously selected router */
1267 if (router)
1268 neigh_node_free_ref(router);
1269
1270 router = tmp_neigh_node;
1271 atomic_inc_not_zero(&router->refcount);
1272 }
1273
1274 neigh_node_free_ref(tmp_neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001275 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001276
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001277 /* use the first candidate if nothing was found. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001278 if (!router && first_candidate &&
1279 atomic_inc_not_zero(&first_candidate->refcount))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001280 router = first_candidate;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001281 }
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001282return_router:
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001283 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001284 return router;
1285}
1286
1287static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1288{
1289 struct ethhdr *ethhdr;
1290
1291 /* drop packet if it has not necessary minimum size */
1292 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1293 return -1;
1294
1295 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1296
1297 /* packet with unicast indication but broadcast recipient */
1298 if (is_broadcast_ether_addr(ethhdr->h_dest))
1299 return -1;
1300
1301 /* packet with broadcast sender address */
1302 if (is_broadcast_ether_addr(ethhdr->h_source))
1303 return -1;
1304
1305 /* not for me */
1306 if (!is_my_mac(ethhdr->h_dest))
1307 return -1;
1308
1309 return 0;
1310}
1311
1312int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1313 int hdr_size)
1314{
1315 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +00001316 struct orig_node *orig_node = NULL;
1317 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001318 struct batman_if *batman_if;
1319 uint8_t dstaddr[ETH_ALEN];
1320 struct unicast_packet *unicast_packet;
1321 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +00001322 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001323 struct sk_buff *new_skb;
1324
1325 unicast_packet = (struct unicast_packet *)skb->data;
1326
1327 /* TTL exceeded */
1328 if (unicast_packet->ttl < 2) {
1329 pr_debug("Warning - can't forward unicast packet from %pM to "
1330 "%pM: ttl exceeded\n", ethhdr->h_source,
1331 unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +00001332 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001333 }
1334
1335 /* get routing information */
1336 spin_lock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001337 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +00001338 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1339
Marek Lindner44524fc2011-02-10 14:33:53 +00001340 if (!orig_node)
1341 goto unlock;
1342
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001343 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001344
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001345 /* find_router() increases neigh_nodes refcount if found. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001346 neigh_node = find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347
Marek Lindner44524fc2011-02-10 14:33:53 +00001348 if (!neigh_node) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001349 spin_unlock_bh(&bat_priv->orig_hash_lock);
Marek Lindner44524fc2011-02-10 14:33:53 +00001350 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001351 }
1352
1353 /* don't lock while sending the packets ... we therefore
1354 * copy the required data before sending */
Marek Lindner44524fc2011-02-10 14:33:53 +00001355 batman_if = neigh_node->if_incoming;
1356 memcpy(dstaddr, neigh_node->addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001357 spin_unlock_bh(&bat_priv->orig_hash_lock);
1358
1359 /* create a copy of the skb, if needed, to modify it. */
1360 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +00001361 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001362
1363 unicast_packet = (struct unicast_packet *)skb->data;
1364
1365 if (unicast_packet->packet_type == BAT_UNICAST &&
1366 atomic_read(&bat_priv->fragmentation) &&
1367 skb->len > batman_if->net_dev->mtu)
1368 return frag_send_skb(skb, bat_priv, batman_if,
1369 dstaddr);
1370
1371 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
Sven Eckelmannae361ce2011-01-25 22:02:31 +00001372 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001373
1374 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1375
1376 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +00001377 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001378
1379 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +00001380 if (!new_skb) {
1381 ret = NET_RX_SUCCESS;
1382 goto out;
1383 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001384
1385 skb = new_skb;
1386 unicast_packet = (struct unicast_packet *)skb->data;
1387 }
1388
1389 /* decrement ttl */
1390 unicast_packet->ttl--;
1391
1392 /* route it */
1393 send_skb_packet(skb, batman_if, dstaddr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001394 ret = NET_RX_SUCCESS;
1395 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001396
Marek Lindner44524fc2011-02-10 14:33:53 +00001397unlock:
1398 rcu_read_unlock();
1399 spin_unlock_bh(&bat_priv->orig_hash_lock);
1400out:
1401 if (neigh_node)
1402 neigh_node_free_ref(neigh_node);
1403 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001404 orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +00001405 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001406}
1407
1408int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1409{
1410 struct unicast_packet *unicast_packet;
1411 int hdr_size = sizeof(struct unicast_packet);
1412
1413 if (check_unicast_packet(skb, hdr_size) < 0)
1414 return NET_RX_DROP;
1415
1416 unicast_packet = (struct unicast_packet *)skb->data;
1417
1418 /* packet for me */
1419 if (is_my_mac(unicast_packet->dest)) {
1420 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1421 return NET_RX_SUCCESS;
1422 }
1423
1424 return route_unicast_packet(skb, recv_if, hdr_size);
1425}
1426
1427int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1428{
1429 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1430 struct unicast_frag_packet *unicast_packet;
1431 int hdr_size = sizeof(struct unicast_frag_packet);
1432 struct sk_buff *new_skb = NULL;
1433 int ret;
1434
1435 if (check_unicast_packet(skb, hdr_size) < 0)
1436 return NET_RX_DROP;
1437
1438 unicast_packet = (struct unicast_frag_packet *)skb->data;
1439
1440 /* packet for me */
1441 if (is_my_mac(unicast_packet->dest)) {
1442
1443 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1444
1445 if (ret == NET_RX_DROP)
1446 return NET_RX_DROP;
1447
1448 /* packet was buffered for late merge */
1449 if (!new_skb)
1450 return NET_RX_SUCCESS;
1451
1452 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1453 sizeof(struct unicast_packet));
1454 return NET_RX_SUCCESS;
1455 }
1456
1457 return route_unicast_packet(skb, recv_if, hdr_size);
1458}
1459
1460
1461int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1462{
1463 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001464 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001465 struct bcast_packet *bcast_packet;
1466 struct ethhdr *ethhdr;
1467 int hdr_size = sizeof(struct bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001468 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001469 int32_t seq_diff;
1470
1471 /* drop packet if it has not necessary minimum size */
1472 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001473 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001474
1475 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1476
1477 /* packet with broadcast indication but unicast recipient */
1478 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001479 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001480
1481 /* packet with broadcast sender address */
1482 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001483 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001484
1485 /* ignore broadcasts sent by myself */
1486 if (is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001487 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001488
1489 bcast_packet = (struct bcast_packet *)skb->data;
1490
1491 /* ignore broadcasts originated by myself */
1492 if (is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001493 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001494
1495 if (bcast_packet->ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001496 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001497
1498 spin_lock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001499 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +00001500 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001501
1502 if (!orig_node)
1503 goto rcu_unlock;
1504
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001505 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001506
Marek Lindnerf3e00082011-01-25 21:52:11 +00001507 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001508
1509 /* check whether the packet is a duplicate */
Marek Lindnerf3e00082011-01-25 21:52:11 +00001510 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1511 ntohl(bcast_packet->seqno)))
1512 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001513
1514 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1515
1516 /* check whether the packet is old and the host just restarted. */
1517 if (window_protected(bat_priv, seq_diff,
Marek Lindnerf3e00082011-01-25 21:52:11 +00001518 &orig_node->bcast_seqno_reset))
1519 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001520
1521 /* mark broadcast in flood history, update window position
1522 * if required. */
1523 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1524 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1525
Marek Lindnerf3e00082011-01-25 21:52:11 +00001526 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001527 spin_unlock_bh(&bat_priv->orig_hash_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001528
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001529 /* rebroadcast packet */
1530 add_bcast_packet_to_list(bat_priv, skb);
1531
1532 /* broadcast for me */
1533 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001534 ret = NET_RX_SUCCESS;
1535 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001536
Marek Lindnerf3e00082011-01-25 21:52:11 +00001537rcu_unlock:
1538 rcu_read_unlock();
1539 spin_unlock_bh(&bat_priv->orig_hash_lock);
1540 goto out;
1541spin_unlock:
1542 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1543 spin_unlock_bh(&bat_priv->orig_hash_lock);
1544out:
1545 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001546 orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001547 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001548}
1549
1550int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1551{
1552 struct vis_packet *vis_packet;
1553 struct ethhdr *ethhdr;
1554 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1555 int hdr_size = sizeof(struct vis_packet);
1556
1557 /* keep skb linear */
1558 if (skb_linearize(skb) < 0)
1559 return NET_RX_DROP;
1560
1561 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1562 return NET_RX_DROP;
1563
1564 vis_packet = (struct vis_packet *)skb->data;
1565 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1566
1567 /* not for me */
1568 if (!is_my_mac(ethhdr->h_dest))
1569 return NET_RX_DROP;
1570
1571 /* ignore own packets */
1572 if (is_my_mac(vis_packet->vis_orig))
1573 return NET_RX_DROP;
1574
1575 if (is_my_mac(vis_packet->sender_orig))
1576 return NET_RX_DROP;
1577
1578 switch (vis_packet->vis_type) {
1579 case VIS_TYPE_SERVER_SYNC:
1580 receive_server_sync_packet(bat_priv, vis_packet,
1581 skb_headlen(skb));
1582 break;
1583
1584 case VIS_TYPE_CLIENT_UPDATE:
1585 receive_client_update_packet(bat_priv, vis_packet,
1586 skb_headlen(skb));
1587 break;
1588
1589 default: /* ignore unknown packet */
1590 break;
1591 }
1592
1593 /* We take a copy of the data in the packet, so we should
1594 always free the skbuf. */
1595 return NET_RX_DROP;
1596}