bat_sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /* Copyright (C) 2010-2012 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "bat_sysfs.h"
  21. #include "translation-table.h"
  22. #include "originator.h"
  23. #include "hard-interface.h"
  24. #include "gateway_common.h"
  25. #include "gateway_client.h"
  26. #include "vis.h"
  27. static struct net_device *kobj_to_netdev(struct kobject *obj)
  28. {
  29. struct device *dev = container_of(obj->parent, struct device, kobj);
  30. return to_net_dev(dev);
  31. }
  32. static struct bat_priv *kobj_to_batpriv(struct kobject *obj)
  33. {
  34. struct net_device *net_dev = kobj_to_netdev(obj);
  35. return netdev_priv(net_dev);
  36. }
  37. #define UEV_TYPE_VAR "BATTYPE="
  38. #define UEV_ACTION_VAR "BATACTION="
  39. #define UEV_DATA_VAR "BATDATA="
  40. static char *uev_action_str[] = {
  41. "add",
  42. "del",
  43. "change"
  44. };
  45. static char *uev_type_str[] = {
  46. "gw"
  47. };
  48. /* Use this, if you have customized show and store functions */
  49. #define BAT_ATTR(_name, _mode, _show, _store) \
  50. struct bat_attribute bat_attr_##_name = { \
  51. .attr = {.name = __stringify(_name), \
  52. .mode = _mode }, \
  53. .show = _show, \
  54. .store = _store, \
  55. };
  56. #define BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  57. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  58. char *buff, size_t count) \
  59. { \
  60. struct net_device *net_dev = kobj_to_netdev(kobj); \
  61. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  62. return __store_bool_attr(buff, count, _post_func, attr, \
  63. &bat_priv->_name, net_dev); \
  64. }
  65. #define BAT_ATTR_SIF_SHOW_BOOL(_name) \
  66. ssize_t show_##_name(struct kobject *kobj, \
  67. struct attribute *attr, char *buff) \
  68. { \
  69. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  70. return sprintf(buff, "%s\n", \
  71. atomic_read(&bat_priv->_name) == 0 ? \
  72. "disabled" : "enabled"); \
  73. } \
  74. /* Use this, if you are going to turn a [name] in the soft-interface
  75. * (bat_priv) on or off
  76. */
  77. #define BAT_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  78. static BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  79. static BAT_ATTR_SIF_SHOW_BOOL(_name) \
  80. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  81. #define BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  82. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  83. char *buff, size_t count) \
  84. { \
  85. struct net_device *net_dev = kobj_to_netdev(kobj); \
  86. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  87. return __store_uint_attr(buff, count, _min, _max, _post_func, \
  88. attr, &bat_priv->_name, net_dev); \
  89. }
  90. #define BAT_ATTR_SIF_SHOW_UINT(_name) \
  91. ssize_t show_##_name(struct kobject *kobj, \
  92. struct attribute *attr, char *buff) \
  93. { \
  94. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  95. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  96. } \
  97. /* Use this, if you are going to set [name] in the soft-interface
  98. * (bat_priv) to an unsigned integer value
  99. */
  100. #define BAT_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
  101. static BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  102. static BAT_ATTR_SIF_SHOW_UINT(_name) \
  103. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  104. #define BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
  105. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  106. char *buff, size_t count) \
  107. { \
  108. struct net_device *net_dev = kobj_to_netdev(kobj); \
  109. struct hard_iface *hard_iface; \
  110. ssize_t length; \
  111. \
  112. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  113. if (!hard_iface) \
  114. return 0; \
  115. \
  116. length = __store_uint_attr(buff, count, _min, _max, _post_func, \
  117. attr, &hard_iface->_name, net_dev); \
  118. \
  119. batadv_hardif_free_ref(hard_iface); \
  120. return length; \
  121. }
  122. #define BAT_ATTR_HIF_SHOW_UINT(_name) \
  123. ssize_t show_##_name(struct kobject *kobj, \
  124. struct attribute *attr, char *buff) \
  125. { \
  126. struct net_device *net_dev = kobj_to_netdev(kobj); \
  127. struct hard_iface *hard_iface; \
  128. ssize_t length; \
  129. \
  130. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  131. if (!hard_iface) \
  132. return 0; \
  133. \
  134. length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
  135. \
  136. batadv_hardif_free_ref(hard_iface); \
  137. return length; \
  138. }
  139. /* Use this, if you are going to set [name] in hard_iface to an
  140. * unsigned integer value
  141. */
  142. #define BAT_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
  143. static BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
  144. static BAT_ATTR_HIF_SHOW_UINT(_name) \
  145. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  146. static int store_bool_attr(char *buff, size_t count,
  147. struct net_device *net_dev,
  148. const char *attr_name, atomic_t *attr)
  149. {
  150. int enabled = -1;
  151. if (buff[count - 1] == '\n')
  152. buff[count - 1] = '\0';
  153. if ((strncmp(buff, "1", 2) == 0) ||
  154. (strncmp(buff, "enable", 7) == 0) ||
  155. (strncmp(buff, "enabled", 8) == 0))
  156. enabled = 1;
  157. if ((strncmp(buff, "0", 2) == 0) ||
  158. (strncmp(buff, "disable", 8) == 0) ||
  159. (strncmp(buff, "disabled", 9) == 0))
  160. enabled = 0;
  161. if (enabled < 0) {
  162. bat_info(net_dev,
  163. "%s: Invalid parameter received: %s\n",
  164. attr_name, buff);
  165. return -EINVAL;
  166. }
  167. if (atomic_read(attr) == enabled)
  168. return count;
  169. bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  170. atomic_read(attr) == 1 ? "enabled" : "disabled",
  171. enabled == 1 ? "enabled" : "disabled");
  172. atomic_set(attr, (unsigned int)enabled);
  173. return count;
  174. }
  175. static inline ssize_t __store_bool_attr(char *buff, size_t count,
  176. void (*post_func)(struct net_device *),
  177. struct attribute *attr,
  178. atomic_t *attr_store, struct net_device *net_dev)
  179. {
  180. int ret;
  181. ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
  182. if (post_func && ret)
  183. post_func(net_dev);
  184. return ret;
  185. }
  186. static int store_uint_attr(const char *buff, size_t count,
  187. struct net_device *net_dev, const char *attr_name,
  188. unsigned int min, unsigned int max, atomic_t *attr)
  189. {
  190. unsigned long uint_val;
  191. int ret;
  192. ret = kstrtoul(buff, 10, &uint_val);
  193. if (ret) {
  194. bat_info(net_dev,
  195. "%s: Invalid parameter received: %s\n",
  196. attr_name, buff);
  197. return -EINVAL;
  198. }
  199. if (uint_val < min) {
  200. bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  201. attr_name, uint_val, min);
  202. return -EINVAL;
  203. }
  204. if (uint_val > max) {
  205. bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  206. attr_name, uint_val, max);
  207. return -EINVAL;
  208. }
  209. if (atomic_read(attr) == uint_val)
  210. return count;
  211. bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
  212. attr_name, atomic_read(attr), uint_val);
  213. atomic_set(attr, uint_val);
  214. return count;
  215. }
  216. static inline ssize_t __store_uint_attr(const char *buff, size_t count,
  217. int min, int max,
  218. void (*post_func)(struct net_device *),
  219. const struct attribute *attr,
  220. atomic_t *attr_store, struct net_device *net_dev)
  221. {
  222. int ret;
  223. ret = store_uint_attr(buff, count, net_dev, attr->name,
  224. min, max, attr_store);
  225. if (post_func && ret)
  226. post_func(net_dev);
  227. return ret;
  228. }
  229. static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
  230. char *buff)
  231. {
  232. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  233. int vis_mode = atomic_read(&bat_priv->vis_mode);
  234. return sprintf(buff, "%s\n",
  235. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  236. "client" : "server");
  237. }
  238. static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
  239. char *buff, size_t count)
  240. {
  241. struct net_device *net_dev = kobj_to_netdev(kobj);
  242. struct bat_priv *bat_priv = netdev_priv(net_dev);
  243. unsigned long val;
  244. int ret, vis_mode_tmp = -1;
  245. ret = kstrtoul(buff, 10, &val);
  246. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  247. (strncmp(buff, "client", 6) == 0) ||
  248. (strncmp(buff, "off", 3) == 0))
  249. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  250. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  251. (strncmp(buff, "server", 6) == 0))
  252. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  253. if (vis_mode_tmp < 0) {
  254. if (buff[count - 1] == '\n')
  255. buff[count - 1] = '\0';
  256. bat_info(net_dev,
  257. "Invalid parameter for 'vis mode' setting received: %s\n",
  258. buff);
  259. return -EINVAL;
  260. }
  261. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  262. return count;
  263. bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
  264. atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
  265. "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
  266. "client" : "server");
  267. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  268. return count;
  269. }
  270. static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
  271. char *buff)
  272. {
  273. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  274. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  275. }
  276. static void post_gw_deselect(struct net_device *net_dev)
  277. {
  278. struct bat_priv *bat_priv = netdev_priv(net_dev);
  279. batadv_gw_deselect(bat_priv);
  280. }
  281. static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
  282. char *buff)
  283. {
  284. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  285. int bytes_written;
  286. switch (atomic_read(&bat_priv->gw_mode)) {
  287. case GW_MODE_CLIENT:
  288. bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
  289. break;
  290. case GW_MODE_SERVER:
  291. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  292. break;
  293. default:
  294. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  295. break;
  296. }
  297. return bytes_written;
  298. }
  299. static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
  300. char *buff, size_t count)
  301. {
  302. struct net_device *net_dev = kobj_to_netdev(kobj);
  303. struct bat_priv *bat_priv = netdev_priv(net_dev);
  304. char *curr_gw_mode_str;
  305. int gw_mode_tmp = -1;
  306. if (buff[count - 1] == '\n')
  307. buff[count - 1] = '\0';
  308. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  309. gw_mode_tmp = GW_MODE_OFF;
  310. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  311. strlen(GW_MODE_CLIENT_NAME)) == 0)
  312. gw_mode_tmp = GW_MODE_CLIENT;
  313. if (strncmp(buff, GW_MODE_SERVER_NAME,
  314. strlen(GW_MODE_SERVER_NAME)) == 0)
  315. gw_mode_tmp = GW_MODE_SERVER;
  316. if (gw_mode_tmp < 0) {
  317. bat_info(net_dev,
  318. "Invalid parameter for 'gw mode' setting received: %s\n",
  319. buff);
  320. return -EINVAL;
  321. }
  322. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  323. return count;
  324. switch (atomic_read(&bat_priv->gw_mode)) {
  325. case GW_MODE_CLIENT:
  326. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  327. break;
  328. case GW_MODE_SERVER:
  329. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  330. break;
  331. default:
  332. curr_gw_mode_str = GW_MODE_OFF_NAME;
  333. break;
  334. }
  335. bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
  336. curr_gw_mode_str, buff);
  337. batadv_gw_deselect(bat_priv);
  338. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  339. return count;
  340. }
  341. static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  342. char *buff)
  343. {
  344. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  345. int down, up;
  346. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  347. batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  348. return sprintf(buff, "%i%s/%i%s\n",
  349. (down > 2048 ? down / 1024 : down),
  350. (down > 2048 ? "MBit" : "KBit"),
  351. (up > 2048 ? up / 1024 : up),
  352. (up > 2048 ? "MBit" : "KBit"));
  353. }
  354. static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  355. char *buff, size_t count)
  356. {
  357. struct net_device *net_dev = kobj_to_netdev(kobj);
  358. if (buff[count - 1] == '\n')
  359. buff[count - 1] = '\0';
  360. return batadv_gw_bandwidth_set(net_dev, buff, count);
  361. }
  362. BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  363. BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  364. #ifdef CONFIG_BATMAN_ADV_BLA
  365. BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  366. #endif
  367. BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  368. BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  369. static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
  370. static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
  371. static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
  372. BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
  373. BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  374. BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  375. post_gw_deselect);
  376. static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
  377. store_gw_bwidth);
  378. #ifdef CONFIG_BATMAN_ADV_DEBUG
  379. BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
  380. #endif
  381. static struct bat_attribute *mesh_attrs[] = {
  382. &bat_attr_aggregated_ogms,
  383. &bat_attr_bonding,
  384. #ifdef CONFIG_BATMAN_ADV_BLA
  385. &bat_attr_bridge_loop_avoidance,
  386. #endif
  387. &bat_attr_fragmentation,
  388. &bat_attr_ap_isolation,
  389. &bat_attr_vis_mode,
  390. &bat_attr_routing_algo,
  391. &bat_attr_gw_mode,
  392. &bat_attr_orig_interval,
  393. &bat_attr_hop_penalty,
  394. &bat_attr_gw_sel_class,
  395. &bat_attr_gw_bandwidth,
  396. #ifdef CONFIG_BATMAN_ADV_DEBUG
  397. &bat_attr_log_level,
  398. #endif
  399. NULL,
  400. };
  401. int batadv_sysfs_add_meshif(struct net_device *dev)
  402. {
  403. struct kobject *batif_kobject = &dev->dev.kobj;
  404. struct bat_priv *bat_priv = netdev_priv(dev);
  405. struct bat_attribute **bat_attr;
  406. int err;
  407. bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
  408. batif_kobject);
  409. if (!bat_priv->mesh_obj) {
  410. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  411. SYSFS_IF_MESH_SUBDIR);
  412. goto out;
  413. }
  414. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
  415. err = sysfs_create_file(bat_priv->mesh_obj,
  416. &((*bat_attr)->attr));
  417. if (err) {
  418. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  419. dev->name, SYSFS_IF_MESH_SUBDIR,
  420. ((*bat_attr)->attr).name);
  421. goto rem_attr;
  422. }
  423. }
  424. return 0;
  425. rem_attr:
  426. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  427. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  428. kobject_put(bat_priv->mesh_obj);
  429. bat_priv->mesh_obj = NULL;
  430. out:
  431. return -ENOMEM;
  432. }
  433. void batadv_sysfs_del_meshif(struct net_device *dev)
  434. {
  435. struct bat_priv *bat_priv = netdev_priv(dev);
  436. struct bat_attribute **bat_attr;
  437. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  438. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  439. kobject_put(bat_priv->mesh_obj);
  440. bat_priv->mesh_obj = NULL;
  441. }
  442. static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
  443. char *buff)
  444. {
  445. struct net_device *net_dev = kobj_to_netdev(kobj);
  446. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  447. ssize_t length;
  448. if (!hard_iface)
  449. return 0;
  450. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  451. "none" : hard_iface->soft_iface->name);
  452. batadv_hardif_free_ref(hard_iface);
  453. return length;
  454. }
  455. static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
  456. char *buff, size_t count)
  457. {
  458. struct net_device *net_dev = kobj_to_netdev(kobj);
  459. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  460. int status_tmp = -1;
  461. int ret = count;
  462. if (!hard_iface)
  463. return count;
  464. if (buff[count - 1] == '\n')
  465. buff[count - 1] = '\0';
  466. if (strlen(buff) >= IFNAMSIZ) {
  467. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  468. buff);
  469. batadv_hardif_free_ref(hard_iface);
  470. return -EINVAL;
  471. }
  472. if (strncmp(buff, "none", 4) == 0)
  473. status_tmp = IF_NOT_IN_USE;
  474. else
  475. status_tmp = IF_I_WANT_YOU;
  476. if (hard_iface->if_status == status_tmp)
  477. goto out;
  478. if ((hard_iface->soft_iface) &&
  479. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  480. goto out;
  481. if (!rtnl_trylock()) {
  482. ret = -ERESTARTSYS;
  483. goto out;
  484. }
  485. if (status_tmp == IF_NOT_IN_USE) {
  486. batadv_hardif_disable_interface(hard_iface);
  487. goto unlock;
  488. }
  489. /* if the interface already is in use */
  490. if (hard_iface->if_status != IF_NOT_IN_USE)
  491. batadv_hardif_disable_interface(hard_iface);
  492. ret = batadv_hardif_enable_interface(hard_iface, buff);
  493. unlock:
  494. rtnl_unlock();
  495. out:
  496. batadv_hardif_free_ref(hard_iface);
  497. return ret;
  498. }
  499. static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
  500. char *buff)
  501. {
  502. struct net_device *net_dev = kobj_to_netdev(kobj);
  503. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  504. ssize_t length;
  505. if (!hard_iface)
  506. return 0;
  507. switch (hard_iface->if_status) {
  508. case IF_TO_BE_REMOVED:
  509. length = sprintf(buff, "disabling\n");
  510. break;
  511. case IF_INACTIVE:
  512. length = sprintf(buff, "inactive\n");
  513. break;
  514. case IF_ACTIVE:
  515. length = sprintf(buff, "active\n");
  516. break;
  517. case IF_TO_BE_ACTIVATED:
  518. length = sprintf(buff, "enabling\n");
  519. break;
  520. case IF_NOT_IN_USE:
  521. default:
  522. length = sprintf(buff, "not in use\n");
  523. break;
  524. }
  525. batadv_hardif_free_ref(hard_iface);
  526. return length;
  527. }
  528. static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
  529. show_mesh_iface, store_mesh_iface);
  530. static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
  531. static struct bat_attribute *batman_attrs[] = {
  532. &bat_attr_mesh_iface,
  533. &bat_attr_iface_status,
  534. NULL,
  535. };
  536. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  537. {
  538. struct kobject *hardif_kobject = &dev->dev.kobj;
  539. struct bat_attribute **bat_attr;
  540. int err;
  541. *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
  542. hardif_kobject);
  543. if (!*hardif_obj) {
  544. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  545. SYSFS_IF_BAT_SUBDIR);
  546. goto out;
  547. }
  548. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
  549. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  550. if (err) {
  551. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  552. dev->name, SYSFS_IF_BAT_SUBDIR,
  553. ((*bat_attr)->attr).name);
  554. goto rem_attr;
  555. }
  556. }
  557. return 0;
  558. rem_attr:
  559. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
  560. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  561. out:
  562. return -ENOMEM;
  563. }
  564. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  565. {
  566. kobject_put(*hardif_obj);
  567. *hardif_obj = NULL;
  568. }
  569. int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  570. enum uev_action action, const char *data)
  571. {
  572. int ret = -ENOMEM;
  573. struct hard_iface *primary_if = NULL;
  574. struct kobject *bat_kobj;
  575. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  576. primary_if = batadv_primary_if_get_selected(bat_priv);
  577. if (!primary_if)
  578. goto out;
  579. bat_kobj = &primary_if->soft_iface->dev.kobj;
  580. uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
  581. strlen(uev_type_str[type]) + 1,
  582. GFP_ATOMIC);
  583. if (!uevent_env[0])
  584. goto out;
  585. sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
  586. uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
  587. strlen(uev_action_str[action]) + 1,
  588. GFP_ATOMIC);
  589. if (!uevent_env[1])
  590. goto out;
  591. sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
  592. /* If the event is DEL, ignore the data field */
  593. if (action != UEV_DEL) {
  594. uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
  595. strlen(data) + 1, GFP_ATOMIC);
  596. if (!uevent_env[2])
  597. goto out;
  598. sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
  599. }
  600. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  601. out:
  602. kfree(uevent_env[0]);
  603. kfree(uevent_env[1]);
  604. kfree(uevent_env[2]);
  605. if (primary_if)
  606. batadv_hardif_free_ref(primary_if);
  607. if (ret)
  608. bat_dbg(DBG_BATMAN, bat_priv,
  609. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  610. uev_type_str[type], uev_action_str[action],
  611. (action == UEV_DEL ? "NULL" : data), ret);
  612. return ret;
  613. }