bat_sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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 *batadv_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 *batadv_kobj_to_batpriv(struct kobject *obj)
  33. {
  34. struct net_device *net_dev = batadv_kobj_to_netdev(obj);
  35. return netdev_priv(net_dev);
  36. }
  37. #define BATADV_UEV_TYPE_VAR "BATTYPE="
  38. #define BATADV_UEV_ACTION_VAR "BATACTION="
  39. #define BATADV_UEV_DATA_VAR "BATDATA="
  40. static char *batadv_uev_action_str[] = {
  41. "add",
  42. "del",
  43. "change"
  44. };
  45. static char *batadv_uev_type_str[] = {
  46. "gw"
  47. };
  48. /* Use this, if you have customized show and store functions */
  49. #define BATADV_ATTR(_name, _mode, _show, _store) \
  50. struct bat_attribute batadv_attr_##_name = { \
  51. .attr = {.name = __stringify(_name), \
  52. .mode = _mode }, \
  53. .show = _show, \
  54. .store = _store, \
  55. };
  56. #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  57. ssize_t batadv_store_##_name(struct kobject *kobj, \
  58. struct attribute *attr, char *buff, \
  59. size_t count) \
  60. { \
  61. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  62. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  63. return __batadv_store_bool_attr(buff, count, _post_func, attr, \
  64. &bat_priv->_name, net_dev); \
  65. }
  66. #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  67. ssize_t batadv_show_##_name(struct kobject *kobj, \
  68. struct attribute *attr, char *buff) \
  69. { \
  70. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  71. return sprintf(buff, "%s\n", \
  72. atomic_read(&bat_priv->_name) == 0 ? \
  73. "disabled" : "enabled"); \
  74. } \
  75. /* Use this, if you are going to turn a [name] in the soft-interface
  76. * (bat_priv) on or off
  77. */
  78. #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  79. static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  80. static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  81. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  82. batadv_store_##_name)
  83. #define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  84. ssize_t batadv_store_##_name(struct kobject *kobj, \
  85. struct attribute *attr, char *buff, \
  86. size_t count) \
  87. { \
  88. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  89. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  90. return __batadv_store_uint_attr(buff, count, _min, _max, \
  91. _post_func, attr, \
  92. &bat_priv->_name, net_dev); \
  93. }
  94. #define BATADV_ATTR_SIF_SHOW_UINT(_name) \
  95. ssize_t batadv_show_##_name(struct kobject *kobj, \
  96. struct attribute *attr, char *buff) \
  97. { \
  98. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  99. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  100. } \
  101. /* Use this, if you are going to set [name] in the soft-interface
  102. * (bat_priv) to an unsigned integer value
  103. */
  104. #define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
  105. static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
  106. static BATADV_ATTR_SIF_SHOW_UINT(_name) \
  107. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  108. batadv_store_##_name)
  109. #define BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
  110. ssize_t batadv_store_##_name(struct kobject *kobj, \
  111. struct attribute *attr, char *buff, \
  112. size_t count) \
  113. { \
  114. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  115. struct hard_iface *hard_iface; \
  116. ssize_t length; \
  117. \
  118. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  119. if (!hard_iface) \
  120. return 0; \
  121. \
  122. length = __batadv_store_uint_attr(buff, count, _min, _max, \
  123. _post_func, attr, \
  124. &hard_iface->_name, net_dev); \
  125. \
  126. batadv_hardif_free_ref(hard_iface); \
  127. return length; \
  128. }
  129. #define BATADV_ATTR_HIF_SHOW_UINT(_name) \
  130. ssize_t batadv_show_##_name(struct kobject *kobj, \
  131. struct attribute *attr, char *buff) \
  132. { \
  133. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  134. struct hard_iface *hard_iface; \
  135. ssize_t length; \
  136. \
  137. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  138. if (!hard_iface) \
  139. return 0; \
  140. \
  141. length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
  142. \
  143. batadv_hardif_free_ref(hard_iface); \
  144. return length; \
  145. }
  146. /* Use this, if you are going to set [name] in hard_iface to an
  147. * unsigned integer value
  148. */
  149. #define BATADV_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
  150. static BATADV_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func)\
  151. static BATADV_ATTR_HIF_SHOW_UINT(_name) \
  152. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  153. batadv_store_##_name)
  154. static int batadv_store_bool_attr(char *buff, size_t count,
  155. struct net_device *net_dev,
  156. const char *attr_name, atomic_t *attr)
  157. {
  158. int enabled = -1;
  159. if (buff[count - 1] == '\n')
  160. buff[count - 1] = '\0';
  161. if ((strncmp(buff, "1", 2) == 0) ||
  162. (strncmp(buff, "enable", 7) == 0) ||
  163. (strncmp(buff, "enabled", 8) == 0))
  164. enabled = 1;
  165. if ((strncmp(buff, "0", 2) == 0) ||
  166. (strncmp(buff, "disable", 8) == 0) ||
  167. (strncmp(buff, "disabled", 9) == 0))
  168. enabled = 0;
  169. if (enabled < 0) {
  170. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  171. attr_name, buff);
  172. return -EINVAL;
  173. }
  174. if (atomic_read(attr) == enabled)
  175. return count;
  176. batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  177. atomic_read(attr) == 1 ? "enabled" : "disabled",
  178. enabled == 1 ? "enabled" : "disabled");
  179. atomic_set(attr, (unsigned int)enabled);
  180. return count;
  181. }
  182. static inline ssize_t
  183. __batadv_store_bool_attr(char *buff, size_t count,
  184. void (*post_func)(struct net_device *),
  185. struct attribute *attr,
  186. atomic_t *attr_store, struct net_device *net_dev)
  187. {
  188. int ret;
  189. ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
  190. attr_store);
  191. if (post_func && ret)
  192. post_func(net_dev);
  193. return ret;
  194. }
  195. static int batadv_store_uint_attr(const char *buff, size_t count,
  196. struct net_device *net_dev,
  197. const char *attr_name,
  198. unsigned int min, unsigned int max,
  199. atomic_t *attr)
  200. {
  201. unsigned long uint_val;
  202. int ret;
  203. ret = kstrtoul(buff, 10, &uint_val);
  204. if (ret) {
  205. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  206. attr_name, buff);
  207. return -EINVAL;
  208. }
  209. if (uint_val < min) {
  210. batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  211. attr_name, uint_val, min);
  212. return -EINVAL;
  213. }
  214. if (uint_val > max) {
  215. batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  216. attr_name, uint_val, max);
  217. return -EINVAL;
  218. }
  219. if (atomic_read(attr) == uint_val)
  220. return count;
  221. batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
  222. attr_name, atomic_read(attr), uint_val);
  223. atomic_set(attr, uint_val);
  224. return count;
  225. }
  226. static inline ssize_t
  227. __batadv_store_uint_attr(const char *buff, size_t count,
  228. int min, int max,
  229. void (*post_func)(struct net_device *),
  230. const struct attribute *attr,
  231. atomic_t *attr_store, struct net_device *net_dev)
  232. {
  233. int ret;
  234. ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
  235. attr_store);
  236. if (post_func && ret)
  237. post_func(net_dev);
  238. return ret;
  239. }
  240. static ssize_t batadv_show_vis_mode(struct kobject *kobj,
  241. struct attribute *attr, char *buff)
  242. {
  243. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  244. int vis_mode = atomic_read(&bat_priv->vis_mode);
  245. return sprintf(buff, "%s\n",
  246. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  247. "client" : "server");
  248. }
  249. static ssize_t batadv_store_vis_mode(struct kobject *kobj,
  250. struct attribute *attr, char *buff,
  251. size_t count)
  252. {
  253. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  254. struct bat_priv *bat_priv = netdev_priv(net_dev);
  255. unsigned long val;
  256. int ret, vis_mode_tmp = -1;
  257. const char *old_mode, *new_mode;
  258. ret = kstrtoul(buff, 10, &val);
  259. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  260. (strncmp(buff, "client", 6) == 0) ||
  261. (strncmp(buff, "off", 3) == 0))
  262. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  263. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  264. (strncmp(buff, "server", 6) == 0))
  265. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  266. if (vis_mode_tmp < 0) {
  267. if (buff[count - 1] == '\n')
  268. buff[count - 1] = '\0';
  269. batadv_info(net_dev,
  270. "Invalid parameter for 'vis mode' setting received: %s\n",
  271. buff);
  272. return -EINVAL;
  273. }
  274. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  275. return count;
  276. if (atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE)
  277. old_mode = "client";
  278. else
  279. old_mode = "server";
  280. if (vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE)
  281. new_mode = "client";
  282. else
  283. new_mode = "server";
  284. batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
  285. new_mode);
  286. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  287. return count;
  288. }
  289. static ssize_t batadv_show_bat_algo(struct kobject *kobj,
  290. struct attribute *attr, char *buff)
  291. {
  292. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  293. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  294. }
  295. static void batadv_post_gw_deselect(struct net_device *net_dev)
  296. {
  297. struct bat_priv *bat_priv = netdev_priv(net_dev);
  298. batadv_gw_deselect(bat_priv);
  299. }
  300. static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
  301. char *buff)
  302. {
  303. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  304. int bytes_written;
  305. switch (atomic_read(&bat_priv->gw_mode)) {
  306. case BATADV_GW_MODE_CLIENT:
  307. bytes_written = sprintf(buff, "%s\n",
  308. BATADV_GW_MODE_CLIENT_NAME);
  309. break;
  310. case BATADV_GW_MODE_SERVER:
  311. bytes_written = sprintf(buff, "%s\n",
  312. BATADV_GW_MODE_SERVER_NAME);
  313. break;
  314. default:
  315. bytes_written = sprintf(buff, "%s\n",
  316. BATADV_GW_MODE_OFF_NAME);
  317. break;
  318. }
  319. return bytes_written;
  320. }
  321. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  322. struct attribute *attr, char *buff,
  323. size_t count)
  324. {
  325. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  326. struct bat_priv *bat_priv = netdev_priv(net_dev);
  327. char *curr_gw_mode_str;
  328. int gw_mode_tmp = -1;
  329. if (buff[count - 1] == '\n')
  330. buff[count - 1] = '\0';
  331. if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
  332. strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
  333. gw_mode_tmp = BATADV_GW_MODE_OFF;
  334. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  335. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  336. gw_mode_tmp = BATADV_GW_MODE_CLIENT;
  337. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  338. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  339. gw_mode_tmp = BATADV_GW_MODE_SERVER;
  340. if (gw_mode_tmp < 0) {
  341. batadv_info(net_dev,
  342. "Invalid parameter for 'gw mode' setting received: %s\n",
  343. buff);
  344. return -EINVAL;
  345. }
  346. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  347. return count;
  348. switch (atomic_read(&bat_priv->gw_mode)) {
  349. case BATADV_GW_MODE_CLIENT:
  350. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  351. break;
  352. case BATADV_GW_MODE_SERVER:
  353. curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
  354. break;
  355. default:
  356. curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
  357. break;
  358. }
  359. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  360. curr_gw_mode_str, buff);
  361. batadv_gw_deselect(bat_priv);
  362. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  363. return count;
  364. }
  365. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  366. struct attribute *attr, char *buff)
  367. {
  368. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  369. int down, up;
  370. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  371. batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  372. return sprintf(buff, "%i%s/%i%s\n",
  373. (down > 2048 ? down / 1024 : down),
  374. (down > 2048 ? "MBit" : "KBit"),
  375. (up > 2048 ? up / 1024 : up),
  376. (up > 2048 ? "MBit" : "KBit"));
  377. }
  378. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  379. struct attribute *attr, char *buff,
  380. size_t count)
  381. {
  382. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  383. if (buff[count - 1] == '\n')
  384. buff[count - 1] = '\0';
  385. return batadv_gw_bandwidth_set(net_dev, buff, count);
  386. }
  387. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  388. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  389. #ifdef CONFIG_BATMAN_ADV_BLA
  390. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  391. #endif
  392. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  393. BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  394. static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
  395. batadv_store_vis_mode);
  396. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  397. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  398. batadv_store_gw_mode);
  399. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
  400. INT_MAX, NULL);
  401. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
  402. NULL);
  403. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
  404. batadv_post_gw_deselect);
  405. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  406. batadv_store_gw_bwidth);
  407. #ifdef CONFIG_BATMAN_ADV_DEBUG
  408. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
  409. #endif
  410. static struct bat_attribute *batadv_mesh_attrs[] = {
  411. &batadv_attr_aggregated_ogms,
  412. &batadv_attr_bonding,
  413. #ifdef CONFIG_BATMAN_ADV_BLA
  414. &batadv_attr_bridge_loop_avoidance,
  415. #endif
  416. &batadv_attr_fragmentation,
  417. &batadv_attr_ap_isolation,
  418. &batadv_attr_vis_mode,
  419. &batadv_attr_routing_algo,
  420. &batadv_attr_gw_mode,
  421. &batadv_attr_orig_interval,
  422. &batadv_attr_hop_penalty,
  423. &batadv_attr_gw_sel_class,
  424. &batadv_attr_gw_bandwidth,
  425. #ifdef CONFIG_BATMAN_ADV_DEBUG
  426. &batadv_attr_log_level,
  427. #endif
  428. NULL,
  429. };
  430. int batadv_sysfs_add_meshif(struct net_device *dev)
  431. {
  432. struct kobject *batif_kobject = &dev->dev.kobj;
  433. struct bat_priv *bat_priv = netdev_priv(dev);
  434. struct bat_attribute **bat_attr;
  435. int err;
  436. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  437. batif_kobject);
  438. if (!bat_priv->mesh_obj) {
  439. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  440. BATADV_SYSFS_IF_MESH_SUBDIR);
  441. goto out;
  442. }
  443. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  444. err = sysfs_create_file(bat_priv->mesh_obj,
  445. &((*bat_attr)->attr));
  446. if (err) {
  447. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  448. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  449. ((*bat_attr)->attr).name);
  450. goto rem_attr;
  451. }
  452. }
  453. return 0;
  454. rem_attr:
  455. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  456. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  457. kobject_put(bat_priv->mesh_obj);
  458. bat_priv->mesh_obj = NULL;
  459. out:
  460. return -ENOMEM;
  461. }
  462. void batadv_sysfs_del_meshif(struct net_device *dev)
  463. {
  464. struct bat_priv *bat_priv = netdev_priv(dev);
  465. struct bat_attribute **bat_attr;
  466. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  467. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  468. kobject_put(bat_priv->mesh_obj);
  469. bat_priv->mesh_obj = NULL;
  470. }
  471. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  472. struct attribute *attr, char *buff)
  473. {
  474. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  475. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  476. ssize_t length;
  477. if (!hard_iface)
  478. return 0;
  479. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  480. "none" : hard_iface->soft_iface->name);
  481. batadv_hardif_free_ref(hard_iface);
  482. return length;
  483. }
  484. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  485. struct attribute *attr, char *buff,
  486. size_t count)
  487. {
  488. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  489. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  490. int status_tmp = -1;
  491. int ret = count;
  492. if (!hard_iface)
  493. return count;
  494. if (buff[count - 1] == '\n')
  495. buff[count - 1] = '\0';
  496. if (strlen(buff) >= IFNAMSIZ) {
  497. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  498. buff);
  499. batadv_hardif_free_ref(hard_iface);
  500. return -EINVAL;
  501. }
  502. if (strncmp(buff, "none", 4) == 0)
  503. status_tmp = IF_NOT_IN_USE;
  504. else
  505. status_tmp = IF_I_WANT_YOU;
  506. if (hard_iface->if_status == status_tmp)
  507. goto out;
  508. if ((hard_iface->soft_iface) &&
  509. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  510. goto out;
  511. if (!rtnl_trylock()) {
  512. ret = -ERESTARTSYS;
  513. goto out;
  514. }
  515. if (status_tmp == IF_NOT_IN_USE) {
  516. batadv_hardif_disable_interface(hard_iface);
  517. goto unlock;
  518. }
  519. /* if the interface already is in use */
  520. if (hard_iface->if_status != IF_NOT_IN_USE)
  521. batadv_hardif_disable_interface(hard_iface);
  522. ret = batadv_hardif_enable_interface(hard_iface, buff);
  523. unlock:
  524. rtnl_unlock();
  525. out:
  526. batadv_hardif_free_ref(hard_iface);
  527. return ret;
  528. }
  529. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  530. struct attribute *attr, char *buff)
  531. {
  532. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  533. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  534. ssize_t length;
  535. if (!hard_iface)
  536. return 0;
  537. switch (hard_iface->if_status) {
  538. case IF_TO_BE_REMOVED:
  539. length = sprintf(buff, "disabling\n");
  540. break;
  541. case IF_INACTIVE:
  542. length = sprintf(buff, "inactive\n");
  543. break;
  544. case IF_ACTIVE:
  545. length = sprintf(buff, "active\n");
  546. break;
  547. case IF_TO_BE_ACTIVATED:
  548. length = sprintf(buff, "enabling\n");
  549. break;
  550. case IF_NOT_IN_USE:
  551. default:
  552. length = sprintf(buff, "not in use\n");
  553. break;
  554. }
  555. batadv_hardif_free_ref(hard_iface);
  556. return length;
  557. }
  558. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  559. batadv_store_mesh_iface);
  560. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  561. static struct bat_attribute *batadv_batman_attrs[] = {
  562. &batadv_attr_mesh_iface,
  563. &batadv_attr_iface_status,
  564. NULL,
  565. };
  566. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  567. {
  568. struct kobject *hardif_kobject = &dev->dev.kobj;
  569. struct bat_attribute **bat_attr;
  570. int err;
  571. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  572. hardif_kobject);
  573. if (!*hardif_obj) {
  574. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  575. BATADV_SYSFS_IF_BAT_SUBDIR);
  576. goto out;
  577. }
  578. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  579. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  580. if (err) {
  581. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  582. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  583. ((*bat_attr)->attr).name);
  584. goto rem_attr;
  585. }
  586. }
  587. return 0;
  588. rem_attr:
  589. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  590. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  591. out:
  592. return -ENOMEM;
  593. }
  594. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  595. {
  596. kobject_put(*hardif_obj);
  597. *hardif_obj = NULL;
  598. }
  599. int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  600. enum uev_action action, const char *data)
  601. {
  602. int ret = -ENOMEM;
  603. struct hard_iface *primary_if = NULL;
  604. struct kobject *bat_kobj;
  605. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  606. primary_if = batadv_primary_if_get_selected(bat_priv);
  607. if (!primary_if)
  608. goto out;
  609. bat_kobj = &primary_if->soft_iface->dev.kobj;
  610. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  611. strlen(batadv_uev_type_str[type]) + 1,
  612. GFP_ATOMIC);
  613. if (!uevent_env[0])
  614. goto out;
  615. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  616. batadv_uev_type_str[type]);
  617. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  618. strlen(batadv_uev_action_str[action]) + 1,
  619. GFP_ATOMIC);
  620. if (!uevent_env[1])
  621. goto out;
  622. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  623. batadv_uev_action_str[action]);
  624. /* If the event is DEL, ignore the data field */
  625. if (action != UEV_DEL) {
  626. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  627. strlen(data) + 1, GFP_ATOMIC);
  628. if (!uevent_env[2])
  629. goto out;
  630. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  631. }
  632. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  633. out:
  634. kfree(uevent_env[0]);
  635. kfree(uevent_env[1]);
  636. kfree(uevent_env[2]);
  637. if (primary_if)
  638. batadv_hardif_free_ref(primary_if);
  639. if (ret)
  640. batadv_dbg(DBG_BATMAN, bat_priv,
  641. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  642. batadv_uev_type_str[type],
  643. batadv_uev_action_str[action],
  644. (action == UEV_DEL ? "NULL" : data), ret);
  645. return ret;
  646. }