linux/net/netfilter/nf_conntrack_tftp.c
<<
>>
Prefs
   1/* (C) 2001-2002 Magnus Boden <mb@ozaba.mine.nu>
   2 *
   3 * This program is free software; you can redistribute it and/or modify
   4 * it under the terms of the GNU General Public License version 2 as
   5 * published by the Free Software Foundation.
   6 */
   7
   8#include <linux/module.h>
   9#include <linux/moduleparam.h>
  10#include <linux/in.h>
  11#include <linux/udp.h>
  12#include <linux/netfilter.h>
  13
  14#include <net/netfilter/nf_conntrack.h>
  15#include <net/netfilter/nf_conntrack_tuple.h>
  16#include <net/netfilter/nf_conntrack_expect.h>
  17#include <net/netfilter/nf_conntrack_ecache.h>
  18#include <net/netfilter/nf_conntrack_helper.h>
  19#include <linux/netfilter/nf_conntrack_tftp.h>
  20
  21MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
  22MODULE_DESCRIPTION("TFTP connection tracking helper");
  23MODULE_LICENSE("GPL");
  24MODULE_ALIAS("ip_conntrack_tftp");
  25
  26#define MAX_PORTS 8
  27static unsigned short ports[MAX_PORTS];
  28static unsigned int ports_c;
  29module_param_array(ports, ushort, &ports_c, 0400);
  30MODULE_PARM_DESC(ports, "Port numbers of TFTP servers");
  31
  32unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb,
  33                                 enum ip_conntrack_info ctinfo,
  34                                 struct nf_conntrack_expect *exp) __read_mostly;
  35EXPORT_SYMBOL_GPL(nf_nat_tftp_hook);
  36
  37static int tftp_help(struct sk_buff *skb,
  38                     unsigned int protoff,
  39                     struct nf_conn *ct,
  40                     enum ip_conntrack_info ctinfo)
  41{
  42        const struct tftphdr *tfh;
  43        struct tftphdr _tftph;
  44        struct nf_conntrack_expect *exp;
  45        struct nf_conntrack_tuple *tuple;
  46        unsigned int ret = NF_ACCEPT;
  47        typeof(nf_nat_tftp_hook) nf_nat_tftp;
  48
  49        tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr),
  50                                 sizeof(_tftph), &_tftph);
  51        if (tfh == NULL)
  52                return NF_ACCEPT;
  53
  54        switch (ntohs(tfh->opcode)) {
  55        case TFTP_OPCODE_READ:
  56        case TFTP_OPCODE_WRITE:
  57                /* RRQ and WRQ works the same way */
  58                nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
  59                nf_ct_dump_tuple(&ct->tuplehash[IP_CT_DIR_REPLY].tuple);
  60
  61                exp = nf_ct_expect_alloc(ct);
  62                if (exp == NULL)
  63                        return NF_DROP;
  64                tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
  65                nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT,
  66                                  nf_ct_l3num(ct),
  67                                  &tuple->src.u3, &tuple->dst.u3,
  68                                  IPPROTO_UDP, NULL, &tuple->dst.u.udp.port);
  69
  70                pr_debug("expect: ");
  71                nf_ct_dump_tuple(&exp->tuple);
  72
  73                nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook);
  74                if (nf_nat_tftp && ct->status & IPS_NAT_MASK)
  75                        ret = nf_nat_tftp(skb, ctinfo, exp);
  76                else if (nf_ct_expect_related(exp) != 0)
  77                        ret = NF_DROP;
  78                nf_ct_expect_put(exp);
  79                break;
  80        case TFTP_OPCODE_DATA:
  81        case TFTP_OPCODE_ACK:
  82                pr_debug("Data/ACK opcode\n");
  83                break;
  84        case TFTP_OPCODE_ERROR:
  85                pr_debug("Error opcode\n");
  86                break;
  87        default:
  88                pr_debug("Unknown opcode\n");
  89        }
  90        return ret;
  91}
  92
  93static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly;
  94static char tftp_names[MAX_PORTS][2][sizeof("tftp-65535")] __read_mostly;
  95
  96static const struct nf_conntrack_expect_policy tftp_exp_policy = {
  97        .max_expected   = 1,
  98        .timeout        = 5 * 60,
  99};
 100
 101static void nf_conntrack_tftp_fini(void)
 102{
 103        int i, j;
 104
 105        for (i = 0; i < ports_c; i++) {
 106                for (j = 0; j < 2; j++)
 107                        nf_conntrack_helper_unregister(&tftp[i][j]);
 108        }
 109}
 110
 111static int __init nf_conntrack_tftp_init(void)
 112{
 113        int i, j, ret;
 114        char *tmpname;
 115
 116        if (ports_c == 0)
 117                ports[ports_c++] = TFTP_PORT;
 118
 119        for (i = 0; i < ports_c; i++) {
 120                memset(&tftp[i], 0, sizeof(tftp[i]));
 121
 122                tftp[i][0].tuple.src.l3num = AF_INET;
 123                tftp[i][1].tuple.src.l3num = AF_INET6;
 124                for (j = 0; j < 2; j++) {
 125                        tftp[i][j].tuple.dst.protonum = IPPROTO_UDP;
 126                        tftp[i][j].tuple.src.u.udp.port = htons(ports[i]);
 127                        tftp[i][j].expect_policy = &tftp_exp_policy;
 128                        tftp[i][j].me = THIS_MODULE;
 129                        tftp[i][j].help = tftp_help;
 130
 131                        tmpname = &tftp_names[i][j][0];
 132                        if (ports[i] == TFTP_PORT)
 133                                sprintf(tmpname, "tftp");
 134                        else
 135                                sprintf(tmpname, "tftp-%u", i);
 136                        tftp[i][j].name = tmpname;
 137
 138                        ret = nf_conntrack_helper_register(&tftp[i][j]);
 139                        if (ret) {
 140                                printk("nf_ct_tftp: failed to register helper "
 141                                       "for pf: %u port: %u\n",
 142                                        tftp[i][j].tuple.src.l3num, ports[i]);
 143                                nf_conntrack_tftp_fini();
 144                                return ret;
 145                        }
 146                }
 147        }
 148        return 0;
 149}
 150
 151module_init(nf_conntrack_tftp_init);
 152module_exit(nf_conntrack_tftp_fini);
 153