blob: 2c816883ca136dd25711a0409a8f8274a7d1eb3c [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner
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 "bat_sysfs.h"
24#include "translation-table.h"
25#include "originator.h"
26#include "hard-interface.h"
27#include "gateway_common.h"
28#include "gateway_client.h"
29#include "vis.h"
30
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020031static struct net_device *kobj_to_netdev(struct kobject *obj)
32{
33 struct device *dev = container_of(obj->parent, struct device, kobj);
34 return to_net_dev(dev);
35}
36
37static struct bat_priv *kobj_to_batpriv(struct kobject *obj)
38{
39 struct net_device *net_dev = kobj_to_netdev(obj);
40 return netdev_priv(net_dev);
41}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
Antonio Quartullic6bda682011-04-26 18:26:01 +020043#define UEV_TYPE_VAR "BATTYPE="
44#define UEV_ACTION_VAR "BATACTION="
45#define UEV_DATA_VAR "BATDATA="
46
47static char *uev_action_str[] = {
48 "add",
49 "del",
50 "change"
51};
52
53static char *uev_type_str[] = {
54 "gw"
55};
56
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057/* Use this, if you have customized show and store functions */
58#define BAT_ATTR(_name, _mode, _show, _store) \
59struct bat_attribute bat_attr_##_name = { \
60 .attr = {.name = __stringify(_name), \
61 .mode = _mode }, \
62 .show = _show, \
63 .store = _store, \
64};
65
66#define BAT_ATTR_STORE_BOOL(_name, _post_func) \
67ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
68 char *buff, size_t count) \
69{ \
70 struct net_device *net_dev = kobj_to_netdev(kobj); \
71 struct bat_priv *bat_priv = netdev_priv(net_dev); \
72 return __store_bool_attr(buff, count, _post_func, attr, \
73 &bat_priv->_name, net_dev); \
74}
75
76#define BAT_ATTR_SHOW_BOOL(_name) \
77ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
78 char *buff) \
79{ \
80 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
81 return sprintf(buff, "%s\n", \
82 atomic_read(&bat_priv->_name) == 0 ? \
83 "disabled" : "enabled"); \
84} \
85
86/* Use this, if you are going to turn a [name] in bat_priv on or off */
87#define BAT_ATTR_BOOL(_name, _mode, _post_func) \
88 static BAT_ATTR_STORE_BOOL(_name, _post_func) \
89 static BAT_ATTR_SHOW_BOOL(_name) \
90 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
91
92
93#define BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
94ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
95 char *buff, size_t count) \
96{ \
97 struct net_device *net_dev = kobj_to_netdev(kobj); \
98 struct bat_priv *bat_priv = netdev_priv(net_dev); \
99 return __store_uint_attr(buff, count, _min, _max, _post_func, \
100 attr, &bat_priv->_name, net_dev); \
101}
102
103#define BAT_ATTR_SHOW_UINT(_name) \
104ssize_t show_##_name(struct kobject *kobj, struct attribute *attr, \
105 char *buff) \
106{ \
107 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
108 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
109} \
110
111/* Use this, if you are going to set [name] in bat_priv to unsigned integer
112 * values only */
113#define BAT_ATTR_UINT(_name, _mode, _min, _max, _post_func) \
114 static BAT_ATTR_STORE_UINT(_name, _min, _max, _post_func) \
115 static BAT_ATTR_SHOW_UINT(_name) \
116 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
117
118
119static int store_bool_attr(char *buff, size_t count,
120 struct net_device *net_dev,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200121 const char *attr_name, atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122{
123 int enabled = -1;
124
125 if (buff[count - 1] == '\n')
126 buff[count - 1] = '\0';
127
128 if ((strncmp(buff, "1", 2) == 0) ||
129 (strncmp(buff, "enable", 7) == 0) ||
130 (strncmp(buff, "enabled", 8) == 0))
131 enabled = 1;
132
133 if ((strncmp(buff, "0", 2) == 0) ||
134 (strncmp(buff, "disable", 8) == 0) ||
135 (strncmp(buff, "disabled", 9) == 0))
136 enabled = 0;
137
138 if (enabled < 0) {
139 bat_info(net_dev,
140 "%s: Invalid parameter received: %s\n",
141 attr_name, buff);
142 return -EINVAL;
143 }
144
145 if (atomic_read(attr) == enabled)
146 return count;
147
148 bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
149 atomic_read(attr) == 1 ? "enabled" : "disabled",
150 enabled == 1 ? "enabled" : "disabled");
151
Eric Dumazet95c96172012-04-15 05:58:06 +0000152 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153 return count;
154}
155
156static inline ssize_t __store_bool_attr(char *buff, size_t count,
157 void (*post_func)(struct net_device *),
158 struct attribute *attr,
159 atomic_t *attr_store, struct net_device *net_dev)
160{
161 int ret;
162
Sven Eckelmann747e4222011-05-14 23:14:50 +0200163 ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000164 if (post_func && ret)
165 post_func(net_dev);
166
167 return ret;
168}
169
Sven Eckelmann747e4222011-05-14 23:14:50 +0200170static int store_uint_attr(const char *buff, size_t count,
171 struct net_device *net_dev, const char *attr_name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172 unsigned int min, unsigned int max, atomic_t *attr)
173{
174 unsigned long uint_val;
175 int ret;
176
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200177 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000178 if (ret) {
179 bat_info(net_dev,
180 "%s: Invalid parameter received: %s\n",
181 attr_name, buff);
182 return -EINVAL;
183 }
184
185 if (uint_val < min) {
186 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
187 attr_name, uint_val, min);
188 return -EINVAL;
189 }
190
191 if (uint_val > max) {
192 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
193 attr_name, uint_val, max);
194 return -EINVAL;
195 }
196
197 if (atomic_read(attr) == uint_val)
198 return count;
199
200 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
201 attr_name, atomic_read(attr), uint_val);
202
203 atomic_set(attr, uint_val);
204 return count;
205}
206
Sven Eckelmann747e4222011-05-14 23:14:50 +0200207static inline ssize_t __store_uint_attr(const char *buff, size_t count,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 int min, int max,
209 void (*post_func)(struct net_device *),
Sven Eckelmann747e4222011-05-14 23:14:50 +0200210 const struct attribute *attr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000211 atomic_t *attr_store, struct net_device *net_dev)
212{
213 int ret;
214
Sven Eckelmann747e4222011-05-14 23:14:50 +0200215 ret = store_uint_attr(buff, count, net_dev, attr->name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216 min, max, attr_store);
217 if (post_func && ret)
218 post_func(net_dev);
219
220 return ret;
221}
222
223static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
224 char *buff)
225{
226 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
227 int vis_mode = atomic_read(&bat_priv->vis_mode);
228
229 return sprintf(buff, "%s\n",
230 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
231 "client" : "server");
232}
233
234static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
235 char *buff, size_t count)
236{
237 struct net_device *net_dev = kobj_to_netdev(kobj);
238 struct bat_priv *bat_priv = netdev_priv(net_dev);
239 unsigned long val;
240 int ret, vis_mode_tmp = -1;
241
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200242 ret = kstrtoul(buff, 10, &val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243
244 if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
245 (strncmp(buff, "client", 6) == 0) ||
246 (strncmp(buff, "off", 3) == 0))
247 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
248
249 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
250 (strncmp(buff, "server", 6) == 0))
251 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
252
253 if (vis_mode_tmp < 0) {
254 if (buff[count - 1] == '\n')
255 buff[count - 1] = '\0';
256
257 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100258 "Invalid parameter for 'vis mode' setting received: %s\n",
259 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 return -EINVAL;
261 }
262
263 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
264 return count;
265
266 bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
267 atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
268 "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
269 "client" : "server");
270
Eric Dumazet95c96172012-04-15 05:58:06 +0000271 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 return count;
273}
274
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800275static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
276 char *buff)
277{
278 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
279 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
280}
281
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282static void post_gw_deselect(struct net_device *net_dev)
283{
284 struct bat_priv *bat_priv = netdev_priv(net_dev);
285 gw_deselect(bat_priv);
286}
287
288static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
289 char *buff)
290{
291 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
292 int bytes_written;
293
294 switch (atomic_read(&bat_priv->gw_mode)) {
295 case GW_MODE_CLIENT:
296 bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
297 break;
298 case GW_MODE_SERVER:
299 bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
300 break;
301 default:
302 bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
303 break;
304 }
305
306 return bytes_written;
307}
308
309static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
310 char *buff, size_t count)
311{
312 struct net_device *net_dev = kobj_to_netdev(kobj);
313 struct bat_priv *bat_priv = netdev_priv(net_dev);
314 char *curr_gw_mode_str;
315 int gw_mode_tmp = -1;
316
317 if (buff[count - 1] == '\n')
318 buff[count - 1] = '\0';
319
320 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
321 gw_mode_tmp = GW_MODE_OFF;
322
323 if (strncmp(buff, GW_MODE_CLIENT_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100324 strlen(GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 gw_mode_tmp = GW_MODE_CLIENT;
326
327 if (strncmp(buff, GW_MODE_SERVER_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100328 strlen(GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 gw_mode_tmp = GW_MODE_SERVER;
330
331 if (gw_mode_tmp < 0) {
332 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100333 "Invalid parameter for 'gw mode' setting received: %s\n",
334 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000335 return -EINVAL;
336 }
337
338 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
339 return count;
340
341 switch (atomic_read(&bat_priv->gw_mode)) {
342 case GW_MODE_CLIENT:
343 curr_gw_mode_str = GW_MODE_CLIENT_NAME;
344 break;
345 case GW_MODE_SERVER:
346 curr_gw_mode_str = GW_MODE_SERVER_NAME;
347 break;
348 default:
349 curr_gw_mode_str = GW_MODE_OFF_NAME;
350 break;
351 }
352
353 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
354 curr_gw_mode_str, buff);
355
356 gw_deselect(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000357 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 return count;
359}
360
361static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
362 char *buff)
363{
364 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
365 int down, up;
366 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
367
368 gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
369 return sprintf(buff, "%i%s/%i%s\n",
370 (down > 2048 ? down / 1024 : down),
371 (down > 2048 ? "MBit" : "KBit"),
372 (up > 2048 ? up / 1024 : up),
373 (up > 2048 ? "MBit" : "KBit"));
374}
375
376static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
377 char *buff, size_t count)
378{
379 struct net_device *net_dev = kobj_to_netdev(kobj);
380
381 if (buff[count - 1] == '\n')
382 buff[count - 1] = '\0';
383
384 return gw_bandwidth_set(net_dev, buff, count);
385}
386
387BAT_ATTR_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
388BAT_ATTR_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100389#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlichc8673052012-01-22 20:00:20 +0100390BAT_ATTR_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100391#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392BAT_ATTR_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200393BAT_ATTR_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000394static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800395static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
397BAT_ATTR_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
398BAT_ATTR_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
399BAT_ATTR_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
400 post_gw_deselect);
401static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
402 store_gw_bwidth);
403#ifdef CONFIG_BATMAN_ADV_DEBUG
Simon Wunderlich23721382012-01-22 20:00:19 +0100404BAT_ATTR_UINT(log_level, S_IRUGO | S_IWUSR, 0, 15, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405#endif
406
407static struct bat_attribute *mesh_attrs[] = {
408 &bat_attr_aggregated_ogms,
409 &bat_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100410#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlichc8673052012-01-22 20:00:20 +0100411 &bat_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100412#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 &bat_attr_fragmentation,
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200414 &bat_attr_ap_isolation,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415 &bat_attr_vis_mode,
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800416 &bat_attr_routing_algo,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417 &bat_attr_gw_mode,
418 &bat_attr_orig_interval,
419 &bat_attr_hop_penalty,
420 &bat_attr_gw_sel_class,
421 &bat_attr_gw_bandwidth,
422#ifdef CONFIG_BATMAN_ADV_DEBUG
423 &bat_attr_log_level,
424#endif
425 NULL,
426};
427
428int sysfs_add_meshif(struct net_device *dev)
429{
430 struct kobject *batif_kobject = &dev->dev.kobj;
431 struct bat_priv *bat_priv = netdev_priv(dev);
432 struct bat_attribute **bat_attr;
433 int err;
434
435 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
436 batif_kobject);
437 if (!bat_priv->mesh_obj) {
438 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
439 SYSFS_IF_MESH_SUBDIR);
440 goto out;
441 }
442
443 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
444 err = sysfs_create_file(bat_priv->mesh_obj,
445 &((*bat_attr)->attr));
446 if (err) {
447 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
448 dev->name, SYSFS_IF_MESH_SUBDIR,
449 ((*bat_attr)->attr).name);
450 goto rem_attr;
451 }
452 }
453
454 return 0;
455
456rem_attr:
457 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
458 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
459
460 kobject_put(bat_priv->mesh_obj);
461 bat_priv->mesh_obj = NULL;
462out:
463 return -ENOMEM;
464}
465
466void sysfs_del_meshif(struct net_device *dev)
467{
468 struct bat_priv *bat_priv = netdev_priv(dev);
469 struct bat_attribute **bat_attr;
470
471 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
472 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
473
474 kobject_put(bat_priv->mesh_obj);
475 bat_priv->mesh_obj = NULL;
476}
477
478static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
479 char *buff)
480{
481 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000482 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 ssize_t length;
484
Marek Lindnere6c10f42011-02-18 12:33:20 +0000485 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486 return 0;
487
Marek Lindnere6c10f42011-02-18 12:33:20 +0000488 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
489 "none" : hard_iface->soft_iface->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000490
Marek Lindnere6c10f42011-02-18 12:33:20 +0000491 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492
493 return length;
494}
495
496static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
497 char *buff, size_t count)
498{
499 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000500 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000501 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000502 int ret = count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000503
Marek Lindnere6c10f42011-02-18 12:33:20 +0000504 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000505 return count;
506
507 if (buff[count - 1] == '\n')
508 buff[count - 1] = '\0';
509
510 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100511 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
512 buff);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000513 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000514 return -EINVAL;
515 }
516
517 if (strncmp(buff, "none", 4) == 0)
518 status_tmp = IF_NOT_IN_USE;
519 else
520 status_tmp = IF_I_WANT_YOU;
521
Marek Lindnere6c10f42011-02-18 12:33:20 +0000522 if (hard_iface->if_status == status_tmp)
523 goto out;
524
525 if ((hard_iface->soft_iface) &&
526 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000527 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200529 if (!rtnl_trylock()) {
530 ret = -ERESTARTSYS;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000531 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532 }
533
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200534 if (status_tmp == IF_NOT_IN_USE) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000535 hardif_disable_interface(hard_iface);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200536 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 }
538
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200539 /* if the interface already is in use */
540 if (hard_iface->if_status != IF_NOT_IN_USE)
541 hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200543 ret = hardif_enable_interface(hard_iface, buff);
544
545unlock:
546 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000547out:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000548 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549 return ret;
550}
551
552static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
553 char *buff)
554{
555 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000556 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 ssize_t length;
558
Marek Lindnere6c10f42011-02-18 12:33:20 +0000559 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000560 return 0;
561
Marek Lindnere6c10f42011-02-18 12:33:20 +0000562 switch (hard_iface->if_status) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 case IF_TO_BE_REMOVED:
564 length = sprintf(buff, "disabling\n");
565 break;
566 case IF_INACTIVE:
567 length = sprintf(buff, "inactive\n");
568 break;
569 case IF_ACTIVE:
570 length = sprintf(buff, "active\n");
571 break;
572 case IF_TO_BE_ACTIVATED:
573 length = sprintf(buff, "enabling\n");
574 break;
575 case IF_NOT_IN_USE:
576 default:
577 length = sprintf(buff, "not in use\n");
578 break;
579 }
580
Marek Lindnere6c10f42011-02-18 12:33:20 +0000581 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582
583 return length;
584}
585
586static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
587 show_mesh_iface, store_mesh_iface);
588static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
589
590static struct bat_attribute *batman_attrs[] = {
591 &bat_attr_mesh_iface,
592 &bat_attr_iface_status,
593 NULL,
594};
595
596int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
597{
598 struct kobject *hardif_kobject = &dev->dev.kobj;
599 struct bat_attribute **bat_attr;
600 int err;
601
602 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
603 hardif_kobject);
604
605 if (!*hardif_obj) {
606 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
607 SYSFS_IF_BAT_SUBDIR);
608 goto out;
609 }
610
611 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
612 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
613 if (err) {
614 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
615 dev->name, SYSFS_IF_BAT_SUBDIR,
616 ((*bat_attr)->attr).name);
617 goto rem_attr;
618 }
619 }
620
621 return 0;
622
623rem_attr:
624 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
625 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
626out:
627 return -ENOMEM;
628}
629
630void sysfs_del_hardif(struct kobject **hardif_obj)
631{
632 kobject_put(*hardif_obj);
633 *hardif_obj = NULL;
634}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200635
636int throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
637 enum uev_action action, const char *data)
638{
639 int ret = -1;
640 struct hard_iface *primary_if = NULL;
641 struct kobject *bat_kobj;
642 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
643
644 primary_if = primary_if_get_selected(bat_priv);
645 if (!primary_if)
646 goto out;
647
648 bat_kobj = &primary_if->soft_iface->dev.kobj;
649
650 uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
651 strlen(uev_type_str[type]) + 1,
652 GFP_ATOMIC);
653 if (!uevent_env[0])
654 goto out;
655
656 sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
657
658 uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
659 strlen(uev_action_str[action]) + 1,
660 GFP_ATOMIC);
661 if (!uevent_env[1])
662 goto out;
663
664 sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
665
666 /* If the event is DEL, ignore the data field */
667 if (action != UEV_DEL) {
668 uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
669 strlen(data) + 1, GFP_ATOMIC);
670 if (!uevent_env[2])
671 goto out;
672
673 sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
674 }
675
676 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
677out:
678 kfree(uevent_env[0]);
679 kfree(uevent_env[1]);
680 kfree(uevent_env[2]);
681
682 if (primary_if)
683 hardif_free_ref(primary_if);
684
685 if (ret)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100686 bat_dbg(DBG_BATMAN, bat_priv,
687 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200688 uev_type_str[type], uev_action_str[action],
689 (action == UEV_DEL ? "NULL" : data), ret);
690 return ret;
691}