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