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