br_netlink.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. /*
  2. * Bridge netlink control interface
  3. *
  4. * Authors:
  5. * Stephen Hemminger <shemminger@osdl.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/net_namespace.h>
  17. #include <net/sock.h>
  18. #include <uapi/linux/if_bridge.h>
  19. #include "br_private.h"
  20. #include "br_private_stp.h"
  21. static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  22. u32 filter_mask)
  23. {
  24. struct net_bridge_vlan *v;
  25. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  26. u16 flags, pvid;
  27. int num_vlans = 0;
  28. if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  29. return 0;
  30. pvid = br_get_pvid(vg);
  31. /* Count number of vlan infos */
  32. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  33. flags = 0;
  34. /* only a context, bridge vlan not activated */
  35. if (!br_vlan_should_use(v))
  36. continue;
  37. if (v->vid == pvid)
  38. flags |= BRIDGE_VLAN_INFO_PVID;
  39. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  40. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  41. if (vid_range_start == 0) {
  42. goto initvars;
  43. } else if ((v->vid - vid_range_end) == 1 &&
  44. flags == vid_range_flags) {
  45. vid_range_end = v->vid;
  46. continue;
  47. } else {
  48. if ((vid_range_end - vid_range_start) > 0)
  49. num_vlans += 2;
  50. else
  51. num_vlans += 1;
  52. }
  53. initvars:
  54. vid_range_start = v->vid;
  55. vid_range_end = v->vid;
  56. vid_range_flags = flags;
  57. }
  58. if (vid_range_start != 0) {
  59. if ((vid_range_end - vid_range_start) > 0)
  60. num_vlans += 2;
  61. else
  62. num_vlans += 1;
  63. }
  64. return num_vlans;
  65. }
  66. static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  67. u32 filter_mask)
  68. {
  69. int num_vlans;
  70. if (!vg)
  71. return 0;
  72. if (filter_mask & RTEXT_FILTER_BRVLAN)
  73. return vg->num_vlans;
  74. rcu_read_lock();
  75. num_vlans = __get_num_vlan_infos(vg, filter_mask);
  76. rcu_read_unlock();
  77. return num_vlans;
  78. }
  79. static size_t br_get_link_af_size_filtered(const struct net_device *dev,
  80. u32 filter_mask)
  81. {
  82. struct net_bridge_vlan_group *vg = NULL;
  83. struct net_bridge_port *p;
  84. struct net_bridge *br;
  85. int num_vlan_infos;
  86. rcu_read_lock();
  87. if (br_port_exists(dev)) {
  88. p = br_port_get_rcu(dev);
  89. vg = nbp_vlan_group_rcu(p);
  90. } else if (dev->priv_flags & IFF_EBRIDGE) {
  91. br = netdev_priv(dev);
  92. vg = br_vlan_group_rcu(br);
  93. }
  94. num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
  95. rcu_read_unlock();
  96. /* Each VLAN is returned in bridge_vlan_info along with flags */
  97. return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
  98. }
  99. static inline size_t br_port_info_size(void)
  100. {
  101. return nla_total_size(1) /* IFLA_BRPORT_STATE */
  102. + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
  103. + nla_total_size(4) /* IFLA_BRPORT_COST */
  104. + nla_total_size(1) /* IFLA_BRPORT_MODE */
  105. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  106. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  107. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  108. + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
  109. + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
  110. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
  111. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
  112. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
  113. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
  114. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
  115. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
  116. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
  117. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
  118. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
  119. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
  120. + nla_total_size(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
  121. + nla_total_size(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
  122. + nla_total_size(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
  123. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  124. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
  125. #endif
  126. + 0;
  127. }
  128. static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
  129. {
  130. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  131. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  132. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  133. + nla_total_size(4) /* IFLA_MASTER */
  134. + nla_total_size(4) /* IFLA_MTU */
  135. + nla_total_size(4) /* IFLA_LINK */
  136. + nla_total_size(1) /* IFLA_OPERSTATE */
  137. + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
  138. + nla_total_size(br_get_link_af_size_filtered(dev,
  139. filter_mask)); /* IFLA_AF_SPEC */
  140. }
  141. static int br_port_fill_attrs(struct sk_buff *skb,
  142. const struct net_bridge_port *p)
  143. {
  144. u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
  145. u64 timerval;
  146. if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
  147. nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
  148. nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
  149. nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
  150. nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
  151. nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
  152. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
  153. nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
  154. nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
  155. nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
  156. nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
  157. !!(p->flags & BR_PROXYARP_WIFI)) ||
  158. nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
  159. &p->designated_root) ||
  160. nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  161. &p->designated_bridge) ||
  162. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
  163. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
  164. nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
  165. nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
  166. nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
  167. p->topology_change_ack) ||
  168. nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending))
  169. return -EMSGSIZE;
  170. timerval = br_timer_value(&p->message_age_timer);
  171. if (nla_put_u64(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval))
  172. return -EMSGSIZE;
  173. timerval = br_timer_value(&p->forward_delay_timer);
  174. if (nla_put_u64(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval))
  175. return -EMSGSIZE;
  176. timerval = br_timer_value(&p->hold_timer);
  177. if (nla_put_u64(skb, IFLA_BRPORT_HOLD_TIMER, timerval))
  178. return -EMSGSIZE;
  179. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  180. if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
  181. p->multicast_router))
  182. return -EMSGSIZE;
  183. #endif
  184. return 0;
  185. }
  186. static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
  187. u16 vid_end, u16 flags)
  188. {
  189. struct bridge_vlan_info vinfo;
  190. if ((vid_end - vid_start) > 0) {
  191. /* add range to skb */
  192. vinfo.vid = vid_start;
  193. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
  194. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  195. sizeof(vinfo), &vinfo))
  196. goto nla_put_failure;
  197. vinfo.vid = vid_end;
  198. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
  199. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  200. sizeof(vinfo), &vinfo))
  201. goto nla_put_failure;
  202. } else {
  203. vinfo.vid = vid_start;
  204. vinfo.flags = flags;
  205. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  206. sizeof(vinfo), &vinfo))
  207. goto nla_put_failure;
  208. }
  209. return 0;
  210. nla_put_failure:
  211. return -EMSGSIZE;
  212. }
  213. static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
  214. struct net_bridge_vlan_group *vg)
  215. {
  216. struct net_bridge_vlan *v;
  217. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  218. u16 flags, pvid;
  219. int err = 0;
  220. /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
  221. * and mark vlan info with begin and end flags
  222. * if vlaninfo represents a range
  223. */
  224. pvid = br_get_pvid(vg);
  225. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  226. flags = 0;
  227. if (!br_vlan_should_use(v))
  228. continue;
  229. if (v->vid == pvid)
  230. flags |= BRIDGE_VLAN_INFO_PVID;
  231. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  232. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  233. if (vid_range_start == 0) {
  234. goto initvars;
  235. } else if ((v->vid - vid_range_end) == 1 &&
  236. flags == vid_range_flags) {
  237. vid_range_end = v->vid;
  238. continue;
  239. } else {
  240. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  241. vid_range_end,
  242. vid_range_flags);
  243. if (err)
  244. return err;
  245. }
  246. initvars:
  247. vid_range_start = v->vid;
  248. vid_range_end = v->vid;
  249. vid_range_flags = flags;
  250. }
  251. if (vid_range_start != 0) {
  252. /* Call it once more to send any left over vlans */
  253. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  254. vid_range_end,
  255. vid_range_flags);
  256. if (err)
  257. return err;
  258. }
  259. return 0;
  260. }
  261. static int br_fill_ifvlaninfo(struct sk_buff *skb,
  262. struct net_bridge_vlan_group *vg)
  263. {
  264. struct bridge_vlan_info vinfo;
  265. struct net_bridge_vlan *v;
  266. u16 pvid;
  267. pvid = br_get_pvid(vg);
  268. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  269. if (!br_vlan_should_use(v))
  270. continue;
  271. vinfo.vid = v->vid;
  272. vinfo.flags = 0;
  273. if (v->vid == pvid)
  274. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  275. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  276. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  277. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  278. sizeof(vinfo), &vinfo))
  279. goto nla_put_failure;
  280. }
  281. return 0;
  282. nla_put_failure:
  283. return -EMSGSIZE;
  284. }
  285. /*
  286. * Create one netlink message for one interface
  287. * Contains port and master info as well as carrier and bridge state.
  288. */
  289. static int br_fill_ifinfo(struct sk_buff *skb,
  290. struct net_bridge_port *port,
  291. u32 pid, u32 seq, int event, unsigned int flags,
  292. u32 filter_mask, const struct net_device *dev)
  293. {
  294. struct net_bridge *br;
  295. struct ifinfomsg *hdr;
  296. struct nlmsghdr *nlh;
  297. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  298. if (port)
  299. br = port->br;
  300. else
  301. br = netdev_priv(dev);
  302. br_debug(br, "br_fill_info event %d port %s master %s\n",
  303. event, dev->name, br->dev->name);
  304. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  305. if (nlh == NULL)
  306. return -EMSGSIZE;
  307. hdr = nlmsg_data(nlh);
  308. hdr->ifi_family = AF_BRIDGE;
  309. hdr->__ifi_pad = 0;
  310. hdr->ifi_type = dev->type;
  311. hdr->ifi_index = dev->ifindex;
  312. hdr->ifi_flags = dev_get_flags(dev);
  313. hdr->ifi_change = 0;
  314. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  315. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  316. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  317. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  318. (dev->addr_len &&
  319. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  320. (dev->ifindex != dev_get_iflink(dev) &&
  321. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  322. goto nla_put_failure;
  323. if (event == RTM_NEWLINK && port) {
  324. struct nlattr *nest
  325. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  326. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  327. goto nla_put_failure;
  328. nla_nest_end(skb, nest);
  329. }
  330. /* Check if the VID information is requested */
  331. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  332. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  333. struct net_bridge_vlan_group *vg;
  334. struct nlattr *af;
  335. int err;
  336. /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
  337. rcu_read_lock();
  338. if (port)
  339. vg = nbp_vlan_group_rcu(port);
  340. else
  341. vg = br_vlan_group_rcu(br);
  342. if (!vg || !vg->num_vlans) {
  343. rcu_read_unlock();
  344. goto done;
  345. }
  346. af = nla_nest_start(skb, IFLA_AF_SPEC);
  347. if (!af) {
  348. rcu_read_unlock();
  349. goto nla_put_failure;
  350. }
  351. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  352. err = br_fill_ifvlaninfo_compressed(skb, vg);
  353. else
  354. err = br_fill_ifvlaninfo(skb, vg);
  355. rcu_read_unlock();
  356. if (err)
  357. goto nla_put_failure;
  358. nla_nest_end(skb, af);
  359. }
  360. done:
  361. nlmsg_end(skb, nlh);
  362. return 0;
  363. nla_put_failure:
  364. nlmsg_cancel(skb, nlh);
  365. return -EMSGSIZE;
  366. }
  367. /*
  368. * Notify listeners of a change in port information
  369. */
  370. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  371. {
  372. struct net *net;
  373. struct sk_buff *skb;
  374. int err = -ENOBUFS;
  375. u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
  376. if (!port)
  377. return;
  378. net = dev_net(port->dev);
  379. br_debug(port->br, "port %u(%s) event %d\n",
  380. (unsigned int)port->port_no, port->dev->name, event);
  381. skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
  382. if (skb == NULL)
  383. goto errout;
  384. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
  385. if (err < 0) {
  386. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  387. WARN_ON(err == -EMSGSIZE);
  388. kfree_skb(skb);
  389. goto errout;
  390. }
  391. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  392. return;
  393. errout:
  394. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  395. }
  396. /*
  397. * Dump information about all ports, in response to GETLINK
  398. */
  399. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  400. struct net_device *dev, u32 filter_mask, int nlflags)
  401. {
  402. struct net_bridge_port *port = br_port_get_rtnl(dev);
  403. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
  404. !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  405. return 0;
  406. return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
  407. filter_mask, dev);
  408. }
  409. static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
  410. int cmd, struct bridge_vlan_info *vinfo)
  411. {
  412. int err = 0;
  413. switch (cmd) {
  414. case RTM_SETLINK:
  415. if (p) {
  416. /* if the MASTER flag is set this will act on the global
  417. * per-VLAN entry as well
  418. */
  419. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
  420. if (err)
  421. break;
  422. } else {
  423. vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
  424. err = br_vlan_add(br, vinfo->vid, vinfo->flags);
  425. }
  426. break;
  427. case RTM_DELLINK:
  428. if (p) {
  429. nbp_vlan_delete(p, vinfo->vid);
  430. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  431. br_vlan_delete(p->br, vinfo->vid);
  432. } else {
  433. br_vlan_delete(br, vinfo->vid);
  434. }
  435. break;
  436. }
  437. return err;
  438. }
  439. static int br_afspec(struct net_bridge *br,
  440. struct net_bridge_port *p,
  441. struct nlattr *af_spec,
  442. int cmd)
  443. {
  444. struct bridge_vlan_info *vinfo_start = NULL;
  445. struct bridge_vlan_info *vinfo = NULL;
  446. struct nlattr *attr;
  447. int err = 0;
  448. int rem;
  449. nla_for_each_nested(attr, af_spec, rem) {
  450. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  451. continue;
  452. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  453. return -EINVAL;
  454. vinfo = nla_data(attr);
  455. if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
  456. return -EINVAL;
  457. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  458. if (vinfo_start)
  459. return -EINVAL;
  460. vinfo_start = vinfo;
  461. /* don't allow range of pvids */
  462. if (vinfo_start->flags & BRIDGE_VLAN_INFO_PVID)
  463. return -EINVAL;
  464. continue;
  465. }
  466. if (vinfo_start) {
  467. struct bridge_vlan_info tmp_vinfo;
  468. int v;
  469. if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
  470. return -EINVAL;
  471. if (vinfo->vid <= vinfo_start->vid)
  472. return -EINVAL;
  473. memcpy(&tmp_vinfo, vinfo_start,
  474. sizeof(struct bridge_vlan_info));
  475. for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
  476. tmp_vinfo.vid = v;
  477. err = br_vlan_info(br, p, cmd, &tmp_vinfo);
  478. if (err)
  479. break;
  480. }
  481. vinfo_start = NULL;
  482. } else {
  483. err = br_vlan_info(br, p, cmd, vinfo);
  484. }
  485. if (err)
  486. break;
  487. }
  488. return err;
  489. }
  490. static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
  491. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  492. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  493. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  494. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  495. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  496. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  497. [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
  498. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  499. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  500. [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
  501. [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
  502. [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
  503. };
  504. /* Change the state of the port and notify spanning tree */
  505. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  506. {
  507. if (state > BR_STATE_BLOCKING)
  508. return -EINVAL;
  509. /* if kernel STP is running, don't allow changes */
  510. if (p->br->stp_enabled == BR_KERNEL_STP)
  511. return -EBUSY;
  512. /* if device is not up, change is not allowed
  513. * if link is not present, only allowable state is disabled
  514. */
  515. if (!netif_running(p->dev) ||
  516. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  517. return -ENETDOWN;
  518. br_set_state(p, state);
  519. br_log_state(p);
  520. br_port_state_selection(p->br);
  521. return 0;
  522. }
  523. /* Set/clear or port flags based on attribute */
  524. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  525. int attrtype, unsigned long mask)
  526. {
  527. if (tb[attrtype]) {
  528. u8 flag = nla_get_u8(tb[attrtype]);
  529. if (flag)
  530. p->flags |= mask;
  531. else
  532. p->flags &= ~mask;
  533. }
  534. }
  535. /* Process bridge protocol info on port */
  536. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  537. {
  538. int err;
  539. unsigned long old_flags = p->flags;
  540. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  541. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  542. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  543. br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  544. br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
  545. br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
  546. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
  547. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
  548. if (tb[IFLA_BRPORT_COST]) {
  549. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  550. if (err)
  551. return err;
  552. }
  553. if (tb[IFLA_BRPORT_PRIORITY]) {
  554. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  555. if (err)
  556. return err;
  557. }
  558. if (tb[IFLA_BRPORT_STATE]) {
  559. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  560. if (err)
  561. return err;
  562. }
  563. if (tb[IFLA_BRPORT_FLUSH])
  564. br_fdb_delete_by_port(p->br, p, 0, 0);
  565. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  566. if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
  567. u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
  568. err = br_multicast_set_port_router(p, mcast_router);
  569. if (err)
  570. return err;
  571. }
  572. #endif
  573. br_port_flags_change(p, old_flags ^ p->flags);
  574. return 0;
  575. }
  576. /* Change state and parameters on port. */
  577. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  578. {
  579. struct nlattr *protinfo;
  580. struct nlattr *afspec;
  581. struct net_bridge_port *p;
  582. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  583. int err = 0;
  584. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
  585. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  586. if (!protinfo && !afspec)
  587. return 0;
  588. p = br_port_get_rtnl(dev);
  589. /* We want to accept dev as bridge itself if the AF_SPEC
  590. * is set to see if someone is setting vlan info on the bridge
  591. */
  592. if (!p && !afspec)
  593. return -EINVAL;
  594. if (p && protinfo) {
  595. if (protinfo->nla_type & NLA_F_NESTED) {
  596. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  597. protinfo, br_port_policy);
  598. if (err)
  599. return err;
  600. spin_lock_bh(&p->br->lock);
  601. err = br_setport(p, tb);
  602. spin_unlock_bh(&p->br->lock);
  603. } else {
  604. /* Binary compatibility with old RSTP */
  605. if (nla_len(protinfo) < sizeof(u8))
  606. return -EINVAL;
  607. spin_lock_bh(&p->br->lock);
  608. err = br_set_port_state(p, nla_get_u8(protinfo));
  609. spin_unlock_bh(&p->br->lock);
  610. }
  611. if (err)
  612. goto out;
  613. }
  614. if (afspec) {
  615. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  616. afspec, RTM_SETLINK);
  617. }
  618. if (err == 0)
  619. br_ifinfo_notify(RTM_NEWLINK, p);
  620. out:
  621. return err;
  622. }
  623. /* Delete port information */
  624. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  625. {
  626. struct nlattr *afspec;
  627. struct net_bridge_port *p;
  628. int err = 0;
  629. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  630. if (!afspec)
  631. return 0;
  632. p = br_port_get_rtnl(dev);
  633. /* We want to accept dev as bridge itself as well */
  634. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  635. return -EINVAL;
  636. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  637. afspec, RTM_DELLINK);
  638. if (err == 0)
  639. /* Send RTM_NEWLINK because userspace
  640. * expects RTM_NEWLINK for vlan dels
  641. */
  642. br_ifinfo_notify(RTM_NEWLINK, p);
  643. return err;
  644. }
  645. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  646. {
  647. if (tb[IFLA_ADDRESS]) {
  648. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  649. return -EINVAL;
  650. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  651. return -EADDRNOTAVAIL;
  652. }
  653. if (!data)
  654. return 0;
  655. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  656. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  657. switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
  658. case htons(ETH_P_8021Q):
  659. case htons(ETH_P_8021AD):
  660. break;
  661. default:
  662. return -EPROTONOSUPPORT;
  663. }
  664. }
  665. #endif
  666. return 0;
  667. }
  668. static int br_dev_newlink(struct net *src_net, struct net_device *dev,
  669. struct nlattr *tb[], struct nlattr *data[])
  670. {
  671. struct net_bridge *br = netdev_priv(dev);
  672. if (tb[IFLA_ADDRESS]) {
  673. spin_lock_bh(&br->lock);
  674. br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
  675. spin_unlock_bh(&br->lock);
  676. }
  677. return register_netdevice(dev);
  678. }
  679. static int br_port_slave_changelink(struct net_device *brdev,
  680. struct net_device *dev,
  681. struct nlattr *tb[],
  682. struct nlattr *data[])
  683. {
  684. struct net_bridge *br = netdev_priv(brdev);
  685. int ret;
  686. if (!data)
  687. return 0;
  688. spin_lock_bh(&br->lock);
  689. ret = br_setport(br_port_get_rtnl(dev), data);
  690. spin_unlock_bh(&br->lock);
  691. return ret;
  692. }
  693. static int br_port_fill_slave_info(struct sk_buff *skb,
  694. const struct net_device *brdev,
  695. const struct net_device *dev)
  696. {
  697. return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
  698. }
  699. static size_t br_port_get_slave_size(const struct net_device *brdev,
  700. const struct net_device *dev)
  701. {
  702. return br_port_info_size();
  703. }
  704. static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
  705. [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
  706. [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
  707. [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
  708. [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
  709. [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
  710. [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
  711. [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
  712. [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
  713. [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
  714. [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
  715. .len = ETH_ALEN },
  716. [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
  717. [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
  718. [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
  719. [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
  720. [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
  721. [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
  722. [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
  723. [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
  724. [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
  725. [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
  726. [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
  727. [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
  728. [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
  729. [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
  730. [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
  731. [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
  732. [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
  733. [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
  734. };
  735. static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
  736. struct nlattr *data[])
  737. {
  738. struct net_bridge *br = netdev_priv(brdev);
  739. int err;
  740. if (!data)
  741. return 0;
  742. if (data[IFLA_BR_FORWARD_DELAY]) {
  743. err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
  744. if (err)
  745. return err;
  746. }
  747. if (data[IFLA_BR_HELLO_TIME]) {
  748. err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
  749. if (err)
  750. return err;
  751. }
  752. if (data[IFLA_BR_MAX_AGE]) {
  753. err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
  754. if (err)
  755. return err;
  756. }
  757. if (data[IFLA_BR_AGEING_TIME]) {
  758. err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
  759. if (err)
  760. return err;
  761. }
  762. if (data[IFLA_BR_STP_STATE]) {
  763. u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
  764. br_stp_set_enabled(br, stp_enabled);
  765. }
  766. if (data[IFLA_BR_PRIORITY]) {
  767. u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
  768. br_stp_set_bridge_priority(br, priority);
  769. }
  770. if (data[IFLA_BR_VLAN_FILTERING]) {
  771. u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
  772. err = __br_vlan_filter_toggle(br, vlan_filter);
  773. if (err)
  774. return err;
  775. }
  776. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  777. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  778. __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
  779. err = __br_vlan_set_proto(br, vlan_proto);
  780. if (err)
  781. return err;
  782. }
  783. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  784. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  785. err = __br_vlan_set_default_pvid(br, defpvid);
  786. if (err)
  787. return err;
  788. }
  789. #endif
  790. if (data[IFLA_BR_GROUP_FWD_MASK]) {
  791. u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
  792. if (fwd_mask & BR_GROUPFWD_RESTRICTED)
  793. return -EINVAL;
  794. br->group_fwd_mask = fwd_mask;
  795. }
  796. if (data[IFLA_BR_GROUP_ADDR]) {
  797. u8 new_addr[ETH_ALEN];
  798. if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
  799. return -EINVAL;
  800. memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
  801. if (!is_link_local_ether_addr(new_addr))
  802. return -EINVAL;
  803. if (new_addr[5] == 1 || /* 802.3x Pause address */
  804. new_addr[5] == 2 || /* 802.3ad Slow protocols */
  805. new_addr[5] == 3) /* 802.1X PAE address */
  806. return -EINVAL;
  807. spin_lock_bh(&br->lock);
  808. memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
  809. spin_unlock_bh(&br->lock);
  810. br->group_addr_set = true;
  811. br_recalculate_fwd_mask(br);
  812. }
  813. if (data[IFLA_BR_FDB_FLUSH])
  814. br_fdb_flush(br);
  815. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  816. if (data[IFLA_BR_MCAST_ROUTER]) {
  817. u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
  818. err = br_multicast_set_router(br, multicast_router);
  819. if (err)
  820. return err;
  821. }
  822. if (data[IFLA_BR_MCAST_SNOOPING]) {
  823. u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
  824. err = br_multicast_toggle(br, mcast_snooping);
  825. if (err)
  826. return err;
  827. }
  828. if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
  829. u8 val;
  830. val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
  831. br->multicast_query_use_ifaddr = !!val;
  832. }
  833. if (data[IFLA_BR_MCAST_QUERIER]) {
  834. u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
  835. err = br_multicast_set_querier(br, mcast_querier);
  836. if (err)
  837. return err;
  838. }
  839. if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
  840. u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
  841. br->hash_elasticity = val;
  842. }
  843. if (data[IFLA_BR_MCAST_HASH_MAX]) {
  844. u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
  845. err = br_multicast_set_hash_max(br, hash_max);
  846. if (err)
  847. return err;
  848. }
  849. if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
  850. u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
  851. br->multicast_last_member_count = val;
  852. }
  853. if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
  854. u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
  855. br->multicast_startup_query_count = val;
  856. }
  857. if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
  858. u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
  859. br->multicast_last_member_interval = clock_t_to_jiffies(val);
  860. }
  861. if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
  862. u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
  863. br->multicast_membership_interval = clock_t_to_jiffies(val);
  864. }
  865. if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
  866. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
  867. br->multicast_querier_interval = clock_t_to_jiffies(val);
  868. }
  869. if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
  870. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
  871. br->multicast_query_interval = clock_t_to_jiffies(val);
  872. }
  873. if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
  874. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
  875. br->multicast_query_response_interval = clock_t_to_jiffies(val);
  876. }
  877. if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
  878. u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
  879. br->multicast_startup_query_interval = clock_t_to_jiffies(val);
  880. }
  881. #endif
  882. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  883. if (data[IFLA_BR_NF_CALL_IPTABLES]) {
  884. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
  885. br->nf_call_iptables = val ? true : false;
  886. }
  887. if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
  888. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
  889. br->nf_call_ip6tables = val ? true : false;
  890. }
  891. if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
  892. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
  893. br->nf_call_arptables = val ? true : false;
  894. }
  895. #endif
  896. return 0;
  897. }
  898. static size_t br_get_size(const struct net_device *brdev)
  899. {
  900. return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
  901. nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
  902. nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
  903. nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
  904. nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
  905. nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
  906. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
  907. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  908. nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
  909. nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
  910. #endif
  911. nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
  912. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
  913. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
  914. nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
  915. nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
  916. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
  917. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
  918. nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
  919. nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
  920. nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
  921. nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
  922. nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
  923. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  924. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
  925. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
  926. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
  927. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
  928. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
  929. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
  930. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
  931. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
  932. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
  933. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
  934. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
  935. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
  936. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
  937. nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
  938. #endif
  939. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  940. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
  941. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
  942. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
  943. #endif
  944. 0;
  945. }
  946. static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
  947. {
  948. struct net_bridge *br = netdev_priv(brdev);
  949. u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
  950. u32 hello_time = jiffies_to_clock_t(br->hello_time);
  951. u32 age_time = jiffies_to_clock_t(br->max_age);
  952. u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
  953. u32 stp_enabled = br->stp_enabled;
  954. u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
  955. u8 vlan_enabled = br_vlan_enabled(br);
  956. u64 clockval;
  957. clockval = br_timer_value(&br->hello_timer);
  958. if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
  959. return -EMSGSIZE;
  960. clockval = br_timer_value(&br->tcn_timer);
  961. if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
  962. return -EMSGSIZE;
  963. clockval = br_timer_value(&br->topology_change_timer);
  964. if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
  965. return -EMSGSIZE;
  966. clockval = br_timer_value(&br->gc_timer);
  967. if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
  968. return -EMSGSIZE;
  969. if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
  970. nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
  971. nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
  972. nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
  973. nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
  974. nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
  975. nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
  976. nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
  977. nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  978. &br->bridge_id) ||
  979. nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
  980. &br->designated_root) ||
  981. nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
  982. nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
  983. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
  984. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
  985. br->topology_change_detected) ||
  986. nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
  987. return -EMSGSIZE;
  988. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  989. if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
  990. nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
  991. return -EMSGSIZE;
  992. #endif
  993. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  994. if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
  995. nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
  996. nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
  997. br->multicast_query_use_ifaddr) ||
  998. nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
  999. nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
  1000. br->hash_elasticity) ||
  1001. nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
  1002. nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
  1003. br->multicast_last_member_count) ||
  1004. nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
  1005. br->multicast_startup_query_count))
  1006. return -EMSGSIZE;
  1007. clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
  1008. if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
  1009. return -EMSGSIZE;
  1010. clockval = jiffies_to_clock_t(br->multicast_membership_interval);
  1011. if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
  1012. return -EMSGSIZE;
  1013. clockval = jiffies_to_clock_t(br->multicast_querier_interval);
  1014. if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
  1015. return -EMSGSIZE;
  1016. clockval = jiffies_to_clock_t(br->multicast_query_interval);
  1017. if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
  1018. return -EMSGSIZE;
  1019. clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
  1020. if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
  1021. return -EMSGSIZE;
  1022. clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
  1023. if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
  1024. return -EMSGSIZE;
  1025. #endif
  1026. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1027. if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
  1028. br->nf_call_iptables ? 1 : 0) ||
  1029. nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
  1030. br->nf_call_ip6tables ? 1 : 0) ||
  1031. nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
  1032. br->nf_call_arptables ? 1 : 0))
  1033. return -EMSGSIZE;
  1034. #endif
  1035. return 0;
  1036. }
  1037. static struct rtnl_af_ops br_af_ops __read_mostly = {
  1038. .family = AF_BRIDGE,
  1039. .get_link_af_size = br_get_link_af_size_filtered,
  1040. };
  1041. struct rtnl_link_ops br_link_ops __read_mostly = {
  1042. .kind = "bridge",
  1043. .priv_size = sizeof(struct net_bridge),
  1044. .setup = br_dev_setup,
  1045. .maxtype = IFLA_BR_MAX,
  1046. .policy = br_policy,
  1047. .validate = br_validate,
  1048. .newlink = br_dev_newlink,
  1049. .changelink = br_changelink,
  1050. .dellink = br_dev_delete,
  1051. .get_size = br_get_size,
  1052. .fill_info = br_fill_info,
  1053. .slave_maxtype = IFLA_BRPORT_MAX,
  1054. .slave_policy = br_port_policy,
  1055. .slave_changelink = br_port_slave_changelink,
  1056. .get_slave_size = br_port_get_slave_size,
  1057. .fill_slave_info = br_port_fill_slave_info,
  1058. };
  1059. int __init br_netlink_init(void)
  1060. {
  1061. int err;
  1062. br_mdb_init();
  1063. rtnl_af_register(&br_af_ops);
  1064. err = rtnl_link_register(&br_link_ops);
  1065. if (err)
  1066. goto out_af;
  1067. return 0;
  1068. out_af:
  1069. rtnl_af_unregister(&br_af_ops);
  1070. br_mdb_uninit();
  1071. return err;
  1072. }
  1073. void br_netlink_fini(void)
  1074. {
  1075. br_mdb_uninit();
  1076. rtnl_af_unregister(&br_af_ops);
  1077. rtnl_link_unregister(&br_link_ops);
  1078. }