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