aboutsummaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorMarek Lindner <lindner_marek@yahoo.de>2011-07-30 06:04:12 -0400
committerMarek Lindner <lindner_marek@yahoo.de>2011-09-08 11:39:06 -0400
commitfc9572756418e13e0bffaf2d58f678b907602507 (patch)
tree373a9b5631dbaa2bc8fc41c75d094a7f0bb157e5 /net
parentb6da4bf5d7951aba4f86d65546474c4e718f650f (diff)
batman-adv: agglomerate all batman iv ogm processing functions in a single file
In preparation of the upcoming improved routing algorithm the code based has to be re-organized to allow choosing the routing algorithm at compile time. Signed-off-by: Marek Lindner <lindner_marek@yahoo.de>
Diffstat (limited to 'net')
-rw-r--r--net/batman-adv/Makefile1
-rw-r--r--net/batman-adv/aggregation.c31
-rw-r--r--net/batman-adv/aggregation.h3
-rw-r--r--net/batman-adv/bat_iv_ogm.c639
-rw-r--r--net/batman-adv/bat_ogm.h30
-rw-r--r--net/batman-adv/hard-interface.c2
-rw-r--r--net/batman-adv/originator.c3
-rw-r--r--net/batman-adv/routing.c606
-rw-r--r--net/batman-adv/routing.h17
9 files changed, 698 insertions, 634 deletions
diff --git a/net/batman-adv/Makefile b/net/batman-adv/Makefile
index 2de93d00631b..32935986af3b 100644
--- a/net/batman-adv/Makefile
+++ b/net/batman-adv/Makefile
@@ -21,6 +21,7 @@
21obj-$(CONFIG_BATMAN_ADV) += batman-adv.o 21obj-$(CONFIG_BATMAN_ADV) += batman-adv.o
22batman-adv-y += aggregation.o 22batman-adv-y += aggregation.o
23batman-adv-y += bat_debugfs.o 23batman-adv-y += bat_debugfs.o
24batman-adv-y += bat_iv_ogm.o
24batman-adv-y += bat_sysfs.o 25batman-adv-y += bat_sysfs.o
25batman-adv-y += bitarray.o 26batman-adv-y += bitarray.o
26batman-adv-y += gateway_client.o 27batman-adv-y += gateway_client.o
diff --git a/net/batman-adv/aggregation.c b/net/batman-adv/aggregation.c
index f20423f4a4d7..4716c9386f21 100644
--- a/net/batman-adv/aggregation.c
+++ b/net/batman-adv/aggregation.c
@@ -264,34 +264,3 @@ void add_bat_packet_to_list(struct bat_priv *bat_priv,
264 spin_unlock_bh(&bat_priv->forw_bat_list_lock); 264 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
265 } 265 }
266} 266}
267
268/* unpack the aggregated packets and process them one by one */
269void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
270 unsigned char *packet_buff, int packet_len,
271 struct hard_iface *if_incoming)
272{
273 struct batman_ogm_packet *batman_ogm_packet;
274 int buff_pos = 0;
275 unsigned char *tt_buff;
276
277 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
278
279 do {
280 /* network to host order for our 32bit seqno and the
281 orig_interval */
282 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
283 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
284
285 tt_buff = packet_buff + buff_pos + BATMAN_OGM_LEN;
286
287 receive_bat_packet(ethhdr, batman_ogm_packet,
288 tt_buff, if_incoming);
289
290 buff_pos += BATMAN_OGM_LEN +
291 tt_len(batman_ogm_packet->tt_num_changes);
292
293 batman_ogm_packet = (struct batman_ogm_packet *)
294 (packet_buff + buff_pos);
295 } while (aggregated_packet(buff_pos, packet_len,
296 batman_ogm_packet->tt_num_changes));
297}
diff --git a/net/batman-adv/aggregation.h b/net/batman-adv/aggregation.h
index 7fc23b028b2a..7a92e4c69ed0 100644
--- a/net/batman-adv/aggregation.h
+++ b/net/batman-adv/aggregation.h
@@ -38,8 +38,5 @@ void add_bat_packet_to_list(struct bat_priv *bat_priv,
38 unsigned char *packet_buff, int packet_len, 38 unsigned char *packet_buff, int packet_len,
39 struct hard_iface *if_incoming, int own_packet, 39 struct hard_iface *if_incoming, int own_packet,
40 unsigned long send_time); 40 unsigned long send_time);
41void receive_aggr_bat_packet(const struct ethhdr *ethhdr,
42 unsigned char *packet_buff, int packet_len,
43 struct hard_iface *if_incoming);
44 41
45#endif /* _NET_BATMAN_ADV_AGGREGATION_H_ */ 42#endif /* _NET_BATMAN_ADV_AGGREGATION_H_ */
diff --git a/net/batman-adv/bat_iv_ogm.c b/net/batman-adv/bat_iv_ogm.c
new file mode 100644
index 000000000000..6fa2d465b6cd
--- /dev/null
+++ b/net/batman-adv/bat_iv_ogm.c
@@ -0,0 +1,639 @@
1/*
2 * Copyright (C) 2007-2011 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
22#include "main.h"
23#include "bat_ogm.h"
24#include "translation-table.h"
25#include "ring_buffer.h"
26#include "originator.h"
27#include "routing.h"
28#include "gateway_common.h"
29#include "gateway_client.h"
30#include "hard-interface.h"
31#include "send.h"
32
33/* is there another aggregated packet here? */
34static int bat_ogm_aggr_packet(int buff_pos, int packet_len,
35 int tt_num_changes)
36{
37 int next_buff_pos = buff_pos + BATMAN_OGM_LEN + tt_len(tt_num_changes);
38
39 return (next_buff_pos <= packet_len) &&
40 (next_buff_pos <= MAX_AGGREGATION_BYTES);
41}
42
43static void bat_ogm_orig_update(struct bat_priv *bat_priv,
44 struct orig_node *orig_node,
45 const struct ethhdr *ethhdr,
46 const struct batman_ogm_packet
47 *batman_ogm_packet,
48 struct hard_iface *if_incoming,
49 const unsigned char *tt_buff, int is_duplicate)
50{
51 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
52 struct neigh_node *router = NULL;
53 struct orig_node *orig_node_tmp;
54 struct hlist_node *node;
55 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
56
57 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
58 "Searching and updating originator entry of received packet\n");
59
60 rcu_read_lock();
61 hlist_for_each_entry_rcu(tmp_neigh_node, node,
62 &orig_node->neigh_list, list) {
63 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
64 (tmp_neigh_node->if_incoming == if_incoming) &&
65 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
66 if (neigh_node)
67 neigh_node_free_ref(neigh_node);
68 neigh_node = tmp_neigh_node;
69 continue;
70 }
71
72 if (is_duplicate)
73 continue;
74
75 spin_lock_bh(&tmp_neigh_node->tq_lock);
76 ring_buffer_set(tmp_neigh_node->tq_recv,
77 &tmp_neigh_node->tq_index, 0);
78 tmp_neigh_node->tq_avg =
79 ring_buffer_avg(tmp_neigh_node->tq_recv);
80 spin_unlock_bh(&tmp_neigh_node->tq_lock);
81 }
82
83 if (!neigh_node) {
84 struct orig_node *orig_tmp;
85
86 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
87 if (!orig_tmp)
88 goto unlock;
89
90 neigh_node = create_neighbor(orig_node, orig_tmp,
91 ethhdr->h_source, if_incoming);
92
93 orig_node_free_ref(orig_tmp);
94 if (!neigh_node)
95 goto unlock;
96 } else
97 bat_dbg(DBG_BATMAN, bat_priv,
98 "Updating existing last-hop neighbor of originator\n");
99
100 rcu_read_unlock();
101
102 orig_node->flags = batman_ogm_packet->flags;
103 neigh_node->last_valid = jiffies;
104
105 spin_lock_bh(&neigh_node->tq_lock);
106 ring_buffer_set(neigh_node->tq_recv,
107 &neigh_node->tq_index,
108 batman_ogm_packet->tq);
109 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
110 spin_unlock_bh(&neigh_node->tq_lock);
111
112 if (!is_duplicate) {
113 orig_node->last_ttl = batman_ogm_packet->ttl;
114 neigh_node->last_ttl = batman_ogm_packet->ttl;
115 }
116
117 bonding_candidate_add(orig_node, neigh_node);
118
119 /* if this neighbor already is our next hop there is nothing
120 * to change */
121 router = orig_node_get_router(orig_node);
122 if (router == neigh_node)
123 goto update_tt;
124
125 /* if this neighbor does not offer a better TQ we won't consider it */
126 if (router && (router->tq_avg > neigh_node->tq_avg))
127 goto update_tt;
128
129 /* if the TQ is the same and the link not more symmetric we
130 * won't consider it either */
131 if (router && (neigh_node->tq_avg == router->tq_avg)) {
132 orig_node_tmp = router->orig_node;
133 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
134 bcast_own_sum_orig =
135 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
136 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
137
138 orig_node_tmp = neigh_node->orig_node;
139 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
140 bcast_own_sum_neigh =
141 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
142 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
143
144 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
145 goto update_tt;
146 }
147
148 update_route(bat_priv, orig_node, neigh_node);
149
150update_tt:
151 /* I have to check for transtable changes only if the OGM has been
152 * sent through a primary interface */
153 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
154 (batman_ogm_packet->ttl > 2)) ||
155 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
156 tt_update_orig(bat_priv, orig_node, tt_buff,
157 batman_ogm_packet->tt_num_changes,
158 batman_ogm_packet->ttvn,
159 batman_ogm_packet->tt_crc);
160
161 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
162 gw_node_update(bat_priv, orig_node,
163 batman_ogm_packet->gw_flags);
164
165 orig_node->gw_flags = batman_ogm_packet->gw_flags;
166
167 /* restart gateway selection if fast or late switching was enabled */
168 if ((orig_node->gw_flags) &&
169 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
170 (atomic_read(&bat_priv->gw_sel_class) > 2))
171 gw_check_election(bat_priv, orig_node);
172
173 goto out;
174
175unlock:
176 rcu_read_unlock();
177out:
178 if (neigh_node)
179 neigh_node_free_ref(neigh_node);
180 if (router)
181 neigh_node_free_ref(router);
182}
183
184static int bat_ogm_calc_tq(struct orig_node *orig_node,
185 struct orig_node *orig_neigh_node,
186 struct batman_ogm_packet *batman_ogm_packet,
187 struct hard_iface *if_incoming)
188{
189 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
190 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
191 struct hlist_node *node;
192 uint8_t total_count;
193 uint8_t orig_eq_count, neigh_rq_count, tq_own;
194 int tq_asym_penalty, ret = 0;
195
196 /* find corresponding one hop neighbor */
197 rcu_read_lock();
198 hlist_for_each_entry_rcu(tmp_neigh_node, node,
199 &orig_neigh_node->neigh_list, list) {
200
201 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
202 continue;
203
204 if (tmp_neigh_node->if_incoming != if_incoming)
205 continue;
206
207 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
208 continue;
209
210 neigh_node = tmp_neigh_node;
211 break;
212 }
213 rcu_read_unlock();
214
215 if (!neigh_node)
216 neigh_node = create_neighbor(orig_neigh_node,
217 orig_neigh_node,
218 orig_neigh_node->orig,
219 if_incoming);
220
221 if (!neigh_node)
222 goto out;
223
224 /* if orig_node is direct neighbor update neigh_node last_valid */
225 if (orig_node == orig_neigh_node)
226 neigh_node->last_valid = jiffies;
227
228 orig_node->last_valid = jiffies;
229
230 /* find packet count of corresponding one hop neighbor */
231 spin_lock_bh(&orig_node->ogm_cnt_lock);
232 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
233 neigh_rq_count = neigh_node->real_packet_count;
234 spin_unlock_bh(&orig_node->ogm_cnt_lock);
235
236 /* pay attention to not get a value bigger than 100 % */
237 total_count = (orig_eq_count > neigh_rq_count ?
238 neigh_rq_count : orig_eq_count);
239
240 /* if we have too few packets (too less data) we set tq_own to zero */
241 /* if we receive too few packets it is not considered bidirectional */
242 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
243 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
244 tq_own = 0;
245 else
246 /* neigh_node->real_packet_count is never zero as we
247 * only purge old information when getting new
248 * information */
249 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
250
251 /*
252 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
253 * affect the nearly-symmetric links only a little, but
254 * punishes asymmetric links more. This will give a value
255 * between 0 and TQ_MAX_VALUE
256 */
257 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
258 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
259 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
260 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
261 (TQ_LOCAL_WINDOW_SIZE *
262 TQ_LOCAL_WINDOW_SIZE *
263 TQ_LOCAL_WINDOW_SIZE);
264
265 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
266 * tq_asym_penalty) /
267 (TQ_MAX_VALUE * TQ_MAX_VALUE));
268
269 bat_dbg(DBG_BATMAN, bat_priv,
270 "bidirectional: "
271 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
272 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
273 "total tq: %3i\n",
274 orig_node->orig, orig_neigh_node->orig, total_count,
275 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
276
277 /* if link has the minimum required transmission quality
278 * consider it bidirectional */
279 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
280 ret = 1;
281
282out:
283 if (neigh_node)
284 neigh_node_free_ref(neigh_node);
285 return ret;
286}
287
288/* processes a batman packet for all interfaces, adjusts the sequence number and
289 * finds out whether it is a duplicate.
290 * returns:
291 * 1 the packet is a duplicate
292 * 0 the packet has not yet been received
293 * -1 the packet is old and has been received while the seqno window
294 * was protected. Caller should drop it.
295 */
296static int bat_ogm_update_seqnos(const struct ethhdr *ethhdr,
297 const struct batman_ogm_packet
298 *batman_ogm_packet,
299 const struct hard_iface *if_incoming)
300{
301 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
302 struct orig_node *orig_node;
303 struct neigh_node *tmp_neigh_node;
304 struct hlist_node *node;
305 int is_duplicate = 0;
306 int32_t seq_diff;
307 int need_update = 0;
308 int set_mark, ret = -1;
309
310 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
311 if (!orig_node)
312 return 0;
313
314 spin_lock_bh(&orig_node->ogm_cnt_lock);
315 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
316
317 /* signalize caller that the packet is to be dropped. */
318 if (window_protected(bat_priv, seq_diff,
319 &orig_node->batman_seqno_reset))
320 goto out;
321
322 rcu_read_lock();
323 hlist_for_each_entry_rcu(tmp_neigh_node, node,
324 &orig_node->neigh_list, list) {
325
326 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
327 orig_node->last_real_seqno,
328 batman_ogm_packet->seqno);
329
330 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
331 (tmp_neigh_node->if_incoming == if_incoming))
332 set_mark = 1;
333 else
334 set_mark = 0;
335
336 /* if the window moved, set the update flag. */
337 need_update |= bit_get_packet(bat_priv,
338 tmp_neigh_node->real_bits,
339 seq_diff, set_mark);
340
341 tmp_neigh_node->real_packet_count =
342 bit_packet_count(tmp_neigh_node->real_bits);
343 }
344 rcu_read_unlock();
345
346 if (need_update) {
347 bat_dbg(DBG_BATMAN, bat_priv,
348 "updating last_seqno: old %d, new %d\n",
349 orig_node->last_real_seqno, batman_ogm_packet->seqno);
350 orig_node->last_real_seqno = batman_ogm_packet->seqno;
351 }
352
353 ret = is_duplicate;
354
355out:
356 spin_unlock_bh(&orig_node->ogm_cnt_lock);
357 orig_node_free_ref(orig_node);
358 return ret;
359}
360
361static void bat_ogm_process(const struct ethhdr *ethhdr,
362 struct batman_ogm_packet *batman_ogm_packet,
363 const unsigned char *tt_buff,
364 struct hard_iface *if_incoming)
365{
366 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
367 struct hard_iface *hard_iface;
368 struct orig_node *orig_neigh_node, *orig_node;
369 struct neigh_node *router = NULL, *router_router = NULL;
370 struct neigh_node *orig_neigh_router = NULL;
371 int has_directlink_flag;
372 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
373 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
374 int is_duplicate;
375 uint32_t if_incoming_seqno;
376
377 /* Silently drop when the batman packet is actually not a
378 * correct packet.
379 *
380 * This might happen if a packet is padded (e.g. Ethernet has a
381 * minimum frame length of 64 byte) and the aggregation interprets
382 * it as an additional length.
383 *
384 * TODO: A more sane solution would be to have a bit in the
385 * batman_ogm_packet to detect whether the packet is the last
386 * packet in an aggregation. Here we expect that the padding
387 * is always zero (or not 0x01)
388 */
389 if (batman_ogm_packet->packet_type != BAT_OGM)
390 return;
391
392 /* could be changed by schedule_own_packet() */
393 if_incoming_seqno = atomic_read(&if_incoming->seqno);
394
395 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
396
397 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
398 batman_ogm_packet->orig) ? 1 : 0);
399
400 bat_dbg(DBG_BATMAN, bat_priv,
401 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
402 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
403 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
404 ethhdr->h_source, if_incoming->net_dev->name,
405 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
406 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
407 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
408 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
409 batman_ogm_packet->ttl, batman_ogm_packet->version,
410 has_directlink_flag);
411
412 rcu_read_lock();
413 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
414 if (hard_iface->if_status != IF_ACTIVE)
415 continue;
416
417 if (hard_iface->soft_iface != if_incoming->soft_iface)
418 continue;
419
420 if (compare_eth(ethhdr->h_source,
421 hard_iface->net_dev->dev_addr))
422 is_my_addr = 1;
423
424 if (compare_eth(batman_ogm_packet->orig,
425 hard_iface->net_dev->dev_addr))
426 is_my_orig = 1;
427
428 if (compare_eth(batman_ogm_packet->prev_sender,
429 hard_iface->net_dev->dev_addr))
430 is_my_oldorig = 1;
431
432 if (is_broadcast_ether_addr(ethhdr->h_source))
433 is_broadcast = 1;
434 }
435 rcu_read_unlock();
436
437 if (batman_ogm_packet->version != COMPAT_VERSION) {
438 bat_dbg(DBG_BATMAN, bat_priv,
439 "Drop packet: incompatible batman version (%i)\n",
440 batman_ogm_packet->version);
441 return;
442 }
443
444 if (is_my_addr) {
445 bat_dbg(DBG_BATMAN, bat_priv,
446 "Drop packet: received my own broadcast (sender: %pM"
447 ")\n",
448 ethhdr->h_source);
449 return;
450 }
451
452 if (is_broadcast) {
453 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
454 "ignoring all packets with broadcast source addr (sender: %pM"
455 ")\n", ethhdr->h_source);
456 return;
457 }
458
459 if (is_my_orig) {
460 unsigned long *word;
461 int offset;
462
463 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
464 if (!orig_neigh_node)
465 return;
466
467 /* neighbor has to indicate direct link and it has to
468 * come via the corresponding interface */
469 /* save packet seqno for bidirectional check */
470 if (has_directlink_flag &&
471 compare_eth(if_incoming->net_dev->dev_addr,
472 batman_ogm_packet->orig)) {
473 offset = if_incoming->if_num * NUM_WORDS;
474
475 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
476 word = &(orig_neigh_node->bcast_own[offset]);
477 bit_mark(word,
478 if_incoming_seqno -
479 batman_ogm_packet->seqno - 2);
480 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
481 bit_packet_count(word);
482 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
483 }
484
485 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
486 "originator packet from myself (via neighbor)\n");
487 orig_node_free_ref(orig_neigh_node);
488 return;
489 }
490
491 if (is_my_oldorig) {
492 bat_dbg(DBG_BATMAN, bat_priv,
493 "Drop packet: ignoring all rebroadcast echos (sender: "
494 "%pM)\n", ethhdr->h_source);
495 return;
496 }
497
498 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
499 if (!orig_node)
500 return;
501
502 is_duplicate = bat_ogm_update_seqnos(ethhdr, batman_ogm_packet,
503 if_incoming);
504
505 if (is_duplicate == -1) {
506 bat_dbg(DBG_BATMAN, bat_priv,
507 "Drop packet: packet within seqno protection time "
508 "(sender: %pM)\n", ethhdr->h_source);
509 goto out;
510 }
511
512 if (batman_ogm_packet->tq == 0) {
513 bat_dbg(DBG_BATMAN, bat_priv,
514 "Drop packet: originator packet with tq equal 0\n");
515 goto out;
516 }
517
518 router = orig_node_get_router(orig_node);
519 if (router)
520 router_router = orig_node_get_router(router->orig_node);
521
522 /* avoid temporary routing loops */
523 if (router && router_router &&
524 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
525 !(compare_eth(batman_ogm_packet->orig,
526 batman_ogm_packet->prev_sender)) &&
527 (compare_eth(router->addr, router_router->addr))) {
528 bat_dbg(DBG_BATMAN, bat_priv,
529 "Drop packet: ignoring all rebroadcast packets that "
530 "may make me loop (sender: %pM)\n", ethhdr->h_source);
531 goto out;
532 }
533
534 /* if sender is a direct neighbor the sender mac equals
535 * originator mac */
536 orig_neigh_node = (is_single_hop_neigh ?
537 orig_node :
538 get_orig_node(bat_priv, ethhdr->h_source));
539 if (!orig_neigh_node)
540 goto out;
541
542 orig_neigh_router = orig_node_get_router(orig_neigh_node);
543
544 /* drop packet if sender is not a direct neighbor and if we
545 * don't route towards it */
546 if (!is_single_hop_neigh && (!orig_neigh_router)) {
547 bat_dbg(DBG_BATMAN, bat_priv,
548 "Drop packet: OGM via unknown neighbor!\n");
549 goto out_neigh;
550 }
551
552 is_bidirectional = bat_ogm_calc_tq(orig_node, orig_neigh_node,
553 batman_ogm_packet, if_incoming);
554
555 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
556
557 /* update ranking if it is not a duplicate or has the same
558 * seqno and similar ttl as the non-duplicate */
559 if (is_bidirectional &&
560 (!is_duplicate ||
561 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
562 (orig_node->last_ttl - 3 <= batman_ogm_packet->ttl))))
563 bat_ogm_orig_update(bat_priv, orig_node, ethhdr,
564 batman_ogm_packet, if_incoming,
565 tt_buff, is_duplicate);
566
567 /* is single hop (direct) neighbor */
568 if (is_single_hop_neigh) {
569
570 /* mark direct link on incoming interface */
571 schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet,
572 1, if_incoming);
573
574 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
575 "rebroadcast neighbor packet with direct link flag\n");
576 goto out_neigh;
577 }
578
579 /* multihop originator */
580 if (!is_bidirectional) {
581 bat_dbg(DBG_BATMAN, bat_priv,
582 "Drop packet: not received via bidirectional link\n");
583 goto out_neigh;
584 }
585
586 if (is_duplicate) {
587 bat_dbg(DBG_BATMAN, bat_priv,
588 "Drop packet: duplicate packet received\n");
589 goto out_neigh;
590 }
591
592 bat_dbg(DBG_BATMAN, bat_priv,
593 "Forwarding packet: rebroadcast originator packet\n");
594 schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet,
595 0, if_incoming);
596
597out_neigh:
598 if ((orig_neigh_node) && (!is_single_hop_neigh))
599 orig_node_free_ref(orig_neigh_node);
600out:
601 if (router)
602 neigh_node_free_ref(router);
603 if (router_router)
604 neigh_node_free_ref(router_router);
605 if (orig_neigh_router)
606 neigh_node_free_ref(orig_neigh_router);
607
608 orig_node_free_ref(orig_node);
609}
610
611void bat_ogm_receive(const struct ethhdr *ethhdr, unsigned char *packet_buff,
612 int packet_len, struct hard_iface *if_incoming)
613{
614 struct batman_ogm_packet *batman_ogm_packet;
615 int buff_pos = 0;
616 unsigned char *tt_buff;
617
618 batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
619
620 /* unpack the aggregated packets and process them one by one */
621 do {
622 /* network to host order for our 32bit seqno and the
623 orig_interval */
624 batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
625 batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
626
627 tt_buff = packet_buff + buff_pos + BATMAN_OGM_LEN;
628
629 bat_ogm_process(ethhdr, batman_ogm_packet,
630 tt_buff, if_incoming);
631
632 buff_pos += BATMAN_OGM_LEN +
633 tt_len(batman_ogm_packet->tt_num_changes);
634
635 batman_ogm_packet = (struct batman_ogm_packet *)
636 (packet_buff + buff_pos);
637 } while (bat_ogm_aggr_packet(buff_pos, packet_len,
638 batman_ogm_packet->tt_num_changes));
639}
diff --git a/net/batman-adv/bat_ogm.h b/net/batman-adv/bat_ogm.h
new file mode 100644
index 000000000000..bdd3d1eb4c20
--- /dev/null
+++ b/net/batman-adv/bat_ogm.h
@@ -0,0 +1,30 @@
1/*
2 * Copyright (C) 2007-2011 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
22#ifndef _NET_BATMAN_ADV_OGM_H_
23#define _NET_BATMAN_ADV_OGM_H_
24
25#include "main.h"
26
27void bat_ogm_receive(const struct ethhdr *ethhdr, unsigned char *packet_buff,
28 int packet_len, struct hard_iface *if_incoming);
29
30#endif /* _NET_BATMAN_ADV_OGM_H_ */
diff --git a/net/batman-adv/hard-interface.c b/net/batman-adv/hard-interface.c
index cf9f4afafdfa..cfee0178ec62 100644
--- a/net/batman-adv/hard-interface.c
+++ b/net/batman-adv/hard-interface.c
@@ -632,7 +632,7 @@ static int batman_skb_recv(struct sk_buff *skb, struct net_device *dev,
632 switch (batman_ogm_packet->packet_type) { 632 switch (batman_ogm_packet->packet_type) {
633 /* batman originator packet */ 633 /* batman originator packet */
634 case BAT_OGM: 634 case BAT_OGM:
635 ret = recv_bat_packet(skb, hard_iface); 635 ret = recv_bat_ogm_packet(skb, hard_iface);
636 break; 636 break;
637 637
638 /* batman icmp packet */ 638 /* batman icmp packet */
diff --git a/net/batman-adv/originator.c b/net/batman-adv/originator.c
index d448018e514f..cd7d2566ff5f 100644
--- a/net/batman-adv/originator.c
+++ b/net/batman-adv/originator.c
@@ -336,8 +336,7 @@ static bool purge_orig_node(struct bat_priv *bat_priv,
336 } else { 336 } else {
337 if (purge_orig_neighbors(bat_priv, orig_node, 337 if (purge_orig_neighbors(bat_priv, orig_node,
338 &best_neigh_node)) { 338 &best_neigh_node)) {
339 update_routes(bat_priv, orig_node, 339 update_route(bat_priv, orig_node, best_neigh_node);
340 best_neigh_node);
341 } 340 }
342 } 341 }
343 342
diff --git a/net/batman-adv/routing.c b/net/batman-adv/routing.c
index 6efd1d0da54a..f961cc5eade5 100644
--- a/net/batman-adv/routing.c
+++ b/net/batman-adv/routing.c
@@ -22,18 +22,14 @@
22#include "main.h" 22#include "main.h"
23#include "routing.h" 23#include "routing.h"
24#include "send.h" 24#include "send.h"
25#include "hash.h"
26#include "soft-interface.h" 25#include "soft-interface.h"
27#include "hard-interface.h" 26#include "hard-interface.h"
28#include "icmp_socket.h" 27#include "icmp_socket.h"
29#include "translation-table.h" 28#include "translation-table.h"
30#include "originator.h" 29#include "originator.h"
31#include "ring_buffer.h"
32#include "vis.h" 30#include "vis.h"
33#include "aggregation.h"
34#include "gateway_common.h"
35#include "gateway_client.h"
36#include "unicast.h" 31#include "unicast.h"
32#include "bat_ogm.h"
37 33
38void slide_own_bcast_window(struct hard_iface *hard_iface) 34void slide_own_bcast_window(struct hard_iface *hard_iface)
39{ 35{
@@ -64,9 +60,9 @@ void slide_own_bcast_window(struct hard_iface *hard_iface)
64 } 60 }
65} 61}
66 62
67static void update_route(struct bat_priv *bat_priv, 63static void _update_route(struct bat_priv *bat_priv,
68 struct orig_node *orig_node, 64 struct orig_node *orig_node,
69 struct neigh_node *neigh_node) 65 struct neigh_node *neigh_node)
70{ 66{
71 struct neigh_node *curr_router; 67 struct neigh_node *curr_router;
72 68
@@ -110,8 +106,8 @@ static void update_route(struct bat_priv *bat_priv,
110 neigh_node_free_ref(curr_router); 106 neigh_node_free_ref(curr_router);
111} 107}
112 108
113void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node, 109void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
114 struct neigh_node *neigh_node) 110 struct neigh_node *neigh_node)
115{ 111{
116 struct neigh_node *router = NULL; 112 struct neigh_node *router = NULL;
117 113
@@ -121,117 +117,13 @@ void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
121 router = orig_node_get_router(orig_node); 117 router = orig_node_get_router(orig_node);
122 118
123 if (router != neigh_node) 119 if (router != neigh_node)
124 update_route(bat_priv, orig_node, neigh_node); 120 _update_route(bat_priv, orig_node, neigh_node);
125 121
126out: 122out:
127 if (router) 123 if (router)
128 neigh_node_free_ref(router); 124 neigh_node_free_ref(router);
129} 125}
130 126
131static int is_bidirectional_neigh(struct orig_node *orig_node,
132 struct orig_node *orig_neigh_node,
133 struct batman_ogm_packet *batman_ogm_packet,
134 struct hard_iface *if_incoming)
135{
136 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
137 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
138 struct hlist_node *node;
139 uint8_t total_count;
140 uint8_t orig_eq_count, neigh_rq_count, tq_own;
141 int tq_asym_penalty, ret = 0;
142
143 /* find corresponding one hop neighbor */
144 rcu_read_lock();
145 hlist_for_each_entry_rcu(tmp_neigh_node, node,
146 &orig_neigh_node->neigh_list, list) {
147
148 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
149 continue;
150
151 if (tmp_neigh_node->if_incoming != if_incoming)
152 continue;
153
154 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
155 continue;
156
157 neigh_node = tmp_neigh_node;
158 break;
159 }
160 rcu_read_unlock();
161
162 if (!neigh_node)
163 neigh_node = create_neighbor(orig_neigh_node,
164 orig_neigh_node,
165 orig_neigh_node->orig,
166 if_incoming);
167
168 if (!neigh_node)
169 goto out;
170
171 /* if orig_node is direct neighbor update neigh_node last_valid */
172 if (orig_node == orig_neigh_node)
173 neigh_node->last_valid = jiffies;
174
175 orig_node->last_valid = jiffies;
176
177 /* find packet count of corresponding one hop neighbor */
178 spin_lock_bh(&orig_node->ogm_cnt_lock);
179 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
180 neigh_rq_count = neigh_node->real_packet_count;
181 spin_unlock_bh(&orig_node->ogm_cnt_lock);
182
183 /* pay attention to not get a value bigger than 100 % */
184 total_count = (orig_eq_count > neigh_rq_count ?
185 neigh_rq_count : orig_eq_count);
186
187 /* if we have too few packets (too less data) we set tq_own to zero */
188 /* if we receive too few packets it is not considered bidirectional */
189 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
190 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
191 tq_own = 0;
192 else
193 /* neigh_node->real_packet_count is never zero as we
194 * only purge old information when getting new
195 * information */
196 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
197
198 /*
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
203 */
204 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
205 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
206 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
207 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
208 (TQ_LOCAL_WINDOW_SIZE *
209 TQ_LOCAL_WINDOW_SIZE *
210 TQ_LOCAL_WINDOW_SIZE);
211
212 batman_ogm_packet->tq = ((batman_ogm_packet->tq * tq_own
213 * tq_asym_penalty) /
214 (TQ_MAX_VALUE * TQ_MAX_VALUE));
215
216 bat_dbg(DBG_BATMAN, bat_priv,
217 "bidirectional: "
218 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
219 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
220 "total tq: %3i\n",
221 orig_node->orig, orig_neigh_node->orig, total_count,
222 neigh_rq_count, tq_own, tq_asym_penalty, batman_ogm_packet->tq);
223
224 /* if link has the minimum required transmission quality
225 * consider it bidirectional */
226 if (batman_ogm_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
227 ret = 1;
228
229out:
230 if (neigh_node)
231 neigh_node_free_ref(neigh_node);
232 return ret;
233}
234
235/* caller must hold the neigh_list_lock */ 127/* caller must hold the neigh_list_lock */
236void bonding_candidate_del(struct orig_node *orig_node, 128void bonding_candidate_del(struct orig_node *orig_node,
237 struct neigh_node *neigh_node) 129 struct neigh_node *neigh_node)
@@ -249,8 +141,8 @@ out:
249 return; 141 return;
250} 142}
251 143
252static void bonding_candidate_add(struct orig_node *orig_node, 144void bonding_candidate_add(struct orig_node *orig_node,
253 struct neigh_node *neigh_node) 145 struct neigh_node *neigh_node)
254{ 146{
255 struct hlist_node *node; 147 struct hlist_node *node;
256 struct neigh_node *tmp_neigh_node, *router = NULL; 148 struct neigh_node *tmp_neigh_node, *router = NULL;
@@ -320,10 +212,9 @@ out:
320} 212}
321 213
322/* copy primary address for bonding */ 214/* copy primary address for bonding */
323static void bonding_save_primary(const struct orig_node *orig_node, 215void bonding_save_primary(const struct orig_node *orig_node,
324 struct orig_node *orig_neigh_node, 216 struct orig_node *orig_neigh_node,
325 const struct batman_ogm_packet 217 const struct batman_ogm_packet *batman_ogm_packet)
326 *batman_ogm_packet)
327{ 218{
328 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) 219 if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
329 return; 220 return;
@@ -331,153 +222,13 @@ static void bonding_save_primary(const struct orig_node *orig_node,
331 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN); 222 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
332} 223}
333 224
334static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
335 const struct ethhdr *ethhdr,
336 const struct batman_ogm_packet *batman_ogm_packet,
337 struct hard_iface *if_incoming,
338 const unsigned char *tt_buff, int is_duplicate)
339{
340 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
341 struct neigh_node *router = NULL;
342 struct orig_node *orig_node_tmp;
343 struct hlist_node *node;
344 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
345
346 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
347 "Searching and updating originator entry of received packet\n");
348
349 rcu_read_lock();
350 hlist_for_each_entry_rcu(tmp_neigh_node, node,
351 &orig_node->neigh_list, list) {
352 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
353 (tmp_neigh_node->if_incoming == if_incoming) &&
354 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
355 if (neigh_node)
356 neigh_node_free_ref(neigh_node);
357 neigh_node = tmp_neigh_node;
358 continue;
359 }
360
361 if (is_duplicate)
362 continue;
363
364 spin_lock_bh(&tmp_neigh_node->tq_lock);
365 ring_buffer_set(tmp_neigh_node->tq_recv,
366 &tmp_neigh_node->tq_index, 0);
367 tmp_neigh_node->tq_avg =
368 ring_buffer_avg(tmp_neigh_node->tq_recv);
369 spin_unlock_bh(&tmp_neigh_node->tq_lock);
370 }
371
372 if (!neigh_node) {
373 struct orig_node *orig_tmp;
374
375 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
376 if (!orig_tmp)
377 goto unlock;
378
379 neigh_node = create_neighbor(orig_node, orig_tmp,
380 ethhdr->h_source, if_incoming);
381
382 orig_node_free_ref(orig_tmp);
383 if (!neigh_node)
384 goto unlock;
385 } else
386 bat_dbg(DBG_BATMAN, bat_priv,
387 "Updating existing last-hop neighbor of originator\n");
388
389 rcu_read_unlock();
390
391 orig_node->flags = batman_ogm_packet->flags;
392 neigh_node->last_valid = jiffies;
393
394 spin_lock_bh(&neigh_node->tq_lock);
395 ring_buffer_set(neigh_node->tq_recv,
396 &neigh_node->tq_index,
397 batman_ogm_packet->tq);
398 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
399 spin_unlock_bh(&neigh_node->tq_lock);
400
401 if (!is_duplicate) {
402 orig_node->last_ttl = batman_ogm_packet->ttl;
403 neigh_node->last_ttl = batman_ogm_packet->ttl;
404 }
405
406 bonding_candidate_add(orig_node, neigh_node);
407
408 /* if this neighbor already is our next hop there is nothing
409 * to change */
410 router = orig_node_get_router(orig_node);
411 if (router == neigh_node)
412 goto update_tt;
413
414 /* if this neighbor does not offer a better TQ we won't consider it */
415 if (router && (router->tq_avg > neigh_node->tq_avg))
416 goto update_tt;
417
418 /* if the TQ is the same and the link not more symmetric we
419 * won't consider it either */
420 if (router && (neigh_node->tq_avg == router->tq_avg)) {
421 orig_node_tmp = router->orig_node;
422 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
423 bcast_own_sum_orig =
424 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
425 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
426
427 orig_node_tmp = neigh_node->orig_node;
428 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
429 bcast_own_sum_neigh =
430 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
431 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
432
433 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
434 goto update_tt;
435 }
436
437 update_routes(bat_priv, orig_node, neigh_node);
438
439update_tt:
440 /* I have to check for transtable changes only if the OGM has been
441 * sent through a primary interface */
442 if (((batman_ogm_packet->orig != ethhdr->h_source) &&
443 (batman_ogm_packet->ttl > 2)) ||
444 (batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
445 tt_update_orig(bat_priv, orig_node, tt_buff,
446 batman_ogm_packet->tt_num_changes,
447 batman_ogm_packet->ttvn,
448 batman_ogm_packet->tt_crc);
449
450 if (orig_node->gw_flags != batman_ogm_packet->gw_flags)
451 gw_node_update(bat_priv, orig_node,
452 batman_ogm_packet->gw_flags);
453
454 orig_node->gw_flags = batman_ogm_packet->gw_flags;
455
456 /* restart gateway selection if fast or late switching was enabled */
457 if ((orig_node->gw_flags) &&
458 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
459 (atomic_read(&bat_priv->gw_sel_class) > 2))
460 gw_check_election(bat_priv, orig_node);
461
462 goto out;
463
464unlock:
465 rcu_read_unlock();
466out:
467 if (neigh_node)
468 neigh_node_free_ref(neigh_node);
469 if (router)
470 neigh_node_free_ref(router);
471}
472
473/* checks whether the host restarted and is in the protection time. 225/* checks whether the host restarted and is in the protection time.
474 * returns: 226 * returns:
475 * 0 if the packet is to be accepted 227 * 0 if the packet is to be accepted
476 * 1 if the packet is to be ignored. 228 * 1 if the packet is to be ignored.
477 */ 229 */
478static int window_protected(struct bat_priv *bat_priv, 230int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
479 int32_t seq_num_diff, 231 unsigned long *last_reset)
480 unsigned long *last_reset)
481{ 232{
482 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) 233 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
483 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) { 234 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
@@ -495,329 +246,7 @@ static int window_protected(struct bat_priv *bat_priv,
495 return 0; 246 return 0;
496} 247}
497 248
498/* processes a batman packet for all interfaces, adjusts the sequence number and 249int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
499 * finds out whether it is a duplicate.
500 * returns:
501 * 1 the packet is a duplicate
502 * 0 the packet has not yet been received
503 * -1 the packet is old and has been received while the seqno window
504 * was protected. Caller should drop it.
505 */
506static int count_real_packets(const struct ethhdr *ethhdr,
507 const struct batman_ogm_packet *batman_ogm_packet,
508 const struct hard_iface *if_incoming)
509{
510 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
511 struct orig_node *orig_node;
512 struct neigh_node *tmp_neigh_node;
513 struct hlist_node *node;
514 int is_duplicate = 0;
515 int32_t seq_diff;
516 int need_update = 0;
517 int set_mark, ret = -1;
518
519 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
520 if (!orig_node)
521 return 0;
522
523 spin_lock_bh(&orig_node->ogm_cnt_lock);
524 seq_diff = batman_ogm_packet->seqno - orig_node->last_real_seqno;
525
526 /* signalize caller that the packet is to be dropped. */
527 if (window_protected(bat_priv, seq_diff,
528 &orig_node->batman_seqno_reset))
529 goto out;
530
531 rcu_read_lock();
532 hlist_for_each_entry_rcu(tmp_neigh_node, node,
533 &orig_node->neigh_list, list) {
534
535 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
536 orig_node->last_real_seqno,
537 batman_ogm_packet->seqno);
538
539 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
540 (tmp_neigh_node->if_incoming == if_incoming))
541 set_mark = 1;
542 else
543 set_mark = 0;
544
545 /* if the window moved, set the update flag. */
546 need_update |= bit_get_packet(bat_priv,
547 tmp_neigh_node->real_bits,
548 seq_diff, set_mark);
549
550 tmp_neigh_node->real_packet_count =
551 bit_packet_count(tmp_neigh_node->real_bits);
552 }
553 rcu_read_unlock();
554
555 if (need_update) {
556 bat_dbg(DBG_BATMAN, bat_priv,
557 "updating last_seqno: old %d, new %d\n",
558 orig_node->last_real_seqno, batman_ogm_packet->seqno);
559 orig_node->last_real_seqno = batman_ogm_packet->seqno;
560 }
561
562 ret = is_duplicate;
563
564out:
565 spin_unlock_bh(&orig_node->ogm_cnt_lock);
566 orig_node_free_ref(orig_node);
567 return ret;
568}
569
570void receive_bat_packet(const struct ethhdr *ethhdr,
571 struct batman_ogm_packet *batman_ogm_packet,
572 const unsigned char *tt_buff,
573 struct hard_iface *if_incoming)
574{
575 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
576 struct hard_iface *hard_iface;
577 struct orig_node *orig_neigh_node, *orig_node;
578 struct neigh_node *router = NULL, *router_router = NULL;
579 struct neigh_node *orig_neigh_router = NULL;
580 int has_directlink_flag;
581 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
582 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
583 int is_duplicate;
584 uint32_t if_incoming_seqno;
585
586 /* Silently drop when the batman packet is actually not a
587 * correct packet.
588 *
589 * This might happen if a packet is padded (e.g. Ethernet has a
590 * minimum frame length of 64 byte) and the aggregation interprets
591 * it as an additional length.
592 *
593 * TODO: A more sane solution would be to have a bit in the
594 * batman_ogm_packet to detect whether the packet is the last
595 * packet in an aggregation. Here we expect that the padding
596 * is always zero (or not 0x01)
597 */
598 if (batman_ogm_packet->packet_type != BAT_OGM)
599 return;
600
601 /* could be changed by schedule_own_packet() */
602 if_incoming_seqno = atomic_read(&if_incoming->seqno);
603
604 has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
605
606 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
607 batman_ogm_packet->orig) ? 1 : 0);
608
609 bat_dbg(DBG_BATMAN, bat_priv,
610 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
611 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
612 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
613 ethhdr->h_source, if_incoming->net_dev->name,
614 if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
615 batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
616 batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
617 batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
618 batman_ogm_packet->ttl, batman_ogm_packet->version,
619 has_directlink_flag);
620
621 rcu_read_lock();
622 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
623 if (hard_iface->if_status != IF_ACTIVE)
624 continue;
625
626 if (hard_iface->soft_iface != if_incoming->soft_iface)
627 continue;
628
629 if (compare_eth(ethhdr->h_source,
630 hard_iface->net_dev->dev_addr))
631 is_my_addr = 1;
632
633 if (compare_eth(batman_ogm_packet->orig,
634 hard_iface->net_dev->dev_addr))
635 is_my_orig = 1;
636
637 if (compare_eth(batman_ogm_packet->prev_sender,
638 hard_iface->net_dev->dev_addr))
639 is_my_oldorig = 1;
640
641 if (is_broadcast_ether_addr(ethhdr->h_source))
642 is_broadcast = 1;
643 }
644 rcu_read_unlock();
645
646 if (batman_ogm_packet->version != COMPAT_VERSION) {
647 bat_dbg(DBG_BATMAN, bat_priv,
648 "Drop packet: incompatible batman version (%i)\n",
649 batman_ogm_packet->version);
650 return;
651 }
652
653 if (is_my_addr) {
654 bat_dbg(DBG_BATMAN, bat_priv,
655 "Drop packet: received my own broadcast (sender: %pM"
656 ")\n",
657 ethhdr->h_source);
658 return;
659 }
660
661 if (is_broadcast) {
662 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
663 "ignoring all packets with broadcast source addr (sender: %pM"
664 ")\n", ethhdr->h_source);
665 return;
666 }
667
668 if (is_my_orig) {
669 unsigned long *word;
670 int offset;
671
672 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
673 if (!orig_neigh_node)
674 return;
675
676 /* neighbor has to indicate direct link and it has to
677 * come via the corresponding interface */
678 /* save packet seqno for bidirectional check */
679 if (has_directlink_flag &&
680 compare_eth(if_incoming->net_dev->dev_addr,
681 batman_ogm_packet->orig)) {
682 offset = if_incoming->if_num * NUM_WORDS;
683
684 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
685 word = &(orig_neigh_node->bcast_own[offset]);
686 bit_mark(word,
687 if_incoming_seqno -
688 batman_ogm_packet->seqno - 2);
689 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
690 bit_packet_count(word);
691 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
692 }
693
694 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
695 "originator packet from myself (via neighbor)\n");
696 orig_node_free_ref(orig_neigh_node);
697 return;
698 }
699
700 if (is_my_oldorig) {
701 bat_dbg(DBG_BATMAN, bat_priv,
702 "Drop packet: ignoring all rebroadcast echos (sender: "
703 "%pM)\n", ethhdr->h_source);
704 return;
705 }
706
707 orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
708 if (!orig_node)
709 return;
710
711 is_duplicate = count_real_packets(ethhdr, batman_ogm_packet,
712 if_incoming);
713
714 if (is_duplicate == -1) {
715 bat_dbg(DBG_BATMAN, bat_priv,
716 "Drop packet: packet within seqno protection time "
717 "(sender: %pM)\n", ethhdr->h_source);
718 goto out;
719 }
720
721 if (batman_ogm_packet->tq == 0) {
722 bat_dbg(DBG_BATMAN, bat_priv,
723 "Drop packet: originator packet with tq equal 0\n");
724 goto out;
725 }
726
727 router = orig_node_get_router(orig_node);
728 if (router)
729 router_router = orig_node_get_router(router->orig_node);
730
731 /* avoid temporary routing loops */
732 if (router && router_router &&
733 (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
734 !(compare_eth(batman_ogm_packet->orig,
735 batman_ogm_packet->prev_sender)) &&
736 (compare_eth(router->addr, router_router->addr))) {
737 bat_dbg(DBG_BATMAN, bat_priv,
738 "Drop packet: ignoring all rebroadcast packets that "
739 "may make me loop (sender: %pM)\n", ethhdr->h_source);
740 goto out;
741 }
742
743 /* if sender is a direct neighbor the sender mac equals
744 * originator mac */
745 orig_neigh_node = (is_single_hop_neigh ?
746 orig_node :
747 get_orig_node(bat_priv, ethhdr->h_source));
748 if (!orig_neigh_node)
749 goto out;
750
751 orig_neigh_router = orig_node_get_router(orig_neigh_node);
752
753 /* drop packet if sender is not a direct neighbor and if we
754 * don't route towards it */
755 if (!is_single_hop_neigh && (!orig_neigh_router)) {
756 bat_dbg(DBG_BATMAN, bat_priv,
757 "Drop packet: OGM via unknown neighbor!\n");
758 goto out_neigh;
759 }
760
761 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
762 batman_ogm_packet,
763 if_incoming);
764
765 bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
766
767 /* update ranking if it is not a duplicate or has the same
768 * seqno and similar ttl as the non-duplicate */
769 if (is_bidirectional &&
770 (!is_duplicate ||
771 ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
772 (orig_node->last_ttl - 3 <= batman_ogm_packet->ttl))))
773 update_orig(bat_priv, orig_node, ethhdr, batman_ogm_packet,
774 if_incoming, tt_buff, is_duplicate);
775
776 /* is single hop (direct) neighbor */
777 if (is_single_hop_neigh) {
778
779 /* mark direct link on incoming interface */
780 schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet,
781 1, if_incoming);
782
783 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
784 "rebroadcast neighbor packet with direct link flag\n");
785 goto out_neigh;
786 }
787
788 /* multihop originator */
789 if (!is_bidirectional) {
790 bat_dbg(DBG_BATMAN, bat_priv,
791 "Drop packet: not received via bidirectional link\n");
792 goto out_neigh;
793 }
794
795 if (is_duplicate) {
796 bat_dbg(DBG_BATMAN, bat_priv,
797 "Drop packet: duplicate packet received\n");
798 goto out_neigh;
799 }
800
801 bat_dbg(DBG_BATMAN, bat_priv,
802 "Forwarding packet: rebroadcast originator packet\n");
803 schedule_forward_packet(orig_node, ethhdr, batman_ogm_packet,
804 0, if_incoming);
805
806out_neigh:
807 if ((orig_neigh_node) && (!is_single_hop_neigh))
808 orig_node_free_ref(orig_neigh_node);
809out:
810 if (router)
811 neigh_node_free_ref(router);
812 if (router_router)
813 neigh_node_free_ref(router_router);
814 if (orig_neigh_router)
815 neigh_node_free_ref(orig_neigh_router);
816
817 orig_node_free_ref(orig_node);
818}
819
820int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
821{ 250{
822 struct ethhdr *ethhdr; 251 struct ethhdr *ethhdr;
823 252
@@ -845,10 +274,7 @@ int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
845 274
846 ethhdr = (struct ethhdr *)skb_mac_header(skb); 275 ethhdr = (struct ethhdr *)skb_mac_header(skb);
847 276
848 receive_aggr_bat_packet(ethhdr, 277 bat_ogm_receive(ethhdr, skb->data, skb_headlen(skb), hard_iface);
849 skb->data,
850 skb_headlen(skb),
851 hard_iface);
852 278
853 kfree_skb(skb); 279 kfree_skb(skb);
854 return NET_RX_SUCCESS; 280 return NET_RX_SUCCESS;
diff --git a/net/batman-adv/routing.h b/net/batman-adv/routing.h
index 893db7f6007b..7aaee0fb0fdc 100644
--- a/net/batman-adv/routing.h
+++ b/net/batman-adv/routing.h
@@ -23,19 +23,15 @@
23#define _NET_BATMAN_ADV_ROUTING_H_ 23#define _NET_BATMAN_ADV_ROUTING_H_
24 24
25void slide_own_bcast_window(struct hard_iface *hard_iface); 25void slide_own_bcast_window(struct hard_iface *hard_iface);
26void receive_bat_packet(const struct ethhdr *ethhdr, 26void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
27 struct batman_ogm_packet *batman_ogm_packet, 27 struct neigh_node *neigh_node);
28 const unsigned char *tt_buff,
29 struct hard_iface *if_incoming);
30void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
31 struct neigh_node *neigh_node);
32int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if); 28int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if);
33int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if); 29int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if);
34int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if); 30int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if);
35int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if); 31int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if);
36int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if); 32int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if);
37int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if); 33int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if);
38int recv_bat_packet(struct sk_buff *skb, struct hard_iface *recv_if); 34int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *recv_if);
39int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if); 35int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if);
40int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if); 36int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if);
41struct neigh_node *find_router(struct bat_priv *bat_priv, 37struct neigh_node *find_router(struct bat_priv *bat_priv,
@@ -43,5 +39,12 @@ struct neigh_node *find_router(struct bat_priv *bat_priv,
43 const struct hard_iface *recv_if); 39 const struct hard_iface *recv_if);
44void bonding_candidate_del(struct orig_node *orig_node, 40void bonding_candidate_del(struct orig_node *orig_node,
45 struct neigh_node *neigh_node); 41 struct neigh_node *neigh_node);
42void bonding_candidate_add(struct orig_node *orig_node,
43 struct neigh_node *neigh_node);
44void bonding_save_primary(const struct orig_node *orig_node,
45 struct orig_node *orig_neigh_node,
46 const struct batman_ogm_packet *batman_ogm_packet);
47int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
48 unsigned long *last_reset);
46 49
47#endif /* _NET_BATMAN_ADV_ROUTING_H_ */ 50#endif /* _NET_BATMAN_ADV_ROUTING_H_ */