Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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