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