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