bat_sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 GW_MODE_CLIENT:
  307. bytes_written = sprintf(buff, "%s\n",
  308. BATADV_GW_MODE_CLIENT_NAME);
  309. break;
  310. case 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 = GW_MODE_OFF;
  334. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  335. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  336. gw_mode_tmp = GW_MODE_CLIENT;
  337. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  338. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  339. gw_mode_tmp = 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 GW_MODE_CLIENT:
  350. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  351. break;
  352. case 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 * JITTER, INT_MAX,
  400. NULL);
  401. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  402. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  403. batadv_post_gw_deselect);
  404. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  405. batadv_store_gw_bwidth);
  406. #ifdef CONFIG_BATMAN_ADV_DEBUG
  407. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
  408. #endif
  409. static struct bat_attribute *batadv_mesh_attrs[] = {
  410. &batadv_attr_aggregated_ogms,
  411. &batadv_attr_bonding,
  412. #ifdef CONFIG_BATMAN_ADV_BLA
  413. &batadv_attr_bridge_loop_avoidance,
  414. #endif
  415. &batadv_attr_fragmentation,
  416. &batadv_attr_ap_isolation,
  417. &batadv_attr_vis_mode,
  418. &batadv_attr_routing_algo,
  419. &batadv_attr_gw_mode,
  420. &batadv_attr_orig_interval,
  421. &batadv_attr_hop_penalty,
  422. &batadv_attr_gw_sel_class,
  423. &batadv_attr_gw_bandwidth,
  424. #ifdef CONFIG_BATMAN_ADV_DEBUG
  425. &batadv_attr_log_level,
  426. #endif
  427. NULL,
  428. };
  429. int batadv_sysfs_add_meshif(struct net_device *dev)
  430. {
  431. struct kobject *batif_kobject = &dev->dev.kobj;
  432. struct bat_priv *bat_priv = netdev_priv(dev);
  433. struct bat_attribute **bat_attr;
  434. int err;
  435. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  436. batif_kobject);
  437. if (!bat_priv->mesh_obj) {
  438. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  439. BATADV_SYSFS_IF_MESH_SUBDIR);
  440. goto out;
  441. }
  442. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  443. err = sysfs_create_file(bat_priv->mesh_obj,
  444. &((*bat_attr)->attr));
  445. if (err) {
  446. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  447. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  448. ((*bat_attr)->attr).name);
  449. goto rem_attr;
  450. }
  451. }
  452. return 0;
  453. rem_attr:
  454. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  455. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  456. kobject_put(bat_priv->mesh_obj);
  457. bat_priv->mesh_obj = NULL;
  458. out:
  459. return -ENOMEM;
  460. }
  461. void batadv_sysfs_del_meshif(struct net_device *dev)
  462. {
  463. struct bat_priv *bat_priv = netdev_priv(dev);
  464. struct bat_attribute **bat_attr;
  465. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  466. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  467. kobject_put(bat_priv->mesh_obj);
  468. bat_priv->mesh_obj = NULL;
  469. }
  470. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  471. struct attribute *attr, char *buff)
  472. {
  473. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  474. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  475. ssize_t length;
  476. if (!hard_iface)
  477. return 0;
  478. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  479. "none" : hard_iface->soft_iface->name);
  480. batadv_hardif_free_ref(hard_iface);
  481. return length;
  482. }
  483. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  484. struct attribute *attr, char *buff,
  485. size_t count)
  486. {
  487. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  488. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  489. int status_tmp = -1;
  490. int ret = count;
  491. if (!hard_iface)
  492. return count;
  493. if (buff[count - 1] == '\n')
  494. buff[count - 1] = '\0';
  495. if (strlen(buff) >= IFNAMSIZ) {
  496. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  497. buff);
  498. batadv_hardif_free_ref(hard_iface);
  499. return -EINVAL;
  500. }
  501. if (strncmp(buff, "none", 4) == 0)
  502. status_tmp = IF_NOT_IN_USE;
  503. else
  504. status_tmp = IF_I_WANT_YOU;
  505. if (hard_iface->if_status == status_tmp)
  506. goto out;
  507. if ((hard_iface->soft_iface) &&
  508. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  509. goto out;
  510. if (!rtnl_trylock()) {
  511. ret = -ERESTARTSYS;
  512. goto out;
  513. }
  514. if (status_tmp == IF_NOT_IN_USE) {
  515. batadv_hardif_disable_interface(hard_iface);
  516. goto unlock;
  517. }
  518. /* if the interface already is in use */
  519. if (hard_iface->if_status != IF_NOT_IN_USE)
  520. batadv_hardif_disable_interface(hard_iface);
  521. ret = batadv_hardif_enable_interface(hard_iface, buff);
  522. unlock:
  523. rtnl_unlock();
  524. out:
  525. batadv_hardif_free_ref(hard_iface);
  526. return ret;
  527. }
  528. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  529. struct attribute *attr, char *buff)
  530. {
  531. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  532. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  533. ssize_t length;
  534. if (!hard_iface)
  535. return 0;
  536. switch (hard_iface->if_status) {
  537. case IF_TO_BE_REMOVED:
  538. length = sprintf(buff, "disabling\n");
  539. break;
  540. case IF_INACTIVE:
  541. length = sprintf(buff, "inactive\n");
  542. break;
  543. case IF_ACTIVE:
  544. length = sprintf(buff, "active\n");
  545. break;
  546. case IF_TO_BE_ACTIVATED:
  547. length = sprintf(buff, "enabling\n");
  548. break;
  549. case IF_NOT_IN_USE:
  550. default:
  551. length = sprintf(buff, "not in use\n");
  552. break;
  553. }
  554. batadv_hardif_free_ref(hard_iface);
  555. return length;
  556. }
  557. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  558. batadv_store_mesh_iface);
  559. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  560. static struct bat_attribute *batadv_batman_attrs[] = {
  561. &batadv_attr_mesh_iface,
  562. &batadv_attr_iface_status,
  563. NULL,
  564. };
  565. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  566. {
  567. struct kobject *hardif_kobject = &dev->dev.kobj;
  568. struct bat_attribute **bat_attr;
  569. int err;
  570. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  571. hardif_kobject);
  572. if (!*hardif_obj) {
  573. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  574. BATADV_SYSFS_IF_BAT_SUBDIR);
  575. goto out;
  576. }
  577. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  578. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  579. if (err) {
  580. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  581. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  582. ((*bat_attr)->attr).name);
  583. goto rem_attr;
  584. }
  585. }
  586. return 0;
  587. rem_attr:
  588. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  589. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  590. out:
  591. return -ENOMEM;
  592. }
  593. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  594. {
  595. kobject_put(*hardif_obj);
  596. *hardif_obj = NULL;
  597. }
  598. int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  599. enum uev_action action, const char *data)
  600. {
  601. int ret = -ENOMEM;
  602. struct hard_iface *primary_if = NULL;
  603. struct kobject *bat_kobj;
  604. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  605. primary_if = batadv_primary_if_get_selected(bat_priv);
  606. if (!primary_if)
  607. goto out;
  608. bat_kobj = &primary_if->soft_iface->dev.kobj;
  609. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  610. strlen(batadv_uev_type_str[type]) + 1,
  611. GFP_ATOMIC);
  612. if (!uevent_env[0])
  613. goto out;
  614. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  615. batadv_uev_type_str[type]);
  616. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  617. strlen(batadv_uev_action_str[action]) + 1,
  618. GFP_ATOMIC);
  619. if (!uevent_env[1])
  620. goto out;
  621. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  622. batadv_uev_action_str[action]);
  623. /* If the event is DEL, ignore the data field */
  624. if (action != UEV_DEL) {
  625. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  626. strlen(data) + 1, GFP_ATOMIC);
  627. if (!uevent_env[2])
  628. goto out;
  629. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  630. }
  631. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  632. out:
  633. kfree(uevent_env[0]);
  634. kfree(uevent_env[1]);
  635. kfree(uevent_env[2]);
  636. if (primary_if)
  637. batadv_hardif_free_ref(primary_if);
  638. if (ret)
  639. batadv_dbg(DBG_BATMAN, bat_priv,
  640. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  641. batadv_uev_type_str[type],
  642. batadv_uev_action_str[action],
  643. (action == UEV_DEL ? "NULL" : data), ret);
  644. return ret;
  645. }