Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #include <linux/module.h> |
| 2 | #include <linux/skbuff.h> |
| 3 | #include <net/ip.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 4 | #include <net/ipv6.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/sctp.h> |
| 6 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 7 | #include <linux/netfilter/x_tables.h> |
| 8 | #include <linux/netfilter/xt_sctp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/netfilter_ipv4/ip_tables.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 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"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 26 | static int |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 27 | match_flags(const struct xt_sctp_flag_info *flag_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 40 | return 1; |
| 41 | } |
| 42 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 43 | static inline int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | match_packet(const struct sk_buff *skb, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 45 | unsigned int offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | const u_int32_t *chunkmap, |
| 47 | int chunk_match_type, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 48 | const struct xt_sctp_flag_info *flag_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | const int flag_count, |
| 50 | int *hotdrop) |
| 51 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | do { |
| 64 | sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch); |
| 65 | if (sch == NULL) { |
| 66 | duprintf("Dropping invalid SCTP packet.\n"); |
| 67 | *hotdrop = 1; |
| 68 | return 0; |
| 69 | } |
| 70 | |
| 71 | duprintf("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d\tflags: %x\n", |
| 72 | ++i, offset, sch->type, htons(sch->length), sch->flags); |
| 73 | |
| 74 | offset += (htons(sch->length) + 3) & ~3; |
| 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: |
| 81 | if (match_flags(flag_info, flag_count, |
| 82 | sch->type, sch->flags)) { |
| 83 | return 1; |
| 84 | } |
| 85 | break; |
| 86 | |
| 87 | case SCTP_CHUNK_MATCH_ALL: |
| 88 | if (match_flags(flag_info, flag_count, |
| 89 | sch->type, sch->flags)) { |
| 90 | SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type); |
| 91 | } |
| 92 | break; |
| 93 | |
| 94 | case SCTP_CHUNK_MATCH_ONLY: |
| 95 | if (!match_flags(flag_info, flag_count, |
| 96 | sch->type, sch->flags)) { |
| 97 | return 0; |
| 98 | } |
| 99 | break; |
| 100 | } |
| 101 | } else { |
| 102 | switch (chunk_match_type) { |
| 103 | case SCTP_CHUNK_MATCH_ONLY: |
| 104 | return 0; |
| 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: |
| 113 | return 0; |
| 114 | case SCTP_CHUNK_MATCH_ONLY: |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | /* This will never be reached, but required to stop compiler whine */ |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int |
| 123 | match(const struct sk_buff *skb, |
| 124 | const struct net_device *in, |
| 125 | const struct net_device *out, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 126 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | const void *matchinfo, |
| 128 | int offset, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 129 | unsigned int protoff, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | int *hotdrop) |
| 131 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 132 | const struct xt_sctp_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | sctp_sctphdr_t _sh, *sh; |
| 134 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 135 | info = (const struct xt_sctp_info *)matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | if (offset) { |
| 138 | duprintf("Dropping non-first fragment.. FIXME\n"); |
| 139 | return 0; |
| 140 | } |
| 141 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 142 | sh = skb_header_pointer(skb, protoff, sizeof(_sh), &_sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (sh == NULL) { |
| 144 | duprintf("Dropping evil TCP offset=0 tinygram.\n"); |
| 145 | *hotdrop = 1; |
| 146 | return 0; |
| 147 | } |
| 148 | duprintf("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest)); |
| 149 | |
| 150 | return SCCHECK(((ntohs(sh->source) >= info->spts[0]) |
| 151 | && (ntohs(sh->source) <= info->spts[1])), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 152 | XT_SCTP_SRC_PORTS, info->flags, info->invflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | && SCCHECK(((ntohs(sh->dest) >= info->dpts[0]) |
| 154 | && (ntohs(sh->dest) <= info->dpts[1])), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 155 | XT_SCTP_DEST_PORTS, info->flags, info->invflags) |
| 156 | && SCCHECK(match_packet(skb, protoff, |
| 157 | info->chunkmap, info->chunk_match_type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | info->flag_info, info->flag_count, |
| 159 | hotdrop), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 160 | XT_SCTP_CHUNK_TYPES, info->flags, info->invflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static int |
| 164 | checkentry(const char *tablename, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 165 | const void *inf, |
Patrick McHardy | c498673 | 2006-03-20 18:02:56 -0800 | [diff] [blame] | 166 | const struct xt_match *match, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | void *matchinfo, |
| 168 | unsigned int matchsize, |
| 169 | unsigned int hook_mask) |
| 170 | { |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 171 | const struct xt_sctp_info *info = matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 173 | return !(info->flags & ~XT_SCTP_VALID_FLAGS) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 174 | && !(info->invflags & ~XT_SCTP_VALID_FLAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | && !(info->invflags & ~info->flags) |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 176 | && ((!(info->flags & XT_SCTP_CHUNK_TYPES)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | (info->chunk_match_type & |
| 178 | (SCTP_CHUNK_MATCH_ALL |
| 179 | | SCTP_CHUNK_MATCH_ANY |
| 180 | | SCTP_CHUNK_MATCH_ONLY))); |
| 181 | } |
| 182 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 183 | static struct xt_match sctp_match = { |
| 184 | .name = "sctp", |
| 185 | .match = match, |
| 186 | .matchsize = sizeof(struct xt_sctp_info), |
| 187 | .proto = IPPROTO_SCTP, |
| 188 | .checkentry = checkentry, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 189 | .family = AF_INET, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 190 | .me = THIS_MODULE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | }; |
| 192 | |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 193 | static struct xt_match sctp6_match = { |
| 194 | .name = "sctp", |
| 195 | .match = match, |
| 196 | .matchsize = sizeof(struct xt_sctp_info), |
| 197 | .proto = IPPROTO_SCTP, |
| 198 | .checkentry = checkentry, |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 199 | .family = AF_INET6, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 200 | .me = THIS_MODULE |
| 201 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 202 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame^] | 203 | static int __init xt_sctp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 205 | int ret; |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 206 | ret = xt_register_match(&sctp_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 207 | if (ret) |
| 208 | return ret; |
| 209 | |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 210 | ret = xt_register_match(&sctp6_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 211 | if (ret) |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 212 | xt_unregister_match(&sctp_match); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 213 | |
| 214 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame^] | 217 | static void __exit xt_sctp_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
Pablo Neira Ayuso | a45049c | 2006-03-22 13:55:40 -0800 | [diff] [blame] | 219 | xt_unregister_match(&sctp6_match); |
| 220 | xt_unregister_match(&sctp_match); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame^] | 223 | module_init(xt_sctp_init); |
| 224 | module_exit(xt_sctp_fini); |