]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/routing.c
Merge branch 'for-rmk' of git://git.pengutronix.de/git/imx/linux-2.6
[net-next-2.6.git] / drivers / staging / batman-adv / routing.c
CommitLineData
5beef3c9 1/*
9b6d10b7 2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
5beef3c9
AL
3 *
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
5beef3c9
AL
22#include "main.h"
23#include "routing.h"
5beef3c9 24#include "send.h"
8a2e042c 25#include "hash.h"
5beef3c9
AL
26#include "soft-interface.h"
27#include "hard-interface.h"
c4121432 28#include "icmp_socket.h"
5beef3c9 29#include "translation-table.h"
8a2e042c 30#include "originator.h"
5beef3c9 31#include "types.h"
5beef3c9
AL
32#include "ring_buffer.h"
33#include "vis.h"
34#include "aggregation.h"
5beef3c9 35
42fa1b92 36static DECLARE_WAIT_QUEUE_HEAD(thread_wait);
5beef3c9 37
5beef3c9
AL
38void slide_own_bcast_window(struct batman_if *batman_if)
39{
b6c35976 40 HASHIT(hashit);
5beef3c9 41 struct orig_node *orig_node;
4efe0b06 42 TYPE_OF_WORD *word;
e7017195 43 unsigned long flags;
5beef3c9 44
e7017195 45 spin_lock_irqsave(&orig_hash_lock, flags);
5beef3c9 46
b6c35976
SW
47 while (hash_iterate(orig_hash, &hashit)) {
48 orig_node = hashit.bucket->data;
4efe0b06 49 word = &(orig_node->bcast_own[batman_if->if_num * NUM_WORDS]);
5beef3c9 50
4efe0b06
AL
51 bit_get_packet(word, 1, 0);
52 orig_node->bcast_own_sum[batman_if->if_num] =
53 bit_packet_count(word);
5beef3c9
AL
54 }
55
e7017195 56 spin_unlock_irqrestore(&orig_hash_lock, flags);
5beef3c9
AL
57}
58
4efe0b06
AL
59static void update_HNA(struct orig_node *orig_node,
60 unsigned char *hna_buff, int hna_buff_len)
5beef3c9 61{
4efe0b06
AL
62 if ((hna_buff_len != orig_node->hna_buff_len) ||
63 ((hna_buff_len > 0) &&
64 (orig_node->hna_buff_len > 0) &&
65 (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
5beef3c9 66
4efe0b06
AL
67 if (orig_node->hna_buff_len > 0)
68 hna_global_del_orig(orig_node,
69 "originator changed hna");
5beef3c9 70
4efe0b06 71 if ((hna_buff_len > 0) && (hna_buff != NULL))
5beef3c9 72 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
4efe0b06
AL
73 }
74}
5beef3c9 75
4efe0b06
AL
76static void update_route(struct orig_node *orig_node,
77 struct neigh_node *neigh_node,
78 unsigned char *hna_buff, int hna_buff_len)
79{
84ec0864
ML
80 /* FIXME: each orig_node->batman_if will be attached to a softif */
81 struct bat_priv *bat_priv = netdev_priv(soft_device);
82
4efe0b06
AL
83 /* route deleted */
84 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
5beef3c9 85
84ec0864 86 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
b9b27e4e 87 orig_node->orig);
4efe0b06 88 hna_global_del_orig(orig_node, "originator timed out");
5beef3c9 89
4efe0b06
AL
90 /* route added */
91 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
5beef3c9 92
84ec0864 93 bat_dbg(DBG_ROUTES, bat_priv,
b9b27e4e
AL
94 "Adding route towards: %pM (via %pM)\n",
95 orig_node->orig, neigh_node->addr);
4efe0b06
AL
96 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
97
98 /* route changed */
5beef3c9 99 } else {
84ec0864 100 bat_dbg(DBG_ROUTES, bat_priv,
6d45d8df
SE
101 "Changing route towards: %pM "
102 "(now via %pM - was via %pM)\n",
103 orig_node->orig, neigh_node->addr,
104 orig_node->router->addr);
4efe0b06 105 }
5beef3c9 106
4efe0b06
AL
107 orig_node->router = neigh_node;
108}
5beef3c9 109
5beef3c9 110
8a2e042c 111void update_routes(struct orig_node *orig_node,
4efe0b06
AL
112 struct neigh_node *neigh_node,
113 unsigned char *hna_buff, int hna_buff_len)
114{
5beef3c9 115
4efe0b06
AL
116 if (orig_node == NULL)
117 return;
118
119 if (orig_node->router != neigh_node)
120 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
121 /* may be just HNA changed */
122 else
123 update_HNA(orig_node, hna_buff, hna_buff_len);
5beef3c9
AL
124}
125
6e0e9388 126static int is_bidirectional_neigh(struct orig_node *orig_node,
4efe0b06
AL
127 struct orig_node *orig_neigh_node,
128 struct batman_packet *batman_packet,
129 struct batman_if *if_incoming)
5beef3c9 130{
84ec0864
ML
131 /* FIXME: each orig_node->batman_if will be attached to a softif */
132 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9 133 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
5beef3c9
AL
134 unsigned char total_count;
135
5beef3c9 136 if (orig_node == orig_neigh_node) {
4efe0b06
AL
137 list_for_each_entry(tmp_neigh_node,
138 &orig_node->neigh_list,
139 list) {
5beef3c9 140
4efe0b06
AL
141 if (compare_orig(tmp_neigh_node->addr,
142 orig_neigh_node->orig) &&
143 (tmp_neigh_node->if_incoming == if_incoming))
5beef3c9
AL
144 neigh_node = tmp_neigh_node;
145 }
146
c4bf05d3 147 if (!neigh_node)
4efe0b06
AL
148 neigh_node = create_neighbor(orig_node,
149 orig_neigh_node,
150 orig_neigh_node->orig,
151 if_incoming);
c4bf05d3
SW
152 /* create_neighbor failed, return 0 */
153 if (!neigh_node)
154 return 0;
5beef3c9
AL
155
156 neigh_node->last_valid = jiffies;
157 } else {
158 /* find packet count of corresponding one hop neighbor */
4efe0b06
AL
159 list_for_each_entry(tmp_neigh_node,
160 &orig_neigh_node->neigh_list, list) {
5beef3c9 161
4efe0b06
AL
162 if (compare_orig(tmp_neigh_node->addr,
163 orig_neigh_node->orig) &&
164 (tmp_neigh_node->if_incoming == if_incoming))
5beef3c9
AL
165 neigh_node = tmp_neigh_node;
166 }
167
c4bf05d3 168 if (!neigh_node)
4efe0b06
AL
169 neigh_node = create_neighbor(orig_neigh_node,
170 orig_neigh_node,
171 orig_neigh_node->orig,
172 if_incoming);
c4bf05d3
SW
173 /* create_neighbor failed, return 0 */
174 if (!neigh_node)
175 return 0;
5beef3c9
AL
176 }
177
178 orig_node->last_valid = jiffies;
179
180 /* pay attention to not get a value bigger than 100 % */
4efe0b06
AL
181 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
182 neigh_node->real_packet_count ?
183 neigh_node->real_packet_count :
184 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
5beef3c9
AL
185
186 /* if we have too few packets (too less data) we set tq_own to zero */
187 /* if we receive too few packets it is not considered bidirectional */
4efe0b06
AL
188 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
189 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
5beef3c9
AL
190 orig_neigh_node->tq_own = 0;
191 else
4efe0b06
AL
192 /* neigh_node->real_packet_count is never zero as we
193 * only purge old information when getting new
194 * information */
195 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
196 neigh_node->real_packet_count;
5beef3c9
AL
197
198 /*
4efe0b06
AL
199 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
200 * affect the nearly-symmetric links only a little, but
201 * punishes asymmetric links more. This will give a value
202 * between 0 and TQ_MAX_VALUE
5beef3c9 203 */
4efe0b06
AL
204 orig_neigh_node->tq_asym_penalty =
205 TQ_MAX_VALUE -
206 (TQ_MAX_VALUE *
207 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
208 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
209 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
210 (TQ_LOCAL_WINDOW_SIZE *
211 TQ_LOCAL_WINDOW_SIZE *
212 TQ_LOCAL_WINDOW_SIZE);
213
214 batman_packet->tq = ((batman_packet->tq *
215 orig_neigh_node->tq_own *
216 orig_neigh_node->tq_asym_penalty) /
208e13e4 217 (TQ_MAX_VALUE * TQ_MAX_VALUE));
5beef3c9 218
84ec0864 219 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
220 "bidirectional: "
221 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
222 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
223 "total tq: %3i\n",
b9b27e4e 224 orig_node->orig, orig_neigh_node->orig, total_count,
4efe0b06
AL
225 neigh_node->real_packet_count, orig_neigh_node->tq_own,
226 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
5beef3c9 227
4efe0b06
AL
228 /* if link has the minimum required transmission quality
229 * consider it bidirectional */
5beef3c9
AL
230 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
231 return 1;
232
233 return 0;
234}
235
4efe0b06
AL
236static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
237 struct batman_packet *batman_packet,
238 struct batman_if *if_incoming,
239 unsigned char *hna_buff, int hna_buff_len,
240 char is_duplicate)
5beef3c9 241{
84ec0864
ML
242 /* FIXME: get bat_priv */
243 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9
AL
244 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
245 int tmp_hna_buff_len;
246
84ec0864 247 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
6d45d8df 248 "Searching and updating originator entry of received packet\n");
5beef3c9
AL
249
250 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
4efe0b06
AL
251 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
252 (tmp_neigh_node->if_incoming == if_incoming)) {
5beef3c9
AL
253 neigh_node = tmp_neigh_node;
254 continue;
255 }
256
257 if (is_duplicate)
258 continue;
259
4efe0b06
AL
260 ring_buffer_set(tmp_neigh_node->tq_recv,
261 &tmp_neigh_node->tq_index, 0);
262 tmp_neigh_node->tq_avg =
263 ring_buffer_avg(tmp_neigh_node->tq_recv);
5beef3c9
AL
264 }
265
c4bf05d3
SW
266 if (!neigh_node) {
267 struct orig_node *orig_tmp;
268
269 orig_tmp = get_orig_node(ethhdr->h_source);
270 if (!orig_tmp)
271 return;
272
4efe0b06 273 neigh_node = create_neighbor(orig_node,
c4bf05d3 274 orig_tmp,
4efe0b06 275 ethhdr->h_source, if_incoming);
c4bf05d3
SW
276 if (!neigh_node)
277 return;
278 } else
84ec0864 279 bat_dbg(DBG_BATMAN, bat_priv,
5ea84fa3 280 "Updating existing last-hop neighbor of originator\n");
5beef3c9
AL
281
282 orig_node->flags = batman_packet->flags;
283 neigh_node->last_valid = jiffies;
284
4efe0b06
AL
285 ring_buffer_set(neigh_node->tq_recv,
286 &neigh_node->tq_index,
287 batman_packet->tq);
5beef3c9
AL
288 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
289
290 if (!is_duplicate) {
291 orig_node->last_ttl = batman_packet->ttl;
292 neigh_node->last_ttl = batman_packet->ttl;
293 }
294
4efe0b06
AL
295 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
296 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
5beef3c9 297
4efe0b06
AL
298 /* if this neighbor already is our next hop there is nothing
299 * to change */
5beef3c9
AL
300 if (orig_node->router == neigh_node)
301 goto update_hna;
302
303 /* if this neighbor does not offer a better TQ we won't consider it */
304 if ((orig_node->router) &&
305 (orig_node->router->tq_avg > neigh_node->tq_avg))
306 goto update_hna;
307
4efe0b06
AL
308 /* if the TQ is the same and the link not more symetric we
309 * won't consider it either */
5beef3c9
AL
310 if ((orig_node->router) &&
311 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
4efe0b06
AL
312 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
313 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
5beef3c9
AL
314 goto update_hna;
315
316 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
317 return;
318
319update_hna:
320 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
5beef3c9
AL
321}
322
f94cee24
SW
323/* checks whether the host restarted and is in the protection time.
324 * returns:
325 * 0 if the packet is to be accepted
326 * 1 if the packet is to be ignored.
327 */
cf2d72ec 328static int window_protected(int32_t seq_num_diff,
f94cee24
SW
329 unsigned long *last_reset)
330{
84ec0864
ML
331 /* FIXME: each orig_node->batman_if will be attached to a softif */
332 struct bat_priv *bat_priv = netdev_priv(soft_device);
333
f94cee24
SW
334 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
335 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
336 if (time_after(jiffies, *last_reset +
337 msecs_to_jiffies(RESET_PROTECTION_MS))) {
338
339 *last_reset = jiffies;
84ec0864 340 bat_dbg(DBG_BATMAN, bat_priv,
f94cee24
SW
341 "old packet received, start protection\n");
342
343 return 0;
344 } else
345 return 1;
346 }
347 return 0;
348}
349
350/* processes a batman packet for all interfaces, adjusts the sequence number and
351 * finds out whether it is a duplicate.
352 * returns:
353 * 1 the packet is a duplicate
354 * 0 the packet has not yet been received
355 * -1 the packet is old and has been received while the seqno window
356 * was protected. Caller should drop it.
357 */
4efe0b06
AL
358static char count_real_packets(struct ethhdr *ethhdr,
359 struct batman_packet *batman_packet,
360 struct batman_if *if_incoming)
5beef3c9 361{
84ec0864
ML
362 /* FIXME: each orig_node->batman_if will be attached to a softif */
363 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9
AL
364 struct orig_node *orig_node;
365 struct neigh_node *tmp_neigh_node;
366 char is_duplicate = 0;
cf2d72ec 367 int32_t seq_diff;
f94cee24
SW
368 int need_update = 0;
369 int set_mark;
5beef3c9
AL
370
371 orig_node = get_orig_node(batman_packet->orig);
372 if (orig_node == NULL)
373 return 0;
374
f94cee24
SW
375 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
376
377 /* signalize caller that the packet is to be dropped. */
378 if (window_protected(seq_diff, &orig_node->batman_seqno_reset))
379 return -1;
380
5beef3c9
AL
381 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
382
f94cee24 383 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
4efe0b06
AL
384 orig_node->last_real_seqno,
385 batman_packet->seqno);
f94cee24 386
4efe0b06
AL
387 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
388 (tmp_neigh_node->if_incoming == if_incoming))
f94cee24 389 set_mark = 1;
5beef3c9 390 else
f94cee24
SW
391 set_mark = 0;
392
393 /* if the window moved, set the update flag. */
394 need_update |= bit_get_packet(tmp_neigh_node->real_bits,
395 seq_diff, set_mark);
5beef3c9 396
4efe0b06
AL
397 tmp_neigh_node->real_packet_count =
398 bit_packet_count(tmp_neigh_node->real_bits);
5beef3c9
AL
399 }
400
f94cee24 401 if (need_update) {
84ec0864
ML
402 bat_dbg(DBG_BATMAN, bat_priv,
403 "updating last_seqno: old %d, new %d\n",
bad2239e 404 orig_node->last_real_seqno, batman_packet->seqno);
5beef3c9
AL
405 orig_node->last_real_seqno = batman_packet->seqno;
406 }
407
408 return is_duplicate;
409}
410
e35fd5ec
SW
411/* copy primary address for bonding */
412static void mark_bonding_address(struct bat_priv *bat_priv,
413 struct orig_node *orig_node,
414 struct orig_node *orig_neigh_node,
415 struct batman_packet *batman_packet)
416
417{
e35fd5ec
SW
418 if (batman_packet->flags & PRIMARIES_FIRST_HOP)
419 memcpy(orig_neigh_node->primary_addr,
420 orig_node->orig, ETH_ALEN);
421
422 return;
423}
424
425/* mark possible bond.candidates in the neighbor list */
426void update_bonding_candidates(struct bat_priv *bat_priv,
427 struct orig_node *orig_node)
428{
429 int candidates;
430 int interference_candidate;
431 int best_tq;
432 struct neigh_node *tmp_neigh_node, *tmp_neigh_node2;
433 struct neigh_node *first_candidate, *last_candidate;
434
e35fd5ec
SW
435 /* update the candidates for this originator */
436 if (!orig_node->router) {
437 orig_node->bond.candidates = 0;
438 return;
439 }
440
441 best_tq = orig_node->router->tq_avg;
442
443 /* update bond.candidates */
444
445 candidates = 0;
446
447 /* mark other nodes which also received "PRIMARIES FIRST HOP" packets
448 * as "bonding partner" */
449
450 /* first, zero the list */
451 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
452 tmp_neigh_node->next_bond_candidate = NULL;
453 }
454
455 first_candidate = NULL;
456 last_candidate = NULL;
457 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
458
459 /* only consider if it has the same primary address ... */
460 if (memcmp(orig_node->orig,
461 tmp_neigh_node->orig_node->primary_addr,
462 ETH_ALEN) != 0)
463 continue;
464
465 /* ... and is good enough to be considered */
466 if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
467 continue;
468
469 /* check if we have another candidate with the same
470 * mac address or interface. If we do, we won't
471 * select this candidate because of possible interference. */
472
473 interference_candidate = 0;
474 list_for_each_entry(tmp_neigh_node2,
475 &orig_node->neigh_list, list) {
476
477 if (tmp_neigh_node2 == tmp_neigh_node)
478 continue;
479
480 /* we only care if the other candidate is even
481 * considered as candidate. */
482 if (tmp_neigh_node2->next_bond_candidate == NULL)
483 continue;
484
485
486 if ((tmp_neigh_node->if_incoming ==
487 tmp_neigh_node2->if_incoming)
488 || (memcmp(tmp_neigh_node->addr,
489 tmp_neigh_node2->addr, ETH_ALEN) == 0)) {
490
491 interference_candidate = 1;
492 break;
493 }
494 }
495 /* don't care further if it is an interference candidate */
496 if (interference_candidate)
497 continue;
498
499 if (first_candidate == NULL) {
500 first_candidate = tmp_neigh_node;
501 tmp_neigh_node->next_bond_candidate = first_candidate;
502 } else
503 tmp_neigh_node->next_bond_candidate = last_candidate;
504
505 last_candidate = tmp_neigh_node;
506
507 candidates++;
508 }
509
510 if (candidates > 0) {
511 first_candidate->next_bond_candidate = last_candidate;
512 orig_node->bond.selected = first_candidate;
513 }
514
515 orig_node->bond.candidates = candidates;
516}
517
4efe0b06 518void receive_bat_packet(struct ethhdr *ethhdr,
e7017195
SW
519 struct batman_packet *batman_packet,
520 unsigned char *hna_buff, int hna_buff_len,
521 struct batman_if *if_incoming)
5beef3c9 522{
e35fd5ec
SW
523 /* FIXME: each orig_node->batman_if will be attached to a softif */
524 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9
AL
525 struct batman_if *batman_if;
526 struct orig_node *orig_neigh_node, *orig_node;
5beef3c9 527 char has_directlink_flag;
4efe0b06
AL
528 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
529 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
530 char is_duplicate;
cf2d72ec 531 uint32_t if_incoming_seqno;
5beef3c9 532
4efe0b06
AL
533 /* Silently drop when the batman packet is actually not a
534 * correct packet.
5beef3c9
AL
535 *
536 * This might happen if a packet is padded (e.g. Ethernet has a
537 * minimum frame length of 64 byte) and the aggregation interprets
538 * it as an additional length.
539 *
4efe0b06
AL
540 * TODO: A more sane solution would be to have a bit in the
541 * batman_packet to detect whether the packet is the last
542 * packet in an aggregation. Here we expect that the padding
543 * is always zero (or not 0x01)
5beef3c9
AL
544 */
545 if (batman_packet->packet_type != BAT_PACKET)
546 return;
547
548 /* could be changed by schedule_own_packet() */
549 if_incoming_seqno = atomic_read(&if_incoming->seqno);
550
5beef3c9
AL
551 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
552
4efe0b06
AL
553 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
554 batman_packet->orig) ? 1 : 0);
5beef3c9 555
84ec0864
ML
556 bat_dbg(DBG_BATMAN, bat_priv,
557 "Received BATMAN packet via NB: %pM, IF: %s [%s] "
6d45d8df
SE
558 "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
559 "TTL %d, V %d, IDF %d)\n",
b9b27e4e
AL
560 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
561 batman_packet->orig, batman_packet->prev_sender,
562 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
563 batman_packet->version, has_directlink_flag);
5beef3c9
AL
564
565 list_for_each_entry_rcu(batman_if, &if_list, list) {
208e13e4 566 if (batman_if->if_status != IF_ACTIVE)
5beef3c9
AL
567 continue;
568
4efe0b06
AL
569 if (compare_orig(ethhdr->h_source,
570 batman_if->net_dev->dev_addr))
5beef3c9
AL
571 is_my_addr = 1;
572
4efe0b06
AL
573 if (compare_orig(batman_packet->orig,
574 batman_if->net_dev->dev_addr))
5beef3c9
AL
575 is_my_orig = 1;
576
4efe0b06
AL
577 if (compare_orig(batman_packet->prev_sender,
578 batman_if->net_dev->dev_addr))
5beef3c9
AL
579 is_my_oldorig = 1;
580
6e0e9388 581 if (compare_orig(ethhdr->h_source, broadcast_addr))
5beef3c9
AL
582 is_broadcast = 1;
583 }
584
585 if (batman_packet->version != COMPAT_VERSION) {
84ec0864 586 bat_dbg(DBG_BATMAN, bat_priv,
4efe0b06
AL
587 "Drop packet: incompatible batman version (%i)\n",
588 batman_packet->version);
5beef3c9
AL
589 return;
590 }
591
592 if (is_my_addr) {
84ec0864 593 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
594 "Drop packet: received my own broadcast (sender: %pM"
595 ")\n",
b9b27e4e 596 ethhdr->h_source);
5beef3c9
AL
597 return;
598 }
599
600 if (is_broadcast) {
84ec0864 601 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
6d45d8df
SE
602 "ignoring all packets with broadcast source addr (sender: %pM"
603 ")\n", ethhdr->h_source);
5beef3c9
AL
604 return;
605 }
606
607 if (is_my_orig) {
4efe0b06
AL
608 TYPE_OF_WORD *word;
609 int offset;
610
5beef3c9
AL
611 orig_neigh_node = get_orig_node(ethhdr->h_source);
612
c4bf05d3
SW
613 if (!orig_neigh_node)
614 return;
615
5ea84fa3 616 /* neighbor has to indicate direct link and it has to
4efe0b06
AL
617 * come via the corresponding interface */
618 /* if received seqno equals last send seqno save new
619 * seqno for bidirectional check */
620 if (has_directlink_flag &&
621 compare_orig(if_incoming->net_dev->dev_addr,
622 batman_packet->orig) &&
623 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
624 offset = if_incoming->if_num * NUM_WORDS;
625 word = &(orig_neigh_node->bcast_own[offset]);
626 bit_mark(word, 0);
627 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
628 bit_packet_count(word);
5beef3c9
AL
629 }
630
84ec0864 631 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
6d45d8df 632 "originator packet from myself (via neighbor)\n");
5beef3c9
AL
633 return;
634 }
635
5beef3c9 636 if (is_my_oldorig) {
84ec0864 637 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
638 "Drop packet: ignoring all rebroadcast echos (sender: "
639 "%pM)\n", ethhdr->h_source);
5beef3c9
AL
640 return;
641 }
642
5beef3c9
AL
643 orig_node = get_orig_node(batman_packet->orig);
644 if (orig_node == NULL)
645 return;
646
f94cee24
SW
647 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
648
649 if (is_duplicate == -1) {
84ec0864 650 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
651 "Drop packet: packet within seqno protection time "
652 "(sender: %pM)\n", ethhdr->h_source);
f94cee24
SW
653 return;
654 }
655
656 if (batman_packet->tq == 0) {
84ec0864 657 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df 658 "Drop packet: originator packet with tq equal 0\n");
f94cee24
SW
659 return;
660 }
661
5beef3c9 662 /* avoid temporary routing loops */
4efe0b06
AL
663 if ((orig_node->router) &&
664 (orig_node->router->orig_node->router) &&
665 (compare_orig(orig_node->router->addr,
666 batman_packet->prev_sender)) &&
5beef3c9 667 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
4efe0b06
AL
668 (compare_orig(orig_node->router->addr,
669 orig_node->router->orig_node->router->addr))) {
84ec0864 670 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
671 "Drop packet: ignoring all rebroadcast packets that "
672 "may make me loop (sender: %pM)\n", ethhdr->h_source);
5beef3c9
AL
673 return;
674 }
675
4efe0b06
AL
676 /* if sender is a direct neighbor the sender mac equals
677 * originator mac */
678 orig_neigh_node = (is_single_hop_neigh ?
679 orig_node : get_orig_node(ethhdr->h_source));
5beef3c9
AL
680 if (orig_neigh_node == NULL)
681 return;
682
4efe0b06
AL
683 /* drop packet if sender is not a direct neighbor and if we
684 * don't route towards it */
685 if (!is_single_hop_neigh &&
686 (orig_neigh_node->router == NULL)) {
84ec0864
ML
687 bat_dbg(DBG_BATMAN, bat_priv,
688 "Drop packet: OGM via unknown neighbor!\n");
5beef3c9
AL
689 return;
690 }
691
6e0e9388 692 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
4efe0b06 693 batman_packet, if_incoming);
5beef3c9 694
4efe0b06
AL
695 /* update ranking if it is not a duplicate or has the same
696 * seqno and similar ttl as the non-duplicate */
697 if (is_bidirectional &&
698 (!is_duplicate ||
699 ((orig_node->last_real_seqno == batman_packet->seqno) &&
700 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
701 update_orig(orig_node, ethhdr, batman_packet,
702 if_incoming, hna_buff, hna_buff_len, is_duplicate);
5beef3c9 703
e35fd5ec
SW
704 mark_bonding_address(bat_priv, orig_node,
705 orig_neigh_node, batman_packet);
706 update_bonding_candidates(bat_priv, orig_node);
707
5ea84fa3 708 /* is single hop (direct) neighbor */
5beef3c9
AL
709 if (is_single_hop_neigh) {
710
711 /* mark direct link on incoming interface */
4efe0b06
AL
712 schedule_forward_packet(orig_node, ethhdr, batman_packet,
713 1, hna_buff_len, if_incoming);
5beef3c9 714
84ec0864 715 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
6d45d8df 716 "rebroadcast neighbor packet with direct link flag\n");
5beef3c9
AL
717 return;
718 }
719
720 /* multihop originator */
721 if (!is_bidirectional) {
84ec0864 722 bat_dbg(DBG_BATMAN, bat_priv,
4efe0b06 723 "Drop packet: not received via bidirectional link\n");
5beef3c9
AL
724 return;
725 }
726
727 if (is_duplicate) {
84ec0864
ML
728 bat_dbg(DBG_BATMAN, bat_priv,
729 "Drop packet: duplicate packet received\n");
5beef3c9
AL
730 return;
731 }
732
84ec0864 733 bat_dbg(DBG_BATMAN, bat_priv,
4efe0b06
AL
734 "Forwarding packet: rebroadcast originator packet\n");
735 schedule_forward_packet(orig_node, ethhdr, batman_packet,
736 0, hna_buff_len, if_incoming);
5beef3c9
AL
737}
738
e7017195
SW
739int recv_bat_packet(struct sk_buff *skb,
740 struct batman_if *batman_if)
5beef3c9 741{
e7017195
SW
742 struct ethhdr *ethhdr;
743 unsigned long flags;
f347b873 744 struct sk_buff *skb_old;
5beef3c9 745
e7017195
SW
746 /* drop packet if it has not necessary minimum size */
747 if (skb_headlen(skb) < sizeof(struct batman_packet))
748 return NET_RX_DROP;
5beef3c9 749
e7017195 750 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 751
4efe0b06
AL
752 /* packet with broadcast indication but unicast recipient */
753 if (!is_bcast(ethhdr->h_dest))
e7017195 754 return NET_RX_DROP;
4efe0b06
AL
755
756 /* packet with broadcast sender address */
757 if (is_bcast(ethhdr->h_source))
e7017195
SW
758 return NET_RX_DROP;
759
e7017195
SW
760 /* TODO: we use headlen instead of "length", because
761 * only this data is paged in. */
f347b873
SE
762
763 /* create a copy of the skb, if needed, to modify it. */
764 if (!skb_clone_writable(skb, skb_headlen(skb))) {
765 skb_old = skb;
766 skb = skb_copy(skb, GFP_ATOMIC);
767 if (!skb)
768 return NET_RX_DROP;
202cfe10 769 ethhdr = (struct ethhdr *)skb_mac_header(skb);
f347b873
SE
770 kfree_skb(skb_old);
771 }
772
773 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06 774 receive_aggr_bat_packet(ethhdr,
e7017195
SW
775 skb->data,
776 skb_headlen(skb),
4efe0b06 777 batman_if);
e7017195
SW
778 spin_unlock_irqrestore(&orig_hash_lock, flags);
779
780 kfree_skb(skb);
781 return NET_RX_SUCCESS;
4efe0b06
AL
782}
783
e4cb3720 784static int recv_my_icmp_packet(struct sk_buff *skb, size_t icmp_len)
5beef3c9 785{
b7a23bce
ML
786 /* FIXME: each batman_if will be attached to a softif */
787 struct bat_priv *bat_priv = netdev_priv(soft_device);
5beef3c9 788 struct orig_node *orig_node;
e4cb3720 789 struct icmp_packet_rr *icmp_packet;
e7017195
SW
790 struct ethhdr *ethhdr;
791 struct sk_buff *skb_old;
792 struct batman_if *batman_if;
793 int ret;
794 unsigned long flags;
795 uint8_t dstaddr[ETH_ALEN];
796
e4cb3720 797 icmp_packet = (struct icmp_packet_rr *)skb->data;
202cfe10 798 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 799
4efe0b06
AL
800 /* add data to device queue */
801 if (icmp_packet->msg_type != ECHO_REQUEST) {
e4cb3720 802 bat_socket_receive_packet(icmp_packet, icmp_len);
e7017195 803 return NET_RX_DROP;
5beef3c9
AL
804 }
805
b7a23bce
ML
806 if (!bat_priv->primary_if)
807 return NET_RX_DROP;
808
4efe0b06
AL
809 /* answer echo request (ping) */
810 /* get routing information */
e7017195 811 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
812 orig_node = ((struct orig_node *)hash_find(orig_hash,
813 icmp_packet->orig));
e7017195 814 ret = NET_RX_DROP;
4efe0b06
AL
815
816 if ((orig_node != NULL) &&
4efe0b06 817 (orig_node->router != NULL)) {
e7017195
SW
818
819 /* don't lock while sending the packets ... we therefore
820 * copy the required data before sending */
35bd69d4 821 batman_if = orig_node->router->if_incoming;
e7017195
SW
822 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
823 spin_unlock_irqrestore(&orig_hash_lock, flags);
824
825 /* create a copy of the skb, if needed, to modify it. */
826 skb_old = NULL;
e4cb3720 827 if (!skb_clone_writable(skb, icmp_len)) {
e7017195
SW
828 skb_old = skb;
829 skb = skb_copy(skb, GFP_ATOMIC);
830 if (!skb)
831 return NET_RX_DROP;
e4cb3720 832 icmp_packet = (struct icmp_packet_rr *)skb->data;
202cfe10 833 ethhdr = (struct ethhdr *)skb_mac_header(skb);
e7017195
SW
834 kfree_skb(skb_old);
835 }
836
4efe0b06 837 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
b7a23bce
ML
838 memcpy(icmp_packet->orig,
839 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
4efe0b06
AL
840 icmp_packet->msg_type = ECHO_REPLY;
841 icmp_packet->ttl = TTL;
842
e7017195
SW
843 send_skb_packet(skb, batman_if, dstaddr);
844 ret = NET_RX_SUCCESS;
5beef3c9 845
e7017195
SW
846 } else
847 spin_unlock_irqrestore(&orig_hash_lock, flags);
848
849 return ret;
4efe0b06 850}
5beef3c9 851
e4cb3720 852static int recv_icmp_ttl_exceeded(struct sk_buff *skb, size_t icmp_len)
4efe0b06 853{
b7a23bce
ML
854 /* FIXME: each batman_if will be attached to a softif */
855 struct bat_priv *bat_priv = netdev_priv(soft_device);
4efe0b06 856 struct orig_node *orig_node;
e7017195
SW
857 struct icmp_packet *icmp_packet;
858 struct ethhdr *ethhdr;
859 struct sk_buff *skb_old;
860 struct batman_if *batman_if;
861 int ret;
862 unsigned long flags;
863 uint8_t dstaddr[ETH_ALEN];
864
149de2e5
ML
865 icmp_packet = (struct icmp_packet *)skb->data;
866 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 867
4efe0b06 868 /* send TTL exceeded if packet is an echo request (traceroute) */
149de2e5 869 if (icmp_packet->msg_type != ECHO_REQUEST) {
c1641862
SE
870 pr_warning("Warning - can't forward icmp packet from %pM to "
871 "%pM: ttl exceeded\n", icmp_packet->orig,
872 icmp_packet->dst);
e7017195 873 return NET_RX_DROP;
149de2e5 874 }
5beef3c9 875
b7a23bce
ML
876 if (!bat_priv->primary_if)
877 return NET_RX_DROP;
878
4efe0b06 879 /* get routing information */
e7017195 880 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
881 orig_node = ((struct orig_node *)
882 hash_find(orig_hash, icmp_packet->orig));
e7017195 883 ret = NET_RX_DROP;
4efe0b06
AL
884
885 if ((orig_node != NULL) &&
4efe0b06 886 (orig_node->router != NULL)) {
e7017195
SW
887
888 /* don't lock while sending the packets ... we therefore
889 * copy the required data before sending */
35bd69d4 890 batman_if = orig_node->router->if_incoming;
e7017195
SW
891 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
892 spin_unlock_irqrestore(&orig_hash_lock, flags);
893
894 /* create a copy of the skb, if needed, to modify it. */
e4cb3720 895 if (!skb_clone_writable(skb, icmp_len)) {
e7017195
SW
896 skb_old = skb;
897 skb = skb_copy(skb, GFP_ATOMIC);
898 if (!skb)
899 return NET_RX_DROP;
900 icmp_packet = (struct icmp_packet *) skb->data;
202cfe10 901 ethhdr = (struct ethhdr *)skb_mac_header(skb);
e7017195
SW
902 kfree_skb(skb_old);
903 }
904
4efe0b06 905 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
b7a23bce
ML
906 memcpy(icmp_packet->orig,
907 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
4efe0b06
AL
908 icmp_packet->msg_type = TTL_EXCEEDED;
909 icmp_packet->ttl = TTL;
910
e7017195
SW
911 send_skb_packet(skb, batman_if, dstaddr);
912 ret = NET_RX_SUCCESS;
5beef3c9 913
e7017195
SW
914 } else
915 spin_unlock_irqrestore(&orig_hash_lock, flags);
5beef3c9 916
e7017195 917 return ret;
4efe0b06 918}
5beef3c9 919
5beef3c9 920
e7017195 921int recv_icmp_packet(struct sk_buff *skb)
4efe0b06 922{
e4cb3720 923 struct icmp_packet_rr *icmp_packet;
e7017195 924 struct ethhdr *ethhdr;
4efe0b06 925 struct orig_node *orig_node;
e7017195
SW
926 struct sk_buff *skb_old;
927 struct batman_if *batman_if;
928 int hdr_size = sizeof(struct icmp_packet);
929 int ret;
930 unsigned long flags;
931 uint8_t dstaddr[ETH_ALEN];
932
e4cb3720
DS
933 /**
934 * we truncate all incoming icmp packets if they don't match our size
935 */
936 if (skb_headlen(skb) >= sizeof(struct icmp_packet_rr))
937 hdr_size = sizeof(struct icmp_packet_rr);
938
e7017195
SW
939 /* drop packet if it has not necessary minimum size */
940 if (skb_headlen(skb) < hdr_size)
941 return NET_RX_DROP;
942
943 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 944
4efe0b06
AL
945 /* packet with unicast indication but broadcast recipient */
946 if (is_bcast(ethhdr->h_dest))
e7017195 947 return NET_RX_DROP;
5beef3c9 948
4efe0b06
AL
949 /* packet with broadcast sender address */
950 if (is_bcast(ethhdr->h_source))
e7017195 951 return NET_RX_DROP;
5beef3c9 952
4efe0b06
AL
953 /* not for me */
954 if (!is_my_mac(ethhdr->h_dest))
e7017195 955 return NET_RX_DROP;
5beef3c9 956
e4cb3720
DS
957 icmp_packet = (struct icmp_packet_rr *)skb->data;
958
959 /* add record route information if not full */
960 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
961 (icmp_packet->rr_cur < BAT_RR_LEN)) {
962 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
963 ethhdr->h_dest, ETH_ALEN);
964 icmp_packet->rr_cur++;
965 }
5beef3c9 966
4efe0b06
AL
967 /* packet for me */
968 if (is_my_mac(icmp_packet->dst))
e4cb3720 969 return recv_my_icmp_packet(skb, hdr_size);
5beef3c9 970
4efe0b06 971 /* TTL exceeded */
e7017195 972 if (icmp_packet->ttl < 2)
e4cb3720 973 return recv_icmp_ttl_exceeded(skb, hdr_size);
5beef3c9 974
e7017195 975 ret = NET_RX_DROP;
5beef3c9 976
4efe0b06 977 /* get routing information */
e7017195 978 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
979 orig_node = ((struct orig_node *)
980 hash_find(orig_hash, icmp_packet->dst));
5beef3c9 981
4efe0b06 982 if ((orig_node != NULL) &&
4efe0b06 983 (orig_node->router != NULL)) {
5beef3c9 984
e7017195
SW
985 /* don't lock while sending the packets ... we therefore
986 * copy the required data before sending */
35bd69d4 987 batman_if = orig_node->router->if_incoming;
e7017195
SW
988 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
989 spin_unlock_irqrestore(&orig_hash_lock, flags);
990
991 /* create a copy of the skb, if needed, to modify it. */
e4cb3720 992 if (!skb_clone_writable(skb, hdr_size)) {
e7017195
SW
993 skb_old = skb;
994 skb = skb_copy(skb, GFP_ATOMIC);
995 if (!skb)
996 return NET_RX_DROP;
e4cb3720 997 icmp_packet = (struct icmp_packet_rr *)skb->data;
202cfe10 998 ethhdr = (struct ethhdr *)skb_mac_header(skb);
e7017195
SW
999 kfree_skb(skb_old);
1000 }
1001
4efe0b06
AL
1002 /* decrement ttl */
1003 icmp_packet->ttl--;
5beef3c9 1004
4efe0b06 1005 /* route it */
e7017195
SW
1006 send_skb_packet(skb, batman_if, dstaddr);
1007 ret = NET_RX_SUCCESS;
1008
1009 } else
1010 spin_unlock_irqrestore(&orig_hash_lock, flags);
1011
1012 return ret;
4efe0b06 1013}
5beef3c9 1014
e35fd5ec
SW
1015/* find a suitable router for this originator, and use
1016 * bonding if possible. */
11f79dec
SW
1017struct neigh_node *find_router(struct orig_node *orig_node,
1018 struct batman_if *recv_if)
e35fd5ec
SW
1019{
1020 /* FIXME: each orig_node->batman_if will be attached to a softif */
1021 struct bat_priv *bat_priv = netdev_priv(soft_device);
1022 struct orig_node *primary_orig_node;
1023 struct orig_node *router_orig;
11f79dec 1024 struct neigh_node *router, *first_candidate, *best_router;
e35fd5ec 1025 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
11f79dec 1026 int bonding_enabled;
e35fd5ec
SW
1027
1028 if (!orig_node)
1029 return NULL;
1030
1031 if (!orig_node->router)
1032 return NULL;
1033
11f79dec
SW
1034 /* without bonding, the first node should
1035 * always choose the default router. */
1036
1037 bonding_enabled = atomic_read(&bat_priv->bonding_enabled);
1038 if (!bonding_enabled && (recv_if == NULL))
1039 return orig_node->router;
e35fd5ec
SW
1040
1041 router_orig = orig_node->router->orig_node;
1042
1043 /* if we have something in the primary_addr, we can search
1044 * for a potential bonding candidate. */
1045 if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0)
1046 return orig_node->router;
1047
1048 /* find the orig_node which has the primary interface. might
1049 * even be the same as our router_orig in many cases */
1050
1051 if (memcmp(router_orig->primary_addr,
1052 router_orig->orig, ETH_ALEN) == 0) {
1053 primary_orig_node = router_orig;
1054 } else {
1055 primary_orig_node = hash_find(orig_hash,
1056 router_orig->primary_addr);
1057 if (!primary_orig_node)
1058 return orig_node->router;
1059 }
1060
1061 /* with less than 2 candidates, we can't do any
1062 * bonding and prefer the original router. */
1063
1064 if (primary_orig_node->bond.candidates < 2)
1065 return orig_node->router;
1066
e35fd5ec 1067
11f79dec
SW
1068 /* all nodes between should choose a candidate which
1069 * is is not on the interface where the packet came
1070 * in. */
1071 first_candidate = primary_orig_node->bond.selected;
1072 router = first_candidate;
1073
1074 if (bonding_enabled) {
1075 /* in the bonding case, send the packets in a round
1076 * robin fashion over the remaining interfaces. */
1077 do {
1078 /* recv_if == NULL on the first node. */
1079 if (router->if_incoming != recv_if)
1080 break;
1081
1082 router = router->next_bond_candidate;
1083 } while (router != first_candidate);
1084
1085 primary_orig_node->bond.selected = router->next_bond_candidate;
e35fd5ec 1086
11f79dec
SW
1087 } else {
1088 /* if bonding is disabled, use the best of the
1089 * remaining candidates which are not using
1090 * this interface. */
1091 best_router = first_candidate;
1092
1093 do {
1094 /* recv_if == NULL on the first node. */
1095 if ((router->if_incoming != recv_if) &&
1096 (router->tq_avg > best_router->tq_avg))
1097 best_router = router;
1098
1099 router = router->next_bond_candidate;
1100 } while (router != first_candidate);
1101
1102 router = best_router;
1103 }
e35fd5ec
SW
1104
1105 return router;
1106}
1107
11f79dec 1108int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
4efe0b06
AL
1109{
1110 struct unicast_packet *unicast_packet;
4efe0b06 1111 struct orig_node *orig_node;
e35fd5ec 1112 struct neigh_node *router;
e7017195
SW
1113 struct ethhdr *ethhdr;
1114 struct batman_if *batman_if;
1115 struct sk_buff *skb_old;
1116 uint8_t dstaddr[ETH_ALEN];
1117 int hdr_size = sizeof(struct unicast_packet);
e7017195
SW
1118 unsigned long flags;
1119
1120 /* drop packet if it has not necessary minimum size */
1121 if (skb_headlen(skb) < hdr_size)
1122 return NET_RX_DROP;
1123
1124 ethhdr = (struct ethhdr *) skb_mac_header(skb);
5beef3c9 1125
4efe0b06
AL
1126 /* packet with unicast indication but broadcast recipient */
1127 if (is_bcast(ethhdr->h_dest))
e7017195 1128 return NET_RX_DROP;
5beef3c9 1129
4efe0b06
AL
1130 /* packet with broadcast sender address */
1131 if (is_bcast(ethhdr->h_source))
e7017195 1132 return NET_RX_DROP;
5beef3c9 1133
4efe0b06
AL
1134 /* not for me */
1135 if (!is_my_mac(ethhdr->h_dest))
e7017195 1136 return NET_RX_DROP;
5beef3c9 1137
e7017195 1138 unicast_packet = (struct unicast_packet *) skb->data;
5beef3c9 1139
4efe0b06
AL
1140 /* packet for me */
1141 if (is_my_mac(unicast_packet->dest)) {
e7017195
SW
1142 interface_rx(skb, hdr_size);
1143 return NET_RX_SUCCESS;
4efe0b06 1144 }
5beef3c9 1145
4efe0b06
AL
1146 /* TTL exceeded */
1147 if (unicast_packet->ttl < 2) {
c1641862
SE
1148 pr_warning("Warning - can't forward unicast packet from %pM to "
1149 "%pM: ttl exceeded\n", ethhdr->h_source,
1150 unicast_packet->dest);
e7017195 1151 return NET_RX_DROP;
4efe0b06 1152 }
5beef3c9 1153
4efe0b06 1154 /* get routing information */
e7017195 1155 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
1156 orig_node = ((struct orig_node *)
1157 hash_find(orig_hash, unicast_packet->dest));
1158
11f79dec 1159 router = find_router(orig_node, recv_if);
e7017195 1160
e35fd5ec 1161 if (!router) {
e7017195 1162 spin_unlock_irqrestore(&orig_hash_lock, flags);
e35fd5ec
SW
1163 return NET_RX_DROP;
1164 }
e7017195 1165
e35fd5ec
SW
1166 /* don't lock while sending the packets ... we therefore
1167 * copy the required data before sending */
4efe0b06 1168
e35fd5ec
SW
1169 batman_if = router->if_incoming;
1170 memcpy(dstaddr, router->addr, ETH_ALEN);
e7017195 1171
e35fd5ec 1172 spin_unlock_irqrestore(&orig_hash_lock, flags);
e7017195 1173
e35fd5ec
SW
1174 /* create a copy of the skb, if needed, to modify it. */
1175 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
1176 skb_old = skb;
1177 skb = skb_copy(skb, GFP_ATOMIC);
1178 if (!skb)
1179 return NET_RX_DROP;
1180 unicast_packet = (struct unicast_packet *) skb->data;
1181 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1182 kfree_skb(skb_old);
1183 }
1184
1185 /* decrement ttl */
1186 unicast_packet->ttl--;
1187
1188 /* route it */
1189 send_skb_packet(skb, batman_if, dstaddr);
1190
1191 return NET_RX_SUCCESS;
4efe0b06 1192}
5beef3c9 1193
e7017195 1194int recv_bcast_packet(struct sk_buff *skb)
4efe0b06
AL
1195{
1196 struct orig_node *orig_node;
1197 struct bcast_packet *bcast_packet;
e7017195
SW
1198 struct ethhdr *ethhdr;
1199 int hdr_size = sizeof(struct bcast_packet);
cf2d72ec 1200 int32_t seq_diff;
e7017195
SW
1201 unsigned long flags;
1202
1203 /* drop packet if it has not necessary minimum size */
1204 if (skb_headlen(skb) < hdr_size)
1205 return NET_RX_DROP;
1206
1207 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 1208
4efe0b06
AL
1209 /* packet with broadcast indication but unicast recipient */
1210 if (!is_bcast(ethhdr->h_dest))
e7017195 1211 return NET_RX_DROP;
5beef3c9 1212
4efe0b06
AL
1213 /* packet with broadcast sender address */
1214 if (is_bcast(ethhdr->h_source))
e7017195 1215 return NET_RX_DROP;
5beef3c9 1216
4efe0b06
AL
1217 /* ignore broadcasts sent by myself */
1218 if (is_my_mac(ethhdr->h_source))
e7017195 1219 return NET_RX_DROP;
5beef3c9 1220
208e13e4 1221 bcast_packet = (struct bcast_packet *)skb->data;
5beef3c9 1222
4efe0b06
AL
1223 /* ignore broadcasts originated by myself */
1224 if (is_my_mac(bcast_packet->orig))
e7017195 1225 return NET_RX_DROP;
5beef3c9 1226
cf2d72ec
SW
1227 if (bcast_packet->ttl < 2)
1228 return NET_RX_DROP;
1229
e7017195 1230 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
1231 orig_node = ((struct orig_node *)
1232 hash_find(orig_hash, bcast_packet->orig));
5beef3c9 1233
4efe0b06 1234 if (orig_node == NULL) {
e7017195
SW
1235 spin_unlock_irqrestore(&orig_hash_lock, flags);
1236 return NET_RX_DROP;
4efe0b06 1237 }
5beef3c9 1238
f94cee24 1239 /* check whether the packet is a duplicate */
4efe0b06
AL
1240 if (get_bit_status(orig_node->bcast_bits,
1241 orig_node->last_bcast_seqno,
cf2d72ec 1242 ntohl(bcast_packet->seqno))) {
e7017195
SW
1243 spin_unlock_irqrestore(&orig_hash_lock, flags);
1244 return NET_RX_DROP;
4efe0b06 1245 }
5beef3c9 1246
cf2d72ec 1247 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
f94cee24
SW
1248
1249 /* check whether the packet is old and the host just restarted. */
1250 if (window_protected(seq_diff, &orig_node->bcast_seqno_reset)) {
1251 spin_unlock_irqrestore(&orig_hash_lock, flags);
1252 return NET_RX_DROP;
1253 }
1254
1255 /* mark broadcast in flood history, update window position
1256 * if required. */
1257 if (bit_get_packet(orig_node->bcast_bits, seq_diff, 1))
cf2d72ec 1258 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
5beef3c9 1259
e7017195 1260 spin_unlock_irqrestore(&orig_hash_lock, flags);
e7017195
SW
1261 /* rebroadcast packet */
1262 add_bcast_packet_to_list(skb);
5beef3c9 1263
4efe0b06 1264 /* broadcast for me */
e7017195 1265 interface_rx(skb, hdr_size);
5beef3c9 1266
e7017195 1267 return NET_RX_SUCCESS;
4efe0b06 1268}
5beef3c9 1269
e7017195 1270int recv_vis_packet(struct sk_buff *skb)
4efe0b06
AL
1271{
1272 struct vis_packet *vis_packet;
e7017195 1273 struct ethhdr *ethhdr;
14741240 1274 struct bat_priv *bat_priv;
e7017195 1275 int hdr_size = sizeof(struct vis_packet);
5beef3c9 1276
e7017195
SW
1277 if (skb_headlen(skb) < hdr_size)
1278 return NET_RX_DROP;
1279
1280 vis_packet = (struct vis_packet *) skb->data;
1281 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 1282
4efe0b06
AL
1283 /* not for me */
1284 if (!is_my_mac(ethhdr->h_dest))
e7017195 1285 return NET_RX_DROP;
5beef3c9 1286
4efe0b06
AL
1287 /* ignore own packets */
1288 if (is_my_mac(vis_packet->vis_orig))
e7017195 1289 return NET_RX_DROP;
5beef3c9 1290
4efe0b06 1291 if (is_my_mac(vis_packet->sender_orig))
e7017195 1292 return NET_RX_DROP;
5beef3c9 1293
14741240
ML
1294 /* FIXME: each batman_if will be attached to a softif */
1295 bat_priv = netdev_priv(soft_device);
1296
4efe0b06
AL
1297 switch (vis_packet->vis_type) {
1298 case VIS_TYPE_SERVER_SYNC:
e7017195 1299 /* TODO: handle fragmented skbs properly */
14741240
ML
1300 receive_server_sync_packet(bat_priv, vis_packet,
1301 skb_headlen(skb));
4efe0b06 1302 break;
5beef3c9 1303
4efe0b06 1304 case VIS_TYPE_CLIENT_UPDATE:
e7017195 1305 /* TODO: handle fragmented skbs properly */
14741240
ML
1306 receive_client_update_packet(bat_priv, vis_packet,
1307 skb_headlen(skb));
4efe0b06 1308 break;
5beef3c9 1309
4efe0b06
AL
1310 default: /* ignore unknown packet */
1311 break;
1312 }
8d03847c
AL
1313
1314 /* We take a copy of the data in the packet, so we should
1315 always free the skbuf. */
1316 return NET_RX_DROP;
5beef3c9 1317}