]>
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"); | |
2ae15b64 | 14 | MODULE_DESCRIPTION("Xtables: SCTP protocol packet match"); |
2e4e6a17 | 15 | MODULE_ALIAS("ipt_sctp"); |
73aaf935 | 16 | MODULE_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 | 27 | static bool |
2e4e6a17 | 28 | match_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 | 42 | static inline bool |
1da177e4 | 43 | match_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 | 48 | u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)]; |
3cf93c96 JE |
49 | const sctp_chunkhdr_t *sch; |
50 | sctp_chunkhdr_t _sch; | |
009e8c96 LZ |
51 | int chunk_match_type = info->chunk_match_type; |
52 | const struct xt_sctp_flag_info *flag_info = info->flag_info; | |
53 | int flag_count = info->flag_count; | |
1da177e4 LT |
54 | |
55 | #ifdef DEBUG_SCTP | |
56 | int i = 0; | |
57 | #endif | |
58 | ||
7c4e36bc | 59 | if (chunk_match_type == SCTP_CHUNK_MATCH_ALL) |
009e8c96 | 60 | SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap); |
1da177e4 | 61 | |
1da177e4 LT |
62 | do { |
63 | sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch); | |
d3dcd4ef | 64 | if (sch == NULL || sch->length == 0) { |
1da177e4 | 65 | duprintf("Dropping invalid SCTP packet.\n"); |
cff533ac | 66 | *hotdrop = true; |
1d93a9cb | 67 | return false; |
601e68e1 | 68 | } |
1da177e4 | 69 | |
601e68e1 | 70 | duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n", |
1da177e4 LT |
71 | ++i, offset, sch->type, htons(sch->length), sch->flags); |
72 | ||
962c8372 | 73 | offset += (ntohs(sch->length) + 3) & ~3; |
1da177e4 LT |
74 | |
75 | duprintf("skb->len: %d\toffset: %d\n", skb->len, offset); | |
76 | ||
009e8c96 | 77 | if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) { |
1da177e4 LT |
78 | switch (chunk_match_type) { |
79 | case SCTP_CHUNK_MATCH_ANY: | |
601e68e1 | 80 | if (match_flags(flag_info, flag_count, |
1da177e4 | 81 | sch->type, sch->flags)) { |
1d93a9cb | 82 | return true; |
1da177e4 LT |
83 | } |
84 | break; | |
85 | ||
86 | case SCTP_CHUNK_MATCH_ALL: | |
601e68e1 | 87 | if (match_flags(flag_info, flag_count, |
7c4e36bc | 88 | sch->type, sch->flags)) |
1da177e4 | 89 | SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type); |
1da177e4 LT |
90 | break; |
91 | ||
92 | case SCTP_CHUNK_MATCH_ONLY: | |
601e68e1 | 93 | if (!match_flags(flag_info, flag_count, |
7c4e36bc | 94 | sch->type, sch->flags)) |
1d93a9cb | 95 | return false; |
1da177e4 LT |
96 | break; |
97 | } | |
98 | } else { | |
99 | switch (chunk_match_type) { | |
100 | case SCTP_CHUNK_MATCH_ONLY: | |
1d93a9cb | 101 | return false; |
1da177e4 LT |
102 | } |
103 | } | |
104 | } while (offset < skb->len); | |
105 | ||
106 | switch (chunk_match_type) { | |
107 | case SCTP_CHUNK_MATCH_ALL: | |
d4e2675a | 108 | return SCTP_CHUNKMAP_IS_CLEAR(chunkmapcopy); |
1da177e4 | 109 | case SCTP_CHUNK_MATCH_ANY: |
1d93a9cb | 110 | return false; |
1da177e4 | 111 | case SCTP_CHUNK_MATCH_ONLY: |
1d93a9cb | 112 | return true; |
1da177e4 LT |
113 | } |
114 | ||
115 | /* This will never be reached, but required to stop compiler whine */ | |
1d93a9cb | 116 | return false; |
1da177e4 LT |
117 | } |
118 | ||
1d93a9cb | 119 | static bool |
f7108a20 | 120 | sctp_mt(const struct sk_buff *skb, const struct xt_match_param *par) |
1da177e4 | 121 | { |
f7108a20 | 122 | const struct xt_sctp_info *info = par->matchinfo; |
3cf93c96 JE |
123 | const sctp_sctphdr_t *sh; |
124 | sctp_sctphdr_t _sh; | |
1da177e4 | 125 | |
f7108a20 | 126 | if (par->fragoff != 0) { |
1da177e4 | 127 | duprintf("Dropping non-first fragment.. FIXME\n"); |
1d93a9cb | 128 | return false; |
1da177e4 | 129 | } |
601e68e1 | 130 | |
f7108a20 | 131 | sh = skb_header_pointer(skb, par->thoff, sizeof(_sh), &_sh); |
1da177e4 LT |
132 | if (sh == NULL) { |
133 | duprintf("Dropping evil TCP offset=0 tinygram.\n"); | |
f7108a20 | 134 | *par->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) |
f7108a20 JE |
145 | && SCCHECK(match_packet(skb, par->thoff + sizeof(sctp_sctphdr_t), |
146 | info, par->hotdrop), | |
2e4e6a17 | 147 | XT_SCTP_CHUNK_TYPES, info->flags, info->invflags); |
1da177e4 LT |
148 | } |
149 | ||
9b4fce7a | 150 | static bool sctp_mt_check(const struct xt_mtchk_param *par) |
2e4e6a17 | 151 | { |
9b4fce7a | 152 | const struct xt_sctp_info *info = par->matchinfo; |
2e4e6a17 | 153 | |
5d04bff0 | 154 | return !(info->flags & ~XT_SCTP_VALID_FLAGS) |
2e4e6a17 HW |
155 | && !(info->invflags & ~XT_SCTP_VALID_FLAGS) |
156 | && !(info->invflags & ~info->flags) | |
601e68e1 | 157 | && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) || |
2e4e6a17 | 158 | (info->chunk_match_type & |
601e68e1 | 159 | (SCTP_CHUNK_MATCH_ALL |
2e4e6a17 HW |
160 | | SCTP_CHUNK_MATCH_ANY |
161 | | SCTP_CHUNK_MATCH_ONLY))); | |
162 | } | |
163 | ||
d3c5ee6d | 164 | static struct xt_match sctp_mt_reg[] __read_mostly = { |
4470bbc7 PM |
165 | { |
166 | .name = "sctp", | |
ee999d8b | 167 | .family = NFPROTO_IPV4, |
d3c5ee6d JE |
168 | .checkentry = sctp_mt_check, |
169 | .match = sctp_mt, | |
4470bbc7 PM |
170 | .matchsize = sizeof(struct xt_sctp_info), |
171 | .proto = IPPROTO_SCTP, | |
172 | .me = THIS_MODULE | |
173 | }, | |
174 | { | |
175 | .name = "sctp", | |
ee999d8b | 176 | .family = NFPROTO_IPV6, |
d3c5ee6d JE |
177 | .checkentry = sctp_mt_check, |
178 | .match = sctp_mt, | |
4470bbc7 PM |
179 | .matchsize = sizeof(struct xt_sctp_info), |
180 | .proto = IPPROTO_SCTP, | |
181 | .me = THIS_MODULE | |
182 | }, | |
5d04bff0 | 183 | }; |
2e4e6a17 | 184 | |
d3c5ee6d | 185 | static int __init sctp_mt_init(void) |
1da177e4 | 186 | { |
d3c5ee6d | 187 | return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg)); |
1da177e4 LT |
188 | } |
189 | ||
d3c5ee6d | 190 | static void __exit sctp_mt_exit(void) |
1da177e4 | 191 | { |
d3c5ee6d | 192 | xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg)); |
1da177e4 LT |
193 | } |
194 | ||
d3c5ee6d JE |
195 | module_init(sctp_mt_init); |
196 | module_exit(sctp_mt_exit); |