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