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