br_netlink.c 13 KB

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