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)
12 #include <net/addrconf.h>
15 #include "br_private.h"
17 static int br_rports_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
18 struct net_device
*dev
)
20 struct net_bridge
*br
= netdev_priv(dev
);
21 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
, &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
++) {
64 struct net_bridge_mdb_entry
*mp
;
65 struct net_bridge_port_group
*p
;
66 struct net_bridge_port_group __rcu
**pp
;
67 struct net_bridge_port
*port
;
69 hlist_for_each_entry_rcu(mp
, &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 memset(&e
, 0, sizeof(e
));
86 e
.ifindex
= port
->dev
->ifindex
;
88 if (p
->addr
.proto
== htons(ETH_P_IP
))
89 e
.addr
.u
.ip4
= p
->addr
.u
.ip4
;
90 #if IS_ENABLED(CONFIG_IPV6)
91 if (p
->addr
.proto
== htons(ETH_P_IPV6
))
92 e
.addr
.u
.ip6
= p
->addr
.u
.ip6
;
94 e
.addr
.proto
= p
->addr
.proto
;
95 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(e
), &e
)) {
96 nla_nest_cancel(skb
, nest2
);
102 nla_nest_end(skb
, nest2
);
110 nla_nest_end(skb
, nest
);
114 static int br_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
116 struct net_device
*dev
;
117 struct net
*net
= sock_net(skb
->sk
);
118 struct nlmsghdr
*nlh
= NULL
;
125 /* In theory this could be wrapped to 0... */
126 cb
->seq
= net
->dev_base_seq
+ br_mdb_rehash_seq
;
128 for_each_netdev_rcu(net
, dev
) {
129 if (dev
->priv_flags
& IFF_EBRIDGE
) {
130 struct br_port_msg
*bpm
;
135 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
136 cb
->nlh
->nlmsg_seq
, RTM_GETMDB
,
137 sizeof(*bpm
), NLM_F_MULTI
);
141 bpm
= nlmsg_data(nlh
);
142 memset(bpm
, 0, sizeof(*bpm
));
143 bpm
->ifindex
= dev
->ifindex
;
144 if (br_mdb_fill_info(skb
, cb
, dev
) < 0)
146 if (br_rports_fill_info(skb
, cb
, dev
) < 0)
164 static int nlmsg_populate_mdb_fill(struct sk_buff
*skb
,
165 struct net_device
*dev
,
166 struct br_mdb_entry
*entry
, u32 pid
,
167 u32 seq
, int type
, unsigned int flags
)
169 struct nlmsghdr
*nlh
;
170 struct br_port_msg
*bpm
;
171 struct nlattr
*nest
, *nest2
;
173 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), 0);
177 bpm
= nlmsg_data(nlh
);
178 memset(bpm
, 0, sizeof(*bpm
));
179 bpm
->family
= AF_BRIDGE
;
180 bpm
->ifindex
= dev
->ifindex
;
181 nest
= nla_nest_start(skb
, MDBA_MDB
);
184 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
188 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(*entry
), entry
))
191 nla_nest_end(skb
, nest2
);
192 nla_nest_end(skb
, nest
);
197 nla_nest_end(skb
, nest
);
199 nlmsg_cancel(skb
, nlh
);
203 static inline size_t rtnl_mdb_nlmsg_size(void)
205 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
206 + nla_total_size(sizeof(struct br_mdb_entry
));
209 static void __br_mdb_notify(struct net_device
*dev
, struct br_mdb_entry
*entry
,
212 struct net
*net
= dev_net(dev
);
216 skb
= nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC
);
220 err
= nlmsg_populate_mdb_fill(skb
, dev
, entry
, 0, 0, type
, NTF_SELF
);
226 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
229 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
232 void br_mdb_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
233 struct br_ip
*group
, int type
)
235 struct br_mdb_entry entry
;
237 memset(&entry
, 0, sizeof(entry
));
238 entry
.ifindex
= port
->dev
->ifindex
;
239 entry
.addr
.proto
= group
->proto
;
240 entry
.addr
.u
.ip4
= group
->u
.ip4
;
241 #if IS_ENABLED(CONFIG_IPV6)
242 entry
.addr
.u
.ip6
= group
->u
.ip6
;
244 __br_mdb_notify(dev
, &entry
, type
);
247 static bool is_valid_mdb_entry(struct br_mdb_entry
*entry
)
249 if (entry
->ifindex
== 0)
252 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
253 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
))
255 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
))
257 #if IS_ENABLED(CONFIG_IPV6)
258 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
259 if (ipv6_addr_is_ll_all_nodes(&entry
->addr
.u
.ip6
))
264 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
)
270 static int br_mdb_parse(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
271 struct net_device
**pdev
, struct br_mdb_entry
**pentry
)
273 struct net
*net
= sock_net(skb
->sk
);
274 struct br_mdb_entry
*entry
;
275 struct br_port_msg
*bpm
;
276 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+1];
277 struct net_device
*dev
;
280 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY_MAX
, NULL
);
284 bpm
= nlmsg_data(nlh
);
285 if (bpm
->ifindex
== 0) {
286 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
290 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
292 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
296 if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
297 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
303 if (!tb
[MDBA_SET_ENTRY
] ||
304 nla_len(tb
[MDBA_SET_ENTRY
]) != sizeof(struct br_mdb_entry
)) {
305 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
309 entry
= nla_data(tb
[MDBA_SET_ENTRY
]);
310 if (!is_valid_mdb_entry(entry
)) {
311 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
319 static int br_mdb_add_group(struct net_bridge
*br
, struct net_bridge_port
*port
,
320 struct br_ip
*group
, unsigned char state
)
322 struct net_bridge_mdb_entry
*mp
;
323 struct net_bridge_port_group
*p
;
324 struct net_bridge_port_group __rcu
**pp
;
325 struct net_bridge_mdb_htable
*mdb
;
326 unsigned long now
= jiffies
;
329 mdb
= mlock_dereference(br
->mdb
, br
);
330 mp
= br_mdb_ip_get(mdb
, group
);
332 mp
= br_multicast_new_group(br
, port
, group
);
338 for (pp
= &mp
->ports
;
339 (p
= mlock_dereference(*pp
, br
)) != NULL
;
343 if ((unsigned long)p
->port
< (unsigned long)port
)
347 p
= br_multicast_new_port_group(port
, group
, *pp
, state
);
350 rcu_assign_pointer(*pp
, p
);
351 if (state
== MDB_TEMPORARY
)
352 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
357 static int __br_mdb_add(struct net
*net
, struct net_bridge
*br
,
358 struct br_mdb_entry
*entry
)
361 struct net_device
*dev
;
362 struct net_bridge_port
*p
;
365 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
368 dev
= __dev_get_by_index(net
, entry
->ifindex
);
372 p
= br_port_get_rtnl(dev
);
373 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
376 memset(&ip
, 0, sizeof(ip
));
377 ip
.proto
= entry
->addr
.proto
;
378 if (ip
.proto
== htons(ETH_P_IP
))
379 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
380 #if IS_ENABLED(CONFIG_IPV6)
382 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
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
);
391 static int br_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
393 struct net
*net
= sock_net(skb
->sk
);
394 struct br_mdb_entry
*entry
;
395 struct net_device
*dev
;
396 struct net_bridge
*br
;
399 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
403 br
= netdev_priv(dev
);
405 err
= __br_mdb_add(net
, br
, entry
);
407 __br_mdb_notify(dev
, entry
, RTM_NEWMDB
);
411 static int __br_mdb_del(struct net_bridge
*br
, struct br_mdb_entry
*entry
)
413 struct net_bridge_mdb_htable
*mdb
;
414 struct net_bridge_mdb_entry
*mp
;
415 struct net_bridge_port_group
*p
;
416 struct net_bridge_port_group __rcu
**pp
;
420 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
423 memset(&ip
, 0, sizeof(ip
));
424 ip
.proto
= entry
->addr
.proto
;
425 if (ip
.proto
== htons(ETH_P_IP
))
426 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
427 #if IS_ENABLED(CONFIG_IPV6)
429 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
432 spin_lock_bh(&br
->multicast_lock
);
433 mdb
= mlock_dereference(br
->mdb
, br
);
435 mp
= br_mdb_ip_get(mdb
, &ip
);
439 for (pp
= &mp
->ports
;
440 (p
= mlock_dereference(*pp
, br
)) != NULL
;
442 if (!p
->port
|| p
->port
->dev
->ifindex
!= entry
->ifindex
)
445 if (p
->port
->state
== BR_STATE_DISABLED
)
448 entry
->state
= p
->state
;
449 rcu_assign_pointer(*pp
, p
->next
);
450 hlist_del_init(&p
->mglist
);
451 del_timer(&p
->timer
);
452 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
455 if (!mp
->ports
&& !mp
->mglist
&&
456 netif_running(br
->dev
))
457 mod_timer(&mp
->timer
, jiffies
);
462 spin_unlock_bh(&br
->multicast_lock
);
466 static int br_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
468 struct net_device
*dev
;
469 struct br_mdb_entry
*entry
;
470 struct net_bridge
*br
;
473 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
477 br
= netdev_priv(dev
);
479 err
= __br_mdb_del(br
, entry
);
481 __br_mdb_notify(dev
, entry
, RTM_DELMDB
);
485 void br_mdb_init(void)
487 rtnl_register(PF_BRIDGE
, RTM_GETMDB
, NULL
, br_mdb_dump
, NULL
);
488 rtnl_register(PF_BRIDGE
, RTM_NEWMDB
, br_mdb_add
, NULL
, NULL
);
489 rtnl_register(PF_BRIDGE
, RTM_DELMDB
, br_mdb_del
, NULL
, NULL
);
492 void br_mdb_uninit(void)
494 rtnl_unregister(PF_BRIDGE
, RTM_GETMDB
);
495 rtnl_unregister(PF_BRIDGE
, RTM_NEWMDB
);
496 rtnl_unregister(PF_BRIDGE
, RTM_DELMDB
);