blob: e223cb43ae8e09e5374051146552e6560be3f75a [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");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080014MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080015MODULE_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)];
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020049 const sctp_chunkhdr_t *sch;
50 sctp_chunkhdr_t _sch;
Li Zefan009e8c92007-10-18 05:12:21 -070051 int chunk_match_type = info->chunk_match_type;
52 const struct xt_sctp_flag_info *flag_info = info->flag_info;
53 int flag_count = info->flag_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#ifdef DEBUG_SCTP
56 int i = 0;
57#endif
58
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070059 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
Li Zefan009e8c92007-10-18 05:12:21 -070060 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 do {
63 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
Patrick McHardyd3dcd4e2006-06-19 23:39:45 -070064 if (sch == NULL || sch->length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 duprintf("Dropping invalid SCTP packet.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070066 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070067 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080070 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 ++i, offset, sch->type, htons(sch->length), sch->flags);
72
Al Viro962c8372006-11-20 17:26:08 -080073 offset += (ntohs(sch->length) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
76
Li Zefan009e8c92007-10-18 05:12:21 -070077 if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 switch (chunk_match_type) {
79 case SCTP_CHUNK_MATCH_ANY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080080 if (match_flags(flag_info, flag_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 sch->type, sch->flags)) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070082 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 }
84 break;
85
86 case SCTP_CHUNK_MATCH_ALL:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080087 if (match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070088 sch->type, sch->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break;
91
92 case SCTP_CHUNK_MATCH_ONLY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080093 if (!match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070094 sch->type, sch->flags))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070095 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 }
98 } else {
99 switch (chunk_match_type) {
100 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700101 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 }
104 } while (offset < skb->len);
105
106 switch (chunk_match_type) {
107 case SCTP_CHUNK_MATCH_ALL:
Li Zefan009e8c92007-10-18 05:12:21 -0700108 return SCTP_CHUNKMAP_IS_CLEAR(info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 case SCTP_CHUNK_MATCH_ANY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700110 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700112 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 }
114
115 /* This will never be reached, but required to stop compiler whine */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700116 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700119static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200120sctp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200122 const struct xt_sctp_info *info = par->matchinfo;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200123 const sctp_sctphdr_t *sh;
124 sctp_sctphdr_t _sh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200126 if (par->fragoff != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 duprintf("Dropping non-first fragment.. FIXME\n");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700128 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800130
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200131 sh = skb_header_pointer(skb, par->thoff, sizeof(_sh), &_sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (sh == NULL) {
133 duprintf("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200134 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700135 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
138
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700139 return SCCHECK(ntohs(sh->source) >= info->spts[0]
140 && ntohs(sh->source) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800141 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700142 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
143 && ntohs(sh->dest) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200145 && SCCHECK(match_packet(skb, par->thoff + sizeof(sctp_sctphdr_t),
146 info, par->hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800147 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200150static bool sctp_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200152 const struct xt_sctp_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800154 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800155 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 && !(info->invflags & ~info->flags)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800157 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 (info->chunk_match_type &
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800159 (SCTP_CHUNK_MATCH_ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 | SCTP_CHUNK_MATCH_ANY
161 | SCTP_CHUNK_MATCH_ONLY)));
162}
163
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800164static struct xt_match sctp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700165 {
166 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200167 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800168 .checkentry = sctp_mt_check,
169 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700170 .matchsize = sizeof(struct xt_sctp_info),
171 .proto = IPPROTO_SCTP,
172 .me = THIS_MODULE
173 },
174 {
175 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200176 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800177 .checkentry = sctp_mt_check,
178 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700179 .matchsize = sizeof(struct xt_sctp_info),
180 .proto = IPPROTO_SCTP,
181 .me = THIS_MODULE
182 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800183};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800184
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800185static int __init sctp_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800187 return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800190static void __exit sctp_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800192 xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800195module_init(sctp_mt_init);
196module_exit(sctp_mt_exit);