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