br_netlink.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621
  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. #include "br_private_tunnel.h"
  22. static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  23. u32 filter_mask)
  24. {
  25. struct net_bridge_vlan *v;
  26. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  27. u16 flags, pvid;
  28. int num_vlans = 0;
  29. if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  30. return 0;
  31. pvid = br_get_pvid(vg);
  32. /* Count number of vlan infos */
  33. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  34. flags = 0;
  35. /* only a context, bridge vlan not activated */
  36. if (!br_vlan_should_use(v))
  37. continue;
  38. if (v->vid == pvid)
  39. flags |= BRIDGE_VLAN_INFO_PVID;
  40. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  41. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  42. if (vid_range_start == 0) {
  43. goto initvars;
  44. } else if ((v->vid - vid_range_end) == 1 &&
  45. flags == vid_range_flags) {
  46. vid_range_end = v->vid;
  47. continue;
  48. } else {
  49. if ((vid_range_end - vid_range_start) > 0)
  50. num_vlans += 2;
  51. else
  52. num_vlans += 1;
  53. }
  54. initvars:
  55. vid_range_start = v->vid;
  56. vid_range_end = v->vid;
  57. vid_range_flags = flags;
  58. }
  59. if (vid_range_start != 0) {
  60. if ((vid_range_end - vid_range_start) > 0)
  61. num_vlans += 2;
  62. else
  63. num_vlans += 1;
  64. }
  65. return num_vlans;
  66. }
  67. static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  68. u32 filter_mask)
  69. {
  70. int num_vlans;
  71. if (!vg)
  72. return 0;
  73. if (filter_mask & RTEXT_FILTER_BRVLAN)
  74. return vg->num_vlans;
  75. rcu_read_lock();
  76. num_vlans = __get_num_vlan_infos(vg, filter_mask);
  77. rcu_read_unlock();
  78. return num_vlans;
  79. }
  80. static size_t br_get_link_af_size_filtered(const struct net_device *dev,
  81. u32 filter_mask)
  82. {
  83. struct net_bridge_vlan_group *vg = NULL;
  84. struct net_bridge_port *p = NULL;
  85. struct net_bridge *br;
  86. int num_vlan_infos;
  87. size_t vinfo_sz = 0;
  88. rcu_read_lock();
  89. if (br_port_exists(dev)) {
  90. p = br_port_get_rcu(dev);
  91. vg = nbp_vlan_group_rcu(p);
  92. } else if (dev->priv_flags & IFF_EBRIDGE) {
  93. br = netdev_priv(dev);
  94. vg = br_vlan_group_rcu(br);
  95. }
  96. num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
  97. rcu_read_unlock();
  98. if (p && (p->flags & BR_VLAN_TUNNEL))
  99. vinfo_sz += br_get_vlan_tunnel_info_size(vg);
  100. /* Each VLAN is returned in bridge_vlan_info along with flags */
  101. vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
  102. return vinfo_sz;
  103. }
  104. static inline size_t br_port_info_size(void)
  105. {
  106. return nla_total_size(1) /* IFLA_BRPORT_STATE */
  107. + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
  108. + nla_total_size(4) /* IFLA_BRPORT_COST */
  109. + nla_total_size(1) /* IFLA_BRPORT_MODE */
  110. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  111. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  112. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  113. + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
  114. + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
  115. + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
  116. + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
  117. + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
  118. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
  119. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
  120. + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
  121. + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
  122. + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
  123. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
  124. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
  125. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
  126. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
  127. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
  128. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
  129. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
  130. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
  131. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
  132. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
  133. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
  134. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  135. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
  136. #endif
  137. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
  138. + 0;
  139. }
  140. static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
  141. {
  142. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  143. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  144. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  145. + nla_total_size(4) /* IFLA_MASTER */
  146. + nla_total_size(4) /* IFLA_MTU */
  147. + nla_total_size(4) /* IFLA_LINK */
  148. + nla_total_size(1) /* IFLA_OPERSTATE */
  149. + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
  150. + nla_total_size(br_get_link_af_size_filtered(dev,
  151. filter_mask)); /* IFLA_AF_SPEC */
  152. }
  153. static int br_port_fill_attrs(struct sk_buff *skb,
  154. const struct net_bridge_port *p)
  155. {
  156. u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
  157. u64 timerval;
  158. if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
  159. nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
  160. nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
  161. nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
  162. nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
  163. nla_put_u8(skb, IFLA_BRPORT_PROTECT,
  164. !!(p->flags & BR_ROOT_BLOCK)) ||
  165. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
  166. !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
  167. nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
  168. !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
  169. nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
  170. nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
  171. !!(p->flags & BR_FLOOD)) ||
  172. nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
  173. !!(p->flags & BR_MCAST_FLOOD)) ||
  174. nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
  175. !!(p->flags & BR_BCAST_FLOOD)) ||
  176. nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
  177. nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
  178. !!(p->flags & BR_PROXYARP_WIFI)) ||
  179. nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
  180. &p->designated_root) ||
  181. nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  182. &p->designated_bridge) ||
  183. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
  184. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
  185. nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
  186. nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
  187. nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
  188. p->topology_change_ack) ||
  189. nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
  190. nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
  191. BR_VLAN_TUNNEL)) ||
  192. nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
  193. nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
  194. !!(p->flags & BR_NEIGH_SUPPRESS)) ||
  195. nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
  196. return -EMSGSIZE;
  197. timerval = br_timer_value(&p->message_age_timer);
  198. if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
  199. IFLA_BRPORT_PAD))
  200. return -EMSGSIZE;
  201. timerval = br_timer_value(&p->forward_delay_timer);
  202. if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
  203. IFLA_BRPORT_PAD))
  204. return -EMSGSIZE;
  205. timerval = br_timer_value(&p->hold_timer);
  206. if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
  207. IFLA_BRPORT_PAD))
  208. return -EMSGSIZE;
  209. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  210. if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
  211. p->multicast_router))
  212. return -EMSGSIZE;
  213. #endif
  214. return 0;
  215. }
  216. static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
  217. u16 vid_end, u16 flags)
  218. {
  219. struct bridge_vlan_info vinfo;
  220. if ((vid_end - vid_start) > 0) {
  221. /* add range to skb */
  222. vinfo.vid = vid_start;
  223. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
  224. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  225. sizeof(vinfo), &vinfo))
  226. goto nla_put_failure;
  227. vinfo.vid = vid_end;
  228. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
  229. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  230. sizeof(vinfo), &vinfo))
  231. goto nla_put_failure;
  232. } else {
  233. vinfo.vid = vid_start;
  234. vinfo.flags = flags;
  235. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  236. sizeof(vinfo), &vinfo))
  237. goto nla_put_failure;
  238. }
  239. return 0;
  240. nla_put_failure:
  241. return -EMSGSIZE;
  242. }
  243. static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
  244. struct net_bridge_vlan_group *vg)
  245. {
  246. struct net_bridge_vlan *v;
  247. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  248. u16 flags, pvid;
  249. int err = 0;
  250. /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
  251. * and mark vlan info with begin and end flags
  252. * if vlaninfo represents a range
  253. */
  254. pvid = br_get_pvid(vg);
  255. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  256. flags = 0;
  257. if (!br_vlan_should_use(v))
  258. continue;
  259. if (v->vid == pvid)
  260. flags |= BRIDGE_VLAN_INFO_PVID;
  261. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  262. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  263. if (vid_range_start == 0) {
  264. goto initvars;
  265. } else if ((v->vid - vid_range_end) == 1 &&
  266. flags == vid_range_flags) {
  267. vid_range_end = v->vid;
  268. continue;
  269. } else {
  270. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  271. vid_range_end,
  272. vid_range_flags);
  273. if (err)
  274. return err;
  275. }
  276. initvars:
  277. vid_range_start = v->vid;
  278. vid_range_end = v->vid;
  279. vid_range_flags = flags;
  280. }
  281. if (vid_range_start != 0) {
  282. /* Call it once more to send any left over vlans */
  283. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  284. vid_range_end,
  285. vid_range_flags);
  286. if (err)
  287. return err;
  288. }
  289. return 0;
  290. }
  291. static int br_fill_ifvlaninfo(struct sk_buff *skb,
  292. struct net_bridge_vlan_group *vg)
  293. {
  294. struct bridge_vlan_info vinfo;
  295. struct net_bridge_vlan *v;
  296. u16 pvid;
  297. pvid = br_get_pvid(vg);
  298. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  299. if (!br_vlan_should_use(v))
  300. continue;
  301. vinfo.vid = v->vid;
  302. vinfo.flags = 0;
  303. if (v->vid == pvid)
  304. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  305. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  306. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  307. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  308. sizeof(vinfo), &vinfo))
  309. goto nla_put_failure;
  310. }
  311. return 0;
  312. nla_put_failure:
  313. return -EMSGSIZE;
  314. }
  315. /*
  316. * Create one netlink message for one interface
  317. * Contains port and master info as well as carrier and bridge state.
  318. */
  319. static int br_fill_ifinfo(struct sk_buff *skb,
  320. const struct net_bridge_port *port,
  321. u32 pid, u32 seq, int event, unsigned int flags,
  322. u32 filter_mask, const struct net_device *dev)
  323. {
  324. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  325. struct net_bridge *br;
  326. struct ifinfomsg *hdr;
  327. struct nlmsghdr *nlh;
  328. if (port)
  329. br = port->br;
  330. else
  331. br = netdev_priv(dev);
  332. br_debug(br, "br_fill_info event %d port %s master %s\n",
  333. event, dev->name, br->dev->name);
  334. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  335. if (nlh == NULL)
  336. return -EMSGSIZE;
  337. hdr = nlmsg_data(nlh);
  338. hdr->ifi_family = AF_BRIDGE;
  339. hdr->__ifi_pad = 0;
  340. hdr->ifi_type = dev->type;
  341. hdr->ifi_index = dev->ifindex;
  342. hdr->ifi_flags = dev_get_flags(dev);
  343. hdr->ifi_change = 0;
  344. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  345. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  346. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  347. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  348. (dev->addr_len &&
  349. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  350. (dev->ifindex != dev_get_iflink(dev) &&
  351. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  352. goto nla_put_failure;
  353. if (event == RTM_NEWLINK && port) {
  354. struct nlattr *nest
  355. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  356. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  357. goto nla_put_failure;
  358. nla_nest_end(skb, nest);
  359. }
  360. /* Check if the VID information is requested */
  361. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  362. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  363. struct net_bridge_vlan_group *vg;
  364. struct nlattr *af;
  365. int err;
  366. /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
  367. rcu_read_lock();
  368. if (port)
  369. vg = nbp_vlan_group_rcu(port);
  370. else
  371. vg = br_vlan_group_rcu(br);
  372. if (!vg || !vg->num_vlans) {
  373. rcu_read_unlock();
  374. goto done;
  375. }
  376. af = nla_nest_start(skb, IFLA_AF_SPEC);
  377. if (!af) {
  378. rcu_read_unlock();
  379. goto nla_put_failure;
  380. }
  381. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  382. err = br_fill_ifvlaninfo_compressed(skb, vg);
  383. else
  384. err = br_fill_ifvlaninfo(skb, vg);
  385. if (port && (port->flags & BR_VLAN_TUNNEL))
  386. err = br_fill_vlan_tunnel_info(skb, vg);
  387. rcu_read_unlock();
  388. if (err)
  389. goto nla_put_failure;
  390. nla_nest_end(skb, af);
  391. }
  392. done:
  393. nlmsg_end(skb, nlh);
  394. return 0;
  395. nla_put_failure:
  396. nlmsg_cancel(skb, nlh);
  397. return -EMSGSIZE;
  398. }
  399. /* Notify listeners of a change in bridge or port information */
  400. void br_ifinfo_notify(int event, const struct net_bridge *br,
  401. const struct net_bridge_port *port)
  402. {
  403. u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
  404. struct net_device *dev;
  405. struct sk_buff *skb;
  406. int err = -ENOBUFS;
  407. struct net *net;
  408. u16 port_no = 0;
  409. if (WARN_ON(!port && !br))
  410. return;
  411. if (port) {
  412. dev = port->dev;
  413. br = port->br;
  414. port_no = port->port_no;
  415. } else {
  416. dev = br->dev;
  417. }
  418. net = dev_net(dev);
  419. br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
  420. skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
  421. if (skb == NULL)
  422. goto errout;
  423. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev);
  424. if (err < 0) {
  425. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  426. WARN_ON(err == -EMSGSIZE);
  427. kfree_skb(skb);
  428. goto errout;
  429. }
  430. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  431. return;
  432. errout:
  433. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  434. }
  435. /*
  436. * Dump information about all ports, in response to GETLINK
  437. */
  438. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  439. struct net_device *dev, u32 filter_mask, int nlflags)
  440. {
  441. struct net_bridge_port *port = br_port_get_rtnl(dev);
  442. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
  443. !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  444. return 0;
  445. return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
  446. filter_mask, dev);
  447. }
  448. static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
  449. int cmd, struct bridge_vlan_info *vinfo, bool *changed)
  450. {
  451. bool curr_change;
  452. int err = 0;
  453. switch (cmd) {
  454. case RTM_SETLINK:
  455. if (p) {
  456. /* if the MASTER flag is set this will act on the global
  457. * per-VLAN entry as well
  458. */
  459. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
  460. &curr_change);
  461. } else {
  462. vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
  463. err = br_vlan_add(br, vinfo->vid, vinfo->flags,
  464. &curr_change);
  465. }
  466. if (curr_change)
  467. *changed = true;
  468. break;
  469. case RTM_DELLINK:
  470. if (p) {
  471. if (!nbp_vlan_delete(p, vinfo->vid))
  472. *changed = true;
  473. if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
  474. !br_vlan_delete(p->br, vinfo->vid))
  475. *changed = true;
  476. } else if (!br_vlan_delete(br, vinfo->vid)) {
  477. *changed = true;
  478. }
  479. break;
  480. }
  481. return err;
  482. }
  483. static int br_process_vlan_info(struct net_bridge *br,
  484. struct net_bridge_port *p, int cmd,
  485. struct bridge_vlan_info *vinfo_curr,
  486. struct bridge_vlan_info **vinfo_last,
  487. bool *changed)
  488. {
  489. if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
  490. return -EINVAL;
  491. if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  492. /* check if we are already processing a range */
  493. if (*vinfo_last)
  494. return -EINVAL;
  495. *vinfo_last = vinfo_curr;
  496. /* don't allow range of pvids */
  497. if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
  498. return -EINVAL;
  499. return 0;
  500. }
  501. if (*vinfo_last) {
  502. struct bridge_vlan_info tmp_vinfo;
  503. int v, err;
  504. if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
  505. return -EINVAL;
  506. if (vinfo_curr->vid <= (*vinfo_last)->vid)
  507. return -EINVAL;
  508. memcpy(&tmp_vinfo, *vinfo_last,
  509. sizeof(struct bridge_vlan_info));
  510. for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
  511. tmp_vinfo.vid = v;
  512. err = br_vlan_info(br, p, cmd, &tmp_vinfo, changed);
  513. if (err)
  514. break;
  515. }
  516. *vinfo_last = NULL;
  517. return err;
  518. }
  519. return br_vlan_info(br, p, cmd, vinfo_curr, changed);
  520. }
  521. static int br_afspec(struct net_bridge *br,
  522. struct net_bridge_port *p,
  523. struct nlattr *af_spec,
  524. int cmd, bool *changed)
  525. {
  526. struct bridge_vlan_info *vinfo_curr = NULL;
  527. struct bridge_vlan_info *vinfo_last = NULL;
  528. struct nlattr *attr;
  529. struct vtunnel_info tinfo_last = {};
  530. struct vtunnel_info tinfo_curr = {};
  531. int err = 0, rem;
  532. nla_for_each_nested(attr, af_spec, rem) {
  533. err = 0;
  534. switch (nla_type(attr)) {
  535. case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
  536. if (!p || !(p->flags & BR_VLAN_TUNNEL))
  537. return -EINVAL;
  538. err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
  539. if (err)
  540. return err;
  541. err = br_process_vlan_tunnel_info(br, p, cmd,
  542. &tinfo_curr,
  543. &tinfo_last,
  544. changed);
  545. if (err)
  546. return err;
  547. break;
  548. case IFLA_BRIDGE_VLAN_INFO:
  549. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  550. return -EINVAL;
  551. vinfo_curr = nla_data(attr);
  552. err = br_process_vlan_info(br, p, cmd, vinfo_curr,
  553. &vinfo_last, changed);
  554. if (err)
  555. return err;
  556. break;
  557. }
  558. }
  559. return err;
  560. }
  561. static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
  562. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  563. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  564. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  565. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  566. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  567. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  568. [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
  569. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  570. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  571. [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
  572. [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
  573. [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
  574. [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
  575. [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
  576. [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
  577. [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
  578. [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
  579. [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
  580. [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
  581. };
  582. /* Change the state of the port and notify spanning tree */
  583. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  584. {
  585. if (state > BR_STATE_BLOCKING)
  586. return -EINVAL;
  587. /* if kernel STP is running, don't allow changes */
  588. if (p->br->stp_enabled == BR_KERNEL_STP)
  589. return -EBUSY;
  590. /* if device is not up, change is not allowed
  591. * if link is not present, only allowable state is disabled
  592. */
  593. if (!netif_running(p->dev) ||
  594. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  595. return -ENETDOWN;
  596. br_set_state(p, state);
  597. br_port_state_selection(p->br);
  598. return 0;
  599. }
  600. /* Set/clear or port flags based on attribute */
  601. static int br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  602. int attrtype, unsigned long mask)
  603. {
  604. unsigned long flags;
  605. int err;
  606. if (!tb[attrtype])
  607. return 0;
  608. if (nla_get_u8(tb[attrtype]))
  609. flags = p->flags | mask;
  610. else
  611. flags = p->flags & ~mask;
  612. err = br_switchdev_set_port_flag(p, flags, mask);
  613. if (err)
  614. return err;
  615. p->flags = flags;
  616. return 0;
  617. }
  618. /* Process bridge protocol info on port */
  619. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  620. {
  621. unsigned long old_flags = p->flags;
  622. bool br_vlan_tunnel_old = false;
  623. int err;
  624. err = br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  625. if (err)
  626. return err;
  627. err = br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  628. if (err)
  629. return err;
  630. err = br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  631. if (err)
  632. return err;
  633. err = br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  634. if (err)
  635. return err;
  636. err = br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
  637. if (err)
  638. return err;
  639. err = br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
  640. if (err)
  641. return err;
  642. err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
  643. if (err)
  644. return err;
  645. err = br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
  646. if (err)
  647. return err;
  648. err = br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
  649. if (err)
  650. return err;
  651. err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
  652. if (err)
  653. return err;
  654. err = br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
  655. if (err)
  656. return err;
  657. br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
  658. err = br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
  659. if (err)
  660. return err;
  661. if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
  662. nbp_vlan_tunnel_info_flush(p);
  663. if (tb[IFLA_BRPORT_COST]) {
  664. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  665. if (err)
  666. return err;
  667. }
  668. if (tb[IFLA_BRPORT_PRIORITY]) {
  669. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  670. if (err)
  671. return err;
  672. }
  673. if (tb[IFLA_BRPORT_STATE]) {
  674. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  675. if (err)
  676. return err;
  677. }
  678. if (tb[IFLA_BRPORT_FLUSH])
  679. br_fdb_delete_by_port(p->br, p, 0, 0);
  680. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  681. if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
  682. u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
  683. err = br_multicast_set_port_router(p, mcast_router);
  684. if (err)
  685. return err;
  686. }
  687. #endif
  688. if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
  689. u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
  690. if (fwd_mask & BR_GROUPFWD_MACPAUSE)
  691. return -EINVAL;
  692. p->group_fwd_mask = fwd_mask;
  693. }
  694. err = br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS,
  695. BR_NEIGH_SUPPRESS);
  696. if (err)
  697. return err;
  698. err = br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
  699. if (err)
  700. return err;
  701. br_port_flags_change(p, old_flags ^ p->flags);
  702. return 0;
  703. }
  704. /* Change state and parameters on port. */
  705. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  706. {
  707. struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
  708. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  709. struct net_bridge_port *p;
  710. struct nlattr *protinfo;
  711. struct nlattr *afspec;
  712. bool changed = false;
  713. int err = 0;
  714. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
  715. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  716. if (!protinfo && !afspec)
  717. return 0;
  718. p = br_port_get_rtnl(dev);
  719. /* We want to accept dev as bridge itself if the AF_SPEC
  720. * is set to see if someone is setting vlan info on the bridge
  721. */
  722. if (!p && !afspec)
  723. return -EINVAL;
  724. if (p && protinfo) {
  725. if (protinfo->nla_type & NLA_F_NESTED) {
  726. err = nla_parse_nested(tb, IFLA_BRPORT_MAX, protinfo,
  727. br_port_policy, NULL);
  728. if (err)
  729. return err;
  730. spin_lock_bh(&p->br->lock);
  731. err = br_setport(p, tb);
  732. spin_unlock_bh(&p->br->lock);
  733. } else {
  734. /* Binary compatibility with old RSTP */
  735. if (nla_len(protinfo) < sizeof(u8))
  736. return -EINVAL;
  737. spin_lock_bh(&p->br->lock);
  738. err = br_set_port_state(p, nla_get_u8(protinfo));
  739. spin_unlock_bh(&p->br->lock);
  740. }
  741. if (err)
  742. goto out;
  743. changed = true;
  744. }
  745. if (afspec)
  746. err = br_afspec(br, p, afspec, RTM_SETLINK, &changed);
  747. if (changed)
  748. br_ifinfo_notify(RTM_NEWLINK, br, p);
  749. out:
  750. return err;
  751. }
  752. /* Delete port information */
  753. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  754. {
  755. struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
  756. struct net_bridge_port *p;
  757. struct nlattr *afspec;
  758. bool changed = false;
  759. int err = 0;
  760. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  761. if (!afspec)
  762. return 0;
  763. p = br_port_get_rtnl(dev);
  764. /* We want to accept dev as bridge itself as well */
  765. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  766. return -EINVAL;
  767. err = br_afspec(br, p, afspec, RTM_DELLINK, &changed);
  768. if (changed)
  769. /* Send RTM_NEWLINK because userspace
  770. * expects RTM_NEWLINK for vlan dels
  771. */
  772. br_ifinfo_notify(RTM_NEWLINK, br, p);
  773. return err;
  774. }
  775. static int br_validate(struct nlattr *tb[], struct nlattr *data[],
  776. struct netlink_ext_ack *extack)
  777. {
  778. if (tb[IFLA_ADDRESS]) {
  779. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  780. return -EINVAL;
  781. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  782. return -EADDRNOTAVAIL;
  783. }
  784. if (!data)
  785. return 0;
  786. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  787. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  788. switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
  789. case htons(ETH_P_8021Q):
  790. case htons(ETH_P_8021AD):
  791. break;
  792. default:
  793. return -EPROTONOSUPPORT;
  794. }
  795. }
  796. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  797. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  798. if (defpvid >= VLAN_VID_MASK)
  799. return -EINVAL;
  800. }
  801. #endif
  802. return 0;
  803. }
  804. static int br_port_slave_changelink(struct net_device *brdev,
  805. struct net_device *dev,
  806. struct nlattr *tb[],
  807. struct nlattr *data[],
  808. struct netlink_ext_ack *extack)
  809. {
  810. struct net_bridge *br = netdev_priv(brdev);
  811. int ret;
  812. if (!data)
  813. return 0;
  814. spin_lock_bh(&br->lock);
  815. ret = br_setport(br_port_get_rtnl(dev), data);
  816. spin_unlock_bh(&br->lock);
  817. return ret;
  818. }
  819. static int br_port_fill_slave_info(struct sk_buff *skb,
  820. const struct net_device *brdev,
  821. const struct net_device *dev)
  822. {
  823. return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
  824. }
  825. static size_t br_port_get_slave_size(const struct net_device *brdev,
  826. const struct net_device *dev)
  827. {
  828. return br_port_info_size();
  829. }
  830. static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
  831. [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
  832. [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
  833. [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
  834. [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
  835. [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
  836. [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
  837. [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
  838. [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
  839. [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
  840. [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
  841. .len = ETH_ALEN },
  842. [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
  843. [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
  844. [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
  845. [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
  846. [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
  847. [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
  848. [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
  849. [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
  850. [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
  851. [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
  852. [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
  853. [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
  854. [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
  855. [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
  856. [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
  857. [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
  858. [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
  859. [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
  860. [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
  861. [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
  862. [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
  863. [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
  864. };
  865. static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
  866. struct nlattr *data[],
  867. struct netlink_ext_ack *extack)
  868. {
  869. struct net_bridge *br = netdev_priv(brdev);
  870. int err;
  871. if (!data)
  872. return 0;
  873. if (data[IFLA_BR_FORWARD_DELAY]) {
  874. err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
  875. if (err)
  876. return err;
  877. }
  878. if (data[IFLA_BR_HELLO_TIME]) {
  879. err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
  880. if (err)
  881. return err;
  882. }
  883. if (data[IFLA_BR_MAX_AGE]) {
  884. err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
  885. if (err)
  886. return err;
  887. }
  888. if (data[IFLA_BR_AGEING_TIME]) {
  889. err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
  890. if (err)
  891. return err;
  892. }
  893. if (data[IFLA_BR_STP_STATE]) {
  894. u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
  895. br_stp_set_enabled(br, stp_enabled);
  896. }
  897. if (data[IFLA_BR_PRIORITY]) {
  898. u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
  899. br_stp_set_bridge_priority(br, priority);
  900. }
  901. if (data[IFLA_BR_VLAN_FILTERING]) {
  902. u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
  903. err = __br_vlan_filter_toggle(br, vlan_filter);
  904. if (err)
  905. return err;
  906. }
  907. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  908. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  909. __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
  910. err = __br_vlan_set_proto(br, vlan_proto);
  911. if (err)
  912. return err;
  913. }
  914. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  915. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  916. err = __br_vlan_set_default_pvid(br, defpvid);
  917. if (err)
  918. return err;
  919. }
  920. if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
  921. __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
  922. err = br_vlan_set_stats(br, vlan_stats);
  923. if (err)
  924. return err;
  925. }
  926. #endif
  927. if (data[IFLA_BR_GROUP_FWD_MASK]) {
  928. u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
  929. if (fwd_mask & BR_GROUPFWD_RESTRICTED)
  930. return -EINVAL;
  931. br->group_fwd_mask = fwd_mask;
  932. }
  933. if (data[IFLA_BR_GROUP_ADDR]) {
  934. u8 new_addr[ETH_ALEN];
  935. if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
  936. return -EINVAL;
  937. memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
  938. if (!is_link_local_ether_addr(new_addr))
  939. return -EINVAL;
  940. if (new_addr[5] == 1 || /* 802.3x Pause address */
  941. new_addr[5] == 2 || /* 802.3ad Slow protocols */
  942. new_addr[5] == 3) /* 802.1X PAE address */
  943. return -EINVAL;
  944. spin_lock_bh(&br->lock);
  945. memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
  946. spin_unlock_bh(&br->lock);
  947. br->group_addr_set = true;
  948. br_recalculate_fwd_mask(br);
  949. }
  950. if (data[IFLA_BR_FDB_FLUSH])
  951. br_fdb_flush(br);
  952. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  953. if (data[IFLA_BR_MCAST_ROUTER]) {
  954. u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
  955. err = br_multicast_set_router(br, multicast_router);
  956. if (err)
  957. return err;
  958. }
  959. if (data[IFLA_BR_MCAST_SNOOPING]) {
  960. u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
  961. err = br_multicast_toggle(br, mcast_snooping);
  962. if (err)
  963. return err;
  964. }
  965. if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
  966. u8 val;
  967. val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
  968. br->multicast_query_use_ifaddr = !!val;
  969. }
  970. if (data[IFLA_BR_MCAST_QUERIER]) {
  971. u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
  972. err = br_multicast_set_querier(br, mcast_querier);
  973. if (err)
  974. return err;
  975. }
  976. if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
  977. u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
  978. br->hash_elasticity = val;
  979. }
  980. if (data[IFLA_BR_MCAST_HASH_MAX]) {
  981. u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
  982. err = br_multicast_set_hash_max(br, hash_max);
  983. if (err)
  984. return err;
  985. }
  986. if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
  987. u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
  988. br->multicast_last_member_count = val;
  989. }
  990. if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
  991. u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
  992. br->multicast_startup_query_count = val;
  993. }
  994. if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
  995. u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
  996. br->multicast_last_member_interval = clock_t_to_jiffies(val);
  997. }
  998. if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
  999. u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
  1000. br->multicast_membership_interval = clock_t_to_jiffies(val);
  1001. }
  1002. if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
  1003. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
  1004. br->multicast_querier_interval = clock_t_to_jiffies(val);
  1005. }
  1006. if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
  1007. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
  1008. br->multicast_query_interval = clock_t_to_jiffies(val);
  1009. }
  1010. if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
  1011. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
  1012. br->multicast_query_response_interval = clock_t_to_jiffies(val);
  1013. }
  1014. if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
  1015. u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
  1016. br->multicast_startup_query_interval = clock_t_to_jiffies(val);
  1017. }
  1018. if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
  1019. __u8 mcast_stats;
  1020. mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
  1021. br->multicast_stats_enabled = !!mcast_stats;
  1022. }
  1023. if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
  1024. __u8 igmp_version;
  1025. igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
  1026. err = br_multicast_set_igmp_version(br, igmp_version);
  1027. if (err)
  1028. return err;
  1029. }
  1030. #if IS_ENABLED(CONFIG_IPV6)
  1031. if (data[IFLA_BR_MCAST_MLD_VERSION]) {
  1032. __u8 mld_version;
  1033. mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
  1034. err = br_multicast_set_mld_version(br, mld_version);
  1035. if (err)
  1036. return err;
  1037. }
  1038. #endif
  1039. #endif
  1040. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1041. if (data[IFLA_BR_NF_CALL_IPTABLES]) {
  1042. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
  1043. br->nf_call_iptables = val ? true : false;
  1044. }
  1045. if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
  1046. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
  1047. br->nf_call_ip6tables = val ? true : false;
  1048. }
  1049. if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
  1050. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
  1051. br->nf_call_arptables = val ? true : false;
  1052. }
  1053. #endif
  1054. return 0;
  1055. }
  1056. static int br_dev_newlink(struct net *src_net, struct net_device *dev,
  1057. struct nlattr *tb[], struct nlattr *data[],
  1058. struct netlink_ext_ack *extack)
  1059. {
  1060. struct net_bridge *br = netdev_priv(dev);
  1061. int err;
  1062. err = register_netdevice(dev);
  1063. if (err)
  1064. return err;
  1065. if (tb[IFLA_ADDRESS]) {
  1066. spin_lock_bh(&br->lock);
  1067. br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
  1068. spin_unlock_bh(&br->lock);
  1069. }
  1070. err = br_changelink(dev, tb, data, extack);
  1071. if (err)
  1072. br_dev_delete(dev, NULL);
  1073. return err;
  1074. }
  1075. static size_t br_get_size(const struct net_device *brdev)
  1076. {
  1077. return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
  1078. nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
  1079. nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
  1080. nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
  1081. nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
  1082. nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
  1083. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
  1084. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  1085. nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
  1086. nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
  1087. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
  1088. #endif
  1089. nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
  1090. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
  1091. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
  1092. nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
  1093. nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
  1094. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
  1095. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
  1096. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
  1097. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
  1098. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
  1099. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
  1100. nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
  1101. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1102. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
  1103. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
  1104. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
  1105. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
  1106. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
  1107. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
  1108. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
  1109. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
  1110. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
  1111. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
  1112. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
  1113. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
  1114. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
  1115. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
  1116. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
  1117. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
  1118. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
  1119. #endif
  1120. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1121. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
  1122. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
  1123. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
  1124. #endif
  1125. 0;
  1126. }
  1127. static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
  1128. {
  1129. struct net_bridge *br = netdev_priv(brdev);
  1130. u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
  1131. u32 hello_time = jiffies_to_clock_t(br->hello_time);
  1132. u32 age_time = jiffies_to_clock_t(br->max_age);
  1133. u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
  1134. u32 stp_enabled = br->stp_enabled;
  1135. u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
  1136. u8 vlan_enabled = br_vlan_enabled(br->dev);
  1137. u64 clockval;
  1138. clockval = br_timer_value(&br->hello_timer);
  1139. if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
  1140. return -EMSGSIZE;
  1141. clockval = br_timer_value(&br->tcn_timer);
  1142. if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
  1143. return -EMSGSIZE;
  1144. clockval = br_timer_value(&br->topology_change_timer);
  1145. if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
  1146. IFLA_BR_PAD))
  1147. return -EMSGSIZE;
  1148. clockval = br_timer_value(&br->gc_work.timer);
  1149. if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
  1150. return -EMSGSIZE;
  1151. if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
  1152. nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
  1153. nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
  1154. nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
  1155. nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
  1156. nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
  1157. nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
  1158. nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
  1159. nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  1160. &br->bridge_id) ||
  1161. nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
  1162. &br->designated_root) ||
  1163. nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
  1164. nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
  1165. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
  1166. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
  1167. br->topology_change_detected) ||
  1168. nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
  1169. return -EMSGSIZE;
  1170. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  1171. if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
  1172. nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
  1173. nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
  1174. return -EMSGSIZE;
  1175. #endif
  1176. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1177. if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
  1178. nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
  1179. nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
  1180. br->multicast_query_use_ifaddr) ||
  1181. nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
  1182. nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
  1183. br->multicast_stats_enabled) ||
  1184. nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
  1185. br->hash_elasticity) ||
  1186. nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
  1187. nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
  1188. br->multicast_last_member_count) ||
  1189. nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
  1190. br->multicast_startup_query_count) ||
  1191. nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
  1192. br->multicast_igmp_version))
  1193. return -EMSGSIZE;
  1194. #if IS_ENABLED(CONFIG_IPV6)
  1195. if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
  1196. br->multicast_mld_version))
  1197. return -EMSGSIZE;
  1198. #endif
  1199. clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
  1200. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
  1201. IFLA_BR_PAD))
  1202. return -EMSGSIZE;
  1203. clockval = jiffies_to_clock_t(br->multicast_membership_interval);
  1204. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
  1205. IFLA_BR_PAD))
  1206. return -EMSGSIZE;
  1207. clockval = jiffies_to_clock_t(br->multicast_querier_interval);
  1208. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
  1209. IFLA_BR_PAD))
  1210. return -EMSGSIZE;
  1211. clockval = jiffies_to_clock_t(br->multicast_query_interval);
  1212. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
  1213. IFLA_BR_PAD))
  1214. return -EMSGSIZE;
  1215. clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
  1216. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
  1217. IFLA_BR_PAD))
  1218. return -EMSGSIZE;
  1219. clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
  1220. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
  1221. IFLA_BR_PAD))
  1222. return -EMSGSIZE;
  1223. #endif
  1224. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1225. if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
  1226. br->nf_call_iptables ? 1 : 0) ||
  1227. nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
  1228. br->nf_call_ip6tables ? 1 : 0) ||
  1229. nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
  1230. br->nf_call_arptables ? 1 : 0))
  1231. return -EMSGSIZE;
  1232. #endif
  1233. return 0;
  1234. }
  1235. static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
  1236. {
  1237. struct net_bridge_port *p = NULL;
  1238. struct net_bridge_vlan_group *vg;
  1239. struct net_bridge_vlan *v;
  1240. struct net_bridge *br;
  1241. int numvls = 0;
  1242. switch (attr) {
  1243. case IFLA_STATS_LINK_XSTATS:
  1244. br = netdev_priv(dev);
  1245. vg = br_vlan_group(br);
  1246. break;
  1247. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1248. p = br_port_get_rtnl(dev);
  1249. if (!p)
  1250. return 0;
  1251. br = p->br;
  1252. vg = nbp_vlan_group(p);
  1253. break;
  1254. default:
  1255. return 0;
  1256. }
  1257. if (vg) {
  1258. /* we need to count all, even placeholder entries */
  1259. list_for_each_entry(v, &vg->vlan_list, vlist)
  1260. numvls++;
  1261. }
  1262. return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
  1263. nla_total_size(sizeof(struct br_mcast_stats)) +
  1264. nla_total_size(0);
  1265. }
  1266. static int br_fill_linkxstats(struct sk_buff *skb,
  1267. const struct net_device *dev,
  1268. int *prividx, int attr)
  1269. {
  1270. struct nlattr *nla __maybe_unused;
  1271. struct net_bridge_port *p = NULL;
  1272. struct net_bridge_vlan_group *vg;
  1273. struct net_bridge_vlan *v;
  1274. struct net_bridge *br;
  1275. struct nlattr *nest;
  1276. int vl_idx = 0;
  1277. switch (attr) {
  1278. case IFLA_STATS_LINK_XSTATS:
  1279. br = netdev_priv(dev);
  1280. vg = br_vlan_group(br);
  1281. break;
  1282. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1283. p = br_port_get_rtnl(dev);
  1284. if (!p)
  1285. return 0;
  1286. br = p->br;
  1287. vg = nbp_vlan_group(p);
  1288. break;
  1289. default:
  1290. return -EINVAL;
  1291. }
  1292. nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
  1293. if (!nest)
  1294. return -EMSGSIZE;
  1295. if (vg) {
  1296. u16 pvid;
  1297. pvid = br_get_pvid(vg);
  1298. list_for_each_entry(v, &vg->vlan_list, vlist) {
  1299. struct bridge_vlan_xstats vxi;
  1300. struct br_vlan_stats stats;
  1301. if (++vl_idx < *prividx)
  1302. continue;
  1303. memset(&vxi, 0, sizeof(vxi));
  1304. vxi.vid = v->vid;
  1305. vxi.flags = v->flags;
  1306. if (v->vid == pvid)
  1307. vxi.flags |= BRIDGE_VLAN_INFO_PVID;
  1308. br_vlan_get_stats(v, &stats);
  1309. vxi.rx_bytes = stats.rx_bytes;
  1310. vxi.rx_packets = stats.rx_packets;
  1311. vxi.tx_bytes = stats.tx_bytes;
  1312. vxi.tx_packets = stats.tx_packets;
  1313. if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
  1314. goto nla_put_failure;
  1315. }
  1316. }
  1317. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1318. if (++vl_idx >= *prividx) {
  1319. nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
  1320. sizeof(struct br_mcast_stats),
  1321. BRIDGE_XSTATS_PAD);
  1322. if (!nla)
  1323. goto nla_put_failure;
  1324. br_multicast_get_stats(br, p, nla_data(nla));
  1325. }
  1326. #endif
  1327. nla_nest_end(skb, nest);
  1328. *prividx = 0;
  1329. return 0;
  1330. nla_put_failure:
  1331. nla_nest_end(skb, nest);
  1332. *prividx = vl_idx;
  1333. return -EMSGSIZE;
  1334. }
  1335. static struct rtnl_af_ops br_af_ops __read_mostly = {
  1336. .family = AF_BRIDGE,
  1337. .get_link_af_size = br_get_link_af_size_filtered,
  1338. };
  1339. struct rtnl_link_ops br_link_ops __read_mostly = {
  1340. .kind = "bridge",
  1341. .priv_size = sizeof(struct net_bridge),
  1342. .setup = br_dev_setup,
  1343. .maxtype = IFLA_BR_MAX,
  1344. .policy = br_policy,
  1345. .validate = br_validate,
  1346. .newlink = br_dev_newlink,
  1347. .changelink = br_changelink,
  1348. .dellink = br_dev_delete,
  1349. .get_size = br_get_size,
  1350. .fill_info = br_fill_info,
  1351. .fill_linkxstats = br_fill_linkxstats,
  1352. .get_linkxstats_size = br_get_linkxstats_size,
  1353. .slave_maxtype = IFLA_BRPORT_MAX,
  1354. .slave_policy = br_port_policy,
  1355. .slave_changelink = br_port_slave_changelink,
  1356. .get_slave_size = br_port_get_slave_size,
  1357. .fill_slave_info = br_port_fill_slave_info,
  1358. };
  1359. int __init br_netlink_init(void)
  1360. {
  1361. int err;
  1362. br_mdb_init();
  1363. rtnl_af_register(&br_af_ops);
  1364. err = rtnl_link_register(&br_link_ops);
  1365. if (err)
  1366. goto out_af;
  1367. return 0;
  1368. out_af:
  1369. rtnl_af_unregister(&br_af_ops);
  1370. br_mdb_uninit();
  1371. return err;
  1372. }
  1373. void br_netlink_fini(void)
  1374. {
  1375. br_mdb_uninit();
  1376. rtnl_af_unregister(&br_af_ops);
  1377. rtnl_link_unregister(&br_link_ops);
  1378. }