sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  1. /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "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. static int batadv_store_bool_attr(char *buff, size_t count,
  110. struct net_device *net_dev,
  111. const char *attr_name, atomic_t *attr)
  112. {
  113. int enabled = -1;
  114. if (buff[count - 1] == '\n')
  115. buff[count - 1] = '\0';
  116. if ((strncmp(buff, "1", 2) == 0) ||
  117. (strncmp(buff, "enable", 7) == 0) ||
  118. (strncmp(buff, "enabled", 8) == 0))
  119. enabled = 1;
  120. if ((strncmp(buff, "0", 2) == 0) ||
  121. (strncmp(buff, "disable", 8) == 0) ||
  122. (strncmp(buff, "disabled", 9) == 0))
  123. enabled = 0;
  124. if (enabled < 0) {
  125. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  126. attr_name, buff);
  127. return -EINVAL;
  128. }
  129. if (atomic_read(attr) == enabled)
  130. return count;
  131. batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  132. atomic_read(attr) == 1 ? "enabled" : "disabled",
  133. enabled == 1 ? "enabled" : "disabled");
  134. atomic_set(attr, (unsigned int)enabled);
  135. return count;
  136. }
  137. static inline ssize_t
  138. __batadv_store_bool_attr(char *buff, size_t count,
  139. void (*post_func)(struct net_device *),
  140. struct attribute *attr,
  141. atomic_t *attr_store, struct net_device *net_dev)
  142. {
  143. int ret;
  144. ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
  145. attr_store);
  146. if (post_func && ret)
  147. post_func(net_dev);
  148. return ret;
  149. }
  150. static int batadv_store_uint_attr(const char *buff, size_t count,
  151. struct net_device *net_dev,
  152. const char *attr_name,
  153. unsigned int min, unsigned int max,
  154. atomic_t *attr)
  155. {
  156. unsigned long uint_val;
  157. int ret;
  158. ret = kstrtoul(buff, 10, &uint_val);
  159. if (ret) {
  160. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  161. attr_name, buff);
  162. return -EINVAL;
  163. }
  164. if (uint_val < min) {
  165. batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  166. attr_name, uint_val, min);
  167. return -EINVAL;
  168. }
  169. if (uint_val > max) {
  170. batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  171. attr_name, uint_val, max);
  172. return -EINVAL;
  173. }
  174. if (atomic_read(attr) == uint_val)
  175. return count;
  176. batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
  177. attr_name, atomic_read(attr), uint_val);
  178. atomic_set(attr, uint_val);
  179. return count;
  180. }
  181. static inline ssize_t
  182. __batadv_store_uint_attr(const char *buff, size_t count,
  183. int min, int max,
  184. void (*post_func)(struct net_device *),
  185. const struct attribute *attr,
  186. atomic_t *attr_store, struct net_device *net_dev)
  187. {
  188. int ret;
  189. ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
  190. attr_store);
  191. if (post_func && ret)
  192. post_func(net_dev);
  193. return ret;
  194. }
  195. static ssize_t batadv_show_vis_mode(struct kobject *kobj,
  196. struct attribute *attr, char *buff)
  197. {
  198. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  199. int vis_mode = atomic_read(&bat_priv->vis_mode);
  200. const char *mode;
  201. if (vis_mode == BATADV_VIS_TYPE_CLIENT_UPDATE)
  202. mode = "client";
  203. else
  204. mode = "server";
  205. return sprintf(buff, "%s\n", mode);
  206. }
  207. static ssize_t batadv_store_vis_mode(struct kobject *kobj,
  208. struct attribute *attr, char *buff,
  209. size_t count)
  210. {
  211. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  212. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  213. unsigned long val;
  214. int ret, vis_mode_tmp = -1;
  215. const char *old_mode, *new_mode;
  216. ret = kstrtoul(buff, 10, &val);
  217. if (((count == 2) && (!ret) &&
  218. (val == BATADV_VIS_TYPE_CLIENT_UPDATE)) ||
  219. (strncmp(buff, "client", 6) == 0) ||
  220. (strncmp(buff, "off", 3) == 0))
  221. vis_mode_tmp = BATADV_VIS_TYPE_CLIENT_UPDATE;
  222. if (((count == 2) && (!ret) &&
  223. (val == BATADV_VIS_TYPE_SERVER_SYNC)) ||
  224. (strncmp(buff, "server", 6) == 0))
  225. vis_mode_tmp = BATADV_VIS_TYPE_SERVER_SYNC;
  226. if (vis_mode_tmp < 0) {
  227. if (buff[count - 1] == '\n')
  228. buff[count - 1] = '\0';
  229. batadv_info(net_dev,
  230. "Invalid parameter for 'vis mode' setting received: %s\n",
  231. buff);
  232. return -EINVAL;
  233. }
  234. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  235. return count;
  236. if (atomic_read(&bat_priv->vis_mode) == BATADV_VIS_TYPE_CLIENT_UPDATE)
  237. old_mode = "client";
  238. else
  239. old_mode = "server";
  240. if (vis_mode_tmp == BATADV_VIS_TYPE_CLIENT_UPDATE)
  241. new_mode = "client";
  242. else
  243. new_mode = "server";
  244. batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
  245. new_mode);
  246. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  247. return count;
  248. }
  249. static ssize_t batadv_show_bat_algo(struct kobject *kobj,
  250. struct attribute *attr, char *buff)
  251. {
  252. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  253. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  254. }
  255. static void batadv_post_gw_deselect(struct net_device *net_dev)
  256. {
  257. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  258. batadv_gw_deselect(bat_priv);
  259. }
  260. static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
  261. char *buff)
  262. {
  263. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  264. int bytes_written;
  265. switch (atomic_read(&bat_priv->gw_mode)) {
  266. case BATADV_GW_MODE_CLIENT:
  267. bytes_written = sprintf(buff, "%s\n",
  268. BATADV_GW_MODE_CLIENT_NAME);
  269. break;
  270. case BATADV_GW_MODE_SERVER:
  271. bytes_written = sprintf(buff, "%s\n",
  272. BATADV_GW_MODE_SERVER_NAME);
  273. break;
  274. default:
  275. bytes_written = sprintf(buff, "%s\n",
  276. BATADV_GW_MODE_OFF_NAME);
  277. break;
  278. }
  279. return bytes_written;
  280. }
  281. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  282. struct attribute *attr, char *buff,
  283. size_t count)
  284. {
  285. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  286. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  287. char *curr_gw_mode_str;
  288. int gw_mode_tmp = -1;
  289. if (buff[count - 1] == '\n')
  290. buff[count - 1] = '\0';
  291. if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
  292. strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
  293. gw_mode_tmp = BATADV_GW_MODE_OFF;
  294. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  295. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  296. gw_mode_tmp = BATADV_GW_MODE_CLIENT;
  297. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  298. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  299. gw_mode_tmp = BATADV_GW_MODE_SERVER;
  300. if (gw_mode_tmp < 0) {
  301. batadv_info(net_dev,
  302. "Invalid parameter for 'gw mode' setting received: %s\n",
  303. buff);
  304. return -EINVAL;
  305. }
  306. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  307. return count;
  308. switch (atomic_read(&bat_priv->gw_mode)) {
  309. case BATADV_GW_MODE_CLIENT:
  310. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  311. break;
  312. case BATADV_GW_MODE_SERVER:
  313. curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
  314. break;
  315. default:
  316. curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
  317. break;
  318. }
  319. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  320. curr_gw_mode_str, buff);
  321. batadv_gw_deselect(bat_priv);
  322. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  323. return count;
  324. }
  325. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  326. struct attribute *attr, char *buff)
  327. {
  328. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  329. int down, up;
  330. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  331. batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  332. return sprintf(buff, "%i%s/%i%s\n",
  333. (down > 2048 ? down / 1024 : down),
  334. (down > 2048 ? "MBit" : "KBit"),
  335. (up > 2048 ? up / 1024 : up),
  336. (up > 2048 ? "MBit" : "KBit"));
  337. }
  338. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  339. struct attribute *attr, char *buff,
  340. size_t count)
  341. {
  342. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  343. if (buff[count - 1] == '\n')
  344. buff[count - 1] = '\0';
  345. return batadv_gw_bandwidth_set(net_dev, buff, count);
  346. }
  347. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  348. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  349. #ifdef CONFIG_BATMAN_ADV_BLA
  350. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  351. #endif
  352. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  353. BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  354. static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
  355. batadv_store_vis_mode);
  356. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  357. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  358. batadv_store_gw_mode);
  359. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
  360. INT_MAX, NULL);
  361. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
  362. NULL);
  363. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
  364. batadv_post_gw_deselect);
  365. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  366. batadv_store_gw_bwidth);
  367. #ifdef CONFIG_BATMAN_ADV_DEBUG
  368. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
  369. #endif
  370. static struct batadv_attribute *batadv_mesh_attrs[] = {
  371. &batadv_attr_aggregated_ogms,
  372. &batadv_attr_bonding,
  373. #ifdef CONFIG_BATMAN_ADV_BLA
  374. &batadv_attr_bridge_loop_avoidance,
  375. #endif
  376. &batadv_attr_fragmentation,
  377. &batadv_attr_ap_isolation,
  378. &batadv_attr_vis_mode,
  379. &batadv_attr_routing_algo,
  380. &batadv_attr_gw_mode,
  381. &batadv_attr_orig_interval,
  382. &batadv_attr_hop_penalty,
  383. &batadv_attr_gw_sel_class,
  384. &batadv_attr_gw_bandwidth,
  385. #ifdef CONFIG_BATMAN_ADV_DEBUG
  386. &batadv_attr_log_level,
  387. #endif
  388. NULL,
  389. };
  390. int batadv_sysfs_add_meshif(struct net_device *dev)
  391. {
  392. struct kobject *batif_kobject = &dev->dev.kobj;
  393. struct batadv_priv *bat_priv = netdev_priv(dev);
  394. struct batadv_attribute **bat_attr;
  395. int err;
  396. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  397. batif_kobject);
  398. if (!bat_priv->mesh_obj) {
  399. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  400. BATADV_SYSFS_IF_MESH_SUBDIR);
  401. goto out;
  402. }
  403. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  404. err = sysfs_create_file(bat_priv->mesh_obj,
  405. &((*bat_attr)->attr));
  406. if (err) {
  407. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  408. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  409. ((*bat_attr)->attr).name);
  410. goto rem_attr;
  411. }
  412. }
  413. return 0;
  414. rem_attr:
  415. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  416. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  417. kobject_put(bat_priv->mesh_obj);
  418. bat_priv->mesh_obj = NULL;
  419. out:
  420. return -ENOMEM;
  421. }
  422. void batadv_sysfs_del_meshif(struct net_device *dev)
  423. {
  424. struct batadv_priv *bat_priv = netdev_priv(dev);
  425. struct batadv_attribute **bat_attr;
  426. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  427. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  428. kobject_put(bat_priv->mesh_obj);
  429. bat_priv->mesh_obj = NULL;
  430. }
  431. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  432. struct attribute *attr, char *buff)
  433. {
  434. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  435. struct batadv_hard_iface *hard_iface;
  436. ssize_t length;
  437. const char *ifname;
  438. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  439. if (!hard_iface)
  440. return 0;
  441. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  442. ifname = "none";
  443. else
  444. ifname = hard_iface->soft_iface->name;
  445. length = sprintf(buff, "%s\n", ifname);
  446. batadv_hardif_free_ref(hard_iface);
  447. return length;
  448. }
  449. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  450. struct attribute *attr, char *buff,
  451. size_t count)
  452. {
  453. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  454. struct batadv_hard_iface *hard_iface;
  455. int status_tmp = -1;
  456. int ret = count;
  457. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  458. if (!hard_iface)
  459. return count;
  460. if (buff[count - 1] == '\n')
  461. buff[count - 1] = '\0';
  462. if (strlen(buff) >= IFNAMSIZ) {
  463. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  464. buff);
  465. batadv_hardif_free_ref(hard_iface);
  466. return -EINVAL;
  467. }
  468. if (strncmp(buff, "none", 4) == 0)
  469. status_tmp = BATADV_IF_NOT_IN_USE;
  470. else
  471. status_tmp = BATADV_IF_I_WANT_YOU;
  472. if (hard_iface->if_status == status_tmp)
  473. goto out;
  474. if ((hard_iface->soft_iface) &&
  475. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  476. goto out;
  477. if (!rtnl_trylock()) {
  478. ret = -ERESTARTSYS;
  479. goto out;
  480. }
  481. if (status_tmp == BATADV_IF_NOT_IN_USE) {
  482. batadv_hardif_disable_interface(hard_iface);
  483. goto unlock;
  484. }
  485. /* if the interface already is in use */
  486. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  487. batadv_hardif_disable_interface(hard_iface);
  488. ret = batadv_hardif_enable_interface(hard_iface, buff);
  489. unlock:
  490. rtnl_unlock();
  491. out:
  492. batadv_hardif_free_ref(hard_iface);
  493. return ret;
  494. }
  495. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  496. struct attribute *attr, char *buff)
  497. {
  498. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  499. struct batadv_hard_iface *hard_iface;
  500. ssize_t length;
  501. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  502. if (!hard_iface)
  503. return 0;
  504. switch (hard_iface->if_status) {
  505. case BATADV_IF_TO_BE_REMOVED:
  506. length = sprintf(buff, "disabling\n");
  507. break;
  508. case BATADV_IF_INACTIVE:
  509. length = sprintf(buff, "inactive\n");
  510. break;
  511. case BATADV_IF_ACTIVE:
  512. length = sprintf(buff, "active\n");
  513. break;
  514. case BATADV_IF_TO_BE_ACTIVATED:
  515. length = sprintf(buff, "enabling\n");
  516. break;
  517. case BATADV_IF_NOT_IN_USE:
  518. default:
  519. length = sprintf(buff, "not in use\n");
  520. break;
  521. }
  522. batadv_hardif_free_ref(hard_iface);
  523. return length;
  524. }
  525. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  526. batadv_store_mesh_iface);
  527. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  528. static struct batadv_attribute *batadv_batman_attrs[] = {
  529. &batadv_attr_mesh_iface,
  530. &batadv_attr_iface_status,
  531. NULL,
  532. };
  533. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  534. {
  535. struct kobject *hardif_kobject = &dev->dev.kobj;
  536. struct batadv_attribute **bat_attr;
  537. int err;
  538. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  539. hardif_kobject);
  540. if (!*hardif_obj) {
  541. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  542. BATADV_SYSFS_IF_BAT_SUBDIR);
  543. goto out;
  544. }
  545. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  546. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  547. if (err) {
  548. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  549. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  550. ((*bat_attr)->attr).name);
  551. goto rem_attr;
  552. }
  553. }
  554. return 0;
  555. rem_attr:
  556. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  557. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  558. out:
  559. return -ENOMEM;
  560. }
  561. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  562. {
  563. kobject_put(*hardif_obj);
  564. *hardif_obj = NULL;
  565. }
  566. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  567. enum batadv_uev_action action, const char *data)
  568. {
  569. int ret = -ENOMEM;
  570. struct batadv_hard_iface *primary_if = NULL;
  571. struct kobject *bat_kobj;
  572. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  573. primary_if = batadv_primary_if_get_selected(bat_priv);
  574. if (!primary_if)
  575. goto out;
  576. bat_kobj = &primary_if->soft_iface->dev.kobj;
  577. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  578. strlen(batadv_uev_type_str[type]) + 1,
  579. GFP_ATOMIC);
  580. if (!uevent_env[0])
  581. goto out;
  582. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  583. batadv_uev_type_str[type]);
  584. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  585. strlen(batadv_uev_action_str[action]) + 1,
  586. GFP_ATOMIC);
  587. if (!uevent_env[1])
  588. goto out;
  589. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  590. batadv_uev_action_str[action]);
  591. /* If the event is DEL, ignore the data field */
  592. if (action != BATADV_UEV_DEL) {
  593. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  594. strlen(data) + 1, GFP_ATOMIC);
  595. if (!uevent_env[2])
  596. goto out;
  597. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  598. }
  599. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  600. out:
  601. kfree(uevent_env[0]);
  602. kfree(uevent_env[1]);
  603. kfree(uevent_env[2]);
  604. if (primary_if)
  605. batadv_hardif_free_ref(primary_if);
  606. if (ret)
  607. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  608. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  609. batadv_uev_type_str[type],
  610. batadv_uev_action_str[action],
  611. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  612. return ret;
  613. }