• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (C) 2010-2017  B.A.T.M.A.N. contributors:
2  *
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, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "sysfs.h"
19 #include "main.h"
20 
21 #include <linux/atomic.h>
22 #include <linux/compiler.h>
23 #include <linux/device.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/if.h>
27 #include <linux/if_vlan.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/netdevice.h>
31 #include <linux/printk.h>
32 #include <linux/rculist.h>
33 #include <linux/rcupdate.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/slab.h>
36 #include <linux/stddef.h>
37 #include <linux/string.h>
38 #include <linux/stringify.h>
39 #include <linux/workqueue.h>
40 
41 #include "bridge_loop_avoidance.h"
42 #include "distributed-arp-table.h"
43 #include "gateway_client.h"
44 #include "gateway_common.h"
45 #include "hard-interface.h"
46 #include "log.h"
47 #include "network-coding.h"
48 #include "packet.h"
49 #include "soft-interface.h"
50 
batadv_kobj_to_netdev(struct kobject * obj)51 static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
52 {
53 	struct device *dev = container_of(obj->parent, struct device, kobj);
54 
55 	return to_net_dev(dev);
56 }
57 
batadv_kobj_to_batpriv(struct kobject * obj)58 static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
59 {
60 	struct net_device *net_dev = batadv_kobj_to_netdev(obj);
61 
62 	return netdev_priv(net_dev);
63 }
64 
65 /**
66  * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
67  * @obj: kobject to covert
68  *
69  * Return: the associated batadv_priv struct.
70  */
batadv_vlan_kobj_to_batpriv(struct kobject * obj)71 static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
72 {
73 	/* VLAN specific attributes are located in the root sysfs folder if they
74 	 * refer to the untagged VLAN..
75 	 */
76 	if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
77 		return batadv_kobj_to_batpriv(obj);
78 
79 	/* ..while the attributes for the tagged vlans are located in
80 	 * the in the corresponding "vlan%VID" subfolder
81 	 */
82 	return batadv_kobj_to_batpriv(obj->parent);
83 }
84 
85 /**
86  * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
87  * @bat_priv: the bat priv with all the soft interface information
88  * @obj: kobject to covert
89  *
90  * Return: the associated softif_vlan struct if found, NULL otherwise.
91  */
92 static struct batadv_softif_vlan *
batadv_kobj_to_vlan(struct batadv_priv * bat_priv,struct kobject * obj)93 batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
94 {
95 	struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
96 
97 	rcu_read_lock();
98 	hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
99 		if (vlan_tmp->kobj != obj)
100 			continue;
101 
102 		if (!kref_get_unless_zero(&vlan_tmp->refcount))
103 			continue;
104 
105 		vlan = vlan_tmp;
106 		break;
107 	}
108 	rcu_read_unlock();
109 
110 	return vlan;
111 }
112 
113 #define BATADV_UEV_TYPE_VAR	"BATTYPE="
114 #define BATADV_UEV_ACTION_VAR	"BATACTION="
115 #define BATADV_UEV_DATA_VAR	"BATDATA="
116 
117 static char *batadv_uev_action_str[] = {
118 	"add",
119 	"del",
120 	"change",
121 	"loopdetect",
122 };
123 
124 static char *batadv_uev_type_str[] = {
125 	"gw",
126 	"bla",
127 };
128 
129 /* Use this, if you have customized show and store functions for vlan attrs */
130 #define BATADV_ATTR_VLAN(_name, _mode, _show, _store)	\
131 struct batadv_attribute batadv_attr_vlan_##_name = {	\
132 	.attr = {.name = __stringify(_name),		\
133 		 .mode = _mode },			\
134 	.show   = _show,				\
135 	.store  = _store,				\
136 }
137 
138 /* Use this, if you have customized show and store functions */
139 #define BATADV_ATTR(_name, _mode, _show, _store)	\
140 struct batadv_attribute batadv_attr_##_name = {		\
141 	.attr = {.name = __stringify(_name),		\
142 		 .mode = _mode },			\
143 	.show   = _show,				\
144 	.store  = _store,				\
145 }
146 
147 #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)			\
148 ssize_t batadv_store_##_name(struct kobject *kobj,			\
149 			     struct attribute *attr, char *buff,	\
150 			     size_t count)				\
151 {									\
152 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
153 	struct batadv_priv *bat_priv = netdev_priv(net_dev);		\
154 									\
155 	return __batadv_store_bool_attr(buff, count, _post_func, attr,	\
156 					&bat_priv->_name, net_dev);	\
157 }
158 
159 #define BATADV_ATTR_SIF_SHOW_BOOL(_name)				\
160 ssize_t batadv_show_##_name(struct kobject *kobj,			\
161 			    struct attribute *attr, char *buff)		\
162 {									\
163 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);	\
164 									\
165 	return sprintf(buff, "%s\n",					\
166 		       atomic_read(&bat_priv->_name) == 0 ?		\
167 		       "disabled" : "enabled");				\
168 }									\
169 
170 /* Use this, if you are going to turn a [name] in the soft-interface
171  * (bat_priv) on or off
172  */
173 #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func)			\
174 	static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func)		\
175 	static BATADV_ATTR_SIF_SHOW_BOOL(_name)				\
176 	static BATADV_ATTR(_name, _mode, batadv_show_##_name,		\
177 			   batadv_store_##_name)
178 
179 #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)	\
180 ssize_t batadv_store_##_name(struct kobject *kobj,			\
181 			     struct attribute *attr, char *buff,	\
182 			     size_t count)				\
183 {									\
184 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
185 	struct batadv_priv *bat_priv = netdev_priv(net_dev);		\
186 									\
187 	return __batadv_store_uint_attr(buff, count, _min, _max,	\
188 					_post_func, attr,		\
189 					&bat_priv->_var, net_dev,	\
190 					NULL);	\
191 }
192 
193 #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var)				\
194 ssize_t batadv_show_##_name(struct kobject *kobj,			\
195 			    struct attribute *attr, char *buff)		\
196 {									\
197 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);	\
198 									\
199 	return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var));	\
200 }									\
201 
202 /* Use this, if you are going to set [name] in the soft-interface
203  * (bat_priv) to an unsigned integer value
204  */
205 #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
206 	static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
207 	static BATADV_ATTR_SIF_SHOW_UINT(_name, _var)			\
208 	static BATADV_ATTR(_name, _mode, batadv_show_##_name,		\
209 			   batadv_store_##_name)
210 
211 #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)			\
212 ssize_t batadv_store_vlan_##_name(struct kobject *kobj,			\
213 				  struct attribute *attr, char *buff,	\
214 				  size_t count)				\
215 {									\
216 	struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
217 	struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv,	\
218 							      kobj);	\
219 	size_t res = __batadv_store_bool_attr(buff, count, _post_func,	\
220 					      attr, &vlan->_name,	\
221 					      bat_priv->soft_iface);	\
222 									\
223 	batadv_softif_vlan_put(vlan);					\
224 	return res;							\
225 }
226 
227 #define BATADV_ATTR_VLAN_SHOW_BOOL(_name)				\
228 ssize_t batadv_show_vlan_##_name(struct kobject *kobj,			\
229 				 struct attribute *attr, char *buff)	\
230 {									\
231 	struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
232 	struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv,	\
233 							      kobj);	\
234 	size_t res = sprintf(buff, "%s\n",				\
235 			     atomic_read(&vlan->_name) == 0 ?		\
236 			     "disabled" : "enabled");			\
237 									\
238 	batadv_softif_vlan_put(vlan);					\
239 	return res;							\
240 }
241 
242 /* Use this, if you are going to turn a [name] in the vlan struct on or off */
243 #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func)			\
244 	static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func)		\
245 	static BATADV_ATTR_VLAN_SHOW_BOOL(_name)			\
246 	static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name,	\
247 				batadv_store_vlan_##_name)
248 
249 #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func)	\
250 ssize_t batadv_store_##_name(struct kobject *kobj,			\
251 			     struct attribute *attr, char *buff,	\
252 			     size_t count)				\
253 {									\
254 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
255 	struct batadv_hard_iface *hard_iface;				\
256 	ssize_t length;							\
257 									\
258 	hard_iface = batadv_hardif_get_by_netdev(net_dev);		\
259 	if (!hard_iface)						\
260 		return 0;						\
261 									\
262 	length = __batadv_store_uint_attr(buff, count, _min, _max,	\
263 					  _post_func, attr,		\
264 					  &hard_iface->_var,		\
265 					  hard_iface->soft_iface,	\
266 					  net_dev);			\
267 									\
268 	batadv_hardif_put(hard_iface);				\
269 	return length;							\
270 }
271 
272 #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var)				\
273 ssize_t batadv_show_##_name(struct kobject *kobj,			\
274 			    struct attribute *attr, char *buff)		\
275 {									\
276 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);	\
277 	struct batadv_hard_iface *hard_iface;				\
278 	ssize_t length;							\
279 									\
280 	hard_iface = batadv_hardif_get_by_netdev(net_dev);		\
281 	if (!hard_iface)						\
282 		return 0;						\
283 									\
284 	length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var));	\
285 									\
286 	batadv_hardif_put(hard_iface);				\
287 	return length;							\
288 }
289 
290 /* Use this, if you are going to set [name] in hard_iface to an
291  * unsigned integer value
292  */
293 #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
294 	static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min,		\
295 					  _max, _post_func)		\
296 	static BATADV_ATTR_HIF_SHOW_UINT(_name, _var)			\
297 	static BATADV_ATTR(_name, _mode, batadv_show_##_name,		\
298 			   batadv_store_##_name)
299 
batadv_store_bool_attr(char * buff,size_t count,struct net_device * net_dev,const char * attr_name,atomic_t * attr,bool * changed)300 static int batadv_store_bool_attr(char *buff, size_t count,
301 				  struct net_device *net_dev,
302 				  const char *attr_name, atomic_t *attr,
303 				  bool *changed)
304 {
305 	int enabled = -1;
306 
307 	*changed = false;
308 
309 	if (buff[count - 1] == '\n')
310 		buff[count - 1] = '\0';
311 
312 	if ((strncmp(buff, "1", 2) == 0) ||
313 	    (strncmp(buff, "enable", 7) == 0) ||
314 	    (strncmp(buff, "enabled", 8) == 0))
315 		enabled = 1;
316 
317 	if ((strncmp(buff, "0", 2) == 0) ||
318 	    (strncmp(buff, "disable", 8) == 0) ||
319 	    (strncmp(buff, "disabled", 9) == 0))
320 		enabled = 0;
321 
322 	if (enabled < 0) {
323 		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
324 			    attr_name, buff);
325 		return -EINVAL;
326 	}
327 
328 	if (atomic_read(attr) == enabled)
329 		return count;
330 
331 	batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
332 		    atomic_read(attr) == 1 ? "enabled" : "disabled",
333 		    enabled == 1 ? "enabled" : "disabled");
334 
335 	*changed = true;
336 
337 	atomic_set(attr, (unsigned int)enabled);
338 	return count;
339 }
340 
341 static inline ssize_t
__batadv_store_bool_attr(char * buff,size_t count,void (* post_func)(struct net_device *),struct attribute * attr,atomic_t * attr_store,struct net_device * net_dev)342 __batadv_store_bool_attr(char *buff, size_t count,
343 			 void (*post_func)(struct net_device *),
344 			 struct attribute *attr,
345 			 atomic_t *attr_store, struct net_device *net_dev)
346 {
347 	bool changed;
348 	int ret;
349 
350 	ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
351 				     attr_store, &changed);
352 	if (post_func && changed)
353 		post_func(net_dev);
354 
355 	return ret;
356 }
357 
batadv_store_uint_attr(const char * buff,size_t count,struct net_device * net_dev,struct net_device * slave_dev,const char * attr_name,unsigned int min,unsigned int max,atomic_t * attr)358 static int batadv_store_uint_attr(const char *buff, size_t count,
359 				  struct net_device *net_dev,
360 				  struct net_device *slave_dev,
361 				  const char *attr_name,
362 				  unsigned int min, unsigned int max,
363 				  atomic_t *attr)
364 {
365 	char ifname[IFNAMSIZ + 3] = "";
366 	unsigned long uint_val;
367 	int ret;
368 
369 	ret = kstrtoul(buff, 10, &uint_val);
370 	if (ret) {
371 		batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
372 			    attr_name, buff);
373 		return -EINVAL;
374 	}
375 
376 	if (uint_val < min) {
377 		batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
378 			    attr_name, uint_val, min);
379 		return -EINVAL;
380 	}
381 
382 	if (uint_val > max) {
383 		batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
384 			    attr_name, uint_val, max);
385 		return -EINVAL;
386 	}
387 
388 	if (atomic_read(attr) == uint_val)
389 		return count;
390 
391 	if (slave_dev)
392 		snprintf(ifname, sizeof(ifname), "%s: ", slave_dev->name);
393 
394 	batadv_info(net_dev, "%s: %sChanging from: %i to: %lu\n",
395 		    attr_name, ifname, atomic_read(attr), uint_val);
396 
397 	atomic_set(attr, uint_val);
398 	return count;
399 }
400 
__batadv_store_uint_attr(const char * buff,size_t count,int min,int max,void (* post_func)(struct net_device *),const struct attribute * attr,atomic_t * attr_store,struct net_device * net_dev,struct net_device * slave_dev)401 static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
402 					int min, int max,
403 					void (*post_func)(struct net_device *),
404 					const struct attribute *attr,
405 					atomic_t *attr_store,
406 					struct net_device *net_dev,
407 					struct net_device *slave_dev)
408 {
409 	int ret;
410 
411 	ret = batadv_store_uint_attr(buff, count, net_dev, slave_dev,
412 				     attr->name, min, max, attr_store);
413 	if (post_func && ret)
414 		post_func(net_dev);
415 
416 	return ret;
417 }
418 
batadv_show_bat_algo(struct kobject * kobj,struct attribute * attr,char * buff)419 static ssize_t batadv_show_bat_algo(struct kobject *kobj,
420 				    struct attribute *attr, char *buff)
421 {
422 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
423 
424 	return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
425 }
426 
batadv_post_gw_reselect(struct net_device * net_dev)427 static void batadv_post_gw_reselect(struct net_device *net_dev)
428 {
429 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
430 
431 	batadv_gw_reselect(bat_priv);
432 }
433 
batadv_show_gw_mode(struct kobject * kobj,struct attribute * attr,char * buff)434 static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
435 				   char *buff)
436 {
437 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
438 	int bytes_written;
439 
440 	/* GW mode is not available if the routing algorithm in use does not
441 	 * implement the GW API
442 	 */
443 	if (!bat_priv->algo_ops->gw.get_best_gw_node ||
444 	    !bat_priv->algo_ops->gw.is_eligible)
445 		return -ENOENT;
446 
447 	switch (atomic_read(&bat_priv->gw.mode)) {
448 	case BATADV_GW_MODE_CLIENT:
449 		bytes_written = sprintf(buff, "%s\n",
450 					BATADV_GW_MODE_CLIENT_NAME);
451 		break;
452 	case BATADV_GW_MODE_SERVER:
453 		bytes_written = sprintf(buff, "%s\n",
454 					BATADV_GW_MODE_SERVER_NAME);
455 		break;
456 	default:
457 		bytes_written = sprintf(buff, "%s\n",
458 					BATADV_GW_MODE_OFF_NAME);
459 		break;
460 	}
461 
462 	return bytes_written;
463 }
464 
batadv_store_gw_mode(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)465 static ssize_t batadv_store_gw_mode(struct kobject *kobj,
466 				    struct attribute *attr, char *buff,
467 				    size_t count)
468 {
469 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
470 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
471 	char *curr_gw_mode_str;
472 	int gw_mode_tmp = -1;
473 
474 	/* toggling GW mode is allowed only if the routing algorithm in use
475 	 * provides the GW API
476 	 */
477 	if (!bat_priv->algo_ops->gw.get_best_gw_node ||
478 	    !bat_priv->algo_ops->gw.is_eligible)
479 		return -EINVAL;
480 
481 	if (buff[count - 1] == '\n')
482 		buff[count - 1] = '\0';
483 
484 	if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
485 		    strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
486 		gw_mode_tmp = BATADV_GW_MODE_OFF;
487 
488 	if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
489 		    strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
490 		gw_mode_tmp = BATADV_GW_MODE_CLIENT;
491 
492 	if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
493 		    strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
494 		gw_mode_tmp = BATADV_GW_MODE_SERVER;
495 
496 	if (gw_mode_tmp < 0) {
497 		batadv_info(net_dev,
498 			    "Invalid parameter for 'gw mode' setting received: %s\n",
499 			    buff);
500 		return -EINVAL;
501 	}
502 
503 	if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
504 		return count;
505 
506 	switch (atomic_read(&bat_priv->gw.mode)) {
507 	case BATADV_GW_MODE_CLIENT:
508 		curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
509 		break;
510 	case BATADV_GW_MODE_SERVER:
511 		curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
512 		break;
513 	default:
514 		curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
515 		break;
516 	}
517 
518 	batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
519 		    curr_gw_mode_str, buff);
520 
521 	/* Invoking batadv_gw_reselect() is not enough to really de-select the
522 	 * current GW. It will only instruct the gateway client code to perform
523 	 * a re-election the next time that this is needed.
524 	 *
525 	 * When gw client mode is being switched off the current GW must be
526 	 * de-selected explicitly otherwise no GW_ADD uevent is thrown on
527 	 * client mode re-activation. This is operation is performed in
528 	 * batadv_gw_check_client_stop().
529 	 */
530 	batadv_gw_reselect(bat_priv);
531 	/* always call batadv_gw_check_client_stop() before changing the gateway
532 	 * state
533 	 */
534 	batadv_gw_check_client_stop(bat_priv);
535 	atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
536 	batadv_gw_tvlv_container_update(bat_priv);
537 	return count;
538 }
539 
batadv_show_gw_sel_class(struct kobject * kobj,struct attribute * attr,char * buff)540 static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
541 					struct attribute *attr, char *buff)
542 {
543 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
544 
545 	/* GW selection class is not available if the routing algorithm in use
546 	 * does not implement the GW API
547 	 */
548 	if (!bat_priv->algo_ops->gw.get_best_gw_node ||
549 	    !bat_priv->algo_ops->gw.is_eligible)
550 		return -ENOENT;
551 
552 	if (bat_priv->algo_ops->gw.show_sel_class)
553 		return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
554 
555 	return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
556 }
557 
batadv_store_gw_sel_class(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)558 static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
559 					 struct attribute *attr, char *buff,
560 					 size_t count)
561 {
562 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
563 
564 	/* setting the GW selection class is allowed only if the routing
565 	 * algorithm in use implements the GW API
566 	 */
567 	if (!bat_priv->algo_ops->gw.get_best_gw_node ||
568 	    !bat_priv->algo_ops->gw.is_eligible)
569 		return -EINVAL;
570 
571 	if (buff[count - 1] == '\n')
572 		buff[count - 1] = '\0';
573 
574 	if (bat_priv->algo_ops->gw.store_sel_class)
575 		return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
576 							      count);
577 
578 	return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
579 					batadv_post_gw_reselect, attr,
580 					&bat_priv->gw.sel_class,
581 					bat_priv->soft_iface, NULL);
582 }
583 
batadv_show_gw_bwidth(struct kobject * kobj,struct attribute * attr,char * buff)584 static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
585 				     struct attribute *attr, char *buff)
586 {
587 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
588 	u32 down, up;
589 
590 	down = atomic_read(&bat_priv->gw.bandwidth_down);
591 	up = atomic_read(&bat_priv->gw.bandwidth_up);
592 
593 	return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
594 		       down % 10, up / 10, up % 10);
595 }
596 
batadv_store_gw_bwidth(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)597 static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
598 				      struct attribute *attr, char *buff,
599 				      size_t count)
600 {
601 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
602 
603 	if (buff[count - 1] == '\n')
604 		buff[count - 1] = '\0';
605 
606 	return batadv_gw_bandwidth_set(net_dev, buff, count);
607 }
608 
609 /**
610  * batadv_show_isolation_mark - print the current isolation mark/mask
611  * @kobj: kobject representing the private mesh sysfs directory
612  * @attr: the batman-adv attribute the user is interacting with
613  * @buff: the buffer that will contain the data to send back to the user
614  *
615  * Return: the number of bytes written into 'buff' on success or a negative
616  * error code in case of failure
617  */
batadv_show_isolation_mark(struct kobject * kobj,struct attribute * attr,char * buff)618 static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
619 					  struct attribute *attr, char *buff)
620 {
621 	struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
622 
623 	return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
624 		       bat_priv->isolation_mark_mask);
625 }
626 
627 /**
628  * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
629  *  by the user
630  * @kobj: kobject representing the private mesh sysfs directory
631  * @attr: the batman-adv attribute the user is interacting with
632  * @buff: the buffer containing the user data
633  * @count: number of bytes in the buffer
634  *
635  * Return: 'count' on success or a negative error code in case of failure
636  */
batadv_store_isolation_mark(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)637 static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
638 					   struct attribute *attr, char *buff,
639 					   size_t count)
640 {
641 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
642 	struct batadv_priv *bat_priv = netdev_priv(net_dev);
643 	u32 mark, mask;
644 	char *mask_ptr;
645 
646 	/* parse the mask if it has been specified, otherwise assume the mask is
647 	 * the biggest possible
648 	 */
649 	mask = 0xFFFFFFFF;
650 	mask_ptr = strchr(buff, '/');
651 	if (mask_ptr) {
652 		*mask_ptr = '\0';
653 		mask_ptr++;
654 
655 		/* the mask must be entered in hex base as it is going to be a
656 		 * bitmask and not a prefix length
657 		 */
658 		if (kstrtou32(mask_ptr, 16, &mask) < 0)
659 			return -EINVAL;
660 	}
661 
662 	/* the mark can be entered in any base */
663 	if (kstrtou32(buff, 0, &mark) < 0)
664 		return -EINVAL;
665 
666 	bat_priv->isolation_mark_mask = mask;
667 	/* erase bits not covered by the mask */
668 	bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
669 
670 	batadv_info(net_dev,
671 		    "New skb mark for extended isolation: %#.8x/%#.8x\n",
672 		    bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
673 
674 	return count;
675 }
676 
677 BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
678 BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
679 #ifdef CONFIG_BATMAN_ADV_BLA
680 BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
681 #endif
682 #ifdef CONFIG_BATMAN_ADV_DAT
683 BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
684 #endif
685 BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
686 static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
687 static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
688 BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
689 		     INT_MAX, NULL);
690 BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
691 		     NULL);
692 static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
693 		   batadv_store_gw_sel_class);
694 static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
695 		   batadv_store_gw_bwidth);
696 #ifdef CONFIG_BATMAN_ADV_MCAST
697 BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
698 #endif
699 #ifdef CONFIG_BATMAN_ADV_DEBUG
700 BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
701 #endif
702 #ifdef CONFIG_BATMAN_ADV_NC
703 BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
704 #endif
705 static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
706 		   batadv_store_isolation_mark);
707 
708 static struct batadv_attribute *batadv_mesh_attrs[] = {
709 	&batadv_attr_aggregated_ogms,
710 	&batadv_attr_bonding,
711 #ifdef CONFIG_BATMAN_ADV_BLA
712 	&batadv_attr_bridge_loop_avoidance,
713 #endif
714 #ifdef CONFIG_BATMAN_ADV_DAT
715 	&batadv_attr_distributed_arp_table,
716 #endif
717 #ifdef CONFIG_BATMAN_ADV_MCAST
718 	&batadv_attr_multicast_mode,
719 #endif
720 	&batadv_attr_fragmentation,
721 	&batadv_attr_routing_algo,
722 	&batadv_attr_gw_mode,
723 	&batadv_attr_orig_interval,
724 	&batadv_attr_hop_penalty,
725 	&batadv_attr_gw_sel_class,
726 	&batadv_attr_gw_bandwidth,
727 #ifdef CONFIG_BATMAN_ADV_DEBUG
728 	&batadv_attr_log_level,
729 #endif
730 #ifdef CONFIG_BATMAN_ADV_NC
731 	&batadv_attr_network_coding,
732 #endif
733 	&batadv_attr_isolation_mark,
734 	NULL,
735 };
736 
737 BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
738 
739 /* array of vlan specific sysfs attributes */
740 static struct batadv_attribute *batadv_vlan_attrs[] = {
741 	&batadv_attr_vlan_ap_isolation,
742 	NULL,
743 };
744 
batadv_sysfs_add_meshif(struct net_device * dev)745 int batadv_sysfs_add_meshif(struct net_device *dev)
746 {
747 	struct kobject *batif_kobject = &dev->dev.kobj;
748 	struct batadv_priv *bat_priv = netdev_priv(dev);
749 	struct batadv_attribute **bat_attr;
750 	int err;
751 
752 	bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
753 						    batif_kobject);
754 	if (!bat_priv->mesh_obj) {
755 		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
756 			   BATADV_SYSFS_IF_MESH_SUBDIR);
757 		goto out;
758 	}
759 
760 	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
761 		err = sysfs_create_file(bat_priv->mesh_obj,
762 					&((*bat_attr)->attr));
763 		if (err) {
764 			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
765 				   dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
766 				   ((*bat_attr)->attr).name);
767 			goto rem_attr;
768 		}
769 	}
770 
771 	return 0;
772 
773 rem_attr:
774 	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
775 		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
776 
777 	kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
778 	kobject_del(bat_priv->mesh_obj);
779 	kobject_put(bat_priv->mesh_obj);
780 	bat_priv->mesh_obj = NULL;
781 out:
782 	return -ENOMEM;
783 }
784 
batadv_sysfs_del_meshif(struct net_device * dev)785 void batadv_sysfs_del_meshif(struct net_device *dev)
786 {
787 	struct batadv_priv *bat_priv = netdev_priv(dev);
788 	struct batadv_attribute **bat_attr;
789 
790 	for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
791 		sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
792 
793 	kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
794 	kobject_del(bat_priv->mesh_obj);
795 	kobject_put(bat_priv->mesh_obj);
796 	bat_priv->mesh_obj = NULL;
797 }
798 
799 /**
800  * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
801  * @dev: netdev of the mesh interface
802  * @vlan: private data of the newly added VLAN interface
803  *
804  * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
805  */
batadv_sysfs_add_vlan(struct net_device * dev,struct batadv_softif_vlan * vlan)806 int batadv_sysfs_add_vlan(struct net_device *dev,
807 			  struct batadv_softif_vlan *vlan)
808 {
809 	char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
810 	struct batadv_priv *bat_priv = netdev_priv(dev);
811 	struct batadv_attribute **bat_attr;
812 	int err;
813 
814 	if (vlan->vid & BATADV_VLAN_HAS_TAG) {
815 		sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
816 			vlan->vid & VLAN_VID_MASK);
817 
818 		vlan->kobj = kobject_create_and_add(vlan_subdir,
819 						    bat_priv->mesh_obj);
820 		if (!vlan->kobj) {
821 			batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
822 				   dev->name, vlan_subdir);
823 			goto out;
824 		}
825 	} else {
826 		/* the untagged LAN uses the root folder to store its "VLAN
827 		 * specific attributes"
828 		 */
829 		vlan->kobj = bat_priv->mesh_obj;
830 		kobject_get(bat_priv->mesh_obj);
831 	}
832 
833 	for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
834 		err = sysfs_create_file(vlan->kobj,
835 					&((*bat_attr)->attr));
836 		if (err) {
837 			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
838 				   dev->name, vlan_subdir,
839 				   ((*bat_attr)->attr).name);
840 			goto rem_attr;
841 		}
842 	}
843 
844 	return 0;
845 
846 rem_attr:
847 	for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
848 		sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
849 
850 	if (vlan->kobj != bat_priv->mesh_obj) {
851 		kobject_uevent(vlan->kobj, KOBJ_REMOVE);
852 		kobject_del(vlan->kobj);
853 	}
854 	kobject_put(vlan->kobj);
855 	vlan->kobj = NULL;
856 out:
857 	return -ENOMEM;
858 }
859 
860 /**
861  * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
862  * @bat_priv: the bat priv with all the soft interface information
863  * @vlan: the private data of the VLAN to destroy
864  */
batadv_sysfs_del_vlan(struct batadv_priv * bat_priv,struct batadv_softif_vlan * vlan)865 void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
866 			   struct batadv_softif_vlan *vlan)
867 {
868 	struct batadv_attribute **bat_attr;
869 
870 	for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
871 		sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
872 
873 	if (vlan->kobj != bat_priv->mesh_obj) {
874 		kobject_uevent(vlan->kobj, KOBJ_REMOVE);
875 		kobject_del(vlan->kobj);
876 	}
877 	kobject_put(vlan->kobj);
878 	vlan->kobj = NULL;
879 }
880 
batadv_show_mesh_iface(struct kobject * kobj,struct attribute * attr,char * buff)881 static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
882 				      struct attribute *attr, char *buff)
883 {
884 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
885 	struct batadv_hard_iface *hard_iface;
886 	ssize_t length;
887 	const char *ifname;
888 
889 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
890 	if (!hard_iface)
891 		return 0;
892 
893 	if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
894 		ifname =  "none";
895 	else
896 		ifname = hard_iface->soft_iface->name;
897 
898 	length = sprintf(buff, "%s\n", ifname);
899 
900 	batadv_hardif_put(hard_iface);
901 
902 	return length;
903 }
904 
905 /**
906  * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
907  * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
908  * @ifname: name of soft-interface to modify
909  *
910  * Changes the parts of the hard+soft interface which can not be modified under
911  * sysfs lock (to prevent deadlock situations).
912  *
913  * Return: 0 on success, 0 < on failure
914  */
batadv_store_mesh_iface_finish(struct net_device * net_dev,char ifname[IFNAMSIZ])915 static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
916 					  char ifname[IFNAMSIZ])
917 {
918 	struct net *net = dev_net(net_dev);
919 	struct batadv_hard_iface *hard_iface;
920 	int status_tmp;
921 	int ret = 0;
922 
923 	ASSERT_RTNL();
924 
925 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
926 	if (!hard_iface)
927 		return 0;
928 
929 	if (strncmp(ifname, "none", 4) == 0)
930 		status_tmp = BATADV_IF_NOT_IN_USE;
931 	else
932 		status_tmp = BATADV_IF_I_WANT_YOU;
933 
934 	if (hard_iface->if_status == status_tmp)
935 		goto out;
936 
937 	if ((hard_iface->soft_iface) &&
938 	    (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0))
939 		goto out;
940 
941 	if (status_tmp == BATADV_IF_NOT_IN_USE) {
942 		batadv_hardif_disable_interface(hard_iface,
943 						BATADV_IF_CLEANUP_AUTO);
944 		goto out;
945 	}
946 
947 	/* if the interface already is in use */
948 	if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
949 		batadv_hardif_disable_interface(hard_iface,
950 						BATADV_IF_CLEANUP_AUTO);
951 
952 	ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
953 out:
954 	batadv_hardif_put(hard_iface);
955 	return ret;
956 }
957 
958 /**
959  * batadv_store_mesh_iface_work - store new hardif mesh_iface state
960  * @work: work queue item
961  *
962  * Changes the parts of the hard+soft interface which can not be modified under
963  * sysfs lock (to prevent deadlock situations).
964  */
batadv_store_mesh_iface_work(struct work_struct * work)965 static void batadv_store_mesh_iface_work(struct work_struct *work)
966 {
967 	struct batadv_store_mesh_work *store_work;
968 	int ret;
969 
970 	store_work = container_of(work, struct batadv_store_mesh_work, work);
971 
972 	rtnl_lock();
973 	ret = batadv_store_mesh_iface_finish(store_work->net_dev,
974 					     store_work->soft_iface_name);
975 	rtnl_unlock();
976 
977 	if (ret < 0)
978 		pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
979 		       store_work->soft_iface_name, store_work->net_dev->name,
980 		       ret);
981 
982 	dev_put(store_work->net_dev);
983 	kfree(store_work);
984 }
985 
batadv_store_mesh_iface(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)986 static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
987 				       struct attribute *attr, char *buff,
988 				       size_t count)
989 {
990 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
991 	struct batadv_store_mesh_work *store_work;
992 
993 	if (buff[count - 1] == '\n')
994 		buff[count - 1] = '\0';
995 
996 	if (strlen(buff) >= IFNAMSIZ) {
997 		pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
998 		       buff);
999 		return -EINVAL;
1000 	}
1001 
1002 	store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
1003 	if (!store_work)
1004 		return -ENOMEM;
1005 
1006 	dev_hold(net_dev);
1007 	INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
1008 	store_work->net_dev = net_dev;
1009 	strlcpy(store_work->soft_iface_name, buff,
1010 		sizeof(store_work->soft_iface_name));
1011 
1012 	queue_work(batadv_event_workqueue, &store_work->work);
1013 
1014 	return count;
1015 }
1016 
batadv_show_iface_status(struct kobject * kobj,struct attribute * attr,char * buff)1017 static ssize_t batadv_show_iface_status(struct kobject *kobj,
1018 					struct attribute *attr, char *buff)
1019 {
1020 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1021 	struct batadv_hard_iface *hard_iface;
1022 	ssize_t length;
1023 
1024 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
1025 	if (!hard_iface)
1026 		return 0;
1027 
1028 	switch (hard_iface->if_status) {
1029 	case BATADV_IF_TO_BE_REMOVED:
1030 		length = sprintf(buff, "disabling\n");
1031 		break;
1032 	case BATADV_IF_INACTIVE:
1033 		length = sprintf(buff, "inactive\n");
1034 		break;
1035 	case BATADV_IF_ACTIVE:
1036 		length = sprintf(buff, "active\n");
1037 		break;
1038 	case BATADV_IF_TO_BE_ACTIVATED:
1039 		length = sprintf(buff, "enabling\n");
1040 		break;
1041 	case BATADV_IF_NOT_IN_USE:
1042 	default:
1043 		length = sprintf(buff, "not in use\n");
1044 		break;
1045 	}
1046 
1047 	batadv_hardif_put(hard_iface);
1048 
1049 	return length;
1050 }
1051 
1052 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1053 
1054 /**
1055  * batadv_store_throughput_override - parse and store throughput override
1056  *  entered by the user
1057  * @kobj: kobject representing the private mesh sysfs directory
1058  * @attr: the batman-adv attribute the user is interacting with
1059  * @buff: the buffer containing the user data
1060  * @count: number of bytes in the buffer
1061  *
1062  * Return: 'count' on success or a negative error code in case of failure
1063  */
batadv_store_throughput_override(struct kobject * kobj,struct attribute * attr,char * buff,size_t count)1064 static ssize_t batadv_store_throughput_override(struct kobject *kobj,
1065 						struct attribute *attr,
1066 						char *buff, size_t count)
1067 {
1068 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1069 	struct batadv_hard_iface *hard_iface;
1070 	u32 tp_override;
1071 	u32 old_tp_override;
1072 	bool ret;
1073 
1074 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
1075 	if (!hard_iface)
1076 		return -EINVAL;
1077 
1078 	if (buff[count - 1] == '\n')
1079 		buff[count - 1] = '\0';
1080 
1081 	ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
1082 				      &tp_override);
1083 	if (!ret)
1084 		return count;
1085 
1086 	old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1087 	if (old_tp_override == tp_override)
1088 		goto out;
1089 
1090 	batadv_info(hard_iface->soft_iface,
1091 		    "%s: %s: Changing from: %u.%u MBit to: %u.%u MBit\n",
1092 		    "throughput_override", net_dev->name,
1093 		    old_tp_override / 10, old_tp_override % 10,
1094 		    tp_override / 10, tp_override % 10);
1095 
1096 	atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
1097 
1098 out:
1099 	batadv_hardif_put(hard_iface);
1100 	return count;
1101 }
1102 
batadv_show_throughput_override(struct kobject * kobj,struct attribute * attr,char * buff)1103 static ssize_t batadv_show_throughput_override(struct kobject *kobj,
1104 					       struct attribute *attr,
1105 					       char *buff)
1106 {
1107 	struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
1108 	struct batadv_hard_iface *hard_iface;
1109 	u32 tp_override;
1110 
1111 	hard_iface = batadv_hardif_get_by_netdev(net_dev);
1112 	if (!hard_iface)
1113 		return -EINVAL;
1114 
1115 	tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
1116 
1117 	return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
1118 		       tp_override % 10);
1119 }
1120 
1121 #endif
1122 
1123 static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
1124 		   batadv_store_mesh_iface);
1125 static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
1126 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1127 BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
1128 		     2 * BATADV_JITTER, INT_MAX, NULL);
1129 static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
1130 		   batadv_store_throughput_override);
1131 #endif
1132 
1133 static struct batadv_attribute *batadv_batman_attrs[] = {
1134 	&batadv_attr_mesh_iface,
1135 	&batadv_attr_iface_status,
1136 #ifdef CONFIG_BATMAN_ADV_BATMAN_V
1137 	&batadv_attr_elp_interval,
1138 	&batadv_attr_throughput_override,
1139 #endif
1140 	NULL,
1141 };
1142 
batadv_sysfs_add_hardif(struct kobject ** hardif_obj,struct net_device * dev)1143 int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
1144 {
1145 	struct kobject *hardif_kobject = &dev->dev.kobj;
1146 	struct batadv_attribute **bat_attr;
1147 	int err;
1148 
1149 	*hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
1150 					     hardif_kobject);
1151 
1152 	if (!*hardif_obj) {
1153 		batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
1154 			   BATADV_SYSFS_IF_BAT_SUBDIR);
1155 		goto out;
1156 	}
1157 
1158 	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
1159 		err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
1160 		if (err) {
1161 			batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
1162 				   dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
1163 				   ((*bat_attr)->attr).name);
1164 			goto rem_attr;
1165 		}
1166 	}
1167 
1168 	return 0;
1169 
1170 rem_attr:
1171 	for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
1172 		sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
1173 out:
1174 	return -ENOMEM;
1175 }
1176 
batadv_sysfs_del_hardif(struct kobject ** hardif_obj)1177 void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
1178 {
1179 	kobject_uevent(*hardif_obj, KOBJ_REMOVE);
1180 	kobject_del(*hardif_obj);
1181 	kobject_put(*hardif_obj);
1182 	*hardif_obj = NULL;
1183 }
1184 
batadv_throw_uevent(struct batadv_priv * bat_priv,enum batadv_uev_type type,enum batadv_uev_action action,const char * data)1185 int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
1186 			enum batadv_uev_action action, const char *data)
1187 {
1188 	int ret = -ENOMEM;
1189 	struct kobject *bat_kobj;
1190 	char *uevent_env[4] = { NULL, NULL, NULL, NULL };
1191 
1192 	bat_kobj = &bat_priv->soft_iface->dev.kobj;
1193 
1194 	uevent_env[0] = kasprintf(GFP_ATOMIC,
1195 				  "%s%s", BATADV_UEV_TYPE_VAR,
1196 				  batadv_uev_type_str[type]);
1197 	if (!uevent_env[0])
1198 		goto out;
1199 
1200 	uevent_env[1] = kasprintf(GFP_ATOMIC,
1201 				  "%s%s", BATADV_UEV_ACTION_VAR,
1202 				  batadv_uev_action_str[action]);
1203 	if (!uevent_env[1])
1204 		goto out;
1205 
1206 	/* If the event is DEL, ignore the data field */
1207 	if (action != BATADV_UEV_DEL) {
1208 		uevent_env[2] = kasprintf(GFP_ATOMIC,
1209 					  "%s%s", BATADV_UEV_DATA_VAR, data);
1210 		if (!uevent_env[2])
1211 			goto out;
1212 	}
1213 
1214 	ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
1215 out:
1216 	kfree(uevent_env[0]);
1217 	kfree(uevent_env[1]);
1218 	kfree(uevent_env[2]);
1219 
1220 	if (ret)
1221 		batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1222 			   "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
1223 			   batadv_uev_type_str[type],
1224 			   batadv_uev_action_str[action],
1225 			   (action == BATADV_UEV_DEL ? "NULL" : data), ret);
1226 	return ret;
1227 }
1228