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