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"); |
Pablo Neira Ayuso | 4dc06f9 | 2008-11-17 16:01:42 +0100 | [diff] [blame] | 25 | MODULE_ALIAS_NFCT_HELPER("tftp"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 26 | |
| 27 | #define MAX_PORTS 8 |
| 28 | static unsigned short ports[MAX_PORTS]; |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 29 | static unsigned int ports_c; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 30 | module_param_array(ports, ushort, &ports_c, 0400); |
| 31 | MODULE_PARM_DESC(ports, "Port numbers of TFTP servers"); |
| 32 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 33 | unsigned int (*nf_nat_tftp_hook)(struct sk_buff *skb, |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 34 | enum ip_conntrack_info ctinfo, |
| 35 | struct nf_conntrack_expect *exp) __read_mostly; |
| 36 | EXPORT_SYMBOL_GPL(nf_nat_tftp_hook); |
| 37 | |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 38 | static int tftp_help(struct sk_buff *skb, |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 39 | unsigned int protoff, |
| 40 | struct nf_conn *ct, |
| 41 | enum ip_conntrack_info ctinfo) |
| 42 | { |
Jan Engelhardt | de24b4e | 2008-01-31 04:50:51 -0800 | [diff] [blame] | 43 | const struct tftphdr *tfh; |
| 44 | struct tftphdr _tftph; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 45 | struct nf_conntrack_expect *exp; |
| 46 | struct nf_conntrack_tuple *tuple; |
| 47 | unsigned int ret = NF_ACCEPT; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 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 */ |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [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); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 61 | |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 62 | exp = nf_ct_expect_alloc(ct); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 63 | if (exp == NULL) { |
| 64 | nf_ct_helper_log(skb, ct, "cannot alloc expectation"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 65 | return NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 66 | } |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 67 | tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple; |
Patrick McHardy | 5e8fbe2 | 2008-04-14 11:15:52 +0200 | [diff] [blame] | 68 | nf_ct_expect_init(exp, NF_CT_EXPECT_CLASS_DEFAULT, |
| 69 | nf_ct_l3num(ct), |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 70 | &tuple->src.u3, &tuple->dst.u3, |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 71 | IPPROTO_UDP, NULL, &tuple->dst.u.udp.port); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 72 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 73 | pr_debug("expect: "); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 74 | nf_ct_dump_tuple(&exp->tuple); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 75 | |
| 76 | nf_nat_tftp = rcu_dereference(nf_nat_tftp_hook); |
| 77 | if (nf_nat_tftp && ct->status & IPS_NAT_MASK) |
Herbert Xu | 3db05fe | 2007-10-15 00:53:15 -0700 | [diff] [blame] | 78 | ret = nf_nat_tftp(skb, ctinfo, exp); |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 79 | else if (nf_ct_expect_related(exp) != 0) { |
| 80 | nf_ct_helper_log(skb, ct, "cannot add expectation"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 81 | ret = NF_DROP; |
Pablo Neira Ayuso | b20ab9cc | 2013-02-10 18:56:56 +0100 | [diff] [blame] | 82 | } |
Patrick McHardy | 6823645 | 2007-07-07 22:30:49 -0700 | [diff] [blame] | 83 | nf_ct_expect_put(exp); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 84 | break; |
| 85 | case TFTP_OPCODE_DATA: |
| 86 | case TFTP_OPCODE_ACK: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 87 | pr_debug("Data/ACK opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 88 | break; |
| 89 | case TFTP_OPCODE_ERROR: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 90 | pr_debug("Error opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 91 | break; |
| 92 | default: |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 93 | pr_debug("Unknown opcode\n"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 94 | } |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | static struct nf_conntrack_helper tftp[MAX_PORTS][2] __read_mostly; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 99 | |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 100 | static const struct nf_conntrack_expect_policy tftp_exp_policy = { |
| 101 | .max_expected = 1, |
| 102 | .timeout = 5 * 60, |
| 103 | }; |
| 104 | |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 105 | static void nf_conntrack_tftp_fini(void) |
| 106 | { |
| 107 | int i, j; |
| 108 | |
| 109 | for (i = 0; i < ports_c; i++) { |
| 110 | for (j = 0; j < 2; j++) |
| 111 | nf_conntrack_helper_unregister(&tftp[i][j]); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | static int __init nf_conntrack_tftp_init(void) |
| 116 | { |
| 117 | int i, j, ret; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 118 | |
| 119 | if (ports_c == 0) |
| 120 | ports[ports_c++] = TFTP_PORT; |
| 121 | |
| 122 | for (i = 0; i < ports_c; i++) { |
| 123 | memset(&tftp[i], 0, sizeof(tftp[i])); |
| 124 | |
| 125 | tftp[i][0].tuple.src.l3num = AF_INET; |
| 126 | tftp[i][1].tuple.src.l3num = AF_INET6; |
| 127 | for (j = 0; j < 2; j++) { |
| 128 | tftp[i][j].tuple.dst.protonum = IPPROTO_UDP; |
| 129 | tftp[i][j].tuple.src.u.udp.port = htons(ports[i]); |
Patrick McHardy | 6002f266 | 2008-03-25 20:09:15 -0700 | [diff] [blame] | 130 | tftp[i][j].expect_policy = &tftp_exp_policy; |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 131 | tftp[i][j].me = THIS_MODULE; |
| 132 | tftp[i][j].help = tftp_help; |
| 133 | |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 134 | if (ports[i] == TFTP_PORT) |
Pablo Neira Ayuso | 3a8fc53 | 2012-01-15 16:34:08 +0100 | [diff] [blame] | 135 | sprintf(tftp[i][j].name, "tftp"); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 136 | else |
Pablo Neira Ayuso | 3a8fc53 | 2012-01-15 16:34:08 +0100 | [diff] [blame] | 137 | sprintf(tftp[i][j].name, "tftp-%u", i); |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 138 | |
| 139 | ret = nf_conntrack_helper_register(&tftp[i][j]); |
| 140 | if (ret) { |
Stephen Hemminger | 654d0fb | 2010-05-13 15:02:08 +0200 | [diff] [blame] | 141 | printk(KERN_ERR "nf_ct_tftp: failed to register" |
| 142 | " helper for pf: %u port: %u\n", |
Patrick McHardy | a536df3 | 2006-12-02 22:10:18 -0800 | [diff] [blame] | 143 | tftp[i][j].tuple.src.l3num, ports[i]); |
| 144 | nf_conntrack_tftp_fini(); |
| 145 | return ret; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | module_init(nf_conntrack_tftp_init); |
| 153 | module_exit(nf_conntrack_tftp_fini); |