blob: 047d0046b28cde8bf19c434e5735f58ff95c45ad [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/netfilter.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080013#include <net/netfilter/nf_conntrack.h>
14#include <net/netfilter/nf_conntrack_core.h>
15#include <net/netfilter/nf_conntrack_helper.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080016#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter/xt_helper.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19MODULE_LICENSE("GPL");
20MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
21MODULE_DESCRIPTION("iptables helper match module");
Harald Welte2e4e6a12006-01-12 13:30:04 -080022MODULE_ALIAS("ipt_helper");
23MODULE_ALIAS("ip6t_helper");
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#if 0
26#define DEBUGP printk
27#else
28#define DEBUGP(format, args...)
29#endif
30
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070031static bool
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080032match(const struct sk_buff *skb,
33 const struct net_device *in,
34 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080035 const struct xt_match *match,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036 const void *matchinfo,
37 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -080038 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070039 bool *hotdrop)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040{
Harald Welte2e4e6a12006-01-12 13:30:04 -080041 const struct xt_helper_info *info = matchinfo;
Jan Engelhardta47362a2007-07-07 22:16:55 -070042 const struct nf_conn *ct;
43 const struct nf_conn_help *master_help;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080044 enum ip_conntrack_info ctinfo;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070045 bool ret = info->invert;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080046
Jan Engelhardta47362a2007-07-07 22:16:55 -070047 ct = nf_ct_get(skb, &ctinfo);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080048 if (!ct) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080049 DEBUGP("xt_helper: Eek! invalid conntrack?\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080050 return ret;
51 }
52
53 if (!ct->master) {
Harald Welte2e4e6a12006-01-12 13:30:04 -080054 DEBUGP("xt_helper: conntrack %p has no master\n", ct);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080055 return ret;
56 }
57
58 read_lock_bh(&nf_conntrack_lock);
Harald Weltedc808fe2006-03-20 17:56:32 -080059 master_help = nfct_help(ct->master);
60 if (!master_help || !master_help->helper) {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080061 DEBUGP("xt_helper: master ct %p has no helper\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080062 exp->expectant);
63 goto out_unlock;
64 }
65
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080066 DEBUGP("master's name = %s , info->name = %s\n",
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067 ct->master->helper->name, info->name);
68
69 if (info->name[0] == '\0')
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070070 ret = !ret;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071 else
Harald Weltedc808fe2006-03-20 17:56:32 -080072 ret ^= !strncmp(master_help->helper->name, info->name,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080073 strlen(master_help->helper->name));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080074out_unlock:
75 read_unlock_bh(&nf_conntrack_lock);
76 return ret;
77}
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080078
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070079static bool check(const char *tablename,
80 const void *inf,
81 const struct xt_match *match,
82 void *matchinfo,
83 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Harald Welte2e4e6a12006-01-12 13:30:04 -080085 struct xt_helper_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080087 if (nf_ct_l3proto_try_module_get(match->family) < 0) {
Yasuyuki Kozakaife0b9292006-12-12 00:28:40 -080088 printk(KERN_WARNING "can't load conntrack support for "
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080089 "proto=%d\n", match->family);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070090 return false;
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 info->name[29] = '\0';
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070093 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080096static void
Patrick McHardyefa74162006-08-22 00:36:37 -070097destroy(const struct xt_match *match, void *matchinfo)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080098{
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080099 nf_ct_l3proto_module_put(match->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -0800100}
101
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700102static struct xt_match xt_helper_match[] = {
103 {
104 .name = "helper",
105 .family = AF_INET,
106 .checkentry = check,
107 .match = match,
108 .destroy = destroy,
109 .matchsize = sizeof(struct xt_helper_info),
110 .me = THIS_MODULE,
111 },
112 {
113 .name = "helper",
114 .family = AF_INET6,
115 .checkentry = check,
116 .match = match,
117 .destroy = destroy,
118 .matchsize = sizeof(struct xt_helper_info),
119 .me = THIS_MODULE,
120 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800123static int __init xt_helper_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700125 return xt_register_matches(xt_helper_match,
126 ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800129static void __exit xt_helper_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700131 xt_unregister_matches(xt_helper_match, ARRAY_SIZE(xt_helper_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800134module_init(xt_helper_init);
135module_exit(xt_helper_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136