blob: ef36a56a02c6881c58296b2bf45c4b99d3836456 [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>
Shan Wei2bf07482010-06-09 14:47:40 +02006#include <net/sctp/sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/sctp.h>
8
Harald Welte2e4e6a12006-01-12 13:30:04 -08009#include <linux/netfilter/x_tables.h>
10#include <linux/netfilter/xt_sctp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/netfilter_ipv4/ip_tables.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080012#include <linux/netfilter_ipv6/ip6_tables.h>
13
14MODULE_LICENSE("GPL");
15MODULE_AUTHOR("Kiran Kumar Immidi");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080016MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
Harald Welte2e4e6a12006-01-12 13:30:04 -080017MODULE_ALIAS("ipt_sctp");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070018MODULE_ALIAS("ip6t_sctp");
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
21 || (!!((invflag) & (option)) ^ (cond)))
22
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070023static bool
Harald Welte2e4e6a12006-01-12 13:30:04 -080024match_flags(const struct xt_sctp_flag_info *flag_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 const int flag_count,
26 u_int8_t chunktype,
27 u_int8_t chunkflags)
28{
29 int i;
30
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070031 for (i = 0; i < flag_count; i++)
32 if (flag_info[i].chunktype == chunktype)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070035 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070038static inline bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070039match_packet(const struct sk_buff *skb,
Harald Welte2e4e6a12006-01-12 13:30:04 -080040 unsigned int offset,
Li Zefan009e8c92007-10-18 05:12:21 -070041 const struct xt_sctp_info *info,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070042 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020045 const sctp_chunkhdr_t *sch;
46 sctp_chunkhdr_t _sch;
Li Zefan009e8c92007-10-18 05:12:21 -070047 int chunk_match_type = info->chunk_match_type;
48 const struct xt_sctp_flag_info *flag_info = info->flag_info;
49 int flag_count = info->flag_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010051#ifdef DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int i = 0;
53#endif
54
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070055 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
Li Zefan009e8c92007-10-18 05:12:21 -070056 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 do {
59 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
Patrick McHardyd3dcd4e2006-06-19 23:39:45 -070060 if (sch == NULL || sch->length == 0) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010061 pr_debug("Dropping invalid SCTP packet.\n");
Jan Engelhardtcff533a2007-07-07 22:15:12 -070062 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070063 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080064 }
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010065#ifdef DEBUG
66 pr_debug("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d"
67 "\tflags: %x\n",
68 ++i, offset, sch->type, htons(sch->length),
69 sch->flags);
70#endif
Shan Wei2bf07482010-06-09 14:47:40 +020071 offset += WORD_ROUND(ntohs(sch->length));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010073 pr_debug("skb->len: %d\toffset: %d\n", skb->len, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Li Zefan009e8c92007-10-18 05:12:21 -070075 if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 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:
Qu Haorand4e26752009-02-09 14:34:56 -0800106 return SCTP_CHUNKMAP_IS_CLEAR(chunkmapcopy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 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
Jan Engelhardt62fc8052009-07-07 20:42:08 +0200118sctp_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200120 const struct xt_sctp_info *info = par->matchinfo;
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200121 const sctp_sctphdr_t *sh;
122 sctp_sctphdr_t _sh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200124 if (par->fragoff != 0) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100125 pr_debug("Dropping non-first fragment.. FIXME\n");
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700126 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800128
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200129 sh = skb_header_pointer(skb, par->thoff, sizeof(_sh), &_sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (sh == NULL) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100131 pr_debug("Dropping evil TCP offset=0 tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200132 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -0700133 return false;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800134 }
Jan Engelhardtbe91fd52010-03-18 02:22:32 +0100135 pr_debug("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700137 return SCCHECK(ntohs(sh->source) >= info->spts[0]
138 && ntohs(sh->source) <= info->spts[1],
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800139 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700140 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
141 && ntohs(sh->dest) <= info->dpts[1],
Harald Welte2e4e6a12006-01-12 13:30:04 -0800142 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
Jan Engelhardtf7108a22008-10-08 11:35:18 +0200143 && SCCHECK(match_packet(skb, par->thoff + sizeof(sctp_sctphdr_t),
Jan Engelhardtb4ba2612009-07-07 20:54:30 +0200144 info, &par->hotdrop),
Harald Welte2e4e6a12006-01-12 13:30:04 -0800145 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100148static int sctp_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200150 const struct xt_sctp_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Jan Engelhardt9f567312010-03-23 17:40:13 +0100152 if (info->flags & ~XT_SCTP_VALID_FLAGS)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100153 return -EINVAL;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100154 if (info->invflags & ~XT_SCTP_VALID_FLAGS)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100155 return -EINVAL;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100156 if (info->invflags & ~info->flags)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100157 return -EINVAL;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100158 if (!(info->flags & XT_SCTP_CHUNK_TYPES))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100159 return 0;
Jan Engelhardt9f567312010-03-23 17:40:13 +0100160 if (info->chunk_match_type & (SCTP_CHUNK_MATCH_ALL |
161 SCTP_CHUNK_MATCH_ANY | SCTP_CHUNK_MATCH_ONLY))
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100162 return 0;
163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800166static struct xt_match sctp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700167 {
168 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200169 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800170 .checkentry = sctp_mt_check,
171 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700172 .matchsize = sizeof(struct xt_sctp_info),
173 .proto = IPPROTO_SCTP,
174 .me = THIS_MODULE
175 },
176 {
177 .name = "sctp",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200178 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800179 .checkentry = sctp_mt_check,
180 .match = sctp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -0700181 .matchsize = sizeof(struct xt_sctp_info),
182 .proto = IPPROTO_SCTP,
183 .me = THIS_MODULE
184 },
Patrick McHardy5d04bff2006-03-20 18:01:58 -0800185};
Harald Welte2e4e6a12006-01-12 13:30:04 -0800186
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800187static int __init sctp_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800189 return xt_register_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 -0800192static void __exit sctp_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800194 xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800197module_init(sctp_mt_init);
198module_exit(sctp_mt_exit);