bat_sysfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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. static int store_bool_attr(char *buff, size_t count,
  105. struct net_device *net_dev,
  106. const char *attr_name, atomic_t *attr)
  107. {
  108. int enabled = -1;
  109. if (buff[count - 1] == '\n')
  110. buff[count - 1] = '\0';
  111. if ((strncmp(buff, "1", 2) == 0) ||
  112. (strncmp(buff, "enable", 7) == 0) ||
  113. (strncmp(buff, "enabled", 8) == 0))
  114. enabled = 1;
  115. if ((strncmp(buff, "0", 2) == 0) ||
  116. (strncmp(buff, "disable", 8) == 0) ||
  117. (strncmp(buff, "disabled", 9) == 0))
  118. enabled = 0;
  119. if (enabled < 0) {
  120. bat_info(net_dev,
  121. "%s: Invalid parameter received: %s\n",
  122. attr_name, buff);
  123. return -EINVAL;
  124. }
  125. if (atomic_read(attr) == enabled)
  126. return count;
  127. bat_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  128. atomic_read(attr) == 1 ? "enabled" : "disabled",
  129. enabled == 1 ? "enabled" : "disabled");
  130. atomic_set(attr, (unsigned int)enabled);
  131. return count;
  132. }
  133. static inline ssize_t __store_bool_attr(char *buff, size_t count,
  134. void (*post_func)(struct net_device *),
  135. struct attribute *attr,
  136. atomic_t *attr_store, struct net_device *net_dev)
  137. {
  138. int ret;
  139. ret = store_bool_attr(buff, count, net_dev, attr->name, attr_store);
  140. if (post_func && ret)
  141. post_func(net_dev);
  142. return ret;
  143. }
  144. static int store_uint_attr(const char *buff, size_t count,
  145. struct net_device *net_dev, const char *attr_name,
  146. unsigned int min, unsigned int max, atomic_t *attr)
  147. {
  148. unsigned long uint_val;
  149. int ret;
  150. ret = kstrtoul(buff, 10, &uint_val);
  151. if (ret) {
  152. bat_info(net_dev,
  153. "%s: Invalid parameter received: %s\n",
  154. attr_name, buff);
  155. return -EINVAL;
  156. }
  157. if (uint_val < min) {
  158. bat_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  159. attr_name, uint_val, min);
  160. return -EINVAL;
  161. }
  162. if (uint_val > max) {
  163. bat_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  164. attr_name, uint_val, max);
  165. return -EINVAL;
  166. }
  167. if (atomic_read(attr) == uint_val)
  168. return count;
  169. bat_info(net_dev, "%s: Changing from: %i to: %lu\n",
  170. attr_name, atomic_read(attr), uint_val);
  171. atomic_set(attr, uint_val);
  172. return count;
  173. }
  174. static inline ssize_t __store_uint_attr(const char *buff, size_t count,
  175. int min, int max,
  176. void (*post_func)(struct net_device *),
  177. const struct attribute *attr,
  178. atomic_t *attr_store, struct net_device *net_dev)
  179. {
  180. int ret;
  181. ret = store_uint_attr(buff, count, net_dev, attr->name,
  182. min, max, attr_store);
  183. if (post_func && ret)
  184. post_func(net_dev);
  185. return ret;
  186. }
  187. static ssize_t show_vis_mode(struct kobject *kobj, struct attribute *attr,
  188. char *buff)
  189. {
  190. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  191. int vis_mode = atomic_read(&bat_priv->vis_mode);
  192. return sprintf(buff, "%s\n",
  193. vis_mode == VIS_TYPE_CLIENT_UPDATE ?
  194. "client" : "server");
  195. }
  196. static ssize_t store_vis_mode(struct kobject *kobj, struct attribute *attr,
  197. char *buff, size_t count)
  198. {
  199. struct net_device *net_dev = kobj_to_netdev(kobj);
  200. struct bat_priv *bat_priv = netdev_priv(net_dev);
  201. unsigned long val;
  202. int ret, vis_mode_tmp = -1;
  203. ret = kstrtoul(buff, 10, &val);
  204. if (((count == 2) && (!ret) && (val == VIS_TYPE_CLIENT_UPDATE)) ||
  205. (strncmp(buff, "client", 6) == 0) ||
  206. (strncmp(buff, "off", 3) == 0))
  207. vis_mode_tmp = VIS_TYPE_CLIENT_UPDATE;
  208. if (((count == 2) && (!ret) && (val == VIS_TYPE_SERVER_SYNC)) ||
  209. (strncmp(buff, "server", 6) == 0))
  210. vis_mode_tmp = VIS_TYPE_SERVER_SYNC;
  211. if (vis_mode_tmp < 0) {
  212. if (buff[count - 1] == '\n')
  213. buff[count - 1] = '\0';
  214. bat_info(net_dev,
  215. "Invalid parameter for 'vis mode' setting received: %s\n",
  216. buff);
  217. return -EINVAL;
  218. }
  219. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  220. return count;
  221. bat_info(net_dev, "Changing vis mode from: %s to: %s\n",
  222. atomic_read(&bat_priv->vis_mode) == VIS_TYPE_CLIENT_UPDATE ?
  223. "client" : "server", vis_mode_tmp == VIS_TYPE_CLIENT_UPDATE ?
  224. "client" : "server");
  225. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  226. return count;
  227. }
  228. static ssize_t show_bat_algo(struct kobject *kobj, struct attribute *attr,
  229. char *buff)
  230. {
  231. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  232. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  233. }
  234. static void post_gw_deselect(struct net_device *net_dev)
  235. {
  236. struct bat_priv *bat_priv = netdev_priv(net_dev);
  237. gw_deselect(bat_priv);
  238. }
  239. static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr,
  240. char *buff)
  241. {
  242. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  243. int bytes_written;
  244. switch (atomic_read(&bat_priv->gw_mode)) {
  245. case GW_MODE_CLIENT:
  246. bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME);
  247. break;
  248. case GW_MODE_SERVER:
  249. bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME);
  250. break;
  251. default:
  252. bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME);
  253. break;
  254. }
  255. return bytes_written;
  256. }
  257. static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr,
  258. char *buff, size_t count)
  259. {
  260. struct net_device *net_dev = kobj_to_netdev(kobj);
  261. struct bat_priv *bat_priv = netdev_priv(net_dev);
  262. char *curr_gw_mode_str;
  263. int gw_mode_tmp = -1;
  264. if (buff[count - 1] == '\n')
  265. buff[count - 1] = '\0';
  266. if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0)
  267. gw_mode_tmp = GW_MODE_OFF;
  268. if (strncmp(buff, GW_MODE_CLIENT_NAME,
  269. strlen(GW_MODE_CLIENT_NAME)) == 0)
  270. gw_mode_tmp = GW_MODE_CLIENT;
  271. if (strncmp(buff, GW_MODE_SERVER_NAME,
  272. strlen(GW_MODE_SERVER_NAME)) == 0)
  273. gw_mode_tmp = GW_MODE_SERVER;
  274. if (gw_mode_tmp < 0) {
  275. bat_info(net_dev,
  276. "Invalid parameter for 'gw mode' setting received: %s\n",
  277. buff);
  278. return -EINVAL;
  279. }
  280. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  281. return count;
  282. switch (atomic_read(&bat_priv->gw_mode)) {
  283. case GW_MODE_CLIENT:
  284. curr_gw_mode_str = GW_MODE_CLIENT_NAME;
  285. break;
  286. case GW_MODE_SERVER:
  287. curr_gw_mode_str = GW_MODE_SERVER_NAME;
  288. break;
  289. default:
  290. curr_gw_mode_str = GW_MODE_OFF_NAME;
  291. break;
  292. }
  293. bat_info(net_dev, "Changing gw mode from: %s to: %s\n",
  294. curr_gw_mode_str, buff);
  295. gw_deselect(bat_priv);
  296. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  297. return count;
  298. }
  299. static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  300. char *buff)
  301. {
  302. struct bat_priv *bat_priv = kobj_to_batpriv(kobj);
  303. int down, up;
  304. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  305. gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  306. return sprintf(buff, "%i%s/%i%s\n",
  307. (down > 2048 ? down / 1024 : down),
  308. (down > 2048 ? "MBit" : "KBit"),
  309. (up > 2048 ? up / 1024 : up),
  310. (up > 2048 ? "MBit" : "KBit"));
  311. }
  312. static ssize_t store_gw_bwidth(struct kobject *kobj, struct attribute *attr,
  313. char *buff, size_t count)
  314. {
  315. struct net_device *net_dev = kobj_to_netdev(kobj);
  316. if (buff[count - 1] == '\n')
  317. buff[count - 1] = '\0';
  318. return gw_bandwidth_set(net_dev, buff, count);
  319. }
  320. BAT_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  321. BAT_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  322. #ifdef CONFIG_BATMAN_ADV_BLA
  323. BAT_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  324. #endif
  325. BAT_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, update_min_mtu);
  326. BAT_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  327. static BAT_ATTR(vis_mode, S_IRUGO | S_IWUSR, show_vis_mode, store_vis_mode);
  328. static BAT_ATTR(routing_algo, S_IRUGO, show_bat_algo, NULL);
  329. static BAT_ATTR(gw_mode, S_IRUGO | S_IWUSR, show_gw_mode, store_gw_mode);
  330. BAT_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * JITTER, INT_MAX, NULL);
  331. BAT_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, TQ_MAX_VALUE, NULL);
  332. BAT_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, TQ_MAX_VALUE,
  333. post_gw_deselect);
  334. static BAT_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, show_gw_bwidth,
  335. store_gw_bwidth);
  336. #ifdef CONFIG_BATMAN_ADV_DEBUG
  337. BAT_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, 15, NULL);
  338. #endif
  339. static struct bat_attribute *mesh_attrs[] = {
  340. &bat_attr_aggregated_ogms,
  341. &bat_attr_bonding,
  342. #ifdef CONFIG_BATMAN_ADV_BLA
  343. &bat_attr_bridge_loop_avoidance,
  344. #endif
  345. &bat_attr_fragmentation,
  346. &bat_attr_ap_isolation,
  347. &bat_attr_vis_mode,
  348. &bat_attr_routing_algo,
  349. &bat_attr_gw_mode,
  350. &bat_attr_orig_interval,
  351. &bat_attr_hop_penalty,
  352. &bat_attr_gw_sel_class,
  353. &bat_attr_gw_bandwidth,
  354. #ifdef CONFIG_BATMAN_ADV_DEBUG
  355. &bat_attr_log_level,
  356. #endif
  357. NULL,
  358. };
  359. int sysfs_add_meshif(struct net_device *dev)
  360. {
  361. struct kobject *batif_kobject = &dev->dev.kobj;
  362. struct bat_priv *bat_priv = netdev_priv(dev);
  363. struct bat_attribute **bat_attr;
  364. int err;
  365. bat_priv->mesh_obj = kobject_create_and_add(SYSFS_IF_MESH_SUBDIR,
  366. batif_kobject);
  367. if (!bat_priv->mesh_obj) {
  368. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  369. SYSFS_IF_MESH_SUBDIR);
  370. goto out;
  371. }
  372. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr) {
  373. err = sysfs_create_file(bat_priv->mesh_obj,
  374. &((*bat_attr)->attr));
  375. if (err) {
  376. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  377. dev->name, SYSFS_IF_MESH_SUBDIR,
  378. ((*bat_attr)->attr).name);
  379. goto rem_attr;
  380. }
  381. }
  382. return 0;
  383. rem_attr:
  384. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  385. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  386. kobject_put(bat_priv->mesh_obj);
  387. bat_priv->mesh_obj = NULL;
  388. out:
  389. return -ENOMEM;
  390. }
  391. void sysfs_del_meshif(struct net_device *dev)
  392. {
  393. struct bat_priv *bat_priv = netdev_priv(dev);
  394. struct bat_attribute **bat_attr;
  395. for (bat_attr = mesh_attrs; *bat_attr; ++bat_attr)
  396. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  397. kobject_put(bat_priv->mesh_obj);
  398. bat_priv->mesh_obj = NULL;
  399. }
  400. static ssize_t show_mesh_iface(struct kobject *kobj, struct attribute *attr,
  401. char *buff)
  402. {
  403. struct net_device *net_dev = kobj_to_netdev(kobj);
  404. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  405. ssize_t length;
  406. if (!hard_iface)
  407. return 0;
  408. length = sprintf(buff, "%s\n", hard_iface->if_status == IF_NOT_IN_USE ?
  409. "none" : hard_iface->soft_iface->name);
  410. hardif_free_ref(hard_iface);
  411. return length;
  412. }
  413. static ssize_t store_mesh_iface(struct kobject *kobj, struct attribute *attr,
  414. char *buff, size_t count)
  415. {
  416. struct net_device *net_dev = kobj_to_netdev(kobj);
  417. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  418. int status_tmp = -1;
  419. int ret = count;
  420. if (!hard_iface)
  421. return count;
  422. if (buff[count - 1] == '\n')
  423. buff[count - 1] = '\0';
  424. if (strlen(buff) >= IFNAMSIZ) {
  425. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  426. buff);
  427. hardif_free_ref(hard_iface);
  428. return -EINVAL;
  429. }
  430. if (strncmp(buff, "none", 4) == 0)
  431. status_tmp = IF_NOT_IN_USE;
  432. else
  433. status_tmp = IF_I_WANT_YOU;
  434. if (hard_iface->if_status == status_tmp)
  435. goto out;
  436. if ((hard_iface->soft_iface) &&
  437. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  438. goto out;
  439. if (!rtnl_trylock()) {
  440. ret = -ERESTARTSYS;
  441. goto out;
  442. }
  443. if (status_tmp == IF_NOT_IN_USE) {
  444. hardif_disable_interface(hard_iface);
  445. goto unlock;
  446. }
  447. /* if the interface already is in use */
  448. if (hard_iface->if_status != IF_NOT_IN_USE)
  449. hardif_disable_interface(hard_iface);
  450. ret = hardif_enable_interface(hard_iface, buff);
  451. unlock:
  452. rtnl_unlock();
  453. out:
  454. hardif_free_ref(hard_iface);
  455. return ret;
  456. }
  457. static ssize_t show_iface_status(struct kobject *kobj, struct attribute *attr,
  458. char *buff)
  459. {
  460. struct net_device *net_dev = kobj_to_netdev(kobj);
  461. struct hard_iface *hard_iface = hardif_get_by_netdev(net_dev);
  462. ssize_t length;
  463. if (!hard_iface)
  464. return 0;
  465. switch (hard_iface->if_status) {
  466. case IF_TO_BE_REMOVED:
  467. length = sprintf(buff, "disabling\n");
  468. break;
  469. case IF_INACTIVE:
  470. length = sprintf(buff, "inactive\n");
  471. break;
  472. case IF_ACTIVE:
  473. length = sprintf(buff, "active\n");
  474. break;
  475. case IF_TO_BE_ACTIVATED:
  476. length = sprintf(buff, "enabling\n");
  477. break;
  478. case IF_NOT_IN_USE:
  479. default:
  480. length = sprintf(buff, "not in use\n");
  481. break;
  482. }
  483. hardif_free_ref(hard_iface);
  484. return length;
  485. }
  486. static BAT_ATTR(mesh_iface, S_IRUGO | S_IWUSR,
  487. show_mesh_iface, store_mesh_iface);
  488. static BAT_ATTR(iface_status, S_IRUGO, show_iface_status, NULL);
  489. static struct bat_attribute *batman_attrs[] = {
  490. &bat_attr_mesh_iface,
  491. &bat_attr_iface_status,
  492. NULL,
  493. };
  494. int sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  495. {
  496. struct kobject *hardif_kobject = &dev->dev.kobj;
  497. struct bat_attribute **bat_attr;
  498. int err;
  499. *hardif_obj = kobject_create_and_add(SYSFS_IF_BAT_SUBDIR,
  500. hardif_kobject);
  501. if (!*hardif_obj) {
  502. bat_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  503. SYSFS_IF_BAT_SUBDIR);
  504. goto out;
  505. }
  506. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr) {
  507. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  508. if (err) {
  509. bat_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  510. dev->name, SYSFS_IF_BAT_SUBDIR,
  511. ((*bat_attr)->attr).name);
  512. goto rem_attr;
  513. }
  514. }
  515. return 0;
  516. rem_attr:
  517. for (bat_attr = batman_attrs; *bat_attr; ++bat_attr)
  518. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  519. out:
  520. return -ENOMEM;
  521. }
  522. void sysfs_del_hardif(struct kobject **hardif_obj)
  523. {
  524. kobject_put(*hardif_obj);
  525. *hardif_obj = NULL;
  526. }
  527. int throw_uevent(struct bat_priv *bat_priv, enum uev_type type,
  528. enum uev_action action, const char *data)
  529. {
  530. int ret = -1;
  531. struct hard_iface *primary_if = NULL;
  532. struct kobject *bat_kobj;
  533. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  534. primary_if = primary_if_get_selected(bat_priv);
  535. if (!primary_if)
  536. goto out;
  537. bat_kobj = &primary_if->soft_iface->dev.kobj;
  538. uevent_env[0] = kmalloc(strlen(UEV_TYPE_VAR) +
  539. strlen(uev_type_str[type]) + 1,
  540. GFP_ATOMIC);
  541. if (!uevent_env[0])
  542. goto out;
  543. sprintf(uevent_env[0], "%s%s", UEV_TYPE_VAR, uev_type_str[type]);
  544. uevent_env[1] = kmalloc(strlen(UEV_ACTION_VAR) +
  545. strlen(uev_action_str[action]) + 1,
  546. GFP_ATOMIC);
  547. if (!uevent_env[1])
  548. goto out;
  549. sprintf(uevent_env[1], "%s%s", UEV_ACTION_VAR, uev_action_str[action]);
  550. /* If the event is DEL, ignore the data field */
  551. if (action != UEV_DEL) {
  552. uevent_env[2] = kmalloc(strlen(UEV_DATA_VAR) +
  553. strlen(data) + 1, GFP_ATOMIC);
  554. if (!uevent_env[2])
  555. goto out;
  556. sprintf(uevent_env[2], "%s%s", UEV_DATA_VAR, data);
  557. }
  558. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  559. out:
  560. kfree(uevent_env[0]);
  561. kfree(uevent_env[1]);
  562. kfree(uevent_env[2]);
  563. if (primary_if)
  564. hardif_free_ref(primary_if);
  565. if (ret)
  566. bat_dbg(DBG_BATMAN, bat_priv,
  567. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  568. uev_type_str[type], uev_action_str[action],
  569. (action == UEV_DEL ? "NULL" : data), ret);
  570. return ret;
  571. }