br_mdb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #include <linux/err.h>
  2. #include <linux/igmp.h>
  3. #include <linux/kernel.h>
  4. #include <linux/netdevice.h>
  5. #include <linux/rculist.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/if_ether.h>
  8. #include <net/ip.h>
  9. #include <net/netlink.h>
  10. #if IS_ENABLED(CONFIG_IPV6)
  11. #include <net/ipv6.h>
  12. #include <net/addrconf.h>
  13. #endif
  14. #include "br_private.h"
  15. static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
  16. struct net_device *dev)
  17. {
  18. struct net_bridge *br = netdev_priv(dev);
  19. struct net_bridge_port *p;
  20. struct nlattr *nest;
  21. if (!br->multicast_router || hlist_empty(&br->router_list))
  22. return 0;
  23. nest = nla_nest_start(skb, MDBA_ROUTER);
  24. if (nest == NULL)
  25. return -EMSGSIZE;
  26. hlist_for_each_entry_rcu(p, &br->router_list, rlist) {
  27. if (p && nla_put_u32(skb, MDBA_ROUTER_PORT, p->dev->ifindex))
  28. goto fail;
  29. }
  30. nla_nest_end(skb, nest);
  31. return 0;
  32. fail:
  33. nla_nest_cancel(skb, nest);
  34. return -EMSGSIZE;
  35. }
  36. static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
  37. struct net_device *dev)
  38. {
  39. struct net_bridge *br = netdev_priv(dev);
  40. struct net_bridge_mdb_htable *mdb;
  41. struct nlattr *nest, *nest2;
  42. int i, err = 0;
  43. int idx = 0, s_idx = cb->args[1];
  44. if (br->multicast_disabled)
  45. return 0;
  46. mdb = rcu_dereference(br->mdb);
  47. if (!mdb)
  48. return 0;
  49. nest = nla_nest_start(skb, MDBA_MDB);
  50. if (nest == NULL)
  51. return -EMSGSIZE;
  52. for (i = 0; i < mdb->max; i++) {
  53. struct net_bridge_mdb_entry *mp;
  54. struct net_bridge_port_group *p;
  55. struct net_bridge_port_group __rcu **pp;
  56. struct net_bridge_port *port;
  57. hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) {
  58. if (idx < s_idx)
  59. goto skip;
  60. nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
  61. if (nest2 == NULL) {
  62. err = -EMSGSIZE;
  63. goto out;
  64. }
  65. for (pp = &mp->ports;
  66. (p = rcu_dereference(*pp)) != NULL;
  67. pp = &p->next) {
  68. port = p->port;
  69. if (port) {
  70. struct br_mdb_entry e;
  71. memset(&e, 0, sizeof(e));
  72. e.ifindex = port->dev->ifindex;
  73. e.state = p->state;
  74. e.vid = p->addr.vid;
  75. if (p->addr.proto == htons(ETH_P_IP))
  76. e.addr.u.ip4 = p->addr.u.ip4;
  77. #if IS_ENABLED(CONFIG_IPV6)
  78. if (p->addr.proto == htons(ETH_P_IPV6))
  79. e.addr.u.ip6 = p->addr.u.ip6;
  80. #endif
  81. e.addr.proto = p->addr.proto;
  82. if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(e), &e)) {
  83. nla_nest_cancel(skb, nest2);
  84. err = -EMSGSIZE;
  85. goto out;
  86. }
  87. }
  88. }
  89. nla_nest_end(skb, nest2);
  90. skip:
  91. idx++;
  92. }
  93. }
  94. out:
  95. cb->args[1] = idx;
  96. nla_nest_end(skb, nest);
  97. return err;
  98. }
  99. static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
  100. {
  101. struct net_device *dev;
  102. struct net *net = sock_net(skb->sk);
  103. struct nlmsghdr *nlh = NULL;
  104. int idx = 0, s_idx;
  105. s_idx = cb->args[0];
  106. rcu_read_lock();
  107. /* In theory this could be wrapped to 0... */
  108. cb->seq = net->dev_base_seq + br_mdb_rehash_seq;
  109. for_each_netdev_rcu(net, dev) {
  110. if (dev->priv_flags & IFF_EBRIDGE) {
  111. struct br_port_msg *bpm;
  112. if (idx < s_idx)
  113. goto skip;
  114. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  115. cb->nlh->nlmsg_seq, RTM_GETMDB,
  116. sizeof(*bpm), NLM_F_MULTI);
  117. if (nlh == NULL)
  118. break;
  119. bpm = nlmsg_data(nlh);
  120. memset(bpm, 0, sizeof(*bpm));
  121. bpm->ifindex = dev->ifindex;
  122. if (br_mdb_fill_info(skb, cb, dev) < 0)
  123. goto out;
  124. if (br_rports_fill_info(skb, cb, dev) < 0)
  125. goto out;
  126. cb->args[1] = 0;
  127. nlmsg_end(skb, nlh);
  128. skip:
  129. idx++;
  130. }
  131. }
  132. out:
  133. if (nlh)
  134. nlmsg_end(skb, nlh);
  135. rcu_read_unlock();
  136. cb->args[0] = idx;
  137. return skb->len;
  138. }
  139. static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
  140. struct net_device *dev,
  141. struct br_mdb_entry *entry, u32 pid,
  142. u32 seq, int type, unsigned int flags)
  143. {
  144. struct nlmsghdr *nlh;
  145. struct br_port_msg *bpm;
  146. struct nlattr *nest, *nest2;
  147. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
  148. if (!nlh)
  149. return -EMSGSIZE;
  150. bpm = nlmsg_data(nlh);
  151. memset(bpm, 0, sizeof(*bpm));
  152. bpm->family = AF_BRIDGE;
  153. bpm->ifindex = dev->ifindex;
  154. nest = nla_nest_start(skb, MDBA_MDB);
  155. if (nest == NULL)
  156. goto cancel;
  157. nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
  158. if (nest2 == NULL)
  159. goto end;
  160. if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
  161. goto end;
  162. nla_nest_end(skb, nest2);
  163. nla_nest_end(skb, nest);
  164. nlmsg_end(skb, nlh);
  165. return 0;
  166. end:
  167. nla_nest_end(skb, nest);
  168. cancel:
  169. nlmsg_cancel(skb, nlh);
  170. return -EMSGSIZE;
  171. }
  172. static inline size_t rtnl_mdb_nlmsg_size(void)
  173. {
  174. return NLMSG_ALIGN(sizeof(struct br_port_msg))
  175. + nla_total_size(sizeof(struct br_mdb_entry));
  176. }
  177. static void __br_mdb_notify(struct net_device *dev, struct br_mdb_entry *entry,
  178. int type)
  179. {
  180. struct net *net = dev_net(dev);
  181. struct sk_buff *skb;
  182. int err = -ENOBUFS;
  183. skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
  184. if (!skb)
  185. goto errout;
  186. err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
  187. if (err < 0) {
  188. kfree_skb(skb);
  189. goto errout;
  190. }
  191. rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
  192. return;
  193. errout:
  194. rtnl_set_sk_err(net, RTNLGRP_MDB, err);
  195. }
  196. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  197. struct br_ip *group, int type, u8 state)
  198. {
  199. struct br_mdb_entry entry;
  200. memset(&entry, 0, sizeof(entry));
  201. entry.ifindex = port->dev->ifindex;
  202. entry.addr.proto = group->proto;
  203. entry.addr.u.ip4 = group->u.ip4;
  204. #if IS_ENABLED(CONFIG_IPV6)
  205. entry.addr.u.ip6 = group->u.ip6;
  206. #endif
  207. entry.state = state;
  208. entry.vid = group->vid;
  209. __br_mdb_notify(dev, &entry, type);
  210. }
  211. static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
  212. struct net_device *dev,
  213. int ifindex, u32 pid,
  214. u32 seq, int type, unsigned int flags)
  215. {
  216. struct br_port_msg *bpm;
  217. struct nlmsghdr *nlh;
  218. struct nlattr *nest;
  219. nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI);
  220. if (!nlh)
  221. return -EMSGSIZE;
  222. bpm = nlmsg_data(nlh);
  223. memset(bpm, 0, sizeof(*bpm));
  224. bpm->family = AF_BRIDGE;
  225. bpm->ifindex = dev->ifindex;
  226. nest = nla_nest_start(skb, MDBA_ROUTER);
  227. if (!nest)
  228. goto cancel;
  229. if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex))
  230. goto end;
  231. nla_nest_end(skb, nest);
  232. nlmsg_end(skb, nlh);
  233. return 0;
  234. end:
  235. nla_nest_end(skb, nest);
  236. cancel:
  237. nlmsg_cancel(skb, nlh);
  238. return -EMSGSIZE;
  239. }
  240. static inline size_t rtnl_rtr_nlmsg_size(void)
  241. {
  242. return NLMSG_ALIGN(sizeof(struct br_port_msg))
  243. + nla_total_size(sizeof(__u32));
  244. }
  245. void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
  246. int type)
  247. {
  248. struct net *net = dev_net(dev);
  249. struct sk_buff *skb;
  250. int err = -ENOBUFS;
  251. int ifindex;
  252. ifindex = port ? port->dev->ifindex : 0;
  253. skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
  254. if (!skb)
  255. goto errout;
  256. err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF);
  257. if (err < 0) {
  258. kfree_skb(skb);
  259. goto errout;
  260. }
  261. rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
  262. return;
  263. errout:
  264. rtnl_set_sk_err(net, RTNLGRP_MDB, err);
  265. }
  266. static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
  267. {
  268. if (entry->ifindex == 0)
  269. return false;
  270. if (entry->addr.proto == htons(ETH_P_IP)) {
  271. if (!ipv4_is_multicast(entry->addr.u.ip4))
  272. return false;
  273. if (ipv4_is_local_multicast(entry->addr.u.ip4))
  274. return false;
  275. #if IS_ENABLED(CONFIG_IPV6)
  276. } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
  277. if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
  278. return false;
  279. #endif
  280. } else
  281. return false;
  282. if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
  283. return false;
  284. if (entry->vid >= VLAN_VID_MASK)
  285. return false;
  286. return true;
  287. }
  288. static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
  289. struct net_device **pdev, struct br_mdb_entry **pentry)
  290. {
  291. struct net *net = sock_net(skb->sk);
  292. struct br_mdb_entry *entry;
  293. struct br_port_msg *bpm;
  294. struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
  295. struct net_device *dev;
  296. int err;
  297. err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL);
  298. if (err < 0)
  299. return err;
  300. bpm = nlmsg_data(nlh);
  301. if (bpm->ifindex == 0) {
  302. pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
  303. return -EINVAL;
  304. }
  305. dev = __dev_get_by_index(net, bpm->ifindex);
  306. if (dev == NULL) {
  307. pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
  308. return -ENODEV;
  309. }
  310. if (!(dev->priv_flags & IFF_EBRIDGE)) {
  311. pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
  312. return -EOPNOTSUPP;
  313. }
  314. *pdev = dev;
  315. if (!tb[MDBA_SET_ENTRY] ||
  316. nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
  317. pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
  318. return -EINVAL;
  319. }
  320. entry = nla_data(tb[MDBA_SET_ENTRY]);
  321. if (!is_valid_mdb_entry(entry)) {
  322. pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
  323. return -EINVAL;
  324. }
  325. *pentry = entry;
  326. return 0;
  327. }
  328. static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
  329. struct br_ip *group, unsigned char state)
  330. {
  331. struct net_bridge_mdb_entry *mp;
  332. struct net_bridge_port_group *p;
  333. struct net_bridge_port_group __rcu **pp;
  334. struct net_bridge_mdb_htable *mdb;
  335. unsigned long now = jiffies;
  336. int err;
  337. mdb = mlock_dereference(br->mdb, br);
  338. mp = br_mdb_ip_get(mdb, group);
  339. if (!mp) {
  340. mp = br_multicast_new_group(br, port, group);
  341. err = PTR_ERR(mp);
  342. if (IS_ERR(mp))
  343. return err;
  344. }
  345. for (pp = &mp->ports;
  346. (p = mlock_dereference(*pp, br)) != NULL;
  347. pp = &p->next) {
  348. if (p->port == port)
  349. return -EEXIST;
  350. if ((unsigned long)p->port < (unsigned long)port)
  351. break;
  352. }
  353. p = br_multicast_new_port_group(port, group, *pp, state);
  354. if (unlikely(!p))
  355. return -ENOMEM;
  356. rcu_assign_pointer(*pp, p);
  357. if (state == MDB_TEMPORARY)
  358. mod_timer(&p->timer, now + br->multicast_membership_interval);
  359. return 0;
  360. }
  361. static int __br_mdb_add(struct net *net, struct net_bridge *br,
  362. struct br_mdb_entry *entry)
  363. {
  364. struct br_ip ip;
  365. struct net_device *dev;
  366. struct net_bridge_port *p;
  367. int ret;
  368. if (!netif_running(br->dev) || br->multicast_disabled)
  369. return -EINVAL;
  370. dev = __dev_get_by_index(net, entry->ifindex);
  371. if (!dev)
  372. return -ENODEV;
  373. p = br_port_get_rtnl(dev);
  374. if (!p || p->br != br || p->state == BR_STATE_DISABLED)
  375. return -EINVAL;
  376. memset(&ip, 0, sizeof(ip));
  377. ip.vid = entry->vid;
  378. ip.proto = entry->addr.proto;
  379. if (ip.proto == htons(ETH_P_IP))
  380. ip.u.ip4 = entry->addr.u.ip4;
  381. #if IS_ENABLED(CONFIG_IPV6)
  382. else
  383. ip.u.ip6 = entry->addr.u.ip6;
  384. #endif
  385. spin_lock_bh(&br->multicast_lock);
  386. ret = br_mdb_add_group(br, p, &ip, entry->state);
  387. spin_unlock_bh(&br->multicast_lock);
  388. return ret;
  389. }
  390. static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
  391. {
  392. struct net *net = sock_net(skb->sk);
  393. struct net_bridge_vlan_group *vg;
  394. struct net_device *dev, *pdev;
  395. struct br_mdb_entry *entry;
  396. struct net_bridge_port *p;
  397. struct net_bridge_vlan *v;
  398. struct net_bridge *br;
  399. int err;
  400. err = br_mdb_parse(skb, nlh, &dev, &entry);
  401. if (err < 0)
  402. return err;
  403. br = netdev_priv(dev);
  404. /* If vlan filtering is enabled and VLAN is not specified
  405. * install mdb entry on all vlans configured on the port.
  406. */
  407. pdev = __dev_get_by_index(net, entry->ifindex);
  408. if (!pdev)
  409. return -ENODEV;
  410. p = br_port_get_rtnl(pdev);
  411. if (!p || p->br != br || p->state == BR_STATE_DISABLED)
  412. return -EINVAL;
  413. vg = nbp_vlan_group(p);
  414. if (br_vlan_enabled(br) && vg && entry->vid == 0) {
  415. list_for_each_entry(v, &vg->vlan_list, vlist) {
  416. entry->vid = v->vid;
  417. err = __br_mdb_add(net, br, entry);
  418. if (err)
  419. break;
  420. __br_mdb_notify(dev, entry, RTM_NEWMDB);
  421. }
  422. } else {
  423. err = __br_mdb_add(net, br, entry);
  424. if (!err)
  425. __br_mdb_notify(dev, entry, RTM_NEWMDB);
  426. }
  427. return err;
  428. }
  429. static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
  430. {
  431. struct net_bridge_mdb_htable *mdb;
  432. struct net_bridge_mdb_entry *mp;
  433. struct net_bridge_port_group *p;
  434. struct net_bridge_port_group __rcu **pp;
  435. struct br_ip ip;
  436. int err = -EINVAL;
  437. if (!netif_running(br->dev) || br->multicast_disabled)
  438. return -EINVAL;
  439. memset(&ip, 0, sizeof(ip));
  440. ip.vid = entry->vid;
  441. ip.proto = entry->addr.proto;
  442. if (ip.proto == htons(ETH_P_IP))
  443. ip.u.ip4 = entry->addr.u.ip4;
  444. #if IS_ENABLED(CONFIG_IPV6)
  445. else
  446. ip.u.ip6 = entry->addr.u.ip6;
  447. #endif
  448. spin_lock_bh(&br->multicast_lock);
  449. mdb = mlock_dereference(br->mdb, br);
  450. mp = br_mdb_ip_get(mdb, &ip);
  451. if (!mp)
  452. goto unlock;
  453. for (pp = &mp->ports;
  454. (p = mlock_dereference(*pp, br)) != NULL;
  455. pp = &p->next) {
  456. if (!p->port || p->port->dev->ifindex != entry->ifindex)
  457. continue;
  458. if (p->port->state == BR_STATE_DISABLED)
  459. goto unlock;
  460. entry->state = p->state;
  461. rcu_assign_pointer(*pp, p->next);
  462. hlist_del_init(&p->mglist);
  463. del_timer(&p->timer);
  464. call_rcu_bh(&p->rcu, br_multicast_free_pg);
  465. err = 0;
  466. if (!mp->ports && !mp->mglist &&
  467. netif_running(br->dev))
  468. mod_timer(&mp->timer, jiffies);
  469. break;
  470. }
  471. unlock:
  472. spin_unlock_bh(&br->multicast_lock);
  473. return err;
  474. }
  475. static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
  476. {
  477. struct net *net = sock_net(skb->sk);
  478. struct net_bridge_vlan_group *vg;
  479. struct net_device *dev, *pdev;
  480. struct br_mdb_entry *entry;
  481. struct net_bridge_port *p;
  482. struct net_bridge_vlan *v;
  483. struct net_bridge *br;
  484. int err;
  485. err = br_mdb_parse(skb, nlh, &dev, &entry);
  486. if (err < 0)
  487. return err;
  488. br = netdev_priv(dev);
  489. /* If vlan filtering is enabled and VLAN is not specified
  490. * delete mdb entry on all vlans configured on the port.
  491. */
  492. pdev = __dev_get_by_index(net, entry->ifindex);
  493. if (!pdev)
  494. return -ENODEV;
  495. p = br_port_get_rtnl(pdev);
  496. if (!p || p->br != br || p->state == BR_STATE_DISABLED)
  497. return -EINVAL;
  498. vg = nbp_vlan_group(p);
  499. if (br_vlan_enabled(br) && vg && entry->vid == 0) {
  500. list_for_each_entry(v, &vg->vlan_list, vlist) {
  501. entry->vid = v->vid;
  502. err = __br_mdb_del(br, entry);
  503. if (!err)
  504. __br_mdb_notify(dev, entry, RTM_DELMDB);
  505. }
  506. } else {
  507. err = __br_mdb_del(br, entry);
  508. if (!err)
  509. __br_mdb_notify(dev, entry, RTM_DELMDB);
  510. }
  511. return err;
  512. }
  513. void br_mdb_init(void)
  514. {
  515. rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, NULL);
  516. rtnl_register(PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, NULL);
  517. rtnl_register(PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, NULL);
  518. }
  519. void br_mdb_uninit(void)
  520. {
  521. rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
  522. rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
  523. rtnl_unregister(PF_BRIDGE, RTM_DELMDB);
  524. }