br_mdb.c 18 KB

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