br_sysfs_br.c 25 KB

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