linux/net/ipv6/netfilter/nf_conntrack_l3proto_ipv6.c
<<
>>
Prefs
   1/*
   2 * Copyright (C)2004 USAGI/WIDE Project
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License version 2 as
   6 * published by the Free Software Foundation.
   7 *
   8 * Author:
   9 *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
  10 */
  11
  12#include <linux/types.h>
  13#include <linux/ipv6.h>
  14#include <linux/in6.h>
  15#include <linux/netfilter.h>
  16#include <linux/module.h>
  17#include <linux/skbuff.h>
  18#include <linux/icmp.h>
  19#include <linux/sysctl.h>
  20#include <net/ipv6.h>
  21#include <net/inet_frag.h>
  22
  23#include <linux/netfilter_ipv6.h>
  24#include <net/netfilter/nf_conntrack.h>
  25#include <net/netfilter/nf_conntrack_helper.h>
  26#include <net/netfilter/nf_conntrack_l4proto.h>
  27#include <net/netfilter/nf_conntrack_l3proto.h>
  28#include <net/netfilter/nf_conntrack_core.h>
  29
  30static bool ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
  31                              struct nf_conntrack_tuple *tuple)
  32{
  33        const u_int32_t *ap;
  34        u_int32_t _addrs[8];
  35
  36        ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
  37                                sizeof(_addrs), _addrs);
  38        if (ap == NULL)
  39                return false;
  40
  41        memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
  42        memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
  43
  44        return true;
  45}
  46
  47static bool ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
  48                              const struct nf_conntrack_tuple *orig)
  49{
  50        memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
  51        memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
  52
  53        return true;
  54}
  55
  56static int ipv6_print_tuple(struct seq_file *s,
  57                            const struct nf_conntrack_tuple *tuple)
  58{
  59        return seq_printf(s, "src=" NIP6_FMT " dst=" NIP6_FMT " ",
  60                          NIP6(*((struct in6_addr *)tuple->src.u3.ip6)),
  61                          NIP6(*((struct in6_addr *)tuple->dst.u3.ip6)));
  62}
  63
  64/*
  65 * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
  66 *
  67 * This function parses (probably truncated) exthdr set "hdr"
  68 * of length "len". "nexthdrp" initially points to some place,
  69 * where type of the first header can be found.
  70 *
  71 * It skips all well-known exthdrs, and returns pointer to the start
  72 * of unparsable area i.e. the first header with unknown type.
  73 * if success, *nexthdr is updated by type/protocol of this header.
  74 *
  75 * NOTES: - it may return pointer pointing beyond end of packet,
  76 *          if the last recognized header is truncated in the middle.
  77 *        - if packet is truncated, so that all parsed headers are skipped,
  78 *          it returns -1.
  79 *        - if packet is fragmented, return pointer of the fragment header.
  80 *        - ESP is unparsable for now and considered like
  81 *          normal payload protocol.
  82 *        - Note also special handling of AUTH header. Thanks to IPsec wizards.
  83 */
  84
  85static int nf_ct_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
  86                                  u8 *nexthdrp, int len)
  87{
  88        u8 nexthdr = *nexthdrp;
  89
  90        while (ipv6_ext_hdr(nexthdr)) {
  91                struct ipv6_opt_hdr hdr;
  92                int hdrlen;
  93
  94                if (len < (int)sizeof(struct ipv6_opt_hdr))
  95                        return -1;
  96                if (nexthdr == NEXTHDR_NONE)
  97                        break;
  98                if (nexthdr == NEXTHDR_FRAGMENT)
  99                        break;
 100                if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
 101                        BUG();
 102                if (nexthdr == NEXTHDR_AUTH)
 103                        hdrlen = (hdr.hdrlen+2)<<2;
 104                else
 105                        hdrlen = ipv6_optlen(&hdr);
 106
 107                nexthdr = hdr.nexthdr;
 108                len -= hdrlen;
 109                start += hdrlen;
 110        }
 111
 112        *nexthdrp = nexthdr;
 113        return start;
 114}
 115
 116static int ipv6_get_l4proto(const struct sk_buff *skb, unsigned int nhoff,
 117                            unsigned int *dataoff, u_int8_t *protonum)
 118{
 119        unsigned int extoff = nhoff + sizeof(struct ipv6hdr);
 120        unsigned char pnum;
 121        int protoff;
 122
 123        if (skb_copy_bits(skb, nhoff + offsetof(struct ipv6hdr, nexthdr),
 124                          &pnum, sizeof(pnum)) != 0) {
 125                pr_debug("ip6_conntrack_core: can't get nexthdr\n");
 126                return -NF_ACCEPT;
 127        }
 128        protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum, skb->len - extoff);
 129        /*
 130         * (protoff == skb->len) mean that the packet doesn't have no data
 131         * except of IPv6 & ext headers. but it's tracked anyway. - YK
 132         */
 133        if ((protoff < 0) || (protoff > skb->len)) {
 134                pr_debug("ip6_conntrack_core: can't find proto in pkt\n");
 135                return -NF_ACCEPT;
 136        }
 137
 138        *dataoff = protoff;
 139        *protonum = pnum;
 140        return NF_ACCEPT;
 141}
 142
 143static unsigned int ipv6_confirm(unsigned int hooknum,
 144                                 struct sk_buff *skb,
 145                                 const struct net_device *in,
 146                                 const struct net_device *out,
 147                                 int (*okfn)(struct sk_buff *))
 148{
 149        struct nf_conn *ct;
 150        const struct nf_conn_help *help;
 151        const struct nf_conntrack_helper *helper;
 152        enum ip_conntrack_info ctinfo;
 153        unsigned int ret, protoff;
 154        unsigned int extoff = (u8 *)(ipv6_hdr(skb) + 1) - skb->data;
 155        unsigned char pnum = ipv6_hdr(skb)->nexthdr;
 156
 157
 158        /* This is where we call the helper: as the packet goes out. */
 159        ct = nf_ct_get(skb, &ctinfo);
 160        if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
 161                goto out;
 162
 163        help = nfct_help(ct);
 164        if (!help)
 165                goto out;
 166        /* rcu_read_lock()ed by nf_hook_slow */
 167        helper = rcu_dereference(help->helper);
 168        if (!helper)
 169                goto out;
 170
 171        protoff = nf_ct_ipv6_skip_exthdr(skb, extoff, &pnum,
 172                                         skb->len - extoff);
 173        if (protoff > skb->len || pnum == NEXTHDR_FRAGMENT) {
 174                pr_debug("proto header not found\n");
 175                return NF_ACCEPT;
 176        }
 177
 178        ret = helper->help(skb, protoff, ct, ctinfo);
 179        if (ret != NF_ACCEPT)
 180                return ret;
 181out:
 182        /* We've seen it coming out the other side: confirm it */
 183        return nf_conntrack_confirm(skb);
 184}
 185
 186static enum ip6_defrag_users nf_ct6_defrag_user(unsigned int hooknum,
 187                                                struct sk_buff *skb)
 188{
 189        if (hooknum == NF_INET_PRE_ROUTING)
 190                return IP6_DEFRAG_CONNTRACK_IN;
 191        else
 192                return IP6_DEFRAG_CONNTRACK_OUT;
 193
 194}
 195
 196static unsigned int ipv6_defrag(unsigned int hooknum,
 197                                struct sk_buff *skb,
 198                                const struct net_device *in,
 199                                const struct net_device *out,
 200                                int (*okfn)(struct sk_buff *))
 201{
 202        struct sk_buff *reasm;
 203
 204        /* Previously seen (loopback)?  */
 205        if (skb->nfct)
 206                return NF_ACCEPT;
 207
 208        reasm = nf_ct_frag6_gather(skb, nf_ct6_defrag_user(hooknum, skb));
 209        /* queued */
 210        if (reasm == NULL)
 211                return NF_STOLEN;
 212
 213        /* error occured or not fragmented */
 214        if (reasm == skb)
 215                return NF_ACCEPT;
 216
 217        nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
 218                           (struct net_device *)out, okfn);
 219
 220        return NF_STOLEN;
 221}
 222
 223static unsigned int ipv6_conntrack_in(unsigned int hooknum,
 224                                      struct sk_buff *skb,
 225                                      const struct net_device *in,
 226                                      const struct net_device *out,
 227                                      int (*okfn)(struct sk_buff *))
 228{
 229        struct sk_buff *reasm = skb->nfct_reasm;
 230
 231        /* This packet is fragmented and has reassembled packet. */
 232        if (reasm) {
 233                /* Reassembled packet isn't parsed yet ? */
 234                if (!reasm->nfct) {
 235                        unsigned int ret;
 236
 237                        ret = nf_conntrack_in(PF_INET6, hooknum, reasm);
 238                        if (ret != NF_ACCEPT)
 239                                return ret;
 240                }
 241                nf_conntrack_get(reasm->nfct);
 242                skb->nfct = reasm->nfct;
 243                skb->nfctinfo = reasm->nfctinfo;
 244                return NF_ACCEPT;
 245        }
 246
 247        return nf_conntrack_in(PF_INET6, hooknum, skb);
 248}
 249
 250static unsigned int ipv6_conntrack_local(unsigned int hooknum,
 251                                         struct sk_buff *skb,
 252                                         const struct net_device *in,
 253                                         const struct net_device *out,
 254                                         int (*okfn)(struct sk_buff *))
 255{
 256        /* root is playing with raw sockets. */
 257        if (skb->len < sizeof(struct ipv6hdr)) {
 258                if (net_ratelimit())
 259                        printk("ipv6_conntrack_local: packet too short\n");
 260                return NF_ACCEPT;
 261        }
 262        return ipv6_conntrack_in(hooknum, skb, in, out, okfn);
 263}
 264
 265static struct nf_hook_ops ipv6_conntrack_ops[] __read_mostly = {
 266        {
 267                .hook           = ipv6_defrag,
 268                .owner          = THIS_MODULE,
 269                .pf             = PF_INET6,
 270                .hooknum        = NF_INET_PRE_ROUTING,
 271                .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
 272        },
 273        {
 274                .hook           = ipv6_conntrack_in,
 275                .owner          = THIS_MODULE,
 276                .pf             = PF_INET6,
 277                .hooknum        = NF_INET_PRE_ROUTING,
 278                .priority       = NF_IP6_PRI_CONNTRACK,
 279        },
 280        {
 281                .hook           = ipv6_conntrack_local,
 282                .owner          = THIS_MODULE,
 283                .pf             = PF_INET6,
 284                .hooknum        = NF_INET_LOCAL_OUT,
 285                .priority       = NF_IP6_PRI_CONNTRACK,
 286        },
 287        {
 288                .hook           = ipv6_defrag,
 289                .owner          = THIS_MODULE,
 290                .pf             = PF_INET6,
 291                .hooknum        = NF_INET_LOCAL_OUT,
 292                .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
 293        },
 294        {
 295                .hook           = ipv6_confirm,
 296                .owner          = THIS_MODULE,
 297                .pf             = PF_INET6,
 298                .hooknum        = NF_INET_POST_ROUTING,
 299                .priority       = NF_IP6_PRI_LAST,
 300        },
 301        {
 302                .hook           = ipv6_confirm,
 303                .owner          = THIS_MODULE,
 304                .pf             = PF_INET6,
 305                .hooknum        = NF_INET_LOCAL_IN,
 306                .priority       = NF_IP6_PRI_LAST-1,
 307        },
 308};
 309
 310#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
 311
 312#include <linux/netfilter/nfnetlink.h>
 313#include <linux/netfilter/nfnetlink_conntrack.h>
 314
 315static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
 316                                const struct nf_conntrack_tuple *tuple)
 317{
 318        NLA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
 319                &tuple->src.u3.ip6);
 320        NLA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
 321                &tuple->dst.u3.ip6);
 322        return 0;
 323
 324nla_put_failure:
 325        return -1;
 326}
 327
 328static const struct nla_policy ipv6_nla_policy[CTA_IP_MAX+1] = {
 329        [CTA_IP_V6_SRC] = { .len = sizeof(u_int32_t)*4 },
 330        [CTA_IP_V6_DST] = { .len = sizeof(u_int32_t)*4 },
 331};
 332
 333static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
 334                                struct nf_conntrack_tuple *t)
 335{
 336        if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
 337                return -EINVAL;
 338
 339        memcpy(&t->src.u3.ip6, nla_data(tb[CTA_IP_V6_SRC]),
 340               sizeof(u_int32_t) * 4);
 341        memcpy(&t->dst.u3.ip6, nla_data(tb[CTA_IP_V6_DST]),
 342               sizeof(u_int32_t) * 4);
 343
 344        return 0;
 345}
 346#endif
 347
 348struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 __read_mostly = {
 349        .l3proto                = PF_INET6,
 350        .name                   = "ipv6",
 351        .pkt_to_tuple           = ipv6_pkt_to_tuple,
 352        .invert_tuple           = ipv6_invert_tuple,
 353        .print_tuple            = ipv6_print_tuple,
 354        .get_l4proto            = ipv6_get_l4proto,
 355#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
 356        .tuple_to_nlattr        = ipv6_tuple_to_nlattr,
 357        .nlattr_to_tuple        = ipv6_nlattr_to_tuple,
 358        .nla_policy             = ipv6_nla_policy,
 359#endif
 360#ifdef CONFIG_SYSCTL
 361        .ctl_table_path         = nf_net_netfilter_sysctl_path,
 362        .ctl_table              = nf_ct_ipv6_sysctl_table,
 363#endif
 364        .me                     = THIS_MODULE,
 365};
 366
 367MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
 368MODULE_LICENSE("GPL");
 369MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
 370
 371static int __init nf_conntrack_l3proto_ipv6_init(void)
 372{
 373        int ret = 0;
 374
 375        need_conntrack();
 376
 377        ret = nf_ct_frag6_init();
 378        if (ret < 0) {
 379                printk("nf_conntrack_ipv6: can't initialize frag6.\n");
 380                return ret;
 381        }
 382        ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
 383        if (ret < 0) {
 384                printk("nf_conntrack_ipv6: can't register tcp.\n");
 385                goto cleanup_frag6;
 386        }
 387
 388        ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
 389        if (ret < 0) {
 390                printk("nf_conntrack_ipv6: can't register udp.\n");
 391                goto cleanup_tcp;
 392        }
 393
 394        ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
 395        if (ret < 0) {
 396                printk("nf_conntrack_ipv6: can't register icmpv6.\n");
 397                goto cleanup_udp;
 398        }
 399
 400        ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
 401        if (ret < 0) {
 402                printk("nf_conntrack_ipv6: can't register ipv6\n");
 403                goto cleanup_icmpv6;
 404        }
 405
 406        ret = nf_register_hooks(ipv6_conntrack_ops,
 407                                ARRAY_SIZE(ipv6_conntrack_ops));
 408        if (ret < 0) {
 409                printk("nf_conntrack_ipv6: can't register pre-routing defrag "
 410                       "hook.\n");
 411                goto cleanup_ipv6;
 412        }
 413        return ret;
 414
 415 cleanup_ipv6:
 416        nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
 417 cleanup_icmpv6:
 418        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
 419 cleanup_udp:
 420        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
 421 cleanup_tcp:
 422        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
 423 cleanup_frag6:
 424        nf_ct_frag6_cleanup();
 425        return ret;
 426}
 427
 428static void __exit nf_conntrack_l3proto_ipv6_fini(void)
 429{
 430        synchronize_net();
 431        nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
 432        nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
 433        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
 434        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
 435        nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
 436        nf_ct_frag6_cleanup();
 437}
 438
 439module_init(nf_conntrack_l3proto_ipv6_init);
 440module_exit(nf_conntrack_l3proto_ipv6_fini);
 441