blob: e27bfe3dd278b9a16b228846e1c39fe8d7a3502c [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include "bat_sysfs.h"
22#include "translation-table.h"
23#include "originator.h"
24#include "hard-interface.h"
25#include "gateway_common.h"
26#include "gateway_client.h"
27#include "vis.h"
28
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020029static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020030{
31 struct device *dev = container_of(obj->parent, struct device, kobj);
32 return to_net_dev(dev);
33}
34
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020035static struct bat_priv *batadv_kobj_to_batpriv(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020036{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020037 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020038 return netdev_priv(net_dev);
39}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000040
Sven Eckelmann347c80f2012-06-03 22:19:07 +020041#define BATADV_UEV_TYPE_VAR "BATTYPE="
42#define BATADV_UEV_ACTION_VAR "BATACTION="
43#define BATADV_UEV_DATA_VAR "BATDATA="
Antonio Quartullic6bda682011-04-26 18:26:01 +020044
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020045static char *batadv_uev_action_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +020046 "add",
47 "del",
48 "change"
49};
50
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020051static char *batadv_uev_type_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +020052 "gw"
53};
54
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +020056#define BATADV_ATTR(_name, _mode, _show, _store) \
57struct bat_attribute batadv_attr_##_name = { \
58 .attr = {.name = __stringify(_name), \
59 .mode = _mode }, \
60 .show = _show, \
61 .store = _store, \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062};
63
Sven Eckelmann347c80f2012-06-03 22:19:07 +020064#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020065ssize_t batadv_store_##_name(struct kobject *kobj, \
66 struct attribute *attr, char *buff, \
67 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020069 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070 struct bat_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020071 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
72 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073}
74
Sven Eckelmann347c80f2012-06-03 22:19:07 +020075#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020076ssize_t batadv_show_##_name(struct kobject *kobj, \
77 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000078{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020079 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080 return sprintf(buff, "%s\n", \
81 atomic_read(&bat_priv->_name) == 0 ? \
82 "disabled" : "enabled"); \
83} \
84
Marek Lindnerf245c382012-03-11 06:17:51 +080085/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020086 * (bat_priv) on or off
87 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +020088#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
89 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
90 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
91 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
92 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093
94
Sven Eckelmann347c80f2012-06-03 22:19:07 +020095#define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020096ssize_t batadv_store_##_name(struct kobject *kobj, \
97 struct attribute *attr, char *buff, \
98 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200100 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000101 struct bat_priv *bat_priv = netdev_priv(net_dev); \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200102 return __batadv_store_uint_attr(buff, count, _min, _max, \
103 _post_func, attr, \
104 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000105}
106
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200107#define BATADV_ATTR_SIF_SHOW_UINT(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200108ssize_t batadv_show_##_name(struct kobject *kobj, \
109 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000110{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200111 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000112 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
113} \
114
Marek Lindnerf245c382012-03-11 06:17:51 +0800115/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200116 * (bat_priv) to an unsigned integer value
117 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200118#define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
119 static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
120 static BATADV_ATTR_SIF_SHOW_UINT(_name) \
121 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
122 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123
124
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200125#define BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200126ssize_t batadv_store_##_name(struct kobject *kobj, \
127 struct attribute *attr, char *buff, \
128 size_t count) \
Linus Luessing9d853f622012-03-11 06:17:52 +0800129{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200130 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann95638772012-05-12 02:09:31 +0200131 struct hard_iface *hard_iface; \
Linus Luessing9d853f622012-03-11 06:17:52 +0800132 ssize_t length; \
133 \
Sven Eckelmann95638772012-05-12 02:09:31 +0200134 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
Linus Luessing9d853f622012-03-11 06:17:52 +0800135 if (!hard_iface) \
136 return 0; \
137 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200138 length = __batadv_store_uint_attr(buff, count, _min, _max, \
139 _post_func, attr, \
140 &hard_iface->_name, net_dev); \
Linus Luessing9d853f622012-03-11 06:17:52 +0800141 \
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200142 batadv_hardif_free_ref(hard_iface); \
Linus Luessing9d853f622012-03-11 06:17:52 +0800143 return length; \
144}
145
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200146#define BATADV_ATTR_HIF_SHOW_UINT(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200147ssize_t batadv_show_##_name(struct kobject *kobj, \
148 struct attribute *attr, char *buff) \
Linus Luessing9d853f622012-03-11 06:17:52 +0800149{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200150 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann95638772012-05-12 02:09:31 +0200151 struct hard_iface *hard_iface; \
Linus Luessing9d853f622012-03-11 06:17:52 +0800152 ssize_t length; \
153 \
Sven Eckelmann95638772012-05-12 02:09:31 +0200154 hard_iface = batadv_hardif_get_by_netdev(net_dev); \
Linus Luessing9d853f622012-03-11 06:17:52 +0800155 if (!hard_iface) \
156 return 0; \
157 \
158 length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
159 \
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200160 batadv_hardif_free_ref(hard_iface); \
Linus Luessing9d853f622012-03-11 06:17:52 +0800161 return length; \
162}
163
164/* Use this, if you are going to set [name] in hard_iface to an
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200165 * unsigned integer value
166 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200167#define BATADV_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
168 static BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)\
169 static BATADV_ATTR_HIF_SHOW_UINT(_name) \
170 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
171 batadv_store_##_name)
Linus Luessing9d853f622012-03-11 06:17:52 +0800172
173
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200174static int batadv_store_bool_attr(char *buff, size_t count,
175 struct net_device *net_dev,
176 const char *attr_name, atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177{
178 int enabled = -1;
179
180 if (buff[count - 1] == '\n')
181 buff[count - 1] = '\0';
182
183 if ((strncmp(buff, "1", 2) == 0) ||
184 (strncmp(buff, "enable", 7) == 0) ||
185 (strncmp(buff, "enabled", 8) == 0))
186 enabled = 1;
187
188 if ((strncmp(buff, "0", 2) == 0) ||
189 (strncmp(buff, "disable", 8) == 0) ||
190 (strncmp(buff, "disabled", 9) == 0))
191 enabled = 0;
192
193 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200194 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
195 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196 return -EINVAL;
197 }
198
199 if (atomic_read(attr) == enabled)
200 return count;
201
Sven Eckelmann3e348192012-05-16 20:23:22 +0200202 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
203 atomic_read(attr) == 1 ? "enabled" : "disabled",
204 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Eric Dumazet95c96172012-04-15 05:58:06 +0000206 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 return count;
208}
209
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200210static inline ssize_t
211__batadv_store_bool_attr(char *buff, size_t count,
212 void (*post_func)(struct net_device *),
213 struct attribute *attr,
214 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215{
216 int ret;
217
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200218 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
219 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 if (post_func && ret)
221 post_func(net_dev);
222
223 return ret;
224}
225
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200226static int batadv_store_uint_attr(const char *buff, size_t count,
227 struct net_device *net_dev,
228 const char *attr_name,
229 unsigned int min, unsigned int max,
230 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231{
232 unsigned long uint_val;
233 int ret;
234
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200235 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200237 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
238 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 return -EINVAL;
240 }
241
242 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200243 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
244 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 return -EINVAL;
246 }
247
248 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200249 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
250 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 return -EINVAL;
252 }
253
254 if (atomic_read(attr) == uint_val)
255 return count;
256
Sven Eckelmann3e348192012-05-16 20:23:22 +0200257 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
258 attr_name, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259
260 atomic_set(attr, uint_val);
261 return count;
262}
263
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200264static inline ssize_t
265__batadv_store_uint_attr(const char *buff, size_t count,
266 int min, int max,
267 void (*post_func)(struct net_device *),
268 const struct attribute *attr,
269 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270{
271 int ret;
272
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200273 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
274 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275 if (post_func && ret)
276 post_func(net_dev);
277
278 return ret;
279}
280
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200281static ssize_t batadv_show_vis_mode(struct kobject *kobj,
282 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200284 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 int vis_mode = atomic_read(&bat_priv->vis_mode);
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200286 const char *mode;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200288 if (vis_mode == BATADV_VIS_TYPE_CLIENT_UPDATE)
289 mode = "client";
290 else
291 mode = "server";
292
293 return sprintf(buff, "%s\n", mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294}
295
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200296static ssize_t batadv_store_vis_mode(struct kobject *kobj,
297 struct attribute *attr, char *buff,
298 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200300 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 struct bat_priv *bat_priv = netdev_priv(net_dev);
302 unsigned long val;
303 int ret, vis_mode_tmp = -1;
Sven Eckelmann3e348192012-05-16 20:23:22 +0200304 const char *old_mode, *new_mode;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200306 ret = kstrtoul(buff, 10, &val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200308 if (((count == 2) && (!ret) &&
309 (val == BATADV_VIS_TYPE_CLIENT_UPDATE)) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 (strncmp(buff, "client", 6) == 0) ||
311 (strncmp(buff, "off", 3) == 0))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200312 vis_mode_tmp = BATADV_VIS_TYPE_CLIENT_UPDATE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200314 if (((count == 2) && (!ret) &&
315 (val == BATADV_VIS_TYPE_SERVER_SYNC)) ||
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 (strncmp(buff, "server", 6) == 0))
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200317 vis_mode_tmp = BATADV_VIS_TYPE_SERVER_SYNC;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000318
319 if (vis_mode_tmp < 0) {
320 if (buff[count - 1] == '\n')
321 buff[count - 1] = '\0';
322
Sven Eckelmann3e348192012-05-16 20:23:22 +0200323 batadv_info(net_dev,
324 "Invalid parameter for 'vis mode' setting received: %s\n",
325 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326 return -EINVAL;
327 }
328
329 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
330 return count;
331
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200332 if (atomic_read(&bat_priv->vis_mode) == BATADV_VIS_TYPE_CLIENT_UPDATE)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200333 old_mode = "client";
334 else
335 old_mode = "server";
336
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200337 if (vis_mode_tmp == BATADV_VIS_TYPE_CLIENT_UPDATE)
Sven Eckelmann3e348192012-05-16 20:23:22 +0200338 new_mode = "client";
339 else
340 new_mode = "server";
341
342 batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
343 new_mode);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344
Eric Dumazet95c96172012-04-15 05:58:06 +0000345 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346 return count;
347}
348
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200349static ssize_t batadv_show_bat_algo(struct kobject *kobj,
350 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800351{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200352 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800353 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
354}
355
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200356static void batadv_post_gw_deselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000357{
358 struct bat_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200359 batadv_gw_deselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360}
361
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200362static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
363 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000364{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200365 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000366 int bytes_written;
367
368 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200369 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200370 bytes_written = sprintf(buff, "%s\n",
371 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200373 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200374 bytes_written = sprintf(buff, "%s\n",
375 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000376 break;
377 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200378 bytes_written = sprintf(buff, "%s\n",
379 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380 break;
381 }
382
383 return bytes_written;
384}
385
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200386static ssize_t batadv_store_gw_mode(struct kobject *kobj,
387 struct attribute *attr, char *buff,
388 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200390 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 struct bat_priv *bat_priv = netdev_priv(net_dev);
392 char *curr_gw_mode_str;
393 int gw_mode_tmp = -1;
394
395 if (buff[count - 1] == '\n')
396 buff[count - 1] = '\0';
397
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200398 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
399 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200400 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000401
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200402 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
403 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200404 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000405
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200406 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
407 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200408 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409
410 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200411 batadv_info(net_dev,
412 "Invalid parameter for 'gw mode' setting received: %s\n",
413 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000414 return -EINVAL;
415 }
416
417 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
418 return count;
419
420 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200421 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200422 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200424 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200425 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000426 break;
427 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200428 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000429 break;
430 }
431
Sven Eckelmann3e348192012-05-16 20:23:22 +0200432 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
433 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200435 batadv_gw_deselect(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000436 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000437 return count;
438}
439
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200440static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
441 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200443 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000444 int down, up;
445 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
446
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200447 batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 return sprintf(buff, "%i%s/%i%s\n",
449 (down > 2048 ? down / 1024 : down),
450 (down > 2048 ? "MBit" : "KBit"),
451 (up > 2048 ? up / 1024 : up),
452 (up > 2048 ? "MBit" : "KBit"));
453}
454
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200455static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
456 struct attribute *attr, char *buff,
457 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000458{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200459 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460
461 if (buff[count - 1] == '\n')
462 buff[count - 1] = '\0';
463
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200464 return batadv_gw_bandwidth_set(net_dev, buff, count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000465}
466
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200467BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
468BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100469#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200470BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100471#endif
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200472BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
473BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
474static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
475 batadv_store_vis_mode);
476static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
477static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
478 batadv_store_gw_mode);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200479BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
480 INT_MAX, NULL);
481BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200482 NULL);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200483BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200484 batadv_post_gw_deselect);
485static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
486 batadv_store_gw_bwidth);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200488BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489#endif
490
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200491static struct bat_attribute *batadv_mesh_attrs[] = {
492 &batadv_attr_aggregated_ogms,
493 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100494#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200495 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100496#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200497 &batadv_attr_fragmentation,
498 &batadv_attr_ap_isolation,
499 &batadv_attr_vis_mode,
500 &batadv_attr_routing_algo,
501 &batadv_attr_gw_mode,
502 &batadv_attr_orig_interval,
503 &batadv_attr_hop_penalty,
504 &batadv_attr_gw_sel_class,
505 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000506#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200507 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508#endif
509 NULL,
510};
511
Sven Eckelmann5853e222012-05-12 02:09:24 +0200512int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000513{
514 struct kobject *batif_kobject = &dev->dev.kobj;
515 struct bat_priv *bat_priv = netdev_priv(dev);
516 struct bat_attribute **bat_attr;
517 int err;
518
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200519 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520 batif_kobject);
521 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200522 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200523 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000524 goto out;
525 }
526
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200527 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000528 err = sysfs_create_file(bat_priv->mesh_obj,
529 &((*bat_attr)->attr));
530 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200531 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200532 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200533 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 goto rem_attr;
535 }
536 }
537
538 return 0;
539
540rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200541 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000542 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
543
544 kobject_put(bat_priv->mesh_obj);
545 bat_priv->mesh_obj = NULL;
546out:
547 return -ENOMEM;
548}
549
Sven Eckelmann5853e222012-05-12 02:09:24 +0200550void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551{
552 struct bat_priv *bat_priv = netdev_priv(dev);
553 struct bat_attribute **bat_attr;
554
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200555 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
557
558 kobject_put(bat_priv->mesh_obj);
559 bat_priv->mesh_obj = NULL;
560}
561
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200562static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
563 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200565 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann95638772012-05-12 02:09:31 +0200566 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000567 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200568 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569
Marek Lindnere6c10f42011-02-18 12:33:20 +0000570 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 return 0;
572
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200573 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
574 ifname = "none";
575 else
576 ifname = hard_iface->soft_iface->name;
577
578 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000579
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200580 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581
582 return length;
583}
584
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200585static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
586 struct attribute *attr, char *buff,
587 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200589 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann95638772012-05-12 02:09:31 +0200590 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000592 int ret = count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593
Marek Lindnere6c10f42011-02-18 12:33:20 +0000594 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 return count;
596
597 if (buff[count - 1] == '\n')
598 buff[count - 1] = '\0';
599
600 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100601 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
602 buff);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200603 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 return -EINVAL;
605 }
606
607 if (strncmp(buff, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200608 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000609 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200610 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611
Marek Lindnere6c10f42011-02-18 12:33:20 +0000612 if (hard_iface->if_status == status_tmp)
613 goto out;
614
615 if ((hard_iface->soft_iface) &&
616 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000617 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200619 if (!rtnl_trylock()) {
620 ret = -ERESTARTSYS;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000621 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 }
623
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200624 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmann95638772012-05-12 02:09:31 +0200625 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200626 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627 }
628
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200629 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200630 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200631 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632
Sven Eckelmann95638772012-05-12 02:09:31 +0200633 ret = batadv_hardif_enable_interface(hard_iface, buff);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200634
635unlock:
636 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000637out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200638 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 return ret;
640}
641
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200642static ssize_t batadv_show_iface_status(struct kobject *kobj,
643 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200645 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann95638772012-05-12 02:09:31 +0200646 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647 ssize_t length;
648
Marek Lindnere6c10f42011-02-18 12:33:20 +0000649 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650 return 0;
651
Marek Lindnere6c10f42011-02-18 12:33:20 +0000652 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200653 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 length = sprintf(buff, "disabling\n");
655 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200656 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000657 length = sprintf(buff, "inactive\n");
658 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200659 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000660 length = sprintf(buff, "active\n");
661 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200662 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663 length = sprintf(buff, "enabling\n");
664 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200665 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666 default:
667 length = sprintf(buff, "not in use\n");
668 break;
669 }
670
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200671 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000672
673 return length;
674}
675
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200676static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
677 batadv_store_mesh_iface);
678static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200680static struct bat_attribute *batadv_batman_attrs[] = {
681 &batadv_attr_mesh_iface,
682 &batadv_attr_iface_status,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683 NULL,
684};
685
Sven Eckelmann5853e222012-05-12 02:09:24 +0200686int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687{
688 struct kobject *hardif_kobject = &dev->dev.kobj;
689 struct bat_attribute **bat_attr;
690 int err;
691
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200692 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
693 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000694
695 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200696 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200697 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698 goto out;
699 }
700
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200701 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
703 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200704 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200705 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200706 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000707 goto rem_attr;
708 }
709 }
710
711 return 0;
712
713rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200714 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000715 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
716out:
717 return -ENOMEM;
718}
719
Sven Eckelmann5853e222012-05-12 02:09:24 +0200720void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721{
722 kobject_put(*hardif_obj);
723 *hardif_obj = NULL;
724}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200725
Sven Eckelmann5853e222012-05-12 02:09:24 +0200726int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
727 enum uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +0200728{
Sven Eckelmann5346c352012-05-05 13:27:28 +0200729 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200730 struct hard_iface *primary_if = NULL;
731 struct kobject *bat_kobj;
732 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
733
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200734 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200735 if (!primary_if)
736 goto out;
737
738 bat_kobj = &primary_if->soft_iface->dev.kobj;
739
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200740 uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200741 strlen(batadv_uev_type_str[type]) + 1,
Antonio Quartullic6bda682011-04-26 18:26:01 +0200742 GFP_ATOMIC);
743 if (!uevent_env[0])
744 goto out;
745
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200746 sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
747 batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200748
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200749 uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200750 strlen(batadv_uev_action_str[action]) + 1,
Antonio Quartullic6bda682011-04-26 18:26:01 +0200751 GFP_ATOMIC);
752 if (!uevent_env[1])
753 goto out;
754
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200755 sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200756 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200757
758 /* If the event is DEL, ignore the data field */
759 if (action != UEV_DEL) {
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200760 uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
Antonio Quartullic6bda682011-04-26 18:26:01 +0200761 strlen(data) + 1, GFP_ATOMIC);
762 if (!uevent_env[2])
763 goto out;
764
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200765 sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200766 }
767
768 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
769out:
770 kfree(uevent_env[0]);
771 kfree(uevent_env[1]);
772 kfree(uevent_env[2]);
773
774 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200775 batadv_hardif_free_ref(primary_if);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200776
777 if (ret)
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200778 batadv_dbg(DBG_BATMAN, bat_priv,
779 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200780 batadv_uev_type_str[type],
781 batadv_uev_action_str[action],
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200782 (action == UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200783 return ret;
784}