batman-adv: split tvlv into a separate file
[deliverable/linux.git] / net / batman-adv / bat_v_ogm.c
CommitLineData
0da00359
AQ
1/* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
2 *
3 * Antonio Quartulli
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, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "bat_v_ogm.h"
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/byteorder/generic.h>
23#include <linux/errno.h>
24#include <linux/etherdevice.h>
25#include <linux/fs.h>
26#include <linux/if_ether.h>
27#include <linux/jiffies.h>
28#include <linux/kernel.h>
27353446 29#include <linux/kref.h>
9323158e 30#include <linux/list.h>
0da00359
AQ
31#include <linux/netdevice.h>
32#include <linux/random.h>
33#include <linux/rculist.h>
34#include <linux/rcupdate.h>
35#include <linux/skbuff.h>
36#include <linux/slab.h>
37#include <linux/stddef.h>
38#include <linux/string.h>
39#include <linux/types.h>
40#include <linux/workqueue.h>
41
42#include "hard-interface.h"
9323158e
AQ
43#include "hash.h"
44#include "originator.h"
0da00359
AQ
45#include "packet.h"
46#include "routing.h"
47#include "send.h"
48#include "translation-table.h"
1f8dce49 49#include "tvlv.h"
0da00359 50
9323158e
AQ
51/**
52 * batadv_v_ogm_orig_get - retrieve and possibly create an originator node
53 * @bat_priv: the bat priv with all the soft interface information
54 * @addr: the address of the originator
55 *
56 * Return: the orig_node corresponding to the specified address. If such object
57 * does not exist it is allocated here. In case of allocation failure returns
58 * NULL.
59 */
60struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
61 const u8 *addr)
62{
63 struct batadv_orig_node *orig_node;
64 int hash_added;
65
66 orig_node = batadv_orig_hash_find(bat_priv, addr);
67 if (orig_node)
68 return orig_node;
69
70 orig_node = batadv_orig_node_new(bat_priv, addr);
71 if (!orig_node)
72 return NULL;
73
74 hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
75 batadv_choose_orig, orig_node,
76 &orig_node->hash_entry);
77 if (hash_added != 0) {
78 /* orig_node->refcounter is initialised to 2 by
79 * batadv_orig_node_new()
80 */
81 batadv_orig_node_put(orig_node);
82 batadv_orig_node_put(orig_node);
83 orig_node = NULL;
84 }
85
86 return orig_node;
87}
88
0da00359
AQ
89/**
90 * batadv_v_ogm_start_timer - restart the OGM sending timer
91 * @bat_priv: the bat priv with all the soft interface information
92 */
93static void batadv_v_ogm_start_timer(struct batadv_priv *bat_priv)
94{
95 unsigned long msecs;
96 /* this function may be invoked in different contexts (ogm rescheduling
97 * or hard_iface activation), but the work timer should not be reset
98 */
99 if (delayed_work_pending(&bat_priv->bat_v.ogm_wq))
100 return;
101
102 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
103 msecs += prandom_u32() % (2 * BATADV_JITTER);
104 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
105 msecs_to_jiffies(msecs));
106}
107
108/**
109 * batadv_v_ogm_send_to_if - send a batman ogm using a given interface
110 * @skb: the OGM to send
111 * @hard_iface: the interface to use to send the OGM
112 */
113static void batadv_v_ogm_send_to_if(struct sk_buff *skb,
114 struct batadv_hard_iface *hard_iface)
115{
116 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
117
118 if (hard_iface->if_status != BATADV_IF_ACTIVE)
119 return;
120
121 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
122 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
123 skb->len + ETH_HLEN);
124
95d39278 125 batadv_send_broadcast_skb(skb, hard_iface);
0da00359
AQ
126}
127
128/**
129 * batadv_v_ogm_send - periodic worker broadcasting the own OGM
130 * @work: work queue item
131 */
132static void batadv_v_ogm_send(struct work_struct *work)
133{
134 struct batadv_hard_iface *hard_iface;
135 struct batadv_priv_bat_v *bat_v;
136 struct batadv_priv *bat_priv;
137 struct batadv_ogm2_packet *ogm_packet;
138 struct sk_buff *skb, *skb_tmp;
139 unsigned char *ogm_buff, *pkt_buff;
140 int ogm_buff_len;
141 u16 tvlv_len = 0;
142
143 bat_v = container_of(work, struct batadv_priv_bat_v, ogm_wq.work);
144 bat_priv = container_of(bat_v, struct batadv_priv, bat_v);
145
146 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
147 goto out;
148
149 ogm_buff = bat_priv->bat_v.ogm_buff;
150 ogm_buff_len = bat_priv->bat_v.ogm_buff_len;
151 /* tt changes have to be committed before the tvlv data is
152 * appended as it may alter the tt tvlv container
153 */
154 batadv_tt_local_commit_changes(bat_priv);
155 tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, &ogm_buff,
156 &ogm_buff_len,
157 BATADV_OGM2_HLEN);
158
159 bat_priv->bat_v.ogm_buff = ogm_buff;
160 bat_priv->bat_v.ogm_buff_len = ogm_buff_len;
161
162 skb = netdev_alloc_skb_ip_align(NULL, ETH_HLEN + ogm_buff_len);
163 if (!skb)
164 goto reschedule;
165
166 skb_reserve(skb, ETH_HLEN);
167 pkt_buff = skb_put(skb, ogm_buff_len);
168 memcpy(pkt_buff, ogm_buff, ogm_buff_len);
169
170 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
171 ogm_packet->seqno = htonl(atomic_read(&bat_priv->bat_v.ogm_seqno));
172 atomic_inc(&bat_priv->bat_v.ogm_seqno);
173 ogm_packet->tvlv_len = htons(tvlv_len);
174
175 /* broadcast on every interface */
176 rcu_read_lock();
177 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
178 if (hard_iface->soft_iface != bat_priv->soft_iface)
179 continue;
180
27353446
SE
181 if (!kref_get_unless_zero(&hard_iface->refcount))
182 continue;
183
0da00359
AQ
184 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
185 "Sending own OGM2 packet (originator %pM, seqno %u, throughput %u, TTL %d) on interface %s [%pM]\n",
186 ogm_packet->orig, ntohl(ogm_packet->seqno),
187 ntohl(ogm_packet->throughput), ogm_packet->ttl,
188 hard_iface->net_dev->name,
189 hard_iface->net_dev->dev_addr);
190
191 /* this skb gets consumed by batadv_v_ogm_send_to_if() */
192 skb_tmp = skb_clone(skb, GFP_ATOMIC);
27353446
SE
193 if (!skb_tmp) {
194 batadv_hardif_put(hard_iface);
0da00359 195 break;
27353446 196 }
0da00359
AQ
197
198 batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
27353446 199 batadv_hardif_put(hard_iface);
0da00359
AQ
200 }
201 rcu_read_unlock();
202
203 consume_skb(skb);
204
205reschedule:
206 batadv_v_ogm_start_timer(bat_priv);
207out:
208 return;
209}
210
211/**
212 * batadv_v_ogm_iface_enable - prepare an interface for B.A.T.M.A.N. V
213 * @hard_iface: the interface to prepare
214 *
215 * Takes care of scheduling own OGM sending routine for this interface.
216 *
217 * Return: 0 on success or a negative error code otherwise
218 */
219int batadv_v_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
220{
221 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
222
223 batadv_v_ogm_start_timer(bat_priv);
224
225 return 0;
226}
227
228/**
229 * batadv_v_ogm_primary_iface_set - set a new primary interface
230 * @primary_iface: the new primary interface
231 */
232void batadv_v_ogm_primary_iface_set(struct batadv_hard_iface *primary_iface)
233{
234 struct batadv_priv *bat_priv = netdev_priv(primary_iface->soft_iface);
235 struct batadv_ogm2_packet *ogm_packet;
236
237 if (!bat_priv->bat_v.ogm_buff)
238 return;
239
240 ogm_packet = (struct batadv_ogm2_packet *)bat_priv->bat_v.ogm_buff;
241 ether_addr_copy(ogm_packet->orig, primary_iface->net_dev->dev_addr);
242}
243
9323158e
AQ
244/**
245 * batadv_v_forward_penalty - apply a penalty to the throughput metric forwarded
246 * with B.A.T.M.A.N. V OGMs
247 * @bat_priv: the bat priv with all the soft interface information
248 * @if_incoming: the interface where the OGM has been received
249 * @if_outgoing: the interface where the OGM has to be forwarded to
250 * @throughput: the current throughput
251 *
252 * Apply a penalty on the current throughput metric value based on the
253 * characteristic of the interface where the OGM has been received. The return
254 * value is computed as follows:
255 * - throughput * 50% if the incoming and outgoing interface are the
256 * same WiFi interface and the throughput is above
257 * 1MBit/s
258 * - throughput if the outgoing interface is the default
259 * interface (i.e. this OGM is processed for the
260 * internal table and not forwarded)
261 * - throughput * hop penalty otherwise
262 *
263 * Return: the penalised throughput metric.
264 */
265static u32 batadv_v_forward_penalty(struct batadv_priv *bat_priv,
266 struct batadv_hard_iface *if_incoming,
267 struct batadv_hard_iface *if_outgoing,
268 u32 throughput)
269{
270 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
271 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
272
273 /* Don't apply hop penalty in default originator table. */
274 if (if_outgoing == BATADV_IF_DEFAULT)
275 return throughput;
276
277 /* Forwarding on the same WiFi interface cuts the throughput in half
278 * due to the store & forward characteristics of WIFI.
279 * Very low throughput values are the exception.
280 */
281 if ((throughput > 10) &&
282 (if_incoming == if_outgoing) &&
c833484e 283 !(if_incoming->bat_v.flags & BATADV_FULL_DUPLEX))
9323158e
AQ
284 return throughput / 2;
285
286 /* hop penalty of 255 equals 100% */
287 return throughput * (hop_penalty_max - hop_penalty) / hop_penalty_max;
288}
289
290/**
efcc9d30
SW
291 * batadv_v_ogm_forward - check conditions and forward an OGM to the given
292 * outgoing interface
9323158e
AQ
293 * @bat_priv: the bat priv with all the soft interface information
294 * @ogm_received: previously received OGM to be forwarded
efcc9d30
SW
295 * @orig_node: the originator which has been updated
296 * @neigh_node: the neigh_node through with the OGM has been received
9323158e
AQ
297 * @if_incoming: the interface on which this OGM was received on
298 * @if_outgoing: the interface to which the OGM has to be forwarded to
299 *
300 * Forward an OGM to an interface after having altered the throughput metric and
301 * the TTL value contained in it. The original OGM isn't modified.
302 */
303static void batadv_v_ogm_forward(struct batadv_priv *bat_priv,
304 const struct batadv_ogm2_packet *ogm_received,
efcc9d30
SW
305 struct batadv_orig_node *orig_node,
306 struct batadv_neigh_node *neigh_node,
9323158e
AQ
307 struct batadv_hard_iface *if_incoming,
308 struct batadv_hard_iface *if_outgoing)
309{
efcc9d30
SW
310 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
311 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
312 struct batadv_neigh_node *router = NULL;
9323158e
AQ
313 struct batadv_ogm2_packet *ogm_forward;
314 unsigned char *skb_buff;
315 struct sk_buff *skb;
316 size_t packet_len;
317 u16 tvlv_len;
318
efcc9d30
SW
319 /* only forward for specific interfaces, not for the default one. */
320 if (if_outgoing == BATADV_IF_DEFAULT)
321 goto out;
322
323 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
324 if (!orig_ifinfo)
325 goto out;
326
327 /* acquire possibly updated router */
328 router = batadv_orig_router_get(orig_node, if_outgoing);
329
330 /* strict rule: forward packets coming from the best next hop only */
331 if (neigh_node != router)
332 goto out;
333
334 /* don't forward the same seqno twice on one interface */
335 if (orig_ifinfo->last_seqno_forwarded == ntohl(ogm_received->seqno))
336 goto out;
337
338 orig_ifinfo->last_seqno_forwarded = ntohl(ogm_received->seqno);
339
9323158e
AQ
340 if (ogm_received->ttl <= 1) {
341 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
efcc9d30 342 goto out;
9323158e
AQ
343 }
344
efcc9d30
SW
345 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
346 if (!neigh_ifinfo)
347 goto out;
348
9323158e
AQ
349 tvlv_len = ntohs(ogm_received->tvlv_len);
350
351 packet_len = BATADV_OGM2_HLEN + tvlv_len;
352 skb = netdev_alloc_skb_ip_align(if_outgoing->net_dev,
353 ETH_HLEN + packet_len);
354 if (!skb)
efcc9d30 355 goto out;
9323158e
AQ
356
357 skb_reserve(skb, ETH_HLEN);
358 skb_buff = skb_put(skb, packet_len);
359 memcpy(skb_buff, ogm_received, packet_len);
360
361 /* apply forward penalty */
362 ogm_forward = (struct batadv_ogm2_packet *)skb_buff;
efcc9d30 363 ogm_forward->throughput = htonl(neigh_ifinfo->bat_v.throughput);
9323158e
AQ
364 ogm_forward->ttl--;
365
366 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
367 "Forwarding OGM2 packet on %s: throughput %u, ttl %u, received via %s\n",
efcc9d30
SW
368 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
369 ogm_forward->ttl, if_incoming->net_dev->name);
9323158e
AQ
370
371 batadv_v_ogm_send_to_if(skb, if_outgoing);
efcc9d30
SW
372
373out:
374 if (orig_ifinfo)
375 batadv_orig_ifinfo_put(orig_ifinfo);
376 if (router)
377 batadv_neigh_node_put(router);
378 if (neigh_ifinfo)
379 batadv_neigh_ifinfo_put(neigh_ifinfo);
9323158e
AQ
380}
381
382/**
383 * batadv_v_ogm_metric_update - update route metric based on OGM
384 * @bat_priv: the bat priv with all the soft interface information
385 * @ogm2: OGM2 structure
386 * @orig_node: Originator structure for which the OGM has been received
387 * @neigh_node: the neigh_node through with the OGM has been received
388 * @if_incoming: the interface where this packet was received
389 * @if_outgoing: the interface for which the packet should be considered
390 *
391 * Return:
392 * 1 if the OGM is new,
393 * 0 if it is not new but valid,
394 * <0 on error (e.g. old OGM)
395 */
396static int batadv_v_ogm_metric_update(struct batadv_priv *bat_priv,
397 const struct batadv_ogm2_packet *ogm2,
398 struct batadv_orig_node *orig_node,
399 struct batadv_neigh_node *neigh_node,
400 struct batadv_hard_iface *if_incoming,
401 struct batadv_hard_iface *if_outgoing)
402{
403 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
404 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
405 bool protection_started = false;
406 int ret = -EINVAL;
407 u32 path_throughput;
408 s32 seq_diff;
409
410 orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
411 if (!orig_ifinfo)
412 goto out;
413
414 seq_diff = ntohl(ogm2->seqno) - orig_ifinfo->last_real_seqno;
415
416 if (!hlist_empty(&orig_node->neigh_list) &&
417 batadv_window_protected(bat_priv, seq_diff,
418 BATADV_OGM_MAX_AGE,
419 &orig_ifinfo->batman_seqno_reset,
420 &protection_started)) {
421 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
422 "Drop packet: packet within window protection time from %pM\n",
423 ogm2->orig);
424 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
425 "Last reset: %ld, %ld\n",
426 orig_ifinfo->batman_seqno_reset, jiffies);
427 goto out;
428 }
429
430 /* drop packets with old seqnos, however accept the first packet after
431 * a host has been rebooted.
432 */
433 if ((seq_diff < 0) && !protection_started)
434 goto out;
435
436 neigh_node->last_seen = jiffies;
437
438 orig_node->last_seen = jiffies;
439
440 orig_ifinfo->last_real_seqno = ntohl(ogm2->seqno);
441 orig_ifinfo->last_ttl = ogm2->ttl;
442
443 neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
444 if (!neigh_ifinfo)
445 goto out;
446
447 path_throughput = batadv_v_forward_penalty(bat_priv, if_incoming,
448 if_outgoing,
449 ntohl(ogm2->throughput));
450 neigh_ifinfo->bat_v.throughput = path_throughput;
451 neigh_ifinfo->bat_v.last_seqno = ntohl(ogm2->seqno);
452 neigh_ifinfo->last_ttl = ogm2->ttl;
453
454 if (seq_diff > 0 || protection_started)
455 ret = 1;
456 else
457 ret = 0;
458out:
459 if (orig_ifinfo)
460 batadv_orig_ifinfo_put(orig_ifinfo);
461 if (neigh_ifinfo)
462 batadv_neigh_ifinfo_put(neigh_ifinfo);
463
464 return ret;
465}
466
467/**
468 * batadv_v_ogm_route_update - update routes based on OGM
469 * @bat_priv: the bat priv with all the soft interface information
470 * @ethhdr: the Ethernet header of the OGM2
471 * @ogm2: OGM2 structure
472 * @orig_node: Originator structure for which the OGM has been received
473 * @neigh_node: the neigh_node through with the OGM has been received
474 * @if_incoming: the interface where this packet was received
475 * @if_outgoing: the interface for which the packet should be considered
efcc9d30
SW
476 *
477 * Return: true if the packet should be forwarded, false otherwise
9323158e 478 */
efcc9d30 479static bool batadv_v_ogm_route_update(struct batadv_priv *bat_priv,
9323158e
AQ
480 const struct ethhdr *ethhdr,
481 const struct batadv_ogm2_packet *ogm2,
482 struct batadv_orig_node *orig_node,
483 struct batadv_neigh_node *neigh_node,
484 struct batadv_hard_iface *if_incoming,
485 struct batadv_hard_iface *if_outgoing)
486{
487 struct batadv_neigh_node *router = NULL;
9323158e 488 struct batadv_orig_node *orig_neigh_node = NULL;
9323158e 489 struct batadv_neigh_node *orig_neigh_router = NULL;
86de37c1
SW
490 struct batadv_neigh_ifinfo *router_ifinfo = NULL, *neigh_ifinfo = NULL;
491 u32 router_throughput, neigh_throughput;
492 u32 router_last_seqno;
493 u32 neigh_last_seqno;
494 s32 neigh_seq_diff;
efcc9d30 495 bool forward = false;
9323158e
AQ
496
497 orig_neigh_node = batadv_v_ogm_orig_get(bat_priv, ethhdr->h_source);
498 if (!orig_neigh_node)
499 goto out;
500
501 orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
502 if_outgoing);
503
504 /* drop packet if sender is not a direct neighbor and if we
505 * don't route towards it
506 */
507 router = batadv_orig_router_get(orig_node, if_outgoing);
508 if (router && router->orig_node != orig_node && !orig_neigh_router) {
509 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
510 "Drop packet: OGM via unknown neighbor!\n");
511 goto out;
512 }
513
86de37c1
SW
514 /* Mark the OGM to be considered for forwarding, and update routes
515 * if needed.
516 */
efcc9d30 517 forward = true;
86de37c1
SW
518
519 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
520 "Searching and updating originator entry of received packet\n");
521
522 /* if this neighbor already is our next hop there is nothing
523 * to change
524 */
525 if (router == neigh_node)
526 goto out;
527
528 /* don't consider neighbours with worse throughput.
529 * also switch route if this seqno is BATADV_V_MAX_ORIGDIFF newer than
530 * the last received seqno from our best next hop.
531 */
532 if (router) {
533 router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
534 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
535
536 /* if these are not allocated, something is wrong. */
537 if (!router_ifinfo || !neigh_ifinfo)
538 goto out;
539
540 neigh_last_seqno = neigh_ifinfo->bat_v.last_seqno;
541 router_last_seqno = router_ifinfo->bat_v.last_seqno;
542 neigh_seq_diff = neigh_last_seqno - router_last_seqno;
543 router_throughput = router_ifinfo->bat_v.throughput;
544 neigh_throughput = neigh_ifinfo->bat_v.throughput;
545
546 if ((neigh_seq_diff < BATADV_OGM_MAX_ORIGDIFF) &&
547 (router_throughput >= neigh_throughput))
548 goto out;
549 }
550
551 batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
9323158e 552out:
9323158e
AQ
553 if (router)
554 batadv_neigh_node_put(router);
555 if (orig_neigh_router)
556 batadv_neigh_node_put(orig_neigh_router);
557 if (orig_neigh_node)
558 batadv_orig_node_put(orig_neigh_node);
86de37c1
SW
559 if (router_ifinfo)
560 batadv_neigh_ifinfo_put(router_ifinfo);
561 if (neigh_ifinfo)
562 batadv_neigh_ifinfo_put(neigh_ifinfo);
efcc9d30
SW
563
564 return forward;
9323158e
AQ
565}
566
567/**
568 * batadv_v_ogm_process_per_outif - process a batman v OGM for an outgoing if
569 * @bat_priv: the bat priv with all the soft interface information
570 * @ethhdr: the Ethernet header of the OGM2
571 * @ogm2: OGM2 structure
572 * @orig_node: Originator structure for which the OGM has been received
573 * @neigh_node: the neigh_node through with the OGM has been received
574 * @if_incoming: the interface where this packet was received
575 * @if_outgoing: the interface for which the packet should be considered
576 */
577static void
578batadv_v_ogm_process_per_outif(struct batadv_priv *bat_priv,
579 const struct ethhdr *ethhdr,
580 const struct batadv_ogm2_packet *ogm2,
581 struct batadv_orig_node *orig_node,
582 struct batadv_neigh_node *neigh_node,
583 struct batadv_hard_iface *if_incoming,
584 struct batadv_hard_iface *if_outgoing)
585{
586 int seqno_age;
efcc9d30 587 bool forward;
9323158e
AQ
588
589 /* first, update the metric with according sanity checks */
590 seqno_age = batadv_v_ogm_metric_update(bat_priv, ogm2, orig_node,
591 neigh_node, if_incoming,
592 if_outgoing);
593
594 /* outdated sequence numbers are to be discarded */
595 if (seqno_age < 0)
596 return;
597
598 /* only unknown & newer OGMs contain TVLVs we are interested in */
599 if ((seqno_age > 0) && (if_outgoing == BATADV_IF_DEFAULT))
600 batadv_tvlv_containers_process(bat_priv, true, orig_node,
601 NULL, NULL,
602 (unsigned char *)(ogm2 + 1),
603 ntohs(ogm2->tvlv_len));
604
605 /* if the metric update went through, update routes if needed */
efcc9d30
SW
606 forward = batadv_v_ogm_route_update(bat_priv, ethhdr, ogm2, orig_node,
607 neigh_node, if_incoming,
608 if_outgoing);
609
610 /* if the routes have been processed correctly, check and forward */
611 if (forward)
612 batadv_v_ogm_forward(bat_priv, ogm2, orig_node, neigh_node,
613 if_incoming, if_outgoing);
9323158e
AQ
614}
615
616/**
617 * batadv_v_ogm_aggr_packet - checks if there is another OGM aggregated
618 * @buff_pos: current position in the skb
619 * @packet_len: total length of the skb
620 * @tvlv_len: tvlv length of the previously considered OGM
621 *
622 * Return: true if there is enough space for another OGM, false otherwise.
623 */
624static bool batadv_v_ogm_aggr_packet(int buff_pos, int packet_len,
625 __be16 tvlv_len)
626{
627 int next_buff_pos = 0;
628
629 next_buff_pos += buff_pos + BATADV_OGM2_HLEN;
630 next_buff_pos += ntohs(tvlv_len);
631
632 return (next_buff_pos <= packet_len) &&
633 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
634}
635
636/**
637 * batadv_v_ogm_process - process an incoming batman v OGM
638 * @skb: the skb containing the OGM
639 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
640 * @if_incoming: the interface where this packet was receved
641 */
642static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
643 struct batadv_hard_iface *if_incoming)
644{
645 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
646 struct ethhdr *ethhdr;
647 struct batadv_orig_node *orig_node = NULL;
648 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
649 struct batadv_neigh_node *neigh_node = NULL;
650 struct batadv_hard_iface *hard_iface;
651 struct batadv_ogm2_packet *ogm_packet;
652 u32 ogm_throughput, link_throughput, path_throughput;
653
654 ethhdr = eth_hdr(skb);
655 ogm_packet = (struct batadv_ogm2_packet *)(skb->data + ogm_offset);
656
657 ogm_throughput = ntohl(ogm_packet->throughput);
658
659 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
660 "Received OGM2 packet via NB: %pM, IF: %s [%pM] (from OG: %pM, seqno %u, troughput %u, TTL %u, V %u, tvlv_len %u)\n",
661 ethhdr->h_source, if_incoming->net_dev->name,
662 if_incoming->net_dev->dev_addr, ogm_packet->orig,
663 ntohl(ogm_packet->seqno), ogm_throughput, ogm_packet->ttl,
664 ogm_packet->version, ntohs(ogm_packet->tvlv_len));
665
666 /* If the troughput metric is 0, immediately drop the packet. No need to
667 * create orig_node / neigh_node for an unusable route.
668 */
669 if (ogm_throughput == 0) {
670 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
671 "Drop packet: originator packet with troughput metric of 0\n");
672 return;
673 }
674
675 /* require ELP packets be to received from this neighbor first */
676 hardif_neigh = batadv_hardif_neigh_get(if_incoming, ethhdr->h_source);
677 if (!hardif_neigh) {
678 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
679 "Drop packet: OGM via unknown neighbor!\n");
680 goto out;
681 }
682
683 orig_node = batadv_v_ogm_orig_get(bat_priv, ogm_packet->orig);
684 if (!orig_node)
685 return;
686
6f0a6b5e
ML
687 neigh_node = batadv_neigh_node_get_or_create(orig_node, if_incoming,
688 ethhdr->h_source);
9323158e
AQ
689 if (!neigh_node)
690 goto out;
691
692 /* Update the received throughput metric to match the link
693 * characteristic:
694 * - If this OGM traveled one hop so far (emitted by single hop
695 * neighbor) the path throughput metric equals the link throughput.
696 * - For OGMs traversing more than hop the path throughput metric is
697 * the smaller of the path throughput and the link throughput.
698 */
699 link_throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
700 path_throughput = min_t(u32, link_throughput, ogm_throughput);
701 ogm_packet->throughput = htonl(path_throughput);
702
703 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet, orig_node,
704 neigh_node, if_incoming,
705 BATADV_IF_DEFAULT);
706
707 rcu_read_lock();
708 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
709 if (hard_iface->if_status != BATADV_IF_ACTIVE)
710 continue;
711
712 if (hard_iface->soft_iface != bat_priv->soft_iface)
713 continue;
714
27353446
SE
715 if (!kref_get_unless_zero(&hard_iface->refcount))
716 continue;
717
9323158e
AQ
718 batadv_v_ogm_process_per_outif(bat_priv, ethhdr, ogm_packet,
719 orig_node, neigh_node,
720 if_incoming, hard_iface);
27353446
SE
721
722 batadv_hardif_put(hard_iface);
9323158e
AQ
723 }
724 rcu_read_unlock();
725out:
726 if (orig_node)
727 batadv_orig_node_put(orig_node);
728 if (neigh_node)
729 batadv_neigh_node_put(neigh_node);
730 if (hardif_neigh)
731 batadv_hardif_neigh_put(hardif_neigh);
732}
733
0da00359
AQ
734/**
735 * batadv_v_ogm_packet_recv - OGM2 receiving handler
736 * @skb: the received OGM
737 * @if_incoming: the interface where this OGM has been received
738 *
739 * Return: NET_RX_SUCCESS and consume the skb on success or returns NET_RX_DROP
740 * (without freeing the skb) on failure
741 */
742int batadv_v_ogm_packet_recv(struct sk_buff *skb,
743 struct batadv_hard_iface *if_incoming)
744{
745 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
746 struct batadv_ogm2_packet *ogm_packet;
747 struct ethhdr *ethhdr = eth_hdr(skb);
9323158e
AQ
748 int ogm_offset;
749 u8 *packet_pos;
750 int ret = NET_RX_DROP;
0da00359
AQ
751
752 /* did we receive a OGM2 packet on an interface that does not have
753 * B.A.T.M.A.N. V enabled ?
754 */
755 if (strcmp(bat_priv->bat_algo_ops->name, "BATMAN_V") != 0)
756 return NET_RX_DROP;
757
758 if (!batadv_check_management_packet(skb, if_incoming, BATADV_OGM2_HLEN))
759 return NET_RX_DROP;
760
761 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
762 return NET_RX_DROP;
763
764 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
765
766 if (batadv_is_my_mac(bat_priv, ogm_packet->orig))
767 return NET_RX_DROP;
768
769 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
770 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
771 skb->len + ETH_HLEN);
772
9323158e
AQ
773 ogm_offset = 0;
774 ogm_packet = (struct batadv_ogm2_packet *)skb->data;
775
776 while (batadv_v_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
777 ogm_packet->tvlv_len)) {
778 batadv_v_ogm_process(skb, ogm_offset, if_incoming);
779
780 ogm_offset += BATADV_OGM2_HLEN;
781 ogm_offset += ntohs(ogm_packet->tvlv_len);
782
783 packet_pos = skb->data + ogm_offset;
784 ogm_packet = (struct batadv_ogm2_packet *)packet_pos;
785 }
786
787 ret = NET_RX_SUCCESS;
0da00359 788 consume_skb(skb);
9323158e
AQ
789
790 return ret;
0da00359
AQ
791}
792
793/**
794 * batadv_v_ogm_init - initialise the OGM2 engine
795 * @bat_priv: the bat priv with all the soft interface information
796 *
797 * Return: 0 on success or a negative error code in case of failure
798 */
799int batadv_v_ogm_init(struct batadv_priv *bat_priv)
800{
801 struct batadv_ogm2_packet *ogm_packet;
802 unsigned char *ogm_buff;
803 u32 random_seqno;
804
805 bat_priv->bat_v.ogm_buff_len = BATADV_OGM2_HLEN;
806 ogm_buff = kzalloc(bat_priv->bat_v.ogm_buff_len, GFP_ATOMIC);
807 if (!ogm_buff)
808 return -ENOMEM;
809
810 bat_priv->bat_v.ogm_buff = ogm_buff;
811 ogm_packet = (struct batadv_ogm2_packet *)ogm_buff;
812 ogm_packet->packet_type = BATADV_OGM2;
813 ogm_packet->version = BATADV_COMPAT_VERSION;
814 ogm_packet->ttl = BATADV_TTL;
815 ogm_packet->flags = BATADV_NO_FLAGS;
816 ogm_packet->throughput = htonl(BATADV_THROUGHPUT_MAX_VALUE);
817
818 /* randomize initial seqno to avoid collision */
819 get_random_bytes(&random_seqno, sizeof(random_seqno));
820 atomic_set(&bat_priv->bat_v.ogm_seqno, random_seqno);
821 INIT_DELAYED_WORK(&bat_priv->bat_v.ogm_wq, batadv_v_ogm_send);
822
823 return 0;
824}
825
826/**
827 * batadv_v_ogm_free - free OGM private resources
828 * @bat_priv: the bat priv with all the soft interface information
829 */
830void batadv_v_ogm_free(struct batadv_priv *bat_priv)
831{
832 cancel_delayed_work_sync(&bat_priv->bat_v.ogm_wq);
833
834 kfree(bat_priv->bat_v.ogm_buff);
835 bat_priv->bat_v.ogm_buff = NULL;
836 bat_priv->bat_v.ogm_buff_len = 0;
837}
This page took 0.088204 seconds and 5 git commands to generate.