blob: a75dc12f96f8c75432ad1c709e6813748bf7506b [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2010-2014 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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
18#include "main.h"
Sven Eckelmannb706b132012-06-10 23:58:51 +020019#include "sysfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000020#include "translation-table.h"
Antonio Quartulli33af49a2012-08-08 18:50:57 +020021#include "distributed-arp-table.h"
Marek Lindner3f4841f2013-04-23 21:40:00 +080022#include "network-coding.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "originator.h"
24#include "hard-interface.h"
Antonio Quartulli90f44352013-07-02 11:04:35 +020025#include "soft-interface.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000026#include "gateway_common.h"
27#include "gateway_client.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028
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);
Antonio Quartullif1386942014-05-10 18:56:37 +020032
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020033 return to_net_dev(dev);
34}
35
Sven Eckelmann56303d32012-06-05 22:31:31 +020036static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020037{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020038 struct net_device *net_dev = batadv_kobj_to_netdev(obj);
Antonio Quartullif1386942014-05-10 18:56:37 +020039
Sven Eckelmann3d222bb2011-06-05 10:20:19 +020040 return netdev_priv(net_dev);
41}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
Antonio Quartulli90f44352013-07-02 11:04:35 +020043/**
44 * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
45 * @obj: kobject to covert
46 *
47 * Returns the associated batadv_priv struct.
48 */
49static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
50{
51 /* VLAN specific attributes are located in the root sysfs folder if they
52 * refer to the untagged VLAN..
53 */
54 if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
55 return batadv_kobj_to_batpriv(obj);
56
57 /* ..while the attributes for the tagged vlans are located in
58 * the in the corresponding "vlan%VID" subfolder
59 */
60 return batadv_kobj_to_batpriv(obj->parent);
61}
62
63/**
64 * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
65 * @obj: kobject to covert
66 *
67 * Returns the associated softif_vlan struct if found, NULL otherwise.
68 */
69static struct batadv_softif_vlan *
70batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
71{
72 struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
73
74 rcu_read_lock();
75 hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
76 if (vlan_tmp->kobj != obj)
77 continue;
78
79 if (!atomic_inc_not_zero(&vlan_tmp->refcount))
80 continue;
81
82 vlan = vlan_tmp;
83 break;
84 }
85 rcu_read_unlock();
86
87 return vlan;
88}
89
Sven Eckelmann347c80f2012-06-03 22:19:07 +020090#define BATADV_UEV_TYPE_VAR "BATTYPE="
91#define BATADV_UEV_ACTION_VAR "BATACTION="
92#define BATADV_UEV_DATA_VAR "BATDATA="
Antonio Quartullic6bda682011-04-26 18:26:01 +020093
Sven Eckelmann0ff9b862012-05-12 18:33:52 +020094static char *batadv_uev_action_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +020095 "add",
96 "del",
97 "change"
98};
99
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200100static char *batadv_uev_type_str[] = {
Antonio Quartullic6bda682011-04-26 18:26:01 +0200101 "gw"
102};
103
Antonio Quartulli90f44352013-07-02 11:04:35 +0200104/* Use this, if you have customized show and store functions for vlan attrs */
105#define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
106struct batadv_attribute batadv_attr_vlan_##_name = { \
107 .attr = {.name = __stringify(_name), \
108 .mode = _mode }, \
109 .show = _show, \
110 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200111}
Antonio Quartulli90f44352013-07-02 11:04:35 +0200112
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113/* Use this, if you have customized show and store functions */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200114#define BATADV_ATTR(_name, _mode, _show, _store) \
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200115struct batadv_attribute batadv_attr_##_name = { \
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200116 .attr = {.name = __stringify(_name), \
117 .mode = _mode }, \
118 .show = _show, \
119 .store = _store, \
Antonio Quartulli2b64df22014-05-15 11:24:26 +0200120}
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200122#define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200123ssize_t batadv_store_##_name(struct kobject *kobj, \
124 struct attribute *attr, char *buff, \
125 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200127 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200128 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200129 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200130 return __batadv_store_bool_attr(buff, count, _post_func, attr, \
131 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132}
133
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200134#define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200135ssize_t batadv_show_##_name(struct kobject *kobj, \
136 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200138 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200139 \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140 return sprintf(buff, "%s\n", \
141 atomic_read(&bat_priv->_name) == 0 ? \
142 "disabled" : "enabled"); \
143} \
144
Marek Lindnerf245c382012-03-11 06:17:51 +0800145/* Use this, if you are going to turn a [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200146 * (bat_priv) on or off
147 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200148#define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
149 static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
150 static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
151 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
152 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200154#define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200155ssize_t batadv_store_##_name(struct kobject *kobj, \
156 struct attribute *attr, char *buff, \
157 size_t count) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158{ \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200159 struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160 struct batadv_priv *bat_priv = netdev_priv(net_dev); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200161 \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200162 return __batadv_store_uint_attr(buff, count, _min, _max, \
163 _post_func, attr, \
164 &bat_priv->_name, net_dev); \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165}
166
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200167#define BATADV_ATTR_SIF_SHOW_UINT(_name) \
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200168ssize_t batadv_show_##_name(struct kobject *kobj, \
169 struct attribute *attr, char *buff) \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170{ \
Sven Eckelmann56303d32012-06-05 22:31:31 +0200171 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200172 \
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173 return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
174} \
175
Marek Lindnerf245c382012-03-11 06:17:51 +0800176/* Use this, if you are going to set [name] in the soft-interface
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200177 * (bat_priv) to an unsigned integer value
178 */
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200179#define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
180 static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
181 static BATADV_ATTR_SIF_SHOW_UINT(_name) \
182 static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
183 batadv_store_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184
Antonio Quartulli90f44352013-07-02 11:04:35 +0200185#define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
186ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
187 struct attribute *attr, char *buff, \
188 size_t count) \
189{ \
190 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
191 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
192 kobj); \
193 size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
194 attr, &vlan->_name, \
195 bat_priv->soft_iface); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200196 \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200197 batadv_softif_vlan_free_ref(vlan); \
198 return res; \
199}
200
201#define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
202ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
203 struct attribute *attr, char *buff) \
204{ \
205 struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
206 struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
207 kobj); \
208 size_t res = sprintf(buff, "%s\n", \
209 atomic_read(&vlan->_name) == 0 ? \
210 "disabled" : "enabled"); \
Antonio Quartullif1386942014-05-10 18:56:37 +0200211 \
Antonio Quartulli90f44352013-07-02 11:04:35 +0200212 batadv_softif_vlan_free_ref(vlan); \
213 return res; \
214}
215
216/* Use this, if you are going to turn a [name] in the vlan struct on or off */
217#define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
218 static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
219 static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
220 static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
221 batadv_store_vlan_##_name)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200223static int batadv_store_bool_attr(char *buff, size_t count,
224 struct net_device *net_dev,
225 const char *attr_name, atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226{
227 int enabled = -1;
228
229 if (buff[count - 1] == '\n')
230 buff[count - 1] = '\0';
231
232 if ((strncmp(buff, "1", 2) == 0) ||
233 (strncmp(buff, "enable", 7) == 0) ||
234 (strncmp(buff, "enabled", 8) == 0))
235 enabled = 1;
236
237 if ((strncmp(buff, "0", 2) == 0) ||
238 (strncmp(buff, "disable", 8) == 0) ||
239 (strncmp(buff, "disabled", 9) == 0))
240 enabled = 0;
241
242 if (enabled < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200243 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
244 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 return -EINVAL;
246 }
247
248 if (atomic_read(attr) == enabled)
249 return count;
250
Sven Eckelmann3e348192012-05-16 20:23:22 +0200251 batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
252 atomic_read(attr) == 1 ? "enabled" : "disabled",
253 enabled == 1 ? "enabled" : "disabled");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Eric Dumazet95c96172012-04-15 05:58:06 +0000255 atomic_set(attr, (unsigned int)enabled);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 return count;
257}
258
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200259static inline ssize_t
260__batadv_store_bool_attr(char *buff, size_t count,
261 void (*post_func)(struct net_device *),
262 struct attribute *attr,
263 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000264{
265 int ret;
266
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200267 ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
268 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269 if (post_func && ret)
270 post_func(net_dev);
271
272 return ret;
273}
274
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200275static int batadv_store_uint_attr(const char *buff, size_t count,
276 struct net_device *net_dev,
277 const char *attr_name,
278 unsigned int min, unsigned int max,
279 atomic_t *attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280{
281 unsigned long uint_val;
282 int ret;
283
Sven Eckelmann25a92b12011-09-30 13:32:01 +0200284 ret = kstrtoul(buff, 10, &uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 if (ret) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200286 batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
287 attr_name, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288 return -EINVAL;
289 }
290
291 if (uint_val < min) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200292 batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
293 attr_name, uint_val, min);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 return -EINVAL;
295 }
296
297 if (uint_val > max) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200298 batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
299 attr_name, uint_val, max);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300 return -EINVAL;
301 }
302
303 if (atomic_read(attr) == uint_val)
304 return count;
305
Sven Eckelmann3e348192012-05-16 20:23:22 +0200306 batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
307 attr_name, atomic_read(attr), uint_val);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
309 atomic_set(attr, uint_val);
310 return count;
311}
312
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200313static inline ssize_t
314__batadv_store_uint_attr(const char *buff, size_t count,
315 int min, int max,
316 void (*post_func)(struct net_device *),
317 const struct attribute *attr,
318 atomic_t *attr_store, struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319{
320 int ret;
321
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200322 ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
323 attr_store);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 if (post_func && ret)
325 post_func(net_dev);
326
327 return ret;
328}
329
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200330static ssize_t batadv_show_bat_algo(struct kobject *kobj,
331 struct attribute *attr, char *buff)
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800332{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200333 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Antonio Quartullif1386942014-05-10 18:56:37 +0200334
Marek Lindnerea3d2fd2011-11-29 00:15:37 +0800335 return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
336}
337
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100338static void batadv_post_gw_reselect(struct net_device *net_dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200340 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Antonio Quartullif1386942014-05-10 18:56:37 +0200341
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100342 batadv_gw_reselect(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000343}
344
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200345static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
346 char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200348 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000349 int bytes_written;
350
351 switch (atomic_read(&bat_priv->gw_mode)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200352 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200353 bytes_written = sprintf(buff, "%s\n",
354 BATADV_GW_MODE_CLIENT_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000355 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200356 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200357 bytes_written = sprintf(buff, "%s\n",
358 BATADV_GW_MODE_SERVER_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359 break;
360 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200361 bytes_written = sprintf(buff, "%s\n",
362 BATADV_GW_MODE_OFF_NAME);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363 break;
364 }
365
366 return bytes_written;
367}
368
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200369static ssize_t batadv_store_gw_mode(struct kobject *kobj,
370 struct attribute *attr, char *buff,
371 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000372{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200373 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200374 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000375 char *curr_gw_mode_str;
376 int gw_mode_tmp = -1;
377
378 if (buff[count - 1] == '\n')
379 buff[count - 1] = '\0';
380
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200381 if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
382 strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200383 gw_mode_tmp = BATADV_GW_MODE_OFF;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200385 if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
386 strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200387 gw_mode_tmp = BATADV_GW_MODE_CLIENT;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000388
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200389 if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
390 strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200391 gw_mode_tmp = BATADV_GW_MODE_SERVER;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
393 if (gw_mode_tmp < 0) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200394 batadv_info(net_dev,
395 "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)) {
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200404 case BATADV_GW_MODE_CLIENT:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200405 curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000406 break;
Sven Eckelmanncd646ab2012-06-03 22:19:18 +0200407 case BATADV_GW_MODE_SERVER:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200408 curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000409 break;
410 default:
Sven Eckelmann97ea4ba2012-06-03 22:19:11 +0200411 curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000412 break;
413 }
414
Sven Eckelmann3e348192012-05-16 20:23:22 +0200415 batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
416 curr_gw_mode_str, buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
Antonio Quartullif3163182013-11-04 20:59:40 +0100418 /* Invoking batadv_gw_reselect() is not enough to really de-select the
419 * current GW. It will only instruct the gateway client code to perform
420 * a re-election the next time that this is needed.
421 *
422 * When gw client mode is being switched off the current GW must be
423 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
424 * client mode re-activation. This is operation is performed in
425 * batadv_gw_check_client_stop().
426 */
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100427 batadv_gw_reselect(bat_priv);
Antonio Quartullic6eaa3f2013-07-13 00:06:00 +0200428 /* always call batadv_gw_check_client_stop() before changing the gateway
429 * state
430 */
431 batadv_gw_check_client_stop(bat_priv);
Eric Dumazet95c96172012-04-15 05:58:06 +0000432 atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
Marek Lindner414254e2013-04-23 21:39:58 +0800433 batadv_gw_tvlv_container_update(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000434 return count;
435}
436
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200437static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
438 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000439{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200440 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
Marek Lindner414254e2013-04-23 21:39:58 +0800441 uint32_t down, up;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000442
Marek Lindner414254e2013-04-23 21:39:58 +0800443 down = atomic_read(&bat_priv->gw.bandwidth_down);
444 up = atomic_read(&bat_priv->gw.bandwidth_up);
445
446 return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
447 down % 10, up / 10, up % 10);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448}
449
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200450static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
451 struct attribute *attr, char *buff,
452 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000453{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200454 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455
456 if (buff[count - 1] == '\n')
457 buff[count - 1] = '\0';
458
Sven Eckelmann84d5e5e2012-05-12 02:09:30 +0200459 return batadv_gw_bandwidth_set(net_dev, buff, count);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000460}
461
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100462/**
463 * batadv_show_isolation_mark - print the current isolation mark/mask
464 * @kobj: kobject representing the private mesh sysfs directory
465 * @attr: the batman-adv attribute the user is interacting with
466 * @buff: the buffer that will contain the data to send back to the user
467 *
468 * Returns the number of bytes written into 'buff' on success or a negative
469 * error code in case of failure
470 */
471static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
472 struct attribute *attr, char *buff)
473{
474 struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
475
476 return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
477 bat_priv->isolation_mark_mask);
478}
479
480/**
481 * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
482 * by the user
483 * @kobj: kobject representing the private mesh sysfs directory
484 * @attr: the batman-adv attribute the user is interacting with
485 * @buff: the buffer containing the user data
486 * @count: number of bytes in the buffer
487 *
488 * Returns 'count' on success or a negative error code in case of failure
489 */
490static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
491 struct attribute *attr, char *buff,
492 size_t count)
493{
494 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
495 struct batadv_priv *bat_priv = netdev_priv(net_dev);
496 uint32_t mark, mask;
497 char *mask_ptr;
498
499 /* parse the mask if it has been specified, otherwise assume the mask is
500 * the biggest possible
501 */
502 mask = 0xFFFFFFFF;
503 mask_ptr = strchr(buff, '/');
504 if (mask_ptr) {
505 *mask_ptr = '\0';
506 mask_ptr++;
507
508 /* the mask must be entered in hex base as it is going to be a
509 * bitmask and not a prefix length
510 */
511 if (kstrtou32(mask_ptr, 16, &mask) < 0)
512 return -EINVAL;
513 }
514
515 /* the mark can be entered in any base */
516 if (kstrtou32(buff, 0, &mark) < 0)
517 return -EINVAL;
518
519 bat_priv->isolation_mark_mask = mask;
520 /* erase bits not covered by the mask */
521 bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
522
523 batadv_info(net_dev,
524 "New skb mark for extended isolation: %#.8x/%#.8x\n",
525 bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
526
527 return count;
528}
529
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200530BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
531BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100532#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200533BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100534#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200535#ifdef CONFIG_BATMAN_ADV_DAT
Marek Lindner17cf0ea2013-04-23 21:39:59 +0800536BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
537 batadv_dat_status_update);
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200538#endif
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200539BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200540static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
541static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
542 batadv_store_gw_mode);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200543BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
544 INT_MAX, NULL);
545BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200546 NULL);
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200547BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
Antonio Quartulli4e820e72013-11-04 20:59:41 +0100548 batadv_post_gw_reselect);
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200549static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
550 batadv_store_gw_bwidth);
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100551#ifdef CONFIG_BATMAN_ADV_MCAST
552BATADV_ATTR_SIF_BOOL(multicast_mode, S_IRUGO | S_IWUSR, NULL);
553#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000554#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200555BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000556#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100557#ifdef CONFIG_BATMAN_ADV_NC
Marek Lindner3f4841f2013-04-23 21:40:00 +0800558BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
559 batadv_nc_status_update);
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100560#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100561static BATADV_ATTR(isolation_mark, S_IRUGO | S_IWUSR,
562 batadv_show_isolation_mark, batadv_store_isolation_mark);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200564static struct batadv_attribute *batadv_mesh_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200565 &batadv_attr_aggregated_ogms,
566 &batadv_attr_bonding,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100567#ifdef CONFIG_BATMAN_ADV_BLA
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200568 &batadv_attr_bridge_loop_avoidance,
Simon Wunderlich7a5cc242012-01-22 20:00:27 +0100569#endif
Antonio Quartulli33af49a2012-08-08 18:50:57 +0200570#ifdef CONFIG_BATMAN_ADV_DAT
571 &batadv_attr_distributed_arp_table,
572#endif
Linus Lüssing1d8ab8d2014-02-15 17:47:52 +0100573#ifdef CONFIG_BATMAN_ADV_MCAST
574 &batadv_attr_multicast_mode,
575#endif
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200576 &batadv_attr_fragmentation,
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200577 &batadv_attr_routing_algo,
578 &batadv_attr_gw_mode,
579 &batadv_attr_orig_interval,
580 &batadv_attr_hop_penalty,
581 &batadv_attr_gw_sel_class,
582 &batadv_attr_gw_bandwidth,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583#ifdef CONFIG_BATMAN_ADV_DEBUG
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200584 &batadv_attr_log_level,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000585#endif
Martin Hundebølld353d8d2013-01-25 11:12:38 +0100586#ifdef CONFIG_BATMAN_ADV_NC
587 &batadv_attr_network_coding,
588#endif
Antonio Quartullic42edfe2013-11-16 12:03:47 +0100589 &batadv_attr_isolation_mark,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590 NULL,
591};
592
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200593BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
594
Antonio Quartulli90f44352013-07-02 11:04:35 +0200595/**
596 * batadv_vlan_attrs - array of vlan specific sysfs attributes
597 */
598static struct batadv_attribute *batadv_vlan_attrs[] = {
Antonio Quartullib8cbd812013-07-02 11:04:36 +0200599 &batadv_attr_vlan_ap_isolation,
Antonio Quartulli90f44352013-07-02 11:04:35 +0200600 NULL,
601};
602
Sven Eckelmann5853e222012-05-12 02:09:24 +0200603int batadv_sysfs_add_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604{
605 struct kobject *batif_kobject = &dev->dev.kobj;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200606 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200607 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000608 int err;
609
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200610 bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 batif_kobject);
612 if (!bat_priv->mesh_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200613 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200614 BATADV_SYSFS_IF_MESH_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615 goto out;
616 }
617
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200618 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 err = sysfs_create_file(bat_priv->mesh_obj,
620 &((*bat_attr)->attr));
621 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200622 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200623 dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200624 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 goto rem_attr;
626 }
627 }
628
629 return 0;
630
631rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200632 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
634
635 kobject_put(bat_priv->mesh_obj);
636 bat_priv->mesh_obj = NULL;
637out:
638 return -ENOMEM;
639}
640
Sven Eckelmann5853e222012-05-12 02:09:24 +0200641void batadv_sysfs_del_meshif(struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200643 struct batadv_priv *bat_priv = netdev_priv(dev);
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200644 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200646 for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647 sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
648
649 kobject_put(bat_priv->mesh_obj);
650 bat_priv->mesh_obj = NULL;
651}
652
Antonio Quartulli90f44352013-07-02 11:04:35 +0200653/**
654 * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
655 * @dev: netdev of the mesh interface
656 * @vlan: private data of the newly added VLAN interface
657 *
658 * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
659 */
660int batadv_sysfs_add_vlan(struct net_device *dev,
661 struct batadv_softif_vlan *vlan)
662{
663 char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
664 struct batadv_priv *bat_priv = netdev_priv(dev);
665 struct batadv_attribute **bat_attr;
666 int err;
667
668 if (vlan->vid & BATADV_VLAN_HAS_TAG) {
669 sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
670 vlan->vid & VLAN_VID_MASK);
671
672 vlan->kobj = kobject_create_and_add(vlan_subdir,
673 bat_priv->mesh_obj);
674 if (!vlan->kobj) {
675 batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
676 dev->name, vlan_subdir);
677 goto out;
678 }
679 } else {
680 /* the untagged LAN uses the root folder to store its "VLAN
681 * specific attributes"
682 */
683 vlan->kobj = bat_priv->mesh_obj;
684 kobject_get(bat_priv->mesh_obj);
685 }
686
687 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
688 err = sysfs_create_file(vlan->kobj,
689 &((*bat_attr)->attr));
690 if (err) {
691 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
692 dev->name, vlan_subdir,
693 ((*bat_attr)->attr).name);
694 goto rem_attr;
695 }
696 }
697
698 return 0;
699
700rem_attr:
701 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
702 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
703
704 kobject_put(vlan->kobj);
705 vlan->kobj = NULL;
706out:
707 return -ENOMEM;
708}
709
710/**
711 * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
712 * @bat_priv: the bat priv with all the soft interface information
713 * @vlan: the private data of the VLAN to destroy
714 */
715void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
716 struct batadv_softif_vlan *vlan)
717{
718 struct batadv_attribute **bat_attr;
719
720 for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
721 sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
722
723 kobject_put(vlan->kobj);
724 vlan->kobj = NULL;
725}
726
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200727static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
728 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200730 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200731 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732 ssize_t length;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200733 const char *ifname;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000734
Sven Eckelmann56303d32012-06-05 22:31:31 +0200735 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000736 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737 return 0;
738
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200739 if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
740 ifname = "none";
741 else
742 ifname = hard_iface->soft_iface->name;
743
744 length = sprintf(buff, "%s\n", ifname);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000745
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200746 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747
748 return length;
749}
750
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200751static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
752 struct attribute *attr, char *buff,
753 size_t count)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200755 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200756 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000757 int status_tmp = -1;
Marek Lindnered75ccb2011-02-10 14:33:51 +0000758 int ret = count;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000759
Sven Eckelmann56303d32012-06-05 22:31:31 +0200760 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000761 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000762 return count;
763
764 if (buff[count - 1] == '\n')
765 buff[count - 1] = '\0';
766
767 if (strlen(buff) >= IFNAMSIZ) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +0100768 pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
769 buff);
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200770 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771 return -EINVAL;
772 }
773
774 if (strncmp(buff, "none", 4) == 0)
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200775 status_tmp = BATADV_IF_NOT_IN_USE;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776 else
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200777 status_tmp = BATADV_IF_I_WANT_YOU;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778
Marek Lindnere6c10f42011-02-18 12:33:20 +0000779 if (hard_iface->if_status == status_tmp)
780 goto out;
781
782 if ((hard_iface->soft_iface) &&
783 (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
Marek Lindnered75ccb2011-02-10 14:33:51 +0000784 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
Matthias Schifferac16d142013-05-28 17:32:32 +0200786 rtnl_lock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200788 if (status_tmp == BATADV_IF_NOT_IN_USE) {
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800789 batadv_hardif_disable_interface(hard_iface,
790 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200791 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000792 }
793
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200794 /* if the interface already is in use */
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200795 if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
Sven Eckelmanna15fd362013-02-11 17:10:24 +0800796 batadv_hardif_disable_interface(hard_iface,
797 BATADV_IF_CLEANUP_AUTO);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798
Sven Eckelmann95638772012-05-12 02:09:31 +0200799 ret = batadv_hardif_enable_interface(hard_iface, buff);
Sven Eckelmann3a4375a2011-05-03 13:10:06 +0200800
801unlock:
802 rtnl_unlock();
Marek Lindnered75ccb2011-02-10 14:33:51 +0000803out:
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200804 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805 return ret;
806}
807
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200808static ssize_t batadv_show_iface_status(struct kobject *kobj,
809 struct attribute *attr, char *buff)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000810{
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200811 struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
Sven Eckelmann56303d32012-06-05 22:31:31 +0200812 struct batadv_hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813 ssize_t length;
814
Sven Eckelmann56303d32012-06-05 22:31:31 +0200815 hard_iface = batadv_hardif_get_by_netdev(net_dev);
Marek Lindnere6c10f42011-02-18 12:33:20 +0000816 if (!hard_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817 return 0;
818
Marek Lindnere6c10f42011-02-18 12:33:20 +0000819 switch (hard_iface->if_status) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200820 case BATADV_IF_TO_BE_REMOVED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821 length = sprintf(buff, "disabling\n");
822 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200823 case BATADV_IF_INACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824 length = sprintf(buff, "inactive\n");
825 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200826 case BATADV_IF_ACTIVE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827 length = sprintf(buff, "active\n");
828 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200829 case BATADV_IF_TO_BE_ACTIVATED:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000830 length = sprintf(buff, "enabling\n");
831 break;
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200832 case BATADV_IF_NOT_IN_USE:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833 default:
834 length = sprintf(buff, "not in use\n");
835 break;
836 }
837
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200838 batadv_hardif_free_ref(hard_iface);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839
840 return length;
841}
842
Sven Eckelmann347c80f2012-06-03 22:19:07 +0200843static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
844 batadv_store_mesh_iface);
845static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200847static struct batadv_attribute *batadv_batman_attrs[] = {
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200848 &batadv_attr_mesh_iface,
849 &batadv_attr_iface_status,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000850 NULL,
851};
852
Sven Eckelmann5853e222012-05-12 02:09:24 +0200853int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000854{
855 struct kobject *hardif_kobject = &dev->dev.kobj;
Sven Eckelmannb4d66b82012-06-05 22:31:29 +0200856 struct batadv_attribute **bat_attr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857 int err;
858
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200859 *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
860 hardif_kobject);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
862 if (!*hardif_obj) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200863 batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200864 BATADV_SYSFS_IF_BAT_SUBDIR);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865 goto out;
866 }
867
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200868 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000869 err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
870 if (err) {
Sven Eckelmann3e348192012-05-16 20:23:22 +0200871 batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
Sven Eckelmann036cbfe2012-06-03 22:19:09 +0200872 dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
Sven Eckelmann3e348192012-05-16 20:23:22 +0200873 ((*bat_attr)->attr).name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000874 goto rem_attr;
875 }
876 }
877
878 return 0;
879
880rem_attr:
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200881 for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882 sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
883out:
884 return -ENOMEM;
885}
886
Sven Eckelmann5853e222012-05-12 02:09:24 +0200887void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000888{
889 kobject_put(*hardif_obj);
890 *hardif_obj = NULL;
891}
Antonio Quartullic6bda682011-04-26 18:26:01 +0200892
Sven Eckelmann56303d32012-06-05 22:31:31 +0200893int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200894 enum batadv_uev_action action, const char *data)
Antonio Quartullic6bda682011-04-26 18:26:01 +0200895{
Sven Eckelmann5346c352012-05-05 13:27:28 +0200896 int ret = -ENOMEM;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200897 struct kobject *bat_kobj;
898 char *uevent_env[4] = { NULL, NULL, NULL, NULL };
899
Marek Lindner736292c2013-01-12 19:19:06 +0800900 bat_kobj = &bat_priv->soft_iface->dev.kobj;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200901
Himangi Saraogib98fe242014-06-29 00:06:29 +0530902 uevent_env[0] = kasprintf(GFP_ATOMIC,
903 "%s%s", BATADV_UEV_TYPE_VAR,
904 batadv_uev_type_str[type]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200905 if (!uevent_env[0])
906 goto out;
907
Himangi Saraogib98fe242014-06-29 00:06:29 +0530908 uevent_env[1] = kasprintf(GFP_ATOMIC,
909 "%s%s", BATADV_UEV_ACTION_VAR,
910 batadv_uev_action_str[action]);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200911 if (!uevent_env[1])
912 goto out;
913
Antonio Quartullic6bda682011-04-26 18:26:01 +0200914 /* If the event is DEL, ignore the data field */
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200915 if (action != BATADV_UEV_DEL) {
Himangi Saraogib98fe242014-06-29 00:06:29 +0530916 uevent_env[2] = kasprintf(GFP_ATOMIC,
917 "%s%s", BATADV_UEV_DATA_VAR, data);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200918 if (!uevent_env[2])
919 goto out;
Antonio Quartullic6bda682011-04-26 18:26:01 +0200920 }
921
922 ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
923out:
924 kfree(uevent_env[0]);
925 kfree(uevent_env[1]);
926 kfree(uevent_env[2]);
927
Antonio Quartullic6bda682011-04-26 18:26:01 +0200928 if (ret)
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200929 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200930 "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
Sven Eckelmann0ff9b862012-05-12 18:33:52 +0200931 batadv_uev_type_str[type],
932 batadv_uev_action_str[action],
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200933 (action == BATADV_UEV_DEL ? "NULL" : data), ret);
Antonio Quartullic6bda682011-04-26 18:26:01 +0200934 return ret;
935}