bat_sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. /*
  2. * Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "bat_sysfs.h"
  23. #include "translation-table.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. #include "gateway_common.h"
  27. #include "gateway_client.h"
  28. #include "vis.h"
  29. static struct net_device *kobj_to_netdev(struct kobject *obj)
  30. {
  31. struct device *dev = container_of(obj->parent, struct device, kobj);
  32. return to_net_dev(dev);
  33. }
  34. static struct bat_priv *kobj_to_batpriv(struct kobject *obj)
  35. {
  36. struct net_device *net_dev = kobj_to_netdev(obj);
  37. return netdev_priv(net_dev);
  38. }
  39. #define UEV_TYPE_VAR "BATTYPE="
  40. #define UEV_ACTION_VAR "BATACTION="
  41. #define UEV_DATA_VAR "BATDATA="
  42. static char *uev_action_str[] = {
  43. "add",
  44. "del",
  45. "change"
  46. };
  47. static char *uev_type_str[] = {
  48. "gw"
  49. };
  50. /* Use this, if you have customized show and store functions */
  51. #define BAT_ATTR(_name, _mode, _show, _store) \
  52. struct bat_attribute bat_attr_##_name = { \
  53. .attr = {.name = __stringify(_name), \
  54. .mode = _mode }, \
  55. .show = _show, \
  56. .store = _store, \
  57. };
  58. #define BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  59. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  60. char *buff, size_t count) \
  61. { \
  62. struct net_device *net_dev = kobj_to_netdev(kobj); \
  63. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  64. return __store_bool_attr(buff, count, _post_func, attr, \
  65. &bat_priv->_name, net_dev); \
  66. }
  67. #define BAT_ATTR_SIF_SHOW_BOOL(_name) \
  68. ssize_t show_##_name(struct kobject *kobj, \
  69. struct attribute *attr, char *buff) \
  70. { \
  71. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  72. return sprintf(buff, "%s\n", \
  73. atomic_read(&bat_priv->_name) == 0 ? \
  74. "disabled" : "enabled"); \
  75. } \
  76. /* Use this, if you are going to turn a [name] in the soft-interface
  77. * (bat_priv) on or off */
  78. #define BAT_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  79. static BAT_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  80. static BAT_ATTR_SIF_SHOW_BOOL(_name) \
  81. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  82. #define BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  83. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  84. char *buff, size_t count) \
  85. { \
  86. struct net_device *net_dev = kobj_to_netdev(kobj); \
  87. struct bat_priv *bat_priv = netdev_priv(net_dev); \
  88. return __store_uint_attr(buff, count, _min, _max, _post_func, \
  89. attr, &bat_priv->_name, net_dev); \
  90. }
  91. #define BAT_ATTR_SIF_SHOW_UINT(_name) \
  92. ssize_t show_##_name(struct kobject *kobj, \
  93. struct attribute *attr, char *buff) \
  94. { \
  95. struct bat_priv *bat_priv = kobj_to_batpriv(kobj); \
  96. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  97. } \
  98. /* Use this, if you are going to set [name] in the soft-interface
  99. * (bat_priv) to an unsigned integer value */
  100. #define BAT_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
  101. static BAT_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  102. static BAT_ATTR_SIF_SHOW_UINT(_name) \
  103. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  104. #define BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
  105. ssize_t store_##_name(struct kobject *kobj, struct attribute *attr, \
  106. char *buff, size_t count) \
  107. { \
  108. struct net_device *net_dev = kobj_to_netdev(kobj); \
  109. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); \
  110. ssize_t length; \
  111. \
  112. if (!hard_iface) \
  113. return 0; \
  114. \
  115. length = __store_uint_attr(buff, count, _min, _max, _post_func, \
  116. attr, &hard_iface->_name, net_dev); \
  117. \
  118. hardif_free_ref(hard_iface); \
  119. return length; \
  120. }
  121. #define BAT_ATTR_HIF_SHOW_UINT(_name) \
  122. ssize_t show_##_name(struct kobject *kobj, \
  123. struct attribute *attr, char *buff) \
  124. { \
  125. struct net_device *net_dev = kobj_to_netdev(kobj); \
  126. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev); \
  127. ssize_t length; \
  128. \
  129. if (!hard_iface) \
  130. return 0; \
  131. \
  132. length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_name));\
  133. \
  134. hardif_free_ref(hard_iface); \
  135. return length; \
  136. }
  137. /* Use this, if you are going to set [name] in hard_iface to an
  138. * unsigned integer value*/
  139. #define BAT_ATTR_HIF_UINT(_name, _mode, _min, _max, _post_func) \
  140. static BAT_ATTR_HIF_STORE_UINT(_name, _min, _max, _post_func) \
  141. static BAT_ATTR_HIF_SHOW_UINT(_name) \
  142. static BAT_ATTR(_name, _mode, show_##_name, store_##_name)
  143. static int store_bool_attr(char *buff, size_t count,
  144. struct net_device *net_dev,
  145. const char *attr_name, atomic_t *attr)
  146. {
  147. int enabled = -1;
  148. if (buff[count - 1] == '\n')
  149. buff[count - 1] = '\0';
  150. if ((strncmp(buff, "1", 2) == 0) ||
  151. (strncmp(buff, "enable", 7) == 0) ||
  152. (strncmp(buff, "enabled", 8) == 0))
  153. enabled = 1;
  154. if ((strncmp(buff, "0", 2) == 0) ||
  155. (strncmp(buff, "disable", 8) == 0) ||
  156. (strncmp(buff, "disabled", 9) == 0))
  157. enabled = 0;
  158. if (enabled < 0) {
  159. bat_info(net_dev,
  160. "%s: Invalid parameter received: %s\n",
  161. attr_name, buff);
  162. return -EINVAL;
  163. }
  164. if (atomic_read(attr) == enabled)
  165. return count;
  166. bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  167. atomic_read(attr) == 1 ? "enabled" : "disabled",
  168. enabled == 1 ? "enabled" : "disabled");
  169. atomic_set(attr, (unsigned int)enabled);
  170. return count;
  171. }
  172. static inline ssize_t __store_bool_attr(char *buff, size_t count,
  173. void (*post_func)(struct net_device *),
  174. struct attribute *attr,
  175. atomic_t *attr_store, struct net_device *net_dev)
  176. {
  177. int ret;
  178. ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
  179. if (post_func && ret)
  180. post_func(net_dev);
  181. return ret;
  182. }
  183. static int store_uint_attr(const char *buff, size_t count,
  184. struct net_device *net_dev, const char *attr_name,
  185. unsigned int min, unsigned int max, atomic_t *attr)
  186. {
  187. unsigned long uint_val;
  188. int ret;
  189. ret = kstrtoul(buff, 10, &uint_val);
  190. if (ret) {
  191. bat_info(net_dev,
  192. "%s: Invalid parameter received: %s\n",
  193. attr_name, buff);
  194. return -EINVAL;
  195. }
  196. if (uint_val < min) {
  197. bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  198. attr_name, uint_val, min);
  199. return -EINVAL;
  200. }
  201. if (uint_val > max) {
  202. bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  203. attr_name, uint_val, max);
  204. return -EINVAL;
  205. }
  206. if (atomic_read(attr) == uint_val)
  207. return count;
  208. bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
  209. attr_name, atomic_read(attr), uint_val);
  210. atomic_set(attr, uint_val);
  211. return count;
  212. }
  213. static inline ssize_t __store_uint_attr(const char *buff, size_t count,
  214. int min, int max,
  215. void (*post_func)(struct net_device *),
  216. const struct attribute *attr,
  217. atomic_t *attr_store, struct net_device *net_dev)
  218. {
  219. int ret;
  220. ret = store_uint_attr(buff, count, net_dev, attr->name,
  221. min, max, attr_store);
  222. if (post_func && ret)
  223. post_func(net_dev);
  224. return ret;
  225. }
  226. static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
  227. char *buff)
  228. {
  229. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  230. int vis_mode = atomic_read(&bat_priv->vis_mode);
  231. return sprintf(buff, "%s\n",
  232. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  233. "client" : "server");
  234. }
  235. static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
  236. char *buff, size_t count)
  237. {
  238. struct net_device *net_dev = kobj_to_netdev(kobj);
  239. struct bat_priv *bat_priv = netdev_priv(net_dev);
  240. unsigned long val;
  241. int ret, vis_mode_tmp = -1;
  242. ret = kstrtoul(buff, 10, &val);
  243. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  244. (strncmp(buff, "client", 6) == 0) ||
  245. (strncmp(buff, "off", 3) == 0))
  246. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  247. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  248. (strncmp(buff, "server", 6) == 0))
  249. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  250. if (vis_mode_tmp < 0) {
  251. if (buff[count - 1] == '\n')
  252. buff[count - 1] = '\0';
  253. bat_info(net_dev,
  254. "Invalid parameter for 'vis mode' setting received: %s\n",
  255. buff);
  256. return -EINVAL;
  257. }
  258. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  259. return count;
  260. bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
  261. atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
  262. "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
  263. "client" : "server");
  264. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  265. return count;
  266. }
  267. static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
  268. char *buff)
  269. {
  270. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  271. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  272. }
  273. static void post_gw_deselect(struct net_device *net_dev)
  274. {
  275. struct bat_priv *bat_priv = netdev_priv(net_dev);
  276. gw_deselect(bat_priv);
  277. }
  278. static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
  279. char *buff)
  280. {
  281. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  282. int bytes_written;
  283. switch (atomic_read(&bat_priv->gw_mode)) {
  284. case GW_MODE_CLIENT:
  285. bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
  286. break;
  287. case GW_MODE_SERVER:
  288. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  289. break;
  290. default:
  291. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  292. break;
  293. }
  294. return bytes_written;
  295. }
  296. static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
  297. char *buff, size_t count)
  298. {
  299. struct net_device *net_dev = kobj_to_netdev(kobj);
  300. struct bat_priv *bat_priv = netdev_priv(net_dev);
  301. char *curr_gw_mode_str;
  302. int gw_mode_tmp = -1;
  303. if (buff[count - 1] == '\n')
  304. buff[count - 1] = '\0';
  305. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  306. gw_mode_tmp = GW_MODE_OFF;
  307. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  308. strlen(GW_MODE_CLIENT_NAME)) == 0)
  309. gw_mode_tmp = GW_MODE_CLIENT;
  310. if (strncmp(buff, GW_MODE_SERVER_NAME,
  311. strlen(GW_MODE_SERVER_NAME)) == 0)
  312. gw_mode_tmp = GW_MODE_SERVER;
  313. if (gw_mode_tmp < 0) {
  314. bat_info(net_dev,
  315. "Invalid parameter for 'gw mode' setting received: %s\n",
  316. buff);
  317. return -EINVAL;
  318. }
  319. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  320. return count;
  321. switch (atomic_read(&bat_priv->gw_mode)) {
  322. case GW_MODE_CLIENT:
  323. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  324. break;
  325. case GW_MODE_SERVER:
  326. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  327. break;
  328. default:
  329. curr_gw_mode_str = GW_MODE_OFF_NAME;
  330. break;
  331. }
  332. bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
  333. curr_gw_mode_str, buff);
  334. gw_deselect(bat_priv);
  335. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  336. return count;
  337. }
  338. static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  339. char *buff)
  340. {
  341. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  342. int down, up;
  343. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  344. gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  345. return sprintf(buff, "%i%s/%i%s\n",
  346. (down > 2048 ? down / 1024 : down),
  347. (down > 2048 ? "MBit" : "KBit"),
  348. (up > 2048 ? up / 1024 : up),
  349. (up > 2048 ? "MBit" : "KBit"));
  350. }
  351. static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  352. char *buff, size_t count)
  353. {
  354. struct net_device *net_dev = kobj_to_netdev(kobj);
  355. if (buff[count - 1] == '\n')
  356. buff[count - 1] = '\0';
  357. return gw_bandwidth_set(net_dev, buff, count);
  358. }
  359. BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  360. BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  361. #ifdef CONFIG_BATMAN_ADV_BLA
  362. BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  363. #endif
  364. BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
  365. BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  366. static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
  367. static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
  368. static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
  369. BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
  370. BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  371. BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  372. post_gw_deselect);
  373. static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
  374. store_gw_bwidth);
  375. #ifdef CONFIG_BATMAN_ADV_DEBUG
  376. BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, 15, NULL);
  377. #endif
  378. static struct bat_attribute *mesh_attrs[] = {
  379. &bat_attr_aggregated_ogms,
  380. &bat_attr_bonding,
  381. #ifdef CONFIG_BATMAN_ADV_BLA
  382. &bat_attr_bridge_loop_avoidance,
  383. #endif
  384. &bat_attr_fragmentation,
  385. &bat_attr_ap_isolation,
  386. &bat_attr_vis_mode,
  387. &bat_attr_routing_algo,
  388. &bat_attr_gw_mode,
  389. &bat_attr_orig_interval,
  390. &bat_attr_hop_penalty,
  391. &bat_attr_gw_sel_class,
  392. &bat_attr_gw_bandwidth,
  393. #ifdef CONFIG_BATMAN_ADV_DEBUG
  394. &bat_attr_log_level,
  395. #endif
  396. NULL,
  397. };
  398. int sysfs_add_meshif(struct net_device *dev)
  399. {
  400. struct kobject *batif_kobject = &dev->dev.kobj;
  401. struct bat_priv *bat_priv = netdev_priv(dev);
  402. struct bat_attribute **bat_attr;
  403. int err;
  404. bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
  405. batif_kobject);
  406. if (!bat_priv->mesh_obj) {
  407. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  408. SYSFS_IF_MESH_SUBDIR);
  409. goto out;
  410. }
  411. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
  412. err = sysfs_create_file(bat_priv->mesh_obj,
  413. &((*bat_attr)->attr));
  414. if (err) {
  415. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  416. dev->name, SYSFS_IF_MESH_SUBDIR,
  417. ((*bat_attr)->attr).name);
  418. goto rem_attr;
  419. }
  420. }
  421. return 0;
  422. rem_attr:
  423. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  424. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  425. kobject_put(bat_priv->mesh_obj);
  426. bat_priv->mesh_obj = NULL;
  427. out:
  428. return -ENOMEM;
  429. }
  430. void sysfs_del_meshif(struct net_device *dev)
  431. {
  432. struct bat_priv *bat_priv = netdev_priv(dev);
  433. struct bat_attribute **bat_attr;
  434. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  435. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  436. kobject_put(bat_priv->mesh_obj);
  437. bat_priv->mesh_obj = NULL;
  438. }
  439. static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
  440. char *buff)
  441. {
  442. struct net_device *net_dev = kobj_to_netdev(kobj);
  443. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  444. ssize_t length;
  445. if (!hard_iface)
  446. return 0;
  447. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  448. "none" : hard_iface->soft_iface->name);
  449. hardif_free_ref(hard_iface);
  450. return length;
  451. }
  452. static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
  453. char *buff, size_t count)
  454. {
  455. struct net_device *net_dev = kobj_to_netdev(kobj);
  456. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  457. int status_tmp = -1;
  458. int ret = count;
  459. if (!hard_iface)
  460. return count;
  461. if (buff[count - 1] == '\n')
  462. buff[count - 1] = '\0';
  463. if (strlen(buff) >= IFNAMSIZ) {
  464. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  465. buff);
  466. hardif_free_ref(hard_iface);
  467. return -EINVAL;
  468. }
  469. if (strncmp(buff, "none", 4) == 0)
  470. status_tmp = IF_NOT_IN_USE;
  471. else
  472. status_tmp = IF_I_WANT_YOU;
  473. if (hard_iface->if_status == status_tmp)
  474. goto out;
  475. if ((hard_iface->soft_iface) &&
  476. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  477. goto out;
  478. if (!rtnl_trylock()) {
  479. ret = -ERESTARTSYS;
  480. goto out;
  481. }
  482. if (status_tmp == IF_NOT_IN_USE) {
  483. hardif_disable_interface(hard_iface);
  484. goto unlock;
  485. }
  486. /* if the interface already is in use */
  487. if (hard_iface->if_status != IF_NOT_IN_USE)
  488. hardif_disable_interface(hard_iface);
  489. ret = hardif_enable_interface(hard_iface, buff);
  490. unlock:
  491. rtnl_unlock();
  492. out:
  493. hardif_free_ref(hard_iface);
  494. return ret;
  495. }
  496. static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
  497. char *buff)
  498. {
  499. struct net_device *net_dev = kobj_to_netdev(kobj);
  500. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  501. ssize_t length;
  502. if (!hard_iface)
  503. return 0;
  504. switch (hard_iface->if_status) {
  505. case IF_TO_BE_REMOVED:
  506. length = sprintf(buff, "disabling\n");
  507. break;
  508. case IF_INACTIVE:
  509. length = sprintf(buff, "inactive\n");
  510. break;
  511. case IF_ACTIVE:
  512. length = sprintf(buff, "active\n");
  513. break;
  514. case IF_TO_BE_ACTIVATED:
  515. length = sprintf(buff, "enabling\n");
  516. break;
  517. case IF_NOT_IN_USE:
  518. default:
  519. length = sprintf(buff, "not in use\n");
  520. break;
  521. }
  522. hardif_free_ref(hard_iface);
  523. return length;
  524. }
  525. static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
  526. show_mesh_iface, store_mesh_iface);
  527. static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
  528. static struct bat_attribute *batman_attrs[] = {
  529. &bat_attr_mesh_iface,
  530. &bat_attr_iface_status,
  531. NULL,
  532. };
  533. int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  534. {
  535. struct kobject *hardif_kobject = &dev->dev.kobj;
  536. struct bat_attribute **bat_attr;
  537. int err;
  538. *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
  539. hardif_kobject);
  540. if (!*hardif_obj) {
  541. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  542. SYSFS_IF_BAT_SUBDIR);
  543. goto out;
  544. }
  545. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
  546. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  547. if (err) {
  548. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  549. dev->name, 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 = batman_attrs; *bat_attr; ++bat_attr)
  557. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  558. out:
  559. return -ENOMEM;
  560. }
  561. void sysfs_del_hardif(struct kobject **hardif_obj)
  562. {
  563. kobject_put(*hardif_obj);
  564. *hardif_obj = NULL;
  565. }
  566. int throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  567. enum uev_action action, const char *data)
  568. {
  569. int ret = -1;
  570. struct hard_iface *primary_if = NULL;
  571. struct kobject *bat_kobj;
  572. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  573. primary_if = 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(UEV_TYPE_VAR) +
  578. strlen(uev_type_str[type]) + 1,
  579. GFP_ATOMIC);
  580. if (!uevent_env[0])
  581. goto out;
  582. sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
  583. uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
  584. strlen(uev_action_str[action]) + 1,
  585. GFP_ATOMIC);
  586. if (!uevent_env[1])
  587. goto out;
  588. sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
  589. /* If the event is DEL, ignore the data field */
  590. if (action != UEV_DEL) {
  591. uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
  592. strlen(data) + 1, GFP_ATOMIC);
  593. if (!uevent_env[2])
  594. goto out;
  595. sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
  596. }
  597. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  598. out:
  599. kfree(uevent_env[0]);
  600. kfree(uevent_env[1]);
  601. kfree(uevent_env[2]);
  602. if (primary_if)
  603. hardif_free_ref(primary_if);
  604. if (ret)
  605. bat_dbg(DBG_BATMAN, bat_priv,
  606. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  607. uev_type_str[type], uev_action_str[action],
  608. (action == UEV_DEL ? "NULL" : data), ret);
  609. return ret;
  610. }