bat_sysfs.c 17 KB

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