sysfs.c 35 KB

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