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