blob: 0a8a80cd4bf19819f797169a9b5c6122f3dc47b3 [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 Quartulli791c2a22013-08-17 12:44:44 +020031
32/**
33 * batadv_dup_status - duplicate status
34 * @BATADV_NO_DUP: the packet is a duplicate
35 * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
36 * neighbor)
37 * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
38 * @BATADV_PROTECTED: originator is currently protected (after reboot)
39 */
40enum batadv_dup_status {
41 BATADV_NO_DUP = 0,
42 BATADV_ORIG_DUP,
43 BATADV_NEIGH_DUP,
44 BATADV_PROTECTED,
45};
46
Antonio Quartulli24a5dee2013-04-08 09:38:12 +020047/**
48 * batadv_ring_buffer_set - update the ring buffer with the given value
49 * @lq_recv: pointer to the ring buffer
50 * @lq_index: index to store the value at
51 * @value: value to store in the ring buffer
52 */
53static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
54 uint8_t value)
55{
56 lq_recv[*lq_index] = value;
57 *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
58}
59
60/**
61 * batadv_ring_buffer_set - compute the average of all non-zero values stored
62 * in the given ring buffer
63 * @lq_recv: pointer to the ring buffer
64 *
65 * Returns computed average value.
66 */
67static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
68{
69 const uint8_t *ptr;
70 uint16_t count = 0, i = 0, sum = 0;
71
72 ptr = lq_recv;
73
74 while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
75 if (*ptr != 0) {
76 count++;
77 sum += *ptr;
78 }
79
80 i++;
81 ptr++;
82 }
83
84 if (count == 0)
85 return 0;
86
87 return (uint8_t)(sum / count);
88}
David S. Millerd98cae64e2013-06-19 16:49:39 -070089
Sven Eckelmann56303d32012-06-05 22:31:31 +020090static struct batadv_neigh_node *
91batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
92 const uint8_t *neigh_addr,
93 struct batadv_orig_node *orig_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +010094 struct batadv_orig_node *orig_neigh)
Marek Lindner7ae8b282012-03-01 15:35:21 +080095{
Sven Eckelmann56303d32012-06-05 22:31:31 +020096 struct batadv_neigh_node *neigh_node;
Marek Lindner7ae8b282012-03-01 15:35:21 +080097
Antonio Quartulli863dd7a2013-03-25 13:49:46 +010098 neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr);
Marek Lindner7ae8b282012-03-01 15:35:21 +080099 if (!neigh_node)
100 goto out;
101
102 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner7ae8b282012-03-01 15:35:21 +0800103
104 neigh_node->orig_node = orig_neigh;
105 neigh_node->if_incoming = hard_iface;
106
107 spin_lock_bh(&orig_node->neigh_list_lock);
108 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
109 spin_unlock_bh(&orig_node->neigh_list_lock);
110
111out:
112 return neigh_node;
113}
114
Sven Eckelmann56303d32012-06-05 22:31:31 +0200115static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200116{
Sven Eckelmann96412692012-06-05 22:31:30 +0200117 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200118 unsigned char *ogm_buff;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800119 uint32_t random_seqno;
Sven Eckelmann5346c352012-05-05 13:27:28 +0200120 int res = -ENOMEM;
Marek Lindnerd7d32ec2012-02-07 17:20:46 +0800121
122 /* randomize initial seqno to avoid collision */
123 get_random_bytes(&random_seqno, sizeof(random_seqno));
Marek Lindner14511512012-08-02 17:20:26 +0200124 atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200125
Marek Lindner14511512012-08-02 17:20:26 +0200126 hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
127 ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
128 if (!ogm_buff)
Marek Lindner77af7572012-02-07 17:20:48 +0800129 goto out;
130
Marek Lindner14511512012-08-02 17:20:26 +0200131 hard_iface->bat_iv.ogm_buff = ogm_buff;
132
133 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200134 batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
135 batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
136 batadv_ogm_packet->header.ttl = 2;
137 batadv_ogm_packet->flags = BATADV_NO_FLAGS;
138 batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
139 batadv_ogm_packet->tt_num_changes = 0;
140 batadv_ogm_packet->ttvn = 0;
Marek Lindner77af7572012-02-07 17:20:48 +0800141
142 res = 0;
143
144out:
145 return res;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200146}
147
Sven Eckelmann56303d32012-06-05 22:31:31 +0200148static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
Marek Lindner00a50072012-02-07 17:20:47 +0800149{
Marek Lindner14511512012-08-02 17:20:26 +0200150 kfree(hard_iface->bat_iv.ogm_buff);
151 hard_iface->bat_iv.ogm_buff = NULL;
Marek Lindner00a50072012-02-07 17:20:47 +0800152}
153
Sven Eckelmann56303d32012-06-05 22:31:31 +0200154static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200155{
Sven Eckelmann96412692012-06-05 22:31:30 +0200156 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200157 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200158
Marek Lindner14511512012-08-02 17:20:26 +0200159 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200160 memcpy(batadv_ogm_packet->orig,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200161 hard_iface->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200162 memcpy(batadv_ogm_packet->prev_sender,
Marek Lindnerd0b9fd82011-07-30 12:33:33 +0200163 hard_iface->net_dev->dev_addr, ETH_ALEN);
164}
165
Sven Eckelmann56303d32012-06-05 22:31:31 +0200166static void
167batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
Marek Lindnerc3229392012-03-11 06:17:50 +0800168{
Sven Eckelmann96412692012-06-05 22:31:30 +0200169 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner14511512012-08-02 17:20:26 +0200170 unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
Marek Lindnerc3229392012-03-11 06:17:50 +0800171
Marek Lindner14511512012-08-02 17:20:26 +0200172 batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200173 batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
174 batadv_ogm_packet->header.ttl = BATADV_TTL;
Marek Lindnerc3229392012-03-11 06:17:50 +0800175}
176
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200177/* when do we schedule our own ogm to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200178static unsigned long
Sven Eckelmann56303d32012-06-05 22:31:31 +0200179batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200180{
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200181 unsigned int msecs;
182
183 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
Akinobu Mitae76e4322012-12-24 11:14:07 +0900184 msecs += prandom_u32() % (2 * BATADV_JITTER);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200185
186 return jiffies + msecs_to_jiffies(msecs);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200187}
188
189/* when do we schedule a ogm packet to be sent */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200190static unsigned long batadv_iv_ogm_fwd_send_time(void)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200191{
Akinobu Mitae76e4322012-12-24 11:14:07 +0900192 return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200193}
194
195/* apply hop penalty for a normal link */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200196static uint8_t batadv_hop_penalty(uint8_t tq,
197 const struct batadv_priv *bat_priv)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200198{
199 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200200 int new_tq;
201
202 new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
203 new_tq /= BATADV_TQ_MAX_VALUE;
204
205 return new_tq;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200206}
207
Marek Lindnerfc957272011-07-30 12:04:12 +0200208/* is there another aggregated packet here? */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200209static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
210 int tt_num_changes)
Marek Lindnerfc957272011-07-30 12:04:12 +0200211{
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200212 int next_buff_pos = 0;
213
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200214 next_buff_pos += buff_pos + BATADV_OGM_HLEN;
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200215 next_buff_pos += batadv_tt_len(tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +0200216
217 return (next_buff_pos <= packet_len) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200218 (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
Marek Lindnerfc957272011-07-30 12:04:12 +0200219}
220
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200221/* send a batman ogm to a given interface */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200222static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
223 struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200224{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200225 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200226 char *fwd_str;
227 uint8_t packet_num;
228 int16_t buff_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200229 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200230 struct sk_buff *skb;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200231 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200232
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200233 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200234 return;
235
236 packet_num = 0;
237 buff_pos = 0;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200238 packet_pos = forw_packet->skb->data;
239 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200240
241 /* adjust all flags and log packets */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200242 while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
Sven Eckelmann96412692012-06-05 22:31:30 +0200243 batadv_ogm_packet->tt_num_changes)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200244 /* we might have aggregated direct link packets with an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200245 * ordinary base packet
246 */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200247 if (forw_packet->direct_link_flags & BIT(packet_num) &&
248 forw_packet->if_incoming == hard_iface)
Sven Eckelmann96412692012-06-05 22:31:30 +0200249 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200250 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200251 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200252
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200253 if (packet_num > 0 || !forw_packet->own)
254 fwd_str = "Forwarding";
255 else
256 fwd_str = "Sending own";
257
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200258 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200259 "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
260 fwd_str, (packet_num > 0 ? "aggregated " : ""),
Sven Eckelmann96412692012-06-05 22:31:30 +0200261 batadv_ogm_packet->orig,
262 ntohl(batadv_ogm_packet->seqno),
263 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
264 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200265 "on" : "off"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200266 batadv_ogm_packet->ttvn, hard_iface->net_dev->name,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200267 hard_iface->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200268
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200269 buff_pos += BATADV_OGM_HLEN;
Sven Eckelmann96412692012-06-05 22:31:30 +0200270 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200271 packet_num++;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200272 packet_pos = forw_packet->skb->data + buff_pos;
273 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200274 }
275
276 /* create clone because function is called more than once */
277 skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200278 if (skb) {
Sven Eckelmannd69909d2012-06-03 22:19:20 +0200279 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
280 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +0200281 skb->len + ETH_HLEN);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200282 batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
Martin Hundebøllf8214862012-04-20 17:02:45 +0200283 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200284}
285
286/* send a batman ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200287static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200288{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200289 struct batadv_hard_iface *hard_iface;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200290 struct net_device *soft_iface;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291 struct batadv_priv *bat_priv;
292 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200293 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200294 unsigned char directlink;
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200295 uint8_t *packet_pos;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200296
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200297 packet_pos = forw_packet->skb->data;
298 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Sven Eckelmann96412692012-06-05 22:31:30 +0200299 directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200300
301 if (!forw_packet->if_incoming) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100302 pr_err("Error - can't forward packet: incoming iface not specified\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200303 goto out;
304 }
305
306 soft_iface = forw_packet->if_incoming->soft_iface;
307 bat_priv = netdev_priv(soft_iface);
308
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200309 if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200310 goto out;
311
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200312 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200313 if (!primary_if)
314 goto out;
315
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200316 /* multihomed peer assumed
317 * non-primary OGMs are only broadcasted on their interface
318 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200319 if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200320 (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200321 /* FIXME: what about aggregated packets ? */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200322 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200323 "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
324 (forw_packet->own ? "Sending own" : "Forwarding"),
Sven Eckelmann96412692012-06-05 22:31:30 +0200325 batadv_ogm_packet->orig,
326 ntohl(batadv_ogm_packet->seqno),
327 batadv_ogm_packet->header.ttl,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200328 forw_packet->if_incoming->net_dev->name,
329 forw_packet->if_incoming->net_dev->dev_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200330
331 /* skb is only used once and than forw_packet is free'd */
Sven Eckelmann9455e342012-05-12 02:09:37 +0200332 batadv_send_skb_packet(forw_packet->skb,
333 forw_packet->if_incoming,
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200334 batadv_broadcast_addr);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200335 forw_packet->skb = NULL;
336
337 goto out;
338 }
339
340 /* broadcast on every interface */
341 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200342 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200343 if (hard_iface->soft_iface != soft_iface)
344 continue;
345
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200346 batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200347 }
348 rcu_read_unlock();
349
350out:
351 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200352 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200353}
354
355/* return true if new_packet can be aggregated with forw_packet */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200356static bool
Sven Eckelmann96412692012-06-05 22:31:30 +0200357batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200358 struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200359 int packet_len, unsigned long send_time,
360 bool directlink,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200361 const struct batadv_hard_iface *if_incoming,
362 const struct batadv_forw_packet *forw_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200363{
Sven Eckelmann96412692012-06-05 22:31:30 +0200364 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200365 int aggregated_bytes = forw_packet->packet_len + packet_len;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200366 struct batadv_hard_iface *primary_if = NULL;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200367 bool res = false;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200368 unsigned long aggregation_end_time;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200369
Sven Eckelmann96412692012-06-05 22:31:30 +0200370 batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200371 aggregation_end_time = send_time;
372 aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200373
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200374 /* we can aggregate the current packet to this aggregated packet
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200375 * if:
376 *
377 * - the send time is within our MAX_AGGREGATION_MS time
378 * - the resulting packet wont be bigger than
379 * MAX_AGGREGATION_BYTES
380 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200381 if (time_before(send_time, forw_packet->send_time) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200382 time_after_eq(aggregation_end_time, forw_packet->send_time) &&
383 (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200384 /* check aggregation compatibility
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200385 * -> direct link packets are broadcasted on
386 * their interface only
387 * -> aggregate packet if the current packet is
388 * a "global" packet as well as the base
389 * packet
390 */
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200391 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200392 if (!primary_if)
393 goto out;
394
395 /* packets without direct link flag and high TTL
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200396 * are flooded through the net
397 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200398 if ((!directlink) &&
Sven Eckelmann96412692012-06-05 22:31:30 +0200399 (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
400 (batadv_ogm_packet->header.ttl != 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200401
402 /* own packets originating non-primary
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200403 * interfaces leave only that interface
404 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200405 ((!forw_packet->own) ||
406 (forw_packet->if_incoming == primary_if))) {
407 res = true;
408 goto out;
409 }
410
411 /* if the incoming packet is sent via this one
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200412 * interface only - we still can aggregate
413 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200414 if ((directlink) &&
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200415 (new_bat_ogm_packet->header.ttl == 1) &&
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200416 (forw_packet->if_incoming == if_incoming) &&
417
418 /* packets from direct neighbors or
419 * own secondary interface packets
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200420 * (= secondary interface packets in general)
421 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200422 (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200423 (forw_packet->own &&
424 forw_packet->if_incoming != primary_if))) {
425 res = true;
426 goto out;
427 }
428 }
429
430out:
431 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200432 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200433 return res;
434}
435
436/* create a new aggregated packet and add this packet to it */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200437static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
438 int packet_len, unsigned long send_time,
439 bool direct_link,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200440 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200441 int own_packet)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200442{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200443 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
444 struct batadv_forw_packet *forw_packet_aggr;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200445 unsigned char *skb_buff;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200446 unsigned int skb_size;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200447
448 if (!atomic_inc_not_zero(&if_incoming->refcount))
449 return;
450
451 /* own packet should always be scheduled */
452 if (!own_packet) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200453 if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200454 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200455 "batman packet queue full\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200456 goto out;
457 }
458 }
459
460 forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
461 if (!forw_packet_aggr) {
462 if (!own_packet)
463 atomic_inc(&bat_priv->batman_queue_left);
464 goto out;
465 }
466
467 if ((atomic_read(&bat_priv->aggregated_ogms)) &&
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200468 (packet_len < BATADV_MAX_AGGREGATION_BYTES))
Sven Eckelmann5b246572012-11-04 17:11:45 +0100469 skb_size = BATADV_MAX_AGGREGATION_BYTES;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200470 else
Sven Eckelmann5b246572012-11-04 17:11:45 +0100471 skb_size = packet_len;
472
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200473 skb_size += ETH_HLEN;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200474
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200475 forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200476 if (!forw_packet_aggr->skb) {
477 if (!own_packet)
478 atomic_inc(&bat_priv->batman_queue_left);
479 kfree(forw_packet_aggr);
480 goto out;
481 }
Simon Wunderlichc54f38c92013-07-29 17:56:44 +0200482 forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200483 skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200484
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200485 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
486 forw_packet_aggr->packet_len = packet_len;
487 memcpy(skb_buff, packet_buff, packet_len);
488
489 forw_packet_aggr->own = own_packet;
490 forw_packet_aggr->if_incoming = if_incoming;
491 forw_packet_aggr->num_packets = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200492 forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200493 forw_packet_aggr->send_time = send_time;
494
495 /* save packet direct link flag status */
496 if (direct_link)
497 forw_packet_aggr->direct_link_flags |= 1;
498
499 /* add new packet to packet list */
500 spin_lock_bh(&bat_priv->forw_bat_list_lock);
501 hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
502 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
503
504 /* start timer for this packet */
505 INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
Sven Eckelmann9455e342012-05-12 02:09:37 +0200506 batadv_send_outstanding_bat_ogm_packet);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200507 queue_delayed_work(batadv_event_workqueue,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200508 &forw_packet_aggr->delayed_work,
509 send_time - jiffies);
510
511 return;
512out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200513 batadv_hardif_free_ref(if_incoming);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200514}
515
516/* aggregate a new packet into the existing ogm packet */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200517static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200518 const unsigned char *packet_buff,
519 int packet_len, bool direct_link)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200520{
521 unsigned char *skb_buff;
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200522 unsigned long new_direct_link_flag;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200523
524 skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
525 memcpy(skb_buff, packet_buff, packet_len);
526 forw_packet_aggr->packet_len += packet_len;
527 forw_packet_aggr->num_packets++;
528
529 /* save packet direct link flag status */
Sven Eckelmann8de47de2012-07-08 16:32:09 +0200530 if (direct_link) {
531 new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
532 forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
533 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200534}
535
Sven Eckelmann56303d32012-06-05 22:31:31 +0200536static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200537 unsigned char *packet_buff,
538 int packet_len,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200539 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200540 int own_packet, unsigned long send_time)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200541{
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200542 /* _aggr -> pointer to the packet we want to aggregate with
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200543 * _pos -> pointer to the position in the queue
544 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200545 struct batadv_forw_packet *forw_packet_aggr = NULL;
546 struct batadv_forw_packet *forw_packet_pos = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200547 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200548 bool direct_link;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200549 unsigned long max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200550
Sven Eckelmann96412692012-06-05 22:31:30 +0200551 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
552 direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200553 max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200554
555 /* find position for the packet in the forward queue */
556 spin_lock_bh(&bat_priv->forw_bat_list_lock);
557 /* own packets are not to be aggregated */
558 if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800559 hlist_for_each_entry(forw_packet_pos,
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200560 &bat_priv->forw_bat_list, list) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200561 if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200562 bat_priv, packet_len,
563 send_time, direct_link,
564 if_incoming,
565 forw_packet_pos)) {
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200566 forw_packet_aggr = forw_packet_pos;
567 break;
568 }
569 }
570 }
571
572 /* nothing to aggregate with - either aggregation disabled or no
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200573 * suitable aggregation packet found
574 */
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200575 if (!forw_packet_aggr) {
576 /* the following section can run without the lock */
577 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
578
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200579 /* if we could not aggregate this packet with one of the others
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200580 * we hold it back for a while, so that it might be aggregated
581 * later on
582 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200583 if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
584 send_time += max_aggregation_jiffies;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200585
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200586 batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
587 send_time, direct_link,
588 if_incoming, own_packet);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200589 } else {
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200590 batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
591 packet_len, direct_link);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200592 spin_unlock_bh(&bat_priv->forw_bat_list_lock);
593 }
594}
595
Sven Eckelmann56303d32012-06-05 22:31:31 +0200596static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200597 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200598 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200599 bool is_single_hop_neigh,
600 bool is_from_best_next_hop,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200601 struct batadv_hard_iface *if_incoming)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200602{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200603 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200604 uint8_t tt_num_changes;
605
Sven Eckelmann96412692012-06-05 22:31:30 +0200606 if (batadv_ogm_packet->header.ttl <= 1) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200607 batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200608 return;
609 }
610
Marek Lindner13b25412012-03-11 06:17:53 +0800611 if (!is_from_best_next_hop) {
612 /* Mark the forwarded packet when it is not coming from our
613 * best next hop. We still need to forward the packet for our
614 * neighbor link quality detection to work in case the packet
615 * originated from a single hop neighbor. Otherwise we can
616 * simply drop the ogm.
617 */
618 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200619 batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
Marek Lindner13b25412012-03-11 06:17:53 +0800620 else
621 return;
622 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200623
Sven Eckelmann96412692012-06-05 22:31:30 +0200624 tt_num_changes = batadv_ogm_packet->tt_num_changes;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200625
Sven Eckelmann96412692012-06-05 22:31:30 +0200626 batadv_ogm_packet->header.ttl--;
627 memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200628
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200629 /* apply hop penalty */
Sven Eckelmann96412692012-06-05 22:31:30 +0200630 batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200631 bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200632
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200633 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200634 "Forwarding packet: tq: %i, ttl: %i\n",
Sven Eckelmann96412692012-06-05 22:31:30 +0200635 batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200636
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200637 /* switch of primaries first hop flag when forwarding */
Sven Eckelmann96412692012-06-05 22:31:30 +0200638 batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
Marek Lindner75cd33f2012-03-01 15:35:16 +0800639 if (is_single_hop_neigh)
Sven Eckelmann96412692012-06-05 22:31:30 +0200640 batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200641 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200642 batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200643
Sven Eckelmann96412692012-06-05 22:31:30 +0200644 batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200645 BATADV_OGM_HLEN + batadv_tt_len(tt_num_changes),
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200646 if_incoming, 0, batadv_iv_ogm_fwd_send_time());
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200647}
648
Antonio Quartullid9896612013-04-17 17:44:43 +0200649/**
650 * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
651 * the given interface
652 * @hard_iface: the interface for which the windows have to be shifted
653 */
654static void
655batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
656{
657 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
658 struct batadv_hashtable *hash = bat_priv->orig_hash;
659 struct hlist_head *head;
660 struct batadv_orig_node *orig_node;
661 unsigned long *word;
662 uint32_t i;
663 size_t word_index;
664 uint8_t *w;
665
666 for (i = 0; i < hash->size; i++) {
667 head = &hash->table[i];
668
669 rcu_read_lock();
670 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
671 spin_lock_bh(&orig_node->ogm_cnt_lock);
672 word_index = hard_iface->if_num * BATADV_NUM_WORDS;
673 word = &(orig_node->bcast_own[word_index]);
674
675 batadv_bit_get_packet(bat_priv, word, 1, 0);
676 w = &orig_node->bcast_own_sum[hard_iface->if_num];
677 *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
678 spin_unlock_bh(&orig_node->ogm_cnt_lock);
679 }
680 rcu_read_unlock();
681 }
682}
683
Sven Eckelmann56303d32012-06-05 22:31:31 +0200684static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200685{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200686 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200687 unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
Sven Eckelmann96412692012-06-05 22:31:30 +0200688 struct batadv_ogm_packet *batadv_ogm_packet;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200689 struct batadv_hard_iface *primary_if;
Marek Lindner14511512012-08-02 17:20:26 +0200690 int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800691 int vis_server, tt_num_changes = 0;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200692 uint32_t seqno;
693 uint8_t bandwidth;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200694
695 vis_server = atomic_read(&bat_priv->vis_mode);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200696 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200697
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800698 if (hard_iface == primary_if)
Marek Lindner14511512012-08-02 17:20:26 +0200699 tt_num_changes = batadv_tt_append_diff(bat_priv, ogm_buff,
700 ogm_buff_len,
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200701 BATADV_OGM_HLEN);
Marek Lindnerbe9aa4c2012-05-07 04:22:05 +0800702
Marek Lindner14511512012-08-02 17:20:26 +0200703 batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200704
705 /* change sequence number to network order */
Marek Lindner14511512012-08-02 17:20:26 +0200706 seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200707 batadv_ogm_packet->seqno = htonl(seqno);
Marek Lindner14511512012-08-02 17:20:26 +0200708 atomic_inc(&hard_iface->bat_iv.ogm_seqno);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200709
Sven Eckelmann807736f2012-07-15 22:26:51 +0200710 batadv_ogm_packet->ttvn = atomic_read(&bat_priv->tt.vn);
711 batadv_ogm_packet->tt_crc = htons(bat_priv->tt.local_crc);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200712 if (tt_num_changes >= 0)
Sven Eckelmann96412692012-06-05 22:31:30 +0200713 batadv_ogm_packet->tt_num_changes = tt_num_changes;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200714
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200715 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC)
Sven Eckelmann96412692012-06-05 22:31:30 +0200716 batadv_ogm_packet->flags |= BATADV_VIS_SERVER;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200717 else
Sven Eckelmann96412692012-06-05 22:31:30 +0200718 batadv_ogm_packet->flags &= ~BATADV_VIS_SERVER;
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200719
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200720 if (hard_iface == primary_if &&
721 atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_SERVER) {
722 bandwidth = (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
723 batadv_ogm_packet->gw_flags = bandwidth;
724 } else {
Sven Eckelmann96412692012-06-05 22:31:30 +0200725 batadv_ogm_packet->gw_flags = BATADV_NO_FLAGS;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200726 }
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200727
Antonio Quartullid9896612013-04-17 17:44:43 +0200728 batadv_iv_ogm_slide_own_bcast_window(hard_iface);
Marek Lindner14511512012-08-02 17:20:26 +0200729 batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
730 hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200731 batadv_iv_ogm_emit_send_time(bat_priv));
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200732
733 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200734 batadv_hardif_free_ref(primary_if);
Marek Lindnerb9dacc52011-08-03 09:09:30 +0200735}
736
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200737static void
Sven Eckelmann56303d32012-06-05 22:31:31 +0200738batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
739 struct batadv_orig_node *orig_node,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200740 const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +0200741 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200742 struct batadv_hard_iface *if_incoming,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200743 const unsigned char *tt_buff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200744 enum batadv_dup_status dup_status)
Marek Lindnerfc957272011-07-30 12:04:12 +0200745{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200746 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
747 struct batadv_neigh_node *router = NULL;
748 struct batadv_orig_node *orig_node_tmp;
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200749 int if_num;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200750 uint8_t sum_orig, sum_neigh;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200751 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200752 uint8_t tq_avg;
Marek Lindnerfc957272011-07-30 12:04:12 +0200753
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200754 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200755 "update_originator(): Searching and updating originator entry of received packet\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200756
757 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200759 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200760 neigh_addr = tmp_neigh_node->addr;
761 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
762 tmp_neigh_node->if_incoming == if_incoming &&
763 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
Antonio Quartulli38dc40e2013-03-30 17:22:00 +0100764 if (WARN(neigh_node, "too many matching neigh_nodes"))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200765 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200766 neigh_node = tmp_neigh_node;
767 continue;
768 }
769
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200770 if (dup_status != BATADV_NO_DUP)
Marek Lindnerfc957272011-07-30 12:04:12 +0200771 continue;
772
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800773 spin_lock_bh(&tmp_neigh_node->lq_update_lock);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200774 batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
775 &tmp_neigh_node->tq_index, 0);
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200776 tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
777 tmp_neigh_node->tq_avg = tq_avg;
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800778 spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200779 }
780
781 if (!neigh_node) {
Sven Eckelmann56303d32012-06-05 22:31:31 +0200782 struct batadv_orig_node *orig_tmp;
Marek Lindnerfc957272011-07-30 12:04:12 +0200783
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200784 orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +0200785 if (!orig_tmp)
786 goto unlock;
787
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200788 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
789 ethhdr->h_source,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100790 orig_node, orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200791
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200792 batadv_orig_node_free_ref(orig_tmp);
Marek Lindnerfc957272011-07-30 12:04:12 +0200793 if (!neigh_node)
794 goto unlock;
795 } else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200796 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200797 "Updating existing last-hop neighbor of originator\n");
Marek Lindnerfc957272011-07-30 12:04:12 +0200798
799 rcu_read_unlock();
800
Sven Eckelmann96412692012-06-05 22:31:30 +0200801 orig_node->flags = batadv_ogm_packet->flags;
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800802 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200803
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800804 spin_lock_bh(&neigh_node->lq_update_lock);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200805 batadv_ring_buffer_set(neigh_node->tq_recv,
806 &neigh_node->tq_index,
Sven Eckelmann96412692012-06-05 22:31:30 +0200807 batadv_ogm_packet->tq);
Sven Eckelmann925a6672012-05-12 02:09:35 +0200808 neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
Marek Lindnere3b0d0d2012-03-17 15:28:32 +0800809 spin_unlock_bh(&neigh_node->lq_update_lock);
Marek Lindnerfc957272011-07-30 12:04:12 +0200810
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200811 if (dup_status == BATADV_NO_DUP) {
Sven Eckelmann96412692012-06-05 22:31:30 +0200812 orig_node->last_ttl = batadv_ogm_packet->header.ttl;
813 neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
Marek Lindnerfc957272011-07-30 12:04:12 +0200814 }
815
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200816 batadv_bonding_candidate_add(orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200817
818 /* if this neighbor already is our next hop there is nothing
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200819 * to change
820 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200821 router = batadv_orig_node_get_router(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200822 if (router == neigh_node)
823 goto update_tt;
824
825 /* if this neighbor does not offer a better TQ we won't consider it */
826 if (router && (router->tq_avg > neigh_node->tq_avg))
827 goto update_tt;
828
829 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200830 * won't consider it either
831 */
Marek Lindnerfc957272011-07-30 12:04:12 +0200832 if (router && (neigh_node->tq_avg == router->tq_avg)) {
833 orig_node_tmp = router->orig_node;
834 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200835 if_num = router->if_incoming->if_num;
836 sum_orig = orig_node_tmp->bcast_own_sum[if_num];
Marek Lindnerfc957272011-07-30 12:04:12 +0200837 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
838
839 orig_node_tmp = neigh_node->orig_node;
840 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
Linus LĂĽssing7caf69f2012-09-18 03:01:08 +0200841 if_num = neigh_node->if_incoming->if_num;
842 sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
Marek Lindnerfc957272011-07-30 12:04:12 +0200843 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
844
Sven Eckelmannbbb1f902012-07-08 17:13:15 +0200845 if (sum_orig >= sum_neigh)
Marek Lindnerfc957272011-07-30 12:04:12 +0200846 goto update_tt;
847 }
848
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200849 batadv_update_route(bat_priv, orig_node, neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200850
851update_tt:
852 /* I have to check for transtable changes only if the OGM has been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200853 * sent through a primary interface
854 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200855 if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
856 (batadv_ogm_packet->header.ttl > 2)) ||
857 (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200858 batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
Sven Eckelmann96412692012-06-05 22:31:30 +0200859 batadv_ogm_packet->tt_num_changes,
860 batadv_ogm_packet->ttvn,
861 ntohs(batadv_ogm_packet->tt_crc));
Marek Lindnerfc957272011-07-30 12:04:12 +0200862
Sven Eckelmann96412692012-06-05 22:31:30 +0200863 if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200864 batadv_gw_node_update(bat_priv, orig_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200865 batadv_ogm_packet->gw_flags);
Marek Lindnerfc957272011-07-30 12:04:12 +0200866
Sven Eckelmann96412692012-06-05 22:31:30 +0200867 orig_node->gw_flags = batadv_ogm_packet->gw_flags;
Marek Lindnerfc957272011-07-30 12:04:12 +0200868
869 /* restart gateway selection if fast or late switching was enabled */
870 if ((orig_node->gw_flags) &&
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200871 (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
Marek Lindnerfc957272011-07-30 12:04:12 +0200872 (atomic_read(&bat_priv->gw_sel_class) > 2))
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200873 batadv_gw_check_election(bat_priv, orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200874
875 goto out;
876
877unlock:
878 rcu_read_unlock();
879out:
880 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200881 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200882 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200883 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +0200884}
885
Sven Eckelmann56303d32012-06-05 22:31:31 +0200886static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
887 struct batadv_orig_node *orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +0200888 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200889 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +0200890{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200891 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
892 struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
Marek Lindnerfc957272011-07-30 12:04:12 +0200893 uint8_t total_count;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200894 uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
895 unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
896 int tq_asym_penalty, inv_asym_penalty, ret = 0;
897 unsigned int combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +0200898
899 /* find corresponding one hop neighbor */
900 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800901 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +0200902 &orig_neigh_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200903 if (!batadv_compare_eth(tmp_neigh_node->addr,
904 orig_neigh_node->orig))
Marek Lindnerfc957272011-07-30 12:04:12 +0200905 continue;
906
907 if (tmp_neigh_node->if_incoming != if_incoming)
908 continue;
909
910 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
911 continue;
912
913 neigh_node = tmp_neigh_node;
914 break;
915 }
916 rcu_read_unlock();
917
918 if (!neigh_node)
Sven Eckelmannfe8bc392012-05-12 18:33:51 +0200919 neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
920 orig_neigh_node->orig,
921 orig_neigh_node,
Antonio Quartulli863dd7a2013-03-25 13:49:46 +0100922 orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200923
924 if (!neigh_node)
925 goto out;
926
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800927 /* if orig_node is direct neighbor update neigh_node last_seen */
Marek Lindnerfc957272011-07-30 12:04:12 +0200928 if (orig_node == orig_neigh_node)
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800929 neigh_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200930
Marek Lindnerd7b2a972012-03-01 15:35:19 +0800931 orig_node->last_seen = jiffies;
Marek Lindnerfc957272011-07-30 12:04:12 +0200932
933 /* find packet count of corresponding one hop neighbor */
934 spin_lock_bh(&orig_node->ogm_cnt_lock);
935 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
936 neigh_rq_count = neigh_node->real_packet_count;
937 spin_unlock_bh(&orig_node->ogm_cnt_lock);
938
939 /* pay attention to not get a value bigger than 100 % */
Sven Eckelmannc67893d2012-07-08 18:33:51 +0200940 if (orig_eq_count > neigh_rq_count)
941 total_count = neigh_rq_count;
942 else
943 total_count = orig_eq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +0200944
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200945 /* if we have too few packets (too less data) we set tq_own to zero
946 * if we receive too few packets it is not considered bidirectional
947 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200948 if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
949 neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
Marek Lindnerfc957272011-07-30 12:04:12 +0200950 tq_own = 0;
951 else
952 /* neigh_node->real_packet_count is never zero as we
953 * only purge old information when getting new
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200954 * information
955 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200956 tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
Marek Lindnerfc957272011-07-30 12:04:12 +0200957
Sven Eckelmann21a12362012-03-07 09:07:46 +0100958 /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
Marek Lindnerfc957272011-07-30 12:04:12 +0200959 * affect the nearly-symmetric links only a little, but
960 * punishes asymmetric links more. This will give a value
961 * between 0 and TQ_MAX_VALUE
962 */
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200963 neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
964 neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
965 neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
966 BATADV_TQ_LOCAL_WINDOW_SIZE *
967 BATADV_TQ_LOCAL_WINDOW_SIZE;
968 inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
969 inv_asym_penalty /= neigh_rq_max_cube;
970 tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
Marek Lindnerfc957272011-07-30 12:04:12 +0200971
Sven Eckelmann96412692012-06-05 22:31:30 +0200972 combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200973 combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
Sven Eckelmann96412692012-06-05 22:31:30 +0200974 batadv_ogm_packet->tq = combined_tq;
Marek Lindnerfc957272011-07-30 12:04:12 +0200975
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200976 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200977 "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
978 orig_node->orig, orig_neigh_node->orig, total_count,
979 neigh_rq_count, tq_own,
Sven Eckelmann96412692012-06-05 22:31:30 +0200980 tq_asym_penalty, batadv_ogm_packet->tq);
Marek Lindnerfc957272011-07-30 12:04:12 +0200981
982 /* if link has the minimum required transmission quality
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200983 * consider it bidirectional
984 */
Sven Eckelmann96412692012-06-05 22:31:30 +0200985 if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnerfc957272011-07-30 12:04:12 +0200986 ret = 1;
987
988out:
989 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200990 batadv_neigh_node_free_ref(neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +0200991 return ret;
992}
993
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +0200994/**
995 * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
996 * adjust the sequence number and find out whether it is a duplicate
997 * @ethhdr: ethernet header of the packet
998 * @batadv_ogm_packet: OGM packet to be considered
999 * @if_incoming: interface on which the OGM packet was received
1000 *
1001 * Returns duplicate status as enum batadv_dup_status
Marek Lindnerfc957272011-07-30 12:04:12 +02001002 */
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001003static enum batadv_dup_status
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001004batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001005 const struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001006 const struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001007{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001008 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1009 struct batadv_orig_node *orig_node;
1010 struct batadv_neigh_node *tmp_neigh_node;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001011 int is_dup;
Marek Lindnerfc957272011-07-30 12:04:12 +02001012 int32_t seq_diff;
1013 int need_update = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001014 int set_mark;
1015 enum batadv_dup_status ret = BATADV_NO_DUP;
Sven Eckelmann96412692012-06-05 22:31:30 +02001016 uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001017 uint8_t *neigh_addr;
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001018 uint8_t packet_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001019
Sven Eckelmann96412692012-06-05 22:31:30 +02001020 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001021 if (!orig_node)
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001022 return BATADV_NO_DUP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001023
1024 spin_lock_bh(&orig_node->ogm_cnt_lock);
Al Viroe0f52112012-04-22 07:46:29 +01001025 seq_diff = seqno - orig_node->last_real_seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001026
1027 /* signalize caller that the packet is to be dropped. */
Antonio Quartulli1e5cc262012-02-26 15:39:42 +01001028 if (!hlist_empty(&orig_node->neigh_list) &&
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001029 batadv_window_protected(bat_priv, seq_diff,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001030 &orig_node->batman_seqno_reset)) {
1031 ret = BATADV_PROTECTED;
Marek Lindnerfc957272011-07-30 12:04:12 +02001032 goto out;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001033 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001034
1035 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001036 hlist_for_each_entry_rcu(tmp_neigh_node,
Marek Lindnerfc957272011-07-30 12:04:12 +02001037 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001038 neigh_addr = tmp_neigh_node->addr;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001039 is_dup = batadv_test_bit(tmp_neigh_node->real_bits,
1040 orig_node->last_real_seqno,
1041 seqno);
1042
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001043 if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001044 tmp_neigh_node->if_incoming == if_incoming) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001045 set_mark = 1;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001046 if (is_dup)
1047 ret = BATADV_NEIGH_DUP;
1048 } else {
Marek Lindnerfc957272011-07-30 12:04:12 +02001049 set_mark = 0;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001050 if (is_dup && (ret != BATADV_NEIGH_DUP))
1051 ret = BATADV_ORIG_DUP;
1052 }
Marek Lindnerfc957272011-07-30 12:04:12 +02001053
1054 /* if the window moved, set the update flag. */
Sven Eckelmann0f5f93222012-05-12 02:09:25 +02001055 need_update |= batadv_bit_get_packet(bat_priv,
1056 tmp_neigh_node->real_bits,
1057 seq_diff, set_mark);
Marek Lindnerfc957272011-07-30 12:04:12 +02001058
Sven Eckelmannbbb1f902012-07-08 17:13:15 +02001059 packet_count = bitmap_weight(tmp_neigh_node->real_bits,
1060 BATADV_TQ_LOCAL_WINDOW_SIZE);
1061 tmp_neigh_node->real_packet_count = packet_count;
Marek Lindnerfc957272011-07-30 12:04:12 +02001062 }
1063 rcu_read_unlock();
1064
1065 if (need_update) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001066 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001067 "updating last_seqno: old %u, new %u\n",
1068 orig_node->last_real_seqno, seqno);
Al Viroe0f52112012-04-22 07:46:29 +01001069 orig_node->last_real_seqno = seqno;
Marek Lindnerfc957272011-07-30 12:04:12 +02001070 }
1071
Marek Lindnerfc957272011-07-30 12:04:12 +02001072out:
1073 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001074 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001075 return ret;
1076}
1077
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001078static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001079 struct batadv_ogm_packet *batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001080 const unsigned char *tt_buff,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001081 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001082{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001083 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
1084 struct batadv_hard_iface *hard_iface;
1085 struct batadv_orig_node *orig_neigh_node, *orig_node;
1086 struct batadv_neigh_node *router = NULL, *router_router = NULL;
1087 struct batadv_neigh_node *orig_neigh_router = NULL;
Marek Lindnerfc957272011-07-30 12:04:12 +02001088 int has_directlink_flag;
1089 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
Antonio Quartulli281581d2013-03-25 22:27:00 +01001090 int is_bidirect;
Marek Lindner75cd33f2012-03-01 15:35:16 +08001091 bool is_single_hop_neigh = false;
Marek Lindner13b25412012-03-11 06:17:53 +08001092 bool is_from_best_next_hop = false;
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001093 int sameseq, similar_ttl;
1094 enum batadv_dup_status dup_status;
Marek Lindnerfc957272011-07-30 12:04:12 +02001095 uint32_t if_incoming_seqno;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001096 uint8_t *prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001097
1098 /* Silently drop when the batman packet is actually not a
1099 * correct packet.
1100 *
1101 * This might happen if a packet is padded (e.g. Ethernet has a
1102 * minimum frame length of 64 byte) and the aggregation interprets
1103 * it as an additional length.
1104 *
1105 * TODO: A more sane solution would be to have a bit in the
Sven Eckelmann96412692012-06-05 22:31:30 +02001106 * batadv_ogm_packet to detect whether the packet is the last
Marek Lindnerfc957272011-07-30 12:04:12 +02001107 * packet in an aggregation. Here we expect that the padding
1108 * is always zero (or not 0x01)
1109 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001110 if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
Marek Lindnerfc957272011-07-30 12:04:12 +02001111 return;
1112
1113 /* could be changed by schedule_own_packet() */
Marek Lindner14511512012-08-02 17:20:26 +02001114 if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
Marek Lindnerfc957272011-07-30 12:04:12 +02001115
Sven Eckelmann96412692012-06-05 22:31:30 +02001116 if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001117 has_directlink_flag = 1;
1118 else
1119 has_directlink_flag = 0;
Marek Lindnerfc957272011-07-30 12:04:12 +02001120
Sven Eckelmann96412692012-06-05 22:31:30 +02001121 if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
Marek Lindner75cd33f2012-03-01 15:35:16 +08001122 is_single_hop_neigh = true;
Marek Lindnerfc957272011-07-30 12:04:12 +02001123
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001124 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Antonio Quartullic0275e22012-12-11 20:29:22 +01001125 "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 +02001126 ethhdr->h_source, if_incoming->net_dev->name,
Sven Eckelmann96412692012-06-05 22:31:30 +02001127 if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
1128 batadv_ogm_packet->prev_sender,
1129 ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn,
1130 ntohs(batadv_ogm_packet->tt_crc),
1131 batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
1132 batadv_ogm_packet->header.ttl,
1133 batadv_ogm_packet->header.version, has_directlink_flag);
Marek Lindnerfc957272011-07-30 12:04:12 +02001134
1135 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001136 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001137 if (hard_iface->if_status != BATADV_IF_ACTIVE)
Marek Lindnerfc957272011-07-30 12:04:12 +02001138 continue;
1139
1140 if (hard_iface->soft_iface != if_incoming->soft_iface)
1141 continue;
1142
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001143 if (batadv_compare_eth(ethhdr->h_source,
1144 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001145 is_my_addr = 1;
1146
Sven Eckelmann96412692012-06-05 22:31:30 +02001147 if (batadv_compare_eth(batadv_ogm_packet->orig,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001148 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001149 is_my_orig = 1;
1150
Sven Eckelmann96412692012-06-05 22:31:30 +02001151 if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001152 hard_iface->net_dev->dev_addr))
Marek Lindnerfc957272011-07-30 12:04:12 +02001153 is_my_oldorig = 1;
Marek Lindnerfc957272011-07-30 12:04:12 +02001154 }
1155 rcu_read_unlock();
1156
Marek Lindnerfc957272011-07-30 12:04:12 +02001157 if (is_my_addr) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001158 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001159 "Drop packet: received my own broadcast (sender: %pM)\n",
1160 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001161 return;
1162 }
1163
Marek Lindnerfc957272011-07-30 12:04:12 +02001164 if (is_my_orig) {
1165 unsigned long *word;
1166 int offset;
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001167 int32_t bit_pos;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001168 int16_t if_num;
1169 uint8_t *weight;
Marek Lindnerfc957272011-07-30 12:04:12 +02001170
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001171 orig_neigh_node = batadv_get_orig_node(bat_priv,
1172 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001173 if (!orig_neigh_node)
1174 return;
1175
1176 /* neighbor has to indicate direct link and it has to
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001177 * come via the corresponding interface
1178 * save packet seqno for bidirectional check
1179 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001180 if (has_directlink_flag &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001181 batadv_compare_eth(if_incoming->net_dev->dev_addr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001182 batadv_ogm_packet->orig)) {
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001183 if_num = if_incoming->if_num;
1184 offset = if_num * BATADV_NUM_WORDS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001185
1186 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
1187 word = &(orig_neigh_node->bcast_own[offset]);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001188 bit_pos = if_incoming_seqno - 2;
Sven Eckelmann96412692012-06-05 22:31:30 +02001189 bit_pos -= ntohl(batadv_ogm_packet->seqno);
Sven Eckelmann9b4a1152012-05-12 13:48:53 +02001190 batadv_set_bit(word, bit_pos);
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001191 weight = &orig_neigh_node->bcast_own_sum[if_num];
1192 *weight = bitmap_weight(word,
1193 BATADV_TQ_LOCAL_WINDOW_SIZE);
Marek Lindnerfc957272011-07-30 12:04:12 +02001194 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
1195 }
1196
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001197 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001198 "Drop packet: originator packet from myself (via neighbor)\n");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001199 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001200 return;
1201 }
1202
1203 if (is_my_oldorig) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001204 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001205 "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
1206 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001207 return;
1208 }
1209
Sven Eckelmann96412692012-06-05 22:31:30 +02001210 if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001211 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001212 "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
1213 ethhdr->h_source);
Marek Lindner13b25412012-03-11 06:17:53 +08001214 return;
1215 }
1216
Sven Eckelmann96412692012-06-05 22:31:30 +02001217 orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
Marek Lindnerfc957272011-07-30 12:04:12 +02001218 if (!orig_node)
1219 return;
1220
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001221 dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
1222 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001223
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001224 if (dup_status == BATADV_PROTECTED) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001225 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001226 "Drop packet: packet within seqno protection time (sender: %pM)\n",
1227 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001228 goto out;
1229 }
1230
Sven Eckelmann96412692012-06-05 22:31:30 +02001231 if (batadv_ogm_packet->tq == 0) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001232 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001233 "Drop packet: originator packet with tq equal 0\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001234 goto out;
1235 }
1236
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001237 router = batadv_orig_node_get_router(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001238 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001239 router_router = batadv_orig_node_get_router(router->orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001240
Marek Lindner13b25412012-03-11 06:17:53 +08001241 if ((router && router->tq_avg != 0) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001242 (batadv_compare_eth(router->addr, ethhdr->h_source)))
Marek Lindner13b25412012-03-11 06:17:53 +08001243 is_from_best_next_hop = true;
1244
Sven Eckelmann96412692012-06-05 22:31:30 +02001245 prev_sender = batadv_ogm_packet->prev_sender;
Marek Lindnerfc957272011-07-30 12:04:12 +02001246 /* avoid temporary routing loops */
1247 if (router && router_router &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001248 (batadv_compare_eth(router->addr, prev_sender)) &&
Sven Eckelmann96412692012-06-05 22:31:30 +02001249 !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001250 (batadv_compare_eth(router->addr, router_router->addr))) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001251 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001252 "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
1253 ethhdr->h_source);
Marek Lindnerfc957272011-07-30 12:04:12 +02001254 goto out;
1255 }
1256
1257 /* if sender is a direct neighbor the sender mac equals
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001258 * originator mac
1259 */
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001260 if (is_single_hop_neigh)
1261 orig_neigh_node = orig_node;
1262 else
1263 orig_neigh_node = batadv_get_orig_node(bat_priv,
1264 ethhdr->h_source);
1265
Marek Lindnerfc957272011-07-30 12:04:12 +02001266 if (!orig_neigh_node)
1267 goto out;
1268
Martin Hundebølld56b1702013-01-25 11:12:39 +01001269 /* Update nc_nodes of the originator */
1270 batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
1271 batadv_ogm_packet, is_single_hop_neigh);
1272
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001273 orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001274
1275 /* drop packet if sender is not a direct neighbor and if we
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001276 * don't route towards it
1277 */
Marek Lindnerfc957272011-07-30 12:04:12 +02001278 if (!is_single_hop_neigh && (!orig_neigh_router)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001279 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001280 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001281 goto out_neigh;
1282 }
1283
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001284 is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001285 batadv_ogm_packet, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001286
Sven Eckelmann30d3c512012-05-12 02:09:36 +02001287 batadv_bonding_save_primary(orig_node, orig_neigh_node,
Sven Eckelmann96412692012-06-05 22:31:30 +02001288 batadv_ogm_packet);
Marek Lindnerfc957272011-07-30 12:04:12 +02001289
1290 /* update ranking if it is not a duplicate or has the same
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001291 * seqno and similar ttl as the non-duplicate
1292 */
Sven Eckelmann96412692012-06-05 22:31:30 +02001293 sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001294 similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
1295 if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
1296 (sameseq && similar_ttl)))
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001297 batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
Sven Eckelmann96412692012-06-05 22:31:30 +02001298 batadv_ogm_packet, if_incoming,
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001299 tt_buff, dup_status);
Marek Lindnerfc957272011-07-30 12:04:12 +02001300
1301 /* is single hop (direct) neighbor */
1302 if (is_single_hop_neigh) {
Marek Lindnerfc957272011-07-30 12:04:12 +02001303 /* mark direct link on incoming interface */
Sven Eckelmann96412692012-06-05 22:31:30 +02001304 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001305 is_single_hop_neigh,
1306 is_from_best_next_hop, if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001307
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001308 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001309 "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001310 goto out_neigh;
1311 }
1312
1313 /* multihop originator */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001314 if (!is_bidirect) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001315 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001316 "Drop packet: not received via bidirectional link\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001317 goto out_neigh;
1318 }
1319
Simon Wunderlich7c24bbb2013-05-23 13:07:42 +02001320 if (dup_status == BATADV_NEIGH_DUP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001321 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001322 "Drop packet: duplicate packet received\n");
Marek Lindnerfc957272011-07-30 12:04:12 +02001323 goto out_neigh;
1324 }
1325
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001326 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001327 "Forwarding packet: rebroadcast originator packet\n");
Sven Eckelmann96412692012-06-05 22:31:30 +02001328 batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001329 is_single_hop_neigh, is_from_best_next_hop,
1330 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001331
1332out_neigh:
1333 if ((orig_neigh_node) && (!is_single_hop_neigh))
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001334 batadv_orig_node_free_ref(orig_neigh_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001335out:
1336 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001337 batadv_neigh_node_free_ref(router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001338 if (router_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001339 batadv_neigh_node_free_ref(router_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001340 if (orig_neigh_router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001341 batadv_neigh_node_free_ref(orig_neigh_router);
Marek Lindnerfc957272011-07-30 12:04:12 +02001342
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001343 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfc957272011-07-30 12:04:12 +02001344}
1345
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001346static int batadv_iv_ogm_receive(struct sk_buff *skb,
Sven Eckelmann56303d32012-06-05 22:31:31 +02001347 struct batadv_hard_iface *if_incoming)
Marek Lindnerfc957272011-07-30 12:04:12 +02001348{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001349 struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Sven Eckelmann96412692012-06-05 22:31:30 +02001350 struct batadv_ogm_packet *batadv_ogm_packet;
Marek Lindner8780dad2011-12-05 04:01:51 +08001351 struct ethhdr *ethhdr;
1352 int buff_pos = 0, packet_len;
1353 unsigned char *tt_buff, *packet_buff;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001354 bool ret;
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001355 uint8_t *packet_pos;
Marek Lindnerc3e29312012-03-04 16:56:25 +08001356
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001357 ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001358 if (!ret)
1359 return NET_RX_DROP;
Marek Lindnerfc957272011-07-30 12:04:12 +02001360
Marek Lindneredbf12b2012-03-11 06:17:49 +08001361 /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
1362 * that does not have B.A.T.M.A.N. IV enabled ?
1363 */
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001364 if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
Marek Lindneredbf12b2012-03-11 06:17:49 +08001365 return NET_RX_DROP;
1366
Sven Eckelmannd69909d2012-06-03 22:19:20 +02001367 batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
1368 batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
Martin Hundebøllf8214862012-04-20 17:02:45 +02001369 skb->len + ETH_HLEN);
1370
Marek Lindner8780dad2011-12-05 04:01:51 +08001371 packet_len = skb_headlen(skb);
Antonio Quartulli7ed4be92013-04-08 15:08:18 +02001372 ethhdr = eth_hdr(skb);
Marek Lindner8780dad2011-12-05 04:01:51 +08001373 packet_buff = skb->data;
Sven Eckelmann96412692012-06-05 22:31:30 +02001374 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
Marek Lindnerfc957272011-07-30 12:04:12 +02001375
1376 /* unpack the aggregated packets and process them one by one */
Marek Lindnerb47506d2013-03-04 10:39:49 +08001377 while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
1378 batadv_ogm_packet->tt_num_changes)) {
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001379 tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
Marek Lindnerfc957272011-07-30 12:04:12 +02001380
Sven Eckelmann96412692012-06-05 22:31:30 +02001381 batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff,
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001382 if_incoming);
Marek Lindnerfc957272011-07-30 12:04:12 +02001383
Sven Eckelmann7e071c72012-06-03 22:19:13 +02001384 buff_pos += BATADV_OGM_HLEN;
Sven Eckelmann96412692012-06-05 22:31:30 +02001385 buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
Marek Lindnerfc957272011-07-30 12:04:12 +02001386
Sven Eckelmannc67893d2012-07-08 18:33:51 +02001387 packet_pos = packet_buff + buff_pos;
1388 batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
Marek Lindnerb47506d2013-03-04 10:39:49 +08001389 }
Marek Lindnerc3e29312012-03-04 16:56:25 +08001390
1391 kfree_skb(skb);
1392 return NET_RX_SUCCESS;
Marek Lindnerfc957272011-07-30 12:04:12 +02001393}
Marek Lindner1c280472011-11-28 17:40:17 +08001394
Sven Eckelmann56303d32012-06-05 22:31:31 +02001395static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
Marek Lindner519d3492012-04-18 17:15:57 +08001396 .name = "BATMAN_IV",
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001397 .bat_iface_enable = batadv_iv_ogm_iface_enable,
1398 .bat_iface_disable = batadv_iv_ogm_iface_disable,
1399 .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
1400 .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
1401 .bat_ogm_schedule = batadv_iv_ogm_schedule,
1402 .bat_ogm_emit = batadv_iv_ogm_emit,
Marek Lindner1c280472011-11-28 17:40:17 +08001403};
1404
Sven Eckelmann81c524f2012-05-12 02:09:22 +02001405int __init batadv_iv_init(void)
Marek Lindner1c280472011-11-28 17:40:17 +08001406{
Marek Lindnerc3e29312012-03-04 16:56:25 +08001407 int ret;
1408
1409 /* batman originator packet */
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001410 ret = batadv_recv_handler_register(BATADV_IV_OGM,
1411 batadv_iv_ogm_receive);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001412 if (ret < 0)
1413 goto out;
1414
Sven Eckelmannfe8bc392012-05-12 18:33:51 +02001415 ret = batadv_algo_register(&batadv_batman_iv);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001416 if (ret < 0)
1417 goto handler_unregister;
1418
1419 goto out;
1420
1421handler_unregister:
Sven Eckelmannacd34af2012-06-03 22:19:21 +02001422 batadv_recv_handler_unregister(BATADV_IV_OGM);
Marek Lindnerc3e29312012-03-04 16:56:25 +08001423out:
1424 return ret;
Marek Lindner1c280472011-11-28 17:40:17 +08001425}