2 * Licensed under GNU GPL version 2 Copyright Magnus Boden <mb@ozaba.mine.nu>
5 * Thu 21 Mar 2002 Harald Welte <laforge@gnumonks.org>
10 #include <linux/module.h>
12 #include <linux/udp.h>
14 #include <linux/netfilter.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
17 #include <linux/netfilter_ipv4/ip_conntrack_tftp.h>
19 MODULE_AUTHOR("Magnus Boden <mb@ozaba.mine.nu>");
20 MODULE_DESCRIPTION("Netfilter connection tracking module for tftp");
21 MODULE_LICENSE("GPL");
24 static int ports
[MAX_PORTS
];
25 static int ports_c
= 0;
27 MODULE_PARM(ports
, "1-" __MODULE_STRING(MAX_PORTS
) "i");
28 MODULE_PARM_DESC(ports
, "port numbers of tftp servers");
31 #define DEBUGP(format, args...)
33 static int tftp_help(const struct iphdr
*iph
, size_t len
,
34 struct ip_conntrack
*ct
,
35 enum ip_conntrack_info ctinfo
)
37 struct udphdr
*udph
= (void *)iph
+ iph
->ihl
* 4;
38 struct tftphdr
*tftph
= (void *)udph
+ 8;
39 struct ip_conntrack_expect exp
;
41 switch (ntohs(tftph
->opcode
)) {
42 /* RRQ and WRQ works the same way */
43 case TFTP_OPCODE_READ
:
44 case TFTP_OPCODE_WRITE
:
46 DUMP_TUPLE(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
47 DUMP_TUPLE(&ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
);
48 memset(&exp
, 0, sizeof(exp
));
50 exp
.tuple
= ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
;
51 exp
.mask
.src
.ip
= 0xffffffff;
52 exp
.mask
.dst
.ip
= 0xffffffff;
53 exp
.mask
.dst
.u
.udp
.port
= 0xffff;
54 exp
.mask
.dst
.protonum
= 0xffff;
58 DUMP_TUPLE(&exp
.tuple
);
59 DUMP_TUPLE(&exp
.mask
);
60 ip_conntrack_expect_related(ct
, &exp
);
63 DEBUGP("Unknown opcode\n");
68 static struct ip_conntrack_helper tftp
[MAX_PORTS
];
69 static char tftp_names
[MAX_PORTS
][10];
71 static void fini(void)
75 for (i
= 0 ; i
< ports_c
; i
++) {
76 DEBUGP("unregistering helper for port %d\n",
78 ip_conntrack_helper_unregister(&tftp
[i
]);
82 static int __init
init(void)
90 for (i
= 0 ; (i
< MAX_PORTS
) && ports
[i
] ; i
++) {
91 /* Create helper structure */
92 memset(&tftp
[i
], 0, sizeof(struct ip_conntrack_helper
));
94 tftp
[i
].tuple
.dst
.protonum
= IPPROTO_UDP
;
95 tftp
[i
].tuple
.src
.u
.udp
.port
= htons(ports
[i
]);
96 tftp
[i
].mask
.dst
.protonum
= 0xFFFF;
97 tftp
[i
].mask
.src
.u
.udp
.port
= 0xFFFF;
98 tftp
[i
].max_expected
= 1;
100 tftp
[i
].flags
= IP_CT_HELPER_F_REUSE_EXPECT
;
101 tftp
[i
].me
= THIS_MODULE
;
102 tftp
[i
].help
= tftp_help
;
104 tmpname
= &tftp_names
[i
][0];
105 if (ports
[i
] == TFTP_PORT
)
106 sprintf(tmpname
, "tftp");
108 sprintf(tmpname
, "tftp-%d", i
);
109 tftp
[i
].name
= tmpname
;
111 DEBUGP("port #%d: %d\n", i
, ports
[i
]);
113 ret
=ip_conntrack_helper_register(&tftp
[i
]);
115 printk("ERROR registering helper for port %d\n",