br_netlink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 inline size_t br_port_info_size(void)
  22. {
  23. return nla_total_size(1) /* IFLA_BRPORT_STATE */
  24. + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
  25. + nla_total_size(4) /* IFLA_BRPORT_COST */
  26. + nla_total_size(1) /* IFLA_BRPORT_MODE */
  27. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  28. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  29. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  30. + 0;
  31. }
  32. static inline size_t br_nlmsg_size(void)
  33. {
  34. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  35. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  36. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  37. + nla_total_size(4) /* IFLA_MASTER */
  38. + nla_total_size(4) /* IFLA_MTU */
  39. + nla_total_size(4) /* IFLA_LINK */
  40. + nla_total_size(1) /* IFLA_OPERSTATE */
  41. + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */
  42. }
  43. static int br_port_fill_attrs(struct sk_buff *skb,
  44. const struct net_bridge_port *p)
  45. {
  46. u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
  47. if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
  48. nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
  49. nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
  50. nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
  51. nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
  52. nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
  53. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)))
  54. return -EMSGSIZE;
  55. return 0;
  56. }
  57. /*
  58. * Create one netlink message for one interface
  59. * Contains port and master info as well as carrier and bridge state.
  60. */
  61. static int br_fill_ifinfo(struct sk_buff *skb,
  62. const struct net_bridge_port *port,
  63. u32 pid, u32 seq, int event, unsigned int flags,
  64. u32 filter_mask, const struct net_device *dev)
  65. {
  66. const struct net_bridge *br;
  67. struct ifinfomsg *hdr;
  68. struct nlmsghdr *nlh;
  69. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  70. if (port)
  71. br = port->br;
  72. else
  73. br = netdev_priv(dev);
  74. br_debug(br, "br_fill_info event %d port %s master %s\n",
  75. event, dev->name, br->dev->name);
  76. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  77. if (nlh == NULL)
  78. return -EMSGSIZE;
  79. hdr = nlmsg_data(nlh);
  80. hdr->ifi_family = AF_BRIDGE;
  81. hdr->__ifi_pad = 0;
  82. hdr->ifi_type = dev->type;
  83. hdr->ifi_index = dev->ifindex;
  84. hdr->ifi_flags = dev_get_flags(dev);
  85. hdr->ifi_change = 0;
  86. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  87. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  88. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  89. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  90. (dev->addr_len &&
  91. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  92. (dev->ifindex != dev->iflink &&
  93. nla_put_u32(skb, IFLA_LINK, dev->iflink)))
  94. goto nla_put_failure;
  95. if (event == RTM_NEWLINK && port) {
  96. struct nlattr *nest
  97. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  98. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  99. goto nla_put_failure;
  100. nla_nest_end(skb, nest);
  101. }
  102. /* Check if the VID information is requested */
  103. if (filter_mask & RTEXT_FILTER_BRVLAN) {
  104. struct nlattr *af;
  105. const struct net_port_vlans *pv;
  106. struct bridge_vlan_info vinfo;
  107. u16 vid;
  108. u16 pvid;
  109. if (port)
  110. pv = nbp_get_vlan_info(port);
  111. else
  112. pv = br_get_vlan_info(br);
  113. if (!pv || bitmap_empty(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN))
  114. goto done;
  115. af = nla_nest_start(skb, IFLA_AF_SPEC);
  116. if (!af)
  117. goto nla_put_failure;
  118. pvid = br_get_pvid(pv);
  119. for (vid = find_first_bit(pv->vlan_bitmap, BR_VLAN_BITMAP_LEN);
  120. vid < BR_VLAN_BITMAP_LEN;
  121. vid = find_next_bit(pv->vlan_bitmap,
  122. BR_VLAN_BITMAP_LEN, vid+1)) {
  123. vinfo.vid = vid;
  124. vinfo.flags = 0;
  125. if (vid == pvid)
  126. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  127. if (test_bit(vid, pv->untagged_bitmap))
  128. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  129. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  130. sizeof(vinfo), &vinfo))
  131. goto nla_put_failure;
  132. }
  133. nla_nest_end(skb, af);
  134. }
  135. done:
  136. return nlmsg_end(skb, nlh);
  137. nla_put_failure:
  138. nlmsg_cancel(skb, nlh);
  139. return -EMSGSIZE;
  140. }
  141. /*
  142. * Notify listeners of a change in port information
  143. */
  144. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  145. {
  146. struct net *net;
  147. struct sk_buff *skb;
  148. int err = -ENOBUFS;
  149. if (!port)
  150. return;
  151. net = dev_net(port->dev);
  152. br_debug(port->br, "port %u(%s) event %d\n",
  153. (unsigned int)port->port_no, port->dev->name, event);
  154. skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
  155. if (skb == NULL)
  156. goto errout;
  157. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, 0, port->dev);
  158. if (err < 0) {
  159. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  160. WARN_ON(err == -EMSGSIZE);
  161. kfree_skb(skb);
  162. goto errout;
  163. }
  164. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  165. return;
  166. errout:
  167. if (err < 0)
  168. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  169. }
  170. /*
  171. * Dump information about all ports, in response to GETLINK
  172. */
  173. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  174. struct net_device *dev, u32 filter_mask)
  175. {
  176. int err = 0;
  177. struct net_bridge_port *port = br_port_get_rcu(dev);
  178. /* not a bridge port and */
  179. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN))
  180. goto out;
  181. err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI,
  182. filter_mask, dev);
  183. out:
  184. return err;
  185. }
  186. static const struct nla_policy ifla_br_policy[IFLA_MAX+1] = {
  187. [IFLA_BRIDGE_FLAGS] = { .type = NLA_U16 },
  188. [IFLA_BRIDGE_MODE] = { .type = NLA_U16 },
  189. [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
  190. .len = sizeof(struct bridge_vlan_info), },
  191. };
  192. static int br_afspec(struct net_bridge *br,
  193. struct net_bridge_port *p,
  194. struct nlattr *af_spec,
  195. int cmd)
  196. {
  197. struct nlattr *tb[IFLA_BRIDGE_MAX+1];
  198. int err = 0;
  199. err = nla_parse_nested(tb, IFLA_BRIDGE_MAX, af_spec, ifla_br_policy);
  200. if (err)
  201. return err;
  202. if (tb[IFLA_BRIDGE_VLAN_INFO]) {
  203. struct bridge_vlan_info *vinfo;
  204. vinfo = nla_data(tb[IFLA_BRIDGE_VLAN_INFO]);
  205. if (vinfo->vid >= VLAN_N_VID)
  206. return -EINVAL;
  207. switch (cmd) {
  208. case RTM_SETLINK:
  209. if (p) {
  210. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
  211. if (err)
  212. break;
  213. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  214. err = br_vlan_add(p->br, vinfo->vid,
  215. vinfo->flags);
  216. } else
  217. err = br_vlan_add(br, vinfo->vid, vinfo->flags);
  218. if (err)
  219. break;
  220. break;
  221. case RTM_DELLINK:
  222. if (p) {
  223. nbp_vlan_delete(p, vinfo->vid);
  224. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  225. br_vlan_delete(p->br, vinfo->vid);
  226. } else
  227. br_vlan_delete(br, vinfo->vid);
  228. break;
  229. }
  230. }
  231. return err;
  232. }
  233. static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = {
  234. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  235. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  236. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  237. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  238. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  239. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  240. };
  241. /* Change the state of the port and notify spanning tree */
  242. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  243. {
  244. if (state > BR_STATE_BLOCKING)
  245. return -EINVAL;
  246. /* if kernel STP is running, don't allow changes */
  247. if (p->br->stp_enabled == BR_KERNEL_STP)
  248. return -EBUSY;
  249. /* if device is not up, change is not allowed
  250. * if link is not present, only allowable state is disabled
  251. */
  252. if (!netif_running(p->dev) ||
  253. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  254. return -ENETDOWN;
  255. p->state = state;
  256. br_log_state(p);
  257. br_port_state_selection(p->br);
  258. return 0;
  259. }
  260. /* Set/clear or port flags based on attribute */
  261. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  262. int attrtype, unsigned long mask)
  263. {
  264. if (tb[attrtype]) {
  265. u8 flag = nla_get_u8(tb[attrtype]);
  266. if (flag)
  267. p->flags |= mask;
  268. else
  269. p->flags &= ~mask;
  270. }
  271. }
  272. /* Process bridge protocol info on port */
  273. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  274. {
  275. int err;
  276. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  277. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  278. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  279. br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  280. if (tb[IFLA_BRPORT_COST]) {
  281. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  282. if (err)
  283. return err;
  284. }
  285. if (tb[IFLA_BRPORT_PRIORITY]) {
  286. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  287. if (err)
  288. return err;
  289. }
  290. if (tb[IFLA_BRPORT_STATE]) {
  291. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  292. if (err)
  293. return err;
  294. }
  295. return 0;
  296. }
  297. /* Change state and parameters on port. */
  298. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh)
  299. {
  300. struct ifinfomsg *ifm;
  301. struct nlattr *protinfo;
  302. struct nlattr *afspec;
  303. struct net_bridge_port *p;
  304. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  305. int err;
  306. ifm = nlmsg_data(nlh);
  307. protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO);
  308. afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC);
  309. if (!protinfo && !afspec)
  310. return 0;
  311. p = br_port_get_rtnl(dev);
  312. /* We want to accept dev as bridge itself if the AF_SPEC
  313. * is set to see if someone is setting vlan info on the brigde
  314. */
  315. if (!p && ((dev->priv_flags & IFF_EBRIDGE) && !afspec))
  316. return -EINVAL;
  317. if (p && protinfo) {
  318. if (protinfo->nla_type & NLA_F_NESTED) {
  319. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  320. protinfo, ifla_brport_policy);
  321. if (err)
  322. return err;
  323. spin_lock_bh(&p->br->lock);
  324. err = br_setport(p, tb);
  325. spin_unlock_bh(&p->br->lock);
  326. } else {
  327. /* Binary compatability with old RSTP */
  328. if (nla_len(protinfo) < sizeof(u8))
  329. return -EINVAL;
  330. spin_lock_bh(&p->br->lock);
  331. err = br_set_port_state(p, nla_get_u8(protinfo));
  332. spin_unlock_bh(&p->br->lock);
  333. }
  334. if (err)
  335. goto out;
  336. }
  337. if (afspec) {
  338. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  339. afspec, RTM_SETLINK);
  340. }
  341. if (err == 0)
  342. br_ifinfo_notify(RTM_NEWLINK, p);
  343. out:
  344. return err;
  345. }
  346. /* Delete port information */
  347. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh)
  348. {
  349. struct ifinfomsg *ifm;
  350. struct nlattr *afspec;
  351. struct net_bridge_port *p;
  352. int err;
  353. ifm = nlmsg_data(nlh);
  354. afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC);
  355. if (!afspec)
  356. return 0;
  357. p = br_port_get_rtnl(dev);
  358. /* We want to accept dev as bridge itself as well */
  359. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  360. return -EINVAL;
  361. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  362. afspec, RTM_DELLINK);
  363. return err;
  364. }
  365. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  366. {
  367. if (tb[IFLA_ADDRESS]) {
  368. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  369. return -EINVAL;
  370. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  371. return -EADDRNOTAVAIL;
  372. }
  373. return 0;
  374. }
  375. static size_t br_get_link_af_size(const struct net_device *dev)
  376. {
  377. struct net_port_vlans *pv;
  378. if (br_port_exists(dev))
  379. pv = nbp_get_vlan_info(br_port_get_rcu(dev));
  380. else if (dev->priv_flags & IFF_EBRIDGE)
  381. pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
  382. else
  383. return 0;
  384. if (!pv)
  385. return 0;
  386. /* Each VLAN is returned in bridge_vlan_info along with flags */
  387. return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
  388. }
  389. static struct rtnl_af_ops br_af_ops = {
  390. .family = AF_BRIDGE,
  391. .get_link_af_size = br_get_link_af_size,
  392. };
  393. struct rtnl_link_ops br_link_ops __read_mostly = {
  394. .kind = "bridge",
  395. .priv_size = sizeof(struct net_bridge),
  396. .setup = br_dev_setup,
  397. .validate = br_validate,
  398. .dellink = br_dev_delete,
  399. };
  400. int __init br_netlink_init(void)
  401. {
  402. int err;
  403. br_mdb_init();
  404. err = rtnl_af_register(&br_af_ops);
  405. if (err)
  406. goto out;
  407. err = rtnl_link_register(&br_link_ops);
  408. if (err)
  409. goto out_af;
  410. return 0;
  411. out_af:
  412. rtnl_af_unregister(&br_af_ops);
  413. out:
  414. br_mdb_uninit();
  415. return err;
  416. }
  417. void __exit br_netlink_fini(void)
  418. {
  419. br_mdb_uninit();
  420. rtnl_af_unregister(&br_af_ops);
  421. rtnl_link_unregister(&br_link_ops);
  422. }