br_sysfs_br.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /*
  2. * Sysfs attributes of bridge
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Stephen Hemminger <shemminger@osdl.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version
  11. * 2 of the License, or (at your option) any later version.
  12. */
  13. #include <linux/capability.h>
  14. #include <linux/kernel.h>
  15. #include <linux/netdevice.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/if_bridge.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/times.h>
  21. #include "br_private.h"
  22. #define to_bridge(cd) ((struct net_bridge *)netdev_priv(to_net_dev(cd)))
  23. /*
  24. * Common code for storing bridge parameters.
  25. */
  26. static ssize_t store_bridge_parm(struct device *d,
  27. const char *buf, size_t len,
  28. int (*set)(struct net_bridge *, unsigned long))
  29. {
  30. struct net_bridge *br = to_bridge(d);
  31. char *endp;
  32. unsigned long val;
  33. int err;
  34. if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
  35. return -EPERM;
  36. val = simple_strtoul(buf, &endp, 0);
  37. if (endp == buf)
  38. return -EINVAL;
  39. if (!rtnl_trylock())
  40. return restart_syscall();
  41. err = (*set)(br, val);
  42. if (!err)
  43. netdev_state_change(br->dev);
  44. rtnl_unlock();
  45. return err ? err : len;
  46. }
  47. static ssize_t forward_delay_show(struct device *d,
  48. struct device_attribute *attr, char *buf)
  49. {
  50. struct net_bridge *br = to_bridge(d);
  51. return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->forward_delay));
  52. }
  53. static ssize_t forward_delay_store(struct device *d,
  54. struct device_attribute *attr,
  55. const char *buf, size_t len)
  56. {
  57. return store_bridge_parm(d, buf, len, br_set_forward_delay);
  58. }
  59. static DEVICE_ATTR_RW(forward_delay);
  60. static ssize_t hello_time_show(struct device *d, struct device_attribute *attr,
  61. char *buf)
  62. {
  63. return sprintf(buf, "%lu\n",
  64. jiffies_to_clock_t(to_bridge(d)->hello_time));
  65. }
  66. static ssize_t hello_time_store(struct device *d,
  67. struct device_attribute *attr, const char *buf,
  68. size_t len)
  69. {
  70. return store_bridge_parm(d, buf, len, br_set_hello_time);
  71. }
  72. static DEVICE_ATTR_RW(hello_time);
  73. static ssize_t max_age_show(struct device *d, struct device_attribute *attr,
  74. char *buf)
  75. {
  76. return sprintf(buf, "%lu\n",
  77. jiffies_to_clock_t(to_bridge(d)->max_age));
  78. }
  79. static ssize_t max_age_store(struct device *d, struct device_attribute *attr,
  80. const char *buf, size_t len)
  81. {
  82. return store_bridge_parm(d, buf, len, br_set_max_age);
  83. }
  84. static DEVICE_ATTR_RW(max_age);
  85. static ssize_t ageing_time_show(struct device *d,
  86. struct device_attribute *attr, char *buf)
  87. {
  88. struct net_bridge *br = to_bridge(d);
  89. return sprintf(buf, "%lu\n", jiffies_to_clock_t(br->ageing_time));
  90. }
  91. static int set_ageing_time(struct net_bridge *br, unsigned long val)
  92. {
  93. return br_set_ageing_time(br, val);
  94. }
  95. static ssize_t ageing_time_store(struct device *d,
  96. struct device_attribute *attr,
  97. const char *buf, size_t len)
  98. {
  99. return store_bridge_parm(d, buf, len, set_ageing_time);
  100. }
  101. static DEVICE_ATTR_RW(ageing_time);
  102. static ssize_t stp_state_show(struct device *d,
  103. struct device_attribute *attr, char *buf)
  104. {
  105. struct net_bridge *br = to_bridge(d);
  106. return sprintf(buf, "%d\n", br->stp_enabled);
  107. }
  108. static int set_stp_state(struct net_bridge *br, unsigned long val)
  109. {
  110. br_stp_set_enabled(br, val);
  111. return 0;
  112. }
  113. static ssize_t stp_state_store(struct device *d,
  114. struct device_attribute *attr, const char *buf,
  115. size_t len)
  116. {
  117. return store_bridge_parm(d, buf, len, set_stp_state);
  118. }
  119. static DEVICE_ATTR_RW(stp_state);
  120. static ssize_t group_fwd_mask_show(struct device *d,
  121. struct device_attribute *attr,
  122. char *buf)
  123. {
  124. struct net_bridge *br = to_bridge(d);
  125. return sprintf(buf, "%#x\n", br->group_fwd_mask);
  126. }
  127. static int set_group_fwd_mask(struct net_bridge *br, unsigned long val)
  128. {
  129. if (val & BR_GROUPFWD_RESTRICTED)
  130. return -EINVAL;
  131. br->group_fwd_mask = val;
  132. return 0;
  133. }
  134. static ssize_t group_fwd_mask_store(struct device *d,
  135. struct device_attribute *attr,
  136. const char *buf,
  137. size_t len)
  138. {
  139. return store_bridge_parm(d, buf, len, set_group_fwd_mask);
  140. }
  141. static DEVICE_ATTR_RW(group_fwd_mask);
  142. static ssize_t priority_show(struct device *d, struct device_attribute *attr,
  143. char *buf)
  144. {
  145. struct net_bridge *br = to_bridge(d);
  146. return sprintf(buf, "%d\n",
  147. (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1]);
  148. }
  149. static int set_priority(struct net_bridge *br, unsigned long val)
  150. {
  151. br_stp_set_bridge_priority(br, (u16) val);
  152. return 0;
  153. }
  154. static ssize_t priority_store(struct device *d, struct device_attribute *attr,
  155. const char *buf, size_t len)
  156. {
  157. return store_bridge_parm(d, buf, len, set_priority);
  158. }
  159. static DEVICE_ATTR_RW(priority);
  160. static ssize_t root_id_show(struct device *d, struct device_attribute *attr,
  161. char *buf)
  162. {
  163. return br_show_bridge_id(buf, &to_bridge(d)->designated_root);
  164. }
  165. static DEVICE_ATTR_RO(root_id);
  166. static ssize_t bridge_id_show(struct device *d, struct device_attribute *attr,
  167. char *buf)
  168. {
  169. return br_show_bridge_id(buf, &to_bridge(d)->bridge_id);
  170. }
  171. static DEVICE_ATTR_RO(bridge_id);
  172. static ssize_t root_port_show(struct device *d, struct device_attribute *attr,
  173. char *buf)
  174. {
  175. return sprintf(buf, "%d\n", to_bridge(d)->root_port);
  176. }
  177. static DEVICE_ATTR_RO(root_port);
  178. static ssize_t root_path_cost_show(struct device *d,
  179. struct device_attribute *attr, char *buf)
  180. {
  181. return sprintf(buf, "%d\n", to_bridge(d)->root_path_cost);
  182. }
  183. static DEVICE_ATTR_RO(root_path_cost);
  184. static ssize_t topology_change_show(struct device *d,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. return sprintf(buf, "%d\n", to_bridge(d)->topology_change);
  188. }
  189. static DEVICE_ATTR_RO(topology_change);
  190. static ssize_t topology_change_detected_show(struct device *d,
  191. struct device_attribute *attr,
  192. char *buf)
  193. {
  194. struct net_bridge *br = to_bridge(d);
  195. return sprintf(buf, "%d\n", br->topology_change_detected);
  196. }
  197. static DEVICE_ATTR_RO(topology_change_detected);
  198. static ssize_t hello_timer_show(struct device *d,
  199. struct device_attribute *attr, char *buf)
  200. {
  201. struct net_bridge *br = to_bridge(d);
  202. return sprintf(buf, "%ld\n", br_timer_value(&br->hello_timer));
  203. }
  204. static DEVICE_ATTR_RO(hello_timer);
  205. static ssize_t tcn_timer_show(struct device *d, struct device_attribute *attr,
  206. char *buf)
  207. {
  208. struct net_bridge *br = to_bridge(d);
  209. return sprintf(buf, "%ld\n", br_timer_value(&br->tcn_timer));
  210. }
  211. static DEVICE_ATTR_RO(tcn_timer);
  212. static ssize_t topology_change_timer_show(struct device *d,
  213. struct device_attribute *attr,
  214. char *buf)
  215. {
  216. struct net_bridge *br = to_bridge(d);
  217. return sprintf(buf, "%ld\n", br_timer_value(&br->topology_change_timer));
  218. }
  219. static DEVICE_ATTR_RO(topology_change_timer);
  220. static ssize_t gc_timer_show(struct device *d, struct device_attribute *attr,
  221. char *buf)
  222. {
  223. struct net_bridge *br = to_bridge(d);
  224. return sprintf(buf, "%ld\n", br_timer_value(&br->gc_timer));
  225. }
  226. static DEVICE_ATTR_RO(gc_timer);
  227. static ssize_t group_addr_show(struct device *d,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct net_bridge *br = to_bridge(d);
  231. return sprintf(buf, "%x:%x:%x:%x:%x:%x\n",
  232. br->group_addr[0], br->group_addr[1],
  233. br->group_addr[2], br->group_addr[3],
  234. br->group_addr[4], br->group_addr[5]);
  235. }
  236. static ssize_t group_addr_store(struct device *d,
  237. struct device_attribute *attr,
  238. const char *buf, size_t len)
  239. {
  240. struct net_bridge *br = to_bridge(d);
  241. u8 new_addr[6];
  242. int i;
  243. if (!ns_capable(dev_net(br->dev)->user_ns, CAP_NET_ADMIN))
  244. return -EPERM;
  245. if (sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
  246. &new_addr[0], &new_addr[1], &new_addr[2],
  247. &new_addr[3], &new_addr[4], &new_addr[5]) != 6)
  248. return -EINVAL;
  249. if (!is_link_local_ether_addr(new_addr))
  250. return -EINVAL;
  251. if (new_addr[5] == 1 || /* 802.3x Pause address */
  252. new_addr[5] == 2 || /* 802.3ad Slow protocols */
  253. new_addr[5] == 3) /* 802.1X PAE address */
  254. return -EINVAL;
  255. if (!rtnl_trylock())
  256. return restart_syscall();
  257. spin_lock_bh(&br->lock);
  258. for (i = 0; i < 6; i++)
  259. br->group_addr[i] = new_addr[i];
  260. spin_unlock_bh(&br->lock);
  261. br->group_addr_set = true;
  262. br_recalculate_fwd_mask(br);
  263. netdev_state_change(br->dev);
  264. rtnl_unlock();
  265. return len;
  266. }
  267. static DEVICE_ATTR_RW(group_addr);
  268. static int set_flush(struct net_bridge *br, unsigned long val)
  269. {
  270. br_fdb_flush(br);
  271. return 0;
  272. }
  273. static ssize_t flush_store(struct device *d,
  274. struct device_attribute *attr,
  275. const char *buf, size_t len)
  276. {
  277. return store_bridge_parm(d, buf, len, set_flush);
  278. }
  279. static DEVICE_ATTR_WO(flush);
  280. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  281. static ssize_t multicast_router_show(struct device *d,
  282. struct device_attribute *attr, char *buf)
  283. {
  284. struct net_bridge *br = to_bridge(d);
  285. return sprintf(buf, "%d\n", br->multicast_router);
  286. }
  287. static ssize_t multicast_router_store(struct device *d,
  288. struct device_attribute *attr,
  289. const char *buf, size_t len)
  290. {
  291. return store_bridge_parm(d, buf, len, br_multicast_set_router);
  292. }
  293. static DEVICE_ATTR_RW(multicast_router);
  294. static ssize_t multicast_snooping_show(struct device *d,
  295. struct device_attribute *attr,
  296. char *buf)
  297. {
  298. struct net_bridge *br = to_bridge(d);
  299. return sprintf(buf, "%d\n", !br->multicast_disabled);
  300. }
  301. static ssize_t multicast_snooping_store(struct device *d,
  302. struct device_attribute *attr,
  303. const char *buf, size_t len)
  304. {
  305. return store_bridge_parm(d, buf, len, br_multicast_toggle);
  306. }
  307. static DEVICE_ATTR_RW(multicast_snooping);
  308. static ssize_t multicast_query_use_ifaddr_show(struct device *d,
  309. struct device_attribute *attr,
  310. char *buf)
  311. {
  312. struct net_bridge *br = to_bridge(d);
  313. return sprintf(buf, "%d\n", br->multicast_query_use_ifaddr);
  314. }
  315. static int set_query_use_ifaddr(struct net_bridge *br, unsigned long val)
  316. {
  317. br->multicast_query_use_ifaddr = !!val;
  318. return 0;
  319. }
  320. static ssize_t
  321. multicast_query_use_ifaddr_store(struct device *d,
  322. struct device_attribute *attr,
  323. const char *buf, size_t len)
  324. {
  325. return store_bridge_parm(d, buf, len, set_query_use_ifaddr);
  326. }
  327. static DEVICE_ATTR_RW(multicast_query_use_ifaddr);
  328. static ssize_t multicast_querier_show(struct device *d,
  329. struct device_attribute *attr,
  330. char *buf)
  331. {
  332. struct net_bridge *br = to_bridge(d);
  333. return sprintf(buf, "%d\n", br->multicast_querier);
  334. }
  335. static ssize_t multicast_querier_store(struct device *d,
  336. struct device_attribute *attr,
  337. const char *buf, size_t len)
  338. {
  339. return store_bridge_parm(d, buf, len, br_multicast_set_querier);
  340. }
  341. static DEVICE_ATTR_RW(multicast_querier);
  342. static ssize_t hash_elasticity_show(struct device *d,
  343. struct device_attribute *attr, char *buf)
  344. {
  345. struct net_bridge *br = to_bridge(d);
  346. return sprintf(buf, "%u\n", br->hash_elasticity);
  347. }
  348. static int set_elasticity(struct net_bridge *br, unsigned long val)
  349. {
  350. br->hash_elasticity = val;
  351. return 0;
  352. }
  353. static ssize_t hash_elasticity_store(struct device *d,
  354. struct device_attribute *attr,
  355. const char *buf, size_t len)
  356. {
  357. return store_bridge_parm(d, buf, len, set_elasticity);
  358. }
  359. static DEVICE_ATTR_RW(hash_elasticity);
  360. static ssize_t hash_max_show(struct device *d, struct device_attribute *attr,
  361. char *buf)
  362. {
  363. struct net_bridge *br = to_bridge(d);
  364. return sprintf(buf, "%u\n", br->hash_max);
  365. }
  366. static ssize_t hash_max_store(struct device *d, struct device_attribute *attr,
  367. const char *buf, size_t len)
  368. {
  369. return store_bridge_parm(d, buf, len, br_multicast_set_hash_max);
  370. }
  371. static DEVICE_ATTR_RW(hash_max);
  372. static ssize_t multicast_last_member_count_show(struct device *d,
  373. struct device_attribute *attr,
  374. char *buf)
  375. {
  376. struct net_bridge *br = to_bridge(d);
  377. return sprintf(buf, "%u\n", br->multicast_last_member_count);
  378. }
  379. static int set_last_member_count(struct net_bridge *br, unsigned long val)
  380. {
  381. br->multicast_last_member_count = val;
  382. return 0;
  383. }
  384. static ssize_t multicast_last_member_count_store(struct device *d,
  385. struct device_attribute *attr,
  386. const char *buf, size_t len)
  387. {
  388. return store_bridge_parm(d, buf, len, set_last_member_count);
  389. }
  390. static DEVICE_ATTR_RW(multicast_last_member_count);
  391. static ssize_t multicast_startup_query_count_show(
  392. struct device *d, struct device_attribute *attr, char *buf)
  393. {
  394. struct net_bridge *br = to_bridge(d);
  395. return sprintf(buf, "%u\n", br->multicast_startup_query_count);
  396. }
  397. static int set_startup_query_count(struct net_bridge *br, unsigned long val)
  398. {
  399. br->multicast_startup_query_count = val;
  400. return 0;
  401. }
  402. static ssize_t multicast_startup_query_count_store(
  403. struct device *d, struct device_attribute *attr, const char *buf,
  404. size_t len)
  405. {
  406. return store_bridge_parm(d, buf, len, set_startup_query_count);
  407. }
  408. static DEVICE_ATTR_RW(multicast_startup_query_count);
  409. static ssize_t multicast_last_member_interval_show(
  410. struct device *d, struct device_attribute *attr, char *buf)
  411. {
  412. struct net_bridge *br = to_bridge(d);
  413. return sprintf(buf, "%lu\n",
  414. jiffies_to_clock_t(br->multicast_last_member_interval));
  415. }
  416. static int set_last_member_interval(struct net_bridge *br, unsigned long val)
  417. {
  418. br->multicast_last_member_interval = clock_t_to_jiffies(val);
  419. return 0;
  420. }
  421. static ssize_t multicast_last_member_interval_store(
  422. struct device *d, struct device_attribute *attr, const char *buf,
  423. size_t len)
  424. {
  425. return store_bridge_parm(d, buf, len, set_last_member_interval);
  426. }
  427. static DEVICE_ATTR_RW(multicast_last_member_interval);
  428. static ssize_t multicast_membership_interval_show(
  429. struct device *d, struct device_attribute *attr, char *buf)
  430. {
  431. struct net_bridge *br = to_bridge(d);
  432. return sprintf(buf, "%lu\n",
  433. jiffies_to_clock_t(br->multicast_membership_interval));
  434. }
  435. static int set_membership_interval(struct net_bridge *br, unsigned long val)
  436. {
  437. br->multicast_membership_interval = clock_t_to_jiffies(val);
  438. return 0;
  439. }
  440. static ssize_t multicast_membership_interval_store(
  441. struct device *d, struct device_attribute *attr, const char *buf,
  442. size_t len)
  443. {
  444. return store_bridge_parm(d, buf, len, set_membership_interval);
  445. }
  446. static DEVICE_ATTR_RW(multicast_membership_interval);
  447. static ssize_t multicast_querier_interval_show(struct device *d,
  448. struct device_attribute *attr,
  449. char *buf)
  450. {
  451. struct net_bridge *br = to_bridge(d);
  452. return sprintf(buf, "%lu\n",
  453. jiffies_to_clock_t(br->multicast_querier_interval));
  454. }
  455. static int set_querier_interval(struct net_bridge *br, unsigned long val)
  456. {
  457. br->multicast_querier_interval = clock_t_to_jiffies(val);
  458. return 0;
  459. }
  460. static ssize_t multicast_querier_interval_store(struct device *d,
  461. struct device_attribute *attr,
  462. const char *buf, size_t len)
  463. {
  464. return store_bridge_parm(d, buf, len, set_querier_interval);
  465. }
  466. static DEVICE_ATTR_RW(multicast_querier_interval);
  467. static ssize_t multicast_query_interval_show(struct device *d,
  468. struct device_attribute *attr,
  469. char *buf)
  470. {
  471. struct net_bridge *br = to_bridge(d);
  472. return sprintf(buf, "%lu\n",
  473. jiffies_to_clock_t(br->multicast_query_interval));
  474. }
  475. static int set_query_interval(struct net_bridge *br, unsigned long val)
  476. {
  477. br->multicast_query_interval = clock_t_to_jiffies(val);
  478. return 0;
  479. }
  480. static ssize_t multicast_query_interval_store(struct device *d,
  481. struct device_attribute *attr,
  482. const char *buf, size_t len)
  483. {
  484. return store_bridge_parm(d, buf, len, set_query_interval);
  485. }
  486. static DEVICE_ATTR_RW(multicast_query_interval);
  487. static ssize_t multicast_query_response_interval_show(
  488. struct device *d, struct device_attribute *attr, char *buf)
  489. {
  490. struct net_bridge *br = to_bridge(d);
  491. return sprintf(
  492. buf, "%lu\n",
  493. jiffies_to_clock_t(br->multicast_query_response_interval));
  494. }
  495. static int set_query_response_interval(struct net_bridge *br, unsigned long val)
  496. {
  497. br->multicast_query_response_interval = clock_t_to_jiffies(val);
  498. return 0;
  499. }
  500. static ssize_t multicast_query_response_interval_store(
  501. struct device *d, struct device_attribute *attr, const char *buf,
  502. size_t len)
  503. {
  504. return store_bridge_parm(d, buf, len, set_query_response_interval);
  505. }
  506. static DEVICE_ATTR_RW(multicast_query_response_interval);
  507. static ssize_t multicast_startup_query_interval_show(
  508. struct device *d, struct device_attribute *attr, char *buf)
  509. {
  510. struct net_bridge *br = to_bridge(d);
  511. return sprintf(
  512. buf, "%lu\n",
  513. jiffies_to_clock_t(br->multicast_startup_query_interval));
  514. }
  515. static int set_startup_query_interval(struct net_bridge *br, unsigned long val)
  516. {
  517. br->multicast_startup_query_interval = clock_t_to_jiffies(val);
  518. return 0;
  519. }
  520. static ssize_t multicast_startup_query_interval_store(
  521. struct device *d, struct device_attribute *attr, const char *buf,
  522. size_t len)
  523. {
  524. return store_bridge_parm(d, buf, len, set_startup_query_interval);
  525. }
  526. static DEVICE_ATTR_RW(multicast_startup_query_interval);
  527. static ssize_t multicast_stats_enabled_show(struct device *d,
  528. struct device_attribute *attr,
  529. char *buf)
  530. {
  531. struct net_bridge *br = to_bridge(d);
  532. return sprintf(buf, "%u\n", br->multicast_stats_enabled);
  533. }
  534. static int set_stats_enabled(struct net_bridge *br, unsigned long val)
  535. {
  536. br->multicast_stats_enabled = !!val;
  537. return 0;
  538. }
  539. static ssize_t multicast_stats_enabled_store(struct device *d,
  540. struct device_attribute *attr,
  541. const char *buf,
  542. size_t len)
  543. {
  544. return store_bridge_parm(d, buf, len, set_stats_enabled);
  545. }
  546. static DEVICE_ATTR_RW(multicast_stats_enabled);
  547. #endif
  548. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  549. static ssize_t nf_call_iptables_show(
  550. struct device *d, struct device_attribute *attr, char *buf)
  551. {
  552. struct net_bridge *br = to_bridge(d);
  553. return sprintf(buf, "%u\n", br->nf_call_iptables);
  554. }
  555. static int set_nf_call_iptables(struct net_bridge *br, unsigned long val)
  556. {
  557. br->nf_call_iptables = val ? true : false;
  558. return 0;
  559. }
  560. static ssize_t nf_call_iptables_store(
  561. struct device *d, struct device_attribute *attr, const char *buf,
  562. size_t len)
  563. {
  564. return store_bridge_parm(d, buf, len, set_nf_call_iptables);
  565. }
  566. static DEVICE_ATTR_RW(nf_call_iptables);
  567. static ssize_t nf_call_ip6tables_show(
  568. struct device *d, struct device_attribute *attr, char *buf)
  569. {
  570. struct net_bridge *br = to_bridge(d);
  571. return sprintf(buf, "%u\n", br->nf_call_ip6tables);
  572. }
  573. static int set_nf_call_ip6tables(struct net_bridge *br, unsigned long val)
  574. {
  575. br->nf_call_ip6tables = val ? true : false;
  576. return 0;
  577. }
  578. static ssize_t nf_call_ip6tables_store(
  579. struct device *d, struct device_attribute *attr, const char *buf,
  580. size_t len)
  581. {
  582. return store_bridge_parm(d, buf, len, set_nf_call_ip6tables);
  583. }
  584. static DEVICE_ATTR_RW(nf_call_ip6tables);
  585. static ssize_t nf_call_arptables_show(
  586. struct device *d, struct device_attribute *attr, char *buf)
  587. {
  588. struct net_bridge *br = to_bridge(d);
  589. return sprintf(buf, "%u\n", br->nf_call_arptables);
  590. }
  591. static int set_nf_call_arptables(struct net_bridge *br, unsigned long val)
  592. {
  593. br->nf_call_arptables = val ? true : false;
  594. return 0;
  595. }
  596. static ssize_t nf_call_arptables_store(
  597. struct device *d, struct device_attribute *attr, const char *buf,
  598. size_t len)
  599. {
  600. return store_bridge_parm(d, buf, len, set_nf_call_arptables);
  601. }
  602. static DEVICE_ATTR_RW(nf_call_arptables);
  603. #endif
  604. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  605. static ssize_t vlan_filtering_show(struct device *d,
  606. struct device_attribute *attr,
  607. char *buf)
  608. {
  609. struct net_bridge *br = to_bridge(d);
  610. return sprintf(buf, "%d\n", br->vlan_enabled);
  611. }
  612. static ssize_t vlan_filtering_store(struct device *d,
  613. struct device_attribute *attr,
  614. const char *buf, size_t len)
  615. {
  616. return store_bridge_parm(d, buf, len, br_vlan_filter_toggle);
  617. }
  618. static DEVICE_ATTR_RW(vlan_filtering);
  619. static ssize_t vlan_protocol_show(struct device *d,
  620. struct device_attribute *attr,
  621. char *buf)
  622. {
  623. struct net_bridge *br = to_bridge(d);
  624. return sprintf(buf, "%#06x\n", ntohs(br->vlan_proto));
  625. }
  626. static ssize_t vlan_protocol_store(struct device *d,
  627. struct device_attribute *attr,
  628. const char *buf, size_t len)
  629. {
  630. return store_bridge_parm(d, buf, len, br_vlan_set_proto);
  631. }
  632. static DEVICE_ATTR_RW(vlan_protocol);
  633. static ssize_t default_pvid_show(struct device *d,
  634. struct device_attribute *attr,
  635. char *buf)
  636. {
  637. struct net_bridge *br = to_bridge(d);
  638. return sprintf(buf, "%d\n", br->default_pvid);
  639. }
  640. static ssize_t default_pvid_store(struct device *d,
  641. struct device_attribute *attr,
  642. const char *buf, size_t len)
  643. {
  644. return store_bridge_parm(d, buf, len, br_vlan_set_default_pvid);
  645. }
  646. static DEVICE_ATTR_RW(default_pvid);
  647. static ssize_t vlan_stats_enabled_show(struct device *d,
  648. struct device_attribute *attr,
  649. char *buf)
  650. {
  651. struct net_bridge *br = to_bridge(d);
  652. return sprintf(buf, "%u\n", br->vlan_stats_enabled);
  653. }
  654. static ssize_t vlan_stats_enabled_store(struct device *d,
  655. struct device_attribute *attr,
  656. const char *buf, size_t len)
  657. {
  658. return store_bridge_parm(d, buf, len, br_vlan_set_stats);
  659. }
  660. static DEVICE_ATTR_RW(vlan_stats_enabled);
  661. #endif
  662. static struct attribute *bridge_attrs[] = {
  663. &dev_attr_forward_delay.attr,
  664. &dev_attr_hello_time.attr,
  665. &dev_attr_max_age.attr,
  666. &dev_attr_ageing_time.attr,
  667. &dev_attr_stp_state.attr,
  668. &dev_attr_group_fwd_mask.attr,
  669. &dev_attr_priority.attr,
  670. &dev_attr_bridge_id.attr,
  671. &dev_attr_root_id.attr,
  672. &dev_attr_root_path_cost.attr,
  673. &dev_attr_root_port.attr,
  674. &dev_attr_topology_change.attr,
  675. &dev_attr_topology_change_detected.attr,
  676. &dev_attr_hello_timer.attr,
  677. &dev_attr_tcn_timer.attr,
  678. &dev_attr_topology_change_timer.attr,
  679. &dev_attr_gc_timer.attr,
  680. &dev_attr_group_addr.attr,
  681. &dev_attr_flush.attr,
  682. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  683. &dev_attr_multicast_router.attr,
  684. &dev_attr_multicast_snooping.attr,
  685. &dev_attr_multicast_querier.attr,
  686. &dev_attr_multicast_query_use_ifaddr.attr,
  687. &dev_attr_hash_elasticity.attr,
  688. &dev_attr_hash_max.attr,
  689. &dev_attr_multicast_last_member_count.attr,
  690. &dev_attr_multicast_startup_query_count.attr,
  691. &dev_attr_multicast_last_member_interval.attr,
  692. &dev_attr_multicast_membership_interval.attr,
  693. &dev_attr_multicast_querier_interval.attr,
  694. &dev_attr_multicast_query_interval.attr,
  695. &dev_attr_multicast_query_response_interval.attr,
  696. &dev_attr_multicast_startup_query_interval.attr,
  697. &dev_attr_multicast_stats_enabled.attr,
  698. #endif
  699. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  700. &dev_attr_nf_call_iptables.attr,
  701. &dev_attr_nf_call_ip6tables.attr,
  702. &dev_attr_nf_call_arptables.attr,
  703. #endif
  704. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  705. &dev_attr_vlan_filtering.attr,
  706. &dev_attr_vlan_protocol.attr,
  707. &dev_attr_default_pvid.attr,
  708. &dev_attr_vlan_stats_enabled.attr,
  709. #endif
  710. NULL
  711. };
  712. static struct attribute_group bridge_group = {
  713. .name = SYSFS_BRIDGE_ATTR,
  714. .attrs = bridge_attrs,
  715. };
  716. /*
  717. * Export the forwarding information table as a binary file
  718. * The records are struct __fdb_entry.
  719. *
  720. * Returns the number of bytes read.
  721. */
  722. static ssize_t brforward_read(struct file *filp, struct kobject *kobj,
  723. struct bin_attribute *bin_attr,
  724. char *buf, loff_t off, size_t count)
  725. {
  726. struct device *dev = kobj_to_dev(kobj);
  727. struct net_bridge *br = to_bridge(dev);
  728. int n;
  729. /* must read whole records */
  730. if (off % sizeof(struct __fdb_entry) != 0)
  731. return -EINVAL;
  732. n = br_fdb_fillbuf(br, buf,
  733. count / sizeof(struct __fdb_entry),
  734. off / sizeof(struct __fdb_entry));
  735. if (n > 0)
  736. n *= sizeof(struct __fdb_entry);
  737. return n;
  738. }
  739. static struct bin_attribute bridge_forward = {
  740. .attr = { .name = SYSFS_BRIDGE_FDB,
  741. .mode = S_IRUGO, },
  742. .read = brforward_read,
  743. };
  744. /*
  745. * Add entries in sysfs onto the existing network class device
  746. * for the bridge.
  747. * Adds a attribute group "bridge" containing tuning parameters.
  748. * Binary attribute containing the forward table
  749. * Sub directory to hold links to interfaces.
  750. *
  751. * Note: the ifobj exists only to be a subdirectory
  752. * to hold links. The ifobj exists in same data structure
  753. * as it's parent the bridge so reference counting works.
  754. */
  755. int br_sysfs_addbr(struct net_device *dev)
  756. {
  757. struct kobject *brobj = &dev->dev.kobj;
  758. struct net_bridge *br = netdev_priv(dev);
  759. int err;
  760. err = sysfs_create_group(brobj, &bridge_group);
  761. if (err) {
  762. pr_info("%s: can't create group %s/%s\n",
  763. __func__, dev->name, bridge_group.name);
  764. goto out1;
  765. }
  766. err = sysfs_create_bin_file(brobj, &bridge_forward);
  767. if (err) {
  768. pr_info("%s: can't create attribute file %s/%s\n",
  769. __func__, dev->name, bridge_forward.attr.name);
  770. goto out2;
  771. }
  772. br->ifobj = kobject_create_and_add(SYSFS_BRIDGE_PORT_SUBDIR, brobj);
  773. if (!br->ifobj) {
  774. pr_info("%s: can't add kobject (directory) %s/%s\n",
  775. __func__, dev->name, SYSFS_BRIDGE_PORT_SUBDIR);
  776. err = -ENOMEM;
  777. goto out3;
  778. }
  779. return 0;
  780. out3:
  781. sysfs_remove_bin_file(&dev->dev.kobj, &bridge_forward);
  782. out2:
  783. sysfs_remove_group(&dev->dev.kobj, &bridge_group);
  784. out1:
  785. return err;
  786. }
  787. void br_sysfs_delbr(struct net_device *dev)
  788. {
  789. struct kobject *kobj = &dev->dev.kobj;
  790. struct net_bridge *br = netdev_priv(dev);
  791. kobject_put(br->ifobj);
  792. sysfs_remove_bin_file(kobj, &bridge_forward);
  793. sysfs_remove_group(kobj, &bridge_group);
  794. }