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>
9 #include <net/netlink.h>
10 #if IS_ENABLED(CONFIG_IPV6)
14 #include "br_private.h"
16 static int br_rports_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
17 struct net_device
*dev
)
19 struct net_bridge
*br
= netdev_priv(dev
);
20 struct net_bridge_port
*p
;
24 if (!br
->multicast_router
|| hlist_empty(&br
->router_list
))
27 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
31 hlist_for_each_entry_rcu(p
, n
, &br
->router_list
, rlist
) {
32 if (p
&& nla_put_u32(skb
, MDBA_ROUTER_PORT
, p
->dev
->ifindex
))
36 nla_nest_end(skb
, nest
);
39 nla_nest_cancel(skb
, nest
);
43 static int br_mdb_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
44 struct net_device
*dev
)
46 struct net_bridge
*br
= netdev_priv(dev
);
47 struct net_bridge_mdb_htable
*mdb
;
48 struct nlattr
*nest
, *nest2
;
50 int idx
= 0, s_idx
= cb
->args
[1];
52 if (br
->multicast_disabled
)
55 mdb
= rcu_dereference(br
->mdb
);
59 nest
= nla_nest_start(skb
, MDBA_MDB
);
63 for (i
= 0; i
< mdb
->max
; i
++) {
65 struct net_bridge_mdb_entry
*mp
;
66 struct net_bridge_port_group
*p
, **pp
;
67 struct net_bridge_port
*port
;
69 hlist_for_each_entry_rcu(mp
, h
, &mdb
->mhash
[i
], hlist
[mdb
->ver
]) {
73 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
80 (p
= rcu_dereference(*pp
)) != NULL
;
84 struct br_mdb_entry e
;
85 e
.ifindex
= port
->dev
->ifindex
;
87 if (p
->addr
.proto
== htons(ETH_P_IP
))
88 e
.addr
.u
.ip4
= p
->addr
.u
.ip4
;
89 #if IS_ENABLED(CONFIG_IPV6)
90 if (p
->addr
.proto
== htons(ETH_P_IPV6
))
91 e
.addr
.u
.ip6
= p
->addr
.u
.ip6
;
93 e
.addr
.proto
= p
->addr
.proto
;
94 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(e
), &e
)) {
95 nla_nest_cancel(skb
, nest2
);
101 nla_nest_end(skb
, nest2
);
109 nla_nest_end(skb
, nest
);
113 static int br_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
115 struct net_device
*dev
;
116 struct net
*net
= sock_net(skb
->sk
);
117 struct nlmsghdr
*nlh
= NULL
;
124 /* In theory this could be wrapped to 0... */
125 cb
->seq
= net
->dev_base_seq
+ br_mdb_rehash_seq
;
127 for_each_netdev_rcu(net
, dev
) {
128 if (dev
->priv_flags
& IFF_EBRIDGE
) {
129 struct br_port_msg
*bpm
;
134 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
135 cb
->nlh
->nlmsg_seq
, RTM_GETMDB
,
136 sizeof(*bpm
), NLM_F_MULTI
);
140 bpm
= nlmsg_data(nlh
);
141 bpm
->ifindex
= dev
->ifindex
;
142 if (br_mdb_fill_info(skb
, cb
, dev
) < 0)
144 if (br_rports_fill_info(skb
, cb
, dev
) < 0)
162 static int nlmsg_populate_mdb_fill(struct sk_buff
*skb
,
163 struct net_device
*dev
,
164 struct br_mdb_entry
*entry
, u32 pid
,
165 u32 seq
, int type
, unsigned int flags
)
167 struct nlmsghdr
*nlh
;
168 struct br_port_msg
*bpm
;
169 struct nlattr
*nest
, *nest2
;
171 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), NLM_F_MULTI
);
175 bpm
= nlmsg_data(nlh
);
176 bpm
->family
= AF_BRIDGE
;
177 bpm
->ifindex
= dev
->ifindex
;
178 nest
= nla_nest_start(skb
, MDBA_MDB
);
181 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
185 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(*entry
), entry
))
188 nla_nest_end(skb
, nest2
);
189 nla_nest_end(skb
, nest
);
190 return nlmsg_end(skb
, nlh
);
193 nla_nest_end(skb
, nest
);
195 nlmsg_cancel(skb
, nlh
);
199 static inline size_t rtnl_mdb_nlmsg_size(void)
201 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
202 + nla_total_size(sizeof(struct br_mdb_entry
));
205 static void __br_mdb_notify(struct net_device
*dev
, struct br_mdb_entry
*entry
,
208 struct net
*net
= dev_net(dev
);
212 skb
= nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC
);
216 err
= nlmsg_populate_mdb_fill(skb
, dev
, entry
, 0, 0, type
, NTF_SELF
);
222 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
225 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
228 void br_mdb_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
229 struct br_ip
*group
, int type
)
231 struct br_mdb_entry entry
;
233 entry
.ifindex
= port
->dev
->ifindex
;
234 entry
.addr
.proto
= group
->proto
;
235 entry
.addr
.u
.ip4
= group
->u
.ip4
;
236 #if IS_ENABLED(CONFIG_IPV6)
237 entry
.addr
.u
.ip6
= group
->u
.ip6
;
239 __br_mdb_notify(dev
, &entry
, type
);
242 static bool is_valid_mdb_entry(struct br_mdb_entry
*entry
)
244 if (entry
->ifindex
== 0)
247 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
248 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
))
250 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
))
252 #if IS_ENABLED(CONFIG_IPV6)
253 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
254 if (!ipv6_is_transient_multicast(&entry
->addr
.u
.ip6
))
259 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
)
265 static int br_mdb_parse(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
266 struct net_device
**pdev
, struct br_mdb_entry
**pentry
)
268 struct net
*net
= sock_net(skb
->sk
);
269 struct br_mdb_entry
*entry
;
270 struct br_port_msg
*bpm
;
271 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+1];
272 struct net_device
*dev
;
275 if (!capable(CAP_NET_ADMIN
))
278 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY
, NULL
);
282 bpm
= nlmsg_data(nlh
);
283 if (bpm
->ifindex
== 0) {
284 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
288 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
290 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
294 if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
295 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
301 if (!tb
[MDBA_SET_ENTRY
] ||
302 nla_len(tb
[MDBA_SET_ENTRY
]) != sizeof(struct br_mdb_entry
)) {
303 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
307 entry
= nla_data(tb
[MDBA_SET_ENTRY
]);
308 if (!is_valid_mdb_entry(entry
)) {
309 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
317 static int br_mdb_add_group(struct net_bridge
*br
, struct net_bridge_port
*port
,
318 struct br_ip
*group
, unsigned char state
)
320 struct net_bridge_mdb_entry
*mp
;
321 struct net_bridge_port_group
*p
;
322 struct net_bridge_port_group __rcu
**pp
;
323 struct net_bridge_mdb_htable
*mdb
;
326 mdb
= mlock_dereference(br
->mdb
, br
);
327 mp
= br_mdb_ip_get(mdb
, group
);
329 mp
= br_multicast_new_group(br
, port
, group
);
335 for (pp
= &mp
->ports
;
336 (p
= mlock_dereference(*pp
, br
)) != NULL
;
340 if ((unsigned long)p
->port
< (unsigned long)port
)
344 p
= br_multicast_new_port_group(port
, group
, *pp
, state
);
347 rcu_assign_pointer(*pp
, p
);
349 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
);
353 static int __br_mdb_add(struct net
*net
, struct net_bridge
*br
,
354 struct br_mdb_entry
*entry
)
357 struct net_device
*dev
;
358 struct net_bridge_port
*p
;
361 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
364 dev
= __dev_get_by_index(net
, entry
->ifindex
);
368 p
= br_port_get_rtnl(dev
);
369 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
372 ip
.proto
= entry
->addr
.proto
;
373 if (ip
.proto
== htons(ETH_P_IP
))
374 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
375 #if IS_ENABLED(CONFIG_IPV6)
377 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
380 spin_lock_bh(&br
->multicast_lock
);
381 ret
= br_mdb_add_group(br
, p
, &ip
, entry
->state
);
382 spin_unlock_bh(&br
->multicast_lock
);
386 static int br_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
388 struct net
*net
= sock_net(skb
->sk
);
389 struct br_mdb_entry
*entry
;
390 struct net_device
*dev
;
391 struct net_bridge
*br
;
394 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
398 br
= netdev_priv(dev
);
400 err
= __br_mdb_add(net
, br
, entry
);
402 __br_mdb_notify(dev
, entry
, RTM_NEWMDB
);
406 static int __br_mdb_del(struct net_bridge
*br
, struct br_mdb_entry
*entry
)
408 struct net_bridge_mdb_htable
*mdb
;
409 struct net_bridge_mdb_entry
*mp
;
410 struct net_bridge_port_group
*p
;
411 struct net_bridge_port_group __rcu
**pp
;
415 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
418 if (timer_pending(&br
->multicast_querier_timer
))
421 ip
.proto
= entry
->addr
.proto
;
422 if (ip
.proto
== htons(ETH_P_IP
))
423 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
424 #if IS_ENABLED(CONFIG_IPV6)
426 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
429 spin_lock_bh(&br
->multicast_lock
);
430 mdb
= mlock_dereference(br
->mdb
, br
);
432 mp
= br_mdb_ip_get(mdb
, &ip
);
436 for (pp
= &mp
->ports
;
437 (p
= mlock_dereference(*pp
, br
)) != NULL
;
439 if (!p
->port
|| p
->port
->dev
->ifindex
!= entry
->ifindex
)
442 if (p
->port
->state
== BR_STATE_DISABLED
)
445 rcu_assign_pointer(*pp
, p
->next
);
446 hlist_del_init(&p
->mglist
);
447 del_timer(&p
->timer
);
448 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
451 if (!mp
->ports
&& !mp
->mglist
&&
452 netif_running(br
->dev
))
453 mod_timer(&mp
->timer
, jiffies
);
458 spin_unlock_bh(&br
->multicast_lock
);
462 static int br_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
464 struct net_device
*dev
;
465 struct br_mdb_entry
*entry
;
466 struct net_bridge
*br
;
469 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
473 br
= netdev_priv(dev
);
475 err
= __br_mdb_del(br
, entry
);
477 __br_mdb_notify(dev
, entry
, RTM_DELMDB
);
481 void br_mdb_init(void)
483 rtnl_register(PF_BRIDGE
, RTM_GETMDB
, NULL
, br_mdb_dump
, NULL
);
484 rtnl_register(PF_BRIDGE
, RTM_NEWMDB
, br_mdb_add
, NULL
, NULL
);
485 rtnl_register(PF_BRIDGE
, RTM_DELMDB
, br_mdb_del
, NULL
, NULL
);
488 void br_mdb_uninit(void)
490 rtnl_unregister(PF_BRIDGE
, RTM_GETMDB
);
491 rtnl_unregister(PF_BRIDGE
, RTM_NEWMDB
);
492 rtnl_unregister(PF_BRIDGE
, RTM_DELMDB
);