]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_sctp.c
[NET]: Fix OOPS due to missing check in dev_parse_header().
[net-next-2.6.git] / net / netfilter / xt_sctp.c
CommitLineData
1da177e4
LT
1#include <linux/module.h>
2#include <linux/skbuff.h>
3#include <net/ip.h>
2e4e6a17 4#include <net/ipv6.h>
1da177e4
LT
5#include <linux/sctp.h>
6
2e4e6a17
HW
7#include <linux/netfilter/x_tables.h>
8#include <linux/netfilter/xt_sctp.h>
1da177e4 9#include <linux/netfilter_ipv4/ip_tables.h>
2e4e6a17
HW
10#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");
73aaf935 16MODULE_ALIAS("ip6t_sctp");
1da177e4
LT
17
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
1d93a9cb 27static bool
2e4e6a17 28match_flags(const struct xt_sctp_flag_info *flag_info,
1da177e4
LT
29 const int flag_count,
30 u_int8_t chunktype,
31 u_int8_t chunkflags)
32{
33 int i;
34
7c4e36bc
JE
35 for (i = 0; i < flag_count; i++)
36 if (flag_info[i].chunktype == chunktype)
1da177e4 37 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
1da177e4 38
1d93a9cb 39 return true;
1da177e4
LT
40}
41
1d93a9cb 42static inline bool
1da177e4 43match_packet(const struct sk_buff *skb,
2e4e6a17 44 unsigned int offset,
1da177e4
LT
45 const u_int32_t *chunkmap,
46 int chunk_match_type,
2e4e6a17 47 const struct xt_sctp_flag_info *flag_info,
1da177e4 48 const int flag_count,
cff533ac 49 bool *hotdrop)
1da177e4 50{
1da177e4
LT
51 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
52 sctp_chunkhdr_t _sch, *sch;
53
54#ifdef DEBUG_SCTP
55 int i = 0;
56#endif
57
7c4e36bc 58 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
1da177e4 59 SCTP_CHUNKMAP_COPY(chunkmapcopy, chunkmap);
1da177e4 60
1da177e4
LT
61 do {
62 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
d3dcd4ef 63 if (sch == NULL || sch->length == 0) {
1da177e4 64 duprintf("Dropping invalid SCTP packet.\n");
cff533ac 65 *hotdrop = true;
1d93a9cb 66 return false;
601e68e1 67 }
1da177e4 68
601e68e1 69 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
1da177e4
LT
70 ++i, offset, sch->type, htons(sch->length), sch->flags);
71
962c8372 72 offset += (ntohs(sch->length) + 3) & ~3;
1da177e4
LT
73
74 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
75
76 if (SCTP_CHUNKMAP_IS_SET(chunkmap, sch->type)) {
77 switch (chunk_match_type) {
78 case SCTP_CHUNK_MATCH_ANY:
601e68e1 79 if (match_flags(flag_info, flag_count,
1da177e4 80 sch->type, sch->flags)) {
1d93a9cb 81 return true;
1da177e4
LT
82 }
83 break;
84
85 case SCTP_CHUNK_MATCH_ALL:
601e68e1 86 if (match_flags(flag_info, flag_count,
7c4e36bc 87 sch->type, sch->flags))
1da177e4 88 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
1da177e4
LT
89 break;
90
91 case SCTP_CHUNK_MATCH_ONLY:
601e68e1 92 if (!match_flags(flag_info, flag_count,
7c4e36bc 93 sch->type, sch->flags))
1d93a9cb 94 return false;
1da177e4
LT
95 break;
96 }
97 } else {
98 switch (chunk_match_type) {
99 case SCTP_CHUNK_MATCH_ONLY:
1d93a9cb 100 return false;
1da177e4
LT
101 }
102 }
103 } while (offset < skb->len);
104
105 switch (chunk_match_type) {
106 case SCTP_CHUNK_MATCH_ALL:
107 return SCTP_CHUNKMAP_IS_CLEAR(chunkmap);
108 case SCTP_CHUNK_MATCH_ANY:
1d93a9cb 109 return false;
1da177e4 110 case SCTP_CHUNK_MATCH_ONLY:
1d93a9cb 111 return true;
1da177e4
LT
112 }
113
114 /* This will never be reached, but required to stop compiler whine */
1d93a9cb 115 return false;
1da177e4
LT
116}
117
1d93a9cb 118static bool
1da177e4
LT
119match(const struct sk_buff *skb,
120 const struct net_device *in,
121 const struct net_device *out,
c4986734 122 const struct xt_match *match,
1da177e4
LT
123 const void *matchinfo,
124 int offset,
2e4e6a17 125 unsigned int protoff,
cff533ac 126 bool *hotdrop)
1da177e4 127{
3e72b2fe 128 const struct xt_sctp_info *info = matchinfo;
1da177e4
LT
129 sctp_sctphdr_t _sh, *sh;
130
1da177e4
LT
131 if (offset) {
132 duprintf("Dropping non-first fragment.. FIXME\n");
1d93a9cb 133 return false;
1da177e4 134 }
601e68e1 135
2e4e6a17 136 sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
1da177e4
LT
137 if (sh == NULL) {
138 duprintf("Dropping evil TCP offset=0 tinygram.\n");
cff533ac 139 *hotdrop = true;
1d93a9cb 140 return false;
601e68e1 141 }
1da177e4
LT
142 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
143
7c4e36bc
JE
144 return SCCHECK(ntohs(sh->source) >= info->spts[0]
145 && ntohs(sh->source) <= info->spts[1],
601e68e1 146 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
7c4e36bc
JE
147 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
148 && ntohs(sh->dest) <= info->dpts[1],
2e4e6a17 149 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
1c7e4772 150 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
2e4e6a17 151 info->chunkmap, info->chunk_match_type,
601e68e1 152 info->flag_info, info->flag_count,
1da177e4 153 hotdrop),
2e4e6a17 154 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
1da177e4
LT
155}
156
ccb79bdc 157static bool
1da177e4 158checkentry(const char *tablename,
2e4e6a17 159 const void *inf,
c4986734 160 const struct xt_match *match,
2e4e6a17 161 void *matchinfo,
2e4e6a17
HW
162 unsigned int hook_mask)
163{
5d04bff0 164 const struct xt_sctp_info *info = matchinfo;
2e4e6a17 165
5d04bff0 166 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
2e4e6a17
HW
167 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
168 && !(info->invflags & ~info->flags)
601e68e1 169 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
2e4e6a17 170 (info->chunk_match_type &
601e68e1 171 (SCTP_CHUNK_MATCH_ALL
2e4e6a17
HW
172 | SCTP_CHUNK_MATCH_ANY
173 | SCTP_CHUNK_MATCH_ONLY)));
174}
175
9f15c530 176static struct xt_match xt_sctp_match[] __read_mostly = {
4470bbc7
PM
177 {
178 .name = "sctp",
179 .family = AF_INET,
180 .checkentry = checkentry,
181 .match = match,
182 .matchsize = sizeof(struct xt_sctp_info),
183 .proto = IPPROTO_SCTP,
184 .me = THIS_MODULE
185 },
186 {
187 .name = "sctp",
188 .family = AF_INET6,
189 .checkentry = checkentry,
190 .match = match,
191 .matchsize = sizeof(struct xt_sctp_info),
192 .proto = IPPROTO_SCTP,
193 .me = THIS_MODULE
194 },
5d04bff0 195};
2e4e6a17 196
65b4b4e8 197static int __init xt_sctp_init(void)
1da177e4 198{
4470bbc7 199 return xt_register_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
1da177e4
LT
200}
201
65b4b4e8 202static void __exit xt_sctp_fini(void)
1da177e4 203{
4470bbc7 204 xt_unregister_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
1da177e4
LT
205}
206
65b4b4e8
AM
207module_init(xt_sctp_init);
208module_exit(xt_sctp_fini);