br_netlink.c 12 KB

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