Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 1 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #include <linux/module.h> |
| 3 | #include <linux/skbuff.h> |
| 4 | #include <net/ip.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 5 | #include <net/ipv6.h> |
Shan Wei | 2bf0748 | 2010-06-09 14:47:40 +0200 | [diff] [blame] | 6 | #include <net/sctp/sctp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <linux/sctp.h> |
| 8 | |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 9 | #include <linux/netfilter/x_tables.h> |
| 10 | #include <linux/netfilter/xt_sctp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/netfilter_ipv4/ip_tables.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 12 | #include <linux/netfilter_ipv6/ip6_tables.h> |
| 13 | |
| 14 | MODULE_LICENSE("GPL"); |
| 15 | MODULE_AUTHOR("Kiran Kumar Immidi"); |
Jan Engelhardt | 2ae15b6 | 2008-01-14 23:42:28 -0800 | [diff] [blame] | 16 | MODULE_DESCRIPTION("Xtables: SCTP protocol packet match"); |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 17 | MODULE_ALIAS("ipt_sctp"); |
Jan Engelhardt | 73aaf93 | 2007-10-11 14:36:40 -0700 | [diff] [blame] | 18 | MODULE_ALIAS("ip6t_sctp"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #define SCCHECK(cond, option, flag, invflag) (!((flag) & (option)) \ |
| 21 | || (!!((invflag) & (option)) ^ (cond))) |
| 22 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 23 | static bool |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 24 | match_flags(const struct xt_sctp_flag_info *flag_info, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | const int flag_count, |
| 26 | u_int8_t chunktype, |
| 27 | u_int8_t chunkflags) |
| 28 | { |
| 29 | int i; |
| 30 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 31 | for (i = 0; i < flag_count; i++) |
| 32 | if (flag_info[i].chunktype == chunktype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | return (chunkflags & flag_info[i].flag_mask) == flag_info[i].flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 35 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 38 | static inline bool |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | match_packet(const struct sk_buff *skb, |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 40 | unsigned int offset, |
Li Zefan | 009e8c9 | 2007-10-18 05:12:21 -0700 | [diff] [blame] | 41 | const struct xt_sctp_info *info, |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 42 | bool *hotdrop) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | u_int32_t chunkmapcopy[256 / sizeof (u_int32_t)]; |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 45 | const sctp_chunkhdr_t *sch; |
| 46 | sctp_chunkhdr_t _sch; |
Li Zefan | 009e8c9 | 2007-10-18 05:12:21 -0700 | [diff] [blame] | 47 | int chunk_match_type = info->chunk_match_type; |
| 48 | const struct xt_sctp_flag_info *flag_info = info->flag_info; |
| 49 | int flag_count = info->flag_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 51 | #ifdef DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int i = 0; |
| 53 | #endif |
| 54 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 55 | if (chunk_match_type == SCTP_CHUNK_MATCH_ALL) |
Li Zefan | 009e8c9 | 2007-10-18 05:12:21 -0700 | [diff] [blame] | 56 | SCTP_CHUNKMAP_COPY(chunkmapcopy, info->chunkmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | do { |
| 59 | sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch); |
Patrick McHardy | d3dcd4e | 2006-06-19 23:39:45 -0700 | [diff] [blame] | 60 | if (sch == NULL || sch->length == 0) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 61 | pr_debug("Dropping invalid SCTP packet.\n"); |
Jan Engelhardt | cff533a | 2007-07-07 22:15:12 -0700 | [diff] [blame] | 62 | *hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 63 | return false; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 64 | } |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 65 | #ifdef DEBUG |
| 66 | pr_debug("Chunk num: %d\toffset: %d\ttype: %d\tlength: %d" |
| 67 | "\tflags: %x\n", |
| 68 | ++i, offset, sch->type, htons(sch->length), |
| 69 | sch->flags); |
| 70 | #endif |
Marcelo Ricardo Leitner | e2f036a | 2016-09-21 08:45:55 -0300 | [diff] [blame] | 71 | offset += SCTP_PAD4(ntohs(sch->length)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 73 | pr_debug("skb->len: %d\toffset: %d\n", skb->len, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | |
Li Zefan | 009e8c9 | 2007-10-18 05:12:21 -0700 | [diff] [blame] | 75 | if (SCTP_CHUNKMAP_IS_SET(info->chunkmap, sch->type)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | switch (chunk_match_type) { |
| 77 | case SCTP_CHUNK_MATCH_ANY: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 78 | if (match_flags(flag_info, flag_count, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | sch->type, sch->flags)) { |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 80 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | break; |
| 83 | |
| 84 | case SCTP_CHUNK_MATCH_ALL: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 85 | if (match_flags(flag_info, flag_count, |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 86 | sch->type, sch->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | SCTP_CHUNKMAP_CLEAR(chunkmapcopy, sch->type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | break; |
| 89 | |
| 90 | case SCTP_CHUNK_MATCH_ONLY: |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 91 | if (!match_flags(flag_info, flag_count, |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 92 | sch->type, sch->flags)) |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 93 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | } else { |
| 97 | switch (chunk_match_type) { |
| 98 | case SCTP_CHUNK_MATCH_ONLY: |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 99 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } while (offset < skb->len); |
| 103 | |
| 104 | switch (chunk_match_type) { |
| 105 | case SCTP_CHUNK_MATCH_ALL: |
Qu Haoran | d4e2675 | 2009-02-09 14:34:56 -0800 | [diff] [blame] | 106 | return SCTP_CHUNKMAP_IS_CLEAR(chunkmapcopy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | case SCTP_CHUNK_MATCH_ANY: |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 108 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | case SCTP_CHUNK_MATCH_ONLY: |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 110 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /* This will never be reached, but required to stop compiler whine */ |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 114 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 117 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 118 | sctp_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 120 | const struct xt_sctp_info *info = par->matchinfo; |
Jan Engelhardt | 3cf93c9 | 2008-04-14 09:56:05 +0200 | [diff] [blame] | 121 | const sctp_sctphdr_t *sh; |
| 122 | sctp_sctphdr_t _sh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 124 | if (par->fragoff != 0) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 125 | pr_debug("Dropping non-first fragment.. FIXME\n"); |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 126 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 128 | |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 129 | sh = skb_header_pointer(skb, par->thoff, sizeof(_sh), &_sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (sh == NULL) { |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 131 | pr_debug("Dropping evil TCP offset=0 tinygram.\n"); |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 132 | par->hotdrop = true; |
Jan Engelhardt | 1d93a9c | 2007-07-07 22:15:35 -0700 | [diff] [blame] | 133 | return false; |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 134 | } |
Jan Engelhardt | be91fd5 | 2010-03-18 02:22:32 +0100 | [diff] [blame] | 135 | pr_debug("spt: %d\tdpt: %d\n", ntohs(sh->source), ntohs(sh->dest)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 137 | return SCCHECK(ntohs(sh->source) >= info->spts[0] |
| 138 | && ntohs(sh->source) <= info->spts[1], |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 139 | XT_SCTP_SRC_PORTS, info->flags, info->invflags) |
Jan Engelhardt | 7c4e36b | 2007-07-07 22:19:08 -0700 | [diff] [blame] | 140 | && SCCHECK(ntohs(sh->dest) >= info->dpts[0] |
| 141 | && ntohs(sh->dest) <= info->dpts[1], |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 142 | XT_SCTP_DEST_PORTS, info->flags, info->invflags) |
Jan Engelhardt | f7108a2 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 143 | && SCCHECK(match_packet(skb, par->thoff + sizeof(sctp_sctphdr_t), |
Jan Engelhardt | b4ba261 | 2009-07-07 20:54:30 +0200 | [diff] [blame] | 144 | info, &par->hotdrop), |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 145 | XT_SCTP_CHUNK_TYPES, info->flags, info->invflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 148 | static int sctp_mt_check(const struct xt_mtchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 150 | const struct xt_sctp_info *info = par->matchinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Jan Engelhardt | 9f56731 | 2010-03-23 17:40:13 +0100 | [diff] [blame] | 152 | if (info->flags & ~XT_SCTP_VALID_FLAGS) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 153 | return -EINVAL; |
Jan Engelhardt | 9f56731 | 2010-03-23 17:40:13 +0100 | [diff] [blame] | 154 | if (info->invflags & ~XT_SCTP_VALID_FLAGS) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 155 | return -EINVAL; |
Jan Engelhardt | 9f56731 | 2010-03-23 17:40:13 +0100 | [diff] [blame] | 156 | if (info->invflags & ~info->flags) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 157 | return -EINVAL; |
Jan Engelhardt | 9f56731 | 2010-03-23 17:40:13 +0100 | [diff] [blame] | 158 | if (!(info->flags & XT_SCTP_CHUNK_TYPES)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 159 | return 0; |
Jan Engelhardt | 9f56731 | 2010-03-23 17:40:13 +0100 | [diff] [blame] | 160 | if (info->chunk_match_type & (SCTP_CHUNK_MATCH_ALL | |
| 161 | SCTP_CHUNK_MATCH_ANY | SCTP_CHUNK_MATCH_ONLY)) |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 162 | return 0; |
| 163 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 166 | static struct xt_match sctp_mt_reg[] __read_mostly = { |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 167 | { |
| 168 | .name = "sctp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 169 | .family = NFPROTO_IPV4, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 170 | .checkentry = sctp_mt_check, |
| 171 | .match = sctp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 172 | .matchsize = sizeof(struct xt_sctp_info), |
| 173 | .proto = IPPROTO_SCTP, |
| 174 | .me = THIS_MODULE |
| 175 | }, |
| 176 | { |
| 177 | .name = "sctp", |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 178 | .family = NFPROTO_IPV6, |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 179 | .checkentry = sctp_mt_check, |
| 180 | .match = sctp_mt, |
Patrick McHardy | 4470bbc | 2006-08-22 00:34:04 -0700 | [diff] [blame] | 181 | .matchsize = sizeof(struct xt_sctp_info), |
| 182 | .proto = IPPROTO_SCTP, |
| 183 | .me = THIS_MODULE |
| 184 | }, |
Patrick McHardy | 5d04bff | 2006-03-20 18:01:58 -0800 | [diff] [blame] | 185 | }; |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 186 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 187 | static int __init sctp_mt_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 189 | return xt_register_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 192 | static void __exit sctp_mt_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 194 | xt_unregister_matches(sctp_mt_reg, ARRAY_SIZE(sctp_mt_reg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Jan Engelhardt | d3c5ee6 | 2007-12-04 23:24:03 -0800 | [diff] [blame] | 197 | module_init(sctp_mt_init); |
| 198 | module_exit(sctp_mt_exit); |