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
seg6_validate_srh(struct ipv6_sr_hdr * srh,int len,bool reduced)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
seg6_get_srh(struct sk_buff * skb,int flags)78fa55a7d7SAndrew Lunn struct ipv6_sr_hdr *seg6_get_srh(struct sk_buff *skb, int flags)
79fa55a7d7SAndrew Lunn {
80fa55a7d7SAndrew Lunn struct ipv6_sr_hdr *srh;
81fa55a7d7SAndrew Lunn int len, srhoff = 0;
82fa55a7d7SAndrew Lunn
83fa55a7d7SAndrew Lunn if (ipv6_find_hdr(skb, &srhoff, IPPROTO_ROUTING, NULL, &flags) < 0)
84fa55a7d7SAndrew Lunn return NULL;
85fa55a7d7SAndrew Lunn
86fa55a7d7SAndrew Lunn if (!pskb_may_pull(skb, srhoff + sizeof(*srh)))
87fa55a7d7SAndrew Lunn return NULL;
88fa55a7d7SAndrew Lunn
89fa55a7d7SAndrew Lunn srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
90fa55a7d7SAndrew Lunn
91fa55a7d7SAndrew Lunn len = (srh->hdrlen + 1) << 3;
92fa55a7d7SAndrew Lunn
93fa55a7d7SAndrew Lunn if (!pskb_may_pull(skb, srhoff + len))
94fa55a7d7SAndrew Lunn return NULL;
95fa55a7d7SAndrew Lunn
96fa55a7d7SAndrew Lunn /* note that pskb_may_pull may change pointers in header;
97fa55a7d7SAndrew Lunn * for this reason it is necessary to reload them when needed.
98fa55a7d7SAndrew Lunn */
99fa55a7d7SAndrew Lunn srh = (struct ipv6_sr_hdr *)(skb->data + srhoff);
100fa55a7d7SAndrew Lunn
101fa55a7d7SAndrew Lunn if (!seg6_validate_srh(srh, len, true))
102fa55a7d7SAndrew Lunn return NULL;
103fa55a7d7SAndrew Lunn
104fa55a7d7SAndrew Lunn return srh;
105fa55a7d7SAndrew Lunn }
106fa55a7d7SAndrew Lunn
107e4129440SAndrew Lunn /* Determine if an ICMP invoking packet contains a segment routing
108e4129440SAndrew Lunn * header. If it does, extract the offset to the true destination
109e4129440SAndrew Lunn * address, which is in the first segment address.
110e4129440SAndrew Lunn */
seg6_icmp_srh(struct sk_buff * skb,struct inet6_skb_parm * opt)111e4129440SAndrew Lunn void seg6_icmp_srh(struct sk_buff *skb, struct inet6_skb_parm *opt)
112e4129440SAndrew Lunn {
113e4129440SAndrew Lunn __u16 network_header = skb->network_header;
114e4129440SAndrew Lunn struct ipv6_sr_hdr *srh;
115e4129440SAndrew Lunn
116e4129440SAndrew Lunn /* Update network header to point to the invoking packet
117e4129440SAndrew Lunn * inside the ICMP packet, so we can use the seg6_get_srh()
118e4129440SAndrew Lunn * helper.
119e4129440SAndrew Lunn */
120e4129440SAndrew Lunn skb_reset_network_header(skb);
121e4129440SAndrew Lunn
122e4129440SAndrew Lunn srh = seg6_get_srh(skb, 0);
123e4129440SAndrew Lunn if (!srh)
124e4129440SAndrew Lunn goto out;
125e4129440SAndrew Lunn
126e4129440SAndrew Lunn if (srh->type != IPV6_SRCRT_TYPE_4)
127e4129440SAndrew Lunn goto out;
128e4129440SAndrew Lunn
129e4129440SAndrew Lunn opt->flags |= IP6SKB_SEG6;
130e4129440SAndrew Lunn opt->srhoff = (unsigned char *)srh - skb->data;
131e4129440SAndrew Lunn
132e4129440SAndrew Lunn out:
133e4129440SAndrew Lunn /* Restore the network header back to the ICMP packet */
134e4129440SAndrew Lunn skb->network_header = network_header;
135e4129440SAndrew Lunn }
136e4129440SAndrew Lunn
137915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family;
138915d7e5eSDavid Lebrun
139915d7e5eSDavid Lebrun static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = {
140915d7e5eSDavid Lebrun [SEG6_ATTR_DST] = { .type = NLA_BINARY,
141915d7e5eSDavid Lebrun .len = sizeof(struct in6_addr) },
142915d7e5eSDavid Lebrun [SEG6_ATTR_DSTLEN] = { .type = NLA_S32, },
143915d7e5eSDavid Lebrun [SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, },
144915d7e5eSDavid Lebrun [SEG6_ATTR_SECRET] = { .type = NLA_BINARY, },
145915d7e5eSDavid Lebrun [SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, },
146915d7e5eSDavid Lebrun [SEG6_ATTR_ALGID] = { .type = NLA_U8, },
147915d7e5eSDavid Lebrun [SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, },
148915d7e5eSDavid Lebrun };
149915d7e5eSDavid Lebrun
1504f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
1514f4853dcSDavid Lebrun
seg6_genl_sethmac(struct sk_buff * skb,struct genl_info * info)1524f4853dcSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
1534f4853dcSDavid Lebrun {
1544f4853dcSDavid Lebrun struct net *net = genl_info_net(info);
1554f4853dcSDavid Lebrun struct seg6_pernet_data *sdata;
1564f4853dcSDavid Lebrun struct seg6_hmac_info *hinfo;
1574f4853dcSDavid Lebrun u32 hmackeyid;
1584f4853dcSDavid Lebrun char *secret;
1594f4853dcSDavid Lebrun int err = 0;
1604f4853dcSDavid Lebrun u8 algid;
1614f4853dcSDavid Lebrun u8 slen;
1624f4853dcSDavid Lebrun
1634f4853dcSDavid Lebrun sdata = seg6_pernet(net);
1644f4853dcSDavid Lebrun
1654f4853dcSDavid Lebrun if (!info->attrs[SEG6_ATTR_HMACKEYID] ||
1664f4853dcSDavid Lebrun !info->attrs[SEG6_ATTR_SECRETLEN] ||
1674f4853dcSDavid Lebrun !info->attrs[SEG6_ATTR_ALGID])
1684f4853dcSDavid Lebrun return -EINVAL;
1694f4853dcSDavid Lebrun
1704f4853dcSDavid Lebrun hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]);
1714f4853dcSDavid Lebrun slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]);
1724f4853dcSDavid Lebrun algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]);
1734f4853dcSDavid Lebrun
1744f4853dcSDavid Lebrun if (hmackeyid == 0)
1754f4853dcSDavid Lebrun return -EINVAL;
1764f4853dcSDavid Lebrun
1774f4853dcSDavid Lebrun if (slen > SEG6_HMAC_SECRET_LEN)
1784f4853dcSDavid Lebrun return -EINVAL;
1794f4853dcSDavid Lebrun
1804f4853dcSDavid Lebrun mutex_lock(&sdata->lock);
1814f4853dcSDavid Lebrun hinfo = seg6_hmac_info_lookup(net, hmackeyid);
1824f4853dcSDavid Lebrun
1834f4853dcSDavid Lebrun if (!slen) {
1844f4853dcSDavid Lebrun err = seg6_hmac_info_del(net, hmackeyid);
1854f4853dcSDavid Lebrun
1864f4853dcSDavid Lebrun goto out_unlock;
1874f4853dcSDavid Lebrun }
1884f4853dcSDavid Lebrun
1894f4853dcSDavid Lebrun if (!info->attrs[SEG6_ATTR_SECRET]) {
1904f4853dcSDavid Lebrun err = -EINVAL;
1914f4853dcSDavid Lebrun goto out_unlock;
1924f4853dcSDavid Lebrun }
1934f4853dcSDavid Lebrun
19484a53580SDavid Lebrun if (slen > nla_len(info->attrs[SEG6_ATTR_SECRET])) {
19584a53580SDavid Lebrun err = -EINVAL;
19684a53580SDavid Lebrun goto out_unlock;
19784a53580SDavid Lebrun }
19884a53580SDavid Lebrun
1994f4853dcSDavid Lebrun if (hinfo) {
2004f4853dcSDavid Lebrun err = seg6_hmac_info_del(net, hmackeyid);
2014f4853dcSDavid Lebrun if (err)
2024f4853dcSDavid Lebrun goto out_unlock;
2034f4853dcSDavid Lebrun }
2044f4853dcSDavid Lebrun
2054f4853dcSDavid Lebrun secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]);
2064f4853dcSDavid Lebrun
2074f4853dcSDavid Lebrun hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL);
2084f4853dcSDavid Lebrun if (!hinfo) {
2094f4853dcSDavid Lebrun err = -ENOMEM;
2104f4853dcSDavid Lebrun goto out_unlock;
2114f4853dcSDavid Lebrun }
2124f4853dcSDavid Lebrun
2134f4853dcSDavid Lebrun memcpy(hinfo->secret, secret, slen);
2144f4853dcSDavid Lebrun hinfo->slen = slen;
2154f4853dcSDavid Lebrun hinfo->alg_id = algid;
2164f4853dcSDavid Lebrun hinfo->hmackeyid = hmackeyid;
2174f4853dcSDavid Lebrun
2184f4853dcSDavid Lebrun err = seg6_hmac_info_add(net, hmackeyid, hinfo);
2194f4853dcSDavid Lebrun if (err)
2204f4853dcSDavid Lebrun kfree(hinfo);
2214f4853dcSDavid Lebrun
2224f4853dcSDavid Lebrun out_unlock:
2234f4853dcSDavid Lebrun mutex_unlock(&sdata->lock);
2244f4853dcSDavid Lebrun return err;
2254f4853dcSDavid Lebrun }
2264f4853dcSDavid Lebrun
2274f4853dcSDavid Lebrun #else
2284f4853dcSDavid Lebrun
seg6_genl_sethmac(struct sk_buff * skb,struct genl_info * info)229915d7e5eSDavid Lebrun static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info)
230915d7e5eSDavid Lebrun {
231915d7e5eSDavid Lebrun return -ENOTSUPP;
232915d7e5eSDavid Lebrun }
233915d7e5eSDavid Lebrun
2344f4853dcSDavid Lebrun #endif
2354f4853dcSDavid Lebrun
seg6_genl_set_tunsrc(struct sk_buff * skb,struct genl_info * info)236915d7e5eSDavid Lebrun static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info)
237915d7e5eSDavid Lebrun {
238915d7e5eSDavid Lebrun struct net *net = genl_info_net(info);
239915d7e5eSDavid Lebrun struct in6_addr *val, *t_old, *t_new;
240915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata;
241915d7e5eSDavid Lebrun
242915d7e5eSDavid Lebrun sdata = seg6_pernet(net);
243915d7e5eSDavid Lebrun
244915d7e5eSDavid Lebrun if (!info->attrs[SEG6_ATTR_DST])
245915d7e5eSDavid Lebrun return -EINVAL;
246915d7e5eSDavid Lebrun
247915d7e5eSDavid Lebrun val = nla_data(info->attrs[SEG6_ATTR_DST]);
248915d7e5eSDavid Lebrun t_new = kmemdup(val, sizeof(*val), GFP_KERNEL);
249e363116bSEric Dumazet if (!t_new)
250e363116bSEric Dumazet return -ENOMEM;
251915d7e5eSDavid Lebrun
252915d7e5eSDavid Lebrun mutex_lock(&sdata->lock);
253915d7e5eSDavid Lebrun
254915d7e5eSDavid Lebrun t_old = sdata->tun_src;
255915d7e5eSDavid Lebrun rcu_assign_pointer(sdata->tun_src, t_new);
256915d7e5eSDavid Lebrun
257915d7e5eSDavid Lebrun mutex_unlock(&sdata->lock);
258915d7e5eSDavid Lebrun
259915d7e5eSDavid Lebrun synchronize_net();
260915d7e5eSDavid Lebrun kfree(t_old);
261915d7e5eSDavid Lebrun
262915d7e5eSDavid Lebrun return 0;
263915d7e5eSDavid Lebrun }
264915d7e5eSDavid Lebrun
seg6_genl_get_tunsrc(struct sk_buff * skb,struct genl_info * info)265915d7e5eSDavid Lebrun static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info)
266915d7e5eSDavid Lebrun {
267915d7e5eSDavid Lebrun struct net *net = genl_info_net(info);
268915d7e5eSDavid Lebrun struct in6_addr *tun_src;
269915d7e5eSDavid Lebrun struct sk_buff *msg;
270915d7e5eSDavid Lebrun void *hdr;
271915d7e5eSDavid Lebrun
272915d7e5eSDavid Lebrun msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
273915d7e5eSDavid Lebrun if (!msg)
274915d7e5eSDavid Lebrun return -ENOMEM;
275915d7e5eSDavid Lebrun
276915d7e5eSDavid Lebrun hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
277915d7e5eSDavid Lebrun &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC);
278915d7e5eSDavid Lebrun if (!hdr)
279915d7e5eSDavid Lebrun goto free_msg;
280915d7e5eSDavid Lebrun
281915d7e5eSDavid Lebrun rcu_read_lock();
282915d7e5eSDavid Lebrun tun_src = rcu_dereference(seg6_pernet(net)->tun_src);
283915d7e5eSDavid Lebrun
284915d7e5eSDavid Lebrun if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src))
285915d7e5eSDavid Lebrun goto nla_put_failure;
286915d7e5eSDavid Lebrun
287915d7e5eSDavid Lebrun rcu_read_unlock();
288915d7e5eSDavid Lebrun
289915d7e5eSDavid Lebrun genlmsg_end(msg, hdr);
290d1f20798SLi RongQing return genlmsg_reply(msg, info);
291915d7e5eSDavid Lebrun
292915d7e5eSDavid Lebrun nla_put_failure:
293915d7e5eSDavid Lebrun rcu_read_unlock();
294915d7e5eSDavid Lebrun free_msg:
295915d7e5eSDavid Lebrun nlmsg_free(msg);
296915d7e5eSDavid Lebrun return -ENOMEM;
297915d7e5eSDavid Lebrun }
298915d7e5eSDavid Lebrun
2994f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
3004f4853dcSDavid Lebrun
__seg6_hmac_fill_info(struct seg6_hmac_info * hinfo,struct sk_buff * msg)3014f4853dcSDavid Lebrun static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo,
3024f4853dcSDavid Lebrun struct sk_buff *msg)
3034f4853dcSDavid Lebrun {
3044f4853dcSDavid Lebrun if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) ||
3054f4853dcSDavid Lebrun nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) ||
3064f4853dcSDavid Lebrun nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) ||
3074f4853dcSDavid Lebrun nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id))
3084f4853dcSDavid Lebrun return -1;
3094f4853dcSDavid Lebrun
3104f4853dcSDavid Lebrun return 0;
3114f4853dcSDavid Lebrun }
3124f4853dcSDavid Lebrun
__seg6_genl_dumphmac_element(struct seg6_hmac_info * hinfo,u32 portid,u32 seq,u32 flags,struct sk_buff * skb,u8 cmd)3134f4853dcSDavid Lebrun static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo,
3144f4853dcSDavid Lebrun u32 portid, u32 seq, u32 flags,
3154f4853dcSDavid Lebrun struct sk_buff *skb, u8 cmd)
3164f4853dcSDavid Lebrun {
3174f4853dcSDavid Lebrun void *hdr;
3184f4853dcSDavid Lebrun
3194f4853dcSDavid Lebrun hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd);
3204f4853dcSDavid Lebrun if (!hdr)
3214f4853dcSDavid Lebrun return -ENOMEM;
3224f4853dcSDavid Lebrun
3234f4853dcSDavid Lebrun if (__seg6_hmac_fill_info(hinfo, skb) < 0)
3244f4853dcSDavid Lebrun goto nla_put_failure;
3254f4853dcSDavid Lebrun
3264f4853dcSDavid Lebrun genlmsg_end(skb, hdr);
3274f4853dcSDavid Lebrun return 0;
3284f4853dcSDavid Lebrun
3294f4853dcSDavid Lebrun nla_put_failure:
3304f4853dcSDavid Lebrun genlmsg_cancel(skb, hdr);
3314f4853dcSDavid Lebrun return -EMSGSIZE;
3324f4853dcSDavid Lebrun }
3334f4853dcSDavid Lebrun
seg6_genl_dumphmac_start(struct netlink_callback * cb)3344f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
3354f4853dcSDavid Lebrun {
3364f4853dcSDavid Lebrun struct net *net = sock_net(cb->skb->sk);
3374f4853dcSDavid Lebrun struct seg6_pernet_data *sdata;
3384f4853dcSDavid Lebrun struct rhashtable_iter *iter;
3394f4853dcSDavid Lebrun
3404f4853dcSDavid Lebrun sdata = seg6_pernet(net);
3414f4853dcSDavid Lebrun iter = (struct rhashtable_iter *)cb->args[0];
3424f4853dcSDavid Lebrun
3434f4853dcSDavid Lebrun if (!iter) {
3444f4853dcSDavid Lebrun iter = kmalloc(sizeof(*iter), GFP_KERNEL);
3454f4853dcSDavid Lebrun if (!iter)
3464f4853dcSDavid Lebrun return -ENOMEM;
3474f4853dcSDavid Lebrun
3484f4853dcSDavid Lebrun cb->args[0] = (long)iter;
3494f4853dcSDavid Lebrun }
3504f4853dcSDavid Lebrun
3514f4853dcSDavid Lebrun rhashtable_walk_enter(&sdata->hmac_infos, iter);
3524f4853dcSDavid Lebrun
3534f4853dcSDavid Lebrun return 0;
3544f4853dcSDavid Lebrun }
3554f4853dcSDavid Lebrun
seg6_genl_dumphmac_done(struct netlink_callback * cb)3564f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
3574f4853dcSDavid Lebrun {
3584f4853dcSDavid Lebrun struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
3594f4853dcSDavid Lebrun
3604f4853dcSDavid Lebrun rhashtable_walk_exit(iter);
3614f4853dcSDavid Lebrun
3624f4853dcSDavid Lebrun kfree(iter);
3634f4853dcSDavid Lebrun
3644f4853dcSDavid Lebrun return 0;
3654f4853dcSDavid Lebrun }
3664f4853dcSDavid Lebrun
seg6_genl_dumphmac(struct sk_buff * skb,struct netlink_callback * cb)3674f4853dcSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
3684f4853dcSDavid Lebrun {
3694f4853dcSDavid Lebrun struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0];
3704f4853dcSDavid Lebrun struct seg6_hmac_info *hinfo;
3714f4853dcSDavid Lebrun int ret;
3724f4853dcSDavid Lebrun
37397a6ec4aSTom Herbert rhashtable_walk_start(iter);
3744f4853dcSDavid Lebrun
3754f4853dcSDavid Lebrun for (;;) {
3764f4853dcSDavid Lebrun hinfo = rhashtable_walk_next(iter);
3774f4853dcSDavid Lebrun
3784f4853dcSDavid Lebrun if (IS_ERR(hinfo)) {
3794f4853dcSDavid Lebrun if (PTR_ERR(hinfo) == -EAGAIN)
3804f4853dcSDavid Lebrun continue;
3814f4853dcSDavid Lebrun ret = PTR_ERR(hinfo);
3824f4853dcSDavid Lebrun goto done;
3834f4853dcSDavid Lebrun } else if (!hinfo) {
3844f4853dcSDavid Lebrun break;
3854f4853dcSDavid Lebrun }
3864f4853dcSDavid Lebrun
3874f4853dcSDavid Lebrun ret = __seg6_genl_dumphmac_element(hinfo,
3884f4853dcSDavid Lebrun NETLINK_CB(cb->skb).portid,
3894f4853dcSDavid Lebrun cb->nlh->nlmsg_seq,
3904f4853dcSDavid Lebrun NLM_F_MULTI,
3914f4853dcSDavid Lebrun skb, SEG6_CMD_DUMPHMAC);
3924f4853dcSDavid Lebrun if (ret)
3934f4853dcSDavid Lebrun goto done;
3944f4853dcSDavid Lebrun }
3954f4853dcSDavid Lebrun
3964f4853dcSDavid Lebrun ret = skb->len;
3974f4853dcSDavid Lebrun
3984f4853dcSDavid Lebrun done:
3994f4853dcSDavid Lebrun rhashtable_walk_stop(iter);
4004f4853dcSDavid Lebrun return ret;
4014f4853dcSDavid Lebrun }
4024f4853dcSDavid Lebrun
4034f4853dcSDavid Lebrun #else
4044f4853dcSDavid Lebrun
seg6_genl_dumphmac_start(struct netlink_callback * cb)4054f4853dcSDavid Lebrun static int seg6_genl_dumphmac_start(struct netlink_callback *cb)
4064f4853dcSDavid Lebrun {
4074f4853dcSDavid Lebrun return 0;
4084f4853dcSDavid Lebrun }
4094f4853dcSDavid Lebrun
seg6_genl_dumphmac_done(struct netlink_callback * cb)4104f4853dcSDavid Lebrun static int seg6_genl_dumphmac_done(struct netlink_callback *cb)
4114f4853dcSDavid Lebrun {
4124f4853dcSDavid Lebrun return 0;
4134f4853dcSDavid Lebrun }
4144f4853dcSDavid Lebrun
seg6_genl_dumphmac(struct sk_buff * skb,struct netlink_callback * cb)415915d7e5eSDavid Lebrun static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb)
416915d7e5eSDavid Lebrun {
417915d7e5eSDavid Lebrun return -ENOTSUPP;
418915d7e5eSDavid Lebrun }
419915d7e5eSDavid Lebrun
4204f4853dcSDavid Lebrun #endif
4214f4853dcSDavid Lebrun
seg6_net_init(struct net * net)422915d7e5eSDavid Lebrun static int __net_init seg6_net_init(struct net *net)
423915d7e5eSDavid Lebrun {
424915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata;
425915d7e5eSDavid Lebrun
426915d7e5eSDavid Lebrun sdata = kzalloc(sizeof(*sdata), GFP_KERNEL);
427915d7e5eSDavid Lebrun if (!sdata)
428915d7e5eSDavid Lebrun return -ENOMEM;
429915d7e5eSDavid Lebrun
430915d7e5eSDavid Lebrun mutex_init(&sdata->lock);
431915d7e5eSDavid Lebrun
432915d7e5eSDavid Lebrun sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL);
433915d7e5eSDavid Lebrun if (!sdata->tun_src) {
434915d7e5eSDavid Lebrun kfree(sdata);
435915d7e5eSDavid Lebrun return -ENOMEM;
436915d7e5eSDavid Lebrun }
437915d7e5eSDavid Lebrun
438915d7e5eSDavid Lebrun net->ipv6.seg6_data = sdata;
439915d7e5eSDavid Lebrun
4404f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
441f04ed7d2SMichelleJin if (seg6_hmac_net_init(net)) {
442f04ed7d2SMichelleJin kfree(rcu_dereference_raw(sdata->tun_src));
44323b08260SMichelleJin kfree(sdata);
444f04ed7d2SMichelleJin return -ENOMEM;
445acaea0d5SZhang Mingyu }
4464f4853dcSDavid Lebrun #endif
4474f4853dcSDavid Lebrun
448915d7e5eSDavid Lebrun return 0;
449915d7e5eSDavid Lebrun }
450915d7e5eSDavid Lebrun
seg6_net_exit(struct net * net)451915d7e5eSDavid Lebrun static void __net_exit seg6_net_exit(struct net *net)
452915d7e5eSDavid Lebrun {
453915d7e5eSDavid Lebrun struct seg6_pernet_data *sdata = seg6_pernet(net);
454915d7e5eSDavid Lebrun
4554f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
4564f4853dcSDavid Lebrun seg6_hmac_net_exit(net);
4574f4853dcSDavid Lebrun #endif
4584f4853dcSDavid Lebrun
459f04ed7d2SMichelleJin kfree(rcu_dereference_raw(sdata->tun_src));
460915d7e5eSDavid Lebrun kfree(sdata);
461915d7e5eSDavid Lebrun }
462915d7e5eSDavid Lebrun
463915d7e5eSDavid Lebrun static struct pernet_operations ip6_segments_ops = {
464915d7e5eSDavid Lebrun .init = seg6_net_init,
465915d7e5eSDavid Lebrun .exit = seg6_net_exit,
466915d7e5eSDavid Lebrun };
467915d7e5eSDavid Lebrun
468915d7e5eSDavid Lebrun static const struct genl_ops seg6_genl_ops[] = {
469915d7e5eSDavid Lebrun {
470915d7e5eSDavid Lebrun .cmd = SEG6_CMD_SETHMAC,
471ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
472915d7e5eSDavid Lebrun .doit = seg6_genl_sethmac,
473915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM,
474915d7e5eSDavid Lebrun },
475915d7e5eSDavid Lebrun {
476915d7e5eSDavid Lebrun .cmd = SEG6_CMD_DUMPHMAC,
477ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
4784f4853dcSDavid Lebrun .start = seg6_genl_dumphmac_start,
479915d7e5eSDavid Lebrun .dumpit = seg6_genl_dumphmac,
4804f4853dcSDavid Lebrun .done = seg6_genl_dumphmac_done,
481915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM,
482915d7e5eSDavid Lebrun },
483915d7e5eSDavid Lebrun {
484915d7e5eSDavid Lebrun .cmd = SEG6_CMD_SET_TUNSRC,
485ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
486915d7e5eSDavid Lebrun .doit = seg6_genl_set_tunsrc,
487915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM,
488915d7e5eSDavid Lebrun },
489915d7e5eSDavid Lebrun {
490915d7e5eSDavid Lebrun .cmd = SEG6_CMD_GET_TUNSRC,
491ef6243acSJohannes Berg .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
492915d7e5eSDavid Lebrun .doit = seg6_genl_get_tunsrc,
493915d7e5eSDavid Lebrun .flags = GENL_ADMIN_PERM,
494915d7e5eSDavid Lebrun },
495915d7e5eSDavid Lebrun };
496915d7e5eSDavid Lebrun
497915d7e5eSDavid Lebrun static struct genl_family seg6_genl_family __ro_after_init = {
498915d7e5eSDavid Lebrun .hdrsize = 0,
499915d7e5eSDavid Lebrun .name = SEG6_GENL_NAME,
500915d7e5eSDavid Lebrun .version = SEG6_GENL_VERSION,
501915d7e5eSDavid Lebrun .maxattr = SEG6_ATTR_MAX,
5023b0f31f2SJohannes Berg .policy = seg6_genl_policy,
503915d7e5eSDavid Lebrun .netnsok = true,
504915d7e5eSDavid Lebrun .parallel_ops = true,
505915d7e5eSDavid Lebrun .ops = seg6_genl_ops,
506915d7e5eSDavid Lebrun .n_ops = ARRAY_SIZE(seg6_genl_ops),
5079c5d03d3SJakub Kicinski .resv_start_op = SEG6_CMD_GET_TUNSRC + 1,
508915d7e5eSDavid Lebrun .module = THIS_MODULE,
509915d7e5eSDavid Lebrun };
510915d7e5eSDavid Lebrun
seg6_init(void)511915d7e5eSDavid Lebrun int __init seg6_init(void)
512915d7e5eSDavid Lebrun {
513672e2477SColin Ian King int err;
514915d7e5eSDavid Lebrun
5159e02973dSVasiliy Kovalev err = register_pernet_subsys(&ip6_segments_ops);
516915d7e5eSDavid Lebrun if (err)
517915d7e5eSDavid Lebrun goto out;
518915d7e5eSDavid Lebrun
5199e02973dSVasiliy Kovalev err = genl_register_family(&seg6_genl_family);
520915d7e5eSDavid Lebrun if (err)
5219e02973dSVasiliy Kovalev goto out_unregister_pernet;
522915d7e5eSDavid Lebrun
52346738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
5246c8702c6SDavid Lebrun err = seg6_iptunnel_init();
5256c8702c6SDavid Lebrun if (err)
5269e02973dSVasiliy Kovalev goto out_unregister_genl;
527d1df6fd8SDavid Lebrun
528d1df6fd8SDavid Lebrun err = seg6_local_init();
5299e02973dSVasiliy Kovalev if (err) {
5309e02973dSVasiliy Kovalev seg6_iptunnel_exit();
5319e02973dSVasiliy Kovalev goto out_unregister_genl;
5329e02973dSVasiliy Kovalev }
53346738b13SDavid Lebrun #endif
5346c8702c6SDavid Lebrun
5354f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5364f4853dcSDavid Lebrun err = seg6_hmac_init();
5374f4853dcSDavid Lebrun if (err)
5384f4853dcSDavid Lebrun goto out_unregister_iptun;
5394f4853dcSDavid Lebrun #endif
5404f4853dcSDavid Lebrun
541915d7e5eSDavid Lebrun pr_info("Segment Routing with IPv6\n");
542915d7e5eSDavid Lebrun
543915d7e5eSDavid Lebrun out:
544915d7e5eSDavid Lebrun return err;
5454f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5464f4853dcSDavid Lebrun out_unregister_iptun:
54746738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
548d1df6fd8SDavid Lebrun seg6_local_exit();
5494f4853dcSDavid Lebrun seg6_iptunnel_exit();
5504f4853dcSDavid Lebrun #endif
55146738b13SDavid Lebrun #endif
55246738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
553915d7e5eSDavid Lebrun out_unregister_genl:
554*3398a40dSHangbin Liu #endif
555*3398a40dSHangbin Liu #if IS_ENABLED(CONFIG_IPV6_SEG6_LWTUNNEL) || IS_ENABLED(CONFIG_IPV6_SEG6_HMAC)
556915d7e5eSDavid Lebrun genl_unregister_family(&seg6_genl_family);
5579e02973dSVasiliy Kovalev #endif
5589e02973dSVasiliy Kovalev out_unregister_pernet:
5599e02973dSVasiliy Kovalev unregister_pernet_subsys(&ip6_segments_ops);
560915d7e5eSDavid Lebrun goto out;
561915d7e5eSDavid Lebrun }
562915d7e5eSDavid Lebrun
seg6_exit(void)563915d7e5eSDavid Lebrun void seg6_exit(void)
564915d7e5eSDavid Lebrun {
5654f4853dcSDavid Lebrun #ifdef CONFIG_IPV6_SEG6_HMAC
5664f4853dcSDavid Lebrun seg6_hmac_exit();
5674f4853dcSDavid Lebrun #endif
56846738b13SDavid Lebrun #ifdef CONFIG_IPV6_SEG6_LWTUNNEL
56958fd673bSHangbin Liu seg6_local_exit();
5706c8702c6SDavid Lebrun seg6_iptunnel_exit();
57146738b13SDavid Lebrun #endif
572915d7e5eSDavid Lebrun genl_unregister_family(&seg6_genl_family);
5736c6b74edSHangbin Liu unregister_pernet_subsys(&ip6_segments_ops);
574915d7e5eSDavid Lebrun }
575