br_netlink.c 41 KB

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