blob: 2cb98bed15862db6ab1e8f2632af1442e2736f1d [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) {
78 /* the OGM could not contain the changes because they were too
79 * many to fit in one frame or because they have already been
80 * sent TT_OGM_APPEND_MAX times. In this case send a tt
81 * request */
82 if (!tt_num_changes) {
83 full_table = false;
84 goto request_table;
85 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086
Antonio Quartullia73105b2011-04-27 14:27:44 +020087 tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
88 (struct tt_change *)tt_buff);
89
90 /* Even if we received the crc into the OGM, we prefer
91 * to recompute it to spot any possible inconsistency
92 * in the global table */
Antonio Quartullia73105b2011-04-27 14:27:44 +020093 orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
Antonio Quartullicc47f662011-04-27 14:27:57 +020094 /* Roaming phase is over: tables are in sync again. I can
95 * unset the flag */
96 orig_node->tt_poss_change = false;
Antonio Quartullia73105b2011-04-27 14:27:44 +020097 } else {
98 /* if we missed more than one change or our tables are not
99 * in sync anymore -> request fresh tt data */
100 if (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
101request_table:
102 bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
103 "Need to retrieve the correct information "
104 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
105 "%u num_changes: %u)\n", orig_node->orig, ttvn,
106 orig_ttvn, tt_crc, orig_node->tt_crc,
107 tt_num_changes);
108 send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
109 full_table);
110 return;
111 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112 }
113}
114
Antonio Quartullia73105b2011-04-27 14:27:44 +0200115static void update_route(struct bat_priv *bat_priv,
116 struct orig_node *orig_node,
117 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000119 struct neigh_node *curr_router;
120
121 curr_router = orig_node_get_router(orig_node);
Marek Lindnera8e7f4b2010-12-12 21:57:10 +0000122
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123 /* route deleted */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000124 if ((curr_router) && (!neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
126 orig_node->orig);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200127 tt_global_del_orig(bat_priv, orig_node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200128 "Deleted route towards originator");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000130 /* route added */
131 } else if ((!curr_router) && (neigh_node)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132
133 bat_dbg(DBG_ROUTES, bat_priv,
134 "Adding route towards: %pM (via %pM)\n",
135 orig_node->orig, neigh_node->addr);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000136 /* route changed */
Sven Eckelmannbb899b82011-05-10 11:22:37 +0200137 } else if (neigh_node && curr_router) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138 bat_dbg(DBG_ROUTES, bat_priv,
139 "Changing route towards: %pM "
140 "(now via %pM - was via %pM)\n",
141 orig_node->orig, neigh_node->addr,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000142 curr_router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143 }
144
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000145 if (curr_router)
146 neigh_node_free_ref(curr_router);
147
148 /* increase refcount of new best neighbor */
Marek Lindner44524fc2011-02-10 14:33:53 +0000149 if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
150 neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000151
152 spin_lock_bh(&orig_node->neigh_list_lock);
153 rcu_assign_pointer(orig_node->router, neigh_node);
154 spin_unlock_bh(&orig_node->neigh_list_lock);
155
156 /* decrease refcount of previous best neighbor */
157 if (curr_router)
158 neigh_node_free_ref(curr_router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159}
160
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200162 struct neigh_node *neigh_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000164 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165
166 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000167 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000169 router = orig_node_get_router(orig_node);
170
171 if (router != neigh_node)
Antonio Quartullia73105b2011-04-27 14:27:44 +0200172 update_route(bat_priv, orig_node, neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000173
174out:
175 if (router)
176 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177}
178
179static int is_bidirectional_neigh(struct orig_node *orig_node,
180 struct orig_node *orig_neigh_node,
181 struct batman_packet *batman_packet,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000182 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000183{
184 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindner1605d0d2011-02-18 12:28:11 +0000185 struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000186 struct hlist_node *node;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200187 uint8_t total_count;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000188 uint8_t orig_eq_count, neigh_rq_count, tq_own;
189 int tq_asym_penalty, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190
Daniele Furlan27aea212011-05-07 22:45:19 +0200191 /* find corresponding one hop neighbor */
192 rcu_read_lock();
193 hlist_for_each_entry_rcu(tmp_neigh_node, node,
194 &orig_neigh_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195
Daniele Furlan27aea212011-05-07 22:45:19 +0200196 if (!compare_eth(tmp_neigh_node->addr, orig_neigh_node->orig))
197 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000198
Daniele Furlan27aea212011-05-07 22:45:19 +0200199 if (tmp_neigh_node->if_incoming != if_incoming)
200 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000201
Daniele Furlan27aea212011-05-07 22:45:19 +0200202 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
203 continue;
Marek Lindner1605d0d2011-02-18 12:28:11 +0000204
Daniele Furlan27aea212011-05-07 22:45:19 +0200205 neigh_node = tmp_neigh_node;
206 break;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 }
Daniele Furlan27aea212011-05-07 22:45:19 +0200208 rcu_read_unlock();
209
210 if (!neigh_node)
211 neigh_node = create_neighbor(orig_neigh_node,
212 orig_neigh_node,
213 orig_neigh_node->orig,
214 if_incoming);
215
216 if (!neigh_node)
217 goto out;
218
219 /* if orig_node is direct neighbour update neigh_node last_valid */
220 if (orig_node == orig_neigh_node)
221 neigh_node->last_valid = jiffies;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
223 orig_node->last_valid = jiffies;
224
Daniele Furlan27aea212011-05-07 22:45:19 +0200225 /* find packet count of corresponding one hop neighbor */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000226 spin_lock_bh(&orig_node->ogm_cnt_lock);
227 orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
228 neigh_rq_count = neigh_node->real_packet_count;
229 spin_unlock_bh(&orig_node->ogm_cnt_lock);
230
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231 /* pay attention to not get a value bigger than 100 % */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000232 total_count = (orig_eq_count > neigh_rq_count ?
233 neigh_rq_count : orig_eq_count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000234
235 /* if we have too few packets (too less data) we set tq_own to zero */
236 /* if we receive too few packets it is not considered bidirectional */
237 if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
Marek Lindner0ede9f42011-01-25 21:52:10 +0000238 (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
239 tq_own = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 else
241 /* neigh_node->real_packet_count is never zero as we
242 * only purge old information when getting new
243 * information */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000244 tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
246 /*
247 * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
248 * affect the nearly-symmetric links only a little, but
249 * punishes asymmetric links more. This will give a value
250 * between 0 and TQ_MAX_VALUE
251 */
Marek Lindner0ede9f42011-01-25 21:52:10 +0000252 tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
253 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
254 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
255 (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
256 (TQ_LOCAL_WINDOW_SIZE *
257 TQ_LOCAL_WINDOW_SIZE *
258 TQ_LOCAL_WINDOW_SIZE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
Marek Lindner0ede9f42011-01-25 21:52:10 +0000260 batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
261 (TQ_MAX_VALUE * TQ_MAX_VALUE));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262
263 bat_dbg(DBG_BATMAN, bat_priv,
264 "bidirectional: "
265 "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
266 "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
267 "total tq: %3i\n",
268 orig_node->orig, orig_neigh_node->orig, total_count,
Marek Lindner0ede9f42011-01-25 21:52:10 +0000269 neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270
271 /* if link has the minimum required transmission quality
272 * consider it bidirectional */
273 if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
Marek Lindnera775eb82011-01-19 20:01:39 +0000274 ret = 1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275
Marek Lindnera775eb82011-01-19 20:01:39 +0000276out:
277 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000278 neigh_node_free_ref(neigh_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000279 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280}
281
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000282/* caller must hold the neigh_list_lock */
283void bonding_candidate_del(struct orig_node *orig_node,
284 struct neigh_node *neigh_node)
285{
286 /* this neighbor is not part of our candidate list */
287 if (list_empty(&neigh_node->bonding_list))
288 goto out;
289
290 list_del_rcu(&neigh_node->bonding_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000291 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner44524fc2011-02-10 14:33:53 +0000292 neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000293 atomic_dec(&orig_node->bond_candidates);
294
295out:
296 return;
297}
298
299static void bonding_candidate_add(struct orig_node *orig_node,
300 struct neigh_node *neigh_node)
301{
302 struct hlist_node *node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000303 struct neigh_node *tmp_neigh_node, *router = NULL;
304 uint8_t interference_candidate = 0;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000305
306 spin_lock_bh(&orig_node->neigh_list_lock);
307
308 /* only consider if it has the same primary address ... */
Marek Lindner39901e72011-02-18 12:28:08 +0000309 if (!compare_eth(orig_node->orig,
310 neigh_node->orig_node->primary_addr))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000311 goto candidate_del;
312
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000313 router = orig_node_get_router(orig_node);
314 if (!router)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000315 goto candidate_del;
316
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000317 /* ... and is good enough to be considered */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000318 if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000319 goto candidate_del;
320
321 /**
322 * check if we have another candidate with the same mac address or
323 * interface. If we do, we won't select this candidate because of
324 * possible interference.
325 */
326 hlist_for_each_entry_rcu(tmp_neigh_node, node,
327 &orig_node->neigh_list, list) {
328
329 if (tmp_neigh_node == neigh_node)
330 continue;
331
332 /* we only care if the other candidate is even
333 * considered as candidate. */
334 if (list_empty(&tmp_neigh_node->bonding_list))
335 continue;
336
337 if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
Marek Lindner39901e72011-02-18 12:28:08 +0000338 (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000339 interference_candidate = 1;
340 break;
341 }
342 }
343
344 /* don't care further if it is an interference candidate */
345 if (interference_candidate)
346 goto candidate_del;
347
348 /* this neighbor already is part of our candidate list */
349 if (!list_empty(&neigh_node->bonding_list))
350 goto out;
351
Marek Lindner44524fc2011-02-10 14:33:53 +0000352 if (!atomic_inc_not_zero(&neigh_node->refcount))
353 goto out;
354
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000355 list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000356 atomic_inc(&orig_node->bond_candidates);
357 goto out;
358
359candidate_del:
360 bonding_candidate_del(orig_node, neigh_node);
361
362out:
363 spin_unlock_bh(&orig_node->neigh_list_lock);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000364
365 if (router)
366 neigh_node_free_ref(router);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000367}
368
369/* copy primary address for bonding */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200370static void bonding_save_primary(const struct orig_node *orig_node,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000371 struct orig_node *orig_neigh_node,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200372 const struct batman_packet *batman_packet)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000373{
374 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
375 return;
376
377 memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
378}
379
Sven Eckelmann747e4222011-05-14 23:14:50 +0200380static void update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
381 const struct ethhdr *ethhdr,
382 const struct batman_packet *batman_packet,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000383 struct hard_iface *if_incoming,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200384 const unsigned char *tt_buff, int is_duplicate)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385{
386 struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000387 struct neigh_node *router = NULL;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000388 struct orig_node *orig_node_tmp;
Marek Lindner9591a792010-12-12 21:57:11 +0000389 struct hlist_node *node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000390 uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
392 bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
393 "Searching and updating originator entry of received packet\n");
394
Marek Lindnerf987ed62010-12-12 21:57:12 +0000395 rcu_read_lock();
396 hlist_for_each_entry_rcu(tmp_neigh_node, node,
397 &orig_node->neigh_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000398 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Marek Lindner44524fc2011-02-10 14:33:53 +0000399 (tmp_neigh_node->if_incoming == if_incoming) &&
400 atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
401 if (neigh_node)
402 neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 neigh_node = tmp_neigh_node;
404 continue;
405 }
406
407 if (is_duplicate)
408 continue;
409
Linus Lüssing68003902011-03-14 22:43:40 +0000410 spin_lock_bh(&tmp_neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411 ring_buffer_set(tmp_neigh_node->tq_recv,
412 &tmp_neigh_node->tq_index, 0);
413 tmp_neigh_node->tq_avg =
414 ring_buffer_avg(tmp_neigh_node->tq_recv);
Linus Lüssing68003902011-03-14 22:43:40 +0000415 spin_unlock_bh(&tmp_neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 }
417
418 if (!neigh_node) {
419 struct orig_node *orig_tmp;
420
421 orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
422 if (!orig_tmp)
Marek Lindnera775eb82011-01-19 20:01:39 +0000423 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000424
425 neigh_node = create_neighbor(orig_node, orig_tmp,
426 ethhdr->h_source, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000427
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000428 orig_node_free_ref(orig_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 if (!neigh_node)
Marek Lindnera775eb82011-01-19 20:01:39 +0000430 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 } else
432 bat_dbg(DBG_BATMAN, bat_priv,
433 "Updating existing last-hop neighbor of originator\n");
434
Marek Lindnera775eb82011-01-19 20:01:39 +0000435 rcu_read_unlock();
436
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 orig_node->flags = batman_packet->flags;
438 neigh_node->last_valid = jiffies;
439
Linus Lüssing68003902011-03-14 22:43:40 +0000440 spin_lock_bh(&neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441 ring_buffer_set(neigh_node->tq_recv,
442 &neigh_node->tq_index,
443 batman_packet->tq);
444 neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
Linus Lüssing68003902011-03-14 22:43:40 +0000445 spin_unlock_bh(&neigh_node->tq_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446
447 if (!is_duplicate) {
448 orig_node->last_ttl = batman_packet->ttl;
449 neigh_node->last_ttl = batman_packet->ttl;
450 }
451
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000452 bonding_candidate_add(orig_node, neigh_node);
453
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000454 /* if this neighbor already is our next hop there is nothing
455 * to change */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000456 router = orig_node_get_router(orig_node);
457 if (router == neigh_node)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200458 goto update_tt;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
460 /* if this neighbor does not offer a better TQ we won't consider it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000461 if (router && (router->tq_avg > neigh_node->tq_avg))
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200462 goto update_tt;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000463
464 /* if the TQ is the same and the link not more symetric we
465 * won't consider it either */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000466 if (router && (neigh_node->tq_avg == router->tq_avg)) {
467 orig_node_tmp = router->orig_node;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000468 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
469 bcast_own_sum_orig =
470 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
471 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
472
473 orig_node_tmp = neigh_node->orig_node;
474 spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
475 bcast_own_sum_neigh =
476 orig_node_tmp->bcast_own_sum[if_incoming->if_num];
477 spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
478
479 if (bcast_own_sum_orig >= bcast_own_sum_neigh)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200480 goto update_tt;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000481 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482
Antonio Quartullia73105b2011-04-27 14:27:44 +0200483 update_routes(bat_priv, orig_node, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200485update_tt:
Antonio Quartullia73105b2011-04-27 14:27:44 +0200486 /* I have to check for transtable changes only if the OGM has been
487 * sent through a primary interface */
488 if (((batman_packet->orig != ethhdr->h_source) &&
489 (batman_packet->ttl > 2)) ||
490 (batman_packet->flags & PRIMARIES_FIRST_HOP))
491 update_transtable(bat_priv, orig_node, tt_buff,
492 batman_packet->tt_num_changes,
493 batman_packet->ttvn,
494 batman_packet->tt_crc);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000496 if (orig_node->gw_flags != batman_packet->gw_flags)
497 gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
498
499 orig_node->gw_flags = batman_packet->gw_flags;
500
501 /* restart gateway selection if fast or late switching was enabled */
502 if ((orig_node->gw_flags) &&
503 (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
504 (atomic_read(&bat_priv->gw_sel_class) > 2))
505 gw_check_election(bat_priv, orig_node);
Marek Lindnera775eb82011-01-19 20:01:39 +0000506
507 goto out;
508
509unlock:
510 rcu_read_unlock();
511out:
512 if (neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +0000513 neigh_node_free_ref(neigh_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000514 if (router)
515 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000516}
517
518/* checks whether the host restarted and is in the protection time.
519 * returns:
520 * 0 if the packet is to be accepted
521 * 1 if the packet is to be ignored.
522 */
523static int window_protected(struct bat_priv *bat_priv,
524 int32_t seq_num_diff,
525 unsigned long *last_reset)
526{
527 if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
528 || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
529 if (time_after(jiffies, *last_reset +
530 msecs_to_jiffies(RESET_PROTECTION_MS))) {
531
532 *last_reset = jiffies;
533 bat_dbg(DBG_BATMAN, bat_priv,
534 "old packet received, start protection\n");
535
536 return 0;
537 } else
538 return 1;
539 }
540 return 0;
541}
542
543/* processes a batman packet for all interfaces, adjusts the sequence number and
544 * finds out whether it is a duplicate.
545 * returns:
546 * 1 the packet is a duplicate
547 * 0 the packet has not yet been received
548 * -1 the packet is old and has been received while the seqno window
549 * was protected. Caller should drop it.
550 */
David Howellsb2c44a52011-06-15 09:41:36 +0200551static int count_real_packets(const struct ethhdr *ethhdr,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200552 const struct batman_packet *batman_packet,
553 const struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554{
555 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
556 struct orig_node *orig_node;
557 struct neigh_node *tmp_neigh_node;
Marek Lindner9591a792010-12-12 21:57:11 +0000558 struct hlist_node *node;
David Howellsb2c44a52011-06-15 09:41:36 +0200559 int is_duplicate = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 int32_t seq_diff;
561 int need_update = 0;
Marek Lindner0ede9f42011-01-25 21:52:10 +0000562 int set_mark, ret = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563
564 orig_node = get_orig_node(bat_priv, batman_packet->orig);
565 if (!orig_node)
566 return 0;
567
Marek Lindner0ede9f42011-01-25 21:52:10 +0000568 spin_lock_bh(&orig_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
570
571 /* signalize caller that the packet is to be dropped. */
572 if (window_protected(bat_priv, seq_diff,
573 &orig_node->batman_seqno_reset))
Marek Lindner0ede9f42011-01-25 21:52:10 +0000574 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000575
Marek Lindnerf987ed62010-12-12 21:57:12 +0000576 rcu_read_lock();
577 hlist_for_each_entry_rcu(tmp_neigh_node, node,
578 &orig_node->neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
580 is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
581 orig_node->last_real_seqno,
582 batman_packet->seqno);
583
Marek Lindner39901e72011-02-18 12:28:08 +0000584 if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585 (tmp_neigh_node->if_incoming == if_incoming))
586 set_mark = 1;
587 else
588 set_mark = 0;
589
590 /* if the window moved, set the update flag. */
591 need_update |= bit_get_packet(bat_priv,
592 tmp_neigh_node->real_bits,
593 seq_diff, set_mark);
594
595 tmp_neigh_node->real_packet_count =
596 bit_packet_count(tmp_neigh_node->real_bits);
597 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000598 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000599
600 if (need_update) {
601 bat_dbg(DBG_BATMAN, bat_priv,
602 "updating last_seqno: old %d, new %d\n",
603 orig_node->last_real_seqno, batman_packet->seqno);
604 orig_node->last_real_seqno = batman_packet->seqno;
605 }
606
Marek Lindner0ede9f42011-01-25 21:52:10 +0000607 ret = is_duplicate;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000608
Marek Lindner0ede9f42011-01-25 21:52:10 +0000609out:
610 spin_unlock_bh(&orig_node->ogm_cnt_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000611 orig_node_free_ref(orig_node);
Marek Lindner0ede9f42011-01-25 21:52:10 +0000612 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000613}
614
Sven Eckelmann747e4222011-05-14 23:14:50 +0200615void receive_bat_packet(const struct ethhdr *ethhdr,
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000616 struct batman_packet *batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200617 const unsigned char *tt_buff,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000618 struct hard_iface *if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619{
620 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000621 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 struct orig_node *orig_neigh_node, *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000623 struct neigh_node *router = NULL, *router_router = NULL;
624 struct neigh_node *orig_neigh_router = NULL;
Sven Eckelmannb4e17052011-06-15 09:41:37 +0200625 int has_directlink_flag;
626 int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
627 int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
David Howellsb2c44a52011-06-15 09:41:36 +0200628 int is_duplicate;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 uint32_t if_incoming_seqno;
630
631 /* Silently drop when the batman packet is actually not a
632 * correct packet.
633 *
634 * This might happen if a packet is padded (e.g. Ethernet has a
635 * minimum frame length of 64 byte) and the aggregation interprets
636 * it as an additional length.
637 *
638 * TODO: A more sane solution would be to have a bit in the
639 * batman_packet to detect whether the packet is the last
640 * packet in an aggregation. Here we expect that the padding
641 * is always zero (or not 0x01)
642 */
643 if (batman_packet->packet_type != BAT_PACKET)
644 return;
645
646 /* could be changed by schedule_own_packet() */
647 if_incoming_seqno = atomic_read(&if_incoming->seqno);
648
649 has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
650
Marek Lindner39901e72011-02-18 12:28:08 +0000651 is_single_hop_neigh = (compare_eth(ethhdr->h_source,
652 batman_packet->orig) ? 1 : 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653
654 bat_dbg(DBG_BATMAN, bat_priv,
655 "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
Antonio Quartullia73105b2011-04-27 14:27:44 +0200656 "(from OG: %pM, via prev OG: %pM, seqno %d, ttvn %u, "
657 "crc %u, changes %u, td %d, TTL %d, V %d, IDF %d)\n",
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658 ethhdr->h_source, if_incoming->net_dev->name,
659 if_incoming->net_dev->dev_addr, batman_packet->orig,
660 batman_packet->prev_sender, batman_packet->seqno,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200661 batman_packet->ttvn, batman_packet->tt_crc,
662 batman_packet->tt_num_changes, batman_packet->tq,
663 batman_packet->ttl, batman_packet->version,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664 has_directlink_flag);
665
666 rcu_read_lock();
Marek Lindnere6c10f42011-02-18 12:33:20 +0000667 list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
668 if (hard_iface->if_status != IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 continue;
670
Marek Lindnere6c10f42011-02-18 12:33:20 +0000671 if (hard_iface->soft_iface != if_incoming->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672 continue;
673
Marek Lindner39901e72011-02-18 12:28:08 +0000674 if (compare_eth(ethhdr->h_source,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000675 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676 is_my_addr = 1;
677
Marek Lindner39901e72011-02-18 12:28:08 +0000678 if (compare_eth(batman_packet->orig,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000679 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 is_my_orig = 1;
681
Marek Lindner39901e72011-02-18 12:28:08 +0000682 if (compare_eth(batman_packet->prev_sender,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000683 hard_iface->net_dev->dev_addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684 is_my_oldorig = 1;
685
Antonio Quartulli44e92bc2011-05-21 01:33:07 +0200686 if (is_broadcast_ether_addr(ethhdr->h_source))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687 is_broadcast = 1;
688 }
689 rcu_read_unlock();
690
691 if (batman_packet->version != COMPAT_VERSION) {
692 bat_dbg(DBG_BATMAN, bat_priv,
693 "Drop packet: incompatible batman version (%i)\n",
694 batman_packet->version);
695 return;
696 }
697
698 if (is_my_addr) {
699 bat_dbg(DBG_BATMAN, bat_priv,
700 "Drop packet: received my own broadcast (sender: %pM"
701 ")\n",
702 ethhdr->h_source);
703 return;
704 }
705
706 if (is_broadcast) {
707 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
708 "ignoring all packets with broadcast source addr (sender: %pM"
709 ")\n", ethhdr->h_source);
710 return;
711 }
712
713 if (is_my_orig) {
714 unsigned long *word;
715 int offset;
716
717 orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 if (!orig_neigh_node)
719 return;
720
721 /* neighbor has to indicate direct link and it has to
722 * come via the corresponding interface */
Daniele Furland1829fa2011-06-07 00:45:55 +0200723 /* save packet seqno for bidirectional check */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000724 if (has_directlink_flag &&
Marek Lindner39901e72011-02-18 12:28:08 +0000725 compare_eth(if_incoming->net_dev->dev_addr,
Daniele Furland1829fa2011-06-07 00:45:55 +0200726 batman_packet->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727 offset = if_incoming->if_num * NUM_WORDS;
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000728
729 spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 word = &(orig_neigh_node->bcast_own[offset]);
Daniele Furland1829fa2011-06-07 00:45:55 +0200731 bit_mark(word,
732 if_incoming_seqno - batman_packet->seqno - 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
734 bit_packet_count(word);
Marek Lindner2ae2daf2011-01-19 20:01:42 +0000735 spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 }
737
738 bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
739 "originator packet from myself (via neighbor)\n");
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000740 orig_node_free_ref(orig_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 return;
742 }
743
744 if (is_my_oldorig) {
745 bat_dbg(DBG_BATMAN, bat_priv,
746 "Drop packet: ignoring all rebroadcast echos (sender: "
747 "%pM)\n", ethhdr->h_source);
748 return;
749 }
750
751 orig_node = get_orig_node(bat_priv, batman_packet->orig);
752 if (!orig_node)
753 return;
754
755 is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
756
757 if (is_duplicate == -1) {
758 bat_dbg(DBG_BATMAN, bat_priv,
759 "Drop packet: packet within seqno protection time "
760 "(sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000761 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 }
763
764 if (batman_packet->tq == 0) {
765 bat_dbg(DBG_BATMAN, bat_priv,
766 "Drop packet: originator packet with tq equal 0\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000767 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000768 }
769
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000770 router = orig_node_get_router(orig_node);
771 if (router)
772 router_router = orig_node_get_router(router->orig_node);
773
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774 /* avoid temporary routing loops */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000775 if (router && router_router &&
776 (compare_eth(router->addr, batman_packet->prev_sender)) &&
Marek Lindner39901e72011-02-18 12:28:08 +0000777 !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000778 (compare_eth(router->addr, router_router->addr))) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 bat_dbg(DBG_BATMAN, bat_priv,
780 "Drop packet: ignoring all rebroadcast packets that "
781 "may make me loop (sender: %pM)\n", ethhdr->h_source);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000782 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 }
784
785 /* if sender is a direct neighbor the sender mac equals
786 * originator mac */
787 orig_neigh_node = (is_single_hop_neigh ?
788 orig_node :
789 get_orig_node(bat_priv, ethhdr->h_source));
790 if (!orig_neigh_node)
Marek Lindnerd0072602011-01-19 20:01:44 +0000791 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000792
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000793 orig_neigh_router = orig_node_get_router(orig_neigh_node);
794
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795 /* drop packet if sender is not a direct neighbor and if we
796 * don't route towards it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000797 if (!is_single_hop_neigh && (!orig_neigh_router)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798 bat_dbg(DBG_BATMAN, bat_priv,
799 "Drop packet: OGM via unknown neighbor!\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000800 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801 }
802
803 is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
804 batman_packet, if_incoming);
805
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000806 bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
807
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808 /* update ranking if it is not a duplicate or has the same
809 * seqno and similar ttl as the non-duplicate */
810 if (is_bidirectional &&
811 (!is_duplicate ||
812 ((orig_node->last_real_seqno == batman_packet->seqno) &&
813 (orig_node->last_ttl - 3 <= batman_packet->ttl))))
814 update_orig(bat_priv, orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200815 if_incoming, tt_buff, is_duplicate);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817 /* is single hop (direct) neighbor */
818 if (is_single_hop_neigh) {
819
820 /* mark direct link on incoming interface */
821 schedule_forward_packet(orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200822 1, if_incoming);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823
824 bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
825 "rebroadcast neighbor packet with direct link flag\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000826 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827 }
828
829 /* multihop originator */
830 if (!is_bidirectional) {
831 bat_dbg(DBG_BATMAN, bat_priv,
832 "Drop packet: not received via bidirectional link\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000833 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000834 }
835
836 if (is_duplicate) {
837 bat_dbg(DBG_BATMAN, bat_priv,
838 "Drop packet: duplicate packet received\n");
Marek Lindner16b1aba2011-01-19 20:01:42 +0000839 goto out_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840 }
841
842 bat_dbg(DBG_BATMAN, bat_priv,
843 "Forwarding packet: rebroadcast originator packet\n");
844 schedule_forward_packet(orig_node, ethhdr, batman_packet,
Antonio Quartullia73105b2011-04-27 14:27:44 +0200845 0, if_incoming);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000846
847out_neigh:
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000848 if ((orig_neigh_node) && (!is_single_hop_neigh))
849 orig_node_free_ref(orig_neigh_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000850out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000851 if (router)
852 neigh_node_free_ref(router);
853 if (router_router)
854 neigh_node_free_ref(router_router);
855 if (orig_neigh_router)
856 neigh_node_free_ref(orig_neigh_router);
857
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000858 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859}
860
Marek Lindnere6c10f42011-02-18 12:33:20 +0000861int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862{
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 struct ethhdr *ethhdr;
864
865 /* drop packet if it has not necessary minimum size */
866 if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
867 return NET_RX_DROP;
868
869 ethhdr = (struct ethhdr *)skb_mac_header(skb);
870
871 /* packet with broadcast indication but unicast recipient */
872 if (!is_broadcast_ether_addr(ethhdr->h_dest))
873 return NET_RX_DROP;
874
875 /* packet with broadcast sender address */
876 if (is_broadcast_ether_addr(ethhdr->h_source))
877 return NET_RX_DROP;
878
879 /* create a copy of the skb, if needed, to modify it. */
880 if (skb_cow(skb, 0) < 0)
881 return NET_RX_DROP;
882
883 /* keep skb linear */
884 if (skb_linearize(skb) < 0)
885 return NET_RX_DROP;
886
887 ethhdr = (struct ethhdr *)skb_mac_header(skb);
888
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000889 receive_aggr_bat_packet(ethhdr,
890 skb->data,
891 skb_headlen(skb),
Marek Lindnere6c10f42011-02-18 12:33:20 +0000892 hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893
894 kfree_skb(skb);
895 return NET_RX_SUCCESS;
896}
897
898static int recv_my_icmp_packet(struct bat_priv *bat_priv,
899 struct sk_buff *skb, size_t icmp_len)
900{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200901 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000902 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000903 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904 struct icmp_packet_rr *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000905 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906
907 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908
909 /* add data to device queue */
910 if (icmp_packet->msg_type != ECHO_REQUEST) {
911 bat_socket_receive_packet(icmp_packet, icmp_len);
Marek Lindner44524fc2011-02-10 14:33:53 +0000912 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913 }
914
Marek Lindner32ae9b22011-04-20 15:40:58 +0200915 primary_if = primary_if_get_selected(bat_priv);
916 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000917 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918
919 /* answer echo request (ping) */
920 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +0000921 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000922 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000923 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000924
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000925 router = orig_node_get_router(orig_node);
926 if (!router)
927 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000928
Marek Lindner44524fc2011-02-10 14:33:53 +0000929 /* create a copy of the skb, if needed, to modify it. */
930 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
931 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000932
Marek Lindner44524fc2011-02-10 14:33:53 +0000933 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000934
Marek Lindner44524fc2011-02-10 14:33:53 +0000935 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200936 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000937 icmp_packet->msg_type = ECHO_REPLY;
938 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000940 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000941 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000942
Marek Lindner44524fc2011-02-10 14:33:53 +0000943out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200944 if (primary_if)
945 hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000946 if (router)
947 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000948 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000949 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950 return ret;
951}
952
953static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
Simon Wunderlich74ef1152010-12-29 16:15:19 +0000954 struct sk_buff *skb)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000955{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200956 struct hard_iface *primary_if = NULL;
Marek Lindner44524fc2011-02-10 14:33:53 +0000957 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000958 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000959 struct icmp_packet *icmp_packet;
Marek Lindner44524fc2011-02-10 14:33:53 +0000960 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961
962 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963
964 /* send TTL exceeded if packet is an echo request (traceroute) */
965 if (icmp_packet->msg_type != ECHO_REQUEST) {
966 pr_debug("Warning - can't forward icmp packet from %pM to "
967 "%pM: ttl exceeded\n", icmp_packet->orig,
968 icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +0000969 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000970 }
971
Marek Lindner32ae9b22011-04-20 15:40:58 +0200972 primary_if = primary_if_get_selected(bat_priv);
973 if (!primary_if)
Marek Lindner44524fc2011-02-10 14:33:53 +0000974 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975
976 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +0000977 orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000978 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000979 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000980
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000981 router = orig_node_get_router(orig_node);
982 if (!router)
983 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000984
Marek Lindner44524fc2011-02-10 14:33:53 +0000985 /* create a copy of the skb, if needed, to modify it. */
986 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
987 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000988
Marek Lindner44524fc2011-02-10 14:33:53 +0000989 icmp_packet = (struct icmp_packet *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000990
Marek Lindner44524fc2011-02-10 14:33:53 +0000991 memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200992 memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Marek Lindner44524fc2011-02-10 14:33:53 +0000993 icmp_packet->msg_type = TTL_EXCEEDED;
994 icmp_packet->ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000995
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000996 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +0000997 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000998
Marek Lindner44524fc2011-02-10 14:33:53 +0000999out:
Marek Lindner32ae9b22011-04-20 15:40:58 +02001000 if (primary_if)
1001 hardif_free_ref(primary_if);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001002 if (router)
1003 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +00001004 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001005 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006 return ret;
1007}
1008
1009
Marek Lindnere6c10f42011-02-18 12:33:20 +00001010int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001011{
1012 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1013 struct icmp_packet_rr *icmp_packet;
1014 struct ethhdr *ethhdr;
Marek Lindner44524fc2011-02-10 14:33:53 +00001015 struct orig_node *orig_node = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001016 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017 int hdr_size = sizeof(struct icmp_packet);
Marek Lindner44524fc2011-02-10 14:33:53 +00001018 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001019
1020 /**
1021 * we truncate all incoming icmp packets if they don't match our size
1022 */
1023 if (skb->len >= sizeof(struct icmp_packet_rr))
1024 hdr_size = sizeof(struct icmp_packet_rr);
1025
1026 /* drop packet if it has not necessary minimum size */
1027 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindner44524fc2011-02-10 14:33:53 +00001028 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
1030 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1031
1032 /* packet with unicast indication but broadcast recipient */
1033 if (is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001034 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035
1036 /* packet with broadcast sender address */
1037 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindner44524fc2011-02-10 14:33:53 +00001038 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001039
1040 /* not for me */
1041 if (!is_my_mac(ethhdr->h_dest))
Marek Lindner44524fc2011-02-10 14:33:53 +00001042 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001043
1044 icmp_packet = (struct icmp_packet_rr *)skb->data;
1045
1046 /* add record route information if not full */
1047 if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
1048 (icmp_packet->rr_cur < BAT_RR_LEN)) {
1049 memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
1050 ethhdr->h_dest, ETH_ALEN);
1051 icmp_packet->rr_cur++;
1052 }
1053
1054 /* packet for me */
1055 if (is_my_mac(icmp_packet->dst))
1056 return recv_my_icmp_packet(bat_priv, skb, hdr_size);
1057
1058 /* TTL exceeded */
1059 if (icmp_packet->ttl < 2)
Simon Wunderlich74ef1152010-12-29 16:15:19 +00001060 return recv_icmp_ttl_exceeded(bat_priv, skb);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001061
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001062 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +00001063 orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
Marek Lindner44524fc2011-02-10 14:33:53 +00001064 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001065 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +00001066
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001067 router = orig_node_get_router(orig_node);
1068 if (!router)
1069 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001070
Marek Lindner44524fc2011-02-10 14:33:53 +00001071 /* create a copy of the skb, if needed, to modify it. */
1072 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
1073 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001074
Marek Lindner44524fc2011-02-10 14:33:53 +00001075 icmp_packet = (struct icmp_packet_rr *)skb->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076
Marek Lindner44524fc2011-02-10 14:33:53 +00001077 /* decrement ttl */
1078 icmp_packet->ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079
Marek Lindner44524fc2011-02-10 14:33:53 +00001080 /* route it */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001081 send_skb_packet(skb, router->if_incoming, router->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001082 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001083
Marek Lindner44524fc2011-02-10 14:33:53 +00001084out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001085 if (router)
1086 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +00001087 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001088 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001089 return ret;
1090}
1091
Linus Lüssing55158622011-03-14 22:43:27 +00001092/* In the bonding case, send the packets in a round
1093 * robin fashion over the remaining interfaces.
1094 *
1095 * This method rotates the bonding list and increases the
1096 * returned router's refcount. */
1097static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001098 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +00001099{
1100 struct neigh_node *tmp_neigh_node;
1101 struct neigh_node *router = NULL, *first_candidate = NULL;
1102
1103 rcu_read_lock();
1104 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1105 bonding_list) {
1106 if (!first_candidate)
1107 first_candidate = tmp_neigh_node;
1108
1109 /* recv_if == NULL on the first node. */
1110 if (tmp_neigh_node->if_incoming == recv_if)
1111 continue;
1112
1113 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1114 continue;
1115
1116 router = tmp_neigh_node;
1117 break;
1118 }
1119
1120 /* use the first candidate if nothing was found. */
1121 if (!router && first_candidate &&
1122 atomic_inc_not_zero(&first_candidate->refcount))
1123 router = first_candidate;
1124
1125 if (!router)
1126 goto out;
1127
1128 /* selected should point to the next element
1129 * after the current router */
1130 spin_lock_bh(&primary_orig->neigh_list_lock);
1131 /* this is a list_move(), which unfortunately
1132 * does not exist as rcu version */
1133 list_del_rcu(&primary_orig->bond_list);
1134 list_add_rcu(&primary_orig->bond_list,
1135 &router->bonding_list);
1136 spin_unlock_bh(&primary_orig->neigh_list_lock);
1137
1138out:
1139 rcu_read_unlock();
1140 return router;
1141}
1142
1143/* Interface Alternating: Use the best of the
1144 * remaining candidates which are not using
1145 * this interface.
1146 *
1147 * Increases the returned router's refcount */
1148static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001149 const struct hard_iface *recv_if)
Linus Lüssing55158622011-03-14 22:43:27 +00001150{
1151 struct neigh_node *tmp_neigh_node;
1152 struct neigh_node *router = NULL, *first_candidate = NULL;
1153
1154 rcu_read_lock();
1155 list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
1156 bonding_list) {
1157 if (!first_candidate)
1158 first_candidate = tmp_neigh_node;
1159
1160 /* recv_if == NULL on the first node. */
1161 if (tmp_neigh_node->if_incoming == recv_if)
1162 continue;
1163
1164 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
1165 continue;
1166
1167 /* if we don't have a router yet
1168 * or this one is better, choose it. */
1169 if ((!router) ||
1170 (tmp_neigh_node->tq_avg > router->tq_avg)) {
1171 /* decrement refcount of
1172 * previously selected router */
1173 if (router)
1174 neigh_node_free_ref(router);
1175
1176 router = tmp_neigh_node;
1177 atomic_inc_not_zero(&router->refcount);
1178 }
1179
1180 neigh_node_free_ref(tmp_neigh_node);
1181 }
1182
1183 /* use the first candidate if nothing was found. */
1184 if (!router && first_candidate &&
1185 atomic_inc_not_zero(&first_candidate->refcount))
1186 router = first_candidate;
1187
1188 rcu_read_unlock();
1189 return router;
1190}
1191
Antonio Quartullia73105b2011-04-27 14:27:44 +02001192int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
1193{
1194 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1195 struct tt_query_packet *tt_query;
1196 struct ethhdr *ethhdr;
1197
1198 /* drop packet if it has not necessary minimum size */
1199 if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
1200 goto out;
1201
1202 /* I could need to modify it */
1203 if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
1204 goto out;
1205
1206 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1207
1208 /* packet with unicast indication but broadcast recipient */
1209 if (is_broadcast_ether_addr(ethhdr->h_dest))
1210 goto out;
1211
1212 /* packet with broadcast sender address */
1213 if (is_broadcast_ether_addr(ethhdr->h_source))
1214 goto out;
1215
1216 tt_query = (struct tt_query_packet *)skb->data;
1217
1218 tt_query->tt_data = ntohs(tt_query->tt_data);
1219
1220 switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
1221 case TT_REQUEST:
1222 /* If we cannot provide an answer the tt_request is
1223 * forwarded */
1224 if (!send_tt_response(bat_priv, tt_query)) {
1225 bat_dbg(DBG_TT, bat_priv,
1226 "Routing TT_REQUEST to %pM [%c]\n",
1227 tt_query->dst,
1228 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1229 tt_query->tt_data = htons(tt_query->tt_data);
1230 return route_unicast_packet(skb, recv_if);
1231 }
1232 break;
1233 case TT_RESPONSE:
1234 /* packet needs to be linearised to access the TT changes */
1235 if (skb_linearize(skb) < 0)
1236 goto out;
1237
1238 if (is_my_mac(tt_query->dst))
1239 handle_tt_response(bat_priv, tt_query);
1240 else {
1241 bat_dbg(DBG_TT, bat_priv,
1242 "Routing TT_RESPONSE to %pM [%c]\n",
1243 tt_query->dst,
1244 (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
1245 tt_query->tt_data = htons(tt_query->tt_data);
1246 return route_unicast_packet(skb, recv_if);
1247 }
1248 break;
1249 }
1250
1251out:
1252 /* returning NET_RX_DROP will make the caller function kfree the skb */
1253 return NET_RX_DROP;
1254}
1255
Antonio Quartullicc47f662011-04-27 14:27:57 +02001256int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
1257{
1258 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1259 struct roam_adv_packet *roam_adv_packet;
1260 struct orig_node *orig_node;
1261 struct ethhdr *ethhdr;
1262
1263 /* drop packet if it has not necessary minimum size */
1264 if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
1265 goto out;
1266
1267 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1268
1269 /* packet with unicast indication but broadcast recipient */
1270 if (is_broadcast_ether_addr(ethhdr->h_dest))
1271 goto out;
1272
1273 /* packet with broadcast sender address */
1274 if (is_broadcast_ether_addr(ethhdr->h_source))
1275 goto out;
1276
1277 roam_adv_packet = (struct roam_adv_packet *)skb->data;
1278
1279 if (!is_my_mac(roam_adv_packet->dst))
1280 return route_unicast_packet(skb, recv_if);
1281
1282 orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
1283 if (!orig_node)
1284 goto out;
1285
1286 bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
1287 "(client %pM)\n", roam_adv_packet->src,
1288 roam_adv_packet->client);
1289
1290 tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
1291 atomic_read(&orig_node->last_ttvn) + 1, true);
1292
1293 /* Roaming phase starts: I have new information but the ttvn has not
1294 * been incremented yet. This flag will make me check all the incoming
1295 * packets for the correct destination. */
1296 bat_priv->tt_poss_change = true;
1297
1298 orig_node_free_ref(orig_node);
1299out:
1300 /* returning NET_RX_DROP will make the caller function kfree the skb */
1301 return NET_RX_DROP;
1302}
1303
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001304/* find a suitable router for this originator, and use
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001305 * bonding if possible. increases the found neighbors
1306 * refcount.*/
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001307struct neigh_node *find_router(struct bat_priv *bat_priv,
1308 struct orig_node *orig_node,
Sven Eckelmann747e4222011-05-14 23:14:50 +02001309 const struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001310{
1311 struct orig_node *primary_orig_node;
1312 struct orig_node *router_orig;
Linus Lüssing55158622011-03-14 22:43:27 +00001313 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001314 static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
1315 int bonding_enabled;
1316
1317 if (!orig_node)
1318 return NULL;
1319
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001320 router = orig_node_get_router(orig_node);
1321 if (!router)
Marek Lindner01df2b62011-05-05 14:14:46 +02001322 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001323
1324 /* without bonding, the first node should
1325 * always choose the default router. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001326 bonding_enabled = atomic_read(&bat_priv->bonding);
1327
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001328 rcu_read_lock();
1329 /* select default router to output */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +00001330 router_orig = router->orig_node;
Marek Lindner01df2b62011-05-05 14:14:46 +02001331 if (!router_orig)
1332 goto err_unlock;
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001333
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001334 if ((!recv_if) && (!bonding_enabled))
1335 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001336
1337 /* if we have something in the primary_addr, we can search
1338 * for a potential bonding candidate. */
Marek Lindner39901e72011-02-18 12:28:08 +00001339 if (compare_eth(router_orig->primary_addr, zero_mac))
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001340 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001341
1342 /* find the orig_node which has the primary interface. might
1343 * even be the same as our router_orig in many cases */
1344
Marek Lindner39901e72011-02-18 12:28:08 +00001345 if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001346 primary_orig_node = router_orig;
1347 } else {
Marek Lindner7aadf882011-02-18 12:28:09 +00001348 primary_orig_node = orig_hash_find(bat_priv,
1349 router_orig->primary_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350 if (!primary_orig_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001351 goto return_router;
Marek Lindner7aadf882011-02-18 12:28:09 +00001352
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001353 orig_node_free_ref(primary_orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001354 }
1355
1356 /* with less than 2 candidates, we can't do any
1357 * bonding and prefer the original router. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001358 if (atomic_read(&primary_orig_node->bond_candidates) < 2)
1359 goto return_router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001360
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001361 /* all nodes between should choose a candidate which
1362 * is is not on the interface where the packet came
1363 * in. */
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001364
Marek Lindner44524fc2011-02-10 14:33:53 +00001365 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001366
Linus Lüssing55158622011-03-14 22:43:27 +00001367 if (bonding_enabled)
1368 router = find_bond_router(primary_orig_node, recv_if);
1369 else
1370 router = find_ifalter_router(primary_orig_node, recv_if);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001371
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001372return_router:
Antonio Quartullie2cbc112011-05-08 20:52:57 +02001373 if (router && router->if_incoming->if_status != IF_ACTIVE)
1374 goto err_unlock;
1375
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001376 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001377 return router;
Marek Lindner01df2b62011-05-05 14:14:46 +02001378err_unlock:
1379 rcu_read_unlock();
1380err:
1381 if (router)
1382 neigh_node_free_ref(router);
1383 return NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001384}
1385
1386static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
1387{
1388 struct ethhdr *ethhdr;
1389
1390 /* drop packet if it has not necessary minimum size */
1391 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1392 return -1;
1393
1394 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1395
1396 /* packet with unicast indication but broadcast recipient */
1397 if (is_broadcast_ether_addr(ethhdr->h_dest))
1398 return -1;
1399
1400 /* packet with broadcast sender address */
1401 if (is_broadcast_ether_addr(ethhdr->h_source))
1402 return -1;
1403
1404 /* not for me */
1405 if (!is_my_mac(ethhdr->h_dest))
1406 return -1;
1407
1408 return 0;
1409}
1410
Linus Lüssing7cefb142011-03-02 17:39:31 +00001411int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001412{
1413 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindner44524fc2011-02-10 14:33:53 +00001414 struct orig_node *orig_node = NULL;
1415 struct neigh_node *neigh_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001416 struct unicast_packet *unicast_packet;
1417 struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
Marek Lindner44524fc2011-02-10 14:33:53 +00001418 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001419 struct sk_buff *new_skb;
1420
1421 unicast_packet = (struct unicast_packet *)skb->data;
1422
1423 /* TTL exceeded */
1424 if (unicast_packet->ttl < 2) {
1425 pr_debug("Warning - can't forward unicast packet from %pM to "
1426 "%pM: ttl exceeded\n", ethhdr->h_source,
1427 unicast_packet->dest);
Marek Lindner44524fc2011-02-10 14:33:53 +00001428 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001429 }
1430
1431 /* get routing information */
Marek Lindner7aadf882011-02-18 12:28:09 +00001432 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1433
Marek Lindner44524fc2011-02-10 14:33:53 +00001434 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001435 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001436
Simon Wunderlicha4c135c2011-01-19 20:01:43 +00001437 /* find_router() increases neigh_nodes refcount if found. */
Marek Lindner44524fc2011-02-10 14:33:53 +00001438 neigh_node = find_router(bat_priv, orig_node, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001439
Marek Lindnerd0072602011-01-19 20:01:44 +00001440 if (!neigh_node)
Marek Lindner44524fc2011-02-10 14:33:53 +00001441 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001442
1443 /* create a copy of the skb, if needed, to modify it. */
1444 if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
Marek Lindner44524fc2011-02-10 14:33:53 +00001445 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001446
1447 unicast_packet = (struct unicast_packet *)skb->data;
1448
1449 if (unicast_packet->packet_type == BAT_UNICAST &&
1450 atomic_read(&bat_priv->fragmentation) &&
Marek Lindnerd0072602011-01-19 20:01:44 +00001451 skb->len > neigh_node->if_incoming->net_dev->mtu) {
1452 ret = frag_send_skb(skb, bat_priv,
1453 neigh_node->if_incoming, neigh_node->addr);
1454 goto out;
1455 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001456
1457 if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
Marek Lindnerd0072602011-01-19 20:01:44 +00001458 frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001459
1460 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1461
1462 if (ret == NET_RX_DROP)
Marek Lindner44524fc2011-02-10 14:33:53 +00001463 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001464
1465 /* packet was buffered for late merge */
Marek Lindner44524fc2011-02-10 14:33:53 +00001466 if (!new_skb) {
1467 ret = NET_RX_SUCCESS;
1468 goto out;
1469 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001470
1471 skb = new_skb;
1472 unicast_packet = (struct unicast_packet *)skb->data;
1473 }
1474
1475 /* decrement ttl */
1476 unicast_packet->ttl--;
1477
1478 /* route it */
Marek Lindnerd0072602011-01-19 20:01:44 +00001479 send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Marek Lindner44524fc2011-02-10 14:33:53 +00001480 ret = NET_RX_SUCCESS;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001481
Marek Lindner44524fc2011-02-10 14:33:53 +00001482out:
1483 if (neigh_node)
1484 neigh_node_free_ref(neigh_node);
1485 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001486 orig_node_free_ref(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +00001487 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001488}
1489
Antonio Quartullia73105b2011-04-27 14:27:44 +02001490static int check_unicast_ttvn(struct bat_priv *bat_priv,
1491 struct sk_buff *skb) {
1492 uint8_t curr_ttvn;
1493 struct orig_node *orig_node;
1494 struct ethhdr *ethhdr;
1495 struct hard_iface *primary_if;
1496 struct unicast_packet *unicast_packet;
Antonio Quartullicc47f662011-04-27 14:27:57 +02001497 bool tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001498
1499 /* I could need to modify it */
1500 if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
1501 return 0;
1502
1503 unicast_packet = (struct unicast_packet *)skb->data;
1504
Antonio Quartullicc47f662011-04-27 14:27:57 +02001505 if (is_my_mac(unicast_packet->dest)) {
1506 tt_poss_change = bat_priv->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001507 curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001508 } else {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001509 orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
1510
1511 if (!orig_node)
1512 return 0;
1513
1514 curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
Antonio Quartullicc47f662011-04-27 14:27:57 +02001515 tt_poss_change = orig_node->tt_poss_change;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001516 orig_node_free_ref(orig_node);
1517 }
1518
1519 /* Check whether I have to reroute the packet */
Antonio Quartullicc47f662011-04-27 14:27:57 +02001520 if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
Antonio Quartullia73105b2011-04-27 14:27:44 +02001521 /* Linearize the skb before accessing it */
1522 if (skb_linearize(skb) < 0)
1523 return 0;
1524
1525 ethhdr = (struct ethhdr *)(skb->data +
1526 sizeof(struct unicast_packet));
Antonio Quartullia73105b2011-04-27 14:27:44 +02001527 orig_node = transtable_search(bat_priv, ethhdr->h_dest);
1528
1529 if (!orig_node) {
1530 if (!is_my_client(bat_priv, ethhdr->h_dest))
1531 return 0;
1532 primary_if = primary_if_get_selected(bat_priv);
1533 if (!primary_if)
1534 return 0;
1535 memcpy(unicast_packet->dest,
1536 primary_if->net_dev->dev_addr, ETH_ALEN);
1537 hardif_free_ref(primary_if);
1538 } else {
1539 memcpy(unicast_packet->dest, orig_node->orig,
1540 ETH_ALEN);
1541 curr_ttvn = (uint8_t)
1542 atomic_read(&orig_node->last_ttvn);
1543 orig_node_free_ref(orig_node);
1544 }
1545
1546 bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
1547 "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
1548 "%pM\n", unicast_packet->ttvn, curr_ttvn,
1549 ethhdr->h_dest, unicast_packet->dest);
1550
1551 unicast_packet->ttvn = curr_ttvn;
1552 }
1553 return 1;
1554}
1555
Marek Lindnere6c10f42011-02-18 12:33:20 +00001556int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001557{
Antonio Quartullia73105b2011-04-27 14:27:44 +02001558 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001559 struct unicast_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001560 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001561
1562 if (check_unicast_packet(skb, hdr_size) < 0)
1563 return NET_RX_DROP;
1564
Antonio Quartullia73105b2011-04-27 14:27:44 +02001565 if (!check_unicast_ttvn(bat_priv, skb))
1566 return NET_RX_DROP;
1567
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001568 unicast_packet = (struct unicast_packet *)skb->data;
1569
1570 /* packet for me */
1571 if (is_my_mac(unicast_packet->dest)) {
1572 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
1573 return NET_RX_SUCCESS;
1574 }
1575
Linus Lüssing7cefb142011-03-02 17:39:31 +00001576 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001577}
1578
Marek Lindnere6c10f42011-02-18 12:33:20 +00001579int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001580{
1581 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1582 struct unicast_frag_packet *unicast_packet;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001583 int hdr_size = sizeof(*unicast_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001584 struct sk_buff *new_skb = NULL;
1585 int ret;
1586
1587 if (check_unicast_packet(skb, hdr_size) < 0)
1588 return NET_RX_DROP;
1589
Antonio Quartullia73105b2011-04-27 14:27:44 +02001590 if (!check_unicast_ttvn(bat_priv, skb))
1591 return NET_RX_DROP;
1592
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001593 unicast_packet = (struct unicast_frag_packet *)skb->data;
1594
1595 /* packet for me */
1596 if (is_my_mac(unicast_packet->dest)) {
1597
1598 ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
1599
1600 if (ret == NET_RX_DROP)
1601 return NET_RX_DROP;
1602
1603 /* packet was buffered for late merge */
1604 if (!new_skb)
1605 return NET_RX_SUCCESS;
1606
1607 interface_rx(recv_if->soft_iface, new_skb, recv_if,
1608 sizeof(struct unicast_packet));
1609 return NET_RX_SUCCESS;
1610 }
1611
Linus Lüssing7cefb142011-03-02 17:39:31 +00001612 return route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001613}
1614
1615
Marek Lindnere6c10f42011-02-18 12:33:20 +00001616int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001617{
1618 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001619 struct orig_node *orig_node = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001620 struct bcast_packet *bcast_packet;
1621 struct ethhdr *ethhdr;
Sven Eckelmann704509b2011-05-14 23:14:54 +02001622 int hdr_size = sizeof(*bcast_packet);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001623 int ret = NET_RX_DROP;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001624 int32_t seq_diff;
1625
1626 /* drop packet if it has not necessary minimum size */
1627 if (unlikely(!pskb_may_pull(skb, hdr_size)))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001628 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001629
1630 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1631
1632 /* packet with broadcast indication but unicast recipient */
1633 if (!is_broadcast_ether_addr(ethhdr->h_dest))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001634 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001635
1636 /* packet with broadcast sender address */
1637 if (is_broadcast_ether_addr(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001638 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001639
1640 /* ignore broadcasts sent by myself */
1641 if (is_my_mac(ethhdr->h_source))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001642 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001643
1644 bcast_packet = (struct bcast_packet *)skb->data;
1645
1646 /* ignore broadcasts originated by myself */
1647 if (is_my_mac(bcast_packet->orig))
Marek Lindnerf3e00082011-01-25 21:52:11 +00001648 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001649
1650 if (bcast_packet->ttl < 2)
Marek Lindnerf3e00082011-01-25 21:52:11 +00001651 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001652
Marek Lindner7aadf882011-02-18 12:28:09 +00001653 orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001654
1655 if (!orig_node)
Antonio Quartullib5a6f692011-04-16 11:30:57 +02001656 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001657
Marek Lindnerf3e00082011-01-25 21:52:11 +00001658 spin_lock_bh(&orig_node->bcast_seqno_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001659
1660 /* check whether the packet is a duplicate */
Marek Lindnerf3e00082011-01-25 21:52:11 +00001661 if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1662 ntohl(bcast_packet->seqno)))
1663 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001664
1665 seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
1666
1667 /* check whether the packet is old and the host just restarted. */
1668 if (window_protected(bat_priv, seq_diff,
Marek Lindnerf3e00082011-01-25 21:52:11 +00001669 &orig_node->bcast_seqno_reset))
1670 goto spin_unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001671
1672 /* mark broadcast in flood history, update window position
1673 * if required. */
1674 if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1675 orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
1676
Marek Lindnerf3e00082011-01-25 21:52:11 +00001677 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001678
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001679 /* rebroadcast packet */
Antonio Quartulli86985292011-06-25 19:09:12 +02001680 add_bcast_packet_to_list(bat_priv, skb, 1);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001681
1682 /* broadcast for me */
1683 interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001684 ret = NET_RX_SUCCESS;
1685 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001686
Marek Lindnerf3e00082011-01-25 21:52:11 +00001687spin_unlock:
1688 spin_unlock_bh(&orig_node->bcast_seqno_lock);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001689out:
1690 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001691 orig_node_free_ref(orig_node);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001692 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001693}
1694
Marek Lindnere6c10f42011-02-18 12:33:20 +00001695int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001696{
1697 struct vis_packet *vis_packet;
1698 struct ethhdr *ethhdr;
1699 struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
Sven Eckelmann704509b2011-05-14 23:14:54 +02001700 int hdr_size = sizeof(*vis_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001701
1702 /* keep skb linear */
1703 if (skb_linearize(skb) < 0)
1704 return NET_RX_DROP;
1705
1706 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1707 return NET_RX_DROP;
1708
1709 vis_packet = (struct vis_packet *)skb->data;
1710 ethhdr = (struct ethhdr *)skb_mac_header(skb);
1711
1712 /* not for me */
1713 if (!is_my_mac(ethhdr->h_dest))
1714 return NET_RX_DROP;
1715
1716 /* ignore own packets */
1717 if (is_my_mac(vis_packet->vis_orig))
1718 return NET_RX_DROP;
1719
1720 if (is_my_mac(vis_packet->sender_orig))
1721 return NET_RX_DROP;
1722
1723 switch (vis_packet->vis_type) {
1724 case VIS_TYPE_SERVER_SYNC:
1725 receive_server_sync_packet(bat_priv, vis_packet,
1726 skb_headlen(skb));
1727 break;
1728
1729 case VIS_TYPE_CLIENT_UPDATE:
1730 receive_client_update_packet(bat_priv, vis_packet,
1731 skb_headlen(skb));
1732 break;
1733
1734 default: /* ignore unknown packet */
1735 break;
1736 }
1737
1738 /* We take a copy of the data in the packet, so we should
1739 always free the skbuf. */
1740 return NET_RX_DROP;
1741}