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