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