blob: 3358273a47b78f6567396de97619dab08a403ec6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/module.h>
2#include <linux/skbuff.h>
3#include <net/ip.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -08004#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/sctp.h>
6
Harald Welte2e4e6a12006-01-12 13:30:04 -08007#include <linux/netfilter/x_tables.h>
8#include <linux/netfilter/xt_sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/netfilter_ipv4/ip_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080010#include <linux/netfilter_ipv6/ip6_tables.h>
11
12MODULE_LICENSE("GPL");
13MODULE_AUTHOR("Kiran Kumar Immidi");
14MODULE_DESCRIPTION("Match for SCTP protocol packets");
15MODULE_ALIAS("ipt_sctp");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070016MODULE_ALIAS("ip6t_sctp");
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#ifdef DEBUG_SCTP
19#define duprintf(format, args...) printk(format , ## args)
20#else
21#define duprintf(format, args...)
22#endif
23
24#define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
25 || (!!((invflag) & (option)) ^ (cond)))
26
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070027static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080028match_flags(const struct xt_sctp_flag_info *flag_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 const int flag_count,
30 u_int8_t chunktype,
31 u_int8_t chunkflags)
32{
33 int i;
34
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070035 for (i = 0; i < flag_count; i++)
36 if (flag_info[i].chunktype == chunktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070039 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070042static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070043match_packet(const struct sk_buff *skb,
Harald Welte2e4e6a12006-01-12 13:30:04 -080044 unsigned int offset,
Li Zefan009e8c92007-10-18 05:12:21 -070045 const struct xt_sctp_info *info,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070046 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
49 sctp_chunkhdr_t _sch, *sch;
Li Zefan009e8c92007-10-18 05:12:21 -070050 int chunk_match_type = info->chunk_match_type;
51 const struct xt_sctp_flag_info *flag_info = info->flag_info;
52 int flag_count = info->flag_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54#ifdef DEBUG_SCTP
55 int i = 0;
56#endif
57
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070058 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
Li Zefan009e8c92007-10-18 05:12:21 -070059 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 do {
62 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
Patrick McHardyd3dcd4e2006-06-19 23:39:45 -070063 if (sch == NULL || sch->length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 duprintf("Dropping invalid SCTP packet.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070065 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070066 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080069 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 ++i, offset, sch->type, htons(sch->length), sch->flags);
71
Al Viro962c8372006-11-20 17:26:08 -080072 offset += (ntohs(sch->length) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
75
Li Zefan009e8c92007-10-18 05:12:21 -070076 if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 switch (chunk_match_type) {
78 case SCTP_CHUNK_MATCH_ANY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080079 if (match_flags(flag_info, flag_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 sch->type, sch->flags)) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070081 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 }
83 break;
84
85 case SCTP_CHUNK_MATCH_ALL:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080086 if (match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070087 sch->type, sch->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 break;
90
91 case SCTP_CHUNK_MATCH_ONLY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080092 if (!match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070093 sch->type, sch->flags))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070094 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 break;
96 }
97 } else {
98 switch (chunk_match_type) {
99 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700100 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
102 }
103 } while (offset < skb->len);
104
105 switch (chunk_match_type) {
106 case SCTP_CHUNK_MATCH_ALL:
Li Zefan009e8c92007-10-18 05:12:21 -0700107 return SCTP_CHUNKMAP_IS_CLEAR(info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 case SCTP_CHUNK_MATCH_ANY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700109 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700111 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 /* This will never be reached, but required to stop compiler whine */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700115 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700118static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119match(const struct sk_buff *skb,
120 const struct net_device *in,
121 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -0800122 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 const void *matchinfo,
124 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800125 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700126 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Patrick McHardy3e72b2f2006-05-29 18:19:19 -0700128 const struct xt_sctp_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 sctp_sctphdr_t _sh, *sh;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (offset) {
132 duprintf("Dropping non-first fragment.. FIXME\n");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700133 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800135
Harald Welte2e4e6a12006-01-12 13:30:04 -0800136 sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (sh == NULL) {
138 duprintf("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700139 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700140 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
143
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700144 return SCCHECK(ntohs(sh->source) >= info->spts[0]
145 && ntohs(sh->source) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800146 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700147 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
148 && ntohs(sh->dest) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800149 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
Jorge Matias1c7e4772006-06-27 03:01:25 -0700150 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
Li Zefan009e8c92007-10-18 05:12:21 -0700151 info, hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700155static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800157 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800158 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 unsigned int hook_mask)
161{
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800162 const struct xt_sctp_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800164 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800165 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 && !(info->invflags & ~info->flags)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800167 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (info->chunk_match_type &
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800169 (SCTP_CHUNK_MATCH_ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 | SCTP_CHUNK_MATCH_ANY
171 | SCTP_CHUNK_MATCH_ONLY)));
172}
173
Patrick McHardy9f15c532007-07-07 22:22:02 -0700174static struct xt_match xt_sctp_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700175 {
176 .name = "sctp",
177 .family = AF_INET,
178 .checkentry = checkentry,
179 .match = match,
180 .matchsize = sizeof(struct xt_sctp_info),
181 .proto = IPPROTO_SCTP,
182 .me = THIS_MODULE
183 },
184 {
185 .name = "sctp",
186 .family = AF_INET6,
187 .checkentry = checkentry,
188 .match = match,
189 .matchsize = sizeof(struct xt_sctp_info),
190 .proto = IPPROTO_SCTP,
191 .me = THIS_MODULE
192 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800193};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800194
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800195static int __init xt_sctp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700197 return xt_register_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800200static void __exit xt_sctp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700202 xt_unregister_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800205module_init(xt_sctp_init);
206module_exit(xt_sctp_fini);