Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Connection tracking protocol helper module for SCTP. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 3 | * |
| 4 | * SCTP is defined in RFC 2960. References to various sections in this code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * are to this RFC. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 6 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [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. |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * Added support for proc manipulation of timeouts. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/timer.h> |
Joe Kappus | da7bc6e | 2006-01-06 23:15:04 -0800 | [diff] [blame] | 18 | #include <linux/interrupt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/netfilter.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/in.h> |
| 22 | #include <linux/ip.h> |
| 23 | #include <linux/sctp.h> |
| 24 | #include <linux/string.h> |
| 25 | #include <linux/seq_file.h> |
| 26 | |
| 27 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
| 28 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #if 0 |
| 31 | #define DEBUGP(format, ...) printk(format, ## __VA_ARGS__) |
| 32 | #else |
| 33 | #define DEBUGP(format, args...) |
| 34 | #endif |
| 35 | |
| 36 | /* Protects conntrack->proto.sctp */ |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 37 | static DEFINE_RWLOCK(sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* FIXME: Examine ipfilter's timeouts and conntrack transitions more |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 40 | closely. They're more complex. --RR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
| 42 | And so for me for SCTP :D -Kiran */ |
| 43 | |
| 44 | static const char *sctp_conntrack_names[] = { |
| 45 | "NONE", |
| 46 | "CLOSED", |
| 47 | "COOKIE_WAIT", |
| 48 | "COOKIE_ECHOED", |
| 49 | "ESTABLISHED", |
| 50 | "SHUTDOWN_SENT", |
| 51 | "SHUTDOWN_RECD", |
| 52 | "SHUTDOWN_ACK_SENT", |
| 53 | }; |
| 54 | |
| 55 | #define SECS * HZ |
| 56 | #define MINS * 60 SECS |
| 57 | #define HOURS * 60 MINS |
| 58 | #define DAYS * 24 HOURS |
| 59 | |
Brian Haley | 94aec08 | 2006-09-18 00:05:22 -0700 | [diff] [blame] | 60 | static unsigned int ip_ct_sctp_timeout_closed __read_mostly = 10 SECS; |
| 61 | static unsigned int ip_ct_sctp_timeout_cookie_wait __read_mostly = 3 SECS; |
| 62 | static unsigned int ip_ct_sctp_timeout_cookie_echoed __read_mostly = 3 SECS; |
| 63 | static unsigned int ip_ct_sctp_timeout_established __read_mostly = 5 DAYS; |
| 64 | static unsigned int ip_ct_sctp_timeout_shutdown_sent __read_mostly = 300 SECS / 1000; |
| 65 | static unsigned int ip_ct_sctp_timeout_shutdown_recd __read_mostly = 300 SECS / 1000; |
| 66 | static unsigned int ip_ct_sctp_timeout_shutdown_ack_sent __read_mostly = 3 SECS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Patrick McHardy | babbdb1 | 2006-01-09 17:48:09 -0800 | [diff] [blame] | 68 | static const unsigned int * sctp_timeouts[] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | = { NULL, /* SCTP_CONNTRACK_NONE */ |
| 70 | &ip_ct_sctp_timeout_closed, /* SCTP_CONNTRACK_CLOSED */ |
| 71 | &ip_ct_sctp_timeout_cookie_wait, /* SCTP_CONNTRACK_COOKIE_WAIT */ |
| 72 | &ip_ct_sctp_timeout_cookie_echoed, /* SCTP_CONNTRACK_COOKIE_ECHOED */ |
| 73 | &ip_ct_sctp_timeout_established, /* SCTP_CONNTRACK_ESTABLISHED */ |
| 74 | &ip_ct_sctp_timeout_shutdown_sent, /* SCTP_CONNTRACK_SHUTDOWN_SENT */ |
| 75 | &ip_ct_sctp_timeout_shutdown_recd, /* SCTP_CONNTRACK_SHUTDOWN_RECD */ |
| 76 | &ip_ct_sctp_timeout_shutdown_ack_sent /* SCTP_CONNTRACK_SHUTDOWN_ACK_SENT */ |
| 77 | }; |
| 78 | |
| 79 | #define sNO SCTP_CONNTRACK_NONE |
| 80 | #define sCL SCTP_CONNTRACK_CLOSED |
| 81 | #define sCW SCTP_CONNTRACK_COOKIE_WAIT |
| 82 | #define sCE SCTP_CONNTRACK_COOKIE_ECHOED |
| 83 | #define sES SCTP_CONNTRACK_ESTABLISHED |
| 84 | #define sSS SCTP_CONNTRACK_SHUTDOWN_SENT |
| 85 | #define sSR SCTP_CONNTRACK_SHUTDOWN_RECD |
| 86 | #define sSA SCTP_CONNTRACK_SHUTDOWN_ACK_SENT |
| 87 | #define sIV SCTP_CONNTRACK_MAX |
| 88 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 89 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | These are the descriptions of the states: |
| 91 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 92 | NOTE: These state names are tantalizingly similar to the states of an |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | SCTP endpoint. But the interpretation of the states is a little different, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 94 | considering that these are the states of the connection and not of an end |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | point. Please note the subtleties. -Kiran |
| 96 | |
| 97 | NONE - Nothing so far. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 98 | COOKIE WAIT - We have seen an INIT chunk in the original direction, or also |
| 99 | an INIT_ACK chunk in the reply direction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | COOKIE ECHOED - We have seen a COOKIE_ECHO chunk in the original direction. |
| 101 | ESTABLISHED - We have seen a COOKIE_ACK in the reply direction. |
| 102 | SHUTDOWN_SENT - We have seen a SHUTDOWN chunk in the original direction. |
| 103 | SHUTDOWN_RECD - We have seen a SHUTDOWN chunk in the reply directoin. |
| 104 | SHUTDOWN_ACK_SENT - We have seen a SHUTDOWN_ACK chunk in the direction opposite |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 105 | to that of the SHUTDOWN chunk. |
| 106 | CLOSED - We have seen a SHUTDOWN_COMPLETE chunk in the direction of |
| 107 | the SHUTDOWN chunk. Connection is closed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | */ |
| 109 | |
| 110 | /* TODO |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 111 | - I have assumed that the first INIT is in the original direction. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | This messes things when an INIT comes in the reply direction in CLOSED |
| 113 | state. |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 114 | - Check the error type in the reply dir before transitioning from |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | cookie echoed to closed. |
| 116 | - Sec 5.2.4 of RFC 2960 |
| 117 | - Multi Homing support. |
| 118 | */ |
| 119 | |
| 120 | /* SCTP conntrack state transitions */ |
Arjan van de Ven | 9b5b5cf | 2005-11-29 16:21:38 -0800 | [diff] [blame] | 121 | static const enum sctp_conntrack sctp_conntracks[2][9][SCTP_CONNTRACK_MAX] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
| 123 | /* ORIGINAL */ |
| 124 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */ |
| 125 | /* init */ {sCW, sCW, sCW, sCE, sES, sSS, sSR, sSA}, |
| 126 | /* init_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA}, |
| 127 | /* abort */ {sCL, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, |
| 128 | /* shutdown */ {sCL, sCL, sCW, sCE, sSS, sSS, sSR, sSA}, |
| 129 | /* shutdown_ack */ {sSA, sCL, sCW, sCE, sES, sSA, sSA, sSA}, |
| 130 | /* error */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant have Stale cookie*/ |
| 131 | /* cookie_echo */ {sCL, sCL, sCE, sCE, sES, sSS, sSR, sSA},/* 5.2.4 - Big TODO */ |
| 132 | /* cookie_ack */ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in orig dir */ |
| 133 | /* shutdown_comp*/ {sCL, sCL, sCW, sCE, sES, sSS, sSR, sCL} |
| 134 | }, |
| 135 | { |
| 136 | /* REPLY */ |
| 137 | /* sNO, sCL, sCW, sCE, sES, sSS, sSR, sSA */ |
| 138 | /* init */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* INIT in sCL Big TODO */ |
| 139 | /* init_ack */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA}, |
| 140 | /* abort */ {sIV, sCL, sCL, sCL, sCL, sCL, sCL, sCL}, |
| 141 | /* shutdown */ {sIV, sCL, sCW, sCE, sSR, sSS, sSR, sSA}, |
| 142 | /* shutdown_ack */ {sIV, sCL, sCW, sCE, sES, sSA, sSA, sSA}, |
| 143 | /* error */ {sIV, sCL, sCW, sCL, sES, sSS, sSR, sSA}, |
| 144 | /* cookie_echo */ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sSA},/* Cant come in reply dir */ |
| 145 | /* cookie_ack */ {sIV, sCL, sCW, sES, sES, sSS, sSR, sSA}, |
| 146 | /* shutdown_comp*/ {sIV, sCL, sCW, sCE, sES, sSS, sSR, sCL} |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | static int sctp_pkt_to_tuple(const struct sk_buff *skb, |
| 151 | unsigned int dataoff, |
| 152 | struct ip_conntrack_tuple *tuple) |
| 153 | { |
| 154 | sctp_sctphdr_t _hdr, *hp; |
| 155 | |
| 156 | DEBUGP(__FUNCTION__); |
| 157 | DEBUGP("\n"); |
| 158 | |
| 159 | /* Actually only need first 8 bytes. */ |
| 160 | hp = skb_header_pointer(skb, dataoff, 8, &_hdr); |
| 161 | if (hp == NULL) |
| 162 | return 0; |
| 163 | |
| 164 | tuple->src.u.sctp.port = hp->source; |
| 165 | tuple->dst.u.sctp.port = hp->dest; |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | static int sctp_invert_tuple(struct ip_conntrack_tuple *tuple, |
| 170 | const struct ip_conntrack_tuple *orig) |
| 171 | { |
| 172 | DEBUGP(__FUNCTION__); |
| 173 | DEBUGP("\n"); |
| 174 | |
| 175 | tuple->src.u.sctp.port = orig->dst.u.sctp.port; |
| 176 | tuple->dst.u.sctp.port = orig->src.u.sctp.port; |
| 177 | return 1; |
| 178 | } |
| 179 | |
| 180 | /* Print out the per-protocol part of the tuple. */ |
| 181 | static int sctp_print_tuple(struct seq_file *s, |
| 182 | const struct ip_conntrack_tuple *tuple) |
| 183 | { |
| 184 | DEBUGP(__FUNCTION__); |
| 185 | DEBUGP("\n"); |
| 186 | |
| 187 | return seq_printf(s, "sport=%hu dport=%hu ", |
| 188 | ntohs(tuple->src.u.sctp.port), |
| 189 | ntohs(tuple->dst.u.sctp.port)); |
| 190 | } |
| 191 | |
| 192 | /* Print out the private part of the conntrack. */ |
| 193 | static int sctp_print_conntrack(struct seq_file *s, |
| 194 | const struct ip_conntrack *conntrack) |
| 195 | { |
| 196 | enum sctp_conntrack state; |
| 197 | |
| 198 | DEBUGP(__FUNCTION__); |
| 199 | DEBUGP("\n"); |
| 200 | |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 201 | read_lock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | state = conntrack->proto.sctp.state; |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 203 | read_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | return seq_printf(s, "%s ", sctp_conntrack_names[state]); |
| 206 | } |
| 207 | |
| 208 | #define for_each_sctp_chunk(skb, sch, _sch, offset, count) \ |
| 209 | for (offset = skb->nh.iph->ihl * 4 + sizeof(sctp_sctphdr_t), count = 0; \ |
| 210 | offset < skb->len && \ |
| 211 | (sch = skb_header_pointer(skb, offset, sizeof(_sch), &_sch)); \ |
Al Viro | cdcb71b | 2006-09-28 14:21:37 -0700 | [diff] [blame] | 212 | offset += (ntohs(sch->length) + 3) & ~3, count++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | /* Some validity checks to make sure the chunks are fine */ |
| 215 | static int do_basic_checks(struct ip_conntrack *conntrack, |
| 216 | const struct sk_buff *skb, |
| 217 | char *map) |
| 218 | { |
| 219 | u_int32_t offset, count; |
| 220 | sctp_chunkhdr_t _sch, *sch; |
| 221 | int flag; |
| 222 | |
| 223 | DEBUGP(__FUNCTION__); |
| 224 | DEBUGP("\n"); |
| 225 | |
| 226 | flag = 0; |
| 227 | |
| 228 | for_each_sctp_chunk (skb, sch, _sch, offset, count) { |
| 229 | DEBUGP("Chunk Num: %d Type: %d\n", count, sch->type); |
| 230 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 231 | if (sch->type == SCTP_CID_INIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | || sch->type == SCTP_CID_INIT_ACK |
| 233 | || sch->type == SCTP_CID_SHUTDOWN_COMPLETE) { |
| 234 | flag = 1; |
| 235 | } |
| 236 | |
Patrick McHardy | e17df68 | 2006-05-02 23:23:07 +0200 | [diff] [blame] | 237 | /* |
| 238 | * Cookie Ack/Echo chunks not the first OR |
| 239 | * Init / Init Ack / Shutdown compl chunks not the only chunks |
| 240 | * OR zero-length. |
| 241 | */ |
| 242 | if (((sch->type == SCTP_CID_COOKIE_ACK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | || sch->type == SCTP_CID_COOKIE_ECHO |
| 244 | || flag) |
Patrick McHardy | e17df68 | 2006-05-02 23:23:07 +0200 | [diff] [blame] | 245 | && count !=0) || !sch->length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | DEBUGP("Basic checks failed\n"); |
| 247 | return 1; |
| 248 | } |
| 249 | |
| 250 | if (map) { |
| 251 | set_bit(sch->type, (void *)map); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | DEBUGP("Basic checks passed\n"); |
Patrick McHardy | dd7271f | 2006-06-29 21:40:23 -0700 | [diff] [blame] | 256 | return count == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static int new_state(enum ip_conntrack_dir dir, |
| 260 | enum sctp_conntrack cur_state, |
| 261 | int chunk_type) |
| 262 | { |
| 263 | int i; |
| 264 | |
| 265 | DEBUGP(__FUNCTION__); |
| 266 | DEBUGP("\n"); |
| 267 | |
| 268 | DEBUGP("Chunk type: %d\n", chunk_type); |
| 269 | |
| 270 | switch (chunk_type) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 271 | case SCTP_CID_INIT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | DEBUGP("SCTP_CID_INIT\n"); |
| 273 | i = 0; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 274 | case SCTP_CID_INIT_ACK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | DEBUGP("SCTP_CID_INIT_ACK\n"); |
| 276 | i = 1; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 277 | case SCTP_CID_ABORT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | DEBUGP("SCTP_CID_ABORT\n"); |
| 279 | i = 2; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 280 | case SCTP_CID_SHUTDOWN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | DEBUGP("SCTP_CID_SHUTDOWN\n"); |
| 282 | i = 3; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 283 | case SCTP_CID_SHUTDOWN_ACK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | DEBUGP("SCTP_CID_SHUTDOWN_ACK\n"); |
| 285 | i = 4; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 286 | case SCTP_CID_ERROR: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | DEBUGP("SCTP_CID_ERROR\n"); |
| 288 | i = 5; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 289 | case SCTP_CID_COOKIE_ECHO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | DEBUGP("SCTP_CID_COOKIE_ECHO\n"); |
| 291 | i = 6; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 292 | case SCTP_CID_COOKIE_ACK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | DEBUGP("SCTP_CID_COOKIE_ACK\n"); |
| 294 | i = 7; break; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 295 | case SCTP_CID_SHUTDOWN_COMPLETE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | DEBUGP("SCTP_CID_SHUTDOWN_COMPLETE\n"); |
| 297 | i = 8; break; |
| 298 | default: |
| 299 | /* Other chunks like DATA, SACK, HEARTBEAT and |
| 300 | its ACK do not cause a change in state */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 301 | DEBUGP("Unknown chunk type, Will stay in %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | sctp_conntrack_names[cur_state]); |
| 303 | return cur_state; |
| 304 | } |
| 305 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 306 | DEBUGP("dir: %d cur_state: %s chunk_type: %d new_state: %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | dir, sctp_conntrack_names[cur_state], chunk_type, |
| 308 | sctp_conntrack_names[sctp_conntracks[dir][i][cur_state]]); |
| 309 | |
| 310 | return sctp_conntracks[dir][i][cur_state]; |
| 311 | } |
| 312 | |
| 313 | /* Returns verdict for packet, or -1 for invalid. */ |
| 314 | static int sctp_packet(struct ip_conntrack *conntrack, |
| 315 | const struct sk_buff *skb, |
| 316 | enum ip_conntrack_info ctinfo) |
| 317 | { |
| 318 | enum sctp_conntrack newconntrack, oldsctpstate; |
| 319 | struct iphdr *iph = skb->nh.iph; |
| 320 | sctp_sctphdr_t _sctph, *sh; |
| 321 | sctp_chunkhdr_t _sch, *sch; |
| 322 | u_int32_t offset, count; |
| 323 | char map[256 / sizeof (char)] = {0}; |
| 324 | |
| 325 | DEBUGP(__FUNCTION__); |
| 326 | DEBUGP("\n"); |
| 327 | |
| 328 | sh = skb_header_pointer(skb, iph->ihl * 4, sizeof(_sctph), &_sctph); |
| 329 | if (sh == NULL) |
| 330 | return -1; |
| 331 | |
| 332 | if (do_basic_checks(conntrack, skb, map) != 0) |
| 333 | return -1; |
| 334 | |
| 335 | /* Check the verification tag (Sec 8.5) */ |
| 336 | if (!test_bit(SCTP_CID_INIT, (void *)map) |
| 337 | && !test_bit(SCTP_CID_SHUTDOWN_COMPLETE, (void *)map) |
| 338 | && !test_bit(SCTP_CID_COOKIE_ECHO, (void *)map) |
| 339 | && !test_bit(SCTP_CID_ABORT, (void *)map) |
| 340 | && !test_bit(SCTP_CID_SHUTDOWN_ACK, (void *)map) |
| 341 | && (sh->vtag != conntrack->proto.sctp.vtag[CTINFO2DIR(ctinfo)])) { |
| 342 | DEBUGP("Verification tag check failed\n"); |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | oldsctpstate = newconntrack = SCTP_CONNTRACK_MAX; |
| 347 | for_each_sctp_chunk (skb, sch, _sch, offset, count) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 348 | write_lock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | /* Special cases of Verification tag check (Sec 8.5.1) */ |
| 351 | if (sch->type == SCTP_CID_INIT) { |
| 352 | /* Sec 8.5.1 (A) */ |
| 353 | if (sh->vtag != 0) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 354 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | } else if (sch->type == SCTP_CID_ABORT) { |
| 358 | /* Sec 8.5.1 (B) */ |
| 359 | if (!(sh->vtag == conntrack->proto.sctp.vtag[CTINFO2DIR(ctinfo)]) |
| 360 | && !(sh->vtag == conntrack->proto.sctp.vtag |
| 361 | [1 - CTINFO2DIR(ctinfo)])) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 362 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | return -1; |
| 364 | } |
| 365 | } else if (sch->type == SCTP_CID_SHUTDOWN_COMPLETE) { |
| 366 | /* Sec 8.5.1 (C) */ |
| 367 | if (!(sh->vtag == conntrack->proto.sctp.vtag[CTINFO2DIR(ctinfo)]) |
| 368 | && !(sh->vtag == conntrack->proto.sctp.vtag |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 369 | [1 - CTINFO2DIR(ctinfo)] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | && (sch->flags & 1))) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 371 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return -1; |
| 373 | } |
| 374 | } else if (sch->type == SCTP_CID_COOKIE_ECHO) { |
| 375 | /* Sec 8.5.1 (D) */ |
| 376 | if (!(sh->vtag == conntrack->proto.sctp.vtag[CTINFO2DIR(ctinfo)])) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 377 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | oldsctpstate = conntrack->proto.sctp.state; |
| 383 | newconntrack = new_state(CTINFO2DIR(ctinfo), oldsctpstate, sch->type); |
| 384 | |
| 385 | /* Invalid */ |
| 386 | if (newconntrack == SCTP_CONNTRACK_MAX) { |
| 387 | DEBUGP("ip_conntrack_sctp: Invalid dir=%i ctype=%u conntrack=%u\n", |
| 388 | CTINFO2DIR(ctinfo), sch->type, oldsctpstate); |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 389 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | /* If it is an INIT or an INIT ACK note down the vtag */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 394 | if (sch->type == SCTP_CID_INIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | || sch->type == SCTP_CID_INIT_ACK) { |
| 396 | sctp_inithdr_t _inithdr, *ih; |
| 397 | |
| 398 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 399 | sizeof(_inithdr), &_inithdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | if (ih == NULL) { |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 401 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | return -1; |
| 403 | } |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 404 | DEBUGP("Setting vtag %x for dir %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | ih->init_tag, !CTINFO2DIR(ctinfo)); |
| 406 | conntrack->proto.sctp.vtag[!CTINFO2DIR(ctinfo)] = ih->init_tag; |
| 407 | } |
| 408 | |
| 409 | conntrack->proto.sctp.state = newconntrack; |
Harald Welte | ac3247b | 2005-08-09 19:28:03 -0700 | [diff] [blame] | 410 | if (oldsctpstate != newconntrack) |
| 411 | ip_conntrack_event_cache(IPCT_PROTOINFO, skb); |
Patrick McHardy | e45b1be | 2005-06-21 14:01:30 -0700 | [diff] [blame] | 412 | write_unlock_bh(&sctp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | ip_ct_refresh_acct(conntrack, ctinfo, skb, *sctp_timeouts[newconntrack]); |
| 416 | |
| 417 | if (oldsctpstate == SCTP_CONNTRACK_COOKIE_ECHOED |
| 418 | && CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY |
| 419 | && newconntrack == SCTP_CONNTRACK_ESTABLISHED) { |
| 420 | DEBUGP("Setting assured bit\n"); |
| 421 | set_bit(IPS_ASSURED_BIT, &conntrack->status); |
Harald Welte | 8ddec74 | 2005-09-24 16:56:08 -0700 | [diff] [blame] | 422 | ip_conntrack_event_cache(IPCT_STATUS, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | return NF_ACCEPT; |
| 426 | } |
| 427 | |
| 428 | /* Called when a new connection for this protocol found. */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 429 | static int sctp_new(struct ip_conntrack *conntrack, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | const struct sk_buff *skb) |
| 431 | { |
| 432 | enum sctp_conntrack newconntrack; |
| 433 | struct iphdr *iph = skb->nh.iph; |
| 434 | sctp_sctphdr_t _sctph, *sh; |
| 435 | sctp_chunkhdr_t _sch, *sch; |
| 436 | u_int32_t offset, count; |
| 437 | char map[256 / sizeof (char)] = {0}; |
| 438 | |
| 439 | DEBUGP(__FUNCTION__); |
| 440 | DEBUGP("\n"); |
| 441 | |
| 442 | sh = skb_header_pointer(skb, iph->ihl * 4, sizeof(_sctph), &_sctph); |
| 443 | if (sh == NULL) |
| 444 | return 0; |
| 445 | |
| 446 | if (do_basic_checks(conntrack, skb, map) != 0) |
| 447 | return 0; |
| 448 | |
| 449 | /* If an OOTB packet has any of these chunks discard (Sec 8.4) */ |
| 450 | if ((test_bit (SCTP_CID_ABORT, (void *)map)) |
| 451 | || (test_bit (SCTP_CID_SHUTDOWN_COMPLETE, (void *)map)) |
| 452 | || (test_bit (SCTP_CID_COOKIE_ACK, (void *)map))) { |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | newconntrack = SCTP_CONNTRACK_MAX; |
| 457 | for_each_sctp_chunk (skb, sch, _sch, offset, count) { |
| 458 | /* Don't need lock here: this conntrack not in circulation yet */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 459 | newconntrack = new_state (IP_CT_DIR_ORIGINAL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | SCTP_CONNTRACK_NONE, sch->type); |
| 461 | |
| 462 | /* Invalid: delete conntrack */ |
| 463 | if (newconntrack == SCTP_CONNTRACK_MAX) { |
| 464 | DEBUGP("ip_conntrack_sctp: invalid new deleting.\n"); |
| 465 | return 0; |
| 466 | } |
| 467 | |
| 468 | /* Copy the vtag into the state info */ |
| 469 | if (sch->type == SCTP_CID_INIT) { |
| 470 | if (sh->vtag == 0) { |
| 471 | sctp_inithdr_t _inithdr, *ih; |
| 472 | |
| 473 | ih = skb_header_pointer(skb, offset + sizeof(sctp_chunkhdr_t), |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 474 | sizeof(_inithdr), &_inithdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (ih == NULL) |
| 476 | return 0; |
| 477 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 478 | DEBUGP("Setting vtag %x for new conn\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | ih->init_tag); |
| 480 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 481 | conntrack->proto.sctp.vtag[IP_CT_DIR_REPLY] = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | ih->init_tag; |
| 483 | } else { |
| 484 | /* Sec 8.5.1 (A) */ |
| 485 | return 0; |
| 486 | } |
| 487 | } |
| 488 | /* If it is a shutdown ack OOTB packet, we expect a return |
| 489 | shutdown complete, otherwise an ABORT Sec 8.4 (5) and (8) */ |
| 490 | else { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 491 | DEBUGP("Setting vtag %x for new conn OOTB\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | sh->vtag); |
| 493 | conntrack->proto.sctp.vtag[IP_CT_DIR_REPLY] = sh->vtag; |
| 494 | } |
| 495 | |
| 496 | conntrack->proto.sctp.state = newconntrack; |
| 497 | } |
| 498 | |
| 499 | return 1; |
| 500 | } |
| 501 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 502 | static struct ip_conntrack_protocol ip_conntrack_protocol_sctp = { |
| 503 | .proto = IPPROTO_SCTP, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | .name = "sctp", |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 505 | .pkt_to_tuple = sctp_pkt_to_tuple, |
| 506 | .invert_tuple = sctp_invert_tuple, |
| 507 | .print_tuple = sctp_print_tuple, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | .print_conntrack = sctp_print_conntrack, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 509 | .packet = sctp_packet, |
| 510 | .new = sctp_new, |
| 511 | .destroy = NULL, |
Harald Welte | 080774a | 2005-08-09 19:32:58 -0700 | [diff] [blame] | 512 | .me = THIS_MODULE, |
| 513 | #if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \ |
| 514 | defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE) |
| 515 | .tuple_to_nfattr = ip_ct_port_tuple_to_nfattr, |
| 516 | .nfattr_to_tuple = ip_ct_port_nfattr_to_tuple, |
| 517 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | }; |
| 519 | |
| 520 | #ifdef CONFIG_SYSCTL |
| 521 | static ctl_table ip_ct_sysctl_table[] = { |
| 522 | { |
| 523 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_CLOSED, |
| 524 | .procname = "ip_conntrack_sctp_timeout_closed", |
| 525 | .data = &ip_ct_sctp_timeout_closed, |
| 526 | .maxlen = sizeof(unsigned int), |
| 527 | .mode = 0644, |
| 528 | .proc_handler = &proc_dointvec_jiffies, |
| 529 | }, |
| 530 | { |
| 531 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_COOKIE_WAIT, |
| 532 | .procname = "ip_conntrack_sctp_timeout_cookie_wait", |
| 533 | .data = &ip_ct_sctp_timeout_cookie_wait, |
| 534 | .maxlen = sizeof(unsigned int), |
| 535 | .mode = 0644, |
| 536 | .proc_handler = &proc_dointvec_jiffies, |
| 537 | }, |
| 538 | { |
| 539 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_COOKIE_ECHOED, |
| 540 | .procname = "ip_conntrack_sctp_timeout_cookie_echoed", |
| 541 | .data = &ip_ct_sctp_timeout_cookie_echoed, |
| 542 | .maxlen = sizeof(unsigned int), |
| 543 | .mode = 0644, |
| 544 | .proc_handler = &proc_dointvec_jiffies, |
| 545 | }, |
| 546 | { |
| 547 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_ESTABLISHED, |
| 548 | .procname = "ip_conntrack_sctp_timeout_established", |
| 549 | .data = &ip_ct_sctp_timeout_established, |
| 550 | .maxlen = sizeof(unsigned int), |
| 551 | .mode = 0644, |
| 552 | .proc_handler = &proc_dointvec_jiffies, |
| 553 | }, |
| 554 | { |
| 555 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_SHUTDOWN_SENT, |
| 556 | .procname = "ip_conntrack_sctp_timeout_shutdown_sent", |
| 557 | .data = &ip_ct_sctp_timeout_shutdown_sent, |
| 558 | .maxlen = sizeof(unsigned int), |
| 559 | .mode = 0644, |
| 560 | .proc_handler = &proc_dointvec_jiffies, |
| 561 | }, |
| 562 | { |
| 563 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_SHUTDOWN_RECD, |
| 564 | .procname = "ip_conntrack_sctp_timeout_shutdown_recd", |
| 565 | .data = &ip_ct_sctp_timeout_shutdown_recd, |
| 566 | .maxlen = sizeof(unsigned int), |
| 567 | .mode = 0644, |
| 568 | .proc_handler = &proc_dointvec_jiffies, |
| 569 | }, |
| 570 | { |
| 571 | .ctl_name = NET_IPV4_NF_CONNTRACK_SCTP_TIMEOUT_SHUTDOWN_ACK_SENT, |
| 572 | .procname = "ip_conntrack_sctp_timeout_shutdown_ack_sent", |
| 573 | .data = &ip_ct_sctp_timeout_shutdown_ack_sent, |
| 574 | .maxlen = sizeof(unsigned int), |
| 575 | .mode = 0644, |
| 576 | .proc_handler = &proc_dointvec_jiffies, |
| 577 | }, |
| 578 | { .ctl_name = 0 } |
| 579 | }; |
| 580 | |
| 581 | static ctl_table ip_ct_netfilter_table[] = { |
| 582 | { |
| 583 | .ctl_name = NET_IPV4_NETFILTER, |
| 584 | .procname = "netfilter", |
| 585 | .mode = 0555, |
| 586 | .child = ip_ct_sysctl_table, |
| 587 | }, |
| 588 | { .ctl_name = 0 } |
| 589 | }; |
| 590 | |
| 591 | static ctl_table ip_ct_ipv4_table[] = { |
| 592 | { |
| 593 | .ctl_name = NET_IPV4, |
| 594 | .procname = "ipv4", |
| 595 | .mode = 0555, |
| 596 | .child = ip_ct_netfilter_table, |
| 597 | }, |
| 598 | { .ctl_name = 0 } |
| 599 | }; |
| 600 | |
| 601 | static ctl_table ip_ct_net_table[] = { |
| 602 | { |
| 603 | .ctl_name = CTL_NET, |
| 604 | .procname = "net", |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 605 | .mode = 0555, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | .child = ip_ct_ipv4_table, |
| 607 | }, |
| 608 | { .ctl_name = 0 } |
| 609 | }; |
| 610 | |
| 611 | static struct ctl_table_header *ip_ct_sysctl_header; |
| 612 | #endif |
| 613 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 614 | static int __init ip_conntrack_proto_sctp_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
| 616 | int ret; |
| 617 | |
| 618 | ret = ip_conntrack_protocol_register(&ip_conntrack_protocol_sctp); |
| 619 | if (ret) { |
| 620 | printk("ip_conntrack_proto_sctp: protocol register failed\n"); |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | #ifdef CONFIG_SYSCTL |
| 625 | ip_ct_sysctl_header = register_sysctl_table(ip_ct_net_table, 0); |
| 626 | if (ip_ct_sysctl_header == NULL) { |
| 627 | ret = -ENOMEM; |
| 628 | printk("ip_conntrack_proto_sctp: can't register to sysctl.\n"); |
| 629 | goto cleanup; |
| 630 | } |
| 631 | #endif |
| 632 | |
| 633 | return ret; |
| 634 | |
| 635 | #ifdef CONFIG_SYSCTL |
| 636 | cleanup: |
| 637 | ip_conntrack_protocol_unregister(&ip_conntrack_protocol_sctp); |
| 638 | #endif |
| 639 | out: |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 640 | DEBUGP("SCTP conntrack module loading %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | ret ? "failed": "succeeded"); |
| 642 | return ret; |
| 643 | } |
| 644 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 645 | static void __exit ip_conntrack_proto_sctp_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
| 647 | ip_conntrack_protocol_unregister(&ip_conntrack_protocol_sctp); |
| 648 | #ifdef CONFIG_SYSCTL |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 649 | unregister_sysctl_table(ip_ct_sysctl_header); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | #endif |
| 651 | DEBUGP("SCTP conntrack module unloaded\n"); |
| 652 | } |
| 653 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 654 | module_init(ip_conntrack_proto_sctp_init); |
| 655 | module_exit(ip_conntrack_proto_sctp_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
| 657 | MODULE_LICENSE("GPL"); |
| 658 | MODULE_AUTHOR("Kiran Kumar Immidi"); |
| 659 | MODULE_DESCRIPTION("Netfilter connection tracking protocol helper for SCTP"); |