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. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  324. return count;
  325. }
  326. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  327. struct attribute *attr, char *buff)
  328. {
  329. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  330. int down, up;
  331. int gw_bandwidth = atomic_read(&bat_priv->gw_bandwidth);
  332. batadv_gw_bandwidth_to_kbit(gw_bandwidth, &down, &up);
  333. return sprintf(buff, "%i%s/%i%s\n",
  334. (down > 2048 ? down / 1024 : down),
  335. (down > 2048 ? "MBit" : "KBit"),
  336. (up > 2048 ? up / 1024 : up),
  337. (up > 2048 ? "MBit" : "KBit"));
  338. }
  339. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  340. struct attribute *attr, char *buff,
  341. size_t count)
  342. {
  343. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  344. if (buff[count - 1] == '\n')
  345. buff[count - 1] = '\0';
  346. return batadv_gw_bandwidth_set(net_dev, buff, count);
  347. }
  348. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  349. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  350. #ifdef CONFIG_BATMAN_ADV_BLA
  351. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  352. #endif
  353. #ifdef CONFIG_BATMAN_ADV_DAT
  354. BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR, NULL);
  355. #endif
  356. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  357. BATADV_ATTR_SIF_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  358. static BATADV_ATTR(vis_mode, S_IRUGO | S_IWUSR, batadv_show_vis_mode,
  359. batadv_store_vis_mode);
  360. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  361. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  362. batadv_store_gw_mode);
  363. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
  364. INT_MAX, NULL);
  365. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
  366. NULL);
  367. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
  368. batadv_post_gw_deselect);
  369. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  370. batadv_store_gw_bwidth);
  371. #ifdef CONFIG_BATMAN_ADV_DEBUG
  372. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
  373. #endif
  374. static struct batadv_attribute *batadv_mesh_attrs[] = {
  375. &batadv_attr_aggregated_ogms,
  376. &batadv_attr_bonding,
  377. #ifdef CONFIG_BATMAN_ADV_BLA
  378. &batadv_attr_bridge_loop_avoidance,
  379. #endif
  380. #ifdef CONFIG_BATMAN_ADV_DAT
  381. &batadv_attr_distributed_arp_table,
  382. #endif
  383. &batadv_attr_fragmentation,
  384. &batadv_attr_ap_isolation,
  385. &batadv_attr_vis_mode,
  386. &batadv_attr_routing_algo,
  387. &batadv_attr_gw_mode,
  388. &batadv_attr_orig_interval,
  389. &batadv_attr_hop_penalty,
  390. &batadv_attr_gw_sel_class,
  391. &batadv_attr_gw_bandwidth,
  392. #ifdef CONFIG_BATMAN_ADV_DEBUG
  393. &batadv_attr_log_level,
  394. #endif
  395. NULL,
  396. };
  397. int batadv_sysfs_add_meshif(struct net_device *dev)
  398. {
  399. struct kobject *batif_kobject = &dev->dev.kobj;
  400. struct batadv_priv *bat_priv = netdev_priv(dev);
  401. struct batadv_attribute **bat_attr;
  402. int err;
  403. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  404. batif_kobject);
  405. if (!bat_priv->mesh_obj) {
  406. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  407. BATADV_SYSFS_IF_MESH_SUBDIR);
  408. goto out;
  409. }
  410. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  411. err = sysfs_create_file(bat_priv->mesh_obj,
  412. &((*bat_attr)->attr));
  413. if (err) {
  414. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  415. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  416. ((*bat_attr)->attr).name);
  417. goto rem_attr;
  418. }
  419. }
  420. return 0;
  421. rem_attr:
  422. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  423. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  424. kobject_put(bat_priv->mesh_obj);
  425. bat_priv->mesh_obj = NULL;
  426. out:
  427. return -ENOMEM;
  428. }
  429. void batadv_sysfs_del_meshif(struct net_device *dev)
  430. {
  431. struct batadv_priv *bat_priv = netdev_priv(dev);
  432. struct batadv_attribute **bat_attr;
  433. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  434. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  435. kobject_put(bat_priv->mesh_obj);
  436. bat_priv->mesh_obj = NULL;
  437. }
  438. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  439. struct attribute *attr, char *buff)
  440. {
  441. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  442. struct batadv_hard_iface *hard_iface;
  443. ssize_t length;
  444. const char *ifname;
  445. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  446. if (!hard_iface)
  447. return 0;
  448. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  449. ifname = "none";
  450. else
  451. ifname = hard_iface->soft_iface->name;
  452. length = sprintf(buff, "%s\n", ifname);
  453. batadv_hardif_free_ref(hard_iface);
  454. return length;
  455. }
  456. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  457. struct attribute *attr, char *buff,
  458. size_t count)
  459. {
  460. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  461. struct batadv_hard_iface *hard_iface;
  462. int status_tmp = -1;
  463. int ret = count;
  464. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  465. if (!hard_iface)
  466. return count;
  467. if (buff[count - 1] == '\n')
  468. buff[count - 1] = '\0';
  469. if (strlen(buff) >= IFNAMSIZ) {
  470. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  471. buff);
  472. batadv_hardif_free_ref(hard_iface);
  473. return -EINVAL;
  474. }
  475. if (strncmp(buff, "none", 4) == 0)
  476. status_tmp = BATADV_IF_NOT_IN_USE;
  477. else
  478. status_tmp = BATADV_IF_I_WANT_YOU;
  479. if (hard_iface->if_status == status_tmp)
  480. goto out;
  481. if ((hard_iface->soft_iface) &&
  482. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  483. goto out;
  484. if (!rtnl_trylock()) {
  485. ret = -ERESTARTSYS;
  486. goto out;
  487. }
  488. if (status_tmp == BATADV_IF_NOT_IN_USE) {
  489. batadv_hardif_disable_interface(hard_iface);
  490. goto unlock;
  491. }
  492. /* if the interface already is in use */
  493. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  494. batadv_hardif_disable_interface(hard_iface);
  495. ret = batadv_hardif_enable_interface(hard_iface, buff);
  496. unlock:
  497. rtnl_unlock();
  498. out:
  499. batadv_hardif_free_ref(hard_iface);
  500. return ret;
  501. }
  502. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  503. struct attribute *attr, char *buff)
  504. {
  505. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  506. struct batadv_hard_iface *hard_iface;
  507. ssize_t length;
  508. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  509. if (!hard_iface)
  510. return 0;
  511. switch (hard_iface->if_status) {
  512. case BATADV_IF_TO_BE_REMOVED:
  513. length = sprintf(buff, "disabling\n");
  514. break;
  515. case BATADV_IF_INACTIVE:
  516. length = sprintf(buff, "inactive\n");
  517. break;
  518. case BATADV_IF_ACTIVE:
  519. length = sprintf(buff, "active\n");
  520. break;
  521. case BATADV_IF_TO_BE_ACTIVATED:
  522. length = sprintf(buff, "enabling\n");
  523. break;
  524. case BATADV_IF_NOT_IN_USE:
  525. default:
  526. length = sprintf(buff, "not in use\n");
  527. break;
  528. }
  529. batadv_hardif_free_ref(hard_iface);
  530. return length;
  531. }
  532. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  533. batadv_store_mesh_iface);
  534. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  535. static struct batadv_attribute *batadv_batman_attrs[] = {
  536. &batadv_attr_mesh_iface,
  537. &batadv_attr_iface_status,
  538. NULL,
  539. };
  540. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  541. {
  542. struct kobject *hardif_kobject = &dev->dev.kobj;
  543. struct batadv_attribute **bat_attr;
  544. int err;
  545. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  546. hardif_kobject);
  547. if (!*hardif_obj) {
  548. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  549. BATADV_SYSFS_IF_BAT_SUBDIR);
  550. goto out;
  551. }
  552. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  553. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  554. if (err) {
  555. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  556. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  557. ((*bat_attr)->attr).name);
  558. goto rem_attr;
  559. }
  560. }
  561. return 0;
  562. rem_attr:
  563. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  564. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  565. out:
  566. return -ENOMEM;
  567. }
  568. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  569. {
  570. kobject_put(*hardif_obj);
  571. *hardif_obj = NULL;
  572. }
  573. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  574. enum batadv_uev_action action, const char *data)
  575. {
  576. int ret = -ENOMEM;
  577. struct batadv_hard_iface *primary_if;
  578. struct kobject *bat_kobj;
  579. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  580. primary_if = batadv_primary_if_get_selected(bat_priv);
  581. if (!primary_if)
  582. goto out;
  583. bat_kobj = &primary_if->soft_iface->dev.kobj;
  584. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  585. strlen(batadv_uev_type_str[type]) + 1,
  586. GFP_ATOMIC);
  587. if (!uevent_env[0])
  588. goto out;
  589. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  590. batadv_uev_type_str[type]);
  591. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  592. strlen(batadv_uev_action_str[action]) + 1,
  593. GFP_ATOMIC);
  594. if (!uevent_env[1])
  595. goto out;
  596. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  597. batadv_uev_action_str[action]);
  598. /* If the event is DEL, ignore the data field */
  599. if (action != BATADV_UEV_DEL) {
  600. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  601. strlen(data) + 1, GFP_ATOMIC);
  602. if (!uevent_env[2])
  603. goto out;
  604. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  605. }
  606. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  607. out:
  608. kfree(uevent_env[0]);
  609. kfree(uevent_env[1]);
  610. kfree(uevent_env[2]);
  611. if (primary_if)
  612. batadv_hardif_free_ref(primary_if);
  613. if (ret)
  614. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  615. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  616. batadv_uev_type_str[type],
  617. batadv_uev_action_str[action],
  618. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  619. return ret;
  620. }