Commit | Line | Data |
---|---|---|
c6c8fea2 SE |
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 */ | |
74ef1153 | 436 | static void mark_bonding_address(struct orig_node *orig_node, |
c6c8fea2 SE |
437 | struct orig_node *orig_neigh_node, |
438 | struct batman_packet *batman_packet) | |
439 | ||
440 | { | |
441 | if (batman_packet->flags & PRIMARIES_FIRST_HOP) | |
442 | memcpy(orig_neigh_node->primary_addr, | |
443 | orig_node->orig, ETH_ALEN); | |
444 | ||
445 | return; | |
446 | } | |
447 | ||
448 | /* mark possible bond.candidates in the neighbor list */ | |
74ef1153 | 449 | void update_bonding_candidates(struct orig_node *orig_node) |
c6c8fea2 SE |
450 | { |
451 | int candidates; | |
452 | int interference_candidate; | |
453 | int best_tq; | |
454 | struct neigh_node *tmp_neigh_node, *tmp_neigh_node2; | |
455 | struct neigh_node *first_candidate, *last_candidate; | |
456 | ||
457 | /* update the candidates for this originator */ | |
458 | if (!orig_node->router) { | |
459 | orig_node->bond.candidates = 0; | |
460 | return; | |
461 | } | |
462 | ||
463 | best_tq = orig_node->router->tq_avg; | |
464 | ||
465 | /* update bond.candidates */ | |
466 | ||
467 | candidates = 0; | |
468 | ||
469 | /* mark other nodes which also received "PRIMARIES FIRST HOP" packets | |
470 | * as "bonding partner" */ | |
471 | ||
472 | /* first, zero the list */ | |
473 | list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) { | |
474 | tmp_neigh_node->next_bond_candidate = NULL; | |
475 | } | |
476 | ||
477 | first_candidate = NULL; | |
478 | last_candidate = NULL; | |
479 | list_for_each_entry(tmp_neigh_node, &orig_node->neigh_list, list) { | |
480 | ||
481 | /* only consider if it has the same primary address ... */ | |
482 | if (memcmp(orig_node->orig, | |
483 | tmp_neigh_node->orig_node->primary_addr, | |
484 | ETH_ALEN) != 0) | |
485 | continue; | |
486 | ||
487 | /* ... and is good enough to be considered */ | |
488 | if (tmp_neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD) | |
489 | continue; | |
490 | ||
491 | /* check if we have another candidate with the same | |
492 | * mac address or interface. If we do, we won't | |
493 | * select this candidate because of possible interference. */ | |
494 | ||
495 | interference_candidate = 0; | |
496 | list_for_each_entry(tmp_neigh_node2, | |
497 | &orig_node->neigh_list, list) { | |
498 | ||
499 | if (tmp_neigh_node2 == tmp_neigh_node) | |
500 | continue; | |
501 | ||
502 | /* we only care if the other candidate is even | |
503 | * considered as candidate. */ | |
504 | if (!tmp_neigh_node2->next_bond_candidate) | |
505 | continue; | |
506 | ||
507 | ||
508 | if ((tmp_neigh_node->if_incoming == | |
509 | tmp_neigh_node2->if_incoming) | |
510 | || (memcmp(tmp_neigh_node->addr, | |
511 | tmp_neigh_node2->addr, ETH_ALEN) == 0)) { | |
512 | ||
513 | interference_candidate = 1; | |
514 | break; | |
515 | } | |
516 | } | |
517 | /* don't care further if it is an interference candidate */ | |
518 | if (interference_candidate) | |
519 | continue; | |
520 | ||
521 | if (!first_candidate) { | |
522 | first_candidate = tmp_neigh_node; | |
523 | tmp_neigh_node->next_bond_candidate = first_candidate; | |
524 | } else | |
525 | tmp_neigh_node->next_bond_candidate = last_candidate; | |
526 | ||
527 | last_candidate = tmp_neigh_node; | |
528 | ||
529 | candidates++; | |
530 | } | |
531 | ||
532 | if (candidates > 0) { | |
533 | first_candidate->next_bond_candidate = last_candidate; | |
534 | orig_node->bond.selected = first_candidate; | |
535 | } | |
536 | ||
537 | orig_node->bond.candidates = candidates; | |
538 | } | |
539 | ||
540 | void receive_bat_packet(struct ethhdr *ethhdr, | |
541 | struct batman_packet *batman_packet, | |
542 | unsigned char *hna_buff, int hna_buff_len, | |
543 | struct batman_if *if_incoming) | |
544 | { | |
545 | struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface); | |
546 | struct batman_if *batman_if; | |
547 | struct orig_node *orig_neigh_node, *orig_node; | |
548 | char has_directlink_flag; | |
549 | char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0; | |
550 | char is_broadcast = 0, is_bidirectional, is_single_hop_neigh; | |
551 | char is_duplicate; | |
552 | uint32_t if_incoming_seqno; | |
553 | ||
554 | /* Silently drop when the batman packet is actually not a | |
555 | * correct packet. | |
556 | * | |
557 | * This might happen if a packet is padded (e.g. Ethernet has a | |
558 | * minimum frame length of 64 byte) and the aggregation interprets | |
559 | * it as an additional length. | |
560 | * | |
561 | * TODO: A more sane solution would be to have a bit in the | |
562 | * batman_packet to detect whether the packet is the last | |
563 | * packet in an aggregation. Here we expect that the padding | |
564 | * is always zero (or not 0x01) | |
565 | */ | |
566 | if (batman_packet->packet_type != BAT_PACKET) | |
567 | return; | |
568 | ||
569 | /* could be changed by schedule_own_packet() */ | |
570 | if_incoming_seqno = atomic_read(&if_incoming->seqno); | |
571 | ||
572 | has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0); | |
573 | ||
574 | is_single_hop_neigh = (compare_orig(ethhdr->h_source, | |
575 | batman_packet->orig) ? 1 : 0); | |
576 | ||
577 | bat_dbg(DBG_BATMAN, bat_priv, | |
578 | "Received BATMAN packet via NB: %pM, IF: %s [%pM] " | |
579 | "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, " | |
580 | "TTL %d, V %d, IDF %d)\n", | |
581 | ethhdr->h_source, if_incoming->net_dev->name, | |
582 | if_incoming->net_dev->dev_addr, batman_packet->orig, | |
583 | batman_packet->prev_sender, batman_packet->seqno, | |
584 | batman_packet->tq, batman_packet->ttl, batman_packet->version, | |
585 | has_directlink_flag); | |
586 | ||
587 | rcu_read_lock(); | |
588 | list_for_each_entry_rcu(batman_if, &if_list, list) { | |
589 | if (batman_if->if_status != IF_ACTIVE) | |
590 | continue; | |
591 | ||
592 | if (batman_if->soft_iface != if_incoming->soft_iface) | |
593 | continue; | |
594 | ||
595 | if (compare_orig(ethhdr->h_source, | |
596 | batman_if->net_dev->dev_addr)) | |
597 | is_my_addr = 1; | |
598 | ||
599 | if (compare_orig(batman_packet->orig, | |
600 | batman_if->net_dev->dev_addr)) | |
601 | is_my_orig = 1; | |
602 | ||
603 | if (compare_orig(batman_packet->prev_sender, | |
604 | batman_if->net_dev->dev_addr)) | |
605 | is_my_oldorig = 1; | |
606 | ||
607 | if (compare_orig(ethhdr->h_source, broadcast_addr)) | |
608 | is_broadcast = 1; | |
609 | } | |
610 | rcu_read_unlock(); | |
611 | ||
612 | if (batman_packet->version != COMPAT_VERSION) { | |
613 | bat_dbg(DBG_BATMAN, bat_priv, | |
614 | "Drop packet: incompatible batman version (%i)\n", | |
615 | batman_packet->version); | |
616 | return; | |
617 | } | |
618 | ||
619 | if (is_my_addr) { | |
620 | bat_dbg(DBG_BATMAN, bat_priv, | |
621 | "Drop packet: received my own broadcast (sender: %pM" | |
622 | ")\n", | |
623 | ethhdr->h_source); | |
624 | return; | |
625 | } | |
626 | ||
627 | if (is_broadcast) { | |
628 | bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: " | |
629 | "ignoring all packets with broadcast source addr (sender: %pM" | |
630 | ")\n", ethhdr->h_source); | |
631 | return; | |
632 | } | |
633 | ||
634 | if (is_my_orig) { | |
635 | unsigned long *word; | |
636 | int offset; | |
637 | ||
638 | orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source); | |
639 | ||
640 | if (!orig_neigh_node) | |
641 | return; | |
642 | ||
643 | /* neighbor has to indicate direct link and it has to | |
644 | * come via the corresponding interface */ | |
645 | /* if received seqno equals last send seqno save new | |
646 | * seqno for bidirectional check */ | |
647 | if (has_directlink_flag && | |
648 | compare_orig(if_incoming->net_dev->dev_addr, | |
649 | batman_packet->orig) && | |
650 | (batman_packet->seqno - if_incoming_seqno + 2 == 0)) { | |
651 | offset = if_incoming->if_num * NUM_WORDS; | |
652 | word = &(orig_neigh_node->bcast_own[offset]); | |
653 | bit_mark(word, 0); | |
654 | orig_neigh_node->bcast_own_sum[if_incoming->if_num] = | |
655 | bit_packet_count(word); | |
656 | } | |
657 | ||
658 | bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: " | |
659 | "originator packet from myself (via neighbor)\n"); | |
660 | return; | |
661 | } | |
662 | ||
663 | if (is_my_oldorig) { | |
664 | bat_dbg(DBG_BATMAN, bat_priv, | |
665 | "Drop packet: ignoring all rebroadcast echos (sender: " | |
666 | "%pM)\n", ethhdr->h_source); | |
667 | return; | |
668 | } | |
669 | ||
670 | orig_node = get_orig_node(bat_priv, batman_packet->orig); | |
671 | if (!orig_node) | |
672 | return; | |
673 | ||
674 | is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming); | |
675 | ||
676 | if (is_duplicate == -1) { | |
677 | bat_dbg(DBG_BATMAN, bat_priv, | |
678 | "Drop packet: packet within seqno protection time " | |
679 | "(sender: %pM)\n", ethhdr->h_source); | |
680 | return; | |
681 | } | |
682 | ||
683 | if (batman_packet->tq == 0) { | |
684 | bat_dbg(DBG_BATMAN, bat_priv, | |
685 | "Drop packet: originator packet with tq equal 0\n"); | |
686 | return; | |
687 | } | |
688 | ||
689 | /* avoid temporary routing loops */ | |
690 | if ((orig_node->router) && | |
691 | (orig_node->router->orig_node->router) && | |
692 | (compare_orig(orig_node->router->addr, | |
693 | batman_packet->prev_sender)) && | |
694 | !(compare_orig(batman_packet->orig, batman_packet->prev_sender)) && | |
695 | (compare_orig(orig_node->router->addr, | |
696 | orig_node->router->orig_node->router->addr))) { | |
697 | bat_dbg(DBG_BATMAN, bat_priv, | |
698 | "Drop packet: ignoring all rebroadcast packets that " | |
699 | "may make me loop (sender: %pM)\n", ethhdr->h_source); | |
700 | return; | |
701 | } | |
702 | ||
703 | /* if sender is a direct neighbor the sender mac equals | |
704 | * originator mac */ | |
705 | orig_neigh_node = (is_single_hop_neigh ? | |
706 | orig_node : | |
707 | get_orig_node(bat_priv, ethhdr->h_source)); | |
708 | if (!orig_neigh_node) | |
709 | return; | |
710 | ||
711 | /* drop packet if sender is not a direct neighbor and if we | |
712 | * don't route towards it */ | |
713 | if (!is_single_hop_neigh && (!orig_neigh_node->router)) { | |
714 | bat_dbg(DBG_BATMAN, bat_priv, | |
715 | "Drop packet: OGM via unknown neighbor!\n"); | |
716 | return; | |
717 | } | |
718 | ||
719 | is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node, | |
720 | batman_packet, if_incoming); | |
721 | ||
722 | /* update ranking if it is not a duplicate or has the same | |
723 | * seqno and similar ttl as the non-duplicate */ | |
724 | if (is_bidirectional && | |
725 | (!is_duplicate || | |
726 | ((orig_node->last_real_seqno == batman_packet->seqno) && | |
727 | (orig_node->last_ttl - 3 <= batman_packet->ttl)))) | |
728 | update_orig(bat_priv, orig_node, ethhdr, batman_packet, | |
729 | if_incoming, hna_buff, hna_buff_len, is_duplicate); | |
730 | ||
74ef1153 SW |
731 | mark_bonding_address(orig_node, orig_neigh_node, batman_packet); |
732 | update_bonding_candidates(orig_node); | |
c6c8fea2 SE |
733 | |
734 | /* is single hop (direct) neighbor */ | |
735 | if (is_single_hop_neigh) { | |
736 | ||
737 | /* mark direct link on incoming interface */ | |
738 | schedule_forward_packet(orig_node, ethhdr, batman_packet, | |
739 | 1, hna_buff_len, if_incoming); | |
740 | ||
741 | bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: " | |
742 | "rebroadcast neighbor packet with direct link flag\n"); | |
743 | return; | |
744 | } | |
745 | ||
746 | /* multihop originator */ | |
747 | if (!is_bidirectional) { | |
748 | bat_dbg(DBG_BATMAN, bat_priv, | |
749 | "Drop packet: not received via bidirectional link\n"); | |
750 | return; | |
751 | } | |
752 | ||
753 | if (is_duplicate) { | |
754 | bat_dbg(DBG_BATMAN, bat_priv, | |
755 | "Drop packet: duplicate packet received\n"); | |
756 | return; | |
757 | } | |
758 | ||
759 | bat_dbg(DBG_BATMAN, bat_priv, | |
760 | "Forwarding packet: rebroadcast originator packet\n"); | |
761 | schedule_forward_packet(orig_node, ethhdr, batman_packet, | |
762 | 0, hna_buff_len, if_incoming); | |
763 | } | |
764 | ||
765 | int recv_bat_packet(struct sk_buff *skb, struct batman_if *batman_if) | |
766 | { | |
767 | struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface); | |
768 | struct ethhdr *ethhdr; | |
769 | ||
770 | /* drop packet if it has not necessary minimum size */ | |
771 | if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet)))) | |
772 | return NET_RX_DROP; | |
773 | ||
774 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
775 | ||
776 | /* packet with broadcast indication but unicast recipient */ | |
777 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) | |
778 | return NET_RX_DROP; | |
779 | ||
780 | /* packet with broadcast sender address */ | |
781 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
782 | return NET_RX_DROP; | |
783 | ||
784 | /* create a copy of the skb, if needed, to modify it. */ | |
785 | if (skb_cow(skb, 0) < 0) | |
786 | return NET_RX_DROP; | |
787 | ||
788 | /* keep skb linear */ | |
789 | if (skb_linearize(skb) < 0) | |
790 | return NET_RX_DROP; | |
791 | ||
792 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
793 | ||
794 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
795 | receive_aggr_bat_packet(ethhdr, | |
796 | skb->data, | |
797 | skb_headlen(skb), | |
798 | batman_if); | |
799 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
800 | ||
801 | kfree_skb(skb); | |
802 | return NET_RX_SUCCESS; | |
803 | } | |
804 | ||
805 | static int recv_my_icmp_packet(struct bat_priv *bat_priv, | |
806 | struct sk_buff *skb, size_t icmp_len) | |
807 | { | |
808 | struct orig_node *orig_node; | |
809 | struct icmp_packet_rr *icmp_packet; | |
c6c8fea2 SE |
810 | struct batman_if *batman_if; |
811 | int ret; | |
812 | uint8_t dstaddr[ETH_ALEN]; | |
813 | ||
814 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
c6c8fea2 SE |
815 | |
816 | /* add data to device queue */ | |
817 | if (icmp_packet->msg_type != ECHO_REQUEST) { | |
818 | bat_socket_receive_packet(icmp_packet, icmp_len); | |
819 | return NET_RX_DROP; | |
820 | } | |
821 | ||
822 | if (!bat_priv->primary_if) | |
823 | return NET_RX_DROP; | |
824 | ||
825 | /* answer echo request (ping) */ | |
826 | /* get routing information */ | |
827 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
828 | orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, | |
829 | compare_orig, choose_orig, | |
830 | icmp_packet->orig)); | |
831 | ret = NET_RX_DROP; | |
832 | ||
833 | if ((orig_node) && (orig_node->router)) { | |
834 | ||
835 | /* don't lock while sending the packets ... we therefore | |
836 | * copy the required data before sending */ | |
837 | batman_if = orig_node->router->if_incoming; | |
838 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
839 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
840 | ||
841 | /* create a copy of the skb, if needed, to modify it. */ | |
842 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) | |
843 | return NET_RX_DROP; | |
844 | ||
845 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
c6c8fea2 SE |
846 | |
847 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); | |
848 | memcpy(icmp_packet->orig, | |
849 | bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN); | |
850 | icmp_packet->msg_type = ECHO_REPLY; | |
851 | icmp_packet->ttl = TTL; | |
852 | ||
853 | send_skb_packet(skb, batman_if, dstaddr); | |
854 | ret = NET_RX_SUCCESS; | |
855 | ||
856 | } else | |
857 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
858 | ||
859 | return ret; | |
860 | } | |
861 | ||
862 | static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, | |
74ef1153 | 863 | struct sk_buff *skb) |
c6c8fea2 SE |
864 | { |
865 | struct orig_node *orig_node; | |
866 | struct icmp_packet *icmp_packet; | |
c6c8fea2 SE |
867 | struct batman_if *batman_if; |
868 | int ret; | |
869 | uint8_t dstaddr[ETH_ALEN]; | |
870 | ||
871 | icmp_packet = (struct icmp_packet *)skb->data; | |
c6c8fea2 SE |
872 | |
873 | /* send TTL exceeded if packet is an echo request (traceroute) */ | |
874 | if (icmp_packet->msg_type != ECHO_REQUEST) { | |
875 | pr_debug("Warning - can't forward icmp packet from %pM to " | |
876 | "%pM: ttl exceeded\n", icmp_packet->orig, | |
877 | icmp_packet->dst); | |
878 | return NET_RX_DROP; | |
879 | } | |
880 | ||
881 | if (!bat_priv->primary_if) | |
882 | return NET_RX_DROP; | |
883 | ||
884 | /* get routing information */ | |
885 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
886 | orig_node = ((struct orig_node *) | |
887 | hash_find(bat_priv->orig_hash, compare_orig, choose_orig, | |
888 | icmp_packet->orig)); | |
889 | ret = NET_RX_DROP; | |
890 | ||
891 | if ((orig_node) && (orig_node->router)) { | |
892 | ||
893 | /* don't lock while sending the packets ... we therefore | |
894 | * copy the required data before sending */ | |
895 | batman_if = orig_node->router->if_incoming; | |
896 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
897 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
898 | ||
899 | /* create a copy of the skb, if needed, to modify it. */ | |
900 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) | |
901 | return NET_RX_DROP; | |
902 | ||
903 | icmp_packet = (struct icmp_packet *) skb->data; | |
c6c8fea2 SE |
904 | |
905 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); | |
906 | memcpy(icmp_packet->orig, | |
907 | bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN); | |
908 | icmp_packet->msg_type = TTL_EXCEEDED; | |
909 | icmp_packet->ttl = TTL; | |
910 | ||
911 | send_skb_packet(skb, batman_if, dstaddr); | |
912 | ret = NET_RX_SUCCESS; | |
913 | ||
914 | } else | |
915 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
916 | ||
917 | return ret; | |
918 | } | |
919 | ||
920 | ||
921 | int recv_icmp_packet(struct sk_buff *skb, struct batman_if *recv_if) | |
922 | { | |
923 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
924 | struct icmp_packet_rr *icmp_packet; | |
925 | struct ethhdr *ethhdr; | |
926 | struct orig_node *orig_node; | |
927 | struct batman_if *batman_if; | |
928 | int hdr_size = sizeof(struct icmp_packet); | |
929 | int ret; | |
930 | uint8_t dstaddr[ETH_ALEN]; | |
931 | ||
932 | /** | |
933 | * we truncate all incoming icmp packets if they don't match our size | |
934 | */ | |
935 | if (skb->len >= sizeof(struct icmp_packet_rr)) | |
936 | hdr_size = sizeof(struct icmp_packet_rr); | |
937 | ||
938 | /* drop packet if it has not necessary minimum size */ | |
939 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
940 | return NET_RX_DROP; | |
941 | ||
942 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
943 | ||
944 | /* packet with unicast indication but broadcast recipient */ | |
945 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
946 | return NET_RX_DROP; | |
947 | ||
948 | /* packet with broadcast sender address */ | |
949 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
950 | return NET_RX_DROP; | |
951 | ||
952 | /* not for me */ | |
953 | if (!is_my_mac(ethhdr->h_dest)) | |
954 | return NET_RX_DROP; | |
955 | ||
956 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
957 | ||
958 | /* add record route information if not full */ | |
959 | if ((hdr_size == sizeof(struct icmp_packet_rr)) && | |
960 | (icmp_packet->rr_cur < BAT_RR_LEN)) { | |
961 | memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), | |
962 | ethhdr->h_dest, ETH_ALEN); | |
963 | icmp_packet->rr_cur++; | |
964 | } | |
965 | ||
966 | /* packet for me */ | |
967 | if (is_my_mac(icmp_packet->dst)) | |
968 | return recv_my_icmp_packet(bat_priv, skb, hdr_size); | |
969 | ||
970 | /* TTL exceeded */ | |
971 | if (icmp_packet->ttl < 2) | |
74ef1153 | 972 | return recv_icmp_ttl_exceeded(bat_priv, skb); |
c6c8fea2 SE |
973 | |
974 | ret = NET_RX_DROP; | |
975 | ||
976 | /* get routing information */ | |
977 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
978 | orig_node = ((struct orig_node *) | |
979 | hash_find(bat_priv->orig_hash, compare_orig, choose_orig, | |
980 | icmp_packet->dst)); | |
981 | ||
982 | if ((orig_node) && (orig_node->router)) { | |
983 | ||
984 | /* don't lock while sending the packets ... we therefore | |
985 | * copy the required data before sending */ | |
986 | batman_if = orig_node->router->if_incoming; | |
987 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
988 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
989 | ||
990 | /* create a copy of the skb, if needed, to modify it. */ | |
991 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) | |
992 | return NET_RX_DROP; | |
993 | ||
994 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
c6c8fea2 SE |
995 | |
996 | /* decrement ttl */ | |
997 | icmp_packet->ttl--; | |
998 | ||
999 | /* route it */ | |
1000 | send_skb_packet(skb, batman_if, dstaddr); | |
1001 | ret = NET_RX_SUCCESS; | |
1002 | ||
1003 | } else | |
1004 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1005 | ||
1006 | return ret; | |
1007 | } | |
1008 | ||
1009 | /* find a suitable router for this originator, and use | |
1010 | * bonding if possible. */ | |
1011 | struct neigh_node *find_router(struct bat_priv *bat_priv, | |
1012 | struct orig_node *orig_node, | |
1013 | struct batman_if *recv_if) | |
1014 | { | |
1015 | struct orig_node *primary_orig_node; | |
1016 | struct orig_node *router_orig; | |
1017 | struct neigh_node *router, *first_candidate, *best_router; | |
1018 | static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; | |
1019 | int bonding_enabled; | |
1020 | ||
1021 | if (!orig_node) | |
1022 | return NULL; | |
1023 | ||
1024 | if (!orig_node->router) | |
1025 | return NULL; | |
1026 | ||
1027 | /* without bonding, the first node should | |
1028 | * always choose the default router. */ | |
1029 | ||
1030 | bonding_enabled = atomic_read(&bat_priv->bonding); | |
1031 | ||
1032 | if ((!recv_if) && (!bonding_enabled)) | |
1033 | return orig_node->router; | |
1034 | ||
1035 | router_orig = orig_node->router->orig_node; | |
1036 | ||
1037 | /* if we have something in the primary_addr, we can search | |
1038 | * for a potential bonding candidate. */ | |
1039 | if (memcmp(router_orig->primary_addr, zero_mac, ETH_ALEN) == 0) | |
1040 | return orig_node->router; | |
1041 | ||
1042 | /* find the orig_node which has the primary interface. might | |
1043 | * even be the same as our router_orig in many cases */ | |
1044 | ||
1045 | if (memcmp(router_orig->primary_addr, | |
1046 | router_orig->orig, ETH_ALEN) == 0) { | |
1047 | primary_orig_node = router_orig; | |
1048 | } else { | |
1049 | primary_orig_node = hash_find(bat_priv->orig_hash, compare_orig, | |
1050 | choose_orig, | |
1051 | router_orig->primary_addr); | |
1052 | ||
1053 | if (!primary_orig_node) | |
1054 | return orig_node->router; | |
1055 | } | |
1056 | ||
1057 | /* with less than 2 candidates, we can't do any | |
1058 | * bonding and prefer the original router. */ | |
1059 | ||
1060 | if (primary_orig_node->bond.candidates < 2) | |
1061 | return orig_node->router; | |
1062 | ||
1063 | ||
1064 | /* all nodes between should choose a candidate which | |
1065 | * is is not on the interface where the packet came | |
1066 | * in. */ | |
1067 | first_candidate = primary_orig_node->bond.selected; | |
1068 | router = first_candidate; | |
1069 | ||
1070 | if (bonding_enabled) { | |
1071 | /* in the bonding case, send the packets in a round | |
1072 | * robin fashion over the remaining interfaces. */ | |
1073 | do { | |
1074 | /* recv_if == NULL on the first node. */ | |
1075 | if (router->if_incoming != recv_if) | |
1076 | break; | |
1077 | ||
1078 | router = router->next_bond_candidate; | |
1079 | } while (router != first_candidate); | |
1080 | ||
1081 | primary_orig_node->bond.selected = router->next_bond_candidate; | |
1082 | ||
1083 | } else { | |
1084 | /* if bonding is disabled, use the best of the | |
1085 | * remaining candidates which are not using | |
1086 | * this interface. */ | |
1087 | best_router = first_candidate; | |
1088 | ||
1089 | do { | |
1090 | /* recv_if == NULL on the first node. */ | |
1091 | if ((router->if_incoming != recv_if) && | |
1092 | (router->tq_avg > best_router->tq_avg)) | |
1093 | best_router = router; | |
1094 | ||
1095 | router = router->next_bond_candidate; | |
1096 | } while (router != first_candidate); | |
1097 | ||
1098 | router = best_router; | |
1099 | } | |
1100 | ||
1101 | return router; | |
1102 | } | |
1103 | ||
1104 | static int check_unicast_packet(struct sk_buff *skb, int hdr_size) | |
1105 | { | |
1106 | struct ethhdr *ethhdr; | |
1107 | ||
1108 | /* drop packet if it has not necessary minimum size */ | |
1109 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
1110 | return -1; | |
1111 | ||
1112 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1113 | ||
1114 | /* packet with unicast indication but broadcast recipient */ | |
1115 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
1116 | return -1; | |
1117 | ||
1118 | /* packet with broadcast sender address */ | |
1119 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
1120 | return -1; | |
1121 | ||
1122 | /* not for me */ | |
1123 | if (!is_my_mac(ethhdr->h_dest)) | |
1124 | return -1; | |
1125 | ||
1126 | return 0; | |
1127 | } | |
1128 | ||
1129 | int route_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if, | |
1130 | int hdr_size) | |
1131 | { | |
1132 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1133 | struct orig_node *orig_node; | |
1134 | struct neigh_node *router; | |
1135 | struct batman_if *batman_if; | |
1136 | uint8_t dstaddr[ETH_ALEN]; | |
1137 | struct unicast_packet *unicast_packet; | |
1138 | struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1139 | int ret; | |
1140 | struct sk_buff *new_skb; | |
1141 | ||
1142 | unicast_packet = (struct unicast_packet *)skb->data; | |
1143 | ||
1144 | /* TTL exceeded */ | |
1145 | if (unicast_packet->ttl < 2) { | |
1146 | pr_debug("Warning - can't forward unicast packet from %pM to " | |
1147 | "%pM: ttl exceeded\n", ethhdr->h_source, | |
1148 | unicast_packet->dest); | |
1149 | return NET_RX_DROP; | |
1150 | } | |
1151 | ||
1152 | /* get routing information */ | |
1153 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
1154 | orig_node = ((struct orig_node *) | |
1155 | hash_find(bat_priv->orig_hash, compare_orig, choose_orig, | |
1156 | unicast_packet->dest)); | |
1157 | ||
1158 | router = find_router(bat_priv, orig_node, recv_if); | |
1159 | ||
1160 | if (!router) { | |
1161 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1162 | return NET_RX_DROP; | |
1163 | } | |
1164 | ||
1165 | /* don't lock while sending the packets ... we therefore | |
1166 | * copy the required data before sending */ | |
1167 | ||
1168 | batman_if = router->if_incoming; | |
1169 | memcpy(dstaddr, router->addr, ETH_ALEN); | |
1170 | ||
1171 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1172 | ||
1173 | /* create a copy of the skb, if needed, to modify it. */ | |
1174 | if (skb_cow(skb, sizeof(struct ethhdr)) < 0) | |
1175 | return NET_RX_DROP; | |
1176 | ||
1177 | unicast_packet = (struct unicast_packet *)skb->data; | |
1178 | ||
1179 | if (unicast_packet->packet_type == BAT_UNICAST && | |
1180 | atomic_read(&bat_priv->fragmentation) && | |
1181 | skb->len > batman_if->net_dev->mtu) | |
1182 | return frag_send_skb(skb, bat_priv, batman_if, | |
1183 | dstaddr); | |
1184 | ||
1185 | if (unicast_packet->packet_type == BAT_UNICAST_FRAG && | |
ae361ce1 | 1186 | frag_can_reassemble(skb, batman_if->net_dev->mtu)) { |
c6c8fea2 SE |
1187 | |
1188 | ret = frag_reassemble_skb(skb, bat_priv, &new_skb); | |
1189 | ||
1190 | if (ret == NET_RX_DROP) | |
1191 | return NET_RX_DROP; | |
1192 | ||
1193 | /* packet was buffered for late merge */ | |
1194 | if (!new_skb) | |
1195 | return NET_RX_SUCCESS; | |
1196 | ||
1197 | skb = new_skb; | |
1198 | unicast_packet = (struct unicast_packet *)skb->data; | |
1199 | } | |
1200 | ||
1201 | /* decrement ttl */ | |
1202 | unicast_packet->ttl--; | |
1203 | ||
1204 | /* route it */ | |
1205 | send_skb_packet(skb, batman_if, dstaddr); | |
1206 | ||
1207 | return NET_RX_SUCCESS; | |
1208 | } | |
1209 | ||
1210 | int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if) | |
1211 | { | |
1212 | struct unicast_packet *unicast_packet; | |
1213 | int hdr_size = sizeof(struct unicast_packet); | |
1214 | ||
1215 | if (check_unicast_packet(skb, hdr_size) < 0) | |
1216 | return NET_RX_DROP; | |
1217 | ||
1218 | unicast_packet = (struct unicast_packet *)skb->data; | |
1219 | ||
1220 | /* packet for me */ | |
1221 | if (is_my_mac(unicast_packet->dest)) { | |
1222 | interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); | |
1223 | return NET_RX_SUCCESS; | |
1224 | } | |
1225 | ||
1226 | return route_unicast_packet(skb, recv_if, hdr_size); | |
1227 | } | |
1228 | ||
1229 | int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if) | |
1230 | { | |
1231 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1232 | struct unicast_frag_packet *unicast_packet; | |
1233 | int hdr_size = sizeof(struct unicast_frag_packet); | |
1234 | struct sk_buff *new_skb = NULL; | |
1235 | int ret; | |
1236 | ||
1237 | if (check_unicast_packet(skb, hdr_size) < 0) | |
1238 | return NET_RX_DROP; | |
1239 | ||
1240 | unicast_packet = (struct unicast_frag_packet *)skb->data; | |
1241 | ||
1242 | /* packet for me */ | |
1243 | if (is_my_mac(unicast_packet->dest)) { | |
1244 | ||
1245 | ret = frag_reassemble_skb(skb, bat_priv, &new_skb); | |
1246 | ||
1247 | if (ret == NET_RX_DROP) | |
1248 | return NET_RX_DROP; | |
1249 | ||
1250 | /* packet was buffered for late merge */ | |
1251 | if (!new_skb) | |
1252 | return NET_RX_SUCCESS; | |
1253 | ||
1254 | interface_rx(recv_if->soft_iface, new_skb, recv_if, | |
1255 | sizeof(struct unicast_packet)); | |
1256 | return NET_RX_SUCCESS; | |
1257 | } | |
1258 | ||
1259 | return route_unicast_packet(skb, recv_if, hdr_size); | |
1260 | } | |
1261 | ||
1262 | ||
1263 | int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if) | |
1264 | { | |
1265 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1266 | struct orig_node *orig_node; | |
1267 | struct bcast_packet *bcast_packet; | |
1268 | struct ethhdr *ethhdr; | |
1269 | int hdr_size = sizeof(struct bcast_packet); | |
1270 | int32_t seq_diff; | |
1271 | ||
1272 | /* drop packet if it has not necessary minimum size */ | |
1273 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
1274 | return NET_RX_DROP; | |
1275 | ||
1276 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1277 | ||
1278 | /* packet with broadcast indication but unicast recipient */ | |
1279 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) | |
1280 | return NET_RX_DROP; | |
1281 | ||
1282 | /* packet with broadcast sender address */ | |
1283 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
1284 | return NET_RX_DROP; | |
1285 | ||
1286 | /* ignore broadcasts sent by myself */ | |
1287 | if (is_my_mac(ethhdr->h_source)) | |
1288 | return NET_RX_DROP; | |
1289 | ||
1290 | bcast_packet = (struct bcast_packet *)skb->data; | |
1291 | ||
1292 | /* ignore broadcasts originated by myself */ | |
1293 | if (is_my_mac(bcast_packet->orig)) | |
1294 | return NET_RX_DROP; | |
1295 | ||
1296 | if (bcast_packet->ttl < 2) | |
1297 | return NET_RX_DROP; | |
1298 | ||
1299 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
1300 | orig_node = ((struct orig_node *) | |
1301 | hash_find(bat_priv->orig_hash, compare_orig, choose_orig, | |
1302 | bcast_packet->orig)); | |
1303 | ||
1304 | if (!orig_node) { | |
1305 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1306 | return NET_RX_DROP; | |
1307 | } | |
1308 | ||
1309 | /* check whether the packet is a duplicate */ | |
1310 | if (get_bit_status(orig_node->bcast_bits, | |
1311 | orig_node->last_bcast_seqno, | |
1312 | ntohl(bcast_packet->seqno))) { | |
1313 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1314 | return NET_RX_DROP; | |
1315 | } | |
1316 | ||
1317 | seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno; | |
1318 | ||
1319 | /* check whether the packet is old and the host just restarted. */ | |
1320 | if (window_protected(bat_priv, seq_diff, | |
1321 | &orig_node->bcast_seqno_reset)) { | |
1322 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1323 | return NET_RX_DROP; | |
1324 | } | |
1325 | ||
1326 | /* mark broadcast in flood history, update window position | |
1327 | * if required. */ | |
1328 | if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1)) | |
1329 | orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno); | |
1330 | ||
1331 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
1332 | /* rebroadcast packet */ | |
1333 | add_bcast_packet_to_list(bat_priv, skb); | |
1334 | ||
1335 | /* broadcast for me */ | |
1336 | interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); | |
1337 | ||
1338 | return NET_RX_SUCCESS; | |
1339 | } | |
1340 | ||
1341 | int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if) | |
1342 | { | |
1343 | struct vis_packet *vis_packet; | |
1344 | struct ethhdr *ethhdr; | |
1345 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1346 | int hdr_size = sizeof(struct vis_packet); | |
1347 | ||
1348 | /* keep skb linear */ | |
1349 | if (skb_linearize(skb) < 0) | |
1350 | return NET_RX_DROP; | |
1351 | ||
1352 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
1353 | return NET_RX_DROP; | |
1354 | ||
1355 | vis_packet = (struct vis_packet *)skb->data; | |
1356 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1357 | ||
1358 | /* not for me */ | |
1359 | if (!is_my_mac(ethhdr->h_dest)) | |
1360 | return NET_RX_DROP; | |
1361 | ||
1362 | /* ignore own packets */ | |
1363 | if (is_my_mac(vis_packet->vis_orig)) | |
1364 | return NET_RX_DROP; | |
1365 | ||
1366 | if (is_my_mac(vis_packet->sender_orig)) | |
1367 | return NET_RX_DROP; | |
1368 | ||
1369 | switch (vis_packet->vis_type) { | |
1370 | case VIS_TYPE_SERVER_SYNC: | |
1371 | receive_server_sync_packet(bat_priv, vis_packet, | |
1372 | skb_headlen(skb)); | |
1373 | break; | |
1374 | ||
1375 | case VIS_TYPE_CLIENT_UPDATE: | |
1376 | receive_client_update_packet(bat_priv, vis_packet, | |
1377 | skb_headlen(skb)); | |
1378 | break; | |
1379 | ||
1380 | default: /* ignore unknown packet */ | |
1381 | break; | |
1382 | } | |
1383 | ||
1384 | /* We take a copy of the data in the packet, so we should | |
1385 | always free the skbuf. */ | |
1386 | return NET_RX_DROP; | |
1387 | } |