]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/routing.c
Staging: batman-adv: Clone shared bat packets before modifying them
[net-next-2.6.git] / drivers / staging / batman-adv / routing.c
CommitLineData
5beef3c9
AL
1/*
2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
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"
28#include "device.h"
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
5beef3c9 36DECLARE_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{
4efe0b06
AL
80 /* route deleted */
81 if ((orig_node->router != NULL) && (neigh_node == NULL)) {
5beef3c9 82
b9b27e4e
AL
83 bat_dbg(DBG_ROUTES, "Deleting route towards: %pM\n",
84 orig_node->orig);
4efe0b06 85 hna_global_del_orig(orig_node, "originator timed out");
5beef3c9 86
4efe0b06
AL
87 /* route added */
88 } else if ((orig_node->router == NULL) && (neigh_node != NULL)) {
5beef3c9 89
4efe0b06 90 bat_dbg(DBG_ROUTES,
b9b27e4e
AL
91 "Adding route towards: %pM (via %pM)\n",
92 orig_node->orig, neigh_node->addr);
4efe0b06
AL
93 hna_global_add_orig(orig_node, hna_buff, hna_buff_len);
94
95 /* route changed */
5beef3c9 96 } else {
b9b27e4e 97 bat_dbg(DBG_ROUTES, "Changing route towards: %pM (now via %pM - was via %pM)\n", orig_node->orig, neigh_node->addr, orig_node->router->addr);
4efe0b06 98 }
5beef3c9 99
4efe0b06
AL
100 if (neigh_node != NULL)
101 orig_node->batman_if = neigh_node->if_incoming;
102 else
103 orig_node->batman_if = NULL;
5beef3c9 104
4efe0b06
AL
105 orig_node->router = neigh_node;
106}
5beef3c9 107
5beef3c9 108
8a2e042c 109void update_routes(struct orig_node *orig_node,
4efe0b06
AL
110 struct neigh_node *neigh_node,
111 unsigned char *hna_buff, int hna_buff_len)
112{
5beef3c9 113
4efe0b06
AL
114 if (orig_node == NULL)
115 return;
116
117 if (orig_node->router != neigh_node)
118 update_route(orig_node, neigh_node, hna_buff, hna_buff_len);
119 /* may be just HNA changed */
120 else
121 update_HNA(orig_node, hna_buff, hna_buff_len);
5beef3c9
AL
122}
123
4efe0b06
AL
124static int isBidirectionalNeigh(struct orig_node *orig_node,
125 struct orig_node *orig_neigh_node,
126 struct batman_packet *batman_packet,
127 struct batman_if *if_incoming)
5beef3c9
AL
128{
129 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
5beef3c9
AL
130 unsigned char total_count;
131
5beef3c9 132 if (orig_node == orig_neigh_node) {
4efe0b06
AL
133 list_for_each_entry(tmp_neigh_node,
134 &orig_node->neigh_list,
135 list) {
5beef3c9 136
4efe0b06
AL
137 if (compare_orig(tmp_neigh_node->addr,
138 orig_neigh_node->orig) &&
139 (tmp_neigh_node->if_incoming == if_incoming))
5beef3c9
AL
140 neigh_node = tmp_neigh_node;
141 }
142
c4bf05d3 143 if (!neigh_node)
4efe0b06
AL
144 neigh_node = create_neighbor(orig_node,
145 orig_neigh_node,
146 orig_neigh_node->orig,
147 if_incoming);
c4bf05d3
SW
148 /* create_neighbor failed, return 0 */
149 if (!neigh_node)
150 return 0;
5beef3c9
AL
151
152 neigh_node->last_valid = jiffies;
153 } else {
154 /* find packet count of corresponding one hop neighbor */
4efe0b06
AL
155 list_for_each_entry(tmp_neigh_node,
156 &orig_neigh_node->neigh_list, list) {
5beef3c9 157
4efe0b06
AL
158 if (compare_orig(tmp_neigh_node->addr,
159 orig_neigh_node->orig) &&
160 (tmp_neigh_node->if_incoming == if_incoming))
5beef3c9
AL
161 neigh_node = tmp_neigh_node;
162 }
163
c4bf05d3 164 if (!neigh_node)
4efe0b06
AL
165 neigh_node = create_neighbor(orig_neigh_node,
166 orig_neigh_node,
167 orig_neigh_node->orig,
168 if_incoming);
c4bf05d3
SW
169 /* create_neighbor failed, return 0 */
170 if (!neigh_node)
171 return 0;
5beef3c9
AL
172 }
173
174 orig_node->last_valid = jiffies;
175
176 /* pay attention to not get a value bigger than 100 % */
4efe0b06
AL
177 total_count = (orig_neigh_node->bcast_own_sum[if_incoming->if_num] >
178 neigh_node->real_packet_count ?
179 neigh_node->real_packet_count :
180 orig_neigh_node->bcast_own_sum[if_incoming->if_num]);
5beef3c9
AL
181
182 /* if we have too few packets (too less data) we set tq_own to zero */
183 /* if we receive too few packets it is not considered bidirectional */
4efe0b06
AL
184 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
185 (neigh_node->real_packet_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
5beef3c9
AL
186 orig_neigh_node->tq_own = 0;
187 else
4efe0b06
AL
188 /* neigh_node->real_packet_count is never zero as we
189 * only purge old information when getting new
190 * information */
191 orig_neigh_node->tq_own = (TQ_MAX_VALUE * total_count) /
192 neigh_node->real_packet_count;
5beef3c9
AL
193
194 /*
4efe0b06
AL
195 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
196 * affect the nearly-symmetric links only a little, but
197 * punishes asymmetric links more. This will give a value
198 * between 0 and TQ_MAX_VALUE
5beef3c9 199 */
4efe0b06
AL
200 orig_neigh_node->tq_asym_penalty =
201 TQ_MAX_VALUE -
202 (TQ_MAX_VALUE *
203 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
204 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count) *
205 (TQ_LOCAL_WINDOW_SIZE - neigh_node->real_packet_count)) /
206 (TQ_LOCAL_WINDOW_SIZE *
207 TQ_LOCAL_WINDOW_SIZE *
208 TQ_LOCAL_WINDOW_SIZE);
209
210 batman_packet->tq = ((batman_packet->tq *
211 orig_neigh_node->tq_own *
212 orig_neigh_node->tq_asym_penalty) /
213 (TQ_MAX_VALUE * TQ_MAX_VALUE));
5beef3c9 214
b9b27e4e
AL
215 bat_dbg(DBG_BATMAN, "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i \n",
216 orig_node->orig, orig_neigh_node->orig, total_count,
4efe0b06
AL
217 neigh_node->real_packet_count, orig_neigh_node->tq_own,
218 orig_neigh_node->tq_asym_penalty, batman_packet->tq);
5beef3c9 219
4efe0b06
AL
220 /* if link has the minimum required transmission quality
221 * consider it bidirectional */
5beef3c9
AL
222 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
223 return 1;
224
225 return 0;
226}
227
4efe0b06
AL
228static void update_orig(struct orig_node *orig_node, struct ethhdr *ethhdr,
229 struct batman_packet *batman_packet,
230 struct batman_if *if_incoming,
231 unsigned char *hna_buff, int hna_buff_len,
232 char is_duplicate)
5beef3c9
AL
233{
234 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
235 int tmp_hna_buff_len;
236
bad2239e 237 bat_dbg(DBG_BATMAN, "update_originator(): Searching and updating originator entry of received packet \n");
5beef3c9
AL
238
239 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
4efe0b06
AL
240 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
241 (tmp_neigh_node->if_incoming == if_incoming)) {
5beef3c9
AL
242 neigh_node = tmp_neigh_node;
243 continue;
244 }
245
246 if (is_duplicate)
247 continue;
248
4efe0b06
AL
249 ring_buffer_set(tmp_neigh_node->tq_recv,
250 &tmp_neigh_node->tq_index, 0);
251 tmp_neigh_node->tq_avg =
252 ring_buffer_avg(tmp_neigh_node->tq_recv);
5beef3c9
AL
253 }
254
c4bf05d3
SW
255 if (!neigh_node) {
256 struct orig_node *orig_tmp;
257
258 orig_tmp = get_orig_node(ethhdr->h_source);
259 if (!orig_tmp)
260 return;
261
4efe0b06 262 neigh_node = create_neighbor(orig_node,
c4bf05d3 263 orig_tmp,
4efe0b06 264 ethhdr->h_source, if_incoming);
c4bf05d3
SW
265 if (!neigh_node)
266 return;
267 } else
4efe0b06 268 bat_dbg(DBG_BATMAN,
5ea84fa3 269 "Updating existing last-hop neighbor of originator\n");
5beef3c9
AL
270
271 orig_node->flags = batman_packet->flags;
272 neigh_node->last_valid = jiffies;
273
4efe0b06
AL
274 ring_buffer_set(neigh_node->tq_recv,
275 &neigh_node->tq_index,
276 batman_packet->tq);
5beef3c9
AL
277 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
278
279 if (!is_duplicate) {
280 orig_node->last_ttl = batman_packet->ttl;
281 neigh_node->last_ttl = batman_packet->ttl;
282 }
283
4efe0b06
AL
284 tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
285 batman_packet->num_hna * ETH_ALEN : hna_buff_len);
5beef3c9 286
4efe0b06
AL
287 /* if this neighbor already is our next hop there is nothing
288 * to change */
5beef3c9
AL
289 if (orig_node->router == neigh_node)
290 goto update_hna;
291
292 /* if this neighbor does not offer a better TQ we won't consider it */
293 if ((orig_node->router) &&
294 (orig_node->router->tq_avg > neigh_node->tq_avg))
295 goto update_hna;
296
4efe0b06
AL
297 /* if the TQ is the same and the link not more symetric we
298 * won't consider it either */
5beef3c9
AL
299 if ((orig_node->router) &&
300 ((neigh_node->tq_avg == orig_node->router->tq_avg) &&
4efe0b06
AL
301 (orig_node->router->orig_node->bcast_own_sum[if_incoming->if_num]
302 >= neigh_node->orig_node->bcast_own_sum[if_incoming->if_num])))
5beef3c9
AL
303 goto update_hna;
304
305 update_routes(orig_node, neigh_node, hna_buff, tmp_hna_buff_len);
306 return;
307
308update_hna:
309 update_routes(orig_node, orig_node->router, hna_buff, tmp_hna_buff_len);
5beef3c9
AL
310}
311
4efe0b06
AL
312static char count_real_packets(struct ethhdr *ethhdr,
313 struct batman_packet *batman_packet,
314 struct batman_if *if_incoming)
5beef3c9
AL
315{
316 struct orig_node *orig_node;
317 struct neigh_node *tmp_neigh_node;
318 char is_duplicate = 0;
4efe0b06 319 uint16_t seq_diff;
5beef3c9
AL
320
321 orig_node = get_orig_node(batman_packet->orig);
322 if (orig_node == NULL)
323 return 0;
324
5beef3c9
AL
325 list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) {
326
327 if (!is_duplicate)
4efe0b06
AL
328 is_duplicate =
329 get_bit_status(tmp_neigh_node->real_bits,
330 orig_node->last_real_seqno,
331 batman_packet->seqno);
332 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
333 if (compare_orig(tmp_neigh_node->addr, ethhdr->h_source) &&
334 (tmp_neigh_node->if_incoming == if_incoming))
335 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 1);
5beef3c9 336 else
4efe0b06 337 bit_get_packet(tmp_neigh_node->real_bits, seq_diff, 0);
5beef3c9 338
4efe0b06
AL
339 tmp_neigh_node->real_packet_count =
340 bit_packet_count(tmp_neigh_node->real_bits);
5beef3c9
AL
341 }
342
343 if (!is_duplicate) {
bad2239e
AL
344 bat_dbg(DBG_BATMAN, "updating last_seqno: old %d, new %d \n",
345 orig_node->last_real_seqno, batman_packet->seqno);
5beef3c9
AL
346 orig_node->last_real_seqno = batman_packet->seqno;
347 }
348
349 return is_duplicate;
350}
351
4efe0b06 352void receive_bat_packet(struct ethhdr *ethhdr,
e7017195
SW
353 struct batman_packet *batman_packet,
354 unsigned char *hna_buff, int hna_buff_len,
355 struct batman_if *if_incoming)
5beef3c9
AL
356{
357 struct batman_if *batman_if;
358 struct orig_node *orig_neigh_node, *orig_node;
5beef3c9 359 char has_directlink_flag;
4efe0b06
AL
360 char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
361 char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
362 char is_duplicate;
5beef3c9
AL
363 unsigned short if_incoming_seqno;
364
4efe0b06
AL
365 /* Silently drop when the batman packet is actually not a
366 * correct packet.
5beef3c9
AL
367 *
368 * This might happen if a packet is padded (e.g. Ethernet has a
369 * minimum frame length of 64 byte) and the aggregation interprets
370 * it as an additional length.
371 *
4efe0b06
AL
372 * TODO: A more sane solution would be to have a bit in the
373 * batman_packet to detect whether the packet is the last
374 * packet in an aggregation. Here we expect that the padding
375 * is always zero (or not 0x01)
5beef3c9
AL
376 */
377 if (batman_packet->packet_type != BAT_PACKET)
378 return;
379
380 /* could be changed by schedule_own_packet() */
381 if_incoming_seqno = atomic_read(&if_incoming->seqno);
382
5beef3c9
AL
383 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
384
4efe0b06
AL
385 is_single_hop_neigh = (compare_orig(ethhdr->h_source,
386 batman_packet->orig) ? 1 : 0);
5beef3c9 387
b9b27e4e
AL
388 bat_dbg(DBG_BATMAN, "Received BATMAN packet via NB: %pM, IF: %s [%s] (from OG: %pM, via prev OG: %pM, seqno %d, tq %d, TTL %d, V %d, IDF %d) \n",
389 ethhdr->h_source, if_incoming->dev, if_incoming->addr_str,
390 batman_packet->orig, batman_packet->prev_sender,
391 batman_packet->seqno, batman_packet->tq, batman_packet->ttl,
392 batman_packet->version, has_directlink_flag);
5beef3c9
AL
393
394 list_for_each_entry_rcu(batman_if, &if_list, list) {
395 if (batman_if->if_active != IF_ACTIVE)
396 continue;
397
4efe0b06
AL
398 if (compare_orig(ethhdr->h_source,
399 batman_if->net_dev->dev_addr))
5beef3c9
AL
400 is_my_addr = 1;
401
4efe0b06
AL
402 if (compare_orig(batman_packet->orig,
403 batman_if->net_dev->dev_addr))
5beef3c9
AL
404 is_my_orig = 1;
405
4efe0b06
AL
406 if (compare_orig(batman_packet->prev_sender,
407 batman_if->net_dev->dev_addr))
5beef3c9
AL
408 is_my_oldorig = 1;
409
410 if (compare_orig(ethhdr->h_source, broadcastAddr))
411 is_broadcast = 1;
412 }
413
414 if (batman_packet->version != COMPAT_VERSION) {
4efe0b06
AL
415 bat_dbg(DBG_BATMAN,
416 "Drop packet: incompatible batman version (%i)\n",
417 batman_packet->version);
5beef3c9
AL
418 return;
419 }
420
421 if (is_my_addr) {
4efe0b06 422 bat_dbg(DBG_BATMAN,
b9b27e4e
AL
423 "Drop packet: received my own broadcast (sender: %pM)\n",
424 ethhdr->h_source);
5beef3c9
AL
425 return;
426 }
427
428 if (is_broadcast) {
b9b27e4e 429 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all packets with broadcast source addr (sender: %pM) \n", ethhdr->h_source);
5beef3c9
AL
430 return;
431 }
432
433 if (is_my_orig) {
4efe0b06
AL
434 TYPE_OF_WORD *word;
435 int offset;
436
5beef3c9
AL
437 orig_neigh_node = get_orig_node(ethhdr->h_source);
438
c4bf05d3
SW
439 if (!orig_neigh_node)
440 return;
441
5ea84fa3 442 /* neighbor has to indicate direct link and it has to
4efe0b06
AL
443 * come via the corresponding interface */
444 /* if received seqno equals last send seqno save new
445 * seqno for bidirectional check */
446 if (has_directlink_flag &&
447 compare_orig(if_incoming->net_dev->dev_addr,
448 batman_packet->orig) &&
449 (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
450 offset = if_incoming->if_num * NUM_WORDS;
451 word = &(orig_neigh_node->bcast_own[offset]);
452 bit_mark(word, 0);
453 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
454 bit_packet_count(word);
5beef3c9
AL
455 }
456
5ea84fa3 457 bat_dbg(DBG_BATMAN, "Drop packet: originator packet from myself (via neighbor) \n");
5beef3c9
AL
458 return;
459 }
460
461 if (batman_packet->tq == 0) {
462 count_real_packets(ethhdr, batman_packet, if_incoming);
463
bad2239e 464 bat_dbg(DBG_BATMAN, "Drop packet: originator packet with tq equal 0 \n");
5beef3c9
AL
465 return;
466 }
467
468 if (is_my_oldorig) {
b9b27e4e 469 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast echos (sender: %pM) \n", ethhdr->h_source);
5beef3c9
AL
470 return;
471 }
472
473 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
474
475 orig_node = get_orig_node(batman_packet->orig);
476 if (orig_node == NULL)
477 return;
478
479 /* avoid temporary routing loops */
4efe0b06
AL
480 if ((orig_node->router) &&
481 (orig_node->router->orig_node->router) &&
482 (compare_orig(orig_node->router->addr,
483 batman_packet->prev_sender)) &&
5beef3c9 484 !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) &&
4efe0b06
AL
485 (compare_orig(orig_node->router->addr,
486 orig_node->router->orig_node->router->addr))) {
b9b27e4e 487 bat_dbg(DBG_BATMAN, "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM) \n", ethhdr->h_source);
5beef3c9
AL
488 return;
489 }
490
4efe0b06
AL
491 /* if sender is a direct neighbor the sender mac equals
492 * originator mac */
493 orig_neigh_node = (is_single_hop_neigh ?
494 orig_node : get_orig_node(ethhdr->h_source));
5beef3c9
AL
495 if (orig_neigh_node == NULL)
496 return;
497
4efe0b06
AL
498 /* drop packet if sender is not a direct neighbor and if we
499 * don't route towards it */
500 if (!is_single_hop_neigh &&
501 (orig_neigh_node->router == NULL)) {
bad2239e 502 bat_dbg(DBG_BATMAN, "Drop packet: OGM via unknown neighbor!\n");
5beef3c9
AL
503 return;
504 }
505
4efe0b06
AL
506 is_bidirectional = isBidirectionalNeigh(orig_node, orig_neigh_node,
507 batman_packet, if_incoming);
5beef3c9 508
4efe0b06
AL
509 /* update ranking if it is not a duplicate or has the same
510 * seqno and similar ttl as the non-duplicate */
511 if (is_bidirectional &&
512 (!is_duplicate ||
513 ((orig_node->last_real_seqno == batman_packet->seqno) &&
514 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
515 update_orig(orig_node, ethhdr, batman_packet,
516 if_incoming, hna_buff, hna_buff_len, is_duplicate);
5beef3c9 517
5ea84fa3 518 /* is single hop (direct) neighbor */
5beef3c9
AL
519 if (is_single_hop_neigh) {
520
521 /* mark direct link on incoming interface */
4efe0b06
AL
522 schedule_forward_packet(orig_node, ethhdr, batman_packet,
523 1, hna_buff_len, if_incoming);
5beef3c9 524
5ea84fa3 525 bat_dbg(DBG_BATMAN, "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
5beef3c9
AL
526 return;
527 }
528
529 /* multihop originator */
530 if (!is_bidirectional) {
4efe0b06
AL
531 bat_dbg(DBG_BATMAN,
532 "Drop packet: not received via bidirectional link\n");
5beef3c9
AL
533 return;
534 }
535
536 if (is_duplicate) {
bad2239e 537 bat_dbg(DBG_BATMAN, "Drop packet: duplicate packet received\n");
5beef3c9
AL
538 return;
539 }
540
4efe0b06
AL
541 bat_dbg(DBG_BATMAN,
542 "Forwarding packet: rebroadcast originator packet\n");
543 schedule_forward_packet(orig_node, ethhdr, batman_packet,
544 0, hna_buff_len, if_incoming);
5beef3c9
AL
545}
546
e7017195
SW
547int recv_bat_packet(struct sk_buff *skb,
548 struct batman_if *batman_if)
5beef3c9 549{
e7017195
SW
550 struct ethhdr *ethhdr;
551 unsigned long flags;
f347b873 552 struct sk_buff *skb_old;
5beef3c9 553
e7017195
SW
554 /* drop packet if it has not necessary minimum size */
555 if (skb_headlen(skb) < sizeof(struct batman_packet))
556 return NET_RX_DROP;
5beef3c9 557
e7017195 558 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 559
4efe0b06
AL
560 /* packet with broadcast indication but unicast recipient */
561 if (!is_bcast(ethhdr->h_dest))
e7017195 562 return NET_RX_DROP;
4efe0b06
AL
563
564 /* packet with broadcast sender address */
565 if (is_bcast(ethhdr->h_source))
e7017195
SW
566 return NET_RX_DROP;
567
e7017195
SW
568 /* TODO: we use headlen instead of "length", because
569 * only this data is paged in. */
f347b873
SE
570
571 /* create a copy of the skb, if needed, to modify it. */
572 if (!skb_clone_writable(skb, skb_headlen(skb))) {
573 skb_old = skb;
574 skb = skb_copy(skb, GFP_ATOMIC);
575 if (!skb)
576 return NET_RX_DROP;
577 kfree_skb(skb_old);
578 }
579
580 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06 581 receive_aggr_bat_packet(ethhdr,
e7017195
SW
582 skb->data,
583 skb_headlen(skb),
4efe0b06 584 batman_if);
e7017195
SW
585 spin_unlock_irqrestore(&orig_hash_lock, flags);
586
587 kfree_skb(skb);
588 return NET_RX_SUCCESS;
4efe0b06
AL
589}
590
e7017195 591static int recv_my_icmp_packet(struct sk_buff *skb)
5beef3c9 592{
5beef3c9 593 struct orig_node *orig_node;
e7017195
SW
594 struct icmp_packet *icmp_packet;
595 struct ethhdr *ethhdr;
596 struct sk_buff *skb_old;
597 struct batman_if *batman_if;
598 int ret;
599 unsigned long flags;
600 uint8_t dstaddr[ETH_ALEN];
601
602 icmp_packet = (struct icmp_packet *) skb->data;
603 ethhdr = (struct ethhdr *) skb_mac_header(skb);
5beef3c9 604
4efe0b06
AL
605 /* add data to device queue */
606 if (icmp_packet->msg_type != ECHO_REQUEST) {
607 bat_device_receive_packet(icmp_packet);
e7017195 608 return NET_RX_DROP;
5beef3c9
AL
609 }
610
4efe0b06
AL
611 /* answer echo request (ping) */
612 /* get routing information */
e7017195 613 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
614 orig_node = ((struct orig_node *)hash_find(orig_hash,
615 icmp_packet->orig));
e7017195 616 ret = NET_RX_DROP;
4efe0b06
AL
617
618 if ((orig_node != NULL) &&
619 (orig_node->batman_if != NULL) &&
620 (orig_node->router != NULL)) {
e7017195
SW
621
622 /* don't lock while sending the packets ... we therefore
623 * copy the required data before sending */
624 batman_if = orig_node->batman_if;
625 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
626 spin_unlock_irqrestore(&orig_hash_lock, flags);
627
628 /* create a copy of the skb, if needed, to modify it. */
629 skb_old = NULL;
630 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
631 skb_old = skb;
632 skb = skb_copy(skb, GFP_ATOMIC);
633 if (!skb)
634 return NET_RX_DROP;
635 icmp_packet = (struct icmp_packet *) skb->data;
636 kfree_skb(skb_old);
637 }
638
4efe0b06
AL
639 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
640 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
641 icmp_packet->msg_type = ECHO_REPLY;
642 icmp_packet->ttl = TTL;
643
e7017195
SW
644 send_skb_packet(skb, batman_if, dstaddr);
645 ret = NET_RX_SUCCESS;
5beef3c9 646
e7017195
SW
647 } else
648 spin_unlock_irqrestore(&orig_hash_lock, flags);
649
650 return ret;
4efe0b06 651}
5beef3c9 652
e7017195 653static int recv_icmp_ttl_exceeded(struct sk_buff *skb)
4efe0b06 654{
4efe0b06 655 struct orig_node *orig_node;
e7017195
SW
656 struct icmp_packet *icmp_packet;
657 struct ethhdr *ethhdr;
658 struct sk_buff *skb_old;
659 struct batman_if *batman_if;
660 int ret;
661 unsigned long flags;
662 uint8_t dstaddr[ETH_ALEN];
663
149de2e5
ML
664 icmp_packet = (struct icmp_packet *)skb->data;
665 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 666
4efe0b06 667 /* send TTL exceeded if packet is an echo request (traceroute) */
149de2e5
ML
668 if (icmp_packet->msg_type != ECHO_REQUEST) {
669 printk(KERN_WARNING "batman-adv:Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
670 icmp_packet->orig, icmp_packet->dst);
e7017195 671 return NET_RX_DROP;
149de2e5 672 }
5beef3c9 673
4efe0b06 674 /* get routing information */
e7017195 675 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
676 orig_node = ((struct orig_node *)
677 hash_find(orig_hash, icmp_packet->orig));
e7017195 678 ret = NET_RX_DROP;
4efe0b06
AL
679
680 if ((orig_node != NULL) &&
681 (orig_node->batman_if != NULL) &&
682 (orig_node->router != NULL)) {
e7017195
SW
683
684 /* don't lock while sending the packets ... we therefore
685 * copy the required data before sending */
686 batman_if = orig_node->batman_if;
687 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
688 spin_unlock_irqrestore(&orig_hash_lock, flags);
689
690 /* create a copy of the skb, if needed, to modify it. */
691 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
692 skb_old = skb;
693 skb = skb_copy(skb, GFP_ATOMIC);
694 if (!skb)
695 return NET_RX_DROP;
696 icmp_packet = (struct icmp_packet *) skb->data;
697 kfree_skb(skb_old);
698 }
699
4efe0b06
AL
700 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
701 memcpy(icmp_packet->orig, ethhdr->h_dest, ETH_ALEN);
702 icmp_packet->msg_type = TTL_EXCEEDED;
703 icmp_packet->ttl = TTL;
704
e7017195
SW
705 send_skb_packet(skb, batman_if, dstaddr);
706 ret = NET_RX_SUCCESS;
5beef3c9 707
e7017195
SW
708 } else
709 spin_unlock_irqrestore(&orig_hash_lock, flags);
5beef3c9 710
e7017195 711 return ret;
4efe0b06 712}
5beef3c9 713
5beef3c9 714
e7017195 715int recv_icmp_packet(struct sk_buff *skb)
4efe0b06
AL
716{
717 struct icmp_packet *icmp_packet;
e7017195 718 struct ethhdr *ethhdr;
4efe0b06 719 struct orig_node *orig_node;
e7017195
SW
720 struct sk_buff *skb_old;
721 struct batman_if *batman_if;
722 int hdr_size = sizeof(struct icmp_packet);
723 int ret;
724 unsigned long flags;
725 uint8_t dstaddr[ETH_ALEN];
726
727 /* drop packet if it has not necessary minimum size */
728 if (skb_headlen(skb) < hdr_size)
729 return NET_RX_DROP;
730
731 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 732
4efe0b06
AL
733 /* packet with unicast indication but broadcast recipient */
734 if (is_bcast(ethhdr->h_dest))
e7017195 735 return NET_RX_DROP;
5beef3c9 736
4efe0b06
AL
737 /* packet with broadcast sender address */
738 if (is_bcast(ethhdr->h_source))
e7017195 739 return NET_RX_DROP;
5beef3c9 740
4efe0b06
AL
741 /* not for me */
742 if (!is_my_mac(ethhdr->h_dest))
e7017195 743 return NET_RX_DROP;
5beef3c9 744
e7017195 745 icmp_packet = (struct icmp_packet *) skb->data;
5beef3c9 746
4efe0b06
AL
747 /* packet for me */
748 if (is_my_mac(icmp_packet->dst))
e7017195 749 return recv_my_icmp_packet(skb);
5beef3c9 750
4efe0b06 751 /* TTL exceeded */
e7017195
SW
752 if (icmp_packet->ttl < 2)
753 return recv_icmp_ttl_exceeded(skb);
5beef3c9 754
e7017195 755 ret = NET_RX_DROP;
5beef3c9 756
4efe0b06 757 /* get routing information */
e7017195 758 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
759 orig_node = ((struct orig_node *)
760 hash_find(orig_hash, icmp_packet->dst));
5beef3c9 761
4efe0b06
AL
762 if ((orig_node != NULL) &&
763 (orig_node->batman_if != NULL) &&
764 (orig_node->router != NULL)) {
5beef3c9 765
e7017195
SW
766 /* don't lock while sending the packets ... we therefore
767 * copy the required data before sending */
768 batman_if = orig_node->batman_if;
769 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
770 spin_unlock_irqrestore(&orig_hash_lock, flags);
771
772 /* create a copy of the skb, if needed, to modify it. */
773 if (!skb_clone_writable(skb, sizeof(struct icmp_packet))) {
774 skb_old = skb;
775 skb = skb_copy(skb, GFP_ATOMIC);
776 if (!skb)
777 return NET_RX_DROP;
778 icmp_packet = (struct icmp_packet *) skb->data;
779 kfree_skb(skb_old);
780 }
781
4efe0b06
AL
782 /* decrement ttl */
783 icmp_packet->ttl--;
5beef3c9 784
4efe0b06 785 /* route it */
e7017195
SW
786 send_skb_packet(skb, batman_if, dstaddr);
787 ret = NET_RX_SUCCESS;
788
789 } else
790 spin_unlock_irqrestore(&orig_hash_lock, flags);
791
792 return ret;
4efe0b06 793}
5beef3c9 794
e7017195 795int recv_unicast_packet(struct sk_buff *skb)
4efe0b06
AL
796{
797 struct unicast_packet *unicast_packet;
4efe0b06 798 struct orig_node *orig_node;
e7017195
SW
799 struct ethhdr *ethhdr;
800 struct batman_if *batman_if;
801 struct sk_buff *skb_old;
802 uint8_t dstaddr[ETH_ALEN];
803 int hdr_size = sizeof(struct unicast_packet);
804 int ret;
805 unsigned long flags;
806
807 /* drop packet if it has not necessary minimum size */
808 if (skb_headlen(skb) < hdr_size)
809 return NET_RX_DROP;
810
811 ethhdr = (struct ethhdr *) skb_mac_header(skb);
5beef3c9 812
4efe0b06
AL
813 /* packet with unicast indication but broadcast recipient */
814 if (is_bcast(ethhdr->h_dest))
e7017195 815 return NET_RX_DROP;
5beef3c9 816
4efe0b06
AL
817 /* packet with broadcast sender address */
818 if (is_bcast(ethhdr->h_source))
e7017195 819 return NET_RX_DROP;
5beef3c9 820
4efe0b06
AL
821 /* not for me */
822 if (!is_my_mac(ethhdr->h_dest))
e7017195 823 return NET_RX_DROP;
5beef3c9 824
e7017195 825 unicast_packet = (struct unicast_packet *) skb->data;
5beef3c9 826
4efe0b06
AL
827 /* packet for me */
828 if (is_my_mac(unicast_packet->dest)) {
e7017195
SW
829 interface_rx(skb, hdr_size);
830 return NET_RX_SUCCESS;
4efe0b06 831 }
5beef3c9 832
4efe0b06
AL
833 /* TTL exceeded */
834 if (unicast_packet->ttl < 2) {
149de2e5
ML
835 printk(KERN_WARNING "batman-adv:Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
836 ethhdr->h_source, unicast_packet->dest);
e7017195 837 return NET_RX_DROP;
4efe0b06 838 }
5beef3c9 839
e7017195 840 ret = NET_RX_DROP;
4efe0b06 841 /* get routing information */
e7017195 842 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
843 orig_node = ((struct orig_node *)
844 hash_find(orig_hash, unicast_packet->dest));
845
846 if ((orig_node != NULL) &&
847 (orig_node->batman_if != NULL) &&
848 (orig_node->router != NULL)) {
e7017195
SW
849
850 /* don't lock while sending the packets ... we therefore
851 * copy the required data before sending */
852 batman_if = orig_node->batman_if;
853 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
854 spin_unlock_irqrestore(&orig_hash_lock, flags);
855
856 /* create a copy of the skb, if needed, to modify it. */
857 if (!skb_clone_writable(skb, sizeof(struct unicast_packet))) {
858 skb_old = skb;
859 skb = skb_copy(skb, GFP_ATOMIC);
860 if (!skb)
861 return NET_RX_DROP;
862 unicast_packet = (struct unicast_packet *) skb->data;
863 kfree_skb(skb_old);
864 }
4efe0b06
AL
865 /* decrement ttl */
866 unicast_packet->ttl--;
867
868 /* route it */
e7017195
SW
869 send_skb_packet(skb, batman_if, dstaddr);
870 ret = NET_RX_SUCCESS;
871
872 } else
873 spin_unlock_irqrestore(&orig_hash_lock, flags);
874
875 return ret;
4efe0b06 876}
5beef3c9 877
5beef3c9 878
e7017195 879int recv_bcast_packet(struct sk_buff *skb)
4efe0b06
AL
880{
881 struct orig_node *orig_node;
882 struct bcast_packet *bcast_packet;
e7017195
SW
883 struct ethhdr *ethhdr;
884 int hdr_size = sizeof(struct bcast_packet);
885 unsigned long flags;
886
887 /* drop packet if it has not necessary minimum size */
888 if (skb_headlen(skb) < hdr_size)
889 return NET_RX_DROP;
890
891 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 892
4efe0b06
AL
893 /* packet with broadcast indication but unicast recipient */
894 if (!is_bcast(ethhdr->h_dest))
e7017195 895 return NET_RX_DROP;
5beef3c9 896
4efe0b06
AL
897 /* packet with broadcast sender address */
898 if (is_bcast(ethhdr->h_source))
e7017195 899 return NET_RX_DROP;
5beef3c9 900
4efe0b06
AL
901 /* ignore broadcasts sent by myself */
902 if (is_my_mac(ethhdr->h_source))
e7017195 903 return NET_RX_DROP;
5beef3c9 904
e7017195 905 bcast_packet = (struct bcast_packet *) skb->data;
5beef3c9 906
4efe0b06
AL
907 /* ignore broadcasts originated by myself */
908 if (is_my_mac(bcast_packet->orig))
e7017195 909 return NET_RX_DROP;
5beef3c9 910
e7017195 911 spin_lock_irqsave(&orig_hash_lock, flags);
4efe0b06
AL
912 orig_node = ((struct orig_node *)
913 hash_find(orig_hash, bcast_packet->orig));
5beef3c9 914
4efe0b06 915 if (orig_node == NULL) {
e7017195
SW
916 spin_unlock_irqrestore(&orig_hash_lock, flags);
917 return NET_RX_DROP;
4efe0b06 918 }
5beef3c9 919
4efe0b06
AL
920 /* check flood history */
921 if (get_bit_status(orig_node->bcast_bits,
922 orig_node->last_bcast_seqno,
923 ntohs(bcast_packet->seqno))) {
e7017195
SW
924 spin_unlock_irqrestore(&orig_hash_lock, flags);
925 return NET_RX_DROP;
4efe0b06 926 }
5beef3c9 927
4efe0b06
AL
928 /* mark broadcast in flood history */
929 if (bit_get_packet(orig_node->bcast_bits,
930 ntohs(bcast_packet->seqno) -
931 orig_node->last_bcast_seqno, 1))
932 orig_node->last_bcast_seqno = ntohs(bcast_packet->seqno);
5beef3c9 933
e7017195
SW
934 spin_unlock_irqrestore(&orig_hash_lock, flags);
935
936 /* rebroadcast packet */
937 add_bcast_packet_to_list(skb);
5beef3c9 938
4efe0b06 939 /* broadcast for me */
e7017195 940 interface_rx(skb, hdr_size);
5beef3c9 941
e7017195 942 return NET_RX_SUCCESS;
4efe0b06 943}
5beef3c9 944
e7017195 945int recv_vis_packet(struct sk_buff *skb)
4efe0b06
AL
946{
947 struct vis_packet *vis_packet;
e7017195
SW
948 struct ethhdr *ethhdr;
949 int hdr_size = sizeof(struct vis_packet);
5beef3c9 950
e7017195
SW
951 if (skb_headlen(skb) < hdr_size)
952 return NET_RX_DROP;
953
954 vis_packet = (struct vis_packet *) skb->data;
955 ethhdr = (struct ethhdr *)skb_mac_header(skb);
5beef3c9 956
4efe0b06
AL
957 /* not for me */
958 if (!is_my_mac(ethhdr->h_dest))
e7017195 959 return NET_RX_DROP;
5beef3c9 960
4efe0b06
AL
961 /* ignore own packets */
962 if (is_my_mac(vis_packet->vis_orig))
e7017195 963 return NET_RX_DROP;
5beef3c9 964
4efe0b06 965 if (is_my_mac(vis_packet->sender_orig))
e7017195 966 return NET_RX_DROP;
5beef3c9 967
4efe0b06
AL
968 switch (vis_packet->vis_type) {
969 case VIS_TYPE_SERVER_SYNC:
e7017195
SW
970 /* TODO: handle fragmented skbs properly */
971 receive_server_sync_packet(vis_packet, skb_headlen(skb));
4efe0b06 972 break;
5beef3c9 973
4efe0b06 974 case VIS_TYPE_CLIENT_UPDATE:
e7017195
SW
975 /* TODO: handle fragmented skbs properly */
976 receive_client_update_packet(vis_packet, skb_headlen(skb));
4efe0b06 977 break;
5beef3c9 978
4efe0b06
AL
979 default: /* ignore unknown packet */
980 break;
981 }
8d03847c
AL
982
983 /* We take a copy of the data in the packet, so we should
984 always free the skbuf. */
985 return NET_RX_DROP;
5beef3c9 986}