xref: /openbmc/linux/net/ipv6/seg6.c (revision 0eb71a9da5796851fa87ddc1a534066c0fe54055)
1915d7e5eSDavid Lebrun /*
2915d7e5eSDavid Lebrun  *  SR-IPv6 implementation
3915d7e5eSDavid Lebrun  *
4915d7e5eSDavid Lebrun  *  Author:
5915d7e5eSDavid Lebrun  *  David Lebrun <david.lebrun@uclouvain.be>
6915d7e5eSDavid Lebrun  *
7915d7e5eSDavid Lebrun  *
8915d7e5eSDavid Lebrun  *  This program is free software; you can redistribute it and/or
9915d7e5eSDavid Lebrun  *	  modify it under the terms of the GNU General Public License
10915d7e5eSDavid Lebrun  *	  as published by the Free Software Foundation; either version
11915d7e5eSDavid Lebrun  *	  2 of the License, or (at your option) any later version.
12915d7e5eSDavid Lebrun  */
13915d7e5eSDavid Lebrun 
14915d7e5eSDavid Lebrun #include <linux/errno.h>
15915d7e5eSDavid Lebrun #include <linux/types.h>
16915d7e5eSDavid Lebrun #include <linux/socket.h>
17915d7e5eSDavid Lebrun #include <linux/net.h>
18915d7e5eSDavid Lebrun #include <linux/in6.h>
19915d7e5eSDavid Lebrun #include <linux/slab.h>
20*0eb71a9dSNeilBrown #include <linux/rhashtable.h>
21915d7e5eSDavid Lebrun 
22915d7e5eSDavid Lebrun #include <net/ipv6.h>
23915d7e5eSDavid Lebrun #include <net/protocol.h>
24915d7e5eSDavid Lebrun 
25915d7e5eSDavid Lebrun #include <net/seg6.h>
26915d7e5eSDavid Lebrun #include <net/genetlink.h>
27915d7e5eSDavid Lebrun #include <linux/seg6.h>
28915d7e5eSDavid Lebrun #include <linux/seg6_genl.h>
294f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
304f4853dcSDavid Lebrun #include <net/seg6_hmac.h>
314f4853dcSDavid Lebrun #endif
32915d7e5eSDavid Lebrun 
336c8702c6SDavid Lebrun bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len)
346c8702c6SDavid Lebrun {
356c8702c6SDavid Lebrun 	int trailing;
366c8702c6SDavid Lebrun 	unsigned int tlv_offset;
376c8702c6SDavid Lebrun 
386c8702c6SDavid Lebrun 	if (srh->type != IPV6_SRCRT_TYPE_4)
396c8702c6SDavid Lebrun 		return false;
406c8702c6SDavid Lebrun 
416c8702c6SDavid Lebrun 	if (((srh->hdrlen + 1) << 3) != len)
426c8702c6SDavid Lebrun 		return false;
436c8702c6SDavid Lebrun 
44925615ceSDavid Lebrun 	if (srh->segments_left > srh->first_segment)
456c8702c6SDavid Lebrun 		return false;
466c8702c6SDavid Lebrun 
476c8702c6SDavid Lebrun 	tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4);
486c8702c6SDavid Lebrun 
496c8702c6SDavid Lebrun 	trailing = len - tlv_offset;
506c8702c6SDavid Lebrun 	if (trailing < 0)
516c8702c6SDavid Lebrun 		return false;
526c8702c6SDavid Lebrun 
536c8702c6SDavid Lebrun 	while (trailing) {
546c8702c6SDavid Lebrun 		struct sr6_tlv *tlv;
556c8702c6SDavid Lebrun 		unsigned int tlv_len;
566c8702c6SDavid Lebrun 
572f3bb642SDavid Lebrun 		if (trailing < sizeof(*tlv))
582f3bb642SDavid Lebrun 			return false;
592f3bb642SDavid Lebrun 
606c8702c6SDavid Lebrun 		tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset);
616c8702c6SDavid Lebrun 		tlv_len = sizeof(*tlv) + tlv->len;
626c8702c6SDavid Lebrun 
636c8702c6SDavid Lebrun 		trailing -= tlv_len;
646c8702c6SDavid Lebrun 		if (trailing < 0)
656c8702c6SDavid Lebrun 			return false;
666c8702c6SDavid Lebrun 
676c8702c6SDavid Lebrun 		tlv_offset += tlv_len;
686c8702c6SDavid Lebrun 	}
696c8702c6SDavid Lebrun 
706c8702c6SDavid Lebrun 	return true;
716c8702c6SDavid Lebrun }
726c8702c6SDavid Lebrun 
73915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family;
74915d7e5eSDavid Lebrun 
75915d7e5eSDavid Lebrun static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
76915d7e5eSDavid Lebrun 	[SEG6_ATTR_DST]				= { .type = NLA_BINARY,
77915d7e5eSDavid Lebrun 		.len = sizeof(struct in6_addr) },
78915d7e5eSDavid Lebrun 	[SEG6_ATTR_DSTLEN]			= { .type = NLA_S32, },
79915d7e5eSDavid Lebrun 	[SEG6_ATTR_HMACKEYID]		= { .type = NLA_U32, },
80915d7e5eSDavid Lebrun 	[SEG6_ATTR_SECRET]			= { .type = NLA_BINARY, },
81915d7e5eSDavid Lebrun 	[SEG6_ATTR_SECRETLEN]		= { .type = NLA_U8, },
82915d7e5eSDavid Lebrun 	[SEG6_ATTR_ALGID]			= { .type = NLA_U8, },
83915d7e5eSDavid Lebrun 	[SEG6_ATTR_HMACINFO]		= { .type = NLA_NESTED, },
84915d7e5eSDavid Lebrun };
85915d7e5eSDavid Lebrun 
864f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
874f4853dcSDavid Lebrun 
884f4853dcSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
894f4853dcSDavid Lebrun {
904f4853dcSDavid Lebrun 	struct net *net = genl_info_net(info);
914f4853dcSDavid Lebrun 	struct seg6_pernet_data *sdata;
924f4853dcSDavid Lebrun 	struct seg6_hmac_info *hinfo;
934f4853dcSDavid Lebrun 	u32 hmackeyid;
944f4853dcSDavid Lebrun 	char *secret;
954f4853dcSDavid Lebrun 	int err = 0;
964f4853dcSDavid Lebrun 	u8 algid;
974f4853dcSDavid Lebrun 	u8 slen;
984f4853dcSDavid Lebrun 
994f4853dcSDavid Lebrun 	sdata = seg6_pernet(net);
1004f4853dcSDavid Lebrun 
1014f4853dcSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
1024f4853dcSDavid Lebrun 	    !info->attrs[SEG6_ATTR_SECRETLEN] ||
1034f4853dcSDavid Lebrun 	    !info->attrs[SEG6_ATTR_ALGID])
1044f4853dcSDavid Lebrun 		return -EINVAL;
1054f4853dcSDavid Lebrun 
1064f4853dcSDavid Lebrun 	hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
1074f4853dcSDavid Lebrun 	slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
1084f4853dcSDavid Lebrun 	algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
1094f4853dcSDavid Lebrun 
1104f4853dcSDavid Lebrun 	if (hmackeyid == 0)
1114f4853dcSDavid Lebrun 		return -EINVAL;
1124f4853dcSDavid Lebrun 
1134f4853dcSDavid Lebrun 	if (slen > SEG6_HMAC_SECRET_LEN)
1144f4853dcSDavid Lebrun 		return -EINVAL;
1154f4853dcSDavid Lebrun 
1164f4853dcSDavid Lebrun 	mutex_lock(&sdata->lock);
1174f4853dcSDavid Lebrun 	hinfo = seg6_hmac_info_lookup(net, hmackeyid);
1184f4853dcSDavid Lebrun 
1194f4853dcSDavid Lebrun 	if (!slen) {
1204f4853dcSDavid Lebrun 		if (!hinfo)
1214f4853dcSDavid Lebrun 			err = -ENOENT;
1224f4853dcSDavid Lebrun 
1234f4853dcSDavid Lebrun 		err = seg6_hmac_info_del(net, hmackeyid);
1244f4853dcSDavid Lebrun 
1254f4853dcSDavid Lebrun 		goto out_unlock;
1264f4853dcSDavid Lebrun 	}
1274f4853dcSDavid Lebrun 
1284f4853dcSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_SECRET]) {
1294f4853dcSDavid Lebrun 		err = -EINVAL;
1304f4853dcSDavid Lebrun 		goto out_unlock;
1314f4853dcSDavid Lebrun 	}
1324f4853dcSDavid Lebrun 
1334f4853dcSDavid Lebrun 	if (hinfo) {
1344f4853dcSDavid Lebrun 		err = seg6_hmac_info_del(net, hmackeyid);
1354f4853dcSDavid Lebrun 		if (err)
1364f4853dcSDavid Lebrun 			goto out_unlock;
1374f4853dcSDavid Lebrun 	}
1384f4853dcSDavid Lebrun 
1394f4853dcSDavid Lebrun 	secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
1404f4853dcSDavid Lebrun 
1414f4853dcSDavid Lebrun 	hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
1424f4853dcSDavid Lebrun 	if (!hinfo) {
1434f4853dcSDavid Lebrun 		err = -ENOMEM;
1444f4853dcSDavid Lebrun 		goto out_unlock;
1454f4853dcSDavid Lebrun 	}
1464f4853dcSDavid Lebrun 
1474f4853dcSDavid Lebrun 	memcpy(hinfo->secret, secret, slen);
1484f4853dcSDavid Lebrun 	hinfo->slen = slen;
1494f4853dcSDavid Lebrun 	hinfo->alg_id = algid;
1504f4853dcSDavid Lebrun 	hinfo->hmackeyid = hmackeyid;
1514f4853dcSDavid Lebrun 
1524f4853dcSDavid Lebrun 	err = seg6_hmac_info_add(net, hmackeyid, hinfo);
1534f4853dcSDavid Lebrun 	if (err)
1544f4853dcSDavid Lebrun 		kfree(hinfo);
1554f4853dcSDavid Lebrun 
1564f4853dcSDavid Lebrun out_unlock:
1574f4853dcSDavid Lebrun 	mutex_unlock(&sdata->lock);
1584f4853dcSDavid Lebrun 	return err;
1594f4853dcSDavid Lebrun }
1604f4853dcSDavid Lebrun 
1614f4853dcSDavid Lebrun #else
1624f4853dcSDavid Lebrun 
163915d7e5eSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
164915d7e5eSDavid Lebrun {
165915d7e5eSDavid Lebrun 	return -ENOTSUPP;
166915d7e5eSDavid Lebrun }
167915d7e5eSDavid Lebrun 
1684f4853dcSDavid Lebrun #endif
1694f4853dcSDavid Lebrun 
170915d7e5eSDavid Lebrun static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
171915d7e5eSDavid Lebrun {
172915d7e5eSDavid Lebrun 	struct net *net = genl_info_net(info);
173915d7e5eSDavid Lebrun 	struct in6_addr *val, *t_old, *t_new;
174915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata;
175915d7e5eSDavid Lebrun 
176915d7e5eSDavid Lebrun 	sdata = seg6_pernet(net);
177915d7e5eSDavid Lebrun 
178915d7e5eSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_DST])
179915d7e5eSDavid Lebrun 		return -EINVAL;
180915d7e5eSDavid Lebrun 
181915d7e5eSDavid Lebrun 	val = nla_data(info->attrs[SEG6_ATTR_DST]);
182915d7e5eSDavid Lebrun 	t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
183e363116bSEric Dumazet 	if (!t_new)
184e363116bSEric Dumazet 		return -ENOMEM;
185915d7e5eSDavid Lebrun 
186915d7e5eSDavid Lebrun 	mutex_lock(&sdata->lock);
187915d7e5eSDavid Lebrun 
188915d7e5eSDavid Lebrun 	t_old = sdata->tun_src;
189915d7e5eSDavid Lebrun 	rcu_assign_pointer(sdata->tun_src, t_new);
190915d7e5eSDavid Lebrun 
191915d7e5eSDavid Lebrun 	mutex_unlock(&sdata->lock);
192915d7e5eSDavid Lebrun 
193915d7e5eSDavid Lebrun 	synchronize_net();
194915d7e5eSDavid Lebrun 	kfree(t_old);
195915d7e5eSDavid Lebrun 
196915d7e5eSDavid Lebrun 	return 0;
197915d7e5eSDavid Lebrun }
198915d7e5eSDavid Lebrun 
199915d7e5eSDavid Lebrun static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
200915d7e5eSDavid Lebrun {
201915d7e5eSDavid Lebrun 	struct net *net = genl_info_net(info);
202915d7e5eSDavid Lebrun 	struct in6_addr *tun_src;
203915d7e5eSDavid Lebrun 	struct sk_buff *msg;
204915d7e5eSDavid Lebrun 	void *hdr;
205915d7e5eSDavid Lebrun 
206915d7e5eSDavid Lebrun 	msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
207915d7e5eSDavid Lebrun 	if (!msg)
208915d7e5eSDavid Lebrun 		return -ENOMEM;
209915d7e5eSDavid Lebrun 
210915d7e5eSDavid Lebrun 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
211915d7e5eSDavid Lebrun 			  &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
212915d7e5eSDavid Lebrun 	if (!hdr)
213915d7e5eSDavid Lebrun 		goto free_msg;
214915d7e5eSDavid Lebrun 
215915d7e5eSDavid Lebrun 	rcu_read_lock();
216915d7e5eSDavid Lebrun 	tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
217915d7e5eSDavid Lebrun 
218915d7e5eSDavid Lebrun 	if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
219915d7e5eSDavid Lebrun 		goto nla_put_failure;
220915d7e5eSDavid Lebrun 
221915d7e5eSDavid Lebrun 	rcu_read_unlock();
222915d7e5eSDavid Lebrun 
223915d7e5eSDavid Lebrun 	genlmsg_end(msg, hdr);
224915d7e5eSDavid Lebrun 	genlmsg_reply(msg, info);
225915d7e5eSDavid Lebrun 
226915d7e5eSDavid Lebrun 	return 0;
227915d7e5eSDavid Lebrun 
228915d7e5eSDavid Lebrun nla_put_failure:
229915d7e5eSDavid Lebrun 	rcu_read_unlock();
230915d7e5eSDavid Lebrun free_msg:
231915d7e5eSDavid Lebrun 	nlmsg_free(msg);
232915d7e5eSDavid Lebrun 	return -ENOMEM;
233915d7e5eSDavid Lebrun }
234915d7e5eSDavid Lebrun 
2354f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
2364f4853dcSDavid Lebrun 
2374f4853dcSDavid Lebrun static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
2384f4853dcSDavid Lebrun 				 struct sk_buff *msg)
2394f4853dcSDavid Lebrun {
2404f4853dcSDavid Lebrun 	if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
2414f4853dcSDavid Lebrun 	    nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
2424f4853dcSDavid Lebrun 	    nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
2434f4853dcSDavid Lebrun 	    nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
2444f4853dcSDavid Lebrun 		return -1;
2454f4853dcSDavid Lebrun 
2464f4853dcSDavid Lebrun 	return 0;
2474f4853dcSDavid Lebrun }
2484f4853dcSDavid Lebrun 
2494f4853dcSDavid Lebrun static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
2504f4853dcSDavid Lebrun 					u32 portid, u32 seq, u32 flags,
2514f4853dcSDavid Lebrun 					struct sk_buff *skb, u8 cmd)
2524f4853dcSDavid Lebrun {
2534f4853dcSDavid Lebrun 	void *hdr;
2544f4853dcSDavid Lebrun 
2554f4853dcSDavid Lebrun 	hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
2564f4853dcSDavid Lebrun 	if (!hdr)
2574f4853dcSDavid Lebrun 		return -ENOMEM;
2584f4853dcSDavid Lebrun 
2594f4853dcSDavid Lebrun 	if (__seg6_hmac_fill_info(hinfo, skb) < 0)
2604f4853dcSDavid Lebrun 		goto nla_put_failure;
2614f4853dcSDavid Lebrun 
2624f4853dcSDavid Lebrun 	genlmsg_end(skb, hdr);
2634f4853dcSDavid Lebrun 	return 0;
2644f4853dcSDavid Lebrun 
2654f4853dcSDavid Lebrun nla_put_failure:
2664f4853dcSDavid Lebrun 	genlmsg_cancel(skb, hdr);
2674f4853dcSDavid Lebrun 	return -EMSGSIZE;
2684f4853dcSDavid Lebrun }
2694f4853dcSDavid Lebrun 
2704f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
2714f4853dcSDavid Lebrun {
2724f4853dcSDavid Lebrun 	struct net *net = sock_net(cb->skb->sk);
2734f4853dcSDavid Lebrun 	struct seg6_pernet_data *sdata;
2744f4853dcSDavid Lebrun 	struct rhashtable_iter *iter;
2754f4853dcSDavid Lebrun 
2764f4853dcSDavid Lebrun 	sdata = seg6_pernet(net);
2774f4853dcSDavid Lebrun 	iter = (struct rhashtable_iter *)cb->args[0];
2784f4853dcSDavid Lebrun 
2794f4853dcSDavid Lebrun 	if (!iter) {
2804f4853dcSDavid Lebrun 		iter = kmalloc(sizeof(*iter), GFP_KERNEL);
2814f4853dcSDavid Lebrun 		if (!iter)
2824f4853dcSDavid Lebrun 			return -ENOMEM;
2834f4853dcSDavid Lebrun 
2844f4853dcSDavid Lebrun 		cb->args[0] = (long)iter;
2854f4853dcSDavid Lebrun 	}
2864f4853dcSDavid Lebrun 
2874f4853dcSDavid Lebrun 	rhashtable_walk_enter(&sdata->hmac_infos, iter);
2884f4853dcSDavid Lebrun 
2894f4853dcSDavid Lebrun 	return 0;
2904f4853dcSDavid Lebrun }
2914f4853dcSDavid Lebrun 
2924f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
2934f4853dcSDavid Lebrun {
2944f4853dcSDavid Lebrun 	struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
2954f4853dcSDavid Lebrun 
2964f4853dcSDavid Lebrun 	rhashtable_walk_exit(iter);
2974f4853dcSDavid Lebrun 
2984f4853dcSDavid Lebrun 	kfree(iter);
2994f4853dcSDavid Lebrun 
3004f4853dcSDavid Lebrun 	return 0;
3014f4853dcSDavid Lebrun }
3024f4853dcSDavid Lebrun 
3034f4853dcSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
3044f4853dcSDavid Lebrun {
3054f4853dcSDavid Lebrun 	struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
3064f4853dcSDavid Lebrun 	struct seg6_hmac_info *hinfo;
3074f4853dcSDavid Lebrun 	int ret;
3084f4853dcSDavid Lebrun 
30997a6ec4aSTom Herbert 	rhashtable_walk_start(iter);
3104f4853dcSDavid Lebrun 
3114f4853dcSDavid Lebrun 	for (;;) {
3124f4853dcSDavid Lebrun 		hinfo = rhashtable_walk_next(iter);
3134f4853dcSDavid Lebrun 
3144f4853dcSDavid Lebrun 		if (IS_ERR(hinfo)) {
3154f4853dcSDavid Lebrun 			if (PTR_ERR(hinfo) == -EAGAIN)
3164f4853dcSDavid Lebrun 				continue;
3174f4853dcSDavid Lebrun 			ret = PTR_ERR(hinfo);
3184f4853dcSDavid Lebrun 			goto done;
3194f4853dcSDavid Lebrun 		} else if (!hinfo) {
3204f4853dcSDavid Lebrun 			break;
3214f4853dcSDavid Lebrun 		}
3224f4853dcSDavid Lebrun 
3234f4853dcSDavid Lebrun 		ret = __seg6_genl_dumphmac_element(hinfo,
3244f4853dcSDavid Lebrun 						   NETLINK_CB(cb->skb).portid,
3254f4853dcSDavid Lebrun 						   cb->nlh->nlmsg_seq,
3264f4853dcSDavid Lebrun 						   NLM_F_MULTI,
3274f4853dcSDavid Lebrun 						   skb, SEG6_CMD_DUMPHMAC);
3284f4853dcSDavid Lebrun 		if (ret)
3294f4853dcSDavid Lebrun 			goto done;
3304f4853dcSDavid Lebrun 	}
3314f4853dcSDavid Lebrun 
3324f4853dcSDavid Lebrun 	ret = skb->len;
3334f4853dcSDavid Lebrun 
3344f4853dcSDavid Lebrun done:
3354f4853dcSDavid Lebrun 	rhashtable_walk_stop(iter);
3364f4853dcSDavid Lebrun 	return ret;
3374f4853dcSDavid Lebrun }
3384f4853dcSDavid Lebrun 
3394f4853dcSDavid Lebrun #else
3404f4853dcSDavid Lebrun 
3414f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
3424f4853dcSDavid Lebrun {
3434f4853dcSDavid Lebrun 	return 0;
3444f4853dcSDavid Lebrun }
3454f4853dcSDavid Lebrun 
3464f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
3474f4853dcSDavid Lebrun {
3484f4853dcSDavid Lebrun 	return 0;
3494f4853dcSDavid Lebrun }
3504f4853dcSDavid Lebrun 
351915d7e5eSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
352915d7e5eSDavid Lebrun {
353915d7e5eSDavid Lebrun 	return -ENOTSUPP;
354915d7e5eSDavid Lebrun }
355915d7e5eSDavid Lebrun 
3564f4853dcSDavid Lebrun #endif
3574f4853dcSDavid Lebrun 
358915d7e5eSDavid Lebrun static int __net_init seg6_net_init(struct net *net)
359915d7e5eSDavid Lebrun {
360915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata;
361915d7e5eSDavid Lebrun 
362915d7e5eSDavid Lebrun 	sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
363915d7e5eSDavid Lebrun 	if (!sdata)
364915d7e5eSDavid Lebrun 		return -ENOMEM;
365915d7e5eSDavid Lebrun 
366915d7e5eSDavid Lebrun 	mutex_init(&sdata->lock);
367915d7e5eSDavid Lebrun 
368915d7e5eSDavid Lebrun 	sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
369915d7e5eSDavid Lebrun 	if (!sdata->tun_src) {
370915d7e5eSDavid Lebrun 		kfree(sdata);
371915d7e5eSDavid Lebrun 		return -ENOMEM;
372915d7e5eSDavid Lebrun 	}
373915d7e5eSDavid Lebrun 
374915d7e5eSDavid Lebrun 	net->ipv6.seg6_data = sdata;
375915d7e5eSDavid Lebrun 
3764f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
3774f4853dcSDavid Lebrun 	seg6_hmac_net_init(net);
3784f4853dcSDavid Lebrun #endif
3794f4853dcSDavid Lebrun 
380915d7e5eSDavid Lebrun 	return 0;
381915d7e5eSDavid Lebrun }
382915d7e5eSDavid Lebrun 
383915d7e5eSDavid Lebrun static void __net_exit seg6_net_exit(struct net *net)
384915d7e5eSDavid Lebrun {
385915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
386915d7e5eSDavid Lebrun 
3874f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
3884f4853dcSDavid Lebrun 	seg6_hmac_net_exit(net);
3894f4853dcSDavid Lebrun #endif
3904f4853dcSDavid Lebrun 
391915d7e5eSDavid Lebrun 	kfree(sdata->tun_src);
392915d7e5eSDavid Lebrun 	kfree(sdata);
393915d7e5eSDavid Lebrun }
394915d7e5eSDavid Lebrun 
395915d7e5eSDavid Lebrun static struct pernet_operations ip6_segments_ops = {
396915d7e5eSDavid Lebrun 	.init = seg6_net_init,
397915d7e5eSDavid Lebrun 	.exit = seg6_net_exit,
398915d7e5eSDavid Lebrun };
399915d7e5eSDavid Lebrun 
400915d7e5eSDavid Lebrun static const struct genl_ops seg6_genl_ops[] = {
401915d7e5eSDavid Lebrun 	{
402915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_SETHMAC,
403915d7e5eSDavid Lebrun 		.doit	= seg6_genl_sethmac,
404915d7e5eSDavid Lebrun 		.policy	= seg6_genl_policy,
405915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
406915d7e5eSDavid Lebrun 	},
407915d7e5eSDavid Lebrun 	{
408915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_DUMPHMAC,
4094f4853dcSDavid Lebrun 		.start	= seg6_genl_dumphmac_start,
410915d7e5eSDavid Lebrun 		.dumpit	= seg6_genl_dumphmac,
4114f4853dcSDavid Lebrun 		.done	= seg6_genl_dumphmac_done,
412915d7e5eSDavid Lebrun 		.policy	= seg6_genl_policy,
413915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
414915d7e5eSDavid Lebrun 	},
415915d7e5eSDavid Lebrun 	{
416915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_SET_TUNSRC,
417915d7e5eSDavid Lebrun 		.doit	= seg6_genl_set_tunsrc,
418915d7e5eSDavid Lebrun 		.policy	= seg6_genl_policy,
419915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
420915d7e5eSDavid Lebrun 	},
421915d7e5eSDavid Lebrun 	{
422915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_GET_TUNSRC,
423915d7e5eSDavid Lebrun 		.doit	= seg6_genl_get_tunsrc,
424915d7e5eSDavid Lebrun 		.policy = seg6_genl_policy,
425915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
426915d7e5eSDavid Lebrun 	},
427915d7e5eSDavid Lebrun };
428915d7e5eSDavid Lebrun 
429915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family __ro_after_init = {
430915d7e5eSDavid Lebrun 	.hdrsize	= 0,
431915d7e5eSDavid Lebrun 	.name		= SEG6_GENL_NAME,
432915d7e5eSDavid Lebrun 	.version	= SEG6_GENL_VERSION,
433915d7e5eSDavid Lebrun 	.maxattr	= SEG6_ATTR_MAX,
434915d7e5eSDavid Lebrun 	.netnsok	= true,
435915d7e5eSDavid Lebrun 	.parallel_ops	= true,
436915d7e5eSDavid Lebrun 	.ops		= seg6_genl_ops,
437915d7e5eSDavid Lebrun 	.n_ops		= ARRAY_SIZE(seg6_genl_ops),
438915d7e5eSDavid Lebrun 	.module		= THIS_MODULE,
439915d7e5eSDavid Lebrun };
440915d7e5eSDavid Lebrun 
441915d7e5eSDavid Lebrun int __init seg6_init(void)
442915d7e5eSDavid Lebrun {
443915d7e5eSDavid Lebrun 	int err = -ENOMEM;
444915d7e5eSDavid Lebrun 
445915d7e5eSDavid Lebrun 	err = genl_register_family(&seg6_genl_family);
446915d7e5eSDavid Lebrun 	if (err)
447915d7e5eSDavid Lebrun 		goto out;
448915d7e5eSDavid Lebrun 
449915d7e5eSDavid Lebrun 	err = register_pernet_subsys(&ip6_segments_ops);
450915d7e5eSDavid Lebrun 	if (err)
451915d7e5eSDavid Lebrun 		goto out_unregister_genl;
452915d7e5eSDavid Lebrun 
45346738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
4546c8702c6SDavid Lebrun 	err = seg6_iptunnel_init();
4556c8702c6SDavid Lebrun 	if (err)
4566c8702c6SDavid Lebrun 		goto out_unregister_pernet;
457d1df6fd8SDavid Lebrun 
458d1df6fd8SDavid Lebrun 	err = seg6_local_init();
459d1df6fd8SDavid Lebrun 	if (err)
460d1df6fd8SDavid Lebrun 		goto out_unregister_pernet;
46146738b13SDavid Lebrun #endif
4626c8702c6SDavid Lebrun 
4634f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4644f4853dcSDavid Lebrun 	err = seg6_hmac_init();
4654f4853dcSDavid Lebrun 	if (err)
4664f4853dcSDavid Lebrun 		goto out_unregister_iptun;
4674f4853dcSDavid Lebrun #endif
4684f4853dcSDavid Lebrun 
469915d7e5eSDavid Lebrun 	pr_info("Segment Routing with IPv6\n");
470915d7e5eSDavid Lebrun 
471915d7e5eSDavid Lebrun out:
472915d7e5eSDavid Lebrun 	return err;
4734f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4744f4853dcSDavid Lebrun out_unregister_iptun:
47546738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
476d1df6fd8SDavid Lebrun 	seg6_local_exit();
4774f4853dcSDavid Lebrun 	seg6_iptunnel_exit();
4784f4853dcSDavid Lebrun #endif
47946738b13SDavid Lebrun #endif
48046738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
4816c8702c6SDavid Lebrun out_unregister_pernet:
4826c8702c6SDavid Lebrun 	unregister_pernet_subsys(&ip6_segments_ops);
48346738b13SDavid Lebrun #endif
484915d7e5eSDavid Lebrun out_unregister_genl:
485915d7e5eSDavid Lebrun 	genl_unregister_family(&seg6_genl_family);
486915d7e5eSDavid Lebrun 	goto out;
487915d7e5eSDavid Lebrun }
488915d7e5eSDavid Lebrun 
489915d7e5eSDavid Lebrun void seg6_exit(void)
490915d7e5eSDavid Lebrun {
4914f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4924f4853dcSDavid Lebrun 	seg6_hmac_exit();
4934f4853dcSDavid Lebrun #endif
49446738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
4956c8702c6SDavid Lebrun 	seg6_iptunnel_exit();
49646738b13SDavid Lebrun #endif
497915d7e5eSDavid Lebrun 	unregister_pernet_subsys(&ip6_segments_ops);
498915d7e5eSDavid Lebrun 	genl_unregister_family(&seg6_genl_family);
499915d7e5eSDavid Lebrun }
500