blob: 5dce1abc35d427bea1b50c4a3870be66b98eaeff [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
Linus Luessing9d853f62012-03-11 06:17:52 +0800120#define BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
121ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
122 char *buff, size_t count) \
123{ \
124 struct net_device *net_dev = kobj_to_netdev(kobj); \
125 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); \
126 ssize_t length; \
127 \
128 if (!hard_iface) \
129 return 0; \
130 \
131 length = __store_uint_attr(buff, count, _min, _max, _post_func, \
132 attr, &hard_iface->_name, net_dev); \
133 \
134 hardif_free_ref(hard_iface); \
135 return length; \
136}
137
138#define BAT_ATTR_HIF_SHOW_UINT(_name) \
139ssize_t show_##_name(struct kobject *kobj, \
140 struct attribute *attr, char *buff) \
141{ \
142 struct net_device *net_dev = kobj_to_netdev(kobj); \
143 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); \
144 ssize_t length; \
145 \
146 if (!hard_iface) \
147 return 0; \
148 \
149 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
150 \
151 hardif_free_ref(hard_iface); \
152 return length; \
153}
154
155/* Use this, if you are going to set [name] in hard_iface to an
156 * unsigned integer value*/
157#define BAT_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
158 static BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
159 static BAT_ATTR_HIF_SHOW_UINT(_name) \
160 static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
161
162
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163static int store_bool_attr(char *buff, size_t count,
164 struct net_device *net_dev,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200165 const char *attr_name, atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166{
167 int enabled = -1;
168
169 if (buff[count - 1] == '\n')
170 buff[count - 1] = '\0';
171
172 if ((strncmp(buff, "1", 2) == 0) ||
173 (strncmp(buff, "enable", 7) == 0) ||
174 (strncmp(buff, "enabled", 8) == 0))
175 enabled = 1;
176
177 if ((strncmp(buff, "0", 2) == 0) ||
178 (strncmp(buff, "disable", 8) == 0) ||
179 (strncmp(buff, "disabled", 9) == 0))
180 enabled = 0;
181
182 if (enabled < 0) {
183 bat_info(net_dev,
184 "%s: Invalid parameter received: %s\n",
185 attr_name, buff);
186 return -EINVAL;
187 }
188
189 if (atomic_read(attr) == enabled)
190 return count;
191
192 bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
193 atomic_read(attr) == 1 ? "enabled" : "disabled",
194 enabled == 1 ? "enabled" : "disabled");
195
Eric Dumazet95c96172012-04-15 05:58:06 +0000196 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 return count;
198}
199
200static inline ssize_t __store_bool_attr(char *buff, size_t count,
201 void (*post_func)(struct net_device *),
202 struct attribute *attr,
203 atomic_t *attr_store, struct net_device *net_dev)
204{
205 int ret;
206
Sven Eckelmann747e4222011-05-14 23:14:50 +0200207 ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 if (post_func && ret)
209 post_func(net_dev);
210
211 return ret;
212}
213
Sven Eckelmann747e4222011-05-14 23:14:50 +0200214static int store_uint_attr(const char *buff, size_t count,
215 struct net_device *net_dev, const char *attr_name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216 unsigned int min, unsigned int max, atomic_t *attr)
217{
218 unsigned long uint_val;
219 int ret;
220
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200221 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 if (ret) {
223 bat_info(net_dev,
224 "%s: Invalid parameter received: %s\n",
225 attr_name, buff);
226 return -EINVAL;
227 }
228
229 if (uint_val < min) {
230 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
231 attr_name, uint_val, min);
232 return -EINVAL;
233 }
234
235 if (uint_val > max) {
236 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
237 attr_name, uint_val, max);
238 return -EINVAL;
239 }
240
241 if (atomic_read(attr) == uint_val)
242 return count;
243
244 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
245 attr_name, atomic_read(attr), uint_val);
246
247 atomic_set(attr, uint_val);
248 return count;
249}
250
Sven Eckelmann747e4222011-05-14 23:14:50 +0200251static inline ssize_t __store_uint_attr(const char *buff, size_t count,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000252 int min, int max,
253 void (*post_func)(struct net_device *),
Sven Eckelmann747e4222011-05-14 23:14:50 +0200254 const struct attribute *attr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 atomic_t *attr_store, struct net_device *net_dev)
256{
257 int ret;
258
Sven Eckelmann747e4222011-05-14 23:14:50 +0200259 ret = store_uint_attr(buff, count, net_dev, attr->name,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000260 min, max, attr_store);
261 if (post_func && ret)
262 post_func(net_dev);
263
264 return ret;
265}
266
267static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
268 char *buff)
269{
270 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
271 int vis_mode = atomic_read(&bat_priv->vis_mode);
272
273 return sprintf(buff, "%s\n",
274 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
275 "client" : "server");
276}
277
278static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
279 char *buff, size_t count)
280{
281 struct net_device *net_dev = kobj_to_netdev(kobj);
282 struct bat_priv *bat_priv = netdev_priv(net_dev);
283 unsigned long val;
284 int ret, vis_mode_tmp = -1;
285
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200286 ret = kstrtoul(buff, 10, &val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
288 if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
289 (strncmp(buff, "client", 6) == 0) ||
290 (strncmp(buff, "off", 3) == 0))
291 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
292
293 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
294 (strncmp(buff, "server", 6) == 0))
295 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
296
297 if (vis_mode_tmp < 0) {
298 if (buff[count - 1] == '\n')
299 buff[count - 1] = '\0';
300
301 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100302 "Invalid parameter for 'vis mode' setting received: %s\n",
303 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 return -EINVAL;
305 }
306
307 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
308 return count;
309
310 bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
311 atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
312 "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
313 "client" : "server");
314
Eric Dumazet95c96172012-04-15 05:58:06 +0000315 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 return count;
317}
318
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800319static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
320 char *buff)
321{
322 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
323 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
324}
325
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326static void post_gw_deselect(struct net_device *net_dev)
327{
328 struct bat_priv *bat_priv = netdev_priv(net_dev);
329 gw_deselect(bat_priv);
330}
331
332static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
333 char *buff)
334{
335 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
336 int bytes_written;
337
338 switch (atomic_read(&bat_priv->gw_mode)) {
339 case GW_MODE_CLIENT:
340 bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
341 break;
342 case GW_MODE_SERVER:
343 bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
344 break;
345 default:
346 bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
347 break;
348 }
349
350 return bytes_written;
351}
352
353static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
354 char *buff, size_t count)
355{
356 struct net_device *net_dev = kobj_to_netdev(kobj);
357 struct bat_priv *bat_priv = netdev_priv(net_dev);
358 char *curr_gw_mode_str;
359 int gw_mode_tmp = -1;
360
361 if (buff[count - 1] == '\n')
362 buff[count - 1] = '\0';
363
364 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
365 gw_mode_tmp = GW_MODE_OFF;
366
367 if (strncmp(buff, GW_MODE_CLIENT_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100368 strlen(GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000369 gw_mode_tmp = GW_MODE_CLIENT;
370
371 if (strncmp(buff, GW_MODE_SERVER_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100372 strlen(GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373 gw_mode_tmp = GW_MODE_SERVER;
374
375 if (gw_mode_tmp < 0) {
376 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100377 "Invalid parameter for 'gw mode' setting received: %s\n",
378 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379 return -EINVAL;
380 }
381
382 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
383 return count;
384
385 switch (atomic_read(&bat_priv->gw_mode)) {
386 case GW_MODE_CLIENT:
387 curr_gw_mode_str = GW_MODE_CLIENT_NAME;
388 break;
389 case GW_MODE_SERVER:
390 curr_gw_mode_str = GW_MODE_SERVER_NAME;
391 break;
392 default:
393 curr_gw_mode_str = GW_MODE_OFF_NAME;
394 break;
395 }
396
397 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
398 curr_gw_mode_str, buff);
399
400 gw_deselect(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000401 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000402 return count;
403}
404
405static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
406 char *buff)
407{
408 struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
409 int down, up;
410 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
411
412 gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
413 return sprintf(buff, "%i%s/%i%s\n",
414 (down > 2048 ? down / 1024 : down),
415 (down > 2048 ? "MBit" : "KBit"),
416 (up > 2048 ? up / 1024 : up),
417 (up > 2048 ? "MBit" : "KBit"));
418}
419
420static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
421 char *buff, size_t count)
422{
423 struct net_device *net_dev = kobj_to_netdev(kobj);
424
425 if (buff[count - 1] == '\n')
426 buff[count - 1] = '\0';
427
428 return gw_bandwidth_set(net_dev, buff, count);
429}
430
Marek Lindnerf245c382012-03-11 06:17:51 +0800431BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
432BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100433#ifdef CONFIG_BATMAN_ADV_BLA
Marek Lindnerf245c382012-03-11 06:17:51 +0800434BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100435#endif
Marek Lindnerf245c382012-03-11 06:17:51 +0800436BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
437BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800439static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
Marek Lindnerf245c382012-03-11 06:17:51 +0800441BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
442BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
443BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
444 post_gw_deselect);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
446 store_gw_bwidth);
447#ifdef CONFIG_BATMAN_ADV_DEBUG
Antonio Quartullief3a4092012-05-09 09:50:45 +0200448BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000449#endif
450
451static struct bat_attribute *mesh_attrs[] = {
452 &bat_attr_aggregated_ogms,
453 &bat_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100454#ifdef CONFIG_BATMAN_ADV_BLA
Simon Wunderlichc8673052012-01-22 20:00:20 +0100455 &bat_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100456#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457 &bat_attr_fragmentation,
Antonio Quartulli59b699c2011-07-07 15:35:36 +0200458 &bat_attr_ap_isolation,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000459 &bat_attr_vis_mode,
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800460 &bat_attr_routing_algo,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000461 &bat_attr_gw_mode,
462 &bat_attr_orig_interval,
463 &bat_attr_hop_penalty,
464 &bat_attr_gw_sel_class,
465 &bat_attr_gw_bandwidth,
466#ifdef CONFIG_BATMAN_ADV_DEBUG
467 &bat_attr_log_level,
468#endif
469 NULL,
470};
471
Sven Eckelmann5853e222012-05-12 02:09:24 +0200472int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000473{
474 struct kobject *batif_kobject = &dev->dev.kobj;
475 struct bat_priv *bat_priv = netdev_priv(dev);
476 struct bat_attribute **bat_attr;
477 int err;
478
479 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
480 batif_kobject);
481 if (!bat_priv->mesh_obj) {
482 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
483 SYSFS_IF_MESH_SUBDIR);
484 goto out;
485 }
486
487 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
488 err = sysfs_create_file(bat_priv->mesh_obj,
489 &((*bat_attr)->attr));
490 if (err) {
491 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
492 dev->name, SYSFS_IF_MESH_SUBDIR,
493 ((*bat_attr)->attr).name);
494 goto rem_attr;
495 }
496 }
497
498 return 0;
499
500rem_attr:
501 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
502 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
503
504 kobject_put(bat_priv->mesh_obj);
505 bat_priv->mesh_obj = NULL;
506out:
507 return -ENOMEM;
508}
509
Sven Eckelmann5853e222012-05-12 02:09:24 +0200510void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000511{
512 struct bat_priv *bat_priv = netdev_priv(dev);
513 struct bat_attribute **bat_attr;
514
515 for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
516 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
517
518 kobject_put(bat_priv->mesh_obj);
519 bat_priv->mesh_obj = NULL;
520}
521
522static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
523 char *buff)
524{
525 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000526 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000527 ssize_t length;
528
Marek Lindnere6c10f42011-02-18 12:33:20 +0000529 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000530 return 0;
531
Marek Lindnere6c10f42011-02-18 12:33:20 +0000532 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
533 "none" : hard_iface->soft_iface->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534
Marek Lindnere6c10f42011-02-18 12:33:20 +0000535 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000536
537 return length;
538}
539
540static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
541 char *buff, size_t count)
542{
543 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000544 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000546 int ret = count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000547
Marek Lindnere6c10f42011-02-18 12:33:20 +0000548 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000549 return count;
550
551 if (buff[count - 1] == '\n')
552 buff[count - 1] = '\0';
553
554 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100555 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
556 buff);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000557 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 return -EINVAL;
559 }
560
561 if (strncmp(buff, "none", 4) == 0)
562 status_tmp = IF_NOT_IN_USE;
563 else
564 status_tmp = IF_I_WANT_YOU;
565
Marek Lindnere6c10f42011-02-18 12:33:20 +0000566 if (hard_iface->if_status == status_tmp)
567 goto out;
568
569 if ((hard_iface->soft_iface) &&
570 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000571 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200573 if (!rtnl_trylock()) {
574 ret = -ERESTARTSYS;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000575 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576 }
577
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200578 if (status_tmp == IF_NOT_IN_USE) {
Marek Lindnere6c10f42011-02-18 12:33:20 +0000579 hardif_disable_interface(hard_iface);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200580 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 }
582
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200583 /* if the interface already is in use */
584 if (hard_iface->if_status != IF_NOT_IN_USE)
585 hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200587 ret = hardif_enable_interface(hard_iface, buff);
588
589unlock:
590 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000591out:
Marek Lindnere6c10f42011-02-18 12:33:20 +0000592 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593 return ret;
594}
595
596static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
597 char *buff)
598{
599 struct net_device *net_dev = kobj_to_netdev(kobj);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000600 struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000601 ssize_t length;
602
Marek Lindnere6c10f42011-02-18 12:33:20 +0000603 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 return 0;
605
Marek Lindnere6c10f42011-02-18 12:33:20 +0000606 switch (hard_iface->if_status) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607 case IF_TO_BE_REMOVED:
608 length = sprintf(buff, "disabling\n");
609 break;
610 case IF_INACTIVE:
611 length = sprintf(buff, "inactive\n");
612 break;
613 case IF_ACTIVE:
614 length = sprintf(buff, "active\n");
615 break;
616 case IF_TO_BE_ACTIVATED:
617 length = sprintf(buff, "enabling\n");
618 break;
619 case IF_NOT_IN_USE:
620 default:
621 length = sprintf(buff, "not in use\n");
622 break;
623 }
624
Marek Lindnere6c10f42011-02-18 12:33:20 +0000625 hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626
627 return length;
628}
629
630static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
631 show_mesh_iface, store_mesh_iface);
632static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
633
634static struct bat_attribute *batman_attrs[] = {
635 &bat_attr_mesh_iface,
636 &bat_attr_iface_status,
637 NULL,
638};
639
Sven Eckelmann5853e222012-05-12 02:09:24 +0200640int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641{
642 struct kobject *hardif_kobject = &dev->dev.kobj;
643 struct bat_attribute **bat_attr;
644 int err;
645
646 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
647 hardif_kobject);
648
649 if (!*hardif_obj) {
650 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
651 SYSFS_IF_BAT_SUBDIR);
652 goto out;
653 }
654
655 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
656 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
657 if (err) {
658 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
659 dev->name, SYSFS_IF_BAT_SUBDIR,
660 ((*bat_attr)->attr).name);
661 goto rem_attr;
662 }
663 }
664
665 return 0;
666
667rem_attr:
668 for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
669 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
670out:
671 return -ENOMEM;
672}
673
Sven Eckelmann5853e222012-05-12 02:09:24 +0200674void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675{
676 kobject_put(*hardif_obj);
677 *hardif_obj = NULL;
678}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200679
Sven Eckelmann5853e222012-05-12 02:09:24 +0200680int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
681 enum uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +0200682{
Sven Eckelmann5346c352012-05-05 13:27:28 +0200683 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200684 struct hard_iface *primary_if = NULL;
685 struct kobject *bat_kobj;
686 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
687
688 primary_if = primary_if_get_selected(bat_priv);
689 if (!primary_if)
690 goto out;
691
692 bat_kobj = &primary_if->soft_iface->dev.kobj;
693
694 uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
695 strlen(uev_type_str[type]) + 1,
696 GFP_ATOMIC);
697 if (!uevent_env[0])
698 goto out;
699
700 sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
701
702 uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
703 strlen(uev_action_str[action]) + 1,
704 GFP_ATOMIC);
705 if (!uevent_env[1])
706 goto out;
707
708 sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
709
710 /* If the event is DEL, ignore the data field */
711 if (action != UEV_DEL) {
712 uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
713 strlen(data) + 1, GFP_ATOMIC);
714 if (!uevent_env[2])
715 goto out;
716
717 sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
718 }
719
720 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
721out:
722 kfree(uevent_env[0]);
723 kfree(uevent_env[1]);
724 kfree(uevent_env[2]);
725
726 if (primary_if)
727 hardif_free_ref(primary_if);
728
729 if (ret)
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100730 bat_dbg(DBG_BATMAN, bat_priv,
731 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Antonio Quartullic6bda682011-04-26 18:26:01 +0200732 uev_type_str[type], uev_action_str[action],
733 (action == UEV_DEL ? "NULL" : data), ret);
734 return ret;
735}