]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/netfilter/xt_sctp.c
[NETFILTER]: xt_sctp: simplify xt_sctp.h
[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");
2ae15b64 14MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
2e4e6a17 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,
009e8c96 45 const struct xt_sctp_info *info,
cff533ac 46 bool *hotdrop)
1da177e4 47{
1da177e4
LT
48 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
49 sctp_chunkhdr_t _sch, *sch;
009e8c96
LZ
50 int chunk_match_type = info->chunk_match_type;
51 const struct xt_sctp_flag_info *flag_info = info->flag_info;
52 int flag_count = info->flag_count;
1da177e4
LT
53
54#ifdef DEBUG_SCTP
55 int i = 0;
56#endif
57
7c4e36bc 58 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
009e8c96 59 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->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
009e8c96 76 if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) {
1da177e4
LT
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:
009e8c96 107 return SCTP_CHUNKMAP_IS_CLEAR(info->chunkmap);
1da177e4 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
d3c5ee6d
JE
119sctp_mt(const struct sk_buff *skb, const struct net_device *in,
120 const struct net_device *out, const struct xt_match *match,
121 const void *matchinfo, int offset, unsigned int protoff, bool *hotdrop)
1da177e4 122{
3e72b2fe 123 const struct xt_sctp_info *info = matchinfo;
1da177e4
LT
124 sctp_sctphdr_t _sh, *sh;
125
1da177e4
LT
126 if (offset) {
127 duprintf("Dropping non-first fragment.. FIXME\n");
1d93a9cb 128 return false;
1da177e4 129 }
601e68e1 130
2e4e6a17 131 sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
1da177e4
LT
132 if (sh == NULL) {
133 duprintf("Dropping evil TCP offset=0 tinygram.\n");
cff533ac 134 *hotdrop = true;
1d93a9cb 135 return false;
601e68e1 136 }
1da177e4
LT
137 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
138
7c4e36bc
JE
139 return SCCHECK(ntohs(sh->source) >= info->spts[0]
140 && ntohs(sh->source) <= info->spts[1],
601e68e1 141 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
7c4e36bc
JE
142 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
143 && ntohs(sh->dest) <= info->dpts[1],
2e4e6a17 144 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
1c7e4772 145 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
009e8c96 146 info, hotdrop),
2e4e6a17 147 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
1da177e4
LT
148}
149
ccb79bdc 150static bool
d3c5ee6d
JE
151sctp_mt_check(const char *tablename, const void *inf,
152 const struct xt_match *match, void *matchinfo,
153 unsigned int hook_mask)
2e4e6a17 154{
5d04bff0 155 const struct xt_sctp_info *info = matchinfo;
2e4e6a17 156
5d04bff0 157 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
2e4e6a17
HW
158 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
159 && !(info->invflags & ~info->flags)
601e68e1 160 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
2e4e6a17 161 (info->chunk_match_type &
601e68e1 162 (SCTP_CHUNK_MATCH_ALL
2e4e6a17
HW
163 | SCTP_CHUNK_MATCH_ANY
164 | SCTP_CHUNK_MATCH_ONLY)));
165}
166
d3c5ee6d 167static struct xt_match sctp_mt_reg[] __read_mostly = {
4470bbc7
PM
168 {
169 .name = "sctp",
170 .family = AF_INET,
d3c5ee6d
JE
171 .checkentry = sctp_mt_check,
172 .match = sctp_mt,
4470bbc7
PM
173 .matchsize = sizeof(struct xt_sctp_info),
174 .proto = IPPROTO_SCTP,
175 .me = THIS_MODULE
176 },
177 {
178 .name = "sctp",
179 .family = AF_INET6,
d3c5ee6d
JE
180 .checkentry = sctp_mt_check,
181 .match = sctp_mt,
4470bbc7
PM
182 .matchsize = sizeof(struct xt_sctp_info),
183 .proto = IPPROTO_SCTP,
184 .me = THIS_MODULE
185 },
5d04bff0 186};
2e4e6a17 187
d3c5ee6d 188static int __init sctp_mt_init(void)
1da177e4 189{
d3c5ee6d 190 return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
1da177e4
LT
191}
192
d3c5ee6d 193static void __exit sctp_mt_exit(void)
1da177e4 194{
d3c5ee6d 195 xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
1da177e4
LT
196}
197
d3c5ee6d
JE
198module_init(sctp_mt_init);
199module_exit(sctp_mt_exit);