Char: rocket, fix signed/unsigned warning
[linux-2.6/mini2440.git] / net / netfilter / xt_sctp.c
blobf907770fd4e9097302ef8343f25401eec5eb6ee9
1 #include <linux/module.h>
2 #include <linux/skbuff.h>
3 #include <net/ip.h>
4 #include <net/ipv6.h>
5 #include <linux/sctp.h>
7 #include <linux/netfilter/x_tables.h>
8 #include <linux/netfilter/xt_sctp.h>
9 #include <linux/netfilter_ipv4/ip_tables.h>
10 #include <linux/netfilter_ipv6/ip6_tables.h>
12 MODULE_LICENSE("GPL");
13 MODULE_AUTHOR("Kiran Kumar Immidi");
14 MODULE_DESCRIPTION("Match for SCTP protocol packets");
15 MODULE_ALIAS("ipt_sctp");
16 MODULE_ALIAS("ip6t_sctp");
18 #ifdef DEBUG_SCTP
19 #define duprintf(format, args...) printk(format , ## args)
20 #else
21 #define duprintf(format, args...)
22 #endif
24 #define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \
25 || (!!((invflag) & (option)) ^ (cond)))
27 static bool
28 match_flags(const struct xt_sctp_flag_info *flag_info,
29 const int flag_count,
30 u_int8_t chunktype,
31 u_int8_t chunkflags)
33 int i;
35 for (i = 0; i < flag_count; i++)
36 if (flag_info[i].chunktype == chunktype)
37 return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag;
39 return true;
42 static inline bool
43 match_packet(const struct sk_buff *skb,
44 unsigned int offset,
45 const u_int32_t *chunkmap,
46 int chunk_match_type,
47 const struct xt_sctp_flag_info *flag_info,
48 const int flag_count,
49 bool *hotdrop)
51 u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
52 sctp_chunkhdr_t _sch, *sch;
54 #ifdef DEBUG_SCTP
55 int i = 0;
56 #endif
58 if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
59 SCTP_CHUNKMAP_COPY(chunkmapcopy, chunkmap);
61 do {
62 sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch);
63 if (sch == NULL || sch->length == 0) {
64 duprintf("Dropping invalid SCTP packet.\n");
65 *hotdrop = true;
66 return false;
69 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
70 ++i, offset, sch->type, htons(sch->length), sch->flags);
72 offset += (ntohs(sch->length) + 3) & ~3;
74 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
76 if (SCTP_CHUNKMAP_IS_SET(chunkmap, sch->type)) {
77 switch (chunk_match_type) {
78 case SCTP_CHUNK_MATCH_ANY:
79 if (match_flags(flag_info, flag_count,
80 sch->type, sch->flags)) {
81 return true;
83 break;
85 case SCTP_CHUNK_MATCH_ALL:
86 if (match_flags(flag_info, flag_count,
87 sch->type, sch->flags))
88 SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type);
89 break;
91 case SCTP_CHUNK_MATCH_ONLY:
92 if (!match_flags(flag_info, flag_count,
93 sch->type, sch->flags))
94 return false;
95 break;
97 } else {
98 switch (chunk_match_type) {
99 case SCTP_CHUNK_MATCH_ONLY:
100 return false;
103 } while (offset < skb->len);
105 switch (chunk_match_type) {
106 case SCTP_CHUNK_MATCH_ALL:
107 return SCTP_CHUNKMAP_IS_CLEAR(chunkmap);
108 case SCTP_CHUNK_MATCH_ANY:
109 return false;
110 case SCTP_CHUNK_MATCH_ONLY:
111 return true;
114 /* This will never be reached, but required to stop compiler whine */
115 return false;
118 static bool
119 match(const struct sk_buff *skb,
120 const struct net_device *in,
121 const struct net_device *out,
122 const struct xt_match *match,
123 const void *matchinfo,
124 int offset,
125 unsigned int protoff,
126 bool *hotdrop)
128 const struct xt_sctp_info *info = matchinfo;
129 sctp_sctphdr_t _sh, *sh;
131 if (offset) {
132 duprintf("Dropping non-first fragment.. FIXME\n");
133 return false;
136 sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
137 if (sh == NULL) {
138 duprintf("Dropping evil TCP offset=0 tinygram.\n");
139 *hotdrop = true;
140 return false;
142 duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
144 return SCCHECK(ntohs(sh->source) >= info->spts[0]
145 && ntohs(sh->source) <= info->spts[1],
146 XT_SCTP_SRC_PORTS, info->flags, info->invflags)
147 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
148 && ntohs(sh->dest) <= info->dpts[1],
149 XT_SCTP_DEST_PORTS, info->flags, info->invflags)
150 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
151 info->chunkmap, info->chunk_match_type,
152 info->flag_info, info->flag_count,
153 hotdrop),
154 XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
157 static bool
158 checkentry(const char *tablename,
159 const void *inf,
160 const struct xt_match *match,
161 void *matchinfo,
162 unsigned int hook_mask)
164 const struct xt_sctp_info *info = matchinfo;
166 return !(info->flags & ~XT_SCTP_VALID_FLAGS)
167 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
168 && !(info->invflags & ~info->flags)
169 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
170 (info->chunk_match_type &
171 (SCTP_CHUNK_MATCH_ALL
172 | SCTP_CHUNK_MATCH_ANY
173 | SCTP_CHUNK_MATCH_ONLY)));
176 static struct xt_match xt_sctp_match[] __read_mostly = {
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
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
197 static int __init xt_sctp_init(void)
199 return xt_register_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
202 static void __exit xt_sctp_fini(void)
204 xt_unregister_matches(xt_sctp_match, ARRAY_SIZE(xt_sctp_match));
207 module_init(xt_sctp_init);
208 module_exit(xt_sctp_fini);