blob: 43c7e1de532c4c79521836ff7ce807a63d671bfd [file] [log] [blame]
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/module.h>
3#include <linux/skbuff.h>
4#include <net/ip.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -08005#include <net/ipv6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/sctp.h>
7
Harald Welte2e4e6a12006-01-12 13:30:04 -08008#include <linux/netfilter/x_tables.h>
9#include <linux/netfilter/xt_sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/netfilter_ipv4/ip_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080011#include <linux/netfilter_ipv6/ip6_tables.h>
12
13MODULE_LICENSE("GPL");
14MODULE_AUTHOR("Kiran Kumar Immidi");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080015MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080016MODULE_ALIAS("ipt_sctp");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070017MODULE_ALIAS("ip6t_sctp");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
20 || (!!((invflag) & (option)) ^ (cond)))
21
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070022static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080023match_flags(const struct xt_sctp_flag_info *flag_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 const int flag_count,
25 u_int8_t chunktype,
26 u_int8_t chunkflags)
27{
28 int i;
29
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070030 for (i = 0; i < flag_count; i++)
31 if (flag_info[i].chunktype == chunktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070034 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070038match_packet(const struct sk_buff *skb,
Harald Welte2e4e6a12006-01-12 13:30:04 -080039 unsigned int offset,
Li Zefan009e8c92007-10-18 05:12:21 -070040 const struct xt_sctp_info *info,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070041 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020044 const sctp_chunkhdr_t *sch;
45 sctp_chunkhdr_t _sch;
Li Zefan009e8c92007-10-18 05:12:21 -070046 int chunk_match_type = info->chunk_match_type;
47 const struct xt_sctp_flag_info *flag_info = info->flag_info;
48 int flag_count = info->flag_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010050#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int i = 0;
52#endif
53
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070054 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
Li Zefan009e8c92007-10-18 05:12:21 -070055 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 do {
58 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
Patrick McHardyd3dcd4e2006-06-19 23:39:45 -070059 if (sch == NULL || sch->length == 0) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010060 pr_debug("Dropping invalid SCTP packet.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070061 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070062 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080063 }
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010064#ifdef DEBUG
65 pr_debug("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d"
66 "\tflags: %x\n",
67 ++i, offset, sch->type, htons(sch->length),
68 sch->flags);
69#endif
Al Viro962c8372006-11-20 17:26:08 -080070 offset += (ntohs(sch->length) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010072 pr_debug("skb->len: %d\toffset: %d\n", skb->len, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Li Zefan009e8c92007-10-18 05:12:21 -070074 if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 switch (chunk_match_type) {
76 case SCTP_CHUNK_MATCH_ANY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080077 if (match_flags(flag_info, flag_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 sch->type, sch->flags)) {
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070079 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81 break;
82
83 case SCTP_CHUNK_MATCH_ALL:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080084 if (match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070085 sch->type, sch->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 break;
88
89 case SCTP_CHUNK_MATCH_ONLY:
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080090 if (!match_flags(flag_info, flag_count,
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070091 sch->type, sch->flags))
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070092 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94 }
95 } else {
96 switch (chunk_match_type) {
97 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070098 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100 }
101 } while (offset < skb->len);
102
103 switch (chunk_match_type) {
104 case SCTP_CHUNK_MATCH_ALL:
Qu Haorand4e26752009-02-09 14:34:56 -0800105 return SCTP_CHUNKMAP_IS_CLEAR(chunkmapcopy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 case SCTP_CHUNK_MATCH_ANY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700107 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 case SCTP_CHUNK_MATCH_ONLY:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700109 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 }
111
112 /* This will never be reached, but required to stop compiler whine */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700113 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700116static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200117sctp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200119 const struct xt_sctp_info *info = par->matchinfo;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200120 const sctp_sctphdr_t *sh;
121 sctp_sctphdr_t _sh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200123 if (par->fragoff != 0) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100124 pr_debug("Dropping non-first fragment.. FIXME\n");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700125 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800127
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200128 sh = skb_header_pointer(skb, par->thoff, sizeof(_sh), &_sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (sh == NULL) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100130 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200131 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700132 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800133 }
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100134 pr_debug("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700136 return SCCHECK(ntohs(sh->source) >= info->spts[0]
137 && ntohs(sh->source) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800138 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700139 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
140 && ntohs(sh->dest) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800141 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200142 && SCCHECK(match_packet(skb, par->thoff + sizeof(sctp_sctphdr_t),
143 info, par->hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800144 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200147static bool sctp_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200149 const struct xt_sctp_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800151 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
Harald Welte2e4e6a12006-01-12 13:30:04 -0800152 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 && !(info->invflags & ~info->flags)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800154 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 (info->chunk_match_type &
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800156 (SCTP_CHUNK_MATCH_ALL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 | SCTP_CHUNK_MATCH_ANY
158 | SCTP_CHUNK_MATCH_ONLY)));
159}
160
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800161static struct xt_match sctp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700162 {
163 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200164 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800165 .checkentry = sctp_mt_check,
166 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700167 .matchsize = sizeof(struct xt_sctp_info),
168 .proto = IPPROTO_SCTP,
169 .me = THIS_MODULE
170 },
171 {
172 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200173 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800174 .checkentry = sctp_mt_check,
175 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700176 .matchsize = sizeof(struct xt_sctp_info),
177 .proto = IPPROTO_SCTP,
178 .me = THIS_MODULE
179 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800180};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800181
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800182static int __init sctp_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800184 return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800187static void __exit sctp_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800189 xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800192module_init(sctp_mt_init);
193module_exit(sctp_mt_exit);