Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* iptables module to match on related connections */ |
2 | /* | |
3 | * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * 19 Mar 2002 Harald Welte <laforge@gnumonks.org>: | |
10 | * - Port to newnat infrastructure | |
11 | */ | |
12 | ||
13 | #include <linux/module.h> | |
14 | #include <linux/skbuff.h> | |
15 | #include <linux/netfilter.h> | |
16 | #include <linux/netfilter_ipv4/ip_conntrack.h> | |
17 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> | |
18 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> | |
19 | #include <linux/netfilter_ipv4/ip_tables.h> | |
20 | #include <linux/netfilter_ipv4/ipt_helper.h> | |
21 | ||
22 | MODULE_LICENSE("GPL"); | |
23 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); | |
24 | MODULE_DESCRIPTION("iptables helper match module"); | |
25 | ||
26 | #if 0 | |
27 | #define DEBUGP printk | |
28 | #else | |
29 | #define DEBUGP(format, args...) | |
30 | #endif | |
31 | ||
32 | static int | |
33 | match(const struct sk_buff *skb, | |
34 | const struct net_device *in, | |
35 | const struct net_device *out, | |
36 | const void *matchinfo, | |
37 | int offset, | |
38 | int *hotdrop) | |
39 | { | |
40 | const struct ipt_helper_info *info = matchinfo; | |
41 | struct ip_conntrack *ct; | |
42 | enum ip_conntrack_info ctinfo; | |
43 | int ret = info->invert; | |
44 | ||
45 | ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo); | |
46 | if (!ct) { | |
47 | DEBUGP("ipt_helper: Eek! invalid conntrack?\n"); | |
48 | return ret; | |
49 | } | |
50 | ||
51 | if (!ct->master) { | |
52 | DEBUGP("ipt_helper: conntrack %p has no master\n", ct); | |
53 | return ret; | |
54 | } | |
55 | ||
56 | READ_LOCK(&ip_conntrack_lock); | |
57 | if (!ct->master->helper) { | |
58 | DEBUGP("ipt_helper: master ct %p has no helper\n", | |
59 | exp->expectant); | |
60 | goto out_unlock; | |
61 | } | |
62 | ||
63 | DEBUGP("master's name = %s , info->name = %s\n", | |
64 | ct->master->helper->name, info->name); | |
65 | ||
66 | if (info->name[0] == '\0') | |
67 | ret ^= 1; | |
68 | else | |
69 | ret ^= !strncmp(ct->master->helper->name, info->name, | |
70 | strlen(ct->master->helper->name)); | |
71 | out_unlock: | |
72 | READ_UNLOCK(&ip_conntrack_lock); | |
73 | return ret; | |
74 | } | |
75 | ||
76 | static int check(const char *tablename, | |
77 | const struct ipt_ip *ip, | |
78 | void *matchinfo, | |
79 | unsigned int matchsize, | |
80 | unsigned int hook_mask) | |
81 | { | |
82 | struct ipt_helper_info *info = matchinfo; | |
83 | ||
84 | info->name[29] = '\0'; | |
85 | ||
86 | /* verify size */ | |
87 | if (matchsize != IPT_ALIGN(sizeof(struct ipt_helper_info))) | |
88 | return 0; | |
89 | ||
90 | return 1; | |
91 | } | |
92 | ||
93 | static struct ipt_match helper_match = { | |
94 | .name = "helper", | |
95 | .match = &match, | |
96 | .checkentry = &check, | |
97 | .me = THIS_MODULE, | |
98 | }; | |
99 | ||
100 | static int __init init(void) | |
101 | { | |
102 | need_ip_conntrack(); | |
103 | return ipt_register_match(&helper_match); | |
104 | } | |
105 | ||
106 | static void __exit fini(void) | |
107 | { | |
108 | ipt_unregister_match(&helper_match); | |
109 | } | |
110 | ||
111 | module_init(init); | |
112 | module_exit(fini); | |
113 |