Merge branches 'release' and 'autoload' into release
[linux-2.6] / net / netfilter / xt_sctp.c
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>
6
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>
11
12 MODULE_LICENSE("GPL");
13 MODULE_AUTHOR("Kiran Kumar Immidi");
14 MODULE_DESCRIPTION("Xtables: SCTP protocol packet match");
15 MODULE_ALIAS("ipt_sctp");
16 MODULE_ALIAS("ip6t_sctp");
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
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)
32 {
33         int i;
34
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;
38
39         return true;
40 }
41
42 static inline bool
43 match_packet(const struct sk_buff *skb,
44              unsigned int offset,
45              const struct xt_sctp_info *info,
46              bool *hotdrop)
47 {
48         u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)];
49         sctp_chunkhdr_t _sch, *sch;
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;
53
54 #ifdef DEBUG_SCTP
55         int i = 0;
56 #endif
57
58         if (chunk_match_type == SCTP_CHUNK_MATCH_ALL)
59                 SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap);
60
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;
67                 }
68
69                 duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n",
70                                 ++i, offset, sch->type, htons(sch->length), sch->flags);
71
72                 offset += (ntohs(sch->length) + 3) & ~3;
73
74                 duprintf("skb->len: %d\toffset: %d\n", skb->len, offset);
75
76                 if (SCTP_CHUNKMAP_IS_SET(info->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;
82                                 }
83                                 break;
84
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;
90
91                         case SCTP_CHUNK_MATCH_ONLY:
92                                 if (!match_flags(flag_info, flag_count,
93                                     sch->type, sch->flags))
94                                         return false;
95                                 break;
96                         }
97                 } else {
98                         switch (chunk_match_type) {
99                         case SCTP_CHUNK_MATCH_ONLY:
100                                 return false;
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(info->chunkmap);
108         case SCTP_CHUNK_MATCH_ANY:
109                 return false;
110         case SCTP_CHUNK_MATCH_ONLY:
111                 return true;
112         }
113
114         /* This will never be reached, but required to stop compiler whine */
115         return false;
116 }
117
118 static bool
119 sctp_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)
122 {
123         const struct xt_sctp_info *info = matchinfo;
124         sctp_sctphdr_t _sh, *sh;
125
126         if (offset) {
127                 duprintf("Dropping non-first fragment.. FIXME\n");
128                 return false;
129         }
130
131         sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh);
132         if (sh == NULL) {
133                 duprintf("Dropping evil TCP offset=0 tinygram.\n");
134                 *hotdrop = true;
135                 return false;
136         }
137         duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest));
138
139         return  SCCHECK(ntohs(sh->source) >= info->spts[0]
140                         && ntohs(sh->source) <= info->spts[1],
141                         XT_SCTP_SRC_PORTS, info->flags, info->invflags)
142                 && SCCHECK(ntohs(sh->dest) >= info->dpts[0]
143                         && ntohs(sh->dest) <= info->dpts[1],
144                         XT_SCTP_DEST_PORTS, info->flags, info->invflags)
145                 && SCCHECK(match_packet(skb, protoff + sizeof (sctp_sctphdr_t),
146                                         info, hotdrop),
147                            XT_SCTP_CHUNK_TYPES, info->flags, info->invflags);
148 }
149
150 static bool
151 sctp_mt_check(const char *tablename, const void *inf,
152               const struct xt_match *match, void *matchinfo,
153               unsigned int hook_mask)
154 {
155         const struct xt_sctp_info *info = matchinfo;
156
157         return !(info->flags & ~XT_SCTP_VALID_FLAGS)
158                 && !(info->invflags & ~XT_SCTP_VALID_FLAGS)
159                 && !(info->invflags & ~info->flags)
160                 && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) ||
161                         (info->chunk_match_type &
162                                 (SCTP_CHUNK_MATCH_ALL
163                                 | SCTP_CHUNK_MATCH_ANY
164                                 | SCTP_CHUNK_MATCH_ONLY)));
165 }
166
167 static struct xt_match sctp_mt_reg[] __read_mostly = {
168         {
169                 .name           = "sctp",
170                 .family         = AF_INET,
171                 .checkentry     = sctp_mt_check,
172                 .match          = sctp_mt,
173                 .matchsize      = sizeof(struct xt_sctp_info),
174                 .proto          = IPPROTO_SCTP,
175                 .me             = THIS_MODULE
176         },
177         {
178                 .name           = "sctp",
179                 .family         = AF_INET6,
180                 .checkentry     = sctp_mt_check,
181                 .match          = sctp_mt,
182                 .matchsize      = sizeof(struct xt_sctp_info),
183                 .proto          = IPPROTO_SCTP,
184                 .me             = THIS_MODULE
185         },
186 };
187
188 static int __init sctp_mt_init(void)
189 {
190         return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
191 }
192
193 static void __exit sctp_mt_exit(void)
194 {
195         xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg));
196 }
197
198 module_init(sctp_mt_init);
199 module_exit(sctp_mt_exit);