1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * SR-IPv6 implementation 4 * 5 * Author: 6 * David Lebrun <david.lebrun@uclouvain.be> 7 */ 8 9#include <linux/errno.h> 10#include <linux/types.h> 11#include <linux/socket.h> 12#include <linux/net.h> 13#include <linux/in6.h> 14#include <linux/slab.h> 15#include <linux/rhashtable.h> 16 17#include <net/ipv6.h> 18#include <net/protocol.h> 19 20#include <net/seg6.h> 21#include <net/genetlink.h> 22#include <linux/seg6.h> 23#include <linux/seg6_genl.h> 24#ifdef CONFIG_IPV6_SEG6_HMAC 25#include <net/seg6_hmac.h> 26#endif 27 28bool seg6_validate_srh(struct ipv6_sr_hdr *srh, int len, bool reduced) 29{ 30 unsigned int tlv_offset; 31 int max_last_entry; 32 int trailing; 33 34 if (srh->type != IPV6_SRCRT_TYPE_4) 35 return false; 36 37 if (((srh->hdrlen + 1) << 3) != len) 38 return false; 39 40 if (!reduced && srh->segments_left > srh->first_segment) { 41 return false; 42 } else { 43 max_last_entry = (srh->hdrlen / 2) - 1; 44 45 if (srh->first_segment > max_last_entry) 46 return false; 47 48 if (srh->segments_left > srh->first_segment + 1) 49 return false; 50 } 51 52 tlv_offset = sizeof(*srh) + ((srh->first_segment + 1) << 4); 53 54 trailing = len - tlv_offset; 55 if (trailing < 0) 56 return false; 57 58 while (trailing) { 59 struct sr6_tlv *tlv; 60 unsigned int tlv_len; 61 62 if (trailing < sizeof(*tlv)) 63 return false; 64 65 tlv = (struct sr6_tlv *)((unsigned char *)srh + tlv_offset); 66 tlv_len = sizeof(*tlv) + tlv->len; 67 68 trailing -= tlv_len; 69 if (trailing < 0) 70 return false; 71 72 tlv_offset += tlv_len; 73 } 74 75 return true; 76} 77 78static struct genl_family seg6_genl_family; 79 80static const struct nla_policy seg6_genl_policy[SEG6_ATTR_MAX + 1] = { 81 [SEG6_ATTR_DST] = { .type = NLA_BINARY, 82 .len = sizeof(struct in6_addr) }, 83 [SEG6_ATTR_DSTLEN] = { .type = NLA_S32, }, 84 [SEG6_ATTR_HMACKEYID] = { .type = NLA_U32, }, 85 [SEG6_ATTR_SECRET] = { .type = NLA_BINARY, }, 86 [SEG6_ATTR_SECRETLEN] = { .type = NLA_U8, }, 87 [SEG6_ATTR_ALGID] = { .type = NLA_U8, }, 88 [SEG6_ATTR_HMACINFO] = { .type = NLA_NESTED, }, 89}; 90 91#ifdef CONFIG_IPV6_SEG6_HMAC 92 93static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info) 94{ 95 struct net *net = genl_info_net(info); 96 struct seg6_pernet_data *sdata; 97 struct seg6_hmac_info *hinfo; 98 u32 hmackeyid; 99 char *secret; 100 int err = 0; 101 u8 algid; 102 u8 slen; 103 104 sdata = seg6_pernet(net); 105 106 if (!info->attrs[SEG6_ATTR_HMACKEYID] || 107 !info->attrs[SEG6_ATTR_SECRETLEN] || 108 !info->attrs[SEG6_ATTR_ALGID]) 109 return -EINVAL; 110 111 hmackeyid = nla_get_u32(info->attrs[SEG6_ATTR_HMACKEYID]); 112 slen = nla_get_u8(info->attrs[SEG6_ATTR_SECRETLEN]); 113 algid = nla_get_u8(info->attrs[SEG6_ATTR_ALGID]); 114 115 if (hmackeyid == 0) 116 return -EINVAL; 117 118 if (slen > SEG6_HMAC_SECRET_LEN) 119 return -EINVAL; 120 121 mutex_lock(&sdata->lock); 122 hinfo = seg6_hmac_info_lookup(net, hmackeyid); 123 124 if (!slen) { 125 if (!hinfo) 126 err = -ENOENT; 127 128 err = seg6_hmac_info_del(net, hmackeyid); 129 130 goto out_unlock; 131 } 132 133 if (!info->attrs[SEG6_ATTR_SECRET]) { 134 err = -EINVAL; 135 goto out_unlock; 136 } 137 138 if (slen > nla_len(info->attrs[SEG6_ATTR_SECRET])) { 139 err = -EINVAL; 140 goto out_unlock; 141 } 142 143 if (hinfo) { 144 err = seg6_hmac_info_del(net, hmackeyid); 145 if (err) 146 goto out_unlock; 147 } 148 149 secret = (char *)nla_data(info->attrs[SEG6_ATTR_SECRET]); 150 151 hinfo = kzalloc(sizeof(*hinfo), GFP_KERNEL); 152 if (!hinfo) { 153 err = -ENOMEM; 154 goto out_unlock; 155 } 156 157 memcpy(hinfo->secret, secret, slen); 158 hinfo->slen = slen; 159 hinfo->alg_id = algid; 160 hinfo->hmackeyid = hmackeyid; 161 162 err = seg6_hmac_info_add(net, hmackeyid, hinfo); 163 if (err) 164 kfree(hinfo); 165 166out_unlock: 167 mutex_unlock(&sdata->lock); 168 return err; 169} 170 171#else 172 173static int seg6_genl_sethmac(struct sk_buff *skb, struct genl_info *info) 174{ 175 return -ENOTSUPP; 176} 177 178#endif 179 180static int seg6_genl_set_tunsrc(struct sk_buff *skb, struct genl_info *info) 181{ 182 struct net *net = genl_info_net(info); 183 struct in6_addr *val, *t_old, *t_new; 184 struct seg6_pernet_data *sdata; 185 186 sdata = seg6_pernet(net); 187 188 if (!info->attrs[SEG6_ATTR_DST]) 189 return -EINVAL; 190 191 val = nla_data(info->attrs[SEG6_ATTR_DST]); 192 t_new = kmemdup(val, sizeof(*val), GFP_KERNEL); 193 if (!t_new) 194 return -ENOMEM; 195 196 mutex_lock(&sdata->lock); 197 198 t_old = sdata->tun_src; 199 rcu_assign_pointer(sdata->tun_src, t_new); 200 201 mutex_unlock(&sdata->lock); 202 203 synchronize_net(); 204 kfree(t_old); 205 206 return 0; 207} 208 209static int seg6_genl_get_tunsrc(struct sk_buff *skb, struct genl_info *info) 210{ 211 struct net *net = genl_info_net(info); 212 struct in6_addr *tun_src; 213 struct sk_buff *msg; 214 void *hdr; 215 216 msg = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 217 if (!msg) 218 return -ENOMEM; 219 220 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, 221 &seg6_genl_family, 0, SEG6_CMD_GET_TUNSRC); 222 if (!hdr) 223 goto free_msg; 224 225 rcu_read_lock(); 226 tun_src = rcu_dereference(seg6_pernet(net)->tun_src); 227 228 if (nla_put(msg, SEG6_ATTR_DST, sizeof(struct in6_addr), tun_src)) 229 goto nla_put_failure; 230 231 rcu_read_unlock(); 232 233 genlmsg_end(msg, hdr); 234 return genlmsg_reply(msg, info); 235 236nla_put_failure: 237 rcu_read_unlock(); 238free_msg: 239 nlmsg_free(msg); 240 return -ENOMEM; 241} 242 243#ifdef CONFIG_IPV6_SEG6_HMAC 244 245static int __seg6_hmac_fill_info(struct seg6_hmac_info *hinfo, 246 struct sk_buff *msg) 247{ 248 if (nla_put_u32(msg, SEG6_ATTR_HMACKEYID, hinfo->hmackeyid) || 249 nla_put_u8(msg, SEG6_ATTR_SECRETLEN, hinfo->slen) || 250 nla_put(msg, SEG6_ATTR_SECRET, hinfo->slen, hinfo->secret) || 251 nla_put_u8(msg, SEG6_ATTR_ALGID, hinfo->alg_id)) 252 return -1; 253 254 return 0; 255} 256 257static int __seg6_genl_dumphmac_element(struct seg6_hmac_info *hinfo, 258 u32 portid, u32 seq, u32 flags, 259 struct sk_buff *skb, u8 cmd) 260{ 261 void *hdr; 262 263 hdr = genlmsg_put(skb, portid, seq, &seg6_genl_family, flags, cmd); 264 if (!hdr) 265 return -ENOMEM; 266 267 if (__seg6_hmac_fill_info(hinfo, skb) < 0) 268 goto nla_put_failure; 269 270 genlmsg_end(skb, hdr); 271 return 0; 272 273nla_put_failure: 274 genlmsg_cancel(skb, hdr); 275 return -EMSGSIZE; 276} 277 278static int seg6_genl_dumphmac_start(struct netlink_callback *cb) 279{ 280 struct net *net = sock_net(cb->skb->sk); 281 struct seg6_pernet_data *sdata; 282 struct rhashtable_iter *iter; 283 284 sdata = seg6_pernet(net); 285 iter = (struct rhashtable_iter *)cb->args[0]; 286 287 if (!iter) { 288 iter = kmalloc(sizeof(*iter), GFP_KERNEL); 289 if (!iter) 290 return -ENOMEM; 291 292 cb->args[0] = (long)iter; 293 } 294 295 rhashtable_walk_enter(&sdata->hmac_infos, iter); 296 297 return 0; 298} 299 300static int seg6_genl_dumphmac_done(struct netlink_callback *cb) 301{ 302 struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0]; 303 304 rhashtable_walk_exit(iter); 305 306 kfree(iter); 307 308 return 0; 309} 310 311static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb) 312{ 313 struct rhashtable_iter *iter = (struct rhashtable_iter *)cb->args[0]; 314 struct seg6_hmac_info *hinfo; 315 int ret; 316 317 rhashtable_walk_start(iter); 318 319 for (;;) { 320 hinfo = rhashtable_walk_next(iter); 321 322 if (IS_ERR(hinfo)) { 323 if (PTR_ERR(hinfo) == -EAGAIN) 324 continue; 325 ret = PTR_ERR(hinfo); 326 goto done; 327 } else if (!hinfo) { 328 break; 329 } 330 331 ret = __seg6_genl_dumphmac_element(hinfo, 332 NETLINK_CB(cb->skb).portid, 333 cb->nlh->nlmsg_seq, 334 NLM_F_MULTI, 335 skb, SEG6_CMD_DUMPHMAC); 336 if (ret) 337 goto done; 338 } 339 340 ret = skb->len; 341 342done: 343 rhashtable_walk_stop(iter); 344 return ret; 345} 346 347#else 348 349static int seg6_genl_dumphmac_start(struct netlink_callback *cb) 350{ 351 return 0; 352} 353 354static int seg6_genl_dumphmac_done(struct netlink_callback *cb) 355{ 356 return 0; 357} 358 359static int seg6_genl_dumphmac(struct sk_buff *skb, struct netlink_callback *cb) 360{ 361 return -ENOTSUPP; 362} 363 364#endif 365 366static int __net_init seg6_net_init(struct net *net) 367{ 368 struct seg6_pernet_data *sdata; 369 370 sdata = kzalloc(sizeof(*sdata), GFP_KERNEL); 371 if (!sdata) 372 return -ENOMEM; 373 374 mutex_init(&sdata->lock); 375 376 sdata->tun_src = kzalloc(sizeof(*sdata->tun_src), GFP_KERNEL); 377 if (!sdata->tun_src) { 378 kfree(sdata); 379 return -ENOMEM; 380 } 381 382 net->ipv6.seg6_data = sdata; 383 384#ifdef CONFIG_IPV6_SEG6_HMAC 385 seg6_hmac_net_init(net); 386#endif 387 388 return 0; 389} 390 391static void __net_exit seg6_net_exit(struct net *net) 392{ 393 struct seg6_pernet_data *sdata = seg6_pernet(net); 394 395#ifdef CONFIG_IPV6_SEG6_HMAC 396 seg6_hmac_net_exit(net); 397#endif 398 399 kfree(sdata->tun_src); 400 kfree(sdata); 401} 402 403static struct pernet_operations ip6_segments_ops = { 404 .init = seg6_net_init, 405 .exit = seg6_net_exit, 406}; 407 408static const struct genl_ops seg6_genl_ops[] = { 409 { 410 .cmd = SEG6_CMD_SETHMAC, 411 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 412 .doit = seg6_genl_sethmac, 413 .flags = GENL_ADMIN_PERM, 414 }, 415 { 416 .cmd = SEG6_CMD_DUMPHMAC, 417 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 418 .start = seg6_genl_dumphmac_start, 419 .dumpit = seg6_genl_dumphmac, 420 .done = seg6_genl_dumphmac_done, 421 .flags = GENL_ADMIN_PERM, 422 }, 423 { 424 .cmd = SEG6_CMD_SET_TUNSRC, 425 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 426 .doit = seg6_genl_set_tunsrc, 427 .flags = GENL_ADMIN_PERM, 428 }, 429 { 430 .cmd = SEG6_CMD_GET_TUNSRC, 431 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP, 432 .doit = seg6_genl_get_tunsrc, 433 .flags = GENL_ADMIN_PERM, 434 }, 435}; 436 437static struct genl_family seg6_genl_family __ro_after_init = { 438 .hdrsize = 0, 439 .name = SEG6_GENL_NAME, 440 .version = SEG6_GENL_VERSION, 441 .maxattr = SEG6_ATTR_MAX, 442 .policy = seg6_genl_policy, 443 .netnsok = true, 444 .parallel_ops = true, 445 .ops = seg6_genl_ops, 446 .n_ops = ARRAY_SIZE(seg6_genl_ops), 447 .module = THIS_MODULE, 448}; 449 450int __init seg6_init(void) 451{ 452 int err; 453 454 err = register_pernet_subsys(&ip6_segments_ops); 455 if (err) 456 goto out; 457 458 err = genl_register_family(&seg6_genl_family); 459 if (err) 460 goto out_unregister_pernet; 461 462#ifdef CONFIG_IPV6_SEG6_LWTUNNEL 463 err = seg6_iptunnel_init(); 464 if (err) 465 goto out_unregister_genl; 466 467 err = seg6_local_init(); 468 if (err) { 469 seg6_iptunnel_exit(); 470 goto out_unregister_genl; 471 } 472#endif 473 474#ifdef CONFIG_IPV6_SEG6_HMAC 475 err = seg6_hmac_init(); 476 if (err) 477 goto out_unregister_iptun; 478#endif 479 480 pr_info("Segment Routing with IPv6\n"); 481 482out: 483 return err; 484#ifdef CONFIG_IPV6_SEG6_HMAC 485out_unregister_iptun: 486#ifdef CONFIG_IPV6_SEG6_LWTUNNEL 487 seg6_local_exit(); 488 seg6_iptunnel_exit(); 489#endif 490#endif 491#ifdef CONFIG_IPV6_SEG6_LWTUNNEL 492out_unregister_genl: 493 genl_unregister_family(&seg6_genl_family); 494#endif 495out_unregister_pernet: 496 unregister_pernet_subsys(&ip6_segments_ops); 497 goto out; 498} 499 500void seg6_exit(void) 501{ 502#ifdef CONFIG_IPV6_SEG6_HMAC 503 seg6_hmac_exit(); 504#endif 505#ifdef CONFIG_IPV6_SEG6_LWTUNNEL 506 seg6_iptunnel_exit(); 507#endif 508 unregister_pernet_subsys(&ip6_segments_ops); 509 genl_unregister_family(&seg6_genl_family); 510} 511