br_mdb.c 17 KB

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