]>
Commit | Line | Data |
---|---|---|
9cfc7bd6 | 1 | /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
2 | * |
3 | * Marek Lindner, Simon Wunderlich | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or | |
6 | * modify it under the terms of version 2 of the GNU General Public | |
7 | * License as published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but | |
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | * General Public License for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License | |
15 | * along with this program; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
17 | * 02110-1301, USA | |
c6c8fea2 SE |
18 | */ |
19 | ||
20 | #include "main.h" | |
21 | #include "routing.h" | |
22 | #include "send.h" | |
c6c8fea2 SE |
23 | #include "soft-interface.h" |
24 | #include "hard-interface.h" | |
25 | #include "icmp_socket.h" | |
26 | #include "translation-table.h" | |
27 | #include "originator.h" | |
c6c8fea2 | 28 | #include "vis.h" |
c6c8fea2 | 29 | #include "unicast.h" |
23721387 | 30 | #include "bridge_loop_avoidance.h" |
c6c8fea2 | 31 | |
63b01037 SE |
32 | static int batadv_route_unicast_packet(struct sk_buff *skb, |
33 | struct hard_iface *recv_if); | |
a7f6ee94 | 34 | |
30d3c511 | 35 | void batadv_slide_own_bcast_window(struct hard_iface *hard_iface) |
c6c8fea2 | 36 | { |
e6c10f43 | 37 | struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
c6c8fea2 | 38 | struct hashtable_t *hash = bat_priv->orig_hash; |
7aadf889 | 39 | struct hlist_node *node; |
c6c8fea2 | 40 | struct hlist_head *head; |
c6c8fea2 SE |
41 | struct orig_node *orig_node; |
42 | unsigned long *word; | |
c90681b8 | 43 | uint32_t i; |
c6c8fea2 SE |
44 | size_t word_index; |
45 | ||
c6c8fea2 SE |
46 | for (i = 0; i < hash->size; i++) { |
47 | head = &hash->table[i]; | |
48 | ||
fb778ea1 | 49 | rcu_read_lock(); |
7aadf889 | 50 | hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) { |
2ae2daf6 | 51 | spin_lock_bh(&orig_node->ogm_cnt_lock); |
e6c10f43 | 52 | word_index = hard_iface->if_num * NUM_WORDS; |
c6c8fea2 SE |
53 | word = &(orig_node->bcast_own[word_index]); |
54 | ||
0f5f9322 | 55 | batadv_bit_get_packet(bat_priv, word, 1, 0); |
e6c10f43 | 56 | orig_node->bcast_own_sum[hard_iface->if_num] = |
0079d2ce | 57 | bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE); |
2ae2daf6 | 58 | spin_unlock_bh(&orig_node->ogm_cnt_lock); |
c6c8fea2 | 59 | } |
fb778ea1 | 60 | rcu_read_unlock(); |
c6c8fea2 | 61 | } |
c6c8fea2 SE |
62 | } |
63 | ||
63b01037 SE |
64 | static void _batadv_update_route(struct bat_priv *bat_priv, |
65 | struct orig_node *orig_node, | |
66 | struct neigh_node *neigh_node) | |
c6c8fea2 | 67 | { |
e1a5382f LL |
68 | struct neigh_node *curr_router; |
69 | ||
7d211efc | 70 | curr_router = batadv_orig_node_get_router(orig_node); |
a8e7f4bc | 71 | |
c6c8fea2 | 72 | /* route deleted */ |
e1a5382f | 73 | if ((curr_router) && (!neigh_node)) { |
1eda58bf SE |
74 | batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n", |
75 | orig_node->orig); | |
08c36d3e SE |
76 | batadv_tt_global_del_orig(bat_priv, orig_node, |
77 | "Deleted route towards originator"); | |
c6c8fea2 | 78 | |
e1a5382f LL |
79 | /* route added */ |
80 | } else if ((!curr_router) && (neigh_node)) { | |
c6c8fea2 | 81 | |
1eda58bf SE |
82 | batadv_dbg(DBG_ROUTES, bat_priv, |
83 | "Adding route towards: %pM (via %pM)\n", | |
84 | orig_node->orig, neigh_node->addr); | |
e1a5382f | 85 | /* route changed */ |
bb899b89 | 86 | } else if (neigh_node && curr_router) { |
1eda58bf SE |
87 | batadv_dbg(DBG_ROUTES, bat_priv, |
88 | "Changing route towards: %pM (now via %pM - was via %pM)\n", | |
89 | orig_node->orig, neigh_node->addr, | |
90 | curr_router->addr); | |
c6c8fea2 SE |
91 | } |
92 | ||
e1a5382f | 93 | if (curr_router) |
7d211efc | 94 | batadv_neigh_node_free_ref(curr_router); |
e1a5382f LL |
95 | |
96 | /* increase refcount of new best neighbor */ | |
44524fcd ML |
97 | if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount)) |
98 | neigh_node = NULL; | |
e1a5382f LL |
99 | |
100 | spin_lock_bh(&orig_node->neigh_list_lock); | |
101 | rcu_assign_pointer(orig_node->router, neigh_node); | |
102 | spin_unlock_bh(&orig_node->neigh_list_lock); | |
103 | ||
104 | /* decrease refcount of previous best neighbor */ | |
105 | if (curr_router) | |
7d211efc | 106 | batadv_neigh_node_free_ref(curr_router); |
c6c8fea2 SE |
107 | } |
108 | ||
30d3c511 SE |
109 | void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node, |
110 | struct neigh_node *neigh_node) | |
c6c8fea2 | 111 | { |
e1a5382f | 112 | struct neigh_node *router = NULL; |
c6c8fea2 SE |
113 | |
114 | if (!orig_node) | |
e1a5382f LL |
115 | goto out; |
116 | ||
7d211efc | 117 | router = batadv_orig_node_get_router(orig_node); |
c6c8fea2 | 118 | |
e1a5382f | 119 | if (router != neigh_node) |
63b01037 | 120 | _batadv_update_route(bat_priv, orig_node, neigh_node); |
e1a5382f LL |
121 | |
122 | out: | |
123 | if (router) | |
7d211efc | 124 | batadv_neigh_node_free_ref(router); |
c6c8fea2 SE |
125 | } |
126 | ||
a4c135c5 | 127 | /* caller must hold the neigh_list_lock */ |
30d3c511 SE |
128 | void batadv_bonding_candidate_del(struct orig_node *orig_node, |
129 | struct neigh_node *neigh_node) | |
a4c135c5 SW |
130 | { |
131 | /* this neighbor is not part of our candidate list */ | |
132 | if (list_empty(&neigh_node->bonding_list)) | |
133 | goto out; | |
134 | ||
135 | list_del_rcu(&neigh_node->bonding_list); | |
a4c135c5 | 136 | INIT_LIST_HEAD(&neigh_node->bonding_list); |
7d211efc | 137 | batadv_neigh_node_free_ref(neigh_node); |
a4c135c5 SW |
138 | atomic_dec(&orig_node->bond_candidates); |
139 | ||
140 | out: | |
141 | return; | |
142 | } | |
143 | ||
30d3c511 SE |
144 | void batadv_bonding_candidate_add(struct orig_node *orig_node, |
145 | struct neigh_node *neigh_node) | |
a4c135c5 SW |
146 | { |
147 | struct hlist_node *node; | |
e1a5382f LL |
148 | struct neigh_node *tmp_neigh_node, *router = NULL; |
149 | uint8_t interference_candidate = 0; | |
a4c135c5 SW |
150 | |
151 | spin_lock_bh(&orig_node->neigh_list_lock); | |
152 | ||
153 | /* only consider if it has the same primary address ... */ | |
1eda58bf SE |
154 | if (!batadv_compare_eth(orig_node->orig, |
155 | neigh_node->orig_node->primary_addr)) | |
a4c135c5 SW |
156 | goto candidate_del; |
157 | ||
7d211efc | 158 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f | 159 | if (!router) |
a4c135c5 SW |
160 | goto candidate_del; |
161 | ||
a4c135c5 | 162 | /* ... and is good enough to be considered */ |
e1a5382f | 163 | if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD) |
a4c135c5 SW |
164 | goto candidate_del; |
165 | ||
9cfc7bd6 | 166 | /* check if we have another candidate with the same mac address or |
a4c135c5 SW |
167 | * interface. If we do, we won't select this candidate because of |
168 | * possible interference. | |
169 | */ | |
170 | hlist_for_each_entry_rcu(tmp_neigh_node, node, | |
171 | &orig_node->neigh_list, list) { | |
172 | ||
173 | if (tmp_neigh_node == neigh_node) | |
174 | continue; | |
175 | ||
176 | /* we only care if the other candidate is even | |
9cfc7bd6 SE |
177 | * considered as candidate. |
178 | */ | |
a4c135c5 SW |
179 | if (list_empty(&tmp_neigh_node->bonding_list)) |
180 | continue; | |
181 | ||
182 | if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) || | |
1eda58bf SE |
183 | (batadv_compare_eth(neigh_node->addr, |
184 | tmp_neigh_node->addr))) { | |
a4c135c5 SW |
185 | interference_candidate = 1; |
186 | break; | |
187 | } | |
188 | } | |
189 | ||
190 | /* don't care further if it is an interference candidate */ | |
191 | if (interference_candidate) | |
192 | goto candidate_del; | |
193 | ||
194 | /* this neighbor already is part of our candidate list */ | |
195 | if (!list_empty(&neigh_node->bonding_list)) | |
196 | goto out; | |
197 | ||
44524fcd ML |
198 | if (!atomic_inc_not_zero(&neigh_node->refcount)) |
199 | goto out; | |
200 | ||
a4c135c5 | 201 | list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list); |
a4c135c5 SW |
202 | atomic_inc(&orig_node->bond_candidates); |
203 | goto out; | |
204 | ||
205 | candidate_del: | |
30d3c511 | 206 | batadv_bonding_candidate_del(orig_node, neigh_node); |
a4c135c5 SW |
207 | |
208 | out: | |
209 | spin_unlock_bh(&orig_node->neigh_list_lock); | |
e1a5382f LL |
210 | |
211 | if (router) | |
7d211efc | 212 | batadv_neigh_node_free_ref(router); |
a4c135c5 SW |
213 | } |
214 | ||
215 | /* copy primary address for bonding */ | |
30d3c511 SE |
216 | void |
217 | batadv_bonding_save_primary(const struct orig_node *orig_node, | |
218 | struct orig_node *orig_neigh_node, | |
219 | const struct batman_ogm_packet *batman_ogm_packet) | |
a4c135c5 | 220 | { |
b6da4bf5 | 221 | if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP)) |
a4c135c5 SW |
222 | return; |
223 | ||
224 | memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN); | |
225 | } | |
226 | ||
c6c8fea2 SE |
227 | /* checks whether the host restarted and is in the protection time. |
228 | * returns: | |
229 | * 0 if the packet is to be accepted | |
230 | * 1 if the packet is to be ignored. | |
231 | */ | |
30d3c511 SE |
232 | int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff, |
233 | unsigned long *last_reset) | |
c6c8fea2 | 234 | { |
7c64fd98 SE |
235 | if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) || |
236 | (seq_num_diff >= EXPECTED_SEQNO_RANGE)) { | |
1eda58bf | 237 | if (!batadv_has_timed_out(*last_reset, RESET_PROTECTION_MS)) |
c6c8fea2 | 238 | return 1; |
8c7bf248 ML |
239 | |
240 | *last_reset = jiffies; | |
1eda58bf SE |
241 | batadv_dbg(DBG_BATMAN, bat_priv, |
242 | "old packet received, start protection\n"); | |
c6c8fea2 | 243 | } |
8c7bf248 | 244 | |
c6c8fea2 SE |
245 | return 0; |
246 | } | |
247 | ||
30d3c511 SE |
248 | bool batadv_check_management_packet(struct sk_buff *skb, |
249 | struct hard_iface *hard_iface, | |
250 | int header_len) | |
c6c8fea2 | 251 | { |
c6c8fea2 SE |
252 | struct ethhdr *ethhdr; |
253 | ||
254 | /* drop packet if it has not necessary minimum size */ | |
c3e29312 ML |
255 | if (unlikely(!pskb_may_pull(skb, header_len))) |
256 | return false; | |
c6c8fea2 SE |
257 | |
258 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
259 | ||
260 | /* packet with broadcast indication but unicast recipient */ | |
261 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) | |
c3e29312 | 262 | return false; |
c6c8fea2 SE |
263 | |
264 | /* packet with broadcast sender address */ | |
265 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
c3e29312 | 266 | return false; |
c6c8fea2 SE |
267 | |
268 | /* create a copy of the skb, if needed, to modify it. */ | |
269 | if (skb_cow(skb, 0) < 0) | |
c3e29312 | 270 | return false; |
c6c8fea2 SE |
271 | |
272 | /* keep skb linear */ | |
273 | if (skb_linearize(skb) < 0) | |
c3e29312 | 274 | return false; |
c6c8fea2 | 275 | |
c3e29312 | 276 | return true; |
c6c8fea2 SE |
277 | } |
278 | ||
63b01037 SE |
279 | static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv, |
280 | struct sk_buff *skb, size_t icmp_len) | |
c6c8fea2 | 281 | { |
32ae9b22 | 282 | struct hard_iface *primary_if = NULL; |
44524fcd | 283 | struct orig_node *orig_node = NULL; |
e1a5382f | 284 | struct neigh_node *router = NULL; |
c6c8fea2 | 285 | struct icmp_packet_rr *icmp_packet; |
44524fcd | 286 | int ret = NET_RX_DROP; |
c6c8fea2 SE |
287 | |
288 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
c6c8fea2 SE |
289 | |
290 | /* add data to device queue */ | |
291 | if (icmp_packet->msg_type != ECHO_REQUEST) { | |
9039dc7e | 292 | batadv_socket_receive_packet(icmp_packet, icmp_len); |
44524fcd | 293 | goto out; |
c6c8fea2 SE |
294 | } |
295 | ||
e5d89254 | 296 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 297 | if (!primary_if) |
44524fcd | 298 | goto out; |
c6c8fea2 SE |
299 | |
300 | /* answer echo request (ping) */ | |
301 | /* get routing information */ | |
da641193 | 302 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); |
44524fcd | 303 | if (!orig_node) |
e1a5382f | 304 | goto out; |
c6c8fea2 | 305 | |
7d211efc | 306 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
307 | if (!router) |
308 | goto out; | |
c6c8fea2 | 309 | |
44524fcd | 310 | /* create a copy of the skb, if needed, to modify it. */ |
0d125074 | 311 | if (skb_cow(skb, ETH_HLEN) < 0) |
44524fcd ML |
312 | goto out; |
313 | ||
314 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
315 | ||
316 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); | |
32ae9b22 | 317 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
44524fcd | 318 | icmp_packet->msg_type = ECHO_REPLY; |
76543d14 | 319 | icmp_packet->header.ttl = TTL; |
44524fcd | 320 | |
9455e34c | 321 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
44524fcd | 322 | ret = NET_RX_SUCCESS; |
c6c8fea2 | 323 | |
44524fcd | 324 | out: |
32ae9b22 | 325 | if (primary_if) |
e5d89254 | 326 | batadv_hardif_free_ref(primary_if); |
e1a5382f | 327 | if (router) |
7d211efc | 328 | batadv_neigh_node_free_ref(router); |
44524fcd | 329 | if (orig_node) |
7d211efc | 330 | batadv_orig_node_free_ref(orig_node); |
c6c8fea2 SE |
331 | return ret; |
332 | } | |
333 | ||
63b01037 SE |
334 | static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv, |
335 | struct sk_buff *skb) | |
c6c8fea2 | 336 | { |
32ae9b22 | 337 | struct hard_iface *primary_if = NULL; |
44524fcd | 338 | struct orig_node *orig_node = NULL; |
e1a5382f | 339 | struct neigh_node *router = NULL; |
c6c8fea2 | 340 | struct icmp_packet *icmp_packet; |
44524fcd | 341 | int ret = NET_RX_DROP; |
c6c8fea2 SE |
342 | |
343 | icmp_packet = (struct icmp_packet *)skb->data; | |
c6c8fea2 SE |
344 | |
345 | /* send TTL exceeded if packet is an echo request (traceroute) */ | |
346 | if (icmp_packet->msg_type != ECHO_REQUEST) { | |
86ceb360 SE |
347 | pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n", |
348 | icmp_packet->orig, icmp_packet->dst); | |
44524fcd | 349 | goto out; |
c6c8fea2 SE |
350 | } |
351 | ||
e5d89254 | 352 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 | 353 | if (!primary_if) |
44524fcd | 354 | goto out; |
c6c8fea2 SE |
355 | |
356 | /* get routing information */ | |
da641193 | 357 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig); |
44524fcd | 358 | if (!orig_node) |
e1a5382f | 359 | goto out; |
c6c8fea2 | 360 | |
7d211efc | 361 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
362 | if (!router) |
363 | goto out; | |
c6c8fea2 | 364 | |
44524fcd | 365 | /* create a copy of the skb, if needed, to modify it. */ |
0d125074 | 366 | if (skb_cow(skb, ETH_HLEN) < 0) |
44524fcd | 367 | goto out; |
c6c8fea2 | 368 | |
44524fcd | 369 | icmp_packet = (struct icmp_packet *)skb->data; |
c6c8fea2 | 370 | |
44524fcd | 371 | memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN); |
32ae9b22 | 372 | memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN); |
44524fcd | 373 | icmp_packet->msg_type = TTL_EXCEEDED; |
76543d14 | 374 | icmp_packet->header.ttl = TTL; |
44524fcd | 375 | |
9455e34c | 376 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
44524fcd | 377 | ret = NET_RX_SUCCESS; |
c6c8fea2 | 378 | |
44524fcd | 379 | out: |
32ae9b22 | 380 | if (primary_if) |
e5d89254 | 381 | batadv_hardif_free_ref(primary_if); |
e1a5382f | 382 | if (router) |
7d211efc | 383 | batadv_neigh_node_free_ref(router); |
44524fcd | 384 | if (orig_node) |
7d211efc | 385 | batadv_orig_node_free_ref(orig_node); |
c6c8fea2 SE |
386 | return ret; |
387 | } | |
388 | ||
389 | ||
30d3c511 | 390 | int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
c6c8fea2 SE |
391 | { |
392 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
393 | struct icmp_packet_rr *icmp_packet; | |
394 | struct ethhdr *ethhdr; | |
44524fcd | 395 | struct orig_node *orig_node = NULL; |
e1a5382f | 396 | struct neigh_node *router = NULL; |
c6c8fea2 | 397 | int hdr_size = sizeof(struct icmp_packet); |
44524fcd | 398 | int ret = NET_RX_DROP; |
c6c8fea2 | 399 | |
9cfc7bd6 | 400 | /* we truncate all incoming icmp packets if they don't match our size */ |
c6c8fea2 SE |
401 | if (skb->len >= sizeof(struct icmp_packet_rr)) |
402 | hdr_size = sizeof(struct icmp_packet_rr); | |
403 | ||
404 | /* drop packet if it has not necessary minimum size */ | |
405 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
44524fcd | 406 | goto out; |
c6c8fea2 SE |
407 | |
408 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
409 | ||
410 | /* packet with unicast indication but broadcast recipient */ | |
411 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
44524fcd | 412 | goto out; |
c6c8fea2 SE |
413 | |
414 | /* packet with broadcast sender address */ | |
415 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
44524fcd | 416 | goto out; |
c6c8fea2 SE |
417 | |
418 | /* not for me */ | |
3193e8fd | 419 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
44524fcd | 420 | goto out; |
c6c8fea2 SE |
421 | |
422 | icmp_packet = (struct icmp_packet_rr *)skb->data; | |
423 | ||
424 | /* add record route information if not full */ | |
425 | if ((hdr_size == sizeof(struct icmp_packet_rr)) && | |
426 | (icmp_packet->rr_cur < BAT_RR_LEN)) { | |
427 | memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]), | |
7c64fd98 | 428 | ethhdr->h_dest, ETH_ALEN); |
c6c8fea2 SE |
429 | icmp_packet->rr_cur++; |
430 | } | |
431 | ||
432 | /* packet for me */ | |
3193e8fd | 433 | if (batadv_is_my_mac(icmp_packet->dst)) |
63b01037 | 434 | return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size); |
c6c8fea2 SE |
435 | |
436 | /* TTL exceeded */ | |
76543d14 | 437 | if (icmp_packet->header.ttl < 2) |
63b01037 | 438 | return batadv_recv_icmp_ttl_exceeded(bat_priv, skb); |
c6c8fea2 | 439 | |
c6c8fea2 | 440 | /* get routing information */ |
da641193 | 441 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst); |
44524fcd | 442 | if (!orig_node) |
e1a5382f | 443 | goto out; |
c6c8fea2 | 444 | |
7d211efc | 445 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f LL |
446 | if (!router) |
447 | goto out; | |
c6c8fea2 | 448 | |
44524fcd | 449 | /* create a copy of the skb, if needed, to modify it. */ |
0d125074 | 450 | if (skb_cow(skb, ETH_HLEN) < 0) |
44524fcd | 451 | goto out; |
c6c8fea2 | 452 | |
44524fcd | 453 | icmp_packet = (struct icmp_packet_rr *)skb->data; |
c6c8fea2 | 454 | |
44524fcd | 455 | /* decrement ttl */ |
76543d14 | 456 | icmp_packet->header.ttl--; |
44524fcd ML |
457 | |
458 | /* route it */ | |
9455e34c | 459 | batadv_send_skb_packet(skb, router->if_incoming, router->addr); |
44524fcd | 460 | ret = NET_RX_SUCCESS; |
c6c8fea2 | 461 | |
44524fcd | 462 | out: |
e1a5382f | 463 | if (router) |
7d211efc | 464 | batadv_neigh_node_free_ref(router); |
44524fcd | 465 | if (orig_node) |
7d211efc | 466 | batadv_orig_node_free_ref(orig_node); |
c6c8fea2 SE |
467 | return ret; |
468 | } | |
469 | ||
55158629 LL |
470 | /* In the bonding case, send the packets in a round |
471 | * robin fashion over the remaining interfaces. | |
472 | * | |
473 | * This method rotates the bonding list and increases the | |
9cfc7bd6 SE |
474 | * returned router's refcount. |
475 | */ | |
63b01037 SE |
476 | static struct neigh_node * |
477 | batadv_find_bond_router(struct orig_node *primary_orig, | |
478 | const struct hard_iface *recv_if) | |
55158629 LL |
479 | { |
480 | struct neigh_node *tmp_neigh_node; | |
481 | struct neigh_node *router = NULL, *first_candidate = NULL; | |
482 | ||
483 | rcu_read_lock(); | |
484 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, | |
485 | bonding_list) { | |
486 | if (!first_candidate) | |
487 | first_candidate = tmp_neigh_node; | |
488 | ||
489 | /* recv_if == NULL on the first node. */ | |
490 | if (tmp_neigh_node->if_incoming == recv_if) | |
491 | continue; | |
492 | ||
493 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) | |
494 | continue; | |
495 | ||
496 | router = tmp_neigh_node; | |
497 | break; | |
498 | } | |
499 | ||
500 | /* use the first candidate if nothing was found. */ | |
501 | if (!router && first_candidate && | |
502 | atomic_inc_not_zero(&first_candidate->refcount)) | |
503 | router = first_candidate; | |
504 | ||
505 | if (!router) | |
506 | goto out; | |
507 | ||
508 | /* selected should point to the next element | |
9cfc7bd6 SE |
509 | * after the current router |
510 | */ | |
55158629 LL |
511 | spin_lock_bh(&primary_orig->neigh_list_lock); |
512 | /* this is a list_move(), which unfortunately | |
9cfc7bd6 SE |
513 | * does not exist as rcu version |
514 | */ | |
55158629 LL |
515 | list_del_rcu(&primary_orig->bond_list); |
516 | list_add_rcu(&primary_orig->bond_list, | |
517 | &router->bonding_list); | |
518 | spin_unlock_bh(&primary_orig->neigh_list_lock); | |
519 | ||
520 | out: | |
521 | rcu_read_unlock(); | |
522 | return router; | |
523 | } | |
524 | ||
525 | /* Interface Alternating: Use the best of the | |
526 | * remaining candidates which are not using | |
527 | * this interface. | |
528 | * | |
9cfc7bd6 SE |
529 | * Increases the returned router's refcount |
530 | */ | |
63b01037 SE |
531 | static struct neigh_node * |
532 | batadv_find_ifalter_router(struct orig_node *primary_orig, | |
533 | const struct hard_iface *recv_if) | |
55158629 LL |
534 | { |
535 | struct neigh_node *tmp_neigh_node; | |
536 | struct neigh_node *router = NULL, *first_candidate = NULL; | |
537 | ||
538 | rcu_read_lock(); | |
539 | list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list, | |
540 | bonding_list) { | |
541 | if (!first_candidate) | |
542 | first_candidate = tmp_neigh_node; | |
543 | ||
544 | /* recv_if == NULL on the first node. */ | |
545 | if (tmp_neigh_node->if_incoming == recv_if) | |
546 | continue; | |
547 | ||
548 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) | |
549 | continue; | |
550 | ||
551 | /* if we don't have a router yet | |
9cfc7bd6 SE |
552 | * or this one is better, choose it. |
553 | */ | |
55158629 LL |
554 | if ((!router) || |
555 | (tmp_neigh_node->tq_avg > router->tq_avg)) { | |
556 | /* decrement refcount of | |
9cfc7bd6 SE |
557 | * previously selected router |
558 | */ | |
55158629 | 559 | if (router) |
7d211efc | 560 | batadv_neigh_node_free_ref(router); |
55158629 LL |
561 | |
562 | router = tmp_neigh_node; | |
563 | atomic_inc_not_zero(&router->refcount); | |
564 | } | |
565 | ||
7d211efc | 566 | batadv_neigh_node_free_ref(tmp_neigh_node); |
55158629 LL |
567 | } |
568 | ||
569 | /* use the first candidate if nothing was found. */ | |
570 | if (!router && first_candidate && | |
571 | atomic_inc_not_zero(&first_candidate->refcount)) | |
572 | router = first_candidate; | |
573 | ||
574 | rcu_read_unlock(); | |
575 | return router; | |
576 | } | |
577 | ||
30d3c511 | 578 | int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if) |
a73105b8 AQ |
579 | { |
580 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
581 | struct tt_query_packet *tt_query; | |
f25bd58a | 582 | uint16_t tt_size; |
a73105b8 | 583 | struct ethhdr *ethhdr; |
1eda58bf | 584 | char tt_flag; |
a73105b8 AQ |
585 | |
586 | /* drop packet if it has not necessary minimum size */ | |
587 | if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet)))) | |
588 | goto out; | |
589 | ||
590 | /* I could need to modify it */ | |
591 | if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0) | |
592 | goto out; | |
593 | ||
594 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
595 | ||
596 | /* packet with unicast indication but broadcast recipient */ | |
597 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
598 | goto out; | |
599 | ||
600 | /* packet with broadcast sender address */ | |
601 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
602 | goto out; | |
603 | ||
604 | tt_query = (struct tt_query_packet *)skb->data; | |
605 | ||
a73105b8 AQ |
606 | switch (tt_query->flags & TT_QUERY_TYPE_MASK) { |
607 | case TT_REQUEST: | |
f8214865 MH |
608 | batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX); |
609 | ||
a73105b8 | 610 | /* If we cannot provide an answer the tt_request is |
9cfc7bd6 SE |
611 | * forwarded |
612 | */ | |
08c36d3e | 613 | if (!batadv_send_tt_response(bat_priv, tt_query)) { |
1eda58bf SE |
614 | tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.'; |
615 | batadv_dbg(DBG_TT, bat_priv, | |
616 | "Routing TT_REQUEST to %pM [%c]\n", | |
617 | tt_query->dst, | |
618 | tt_flag); | |
63b01037 | 619 | return batadv_route_unicast_packet(skb, recv_if); |
a73105b8 AQ |
620 | } |
621 | break; | |
622 | case TT_RESPONSE: | |
f8214865 MH |
623 | batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX); |
624 | ||
3193e8fd | 625 | if (batadv_is_my_mac(tt_query->dst)) { |
dc58fe32 | 626 | /* packet needs to be linearized to access the TT |
9cfc7bd6 SE |
627 | * changes |
628 | */ | |
dc58fe32 AQ |
629 | if (skb_linearize(skb) < 0) |
630 | goto out; | |
d2b6cc8e AQ |
631 | /* skb_linearize() possibly changed skb->data */ |
632 | tt_query = (struct tt_query_packet *)skb->data; | |
a73105b8 | 633 | |
08c36d3e | 634 | tt_size = batadv_tt_len(ntohs(tt_query->tt_data)); |
8b7342d6 AQ |
635 | |
636 | /* Ensure we have all the claimed data */ | |
637 | if (unlikely(skb_headlen(skb) < | |
f25bd58a | 638 | sizeof(struct tt_query_packet) + tt_size)) |
8b7342d6 AQ |
639 | goto out; |
640 | ||
08c36d3e | 641 | batadv_handle_tt_response(bat_priv, tt_query); |
dc58fe32 | 642 | } else { |
1eda58bf SE |
643 | tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.'; |
644 | batadv_dbg(DBG_TT, bat_priv, | |
645 | "Routing TT_RESPONSE to %pM [%c]\n", | |
646 | tt_query->dst, | |
647 | tt_flag); | |
63b01037 | 648 | return batadv_route_unicast_packet(skb, recv_if); |
a73105b8 AQ |
649 | } |
650 | break; | |
651 | } | |
652 | ||
653 | out: | |
654 | /* returning NET_RX_DROP will make the caller function kfree the skb */ | |
655 | return NET_RX_DROP; | |
656 | } | |
657 | ||
30d3c511 | 658 | int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if) |
cc47f66e AQ |
659 | { |
660 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
661 | struct roam_adv_packet *roam_adv_packet; | |
662 | struct orig_node *orig_node; | |
663 | struct ethhdr *ethhdr; | |
664 | ||
665 | /* drop packet if it has not necessary minimum size */ | |
666 | if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet)))) | |
667 | goto out; | |
668 | ||
669 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
670 | ||
671 | /* packet with unicast indication but broadcast recipient */ | |
672 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
673 | goto out; | |
674 | ||
675 | /* packet with broadcast sender address */ | |
676 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
677 | goto out; | |
678 | ||
f8214865 MH |
679 | batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX); |
680 | ||
cc47f66e AQ |
681 | roam_adv_packet = (struct roam_adv_packet *)skb->data; |
682 | ||
3193e8fd | 683 | if (!batadv_is_my_mac(roam_adv_packet->dst)) |
63b01037 | 684 | return batadv_route_unicast_packet(skb, recv_if); |
cc47f66e | 685 | |
20ff9d59 SW |
686 | /* check if it is a backbone gateway. we don't accept |
687 | * roaming advertisement from it, as it has the same | |
688 | * entries as we have. | |
689 | */ | |
08adf151 | 690 | if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src)) |
20ff9d59 SW |
691 | goto out; |
692 | ||
da641193 | 693 | orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src); |
cc47f66e AQ |
694 | if (!orig_node) |
695 | goto out; | |
696 | ||
1eda58bf SE |
697 | batadv_dbg(DBG_TT, bat_priv, |
698 | "Received ROAMING_ADV from %pM (client %pM)\n", | |
699 | roam_adv_packet->src, roam_adv_packet->client); | |
cc47f66e | 700 | |
08c36d3e SE |
701 | batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client, |
702 | atomic_read(&orig_node->last_ttvn) + 1, true, | |
703 | false); | |
cc47f66e AQ |
704 | |
705 | /* Roaming phase starts: I have new information but the ttvn has not | |
706 | * been incremented yet. This flag will make me check all the incoming | |
9cfc7bd6 SE |
707 | * packets for the correct destination. |
708 | */ | |
cc47f66e AQ |
709 | bat_priv->tt_poss_change = true; |
710 | ||
7d211efc | 711 | batadv_orig_node_free_ref(orig_node); |
cc47f66e AQ |
712 | out: |
713 | /* returning NET_RX_DROP will make the caller function kfree the skb */ | |
714 | return NET_RX_DROP; | |
715 | } | |
716 | ||
c6c8fea2 | 717 | /* find a suitable router for this originator, and use |
a4c135c5 | 718 | * bonding if possible. increases the found neighbors |
9cfc7bd6 SE |
719 | * refcount. |
720 | */ | |
30d3c511 SE |
721 | struct neigh_node *batadv_find_router(struct bat_priv *bat_priv, |
722 | struct orig_node *orig_node, | |
723 | const struct hard_iface *recv_if) | |
c6c8fea2 SE |
724 | { |
725 | struct orig_node *primary_orig_node; | |
726 | struct orig_node *router_orig; | |
55158629 | 727 | struct neigh_node *router; |
c6c8fea2 SE |
728 | static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0}; |
729 | int bonding_enabled; | |
da641193 | 730 | uint8_t *primary_addr; |
c6c8fea2 SE |
731 | |
732 | if (!orig_node) | |
733 | return NULL; | |
734 | ||
7d211efc | 735 | router = batadv_orig_node_get_router(orig_node); |
e1a5382f | 736 | if (!router) |
01df2b65 | 737 | goto err; |
c6c8fea2 SE |
738 | |
739 | /* without bonding, the first node should | |
9cfc7bd6 SE |
740 | * always choose the default router. |
741 | */ | |
c6c8fea2 SE |
742 | bonding_enabled = atomic_read(&bat_priv->bonding); |
743 | ||
a4c135c5 SW |
744 | rcu_read_lock(); |
745 | /* select default router to output */ | |
e1a5382f | 746 | router_orig = router->orig_node; |
01df2b65 ML |
747 | if (!router_orig) |
748 | goto err_unlock; | |
a4c135c5 | 749 | |
a4c135c5 SW |
750 | if ((!recv_if) && (!bonding_enabled)) |
751 | goto return_router; | |
c6c8fea2 | 752 | |
da641193 SE |
753 | primary_addr = router_orig->primary_addr; |
754 | ||
c6c8fea2 | 755 | /* if we have something in the primary_addr, we can search |
9cfc7bd6 SE |
756 | * for a potential bonding candidate. |
757 | */ | |
1eda58bf | 758 | if (batadv_compare_eth(primary_addr, zero_mac)) |
a4c135c5 | 759 | goto return_router; |
c6c8fea2 SE |
760 | |
761 | /* find the orig_node which has the primary interface. might | |
9cfc7bd6 SE |
762 | * even be the same as our router_orig in many cases |
763 | */ | |
1eda58bf | 764 | if (batadv_compare_eth(primary_addr, router_orig->orig)) { |
c6c8fea2 SE |
765 | primary_orig_node = router_orig; |
766 | } else { | |
da641193 SE |
767 | primary_orig_node = batadv_orig_hash_find(bat_priv, |
768 | primary_addr); | |
c6c8fea2 | 769 | if (!primary_orig_node) |
a4c135c5 | 770 | goto return_router; |
7aadf889 | 771 | |
7d211efc | 772 | batadv_orig_node_free_ref(primary_orig_node); |
c6c8fea2 SE |
773 | } |
774 | ||
775 | /* with less than 2 candidates, we can't do any | |
9cfc7bd6 SE |
776 | * bonding and prefer the original router. |
777 | */ | |
a4c135c5 SW |
778 | if (atomic_read(&primary_orig_node->bond_candidates) < 2) |
779 | goto return_router; | |
c6c8fea2 | 780 | |
c6c8fea2 SE |
781 | /* all nodes between should choose a candidate which |
782 | * is is not on the interface where the packet came | |
9cfc7bd6 SE |
783 | * in. |
784 | */ | |
7d211efc | 785 | batadv_neigh_node_free_ref(router); |
c6c8fea2 | 786 | |
55158629 | 787 | if (bonding_enabled) |
63b01037 | 788 | router = batadv_find_bond_router(primary_orig_node, recv_if); |
55158629 | 789 | else |
63b01037 | 790 | router = batadv_find_ifalter_router(primary_orig_node, recv_if); |
c6c8fea2 | 791 | |
a4c135c5 | 792 | return_router: |
e2cbc11c AQ |
793 | if (router && router->if_incoming->if_status != IF_ACTIVE) |
794 | goto err_unlock; | |
795 | ||
a4c135c5 | 796 | rcu_read_unlock(); |
c6c8fea2 | 797 | return router; |
01df2b65 ML |
798 | err_unlock: |
799 | rcu_read_unlock(); | |
800 | err: | |
801 | if (router) | |
7d211efc | 802 | batadv_neigh_node_free_ref(router); |
01df2b65 | 803 | return NULL; |
c6c8fea2 SE |
804 | } |
805 | ||
63b01037 | 806 | static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size) |
c6c8fea2 SE |
807 | { |
808 | struct ethhdr *ethhdr; | |
809 | ||
810 | /* drop packet if it has not necessary minimum size */ | |
811 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
812 | return -1; | |
813 | ||
814 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
815 | ||
816 | /* packet with unicast indication but broadcast recipient */ | |
817 | if (is_broadcast_ether_addr(ethhdr->h_dest)) | |
818 | return -1; | |
819 | ||
820 | /* packet with broadcast sender address */ | |
821 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
822 | return -1; | |
823 | ||
824 | /* not for me */ | |
3193e8fd | 825 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
c6c8fea2 SE |
826 | return -1; |
827 | ||
828 | return 0; | |
829 | } | |
830 | ||
63b01037 SE |
831 | static int batadv_route_unicast_packet(struct sk_buff *skb, |
832 | struct hard_iface *recv_if) | |
c6c8fea2 SE |
833 | { |
834 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
44524fcd ML |
835 | struct orig_node *orig_node = NULL; |
836 | struct neigh_node *neigh_node = NULL; | |
c6c8fea2 SE |
837 | struct unicast_packet *unicast_packet; |
838 | struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
44524fcd | 839 | int ret = NET_RX_DROP; |
c6c8fea2 SE |
840 | struct sk_buff *new_skb; |
841 | ||
842 | unicast_packet = (struct unicast_packet *)skb->data; | |
843 | ||
844 | /* TTL exceeded */ | |
76543d14 | 845 | if (unicast_packet->header.ttl < 2) { |
86ceb360 SE |
846 | pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n", |
847 | ethhdr->h_source, unicast_packet->dest); | |
44524fcd | 848 | goto out; |
c6c8fea2 SE |
849 | } |
850 | ||
851 | /* get routing information */ | |
da641193 | 852 | orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest); |
7aadf889 | 853 | |
44524fcd | 854 | if (!orig_node) |
b5a6f69c | 855 | goto out; |
c6c8fea2 | 856 | |
a4c135c5 | 857 | /* find_router() increases neigh_nodes refcount if found. */ |
30d3c511 | 858 | neigh_node = batadv_find_router(bat_priv, orig_node, recv_if); |
c6c8fea2 | 859 | |
d0072609 | 860 | if (!neigh_node) |
44524fcd | 861 | goto out; |
c6c8fea2 SE |
862 | |
863 | /* create a copy of the skb, if needed, to modify it. */ | |
0d125074 | 864 | if (skb_cow(skb, ETH_HLEN) < 0) |
44524fcd | 865 | goto out; |
c6c8fea2 SE |
866 | |
867 | unicast_packet = (struct unicast_packet *)skb->data; | |
868 | ||
76543d14 | 869 | if (unicast_packet->header.packet_type == BAT_UNICAST && |
c6c8fea2 | 870 | atomic_read(&bat_priv->fragmentation) && |
d0072609 | 871 | skb->len > neigh_node->if_incoming->net_dev->mtu) { |
88ed1e77 SE |
872 | ret = batadv_frag_send_skb(skb, bat_priv, |
873 | neigh_node->if_incoming, | |
874 | neigh_node->addr); | |
d0072609 ML |
875 | goto out; |
876 | } | |
c6c8fea2 | 877 | |
76543d14 | 878 | if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG && |
f0530ee5 SE |
879 | batadv_frag_can_reassemble(skb, |
880 | neigh_node->if_incoming->net_dev->mtu)) { | |
c6c8fea2 | 881 | |
88ed1e77 | 882 | ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); |
c6c8fea2 SE |
883 | |
884 | if (ret == NET_RX_DROP) | |
44524fcd | 885 | goto out; |
c6c8fea2 SE |
886 | |
887 | /* packet was buffered for late merge */ | |
44524fcd ML |
888 | if (!new_skb) { |
889 | ret = NET_RX_SUCCESS; | |
890 | goto out; | |
891 | } | |
c6c8fea2 SE |
892 | |
893 | skb = new_skb; | |
894 | unicast_packet = (struct unicast_packet *)skb->data; | |
895 | } | |
896 | ||
897 | /* decrement ttl */ | |
76543d14 | 898 | unicast_packet->header.ttl--; |
c6c8fea2 | 899 | |
f8214865 MH |
900 | /* Update stats counter */ |
901 | batadv_inc_counter(bat_priv, BAT_CNT_FORWARD); | |
902 | batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES, | |
903 | skb->len + ETH_HLEN); | |
904 | ||
c6c8fea2 | 905 | /* route it */ |
9455e34c | 906 | batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
44524fcd | 907 | ret = NET_RX_SUCCESS; |
c6c8fea2 | 908 | |
44524fcd ML |
909 | out: |
910 | if (neigh_node) | |
7d211efc | 911 | batadv_neigh_node_free_ref(neigh_node); |
44524fcd | 912 | if (orig_node) |
7d211efc | 913 | batadv_orig_node_free_ref(orig_node); |
44524fcd | 914 | return ret; |
c6c8fea2 SE |
915 | } |
916 | ||
63b01037 SE |
917 | static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv, |
918 | struct sk_buff *skb) { | |
a73105b8 AQ |
919 | uint8_t curr_ttvn; |
920 | struct orig_node *orig_node; | |
921 | struct ethhdr *ethhdr; | |
922 | struct hard_iface *primary_if; | |
923 | struct unicast_packet *unicast_packet; | |
cc47f66e | 924 | bool tt_poss_change; |
3e34819e | 925 | int is_old_ttvn; |
a73105b8 AQ |
926 | |
927 | /* I could need to modify it */ | |
928 | if (skb_cow(skb, sizeof(struct unicast_packet)) < 0) | |
929 | return 0; | |
930 | ||
931 | unicast_packet = (struct unicast_packet *)skb->data; | |
932 | ||
3193e8fd | 933 | if (batadv_is_my_mac(unicast_packet->dest)) { |
cc47f66e | 934 | tt_poss_change = bat_priv->tt_poss_change; |
a73105b8 | 935 | curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn); |
cc47f66e | 936 | } else { |
da641193 SE |
937 | orig_node = batadv_orig_hash_find(bat_priv, |
938 | unicast_packet->dest); | |
a73105b8 AQ |
939 | |
940 | if (!orig_node) | |
941 | return 0; | |
942 | ||
943 | curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn); | |
cc47f66e | 944 | tt_poss_change = orig_node->tt_poss_change; |
7d211efc | 945 | batadv_orig_node_free_ref(orig_node); |
a73105b8 AQ |
946 | } |
947 | ||
948 | /* Check whether I have to reroute the packet */ | |
3e34819e SE |
949 | is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn); |
950 | if (is_old_ttvn || tt_poss_change) { | |
8710e261 AQ |
951 | /* check if there is enough data before accessing it */ |
952 | if (pskb_may_pull(skb, sizeof(struct unicast_packet) + | |
953 | ETH_HLEN) < 0) | |
a73105b8 AQ |
954 | return 0; |
955 | ||
956 | ethhdr = (struct ethhdr *)(skb->data + | |
957 | sizeof(struct unicast_packet)); | |
3275e7cc AQ |
958 | |
959 | /* we don't have an updated route for this client, so we should | |
960 | * not try to reroute the packet!! | |
961 | */ | |
08c36d3e SE |
962 | if (batadv_tt_global_client_is_roaming(bat_priv, |
963 | ethhdr->h_dest)) | |
3275e7cc AQ |
964 | return 1; |
965 | ||
08c36d3e SE |
966 | orig_node = batadv_transtable_search(bat_priv, NULL, |
967 | ethhdr->h_dest); | |
a73105b8 AQ |
968 | |
969 | if (!orig_node) { | |
08c36d3e | 970 | if (!batadv_is_my_client(bat_priv, ethhdr->h_dest)) |
a73105b8 | 971 | return 0; |
e5d89254 | 972 | primary_if = batadv_primary_if_get_selected(bat_priv); |
a73105b8 AQ |
973 | if (!primary_if) |
974 | return 0; | |
975 | memcpy(unicast_packet->dest, | |
976 | primary_if->net_dev->dev_addr, ETH_ALEN); | |
e5d89254 | 977 | batadv_hardif_free_ref(primary_if); |
a73105b8 AQ |
978 | } else { |
979 | memcpy(unicast_packet->dest, orig_node->orig, | |
980 | ETH_ALEN); | |
981 | curr_ttvn = (uint8_t) | |
982 | atomic_read(&orig_node->last_ttvn); | |
7d211efc | 983 | batadv_orig_node_free_ref(orig_node); |
a73105b8 AQ |
984 | } |
985 | ||
1eda58bf SE |
986 | batadv_dbg(DBG_ROUTES, bat_priv, |
987 | "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n", | |
988 | unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest, | |
989 | unicast_packet->dest); | |
a73105b8 AQ |
990 | |
991 | unicast_packet->ttvn = curr_ttvn; | |
992 | } | |
993 | return 1; | |
994 | } | |
995 | ||
30d3c511 | 996 | int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
c6c8fea2 | 997 | { |
a73105b8 | 998 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); |
c6c8fea2 | 999 | struct unicast_packet *unicast_packet; |
704509b8 | 1000 | int hdr_size = sizeof(*unicast_packet); |
c6c8fea2 | 1001 | |
63b01037 | 1002 | if (batadv_check_unicast_packet(skb, hdr_size) < 0) |
c6c8fea2 SE |
1003 | return NET_RX_DROP; |
1004 | ||
63b01037 | 1005 | if (!batadv_check_unicast_ttvn(bat_priv, skb)) |
a73105b8 AQ |
1006 | return NET_RX_DROP; |
1007 | ||
c6c8fea2 SE |
1008 | unicast_packet = (struct unicast_packet *)skb->data; |
1009 | ||
1010 | /* packet for me */ | |
3193e8fd | 1011 | if (batadv_is_my_mac(unicast_packet->dest)) { |
04b482a2 SE |
1012 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, |
1013 | hdr_size); | |
c6c8fea2 SE |
1014 | return NET_RX_SUCCESS; |
1015 | } | |
1016 | ||
63b01037 | 1017 | return batadv_route_unicast_packet(skb, recv_if); |
c6c8fea2 SE |
1018 | } |
1019 | ||
30d3c511 SE |
1020 | int batadv_recv_ucast_frag_packet(struct sk_buff *skb, |
1021 | struct hard_iface *recv_if) | |
c6c8fea2 SE |
1022 | { |
1023 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
1024 | struct unicast_frag_packet *unicast_packet; | |
704509b8 | 1025 | int hdr_size = sizeof(*unicast_packet); |
c6c8fea2 SE |
1026 | struct sk_buff *new_skb = NULL; |
1027 | int ret; | |
1028 | ||
63b01037 | 1029 | if (batadv_check_unicast_packet(skb, hdr_size) < 0) |
c6c8fea2 SE |
1030 | return NET_RX_DROP; |
1031 | ||
63b01037 | 1032 | if (!batadv_check_unicast_ttvn(bat_priv, skb)) |
a73105b8 AQ |
1033 | return NET_RX_DROP; |
1034 | ||
c6c8fea2 SE |
1035 | unicast_packet = (struct unicast_frag_packet *)skb->data; |
1036 | ||
1037 | /* packet for me */ | |
3193e8fd | 1038 | if (batadv_is_my_mac(unicast_packet->dest)) { |
c6c8fea2 | 1039 | |
88ed1e77 | 1040 | ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb); |
c6c8fea2 SE |
1041 | |
1042 | if (ret == NET_RX_DROP) | |
1043 | return NET_RX_DROP; | |
1044 | ||
1045 | /* packet was buffered for late merge */ | |
1046 | if (!new_skb) | |
1047 | return NET_RX_SUCCESS; | |
1048 | ||
04b482a2 SE |
1049 | batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if, |
1050 | sizeof(struct unicast_packet)); | |
c6c8fea2 SE |
1051 | return NET_RX_SUCCESS; |
1052 | } | |
1053 | ||
63b01037 | 1054 | return batadv_route_unicast_packet(skb, recv_if); |
c6c8fea2 SE |
1055 | } |
1056 | ||
1057 | ||
30d3c511 | 1058 | int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
c6c8fea2 SE |
1059 | { |
1060 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
f3e0008f | 1061 | struct orig_node *orig_node = NULL; |
c6c8fea2 SE |
1062 | struct bcast_packet *bcast_packet; |
1063 | struct ethhdr *ethhdr; | |
704509b8 | 1064 | int hdr_size = sizeof(*bcast_packet); |
f3e0008f | 1065 | int ret = NET_RX_DROP; |
c6c8fea2 SE |
1066 | int32_t seq_diff; |
1067 | ||
1068 | /* drop packet if it has not necessary minimum size */ | |
1069 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
f3e0008f | 1070 | goto out; |
c6c8fea2 SE |
1071 | |
1072 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1073 | ||
1074 | /* packet with broadcast indication but unicast recipient */ | |
1075 | if (!is_broadcast_ether_addr(ethhdr->h_dest)) | |
f3e0008f | 1076 | goto out; |
c6c8fea2 SE |
1077 | |
1078 | /* packet with broadcast sender address */ | |
1079 | if (is_broadcast_ether_addr(ethhdr->h_source)) | |
f3e0008f | 1080 | goto out; |
c6c8fea2 SE |
1081 | |
1082 | /* ignore broadcasts sent by myself */ | |
3193e8fd | 1083 | if (batadv_is_my_mac(ethhdr->h_source)) |
f3e0008f | 1084 | goto out; |
c6c8fea2 SE |
1085 | |
1086 | bcast_packet = (struct bcast_packet *)skb->data; | |
1087 | ||
1088 | /* ignore broadcasts originated by myself */ | |
3193e8fd | 1089 | if (batadv_is_my_mac(bcast_packet->orig)) |
f3e0008f | 1090 | goto out; |
c6c8fea2 | 1091 | |
76543d14 | 1092 | if (bcast_packet->header.ttl < 2) |
f3e0008f | 1093 | goto out; |
c6c8fea2 | 1094 | |
da641193 | 1095 | orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig); |
f3e0008f ML |
1096 | |
1097 | if (!orig_node) | |
b5a6f69c | 1098 | goto out; |
c6c8fea2 | 1099 | |
f3e0008f | 1100 | spin_lock_bh(&orig_node->bcast_seqno_lock); |
c6c8fea2 SE |
1101 | |
1102 | /* check whether the packet is a duplicate */ | |
9b4a1159 SE |
1103 | if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno, |
1104 | ntohl(bcast_packet->seqno))) | |
f3e0008f | 1105 | goto spin_unlock; |
c6c8fea2 SE |
1106 | |
1107 | seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno; | |
1108 | ||
1109 | /* check whether the packet is old and the host just restarted. */ | |
30d3c511 SE |
1110 | if (batadv_window_protected(bat_priv, seq_diff, |
1111 | &orig_node->bcast_seqno_reset)) | |
f3e0008f | 1112 | goto spin_unlock; |
c6c8fea2 SE |
1113 | |
1114 | /* mark broadcast in flood history, update window position | |
9cfc7bd6 SE |
1115 | * if required. |
1116 | */ | |
0f5f9322 | 1117 | if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1)) |
c6c8fea2 SE |
1118 | orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno); |
1119 | ||
f3e0008f | 1120 | spin_unlock_bh(&orig_node->bcast_seqno_lock); |
f3e0008f | 1121 | |
fe2da6ff | 1122 | /* check whether this has been sent by another originator before */ |
08adf151 | 1123 | if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size)) |
fe2da6ff SW |
1124 | goto out; |
1125 | ||
c6c8fea2 | 1126 | /* rebroadcast packet */ |
9455e34c | 1127 | batadv_add_bcast_packet_to_list(bat_priv, skb, 1); |
c6c8fea2 | 1128 | |
23721387 SW |
1129 | /* don't hand the broadcast up if it is from an originator |
1130 | * from the same backbone. | |
1131 | */ | |
08adf151 | 1132 | if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size)) |
23721387 SW |
1133 | goto out; |
1134 | ||
c6c8fea2 | 1135 | /* broadcast for me */ |
04b482a2 | 1136 | batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size); |
f3e0008f ML |
1137 | ret = NET_RX_SUCCESS; |
1138 | goto out; | |
c6c8fea2 | 1139 | |
f3e0008f ML |
1140 | spin_unlock: |
1141 | spin_unlock_bh(&orig_node->bcast_seqno_lock); | |
f3e0008f ML |
1142 | out: |
1143 | if (orig_node) | |
7d211efc | 1144 | batadv_orig_node_free_ref(orig_node); |
f3e0008f | 1145 | return ret; |
c6c8fea2 SE |
1146 | } |
1147 | ||
30d3c511 | 1148 | int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if) |
c6c8fea2 SE |
1149 | { |
1150 | struct vis_packet *vis_packet; | |
1151 | struct ethhdr *ethhdr; | |
1152 | struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface); | |
704509b8 | 1153 | int hdr_size = sizeof(*vis_packet); |
c6c8fea2 SE |
1154 | |
1155 | /* keep skb linear */ | |
1156 | if (skb_linearize(skb) < 0) | |
1157 | return NET_RX_DROP; | |
1158 | ||
1159 | if (unlikely(!pskb_may_pull(skb, hdr_size))) | |
1160 | return NET_RX_DROP; | |
1161 | ||
1162 | vis_packet = (struct vis_packet *)skb->data; | |
1163 | ethhdr = (struct ethhdr *)skb_mac_header(skb); | |
1164 | ||
1165 | /* not for me */ | |
3193e8fd | 1166 | if (!batadv_is_my_mac(ethhdr->h_dest)) |
c6c8fea2 SE |
1167 | return NET_RX_DROP; |
1168 | ||
1169 | /* ignore own packets */ | |
3193e8fd | 1170 | if (batadv_is_my_mac(vis_packet->vis_orig)) |
c6c8fea2 SE |
1171 | return NET_RX_DROP; |
1172 | ||
3193e8fd | 1173 | if (batadv_is_my_mac(vis_packet->sender_orig)) |
c6c8fea2 SE |
1174 | return NET_RX_DROP; |
1175 | ||
1176 | switch (vis_packet->vis_type) { | |
1177 | case VIS_TYPE_SERVER_SYNC: | |
d0f714f4 SE |
1178 | batadv_receive_server_sync_packet(bat_priv, vis_packet, |
1179 | skb_headlen(skb)); | |
c6c8fea2 SE |
1180 | break; |
1181 | ||
1182 | case VIS_TYPE_CLIENT_UPDATE: | |
d0f714f4 SE |
1183 | batadv_receive_client_update_packet(bat_priv, vis_packet, |
1184 | skb_headlen(skb)); | |
c6c8fea2 SE |
1185 | break; |
1186 | ||
1187 | default: /* ignore unknown packet */ | |
1188 | break; | |
1189 | } | |
1190 | ||
1191 | /* We take a copy of the data in the packet, so we should | |
9cfc7bd6 SE |
1192 | * always free the skbuf. |
1193 | */ | |
c6c8fea2 SE |
1194 | return NET_RX_DROP; |
1195 | } |