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