blob: 62da5278014a77e1e8a60902541b1999b2f85964 [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
Marek Lindnerfc957272011-07-30 12:04:12 +02002 *
3 * Marek Lindner, Simon Wunderlich
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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Marek Lindnerfc957272011-07-30 12:04:12 +020018 */
19
20#include "main.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020021#include "translation-table.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020022#include "originator.h"
23#include "routing.h"
24#include "gateway_common.h"
25#include "gateway_client.h"
26#include "hard-interface.h"
27#include "send.h"
Marek Lindner1c280472011-11-28 17:40:17 +080028#include "bat_algo.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010029#include "network-coding.h"
Marek Lindnerfc957272011-07-30 12:04:12 +020030
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020031/**
32 * batadv_ring_buffer_set - update the ring buffer with the given value
33 * @lq_recv: pointer to the ring buffer
34 * @lq_index: index to store the value at
35 * @value: value to store in the ring buffer
36 */
37static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
38 uint8_t value)
39{
40 lq_recv[*lq_index] = value;
41 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
42}
43
44/**
45 * batadv_ring_buffer_set - compute the average of all non-zero values stored
46 * in the given ring buffer
47 * @lq_recv: pointer to the ring buffer
48 *
49 * Returns computed average value.
50 */
51static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
52{
53 const uint8_t *ptr;
54 uint16_t count = 0, i = 0, sum = 0;
55
56 ptr = lq_recv;
57
58 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
59 if (*ptr != 0) {
60 count++;
61 sum += *ptr;
62 }
63
64 i++;
65 ptr++;
66 }
67
68 if (count == 0)
69 return 0;
70
71 return (uint8_t)(sum / count);
72}
David S. Millerd98cae64e2013-06-19 16:49:39 -070073
74/*
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +020075 * batadv_dup_status - duplicate status
76 * @BATADV_NO_DUP: the packet is a duplicate
77 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
78 * neighbor)
79 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
80 * @BATADV_PROTECTED: originator is currently protected (after reboot)
81 */
82enum batadv_dup_status {
83 BATADV_NO_DUP = 0,
84 BATADV_ORIG_DUP,
85 BATADV_NEIGH_DUP,
86 BATADV_PROTECTED,
87};
88
Sven Eckelmann56303d32012-06-05 22:31:31 +020089static struct batadv_neigh_node *
90batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
91 const uint8_t *neigh_addr,
92 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +010093 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +080094{
Sven Eckelmann56303d32012-06-05 22:31:31 +020095 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +080096
Antonio Quartulli863dd7a2013-03-25 13:49:46 +010097 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +080098 if (!neigh_node)
99 goto out;
100
101 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800102
103 neigh_node->orig_node = orig_neigh;
104 neigh_node->if_incoming = hard_iface;
105
106 spin_lock_bh(&orig_node->neigh_list_lock);
107 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
108 spin_unlock_bh(&orig_node->neigh_list_lock);
109
110out:
111 return neigh_node;
112}
113
Sven Eckelmann56303d32012-06-05 22:31:31 +0200114static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200115{
Sven Eckelmann96412692012-06-05 22:31:30 +0200116 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200117 unsigned char *ogm_buff;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800118 uint32_t random_seqno;
Sven Eckelmann5346c352012-05-05 13:27:28 +0200119 int res = -ENOMEM;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800120
121 /* randomize initial seqno to avoid collision */
122 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200123 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200124
Marek Lindner14511512012-08-02 17:20:26 +0200125 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
126 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
127 if (!ogm_buff)
Marek Lindner77af7572012-02-07 17:20:48 +0800128 goto out;
129
Marek Lindner14511512012-08-02 17:20:26 +0200130 hard_iface->bat_iv.ogm_buff = ogm_buff;
131
132 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200133 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
134 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
135 batadv_ogm_packet->header.ttl = 2;
136 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
137 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
138 batadv_ogm_packet->tt_num_changes = 0;
139 batadv_ogm_packet->ttvn = 0;
Marek Lindner77af7572012-02-07 17:20:48 +0800140
141 res = 0;
142
143out:
144 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200145}
146
Sven Eckelmann56303d32012-06-05 22:31:31 +0200147static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800148{
Marek Lindner14511512012-08-02 17:20:26 +0200149 kfree(hard_iface->bat_iv.ogm_buff);
150 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800151}
152
Sven Eckelmann56303d32012-06-05 22:31:31 +0200153static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200154{
Sven Eckelmann96412692012-06-05 22:31:30 +0200155 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200156 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200157
Marek Lindner14511512012-08-02 17:20:26 +0200158 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200159 memcpy(batadv_ogm_packet->orig,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200160 hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200161 memcpy(batadv_ogm_packet->prev_sender,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200162 hard_iface->net_dev->dev_addr, ETH_ALEN);
163}
164
Sven Eckelmann56303d32012-06-05 22:31:31 +0200165static void
166batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800167{
Sven Eckelmann96412692012-06-05 22:31:30 +0200168 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200169 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800170
Marek Lindner14511512012-08-02 17:20:26 +0200171 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200172 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
173 batadv_ogm_packet->header.ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800174}
175
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200176/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200177static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200178batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200179{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200180 unsigned int msecs;
181
182 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900183 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200184
185 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200186}
187
188/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200189static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200190{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900191 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200192}
193
194/* apply hop penalty for a normal link */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200195static uint8_t batadv_hop_penalty(uint8_t tq,
196 const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200197{
198 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200199 int new_tq;
200
201 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
202 new_tq /= BATADV_TQ_MAX_VALUE;
203
204 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200205}
206
Marek Lindnerfc957272011-07-30 12:04:12 +0200207/* is there another aggregated packet here? */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200208static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
209 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +0200210{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200211 int next_buff_pos = 0;
212
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200213 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200214 next_buff_pos += batadv_tt_len(tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +0200215
216 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200217 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200218}
219
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200220/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200221static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
222 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200223{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200224 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200225 char *fwd_str;
226 uint8_t packet_num;
227 int16_t buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200228 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200229 struct sk_buff *skb;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200230 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200231
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200232 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200233 return;
234
235 packet_num = 0;
236 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200237 packet_pos = forw_packet->skb->data;
238 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200239
240 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200241 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Sven Eckelmann96412692012-06-05 22:31:30 +0200242 batadv_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200243 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200244 * ordinary base packet
245 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200246 if (forw_packet->direct_link_flags & BIT(packet_num) &&
247 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200248 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200249 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200250 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200251
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200252 if (packet_num > 0 || !forw_packet->own)
253 fwd_str = "Forwarding";
254 else
255 fwd_str = "Sending own";
256
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200257 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200258 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
259 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200260 batadv_ogm_packet->orig,
261 ntohl(batadv_ogm_packet->seqno),
262 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
263 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200264 "on" : "off"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200265 batadv_ogm_packet->ttvn, hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200266 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200267
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200268 buff_pos += BATADV_OGM_HLEN;
Sven Eckelmann96412692012-06-05 22:31:30 +0200269 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200270 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200271 packet_pos = forw_packet->skb->data + buff_pos;
272 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200273 }
274
275 /* create clone because function is called more than once */
276 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200277 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200278 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
279 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200280 skb->len + ETH_HLEN);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200281 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200282 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200283}
284
285/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200286static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200287{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200288 struct batadv_hard_iface *hard_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200289 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200290 struct batadv_priv *bat_priv;
291 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200292 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200293 unsigned char directlink;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200294 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200295
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200296 packet_pos = forw_packet->skb->data;
297 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200298 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200299
300 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100301 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200302 goto out;
303 }
304
305 soft_iface = forw_packet->if_incoming->soft_iface;
306 bat_priv = netdev_priv(soft_iface);
307
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200308 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200309 goto out;
310
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200311 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200312 if (!primary_if)
313 goto out;
314
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200315 /* multihomed peer assumed
316 * non-primary OGMs are only broadcasted on their interface
317 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200318 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200319 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200320 /* FIXME: what about aggregated packets ? */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200321 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200322 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
323 (forw_packet->own ? "Sending own" : "Forwarding"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200324 batadv_ogm_packet->orig,
325 ntohl(batadv_ogm_packet->seqno),
326 batadv_ogm_packet->header.ttl,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200327 forw_packet->if_incoming->net_dev->name,
328 forw_packet->if_incoming->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200329
330 /* skb is only used once and than forw_packet is free'd */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200331 batadv_send_skb_packet(forw_packet->skb,
332 forw_packet->if_incoming,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200333 batadv_broadcast_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200334 forw_packet->skb = NULL;
335
336 goto out;
337 }
338
339 /* broadcast on every interface */
340 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200341 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200342 if (hard_iface->soft_iface != soft_iface)
343 continue;
344
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200345 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200346 }
347 rcu_read_unlock();
348
349out:
350 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200351 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200352}
353
354/* return true if new_packet can be aggregated with forw_packet */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200355static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200356batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200358 int packet_len, unsigned long send_time,
359 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200360 const struct batadv_hard_iface *if_incoming,
361 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200362{
Sven Eckelmann96412692012-06-05 22:31:30 +0200363 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200364 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200365 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200366 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200367 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200368
Sven Eckelmann96412692012-06-05 22:31:30 +0200369 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200370 aggregation_end_time = send_time;
371 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200372
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200373 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200374 * if:
375 *
376 * - the send time is within our MAX_AGGREGATION_MS time
377 * - the resulting packet wont be bigger than
378 * MAX_AGGREGATION_BYTES
379 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200380 if (time_before(send_time, forw_packet->send_time) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200381 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
382 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200383 /* check aggregation compatibility
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200384 * -> direct link packets are broadcasted on
385 * their interface only
386 * -> aggregate packet if the current packet is
387 * a "global" packet as well as the base
388 * packet
389 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200390 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200391 if (!primary_if)
392 goto out;
393
394 /* packets without direct link flag and high TTL
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200395 * are flooded through the net
396 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200397 if ((!directlink) &&
Sven Eckelmann96412692012-06-05 22:31:30 +0200398 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
399 (batadv_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200400
401 /* own packets originating non-primary
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200402 * interfaces leave only that interface
403 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200404 ((!forw_packet->own) ||
405 (forw_packet->if_incoming == primary_if))) {
406 res = true;
407 goto out;
408 }
409
410 /* if the incoming packet is sent via this one
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200411 * interface only - we still can aggregate
412 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200413 if ((directlink) &&
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200414 (new_bat_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200415 (forw_packet->if_incoming == if_incoming) &&
416
417 /* packets from direct neighbors or
418 * own secondary interface packets
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200419 * (= secondary interface packets in general)
420 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200421 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200422 (forw_packet->own &&
423 forw_packet->if_incoming != primary_if))) {
424 res = true;
425 goto out;
426 }
427 }
428
429out:
430 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200431 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200432 return res;
433}
434
435/* create a new aggregated packet and add this packet to it */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200436static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
437 int packet_len, unsigned long send_time,
438 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200439 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200440 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200441{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200442 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
443 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200444 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200445 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200446
447 if (!atomic_inc_not_zero(&if_incoming->refcount))
448 return;
449
450 /* own packet should always be scheduled */
451 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200452 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200453 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200454 "batman packet queue full\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200455 goto out;
456 }
457 }
458
459 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
460 if (!forw_packet_aggr) {
461 if (!own_packet)
462 atomic_inc(&bat_priv->batman_queue_left);
463 goto out;
464 }
465
466 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200467 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
Sven Eckelmann5b246572012-11-04 17:11:45 +0100468 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200469 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100470 skb_size = packet_len;
471
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200472 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200473
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200474 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200475 if (!forw_packet_aggr->skb) {
476 if (!own_packet)
477 atomic_inc(&bat_priv->batman_queue_left);
478 kfree(forw_packet_aggr);
479 goto out;
480 }
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200481 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200482
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200483 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
484 forw_packet_aggr->packet_len = packet_len;
485 memcpy(skb_buff, packet_buff, packet_len);
486
487 forw_packet_aggr->own = own_packet;
488 forw_packet_aggr->if_incoming = if_incoming;
489 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200490 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200491 forw_packet_aggr->send_time = send_time;
492
493 /* save packet direct link flag status */
494 if (direct_link)
495 forw_packet_aggr->direct_link_flags |= 1;
496
497 /* add new packet to packet list */
498 spin_lock_bh(&bat_priv->forw_bat_list_lock);
499 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
500 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
501
502 /* start timer for this packet */
503 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200504 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200505 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200506 &forw_packet_aggr->delayed_work,
507 send_time - jiffies);
508
509 return;
510out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200511 batadv_hardif_free_ref(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200512}
513
514/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200515static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200516 const unsigned char *packet_buff,
517 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200518{
519 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200520 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200521
522 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
523 memcpy(skb_buff, packet_buff, packet_len);
524 forw_packet_aggr->packet_len += packet_len;
525 forw_packet_aggr->num_packets++;
526
527 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200528 if (direct_link) {
529 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
530 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
531 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200532}
533
Sven Eckelmann56303d32012-06-05 22:31:31 +0200534static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200535 unsigned char *packet_buff,
536 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200537 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200538 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200539{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200540 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200541 * _pos -> pointer to the position in the queue
542 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200543 struct batadv_forw_packet *forw_packet_aggr = NULL;
544 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200545 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200546 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200547 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200548
Sven Eckelmann96412692012-06-05 22:31:30 +0200549 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
550 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200551 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200552
553 /* find position for the packet in the forward queue */
554 spin_lock_bh(&bat_priv->forw_bat_list_lock);
555 /* own packets are not to be aggregated */
556 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800557 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200558 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200559 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200560 bat_priv, packet_len,
561 send_time, direct_link,
562 if_incoming,
563 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200564 forw_packet_aggr = forw_packet_pos;
565 break;
566 }
567 }
568 }
569
570 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200571 * suitable aggregation packet found
572 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200573 if (!forw_packet_aggr) {
574 /* the following section can run without the lock */
575 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
576
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200577 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200578 * we hold it back for a while, so that it might be aggregated
579 * later on
580 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200581 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
582 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200583
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200584 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
585 send_time, direct_link,
586 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200587 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200588 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
589 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200590 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
591 }
592}
593
Sven Eckelmann56303d32012-06-05 22:31:31 +0200594static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200595 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200596 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200597 bool is_single_hop_neigh,
598 bool is_from_best_next_hop,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200599 struct batadv_hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200600{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200602 uint8_t tt_num_changes;
603
Sven Eckelmann96412692012-06-05 22:31:30 +0200604 if (batadv_ogm_packet->header.ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200605 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200606 return;
607 }
608
Marek Lindner13b25412012-03-11 06:17:53 +0800609 if (!is_from_best_next_hop) {
610 /* Mark the forwarded packet when it is not coming from our
611 * best next hop. We still need to forward the packet for our
612 * neighbor link quality detection to work in case the packet
613 * originated from a single hop neighbor. Otherwise we can
614 * simply drop the ogm.
615 */
616 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200617 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800618 else
619 return;
620 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200621
Sven Eckelmann96412692012-06-05 22:31:30 +0200622 tt_num_changes = batadv_ogm_packet->tt_num_changes;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200623
Sven Eckelmann96412692012-06-05 22:31:30 +0200624 batadv_ogm_packet->header.ttl--;
625 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200626
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200627 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200628 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200629 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200630
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200631 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200632 "Forwarding packet: tq: %i, ttl: %i\n",
Sven Eckelmann96412692012-06-05 22:31:30 +0200633 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200634
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200635 /* switch of primaries first hop flag when forwarding */
Sven Eckelmann96412692012-06-05 22:31:30 +0200636 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800637 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200638 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200639 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200640 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200641
Sven Eckelmann96412692012-06-05 22:31:30 +0200642 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200643 BATADV_OGM_HLEN + batadv_tt_len(tt_num_changes),
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200644 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200645}
646
Antonio Quartullid9896612013-04-17 17:44:43 +0200647/**
648 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
649 * the given interface
650 * @hard_iface: the interface for which the windows have to be shifted
651 */
652static void
653batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
654{
655 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
656 struct batadv_hashtable *hash = bat_priv->orig_hash;
657 struct hlist_head *head;
658 struct batadv_orig_node *orig_node;
659 unsigned long *word;
660 uint32_t i;
661 size_t word_index;
662 uint8_t *w;
663
664 for (i = 0; i < hash->size; i++) {
665 head = &hash->table[i];
666
667 rcu_read_lock();
668 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
669 spin_lock_bh(&orig_node->ogm_cnt_lock);
670 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
671 word = &(orig_node->bcast_own[word_index]);
672
673 batadv_bit_get_packet(bat_priv, word, 1, 0);
674 w = &orig_node->bcast_own_sum[hard_iface->if_num];
675 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
676 spin_unlock_bh(&orig_node->ogm_cnt_lock);
677 }
678 rcu_read_unlock();
679 }
680}
681
Sven Eckelmann56303d32012-06-05 22:31:31 +0200682static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200683{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200685 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200686 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200687 struct batadv_hard_iface *primary_if;
Marek Lindner14511512012-08-02 17:20:26 +0200688 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800689 int vis_server, tt_num_changes = 0;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200690 uint32_t seqno;
691 uint8_t bandwidth;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200692
693 vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200694 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200695
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800696 if (hard_iface == primary_if)
Marek Lindner14511512012-08-02 17:20:26 +0200697 tt_num_changes = batadv_tt_append_diff(bat_priv, ogm_buff,
698 ogm_buff_len,
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200699 BATADV_OGM_HLEN);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800700
Marek Lindner14511512012-08-02 17:20:26 +0200701 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200702
703 /* change sequence number to network order */
Marek Lindner14511512012-08-02 17:20:26 +0200704 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200705 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200706 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200707
Sven Eckelmann807736f2012-07-15 22:26:51 +0200708 batadv_ogm_packet->ttvn = atomic_read(&bat_priv->tt.vn);
709 batadv_ogm_packet->tt_crc = htons(bat_priv->tt.local_crc);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200710 if (tt_num_changes >= 0)
Sven Eckelmann96412692012-06-05 22:31:30 +0200711 batadv_ogm_packet->tt_num_changes = tt_num_changes;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200712
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200713 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC)
Sven Eckelmann96412692012-06-05 22:31:30 +0200714 batadv_ogm_packet->flags |= BATADV_VIS_SERVER;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200715 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200716 batadv_ogm_packet->flags &= ~BATADV_VIS_SERVER;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200717
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200718 if (hard_iface == primary_if &&
719 atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_SERVER) {
720 bandwidth = (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
721 batadv_ogm_packet->gw_flags = bandwidth;
722 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +0200723 batadv_ogm_packet->gw_flags = BATADV_NO_FLAGS;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200724 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200725
Antonio Quartullid9896612013-04-17 17:44:43 +0200726 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200727 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
728 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200729 batadv_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200730
731 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200732 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200733}
734
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200735static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200736batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
737 struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200738 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200739 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200740 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200741 const unsigned char *tt_buff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200742 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200743{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200744 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
745 struct batadv_neigh_node *router = NULL;
746 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200747 int if_num;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200748 uint8_t sum_orig, sum_neigh;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200749 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200750 uint8_t tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200751
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200752 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200753 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200754
755 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800756 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200757 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200758 neigh_addr = tmp_neigh_node->addr;
759 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
760 tmp_neigh_node->if_incoming == if_incoming &&
761 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100762 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200763 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200764 neigh_node = tmp_neigh_node;
765 continue;
766 }
767
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200768 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200769 continue;
770
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800771 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200772 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
773 &tmp_neigh_node->tq_index, 0);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200774 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
775 tmp_neigh_node->tq_avg = tq_avg;
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800776 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200777 }
778
779 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200780 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +0200781
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200782 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +0200783 if (!orig_tmp)
784 goto unlock;
785
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200786 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
787 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100788 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200789
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200790 batadv_orig_node_free_ref(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200791 if (!neigh_node)
792 goto unlock;
793 } else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200794 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200795 "Updating existing last-hop neighbor of originator\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200796
797 rcu_read_unlock();
798
Sven Eckelmann96412692012-06-05 22:31:30 +0200799 orig_node->flags = batadv_ogm_packet->flags;
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800800 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200801
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800802 spin_lock_bh(&neigh_node->lq_update_lock);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200803 batadv_ring_buffer_set(neigh_node->tq_recv,
804 &neigh_node->tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +0200805 batadv_ogm_packet->tq);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200806 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800807 spin_unlock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200808
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200809 if (dup_status == BATADV_NO_DUP) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200810 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
811 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200812 }
813
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200814 batadv_bonding_candidate_add(orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200815
816 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200817 * to change
818 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200819 router = batadv_orig_node_get_router(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200820 if (router == neigh_node)
821 goto update_tt;
822
823 /* if this neighbor does not offer a better TQ we won't consider it */
824 if (router && (router->tq_avg > neigh_node->tq_avg))
825 goto update_tt;
826
827 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200828 * won't consider it either
829 */
Marek Lindnerfc957272011-07-30 12:04:12 +0200830 if (router && (neigh_node->tq_avg == router->tq_avg)) {
831 orig_node_tmp = router->orig_node;
832 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200833 if_num = router->if_incoming->if_num;
834 sum_orig = orig_node_tmp->bcast_own_sum[if_num];
Marek Lindnerfc957272011-07-30 12:04:12 +0200835 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
836
837 orig_node_tmp = neigh_node->orig_node;
838 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200839 if_num = neigh_node->if_incoming->if_num;
840 sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
Marek Lindnerfc957272011-07-30 12:04:12 +0200841 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
842
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200843 if (sum_orig >= sum_neigh)
Marek Lindnerfc957272011-07-30 12:04:12 +0200844 goto update_tt;
845 }
846
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200847 batadv_update_route(bat_priv, orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200848
849update_tt:
850 /* I have to check for transtable changes only if the OGM has been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200851 * sent through a primary interface
852 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200853 if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
854 (batadv_ogm_packet->header.ttl > 2)) ||
855 (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200856 batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
Sven Eckelmann96412692012-06-05 22:31:30 +0200857 batadv_ogm_packet->tt_num_changes,
858 batadv_ogm_packet->ttvn,
859 ntohs(batadv_ogm_packet->tt_crc));
Marek Lindnerfc957272011-07-30 12:04:12 +0200860
Sven Eckelmann96412692012-06-05 22:31:30 +0200861 if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200862 batadv_gw_node_update(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200863 batadv_ogm_packet->gw_flags);
Marek Lindnerfc957272011-07-30 12:04:12 +0200864
Sven Eckelmann96412692012-06-05 22:31:30 +0200865 orig_node->gw_flags = batadv_ogm_packet->gw_flags;
Marek Lindnerfc957272011-07-30 12:04:12 +0200866
867 /* restart gateway selection if fast or late switching was enabled */
868 if ((orig_node->gw_flags) &&
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200869 (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
Marek Lindnerfc957272011-07-30 12:04:12 +0200870 (atomic_read(&bat_priv->gw_sel_class) > 2))
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200871 batadv_gw_check_election(bat_priv, orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200872
873 goto out;
874
875unlock:
876 rcu_read_unlock();
877out:
878 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200879 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200880 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200881 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +0200882}
883
Sven Eckelmann56303d32012-06-05 22:31:31 +0200884static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
885 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200886 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200887 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200888{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200889 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
890 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Marek Lindnerfc957272011-07-30 12:04:12 +0200891 uint8_t total_count;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200892 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
893 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
894 int tq_asym_penalty, inv_asym_penalty, ret = 0;
895 unsigned int combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +0200896
897 /* find corresponding one hop neighbor */
898 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800899 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200900 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200901 if (!batadv_compare_eth(tmp_neigh_node->addr,
902 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +0200903 continue;
904
905 if (tmp_neigh_node->if_incoming != if_incoming)
906 continue;
907
908 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
909 continue;
910
911 neigh_node = tmp_neigh_node;
912 break;
913 }
914 rcu_read_unlock();
915
916 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200917 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
918 orig_neigh_node->orig,
919 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100920 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200921
922 if (!neigh_node)
923 goto out;
924
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800925 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +0200926 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800927 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200928
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800929 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200930
931 /* find packet count of corresponding one hop neighbor */
932 spin_lock_bh(&orig_node->ogm_cnt_lock);
933 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
934 neigh_rq_count = neigh_node->real_packet_count;
935 spin_unlock_bh(&orig_node->ogm_cnt_lock);
936
937 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200938 if (orig_eq_count > neigh_rq_count)
939 total_count = neigh_rq_count;
940 else
941 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +0200942
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200943 /* if we have too few packets (too less data) we set tq_own to zero
944 * if we receive too few packets it is not considered bidirectional
945 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200946 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
947 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200948 tq_own = 0;
949 else
950 /* neigh_node->real_packet_count is never zero as we
951 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200952 * information
953 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200954 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +0200955
Sven Eckelmann21a12362012-03-07 09:07:46 +0100956 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200957 * affect the nearly-symmetric links only a little, but
958 * punishes asymmetric links more. This will give a value
959 * between 0 and TQ_MAX_VALUE
960 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200961 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
962 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
963 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
964 BATADV_TQ_LOCAL_WINDOW_SIZE *
965 BATADV_TQ_LOCAL_WINDOW_SIZE;
966 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
967 inv_asym_penalty /= neigh_rq_max_cube;
968 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +0200969
Sven Eckelmann96412692012-06-05 22:31:30 +0200970 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200971 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +0200972 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +0200973
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200974 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200975 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
976 orig_node->orig, orig_neigh_node->orig, total_count,
977 neigh_rq_count, tq_own,
Sven Eckelmann96412692012-06-05 22:31:30 +0200978 tq_asym_penalty, batadv_ogm_packet->tq);
Marek Lindnerfc957272011-07-30 12:04:12 +0200979
980 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200981 * consider it bidirectional
982 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200983 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +0200984 ret = 1;
985
986out:
987 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200988 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200989 return ret;
990}
991
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200992/**
993 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
994 * adjust the sequence number and find out whether it is a duplicate
995 * @ethhdr: ethernet header of the packet
996 * @batadv_ogm_packet: OGM packet to be considered
997 * @if_incoming: interface on which the OGM packet was received
998 *
999 * Returns duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001000 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001001static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001002batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001003 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001004 const struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001005{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001006 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1007 struct batadv_orig_node *orig_node;
1008 struct batadv_neigh_node *tmp_neigh_node;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001009 int is_dup;
Marek Lindnerfc957272011-07-30 12:04:12 +02001010 int32_t seq_diff;
1011 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001012 int set_mark;
1013 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann96412692012-06-05 22:31:30 +02001014 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001015 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001016 uint8_t packet_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001017
Sven Eckelmann96412692012-06-05 22:31:30 +02001018 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001019 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001020 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001021
1022 spin_lock_bh(&orig_node->ogm_cnt_lock);
Al Viroe0f52112012-04-22 07:46:29 +01001023 seq_diff = seqno - orig_node->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001024
1025 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001026 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001027 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001028 &orig_node->batman_seqno_reset)) {
1029 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001030 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001031 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001032
1033 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001034 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001035 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001036 neigh_addr = tmp_neigh_node->addr;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001037 is_dup = batadv_test_bit(tmp_neigh_node->real_bits,
1038 orig_node->last_real_seqno,
1039 seqno);
1040
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001041 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001042 tmp_neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001043 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001044 if (is_dup)
1045 ret = BATADV_NEIGH_DUP;
1046 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001047 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001048 if (is_dup && (ret != BATADV_NEIGH_DUP))
1049 ret = BATADV_ORIG_DUP;
1050 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001051
1052 /* if the window moved, set the update flag. */
Sven Eckelmann0f5f9322012-05-12 02:09:25 +02001053 need_update |= batadv_bit_get_packet(bat_priv,
1054 tmp_neigh_node->real_bits,
1055 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001056
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001057 packet_count = bitmap_weight(tmp_neigh_node->real_bits,
1058 BATADV_TQ_LOCAL_WINDOW_SIZE);
1059 tmp_neigh_node->real_packet_count = packet_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001060 }
1061 rcu_read_unlock();
1062
1063 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001064 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001065 "updating last_seqno: old %u, new %u\n",
1066 orig_node->last_real_seqno, seqno);
Al Viroe0f52112012-04-22 07:46:29 +01001067 orig_node->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001068 }
1069
Marek Lindnerfc957272011-07-30 12:04:12 +02001070out:
1071 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001072 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001073 return ret;
1074}
1075
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001076static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001077 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001078 const unsigned char *tt_buff,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001079 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001080{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001081 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1082 struct batadv_hard_iface *hard_iface;
1083 struct batadv_orig_node *orig_neigh_node, *orig_node;
1084 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1085 struct batadv_neigh_node *orig_neigh_router = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001086 int has_directlink_flag;
1087 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
Antonio Quartulli281581d2013-03-25 22:27:00 +01001088 int is_bidirect;
Marek Lindner75cd33f2012-03-01 15:35:16 +08001089 bool is_single_hop_neigh = false;
Marek Lindner13b25412012-03-11 06:17:53 +08001090 bool is_from_best_next_hop = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001091 int sameseq, similar_ttl;
1092 enum batadv_dup_status dup_status;
Marek Lindnerfc957272011-07-30 12:04:12 +02001093 uint32_t if_incoming_seqno;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001094 uint8_t *prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001095
1096 /* Silently drop when the batman packet is actually not a
1097 * correct packet.
1098 *
1099 * This might happen if a packet is padded (e.g. Ethernet has a
1100 * minimum frame length of 64 byte) and the aggregation interprets
1101 * it as an additional length.
1102 *
1103 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001104 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001105 * packet in an aggregation. Here we expect that the padding
1106 * is always zero (or not 0x01)
1107 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001108 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001109 return;
1110
1111 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001112 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001113
Sven Eckelmann96412692012-06-05 22:31:30 +02001114 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001115 has_directlink_flag = 1;
1116 else
1117 has_directlink_flag = 0;
Marek Lindnerfc957272011-07-30 12:04:12 +02001118
Sven Eckelmann96412692012-06-05 22:31:30 +02001119 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
Marek Lindner75cd33f2012-03-01 15:35:16 +08001120 is_single_hop_neigh = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001121
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001122 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Antonio Quartullic0275e22012-12-11 20:29:22 +01001123 "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %#.4x, changes %u, tq %d, TTL %d, V %d, IDF %d)\n",
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001124 ethhdr->h_source, if_incoming->net_dev->name,
Sven Eckelmann96412692012-06-05 22:31:30 +02001125 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1126 batadv_ogm_packet->prev_sender,
1127 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn,
1128 ntohs(batadv_ogm_packet->tt_crc),
1129 batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
1130 batadv_ogm_packet->header.ttl,
1131 batadv_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001132
1133 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001134 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001135 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001136 continue;
1137
1138 if (hard_iface->soft_iface != if_incoming->soft_iface)
1139 continue;
1140
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001141 if (batadv_compare_eth(ethhdr->h_source,
1142 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001143 is_my_addr = 1;
1144
Sven Eckelmann96412692012-06-05 22:31:30 +02001145 if (batadv_compare_eth(batadv_ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001146 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001147 is_my_orig = 1;
1148
Sven Eckelmann96412692012-06-05 22:31:30 +02001149 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001150 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001151 is_my_oldorig = 1;
Marek Lindnerfc957272011-07-30 12:04:12 +02001152 }
1153 rcu_read_unlock();
1154
Marek Lindnerfc957272011-07-30 12:04:12 +02001155 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001156 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001157 "Drop packet: received my own broadcast (sender: %pM)\n",
1158 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001159 return;
1160 }
1161
Marek Lindnerfc957272011-07-30 12:04:12 +02001162 if (is_my_orig) {
1163 unsigned long *word;
1164 int offset;
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001165 int32_t bit_pos;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001166 int16_t if_num;
1167 uint8_t *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001168
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001169 orig_neigh_node = batadv_get_orig_node(bat_priv,
1170 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001171 if (!orig_neigh_node)
1172 return;
1173
1174 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001175 * come via the corresponding interface
1176 * save packet seqno for bidirectional check
1177 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001178 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001179 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001180 batadv_ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001181 if_num = if_incoming->if_num;
1182 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001183
1184 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1185 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001186 bit_pos = if_incoming_seqno - 2;
Sven Eckelmann96412692012-06-05 22:31:30 +02001187 bit_pos -= ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001188 batadv_set_bit(word, bit_pos);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001189 weight = &orig_neigh_node->bcast_own_sum[if_num];
1190 *weight = bitmap_weight(word,
1191 BATADV_TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001192 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1193 }
1194
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001195 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001196 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001197 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001198 return;
1199 }
1200
1201 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001202 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001203 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1204 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001205 return;
1206 }
1207
Sven Eckelmann96412692012-06-05 22:31:30 +02001208 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001209 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001210 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1211 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001212 return;
1213 }
1214
Sven Eckelmann96412692012-06-05 22:31:30 +02001215 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001216 if (!orig_node)
1217 return;
1218
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001219 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1220 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001221
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001222 if (dup_status == BATADV_PROTECTED) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001223 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001224 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1225 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001226 goto out;
1227 }
1228
Sven Eckelmann96412692012-06-05 22:31:30 +02001229 if (batadv_ogm_packet->tq == 0) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001230 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001231 "Drop packet: originator packet with tq equal 0\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001232 goto out;
1233 }
1234
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001235 router = batadv_orig_node_get_router(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001236 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001237 router_router = batadv_orig_node_get_router(router->orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001238
Marek Lindner13b25412012-03-11 06:17:53 +08001239 if ((router && router->tq_avg != 0) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001240 (batadv_compare_eth(router->addr, ethhdr->h_source)))
Marek Lindner13b25412012-03-11 06:17:53 +08001241 is_from_best_next_hop = true;
1242
Sven Eckelmann96412692012-06-05 22:31:30 +02001243 prev_sender = batadv_ogm_packet->prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001244 /* avoid temporary routing loops */
1245 if (router && router_router &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001246 (batadv_compare_eth(router->addr, prev_sender)) &&
Sven Eckelmann96412692012-06-05 22:31:30 +02001247 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001248 (batadv_compare_eth(router->addr, router_router->addr))) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001249 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001250 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1251 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001252 goto out;
1253 }
1254
1255 /* if sender is a direct neighbor the sender mac equals
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001256 * originator mac
1257 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001258 if (is_single_hop_neigh)
1259 orig_neigh_node = orig_node;
1260 else
1261 orig_neigh_node = batadv_get_orig_node(bat_priv,
1262 ethhdr->h_source);
1263
Marek Lindnerfc957272011-07-30 12:04:12 +02001264 if (!orig_neigh_node)
1265 goto out;
1266
Martin Hundebølld56b1702013-01-25 11:12:39 +01001267 /* Update nc_nodes of the originator */
1268 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1269 batadv_ogm_packet, is_single_hop_neigh);
1270
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001271 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001272
1273 /* drop packet if sender is not a direct neighbor and if we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001274 * don't route towards it
1275 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001276 if (!is_single_hop_neigh && (!orig_neigh_router)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001277 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001278 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001279 goto out_neigh;
1280 }
1281
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001282 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001283 batadv_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001284
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001285 batadv_bonding_save_primary(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001286 batadv_ogm_packet);
Marek Lindnerfc957272011-07-30 12:04:12 +02001287
1288 /* update ranking if it is not a duplicate or has the same
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001289 * seqno and similar ttl as the non-duplicate
1290 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001291 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001292 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1293 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1294 (sameseq && similar_ttl)))
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001295 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001296 batadv_ogm_packet, if_incoming,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001297 tt_buff, dup_status);
Marek Lindnerfc957272011-07-30 12:04:12 +02001298
1299 /* is single hop (direct) neighbor */
1300 if (is_single_hop_neigh) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001301 /* mark direct link on incoming interface */
Sven Eckelmann96412692012-06-05 22:31:30 +02001302 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001303 is_single_hop_neigh,
1304 is_from_best_next_hop, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001305
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001306 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001307 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001308 goto out_neigh;
1309 }
1310
1311 /* multihop originator */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001312 if (!is_bidirect) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001313 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001314 "Drop packet: not received via bidirectional link\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001315 goto out_neigh;
1316 }
1317
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001318 if (dup_status == BATADV_NEIGH_DUP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001319 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001320 "Drop packet: duplicate packet received\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001321 goto out_neigh;
1322 }
1323
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001324 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001325 "Forwarding packet: rebroadcast originator packet\n");
Sven Eckelmann96412692012-06-05 22:31:30 +02001326 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001327 is_single_hop_neigh, is_from_best_next_hop,
1328 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001329
1330out_neigh:
1331 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001332 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001333out:
1334 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001335 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001336 if (router_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001337 batadv_neigh_node_free_ref(router_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001338 if (orig_neigh_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001339 batadv_neigh_node_free_ref(orig_neigh_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001340
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001341 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001342}
1343
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001344static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001345 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001346{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001347 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001348 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001349 struct ethhdr *ethhdr;
1350 int buff_pos = 0, packet_len;
1351 unsigned char *tt_buff, *packet_buff;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001352 bool ret;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001353 uint8_t *packet_pos;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001354
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001355 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001356 if (!ret)
1357 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001358
Marek Lindneredbf12b2012-03-11 06:17:49 +08001359 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1360 * that does not have B.A.T.M.A.N. IV enabled ?
1361 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001362 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001363 return NET_RX_DROP;
1364
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001365 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1366 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001367 skb->len + ETH_HLEN);
1368
Marek Lindner8780dad2011-12-05 04:01:51 +08001369 packet_len = skb_headlen(skb);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001370 ethhdr = eth_hdr(skb);
Marek Lindner8780dad2011-12-05 04:01:51 +08001371 packet_buff = skb->data;
Sven Eckelmann96412692012-06-05 22:31:30 +02001372 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001373
1374 /* unpack the aggregated packets and process them one by one */
Marek Lindnerb47506d2013-03-04 10:39:49 +08001375 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1376 batadv_ogm_packet->tt_num_changes)) {
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001377 tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001378
Sven Eckelmann96412692012-06-05 22:31:30 +02001379 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001380 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001381
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001382 buff_pos += BATADV_OGM_HLEN;
Sven Eckelmann96412692012-06-05 22:31:30 +02001383 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +02001384
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001385 packet_pos = packet_buff + buff_pos;
1386 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001387 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001388
1389 kfree_skb(skb);
1390 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001391}
Marek Lindner1c280472011-11-28 17:40:17 +08001392
Sven Eckelmann56303d32012-06-05 22:31:31 +02001393static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001394 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001395 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1396 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1397 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1398 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1399 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1400 .bat_ogm_emit = batadv_iv_ogm_emit,
Marek Lindner1c280472011-11-28 17:40:17 +08001401};
1402
Sven Eckelmann81c524f2012-05-12 02:09:22 +02001403int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08001404{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001405 int ret;
1406
1407 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001408 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1409 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001410 if (ret < 0)
1411 goto out;
1412
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001413 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001414 if (ret < 0)
1415 goto handler_unregister;
1416
1417 goto out;
1418
1419handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001420 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001421out:
1422 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001423}