Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 1 | /* |
| 2 | * xfrm6_policy.c: based on xfrm4_policy.c |
| 3 | * |
| 4 | * Authors: |
| 5 | * Mitsuru KANDA @USAGI |
| 6 | * Kazunori MIYAZAWA @USAGI |
| 7 | * Kunihiro Ishiguro <kunihiro@ipinfusion.com> |
| 8 | * IPv6 support |
| 9 | * YOSHIFUJI Hideaki |
| 10 | * Split up af-specific portion |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/err.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/netdevice.h> |
| 17 | #include <net/addrconf.h> |
| 18 | #include <net/dst.h> |
| 19 | #include <net/xfrm.h> |
| 20 | #include <net/ip.h> |
| 21 | #include <net/ipv6.h> |
| 22 | #include <net/ip6_route.h> |
| 23 | #include <net/l3mdev.h> |
| 24 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 25 | #include <net/mip6.h> |
| 26 | #endif |
| 27 | |
| 28 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo; |
| 29 | |
| 30 | static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif, |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 31 | int mark, |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 32 | const xfrm_address_t *saddr, |
| 33 | const xfrm_address_t *daddr) |
| 34 | { |
| 35 | struct flowi6 fl6; |
| 36 | struct dst_entry *dst; |
| 37 | int err; |
| 38 | |
| 39 | memset(&fl6, 0, sizeof(fl6)); |
| 40 | fl6.flowi6_oif = oif; |
| 41 | fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF; |
| 42 | memcpy(&fl6.daddr, daddr, sizeof(fl6.daddr)); |
| 43 | if (saddr) |
| 44 | memcpy(&fl6.saddr, saddr, sizeof(fl6.saddr)); |
| 45 | |
| 46 | dst = ip6_route_output(net, NULL, &fl6); |
| 47 | |
| 48 | err = dst->error; |
| 49 | if (dst->error) { |
| 50 | dst_release(dst); |
| 51 | dst = ERR_PTR(err); |
| 52 | } |
| 53 | |
| 54 | return dst; |
| 55 | } |
| 56 | |
| 57 | static int xfrm6_get_saddr(struct net *net, int oif, |
| 58 | xfrm_address_t *saddr, xfrm_address_t *daddr) |
| 59 | { |
| 60 | struct dst_entry *dst; |
| 61 | struct net_device *dev; |
| 62 | |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 63 | dst = xfrm6_dst_lookup(net, 0, oif, 0, NULL, daddr); |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 64 | if (IS_ERR(dst)) |
| 65 | return -EHOSTUNREACH; |
| 66 | |
| 67 | dev = ip6_dst_idev(dst)->dev; |
| 68 | ipv6_dev_get_saddr(dev_net(dev), dev, &daddr->in6, 0, &saddr->in6); |
| 69 | dst_release(dst); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int xfrm6_get_tos(const struct flowi *fl) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 78 | static int xfrm6_get_mark(const struct flowi *fl) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 83 | static int xfrm6_init_path(struct xfrm_dst *path, struct dst_entry *dst, |
| 84 | int nfheader_len) |
| 85 | { |
| 86 | if (dst->ops->family == AF_INET6) { |
| 87 | struct rt6_info *rt = (struct rt6_info *)dst; |
| 88 | path->path_cookie = rt6_get_cookie(rt); |
| 89 | } |
| 90 | |
| 91 | path->u.rt6.rt6i_nfheader_len = nfheader_len; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int xfrm6_fill_dst(struct xfrm_dst *xdst, struct net_device *dev, |
| 97 | const struct flowi *fl) |
| 98 | { |
| 99 | struct rt6_info *rt = (struct rt6_info *)xdst->route; |
| 100 | |
| 101 | xdst->u.dst.dev = dev; |
| 102 | dev_hold(dev); |
| 103 | |
| 104 | xdst->u.rt6.rt6i_idev = in6_dev_get(dev); |
| 105 | if (!xdst->u.rt6.rt6i_idev) { |
| 106 | dev_put(dev); |
| 107 | return -ENODEV; |
| 108 | } |
| 109 | |
| 110 | /* Sheit... I remember I did this right. Apparently, |
| 111 | * it was magically lost, so this code needs audit */ |
| 112 | xdst->u.rt6.rt6i_flags = rt->rt6i_flags & (RTF_ANYCAST | |
| 113 | RTF_LOCAL); |
| 114 | xdst->u.rt6.rt6i_metric = rt->rt6i_metric; |
| 115 | xdst->u.rt6.rt6i_node = rt->rt6i_node; |
| 116 | xdst->route_cookie = rt6_get_cookie(rt); |
| 117 | xdst->u.rt6.rt6i_gateway = rt->rt6i_gateway; |
| 118 | xdst->u.rt6.rt6i_dst = rt->rt6i_dst; |
| 119 | xdst->u.rt6.rt6i_src = rt->rt6i_src; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static inline void |
| 125 | _decode_session6(struct sk_buff *skb, struct flowi *fl, int reverse) |
| 126 | { |
| 127 | struct flowi6 *fl6 = &fl->u.ip6; |
| 128 | int onlyproto = 0; |
| 129 | const struct ipv6hdr *hdr = ipv6_hdr(skb); |
| 130 | u16 offset = sizeof(*hdr); |
| 131 | struct ipv6_opt_hdr *exthdr; |
| 132 | const unsigned char *nh = skb_network_header(skb); |
| 133 | u16 nhoff = IP6CB(skb)->nhoff; |
| 134 | int oif = 0; |
| 135 | u8 nexthdr; |
| 136 | |
| 137 | if (!nhoff) |
| 138 | nhoff = offsetof(struct ipv6hdr, nexthdr); |
| 139 | |
| 140 | nexthdr = nh[nhoff]; |
| 141 | |
| 142 | if (skb_dst(skb)) |
| 143 | oif = l3mdev_fib_oif(skb_dst(skb)->dev); |
| 144 | |
| 145 | memset(fl6, 0, sizeof(struct flowi6)); |
| 146 | fl6->flowi6_mark = skb->mark; |
| 147 | fl6->flowi6_oif = reverse ? skb->skb_iif : oif; |
| 148 | |
| 149 | fl6->daddr = reverse ? hdr->saddr : hdr->daddr; |
| 150 | fl6->saddr = reverse ? hdr->daddr : hdr->saddr; |
| 151 | |
| 152 | while (nh + offset + 1 < skb->data || |
| 153 | pskb_may_pull(skb, nh + offset + 1 - skb->data)) { |
| 154 | nh = skb_network_header(skb); |
| 155 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
| 156 | |
| 157 | switch (nexthdr) { |
| 158 | case NEXTHDR_FRAGMENT: |
| 159 | onlyproto = 1; |
| 160 | case NEXTHDR_ROUTING: |
| 161 | case NEXTHDR_HOP: |
| 162 | case NEXTHDR_DEST: |
| 163 | offset += ipv6_optlen(exthdr); |
| 164 | nexthdr = exthdr->nexthdr; |
| 165 | exthdr = (struct ipv6_opt_hdr *)(nh + offset); |
| 166 | break; |
| 167 | |
| 168 | case IPPROTO_UDP: |
| 169 | case IPPROTO_UDPLITE: |
| 170 | case IPPROTO_TCP: |
| 171 | case IPPROTO_SCTP: |
| 172 | case IPPROTO_DCCP: |
| 173 | if (!onlyproto && (nh + offset + 4 < skb->data || |
| 174 | pskb_may_pull(skb, nh + offset + 4 - skb->data))) { |
| 175 | __be16 *ports; |
| 176 | |
| 177 | nh = skb_network_header(skb); |
| 178 | ports = (__be16 *)(nh + offset); |
| 179 | fl6->fl6_sport = ports[!!reverse]; |
| 180 | fl6->fl6_dport = ports[!reverse]; |
| 181 | } |
| 182 | fl6->flowi6_proto = nexthdr; |
| 183 | return; |
| 184 | |
| 185 | case IPPROTO_ICMPV6: |
| 186 | if (!onlyproto && (nh + offset + 2 < skb->data || |
| 187 | pskb_may_pull(skb, nh + offset + 2 - skb->data))) { |
| 188 | u8 *icmp; |
| 189 | |
| 190 | nh = skb_network_header(skb); |
| 191 | icmp = (u8 *)(nh + offset); |
| 192 | fl6->fl6_icmp_type = icmp[0]; |
| 193 | fl6->fl6_icmp_code = icmp[1]; |
| 194 | } |
| 195 | fl6->flowi6_proto = nexthdr; |
| 196 | return; |
| 197 | |
| 198 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
| 199 | case IPPROTO_MH: |
| 200 | offset += ipv6_optlen(exthdr); |
| 201 | if (!onlyproto && (nh + offset + 3 < skb->data || |
| 202 | pskb_may_pull(skb, nh + offset + 3 - skb->data))) { |
| 203 | struct ip6_mh *mh; |
| 204 | |
| 205 | nh = skb_network_header(skb); |
| 206 | mh = (struct ip6_mh *)(nh + offset); |
| 207 | fl6->fl6_mh_type = mh->ip6mh_type; |
| 208 | } |
| 209 | fl6->flowi6_proto = nexthdr; |
| 210 | return; |
| 211 | #endif |
| 212 | |
| 213 | /* XXX Why are there these headers? */ |
| 214 | case IPPROTO_AH: |
| 215 | case IPPROTO_ESP: |
| 216 | case IPPROTO_COMP: |
| 217 | default: |
| 218 | fl6->fl6_ipsec_spi = 0; |
| 219 | fl6->flowi6_proto = nexthdr; |
| 220 | return; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static inline int xfrm6_garbage_collect(struct dst_ops *ops) |
| 226 | { |
| 227 | struct net *net = container_of(ops, struct net, xfrm.xfrm6_dst_ops); |
| 228 | |
| 229 | xfrm6_policy_afinfo.garbage_collect(net); |
| 230 | return dst_entries_get_fast(ops) > ops->gc_thresh * 2; |
| 231 | } |
| 232 | |
| 233 | static void xfrm6_update_pmtu(struct dst_entry *dst, struct sock *sk, |
| 234 | struct sk_buff *skb, u32 mtu) |
| 235 | { |
| 236 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 237 | struct dst_entry *path = xdst->route; |
| 238 | |
| 239 | path->ops->update_pmtu(path, sk, skb, mtu); |
| 240 | } |
| 241 | |
| 242 | static void xfrm6_redirect(struct dst_entry *dst, struct sock *sk, |
| 243 | struct sk_buff *skb) |
| 244 | { |
| 245 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 246 | struct dst_entry *path = xdst->route; |
| 247 | |
| 248 | path->ops->redirect(path, sk, skb); |
| 249 | } |
| 250 | |
| 251 | static void xfrm6_dst_destroy(struct dst_entry *dst) |
| 252 | { |
| 253 | struct xfrm_dst *xdst = (struct xfrm_dst *)dst; |
| 254 | |
| 255 | if (likely(xdst->u.rt6.rt6i_idev)) |
| 256 | in6_dev_put(xdst->u.rt6.rt6i_idev); |
| 257 | dst_destroy_metrics_generic(dst); |
| 258 | xfrm_dst_destroy(xdst); |
| 259 | } |
| 260 | |
| 261 | static void xfrm6_dst_ifdown(struct dst_entry *dst, struct net_device *dev, |
| 262 | int unregister) |
| 263 | { |
| 264 | struct xfrm_dst *xdst; |
| 265 | |
| 266 | if (!unregister) |
| 267 | return; |
| 268 | |
| 269 | xdst = (struct xfrm_dst *)dst; |
| 270 | if (xdst->u.rt6.rt6i_idev->dev == dev) { |
| 271 | struct inet6_dev *loopback_idev = |
| 272 | in6_dev_get(dev_net(dev)->loopback_dev); |
| 273 | BUG_ON(!loopback_idev); |
| 274 | |
| 275 | do { |
| 276 | in6_dev_put(xdst->u.rt6.rt6i_idev); |
| 277 | xdst->u.rt6.rt6i_idev = loopback_idev; |
| 278 | in6_dev_hold(loopback_idev); |
| 279 | xdst = (struct xfrm_dst *)xdst->u.dst.child; |
| 280 | } while (xdst->u.dst.xfrm); |
| 281 | |
| 282 | __in6_dev_put(loopback_idev); |
| 283 | } |
| 284 | |
| 285 | xfrm_dst_ifdown(dst, dev); |
| 286 | } |
| 287 | |
| 288 | static struct dst_ops xfrm6_dst_ops_template = { |
| 289 | .family = AF_INET6, |
| 290 | .gc = xfrm6_garbage_collect, |
| 291 | .update_pmtu = xfrm6_update_pmtu, |
| 292 | .redirect = xfrm6_redirect, |
| 293 | .cow_metrics = dst_cow_metrics_generic, |
| 294 | .destroy = xfrm6_dst_destroy, |
| 295 | .ifdown = xfrm6_dst_ifdown, |
| 296 | .local_out = __ip6_local_out, |
| 297 | .gc_thresh = INT_MAX, |
| 298 | }; |
| 299 | |
| 300 | static struct xfrm_policy_afinfo xfrm6_policy_afinfo = { |
| 301 | .family = AF_INET6, |
| 302 | .dst_ops = &xfrm6_dst_ops_template, |
| 303 | .dst_lookup = xfrm6_dst_lookup, |
| 304 | .get_saddr = xfrm6_get_saddr, |
| 305 | .decode_session = _decode_session6, |
| 306 | .get_tos = xfrm6_get_tos, |
Kyle Swenson | e01461f | 2021-03-15 11:14:57 -0600 | [diff] [blame] | 307 | .get_mark = xfrm6_get_mark, |
Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame] | 308 | .init_path = xfrm6_init_path, |
| 309 | .fill_dst = xfrm6_fill_dst, |
| 310 | .blackhole_route = ip6_blackhole_route, |
| 311 | }; |
| 312 | |
| 313 | static int __init xfrm6_policy_init(void) |
| 314 | { |
| 315 | return xfrm_policy_register_afinfo(&xfrm6_policy_afinfo); |
| 316 | } |
| 317 | |
| 318 | static void xfrm6_policy_fini(void) |
| 319 | { |
| 320 | xfrm_policy_unregister_afinfo(&xfrm6_policy_afinfo); |
| 321 | } |
| 322 | |
| 323 | #ifdef CONFIG_SYSCTL |
| 324 | static struct ctl_table xfrm6_policy_table[] = { |
| 325 | { |
| 326 | .procname = "xfrm6_gc_thresh", |
| 327 | .data = &init_net.xfrm.xfrm6_dst_ops.gc_thresh, |
| 328 | .maxlen = sizeof(int), |
| 329 | .mode = 0644, |
| 330 | .proc_handler = proc_dointvec, |
| 331 | }, |
| 332 | { } |
| 333 | }; |
| 334 | |
| 335 | static int __net_init xfrm6_net_sysctl_init(struct net *net) |
| 336 | { |
| 337 | struct ctl_table *table; |
| 338 | struct ctl_table_header *hdr; |
| 339 | |
| 340 | table = xfrm6_policy_table; |
| 341 | if (!net_eq(net, &init_net)) { |
| 342 | table = kmemdup(table, sizeof(xfrm6_policy_table), GFP_KERNEL); |
| 343 | if (!table) |
| 344 | goto err_alloc; |
| 345 | |
| 346 | table[0].data = &net->xfrm.xfrm6_dst_ops.gc_thresh; |
| 347 | } |
| 348 | |
| 349 | hdr = register_net_sysctl(net, "net/ipv6", table); |
| 350 | if (!hdr) |
| 351 | goto err_reg; |
| 352 | |
| 353 | net->ipv6.sysctl.xfrm6_hdr = hdr; |
| 354 | return 0; |
| 355 | |
| 356 | err_reg: |
| 357 | if (!net_eq(net, &init_net)) |
| 358 | kfree(table); |
| 359 | err_alloc: |
| 360 | return -ENOMEM; |
| 361 | } |
| 362 | |
| 363 | static void __net_exit xfrm6_net_sysctl_exit(struct net *net) |
| 364 | { |
| 365 | struct ctl_table *table; |
| 366 | |
| 367 | if (!net->ipv6.sysctl.xfrm6_hdr) |
| 368 | return; |
| 369 | |
| 370 | table = net->ipv6.sysctl.xfrm6_hdr->ctl_table_arg; |
| 371 | unregister_net_sysctl_table(net->ipv6.sysctl.xfrm6_hdr); |
| 372 | if (!net_eq(net, &init_net)) |
| 373 | kfree(table); |
| 374 | } |
| 375 | #else /* CONFIG_SYSCTL */ |
| 376 | static int inline xfrm6_net_sysctl_init(struct net *net) |
| 377 | { |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void inline xfrm6_net_sysctl_exit(struct net *net) |
| 382 | { |
| 383 | } |
| 384 | #endif |
| 385 | |
| 386 | static int __net_init xfrm6_net_init(struct net *net) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | memcpy(&net->xfrm.xfrm6_dst_ops, &xfrm6_dst_ops_template, |
| 391 | sizeof(xfrm6_dst_ops_template)); |
| 392 | ret = dst_entries_init(&net->xfrm.xfrm6_dst_ops); |
| 393 | if (ret) |
| 394 | return ret; |
| 395 | |
| 396 | ret = xfrm6_net_sysctl_init(net); |
| 397 | if (ret) |
| 398 | dst_entries_destroy(&net->xfrm.xfrm6_dst_ops); |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static void __net_exit xfrm6_net_exit(struct net *net) |
| 404 | { |
| 405 | xfrm6_net_sysctl_exit(net); |
| 406 | dst_entries_destroy(&net->xfrm.xfrm6_dst_ops); |
| 407 | } |
| 408 | |
| 409 | static struct pernet_operations xfrm6_net_ops = { |
| 410 | .init = xfrm6_net_init, |
| 411 | .exit = xfrm6_net_exit, |
| 412 | }; |
| 413 | |
| 414 | int __init xfrm6_init(void) |
| 415 | { |
| 416 | int ret; |
| 417 | |
| 418 | ret = xfrm6_policy_init(); |
| 419 | if (ret) |
| 420 | goto out; |
| 421 | ret = xfrm6_state_init(); |
| 422 | if (ret) |
| 423 | goto out_policy; |
| 424 | |
| 425 | ret = xfrm6_protocol_init(); |
| 426 | if (ret) |
| 427 | goto out_state; |
| 428 | |
| 429 | register_pernet_subsys(&xfrm6_net_ops); |
| 430 | out: |
| 431 | return ret; |
| 432 | out_state: |
| 433 | xfrm6_state_fini(); |
| 434 | out_policy: |
| 435 | xfrm6_policy_fini(); |
| 436 | goto out; |
| 437 | } |
| 438 | |
| 439 | void xfrm6_fini(void) |
| 440 | { |
| 441 | unregister_pernet_subsys(&xfrm6_net_ops); |
| 442 | xfrm6_protocol_fini(); |
| 443 | xfrm6_policy_fini(); |
| 444 | xfrm6_state_fini(); |
| 445 | } |