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