sysfs.c 34 KB

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