2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2016-11-08 13:57:40 +00:00
|
|
|
/*
|
|
|
|
* SR-IPv6 implementation
|
|
|
|
*
|
|
|
|
* Author:
|
|
|
|
* David Lebrun <david.lebrun@uclouvain.be>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/in6.h>
|
|
|
|
#include <linux/slab.h>
|
2018-06-18 02:52:50 +00:00
|
|
|
#include <linux/rhashtable.h>
|
2016-11-08 13:57:40 +00:00
|
|
|
|
|
|
|
#include <net/ipv6.h>
|
|
|
|
#include <net/protocol.h>
|
|
|
|
|
|
|
|
#include <net/seg6.h>
|
|
|
|
#include <net/genetlink.h>
|
|
|
|
#include <linux/seg6.h>
|
|
|
|
#include <linux/seg6_genl.h>
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
#include <net/seg6_hmac.h>
|
|
|
|
#endif
|
2016-11-08 13:57:40 +00:00
|
|
|
|
2020-06-03 06:54:42 +00:00
|
|
|
bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced)
|
2016-11-08 13:57:41 +00:00
|
|
|
{
|
|
|
|
unsigned int tlv_offset;
|
2020-05-04 14:42:11 +00:00
|
|
|
int max_last_entry;
|
|
|
|
int trailing;
|
2016-11-08 13:57:41 +00:00
|
|
|
|
|
|
|
if (srh->type != IPV6_SRCRT_TYPE_4)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (((srh->hdrlen + 1) << 3) != len)
|
|
|
|
return false;
|
|
|
|
|
2020-06-03 06:54:42 +00:00
|
|
|
if (!reduced && srh->segments_left > srh->first_segment) {
|
2020-05-04 14:42:11 +00:00
|
|
|
return false;
|
2020-06-03 06:54:42 +00:00
|
|
|
} else {
|
|
|
|
max_last_entry = (srh->hdrlen / 2) - 1;
|
2020-05-04 14:42:11 +00:00
|
|
|
|
2020-06-03 06:54:42 +00:00
|
|
|
if (srh->first_segment > max_last_entry)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (srh->segments_left > srh->first_segment + 1)
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-08 13:57:41 +00:00
|
|
|
|
|
|
|
tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4);
|
|
|
|
|
|
|
|
trailing = len - tlv_offset;
|
|
|
|
if (trailing < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (trailing) {
|
|
|
|
struct sr6_tlv *tlv;
|
|
|
|
unsigned int tlv_len;
|
|
|
|
|
2017-04-18 15:59:49 +00:00
|
|
|
if (trailing < sizeof(*tlv))
|
|
|
|
return false;
|
|
|
|
|
2016-11-08 13:57:41 +00:00
|
|
|
tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset);
|
|
|
|
tlv_len = sizeof(*tlv) + tlv->len;
|
|
|
|
|
|
|
|
trailing -= tlv_len;
|
|
|
|
if (trailing < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
tlv_offset += tlv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:11:30 +00:00
|
|
|
struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags)
|
|
|
|
{
|
|
|
|
struct ipv6_sr_hdr *srh;
|
|
|
|
int len, srhoff = 0;
|
|
|
|
|
|
|
|
if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
|
|
|
|
|
|
|
|
len = (srh->hdrlen + 1) << 3;
|
|
|
|
|
|
|
|
if (!pskb_may_pull(skb, srhoff + len))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* note that pskb_may_pull may change pointers in header;
|
|
|
|
* for this reason it is necessary to reload them when needed.
|
|
|
|
*/
|
|
|
|
srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
|
|
|
|
|
|
|
|
if (!seg6_validate_srh(srh, len, true))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return srh;
|
|
|
|
}
|
|
|
|
|
2022-01-03 17:11:31 +00:00
|
|
|
/* Determine if an ICMP invoking packet contains a segment routing
|
|
|
|
* header. If it does, extract the offset to the true destination
|
|
|
|
* address, which is in the first segment address.
|
|
|
|
*/
|
|
|
|
void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt)
|
|
|
|
{
|
|
|
|
__u16 network_header = skb->network_header;
|
|
|
|
struct ipv6_sr_hdr *srh;
|
|
|
|
|
|
|
|
/* Update network header to point to the invoking packet
|
|
|
|
* inside the ICMP packet, so we can use the seg6_get_srh()
|
|
|
|
* helper.
|
|
|
|
*/
|
|
|
|
skb_reset_network_header(skb);
|
|
|
|
|
|
|
|
srh = seg6_get_srh(skb, 0);
|
|
|
|
if (!srh)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (srh->type != IPV6_SRCRT_TYPE_4)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
opt->flags |= IP6SKB_SEG6;
|
|
|
|
opt->srhoff = (unsigned char *)srh - skb->data;
|
|
|
|
|
|
|
|
out:
|
|
|
|
/* Restore the network header back to the ICMP packet */
|
|
|
|
skb->network_header = network_header;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
static struct genl_family seg6_genl_family;
|
|
|
|
|
|
|
|
static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
|
|
|
|
[SEG6_ATTR_DST] = { .type = NLA_BINARY,
|
|
|
|
.len = sizeof(struct in6_addr) },
|
|
|
|
[SEG6_ATTR_DSTLEN] = { .type = NLA_S32, },
|
|
|
|
[SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, },
|
|
|
|
[SEG6_ATTR_SECRET] = { .type = NLA_BINARY, },
|
|
|
|
[SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, },
|
|
|
|
[SEG6_ATTR_ALGID] = { .type = NLA_U8, },
|
|
|
|
[SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, },
|
|
|
|
};
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
|
|
|
|
static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct net *net = genl_info_net(info);
|
|
|
|
struct seg6_pernet_data *sdata;
|
|
|
|
struct seg6_hmac_info *hinfo;
|
|
|
|
u32 hmackeyid;
|
|
|
|
char *secret;
|
|
|
|
int err = 0;
|
|
|
|
u8 algid;
|
|
|
|
u8 slen;
|
|
|
|
|
|
|
|
sdata = seg6_pernet(net);
|
|
|
|
|
|
|
|
if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
|
|
|
|
!info->attrs[SEG6_ATTR_SECRETLEN] ||
|
|
|
|
!info->attrs[SEG6_ATTR_ALGID])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
|
|
|
|
slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
|
|
|
|
algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
|
|
|
|
|
|
|
|
if (hmackeyid == 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (slen > SEG6_HMAC_SECRET_LEN)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&sdata->lock);
|
|
|
|
hinfo = seg6_hmac_info_lookup(net, hmackeyid);
|
|
|
|
|
|
|
|
if (!slen) {
|
|
|
|
err = seg6_hmac_info_del(net, hmackeyid);
|
|
|
|
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!info->attrs[SEG6_ATTR_SECRET]) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
ipv6: sr: fix out-of-bounds read when setting HMAC data.
The SRv6 layer allows defining HMAC data that can later be used to sign IPv6
Segment Routing Headers. This configuration is realised via netlink through
four attributes: SEG6_ATTR_HMACKEYID, SEG6_ATTR_SECRET, SEG6_ATTR_SECRETLEN and
SEG6_ATTR_ALGID. Because the SECRETLEN attribute is decoupled from the actual
length of the SECRET attribute, it is possible to provide invalid combinations
(e.g., secret = "", secretlen = 64). This case is not checked in the code and
with an appropriately crafted netlink message, an out-of-bounds read of up
to 64 bytes (max secret length) can occur past the skb end pointer and into
skb_shared_info:
Breakpoint 1, seg6_genl_sethmac (skb=<optimized out>, info=<optimized out>) at net/ipv6/seg6.c:208
208 memcpy(hinfo->secret, secret, slen);
(gdb) bt
#0 seg6_genl_sethmac (skb=<optimized out>, info=<optimized out>) at net/ipv6/seg6.c:208
#1 0xffffffff81e012e9 in genl_family_rcv_msg_doit (skb=skb@entry=0xffff88800b1f9f00, nlh=nlh@entry=0xffff88800b1b7600,
extack=extack@entry=0xffffc90000ba7af0, ops=ops@entry=0xffffc90000ba7a80, hdrlen=4, net=0xffffffff84237580 <init_net>, family=<optimized out>,
family=<optimized out>) at net/netlink/genetlink.c:731
#2 0xffffffff81e01435 in genl_family_rcv_msg (extack=0xffffc90000ba7af0, nlh=0xffff88800b1b7600, skb=0xffff88800b1f9f00,
family=0xffffffff82fef6c0 <seg6_genl_family>) at net/netlink/genetlink.c:775
#3 genl_rcv_msg (skb=0xffff88800b1f9f00, nlh=0xffff88800b1b7600, extack=0xffffc90000ba7af0) at net/netlink/genetlink.c:792
#4 0xffffffff81dfffc3 in netlink_rcv_skb (skb=skb@entry=0xffff88800b1f9f00, cb=cb@entry=0xffffffff81e01350 <genl_rcv_msg>)
at net/netlink/af_netlink.c:2501
#5 0xffffffff81e00919 in genl_rcv (skb=0xffff88800b1f9f00) at net/netlink/genetlink.c:803
#6 0xffffffff81dff6ae in netlink_unicast_kernel (ssk=0xffff888010eec800, skb=0xffff88800b1f9f00, sk=0xffff888004aed000)
at net/netlink/af_netlink.c:1319
#7 netlink_unicast (ssk=ssk@entry=0xffff888010eec800, skb=skb@entry=0xffff88800b1f9f00, portid=portid@entry=0, nonblock=<optimized out>)
at net/netlink/af_netlink.c:1345
#8 0xffffffff81dff9a4 in netlink_sendmsg (sock=<optimized out>, msg=0xffffc90000ba7e48, len=<optimized out>) at net/netlink/af_netlink.c:1921
...
(gdb) p/x ((struct sk_buff *)0xffff88800b1f9f00)->head + ((struct sk_buff *)0xffff88800b1f9f00)->end
$1 = 0xffff88800b1b76c0
(gdb) p/x secret
$2 = 0xffff88800b1b76c0
(gdb) p slen
$3 = 64 '@'
The OOB data can then be read back from userspace by dumping HMAC state. This
commit fixes this by ensuring SECRETLEN cannot exceed the actual length of
SECRET.
Reported-by: Lucas Leong <wmliang.tw@gmail.com>
Tested: verified that EINVAL is correctly returned when secretlen > len(secret)
Fixes: 4f4853dc1c9c1 ("ipv6: sr: implement API to control SR HMAC structure")
Signed-off-by: David Lebrun <dlebrun@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2022-09-02 09:45:06 +00:00
|
|
|
if (slen > nla_len(info->attrs[SEG6_ATTR_SECRET])) {
|
|
|
|
err = -EINVAL;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
if (hinfo) {
|
|
|
|
err = seg6_hmac_info_del(net, hmackeyid);
|
|
|
|
if (err)
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
|
|
|
|
|
|
|
|
hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
|
|
|
|
if (!hinfo) {
|
|
|
|
err = -ENOMEM;
|
|
|
|
goto out_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(hinfo->secret, secret, slen);
|
|
|
|
hinfo->slen = slen;
|
|
|
|
hinfo->alg_id = algid;
|
|
|
|
hinfo->hmackeyid = hmackeyid;
|
|
|
|
|
|
|
|
err = seg6_hmac_info_add(net, hmackeyid, hinfo);
|
|
|
|
if (err)
|
|
|
|
kfree(hinfo);
|
|
|
|
|
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&sdata->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct net *net = genl_info_net(info);
|
|
|
|
struct in6_addr *val, *t_old, *t_new;
|
|
|
|
struct seg6_pernet_data *sdata;
|
|
|
|
|
|
|
|
sdata = seg6_pernet(net);
|
|
|
|
|
|
|
|
if (!info->attrs[SEG6_ATTR_DST])
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
val = nla_data(info->attrs[SEG6_ATTR_DST]);
|
|
|
|
t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
|
2017-01-20 15:57:42 +00:00
|
|
|
if (!t_new)
|
|
|
|
return -ENOMEM;
|
2016-11-08 13:57:40 +00:00
|
|
|
|
|
|
|
mutex_lock(&sdata->lock);
|
|
|
|
|
|
|
|
t_old = sdata->tun_src;
|
|
|
|
rcu_assign_pointer(sdata->tun_src, t_new);
|
|
|
|
|
|
|
|
mutex_unlock(&sdata->lock);
|
|
|
|
|
|
|
|
synchronize_net();
|
|
|
|
kfree(t_old);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
|
|
|
|
{
|
|
|
|
struct net *net = genl_info_net(info);
|
|
|
|
struct in6_addr *tun_src;
|
|
|
|
struct sk_buff *msg;
|
|
|
|
void *hdr;
|
|
|
|
|
|
|
|
msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
|
|
|
|
if (!msg)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
|
|
|
|
&seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
|
|
|
|
if (!hdr)
|
|
|
|
goto free_msg;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
|
|
|
|
|
|
|
|
if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
genlmsg_end(msg, hdr);
|
2019-02-11 11:32:20 +00:00
|
|
|
return genlmsg_reply(msg, info);
|
2016-11-08 13:57:40 +00:00
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
rcu_read_unlock();
|
|
|
|
free_msg:
|
|
|
|
nlmsg_free(msg);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
|
|
|
|
static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
|
|
|
|
struct sk_buff *msg)
|
|
|
|
{
|
|
|
|
if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
|
|
|
|
nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
|
|
|
|
nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
|
|
|
|
nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
|
|
|
|
u32 portid, u32 seq, u32 flags,
|
|
|
|
struct sk_buff *skb, u8 cmd)
|
|
|
|
{
|
|
|
|
void *hdr;
|
|
|
|
|
|
|
|
hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
|
|
|
|
if (!hdr)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (__seg6_hmac_fill_info(hinfo, skb) < 0)
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
genlmsg_end(skb, hdr);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
genlmsg_cancel(skb, hdr);
|
|
|
|
return -EMSGSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
struct net *net = sock_net(cb->skb->sk);
|
|
|
|
struct seg6_pernet_data *sdata;
|
|
|
|
struct rhashtable_iter *iter;
|
|
|
|
|
|
|
|
sdata = seg6_pernet(net);
|
|
|
|
iter = (struct rhashtable_iter *)cb->args[0];
|
|
|
|
|
|
|
|
if (!iter) {
|
|
|
|
iter = kmalloc(sizeof(*iter), GFP_KERNEL);
|
|
|
|
if (!iter)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
cb->args[0] = (long)iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
rhashtable_walk_enter(&sdata->hmac_infos, iter);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
|
|
|
|
|
|
|
|
rhashtable_walk_exit(iter);
|
|
|
|
|
|
|
|
kfree(iter);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
|
|
|
|
struct seg6_hmac_info *hinfo;
|
|
|
|
int ret;
|
|
|
|
|
2017-12-04 18:31:41 +00:00
|
|
|
rhashtable_walk_start(iter);
|
2016-11-08 13:59:18 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
hinfo = rhashtable_walk_next(iter);
|
|
|
|
|
|
|
|
if (IS_ERR(hinfo)) {
|
|
|
|
if (PTR_ERR(hinfo) == -EAGAIN)
|
|
|
|
continue;
|
|
|
|
ret = PTR_ERR(hinfo);
|
|
|
|
goto done;
|
|
|
|
} else if (!hinfo) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = __seg6_genl_dumphmac_element(hinfo,
|
|
|
|
NETLINK_CB(cb->skb).portid,
|
|
|
|
cb->nlh->nlmsg_seq,
|
|
|
|
NLM_F_MULTI,
|
|
|
|
skb, SEG6_CMD_DUMPHMAC);
|
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = skb->len;
|
|
|
|
|
|
|
|
done:
|
|
|
|
rhashtable_walk_stop(iter);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
|
|
|
|
{
|
|
|
|
return -ENOTSUPP;
|
|
|
|
}
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
static int __net_init seg6_net_init(struct net *net)
|
|
|
|
{
|
|
|
|
struct seg6_pernet_data *sdata;
|
|
|
|
|
|
|
|
sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
|
|
|
|
if (!sdata)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
mutex_init(&sdata->lock);
|
|
|
|
|
|
|
|
sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
|
|
|
|
if (!sdata->tun_src) {
|
|
|
|
kfree(sdata);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
net->ipv6.seg6_data = sdata;
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
2021-09-27 03:34:56 +00:00
|
|
|
if (seg6_hmac_net_init(net)) {
|
|
|
|
kfree(rcu_dereference_raw(sdata->tun_src));
|
2021-10-02 22:33:32 +00:00
|
|
|
kfree(sdata);
|
2021-09-27 03:34:56 +00:00
|
|
|
return -ENOMEM;
|
2021-11-03 06:46:17 +00:00
|
|
|
}
|
2016-11-08 13:59:18 +00:00
|
|
|
#endif
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit seg6_net_exit(struct net *net)
|
|
|
|
{
|
|
|
|
struct seg6_pernet_data *sdata = seg6_pernet(net);
|
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
seg6_hmac_net_exit(net);
|
|
|
|
#endif
|
|
|
|
|
2021-09-27 03:34:56 +00:00
|
|
|
kfree(rcu_dereference_raw(sdata->tun_src));
|
2016-11-08 13:57:40 +00:00
|
|
|
kfree(sdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations ip6_segments_ops = {
|
|
|
|
.init = seg6_net_init,
|
|
|
|
.exit = seg6_net_exit,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct genl_ops seg6_genl_ops[] = {
|
|
|
|
{
|
|
|
|
.cmd = SEG6_CMD_SETHMAC,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2016-11-08 13:57:40 +00:00
|
|
|
.doit = seg6_genl_sethmac,
|
|
|
|
.flags = GENL_ADMIN_PERM,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.cmd = SEG6_CMD_DUMPHMAC,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2016-11-08 13:59:18 +00:00
|
|
|
.start = seg6_genl_dumphmac_start,
|
2016-11-08 13:57:40 +00:00
|
|
|
.dumpit = seg6_genl_dumphmac,
|
2016-11-08 13:59:18 +00:00
|
|
|
.done = seg6_genl_dumphmac_done,
|
2016-11-08 13:57:40 +00:00
|
|
|
.flags = GENL_ADMIN_PERM,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.cmd = SEG6_CMD_SET_TUNSRC,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2016-11-08 13:57:40 +00:00
|
|
|
.doit = seg6_genl_set_tunsrc,
|
|
|
|
.flags = GENL_ADMIN_PERM,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
.cmd = SEG6_CMD_GET_TUNSRC,
|
2019-04-26 12:07:31 +00:00
|
|
|
.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
|
2016-11-08 13:57:40 +00:00
|
|
|
.doit = seg6_genl_get_tunsrc,
|
|
|
|
.flags = GENL_ADMIN_PERM,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct genl_family seg6_genl_family __ro_after_init = {
|
|
|
|
.hdrsize = 0,
|
|
|
|
.name = SEG6_GENL_NAME,
|
|
|
|
.version = SEG6_GENL_VERSION,
|
|
|
|
.maxattr = SEG6_ATTR_MAX,
|
genetlink: make policy common to family
Since maxattr is common, the policy can't really differ sanely,
so make it common as well.
The only user that did in fact manage to make a non-common policy
is taskstats, which has to be really careful about it (since it's
still using a common maxattr!). This is no longer supported, but
we can fake it using pre_doit.
This reduces the size of e.g. nl80211.o (which has lots of commands):
text data bss dec hex filename
398745 14323 2240 415308 6564c net/wireless/nl80211.o (before)
397913 14331 2240 414484 65314 net/wireless/nl80211.o (after)
--------------------------------
-832 +8 0 -824
Which is obviously just 8 bytes for each command, and an added 8
bytes for the new policy pointer. I'm not sure why the ops list is
counted as .text though.
Most of the code transformations were done using the following spatch:
@ops@
identifier OPS;
expression POLICY;
@@
struct genl_ops OPS[] = {
...,
{
- .policy = POLICY,
},
...
};
@@
identifier ops.OPS;
expression ops.POLICY;
identifier fam;
expression M;
@@
struct genl_family fam = {
.ops = OPS,
.maxattr = M,
+ .policy = POLICY,
...
};
This also gets rid of devlink_nl_cmd_region_read_dumpit() accessing
the cb->data as ops, which we want to change in a later genl patch.
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-03-21 21:51:02 +00:00
|
|
|
.policy = seg6_genl_policy,
|
2016-11-08 13:57:40 +00:00
|
|
|
.netnsok = true,
|
|
|
|
.parallel_ops = true,
|
|
|
|
.ops = seg6_genl_ops,
|
|
|
|
.n_ops = ARRAY_SIZE(seg6_genl_ops),
|
2022-08-25 00:18:30 +00:00
|
|
|
.resv_start_op = SEG6_CMD_GET_TUNSRC + 1,
|
2016-11-08 13:57:40 +00:00
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
int __init seg6_init(void)
|
|
|
|
{
|
2020-04-15 23:16:30 +00:00
|
|
|
int err;
|
2016-11-08 13:57:40 +00:00
|
|
|
|
|
|
|
err = genl_register_family(&seg6_genl_family);
|
|
|
|
if (err)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err = register_pernet_subsys(&ip6_segments_ops);
|
|
|
|
if (err)
|
|
|
|
goto out_unregister_genl;
|
|
|
|
|
2016-11-15 15:14:04 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
|
2016-11-08 13:57:41 +00:00
|
|
|
err = seg6_iptunnel_init();
|
|
|
|
if (err)
|
|
|
|
goto out_unregister_pernet;
|
2017-08-05 10:38:26 +00:00
|
|
|
|
|
|
|
err = seg6_local_init();
|
|
|
|
if (err)
|
|
|
|
goto out_unregister_pernet;
|
2016-11-15 15:14:04 +00:00
|
|
|
#endif
|
2016-11-08 13:57:41 +00:00
|
|
|
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
err = seg6_hmac_init();
|
|
|
|
if (err)
|
|
|
|
goto out_unregister_iptun;
|
|
|
|
#endif
|
|
|
|
|
2016-11-08 13:57:40 +00:00
|
|
|
pr_info("Segment Routing with IPv6\n");
|
|
|
|
|
|
|
|
out:
|
|
|
|
return err;
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
out_unregister_iptun:
|
2016-11-15 15:14:04 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
|
2017-08-05 10:38:26 +00:00
|
|
|
seg6_local_exit();
|
2016-11-08 13:59:18 +00:00
|
|
|
seg6_iptunnel_exit();
|
|
|
|
#endif
|
2016-11-15 15:14:04 +00:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
|
2016-11-08 13:57:41 +00:00
|
|
|
out_unregister_pernet:
|
|
|
|
unregister_pernet_subsys(&ip6_segments_ops);
|
2016-11-15 15:14:04 +00:00
|
|
|
#endif
|
2016-11-08 13:57:40 +00:00
|
|
|
out_unregister_genl:
|
|
|
|
genl_unregister_family(&seg6_genl_family);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void seg6_exit(void)
|
|
|
|
{
|
2016-11-08 13:59:18 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_HMAC
|
|
|
|
seg6_hmac_exit();
|
|
|
|
#endif
|
2016-11-15 15:14:04 +00:00
|
|
|
#ifdef CONFIG_IPV6_SEG6_LWTUNNEL
|
2016-11-08 13:57:41 +00:00
|
|
|
seg6_iptunnel_exit();
|
2016-11-15 15:14:04 +00:00
|
|
|
#endif
|
2016-11-08 13:57:40 +00:00
|
|
|
unregister_pernet_subsys(&ip6_segments_ops);
|
|
|
|
genl_unregister_family(&seg6_genl_family);
|
|
|
|
}
|