xref: /openbmc/linux/net/ipv6/seg6.c (revision fa55a7d745de2d10489295b0674a403e2a5d490d)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2915d7e5eSDavid Lebrun /*
3915d7e5eSDavid Lebrun  *  SR-IPv6 implementation
4915d7e5eSDavid Lebrun  *
5915d7e5eSDavid Lebrun  *  Author:
6915d7e5eSDavid Lebrun  *  David Lebrun <david.lebrun@uclouvain.be>
7915d7e5eSDavid Lebrun  */
8915d7e5eSDavid Lebrun 
9915d7e5eSDavid Lebrun #include <linux/errno.h>
10915d7e5eSDavid Lebrun #include <linux/types.h>
11915d7e5eSDavid Lebrun #include <linux/socket.h>
12915d7e5eSDavid Lebrun #include <linux/net.h>
13915d7e5eSDavid Lebrun #include <linux/in6.h>
14915d7e5eSDavid Lebrun #include <linux/slab.h>
150eb71a9dSNeilBrown #include <linux/rhashtable.h>
16915d7e5eSDavid Lebrun 
17915d7e5eSDavid Lebrun #include <net/ipv6.h>
18915d7e5eSDavid Lebrun #include <net/protocol.h>
19915d7e5eSDavid Lebrun 
20915d7e5eSDavid Lebrun #include <net/seg6.h>
21915d7e5eSDavid Lebrun #include <net/genetlink.h>
22915d7e5eSDavid Lebrun #include <linux/seg6.h>
23915d7e5eSDavid Lebrun #include <linux/seg6_genl.h>
244f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
254f4853dcSDavid Lebrun #include <net/seg6_hmac.h>
264f4853dcSDavid Lebrun #endif
27915d7e5eSDavid Lebrun 
28bb986a50SAhmed Abdelsalam bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced)
296c8702c6SDavid Lebrun {
306c8702c6SDavid Lebrun 	unsigned int tlv_offset;
310cb7498fSAhmed Abdelsalam 	int max_last_entry;
320cb7498fSAhmed Abdelsalam 	int trailing;
336c8702c6SDavid Lebrun 
346c8702c6SDavid Lebrun 	if (srh->type != IPV6_SRCRT_TYPE_4)
356c8702c6SDavid Lebrun 		return false;
366c8702c6SDavid Lebrun 
376c8702c6SDavid Lebrun 	if (((srh->hdrlen + 1) << 3) != len)
386c8702c6SDavid Lebrun 		return false;
396c8702c6SDavid Lebrun 
40bb986a50SAhmed Abdelsalam 	if (!reduced && srh->segments_left > srh->first_segment) {
41bb986a50SAhmed Abdelsalam 		return false;
42bb986a50SAhmed Abdelsalam 	} else {
430cb7498fSAhmed Abdelsalam 		max_last_entry = (srh->hdrlen / 2) - 1;
440cb7498fSAhmed Abdelsalam 
450cb7498fSAhmed Abdelsalam 		if (srh->first_segment > max_last_entry)
460cb7498fSAhmed Abdelsalam 			return false;
470cb7498fSAhmed Abdelsalam 
480cb7498fSAhmed Abdelsalam 		if (srh->segments_left > srh->first_segment + 1)
496c8702c6SDavid Lebrun 			return false;
50bb986a50SAhmed Abdelsalam 	}
516c8702c6SDavid Lebrun 
526c8702c6SDavid Lebrun 	tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4);
536c8702c6SDavid Lebrun 
546c8702c6SDavid Lebrun 	trailing = len - tlv_offset;
556c8702c6SDavid Lebrun 	if (trailing < 0)
566c8702c6SDavid Lebrun 		return false;
576c8702c6SDavid Lebrun 
586c8702c6SDavid Lebrun 	while (trailing) {
596c8702c6SDavid Lebrun 		struct sr6_tlv *tlv;
606c8702c6SDavid Lebrun 		unsigned int tlv_len;
616c8702c6SDavid Lebrun 
622f3bb642SDavid Lebrun 		if (trailing < sizeof(*tlv))
632f3bb642SDavid Lebrun 			return false;
642f3bb642SDavid Lebrun 
656c8702c6SDavid Lebrun 		tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset);
666c8702c6SDavid Lebrun 		tlv_len = sizeof(*tlv) + tlv->len;
676c8702c6SDavid Lebrun 
686c8702c6SDavid Lebrun 		trailing -= tlv_len;
696c8702c6SDavid Lebrun 		if (trailing < 0)
706c8702c6SDavid Lebrun 			return false;
716c8702c6SDavid Lebrun 
726c8702c6SDavid Lebrun 		tlv_offset += tlv_len;
736c8702c6SDavid Lebrun 	}
746c8702c6SDavid Lebrun 
756c8702c6SDavid Lebrun 	return true;
766c8702c6SDavid Lebrun }
776c8702c6SDavid Lebrun 
78*fa55a7d7SAndrew Lunn struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags)
79*fa55a7d7SAndrew Lunn {
80*fa55a7d7SAndrew Lunn 	struct ipv6_sr_hdr *srh;
81*fa55a7d7SAndrew Lunn 	int len, srhoff = 0;
82*fa55a7d7SAndrew Lunn 
83*fa55a7d7SAndrew Lunn 	if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
84*fa55a7d7SAndrew Lunn 		return NULL;
85*fa55a7d7SAndrew Lunn 
86*fa55a7d7SAndrew Lunn 	if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
87*fa55a7d7SAndrew Lunn 		return NULL;
88*fa55a7d7SAndrew Lunn 
89*fa55a7d7SAndrew Lunn 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
90*fa55a7d7SAndrew Lunn 
91*fa55a7d7SAndrew Lunn 	len = (srh->hdrlen + 1) << 3;
92*fa55a7d7SAndrew Lunn 
93*fa55a7d7SAndrew Lunn 	if (!pskb_may_pull(skb, srhoff + len))
94*fa55a7d7SAndrew Lunn 		return NULL;
95*fa55a7d7SAndrew Lunn 
96*fa55a7d7SAndrew Lunn 	/* note that pskb_may_pull may change pointers in header;
97*fa55a7d7SAndrew Lunn 	 * for this reason it is necessary to reload them when needed.
98*fa55a7d7SAndrew Lunn 	 */
99*fa55a7d7SAndrew Lunn 	srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
100*fa55a7d7SAndrew Lunn 
101*fa55a7d7SAndrew Lunn 	if (!seg6_validate_srh(srh, len, true))
102*fa55a7d7SAndrew Lunn 		return NULL;
103*fa55a7d7SAndrew Lunn 
104*fa55a7d7SAndrew Lunn 	return srh;
105*fa55a7d7SAndrew Lunn }
106*fa55a7d7SAndrew Lunn 
107915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family;
108915d7e5eSDavid Lebrun 
109915d7e5eSDavid Lebrun static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
110915d7e5eSDavid Lebrun 	[SEG6_ATTR_DST]				= { .type = NLA_BINARY,
111915d7e5eSDavid Lebrun 		.len = sizeof(struct in6_addr) },
112915d7e5eSDavid Lebrun 	[SEG6_ATTR_DSTLEN]			= { .type = NLA_S32, },
113915d7e5eSDavid Lebrun 	[SEG6_ATTR_HMACKEYID]		= { .type = NLA_U32, },
114915d7e5eSDavid Lebrun 	[SEG6_ATTR_SECRET]			= { .type = NLA_BINARY, },
115915d7e5eSDavid Lebrun 	[SEG6_ATTR_SECRETLEN]		= { .type = NLA_U8, },
116915d7e5eSDavid Lebrun 	[SEG6_ATTR_ALGID]			= { .type = NLA_U8, },
117915d7e5eSDavid Lebrun 	[SEG6_ATTR_HMACINFO]		= { .type = NLA_NESTED, },
118915d7e5eSDavid Lebrun };
119915d7e5eSDavid Lebrun 
1204f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
1214f4853dcSDavid Lebrun 
1224f4853dcSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
1234f4853dcSDavid Lebrun {
1244f4853dcSDavid Lebrun 	struct net *net = genl_info_net(info);
1254f4853dcSDavid Lebrun 	struct seg6_pernet_data *sdata;
1264f4853dcSDavid Lebrun 	struct seg6_hmac_info *hinfo;
1274f4853dcSDavid Lebrun 	u32 hmackeyid;
1284f4853dcSDavid Lebrun 	char *secret;
1294f4853dcSDavid Lebrun 	int err = 0;
1304f4853dcSDavid Lebrun 	u8 algid;
1314f4853dcSDavid Lebrun 	u8 slen;
1324f4853dcSDavid Lebrun 
1334f4853dcSDavid Lebrun 	sdata = seg6_pernet(net);
1344f4853dcSDavid Lebrun 
1354f4853dcSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
1364f4853dcSDavid Lebrun 	    !info->attrs[SEG6_ATTR_SECRETLEN] ||
1374f4853dcSDavid Lebrun 	    !info->attrs[SEG6_ATTR_ALGID])
1384f4853dcSDavid Lebrun 		return -EINVAL;
1394f4853dcSDavid Lebrun 
1404f4853dcSDavid Lebrun 	hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
1414f4853dcSDavid Lebrun 	slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
1424f4853dcSDavid Lebrun 	algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
1434f4853dcSDavid Lebrun 
1444f4853dcSDavid Lebrun 	if (hmackeyid == 0)
1454f4853dcSDavid Lebrun 		return -EINVAL;
1464f4853dcSDavid Lebrun 
1474f4853dcSDavid Lebrun 	if (slen > SEG6_HMAC_SECRET_LEN)
1484f4853dcSDavid Lebrun 		return -EINVAL;
1494f4853dcSDavid Lebrun 
1504f4853dcSDavid Lebrun 	mutex_lock(&sdata->lock);
1514f4853dcSDavid Lebrun 	hinfo = seg6_hmac_info_lookup(net, hmackeyid);
1524f4853dcSDavid Lebrun 
1534f4853dcSDavid Lebrun 	if (!slen) {
1544f4853dcSDavid Lebrun 		err = seg6_hmac_info_del(net, hmackeyid);
1554f4853dcSDavid Lebrun 
1564f4853dcSDavid Lebrun 		goto out_unlock;
1574f4853dcSDavid Lebrun 	}
1584f4853dcSDavid Lebrun 
1594f4853dcSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_SECRET]) {
1604f4853dcSDavid Lebrun 		err = -EINVAL;
1614f4853dcSDavid Lebrun 		goto out_unlock;
1624f4853dcSDavid Lebrun 	}
1634f4853dcSDavid Lebrun 
1644f4853dcSDavid Lebrun 	if (hinfo) {
1654f4853dcSDavid Lebrun 		err = seg6_hmac_info_del(net, hmackeyid);
1664f4853dcSDavid Lebrun 		if (err)
1674f4853dcSDavid Lebrun 			goto out_unlock;
1684f4853dcSDavid Lebrun 	}
1694f4853dcSDavid Lebrun 
1704f4853dcSDavid Lebrun 	secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
1714f4853dcSDavid Lebrun 
1724f4853dcSDavid Lebrun 	hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
1734f4853dcSDavid Lebrun 	if (!hinfo) {
1744f4853dcSDavid Lebrun 		err = -ENOMEM;
1754f4853dcSDavid Lebrun 		goto out_unlock;
1764f4853dcSDavid Lebrun 	}
1774f4853dcSDavid Lebrun 
1784f4853dcSDavid Lebrun 	memcpy(hinfo->secret, secret, slen);
1794f4853dcSDavid Lebrun 	hinfo->slen = slen;
1804f4853dcSDavid Lebrun 	hinfo->alg_id = algid;
1814f4853dcSDavid Lebrun 	hinfo->hmackeyid = hmackeyid;
1824f4853dcSDavid Lebrun 
1834f4853dcSDavid Lebrun 	err = seg6_hmac_info_add(net, hmackeyid, hinfo);
1844f4853dcSDavid Lebrun 	if (err)
1854f4853dcSDavid Lebrun 		kfree(hinfo);
1864f4853dcSDavid Lebrun 
1874f4853dcSDavid Lebrun out_unlock:
1884f4853dcSDavid Lebrun 	mutex_unlock(&sdata->lock);
1894f4853dcSDavid Lebrun 	return err;
1904f4853dcSDavid Lebrun }
1914f4853dcSDavid Lebrun 
1924f4853dcSDavid Lebrun #else
1934f4853dcSDavid Lebrun 
194915d7e5eSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
195915d7e5eSDavid Lebrun {
196915d7e5eSDavid Lebrun 	return -ENOTSUPP;
197915d7e5eSDavid Lebrun }
198915d7e5eSDavid Lebrun 
1994f4853dcSDavid Lebrun #endif
2004f4853dcSDavid Lebrun 
201915d7e5eSDavid Lebrun static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
202915d7e5eSDavid Lebrun {
203915d7e5eSDavid Lebrun 	struct net *net = genl_info_net(info);
204915d7e5eSDavid Lebrun 	struct in6_addr *val, *t_old, *t_new;
205915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata;
206915d7e5eSDavid Lebrun 
207915d7e5eSDavid Lebrun 	sdata = seg6_pernet(net);
208915d7e5eSDavid Lebrun 
209915d7e5eSDavid Lebrun 	if (!info->attrs[SEG6_ATTR_DST])
210915d7e5eSDavid Lebrun 		return -EINVAL;
211915d7e5eSDavid Lebrun 
212915d7e5eSDavid Lebrun 	val = nla_data(info->attrs[SEG6_ATTR_DST]);
213915d7e5eSDavid Lebrun 	t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
214e363116bSEric Dumazet 	if (!t_new)
215e363116bSEric Dumazet 		return -ENOMEM;
216915d7e5eSDavid Lebrun 
217915d7e5eSDavid Lebrun 	mutex_lock(&sdata->lock);
218915d7e5eSDavid Lebrun 
219915d7e5eSDavid Lebrun 	t_old = sdata->tun_src;
220915d7e5eSDavid Lebrun 	rcu_assign_pointer(sdata->tun_src, t_new);
221915d7e5eSDavid Lebrun 
222915d7e5eSDavid Lebrun 	mutex_unlock(&sdata->lock);
223915d7e5eSDavid Lebrun 
224915d7e5eSDavid Lebrun 	synchronize_net();
225915d7e5eSDavid Lebrun 	kfree(t_old);
226915d7e5eSDavid Lebrun 
227915d7e5eSDavid Lebrun 	return 0;
228915d7e5eSDavid Lebrun }
229915d7e5eSDavid Lebrun 
230915d7e5eSDavid Lebrun static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
231915d7e5eSDavid Lebrun {
232915d7e5eSDavid Lebrun 	struct net *net = genl_info_net(info);
233915d7e5eSDavid Lebrun 	struct in6_addr *tun_src;
234915d7e5eSDavid Lebrun 	struct sk_buff *msg;
235915d7e5eSDavid Lebrun 	void *hdr;
236915d7e5eSDavid Lebrun 
237915d7e5eSDavid Lebrun 	msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
238915d7e5eSDavid Lebrun 	if (!msg)
239915d7e5eSDavid Lebrun 		return -ENOMEM;
240915d7e5eSDavid Lebrun 
241915d7e5eSDavid Lebrun 	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
242915d7e5eSDavid Lebrun 			  &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
243915d7e5eSDavid Lebrun 	if (!hdr)
244915d7e5eSDavid Lebrun 		goto free_msg;
245915d7e5eSDavid Lebrun 
246915d7e5eSDavid Lebrun 	rcu_read_lock();
247915d7e5eSDavid Lebrun 	tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
248915d7e5eSDavid Lebrun 
249915d7e5eSDavid Lebrun 	if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
250915d7e5eSDavid Lebrun 		goto nla_put_failure;
251915d7e5eSDavid Lebrun 
252915d7e5eSDavid Lebrun 	rcu_read_unlock();
253915d7e5eSDavid Lebrun 
254915d7e5eSDavid Lebrun 	genlmsg_end(msg, hdr);
255d1f20798SLi RongQing 	return genlmsg_reply(msg, info);
256915d7e5eSDavid Lebrun 
257915d7e5eSDavid Lebrun nla_put_failure:
258915d7e5eSDavid Lebrun 	rcu_read_unlock();
259915d7e5eSDavid Lebrun free_msg:
260915d7e5eSDavid Lebrun 	nlmsg_free(msg);
261915d7e5eSDavid Lebrun 	return -ENOMEM;
262915d7e5eSDavid Lebrun }
263915d7e5eSDavid Lebrun 
2644f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
2654f4853dcSDavid Lebrun 
2664f4853dcSDavid Lebrun static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
2674f4853dcSDavid Lebrun 				 struct sk_buff *msg)
2684f4853dcSDavid Lebrun {
2694f4853dcSDavid Lebrun 	if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
2704f4853dcSDavid Lebrun 	    nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
2714f4853dcSDavid Lebrun 	    nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
2724f4853dcSDavid Lebrun 	    nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
2734f4853dcSDavid Lebrun 		return -1;
2744f4853dcSDavid Lebrun 
2754f4853dcSDavid Lebrun 	return 0;
2764f4853dcSDavid Lebrun }
2774f4853dcSDavid Lebrun 
2784f4853dcSDavid Lebrun static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
2794f4853dcSDavid Lebrun 					u32 portid, u32 seq, u32 flags,
2804f4853dcSDavid Lebrun 					struct sk_buff *skb, u8 cmd)
2814f4853dcSDavid Lebrun {
2824f4853dcSDavid Lebrun 	void *hdr;
2834f4853dcSDavid Lebrun 
2844f4853dcSDavid Lebrun 	hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
2854f4853dcSDavid Lebrun 	if (!hdr)
2864f4853dcSDavid Lebrun 		return -ENOMEM;
2874f4853dcSDavid Lebrun 
2884f4853dcSDavid Lebrun 	if (__seg6_hmac_fill_info(hinfo, skb) < 0)
2894f4853dcSDavid Lebrun 		goto nla_put_failure;
2904f4853dcSDavid Lebrun 
2914f4853dcSDavid Lebrun 	genlmsg_end(skb, hdr);
2924f4853dcSDavid Lebrun 	return 0;
2934f4853dcSDavid Lebrun 
2944f4853dcSDavid Lebrun nla_put_failure:
2954f4853dcSDavid Lebrun 	genlmsg_cancel(skb, hdr);
2964f4853dcSDavid Lebrun 	return -EMSGSIZE;
2974f4853dcSDavid Lebrun }
2984f4853dcSDavid Lebrun 
2994f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
3004f4853dcSDavid Lebrun {
3014f4853dcSDavid Lebrun 	struct net *net = sock_net(cb->skb->sk);
3024f4853dcSDavid Lebrun 	struct seg6_pernet_data *sdata;
3034f4853dcSDavid Lebrun 	struct rhashtable_iter *iter;
3044f4853dcSDavid Lebrun 
3054f4853dcSDavid Lebrun 	sdata = seg6_pernet(net);
3064f4853dcSDavid Lebrun 	iter = (struct rhashtable_iter *)cb->args[0];
3074f4853dcSDavid Lebrun 
3084f4853dcSDavid Lebrun 	if (!iter) {
3094f4853dcSDavid Lebrun 		iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3104f4853dcSDavid Lebrun 		if (!iter)
3114f4853dcSDavid Lebrun 			return -ENOMEM;
3124f4853dcSDavid Lebrun 
3134f4853dcSDavid Lebrun 		cb->args[0] = (long)iter;
3144f4853dcSDavid Lebrun 	}
3154f4853dcSDavid Lebrun 
3164f4853dcSDavid Lebrun 	rhashtable_walk_enter(&sdata->hmac_infos, iter);
3174f4853dcSDavid Lebrun 
3184f4853dcSDavid Lebrun 	return 0;
3194f4853dcSDavid Lebrun }
3204f4853dcSDavid Lebrun 
3214f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
3224f4853dcSDavid Lebrun {
3234f4853dcSDavid Lebrun 	struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
3244f4853dcSDavid Lebrun 
3254f4853dcSDavid Lebrun 	rhashtable_walk_exit(iter);
3264f4853dcSDavid Lebrun 
3274f4853dcSDavid Lebrun 	kfree(iter);
3284f4853dcSDavid Lebrun 
3294f4853dcSDavid Lebrun 	return 0;
3304f4853dcSDavid Lebrun }
3314f4853dcSDavid Lebrun 
3324f4853dcSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
3334f4853dcSDavid Lebrun {
3344f4853dcSDavid Lebrun 	struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
3354f4853dcSDavid Lebrun 	struct seg6_hmac_info *hinfo;
3364f4853dcSDavid Lebrun 	int ret;
3374f4853dcSDavid Lebrun 
33897a6ec4aSTom Herbert 	rhashtable_walk_start(iter);
3394f4853dcSDavid Lebrun 
3404f4853dcSDavid Lebrun 	for (;;) {
3414f4853dcSDavid Lebrun 		hinfo = rhashtable_walk_next(iter);
3424f4853dcSDavid Lebrun 
3434f4853dcSDavid Lebrun 		if (IS_ERR(hinfo)) {
3444f4853dcSDavid Lebrun 			if (PTR_ERR(hinfo) == -EAGAIN)
3454f4853dcSDavid Lebrun 				continue;
3464f4853dcSDavid Lebrun 			ret = PTR_ERR(hinfo);
3474f4853dcSDavid Lebrun 			goto done;
3484f4853dcSDavid Lebrun 		} else if (!hinfo) {
3494f4853dcSDavid Lebrun 			break;
3504f4853dcSDavid Lebrun 		}
3514f4853dcSDavid Lebrun 
3524f4853dcSDavid Lebrun 		ret = __seg6_genl_dumphmac_element(hinfo,
3534f4853dcSDavid Lebrun 						   NETLINK_CB(cb->skb).portid,
3544f4853dcSDavid Lebrun 						   cb->nlh->nlmsg_seq,
3554f4853dcSDavid Lebrun 						   NLM_F_MULTI,
3564f4853dcSDavid Lebrun 						   skb, SEG6_CMD_DUMPHMAC);
3574f4853dcSDavid Lebrun 		if (ret)
3584f4853dcSDavid Lebrun 			goto done;
3594f4853dcSDavid Lebrun 	}
3604f4853dcSDavid Lebrun 
3614f4853dcSDavid Lebrun 	ret = skb->len;
3624f4853dcSDavid Lebrun 
3634f4853dcSDavid Lebrun done:
3644f4853dcSDavid Lebrun 	rhashtable_walk_stop(iter);
3654f4853dcSDavid Lebrun 	return ret;
3664f4853dcSDavid Lebrun }
3674f4853dcSDavid Lebrun 
3684f4853dcSDavid Lebrun #else
3694f4853dcSDavid Lebrun 
3704f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
3714f4853dcSDavid Lebrun {
3724f4853dcSDavid Lebrun 	return 0;
3734f4853dcSDavid Lebrun }
3744f4853dcSDavid Lebrun 
3754f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
3764f4853dcSDavid Lebrun {
3774f4853dcSDavid Lebrun 	return 0;
3784f4853dcSDavid Lebrun }
3794f4853dcSDavid Lebrun 
380915d7e5eSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
381915d7e5eSDavid Lebrun {
382915d7e5eSDavid Lebrun 	return -ENOTSUPP;
383915d7e5eSDavid Lebrun }
384915d7e5eSDavid Lebrun 
3854f4853dcSDavid Lebrun #endif
3864f4853dcSDavid Lebrun 
387915d7e5eSDavid Lebrun static int __net_init seg6_net_init(struct net *net)
388915d7e5eSDavid Lebrun {
389915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata;
390915d7e5eSDavid Lebrun 
391915d7e5eSDavid Lebrun 	sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
392915d7e5eSDavid Lebrun 	if (!sdata)
393915d7e5eSDavid Lebrun 		return -ENOMEM;
394915d7e5eSDavid Lebrun 
395915d7e5eSDavid Lebrun 	mutex_init(&sdata->lock);
396915d7e5eSDavid Lebrun 
397915d7e5eSDavid Lebrun 	sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
398915d7e5eSDavid Lebrun 	if (!sdata->tun_src) {
399915d7e5eSDavid Lebrun 		kfree(sdata);
400915d7e5eSDavid Lebrun 		return -ENOMEM;
401915d7e5eSDavid Lebrun 	}
402915d7e5eSDavid Lebrun 
403915d7e5eSDavid Lebrun 	net->ipv6.seg6_data = sdata;
404915d7e5eSDavid Lebrun 
4054f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
406f04ed7d2SMichelleJin 	if (seg6_hmac_net_init(net)) {
407f04ed7d2SMichelleJin 		kfree(rcu_dereference_raw(sdata->tun_src));
40823b08260SMichelleJin 		kfree(sdata);
409f04ed7d2SMichelleJin 		return -ENOMEM;
410acaea0d5SZhang Mingyu 	}
4114f4853dcSDavid Lebrun #endif
4124f4853dcSDavid Lebrun 
413915d7e5eSDavid Lebrun 	return 0;
414915d7e5eSDavid Lebrun }
415915d7e5eSDavid Lebrun 
416915d7e5eSDavid Lebrun static void __net_exit seg6_net_exit(struct net *net)
417915d7e5eSDavid Lebrun {
418915d7e5eSDavid Lebrun 	struct seg6_pernet_data *sdata = seg6_pernet(net);
419915d7e5eSDavid Lebrun 
4204f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4214f4853dcSDavid Lebrun 	seg6_hmac_net_exit(net);
4224f4853dcSDavid Lebrun #endif
4234f4853dcSDavid Lebrun 
424f04ed7d2SMichelleJin 	kfree(rcu_dereference_raw(sdata->tun_src));
425915d7e5eSDavid Lebrun 	kfree(sdata);
426915d7e5eSDavid Lebrun }
427915d7e5eSDavid Lebrun 
428915d7e5eSDavid Lebrun static struct pernet_operations ip6_segments_ops = {
429915d7e5eSDavid Lebrun 	.init = seg6_net_init,
430915d7e5eSDavid Lebrun 	.exit = seg6_net_exit,
431915d7e5eSDavid Lebrun };
432915d7e5eSDavid Lebrun 
433915d7e5eSDavid Lebrun static const struct genl_ops seg6_genl_ops[] = {
434915d7e5eSDavid Lebrun 	{
435915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_SETHMAC,
436ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
437915d7e5eSDavid Lebrun 		.doit	= seg6_genl_sethmac,
438915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
439915d7e5eSDavid Lebrun 	},
440915d7e5eSDavid Lebrun 	{
441915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_DUMPHMAC,
442ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4434f4853dcSDavid Lebrun 		.start	= seg6_genl_dumphmac_start,
444915d7e5eSDavid Lebrun 		.dumpit	= seg6_genl_dumphmac,
4454f4853dcSDavid Lebrun 		.done	= seg6_genl_dumphmac_done,
446915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
447915d7e5eSDavid Lebrun 	},
448915d7e5eSDavid Lebrun 	{
449915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_SET_TUNSRC,
450ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
451915d7e5eSDavid Lebrun 		.doit	= seg6_genl_set_tunsrc,
452915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
453915d7e5eSDavid Lebrun 	},
454915d7e5eSDavid Lebrun 	{
455915d7e5eSDavid Lebrun 		.cmd	= SEG6_CMD_GET_TUNSRC,
456ef6243acSJohannes Berg 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
457915d7e5eSDavid Lebrun 		.doit	= seg6_genl_get_tunsrc,
458915d7e5eSDavid Lebrun 		.flags	= GENL_ADMIN_PERM,
459915d7e5eSDavid Lebrun 	},
460915d7e5eSDavid Lebrun };
461915d7e5eSDavid Lebrun 
462915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family __ro_after_init = {
463915d7e5eSDavid Lebrun 	.hdrsize	= 0,
464915d7e5eSDavid Lebrun 	.name		= SEG6_GENL_NAME,
465915d7e5eSDavid Lebrun 	.version	= SEG6_GENL_VERSION,
466915d7e5eSDavid Lebrun 	.maxattr	= SEG6_ATTR_MAX,
4673b0f31f2SJohannes Berg 	.policy = seg6_genl_policy,
468915d7e5eSDavid Lebrun 	.netnsok	= true,
469915d7e5eSDavid Lebrun 	.parallel_ops	= true,
470915d7e5eSDavid Lebrun 	.ops		= seg6_genl_ops,
471915d7e5eSDavid Lebrun 	.n_ops		= ARRAY_SIZE(seg6_genl_ops),
472915d7e5eSDavid Lebrun 	.module		= THIS_MODULE,
473915d7e5eSDavid Lebrun };
474915d7e5eSDavid Lebrun 
475915d7e5eSDavid Lebrun int __init seg6_init(void)
476915d7e5eSDavid Lebrun {
477672e2477SColin Ian King 	int err;
478915d7e5eSDavid Lebrun 
479915d7e5eSDavid Lebrun 	err = genl_register_family(&seg6_genl_family);
480915d7e5eSDavid Lebrun 	if (err)
481915d7e5eSDavid Lebrun 		goto out;
482915d7e5eSDavid Lebrun 
483915d7e5eSDavid Lebrun 	err = register_pernet_subsys(&ip6_segments_ops);
484915d7e5eSDavid Lebrun 	if (err)
485915d7e5eSDavid Lebrun 		goto out_unregister_genl;
486915d7e5eSDavid Lebrun 
48746738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
4886c8702c6SDavid Lebrun 	err = seg6_iptunnel_init();
4896c8702c6SDavid Lebrun 	if (err)
4906c8702c6SDavid Lebrun 		goto out_unregister_pernet;
491d1df6fd8SDavid Lebrun 
492d1df6fd8SDavid Lebrun 	err = seg6_local_init();
493d1df6fd8SDavid Lebrun 	if (err)
494d1df6fd8SDavid Lebrun 		goto out_unregister_pernet;
49546738b13SDavid Lebrun #endif
4966c8702c6SDavid Lebrun 
4974f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4984f4853dcSDavid Lebrun 	err = seg6_hmac_init();
4994f4853dcSDavid Lebrun 	if (err)
5004f4853dcSDavid Lebrun 		goto out_unregister_iptun;
5014f4853dcSDavid Lebrun #endif
5024f4853dcSDavid Lebrun 
503915d7e5eSDavid Lebrun 	pr_info("Segment Routing with IPv6\n");
504915d7e5eSDavid Lebrun 
505915d7e5eSDavid Lebrun out:
506915d7e5eSDavid Lebrun 	return err;
5074f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5084f4853dcSDavid Lebrun out_unregister_iptun:
50946738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
510d1df6fd8SDavid Lebrun 	seg6_local_exit();
5114f4853dcSDavid Lebrun 	seg6_iptunnel_exit();
5124f4853dcSDavid Lebrun #endif
51346738b13SDavid Lebrun #endif
51446738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
5156c8702c6SDavid Lebrun out_unregister_pernet:
5166c8702c6SDavid Lebrun 	unregister_pernet_subsys(&ip6_segments_ops);
51746738b13SDavid Lebrun #endif
518915d7e5eSDavid Lebrun out_unregister_genl:
519915d7e5eSDavid Lebrun 	genl_unregister_family(&seg6_genl_family);
520915d7e5eSDavid Lebrun 	goto out;
521915d7e5eSDavid Lebrun }
522915d7e5eSDavid Lebrun 
523915d7e5eSDavid Lebrun void seg6_exit(void)
524915d7e5eSDavid Lebrun {
5254f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5264f4853dcSDavid Lebrun 	seg6_hmac_exit();
5274f4853dcSDavid Lebrun #endif
52846738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
5296c8702c6SDavid Lebrun 	seg6_iptunnel_exit();
53046738b13SDavid Lebrun #endif
531915d7e5eSDavid Lebrun 	unregister_pernet_subsys(&ip6_segments_ops);
532915d7e5eSDavid Lebrun 	genl_unregister_family(&seg6_genl_family);
533915d7e5eSDavid Lebrun }
534