sysfs.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /* Copyright (C) 2010-2016 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "sysfs.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/compiler.h>
  21. #include <linux/device.h>
  22. #include <linux/errno.h>
  23. #include <linux/fs.h>
  24. #include <linux/if.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/kernel.h>
  27. #include <linux/kref.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/printk.h>
  30. #include <linux/rculist.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/rtnetlink.h>
  33. #include <linux/slab.h>
  34. #include <linux/stddef.h>
  35. #include <linux/string.h>
  36. #include <linux/stringify.h>
  37. #include <linux/workqueue.h>
  38. #include "bridge_loop_avoidance.h"
  39. #include "distributed-arp-table.h"
  40. #include "gateway_client.h"
  41. #include "gateway_common.h"
  42. #include "hard-interface.h"
  43. #include "log.h"
  44. #include "network-coding.h"
  45. #include "packet.h"
  46. #include "soft-interface.h"
  47. static struct net_device *batadv_kobj_to_netdev(struct kobject *obj)
  48. {
  49. struct device *dev = container_of(obj->parent, struct device, kobj);
  50. return to_net_dev(dev);
  51. }
  52. static struct batadv_priv *batadv_kobj_to_batpriv(struct kobject *obj)
  53. {
  54. struct net_device *net_dev = batadv_kobj_to_netdev(obj);
  55. return netdev_priv(net_dev);
  56. }
  57. /**
  58. * batadv_vlan_kobj_to_batpriv - convert a vlan kobj in the associated batpriv
  59. * @obj: kobject to covert
  60. *
  61. * Return: the associated batadv_priv struct.
  62. */
  63. static struct batadv_priv *batadv_vlan_kobj_to_batpriv(struct kobject *obj)
  64. {
  65. /* VLAN specific attributes are located in the root sysfs folder if they
  66. * refer to the untagged VLAN..
  67. */
  68. if (!strcmp(BATADV_SYSFS_IF_MESH_SUBDIR, obj->name))
  69. return batadv_kobj_to_batpriv(obj);
  70. /* ..while the attributes for the tagged vlans are located in
  71. * the in the corresponding "vlan%VID" subfolder
  72. */
  73. return batadv_kobj_to_batpriv(obj->parent);
  74. }
  75. /**
  76. * batadv_kobj_to_vlan - convert a kobj in the associated softif_vlan struct
  77. * @bat_priv: the bat priv with all the soft interface information
  78. * @obj: kobject to covert
  79. *
  80. * Return: the associated softif_vlan struct if found, NULL otherwise.
  81. */
  82. static struct batadv_softif_vlan *
  83. batadv_kobj_to_vlan(struct batadv_priv *bat_priv, struct kobject *obj)
  84. {
  85. struct batadv_softif_vlan *vlan_tmp, *vlan = NULL;
  86. rcu_read_lock();
  87. hlist_for_each_entry_rcu(vlan_tmp, &bat_priv->softif_vlan_list, list) {
  88. if (vlan_tmp->kobj != obj)
  89. continue;
  90. if (!kref_get_unless_zero(&vlan_tmp->refcount))
  91. continue;
  92. vlan = vlan_tmp;
  93. break;
  94. }
  95. rcu_read_unlock();
  96. return vlan;
  97. }
  98. #define BATADV_UEV_TYPE_VAR "BATTYPE="
  99. #define BATADV_UEV_ACTION_VAR "BATACTION="
  100. #define BATADV_UEV_DATA_VAR "BATDATA="
  101. static char *batadv_uev_action_str[] = {
  102. "add",
  103. "del",
  104. "change",
  105. "loopdetect",
  106. };
  107. static char *batadv_uev_type_str[] = {
  108. "gw",
  109. "bla",
  110. };
  111. /* Use this, if you have customized show and store functions for vlan attrs */
  112. #define BATADV_ATTR_VLAN(_name, _mode, _show, _store) \
  113. struct batadv_attribute batadv_attr_vlan_##_name = { \
  114. .attr = {.name = __stringify(_name), \
  115. .mode = _mode }, \
  116. .show = _show, \
  117. .store = _store, \
  118. }
  119. /* Use this, if you have customized show and store functions */
  120. #define BATADV_ATTR(_name, _mode, _show, _store) \
  121. struct batadv_attribute batadv_attr_##_name = { \
  122. .attr = {.name = __stringify(_name), \
  123. .mode = _mode }, \
  124. .show = _show, \
  125. .store = _store, \
  126. }
  127. #define BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  128. ssize_t batadv_store_##_name(struct kobject *kobj, \
  129. struct attribute *attr, char *buff, \
  130. size_t count) \
  131. { \
  132. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  133. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  134. \
  135. return __batadv_store_bool_attr(buff, count, _post_func, attr, \
  136. &bat_priv->_name, net_dev); \
  137. }
  138. #define BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  139. ssize_t batadv_show_##_name(struct kobject *kobj, \
  140. struct attribute *attr, char *buff) \
  141. { \
  142. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  143. \
  144. return sprintf(buff, "%s\n", \
  145. atomic_read(&bat_priv->_name) == 0 ? \
  146. "disabled" : "enabled"); \
  147. } \
  148. /* Use this, if you are going to turn a [name] in the soft-interface
  149. * (bat_priv) on or off
  150. */
  151. #define BATADV_ATTR_SIF_BOOL(_name, _mode, _post_func) \
  152. static BATADV_ATTR_SIF_STORE_BOOL(_name, _post_func) \
  153. static BATADV_ATTR_SIF_SHOW_BOOL(_name) \
  154. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  155. batadv_store_##_name)
  156. #define BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
  157. ssize_t batadv_store_##_name(struct kobject *kobj, \
  158. struct attribute *attr, char *buff, \
  159. size_t count) \
  160. { \
  161. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  162. struct batadv_priv *bat_priv = netdev_priv(net_dev); \
  163. \
  164. return __batadv_store_uint_attr(buff, count, _min, _max, \
  165. _post_func, attr, \
  166. &bat_priv->_var, net_dev); \
  167. }
  168. #define BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
  169. ssize_t batadv_show_##_name(struct kobject *kobj, \
  170. struct attribute *attr, char *buff) \
  171. { \
  172. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj); \
  173. \
  174. return sprintf(buff, "%i\n", atomic_read(&bat_priv->_var)); \
  175. } \
  176. /* Use this, if you are going to set [name] in the soft-interface
  177. * (bat_priv) to an unsigned integer value
  178. */
  179. #define BATADV_ATTR_SIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
  180. static BATADV_ATTR_SIF_STORE_UINT(_name, _var, _min, _max, _post_func)\
  181. static BATADV_ATTR_SIF_SHOW_UINT(_name, _var) \
  182. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  183. batadv_store_##_name)
  184. #define BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
  185. ssize_t batadv_store_vlan_##_name(struct kobject *kobj, \
  186. struct attribute *attr, char *buff, \
  187. size_t count) \
  188. { \
  189. struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
  190. struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
  191. kobj); \
  192. size_t res = __batadv_store_bool_attr(buff, count, _post_func, \
  193. attr, &vlan->_name, \
  194. bat_priv->soft_iface); \
  195. \
  196. batadv_softif_vlan_put(vlan); \
  197. return res; \
  198. }
  199. #define BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
  200. ssize_t batadv_show_vlan_##_name(struct kobject *kobj, \
  201. struct attribute *attr, char *buff) \
  202. { \
  203. struct batadv_priv *bat_priv = batadv_vlan_kobj_to_batpriv(kobj);\
  204. struct batadv_softif_vlan *vlan = batadv_kobj_to_vlan(bat_priv, \
  205. kobj); \
  206. size_t res = sprintf(buff, "%s\n", \
  207. atomic_read(&vlan->_name) == 0 ? \
  208. "disabled" : "enabled"); \
  209. \
  210. batadv_softif_vlan_put(vlan); \
  211. return res; \
  212. }
  213. /* Use this, if you are going to turn a [name] in the vlan struct on or off */
  214. #define BATADV_ATTR_VLAN_BOOL(_name, _mode, _post_func) \
  215. static BATADV_ATTR_VLAN_STORE_BOOL(_name, _post_func) \
  216. static BATADV_ATTR_VLAN_SHOW_BOOL(_name) \
  217. static BATADV_ATTR_VLAN(_name, _mode, batadv_show_vlan_##_name, \
  218. batadv_store_vlan_##_name)
  219. #define BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, _max, _post_func) \
  220. ssize_t batadv_store_##_name(struct kobject *kobj, \
  221. struct attribute *attr, char *buff, \
  222. size_t count) \
  223. { \
  224. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  225. struct batadv_hard_iface *hard_iface; \
  226. ssize_t length; \
  227. \
  228. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  229. if (!hard_iface) \
  230. return 0; \
  231. \
  232. length = __batadv_store_uint_attr(buff, count, _min, _max, \
  233. _post_func, attr, \
  234. &hard_iface->_var, net_dev); \
  235. \
  236. batadv_hardif_put(hard_iface); \
  237. return length; \
  238. }
  239. #define BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
  240. ssize_t batadv_show_##_name(struct kobject *kobj, \
  241. struct attribute *attr, char *buff) \
  242. { \
  243. struct net_device *net_dev = batadv_kobj_to_netdev(kobj); \
  244. struct batadv_hard_iface *hard_iface; \
  245. ssize_t length; \
  246. \
  247. hard_iface = batadv_hardif_get_by_netdev(net_dev); \
  248. if (!hard_iface) \
  249. return 0; \
  250. \
  251. length = sprintf(buff, "%i\n", atomic_read(&hard_iface->_var)); \
  252. \
  253. batadv_hardif_put(hard_iface); \
  254. return length; \
  255. }
  256. /* Use this, if you are going to set [name] in hard_iface to an
  257. * unsigned integer value
  258. */
  259. #define BATADV_ATTR_HIF_UINT(_name, _var, _mode, _min, _max, _post_func)\
  260. static BATADV_ATTR_HIF_STORE_UINT(_name, _var, _min, \
  261. _max, _post_func) \
  262. static BATADV_ATTR_HIF_SHOW_UINT(_name, _var) \
  263. static BATADV_ATTR(_name, _mode, batadv_show_##_name, \
  264. batadv_store_##_name)
  265. static int batadv_store_bool_attr(char *buff, size_t count,
  266. struct net_device *net_dev,
  267. const char *attr_name, atomic_t *attr,
  268. bool *changed)
  269. {
  270. int enabled = -1;
  271. *changed = false;
  272. if (buff[count - 1] == '\n')
  273. buff[count - 1] = '\0';
  274. if ((strncmp(buff, "1", 2) == 0) ||
  275. (strncmp(buff, "enable", 7) == 0) ||
  276. (strncmp(buff, "enabled", 8) == 0))
  277. enabled = 1;
  278. if ((strncmp(buff, "0", 2) == 0) ||
  279. (strncmp(buff, "disable", 8) == 0) ||
  280. (strncmp(buff, "disabled", 9) == 0))
  281. enabled = 0;
  282. if (enabled < 0) {
  283. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  284. attr_name, buff);
  285. return -EINVAL;
  286. }
  287. if (atomic_read(attr) == enabled)
  288. return count;
  289. batadv_info(net_dev, "%s: Changing from: %s to: %s\n", attr_name,
  290. atomic_read(attr) == 1 ? "enabled" : "disabled",
  291. enabled == 1 ? "enabled" : "disabled");
  292. *changed = true;
  293. atomic_set(attr, (unsigned int)enabled);
  294. return count;
  295. }
  296. static inline ssize_t
  297. __batadv_store_bool_attr(char *buff, size_t count,
  298. void (*post_func)(struct net_device *),
  299. struct attribute *attr,
  300. atomic_t *attr_store, struct net_device *net_dev)
  301. {
  302. bool changed;
  303. int ret;
  304. ret = batadv_store_bool_attr(buff, count, net_dev, attr->name,
  305. attr_store, &changed);
  306. if (post_func && changed)
  307. post_func(net_dev);
  308. return ret;
  309. }
  310. static int batadv_store_uint_attr(const char *buff, size_t count,
  311. struct net_device *net_dev,
  312. const char *attr_name,
  313. unsigned int min, unsigned int max,
  314. atomic_t *attr)
  315. {
  316. unsigned long uint_val;
  317. int ret;
  318. ret = kstrtoul(buff, 10, &uint_val);
  319. if (ret) {
  320. batadv_info(net_dev, "%s: Invalid parameter received: %s\n",
  321. attr_name, buff);
  322. return -EINVAL;
  323. }
  324. if (uint_val < min) {
  325. batadv_info(net_dev, "%s: Value is too small: %lu min: %u\n",
  326. attr_name, uint_val, min);
  327. return -EINVAL;
  328. }
  329. if (uint_val > max) {
  330. batadv_info(net_dev, "%s: Value is too big: %lu max: %u\n",
  331. attr_name, uint_val, max);
  332. return -EINVAL;
  333. }
  334. if (atomic_read(attr) == uint_val)
  335. return count;
  336. batadv_info(net_dev, "%s: Changing from: %i to: %lu\n",
  337. attr_name, atomic_read(attr), uint_val);
  338. atomic_set(attr, uint_val);
  339. return count;
  340. }
  341. static ssize_t __batadv_store_uint_attr(const char *buff, size_t count,
  342. int min, int max,
  343. void (*post_func)(struct net_device *),
  344. const struct attribute *attr,
  345. atomic_t *attr_store,
  346. struct net_device *net_dev)
  347. {
  348. int ret;
  349. ret = batadv_store_uint_attr(buff, count, net_dev, attr->name, min, max,
  350. attr_store);
  351. if (post_func && ret)
  352. post_func(net_dev);
  353. return ret;
  354. }
  355. static ssize_t batadv_show_bat_algo(struct kobject *kobj,
  356. struct attribute *attr, char *buff)
  357. {
  358. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  359. return sprintf(buff, "%s\n", bat_priv->algo_ops->name);
  360. }
  361. static void batadv_post_gw_reselect(struct net_device *net_dev)
  362. {
  363. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  364. batadv_gw_reselect(bat_priv);
  365. }
  366. static ssize_t batadv_show_gw_mode(struct kobject *kobj, struct attribute *attr,
  367. char *buff)
  368. {
  369. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  370. int bytes_written;
  371. /* GW mode is not available if the routing algorithm in use does not
  372. * implement the GW API
  373. */
  374. if (!bat_priv->algo_ops->gw.get_best_gw_node ||
  375. !bat_priv->algo_ops->gw.is_eligible)
  376. return -ENOENT;
  377. switch (atomic_read(&bat_priv->gw.mode)) {
  378. case BATADV_GW_MODE_CLIENT:
  379. bytes_written = sprintf(buff, "%s\n",
  380. BATADV_GW_MODE_CLIENT_NAME);
  381. break;
  382. case BATADV_GW_MODE_SERVER:
  383. bytes_written = sprintf(buff, "%s\n",
  384. BATADV_GW_MODE_SERVER_NAME);
  385. break;
  386. default:
  387. bytes_written = sprintf(buff, "%s\n",
  388. BATADV_GW_MODE_OFF_NAME);
  389. break;
  390. }
  391. return bytes_written;
  392. }
  393. static ssize_t batadv_store_gw_mode(struct kobject *kobj,
  394. struct attribute *attr, char *buff,
  395. size_t count)
  396. {
  397. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  398. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  399. char *curr_gw_mode_str;
  400. int gw_mode_tmp = -1;
  401. /* toggling GW mode is allowed only if the routing algorithm in use
  402. * provides the GW API
  403. */
  404. if (!bat_priv->algo_ops->gw.get_best_gw_node ||
  405. !bat_priv->algo_ops->gw.is_eligible)
  406. return -EINVAL;
  407. if (buff[count - 1] == '\n')
  408. buff[count - 1] = '\0';
  409. if (strncmp(buff, BATADV_GW_MODE_OFF_NAME,
  410. strlen(BATADV_GW_MODE_OFF_NAME)) == 0)
  411. gw_mode_tmp = BATADV_GW_MODE_OFF;
  412. if (strncmp(buff, BATADV_GW_MODE_CLIENT_NAME,
  413. strlen(BATADV_GW_MODE_CLIENT_NAME)) == 0)
  414. gw_mode_tmp = BATADV_GW_MODE_CLIENT;
  415. if (strncmp(buff, BATADV_GW_MODE_SERVER_NAME,
  416. strlen(BATADV_GW_MODE_SERVER_NAME)) == 0)
  417. gw_mode_tmp = BATADV_GW_MODE_SERVER;
  418. if (gw_mode_tmp < 0) {
  419. batadv_info(net_dev,
  420. "Invalid parameter for 'gw mode' setting received: %s\n",
  421. buff);
  422. return -EINVAL;
  423. }
  424. if (atomic_read(&bat_priv->gw.mode) == gw_mode_tmp)
  425. return count;
  426. switch (atomic_read(&bat_priv->gw.mode)) {
  427. case BATADV_GW_MODE_CLIENT:
  428. curr_gw_mode_str = BATADV_GW_MODE_CLIENT_NAME;
  429. break;
  430. case BATADV_GW_MODE_SERVER:
  431. curr_gw_mode_str = BATADV_GW_MODE_SERVER_NAME;
  432. break;
  433. default:
  434. curr_gw_mode_str = BATADV_GW_MODE_OFF_NAME;
  435. break;
  436. }
  437. batadv_info(net_dev, "Changing gw mode from: %s to: %s\n",
  438. curr_gw_mode_str, buff);
  439. /* Invoking batadv_gw_reselect() is not enough to really de-select the
  440. * current GW. It will only instruct the gateway client code to perform
  441. * a re-election the next time that this is needed.
  442. *
  443. * When gw client mode is being switched off the current GW must be
  444. * de-selected explicitly otherwise no GW_ADD uevent is thrown on
  445. * client mode re-activation. This is operation is performed in
  446. * batadv_gw_check_client_stop().
  447. */
  448. batadv_gw_reselect(bat_priv);
  449. /* always call batadv_gw_check_client_stop() before changing the gateway
  450. * state
  451. */
  452. batadv_gw_check_client_stop(bat_priv);
  453. atomic_set(&bat_priv->gw.mode, (unsigned int)gw_mode_tmp);
  454. batadv_gw_tvlv_container_update(bat_priv);
  455. return count;
  456. }
  457. static ssize_t batadv_show_gw_sel_class(struct kobject *kobj,
  458. struct attribute *attr, char *buff)
  459. {
  460. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  461. /* GW selection class is not available if the routing algorithm in use
  462. * does not implement the GW API
  463. */
  464. if (!bat_priv->algo_ops->gw.get_best_gw_node ||
  465. !bat_priv->algo_ops->gw.is_eligible)
  466. return -ENOENT;
  467. if (bat_priv->algo_ops->gw.show_sel_class)
  468. return bat_priv->algo_ops->gw.show_sel_class(bat_priv, buff);
  469. return sprintf(buff, "%i\n", atomic_read(&bat_priv->gw.sel_class));
  470. }
  471. static ssize_t batadv_store_gw_sel_class(struct kobject *kobj,
  472. struct attribute *attr, char *buff,
  473. size_t count)
  474. {
  475. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  476. /* setting the GW selection class is allowed only if the routing
  477. * algorithm in use implements the GW API
  478. */
  479. if (!bat_priv->algo_ops->gw.get_best_gw_node ||
  480. !bat_priv->algo_ops->gw.is_eligible)
  481. return -EINVAL;
  482. if (buff[count - 1] == '\n')
  483. buff[count - 1] = '\0';
  484. if (bat_priv->algo_ops->gw.store_sel_class)
  485. return bat_priv->algo_ops->gw.store_sel_class(bat_priv, buff,
  486. count);
  487. return __batadv_store_uint_attr(buff, count, 1, BATADV_TQ_MAX_VALUE,
  488. batadv_post_gw_reselect, attr,
  489. &bat_priv->gw.sel_class,
  490. bat_priv->soft_iface);
  491. }
  492. static ssize_t batadv_show_gw_bwidth(struct kobject *kobj,
  493. struct attribute *attr, char *buff)
  494. {
  495. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  496. u32 down, up;
  497. down = atomic_read(&bat_priv->gw.bandwidth_down);
  498. up = atomic_read(&bat_priv->gw.bandwidth_up);
  499. return sprintf(buff, "%u.%u/%u.%u MBit\n", down / 10,
  500. down % 10, up / 10, up % 10);
  501. }
  502. static ssize_t batadv_store_gw_bwidth(struct kobject *kobj,
  503. struct attribute *attr, char *buff,
  504. size_t count)
  505. {
  506. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  507. if (buff[count - 1] == '\n')
  508. buff[count - 1] = '\0';
  509. return batadv_gw_bandwidth_set(net_dev, buff, count);
  510. }
  511. /**
  512. * batadv_show_isolation_mark - print the current isolation mark/mask
  513. * @kobj: kobject representing the private mesh sysfs directory
  514. * @attr: the batman-adv attribute the user is interacting with
  515. * @buff: the buffer that will contain the data to send back to the user
  516. *
  517. * Return: the number of bytes written into 'buff' on success or a negative
  518. * error code in case of failure
  519. */
  520. static ssize_t batadv_show_isolation_mark(struct kobject *kobj,
  521. struct attribute *attr, char *buff)
  522. {
  523. struct batadv_priv *bat_priv = batadv_kobj_to_batpriv(kobj);
  524. return sprintf(buff, "%#.8x/%#.8x\n", bat_priv->isolation_mark,
  525. bat_priv->isolation_mark_mask);
  526. }
  527. /**
  528. * batadv_store_isolation_mark - parse and store the isolation mark/mask entered
  529. * by the user
  530. * @kobj: kobject representing the private mesh sysfs directory
  531. * @attr: the batman-adv attribute the user is interacting with
  532. * @buff: the buffer containing the user data
  533. * @count: number of bytes in the buffer
  534. *
  535. * Return: 'count' on success or a negative error code in case of failure
  536. */
  537. static ssize_t batadv_store_isolation_mark(struct kobject *kobj,
  538. struct attribute *attr, char *buff,
  539. size_t count)
  540. {
  541. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  542. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  543. u32 mark, mask;
  544. char *mask_ptr;
  545. /* parse the mask if it has been specified, otherwise assume the mask is
  546. * the biggest possible
  547. */
  548. mask = 0xFFFFFFFF;
  549. mask_ptr = strchr(buff, '/');
  550. if (mask_ptr) {
  551. *mask_ptr = '\0';
  552. mask_ptr++;
  553. /* the mask must be entered in hex base as it is going to be a
  554. * bitmask and not a prefix length
  555. */
  556. if (kstrtou32(mask_ptr, 16, &mask) < 0)
  557. return -EINVAL;
  558. }
  559. /* the mark can be entered in any base */
  560. if (kstrtou32(buff, 0, &mark) < 0)
  561. return -EINVAL;
  562. bat_priv->isolation_mark_mask = mask;
  563. /* erase bits not covered by the mask */
  564. bat_priv->isolation_mark = mark & bat_priv->isolation_mark_mask;
  565. batadv_info(net_dev,
  566. "New skb mark for extended isolation: %#.8x/%#.8x\n",
  567. bat_priv->isolation_mark, bat_priv->isolation_mark_mask);
  568. return count;
  569. }
  570. BATADV_ATTR_SIF_BOOL(aggregated_ogms, 0644, NULL);
  571. BATADV_ATTR_SIF_BOOL(bonding, 0644, NULL);
  572. #ifdef CONFIG_BATMAN_ADV_BLA
  573. BATADV_ATTR_SIF_BOOL(bridge_loop_avoidance, 0644, batadv_bla_status_update);
  574. #endif
  575. #ifdef CONFIG_BATMAN_ADV_DAT
  576. BATADV_ATTR_SIF_BOOL(distributed_arp_table, 0644, batadv_dat_status_update);
  577. #endif
  578. BATADV_ATTR_SIF_BOOL(fragmentation, 0644, batadv_update_min_mtu);
  579. static BATADV_ATTR(routing_algo, 0444, batadv_show_bat_algo, NULL);
  580. static BATADV_ATTR(gw_mode, 0644, batadv_show_gw_mode, batadv_store_gw_mode);
  581. BATADV_ATTR_SIF_UINT(orig_interval, orig_interval, 0644, 2 * BATADV_JITTER,
  582. INT_MAX, NULL);
  583. BATADV_ATTR_SIF_UINT(hop_penalty, hop_penalty, 0644, 0, BATADV_TQ_MAX_VALUE,
  584. NULL);
  585. static BATADV_ATTR(gw_sel_class, 0644, batadv_show_gw_sel_class,
  586. batadv_store_gw_sel_class);
  587. static BATADV_ATTR(gw_bandwidth, 0644, batadv_show_gw_bwidth,
  588. batadv_store_gw_bwidth);
  589. #ifdef CONFIG_BATMAN_ADV_MCAST
  590. BATADV_ATTR_SIF_BOOL(multicast_mode, 0644, NULL);
  591. #endif
  592. #ifdef CONFIG_BATMAN_ADV_DEBUG
  593. BATADV_ATTR_SIF_UINT(log_level, log_level, 0644, 0, BATADV_DBG_ALL, NULL);
  594. #endif
  595. #ifdef CONFIG_BATMAN_ADV_NC
  596. BATADV_ATTR_SIF_BOOL(network_coding, 0644, batadv_nc_status_update);
  597. #endif
  598. static BATADV_ATTR(isolation_mark, 0644, batadv_show_isolation_mark,
  599. batadv_store_isolation_mark);
  600. static struct batadv_attribute *batadv_mesh_attrs[] = {
  601. &batadv_attr_aggregated_ogms,
  602. &batadv_attr_bonding,
  603. #ifdef CONFIG_BATMAN_ADV_BLA
  604. &batadv_attr_bridge_loop_avoidance,
  605. #endif
  606. #ifdef CONFIG_BATMAN_ADV_DAT
  607. &batadv_attr_distributed_arp_table,
  608. #endif
  609. #ifdef CONFIG_BATMAN_ADV_MCAST
  610. &batadv_attr_multicast_mode,
  611. #endif
  612. &batadv_attr_fragmentation,
  613. &batadv_attr_routing_algo,
  614. &batadv_attr_gw_mode,
  615. &batadv_attr_orig_interval,
  616. &batadv_attr_hop_penalty,
  617. &batadv_attr_gw_sel_class,
  618. &batadv_attr_gw_bandwidth,
  619. #ifdef CONFIG_BATMAN_ADV_DEBUG
  620. &batadv_attr_log_level,
  621. #endif
  622. #ifdef CONFIG_BATMAN_ADV_NC
  623. &batadv_attr_network_coding,
  624. #endif
  625. &batadv_attr_isolation_mark,
  626. NULL,
  627. };
  628. BATADV_ATTR_VLAN_BOOL(ap_isolation, 0644, NULL);
  629. /* array of vlan specific sysfs attributes */
  630. static struct batadv_attribute *batadv_vlan_attrs[] = {
  631. &batadv_attr_vlan_ap_isolation,
  632. NULL,
  633. };
  634. int batadv_sysfs_add_meshif(struct net_device *dev)
  635. {
  636. struct kobject *batif_kobject = &dev->dev.kobj;
  637. struct batadv_priv *bat_priv = netdev_priv(dev);
  638. struct batadv_attribute **bat_attr;
  639. int err;
  640. bat_priv->mesh_obj = kobject_create_and_add(BATADV_SYSFS_IF_MESH_SUBDIR,
  641. batif_kobject);
  642. if (!bat_priv->mesh_obj) {
  643. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  644. BATADV_SYSFS_IF_MESH_SUBDIR);
  645. goto out;
  646. }
  647. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr) {
  648. err = sysfs_create_file(bat_priv->mesh_obj,
  649. &((*bat_attr)->attr));
  650. if (err) {
  651. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  652. dev->name, BATADV_SYSFS_IF_MESH_SUBDIR,
  653. ((*bat_attr)->attr).name);
  654. goto rem_attr;
  655. }
  656. }
  657. return 0;
  658. rem_attr:
  659. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  660. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  661. kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
  662. kobject_del(bat_priv->mesh_obj);
  663. kobject_put(bat_priv->mesh_obj);
  664. bat_priv->mesh_obj = NULL;
  665. out:
  666. return -ENOMEM;
  667. }
  668. void batadv_sysfs_del_meshif(struct net_device *dev)
  669. {
  670. struct batadv_priv *bat_priv = netdev_priv(dev);
  671. struct batadv_attribute **bat_attr;
  672. for (bat_attr = batadv_mesh_attrs; *bat_attr; ++bat_attr)
  673. sysfs_remove_file(bat_priv->mesh_obj, &((*bat_attr)->attr));
  674. kobject_uevent(bat_priv->mesh_obj, KOBJ_REMOVE);
  675. kobject_del(bat_priv->mesh_obj);
  676. kobject_put(bat_priv->mesh_obj);
  677. bat_priv->mesh_obj = NULL;
  678. }
  679. /**
  680. * batadv_sysfs_add_vlan - add all the needed sysfs objects for the new vlan
  681. * @dev: netdev of the mesh interface
  682. * @vlan: private data of the newly added VLAN interface
  683. *
  684. * Return: 0 on success and -ENOMEM if any of the structure allocations fails.
  685. */
  686. int batadv_sysfs_add_vlan(struct net_device *dev,
  687. struct batadv_softif_vlan *vlan)
  688. {
  689. char vlan_subdir[sizeof(BATADV_SYSFS_VLAN_SUBDIR_PREFIX) + 5];
  690. struct batadv_priv *bat_priv = netdev_priv(dev);
  691. struct batadv_attribute **bat_attr;
  692. int err;
  693. if (vlan->vid & BATADV_VLAN_HAS_TAG) {
  694. sprintf(vlan_subdir, BATADV_SYSFS_VLAN_SUBDIR_PREFIX "%hu",
  695. vlan->vid & VLAN_VID_MASK);
  696. vlan->kobj = kobject_create_and_add(vlan_subdir,
  697. bat_priv->mesh_obj);
  698. if (!vlan->kobj) {
  699. batadv_err(dev, "Can't add sysfs directory: %s/%s\n",
  700. dev->name, vlan_subdir);
  701. goto out;
  702. }
  703. } else {
  704. /* the untagged LAN uses the root folder to store its "VLAN
  705. * specific attributes"
  706. */
  707. vlan->kobj = bat_priv->mesh_obj;
  708. kobject_get(bat_priv->mesh_obj);
  709. }
  710. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr) {
  711. err = sysfs_create_file(vlan->kobj,
  712. &((*bat_attr)->attr));
  713. if (err) {
  714. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  715. dev->name, vlan_subdir,
  716. ((*bat_attr)->attr).name);
  717. goto rem_attr;
  718. }
  719. }
  720. return 0;
  721. rem_attr:
  722. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
  723. sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
  724. if (vlan->kobj != bat_priv->mesh_obj) {
  725. kobject_uevent(vlan->kobj, KOBJ_REMOVE);
  726. kobject_del(vlan->kobj);
  727. }
  728. kobject_put(vlan->kobj);
  729. vlan->kobj = NULL;
  730. out:
  731. return -ENOMEM;
  732. }
  733. /**
  734. * batadv_sysfs_del_vlan - remove all the sysfs objects for a given VLAN
  735. * @bat_priv: the bat priv with all the soft interface information
  736. * @vlan: the private data of the VLAN to destroy
  737. */
  738. void batadv_sysfs_del_vlan(struct batadv_priv *bat_priv,
  739. struct batadv_softif_vlan *vlan)
  740. {
  741. struct batadv_attribute **bat_attr;
  742. for (bat_attr = batadv_vlan_attrs; *bat_attr; ++bat_attr)
  743. sysfs_remove_file(vlan->kobj, &((*bat_attr)->attr));
  744. if (vlan->kobj != bat_priv->mesh_obj) {
  745. kobject_uevent(vlan->kobj, KOBJ_REMOVE);
  746. kobject_del(vlan->kobj);
  747. }
  748. kobject_put(vlan->kobj);
  749. vlan->kobj = NULL;
  750. }
  751. static ssize_t batadv_show_mesh_iface(struct kobject *kobj,
  752. struct attribute *attr, char *buff)
  753. {
  754. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  755. struct batadv_hard_iface *hard_iface;
  756. ssize_t length;
  757. const char *ifname;
  758. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  759. if (!hard_iface)
  760. return 0;
  761. if (hard_iface->if_status == BATADV_IF_NOT_IN_USE)
  762. ifname = "none";
  763. else
  764. ifname = hard_iface->soft_iface->name;
  765. length = sprintf(buff, "%s\n", ifname);
  766. batadv_hardif_put(hard_iface);
  767. return length;
  768. }
  769. /**
  770. * batadv_store_mesh_iface_finish - store new hardif mesh_iface state
  771. * @net_dev: netdevice to add/remove to/from batman-adv soft-interface
  772. * @ifname: name of soft-interface to modify
  773. *
  774. * Changes the parts of the hard+soft interface which can not be modified under
  775. * sysfs lock (to prevent deadlock situations).
  776. *
  777. * Return: 0 on success, 0 < on failure
  778. */
  779. static int batadv_store_mesh_iface_finish(struct net_device *net_dev,
  780. char ifname[IFNAMSIZ])
  781. {
  782. struct net *net = dev_net(net_dev);
  783. struct batadv_hard_iface *hard_iface;
  784. int status_tmp;
  785. int ret = 0;
  786. ASSERT_RTNL();
  787. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  788. if (!hard_iface)
  789. return 0;
  790. if (strncmp(ifname, "none", 4) == 0)
  791. status_tmp = BATADV_IF_NOT_IN_USE;
  792. else
  793. status_tmp = BATADV_IF_I_WANT_YOU;
  794. if (hard_iface->if_status == status_tmp)
  795. goto out;
  796. if ((hard_iface->soft_iface) &&
  797. (strncmp(hard_iface->soft_iface->name, ifname, IFNAMSIZ) == 0))
  798. goto out;
  799. if (status_tmp == BATADV_IF_NOT_IN_USE) {
  800. batadv_hardif_disable_interface(hard_iface,
  801. BATADV_IF_CLEANUP_AUTO);
  802. goto out;
  803. }
  804. /* if the interface already is in use */
  805. if (hard_iface->if_status != BATADV_IF_NOT_IN_USE)
  806. batadv_hardif_disable_interface(hard_iface,
  807. BATADV_IF_CLEANUP_AUTO);
  808. ret = batadv_hardif_enable_interface(hard_iface, net, ifname);
  809. out:
  810. batadv_hardif_put(hard_iface);
  811. return ret;
  812. }
  813. /**
  814. * batadv_store_mesh_iface_work - store new hardif mesh_iface state
  815. * @work: work queue item
  816. *
  817. * Changes the parts of the hard+soft interface which can not be modified under
  818. * sysfs lock (to prevent deadlock situations).
  819. */
  820. static void batadv_store_mesh_iface_work(struct work_struct *work)
  821. {
  822. struct batadv_store_mesh_work *store_work;
  823. int ret;
  824. store_work = container_of(work, struct batadv_store_mesh_work, work);
  825. rtnl_lock();
  826. ret = batadv_store_mesh_iface_finish(store_work->net_dev,
  827. store_work->soft_iface_name);
  828. rtnl_unlock();
  829. if (ret < 0)
  830. pr_err("Failed to store new mesh_iface state %s for %s: %d\n",
  831. store_work->soft_iface_name, store_work->net_dev->name,
  832. ret);
  833. dev_put(store_work->net_dev);
  834. kfree(store_work);
  835. }
  836. static ssize_t batadv_store_mesh_iface(struct kobject *kobj,
  837. struct attribute *attr, char *buff,
  838. size_t count)
  839. {
  840. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  841. struct batadv_store_mesh_work *store_work;
  842. if (buff[count - 1] == '\n')
  843. buff[count - 1] = '\0';
  844. if (strlen(buff) >= IFNAMSIZ) {
  845. pr_err("Invalid parameter for 'mesh_iface' setting received: interface name too long '%s'\n",
  846. buff);
  847. return -EINVAL;
  848. }
  849. store_work = kmalloc(sizeof(*store_work), GFP_KERNEL);
  850. if (!store_work)
  851. return -ENOMEM;
  852. dev_hold(net_dev);
  853. INIT_WORK(&store_work->work, batadv_store_mesh_iface_work);
  854. store_work->net_dev = net_dev;
  855. strlcpy(store_work->soft_iface_name, buff,
  856. sizeof(store_work->soft_iface_name));
  857. queue_work(batadv_event_workqueue, &store_work->work);
  858. return count;
  859. }
  860. static ssize_t batadv_show_iface_status(struct kobject *kobj,
  861. struct attribute *attr, char *buff)
  862. {
  863. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  864. struct batadv_hard_iface *hard_iface;
  865. ssize_t length;
  866. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  867. if (!hard_iface)
  868. return 0;
  869. switch (hard_iface->if_status) {
  870. case BATADV_IF_TO_BE_REMOVED:
  871. length = sprintf(buff, "disabling\n");
  872. break;
  873. case BATADV_IF_INACTIVE:
  874. length = sprintf(buff, "inactive\n");
  875. break;
  876. case BATADV_IF_ACTIVE:
  877. length = sprintf(buff, "active\n");
  878. break;
  879. case BATADV_IF_TO_BE_ACTIVATED:
  880. length = sprintf(buff, "enabling\n");
  881. break;
  882. case BATADV_IF_NOT_IN_USE:
  883. default:
  884. length = sprintf(buff, "not in use\n");
  885. break;
  886. }
  887. batadv_hardif_put(hard_iface);
  888. return length;
  889. }
  890. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  891. /**
  892. * batadv_store_throughput_override - parse and store throughput override
  893. * entered by the user
  894. * @kobj: kobject representing the private mesh sysfs directory
  895. * @attr: the batman-adv attribute the user is interacting with
  896. * @buff: the buffer containing the user data
  897. * @count: number of bytes in the buffer
  898. *
  899. * Return: 'count' on success or a negative error code in case of failure
  900. */
  901. static ssize_t batadv_store_throughput_override(struct kobject *kobj,
  902. struct attribute *attr,
  903. char *buff, size_t count)
  904. {
  905. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  906. struct batadv_hard_iface *hard_iface;
  907. u32 tp_override;
  908. u32 old_tp_override;
  909. bool ret;
  910. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  911. if (!hard_iface)
  912. return -EINVAL;
  913. if (buff[count - 1] == '\n')
  914. buff[count - 1] = '\0';
  915. ret = batadv_parse_throughput(net_dev, buff, "throughput_override",
  916. &tp_override);
  917. if (!ret)
  918. return count;
  919. old_tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
  920. if (old_tp_override == tp_override)
  921. goto out;
  922. batadv_info(net_dev, "%s: Changing from: %u.%u MBit to: %u.%u MBit\n",
  923. "throughput_override",
  924. old_tp_override / 10, old_tp_override % 10,
  925. tp_override / 10, tp_override % 10);
  926. atomic_set(&hard_iface->bat_v.throughput_override, tp_override);
  927. out:
  928. batadv_hardif_put(hard_iface);
  929. return count;
  930. }
  931. static ssize_t batadv_show_throughput_override(struct kobject *kobj,
  932. struct attribute *attr,
  933. char *buff)
  934. {
  935. struct net_device *net_dev = batadv_kobj_to_netdev(kobj);
  936. struct batadv_hard_iface *hard_iface;
  937. u32 tp_override;
  938. hard_iface = batadv_hardif_get_by_netdev(net_dev);
  939. if (!hard_iface)
  940. return -EINVAL;
  941. tp_override = atomic_read(&hard_iface->bat_v.throughput_override);
  942. return sprintf(buff, "%u.%u MBit\n", tp_override / 10,
  943. tp_override % 10);
  944. }
  945. #endif
  946. static BATADV_ATTR(mesh_iface, 0644, batadv_show_mesh_iface,
  947. batadv_store_mesh_iface);
  948. static BATADV_ATTR(iface_status, 0444, batadv_show_iface_status, NULL);
  949. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  950. BATADV_ATTR_HIF_UINT(elp_interval, bat_v.elp_interval, 0644,
  951. 2 * BATADV_JITTER, INT_MAX, NULL);
  952. static BATADV_ATTR(throughput_override, 0644, batadv_show_throughput_override,
  953. batadv_store_throughput_override);
  954. #endif
  955. static struct batadv_attribute *batadv_batman_attrs[] = {
  956. &batadv_attr_mesh_iface,
  957. &batadv_attr_iface_status,
  958. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  959. &batadv_attr_elp_interval,
  960. &batadv_attr_throughput_override,
  961. #endif
  962. NULL,
  963. };
  964. int batadv_sysfs_add_hardif(struct kobject **hardif_obj, struct net_device *dev)
  965. {
  966. struct kobject *hardif_kobject = &dev->dev.kobj;
  967. struct batadv_attribute **bat_attr;
  968. int err;
  969. *hardif_obj = kobject_create_and_add(BATADV_SYSFS_IF_BAT_SUBDIR,
  970. hardif_kobject);
  971. if (!*hardif_obj) {
  972. batadv_err(dev, "Can't add sysfs directory: %s/%s\n", dev->name,
  973. BATADV_SYSFS_IF_BAT_SUBDIR);
  974. goto out;
  975. }
  976. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr) {
  977. err = sysfs_create_file(*hardif_obj, &((*bat_attr)->attr));
  978. if (err) {
  979. batadv_err(dev, "Can't add sysfs file: %s/%s/%s\n",
  980. dev->name, BATADV_SYSFS_IF_BAT_SUBDIR,
  981. ((*bat_attr)->attr).name);
  982. goto rem_attr;
  983. }
  984. }
  985. return 0;
  986. rem_attr:
  987. for (bat_attr = batadv_batman_attrs; *bat_attr; ++bat_attr)
  988. sysfs_remove_file(*hardif_obj, &((*bat_attr)->attr));
  989. out:
  990. return -ENOMEM;
  991. }
  992. void batadv_sysfs_del_hardif(struct kobject **hardif_obj)
  993. {
  994. kobject_uevent(*hardif_obj, KOBJ_REMOVE);
  995. kobject_del(*hardif_obj);
  996. kobject_put(*hardif_obj);
  997. *hardif_obj = NULL;
  998. }
  999. int batadv_throw_uevent(struct batadv_priv *bat_priv, enum batadv_uev_type type,
  1000. enum batadv_uev_action action, const char *data)
  1001. {
  1002. int ret = -ENOMEM;
  1003. struct kobject *bat_kobj;
  1004. char *uevent_env[4] = { NULL, NULL, NULL, NULL };
  1005. bat_kobj = &bat_priv->soft_iface->dev.kobj;
  1006. uevent_env[0] = kasprintf(GFP_ATOMIC,
  1007. "%s%s", BATADV_UEV_TYPE_VAR,
  1008. batadv_uev_type_str[type]);
  1009. if (!uevent_env[0])
  1010. goto out;
  1011. uevent_env[1] = kasprintf(GFP_ATOMIC,
  1012. "%s%s", BATADV_UEV_ACTION_VAR,
  1013. batadv_uev_action_str[action]);
  1014. if (!uevent_env[1])
  1015. goto out;
  1016. /* If the event is DEL, ignore the data field */
  1017. if (action != BATADV_UEV_DEL) {
  1018. uevent_env[2] = kasprintf(GFP_ATOMIC,
  1019. "%s%s", BATADV_UEV_DATA_VAR, data);
  1020. if (!uevent_env[2])
  1021. goto out;
  1022. }
  1023. ret = kobject_uevent_env(bat_kobj, KOBJ_CHANGE, uevent_env);
  1024. out:
  1025. kfree(uevent_env[0]);
  1026. kfree(uevent_env[1]);
  1027. kfree(uevent_env[2]);
  1028. if (ret)
  1029. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1030. "Impossible to send uevent for (%s,%s,%s) event (err: %d)\n",
  1031. batadv_uev_type_str[type],
  1032. batadv_uev_action_str[action],
  1033. (action == BATADV_UEV_DEL ? "NULL" : data), ret);
  1034. return ret;
  1035. }