sysfs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /* Copyright (C) 2010-2013 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 "distributed-arp-table.h"
  23. #include "originator.h"
  24. #include "hard-interface.h"
  25. #include "gateway_common.h"
  26. #include "gateway_client.h"
  27. #include "vis.h"
  28. static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
  29. {
  30. struct device *dev = container_of(obj->parent, struct device, kobj);
  31. return to_net_dev(dev);
  32. }
  33. static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
  34. {
  35. struct net_device *net_dev = batadv_kobj_to_netdev(obj);
  36. return netdev_priv(net_dev);
  37. }
  38. #define BATADV_UEV_TYPE_VAR "BATTYPE="
  39. #define BATADV_UEV_ACTION_VAR "BATACTION="
  40. #define BATADV_UEV_DATA_VAR "BATDATA="
  41. static char *batadv_uev_action_str[] = {
  42. "add",
  43. "del",
  44. "change"
  45. };
  46. static char *batadv_uev_type_str[] = {
  47. "gw"
  48. };
  49. /* Use this, if you have customized show and store functions */
  50. #define BATADV_ATTR(_name, _mode, _show, _store) \
  51. struct batadv_attribute batadv_attr_##_name = { \
  52. .attr = {.name = __stringify(_name), \
  53. .mode = _mode }, \
  54. .show = _show, \
  55. .store = _store, \
  56. };
  57. #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  58. ssize_t batadv_store_##_name(struct kobject *kobj, \
  59. struct attribute *attr, char *buff, \
  60. size_t count) \
  61. { \
  62. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  63. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  64. return __batadv_store_bool_attr(buff, count, _post_func, attr, \
  65. &bat_priv->_name, net_dev); \
  66. }
  67. #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  68. ssize_t batadv_show_##_name(struct kobject *kobj, \
  69. struct attribute *attr, char *buff) \
  70. { \
  71. struct batadv_priv *bat_priv = batadv_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. */
  79. #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  80. static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  81. static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  82. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  83. batadv_store_##_name)
  84. #define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  85. ssize_t batadv_store_##_name(struct kobject *kobj, \
  86. struct attribute *attr, char *buff, \
  87. size_t count) \
  88. { \
  89. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  90. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  91. return __batadv_store_uint_attr(buff, count, _min, _max, \
  92. _post_func, attr, \
  93. &bat_priv->_name, net_dev); \
  94. }
  95. #define BATADV_ATTR_SIF_SHOW_UINT(_name) \
  96. ssize_t batadv_show_##_name(struct kobject *kobj, \
  97. struct attribute *attr, char *buff) \
  98. { \
  99. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  100. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  101. } \
  102. /* Use this, if you are going to set [name] in the soft-interface
  103. * (bat_priv) to an unsigned integer value
  104. */
  105. #define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
  106. static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
  107. static BATADV_ATTR_SIF_SHOW_UINT(_name) \
  108. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  109. batadv_store_##_name)
  110. static int batadv_store_bool_attr(char *buff, size_t count,
  111. struct net_device *net_dev,
  112. const char *attr_name, atomic_t *attr)
  113. {
  114. int enabled = -1;
  115. if (buff[count - 1] == '\n')
  116. buff[count - 1] = '\0';
  117. if ((strncmp(buff, "1", 2) == 0) ||
  118. (strncmp(buff, "enable", 7) == 0) ||
  119. (strncmp(buff, "enabled", 8) == 0))
  120. enabled = 1;
  121. if ((strncmp(buff, "0", 2) == 0) ||
  122. (strncmp(buff, "disable", 8) == 0) ||
  123. (strncmp(buff, "disabled", 9) == 0))
  124. enabled = 0;
  125. if (enabled < 0) {
  126. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  127. attr_name, buff);
  128. return -EINVAL;
  129. }
  130. if (atomic_read(attr) == enabled)
  131. return count;
  132. batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  133. atomic_read(attr) == 1 ? "enabled" : "disabled",
  134. enabled == 1 ? "enabled" : "disabled");
  135. atomic_set(attr, (unsigned int)enabled);
  136. return count;
  137. }
  138. static inline ssize_t
  139. __batadv_store_bool_attr(char *buff, size_t count,
  140. void (*post_func)(struct net_device *),
  141. struct attribute *attr,
  142. atomic_t *attr_store, struct net_device *net_dev)
  143. {
  144. int ret;
  145. ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
  146. attr_store);
  147. if (post_func && ret)
  148. post_func(net_dev);
  149. return ret;
  150. }
  151. static int batadv_store_uint_attr(const char *buff, size_t count,
  152. struct net_device *net_dev,
  153. const char *attr_name,
  154. unsigned int min, unsigned int max,
  155. atomic_t *attr)
  156. {
  157. unsigned long uint_val;
  158. int ret;
  159. ret = kstrtoul(buff, 10, &uint_val);
  160. if (ret) {
  161. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  162. attr_name, buff);
  163. return -EINVAL;
  164. }
  165. if (uint_val < min) {
  166. batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  167. attr_name, uint_val, min);
  168. return -EINVAL;
  169. }
  170. if (uint_val > max) {
  171. batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  172. attr_name, uint_val, max);
  173. return -EINVAL;
  174. }
  175. if (atomic_read(attr) == uint_val)
  176. return count;
  177. batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
  178. attr_name, atomic_read(attr), uint_val);
  179. atomic_set(attr, uint_val);
  180. return count;
  181. }
  182. static inline ssize_t
  183. __batadv_store_uint_attr(const char *buff, size_t count,
  184. int min, int max,
  185. void (*post_func)(struct net_device *),
  186. const struct attribute *attr,
  187. atomic_t *attr_store, struct net_device *net_dev)
  188. {
  189. int ret;
  190. ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
  191. attr_store);
  192. if (post_func && ret)
  193. post_func(net_dev);
  194. return ret;
  195. }
  196. static ssize_t batadv_show_vis_mode(struct kobject *kobj,
  197. struct attribute *attr, char *buff)
  198. {
  199. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  200. int vis_mode = atomic_read(&bat_priv->vis_mode);
  201. const char *mode;
  202. if (vis_mode == BATADV_VIS_TYPE_CLIENT_UPDATE)
  203. mode = "client";
  204. else
  205. mode = "server";
  206. return sprintf(buff, "%s\n", mode);
  207. }
  208. static ssize_t batadv_store_vis_mode(struct kobject *kobj,
  209. struct attribute *attr, char *buff,
  210. size_t count)
  211. {
  212. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  213. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  214. unsigned long val;
  215. int ret, vis_mode_tmp = -1;
  216. const char *old_mode, *new_mode;
  217. ret = kstrtoul(buff, 10, &val);
  218. if (((count == 2) && (!ret) &&
  219. (val == BATADV_VIS_TYPE_CLIENT_UPDATE)) ||
  220. (strncmp(buff, "client", 6) == 0) ||
  221. (strncmp(buff, "off", 3) == 0))
  222. vis_mode_tmp = BATADV_VIS_TYPE_CLIENT_UPDATE;
  223. if (((count == 2) && (!ret) &&
  224. (val == BATADV_VIS_TYPE_SERVER_SYNC)) ||
  225. (strncmp(buff, "server", 6) == 0))
  226. vis_mode_tmp = BATADV_VIS_TYPE_SERVER_SYNC;
  227. if (vis_mode_tmp < 0) {
  228. if (buff[count - 1] == '\n')
  229. buff[count - 1] = '\0';
  230. batadv_info(net_dev,
  231. "Invalid parameter for 'vis mode' setting received: %s\n",
  232. buff);
  233. return -EINVAL;
  234. }
  235. if (atomic_read(&bat_priv->vis_mode) == vis_mode_tmp)
  236. return count;
  237. if (atomic_read(&bat_priv->vis_mode) == BATADV_VIS_TYPE_CLIENT_UPDATE)
  238. old_mode = "client";
  239. else
  240. old_mode = "server";
  241. if (vis_mode_tmp == BATADV_VIS_TYPE_CLIENT_UPDATE)
  242. new_mode = "client";
  243. else
  244. new_mode = "server";
  245. batadv_info(net_dev, "Changing vis mode from: %s to: %s\n", old_mode,
  246. new_mode);
  247. atomic_set(&bat_priv->vis_mode, (unsigned int)vis_mode_tmp);
  248. return count;
  249. }
  250. static ssize_t batadv_show_bat_algo(struct kobject *kobj,
  251. struct attribute *attr, char *buff)
  252. {
  253. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  254. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  255. }
  256. static void batadv_post_gw_deselect(struct net_device *net_dev)
  257. {
  258. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  259. batadv_gw_deselect(bat_priv);
  260. }
  261. static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
  262. char *buff)
  263. {
  264. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  265. int bytes_written;
  266. switch (atomic_read(&bat_priv->gw_mode)) {
  267. case BATADV_GW_MODE_CLIENT:
  268. bytes_written = sprintf(buff, "%s\n",
  269. BATADV_GW_MODE_CLIENT_NAME);
  270. break;
  271. case BATADV_GW_MODE_SERVER:
  272. bytes_written = sprintf(buff, "%s\n",
  273. BATADV_GW_MODE_SERVER_NAME);
  274. break;
  275. default:
  276. bytes_written = sprintf(buff, "%s\n",
  277. BATADV_GW_MODE_OFF_NAME);
  278. break;
  279. }
  280. return bytes_written;
  281. }
  282. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  283. struct attribute *attr, char *buff,
  284. size_t count)
  285. {
  286. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  287. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  288. char *curr_gw_mode_str;
  289. int gw_mode_tmp = -1;
  290. if (buff[count - 1] == '\n')
  291. buff[count - 1] = '\0';
  292. if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
  293. strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
  294. gw_mode_tmp = BATADV_GW_MODE_OFF;
  295. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  296. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  297. gw_mode_tmp = BATADV_GW_MODE_CLIENT;
  298. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  299. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  300. gw_mode_tmp = BATADV_GW_MODE_SERVER;
  301. if (gw_mode_tmp < 0) {
  302. batadv_info(net_dev,
  303. "Invalid parameter for 'gw mode' setting received: %s\n",
  304. buff);
  305. return -EINVAL;
  306. }
  307. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  308. return count;
  309. switch (atomic_read(&bat_priv->gw_mode)) {
  310. case BATADV_GW_MODE_CLIENT:
  311. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  312. break;
  313. case BATADV_GW_MODE_SERVER:
  314. curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
  315. break;
  316. default:
  317. curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
  318. break;
  319. }
  320. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  321. curr_gw_mode_str, buff);
  322. batadv_gw_deselect(bat_priv);
  323. /* always call batadv_gw_check_client_stop() before changing the gateway
  324. * state
  325. */
  326. batadv_gw_check_client_stop(bat_priv);
  327. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  328. batadv_gw_tvlv_container_update(bat_priv);
  329. return count;
  330. }
  331. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  332. struct attribute *attr, char *buff)
  333. {
  334. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  335. uint32_t down, up;
  336. down = atomic_read(&bat_priv->gw.bandwidth_down);
  337. up = atomic_read(&bat_priv->gw.bandwidth_up);
  338. return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
  339. down % 10, up / 10, up % 10);
  340. }
  341. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  342. struct attribute *attr, char *buff,
  343. size_t count)
  344. {
  345. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  346. if (buff[count - 1] == '\n')
  347. buff[count - 1] = '\0';
  348. return batadv_gw_bandwidth_set(net_dev, buff, count);
  349. }
  350. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  351. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  352. #ifdef CONFIG_BATMAN_ADV_BLA
  353. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  354. #endif
  355. #ifdef CONFIG_BATMAN_ADV_DAT
  356. BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR, NULL);
  357. #endif
  358. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  359. BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  360. static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
  361. batadv_store_vis_mode);
  362. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  363. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  364. batadv_store_gw_mode);
  365. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
  366. INT_MAX, NULL);
  367. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
  368. NULL);
  369. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
  370. batadv_post_gw_deselect);
  371. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  372. batadv_store_gw_bwidth);
  373. #ifdef CONFIG_BATMAN_ADV_DEBUG
  374. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
  375. #endif
  376. #ifdef CONFIG_BATMAN_ADV_NC
  377. BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR, NULL);
  378. #endif
  379. static struct batadv_attribute *batadv_mesh_attrs[] = {
  380. &batadv_attr_aggregated_ogms,
  381. &batadv_attr_bonding,
  382. #ifdef CONFIG_BATMAN_ADV_BLA
  383. &batadv_attr_bridge_loop_avoidance,
  384. #endif
  385. #ifdef CONFIG_BATMAN_ADV_DAT
  386. &batadv_attr_distributed_arp_table,
  387. #endif
  388. &batadv_attr_fragmentation,
  389. &batadv_attr_ap_isolation,
  390. &batadv_attr_vis_mode,
  391. &batadv_attr_routing_algo,
  392. &batadv_attr_gw_mode,
  393. &batadv_attr_orig_interval,
  394. &batadv_attr_hop_penalty,
  395. &batadv_attr_gw_sel_class,
  396. &batadv_attr_gw_bandwidth,
  397. #ifdef CONFIG_BATMAN_ADV_DEBUG
  398. &batadv_attr_log_level,
  399. #endif
  400. #ifdef CONFIG_BATMAN_ADV_NC
  401. &batadv_attr_network_coding,
  402. #endif
  403. NULL,
  404. };
  405. int batadv_sysfs_add_meshif(struct net_device *dev)
  406. {
  407. struct kobject *batif_kobject = &dev->dev.kobj;
  408. struct batadv_priv *bat_priv = netdev_priv(dev);
  409. struct batadv_attribute **bat_attr;
  410. int err;
  411. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  412. batif_kobject);
  413. if (!bat_priv->mesh_obj) {
  414. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  415. BATADV_SYSFS_IF_MESH_SUBDIR);
  416. goto out;
  417. }
  418. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  419. err = sysfs_create_file(bat_priv->mesh_obj,
  420. &((*bat_attr)->attr));
  421. if (err) {
  422. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  423. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  424. ((*bat_attr)->attr).name);
  425. goto rem_attr;
  426. }
  427. }
  428. return 0;
  429. rem_attr:
  430. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  431. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  432. kobject_put(bat_priv->mesh_obj);
  433. bat_priv->mesh_obj = NULL;
  434. out:
  435. return -ENOMEM;
  436. }
  437. void batadv_sysfs_del_meshif(struct net_device *dev)
  438. {
  439. struct batadv_priv *bat_priv = netdev_priv(dev);
  440. struct batadv_attribute **bat_attr;
  441. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  442. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  443. kobject_put(bat_priv->mesh_obj);
  444. bat_priv->mesh_obj = NULL;
  445. }
  446. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  447. struct attribute *attr, char *buff)
  448. {
  449. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  450. struct batadv_hard_iface *hard_iface;
  451. ssize_t length;
  452. const char *ifname;
  453. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  454. if (!hard_iface)
  455. return 0;
  456. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  457. ifname = "none";
  458. else
  459. ifname = hard_iface->soft_iface->name;
  460. length = sprintf(buff, "%s\n", ifname);
  461. batadv_hardif_free_ref(hard_iface);
  462. return length;
  463. }
  464. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  465. struct attribute *attr, char *buff,
  466. size_t count)
  467. {
  468. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  469. struct batadv_hard_iface *hard_iface;
  470. int status_tmp = -1;
  471. int ret = count;
  472. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  473. if (!hard_iface)
  474. return count;
  475. if (buff[count - 1] == '\n')
  476. buff[count - 1] = '\0';
  477. if (strlen(buff) >= IFNAMSIZ) {
  478. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  479. buff);
  480. batadv_hardif_free_ref(hard_iface);
  481. return -EINVAL;
  482. }
  483. if (strncmp(buff, "none", 4) == 0)
  484. status_tmp = BATADV_IF_NOT_IN_USE;
  485. else
  486. status_tmp = BATADV_IF_I_WANT_YOU;
  487. if (hard_iface->if_status == status_tmp)
  488. goto out;
  489. if ((hard_iface->soft_iface) &&
  490. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  491. goto out;
  492. rtnl_lock();
  493. if (status_tmp == BATADV_IF_NOT_IN_USE) {
  494. batadv_hardif_disable_interface(hard_iface,
  495. BATADV_IF_CLEANUP_AUTO);
  496. goto unlock;
  497. }
  498. /* if the interface already is in use */
  499. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  500. batadv_hardif_disable_interface(hard_iface,
  501. BATADV_IF_CLEANUP_AUTO);
  502. ret = batadv_hardif_enable_interface(hard_iface, buff);
  503. unlock:
  504. rtnl_unlock();
  505. out:
  506. batadv_hardif_free_ref(hard_iface);
  507. return ret;
  508. }
  509. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  510. struct attribute *attr, char *buff)
  511. {
  512. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  513. struct batadv_hard_iface *hard_iface;
  514. ssize_t length;
  515. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  516. if (!hard_iface)
  517. return 0;
  518. switch (hard_iface->if_status) {
  519. case BATADV_IF_TO_BE_REMOVED:
  520. length = sprintf(buff, "disabling\n");
  521. break;
  522. case BATADV_IF_INACTIVE:
  523. length = sprintf(buff, "inactive\n");
  524. break;
  525. case BATADV_IF_ACTIVE:
  526. length = sprintf(buff, "active\n");
  527. break;
  528. case BATADV_IF_TO_BE_ACTIVATED:
  529. length = sprintf(buff, "enabling\n");
  530. break;
  531. case BATADV_IF_NOT_IN_USE:
  532. default:
  533. length = sprintf(buff, "not in use\n");
  534. break;
  535. }
  536. batadv_hardif_free_ref(hard_iface);
  537. return length;
  538. }
  539. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  540. batadv_store_mesh_iface);
  541. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  542. static struct batadv_attribute *batadv_batman_attrs[] = {
  543. &batadv_attr_mesh_iface,
  544. &batadv_attr_iface_status,
  545. NULL,
  546. };
  547. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  548. {
  549. struct kobject *hardif_kobject = &dev->dev.kobj;
  550. struct batadv_attribute **bat_attr;
  551. int err;
  552. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  553. hardif_kobject);
  554. if (!*hardif_obj) {
  555. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  556. BATADV_SYSFS_IF_BAT_SUBDIR);
  557. goto out;
  558. }
  559. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  560. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  561. if (err) {
  562. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  563. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  564. ((*bat_attr)->attr).name);
  565. goto rem_attr;
  566. }
  567. }
  568. return 0;
  569. rem_attr:
  570. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  571. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  572. out:
  573. return -ENOMEM;
  574. }
  575. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  576. {
  577. kobject_put(*hardif_obj);
  578. *hardif_obj = NULL;
  579. }
  580. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  581. enum batadv_uev_action action, const char *data)
  582. {
  583. int ret = -ENOMEM;
  584. struct kobject *bat_kobj;
  585. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  586. bat_kobj = &bat_priv->soft_iface->dev.kobj;
  587. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  588. strlen(batadv_uev_type_str[type]) + 1,
  589. GFP_ATOMIC);
  590. if (!uevent_env[0])
  591. goto out;
  592. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  593. batadv_uev_type_str[type]);
  594. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  595. strlen(batadv_uev_action_str[action]) + 1,
  596. GFP_ATOMIC);
  597. if (!uevent_env[1])
  598. goto out;
  599. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  600. batadv_uev_action_str[action]);
  601. /* If the event is DEL, ignore the data field */
  602. if (action != BATADV_UEV_DEL) {
  603. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  604. strlen(data) + 1, GFP_ATOMIC);
  605. if (!uevent_env[2])
  606. goto out;
  607. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  608. }
  609. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  610. out:
  611. kfree(uevent_env[0]);
  612. kfree(uevent_env[1]);
  613. kfree(uevent_env[2]);
  614. if (ret)
  615. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  616. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  617. batadv_uev_type_str[type],
  618. batadv_uev_action_str[action],
  619. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  620. return ret;
  621. }