blob: c002153b80ab76ef6e3725dfc9dc6d4fce242d69 [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");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#ifdef DEBUG_SCTP
18#define duprintf(format, args...) printk(format , ## args)
19#else
20#define duprintf(format, args...)
21#endif
22
23#define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
24 || (!!((invflag) & (option)) ^ (cond)))
25
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070026static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080027match_flags(const struct xt_sctp_flag_info *flag_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 const int flag_count,
29 u_int8_t chunktype,
30 u_int8_t chunkflags)
31{
32 int i;
33
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070034 for (i = 0; i < flag_count; i++)
35 if (flag_info[i].chunktype == chunktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070042match_packet(const struct sk_buff *skb,
Harald Welte2e4e6a12006-01-12 13:30:04 -080043 unsigned int offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 const u_int32_t *chunkmap,
45 int chunk_match_type,
Harald Welte2e4e6a12006-01-12 13:30:04 -080046 const struct xt_sctp_flag_info *flag_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 const int flag_count,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070048 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
51 sctp_chunkhdr_t _sch, *sch;
52
53#ifdef DEBUG_SCTP
54 int i = 0;
55#endif
56
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070057 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 SCTP_CHUNKMAP_COPY(chunkmapcopy, chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 do {
61 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
Patrick McHardyd3dcd4e2006-06-19 23:39:45 -070062 if (sch == NULL || sch->length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 duprintf("Dropping invalid SCTP packet.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070064 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070065 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080068 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 ++i, offset, sch->type, htons(sch->length), sch->flags);
70
Al Viro962c8372006-11-20 17:26:08 -080071 offset += (ntohs(sch->length) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
74
75 if (SCTP_CHUNKMAP_IS_SET(chunkmap, sch->type)) {
76 switch (chunk_match_type) {
77 case SCTP_CHUNK_MATCH_ANY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080078 if (match_flags(flag_info, flag_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 sch->type, sch->flags)) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070080 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 }
82 break;
83
84 case SCTP_CHUNK_MATCH_ALL:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080085 if (match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070086 sch->type, sch->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 break;
89
90 case SCTP_CHUNK_MATCH_ONLY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080091 if (!match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070092 sch->type, sch->flags))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070093 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
95 }
96 } else {
97 switch (chunk_match_type) {
98 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070099 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101 }
102 } while (offset < skb->len);
103
104 switch (chunk_match_type) {
105 case SCTP_CHUNK_MATCH_ALL:
106 return SCTP_CHUNKMAP_IS_CLEAR(chunkmap);
107 case SCTP_CHUNK_MATCH_ANY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700108 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700110 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 /* This will never be reached, but required to stop compiler whine */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700114 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700117static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118match(const struct sk_buff *skb,
119 const struct net_device *in,
120 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -0800121 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 const void *matchinfo,
123 int offset,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800124 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700125 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Patrick McHardy3e72b2f2006-05-29 18:19:19 -0700127 const struct xt_sctp_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 sctp_sctphdr_t _sh, *sh;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (offset) {
131 duprintf("Dropping non-first fragment.. FIXME\n");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700132 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800134
Harald Welte2e4e6a12006-01-12 13:30:04 -0800135 sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (sh == NULL) {
137 duprintf("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -0700138 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700139 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
142
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700143 return SCCHECK(ntohs(sh->source) >= info->spts[0]
144 && ntohs(sh->source) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800145 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700146 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
147 && ntohs(sh->dest) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800148 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
Jorge Matias1c7e4772006-06-27 03:01:25 -0700149 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800150 info->chunkmap, info->chunk_match_type,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800151 info->flag_info, info->flag_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800153 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700156static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800158 const void *inf,
Patrick McHardyc4986732006-03-20 18:02:56 -0800159 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 void *matchinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 unsigned int hook_mask)
162{
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800163 const struct xt_sctp_info *info = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800165 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800166 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 && !(info->invflags & ~info->flags)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800168 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 (info->chunk_match_type &
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800170 (SCTP_CHUNK_MATCH_ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 | SCTP_CHUNK_MATCH_ANY
172 | SCTP_CHUNK_MATCH_ONLY)));
173}
174
Patrick McHardy9f15c532007-07-07 22:22:02 -0700175static struct xt_match xt_sctp_match[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700176 {
177 .name = "sctp",
178 .family = AF_INET,
179 .checkentry = checkentry,
180 .match = match,
181 .matchsize = sizeof(struct xt_sctp_info),
182 .proto = IPPROTO_SCTP,
183 .me = THIS_MODULE
184 },
185 {
186 .name = "sctp",
187 .family = AF_INET6,
188 .checkentry = checkentry,
189 .match = match,
190 .matchsize = sizeof(struct xt_sctp_info),
191 .proto = IPPROTO_SCTP,
192 .me = THIS_MODULE
193 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800194};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800195
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800196static int __init xt_sctp_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700198 return xt_register_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800201static void __exit xt_sctp_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700203 xt_unregister_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800206module_init(xt_sctp_init);
207module_exit(xt_sctp_fini);