br_netlink.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_each_set_bit(vid, pv->vlan_bitmap, BR_VLAN_BITMAP_LEN) {
  120. vinfo.vid = vid;
  121. vinfo.flags = 0;
  122. if (vid == pvid)
  123. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  124. if (test_bit(vid, pv->untagged_bitmap))
  125. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  126. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  127. sizeof(vinfo), &vinfo))
  128. goto nla_put_failure;
  129. }
  130. nla_nest_end(skb, af);
  131. }
  132. done:
  133. return nlmsg_end(skb, nlh);
  134. nla_put_failure:
  135. nlmsg_cancel(skb, nlh);
  136. return -EMSGSIZE;
  137. }
  138. /*
  139. * Notify listeners of a change in port information
  140. */
  141. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  142. {
  143. struct net *net;
  144. struct sk_buff *skb;
  145. int err = -ENOBUFS;
  146. if (!port)
  147. return;
  148. net = dev_net(port->dev);
  149. br_debug(port->br, "port %u(%s) event %d\n",
  150. (unsigned int)port->port_no, port->dev->name, event);
  151. skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
  152. if (skb == NULL)
  153. goto errout;
  154. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, 0, port->dev);
  155. if (err < 0) {
  156. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  157. WARN_ON(err == -EMSGSIZE);
  158. kfree_skb(skb);
  159. goto errout;
  160. }
  161. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  162. return;
  163. errout:
  164. if (err < 0)
  165. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  166. }
  167. /*
  168. * Dump information about all ports, in response to GETLINK
  169. */
  170. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  171. struct net_device *dev, u32 filter_mask)
  172. {
  173. int err = 0;
  174. struct net_bridge_port *port = br_port_get_rcu(dev);
  175. /* not a bridge port and */
  176. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN))
  177. goto out;
  178. err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI,
  179. filter_mask, dev);
  180. out:
  181. return err;
  182. }
  183. static const struct nla_policy ifla_br_policy[IFLA_MAX+1] = {
  184. [IFLA_BRIDGE_FLAGS] = { .type = NLA_U16 },
  185. [IFLA_BRIDGE_MODE] = { .type = NLA_U16 },
  186. [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
  187. .len = sizeof(struct bridge_vlan_info), },
  188. };
  189. static int br_afspec(struct net_bridge *br,
  190. struct net_bridge_port *p,
  191. struct nlattr *af_spec,
  192. int cmd)
  193. {
  194. struct nlattr *tb[IFLA_BRIDGE_MAX+1];
  195. int err = 0;
  196. err = nla_parse_nested(tb, IFLA_BRIDGE_MAX, af_spec, ifla_br_policy);
  197. if (err)
  198. return err;
  199. if (tb[IFLA_BRIDGE_VLAN_INFO]) {
  200. struct bridge_vlan_info *vinfo;
  201. vinfo = nla_data(tb[IFLA_BRIDGE_VLAN_INFO]);
  202. if (vinfo->vid >= VLAN_N_VID)
  203. return -EINVAL;
  204. switch (cmd) {
  205. case RTM_SETLINK:
  206. if (p) {
  207. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
  208. if (err)
  209. break;
  210. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  211. err = br_vlan_add(p->br, vinfo->vid,
  212. vinfo->flags);
  213. } else
  214. err = br_vlan_add(br, vinfo->vid, vinfo->flags);
  215. if (err)
  216. break;
  217. break;
  218. case RTM_DELLINK:
  219. if (p) {
  220. nbp_vlan_delete(p, vinfo->vid);
  221. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  222. br_vlan_delete(p->br, vinfo->vid);
  223. } else
  224. br_vlan_delete(br, vinfo->vid);
  225. break;
  226. }
  227. }
  228. return err;
  229. }
  230. static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = {
  231. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  232. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  233. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  234. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  235. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  236. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  237. };
  238. /* Change the state of the port and notify spanning tree */
  239. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  240. {
  241. if (state > BR_STATE_BLOCKING)
  242. return -EINVAL;
  243. /* if kernel STP is running, don't allow changes */
  244. if (p->br->stp_enabled == BR_KERNEL_STP)
  245. return -EBUSY;
  246. /* if device is not up, change is not allowed
  247. * if link is not present, only allowable state is disabled
  248. */
  249. if (!netif_running(p->dev) ||
  250. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  251. return -ENETDOWN;
  252. p->state = state;
  253. br_log_state(p);
  254. br_port_state_selection(p->br);
  255. return 0;
  256. }
  257. /* Set/clear or port flags based on attribute */
  258. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  259. int attrtype, unsigned long mask)
  260. {
  261. if (tb[attrtype]) {
  262. u8 flag = nla_get_u8(tb[attrtype]);
  263. if (flag)
  264. p->flags |= mask;
  265. else
  266. p->flags &= ~mask;
  267. }
  268. }
  269. /* Process bridge protocol info on port */
  270. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  271. {
  272. int err;
  273. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  274. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  275. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  276. br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  277. if (tb[IFLA_BRPORT_COST]) {
  278. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  279. if (err)
  280. return err;
  281. }
  282. if (tb[IFLA_BRPORT_PRIORITY]) {
  283. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  284. if (err)
  285. return err;
  286. }
  287. if (tb[IFLA_BRPORT_STATE]) {
  288. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  289. if (err)
  290. return err;
  291. }
  292. return 0;
  293. }
  294. /* Change state and parameters on port. */
  295. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh)
  296. {
  297. struct nlattr *protinfo;
  298. struct nlattr *afspec;
  299. struct net_bridge_port *p;
  300. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  301. int err = 0;
  302. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
  303. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  304. if (!protinfo && !afspec)
  305. return 0;
  306. p = br_port_get_rtnl(dev);
  307. /* We want to accept dev as bridge itself if the AF_SPEC
  308. * is set to see if someone is setting vlan info on the brigde
  309. */
  310. if (!p && !afspec)
  311. return -EINVAL;
  312. if (p && protinfo) {
  313. if (protinfo->nla_type & NLA_F_NESTED) {
  314. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  315. protinfo, ifla_brport_policy);
  316. if (err)
  317. return err;
  318. spin_lock_bh(&p->br->lock);
  319. err = br_setport(p, tb);
  320. spin_unlock_bh(&p->br->lock);
  321. } else {
  322. /* Binary compatability with old RSTP */
  323. if (nla_len(protinfo) < sizeof(u8))
  324. return -EINVAL;
  325. spin_lock_bh(&p->br->lock);
  326. err = br_set_port_state(p, nla_get_u8(protinfo));
  327. spin_unlock_bh(&p->br->lock);
  328. }
  329. if (err)
  330. goto out;
  331. }
  332. if (afspec) {
  333. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  334. afspec, RTM_SETLINK);
  335. }
  336. if (err == 0)
  337. br_ifinfo_notify(RTM_NEWLINK, p);
  338. out:
  339. return err;
  340. }
  341. /* Delete port information */
  342. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh)
  343. {
  344. struct nlattr *afspec;
  345. struct net_bridge_port *p;
  346. int err;
  347. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  348. if (!afspec)
  349. return 0;
  350. p = br_port_get_rtnl(dev);
  351. /* We want to accept dev as bridge itself as well */
  352. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  353. return -EINVAL;
  354. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  355. afspec, RTM_DELLINK);
  356. return err;
  357. }
  358. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  359. {
  360. if (tb[IFLA_ADDRESS]) {
  361. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  362. return -EINVAL;
  363. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  364. return -EADDRNOTAVAIL;
  365. }
  366. return 0;
  367. }
  368. static size_t br_get_link_af_size(const struct net_device *dev)
  369. {
  370. struct net_port_vlans *pv;
  371. if (br_port_exists(dev))
  372. pv = nbp_get_vlan_info(br_port_get_rcu(dev));
  373. else if (dev->priv_flags & IFF_EBRIDGE)
  374. pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
  375. else
  376. return 0;
  377. if (!pv)
  378. return 0;
  379. /* Each VLAN is returned in bridge_vlan_info along with flags */
  380. return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
  381. }
  382. static struct rtnl_af_ops br_af_ops = {
  383. .family = AF_BRIDGE,
  384. .get_link_af_size = br_get_link_af_size,
  385. };
  386. struct rtnl_link_ops br_link_ops __read_mostly = {
  387. .kind = "bridge",
  388. .priv_size = sizeof(struct net_bridge),
  389. .setup = br_dev_setup,
  390. .validate = br_validate,
  391. .dellink = br_dev_delete,
  392. };
  393. int __init br_netlink_init(void)
  394. {
  395. int err;
  396. br_mdb_init();
  397. err = rtnl_af_register(&br_af_ops);
  398. if (err)
  399. goto out;
  400. err = rtnl_link_register(&br_link_ops);
  401. if (err)
  402. goto out_af;
  403. return 0;
  404. out_af:
  405. rtnl_af_unregister(&br_af_ops);
  406. out:
  407. br_mdb_uninit();
  408. return err;
  409. }
  410. void __exit br_netlink_fini(void)
  411. {
  412. br_mdb_uninit();
  413. rtnl_af_unregister(&br_af_ops);
  414. rtnl_link_unregister(&br_link_ops);
  415. }