Commit 5b2f94b2 authored by Roopa Prabhu's avatar Roopa Prabhu Committed by David S. Miller

net: rtnetlink: support for fdb get

This patch adds support for fdb get similar to
route get. arguments can be any of the following (similar to fdb add/del/dump):
[bridge, mac, vlan] or
[bridge_port, mac, vlan, flags=[NTF_MASTER]] or
[dev, mac, [vni|vlan], flags=[NTF_SELF]]
Signed-off-by: default avatarRoopa Prabhu <roopa@cumulusnetworks.com>
Reviewed-by: default avatarDavid Ahern <dsa@cumulusnetworks.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 5312b93b
...@@ -1387,7 +1387,12 @@ struct net_device_ops { ...@@ -1387,7 +1387,12 @@ struct net_device_ops {
struct net_device *dev, struct net_device *dev,
struct net_device *filter_dev, struct net_device *filter_dev,
int *idx); int *idx);
int (*ndo_fdb_get)(struct sk_buff *skb,
struct nlattr *tb[],
struct net_device *dev,
const unsigned char *addr,
u16 vid, u32 portid, u32 seq,
struct netlink_ext_ack *extack);
int (*ndo_bridge_setlink)(struct net_device *dev, int (*ndo_bridge_setlink)(struct net_device *dev,
struct nlmsghdr *nlh, struct nlmsghdr *nlh,
u16 flags, u16 flags,
......
...@@ -3460,6 +3460,18 @@ void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change, ...@@ -3460,6 +3460,18 @@ void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
new_nsid, new_ifindex); new_nsid, new_ifindex);
} }
static const struct nla_policy nda_policy[NDA_MAX+1] = {
[NDA_DST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
[NDA_LLADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
[NDA_CACHEINFO] = { .len = sizeof(struct nda_cacheinfo) },
[NDA_PROBES] = { .type = NLA_U32 },
[NDA_VLAN] = { .type = NLA_U16 },
[NDA_PORT] = { .type = NLA_U16 },
[NDA_VNI] = { .type = NLA_U32 },
[NDA_IFINDEX] = { .type = NLA_U32 },
[NDA_MASTER] = { .type = NLA_U32 },
};
static int nlmsg_populate_fdb_fill(struct sk_buff *skb, static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
struct net_device *dev, struct net_device *dev,
u8 *addr, u16 vid, u32 pid, u32 seq, u8 *addr, u16 vid, u32 pid, u32 seq,
...@@ -4021,6 +4033,160 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb) ...@@ -4021,6 +4033,160 @@ static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
return skb->len; return skb->len;
} }
static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
struct nlattr **tb, u8 *ndm_flags,
int *br_idx, int *brport_idx, u8 **addr,
u16 *vid, struct netlink_ext_ack *extack)
{
struct ndmsg *ndm;
int err, i;
if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
return -EINVAL;
}
ndm = nlmsg_data(nlh);
if (ndm->ndm_pad1 || ndm->ndm_pad2 || ndm->ndm_state ||
ndm->ndm_type) {
NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
return -EINVAL;
}
if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
return -EINVAL;
}
err = nlmsg_parse_strict(nlh, sizeof(struct ndmsg), tb, NDA_MAX,
nda_policy, extack);
if (err < 0)
return err;
*ndm_flags = ndm->ndm_flags;
*brport_idx = ndm->ndm_ifindex;
for (i = 0; i <= NDA_MAX; ++i) {
if (!tb[i])
continue;
switch (i) {
case NDA_MASTER:
*br_idx = nla_get_u32(tb[i]);
break;
case NDA_LLADDR:
if (nla_len(tb[i]) != ETH_ALEN) {
NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
return -EINVAL;
}
*addr = nla_data(tb[i]);
break;
case NDA_VLAN:
err = fdb_vid_parse(tb[i], vid, extack);
if (err)
return err;
break;
case NDA_VNI:
break;
default:
NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
return -EINVAL;
}
}
return 0;
}
static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
struct netlink_ext_ack *extack)
{
struct net_device *dev = NULL, *br_dev = NULL;
const struct net_device_ops *ops = NULL;
struct net *net = sock_net(in_skb->sk);
struct nlattr *tb[NDA_MAX + 1];
struct sk_buff *skb;
int brport_idx = 0;
u8 ndm_flags = 0;
int br_idx = 0;
u8 *addr = NULL;
u16 vid = 0;
int err;
err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
&brport_idx, &addr, &vid, extack);
if (err < 0)
return err;
if (brport_idx) {
dev = __dev_get_by_index(net, brport_idx);
if (!dev) {
NL_SET_ERR_MSG(extack, "Unknown device ifindex");
return -ENODEV;
}
}
if (br_idx) {
if (dev) {
NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
return -EINVAL;
}
br_dev = __dev_get_by_index(net, br_idx);
if (!br_dev) {
NL_SET_ERR_MSG(extack, "Invalid master ifindex");
return -EINVAL;
}
ops = br_dev->netdev_ops;
}
if (dev) {
if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
if (!(dev->priv_flags & IFF_BRIDGE_PORT)) {
NL_SET_ERR_MSG(extack, "Device is not a bridge port");
return -EINVAL;
}
br_dev = netdev_master_upper_dev_get(dev);
if (!br_dev) {
NL_SET_ERR_MSG(extack, "Master of device not found");
return -EINVAL;
}
ops = br_dev->netdev_ops;
} else {
if (!(ndm_flags & NTF_SELF)) {
NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
return -EINVAL;
}
ops = dev->netdev_ops;
}
}
if (!br_dev && !dev) {
NL_SET_ERR_MSG(extack, "No device specified");
return -ENODEV;
}
if (!ops || !ops->ndo_fdb_get) {
NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
return -EOPNOTSUPP;
}
skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
if (!skb)
return -ENOBUFS;
if (br_dev)
dev = br_dev;
err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
NETLINK_CB(in_skb).portid,
nlh->nlmsg_seq, extack);
if (err)
goto out;
return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
out:
kfree_skb(skb);
return err;
}
static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask, static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
unsigned int attrnum, unsigned int flag) unsigned int attrnum, unsigned int flag)
{ {
...@@ -5081,7 +5247,7 @@ void __init rtnetlink_init(void) ...@@ -5081,7 +5247,7 @@ void __init rtnetlink_init(void)
rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0); rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0); rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, 0);
rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, 0); rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0); rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0); rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment