blob: 03b76a41ac4e1608ec19ceac17c9fe312477afb7 [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
Antonio Quartullic6bda682011-04-26 18:26:01 +020041#define UEV_TYPE_VAR "BATTYPE="
42#define UEV_ACTION_VAR "BATACTION="
43#define UEV_DATA_VAR "BATDATA="
44
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 */
56#define BAT_ATTR(_name, _mode, _show, _store) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020057struct bat_attribute batadv_attr_##_name = { \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000058 .attr = {.name = __stringify(_name), \
59 .mode = _mode }, \
60 .show = _show, \
61 .store = _store, \
62};
63
Marek Lindnerf245c382012-03-11 06:17:51 +080064#define BAT_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
Marek Lindnerf245c382012-03-11 06:17:51 +080075#define BAT_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 */
Marek Lindnerf245c382012-03-11 06:17:51 +080088#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 Eckelmann0ff9b862012-05-12 18:33:52 +020091 static BAT_ATTR(_name, _mode, batadv_show_##_name, \
92 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093
94
Marek Lindnerf245c382012-03-11 06:17:51 +080095#define BAT_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
Marek Lindnerf245c382012-03-11 06:17:51 +0800107#define BAT_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 */
Marek Lindnerf245c382012-03-11 06:17:51 +0800118#define BAT_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
119 static BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
120 static BAT_ATTR_SIF_SHOW_UINT(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200121 static BAT_ATTR(_name, _mode, batadv_show_##_name, \
122 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000123
124
Linus Luessing9d853f62012-03-11 06:17:52 +0800125#define BAT_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 Luessing9d853f62012-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 Luessing9d853f62012-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 Luessing9d853f62012-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 Luessing9d853f62012-03-11 06:17:52 +0800141 \
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200142 batadv_hardif_free_ref(hard_iface); \
Linus Luessing9d853f62012-03-11 06:17:52 +0800143 return length; \
144}
145
146#define BAT_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 Luessing9d853f62012-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 Luessing9d853f62012-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 Luessing9d853f62012-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 Luessing9d853f62012-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 */
Linus Luessing9d853f62012-03-11 06:17:52 +0800167#define BAT_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
168 static BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
169 static BAT_ATTR_HIF_SHOW_UINT(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200170 static BAT_ATTR(_name, _mode, batadv_show_##_name, \
171 batadv_store_##_name)
Linus Luessing9d853f62012-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) {
194 bat_info(net_dev,
195 "%s: Invalid parameter received: %s\n",
196 attr_name, buff);
197 return -EINVAL;
198 }
199
200 if (atomic_read(attr) == enabled)
201 return count;
202
203 bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
204 atomic_read(attr) == 1 ? "enabled" : "disabled",
205 enabled == 1 ? "enabled" : "disabled");
206
Eric Dumazet95c96172012-04-15 05:58:06 +0000207 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 return count;
209}
210
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200211static inline ssize_t
212__batadv_store_bool_attr(char *buff, size_t count,
213 void (*post_func)(struct net_device *),
214 struct attribute *attr,
215 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216{
217 int ret;
218
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200219 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
220 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221 if (post_func && ret)
222 post_func(net_dev);
223
224 return ret;
225}
226
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200227static int batadv_store_uint_attr(const char *buff, size_t count,
228 struct net_device *net_dev,
229 const char *attr_name,
230 unsigned int min, unsigned int max,
231 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000232{
233 unsigned long uint_val;
234 int ret;
235
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200236 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237 if (ret) {
238 bat_info(net_dev,
239 "%s: Invalid parameter received: %s\n",
240 attr_name, buff);
241 return -EINVAL;
242 }
243
244 if (uint_val < min) {
245 bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
246 attr_name, uint_val, min);
247 return -EINVAL;
248 }
249
250 if (uint_val > max) {
251 bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
252 attr_name, uint_val, max);
253 return -EINVAL;
254 }
255
256 if (atomic_read(attr) == uint_val)
257 return count;
258
259 bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
260 attr_name, atomic_read(attr), uint_val);
261
262 atomic_set(attr, uint_val);
263 return count;
264}
265
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200266static inline ssize_t
267__batadv_store_uint_attr(const char *buff, size_t count,
268 int min, int max,
269 void (*post_func)(struct net_device *),
270 const struct attribute *attr,
271 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272{
273 int ret;
274
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200275 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
276 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277 if (post_func && ret)
278 post_func(net_dev);
279
280 return ret;
281}
282
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200283static ssize_t batadv_show_vis_mode(struct kobject *kobj,
284 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200286 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 int vis_mode = atomic_read(&bat_priv->vis_mode);
288
289 return sprintf(buff, "%s\n",
290 vis_mode == VIS_TYPE_CLIENT_UPDATE ?
291 "client" : "server");
292}
293
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200294static ssize_t batadv_store_vis_mode(struct kobject *kobj,
295 struct attribute *attr, char *buff,
296 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200298 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000299 struct bat_priv *bat_priv = netdev_priv(net_dev);
300 unsigned long val;
301 int ret, vis_mode_tmp = -1;
302
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200303 ret = kstrtoul(buff, 10, &val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304
305 if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
306 (strncmp(buff, "client", 6) == 0) ||
307 (strncmp(buff, "off", 3) == 0))
308 vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
309
310 if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
311 (strncmp(buff, "server", 6) == 0))
312 vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
313
314 if (vis_mode_tmp < 0) {
315 if (buff[count - 1] == '\n')
316 buff[count - 1] = '\0';
317
318 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100319 "Invalid parameter for 'vis mode' setting received: %s\n",
320 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 return -EINVAL;
322 }
323
324 if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
325 return count;
326
327 bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
328 atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
329 "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
330 "client" : "server");
331
Eric Dumazet95c96172012-04-15 05:58:06 +0000332 atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333 return count;
334}
335
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200336static ssize_t batadv_show_bat_algo(struct kobject *kobj,
337 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800338{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200339 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800340 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
341}
342
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200343static void batadv_post_gw_deselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344{
345 struct bat_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200346 batadv_gw_deselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347}
348
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200349static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
350 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200352 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000353 int bytes_written;
354
355 switch (atomic_read(&bat_priv->gw_mode)) {
356 case GW_MODE_CLIENT:
357 bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
358 break;
359 case GW_MODE_SERVER:
360 bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
361 break;
362 default:
363 bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
364 break;
365 }
366
367 return bytes_written;
368}
369
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200370static ssize_t batadv_store_gw_mode(struct kobject *kobj,
371 struct attribute *attr, char *buff,
372 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200374 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 struct bat_priv *bat_priv = netdev_priv(net_dev);
376 char *curr_gw_mode_str;
377 int gw_mode_tmp = -1;
378
379 if (buff[count - 1] == '\n')
380 buff[count - 1] = '\0';
381
382 if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
383 gw_mode_tmp = GW_MODE_OFF;
384
385 if (strncmp(buff, GW_MODE_CLIENT_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100386 strlen(GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 gw_mode_tmp = GW_MODE_CLIENT;
388
389 if (strncmp(buff, GW_MODE_SERVER_NAME,
Sven Eckelmann7c64fd92012-02-28 10:55:36 +0100390 strlen(GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391 gw_mode_tmp = GW_MODE_SERVER;
392
393 if (gw_mode_tmp < 0) {
394 bat_info(net_dev,
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100395 "Invalid parameter for 'gw mode' setting received: %s\n",
396 buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397 return -EINVAL;
398 }
399
400 if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
401 return count;
402
403 switch (atomic_read(&bat_priv->gw_mode)) {
404 case GW_MODE_CLIENT:
405 curr_gw_mode_str = GW_MODE_CLIENT_NAME;
406 break;
407 case GW_MODE_SERVER:
408 curr_gw_mode_str = GW_MODE_SERVER_NAME;
409 break;
410 default:
411 curr_gw_mode_str = GW_MODE_OFF_NAME;
412 break;
413 }
414
415 bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
416 curr_gw_mode_str, buff);
417
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +0200418 batadv_gw_deselect(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000419 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420 return count;
421}
422
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200423static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
424 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000425{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200426 struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000427 int down, up;
428 int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
429
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200430 batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000431 return sprintf(buff, "%i%s/%i%s\n",
432 (down > 2048 ? down / 1024 : down),
433 (down > 2048 ? "MBit" : "KBit"),
434 (up > 2048 ? up / 1024 : up),
435 (up > 2048 ? "MBit" : "KBit"));
436}
437
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200438static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
439 struct attribute *attr, char *buff,
440 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000441{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200442 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443
444 if (buff[count - 1] == '\n')
445 buff[count - 1] = '\0';
446
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200447 return batadv_gw_bandwidth_set(net_dev, buff, count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448}
449
Marek Lindnerf245c382012-03-11 06:17:51 +0800450BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
451BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100452#ifdef CONFIG_BATMAN_ADV_BLA
Marek Lindnerf245c382012-03-11 06:17:51 +0800453BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100454#endif
Sven Eckelmann95638772012-05-12 02:09:31 +0200455BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
Marek Lindnerf245c382012-03-11 06:17:51 +0800456BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200457static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
458 batadv_store_vis_mode);
459static BAT_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
460static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
461 batadv_store_gw_mode);
Marek Lindnerf245c382012-03-11 06:17:51 +0800462BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
463BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
464BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200465 batadv_post_gw_deselect);
466static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
467 batadv_store_gw_bwidth);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000468#ifdef CONFIG_BATMAN_ADV_DEBUG
Antonio Quartullief3a4092012-05-09 09:50:45 +0200469BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470#endif
471
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200472static struct bat_attribute *batadv_mesh_attrs[] = {
473 &batadv_attr_aggregated_ogms,
474 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100475#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200476 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100477#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200478 &batadv_attr_fragmentation,
479 &batadv_attr_ap_isolation,
480 &batadv_attr_vis_mode,
481 &batadv_attr_routing_algo,
482 &batadv_attr_gw_mode,
483 &batadv_attr_orig_interval,
484 &batadv_attr_hop_penalty,
485 &batadv_attr_gw_sel_class,
486 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000487#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200488 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000489#endif
490 NULL,
491};
492
Sven Eckelmann5853e222012-05-12 02:09:24 +0200493int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
495 struct kobject *batif_kobject = &dev->dev.kobj;
496 struct bat_priv *bat_priv = netdev_priv(dev);
497 struct bat_attribute **bat_attr;
498 int err;
499
500 bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
501 batif_kobject);
502 if (!bat_priv->mesh_obj) {
503 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
504 SYSFS_IF_MESH_SUBDIR);
505 goto out;
506 }
507
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200508 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000509 err = sysfs_create_file(bat_priv->mesh_obj,
510 &((*bat_attr)->attr));
511 if (err) {
512 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
513 dev->name, SYSFS_IF_MESH_SUBDIR,
514 ((*bat_attr)->attr).name);
515 goto rem_attr;
516 }
517 }
518
519 return 0;
520
521rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200522 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000523 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
524
525 kobject_put(bat_priv->mesh_obj);
526 bat_priv->mesh_obj = NULL;
527out:
528 return -ENOMEM;
529}
530
Sven Eckelmann5853e222012-05-12 02:09:24 +0200531void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532{
533 struct bat_priv *bat_priv = netdev_priv(dev);
534 struct bat_attribute **bat_attr;
535
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200536 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000537 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
538
539 kobject_put(bat_priv->mesh_obj);
540 bat_priv->mesh_obj = NULL;
541}
542
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200543static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
544 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000545{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200546 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann95638772012-05-12 02:09:31 +0200547 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000548 ssize_t length;
549
Marek Lindnere6c10f42011-02-18 12:33:20 +0000550 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551 return 0;
552
Marek Lindnere6c10f42011-02-18 12:33:20 +0000553 length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
554 "none" : hard_iface->soft_iface->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000555
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200556 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557
558 return length;
559}
560
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200561static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
562 struct attribute *attr, char *buff,
563 size_t count)
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 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000568 int ret = count;
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 count;
572
573 if (buff[count - 1] == '\n')
574 buff[count - 1] = '\0';
575
576 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100577 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
578 buff);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200579 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000580 return -EINVAL;
581 }
582
583 if (strncmp(buff, "none", 4) == 0)
584 status_tmp = IF_NOT_IN_USE;
585 else
586 status_tmp = IF_I_WANT_YOU;
587
Marek Lindnere6c10f42011-02-18 12:33:20 +0000588 if (hard_iface->if_status == status_tmp)
589 goto out;
590
591 if ((hard_iface->soft_iface) &&
592 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000593 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200595 if (!rtnl_trylock()) {
596 ret = -ERESTARTSYS;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000597 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000598 }
599
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200600 if (status_tmp == IF_NOT_IN_USE) {
Sven Eckelmann95638772012-05-12 02:09:31 +0200601 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200602 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000603 }
604
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200605 /* if the interface already is in use */
606 if (hard_iface->if_status != IF_NOT_IN_USE)
Sven Eckelmann95638772012-05-12 02:09:31 +0200607 batadv_hardif_disable_interface(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608
Sven Eckelmann95638772012-05-12 02:09:31 +0200609 ret = batadv_hardif_enable_interface(hard_iface, buff);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200610
611unlock:
612 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000613out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200614 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615 return ret;
616}
617
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200618static ssize_t batadv_show_iface_status(struct kobject *kobj,
619 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200621 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann95638772012-05-12 02:09:31 +0200622 struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623 ssize_t length;
624
Marek Lindnere6c10f42011-02-18 12:33:20 +0000625 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000626 return 0;
627
Marek Lindnere6c10f42011-02-18 12:33:20 +0000628 switch (hard_iface->if_status) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000629 case IF_TO_BE_REMOVED:
630 length = sprintf(buff, "disabling\n");
631 break;
632 case IF_INACTIVE:
633 length = sprintf(buff, "inactive\n");
634 break;
635 case IF_ACTIVE:
636 length = sprintf(buff, "active\n");
637 break;
638 case IF_TO_BE_ACTIVATED:
639 length = sprintf(buff, "enabling\n");
640 break;
641 case IF_NOT_IN_USE:
642 default:
643 length = sprintf(buff, "not in use\n");
644 break;
645 }
646
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200647 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000648
649 return length;
650}
651
652static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200653 batadv_show_mesh_iface, batadv_store_mesh_iface);
654static BAT_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200656static struct bat_attribute *batadv_batman_attrs[] = {
657 &batadv_attr_mesh_iface,
658 &batadv_attr_iface_status,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659 NULL,
660};
661
Sven Eckelmann5853e222012-05-12 02:09:24 +0200662int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000663{
664 struct kobject *hardif_kobject = &dev->dev.kobj;
665 struct bat_attribute **bat_attr;
666 int err;
667
668 *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
669 hardif_kobject);
670
671 if (!*hardif_obj) {
672 bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
673 SYSFS_IF_BAT_SUBDIR);
674 goto out;
675 }
676
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200677 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
679 if (err) {
680 bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
681 dev->name, SYSFS_IF_BAT_SUBDIR,
682 ((*bat_attr)->attr).name);
683 goto rem_attr;
684 }
685 }
686
687 return 0;
688
689rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200690 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
692out:
693 return -ENOMEM;
694}
695
Sven Eckelmann5853e222012-05-12 02:09:24 +0200696void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000697{
698 kobject_put(*hardif_obj);
699 *hardif_obj = NULL;
700}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200701
Sven Eckelmann5853e222012-05-12 02:09:24 +0200702int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
703 enum uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +0200704{
Sven Eckelmann5346c352012-05-05 13:27:28 +0200705 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200706 struct hard_iface *primary_if = NULL;
707 struct kobject *bat_kobj;
708 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
709
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200710 primary_if = batadv_primary_if_get_selected(bat_priv);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200711 if (!primary_if)
712 goto out;
713
714 bat_kobj = &primary_if->soft_iface->dev.kobj;
715
716 uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200717 strlen(batadv_uev_type_str[type]) + 1,
Antonio Quartullic6bda682011-04-26 18:26:01 +0200718 GFP_ATOMIC);
719 if (!uevent_env[0])
720 goto out;
721
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200722 sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200723
724 uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200725 strlen(batadv_uev_action_str[action]) + 1,
Antonio Quartullic6bda682011-04-26 18:26:01 +0200726 GFP_ATOMIC);
727 if (!uevent_env[1])
728 goto out;
729
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200730 sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR,
731 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200732
733 /* If the event is DEL, ignore the data field */
734 if (action != UEV_DEL) {
735 uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
736 strlen(data) + 1, GFP_ATOMIC);
737 if (!uevent_env[2])
738 goto out;
739
740 sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
741 }
742
743 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
744out:
745 kfree(uevent_env[0]);
746 kfree(uevent_env[1]);
747 kfree(uevent_env[2]);
748
749 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200750 batadv_hardif_free_ref(primary_if);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200751
752 if (ret)
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200753 batadv_dbg(DBG_BATMAN, bat_priv,
754 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200755 batadv_uev_type_str[type],
756 batadv_uev_action_str[action],
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200757 (action == UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200758 return ret;
759}