sysfs.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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 "network-coding.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. #include "soft-interface.h"
  27. #include "gateway_common.h"
  28. #include "gateway_client.h"
  29. static struct net_device *batadv_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 batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
  35. {
  36. struct net_device *net_dev = batadv_kobj_to_netdev(obj);
  37. return netdev_priv(net_dev);
  38. }
  39. /**
  40. * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
  41. * @obj: kobject to covert
  42. *
  43. * Returns the associated batadv_priv struct.
  44. */
  45. static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
  46. {
  47. /* VLAN specific attributes are located in the root sysfs folder if they
  48. * refer to the untagged VLAN..
  49. */
  50. if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
  51. return batadv_kobj_to_batpriv(obj);
  52. /* ..while the attributes for the tagged vlans are located in
  53. * the in the corresponding "vlan%VID" subfolder
  54. */
  55. return batadv_kobj_to_batpriv(obj->parent);
  56. }
  57. /**
  58. * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
  59. * @obj: kobject to covert
  60. *
  61. * Returns the associated softif_vlan struct if found, NULL otherwise.
  62. */
  63. static struct batadv_softif_vlan *
  64. batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
  65. {
  66. struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
  67. rcu_read_lock();
  68. hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
  69. if (vlan_tmp->kobj != obj)
  70. continue;
  71. if (!atomic_inc_not_zero(&vlan_tmp->refcount))
  72. continue;
  73. vlan = vlan_tmp;
  74. break;
  75. }
  76. rcu_read_unlock();
  77. return vlan;
  78. }
  79. #define BATADV_UEV_TYPE_VAR "BATTYPE="
  80. #define BATADV_UEV_ACTION_VAR "BATACTION="
  81. #define BATADV_UEV_DATA_VAR "BATDATA="
  82. static char *batadv_uev_action_str[] = {
  83. "add",
  84. "del",
  85. "change"
  86. };
  87. static char *batadv_uev_type_str[] = {
  88. "gw"
  89. };
  90. /* Use this, if you have customized show and store functions for vlan attrs */
  91. #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
  92. struct batadv_attribute batadv_attr_vlan_##_name = { \
  93. .attr = {.name = __stringify(_name), \
  94. .mode = _mode }, \
  95. .show = _show, \
  96. .store = _store, \
  97. };
  98. /* Use this, if you have customized show and store functions */
  99. #define BATADV_ATTR(_name, _mode, _show, _store) \
  100. struct batadv_attribute batadv_attr_##_name = { \
  101. .attr = {.name = __stringify(_name), \
  102. .mode = _mode }, \
  103. .show = _show, \
  104. .store = _store, \
  105. };
  106. #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  107. ssize_t batadv_store_##_name(struct kobject *kobj, \
  108. struct attribute *attr, char *buff, \
  109. size_t count) \
  110. { \
  111. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  112. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  113. return __batadv_store_bool_attr(buff, count, _post_func, attr, \
  114. &bat_priv->_name, net_dev); \
  115. }
  116. #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  117. ssize_t batadv_show_##_name(struct kobject *kobj, \
  118. struct attribute *attr, char *buff) \
  119. { \
  120. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  121. return sprintf(buff, "%s\n", \
  122. atomic_read(&bat_priv->_name) == 0 ? \
  123. "disabled" : "enabled"); \
  124. } \
  125. /* Use this, if you are going to turn a [name] in the soft-interface
  126. * (bat_priv) on or off
  127. */
  128. #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  129. static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  130. static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  131. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  132. batadv_store_##_name)
  133. #define BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func) \
  134. ssize_t batadv_store_##_name(struct kobject *kobj, \
  135. struct attribute *attr, char *buff, \
  136. size_t count) \
  137. { \
  138. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  139. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  140. return __batadv_store_uint_attr(buff, count, _min, _max, \
  141. _post_func, attr, \
  142. &bat_priv->_name, net_dev); \
  143. }
  144. #define BATADV_ATTR_SIF_SHOW_UINT(_name) \
  145. ssize_t batadv_show_##_name(struct kobject *kobj, \
  146. struct attribute *attr, char *buff) \
  147. { \
  148. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  149. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_name)); \
  150. } \
  151. /* Use this, if you are going to set [name] in the soft-interface
  152. * (bat_priv) to an unsigned integer value
  153. */
  154. #define BATADV_ATTR_SIF_UINT(_name, _mode, _min, _max, _post_func) \
  155. static BATADV_ATTR_SIF_STORE_UINT(_name, _min, _max, _post_func)\
  156. static BATADV_ATTR_SIF_SHOW_UINT(_name) \
  157. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  158. batadv_store_##_name)
  159. #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
  160. ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
  161. struct attribute *attr, char *buff, \
  162. size_t count) \
  163. { \
  164. struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
  165. struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
  166. kobj); \
  167. size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
  168. attr, &vlan->_name, \
  169. bat_priv->soft_iface); \
  170. batadv_softif_vlan_free_ref(vlan); \
  171. return res; \
  172. }
  173. #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
  174. ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
  175. struct attribute *attr, char *buff) \
  176. { \
  177. struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
  178. struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
  179. kobj); \
  180. size_t res = sprintf(buff, "%s\n", \
  181. atomic_read(&vlan->_name) == 0 ? \
  182. "disabled" : "enabled"); \
  183. batadv_softif_vlan_free_ref(vlan); \
  184. return res; \
  185. }
  186. /* Use this, if you are going to turn a [name] in the vlan struct on or off */
  187. #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
  188. static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
  189. static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
  190. static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
  191. batadv_store_vlan_##_name)
  192. static int batadv_store_bool_attr(char *buff, size_t count,
  193. struct net_device *net_dev,
  194. const char *attr_name, atomic_t *attr)
  195. {
  196. int enabled = -1;
  197. if (buff[count - 1] == '\n')
  198. buff[count - 1] = '\0';
  199. if ((strncmp(buff, "1", 2) == 0) ||
  200. (strncmp(buff, "enable", 7) == 0) ||
  201. (strncmp(buff, "enabled", 8) == 0))
  202. enabled = 1;
  203. if ((strncmp(buff, "0", 2) == 0) ||
  204. (strncmp(buff, "disable", 8) == 0) ||
  205. (strncmp(buff, "disabled", 9) == 0))
  206. enabled = 0;
  207. if (enabled < 0) {
  208. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  209. attr_name, buff);
  210. return -EINVAL;
  211. }
  212. if (atomic_read(attr) == enabled)
  213. return count;
  214. batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  215. atomic_read(attr) == 1 ? "enabled" : "disabled",
  216. enabled == 1 ? "enabled" : "disabled");
  217. atomic_set(attr, (unsigned int)enabled);
  218. return count;
  219. }
  220. static inline ssize_t
  221. __batadv_store_bool_attr(char *buff, size_t count,
  222. void (*post_func)(struct net_device *),
  223. struct attribute *attr,
  224. atomic_t *attr_store, struct net_device *net_dev)
  225. {
  226. int ret;
  227. ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
  228. attr_store);
  229. if (post_func && ret)
  230. post_func(net_dev);
  231. return ret;
  232. }
  233. static int batadv_store_uint_attr(const char *buff, size_t count,
  234. struct net_device *net_dev,
  235. const char *attr_name,
  236. unsigned int min, unsigned int max,
  237. atomic_t *attr)
  238. {
  239. unsigned long uint_val;
  240. int ret;
  241. ret = kstrtoul(buff, 10, &uint_val);
  242. if (ret) {
  243. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  244. attr_name, buff);
  245. return -EINVAL;
  246. }
  247. if (uint_val < min) {
  248. batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  249. attr_name, uint_val, min);
  250. return -EINVAL;
  251. }
  252. if (uint_val > max) {
  253. batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  254. attr_name, uint_val, max);
  255. return -EINVAL;
  256. }
  257. if (atomic_read(attr) == uint_val)
  258. return count;
  259. batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
  260. attr_name, atomic_read(attr), uint_val);
  261. atomic_set(attr, uint_val);
  262. return count;
  263. }
  264. static inline ssize_t
  265. __batadv_store_uint_attr(const char *buff, size_t count,
  266. int min, int max,
  267. void (*post_func)(struct net_device *),
  268. const struct attribute *attr,
  269. atomic_t *attr_store, struct net_device *net_dev)
  270. {
  271. int ret;
  272. ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
  273. attr_store);
  274. if (post_func && ret)
  275. post_func(net_dev);
  276. return ret;
  277. }
  278. static ssize_t batadv_show_bat_algo(struct kobject *kobj,
  279. struct attribute *attr, char *buff)
  280. {
  281. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  282. return sprintf(buff, "%s\n", bat_priv->bat_algo_ops->name);
  283. }
  284. static void batadv_post_gw_deselect(struct net_device *net_dev)
  285. {
  286. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  287. batadv_gw_deselect(bat_priv);
  288. }
  289. static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
  290. char *buff)
  291. {
  292. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  293. int bytes_written;
  294. switch (atomic_read(&bat_priv->gw_mode)) {
  295. case BATADV_GW_MODE_CLIENT:
  296. bytes_written = sprintf(buff, "%s\n",
  297. BATADV_GW_MODE_CLIENT_NAME);
  298. break;
  299. case BATADV_GW_MODE_SERVER:
  300. bytes_written = sprintf(buff, "%s\n",
  301. BATADV_GW_MODE_SERVER_NAME);
  302. break;
  303. default:
  304. bytes_written = sprintf(buff, "%s\n",
  305. BATADV_GW_MODE_OFF_NAME);
  306. break;
  307. }
  308. return bytes_written;
  309. }
  310. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  311. struct attribute *attr, char *buff,
  312. size_t count)
  313. {
  314. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  315. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  316. char *curr_gw_mode_str;
  317. int gw_mode_tmp = -1;
  318. if (buff[count - 1] == '\n')
  319. buff[count - 1] = '\0';
  320. if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
  321. strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
  322. gw_mode_tmp = BATADV_GW_MODE_OFF;
  323. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  324. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  325. gw_mode_tmp = BATADV_GW_MODE_CLIENT;
  326. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  327. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  328. gw_mode_tmp = BATADV_GW_MODE_SERVER;
  329. if (gw_mode_tmp < 0) {
  330. batadv_info(net_dev,
  331. "Invalid parameter for 'gw mode' setting received: %s\n",
  332. buff);
  333. return -EINVAL;
  334. }
  335. if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp)
  336. return count;
  337. switch (atomic_read(&bat_priv->gw_mode)) {
  338. case BATADV_GW_MODE_CLIENT:
  339. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  340. break;
  341. case BATADV_GW_MODE_SERVER:
  342. curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
  343. break;
  344. default:
  345. curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
  346. break;
  347. }
  348. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  349. curr_gw_mode_str, buff);
  350. batadv_gw_deselect(bat_priv);
  351. /* always call batadv_gw_check_client_stop() before changing the gateway
  352. * state
  353. */
  354. batadv_gw_check_client_stop(bat_priv);
  355. atomic_set(&bat_priv->gw_mode, (unsigned int)gw_mode_tmp);
  356. batadv_gw_tvlv_container_update(bat_priv);
  357. return count;
  358. }
  359. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  360. struct attribute *attr, char *buff)
  361. {
  362. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  363. uint32_t down, up;
  364. down = atomic_read(&bat_priv->gw.bandwidth_down);
  365. up = atomic_read(&bat_priv->gw.bandwidth_up);
  366. return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
  367. down % 10, up / 10, up % 10);
  368. }
  369. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  370. struct attribute *attr, char *buff,
  371. size_t count)
  372. {
  373. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  374. if (buff[count - 1] == '\n')
  375. buff[count - 1] = '\0';
  376. return batadv_gw_bandwidth_set(net_dev, buff, count);
  377. }
  378. BATADV_ATTR_SIF_BOOL(aggregated_ogms, S_IRUGO | S_IWUSR, NULL);
  379. BATADV_ATTR_SIF_BOOL(bonding, S_IRUGO | S_IWUSR, NULL);
  380. #ifdef CONFIG_BATMAN_ADV_BLA
  381. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, S_IRUGO | S_IWUSR, NULL);
  382. #endif
  383. #ifdef CONFIG_BATMAN_ADV_DAT
  384. BATADV_ATTR_SIF_BOOL(distributed_arp_table, S_IRUGO | S_IWUSR,
  385. batadv_dat_status_update);
  386. #endif
  387. BATADV_ATTR_SIF_BOOL(fragmentation, S_IRUGO | S_IWUSR, batadv_update_min_mtu);
  388. static BATADV_ATTR(routing_algo, S_IRUGO, batadv_show_bat_algo, NULL);
  389. static BATADV_ATTR(gw_mode, S_IRUGO | S_IWUSR, batadv_show_gw_mode,
  390. batadv_store_gw_mode);
  391. BATADV_ATTR_SIF_UINT(orig_interval, S_IRUGO | S_IWUSR, 2 * BATADV_JITTER,
  392. INT_MAX, NULL);
  393. BATADV_ATTR_SIF_UINT(hop_penalty, S_IRUGO | S_IWUSR, 0, BATADV_TQ_MAX_VALUE,
  394. NULL);
  395. BATADV_ATTR_SIF_UINT(gw_sel_class, S_IRUGO | S_IWUSR, 1, BATADV_TQ_MAX_VALUE,
  396. batadv_post_gw_deselect);
  397. static BATADV_ATTR(gw_bandwidth, S_IRUGO | S_IWUSR, batadv_show_gw_bwidth,
  398. batadv_store_gw_bwidth);
  399. #ifdef CONFIG_BATMAN_ADV_DEBUG
  400. BATADV_ATTR_SIF_UINT(log_level, S_IRUGO | S_IWUSR, 0, BATADV_DBG_ALL, NULL);
  401. #endif
  402. #ifdef CONFIG_BATMAN_ADV_NC
  403. BATADV_ATTR_SIF_BOOL(network_coding, S_IRUGO | S_IWUSR,
  404. batadv_nc_status_update);
  405. #endif
  406. static struct batadv_attribute *batadv_mesh_attrs[] = {
  407. &batadv_attr_aggregated_ogms,
  408. &batadv_attr_bonding,
  409. #ifdef CONFIG_BATMAN_ADV_BLA
  410. &batadv_attr_bridge_loop_avoidance,
  411. #endif
  412. #ifdef CONFIG_BATMAN_ADV_DAT
  413. &batadv_attr_distributed_arp_table,
  414. #endif
  415. &batadv_attr_fragmentation,
  416. &batadv_attr_routing_algo,
  417. &batadv_attr_gw_mode,
  418. &batadv_attr_orig_interval,
  419. &batadv_attr_hop_penalty,
  420. &batadv_attr_gw_sel_class,
  421. &batadv_attr_gw_bandwidth,
  422. #ifdef CONFIG_BATMAN_ADV_DEBUG
  423. &batadv_attr_log_level,
  424. #endif
  425. #ifdef CONFIG_BATMAN_ADV_NC
  426. &batadv_attr_network_coding,
  427. #endif
  428. NULL,
  429. };
  430. BATADV_ATTR_VLAN_BOOL(ap_isolation, S_IRUGO | S_IWUSR, NULL);
  431. /**
  432. * batadv_vlan_attrs - array of vlan specific sysfs attributes
  433. */
  434. static struct batadv_attribute *batadv_vlan_attrs[] = {
  435. &batadv_attr_vlan_ap_isolation,
  436. NULL,
  437. };
  438. int batadv_sysfs_add_meshif(struct net_device *dev)
  439. {
  440. struct kobject *batif_kobject = &dev->dev.kobj;
  441. struct batadv_priv *bat_priv = netdev_priv(dev);
  442. struct batadv_attribute **bat_attr;
  443. int err;
  444. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  445. batif_kobject);
  446. if (!bat_priv->mesh_obj) {
  447. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  448. BATADV_SYSFS_IF_MESH_SUBDIR);
  449. goto out;
  450. }
  451. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  452. err = sysfs_create_file(bat_priv->mesh_obj,
  453. &((*bat_attr)->attr));
  454. if (err) {
  455. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  456. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  457. ((*bat_attr)->attr).name);
  458. goto rem_attr;
  459. }
  460. }
  461. return 0;
  462. rem_attr:
  463. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  464. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  465. kobject_put(bat_priv->mesh_obj);
  466. bat_priv->mesh_obj = NULL;
  467. out:
  468. return -ENOMEM;
  469. }
  470. void batadv_sysfs_del_meshif(struct net_device *dev)
  471. {
  472. struct batadv_priv *bat_priv = netdev_priv(dev);
  473. struct batadv_attribute **bat_attr;
  474. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  475. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  476. kobject_put(bat_priv->mesh_obj);
  477. bat_priv->mesh_obj = NULL;
  478. }
  479. /**
  480. * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
  481. * @dev: netdev of the mesh interface
  482. * @vlan: private data of the newly added VLAN interface
  483. *
  484. * Returns 0 on success and -ENOMEM if any of the structure allocations fails.
  485. */
  486. int batadv_sysfs_add_vlan(struct net_device *dev,
  487. struct batadv_softif_vlan *vlan)
  488. {
  489. char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
  490. struct batadv_priv *bat_priv = netdev_priv(dev);
  491. struct batadv_attribute **bat_attr;
  492. int err;
  493. if (vlan->vid & BATADV_VLAN_HAS_TAG) {
  494. sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
  495. vlan->vid & VLAN_VID_MASK);
  496. vlan->kobj = kobject_create_and_add(vlan_subdir,
  497. bat_priv->mesh_obj);
  498. if (!vlan->kobj) {
  499. batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
  500. dev->name, vlan_subdir);
  501. goto out;
  502. }
  503. } else {
  504. /* the untagged LAN uses the root folder to store its "VLAN
  505. * specific attributes"
  506. */
  507. vlan->kobj = bat_priv->mesh_obj;
  508. kobject_get(bat_priv->mesh_obj);
  509. }
  510. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
  511. err = sysfs_create_file(vlan->kobj,
  512. &((*bat_attr)->attr));
  513. if (err) {
  514. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  515. dev->name, vlan_subdir,
  516. ((*bat_attr)->attr).name);
  517. goto rem_attr;
  518. }
  519. }
  520. return 0;
  521. rem_attr:
  522. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
  523. sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
  524. kobject_put(vlan->kobj);
  525. vlan->kobj = NULL;
  526. out:
  527. return -ENOMEM;
  528. }
  529. /**
  530. * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
  531. * @bat_priv: the bat priv with all the soft interface information
  532. * @vlan: the private data of the VLAN to destroy
  533. */
  534. void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
  535. struct batadv_softif_vlan *vlan)
  536. {
  537. struct batadv_attribute **bat_attr;
  538. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
  539. sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
  540. kobject_put(vlan->kobj);
  541. vlan->kobj = NULL;
  542. }
  543. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  544. struct attribute *attr, char *buff)
  545. {
  546. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  547. struct batadv_hard_iface *hard_iface;
  548. ssize_t length;
  549. const char *ifname;
  550. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  551. if (!hard_iface)
  552. return 0;
  553. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  554. ifname = "none";
  555. else
  556. ifname = hard_iface->soft_iface->name;
  557. length = sprintf(buff, "%s\n", ifname);
  558. batadv_hardif_free_ref(hard_iface);
  559. return length;
  560. }
  561. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  562. struct attribute *attr, char *buff,
  563. size_t count)
  564. {
  565. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  566. struct batadv_hard_iface *hard_iface;
  567. int status_tmp = -1;
  568. int ret = count;
  569. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  570. if (!hard_iface)
  571. return count;
  572. if (buff[count - 1] == '\n')
  573. buff[count - 1] = '\0';
  574. if (strlen(buff) >= IFNAMSIZ) {
  575. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  576. buff);
  577. batadv_hardif_free_ref(hard_iface);
  578. return -EINVAL;
  579. }
  580. if (strncmp(buff, "none", 4) == 0)
  581. status_tmp = BATADV_IF_NOT_IN_USE;
  582. else
  583. status_tmp = BATADV_IF_I_WANT_YOU;
  584. if (hard_iface->if_status == status_tmp)
  585. goto out;
  586. if ((hard_iface->soft_iface) &&
  587. (strncmp(hard_iface->soft_iface->name, buff, IFNAMSIZ) == 0))
  588. goto out;
  589. rtnl_lock();
  590. if (status_tmp == BATADV_IF_NOT_IN_USE) {
  591. batadv_hardif_disable_interface(hard_iface,
  592. BATADV_IF_CLEANUP_AUTO);
  593. goto unlock;
  594. }
  595. /* if the interface already is in use */
  596. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  597. batadv_hardif_disable_interface(hard_iface,
  598. BATADV_IF_CLEANUP_AUTO);
  599. ret = batadv_hardif_enable_interface(hard_iface, buff);
  600. unlock:
  601. rtnl_unlock();
  602. out:
  603. batadv_hardif_free_ref(hard_iface);
  604. return ret;
  605. }
  606. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  607. struct attribute *attr, char *buff)
  608. {
  609. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  610. struct batadv_hard_iface *hard_iface;
  611. ssize_t length;
  612. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  613. if (!hard_iface)
  614. return 0;
  615. switch (hard_iface->if_status) {
  616. case BATADV_IF_TO_BE_REMOVED:
  617. length = sprintf(buff, "disabling\n");
  618. break;
  619. case BATADV_IF_INACTIVE:
  620. length = sprintf(buff, "inactive\n");
  621. break;
  622. case BATADV_IF_ACTIVE:
  623. length = sprintf(buff, "active\n");
  624. break;
  625. case BATADV_IF_TO_BE_ACTIVATED:
  626. length = sprintf(buff, "enabling\n");
  627. break;
  628. case BATADV_IF_NOT_IN_USE:
  629. default:
  630. length = sprintf(buff, "not in use\n");
  631. break;
  632. }
  633. batadv_hardif_free_ref(hard_iface);
  634. return length;
  635. }
  636. static BATADV_ATTR(mesh_iface, S_IRUGO | S_IWUSR, batadv_show_mesh_iface,
  637. batadv_store_mesh_iface);
  638. static BATADV_ATTR(iface_status, S_IRUGO, batadv_show_iface_status, NULL);
  639. static struct batadv_attribute *batadv_batman_attrs[] = {
  640. &batadv_attr_mesh_iface,
  641. &batadv_attr_iface_status,
  642. NULL,
  643. };
  644. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  645. {
  646. struct kobject *hardif_kobject = &dev->dev.kobj;
  647. struct batadv_attribute **bat_attr;
  648. int err;
  649. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  650. hardif_kobject);
  651. if (!*hardif_obj) {
  652. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  653. BATADV_SYSFS_IF_BAT_SUBDIR);
  654. goto out;
  655. }
  656. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  657. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  658. if (err) {
  659. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  660. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  661. ((*bat_attr)->attr).name);
  662. goto rem_attr;
  663. }
  664. }
  665. return 0;
  666. rem_attr:
  667. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  668. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  669. out:
  670. return -ENOMEM;
  671. }
  672. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  673. {
  674. kobject_put(*hardif_obj);
  675. *hardif_obj = NULL;
  676. }
  677. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  678. enum batadv_uev_action action, const char *data)
  679. {
  680. int ret = -ENOMEM;
  681. struct kobject *bat_kobj;
  682. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  683. bat_kobj = &bat_priv->soft_iface->dev.kobj;
  684. uevent_env[0] = kmalloc(strlen(BATADV_UEV_TYPE_VAR) +
  685. strlen(batadv_uev_type_str[type]) + 1,
  686. GFP_ATOMIC);
  687. if (!uevent_env[0])
  688. goto out;
  689. sprintf(uevent_env[0], "%s%s", BATADV_UEV_TYPE_VAR,
  690. batadv_uev_type_str[type]);
  691. uevent_env[1] = kmalloc(strlen(BATADV_UEV_ACTION_VAR) +
  692. strlen(batadv_uev_action_str[action]) + 1,
  693. GFP_ATOMIC);
  694. if (!uevent_env[1])
  695. goto out;
  696. sprintf(uevent_env[1], "%s%s", BATADV_UEV_ACTION_VAR,
  697. batadv_uev_action_str[action]);
  698. /* If the event is DEL, ignore the data field */
  699. if (action != BATADV_UEV_DEL) {
  700. uevent_env[2] = kmalloc(strlen(BATADV_UEV_DATA_VAR) +
  701. strlen(data) + 1, GFP_ATOMIC);
  702. if (!uevent_env[2])
  703. goto out;
  704. sprintf(uevent_env[2], "%s%s", BATADV_UEV_DATA_VAR, data);
  705. }
  706. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  707. out:
  708. kfree(uevent_env[0]);
  709. kfree(uevent_env[1]);
  710. kfree(uevent_env[2]);
  711. if (ret)
  712. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  713. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  714. batadv_uev_type_str[type],
  715. batadv_uev_action_str[action],
  716. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  717. return ret;
  718. }