Commit | Line | Data |
---|---|---|
0b873931 | 1 | /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors: |
fc957275 ML |
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 | |
fc957275 ML |
18 | */ |
19 | ||
20 | #include "main.h" | |
fc957275 | 21 | #include "translation-table.h" |
fc957275 ML |
22 | #include "originator.h" |
23 | #include "routing.h" | |
24 | #include "gateway_common.h" | |
25 | #include "gateway_client.h" | |
26 | #include "hard-interface.h" | |
27 | #include "send.h" | |
1c280471 | 28 | #include "bat_algo.h" |
d56b1705 | 29 | #include "network-coding.h" |
fc957275 | 30 | |
791c2a2d AQ |
31 | |
32 | /** | |
33 | * batadv_dup_status - duplicate status | |
34 | * @BATADV_NO_DUP: the packet is a duplicate | |
35 | * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the | |
36 | * neighbor) | |
37 | * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor | |
38 | * @BATADV_PROTECTED: originator is currently protected (after reboot) | |
39 | */ | |
40 | enum batadv_dup_status { | |
41 | BATADV_NO_DUP = 0, | |
42 | BATADV_ORIG_DUP, | |
43 | BATADV_NEIGH_DUP, | |
44 | BATADV_PROTECTED, | |
45 | }; | |
46 | ||
24a5deeb AQ |
47 | /** |
48 | * batadv_ring_buffer_set - update the ring buffer with the given value | |
49 | * @lq_recv: pointer to the ring buffer | |
50 | * @lq_index: index to store the value at | |
51 | * @value: value to store in the ring buffer | |
52 | */ | |
53 | static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index, | |
54 | uint8_t value) | |
55 | { | |
56 | lq_recv[*lq_index] = value; | |
57 | *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE; | |
58 | } | |
59 | ||
60 | /** | |
61 | * batadv_ring_buffer_set - compute the average of all non-zero values stored | |
62 | * in the given ring buffer | |
63 | * @lq_recv: pointer to the ring buffer | |
64 | * | |
65 | * Returns computed average value. | |
66 | */ | |
67 | static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[]) | |
68 | { | |
69 | const uint8_t *ptr; | |
70 | uint16_t count = 0, i = 0, sum = 0; | |
71 | ||
72 | ptr = lq_recv; | |
73 | ||
74 | while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) { | |
75 | if (*ptr != 0) { | |
76 | count++; | |
77 | sum += *ptr; | |
78 | } | |
79 | ||
80 | i++; | |
81 | ptr++; | |
82 | } | |
83 | ||
84 | if (count == 0) | |
85 | return 0; | |
86 | ||
87 | return (uint8_t)(sum / count); | |
88 | } | |
d98cae64 | 89 | |
d0015fdd AQ |
90 | /** |
91 | * batadv_iv_ogm_orig_free - free the private resources allocated for this | |
92 | * orig_node | |
93 | * @orig_node: the orig_node for which the resources have to be free'd | |
94 | */ | |
95 | static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node) | |
96 | { | |
97 | kfree(orig_node->bat_iv.bcast_own); | |
98 | kfree(orig_node->bat_iv.bcast_own_sum); | |
99 | } | |
100 | ||
101 | /** | |
102 | * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to | |
103 | * include the new hard-interface | |
104 | * @orig_node: the orig_node that has to be changed | |
105 | * @max_if_num: the current amount of interfaces | |
106 | * | |
107 | * Returns 0 on success, a negative error code otherwise. | |
108 | */ | |
109 | static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node, | |
110 | int max_if_num) | |
111 | { | |
112 | void *data_ptr; | |
113 | size_t data_size, old_size; | |
114 | int ret = -ENOMEM; | |
115 | ||
116 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); | |
117 | ||
118 | data_size = max_if_num * sizeof(unsigned long) * BATADV_NUM_WORDS; | |
119 | old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS; | |
120 | data_ptr = kmalloc(data_size, GFP_ATOMIC); | |
121 | if (!data_ptr) | |
122 | goto unlock; | |
123 | ||
124 | memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size); | |
125 | kfree(orig_node->bat_iv.bcast_own); | |
126 | orig_node->bat_iv.bcast_own = data_ptr; | |
127 | ||
128 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
129 | if (!data_ptr) { | |
130 | kfree(orig_node->bat_iv.bcast_own); | |
131 | goto unlock; | |
132 | } | |
133 | ||
134 | memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, | |
135 | (max_if_num - 1) * sizeof(uint8_t)); | |
136 | kfree(orig_node->bat_iv.bcast_own_sum); | |
137 | orig_node->bat_iv.bcast_own_sum = data_ptr; | |
138 | ||
139 | ret = 0; | |
140 | ||
141 | unlock: | |
142 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); | |
143 | ||
144 | return ret; | |
145 | } | |
146 | ||
147 | /** | |
148 | * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to | |
149 | * exclude the removed interface | |
150 | * @orig_node: the orig_node that has to be changed | |
151 | * @max_if_num: the current amount of interfaces | |
152 | * @del_if_num: the index of the interface being removed | |
153 | * | |
154 | * Returns 0 on success, a negative error code otherwise. | |
155 | */ | |
156 | static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node, | |
157 | int max_if_num, int del_if_num) | |
158 | { | |
159 | int chunk_size, ret = -ENOMEM, if_offset; | |
160 | void *data_ptr = NULL; | |
161 | ||
162 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); | |
163 | ||
164 | /* last interface was removed */ | |
165 | if (max_if_num == 0) | |
166 | goto free_bcast_own; | |
167 | ||
168 | chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS; | |
169 | data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC); | |
170 | if (!data_ptr) | |
171 | goto unlock; | |
172 | ||
173 | /* copy first part */ | |
174 | memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size); | |
175 | ||
176 | /* copy second part */ | |
177 | memcpy((char *)data_ptr + del_if_num * chunk_size, | |
178 | orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size), | |
179 | (max_if_num - del_if_num) * chunk_size); | |
180 | ||
181 | free_bcast_own: | |
182 | kfree(orig_node->bat_iv.bcast_own); | |
183 | orig_node->bat_iv.bcast_own = data_ptr; | |
184 | ||
185 | if (max_if_num == 0) | |
186 | goto free_own_sum; | |
187 | ||
188 | data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC); | |
189 | if (!data_ptr) { | |
190 | kfree(orig_node->bat_iv.bcast_own); | |
191 | goto unlock; | |
192 | } | |
193 | ||
194 | memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum, | |
195 | del_if_num * sizeof(uint8_t)); | |
196 | ||
197 | if_offset = (del_if_num + 1) * sizeof(uint8_t); | |
198 | memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t), | |
199 | orig_node->bat_iv.bcast_own_sum + if_offset, | |
200 | (max_if_num - del_if_num) * sizeof(uint8_t)); | |
201 | ||
202 | free_own_sum: | |
203 | kfree(orig_node->bat_iv.bcast_own_sum); | |
204 | orig_node->bat_iv.bcast_own_sum = data_ptr; | |
205 | ||
206 | ret = 0; | |
207 | unlock: | |
208 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
bbad0a5e AQ |
213 | /** |
214 | * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator | |
215 | * @bat_priv: the bat priv with all the soft interface information | |
216 | * @addr: mac address of the originator | |
217 | * | |
218 | * Returns the originator object corresponding to the passed mac address or NULL | |
219 | * on failure. | |
220 | * If the object does not exists it is created an initialised. | |
221 | */ | |
222 | static struct batadv_orig_node * | |
223 | batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr) | |
224 | { | |
225 | struct batadv_orig_node *orig_node; | |
226 | int size, hash_added; | |
227 | ||
228 | orig_node = batadv_orig_hash_find(bat_priv, addr); | |
229 | if (orig_node) | |
230 | return orig_node; | |
231 | ||
232 | orig_node = batadv_orig_node_new(bat_priv, addr); | |
233 | if (!orig_node) | |
234 | return NULL; | |
235 | ||
236 | spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock); | |
237 | ||
238 | size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS; | |
239 | orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC); | |
240 | if (!orig_node->bat_iv.bcast_own) | |
241 | goto free_orig_node; | |
242 | ||
243 | size = bat_priv->num_ifaces * sizeof(uint8_t); | |
244 | orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC); | |
245 | if (!orig_node->bat_iv.bcast_own_sum) | |
246 | goto free_bcast_own; | |
247 | ||
248 | hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig, | |
249 | batadv_choose_orig, orig_node, | |
250 | &orig_node->hash_entry); | |
251 | if (hash_added != 0) | |
252 | goto free_bcast_own; | |
253 | ||
254 | return orig_node; | |
255 | ||
256 | free_bcast_own: | |
257 | kfree(orig_node->bat_iv.bcast_own); | |
258 | free_orig_node: | |
259 | batadv_orig_node_free_ref(orig_node); | |
260 | ||
261 | return NULL; | |
262 | } | |
263 | ||
56303d34 SE |
264 | static struct batadv_neigh_node * |
265 | batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface, | |
266 | const uint8_t *neigh_addr, | |
267 | struct batadv_orig_node *orig_node, | |
863dd7a8 | 268 | struct batadv_orig_node *orig_neigh) |
7ae8b285 | 269 | { |
0538f759 | 270 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
56303d34 | 271 | struct batadv_neigh_node *neigh_node; |
7ae8b285 | 272 | |
0538f759 | 273 | neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node); |
7ae8b285 ML |
274 | if (!neigh_node) |
275 | goto out; | |
276 | ||
0538f759 | 277 | spin_lock_init(&neigh_node->bat_iv.lq_update_lock); |
7ae8b285 | 278 | |
0538f759 AQ |
279 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
280 | "Creating new neighbor %pM for orig_node %pM on interface %s\n", | |
281 | neigh_addr, orig_node->orig, hard_iface->net_dev->name); | |
7ae8b285 ML |
282 | |
283 | spin_lock_bh(&orig_node->neigh_list_lock); | |
284 | hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list); | |
285 | spin_unlock_bh(&orig_node->neigh_list_lock); | |
286 | ||
287 | out: | |
288 | return neigh_node; | |
289 | } | |
290 | ||
56303d34 | 291 | static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface) |
d0b9fd89 | 292 | { |
96412690 | 293 | struct batadv_ogm_packet *batadv_ogm_packet; |
14511519 | 294 | unsigned char *ogm_buff; |
d7d32ec0 | 295 | uint32_t random_seqno; |
5346c35e | 296 | int res = -ENOMEM; |
d7d32ec0 ML |
297 | |
298 | /* randomize initial seqno to avoid collision */ | |
299 | get_random_bytes(&random_seqno, sizeof(random_seqno)); | |
14511519 | 300 | atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno); |
d0b9fd89 | 301 | |
14511519 ML |
302 | hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN; |
303 | ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC); | |
304 | if (!ogm_buff) | |
77af7575 ML |
305 | goto out; |
306 | ||
14511519 ML |
307 | hard_iface->bat_iv.ogm_buff = ogm_buff; |
308 | ||
309 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; | |
96412690 SE |
310 | batadv_ogm_packet->header.packet_type = BATADV_IV_OGM; |
311 | batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION; | |
312 | batadv_ogm_packet->header.ttl = 2; | |
313 | batadv_ogm_packet->flags = BATADV_NO_FLAGS; | |
414254e3 | 314 | batadv_ogm_packet->reserved = 0; |
96412690 | 315 | batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE; |
77af7575 ML |
316 | |
317 | res = 0; | |
318 | ||
319 | out: | |
320 | return res; | |
d0b9fd89 ML |
321 | } |
322 | ||
56303d34 | 323 | static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface) |
00a50076 | 324 | { |
14511519 ML |
325 | kfree(hard_iface->bat_iv.ogm_buff); |
326 | hard_iface->bat_iv.ogm_buff = NULL; | |
00a50076 ML |
327 | } |
328 | ||
56303d34 | 329 | static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface) |
d0b9fd89 | 330 | { |
96412690 | 331 | struct batadv_ogm_packet *batadv_ogm_packet; |
14511519 | 332 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
d0b9fd89 | 333 | |
14511519 | 334 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
96412690 | 335 | memcpy(batadv_ogm_packet->orig, |
c3229398 | 336 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
96412690 | 337 | memcpy(batadv_ogm_packet->prev_sender, |
c3229398 | 338 | hard_iface->net_dev->dev_addr, ETH_ALEN); |
d0b9fd89 ML |
339 | } |
340 | ||
56303d34 SE |
341 | static void |
342 | batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface) | |
d0b9fd89 | 343 | { |
96412690 | 344 | struct batadv_ogm_packet *batadv_ogm_packet; |
14511519 | 345 | unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff; |
d0b9fd89 | 346 | |
14511519 | 347 | batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff; |
96412690 SE |
348 | batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP; |
349 | batadv_ogm_packet->header.ttl = BATADV_TTL; | |
d0b9fd89 ML |
350 | } |
351 | ||
b9dacc52 | 352 | /* when do we schedule our own ogm to be sent */ |
fe8bc396 | 353 | static unsigned long |
56303d34 | 354 | batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv) |
b9dacc52 | 355 | { |
42d0b044 SE |
356 | unsigned int msecs; |
357 | ||
358 | msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER; | |
e76e4320 | 359 | msecs += prandom_u32() % (2 * BATADV_JITTER); |
42d0b044 SE |
360 | |
361 | return jiffies + msecs_to_jiffies(msecs); | |
b9dacc52 ML |
362 | } |
363 | ||
364 | /* when do we schedule a ogm packet to be sent */ | |
fe8bc396 | 365 | static unsigned long batadv_iv_ogm_fwd_send_time(void) |
b9dacc52 | 366 | { |
e76e4320 | 367 | return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2)); |
b9dacc52 ML |
368 | } |
369 | ||
370 | /* apply hop penalty for a normal link */ | |
56303d34 SE |
371 | static uint8_t batadv_hop_penalty(uint8_t tq, |
372 | const struct batadv_priv *bat_priv) | |
b9dacc52 ML |
373 | { |
374 | int hop_penalty = atomic_read(&bat_priv->hop_penalty); | |
42d0b044 SE |
375 | int new_tq; |
376 | ||
377 | new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty); | |
378 | new_tq /= BATADV_TQ_MAX_VALUE; | |
379 | ||
380 | return new_tq; | |
b9dacc52 ML |
381 | } |
382 | ||
fc957275 | 383 | /* is there another aggregated packet here? */ |
fe8bc396 | 384 | static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len, |
ef261577 | 385 | __be16 tvlv_len) |
fc957275 | 386 | { |
08c36d3e SE |
387 | int next_buff_pos = 0; |
388 | ||
7e071c79 | 389 | next_buff_pos += buff_pos + BATADV_OGM_HLEN; |
ef261577 | 390 | next_buff_pos += ntohs(tvlv_len); |
fc957275 ML |
391 | |
392 | return (next_buff_pos <= packet_len) && | |
42d0b044 | 393 | (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES); |
fc957275 ML |
394 | } |
395 | ||
b9dacc52 | 396 | /* send a batman ogm to a given interface */ |
56303d34 SE |
397 | static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet, |
398 | struct batadv_hard_iface *hard_iface) | |
b9dacc52 | 399 | { |
56303d34 | 400 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
b9dacc52 ML |
401 | char *fwd_str; |
402 | uint8_t packet_num; | |
403 | int16_t buff_pos; | |
96412690 | 404 | struct batadv_ogm_packet *batadv_ogm_packet; |
b9dacc52 | 405 | struct sk_buff *skb; |
c67893d1 | 406 | uint8_t *packet_pos; |
b9dacc52 | 407 | |
e9a4f295 | 408 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
b9dacc52 ML |
409 | return; |
410 | ||
411 | packet_num = 0; | |
412 | buff_pos = 0; | |
c67893d1 SE |
413 | packet_pos = forw_packet->skb->data; |
414 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; | |
b9dacc52 ML |
415 | |
416 | /* adjust all flags and log packets */ | |
fe8bc396 | 417 | while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len, |
ef261577 | 418 | batadv_ogm_packet->tvlv_len)) { |
b9dacc52 | 419 | /* we might have aggregated direct link packets with an |
9cfc7bd6 SE |
420 | * ordinary base packet |
421 | */ | |
8de47de5 SE |
422 | if (forw_packet->direct_link_flags & BIT(packet_num) && |
423 | forw_packet->if_incoming == hard_iface) | |
96412690 | 424 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
b9dacc52 | 425 | else |
96412690 | 426 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
b9dacc52 | 427 | |
c67893d1 SE |
428 | if (packet_num > 0 || !forw_packet->own) |
429 | fwd_str = "Forwarding"; | |
430 | else | |
431 | fwd_str = "Sending own"; | |
432 | ||
39c75a51 | 433 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
e1bf0c14 | 434 | "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n", |
1eda58bf | 435 | fwd_str, (packet_num > 0 ? "aggregated " : ""), |
96412690 SE |
436 | batadv_ogm_packet->orig, |
437 | ntohl(batadv_ogm_packet->seqno), | |
438 | batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl, | |
439 | (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? | |
1eda58bf | 440 | "on" : "off"), |
e1bf0c14 | 441 | hard_iface->net_dev->name, |
1eda58bf | 442 | hard_iface->net_dev->dev_addr); |
b9dacc52 | 443 | |
7e071c79 | 444 | buff_pos += BATADV_OGM_HLEN; |
ef261577 | 445 | buff_pos += ntohs(batadv_ogm_packet->tvlv_len); |
b9dacc52 | 446 | packet_num++; |
c67893d1 SE |
447 | packet_pos = forw_packet->skb->data + buff_pos; |
448 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; | |
b9dacc52 ML |
449 | } |
450 | ||
451 | /* create clone because function is called more than once */ | |
452 | skb = skb_clone(forw_packet->skb, GFP_ATOMIC); | |
f8214865 | 453 | if (skb) { |
d69909d2 SE |
454 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX); |
455 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES, | |
f8214865 | 456 | skb->len + ETH_HLEN); |
3193e8fd | 457 | batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr); |
f8214865 | 458 | } |
b9dacc52 ML |
459 | } |
460 | ||
461 | /* send a batman ogm packet */ | |
56303d34 | 462 | static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet) |
b9dacc52 | 463 | { |
56303d34 | 464 | struct batadv_hard_iface *hard_iface; |
b9dacc52 | 465 | struct net_device *soft_iface; |
56303d34 SE |
466 | struct batadv_priv *bat_priv; |
467 | struct batadv_hard_iface *primary_if = NULL; | |
96412690 | 468 | struct batadv_ogm_packet *batadv_ogm_packet; |
b9dacc52 | 469 | unsigned char directlink; |
c67893d1 | 470 | uint8_t *packet_pos; |
b9dacc52 | 471 | |
c67893d1 SE |
472 | packet_pos = forw_packet->skb->data; |
473 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; | |
96412690 | 474 | directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0); |
b9dacc52 ML |
475 | |
476 | if (!forw_packet->if_incoming) { | |
86ceb360 | 477 | pr_err("Error - can't forward packet: incoming iface not specified\n"); |
b9dacc52 ML |
478 | goto out; |
479 | } | |
480 | ||
481 | soft_iface = forw_packet->if_incoming->soft_iface; | |
482 | bat_priv = netdev_priv(soft_iface); | |
483 | ||
e9a4f295 | 484 | if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE) |
b9dacc52 ML |
485 | goto out; |
486 | ||
e5d89254 | 487 | primary_if = batadv_primary_if_get_selected(bat_priv); |
b9dacc52 ML |
488 | if (!primary_if) |
489 | goto out; | |
490 | ||
9cfc7bd6 SE |
491 | /* multihomed peer assumed |
492 | * non-primary OGMs are only broadcasted on their interface | |
493 | */ | |
96412690 | 494 | if ((directlink && (batadv_ogm_packet->header.ttl == 1)) || |
b9dacc52 | 495 | (forw_packet->own && (forw_packet->if_incoming != primary_if))) { |
b9dacc52 | 496 | /* FIXME: what about aggregated packets ? */ |
39c75a51 | 497 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
498 | "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n", |
499 | (forw_packet->own ? "Sending own" : "Forwarding"), | |
96412690 SE |
500 | batadv_ogm_packet->orig, |
501 | ntohl(batadv_ogm_packet->seqno), | |
502 | batadv_ogm_packet->header.ttl, | |
1eda58bf SE |
503 | forw_packet->if_incoming->net_dev->name, |
504 | forw_packet->if_incoming->net_dev->dev_addr); | |
b9dacc52 ML |
505 | |
506 | /* skb is only used once and than forw_packet is free'd */ | |
9455e34c SE |
507 | batadv_send_skb_packet(forw_packet->skb, |
508 | forw_packet->if_incoming, | |
3193e8fd | 509 | batadv_broadcast_addr); |
b9dacc52 ML |
510 | forw_packet->skb = NULL; |
511 | ||
512 | goto out; | |
513 | } | |
514 | ||
515 | /* broadcast on every interface */ | |
516 | rcu_read_lock(); | |
3193e8fd | 517 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
b9dacc52 ML |
518 | if (hard_iface->soft_iface != soft_iface) |
519 | continue; | |
520 | ||
fe8bc396 | 521 | batadv_iv_ogm_send_to_if(forw_packet, hard_iface); |
b9dacc52 ML |
522 | } |
523 | rcu_read_unlock(); | |
524 | ||
525 | out: | |
526 | if (primary_if) | |
e5d89254 | 527 | batadv_hardif_free_ref(primary_if); |
b9dacc52 ML |
528 | } |
529 | ||
530 | /* return true if new_packet can be aggregated with forw_packet */ | |
fe8bc396 | 531 | static bool |
96412690 | 532 | batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet, |
56303d34 | 533 | struct batadv_priv *bat_priv, |
fe8bc396 SE |
534 | int packet_len, unsigned long send_time, |
535 | bool directlink, | |
56303d34 SE |
536 | const struct batadv_hard_iface *if_incoming, |
537 | const struct batadv_forw_packet *forw_packet) | |
b9dacc52 | 538 | { |
96412690 | 539 | struct batadv_ogm_packet *batadv_ogm_packet; |
b9dacc52 | 540 | int aggregated_bytes = forw_packet->packet_len + packet_len; |
56303d34 | 541 | struct batadv_hard_iface *primary_if = NULL; |
b9dacc52 | 542 | bool res = false; |
42d0b044 | 543 | unsigned long aggregation_end_time; |
b9dacc52 | 544 | |
96412690 | 545 | batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data; |
42d0b044 SE |
546 | aggregation_end_time = send_time; |
547 | aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); | |
b9dacc52 | 548 | |
9cfc7bd6 | 549 | /* we can aggregate the current packet to this aggregated packet |
b9dacc52 ML |
550 | * if: |
551 | * | |
552 | * - the send time is within our MAX_AGGREGATION_MS time | |
553 | * - the resulting packet wont be bigger than | |
554 | * MAX_AGGREGATION_BYTES | |
555 | */ | |
b9dacc52 | 556 | if (time_before(send_time, forw_packet->send_time) && |
42d0b044 SE |
557 | time_after_eq(aggregation_end_time, forw_packet->send_time) && |
558 | (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) { | |
9cfc7bd6 | 559 | /* check aggregation compatibility |
b9dacc52 ML |
560 | * -> direct link packets are broadcasted on |
561 | * their interface only | |
562 | * -> aggregate packet if the current packet is | |
563 | * a "global" packet as well as the base | |
564 | * packet | |
565 | */ | |
e5d89254 | 566 | primary_if = batadv_primary_if_get_selected(bat_priv); |
b9dacc52 ML |
567 | if (!primary_if) |
568 | goto out; | |
569 | ||
570 | /* packets without direct link flag and high TTL | |
9cfc7bd6 SE |
571 | * are flooded through the net |
572 | */ | |
b9dacc52 | 573 | if ((!directlink) && |
96412690 SE |
574 | (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) && |
575 | (batadv_ogm_packet->header.ttl != 1) && | |
b9dacc52 ML |
576 | |
577 | /* own packets originating non-primary | |
9cfc7bd6 SE |
578 | * interfaces leave only that interface |
579 | */ | |
b9dacc52 ML |
580 | ((!forw_packet->own) || |
581 | (forw_packet->if_incoming == primary_if))) { | |
582 | res = true; | |
583 | goto out; | |
584 | } | |
585 | ||
586 | /* if the incoming packet is sent via this one | |
9cfc7bd6 SE |
587 | * interface only - we still can aggregate |
588 | */ | |
b9dacc52 | 589 | if ((directlink) && |
fe8bc396 | 590 | (new_bat_ogm_packet->header.ttl == 1) && |
b9dacc52 ML |
591 | (forw_packet->if_incoming == if_incoming) && |
592 | ||
593 | /* packets from direct neighbors or | |
594 | * own secondary interface packets | |
9cfc7bd6 SE |
595 | * (= secondary interface packets in general) |
596 | */ | |
96412690 | 597 | (batadv_ogm_packet->flags & BATADV_DIRECTLINK || |
b9dacc52 ML |
598 | (forw_packet->own && |
599 | forw_packet->if_incoming != primary_if))) { | |
600 | res = true; | |
601 | goto out; | |
602 | } | |
603 | } | |
604 | ||
605 | out: | |
606 | if (primary_if) | |
e5d89254 | 607 | batadv_hardif_free_ref(primary_if); |
b9dacc52 ML |
608 | return res; |
609 | } | |
610 | ||
611 | /* create a new aggregated packet and add this packet to it */ | |
fe8bc396 SE |
612 | static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff, |
613 | int packet_len, unsigned long send_time, | |
614 | bool direct_link, | |
56303d34 | 615 | struct batadv_hard_iface *if_incoming, |
fe8bc396 | 616 | int own_packet) |
b9dacc52 | 617 | { |
56303d34 SE |
618 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
619 | struct batadv_forw_packet *forw_packet_aggr; | |
b9dacc52 | 620 | unsigned char *skb_buff; |
42d0b044 | 621 | unsigned int skb_size; |
b9dacc52 ML |
622 | |
623 | if (!atomic_inc_not_zero(&if_incoming->refcount)) | |
624 | return; | |
625 | ||
626 | /* own packet should always be scheduled */ | |
627 | if (!own_packet) { | |
3e34819e | 628 | if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) { |
39c75a51 | 629 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 630 | "batman packet queue full\n"); |
b9dacc52 ML |
631 | goto out; |
632 | } | |
633 | } | |
634 | ||
635 | forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC); | |
636 | if (!forw_packet_aggr) { | |
637 | if (!own_packet) | |
638 | atomic_inc(&bat_priv->batman_queue_left); | |
639 | goto out; | |
640 | } | |
641 | ||
642 | if ((atomic_read(&bat_priv->aggregated_ogms)) && | |
42d0b044 | 643 | (packet_len < BATADV_MAX_AGGREGATION_BYTES)) |
5b246574 | 644 | skb_size = BATADV_MAX_AGGREGATION_BYTES; |
b9dacc52 | 645 | else |
5b246574 SE |
646 | skb_size = packet_len; |
647 | ||
41ab6c48 | 648 | skb_size += ETH_HLEN; |
b9dacc52 | 649 | |
41ab6c48 | 650 | forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size); |
b9dacc52 ML |
651 | if (!forw_packet_aggr->skb) { |
652 | if (!own_packet) | |
653 | atomic_inc(&bat_priv->batman_queue_left); | |
654 | kfree(forw_packet_aggr); | |
655 | goto out; | |
656 | } | |
c54f38c9 | 657 | forw_packet_aggr->skb->priority = TC_PRIO_CONTROL; |
41ab6c48 | 658 | skb_reserve(forw_packet_aggr->skb, ETH_HLEN); |
b9dacc52 | 659 | |
b9dacc52 ML |
660 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); |
661 | forw_packet_aggr->packet_len = packet_len; | |
662 | memcpy(skb_buff, packet_buff, packet_len); | |
663 | ||
664 | forw_packet_aggr->own = own_packet; | |
665 | forw_packet_aggr->if_incoming = if_incoming; | |
666 | forw_packet_aggr->num_packets = 0; | |
42d0b044 | 667 | forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS; |
b9dacc52 ML |
668 | forw_packet_aggr->send_time = send_time; |
669 | ||
670 | /* save packet direct link flag status */ | |
671 | if (direct_link) | |
672 | forw_packet_aggr->direct_link_flags |= 1; | |
673 | ||
674 | /* add new packet to packet list */ | |
675 | spin_lock_bh(&bat_priv->forw_bat_list_lock); | |
676 | hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list); | |
677 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); | |
678 | ||
679 | /* start timer for this packet */ | |
680 | INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work, | |
9455e34c | 681 | batadv_send_outstanding_bat_ogm_packet); |
3193e8fd | 682 | queue_delayed_work(batadv_event_workqueue, |
b9dacc52 ML |
683 | &forw_packet_aggr->delayed_work, |
684 | send_time - jiffies); | |
685 | ||
686 | return; | |
687 | out: | |
e5d89254 | 688 | batadv_hardif_free_ref(if_incoming); |
b9dacc52 ML |
689 | } |
690 | ||
691 | /* aggregate a new packet into the existing ogm packet */ | |
56303d34 | 692 | static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr, |
fe8bc396 SE |
693 | const unsigned char *packet_buff, |
694 | int packet_len, bool direct_link) | |
b9dacc52 ML |
695 | { |
696 | unsigned char *skb_buff; | |
8de47de5 | 697 | unsigned long new_direct_link_flag; |
b9dacc52 ML |
698 | |
699 | skb_buff = skb_put(forw_packet_aggr->skb, packet_len); | |
700 | memcpy(skb_buff, packet_buff, packet_len); | |
701 | forw_packet_aggr->packet_len += packet_len; | |
702 | forw_packet_aggr->num_packets++; | |
703 | ||
704 | /* save packet direct link flag status */ | |
8de47de5 SE |
705 | if (direct_link) { |
706 | new_direct_link_flag = BIT(forw_packet_aggr->num_packets); | |
707 | forw_packet_aggr->direct_link_flags |= new_direct_link_flag; | |
708 | } | |
b9dacc52 ML |
709 | } |
710 | ||
56303d34 | 711 | static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv, |
fe8bc396 SE |
712 | unsigned char *packet_buff, |
713 | int packet_len, | |
56303d34 | 714 | struct batadv_hard_iface *if_incoming, |
fe8bc396 | 715 | int own_packet, unsigned long send_time) |
b9dacc52 | 716 | { |
9cfc7bd6 | 717 | /* _aggr -> pointer to the packet we want to aggregate with |
b9dacc52 ML |
718 | * _pos -> pointer to the position in the queue |
719 | */ | |
56303d34 SE |
720 | struct batadv_forw_packet *forw_packet_aggr = NULL; |
721 | struct batadv_forw_packet *forw_packet_pos = NULL; | |
96412690 | 722 | struct batadv_ogm_packet *batadv_ogm_packet; |
b9dacc52 | 723 | bool direct_link; |
42d0b044 | 724 | unsigned long max_aggregation_jiffies; |
b9dacc52 | 725 | |
96412690 SE |
726 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; |
727 | direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0; | |
42d0b044 | 728 | max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS); |
b9dacc52 ML |
729 | |
730 | /* find position for the packet in the forward queue */ | |
731 | spin_lock_bh(&bat_priv->forw_bat_list_lock); | |
732 | /* own packets are not to be aggregated */ | |
733 | if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) { | |
b67bfe0d | 734 | hlist_for_each_entry(forw_packet_pos, |
b9dacc52 | 735 | &bat_priv->forw_bat_list, list) { |
96412690 | 736 | if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet, |
fe8bc396 SE |
737 | bat_priv, packet_len, |
738 | send_time, direct_link, | |
739 | if_incoming, | |
740 | forw_packet_pos)) { | |
b9dacc52 ML |
741 | forw_packet_aggr = forw_packet_pos; |
742 | break; | |
743 | } | |
744 | } | |
745 | } | |
746 | ||
747 | /* nothing to aggregate with - either aggregation disabled or no | |
9cfc7bd6 SE |
748 | * suitable aggregation packet found |
749 | */ | |
b9dacc52 ML |
750 | if (!forw_packet_aggr) { |
751 | /* the following section can run without the lock */ | |
752 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); | |
753 | ||
9cfc7bd6 | 754 | /* if we could not aggregate this packet with one of the others |
b9dacc52 ML |
755 | * we hold it back for a while, so that it might be aggregated |
756 | * later on | |
757 | */ | |
42d0b044 SE |
758 | if (!own_packet && atomic_read(&bat_priv->aggregated_ogms)) |
759 | send_time += max_aggregation_jiffies; | |
b9dacc52 | 760 | |
fe8bc396 SE |
761 | batadv_iv_ogm_aggregate_new(packet_buff, packet_len, |
762 | send_time, direct_link, | |
763 | if_incoming, own_packet); | |
b9dacc52 | 764 | } else { |
fe8bc396 SE |
765 | batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff, |
766 | packet_len, direct_link); | |
b9dacc52 ML |
767 | spin_unlock_bh(&bat_priv->forw_bat_list_lock); |
768 | } | |
769 | } | |
770 | ||
56303d34 | 771 | static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node, |
fe8bc396 | 772 | const struct ethhdr *ethhdr, |
96412690 | 773 | struct batadv_ogm_packet *batadv_ogm_packet, |
fe8bc396 SE |
774 | bool is_single_hop_neigh, |
775 | bool is_from_best_next_hop, | |
56303d34 | 776 | struct batadv_hard_iface *if_incoming) |
b9dacc52 | 777 | { |
56303d34 | 778 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
ef261577 | 779 | uint16_t tvlv_len; |
b9dacc52 | 780 | |
96412690 | 781 | if (batadv_ogm_packet->header.ttl <= 1) { |
39c75a51 | 782 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n"); |
b9dacc52 ML |
783 | return; |
784 | } | |
785 | ||
13b2541b ML |
786 | if (!is_from_best_next_hop) { |
787 | /* Mark the forwarded packet when it is not coming from our | |
788 | * best next hop. We still need to forward the packet for our | |
789 | * neighbor link quality detection to work in case the packet | |
790 | * originated from a single hop neighbor. Otherwise we can | |
791 | * simply drop the ogm. | |
792 | */ | |
793 | if (is_single_hop_neigh) | |
96412690 | 794 | batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP; |
13b2541b ML |
795 | else |
796 | return; | |
797 | } | |
b9dacc52 | 798 | |
ef261577 | 799 | tvlv_len = ntohs(batadv_ogm_packet->tvlv_len); |
b9dacc52 | 800 | |
96412690 SE |
801 | batadv_ogm_packet->header.ttl--; |
802 | memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN); | |
b9dacc52 | 803 | |
b9dacc52 | 804 | /* apply hop penalty */ |
96412690 | 805 | batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq, |
fe8bc396 | 806 | bat_priv); |
b9dacc52 | 807 | |
39c75a51 | 808 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 809 | "Forwarding packet: tq: %i, ttl: %i\n", |
96412690 | 810 | batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl); |
b9dacc52 | 811 | |
b9dacc52 | 812 | /* switch of primaries first hop flag when forwarding */ |
96412690 | 813 | batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP; |
75cd33f8 | 814 | if (is_single_hop_neigh) |
96412690 | 815 | batadv_ogm_packet->flags |= BATADV_DIRECTLINK; |
b9dacc52 | 816 | else |
96412690 | 817 | batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK; |
b9dacc52 | 818 | |
96412690 | 819 | batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet, |
ef261577 | 820 | BATADV_OGM_HLEN + tvlv_len, |
fe8bc396 | 821 | if_incoming, 0, batadv_iv_ogm_fwd_send_time()); |
b9dacc52 ML |
822 | } |
823 | ||
d9896617 AQ |
824 | /** |
825 | * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for | |
826 | * the given interface | |
827 | * @hard_iface: the interface for which the windows have to be shifted | |
828 | */ | |
829 | static void | |
830 | batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface) | |
831 | { | |
832 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); | |
833 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
834 | struct hlist_head *head; | |
835 | struct batadv_orig_node *orig_node; | |
836 | unsigned long *word; | |
837 | uint32_t i; | |
838 | size_t word_index; | |
839 | uint8_t *w; | |
bbad0a5e | 840 | int if_num; |
d9896617 AQ |
841 | |
842 | for (i = 0; i < hash->size; i++) { | |
843 | head = &hash->table[i]; | |
844 | ||
845 | rcu_read_lock(); | |
846 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { | |
bbad0a5e | 847 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
d9896617 | 848 | word_index = hard_iface->if_num * BATADV_NUM_WORDS; |
bbad0a5e | 849 | word = &(orig_node->bat_iv.bcast_own[word_index]); |
d9896617 AQ |
850 | |
851 | batadv_bit_get_packet(bat_priv, word, 1, 0); | |
bbad0a5e AQ |
852 | if_num = hard_iface->if_num; |
853 | w = &orig_node->bat_iv.bcast_own_sum[if_num]; | |
d9896617 | 854 | *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE); |
bbad0a5e | 855 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
d9896617 AQ |
856 | } |
857 | rcu_read_unlock(); | |
858 | } | |
859 | } | |
860 | ||
56303d34 | 861 | static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface) |
b9dacc52 | 862 | { |
56303d34 | 863 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
14511519 | 864 | unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff; |
96412690 | 865 | struct batadv_ogm_packet *batadv_ogm_packet; |
56303d34 | 866 | struct batadv_hard_iface *primary_if; |
14511519 | 867 | int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len; |
bbb1f90e | 868 | uint32_t seqno; |
ef261577 | 869 | uint16_t tvlv_len = 0; |
b9dacc52 | 870 | |
e5d89254 | 871 | primary_if = batadv_primary_if_get_selected(bat_priv); |
b9dacc52 | 872 | |
e1bf0c14 ML |
873 | if (hard_iface == primary_if) { |
874 | /* tt changes have to be committed before the tvlv data is | |
875 | * appended as it may alter the tt tvlv container | |
876 | */ | |
877 | batadv_tt_local_commit_changes(bat_priv); | |
ef261577 ML |
878 | tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff, |
879 | ogm_buff_len, | |
880 | BATADV_OGM_HLEN); | |
e1bf0c14 | 881 | } |
be9aa4c1 | 882 | |
14511519 | 883 | batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff); |
ef261577 | 884 | batadv_ogm_packet->tvlv_len = htons(tvlv_len); |
b9dacc52 ML |
885 | |
886 | /* change sequence number to network order */ | |
14511519 | 887 | seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno); |
bbb1f90e | 888 | batadv_ogm_packet->seqno = htonl(seqno); |
14511519 | 889 | atomic_inc(&hard_iface->bat_iv.ogm_seqno); |
b9dacc52 | 890 | |
d9896617 | 891 | batadv_iv_ogm_slide_own_bcast_window(hard_iface); |
14511519 ML |
892 | batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff, |
893 | hard_iface->bat_iv.ogm_buff_len, hard_iface, 1, | |
fe8bc396 | 894 | batadv_iv_ogm_emit_send_time(bat_priv)); |
b9dacc52 ML |
895 | |
896 | if (primary_if) | |
e5d89254 | 897 | batadv_hardif_free_ref(primary_if); |
b9dacc52 ML |
898 | } |
899 | ||
fe8bc396 | 900 | static void |
56303d34 SE |
901 | batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv, |
902 | struct batadv_orig_node *orig_node, | |
fe8bc396 | 903 | const struct ethhdr *ethhdr, |
96412690 | 904 | const struct batadv_ogm_packet *batadv_ogm_packet, |
56303d34 | 905 | struct batadv_hard_iface *if_incoming, |
fe8bc396 | 906 | const unsigned char *tt_buff, |
7c24bbbe | 907 | enum batadv_dup_status dup_status) |
fc957275 | 908 | { |
56303d34 SE |
909 | struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL; |
910 | struct batadv_neigh_node *router = NULL; | |
911 | struct batadv_orig_node *orig_node_tmp; | |
7caf69fb | 912 | int if_num; |
bbb1f90e | 913 | uint8_t sum_orig, sum_neigh; |
1eda58bf | 914 | uint8_t *neigh_addr; |
bbb1f90e | 915 | uint8_t tq_avg; |
fc957275 | 916 | |
39c75a51 | 917 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 918 | "update_originator(): Searching and updating originator entry of received packet\n"); |
fc957275 ML |
919 | |
920 | rcu_read_lock(); | |
b67bfe0d | 921 | hlist_for_each_entry_rcu(tmp_neigh_node, |
fc957275 | 922 | &orig_node->neigh_list, list) { |
1eda58bf SE |
923 | neigh_addr = tmp_neigh_node->addr; |
924 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && | |
925 | tmp_neigh_node->if_incoming == if_incoming && | |
926 | atomic_inc_not_zero(&tmp_neigh_node->refcount)) { | |
38dc40ef | 927 | if (WARN(neigh_node, "too many matching neigh_nodes")) |
7d211efc | 928 | batadv_neigh_node_free_ref(neigh_node); |
fc957275 ML |
929 | neigh_node = tmp_neigh_node; |
930 | continue; | |
931 | } | |
932 | ||
7c24bbbe | 933 | if (dup_status != BATADV_NO_DUP) |
fc957275 ML |
934 | continue; |
935 | ||
0538f759 AQ |
936 | spin_lock_bh(&tmp_neigh_node->bat_iv.lq_update_lock); |
937 | batadv_ring_buffer_set(tmp_neigh_node->bat_iv.tq_recv, | |
938 | &tmp_neigh_node->bat_iv.tq_index, 0); | |
939 | tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->bat_iv.tq_recv); | |
940 | tmp_neigh_node->bat_iv.tq_avg = tq_avg; | |
941 | spin_unlock_bh(&tmp_neigh_node->bat_iv.lq_update_lock); | |
fc957275 ML |
942 | } |
943 | ||
944 | if (!neigh_node) { | |
56303d34 | 945 | struct batadv_orig_node *orig_tmp; |
fc957275 | 946 | |
bbad0a5e | 947 | orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source); |
fc957275 ML |
948 | if (!orig_tmp) |
949 | goto unlock; | |
950 | ||
fe8bc396 SE |
951 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
952 | ethhdr->h_source, | |
863dd7a8 | 953 | orig_node, orig_tmp); |
fc957275 | 954 | |
7d211efc | 955 | batadv_orig_node_free_ref(orig_tmp); |
fc957275 ML |
956 | if (!neigh_node) |
957 | goto unlock; | |
958 | } else | |
39c75a51 | 959 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 960 | "Updating existing last-hop neighbor of originator\n"); |
fc957275 ML |
961 | |
962 | rcu_read_unlock(); | |
963 | ||
d7b2a97e | 964 | neigh_node->last_seen = jiffies; |
fc957275 | 965 | |
0538f759 AQ |
966 | spin_lock_bh(&neigh_node->bat_iv.lq_update_lock); |
967 | batadv_ring_buffer_set(neigh_node->bat_iv.tq_recv, | |
968 | &neigh_node->bat_iv.tq_index, | |
96412690 | 969 | batadv_ogm_packet->tq); |
0538f759 AQ |
970 | tq_avg = batadv_ring_buffer_avg(neigh_node->bat_iv.tq_recv); |
971 | neigh_node->bat_iv.tq_avg = tq_avg; | |
972 | spin_unlock_bh(&neigh_node->bat_iv.lq_update_lock); | |
fc957275 | 973 | |
7c24bbbe | 974 | if (dup_status == BATADV_NO_DUP) { |
96412690 SE |
975 | orig_node->last_ttl = batadv_ogm_packet->header.ttl; |
976 | neigh_node->last_ttl = batadv_ogm_packet->header.ttl; | |
fc957275 ML |
977 | } |
978 | ||
6680a124 | 979 | batadv_bonding_candidate_add(bat_priv, orig_node, neigh_node); |
fc957275 ML |
980 | |
981 | /* if this neighbor already is our next hop there is nothing | |
9cfc7bd6 SE |
982 | * to change |
983 | */ | |
7d211efc | 984 | router = batadv_orig_node_get_router(orig_node); |
fc957275 | 985 | if (router == neigh_node) |
e1bf0c14 | 986 | goto out; |
fc957275 ML |
987 | |
988 | /* if this neighbor does not offer a better TQ we won't consider it */ | |
0538f759 | 989 | if (router && (router->bat_iv.tq_avg > neigh_node->bat_iv.tq_avg)) |
e1bf0c14 | 990 | goto out; |
fc957275 ML |
991 | |
992 | /* if the TQ is the same and the link not more symmetric we | |
9cfc7bd6 SE |
993 | * won't consider it either |
994 | */ | |
0538f759 | 995 | if (router && (neigh_node->bat_iv.tq_avg == router->bat_iv.tq_avg)) { |
fc957275 | 996 | orig_node_tmp = router->orig_node; |
bbad0a5e | 997 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
7caf69fb | 998 | if_num = router->if_incoming->if_num; |
bbad0a5e AQ |
999 | sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
1000 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); | |
fc957275 ML |
1001 | |
1002 | orig_node_tmp = neigh_node->orig_node; | |
bbad0a5e | 1003 | spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); |
7caf69fb | 1004 | if_num = neigh_node->if_incoming->if_num; |
bbad0a5e AQ |
1005 | sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num]; |
1006 | spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock); | |
fc957275 | 1007 | |
bbb1f90e | 1008 | if (sum_orig >= sum_neigh) |
e1bf0c14 | 1009 | goto out; |
fc957275 ML |
1010 | } |
1011 | ||
30d3c511 | 1012 | batadv_update_route(bat_priv, orig_node, neigh_node); |
fc957275 ML |
1013 | goto out; |
1014 | ||
1015 | unlock: | |
1016 | rcu_read_unlock(); | |
1017 | out: | |
1018 | if (neigh_node) | |
7d211efc | 1019 | batadv_neigh_node_free_ref(neigh_node); |
fc957275 | 1020 | if (router) |
7d211efc | 1021 | batadv_neigh_node_free_ref(router); |
fc957275 ML |
1022 | } |
1023 | ||
56303d34 SE |
1024 | static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node, |
1025 | struct batadv_orig_node *orig_neigh_node, | |
96412690 | 1026 | struct batadv_ogm_packet *batadv_ogm_packet, |
56303d34 | 1027 | struct batadv_hard_iface *if_incoming) |
fc957275 | 1028 | { |
56303d34 SE |
1029 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
1030 | struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node; | |
fc957275 | 1031 | uint8_t total_count; |
42d0b044 SE |
1032 | uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own; |
1033 | unsigned int neigh_rq_inv_cube, neigh_rq_max_cube; | |
bbad0a5e | 1034 | int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0; |
42d0b044 | 1035 | unsigned int combined_tq; |
fc957275 ML |
1036 | |
1037 | /* find corresponding one hop neighbor */ | |
1038 | rcu_read_lock(); | |
b67bfe0d | 1039 | hlist_for_each_entry_rcu(tmp_neigh_node, |
fc957275 | 1040 | &orig_neigh_node->neigh_list, list) { |
1eda58bf SE |
1041 | if (!batadv_compare_eth(tmp_neigh_node->addr, |
1042 | orig_neigh_node->orig)) | |
fc957275 ML |
1043 | continue; |
1044 | ||
1045 | if (tmp_neigh_node->if_incoming != if_incoming) | |
1046 | continue; | |
1047 | ||
1048 | if (!atomic_inc_not_zero(&tmp_neigh_node->refcount)) | |
1049 | continue; | |
1050 | ||
1051 | neigh_node = tmp_neigh_node; | |
1052 | break; | |
1053 | } | |
1054 | rcu_read_unlock(); | |
1055 | ||
1056 | if (!neigh_node) | |
fe8bc396 SE |
1057 | neigh_node = batadv_iv_ogm_neigh_new(if_incoming, |
1058 | orig_neigh_node->orig, | |
1059 | orig_neigh_node, | |
863dd7a8 | 1060 | orig_neigh_node); |
fc957275 ML |
1061 | |
1062 | if (!neigh_node) | |
1063 | goto out; | |
1064 | ||
d7b2a97e | 1065 | /* if orig_node is direct neighbor update neigh_node last_seen */ |
fc957275 | 1066 | if (orig_node == orig_neigh_node) |
d7b2a97e | 1067 | neigh_node->last_seen = jiffies; |
fc957275 | 1068 | |
d7b2a97e | 1069 | orig_node->last_seen = jiffies; |
fc957275 ML |
1070 | |
1071 | /* find packet count of corresponding one hop neighbor */ | |
bbad0a5e AQ |
1072 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
1073 | if_num = if_incoming->if_num; | |
1074 | orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num]; | |
0538f759 | 1075 | neigh_rq_count = neigh_node->bat_iv.real_packet_count; |
bbad0a5e | 1076 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
fc957275 ML |
1077 | |
1078 | /* pay attention to not get a value bigger than 100 % */ | |
c67893d1 SE |
1079 | if (orig_eq_count > neigh_rq_count) |
1080 | total_count = neigh_rq_count; | |
1081 | else | |
1082 | total_count = orig_eq_count; | |
fc957275 | 1083 | |
9cfc7bd6 SE |
1084 | /* if we have too few packets (too less data) we set tq_own to zero |
1085 | * if we receive too few packets it is not considered bidirectional | |
1086 | */ | |
42d0b044 SE |
1087 | if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM || |
1088 | neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM) | |
fc957275 ML |
1089 | tq_own = 0; |
1090 | else | |
1091 | /* neigh_node->real_packet_count is never zero as we | |
1092 | * only purge old information when getting new | |
9cfc7bd6 SE |
1093 | * information |
1094 | */ | |
42d0b044 | 1095 | tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count; |
fc957275 | 1096 | |
21a1236b | 1097 | /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does |
fc957275 ML |
1098 | * affect the nearly-symmetric links only a little, but |
1099 | * punishes asymmetric links more. This will give a value | |
1100 | * between 0 and TQ_MAX_VALUE | |
1101 | */ | |
42d0b044 SE |
1102 | neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count; |
1103 | neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv; | |
1104 | neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE * | |
1105 | BATADV_TQ_LOCAL_WINDOW_SIZE * | |
1106 | BATADV_TQ_LOCAL_WINDOW_SIZE; | |
1107 | inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube; | |
1108 | inv_asym_penalty /= neigh_rq_max_cube; | |
1109 | tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty; | |
1110 | ||
96412690 | 1111 | combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty; |
42d0b044 | 1112 | combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE; |
96412690 | 1113 | batadv_ogm_packet->tq = combined_tq; |
fc957275 | 1114 | |
39c75a51 | 1115 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1116 | "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n", |
1117 | orig_node->orig, orig_neigh_node->orig, total_count, | |
1118 | neigh_rq_count, tq_own, | |
96412690 | 1119 | tq_asym_penalty, batadv_ogm_packet->tq); |
fc957275 ML |
1120 | |
1121 | /* if link has the minimum required transmission quality | |
9cfc7bd6 SE |
1122 | * consider it bidirectional |
1123 | */ | |
96412690 | 1124 | if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT) |
fc957275 ML |
1125 | ret = 1; |
1126 | ||
1127 | out: | |
1128 | if (neigh_node) | |
7d211efc | 1129 | batadv_neigh_node_free_ref(neigh_node); |
fc957275 ML |
1130 | return ret; |
1131 | } | |
1132 | ||
7c24bbbe SW |
1133 | /** |
1134 | * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces, | |
1135 | * adjust the sequence number and find out whether it is a duplicate | |
1136 | * @ethhdr: ethernet header of the packet | |
1137 | * @batadv_ogm_packet: OGM packet to be considered | |
1138 | * @if_incoming: interface on which the OGM packet was received | |
1139 | * | |
1140 | * Returns duplicate status as enum batadv_dup_status | |
fc957275 | 1141 | */ |
7c24bbbe | 1142 | static enum batadv_dup_status |
fe8bc396 | 1143 | batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr, |
96412690 | 1144 | const struct batadv_ogm_packet *batadv_ogm_packet, |
56303d34 | 1145 | const struct batadv_hard_iface *if_incoming) |
fc957275 | 1146 | { |
56303d34 SE |
1147 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
1148 | struct batadv_orig_node *orig_node; | |
1149 | struct batadv_neigh_node *tmp_neigh_node; | |
7c24bbbe | 1150 | int is_dup; |
fc957275 ML |
1151 | int32_t seq_diff; |
1152 | int need_update = 0; | |
7c24bbbe SW |
1153 | int set_mark; |
1154 | enum batadv_dup_status ret = BATADV_NO_DUP; | |
96412690 | 1155 | uint32_t seqno = ntohl(batadv_ogm_packet->seqno); |
1eda58bf | 1156 | uint8_t *neigh_addr; |
bbb1f90e | 1157 | uint8_t packet_count; |
0538f759 | 1158 | unsigned long *bitmap; |
fc957275 | 1159 | |
bbad0a5e | 1160 | orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); |
fc957275 | 1161 | if (!orig_node) |
7c24bbbe | 1162 | return BATADV_NO_DUP; |
fc957275 | 1163 | |
bbad0a5e | 1164 | spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
e0f5211f | 1165 | seq_diff = seqno - orig_node->last_real_seqno; |
fc957275 ML |
1166 | |
1167 | /* signalize caller that the packet is to be dropped. */ | |
1e5cc266 | 1168 | if (!hlist_empty(&orig_node->neigh_list) && |
30d3c511 | 1169 | batadv_window_protected(bat_priv, seq_diff, |
7c24bbbe SW |
1170 | &orig_node->batman_seqno_reset)) { |
1171 | ret = BATADV_PROTECTED; | |
fc957275 | 1172 | goto out; |
7c24bbbe | 1173 | } |
fc957275 ML |
1174 | |
1175 | rcu_read_lock(); | |
b67bfe0d | 1176 | hlist_for_each_entry_rcu(tmp_neigh_node, |
fc957275 | 1177 | &orig_node->neigh_list, list) { |
1eda58bf | 1178 | neigh_addr = tmp_neigh_node->addr; |
0538f759 | 1179 | is_dup = batadv_test_bit(tmp_neigh_node->bat_iv.real_bits, |
7c24bbbe SW |
1180 | orig_node->last_real_seqno, |
1181 | seqno); | |
1182 | ||
1eda58bf | 1183 | if (batadv_compare_eth(neigh_addr, ethhdr->h_source) && |
7c24bbbe | 1184 | tmp_neigh_node->if_incoming == if_incoming) { |
fc957275 | 1185 | set_mark = 1; |
7c24bbbe SW |
1186 | if (is_dup) |
1187 | ret = BATADV_NEIGH_DUP; | |
1188 | } else { | |
fc957275 | 1189 | set_mark = 0; |
7c24bbbe SW |
1190 | if (is_dup && (ret != BATADV_NEIGH_DUP)) |
1191 | ret = BATADV_ORIG_DUP; | |
1192 | } | |
fc957275 ML |
1193 | |
1194 | /* if the window moved, set the update flag. */ | |
0538f759 AQ |
1195 | bitmap = tmp_neigh_node->bat_iv.real_bits; |
1196 | need_update |= batadv_bit_get_packet(bat_priv, bitmap, | |
0f5f9322 | 1197 | seq_diff, set_mark); |
fc957275 | 1198 | |
0538f759 | 1199 | packet_count = bitmap_weight(tmp_neigh_node->bat_iv.real_bits, |
bbb1f90e | 1200 | BATADV_TQ_LOCAL_WINDOW_SIZE); |
0538f759 | 1201 | tmp_neigh_node->bat_iv.real_packet_count = packet_count; |
fc957275 ML |
1202 | } |
1203 | rcu_read_unlock(); | |
1204 | ||
1205 | if (need_update) { | |
39c75a51 | 1206 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1207 | "updating last_seqno: old %u, new %u\n", |
1208 | orig_node->last_real_seqno, seqno); | |
e0f5211f | 1209 | orig_node->last_real_seqno = seqno; |
fc957275 ML |
1210 | } |
1211 | ||
fc957275 | 1212 | out: |
bbad0a5e | 1213 | spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock); |
7d211efc | 1214 | batadv_orig_node_free_ref(orig_node); |
fc957275 ML |
1215 | return ret; |
1216 | } | |
1217 | ||
fe8bc396 | 1218 | static void batadv_iv_ogm_process(const struct ethhdr *ethhdr, |
96412690 | 1219 | struct batadv_ogm_packet *batadv_ogm_packet, |
fe8bc396 | 1220 | const unsigned char *tt_buff, |
56303d34 | 1221 | struct batadv_hard_iface *if_incoming) |
fc957275 | 1222 | { |
56303d34 SE |
1223 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
1224 | struct batadv_hard_iface *hard_iface; | |
0538f759 | 1225 | struct batadv_orig_node *orig_neigh_node, *orig_node, *orig_node_tmp; |
56303d34 SE |
1226 | struct batadv_neigh_node *router = NULL, *router_router = NULL; |
1227 | struct batadv_neigh_node *orig_neigh_router = NULL; | |
fc957275 ML |
1228 | int has_directlink_flag; |
1229 | int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0; | |
281581d3 | 1230 | int is_bidirect; |
75cd33f8 | 1231 | bool is_single_hop_neigh = false; |
13b2541b | 1232 | bool is_from_best_next_hop = false; |
7c24bbbe SW |
1233 | int sameseq, similar_ttl; |
1234 | enum batadv_dup_status dup_status; | |
fc957275 | 1235 | uint32_t if_incoming_seqno; |
1eda58bf | 1236 | uint8_t *prev_sender; |
fc957275 ML |
1237 | |
1238 | /* Silently drop when the batman packet is actually not a | |
1239 | * correct packet. | |
1240 | * | |
1241 | * This might happen if a packet is padded (e.g. Ethernet has a | |
1242 | * minimum frame length of 64 byte) and the aggregation interprets | |
1243 | * it as an additional length. | |
1244 | * | |
1245 | * TODO: A more sane solution would be to have a bit in the | |
96412690 | 1246 | * batadv_ogm_packet to detect whether the packet is the last |
fc957275 ML |
1247 | * packet in an aggregation. Here we expect that the padding |
1248 | * is always zero (or not 0x01) | |
1249 | */ | |
96412690 | 1250 | if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM) |
fc957275 ML |
1251 | return; |
1252 | ||
1253 | /* could be changed by schedule_own_packet() */ | |
14511519 | 1254 | if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno); |
fc957275 | 1255 | |
96412690 | 1256 | if (batadv_ogm_packet->flags & BATADV_DIRECTLINK) |
acd34afa SE |
1257 | has_directlink_flag = 1; |
1258 | else | |
1259 | has_directlink_flag = 0; | |
fc957275 | 1260 | |
96412690 | 1261 | if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig)) |
75cd33f8 | 1262 | is_single_hop_neigh = true; |
fc957275 | 1263 | |
39c75a51 | 1264 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
e1bf0c14 | 1265 | "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n", |
1eda58bf | 1266 | ethhdr->h_source, if_incoming->net_dev->name, |
96412690 SE |
1267 | if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig, |
1268 | batadv_ogm_packet->prev_sender, | |
e1bf0c14 | 1269 | ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->tq, |
96412690 SE |
1270 | batadv_ogm_packet->header.ttl, |
1271 | batadv_ogm_packet->header.version, has_directlink_flag); | |
fc957275 ML |
1272 | |
1273 | rcu_read_lock(); | |
3193e8fd | 1274 | list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) { |
e9a4f295 | 1275 | if (hard_iface->if_status != BATADV_IF_ACTIVE) |
fc957275 ML |
1276 | continue; |
1277 | ||
1278 | if (hard_iface->soft_iface != if_incoming->soft_iface) | |
1279 | continue; | |
1280 | ||
1eda58bf SE |
1281 | if (batadv_compare_eth(ethhdr->h_source, |
1282 | hard_iface->net_dev->dev_addr)) | |
fc957275 ML |
1283 | is_my_addr = 1; |
1284 | ||
96412690 | 1285 | if (batadv_compare_eth(batadv_ogm_packet->orig, |
1eda58bf | 1286 | hard_iface->net_dev->dev_addr)) |
fc957275 ML |
1287 | is_my_orig = 1; |
1288 | ||
96412690 | 1289 | if (batadv_compare_eth(batadv_ogm_packet->prev_sender, |
1eda58bf | 1290 | hard_iface->net_dev->dev_addr)) |
fc957275 | 1291 | is_my_oldorig = 1; |
fc957275 ML |
1292 | } |
1293 | rcu_read_unlock(); | |
1294 | ||
fc957275 | 1295 | if (is_my_addr) { |
39c75a51 | 1296 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1297 | "Drop packet: received my own broadcast (sender: %pM)\n", |
1298 | ethhdr->h_source); | |
fc957275 ML |
1299 | return; |
1300 | } | |
1301 | ||
fc957275 ML |
1302 | if (is_my_orig) { |
1303 | unsigned long *word; | |
1304 | int offset; | |
9b4a1159 | 1305 | int32_t bit_pos; |
42d0b044 SE |
1306 | int16_t if_num; |
1307 | uint8_t *weight; | |
fc957275 | 1308 | |
bbad0a5e AQ |
1309 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
1310 | ethhdr->h_source); | |
fc957275 ML |
1311 | if (!orig_neigh_node) |
1312 | return; | |
1313 | ||
1314 | /* neighbor has to indicate direct link and it has to | |
9cfc7bd6 SE |
1315 | * come via the corresponding interface |
1316 | * save packet seqno for bidirectional check | |
1317 | */ | |
fc957275 | 1318 | if (has_directlink_flag && |
1eda58bf | 1319 | batadv_compare_eth(if_incoming->net_dev->dev_addr, |
96412690 | 1320 | batadv_ogm_packet->orig)) { |
42d0b044 SE |
1321 | if_num = if_incoming->if_num; |
1322 | offset = if_num * BATADV_NUM_WORDS; | |
fc957275 | 1323 | |
bbad0a5e AQ |
1324 | spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
1325 | word = &(orig_neigh_node->bat_iv.bcast_own[offset]); | |
9b4a1159 | 1326 | bit_pos = if_incoming_seqno - 2; |
96412690 | 1327 | bit_pos -= ntohl(batadv_ogm_packet->seqno); |
9b4a1159 | 1328 | batadv_set_bit(word, bit_pos); |
bbad0a5e | 1329 | weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num]; |
42d0b044 SE |
1330 | *weight = bitmap_weight(word, |
1331 | BATADV_TQ_LOCAL_WINDOW_SIZE); | |
bbad0a5e | 1332 | spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock); |
fc957275 ML |
1333 | } |
1334 | ||
39c75a51 | 1335 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1336 | "Drop packet: originator packet from myself (via neighbor)\n"); |
7d211efc | 1337 | batadv_orig_node_free_ref(orig_neigh_node); |
fc957275 ML |
1338 | return; |
1339 | } | |
1340 | ||
1341 | if (is_my_oldorig) { | |
39c75a51 | 1342 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1343 | "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n", |
1344 | ethhdr->h_source); | |
fc957275 ML |
1345 | return; |
1346 | } | |
1347 | ||
96412690 | 1348 | if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) { |
39c75a51 | 1349 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1350 | "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n", |
1351 | ethhdr->h_source); | |
13b2541b ML |
1352 | return; |
1353 | } | |
1354 | ||
bbad0a5e | 1355 | orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig); |
fc957275 ML |
1356 | if (!orig_node) |
1357 | return; | |
1358 | ||
7c24bbbe SW |
1359 | dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet, |
1360 | if_incoming); | |
fc957275 | 1361 | |
7c24bbbe | 1362 | if (dup_status == BATADV_PROTECTED) { |
39c75a51 | 1363 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1364 | "Drop packet: packet within seqno protection time (sender: %pM)\n", |
1365 | ethhdr->h_source); | |
fc957275 ML |
1366 | goto out; |
1367 | } | |
1368 | ||
96412690 | 1369 | if (batadv_ogm_packet->tq == 0) { |
39c75a51 | 1370 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1371 | "Drop packet: originator packet with tq equal 0\n"); |
fc957275 ML |
1372 | goto out; |
1373 | } | |
1374 | ||
7d211efc | 1375 | router = batadv_orig_node_get_router(orig_node); |
0538f759 AQ |
1376 | if (router) { |
1377 | orig_node_tmp = router->orig_node; | |
1378 | router_router = batadv_orig_node_get_router(orig_node_tmp); | |
1379 | } | |
fc957275 | 1380 | |
0538f759 | 1381 | if ((router && router->bat_iv.tq_avg != 0) && |
1eda58bf | 1382 | (batadv_compare_eth(router->addr, ethhdr->h_source))) |
13b2541b ML |
1383 | is_from_best_next_hop = true; |
1384 | ||
96412690 | 1385 | prev_sender = batadv_ogm_packet->prev_sender; |
fc957275 ML |
1386 | /* avoid temporary routing loops */ |
1387 | if (router && router_router && | |
1eda58bf | 1388 | (batadv_compare_eth(router->addr, prev_sender)) && |
96412690 | 1389 | !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) && |
1eda58bf | 1390 | (batadv_compare_eth(router->addr, router_router->addr))) { |
39c75a51 | 1391 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf SE |
1392 | "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n", |
1393 | ethhdr->h_source); | |
fc957275 ML |
1394 | goto out; |
1395 | } | |
1396 | ||
ef261577 ML |
1397 | batadv_tvlv_ogm_receive(bat_priv, batadv_ogm_packet, orig_node); |
1398 | ||
fc957275 | 1399 | /* if sender is a direct neighbor the sender mac equals |
9cfc7bd6 SE |
1400 | * originator mac |
1401 | */ | |
c67893d1 SE |
1402 | if (is_single_hop_neigh) |
1403 | orig_neigh_node = orig_node; | |
1404 | else | |
bbad0a5e AQ |
1405 | orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv, |
1406 | ethhdr->h_source); | |
c67893d1 | 1407 | |
fc957275 ML |
1408 | if (!orig_neigh_node) |
1409 | goto out; | |
1410 | ||
d56b1705 MH |
1411 | /* Update nc_nodes of the originator */ |
1412 | batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node, | |
1413 | batadv_ogm_packet, is_single_hop_neigh); | |
1414 | ||
7d211efc | 1415 | orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node); |
fc957275 ML |
1416 | |
1417 | /* drop packet if sender is not a direct neighbor and if we | |
9cfc7bd6 SE |
1418 | * don't route towards it |
1419 | */ | |
fc957275 | 1420 | if (!is_single_hop_neigh && (!orig_neigh_router)) { |
39c75a51 | 1421 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1422 | "Drop packet: OGM via unknown neighbor!\n"); |
fc957275 ML |
1423 | goto out_neigh; |
1424 | } | |
1425 | ||
fe8bc396 | 1426 | is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node, |
96412690 | 1427 | batadv_ogm_packet, if_incoming); |
fc957275 | 1428 | |
30d3c511 | 1429 | batadv_bonding_save_primary(orig_node, orig_neigh_node, |
96412690 | 1430 | batadv_ogm_packet); |
fc957275 ML |
1431 | |
1432 | /* update ranking if it is not a duplicate or has the same | |
9cfc7bd6 SE |
1433 | * seqno and similar ttl as the non-duplicate |
1434 | */ | |
96412690 | 1435 | sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno); |
7c24bbbe SW |
1436 | similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl; |
1437 | if (is_bidirect && ((dup_status == BATADV_NO_DUP) || | |
1438 | (sameseq && similar_ttl))) | |
fe8bc396 | 1439 | batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr, |
96412690 | 1440 | batadv_ogm_packet, if_incoming, |
7c24bbbe | 1441 | tt_buff, dup_status); |
fc957275 ML |
1442 | |
1443 | /* is single hop (direct) neighbor */ | |
1444 | if (is_single_hop_neigh) { | |
fc957275 | 1445 | /* mark direct link on incoming interface */ |
96412690 | 1446 | batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet, |
fe8bc396 SE |
1447 | is_single_hop_neigh, |
1448 | is_from_best_next_hop, if_incoming); | |
fc957275 | 1449 | |
39c75a51 | 1450 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1451 | "Forwarding packet: rebroadcast neighbor packet with direct link flag\n"); |
fc957275 ML |
1452 | goto out_neigh; |
1453 | } | |
1454 | ||
1455 | /* multihop originator */ | |
fe8bc396 | 1456 | if (!is_bidirect) { |
39c75a51 | 1457 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1458 | "Drop packet: not received via bidirectional link\n"); |
fc957275 ML |
1459 | goto out_neigh; |
1460 | } | |
1461 | ||
7c24bbbe | 1462 | if (dup_status == BATADV_NEIGH_DUP) { |
39c75a51 | 1463 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1464 | "Drop packet: duplicate packet received\n"); |
fc957275 ML |
1465 | goto out_neigh; |
1466 | } | |
1467 | ||
39c75a51 | 1468 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 1469 | "Forwarding packet: rebroadcast originator packet\n"); |
96412690 | 1470 | batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet, |
fe8bc396 SE |
1471 | is_single_hop_neigh, is_from_best_next_hop, |
1472 | if_incoming); | |
fc957275 ML |
1473 | |
1474 | out_neigh: | |
1475 | if ((orig_neigh_node) && (!is_single_hop_neigh)) | |
7d211efc | 1476 | batadv_orig_node_free_ref(orig_neigh_node); |
fc957275 ML |
1477 | out: |
1478 | if (router) | |
7d211efc | 1479 | batadv_neigh_node_free_ref(router); |
fc957275 | 1480 | if (router_router) |
7d211efc | 1481 | batadv_neigh_node_free_ref(router_router); |
fc957275 | 1482 | if (orig_neigh_router) |
7d211efc | 1483 | batadv_neigh_node_free_ref(orig_neigh_router); |
fc957275 | 1484 | |
7d211efc | 1485 | batadv_orig_node_free_ref(orig_node); |
fc957275 ML |
1486 | } |
1487 | ||
fe8bc396 | 1488 | static int batadv_iv_ogm_receive(struct sk_buff *skb, |
56303d34 | 1489 | struct batadv_hard_iface *if_incoming) |
fc957275 | 1490 | { |
56303d34 | 1491 | struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface); |
96412690 | 1492 | struct batadv_ogm_packet *batadv_ogm_packet; |
8780dad9 ML |
1493 | struct ethhdr *ethhdr; |
1494 | int buff_pos = 0, packet_len; | |
ef261577 | 1495 | unsigned char *tvlv_buff, *packet_buff; |
c67893d1 | 1496 | uint8_t *packet_pos; |
ef261577 | 1497 | bool ret; |
c3e29312 | 1498 | |
7e071c79 | 1499 | ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN); |
c3e29312 ML |
1500 | if (!ret) |
1501 | return NET_RX_DROP; | |
fc957275 | 1502 | |
edbf12ba ML |
1503 | /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface |
1504 | * that does not have B.A.T.M.A.N. IV enabled ? | |
1505 | */ | |
fe8bc396 | 1506 | if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit) |
edbf12ba ML |
1507 | return NET_RX_DROP; |
1508 | ||
d69909d2 SE |
1509 | batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX); |
1510 | batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES, | |
f8214865 MH |
1511 | skb->len + ETH_HLEN); |
1512 | ||
8780dad9 | 1513 | packet_len = skb_headlen(skb); |
7ed4be95 | 1514 | ethhdr = eth_hdr(skb); |
8780dad9 | 1515 | packet_buff = skb->data; |
96412690 | 1516 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff; |
fc957275 ML |
1517 | |
1518 | /* unpack the aggregated packets and process them one by one */ | |
b47506d9 | 1519 | while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len, |
ef261577 ML |
1520 | batadv_ogm_packet->tvlv_len)) { |
1521 | tvlv_buff = packet_buff + buff_pos + BATADV_OGM_HLEN; | |
fc957275 | 1522 | |
ef261577 ML |
1523 | batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, |
1524 | tvlv_buff, if_incoming); | |
fc957275 | 1525 | |
7e071c79 | 1526 | buff_pos += BATADV_OGM_HLEN; |
ef261577 | 1527 | buff_pos += ntohs(batadv_ogm_packet->tvlv_len); |
fc957275 | 1528 | |
c67893d1 SE |
1529 | packet_pos = packet_buff + buff_pos; |
1530 | batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos; | |
b47506d9 | 1531 | } |
c3e29312 ML |
1532 | |
1533 | kfree_skb(skb); | |
1534 | return NET_RX_SUCCESS; | |
fc957275 | 1535 | } |
1c280471 | 1536 | |
737a2a22 AQ |
1537 | /** |
1538 | * batadv_iv_ogm_orig_print - print the originator table | |
1539 | * @bat_priv: the bat priv with all the soft interface information | |
1540 | * @seq: debugfs table seq_file struct | |
1541 | */ | |
1542 | static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv, | |
1543 | struct seq_file *seq) | |
1544 | { | |
1545 | struct batadv_neigh_node *neigh_node, *neigh_node_tmp; | |
1546 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
1547 | int last_seen_msecs, last_seen_secs; | |
1548 | struct batadv_orig_node *orig_node; | |
1549 | unsigned long last_seen_jiffies; | |
1550 | struct hlist_head *head; | |
1551 | int batman_count = 0; | |
1552 | uint32_t i; | |
1553 | ||
1554 | seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n", | |
1555 | "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE, | |
1556 | "Nexthop", "outgoingIF", "Potential nexthops"); | |
1557 | ||
1558 | for (i = 0; i < hash->size; i++) { | |
1559 | head = &hash->table[i]; | |
1560 | ||
1561 | rcu_read_lock(); | |
1562 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { | |
1563 | neigh_node = batadv_orig_node_get_router(orig_node); | |
1564 | if (!neigh_node) | |
1565 | continue; | |
1566 | ||
1567 | if (neigh_node->bat_iv.tq_avg == 0) | |
1568 | goto next; | |
1569 | ||
1570 | last_seen_jiffies = jiffies - orig_node->last_seen; | |
1571 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); | |
1572 | last_seen_secs = last_seen_msecs / 1000; | |
1573 | last_seen_msecs = last_seen_msecs % 1000; | |
1574 | ||
1575 | seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:", | |
1576 | orig_node->orig, last_seen_secs, | |
1577 | last_seen_msecs, neigh_node->bat_iv.tq_avg, | |
1578 | neigh_node->addr, | |
1579 | neigh_node->if_incoming->net_dev->name); | |
1580 | ||
1581 | hlist_for_each_entry_rcu(neigh_node_tmp, | |
1582 | &orig_node->neigh_list, list) { | |
1583 | seq_printf(seq, " %pM (%3i)", | |
1584 | neigh_node_tmp->addr, | |
1585 | neigh_node_tmp->bat_iv.tq_avg); | |
1586 | } | |
1587 | ||
1588 | seq_puts(seq, "\n"); | |
1589 | batman_count++; | |
1590 | ||
1591 | next: | |
1592 | batadv_neigh_node_free_ref(neigh_node); | |
1593 | } | |
1594 | rcu_read_unlock(); | |
1595 | } | |
1596 | ||
1597 | if (batman_count == 0) | |
1598 | seq_puts(seq, "No batman nodes in range ...\n"); | |
1599 | } | |
1600 | ||
a3285a8f AQ |
1601 | /** |
1602 | * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors | |
1603 | * @neigh1: the first neighbor object of the comparison | |
1604 | * @neigh2: the second neighbor object of the comparison | |
1605 | * | |
1606 | * Returns a value less, equal to or greater than 0 if the metric via neigh1 is | |
1607 | * lower, the same as or higher than the metric via neigh2 | |
1608 | */ | |
1609 | static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1, | |
1610 | struct batadv_neigh_node *neigh2) | |
1611 | { | |
1612 | uint8_t tq1, tq2; | |
1613 | ||
1614 | tq1 = neigh1->bat_iv.tq_avg; | |
1615 | tq2 = neigh2->bat_iv.tq_avg; | |
1616 | ||
1617 | return tq1 - tq2; | |
1618 | } | |
1619 | ||
c43c981e AQ |
1620 | /** |
1621 | * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than | |
1622 | * neigh2 from the metric prospective | |
1623 | * @neigh1: the first neighbor object of the comparison | |
1624 | * @neigh2: the second neighbor object of the comparison | |
1625 | * | |
1626 | * Returns true if the metric via neigh1 is equally good or better than the | |
1627 | * metric via neigh2, false otherwise. | |
1628 | */ | |
1629 | static bool batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1, | |
1630 | struct batadv_neigh_node *neigh2) | |
1631 | { | |
1632 | int diff = batadv_iv_ogm_neigh_cmp(neigh1, neigh2); | |
1633 | ||
1634 | return diff > -BATADV_TQ_SIMILARITY_THRESHOLD; | |
1635 | } | |
1636 | ||
56303d34 | 1637 | static struct batadv_algo_ops batadv_batman_iv __read_mostly = { |
519d3497 | 1638 | .name = "BATMAN_IV", |
fe8bc396 SE |
1639 | .bat_iface_enable = batadv_iv_ogm_iface_enable, |
1640 | .bat_iface_disable = batadv_iv_ogm_iface_disable, | |
1641 | .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac, | |
1642 | .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set, | |
1643 | .bat_ogm_schedule = batadv_iv_ogm_schedule, | |
1644 | .bat_ogm_emit = batadv_iv_ogm_emit, | |
a3285a8f | 1645 | .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp, |
c43c981e | 1646 | .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob, |
737a2a22 | 1647 | .bat_orig_print = batadv_iv_ogm_orig_print, |
d0015fdd AQ |
1648 | .bat_orig_free = batadv_iv_ogm_orig_free, |
1649 | .bat_orig_add_if = batadv_iv_ogm_orig_add_if, | |
1650 | .bat_orig_del_if = batadv_iv_ogm_orig_del_if, | |
1c280471 ML |
1651 | }; |
1652 | ||
81c524f7 | 1653 | int __init batadv_iv_init(void) |
1c280471 | 1654 | { |
c3e29312 ML |
1655 | int ret; |
1656 | ||
1657 | /* batman originator packet */ | |
acd34afa SE |
1658 | ret = batadv_recv_handler_register(BATADV_IV_OGM, |
1659 | batadv_iv_ogm_receive); | |
c3e29312 ML |
1660 | if (ret < 0) |
1661 | goto out; | |
1662 | ||
fe8bc396 | 1663 | ret = batadv_algo_register(&batadv_batman_iv); |
c3e29312 ML |
1664 | if (ret < 0) |
1665 | goto handler_unregister; | |
1666 | ||
1667 | goto out; | |
1668 | ||
1669 | handler_unregister: | |
acd34afa | 1670 | batadv_recv_handler_unregister(BATADV_IV_OGM); |
c3e29312 ML |
1671 | out: |
1672 | return ret; | |
1c280471 | 1673 | } |