David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 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/types.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/net.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <net/ip.h> |
| 19 | #include <net/lwtunnel.h> |
| 20 | #include <net/netevent.h> |
| 21 | #include <net/netns/generic.h> |
| 22 | #include <net/ip6_fib.h> |
| 23 | #include <net/route.h> |
| 24 | #include <net/seg6.h> |
| 25 | #include <linux/seg6.h> |
| 26 | #include <linux/seg6_local.h> |
| 27 | #include <net/addrconf.h> |
| 28 | #include <net/ip6_route.h> |
| 29 | #include <net/dst_cache.h> |
| 30 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 31 | #include <net/seg6_hmac.h> |
| 32 | #endif |
| 33 | |
| 34 | struct seg6_local_lwt; |
| 35 | |
| 36 | struct seg6_action_desc { |
| 37 | int action; |
| 38 | unsigned long attrs; |
| 39 | int (*input)(struct sk_buff *skb, struct seg6_local_lwt *slwt); |
| 40 | int static_headroom; |
| 41 | }; |
| 42 | |
| 43 | struct seg6_local_lwt { |
| 44 | int action; |
| 45 | struct ipv6_sr_hdr *srh; |
| 46 | int table; |
| 47 | struct in_addr nh4; |
| 48 | struct in6_addr nh6; |
| 49 | int iif; |
| 50 | int oif; |
| 51 | |
| 52 | int headroom; |
| 53 | struct seg6_action_desc *desc; |
| 54 | }; |
| 55 | |
| 56 | static struct seg6_local_lwt *seg6_local_lwtunnel(struct lwtunnel_state *lwt) |
| 57 | { |
| 58 | return (struct seg6_local_lwt *)lwt->data; |
| 59 | } |
| 60 | |
David Lebrun | 140f04c | 2017-08-05 12:39:48 +0200 | [diff] [blame] | 61 | static struct ipv6_sr_hdr *get_srh(struct sk_buff *skb) |
| 62 | { |
| 63 | struct ipv6_sr_hdr *srh; |
| 64 | struct ipv6hdr *hdr; |
| 65 | int len; |
| 66 | |
| 67 | hdr = ipv6_hdr(skb); |
| 68 | if (hdr->nexthdr != IPPROTO_ROUTING) |
| 69 | return NULL; |
| 70 | |
| 71 | srh = (struct ipv6_sr_hdr *)(hdr + 1); |
| 72 | len = (srh->hdrlen + 1) << 3; |
| 73 | |
| 74 | if (!pskb_may_pull(skb, sizeof(*hdr) + len)) |
| 75 | return NULL; |
| 76 | |
| 77 | if (!seg6_validate_srh(srh, len)) |
| 78 | return NULL; |
| 79 | |
| 80 | return srh; |
| 81 | } |
| 82 | |
| 83 | static struct ipv6_sr_hdr *get_and_validate_srh(struct sk_buff *skb) |
| 84 | { |
| 85 | struct ipv6_sr_hdr *srh; |
| 86 | |
| 87 | srh = get_srh(skb); |
| 88 | if (!srh) |
| 89 | return NULL; |
| 90 | |
| 91 | if (srh->segments_left == 0) |
| 92 | return NULL; |
| 93 | |
| 94 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 95 | if (!seg6_hmac_validate_skb(skb)) |
| 96 | return NULL; |
| 97 | #endif |
| 98 | |
| 99 | return srh; |
| 100 | } |
| 101 | |
| 102 | /* regular endpoint function */ |
| 103 | static int input_action_end(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 104 | { |
| 105 | struct ipv6_sr_hdr *srh; |
| 106 | struct in6_addr *addr; |
| 107 | |
| 108 | srh = get_and_validate_srh(skb); |
| 109 | if (!srh) |
| 110 | goto drop; |
| 111 | |
| 112 | srh->segments_left--; |
| 113 | addr = srh->segments + srh->segments_left; |
| 114 | |
| 115 | ipv6_hdr(skb)->daddr = *addr; |
| 116 | |
| 117 | skb_dst_drop(skb); |
| 118 | ip6_route_input(skb); |
| 119 | |
| 120 | return dst_input(skb); |
| 121 | |
| 122 | drop: |
| 123 | kfree_skb(skb); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | |
| 127 | /* regular endpoint, and forward to specified nexthop */ |
| 128 | static int input_action_end_x(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 129 | { |
| 130 | struct net *net = dev_net(skb->dev); |
| 131 | struct ipv6_sr_hdr *srh; |
| 132 | struct dst_entry *dst; |
| 133 | struct in6_addr *addr; |
| 134 | struct ipv6hdr *hdr; |
| 135 | struct flowi6 fl6; |
| 136 | int flags; |
| 137 | |
| 138 | srh = get_and_validate_srh(skb); |
| 139 | if (!srh) |
| 140 | goto drop; |
| 141 | |
| 142 | srh->segments_left--; |
| 143 | addr = srh->segments + srh->segments_left; |
| 144 | |
| 145 | hdr = ipv6_hdr(skb); |
| 146 | hdr->daddr = *addr; |
| 147 | |
| 148 | skb_dst_drop(skb); |
| 149 | |
| 150 | fl6.flowi6_iif = skb->dev->ifindex; |
| 151 | fl6.daddr = slwt->nh6; |
| 152 | fl6.saddr = hdr->saddr; |
| 153 | fl6.flowlabel = ip6_flowinfo(hdr); |
| 154 | fl6.flowi6_mark = skb->mark; |
| 155 | fl6.flowi6_proto = hdr->nexthdr; |
| 156 | |
| 157 | flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE | |
| 158 | RT6_LOOKUP_F_REACHABLE; |
| 159 | |
| 160 | dst = ip6_route_input_lookup(net, skb->dev, &fl6, flags); |
| 161 | if (dst->dev->flags & IFF_LOOPBACK) |
| 162 | goto drop; |
| 163 | |
| 164 | skb_dst_set(skb, dst); |
| 165 | |
| 166 | return dst_input(skb); |
| 167 | |
| 168 | drop: |
| 169 | kfree_skb(skb); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | /* decapsulate and forward to specified nexthop */ |
| 174 | static int input_action_end_dx6(struct sk_buff *skb, |
| 175 | struct seg6_local_lwt *slwt) |
| 176 | { |
| 177 | struct net *net = dev_net(skb->dev); |
| 178 | struct ipv6hdr *inner_hdr; |
| 179 | struct ipv6_sr_hdr *srh; |
| 180 | struct dst_entry *dst; |
| 181 | unsigned int off = 0; |
| 182 | struct flowi6 fl6; |
| 183 | bool use_nh; |
| 184 | int flags; |
| 185 | |
| 186 | /* this function accepts IPv6 encapsulated packets, with either |
| 187 | * an SRH with SL=0, or no SRH. |
| 188 | */ |
| 189 | |
| 190 | srh = get_srh(skb); |
| 191 | if (srh && srh->segments_left > 0) |
| 192 | goto drop; |
| 193 | |
| 194 | #ifdef CONFIG_IPV6_SEG6_HMAC |
| 195 | if (srh && !seg6_hmac_validate_skb(skb)) |
| 196 | goto drop; |
| 197 | #endif |
| 198 | |
| 199 | if (ipv6_find_hdr(skb, &off, IPPROTO_IPV6, NULL, NULL) < 0) |
| 200 | goto drop; |
| 201 | |
| 202 | if (!pskb_pull(skb, off)) |
| 203 | goto drop; |
| 204 | |
| 205 | skb_postpull_rcsum(skb, skb_network_header(skb), off); |
| 206 | |
| 207 | skb_reset_network_header(skb); |
| 208 | skb_reset_transport_header(skb); |
| 209 | skb->encapsulation = 0; |
| 210 | |
| 211 | inner_hdr = ipv6_hdr(skb); |
| 212 | |
| 213 | /* The inner packet is not associated to any local interface, |
| 214 | * so we do not call netif_rx(). |
| 215 | * |
| 216 | * If slwt->nh6 is set to ::, then lookup the nexthop for the |
| 217 | * inner packet's DA. Otherwise, use the specified nexthop. |
| 218 | */ |
| 219 | |
| 220 | use_nh = !ipv6_addr_any(&slwt->nh6); |
| 221 | |
| 222 | skb_dst_drop(skb); |
| 223 | |
| 224 | fl6.flowi6_iif = skb->dev->ifindex; |
| 225 | fl6.daddr = use_nh ? slwt->nh6 : inner_hdr->daddr; |
| 226 | fl6.saddr = inner_hdr->saddr; |
| 227 | fl6.flowlabel = ip6_flowinfo(inner_hdr); |
| 228 | fl6.flowi6_mark = skb->mark; |
| 229 | fl6.flowi6_proto = inner_hdr->nexthdr; |
| 230 | |
| 231 | flags = RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_REACHABLE; |
| 232 | if (use_nh) |
| 233 | flags |= RT6_LOOKUP_F_IFACE; |
| 234 | |
| 235 | dst = ip6_route_input_lookup(net, skb->dev, &fl6, flags); |
| 236 | if (dst->dev->flags & IFF_LOOPBACK) |
| 237 | goto drop; |
| 238 | |
| 239 | skb_dst_set(skb, dst); |
| 240 | |
| 241 | return dst_input(skb); |
| 242 | drop: |
| 243 | kfree_skb(skb); |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
| 247 | /* push an SRH on top of the current one */ |
| 248 | static int input_action_end_b6(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 249 | { |
| 250 | struct ipv6_sr_hdr *srh; |
| 251 | int err = -EINVAL; |
| 252 | |
| 253 | srh = get_and_validate_srh(skb); |
| 254 | if (!srh) |
| 255 | goto drop; |
| 256 | |
| 257 | err = seg6_do_srh_inline(skb, slwt->srh); |
| 258 | if (err) |
| 259 | goto drop; |
| 260 | |
| 261 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 262 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
| 263 | |
| 264 | skb_dst_drop(skb); |
| 265 | ip6_route_input(skb); |
| 266 | |
| 267 | return dst_input(skb); |
| 268 | |
| 269 | drop: |
| 270 | kfree_skb(skb); |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | /* encapsulate within an outer IPv6 header and a specified SRH */ |
| 275 | static int input_action_end_b6_encap(struct sk_buff *skb, |
| 276 | struct seg6_local_lwt *slwt) |
| 277 | { |
| 278 | struct ipv6_sr_hdr *srh; |
| 279 | struct in6_addr *addr; |
| 280 | int err = -EINVAL; |
| 281 | |
| 282 | srh = get_and_validate_srh(skb); |
| 283 | if (!srh) |
| 284 | goto drop; |
| 285 | |
| 286 | srh->segments_left--; |
| 287 | addr = srh->segments + srh->segments_left; |
| 288 | ipv6_hdr(skb)->daddr = *addr; |
| 289 | |
| 290 | skb_reset_inner_headers(skb); |
| 291 | skb->encapsulation = 1; |
| 292 | |
David Lebrun | 32d99d0 | 2017-08-25 09:56:44 +0200 | [diff] [blame^] | 293 | err = seg6_do_srh_encap(skb, slwt->srh, IPPROTO_IPV6); |
David Lebrun | 140f04c | 2017-08-05 12:39:48 +0200 | [diff] [blame] | 294 | if (err) |
| 295 | goto drop; |
| 296 | |
| 297 | ipv6_hdr(skb)->payload_len = htons(skb->len - sizeof(struct ipv6hdr)); |
| 298 | skb_set_transport_header(skb, sizeof(struct ipv6hdr)); |
| 299 | |
| 300 | skb_dst_drop(skb); |
| 301 | ip6_route_input(skb); |
| 302 | |
| 303 | return dst_input(skb); |
| 304 | |
| 305 | drop: |
| 306 | kfree_skb(skb); |
| 307 | return err; |
| 308 | } |
| 309 | |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 310 | static struct seg6_action_desc seg6_action_table[] = { |
| 311 | { |
| 312 | .action = SEG6_LOCAL_ACTION_END, |
| 313 | .attrs = 0, |
David Lebrun | 140f04c | 2017-08-05 12:39:48 +0200 | [diff] [blame] | 314 | .input = input_action_end, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 315 | }, |
David Lebrun | 140f04c | 2017-08-05 12:39:48 +0200 | [diff] [blame] | 316 | { |
| 317 | .action = SEG6_LOCAL_ACTION_END_X, |
| 318 | .attrs = (1 << SEG6_LOCAL_NH6), |
| 319 | .input = input_action_end_x, |
| 320 | }, |
| 321 | { |
| 322 | .action = SEG6_LOCAL_ACTION_END_DX6, |
| 323 | .attrs = (1 << SEG6_LOCAL_NH6), |
| 324 | .input = input_action_end_dx6, |
| 325 | }, |
| 326 | { |
| 327 | .action = SEG6_LOCAL_ACTION_END_B6, |
| 328 | .attrs = (1 << SEG6_LOCAL_SRH), |
| 329 | .input = input_action_end_b6, |
| 330 | }, |
| 331 | { |
| 332 | .action = SEG6_LOCAL_ACTION_END_B6_ENCAP, |
| 333 | .attrs = (1 << SEG6_LOCAL_SRH), |
| 334 | .input = input_action_end_b6_encap, |
| 335 | .static_headroom = sizeof(struct ipv6hdr), |
| 336 | } |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 337 | }; |
| 338 | |
| 339 | static struct seg6_action_desc *__get_action_desc(int action) |
| 340 | { |
| 341 | struct seg6_action_desc *desc; |
| 342 | int i, count; |
| 343 | |
| 344 | count = sizeof(seg6_action_table) / sizeof(struct seg6_action_desc); |
| 345 | for (i = 0; i < count; i++) { |
| 346 | desc = &seg6_action_table[i]; |
| 347 | if (desc->action == action) |
| 348 | return desc; |
| 349 | } |
| 350 | |
| 351 | return NULL; |
| 352 | } |
| 353 | |
| 354 | static int seg6_local_input(struct sk_buff *skb) |
| 355 | { |
| 356 | struct dst_entry *orig_dst = skb_dst(skb); |
| 357 | struct seg6_action_desc *desc; |
| 358 | struct seg6_local_lwt *slwt; |
| 359 | |
| 360 | slwt = seg6_local_lwtunnel(orig_dst->lwtstate); |
| 361 | desc = slwt->desc; |
| 362 | |
| 363 | return desc->input(skb, slwt); |
| 364 | } |
| 365 | |
| 366 | static const struct nla_policy seg6_local_policy[SEG6_LOCAL_MAX + 1] = { |
| 367 | [SEG6_LOCAL_ACTION] = { .type = NLA_U32 }, |
| 368 | [SEG6_LOCAL_SRH] = { .type = NLA_BINARY }, |
| 369 | [SEG6_LOCAL_TABLE] = { .type = NLA_U32 }, |
| 370 | [SEG6_LOCAL_NH4] = { .type = NLA_BINARY, |
| 371 | .len = sizeof(struct in_addr) }, |
| 372 | [SEG6_LOCAL_NH6] = { .type = NLA_BINARY, |
| 373 | .len = sizeof(struct in6_addr) }, |
| 374 | [SEG6_LOCAL_IIF] = { .type = NLA_U32 }, |
| 375 | [SEG6_LOCAL_OIF] = { .type = NLA_U32 }, |
| 376 | }; |
| 377 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 378 | static int parse_nla_srh(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 379 | { |
| 380 | struct ipv6_sr_hdr *srh; |
| 381 | int len; |
| 382 | |
| 383 | srh = nla_data(attrs[SEG6_LOCAL_SRH]); |
| 384 | len = nla_len(attrs[SEG6_LOCAL_SRH]); |
| 385 | |
| 386 | /* SRH must contain at least one segment */ |
| 387 | if (len < sizeof(*srh) + sizeof(struct in6_addr)) |
| 388 | return -EINVAL; |
| 389 | |
| 390 | if (!seg6_validate_srh(srh, len)) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | slwt->srh = kmalloc(len, GFP_KERNEL); |
| 394 | if (!slwt->srh) |
| 395 | return -ENOMEM; |
| 396 | |
| 397 | memcpy(slwt->srh, srh, len); |
| 398 | |
| 399 | slwt->headroom += len; |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int put_nla_srh(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 405 | { |
| 406 | struct ipv6_sr_hdr *srh; |
| 407 | struct nlattr *nla; |
| 408 | int len; |
| 409 | |
| 410 | srh = slwt->srh; |
| 411 | len = (srh->hdrlen + 1) << 3; |
| 412 | |
| 413 | nla = nla_reserve(skb, SEG6_LOCAL_SRH, len); |
| 414 | if (!nla) |
| 415 | return -EMSGSIZE; |
| 416 | |
| 417 | memcpy(nla_data(nla), srh, len); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | static int cmp_nla_srh(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 423 | { |
| 424 | int len = (a->srh->hdrlen + 1) << 3; |
| 425 | |
| 426 | if (len != ((b->srh->hdrlen + 1) << 3)) |
| 427 | return 1; |
| 428 | |
| 429 | return memcmp(a->srh, b->srh, len); |
| 430 | } |
| 431 | |
| 432 | static int parse_nla_table(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 433 | { |
| 434 | slwt->table = nla_get_u32(attrs[SEG6_LOCAL_TABLE]); |
| 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int put_nla_table(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 440 | { |
| 441 | if (nla_put_u32(skb, SEG6_LOCAL_TABLE, slwt->table)) |
| 442 | return -EMSGSIZE; |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | static int cmp_nla_table(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 448 | { |
| 449 | if (a->table != b->table) |
| 450 | return 1; |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | static int parse_nla_nh4(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 456 | { |
| 457 | memcpy(&slwt->nh4, nla_data(attrs[SEG6_LOCAL_NH4]), |
| 458 | sizeof(struct in_addr)); |
| 459 | |
| 460 | return 0; |
| 461 | } |
| 462 | |
| 463 | static int put_nla_nh4(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 464 | { |
| 465 | struct nlattr *nla; |
| 466 | |
| 467 | nla = nla_reserve(skb, SEG6_LOCAL_NH4, sizeof(struct in_addr)); |
| 468 | if (!nla) |
| 469 | return -EMSGSIZE; |
| 470 | |
| 471 | memcpy(nla_data(nla), &slwt->nh4, sizeof(struct in_addr)); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int cmp_nla_nh4(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 477 | { |
| 478 | return memcmp(&a->nh4, &b->nh4, sizeof(struct in_addr)); |
| 479 | } |
| 480 | |
| 481 | static int parse_nla_nh6(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 482 | { |
| 483 | memcpy(&slwt->nh6, nla_data(attrs[SEG6_LOCAL_NH6]), |
| 484 | sizeof(struct in6_addr)); |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int put_nla_nh6(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 490 | { |
| 491 | struct nlattr *nla; |
| 492 | |
| 493 | nla = nla_reserve(skb, SEG6_LOCAL_NH6, sizeof(struct in6_addr)); |
| 494 | if (!nla) |
| 495 | return -EMSGSIZE; |
| 496 | |
| 497 | memcpy(nla_data(nla), &slwt->nh6, sizeof(struct in6_addr)); |
| 498 | |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | static int cmp_nla_nh6(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 503 | { |
| 504 | return memcmp(&a->nh6, &b->nh6, sizeof(struct in6_addr)); |
| 505 | } |
| 506 | |
| 507 | static int parse_nla_iif(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 508 | { |
| 509 | slwt->iif = nla_get_u32(attrs[SEG6_LOCAL_IIF]); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | static int put_nla_iif(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 515 | { |
| 516 | if (nla_put_u32(skb, SEG6_LOCAL_IIF, slwt->iif)) |
| 517 | return -EMSGSIZE; |
| 518 | |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | static int cmp_nla_iif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 523 | { |
| 524 | if (a->iif != b->iif) |
| 525 | return 1; |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int parse_nla_oif(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 531 | { |
| 532 | slwt->oif = nla_get_u32(attrs[SEG6_LOCAL_OIF]); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static int put_nla_oif(struct sk_buff *skb, struct seg6_local_lwt *slwt) |
| 538 | { |
| 539 | if (nla_put_u32(skb, SEG6_LOCAL_OIF, slwt->oif)) |
| 540 | return -EMSGSIZE; |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | static int cmp_nla_oif(struct seg6_local_lwt *a, struct seg6_local_lwt *b) |
| 546 | { |
| 547 | if (a->oif != b->oif) |
| 548 | return 1; |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 553 | struct seg6_action_param { |
| 554 | int (*parse)(struct nlattr **attrs, struct seg6_local_lwt *slwt); |
| 555 | int (*put)(struct sk_buff *skb, struct seg6_local_lwt *slwt); |
| 556 | int (*cmp)(struct seg6_local_lwt *a, struct seg6_local_lwt *b); |
| 557 | }; |
| 558 | |
| 559 | static struct seg6_action_param seg6_action_params[SEG6_LOCAL_MAX + 1] = { |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 560 | [SEG6_LOCAL_SRH] = { .parse = parse_nla_srh, |
| 561 | .put = put_nla_srh, |
| 562 | .cmp = cmp_nla_srh }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 563 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 564 | [SEG6_LOCAL_TABLE] = { .parse = parse_nla_table, |
| 565 | .put = put_nla_table, |
| 566 | .cmp = cmp_nla_table }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 567 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 568 | [SEG6_LOCAL_NH4] = { .parse = parse_nla_nh4, |
| 569 | .put = put_nla_nh4, |
| 570 | .cmp = cmp_nla_nh4 }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 571 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 572 | [SEG6_LOCAL_NH6] = { .parse = parse_nla_nh6, |
| 573 | .put = put_nla_nh6, |
| 574 | .cmp = cmp_nla_nh6 }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 575 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 576 | [SEG6_LOCAL_IIF] = { .parse = parse_nla_iif, |
| 577 | .put = put_nla_iif, |
| 578 | .cmp = cmp_nla_iif }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 579 | |
David Lebrun | 2d9cc60 | 2017-08-05 12:38:27 +0200 | [diff] [blame] | 580 | [SEG6_LOCAL_OIF] = { .parse = parse_nla_oif, |
| 581 | .put = put_nla_oif, |
| 582 | .cmp = cmp_nla_oif }, |
David Lebrun | d1df6fd | 2017-08-05 12:38:26 +0200 | [diff] [blame] | 583 | }; |
| 584 | |
| 585 | static int parse_nla_action(struct nlattr **attrs, struct seg6_local_lwt *slwt) |
| 586 | { |
| 587 | struct seg6_action_param *param; |
| 588 | struct seg6_action_desc *desc; |
| 589 | int i, err; |
| 590 | |
| 591 | desc = __get_action_desc(slwt->action); |
| 592 | if (!desc) |
| 593 | return -EINVAL; |
| 594 | |
| 595 | if (!desc->input) |
| 596 | return -EOPNOTSUPP; |
| 597 | |
| 598 | slwt->desc = desc; |
| 599 | slwt->headroom += desc->static_headroom; |
| 600 | |
| 601 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { |
| 602 | if (desc->attrs & (1 << i)) { |
| 603 | if (!attrs[i]) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | param = &seg6_action_params[i]; |
| 607 | |
| 608 | err = param->parse(attrs, slwt); |
| 609 | if (err < 0) |
| 610 | return err; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int seg6_local_build_state(struct nlattr *nla, unsigned int family, |
| 618 | const void *cfg, struct lwtunnel_state **ts, |
| 619 | struct netlink_ext_ack *extack) |
| 620 | { |
| 621 | struct nlattr *tb[SEG6_LOCAL_MAX + 1]; |
| 622 | struct lwtunnel_state *newts; |
| 623 | struct seg6_local_lwt *slwt; |
| 624 | int err; |
| 625 | |
| 626 | err = nla_parse_nested(tb, SEG6_LOCAL_MAX, nla, seg6_local_policy, |
| 627 | extack); |
| 628 | |
| 629 | if (err < 0) |
| 630 | return err; |
| 631 | |
| 632 | if (!tb[SEG6_LOCAL_ACTION]) |
| 633 | return -EINVAL; |
| 634 | |
| 635 | newts = lwtunnel_state_alloc(sizeof(*slwt)); |
| 636 | if (!newts) |
| 637 | return -ENOMEM; |
| 638 | |
| 639 | slwt = seg6_local_lwtunnel(newts); |
| 640 | slwt->action = nla_get_u32(tb[SEG6_LOCAL_ACTION]); |
| 641 | |
| 642 | err = parse_nla_action(tb, slwt); |
| 643 | if (err < 0) |
| 644 | goto out_free; |
| 645 | |
| 646 | newts->type = LWTUNNEL_ENCAP_SEG6_LOCAL; |
| 647 | newts->flags = LWTUNNEL_STATE_INPUT_REDIRECT; |
| 648 | newts->headroom = slwt->headroom; |
| 649 | |
| 650 | *ts = newts; |
| 651 | |
| 652 | return 0; |
| 653 | |
| 654 | out_free: |
| 655 | kfree(slwt->srh); |
| 656 | kfree(newts); |
| 657 | return err; |
| 658 | } |
| 659 | |
| 660 | static void seg6_local_destroy_state(struct lwtunnel_state *lwt) |
| 661 | { |
| 662 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); |
| 663 | |
| 664 | kfree(slwt->srh); |
| 665 | } |
| 666 | |
| 667 | static int seg6_local_fill_encap(struct sk_buff *skb, |
| 668 | struct lwtunnel_state *lwt) |
| 669 | { |
| 670 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); |
| 671 | struct seg6_action_param *param; |
| 672 | int i, err; |
| 673 | |
| 674 | if (nla_put_u32(skb, SEG6_LOCAL_ACTION, slwt->action)) |
| 675 | return -EMSGSIZE; |
| 676 | |
| 677 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { |
| 678 | if (slwt->desc->attrs & (1 << i)) { |
| 679 | param = &seg6_action_params[i]; |
| 680 | err = param->put(skb, slwt); |
| 681 | if (err < 0) |
| 682 | return err; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int seg6_local_get_encap_size(struct lwtunnel_state *lwt) |
| 690 | { |
| 691 | struct seg6_local_lwt *slwt = seg6_local_lwtunnel(lwt); |
| 692 | unsigned long attrs; |
| 693 | int nlsize; |
| 694 | |
| 695 | nlsize = nla_total_size(4); /* action */ |
| 696 | |
| 697 | attrs = slwt->desc->attrs; |
| 698 | |
| 699 | if (attrs & (1 << SEG6_LOCAL_SRH)) |
| 700 | nlsize += nla_total_size((slwt->srh->hdrlen + 1) << 3); |
| 701 | |
| 702 | if (attrs & (1 << SEG6_LOCAL_TABLE)) |
| 703 | nlsize += nla_total_size(4); |
| 704 | |
| 705 | if (attrs & (1 << SEG6_LOCAL_NH4)) |
| 706 | nlsize += nla_total_size(4); |
| 707 | |
| 708 | if (attrs & (1 << SEG6_LOCAL_NH6)) |
| 709 | nlsize += nla_total_size(16); |
| 710 | |
| 711 | if (attrs & (1 << SEG6_LOCAL_IIF)) |
| 712 | nlsize += nla_total_size(4); |
| 713 | |
| 714 | if (attrs & (1 << SEG6_LOCAL_OIF)) |
| 715 | nlsize += nla_total_size(4); |
| 716 | |
| 717 | return nlsize; |
| 718 | } |
| 719 | |
| 720 | static int seg6_local_cmp_encap(struct lwtunnel_state *a, |
| 721 | struct lwtunnel_state *b) |
| 722 | { |
| 723 | struct seg6_local_lwt *slwt_a, *slwt_b; |
| 724 | struct seg6_action_param *param; |
| 725 | int i; |
| 726 | |
| 727 | slwt_a = seg6_local_lwtunnel(a); |
| 728 | slwt_b = seg6_local_lwtunnel(b); |
| 729 | |
| 730 | if (slwt_a->action != slwt_b->action) |
| 731 | return 1; |
| 732 | |
| 733 | if (slwt_a->desc->attrs != slwt_b->desc->attrs) |
| 734 | return 1; |
| 735 | |
| 736 | for (i = 0; i < SEG6_LOCAL_MAX + 1; i++) { |
| 737 | if (slwt_a->desc->attrs & (1 << i)) { |
| 738 | param = &seg6_action_params[i]; |
| 739 | if (param->cmp(slwt_a, slwt_b)) |
| 740 | return 1; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | return 0; |
| 745 | } |
| 746 | |
| 747 | static const struct lwtunnel_encap_ops seg6_local_ops = { |
| 748 | .build_state = seg6_local_build_state, |
| 749 | .destroy_state = seg6_local_destroy_state, |
| 750 | .input = seg6_local_input, |
| 751 | .fill_encap = seg6_local_fill_encap, |
| 752 | .get_encap_size = seg6_local_get_encap_size, |
| 753 | .cmp_encap = seg6_local_cmp_encap, |
| 754 | .owner = THIS_MODULE, |
| 755 | }; |
| 756 | |
| 757 | int __init seg6_local_init(void) |
| 758 | { |
| 759 | return lwtunnel_encap_add_ops(&seg6_local_ops, |
| 760 | LWTUNNEL_ENCAP_SEG6_LOCAL); |
| 761 | } |
| 762 | |
| 763 | void seg6_local_exit(void) |
| 764 | { |
| 765 | lwtunnel_encap_del_ops(&seg6_local_ops, LWTUNNEL_ENCAP_SEG6_LOCAL); |
| 766 | } |