blob: c15e6c1c20b55fd65a36e363ab9c7c6b8dac6845 [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;
42 struct hlist_node *walk;
43 struct hlist_head *head;
44 struct element_t *bucket;
45 struct orig_node *orig_node;
46 unsigned long *word;
47 int i;
48 size_t word_index;
49
50 spin_lock_bh(&bat_priv->orig_hash_lock);
51
52 for (i = 0; i < hash->size; i++) {
53 head = &hash->table[i];
54
55 hlist_for_each_entry(bucket, walk, head, hlist) {
56 orig_node = bucket->data;
57 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);
63 }
64 }
65
66 spin_unlock_bh(&bat_priv->orig_hash_lock);
67}
68
69static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
70 unsigned char *hna_buff, int hna_buff_len)
71{
72 if ((hna_buff_len != orig_node->hna_buff_len) ||
73 ((hna_buff_len > 0) &&
74 (orig_node->hna_buff_len > 0) &&
75 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
76
77 if (orig_node->hna_buff_len > 0)
78 hna_global_del_orig(bat_priv, orig_node,
79 "originator changed hna");
80
81 if ((hna_buff_len > 0) && (hna_buff))
82 hna_global_add_orig(bat_priv, orig_node,
83 hna_buff, hna_buff_len);
84 }
85}
86
87static void update_route(struct bat_priv *bat_priv,
88 struct orig_node *orig_node,
89 struct neigh_node *neigh_node,
90 unsigned char *hna_buff, int hna_buff_len)
91{
Marek Lindnera8e7f4b2010-12-12 21:57:10 +000092 struct neigh_node *neigh_node_tmp;
93
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094 /* route deleted */
95 if ((orig_node->router) && (!neigh_node)) {
96
97 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
98 orig_node->orig);
99 hna_global_del_orig(bat_priv, orig_node,
100 "originator timed out");
101
102 /* route added */
103 } else if ((!orig_node->router) && (neigh_node)) {
104
105 bat_dbg(DBG_ROUTES, bat_priv,
106 "Adding route towards: %pM (via %pM)\n",
107 orig_node->orig, neigh_node->addr);
108 hna_global_add_orig(bat_priv, orig_node,
109 hna_buff, hna_buff_len);
110
111 /* route changed */
112 } else {
113 bat_dbg(DBG_ROUTES, bat_priv,
114 "Changing route towards: %pM "
115 "(now via %pM - was via %pM)\n",
116 orig_node->orig, neigh_node->addr,
117 orig_node->router->addr);
118 }
119
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000120 if (neigh_node)
121 kref_get(&neigh_node->refcount);
122 neigh_node_tmp = orig_node->router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 orig_node->router = neigh_node;
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000124 if (neigh_node_tmp)
125 kref_put(&neigh_node_tmp->refcount, neigh_node_free_ref);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126}
127
128
129void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
130 struct neigh_node *neigh_node, unsigned char *hna_buff,
131 int hna_buff_len)
132{
133
134 if (!orig_node)
135 return;
136
137 if (orig_node->router != neigh_node)
138 update_route(bat_priv, orig_node, neigh_node,
139 hna_buff, hna_buff_len);
140 /* may be just HNA changed */
141 else
142 update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
143}
144
145static int is_bidirectional_neigh(struct orig_node *orig_node,
146 struct orig_node *orig_neigh_node,
147 struct batman_packet *batman_packet,
148 struct batman_if *if_incoming)
149{
150 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
151 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Marek Lindner9591a792010-12-12 21:57:11 +0000152 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 unsigned char total_count;
Marek Lindnera775eb82011-01-19 20:01:39 +0000154 int ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155
156 if (orig_node == orig_neigh_node) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000157 rcu_read_lock();
158 hlist_for_each_entry_rcu(tmp_neigh_node, node,
159 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
161 if (compare_orig(tmp_neigh_node->addr,
162 orig_neigh_node->orig) &&
163 (tmp_neigh_node->if_incoming == if_incoming))
164 neigh_node = tmp_neigh_node;
165 }
166
167 if (!neigh_node)
168 neigh_node = create_neighbor(orig_node,
169 orig_neigh_node,
170 orig_neigh_node->orig,
171 if_incoming);
172 /* create_neighbor failed, return 0 */
173 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000174 goto unlock;
175
176 kref_get(&neigh_node->refcount);
177 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178
179 neigh_node->last_valid = jiffies;
180 } else {
181 /* find packet count of corresponding one hop neighbor */
Marek Lindnerf987ed62010-12-12 21:57:12 +0000182 rcu_read_lock();
183 hlist_for_each_entry_rcu(tmp_neigh_node, node,
184 &orig_neigh_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185
186 if (compare_orig(tmp_neigh_node->addr,
187 orig_neigh_node->orig) &&
188 (tmp_neigh_node->if_incoming == if_incoming))
189 neigh_node = tmp_neigh_node;
190 }
191
192 if (!neigh_node)
193 neigh_node = create_neighbor(orig_neigh_node,
194 orig_neigh_node,
195 orig_neigh_node->orig,
196 if_incoming);
197 /* create_neighbor failed, return 0 */
198 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000199 goto unlock;
200
201 kref_get(&neigh_node->refcount);
202 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203 }
204
205 orig_node->last_valid = jiffies;
206
207 /* pay attention to not get a value bigger than 100 % */
208 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
209 neigh_node->real_packet_count ?
210 neigh_node->real_packet_count :
211 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
212
213 /* if we have too few packets (too less data) we set tq_own to zero */
214 /* if we receive too few packets it is not considered bidirectional */
215 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
216 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
217 orig_neigh_node->tq_own = 0;
218 else
219 /* neigh_node->real_packet_count is never zero as we
220 * only purge old information when getting new
221 * information */
222 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
223 neigh_node->real_packet_count;
224
225 /*
226 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
227 * affect the nearly-symmetric links only a little, but
228 * punishes asymmetric links more. This will give a value
229 * between 0 and TQ_MAX_VALUE
230 */
231 orig_neigh_node->tq_asym_penalty =
232 TQ_MAX_VALUE -
233 (TQ_MAX_VALUE *
234 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
235 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
236 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
237 (TQ_LOCAL_WINDOW_SIZE *
238 TQ_LOCAL_WINDOW_SIZE *
239 TQ_LOCAL_WINDOW_SIZE);
240
241 batman_packet->tq = ((batman_packet->tq *
242 orig_neigh_node->tq_own *
243 orig_neigh_node->tq_asym_penalty) /
244 (TQ_MAX_VALUE * TQ_MAX_VALUE));
245
246 bat_dbg(DBG_BATMAN, bat_priv,
247 "bidirectional: "
248 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
249 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
250 "total tq: %3i\n",
251 orig_node->orig, orig_neigh_node->orig, total_count,
252 neigh_node->real_packet_count, orig_neigh_node->tq_own,
253 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
254
255 /* if link has the minimum required transmission quality
256 * consider it bidirectional */
257 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnera775eb82011-01-19 20:01:39 +0000258 ret = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Marek Lindnera775eb82011-01-19 20:01:39 +0000260 goto out;
261
262unlock:
263 rcu_read_unlock();
264out:
265 if (neigh_node)
266 kref_put(&neigh_node->refcount, neigh_node_free_ref);
267 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268}
269
270static void update_orig(struct bat_priv *bat_priv,
271 struct orig_node *orig_node,
272 struct ethhdr *ethhdr,
273 struct batman_packet *batman_packet,
274 struct batman_if *if_incoming,
275 unsigned char *hna_buff, int hna_buff_len,
276 char is_duplicate)
277{
278 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Marek Lindner9591a792010-12-12 21:57:11 +0000279 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280 int tmp_hna_buff_len;
281
282 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
283 "Searching and updating originator entry of received packet\n");
284
Marek Lindnerf987ed62010-12-12 21:57:12 +0000285 rcu_read_lock();
286 hlist_for_each_entry_rcu(tmp_neigh_node, node,
287 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
289 (tmp_neigh_node->if_incoming == if_incoming)) {
290 neigh_node = tmp_neigh_node;
291 continue;
292 }
293
294 if (is_duplicate)
295 continue;
296
297 ring_buffer_set(tmp_neigh_node->tq_recv,
298 &tmp_neigh_node->tq_index, 0);
299 tmp_neigh_node->tq_avg =
300 ring_buffer_avg(tmp_neigh_node->tq_recv);
301 }
302
303 if (!neigh_node) {
304 struct orig_node *orig_tmp;
305
306 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
307 if (!orig_tmp)
Marek Lindnera775eb82011-01-19 20:01:39 +0000308 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309
310 neigh_node = create_neighbor(orig_node, orig_tmp,
311 ethhdr->h_source, if_incoming);
312 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000313 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314 } else
315 bat_dbg(DBG_BATMAN, bat_priv,
316 "Updating existing last-hop neighbor of originator\n");
317
Marek Lindnera775eb82011-01-19 20:01:39 +0000318 kref_get(&neigh_node->refcount);
319 rcu_read_unlock();
320
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 orig_node->flags = batman_packet->flags;
322 neigh_node->last_valid = jiffies;
323
324 ring_buffer_set(neigh_node->tq_recv,
325 &neigh_node->tq_index,
326 batman_packet->tq);
327 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
328
329 if (!is_duplicate) {
330 orig_node->last_ttl = batman_packet->ttl;
331 neigh_node->last_ttl = batman_packet->ttl;
332 }
333
334 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
335 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
336
337 /* if this neighbor already is our next hop there is nothing
338 * to change */
339 if (orig_node->router == neigh_node)
340 goto update_hna;
341
342 /* if this neighbor does not offer a better TQ we won't consider it */
343 if ((orig_node->router) &&
344 (orig_node->router->tq_avg > neigh_node->tq_avg))
345 goto update_hna;
346
347 /* if the TQ is the same and the link not more symetric we
348 * won't consider it either */
349 if ((orig_node->router) &&
350 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
351 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
352 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
353 goto update_hna;
354
355 update_routes(bat_priv, orig_node, neigh_node,
356 hna_buff, tmp_hna_buff_len);
357 goto update_gw;
358
359update_hna:
360 update_routes(bat_priv, orig_node, orig_node->router,
361 hna_buff, tmp_hna_buff_len);
362
363update_gw:
364 if (orig_node->gw_flags != batman_packet->gw_flags)
365 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
366
367 orig_node->gw_flags = batman_packet->gw_flags;
368
369 /* restart gateway selection if fast or late switching was enabled */
370 if ((orig_node->gw_flags) &&
371 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
372 (atomic_read(&bat_priv->gw_sel_class) > 2))
373 gw_check_election(bat_priv, orig_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000374
375 goto out;
376
377unlock:
378 rcu_read_unlock();
379out:
380 if (neigh_node)
381 kref_put(&neigh_node->refcount, neigh_node_free_ref);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382}
383
384/* checks whether the host restarted and is in the protection time.
385 * returns:
386 * 0 if the packet is to be accepted
387 * 1 if the packet is to be ignored.
388 */
389static int window_protected(struct bat_priv *bat_priv,
390 int32_t seq_num_diff,
391 unsigned long *last_reset)
392{
393 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
394 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
395 if (time_after(jiffies, *last_reset +
396 msecs_to_jiffies(RESET_PROTECTION_MS))) {
397
398 *last_reset = jiffies;
399 bat_dbg(DBG_BATMAN, bat_priv,
400 "old packet received, start protection\n");
401
402 return 0;
403 } else
404 return 1;
405 }
406 return 0;
407}
408
409/* processes a batman packet for all interfaces, adjusts the sequence number and
410 * finds out whether it is a duplicate.
411 * returns:
412 * 1 the packet is a duplicate
413 * 0 the packet has not yet been received
414 * -1 the packet is old and has been received while the seqno window
415 * was protected. Caller should drop it.
416 */
417static char count_real_packets(struct ethhdr *ethhdr,
418 struct batman_packet *batman_packet,
419 struct batman_if *if_incoming)
420{
421 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
422 struct orig_node *orig_node;
423 struct neigh_node *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000424 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425 char is_duplicate = 0;
426 int32_t seq_diff;
427 int need_update = 0;
428 int set_mark;
429
430 orig_node = get_orig_node(bat_priv, batman_packet->orig);
431 if (!orig_node)
432 return 0;
433
434 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
435
436 /* signalize caller that the packet is to be dropped. */
437 if (window_protected(bat_priv, seq_diff,
438 &orig_node->batman_seqno_reset))
439 return -1;
440
Marek Lindnerf987ed62010-12-12 21:57:12 +0000441 rcu_read_lock();
442 hlist_for_each_entry_rcu(tmp_neigh_node, node,
443 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444
445 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
446 orig_node->last_real_seqno,
447 batman_packet->seqno);
448
449 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
450 (tmp_neigh_node->if_incoming == if_incoming))
451 set_mark = 1;
452 else
453 set_mark = 0;
454
455 /* if the window moved, set the update flag. */
456 need_update |= bit_get_packet(bat_priv,
457 tmp_neigh_node->real_bits,
458 seq_diff, set_mark);
459
460 tmp_neigh_node->real_packet_count =
461 bit_packet_count(tmp_neigh_node->real_bits);
462 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000463 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
465 if (need_update) {
466 bat_dbg(DBG_BATMAN, bat_priv,
467 "updating last_seqno: old %d, new %d\n",
468 orig_node->last_real_seqno, batman_packet->seqno);
469 orig_node->last_real_seqno = batman_packet->seqno;
470 }
471
472 return is_duplicate;
473}
474
475/* copy primary address for bonding */
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000476static void mark_bonding_address(struct orig_node *orig_node,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000477 struct orig_node *orig_neigh_node,
478 struct batman_packet *batman_packet)
479
480{
481 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
482 memcpy(orig_neigh_node->primary_addr,
483 orig_node->orig, ETH_ALEN);
484
485 return;
486}
487
488/* mark possible bond.candidates in the neighbor list */
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000489void update_bonding_candidates(struct orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490{
491 int candidates;
492 int interference_candidate;
493 int best_tq;
Marek Lindner9591a792010-12-12 21:57:11 +0000494 struct hlist_node *node, *node2;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
496 struct neigh_node *first_candidate, *last_candidate;
497
498 /* update the candidates for this originator */
499 if (!orig_node->router) {
500 orig_node->bond.candidates = 0;
501 return;
502 }
503
504 best_tq = orig_node->router->tq_avg;
505
506 /* update bond.candidates */
507
508 candidates = 0;
509
510 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
511 * as "bonding partner" */
512
513 /* first, zero the list */
Marek Lindnerf987ed62010-12-12 21:57:12 +0000514 rcu_read_lock();
515 hlist_for_each_entry_rcu(tmp_neigh_node, node,
516 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517 tmp_neigh_node->next_bond_candidate = NULL;
518 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000519 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520
521 first_candidate = NULL;
522 last_candidate = NULL;
Marek Lindnerf987ed62010-12-12 21:57:12 +0000523
524 rcu_read_lock();
525 hlist_for_each_entry_rcu(tmp_neigh_node, node,
526 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527
528 /* only consider if it has the same primary address ... */
529 if (memcmp(orig_node->orig,
530 tmp_neigh_node->orig_node->primary_addr,
531 ETH_ALEN) != 0)
532 continue;
533
534 /* ... and is good enough to be considered */
535 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
536 continue;
537
538 /* check if we have another candidate with the same
539 * mac address or interface. If we do, we won't
540 * select this candidate because of possible interference. */
541
542 interference_candidate = 0;
Marek Lindnerf987ed62010-12-12 21:57:12 +0000543 hlist_for_each_entry_rcu(tmp_neigh_node2, node2,
544 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545
546 if (tmp_neigh_node2 == tmp_neigh_node)
547 continue;
548
549 /* we only care if the other candidate is even
550 * considered as candidate. */
551 if (!tmp_neigh_node2->next_bond_candidate)
552 continue;
553
554
555 if ((tmp_neigh_node->if_incoming ==
556 tmp_neigh_node2->if_incoming)
557 || (memcmp(tmp_neigh_node->addr,
558 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
559
560 interference_candidate = 1;
561 break;
562 }
563 }
564 /* don't care further if it is an interference candidate */
565 if (interference_candidate)
566 continue;
567
568 if (!first_candidate) {
569 first_candidate = tmp_neigh_node;
570 tmp_neigh_node->next_bond_candidate = first_candidate;
571 } else
572 tmp_neigh_node->next_bond_candidate = last_candidate;
573
574 last_candidate = tmp_neigh_node;
575
576 candidates++;
577 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000578 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
580 if (candidates > 0) {
581 first_candidate->next_bond_candidate = last_candidate;
582 orig_node->bond.selected = first_candidate;
583 }
584
585 orig_node->bond.candidates = candidates;
586}
587
588void receive_bat_packet(struct ethhdr *ethhdr,
589 struct batman_packet *batman_packet,
590 unsigned char *hna_buff, int hna_buff_len,
591 struct batman_if *if_incoming)
592{
593 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
594 struct batman_if *batman_if;
595 struct orig_node *orig_neigh_node, *orig_node;
596 char has_directlink_flag;
597 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
598 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
599 char is_duplicate;
600 uint32_t if_incoming_seqno;
601
602 /* Silently drop when the batman packet is actually not a
603 * correct packet.
604 *
605 * This might happen if a packet is padded (e.g. Ethernet has a
606 * minimum frame length of 64 byte) and the aggregation interprets
607 * it as an additional length.
608 *
609 * TODO: A more sane solution would be to have a bit in the
610 * batman_packet to detect whether the packet is the last
611 * packet in an aggregation. Here we expect that the padding
612 * is always zero (or not 0x01)
613 */
614 if (batman_packet->packet_type != BAT_PACKET)
615 return;
616
617 /* could be changed by schedule_own_packet() */
618 if_incoming_seqno = atomic_read(&if_incoming->seqno);
619
620 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
621
622 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
623 batman_packet->orig) ? 1 : 0);
624
625 bat_dbg(DBG_BATMAN, bat_priv,
626 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
627 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
628 "TTL %d, V %d, IDF %d)\n",
629 ethhdr->h_source, if_incoming->net_dev->name,
630 if_incoming->net_dev->dev_addr, batman_packet->orig,
631 batman_packet->prev_sender, batman_packet->seqno,
632 batman_packet->tq, batman_packet->ttl, batman_packet->version,
633 has_directlink_flag);
634
635 rcu_read_lock();
636 list_for_each_entry_rcu(batman_if, &if_list, list) {
637 if (batman_if->if_status != IF_ACTIVE)
638 continue;
639
640 if (batman_if->soft_iface != if_incoming->soft_iface)
641 continue;
642
643 if (compare_orig(ethhdr->h_source,
644 batman_if->net_dev->dev_addr))
645 is_my_addr = 1;
646
647 if (compare_orig(batman_packet->orig,
648 batman_if->net_dev->dev_addr))
649 is_my_orig = 1;
650
651 if (compare_orig(batman_packet->prev_sender,
652 batman_if->net_dev->dev_addr))
653 is_my_oldorig = 1;
654
655 if (compare_orig(ethhdr->h_source, broadcast_addr))
656 is_broadcast = 1;
657 }
658 rcu_read_unlock();
659
660 if (batman_packet->version != COMPAT_VERSION) {
661 bat_dbg(DBG_BATMAN, bat_priv,
662 "Drop packet: incompatible batman version (%i)\n",
663 batman_packet->version);
664 return;
665 }
666
667 if (is_my_addr) {
668 bat_dbg(DBG_BATMAN, bat_priv,
669 "Drop packet: received my own broadcast (sender: %pM"
670 ")\n",
671 ethhdr->h_source);
672 return;
673 }
674
675 if (is_broadcast) {
676 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
677 "ignoring all packets with broadcast source addr (sender: %pM"
678 ")\n", ethhdr->h_source);
679 return;
680 }
681
682 if (is_my_orig) {
683 unsigned long *word;
684 int offset;
685
686 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
687
688 if (!orig_neigh_node)
689 return;
690
691 /* neighbor has to indicate direct link and it has to
692 * come via the corresponding interface */
693 /* if received seqno equals last send seqno save new
694 * seqno for bidirectional check */
695 if (has_directlink_flag &&
696 compare_orig(if_incoming->net_dev->dev_addr,
697 batman_packet->orig) &&
698 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
699 offset = if_incoming->if_num * NUM_WORDS;
700 word = &(orig_neigh_node->bcast_own[offset]);
701 bit_mark(word, 0);
702 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
703 bit_packet_count(word);
704 }
705
706 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
707 "originator packet from myself (via neighbor)\n");
708 return;
709 }
710
711 if (is_my_oldorig) {
712 bat_dbg(DBG_BATMAN, bat_priv,
713 "Drop packet: ignoring all rebroadcast echos (sender: "
714 "%pM)\n", ethhdr->h_source);
715 return;
716 }
717
718 orig_node = get_orig_node(bat_priv, batman_packet->orig);
719 if (!orig_node)
720 return;
721
722 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
723
724 if (is_duplicate == -1) {
725 bat_dbg(DBG_BATMAN, bat_priv,
726 "Drop packet: packet within seqno protection time "
727 "(sender: %pM)\n", ethhdr->h_source);
728 return;
729 }
730
731 if (batman_packet->tq == 0) {
732 bat_dbg(DBG_BATMAN, bat_priv,
733 "Drop packet: originator packet with tq equal 0\n");
734 return;
735 }
736
737 /* avoid temporary routing loops */
738 if ((orig_node->router) &&
739 (orig_node->router->orig_node->router) &&
740 (compare_orig(orig_node->router->addr,
741 batman_packet->prev_sender)) &&
742 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
743 (compare_orig(orig_node->router->addr,
744 orig_node->router->orig_node->router->addr))) {
745 bat_dbg(DBG_BATMAN, bat_priv,
746 "Drop packet: ignoring all rebroadcast packets that "
747 "may make me loop (sender: %pM)\n", ethhdr->h_source);
748 return;
749 }
750
751 /* if sender is a direct neighbor the sender mac equals
752 * originator mac */
753 orig_neigh_node = (is_single_hop_neigh ?
754 orig_node :
755 get_orig_node(bat_priv, ethhdr->h_source));
756 if (!orig_neigh_node)
757 return;
758
759 /* drop packet if sender is not a direct neighbor and if we
760 * don't route towards it */
761 if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
762 bat_dbg(DBG_BATMAN, bat_priv,
763 "Drop packet: OGM via unknown neighbor!\n");
764 return;
765 }
766
767 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
768 batman_packet, if_incoming);
769
770 /* update ranking if it is not a duplicate or has the same
771 * seqno and similar ttl as the non-duplicate */
772 if (is_bidirectional &&
773 (!is_duplicate ||
774 ((orig_node->last_real_seqno == batman_packet->seqno) &&
775 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
776 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
777 if_incoming, hna_buff, hna_buff_len, is_duplicate);
778
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000779 mark_bonding_address(orig_node, orig_neigh_node, batman_packet);
780 update_bonding_candidates(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781
782 /* is single hop (direct) neighbor */
783 if (is_single_hop_neigh) {
784
785 /* mark direct link on incoming interface */
786 schedule_forward_packet(orig_node, ethhdr, batman_packet,
787 1, hna_buff_len, if_incoming);
788
789 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
790 "rebroadcast neighbor packet with direct link flag\n");
791 return;
792 }
793
794 /* multihop originator */
795 if (!is_bidirectional) {
796 bat_dbg(DBG_BATMAN, bat_priv,
797 "Drop packet: not received via bidirectional link\n");
798 return;
799 }
800
801 if (is_duplicate) {
802 bat_dbg(DBG_BATMAN, bat_priv,
803 "Drop packet: duplicate packet received\n");
804 return;
805 }
806
807 bat_dbg(DBG_BATMAN, bat_priv,
808 "Forwarding packet: rebroadcast originator packet\n");
809 schedule_forward_packet(orig_node, ethhdr, batman_packet,
810 0, hna_buff_len, if_incoming);
811}
812
813int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if)
814{
815 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
816 struct ethhdr *ethhdr;
817
818 /* drop packet if it has not necessary minimum size */
819 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
820 return NET_RX_DROP;
821
822 ethhdr = (struct ethhdr *)skb_mac_header(skb);
823
824 /* packet with broadcast indication but unicast recipient */
825 if (!is_broadcast_ether_addr(ethhdr->h_dest))
826 return NET_RX_DROP;
827
828 /* packet with broadcast sender address */
829 if (is_broadcast_ether_addr(ethhdr->h_source))
830 return NET_RX_DROP;
831
832 /* create a copy of the skb, if needed, to modify it. */
833 if (skb_cow(skb, 0) < 0)
834 return NET_RX_DROP;
835
836 /* keep skb linear */
837 if (skb_linearize(skb) < 0)
838 return NET_RX_DROP;
839
840 ethhdr = (struct ethhdr *)skb_mac_header(skb);
841
842 spin_lock_bh(&bat_priv->orig_hash_lock);
843 receive_aggr_bat_packet(ethhdr,
844 skb->data,
845 skb_headlen(skb),
846 batman_if);
847 spin_unlock_bh(&bat_priv->orig_hash_lock);
848
849 kfree_skb(skb);
850 return NET_RX_SUCCESS;
851}
852
853static int recv_my_icmp_packet(struct bat_priv *bat_priv,
854 struct sk_buff *skb, size_t icmp_len)
855{
856 struct orig_node *orig_node;
857 struct icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858 struct batman_if *batman_if;
859 int ret;
860 uint8_t dstaddr[ETH_ALEN];
861
862 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
864 /* add data to device queue */
865 if (icmp_packet->msg_type != ECHO_REQUEST) {
866 bat_socket_receive_packet(icmp_packet, icmp_len);
867 return NET_RX_DROP;
868 }
869
870 if (!bat_priv->primary_if)
871 return NET_RX_DROP;
872
873 /* answer echo request (ping) */
874 /* get routing information */
875 spin_lock_bh(&bat_priv->orig_hash_lock);
876 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
877 compare_orig, choose_orig,
878 icmp_packet->orig));
879 ret = NET_RX_DROP;
880
881 if ((orig_node) && (orig_node->router)) {
882
883 /* don't lock while sending the packets ... we therefore
884 * copy the required data before sending */
885 batman_if = orig_node->router->if_incoming;
886 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
887 spin_unlock_bh(&bat_priv->orig_hash_lock);
888
889 /* create a copy of the skb, if needed, to modify it. */
890 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
891 return NET_RX_DROP;
892
893 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
895 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
896 memcpy(icmp_packet->orig,
897 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
898 icmp_packet->msg_type = ECHO_REPLY;
899 icmp_packet->ttl = TTL;
900
901 send_skb_packet(skb, batman_if, dstaddr);
902 ret = NET_RX_SUCCESS;
903
904 } else
905 spin_unlock_bh(&bat_priv->orig_hash_lock);
906
907 return ret;
908}
909
910static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000911 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000912{
913 struct orig_node *orig_node;
914 struct icmp_packet *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915 struct batman_if *batman_if;
916 int ret;
917 uint8_t dstaddr[ETH_ALEN];
918
919 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000920
921 /* send TTL exceeded if packet is an echo request (traceroute) */
922 if (icmp_packet->msg_type != ECHO_REQUEST) {
923 pr_debug("Warning - can't forward icmp packet from %pM to "
924 "%pM: ttl exceeded\n", icmp_packet->orig,
925 icmp_packet->dst);
926 return NET_RX_DROP;
927 }
928
929 if (!bat_priv->primary_if)
930 return NET_RX_DROP;
931
932 /* get routing information */
933 spin_lock_bh(&bat_priv->orig_hash_lock);
934 orig_node = ((struct orig_node *)
935 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
936 icmp_packet->orig));
937 ret = NET_RX_DROP;
938
939 if ((orig_node) && (orig_node->router)) {
940
941 /* don't lock while sending the packets ... we therefore
942 * copy the required data before sending */
943 batman_if = orig_node->router->if_incoming;
944 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
945 spin_unlock_bh(&bat_priv->orig_hash_lock);
946
947 /* create a copy of the skb, if needed, to modify it. */
948 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
949 return NET_RX_DROP;
950
951 icmp_packet = (struct icmp_packet *) skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000952
953 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
954 memcpy(icmp_packet->orig,
955 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
956 icmp_packet->msg_type = TTL_EXCEEDED;
957 icmp_packet->ttl = TTL;
958
959 send_skb_packet(skb, batman_if, dstaddr);
960 ret = NET_RX_SUCCESS;
961
962 } else
963 spin_unlock_bh(&bat_priv->orig_hash_lock);
964
965 return ret;
966}
967
968
969int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if)
970{
971 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
972 struct icmp_packet_rr *icmp_packet;
973 struct ethhdr *ethhdr;
974 struct orig_node *orig_node;
975 struct batman_if *batman_if;
976 int hdr_size = sizeof(struct icmp_packet);
977 int ret;
978 uint8_t dstaddr[ETH_ALEN];
979
980 /**
981 * we truncate all incoming icmp packets if they don't match our size
982 */
983 if (skb->len >= sizeof(struct icmp_packet_rr))
984 hdr_size = sizeof(struct icmp_packet_rr);
985
986 /* drop packet if it has not necessary minimum size */
987 if (unlikely(!pskb_may_pull(skb, hdr_size)))
988 return NET_RX_DROP;
989
990 ethhdr = (struct ethhdr *)skb_mac_header(skb);
991
992 /* packet with unicast indication but broadcast recipient */
993 if (is_broadcast_ether_addr(ethhdr->h_dest))
994 return NET_RX_DROP;
995
996 /* packet with broadcast sender address */
997 if (is_broadcast_ether_addr(ethhdr->h_source))
998 return NET_RX_DROP;
999
1000 /* not for me */
1001 if (!is_my_mac(ethhdr->h_dest))
1002 return NET_RX_DROP;
1003
1004 icmp_packet = (struct icmp_packet_rr *)skb->data;
1005
1006 /* add record route information if not full */
1007 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1008 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1009 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1010 ethhdr->h_dest, ETH_ALEN);
1011 icmp_packet->rr_cur++;
1012 }
1013
1014 /* packet for me */
1015 if (is_my_mac(icmp_packet->dst))
1016 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1017
1018 /* TTL exceeded */
1019 if (icmp_packet->ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +00001020 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001021
1022 ret = NET_RX_DROP;
1023
1024 /* get routing information */
1025 spin_lock_bh(&bat_priv->orig_hash_lock);
1026 orig_node = ((struct orig_node *)
1027 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1028 icmp_packet->dst));
1029
1030 if ((orig_node) && (orig_node->router)) {
1031
1032 /* don't lock while sending the packets ... we therefore
1033 * copy the required data before sending */
1034 batman_if = orig_node->router->if_incoming;
1035 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
1036 spin_unlock_bh(&bat_priv->orig_hash_lock);
1037
1038 /* create a copy of the skb, if needed, to modify it. */
1039 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1040 return NET_RX_DROP;
1041
1042 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
1044 /* decrement ttl */
1045 icmp_packet->ttl--;
1046
1047 /* route it */
1048 send_skb_packet(skb, batman_if, dstaddr);
1049 ret = NET_RX_SUCCESS;
1050
1051 } else
1052 spin_unlock_bh(&bat_priv->orig_hash_lock);
1053
1054 return ret;
1055}
1056
1057/* find a suitable router for this originator, and use
1058 * bonding if possible. */
1059struct neigh_node *find_router(struct bat_priv *bat_priv,
1060 struct orig_node *orig_node,
1061 struct batman_if *recv_if)
1062{
1063 struct orig_node *primary_orig_node;
1064 struct orig_node *router_orig;
1065 struct neigh_node *router, *first_candidate, *best_router;
1066 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1067 int bonding_enabled;
1068
1069 if (!orig_node)
1070 return NULL;
1071
1072 if (!orig_node->router)
1073 return NULL;
1074
1075 /* without bonding, the first node should
1076 * always choose the default router. */
1077
1078 bonding_enabled = atomic_read(&bat_priv->bonding);
1079
1080 if ((!recv_if) && (!bonding_enabled))
1081 return orig_node->router;
1082
1083 router_orig = orig_node->router->orig_node;
1084
1085 /* if we have something in the primary_addr, we can search
1086 * for a potential bonding candidate. */
1087 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1088 return orig_node->router;
1089
1090 /* find the orig_node which has the primary interface. might
1091 * even be the same as our router_orig in many cases */
1092
1093 if (memcmp(router_orig->primary_addr,
1094 router_orig->orig, ETH_ALEN) == 0) {
1095 primary_orig_node = router_orig;
1096 } else {
1097 primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig,
1098 choose_orig,
1099 router_orig->primary_addr);
1100
1101 if (!primary_orig_node)
1102 return orig_node->router;
1103 }
1104
1105 /* with less than 2 candidates, we can't do any
1106 * bonding and prefer the original router. */
1107
1108 if (primary_orig_node->bond.candidates < 2)
1109 return orig_node->router;
1110
1111
1112 /* all nodes between should choose a candidate which
1113 * is is not on the interface where the packet came
1114 * in. */
1115 first_candidate = primary_orig_node->bond.selected;
1116 router = first_candidate;
1117
1118 if (bonding_enabled) {
1119 /* in the bonding case, send the packets in a round
1120 * robin fashion over the remaining interfaces. */
1121 do {
1122 /* recv_if == NULL on the first node. */
1123 if (router->if_incoming != recv_if)
1124 break;
1125
1126 router = router->next_bond_candidate;
1127 } while (router != first_candidate);
1128
1129 primary_orig_node->bond.selected = router->next_bond_candidate;
1130
1131 } else {
1132 /* if bonding is disabled, use the best of the
1133 * remaining candidates which are not using
1134 * this interface. */
1135 best_router = first_candidate;
1136
1137 do {
1138 /* recv_if == NULL on the first node. */
1139 if ((router->if_incoming != recv_if) &&
1140 (router->tq_avg > best_router->tq_avg))
1141 best_router = router;
1142
1143 router = router->next_bond_candidate;
1144 } while (router != first_candidate);
1145
1146 router = best_router;
1147 }
1148
1149 return router;
1150}
1151
1152static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1153{
1154 struct ethhdr *ethhdr;
1155
1156 /* drop packet if it has not necessary minimum size */
1157 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1158 return -1;
1159
1160 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1161
1162 /* packet with unicast indication but broadcast recipient */
1163 if (is_broadcast_ether_addr(ethhdr->h_dest))
1164 return -1;
1165
1166 /* packet with broadcast sender address */
1167 if (is_broadcast_ether_addr(ethhdr->h_source))
1168 return -1;
1169
1170 /* not for me */
1171 if (!is_my_mac(ethhdr->h_dest))
1172 return -1;
1173
1174 return 0;
1175}
1176
1177int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if,
1178 int hdr_size)
1179{
1180 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1181 struct orig_node *orig_node;
1182 struct neigh_node *router;
1183 struct batman_if *batman_if;
1184 uint8_t dstaddr[ETH_ALEN];
1185 struct unicast_packet *unicast_packet;
1186 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
1187 int ret;
1188 struct sk_buff *new_skb;
1189
1190 unicast_packet = (struct unicast_packet *)skb->data;
1191
1192 /* TTL exceeded */
1193 if (unicast_packet->ttl < 2) {
1194 pr_debug("Warning - can't forward unicast packet from %pM to "
1195 "%pM: ttl exceeded\n", ethhdr->h_source,
1196 unicast_packet->dest);
1197 return NET_RX_DROP;
1198 }
1199
1200 /* get routing information */
1201 spin_lock_bh(&bat_priv->orig_hash_lock);
1202 orig_node = ((struct orig_node *)
1203 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1204 unicast_packet->dest));
1205
1206 router = find_router(bat_priv, orig_node, recv_if);
1207
1208 if (!router) {
1209 spin_unlock_bh(&bat_priv->orig_hash_lock);
1210 return NET_RX_DROP;
1211 }
1212
1213 /* don't lock while sending the packets ... we therefore
1214 * copy the required data before sending */
1215
1216 batman_if = router->if_incoming;
1217 memcpy(dstaddr, router->addr, ETH_ALEN);
1218
1219 spin_unlock_bh(&bat_priv->orig_hash_lock);
1220
1221 /* create a copy of the skb, if needed, to modify it. */
1222 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1223 return NET_RX_DROP;
1224
1225 unicast_packet = (struct unicast_packet *)skb->data;
1226
1227 if (unicast_packet->packet_type == BAT_UNICAST &&
1228 atomic_read(&bat_priv->fragmentation) &&
1229 skb->len > batman_if->net_dev->mtu)
1230 return frag_send_skb(skb, bat_priv, batman_if,
1231 dstaddr);
1232
1233 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
Sven Eckelmannae361ce2011-01-25 22:02:31 +00001234 frag_can_reassemble(skb, batman_if->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001235
1236 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1237
1238 if (ret == NET_RX_DROP)
1239 return NET_RX_DROP;
1240
1241 /* packet was buffered for late merge */
1242 if (!new_skb)
1243 return NET_RX_SUCCESS;
1244
1245 skb = new_skb;
1246 unicast_packet = (struct unicast_packet *)skb->data;
1247 }
1248
1249 /* decrement ttl */
1250 unicast_packet->ttl--;
1251
1252 /* route it */
1253 send_skb_packet(skb, batman_if, dstaddr);
1254
1255 return NET_RX_SUCCESS;
1256}
1257
1258int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1259{
1260 struct unicast_packet *unicast_packet;
1261 int hdr_size = sizeof(struct unicast_packet);
1262
1263 if (check_unicast_packet(skb, hdr_size) < 0)
1264 return NET_RX_DROP;
1265
1266 unicast_packet = (struct unicast_packet *)skb->data;
1267
1268 /* packet for me */
1269 if (is_my_mac(unicast_packet->dest)) {
1270 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1271 return NET_RX_SUCCESS;
1272 }
1273
1274 return route_unicast_packet(skb, recv_if, hdr_size);
1275}
1276
1277int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
1278{
1279 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1280 struct unicast_frag_packet *unicast_packet;
1281 int hdr_size = sizeof(struct unicast_frag_packet);
1282 struct sk_buff *new_skb = NULL;
1283 int ret;
1284
1285 if (check_unicast_packet(skb, hdr_size) < 0)
1286 return NET_RX_DROP;
1287
1288 unicast_packet = (struct unicast_frag_packet *)skb->data;
1289
1290 /* packet for me */
1291 if (is_my_mac(unicast_packet->dest)) {
1292
1293 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1294
1295 if (ret == NET_RX_DROP)
1296 return NET_RX_DROP;
1297
1298 /* packet was buffered for late merge */
1299 if (!new_skb)
1300 return NET_RX_SUCCESS;
1301
1302 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1303 sizeof(struct unicast_packet));
1304 return NET_RX_SUCCESS;
1305 }
1306
1307 return route_unicast_packet(skb, recv_if, hdr_size);
1308}
1309
1310
1311int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
1312{
1313 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1314 struct orig_node *orig_node;
1315 struct bcast_packet *bcast_packet;
1316 struct ethhdr *ethhdr;
1317 int hdr_size = sizeof(struct bcast_packet);
1318 int32_t seq_diff;
1319
1320 /* drop packet if it has not necessary minimum size */
1321 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1322 return NET_RX_DROP;
1323
1324 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1325
1326 /* packet with broadcast indication but unicast recipient */
1327 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1328 return NET_RX_DROP;
1329
1330 /* packet with broadcast sender address */
1331 if (is_broadcast_ether_addr(ethhdr->h_source))
1332 return NET_RX_DROP;
1333
1334 /* ignore broadcasts sent by myself */
1335 if (is_my_mac(ethhdr->h_source))
1336 return NET_RX_DROP;
1337
1338 bcast_packet = (struct bcast_packet *)skb->data;
1339
1340 /* ignore broadcasts originated by myself */
1341 if (is_my_mac(bcast_packet->orig))
1342 return NET_RX_DROP;
1343
1344 if (bcast_packet->ttl < 2)
1345 return NET_RX_DROP;
1346
1347 spin_lock_bh(&bat_priv->orig_hash_lock);
1348 orig_node = ((struct orig_node *)
1349 hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
1350 bcast_packet->orig));
1351
1352 if (!orig_node) {
1353 spin_unlock_bh(&bat_priv->orig_hash_lock);
1354 return NET_RX_DROP;
1355 }
1356
1357 /* check whether the packet is a duplicate */
1358 if (get_bit_status(orig_node->bcast_bits,
1359 orig_node->last_bcast_seqno,
1360 ntohl(bcast_packet->seqno))) {
1361 spin_unlock_bh(&bat_priv->orig_hash_lock);
1362 return NET_RX_DROP;
1363 }
1364
1365 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1366
1367 /* check whether the packet is old and the host just restarted. */
1368 if (window_protected(bat_priv, seq_diff,
1369 &orig_node->bcast_seqno_reset)) {
1370 spin_unlock_bh(&bat_priv->orig_hash_lock);
1371 return NET_RX_DROP;
1372 }
1373
1374 /* mark broadcast in flood history, update window position
1375 * if required. */
1376 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1377 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1378
1379 spin_unlock_bh(&bat_priv->orig_hash_lock);
1380 /* rebroadcast packet */
1381 add_bcast_packet_to_list(bat_priv, skb);
1382
1383 /* broadcast for me */
1384 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1385
1386 return NET_RX_SUCCESS;
1387}
1388
1389int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
1390{
1391 struct vis_packet *vis_packet;
1392 struct ethhdr *ethhdr;
1393 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1394 int hdr_size = sizeof(struct vis_packet);
1395
1396 /* keep skb linear */
1397 if (skb_linearize(skb) < 0)
1398 return NET_RX_DROP;
1399
1400 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1401 return NET_RX_DROP;
1402
1403 vis_packet = (struct vis_packet *)skb->data;
1404 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1405
1406 /* not for me */
1407 if (!is_my_mac(ethhdr->h_dest))
1408 return NET_RX_DROP;
1409
1410 /* ignore own packets */
1411 if (is_my_mac(vis_packet->vis_orig))
1412 return NET_RX_DROP;
1413
1414 if (is_my_mac(vis_packet->sender_orig))
1415 return NET_RX_DROP;
1416
1417 switch (vis_packet->vis_type) {
1418 case VIS_TYPE_SERVER_SYNC:
1419 receive_server_sync_packet(bat_priv, vis_packet,
1420 skb_headlen(skb));
1421 break;
1422
1423 case VIS_TYPE_CLIENT_UPDATE:
1424 receive_client_update_packet(bat_priv, vis_packet,
1425 skb_headlen(skb));
1426 break;
1427
1428 default: /* ignore unknown packet */
1429 break;
1430 }
1431
1432 /* We take a copy of the data in the packet, so we should
1433 always free the skbuf. */
1434 return NET_RX_DROP;
1435}