blob: 04a6d51f1d585ebe0ee4590c0e4d98cc3931301b [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2009-2013 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "main.h"
Antonio Quartulli785ea112011-11-23 11:35:44 +010019#include "distributed-arp-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020#include "originator.h"
21#include "hash.h"
22#include "translation-table.h"
23#include "routing.h"
24#include "gateway_client.h"
25#include "hard-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include "soft-interface.h"
Simon Wunderlich23721382012-01-22 20:00:19 +010027#include "bridge_loop_avoidance.h"
Martin Hundebølld56b1702013-01-25 11:12:39 +010028#include "network-coding.h"
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +020029#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Antonio Quartullidec05072012-11-10 11:00:32 +010031/* hash class keys */
32static struct lock_class_key batadv_orig_hash_lock_class_key;
33
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020034static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020036/* returns 1 if they are the same originator */
Antonio Quartullibbad0a52013-09-02 12:15:02 +020037int batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020038{
Sven Eckelmann56303d32012-06-05 22:31:31 +020039 const void *data1 = container_of(node, struct batadv_orig_node,
40 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020041
dingtianhong323813e2013-12-26 19:40:39 +080042 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020043}
44
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020045/**
46 * batadv_orig_node_vlan_get - get an orig_node_vlan object
47 * @orig_node: the originator serving the VLAN
48 * @vid: the VLAN identifier
49 *
50 * Returns the vlan object identified by vid and belonging to orig_node or NULL
51 * if it does not exist.
52 */
53struct batadv_orig_node_vlan *
54batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
55 unsigned short vid)
56{
57 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
58
59 rcu_read_lock();
60 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
61 if (tmp->vid != vid)
62 continue;
63
64 if (!atomic_inc_not_zero(&tmp->refcount))
65 continue;
66
67 vlan = tmp;
68
69 break;
70 }
71 rcu_read_unlock();
72
73 return vlan;
74}
75
76/**
77 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
78 * object
79 * @orig_node: the originator serving the VLAN
80 * @vid: the VLAN identifier
81 *
82 * Returns NULL in case of failure or the vlan object identified by vid and
83 * belonging to orig_node otherwise. The object is created and added to the list
84 * if it does not exist.
85 *
86 * The object is returned with refcounter increased by 1.
87 */
88struct batadv_orig_node_vlan *
89batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
90 unsigned short vid)
91{
92 struct batadv_orig_node_vlan *vlan;
93
94 spin_lock_bh(&orig_node->vlan_list_lock);
95
96 /* first look if an object for this vid already exists */
97 vlan = batadv_orig_node_vlan_get(orig_node, vid);
98 if (vlan)
99 goto out;
100
101 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
102 if (!vlan)
103 goto out;
104
105 atomic_set(&vlan->refcount, 2);
106 vlan->vid = vid;
107
108 list_add_rcu(&vlan->list, &orig_node->vlan_list);
109
110out:
111 spin_unlock_bh(&orig_node->vlan_list_lock);
112
113 return vlan;
114}
115
116/**
117 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
118 * the originator-vlan object
119 * @orig_vlan: the originator-vlan object to release
120 */
121void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
122{
123 if (atomic_dec_and_test(&orig_vlan->refcount))
124 kfree_rcu(orig_vlan, rcu);
125}
126
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128{
129 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200130 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200132 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133
134 if (!bat_priv->orig_hash)
135 goto err;
136
Antonio Quartullidec05072012-11-10 11:00:32 +0100137 batadv_hash_set_lock_class(bat_priv->orig_hash,
138 &batadv_orig_hash_lock_class_key);
139
Antonio Quartulli72414442012-12-25 13:14:37 +0100140 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
141 queue_delayed_work(batadv_event_workqueue,
142 &bat_priv->orig_work,
143 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
144
Sven Eckelmann5346c352012-05-05 13:27:28 +0200145 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000146
147err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200148 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149}
150
Sven Eckelmann56303d32012-06-05 22:31:31 +0200151void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000152{
Marek Lindner44524fc2011-02-10 14:33:53 +0000153 if (atomic_dec_and_test(&neigh_node->refcount))
Paul E. McKenneyae179ae2011-05-01 23:27:50 -0700154 kfree_rcu(neigh_node, rcu);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000155}
156
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000157/* increases the refcounter of a found router */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200158struct batadv_neigh_node *
159batadv_orig_node_get_router(struct batadv_orig_node *orig_node)
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000160{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200161 struct batadv_neigh_node *router;
Linus LĂĽssinge1a5382f2011-03-14 22:43:37 +0000162
163 rcu_read_lock();
164 router = rcu_dereference(orig_node->router);
165
166 if (router && !atomic_inc_not_zero(&router->refcount))
167 router = NULL;
168
169 rcu_read_unlock();
170 return router;
171}
172
Antonio Quartulli0538f752013-09-02 12:15:01 +0200173/**
174 * batadv_neigh_node_new - create and init a new neigh_node object
175 * @hard_iface: the interface where the neighbour is connected to
176 * @neigh_addr: the mac address of the neighbour interface
177 * @orig_node: originator object representing the neighbour
178 *
179 * Allocates a new neigh_node object and initialises all the generic fields.
180 * Returns the new object or NULL on failure.
181 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200182struct batadv_neigh_node *
183batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
Antonio Quartulli0538f752013-09-02 12:15:01 +0200184 const uint8_t *neigh_addr,
185 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000186{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200187 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Sven Eckelmann704509b2011-05-14 23:14:54 +0200189 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800191 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Marek Lindner9591a792010-12-12 21:57:11 +0000193 INIT_HLIST_NODE(&neigh_node->list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000194
Marek Lindner7ae8b282012-03-01 15:35:21 +0800195 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
Antonio Quartulli0538f752013-09-02 12:15:01 +0200196 neigh_node->if_incoming = hard_iface;
197 neigh_node->orig_node = orig_node;
198
199 INIT_LIST_HEAD(&neigh_node->bonding_list);
Marek Lindner1605d0d2011-02-18 12:28:11 +0000200
201 /* extra reference for return */
202 atomic_set(&neigh_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000203
Marek Lindner7ae8b282012-03-01 15:35:21 +0800204out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205 return neigh_node;
206}
207
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200208static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209{
Sasha Levinb67bfe02013-02-27 17:06:00 -0800210 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200211 struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
212 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000213
Sven Eckelmann56303d32012-06-05 22:31:31 +0200214 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215
Marek Lindnerf987ed62010-12-12 21:57:12 +0000216 spin_lock_bh(&orig_node->neigh_list_lock);
217
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000218 /* for all bonding members ... */
219 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
220 &orig_node->bond_list, bonding_list) {
221 list_del_rcu(&neigh_node->bonding_list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200222 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000223 }
224
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800226 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +0000227 &orig_node->neigh_list, list) {
Marek Lindnerf987ed62010-12-12 21:57:12 +0000228 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200229 batadv_neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 }
231
Marek Lindnerf987ed62010-12-12 21:57:12 +0000232 spin_unlock_bh(&orig_node->neigh_list_lock);
233
Martin Hundebølld56b1702013-01-25 11:12:39 +0100234 /* Free nc_nodes */
235 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
236
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200237 batadv_frag_purge_orig(orig_node, NULL);
238
Antonio Quartulli95fb1302013-08-07 18:28:55 +0200239 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
Sven Eckelmann08c36d32012-05-12 02:09:39 +0200240 "originator timed out");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200242 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
243 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
244
Antonio Quartullia73105b2011-04-27 14:27:44 +0200245 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 kfree(orig_node);
247}
248
Linus LĂĽssing72822222013-04-15 21:43:29 +0800249/**
250 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
251 * schedule an rcu callback for freeing it
252 * @orig_node: the orig node to free
253 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200254void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000255{
256 if (atomic_dec_and_test(&orig_node->refcount))
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200257 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000258}
259
Linus LĂĽssing72822222013-04-15 21:43:29 +0800260/**
261 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
262 * possibly free it (without rcu callback)
263 * @orig_node: the orig node to free
264 */
265void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
266{
267 if (atomic_dec_and_test(&orig_node->refcount))
268 batadv_orig_node_free_rcu(&orig_node->rcu);
269}
270
Sven Eckelmann56303d32012-06-05 22:31:31 +0200271void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200273 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800274 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000275 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000276 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200277 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200278 uint32_t i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000279
280 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281 return;
282
283 cancel_delayed_work_sync(&bat_priv->orig_work);
284
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000286
287 for (i = 0; i < hash->size; i++) {
288 head = &hash->table[i];
289 list_lock = &hash->list_locks[i];
290
291 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800292 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000293 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800294 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200295 batadv_orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000296 }
297 spin_unlock_bh(list_lock);
298 }
299
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200300 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301}
302
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200303/**
304 * batadv_orig_node_new - creates a new orig_node
305 * @bat_priv: the bat priv with all the soft interface information
306 * @addr: the mac address of the originator
307 *
308 * Creates a new originator object and initialise all the generic fields.
309 * The new object is not added to the originator list.
310 * Returns the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200311 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200312struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200313 const uint8_t *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000314{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200315 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200316 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200317 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200318 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200320 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
321 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322
Sven Eckelmann704509b2011-05-14 23:14:54 +0200323 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 if (!orig_node)
325 return NULL;
326
Marek Lindner9591a792010-12-12 21:57:11 +0000327 INIT_HLIST_HEAD(&orig_node->neigh_list);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000328 INIT_LIST_HEAD(&orig_node->bond_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200329 INIT_LIST_HEAD(&orig_node->vlan_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000330 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000331 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200332 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200333 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200334 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000335
Martin Hundebølld56b1702013-01-25 11:12:39 +0100336 batadv_nc_init_orig(orig_node);
337
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000338 /* extra reference for return */
339 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
Antonio Quartulli17071572011-11-07 16:36:40 +0100341 orig_node->tt_initialised = false;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000342 orig_node->bat_priv = bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 memcpy(orig_node->orig, addr, ETH_ALEN);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100344 batadv_dat_init_orig_node_addr(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000345 orig_node->router = NULL;
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200346 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200347 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200348 orig_node->tt_buff_len = 0;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200349 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
350 orig_node->bcast_seqno_reset = reset_time;
351 orig_node->batman_seqno_reset = reset_time;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000353 atomic_set(&orig_node->bond_candidates, 0);
354
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200355 /* create a vlan object for the "untagged" LAN */
356 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
357 if (!vlan)
358 goto free_orig_node;
359 /* batadv_orig_node_vlan_new() increases the refcounter.
360 * Immediately release vlan since it is not needed anymore in this
361 * context
362 */
363 batadv_orig_node_vlan_free_ref(vlan);
364
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200365 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
366 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
367 spin_lock_init(&orig_node->fragments[i].lock);
368 orig_node->fragments[i].size = 0;
369 }
370
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000371 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372free_orig_node:
373 kfree(orig_node);
374 return NULL;
375}
376
Sven Eckelmann56303d32012-06-05 22:31:31 +0200377static bool
378batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
379 struct batadv_orig_node *orig_node,
Antonio Quartulli81e26b12013-09-02 12:15:07 +0200380 struct batadv_neigh_node **best_neigh)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381{
Antonio Quartulli81e26b12013-09-02 12:15:07 +0200382 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800383 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200384 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +0800386 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200387 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Antonio Quartulli81e26b12013-09-02 12:15:07 +0200389 *best_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000390
Marek Lindnerf987ed62010-12-12 21:57:12 +0000391 spin_lock_bh(&orig_node->neigh_list_lock);
392
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800394 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +0000395 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200396 last_seen = neigh_node->last_seen;
397 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000398
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200399 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200400 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
401 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
402 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200403 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
404 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
405 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200406 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200407 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
408 orig_node->orig, neigh_node->addr,
409 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200411 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200412 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
413 orig_node->orig, neigh_node->addr,
414 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415
416 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +0000417
Marek Lindnerf987ed62010-12-12 21:57:12 +0000418 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200419 batadv_bonding_candidate_del(orig_node, neigh_node);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200420 batadv_neigh_node_free_ref(neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421 } else {
Antonio Quartulli81e26b12013-09-02 12:15:07 +0200422 /* store the best_neighbour if this is the first
423 * iteration or if a better neighbor has been found
424 */
425 if (!*best_neigh ||
426 bao->bat_neigh_cmp(neigh_node, *best_neigh) > 0)
427 *best_neigh = neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000428 }
429 }
Marek Lindnerf987ed62010-12-12 21:57:12 +0000430
431 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000432 return neigh_purged;
433}
434
Sven Eckelmann56303d32012-06-05 22:31:31 +0200435static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
436 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200438 struct batadv_neigh_node *best_neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200440 if (batadv_has_timed_out(orig_node->last_seen,
441 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200442 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200443 "Originator timeout: originator %pM, last_seen %u\n",
444 orig_node->orig,
445 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000446 return true;
447 } else {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200448 if (batadv_purge_orig_neighbors(bat_priv, orig_node,
449 &best_neigh_node))
Sven Eckelmann30d3c512012-05-12 02:09:36 +0200450 batadv_update_route(bat_priv, orig_node,
451 best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452 }
453
454 return false;
455}
456
Sven Eckelmann56303d32012-06-05 22:31:31 +0200457static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200459 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800460 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000462 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200463 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200464 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
466 if (!hash)
467 return;
468
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469 /* for all origins... */
470 for (i = 0; i < hash->size; i++) {
471 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000472 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000474 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800475 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000476 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200477 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +0800478 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800479 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200480 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000481 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200483
484 batadv_frag_purge_orig(orig_node,
485 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000487 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000488 }
489
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200490 batadv_gw_node_purge(bat_priv);
491 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492}
493
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200494static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200496 struct delayed_work *delayed_work;
497 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000498
Sven Eckelmann56303d32012-06-05 22:31:31 +0200499 delayed_work = container_of(work, struct delayed_work, work);
500 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200501 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +0100502 queue_delayed_work(batadv_event_workqueue,
503 &bat_priv->orig_work,
504 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505}
506
Sven Eckelmann56303d32012-06-05 22:31:31 +0200507void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200509 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510}
511
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200512int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513{
514 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200515 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200516 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000517
Marek Lindner30da63a2012-08-03 17:15:46 +0200518 primary_if = batadv_seq_print_text_primary_if_get(seq);
519 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200520 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000521
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200522 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200523 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200524 primary_if->net_dev->dev_addr, net_dev->name,
525 bat_priv->bat_algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000526
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200527 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200529 if (!bat_priv->bat_algo_ops->bat_orig_print) {
530 seq_puts(seq,
531 "No printing function for this routing protocol\n");
532 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 }
534
Antonio Quartulli737a2a222013-09-02 12:15:03 +0200535 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536
Marek Lindner30da63a2012-08-03 17:15:46 +0200537 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538}
539
Sven Eckelmann56303d32012-06-05 22:31:31 +0200540int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
541 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200543 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200544 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200545 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000546 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200547 struct batadv_orig_node *orig_node;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200548 uint32_t i;
549 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550
551 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200552 * if_num
553 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554 for (i = 0; i < hash->size; i++) {
555 head = &hash->table[i];
556
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000557 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800558 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200559 ret = 0;
560 if (bao->bat_orig_add_if)
561 ret = bao->bat_orig_add_if(orig_node,
562 max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200563 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 goto err;
565 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000566 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 }
568
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 return 0;
570
571err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000572 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000573 return -ENOMEM;
574}
575
Sven Eckelmann56303d32012-06-05 22:31:31 +0200576int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
577 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000578{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200579 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200580 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200582 struct batadv_hard_iface *hard_iface_tmp;
583 struct batadv_orig_node *orig_node;
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200584 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200585 uint32_t i;
586 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000587
588 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200589 * if_num
590 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 for (i = 0; i < hash->size; i++) {
592 head = &hash->table[i];
593
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000594 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -0800595 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200596 ret = 0;
597 if (bao->bat_orig_del_if)
598 ret = bao->bat_orig_del_if(orig_node,
599 max_if_num,
600 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200601 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602 goto err;
603 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000604 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000605 }
606
607 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
608 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200609 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200610 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 continue;
612
Marek Lindnere6c10f42011-02-18 12:33:20 +0000613 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 continue;
615
Marek Lindnere6c10f42011-02-18 12:33:20 +0000616 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617 continue;
618
Marek Lindnere6c10f42011-02-18 12:33:20 +0000619 if (hard_iface_tmp->if_num > hard_iface->if_num)
620 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 }
622 rcu_read_unlock();
623
Marek Lindnere6c10f42011-02-18 12:33:20 +0000624 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 return 0;
626
627err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000628 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 return -ENOMEM;
630}