br_netlink.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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, const struct net_bridge_port *port,
  61. u32 pid, u32 seq, int event, unsigned int flags)
  62. {
  63. const struct net_bridge *br = port->br;
  64. const struct net_device *dev = port->dev;
  65. struct ifinfomsg *hdr;
  66. struct nlmsghdr *nlh;
  67. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  68. br_debug(br, "br_fill_info event %d port %s master %s\n",
  69. event, dev->name, br->dev->name);
  70. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  71. if (nlh == NULL)
  72. return -EMSGSIZE;
  73. hdr = nlmsg_data(nlh);
  74. hdr->ifi_family = AF_BRIDGE;
  75. hdr->__ifi_pad = 0;
  76. hdr->ifi_type = dev->type;
  77. hdr->ifi_index = dev->ifindex;
  78. hdr->ifi_flags = dev_get_flags(dev);
  79. hdr->ifi_change = 0;
  80. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  81. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  82. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  83. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  84. (dev->addr_len &&
  85. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  86. (dev->ifindex != dev->iflink &&
  87. nla_put_u32(skb, IFLA_LINK, dev->iflink)))
  88. goto nla_put_failure;
  89. if (event == RTM_NEWLINK) {
  90. struct nlattr *nest
  91. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  92. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  93. goto nla_put_failure;
  94. nla_nest_end(skb, nest);
  95. }
  96. return nlmsg_end(skb, nlh);
  97. nla_put_failure:
  98. nlmsg_cancel(skb, nlh);
  99. return -EMSGSIZE;
  100. }
  101. /*
  102. * Notify listeners of a change in port information
  103. */
  104. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  105. {
  106. struct net *net;
  107. struct sk_buff *skb;
  108. int err = -ENOBUFS;
  109. if (!port)
  110. return;
  111. net = dev_net(port->dev);
  112. br_debug(port->br, "port %u(%s) event %d\n",
  113. (unsigned int)port->port_no, port->dev->name, event);
  114. skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
  115. if (skb == NULL)
  116. goto errout;
  117. err = br_fill_ifinfo(skb, port, 0, 0, event, 0);
  118. if (err < 0) {
  119. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  120. WARN_ON(err == -EMSGSIZE);
  121. kfree_skb(skb);
  122. goto errout;
  123. }
  124. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  125. return;
  126. errout:
  127. if (err < 0)
  128. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  129. }
  130. /*
  131. * Dump information about all ports, in response to GETLINK
  132. */
  133. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  134. struct net_device *dev)
  135. {
  136. int err = 0;
  137. struct net_bridge_port *port = br_port_get_rcu(dev);
  138. /* not a bridge port */
  139. if (!port)
  140. goto out;
  141. err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI);
  142. out:
  143. return err;
  144. }
  145. const struct nla_policy ifla_br_policy[IFLA_MAX+1] = {
  146. [IFLA_BRIDGE_FLAGS] = { .type = NLA_U16 },
  147. [IFLA_BRIDGE_MODE] = { .type = NLA_U16 },
  148. [IFLA_BRIDGE_VLAN_INFO] = { .type = NLA_BINARY,
  149. .len = sizeof(struct bridge_vlan_info), },
  150. };
  151. static int br_afspec(struct net_bridge *br,
  152. struct net_bridge_port *p,
  153. struct nlattr *af_spec,
  154. int cmd)
  155. {
  156. struct nlattr *tb[IFLA_BRIDGE_MAX+1];
  157. int err = 0;
  158. err = nla_parse_nested(tb, IFLA_BRIDGE_MAX, af_spec, ifla_br_policy);
  159. if (err)
  160. return err;
  161. if (tb[IFLA_BRIDGE_VLAN_INFO]) {
  162. struct bridge_vlan_info *vinfo;
  163. vinfo = nla_data(tb[IFLA_BRIDGE_VLAN_INFO]);
  164. if (vinfo->vid >= VLAN_N_VID)
  165. return -EINVAL;
  166. switch (cmd) {
  167. case RTM_SETLINK:
  168. if (p) {
  169. err = nbp_vlan_add(p, vinfo->vid);
  170. if (err)
  171. break;
  172. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  173. err = br_vlan_add(p->br, vinfo->vid);
  174. } else
  175. err = br_vlan_add(br, vinfo->vid);
  176. if (err)
  177. break;
  178. break;
  179. case RTM_DELLINK:
  180. if (p) {
  181. nbp_vlan_delete(p, vinfo->vid);
  182. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  183. br_vlan_delete(p->br, vinfo->vid);
  184. } else
  185. br_vlan_delete(br, vinfo->vid);
  186. break;
  187. }
  188. }
  189. return err;
  190. }
  191. static const struct nla_policy ifla_brport_policy[IFLA_BRPORT_MAX + 1] = {
  192. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  193. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  194. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  195. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  196. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  197. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  198. };
  199. /* Change the state of the port and notify spanning tree */
  200. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  201. {
  202. if (state > BR_STATE_BLOCKING)
  203. return -EINVAL;
  204. /* if kernel STP is running, don't allow changes */
  205. if (p->br->stp_enabled == BR_KERNEL_STP)
  206. return -EBUSY;
  207. /* if device is not up, change is not allowed
  208. * if link is not present, only allowable state is disabled
  209. */
  210. if (!netif_running(p->dev) ||
  211. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  212. return -ENETDOWN;
  213. p->state = state;
  214. br_log_state(p);
  215. br_port_state_selection(p->br);
  216. return 0;
  217. }
  218. /* Set/clear or port flags based on attribute */
  219. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  220. int attrtype, unsigned long mask)
  221. {
  222. if (tb[attrtype]) {
  223. u8 flag = nla_get_u8(tb[attrtype]);
  224. if (flag)
  225. p->flags |= mask;
  226. else
  227. p->flags &= ~mask;
  228. }
  229. }
  230. /* Process bridge protocol info on port */
  231. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  232. {
  233. int err;
  234. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  235. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  236. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  237. if (tb[IFLA_BRPORT_COST]) {
  238. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  239. if (err)
  240. return err;
  241. }
  242. if (tb[IFLA_BRPORT_PRIORITY]) {
  243. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  244. if (err)
  245. return err;
  246. }
  247. if (tb[IFLA_BRPORT_STATE]) {
  248. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  249. if (err)
  250. return err;
  251. }
  252. return 0;
  253. }
  254. /* Change state and parameters on port. */
  255. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh)
  256. {
  257. struct ifinfomsg *ifm;
  258. struct nlattr *protinfo;
  259. struct nlattr *afspec;
  260. struct net_bridge_port *p;
  261. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  262. int err;
  263. ifm = nlmsg_data(nlh);
  264. protinfo = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_PROTINFO);
  265. afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC);
  266. if (!protinfo && !afspec)
  267. return 0;
  268. p = br_port_get_rtnl(dev);
  269. /* We want to accept dev as bridge itself if the AF_SPEC
  270. * is set to see if someone is setting vlan info on the brigde
  271. */
  272. if (!p && ((dev->priv_flags & IFF_EBRIDGE) && !afspec))
  273. return -EINVAL;
  274. if (p && protinfo) {
  275. if (protinfo->nla_type & NLA_F_NESTED) {
  276. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  277. protinfo, ifla_brport_policy);
  278. if (err)
  279. return err;
  280. spin_lock_bh(&p->br->lock);
  281. err = br_setport(p, tb);
  282. spin_unlock_bh(&p->br->lock);
  283. } else {
  284. /* Binary compatability with old RSTP */
  285. if (nla_len(protinfo) < sizeof(u8))
  286. return -EINVAL;
  287. spin_lock_bh(&p->br->lock);
  288. err = br_set_port_state(p, nla_get_u8(protinfo));
  289. spin_unlock_bh(&p->br->lock);
  290. }
  291. if (err)
  292. goto out;
  293. }
  294. if (afspec) {
  295. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  296. afspec, RTM_SETLINK);
  297. }
  298. if (err == 0)
  299. br_ifinfo_notify(RTM_NEWLINK, p);
  300. out:
  301. return err;
  302. }
  303. /* Delete port information */
  304. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh)
  305. {
  306. struct ifinfomsg *ifm;
  307. struct nlattr *afspec;
  308. struct net_bridge_port *p;
  309. int err;
  310. ifm = nlmsg_data(nlh);
  311. afspec = nlmsg_find_attr(nlh, sizeof(*ifm), IFLA_AF_SPEC);
  312. if (!afspec)
  313. return 0;
  314. p = br_port_get_rtnl(dev);
  315. /* We want to accept dev as bridge itself as well */
  316. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  317. return -EINVAL;
  318. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  319. afspec, RTM_DELLINK);
  320. return err;
  321. }
  322. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  323. {
  324. if (tb[IFLA_ADDRESS]) {
  325. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  326. return -EINVAL;
  327. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  328. return -EADDRNOTAVAIL;
  329. }
  330. return 0;
  331. }
  332. struct rtnl_link_ops br_link_ops __read_mostly = {
  333. .kind = "bridge",
  334. .priv_size = sizeof(struct net_bridge),
  335. .setup = br_dev_setup,
  336. .validate = br_validate,
  337. .dellink = br_dev_delete,
  338. };
  339. int __init br_netlink_init(void)
  340. {
  341. int err;
  342. br_mdb_init();
  343. err = rtnl_link_register(&br_link_ops);
  344. if (err)
  345. goto out;
  346. return 0;
  347. out:
  348. br_mdb_uninit();
  349. return err;
  350. }
  351. void __exit br_netlink_fini(void)
  352. {
  353. br_mdb_uninit();
  354. rtnl_link_unregister(&br_link_ops);
  355. }