blob: cead606008a1a99117e2ae7d0609787c130e6333 [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 "soft-interface.h"
24#include "hard-interface.h"
25#include "routing.h"
26#include "send.h"
27#include "bat_debugfs.h"
28#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000029#include "hash.h"
30#include "gateway_common.h"
31#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032#include "bat_sysfs.h"
33#include <linux/slab.h>
34#include <linux/ethtool.h>
35#include <linux/etherdevice.h>
36#include <linux/if_vlan.h>
37#include "unicast.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038
39
40static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd);
41static void bat_get_drvinfo(struct net_device *dev,
42 struct ethtool_drvinfo *info);
43static u32 bat_get_msglevel(struct net_device *dev);
44static void bat_set_msglevel(struct net_device *dev, u32 value);
45static u32 bat_get_link(struct net_device *dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046
47static const struct ethtool_ops bat_ethtool_ops = {
48 .get_settings = bat_get_settings,
49 .get_drvinfo = bat_get_drvinfo,
50 .get_msglevel = bat_get_msglevel,
51 .set_msglevel = bat_set_msglevel,
52 .get_link = bat_get_link,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053};
54
55int my_skb_head_push(struct sk_buff *skb, unsigned int len)
56{
57 int result;
58
59 /**
60 * TODO: We must check if we can release all references to non-payload
61 * data using skb_header_release in our skbs to allow skb_cow_header to
62 * work optimally. This means that those skbs are not allowed to read
63 * or write any data which is before the current position of skb->data
64 * after that call and thus allow other skbs with the same data buffer
65 * to write freely in that area.
66 */
67 result = skb_cow_head(skb, len);
68 if (result < 0)
69 return result;
70
71 skb_push(skb, len);
72 return 0;
73}
74
Marek Lindner7d2b5542011-02-10 14:33:50 +000075static void softif_neigh_free_ref(struct softif_neigh *softif_neigh)
76{
77 if (atomic_dec_and_test(&softif_neigh->refcount))
Paul E. McKenney8e3572c2011-05-02 00:52:23 -070078 kfree_rcu(softif_neigh, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079}
80
Marek Lindner61906ae2011-04-21 15:52:17 +020081static void softif_neigh_vid_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000082{
Marek Lindner61906ae2011-04-21 15:52:17 +020083 struct softif_neigh_vid *softif_neigh_vid;
84 struct softif_neigh *softif_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085 struct hlist_node *node, *node_tmp;
Marek Lindner61906ae2011-04-21 15:52:17 +020086 struct bat_priv *bat_priv;
Simon Wunderlichba85fac2011-04-17 20:34:27 +020087
Marek Lindner61906ae2011-04-21 15:52:17 +020088 softif_neigh_vid = container_of(rcu, struct softif_neigh_vid, rcu);
89 bat_priv = softif_neigh_vid->bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000090
91 spin_lock_bh(&bat_priv->softif_neigh_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092 hlist_for_each_entry_safe(softif_neigh, node, node_tmp,
Marek Lindner61906ae2011-04-21 15:52:17 +020093 &softif_neigh_vid->softif_neigh_list, list) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094 hlist_del_rcu(&softif_neigh->list);
Marek Lindner7d2b5542011-02-10 14:33:50 +000095 softif_neigh_free_ref(softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000096 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097 spin_unlock_bh(&bat_priv->softif_neigh_lock);
Simon Wunderlichba85fac2011-04-17 20:34:27 +020098
Marek Lindner61906ae2011-04-21 15:52:17 +020099 kfree(softif_neigh_vid);
100}
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200101
Marek Lindner61906ae2011-04-21 15:52:17 +0200102static void softif_neigh_vid_free_ref(struct softif_neigh_vid *softif_neigh_vid)
103{
104 if (atomic_dec_and_test(&softif_neigh_vid->refcount))
105 call_rcu(&softif_neigh_vid->rcu, softif_neigh_vid_free_rcu);
106}
107
108static struct softif_neigh_vid *softif_neigh_vid_get(struct bat_priv *bat_priv,
109 short vid)
110{
111 struct softif_neigh_vid *softif_neigh_vid;
112 struct hlist_node *node;
113
114 rcu_read_lock();
115 hlist_for_each_entry_rcu(softif_neigh_vid, node,
116 &bat_priv->softif_neigh_vids, list) {
117 if (softif_neigh_vid->vid != vid)
118 continue;
119
120 if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
121 continue;
122
123 goto out;
124 }
125
Sven Eckelmann704509b2011-05-14 23:14:54 +0200126 softif_neigh_vid = kzalloc(sizeof(*softif_neigh_vid), GFP_ATOMIC);
Marek Lindner61906ae2011-04-21 15:52:17 +0200127 if (!softif_neigh_vid)
128 goto out;
129
130 softif_neigh_vid->vid = vid;
131 softif_neigh_vid->bat_priv = bat_priv;
132
133 /* initialize with 2 - caller decrements counter by one */
134 atomic_set(&softif_neigh_vid->refcount, 2);
135 INIT_HLIST_HEAD(&softif_neigh_vid->softif_neigh_list);
136 INIT_HLIST_NODE(&softif_neigh_vid->list);
137 spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
138 hlist_add_head_rcu(&softif_neigh_vid->list,
139 &bat_priv->softif_neigh_vids);
140 spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
141
142out:
143 rcu_read_unlock();
144 return softif_neigh_vid;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145}
146
147static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200148 const uint8_t *addr, short vid)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000149{
Marek Lindner61906ae2011-04-21 15:52:17 +0200150 struct softif_neigh_vid *softif_neigh_vid;
151 struct softif_neigh *softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000152 struct hlist_node *node;
153
Marek Lindner61906ae2011-04-21 15:52:17 +0200154 softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
155 if (!softif_neigh_vid)
156 goto out;
157
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158 rcu_read_lock();
159 hlist_for_each_entry_rcu(softif_neigh, node,
Marek Lindner61906ae2011-04-21 15:52:17 +0200160 &softif_neigh_vid->softif_neigh_list,
161 list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000162 if (!compare_eth(softif_neigh->addr, addr))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 continue;
164
Marek Lindner7d2b5542011-02-10 14:33:50 +0000165 if (!atomic_inc_not_zero(&softif_neigh->refcount))
166 continue;
167
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000168 softif_neigh->last_seen = jiffies;
Marek Lindner61906ae2011-04-21 15:52:17 +0200169 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170 }
171
Sven Eckelmann704509b2011-05-14 23:14:54 +0200172 softif_neigh = kzalloc(sizeof(*softif_neigh), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173 if (!softif_neigh)
Marek Lindner61906ae2011-04-21 15:52:17 +0200174 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175
176 memcpy(softif_neigh->addr, addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177 softif_neigh->last_seen = jiffies;
Marek Lindner7d2b5542011-02-10 14:33:50 +0000178 /* initialize with 2 - caller decrements counter by one */
179 atomic_set(&softif_neigh->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
181 INIT_HLIST_NODE(&softif_neigh->list);
182 spin_lock_bh(&bat_priv->softif_neigh_lock);
Marek Lindner61906ae2011-04-21 15:52:17 +0200183 hlist_add_head_rcu(&softif_neigh->list,
184 &softif_neigh_vid->softif_neigh_list);
185 spin_unlock_bh(&bat_priv->softif_neigh_lock);
186
187unlock:
188 rcu_read_unlock();
189out:
190 if (softif_neigh_vid)
191 softif_neigh_vid_free_ref(softif_neigh_vid);
192 return softif_neigh;
193}
194
195static struct softif_neigh *softif_neigh_get_selected(
196 struct softif_neigh_vid *softif_neigh_vid)
197{
198 struct softif_neigh *softif_neigh;
199
200 rcu_read_lock();
201 softif_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
202
203 if (softif_neigh && !atomic_inc_not_zero(&softif_neigh->refcount))
204 softif_neigh = NULL;
205
206 rcu_read_unlock();
207 return softif_neigh;
208}
209
210static struct softif_neigh *softif_neigh_vid_get_selected(
211 struct bat_priv *bat_priv,
212 short vid)
213{
214 struct softif_neigh_vid *softif_neigh_vid;
215 struct softif_neigh *softif_neigh = NULL;
216
217 softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
218 if (!softif_neigh_vid)
219 goto out;
220
221 softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
222out:
223 if (softif_neigh_vid)
224 softif_neigh_vid_free_ref(softif_neigh_vid);
225 return softif_neigh;
226}
227
228static void softif_neigh_vid_select(struct bat_priv *bat_priv,
229 struct softif_neigh *new_neigh,
230 short vid)
231{
232 struct softif_neigh_vid *softif_neigh_vid;
233 struct softif_neigh *curr_neigh;
234
235 softif_neigh_vid = softif_neigh_vid_get(bat_priv, vid);
236 if (!softif_neigh_vid)
237 goto out;
238
239 spin_lock_bh(&bat_priv->softif_neigh_lock);
240
241 if (new_neigh && !atomic_inc_not_zero(&new_neigh->refcount))
242 new_neigh = NULL;
243
244 curr_neigh = softif_neigh_vid->softif_neigh;
245 rcu_assign_pointer(softif_neigh_vid->softif_neigh, new_neigh);
246
247 if ((curr_neigh) && (!new_neigh))
248 bat_dbg(DBG_ROUTES, bat_priv,
249 "Removing mesh exit point on vid: %d (prev: %pM).\n",
250 vid, curr_neigh->addr);
251 else if ((curr_neigh) && (new_neigh))
252 bat_dbg(DBG_ROUTES, bat_priv,
253 "Changing mesh exit point on vid: %d from %pM "
254 "to %pM.\n", vid, curr_neigh->addr, new_neigh->addr);
255 else if ((!curr_neigh) && (new_neigh))
256 bat_dbg(DBG_ROUTES, bat_priv,
257 "Setting mesh exit point on vid: %d to %pM.\n",
258 vid, new_neigh->addr);
259
260 if (curr_neigh)
261 softif_neigh_free_ref(curr_neigh);
262
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 spin_unlock_bh(&bat_priv->softif_neigh_lock);
264
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000265out:
Marek Lindner61906ae2011-04-21 15:52:17 +0200266 if (softif_neigh_vid)
267 softif_neigh_vid_free_ref(softif_neigh_vid);
268}
269
270static void softif_neigh_vid_deselect(struct bat_priv *bat_priv,
271 struct softif_neigh_vid *softif_neigh_vid)
272{
273 struct softif_neigh *curr_neigh;
274 struct softif_neigh *softif_neigh = NULL, *softif_neigh_tmp;
275 struct hard_iface *primary_if = NULL;
276 struct hlist_node *node;
277
278 primary_if = primary_if_get_selected(bat_priv);
279 if (!primary_if)
280 goto out;
281
282 /* find new softif_neigh immediately to avoid temporary loops */
283 rcu_read_lock();
284 curr_neigh = rcu_dereference(softif_neigh_vid->softif_neigh);
285
286 hlist_for_each_entry_rcu(softif_neigh_tmp, node,
287 &softif_neigh_vid->softif_neigh_list,
288 list) {
289 if (softif_neigh_tmp == curr_neigh)
290 continue;
291
292 /* we got a neighbor but its mac is 'bigger' than ours */
293 if (memcmp(primary_if->net_dev->dev_addr,
294 softif_neigh_tmp->addr, ETH_ALEN) < 0)
295 continue;
296
297 if (!atomic_inc_not_zero(&softif_neigh_tmp->refcount))
298 continue;
299
300 softif_neigh = softif_neigh_tmp;
301 goto unlock;
302 }
303
304unlock:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 rcu_read_unlock();
Marek Lindner61906ae2011-04-21 15:52:17 +0200306out:
307 softif_neigh_vid_select(bat_priv, softif_neigh, softif_neigh_vid->vid);
308
309 if (primary_if)
310 hardif_free_ref(primary_if);
311 if (softif_neigh)
312 softif_neigh_free_ref(softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313}
314
315int softif_neigh_seq_print_text(struct seq_file *seq, void *offset)
316{
317 struct net_device *net_dev = (struct net_device *)seq->private;
318 struct bat_priv *bat_priv = netdev_priv(net_dev);
Marek Lindner61906ae2011-04-21 15:52:17 +0200319 struct softif_neigh_vid *softif_neigh_vid;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000320 struct softif_neigh *softif_neigh;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200321 struct hard_iface *primary_if;
Marek Lindner61906ae2011-04-21 15:52:17 +0200322 struct hlist_node *node, *node_tmp;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200323 struct softif_neigh *curr_softif_neigh;
Marek Lindner61906ae2011-04-21 15:52:17 +0200324 int ret = 0, last_seen_secs, last_seen_msecs;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325
Marek Lindner32ae9b22011-04-20 15:40:58 +0200326 primary_if = primary_if_get_selected(bat_priv);
327 if (!primary_if) {
328 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
329 "please specify interfaces to enable it\n",
330 net_dev->name);
331 goto out;
332 }
333
334 if (primary_if->if_status != IF_ACTIVE) {
335 ret = seq_printf(seq, "BATMAN mesh %s "
336 "disabled - primary interface not active\n",
337 net_dev->name);
338 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339 }
340
341 seq_printf(seq, "Softif neighbor list (%s)\n", net_dev->name);
342
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343 rcu_read_lock();
Marek Lindner61906ae2011-04-21 15:52:17 +0200344 hlist_for_each_entry_rcu(softif_neigh_vid, node,
345 &bat_priv->softif_neigh_vids, list) {
346 seq_printf(seq, " %-15s %s on vid: %d\n",
347 "Originator", "last-seen", softif_neigh_vid->vid);
348
349 curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
350
351 hlist_for_each_entry_rcu(softif_neigh, node_tmp,
352 &softif_neigh_vid->softif_neigh_list,
353 list) {
354 last_seen_secs = jiffies_to_msecs(jiffies -
355 softif_neigh->last_seen) / 1000;
356 last_seen_msecs = jiffies_to_msecs(jiffies -
357 softif_neigh->last_seen) % 1000;
358 seq_printf(seq, "%s %pM %3i.%03is\n",
359 curr_softif_neigh == softif_neigh
360 ? "=>" : " ", softif_neigh->addr,
361 last_seen_secs, last_seen_msecs);
362 }
363
364 if (curr_softif_neigh)
365 softif_neigh_free_ref(curr_softif_neigh);
366
367 seq_printf(seq, "\n");
368 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369 rcu_read_unlock();
370
Marek Lindner32ae9b22011-04-20 15:40:58 +0200371out:
372 if (primary_if)
373 hardif_free_ref(primary_if);
374 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375}
376
Marek Lindner61906ae2011-04-21 15:52:17 +0200377void softif_neigh_purge(struct bat_priv *bat_priv)
378{
379 struct softif_neigh *softif_neigh, *curr_softif_neigh;
380 struct softif_neigh_vid *softif_neigh_vid;
381 struct hlist_node *node, *node_tmp, *node_tmp2;
382 char do_deselect;
383
384 rcu_read_lock();
385 hlist_for_each_entry_rcu(softif_neigh_vid, node,
386 &bat_priv->softif_neigh_vids, list) {
387 if (!atomic_inc_not_zero(&softif_neigh_vid->refcount))
388 continue;
389
390 curr_softif_neigh = softif_neigh_get_selected(softif_neigh_vid);
391 do_deselect = 0;
392
393 spin_lock_bh(&bat_priv->softif_neigh_lock);
394 hlist_for_each_entry_safe(softif_neigh, node_tmp, node_tmp2,
395 &softif_neigh_vid->softif_neigh_list,
396 list) {
397 if ((!time_after(jiffies, softif_neigh->last_seen +
398 msecs_to_jiffies(SOFTIF_NEIGH_TIMEOUT))) &&
399 (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE))
400 continue;
401
402 if (curr_softif_neigh == softif_neigh) {
403 bat_dbg(DBG_ROUTES, bat_priv,
404 "Current mesh exit point on vid: %d "
405 "'%pM' vanished.\n",
406 softif_neigh_vid->vid,
407 softif_neigh->addr);
408 do_deselect = 1;
409 }
410
411 hlist_del_rcu(&softif_neigh->list);
412 softif_neigh_free_ref(softif_neigh);
413 }
414 spin_unlock_bh(&bat_priv->softif_neigh_lock);
415
416 /* soft_neigh_vid_deselect() needs to acquire the
417 * softif_neigh_lock */
418 if (do_deselect)
419 softif_neigh_vid_deselect(bat_priv, softif_neigh_vid);
420
421 if (curr_softif_neigh)
422 softif_neigh_free_ref(curr_softif_neigh);
423
424 softif_neigh_vid_free_ref(softif_neigh_vid);
425 }
426 rcu_read_unlock();
427
428 spin_lock_bh(&bat_priv->softif_neigh_vid_lock);
429 hlist_for_each_entry_safe(softif_neigh_vid, node, node_tmp,
430 &bat_priv->softif_neigh_vids, list) {
431 if (!hlist_empty(&softif_neigh_vid->softif_neigh_list))
432 continue;
433
434 hlist_del_rcu(&softif_neigh_vid->list);
435 softif_neigh_vid_free_ref(softif_neigh_vid);
436 }
437 spin_unlock_bh(&bat_priv->softif_neigh_vid_lock);
438
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439}
440
441static void softif_batman_recv(struct sk_buff *skb, struct net_device *dev,
442 short vid)
443{
444 struct bat_priv *bat_priv = netdev_priv(dev);
445 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
446 struct batman_packet *batman_packet;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200447 struct softif_neigh *softif_neigh = NULL;
448 struct hard_iface *primary_if = NULL;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200449 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450
451 if (ntohs(ethhdr->h_proto) == ETH_P_8021Q)
452 batman_packet = (struct batman_packet *)
453 (skb->data + ETH_HLEN + VLAN_HLEN);
454 else
455 batman_packet = (struct batman_packet *)(skb->data + ETH_HLEN);
456
457 if (batman_packet->version != COMPAT_VERSION)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200458 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459
460 if (batman_packet->packet_type != BAT_PACKET)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200461 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462
463 if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200464 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465
466 if (is_my_mac(batman_packet->orig))
Marek Lindner32ae9b22011-04-20 15:40:58 +0200467 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468
469 softif_neigh = softif_neigh_get(bat_priv, batman_packet->orig, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 if (!softif_neigh)
Marek Lindner32ae9b22011-04-20 15:40:58 +0200471 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472
Marek Lindner61906ae2011-04-21 15:52:17 +0200473 curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200474 if (curr_softif_neigh == softif_neigh)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 goto out;
476
Marek Lindner32ae9b22011-04-20 15:40:58 +0200477 primary_if = primary_if_get_selected(bat_priv);
478 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000479 goto out;
480
481 /* we got a neighbor but its mac is 'bigger' than ours */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200482 if (memcmp(primary_if->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 softif_neigh->addr, ETH_ALEN) < 0)
484 goto out;
485
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486 /* close own batX device and use softif_neigh as exit node */
Marek Lindner61906ae2011-04-21 15:52:17 +0200487 if (!curr_softif_neigh) {
488 softif_neigh_vid_select(bat_priv, softif_neigh, vid);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200489 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490 }
491
Marek Lindner61906ae2011-04-21 15:52:17 +0200492 /* switch to new 'smallest neighbor' */
493 if (memcmp(softif_neigh->addr, curr_softif_neigh->addr, ETH_ALEN) < 0)
494 softif_neigh_vid_select(bat_priv, softif_neigh, vid);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000495
496out:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000497 kfree_skb(skb);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200498 if (softif_neigh)
499 softif_neigh_free_ref(softif_neigh);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200500 if (curr_softif_neigh)
501 softif_neigh_free_ref(curr_softif_neigh);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200502 if (primary_if)
503 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504 return;
505}
506
507static int interface_open(struct net_device *dev)
508{
509 netif_start_queue(dev);
510 return 0;
511}
512
513static int interface_release(struct net_device *dev)
514{
515 netif_stop_queue(dev);
516 return 0;
517}
518
519static struct net_device_stats *interface_stats(struct net_device *dev)
520{
521 struct bat_priv *bat_priv = netdev_priv(dev);
522 return &bat_priv->stats;
523}
524
525static int interface_set_mac_addr(struct net_device *dev, void *p)
526{
527 struct bat_priv *bat_priv = netdev_priv(dev);
528 struct sockaddr *addr = p;
529
530 if (!is_valid_ether_addr(addr->sa_data))
531 return -EADDRNOTAVAIL;
532
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200533 /* only modify transtable if it has been initialised before */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 if (atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE) {
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200535 tt_local_remove(bat_priv, dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536 "mac address changed");
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200537 tt_local_add(dev, addr->sa_data);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 }
539
540 memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
541 return 0;
542}
543
544static int interface_change_mtu(struct net_device *dev, int new_mtu)
545{
546 /* check ranges */
547 if ((new_mtu < 68) || (new_mtu > hardif_min_mtu(dev)))
548 return -EINVAL;
549
550 dev->mtu = new_mtu;
551
552 return 0;
553}
554
555int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
556{
557 struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
558 struct bat_priv *bat_priv = netdev_priv(soft_iface);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200559 struct hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 struct bcast_packet *bcast_packet;
561 struct vlan_ethhdr *vhdr;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200562 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 int data_len = skb->len, ret;
564 short vid = -1;
565 bool do_bcast = false;
566
567 if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
568 goto dropped;
569
570 soft_iface->trans_start = jiffies;
571
572 switch (ntohs(ethhdr->h_proto)) {
573 case ETH_P_8021Q:
574 vhdr = (struct vlan_ethhdr *)skb->data;
575 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
576
577 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
578 break;
579
580 /* fall through */
581 case ETH_P_BATMAN:
582 softif_batman_recv(skb, soft_iface, vid);
583 goto end;
584 }
585
586 /**
587 * if we have a another chosen mesh exit node in range
588 * it will transport the packets to the mesh
589 */
Marek Lindner61906ae2011-04-21 15:52:17 +0200590 curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
591 if (curr_softif_neigh)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 goto dropped;
593
594 /* TODO: check this for locks */
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200595 tt_local_add(soft_iface, ethhdr->h_source);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596
597 if (is_multicast_ether_addr(ethhdr->h_dest)) {
598 ret = gw_is_target(bat_priv, skb);
599
600 if (ret < 0)
601 goto dropped;
602
603 if (ret == 0)
604 do_bcast = true;
605 }
606
607 /* ethernet packet should be broadcasted */
608 if (do_bcast) {
Marek Lindner32ae9b22011-04-20 15:40:58 +0200609 primary_if = primary_if_get_selected(bat_priv);
610 if (!primary_if)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 goto dropped;
612
Sven Eckelmann704509b2011-05-14 23:14:54 +0200613 if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 goto dropped;
615
616 bcast_packet = (struct bcast_packet *)skb->data;
617 bcast_packet->version = COMPAT_VERSION;
618 bcast_packet->ttl = TTL;
619
620 /* batman packet type: broadcast */
621 bcast_packet->packet_type = BAT_BCAST;
622
623 /* hw address of first interface is the orig mac because only
624 * this mac is known throughout the mesh */
625 memcpy(bcast_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200626 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627
628 /* set broadcast sequence number */
629 bcast_packet->seqno =
630 htonl(atomic_inc_return(&bat_priv->bcast_seqno));
631
632 add_bcast_packet_to_list(bat_priv, skb);
633
634 /* a copy is stored in the bcast list, therefore removing
635 * the original skb. */
636 kfree_skb(skb);
637
638 /* unicast packet */
639 } else {
640 ret = unicast_send_skb(skb, bat_priv);
641 if (ret != 0)
642 goto dropped_freed;
643 }
644
645 bat_priv->stats.tx_packets++;
646 bat_priv->stats.tx_bytes += data_len;
647 goto end;
648
649dropped:
650 kfree_skb(skb);
651dropped_freed:
652 bat_priv->stats.tx_dropped++;
653end:
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200654 if (curr_softif_neigh)
655 softif_neigh_free_ref(curr_softif_neigh);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200656 if (primary_if)
657 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658 return NETDEV_TX_OK;
659}
660
661void interface_rx(struct net_device *soft_iface,
Marek Lindnere6c10f42011-02-18 12:33:20 +0000662 struct sk_buff *skb, struct hard_iface *recv_if,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 int hdr_size)
664{
665 struct bat_priv *bat_priv = netdev_priv(soft_iface);
666 struct unicast_packet *unicast_packet;
667 struct ethhdr *ethhdr;
668 struct vlan_ethhdr *vhdr;
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200669 struct softif_neigh *curr_softif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670 short vid = -1;
671 int ret;
672
673 /* check if enough space is available for pulling, and pull */
674 if (!pskb_may_pull(skb, hdr_size))
675 goto dropped;
676
677 skb_pull_rcsum(skb, hdr_size);
678 skb_reset_mac_header(skb);
679
680 ethhdr = (struct ethhdr *)skb_mac_header(skb);
681
682 switch (ntohs(ethhdr->h_proto)) {
683 case ETH_P_8021Q:
684 vhdr = (struct vlan_ethhdr *)skb->data;
685 vid = ntohs(vhdr->h_vlan_TCI) & VLAN_VID_MASK;
686
687 if (ntohs(vhdr->h_vlan_encapsulated_proto) != ETH_P_BATMAN)
688 break;
689
690 /* fall through */
691 case ETH_P_BATMAN:
692 goto dropped;
693 }
694
695 /**
696 * if we have a another chosen mesh exit node in range
697 * it will transport the packets to the non-mesh network
698 */
Marek Lindner61906ae2011-04-21 15:52:17 +0200699 curr_softif_neigh = softif_neigh_vid_get_selected(bat_priv, vid);
700 if (curr_softif_neigh) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701 skb_push(skb, hdr_size);
702 unicast_packet = (struct unicast_packet *)skb->data;
703
704 if ((unicast_packet->packet_type != BAT_UNICAST) &&
705 (unicast_packet->packet_type != BAT_UNICAST_FRAG))
706 goto dropped;
707
708 skb_reset_mac_header(skb);
709
710 memcpy(unicast_packet->dest,
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200711 curr_softif_neigh->addr, ETH_ALEN);
Linus Lüssing7cefb142011-03-02 17:39:31 +0000712 ret = route_unicast_packet(skb, recv_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 if (ret == NET_RX_DROP)
714 goto dropped;
715
716 goto out;
717 }
718
719 /* skb->dev & skb->pkt_type are set here */
720 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
721 goto dropped;
722 skb->protocol = eth_type_trans(skb, soft_iface);
723
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300724 /* should not be necessary anymore as we use skb_pull_rcsum()
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 * TODO: please verify this and remove this TODO
726 * -- Dec 21st 2009, Simon Wunderlich */
727
728/* skb->ip_summed = CHECKSUM_UNNECESSARY;*/
729
730 bat_priv->stats.rx_packets++;
731 bat_priv->stats.rx_bytes += skb->len + sizeof(struct ethhdr);
732
733 soft_iface->last_rx = jiffies;
734
735 netif_rx(skb);
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200736 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737
738dropped:
739 kfree_skb(skb);
740out:
Simon Wunderlichba85fac2011-04-17 20:34:27 +0200741 if (curr_softif_neigh)
742 softif_neigh_free_ref(curr_softif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743 return;
744}
745
746#ifdef HAVE_NET_DEVICE_OPS
747static const struct net_device_ops bat_netdev_ops = {
748 .ndo_open = interface_open,
749 .ndo_stop = interface_release,
750 .ndo_get_stats = interface_stats,
751 .ndo_set_mac_address = interface_set_mac_addr,
752 .ndo_change_mtu = interface_change_mtu,
753 .ndo_start_xmit = interface_tx,
754 .ndo_validate_addr = eth_validate_addr
755};
756#endif
757
758static void interface_setup(struct net_device *dev)
759{
760 struct bat_priv *priv = netdev_priv(dev);
761 char dev_addr[ETH_ALEN];
762
763 ether_setup(dev);
764
765#ifdef HAVE_NET_DEVICE_OPS
766 dev->netdev_ops = &bat_netdev_ops;
767#else
768 dev->open = interface_open;
769 dev->stop = interface_release;
770 dev->get_stats = interface_stats;
771 dev->set_mac_address = interface_set_mac_addr;
772 dev->change_mtu = interface_change_mtu;
773 dev->hard_start_xmit = interface_tx;
774#endif
775 dev->destructor = free_netdev;
Andrew Lunnaf20b712011-04-17 20:39:07 +0200776 dev->tx_queue_len = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777
778 /**
779 * can't call min_mtu, because the needed variables
780 * have not been initialized yet
781 */
782 dev->mtu = ETH_DATA_LEN;
Sven Eckelmann6e215fd2011-05-08 12:45:45 +0200783 /* reserve more space in the skbuff for our header */
784 dev->hard_header_len = BAT_HEADER_LEN;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
786 /* generate random address */
787 random_ether_addr(dev_addr);
788 memcpy(dev->dev_addr, dev_addr, ETH_ALEN);
789
790 SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);
791
Sven Eckelmann704509b2011-05-14 23:14:54 +0200792 memset(priv, 0, sizeof(*priv));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793}
794
Sven Eckelmann747e4222011-05-14 23:14:50 +0200795struct net_device *softif_create(const char *name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796{
797 struct net_device *soft_iface;
798 struct bat_priv *bat_priv;
799 int ret;
800
Sven Eckelmann704509b2011-05-14 23:14:54 +0200801 soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802
803 if (!soft_iface) {
804 pr_err("Unable to allocate the batman interface: %s\n", name);
805 goto out;
806 }
807
Sven Eckelmannc3caf512011-05-03 11:51:38 +0200808 ret = register_netdevice(soft_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000809 if (ret < 0) {
810 pr_err("Unable to register the batman interface '%s': %i\n",
811 name, ret);
812 goto free_soft_iface;
813 }
814
815 bat_priv = netdev_priv(soft_iface);
816
817 atomic_set(&bat_priv->aggregated_ogms, 1);
818 atomic_set(&bat_priv->bonding, 0);
819 atomic_set(&bat_priv->vis_mode, VIS_TYPE_CLIENT_UPDATE);
820 atomic_set(&bat_priv->gw_mode, GW_MODE_OFF);
821 atomic_set(&bat_priv->gw_sel_class, 20);
822 atomic_set(&bat_priv->gw_bandwidth, 41);
823 atomic_set(&bat_priv->orig_interval, 1000);
824 atomic_set(&bat_priv->hop_penalty, 10);
825 atomic_set(&bat_priv->log_level, 0);
826 atomic_set(&bat_priv->fragmentation, 1);
827 atomic_set(&bat_priv->bcast_queue_left, BCAST_QUEUE_LEN);
828 atomic_set(&bat_priv->batman_queue_left, BATMAN_QUEUE_LEN);
829
830 atomic_set(&bat_priv->mesh_state, MESH_INACTIVE);
831 atomic_set(&bat_priv->bcast_seqno, 1);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200832 atomic_set(&bat_priv->tt_local_changed, 0);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833
834 bat_priv->primary_if = NULL;
835 bat_priv->num_ifaces = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000836
837 ret = sysfs_add_meshif(soft_iface);
838 if (ret < 0)
839 goto unreg_soft_iface;
840
841 ret = debugfs_add_meshif(soft_iface);
842 if (ret < 0)
843 goto unreg_sysfs;
844
845 ret = mesh_init(soft_iface);
846 if (ret < 0)
847 goto unreg_debugfs;
848
849 return soft_iface;
850
851unreg_debugfs:
852 debugfs_del_meshif(soft_iface);
853unreg_sysfs:
854 sysfs_del_meshif(soft_iface);
855unreg_soft_iface:
856 unregister_netdev(soft_iface);
857 return NULL;
858
859free_soft_iface:
860 free_netdev(soft_iface);
861out:
862 return NULL;
863}
864
865void softif_destroy(struct net_device *soft_iface)
866{
867 debugfs_del_meshif(soft_iface);
868 sysfs_del_meshif(soft_iface);
869 mesh_free(soft_iface);
870 unregister_netdevice(soft_iface);
871}
872
Sven Eckelmann747e4222011-05-14 23:14:50 +0200873int softif_is_valid(const struct net_device *net_dev)
Sven Eckelmanne44d8fe2011-03-04 21:36:41 +0000874{
875#ifdef HAVE_NET_DEVICE_OPS
876 if (net_dev->netdev_ops->ndo_start_xmit == interface_tx)
877 return 1;
878#else
879 if (net_dev->hard_start_xmit == interface_tx)
880 return 1;
881#endif
882
883 return 0;
884}
885
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000886/* ethtool */
887static int bat_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
888{
889 cmd->supported = 0;
890 cmd->advertising = 0;
David Decotigny70739492011-04-27 18:32:40 +0000891 ethtool_cmd_speed_set(cmd, SPEED_10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000892 cmd->duplex = DUPLEX_FULL;
893 cmd->port = PORT_TP;
894 cmd->phy_address = 0;
895 cmd->transceiver = XCVR_INTERNAL;
896 cmd->autoneg = AUTONEG_DISABLE;
897 cmd->maxtxpkt = 0;
898 cmd->maxrxpkt = 0;
899
900 return 0;
901}
902
903static void bat_get_drvinfo(struct net_device *dev,
904 struct ethtool_drvinfo *info)
905{
906 strcpy(info->driver, "B.A.T.M.A.N. advanced");
907 strcpy(info->version, SOURCE_VERSION);
908 strcpy(info->fw_version, "N/A");
909 strcpy(info->bus_info, "batman");
910}
911
912static u32 bat_get_msglevel(struct net_device *dev)
913{
914 return -EOPNOTSUPP;
915}
916
917static void bat_set_msglevel(struct net_device *dev, u32 value)
918{
919}
920
921static u32 bat_get_link(struct net_device *dev)
922{
923 return 1;
924}