blob: 13444e92bc99c9d3acef469be562e980d3469d2d [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "routing.h"
24#include "send.h"
25#include "hash.h"
26#include "soft-interface.h"
27#include "hard-interface.h"
28#include "icmp_socket.h"
29#include "translation-table.h"
30#include "originator.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031#include "ring_buffer.h"
32#include "vis.h"
33#include "aggregation.h"
34#include "gateway_common.h"
35#include "gateway_client.h"
36#include "unicast.h"
37
Marek Lindnere6c10f42011-02-18 12:33:20 +000038void slide_own_bcast_window(struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039{
Marek Lindnere6c10f42011-02-18 12:33:20 +000040 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +000042 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044 struct orig_node *orig_node;
45 unsigned long *word;
46 int i;
47 size_t word_index;
48
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049 for (i = 0; i < hash->size; i++) {
50 head = &hash->table[i];
51
Marek Lindnerfb778ea2011-01-19 20:01:40 +000052 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +000053 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Marek Lindner2ae2daf2011-01-19 20:01:42 +000054 spin_lock_bh(&orig_node->ogm_cnt_lock);
Marek Lindnere6c10f42011-02-18 12:33:20 +000055 word_index = hard_iface->if_num * NUM_WORDS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056 word = &(orig_node->bcast_own[word_index]);
57
58 bit_get_packet(bat_priv, word, 1, 0);
Marek Lindnere6c10f42011-02-18 12:33:20 +000059 orig_node->bcast_own_sum[hard_iface->if_num] =
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000060 bit_packet_count(word);
Marek Lindner2ae2daf2011-01-19 20:01:42 +000061 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +000063 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065}
66
Antonio Quartullia73105b2011-04-27 14:27:44 +020067static void update_transtable(struct bat_priv *bat_priv,
68 struct orig_node *orig_node,
69 const unsigned char *tt_buff,
70 uint8_t tt_num_changes, uint8_t ttvn,
71 uint16_t tt_crc)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072{
Antonio Quartullia73105b2011-04-27 14:27:44 +020073 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
74 bool full_table = true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075
Antonio Quartullia73105b2011-04-27 14:27:44 +020076 /* the ttvn increased by one -> we can apply the attached changes */
77 if (ttvn - orig_ttvn == 1) {
Antonio Quartulli015758d2011-07-09 17:52:13 +020078 /* the OGM could not contain the changes due to their size or
79 * because they have already been sent TT_OGM_APPEND_MAX times.
80 * In this case send a tt request */
Antonio Quartullia73105b2011-04-27 14:27:44 +020081 if (!tt_num_changes) {
82 full_table = false;
83 goto request_table;
84 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085
Antonio Quartullia73105b2011-04-27 14:27:44 +020086 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
87 (struct tt_change *)tt_buff);
88
Antonio Quartulli015758d2011-07-09 17:52:13 +020089 /* Even if we received the precomputed crc with the OGM, we
90 * prefer to recompute it to spot any possible inconsistency
Antonio Quartullia73105b2011-04-27 14:27:44 +020091 * in the global table */
Antonio Quartullia73105b2011-04-27 14:27:44 +020092 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullia7f9bec2011-07-07 14:24:34 +020093
94 /* The ttvn alone is not enough to guarantee consistency
Antonio Quartulli015758d2011-07-09 17:52:13 +020095 * because a single value could represent different states
Antonio Quartullia7f9bec2011-07-07 14:24:34 +020096 * (due to the wrap around). Thus a node has to check whether
97 * the resulting table (after applying the changes) is still
98 * consistent or not. E.g. a node could disconnect while its
99 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
100 * checking the CRC value is mandatory to detect the
101 * inconsistency */
102 if (orig_node->tt_crc != tt_crc)
103 goto request_table;
104
Antonio Quartullicc47f662011-04-27 14:27:57 +0200105 /* Roaming phase is over: tables are in sync again. I can
106 * unset the flag */
107 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200108 } else {
109 /* if we missed more than one change or our tables are not
110 * in sync anymore -> request fresh tt data */
111 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
112request_table:
113 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
114 "Need to retrieve the correct information "
115 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
116 "%u num_changes: %u)\n", orig_node->orig, ttvn,
117 orig_ttvn, tt_crc, orig_node->tt_crc,
118 tt_num_changes);
119 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
120 full_table);
121 return;
122 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 }
124}
125
Antonio Quartullia73105b2011-04-27 14:27:44 +0200126static void update_route(struct bat_priv *bat_priv,
127 struct orig_node *orig_node,
128 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000130 struct neigh_node *curr_router;
131
132 curr_router = orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000133
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000135 if ((curr_router) && (!neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
137 orig_node->orig);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200138 tt_global_del_orig(bat_priv, orig_node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200139 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000141 /* route added */
142 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143
144 bat_dbg(DBG_ROUTES, bat_priv,
145 "Adding route towards: %pM (via %pM)\n",
146 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000147 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +0200148 } else if (neigh_node && curr_router) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149 bat_dbg(DBG_ROUTES, bat_priv,
150 "Changing route towards: %pM "
151 "(now via %pM - was via %pM)\n",
152 orig_node->orig, neigh_node->addr,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000153 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154 }
155
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000156 if (curr_router)
157 neigh_node_free_ref(curr_router);
158
159 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +0000160 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
161 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000162
163 spin_lock_bh(&orig_node->neigh_list_lock);
164 rcu_assign_pointer(orig_node->router, neigh_node);
165 spin_unlock_bh(&orig_node->neigh_list_lock);
166
167 /* decrease refcount of previous best neighbor */
168 if (curr_router)
169 neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170}
171
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200173 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000175 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176
177 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000178 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000180 router = orig_node_get_router(orig_node);
181
182 if (router != neigh_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200183 update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000184
185out:
186 if (router)
187 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188}
189
190static int is_bidirectional_neigh(struct orig_node *orig_node,
191 struct orig_node *orig_neigh_node,
192 struct batman_packet *batman_packet,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000193 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194{
195 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner1605d0d2011-02-18 12:28:11 +0000196 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000197 struct hlist_node *node;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200198 uint8_t total_count;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000199 uint8_t orig_eq_count, neigh_rq_count, tq_own;
200 int tq_asym_penalty, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
Daniele Furlan27aea212011-05-07 22:45:19 +0200202 /* find corresponding one hop neighbor */
203 rcu_read_lock();
204 hlist_for_each_entry_rcu(tmp_neigh_node, node,
205 &orig_neigh_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Daniele Furlan27aea212011-05-07 22:45:19 +0200207 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
208 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000209
Daniele Furlan27aea212011-05-07 22:45:19 +0200210 if (tmp_neigh_node->if_incoming != if_incoming)
211 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000212
Daniele Furlan27aea212011-05-07 22:45:19 +0200213 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
214 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000215
Daniele Furlan27aea212011-05-07 22:45:19 +0200216 neigh_node = tmp_neigh_node;
217 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218 }
Daniele Furlan27aea212011-05-07 22:45:19 +0200219 rcu_read_unlock();
220
221 if (!neigh_node)
222 neigh_node = create_neighbor(orig_neigh_node,
223 orig_neigh_node,
224 orig_neigh_node->orig,
225 if_incoming);
226
227 if (!neigh_node)
228 goto out;
229
Antonio Quartulli015758d2011-07-09 17:52:13 +0200230 /* if orig_node is direct neighbor update neigh_node last_valid */
Daniele Furlan27aea212011-05-07 22:45:19 +0200231 if (orig_node == orig_neigh_node)
232 neigh_node->last_valid = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233
234 orig_node->last_valid = jiffies;
235
Daniele Furlan27aea212011-05-07 22:45:19 +0200236 /* find packet count of corresponding one hop neighbor */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000237 spin_lock_bh(&orig_node->ogm_cnt_lock);
238 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
239 neigh_rq_count = neigh_node->real_packet_count;
240 spin_unlock_bh(&orig_node->ogm_cnt_lock);
241
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 /* pay attention to not get a value bigger than 100 % */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000243 total_count = (orig_eq_count > neigh_rq_count ?
244 neigh_rq_count : orig_eq_count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
246 /* if we have too few packets (too less data) we set tq_own to zero */
247 /* if we receive too few packets it is not considered bidirectional */
248 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
Marek Lindner0ede9f42011-01-25 21:52:10 +0000249 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
250 tq_own = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 else
252 /* neigh_node->real_packet_count is never zero as we
253 * only purge old information when getting new
254 * information */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000255 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256
257 /*
258 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
259 * affect the nearly-symmetric links only a little, but
260 * punishes asymmetric links more. This will give a value
261 * between 0 and TQ_MAX_VALUE
262 */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000263 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
264 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
265 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
266 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
267 (TQ_LOCAL_WINDOW_SIZE *
268 TQ_LOCAL_WINDOW_SIZE *
269 TQ_LOCAL_WINDOW_SIZE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
Marek Lindner0ede9f42011-01-25 21:52:10 +0000271 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
272 (TQ_MAX_VALUE * TQ_MAX_VALUE));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
274 bat_dbg(DBG_BATMAN, bat_priv,
275 "bidirectional: "
276 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
277 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
278 "total tq: %3i\n",
279 orig_node->orig, orig_neigh_node->orig, total_count,
Marek Lindner0ede9f42011-01-25 21:52:10 +0000280 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
282 /* if link has the minimum required transmission quality
283 * consider it bidirectional */
284 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnera775eb82011-01-19 20:01:39 +0000285 ret = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286
Marek Lindnera775eb82011-01-19 20:01:39 +0000287out:
288 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000289 neigh_node_free_ref(neigh_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000290 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291}
292
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000293/* caller must hold the neigh_list_lock */
294void bonding_candidate_del(struct orig_node *orig_node,
295 struct neigh_node *neigh_node)
296{
297 /* this neighbor is not part of our candidate list */
298 if (list_empty(&neigh_node->bonding_list))
299 goto out;
300
301 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000302 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000303 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000304 atomic_dec(&orig_node->bond_candidates);
305
306out:
307 return;
308}
309
310static void bonding_candidate_add(struct orig_node *orig_node,
311 struct neigh_node *neigh_node)
312{
313 struct hlist_node *node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000314 struct neigh_node *tmp_neigh_node, *router = NULL;
315 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000316
317 spin_lock_bh(&orig_node->neigh_list_lock);
318
319 /* only consider if it has the same primary address ... */
Marek Lindner39901e72011-02-18 12:28:08 +0000320 if (!compare_eth(orig_node->orig,
321 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000322 goto candidate_del;
323
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000324 router = orig_node_get_router(orig_node);
325 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000326 goto candidate_del;
327
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000328 /* ... and is good enough to be considered */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000329 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000330 goto candidate_del;
331
332 /**
333 * check if we have another candidate with the same mac address or
334 * interface. If we do, we won't select this candidate because of
335 * possible interference.
336 */
337 hlist_for_each_entry_rcu(tmp_neigh_node, node,
338 &orig_node->neigh_list, list) {
339
340 if (tmp_neigh_node == neigh_node)
341 continue;
342
343 /* we only care if the other candidate is even
344 * considered as candidate. */
345 if (list_empty(&tmp_neigh_node->bonding_list))
346 continue;
347
348 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Marek Lindner39901e72011-02-18 12:28:08 +0000349 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000350 interference_candidate = 1;
351 break;
352 }
353 }
354
355 /* don't care further if it is an interference candidate */
356 if (interference_candidate)
357 goto candidate_del;
358
359 /* this neighbor already is part of our candidate list */
360 if (!list_empty(&neigh_node->bonding_list))
361 goto out;
362
Marek Lindner44524fc2011-02-10 14:33:53 +0000363 if (!atomic_inc_not_zero(&neigh_node->refcount))
364 goto out;
365
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000366 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000367 atomic_inc(&orig_node->bond_candidates);
368 goto out;
369
370candidate_del:
371 bonding_candidate_del(orig_node, neigh_node);
372
373out:
374 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000375
376 if (router)
377 neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000378}
379
380/* copy primary address for bonding */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200381static void bonding_save_primary(const struct orig_node *orig_node,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000382 struct orig_node *orig_neigh_node,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200383 const struct batman_packet *batman_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000384{
385 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
386 return;
387
388 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
389}
390
Sven Eckelmann747e4222011-05-14 23:14:50 +0200391static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
392 const struct ethhdr *ethhdr,
393 const struct batman_packet *batman_packet,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000394 struct hard_iface *if_incoming,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200395 const unsigned char *tt_buff, int is_duplicate)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396{
397 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000398 struct neigh_node *router = NULL;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000399 struct orig_node *orig_node_tmp;
Marek Lindner9591a792010-12-12 21:57:11 +0000400 struct hlist_node *node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000401 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402
403 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
404 "Searching and updating originator entry of received packet\n");
405
Marek Lindnerf987ed62010-12-12 21:57:12 +0000406 rcu_read_lock();
407 hlist_for_each_entry_rcu(tmp_neigh_node, node,
408 &orig_node->neigh_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000409 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Marek Lindner44524fc2011-02-10 14:33:53 +0000410 (tmp_neigh_node->if_incoming == if_incoming) &&
411 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
412 if (neigh_node)
413 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 neigh_node = tmp_neigh_node;
415 continue;
416 }
417
418 if (is_duplicate)
419 continue;
420
Linus Lüssing68003902011-03-14 22:43:40 +0000421 spin_lock_bh(&tmp_neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422 ring_buffer_set(tmp_neigh_node->tq_recv,
423 &tmp_neigh_node->tq_index, 0);
424 tmp_neigh_node->tq_avg =
425 ring_buffer_avg(tmp_neigh_node->tq_recv);
Linus Lüssing68003902011-03-14 22:43:40 +0000426 spin_unlock_bh(&tmp_neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 }
428
429 if (!neigh_node) {
430 struct orig_node *orig_tmp;
431
432 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
433 if (!orig_tmp)
Marek Lindnera775eb82011-01-19 20:01:39 +0000434 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000435
436 neigh_node = create_neighbor(orig_node, orig_tmp,
437 ethhdr->h_source, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000438
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000439 orig_node_free_ref(orig_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000441 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442 } else
443 bat_dbg(DBG_BATMAN, bat_priv,
444 "Updating existing last-hop neighbor of originator\n");
445
Marek Lindnera775eb82011-01-19 20:01:39 +0000446 rcu_read_unlock();
447
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 orig_node->flags = batman_packet->flags;
449 neigh_node->last_valid = jiffies;
450
Linus Lüssing68003902011-03-14 22:43:40 +0000451 spin_lock_bh(&neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 ring_buffer_set(neigh_node->tq_recv,
453 &neigh_node->tq_index,
454 batman_packet->tq);
455 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
Linus Lüssing68003902011-03-14 22:43:40 +0000456 spin_unlock_bh(&neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457
458 if (!is_duplicate) {
459 orig_node->last_ttl = batman_packet->ttl;
460 neigh_node->last_ttl = batman_packet->ttl;
461 }
462
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000463 bonding_candidate_add(orig_node, neigh_node);
464
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465 /* if this neighbor already is our next hop there is nothing
466 * to change */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000467 router = orig_node_get_router(orig_node);
468 if (router == neigh_node)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200469 goto update_tt;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470
471 /* if this neighbor does not offer a better TQ we won't consider it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000472 if (router && (router->tq_avg > neigh_node->tq_avg))
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200473 goto update_tt;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000474
Antonio Quartulli015758d2011-07-09 17:52:13 +0200475 /* if the TQ is the same and the link not more symmetric we
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000476 * won't consider it either */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000477 if (router && (neigh_node->tq_avg == router->tq_avg)) {
478 orig_node_tmp = router->orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000479 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
480 bcast_own_sum_orig =
481 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
482 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
483
484 orig_node_tmp = neigh_node->orig_node;
485 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
486 bcast_own_sum_neigh =
487 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
488 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
489
490 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200491 goto update_tt;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000492 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
Antonio Quartullia73105b2011-04-27 14:27:44 +0200494 update_routes(bat_priv, orig_node, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200496update_tt:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200497 /* I have to check for transtable changes only if the OGM has been
498 * sent through a primary interface */
499 if (((batman_packet->orig != ethhdr->h_source) &&
500 (batman_packet->ttl > 2)) ||
501 (batman_packet->flags & PRIMARIES_FIRST_HOP))
502 update_transtable(bat_priv, orig_node, tt_buff,
503 batman_packet->tt_num_changes,
504 batman_packet->ttvn,
505 batman_packet->tt_crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000507 if (orig_node->gw_flags != batman_packet->gw_flags)
508 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
509
510 orig_node->gw_flags = batman_packet->gw_flags;
511
512 /* restart gateway selection if fast or late switching was enabled */
513 if ((orig_node->gw_flags) &&
514 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
515 (atomic_read(&bat_priv->gw_sel_class) > 2))
516 gw_check_election(bat_priv, orig_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000517
518 goto out;
519
520unlock:
521 rcu_read_unlock();
522out:
523 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000524 neigh_node_free_ref(neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000525 if (router)
526 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527}
528
529/* checks whether the host restarted and is in the protection time.
530 * returns:
531 * 0 if the packet is to be accepted
532 * 1 if the packet is to be ignored.
533 */
534static int window_protected(struct bat_priv *bat_priv,
535 int32_t seq_num_diff,
536 unsigned long *last_reset)
537{
538 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
539 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
540 if (time_after(jiffies, *last_reset +
541 msecs_to_jiffies(RESET_PROTECTION_MS))) {
542
543 *last_reset = jiffies;
544 bat_dbg(DBG_BATMAN, bat_priv,
545 "old packet received, start protection\n");
546
547 return 0;
548 } else
549 return 1;
550 }
551 return 0;
552}
553
554/* processes a batman packet for all interfaces, adjusts the sequence number and
555 * finds out whether it is a duplicate.
556 * returns:
557 * 1 the packet is a duplicate
558 * 0 the packet has not yet been received
559 * -1 the packet is old and has been received while the seqno window
560 * was protected. Caller should drop it.
561 */
David Howellsb2c44a52011-06-15 09:41:36 +0200562static int count_real_packets(const struct ethhdr *ethhdr,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200563 const struct batman_packet *batman_packet,
564 const struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565{
566 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
567 struct orig_node *orig_node;
568 struct neigh_node *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000569 struct hlist_node *node;
David Howellsb2c44a52011-06-15 09:41:36 +0200570 int is_duplicate = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 int32_t seq_diff;
572 int need_update = 0;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000573 int set_mark, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
575 orig_node = get_orig_node(bat_priv, batman_packet->orig);
576 if (!orig_node)
577 return 0;
578
Marek Lindner0ede9f42011-01-25 21:52:10 +0000579 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
581
582 /* signalize caller that the packet is to be dropped. */
583 if (window_protected(bat_priv, seq_diff,
584 &orig_node->batman_seqno_reset))
Marek Lindner0ede9f42011-01-25 21:52:10 +0000585 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Marek Lindnerf987ed62010-12-12 21:57:12 +0000587 rcu_read_lock();
588 hlist_for_each_entry_rcu(tmp_neigh_node, node,
589 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590
591 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
592 orig_node->last_real_seqno,
593 batman_packet->seqno);
594
Marek Lindner39901e72011-02-18 12:28:08 +0000595 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 (tmp_neigh_node->if_incoming == if_incoming))
597 set_mark = 1;
598 else
599 set_mark = 0;
600
601 /* if the window moved, set the update flag. */
602 need_update |= bit_get_packet(bat_priv,
603 tmp_neigh_node->real_bits,
604 seq_diff, set_mark);
605
606 tmp_neigh_node->real_packet_count =
607 bit_packet_count(tmp_neigh_node->real_bits);
608 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000609 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610
611 if (need_update) {
612 bat_dbg(DBG_BATMAN, bat_priv,
613 "updating last_seqno: old %d, new %d\n",
614 orig_node->last_real_seqno, batman_packet->seqno);
615 orig_node->last_real_seqno = batman_packet->seqno;
616 }
617
Marek Lindner0ede9f42011-01-25 21:52:10 +0000618 ret = is_duplicate;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000619
Marek Lindner0ede9f42011-01-25 21:52:10 +0000620out:
621 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000622 orig_node_free_ref(orig_node);
Marek Lindner0ede9f42011-01-25 21:52:10 +0000623 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624}
625
Sven Eckelmann747e4222011-05-14 23:14:50 +0200626void receive_bat_packet(const struct ethhdr *ethhdr,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000627 struct batman_packet *batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200628 const unsigned char *tt_buff,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000629 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630{
631 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000632 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 struct orig_node *orig_neigh_node, *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000634 struct neigh_node *router = NULL, *router_router = NULL;
635 struct neigh_node *orig_neigh_router = NULL;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200636 int has_directlink_flag;
637 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
638 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
David Howellsb2c44a52011-06-15 09:41:36 +0200639 int is_duplicate;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000640 uint32_t if_incoming_seqno;
641
642 /* Silently drop when the batman packet is actually not a
643 * correct packet.
644 *
645 * This might happen if a packet is padded (e.g. Ethernet has a
646 * minimum frame length of 64 byte) and the aggregation interprets
647 * it as an additional length.
648 *
649 * TODO: A more sane solution would be to have a bit in the
650 * batman_packet to detect whether the packet is the last
651 * packet in an aggregation. Here we expect that the padding
652 * is always zero (or not 0x01)
653 */
654 if (batman_packet->packet_type != BAT_PACKET)
655 return;
656
657 /* could be changed by schedule_own_packet() */
658 if_incoming_seqno = atomic_read(&if_incoming->seqno);
659
660 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
661
Marek Lindner39901e72011-02-18 12:28:08 +0000662 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
663 batman_packet->orig) ? 1 : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
665 bat_dbg(DBG_BATMAN, bat_priv,
666 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200667 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
668 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 ethhdr->h_source, if_incoming->net_dev->name,
670 if_incoming->net_dev->dev_addr, batman_packet->orig,
671 batman_packet->prev_sender, batman_packet->seqno,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200672 batman_packet->ttvn, batman_packet->tt_crc,
673 batman_packet->tt_num_changes, batman_packet->tq,
674 batman_packet->ttl, batman_packet->version,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675 has_directlink_flag);
676
677 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000678 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
679 if (hard_iface->if_status != IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 continue;
681
Marek Lindnere6c10f42011-02-18 12:33:20 +0000682 if (hard_iface->soft_iface != if_incoming->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683 continue;
684
Marek Lindner39901e72011-02-18 12:28:08 +0000685 if (compare_eth(ethhdr->h_source,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000686 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687 is_my_addr = 1;
688
Marek Lindner39901e72011-02-18 12:28:08 +0000689 if (compare_eth(batman_packet->orig,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000690 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691 is_my_orig = 1;
692
Marek Lindner39901e72011-02-18 12:28:08 +0000693 if (compare_eth(batman_packet->prev_sender,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000694 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695 is_my_oldorig = 1;
696
Antonio Quartulli44e92bc2011-05-21 01:33:07 +0200697 if (is_broadcast_ether_addr(ethhdr->h_source))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698 is_broadcast = 1;
699 }
700 rcu_read_unlock();
701
702 if (batman_packet->version != COMPAT_VERSION) {
703 bat_dbg(DBG_BATMAN, bat_priv,
704 "Drop packet: incompatible batman version (%i)\n",
705 batman_packet->version);
706 return;
707 }
708
709 if (is_my_addr) {
710 bat_dbg(DBG_BATMAN, bat_priv,
711 "Drop packet: received my own broadcast (sender: %pM"
712 ")\n",
713 ethhdr->h_source);
714 return;
715 }
716
717 if (is_broadcast) {
718 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
719 "ignoring all packets with broadcast source addr (sender: %pM"
720 ")\n", ethhdr->h_source);
721 return;
722 }
723
724 if (is_my_orig) {
725 unsigned long *word;
726 int offset;
727
728 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 if (!orig_neigh_node)
730 return;
731
732 /* neighbor has to indicate direct link and it has to
733 * come via the corresponding interface */
Daniele Furland1829fa2011-06-07 00:45:55 +0200734 /* save packet seqno for bidirectional check */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000735 if (has_directlink_flag &&
Marek Lindner39901e72011-02-18 12:28:08 +0000736 compare_eth(if_incoming->net_dev->dev_addr,
Daniele Furland1829fa2011-06-07 00:45:55 +0200737 batman_packet->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738 offset = if_incoming->if_num * NUM_WORDS;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000739
740 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 word = &(orig_neigh_node->bcast_own[offset]);
Daniele Furland1829fa2011-06-07 00:45:55 +0200742 bit_mark(word,
743 if_incoming_seqno - batman_packet->seqno - 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
745 bit_packet_count(word);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000746 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 }
748
749 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
750 "originator packet from myself (via neighbor)\n");
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000751 orig_node_free_ref(orig_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 return;
753 }
754
755 if (is_my_oldorig) {
756 bat_dbg(DBG_BATMAN, bat_priv,
757 "Drop packet: ignoring all rebroadcast echos (sender: "
758 "%pM)\n", ethhdr->h_source);
759 return;
760 }
761
762 orig_node = get_orig_node(bat_priv, batman_packet->orig);
763 if (!orig_node)
764 return;
765
766 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
767
768 if (is_duplicate == -1) {
769 bat_dbg(DBG_BATMAN, bat_priv,
770 "Drop packet: packet within seqno protection time "
771 "(sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000772 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000773 }
774
775 if (batman_packet->tq == 0) {
776 bat_dbg(DBG_BATMAN, bat_priv,
777 "Drop packet: originator packet with tq equal 0\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000778 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 }
780
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000781 router = orig_node_get_router(orig_node);
782 if (router)
783 router_router = orig_node_get_router(router->orig_node);
784
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785 /* avoid temporary routing loops */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000786 if (router && router_router &&
787 (compare_eth(router->addr, batman_packet->prev_sender)) &&
Marek Lindner39901e72011-02-18 12:28:08 +0000788 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000789 (compare_eth(router->addr, router_router->addr))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790 bat_dbg(DBG_BATMAN, bat_priv,
791 "Drop packet: ignoring all rebroadcast packets that "
792 "may make me loop (sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000793 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 }
795
796 /* if sender is a direct neighbor the sender mac equals
797 * originator mac */
798 orig_neigh_node = (is_single_hop_neigh ?
799 orig_node :
800 get_orig_node(bat_priv, ethhdr->h_source));
801 if (!orig_neigh_node)
Marek Lindnerd0072602011-01-19 20:01:44 +0000802 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000804 orig_neigh_router = orig_node_get_router(orig_neigh_node);
805
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806 /* drop packet if sender is not a direct neighbor and if we
807 * don't route towards it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000808 if (!is_single_hop_neigh && (!orig_neigh_router)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809 bat_dbg(DBG_BATMAN, bat_priv,
810 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000811 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000812 }
813
814 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
815 batman_packet, if_incoming);
816
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000817 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
818
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819 /* update ranking if it is not a duplicate or has the same
820 * seqno and similar ttl as the non-duplicate */
821 if (is_bidirectional &&
822 (!is_duplicate ||
823 ((orig_node->last_real_seqno == batman_packet->seqno) &&
824 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
825 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200826 if_incoming, tt_buff, is_duplicate);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000828 /* is single hop (direct) neighbor */
829 if (is_single_hop_neigh) {
830
831 /* mark direct link on incoming interface */
832 schedule_forward_packet(orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200833 1, if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834
835 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
836 "rebroadcast neighbor packet with direct link flag\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000837 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000838 }
839
840 /* multihop originator */
841 if (!is_bidirectional) {
842 bat_dbg(DBG_BATMAN, bat_priv,
843 "Drop packet: not received via bidirectional link\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000844 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845 }
846
847 if (is_duplicate) {
848 bat_dbg(DBG_BATMAN, bat_priv,
849 "Drop packet: duplicate packet received\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000850 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851 }
852
853 bat_dbg(DBG_BATMAN, bat_priv,
854 "Forwarding packet: rebroadcast originator packet\n");
855 schedule_forward_packet(orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200856 0, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000857
858out_neigh:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000859 if ((orig_neigh_node) && (!is_single_hop_neigh))
860 orig_node_free_ref(orig_neigh_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000861out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000862 if (router)
863 neigh_node_free_ref(router);
864 if (router_router)
865 neigh_node_free_ref(router_router);
866 if (orig_neigh_router)
867 neigh_node_free_ref(orig_neigh_router);
868
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000869 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000870}
871
Marek Lindnere6c10f42011-02-18 12:33:20 +0000872int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874 struct ethhdr *ethhdr;
875
876 /* drop packet if it has not necessary minimum size */
877 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
878 return NET_RX_DROP;
879
880 ethhdr = (struct ethhdr *)skb_mac_header(skb);
881
882 /* packet with broadcast indication but unicast recipient */
883 if (!is_broadcast_ether_addr(ethhdr->h_dest))
884 return NET_RX_DROP;
885
886 /* packet with broadcast sender address */
887 if (is_broadcast_ether_addr(ethhdr->h_source))
888 return NET_RX_DROP;
889
890 /* create a copy of the skb, if needed, to modify it. */
891 if (skb_cow(skb, 0) < 0)
892 return NET_RX_DROP;
893
894 /* keep skb linear */
895 if (skb_linearize(skb) < 0)
896 return NET_RX_DROP;
897
898 ethhdr = (struct ethhdr *)skb_mac_header(skb);
899
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000900 receive_aggr_bat_packet(ethhdr,
901 skb->data,
902 skb_headlen(skb),
Marek Lindnere6c10f42011-02-18 12:33:20 +0000903 hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905 kfree_skb(skb);
906 return NET_RX_SUCCESS;
907}
908
909static int recv_my_icmp_packet(struct bat_priv *bat_priv,
910 struct sk_buff *skb, size_t icmp_len)
911{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200912 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000913 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000914 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915 struct icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000916 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000917
918 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000919
920 /* add data to device queue */
921 if (icmp_packet->msg_type != ECHO_REQUEST) {
922 bat_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000923 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924 }
925
Marek Lindner32ae9b22011-04-20 15:40:58 +0200926 primary_if = primary_if_get_selected(bat_priv);
927 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000928 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000929
930 /* answer echo request (ping) */
931 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +0000932 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000933 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000934 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000935
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000936 router = orig_node_get_router(orig_node);
937 if (!router)
938 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939
Marek Lindner44524fc2011-02-10 14:33:53 +0000940 /* create a copy of the skb, if needed, to modify it. */
941 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
942 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943
Marek Lindner44524fc2011-02-10 14:33:53 +0000944 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945
Marek Lindner44524fc2011-02-10 14:33:53 +0000946 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200947 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000948 icmp_packet->msg_type = ECHO_REPLY;
949 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000951 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000952 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000953
Marek Lindner44524fc2011-02-10 14:33:53 +0000954out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200955 if (primary_if)
956 hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000957 if (router)
958 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000959 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000960 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961 return ret;
962}
963
964static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000965 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200967 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000968 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000969 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970 struct icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000971 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972
973 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000974
975 /* send TTL exceeded if packet is an echo request (traceroute) */
976 if (icmp_packet->msg_type != ECHO_REQUEST) {
977 pr_debug("Warning - can't forward icmp packet from %pM to "
978 "%pM: ttl exceeded\n", icmp_packet->orig,
979 icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000980 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000981 }
982
Marek Lindner32ae9b22011-04-20 15:40:58 +0200983 primary_if = primary_if_get_selected(bat_priv);
984 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000985 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000986
987 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +0000988 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000989 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000990 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000991
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000992 router = orig_node_get_router(orig_node);
993 if (!router)
994 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995
Marek Lindner44524fc2011-02-10 14:33:53 +0000996 /* create a copy of the skb, if needed, to modify it. */
997 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
998 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999
Marek Lindner44524fc2011-02-10 14:33:53 +00001000 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001001
Marek Lindner44524fc2011-02-10 14:33:53 +00001002 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +02001003 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +00001004 icmp_packet->msg_type = TTL_EXCEEDED;
1005 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001007 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001008 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009
Marek Lindner44524fc2011-02-10 14:33:53 +00001010out:
Marek Lindner32ae9b22011-04-20 15:40:58 +02001011 if (primary_if)
1012 hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001013 if (router)
1014 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +00001015 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001016 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017 return ret;
1018}
1019
1020
Marek Lindnere6c10f42011-02-18 12:33:20 +00001021int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001022{
1023 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1024 struct icmp_packet_rr *icmp_packet;
1025 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +00001026 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001027 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028 int hdr_size = sizeof(struct icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +00001029 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030
1031 /**
1032 * we truncate all incoming icmp packets if they don't match our size
1033 */
1034 if (skb->len >= sizeof(struct icmp_packet_rr))
1035 hdr_size = sizeof(struct icmp_packet_rr);
1036
1037 /* drop packet if it has not necessary minimum size */
1038 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +00001039 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001040
1041 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1042
1043 /* packet with unicast indication but broadcast recipient */
1044 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001045 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046
1047 /* packet with broadcast sender address */
1048 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +00001049 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001050
1051 /* not for me */
1052 if (!is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001053 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054
1055 icmp_packet = (struct icmp_packet_rr *)skb->data;
1056
1057 /* add record route information if not full */
1058 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1059 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1060 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1061 ethhdr->h_dest, ETH_ALEN);
1062 icmp_packet->rr_cur++;
1063 }
1064
1065 /* packet for me */
1066 if (is_my_mac(icmp_packet->dst))
1067 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1068
1069 /* TTL exceeded */
1070 if (icmp_packet->ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +00001071 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001072
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001073 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +00001074 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +00001075 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001076 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +00001077
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001078 router = orig_node_get_router(orig_node);
1079 if (!router)
1080 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081
Marek Lindner44524fc2011-02-10 14:33:53 +00001082 /* create a copy of the skb, if needed, to modify it. */
1083 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1084 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085
Marek Lindner44524fc2011-02-10 14:33:53 +00001086 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001087
Marek Lindner44524fc2011-02-10 14:33:53 +00001088 /* decrement ttl */
1089 icmp_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090
Marek Lindner44524fc2011-02-10 14:33:53 +00001091 /* route it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001092 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001093 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001094
Marek Lindner44524fc2011-02-10 14:33:53 +00001095out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001096 if (router)
1097 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +00001098 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001099 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001100 return ret;
1101}
1102
Linus Lüssing55158622011-03-14 22:43:27 +00001103/* In the bonding case, send the packets in a round
1104 * robin fashion over the remaining interfaces.
1105 *
1106 * This method rotates the bonding list and increases the
1107 * returned router's refcount. */
1108static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001109 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +00001110{
1111 struct neigh_node *tmp_neigh_node;
1112 struct neigh_node *router = NULL, *first_candidate = NULL;
1113
1114 rcu_read_lock();
1115 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1116 bonding_list) {
1117 if (!first_candidate)
1118 first_candidate = tmp_neigh_node;
1119
1120 /* recv_if == NULL on the first node. */
1121 if (tmp_neigh_node->if_incoming == recv_if)
1122 continue;
1123
1124 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1125 continue;
1126
1127 router = tmp_neigh_node;
1128 break;
1129 }
1130
1131 /* use the first candidate if nothing was found. */
1132 if (!router && first_candidate &&
1133 atomic_inc_not_zero(&first_candidate->refcount))
1134 router = first_candidate;
1135
1136 if (!router)
1137 goto out;
1138
1139 /* selected should point to the next element
1140 * after the current router */
1141 spin_lock_bh(&primary_orig->neigh_list_lock);
1142 /* this is a list_move(), which unfortunately
1143 * does not exist as rcu version */
1144 list_del_rcu(&primary_orig->bond_list);
1145 list_add_rcu(&primary_orig->bond_list,
1146 &router->bonding_list);
1147 spin_unlock_bh(&primary_orig->neigh_list_lock);
1148
1149out:
1150 rcu_read_unlock();
1151 return router;
1152}
1153
1154/* Interface Alternating: Use the best of the
1155 * remaining candidates which are not using
1156 * this interface.
1157 *
1158 * Increases the returned router's refcount */
1159static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001160 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +00001161{
1162 struct neigh_node *tmp_neigh_node;
1163 struct neigh_node *router = NULL, *first_candidate = NULL;
1164
1165 rcu_read_lock();
1166 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1167 bonding_list) {
1168 if (!first_candidate)
1169 first_candidate = tmp_neigh_node;
1170
1171 /* recv_if == NULL on the first node. */
1172 if (tmp_neigh_node->if_incoming == recv_if)
1173 continue;
1174
1175 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1176 continue;
1177
1178 /* if we don't have a router yet
1179 * or this one is better, choose it. */
1180 if ((!router) ||
1181 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1182 /* decrement refcount of
1183 * previously selected router */
1184 if (router)
1185 neigh_node_free_ref(router);
1186
1187 router = tmp_neigh_node;
1188 atomic_inc_not_zero(&router->refcount);
1189 }
1190
1191 neigh_node_free_ref(tmp_neigh_node);
1192 }
1193
1194 /* use the first candidate if nothing was found. */
1195 if (!router && first_candidate &&
1196 atomic_inc_not_zero(&first_candidate->refcount))
1197 router = first_candidate;
1198
1199 rcu_read_unlock();
1200 return router;
1201}
1202
Antonio Quartullia73105b2011-04-27 14:27:44 +02001203int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1204{
1205 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1206 struct tt_query_packet *tt_query;
1207 struct ethhdr *ethhdr;
1208
1209 /* drop packet if it has not necessary minimum size */
1210 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1211 goto out;
1212
1213 /* I could need to modify it */
1214 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1215 goto out;
1216
1217 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1218
1219 /* packet with unicast indication but broadcast recipient */
1220 if (is_broadcast_ether_addr(ethhdr->h_dest))
1221 goto out;
1222
1223 /* packet with broadcast sender address */
1224 if (is_broadcast_ether_addr(ethhdr->h_source))
1225 goto out;
1226
1227 tt_query = (struct tt_query_packet *)skb->data;
1228
1229 tt_query->tt_data = ntohs(tt_query->tt_data);
1230
1231 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1232 case TT_REQUEST:
1233 /* If we cannot provide an answer the tt_request is
1234 * forwarded */
1235 if (!send_tt_response(bat_priv, tt_query)) {
1236 bat_dbg(DBG_TT, bat_priv,
1237 "Routing TT_REQUEST to %pM [%c]\n",
1238 tt_query->dst,
1239 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1240 tt_query->tt_data = htons(tt_query->tt_data);
1241 return route_unicast_packet(skb, recv_if);
1242 }
1243 break;
1244 case TT_RESPONSE:
Antonio Quartulli015758d2011-07-09 17:52:13 +02001245 /* packet needs to be linearized to access the TT changes */
Antonio Quartullia73105b2011-04-27 14:27:44 +02001246 if (skb_linearize(skb) < 0)
1247 goto out;
1248
1249 if (is_my_mac(tt_query->dst))
1250 handle_tt_response(bat_priv, tt_query);
1251 else {
1252 bat_dbg(DBG_TT, bat_priv,
1253 "Routing TT_RESPONSE to %pM [%c]\n",
1254 tt_query->dst,
1255 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1256 tt_query->tt_data = htons(tt_query->tt_data);
1257 return route_unicast_packet(skb, recv_if);
1258 }
1259 break;
1260 }
1261
1262out:
1263 /* returning NET_RX_DROP will make the caller function kfree the skb */
1264 return NET_RX_DROP;
1265}
1266
Antonio Quartullicc47f662011-04-27 14:27:57 +02001267int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1268{
1269 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1270 struct roam_adv_packet *roam_adv_packet;
1271 struct orig_node *orig_node;
1272 struct ethhdr *ethhdr;
1273
1274 /* drop packet if it has not necessary minimum size */
1275 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1276 goto out;
1277
1278 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1279
1280 /* packet with unicast indication but broadcast recipient */
1281 if (is_broadcast_ether_addr(ethhdr->h_dest))
1282 goto out;
1283
1284 /* packet with broadcast sender address */
1285 if (is_broadcast_ether_addr(ethhdr->h_source))
1286 goto out;
1287
1288 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1289
1290 if (!is_my_mac(roam_adv_packet->dst))
1291 return route_unicast_packet(skb, recv_if);
1292
1293 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1294 if (!orig_node)
1295 goto out;
1296
1297 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1298 "(client %pM)\n", roam_adv_packet->src,
1299 roam_adv_packet->client);
1300
1301 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
Antonio Quartullibc279082011-07-07 15:35:35 +02001302 atomic_read(&orig_node->last_ttvn) + 1, true, false);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001303
1304 /* Roaming phase starts: I have new information but the ttvn has not
1305 * been incremented yet. This flag will make me check all the incoming
1306 * packets for the correct destination. */
1307 bat_priv->tt_poss_change = true;
1308
1309 orig_node_free_ref(orig_node);
1310out:
1311 /* returning NET_RX_DROP will make the caller function kfree the skb */
1312 return NET_RX_DROP;
1313}
1314
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001315/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001316 * bonding if possible. increases the found neighbors
1317 * refcount.*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001318struct neigh_node *find_router(struct bat_priv *bat_priv,
1319 struct orig_node *orig_node,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001320 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001321{
1322 struct orig_node *primary_orig_node;
1323 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +00001324 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001325 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1326 int bonding_enabled;
1327
1328 if (!orig_node)
1329 return NULL;
1330
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001331 router = orig_node_get_router(orig_node);
1332 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +02001333 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001334
1335 /* without bonding, the first node should
1336 * always choose the default router. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001337 bonding_enabled = atomic_read(&bat_priv->bonding);
1338
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001339 rcu_read_lock();
1340 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001341 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +02001342 if (!router_orig)
1343 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001344
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001345 if ((!recv_if) && (!bonding_enabled))
1346 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347
1348 /* if we have something in the primary_addr, we can search
1349 * for a potential bonding candidate. */
Marek Lindner39901e72011-02-18 12:28:08 +00001350 if (compare_eth(router_orig->primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001351 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001352
1353 /* find the orig_node which has the primary interface. might
1354 * even be the same as our router_orig in many cases */
1355
Marek Lindner39901e72011-02-18 12:28:08 +00001356 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001357 primary_orig_node = router_orig;
1358 } else {
Marek Lindner7aadf882011-02-18 12:28:09 +00001359 primary_orig_node = orig_hash_find(bat_priv,
1360 router_orig->primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001361 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001362 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +00001363
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001364 orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001365 }
1366
1367 /* with less than 2 candidates, we can't do any
1368 * bonding and prefer the original router. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001369 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1370 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001371
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001372 /* all nodes between should choose a candidate which
1373 * is is not on the interface where the packet came
1374 * in. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001375
Marek Lindner44524fc2011-02-10 14:33:53 +00001376 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001377
Linus Lüssing55158622011-03-14 22:43:27 +00001378 if (bonding_enabled)
1379 router = find_bond_router(primary_orig_node, recv_if);
1380 else
1381 router = find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001382
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001383return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +02001384 if (router && router->if_incoming->if_status != IF_ACTIVE)
1385 goto err_unlock;
1386
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001387 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001388 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +02001389err_unlock:
1390 rcu_read_unlock();
1391err:
1392 if (router)
1393 neigh_node_free_ref(router);
1394 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001395}
1396
1397static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1398{
1399 struct ethhdr *ethhdr;
1400
1401 /* drop packet if it has not necessary minimum size */
1402 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1403 return -1;
1404
1405 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1406
1407 /* packet with unicast indication but broadcast recipient */
1408 if (is_broadcast_ether_addr(ethhdr->h_dest))
1409 return -1;
1410
1411 /* packet with broadcast sender address */
1412 if (is_broadcast_ether_addr(ethhdr->h_source))
1413 return -1;
1414
1415 /* not for me */
1416 if (!is_my_mac(ethhdr->h_dest))
1417 return -1;
1418
1419 return 0;
1420}
1421
Linus Lüssing7cefb142011-03-02 17:39:31 +00001422int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001423{
1424 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +00001425 struct orig_node *orig_node = NULL;
1426 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001427 struct unicast_packet *unicast_packet;
1428 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +00001429 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001430 struct sk_buff *new_skb;
1431
1432 unicast_packet = (struct unicast_packet *)skb->data;
1433
1434 /* TTL exceeded */
1435 if (unicast_packet->ttl < 2) {
1436 pr_debug("Warning - can't forward unicast packet from %pM to "
1437 "%pM: ttl exceeded\n", ethhdr->h_source,
1438 unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +00001439 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001440 }
1441
1442 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +00001443 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1444
Marek Lindner44524fc2011-02-10 14:33:53 +00001445 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001446 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001447
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001448 /* find_router() increases neigh_nodes refcount if found. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001449 neigh_node = find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001450
Marek Lindnerd0072602011-01-19 20:01:44 +00001451 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +00001452 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001453
1454 /* create a copy of the skb, if needed, to modify it. */
1455 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +00001456 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001457
1458 unicast_packet = (struct unicast_packet *)skb->data;
1459
1460 if (unicast_packet->packet_type == BAT_UNICAST &&
1461 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +00001462 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1463 ret = frag_send_skb(skb, bat_priv,
1464 neigh_node->if_incoming, neigh_node->addr);
1465 goto out;
1466 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001467
1468 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
Marek Lindnerd0072602011-01-19 20:01:44 +00001469 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001470
1471 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1472
1473 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +00001474 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001475
1476 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +00001477 if (!new_skb) {
1478 ret = NET_RX_SUCCESS;
1479 goto out;
1480 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001481
1482 skb = new_skb;
1483 unicast_packet = (struct unicast_packet *)skb->data;
1484 }
1485
1486 /* decrement ttl */
1487 unicast_packet->ttl--;
1488
1489 /* route it */
Marek Lindnerd0072602011-01-19 20:01:44 +00001490 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001491 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001492
Marek Lindner44524fc2011-02-10 14:33:53 +00001493out:
1494 if (neigh_node)
1495 neigh_node_free_ref(neigh_node);
1496 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001497 orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +00001498 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001499}
1500
Antonio Quartullia73105b2011-04-27 14:27:44 +02001501static int check_unicast_ttvn(struct bat_priv *bat_priv,
1502 struct sk_buff *skb) {
1503 uint8_t curr_ttvn;
1504 struct orig_node *orig_node;
1505 struct ethhdr *ethhdr;
1506 struct hard_iface *primary_if;
1507 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001508 bool tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001509
1510 /* I could need to modify it */
1511 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1512 return 0;
1513
1514 unicast_packet = (struct unicast_packet *)skb->data;
1515
Antonio Quartullicc47f662011-04-27 14:27:57 +02001516 if (is_my_mac(unicast_packet->dest)) {
1517 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001518 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001519 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001520 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1521
1522 if (!orig_node)
1523 return 0;
1524
1525 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001526 tt_poss_change = orig_node->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527 orig_node_free_ref(orig_node);
1528 }
1529
1530 /* Check whether I have to reroute the packet */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001531 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001532 /* Linearize the skb before accessing it */
1533 if (skb_linearize(skb) < 0)
1534 return 0;
1535
1536 ethhdr = (struct ethhdr *)(skb->data +
1537 sizeof(struct unicast_packet));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001538 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
1539
1540 if (!orig_node) {
1541 if (!is_my_client(bat_priv, ethhdr->h_dest))
1542 return 0;
1543 primary_if = primary_if_get_selected(bat_priv);
1544 if (!primary_if)
1545 return 0;
1546 memcpy(unicast_packet->dest,
1547 primary_if->net_dev->dev_addr, ETH_ALEN);
1548 hardif_free_ref(primary_if);
1549 } else {
1550 memcpy(unicast_packet->dest, orig_node->orig,
1551 ETH_ALEN);
1552 curr_ttvn = (uint8_t)
1553 atomic_read(&orig_node->last_ttvn);
1554 orig_node_free_ref(orig_node);
1555 }
1556
1557 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1558 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1559 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1560 ethhdr->h_dest, unicast_packet->dest);
1561
1562 unicast_packet->ttvn = curr_ttvn;
1563 }
1564 return 1;
1565}
1566
Marek Lindnere6c10f42011-02-18 12:33:20 +00001567int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001568{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001569 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001570 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001571 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001572
1573 if (check_unicast_packet(skb, hdr_size) < 0)
1574 return NET_RX_DROP;
1575
Antonio Quartullia73105b2011-04-27 14:27:44 +02001576 if (!check_unicast_ttvn(bat_priv, skb))
1577 return NET_RX_DROP;
1578
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001579 unicast_packet = (struct unicast_packet *)skb->data;
1580
1581 /* packet for me */
1582 if (is_my_mac(unicast_packet->dest)) {
1583 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1584 return NET_RX_SUCCESS;
1585 }
1586
Linus Lüssing7cefb142011-03-02 17:39:31 +00001587 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001588}
1589
Marek Lindnere6c10f42011-02-18 12:33:20 +00001590int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001591{
1592 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1593 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001594 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001595 struct sk_buff *new_skb = NULL;
1596 int ret;
1597
1598 if (check_unicast_packet(skb, hdr_size) < 0)
1599 return NET_RX_DROP;
1600
Antonio Quartullia73105b2011-04-27 14:27:44 +02001601 if (!check_unicast_ttvn(bat_priv, skb))
1602 return NET_RX_DROP;
1603
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001604 unicast_packet = (struct unicast_frag_packet *)skb->data;
1605
1606 /* packet for me */
1607 if (is_my_mac(unicast_packet->dest)) {
1608
1609 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1610
1611 if (ret == NET_RX_DROP)
1612 return NET_RX_DROP;
1613
1614 /* packet was buffered for late merge */
1615 if (!new_skb)
1616 return NET_RX_SUCCESS;
1617
1618 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1619 sizeof(struct unicast_packet));
1620 return NET_RX_SUCCESS;
1621 }
1622
Linus Lüssing7cefb142011-03-02 17:39:31 +00001623 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001624}
1625
1626
Marek Lindnere6c10f42011-02-18 12:33:20 +00001627int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001628{
1629 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001630 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001631 struct bcast_packet *bcast_packet;
1632 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001633 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001634 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001635 int32_t seq_diff;
1636
1637 /* drop packet if it has not necessary minimum size */
1638 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001639 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001640
1641 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1642
1643 /* packet with broadcast indication but unicast recipient */
1644 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001645 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001646
1647 /* packet with broadcast sender address */
1648 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001649 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001650
1651 /* ignore broadcasts sent by myself */
1652 if (is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001653 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001654
1655 bcast_packet = (struct bcast_packet *)skb->data;
1656
1657 /* ignore broadcasts originated by myself */
1658 if (is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001659 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001660
1661 if (bcast_packet->ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001662 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001663
Marek Lindner7aadf882011-02-18 12:28:09 +00001664 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001665
1666 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001667 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001668
Marek Lindnerf3e00082011-01-25 21:52:11 +00001669 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001670
1671 /* check whether the packet is a duplicate */
Marek Lindnerf3e00082011-01-25 21:52:11 +00001672 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1673 ntohl(bcast_packet->seqno)))
1674 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001675
1676 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1677
1678 /* check whether the packet is old and the host just restarted. */
1679 if (window_protected(bat_priv, seq_diff,
Marek Lindnerf3e00082011-01-25 21:52:11 +00001680 &orig_node->bcast_seqno_reset))
1681 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001682
1683 /* mark broadcast in flood history, update window position
1684 * if required. */
1685 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1686 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1687
Marek Lindnerf3e00082011-01-25 21:52:11 +00001688 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001689
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001690 /* rebroadcast packet */
Antonio Quartulli86985292011-06-25 19:09:12 +02001691 add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001692
1693 /* broadcast for me */
1694 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001695 ret = NET_RX_SUCCESS;
1696 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001697
Marek Lindnerf3e00082011-01-25 21:52:11 +00001698spin_unlock:
1699 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001700out:
1701 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001702 orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001703 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001704}
1705
Marek Lindnere6c10f42011-02-18 12:33:20 +00001706int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001707{
1708 struct vis_packet *vis_packet;
1709 struct ethhdr *ethhdr;
1710 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001711 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001712
1713 /* keep skb linear */
1714 if (skb_linearize(skb) < 0)
1715 return NET_RX_DROP;
1716
1717 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1718 return NET_RX_DROP;
1719
1720 vis_packet = (struct vis_packet *)skb->data;
1721 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1722
1723 /* not for me */
1724 if (!is_my_mac(ethhdr->h_dest))
1725 return NET_RX_DROP;
1726
1727 /* ignore own packets */
1728 if (is_my_mac(vis_packet->vis_orig))
1729 return NET_RX_DROP;
1730
1731 if (is_my_mac(vis_packet->sender_orig))
1732 return NET_RX_DROP;
1733
1734 switch (vis_packet->vis_type) {
1735 case VIS_TYPE_SERVER_SYNC:
1736 receive_server_sync_packet(bat_priv, vis_packet,
1737 skb_headlen(skb));
1738 break;
1739
1740 case VIS_TYPE_CLIENT_UPDATE:
1741 receive_client_update_packet(bat_priv, vis_packet,
1742 skb_headlen(skb));
1743 break;
1744
1745 default: /* ignore unknown packet */
1746 break;
1747 }
1748
1749 /* We take a copy of the data in the packet, so we should
1750 always free the skbuf. */
1751 return NET_RX_DROP;
1752}