Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Connection tracking protocol helper module for SCTP. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 3 | * |
| 4 | * SCTP is defined in RFC 2960. References to various sections in this code |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 5 | * are to this RFC. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 6 | * |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 13 | #include <linux/timer.h> |
| 14 | #include <linux/netfilter.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/in.h> |
| 17 | #include <linux/ip.h> |
| 18 | #include <linux/sctp.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/seq_file.h> |
Yasuyuki Kozakai | 40a839f | 2006-06-27 03:00:35 -0700 | [diff] [blame] | 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/interrupt.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 23 | |
| 24 | #include <net/netfilter/nf_conntrack.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Martin Josefsson | f618012 | 2006-11-29 02:35:01 +0100 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_ecache.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 27 | |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 28 | /* Protects ct->proto.sctp */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 29 | static DEFINE_RWLOCK(sctp_lock); |
| 30 | |
| 31 | /* FIXME: Examine ipfilter's timeouts and conntrack transitions more |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 32 | closely. They're more complex. --RR |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 33 | |
| 34 | And so for me for SCTP :D -Kiran */ |
| 35 | |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 36 | static const char *const sctp_conntrack_names[] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 37 | "NONE", |
| 38 | "CLOSED", |
| 39 | "COOKIE_WAIT", |
| 40 | "COOKIE_ECHOED", |
| 41 | "ESTABLISHED", |
| 42 | "SHUTDOWN_SENT", |
| 43 | "SHUTDOWN_RECD", |
| 44 | "SHUTDOWN_ACK_SENT", |
| 45 | }; |
| 46 | |
| 47 | #define SECS * HZ |
| 48 | #define MINS * 60 SECS |
| 49 | #define HOURS * 60 MINS |
| 50 | #define DAYS * 24 HOURS |
| 51 | |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 52 | static unsigned int sctp_timeouts[SCTP_CONNTRACK_MAX] __read_mostly = { |
| 53 | [SCTP_CONNTRACK_CLOSED] = 10 SECS, |
| 54 | [SCTP_CONNTRACK_COOKIE_WAIT] = 3 SECS, |
| 55 | [SCTP_CONNTRACK_COOKIE_ECHOED] = 3 SECS, |
| 56 | [SCTP_CONNTRACK_ESTABLISHED] = 5 DAYS, |
| 57 | [SCTP_CONNTRACK_SHUTDOWN_SENT] = 300 SECS / 1000, |
| 58 | [SCTP_CONNTRACK_SHUTDOWN_RECD] = 300 SECS / 1000, |
| 59 | [SCTP_CONNTRACK_SHUTDOWN_ACK_SENT] = 3 SECS, |
| 60 | }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 61 | |
| 62 | #define sNO SCTP_CONNTRACK_NONE |
| 63 | #define sCL SCTP_CONNTRACK_CLOSED |
| 64 | #define sCW SCTP_CONNTRACK_COOKIE_WAIT |
| 65 | #define sCE SCTP_CONNTRACK_COOKIE_ECHOED |
| 66 | #define sES SCTP_CONNTRACK_ESTABLISHED |
| 67 | #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT |
| 68 | #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD |
| 69 | #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT |
| 70 | #define sIV SCTP_CONNTRACK_MAX |
| 71 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 72 | /* |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 73 | These are the descriptions of the states: |
| 74 | |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 75 | NOTE: These state names are tantalizingly similar to the states of an |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 76 | SCTP endpoint. But the interpretation of the states is a little different, |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 77 | considering that these are the states of the connection and not of an end |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 78 | point. Please note the subtleties. -Kiran |
| 79 | |
| 80 | NONE - Nothing so far. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 81 | COOKIE WAIT - We have seen an INIT chunk in the original direction, or also |
| 82 | an INIT_ACK chunk in the reply direction. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 83 | COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction. |
| 84 | ESTABLISHED - We have seen a COOKIE_ACK in the reply direction. |
| 85 | SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction. |
| 86 | SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin. |
| 87 | SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 88 | to that of the SHUTDOWN chunk. |
| 89 | CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of |
| 90 | the SHUTDOWN chunk. Connection is closed. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 91 | */ |
| 92 | |
| 93 | /* TODO |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 94 | - I have assumed that the first INIT is in the original direction. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 95 | This messes things when an INIT comes in the reply direction in CLOSED |
| 96 | state. |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 97 | - Check the error type in the reply dir before transitioning from |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 98 | cookie echoed to closed. |
| 99 | - Sec 5.2.4 of RFC 2960 |
| 100 | - Multi Homing support. |
| 101 | */ |
| 102 | |
| 103 | /* SCTP conntrack state transitions */ |
Patrick McHardy | a5e73c2 | 2008-01-14 23:45:11 -0800 | [diff] [blame] | 104 | static const u8 sctp_conntracks[2][9][SCTP_CONNTRACK_MAX] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 105 | { |
| 106 | /* ORIGINAL */ |
| 107 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */ |
| 108 | /* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA}, |
| 109 | /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA}, |
| 110 | /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, |
| 111 | /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA}, |
| 112 | /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA}, |
| 113 | /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant have Stale cookie*/ |
| 114 | /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */ |
| 115 | /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in orig dir */ |
| 116 | /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL} |
| 117 | }, |
| 118 | { |
| 119 | /* REPLY */ |
| 120 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */ |
| 121 | /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* INIT in sCL Big TODO */ |
| 122 | /* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA}, |
| 123 | /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, |
| 124 | /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA}, |
| 125 | /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA}, |
| 126 | /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA}, |
| 127 | /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in reply dir */ |
| 128 | /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA}, |
| 129 | /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL} |
| 130 | } |
| 131 | }; |
| 132 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 133 | static bool sctp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
| 134 | struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 135 | { |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 136 | const struct sctphdr *hp; |
| 137 | struct sctphdr _hdr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 138 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 139 | /* Actually only need first 8 bytes. */ |
| 140 | hp = skb_header_pointer(skb, dataoff, 8, &_hdr); |
| 141 | if (hp == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 142 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 143 | |
| 144 | tuple->src.u.sctp.port = hp->source; |
| 145 | tuple->dst.u.sctp.port = hp->dest; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 146 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 149 | static bool sctp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 150 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 151 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 152 | tuple->src.u.sctp.port = orig->dst.u.sctp.port; |
| 153 | tuple->dst.u.sctp.port = orig->src.u.sctp.port; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 154 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Print out the per-protocol part of the tuple. */ |
| 158 | static int sctp_print_tuple(struct seq_file *s, |
| 159 | const struct nf_conntrack_tuple *tuple) |
| 160 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 161 | return seq_printf(s, "sport=%hu dport=%hu ", |
| 162 | ntohs(tuple->src.u.sctp.port), |
| 163 | ntohs(tuple->dst.u.sctp.port)); |
| 164 | } |
| 165 | |
| 166 | /* Print out the private part of the conntrack. */ |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 167 | static int sctp_print_conntrack(struct seq_file *s, const struct nf_conn *ct) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 168 | { |
| 169 | enum sctp_conntrack state; |
| 170 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 171 | read_lock_bh(&sctp_lock); |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 172 | state = ct->proto.sctp.state; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 173 | read_unlock_bh(&sctp_lock); |
| 174 | |
| 175 | return seq_printf(s, "%s ", sctp_conntrack_names[state]); |
| 176 | } |
| 177 | |
| 178 | #define for_each_sctp_chunk(skb, sch, _sch, offset, dataoff, count) \ |
Jan Engelhardt | e79ec50 | 2007-12-17 22:44:06 -0800 | [diff] [blame] | 179 | for ((offset) = (dataoff) + sizeof(sctp_sctphdr_t), (count) = 0; \ |
| 180 | (offset) < (skb)->len && \ |
| 181 | ((sch) = skb_header_pointer((skb), (offset), sizeof(_sch), &(_sch))); \ |
| 182 | (offset) += (ntohs((sch)->length) + 3) & ~3, (count)++) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 183 | |
| 184 | /* Some validity checks to make sure the chunks are fine */ |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 185 | static int do_basic_checks(struct nf_conn *ct, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 186 | const struct sk_buff *skb, |
| 187 | unsigned int dataoff, |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 188 | unsigned long *map) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 189 | { |
| 190 | u_int32_t offset, count; |
| 191 | sctp_chunkhdr_t _sch, *sch; |
| 192 | int flag; |
| 193 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 194 | flag = 0; |
| 195 | |
| 196 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 197 | pr_debug("Chunk Num: %d Type: %d\n", count, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 198 | |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 199 | if (sch->type == SCTP_CID_INIT || |
| 200 | sch->type == SCTP_CID_INIT_ACK || |
| 201 | sch->type == SCTP_CID_SHUTDOWN_COMPLETE) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 202 | flag = 1; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 203 | |
Patrick McHardy | e17df68 | 2006-05-02 23:23:07 +0200 | [diff] [blame] | 204 | /* |
| 205 | * Cookie Ack/Echo chunks not the first OR |
| 206 | * Init / Init Ack / Shutdown compl chunks not the only chunks |
| 207 | * OR zero-length. |
| 208 | */ |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 209 | if (((sch->type == SCTP_CID_COOKIE_ACK || |
| 210 | sch->type == SCTP_CID_COOKIE_ECHO || |
| 211 | flag) && |
| 212 | count != 0) || !sch->length) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 213 | pr_debug("Basic checks failed\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 214 | return 1; |
| 215 | } |
| 216 | |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 217 | if (map) |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 218 | set_bit(sch->type, map); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 221 | pr_debug("Basic checks passed\n"); |
Patrick McHardy | dd7271f | 2006-06-29 21:40:23 -0700 | [diff] [blame] | 222 | return count == 0; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 225 | static int sctp_new_state(enum ip_conntrack_dir dir, |
| 226 | enum sctp_conntrack cur_state, |
| 227 | int chunk_type) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 228 | { |
| 229 | int i; |
| 230 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 231 | pr_debug("Chunk type: %d\n", chunk_type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 232 | |
| 233 | switch (chunk_type) { |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 234 | case SCTP_CID_INIT: |
| 235 | pr_debug("SCTP_CID_INIT\n"); |
| 236 | i = 0; |
| 237 | break; |
| 238 | case SCTP_CID_INIT_ACK: |
| 239 | pr_debug("SCTP_CID_INIT_ACK\n"); |
| 240 | i = 1; |
| 241 | break; |
| 242 | case SCTP_CID_ABORT: |
| 243 | pr_debug("SCTP_CID_ABORT\n"); |
| 244 | i = 2; |
| 245 | break; |
| 246 | case SCTP_CID_SHUTDOWN: |
| 247 | pr_debug("SCTP_CID_SHUTDOWN\n"); |
| 248 | i = 3; |
| 249 | break; |
| 250 | case SCTP_CID_SHUTDOWN_ACK: |
| 251 | pr_debug("SCTP_CID_SHUTDOWN_ACK\n"); |
| 252 | i = 4; |
| 253 | break; |
| 254 | case SCTP_CID_ERROR: |
| 255 | pr_debug("SCTP_CID_ERROR\n"); |
| 256 | i = 5; |
| 257 | break; |
| 258 | case SCTP_CID_COOKIE_ECHO: |
| 259 | pr_debug("SCTP_CID_COOKIE_ECHO\n"); |
| 260 | i = 6; |
| 261 | break; |
| 262 | case SCTP_CID_COOKIE_ACK: |
| 263 | pr_debug("SCTP_CID_COOKIE_ACK\n"); |
| 264 | i = 7; |
| 265 | break; |
| 266 | case SCTP_CID_SHUTDOWN_COMPLETE: |
| 267 | pr_debug("SCTP_CID_SHUTDOWN_COMPLETE\n"); |
| 268 | i = 8; |
| 269 | break; |
| 270 | default: |
| 271 | /* Other chunks like DATA, SACK, HEARTBEAT and |
| 272 | its ACK do not cause a change in state */ |
| 273 | pr_debug("Unknown chunk type, Will stay in %s\n", |
| 274 | sctp_conntrack_names[cur_state]); |
| 275 | return cur_state; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 276 | } |
| 277 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 278 | pr_debug("dir: %d cur_state: %s chunk_type: %d new_state: %s\n", |
| 279 | dir, sctp_conntrack_names[cur_state], chunk_type, |
| 280 | sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 281 | |
| 282 | return sctp_conntracks[dir][i][cur_state]; |
| 283 | } |
| 284 | |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 285 | /* Returns verdict for packet, or -NF_ACCEPT for invalid. */ |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 286 | static int sctp_packet(struct nf_conn *ct, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 287 | const struct sk_buff *skb, |
| 288 | unsigned int dataoff, |
| 289 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 290 | u_int8_t pf, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 291 | unsigned int hooknum) |
| 292 | { |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 293 | enum sctp_conntrack new_state, old_state; |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 294 | enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo); |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 295 | const struct sctphdr *sh; |
| 296 | struct sctphdr _sctph; |
| 297 | const struct sctp_chunkhdr *sch; |
| 298 | struct sctp_chunkhdr _sch; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 299 | u_int32_t offset, count; |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 300 | unsigned long map[256 / sizeof(unsigned long)] = { 0 }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 301 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 302 | sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph); |
| 303 | if (sh == NULL) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 304 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 305 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 306 | if (do_basic_checks(ct, skb, dataoff, map) != 0) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 307 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 308 | |
| 309 | /* Check the verification tag (Sec 8.5) */ |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 310 | if (!test_bit(SCTP_CID_INIT, map) && |
| 311 | !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) && |
| 312 | !test_bit(SCTP_CID_COOKIE_ECHO, map) && |
| 313 | !test_bit(SCTP_CID_ABORT, map) && |
| 314 | !test_bit(SCTP_CID_SHUTDOWN_ACK, map) && |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 315 | sh->vtag != ct->proto.sctp.vtag[dir]) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 316 | pr_debug("Verification tag check failed\n"); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 317 | goto out; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 320 | old_state = new_state = SCTP_CONNTRACK_MAX; |
Patrick McHardy | 4a64830 | 2008-01-14 23:47:25 -0800 | [diff] [blame] | 321 | write_lock_bh(&sctp_lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 322 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 323 | /* Special cases of Verification tag check (Sec 8.5.1) */ |
| 324 | if (sch->type == SCTP_CID_INIT) { |
| 325 | /* Sec 8.5.1 (A) */ |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 326 | if (sh->vtag != 0) |
| 327 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 328 | } else if (sch->type == SCTP_CID_ABORT) { |
| 329 | /* Sec 8.5.1 (B) */ |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 330 | if (sh->vtag != ct->proto.sctp.vtag[dir] && |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 331 | sh->vtag != ct->proto.sctp.vtag[!dir]) |
| 332 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 333 | } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) { |
| 334 | /* Sec 8.5.1 (C) */ |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 335 | if (sh->vtag != ct->proto.sctp.vtag[dir] && |
| 336 | sh->vtag != ct->proto.sctp.vtag[!dir] && |
Patrick McHardy | 9b1c2cf | 2008-01-14 23:48:02 -0800 | [diff] [blame] | 337 | sch->flags & SCTP_CHUNK_FLAG_T) |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 338 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 339 | } else if (sch->type == SCTP_CID_COOKIE_ECHO) { |
| 340 | /* Sec 8.5.1 (D) */ |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 341 | if (sh->vtag != ct->proto.sctp.vtag[dir]) |
| 342 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 345 | old_state = ct->proto.sctp.state; |
| 346 | new_state = sctp_new_state(dir, old_state, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 347 | |
| 348 | /* Invalid */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 349 | if (new_state == SCTP_CONNTRACK_MAX) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 350 | pr_debug("nf_conntrack_sctp: Invalid dir=%i ctype=%u " |
| 351 | "conntrack=%u\n", |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 352 | dir, sch->type, old_state); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 353 | goto out_unlock; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* If it is an INIT or an INIT ACK note down the vtag */ |
Patrick McHardy | 5447d47 | 2008-01-14 23:45:48 -0800 | [diff] [blame] | 357 | if (sch->type == SCTP_CID_INIT || |
| 358 | sch->type == SCTP_CID_INIT_ACK) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 359 | sctp_inithdr_t _inithdr, *ih; |
| 360 | |
| 361 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 362 | sizeof(_inithdr), &_inithdr); |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 363 | if (ih == NULL) |
| 364 | goto out_unlock; |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 365 | pr_debug("Setting vtag %x for dir %d\n", |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 366 | ih->init_tag, !dir); |
| 367 | ct->proto.sctp.vtag[!dir] = ih->init_tag; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 368 | } |
| 369 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 370 | ct->proto.sctp.state = new_state; |
| 371 | if (old_state != new_state) |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 372 | nf_conntrack_event_cache(IPCT_PROTOINFO, ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 373 | } |
Patrick McHardy | 4a64830 | 2008-01-14 23:47:25 -0800 | [diff] [blame] | 374 | write_unlock_bh(&sctp_lock); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 375 | |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 376 | nf_ct_refresh_acct(ct, ctinfo, skb, sctp_timeouts[new_state]); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 377 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 378 | if (old_state == SCTP_CONNTRACK_COOKIE_ECHOED && |
Patrick McHardy | 8528819 | 2008-01-14 23:46:37 -0800 | [diff] [blame] | 379 | dir == IP_CT_DIR_REPLY && |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 380 | new_state == SCTP_CONNTRACK_ESTABLISHED) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 381 | pr_debug("Setting assured bit\n"); |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 382 | set_bit(IPS_ASSURED_BIT, &ct->status); |
Alexey Dobriyan | a71996f | 2008-10-08 11:35:07 +0200 | [diff] [blame] | 383 | nf_conntrack_event_cache(IPCT_STATUS, ct); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return NF_ACCEPT; |
Patrick McHardy | b37e933 | 2008-01-14 23:46:52 -0800 | [diff] [blame] | 387 | |
| 388 | out_unlock: |
| 389 | write_unlock_bh(&sctp_lock); |
| 390 | out: |
| 391 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /* Called when a new connection for this protocol found. */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 395 | static bool sctp_new(struct nf_conn *ct, const struct sk_buff *skb, |
| 396 | unsigned int dataoff) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 397 | { |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 398 | enum sctp_conntrack new_state; |
Jan Engelhardt | 12c33aa | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 399 | const struct sctphdr *sh; |
| 400 | struct sctphdr _sctph; |
| 401 | const struct sctp_chunkhdr *sch; |
| 402 | struct sctp_chunkhdr _sch; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | u_int32_t offset, count; |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 404 | unsigned long map[256 / sizeof(unsigned long)] = { 0 }; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 405 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 406 | sh = skb_header_pointer(skb, dataoff, sizeof(_sctph), &_sctph); |
| 407 | if (sh == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 408 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 409 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 410 | if (do_basic_checks(ct, skb, dataoff, map) != 0) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 411 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 412 | |
| 413 | /* If an OOTB packet has any of these chunks discard (Sec 8.4) */ |
Patrick McHardy | 35c6d3c | 2008-01-14 23:46:05 -0800 | [diff] [blame] | 414 | if (test_bit(SCTP_CID_ABORT, map) || |
| 415 | test_bit(SCTP_CID_SHUTDOWN_COMPLETE, map) || |
| 416 | test_bit(SCTP_CID_COOKIE_ACK, map)) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 417 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 418 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 419 | new_state = SCTP_CONNTRACK_MAX; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 420 | for_each_sctp_chunk (skb, sch, _sch, offset, dataoff, count) { |
| 421 | /* Don't need lock here: this conntrack not in circulation yet */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 422 | new_state = sctp_new_state(IP_CT_DIR_ORIGINAL, |
| 423 | SCTP_CONNTRACK_NONE, sch->type); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 424 | |
| 425 | /* Invalid: delete conntrack */ |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 426 | if (new_state == SCTP_CONNTRACK_NONE || |
| 427 | new_state == SCTP_CONNTRACK_MAX) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 428 | pr_debug("nf_conntrack_sctp: invalid new deleting.\n"); |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 429 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /* Copy the vtag into the state info */ |
| 433 | if (sch->type == SCTP_CID_INIT) { |
| 434 | if (sh->vtag == 0) { |
| 435 | sctp_inithdr_t _inithdr, *ih; |
| 436 | |
| 437 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | 601e68e | 2007-02-12 11:15:49 -0800 | [diff] [blame] | 438 | sizeof(_inithdr), &_inithdr); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 439 | if (ih == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 440 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 441 | |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 442 | pr_debug("Setting vtag %x for new conn\n", |
| 443 | ih->init_tag); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 444 | |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 445 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 446 | ih->init_tag; |
| 447 | } else { |
| 448 | /* Sec 8.5.1 (A) */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 449 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | /* If it is a shutdown ack OOTB packet, we expect a return |
| 453 | shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */ |
| 454 | else { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 455 | pr_debug("Setting vtag %x for new conn OOTB\n", |
| 456 | sh->vtag); |
Patrick McHardy | 112f35c | 2008-01-14 23:46:20 -0800 | [diff] [blame] | 457 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 458 | } |
| 459 | |
Patrick McHardy | efe9f68 | 2008-01-14 23:47:09 -0800 | [diff] [blame] | 460 | ct->proto.sctp.state = new_state; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 461 | } |
| 462 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 463 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 464 | } |
| 465 | |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 466 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
| 467 | |
| 468 | #include <linux/netfilter/nfnetlink.h> |
| 469 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 470 | |
| 471 | static int sctp_to_nlattr(struct sk_buff *skb, struct nlattr *nla, |
| 472 | const struct nf_conn *ct) |
| 473 | { |
| 474 | struct nlattr *nest_parms; |
| 475 | |
| 476 | read_lock_bh(&sctp_lock); |
| 477 | nest_parms = nla_nest_start(skb, CTA_PROTOINFO_SCTP | NLA_F_NESTED); |
| 478 | if (!nest_parms) |
| 479 | goto nla_put_failure; |
| 480 | |
| 481 | NLA_PUT_U8(skb, CTA_PROTOINFO_SCTP_STATE, ct->proto.sctp.state); |
| 482 | |
| 483 | NLA_PUT_BE32(skb, |
| 484 | CTA_PROTOINFO_SCTP_VTAG_ORIGINAL, |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 485 | ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL]); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 486 | |
| 487 | NLA_PUT_BE32(skb, |
| 488 | CTA_PROTOINFO_SCTP_VTAG_REPLY, |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 489 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY]); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 490 | |
| 491 | read_unlock_bh(&sctp_lock); |
| 492 | |
| 493 | nla_nest_end(skb, nest_parms); |
| 494 | |
| 495 | return 0; |
| 496 | |
| 497 | nla_put_failure: |
| 498 | read_unlock_bh(&sctp_lock); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | static const struct nla_policy sctp_nla_policy[CTA_PROTOINFO_SCTP_MAX+1] = { |
| 503 | [CTA_PROTOINFO_SCTP_STATE] = { .type = NLA_U8 }, |
| 504 | [CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] = { .type = NLA_U32 }, |
| 505 | [CTA_PROTOINFO_SCTP_VTAG_REPLY] = { .type = NLA_U32 }, |
| 506 | }; |
| 507 | |
| 508 | static int nlattr_to_sctp(struct nlattr *cda[], struct nf_conn *ct) |
| 509 | { |
| 510 | struct nlattr *attr = cda[CTA_PROTOINFO_SCTP]; |
| 511 | struct nlattr *tb[CTA_PROTOINFO_SCTP_MAX+1]; |
| 512 | int err; |
| 513 | |
| 514 | /* updates may not contain the internal protocol info, skip parsing */ |
| 515 | if (!attr) |
| 516 | return 0; |
| 517 | |
| 518 | err = nla_parse_nested(tb, |
| 519 | CTA_PROTOINFO_SCTP_MAX, |
| 520 | attr, |
| 521 | sctp_nla_policy); |
| 522 | if (err < 0) |
| 523 | return err; |
| 524 | |
| 525 | if (!tb[CTA_PROTOINFO_SCTP_STATE] || |
| 526 | !tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL] || |
| 527 | !tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]) |
| 528 | return -EINVAL; |
| 529 | |
| 530 | write_lock_bh(&sctp_lock); |
| 531 | ct->proto.sctp.state = nla_get_u8(tb[CTA_PROTOINFO_SCTP_STATE]); |
| 532 | ct->proto.sctp.vtag[IP_CT_DIR_ORIGINAL] = |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 533 | nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_ORIGINAL]); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 534 | ct->proto.sctp.vtag[IP_CT_DIR_REPLY] = |
Patrick McHardy | 5547cd0 | 2008-07-21 10:03:49 -0700 | [diff] [blame] | 535 | nla_get_be32(tb[CTA_PROTOINFO_SCTP_VTAG_REPLY]); |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 536 | write_unlock_bh(&sctp_lock); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | #endif |
| 541 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 542 | #ifdef CONFIG_SYSCTL |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 543 | static unsigned int sctp_sysctl_table_users; |
| 544 | static struct ctl_table_header *sctp_sysctl_header; |
| 545 | static struct ctl_table sctp_sysctl_table[] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 546 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 547 | .procname = "nf_conntrack_sctp_timeout_closed", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 548 | .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 549 | .maxlen = sizeof(unsigned int), |
| 550 | .mode = 0644, |
| 551 | .proc_handler = &proc_dointvec_jiffies, |
| 552 | }, |
| 553 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 554 | .procname = "nf_conntrack_sctp_timeout_cookie_wait", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 555 | .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 556 | .maxlen = sizeof(unsigned int), |
| 557 | .mode = 0644, |
| 558 | .proc_handler = &proc_dointvec_jiffies, |
| 559 | }, |
| 560 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 561 | .procname = "nf_conntrack_sctp_timeout_cookie_echoed", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 562 | .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 563 | .maxlen = sizeof(unsigned int), |
| 564 | .mode = 0644, |
| 565 | .proc_handler = &proc_dointvec_jiffies, |
| 566 | }, |
| 567 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 568 | .procname = "nf_conntrack_sctp_timeout_established", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 569 | .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 570 | .maxlen = sizeof(unsigned int), |
| 571 | .mode = 0644, |
| 572 | .proc_handler = &proc_dointvec_jiffies, |
| 573 | }, |
| 574 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 575 | .procname = "nf_conntrack_sctp_timeout_shutdown_sent", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 576 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 577 | .maxlen = sizeof(unsigned int), |
| 578 | .mode = 0644, |
| 579 | .proc_handler = &proc_dointvec_jiffies, |
| 580 | }, |
| 581 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 582 | .procname = "nf_conntrack_sctp_timeout_shutdown_recd", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 583 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 584 | .maxlen = sizeof(unsigned int), |
| 585 | .mode = 0644, |
| 586 | .proc_handler = &proc_dointvec_jiffies, |
| 587 | }, |
| 588 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 589 | .procname = "nf_conntrack_sctp_timeout_shutdown_ack_sent", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 590 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT], |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 591 | .maxlen = sizeof(unsigned int), |
| 592 | .mode = 0644, |
| 593 | .proc_handler = &proc_dointvec_jiffies, |
| 594 | }, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 595 | { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 596 | .ctl_name = 0 |
| 597 | } |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 598 | }; |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 599 | |
| 600 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
| 601 | static struct ctl_table sctp_compat_sysctl_table[] = { |
| 602 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 603 | .procname = "ip_conntrack_sctp_timeout_closed", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 604 | .data = &sctp_timeouts[SCTP_CONNTRACK_CLOSED], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 605 | .maxlen = sizeof(unsigned int), |
| 606 | .mode = 0644, |
| 607 | .proc_handler = &proc_dointvec_jiffies, |
| 608 | }, |
| 609 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 610 | .procname = "ip_conntrack_sctp_timeout_cookie_wait", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 611 | .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_WAIT], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 612 | .maxlen = sizeof(unsigned int), |
| 613 | .mode = 0644, |
| 614 | .proc_handler = &proc_dointvec_jiffies, |
| 615 | }, |
| 616 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 617 | .procname = "ip_conntrack_sctp_timeout_cookie_echoed", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 618 | .data = &sctp_timeouts[SCTP_CONNTRACK_COOKIE_ECHOED], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 619 | .maxlen = sizeof(unsigned int), |
| 620 | .mode = 0644, |
| 621 | .proc_handler = &proc_dointvec_jiffies, |
| 622 | }, |
| 623 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 624 | .procname = "ip_conntrack_sctp_timeout_established", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 625 | .data = &sctp_timeouts[SCTP_CONNTRACK_ESTABLISHED], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 626 | .maxlen = sizeof(unsigned int), |
| 627 | .mode = 0644, |
| 628 | .proc_handler = &proc_dointvec_jiffies, |
| 629 | }, |
| 630 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 631 | .procname = "ip_conntrack_sctp_timeout_shutdown_sent", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 632 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_SENT], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 633 | .maxlen = sizeof(unsigned int), |
| 634 | .mode = 0644, |
| 635 | .proc_handler = &proc_dointvec_jiffies, |
| 636 | }, |
| 637 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 638 | .procname = "ip_conntrack_sctp_timeout_shutdown_recd", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 639 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_RECD], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 640 | .maxlen = sizeof(unsigned int), |
| 641 | .mode = 0644, |
| 642 | .proc_handler = &proc_dointvec_jiffies, |
| 643 | }, |
| 644 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 645 | .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent", |
Patrick McHardy | 86c0bf4 | 2008-01-14 23:48:17 -0800 | [diff] [blame] | 646 | .data = &sctp_timeouts[SCTP_CONNTRACK_SHUTDOWN_ACK_SENT], |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 647 | .maxlen = sizeof(unsigned int), |
| 648 | .mode = 0644, |
| 649 | .proc_handler = &proc_dointvec_jiffies, |
| 650 | }, |
| 651 | { |
| 652 | .ctl_name = 0 |
| 653 | } |
| 654 | }; |
| 655 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 656 | #endif |
| 657 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 658 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp4 __read_mostly = { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 659 | .l3proto = PF_INET, |
| 660 | .l4proto = IPPROTO_SCTP, |
| 661 | .name = "sctp", |
| 662 | .pkt_to_tuple = sctp_pkt_to_tuple, |
| 663 | .invert_tuple = sctp_invert_tuple, |
| 664 | .print_tuple = sctp_print_tuple, |
| 665 | .print_conntrack = sctp_print_conntrack, |
| 666 | .packet = sctp_packet, |
| 667 | .new = sctp_new, |
| 668 | .me = THIS_MODULE, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 669 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 670 | .to_nlattr = sctp_to_nlattr, |
| 671 | .from_nlattr = nlattr_to_sctp, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 672 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
| 673 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
| 674 | .nla_policy = nf_ct_port_nla_policy, |
| 675 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 676 | #ifdef CONFIG_SYSCTL |
| 677 | .ctl_table_users = &sctp_sysctl_table_users, |
| 678 | .ctl_table_header = &sctp_sysctl_header, |
| 679 | .ctl_table = sctp_sysctl_table, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 680 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
| 681 | .ctl_compat_table = sctp_compat_sysctl_table, |
| 682 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 683 | #endif |
| 684 | }; |
| 685 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 686 | static struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp6 __read_mostly = { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 687 | .l3proto = PF_INET6, |
| 688 | .l4proto = IPPROTO_SCTP, |
| 689 | .name = "sctp", |
| 690 | .pkt_to_tuple = sctp_pkt_to_tuple, |
| 691 | .invert_tuple = sctp_invert_tuple, |
| 692 | .print_tuple = sctp_print_tuple, |
| 693 | .print_conntrack = sctp_print_conntrack, |
| 694 | .packet = sctp_packet, |
| 695 | .new = sctp_new, |
| 696 | .me = THIS_MODULE, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 697 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
Pablo Neira Ayuso | a258860 | 2008-06-09 15:56:39 -0700 | [diff] [blame] | 698 | .to_nlattr = sctp_to_nlattr, |
| 699 | .from_nlattr = nlattr_to_sctp, |
Pablo Neira Ayuso | c7212e9 | 2007-12-17 22:29:02 -0800 | [diff] [blame] | 700 | .tuple_to_nlattr = nf_ct_port_tuple_to_nlattr, |
| 701 | .nlattr_to_tuple = nf_ct_port_nlattr_to_tuple, |
| 702 | .nla_policy = nf_ct_port_nla_policy, |
| 703 | #endif |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 704 | #ifdef CONFIG_SYSCTL |
| 705 | .ctl_table_users = &sctp_sysctl_table_users, |
| 706 | .ctl_table_header = &sctp_sysctl_header, |
| 707 | .ctl_table = sctp_sysctl_table, |
| 708 | #endif |
| 709 | }; |
| 710 | |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 711 | static int __init nf_conntrack_proto_sctp_init(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 712 | { |
| 713 | int ret; |
| 714 | |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 715 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 716 | if (ret) { |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 717 | printk("nf_conntrack_l4proto_sctp4: protocol register failed\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 718 | goto out; |
| 719 | } |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 720 | ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_sctp6); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 721 | if (ret) { |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 722 | printk("nf_conntrack_l4proto_sctp6: protocol register failed\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 723 | goto cleanup_sctp4; |
| 724 | } |
| 725 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 726 | return ret; |
| 727 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 728 | cleanup_sctp4: |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 729 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 730 | out: |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 731 | return ret; |
| 732 | } |
| 733 | |
Stephen Hemminger | 2f0d2f1 | 2008-01-31 04:08:10 -0800 | [diff] [blame] | 734 | static void __exit nf_conntrack_proto_sctp_fini(void) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 735 | { |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 736 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp6); |
| 737 | nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_sctp4); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 740 | module_init(nf_conntrack_proto_sctp_init); |
| 741 | module_exit(nf_conntrack_proto_sctp_fini); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 742 | |
| 743 | MODULE_LICENSE("GPL"); |
| 744 | MODULE_AUTHOR("Kiran Kumar Immidi"); |
| 745 | MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP"); |
Patrick McHardy | d2483dd | 2006-12-02 22:06:05 -0800 | [diff] [blame] | 746 | MODULE_ALIAS("ip_conntrack_proto_sctp"); |