Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 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> |
Yasuyuki Kozakai | 1863f09 | 2006-12-02 22:12:54 -0800 | [diff] [blame] | 12 | #include <linux/netfilter.h> |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 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 | |
| 21 | MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>"); |
| 22 | MODULE_DESCRIPTION("TFTP connection tracking helper"); |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_ALIAS("ip_conntrack_tftp"); |
| 25 | |
| 26 | #define MAX_PORTS 8 |
| 27 | static unsigned short ports[MAX_PORTS]; |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 28 | static unsigned int ports_c; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 29 | module_param_array(ports, ushort, &ports_c, 0400); |
| 30 | MODULE_PARM_DESC(ports, "Port numbers of TFTP servers"); |
| 31 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 32 | unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb, |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 33 | enum ip_conntrack_info ctinfo, |
| 34 | struct nf_conntrack_expect *exp) __read_mostly; |
| 35 | EXPORT_SYMBOL_GPL(nf_nat_tftp_hook); |
| 36 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 37 | static int tftp_help(struct sk_buff *skb, |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 38 | unsigned int protoff, |
| 39 | struct nf_conn *ct, |
| 40 | enum ip_conntrack_info ctinfo) |
| 41 | { |
Jan Engelhardt | de24b4e | 2008-01-31 04:50:51 -0800 | [diff] [blame] | 42 | const struct tftphdr *tfh; |
| 43 | struct tftphdr _tftph; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 44 | struct nf_conntrack_expect *exp; |
| 45 | struct nf_conntrack_tuple *tuple; |
| 46 | unsigned int ret = NF_ACCEPT; |
| 47 | int family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num; |
| 48 | typeof(nf_nat_tftp_hook) nf_nat_tftp; |
| 49 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 50 | tfh = skb_header_pointer(skb, protoff + sizeof(struct udphdr), |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 51 | sizeof(_tftph), &_tftph); |
| 52 | if (tfh == NULL) |
| 53 | return NF_ACCEPT; |
| 54 | |
| 55 | switch (ntohs(tfh->opcode)) { |
| 56 | case TFTP_OPCODE_READ: |
| 57 | case TFTP_OPCODE_WRITE: |
| 58 | /* RRQ and WRQ works the same way */ |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 59 | NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple); |
| 60 | NF_CT_DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_REPLY].tuple); |
| 61 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 62 | exp = nf_ct_expect_alloc(ct); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 63 | if (exp == NULL) |
| 64 | return NF_DROP; |
| 65 | tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 66 | nf_ct_expect_init(exp, family, &tuple->src.u3, &tuple->dst.u3, |
| 67 | IPPROTO_UDP, NULL, &tuple->dst.u.udp.port); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 68 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 69 | pr_debug("expect: "); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 70 | NF_CT_DUMP_TUPLE(&exp->tuple); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 71 | |
| 72 | nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook); |
| 73 | if (nf_nat_tftp && ct->status & IPS_NAT_MASK) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 74 | ret = nf_nat_tftp(skb, ctinfo, exp); |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 75 | else if (nf_ct_expect_related(exp) != 0) |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 76 | ret = NF_DROP; |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 77 | nf_ct_expect_put(exp); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 78 | break; |
| 79 | case TFTP_OPCODE_DATA: |
| 80 | case TFTP_OPCODE_ACK: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 81 | pr_debug("Data/ACK opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 82 | break; |
| 83 | case TFTP_OPCODE_ERROR: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 84 | pr_debug("Error opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 85 | break; |
| 86 | default: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 87 | pr_debug("Unknown opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly; |
| 93 | static char tftp_names[MAX_PORTS][2][sizeof("tftp-65535")] __read_mostly; |
| 94 | |
| 95 | static void nf_conntrack_tftp_fini(void) |
| 96 | { |
| 97 | int i, j; |
| 98 | |
| 99 | for (i = 0; i < ports_c; i++) { |
| 100 | for (j = 0; j < 2; j++) |
| 101 | nf_conntrack_helper_unregister(&tftp[i][j]); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static int __init nf_conntrack_tftp_init(void) |
| 106 | { |
| 107 | int i, j, ret; |
| 108 | char *tmpname; |
| 109 | |
| 110 | if (ports_c == 0) |
| 111 | ports[ports_c++] = TFTP_PORT; |
| 112 | |
| 113 | for (i = 0; i < ports_c; i++) { |
| 114 | memset(&tftp[i], 0, sizeof(tftp[i])); |
| 115 | |
| 116 | tftp[i][0].tuple.src.l3num = AF_INET; |
| 117 | tftp[i][1].tuple.src.l3num = AF_INET6; |
| 118 | for (j = 0; j < 2; j++) { |
| 119 | tftp[i][j].tuple.dst.protonum = IPPROTO_UDP; |
| 120 | tftp[i][j].tuple.src.u.udp.port = htons(ports[i]); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 121 | tftp[i][j].max_expected = 1; |
| 122 | tftp[i][j].timeout = 5 * 60; /* 5 minutes */ |
| 123 | tftp[i][j].me = THIS_MODULE; |
| 124 | tftp[i][j].help = tftp_help; |
| 125 | |
| 126 | tmpname = &tftp_names[i][j][0]; |
| 127 | if (ports[i] == TFTP_PORT) |
| 128 | sprintf(tmpname, "tftp"); |
| 129 | else |
| 130 | sprintf(tmpname, "tftp-%u", i); |
| 131 | tftp[i][j].name = tmpname; |
| 132 | |
| 133 | ret = nf_conntrack_helper_register(&tftp[i][j]); |
| 134 | if (ret) { |
| 135 | printk("nf_ct_tftp: failed to register helper " |
| 136 | "for pf: %u port: %u\n", |
| 137 | tftp[i][j].tuple.src.l3num, ports[i]); |
| 138 | nf_conntrack_tftp_fini(); |
| 139 | return ret; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | module_init(nf_conntrack_tftp_init); |
| 147 | module_exit(nf_conntrack_tftp_fini); |