sysfs.c 21 KB

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