Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 16 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 18 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> |
| 19 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 20 | #else |
| 21 | #include <net/netfilter/nf_conntrack.h> |
| 22 | #include <net/netfilter/nf_conntrack_core.h> |
| 23 | #include <net/netfilter/nf_conntrack_helper.h> |
| 24 | #endif |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 25 | #include <linux/netfilter/x_tables.h> |
| 26 | #include <linux/netfilter/xt_helper.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>"); |
| 30 | MODULE_DESCRIPTION("iptables helper match module"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 31 | MODULE_ALIAS("ipt_helper"); |
| 32 | MODULE_ALIAS("ip6t_helper"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #if 0 |
| 35 | #define DEBUGP printk |
| 36 | #else |
| 37 | #define DEBUGP(format, args...) |
| 38 | #endif |
| 39 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 40 | #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | static int |
| 42 | match(const struct sk_buff *skb, |
| 43 | const struct net_device *in, |
| 44 | const struct net_device *out, |
| 45 | const void *matchinfo, |
| 46 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 47 | unsigned int protoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | int *hotdrop) |
| 49 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 50 | const struct xt_helper_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | struct ip_conntrack *ct; |
| 52 | enum ip_conntrack_info ctinfo; |
| 53 | int ret = info->invert; |
| 54 | |
| 55 | ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo); |
| 56 | if (!ct) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 57 | DEBUGP("xt_helper: Eek! invalid conntrack?\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | if (!ct->master) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 62 | DEBUGP("xt_helper: conntrack %p has no master\n", ct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | return ret; |
| 64 | } |
| 65 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 66 | read_lock_bh(&ip_conntrack_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (!ct->master->helper) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 68 | DEBUGP("xt_helper: master ct %p has no helper\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | exp->expectant); |
| 70 | goto out_unlock; |
| 71 | } |
| 72 | |
| 73 | DEBUGP("master's name = %s , info->name = %s\n", |
| 74 | ct->master->helper->name, info->name); |
| 75 | |
| 76 | if (info->name[0] == '\0') |
| 77 | ret ^= 1; |
| 78 | else |
| 79 | ret ^= !strncmp(ct->master->helper->name, info->name, |
| 80 | strlen(ct->master->helper->name)); |
| 81 | out_unlock: |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 82 | read_unlock_bh(&ip_conntrack_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return ret; |
| 84 | } |
| 85 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 86 | #else /* CONFIG_IP_NF_CONNTRACK */ |
| 87 | |
| 88 | static int |
| 89 | match(const struct sk_buff *skb, |
| 90 | const struct net_device *in, |
| 91 | const struct net_device *out, |
| 92 | const void *matchinfo, |
| 93 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 94 | unsigned int protoff, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 95 | int *hotdrop) |
| 96 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 97 | const struct xt_helper_info *info = matchinfo; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 98 | struct nf_conn *ct; |
| 99 | enum ip_conntrack_info ctinfo; |
| 100 | int ret = info->invert; |
| 101 | |
| 102 | ct = nf_ct_get((struct sk_buff *)skb, &ctinfo); |
| 103 | if (!ct) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 104 | DEBUGP("xt_helper: Eek! invalid conntrack?\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | if (!ct->master) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 109 | DEBUGP("xt_helper: conntrack %p has no master\n", ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | read_lock_bh(&nf_conntrack_lock); |
| 114 | if (!ct->master->helper) { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 115 | DEBUGP("xt_helper: master ct %p has no helper\n", |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 116 | exp->expectant); |
| 117 | goto out_unlock; |
| 118 | } |
| 119 | |
| 120 | DEBUGP("master's name = %s , info->name = %s\n", |
| 121 | ct->master->helper->name, info->name); |
| 122 | |
| 123 | if (info->name[0] == '\0') |
| 124 | ret ^= 1; |
| 125 | else |
| 126 | ret ^= !strncmp(ct->master->helper->name, info->name, |
| 127 | strlen(ct->master->helper->name)); |
| 128 | out_unlock: |
| 129 | read_unlock_bh(&nf_conntrack_lock); |
| 130 | return ret; |
| 131 | } |
| 132 | #endif |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | static int check(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | const void *inf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | void *matchinfo, |
| 137 | unsigned int matchsize, |
| 138 | unsigned int hook_mask) |
| 139 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 140 | struct xt_helper_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | info->name[29] = '\0'; |
| 143 | |
| 144 | /* verify size */ |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 145 | if (matchsize != XT_ALIGN(sizeof(struct xt_helper_info))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | return 0; |
| 147 | |
| 148 | return 1; |
| 149 | } |
| 150 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 151 | static struct xt_match helper_match = { |
| 152 | .name = "helper", |
| 153 | .match = &match, |
| 154 | .checkentry = &check, |
| 155 | .me = THIS_MODULE, |
| 156 | }; |
| 157 | static struct xt_match helper6_match = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | .name = "helper", |
| 159 | .match = &match, |
| 160 | .checkentry = &check, |
| 161 | .me = THIS_MODULE, |
| 162 | }; |
| 163 | |
| 164 | static int __init init(void) |
| 165 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 166 | int ret; |
| 167 | need_conntrack(); |
| 168 | |
| 169 | ret = xt_register_match(AF_INET, &helper_match); |
| 170 | if (ret < 0) |
| 171 | return ret; |
| 172 | |
| 173 | ret = xt_register_match(AF_INET6, &helper6_match); |
| 174 | if (ret < 0) |
| 175 | xt_unregister_match(AF_INET, &helper_match); |
| 176 | |
| 177 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void __exit fini(void) |
| 181 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 182 | xt_unregister_match(AF_INET, &helper_match); |
| 183 | xt_unregister_match(AF_INET6, &helper6_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | module_init(init); |
| 187 | module_exit(fini); |
| 188 | |