bat_sysfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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", GW_MODE_CLIENT_NAME);
  308. break;
  309. case GW_MODE_SERVER:
  310. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  311. break;
  312. default:
  313. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  314. break;
  315. }
  316. return bytes_written;
  317. }
  318. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  319. struct attribute *attr, char *buff,
  320. size_t count)
  321. {
  322. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  323. struct bat_priv *bat_priv = netdev_priv(net_dev);
  324. char *curr_gw_mode_str;
  325. int gw_mode_tmp = -1;
  326. if (buff[count - 1] == '\n')
  327. buff[count - 1] = '\0';
  328. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  329. gw_mode_tmp = GW_MODE_OFF;
  330. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  331. strlen(GW_MODE_CLIENT_NAME)) == 0)
  332. gw_mode_tmp = GW_MODE_CLIENT;
  333. if (strncmp(buff, GW_MODE_SERVER_NAME,
  334. strlen(GW_MODE_SERVER_NAME)) == 0)
  335. gw_mode_tmp = GW_MODE_SERVER;
  336. if (gw_mode_tmp < 0) {
  337. batadv_info(net_dev,
  338. "Invalid parameter for 'gw mode' setting received: %s\n",
  339. buff);
  340. return -EINVAL;
  341. }
  342. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  343. return count;
  344. switch (atomic_read(&bat_priv->gw_mode)) {
  345. case GW_MODE_CLIENT:
  346. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  347. break;
  348. case GW_MODE_SERVER:
  349. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  350. break;
  351. default:
  352. curr_gw_mode_str = GW_MODE_OFF_NAME;
  353. break;
  354. }
  355. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  356. curr_gw_mode_str, buff);
  357. batadv_gw_deselect(bat_priv);
  358. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  359. return count;
  360. }
  361. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  362. struct attribute *attr, char *buff)
  363. {
  364. struct bat_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  365. int down, up;
  366. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  367. batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  368. return sprintf(buff, "%i%s/%i%s\n",
  369. (down > 2048 ? down / 1024 : down),
  370. (down > 2048 ? "MBit" : "KBit"),
  371. (up > 2048 ? up / 1024 : up),
  372. (up > 2048 ? "MBit" : "KBit"));
  373. }
  374. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  375. struct attribute *attr, char *buff,
  376. size_t count)
  377. {
  378. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  379. if (buff[count - 1] == '\n')
  380. buff[count - 1] = '\0';
  381. return batadv_gw_bandwidth_set(net_dev, buff, count);
  382. }
  383. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  384. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  385. #ifdef CONFIG_BATMAN_ADV_BLA
  386. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  387. #endif
  388. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  389. BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  390. static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
  391. batadv_store_vis_mode);
  392. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  393. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  394. batadv_store_gw_mode);
  395. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX,
  396. NULL);
  397. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  398. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  399. batadv_post_gw_deselect);
  400. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  401. batadv_store_gw_bwidth);
  402. #ifdef CONFIG_BATMAN_ADV_DEBUG
  403. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, DBG_ALL, NULL);
  404. #endif
  405. static struct bat_attribute *batadv_mesh_attrs[] = {
  406. &batadv_attr_aggregated_ogms,
  407. &batadv_attr_bonding,
  408. #ifdef CONFIG_BATMAN_ADV_BLA
  409. &batadv_attr_bridge_loop_avoidance,
  410. #endif
  411. &batadv_attr_fragmentation,
  412. &batadv_attr_ap_isolation,
  413. &batadv_attr_vis_mode,
  414. &batadv_attr_routing_algo,
  415. &batadv_attr_gw_mode,
  416. &batadv_attr_orig_interval,
  417. &batadv_attr_hop_penalty,
  418. &batadv_attr_gw_sel_class,
  419. &batadv_attr_gw_bandwidth,
  420. #ifdef CONFIG_BATMAN_ADV_DEBUG
  421. &batadv_attr_log_level,
  422. #endif
  423. NULL,
  424. };
  425. int batadv_sysfs_add_meshif(struct net_device *dev)
  426. {
  427. struct kobject *batif_kobject = &dev->dev.kobj;
  428. struct bat_priv *bat_priv = netdev_priv(dev);
  429. struct bat_attribute **bat_attr;
  430. int err;
  431. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  432. batif_kobject);
  433. if (!bat_priv->mesh_obj) {
  434. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  435. BATADV_SYSFS_IF_MESH_SUBDIR);
  436. goto out;
  437. }
  438. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  439. err = sysfs_create_file(bat_priv->mesh_obj,
  440. &((*bat_attr)->attr));
  441. if (err) {
  442. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  443. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  444. ((*bat_attr)->attr).name);
  445. goto rem_attr;
  446. }
  447. }
  448. return 0;
  449. rem_attr:
  450. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  451. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  452. kobject_put(bat_priv->mesh_obj);
  453. bat_priv->mesh_obj = NULL;
  454. out:
  455. return -ENOMEM;
  456. }
  457. void batadv_sysfs_del_meshif(struct net_device *dev)
  458. {
  459. struct bat_priv *bat_priv = netdev_priv(dev);
  460. struct bat_attribute **bat_attr;
  461. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  462. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  463. kobject_put(bat_priv->mesh_obj);
  464. bat_priv->mesh_obj = NULL;
  465. }
  466. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  467. struct attribute *attr, char *buff)
  468. {
  469. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  470. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  471. ssize_t length;
  472. if (!hard_iface)
  473. return 0;
  474. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  475. "none" : hard_iface->soft_iface->name);
  476. batadv_hardif_free_ref(hard_iface);
  477. return length;
  478. }
  479. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  480. struct attribute *attr, char *buff,
  481. size_t count)
  482. {
  483. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  484. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  485. int status_tmp = -1;
  486. int ret = count;
  487. if (!hard_iface)
  488. return count;
  489. if (buff[count - 1] == '\n')
  490. buff[count - 1] = '\0';
  491. if (strlen(buff) >= IFNAMSIZ) {
  492. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  493. buff);
  494. batadv_hardif_free_ref(hard_iface);
  495. return -EINVAL;
  496. }
  497. if (strncmp(buff, "none", 4) == 0)
  498. status_tmp = IF_NOT_IN_USE;
  499. else
  500. status_tmp = IF_I_WANT_YOU;
  501. if (hard_iface->if_status == status_tmp)
  502. goto out;
  503. if ((hard_iface->soft_iface) &&
  504. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  505. goto out;
  506. if (!rtnl_trylock()) {
  507. ret = -ERESTARTSYS;
  508. goto out;
  509. }
  510. if (status_tmp == IF_NOT_IN_USE) {
  511. batadv_hardif_disable_interface(hard_iface);
  512. goto unlock;
  513. }
  514. /* if the interface already is in use */
  515. if (hard_iface->if_status != IF_NOT_IN_USE)
  516. batadv_hardif_disable_interface(hard_iface);
  517. ret = batadv_hardif_enable_interface(hard_iface, buff);
  518. unlock:
  519. rtnl_unlock();
  520. out:
  521. batadv_hardif_free_ref(hard_iface);
  522. return ret;
  523. }
  524. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  525. struct attribute *attr, char *buff)
  526. {
  527. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  528. struct hard_iface *hard_iface = batadv_hardif_get_by_netdev(net_dev);
  529. ssize_t length;
  530. if (!hard_iface)
  531. return 0;
  532. switch (hard_iface->if_status) {
  533. case IF_TO_BE_REMOVED:
  534. length = sprintf(buff, "disabling\n");
  535. break;
  536. case IF_INACTIVE:
  537. length = sprintf(buff, "inactive\n");
  538. break;
  539. case IF_ACTIVE:
  540. length = sprintf(buff, "active\n");
  541. break;
  542. case IF_TO_BE_ACTIVATED:
  543. length = sprintf(buff, "enabling\n");
  544. break;
  545. case IF_NOT_IN_USE:
  546. default:
  547. length = sprintf(buff, "not in use\n");
  548. break;
  549. }
  550. batadv_hardif_free_ref(hard_iface);
  551. return length;
  552. }
  553. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  554. batadv_store_mesh_iface);
  555. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  556. static struct bat_attribute *batadv_batman_attrs[] = {
  557. &batadv_attr_mesh_iface,
  558. &batadv_attr_iface_status,
  559. NULL,
  560. };
  561. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  562. {
  563. struct kobject *hardif_kobject = &dev->dev.kobj;
  564. struct bat_attribute **bat_attr;
  565. int err;
  566. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  567. hardif_kobject);
  568. if (!*hardif_obj) {
  569. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  570. BATADV_SYSFS_IF_BAT_SUBDIR);
  571. goto out;
  572. }
  573. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  574. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  575. if (err) {
  576. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  577. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  578. ((*bat_attr)->attr).name);
  579. goto rem_attr;
  580. }
  581. }
  582. return 0;
  583. rem_attr:
  584. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  585. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  586. out:
  587. return -ENOMEM;
  588. }
  589. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  590. {
  591. kobject_put(*hardif_obj);
  592. *hardif_obj = NULL;
  593. }
  594. int batadv_throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  595. enum uev_action action, const char *data)
  596. {
  597. int ret = -ENOMEM;
  598. struct hard_iface *primary_if = NULL;
  599. struct kobject *bat_kobj;
  600. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  601. primary_if = batadv_primary_if_get_selected(bat_priv);
  602. if (!primary_if)
  603. goto out;
  604. bat_kobj = &primary_if->soft_iface->dev.kobj;
  605. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  606. strlen(batadv_uev_type_str[type]) + 1,
  607. GFP_ATOMIC);
  608. if (!uevent_env[0])
  609. goto out;
  610. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  611. batadv_uev_type_str[type]);
  612. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  613. strlen(batadv_uev_action_str[action]) + 1,
  614. GFP_ATOMIC);
  615. if (!uevent_env[1])
  616. goto out;
  617. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  618. batadv_uev_action_str[action]);
  619. /* If the event is DEL, ignore the data field */
  620. if (action != UEV_DEL) {
  621. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  622. strlen(data) + 1, GFP_ATOMIC);
  623. if (!uevent_env[2])
  624. goto out;
  625. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  626. }
  627. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  628. out:
  629. kfree(uevent_env[0]);
  630. kfree(uevent_env[1]);
  631. kfree(uevent_env[2]);
  632. if (primary_if)
  633. batadv_hardif_free_ref(primary_if);
  634. if (ret)
  635. batadv_dbg(DBG_BATMAN, bat_priv,
  636. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  637. batadv_uev_type_str[type],
  638. batadv_uev_action_str[action],
  639. (action == UEV_DEL ? "NULL" : data), ret);
  640. return ret;
  641. }