blob: 913299d03f4a3d1ca0aa92f420f1b741f59e6d96 [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
Marek Lindnerf245c382012-03-11 06:17:51 +080066#define BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067ssize_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
Marek Lindnerf245c382012-03-11 06:17:51 +080076#define BAT_ATTR_SIF_SHOW_BOOL(_name) \
77ssize_t show_##_name(struct kobject *kobj, \
78 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079{ \
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
Marek Lindnerf245c382012-03-11 06:17:51 +080086/* Use this, if you are going to turn a [name] in the soft-interface
87 * (bat_priv) on or off */
88#define BAT_ATTR_SIF_BOOL(_name, _mode, _post_func) \
89 static BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
90 static BAT_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
92
93
Marek Lindnerf245c382012-03-11 06:17:51 +080094#define BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
Marek Lindnerf245c382012-03-11 06:17:51 +080096 char *buff, size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097{ \
98 struct net_device *net_dev = kobj_to_netdev(kobj); \
99 struct bat_priv *bat_priv = netdev_priv(net_dev); \
100 return __store_uint_attr(buff, count, _min, _max, _post_func, \
101 attr, &bat_priv->_name, net_dev); \
102}
103
Marek Lindnerf245c382012-03-11 06:17:51 +0800104#define BAT_ATTR_SIF_SHOW_UINT(_name) \
105ssize_t show_##_name(struct kobject *kobj, \
106 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107{ \
108 struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
109 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
110} \
111
Marek Lindnerf245c382012-03-11 06:17:51 +0800112/* Use this, if you are going to set [name] in the soft-interface
113 * (bat_priv) to an unsigned integer value */
114#define BAT_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
115 static BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
116 static BAT_ATTR_SIF_SHOW_UINT(_name) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000117 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
118
119
120static int store_bool_attr(char *buff, size_t count,
121 struct net_device *net_dev,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200122 const char *attr_name, atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123{
124 int enabled = -1;
125
126 if (buff[count - 1] == '\n')
127 buff[count - 1] = '\0';
128
129 if ((strncmp(buff, "1", 2) == 0) ||
130 (strncmp(buff, "enable", 7) == 0) ||
131 (strncmp(buff, "enabled", 8) == 0))
132 enabled = 1;
133
134 if ((strncmp(buff, "0", 2) == 0) ||
135 (strncmp(buff, "disable", 8) == 0) ||
136 (strncmp(buff, "disabled", 9) == 0))
137 enabled = 0;
138
139 if (enabled < 0) {
140 bat_info(net_dev,
141 "%s: Invalid parameter received: %s\n",
142 attr_name, buff);
143 return -EINVAL;
144 }
145
146 if (atomic_read(attr) == enabled)
147 return count;
148
149 bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
150 atomic_read(attr) == 1 ? "enabled" : "disabled",
151 enabled == 1 ? "enabled" : "disabled");
152
Eric Dumazet95c96172012-04-15 05:58:06 +0000153 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000154 return count;
155}
156
157static inline ssize_t __store_bool_attr(char *buff, size_t count,
158 void (*post_func)(struct net_device *),
159 struct attribute *attr,
160 atomic_t *attr_store, struct net_device *net_dev)
161{
162 int ret;
163
Sven Eckelmann747e4222011-05-14 23:14:50 +0200164 ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 if (post_func && ret)
166 post_func(net_dev);
167
168 return ret;
169}
170
Sven Eckelmann747e4222011-05-14 23:14:50 +0200171static int store_uint_attr(const char *buff, size_t count,
172 struct net_device *net_dev, const char *attr_name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173 unsigned int min, unsigned int max, atomic_t *attr)
174{
175 unsigned long uint_val;
176 int ret;
177
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200178 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179 if (ret) {
180 bat_info(net_dev,
181 "%s: Invalid parameter received: %s\n",
182 attr_name, buff);
183 return -EINVAL;
184 }
185
186 if (uint_val < min) {
187 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
188 attr_name, uint_val, min);
189 return -EINVAL;
190 }
191
192 if (uint_val > max) {
193 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
194 attr_name, uint_val, max);
195 return -EINVAL;
196 }
197
198 if (atomic_read(attr) == uint_val)
199 return count;
200
201 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
202 attr_name, atomic_read(attr), uint_val);
203
204 atomic_set(attr, uint_val);
205 return count;
206}
207
Sven Eckelmann747e4222011-05-14 23:14:50 +0200208static inline ssize_t __store_uint_attr(const char *buff, size_t count,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 int min, int max,
210 void (*post_func)(struct net_device *),
Sven Eckelmann747e4222011-05-14 23:14:50 +0200211 const struct attribute *attr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212 atomic_t *attr_store, struct net_device *net_dev)
213{
214 int ret;
215
Sven Eckelmann747e4222011-05-14 23:14:50 +0200216 ret = store_uint_attr(buff, count, net_dev, attr->name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217 min, max, attr_store);
218 if (post_func && ret)
219 post_func(net_dev);
220
221 return ret;
222}
223
224static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
225 char *buff)
226{
227 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
228 int vis_mode = atomic_read(&bat_priv->vis_mode);
229
230 return sprintf(buff, "%s\n",
231 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
232 "client" : "server");
233}
234
235static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
236 char *buff, size_t count)
237{
238 struct net_device *net_dev = kobj_to_netdev(kobj);
239 struct bat_priv *bat_priv = netdev_priv(net_dev);
240 unsigned long val;
241 int ret, vis_mode_tmp = -1;
242
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200243 ret = kstrtoul(buff, 10, &val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
245 if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
246 (strncmp(buff, "client", 6) == 0) ||
247 (strncmp(buff, "off", 3) == 0))
248 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
249
250 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
251 (strncmp(buff, "server", 6) == 0))
252 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
253
254 if (vis_mode_tmp < 0) {
255 if (buff[count - 1] == '\n')
256 buff[count - 1] = '\0';
257
258 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100259 "Invalid parameter for 'vis mode' setting received: %s\n",
260 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261 return -EINVAL;
262 }
263
264 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
265 return count;
266
267 bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
268 atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
269 "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
270 "client" : "server");
271
Eric Dumazet95c96172012-04-15 05:58:06 +0000272 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273 return count;
274}
275
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800276static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
277 char *buff)
278{
279 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
280 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
281}
282
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283static void post_gw_deselect(struct net_device *net_dev)
284{
285 struct bat_priv *bat_priv = netdev_priv(net_dev);
286 gw_deselect(bat_priv);
287}
288
289static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
290 char *buff)
291{
292 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
293 int bytes_written;
294
295 switch (atomic_read(&bat_priv->gw_mode)) {
296 case GW_MODE_CLIENT:
297 bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
298 break;
299 case GW_MODE_SERVER:
300 bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
301 break;
302 default:
303 bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
304 break;
305 }
306
307 return bytes_written;
308}
309
310static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
311 char *buff, size_t count)
312{
313 struct net_device *net_dev = kobj_to_netdev(kobj);
314 struct bat_priv *bat_priv = netdev_priv(net_dev);
315 char *curr_gw_mode_str;
316 int gw_mode_tmp = -1;
317
318 if (buff[count - 1] == '\n')
319 buff[count - 1] = '\0';
320
321 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
322 gw_mode_tmp = GW_MODE_OFF;
323
324 if (strncmp(buff, GW_MODE_CLIENT_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100325 strlen(GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326 gw_mode_tmp = GW_MODE_CLIENT;
327
328 if (strncmp(buff, GW_MODE_SERVER_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100329 strlen(GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000330 gw_mode_tmp = GW_MODE_SERVER;
331
332 if (gw_mode_tmp < 0) {
333 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100334 "Invalid parameter for 'gw mode' setting received: %s\n",
335 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336 return -EINVAL;
337 }
338
339 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
340 return count;
341
342 switch (atomic_read(&bat_priv->gw_mode)) {
343 case GW_MODE_CLIENT:
344 curr_gw_mode_str = GW_MODE_CLIENT_NAME;
345 break;
346 case GW_MODE_SERVER:
347 curr_gw_mode_str = GW_MODE_SERVER_NAME;
348 break;
349 default:
350 curr_gw_mode_str = GW_MODE_OFF_NAME;
351 break;
352 }
353
354 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
355 curr_gw_mode_str, buff);
356
357 gw_deselect(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000358 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359 return count;
360}
361
362static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
363 char *buff)
364{
365 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
366 int down, up;
367 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
368
369 gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
370 return sprintf(buff, "%i%s/%i%s\n",
371 (down > 2048 ? down / 1024 : down),
372 (down > 2048 ? "MBit" : "KBit"),
373 (up > 2048 ? up / 1024 : up),
374 (up > 2048 ? "MBit" : "KBit"));
375}
376
377static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
378 char *buff, size_t count)
379{
380 struct net_device *net_dev = kobj_to_netdev(kobj);
381
382 if (buff[count - 1] == '\n')
383 buff[count - 1] = '\0';
384
385 return gw_bandwidth_set(net_dev, buff, count);
386}
387
Marek Lindnerf245c382012-03-11 06:17:51 +0800388BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
389BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100390#ifdef CONFIG_BATMAN_ADV_BLA
Marek Lindnerf245c382012-03-11 06:17:51 +0800391BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100392#endif
Marek Lindnerf245c382012-03-11 06:17:51 +0800393BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
394BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800396static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
Marek Lindnerf245c382012-03-11 06:17:51 +0800398BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
399BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
400BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
401 post_gw_deselect);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
403 store_gw_bwidth);
404#ifdef CONFIG_BATMAN_ADV_DEBUG
Marek Lindnerf245c382012-03-11 06:17:51 +0800405BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, 15, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406#endif
407
408static struct bat_attribute *mesh_attrs[] = {
409 &bat_attr_aggregated_ogms,
410 &bat_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100411#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlichc8673052012-01-22 20:00:20 +0100412 &bat_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100413#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 &bat_attr_fragmentation,
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200415 &bat_attr_ap_isolation,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000416 &bat_attr_vis_mode,
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800417 &bat_attr_routing_algo,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000418 &bat_attr_gw_mode,
419 &bat_attr_orig_interval,
420 &bat_attr_hop_penalty,
421 &bat_attr_gw_sel_class,
422 &bat_attr_gw_bandwidth,
423#ifdef CONFIG_BATMAN_ADV_DEBUG
424 &bat_attr_log_level,
425#endif
426 NULL,
427};
428
429int sysfs_add_meshif(struct net_device *dev)
430{
431 struct kobject *batif_kobject = &dev->dev.kobj;
432 struct bat_priv *bat_priv = netdev_priv(dev);
433 struct bat_attribute **bat_attr;
434 int err;
435
436 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
437 batif_kobject);
438 if (!bat_priv->mesh_obj) {
439 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
440 SYSFS_IF_MESH_SUBDIR);
441 goto out;
442 }
443
444 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
445 err = sysfs_create_file(bat_priv->mesh_obj,
446 &((*bat_attr)->attr));
447 if (err) {
448 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
449 dev->name, SYSFS_IF_MESH_SUBDIR,
450 ((*bat_attr)->attr).name);
451 goto rem_attr;
452 }
453 }
454
455 return 0;
456
457rem_attr:
458 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
459 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
460
461 kobject_put(bat_priv->mesh_obj);
462 bat_priv->mesh_obj = NULL;
463out:
464 return -ENOMEM;
465}
466
467void sysfs_del_meshif(struct net_device *dev)
468{
469 struct bat_priv *bat_priv = netdev_priv(dev);
470 struct bat_attribute **bat_attr;
471
472 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
473 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
474
475 kobject_put(bat_priv->mesh_obj);
476 bat_priv->mesh_obj = NULL;
477}
478
479static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
480 char *buff)
481{
482 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000483 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484 ssize_t length;
485
Marek Lindnere6c10f42011-02-18 12:33:20 +0000486 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487 return 0;
488
Marek Lindnere6c10f42011-02-18 12:33:20 +0000489 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
490 "none" : hard_iface->soft_iface->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000491
Marek Lindnere6c10f42011-02-18 12:33:20 +0000492 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000493
494 return length;
495}
496
497static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
498 char *buff, size_t count)
499{
500 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000501 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000502 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000503 int ret = count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000504
Marek Lindnere6c10f42011-02-18 12:33:20 +0000505 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506 return count;
507
508 if (buff[count - 1] == '\n')
509 buff[count - 1] = '\0';
510
511 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100512 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
513 buff);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000514 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000515 return -EINVAL;
516 }
517
518 if (strncmp(buff, "none", 4) == 0)
519 status_tmp = IF_NOT_IN_USE;
520 else
521 status_tmp = IF_I_WANT_YOU;
522
Marek Lindnere6c10f42011-02-18 12:33:20 +0000523 if (hard_iface->if_status == status_tmp)
524 goto out;
525
526 if ((hard_iface->soft_iface) &&
527 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000528 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000529
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200530 if (!rtnl_trylock()) {
531 ret = -ERESTARTSYS;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000532 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000533 }
534
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200535 if (status_tmp == IF_NOT_IN_USE) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000536 hardif_disable_interface(hard_iface);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200537 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000538 }
539
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200540 /* if the interface already is in use */
541 if (hard_iface->if_status != IF_NOT_IN_USE)
542 hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000543
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200544 ret = hardif_enable_interface(hard_iface, buff);
545
546unlock:
547 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000548out:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000549 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000550 return ret;
551}
552
553static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
554 char *buff)
555{
556 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000557 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 ssize_t length;
559
Marek Lindnere6c10f42011-02-18 12:33:20 +0000560 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000561 return 0;
562
Marek Lindnere6c10f42011-02-18 12:33:20 +0000563 switch (hard_iface->if_status) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 case IF_TO_BE_REMOVED:
565 length = sprintf(buff, "disabling\n");
566 break;
567 case IF_INACTIVE:
568 length = sprintf(buff, "inactive\n");
569 break;
570 case IF_ACTIVE:
571 length = sprintf(buff, "active\n");
572 break;
573 case IF_TO_BE_ACTIVATED:
574 length = sprintf(buff, "enabling\n");
575 break;
576 case IF_NOT_IN_USE:
577 default:
578 length = sprintf(buff, "not in use\n");
579 break;
580 }
581
Marek Lindnere6c10f42011-02-18 12:33:20 +0000582 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583
584 return length;
585}
586
587static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
588 show_mesh_iface, store_mesh_iface);
589static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
590
591static struct bat_attribute *batman_attrs[] = {
592 &bat_attr_mesh_iface,
593 &bat_attr_iface_status,
594 NULL,
595};
596
597int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
598{
599 struct kobject *hardif_kobject = &dev->dev.kobj;
600 struct bat_attribute **bat_attr;
601 int err;
602
603 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
604 hardif_kobject);
605
606 if (!*hardif_obj) {
607 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
608 SYSFS_IF_BAT_SUBDIR);
609 goto out;
610 }
611
612 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
613 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
614 if (err) {
615 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
616 dev->name, SYSFS_IF_BAT_SUBDIR,
617 ((*bat_attr)->attr).name);
618 goto rem_attr;
619 }
620 }
621
622 return 0;
623
624rem_attr:
625 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
626 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
627out:
628 return -ENOMEM;
629}
630
631void sysfs_del_hardif(struct kobject **hardif_obj)
632{
633 kobject_put(*hardif_obj);
634 *hardif_obj = NULL;
635}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200636
637int throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
638 enum uev_action action, const char *data)
639{
640 int ret = -1;
641 struct hard_iface *primary_if = NULL;
642 struct kobject *bat_kobj;
643 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
644
645 primary_if = primary_if_get_selected(bat_priv);
646 if (!primary_if)
647 goto out;
648
649 bat_kobj = &primary_if->soft_iface->dev.kobj;
650
651 uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
652 strlen(uev_type_str[type]) + 1,
653 GFP_ATOMIC);
654 if (!uevent_env[0])
655 goto out;
656
657 sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
658
659 uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
660 strlen(uev_action_str[action]) + 1,
661 GFP_ATOMIC);
662 if (!uevent_env[1])
663 goto out;
664
665 sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
666
667 /* If the event is DEL, ignore the data field */
668 if (action != UEV_DEL) {
669 uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
670 strlen(data) + 1, GFP_ATOMIC);
671 if (!uevent_env[2])
672 goto out;
673
674 sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
675 }
676
677 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
678out:
679 kfree(uevent_env[0]);
680 kfree(uevent_env[1]);
681 kfree(uevent_env[2]);
682
683 if (primary_if)
684 hardif_free_ref(primary_if);
685
686 if (ret)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100687 bat_dbg(DBG_BATMAN, bat_priv,
688 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200689 uev_type_str[type], uev_action_str[action],
690 (action == UEV_DEL ? "NULL" : data), ret);
691 return ret;
692}