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